Skip to content

Commit 91a6e12

Browse files
PYBIND11_OBJECT_CVT should use namespace for error_already_set() (#3797)
* PYBIND11_OBJECT_CVT should use namespace for error_already_set() This change makes the macro usable outside of pybind11 namespace. * added test for use of PYBIND11_OBJECT_CVT for classes in external to pybind11 namespaces * Extended test_pytypes.cpp and test_pytest.py The added test defines a dummy function that takes a custom-defined class external::float_ that uses PYBIND11_OBJECT_CVT * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * fixed issues pointed out by CI * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * fixed memory leak in default constructor Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent d75b353 commit 91a6e12

File tree

3 files changed

+40
-2
lines changed

3 files changed

+40
-2
lines changed

include/pybind11/pytypes.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1038,12 +1038,12 @@ public:
10381038
Name(const object &o) \
10391039
: Parent(check_(o) ? o.inc_ref().ptr() : ConvertFun(o.ptr()), stolen_t{}) { \
10401040
if (!m_ptr) \
1041-
throw error_already_set(); \
1041+
throw ::pybind11::error_already_set(); \
10421042
} \
10431043
/* NOLINTNEXTLINE(google-explicit-constructor) */ \
10441044
Name(object &&o) : Parent(check_(o) ? o.release().ptr() : ConvertFun(o.ptr()), stolen_t{}) { \
10451045
if (!m_ptr) \
1046-
throw error_already_set(); \
1046+
throw ::pybind11::error_already_set(); \
10471047
}
10481048

10491049
#define PYBIND11_OBJECT_CVT_DEFAULT(Name, Parent, CheckFun, ConvertFun) \

tests/test_pytypes.cpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,34 @@
1111

1212
#include <utility>
1313

14+
namespace external {
15+
namespace detail {
16+
bool check(PyObject *o) { return PyFloat_Check(o) != 0; }
17+
18+
PyObject *conv(PyObject *o) {
19+
PyObject *ret = nullptr;
20+
if (PyLong_Check(o)) {
21+
double v = PyLong_AsDouble(o);
22+
if (!(v == -1.0 && PyErr_Occurred())) {
23+
ret = PyFloat_FromDouble(v);
24+
}
25+
} else {
26+
PyErr_SetString(PyExc_TypeError, "Unexpected type");
27+
}
28+
return ret;
29+
}
30+
31+
PyObject *default_constructed() { return PyFloat_FromDouble(0.0); }
32+
} // namespace detail
33+
class float_ : public py::object {
34+
PYBIND11_OBJECT_CVT(float_, py::object, external::detail::check, external::detail::conv)
35+
36+
float_() : py::object(external::detail::default_constructed(), stolen_t{}) {}
37+
38+
double get_value() const { return PyFloat_AsDouble(this->ptr()); }
39+
};
40+
} // namespace external
41+
1442
TEST_SUBMODULE(pytypes, m) {
1543
// test_bool
1644
m.def("get_bool", [] { return py::bool_(false); });
@@ -545,4 +573,9 @@ TEST_SUBMODULE(pytypes, m) {
545573
py::detail::accessor_policies::tuple_item::set(o, (py::size_t) 0, s0);
546574
return o;
547575
});
576+
577+
m.def("square_float_", [](const external::float_ &x) -> double {
578+
double v = x.get_value();
579+
return v * v;
580+
});
548581
}

tests/test_pytypes.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -634,3 +634,8 @@ def test_implementation_details():
634634
assert m.tuple_item_set_ssize_t() == ("emely", "edmond")
635635
assert m.tuple_item_get_size_t(tup) == 93
636636
assert m.tuple_item_set_size_t() == ("candy", "cat")
637+
638+
639+
def test_external_float_():
640+
r1 = m.square_float_(2.0)
641+
assert r1 == 4.0

0 commit comments

Comments
 (0)