|
| 1 | +// Copyright (c) 2024 The pybind Community. |
| 2 | + |
| 3 | +#pragma once |
| 4 | + |
| 5 | +#include <pybind11/pytypes.h> |
| 6 | + |
| 7 | +#include "common.h" |
| 8 | +#include "internals.h" |
| 9 | + |
| 10 | +#include <typeinfo> |
| 11 | + |
| 12 | +PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE) |
| 13 | +PYBIND11_NAMESPACE_BEGIN(detail) |
| 14 | + |
| 15 | +// Forward declaration needed here: Refactoring opportunity. |
| 16 | +extern "C" inline PyObject *pybind11_object_new(PyTypeObject *type, PyObject *, PyObject *); |
| 17 | + |
| 18 | +inline bool type_is_managed_by_our_internals(PyTypeObject *type_obj) { |
| 19 | +#if defined(PYPY_VERSION) |
| 20 | + auto &internals = get_internals(); |
| 21 | + return bool(internals.registered_types_py.find(type_obj) |
| 22 | + != internals.registered_types_py.end()); |
| 23 | +#else |
| 24 | + return bool(type_obj->tp_new == pybind11_object_new); |
| 25 | +#endif |
| 26 | +} |
| 27 | + |
| 28 | +inline bool is_instance_method_of_type(PyTypeObject *type_obj, PyObject *attr_name) { |
| 29 | + PyObject *descr = _PyType_Lookup(type_obj, attr_name); |
| 30 | + return bool((descr != nullptr) && PyInstanceMethod_Check(descr)); |
| 31 | +} |
| 32 | + |
| 33 | +inline object try_get_cpp_conduit_method(PyObject *obj) { |
| 34 | + if (PyType_Check(obj)) { |
| 35 | + return object(); |
| 36 | + } |
| 37 | + PyTypeObject *type_obj = Py_TYPE(obj); |
| 38 | + str attr_name("_pybind11_conduit_v1_"); |
| 39 | + bool assumed_to_be_callable = false; |
| 40 | + if (type_is_managed_by_our_internals(type_obj)) { |
| 41 | + if (!is_instance_method_of_type(type_obj, attr_name.ptr())) { |
| 42 | + return object(); |
| 43 | + } |
| 44 | + assumed_to_be_callable = true; |
| 45 | + } |
| 46 | + PyObject *method = PyObject_GetAttr(obj, attr_name.ptr()); |
| 47 | + if (method == nullptr) { |
| 48 | + PyErr_Clear(); |
| 49 | + return object(); |
| 50 | + } |
| 51 | + if (!assumed_to_be_callable && PyCallable_Check(method) == 0) { |
| 52 | + Py_DECREF(method); |
| 53 | + return object(); |
| 54 | + } |
| 55 | + return reinterpret_steal<object>(method); |
| 56 | +} |
| 57 | + |
| 58 | +inline void *try_raw_pointer_ephemeral_from_cpp_conduit(handle src, |
| 59 | + const std::type_info *cpp_type_info) { |
| 60 | + object method = try_get_cpp_conduit_method(src.ptr()); |
| 61 | + if (method) { |
| 62 | + capsule cpp_type_info_capsule(const_cast<void *>(static_cast<const void *>(cpp_type_info)), |
| 63 | + typeid(std::type_info).name()); |
| 64 | + object cpp_conduit = method(bytes(PYBIND11_PLATFORM_ABI_ID), |
| 65 | + cpp_type_info_capsule, |
| 66 | + bytes("raw_pointer_ephemeral")); |
| 67 | + if (isinstance<capsule>(cpp_conduit)) { |
| 68 | + return reinterpret_borrow<capsule>(cpp_conduit).get_pointer(); |
| 69 | + } |
| 70 | + } |
| 71 | + return nullptr; |
| 72 | +} |
| 73 | + |
| 74 | +#define PYBIND11_HAS_CPP_CONDUIT 1 |
| 75 | + |
| 76 | +PYBIND11_NAMESPACE_END(detail) |
| 77 | +PYBIND11_NAMESPACE_END(PYBIND11_NAMESPACE) |
0 commit comments