@@ -36,72 +36,6 @@ struct is_smart_holder_type<smart_holder> : std::true_type {};
3636// SMART_HOLDER_WIP: Needs refactoring of existing pybind11 code.
3737inline void register_instance (instance *self, void *valptr, const type_info *tinfo);
3838inline bool deregister_instance (instance *self, void *valptr, const type_info *tinfo);
39- extern " C" inline PyObject *pybind11_object_new (PyTypeObject *type, PyObject *, PyObject *);
40-
41- // Replace all occurrences of substrings in a string.
42- inline void replace_all (std::string &str, const std::string &from, const std::string &to) {
43- if (str.empty ()) {
44- return ;
45- }
46- size_t pos = 0 ;
47- while ((pos = str.find (from, pos)) != std::string::npos) {
48- str.replace (pos, from.length (), to);
49- pos += to.length ();
50- }
51- }
52-
53- inline bool type_is_pybind11_class_ (PyTypeObject *type_obj) {
54- #if defined(PYPY_VERSION)
55- auto &internals = get_internals ();
56- return bool (internals.registered_types_py .find (type_obj)
57- != internals.registered_types_py .end ());
58- #else
59- return bool (type_obj->tp_new == pybind11_object_new);
60- #endif
61- }
62-
63- inline bool is_instance_method_of_type (PyTypeObject *type_obj, PyObject *attr_name) {
64- PyObject *descr = _PyType_Lookup (type_obj, attr_name);
65- return bool ((descr != nullptr ) && PyInstanceMethod_Check (descr));
66- }
67-
68- inline object try_get_as_capsule_method (PyObject *obj, PyObject *attr_name) {
69- if (PyType_Check (obj)) {
70- return object ();
71- }
72- PyTypeObject *type_obj = Py_TYPE (obj);
73- bool known_callable = false ;
74- if (type_is_pybind11_class_ (type_obj)) {
75- if (!is_instance_method_of_type (type_obj, attr_name)) {
76- return object ();
77- }
78- known_callable = true ;
79- }
80- PyObject *method = PyObject_GetAttr (obj, attr_name);
81- if (method == nullptr ) {
82- PyErr_Clear ();
83- return object ();
84- }
85- if (!known_callable && PyCallable_Check (method) == 0 ) {
86- Py_DECREF (method);
87- return object ();
88- }
89- return reinterpret_steal<object>(method);
90- }
91-
92- inline void *try_as_void_ptr_capsule_get_pointer (handle src, const char *typeid_name) {
93- std::string suffix = clean_type_id (typeid_name);
94- replace_all (suffix, " ::" , " _" ); // Convert `a::b::c` to `a_b_c`.
95- replace_all (suffix, " *" , " " );
96- object as_capsule_method = try_get_as_capsule_method (src.ptr (), str (" as_" + suffix).ptr ());
97- if (as_capsule_method) {
98- object void_ptr_capsule = as_capsule_method ();
99- if (isinstance<capsule>(void_ptr_capsule)) {
100- return reinterpret_borrow<capsule>(void_ptr_capsule).get_pointer ();
101- }
102- }
103- return nullptr ;
104- }
10539
10640// The modified_type_caster_generic_load_impl could replace type_caster_generic::load_impl but not
10741// vice versa. The main difference is that the original code only propagates a reference to the
@@ -176,15 +110,6 @@ class modified_type_caster_generic_load_impl {
176110 return false ;
177111 }
178112
179- bool try_as_void_ptr_capsule (handle src) {
180- unowned_void_ptr_from_void_ptr_capsule
181- = try_as_void_ptr_capsule_get_pointer (src, cpptype->name ());
182- if (unowned_void_ptr_from_void_ptr_capsule != nullptr ) {
183- return true ;
184- }
185- return false ;
186- }
187-
188113 PYBIND11_NOINLINE static void *local_load (PyObject *src, const type_info *ti) {
189114 std::unique_ptr<modified_type_caster_generic_load_impl> loader (
190115 new modified_type_caster_generic_load_impl (ti));
@@ -337,16 +262,12 @@ class modified_type_caster_generic_load_impl {
337262 loaded_v_h = value_and_holder ();
338263 return true ;
339264 }
340- if (convert && cpptype && try_as_void_ptr_capsule (src)) {
341- return true ;
342- }
343265 return false ;
344266 }
345267
346268 const type_info *typeinfo = nullptr ;
347269 const std::type_info *cpptype = nullptr ;
348270 void *unowned_void_ptr_from_direct_conversion = nullptr ;
349- void *unowned_void_ptr_from_void_ptr_capsule = nullptr ;
350271 const std::type_info *loaded_v_h_cpptype = nullptr ;
351272 std::vector<void *(*) (void *)> implicit_casts;
352273 value_and_holder loaded_v_h;
@@ -499,10 +420,7 @@ struct smart_holder_type_caster_load {
499420 }
500421
501422 T *loaded_as_raw_ptr_unowned () const {
502- void *void_ptr = load_impl.unowned_void_ptr_from_void_ptr_capsule ;
503- if (void_ptr == nullptr ) {
504- void_ptr = load_impl.unowned_void_ptr_from_direct_conversion ;
505- }
423+ void *void_ptr = load_impl.unowned_void_ptr_from_direct_conversion ;
506424 if (void_ptr == nullptr ) {
507425 if (have_holder ()) {
508426 throw_if_uninitialized_or_disowned_holder (typeid (T));
@@ -531,13 +449,6 @@ struct smart_holder_type_caster_load {
531449 }
532450
533451 std::shared_ptr<T> loaded_as_shared_ptr (handle responsible_parent = nullptr ) const {
534- if (load_impl.unowned_void_ptr_from_void_ptr_capsule ) {
535- if (responsible_parent) {
536- return make_shared_ptr_with_responsible_parent (responsible_parent);
537- }
538- throw cast_error (" Unowned pointer from a void pointer capsule cannot be converted to a"
539- " std::shared_ptr." );
540- }
541452 if (load_impl.unowned_void_ptr_from_direct_conversion != nullptr ) {
542453 if (responsible_parent) {
543454 return make_shared_ptr_with_responsible_parent (responsible_parent);
@@ -601,10 +512,6 @@ struct smart_holder_type_caster_load {
601512
602513 template <typename D>
603514 std::unique_ptr<T, D> loaded_as_unique_ptr (const char *context = " loaded_as_unique_ptr" ) {
604- if (load_impl.unowned_void_ptr_from_void_ptr_capsule ) {
605- throw cast_error (" Unowned pointer from a void pointer capsule cannot be converted to a"
606- " std::unique_ptr." );
607- }
608515 if (load_impl.unowned_void_ptr_from_direct_conversion != nullptr ) {
609516 throw cast_error (" Unowned pointer from direct conversion cannot be converted to a"
610517 " std::unique_ptr." );
0 commit comments