diff --git a/include/pybind11/embed.h b/include/pybind11/embed.h index 0eb656b0..98bf7690 100644 --- a/include/pybind11/embed.h +++ b/include/pybind11/embed.h @@ -99,8 +99,7 @@ inline void initialize_interpreter(bool init_signal_handlers = true) { Py_InitializeEx(init_signal_handlers ? 1 : 0); // Make .py files in the working directory available by default - auto sys_path = reinterpret_borrow(module::import("sys").attr("path")); - sys_path.append("."); + module::import("sys").attr("path").cast().append("."); } /** \rst diff --git a/include/pybind11/pytypes.h b/include/pybind11/pytypes.h index 095d40f1..b652b8e9 100644 --- a/include/pybind11/pytypes.h +++ b/include/pybind11/pytypes.h @@ -731,7 +731,12 @@ NAMESPACE_END(detail) #define PYBIND11_OBJECT_CVT(Name, Parent, CheckFun, ConvertFun) \ PYBIND11_OBJECT_COMMON(Name, Parent, CheckFun) \ /* This is deliberately not 'explicit' to allow implicit conversion from object: */ \ - Name(const object &o) : Parent(ConvertFun(o.ptr()), stolen_t{}) { if (!m_ptr) throw error_already_set(); } + Name(const object &o) \ + : Parent(check_(o) ? o.inc_ref().ptr() : ConvertFun(o.ptr()), stolen_t{}) \ + { if (!m_ptr) throw error_already_set(); } \ + Name(object &&o) \ + : Parent(check_(o) ? o.release().ptr() : ConvertFun(o.ptr()), stolen_t{}) \ + { if (!m_ptr) throw error_already_set(); } #define PYBIND11_OBJECT(Name, Parent, CheckFun) \ PYBIND11_OBJECT_COMMON(Name, Parent, CheckFun) \ diff --git a/tests/test_pytypes.py b/tests/test_pytypes.py index 1c75c47d..b8dad841 100644 --- a/tests/test_pytypes.py +++ b/tests/test_pytypes.py @@ -174,9 +174,20 @@ def test_constructors(): } inputs = {k.__name__: v for k, v in data.items()} expected = {k.__name__: k(v) for k, v in data.items()} + assert m.converting_constructors(inputs) == expected assert m.cast_functions(inputs) == expected + # Converting constructors and cast functions should just reference rather + # than copy when no conversion is needed: + noconv1 = m.converting_constructors(expected) + for k in noconv1: + assert noconv1[k] is expected[k] + + noconv2 = m.cast_functions(expected) + for k in noconv2: + assert noconv2[k] is expected[k] + def test_implicit_casting(): """Tests implicit casting when assigning or appending to dicts and lists."""