From 91a6e129d9274a0b10943044e5aeae08471e00fe Mon Sep 17 00:00:00 2001 From: Oleksandr Pavlyk Date: Fri, 11 Mar 2022 14:18:25 -0600 Subject: [PATCH 1/5] 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> --- include/pybind11/pytypes.h | 4 ++-- tests/test_pytypes.cpp | 33 +++++++++++++++++++++++++++++++++ tests/test_pytypes.py | 5 +++++ 3 files changed, 40 insertions(+), 2 deletions(-) diff --git a/include/pybind11/pytypes.h b/include/pybind11/pytypes.h index dc753d32c..e54941485 100644 --- a/include/pybind11/pytypes.h +++ b/include/pybind11/pytypes.h @@ -1038,12 +1038,12 @@ public: Name(const object &o) \ : Parent(check_(o) ? o.inc_ref().ptr() : ConvertFun(o.ptr()), stolen_t{}) { \ if (!m_ptr) \ - throw error_already_set(); \ + throw ::pybind11::error_already_set(); \ } \ /* NOLINTNEXTLINE(google-explicit-constructor) */ \ Name(object &&o) : Parent(check_(o) ? o.release().ptr() : ConvertFun(o.ptr()), stolen_t{}) { \ if (!m_ptr) \ - throw error_already_set(); \ + throw ::pybind11::error_already_set(); \ } #define PYBIND11_OBJECT_CVT_DEFAULT(Name, Parent, CheckFun, ConvertFun) \ diff --git a/tests/test_pytypes.cpp b/tests/test_pytypes.cpp index 1ed237ea2..b859497b8 100644 --- a/tests/test_pytypes.cpp +++ b/tests/test_pytypes.cpp @@ -11,6 +11,34 @@ #include +namespace external { +namespace detail { +bool check(PyObject *o) { return PyFloat_Check(o) != 0; } + +PyObject *conv(PyObject *o) { + PyObject *ret = nullptr; + if (PyLong_Check(o)) { + double v = PyLong_AsDouble(o); + if (!(v == -1.0 && PyErr_Occurred())) { + ret = PyFloat_FromDouble(v); + } + } else { + PyErr_SetString(PyExc_TypeError, "Unexpected type"); + } + return ret; +} + +PyObject *default_constructed() { return PyFloat_FromDouble(0.0); } +} // namespace detail +class float_ : public py::object { + PYBIND11_OBJECT_CVT(float_, py::object, external::detail::check, external::detail::conv) + + float_() : py::object(external::detail::default_constructed(), stolen_t{}) {} + + double get_value() const { return PyFloat_AsDouble(this->ptr()); } +}; +} // namespace external + TEST_SUBMODULE(pytypes, m) { // test_bool m.def("get_bool", [] { return py::bool_(false); }); @@ -545,4 +573,9 @@ TEST_SUBMODULE(pytypes, m) { py::detail::accessor_policies::tuple_item::set(o, (py::size_t) 0, s0); return o; }); + + m.def("square_float_", [](const external::float_ &x) -> double { + double v = x.get_value(); + return v * v; + }); } diff --git a/tests/test_pytypes.py b/tests/test_pytypes.py index becd1cc8a..85afb9423 100644 --- a/tests/test_pytypes.py +++ b/tests/test_pytypes.py @@ -634,3 +634,8 @@ def test_implementation_details(): assert m.tuple_item_set_ssize_t() == ("emely", "edmond") assert m.tuple_item_get_size_t(tup) == 93 assert m.tuple_item_set_size_t() == ("candy", "cat") + + +def test_external_float_(): + r1 = m.square_float_(2.0) + assert r1 == 4.0 From f8a532a7ded45203cd755d53f0da5f1fadbbea2d Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 15 Mar 2022 10:18:12 -0400 Subject: [PATCH 2/5] [pre-commit.ci] pre-commit autoupdate (#3800) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit updates: - [github.com/asottile/pyupgrade: v2.31.0 → v2.31.1](https://github.com/asottile/pyupgrade/compare/v2.31.0...v2.31.1) - [github.com/sirosen/texthooks: 0.2.2 → 0.3.1](https://github.com/sirosen/texthooks/compare/0.2.2...0.3.1) - [github.com/hadialqattan/pycln: v1.2.4 → v1.2.5](https://github.com/hadialqattan/pycln/compare/v1.2.4...v1.2.5) - [github.com/pre-commit/mirrors-mypy: v0.931 → v0.940](https://github.com/pre-commit/mirrors-mypy/compare/v0.931...v0.940) - [github.com/mgedmin/check-manifest: 0.47 → 0.48](https://github.com/mgedmin/check-manifest/compare/0.47...0.48) Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> --- .pre-commit-config.yaml | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index e9a0e03f2..c8b527613 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -32,7 +32,7 @@ repos: # Upgrade old Python syntax - repo: https://github.com/asottile/pyupgrade - rev: "v2.31.0" + rev: "v2.31.1" hooks: - id: pyupgrade args: [--py36-plus] @@ -64,14 +64,14 @@ repos: - id: remove-tabs - repo: https://github.com/sirosen/texthooks - rev: "0.2.2" + rev: "0.3.1" hooks: - id: fix-ligatures - id: fix-smartquotes # Autoremoves unused imports - repo: https://github.com/hadialqattan/pycln - rev: "v1.2.4" + rev: "v1.2.5" hooks: - id: pycln @@ -122,7 +122,7 @@ repos: # Check static types with mypy - repo: https://github.com/pre-commit/mirrors-mypy - rev: "v0.931" + rev: "v0.940" hooks: - id: mypy args: [--show-error-codes] @@ -131,7 +131,7 @@ repos: # Checks the manifest for missing files (native support) - repo: https://github.com/mgedmin/check-manifest - rev: "0.47" + rev: "0.48" hooks: - id: check-manifest # This is a slow hook, so only run this if --hook-stage manual is passed From a7e7a6e8460767b70f11b375ea564d3856272260 Mon Sep 17 00:00:00 2001 From: Axel Huebl Date: Thu, 17 Mar 2022 11:21:28 -0700 Subject: [PATCH 3/5] Docs: No Strip in Debug (#3779) The docs were not 100% the same as we advertise with our tooling function: most users do not want to strip symbols in Debug builds. --- docs/compiling.rst | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/docs/compiling.rst b/docs/compiling.rst index 4ae9234e1..2b543be0b 100644 --- a/docs/compiling.rst +++ b/docs/compiling.rst @@ -505,7 +505,10 @@ You can use these targets to build complex applications. For example, the target_link_libraries(example PRIVATE pybind11::module pybind11::lto pybind11::windows_extras) pybind11_extension(example) - pybind11_strip(example) + if(NOT MSVC AND NOT ${CMAKE_BUILD_TYPE} MATCHES Debug|RelWithDebInfo) + # Strip unnecessary sections of the binary on Linux/macOS + pybind11_strip(example) + endif() set_target_properties(example PROPERTIES CXX_VISIBILITY_PRESET "hidden" CUDA_VISIBILITY_PRESET "hidden") From 8b1944d390e9a89f22a1e47d2acbe7c547662aad Mon Sep 17 00:00:00 2001 From: JonTriebenbach <40633432+JonTriebenbach@users.noreply.github.com> Date: Thu, 17 Mar 2022 14:51:16 -0500 Subject: [PATCH 4/5] Remove idioms in code comments (#3809) --- .github/workflows/ci.yml | 2 +- docs/advanced/pycpp/numpy.rst | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 11c93bafc..3523e82c0 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -667,7 +667,7 @@ jobs: # This verifies that the documentation is not horribly broken, and does a - # basic sanity check on the SDist. + # basic validation check on the SDist. doxygen: name: "Documentation build test" runs-on: ubuntu-latest diff --git a/docs/advanced/pycpp/numpy.rst b/docs/advanced/pycpp/numpy.rst index 8ad341004..b6ef019ed 100644 --- a/docs/advanced/pycpp/numpy.rst +++ b/docs/advanced/pycpp/numpy.rst @@ -87,7 +87,7 @@ buffer objects (e.g. a NumPy matrix). /* Request a buffer descriptor from Python */ py::buffer_info info = b.request(); - /* Some sanity checks ... */ + /* Some basic validation checks ... */ if (info.format != py::format_descriptor::format()) throw std::runtime_error("Incompatible format: expected a double array!"); From b3a43d137c6c9ecf271a2a3d51a51b2fd1c3e8f6 Mon Sep 17 00:00:00 2001 From: Laramie Leavitt Date: Fri, 18 Mar 2022 11:10:31 -0700 Subject: [PATCH 5/5] Use rvalue reference for std::variant cast_op (#3811) Nearly every call site of cast_op uses an r-value reference. Except stl.h variant_caster::load_alternative for handling std::variant. Fix that. --- include/pybind11/stl.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/pybind11/stl.h b/include/pybind11/stl.h index b66129688..e3a061444 100644 --- a/include/pybind11/stl.h +++ b/include/pybind11/stl.h @@ -372,7 +372,7 @@ struct variant_caster> { bool load_alternative(handle src, bool convert, type_list) { auto caster = make_caster(); if (caster.load(src, convert)) { - value = cast_op(caster); + value = cast_op(std::move(caster)); return true; } return load_alternative(src, convert, type_list{});