mirror of https://github.com/pybind/pybind11.git
Merge bb02ebc654
into 1f8b4a7f1a
This commit is contained in:
commit
2d9c5339c7
|
@ -38,6 +38,9 @@ PYBIND11_NAMESPACE_BEGIN(initimpl)
|
||||||
|
|
||||||
inline void no_nullptr(void *ptr) {
|
inline void no_nullptr(void *ptr) {
|
||||||
if (!ptr) {
|
if (!ptr) {
|
||||||
|
if (PyErr_Occurred()) {
|
||||||
|
throw error_already_set();
|
||||||
|
}
|
||||||
throw type_error("pybind11::init(): factory function returned nullptr");
|
throw type_error("pybind11::init(): factory function returned nullptr");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -412,6 +412,16 @@ TEST_SUBMODULE(factory_constructors, m) {
|
||||||
"__init__", [](NoisyAlloc &a, int i, const std::string &) { new (&a) NoisyAlloc(i); });
|
"__init__", [](NoisyAlloc &a, int i, const std::string &) { new (&a) NoisyAlloc(i); });
|
||||||
});
|
});
|
||||||
|
|
||||||
|
struct FactoryErrorAlreadySet {};
|
||||||
|
py::class_<FactoryErrorAlreadySet>(m, "FactoryErrorAlreadySet")
|
||||||
|
.def(py::init([](bool set_error) -> FactoryErrorAlreadySet * {
|
||||||
|
if (!set_error) {
|
||||||
|
return new FactoryErrorAlreadySet();
|
||||||
|
}
|
||||||
|
py::set_error(PyExc_ValueError, "factory sets error and returns nullptr");
|
||||||
|
return nullptr;
|
||||||
|
}));
|
||||||
|
|
||||||
// static_assert testing (the following def's should all fail with appropriate compilation
|
// static_assert testing (the following def's should all fail with appropriate compilation
|
||||||
// errors):
|
// errors):
|
||||||
#if 0
|
#if 0
|
||||||
|
|
|
@ -516,3 +516,10 @@ def test_invalid_self():
|
||||||
str(excinfo.value)
|
str(excinfo.value)
|
||||||
== "__init__(self, ...) called with invalid or missing `self` argument"
|
== "__init__(self, ...) called with invalid or missing `self` argument"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def test_factory_error_already_set():
|
||||||
|
obj = m.FactoryErrorAlreadySet(False)
|
||||||
|
assert isinstance(obj, m.FactoryErrorAlreadySet)
|
||||||
|
with pytest.raises(ValueError, match="factory sets error and returns nullptr"):
|
||||||
|
m.FactoryErrorAlreadySet(True)
|
||||||
|
|
Loading…
Reference in New Issue