Skip to content

Commit

Permalink
.__cpp_transporter__() implementation: compare `pybind11_platform_a…
Browse files Browse the repository at this point in the history
…bi_id`, `cpp_typeid_name`
  • Loading branch information
Ralf W. Grosse-Kunstleve committed Aug 28, 2024
1 parent 59f3036 commit d2479aa
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 10 deletions.
4 changes: 3 additions & 1 deletion include/pybind11/detail/cpp_transporter.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "../pytypes.h"
#include "common.h"
#include "internals.h"
#include "platform_abi_id.h"

PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE)
PYBIND11_NAMESPACE_BEGIN(detail)
Expand Down Expand Up @@ -55,7 +56,8 @@ inline object try_get_cpp_transporter_method(PyObject *obj) {
inline void *try_raw_pointer_ephemeral_from_cpp_transporter(handle src, const char *typeid_name) {
object method = try_get_cpp_transporter_method(src.ptr());
if (method) {
object cpp_transporter = method("cpp_abi_code", typeid_name, "raw_pointer_ephemeral");
object cpp_transporter
= method(PYBIND11_PLATFORM_ABI_ID, typeid_name, "raw_pointer_ephemeral");
if (isinstance<capsule>(cpp_transporter)) {
return reinterpret_borrow<capsule>(cpp_transporter).get_pointer();
}
Expand Down
44 changes: 38 additions & 6 deletions tests/test_cpp_transporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,43 @@ def test_exo_passed_to_home():
assert home_planet.get_luggage(t_e) == "exo"


def test_call_cpp_transporter():
def test_call_cpp_transporter_success():
t_h = home_planet.Traveler("home")
assert (
t_h.__cpp_transporter__(
"cpp_abi_code", "cpp_typeid_name", "raw_pointer_ephemeral"
)
is not None
cap = t_h.__cpp_transporter__(
home_planet.PYBIND11_PLATFORM_ABI_ID,
home_planet.typeid_Traveler_name,
"raw_pointer_ephemeral",
)
assert cap.__class__.__name__ == "PyCapsule"


def test_call_cpp_transporter_platform_abi_id_mismatch():
t_h = home_planet.Traveler("home")
cap = t_h.__cpp_transporter__(
home_planet.PYBIND11_PLATFORM_ABI_ID + "MISMATCH",
home_planet.typeid_Traveler_name,
"raw_pointer_ephemeral",
)
assert cap is None
diag = t_h.__cpp_transporter__(
home_planet.PYBIND11_PLATFORM_ABI_ID + "MISMATCH",
home_planet.typeid_Traveler_name,
"query_mismatch",
)
assert diag == "pybind11_platform_abi_id_mismatch"


def test_call_cpp_transporter_type_id_name_mismatch():
t_h = home_planet.Traveler("home")
cap = t_h.__cpp_transporter__(
home_planet.PYBIND11_PLATFORM_ABI_ID,
home_planet.typeid_Traveler_name + "MISMATCH",
"raw_pointer_ephemeral",
)
assert cap is None
diag = t_h.__cpp_transporter__(
home_planet.PYBIND11_PLATFORM_ABI_ID,
home_planet.typeid_Traveler_name + "MISMATCH",
"query_mismatch",
)
assert diag == "cpp_typeid_name_mismatch"
22 changes: 19 additions & 3 deletions tests/test_cpp_transporter_traveler_bindings.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#include "test_cpp_transporter_traveler_type.h"

#include <cstring>
#include <string>

namespace pybind11_tests {
Expand All @@ -12,14 +13,29 @@ namespace test_cpp_transporter {
namespace py = pybind11;

inline void wrap_traveler(py::module_ m) {
m.attr("PYBIND11_PLATFORM_ABI_ID") = PYBIND11_PLATFORM_ABI_ID;
m.attr("typeid_Traveler_name") = typeid(Traveler).name();

py::class_<Traveler>(m, "Traveler")
.def(py::init<std::string>())
.def("__cpp_transporter__",
[](py::handle self,
const py::str & /*cpp_abi_code*/,
const py::str & /*cpp_typeid_name*/,
const py::str &pointer_kind) {
const py::str &pybind11_platform_abi_id,
const py::str &cpp_typeid_name,
const py::str &pointer_kind) -> py::object {
auto pointer_kind_cpp = pointer_kind.cast<std::string>();
if (pybind11_platform_abi_id.cast<std::string>() != PYBIND11_PLATFORM_ABI_ID) {
if (pointer_kind_cpp == "query_mismatch") {
return py::cast("pybind11_platform_abi_id_mismatch");
}
return py::none();
}
if (cpp_typeid_name.cast<std::string>() != typeid(Traveler).name()) {
if (pointer_kind_cpp == "query_mismatch") {
return py::cast("cpp_typeid_name_mismatch");
}
return py::none();
}
if (pointer_kind_cpp != "raw_pointer_ephemeral") {
throw std::runtime_error("Unknown pointer_kind: \"" + pointer_kind_cpp
+ "\"");
Expand Down

0 comments on commit d2479aa

Please sign in to comment.