1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
|
diff --git a/pybind11_protobuf/BUILD b/pybind11_protobuf/BUILD
index b62eb91..1856ad3 100644
--- a/pybind11_protobuf/BUILD
+++ b/pybind11_protobuf/BUILD
@@ -61,9 +61,11 @@ pybind_library(
"@com_google_absl//absl/container:flat_hash_map",
"@com_google_absl//absl/strings",
"@com_google_absl//absl/types:optional",
- "@com_google_protobuf//:proto_api",
"@com_google_protobuf//:protobuf",
- ],
+ ] + select({
+ ":enable_pyproto_api_setting": ["@com_google_protobuf//:proto_api"],
+ "//conditions:default": [],
+ }),
)
pybind_library(
diff --git a/pybind11_protobuf/proto_cast_util.cc b/pybind11_protobuf/proto_cast_util.cc
index 52c57c9..771e688 100644
--- a/pybind11_protobuf/proto_cast_util.cc
+++ b/pybind11_protobuf/proto_cast_util.cc
@@ -14,9 +14,12 @@
#include "google/protobuf/descriptor.pb.h"
#include "google/protobuf/descriptor.h"
+#include "google/protobuf/descriptor_database.h"
#include "google/protobuf/dynamic_message.h"
#include "google/protobuf/message.h"
+#if defined(PYBIND11_PROTOBUF_ENABLE_PYPROTO_API)
#include "python/google/protobuf/proto_api.h"
+#endif
#include "absl/container/flat_hash_map.h"
#include "absl/strings/numbers.h"
#include "absl/strings/str_replace.h"
@@ -35,8 +38,12 @@ using ::google::protobuf::FileDescriptor;
using ::google::protobuf::FileDescriptorProto;
using ::google::protobuf::Message;
using ::google::protobuf::MessageFactory;
+#if defined(PYBIND11_PROTOBUF_ENABLE_PYPROTO_API)
using ::google::protobuf::python::PyProto_API;
using ::google::protobuf::python::PyProtoAPICapsuleName;
+#else
+struct PyProto_API;
+#endif
namespace pybind11_protobuf {
namespace {
@@ -321,6 +328,7 @@ py::object GlobalState::PyMessageInstance(const Descriptor* descriptor) {
module_name + "?");
}
+#if defined(PYBIND11_PROTOBUF_ENABLE_PYPROTO_API)
std::pair<py::object, Message*> GlobalState::PyFastCppProtoMessageInstance(
const Descriptor* descriptor) {
assert(descriptor != nullptr);
@@ -361,6 +369,7 @@ std::pair<py::object, Message*> GlobalState::PyFastCppProtoMessageInstance(
}
return {std::move(result), message};
}
+#endif
// Create C++ DescriptorPools based on Python DescriptorPools.
// The Python pool will provide message definitions when they are needed.
@@ -500,6 +509,7 @@ class PythonDescriptorPoolWrapper {
private:
bool CopyToFileDescriptorProto(py::handle py_file_descriptor,
FileDescriptorProto* output) {
+#if defined(PYBIND11_PROTOBUF_ENABLE_PYPROTO_API)
if (GlobalState::instance()->py_proto_api()) {
try {
py::object c_proto = py::reinterpret_steal<py::object>(
@@ -518,6 +528,7 @@ class PythonDescriptorPoolWrapper {
PyErr_Print();
}
}
+#endif
py::object wire = py_file_descriptor.attr("serialized_pb");
const char* bytes = PYBIND11_BYTES_AS_STRING(wire.ptr());
@@ -561,6 +572,9 @@ void ImportProtoDescriptorModule(const Descriptor* descriptor) {
const Message* PyProtoGetCppMessagePointer(py::handle src) {
assert(PyGILState_Check());
+#ifndef PYBIND11_PROTOBUF_ENABLE_PYPROTO_API
+ return nullptr;
+#else
if (!GlobalState::instance()->py_proto_api()) return nullptr;
auto* ptr =
GlobalState::instance()->py_proto_api()->GetMessagePointer(src.ptr());
@@ -571,6 +585,7 @@ const Message* PyProtoGetCppMessagePointer(py::handle src) {
return nullptr;
}
return ptr;
+#endif
}
absl::optional<std::string> PyProtoDescriptorName(py::handle py_proto) {
@@ -732,6 +747,9 @@ py::handle GenericPyProtoCast(Message* src, py::return_value_policy policy,
py::handle GenericFastCppProtoCast(Message* src, py::return_value_policy policy,
py::handle parent, bool is_const) {
+#ifndef PYBIND11_PROTOBUF_ENABLE_PYPROTO_API
+ throw std::logic_error("Not implemented");
+#else
assert(policy != pybind11::return_value_policy::automatic);
assert(policy != pybind11::return_value_policy::automatic_reference);
assert(src != nullptr);
@@ -802,6 +820,7 @@ py::handle GenericFastCppProtoCast(Message* src, py::return_value_policy policy,
std::string message("pybind11_protobuf unhandled return_value_policy::");
throw py::cast_error(message + ReturnValuePolicyName(policy));
}
+#endif
}
py::handle GenericProtoCast(Message* src, py::return_value_policy policy,
|