Skip to content
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

Throw exception on returning a unique_ptr or shared_ptr nullptr (or any other holder type) from py::init, rather than crashing #2430

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions include/pybind11/detail/init.h
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ void construct(value_and_holder &v_h, Alias<Class> *alias_ptr, bool) {
template <typename Class>
void construct(value_and_holder &v_h, Holder<Class> holder, bool need_alias) {
auto *ptr = holder_helper<Holder<Class>>::get(holder);
no_nullptr(ptr);
// If we need an alias, check that the held pointer is actually an alias instance
if (Class::has_alias && need_alias && !is_alias<Class>(ptr))
throw type_error("pybind11::init(): construction failed: returned holder-wrapped instance "
Expand Down
4 changes: 4 additions & 0 deletions tests/test_factory_constructors.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,8 @@ TEST_SUBMODULE(factory_constructors, m) {
MAKE_TAG_TYPE(TF4);
MAKE_TAG_TYPE(TF5);
MAKE_TAG_TYPE(null_ptr);
MAKE_TAG_TYPE(null_unique_ptr);
MAKE_TAG_TYPE(null_shared_ptr);
MAKE_TAG_TYPE(base);
MAKE_TAG_TYPE(invalid_base);
MAKE_TAG_TYPE(alias);
Expand Down Expand Up @@ -194,6 +196,8 @@ TEST_SUBMODULE(factory_constructors, m) {

// Returns nullptr:
.def(py::init([](null_ptr_tag) { return (TestFactory3 *) nullptr; }))
.def(py::init([](null_unique_ptr_tag) { return std::unique_ptr<TestFactory3>(); }))
.def(py::init([](null_shared_ptr_tag) { return std::shared_ptr<TestFactory3>(); }))

.def_readwrite("value", &TestFactory3::value)
;
Expand Down
9 changes: 6 additions & 3 deletions tests/test_factory_constructors.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,12 @@ def test_init_factory_basic():
z3 = m.TestFactory3("bye")
assert z3.value == "bye"

with pytest.raises(TypeError) as excinfo:
m.TestFactory3(tag.null_ptr)
assert str(excinfo.value) == "pybind11::init(): factory function returned nullptr"
for null_ptr_kind in [tag.null_ptr,
tag.null_unique_ptr,
tag.null_shared_ptr]:
with pytest.raises(TypeError) as excinfo:
m.TestFactory3(null_ptr_kind)
assert str(excinfo.value) == "pybind11::init(): factory function returned nullptr"

assert [i.alive() for i in cstats] == [3, 3, 3]
assert ConstructorStats.detail_reg_inst() == n_inst + 9
Expand Down