Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

For internal testing only (flop TGP-head 2024-25-25). #176

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions pybind11_protobuf/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ pybind_library(
":check_unknown_fields",
":enum_type_caster",
":proto_cast_util",
"//net/proto:rawmessage",
"@com_google_absl//absl/strings:string_view",
"@com_google_protobuf//:protobuf",
],
Expand Down
17 changes: 13 additions & 4 deletions pybind11_protobuf/native_proto_caster.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include <type_traits>
#include <utility>

#include "net/proto/rawmessage.h"
#include "google/protobuf/message.h"
#include "absl/strings/string_view.h"
#include "pybind11_protobuf/enum_type_caster.h"
Expand Down Expand Up @@ -80,6 +81,10 @@ namespace detail {
//
constexpr bool pybind11_protobuf_enable_type_caster(...) { return true; }

constexpr bool pybind11_protobuf_enable_type_caster(::RawMessage *) {
return false;
}

// pybind11 type_caster<> specialization for c++ protocol buffer types using
// inheritance from google::proto_caster<>.
template <typename ProtoType>
Expand All @@ -95,13 +100,17 @@ struct type_caster<

template <typename ProtoType>
struct copyable_holder_caster_shared_ptr_with_smart_holder_support_enabled<
ProtoType, enable_if_t<std::is_base_of<::google::protobuf::Message, ProtoType>::value>>
: std::false_type {};
ProtoType,
enable_if_t<(std::is_base_of<::google::protobuf::Message, ProtoType>::value &&
pybind11_protobuf_enable_type_caster(
static_cast<ProtoType *>(nullptr)))>> : std::false_type {};

template <typename ProtoType>
struct move_only_holder_caster_unique_ptr_with_smart_holder_support_enabled<
ProtoType, enable_if_t<std::is_base_of<::google::protobuf::Message, ProtoType>::value>>
: std::false_type {};
ProtoType,
enable_if_t<(std::is_base_of<::google::protobuf::Message, ProtoType>::value &&
pybind11_protobuf_enable_type_caster(
static_cast<ProtoType *>(nullptr)))>> : std::false_type {};

#endif // PYBIND11_HAS_INTERNALS_WITH_SMART_HOLDER_SUPPORT

Expand Down
20 changes: 19 additions & 1 deletion pybind11_protobuf/proto_caster_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,24 @@

namespace pybind11_protobuf {

namespace internal {

template <
typename To, typename Proto2Message = ::google::protobuf::Message,
std::enable_if<std::is_polymorphic<Proto2Message>::value, int>::type = 0>
const To *dynamic_cast_proxy(const ::google::protobuf::Message *message) {
return dynamic_cast<const To *>(message);
}

template <
typename To, typename Proto2Message = ::google::protobuf::Message,
std::enable_if<!std::is_polymorphic<Proto2Message>::value, int>::type = 0>
const To *dynamic_cast_proxy(const ::google::protobuf::Message *message) {
return ::google::protobuf::DynamicCastToGenerated<To>(message);
}

} // namespace internal

// pybind11 constructs c++ references using the following mechanism, for
// example:
//
Expand Down Expand Up @@ -55,7 +73,7 @@ struct proto_caster_load_impl {
const ::google::protobuf::Message *message =
pybind11_protobuf::PyProtoGetCppMessagePointer(src);
if (message) {
value = ::google::protobuf::DynamicCastToGenerated<ProtoType>(message);
value = internal::dynamic_cast_proxy<ProtoType>(message);
if (value) {
// If the capability were available, then we could probe PyProto_API and
// allow c++ mutability based on the python reference count.
Expand Down
9 changes: 9 additions & 0 deletions pybind11_protobuf/tests/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -358,3 +358,12 @@ py_test(
requirement("absl_py"),
],
)

pybind_extension(
name = "classh_rawmessage",
srcs = ["classh_rawmessage.cpp"],
deps = [
"//net/proto:rawmessage",
"//pybind11_protobuf:native_proto_caster",
],
)
27 changes: 27 additions & 0 deletions pybind11_protobuf/tests/classh_rawmessage.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
The code below compiled before https://github.com/pybind/pybind11/pull/5257
was merged into pybind11k, but with `PYBIND11_SMART_HOLDER_TYPE_CASTERS`
becoming a no-op, this error appears:

.../pybind11/include/pybind11/detail/../cast.h:72:30: error: no member named
'operator RawMessage *' in 'pybind11::detail::type_caster<RawMessage>' 72 |
return std::move(caster).operator result_t(); | ~~~~~~~~~~~~~~~~~ ^
*/

#include <pybind11/smart_holder.h>

#include "net/proto/rawmessage.h"
#include "pybind11_protobuf/native_proto_caster.h"

PYBIND11_SMART_HOLDER_TYPE_CASTERS(::RawMessage)

namespace py = pybind11;

PYBIND11_MODULE(rawmessage, m) {
py::classh<::RawMessage> w_t(m, "RawMessage", py::is_final());
w_t.def(py::init([]() { return std::make_unique<::RawMessage>().release(); }),
py::return_value_policy::_clif_automatic);
w_t.def("as_proto2_Message", [](::RawMessage* self) {
return py::capsule(static_cast<void*>(self));
});
}
Loading