Skip to content

Commit

Permalink
Remove try_as_void_ptr_capsule feature from smart_holder branch.
Browse files Browse the repository at this point in the history
This manual removal was informed by reviews of these commits (newest to oldest):

* d930de0
* 114c0f2
* 1c10b09
* 9d4b4df
* da058a2
  • Loading branch information
Ralf W. Grosse-Kunstleve authored and rwgk committed Jun 11, 2024
1 parent c4df281 commit bf79caa
Show file tree
Hide file tree
Showing 4 changed files with 1 addition and 322 deletions.
95 changes: 1 addition & 94 deletions include/pybind11/detail/smart_holder_type_casters.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,72 +36,6 @@ struct is_smart_holder_type<smart_holder> : std::true_type {};
// SMART_HOLDER_WIP: Needs refactoring of existing pybind11 code.
inline void register_instance(instance *self, void *valptr, const type_info *tinfo);
inline bool deregister_instance(instance *self, void *valptr, const type_info *tinfo);
extern "C" inline PyObject *pybind11_object_new(PyTypeObject *type, PyObject *, PyObject *);

// Replace all occurrences of substrings in a string.
inline void replace_all(std::string &str, const std::string &from, const std::string &to) {
if (str.empty()) {
return;
}
size_t pos = 0;
while ((pos = str.find(from, pos)) != std::string::npos) {
str.replace(pos, from.length(), to);
pos += to.length();
}
}

inline bool type_is_pybind11_class_(PyTypeObject *type_obj) {
#if defined(PYPY_VERSION)
auto &internals = get_internals();
return bool(internals.registered_types_py.find(type_obj)
!= internals.registered_types_py.end());
#else
return bool(type_obj->tp_new == pybind11_object_new);
#endif
}

inline bool is_instance_method_of_type(PyTypeObject *type_obj, PyObject *attr_name) {
PyObject *descr = _PyType_Lookup(type_obj, attr_name);
return bool((descr != nullptr) && PyInstanceMethod_Check(descr));
}

inline object try_get_as_capsule_method(PyObject *obj, PyObject *attr_name) {
if (PyType_Check(obj)) {
return object();
}
PyTypeObject *type_obj = Py_TYPE(obj);
bool known_callable = false;
if (type_is_pybind11_class_(type_obj)) {
if (!is_instance_method_of_type(type_obj, attr_name)) {
return object();
}
known_callable = true;
}
PyObject *method = PyObject_GetAttr(obj, attr_name);
if (method == nullptr) {
PyErr_Clear();
return object();
}
if (!known_callable && PyCallable_Check(method) == 0) {
Py_DECREF(method);
return object();
}
return reinterpret_steal<object>(method);
}

inline void *try_as_void_ptr_capsule_get_pointer(handle src, const char *typeid_name) {
std::string suffix = clean_type_id(typeid_name);
replace_all(suffix, "::", "_"); // Convert `a::b::c` to `a_b_c`.
replace_all(suffix, "*", "");
object as_capsule_method = try_get_as_capsule_method(src.ptr(), str("as_" + suffix).ptr());
if (as_capsule_method) {
object void_ptr_capsule = as_capsule_method();
if (isinstance<capsule>(void_ptr_capsule)) {
return reinterpret_borrow<capsule>(void_ptr_capsule).get_pointer();
}
}
return nullptr;
}

// The modified_type_caster_generic_load_impl could replace type_caster_generic::load_impl but not
// vice versa. The main difference is that the original code only propagates a reference to the
Expand Down Expand Up @@ -176,15 +110,6 @@ class modified_type_caster_generic_load_impl {
return false;
}

bool try_as_void_ptr_capsule(handle src) {
unowned_void_ptr_from_void_ptr_capsule
= try_as_void_ptr_capsule_get_pointer(src, cpptype->name());
if (unowned_void_ptr_from_void_ptr_capsule != nullptr) {
return true;
}
return false;
}

PYBIND11_NOINLINE static void *local_load(PyObject *src, const type_info *ti) {
std::unique_ptr<modified_type_caster_generic_load_impl> loader(
new modified_type_caster_generic_load_impl(ti));
Expand Down Expand Up @@ -337,16 +262,12 @@ class modified_type_caster_generic_load_impl {
loaded_v_h = value_and_holder();
return true;
}
if (convert && cpptype && try_as_void_ptr_capsule(src)) {
return true;
}
return false;
}

const type_info *typeinfo = nullptr;
const std::type_info *cpptype = nullptr;
void *unowned_void_ptr_from_direct_conversion = nullptr;
void *unowned_void_ptr_from_void_ptr_capsule = nullptr;
const std::type_info *loaded_v_h_cpptype = nullptr;
std::vector<void *(*) (void *)> implicit_casts;
value_and_holder loaded_v_h;
Expand Down Expand Up @@ -499,10 +420,7 @@ struct smart_holder_type_caster_load {
}

T *loaded_as_raw_ptr_unowned() const {
void *void_ptr = load_impl.unowned_void_ptr_from_void_ptr_capsule;
if (void_ptr == nullptr) {
void_ptr = load_impl.unowned_void_ptr_from_direct_conversion;
}
void *void_ptr = load_impl.unowned_void_ptr_from_direct_conversion;
if (void_ptr == nullptr) {
if (have_holder()) {
throw_if_uninitialized_or_disowned_holder(typeid(T));
Expand Down Expand Up @@ -531,13 +449,6 @@ struct smart_holder_type_caster_load {
}

std::shared_ptr<T> loaded_as_shared_ptr(handle responsible_parent = nullptr) const {
if (load_impl.unowned_void_ptr_from_void_ptr_capsule) {
if (responsible_parent) {
return make_shared_ptr_with_responsible_parent(responsible_parent);
}
throw cast_error("Unowned pointer from a void pointer capsule cannot be converted to a"
" std::shared_ptr.");
}
if (load_impl.unowned_void_ptr_from_direct_conversion != nullptr) {
if (responsible_parent) {
return make_shared_ptr_with_responsible_parent(responsible_parent);
Expand Down Expand Up @@ -601,10 +512,6 @@ struct smart_holder_type_caster_load {

template <typename D>
std::unique_ptr<T, D> loaded_as_unique_ptr(const char *context = "loaded_as_unique_ptr") {
if (load_impl.unowned_void_ptr_from_void_ptr_capsule) {
throw cast_error("Unowned pointer from a void pointer capsule cannot be converted to a"
" std::unique_ptr.");
}
if (load_impl.unowned_void_ptr_from_direct_conversion != nullptr) {
throw cast_error("Unowned pointer from direct conversion cannot be converted to a"
" std::unique_ptr.");
Expand Down
1 change: 0 additions & 1 deletion tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,6 @@ set(PYBIND11_TEST_FILES
test_class_sh_unique_ptr_custom_deleter
test_class_sh_unique_ptr_member
test_class_sh_virtual_py_cpp_mix
test_class_sh_void_ptr_capsule
test_classh_mock
test_const_name
test_constants_and_functions
Expand Down
127 changes: 0 additions & 127 deletions tests/test_class_sh_void_ptr_capsule.cpp

This file was deleted.

100 changes: 0 additions & 100 deletions tests/test_class_sh_void_ptr_capsule.py

This file was deleted.

0 comments on commit bf79caa

Please sign in to comment.