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 GlobalState::PyFastCppProtoMessageInstance( const Descriptor* descriptor) { assert(descriptor != nullptr); @@ -361,6 +369,7 @@ std::pair 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( @@ -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 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,