Skip to content

Commit c169057

Browse files
committed
Factor out pybind11/compat/pybind11_conduit_v1.h
1 parent b116ec1 commit c169057

File tree

4 files changed

+53
-42
lines changed

4 files changed

+53
-42
lines changed

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@ set(PYBIND11_HEADERS
142142
include/pybind11/cast.h
143143
include/pybind11/chrono.h
144144
include/pybind11/common.h
145+
include/pybind11/compat/pybind11_conduit_v1.h
145146
include/pybind11/compat/pybind11_platform_abi_id.h
146147
include/pybind11/compat/wrap_include_python_h.h
147148
include/pybind11/complex.h
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// Copyright (c) 2024 The pybind Community.
2+
3+
// THIS MUST STAY AT THE TOP!
4+
#include "pybind11_platform_abi_id.h"
5+
6+
#include <Python.h>
7+
#include <typeinfo>
8+
9+
namespace pybind11_conduit_v1 {
10+
11+
inline void *get_raw_pointer_ephemeral(PyObject *py_obj, const std::type_info *cpp_type_info) {
12+
PyObject *cpp_type_info_capsule
13+
= PyCapsule_New(const_cast<void *>(static_cast<const void *>(cpp_type_info)),
14+
typeid(std::type_info).name(),
15+
nullptr);
16+
if (cpp_type_info_capsule == nullptr) {
17+
return nullptr;
18+
}
19+
PyObject *cpp_conduit = PyObject_CallMethod(py_obj,
20+
"_pybind11_conduit_v1_",
21+
"yOy",
22+
PYBIND11_PLATFORM_ABI_ID,
23+
cpp_type_info_capsule,
24+
"raw_pointer_ephemeral");
25+
Py_DECREF(cpp_type_info_capsule);
26+
if (cpp_conduit == nullptr) {
27+
return nullptr;
28+
}
29+
void *raw_ptr = PyCapsule_GetPointer(cpp_conduit, cpp_type_info->name());
30+
Py_DECREF(cpp_conduit);
31+
if (PyErr_Occurred()) {
32+
return nullptr;
33+
}
34+
return raw_ptr;
35+
}
36+
37+
template <typename T>
38+
T *get_type_pointer_ephemeral(PyObject *py_obj) {
39+
void *raw_ptr = get_raw_pointer_ephemeral(py_obj, &typeid(T));
40+
if (raw_ptr == nullptr) {
41+
return nullptr;
42+
}
43+
return static_cast<T *>(raw_ptr);
44+
}
45+
46+
} // namespace pybind11_conduit_v1

tests/exo_planet_c_api.cpp

Lines changed: 5 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,26 @@
11
// Copyright (c) 2024 The pybind Community.
22

33
// THIS MUST STAY AT THE TOP!
4-
#include <pybind11/compat/pybind11_platform_abi_id.h>
4+
#include <pybind11/compat/pybind11_conduit_v1.h> // VERY light-weight dependency.
55

66
#include "test_cpp_conduit_traveler_types.h"
77

88
#include <Python.h>
9-
#include <typeinfo>
109

1110
namespace {
1211

13-
void *get_cpp_conduit_void_ptr(PyObject *py_obj, const std::type_info *cpp_type_info) {
14-
PyObject *cpp_type_info_capsule
15-
= PyCapsule_New(const_cast<void *>(static_cast<const void *>(cpp_type_info)),
16-
typeid(std::type_info).name(),
17-
nullptr);
18-
if (cpp_type_info_capsule == nullptr) {
19-
return nullptr;
20-
}
21-
PyObject *cpp_conduit = PyObject_CallMethod(py_obj,
22-
"_pybind11_conduit_v1_",
23-
"yOy",
24-
PYBIND11_PLATFORM_ABI_ID,
25-
cpp_type_info_capsule,
26-
"raw_pointer_ephemeral");
27-
Py_DECREF(cpp_type_info_capsule);
28-
if (cpp_conduit == nullptr) {
29-
return nullptr;
30-
}
31-
void *void_ptr = PyCapsule_GetPointer(cpp_conduit, cpp_type_info->name());
32-
Py_DECREF(cpp_conduit);
33-
if (PyErr_Occurred()) {
34-
return nullptr;
35-
}
36-
return void_ptr;
37-
}
38-
39-
template <typename T>
40-
T *get_cpp_conduit_type_ptr(PyObject *py_obj) {
41-
void *void_ptr = get_cpp_conduit_void_ptr(py_obj, &typeid(T));
42-
if (void_ptr == nullptr) {
43-
return nullptr;
44-
}
45-
return static_cast<T *>(void_ptr);
46-
}
47-
4812
extern "C" PyObject *wrapGetLuggage(PyObject * /*self*/, PyObject *traveler) {
49-
const auto *cpp_traveler
50-
= get_cpp_conduit_type_ptr<pybind11_tests::test_cpp_conduit::Traveler>(traveler);
13+
const auto *cpp_traveler = pybind11_conduit_v1::get_type_pointer_ephemeral<
14+
pybind11_tests::test_cpp_conduit::Traveler>(traveler);
5115
if (cpp_traveler == nullptr) {
5216
return nullptr;
5317
}
5418
return PyUnicode_FromString(cpp_traveler->luggage.c_str());
5519
}
5620

5721
extern "C" PyObject *wrapGetPoints(PyObject * /*self*/, PyObject *premium_traveler) {
58-
const auto *cpp_premium_traveler
59-
= get_cpp_conduit_type_ptr<pybind11_tests::test_cpp_conduit::PremiumTraveler>(
60-
premium_traveler);
22+
const auto *cpp_premium_traveler = pybind11_conduit_v1::get_type_pointer_ephemeral<
23+
pybind11_tests::test_cpp_conduit::PremiumTraveler>(premium_traveler);
6124
if (cpp_premium_traveler == nullptr) {
6225
return nullptr;
6326
}

tests/extra_python_package/test_files.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
}
5252

5353
compat_headers = {
54+
"include/pybind11/compat/pybind11_conduit_v1.h",
5455
"include/pybind11/compat/pybind11_platform_abi_id.h",
5556
"include/pybind11/compat/wrap_include_python_h.h",
5657
}

0 commit comments

Comments
 (0)