-
Notifications
You must be signed in to change notification settings - Fork 2.2k
[Smart holder] Support void pointer capsules #3633
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 9 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
ccb1afb
Make smart holder type casters support void pointer capsules.
wangxf123456 3e3f70c
Fix warnings
wangxf123456 c42912d
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 2fffb6f
Fix checks
wangxf123456 e5a5607
Merge branch 'smart_holder' of github.com:wangxf123456/pybind11 into …
wangxf123456 e3b0e23
Fix check failures under CentOS
wangxf123456 505bfba
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 76dc820
Remove unused regex module
wangxf123456 86b6cea
Merge branch 'smart_holder' of github.com:wangxf123456/pybind11 into …
wangxf123456 c0b669f
Resolve comments
wangxf123456 6c48f56
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] dc45d1b
Resolve comments
wangxf123456 82b6363
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 9595439
Fix clangtidy
wangxf123456 f571da2
Resolve comments
wangxf123456 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -106,6 +106,34 @@ class modified_type_caster_generic_load_impl { | |
return false; | ||
} | ||
|
||
bool try_as_void_ptr_capsule(handle src) { | ||
std::string type_name = cpptype->name(); | ||
detail::clean_type_id(type_name); | ||
|
||
// Convert `a::b::c` to `a_b_c` | ||
size_t pos = type_name.find("::"); | ||
while (pos != std::string::npos) { | ||
type_name.replace(pos, 2, "_"); | ||
pos = type_name.find("::", pos); | ||
} | ||
|
||
std::string as_void_ptr_function_name = "as_" + type_name; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Very tiny nit, this could be an allocation of "as_" and += type_name and remove an allocation. |
||
if (hasattr(src, as_void_ptr_function_name.c_str())) { | ||
auto void_ptr_capsule = reinterpret_borrow<object>( | ||
wangxf123456 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
PyObject_CallMethod(src.ptr(), | ||
const_cast<char *>(as_void_ptr_function_name.c_str()), | ||
nullptr)); | ||
if (isinstance<capsule>(void_ptr_capsule)) { | ||
unowned_void_ptr_from_void_ptr_capsule = static_cast<void *>( | ||
PyCapsule_GetPointer( | ||
void_ptr_capsule.ptr(), | ||
PyCapsule_GetName(void_ptr_capsule.ptr()))); | ||
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)); | ||
|
@@ -161,6 +189,9 @@ class modified_type_caster_generic_load_impl { | |
template <typename ThisT> | ||
PYBIND11_NOINLINE bool load_impl(handle src, bool convert) { | ||
if (!src) return false; | ||
if (cpptype && try_as_void_ptr_capsule(src)) { | ||
return true; | ||
} | ||
if (!typeinfo) return try_load_foreign_module_local(src); | ||
|
||
auto &this_ = static_cast<ThisT &>(*this); | ||
|
@@ -250,6 +281,7 @@ class modified_type_caster_generic_load_impl { | |
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; | ||
void *(*implicit_cast)(void *) = nullptr; | ||
value_and_holder loaded_v_h; | ||
|
@@ -366,15 +398,18 @@ struct smart_holder_type_caster_load { | |
} | ||
|
||
T *loaded_as_raw_ptr_unowned() const { | ||
void *void_ptr = load_impl.unowned_void_ptr_from_direct_conversion; | ||
void *void_ptr = load_impl.unowned_void_ptr_from_void_ptr_capsule; | ||
if (void_ptr == nullptr) { | ||
if (have_holder()) { | ||
throw_if_uninitialized_or_disowned_holder(); | ||
void_ptr = holder().template as_raw_ptr_unowned<void>(); | ||
} else if (load_impl.loaded_v_h.vh != nullptr) | ||
void_ptr = load_impl.loaded_v_h.value_ptr(); | ||
if (void_ptr == nullptr) | ||
return nullptr; | ||
void_ptr = load_impl.unowned_void_ptr_from_direct_conversion; | ||
if (void_ptr == nullptr) { | ||
wangxf123456 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if (have_holder()) { | ||
throw_if_uninitialized_or_disowned_holder(); | ||
void_ptr = holder().template as_raw_ptr_unowned<void>(); | ||
} else if (load_impl.loaded_v_h.vh != nullptr) | ||
void_ptr = load_impl.loaded_v_h.value_ptr(); | ||
if (void_ptr == nullptr) | ||
return nullptr; | ||
} | ||
} | ||
return convert_type(void_ptr); | ||
} | ||
|
@@ -387,6 +422,10 @@ struct smart_holder_type_caster_load { | |
} | ||
|
||
std::shared_ptr<T> loaded_as_shared_ptr() const { | ||
if (load_impl.unowned_void_ptr_from_void_ptr_capsule) { | ||
throw cast_error("Void pointer capsule cannot be converted to a" | ||
" std::shared_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::shared_ptr."); | ||
|
@@ -441,6 +480,10 @@ 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("Void pointer capsule cannot be converted to a" | ||
wangxf123456 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
" 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."); | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
#include "pybind11_tests.h" | ||
|
||
#include <Python.h> | ||
wangxf123456 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
#include <pybind11/smart_holder.h> | ||
|
||
#include <memory> | ||
|
||
namespace pybind11_tests { | ||
namespace class_sh_void_ptr_capsule { | ||
|
||
struct CapsuleBase { | ||
wangxf123456 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
CapsuleBase() = default; | ||
virtual ~CapsuleBase() = default; | ||
|
||
bool capsule_generated = false; | ||
virtual int get() const { return 100; } | ||
}; | ||
|
||
struct Valid: public CapsuleBase { | ||
int get() const override { return 101; } | ||
|
||
PyObject* as_pybind11_tests_class_sh_void_ptr_capsule_Valid() { | ||
void* vptr = static_cast<void*>(this); | ||
wangxf123456 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
capsule_generated = true; | ||
// We assume vptr out lives the capsule, so we use nullptr for the | ||
// destructor. | ||
return PyCapsule_New( | ||
vptr, "::pybind11_tests::class_sh_void_ptr_capsule::Valid", | ||
nullptr); | ||
} | ||
}; | ||
|
||
struct NoConversion: public CapsuleBase { | ||
int get() const override { return 102; } | ||
}; | ||
|
||
struct NoCapsuleReturned: public CapsuleBase { | ||
int get() const { return 103; } | ||
|
||
PyObject* as_pybind11_tests_class_sh_void_ptr_capsule_NoCapsuleReturned() { | ||
capsule_generated = true; | ||
Py_XINCREF(Py_None); | ||
return Py_None; | ||
} | ||
}; | ||
|
||
struct AsAnotherObject: public CapsuleBase { | ||
int get() const override { return 104; } | ||
|
||
PyObject* as_pybind11_tests_class_sh_void_ptr_capsule_Valid() { | ||
void* vptr = static_cast<void*>(this); | ||
capsule_generated = true; | ||
// We assume vptr out lives the capsule, so we use nullptr for the | ||
// destructor. | ||
return PyCapsule_New( | ||
vptr, "::pybind11_tests::class_sh_void_ptr_capsule::Valid", | ||
nullptr); | ||
} | ||
}; | ||
|
||
int get_from_valid_capsule(const Valid* c) { | ||
return c->get(); | ||
} | ||
|
||
int get_from_shared_ptr_valid_capsule(std::shared_ptr<Valid> c) { | ||
return c->get(); | ||
} | ||
|
||
int get_from_unique_ptr_valid_capsule(std::unique_ptr<Valid> c) { | ||
return c->get(); | ||
} | ||
|
||
int get_from_no_conversion_capsule(const NoConversion* c) { | ||
return c->get(); | ||
} | ||
|
||
int get_from_no_capsule_returned(const NoCapsuleReturned* c) { | ||
return c->get(); | ||
} | ||
|
||
} // namespace class_sh_void_ptr_capsule | ||
} // namespace pybind11_tests | ||
|
||
PYBIND11_SMART_HOLDER_TYPE_CASTERS(pybind11_tests::class_sh_void_ptr_capsule::CapsuleBase) | ||
PYBIND11_SMART_HOLDER_TYPE_CASTERS(pybind11_tests::class_sh_void_ptr_capsule::Valid) | ||
PYBIND11_SMART_HOLDER_TYPE_CASTERS(pybind11_tests::class_sh_void_ptr_capsule::NoConversion) | ||
PYBIND11_SMART_HOLDER_TYPE_CASTERS(pybind11_tests::class_sh_void_ptr_capsule::NoCapsuleReturned) | ||
PYBIND11_SMART_HOLDER_TYPE_CASTERS(pybind11_tests::class_sh_void_ptr_capsule::AsAnotherObject) | ||
|
||
TEST_SUBMODULE(class_sh_void_ptr_capsule, m) { | ||
using namespace pybind11_tests::class_sh_void_ptr_capsule; | ||
|
||
py::classh<CapsuleBase>(m, "CapsuleBase") | ||
.def(py::init<>()) | ||
.def("get", &CapsuleBase::get) | ||
.def_readonly("capsule_generated", &CapsuleBase::capsule_generated); | ||
|
||
py::classh<Valid, CapsuleBase>(m, "Valid") | ||
.def(py::init<>()) | ||
.def("as_pybind11_tests_class_sh_void_ptr_capsule_Valid", | ||
[](CapsuleBase* self) { | ||
Valid *obj = dynamic_cast<Valid *>(self); | ||
PyObject* capsule = obj->as_pybind11_tests_class_sh_void_ptr_capsule_Valid(); | ||
return pybind11::reinterpret_steal<py::capsule>(capsule); | ||
}); | ||
|
||
py::classh<NoConversion, CapsuleBase>(m, "NoConversion") | ||
.def(py::init<>()); | ||
|
||
py::classh<NoCapsuleReturned, CapsuleBase>(m, "NoCapsuleReturned") | ||
.def(py::init<>()) | ||
.def("as_pybind11_tests_class_sh_void_ptr_capsule_NoCapsuleReturned", | ||
[](CapsuleBase* self) { | ||
NoCapsuleReturned *obj = dynamic_cast<NoCapsuleReturned *>(self); | ||
PyObject* capsule = obj->as_pybind11_tests_class_sh_void_ptr_capsule_NoCapsuleReturned(); | ||
return pybind11::reinterpret_steal<py::capsule>(capsule); | ||
}); | ||
|
||
py::classh<AsAnotherObject, CapsuleBase>(m, "AsAnotherObject") | ||
.def(py::init<>()) | ||
.def("as_pybind11_tests_class_sh_void_ptr_capsule_Valid", | ||
[](CapsuleBase* self) { | ||
AsAnotherObject *obj = dynamic_cast<AsAnotherObject *>(self); | ||
wangxf123456 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
PyObject* capsule = obj->as_pybind11_tests_class_sh_void_ptr_capsule_Valid(); | ||
return pybind11::reinterpret_steal<py::capsule>(capsule); | ||
}); | ||
|
||
m.def("get_from_valid_capsule", &get_from_valid_capsule); | ||
m.def("get_from_shared_ptr_valid_capsule", &get_from_shared_ptr_valid_capsule); | ||
m.def("get_from_unique_ptr_valid_capsule", &get_from_unique_ptr_valid_capsule); | ||
m.def("get_from_no_conversion_capsule", &get_from_no_conversion_capsule); | ||
m.def("get_from_no_capsule_returned", &get_from_no_capsule_returned); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
# -*- coding: utf-8 -*- | ||
import pytest | ||
|
||
from pybind11_tests import class_sh_void_ptr_capsule as m | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"ctor, caller, expected, capsule_generated", | ||
[ | ||
(m.Valid, m.get_from_valid_capsule, 101, True), | ||
(m.NoConversion, m.get_from_no_conversion_capsule, 102, False), | ||
(m.NoCapsuleReturned, m.get_from_no_capsule_returned, 103, True), | ||
(m.AsAnotherObject, m.get_from_valid_capsule, 104, True), | ||
], | ||
) | ||
def test_as_void_ptr_capsule(ctor, caller, expected, capsule_generated): | ||
obj = ctor() | ||
assert caller(obj) == expected | ||
assert obj.capsule_generated == capsule_generated | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"ctor, caller, pointer_type, capsule_generated", | ||
[ | ||
(m.AsAnotherObject, m.get_from_shared_ptr_valid_capsule, "shared_ptr", True), | ||
(m.AsAnotherObject, m.get_from_unique_ptr_valid_capsule, "unique_ptr", True), | ||
], | ||
) | ||
def test_as_void_ptr_capsule_unsupported(ctor, caller, pointer_type, capsule_generated): | ||
obj = ctor() | ||
with pytest.raises(RuntimeError) as excinfo: | ||
caller(obj) | ||
assert pointer_type in str(excinfo.value) | ||
assert obj.capsule_generated == capsule_generated |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.