From 44678e5476db5b0d46a03bb4625c03a6ea7e49c8 Mon Sep 17 00:00:00 2001 From: "Ralf W. Grosse-Kunstleve" Date: Fri, 5 Mar 2021 14:45:57 -0800 Subject: [PATCH 001/638] Shuffling code in test_multiple_inheritance.cpp to separate struct/class definitions from bindings code. (#2890) --- tests/test_multiple_inheritance.cpp | 46 ++++++++++++++++++----------- 1 file changed, 29 insertions(+), 17 deletions(-) diff --git a/tests/test_multiple_inheritance.cpp b/tests/test_multiple_inheritance.cpp index e67200809d..ad801fe198 100644 --- a/tests/test_multiple_inheritance.cpp +++ b/tests/test_multiple_inheritance.cpp @@ -11,6 +11,8 @@ #include "pybind11_tests.h" #include "constructor_stats.h" +namespace { + // Many bases for testing that multiple inheritance from many classes (i.e. requiring extra // space for holder constructed flags) works. template struct BaseN { @@ -43,7 +45,34 @@ int WithStatic2::static_value2 = 2; int VanillaStaticMix1::static_value = 12; int VanillaStaticMix2::static_value = 12; +// test_multiple_inheritance_virtbase +struct Base1a { + Base1a(int i) : i(i) { } + int foo() { return i; } + int i; +}; +struct Base2a { + Base2a(int i) : i(i) { } + int bar() { return i; } + int i; +}; +struct Base12a : Base1a, Base2a { + Base12a(int i, int j) : Base1a(i), Base2a(j) { } +}; + +// test_mi_unaligned_base +// test_mi_base_return +struct I801B1 { int a = 1; I801B1() = default; I801B1(const I801B1 &) = default; virtual ~I801B1() = default; }; +struct I801B2 { int b = 2; I801B2() = default; I801B2(const I801B2 &) = default; virtual ~I801B2() = default; }; +struct I801C : I801B1, I801B2 {}; +struct I801D : I801C {}; // Indirect MI + +} // namespace + TEST_SUBMODULE(multiple_inheritance, m) { + // Please do not interleave `struct` and `class` definitions with bindings code, + // but implement `struct`s and `class`es in the anonymous namespace above. + // This helps keeping the smart_holder branch in sync with master. // test_multiple_inheritance_mix1 // test_multiple_inheritance_mix2 @@ -99,27 +128,14 @@ TEST_SUBMODULE(multiple_inheritance, m) { // test_multiple_inheritance_virtbase // Test the case where not all base classes are specified, and where pybind11 requires the // py::multiple_inheritance flag to perform proper casting between types. - struct Base1a { - Base1a(int i) : i(i) { } - int foo() { return i; } - int i; - }; py::class_>(m, "Base1a") .def(py::init()) .def("foo", &Base1a::foo); - struct Base2a { - Base2a(int i) : i(i) { } - int bar() { return i; } - int i; - }; py::class_>(m, "Base2a") .def(py::init()) .def("bar", &Base2a::bar); - struct Base12a : Base1a, Base2a { - Base12a(int i, int j) : Base1a(i), Base2a(j) { } - }; py::class_>(m, "Base12a", py::multiple_inheritance()) .def(py::init()); @@ -130,10 +146,6 @@ TEST_SUBMODULE(multiple_inheritance, m) { // test_mi_unaligned_base // test_mi_base_return // Issue #801: invalid casting to derived type with MI bases - struct I801B1 { int a = 1; I801B1() = default; I801B1(const I801B1 &) = default; virtual ~I801B1() = default; }; - struct I801B2 { int b = 2; I801B2() = default; I801B2(const I801B2 &) = default; virtual ~I801B2() = default; }; - struct I801C : I801B1, I801B2 {}; - struct I801D : I801C {}; // Indirect MI // Unregistered classes: struct I801B3 { int c = 3; virtual ~I801B3() = default; }; struct I801E : I801B3, I801D {}; From f110889dde14da15dab08d431791a2080f9a9357 Mon Sep 17 00:00:00 2001 From: Boris Staletic Date: Mon, 8 Mar 2021 08:42:01 +0100 Subject: [PATCH 002/638] Use correct duration representation when casting from datetime.timdelta to std::chrono::duration (#2870) * Use correct duration representation when casting from datetime.timdelta to std::chrono::duration * When asserting datetime/timedelta/date/time we can equality-compare whole objects --- include/pybind11/chrono.h | 2 +- tests/test_chrono.py | 35 +++++++++++++---------------------- 2 files changed, 14 insertions(+), 23 deletions(-) diff --git a/include/pybind11/chrono.h b/include/pybind11/chrono.h index 551a802ad3..317afd1837 100644 --- a/include/pybind11/chrono.h +++ b/include/pybind11/chrono.h @@ -35,7 +35,7 @@ template class duration_caster { using rep = typename type::rep; using period = typename type::period; - using days = std::chrono::duration>; + using days = std::chrono::duration>; // signed 25 bits required by the standard. bool load(handle src, bool) { using namespace std::chrono; diff --git a/tests/test_chrono.py b/tests/test_chrono.py index e9e24e0826..12312c7207 100644 --- a/tests/test_chrono.py +++ b/tests/test_chrono.py @@ -39,9 +39,7 @@ def test_chrono_system_clock_roundtrip(): # They should be identical (no information lost on roundtrip) diff = abs(date1 - date2) - assert diff.days == 0 - assert diff.seconds == 0 - assert diff.microseconds == 0 + assert diff == datetime.timedelta(0) def test_chrono_system_clock_roundtrip_date(): @@ -64,9 +62,7 @@ def test_chrono_system_clock_roundtrip_date(): assert diff.microseconds == 0 # Year, Month & Day should be the same after the round trip - assert date1.year == date2.year - assert date1.month == date2.month - assert date1.day == date2.day + assert date1 == date2 # There should be no time information assert time2.hour == 0 @@ -117,10 +113,7 @@ def test_chrono_system_clock_roundtrip_time(time1, tz, monkeypatch): assert isinstance(time2, datetime.time) # Hour, Minute, Second & Microsecond should be the same after the round trip - assert time1.hour == time2.hour - assert time1.minute == time2.minute - assert time1.second == time2.second - assert time1.microsecond == time2.microsecond + assert time1 == time2 # There should be no date information (i.e. date = python base date) assert date2.year == 1970 @@ -140,9 +133,13 @@ def test_chrono_duration_roundtrip(): cpp_diff = m.test_chrono3(diff) - assert cpp_diff.days == diff.days - assert cpp_diff.seconds == diff.seconds - assert cpp_diff.microseconds == diff.microseconds + assert cpp_diff == diff + + # Negative timedelta roundtrip + diff = datetime.timedelta(microseconds=-1) + cpp_diff = m.test_chrono3(diff) + + assert cpp_diff == diff def test_chrono_duration_subtraction_equivalence(): @@ -153,9 +150,7 @@ def test_chrono_duration_subtraction_equivalence(): diff = date2 - date1 cpp_diff = m.test_chrono4(date2, date1) - assert cpp_diff.days == diff.days - assert cpp_diff.seconds == diff.seconds - assert cpp_diff.microseconds == diff.microseconds + assert cpp_diff == diff def test_chrono_duration_subtraction_equivalence_date(): @@ -166,9 +161,7 @@ def test_chrono_duration_subtraction_equivalence_date(): diff = date2 - date1 cpp_diff = m.test_chrono4(date2, date1) - assert cpp_diff.days == diff.days - assert cpp_diff.seconds == diff.seconds - assert cpp_diff.microseconds == diff.microseconds + assert cpp_diff == diff def test_chrono_steady_clock(): @@ -183,9 +176,7 @@ def test_chrono_steady_clock_roundtrip(): assert isinstance(time2, datetime.timedelta) # They should be identical (no information lost on roundtrip) - assert time1.days == time2.days - assert time1.seconds == time2.seconds - assert time1.microseconds == time2.microseconds + assert time1 == time2 def test_floating_point_duration(): From 0e01c243c7ffae3a2e52f998bacfe82f56aa96d9 Mon Sep 17 00:00:00 2001 From: "Ralf W. Grosse-Kunstleve" Date: Tue, 9 Mar 2021 15:07:44 -0800 Subject: [PATCH 003/638] Generalizing suppression for pypocketfft. (#2896) * Adding suppression for pypocketfft. * Generalize existing pypocketfft Valgrind suppression Co-authored-by: Yannick Jadoul --- tests/valgrind-numpy-scipy.supp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/valgrind-numpy-scipy.supp b/tests/valgrind-numpy-scipy.supp index e8ff680dd3..fea9bcb4a6 100644 --- a/tests/valgrind-numpy-scipy.supp +++ b/tests/valgrind-numpy-scipy.supp @@ -111,7 +111,7 @@ fun:_Znwm fun:PyInit_pypocketfft fun:_PyImport_LoadDynamicModuleWithSpec - fun:_imp_create_dynamic_impl.constprop.3 + fun:_imp_create_dynamic_impl.constprop.* fun:_imp_create_dynamic fun:cfunction_vectorcall_FASTCALL fun:PyVectorcall_Call From 3df0ee6fe36f85786477fb1d15dc68ea9087b073 Mon Sep 17 00:00:00 2001 From: JYX <1449843302@qq.com> Date: Fri, 2 Apr 2021 23:46:43 +0800 Subject: [PATCH 004/638] docs: typo in classes.rst (#2926) --- docs/advanced/classes.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/advanced/classes.rst b/docs/advanced/classes.rst index 4927902069..3dfc0c935b 100644 --- a/docs/advanced/classes.rst +++ b/docs/advanced/classes.rst @@ -259,7 +259,7 @@ override the ``name()`` method): .. note:: - Note the trailing commas in the ``PYBIND11_OVERIDE`` calls to ``name()`` + Note the trailing commas in the ``PYBIND11_OVERRIDE`` calls to ``name()`` and ``bark()``. These are needed to portably implement a trampoline for a function that does not take any arguments. For functions that take a nonzero number of arguments, the trailing comma must be omitted. From c2db53da56af7bce1dabf1b3c71913385111f49b Mon Sep 17 00:00:00 2001 From: Robert Haschke Date: Fri, 2 Apr 2021 19:13:44 +0200 Subject: [PATCH 005/638] fix: catch missing self argument in overloads constructor (#2914) --- include/pybind11/pybind11.h | 4 ++-- tests/test_factory_constructors.py | 10 ++++++---- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/include/pybind11/pybind11.h b/include/pybind11/pybind11.h index 648300eef2..eff40a8495 100644 --- a/include/pybind11/pybind11.h +++ b/include/pybind11/pybind11.h @@ -555,8 +555,8 @@ class cpp_function : public function { auto self_value_and_holder = value_and_holder(); if (overloads->is_constructor) { - if (!PyObject_TypeCheck(parent.ptr(), (PyTypeObject *) overloads->scope.ptr())) { - PyErr_SetString(PyExc_TypeError, "__init__(self, ...) called with invalid `self` argument"); + if (!parent || !PyObject_TypeCheck(parent.ptr(), (PyTypeObject *) overloads->scope.ptr())) { + PyErr_SetString(PyExc_TypeError, "__init__(self, ...) called with invalid or missing `self` argument"); return nullptr; } diff --git a/tests/test_factory_constructors.py b/tests/test_factory_constructors.py index ffcce6fd4d..8c6ca173ac 100644 --- a/tests/test_factory_constructors.py +++ b/tests/test_factory_constructors.py @@ -486,7 +486,9 @@ def __init__(self, bad): # Same as above, but for a class with an alias: class BrokenTF6(m.TestFactory6): def __init__(self, bad): - if bad == 1: + if bad == 0: + m.TestFactory6.__init__() + elif bad == 1: a = m.TestFactory2(tag.pointer, 1) m.TestFactory6.__init__(a, tag.base, 1) elif bad == 2: @@ -506,13 +508,13 @@ def __init__(self, bad): BrokenTF1(arg) assert ( str(excinfo.value) - == "__init__(self, ...) called with invalid `self` argument" + == "__init__(self, ...) called with invalid or missing `self` argument" ) - for arg in (1, 2, 3, 4): + for arg in (0, 1, 2, 3, 4): with pytest.raises(TypeError) as excinfo: BrokenTF6(arg) assert ( str(excinfo.value) - == "__init__(self, ...) called with invalid `self` argument" + == "__init__(self, ...) called with invalid or missing `self` argument" ) From 1259db6fd99bc60228ef0e4af2e379f8e86abc1d Mon Sep 17 00:00:00 2001 From: Jerome Robert Date: Fri, 2 Apr 2021 19:26:17 +0200 Subject: [PATCH 006/638] Fix Pybind11Extension on mingw64 (#2921) * Pybind11Extension add the "/EHsc /bigobj /std:c++14" flags on Windows. This is good for Visual C++ but not for Mingw. * According https://github.com/msys2/MINGW-packages/blob/master/mingw-w64-python2/0410-MINGW-build-extensions-with-GCC.patch sysconfig.get_platform() is the way to check for a Mingw64 --- pybind11/setup_helpers.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pybind11/setup_helpers.py b/pybind11/setup_helpers.py index c69064ca50..84a4584c5d 100644 --- a/pybind11/setup_helpers.py +++ b/pybind11/setup_helpers.py @@ -47,6 +47,7 @@ import threading import platform import warnings +import sysconfig try: from setuptools.command.build_ext import build_ext as _build_ext @@ -59,7 +60,7 @@ import distutils.ccompiler -WIN = sys.platform.startswith("win32") +WIN = sys.platform.startswith("win32") and sysconfig.get_platform() != "mingw" PY2 = sys.version_info[0] < 3 MACOS = sys.platform.startswith("darwin") STD_TMPL = "/std:c++{}" if WIN else "-std=c++{}" From f676782becc5bcd30c56b4da1ff1026db61bcce3 Mon Sep 17 00:00:00 2001 From: Eric Cousineau Date: Fri, 2 Apr 2021 14:34:09 -0400 Subject: [PATCH 007/638] env: Add surrogate for pytest.deprecated_call for ptyest<3.9 (#2923) env: Add surrogate for pytest.deprecated_call for ptyest<3.9 --- tests/env.py | 19 +++++++++++++++++++ tests/test_builtin_casters.py | 4 ++-- 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/tests/env.py b/tests/env.py index 5cded44127..6172b451b3 100644 --- a/tests/env.py +++ b/tests/env.py @@ -2,6 +2,8 @@ import platform import sys +import pytest + LINUX = sys.platform.startswith("linux") MACOS = sys.platform.startswith("darwin") WIN = sys.platform.startswith("win32") or sys.platform.startswith("cygwin") @@ -12,3 +14,20 @@ PY2 = sys.version_info.major == 2 PY = sys.version_info + + +def deprecated_call(): + """ + pytest.deprecated_call() seems broken in pytest<3.9.x; concretely, it + doesn't work on CPython 3.8.0 with pytest==3.3.2 on Ubuntu 18.04 (#2922). + + This is a narrowed reimplementation of the following PR :( + https://github.com/pytest-dev/pytest/pull/4104 + """ + # TODO: Remove this when testing requires pytest>=3.9. + pieces = pytest.__version__.split(".") + pytest_major_minor = (int(pieces[0]), int(pieces[1])) + if pytest_major_minor < (3, 9): + return pytest.warns((DeprecationWarning, PendingDeprecationWarning)) + else: + return pytest.deprecated_call() diff --git a/tests/test_builtin_casters.py b/tests/test_builtin_casters.py index fda0384333..5ee8603a6b 100644 --- a/tests/test_builtin_casters.py +++ b/tests/test_builtin_casters.py @@ -301,7 +301,7 @@ def cant_convert(v): cant_convert(3.14159) # TODO: Avoid DeprecationWarning in `PyLong_AsLong` (and similar) if (3, 8) <= env.PY < (3, 10): - with pytest.deprecated_call(): + with env.deprecated_call(): assert convert(Int()) == 42 else: assert convert(Int()) == 42 @@ -336,7 +336,7 @@ def require_implicit(v): # The implicit conversion from np.float32 is undesirable but currently accepted. # TODO: Avoid DeprecationWarning in `PyLong_AsLong` (and similar) if (3, 8) <= env.PY < (3, 10): - with pytest.deprecated_call(): + with env.deprecated_call(): assert convert(np.float32(3.14159)) == 3 else: assert convert(np.float32(3.14159)) == 3 From ad6bf5cd39ca64b4a9bf846b84b11c4c8df1c8e1 Mon Sep 17 00:00:00 2001 From: "Ralf W. Grosse-Kunstleve" Date: Fri, 2 Apr 2021 18:17:12 -0700 Subject: [PATCH 008/638] Adding PyGILState_Check() in object_api<>::operator(). (#2919) * Adding PyGILState_Check() in object_api<>::operator(). * Enabling PyGILState_Check() for Python >= 3.6 only. Possibly, this explains why PyGILState_Check() cannot safely be used with Python 3.4 and 3.5: https://github.com/python/cpython/pull/10267#issuecomment-434881587 * Adding simple micro benchmark. * Reducing test time to minimum (purely for coverage, not for accurate results). * Fixing silly oversight. * Minor code organization improvement in test. * Adding example runtimes. * Removing capsys (just run with `-k test_callback_num_times -s` and using `.format()`. --- include/pybind11/cast.h | 5 +++++ tests/test_callbacks.cpp | 6 ++++++ tests/test_callbacks.py | 32 ++++++++++++++++++++++++++++++++ 3 files changed, 43 insertions(+) diff --git a/include/pybind11/cast.h b/include/pybind11/cast.h index 0ad10e9a93..f68a35bfa4 100644 --- a/include/pybind11/cast.h +++ b/include/pybind11/cast.h @@ -1348,6 +1348,11 @@ unpacking_collector collect_arguments(Args &&...args) { template template object object_api::operator()(Args &&...args) const { +#if !defined(NDEBUG) && PY_VERSION_HEX >= 0x03060000 + if (!PyGILState_Check()) { + pybind11_fail("pybind11::object_api<>::operator() PyGILState_Check() failure."); + } +#endif return detail::collect_arguments(std::forward(args)...).call(derived().ptr()); } diff --git a/tests/test_callbacks.cpp b/tests/test_callbacks.cpp index dffe538fc5..61bc3a8f06 100644 --- a/tests/test_callbacks.cpp +++ b/tests/test_callbacks.cpp @@ -172,4 +172,10 @@ TEST_SUBMODULE(callbacks, m) { for (auto i : work) start_f(py::cast(i)); }); + + m.def("callback_num_times", [](py::function f, std::size_t num) { + for (std::size_t i = 0; i < num; i++) { + f(); + } + }); } diff --git a/tests/test_callbacks.py b/tests/test_callbacks.py index 039b877ced..cec68bda5c 100644 --- a/tests/test_callbacks.py +++ b/tests/test_callbacks.py @@ -2,6 +2,7 @@ import pytest from pybind11_tests import callbacks as m from threading import Thread +import time def test_callbacks(): @@ -146,3 +147,34 @@ def test_async_async_callbacks(): t = Thread(target=test_async_callbacks) t.start() t.join() + + +def test_callback_num_times(): + # Super-simple micro-benchmarking related to PR #2919. + # Example runtimes (Intel Xeon 2.2GHz, fully optimized): + # num_millions 1, repeats 2: 0.1 secs + # num_millions 20, repeats 10: 11.5 secs + one_million = 1000000 + num_millions = 1 # Try 20 for actual micro-benchmarking. + repeats = 2 # Try 10. + rates = [] + for rep in range(repeats): + t0 = time.time() + m.callback_num_times(lambda: None, num_millions * one_million) + td = time.time() - t0 + rate = num_millions / td if td else 0 + rates.append(rate) + if not rep: + print() + print( + "callback_num_times: {:d} million / {:.3f} seconds = {:.3f} million / second".format( + num_millions, td, rate + ) + ) + if len(rates) > 1: + print("Min Mean Max") + print( + "{:6.3f} {:6.3f} {:6.3f}".format( + min(rates), sum(rates) / len(rates), max(rates) + ) + ) From 16c23fef312bcb98c951b71170a49508e1de4eb7 Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Mon, 5 Apr 2021 22:53:59 -0400 Subject: [PATCH 009/638] chore(deps): bump pre-commit/action from v2.0.0 to v2.0.2 (#2935) --- .github/workflows/format.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/format.yml b/.github/workflows/format.yml index 5cebed17da..c1d576e13b 100644 --- a/.github/workflows/format.yml +++ b/.github/workflows/format.yml @@ -19,7 +19,7 @@ jobs: steps: - uses: actions/checkout@v2 - uses: actions/setup-python@v2 - - uses: pre-commit/action@v2.0.0 + - uses: pre-commit/action@v2.0.2 with: # Slow hooks are marked with manual - slow is okay here, run them too extra_args: --hook-stage manual --all-files From 4f0727f2804f96b896155f1a680ea6da23cd4407 Mon Sep 17 00:00:00 2001 From: Weiming Zhao Date: Mon, 5 Apr 2021 20:50:50 -0700 Subject: [PATCH 010/638] Fix the enabling of default extension handling (#2938) --- tools/pybind11NewTools.cmake | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tools/pybind11NewTools.cmake b/tools/pybind11NewTools.cmake index 18da8be170..a20803bc74 100644 --- a/tools/pybind11NewTools.cmake +++ b/tools/pybind11NewTools.cmake @@ -230,7 +230,7 @@ function(pybind11_add_module target_name) endif() # If we don't pass a WITH_SOABI or WITHOUT_SOABI, use our own default handling of extensions - if(NOT ARG_WITHOUT_SOABI OR NOT "WITH_SOABI" IN_LIST ARG_UNPARSED_ARGUMENTS) + if(NOT ARG_WITHOUT_SOABI AND NOT "WITH_SOABI" IN_LIST ARG_UNPARSED_ARGUMENTS) pybind11_extension(${target_name}) endif() From e0c1dadb757296c8bc91760a62bfb48b48c39530 Mon Sep 17 00:00:00 2001 From: Henry Schreiner Date: Sat, 10 Apr 2021 11:12:56 -0400 Subject: [PATCH 011/638] chore: add myself to CODEOWNERS (#2940) * chore: add myself to CODEOWNERS This will ensure I get notified about pull requests where these files change. * Update .github/CODEOWNERS --- .github/CODEOWNERS | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 .github/CODEOWNERS diff --git a/.github/CODEOWNERS b/.github/CODEOWNERS new file mode 100644 index 0000000000..60af5e7971 --- /dev/null +++ b/.github/CODEOWNERS @@ -0,0 +1,6 @@ +*.cmake @henryiii +CMakeLists.txt @henryiii +*.yml @henryiii +*.yaml @henryiii +/tools/ @henryiii +/pybind11/ @henryiii From 6709abba934e968d31c24f20c132c67a6f424a6a Mon Sep 17 00:00:00 2001 From: Tamaki Nishino Date: Wed, 14 Apr 2021 08:53:56 +0900 Subject: [PATCH 012/638] Allow function pointer extraction from overloaded functions (#2944) * Add a failure test for overloaded functions * Allow function pointer extraction from overloaded functions --- include/pybind11/functional.h | 16 +++++++++++----- tests/test_callbacks.cpp | 2 ++ tests/test_callbacks.py | 4 ++++ 3 files changed, 17 insertions(+), 5 deletions(-) diff --git a/include/pybind11/functional.h b/include/pybind11/functional.h index 92c17dc225..aee9be4e45 100644 --- a/include/pybind11/functional.h +++ b/include/pybind11/functional.h @@ -46,11 +46,17 @@ struct type_caster> { auto c = reinterpret_borrow(PyCFunction_GET_SELF(cfunc.ptr())); auto rec = (function_record *) c; - if (rec && rec->is_stateless && - same_type(typeid(function_type), *reinterpret_cast(rec->data[1]))) { - struct capture { function_type f; }; - value = ((capture *) &rec->data)->f; - return true; + while (rec != nullptr) { + if (rec->is_stateless + && same_type(typeid(function_type), + *reinterpret_cast(rec->data[1]))) { + struct capture { + function_type f; + }; + value = ((capture *) &rec->data)->f; + return true; + } + rec = rec->next; } } diff --git a/tests/test_callbacks.cpp b/tests/test_callbacks.cpp index 61bc3a8f06..33927f5c38 100644 --- a/tests/test_callbacks.cpp +++ b/tests/test_callbacks.cpp @@ -97,6 +97,8 @@ TEST_SUBMODULE(callbacks, m) { // test_cpp_function_roundtrip /* Test if passing a function pointer from C++ -> Python -> C++ yields the original pointer */ m.def("dummy_function", &dummy_function); + m.def("dummy_function_overloaded", [](int i, int j) { return i + j; }); + m.def("dummy_function_overloaded", &dummy_function); m.def("dummy_function2", [](int i, int j) { return i + j; }); m.def("roundtrip", [](std::function f, bool expect_none = false) { if (expect_none && f) diff --git a/tests/test_callbacks.py b/tests/test_callbacks.py index cec68bda5c..3526614304 100644 --- a/tests/test_callbacks.py +++ b/tests/test_callbacks.py @@ -93,6 +93,10 @@ def test_cpp_function_roundtrip(): m.test_dummy_function(m.roundtrip(m.dummy_function)) == "matches dummy_function: eval(1) = 2" ) + assert ( + m.test_dummy_function(m.dummy_function_overloaded) + == "matches dummy_function: eval(1) = 2" + ) assert m.roundtrip(None, expect_none=True) is None assert ( m.test_dummy_function(lambda x: x + 2) From 62976cfcb8ccc76a79849028d7f5a3f8c7019007 Mon Sep 17 00:00:00 2001 From: Philipp Bucher Date: Wed, 14 Apr 2021 20:01:27 +0200 Subject: [PATCH 013/638] fix: using -Werror-all for Intel (#2948) * correcting Werror for Intel * adding ward for Intel * adding wards for intel * another ward for Intel * missed one intel ward * exact match for intel compiler * removing inline limits * disable warnings about inline limits * formatter suggestion * more indent * hopefully make formatter happy * addressed review * fix && * Update tests/CMakeLists.txt Co-authored-by: Henry Schreiner Co-authored-by: Henry Schreiner --- include/pybind11/pybind11.h | 6 +++--- tests/CMakeLists.txt | 9 ++++++++- tests/test_constants_and_functions.cpp | 8 ++++---- tests/test_operator_overloading.cpp | 6 +++--- 4 files changed, 18 insertions(+), 11 deletions(-) diff --git a/include/pybind11/pybind11.h b/include/pybind11/pybind11.h index eff40a8495..754c875cda 100644 --- a/include/pybind11/pybind11.h +++ b/include/pybind11/pybind11.h @@ -143,12 +143,12 @@ class cpp_function : public function { /* Without these pragmas, GCC warns that there might not be enough space to use the placement new operator. However, the 'if' statement above ensures that this is the case. */ -#if defined(__GNUG__) && !defined(__clang__) && __GNUC__ >= 6 +#if defined(__GNUG__) && __GNUC__ >= 6 && !defined(__clang__) && !defined(__INTEL_COMPILER) # pragma GCC diagnostic push # pragma GCC diagnostic ignored "-Wplacement-new" #endif new ((capture *) &rec->data) capture { std::forward(f) }; -#if defined(__GNUG__) && !defined(__clang__) && __GNUC__ >= 6 +#if defined(__GNUG__) && __GNUC__ >= 6 && !defined(__clang__) && !defined(__INTEL_COMPILER) # pragma GCC diagnostic pop #endif if (!std::is_trivially_destructible::value) @@ -2283,6 +2283,6 @@ PYBIND11_NAMESPACE_END(PYBIND11_NAMESPACE) #if defined(_MSC_VER) && !defined(__INTEL_COMPILER) # pragma warning(pop) -#elif defined(__GNUG__) && !defined(__clang__) +#elif defined(__GNUG__) && !defined(__clang__) && !defined(__INTEL_COMPILER) # pragma GCC diagnostic pop #endif diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 9ff6c0dd9a..3d89404911 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -268,8 +268,15 @@ function(pybind11_enable_warnings target_name) target_compile_options(${target_name} PRIVATE /WX) elseif(PYBIND11_CUDA_TESTS) target_compile_options(${target_name} PRIVATE "SHELL:-Werror all-warnings") - elseif(CMAKE_CXX_COMPILER_ID MATCHES "(GNU|Intel|Clang)") + elseif(CMAKE_CXX_COMPILER_ID MATCHES "(GNU|Clang|IntelLLVM)") target_compile_options(${target_name} PRIVATE -Werror) + elseif(CMAKE_CXX_COMPILER_ID STREQUAL "Intel") + target_compile_options( + ${target_name} + PRIVATE + -Werror-all + # "Inlining inhibited by limit max-size", "Inlining inhibited by limit max-total-size" + -diag-disable 11074,11076) endif() endif() diff --git a/tests/test_constants_and_functions.cpp b/tests/test_constants_and_functions.cpp index 8855dd7d7c..5bbbd0c70b 100644 --- a/tests/test_constants_and_functions.cpp +++ b/tests/test_constants_and_functions.cpp @@ -56,12 +56,12 @@ int f1(int x) noexcept { return x+1; } #endif int f2(int x) noexcept(true) { return x+2; } int f3(int x) noexcept(false) { return x+3; } -#if defined(__GNUG__) +#if defined(__GNUG__) && !defined(__INTEL_COMPILER) # pragma GCC diagnostic push # pragma GCC diagnostic ignored "-Wdeprecated" #endif int f4(int x) throw() { return x+4; } // Deprecated equivalent to noexcept(true) -#if defined(__GNUG__) +#if defined(__GNUG__) && !defined(__INTEL_COMPILER) # pragma GCC diagnostic pop #endif struct C { @@ -71,13 +71,13 @@ struct C { int m4(int x) const noexcept(true) { return x-4; } int m5(int x) noexcept(false) { return x-5; } int m6(int x) const noexcept(false) { return x-6; } -#if defined(__GNUG__) +#if defined(__GNUG__) && !defined(__INTEL_COMPILER) # pragma GCC diagnostic push # pragma GCC diagnostic ignored "-Wdeprecated" #endif int m7(int x) throw() { return x-7; } int m8(int x) const throw() { return x-8; } -#if defined(__GNUG__) +#if defined(__GNUG__) && !defined(__INTEL_COMPILER) # pragma GCC diagnostic pop #endif }; diff --git a/tests/test_operator_overloading.cpp b/tests/test_operator_overloading.cpp index 0a27bfd57b..eb3167dc08 100644 --- a/tests/test_operator_overloading.cpp +++ b/tests/test_operator_overloading.cpp @@ -80,8 +80,8 @@ std::string abs(const Vector2&) { return "abs(Vector2)"; } -// MSVC warns about unknown pragmas, and warnings are errors. -#ifndef _MSC_VER +// MSVC & Intel warns about unknown pragmas, and warnings are errors. +#if !defined(_MSC_VER) && !defined(__INTEL_COMPILER) #pragma GCC diagnostic push // clang 7.0.0 and Apple LLVM 10.0.1 introduce `-Wself-assign-overloaded` to // `-Wall`, which is used here for overloading (e.g. `py::self += py::self `). @@ -221,6 +221,6 @@ TEST_SUBMODULE(operators, m) { .def(py::self == py::self); } -#ifndef _MSC_VER +#if !defined(_MSC_VER) && !defined(__INTEL_COMPILER) #pragma GCC diagnostic pop #endif From 114be7f4ade0ad798cd4c7f5d65ebe4ba8bd892d Mon Sep 17 00:00:00 2001 From: Henry Schreiner Date: Thu, 15 Apr 2021 18:27:16 -0400 Subject: [PATCH 014/638] docs: remove recommonmark (#2955) --- docs/requirements.txt | 2 -- 1 file changed, 2 deletions(-) diff --git a/docs/requirements.txt b/docs/requirements.txt index a1276a65d9..671dc4b3f3 100644 --- a/docs/requirements.txt +++ b/docs/requirements.txt @@ -1,6 +1,4 @@ breathe==4.26.1 -commonmark==0.9.1 -recommonmark==0.7.1 sphinx==3.3.1 sphinx_rtd_theme==0.5.0 sphinxcontrib-moderncmakedomain==3.17 From e08a58111dbea38d667b209f7543864d51a3b185 Mon Sep 17 00:00:00 2001 From: mvoelkle-cern <54059203+mvoelkle-cern@users.noreply.github.com> Date: Mon, 19 Apr 2021 19:53:57 +0200 Subject: [PATCH 015/638] Fix compilation with gcc < 5 (#2956) When the user defines _GLIBCXX_USE_CXX11_ABI=0 to force the pre-c++11 ABI, numpy.h assumes that is_trivially_copyable is available. It is not necessarily the case. This patch uses clang's feature detection instead. The workaround is for certain libstdc++ versions, so the test should target these particular versions. --- include/pybind11/numpy.h | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/include/pybind11/numpy.h b/include/pybind11/numpy.h index d1ddc4a9db..dd7f43d16a 100644 --- a/include/pybind11/numpy.h +++ b/include/pybind11/numpy.h @@ -319,14 +319,13 @@ template using remove_all_extents_t = typename array_info::type; template using is_pod_struct = all_of< std::is_standard_layout, // since we're accessing directly in memory we need a standard layout type -#if !defined(__GNUG__) || defined(_LIBCPP_VERSION) || defined(_GLIBCXX_USE_CXX11_ABI) - // _GLIBCXX_USE_CXX11_ABI indicates that we're using libstdc++ from GCC 5 or newer, independent - // of the actual compiler (Clang can also use libstdc++, but it always defines __GNUC__ == 4). - std::is_trivially_copyable, -#else - // GCC 4 doesn't implement is_trivially_copyable, so approximate it +#if defined(__GLIBCXX__) && (__GLIBCXX__ < 20150422 || __GLIBCXX__ == 20150623 || __GLIBCXX__ == 20150626 || __GLIBCXX__ == 20160803) + // libstdc++ < 5 (including versions 4.8.5, 4.9.3 and 4.9.4 which were released after 5) + // don't implement is_trivially_copyable, so approximate it std::is_trivially_destructible, satisfies_any_of, +#else + std::is_trivially_copyable, #endif satisfies_none_of >; From 5e4804bb66e7d1915558adca1803db0815398fce Mon Sep 17 00:00:00 2001 From: Henry Schreiner Date: Wed, 21 Apr 2021 13:30:01 -0400 Subject: [PATCH 016/638] tests: use master commit for pytest on 3.10 (#2967) --- tests/requirements.txt | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/requirements.txt b/tests/requirements.txt index 257d8d85fb..00cb5f119f 100644 --- a/tests/requirements.txt +++ b/tests/requirements.txt @@ -5,7 +5,8 @@ numpy==1.19.3; (platform_python_implementation!="PyPy" or sys_platform=="linux") numpy==1.20.0; (platform_python_implementation!="PyPy" or sys_platform=="linux") and python_version>="3.7" and python_version<"3.10" pytest==4.6.9; python_version<"3.5" pytest==6.1.2; python_version=="3.5" -pytest==6.2.1; python_version>="3.6" +pytest==6.2.1; python_version>="3.6" and python_version<="3.9" +pytest @ git+https://github.com/pytest-dev/pytest@c117bc350ec1e570672fda3b2ad234fd52e72b53; python_version>="3.10" pytest-timeout scipy==1.2.3; (platform_python_implementation!="PyPy" or sys_platform=="linux") and python_version<"3.6" scipy==1.5.4; (platform_python_implementation!="PyPy" or sys_platform=="linux") and python_version>="3.6" and python_version<"3.10" From bca4b36ba75957b463463a35ad7dc98b8326b04c Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Wed, 21 Apr 2021 15:00:00 -0400 Subject: [PATCH 017/638] chore(deps): bump pre-commit/action from v2.0.2 to v2.0.3 (#2964) Bumps [pre-commit/action](https://github.com/pre-commit/action) from v2.0.2 to v2.0.3. - [Release notes](https://github.com/pre-commit/action/releases) - [Commits](https://github.com/pre-commit/action/compare/v2.0.2...9b88afc9cd57fd75b655d5c71bd38146d07135fe) Signed-off-by: dependabot[bot] Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> --- .github/workflows/format.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/format.yml b/.github/workflows/format.yml index c1d576e13b..6550acccdb 100644 --- a/.github/workflows/format.yml +++ b/.github/workflows/format.yml @@ -19,7 +19,7 @@ jobs: steps: - uses: actions/checkout@v2 - uses: actions/setup-python@v2 - - uses: pre-commit/action@v2.0.2 + - uses: pre-commit/action@v2.0.3 with: # Slow hooks are marked with manual - slow is okay here, run them too extra_args: --hook-stage manual --all-files From 54430436fee2afc4f8443691075a6208f9ea8eba Mon Sep 17 00:00:00 2001 From: Henry Schreiner Date: Thu, 22 Apr 2021 00:03:07 -0400 Subject: [PATCH 018/638] ci: install Boost for boost checks (#2968) --- .github/workflows/ci.yml | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index a1bdec7bd7..19e1030d82 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -66,9 +66,13 @@ jobs: with: python-version: ${{ matrix.python }} - - name: Setup Boost (Windows / Linux latest) - shell: bash - run: echo "BOOST_ROOT=$BOOST_ROOT_1_72_0" >> $GITHUB_ENV + - name: Setup Boost (Linux) + if: runner.os == 'Linux' + run: sudo apt-get install libboost-dev + + - name: Setup Boost (macOS) + if: runner.os == 'macOS' + run: brew install boost - name: Update CMake uses: jwlawson/actions-setup-cmake@v1.8 From 0c93a0f3fcf6bf26be584558d7426564720cea6f Mon Sep 17 00:00:00 2001 From: Pieter P Date: Tue, 4 May 2021 07:04:38 +0200 Subject: [PATCH 019/638] Fix Unicode support for ostream redirects (#2982) * Crash when printing Unicode to redirected cout Add failing tests * Fix Unicode crashes redirected cout * pythonbuf::utf8_remainder check end iterator * Remove trailing whitespace and formatting iostream * Avoid buffer overflow if ostream redirect races This doesn't solve the actual race, but at least it now has a much lower probability of reading past the end of the buffer even when data races do occur. --- include/pybind11/iostream.h | 75 ++++++++++++++++++++++++++----- tests/test_iostream.py | 90 +++++++++++++++++++++++++++++++++++++ 2 files changed, 153 insertions(+), 12 deletions(-) diff --git a/include/pybind11/iostream.h b/include/pybind11/iostream.h index 9dee755431..7c1c718b02 100644 --- a/include/pybind11/iostream.h +++ b/include/pybind11/iostream.h @@ -16,6 +16,9 @@ #include #include #include +#include +#include +#include PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE) PYBIND11_NAMESPACE_BEGIN(detail) @@ -38,25 +41,73 @@ class pythonbuf : public std::streambuf { return sync() == 0 ? traits_type::not_eof(c) : traits_type::eof(); } + // Computes how many bytes at the end of the buffer are part of an + // incomplete sequence of UTF-8 bytes. + // Precondition: pbase() < pptr() + size_t utf8_remainder() const { + const auto rbase = std::reverse_iterator(pbase()); + const auto rpptr = std::reverse_iterator(pptr()); + auto is_ascii = [](char c) { + return (static_cast(c) & 0x80) == 0x00; + }; + auto is_leading = [](char c) { + return (static_cast(c) & 0xC0) == 0xC0; + }; + auto is_leading_2b = [](char c) { + return static_cast(c) <= 0xDF; + }; + auto is_leading_3b = [](char c) { + return static_cast(c) <= 0xEF; + }; + // If the last character is ASCII, there are no incomplete code points + if (is_ascii(*rpptr)) + return 0; + // Otherwise, work back from the end of the buffer and find the first + // UTF-8 leading byte + const auto rpend = rbase - rpptr >= 3 ? rpptr + 3 : rbase; + const auto leading = std::find_if(rpptr, rpend, is_leading); + if (leading == rbase) + return 0; + const auto dist = static_cast(leading - rpptr); + size_t remainder = 0; + + if (dist == 0) + remainder = 1; // 1-byte code point is impossible + else if (dist == 1) + remainder = is_leading_2b(*leading) ? 0 : dist + 1; + else if (dist == 2) + remainder = is_leading_3b(*leading) ? 0 : dist + 1; + // else if (dist >= 3), at least 4 bytes before encountering an UTF-8 + // leading byte, either no remainder or invalid UTF-8. + // Invalid UTF-8 will cause an exception later when converting + // to a Python string, so that's not handled here. + return remainder; + } + // This function must be non-virtual to be called in a destructor. If the // rare MSVC test failure shows up with this version, then this should be // simplified to a fully qualified call. int _sync() { - if (pbase() != pptr()) { - - { - gil_scoped_acquire tmp; - + if (pbase() != pptr()) { // If buffer is not empty + gil_scoped_acquire tmp; + // Placed inside gil_scoped_acquire as a mutex to avoid a race. + if (pbase() != pptr()) { // Check again under the lock // This subtraction cannot be negative, so dropping the sign. - str line(pbase(), static_cast(pptr() - pbase())); - - pywrite(line); - pyflush(); - - // Placed inside gil_scoped_aquire as a mutex to avoid a race + auto size = static_cast(pptr() - pbase()); + size_t remainder = utf8_remainder(); + + if (size > remainder) { + str line(pbase(), size - remainder); + pywrite(line); + pyflush(); + } + + // Copy the remainder at the end of the buffer to the beginning: + if (remainder > 0) + std::memmove(pbase(), pptr() - remainder, remainder); setp(pbase(), epptr()); + pbump(static_cast(remainder)); } - } return 0; } diff --git a/tests/test_iostream.py b/tests/test_iostream.py index 6d493beda3..e2b74d01cb 100644 --- a/tests/test_iostream.py +++ b/tests/test_iostream.py @@ -69,6 +69,96 @@ def test_captured_large_string(capsys): assert stderr == "" +def test_captured_utf8_2byte_offset0(capsys): + msg = "\u07FF" + msg = "" + msg * (1024 // len(msg) + 1) + + m.captured_output_default(msg) + stdout, stderr = capsys.readouterr() + assert stdout == msg + assert stderr == "" + + +def test_captured_utf8_2byte_offset1(capsys): + msg = "\u07FF" + msg = "1" + msg * (1024 // len(msg) + 1) + + m.captured_output_default(msg) + stdout, stderr = capsys.readouterr() + assert stdout == msg + assert stderr == "" + + +def test_captured_utf8_3byte_offset0(capsys): + msg = "\uFFFF" + msg = "" + msg * (1024 // len(msg) + 1) + + m.captured_output_default(msg) + stdout, stderr = capsys.readouterr() + assert stdout == msg + assert stderr == "" + + +def test_captured_utf8_3byte_offset1(capsys): + msg = "\uFFFF" + msg = "1" + msg * (1024 // len(msg) + 1) + + m.captured_output_default(msg) + stdout, stderr = capsys.readouterr() + assert stdout == msg + assert stderr == "" + + +def test_captured_utf8_3byte_offset2(capsys): + msg = "\uFFFF" + msg = "12" + msg * (1024 // len(msg) + 1) + + m.captured_output_default(msg) + stdout, stderr = capsys.readouterr() + assert stdout == msg + assert stderr == "" + + +def test_captured_utf8_4byte_offset0(capsys): + msg = "\U0010FFFF" + msg = "" + msg * (1024 // len(msg) + 1) + + m.captured_output_default(msg) + stdout, stderr = capsys.readouterr() + assert stdout == msg + assert stderr == "" + + +def test_captured_utf8_4byte_offset1(capsys): + msg = "\U0010FFFF" + msg = "1" + msg * (1024 // len(msg) + 1) + + m.captured_output_default(msg) + stdout, stderr = capsys.readouterr() + assert stdout == msg + assert stderr == "" + + +def test_captured_utf8_4byte_offset2(capsys): + msg = "\U0010FFFF" + msg = "12" + msg * (1024 // len(msg) + 1) + + m.captured_output_default(msg) + stdout, stderr = capsys.readouterr() + assert stdout == msg + assert stderr == "" + + +def test_captured_utf8_4byte_offset3(capsys): + msg = "\U0010FFFF" + msg = "123" + msg * (1024 // len(msg) + 1) + + m.captured_output_default(msg) + stdout, stderr = capsys.readouterr() + assert stdout == msg + assert stderr == "" + + def test_guard_capture(capsys): msg = "I've been redirected to Python, I hope!" m.guard_output(msg) From 417fd120ccd6a40294058a4d9cc3c6c6038fbb26 Mon Sep 17 00:00:00 2001 From: Akira Kawata Date: Thu, 6 May 2021 16:19:05 +0900 Subject: [PATCH 020/638] Fix: fix typo of WITHOUT_SOABI (#2992) --- tools/pybind11Config.cmake.in | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tools/pybind11Config.cmake.in b/tools/pybind11Config.cmake.in index 9921aeb355..0f136005b7 100644 --- a/tools/pybind11Config.cmake.in +++ b/tools/pybind11Config.cmake.in @@ -139,7 +139,7 @@ This module defines the following commands to assist with creating Python module pybind11_add_module( [STATIC|SHARED|MODULE] - [THIN_LTO] [OPT_SIZE] [NO_EXTRAS] [WITHOUT_SOBAI] + [THIN_LTO] [OPT_SIZE] [NO_EXTRAS] [WITHOUT_SOABI] ... ) From b6ec0e950c4a12d25cadf492f193ced94f681d6c Mon Sep 17 00:00:00 2001 From: Eric Cousineau Date: Thu, 6 May 2021 10:13:30 -0400 Subject: [PATCH 021/638] functions: Add doc on incorrect argument index (#2979) test_call_policies: Explicitly check free-functions and static methods --- docs/advanced/functions.rst | 3 +++ tests/test_call_policies.cpp | 8 +++++++- tests/test_call_policies.py | 30 ++++++++++++++++++++++++++++++ 3 files changed, 40 insertions(+), 1 deletion(-) diff --git a/docs/advanced/functions.rst b/docs/advanced/functions.rst index 6a93ab9da8..f6061ffbe6 100644 --- a/docs/advanced/functions.rst +++ b/docs/advanced/functions.rst @@ -182,6 +182,9 @@ relies on the ability to create a *weak reference* to the nurse object. When the nurse object is not a pybind11-registered type and does not support weak references, an exception will be thrown. +If you use an incorrect argument index, you will get a ``RuntimeError`` saying +``Could not activate keep_alive!``. You should review the indices you're using. + Consider the following example: here, the binding code for a list append operation ties the lifetime of the newly added element to the underlying container: diff --git a/tests/test_call_policies.cpp b/tests/test_call_policies.cpp index 26c83f81b0..7cb98d0d86 100644 --- a/tests/test_call_policies.cpp +++ b/tests/test_call_policies.cpp @@ -51,6 +51,7 @@ TEST_SUBMODULE(call_policies, m) { void addChild(Child *) { } Child *returnChild() { return new Child(); } Child *returnNullChild() { return nullptr; } + static Child *staticFunction(Parent*) { return new Child(); } }; py::class_(m, "Parent") .def(py::init<>()) @@ -60,7 +61,12 @@ TEST_SUBMODULE(call_policies, m) { .def("returnChild", &Parent::returnChild) .def("returnChildKeepAlive", &Parent::returnChild, py::keep_alive<1, 0>()) .def("returnNullChildKeepAliveChild", &Parent::returnNullChild, py::keep_alive<1, 0>()) - .def("returnNullChildKeepAliveParent", &Parent::returnNullChild, py::keep_alive<0, 1>()); + .def("returnNullChildKeepAliveParent", &Parent::returnNullChild, py::keep_alive<0, 1>()) + .def_static( + "staticFunction", &Parent::staticFunction, py::keep_alive<1, 0>()); + + m.def("free_function", [](Parent*, Child*) {}, py::keep_alive<1, 2>()); + m.def("invalid_arg_index", []{}, py::keep_alive<0, 1>()); #if !defined(PYPY_VERSION) // test_alive_gc diff --git a/tests/test_call_policies.py b/tests/test_call_policies.py index e0413d1451..af017e9d0c 100644 --- a/tests/test_call_policies.py +++ b/tests/test_call_policies.py @@ -46,6 +46,19 @@ def test_keep_alive_argument(capture): """ ) + p = m.Parent() + c = m.Child() + assert ConstructorStats.detail_reg_inst() == n_inst + 2 + m.free_function(p, c) + del c + assert ConstructorStats.detail_reg_inst() == n_inst + 2 + del p + assert ConstructorStats.detail_reg_inst() == n_inst + + with pytest.raises(RuntimeError) as excinfo: + m.invalid_arg_index() + assert str(excinfo.value) == "Could not activate keep_alive!" + def test_keep_alive_return_value(capture): n_inst = ConstructorStats.detail_reg_inst() @@ -85,6 +98,23 @@ def test_keep_alive_return_value(capture): """ ) + p = m.Parent() + assert ConstructorStats.detail_reg_inst() == n_inst + 1 + with capture: + m.Parent.staticFunction(p) + assert ConstructorStats.detail_reg_inst() == n_inst + 2 + assert capture == "Allocating child." + with capture: + del p + assert ConstructorStats.detail_reg_inst() == n_inst + assert ( + capture + == """ + Releasing parent. + Releasing child. + """ + ) + # https://foss.heptapod.net/pypy/pypy/-/issues/2447 @pytest.mark.xfail("env.PYPY", reason="_PyObject_GetDictPtr is unimplemented") From bc7cf6ef17031393d300acb30b8526345e6465a7 Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Thu, 13 May 2021 23:19:51 -0400 Subject: [PATCH 022/638] chore(deps): bump jwlawson/actions-setup-cmake from 1.8 to 1.9 (#3000) --- .github/workflows/ci.yml | 12 ++++++------ .github/workflows/configure.yml | 2 +- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 19e1030d82..8adb0743b7 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -75,7 +75,7 @@ jobs: run: brew install boost - name: Update CMake - uses: jwlawson/actions-setup-cmake@v1.8 + uses: jwlawson/actions-setup-cmake@v1.9 - name: Cache wheels if: runner.os == 'macOS' @@ -193,7 +193,7 @@ jobs: debug: ${{ matrix.python-debug }} - name: Update CMake - uses: jwlawson/actions-setup-cmake@v1.8 + uses: jwlawson/actions-setup-cmake@v1.9 - name: Valgrind cache if: matrix.valgrind @@ -445,7 +445,7 @@ jobs: run: python3 -m pip install --upgrade pip - name: Update CMake - uses: jwlawson/actions-setup-cmake@v1.8 + uses: jwlawson/actions-setup-cmake@v1.9 - name: Configure shell: bash @@ -727,7 +727,7 @@ jobs: architecture: x86 - name: Update CMake - uses: jwlawson/actions-setup-cmake@v1.8 + uses: jwlawson/actions-setup-cmake@v1.9 - name: Prepare MSVC uses: ilammy/msvc-dev-cmd@v1 @@ -773,7 +773,7 @@ jobs: python-version: ${{ matrix.python }} - name: Update CMake - uses: jwlawson/actions-setup-cmake@v1.8 + uses: jwlawson/actions-setup-cmake@v1.9 - name: Prepare MSVC uses: ilammy/msvc-dev-cmd@v1 @@ -827,7 +827,7 @@ jobs: python-version: ${{ matrix.python }} - name: Update CMake - uses: jwlawson/actions-setup-cmake@v1.8 + uses: jwlawson/actions-setup-cmake@v1.9 - name: Prepare env run: python -m pip install -r tests/requirements.txt --prefer-binary diff --git a/.github/workflows/configure.yml b/.github/workflows/configure.yml index 0f3117be99..abcff1dde5 100644 --- a/.github/workflows/configure.yml +++ b/.github/workflows/configure.yml @@ -55,7 +55,7 @@ jobs: # An action for adding a specific version of CMake: # https://github.com/jwlawson/actions-setup-cmake - name: Setup CMake ${{ matrix.cmake }} - uses: jwlawson/actions-setup-cmake@v1.8 + uses: jwlawson/actions-setup-cmake@v1.9 with: cmake-version: ${{ matrix.cmake }} From f61855b9d8821e2576960109a2a67379a6c2366f Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Thu, 13 May 2021 23:21:13 -0400 Subject: [PATCH 023/638] chore(deps): bump ilammy/msvc-dev-cmd from 1 to 1.8.0 (#3001) --- .github/workflows/ci.yml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 8adb0743b7..ac797181cb 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -151,7 +151,7 @@ jobs: # MSVC, but for now, this action works: - name: Prepare compiler environment for Windows 🐍 2.7 if: matrix.python == 2.7 && runner.os == 'Windows' - uses: ilammy/msvc-dev-cmd@v1 + uses: ilammy/msvc-dev-cmd@v1.8.0 with: arch: x64 @@ -730,7 +730,7 @@ jobs: uses: jwlawson/actions-setup-cmake@v1.9 - name: Prepare MSVC - uses: ilammy/msvc-dev-cmd@v1 + uses: ilammy/msvc-dev-cmd@v1.8.0 with: arch: x86 @@ -776,7 +776,7 @@ jobs: uses: jwlawson/actions-setup-cmake@v1.9 - name: Prepare MSVC - uses: ilammy/msvc-dev-cmd@v1 + uses: ilammy/msvc-dev-cmd@v1.8.0 with: toolset: 14.0 From 14023c9cda70215ccef7fd92ebb4c42197e98e1f Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Thu, 27 May 2021 06:10:28 +0000 Subject: [PATCH 024/638] chore(deps): bump ilammy/msvc-dev-cmd from 1.8.0 to 1.8.1 (#3021) --- .github/workflows/ci.yml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index ac797181cb..de7aeeb00b 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -151,7 +151,7 @@ jobs: # MSVC, but for now, this action works: - name: Prepare compiler environment for Windows 🐍 2.7 if: matrix.python == 2.7 && runner.os == 'Windows' - uses: ilammy/msvc-dev-cmd@v1.8.0 + uses: ilammy/msvc-dev-cmd@v1.8.1 with: arch: x64 @@ -730,7 +730,7 @@ jobs: uses: jwlawson/actions-setup-cmake@v1.9 - name: Prepare MSVC - uses: ilammy/msvc-dev-cmd@v1.8.0 + uses: ilammy/msvc-dev-cmd@v1.8.1 with: arch: x86 @@ -776,7 +776,7 @@ jobs: uses: jwlawson/actions-setup-cmake@v1.9 - name: Prepare MSVC - uses: ilammy/msvc-dev-cmd@v1.8.0 + uses: ilammy/msvc-dev-cmd@v1.8.1 with: toolset: 14.0 From 3ac690b88be00a44c1988afc209a395d87a57051 Mon Sep 17 00:00:00 2001 From: Yichen Date: Thu, 27 May 2021 23:00:18 +0800 Subject: [PATCH 025/638] Explicitly export exception types. (#2999) * Set visibility of exceptions to default. Co-authored-by: XZiar * add test * update docs * Skip failed test. --- docs/advanced/exceptions.rst | 4 ++++ include/pybind11/detail/common.h | 11 +++++++++-- include/pybind11/detail/internals.h | 2 ++ include/pybind11/pytypes.h | 9 ++++++++- tests/pybind11_cross_module_tests.cpp | 8 ++++++++ tests/test_exceptions.cpp | 2 ++ tests/test_exceptions.h | 12 ++++++++++++ tests/test_exceptions.py | 14 ++++++++++++++ 8 files changed, 59 insertions(+), 3 deletions(-) create mode 100644 tests/test_exceptions.h diff --git a/docs/advanced/exceptions.rst b/docs/advanced/exceptions.rst index 7a4d6cbc6e..f70c202fd5 100644 --- a/docs/advanced/exceptions.rst +++ b/docs/advanced/exceptions.rst @@ -164,6 +164,10 @@ section. may be explicitly (re-)thrown to delegate it to the other, previously-declared existing exception translators. + Note that ``libc++`` and ``libstdc++`` `behave differently `_ + with ``-fvisibility=hidden``. Therefore exceptions that are used across ABI boundaries need to be explicitly exported, as exercised in ``tests/test_exceptions.h``. + See also: "Problems with C++ exceptions" under `GCC Wiki `_. + .. _handling_python_exceptions_cpp: Handling exceptions from Python in C++ diff --git a/include/pybind11/detail/common.h b/include/pybind11/detail/common.h index 5560198a05..987f0badf9 100644 --- a/include/pybind11/detail/common.h +++ b/include/pybind11/detail/common.h @@ -723,16 +723,23 @@ using expand_side_effects = bool[]; PYBIND11_NAMESPACE_END(detail) +#if defined(_MSC_VER) +# pragma warning(push) +# pragma warning(disable: 4275) // warning C4275: An exported class was derived from a class that wasn't exported. Can be ignored when derived from a STL class. +#endif /// C++ bindings of builtin Python exceptions -class builtin_exception : public std::runtime_error { +class PYBIND11_EXPORT builtin_exception : public std::runtime_error { public: using std::runtime_error::runtime_error; /// Set the error using the Python C API virtual void set_error() const = 0; }; +#if defined(_MSC_VER) +# pragma warning(pop) +#endif #define PYBIND11_RUNTIME_EXCEPTION(name, type) \ - class name : public builtin_exception { public: \ + class PYBIND11_EXPORT name : public builtin_exception { public: \ using builtin_exception::builtin_exception; \ name() : name("") { } \ void set_error() const override { PyErr_SetString(type, what()); } \ diff --git a/include/pybind11/detail/internals.h b/include/pybind11/detail/internals.h index 75fcd3c208..5578c65160 100644 --- a/include/pybind11/detail/internals.h +++ b/include/pybind11/detail/internals.h @@ -276,6 +276,8 @@ PYBIND11_NOINLINE inline internals &get_internals() { // initial exception translator, below, so add another for our local exception classes. // // libstdc++ doesn't require this (types there are identified only by name) + // libc++ with CPython doesn't require this (types are explicitly exported) + // libc++ with PyPy still need it, awaiting further investigation #if !defined(__GLIBCXX__) (*internals_pp)->registered_exception_translators.push_front(&translate_local_exception); #endif diff --git a/include/pybind11/pytypes.h b/include/pybind11/pytypes.h index 6f0e6067ef..6a1d8356a5 100644 --- a/include/pybind11/pytypes.h +++ b/include/pybind11/pytypes.h @@ -319,11 +319,15 @@ PYBIND11_NAMESPACE_BEGIN(detail) inline std::string error_string(); PYBIND11_NAMESPACE_END(detail) +#if defined(_MSC_VER) +# pragma warning(push) +# pragma warning(disable: 4275 4251) // warning C4275: An exported class was derived from a class that wasn't exported. Can be ignored when derived from a STL class. +#endif /// Fetch and hold an error which was already set in Python. An instance of this is typically /// thrown to propagate python-side errors back through C++ which can either be caught manually or /// else falls back to the function dispatcher (which then raises the captured error back to /// python). -class error_already_set : public std::runtime_error { +class PYBIND11_EXPORT error_already_set : public std::runtime_error { public: /// Constructs a new exception from the current Python error indicator, if any. The current /// Python error indicator will be cleared. @@ -371,6 +375,9 @@ class error_already_set : public std::runtime_error { private: object m_type, m_value, m_trace; }; +#if defined(_MSC_VER) +# pragma warning(pop) +#endif /** \defgroup python_builtins _ Unless stated otherwise, the following C++ functions behave the same diff --git a/tests/pybind11_cross_module_tests.cpp b/tests/pybind11_cross_module_tests.cpp index f705e31061..944d085597 100644 --- a/tests/pybind11_cross_module_tests.cpp +++ b/tests/pybind11_cross_module_tests.cpp @@ -9,6 +9,7 @@ #include "pybind11_tests.h" #include "local_bindings.h" +#include "test_exceptions.h" #include #include @@ -30,6 +31,13 @@ PYBIND11_MODULE(pybind11_cross_module_tests, m) { m.def("throw_pybind_value_error", []() { throw py::value_error("pybind11 value error"); }); m.def("throw_pybind_type_error", []() { throw py::type_error("pybind11 type error"); }); m.def("throw_stop_iteration", []() { throw py::stop_iteration(); }); + py::register_exception_translator([](std::exception_ptr p) { + try { + if (p) std::rethrow_exception(p); + } catch (const shared_exception &e) { + PyErr_SetString(PyExc_KeyError, e.what()); + } + }); // test_local_bindings.py // Local to both: diff --git a/tests/test_exceptions.cpp b/tests/test_exceptions.cpp index e27c16dfef..4c5e10dbe0 100644 --- a/tests/test_exceptions.cpp +++ b/tests/test_exceptions.cpp @@ -7,6 +7,7 @@ BSD-style license that can be found in the LICENSE file. */ +#include "test_exceptions.h" #include "pybind11_tests.h" // A type that should be raised as an exception in Python @@ -228,4 +229,5 @@ TEST_SUBMODULE(exceptions, m) { // Test repr that cannot be displayed m.def("simple_bool_passthrough", [](bool x) {return x;}); + m.def("throw_should_be_translated_to_key_error", []() { throw shared_exception(); }); } diff --git a/tests/test_exceptions.h b/tests/test_exceptions.h new file mode 100644 index 0000000000..5d02d1b35b --- /dev/null +++ b/tests/test_exceptions.h @@ -0,0 +1,12 @@ +#pragma once +#include "pybind11_tests.h" +#include + +// shared exceptions for cross_module_tests + +class PYBIND11_EXPORT shared_exception : public pybind11::builtin_exception { +public: + using builtin_exception::builtin_exception; + explicit shared_exception() : shared_exception("") {} + void set_error() const override { PyErr_SetString(PyExc_RuntimeError, what()); } +}; diff --git a/tests/test_exceptions.py b/tests/test_exceptions.py index c6cb65299e..b9fc269381 100644 --- a/tests/test_exceptions.py +++ b/tests/test_exceptions.py @@ -3,6 +3,8 @@ import pytest +import env # noqa: F401 + from pybind11_tests import exceptions as m import pybind11_cross_module_tests as cm @@ -44,6 +46,18 @@ def test_cross_module_exceptions(): cm.throw_stop_iteration() +# TODO: FIXME +@pytest.mark.xfail( + "env.PYPY and env.MACOS", + raises=RuntimeError, + reason="Expected failure with PyPy and libc++ (Issue #2847 & PR #2999)", +) +def test_cross_module_exception_translator(): + with pytest.raises(KeyError): + # translator registered in cross_module_tests + m.throw_should_be_translated_to_key_error() + + def test_python_call_in_catch(): d = {} assert m.python_call_in_destructor(d) is True From e25b1505dbca042289c4076141092cff49b0b96b Mon Sep 17 00:00:00 2001 From: "Ralf W. Grosse-Kunstleve" Date: Thu, 27 May 2021 12:51:46 -0700 Subject: [PATCH 026/638] Adjusting valgrind suppression for pypocketfft to resolve systematic failures that started to appear on 2020-05-27. (#3022) --- tests/valgrind-numpy-scipy.supp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/valgrind-numpy-scipy.supp b/tests/valgrind-numpy-scipy.supp index fea9bcb4a6..001b6c717a 100644 --- a/tests/valgrind-numpy-scipy.supp +++ b/tests/valgrind-numpy-scipy.supp @@ -111,7 +111,7 @@ fun:_Znwm fun:PyInit_pypocketfft fun:_PyImport_LoadDynamicModuleWithSpec - fun:_imp_create_dynamic_impl.constprop.* + fun:_imp_create_dynamic_impl* fun:_imp_create_dynamic fun:cfunction_vectorcall_FASTCALL fun:PyVectorcall_Call From 4c7697dbe973ed01ae6fbec37d186ebd05982e1f Mon Sep 17 00:00:00 2001 From: "Jack S. Hale" Date: Tue, 8 Jun 2021 20:56:45 +0200 Subject: [PATCH 027/638] Add const T to docstring generation. (#3020) * Add const T to docstring generation. * Change order. * See if existing test triggers for a const type. * Add tests. * Fix test. * Remove experiment. * Reformat. * More tests, checks run. * Adding `test_fmt_desc_` prefix to new test functions. * Using pytest.mark.parametrize to 1. condense test; 2. exercise all functions even if one fails; 3. be less platform-specific (e.g. C++ float is not necessarily float32). Co-authored-by: Ralf W. Grosse-Kunstleve --- include/pybind11/numpy.h | 9 +++++++-- tests/test_numpy_array.cpp | 6 ++++++ tests/test_numpy_array.py | 13 +++++++++++++ 3 files changed, 26 insertions(+), 2 deletions(-) diff --git a/include/pybind11/numpy.h b/include/pybind11/numpy.h index dd7f43d16a..ec4c53b430 100644 --- a/include/pybind11/numpy.h +++ b/include/pybind11/numpy.h @@ -1029,7 +1029,10 @@ struct npy_format_descriptor_name::value>> { template struct npy_format_descriptor_name::value>> { - static constexpr auto name = _::value || std::is_same::value>( + static constexpr auto name = _::value + || std::is_same::value + || std::is_same::value + || std::is_same::value>( _("numpy.float") + _(), _("numpy.longdouble") ); }; @@ -1037,7 +1040,9 @@ struct npy_format_descriptor_name::valu template struct npy_format_descriptor_name::value>> { static constexpr auto name = _::value - || std::is_same::value>( + || std::is_same::value + || std::is_same::value + || std::is_same::value>( _("numpy.complex") + _(), _("numpy.longcomplex") ); }; diff --git a/tests/test_numpy_array.cpp b/tests/test_numpy_array.cpp index dca7145f9d..204ea8367e 100644 --- a/tests/test_numpy_array.cpp +++ b/tests/test_numpy_array.cpp @@ -437,4 +437,10 @@ TEST_SUBMODULE(numpy_array, sm) { sm.def("accept_double_f_style_forcecast_noconvert", [](py::array_t) {}, "a"_a.noconvert()); + + // Check that types returns correct npy format descriptor + sm.def("test_fmt_desc_float", [](py::array_t) {}); + sm.def("test_fmt_desc_double", [](py::array_t) {}); + sm.def("test_fmt_desc_const_float", [](py::array_t) {}); + sm.def("test_fmt_desc_const_double", [](py::array_t) {}); } diff --git a/tests/test_numpy_array.py b/tests/test_numpy_array.py index 02f3ecfc0e..548c84bab1 100644 --- a/tests/test_numpy_array.py +++ b/tests/test_numpy_array.py @@ -482,6 +482,19 @@ def test_index_using_ellipsis(): assert a.shape == (6,) +@pytest.mark.parametrize( + "test_func", + [ + m.test_fmt_desc_float, + m.test_fmt_desc_double, + m.test_fmt_desc_const_float, + m.test_fmt_desc_const_double, + ], +) +def test_format_descriptors_for_floating_point_types(test_func): + assert "numpy.ndarray[numpy.float" in test_func.__doc__ + + @pytest.mark.parametrize("forcecast", [False, True]) @pytest.mark.parametrize("contiguity", [None, "C", "F"]) @pytest.mark.parametrize("noconvert", [False, True]) From cd4b49a2c8c659d3933cf902bb10e34931dbd1e7 Mon Sep 17 00:00:00 2001 From: Aaron Gokaslan Date: Thu, 17 Jun 2021 16:20:17 -0400 Subject: [PATCH 028/638] Update py::kwargs examples to pass by reference (#3038) --- docs/advanced/functions.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/advanced/functions.rst b/docs/advanced/functions.rst index f6061ffbe6..d880008b0f 100644 --- a/docs/advanced/functions.rst +++ b/docs/advanced/functions.rst @@ -254,7 +254,7 @@ For instance, the following statement iterates over a Python ``dict``: .. code-block:: cpp - void print_dict(py::dict dict) { + void print_dict(const py::dict& dict) { /* Easily interact with Python types */ for (auto item : dict) std::cout << "key=" << std::string(py::str(item.first)) << ", " @@ -292,7 +292,7 @@ Such functions can also be created using pybind11: .. code-block:: cpp - void generic(py::args args, py::kwargs kwargs) { + void generic(py::args args, const py::kwargs& kwargs) { /// .. do something with args if (kwargs) /// .. do something with kwargs From b4b67f026bc2aa1a1ae17e9858a9e61210c77c31 Mon Sep 17 00:00:00 2001 From: Aaron Gokaslan Date: Thu, 17 Jun 2021 16:39:59 -0400 Subject: [PATCH 029/638] Fix typos (#3044) --- docs/advanced/pycpp/numpy.rst | 2 +- docs/compiling.rst | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/docs/advanced/pycpp/numpy.rst b/docs/advanced/pycpp/numpy.rst index 0a81aa8fd5..53ec8c1a3c 100644 --- a/docs/advanced/pycpp/numpy.rst +++ b/docs/advanced/pycpp/numpy.rst @@ -390,7 +390,7 @@ operation on the C++ side: py::array b = a[py::make_tuple(0, py::ellipsis(), 0)]; .. versionchanged:: 2.6 - ``py::ellipsis()`` is now also avaliable in Python 2. + ``py::ellipsis()`` is now also available in Python 2. Memory view =========== diff --git a/docs/compiling.rst b/docs/compiling.rst index 3a8a270d58..27f30e5a06 100644 --- a/docs/compiling.rst +++ b/docs/compiling.rst @@ -149,7 +149,7 @@ Your ``pyproject.toml`` file will likely look something like this: and ``pyproject.toml`` are not even contained in the wheel, so this high Pip requirement is only for source builds, and will not affect users of your binary wheels. If you are building SDists and wheels, then - `pypa-build`_ is the recommended offical tool. + `pypa-build`_ is the recommended official tool. .. _PEP 517: https://www.python.org/dev/peps/pep-0517/ .. _cibuildwheel: https://cibuildwheel.readthedocs.io @@ -411,7 +411,7 @@ existing targets instead: .. code-block:: cmake - cmake_minumum_required(VERSION 3.15...3.19) + cmake_minimum_required(VERSION 3.15...3.19) project(example LANGUAGES CXX) find_package(Python COMPONENTS Interpreter Development REQUIRED) @@ -516,7 +516,7 @@ Instead of setting properties, you can set ``CMAKE_*`` variables to initialize t compiler flags are provided to ensure high quality code generation. In contrast to the ``pybind11_add_module()`` command, the CMake interface provides a *composable* set of targets to ensure that you retain flexibility. - It can be expecially important to provide or set these properties; the + It can be especially important to provide or set these properties; the :ref:`FAQ ` contains an explanation on why these are needed. .. versionadded:: 2.6 From 19d99a87fedc7272ec48bd90e91a00eff8e57b68 Mon Sep 17 00:00:00 2001 From: "Ralf W. Grosse-Kunstleve" Date: Fri, 18 Jun 2021 01:35:36 -0700 Subject: [PATCH 030/638] Working around Centos 8 failure. (#3030) * Experiment using -DCMAKE_BUILD_TYPE=Debug for Centos 8. * Moving comment because the current placement seems to mess up the cmake command. * Using `echo > VAR_XXX` and `$(cat VAR_XXX)` trick to emulate using variables (https://github.com/actions/starter-workflows/issues/68#issuecomment-524937002). --- .github/workflows/ci.yml | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index de7aeeb00b..2ed1da5c5e 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -592,10 +592,21 @@ jobs: - name: Install dependencies run: python3 -m pip install cmake -r tests/requirements.txt --prefer-binary + - name: VAR_BUILD_TYPE 7 + if: matrix.centos == 7 + run: echo Release > VAR_BUILD_TYPE + + # Using Debug to avoid segfault that appeared around 2021-06-04, + # apparently when the gcc version changed from 8.3 to 8.4. + - name: VAR_BUILD_TYPE 8 + if: matrix.centos == 8 + run: echo Debug > VAR_BUILD_TYPE + - name: Configure shell: bash run: > cmake -S . -B build + -DCMAKE_BUILD_TYPE=$(cat VAR_BUILD_TYPE) -DPYBIND11_WERROR=ON -DDOWNLOAD_CATCH=ON -DDOWNLOAD_EIGEN=ON From 79178e713d8f3ac3ae5b77017eb57f1bc67f336f Mon Sep 17 00:00:00 2001 From: jbarlow83 Date: Fri, 18 Jun 2021 07:17:34 -0700 Subject: [PATCH 031/638] fix(setup_helpers): try import multiprocessing.synchronize too (#3043) --- pybind11/setup_helpers.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/pybind11/setup_helpers.py b/pybind11/setup_helpers.py index 84a4584c5d..1050f61253 100644 --- a/pybind11/setup_helpers.py +++ b/pybind11/setup_helpers.py @@ -410,7 +410,9 @@ def _single_compile(obj): compiler._compile(obj, src, ext, cc_args, extra_postargs, pp_opts) try: - import multiprocessing + # Importing .synchronize checks for platforms that have some multiprocessing + # capabilities but lack semaphores, such as AWS Lambda and Android Termux. + import multiprocessing.synchronize from multiprocessing.pool import ThreadPool except ImportError: threads = 1 From af6218ff78d480c5e93237174ee6f02ec36853cc Mon Sep 17 00:00:00 2001 From: Aaron Gokaslan Date: Sat, 19 Jun 2021 13:53:27 -0400 Subject: [PATCH 032/638] fix(clang-tidy): Apply performance fixes from clang-tidy (#3046) * Apply performance fixes from clang-tidy * 2nd Round of Perf Optimizations * 3rd round of fixes & handle false-positive * Apply missing fix and clang-format * Apply reviewer comment --- .clang-tidy | 4 +++ include/pybind11/buffer_info.h | 6 ++-- include/pybind11/cast.h | 11 +++--- include/pybind11/eval.h | 8 +++-- include/pybind11/iostream.h | 39 ++++++++++----------- include/pybind11/numpy.h | 4 +-- include/pybind11/pybind11.h | 62 +++++++++++++++++++--------------- include/pybind11/pytypes.h | 4 +-- include/pybind11/stl.h | 11 +++--- include/pybind11/stl_bind.h | 49 ++++++++++++++------------- 10 files changed, 106 insertions(+), 92 deletions(-) diff --git a/.clang-tidy b/.clang-tidy index e29d929897..3079b3b3c0 100644 --- a/.clang-tidy +++ b/.clang-tidy @@ -10,4 +10,8 @@ modernize-use-auto, modernize-use-emplace, ' +CheckOptions: +- key: performance-unnecessary-value-param.AllowedTypes + value: 'exception_ptr$;' + HeaderFilterRegex: 'pybind11/.*h' diff --git a/include/pybind11/buffer_info.h b/include/pybind11/buffer_info.h index d803004a10..47dc39d4ea 100644 --- a/include/pybind11/buffer_info.h +++ b/include/pybind11/buffer_info.h @@ -91,11 +91,9 @@ struct buffer_info { buffer_info(const buffer_info &) = delete; buffer_info& operator=(const buffer_info &) = delete; - buffer_info(buffer_info &&other) { - (*this) = std::move(other); - } + buffer_info(buffer_info &&other) noexcept { (*this) = std::move(other); } - buffer_info& operator=(buffer_info &&rhs) { + buffer_info &operator=(buffer_info &&rhs) noexcept { ptr = rhs.ptr; itemsize = rhs.itemsize; size = rhs.size; diff --git a/include/pybind11/cast.h b/include/pybind11/cast.h index f68a35bfa4..1ef1a9ce9b 100644 --- a/include/pybind11/cast.h +++ b/include/pybind11/cast.h @@ -1064,7 +1064,9 @@ struct kw_only {}; struct pos_only {}; template -arg_v arg::operator=(T &&value) const { return {std::move(*this), std::forward(value)}; } +arg_v arg::operator=(T &&value) const { + return {*this, std::forward(value)}; +} /// Alias for backward compatibility -- to be removed in version 2.0 template using arg_t = arg_v; @@ -1286,7 +1288,7 @@ class unpacking_collector { "may be passed via py::arg() to a python function call. " "(compile in debug mode for details)"); } - [[noreturn]] static void nameless_argument_error(std::string type) { + [[noreturn]] static void nameless_argument_error(const std::string &type) { throw type_error("Got kwargs without a name of type '" + type + "'; only named " "arguments may be passed via py::arg() to a python function call. "); } @@ -1295,7 +1297,7 @@ class unpacking_collector { "(compile in debug mode for details)"); } - [[noreturn]] static void multiple_values_error(std::string name) { + [[noreturn]] static void multiple_values_error(const std::string &name) { throw type_error("Got multiple values for keyword argument '" + name + "'"); } @@ -1304,7 +1306,8 @@ class unpacking_collector { "(compile in debug mode for details)"); } - [[noreturn]] static void argument_cast_error(std::string name, std::string type) { + [[noreturn]] static void argument_cast_error(const std::string &name, + const std::string &type) { throw cast_error("Unable to convert call argument '" + name + "' of type '" + type + "' to Python object"); } diff --git a/include/pybind11/eval.h b/include/pybind11/eval.h index fa6b8af479..bf06504a33 100644 --- a/include/pybind11/eval.h +++ b/include/pybind11/eval.h @@ -11,6 +11,8 @@ #pragma once +#include + #include "pybind11.h" PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE) @@ -43,7 +45,7 @@ enum eval_mode { }; template -object eval(str expr, object global = globals(), object local = object()) { +object eval(const str &expr, object global = globals(), object local = object()) { if (!local) local = global; @@ -75,8 +77,8 @@ object eval(const char (&s)[N], object global = globals(), object local = object return eval(expr, global, local); } -inline void exec(str expr, object global = globals(), object local = object()) { - eval(expr, global, local); +inline void exec(const str &expr, object global = globals(), object local = object()) { + eval(expr, std::move(global), std::move(local)); } template diff --git a/include/pybind11/iostream.h b/include/pybind11/iostream.h index 7c1c718b02..6a793eadca 100644 --- a/include/pybind11/iostream.h +++ b/include/pybind11/iostream.h @@ -11,14 +11,15 @@ #include "pybind11.h" -#include -#include -#include -#include -#include +#include #include +#include #include -#include +#include +#include +#include +#include +#include PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE) PYBIND11_NAMESPACE_BEGIN(detail) @@ -117,11 +118,8 @@ class pythonbuf : public std::streambuf { } public: - - pythonbuf(object pyostream, size_t buffer_size = 1024) - : buf_size(buffer_size), - d_buffer(new char[buf_size]), - pywrite(pyostream.attr("write")), + pythonbuf(const object &pyostream, size_t buffer_size = 1024) + : buf_size(buffer_size), d_buffer(new char[buf_size]), pywrite(pyostream.attr("write")), pyflush(pyostream.attr("flush")) { setp(d_buffer.get(), d_buffer.get() + buf_size - 1); } @@ -168,9 +166,8 @@ class scoped_ostream_redirect { detail::pythonbuf buffer; public: - scoped_ostream_redirect( - std::ostream &costream = std::cout, - object pyostream = module_::import("sys").attr("stdout")) + scoped_ostream_redirect(std::ostream &costream = std::cout, + const object &pyostream = module_::import("sys").attr("stdout")) : costream(costream), buffer(pyostream) { old = costream.rdbuf(&buffer); } @@ -199,10 +196,9 @@ class scoped_ostream_redirect { \endrst */ class scoped_estream_redirect : public scoped_ostream_redirect { public: - scoped_estream_redirect( - std::ostream &costream = std::cerr, - object pyostream = module_::import("sys").attr("stderr")) - : scoped_ostream_redirect(costream,pyostream) {} + scoped_estream_redirect(std::ostream &costream = std::cerr, + const object &pyostream = module_::import("sys").attr("stderr")) + : scoped_ostream_redirect(costream, pyostream) {} }; @@ -261,9 +257,10 @@ PYBIND11_NAMESPACE_END(detail) m.noisy_function_with_error_printing() \endrst */ -inline class_ add_ostream_redirect(module_ m, std::string name = "ostream_redirect") { - return class_(m, name.c_str(), module_local()) - .def(init(), arg("stdout")=true, arg("stderr")=true) +inline class_ +add_ostream_redirect(module_ m, const std::string &name = "ostream_redirect") { + return class_(std::move(m), name.c_str(), module_local()) + .def(init(), arg("stdout") = true, arg("stderr") = true) .def("__enter__", &detail::OstreamRedirect::enter) .def("__exit__", [](detail::OstreamRedirect &self_, args) { self_.exit(); }); } diff --git a/include/pybind11/numpy.h b/include/pybind11/numpy.h index ec4c53b430..9287842460 100644 --- a/include/pybind11/numpy.h +++ b/include/pybind11/numpy.h @@ -164,10 +164,10 @@ struct npy_api { NPY_ULONG_, NPY_ULONGLONG_, NPY_UINT_), }; - typedef struct { + struct PyArray_Dims { Py_intptr_t *ptr; int len; - } PyArray_Dims; + }; static npy_api& get() { static npy_api api = lookup(); diff --git a/include/pybind11/pybind11.h b/include/pybind11/pybind11.h index 754c875cda..2ff88dbf5b 100644 --- a/include/pybind11/pybind11.h +++ b/include/pybind11/pybind11.h @@ -1412,15 +1412,15 @@ class class_ : public detail::generic_type { template class_ &def_readwrite_static(const char *name, D *pm, const Extra& ...extra) { - cpp_function fget([pm](object) -> const D &{ return *pm; }, scope(*this)), - fset([pm](object, const D &value) { *pm = value; }, scope(*this)); + cpp_function fget([pm](const object &) -> const D & { return *pm; }, scope(*this)), + fset([pm](const object &, const D &value) { *pm = value; }, scope(*this)); def_property_static(name, fget, fset, return_value_policy::reference, extra...); return *this; } template class_ &def_readonly_static(const char *name, const D *pm, const Extra& ...extra) { - cpp_function fget([pm](object) -> const D &{ return *pm; }, scope(*this)); + cpp_function fget([pm](const object &) -> const D & { return *pm; }, scope(*this)); def_property_readonly_static(name, fget, return_value_policy::reference, extra...); return *this; } @@ -1671,30 +1671,36 @@ struct enum_base { }, name("__members__")), none(), none(), "" ); - #define PYBIND11_ENUM_OP_STRICT(op, expr, strict_behavior) \ - m_base.attr(op) = cpp_function( \ - [](object a, object b) { \ - if (!type::handle_of(a).is(type::handle_of(b))) \ - strict_behavior; \ - return expr; \ - }, \ - name(op), is_method(m_base), arg("other")) - - #define PYBIND11_ENUM_OP_CONV(op, expr) \ - m_base.attr(op) = cpp_function( \ - [](object a_, object b_) { \ - int_ a(a_), b(b_); \ - return expr; \ - }, \ - name(op), is_method(m_base), arg("other")) - - #define PYBIND11_ENUM_OP_CONV_LHS(op, expr) \ - m_base.attr(op) = cpp_function( \ - [](object a_, object b) { \ - int_ a(a_); \ - return expr; \ - }, \ - name(op), is_method(m_base), arg("other")) +#define PYBIND11_ENUM_OP_STRICT(op, expr, strict_behavior) \ + m_base.attr(op) = cpp_function( \ + [](const object &a, const object &b) { \ + if (!type::handle_of(a).is(type::handle_of(b))) \ + strict_behavior; \ + return expr; \ + }, \ + name(op), \ + is_method(m_base), \ + arg("other")) + +#define PYBIND11_ENUM_OP_CONV(op, expr) \ + m_base.attr(op) = cpp_function( \ + [](const object &a_, const object &b_) { \ + int_ a(a_), b(b_); \ + return expr; \ + }, \ + name(op), \ + is_method(m_base), \ + arg("other")) + +#define PYBIND11_ENUM_OP_CONV_LHS(op, expr) \ + m_base.attr(op) = cpp_function( \ + [](const object &a_, const object &b) { \ + int_ a(a_); \ + return expr; \ + }, \ + name(op), \ + is_method(m_base), \ + arg("other")) if (is_convertible) { PYBIND11_ENUM_OP_CONV_LHS("__eq__", !b.is_none() && a.equal(b)); @@ -2054,7 +2060,7 @@ exception ®ister_exception(handle scope, } PYBIND11_NAMESPACE_BEGIN(detail) -PYBIND11_NOINLINE inline void print(tuple args, dict kwargs) { +PYBIND11_NOINLINE inline void print(const tuple &args, const dict &kwargs) { auto strings = tuple(args.size()); for (size_t i = 0; i < args.size(); ++i) { strings[i] = str(args[i]); diff --git a/include/pybind11/pytypes.h b/include/pybind11/pytypes.h index 6a1d8356a5..d1d92af6ad 100644 --- a/include/pybind11/pytypes.h +++ b/include/pybind11/pytypes.h @@ -503,7 +503,7 @@ class accessor : public object_api> { public: accessor(handle obj, key_type key) : obj(obj), key(std::move(key)) { } accessor(const accessor &) = default; - accessor(accessor &&) = default; + accessor(accessor &&) noexcept = default; // accessor overload required to override default assignment operator (templates are not allowed // to replace default compiler-generated assignments). @@ -1508,7 +1508,7 @@ class memoryview : public object { detail::any_container shape, detail::any_container strides) { return memoryview::from_buffer( - const_cast(ptr), itemsize, format, shape, strides, true); + const_cast(ptr), itemsize, format, std::move(shape), std::move(strides), true); } template diff --git a/include/pybind11/stl.h b/include/pybind11/stl.h index 18fbafb1e2..61d3fba61e 100644 --- a/include/pybind11/stl.h +++ b/include/pybind11/stl.h @@ -159,10 +159,13 @@ template struct list_caster { } private: - template ().reserve(0)), void>::value, int> = 0> - void reserve_maybe(sequence s, Type *) { value.reserve(s.size()); } - void reserve_maybe(sequence, void *) { } + template < + typename T = Type, + enable_if_t().reserve(0)), void>::value, int> = 0> + void reserve_maybe(const sequence &s, Type *) { + value.reserve(s.size()); + } + void reserve_maybe(const sequence &, void *) {} public: template diff --git a/include/pybind11/stl_bind.h b/include/pybind11/stl_bind.h index 83195ee496..b78bd27ced 100644 --- a/include/pybind11/stl_bind.h +++ b/include/pybind11/stl_bind.h @@ -128,11 +128,11 @@ void vector_modifiers(enable_if_t(new Vector()); v->reserve(len_hint(it)); for (handle h : it) - v->push_back(h.cast()); + v->push_back(h.cast()); return v.release(); })); @@ -151,27 +151,28 @@ void vector_modifiers(enable_if_t()); - } - } catch (const cast_error &) { - v.erase(v.begin() + static_cast(old_size), v.end()); - try { - v.shrink_to_fit(); - } catch (const std::exception &) { - // Do nothing - } - throw; - } - }, - arg("L"), - "Extend the list by appending all the items in the given list" - ); + cl.def( + "extend", + [](Vector &v, const iterable &it) { + const size_t old_size = v.size(); + v.reserve(old_size + len_hint(it)); + try { + for (handle h : it) { + v.push_back(h.cast()); + } + } catch (const cast_error &) { + v.erase(v.begin() + static_cast(old_size), + v.end()); + try { + v.shrink_to_fit(); + } catch (const std::exception &) { + // Do nothing + } + throw; + } + }, + arg("L"), + "Extend the list by appending all the items in the given list"); cl.def("insert", [](Vector &v, DiffType i, const T &x) { @@ -400,7 +401,7 @@ void vector_buffer_impl(Class_& cl, std::true_type) { return buffer_info(v.data(), static_cast(sizeof(T)), format_descriptor::format(), 1, {v.size()}, {sizeof(T)}); }); - cl.def(init([](buffer buf) { + cl.def(init([](const buffer &buf) { auto info = buf.request(); if (info.ndim != 1 || info.strides[0] % static_cast(sizeof(T))) throw type_error("Only valid 1D buffers can be copied to a vector"); From e0b5cbd4c6503a96747ca184621e242818fa2ef4 Mon Sep 17 00:00:00 2001 From: Aaron Gokaslan Date: Sat, 19 Jun 2021 18:38:56 -0400 Subject: [PATCH 033/638] chore(clang-tidy): add more modernize clang-tidy checks (#3049) * Add more modernize clang-tidy checks * Enable a few more --- .clang-tidy | 15 +++++++++++---- tests/test_factory_constructors.cpp | 4 ++-- 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/.clang-tidy b/.clang-tidy index 3079b3b3c0..9098e77ff2 100644 --- a/.clang-tidy +++ b/.clang-tidy @@ -2,12 +2,17 @@ FormatStyle: file Checks: ' llvm-namespace-comment, -modernize-use-override, -readability-container-size-empty, -modernize-use-using, -modernize-use-equals-default, +modernize-avoid-bind, +modernize-replace-auto-ptr, +modernize-replace-disallow-copy-and-assign-macro, +modernize-shrink-to-fit, modernize-use-auto, +modernize-use-equals-default, +modernize-use-equals-delete, modernize-use-emplace, +modernize-use-override, +modernize-use-using, +readability-container-size-empty, ' CheckOptions: @@ -15,3 +20,5 @@ CheckOptions: value: 'exception_ptr$;' HeaderFilterRegex: 'pybind11/.*h' + +WarningsAsErrors: '*' diff --git a/tests/test_factory_constructors.cpp b/tests/test_factory_constructors.cpp index 7ff7e7b52c..6832ac4943 100644 --- a/tests/test_factory_constructors.cpp +++ b/tests/test_factory_constructors.cpp @@ -20,12 +20,12 @@ class TestFactory1 { TestFactory1() : value("(empty)") { print_default_created(this); } TestFactory1(int v) : value(std::to_string(v)) { print_created(this, value); } TestFactory1(std::string v) : value(std::move(v)) { print_created(this, value); } +public: + std::string value; TestFactory1(TestFactory1 &&) = delete; TestFactory1(const TestFactory1 &) = delete; TestFactory1 &operator=(TestFactory1 &&) = delete; TestFactory1 &operator=(const TestFactory1 &) = delete; -public: - std::string value; ~TestFactory1() { print_destroyed(this); } }; // Non-public construction, but moveable: From 9b3b35770650832a70c4829b1dcbcd2ab62ae09e Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Sat, 19 Jun 2021 18:39:54 -0400 Subject: [PATCH 034/638] chore(deps): bump ilammy/msvc-dev-cmd from 1.8.1 to 1.9.0 (#3027) --- .github/workflows/ci.yml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 2ed1da5c5e..e00745df7e 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -151,7 +151,7 @@ jobs: # MSVC, but for now, this action works: - name: Prepare compiler environment for Windows 🐍 2.7 if: matrix.python == 2.7 && runner.os == 'Windows' - uses: ilammy/msvc-dev-cmd@v1.8.1 + uses: ilammy/msvc-dev-cmd@v1.9.0 with: arch: x64 @@ -741,7 +741,7 @@ jobs: uses: jwlawson/actions-setup-cmake@v1.9 - name: Prepare MSVC - uses: ilammy/msvc-dev-cmd@v1.8.1 + uses: ilammy/msvc-dev-cmd@v1.9.0 with: arch: x86 @@ -787,7 +787,7 @@ jobs: uses: jwlawson/actions-setup-cmake@v1.9 - name: Prepare MSVC - uses: ilammy/msvc-dev-cmd@v1.8.1 + uses: ilammy/msvc-dev-cmd@v1.9.0 with: toolset: 14.0 From d00fc62931bde0f2899940f4d487c979a0110422 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20Sch=C3=BCtz?= Date: Sun, 20 Jun 2021 00:45:53 +0200 Subject: [PATCH 035/638] use CMAKE_INSTALL_FULL_INCLUDEDIR (#3005) --- tools/pybind11Config.cmake.in | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tools/pybind11Config.cmake.in b/tools/pybind11Config.cmake.in index 0f136005b7..6fa03a0f24 100644 --- a/tools/pybind11Config.cmake.in +++ b/tools/pybind11Config.cmake.in @@ -201,7 +201,7 @@ Using ``find_package`` with version info is not recommended except for release v @PACKAGE_INIT@ # Location of pybind11/pybind11.h -set(pybind11_INCLUDE_DIR "${PACKAGE_PREFIX_DIR}/@CMAKE_INSTALL_INCLUDEDIR@") +set(pybind11_INCLUDE_DIR "@CMAKE_INSTALL_FULL_INCLUDEDIR@") set(pybind11_LIBRARY "") set(pybind11_DEFINITIONS USING_pybind11) From 3b30b0a51e48a3273918f30dfb222db604cea5e9 Mon Sep 17 00:00:00 2001 From: Aaron Gokaslan Date: Mon, 21 Jun 2021 10:37:48 -0400 Subject: [PATCH 036/638] fix(clang-tidy): clang-tidy readability and misc fixes, like adding const (#3052) * Enable and apply clang-tidy readability and misc fixes. * Revert deprecated tester * add space to tests/test_constants_and_functions.cpp --- .clang-tidy | 13 +++++++++++++ include/pybind11/detail/common.h | 2 +- include/pybind11/detail/descr.h | 4 ++-- include/pybind11/detail/type_caster_base.h | 6 +++--- include/pybind11/numpy.h | 4 ++-- include/pybind11/pybind11.h | 3 ++- tests/local_bindings.h | 2 +- tests/test_constants_and_functions.cpp | 10 +++++++--- tests/test_factory_constructors.cpp | 4 ++-- tests/test_iostream.cpp | 6 +++--- tests/test_methods_and_attributes.cpp | 12 +++++------- tests/test_modules.cpp | 3 ++- tests/test_multiple_inheritance.cpp | 8 ++++---- tests/test_numpy_vectorize.cpp | 2 +- tests/test_smart_ptr.cpp | 2 +- tests/test_stl_binders.cpp | 4 +++- 16 files changed, 52 insertions(+), 33 deletions(-) diff --git a/.clang-tidy b/.clang-tidy index 9098e77ff2..60531abe05 100644 --- a/.clang-tidy +++ b/.clang-tidy @@ -2,17 +2,30 @@ FormatStyle: file Checks: ' llvm-namespace-comment, +misc-misplaced-const, +misc-static-assert, +misc-uniqueptr-reset-release, modernize-avoid-bind, modernize-replace-auto-ptr, modernize-replace-disallow-copy-and-assign-macro, modernize-shrink-to-fit, modernize-use-auto, +modernize-use-bool-literals, modernize-use-equals-default, modernize-use-equals-delete, +modernize-use-default-member-init, +modernize-use-noexcept, modernize-use-emplace, modernize-use-override, modernize-use-using, readability-container-size-empty, +readability-make-member-function-const, +readability-redundant-function-ptr-dereference, +readability-redundant-smartptr-get, +readability-redundant-string-cstr, +readability-simplify-subscript-expr, +readability-string-compare, +readability-uniqueptr-delete-release, ' CheckOptions: diff --git a/include/pybind11/detail/common.h b/include/pybind11/detail/common.h index 987f0badf9..1f7446103e 100644 --- a/include/pybind11/detail/common.h +++ b/include/pybind11/detail/common.h @@ -480,7 +480,7 @@ struct instance { void allocate_layout(); /// Destroys/deallocates all of the above - void deallocate_layout(); + void deallocate_layout() const; /// Returns the value_and_holder wrapper for the given type (or the first, if `find_type` /// omitted). Returns a default-constructed (with `.inst = nullptr`) object on failure if diff --git a/include/pybind11/detail/descr.h b/include/pybind11/detail/descr.h index 92720cd562..7cb8350e7b 100644 --- a/include/pybind11/detail/descr.h +++ b/include/pybind11/detail/descr.h @@ -23,9 +23,9 @@ PYBIND11_NAMESPACE_BEGIN(detail) /* Concatenate type signatures at compile time */ template struct descr { - char text[N + 1]; + char text[N + 1]{'\0'}; - constexpr descr() : text{'\0'} { } + constexpr descr() = default; constexpr descr(char const (&s)[N+1]) : descr(s, make_index_sequence()) { } template diff --git a/include/pybind11/detail/type_caster_base.h b/include/pybind11/detail/type_caster_base.h index 8ebe39879a..0dd9d48e55 100644 --- a/include/pybind11/detail/type_caster_base.h +++ b/include/pybind11/detail/type_caster_base.h @@ -241,7 +241,7 @@ struct value_and_holder { ? inst->simple_holder_constructed : inst->nonsimple.status[index] & instance::status_holder_constructed; } - void set_holder_constructed(bool v = true) { + void set_holder_constructed(bool v = true) const { if (inst->simple_layout) inst->simple_holder_constructed = v; else if (v) @@ -254,7 +254,7 @@ struct value_and_holder { ? inst->simple_instance_registered : inst->nonsimple.status[index] & instance::status_instance_registered; } - void set_instance_registered(bool v = true) { + void set_instance_registered(bool v = true) const { if (inst->simple_layout) inst->simple_instance_registered = v; else if (v) @@ -397,7 +397,7 @@ PYBIND11_NOINLINE inline void instance::allocate_layout() { owned = true; } -PYBIND11_NOINLINE inline void instance::deallocate_layout() { +PYBIND11_NOINLINE inline void instance::deallocate_layout() const { if (!simple_layout) PyMem_Free(nonsimple.values_and_holders); } diff --git a/include/pybind11/numpy.h b/include/pybind11/numpy.h index 9287842460..fdb7a9794b 100644 --- a/include/pybind11/numpy.h +++ b/include/pybind11/numpy.h @@ -1291,7 +1291,7 @@ class common_iterator { using value_type = container_type::value_type; using size_type = container_type::size_type; - common_iterator() : p_ptr(0), m_strides() {} + common_iterator() : m_strides() {} common_iterator(void* ptr, const container_type& strides, const container_type& shape) : p_ptr(reinterpret_cast(ptr)), m_strides(strides.size()) { @@ -1312,7 +1312,7 @@ class common_iterator { } private: - char* p_ptr; + char *p_ptr{0}; container_type m_strides; }; diff --git a/include/pybind11/pybind11.h b/include/pybind11/pybind11.h index 2ff88dbf5b..4979236050 100644 --- a/include/pybind11/pybind11.h +++ b/include/pybind11/pybind11.h @@ -395,7 +395,8 @@ class cpp_function : public function { rec->def = new PyMethodDef(); std::memset(rec->def, 0, sizeof(PyMethodDef)); rec->def->ml_name = rec->name; - rec->def->ml_meth = reinterpret_cast(reinterpret_cast(*dispatcher)); + rec->def->ml_meth + = reinterpret_cast(reinterpret_cast(dispatcher)); rec->def->ml_flags = METH_VARARGS | METH_KEYWORDS; capsule rec_capsule(unique_rec.release(), [](void *ptr) { diff --git a/tests/local_bindings.h b/tests/local_bindings.h index 22537b13ad..91c23fe594 100644 --- a/tests/local_bindings.h +++ b/tests/local_bindings.h @@ -56,7 +56,7 @@ class Pet { public: Pet(std::string name) : name_(name) {} std::string name_; - const std::string &name() { return name_; } + const std::string &name() const { return name_; } }; } // namespace pets diff --git a/tests/test_constants_and_functions.cpp b/tests/test_constants_and_functions.cpp index 5bbbd0c70b..04830d8d2d 100644 --- a/tests/test_constants_and_functions.cpp +++ b/tests/test_constants_and_functions.cpp @@ -1,5 +1,6 @@ /* - tests/test_constants_and_functions.cpp -- global constants and functions, enumerations, raw byte strings + tests/test_constants_and_functions.cpp -- global constants and functions, enumerations, raw + byte strings Copyright (c) 2016 Wenzel Jakob @@ -60,6 +61,7 @@ int f3(int x) noexcept(false) { return x+3; } # pragma GCC diagnostic push # pragma GCC diagnostic ignored "-Wdeprecated" #endif +// NOLINTNEXTLINE(modernize-use-noexcept) int f4(int x) throw() { return x+4; } // Deprecated equivalent to noexcept(true) #if defined(__GNUG__) && !defined(__INTEL_COMPILER) # pragma GCC diagnostic pop @@ -75,8 +77,10 @@ struct C { # pragma GCC diagnostic push # pragma GCC diagnostic ignored "-Wdeprecated" #endif - int m7(int x) throw() { return x-7; } - int m8(int x) const throw() { return x-8; } + // NOLINTNEXTLINE(modernize-use-noexcept) + int m7(int x) throw() { return x - 7; } + // NOLINTNEXTLINE(modernize-use-noexcept) + int m8(int x) const throw() { return x - 8; } #if defined(__GNUG__) && !defined(__INTEL_COMPILER) # pragma GCC diagnostic pop #endif diff --git a/tests/test_factory_constructors.cpp b/tests/test_factory_constructors.cpp index 6832ac4943..7308ad5fc1 100644 --- a/tests/test_factory_constructors.cpp +++ b/tests/test_factory_constructors.cpp @@ -77,7 +77,7 @@ class TestFactory6 { TestFactory6(const TestFactory6 &f) { print_copy_created(this); value = f.value; alias = f.alias; } virtual ~TestFactory6() { print_destroyed(this); } virtual int get() { return value; } - bool has_alias() { return alias; } + bool has_alias() const { return alias; } }; class PyTF6 : public TestFactory6 { public: @@ -102,7 +102,7 @@ class TestFactory7 { TestFactory7(const TestFactory7 &f) { print_copy_created(this); value = f.value; alias = f.alias; } virtual ~TestFactory7() { print_destroyed(this); } virtual int get() { return value; } - bool has_alias() { return alias; } + bool has_alias() const { return alias; } }; class PyTF7 : public TestFactory7 { public: diff --git a/tests/test_iostream.cpp b/tests/test_iostream.cpp index 1be0655dfb..f21fed5465 100644 --- a/tests/test_iostream.cpp +++ b/tests/test_iostream.cpp @@ -34,7 +34,7 @@ void noisy_funct_dual(std::string msg, std::string emsg) { // simply repeatedly write to std::cerr until stopped // redirect is called at some point to test the safety of scoped_estream_redirect struct TestThread { - TestThread() : t_{nullptr}, stop_{false} { + TestThread() : stop_{false} { auto thread_f = [this] { while (!stop_) { std::cout << "x" << std::flush; @@ -49,7 +49,7 @@ struct TestThread { void stop() { stop_ = true; } - void join() { + void join() const { py::gil_scoped_release gil_lock; t_->join(); } @@ -59,7 +59,7 @@ struct TestThread { std::this_thread::sleep_for(std::chrono::milliseconds(50)); } - std::thread * t_; + std::thread *t_{nullptr}; std::atomic stop_; }; diff --git a/tests/test_methods_and_attributes.cpp b/tests/test_methods_and_attributes.cpp index f99909bda7..1f855ef354 100644 --- a/tests/test_methods_and_attributes.cpp +++ b/tests/test_methods_and_attributes.cpp @@ -25,9 +25,7 @@ class ExampleMandA { ExampleMandA(ExampleMandA &&e) : value(e.value) { print_move_created(this); } ~ExampleMandA() { print_destroyed(this); } - std::string toString() { - return "ExampleMandA[value=" + std::to_string(value) + "]"; - } + std::string toString() const { return "ExampleMandA[value=" + std::to_string(value) + "]"; } void operator=(const ExampleMandA &e) { print_copy_assigned(this); value = e.value; } void operator=(ExampleMandA &&e) { print_move_assigned(this); value = e.value; } @@ -48,13 +46,13 @@ class ExampleMandA { ExampleMandA self1() { return *this; } // return by value ExampleMandA &self2() { return *this; } // return by reference - const ExampleMandA &self3() { return *this; } // return by const reference + const ExampleMandA &self3() const { return *this; } // return by const reference ExampleMandA *self4() { return this; } // return by pointer - const ExampleMandA *self5() { return this; } // return by const pointer + const ExampleMandA *self5() const { return this; } // return by const pointer - int internal1() { return value; } // return by value + int internal1() const { return value; } // return by value int &internal2() { return value; } // return by reference - const int &internal3() { return value; } // return by const reference + const int &internal3() const { return value; } // return by const reference int *internal4() { return &value; } // return by pointer const int *internal5() { return &value; } // return by const pointer diff --git a/tests/test_modules.cpp b/tests/test_modules.cpp index 67387e809b..5867135404 100644 --- a/tests/test_modules.cpp +++ b/tests/test_modules.cpp @@ -24,7 +24,8 @@ TEST_SUBMODULE(modules, m) { ~A() { print_destroyed(this); } A(const A&) { print_copy_created(this); } A& operator=(const A ©) { print_copy_assigned(this); v = copy.v; return *this; } - std::string toString() { return "A[" + std::to_string(v) + "]"; } + std::string toString() const { return "A[" + std::to_string(v) + "]"; } + private: int v; }; diff --git a/tests/test_multiple_inheritance.cpp b/tests/test_multiple_inheritance.cpp index ad801fe198..daecb9564f 100644 --- a/tests/test_multiple_inheritance.cpp +++ b/tests/test_multiple_inheritance.cpp @@ -48,12 +48,12 @@ int VanillaStaticMix2::static_value = 12; // test_multiple_inheritance_virtbase struct Base1a { Base1a(int i) : i(i) { } - int foo() { return i; } + int foo() const { return i; } int i; }; struct Base2a { Base2a(int i) : i(i) { } - int bar() { return i; } + int bar() const { return i; } int i; }; struct Base12a : Base1a, Base2a { @@ -78,7 +78,7 @@ TEST_SUBMODULE(multiple_inheritance, m) { // test_multiple_inheritance_mix2 struct Base1 { Base1(int i) : i(i) { } - int foo() { return i; } + int foo() const { return i; } int i; }; py::class_ b1(m, "Base1"); @@ -87,7 +87,7 @@ TEST_SUBMODULE(multiple_inheritance, m) { struct Base2 { Base2(int i) : i(i) { } - int bar() { return i; } + int bar() const { return i; } int i; }; py::class_ b2(m, "Base2"); diff --git a/tests/test_numpy_vectorize.cpp b/tests/test_numpy_vectorize.cpp index 274b7558a9..8ed9b27e1b 100644 --- a/tests/test_numpy_vectorize.cpp +++ b/tests/test_numpy_vectorize.cpp @@ -62,7 +62,7 @@ TEST_SUBMODULE(numpy_vectorize, m) { // test_method_vectorization struct VectorizeTestClass { VectorizeTestClass(int v) : value{v} {}; - float method(int x, float y) { return y + (float) (x + value); } + float method(int x, float y) const { return y + (float) (x + value); } int value = 0; }; py::class_ vtc(m, "VectorizeTestClass"); diff --git a/tests/test_smart_ptr.cpp b/tests/test_smart_ptr.cpp index e0af249790..2ece3bf0c9 100644 --- a/tests/test_smart_ptr.cpp +++ b/tests/test_smart_ptr.cpp @@ -240,7 +240,7 @@ struct ElementBase { struct ElementA : ElementBase { ElementA(int v) : v(v) { } - int value() { return v; } + int value() const { return v; } int v; }; diff --git a/tests/test_stl_binders.cpp b/tests/test_stl_binders.cpp index c791477e3e..22847eb7a1 100644 --- a/tests/test_stl_binders.cpp +++ b/tests/test_stl_binders.cpp @@ -125,5 +125,7 @@ TEST_SUBMODULE(stl_binders, m) { PYBIND11_NUMPY_DTYPE(VStruct, w, x, y, z); py::class_(m, "VStruct").def_readwrite("x", &VStruct::x); py::bind_vector>(m, "VectorStruct", py::buffer_protocol()); - m.def("get_vectorstruct", [] {return std::vector {{0, 5, 3.0, 1}, {1, 30, -1e4, 0}};}); + m.def("get_vectorstruct", [] { + return std::vector{{false, 5, 3.0, true}, {true, 30, -1e4, false}}; + }); } From dac74ebdf5d64cb30a4492003a87d7455ad00144 Mon Sep 17 00:00:00 2001 From: Aaron Gokaslan Date: Tue, 22 Jun 2021 12:11:54 -0400 Subject: [PATCH 037/638] fix(clang-tidy): performance fixes applied in tests and CI (#3051) * Initial fixes * Whoops * Finish clang-tidy manual fixes * Add two missing fixes * Revert * Update clang-tidy * Try to fix unreachable code error * Move nolint comment * Apply missing fix * Don't override clang-tidy config * Does this fix clang-tidy? * Make all clang-tidy errors visible * Add comments about NOLINTs and remove a few * Fix typo --- .clang-tidy | 1 + .github/workflows/format.yml | 6 +- include/pybind11/iostream.h | 2 +- include/pybind11/pybind11.h | 17 +-- tests/local_bindings.h | 4 +- tests/object.h | 4 +- tests/pybind11_cross_module_tests.cpp | 3 + tests/test_buffers.cpp | 43 ++++---- tests/test_builtin_casters.cpp | 12 +- tests/test_callbacks.cpp | 42 ++++--- tests/test_class.cpp | 22 ++-- tests/test_constants_and_functions.cpp | 13 ++- tests/test_copy_move.cpp | 31 ++++-- tests/test_custom_type_casters.cpp | 23 +++- tests/test_eigen.cpp | 21 +++- tests/test_embed/test_interpreter.cpp | 5 +- tests/test_eval.cpp | 4 +- tests/test_exceptions.cpp | 26 +++-- tests/test_factory_constructors.cpp | 126 ++++++++++++--------- tests/test_gil_scoped.cpp | 27 ++--- tests/test_iostream.cpp | 21 ++-- tests/test_kwargs_and_defaults.cpp | 24 ++-- tests/test_local_bindings.cpp | 2 + tests/test_methods_and_attributes.cpp | 61 +++++++---- tests/test_multiple_inheritance.cpp | 1 + tests/test_numpy_array.cpp | 136 +++++++++++++++-------- tests/test_numpy_dtypes.cpp | 9 +- tests/test_numpy_vectorize.cpp | 54 +++++---- tests/test_opaque_types.cpp | 2 +- tests/test_operator_overloading.cpp | 13 ++- tests/test_pickling.cpp | 63 +++++------ tests/test_pytypes.cpp | 64 ++++++----- tests/test_sequences_and_iterators.cpp | 146 ++++++++++++++----------- tests/test_smart_ptr.cpp | 50 ++++++--- tests/test_stl.cpp | 8 +- tests/test_virtual_functions.cpp | 15 ++- 36 files changed, 667 insertions(+), 434 deletions(-) diff --git a/.clang-tidy b/.clang-tidy index 60531abe05..22a736208c 100644 --- a/.clang-tidy +++ b/.clang-tidy @@ -18,6 +18,7 @@ modernize-use-noexcept, modernize-use-emplace, modernize-use-override, modernize-use-using, +*performance*, readability-container-size-empty, readability-make-member-function-const, readability-redundant-function-ptr-dereference, diff --git a/.github/workflows/format.yml b/.github/workflows/format.yml index 6550acccdb..27f52932b7 100644 --- a/.github/workflows/format.yml +++ b/.github/workflows/format.yml @@ -27,7 +27,7 @@ jobs: clang-tidy: name: Clang-Tidy runs-on: ubuntu-latest - container: silkeh/clang:10 + container: silkeh/clang:12 steps: - uses: actions/checkout@v2 @@ -37,10 +37,10 @@ jobs: - name: Configure run: > cmake -S . -B build - -DCMAKE_CXX_CLANG_TIDY="$(which clang-tidy);--warnings-as-errors=*" + -DCMAKE_CXX_CLANG_TIDY="$(which clang-tidy)" -DDOWNLOAD_EIGEN=ON -DDOWNLOAD_CATCH=ON -DCMAKE_CXX_STANDARD=17 - name: Build - run: cmake --build build -j 2 + run: cmake --build build -j 2 -- --keep-going diff --git a/include/pybind11/iostream.h b/include/pybind11/iostream.h index 6a793eadca..89d1813ba8 100644 --- a/include/pybind11/iostream.h +++ b/include/pybind11/iostream.h @@ -262,7 +262,7 @@ add_ostream_redirect(module_ m, const std::string &name = "ostream_redirect") { return class_(std::move(m), name.c_str(), module_local()) .def(init(), arg("stdout") = true, arg("stderr") = true) .def("__enter__", &detail::OstreamRedirect::enter) - .def("__exit__", [](detail::OstreamRedirect &self_, args) { self_.exit(); }); + .def("__exit__", [](detail::OstreamRedirect &self_, const args &) { self_.exit(); }); } PYBIND11_NAMESPACE_END(PYBIND11_NAMESPACE) diff --git a/include/pybind11/pybind11.h b/include/pybind11/pybind11.h index 4979236050..1fb0d1fa18 100644 --- a/include/pybind11/pybind11.h +++ b/include/pybind11/pybind11.h @@ -1629,12 +1629,13 @@ struct enum_base { auto static_property = handle((PyObject *) get_internals().static_property_type); m_base.attr("__repr__") = cpp_function( - [](object arg) -> str { + [](const object &arg) -> str { handle type = type::handle_of(arg); object type_name = type.attr("__name__"); return pybind11::str("<{}.{}: {}>").format(type_name, enum_name(arg), int_(arg)); - }, name("__repr__"), is_method(m_base) - ); + }, + name("__repr__"), + is_method(m_base)); m_base.attr("name") = property(cpp_function(&enum_name, name("name"), is_method(m_base))); @@ -1718,8 +1719,10 @@ struct enum_base { PYBIND11_ENUM_OP_CONV("__ror__", a | b); PYBIND11_ENUM_OP_CONV("__xor__", a ^ b); PYBIND11_ENUM_OP_CONV("__rxor__", a ^ b); - m_base.attr("__invert__") = cpp_function( - [](object arg) { return ~(int_(arg)); }, name("__invert__"), is_method(m_base)); + m_base.attr("__invert__") + = cpp_function([](const object &arg) { return ~(int_(arg)); }, + name("__invert__"), + is_method(m_base)); } } else { PYBIND11_ENUM_OP_STRICT("__eq__", int_(a).equal(int_(b)), return false); @@ -1740,10 +1743,10 @@ struct enum_base { #undef PYBIND11_ENUM_OP_STRICT m_base.attr("__getstate__") = cpp_function( - [](object arg) { return int_(arg); }, name("__getstate__"), is_method(m_base)); + [](const object &arg) { return int_(arg); }, name("__getstate__"), is_method(m_base)); m_base.attr("__hash__") = cpp_function( - [](object arg) { return int_(arg); }, name("__hash__"), is_method(m_base)); + [](const object &arg) { return int_(arg); }, name("__hash__"), is_method(m_base)); } PYBIND11_NOINLINE void value(char const* name_, object value, const char *doc = nullptr) { diff --git a/tests/local_bindings.h b/tests/local_bindings.h index 91c23fe594..8629ed7784 100644 --- a/tests/local_bindings.h +++ b/tests/local_bindings.h @@ -1,4 +1,6 @@ #pragma once +#include + #include "pybind11_tests.h" /// Simple class used to test py::local: @@ -54,7 +56,7 @@ py::class_ bind_local(Args && ...args) { namespace pets { class Pet { public: - Pet(std::string name) : name_(name) {} + Pet(std::string name) : name_(std::move(name)) {} std::string name_; const std::string &name() const { return name_; } }; diff --git a/tests/object.h b/tests/object.h index 9235f19c20..865a9beae8 100644 --- a/tests/object.h +++ b/tests/object.h @@ -81,7 +81,7 @@ template class ref { } /// Move constructor - ref(ref &&r) : m_ptr(r.m_ptr) { + ref(ref &&r) noexcept : m_ptr(r.m_ptr) { r.m_ptr = nullptr; print_move_created(this, "with pointer", m_ptr); track_move_created((ref_tag*) this); @@ -96,7 +96,7 @@ template class ref { } /// Move another reference into the current one - ref& operator=(ref&& r) { + ref &operator=(ref &&r) noexcept { print_move_assigned(this, "pointer", r.m_ptr); track_move_assigned((ref_tag*) this); if (*this == r) diff --git a/tests/pybind11_cross_module_tests.cpp b/tests/pybind11_cross_module_tests.cpp index 944d085597..f238ddb042 100644 --- a/tests/pybind11_cross_module_tests.cpp +++ b/tests/pybind11_cross_module_tests.cpp @@ -104,6 +104,8 @@ PYBIND11_MODULE(pybind11_cross_module_tests, m) { m.def("return_self", [](LocalVec *v) { return v; }); m.def("return_copy", [](const LocalVec &v) { return LocalVec(v); }); + // Changing this broke things with pygrep. TODO fix + // NOLINTNEXTLINE class Dog : public pets::Pet { public: Dog(std::string name) : Pet(name) {}; }; py::class_(m, "Pet", py::module_local()) .def("name", &pets::Pet::name); @@ -126,6 +128,7 @@ PYBIND11_MODULE(pybind11_cross_module_tests, m) { // test_missing_header_message // The main module already includes stl.h, but we need to test the error message // which appears when this header is missing. + // NOLINTNEXTLINE(performance-unnecessary-value-param) m.def("missing_header_arg", [](std::vector) { }); m.def("missing_header_return", []() { return std::vector(); }); } diff --git a/tests/test_buffers.cpp b/tests/test_buffers.cpp index 46eabf3963..9902c1084e 100644 --- a/tests/test_buffers.cpp +++ b/tests/test_buffers.cpp @@ -27,7 +27,7 @@ TEST_SUBMODULE(buffers, m) { memcpy(m_data, s.m_data, sizeof(float) * (size_t) (m_rows * m_cols)); } - Matrix(Matrix &&s) : m_rows(s.m_rows), m_cols(s.m_cols), m_data(s.m_data) { + Matrix(Matrix &&s) noexcept : m_rows(s.m_rows), m_cols(s.m_cols), m_data(s.m_data) { print_move_created(this); s.m_rows = 0; s.m_cols = 0; @@ -49,7 +49,7 @@ TEST_SUBMODULE(buffers, m) { return *this; } - Matrix &operator=(Matrix &&s) { + Matrix &operator=(Matrix &&s) noexcept { print_move_assigned(this, std::to_string(m_rows) + "x" + std::to_string(m_cols) + " matrix"); if (&s != this) { delete[] m_data; @@ -79,6 +79,7 @@ TEST_SUBMODULE(buffers, m) { py::class_(m, "Matrix", py::buffer_protocol()) .def(py::init()) /// Construct from a buffer + // NOLINTNEXTLINE(performance-unnecessary-value-param) .def(py::init([](py::buffer const b) { py::buffer_info info = b.request(); if (info.format != py::format_descriptor::format() || info.ndim != 2) @@ -89,31 +90,31 @@ TEST_SUBMODULE(buffers, m) { return v; })) - .def("rows", &Matrix::rows) - .def("cols", &Matrix::cols) + .def("rows", &Matrix::rows) + .def("cols", &Matrix::cols) /// Bare bones interface - .def("__getitem__", [](const Matrix &m, std::pair i) { - if (i.first >= m.rows() || i.second >= m.cols()) - throw py::index_error(); - return m(i.first, i.second); - }) - .def("__setitem__", [](Matrix &m, std::pair i, float v) { - if (i.first >= m.rows() || i.second >= m.cols()) - throw py::index_error(); - m(i.first, i.second) = v; - }) - /// Provide buffer access - .def_buffer([](Matrix &m) -> py::buffer_info { + .def("__getitem__", + [](const Matrix &m, std::pair i) { + if (i.first >= m.rows() || i.second >= m.cols()) + throw py::index_error(); + return m(i.first, i.second); + }) + .def("__setitem__", + [](Matrix &m, std::pair i, float v) { + if (i.first >= m.rows() || i.second >= m.cols()) + throw py::index_error(); + m(i.first, i.second) = v; + }) + /// Provide buffer access + .def_buffer([](Matrix &m) -> py::buffer_info { return py::buffer_info( m.data(), /* Pointer to buffer */ { m.rows(), m.cols() }, /* Buffer dimensions */ { sizeof(float) * size_t(m.cols()), /* Strides (in bytes) for each index */ sizeof(float) } ); - }) - ; - + }); // test_inherited_protocol class SquareMatrix : public Matrix { @@ -208,7 +209,5 @@ TEST_SUBMODULE(buffers, m) { }) ; - m.def("get_buffer_info", [](py::buffer buffer) { - return buffer.request(); - }); + m.def("get_buffer_info", [](const py::buffer &buffer) { return buffer.request(); }); } diff --git a/tests/test_builtin_casters.cpp b/tests/test_builtin_casters.cpp index f4e775676d..a98b67bdbd 100644 --- a/tests/test_builtin_casters.cpp +++ b/tests/test_builtin_casters.cpp @@ -30,7 +30,11 @@ class type_caster { // cast operator. bool load(handle, bool) { return true; } - operator ConstRefCasted&&() { value = {1}; return std::move(value); } + operator ConstRefCasted &&() { + value = {1}; + // NOLINTNEXTLINE(performance-move-const-arg) + return std::move(value); + } operator ConstRefCasted&() { value = {2}; return value; } operator ConstRefCasted*() { value = {3}; return &value; } @@ -101,6 +105,7 @@ TEST_SUBMODULE(builtin_casters, m) { // test_bytes_to_string m.def("strlen", [](char *s) { return strlen(s); }); + // NOLINTNEXTLINE(performance-unnecessary-value-param) m.def("string_length", [](std::string s) { return s.length(); }); #ifdef PYBIND11_HAS_U8STRING @@ -146,6 +151,7 @@ TEST_SUBMODULE(builtin_casters, m) { m.def("int_passthrough_noconvert", [](int arg) { return arg; }, py::arg{}.noconvert()); // test_tuple + // NOLINTNEXTLINE(performance-unnecessary-value-param) m.def("pair_passthrough", [](std::pair input) { return std::make_pair(input.second, input.first); }, "Return a pair in reversed order"); @@ -177,10 +183,13 @@ TEST_SUBMODULE(builtin_casters, m) { // test_none_deferred m.def("defer_none_cstring", [](char *) { return false; }); + // NOLINTNEXTLINE(performance-unnecessary-value-param) m.def("defer_none_cstring", [](py::none) { return true; }); m.def("defer_none_custom", [](UserType *) { return false; }); + // NOLINTNEXTLINE(performance-unnecessary-value-param) m.def("defer_none_custom", [](py::none) { return true; }); m.def("nodefer_none_void", [](void *) { return true; }); + // NOLINTNEXTLINE(performance-unnecessary-value-param) m.def("nodefer_none_void", [](py::none) { return false; }); // test_void_caster @@ -231,6 +240,7 @@ TEST_SUBMODULE(builtin_casters, m) { }, "copy"_a); m.def("refwrap_iiw", [](const IncType &w) { return w.value(); }); + // NOLINTNEXTLINE(performance-unnecessary-value-param) m.def("refwrap_call_iiw", [](IncType &w, py::function f) { py::list l; l.append(f(std::ref(w))); diff --git a/tests/test_callbacks.cpp b/tests/test_callbacks.cpp index 33927f5c38..908d5743e7 100644 --- a/tests/test_callbacks.cpp +++ b/tests/test_callbacks.cpp @@ -17,8 +17,8 @@ int dummy_function(int i) { return i + 1; } TEST_SUBMODULE(callbacks, m) { // test_callbacks, test_function_signatures - m.def("test_callback1", [](py::object func) { return func(); }); - m.def("test_callback2", [](py::object func) { return func("Hello", 'x', true, 5); }); + m.def("test_callback1", [](const py::object &func) { return func(); }); + m.def("test_callback2", [](const py::object &func) { return func("Hello", 'x', true, 5); }); m.def("test_callback3", [](const std::function &func) { return "func(43) = " + std::to_string(func(43)); }); m.def("test_callback4", []() -> std::function { return [](int i) { return i+1; }; }); @@ -27,51 +27,48 @@ TEST_SUBMODULE(callbacks, m) { }); // test_keyword_args_and_generalized_unpacking - m.def("test_tuple_unpacking", [](py::function f) { + m.def("test_tuple_unpacking", [](const py::function &f) { auto t1 = py::make_tuple(2, 3); auto t2 = py::make_tuple(5, 6); return f("positional", 1, *t1, 4, *t2); }); - m.def("test_dict_unpacking", [](py::function f) { + m.def("test_dict_unpacking", [](const py::function &f) { auto d1 = py::dict("key"_a="value", "a"_a=1); auto d2 = py::dict(); auto d3 = py::dict("b"_a=2); return f("positional", 1, **d1, **d2, **d3); }); - m.def("test_keyword_args", [](py::function f) { - return f("x"_a=10, "y"_a=20); - }); + m.def("test_keyword_args", [](const py::function &f) { return f("x"_a = 10, "y"_a = 20); }); - m.def("test_unpacking_and_keywords1", [](py::function f) { + m.def("test_unpacking_and_keywords1", [](const py::function &f) { auto args = py::make_tuple(2); auto kwargs = py::dict("d"_a=4); return f(1, *args, "c"_a=3, **kwargs); }); - m.def("test_unpacking_and_keywords2", [](py::function f) { + m.def("test_unpacking_and_keywords2", [](const py::function &f) { auto kwargs1 = py::dict("a"_a=1); auto kwargs2 = py::dict("c"_a=3, "d"_a=4); return f("positional", *py::make_tuple(1), 2, *py::make_tuple(3, 4), 5, "key"_a="value", **kwargs1, "b"_a=2, **kwargs2, "e"_a=5); }); - m.def("test_unpacking_error1", [](py::function f) { + m.def("test_unpacking_error1", [](const py::function &f) { auto kwargs = py::dict("x"_a=3); return f("x"_a=1, "y"_a=2, **kwargs); // duplicate ** after keyword }); - m.def("test_unpacking_error2", [](py::function f) { + m.def("test_unpacking_error2", [](const py::function &f) { auto kwargs = py::dict("x"_a=3); return f(**kwargs, "x"_a=1); // duplicate keyword after ** }); - m.def("test_arg_conversion_error1", [](py::function f) { - f(234, UnregisteredType(), "kw"_a=567); - }); + m.def("test_arg_conversion_error1", + [](const py::function &f) { f(234, UnregisteredType(), "kw"_a = 567); }); - m.def("test_arg_conversion_error2", [](py::function f) { + m.def("test_arg_conversion_error2", [](const py::function &f) { f(234, "expected_name"_a=UnregisteredType(), "kw"_a=567); }); @@ -80,7 +77,7 @@ TEST_SUBMODULE(callbacks, m) { Payload() { print_default_created(this); } ~Payload() { print_destroyed(this); } Payload(const Payload &) { print_copy_created(this); } - Payload(Payload &&) { print_move_created(this); } + Payload(Payload &&) noexcept { print_move_created(this); } }; // Export the payload constructor statistics for testing purposes: m.def("payload_cstats", &ConstructorStats::get); @@ -127,7 +124,8 @@ TEST_SUBMODULE(callbacks, m) { virtual ~AbstractBase() {}; // NOLINT(modernize-use-equals-default) virtual unsigned int func() = 0; }; - m.def("func_accepting_func_accepting_base", [](std::function) { }); + m.def("func_accepting_func_accepting_base", + [](const std::function &) {}); struct MovableObject { bool valid = true; @@ -135,8 +133,8 @@ TEST_SUBMODULE(callbacks, m) { MovableObject() = default; MovableObject(const MovableObject &) = default; MovableObject &operator=(const MovableObject &) = default; - MovableObject(MovableObject &&o) : valid(o.valid) { o.valid = false; } - MovableObject &operator=(MovableObject &&o) { + MovableObject(MovableObject &&o) noexcept : valid(o.valid) { o.valid = false; } + MovableObject &operator=(MovableObject &&o) noexcept { valid = o.valid; o.valid = false; return *this; @@ -145,7 +143,7 @@ TEST_SUBMODULE(callbacks, m) { py::class_(m, "MovableObject"); // test_movable_object - m.def("callback_with_movable", [](std::function f) { + m.def("callback_with_movable", [](const std::function &f) { auto x = MovableObject(); f(x); // lvalue reference shouldn't move out object return x.valid; // must still return `true` @@ -159,7 +157,7 @@ TEST_SUBMODULE(callbacks, m) { // test async Python callbacks using callback_f = std::function; - m.def("test_async_callback", [](callback_f f, py::list work) { + m.def("test_async_callback", [](const callback_f &f, const py::list &work) { // make detached thread that calls `f` with piece of work after a little delay auto start_f = [f](int j) { auto invoke_f = [f, j] { @@ -175,7 +173,7 @@ TEST_SUBMODULE(callbacks, m) { start_f(py::cast(i)); }); - m.def("callback_num_times", [](py::function f, std::size_t num) { + m.def("callback_num_times", [](const py::function &f, std::size_t num) { for (std::size_t i = 0; i < num; i++) { f(); } diff --git a/tests/test_class.cpp b/tests/test_class.cpp index bd545e8c68..a26c59c1ce 100644 --- a/tests/test_class.cpp +++ b/tests/test_class.cpp @@ -19,6 +19,8 @@ #include "local_bindings.h" #include +#include + #if defined(_MSC_VER) # pragma warning(disable: 4324) // warning C4324: structure was padded due to alignment specifier #endif @@ -129,6 +131,7 @@ TEST_SUBMODULE(class_, m) { m.def("return_none", []() -> BaseClass* { return nullptr; }); // test_isinstance + // NOLINTNEXTLINE(performance-unnecessary-value-param) m.def("check_instances", [](py::list l) { return py::make_tuple( py::isinstance(l[0]), @@ -155,17 +158,13 @@ TEST_SUBMODULE(class_, m) { return py::type::of(); }); - m.def("get_type_of", [](py::object ob) { - return py::type::of(ob); - }); + m.def("get_type_of", [](py::object ob) { return py::type::of(std::move(ob)); }); m.def("get_type_classic", [](py::handle h) { return h.get_type(); }); - m.def("as_type", [](py::object ob) { - return py::type(ob); - }); + m.def("as_type", [](const py::object &ob) { return py::type(ob); }); // test_mismatched_holder struct MismatchBase1 { }; @@ -219,6 +218,7 @@ TEST_SUBMODULE(class_, m) { py::implicitly_convertible(); m.def("implicitly_convert_argument", [](const ConvertibleFromUserType &r) { return r.i; }); + // NOLINTNEXTLINE(performance-unnecessary-value-param) m.def("implicitly_convert_variable", [](py::object o) { // `o` is `UserType` and `r` is a reference to a temporary created by implicit // conversion. This is valid when called inside a bound function because the temp @@ -397,6 +397,7 @@ TEST_SUBMODULE(class_, m) { struct StringWrapper { std::string str; }; m.def("test_error_after_conversions", [](int) {}); m.def("test_error_after_conversions", + // NOLINTNEXTLINE(performance-unnecessary-value-param) [](StringWrapper) -> NotRegistered { return {}; }); py::class_(m, "StringWrapper").def(py::init()); py::implicitly_convertible(); @@ -461,19 +462,20 @@ TEST_SUBMODULE(class_, m) { struct OtherDuplicate {}; struct DuplicateNested {}; struct OtherDuplicateNested {}; - m.def("register_duplicate_class_name", [](py::module_ m) { + + m.def("register_duplicate_class_name", [](const py::module_ &m) { py::class_(m, "Duplicate"); py::class_(m, "Duplicate"); }); - m.def("register_duplicate_class_type", [](py::module_ m) { + m.def("register_duplicate_class_type", [](const py::module_ &m) { py::class_(m, "OtherDuplicate"); py::class_(m, "YetAnotherDuplicate"); }); - m.def("register_duplicate_nested_class_name", [](py::object gt) { + m.def("register_duplicate_nested_class_name", [](const py::object >) { py::class_(gt, "DuplicateNested"); py::class_(gt, "DuplicateNested"); }); - m.def("register_duplicate_nested_class_type", [](py::object gt) { + m.def("register_duplicate_nested_class_type", [](const py::object >) { py::class_(gt, "OtherDuplicateNested"); py::class_(gt, "YetAnotherDuplicateNested"); }); diff --git a/tests/test_constants_and_functions.cpp b/tests/test_constants_and_functions.cpp index 04830d8d2d..e7f018540c 100644 --- a/tests/test_constants_and_functions.cpp +++ b/tests/test_constants_and_functions.cpp @@ -34,7 +34,7 @@ py::bytes return_bytes() { return std::string(data, 4); } -std::string print_bytes(py::bytes bytes) { +std::string print_bytes(const py::bytes &bytes) { std::string ret = "bytes["; const auto value = static_cast(bytes); for (size_t i = 0; i < value.length(); ++i) { @@ -146,8 +146,13 @@ TEST_SUBMODULE(constants_and_functions, m) { LargeCapture capture; // VS 2015's MSVC is acting up if we create the array here m.def("should_raise", [capture](int) { return capture.zeros[9] + 33; }, py::kw_only(), py::arg()); }); - m.def("register_with_raising_repr", [](py::module_ m, py::object default_value) { - m.def("should_raise", [](int, int, py::object) { return 42; }, "some docstring", - py::arg_v("x", 42), py::arg_v("y", 42, ""), py::arg_v("z", default_value)); + m.def("register_with_raising_repr", [](py::module_ m, const py::object &default_value) { + m.def( + "should_raise", + [](int, int, const py::object &) { return 42; }, + "some docstring", + py::arg_v("x", 42), + py::arg_v("y", 42, ""), + py::arg_v("z", default_value)); }); } diff --git a/tests/test_copy_move.cpp b/tests/test_copy_move.cpp index 322e9bb85d..08f4dd7497 100644 --- a/tests/test_copy_move.cpp +++ b/tests/test_copy_move.cpp @@ -37,9 +37,16 @@ template <> lacking_move_ctor empty::instance_ = {}; class MoveOnlyInt { public: MoveOnlyInt() { print_default_created(this); } - MoveOnlyInt(int v) : value{std::move(v)} { print_created(this, value); } - MoveOnlyInt(MoveOnlyInt &&m) { print_move_created(this, m.value); std::swap(value, m.value); } - MoveOnlyInt &operator=(MoveOnlyInt &&m) { print_move_assigned(this, m.value); std::swap(value, m.value); return *this; } + MoveOnlyInt(int v) : value{v} { print_created(this, value); } + MoveOnlyInt(MoveOnlyInt &&m) noexcept { + print_move_created(this, m.value); + std::swap(value, m.value); + } + MoveOnlyInt &operator=(MoveOnlyInt &&m) noexcept { + print_move_assigned(this, m.value); + std::swap(value, m.value); + return *this; + } MoveOnlyInt(const MoveOnlyInt &) = delete; MoveOnlyInt &operator=(const MoveOnlyInt &) = delete; ~MoveOnlyInt() { print_destroyed(this); } @@ -49,9 +56,16 @@ class MoveOnlyInt { class MoveOrCopyInt { public: MoveOrCopyInt() { print_default_created(this); } - MoveOrCopyInt(int v) : value{std::move(v)} { print_created(this, value); } - MoveOrCopyInt(MoveOrCopyInt &&m) { print_move_created(this, m.value); std::swap(value, m.value); } - MoveOrCopyInt &operator=(MoveOrCopyInt &&m) { print_move_assigned(this, m.value); std::swap(value, m.value); return *this; } + MoveOrCopyInt(int v) : value{v} { print_created(this, value); } + MoveOrCopyInt(MoveOrCopyInt &&m) noexcept { + print_move_created(this, m.value); + std::swap(value, m.value); + } + MoveOrCopyInt &operator=(MoveOrCopyInt &&m) noexcept { + print_move_assigned(this, m.value); + std::swap(value, m.value); + return *this; + } MoveOrCopyInt(const MoveOrCopyInt &c) { print_copy_created(this, c.value); value = c.value; } MoveOrCopyInt &operator=(const MoveOrCopyInt &c) { print_copy_assigned(this, c.value); value = c.value; return *this; } ~MoveOrCopyInt() { print_destroyed(this); } @@ -61,7 +75,7 @@ class MoveOrCopyInt { class CopyOnlyInt { public: CopyOnlyInt() { print_default_created(this); } - CopyOnlyInt(int v) : value{std::move(v)} { print_created(this, value); } + CopyOnlyInt(int v) : value{v} { print_created(this, value); } CopyOnlyInt(const CopyOnlyInt &c) { print_copy_created(this, c.value); value = c.value; } CopyOnlyInt &operator=(const CopyOnlyInt &c) { print_copy_assigned(this, c.value); value = c.value; return *this; } ~CopyOnlyInt() { print_destroyed(this); } @@ -111,6 +125,7 @@ TEST_SUBMODULE(copy_move_policies, m) { py::return_value_policy::move); // test_move_and_copy_casts + // NOLINTNEXTLINE(performance-unnecessary-value-param) m.def("move_and_copy_casts", [](py::object o) { int r = 0; r += py::cast(o).value; /* moves */ @@ -126,7 +141,9 @@ TEST_SUBMODULE(copy_move_policies, m) { // test_move_and_copy_loads m.def("move_only", [](MoveOnlyInt m) { return m.value; }); + // NOLINTNEXTLINE(performance-unnecessary-value-param) m.def("move_or_copy", [](MoveOrCopyInt m) { return m.value; }); + // NOLINTNEXTLINE(performance-unnecessary-value-param) m.def("copy_only", [](CopyOnlyInt m) { return m.value; }); m.def("move_pair", [](std::pair p) { return p.first.value + p.second.value; diff --git a/tests/test_custom_type_casters.cpp b/tests/test_custom_type_casters.cpp index 3fe910d491..076777d6f1 100644 --- a/tests/test_custom_type_casters.cpp +++ b/tests/test_custom_type_casters.cpp @@ -67,9 +67,12 @@ class DestructionTester { DestructionTester() { print_default_created(this); } ~DestructionTester() { print_destroyed(this); } DestructionTester(const DestructionTester &) { print_copy_created(this); } - DestructionTester(DestructionTester &&) { print_move_created(this); } + DestructionTester(DestructionTester &&) noexcept { print_move_created(this); } DestructionTester &operator=(const DestructionTester &) { print_copy_assigned(this); return *this; } - DestructionTester &operator=(DestructionTester &&) { print_move_assigned(this); return *this; } + DestructionTester &operator=(DestructionTester &&) noexcept { + print_move_assigned(this); + return *this; + } }; namespace pybind11 { namespace detail { template <> struct type_caster { @@ -94,7 +97,11 @@ TEST_SUBMODULE(custom_type_casters, m) { class ArgInspector { public: ArgInspector1 f(ArgInspector1 a, ArgAlwaysConverts) { return a; } - std::string g(ArgInspector1 a, const ArgInspector1 &b, int c, ArgInspector2 *d, ArgAlwaysConverts) { + std::string g(const ArgInspector1 &a, + const ArgInspector1 &b, + int c, + ArgInspector2 *d, + ArgAlwaysConverts) { return a.arg + "\n" + b.arg + "\n" + std::to_string(c) + "\n" + d->arg; } static ArgInspector2 h(ArgInspector2 a, ArgAlwaysConverts) { return a; } @@ -106,8 +113,14 @@ TEST_SUBMODULE(custom_type_casters, m) { .def("g", &ArgInspector::g, "a"_a.noconvert(), "b"_a, "c"_a.noconvert()=13, "d"_a=ArgInspector2(), py::arg() = ArgAlwaysConverts()) .def_static("h", &ArgInspector::h, py::arg{}.noconvert(), py::arg() = ArgAlwaysConverts()) ; - m.def("arg_inspect_func", [](ArgInspector2 a, ArgInspector1 b, ArgAlwaysConverts) { return a.arg + "\n" + b.arg; }, - py::arg{}.noconvert(false), py::arg_v(nullptr, ArgInspector1()).noconvert(true), py::arg() = ArgAlwaysConverts()); + m.def( + "arg_inspect_func", + [](const ArgInspector2 &a, const ArgInspector1 &b, ArgAlwaysConverts) { + return a.arg + "\n" + b.arg; + }, + py::arg{}.noconvert(false), + py::arg_v(nullptr, ArgInspector1()).noconvert(true), + py::arg() = ArgAlwaysConverts()); m.def("floats_preferred", [](double f) { return 0.5 * f; }, "f"_a); m.def("floats_only", [](double f) { return 0.5 * f; }, "f"_a.noconvert()); diff --git a/tests/test_eigen.cpp b/tests/test_eigen.cpp index 8432547439..a24bf273d2 100644 --- a/tests/test_eigen.cpp +++ b/tests/test_eigen.cpp @@ -54,8 +54,7 @@ void reset_refs() { } // Returns element 2,1 from a matrix (used to test copy/nocopy) -double get_elem(Eigen::Ref m) { return m(2, 1); }; - +double get_elem(const Eigen::Ref &m) { return m(2, 1); }; // Returns a matrix with 10*r + 100*c added to each matrix element (to help test that the matrix // reference is referencing rows/columns correctly). @@ -94,14 +93,16 @@ TEST_SUBMODULE(eigen, m) { m.def("double_complex", [](const Eigen::VectorXcf &x) -> Eigen::VectorXcf { return 2.0f * x; }); m.def("double_threec", [](py::EigenDRef x) { x *= 2; }); m.def("double_threer", [](py::EigenDRef x) { x *= 2; }); - m.def("double_mat_cm", [](Eigen::MatrixXf x) -> Eigen::MatrixXf { return 2.0f * x; }); - m.def("double_mat_rm", [](DenseMatrixR x) -> DenseMatrixR { return 2.0f * x; }); + m.def("double_mat_cm", [](const Eigen::MatrixXf &x) -> Eigen::MatrixXf { return 2.0f * x; }); + m.def("double_mat_rm", [](const DenseMatrixR &x) -> DenseMatrixR { return 2.0f * x; }); // test_eigen_ref_to_python // Different ways of passing via Eigen::Ref; the first and second are the Eigen-recommended + // NOLINTNEXTLINE (performance-unnecessary-value-param) m.def("cholesky1", [](Eigen::Ref x) -> Eigen::MatrixXd { return x.llt().matrixL(); }); m.def("cholesky2", [](const Eigen::Ref &x) -> Eigen::MatrixXd { return x.llt().matrixL(); }); m.def("cholesky3", [](const Eigen::Ref &x) -> Eigen::MatrixXd { return x.llt().matrixL(); }); + // NOLINTNEXTLINE (performance-unnecessary-value-param) m.def("cholesky4", [](Eigen::Ref x) -> Eigen::MatrixXd { return x.llt().matrixL(); }); // test_eigen_ref_mutators @@ -247,8 +248,11 @@ TEST_SUBMODULE(eigen, m) { m.def("fixed_copy_r", [](const FixedMatrixR &m) -> FixedMatrixR { return m; }); m.def("fixed_copy_c", [](const FixedMatrixC &m) -> FixedMatrixC { return m; }); // test_mutator_descriptors + // NOLINTNEXTLINE (performance-unnecessary-value-param) m.def("fixed_mutator_r", [](Eigen::Ref) {}); + // NOLINTNEXTLINE (performance-unnecessary-value-param) m.def("fixed_mutator_c", [](Eigen::Ref) {}); + // NOLINTNEXTLINE (performance-unnecessary-value-param) m.def("fixed_mutator_a", [](py::EigenDRef) {}); // test_dense m.def("dense_r", [mat]() -> DenseMatrixR { return DenseMatrixR(mat); }); @@ -280,6 +284,7 @@ TEST_SUBMODULE(eigen, m) { // that would allow copying (if types or strides don't match) for comparison: m.def("get_elem", &get_elem); // Now this alternative that calls the tells pybind to fail rather than copy: + // NOLINTNEXTLINE (performance-unnecessary-value-param) m.def("get_elem_nocopy", [](Eigen::Ref m) -> double { return get_elem(m); }, py::arg{}.noconvert()); // Also test a row-major-only no-copy const ref: @@ -295,13 +300,16 @@ TEST_SUBMODULE(eigen, m) { // test_issue1105 // Issue #1105: when converting from a numpy two-dimensional (Nx1) or (1xN) value into a dense - // eigen Vector or RowVector, the argument would fail to load because the numpy copy would fail: - // numpy won't broadcast a Nx1 into a 1-dimensional vector. + // eigen Vector or RowVector, the argument would fail to load because the numpy copy would + // fail: numpy won't broadcast a Nx1 into a 1-dimensional vector. NOLINTNEXTLINE + // NOLINTNEXTLINE (performance-unnecessary-value-param) m.def("iss1105_col", [](Eigen::VectorXd) { return true; }); + // NOLINTNEXTLINE (performance-unnecessary-value-param) m.def("iss1105_row", [](Eigen::RowVectorXd) { return true; }); // test_named_arguments // Make sure named arguments are working properly: + // NOLINTNEXTLINE (performance-unnecessary-value-param) m.def("matrix_multiply", [](const py::EigenDRef A, const py::EigenDRef B) -> Eigen::MatrixXd { if (A.cols() != B.rows()) throw std::domain_error("Nonconformable matrices!"); @@ -318,6 +326,7 @@ TEST_SUBMODULE(eigen, m) { // In case of a failure (the caster's temp array does not live long enough), creating // a new array (np.ones(10)) increases the chances that the temp array will be garbage // collected and/or that its memory will be overridden with different values. + // NOLINTNEXTLINE (performance-unnecessary-value-param) m.def("get_elem_direct", [](Eigen::Ref v) { py::module_::import("numpy").attr("ones")(10); return v(5); diff --git a/tests/test_embed/test_interpreter.cpp b/tests/test_embed/test_interpreter.cpp index 944334ce66..6925ee9782 100644 --- a/tests/test_embed/test_interpreter.cpp +++ b/tests/test_embed/test_interpreter.cpp @@ -8,16 +8,17 @@ #include -#include #include #include +#include +#include namespace py = pybind11; using namespace py::literals; class Widget { public: - Widget(std::string message) : message(message) { } + Widget(std::string message) : message(std::move(message)) {} virtual ~Widget() = default; std::string the_message() const { return message; } diff --git a/tests/test_eval.cpp b/tests/test_eval.cpp index 5416c2ec4d..66cdf71051 100644 --- a/tests/test_eval.cpp +++ b/tests/test_eval.cpp @@ -9,7 +9,9 @@ #include + #include "pybind11_tests.h" +#include TEST_SUBMODULE(eval_, m) { // test_evals @@ -67,7 +69,7 @@ TEST_SUBMODULE(eval_, m) { int val_out; local["call_test2"] = py::cpp_function([&](int value) { val_out = value; }); - auto result = py::eval_file(filename, global, local); + auto result = py::eval_file(std::move(filename), global, local); return val_out == 43 && result.is_none(); }); diff --git a/tests/test_exceptions.cpp b/tests/test_exceptions.cpp index 4c5e10dbe0..d34f1a94e1 100644 --- a/tests/test_exceptions.cpp +++ b/tests/test_exceptions.cpp @@ -8,7 +8,9 @@ */ #include "test_exceptions.h" + #include "pybind11_tests.h" +#include // A type that should be raised as an exception in Python class MyException : public std::exception { @@ -199,6 +201,8 @@ TEST_SUBMODULE(exceptions, m) { throw py::error_already_set(); }); + // Changing this broke things. Don't know why + // NOLINTNEXTLINE(performance-unnecessary-value-param) m.def("python_call_in_destructor", [](py::dict d) { try { PythonCallInDestructor set_dict_in_destructor(d); @@ -210,21 +214,23 @@ TEST_SUBMODULE(exceptions, m) { return false; }); - m.def("python_alreadyset_in_destructor", [](py::str s) { + m.def("python_alreadyset_in_destructor", [](const py::str &s) { PythonAlreadySetInDestructor alreadyset_in_destructor(s); return true; }); // test_nested_throws - m.def("try_catch", [m](py::object exc_type, py::function f, py::args args) { - try { f(*args); } - catch (py::error_already_set &ex) { - if (ex.matches(exc_type)) - py::print(ex.what()); - else - throw; - } - }); + m.def("try_catch", + [m](const py::object &exc_type, const py::function &f, const py::args &args) { + try { + f(*args); + } catch (py::error_already_set &ex) { + if (ex.matches(exc_type)) + py::print(ex.what()); + else + throw; + } + }); // Test repr that cannot be displayed m.def("simple_bool_passthrough", [](bool x) {return x;}); diff --git a/tests/test_factory_constructors.cpp b/tests/test_factory_constructors.cpp index 7308ad5fc1..235ec4dca6 100644 --- a/tests/test_factory_constructors.cpp +++ b/tests/test_factory_constructors.cpp @@ -8,10 +8,11 @@ BSD-style license that can be found in the LICENSE file. */ -#include "pybind11_tests.h" #include "constructor_stats.h" +#include "pybind11_tests.h" #include #include +#include // Classes for testing python construction via C++ factory function: // Not publicly constructible, copyable, or movable: @@ -35,8 +36,15 @@ class TestFactory2 { TestFactory2(int v) : value(std::to_string(v)) { print_created(this, value); } TestFactory2(std::string v) : value(std::move(v)) { print_created(this, value); } public: - TestFactory2(TestFactory2 &&m) { value = std::move(m.value); print_move_created(this); } - TestFactory2 &operator=(TestFactory2 &&m) { value = std::move(m.value); print_move_assigned(this); return *this; } + TestFactory2(TestFactory2 &&m) noexcept { + value = std::move(m.value); + print_move_created(this); + } + TestFactory2 &operator=(TestFactory2 &&m) noexcept { + value = std::move(m.value); + print_move_assigned(this); + return *this; + } std::string value; ~TestFactory2() { print_destroyed(this); } }; @@ -48,8 +56,15 @@ class TestFactory3 { TestFactory3(int v) : value(std::to_string(v)) { print_created(this, value); } public: TestFactory3(std::string v) : value(std::move(v)) { print_created(this, value); } - TestFactory3(TestFactory3 &&m) { value = std::move(m.value); print_move_created(this); } - TestFactory3 &operator=(TestFactory3 &&m) { value = std::move(m.value); print_move_assigned(this); return *this; } + TestFactory3(TestFactory3 &&m) noexcept { + value = std::move(m.value); + print_move_created(this); + } + TestFactory3 &operator=(TestFactory3 &&m) noexcept { + value = std::move(m.value); + print_move_assigned(this); + return *this; + } std::string value; virtual ~TestFactory3() { print_destroyed(this); } }; @@ -73,7 +88,11 @@ class TestFactory6 { bool alias = false; public: TestFactory6(int i) : value{i} { print_created(this, i); } - TestFactory6(TestFactory6 &&f) { print_move_created(this); value = f.value; alias = f.alias; } + TestFactory6(TestFactory6 &&f) noexcept { + print_move_created(this); + value = f.value; + alias = f.alias; + } TestFactory6(const TestFactory6 &f) { print_copy_created(this); value = f.value; alias = f.alias; } virtual ~TestFactory6() { print_destroyed(this); } virtual int get() { return value; } @@ -85,7 +104,7 @@ class PyTF6 : public TestFactory6 { // when an alias is needed: PyTF6(TestFactory6 &&base) : TestFactory6(std::move(base)) { alias = true; print_created(this, "move", value); } PyTF6(int i) : TestFactory6(i) { alias = true; print_created(this, i); } - PyTF6(PyTF6 &&f) : TestFactory6(std::move(f)) { print_move_created(this); } + PyTF6(PyTF6 &&f) noexcept : TestFactory6(std::move(f)) { print_move_created(this); } PyTF6(const PyTF6 &f) : TestFactory6(f) { print_copy_created(this); } PyTF6(std::string s) : TestFactory6((int) s.size()) { alias = true; print_created(this, s); } ~PyTF6() override { print_destroyed(this); } @@ -98,7 +117,11 @@ class TestFactory7 { bool alias = false; public: TestFactory7(int i) : value{i} { print_created(this, i); } - TestFactory7(TestFactory7 &&f) { print_move_created(this); value = f.value; alias = f.alias; } + TestFactory7(TestFactory7 &&f) noexcept { + print_move_created(this); + value = f.value; + alias = f.alias; + } TestFactory7(const TestFactory7 &f) { print_copy_created(this); value = f.value; alias = f.alias; } virtual ~TestFactory7() { print_destroyed(this); } virtual int get() { return value; } @@ -107,7 +130,7 @@ class TestFactory7 { class PyTF7 : public TestFactory7 { public: PyTF7(int i) : TestFactory7(i) { alias = true; print_created(this, i); } - PyTF7(PyTF7 &&f) : TestFactory7(std::move(f)) { print_move_created(this); } + PyTF7(PyTF7 &&f) noexcept : TestFactory7(std::move(f)) { print_move_created(this); } PyTF7(const PyTF7 &f) : TestFactory7(f) { print_copy_created(this); } ~PyTF7() override { print_destroyed(this); } int get() override { PYBIND11_OVERRIDE(int, TestFactory7, get, /*no args*/); } @@ -122,7 +145,9 @@ class TestFactoryHelper { // Holder: static std::unique_ptr construct1(int a) { return std::unique_ptr(new TestFactory1(a)); } // pointer again - static TestFactory1 *construct1_string(std::string a) { return new TestFactory1(a); } + static TestFactory1 *construct1_string(std::string a) { + return new TestFactory1(std::move(a)); + } // Moveable type: // pointer: @@ -130,7 +155,7 @@ class TestFactoryHelper { // holder: static std::unique_ptr construct2(int a) { return std::unique_ptr(new TestFactory2(a)); } // by value moving: - static TestFactory2 construct2(std::string a) { return TestFactory2(a); } + static TestFactory2 construct2(std::string a) { return TestFactory2(std::move(a)); } // shared_ptr holder type: // pointer: @@ -173,10 +198,11 @@ TEST_SUBMODULE(factory_constructors, m) { ; py::class_(m, "TestFactory2") .def(py::init([](pointer_tag, int v) { return TestFactoryHelper::construct2(v); })) - .def(py::init([](unique_ptr_tag, std::string v) { return TestFactoryHelper::construct2(v); })) + .def(py::init([](unique_ptr_tag, std::string v) { + return TestFactoryHelper::construct2(std::move(v)); + })) .def(py::init([](move_tag) { return TestFactoryHelper::construct2(); })) - .def_readwrite("value", &TestFactory2::value) - ; + .def_readwrite("value", &TestFactory2::value); // Stateful & reused: int c = 1; @@ -188,7 +214,9 @@ TEST_SUBMODULE(factory_constructors, m) { .def(py::init([](pointer_tag, int v) { return TestFactoryHelper::construct3(v); })) .def(py::init([](shared_ptr_tag) { return TestFactoryHelper::construct3(); })); ignoreOldStyleInitWarnings([&pyTestFactory3]() { - pyTestFactory3.def("__init__", [](TestFactory3 &self, std::string v) { new (&self) TestFactory3(v); }); // placement-new ctor + pyTestFactory3.def("__init__", [](TestFactory3 &self, std::string v) { + new (&self) TestFactory3(std::move(v)); + }); // placement-new ctor }); pyTestFactory3 // factories returning a derived type: @@ -219,52 +247,54 @@ TEST_SUBMODULE(factory_constructors, m) { py::class_(m, "TestFactory6") .def(py::init([](base_tag, int i) { return TestFactory6(i); })) .def(py::init([](alias_tag, int i) { return PyTF6(i); })) - .def(py::init([](alias_tag, std::string s) { return PyTF6(s); })) + .def(py::init([](alias_tag, std::string s) { return PyTF6(std::move(s)); })) .def(py::init([](alias_tag, pointer_tag, int i) { return new PyTF6(i); })) .def(py::init([](base_tag, pointer_tag, int i) { return new TestFactory6(i); })) - .def(py::init([](base_tag, alias_tag, pointer_tag, int i) { return (TestFactory6 *) new PyTF6(i); })) + .def(py::init( + [](base_tag, alias_tag, pointer_tag, int i) { return (TestFactory6 *) new PyTF6(i); })) .def("get", &TestFactory6::get) .def("has_alias", &TestFactory6::has_alias) - .def_static("get_cstats", &ConstructorStats::get, py::return_value_policy::reference) - .def_static("get_alias_cstats", &ConstructorStats::get, py::return_value_policy::reference) - ; + .def_static( + "get_cstats", &ConstructorStats::get, py::return_value_policy::reference) + .def_static( + "get_alias_cstats", &ConstructorStats::get, py::return_value_policy::reference); // test_init_factory_dual // Separate alias constructor testing py::class_>(m, "TestFactory7") - .def(py::init( - [](int i) { return TestFactory7(i); }, - [](int i) { return PyTF7(i); })) - .def(py::init( - [](pointer_tag, int i) { return new TestFactory7(i); }, - [](pointer_tag, int i) { return new PyTF7(i); })) - .def(py::init( - [](mixed_tag, int i) { return new TestFactory7(i); }, - [](mixed_tag, int i) { return PyTF7(i); })) - .def(py::init( - [](mixed_tag, std::string s) { return TestFactory7((int) s.size()); }, - [](mixed_tag, std::string s) { return new PyTF7((int) s.size()); })) - .def(py::init( - [](base_tag, pointer_tag, int i) { return new TestFactory7(i); }, - [](base_tag, pointer_tag, int i) { return (TestFactory7 *) new PyTF7(i); })) - .def(py::init( - [](alias_tag, pointer_tag, int i) { return new PyTF7(i); }, - [](alias_tag, pointer_tag, int i) { return new PyTF7(10*i); })) + .def(py::init([](int i) { return TestFactory7(i); }, [](int i) { return PyTF7(i); })) + .def(py::init([](pointer_tag, int i) { return new TestFactory7(i); }, + [](pointer_tag, int i) { return new PyTF7(i); })) + .def(py::init([](mixed_tag, int i) { return new TestFactory7(i); }, + [](mixed_tag, int i) { return PyTF7(i); })) + .def(py::init([](mixed_tag, const std::string &s) { return TestFactory7((int) s.size()); }, + [](mixed_tag, const std::string &s) { return new PyTF7((int) s.size()); })) + .def(py::init([](base_tag, pointer_tag, int i) { return new TestFactory7(i); }, + [](base_tag, pointer_tag, int i) { return (TestFactory7 *) new PyTF7(i); })) + .def(py::init([](alias_tag, pointer_tag, int i) { return new PyTF7(i); }, + [](alias_tag, pointer_tag, int i) { return new PyTF7(10 * i); })) .def(py::init( [](shared_ptr_tag, base_tag, int i) { return std::make_shared(i); }, - [](shared_ptr_tag, base_tag, int i) { auto *p = new PyTF7(i); return std::shared_ptr(p); })) - .def(py::init( - [](shared_ptr_tag, invalid_base_tag, int i) { return std::make_shared(i); }, - [](shared_ptr_tag, invalid_base_tag, int i) { return std::make_shared(i); })) // <-- invalid alias factory + [](shared_ptr_tag, base_tag, int i) { + auto *p = new PyTF7(i); + return std::shared_ptr(p); + })) + .def(py::init([](shared_ptr_tag, + invalid_base_tag, + int i) { return std::make_shared(i); }, + [](shared_ptr_tag, invalid_base_tag, int i) { + return std::make_shared(i); + })) // <-- invalid alias factory .def("get", &TestFactory7::get) .def("has_alias", &TestFactory7::has_alias) - .def_static("get_cstats", &ConstructorStats::get, py::return_value_policy::reference) - .def_static("get_alias_cstats", &ConstructorStats::get, py::return_value_policy::reference) - ; + .def_static( + "get_cstats", &ConstructorStats::get, py::return_value_policy::reference) + .def_static( + "get_alias_cstats", &ConstructorStats::get, py::return_value_policy::reference); // test_placement_new_alternative // Class with a custom new operator but *without* a placement new operator (issue #948) @@ -331,12 +361,10 @@ TEST_SUBMODULE(factory_constructors, m) { pyNoisyAlloc.def(py::init([](int i, double) { return new NoisyAlloc(i); })); // Regular again: requires yet another preallocation ignoreOldStyleInitWarnings([&pyNoisyAlloc]() { - pyNoisyAlloc.def("__init__", [](NoisyAlloc &a, int i, std::string) { new (&a) NoisyAlloc(i); }); + pyNoisyAlloc.def( + "__init__", [](NoisyAlloc &a, int i, const std::string &) { new (&a) NoisyAlloc(i); }); }); - - - // static_assert testing (the following def's should all fail with appropriate compilation errors): #if 0 struct BadF1Base {}; diff --git a/tests/test_gil_scoped.cpp b/tests/test_gil_scoped.cpp index b6a45a5f03..b261085c88 100644 --- a/tests/test_gil_scoped.cpp +++ b/tests/test_gil_scoped.cpp @@ -35,20 +35,15 @@ TEST_SUBMODULE(gil_scoped, m) { .def("virtual_func", &VirtClass::virtual_func) .def("pure_virtual_func", &VirtClass::pure_virtual_func); - m.def("test_callback_py_obj", - [](py::object func) { func(); }); - m.def("test_callback_std_func", - [](const std::function &func) { func(); }); - m.def("test_callback_virtual_func", - [](VirtClass &virt) { virt.virtual_func(); }); - m.def("test_callback_pure_virtual_func", - [](VirtClass &virt) { virt.pure_virtual_func(); }); - m.def("test_cross_module_gil", - []() { - auto cm = py::module_::import("cross_module_gil_utils"); - auto gil_acquire = reinterpret_cast( - PyLong_AsVoidPtr(cm.attr("gil_acquire_funcaddr").ptr())); - py::gil_scoped_release gil_release; - gil_acquire(); - }); + m.def("test_callback_py_obj", [](py::object &func) { func(); }); + m.def("test_callback_std_func", [](const std::function &func) { func(); }); + m.def("test_callback_virtual_func", [](VirtClass &virt) { virt.virtual_func(); }); + m.def("test_callback_pure_virtual_func", [](VirtClass &virt) { virt.pure_virtual_func(); }); + m.def("test_cross_module_gil", []() { + auto cm = py::module_::import("cross_module_gil_utils"); + auto gil_acquire + = reinterpret_cast(PyLong_AsVoidPtr(cm.attr("gil_acquire_funcaddr").ptr())); + py::gil_scoped_release gil_release; + gil_acquire(); + }); } diff --git a/tests/test_iostream.cpp b/tests/test_iostream.cpp index f21fed5465..908788007f 100644 --- a/tests/test_iostream.cpp +++ b/tests/test_iostream.cpp @@ -17,15 +17,14 @@ #include #include - -void noisy_function(std::string msg, bool flush) { +void noisy_function(const std::string &msg, bool flush) { std::cout << msg; if (flush) std::cout << std::flush; } -void noisy_funct_dual(std::string msg, std::string emsg) { +void noisy_funct_dual(const std::string &msg, const std::string &emsg) { std::cout << msg; std::cerr << emsg; } @@ -70,12 +69,12 @@ TEST_SUBMODULE(iostream, m) { // test_evals - m.def("captured_output_default", [](std::string msg) { + m.def("captured_output_default", [](const std::string &msg) { py::scoped_ostream_redirect redir; std::cout << msg << std::flush; }); - m.def("captured_output", [](std::string msg) { + m.def("captured_output", [](const std::string &msg) { py::scoped_ostream_redirect redir(std::cout, py::module_::import("sys").attr("stdout")); std::cout << msg << std::flush; }); @@ -84,7 +83,7 @@ TEST_SUBMODULE(iostream, m) { py::call_guard(), py::arg("msg"), py::arg("flush")=true); - m.def("captured_err", [](std::string msg) { + m.def("captured_err", [](const std::string &msg) { py::scoped_ostream_redirect redir(std::cerr, py::module_::import("sys").attr("stderr")); std::cerr << msg << std::flush; }); @@ -95,15 +94,11 @@ TEST_SUBMODULE(iostream, m) { py::call_guard(), py::arg("msg"), py::arg("emsg")); - m.def("raw_output", [](std::string msg) { - std::cout << msg << std::flush; - }); + m.def("raw_output", [](const std::string &msg) { std::cout << msg << std::flush; }); - m.def("raw_err", [](std::string msg) { - std::cerr << msg << std::flush; - }); + m.def("raw_err", [](const std::string &msg) { std::cerr << msg << std::flush; }); - m.def("captured_dual", [](std::string msg, std::string emsg) { + m.def("captured_dual", [](const std::string &msg, const std::string &emsg) { py::scoped_ostream_redirect redirout(std::cout, py::module_::import("sys").attr("stdout")); py::scoped_ostream_redirect redirerr(std::cerr, py::module_::import("sys").attr("stderr")); std::cout << msg << std::flush; diff --git a/tests/test_kwargs_and_defaults.cpp b/tests/test_kwargs_and_defaults.cpp index 627a79690e..ab1c94c914 100644 --- a/tests/test_kwargs_and_defaults.cpp +++ b/tests/test_kwargs_and_defaults.cpp @@ -11,6 +11,8 @@ #include "constructor_stats.h" #include +#include + TEST_SUBMODULE(kwargs_and_defaults, m) { auto kw_func = [](int x, int y) { return "x=" + std::to_string(x) + ", y=" + std::to_string(y); }; @@ -37,18 +39,16 @@ TEST_SUBMODULE(kwargs_and_defaults, m) { m.def("args_function", [](py::args args) -> py::tuple { return std::move(args); }); - m.def("args_kwargs_function", [](py::args args, py::kwargs kwargs) { + m.def("args_kwargs_function", [](const py::args &args, const py::kwargs &kwargs) { return py::make_tuple(args, kwargs); }); // test_mixed_args_and_kwargs - m.def("mixed_plus_args", [](int i, double j, py::args args) { - return py::make_tuple(i, j, args); - }); - m.def("mixed_plus_kwargs", [](int i, double j, py::kwargs kwargs) { - return py::make_tuple(i, j, kwargs); - }); - auto mixed_plus_both = [](int i, double j, py::args args, py::kwargs kwargs) { + m.def("mixed_plus_args", + [](int i, double j, const py::args &args) { return py::make_tuple(i, j, args); }); + m.def("mixed_plus_kwargs", + [](int i, double j, const py::kwargs &kwargs) { return py::make_tuple(i, j, kwargs); }); + auto mixed_plus_both = [](int i, double j, const py::args &args, const py::kwargs &kwargs) { return py::make_tuple(i, j, args, kwargs); }; m.def("mixed_plus_args_kwargs", mixed_plus_both); @@ -65,6 +65,8 @@ TEST_SUBMODULE(kwargs_and_defaults, m) { #endif m.def("arg_refcount_h", [](py::handle h) { GC_IF_NEEDED; return h.ref_count(); }); m.def("arg_refcount_h", [](py::handle h, py::handle, py::handle) { GC_IF_NEEDED; return h.ref_count(); }); + // TODO replace the following nolints as appropiate + // NOLINTNEXTLINE(performance-unnecessary-value-param) m.def("arg_refcount_o", [](py::object o) { GC_IF_NEEDED; return o.ref_count(); }); m.def("args_refcount", [](py::args a) { GC_IF_NEEDED; @@ -74,6 +76,7 @@ TEST_SUBMODULE(kwargs_and_defaults, m) { t[i] = (int) Py_REFCNT(PyTuple_GET_ITEM(a.ptr(), static_cast(i))); return t; }); + // NOLINTNEXTLINE(performance-unnecessary-value-param) m.def("mixed_args_refcount", [](py::object o, py::args a) { GC_IF_NEEDED; py::tuple t(a.size() + 1); @@ -103,6 +106,7 @@ TEST_SUBMODULE(kwargs_and_defaults, m) { py::arg() = 3, "j"_a = 4, py::kw_only(), "k"_a = 5, "z"_a); m.def("kw_only_mixed", [](int i, int j) { return py::make_tuple(i, j); }, "i"_a, py::kw_only(), "j"_a); + // NOLINTNEXTLINE(performance-unnecessary-value-param) m.def("kw_only_plus_more", [](int i, int j, int k, py::kwargs kwargs) { return py::make_tuple(i, j, k, kwargs); }, py::arg() /* positional */, py::arg("j") = -1 /* both */, py::kw_only(), py::arg("k") /* kw-only */); @@ -137,6 +141,8 @@ TEST_SUBMODULE(kwargs_and_defaults, m) { // Make sure a class (not an instance) can be used as a default argument. // The return value doesn't matter, only that the module is importable. - m.def("class_default_argument", [](py::object a) { return py::repr(a); }, + m.def( + "class_default_argument", + [](py::object a) { return py::repr(std::move(a)); }, "a"_a = py::module_::import("decimal").attr("Decimal")); } diff --git a/tests/test_local_bindings.cpp b/tests/test_local_bindings.cpp index c61e388833..2ac77b143b 100644 --- a/tests/test_local_bindings.cpp +++ b/tests/test_local_bindings.cpp @@ -86,6 +86,8 @@ TEST_SUBMODULE(local_bindings, m) { m.def("return_self", [](LocalVec *v) { return v; }); m.def("return_copy", [](const LocalVec &v) { return LocalVec(v); }); + // Reformatting this class broke pygrep checks + // NOLINTNEXTLINE class Cat : public pets::Pet { public: Cat(std::string name) : Pet(name) {}; }; py::class_(m, "Pet", py::module_local()) .def("get_name", &pets::Pet::name); diff --git a/tests/test_methods_and_attributes.cpp b/tests/test_methods_and_attributes.cpp index 1f855ef354..3eb22b4a37 100644 --- a/tests/test_methods_and_attributes.cpp +++ b/tests/test_methods_and_attributes.cpp @@ -22,14 +22,18 @@ class ExampleMandA { ExampleMandA(int value) : value(value) { print_created(this, value); } ExampleMandA(const ExampleMandA &e) : value(e.value) { print_copy_created(this); } ExampleMandA(std::string&&) {} - ExampleMandA(ExampleMandA &&e) : value(e.value) { print_move_created(this); } + ExampleMandA(ExampleMandA &&e) noexcept : value(e.value) { print_move_created(this); } ~ExampleMandA() { print_destroyed(this); } std::string toString() const { return "ExampleMandA[value=" + std::to_string(value) + "]"; } void operator=(const ExampleMandA &e) { print_copy_assigned(this); value = e.value; } - void operator=(ExampleMandA &&e) { print_move_assigned(this); value = e.value; } + void operator=(ExampleMandA &&e) noexcept { + print_move_assigned(this); + value = e.value; + } + // NOLINTNEXTLINE(performance-unnecessary-value-param) void add1(ExampleMandA other) { value += other.value; } // passing by value void add2(ExampleMandA &other) { value += other.value; } // passing by reference void add3(const ExampleMandA &other) { value += other.value; } // passing by const reference @@ -112,7 +116,7 @@ int none1(const NoneTester &obj) { return obj.answer; } int none2(NoneTester *obj) { return obj ? obj->answer : -1; } int none3(std::shared_ptr &obj) { return obj ? obj->answer : -1; } int none4(std::shared_ptr *obj) { return obj && *obj ? (*obj)->answer : -1; } -int none5(std::shared_ptr obj) { return obj ? obj->answer : -1; } +int none5(const std::shared_ptr &obj) { return obj ? obj->answer : -1; } struct StrIssue { int val = -1; @@ -226,36 +230,41 @@ TEST_SUBMODULE(methods_and_attributes, m) { .def(py::init<>()) .def_readonly("def_readonly", &TestProperties::value) .def_readwrite("def_readwrite", &TestProperties::value) - .def_property("def_writeonly", nullptr, - [](TestProperties& s,int v) { s.value = v; } ) + .def_property("def_writeonly", nullptr, [](TestProperties &s, int v) { s.value = v; }) .def_property("def_property_writeonly", nullptr, &TestProperties::set) .def_property_readonly("def_property_readonly", &TestProperties::get) .def_property("def_property", &TestProperties::get, &TestProperties::set) .def_property("def_property_impossible", nullptr, nullptr) .def_readonly_static("def_readonly_static", &TestProperties::static_value) .def_readwrite_static("def_readwrite_static", &TestProperties::static_value) - .def_property_static("def_writeonly_static", nullptr, - [](py::object, int v) { TestProperties::static_value = v; }) - .def_property_readonly_static("def_property_readonly_static", - [](py::object) { return TestProperties::static_get(); }) - .def_property_static("def_property_writeonly_static", nullptr, - [](py::object, int v) { return TestProperties::static_set(v); }) - .def_property_static("def_property_static", - [](py::object) { return TestProperties::static_get(); }, - [](py::object, int v) { TestProperties::static_set(v); }) - .def_property_static("static_cls", - [](py::object cls) { return cls; }, - [](py::object cls, py::function f) { f(cls); }); + .def_property_static("def_writeonly_static", + nullptr, + [](const py::object &, int v) { TestProperties::static_value = v; }) + .def_property_readonly_static( + "def_property_readonly_static", + [](const py::object &) { return TestProperties::static_get(); }) + .def_property_static( + "def_property_writeonly_static", + nullptr, + [](const py::object &, int v) { return TestProperties::static_set(v); }) + .def_property_static( + "def_property_static", + [](const py::object &) { return TestProperties::static_get(); }, + [](const py::object &, int v) { TestProperties::static_set(v); }) + .def_property_static( + "static_cls", + [](py::object cls) { return cls; }, + [](const py::object &cls, const py::function &f) { f(cls); }); py::class_(m, "TestPropertiesOverride") .def(py::init<>()) .def_readonly("def_readonly", &TestPropertiesOverride::value) .def_readonly_static("def_readonly_static", &TestPropertiesOverride::static_value); - auto static_get1 = [](py::object) -> const UserType & { return TestPropRVP::sv1; }; - auto static_get2 = [](py::object) -> const UserType & { return TestPropRVP::sv2; }; - auto static_set1 = [](py::object, int v) { TestPropRVP::sv1.set(v); }; - auto static_set2 = [](py::object, int v) { TestPropRVP::sv2.set(v); }; + auto static_get1 = [](const py::object &) -> const UserType & { return TestPropRVP::sv1; }; + auto static_get2 = [](const py::object &) -> const UserType & { return TestPropRVP::sv2; }; + auto static_set1 = [](const py::object &, int v) { TestPropRVP::sv1.set(v); }; + auto static_set2 = [](const py::object &, int v) { TestPropRVP::sv2.set(v); }; auto rvp_copy = py::return_value_policy::copy; // test_property_return_value_policies @@ -266,24 +275,30 @@ TEST_SUBMODULE(methods_and_attributes, m) { .def_property_readonly("ro_func", py::cpp_function(&TestPropRVP::get2, rvp_copy)) .def_property("rw_ref", &TestPropRVP::get1, &TestPropRVP::set1) .def_property("rw_copy", &TestPropRVP::get2, &TestPropRVP::set2, rvp_copy) - .def_property("rw_func", py::cpp_function(&TestPropRVP::get2, rvp_copy), &TestPropRVP::set2) + .def_property( + "rw_func", py::cpp_function(&TestPropRVP::get2, rvp_copy), &TestPropRVP::set2) .def_property_readonly_static("static_ro_ref", static_get1) .def_property_readonly_static("static_ro_copy", static_get2, rvp_copy) .def_property_readonly_static("static_ro_func", py::cpp_function(static_get2, rvp_copy)) .def_property_static("static_rw_ref", static_get1, static_set1) .def_property_static("static_rw_copy", static_get2, static_set2, rvp_copy) - .def_property_static("static_rw_func", py::cpp_function(static_get2, rvp_copy), static_set2) + .def_property_static( + "static_rw_func", py::cpp_function(static_get2, rvp_copy), static_set2) // test_property_rvalue_policy .def_property_readonly("rvalue", &TestPropRVP::get_rvalue) + // NOLINTNEXTLINE(performance-unnecessary-value-param) .def_property_readonly_static("static_rvalue", [](py::object) { return UserType(1); }); // test_metaclass_override struct MetaclassOverride { }; py::class_(m, "MetaclassOverride", py::metaclass((PyObject *) &PyType_Type)) + // NOLINTNEXTLINE(performance-unnecessary-value-param) .def_property_readonly_static("readonly", [](py::object) { return 1; }); // test_overload_ordering + // NOLINTNEXTLINE(performance-unnecessary-value-param) m.def("overload_order", [](std::string) { return 1; }); + // NOLINTNEXTLINE(performance-unnecessary-value-param) m.def("overload_order", [](std::string) { return 2; }); m.def("overload_order", [](int) { return 3; }); m.def("overload_order", [](int) { return 4; }, py::prepend{}); diff --git a/tests/test_multiple_inheritance.cpp b/tests/test_multiple_inheritance.cpp index daecb9564f..f4acf7861e 100644 --- a/tests/test_multiple_inheritance.cpp +++ b/tests/test_multiple_inheritance.cpp @@ -141,6 +141,7 @@ TEST_SUBMODULE(multiple_inheritance, m) { .def(py::init()); m.def("bar_base2a", [](Base2a *b) { return b->bar(); }); + // NOLINTNEXTLINE(performance-unnecessary-value-param) m.def("bar_base2a_sharedptr", [](std::shared_ptr b) { return b->bar(); }); // test_mi_unaligned_base diff --git a/tests/test_numpy_array.cpp b/tests/test_numpy_array.cpp index 204ea8367e..a6c7ae8d74 100644 --- a/tests/test_numpy_array.cpp +++ b/tests/test_numpy_array.cpp @@ -13,6 +13,7 @@ #include #include +#include // Size / dtype checks. struct DtypeCheck { @@ -192,7 +193,7 @@ TEST_SUBMODULE(numpy_array, sm) { sm.def("scalar_int", []() { return py::array(py::dtype("i"), {}, {}, &data_i); }); // test_wrap - sm.def("wrap", [](py::array a) { + sm.def("wrap", [](const py::array &a) { return py::array( a.dtype(), {a.shape(), a.shape() + a.ndim()}, @@ -222,8 +223,10 @@ TEST_SUBMODULE(numpy_array, sm) { // test_isinstance sm.def("isinstance_untyped", [](py::object yes, py::object no) { - return py::isinstance(yes) && !py::isinstance(no); + return py::isinstance(std::move(yes)) + && !py::isinstance(std::move(no)); }); + // NOLINTNEXTLINE(performance-unnecessary-value-param) sm.def("isinstance_typed", [](py::object o) { return py::isinstance>(o) && !py::isinstance>(o); }); @@ -236,7 +239,7 @@ TEST_SUBMODULE(numpy_array, sm) { "array_t"_a=py::array_t() ); }); - sm.def("converting_constructors", [](py::object o) { + sm.def("converting_constructors", [](const py::object &o) { return py::dict( "array"_a=py::array(o), "array_t"_a=py::array_t(o), @@ -245,39 +248,59 @@ TEST_SUBMODULE(numpy_array, sm) { }); // test_overload_resolution + // NOLINTNEXTLINE(performance-unnecessary-value-param) sm.def("overloaded", [](py::array_t) { return "double"; }); + // NOLINTNEXTLINE(performance-unnecessary-value-param) sm.def("overloaded", [](py::array_t) { return "float"; }); + // NOLINTNEXTLINE(performance-unnecessary-value-param) sm.def("overloaded", [](py::array_t) { return "int"; }); + // NOLINTNEXTLINE(performance-unnecessary-value-param) sm.def("overloaded", [](py::array_t) { return "unsigned short"; }); + // NOLINTNEXTLINE(performance-unnecessary-value-param) sm.def("overloaded", [](py::array_t) { return "long long"; }); + // NOLINTNEXTLINE(performance-unnecessary-value-param) sm.def("overloaded", [](py::array_t>) { return "double complex"; }); + // NOLINTNEXTLINE(performance-unnecessary-value-param) sm.def("overloaded", [](py::array_t>) { return "float complex"; }); + // NOLINTNEXTLINE(performance-unnecessary-value-param) sm.def("overloaded2", [](py::array_t>) { return "double complex"; }); + // NOLINTNEXTLINE(performance-unnecessary-value-param) sm.def("overloaded2", [](py::array_t) { return "double"; }); + // NOLINTNEXTLINE(performance-unnecessary-value-param) sm.def("overloaded2", [](py::array_t>) { return "float complex"; }); + // NOLINTNEXTLINE(performance-unnecessary-value-param) sm.def("overloaded2", [](py::array_t) { return "float"; }); // [workaround(intel)] ICC 20/21 breaks with py::arg().stuff, using py::arg{}.stuff works. // Only accept the exact types: + // NOLINTNEXTLINE(performance-unnecessary-value-param) sm.def("overloaded3", [](py::array_t) { return "int"; }, py::arg{}.noconvert()); + // NOLINTNEXTLINE(performance-unnecessary-value-param) sm.def("overloaded3", [](py::array_t) { return "double"; }, py::arg{}.noconvert()); // Make sure we don't do unsafe coercion (e.g. float to int) when not using forcecast, but // rather that float gets converted via the safe (conversion to double) overload: + // NOLINTNEXTLINE(performance-unnecessary-value-param) sm.def("overloaded4", [](py::array_t) { return "long long"; }); + // NOLINTNEXTLINE(performance-unnecessary-value-param) sm.def("overloaded4", [](py::array_t) { return "double"; }); // But we do allow conversion to int if forcecast is enabled (but only if no overload matches // without conversion) + // NOLINTNEXTLINE(performance-unnecessary-value-param) sm.def("overloaded5", [](py::array_t) { return "unsigned int"; }); + // NOLINTNEXTLINE(performance-unnecessary-value-param) sm.def("overloaded5", [](py::array_t) { return "double"; }); // test_greedy_string_overload // Issue 685: ndarray shouldn't go to std::string overload + // NOLINTNEXTLINE(performance-unnecessary-value-param) sm.def("issue685", [](std::string) { return "string"; }); + // NOLINTNEXTLINE(performance-unnecessary-value-param) sm.def("issue685", [](py::array) { return "array"; }); + // NOLINTNEXTLINE(performance-unnecessary-value-param) sm.def("issue685", [](py::object) { return "other"; }); // test_array_unchecked_fixed_dims @@ -306,7 +329,7 @@ TEST_SUBMODULE(numpy_array, sm) { r(i, j, k) = start++; return a; }); - sm.def("proxy_squared_L2_norm", [](py::array_t a) { + sm.def("proxy_squared_L2_norm", [](const py::array_t &a) { auto r = a.unchecked<1>(); double sumsq = 0; for (py::ssize_t i = 0; i < r.shape(0); i++) @@ -396,51 +419,78 @@ TEST_SUBMODULE(numpy_array, sm) { return a; }); - sm.def("index_using_ellipsis", [](py::array a) { - return a[py::make_tuple(0, py::ellipsis(), 0)]; - }); + sm.def("index_using_ellipsis", + [](const py::array &a) { return a[py::make_tuple(0, py::ellipsis(), 0)]; }); // test_argument_conversions - sm.def("accept_double", - [](py::array_t) {}, - py::arg("a")); - sm.def("accept_double_forcecast", - [](py::array_t) {}, - py::arg("a")); - sm.def("accept_double_c_style", - [](py::array_t) {}, - py::arg("a")); - sm.def("accept_double_c_style_forcecast", - [](py::array_t) {}, - py::arg("a")); - sm.def("accept_double_f_style", - [](py::array_t) {}, - py::arg("a")); - sm.def("accept_double_f_style_forcecast", - [](py::array_t) {}, - py::arg("a")); - sm.def("accept_double_noconvert", - [](py::array_t) {}, - "a"_a.noconvert()); - sm.def("accept_double_forcecast_noconvert", - [](py::array_t) {}, - "a"_a.noconvert()); - sm.def("accept_double_c_style_noconvert", - [](py::array_t) {}, - "a"_a.noconvert()); - sm.def("accept_double_c_style_forcecast_noconvert", - [](py::array_t) {}, - "a"_a.noconvert()); - sm.def("accept_double_f_style_noconvert", - [](py::array_t) {}, - "a"_a.noconvert()); - sm.def("accept_double_f_style_forcecast_noconvert", - [](py::array_t) {}, - "a"_a.noconvert()); + sm.def( + "accept_double", + // NOLINTNEXTLINE(performance-unnecessary-value-param) + [](py::array_t) {}, + py::arg("a")); + sm.def( + "accept_double_forcecast", + // NOLINTNEXTLINE(performance-unnecessary-value-param) + [](py::array_t) {}, + py::arg("a")); + sm.def( + "accept_double_c_style", + // NOLINTNEXTLINE(performance-unnecessary-value-param) + [](py::array_t) {}, + py::arg("a")); + sm.def( + "accept_double_c_style_forcecast", + // NOLINTNEXTLINE(performance-unnecessary-value-param) + [](py::array_t) {}, + py::arg("a")); + sm.def( + "accept_double_f_style", + // NOLINTNEXTLINE(performance-unnecessary-value-param) + [](py::array_t) {}, + py::arg("a")); + sm.def( + "accept_double_f_style_forcecast", + // NOLINTNEXTLINE(performance-unnecessary-value-param) + [](py::array_t) {}, + py::arg("a")); + sm.def( + "accept_double_noconvert", + // NOLINTNEXTLINE(performance-unnecessary-value-param) + [](py::array_t) {}, + "a"_a.noconvert()); + sm.def( + "accept_double_forcecast_noconvert", + // NOLINTNEXTLINE(performance-unnecessary-value-param) + [](py::array_t) {}, + "a"_a.noconvert()); + sm.def( + "accept_double_c_style_noconvert", + // NOLINTNEXTLINE(performance-unnecessary-value-param) + [](py::array_t) {}, + "a"_a.noconvert()); + sm.def( + "accept_double_c_style_forcecast_noconvert", + // NOLINTNEXTLINE(performance-unnecessary-value-param) + [](py::array_t) {}, + "a"_a.noconvert()); + sm.def( + "accept_double_f_style_noconvert", + // NOLINTNEXTLINE(performance-unnecessary-value-param) + [](py::array_t) {}, + "a"_a.noconvert()); + sm.def( + "accept_double_f_style_forcecast_noconvert", + // NOLINTNEXTLINE(performance-unnecessary-value-param) + [](py::array_t) {}, + "a"_a.noconvert()); // Check that types returns correct npy format descriptor + // NOLINTNEXTLINE(performance-unnecessary-value-param) sm.def("test_fmt_desc_float", [](py::array_t) {}); + // NOLINTNEXTLINE(performance-unnecessary-value-param) sm.def("test_fmt_desc_double", [](py::array_t) {}); + // NOLINTNEXTLINE(performance-unnecessary-value-param) sm.def("test_fmt_desc_const_float", [](py::array_t) {}); + // NOLINTNEXTLINE(performance-unnecessary-value-param) sm.def("test_fmt_desc_const_double", [](py::array_t) {}); } diff --git a/tests/test_numpy_dtypes.cpp b/tests/test_numpy_dtypes.cpp index 9dece73ec3..edf9cf200f 100644 --- a/tests/test_numpy_dtypes.cpp +++ b/tests/test_numpy_dtypes.cpp @@ -266,10 +266,11 @@ TEST_SUBMODULE(numpy_dtypes, m) { .def_readwrite("uint_", &SimpleStruct::uint_) .def_readwrite("float_", &SimpleStruct::float_) .def_readwrite("ldbl_", &SimpleStruct::ldbl_) - .def("astuple", [](const SimpleStruct& self) { - return py::make_tuple(self.bool_, self.uint_, self.float_, self.ldbl_); - }) - .def_static("fromtuple", [](const py::tuple tup) { + .def("astuple", + [](const SimpleStruct &self) { + return py::make_tuple(self.bool_, self.uint_, self.float_, self.ldbl_); + }) + .def_static("fromtuple", [](const py::tuple &tup) { if (py::len(tup) != 4) { throw py::cast_error("Invalid size"); } diff --git a/tests/test_numpy_vectorize.cpp b/tests/test_numpy_vectorize.cpp index 8ed9b27e1b..77e281bbdc 100644 --- a/tests/test_numpy_vectorize.cpp +++ b/tests/test_numpy_vectorize.cpp @@ -11,6 +11,8 @@ #include "pybind11_tests.h" #include +#include + double my_func(int x, float y, double z) { py::print("my_func(x:int={}, y:float={:.0f}, z:float={:.0f})"_s.format(x, y, z)); return (float) x*y*z; @@ -25,11 +27,10 @@ TEST_SUBMODULE(numpy_vectorize, m) { m.def("vectorized_func", py::vectorize(my_func)); // Vectorize a lambda function with a capture object (e.g. to exclude some arguments from the vectorization) - m.def("vectorized_func2", - [](py::array_t x, py::array_t y, float z) { - return py::vectorize([z](int x, float y) { return my_func(x, y, z); })(x, y); - } - ); + m.def("vectorized_func2", [](py::array_t x, py::array_t y, float z) { + return py::vectorize([z](int x, float y) { return my_func(x, y, z); })(std::move(x), + std::move(y)); + }); // Vectorize a complex-valued function m.def("vectorized_func3", py::vectorize( @@ -38,8 +39,12 @@ TEST_SUBMODULE(numpy_vectorize, m) { // test_type_selection // NumPy function which only accepts specific data types + // Alot of these no lints could be replaced with const refs, and probably should at some point. + // NOLINTNEXTLINE(performance-unnecessary-value-param) m.def("selective_func", [](py::array_t) { return "Int branch taken."; }); + // NOLINTNEXTLINE(performance-unnecessary-value-param) m.def("selective_func", [](py::array_t) { return "Float branch taken."; }); + // NOLINTNEXTLINE(performance-unnecessary-value-param) m.def("selective_func", [](py::array_t, py::array::c_style>) { return "Complex float branch taken."; }); @@ -53,11 +58,16 @@ TEST_SUBMODULE(numpy_vectorize, m) { py::class_(m, "NonPODClass") .def(py::init()) .def_readwrite("value", &NonPODClass::value); - m.def("vec_passthrough", py::vectorize( - [](double *a, double b, py::array_t c, const int &d, int &e, NonPODClass f, const double g) { - return *a + b + c.at(0) + d + e + f.value + g; - } - )); + m.def("vec_passthrough", + py::vectorize([](double *a, + double b, + // Changing this broke things + // NOLINTNEXTLINE(performance-unnecessary-value-param) + py::array_t c, + const int &d, + int &e, + NonPODClass f, + const double g) { return *a + b + c.at(0) + d + e + f.value + g; })); // test_method_vectorization struct VectorizeTestClass { @@ -78,16 +88,20 @@ TEST_SUBMODULE(numpy_vectorize, m) { .value("f_trivial", py::detail::broadcast_trivial::f_trivial) .value("c_trivial", py::detail::broadcast_trivial::c_trivial) .value("non_trivial", py::detail::broadcast_trivial::non_trivial); - m.def("vectorized_is_trivial", []( - py::array_t arg1, - py::array_t arg2, - py::array_t arg3 - ) { - py::ssize_t ndim; - std::vector shape; - std::array buffers {{ arg1.request(), arg2.request(), arg3.request() }}; - return py::detail::broadcast(buffers, ndim, shape); - }); + m.def("vectorized_is_trivial", + []( + // NOLINTNEXTLINE(performance-unnecessary-value-param) + py::array_t arg1, + // NOLINTNEXTLINE(performance-unnecessary-value-param) + py::array_t arg2, + // NOLINTNEXTLINE(performance-unnecessary-value-param) + py::array_t arg3) { + py::ssize_t ndim; + std::vector shape; + std::array buffers{ + {arg1.request(), arg2.request(), arg3.request()}}; + return py::detail::broadcast(buffers, ndim, shape); + }); m.def("add_to", py::vectorize([](NonPODClass& x, int a) { x.value += a; })); } diff --git a/tests/test_opaque_types.cpp b/tests/test_opaque_types.cpp index 5a23431635..804de6d4ff 100644 --- a/tests/test_opaque_types.cpp +++ b/tests/test_opaque_types.cpp @@ -44,7 +44,7 @@ TEST_SUBMODULE(opaque_types, m) { m.def("print_opaque_list", [](const StringList &l) { std::string ret = "Opaque list: ["; bool first = true; - for (auto entry : l) { + for (const auto &entry : l) { if (!first) ret += ", "; ret += entry; diff --git a/tests/test_operator_overloading.cpp b/tests/test_operator_overloading.cpp index eb3167dc08..0b6c496cf2 100644 --- a/tests/test_operator_overloading.cpp +++ b/tests/test_operator_overloading.cpp @@ -16,9 +16,18 @@ class Vector2 { public: Vector2(float x, float y) : x(x), y(y) { print_created(this, toString()); } Vector2(const Vector2 &v) : x(v.x), y(v.y) { print_copy_created(this); } - Vector2(Vector2 &&v) : x(v.x), y(v.y) { print_move_created(this); v.x = v.y = 0; } + Vector2(Vector2 &&v) noexcept : x(v.x), y(v.y) { + print_move_created(this); + v.x = v.y = 0; + } Vector2 &operator=(const Vector2 &v) { x = v.x; y = v.y; print_copy_assigned(this); return *this; } - Vector2 &operator=(Vector2 &&v) { x = v.x; y = v.y; v.x = v.y = 0; print_move_assigned(this); return *this; } + Vector2 &operator=(Vector2 &&v) noexcept { + x = v.x; + y = v.y; + v.x = v.y = 0; + print_move_assigned(this); + return *this; + } ~Vector2() { print_destroyed(this); } std::string toString() const { return "[" + std::to_string(x) + ", " + std::to_string(y) + "]"; } diff --git a/tests/test_pickling.cpp b/tests/test_pickling.cpp index 1a48595af6..c2cee6c3c7 100644 --- a/tests/test_pickling.cpp +++ b/tests/test_pickling.cpp @@ -46,17 +46,16 @@ TEST_SUBMODULE(pickling, m) { return py::make_tuple(p.value(), p.extra1(), p.extra2()); }); ignoreOldStyleInitWarnings([&pyPickleable]() { - pyPickleable - .def("__setstate__", [](Pickleable &p, py::tuple t) { - if (t.size() != 3) - throw std::runtime_error("Invalid state!"); - /* Invoke the constructor (need to use in-place version) */ - new (&p) Pickleable(t[0].cast()); - - /* Assign any additional state */ - p.setExtra1(t[1].cast()); - p.setExtra2(t[2].cast()); - }); + pyPickleable.def("__setstate__", [](Pickleable &p, const py::tuple &t) { + if (t.size() != 3) + throw std::runtime_error("Invalid state!"); + /* Invoke the constructor (need to use in-place version) */ + new (&p) Pickleable(t[0].cast()); + + /* Assign any additional state */ + p.setExtra1(t[1].cast()); + p.setExtra2(t[2].cast()); + }); }); py::class_(m, "PickleableNew") @@ -65,7 +64,7 @@ TEST_SUBMODULE(pickling, m) { [](const PickleableNew &p) { return py::make_tuple(p.value(), p.extra1(), p.extra2()); }, - [](py::tuple t) { + [](const py::tuple &t) { if (t.size() != 3) throw std::runtime_error("Invalid state!"); auto p = PickleableNew(t[0].cast()); @@ -73,8 +72,7 @@ TEST_SUBMODULE(pickling, m) { p.setExtra1(t[1].cast()); p.setExtra2(t[2].cast()); return p; - } - )); + })); #if !defined(PYPY_VERSION) // test_roundtrip_with_dict @@ -92,35 +90,33 @@ TEST_SUBMODULE(pickling, m) { }; py::class_ pyPickleableWithDict(m, "PickleableWithDict", py::dynamic_attr()); - pyPickleableWithDict - .def(py::init()) + pyPickleableWithDict.def(py::init()) .def_readwrite("value", &PickleableWithDict::value) .def_readwrite("extra", &PickleableWithDict::extra) - .def("__getstate__", [](py::object self) { + .def("__getstate__", [](const py::object &self) { /* Also include __dict__ in state */ return py::make_tuple(self.attr("value"), self.attr("extra"), self.attr("__dict__")); }); ignoreOldStyleInitWarnings([&pyPickleableWithDict]() { - pyPickleableWithDict - .def("__setstate__", [](py::object self, py::tuple t) { - if (t.size() != 3) - throw std::runtime_error("Invalid state!"); - /* Cast and construct */ - auto& p = self.cast(); - new (&p) PickleableWithDict(t[0].cast()); - - /* Assign C++ state */ - p.extra = t[1].cast(); - - /* Assign Python state */ - self.attr("__dict__") = t[2]; - }); + pyPickleableWithDict.def("__setstate__", [](const py::object &self, const py::tuple &t) { + if (t.size() != 3) + throw std::runtime_error("Invalid state!"); + /* Cast and construct */ + auto &p = self.cast(); + new (&p) PickleableWithDict(t[0].cast()); + + /* Assign C++ state */ + p.extra = t[1].cast(); + + /* Assign Python state */ + self.attr("__dict__") = t[2]; + }); }); py::class_(m, "PickleableWithDictNew") .def(py::init()) .def(py::pickle( - [](py::object self) { + [](const py::object &self) { return py::make_tuple(self.attr("value"), self.attr("extra"), self.attr("__dict__")); }, [](const py::tuple &t) { @@ -132,7 +128,6 @@ TEST_SUBMODULE(pickling, m) { auto py_state = t[2].cast(); return std::make_pair(cpp_state, py_state); - } - )); + })); #endif } diff --git a/tests/test_pytypes.cpp b/tests/test_pytypes.cpp index 6921796aa0..9e0ebc206f 100644 --- a/tests/test_pytypes.cpp +++ b/tests/test_pytypes.cpp @@ -7,6 +7,8 @@ BSD-style license that can be found in the LICENSE file. */ +#include + #include "pybind11_tests.h" @@ -27,16 +29,14 @@ TEST_SUBMODULE(pytypes, m) { list.insert(2, "inserted-2"); return list; }); - m.def("print_list", [](py::list list) { + m.def("print_list", [](const py::list &list) { int index = 0; for (auto item : list) py::print("list item {}: {}"_s.format(index++, item)); }); // test_none m.def("get_none", []{return py::none();}); - m.def("print_none", [](py::none none) { - py::print("none: {}"_s.format(none)); - }); + m.def("print_none", [](const py::none &none) { py::print("none: {}"_s.format(none)); }); // test_set m.def("get_set", []() { @@ -46,20 +46,17 @@ TEST_SUBMODULE(pytypes, m) { set.add(std::string("key3")); return set; }); - m.def("print_set", [](py::set set) { + m.def("print_set", [](const py::set &set) { for (auto item : set) py::print("key:", item); }); - m.def("set_contains", [](py::set set, py::object key) { - return set.contains(key); - }); - m.def("set_contains", [](py::set set, const char* key) { - return set.contains(key); - }); + m.def("set_contains", + [](const py::set &set, const py::object &key) { return set.contains(key); }); + m.def("set_contains", [](const py::set &set, const char *key) { return set.contains(key); }); // test_dict m.def("get_dict", []() { return py::dict("key"_a="value"); }); - m.def("print_dict", [](py::dict dict) { + m.def("print_dict", [](const py::dict &dict) { for (auto item : dict) py::print("key: {}, value={}"_s.format(item.first, item.second)); }); @@ -68,12 +65,10 @@ TEST_SUBMODULE(pytypes, m) { auto d2 = py::dict("z"_a=3, **d1); return d2; }); - m.def("dict_contains", [](py::dict dict, py::object val) { - return dict.contains(val); - }); - m.def("dict_contains", [](py::dict dict, const char* val) { - return dict.contains(val); - }); + m.def("dict_contains", + [](const py::dict &dict, py::object val) { return dict.contains(val); }); + m.def("dict_contains", + [](const py::dict &dict, const char *val) { return dict.contains(val); }); // test_str m.def("str_from_string", []() { return py::str(std::string("baz")); }); @@ -137,7 +132,7 @@ TEST_SUBMODULE(pytypes, m) { }); // test_accessors - m.def("accessor_api", [](py::object o) { + m.def("accessor_api", [](const py::object &o) { auto d = py::dict(); d["basic_attr"] = o.attr("basic_attr"); @@ -178,7 +173,7 @@ TEST_SUBMODULE(pytypes, m) { return d; }); - m.def("tuple_accessor", [](py::tuple existing_t) { + m.def("tuple_accessor", [](const py::tuple &existing_t) { try { existing_t[0] = 1; } catch (const py::error_already_set &) { @@ -226,7 +221,7 @@ TEST_SUBMODULE(pytypes, m) { ); }); - m.def("converting_constructors", [](py::dict d) { + m.def("converting_constructors", [](const py::dict &d) { return py::dict( "bytes"_a=py::bytes(d["bytes"]), "bytearray"_a=py::bytearray(d["bytearray"]), @@ -242,6 +237,7 @@ TEST_SUBMODULE(pytypes, m) { ); }); + // NOLINTNEXTLINE(performance-unnecessary-value-param) m.def("cast_functions", [](py::dict d) { // When converting between Python types, obj.cast() should be the same as T(obj) return py::dict( @@ -259,8 +255,10 @@ TEST_SUBMODULE(pytypes, m) { ); }); + // NOLINTNEXTLINE(performance-unnecessary-value-param) m.def("convert_to_pybind11_str", [](py::object o) { return py::str(o); }); + // NOLINTNEXTLINE(performance-unnecessary-value-param) m.def("nonconverting_constructor", [](std::string type, py::object value, bool move) -> py::object { if (type == "bytes") { return move ? py::bytes(std::move(value)) : py::bytes(value); @@ -333,8 +331,9 @@ TEST_SUBMODULE(pytypes, m) { m.def("print_failure", []() { py::print(42, UnregisteredType()); }); - m.def("hash_function", [](py::object obj) { return py::hash(obj); }); + m.def("hash_function", [](py::object obj) { return py::hash(std::move(obj)); }); + // NOLINTNEXTLINE(performance-unnecessary-value-param) m.def("test_number_protocol", [](py::object a, py::object b) { py::list l; l.append(a.equal(b)); @@ -355,6 +354,7 @@ TEST_SUBMODULE(pytypes, m) { return l; }); + // NOLINTNEXTLINE(performance-unnecessary-value-param) m.def("test_list_slicing", [](py::list a) { return a[py::slice(0, -1, 2)]; }); @@ -369,10 +369,12 @@ TEST_SUBMODULE(pytypes, m) { return is_this_none; }); + // NOLINTNEXTLINE(performance-unnecessary-value-param) m.def("test_memoryview_object", [](py::buffer b) { return py::memoryview(b); }); + // NOLINTNEXTLINE(performance-unnecessary-value-param) m.def("test_memoryview_buffer_info", [](py::buffer b) { return py::memoryview(b.request()); }); @@ -425,20 +427,22 @@ TEST_SUBMODULE(pytypes, m) { m.attr("PYBIND11_STR_LEGACY_PERMISSIVE") = true; #endif - m.def("isinstance_pybind11_bytes", [](py::object o) { return py::isinstance(o); }); - m.def("isinstance_pybind11_str", [](py::object o) { return py::isinstance(o); }); + m.def("isinstance_pybind11_bytes", + [](py::object o) { return py::isinstance(std::move(o)); }); + m.def("isinstance_pybind11_str", + [](py::object o) { return py::isinstance(std::move(o)); }); - m.def("pass_to_pybind11_bytes", [](py::bytes b) { return py::len(b); }); - m.def("pass_to_pybind11_str", [](py::str s) { return py::len(s); }); + m.def("pass_to_pybind11_bytes", [](py::bytes b) { return py::len(std::move(b)); }); + m.def("pass_to_pybind11_str", [](py::str s) { return py::len(std::move(s)); }); + // NOLINTNEXTLINE(performance-unnecessary-value-param) m.def("pass_to_std_string", [](std::string s) { return s.size(); }); // test_weakref m.def("weakref_from_handle", [](py::handle h) { return py::weakref(h); }); m.def("weakref_from_handle_and_function", - [](py::handle h, py::function f) { return py::weakref(h, f); }); - m.def("weakref_from_object", - [](py::object o) { return py::weakref(o); }); + [](py::handle h, py::function f) { return py::weakref(h, std::move(f)); }); + m.def("weakref_from_object", [](const py::object &o) { return py::weakref(o); }); m.def("weakref_from_object_and_function", - [](py::object o, py::function f) { return py::weakref(o, f); }); + [](py::object o, py::function f) { return py::weakref(std::move(o), std::move(f)); }); } diff --git a/tests/test_sequences_and_iterators.cpp b/tests/test_sequences_and_iterators.cpp index d318052a48..c39798229f 100644 --- a/tests/test_sequences_and_iterators.cpp +++ b/tests/test_sequences_and_iterators.cpp @@ -14,6 +14,7 @@ #include #include +#include template class NonZeroIterator { @@ -80,18 +81,17 @@ TEST_SUBMODULE(sequences_and_iterators, m) { int start,stop,step; int size; }; - py::class_(m,"Sliceable") + py::class_(m, "Sliceable") .def(py::init()) - .def("__getitem__",[](const Sliceable &s, py::slice slice) { - py::ssize_t start, stop, step, slicelength; - if (!slice.compute(s.size, &start, &stop, &step, &slicelength)) - throw py::error_already_set(); - int istart = static_cast(start); - int istop = static_cast(stop); - int istep = static_cast(step); - return std::make_tuple(istart,istop,istep); - }) - ; + .def("__getitem__", [](const Sliceable &s, const py::slice &slice) { + py::ssize_t start, stop, step, slicelength; + if (!slice.compute(s.size, &start, &stop, &step, &slicelength)) + throw py::error_already_set(); + int istart = static_cast(start); + int istop = static_cast(stop); + int istep = static_cast(step); + return std::make_tuple(istart, istop, istep); + }); // test_sequence class Sequence { @@ -111,7 +111,7 @@ TEST_SUBMODULE(sequences_and_iterators, m) { m_data = new float[m_size]; memcpy(m_data, s.m_data, sizeof(float)*m_size); } - Sequence(Sequence &&s) : m_size(s.m_size), m_data(s.m_data) { + Sequence(Sequence &&s) noexcept : m_size(s.m_size), m_data(s.m_data) { print_move_created(this); s.m_size = 0; s.m_data = nullptr; @@ -130,7 +130,7 @@ TEST_SUBMODULE(sequences_and_iterators, m) { return *this; } - Sequence &operator=(Sequence &&s) { + Sequence &operator=(Sequence &&s) noexcept { if (&s != this) { delete[] m_data; m_size = s.m_size; @@ -179,43 +179,54 @@ TEST_SUBMODULE(sequences_and_iterators, m) { }; py::class_(m, "Sequence") .def(py::init()) - .def(py::init&>()) + .def(py::init &>()) /// Bare bones interface - .def("__getitem__", [](const Sequence &s, size_t i) { - if (i >= s.size()) throw py::index_error(); - return s[i]; - }) - .def("__setitem__", [](Sequence &s, size_t i, float v) { - if (i >= s.size()) throw py::index_error(); - s[i] = v; - }) + .def("__getitem__", + [](const Sequence &s, size_t i) { + if (i >= s.size()) + throw py::index_error(); + return s[i]; + }) + .def("__setitem__", + [](Sequence &s, size_t i, float v) { + if (i >= s.size()) + throw py::index_error(); + s[i] = v; + }) .def("__len__", &Sequence::size) /// Optional sequence protocol operations - .def("__iter__", [](const Sequence &s) { return py::make_iterator(s.begin(), s.end()); }, - py::keep_alive<0, 1>() /* Essential: keep object alive while iterator exists */) + .def( + "__iter__", + [](const Sequence &s) { return py::make_iterator(s.begin(), s.end()); }, + py::keep_alive<0, 1>() /* Essential: keep object alive while iterator exists */) .def("__contains__", [](const Sequence &s, float v) { return s.contains(v); }) .def("__reversed__", [](const Sequence &s) -> Sequence { return s.reversed(); }) /// Slicing protocol (optional) - .def("__getitem__", [](const Sequence &s, py::slice slice) -> Sequence* { - size_t start, stop, step, slicelength; - if (!slice.compute(s.size(), &start, &stop, &step, &slicelength)) - throw py::error_already_set(); - auto *seq = new Sequence(slicelength); - for (size_t i = 0; i < slicelength; ++i) { - (*seq)[i] = s[start]; start += step; - } - return seq; - }) - .def("__setitem__", [](Sequence &s, py::slice slice, const Sequence &value) { - size_t start, stop, step, slicelength; - if (!slice.compute(s.size(), &start, &stop, &step, &slicelength)) - throw py::error_already_set(); - if (slicelength != value.size()) - throw std::runtime_error("Left and right hand size of slice assignment have different sizes!"); - for (size_t i = 0; i < slicelength; ++i) { - s[start] = value[i]; start += step; - } - }) + .def("__getitem__", + [](const Sequence &s, const py::slice &slice) -> Sequence * { + size_t start, stop, step, slicelength; + if (!slice.compute(s.size(), &start, &stop, &step, &slicelength)) + throw py::error_already_set(); + auto *seq = new Sequence(slicelength); + for (size_t i = 0; i < slicelength; ++i) { + (*seq)[i] = s[start]; + start += step; + } + return seq; + }) + .def("__setitem__", + [](Sequence &s, const py::slice &slice, const Sequence &value) { + size_t start, stop, step, slicelength; + if (!slice.compute(s.size(), &start, &stop, &step, &slicelength)) + throw py::error_already_set(); + if (slicelength != value.size()) + throw std::runtime_error( + "Left and right hand size of slice assignment have different sizes!"); + for (size_t i = 0; i < slicelength; ++i) { + s[start] = value[i]; + start += step; + } + }) /// Comparisons .def(py::self == py::self) .def(py::self != py::self) @@ -231,8 +242,8 @@ TEST_SUBMODULE(sequences_and_iterators, m) { StringMap(std::unordered_map init) : map(std::move(init)) {} - void set(std::string key, std::string val) { map[key] = val; } - std::string get(std::string key) const { return map.at(key); } + void set(const std::string &key, std::string val) { map[key] = std::move(val); } + std::string get(const std::string &key) const { return map.at(key); } size_t size() const { return map.size(); } private: std::unordered_map map; @@ -243,19 +254,24 @@ TEST_SUBMODULE(sequences_and_iterators, m) { py::class_(m, "StringMap") .def(py::init<>()) .def(py::init>()) - .def("__getitem__", [](const StringMap &map, std::string key) { - try { return map.get(key); } - catch (const std::out_of_range&) { - throw py::key_error("key '" + key + "' does not exist"); - } - }) + .def("__getitem__", + [](const StringMap &map, const std::string &key) { + try { + return map.get(key); + } catch (const std::out_of_range &) { + throw py::key_error("key '" + key + "' does not exist"); + } + }) .def("__setitem__", &StringMap::set) .def("__len__", &StringMap::size) - .def("__iter__", [](const StringMap &map) { return py::make_key_iterator(map.begin(), map.end()); }, - py::keep_alive<0, 1>()) - .def("items", [](const StringMap &map) { return py::make_iterator(map.begin(), map.end()); }, - py::keep_alive<0, 1>()) - ; + .def( + "__iter__", + [](const StringMap &map) { return py::make_key_iterator(map.begin(), map.end()); }, + py::keep_alive<0, 1>()) + .def( + "items", + [](const StringMap &map) { return py::make_iterator(map.begin(), map.end()); }, + py::keep_alive<0, 1>()); // test_generalized_iterators class IntPairs { @@ -304,7 +320,7 @@ TEST_SUBMODULE(sequences_and_iterators, m) { #endif // test_python_iterator_in_cpp - m.def("object_to_list", [](py::object o) { + m.def("object_to_list", [](const py::object &o) { auto l = py::list(); for (auto item : o) { l.append(item); @@ -322,22 +338,22 @@ TEST_SUBMODULE(sequences_and_iterators, m) { }); // test_sequence_length: check that Python sequences can be converted to py::sequence. - m.def("sequence_length", [](py::sequence seq) { return seq.size(); }); + m.def("sequence_length", [](const py::sequence &seq) { return seq.size(); }); // Make sure that py::iterator works with std algorithms - m.def("count_none", [](py::object o) { + m.def("count_none", [](const py::object &o) { return std::count_if(o.begin(), o.end(), [](py::handle h) { return h.is_none(); }); }); - m.def("find_none", [](py::object o) { + m.def("find_none", [](const py::object &o) { auto it = std::find_if(o.begin(), o.end(), [](py::handle h) { return h.is_none(); }); return it->is_none(); }); - m.def("count_nonzeros", [](py::dict d) { - return std::count_if(d.begin(), d.end(), [](std::pair p) { - return p.second.cast() != 0; - }); + m.def("count_nonzeros", [](const py::dict &d) { + return std::count_if(d.begin(), d.end(), [](std::pair p) { + return p.second.cast() != 0; + }); }); m.def("tuple_iterator", &test_random_access_iterator); diff --git a/tests/test_smart_ptr.cpp b/tests/test_smart_ptr.cpp index 2ece3bf0c9..534dec0be4 100644 --- a/tests/test_smart_ptr.cpp +++ b/tests/test_smart_ptr.cpp @@ -170,7 +170,7 @@ struct SharedPtrRef { struct A { A() { print_created(this); } A(const A &) { print_copy_created(this); } - A(A &&) { print_move_created(this); } + A(A &&) noexcept { print_move_created(this); } ~A() { print_destroyed(this); } }; @@ -183,7 +183,7 @@ struct SharedFromThisRef { struct B : std::enable_shared_from_this { B() { print_created(this); } B(const B &) : std::enable_shared_from_this() { print_copy_created(this); } - B(B &&) : std::enable_shared_from_this() { print_move_created(this); } + B(B &&) noexcept : std::enable_shared_from_this() { print_move_created(this); } ~B() { print_destroyed(this); } }; @@ -209,7 +209,9 @@ struct C { struct TypeForHolderWithAddressOf { TypeForHolderWithAddressOf() { print_created(this); } TypeForHolderWithAddressOf(const TypeForHolderWithAddressOf &) { print_copy_created(this); } - TypeForHolderWithAddressOf(TypeForHolderWithAddressOf &&) { print_move_created(this); } + TypeForHolderWithAddressOf(TypeForHolderWithAddressOf &&) noexcept { + print_move_created(this); + } ~TypeForHolderWithAddressOf() { print_destroyed(this); } std::string toString() const { return "TypeForHolderWithAddressOf[" + std::to_string(value) + "]"; @@ -245,7 +247,7 @@ struct ElementA : ElementBase { }; struct ElementList { - void add(std::shared_ptr e) { l.push_back(e); } + void add(const std::shared_ptr &e) { l.push_back(e); } std::vector> l; }; @@ -308,6 +310,7 @@ TEST_SUBMODULE(smart_ptr, m) { m.def("make_myobject2_1", []() { return new MyObject2(6); }); m.def("make_myobject2_2", []() { return std::make_shared(7); }); m.def("print_myobject2_1", [](const MyObject2 *obj) { py::print(obj->toString()); }); + // NOLINTNEXTLINE(performance-unnecessary-value-param) m.def("print_myobject2_2", [](std::shared_ptr obj) { py::print(obj->toString()); }); m.def("print_myobject2_3", [](const std::shared_ptr &obj) { py::print(obj->toString()); }); m.def("print_myobject2_4", [](const std::shared_ptr *obj) { py::print((*obj)->toString()); }); @@ -317,6 +320,7 @@ TEST_SUBMODULE(smart_ptr, m) { m.def("make_myobject3_1", []() { return new MyObject3(8); }); m.def("make_myobject3_2", []() { return std::make_shared(9); }); m.def("print_myobject3_1", [](const MyObject3 *obj) { py::print(obj->toString()); }); + // NOLINTNEXTLINE(performance-unnecessary-value-param) m.def("print_myobject3_2", [](std::shared_ptr obj) { py::print(obj->toString()); }); m.def("print_myobject3_3", [](const std::shared_ptr &obj) { py::print(obj->toString()); }); m.def("print_myobject3_4", [](const std::shared_ptr *obj) { py::print((*obj)->toString()); }); @@ -358,12 +362,15 @@ TEST_SUBMODULE(smart_ptr, m) { py::class_>(m, "SharedPtrRef") .def(py::init<>()) .def_readonly("ref", &SharedPtrRef::value) - .def_property_readonly("copy", [](const SharedPtrRef &s) { return s.value; }, - py::return_value_policy::copy) + .def_property_readonly( + "copy", [](const SharedPtrRef &s) { return s.value; }, py::return_value_policy::copy) .def_readonly("holder_ref", &SharedPtrRef::shared) - .def_property_readonly("holder_copy", [](const SharedPtrRef &s) { return s.shared; }, - py::return_value_policy::copy) + .def_property_readonly( + "holder_copy", + [](const SharedPtrRef &s) { return s.shared; }, + py::return_value_policy::copy) .def("set_ref", [](SharedPtrRef &, const A &) { return true; }) + // NOLINTNEXTLINE(performance-unnecessary-value-param) .def("set_holder", [](SharedPtrRef &, std::shared_ptr) { return true; }); // test_shared_ptr_from_this_and_references @@ -372,13 +379,19 @@ TEST_SUBMODULE(smart_ptr, m) { py::class_>(m, "SharedFromThisRef") .def(py::init<>()) .def_readonly("bad_wp", &SharedFromThisRef::value) - .def_property_readonly("ref", [](const SharedFromThisRef &s) -> const B & { return *s.shared; }) - .def_property_readonly("copy", [](const SharedFromThisRef &s) { return s.value; }, - py::return_value_policy::copy) + .def_property_readonly("ref", + [](const SharedFromThisRef &s) -> const B & { return *s.shared; }) + .def_property_readonly( + "copy", + [](const SharedFromThisRef &s) { return s.value; }, + py::return_value_policy::copy) .def_readonly("holder_ref", &SharedFromThisRef::shared) - .def_property_readonly("holder_copy", [](const SharedFromThisRef &s) { return s.shared; }, - py::return_value_policy::copy) + .def_property_readonly( + "holder_copy", + [](const SharedFromThisRef &s) { return s.shared; }, + py::return_value_policy::copy) .def("set_ref", [](SharedFromThisRef &, const B &) { return true; }) + // NOLINTNEXTLINE(performance-unnecessary-value-param) .def("set_holder", [](SharedFromThisRef &, std::shared_ptr) { return true; }); // Issue #865: shared_from_this doesn't work with virtual inheritance @@ -396,10 +409,14 @@ TEST_SUBMODULE(smart_ptr, m) { py::class_(m, "TypeForHolderWithAddressOf") .def_static("make", []() { return HolderWithAddressOf(new TypeForHolderWithAddressOf); }) .def("get", [](const HolderWithAddressOf &self) { return self.get(); }) - .def("print_object_1", [](const TypeForHolderWithAddressOf *obj) { py::print(obj->toString()); }) + .def("print_object_1", + [](const TypeForHolderWithAddressOf *obj) { py::print(obj->toString()); }) + // NOLINTNEXTLINE(performance-unnecessary-value-param) .def("print_object_2", [](HolderWithAddressOf obj) { py::print(obj.get()->toString()); }) - .def("print_object_3", [](const HolderWithAddressOf &obj) { py::print(obj.get()->toString()); }) - .def("print_object_4", [](const HolderWithAddressOf *obj) { py::print((*obj).get()->toString()); }); + .def("print_object_3", + [](const HolderWithAddressOf &obj) { py::print(obj.get()->toString()); }) + .def("print_object_4", + [](const HolderWithAddressOf *obj) { py::print((*obj).get()->toString()); }); // test_move_only_holder_with_addressof_operator using MoveOnlyHolderWithAddressOf = unique_ptr_with_addressof_operator; @@ -411,6 +428,7 @@ TEST_SUBMODULE(smart_ptr, m) { // test_smart_ptr_from_default py::class_>(m, "HeldByDefaultHolder") .def(py::init<>()) + // NOLINTNEXTLINE(performance-unnecessary-value-param) .def_static("load_shared_ptr", [](std::shared_ptr) {}); // test_shared_ptr_gc diff --git a/tests/test_stl.cpp b/tests/test_stl.cpp index 0590162770..07899b84e0 100644 --- a/tests/test_stl.cpp +++ b/tests/test_stl.cpp @@ -202,6 +202,7 @@ TEST_SUBMODULE(stl, m) { }, py::arg_v("x", std::nullopt, "None")); m.def("nodefer_none_optional", [](std::optional) { return true; }); + // NOLINTNEXTLINE(performance-unnecessary-value-param) m.def("nodefer_none_optional", [](py::none) { return false; }); using opt_holder = OptionalHolder; @@ -245,13 +246,13 @@ TEST_SUBMODULE(stl, m) { using result_type = const char *; result_type operator()(int) { return "int"; } - result_type operator()(std::string) { return "std::string"; } + result_type operator()(const std::string &) { return "std::string"; } result_type operator()(double) { return "double"; } result_type operator()(std::nullptr_t) { return "std::nullptr_t"; } }; // test_variant - m.def("load_variant", [](variant v) { + m.def("load_variant", [](const variant &v) { return py::detail::visit_helper::call(visitor(), v); }); m.def("load_variant_2pass", [](variant v) { @@ -287,8 +288,11 @@ TEST_SUBMODULE(stl, m) { m.def("stl_pass_by_pointer", [](std::vector* v) { return *v; }, "v"_a=nullptr); // #1258: pybind11/stl.h converts string to vector + // NOLINTNEXTLINE(performance-unnecessary-value-param) m.def("func_with_string_or_vector_string_arg_overload", [](std::vector) { return 1; }); + // NOLINTNEXTLINE(performance-unnecessary-value-param) m.def("func_with_string_or_vector_string_arg_overload", [](std::list) { return 2; }); + // NOLINTNEXTLINE(performance-unnecessary-value-param) m.def("func_with_string_or_vector_string_arg_overload", [](std::string) { return 3; }); class Placeholder { diff --git a/tests/test_virtual_functions.cpp b/tests/test_virtual_functions.cpp index 685d64a7ca..51068f6380 100644 --- a/tests/test_virtual_functions.cpp +++ b/tests/test_virtual_functions.cpp @@ -17,7 +17,10 @@ class ExampleVirt { public: ExampleVirt(int state) : state(state) { print_created(this, state); } ExampleVirt(const ExampleVirt &e) : state(e.state) { print_copy_created(this); } - ExampleVirt(ExampleVirt &&e) : state(e.state) { print_move_created(this); e.state = 0; } + ExampleVirt(ExampleVirt &&e) noexcept : state(e.state) { + print_move_created(this); + e.state = 0; + } virtual ~ExampleVirt() { print_destroyed(this); } virtual int run(int value) { @@ -100,7 +103,10 @@ class PyExampleVirt : public ExampleVirt { class NonCopyable { public: NonCopyable(int a, int b) : value{new int(a*b)} { print_created(this, a, b); } - NonCopyable(NonCopyable &&o) { value = std::move(o.value); print_move_created(this); } + NonCopyable(NonCopyable &&o) noexcept { + value = std::move(o.value); + print_move_created(this); + } NonCopyable(const NonCopyable &) = delete; NonCopyable() = delete; void operator=(const NonCopyable &) = delete; @@ -120,7 +126,10 @@ class Movable { public: Movable(int a, int b) : value{a+b} { print_created(this, a, b); } Movable(const Movable &m) { value = m.value; print_copy_created(this); } - Movable(Movable &&m) { value = std::move(m.value); print_move_created(this); } + Movable(Movable &&m) noexcept { + value = m.value; + print_move_created(this); + } std::string get_value() const { return std::to_string(value); } ~Movable() { print_destroyed(this); } private: From 484b0f04332fff15543d4c2a49c355d6942455fa Mon Sep 17 00:00:00 2001 From: "Ralf W. Grosse-Kunstleve" Date: Tue, 22 Jun 2021 15:12:58 -0700 Subject: [PATCH 038/638] Updating and slightly enhancing instructions for running clang-tidy. (#3055) * Updating and slightly enhancing instructions for running clang-tidy. * Making documented commands identical to workflow commands. Adding comment in workflow file pointing to documentation. --- .github/CONTRIBUTING.md | 13 +++++++------ .github/workflows/format.yml | 2 ++ 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/.github/CONTRIBUTING.md b/.github/CONTRIBUTING.md index 08d9e7cb93..6f9ebf21f1 100644 --- a/.github/CONTRIBUTING.md +++ b/.github/CONTRIBUTING.md @@ -203,14 +203,15 @@ of the pybind11 repo. [`clang-tidy`][clang-tidy] performs deeper static code analyses and is more complex to run, compared to `clang-format`, but support for `clang-tidy` is built into the pybind11 CMake configuration. To run `clang-tidy`, the -following recipe should work. Files will be modified in place, so you can -use git to monitor the changes. +following recipe should work. Run the `docker` command from the top-level +directory inside your pybind11 git clone. Files will be modified in place, +so you can use git to monitor the changes. ```bash -docker run --rm -v $PWD:/pybind11 -it silkeh/clang:10 -apt-get update && apt-get install python3-dev python3-pytest -cmake -S pybind11/ -B build -DCMAKE_CXX_CLANG_TIDY="$(which clang-tidy);-fix" -cmake --build build +docker run --rm -v $PWD:/mounted_pybind11 -it silkeh/clang:12 +apt-get update && apt-get install -y python3-dev python3-pytest +cmake -S /mounted_pybind11/ -B build -DCMAKE_CXX_CLANG_TIDY="$(which clang-tidy);-fix" -DDOWNLOAD_EIGEN=ON -DDOWNLOAD_CATCH=ON -DCMAKE_CXX_STANDARD=17 +cmake --build build -j 2 -- --keep-going ``` ### Include what you use diff --git a/.github/workflows/format.yml b/.github/workflows/format.yml index 27f52932b7..ab7b40503a 100644 --- a/.github/workflows/format.yml +++ b/.github/workflows/format.yml @@ -25,6 +25,8 @@ jobs: extra_args: --hook-stage manual --all-files clang-tidy: + # When making changes here, please also review the "Clang-Tidy" section + # in .github/CONTRIBUTING.md and update as needed. name: Clang-Tidy runs-on: ubuntu-latest container: silkeh/clang:12 From 93e69191c1ad81ccde1b3dfd46bb69dccd8e45ce Mon Sep 17 00:00:00 2001 From: Cris Luengo Date: Fri, 25 Jun 2021 18:56:17 -0600 Subject: [PATCH 039/638] fix: enable py::implicitly_convertible for py::class_-wrapped types (#3059) * Allow casting from None to a custom object, closes #2778 * ci.yml patch from the smart_holder branch for full CI coverage. --- .github/workflows/ci.yml | 26 +++++++++++----------- include/pybind11/detail/type_caster_base.h | 20 +++++++++++------ tests/test_methods_and_attributes.cpp | 18 +++++++++++++++ tests/test_methods_and_attributes.py | 11 +++++++++ 4 files changed, 55 insertions(+), 20 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index e00745df7e..df7441b9e6 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -100,7 +100,7 @@ jobs: run: > cmake -S . -B . -DPYBIND11_WERROR=ON - -DDOWNLOAD_CATCH=ON + -DDOWNLOAD_CATCH=OFF -DDOWNLOAD_EIGEN=ON -DCMAKE_CXX_STANDARD=11 ${{ matrix.args }} @@ -111,10 +111,10 @@ jobs: - name: Python tests C++11 run: cmake --build . --target pytest -j 2 - - name: C++11 tests - # TODO: Figure out how to load the DLL on Python 3.8+ - if: "!(runner.os == 'Windows' && (matrix.python == 3.8 || matrix.python == 3.9 || matrix.python == '3.10-dev'))" - run: cmake --build . --target cpptest -j 2 + #- name: C++11 tests + # # TODO: Figure out how to load the DLL on Python 3.8+ + # if: "!(runner.os == 'Windows' && (matrix.python == 3.8 || matrix.python == 3.9 || matrix.python == '3.10-dev'))" + # run: cmake --build . --target cpptest -j 2 - name: Interface test C++11 run: cmake --build . --target test_cmake_build @@ -127,7 +127,7 @@ jobs: run: > cmake -S . -B build2 -DPYBIND11_WERROR=ON - -DDOWNLOAD_CATCH=ON + -DDOWNLOAD_CATCH=OFF -DDOWNLOAD_EIGEN=ON -DCMAKE_CXX_STANDARD=17 ${{ matrix.args }} @@ -139,10 +139,10 @@ jobs: - name: Python tests run: cmake --build build2 --target pytest - - name: C++ tests - # TODO: Figure out how to load the DLL on Python 3.8+ - if: "!(runner.os == 'Windows' && (matrix.python == 3.8 || matrix.python == 3.9 || matrix.python == '3.10-dev'))" - run: cmake --build build2 --target cpptest + #- name: C++ tests + # # TODO: Figure out how to load the DLL on Python 3.8+ + # if: "!(runner.os == 'Windows' && (matrix.python == 3.8 || matrix.python == 3.9 || matrix.python == '3.10-dev'))" + # run: cmake --build build2 --target cpptest - name: Interface test run: cmake --build build2 --target test_cmake_build @@ -754,7 +754,7 @@ jobs: cmake -S . -B build -G "Visual Studio 16 2019" -A Win32 -DPYBIND11_WERROR=ON - -DDOWNLOAD_CATCH=ON + -DDOWNLOAD_CATCH=OFF -DDOWNLOAD_EIGEN=ON ${{ matrix.args }} - name: Build C++11 @@ -800,7 +800,7 @@ jobs: cmake -S . -B build -G "Visual Studio 14 2015" -A x64 -DPYBIND11_WERROR=ON - -DDOWNLOAD_CATCH=ON + -DDOWNLOAD_CATCH=OFF -DDOWNLOAD_EIGEN=ON - name: Build C++14 @@ -849,7 +849,7 @@ jobs: cmake -S . -B build -G "Visual Studio 15 2017" -A x64 -DPYBIND11_WERROR=ON - -DDOWNLOAD_CATCH=ON + -DDOWNLOAD_CATCH=OFF -DDOWNLOAD_EIGEN=ON -DCMAKE_CXX_STANDARD=${{ matrix.std }} ${{ matrix.args }} diff --git a/include/pybind11/detail/type_caster_base.h b/include/pybind11/detail/type_caster_base.h index 0dd9d48e55..a8d3938c7c 100644 --- a/include/pybind11/detail/type_caster_base.h +++ b/include/pybind11/detail/type_caster_base.h @@ -657,12 +657,6 @@ class type_caster_generic { PYBIND11_NOINLINE bool load_impl(handle src, bool convert) { if (!src) return false; if (!typeinfo) return try_load_foreign_module_local(src); - if (src.is_none()) { - // Defer accepting None to other overloads (if we aren't in convert mode): - if (!convert) return false; - value = nullptr; - return true; - } auto &this_ = static_cast(*this); this_.check_holder_compat(); @@ -731,7 +725,19 @@ class type_caster_generic { } // Global typeinfo has precedence over foreign module_local - return try_load_foreign_module_local(src); + if (try_load_foreign_module_local(src)) { + return true; + } + + // Custom converters didn't take None, now we convert None to nullptr. + if (src.is_none()) { + // Defer accepting None to other overloads (if we aren't in convert mode): + if (!convert) return false; + value = nullptr; + return true; + } + + return false; } diff --git a/tests/test_methods_and_attributes.cpp b/tests/test_methods_and_attributes.cpp index 3eb22b4a37..67ee117c6c 100644 --- a/tests/test_methods_and_attributes.cpp +++ b/tests/test_methods_and_attributes.cpp @@ -118,6 +118,14 @@ int none3(std::shared_ptr &obj) { return obj ? obj->answer : -1; } int none4(std::shared_ptr *obj) { return obj && *obj ? (*obj)->answer : -1; } int none5(const std::shared_ptr &obj) { return obj ? obj->answer : -1; } +// Issue #2778: implicit casting from None to object (not pointer) +class NoneCastTester { +public: + int answer = -1; + NoneCastTester() = default; + NoneCastTester(int v) : answer(v) {}; +}; + struct StrIssue { int val = -1; @@ -354,6 +362,16 @@ TEST_SUBMODULE(methods_and_attributes, m) { m.def("no_none_kwarg", &none2, "a"_a.none(false)); m.def("no_none_kwarg_kw_only", &none2, py::kw_only(), "a"_a.none(false)); + // test_casts_none + // Issue #2778: implicit casting from None to object (not pointer) + py::class_(m, "NoneCastTester") + .def(py::init<>()) + .def(py::init()) + .def(py::init([](py::none const&) { return NoneCastTester{}; })); + py::implicitly_convertible(); + m.def("ok_obj_or_none", [](NoneCastTester const& foo) { return foo.answer; }); + + // test_str_issue // Issue #283: __str__ called on uninitialized instance when constructor arguments invalid py::class_(m, "StrIssue") diff --git a/tests/test_methods_and_attributes.py b/tests/test_methods_and_attributes.py index 2aaf9331fd..89b7225a97 100644 --- a/tests/test_methods_and_attributes.py +++ b/tests/test_methods_and_attributes.py @@ -431,6 +431,17 @@ def test_accepts_none(msg): assert "incompatible function arguments" in str(excinfo.value) +def test_casts_none(msg): + """#2778: implicit casting from None to object (not pointer)""" + a = m.NoneCastTester() + assert m.ok_obj_or_none(a) == -1 + a = m.NoneCastTester(4) + assert m.ok_obj_or_none(a) == 4 + a = m.NoneCastTester(None) + assert m.ok_obj_or_none(a) == -1 + assert m.ok_obj_or_none(None) == -1 + + def test_str_issue(msg): """#283: __str__ called on uninitialized instance when constructor arguments invalid""" From fbae8f313be887e0bf4b702fe551966efdc8cbc2 Mon Sep 17 00:00:00 2001 From: "Ralf W. Grosse-Kunstleve" Date: Wed, 30 Jun 2021 12:34:32 -0700 Subject: [PATCH 040/638] pickle setstate: setattr __dict__ only if not empty (#2972) * pickle setstate: setattr __dict__ only if not empty, to not force use of py::dynamic_attr() unnecessarily. * Adding unit test. * Clang 3.6 & 3.7 compatibility. * PyPy compatibility. * Minor iwyu fix, additional comment. * Addressing reviewer requests. * Applying clang-tidy suggested fixes. * Adding check_dynamic_cast_SimpleCppDerived, related to issue #3062. --- include/pybind11/detail/init.h | 8 ++++- tests/test_pickling.cpp | 56 ++++++++++++++++++++++++++++++++++ tests/test_pickling.py | 36 ++++++++++++++++++++++ 3 files changed, 99 insertions(+), 1 deletion(-) diff --git a/include/pybind11/detail/init.h b/include/pybind11/detail/init.h index 3ef78c1179..3269e04254 100644 --- a/include/pybind11/detail/init.h +++ b/include/pybind11/detail/init.h @@ -293,7 +293,13 @@ template ::value, int> = 0> void setstate(value_and_holder &v_h, std::pair &&result, bool need_alias) { construct(v_h, std::move(result.first), need_alias); - setattr((PyObject *) v_h.inst, "__dict__", result.second); + auto d = handle(result.second); + if (PyDict_Check(d.ptr()) && PyDict_Size(d.ptr()) == 0) { + // Skipping setattr below, to not force use of py::dynamic_attr() for Class unnecessarily. + // See PR #2972 for details. + return; + } + setattr((PyObject *) v_h.inst, "__dict__", d); } /// Implementation for py::pickle(GetState, SetState) diff --git a/tests/test_pickling.cpp b/tests/test_pickling.cpp index c2cee6c3c7..0d5827315f 100644 --- a/tests/test_pickling.cpp +++ b/tests/test_pickling.cpp @@ -1,7 +1,9 @@ +// clang-format off /* tests/test_pickling.cpp -- pickle support Copyright (c) 2016 Wenzel Jakob + Copyright (c) 2021 The Pybind Development Team. All rights reserved. Use of this source code is governed by a BSD-style license that can be found in the LICENSE file. @@ -9,6 +11,58 @@ #include "pybind11_tests.h" +// clang-format on + +#include +#include +#include + +namespace exercise_trampoline { + +struct SimpleBase { + int num = 0; + virtual ~SimpleBase() = default; + + // For compatibility with old clang versions: + SimpleBase() = default; + SimpleBase(const SimpleBase &) = default; +}; + +struct SimpleBaseTrampoline : SimpleBase {}; + +struct SimpleCppDerived : SimpleBase {}; + +void wrap(py::module m) { + py::class_(m, "SimpleBase") + .def(py::init<>()) + .def_readwrite("num", &SimpleBase::num) + .def(py::pickle( + [](const py::object &self) { + py::dict d; + if (py::hasattr(self, "__dict__")) + d = self.attr("__dict__"); + return py::make_tuple(self.attr("num"), d); + }, + [](const py::tuple &t) { + if (t.size() != 2) + throw std::runtime_error("Invalid state!"); + auto cpp_state = std::unique_ptr(new SimpleBaseTrampoline); + cpp_state->num = t[0].cast(); + auto py_state = t[1].cast(); + return std::make_pair(std::move(cpp_state), py_state); + })); + + m.def("make_SimpleCppDerivedAsBase", + []() { return std::unique_ptr(new SimpleCppDerived); }); + m.def("check_dynamic_cast_SimpleCppDerived", [](const SimpleBase *base_ptr) { + return dynamic_cast(base_ptr) != nullptr; + }); +} + +} // namespace exercise_trampoline + +// clang-format off + TEST_SUBMODULE(pickling, m) { // test_roundtrip class Pickleable { @@ -130,4 +184,6 @@ TEST_SUBMODULE(pickling, m) { return std::make_pair(cpp_state, py_state); })); #endif + + exercise_trampoline::wrap(m); } diff --git a/tests/test_pickling.py b/tests/test_pickling.py index 6b27a73a57..303213068d 100644 --- a/tests/test_pickling.py +++ b/tests/test_pickling.py @@ -45,3 +45,39 @@ def test_enum_pickle(): data = pickle.dumps(e.EOne, 2) assert e.EOne == pickle.loads(data) + + +# +# exercise_trampoline +# +class SimplePyDerived(m.SimpleBase): + pass + + +def test_roundtrip_simple_py_derived(): + p = SimplePyDerived() + p.num = 202 + p.stored_in_dict = 303 + data = pickle.dumps(p, pickle.HIGHEST_PROTOCOL) + p2 = pickle.loads(data) + assert isinstance(p2, SimplePyDerived) + assert p2.num == 202 + assert p2.stored_in_dict == 303 + + +def test_roundtrip_simple_cpp_derived(): + p = m.make_SimpleCppDerivedAsBase() + assert m.check_dynamic_cast_SimpleCppDerived(p) + p.num = 404 + if not env.PYPY: + # To ensure that this unit test is not accidentally invalidated. + with pytest.raises(AttributeError): + # Mimics the `setstate` C++ implementation. + setattr(p, "__dict__", {}) # noqa: B010 + data = pickle.dumps(p, pickle.HIGHEST_PROTOCOL) + p2 = pickle.loads(data) + assert isinstance(p2, m.SimpleBase) + assert p2.num == 404 + # Issue #3062: pickleable base C++ classes can incur object slicing + # if derived typeid is not registered with pybind11 + assert not m.check_dynamic_cast_SimpleCppDerived(p2) From cad79c1146c64ece40a9513dbffb4bb8ba93d561 Mon Sep 17 00:00:00 2001 From: "Ralf W. Grosse-Kunstleve" Date: Wed, 30 Jun 2021 14:30:26 -0700 Subject: [PATCH 041/638] tests: remove very minor oversight in PR #3059. (#3066) --- tests/test_methods_and_attributes.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_methods_and_attributes.py b/tests/test_methods_and_attributes.py index 89b7225a97..750ec02f89 100644 --- a/tests/test_methods_and_attributes.py +++ b/tests/test_methods_and_attributes.py @@ -431,7 +431,7 @@ def test_accepts_none(msg): assert "incompatible function arguments" in str(excinfo.value) -def test_casts_none(msg): +def test_casts_none(): """#2778: implicit casting from None to object (not pointer)""" a = m.NoneCastTester() assert m.ok_obj_or_none(a) == -1 From 733f8de24feed964f96b639a0a44247f46bed868 Mon Sep 17 00:00:00 2001 From: jonathan-conder-sm <63538679+jonathan-conder-sm@users.noreply.github.com> Date: Thu, 1 Jul 2021 17:19:14 +1200 Subject: [PATCH 042/638] Avoid string copy if possible when passing a Python object to std::ostream (#3042) --- include/pybind11/stl.h | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/include/pybind11/stl.h b/include/pybind11/stl.h index 61d3fba61e..ca20b7483b 100644 --- a/include/pybind11/stl.h +++ b/include/pybind11/stl.h @@ -380,7 +380,11 @@ struct type_caster> : variant_caster> { PYBIND11_NAMESPACE_END(detail) inline std::ostream &operator<<(std::ostream &os, const handle &obj) { +#ifdef PYBIND11_HAS_STRING_VIEW + os << str(obj).cast(); +#else os << (std::string) str(obj); +#endif return os; } From f067deb563d1073a7193b87d058fce37838ea5aa Mon Sep 17 00:00:00 2001 From: cyy Date: Thu, 1 Jul 2021 14:35:25 +0800 Subject: [PATCH 043/638] avoid unnecessary strlen (#3058) --- include/pybind11/attr.h | 2 +- include/pybind11/pybind11.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/include/pybind11/attr.h b/include/pybind11/attr.h index 50efdc7cec..97147904ce 100644 --- a/include/pybind11/attr.h +++ b/include/pybind11/attr.h @@ -377,7 +377,7 @@ template <> struct process_attribute : process_attribu }; inline void process_kw_only_arg(const arg &a, function_record *r) { - if (!a.name || strlen(a.name) == 0) + if (!a.name || a.name[0] == '\0') pybind11_fail("arg(): cannot specify an unnamed argument after an kw_only() annotation"); ++r->nargs_kw_only; } diff --git a/include/pybind11/pybind11.h b/include/pybind11/pybind11.h index 1fb0d1fa18..9938b0b716 100644 --- a/include/pybind11/pybind11.h +++ b/include/pybind11/pybind11.h @@ -470,7 +470,7 @@ class cpp_function : public function { signatures += it->signature; signatures += "\n"; } - if (it->doc && strlen(it->doc) > 0 && options::show_user_defined_docstrings()) { + if (it->doc && it->doc[0] != '\0' && options::show_user_defined_docstrings()) { // If we're appending another docstring, and aren't printing function signatures, we // need to append a newline first: if (!options::show_function_signatures()) { From 5bcaaa0423c6757ca1c2738d0a54947dacdb03a1 Mon Sep 17 00:00:00 2001 From: Antony Lee Date: Fri, 2 Jul 2021 16:00:50 +0200 Subject: [PATCH 044/638] Add a std::filesystem::path <-> os.PathLike caster. (#2730) --- docs/advanced/cast/overview.rst | 6 +++ include/pybind11/stl.h | 81 +++++++++++++++++++++++++++++++++ tests/CMakeLists.txt | 37 +++++++++++++++ tests/test_stl.cpp | 6 +++ tests/test_stl.py | 19 ++++++++ 5 files changed, 149 insertions(+) diff --git a/docs/advanced/cast/overview.rst b/docs/advanced/cast/overview.rst index b0e32a52f9..6341fce6d4 100644 --- a/docs/advanced/cast/overview.rst +++ b/docs/advanced/cast/overview.rst @@ -151,6 +151,8 @@ as arguments and return values, refer to the section on binding :ref:`classes`. +------------------------------------+---------------------------+-------------------------------+ | ``std::variant<...>`` | Type-safe union (C++17) | :file:`pybind11/stl.h` | +------------------------------------+---------------------------+-------------------------------+ +| ``std::filesystem::path`` | STL path (C++17) [#]_ | :file:`pybind11/stl.h` | ++------------------------------------+---------------------------+-------------------------------+ | ``std::function<...>`` | STL polymorphic function | :file:`pybind11/functional.h` | +------------------------------------+---------------------------+-------------------------------+ | ``std::chrono::duration<...>`` | STL time duration | :file:`pybind11/chrono.h` | @@ -163,3 +165,7 @@ as arguments and return values, refer to the section on binding :ref:`classes`. +------------------------------------+---------------------------+-------------------------------+ | ``Eigen::SparseMatrix<...>`` | Eigen: sparse matrix | :file:`pybind11/eigen.h` | +------------------------------------+---------------------------+-------------------------------+ + +.. [#] ``std::filesystem::path`` is converted to ``pathlib.Path`` and + ``os.PathLike`` is converted to ``std::filesystem::path``, but this requires + Python 3.6 (for ``__fspath__`` support). diff --git a/include/pybind11/stl.h b/include/pybind11/stl.h index ca20b7483b..2350a5247f 100644 --- a/include/pybind11/stl.h +++ b/include/pybind11/stl.h @@ -41,11 +41,21 @@ # include # define PYBIND11_HAS_VARIANT 1 # endif +// std::filesystem::path +# if defined(PYBIND11_CPP17) && __has_include() && \ + PY_VERSION_HEX >= 0x03060000 +# include +# define PYBIND11_HAS_FILESYSTEM 1 +# endif #elif defined(_MSC_VER) && defined(PYBIND11_CPP17) # include # include # define PYBIND11_HAS_OPTIONAL 1 # define PYBIND11_HAS_VARIANT 1 +# if PY_VERSION_HEX >= 0x03060000 +# include +# define PYBIND11_HAS_FILESYSTEM 1 +# endif #endif PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE) @@ -377,6 +387,77 @@ template struct type_caster> : variant_caster> { }; #endif +#if defined(PYBIND11_HAS_FILESYSTEM) +template struct path_caster { + +private: + static PyObject* unicode_from_fs_native(const std::string& w) { +#if !defined(PYPY_VERSION) + return PyUnicode_DecodeFSDefaultAndSize(w.c_str(), ssize_t(w.size())); +#else + // PyPy mistakenly declares the first parameter as non-const. + return PyUnicode_DecodeFSDefaultAndSize( + const_cast(w.c_str()), ssize_t(w.size())); +#endif + } + + static PyObject* unicode_from_fs_native(const std::wstring& w) { + return PyUnicode_FromWideChar(w.c_str(), ssize_t(w.size())); + } + +public: + static handle cast(const T& path, return_value_policy, handle) { + if (auto py_str = unicode_from_fs_native(path.native())) { + return module::import("pathlib").attr("Path")(reinterpret_steal(py_str)) + .release(); + } + return nullptr; + } + + bool load(handle handle, bool) { + // PyUnicode_FSConverter and PyUnicode_FSDecoder normally take care of + // calling PyOS_FSPath themselves, but that's broken on PyPy (PyPy + // issue #3168) so we do it ourselves instead. + PyObject* buf = PyOS_FSPath(handle.ptr()); + if (!buf) { + PyErr_Clear(); + return false; + } + PyObject* native = nullptr; + if constexpr (std::is_same_v) { + if (PyUnicode_FSConverter(buf, &native)) { + if (auto c_str = PyBytes_AsString(native)) { + // AsString returns a pointer to the internal buffer, which + // must not be free'd. + value = c_str; + } + } + } else if constexpr (std::is_same_v) { + if (PyUnicode_FSDecoder(buf, &native)) { + if (auto c_str = PyUnicode_AsWideCharString(native, nullptr)) { + // AsWideCharString returns a new string that must be free'd. + value = c_str; // Copies the string. + PyMem_Free(c_str); + } + } + } + Py_XDECREF(native); + Py_DECREF(buf); + if (PyErr_Occurred()) { + PyErr_Clear(); + return false; + } else { + return true; + } + } + + PYBIND11_TYPE_CASTER(T, _("os.PathLike")); +}; + +template<> struct type_caster + : public path_caster {}; +#endif + PYBIND11_NAMESPACE_END(detail) inline std::ostream &operator<<(std::ostream &os, const handle &obj) { diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 3d89404911..3729b5c7df 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -247,6 +247,41 @@ if(Boost_FOUND) endif() endif() +# Check if we need to add -lstdc++fs or -lc++fs or nothing +if(MSVC) + set(STD_FS_NO_LIB_NEEDED TRUE) +else() + file( + WRITE ${CMAKE_CURRENT_BINARY_DIR}/main.cpp + "#include \nint main(int argc, char ** argv) {\n std::filesystem::path p(argv[0]);\n return p.string().length();\n}" + ) + try_compile( + STD_FS_NO_LIB_NEEDED ${CMAKE_CURRENT_BINARY_DIR} + SOURCES ${CMAKE_CURRENT_BINARY_DIR}/main.cpp + COMPILE_DEFINITIONS -std=c++17) + try_compile( + STD_FS_NEEDS_STDCXXFS ${CMAKE_CURRENT_BINARY_DIR} + SOURCES ${CMAKE_CURRENT_BINARY_DIR}/main.cpp + COMPILE_DEFINITIONS -std=c++17 + LINK_LIBRARIES stdc++fs) + try_compile( + STD_FS_NEEDS_CXXFS ${CMAKE_CURRENT_BINARY_DIR} + SOURCES ${CMAKE_CURRENT_BINARY_DIR}/main.cpp + COMPILE_DEFINITIONS -std=c++17 + LINK_LIBRARIES c++fs) +endif() + +if(${STD_FS_NEEDS_STDCXXFS}) + set(STD_FS_LIB stdc++fs) +elseif(${STD_FS_NEEDS_CXXFS}) + set(STD_FS_LIB c++fs) +elseif(${STD_FS_NO_LIB_NEEDED}) + set(STD_FS_LIB "") +else() + message(WARNING "Unknown compiler - not passing -lstdc++fs") + set(STD_FS_LIB "") +endif() + # Compile with compiler warnings turned on function(pybind11_enable_warnings target_name) if(MSVC) @@ -357,6 +392,8 @@ foreach(target ${test_targets}) target_compile_definitions(${target} PRIVATE -DPYBIND11_TEST_BOOST) endif() + target_link_libraries(${target} PRIVATE ${STD_FS_LIB}) + # Always write the output file directly into the 'tests' directory (even on MSVC) if(NOT CMAKE_LIBRARY_OUTPUT_DIRECTORY) set_target_properties(${target} PROPERTIES LIBRARY_OUTPUT_DIRECTORY diff --git a/tests/test_stl.cpp b/tests/test_stl.cpp index 07899b84e0..7183c56b7a 100644 --- a/tests/test_stl.cpp +++ b/tests/test_stl.cpp @@ -238,6 +238,12 @@ TEST_SUBMODULE(stl, m) { .def("member_initialized", &opt_exp_holder::member_initialized); #endif +#ifdef PYBIND11_HAS_FILESYSTEM + // test_fs_path + m.attr("has_filesystem") = true; + m.def("parent_path", [](const std::filesystem::path& p) { return p.parent_path(); }); +#endif + #ifdef PYBIND11_HAS_VARIANT static_assert(std::is_same::value, "visitor::result_type is required by boost::variant in C++11 mode"); diff --git a/tests/test_stl.py b/tests/test_stl.py index 330017544d..96939257cf 100644 --- a/tests/test_stl.py +++ b/tests/test_stl.py @@ -162,6 +162,25 @@ def test_exp_optional(): assert holder.member_initialized() +@pytest.mark.skipif(not hasattr(m, "has_filesystem"), reason="no ") +def test_fs_path(): + from pathlib import Path + + class PseudoStrPath: + def __fspath__(self): + return "foo/bar" + + class PseudoBytesPath: + def __fspath__(self): + return b"foo/bar" + + assert m.parent_path(Path("foo/bar")) == Path("foo") + assert m.parent_path("foo/bar") == Path("foo") + assert m.parent_path(b"foo/bar") == Path("foo") + assert m.parent_path(PseudoStrPath()) == Path("foo") + assert m.parent_path(PseudoBytesPath()) == Path("foo") + + @pytest.mark.skipif(not hasattr(m, "load_variant"), reason="no ") def test_variant(doc): assert m.load_variant(1) == "int" From 6d4409466b6c7e6fa53e7b93cb05d78647c26313 Mon Sep 17 00:00:00 2001 From: Dustin Spicuzza Date: Fri, 2 Jul 2021 10:02:33 -0400 Subject: [PATCH 045/638] Check dict item accesses where it isn't already checked (#2863) * Convert PyDict_GetXXX to internal error checking variant * Check unlikely error return from PyDict_DelItemString --- include/pybind11/pybind11.h | 10 ++++++---- include/pybind11/pytypes.h | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 4 deletions(-) diff --git a/include/pybind11/pybind11.h b/include/pybind11/pybind11.h index 9938b0b716..4d7bc5c73e 100644 --- a/include/pybind11/pybind11.h +++ b/include/pybind11/pybind11.h @@ -636,7 +636,7 @@ class cpp_function : public function { bool bad_arg = false; for (; args_copied < args_to_copy; ++args_copied) { const argument_record *arg_rec = args_copied < func.args.size() ? &func.args[args_copied] : nullptr; - if (kwargs_in && arg_rec && arg_rec->name && PyDict_GetItemString(kwargs_in, arg_rec->name)) { + if (kwargs_in && arg_rec && arg_rec->name && dict_getitemstring(kwargs_in, arg_rec->name)) { bad_arg = true; break; } @@ -684,7 +684,7 @@ class cpp_function : public function { handle value; if (kwargs_in && arg_rec.name) - value = PyDict_GetItemString(kwargs.ptr(), arg_rec.name); + value = dict_getitemstring(kwargs.ptr(), arg_rec.name); if (value) { // Consume a kwargs value @@ -692,7 +692,9 @@ class cpp_function : public function { kwargs = reinterpret_steal(PyDict_Copy(kwargs.ptr())); copied_kwargs = true; } - PyDict_DelItemString(kwargs.ptr(), arg_rec.name); + if (PyDict_DelItemString(kwargs.ptr(), arg_rec.name) == -1) { + throw error_already_set(); + } } else if (arg_rec.value) { value = arg_rec.value; } @@ -2139,7 +2141,7 @@ inline function get_type_override(const void *this_ptr, const type_info *this_ty if (frame && (std::string) str(frame->f_code->co_name) == name && frame->f_code->co_argcount > 0) { PyFrame_FastToLocals(frame); - PyObject *self_caller = PyDict_GetItem( + PyObject *self_caller = dict_getitem( frame->f_locals, PyTuple_GET_ITEM(frame->f_code->co_varnames, 0)); if (self_caller == self.ptr()) return function(); diff --git a/include/pybind11/pytypes.h b/include/pybind11/pytypes.h index d1d92af6ad..80637fb980 100644 --- a/include/pybind11/pytypes.h +++ b/include/pybind11/pytypes.h @@ -485,6 +485,43 @@ inline handle get_function(handle value) { return value; } +// Reimplementation of python's dict helper functions to ensure that exceptions +// aren't swallowed (see #2862) + +// copied from cpython _PyDict_GetItemStringWithError +inline PyObject * dict_getitemstring(PyObject *v, const char *key) +{ +#if PY_MAJOR_VERSION >= 3 + PyObject *kv, *rv; + kv = PyUnicode_FromString(key); + if (kv == NULL) { + throw error_already_set(); + } + + rv = PyDict_GetItemWithError(v, kv); + Py_DECREF(kv); + if (rv == NULL && PyErr_Occurred()) { + throw error_already_set(); + } + return rv; +#else + return PyDict_GetItemString(v, key); +#endif +} + +inline PyObject * dict_getitem(PyObject *v, PyObject *key) +{ +#if PY_MAJOR_VERSION >= 3 + PyObject *rv = PyDict_GetItemWithError(v, key); + if (rv == NULL && PyErr_Occurred()) { + throw error_already_set(); + } + return rv; +#else + return PyDict_GetItem(v, key); +#endif +} + // Helper aliases/functions to support implicit casting of values given to python accessors/methods. // When given a pyobject, this simply returns the pyobject as-is; for other C++ type, the value goes // through pybind11::cast(obj) to convert it to an `object`. From 795e3c4ce11dea3df7ef164c3faeef01026cb6c5 Mon Sep 17 00:00:00 2001 From: "Ralf W. Grosse-Kunstleve" Date: Fri, 2 Jul 2021 14:14:18 -0700 Subject: [PATCH 046/638] Removing `AlignConsecutiveAssignments: true`. (#3067) Also removing a commented-out line. --- .clang-format | 2 -- 1 file changed, 2 deletions(-) diff --git a/.clang-format b/.clang-format index 8700fca84d..8e0fd8b014 100644 --- a/.clang-format +++ b/.clang-format @@ -3,7 +3,6 @@ # clang-format --style=llvm --dump-config BasedOnStyle: LLVM AccessModifierOffset: -4 -AlignConsecutiveAssignments: true AlwaysBreakTemplateDeclarations: Yes BinPackArguments: false BinPackParameters: false @@ -15,7 +14,6 @@ IndentPPDirectives: AfterHash IndentWidth: 4 Language: Cpp SpaceAfterCStyleCast: true -# SpaceInEmptyBlock: true # too new Standard: Cpp11 TabWidth: 4 ... From 8bee61b645da98c833166e85113f55a114319ceb Mon Sep 17 00:00:00 2001 From: luzpaz Date: Sun, 4 Jul 2021 19:58:35 -0400 Subject: [PATCH 047/638] docs: fix various typos (#3075) Found via `codespell -q 3 -L nd,ot,thist` --- .github/workflows/ci.yml | 2 +- docs/changelog.rst | 2 +- docs/compiling.rst | 2 +- docs/faq.rst | 2 +- docs/upgrade.rst | 2 +- include/pybind11/detail/class.h | 2 +- pybind11/setup_helpers.py | 2 +- tests/test_builtin_casters.py | 2 +- tests/test_kwargs_and_defaults.cpp | 2 +- tests/test_numpy_vectorize.cpp | 2 +- tests/valgrind-numpy-scipy.supp | 2 +- 11 files changed, 11 insertions(+), 11 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index df7441b9e6..2fa4303d9b 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -384,7 +384,7 @@ jobs: run: yum -y install https://developer.download.nvidia.com/hpc-sdk/20.9/nvhpc-20-9-20.9-1.x86_64.rpm https://developer.download.nvidia.com/hpc-sdk/20.9/nvhpc-2020-20.9-1.x86_64.rpm # On CentOS 7, we have to filter a few tests (compiler internal error) - # and allow deeper templete recursion (not needed on CentOS 8 with a newer + # and allow deeper template recursion (not needed on CentOS 8 with a newer # standard library). On some systems, you many need further workarounds: # https://github.com/pybind/pybind11/pull/2475 - name: Configure diff --git a/docs/changelog.rst b/docs/changelog.rst index 84f1352fb9..6787349e53 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -504,7 +504,7 @@ v2.4.0 (Sep 19, 2019) `#1888 `_. * ``py::details::overload_cast_impl`` is available in C++11 mode, can be used - like ``overload_cast`` with an additional set of parantheses. + like ``overload_cast`` with an additional set of parentheses. `#1581 `_. * Fixed ``get_include()`` on Conda. diff --git a/docs/compiling.rst b/docs/compiling.rst index 27f30e5a06..5d02727bad 100644 --- a/docs/compiling.rst +++ b/docs/compiling.rst @@ -93,7 +93,7 @@ to a memory dependent number. If you are developing rapidly and have a lot of C++ files, you may want to avoid rebuilding files that have not changed. For simple cases were you are using ``pip install -e .`` and do not have local headers, you can skip the -rebuild if a object file is newer than it's source (headers are not checked!) +rebuild if an object file is newer than its source (headers are not checked!) with the following: .. code-block:: python diff --git a/docs/faq.rst b/docs/faq.rst index 8c0b0f9fc2..56ded0329a 100644 --- a/docs/faq.rst +++ b/docs/faq.rst @@ -180,7 +180,7 @@ How can I create smaller binaries? To do its job, pybind11 extensively relies on a programming technique known as *template metaprogramming*, which is a way of performing computation at compile -time using type information. Template metaprogamming usually instantiates code +time using type information. Template metaprogramming usually instantiates code involving significant numbers of deeply nested types that are either completely removed or reduced to just a few instructions during the compiler's optimization phase. However, due to the nested nature of these types, the resulting symbol diff --git a/docs/upgrade.rst b/docs/upgrade.rst index 5691879c85..b2b6b12456 100644 --- a/docs/upgrade.rst +++ b/docs/upgrade.rst @@ -281,7 +281,7 @@ Within pybind11's CMake build system, ``pybind11_add_module`` has always been setting the ``-fvisibility=hidden`` flag in release mode. From now on, it's being applied unconditionally, even in debug mode and it can no longer be opted out of with the ``NO_EXTRAS`` option. The ``pybind11::module`` target now also -adds this flag to it's interface. The ``pybind11::embed`` target is unchanged. +adds this flag to its interface. The ``pybind11::embed`` target is unchanged. The most significant change here is for the ``pybind11::module`` target. If you were previously relying on default visibility, i.e. if your Python module was diff --git a/include/pybind11/detail/class.h b/include/pybind11/detail/class.h index 2f414e5c7c..ca6854e7bf 100644 --- a/include/pybind11/detail/class.h +++ b/include/pybind11/detail/class.h @@ -329,7 +329,7 @@ inline bool deregister_instance(instance *self, void *valptr, const type_info *t inline PyObject *make_new_instance(PyTypeObject *type) { #if defined(PYPY_VERSION) // PyPy gets tp_basicsize wrong (issue 2482) under multiple inheritance when the first inherited - // object is a a plain Python type (i.e. not derived from an extension type). Fix it. + // object is a plain Python type (i.e. not derived from an extension type). Fix it. ssize_t instance_size = static_cast(sizeof(instance)); if (type->tp_basicsize < instance_size) { type->tp_basicsize = instance_size; diff --git a/pybind11/setup_helpers.py b/pybind11/setup_helpers.py index 1050f61253..ec553d1669 100644 --- a/pybind11/setup_helpers.py +++ b/pybind11/setup_helpers.py @@ -85,7 +85,7 @@ class Pybind11Extension(_Extension): * ``stdlib=libc++`` on macOS * ``visibility=hidden`` and ``-g0`` on Unix - Finally, you can set ``cxx_std`` via constructor or afterwords to enable + Finally, you can set ``cxx_std`` via constructor or afterwards to enable flags for C++ std, and a few extra helper flags related to the C++ standard level. It is _highly_ recommended you either set this, or use the provided ``build_ext``, which will search for the highest supported extension for diff --git a/tests/test_builtin_casters.py b/tests/test_builtin_casters.py index 5ee8603a6b..99c400c308 100644 --- a/tests/test_builtin_casters.py +++ b/tests/test_builtin_casters.py @@ -521,7 +521,7 @@ def test_void_caster_2(): def test_const_ref_caster(): """Verifies that const-ref is propagated through type_caster cast_op. - The returned ConstRefCasted type is a mimimal type that is constructed to + The returned ConstRefCasted type is a minimal type that is constructed to reference the casting mode used. """ x = False diff --git a/tests/test_kwargs_and_defaults.cpp b/tests/test_kwargs_and_defaults.cpp index ab1c94c914..a349d43184 100644 --- a/tests/test_kwargs_and_defaults.cpp +++ b/tests/test_kwargs_and_defaults.cpp @@ -65,7 +65,7 @@ TEST_SUBMODULE(kwargs_and_defaults, m) { #endif m.def("arg_refcount_h", [](py::handle h) { GC_IF_NEEDED; return h.ref_count(); }); m.def("arg_refcount_h", [](py::handle h, py::handle, py::handle) { GC_IF_NEEDED; return h.ref_count(); }); - // TODO replace the following nolints as appropiate + // TODO replace the following nolints as appropriate // NOLINTNEXTLINE(performance-unnecessary-value-param) m.def("arg_refcount_o", [](py::object o) { GC_IF_NEEDED; return o.ref_count(); }); m.def("args_refcount", [](py::args a) { diff --git a/tests/test_numpy_vectorize.cpp b/tests/test_numpy_vectorize.cpp index 77e281bbdc..0be30d8e88 100644 --- a/tests/test_numpy_vectorize.cpp +++ b/tests/test_numpy_vectorize.cpp @@ -39,7 +39,7 @@ TEST_SUBMODULE(numpy_vectorize, m) { // test_type_selection // NumPy function which only accepts specific data types - // Alot of these no lints could be replaced with const refs, and probably should at some point. + // A lot of these no lints could be replaced with const refs, and probably should at some point. // NOLINTNEXTLINE(performance-unnecessary-value-param) m.def("selective_func", [](py::array_t) { return "Int branch taken."; }); // NOLINTNEXTLINE(performance-unnecessary-value-param) diff --git a/tests/valgrind-numpy-scipy.supp b/tests/valgrind-numpy-scipy.supp index 001b6c717a..16db302c17 100644 --- a/tests/valgrind-numpy-scipy.supp +++ b/tests/valgrind-numpy-scipy.supp @@ -2,7 +2,7 @@ # # On updating a dependency, to get a list of "default" leaks in e.g. NumPy, run # `PYTHONMALLOC=malloc valgrind --leak-check=full --show-leak-kinds=definite,indirect python3.9-dbg -c "import numpy"` -# To use theses suppression files, add e.g. `--suppressions=valgrind-numpy-scipy.supp` +# To use these suppression files, add e.g. `--suppressions=valgrind-numpy-scipy.supp` { Leaks when importing NumPy From 0ad116d3717a3a39b837b9053a6f9b051784454a Mon Sep 17 00:00:00 2001 From: "Ralf W. Grosse-Kunstleve" Date: Tue, 6 Jul 2021 14:10:35 -0700 Subject: [PATCH 048/638] Adding codespell to .pre-commit-config.yaml (follow-on to PR #3075). (#3076) --- .pre-commit-config.yaml | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 6781ac4f11..85d575464f 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -81,6 +81,17 @@ repos: stages: [manual] additional_dependencies: [cmake, ninja] +- repo: https://github.com/codespell-project/codespell + rev: v2.1.0 + hooks: + - id: codespell + name: codespell + description: Checks for common misspellings in text files. + entry: codespell + language: python + types: [text] + args: ["-q", "3", "--skip", "*.supp", "-L", "nd,ot,thist,readded"] + # The original pybind11 checks for a few C++ style items - repo: local hooks: From c090c8c409feb6f7f89657b0677a46b3e2192493 Mon Sep 17 00:00:00 2001 From: Robert Haschke Date: Wed, 7 Jul 2021 00:13:13 +0200 Subject: [PATCH 049/638] Unify cast_error message thrown by [simple|unpacking]_collector (#3013) * Unify cast_error message thrown by [simple|unpacking]_collector simple_collector and unpacking_collector throw different error messages when the casting of an argument failed: While the former mentions make_tuple(), the latter emphasises the call argument (and its name/position). * Consolidating "Unable to convert call argument" error reporting code to guarantee uniformity. Co-authored-by: Ralf W. Grosse-Kunstleve --- include/pybind11/cast.h | 40 ++++++++++++++++++++++------------------ tests/test_pytypes.py | 6 +++--- 2 files changed, 25 insertions(+), 21 deletions(-) diff --git a/include/pybind11/cast.h b/include/pybind11/cast.h index 1ef1a9ce9b..4a219e2df3 100644 --- a/include/pybind11/cast.h +++ b/include/pybind11/cast.h @@ -958,6 +958,21 @@ template <> inline void cast_safe(object &&) {} PYBIND11_NAMESPACE_END(detail) +// The overloads could coexist, i.e. the #if is not strictly speaking needed, +// but it is an easy minor optimization. +#if defined(NDEBUG) +inline cast_error cast_error_unable_to_convert_call_arg() { + return cast_error( + "Unable to convert call argument to Python object (compile in debug mode for details)"); +} +#else +inline cast_error cast_error_unable_to_convert_call_arg(const std::string &name, + const std::string &type) { + return cast_error("Unable to convert call argument '" + name + "' of type '" + type + + "' to Python object"); +} +#endif + template tuple make_tuple() { return tuple(0); } @@ -971,11 +986,10 @@ template argtypes { {type_id()...} }; - throw cast_error("make_tuple(): unable to convert argument of type '" + - argtypes[i] + "' to Python object"); + throw cast_error_unable_to_convert_call_arg(std::to_string(i), argtypes[i]); #endif } } @@ -1230,9 +1244,10 @@ class unpacking_collector { auto o = reinterpret_steal(detail::make_caster::cast(std::forward(x), policy, {})); if (!o) { #if defined(NDEBUG) - argument_cast_error(); + throw cast_error_unable_to_convert_call_arg(); #else - argument_cast_error(std::to_string(args_list.size()), type_id()); + throw cast_error_unable_to_convert_call_arg( + std::to_string(args_list.size()), type_id()); #endif } args_list.append(o); @@ -1260,9 +1275,9 @@ class unpacking_collector { } if (!a.value) { #if defined(NDEBUG) - argument_cast_error(); + throw cast_error_unable_to_convert_call_arg(); #else - argument_cast_error(a.name, a.type); + throw cast_error_unable_to_convert_call_arg(a.name, a.type); #endif } m_kwargs[a.name] = a.value; @@ -1301,17 +1316,6 @@ class unpacking_collector { throw type_error("Got multiple values for keyword argument '" + name + "'"); } - [[noreturn]] static void argument_cast_error() { - throw cast_error("Unable to convert call argument to Python object " - "(compile in debug mode for details)"); - } - - [[noreturn]] static void argument_cast_error(const std::string &name, - const std::string &type) { - throw cast_error("Unable to convert call argument '" + name - + "' of type '" + type + "' to Python object"); - } - private: tuple m_args; dict m_kwargs; diff --git a/tests/test_pytypes.py b/tests/test_pytypes.py index f17bf76b77..f234c77315 100644 --- a/tests/test_pytypes.py +++ b/tests/test_pytypes.py @@ -372,10 +372,10 @@ def test_print(capture): with pytest.raises(RuntimeError) as excinfo: m.print_failure() - assert str(excinfo.value) == "make_tuple(): unable to convert " + ( - "argument of type 'UnregisteredType' to Python object" + assert str(excinfo.value) == "Unable to convert call argument " + ( + "'1' of type 'UnregisteredType' to Python object" if debug_enabled - else "arguments to Python object (compile in debug mode for details)" + else "to Python object (compile in debug mode for details)" ) From 6d1b197b4669c70da94c8efb516374e1935d3ea3 Mon Sep 17 00:00:00 2001 From: "Ralf W. Grosse-Kunstleve" Date: Thu, 8 Jul 2021 09:02:48 -0700 Subject: [PATCH 050/638] Splitting out pybind11/stl/filesystem.h. (#3077) * Splitting out pybind11/stl/filesystem.h. To solve breakages like: https://github.com/deepmind/open_spiel/runs/2999582108 Mostly following the suggestion here: https://github.com/pybind/pybind11/pull/2730#issuecomment-750507575 Except using pybind11/stl/filesystem.h instead of pybind11/stlfs.h, as decided via chat. stl.h restored to the exact state before merging PR #2730 via: ``` git checkout 733f8de24feed964f96b639a0a44247f46bed868 stl.h ``` * Properly including new stl subdirectory in pip wheel config. This now passes interactively: ``` pytest tests/extra_python_package/ ``` * iwyu cleanup. iwyuh.py -c -std=c++17 -DPYBIND11_TEST_BOOST -Ipybind11/include -I/usr/include/python3.9 -I/usr/include/eigen3 include/pybind11/stl/filesystem.h * Adding PYBIND11_HAS_FILESYSTEM_IS_OPTIONAL. * Eliminating else after return. --- CMakeLists.txt | 3 +- include/pybind11/stl.h | 81 ------------------ include/pybind11/stl/filesystem.h | 103 +++++++++++++++++++++++ tests/extra_python_package/test_files.py | 7 +- tests/test_stl.cpp | 5 ++ tools/setup_global.py.in | 4 +- tools/setup_main.py.in | 2 + 7 files changed, 121 insertions(+), 84 deletions(-) create mode 100644 include/pybind11/stl/filesystem.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 152763eb42..c988ea0b50 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -125,7 +125,8 @@ set(PYBIND11_HEADERS include/pybind11/pybind11.h include/pybind11/pytypes.h include/pybind11/stl.h - include/pybind11/stl_bind.h) + include/pybind11/stl_bind.h + include/pybind11/stl/filesystem.h) # Compare with grep and warn if mismatched if(PYBIND11_MASTER_PROJECT AND NOT CMAKE_VERSION VERSION_LESS 3.12) diff --git a/include/pybind11/stl.h b/include/pybind11/stl.h index 2350a5247f..ca20b7483b 100644 --- a/include/pybind11/stl.h +++ b/include/pybind11/stl.h @@ -41,21 +41,11 @@ # include # define PYBIND11_HAS_VARIANT 1 # endif -// std::filesystem::path -# if defined(PYBIND11_CPP17) && __has_include() && \ - PY_VERSION_HEX >= 0x03060000 -# include -# define PYBIND11_HAS_FILESYSTEM 1 -# endif #elif defined(_MSC_VER) && defined(PYBIND11_CPP17) # include # include # define PYBIND11_HAS_OPTIONAL 1 # define PYBIND11_HAS_VARIANT 1 -# if PY_VERSION_HEX >= 0x03060000 -# include -# define PYBIND11_HAS_FILESYSTEM 1 -# endif #endif PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE) @@ -387,77 +377,6 @@ template struct type_caster> : variant_caster> { }; #endif -#if defined(PYBIND11_HAS_FILESYSTEM) -template struct path_caster { - -private: - static PyObject* unicode_from_fs_native(const std::string& w) { -#if !defined(PYPY_VERSION) - return PyUnicode_DecodeFSDefaultAndSize(w.c_str(), ssize_t(w.size())); -#else - // PyPy mistakenly declares the first parameter as non-const. - return PyUnicode_DecodeFSDefaultAndSize( - const_cast(w.c_str()), ssize_t(w.size())); -#endif - } - - static PyObject* unicode_from_fs_native(const std::wstring& w) { - return PyUnicode_FromWideChar(w.c_str(), ssize_t(w.size())); - } - -public: - static handle cast(const T& path, return_value_policy, handle) { - if (auto py_str = unicode_from_fs_native(path.native())) { - return module::import("pathlib").attr("Path")(reinterpret_steal(py_str)) - .release(); - } - return nullptr; - } - - bool load(handle handle, bool) { - // PyUnicode_FSConverter and PyUnicode_FSDecoder normally take care of - // calling PyOS_FSPath themselves, but that's broken on PyPy (PyPy - // issue #3168) so we do it ourselves instead. - PyObject* buf = PyOS_FSPath(handle.ptr()); - if (!buf) { - PyErr_Clear(); - return false; - } - PyObject* native = nullptr; - if constexpr (std::is_same_v) { - if (PyUnicode_FSConverter(buf, &native)) { - if (auto c_str = PyBytes_AsString(native)) { - // AsString returns a pointer to the internal buffer, which - // must not be free'd. - value = c_str; - } - } - } else if constexpr (std::is_same_v) { - if (PyUnicode_FSDecoder(buf, &native)) { - if (auto c_str = PyUnicode_AsWideCharString(native, nullptr)) { - // AsWideCharString returns a new string that must be free'd. - value = c_str; // Copies the string. - PyMem_Free(c_str); - } - } - } - Py_XDECREF(native); - Py_DECREF(buf); - if (PyErr_Occurred()) { - PyErr_Clear(); - return false; - } else { - return true; - } - } - - PYBIND11_TYPE_CASTER(T, _("os.PathLike")); -}; - -template<> struct type_caster - : public path_caster {}; -#endif - PYBIND11_NAMESPACE_END(detail) inline std::ostream &operator<<(std::ostream &os, const handle &obj) { diff --git a/include/pybind11/stl/filesystem.h b/include/pybind11/stl/filesystem.h new file mode 100644 index 0000000000..7a8acdb60b --- /dev/null +++ b/include/pybind11/stl/filesystem.h @@ -0,0 +1,103 @@ +// Copyright (c) 2021 The Pybind Development Team. +// All rights reserved. Use of this source code is governed by a +// BSD-style license that can be found in the LICENSE file. + +#pragma once + +#include "../cast.h" +#include "../pybind11.h" +#include "../pytypes.h" + +#include "../detail/common.h" +#include "../detail/descr.h" + +#include + +#ifdef __has_include +# if defined(PYBIND11_CPP17) && __has_include() && \ + PY_VERSION_HEX >= 0x03060000 +# include +# define PYBIND11_HAS_FILESYSTEM 1 +# endif +#endif + +#if !defined(PYBIND11_HAS_FILESYSTEM) && !defined(PYBIND11_HAS_FILESYSTEM_IS_OPTIONAL) +# error \ + "#include is not available. (Use -DPYBIND11_HAS_FILESYSTEM_IS_OPTIONAL to ignore.)" +#endif + +PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE) +PYBIND11_NAMESPACE_BEGIN(detail) + +#if defined(PYBIND11_HAS_FILESYSTEM) +template struct path_caster { + +private: + static PyObject* unicode_from_fs_native(const std::string& w) { +#if !defined(PYPY_VERSION) + return PyUnicode_DecodeFSDefaultAndSize(w.c_str(), ssize_t(w.size())); +#else + // PyPy mistakenly declares the first parameter as non-const. + return PyUnicode_DecodeFSDefaultAndSize( + const_cast(w.c_str()), ssize_t(w.size())); +#endif + } + + static PyObject* unicode_from_fs_native(const std::wstring& w) { + return PyUnicode_FromWideChar(w.c_str(), ssize_t(w.size())); + } + +public: + static handle cast(const T& path, return_value_policy, handle) { + if (auto py_str = unicode_from_fs_native(path.native())) { + return module_::import("pathlib").attr("Path")(reinterpret_steal(py_str)) + .release(); + } + return nullptr; + } + + bool load(handle handle, bool) { + // PyUnicode_FSConverter and PyUnicode_FSDecoder normally take care of + // calling PyOS_FSPath themselves, but that's broken on PyPy (PyPy + // issue #3168) so we do it ourselves instead. + PyObject* buf = PyOS_FSPath(handle.ptr()); + if (!buf) { + PyErr_Clear(); + return false; + } + PyObject* native = nullptr; + if constexpr (std::is_same_v) { + if (PyUnicode_FSConverter(buf, &native)) { + if (auto c_str = PyBytes_AsString(native)) { + // AsString returns a pointer to the internal buffer, which + // must not be free'd. + value = c_str; + } + } + } else if constexpr (std::is_same_v) { + if (PyUnicode_FSDecoder(buf, &native)) { + if (auto c_str = PyUnicode_AsWideCharString(native, nullptr)) { + // AsWideCharString returns a new string that must be free'd. + value = c_str; // Copies the string. + PyMem_Free(c_str); + } + } + } + Py_XDECREF(native); + Py_DECREF(buf); + if (PyErr_Occurred()) { + PyErr_Clear(); + return false; + } + return true; + } + + PYBIND11_TYPE_CASTER(T, _("os.PathLike")); +}; + +template<> struct type_caster + : public path_caster {}; +#endif // PYBIND11_HAS_FILESYSTEM + +PYBIND11_NAMESPACE_END(detail) +PYBIND11_NAMESPACE_END(PYBIND11_NAMESPACE) diff --git a/tests/extra_python_package/test_files.py b/tests/extra_python_package/test_files.py index 064a3e12fe..c1d12ed76b 100644 --- a/tests/extra_python_package/test_files.py +++ b/tests/extra_python_package/test_files.py @@ -46,6 +46,10 @@ "include/pybind11/detail/typeid.h", } +stl_headers = { + "include/pybind11/stl/filesystem.h", +} + cmake_files = { "share/cmake/pybind11/FindPythonLibsNew.cmake", "share/cmake/pybind11/pybind11Common.cmake", @@ -67,7 +71,7 @@ "setup_helpers.pyi", } -headers = main_headers | detail_headers +headers = main_headers | detail_headers | stl_headers src_files = headers | cmake_files all_files = src_files | py_files @@ -77,6 +81,7 @@ "pybind11/include", "pybind11/include/pybind11", "pybind11/include/pybind11/detail", + "pybind11/include/pybind11/stl", "pybind11/share", "pybind11/share/cmake", "pybind11/share/cmake/pybind11", diff --git a/tests/test_stl.cpp b/tests/test_stl.cpp index 7183c56b7a..dc75762e85 100644 --- a/tests/test_stl.cpp +++ b/tests/test_stl.cpp @@ -11,6 +11,11 @@ #include "constructor_stats.h" #include +#ifndef PYBIND11_HAS_FILESYSTEM_IS_OPTIONAL +#define PYBIND11_HAS_FILESYSTEM_IS_OPTIONAL +#endif +#include + #include #include diff --git a/tools/setup_global.py.in b/tools/setup_global.py.in index 4cf040b2d8..8b7e538714 100644 --- a/tools/setup_global.py.in +++ b/tools/setup_global.py.in @@ -33,8 +33,9 @@ class InstallHeadersNested(install_headers): main_headers = glob.glob("pybind11/include/pybind11/*.h") detail_headers = glob.glob("pybind11/include/pybind11/detail/*.h") +stl_headers = glob.glob("pybind11/include/pybind11/stl/*.h") cmake_files = glob.glob("pybind11/share/cmake/pybind11/*.cmake") -headers = main_headers + detail_headers +headers = main_headers + detail_headers + stl_headers cmdclass = {"install_headers": InstallHeadersNested} $extra_cmd @@ -58,6 +59,7 @@ setup( (base + "share/cmake/pybind11", cmake_files), (base + "include/pybind11", main_headers), (base + "include/pybind11/detail", detail_headers), + (base + "include/pybind11/stl", stl_headers), ], cmdclass=cmdclass, ) diff --git a/tools/setup_main.py.in b/tools/setup_main.py.in index 2231a08fdc..bcd4ef4aeb 100644 --- a/tools/setup_main.py.in +++ b/tools/setup_main.py.in @@ -16,12 +16,14 @@ setup( "pybind11", "pybind11.include.pybind11", "pybind11.include.pybind11.detail", + "pybind11.include.pybind11.stl", "pybind11.share.cmake.pybind11", ], package_data={ "pybind11": ["py.typed", "*.pyi"], "pybind11.include.pybind11": ["*.h"], "pybind11.include.pybind11.detail": ["*.h"], + "pybind11.include.pybind11.stl": ["*.h"], "pybind11.share.cmake.pybind11": ["*.cmake"], }, extras_require={ From b5357d1fa8e91ddbfbc2ad057b9a1ccb74becbba Mon Sep 17 00:00:00 2001 From: Aaron Gokaslan Date: Fri, 9 Jul 2021 09:45:53 -0400 Subject: [PATCH 051/638] fix(clang-tidy): Enable clang-tidy else-after-return and redundant void checks (#3080) * Enable clang-tidy else-after-return and redundant void checks * Fix remaining else-after * Address reviewer comments * Fix indentation * Rerun clang-tidy post merge --- .clang-tidy | 3 + include/pybind11/cast.h | 85 ++++++++++++---------- include/pybind11/chrono.h | 4 +- include/pybind11/detail/class.h | 4 +- include/pybind11/detail/type_caster_base.h | 4 +- include/pybind11/eigen.h | 9 +-- include/pybind11/functional.h | 3 +- include/pybind11/numpy.h | 6 +- include/pybind11/pybind11.h | 45 ++++++------ include/pybind11/pytypes.h | 21 ++---- include/pybind11/stl.h | 3 +- include/pybind11/stl_bind.h | 13 ++-- tests/test_callbacks.cpp | 9 ++- tests/test_class.cpp | 3 +- tests/test_copy_move.cpp | 3 +- tests/test_pytypes.cpp | 10 +-- tests/test_virtual_functions.cpp | 4 +- 17 files changed, 114 insertions(+), 115 deletions(-) diff --git a/.clang-tidy b/.clang-tidy index 22a736208c..ea50bf39bc 100644 --- a/.clang-tidy +++ b/.clang-tidy @@ -1,11 +1,13 @@ FormatStyle: file Checks: ' +clang-analyzer-optin.cplusplus.VirtualCall, llvm-namespace-comment, misc-misplaced-const, misc-static-assert, misc-uniqueptr-reset-release, modernize-avoid-bind, +modernize-redundant-void-arg, modernize-replace-auto-ptr, modernize-replace-disallow-copy-and-assign-macro, modernize-shrink-to-fit, @@ -20,6 +22,7 @@ modernize-use-override, modernize-use-using, *performance*, readability-container-size-empty, +readability-else-after-return, readability-make-member-function-const, readability-redundant-function-ptr-dereference, readability-redundant-smartptr-get, diff --git a/include/pybind11/cast.h b/include/pybind11/cast.h index 4a219e2df3..a748c77c0f 100644 --- a/include/pybind11/cast.h +++ b/include/pybind11/cast.h @@ -85,25 +85,28 @@ template class type_caster> { operator std::reference_wrapper() { return cast_op(subcaster); } }; -#define PYBIND11_TYPE_CASTER(type, py_name) \ - protected: \ - type value; \ - public: \ - static constexpr auto name = py_name; \ - template >::value, int> = 0> \ - static handle cast(T_ *src, return_value_policy policy, handle parent) { \ - if (!src) return none().release(); \ - if (policy == return_value_policy::take_ownership) { \ - auto h = cast(std::move(*src), policy, parent); delete src; return h; \ - } else { \ - return cast(*src, policy, parent); \ - } \ - } \ - operator type*() { return &value; } \ - operator type&() { return value; } \ - operator type&&() && { return std::move(value); } \ - template using cast_op_type = pybind11::detail::movable_cast_op_type - +#define PYBIND11_TYPE_CASTER(type, py_name) \ +protected: \ + type value; \ + \ +public: \ + static constexpr auto name = py_name; \ + template >::value, int> = 0> \ + static handle cast(T_ *src, return_value_policy policy, handle parent) { \ + if (!src) \ + return none().release(); \ + if (policy == return_value_policy::take_ownership) { \ + auto h = cast(std::move(*src), policy, parent); \ + delete src; \ + return h; \ + } \ + return cast(*src, policy, parent); \ + } \ + operator type *() { return &value; } \ + operator type &() { return value; } \ + operator type &&() && { return std::move(value); } \ + template \ + using cast_op_type = pybind11::detail::movable_cast_op_type template using is_std_char_type = any_of< std::is_same, /* std::string */ @@ -247,7 +250,8 @@ template <> class type_caster : public type_caster { bool load(handle h, bool) { if (!h) { return false; - } else if (h.is_none()) { + } + if (h.is_none()) { value = nullptr; return true; } @@ -272,8 +276,7 @@ template <> class type_caster : public type_caster { static handle cast(const void *ptr, return_value_policy /* policy */, handle /* parent */) { if (ptr) return capsule(ptr).release(); - else - return none().inc_ref(); + return none().inc_ref(); } template using cast_op_type = void*&; @@ -289,9 +292,15 @@ template <> class type_caster { public: bool load(handle src, bool convert) { if (!src) return false; - else if (src.ptr() == Py_True) { value = true; return true; } - else if (src.ptr() == Py_False) { value = false; return true; } - else if (convert || !std::strcmp("numpy.bool_", Py_TYPE(src.ptr())->tp_name)) { + if (src.ptr() == Py_True) { + value = true; + return true; + } + if (src.ptr() == Py_False) { + value = false; + return true; + } + if (convert || !std::strcmp("numpy.bool_", Py_TYPE(src.ptr())->tp_name)) { // (allow non-implicit conversion for numpy booleans) Py_ssize_t res = -1; @@ -315,9 +324,8 @@ template <> class type_caster { if (res == 0 || res == 1) { value = (bool) res; return true; - } else { - PyErr_Clear(); } + PyErr_Clear(); } return false; } @@ -351,7 +359,8 @@ template struct string_caster { handle load_src = src; if (!src) { return false; - } else if (!PyUnicode_Check(load_src.ptr())) { + } + if (!PyUnicode_Check(load_src.ptr())) { #if PY_MAJOR_VERSION >= 3 return load_bytes(load_src); #else @@ -554,10 +563,11 @@ template class Tuple, typename... Ts> class tuple_caster static handle cast(T *src, return_value_policy policy, handle parent) { if (!src) return none().release(); if (policy == return_value_policy::take_ownership) { - auto h = cast(std::move(*src), policy, parent); delete src; return h; - } else { - return cast(*src, policy, parent); + auto h = cast(std::move(*src), policy, parent); + delete src; + return h; } + return cast(*src, policy, parent); } static constexpr auto name = _("Tuple[") + concat(make_caster::name...) + _("]"); @@ -664,14 +674,14 @@ struct copyable_holder_caster : public type_caster_base { value = v_h.value_ptr(); holder = v_h.template holder(); return true; - } else { - throw cast_error("Unable to cast from non-held to held instance (T& to Holder) " + } + throw cast_error("Unable to cast from non-held to held instance (T& to Holder) " #if defined(NDEBUG) - "(compile in debug mode for type information)"); + "(compile in debug mode for type information)"); #else - "of type '" + type_id() + "''"); + "of type '" + + type_id() + "''"); #endif - } } template ::value, int> = 0> @@ -917,8 +927,7 @@ template detail::enable_if_t::value, T> cast template detail::enable_if_t::value, T> cast(object &&object) { if (object.ref_count() > 1) return cast(object); - else - return move(std::move(object)); + return move(std::move(object)); } template detail::enable_if_t::value, T> cast(object &&object) { return cast(object); diff --git a/include/pybind11/chrono.h b/include/pybind11/chrono.h index 317afd1837..d32b5b0569 100644 --- a/include/pybind11/chrono.h +++ b/include/pybind11/chrono.h @@ -53,11 +53,11 @@ template class duration_caster { return true; } // If invoked with a float we assume it is seconds and convert - else if (PyFloat_Check(src.ptr())) { + if (PyFloat_Check(src.ptr())) { value = type(duration_cast>(duration(PyFloat_AsDouble(src.ptr())))); return true; } - else return false; + return false; } // If this is a duration just return it back diff --git a/include/pybind11/detail/class.h b/include/pybind11/detail/class.h index ca6854e7bf..5fee3318b0 100644 --- a/include/pybind11/detail/class.h +++ b/include/pybind11/detail/class.h @@ -162,9 +162,7 @@ extern "C" inline PyObject *pybind11_meta_getattro(PyObject *obj, PyObject *name Py_INCREF(descr); return descr; } - else { - return PyType_Type.tp_getattro(obj, name); - } + return PyType_Type.tp_getattro(obj, name); } #endif diff --git a/include/pybind11/detail/type_caster_base.h b/include/pybind11/detail/type_caster_base.h index a8d3938c7c..451690dc73 100644 --- a/include/pybind11/detail/type_caster_base.h +++ b/include/pybind11/detail/type_caster_base.h @@ -670,7 +670,7 @@ class type_caster_generic { return true; } // Case 2: We have a derived class - else if (PyType_IsSubtype(srctype, typeinfo->type)) { + if (PyType_IsSubtype(srctype, typeinfo->type)) { auto &bases = all_type_info(srctype); bool no_cpp_mi = typeinfo->simple_type; @@ -687,7 +687,7 @@ class type_caster_generic { // Case 2b: the python type inherits from multiple C++ bases. Check the bases to see if // we can find an exact match (or, for a simple C++ type, an inherited match); if so, we // can safely reinterpret_cast to the relevant pointer. - else if (bases.size() > 1) { + if (bases.size() > 1) { for (auto base : bases) { if (no_cpp_mi ? PyType_IsSubtype(base->type, typeinfo->type) : base->type == typeinfo->type) { this_.load_value(reinterpret_cast(src.ptr())->get_value_and_holder(base)); diff --git a/include/pybind11/eigen.h b/include/pybind11/eigen.h index e8c6f63391..218fe27034 100644 --- a/include/pybind11/eigen.h +++ b/include/pybind11/eigen.h @@ -169,21 +169,18 @@ template struct EigenProps { return false; // Vector size mismatch return {rows == 1 ? 1 : n, cols == 1 ? 1 : n, stride}; } - else if (fixed) { + if (fixed) { // The type has a fixed size, but is not a vector: abort return false; } - else if (fixed_cols) { + if (fixed_cols) { // Since this isn't a vector, cols must be != 1. We allow this only if it exactly // equals the number of elements (rows is Dynamic, and so 1 row is allowed). if (cols != n) return false; return {1, n, stride}; - } - else { - // Otherwise it's either fully dynamic, or column dynamic; both become a column vector + } // Otherwise it's either fully dynamic, or column dynamic; both become a column vector if (fixed_rows && rows != n) return false; return {n, 1, stride}; - } } static constexpr bool show_writeable = is_eigen_dense_map::value && is_eigen_mutable_map::value; diff --git a/include/pybind11/functional.h b/include/pybind11/functional.h index aee9be4e45..cc7099c6a3 100644 --- a/include/pybind11/functional.h +++ b/include/pybind11/functional.h @@ -98,8 +98,7 @@ struct type_caster> { auto result = f_.template target(); if (result) return cpp_function(*result, policy).release(); - else - return cpp_function(std::forward(f_), policy).release(); + return cpp_function(std::forward(f_), policy).release(); } PYBIND11_TYPE_CASTER(type, _("Callable[[") + concat(make_caster::name...) + _("], ") diff --git a/include/pybind11/numpy.h b/include/pybind11/numpy.h index fdb7a9794b..edba8bac92 100644 --- a/include/pybind11/numpy.h +++ b/include/pybind11/numpy.h @@ -1340,9 +1340,8 @@ template class multi_array_iterator { if (++m_index[i] != m_shape[i]) { increment_common_iterator(i); break; - } else { - m_index[i] = 0; } + m_index[i] = 0; } return *this; } @@ -1493,8 +1492,7 @@ struct vectorize_returned_array { static Type create(broadcast_trivial trivial, const std::vector &shape) { if (trivial == broadcast_trivial::f_trivial) return array_t(shape); - else - return array_t(shape); + return array_t(shape); } static Return *mutable_data(Type &array) { diff --git a/include/pybind11/pybind11.h b/include/pybind11/pybind11.h index 4d7bc5c73e..616fa70255 100644 --- a/include/pybind11/pybind11.h +++ b/include/pybind11/pybind11.h @@ -396,7 +396,7 @@ class cpp_function : public function { std::memset(rec->def, 0, sizeof(PyMethodDef)); rec->def->ml_name = rec->name; rec->def->ml_meth - = reinterpret_cast(reinterpret_cast(dispatcher)); + = reinterpret_cast(reinterpret_cast(dispatcher)); rec->def->ml_flags = METH_VARARGS | METH_KEYWORDS; capsule rec_capsule(unique_rec.release(), [](void *ptr) { @@ -924,20 +924,20 @@ class cpp_function : public function { append_note_if_missing_header_is_suspected(msg); PyErr_SetString(PyExc_TypeError, msg.c_str()); return nullptr; - } else if (!result) { + } + if (!result) { std::string msg = "Unable to convert function return value to a " "Python type! The signature was\n\t"; msg += it->signature; append_note_if_missing_header_is_suspected(msg); PyErr_SetString(PyExc_TypeError, msg.c_str()); return nullptr; - } else { - if (overloads->is_constructor && !self_value_and_holder.holder_constructed()) { - auto *pi = reinterpret_cast(parent.ptr()); - self_value_and_holder.type->init_instance(pi, nullptr); - } - return result.ptr(); } + if (overloads->is_constructor && !self_value_and_holder.holder_constructed()) { + auto *pi = reinterpret_cast(parent.ptr()); + self_value_and_holder.type->init_instance(pi, nullptr); + } + return result.ptr(); } }; @@ -1860,9 +1860,9 @@ PYBIND11_NOINLINE inline void keep_alive_impl(size_t Nurse, size_t Patient, func auto get_arg = [&](size_t n) { if (n == 0) return ret; - else if (n == 1 && call.init_self) + if (n == 1 && call.init_self) return call.init_self; - else if (n <= call.args.size()) + if (n <= call.args.size()) return call.args[n - 1]; return handle(); }; @@ -2186,18 +2186,19 @@ template function get_override(const T *this_ptr, const char *name) { return tinfo ? detail::get_type_override(this_ptr, tinfo, name) : function(); } -#define PYBIND11_OVERRIDE_IMPL(ret_type, cname, name, ...) \ - do { \ - pybind11::gil_scoped_acquire gil; \ - pybind11::function override = pybind11::get_override(static_cast(this), name); \ - if (override) { \ - auto o = override(__VA_ARGS__); \ - if (pybind11::detail::cast_is_temporary_value_reference::value) { \ - static pybind11::detail::override_caster_t caster; \ - return pybind11::detail::cast_ref(std::move(o), caster); \ - } \ - else return pybind11::detail::cast_safe(std::move(o)); \ - } \ +#define PYBIND11_OVERRIDE_IMPL(ret_type, cname, name, ...) \ + do { \ + pybind11::gil_scoped_acquire gil; \ + pybind11::function override \ + = pybind11::get_override(static_cast(this), name); \ + if (override) { \ + auto o = override(__VA_ARGS__); \ + if (pybind11::detail::cast_is_temporary_value_reference::value) { \ + static pybind11::detail::override_caster_t caster; \ + return pybind11::detail::cast_ref(std::move(o), caster); \ + } \ + return pybind11::detail::cast_safe(std::move(o)); \ + } \ } while (false) /** \rst diff --git a/include/pybind11/pytypes.h b/include/pybind11/pytypes.h index 80637fb980..fb9680dfa1 100644 --- a/include/pybind11/pytypes.h +++ b/include/pybind11/pytypes.h @@ -440,19 +440,17 @@ inline object getattr(handle obj, const char *name) { inline object getattr(handle obj, handle name, handle default_) { if (PyObject *result = PyObject_GetAttr(obj.ptr(), name.ptr())) { return reinterpret_steal(result); - } else { - PyErr_Clear(); - return reinterpret_borrow(default_); } + PyErr_Clear(); + return reinterpret_borrow(default_); } inline object getattr(handle obj, const char *name, handle default_) { if (PyObject *result = PyObject_GetAttrString(obj.ptr(), name)) { return reinterpret_steal(result); - } else { - PyErr_Clear(); - return reinterpret_borrow(default_); } + PyErr_Clear(); + return reinterpret_borrow(default_); } inline void setattr(handle obj, handle name, handle value) { @@ -791,10 +789,9 @@ inline bool PyIterable_Check(PyObject *obj) { if (iter) { Py_DECREF(iter); return true; - } else { - PyErr_Clear(); - return false; } + PyErr_Clear(); + return false; } inline bool PyNone_Check(PyObject *o) { return o == Py_None; } @@ -1188,10 +1185,8 @@ Unsigned as_unsigned(PyObject *o) { unsigned long v = PyLong_AsUnsignedLong(o); return v == (unsigned long) -1 && PyErr_Occurred() ? (Unsigned) -1 : (Unsigned) v; } - else { - unsigned long long v = PyLong_AsUnsignedLongLong(o); - return v == (unsigned long long) -1 && PyErr_Occurred() ? (Unsigned) -1 : (Unsigned) v; - } + unsigned long long v = PyLong_AsUnsignedLongLong(o); + return v == (unsigned long long) -1 && PyErr_Occurred() ? (Unsigned) -1 : (Unsigned) v; } PYBIND11_NAMESPACE_END(detail) diff --git a/include/pybind11/stl.h b/include/pybind11/stl.h index ca20b7483b..51994c656b 100644 --- a/include/pybind11/stl.h +++ b/include/pybind11/stl.h @@ -278,7 +278,8 @@ template struct optional_caster { bool load(handle src, bool convert) { if (!src) { return false; - } else if (src.is_none()) { + } + if (src.is_none()) { return true; // default-constructed value is already empty } value_conv inner_caster; diff --git a/include/pybind11/stl_bind.h b/include/pybind11/stl_bind.h index b78bd27ced..76700763fe 100644 --- a/include/pybind11/stl_bind.h +++ b/include/pybind11/stl_bind.h @@ -414,13 +414,12 @@ void vector_buffer_impl(Class_& cl, std::true_type) { if (step == 1) { return Vector(p, end); } - else { - Vector vec; - vec.reserve((size_t) info.shape[0]); - for (; p != end; p += step) - vec.push_back(*p); - return vec; - } + Vector vec; + vec.reserve((size_t) info.shape[0]); + for (; p != end; p += step) + vec.push_back(*p); + return vec; + })); return; diff --git a/tests/test_callbacks.cpp b/tests/test_callbacks.cpp index 908d5743e7..7e79217a3d 100644 --- a/tests/test_callbacks.cpp +++ b/tests/test_callbacks.cpp @@ -82,7 +82,7 @@ TEST_SUBMODULE(callbacks, m) { // Export the payload constructor statistics for testing purposes: m.def("payload_cstats", &ConstructorStats::get); /* Test cleanup of lambda closure */ - m.def("test_cleanup", []() -> std::function { + m.def("test_cleanup", []() -> std::function { Payload p; return [p]() { @@ -108,12 +108,13 @@ TEST_SUBMODULE(callbacks, m) { if (!result) { auto r = f(1); return "can't convert to function pointer: eval(1) = " + std::to_string(r); - } else if (*result == dummy_function) { + } + if (*result == dummy_function) { auto r = (*result)(1); return "matches dummy_function: eval(1) = " + std::to_string(r); - } else { - return "argument does NOT match dummy_function. This should never happen!"; } + return "argument does NOT match dummy_function. This should never happen!"; + }); class AbstractBase { diff --git a/tests/test_class.cpp b/tests/test_class.cpp index a26c59c1ce..bcc73ef0b8 100644 --- a/tests/test_class.cpp +++ b/tests/test_class.cpp @@ -154,8 +154,7 @@ TEST_SUBMODULE(class_, m) { // return py::type::of(); if (category == 1) return py::type::of(); - else - return py::type::of(); + return py::type::of(); }); m.def("get_type_of", [](py::object ob) { return py::type::of(std::move(ob)); }); diff --git a/tests/test_copy_move.cpp b/tests/test_copy_move.cpp index 08f4dd7497..05caf28ff1 100644 --- a/tests/test_copy_move.cpp +++ b/tests/test_copy_move.cpp @@ -203,8 +203,7 @@ TEST_SUBMODULE(copy_move_policies, m) { void *ptr = std::malloc(bytes); if (ptr) return ptr; - else - throw std::bad_alloc{}; + throw std::bad_alloc{}; } }; py::class_(m, "PrivateOpNew").def_readonly("value", &PrivateOpNew::value); diff --git a/tests/test_pytypes.cpp b/tests/test_pytypes.cpp index 9e0ebc206f..2b91af37b0 100644 --- a/tests/test_pytypes.cpp +++ b/tests/test_pytypes.cpp @@ -263,13 +263,13 @@ TEST_SUBMODULE(pytypes, m) { if (type == "bytes") { return move ? py::bytes(std::move(value)) : py::bytes(value); } - else if (type == "none") { + if (type == "none") { return move ? py::none(std::move(value)) : py::none(value); } - else if (type == "ellipsis") { + if (type == "ellipsis") { return move ? py::ellipsis(std::move(value)) : py::ellipsis(value); } - else if (type == "type") { + if (type == "type") { return move ? py::type(std::move(value)) : py::type(value); } throw std::runtime_error("Invalid type"); @@ -385,9 +385,7 @@ TEST_SUBMODULE(pytypes, m) { if (is_unsigned) return py::memoryview::from_buffer( ui16, { 4 }, { sizeof(uint16_t) }); - else - return py::memoryview::from_buffer( - si16, { 5 }, { sizeof(int16_t) }); + return py::memoryview::from_buffer(si16, {5}, {sizeof(int16_t)}); }); m.def("test_memoryview_from_buffer_nativeformat", []() { diff --git a/tests/test_virtual_functions.cpp b/tests/test_virtual_functions.cpp index 51068f6380..5280af8eb6 100644 --- a/tests/test_virtual_functions.cpp +++ b/tests/test_virtual_functions.cpp @@ -112,7 +112,9 @@ class NonCopyable { void operator=(const NonCopyable &) = delete; void operator=(NonCopyable &&) = delete; std::string get_value() const { - if (value) return std::to_string(*value); else return "(null)"; + if (value) + return std::to_string(*value); + return "(null)"; } ~NonCopyable() { print_destroyed(this); } From bac5a0c3700f1ee7b4fcc98659f3c924e915322f Mon Sep 17 00:00:00 2001 From: "Ralf W. Grosse-Kunstleve" Date: Fri, 9 Jul 2021 14:09:56 -0700 Subject: [PATCH 052/638] Go all the way fixing clang-tidy issues to avoid the NOLINTNEXTLINE clutter and clang-format issues. This was really meant to be part of PR #3051 but was held back either out of an abundance of caution, or because of confusion caused by stray semicolons. (#3086) --- tests/pybind11_cross_module_tests.cpp | 10 +++++++--- tests/test_builtin_casters.cpp | 10 ++++++---- tests/test_kwargs_and_defaults.cpp | 13 +++++++++---- tests/test_local_bindings.cpp | 10 +++++++--- 4 files changed, 29 insertions(+), 14 deletions(-) diff --git a/tests/pybind11_cross_module_tests.cpp b/tests/pybind11_cross_module_tests.cpp index f238ddb042..27a349d231 100644 --- a/tests/pybind11_cross_module_tests.cpp +++ b/tests/pybind11_cross_module_tests.cpp @@ -10,8 +10,11 @@ #include "pybind11_tests.h" #include "local_bindings.h" #include "test_exceptions.h" + #include + #include +#include PYBIND11_MODULE(pybind11_cross_module_tests, m) { m.doc() = "pybind11 cross-module test module"; @@ -104,9 +107,10 @@ PYBIND11_MODULE(pybind11_cross_module_tests, m) { m.def("return_self", [](LocalVec *v) { return v; }); m.def("return_copy", [](const LocalVec &v) { return LocalVec(v); }); - // Changing this broke things with pygrep. TODO fix - // NOLINTNEXTLINE - class Dog : public pets::Pet { public: Dog(std::string name) : Pet(name) {}; }; + class Dog : public pets::Pet { + public: + Dog(std::string name) : Pet(std::move(name)) {} + }; py::class_(m, "Pet", py::module_local()) .def("name", &pets::Pet::name); // Binding for local extending class: diff --git a/tests/test_builtin_casters.cpp b/tests/test_builtin_casters.cpp index a98b67bdbd..caafb06314 100644 --- a/tests/test_builtin_casters.cpp +++ b/tests/test_builtin_casters.cpp @@ -151,10 +151,12 @@ TEST_SUBMODULE(builtin_casters, m) { m.def("int_passthrough_noconvert", [](int arg) { return arg; }, py::arg{}.noconvert()); // test_tuple - // NOLINTNEXTLINE(performance-unnecessary-value-param) - m.def("pair_passthrough", [](std::pair input) { - return std::make_pair(input.second, input.first); - }, "Return a pair in reversed order"); + m.def( + "pair_passthrough", + [](const std::pair &input) { + return std::make_pair(input.second, input.first); + }, + "Return a pair in reversed order"); m.def("tuple_passthrough", [](std::tuple input) { return std::make_tuple(std::get<2>(input), std::get<1>(input), std::get<0>(input)); }, "Return a triple in reversed order"); diff --git a/tests/test_kwargs_and_defaults.cpp b/tests/test_kwargs_and_defaults.cpp index a349d43184..3e078bd9ba 100644 --- a/tests/test_kwargs_and_defaults.cpp +++ b/tests/test_kwargs_and_defaults.cpp @@ -106,10 +106,15 @@ TEST_SUBMODULE(kwargs_and_defaults, m) { py::arg() = 3, "j"_a = 4, py::kw_only(), "k"_a = 5, "z"_a); m.def("kw_only_mixed", [](int i, int j) { return py::make_tuple(i, j); }, "i"_a, py::kw_only(), "j"_a); - // NOLINTNEXTLINE(performance-unnecessary-value-param) - m.def("kw_only_plus_more", [](int i, int j, int k, py::kwargs kwargs) { - return py::make_tuple(i, j, k, kwargs); }, - py::arg() /* positional */, py::arg("j") = -1 /* both */, py::kw_only(), py::arg("k") /* kw-only */); + m.def( + "kw_only_plus_more", + [](int i, int j, int k, const py::kwargs &kwargs) { + return py::make_tuple(i, j, k, kwargs); + }, + py::arg() /* positional */, + py::arg("j") = -1 /* both */, + py::kw_only(), + py::arg("k") /* kw-only */); m.def("register_invalid_kw_only", [](py::module_ m) { m.def("bad_kw_only", [](int i, int j) { return py::make_tuple(i, j); }, diff --git a/tests/test_local_bindings.cpp b/tests/test_local_bindings.cpp index 2ac77b143b..bfbab3ed3b 100644 --- a/tests/test_local_bindings.cpp +++ b/tests/test_local_bindings.cpp @@ -10,9 +10,12 @@ #include "pybind11_tests.h" #include "local_bindings.h" + #include #include + #include +#include TEST_SUBMODULE(local_bindings, m) { // test_load_external @@ -86,9 +89,10 @@ TEST_SUBMODULE(local_bindings, m) { m.def("return_self", [](LocalVec *v) { return v; }); m.def("return_copy", [](const LocalVec &v) { return LocalVec(v); }); - // Reformatting this class broke pygrep checks - // NOLINTNEXTLINE - class Cat : public pets::Pet { public: Cat(std::string name) : Pet(name) {}; }; + class Cat : public pets::Pet { + public: + Cat(std::string name) : Pet(std::move(name)) {} + }; py::class_(m, "Pet", py::module_local()) .def("get_name", &pets::Pet::name); // Binding for local extending class: From cf006af2f06810b10e2ff0fa8281eb25642e9122 Mon Sep 17 00:00:00 2001 From: Jan Iwaszkiewicz Date: Sat, 10 Jul 2021 20:16:07 +0200 Subject: [PATCH 053/638] Fix typos and docs style (#3088) * py::pickle typo * correct dots and parentheses --- docs/advanced/classes.rst | 2 +- docs/faq.rst | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/docs/advanced/classes.rst b/docs/advanced/classes.rst index 3dfc0c935b..9d1b1f0d9b 100644 --- a/docs/advanced/classes.rst +++ b/docs/advanced/classes.rst @@ -804,7 +804,7 @@ to bind these two functions: } )); -The ``__setstate__`` part of the ``py::picke()`` definition follows the same +The ``__setstate__`` part of the ``py::pickle()`` definition follows the same rules as the single-argument version of ``py::init()``. The return type can be a value, pointer or holder type. See :ref:`custom_constructors` for details. diff --git a/docs/faq.rst b/docs/faq.rst index 56ded0329a..d6a048b06f 100644 --- a/docs/faq.rst +++ b/docs/faq.rst @@ -5,7 +5,7 @@ Frequently asked questions =========================================================== 1. Make sure that the name specified in PYBIND11_MODULE is identical to the -filename of the extension library (without suffixes such as .so) +filename of the extension library (without suffixes such as ``.so``). 2. If the above did not fix the issue, you are likely using an incompatible version of Python (for instance, the extension library was compiled against @@ -170,7 +170,7 @@ complete independence of the symbols involved when not using ``-fvisibility=hidden``. Additionally, ``-fvisibility=hidden`` can deliver considerably binary size -savings. (See the following section for more details). +savings. (See the following section for more details.) .. _`faq:symhidden`: From 0f4761b44ae2a9b7caf2f95e26dfc62178a2a776 Mon Sep 17 00:00:00 2001 From: "Ralf W. Grosse-Kunstleve" Date: Sun, 11 Jul 2021 00:40:38 -0700 Subject: [PATCH 054/638] Rollback of DOWNLOAD_CATCH=OFF change merged via PR #3059. (#3092) --- .github/workflows/ci.yml | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 2fa4303d9b..03c9225c12 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -100,7 +100,7 @@ jobs: run: > cmake -S . -B . -DPYBIND11_WERROR=ON - -DDOWNLOAD_CATCH=OFF + -DDOWNLOAD_CATCH=ON -DDOWNLOAD_EIGEN=ON -DCMAKE_CXX_STANDARD=11 ${{ matrix.args }} @@ -111,10 +111,10 @@ jobs: - name: Python tests C++11 run: cmake --build . --target pytest -j 2 - #- name: C++11 tests - # # TODO: Figure out how to load the DLL on Python 3.8+ - # if: "!(runner.os == 'Windows' && (matrix.python == 3.8 || matrix.python == 3.9 || matrix.python == '3.10-dev'))" - # run: cmake --build . --target cpptest -j 2 + - name: C++11 tests + # TODO: Figure out how to load the DLL on Python 3.8+ + if: "!(runner.os == 'Windows' && (matrix.python == 3.8 || matrix.python == 3.9 || matrix.python == '3.10-dev'))" + run: cmake --build . --target cpptest -j 2 - name: Interface test C++11 run: cmake --build . --target test_cmake_build @@ -127,7 +127,7 @@ jobs: run: > cmake -S . -B build2 -DPYBIND11_WERROR=ON - -DDOWNLOAD_CATCH=OFF + -DDOWNLOAD_CATCH=ON -DDOWNLOAD_EIGEN=ON -DCMAKE_CXX_STANDARD=17 ${{ matrix.args }} @@ -139,10 +139,10 @@ jobs: - name: Python tests run: cmake --build build2 --target pytest - #- name: C++ tests - # # TODO: Figure out how to load the DLL on Python 3.8+ - # if: "!(runner.os == 'Windows' && (matrix.python == 3.8 || matrix.python == 3.9 || matrix.python == '3.10-dev'))" - # run: cmake --build build2 --target cpptest + - name: C++ tests + # TODO: Figure out how to load the DLL on Python 3.8+ + if: "!(runner.os == 'Windows' && (matrix.python == 3.8 || matrix.python == 3.9 || matrix.python == '3.10-dev'))" + run: cmake --build build2 --target cpptest - name: Interface test run: cmake --build build2 --target test_cmake_build @@ -754,7 +754,7 @@ jobs: cmake -S . -B build -G "Visual Studio 16 2019" -A Win32 -DPYBIND11_WERROR=ON - -DDOWNLOAD_CATCH=OFF + -DDOWNLOAD_CATCH=ON -DDOWNLOAD_EIGEN=ON ${{ matrix.args }} - name: Build C++11 @@ -800,7 +800,7 @@ jobs: cmake -S . -B build -G "Visual Studio 14 2015" -A x64 -DPYBIND11_WERROR=ON - -DDOWNLOAD_CATCH=OFF + -DDOWNLOAD_CATCH=ON -DDOWNLOAD_EIGEN=ON - name: Build C++14 @@ -849,7 +849,7 @@ jobs: cmake -S . -B build -G "Visual Studio 15 2017" -A x64 -DPYBIND11_WERROR=ON - -DDOWNLOAD_CATCH=OFF + -DDOWNLOAD_CATCH=ON -DDOWNLOAD_EIGEN=ON -DCMAKE_CXX_STANDARD=${{ matrix.std }} ${{ matrix.args }} From 7a64b8adcc7d74b7f6ce601db8b47a00e4cf107c Mon Sep 17 00:00:00 2001 From: Henry Schreiner Date: Mon, 12 Jul 2021 14:10:46 -0400 Subject: [PATCH 055/638] docs: fix script issues for changelog compilation (#3100) [skip ci] --- tools/make_changelog.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/tools/make_changelog.py b/tools/make_changelog.py index 609ce2f194..4e8fbf5b00 100755 --- a/tools/make_changelog.py +++ b/tools/make_changelog.py @@ -27,7 +27,10 @@ api = ghapi.all.GhApi(owner="pybind", repo="pybind11") -issues = api.issues.list_for_repo(labels="needs changelog", state="closed") +issues_pages = ghapi.page.paged( + api.issues.list_for_repo, labels="needs changelog", state="closed" +) +issues = (issue for page in issues_pages for issue in page) missing = [] for issue in issues: @@ -41,7 +44,7 @@ msg += f"\n `#{issue.number} <{issue.html_url}>`_" - print(Syntax(msg, "rst", theme="ansi_light")) + print(Syntax(msg, "rst", theme="ansi_light", word_wrap=True)) print() else: From 2d468697d9c1a6209c2ccf13f3b57ba548230655 Mon Sep 17 00:00:00 2001 From: "Ralf W. Grosse-Kunstleve" Date: Mon, 12 Jul 2021 13:10:28 -0700 Subject: [PATCH 056/638] NOLINT reduction (#3096) * Copying from prework_no_rst branch (PR #3087): test_numpy_array.cpp, test_stl.cpp * Manual changes reducing NOLINTs. * clang-format-diff.py * Minor adjustment to avoid MSVC warning C4702: unreachable code --- tests/pybind11_cross_module_tests.cpp | 3 +- tests/test_buffers.cpp | 3 +- tests/test_builtin_casters.cpp | 15 ++-- tests/test_class.cpp | 9 +- tests/test_copy_move.cpp | 4 +- tests/test_eigen.cpp | 52 +++++------ tests/test_exceptions.cpp | 9 +- tests/test_kwargs_and_defaults.cpp | 10 +-- tests/test_methods_and_attributes.cpp | 13 ++- tests/test_multiple_inheritance.cpp | 3 +- tests/test_numpy_array.cpp | 124 ++++++++++---------------- tests/test_numpy_vectorize.cpp | 24 +++-- tests/test_pytypes.cpp | 62 ++++++------- tests/test_stl.cpp | 14 ++- 14 files changed, 140 insertions(+), 205 deletions(-) diff --git a/tests/pybind11_cross_module_tests.cpp b/tests/pybind11_cross_module_tests.cpp index 27a349d231..e79701c6c6 100644 --- a/tests/pybind11_cross_module_tests.cpp +++ b/tests/pybind11_cross_module_tests.cpp @@ -132,7 +132,6 @@ PYBIND11_MODULE(pybind11_cross_module_tests, m) { // test_missing_header_message // The main module already includes stl.h, but we need to test the error message // which appears when this header is missing. - // NOLINTNEXTLINE(performance-unnecessary-value-param) - m.def("missing_header_arg", [](std::vector) { }); + m.def("missing_header_arg", [](const std::vector &) {}); m.def("missing_header_return", []() { return std::vector(); }); } diff --git a/tests/test_buffers.cpp b/tests/test_buffers.cpp index 9902c1084e..15dd4f1c74 100644 --- a/tests/test_buffers.cpp +++ b/tests/test_buffers.cpp @@ -79,8 +79,7 @@ TEST_SUBMODULE(buffers, m) { py::class_(m, "Matrix", py::buffer_protocol()) .def(py::init()) /// Construct from a buffer - // NOLINTNEXTLINE(performance-unnecessary-value-param) - .def(py::init([](py::buffer const b) { + .def(py::init([](const py::buffer &b) { py::buffer_info info = b.request(); if (info.format != py::format_descriptor::format() || info.ndim != 2) throw std::runtime_error("Incompatible buffer format!"); diff --git a/tests/test_builtin_casters.cpp b/tests/test_builtin_casters.cpp index caafb06314..51e410cadd 100644 --- a/tests/test_builtin_casters.cpp +++ b/tests/test_builtin_casters.cpp @@ -105,8 +105,7 @@ TEST_SUBMODULE(builtin_casters, m) { // test_bytes_to_string m.def("strlen", [](char *s) { return strlen(s); }); - // NOLINTNEXTLINE(performance-unnecessary-value-param) - m.def("string_length", [](std::string s) { return s.length(); }); + m.def("string_length", [](const std::string &s) { return s.length(); }); #ifdef PYBIND11_HAS_U8STRING m.attr("has_u8string") = true; @@ -185,14 +184,11 @@ TEST_SUBMODULE(builtin_casters, m) { // test_none_deferred m.def("defer_none_cstring", [](char *) { return false; }); - // NOLINTNEXTLINE(performance-unnecessary-value-param) - m.def("defer_none_cstring", [](py::none) { return true; }); + m.def("defer_none_cstring", [](const py::none &) { return true; }); m.def("defer_none_custom", [](UserType *) { return false; }); - // NOLINTNEXTLINE(performance-unnecessary-value-param) - m.def("defer_none_custom", [](py::none) { return true; }); + m.def("defer_none_custom", [](const py::none &) { return true; }); m.def("nodefer_none_void", [](void *) { return true; }); - // NOLINTNEXTLINE(performance-unnecessary-value-param) - m.def("nodefer_none_void", [](py::none) { return false; }); + m.def("nodefer_none_void", [](const py::none &) { return false; }); // test_void_caster m.def("load_nullptr_t", [](std::nullptr_t) {}); // not useful, but it should still compile @@ -242,8 +238,7 @@ TEST_SUBMODULE(builtin_casters, m) { }, "copy"_a); m.def("refwrap_iiw", [](const IncType &w) { return w.value(); }); - // NOLINTNEXTLINE(performance-unnecessary-value-param) - m.def("refwrap_call_iiw", [](IncType &w, py::function f) { + m.def("refwrap_call_iiw", [](IncType &w, const py::function &f) { py::list l; l.append(f(std::ref(w))); l.append(f(std::cref(w))); diff --git a/tests/test_class.cpp b/tests/test_class.cpp index bcc73ef0b8..73de2a61aa 100644 --- a/tests/test_class.cpp +++ b/tests/test_class.cpp @@ -131,8 +131,7 @@ TEST_SUBMODULE(class_, m) { m.def("return_none", []() -> BaseClass* { return nullptr; }); // test_isinstance - // NOLINTNEXTLINE(performance-unnecessary-value-param) - m.def("check_instances", [](py::list l) { + m.def("check_instances", [](const py::list &l) { return py::make_tuple( py::isinstance(l[0]), py::isinstance(l[1]), @@ -217,8 +216,7 @@ TEST_SUBMODULE(class_, m) { py::implicitly_convertible(); m.def("implicitly_convert_argument", [](const ConvertibleFromUserType &r) { return r.i; }); - // NOLINTNEXTLINE(performance-unnecessary-value-param) - m.def("implicitly_convert_variable", [](py::object o) { + m.def("implicitly_convert_variable", [](const py::object &o) { // `o` is `UserType` and `r` is a reference to a temporary created by implicit // conversion. This is valid when called inside a bound function because the temp // object is attached to the same life support system as the arguments. @@ -396,8 +394,7 @@ TEST_SUBMODULE(class_, m) { struct StringWrapper { std::string str; }; m.def("test_error_after_conversions", [](int) {}); m.def("test_error_after_conversions", - // NOLINTNEXTLINE(performance-unnecessary-value-param) - [](StringWrapper) -> NotRegistered { return {}; }); + [](const StringWrapper &) -> NotRegistered { return {}; }); py::class_(m, "StringWrapper").def(py::init()); py::implicitly_convertible(); diff --git a/tests/test_copy_move.cpp b/tests/test_copy_move.cpp index 05caf28ff1..859da9df7e 100644 --- a/tests/test_copy_move.cpp +++ b/tests/test_copy_move.cpp @@ -126,7 +126,7 @@ TEST_SUBMODULE(copy_move_policies, m) { // test_move_and_copy_casts // NOLINTNEXTLINE(performance-unnecessary-value-param) - m.def("move_and_copy_casts", [](py::object o) { + m.def("move_and_copy_casts", [](const py::object &o) { int r = 0; r += py::cast(o).value; /* moves */ r += py::cast(o).value; /* moves */ @@ -141,8 +141,10 @@ TEST_SUBMODULE(copy_move_policies, m) { // test_move_and_copy_loads m.def("move_only", [](MoveOnlyInt m) { return m.value; }); + // Changing this breaks the existing test: needs careful review. // NOLINTNEXTLINE(performance-unnecessary-value-param) m.def("move_or_copy", [](MoveOrCopyInt m) { return m.value; }); + // Changing this breaks the existing test: needs careful review. // NOLINTNEXTLINE(performance-unnecessary-value-param) m.def("copy_only", [](CopyOnlyInt m) { return m.value; }); m.def("move_pair", [](std::pair p) { diff --git a/tests/test_eigen.cpp b/tests/test_eigen.cpp index a24bf273d2..ba74f58156 100644 --- a/tests/test_eigen.cpp +++ b/tests/test_eigen.cpp @@ -98,12 +98,13 @@ TEST_SUBMODULE(eigen, m) { // test_eigen_ref_to_python // Different ways of passing via Eigen::Ref; the first and second are the Eigen-recommended - // NOLINTNEXTLINE (performance-unnecessary-value-param) - m.def("cholesky1", [](Eigen::Ref x) -> Eigen::MatrixXd { return x.llt().matrixL(); }); + m.def("cholesky1", + [](const Eigen::Ref &x) -> Eigen::MatrixXd { return x.llt().matrixL(); }); m.def("cholesky2", [](const Eigen::Ref &x) -> Eigen::MatrixXd { return x.llt().matrixL(); }); m.def("cholesky3", [](const Eigen::Ref &x) -> Eigen::MatrixXd { return x.llt().matrixL(); }); - // NOLINTNEXTLINE (performance-unnecessary-value-param) - m.def("cholesky4", [](Eigen::Ref x) -> Eigen::MatrixXd { return x.llt().matrixL(); }); + m.def("cholesky4", [](const Eigen::Ref &x) -> Eigen::MatrixXd { + return x.llt().matrixL(); + }); // test_eigen_ref_mutators // Mutators: these add some value to the given element using Eigen, but Eigen should be mapping into @@ -248,12 +249,9 @@ TEST_SUBMODULE(eigen, m) { m.def("fixed_copy_r", [](const FixedMatrixR &m) -> FixedMatrixR { return m; }); m.def("fixed_copy_c", [](const FixedMatrixC &m) -> FixedMatrixC { return m; }); // test_mutator_descriptors - // NOLINTNEXTLINE (performance-unnecessary-value-param) - m.def("fixed_mutator_r", [](Eigen::Ref) {}); - // NOLINTNEXTLINE (performance-unnecessary-value-param) - m.def("fixed_mutator_c", [](Eigen::Ref) {}); - // NOLINTNEXTLINE (performance-unnecessary-value-param) - m.def("fixed_mutator_a", [](py::EigenDRef) {}); + m.def("fixed_mutator_r", [](const Eigen::Ref &) {}); + m.def("fixed_mutator_c", [](const Eigen::Ref &) {}); + m.def("fixed_mutator_a", [](const py::EigenDRef &) {}); // test_dense m.def("dense_r", [mat]() -> DenseMatrixR { return DenseMatrixR(mat); }); m.def("dense_c", [mat]() -> DenseMatrixC { return DenseMatrixC(mat); }); @@ -284,9 +282,10 @@ TEST_SUBMODULE(eigen, m) { // that would allow copying (if types or strides don't match) for comparison: m.def("get_elem", &get_elem); // Now this alternative that calls the tells pybind to fail rather than copy: - // NOLINTNEXTLINE (performance-unnecessary-value-param) - m.def("get_elem_nocopy", [](Eigen::Ref m) -> double { return get_elem(m); }, - py::arg{}.noconvert()); + m.def( + "get_elem_nocopy", + [](const Eigen::Ref &m) -> double { return get_elem(m); }, + py::arg{}.noconvert()); // Also test a row-major-only no-copy const ref: m.def("get_elem_rm_nocopy", [](Eigen::Ref> &m) -> long { return m(2, 1); }, py::arg{}.noconvert()); @@ -301,20 +300,22 @@ TEST_SUBMODULE(eigen, m) { // test_issue1105 // Issue #1105: when converting from a numpy two-dimensional (Nx1) or (1xN) value into a dense // eigen Vector or RowVector, the argument would fail to load because the numpy copy would - // fail: numpy won't broadcast a Nx1 into a 1-dimensional vector. NOLINTNEXTLINE - // NOLINTNEXTLINE (performance-unnecessary-value-param) - m.def("iss1105_col", [](Eigen::VectorXd) { return true; }); - // NOLINTNEXTLINE (performance-unnecessary-value-param) - m.def("iss1105_row", [](Eigen::RowVectorXd) { return true; }); + // fail: numpy won't broadcast a Nx1 into a 1-dimensional vector. + m.def("iss1105_col", [](const Eigen::VectorXd &) { return true; }); + m.def("iss1105_row", [](const Eigen::RowVectorXd &) { return true; }); // test_named_arguments // Make sure named arguments are working properly: - // NOLINTNEXTLINE (performance-unnecessary-value-param) - m.def("matrix_multiply", [](const py::EigenDRef A, const py::EigenDRef B) - -> Eigen::MatrixXd { - if (A.cols() != B.rows()) throw std::domain_error("Nonconformable matrices!"); - return A * B; - }, py::arg("A"), py::arg("B")); + m.def( + "matrix_multiply", + [](const py::EigenDRef &A, + const py::EigenDRef &B) -> Eigen::MatrixXd { + if (A.cols() != B.rows()) + throw std::domain_error("Nonconformable matrices!"); + return A * B; + }, + py::arg("A"), + py::arg("B")); // test_custom_operator_new py::class_(m, "CustomOperatorNew") @@ -326,8 +327,7 @@ TEST_SUBMODULE(eigen, m) { // In case of a failure (the caster's temp array does not live long enough), creating // a new array (np.ones(10)) increases the chances that the temp array will be garbage // collected and/or that its memory will be overridden with different values. - // NOLINTNEXTLINE (performance-unnecessary-value-param) - m.def("get_elem_direct", [](Eigen::Ref v) { + m.def("get_elem_direct", [](const Eigen::Ref &v) { py::module_::import("numpy").attr("ones")(10); return v(5); }); diff --git a/tests/test_exceptions.cpp b/tests/test_exceptions.cpp index d34f1a94e1..f7bacd07e4 100644 --- a/tests/test_exceptions.cpp +++ b/tests/test_exceptions.cpp @@ -201,17 +201,16 @@ TEST_SUBMODULE(exceptions, m) { throw py::error_already_set(); }); - // Changing this broke things. Don't know why - // NOLINTNEXTLINE(performance-unnecessary-value-param) - m.def("python_call_in_destructor", [](py::dict d) { + m.def("python_call_in_destructor", [](const py::dict &d) { + bool retval = false; try { PythonCallInDestructor set_dict_in_destructor(d); PyErr_SetString(PyExc_ValueError, "foo"); throw py::error_already_set(); } catch (const py::error_already_set&) { - return true; + retval = true; } - return false; + return retval; }); m.def("python_alreadyset_in_destructor", [](const py::str &s) { diff --git a/tests/test_kwargs_and_defaults.cpp b/tests/test_kwargs_and_defaults.cpp index 3e078bd9ba..63332d32ea 100644 --- a/tests/test_kwargs_and_defaults.cpp +++ b/tests/test_kwargs_and_defaults.cpp @@ -65,9 +65,10 @@ TEST_SUBMODULE(kwargs_and_defaults, m) { #endif m.def("arg_refcount_h", [](py::handle h) { GC_IF_NEEDED; return h.ref_count(); }); m.def("arg_refcount_h", [](py::handle h, py::handle, py::handle) { GC_IF_NEEDED; return h.ref_count(); }); - // TODO replace the following nolints as appropriate - // NOLINTNEXTLINE(performance-unnecessary-value-param) - m.def("arg_refcount_o", [](py::object o) { GC_IF_NEEDED; return o.ref_count(); }); + m.def("arg_refcount_o", [](const py::object &o) { + GC_IF_NEEDED; + return o.ref_count(); + }); m.def("args_refcount", [](py::args a) { GC_IF_NEEDED; py::tuple t(a.size()); @@ -76,8 +77,7 @@ TEST_SUBMODULE(kwargs_and_defaults, m) { t[i] = (int) Py_REFCNT(PyTuple_GET_ITEM(a.ptr(), static_cast(i))); return t; }); - // NOLINTNEXTLINE(performance-unnecessary-value-param) - m.def("mixed_args_refcount", [](py::object o, py::args a) { + m.def("mixed_args_refcount", [](const py::object &o, py::args a) { GC_IF_NEEDED; py::tuple t(a.size() + 1); t[0] = o.ref_count(); diff --git a/tests/test_methods_and_attributes.cpp b/tests/test_methods_and_attributes.cpp index 67ee117c6c..6e5999f0c4 100644 --- a/tests/test_methods_and_attributes.cpp +++ b/tests/test_methods_and_attributes.cpp @@ -294,20 +294,17 @@ TEST_SUBMODULE(methods_and_attributes, m) { "static_rw_func", py::cpp_function(static_get2, rvp_copy), static_set2) // test_property_rvalue_policy .def_property_readonly("rvalue", &TestPropRVP::get_rvalue) - // NOLINTNEXTLINE(performance-unnecessary-value-param) - .def_property_readonly_static("static_rvalue", [](py::object) { return UserType(1); }); + .def_property_readonly_static("static_rvalue", + [](const py::object &) { return UserType(1); }); // test_metaclass_override struct MetaclassOverride { }; py::class_(m, "MetaclassOverride", py::metaclass((PyObject *) &PyType_Type)) - // NOLINTNEXTLINE(performance-unnecessary-value-param) - .def_property_readonly_static("readonly", [](py::object) { return 1; }); + .def_property_readonly_static("readonly", [](const py::object &) { return 1; }); // test_overload_ordering - // NOLINTNEXTLINE(performance-unnecessary-value-param) - m.def("overload_order", [](std::string) { return 1; }); - // NOLINTNEXTLINE(performance-unnecessary-value-param) - m.def("overload_order", [](std::string) { return 2; }); + m.def("overload_order", [](const std::string &) { return 1; }); + m.def("overload_order", [](const std::string &) { return 2; }); m.def("overload_order", [](int) { return 3; }); m.def("overload_order", [](int) { return 4; }, py::prepend{}); diff --git a/tests/test_multiple_inheritance.cpp b/tests/test_multiple_inheritance.cpp index f4acf7861e..d6b24d34da 100644 --- a/tests/test_multiple_inheritance.cpp +++ b/tests/test_multiple_inheritance.cpp @@ -141,8 +141,7 @@ TEST_SUBMODULE(multiple_inheritance, m) { .def(py::init()); m.def("bar_base2a", [](Base2a *b) { return b->bar(); }); - // NOLINTNEXTLINE(performance-unnecessary-value-param) - m.def("bar_base2a_sharedptr", [](std::shared_ptr b) { return b->bar(); }); + m.def("bar_base2a_sharedptr", [](const std::shared_ptr &b) { return b->bar(); }); // test_mi_unaligned_base // test_mi_base_return diff --git a/tests/test_numpy_array.cpp b/tests/test_numpy_array.cpp index a6c7ae8d74..5c22a3d25b 100644 --- a/tests/test_numpy_array.cpp +++ b/tests/test_numpy_array.cpp @@ -226,8 +226,7 @@ TEST_SUBMODULE(numpy_array, sm) { return py::isinstance(std::move(yes)) && !py::isinstance(std::move(no)); }); - // NOLINTNEXTLINE(performance-unnecessary-value-param) - sm.def("isinstance_typed", [](py::object o) { + sm.def("isinstance_typed", [](const py::object &o) { return py::isinstance>(o) && !py::isinstance>(o); }); @@ -248,60 +247,47 @@ TEST_SUBMODULE(numpy_array, sm) { }); // test_overload_resolution - // NOLINTNEXTLINE(performance-unnecessary-value-param) - sm.def("overloaded", [](py::array_t) { return "double"; }); - // NOLINTNEXTLINE(performance-unnecessary-value-param) - sm.def("overloaded", [](py::array_t) { return "float"; }); - // NOLINTNEXTLINE(performance-unnecessary-value-param) - sm.def("overloaded", [](py::array_t) { return "int"; }); - // NOLINTNEXTLINE(performance-unnecessary-value-param) - sm.def("overloaded", [](py::array_t) { return "unsigned short"; }); - // NOLINTNEXTLINE(performance-unnecessary-value-param) - sm.def("overloaded", [](py::array_t) { return "long long"; }); - // NOLINTNEXTLINE(performance-unnecessary-value-param) - sm.def("overloaded", [](py::array_t>) { return "double complex"; }); - // NOLINTNEXTLINE(performance-unnecessary-value-param) - sm.def("overloaded", [](py::array_t>) { return "float complex"; }); - - // NOLINTNEXTLINE(performance-unnecessary-value-param) - sm.def("overloaded2", [](py::array_t>) { return "double complex"; }); - // NOLINTNEXTLINE(performance-unnecessary-value-param) - sm.def("overloaded2", [](py::array_t) { return "double"; }); - // NOLINTNEXTLINE(performance-unnecessary-value-param) - sm.def("overloaded2", [](py::array_t>) { return "float complex"; }); - // NOLINTNEXTLINE(performance-unnecessary-value-param) - sm.def("overloaded2", [](py::array_t) { return "float"; }); + sm.def("overloaded", [](const py::array_t &) { return "double"; }); + sm.def("overloaded", [](const py::array_t &) { return "float"; }); + sm.def("overloaded", [](const py::array_t &) { return "int"; }); + sm.def("overloaded", [](const py::array_t &) { return "unsigned short"; }); + sm.def("overloaded", [](const py::array_t &) { return "long long"; }); + sm.def("overloaded", + [](const py::array_t> &) { return "double complex"; }); + sm.def("overloaded", [](const py::array_t> &) { return "float complex"; }); + + sm.def("overloaded2", + [](const py::array_t> &) { return "double complex"; }); + sm.def("overloaded2", [](const py::array_t &) { return "double"; }); + sm.def("overloaded2", + [](const py::array_t> &) { return "float complex"; }); + sm.def("overloaded2", [](const py::array_t &) { return "float"; }); // [workaround(intel)] ICC 20/21 breaks with py::arg().stuff, using py::arg{}.stuff works. // Only accept the exact types: - // NOLINTNEXTLINE(performance-unnecessary-value-param) - sm.def("overloaded3", [](py::array_t) { return "int"; }, py::arg{}.noconvert()); - // NOLINTNEXTLINE(performance-unnecessary-value-param) - sm.def("overloaded3", [](py::array_t) { return "double"; }, py::arg{}.noconvert()); + sm.def( + "overloaded3", [](const py::array_t &) { return "int"; }, py::arg{}.noconvert()); + sm.def( + "overloaded3", + [](const py::array_t &) { return "double"; }, + py::arg{}.noconvert()); // Make sure we don't do unsafe coercion (e.g. float to int) when not using forcecast, but // rather that float gets converted via the safe (conversion to double) overload: - // NOLINTNEXTLINE(performance-unnecessary-value-param) - sm.def("overloaded4", [](py::array_t) { return "long long"; }); - // NOLINTNEXTLINE(performance-unnecessary-value-param) - sm.def("overloaded4", [](py::array_t) { return "double"; }); + sm.def("overloaded4", [](const py::array_t &) { return "long long"; }); + sm.def("overloaded4", [](const py::array_t &) { return "double"; }); // But we do allow conversion to int if forcecast is enabled (but only if no overload matches // without conversion) - // NOLINTNEXTLINE(performance-unnecessary-value-param) - sm.def("overloaded5", [](py::array_t) { return "unsigned int"; }); - // NOLINTNEXTLINE(performance-unnecessary-value-param) - sm.def("overloaded5", [](py::array_t) { return "double"; }); + sm.def("overloaded5", [](const py::array_t &) { return "unsigned int"; }); + sm.def("overloaded5", [](const py::array_t &) { return "double"; }); // test_greedy_string_overload // Issue 685: ndarray shouldn't go to std::string overload - // NOLINTNEXTLINE(performance-unnecessary-value-param) - sm.def("issue685", [](std::string) { return "string"; }); - // NOLINTNEXTLINE(performance-unnecessary-value-param) - sm.def("issue685", [](py::array) { return "array"; }); - // NOLINTNEXTLINE(performance-unnecessary-value-param) - sm.def("issue685", [](py::object) { return "other"; }); + sm.def("issue685", [](const std::string &) { return "string"; }); + sm.def("issue685", [](const py::array &) { return "array"; }); + sm.def("issue685", [](const py::object &) { return "other"; }); // test_array_unchecked_fixed_dims sm.def("proxy_add2", [](py::array_t a, double v) { @@ -424,73 +410,53 @@ TEST_SUBMODULE(numpy_array, sm) { // test_argument_conversions sm.def( - "accept_double", - // NOLINTNEXTLINE(performance-unnecessary-value-param) - [](py::array_t) {}, - py::arg("a")); + "accept_double", [](const py::array_t &) {}, py::arg("a")); sm.def( "accept_double_forcecast", - // NOLINTNEXTLINE(performance-unnecessary-value-param) - [](py::array_t) {}, + [](const py::array_t &) {}, py::arg("a")); sm.def( "accept_double_c_style", - // NOLINTNEXTLINE(performance-unnecessary-value-param) - [](py::array_t) {}, + [](const py::array_t &) {}, py::arg("a")); sm.def( "accept_double_c_style_forcecast", - // NOLINTNEXTLINE(performance-unnecessary-value-param) - [](py::array_t) {}, + [](const py::array_t &) {}, py::arg("a")); sm.def( "accept_double_f_style", - // NOLINTNEXTLINE(performance-unnecessary-value-param) - [](py::array_t) {}, + [](const py::array_t &) {}, py::arg("a")); sm.def( "accept_double_f_style_forcecast", - // NOLINTNEXTLINE(performance-unnecessary-value-param) - [](py::array_t) {}, + [](const py::array_t &) {}, py::arg("a")); sm.def( - "accept_double_noconvert", - // NOLINTNEXTLINE(performance-unnecessary-value-param) - [](py::array_t) {}, - "a"_a.noconvert()); + "accept_double_noconvert", [](const py::array_t &) {}, "a"_a.noconvert()); sm.def( "accept_double_forcecast_noconvert", - // NOLINTNEXTLINE(performance-unnecessary-value-param) - [](py::array_t) {}, + [](const py::array_t &) {}, "a"_a.noconvert()); sm.def( "accept_double_c_style_noconvert", - // NOLINTNEXTLINE(performance-unnecessary-value-param) - [](py::array_t) {}, + [](const py::array_t &) {}, "a"_a.noconvert()); sm.def( "accept_double_c_style_forcecast_noconvert", - // NOLINTNEXTLINE(performance-unnecessary-value-param) - [](py::array_t) {}, + [](const py::array_t &) {}, "a"_a.noconvert()); sm.def( "accept_double_f_style_noconvert", - // NOLINTNEXTLINE(performance-unnecessary-value-param) - [](py::array_t) {}, + [](const py::array_t &) {}, "a"_a.noconvert()); sm.def( "accept_double_f_style_forcecast_noconvert", - // NOLINTNEXTLINE(performance-unnecessary-value-param) - [](py::array_t) {}, + [](const py::array_t &) {}, "a"_a.noconvert()); // Check that types returns correct npy format descriptor - // NOLINTNEXTLINE(performance-unnecessary-value-param) - sm.def("test_fmt_desc_float", [](py::array_t) {}); - // NOLINTNEXTLINE(performance-unnecessary-value-param) - sm.def("test_fmt_desc_double", [](py::array_t) {}); - // NOLINTNEXTLINE(performance-unnecessary-value-param) - sm.def("test_fmt_desc_const_float", [](py::array_t) {}); - // NOLINTNEXTLINE(performance-unnecessary-value-param) - sm.def("test_fmt_desc_const_double", [](py::array_t) {}); + sm.def("test_fmt_desc_float", [](const py::array_t &) {}); + sm.def("test_fmt_desc_double", [](const py::array_t &) {}); + sm.def("test_fmt_desc_const_float", [](const py::array_t &) {}); + sm.def("test_fmt_desc_const_double", [](const py::array_t &) {}); } diff --git a/tests/test_numpy_vectorize.cpp b/tests/test_numpy_vectorize.cpp index 0be30d8e88..42fb2002ec 100644 --- a/tests/test_numpy_vectorize.cpp +++ b/tests/test_numpy_vectorize.cpp @@ -40,13 +40,13 @@ TEST_SUBMODULE(numpy_vectorize, m) { // test_type_selection // NumPy function which only accepts specific data types // A lot of these no lints could be replaced with const refs, and probably should at some point. - // NOLINTNEXTLINE(performance-unnecessary-value-param) - m.def("selective_func", [](py::array_t) { return "Int branch taken."; }); - // NOLINTNEXTLINE(performance-unnecessary-value-param) - m.def("selective_func", [](py::array_t) { return "Float branch taken."; }); - // NOLINTNEXTLINE(performance-unnecessary-value-param) - m.def("selective_func", [](py::array_t, py::array::c_style>) { return "Complex float branch taken."; }); - + m.def("selective_func", + [](const py::array_t &) { return "Int branch taken."; }); + m.def("selective_func", + [](const py::array_t &) { return "Float branch taken."; }); + m.def("selective_func", [](const py::array_t, py::array::c_style> &) { + return "Complex float branch taken."; + }); // test_passthrough_arguments // Passthrough test: references and non-pod types should be automatically passed through (in the @@ -89,13 +89,9 @@ TEST_SUBMODULE(numpy_vectorize, m) { .value("c_trivial", py::detail::broadcast_trivial::c_trivial) .value("non_trivial", py::detail::broadcast_trivial::non_trivial); m.def("vectorized_is_trivial", - []( - // NOLINTNEXTLINE(performance-unnecessary-value-param) - py::array_t arg1, - // NOLINTNEXTLINE(performance-unnecessary-value-param) - py::array_t arg2, - // NOLINTNEXTLINE(performance-unnecessary-value-param) - py::array_t arg3) { + [](const py::array_t &arg1, + const py::array_t &arg2, + const py::array_t &arg3) { py::ssize_t ndim; std::vector shape; std::array buffers{ diff --git a/tests/test_pytypes.cpp b/tests/test_pytypes.cpp index 2b91af37b0..6ed59aad21 100644 --- a/tests/test_pytypes.cpp +++ b/tests/test_pytypes.cpp @@ -237,8 +237,7 @@ TEST_SUBMODULE(pytypes, m) { ); }); - // NOLINTNEXTLINE(performance-unnecessary-value-param) - m.def("cast_functions", [](py::dict d) { + m.def("cast_functions", [](const py::dict &d) { // When converting between Python types, obj.cast() should be the same as T(obj) return py::dict( "bytes"_a=d["bytes"].cast(), @@ -255,25 +254,24 @@ TEST_SUBMODULE(pytypes, m) { ); }); - // NOLINTNEXTLINE(performance-unnecessary-value-param) - m.def("convert_to_pybind11_str", [](py::object o) { return py::str(o); }); - - // NOLINTNEXTLINE(performance-unnecessary-value-param) - m.def("nonconverting_constructor", [](std::string type, py::object value, bool move) -> py::object { - if (type == "bytes") { - return move ? py::bytes(std::move(value)) : py::bytes(value); - } - if (type == "none") { - return move ? py::none(std::move(value)) : py::none(value); - } - if (type == "ellipsis") { - return move ? py::ellipsis(std::move(value)) : py::ellipsis(value); - } - if (type == "type") { - return move ? py::type(std::move(value)) : py::type(value); - } - throw std::runtime_error("Invalid type"); - }); + m.def("convert_to_pybind11_str", [](const py::object &o) { return py::str(o); }); + + m.def("nonconverting_constructor", + [](const std::string &type, py::object value, bool move) -> py::object { + if (type == "bytes") { + return move ? py::bytes(std::move(value)) : py::bytes(value); + } + if (type == "none") { + return move ? py::none(std::move(value)) : py::none(value); + } + if (type == "ellipsis") { + return move ? py::ellipsis(std::move(value)) : py::ellipsis(value); + } + if (type == "type") { + return move ? py::type(std::move(value)) : py::type(value); + } + throw std::runtime_error("Invalid type"); + }); m.def("get_implicit_casting", []() { py::dict d; @@ -333,8 +331,7 @@ TEST_SUBMODULE(pytypes, m) { m.def("hash_function", [](py::object obj) { return py::hash(std::move(obj)); }); - // NOLINTNEXTLINE(performance-unnecessary-value-param) - m.def("test_number_protocol", [](py::object a, py::object b) { + m.def("test_number_protocol", [](const py::object &a, const py::object &b) { py::list l; l.append(a.equal(b)); l.append(a.not_equal(b)); @@ -354,10 +351,7 @@ TEST_SUBMODULE(pytypes, m) { return l; }); - // NOLINTNEXTLINE(performance-unnecessary-value-param) - m.def("test_list_slicing", [](py::list a) { - return a[py::slice(0, -1, 2)]; - }); + m.def("test_list_slicing", [](const py::list &a) { return a[py::slice(0, -1, 2)]; }); // See #2361 m.def("issue2361_str_implicit_copy_none", []() { @@ -369,15 +363,10 @@ TEST_SUBMODULE(pytypes, m) { return is_this_none; }); - // NOLINTNEXTLINE(performance-unnecessary-value-param) - m.def("test_memoryview_object", [](py::buffer b) { - return py::memoryview(b); - }); + m.def("test_memoryview_object", [](const py::buffer &b) { return py::memoryview(b); }); - // NOLINTNEXTLINE(performance-unnecessary-value-param) - m.def("test_memoryview_buffer_info", [](py::buffer b) { - return py::memoryview(b.request()); - }); + m.def("test_memoryview_buffer_info", + [](const py::buffer &b) { return py::memoryview(b.request()); }); m.def("test_memoryview_from_buffer", [](bool is_unsigned) { static const int16_t si16[] = { 3, 1, 4, 1, 5 }; @@ -432,8 +421,7 @@ TEST_SUBMODULE(pytypes, m) { m.def("pass_to_pybind11_bytes", [](py::bytes b) { return py::len(std::move(b)); }); m.def("pass_to_pybind11_str", [](py::str s) { return py::len(std::move(s)); }); - // NOLINTNEXTLINE(performance-unnecessary-value-param) - m.def("pass_to_std_string", [](std::string s) { return s.size(); }); + m.def("pass_to_std_string", [](const std::string &s) { return s.size(); }); // test_weakref m.def("weakref_from_handle", diff --git a/tests/test_stl.cpp b/tests/test_stl.cpp index dc75762e85..1f308e1393 100644 --- a/tests/test_stl.cpp +++ b/tests/test_stl.cpp @@ -207,8 +207,7 @@ TEST_SUBMODULE(stl, m) { }, py::arg_v("x", std::nullopt, "None")); m.def("nodefer_none_optional", [](std::optional) { return true; }); - // NOLINTNEXTLINE(performance-unnecessary-value-param) - m.def("nodefer_none_optional", [](py::none) { return false; }); + m.def("nodefer_none_optional", [](const py::none &) { return false; }); using opt_holder = OptionalHolder; py::class_(m, "OptionalHolder", "Class with optional member") @@ -299,12 +298,11 @@ TEST_SUBMODULE(stl, m) { m.def("stl_pass_by_pointer", [](std::vector* v) { return *v; }, "v"_a=nullptr); // #1258: pybind11/stl.h converts string to vector - // NOLINTNEXTLINE(performance-unnecessary-value-param) - m.def("func_with_string_or_vector_string_arg_overload", [](std::vector) { return 1; }); - // NOLINTNEXTLINE(performance-unnecessary-value-param) - m.def("func_with_string_or_vector_string_arg_overload", [](std::list) { return 2; }); - // NOLINTNEXTLINE(performance-unnecessary-value-param) - m.def("func_with_string_or_vector_string_arg_overload", [](std::string) { return 3; }); + m.def("func_with_string_or_vector_string_arg_overload", + [](const std::vector &) { return 1; }); + m.def("func_with_string_or_vector_string_arg_overload", + [](const std::list &) { return 2; }); + m.def("func_with_string_or_vector_string_arg_overload", [](const std::string &) { return 3; }); class Placeholder { public: From 7472d37a93514dd22bf27da4123ff7b7f2a1545a Mon Sep 17 00:00:00 2001 From: "Ralf W. Grosse-Kunstleve" Date: Mon, 12 Jul 2021 13:39:06 -0700 Subject: [PATCH 057/638] Adding iostream.h thread-safety documentation. (#2995) * Adding iostream.h thread-safety documentation. * Restoring `TestThread` code with added `std::lock_guard`. * Updating new comments to reflect new information. * Fixing up `git rebase -X theirs` accidents. --- docs/advanced/pycpp/utilities.rst | 11 ++++++++ include/pybind11/iostream.h | 45 +++++++++++++++++-------------- tests/test_iostream.cpp | 14 +++++++++- 3 files changed, 49 insertions(+), 21 deletions(-) diff --git a/docs/advanced/pycpp/utilities.rst b/docs/advanced/pycpp/utilities.rst index c15051fb96..8b8e5196af 100644 --- a/docs/advanced/pycpp/utilities.rst +++ b/docs/advanced/pycpp/utilities.rst @@ -47,6 +47,17 @@ redirects output to the corresponding Python streams: call_noisy_func(); }); +.. warning:: + + The implementation in ``pybind11/iostream.h`` is NOT thread safe. Multiple + threads writing to a redirected ostream concurrently cause data races + and potentially buffer overflows. Therefore it is currrently a requirement + that all (possibly) concurrent redirected ostream writes are protected by + a mutex. #HelpAppreciated: Work on iostream.h thread safety. For more + background see the discussions under + `PR #2982 `_ and + `PR #2995 `_. + This method respects flushes on the output streams and will flush if needed when the scoped guard is destroyed. This allows the output to be redirected in real time, such as to a Jupyter notebook. The two arguments, the C++ stream and diff --git a/include/pybind11/iostream.h b/include/pybind11/iostream.h index 89d1813ba8..c98067ad8a 100644 --- a/include/pybind11/iostream.h +++ b/include/pybind11/iostream.h @@ -5,6 +5,16 @@ All rights reserved. Use of this source code is governed by a BSD-style license that can be found in the LICENSE file. + + WARNING: The implementation in this file is NOT thread safe. Multiple + threads writing to a redirected ostream concurrently cause data races + and potentially buffer overflows. Therefore it is currrently a requirement + that all (possibly) concurrent redirected ostream writes are protected by + a mutex. + #HelpAppreciated: Work on iostream.h thread safety. + For more background see the discussions under + https://github.com/pybind/pybind11/pull/2982 and + https://github.com/pybind/pybind11/pull/2995. */ #pragma once @@ -85,30 +95,25 @@ class pythonbuf : public std::streambuf { return remainder; } - // This function must be non-virtual to be called in a destructor. If the - // rare MSVC test failure shows up with this version, then this should be - // simplified to a fully qualified call. + // This function must be non-virtual to be called in a destructor. int _sync() { if (pbase() != pptr()) { // If buffer is not empty gil_scoped_acquire tmp; - // Placed inside gil_scoped_acquire as a mutex to avoid a race. - if (pbase() != pptr()) { // Check again under the lock - // This subtraction cannot be negative, so dropping the sign. - auto size = static_cast(pptr() - pbase()); - size_t remainder = utf8_remainder(); - - if (size > remainder) { - str line(pbase(), size - remainder); - pywrite(line); - pyflush(); - } - - // Copy the remainder at the end of the buffer to the beginning: - if (remainder > 0) - std::memmove(pbase(), pptr() - remainder, remainder); - setp(pbase(), epptr()); - pbump(static_cast(remainder)); + // This subtraction cannot be negative, so dropping the sign. + auto size = static_cast(pptr() - pbase()); + size_t remainder = utf8_remainder(); + + if (size > remainder) { + str line(pbase(), size - remainder); + pywrite(line); + pyflush(); } + + // Copy the remainder at the end of the buffer to the beginning: + if (remainder > 0) + std::memmove(pbase(), pptr() - remainder, remainder); + setp(pbase(), epptr()); + pbump(static_cast(remainder)); } return 0; } diff --git a/tests/test_iostream.cpp b/tests/test_iostream.cpp index 908788007f..c620b59493 100644 --- a/tests/test_iostream.cpp +++ b/tests/test_iostream.cpp @@ -15,6 +15,8 @@ #include "pybind11_tests.h" #include #include +#include +#include #include void noisy_function(const std::string &msg, bool flush) { @@ -35,8 +37,18 @@ void noisy_funct_dual(const std::string &msg, const std::string &emsg) { struct TestThread { TestThread() : stop_{false} { auto thread_f = [this] { + static std::mutex cout_mutex; while (!stop_) { - std::cout << "x" << std::flush; + { + // #HelpAppreciated: Work on iostream.h thread safety. + // Without this lock, the clang ThreadSanitizer (tsan) reliably reports a + // data race, and this test is predictably flakey on Windows. + // For more background see the discussion under + // https://github.com/pybind/pybind11/pull/2982 and + // https://github.com/pybind/pybind11/pull/2995. + const std::lock_guard lock(cout_mutex); + std::cout << "x" << std::flush; + } std::this_thread::sleep_for(std::chrono::microseconds(50)); } }; t_ = new std::thread(std::move(thread_f)); From f0a65c899c7245f1dbaabad73330b4120ed6f2d4 Mon Sep 17 00:00:00 2001 From: Henry Schreiner Date: Mon, 12 Jul 2021 16:57:28 -0400 Subject: [PATCH 058/638] docs(fix): spelling mistake in recent commit --- docs/advanced/pycpp/utilities.rst | 2 +- include/pybind11/iostream.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/advanced/pycpp/utilities.rst b/docs/advanced/pycpp/utilities.rst index 8b8e5196af..30429e84d7 100644 --- a/docs/advanced/pycpp/utilities.rst +++ b/docs/advanced/pycpp/utilities.rst @@ -51,7 +51,7 @@ redirects output to the corresponding Python streams: The implementation in ``pybind11/iostream.h`` is NOT thread safe. Multiple threads writing to a redirected ostream concurrently cause data races - and potentially buffer overflows. Therefore it is currrently a requirement + and potentially buffer overflows. Therefore it is currently a requirement that all (possibly) concurrent redirected ostream writes are protected by a mutex. #HelpAppreciated: Work on iostream.h thread safety. For more background see the discussions under diff --git a/include/pybind11/iostream.h b/include/pybind11/iostream.h index c98067ad8a..e4d2095857 100644 --- a/include/pybind11/iostream.h +++ b/include/pybind11/iostream.h @@ -8,7 +8,7 @@ WARNING: The implementation in this file is NOT thread safe. Multiple threads writing to a redirected ostream concurrently cause data races - and potentially buffer overflows. Therefore it is currrently a requirement + and potentially buffer overflows. Therefore it is currently a requirement that all (possibly) concurrent redirected ostream writes are protected by a mutex. #HelpAppreciated: Work on iostream.h thread safety. From 9f11951b5beee10c86eaeba56707da22d913bdbd Mon Sep 17 00:00:00 2001 From: "Ralf W. Grosse-Kunstleve" Date: Mon, 12 Jul 2021 14:00:48 -0700 Subject: [PATCH 059/638] Fixing spelling errors that went undetected because the pre-commit spell check was added after the CI for PR #2995 last ran. (#3103) From ddf0efb9905770399eea765b4aaca09446155f04 Mon Sep 17 00:00:00 2001 From: Henry Schreiner Date: Mon, 12 Jul 2021 17:45:40 -0400 Subject: [PATCH 060/638] chore: add nox support (#3101) * chore: add nox support * chore: add more lines to CODEOWNERS --- .github/CODEOWNERS | 3 ++ .github/CONTRIBUTING.md | 27 ++++++++++++++ .pre-commit-config.yaml | 1 + noxfile.py | 83 +++++++++++++++++++++++++++++++++++++++++ setup.cfg | 1 + 5 files changed, 115 insertions(+) create mode 100644 noxfile.py diff --git a/.github/CODEOWNERS b/.github/CODEOWNERS index 60af5e7971..4e2c66902e 100644 --- a/.github/CODEOWNERS +++ b/.github/CODEOWNERS @@ -4,3 +4,6 @@ CMakeLists.txt @henryiii *.yaml @henryiii /tools/ @henryiii /pybind11/ @henryiii +noxfile.py @henryiii +.clang-format @henryiii +.clang-tidy @henryiii diff --git a/.github/CONTRIBUTING.md b/.github/CONTRIBUTING.md index 6f9ebf21f1..ff1997f989 100644 --- a/.github/CONTRIBUTING.md +++ b/.github/CONTRIBUTING.md @@ -53,6 +53,33 @@ derivative works thereof, in binary and source code form. ## Development of pybind11 +### Quick setup + +To setup a quick development environment, use [`nox`](https://nox.thea.codes). +This will allow you to do some common tasks with minimal setup effort, but will +take more time to run and be less flexible than a full development environment. +If you use [`pipx run nox`](https://pipx.pypa.io), you don't even need to +install `nox`. Examples: + +```bash +# List all available sessions +nox -l + +# Run linters +nox -s lint + +# Run tests +nox -s tests + +# Build and preview docs +nox -s docs -- serve + +# Build SDists and wheels +nox -s build +``` + +### Full setup + To setup an ideal development environment, run the following commands on a system with CMake 3.14+: diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 85d575464f..3f5a5723c4 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -28,6 +28,7 @@ repos: - id: requirements-txt-fixer - id: trailing-whitespace - id: fix-encoding-pragma + exclude: ^noxfile.py$ # Black, the code formatter, natively supports pre-commit - repo: https://github.com/psf/black diff --git a/noxfile.py b/noxfile.py new file mode 100644 index 0000000000..3bc61273ff --- /dev/null +++ b/noxfile.py @@ -0,0 +1,83 @@ +import nox + + +nox.options.sessions = ["lint", "tests", "tests_packaging"] + + +@nox.session(reuse_venv=True) +def lint(session: nox.Session) -> None: + """ + Lint the codebase (except for clang-format/tidy). + """ + session.install("pre-commit") + session.run("pre-commit", "run", "-a") + + +@nox.session +def tests(session: nox.Session) -> None: + """ + Run the tests (requires a compiler). + """ + tmpdir = session.create_tmp() + session.install("pytest", "cmake") + session.run( + "cmake", + "-S", + ".", + "-B", + tmpdir, + "-DPYBIND11_WERROR=ON", + "-DDOWNLOAD_CATCH=ON", + "-DDOWNLOAD_EIGEN=ON", + *session.posargs + ) + session.run("cmake", "--build", tmpdir) + session.run("cmake", "--build", tmpdir, "--config=Release", "--target", "check") + + +@nox.session +def tests_packaging(session: nox.Session) -> None: + """ + Run the packaging tests. + """ + + session.install("-r", "tests/requirements.txt", "--prefer-binary") + session.run("pytest", "tests/extra_python_package") + + +@nox.session(reuse_venv=True) +def docs(session: nox.Session) -> None: + """ + Build the docs. Pass "serve" to serve. + """ + + session.install("-r", "docs/requirements.txt") + session.chdir("docs") + session.run("sphinx-build", "-M", "html", ".", "_build") + + if session.posargs: + if "serve" in session.posargs: + print("Launching docs at http://localhost:8000/ - use Ctrl-C to quit") + session.run("python", "-m", "http.server", "8000", "-d", "_build/html") + else: + print("Unsupported argument to docs") + + +@nox.session(reuse_venv=True) +def make_changelog(session: nox.Session) -> None: + """ + Inspect the closed issues and make entries for a changelog. + """ + session.install("ghapi", "rich") + session.run("python", "tools/make_changelog.py") + + +@nox.session(reuse_venv=True) +def build(session: nox.Session) -> None: + """ + Build SDists and wheels. + """ + + session.install("build") + session.run("python", "-m", "build") + session.run("python", "-m", "build", env={"PYBIND11_GLOBAL_SDIST": "1"}) diff --git a/setup.cfg b/setup.cfg index 041e62dae6..527d18aacf 100644 --- a/setup.cfg +++ b/setup.cfg @@ -47,6 +47,7 @@ ignore = pybind11/include/** pybind11/share/** CMakeLists.txt + noxfile.py [flake8] From 75090647cea463e4e43914e404674cb1b8b47a13 Mon Sep 17 00:00:00 2001 From: "Ralf W. Grosse-Kunstleve" Date: Mon, 12 Jul 2021 16:56:10 -0700 Subject: [PATCH 061/638] More precise return_value_policy::automatic documentation. (#2920) * Adding test_return_vector_bool_raw_ptr to test_stl.py. * First attempt to make the documentation more accurate, but not trying to be comprehensive, to not bloat the reference table with too many details. * Fixing minor oversights. * Applying reviewer suggestion. --- docs/advanced/functions.rst | 7 ++++--- tests/test_stl.cpp | 6 ++++++ tests/test_stl.py | 7 +++++++ 3 files changed, 17 insertions(+), 3 deletions(-) diff --git a/docs/advanced/functions.rst b/docs/advanced/functions.rst index d880008b0f..a537cb65ee 100644 --- a/docs/advanced/functions.rst +++ b/docs/advanced/functions.rst @@ -90,17 +90,18 @@ The following table provides an overview of available policies: | | return value is referenced by Python. This is the default policy for | | | property getters created via ``def_property``, ``def_readwrite``, etc. | +--------------------------------------------------+----------------------------------------------------------------------------+ -| :enum:`return_value_policy::automatic` | **Default policy.** This policy falls back to the policy | +| :enum:`return_value_policy::automatic` | This policy falls back to the policy | | | :enum:`return_value_policy::take_ownership` when the return value is a | | | pointer. Otherwise, it uses :enum:`return_value_policy::move` or | | | :enum:`return_value_policy::copy` for rvalue and lvalue references, | | | respectively. See above for a description of what all of these different | -| | policies do. | +| | policies do. This is the default policy for ``py::class_``-wrapped types. | +--------------------------------------------------+----------------------------------------------------------------------------+ | :enum:`return_value_policy::automatic_reference` | As above, but use policy :enum:`return_value_policy::reference` when the | | | return value is a pointer. This is the default conversion policy for | | | function arguments when calling Python functions manually from C++ code | -| | (i.e. via handle::operator()). You probably won't need to use this. | +| | (i.e. via ``handle::operator()``) and the casters in ``pybind11/stl.h``. | +| | You probably won't need to use this explicitly. | +--------------------------------------------------+----------------------------------------------------------------------------+ Return value policies can also be applied to properties: diff --git a/tests/test_stl.cpp b/tests/test_stl.cpp index 1f308e1393..eb8cdab7b5 100644 --- a/tests/test_stl.cpp +++ b/tests/test_stl.cpp @@ -334,4 +334,10 @@ TEST_SUBMODULE(stl, m) { py::class_(m, "Issue1561Outer") .def(py::init<>()) .def_readwrite("list", &Issue1561Outer::list); + + m.def( + "return_vector_bool_raw_ptr", + []() { return new std::vector(4513); }, + // Without explicitly specifying `take_ownership`, this function leaks. + py::return_value_policy::take_ownership); } diff --git a/tests/test_stl.py b/tests/test_stl.py index 96939257cf..5fd3570fae 100644 --- a/tests/test_stl.py +++ b/tests/test_stl.py @@ -283,3 +283,10 @@ def test_issue_1561(): bar.list = [m.Issue1561Inner("bar")] bar.list assert bar.list[0].data == "bar" + + +def test_return_vector_bool_raw_ptr(): + # Add `while True:` for manual leak checking. + v = m.return_vector_bool_raw_ptr() + assert isinstance(v, list) + assert len(v) == 4513 From 84fdadfbcc50d3eaa9f8a64a83505eae17e81560 Mon Sep 17 00:00:00 2001 From: Henry Schreiner Date: Mon, 12 Jul 2021 14:56:06 -0400 Subject: [PATCH 062/638] chore: update pre-commit hooks --- .pre-commit-config.yaml | 11 ++++++----- tests/test_class.py | 2 +- tests/test_sequences_and_iterators.py | 4 ++-- tests/test_stl.py | 2 +- 4 files changed, 10 insertions(+), 9 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 3f5a5723c4..4ce9cff57d 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -15,7 +15,7 @@ repos: # Standard hooks - repo: https://github.com/pre-commit/pre-commit-hooks - rev: v3.4.0 + rev: v4.0.1 hooks: - id: check-added-large-files - id: check-case-conflict @@ -32,7 +32,7 @@ repos: # Black, the code formatter, natively supports pre-commit - repo: https://github.com/psf/black - rev: 20.8b1 + rev: 21.6b0 hooks: - id: black # By default, this ignores pyi files, though black supports them @@ -41,13 +41,13 @@ repos: # Changes tabs to spaces - repo: https://github.com/Lucas-C/pre-commit-hooks - rev: v1.1.9 + rev: v1.1.10 hooks: - id: remove-tabs # Flake8 also supports pre-commit natively (same author) - repo: https://gitlab.com/pycqa/flake8 - rev: 3.8.4 + rev: 3.9.2 hooks: - id: flake8 additional_dependencies: [flake8-bugbear, pep8-naming] @@ -64,7 +64,7 @@ repos: # Check static types with mypy - repo: https://github.com/pre-commit/mirrors-mypy - rev: v0.800 + rev: v0.910 hooks: - id: mypy # The default Python type ignores .pyi files, so let's rerun if detected @@ -72,6 +72,7 @@ repos: files: ^pybind11.*\.pyi?$ # Running per-file misbehaves a bit, so just run on all files, it's fast pass_filenames: false + additional_dependencies: [typed_ast] # Checks the manifest for missing files (native support) - repo: https://github.com/mgedmin/check-manifest diff --git a/tests/test_class.py b/tests/test_class.py index bdcced9643..1ff60fe214 100644 --- a/tests/test_class.py +++ b/tests/test_class.py @@ -321,7 +321,7 @@ def foo(self): def test_brace_initialization(): - """ Tests that simple POD classes can be constructed using C++11 brace initialization """ + """Tests that simple POD classes can be constructed using C++11 brace initialization""" a = m.BraceInitialization(123, "test") assert a.field1 == 123 assert a.field2 == "test" diff --git a/tests/test_sequences_and_iterators.py b/tests/test_sequences_and_iterators.py index c3b608c436..70fb697f95 100644 --- a/tests/test_sequences_and_iterators.py +++ b/tests/test_sequences_and_iterators.py @@ -104,7 +104,7 @@ def test_sequence(): def test_sequence_length(): - """#2076: Exception raised by len(arg) should be propagated """ + """#2076: Exception raised by len(arg) should be propagated""" class BadLen(RuntimeError): pass @@ -187,7 +187,7 @@ def test_iterator_passthrough(): def test_iterator_rvp(): - """#388: Can't make iterators via make_iterator() with different r/v policies """ + """#388: Can't make iterators via make_iterator() with different r/v policies""" import pybind11_tests.sequences_and_iterators as m assert list(m.make_iterator_1()) == [1, 2, 3] diff --git a/tests/test_stl.py b/tests/test_stl.py index 5fd3570fae..6f08459043 100644 --- a/tests/test_stl.py +++ b/tests/test_stl.py @@ -278,7 +278,7 @@ def test_array_cast_sequence(): def test_issue_1561(): - """ check fix for issue #1561 """ + """check fix for issue #1561""" bar = m.Issue1561Outer() bar.list = [m.Issue1561Inner("bar")] bar.list From 11e12fe455831cae33b138a0ce4d4df5da9ace96 Mon Sep 17 00:00:00 2001 From: Henry Schreiner Date: Mon, 12 Jul 2021 14:58:59 -0400 Subject: [PATCH 063/638] chore: move some config to pyproject.toml --- pyproject.toml | 32 ++++++++++++++++++++++++++++++++ setup.cfg | 31 ------------------------------- 2 files changed, 32 insertions(+), 31 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 5c9d153541..f86470eef8 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,3 +1,35 @@ [build-system] requires = ["setuptools>=42", "wheel", "cmake>=3.18", "ninja"] build-backend = "setuptools.build_meta" + +[tool.check-manifest] +ignore = [ + "tests/**", + "docs/**", + "tools/**", + "include/**", + ".*", + "pybind11/include/**", + "pybind11/share/**", + "CMakeLists.txt", + "noxfile.py", +] + +[tool.mypy] +files = "pybind11" +python_version = "2.7" +warn_unused_configs = true + +disallow_any_generics = true +disallow_subclassing_any = true +disallow_untyped_calls = true +disallow_untyped_defs = true +disallow_incomplete_defs = true +check_untyped_defs = true +disallow_untyped_decorators = true +no_implicit_optional = true +warn_redundant_casts = true +warn_unused_ignores = true +warn_return_any = true +no_implicit_reexport = true +strict_equality = true diff --git a/setup.cfg b/setup.cfg index 527d18aacf..31038eb026 100644 --- a/setup.cfg +++ b/setup.cfg @@ -37,18 +37,6 @@ zip_safe = False [bdist_wheel] universal=1 -[check-manifest] -ignore = - tests/** - docs/** - tools/** - include/** - .* - pybind11/include/** - pybind11/share/** - CMakeLists.txt - noxfile.py - [flake8] max-line-length = 99 @@ -62,25 +50,6 @@ ignore = # Black conflict W503, E203 -[mypy] -files = pybind11 -python_version = 2.7 -warn_unused_configs = True - -# Currently (0.800) identical to --strict -disallow_any_generics = True -disallow_subclassing_any = True -disallow_untyped_calls = True -disallow_untyped_defs = True -disallow_incomplete_defs = True -check_untyped_defs = True -disallow_untyped_decorators = True -no_implicit_optional = True -warn_redundant_casts = True -warn_unused_ignores = True -warn_return_any = True -no_implicit_reexport = True -strict_equality = True [tool:pytest] timeout = 300 From 0e2e003508ab0dfbea8a0f825d3d001b5f4be2cd Mon Sep 17 00:00:00 2001 From: Henry Schreiner Date: Mon, 12 Jul 2021 15:01:19 -0400 Subject: [PATCH 064/638] style: add pyupgrade check, 2.7+ --- .pre-commit-config.yaml | 5 +++++ tests/extra_python_package/test_files.py | 26 ++++++++++++------------ tests/test_builtin_casters.py | 2 +- tests/test_callbacks.py | 2 +- tests/test_pytypes.py | 4 ++-- 5 files changed, 22 insertions(+), 17 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 4ce9cff57d..133eb6df4e 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -30,6 +30,11 @@ repos: - id: fix-encoding-pragma exclude: ^noxfile.py$ +- repo: https://github.com/asottile/pyupgrade + rev: v2.21.0 + hooks: + - id: pyupgrade + # Black, the code formatter, natively supports pre-commit - repo: https://github.com/psf/black rev: 21.6b0 diff --git a/tests/extra_python_package/test_files.py b/tests/extra_python_package/test_files.py index c1d12ed76b..43e93c2633 100644 --- a/tests/extra_python_package/test_files.py +++ b/tests/extra_python_package/test_files.py @@ -126,7 +126,7 @@ def test_build_sdist(monkeypatch, tmpdir): with tarfile.open(str(sdist)) as tar: start = tar.getnames()[0] + "/" version = start[9:-1] - simpler = set(n.split("/", 1)[-1] for n in tar.getnames()[1:]) + simpler = {n.split("/", 1)[-1] for n in tar.getnames()[1:]} with contextlib.closing( tar.extractfile(tar.getmember(start + "setup.py")) @@ -138,9 +138,9 @@ def test_build_sdist(monkeypatch, tmpdir): ) as f: pyproject_toml = f.read() - files = set("pybind11/{}".format(n) for n in all_files) + files = {"pybind11/{}".format(n) for n in all_files} files |= sdist_files - files |= set("pybind11{}".format(n) for n in local_sdist_files) + files |= {"pybind11{}".format(n) for n in local_sdist_files} files.add("pybind11.egg-info/entry_points.txt") files.add("pybind11.egg-info/requires.txt") assert simpler == files @@ -181,7 +181,7 @@ def test_build_global_dist(monkeypatch, tmpdir): with tarfile.open(str(sdist)) as tar: start = tar.getnames()[0] + "/" version = start[16:-1] - simpler = set(n.split("/", 1)[-1] for n in tar.getnames()[1:]) + simpler = {n.split("/", 1)[-1] for n in tar.getnames()[1:]} with contextlib.closing( tar.extractfile(tar.getmember(start + "setup.py")) @@ -193,9 +193,9 @@ def test_build_global_dist(monkeypatch, tmpdir): ) as f: pyproject_toml = f.read() - files = set("pybind11/{}".format(n) for n in all_files) + files = {"pybind11/{}".format(n) for n in all_files} files |= sdist_files - files |= set("pybind11_global{}".format(n) for n in local_sdist_files) + files |= {"pybind11_global{}".format(n) for n in local_sdist_files} assert simpler == files with open(os.path.join(MAIN_DIR, "tools", "setup_global.py.in"), "rb") as f: @@ -220,7 +220,7 @@ def tests_build_wheel(monkeypatch, tmpdir): (wheel,) = tmpdir.visit("*.whl") - files = set("pybind11/{}".format(n) for n in all_files) + files = {"pybind11/{}".format(n) for n in all_files} files |= { "dist-info/LICENSE", "dist-info/METADATA", @@ -233,10 +233,10 @@ def tests_build_wheel(monkeypatch, tmpdir): with zipfile.ZipFile(str(wheel)) as z: names = z.namelist() - trimmed = set(n for n in names if "dist-info" not in n) - trimmed |= set( + trimmed = {n for n in names if "dist-info" not in n} + trimmed |= { "dist-info/{}".format(n.split("/", 1)[-1]) for n in names if "dist-info" in n - ) + } assert files == trimmed @@ -250,8 +250,8 @@ def tests_build_global_wheel(monkeypatch, tmpdir): (wheel,) = tmpdir.visit("*.whl") - files = set("data/data/{}".format(n) for n in src_files) - files |= set("data/headers/{}".format(n[8:]) for n in headers) + files = {"data/data/{}".format(n) for n in src_files} + files |= {"data/headers/{}".format(n[8:]) for n in headers} files |= { "dist-info/LICENSE", "dist-info/METADATA", @@ -264,6 +264,6 @@ def tests_build_global_wheel(monkeypatch, tmpdir): names = z.namelist() beginning = names[0].split("/", 1)[0].rsplit(".", 1)[0] - trimmed = set(n[len(beginning) + 1 :] for n in names) + trimmed = {n[len(beginning) + 1 :] for n in names} assert files == trimmed diff --git a/tests/test_builtin_casters.py b/tests/test_builtin_casters.py index 99c400c308..1b9dbb2b40 100644 --- a/tests/test_builtin_casters.py +++ b/tests/test_builtin_casters.py @@ -50,7 +50,7 @@ def test_single_char_arguments(): """Tests failures for passing invalid inputs to char-accepting functions""" def toobig_message(r): - return "Character code point not in range({0:#x})".format(r) + return "Character code point not in range({:#x})".format(r) toolong_message = "Expected a character, but multi-character string found" diff --git a/tests/test_callbacks.py b/tests/test_callbacks.py index 3526614304..397bf63d24 100644 --- a/tests/test_callbacks.py +++ b/tests/test_callbacks.py @@ -144,7 +144,7 @@ def gen_f(): from time import sleep sleep(0.5) - assert sum(res) == sum([x + 3 for x in work]) + assert sum(res) == sum(x + 3 for x in work) def test_async_async_callbacks(): diff --git a/tests/test_pytypes.py b/tests/test_pytypes.py index f234c77315..25e9f6dffc 100644 --- a/tests/test_pytypes.py +++ b/tests/test_pytypes.py @@ -65,7 +65,7 @@ def test_set(capture, doc): """ ) - assert not m.set_contains(set([]), 42) + assert not m.set_contains(set(), 42) assert m.set_contains({42}, 42) assert m.set_contains({"foo"}, "foo") @@ -448,7 +448,7 @@ def test_memoryview(method, args, fmt, expected_view): view_as_list = list(view) else: # Using max to pick non-zero byte (big-endian vs little-endian). - view_as_list = [max([ord(c) for c in s]) for s in view] + view_as_list = [max(ord(c) for c in s) for s in view] assert view_as_list == list(expected_view) From 6a644c8f0472ba4a23517a22e30aff9e5eff1dea Mon Sep 17 00:00:00 2001 From: Henry Schreiner Date: Tue, 13 Jul 2021 00:08:29 -0400 Subject: [PATCH 065/638] docs: update changelog (#3099) * docs: update changelog * docs: add one more and merge tidy --- docs/changelog.rst | 131 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 131 insertions(+) diff --git a/docs/changelog.rst b/docs/changelog.rst index 6787349e53..d71fccc251 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -9,12 +9,143 @@ Starting with version 1.8.0, pybind11 releases use a `semantic versioning v2.7.0 (TBA, not yet released) ------------------------------ +New features: + +* Enable ``py::implicitly_convertible`` for + ``py::class_``-wrapped types. + `#3059 `_ + +* Allow function pointer extraction from overloaded functions. + `#2944 `_ + +* NumPy: added ``.char_()`` to type which gives the NumPy public ``char`` + result, which also distinguishes types by bit length (unlike ``.kind()``). + `#2864 `_ + +* Add ``pybind11::bytearray`` to manipulate ``bytearray`` similar to ``bytes``. + `#2799 `_ + +* ``pybind11/stl/filesystem.h`` registers a type caster that, on C++17/Python + 3.6+, converts ``std::filesystem::path`` to ``pathlib.Path`` and any + ``os.PathLike`` to ``std::filesystem::path``. + `#2730 `_ + + +Changes: + * ``py::str`` changed to exclusively hold `PyUnicodeObject`. Previously ``py::str`` could also hold `bytes`, which is probably surprising, was never documented, and can mask bugs (e.g. accidental use of ``py::str`` instead of ``py::bytes``). `#2409 `_ +* Add a safety guard to ensure that the Python GIL is held when C++ calls back + into Python via ``object_api<>::operator()`` (e.g. ``py::function`` + ``__call__``). (This feature is available for Python 3.6+ only.) + `#2919 `_ + +* Catch a missing ``self`` argument in calls to ``__init__()``. + `#2914 `_ + +* Use ``std::string_view`` if available to avoid a copy when passing an object + to a ``std::ostream``. + `#3042 `_ + +* An important warning about thread safety was added to the ``iostream.h`` + documentation; attempts to make ``py::scoped_ostream_redirect`` thread safe + have been removed, as it was only partially effective. + `#2995 `_ + + +Fixes: + +* Performance: avoid unnecessary strlen calls. + `#3058 `_ + +* Fix auto-generated documentation string when using ``const T`` in + ``pyarray_t``. + `#3020 `_ + +* Unify error messages thrown by ``simple_collector``/``unpacking_collector``. + `#3013 `_ + +* ``pybind11::builtin_exception`` is now explicitly exported, which means the + types included/defined in different modules are identical, and exceptions + raised in different modules can be caught correctly. The documentation was + updated to explain that custom exceptions that are used across module + boundaries need to be explicitly exported as well. + `#2999 `_ + +* Fixed exception when printing UTF-8 to a ``scoped_ostream_redirect``. + `#2982 `_ + +* Pickle support enhancement: ``setstate`` implementation will attempt to + ``setattr`` ``__dict__`` only if the unpickled ``dict`` object is not empty, + to not force use of ``py::dynamic_attr()`` unnecessarily. + `#2972 `_ + +* Allow negative timedelta values to roundtrip. + `#2870 `_ + +* Fix unchecked errors could potentially swallow signals/other exceptions. + `#2863 `_ + +* Add null pointer check with ``std::localtime``. + `#2846 `_ + +* Fix the ``weakref`` constructor from ``py::object`` to create a new + ``weakref`` on conversion. + `#2832 `_ + +* Avoid relying on exceptions in C++17 when getting a ``shared_ptr`` holder + from a ``shared_from_this`` class. + `#2819 `_ + + +Build system improvements: + +* In ``setup_helpers.py``, test for platforms that have some multiprocessing + features but lack semaphores, which ``ParallelCompile`` requires. + `#3043 `_ + +* Fix ``pybind11_INCLUDE_DIR`` in case ``CMAKE_INSTALL_INCLUDEDIR`` is + absolute. + `#3005 `_ + +* Fix bug not respecting ``WITH_SOABI`` or ``WITHOUT_SOABI`` to CMake. + `#2938 `_ + +* Fix the default ``Pybind11Extension`` compilation flags with a Mingw64 python. + `#2921 `_ + +* Clang on Windows: do not pass ``/MP`` (ignored flag). + `#2824 `_ + + +Backend and tidying up: + +* Enable clang-tidy performance, readability, and modernization checks + throughout the codebase to enforce best coding practices. + `#3046 `_, + `#3049 `_, + `#3051 `_, + `#3052 `_, and + `#3080 `_ + +* Checks for common misspellings were added to the pre-commit hooks. + `#3076 `_ + +* Changed ``Werror`` to stricter ``Werror-all`` for Intel compiler and fixed + minor issues. + `#2948 `_ + +* Fixed compilation with GCC < 5 when the user defines ``_GLIBCXX_USE_CXX11_ABI``. + `#2956 `_ + +* Added nox support for easier local testing and linting of contributions. + `#3101 `_ + + v2.6.2 (Jan 26, 2021) --------------------- From 25e470c57d6a5df00c7a4010ecfb9f2c991bca14 Mon Sep 17 00:00:00 2001 From: Aaron Gokaslan Date: Tue, 13 Jul 2021 09:54:32 -0400 Subject: [PATCH 066/638] fix(clang-tidy): Add cppcoreguidelines-init-vars,slicing, and throw-by-value-catch-by-reference checks (#3094) * clang-tidy: guard against more UB behavior * Remove slicing check for now --- .clang-tidy | 4 ++++ include/pybind11/eval.h | 4 ++-- include/pybind11/pytypes.h | 18 ++++++++--------- include/pybind11/stl_bind.h | 27 +++++++++++++------------- tests/test_eval.cpp | 2 +- tests/test_numpy_vectorize.cpp | 2 +- tests/test_sequences_and_iterators.cpp | 6 +++--- 7 files changed, 33 insertions(+), 30 deletions(-) diff --git a/.clang-tidy b/.clang-tidy index ea50bf39bc..72304528a5 100644 --- a/.clang-tidy +++ b/.clang-tidy @@ -1,10 +1,12 @@ FormatStyle: file Checks: ' +cppcoreguidelines-init-variables, clang-analyzer-optin.cplusplus.VirtualCall, llvm-namespace-comment, misc-misplaced-const, misc-static-assert, +misc-throw-by-value-catch-by-reference, misc-uniqueptr-reset-release, modernize-avoid-bind, modernize-redundant-void-arg, @@ -33,6 +35,8 @@ readability-uniqueptr-delete-release, ' CheckOptions: +- key: performance-for-range-copy.WarnOnAllAutoCopies + value: true - key: performance-unnecessary-value-param.AllowedTypes value: 'exception_ptr$;' diff --git a/include/pybind11/eval.h b/include/pybind11/eval.h index bf06504a33..33fcdc09d1 100644 --- a/include/pybind11/eval.h +++ b/include/pybind11/eval.h @@ -55,7 +55,7 @@ object eval(const str &expr, object global = globals(), object local = object()) this seems to be the only alternative */ std::string buffer = "# -*- coding: utf-8 -*-\n" + (std::string) expr; - int start; + int start = 0; switch (mode) { case eval_expr: start = Py_eval_input; break; case eval_single_statement: start = Py_single_input; break; @@ -107,7 +107,7 @@ object eval_file(str fname, object global = globals(), object local = object()) detail::ensure_builtins_in_globals(global); - int start; + int start = 0; switch (mode) { case eval_expr: start = Py_eval_input; break; case eval_single_statement: start = Py_single_input; break; diff --git a/include/pybind11/pytypes.h b/include/pybind11/pytypes.h index fb9680dfa1..32386a44d0 100644 --- a/include/pybind11/pytypes.h +++ b/include/pybind11/pytypes.h @@ -490,7 +490,7 @@ inline handle get_function(handle value) { inline PyObject * dict_getitemstring(PyObject *v, const char *key) { #if PY_MAJOR_VERSION >= 3 - PyObject *kv, *rv; + PyObject *kv = nullptr, *rv = nullptr; kv = PyUnicode_FromString(key); if (kv == NULL) { throw error_already_set(); @@ -1016,8 +1016,8 @@ class str : public object { if (!temp) pybind11_fail("Unable to extract string contents! (encoding issue)"); } - char *buffer; - ssize_t length; + char *buffer = nullptr; + ssize_t length = 0; if (PYBIND11_BYTES_AS_STRING_AND_SIZE(temp.ptr(), &buffer, &length)) pybind11_fail("Unable to extract string contents! (invalid type)"); return std::string(buffer, (size_t) length); @@ -1072,8 +1072,8 @@ class bytes : public object { explicit bytes(const pybind11::str &s); operator std::string() const { - char *buffer; - ssize_t length; + char *buffer = nullptr; + ssize_t length = 0; if (PYBIND11_BYTES_AS_STRING_AND_SIZE(m_ptr, &buffer, &length)) pybind11_fail("Unable to extract bytes contents!"); return std::string(buffer, (size_t) length); @@ -1090,8 +1090,8 @@ inline bytes::bytes(const pybind11::str &s) { if (!temp) pybind11_fail("Unable to extract string contents! (encoding issue)"); } - char *buffer; - ssize_t length; + char *buffer = nullptr; + ssize_t length = 0; if (PYBIND11_BYTES_AS_STRING_AND_SIZE(temp.ptr(), &buffer, &length)) pybind11_fail("Unable to extract string contents! (invalid type)"); auto obj = reinterpret_steal(PYBIND11_BYTES_FROM_STRING_AND_SIZE(buffer, length)); @@ -1101,8 +1101,8 @@ inline bytes::bytes(const pybind11::str &s) { } inline str::str(const bytes& b) { - char *buffer; - ssize_t length; + char *buffer = nullptr; + ssize_t length = 0; if (PYBIND11_BYTES_AS_STRING_AND_SIZE(b.ptr(), &buffer, &length)) pybind11_fail("Unable to extract bytes contents!"); auto obj = reinterpret_steal(PyUnicode_FromStringAndSize(buffer, (ssize_t) length)); diff --git a/include/pybind11/stl_bind.h b/include/pybind11/stl_bind.h index 76700763fe..effac0b931 100644 --- a/include/pybind11/stl_bind.h +++ b/include/pybind11/stl_bind.h @@ -217,9 +217,10 @@ void vector_modifiers(enable_if_t Vector * { - size_t start, stop, step, slicelength; + size_t start = 0, stop = 0, step = 0, slicelength = 0; if (!slice.compute(v.size(), &start, &stop, &step, &slicelength)) throw error_already_set(); @@ -234,12 +235,12 @@ void vector_modifiers(enable_if_t), diff --git a/tests/test_eval.cpp b/tests/test_eval.cpp index 66cdf71051..16cdf17f22 100644 --- a/tests/test_eval.cpp +++ b/tests/test_eval.cpp @@ -66,7 +66,7 @@ TEST_SUBMODULE(eval_, m) { auto local = py::dict(); local["y"] = py::int_(43); - int val_out; + int val_out = 0; local["call_test2"] = py::cpp_function([&](int value) { val_out = value; }); auto result = py::eval_file(std::move(filename), global, local); diff --git a/tests/test_numpy_vectorize.cpp b/tests/test_numpy_vectorize.cpp index 42fb2002ec..ed08a42bef 100644 --- a/tests/test_numpy_vectorize.cpp +++ b/tests/test_numpy_vectorize.cpp @@ -92,7 +92,7 @@ TEST_SUBMODULE(numpy_vectorize, m) { [](const py::array_t &arg1, const py::array_t &arg2, const py::array_t &arg3) { - py::ssize_t ndim; + py::ssize_t ndim = 0; std::vector shape; std::array buffers{ {arg1.request(), arg2.request(), arg3.request()}}; diff --git a/tests/test_sequences_and_iterators.cpp b/tests/test_sequences_and_iterators.cpp index c39798229f..d49fb1f459 100644 --- a/tests/test_sequences_and_iterators.cpp +++ b/tests/test_sequences_and_iterators.cpp @@ -84,7 +84,7 @@ TEST_SUBMODULE(sequences_and_iterators, m) { py::class_(m, "Sliceable") .def(py::init()) .def("__getitem__", [](const Sliceable &s, const py::slice &slice) { - py::ssize_t start, stop, step, slicelength; + py::ssize_t start = 0, stop = 0, step = 0, slicelength = 0; if (!slice.compute(s.size, &start, &stop, &step, &slicelength)) throw py::error_already_set(); int istart = static_cast(start); @@ -204,7 +204,7 @@ TEST_SUBMODULE(sequences_and_iterators, m) { /// Slicing protocol (optional) .def("__getitem__", [](const Sequence &s, const py::slice &slice) -> Sequence * { - size_t start, stop, step, slicelength; + size_t start = 0, stop = 0, step = 0, slicelength = 0; if (!slice.compute(s.size(), &start, &stop, &step, &slicelength)) throw py::error_already_set(); auto *seq = new Sequence(slicelength); @@ -216,7 +216,7 @@ TEST_SUBMODULE(sequences_and_iterators, m) { }) .def("__setitem__", [](Sequence &s, const py::slice &slice, const Sequence &value) { - size_t start, stop, step, slicelength; + size_t start = 0, stop = 0, step = 0, slicelength = 0; if (!slice.compute(s.size(), &start, &stop, &step, &slicelength)) throw py::error_already_set(); if (slicelength != value.size()) From 2b7985e548ee7fb551528384ae864f8b175ffbe9 Mon Sep 17 00:00:00 2001 From: jbarlow83 Date: Tue, 13 Jul 2021 12:32:56 -0700 Subject: [PATCH 067/638] Improve documentation of discard_as_unraisable() API (#2697) * Improve documentation of discard_as_unraisable() API * Update pytypes.h Remove "the above" * Update pytypes.h Fix precommit error Co-authored-by: Aaron Gokaslan --- include/pybind11/pytypes.h | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/include/pybind11/pytypes.h b/include/pybind11/pytypes.h index 32386a44d0..0fca32a4e9 100644 --- a/include/pybind11/pytypes.h +++ b/include/pybind11/pytypes.h @@ -345,16 +345,17 @@ class PYBIND11_EXPORT error_already_set : public std::runtime_error { /// error variables (but the `.what()` string is still available). void restore() { PyErr_Restore(m_type.release().ptr(), m_value.release().ptr(), m_trace.release().ptr()); } - /// If it is impossible to raise the currently-held error, such as in destructor, we can write - /// it out using Python's unraisable hook (sys.unraisablehook). The error context should be - /// some object whose repr() helps identify the location of the error. Python already knows the - /// type and value of the error, so there is no need to repeat that. For example, __func__ could - /// be helpful. After this call, the current object no longer stores the error variables, - /// and neither does Python. + /// If it is impossible to raise the currently-held error, such as in a destructor, we can write + /// it out using Python's unraisable hook (`sys.unraisablehook`). The error context should be + /// some object whose `repr()` helps identify the location of the error. Python already knows the + /// type and value of the error, so there is no need to repeat that. After this call, the current + /// object no longer stores the error variables, and neither does Python. void discard_as_unraisable(object err_context) { restore(); PyErr_WriteUnraisable(err_context.ptr()); } + /// An alternate version of `discard_as_unraisable()`, where a string provides information on the + /// location of the error. For example, `__func__` could be helpful. void discard_as_unraisable(const char *err_context) { discard_as_unraisable(reinterpret_steal(PYBIND11_FROM_STRING(err_context))); } From 1be0a0a6107478d8523191a053c24f47ebeda1ad Mon Sep 17 00:00:00 2001 From: Antony Lee Date: Tue, 13 Jul 2021 23:21:55 +0200 Subject: [PATCH 068/638] Add helper to build in-tree extensions. (#2831) For single-file extensions, a convenient pattern offered by cython is to place the source files directly in the python source tree (`foo/__init__.py`, `foo/ext.pyx`), deriving the package names from their filesystem location. Adapt this pattern for pybind11, using an `intree_extensions` helper, which should be thought of as the moral equivalent to `cythonize`. Differences with cythonize: I chose not to include globbing support (`intree_extensions(glob.glob("**/*.cpp"))` seems sufficient), nor to provide extension-customization kwargs (directly setting the attributes on the resulting Pybind11Extension objects seems sufficient). We could choose to have `intree_extension` (singular instead) and make users write `[*map(intree_extension, glob.glob("**/*.cpp"))]`; no strong opinion here. Co-authored-by: Aaron Gokaslan --- docs/compiling.rst | 13 +++++++ pybind11/setup_helpers.py | 36 +++++++++++++++++++ pybind11/setup_helpers.pyi | 5 ++- tests/extra_setuptools/test_setuphelper.py | 42 ++++++++++++++++++++++ 4 files changed, 95 insertions(+), 1 deletion(-) diff --git a/docs/compiling.rst b/docs/compiling.rst index 5d02727bad..bf7acfc808 100644 --- a/docs/compiling.rst +++ b/docs/compiling.rst @@ -70,6 +70,19 @@ that is supported via a ``build_ext`` command override; it will only affect ext_modules=ext_modules ) +If you have single-file extension modules that are directly stored in the +Python source tree (``foo.cpp`` in the same directory as where a ``foo.py`` +would be located), you can also generate ``Pybind11Extensions`` using +``setup_helpers.intree_extensions``: ``intree_extensions(["path/to/foo.cpp", +...])`` returns a list of ``Pybind11Extensions`` which can be passed to +``ext_modules``, possibly after further customizing their attributes +(``libraries``, ``include_dirs``, etc.). By doing so, a ``foo.*.so`` extension +module will be generated and made available upon installation. + +``intree_extension`` will automatically detect if you are using a ``src``-style +layout (as long as no namespace packages are involved), but you can also +explicitly pass ``package_dir`` to it (as in ``setuptools.setup``). + Since pybind11 does not require NumPy when building, a light-weight replacement for NumPy's parallel compilation distutils tool is included. Use it like this: diff --git a/pybind11/setup_helpers.py b/pybind11/setup_helpers.py index ec553d1669..2b27f1f5ad 100644 --- a/pybind11/setup_helpers.py +++ b/pybind11/setup_helpers.py @@ -303,6 +303,42 @@ def build_extensions(self): _build_ext.build_extensions(self) +def intree_extensions(paths, package_dir=None): + """ + Generate Pybind11Extensions from source files directly located in a Python + source tree. + + ``package_dir`` behaves as in ``setuptools.setup``. If unset, the Python + package root parent is determined as the first parent directory that does + not contain an ``__init__.py`` file. + """ + exts = [] + for path in paths: + if package_dir is None: + parent, _ = os.path.split(path) + while os.path.exists(os.path.join(parent, "__init__.py")): + parent, _ = os.path.split(parent) + relname, _ = os.path.splitext(os.path.relpath(path, parent)) + qualified_name = relname.replace(os.path.sep, ".") + exts.append(Pybind11Extension(qualified_name, [path])) + else: + found = False + for prefix, parent in package_dir.items(): + if path.startswith(parent): + found = True + relname, _ = os.path.splitext(os.path.relpath(path, parent)) + qualified_name = relname.replace(os.path.sep, ".") + if prefix: + qualified_name = prefix + "." + qualified_name + exts.append(Pybind11Extension(qualified_name, [path])) + if not found: + raise ValueError( + "path {} is not a child of any of the directories listed " + "in 'package_dir' ({})".format(path, package_dir) + ) + return exts + + def naive_recompile(obj, src): """ This will recompile only if the source file changes. It does not check diff --git a/pybind11/setup_helpers.pyi b/pybind11/setup_helpers.pyi index 23232e1fda..8b16c0a137 100644 --- a/pybind11/setup_helpers.pyi +++ b/pybind11/setup_helpers.pyi @@ -1,7 +1,7 @@ # IMPORTANT: Should stay in sync with setup_helpers.py (mostly checked by CI / # pre-commit). -from typing import Any, Callable, Iterator, Optional, Type, TypeVar, Union +from typing import Any, Callable, Dict, Iterator, List, Optional, Type, TypeVar, Union from types import TracebackType from distutils.command.build_ext import build_ext as _build_ext # type: ignore @@ -33,6 +33,9 @@ def auto_cpp_level(compiler: distutils.ccompiler.CCompiler) -> Union[int, str]: class build_ext(_build_ext): # type: ignore def build_extensions(self) -> None: ... +def intree_extensions( + paths: Iterator[str], package_dir: Optional[Dict[str, str]] = None +) -> List[Pybind11Extension]: ... def no_recompile(obj: str, src: str) -> bool: ... def naive_recompile(obj: str, src: str) -> bool: ... diff --git a/tests/extra_setuptools/test_setuphelper.py b/tests/extra_setuptools/test_setuphelper.py index 0d8bd0e4d6..fb2d342086 100644 --- a/tests/extra_setuptools/test_setuphelper.py +++ b/tests/extra_setuptools/test_setuphelper.py @@ -99,3 +99,45 @@ def test_simple_setup_py(monkeypatch, tmpdir, parallel, std): subprocess.check_call( [sys.executable, "test.py"], stdout=sys.stdout, stderr=sys.stderr ) + + +def test_intree_extensions(monkeypatch, tmpdir): + monkeypatch.syspath_prepend(MAIN_DIR) + + from pybind11.setup_helpers import intree_extensions + + monkeypatch.chdir(tmpdir) + root = tmpdir + root.ensure_dir() + subdir = root / "dir" + subdir.ensure_dir() + src = subdir / "ext.cpp" + src.ensure() + (ext,) = intree_extensions([src.relto(tmpdir)]) + assert ext.name == "ext" + subdir.ensure("__init__.py") + (ext,) = intree_extensions([src.relto(tmpdir)]) + assert ext.name == "dir.ext" + + +def test_intree_extensions_package_dir(monkeypatch, tmpdir): + monkeypatch.syspath_prepend(MAIN_DIR) + + from pybind11.setup_helpers import intree_extensions + + monkeypatch.chdir(tmpdir) + root = tmpdir / "src" + root.ensure_dir() + subdir = root / "dir" + subdir.ensure_dir() + src = subdir / "ext.cpp" + src.ensure() + (ext,) = intree_extensions([src.relto(tmpdir)], package_dir={"": "src"}) + assert ext.name == "dir.ext" + (ext,) = intree_extensions([src.relto(tmpdir)], package_dir={"foo": "src"}) + assert ext.name == "foo.dir.ext" + subdir.ensure("__init__.py") + (ext,) = intree_extensions([src.relto(tmpdir)], package_dir={"": "src"}) + assert ext.name == "dir.ext" + (ext,) = intree_extensions([src.relto(tmpdir)], package_dir={"foo": "src"}) + assert ext.name == "foo.dir.ext" From aca6c3ba37ca8f0855dfe60370b407abca4cb994 Mon Sep 17 00:00:00 2001 From: "Ralf W. Grosse-Kunstleve" Date: Tue, 13 Jul 2021 18:14:58 -0700 Subject: [PATCH 069/638] * Removing stray semicolons (discovered by running clang-format v12 followed by tools/check-style.sh). (#3087) * Manually moving `// NOLINT` comments so that clang-format does not move them to the wrong places. * Manually reformatting comments related to `static_assert`s so that clang-format does not need two passes. * Empty lines between #includes, to prevent clang-format from shuffling the order and thereby confusing MSVC 2015. * git diff -U0 --no-color HEAD^ | python3 $HOME/clone/llvm-project/clang/tools/clang-format/clang-format-diff.py -p1 -style=file -i --- include/pybind11/attr.h | 3 ++- include/pybind11/detail/common.h | 3 ++- tests/pybind11_tests.h | 7 ++++++- tests/test_buffers.cpp | 2 +- tests/test_callbacks.cpp | 2 +- tests/test_eigen.cpp | 5 ++++- tests/test_methods_and_attributes.cpp | 12 ++++++------ tests/test_smart_ptr.cpp | 2 +- 8 files changed, 23 insertions(+), 13 deletions(-) diff --git a/include/pybind11/attr.h b/include/pybind11/attr.h index 97147904ce..60ed9fd90e 100644 --- a/include/pybind11/attr.h +++ b/include/pybind11/attr.h @@ -62,7 +62,8 @@ struct metaclass { handle value; PYBIND11_DEPRECATED("py::metaclass() is no longer required. It's turned on by default now.") - metaclass() { } // NOLINT(modernize-use-equals-default): breaks MSVC 2015 when adding an attribute + // NOLINTNEXTLINE(modernize-use-equals-default): breaks MSVC 2015 when adding an attribute + metaclass() {} /// Override pybind11's default metaclass explicit metaclass(handle value) : value(value) { } diff --git a/include/pybind11/detail/common.h b/include/pybind11/detail/common.h index 1f7446103e..f804e7c85a 100644 --- a/include/pybind11/detail/common.h +++ b/include/pybind11/detail/common.h @@ -801,7 +801,8 @@ struct nodelete { template void operator()(T*) { } }; PYBIND11_NAMESPACE_BEGIN(detail) template struct overload_cast_impl { - constexpr overload_cast_impl() {}; // NOLINT(modernize-use-equals-default): MSVC 2015 needs this + // NOLINTNEXTLINE(modernize-use-equals-default): MSVC 2015 needs this + constexpr overload_cast_impl() {} template constexpr auto operator()(Return (*pf)(Args...)) const noexcept diff --git a/tests/pybind11_tests.h b/tests/pybind11_tests.h index ccb0529787..d970ba8bd4 100644 --- a/tests/pybind11_tests.h +++ b/tests/pybind11_tests.h @@ -1,10 +1,15 @@ #pragma once + +// This must be kept first for MSVC 2015. +// Do not remove the empty line between the #includes. #include + #include #if defined(_MSC_VER) && _MSC_VER < 1910 // We get some really long type names here which causes MSVC 2015 to emit warnings -# pragma warning(disable: 4503) // warning C4503: decorated name length exceeded, name was truncated +# pragma warning( \ + disable : 4503) // warning C4503: decorated name length exceeded, name was truncated #endif namespace py = pybind11; diff --git a/tests/test_buffers.cpp b/tests/test_buffers.cpp index 15dd4f1c74..e77c626f87 100644 --- a/tests/test_buffers.cpp +++ b/tests/test_buffers.cpp @@ -154,7 +154,7 @@ TEST_SUBMODULE(buffers, m) { py::format_descriptor::format(), 1); } - ConstBuffer() : value(new int32_t{0}) { }; + ConstBuffer() : value(new int32_t{0}) {} }; py::class_(m, "ConstBuffer", py::buffer_protocol()) .def(py::init<>()) diff --git a/tests/test_callbacks.cpp b/tests/test_callbacks.cpp index 7e79217a3d..a208b44d04 100644 --- a/tests/test_callbacks.cpp +++ b/tests/test_callbacks.cpp @@ -122,7 +122,7 @@ TEST_SUBMODULE(callbacks, m) { // [workaround(intel)] = default does not work here // Defaulting this destructor results in linking errors with the Intel compiler // (in Debug builds only, tested with icpc (ICC) 2021.1 Beta 20200827) - virtual ~AbstractBase() {}; // NOLINT(modernize-use-equals-default) + virtual ~AbstractBase() {} // NOLINT(modernize-use-equals-default) virtual unsigned int func() = 0; }; m.def("func_accepting_func_accepting_base", diff --git a/tests/test_eigen.cpp b/tests/test_eigen.cpp index ba74f58156..651be0575f 100644 --- a/tests/test_eigen.cpp +++ b/tests/test_eigen.cpp @@ -258,7 +258,10 @@ TEST_SUBMODULE(eigen, m) { m.def("dense_copy_r", [](const DenseMatrixR &m) -> DenseMatrixR { return m; }); m.def("dense_copy_c", [](const DenseMatrixC &m) -> DenseMatrixC { return m; }); // test_sparse, test_sparse_signature - m.def("sparse_r", [mat]() -> SparseMatrixR { return Eigen::SparseView(mat); }); //NOLINT(clang-analyzer-core.uninitialized.UndefReturn) + m.def("sparse_r", [mat]() -> SparseMatrixR { + // NOLINTNEXTLINE(clang-analyzer-core.uninitialized.UndefReturn) + return Eigen::SparseView(mat); + }); m.def("sparse_c", [mat]() -> SparseMatrixC { return Eigen::SparseView(mat); }); m.def("sparse_copy_r", [](const SparseMatrixR &m) -> SparseMatrixR { return m; }); m.def("sparse_copy_c", [](const SparseMatrixC &m) -> SparseMatrixC { return m; }); diff --git a/tests/test_methods_and_attributes.cpp b/tests/test_methods_and_attributes.cpp index 6e5999f0c4..9db31a0b82 100644 --- a/tests/test_methods_and_attributes.cpp +++ b/tests/test_methods_and_attributes.cpp @@ -123,7 +123,7 @@ class NoneCastTester { public: int answer = -1; NoneCastTester() = default; - NoneCastTester(int v) : answer(v) {}; + NoneCastTester(int v) : answer(v) {} }; struct StrIssue { @@ -390,14 +390,14 @@ TEST_SUBMODULE(methods_and_attributes, m) { .def("increase_value", &RegisteredDerived::increase_value) .def_readwrite("rw_value", &RegisteredDerived::rw_value) .def_readonly("ro_value", &RegisteredDerived::ro_value) - // These should trigger a static_assert if uncommented - //.def_readwrite("fails", &UserType::value) // should trigger a static_assert if uncommented - //.def_readonly("fails", &UserType::value) // should trigger a static_assert if uncommented + // Uncommenting the next line should trigger a static_assert: + // .def_readwrite("fails", &UserType::value) + // Uncommenting the next line should trigger a static_assert: + // .def_readonly("fails", &UserType::value) .def_property("rw_value_prop", &RegisteredDerived::get_int, &RegisteredDerived::set_int) .def_property_readonly("ro_value_prop", &RegisteredDerived::get_double) // This one is in the registered class: - .def("sum", &RegisteredDerived::sum) - ; + .def("sum", &RegisteredDerived::sum); using Adapted = decltype(py::method_adaptor(&RegisteredDerived::do_nothing)); static_assert(std::is_same::value, ""); diff --git a/tests/test_smart_ptr.cpp b/tests/test_smart_ptr.cpp index 534dec0be4..7fd5a9b36e 100644 --- a/tests/test_smart_ptr.cpp +++ b/tests/test_smart_ptr.cpp @@ -24,7 +24,7 @@ template class huge_unique_ptr { std::unique_ptr ptr; uint64_t padding[10]; public: - huge_unique_ptr(T *p) : ptr(p) {}; + huge_unique_ptr(T *p) : ptr(p) {} T *get() { return ptr.get(); } }; From 7331d381af4639f385fa07b5cb07d6c5dbacba71 Mon Sep 17 00:00:00 2001 From: Shane Loretz Date: Tue, 13 Jul 2021 21:21:55 -0700 Subject: [PATCH 070/638] Raise codec errors when casting to std::string (#2903) * Raise codec errors when casting to std::string Allow the codec's exception to be raised instead of RuntimeError when casting from py::str to std::string. PY2 allows ucs surrogates in UTF-8 conversion Signed-off-by: Shane Loretz Signed-off-by: Shane Loretz * Attempt to fix py2 error * Revert all unicode literals * Fixed Co-authored-by: Aaron Gokaslan --- include/pybind11/pytypes.h | 2 +- tests/test_pytypes.cpp | 3 +++ tests/test_pytypes.py | 8 ++++++++ 3 files changed, 12 insertions(+), 1 deletion(-) diff --git a/include/pybind11/pytypes.h b/include/pybind11/pytypes.h index 0fca32a4e9..b483fb323c 100644 --- a/include/pybind11/pytypes.h +++ b/include/pybind11/pytypes.h @@ -1015,7 +1015,7 @@ class str : public object { if (PyUnicode_Check(m_ptr)) { temp = reinterpret_steal(PyUnicode_AsUTF8String(m_ptr)); if (!temp) - pybind11_fail("Unable to extract string contents! (encoding issue)"); + throw error_already_set(); } char *buffer = nullptr; ssize_t length = 0; diff --git a/tests/test_pytypes.cpp b/tests/test_pytypes.cpp index 6ed59aad21..d70536d3f0 100644 --- a/tests/test_pytypes.cpp +++ b/tests/test_pytypes.cpp @@ -76,6 +76,9 @@ TEST_SUBMODULE(pytypes, m) { m.def("str_from_object", [](const py::object& obj) { return py::str(obj); }); m.def("repr_from_object", [](const py::object& obj) { return py::repr(obj); }); m.def("str_from_handle", [](py::handle h) { return py::str(h); }); + m.def("str_from_string_from_str", [](const py::str& obj) { + return py::str(static_cast(obj)); + }); m.def("str_format", []() { auto s1 = "{} + {} = {}"_s.format(1, 2, 3); diff --git a/tests/test_pytypes.py b/tests/test_pytypes.py index 25e9f6dffc..66d6d30a0c 100644 --- a/tests/test_pytypes.py +++ b/tests/test_pytypes.py @@ -133,6 +133,14 @@ def __repr__(self): else: assert m.str_from_handle(malformed_utf8) == "b'\\x80'" + assert m.str_from_string_from_str("this is a str") == "this is a str" + ucs_surrogates_str = u"\udcc3" + if env.PY2: + assert u"\udcc3" == m.str_from_string_from_str(ucs_surrogates_str) + else: + with pytest.raises(UnicodeEncodeError): + m.str_from_string_from_str(ucs_surrogates_str) + def test_bytes(doc): assert m.bytes_from_string().decode() == "foo" From cd061aeef164b380ba7a4b7699679a38f87cb111 Mon Sep 17 00:00:00 2001 From: Henry Schreiner Date: Wed, 14 Jul 2021 16:49:13 -0400 Subject: [PATCH 071/638] style: pre-commit cleanup (#3111) * style: disallow PyTest (should be pytest) * style: cleanup spell checking a bit * style: add a few items to the .gitignore --- .gitignore | 2 ++ .pre-commit-config.yaml | 10 +++------- docs/changelog.rst | 2 +- tests/CMakeLists.txt | 2 +- 4 files changed, 7 insertions(+), 9 deletions(-) diff --git a/.gitignore b/.gitignore index 3f36b89e0c..3cf4fbbda0 100644 --- a/.gitignore +++ b/.gitignore @@ -41,3 +41,5 @@ pybind11Targets.cmake /.vscode /pybind11/include/* /pybind11/share/* +/docs/_build/* +.ipynb_checkpoints/ diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 133eb6df4e..4b0a349165 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -92,12 +92,8 @@ repos: rev: v2.1.0 hooks: - id: codespell - name: codespell - description: Checks for common misspellings in text files. - entry: codespell - language: python - types: [text] - args: ["-q", "3", "--skip", "*.supp", "-L", "nd,ot,thist,readded"] + exclude: ".supp$" + args: ["-L", "nd,ot,thist"] # The original pybind11 checks for a few C++ style items - repo: local @@ -105,7 +101,7 @@ repos: - id: disallow-caps name: Disallow improper capitalization language: pygrep - entry: PyBind|Numpy|Cmake|CCache + entry: PyBind|Numpy|Cmake|CCache|PyTest exclude: .pre-commit-config.yaml - repo: local diff --git a/docs/changelog.rst b/docs/changelog.rst index d71fccc251..698b78c1a0 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -226,7 +226,7 @@ Bug fixes: * Fix ``py::gil_scoped_acquire`` assert with CPython 3.9 debug build. `#2683 `_ -* Fix issue with a test failing on PyTest 6.2. +* Fix issue with a test failing on pytest 6.2. `#2741 `_ Warning fixes: diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 3729b5c7df..dc70038dba 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -315,7 +315,7 @@ function(pybind11_enable_warnings target_name) endif() endif() - # Needs to be readded since the ordering requires these to be after the ones above + # Needs to be re-added since the ordering requires these to be after the ones above if(CMAKE_CXX_STANDARD AND CMAKE_CXX_COMPILER_ID MATCHES "Clang" AND PYTHON_VERSION VERSION_LESS 3.0) From c4b0dc7cdd9cd70acce65940305d039ef336fc23 Mon Sep 17 00:00:00 2001 From: Aaron Gokaslan Date: Wed, 14 Jul 2021 16:52:13 -0400 Subject: [PATCH 072/638] Add shellcheck style checking (#3114) --- .pre-commit-config.yaml | 5 +++++ tools/check-style.sh | 6 +++--- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 4b0a349165..c6f31b280b 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -95,6 +95,11 @@ repos: exclude: ".supp$" args: ["-L", "nd,ot,thist"] +- repo: https://github.com/shellcheck-py/shellcheck-py + rev: v0.7.2.1 + hooks: + - id: shellcheck + # The original pybind11 checks for a few C++ style items - repo: local hooks: diff --git a/tools/check-style.sh b/tools/check-style.sh index f7af2a4169..6d832523a7 100755 --- a/tools/check-style.sh +++ b/tools/check-style.sh @@ -16,11 +16,11 @@ check_style_errors=0 IFS=$'\n' -found="$(grep '\<\(if\|for\|while\|catch\)(\|){' $@ -rn --color=always)" +found="$(grep '\<\(if\|for\|while\|catch\)(\|){' "$@" -rn --color=always)" if [ -n "$found" ]; then echo -e '\033[31;01mError: found the following coding style problems:\033[0m' check_style_errors=1 - echo "$found" | sed -e 's/^/ /' + echo "${found//^/ /}" fi found="$(awk ' @@ -34,7 +34,7 @@ last && /^\s*{/ { last="" } { last = /(if|for|while|catch|switch)\s*\(.*\)\s*$/ ? $0 : "" } -' $(find include -type f) $@)" +' "$(find include -type f)" "$@")" if [ -n "$found" ]; then check_style_errors=1 echo -e '\033[31;01mError: braces should occur on the same line as the if/while/.. statement. Found issues in the following files:\033[0m' From 5cd37507572db8a849309340de9b64645c9094f0 Mon Sep 17 00:00:00 2001 From: Boris Staletic Date: Thu, 15 Jul 2021 16:38:52 +0000 Subject: [PATCH 073/638] Enable -Wstrict-aliasing warning (#2816) * Enable -Wstrict-aliasing warning * Narrow down the scope of -Wstrict-aliasing * Go home, MSVC, you're drunk * Make sure "pragma GCC" is not executed on ICC Co-authored-by: Yannick Jadoul --- include/pybind11/pybind11.h | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/include/pybind11/pybind11.h b/include/pybind11/pybind11.h index 616fa70255..b4aab51a7c 100644 --- a/include/pybind11/pybind11.h +++ b/include/pybind11/pybind11.h @@ -35,7 +35,6 @@ # pragma GCC diagnostic ignored "-Wunused-but-set-parameter" # pragma GCC diagnostic ignored "-Wunused-but-set-variable" # pragma GCC diagnostic ignored "-Wmissing-field-initializers" -# pragma GCC diagnostic ignored "-Wstrict-aliasing" # pragma GCC diagnostic ignored "-Wattributes" # if __GNUC__ >= 7 # pragma GCC diagnostic ignored "-Wnoexcept-type" @@ -49,10 +48,18 @@ #include "detail/init.h" #include +#include #include #include #include +#if defined(__cpp_lib_launder) && !(defined(_MSC_VER) && (_MSC_VER < 1914)) +# define PYBIND11_STD_LAUNDER std::launder +# define PYBIND11_HAS_STD_LAUNDER 1 +#else +# define PYBIND11_STD_LAUNDER +# define PYBIND11_HAS_STD_LAUNDER 0 +#endif #if defined(__GNUG__) && !defined(__clang__) # include #endif @@ -151,8 +158,21 @@ class cpp_function : public function { #if defined(__GNUG__) && __GNUC__ >= 6 && !defined(__clang__) && !defined(__INTEL_COMPILER) # pragma GCC diagnostic pop #endif +#if defined(__GNUG__) && !PYBIND11_HAS_STD_LAUNDER && !defined(__INTEL_COMPILER) +# pragma GCC diagnostic push +# pragma GCC diagnostic ignored "-Wstrict-aliasing" +#endif + // UB without std::launder, but without breaking ABI and/or + // a significant refactoring it's "impossible" to solve. if (!std::is_trivially_destructible::value) - rec->free_data = [](function_record *r) { ((capture *) &r->data)->~capture(); }; + rec->free_data = [](function_record *r) { + auto data = PYBIND11_STD_LAUNDER((capture *) &r->data); + (void) data; + data->~capture(); + }; +#if defined(__GNUG__) && !PYBIND11_HAS_STD_LAUNDER && !defined(__INTEL_COMPILER) +# pragma GCC diagnostic pop +#endif } else { rec->data[0] = new capture { std::forward(f) }; rec->free_data = [](function_record *r) { delete ((capture *) r->data[0]); }; From 31843d455dd347ce9ccb27dd5b51822fe2b561c5 Mon Sep 17 00:00:00 2001 From: Henry Schreiner Date: Thu, 15 Jul 2021 15:01:13 -0400 Subject: [PATCH 074/638] docs: reduce visibility of 3.9.0 warning (#3105) --- README.rst | 15 +-------------- docs/limitations.rst | 20 ++++++++++---------- 2 files changed, 11 insertions(+), 24 deletions(-) diff --git a/README.rst b/README.rst index 6c2a255109..69a7797e83 100644 --- a/README.rst +++ b/README.rst @@ -13,19 +13,6 @@ .. start -.. warning:: - - Combining older versions of pybind11 (< 2.6.0) with Python 3.9.0 will - trigger undefined behavior that typically manifests as crashes during - interpreter shutdown (but could also destroy your data. **You have been - warned.**) - - We recommend that you update to the latest patch release of Python (3.9.1), - which includes a `fix `_ - that resolves this problem. If you do use Python 3.9.0, please update to - the latest version of pybind11 (2.6.0 or newer), which includes a temporary - workaround specifically when Python 3.9.0 is detected at runtime. - **pybind11** is a lightweight header-only library that exposes C++ types in Python and vice versa, mainly to create Python bindings of existing @@ -110,7 +97,7 @@ goodies: transparently applied to all entries of one or more NumPy array arguments. -- Python’s slice-based access and assignment operations can be +- Python's slice-based access and assignment operations can be supported with just a few lines of code. - Everything is contained in just a few header files; there is no need diff --git a/docs/limitations.rst b/docs/limitations.rst index be7300cdb6..def5ad659c 100644 --- a/docs/limitations.rst +++ b/docs/limitations.rst @@ -57,16 +57,16 @@ clean, well written patch would likely be accepted to solve them. Python 3.9.0 warning ^^^^^^^^^^^^^^^^^^^^ -Combining older versions of pybind11 (< 2.6.0) with Python on 3.9.0 will -trigger undefined behavior that typically manifests as crashes during +Combining older versions of pybind11 (< 2.6.0) with Python on exactly 3.9.0 +will trigger undefined behavior that typically manifests as crashes during interpreter shutdown (but could also destroy your data. **You have been warned**). -This issue has been -`fixed in Python `_. As a -mitigation until 3.9.1 is released and commonly used, pybind11 (2.6.0 or newer) -includes a temporary workaround specifically when Python 3.9.0 is detected at -runtime, leaking about 50 bytes of memory when a callback function is garbage -collected. For reference; the pybind11 test suite has about 2,000 such -callbacks, but only 49 are garbage collected before the end-of-process. Wheels -built with Python 3.9.0 will correctly avoid the leak when run in Python 3.9.1. +This issue was `fixed in Python `_. +As a mitigation for this bug, pybind11 2.6.0 or newer includes a workaround +specifically when Python 3.9.0 is detected at runtime, leaking about 50 bytes +of memory when a callback function is garbage collected. For reference, the +pybind11 test suite has about 2,000 such callbacks, but only 49 are garbage +collected before the end-of-process. Wheels (even if built with Python 3.9.0) +will correctly avoid the leak when run in Python 3.9.1, and this does not +affect other 3.X versions. From 2415c09459efb0c6ab33d9dfe2a555237577dc6e Mon Sep 17 00:00:00 2001 From: Henry Schreiner Date: Thu, 15 Jul 2021 15:01:29 -0400 Subject: [PATCH 075/638] feat(package): support pipx run (#3117) --- tools/setup_main.py.in | 3 +++ 1 file changed, 3 insertions(+) diff --git a/tools/setup_main.py.in b/tools/setup_main.py.in index bcd4ef4aeb..533a75ae71 100644 --- a/tools/setup_main.py.in +++ b/tools/setup_main.py.in @@ -32,6 +32,9 @@ setup( entry_points={ "console_scripts": [ "pybind11-config = pybind11.__main__:main", + ], + "pipx.run": [ + "pybind11 = pybind11.__main__:main", ] }, cmdclass=cmdclass From 55f6f6e9bf7f0848a1c1415f90120be9076e8add Mon Sep 17 00:00:00 2001 From: Axel Huebl Date: Thu, 15 Jul 2021 12:41:36 -0700 Subject: [PATCH 076/638] Fix: RTD Docutils Build (#3119) The docutils 0.17+ release uses more semantic HTML5 tags, which the RTD theme does not (yet) know. This breaks side bar, lists and other elements. --- docs/requirements.txt | 3 +++ 1 file changed, 3 insertions(+) diff --git a/docs/requirements.txt b/docs/requirements.txt index 671dc4b3f3..8f293b5d34 100644 --- a/docs/requirements.txt +++ b/docs/requirements.txt @@ -1,4 +1,7 @@ breathe==4.26.1 +# docutils 0.17 breaks HTML tags & RTD theme +# https://github.com/sphinx-doc/sphinx/issues/9001 +docutils==0.16 sphinx==3.3.1 sphinx_rtd_theme==0.5.0 sphinxcontrib-moderncmakedomain==3.17 From 1b10292c0597a497cdee43dcba731bcdd1ce0c49 Mon Sep 17 00:00:00 2001 From: Henry Schreiner Date: Thu, 15 Jul 2021 16:54:40 -0400 Subject: [PATCH 077/638] chore: support PDF from nox (#3121) --- noxfile.py | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/noxfile.py b/noxfile.py index 3bc61273ff..f0f6680159 100644 --- a/noxfile.py +++ b/noxfile.py @@ -53,14 +53,18 @@ def docs(session: nox.Session) -> None: session.install("-r", "docs/requirements.txt") session.chdir("docs") + + if "pdf" in session.posargs: + session.run("sphinx-build", "-M", "latexpdf", ".", "_build") + return + session.run("sphinx-build", "-M", "html", ".", "_build") - if session.posargs: - if "serve" in session.posargs: - print("Launching docs at http://localhost:8000/ - use Ctrl-C to quit") - session.run("python", "-m", "http.server", "8000", "-d", "_build/html") - else: - print("Unsupported argument to docs") + if "serve" in session.posargs: + session.log("Launching docs at http://localhost:8000/ - use Ctrl-C to quit") + session.run("python", "-m", "http.server", "8000", "-d", "_build/html") + elif session.posargs: + session.error("Unsupported argument to docs") @nox.session(reuse_venv=True) From 4359e00b97154c663b5710a5aef1c961d48753d5 Mon Sep 17 00:00:00 2001 From: "Ralf W. Grosse-Kunstleve" Date: Thu, 15 Jul 2021 15:00:57 -0700 Subject: [PATCH 078/638] Introducing PYBIND11_VERSION_HEX (#3120) * Introducing PYBIND11_VERSION_HEX (better late than never!) * PYBIND11_VERSION_HEX consistency check in setup.py --- include/pybind11/detail/common.h | 4 ++++ setup.py | 39 ++++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+) diff --git a/include/pybind11/detail/common.h b/include/pybind11/detail/common.h index f804e7c85a..f036269afd 100644 --- a/include/pybind11/detail/common.h +++ b/include/pybind11/detail/common.h @@ -13,6 +13,10 @@ #define PYBIND11_VERSION_MINOR 6 #define PYBIND11_VERSION_PATCH 3.dev1 +// Similar to Python's convention: https://docs.python.org/3/c-api/apiabiversion.html +// Additional convention: 0xD = dev +#define PYBIND11_VERSION_HEX 0x020603D1 + #define PYBIND11_NAMESPACE_BEGIN(name) namespace name { #define PYBIND11_NAMESPACE_END(name) } diff --git a/setup.py b/setup.py index 3a0327982a..ced3d69581 100644 --- a/setup.py +++ b/setup.py @@ -19,6 +19,36 @@ r"^\s*#\s*define\s+PYBIND11_VERSION_([A-Z]+)\s+(.*)$", re.MULTILINE ) + +def build_expected_version_hex(matches): + patch_level_serial = matches["PATCH"] + serial = None + try: + major = int(matches["MAJOR"]) + minor = int(matches["MINOR"]) + flds = patch_level_serial.split(".") + if flds: + patch = int(flds[0]) + level = None + if len(flds) == 1: + level = "0" + serial = 0 + elif len(flds) == 2: + level_serial = flds[1] + for level in ("a", "b", "c", "dev"): + if level_serial.startswith(level): + serial = int(level_serial[len(level) :]) + break + except ValueError: + pass + if serial is None: + msg = 'Invalid PYBIND11_VERSION_PATCH: "{}"'.format(patch_level_serial) + raise RuntimeError(msg) + return "0x{:02x}{:02x}{:02x}{}{:x}".format( + major, minor, patch, level[:1].upper(), serial + ) + + # PYBIND11_GLOBAL_SDIST will build a different sdist, with the python-headers # files, and the sys.prefix files (CMake and headers). @@ -49,6 +79,15 @@ ) raise RuntimeError(msg) +version_hex = matches.get("HEX", "MISSING") +expected_version_hex = build_expected_version_hex(matches) +if version_hex != expected_version_hex: + msg = "PYBIND11_VERSION_HEX {} does not match expected value {}!".format( + version_hex, + expected_version_hex, + ) + raise RuntimeError(msg) + def get_and_replace(filename, binary=False, **opts): with open(filename, "rb" if binary else "r") as f: From 6642f389dc5b7b05b5682ba5db8460817649791d Mon Sep 17 00:00:00 2001 From: Henry Schreiner Date: Thu, 15 Jul 2021 20:00:07 -0400 Subject: [PATCH 079/638] docs: update changelog (#3122) --- docs/changelog.rst | 27 ++++++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/docs/changelog.rst b/docs/changelog.rst index 698b78c1a0..31dcae2069 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -30,6 +30,10 @@ New features: ``os.PathLike`` to ``std::filesystem::path``. `#2730 `_ +* A ``PYBIND11_VERSION_HEX`` define was added, similar to ``PY_VERSION_HEX``. + `#3120 `_ + + Changes: @@ -101,6 +105,10 @@ Fixes: from a ``shared_from_this`` class. `#2819 `_ +* Allow the codec's exception to be raised instead of :code:`RuntimeError` when + casting from :code:`py::str` to :code:`std::string`. + `#2903 `_ + Build system improvements: @@ -121,6 +129,10 @@ Build system improvements: * Clang on Windows: do not pass ``/MP`` (ignored flag). `#2824 `_ +* ``pybind11.setup_helpers.intree_extensions`` can be used to generate + ``Pybind11Extension`` instances from cpp files placed in the Python package + source tree. + `#2831 `_ Backend and tidying up: @@ -129,8 +141,10 @@ Backend and tidying up: `#3046 `_, `#3049 `_, `#3051 `_, - `#3052 `_, and - `#3080 `_ + `#3052 `_, + `#3080 `_, and + `#3094 `_ + * Checks for common misspellings were added to the pre-commit hooks. `#3076 `_ @@ -143,7 +157,14 @@ Backend and tidying up: `#2956 `_ * Added nox support for easier local testing and linting of contributions. - `#3101 `_ + `#3101 `_ and + `#3121 `_ + +* Avoid RTD style issue with docutils 0.17+. + `#3119 `_ + +* Support pipx run, such as ``pipx run pybind11 --include`` for a quick compile. + `#3117 `_ From 65e95ea8675ea34bdd566d6461330f25c651e5a8 Mon Sep 17 00:00:00 2001 From: Henry Schreiner Date: Fri, 16 Jul 2021 09:27:47 -0400 Subject: [PATCH 080/638] chore: bump to 2.7.0 (#3123) --- docs/changelog.rst | 4 ++-- docs/release.rst | 2 +- include/pybind11/detail/common.h | 6 +++--- pybind11/_version.py | 2 +- 4 files changed, 7 insertions(+), 7 deletions(-) diff --git a/docs/changelog.rst b/docs/changelog.rst index 31dcae2069..293e9fb7ff 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -6,8 +6,8 @@ Changelog Starting with version 1.8.0, pybind11 releases use a `semantic versioning `_ policy. -v2.7.0 (TBA, not yet released) ------------------------------- +v2.7.0 (Jul 16, 2021) +--------------------- New features: diff --git a/docs/release.rst b/docs/release.rst index 9dbff03838..2c64e64ab8 100644 --- a/docs/release.rst +++ b/docs/release.rst @@ -53,7 +53,7 @@ To release a new version of pybind11: name like "Version X.Y.Z", and optionally copy-and-paste the changelog into the description (processed as markdown by Pandoc). Check "pre-release" if this is a beta/RC. You can get partway there with - ``cat docs/changelog.rst | pandsoc -f rst -t markdown``. + ``cat docs/changelog.rst | pandoc -f rst -t markdown``. - CLI method: with ``gh`` installed, run ``gh release create vX.Y.Z -t "Version X.Y.Z"`` If this is a pre-release, add ``-p``. diff --git a/include/pybind11/detail/common.h b/include/pybind11/detail/common.h index f036269afd..cf20e4a2eb 100644 --- a/include/pybind11/detail/common.h +++ b/include/pybind11/detail/common.h @@ -10,12 +10,12 @@ #pragma once #define PYBIND11_VERSION_MAJOR 2 -#define PYBIND11_VERSION_MINOR 6 -#define PYBIND11_VERSION_PATCH 3.dev1 +#define PYBIND11_VERSION_MINOR 7 +#define PYBIND11_VERSION_PATCH 0 // Similar to Python's convention: https://docs.python.org/3/c-api/apiabiversion.html // Additional convention: 0xD = dev -#define PYBIND11_VERSION_HEX 0x020603D1 +#define PYBIND11_VERSION_HEX 0x02070000 #define PYBIND11_NAMESPACE_BEGIN(name) namespace name { #define PYBIND11_NAMESPACE_END(name) } diff --git a/pybind11/_version.py b/pybind11/_version.py index ffadcca5a8..5c0fa0b44f 100644 --- a/pybind11/_version.py +++ b/pybind11/_version.py @@ -8,5 +8,5 @@ def _to_int(s): return s -__version__ = "2.6.3.dev1" +__version__ = "2.7.0" version_info = tuple(_to_int(s) for s in __version__.split(".")) From 74935f8d675a308244a7cdcf92aa97f41f496124 Mon Sep 17 00:00:00 2001 From: Henry Schreiner Date: Sat, 17 Jul 2021 11:50:42 -0400 Subject: [PATCH 081/638] chore: post-release (#3128) --- docs/changelog.rst | 5 +++ docs/release.rst | 64 ++++++++++++++++---------------- include/pybind11/detail/common.h | 4 +- pybind11/_version.py | 2 +- 4 files changed, 41 insertions(+), 34 deletions(-) diff --git a/docs/changelog.rst b/docs/changelog.rst index 293e9fb7ff..5e8a63258f 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -6,6 +6,11 @@ Changelog Starting with version 1.8.0, pybind11 releases use a `semantic versioning `_ policy. +Next version (WIP) +------------------ + + + v2.7.0 (Jul 16, 2021) --------------------- diff --git a/docs/release.rst b/docs/release.rst index 2c64e64ab8..e4f3d1902a 100644 --- a/docs/release.rst +++ b/docs/release.rst @@ -15,31 +15,33 @@ For example: For beta, ``PYBIND11_VERSION_PATCH`` should be ``Z.b1``. RC's can be ``Z.rc1``. Always include the dot (even though PEP 440 allows it to be dropped). For a -final release, this must be a simple integer. +final release, this must be a simple integer. There is also a HEX version of +the version just below. To release a new version of pybind11: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - Update the version number - - Update ``PYBIND11_VERSION_MAJOR`` etc. in - ``include/pybind11/detail/common.h``. PATCH should be a simple integer. - - Update ``pybind11/_version.py`` (match above) - - Ensure that all the information in ``setup.cfg`` is up-to-date, like - supported Python versions. - - Add release date in ``docs/changelog.rst``. - - Check to make sure - `needs-changelog `_ - issues are entered in the changelog (clear the label when done). - - ``git add`` and ``git commit``, ``git push``. **Ensure CI passes**. (If it - fails due to a known flake issue, either ignore or restart CI.) + - Update ``PYBIND11_VERSION_MAJOR`` etc. in + ``include/pybind11/detail/common.h``. PATCH should be a simple integer. + - Update the version HEX just below, as well. + - Update ``pybind11/_version.py`` (match above) + - Ensure that all the information in ``setup.cfg`` is up-to-date, like + supported Python versions. + - Add release date in ``docs/changelog.rst``. + - Check to make sure + `needs-changelog `_ + issues are entered in the changelog (clear the label when done). + - ``git add`` and ``git commit``, ``git push``. **Ensure CI passes**. (If it + fails due to a known flake issue, either ignore or restart CI.) - Add a release branch if this is a new minor version, or update the existing release branch if it is a patch version - - New branch: ``git checkout -b vX.Y``, ``git push -u origin vX.Y`` - - Update branch: ``git checkout vX.Y``, ``git merge ``, ``git push`` + - New branch: ``git checkout -b vX.Y``, ``git push -u origin vX.Y`` + - Update branch: ``git checkout vX.Y``, ``git merge ``, ``git push`` - Update tags (optional; if you skip this, the GitHub release makes a - non-annotated tag for you) - - ``git tag -a vX.Y.Z -m 'vX.Y.Z release'``. - - ``git push --tags``. + non-annotated tag for you) + - ``git tag -a vX.Y.Z -m 'vX.Y.Z release'``. + - ``git push --tags``. - Update stable - ``git checkout stable`` - ``git merge master`` @@ -48,22 +50,22 @@ To release a new version of pybind11: notifications to users watching releases, and also uploads PyPI packages). (Note: if you do not use an existing tag, this creates a new lightweight tag for you, so you could skip the above step). - - GUI method: click "Create a new release" on the far right, fill in the tag - name (if you didn't tag above, it will be made here), fill in a release - name like "Version X.Y.Z", and optionally copy-and-paste the changelog into - the description (processed as markdown by Pandoc). Check "pre-release" if - this is a beta/RC. You can get partway there with - ``cat docs/changelog.rst | pandoc -f rst -t markdown``. - - CLI method: with ``gh`` installed, run ``gh release create vX.Y.Z -t "Version X.Y.Z"`` - If this is a pre-release, add ``-p``. + - GUI method: click "Create a new release" on the far right, fill in the tag + name (if you didn't tag above, it will be made here), fill in a release + name like "Version X.Y.Z", and optionally copy-and-paste the changelog into + the description (processed as markdown by Pandoc). Check "pre-release" if + this is a beta/RC. You can get partway there with + ``cat docs/changelog.rst | pandoc -f rst -t gfm``. + - CLI method: with ``gh`` installed, run ``gh release create vX.Y.Z -t "Version X.Y.Z"`` + If this is a pre-release, add ``-p``. - Get back to work - - Make sure you are on master, not somewhere else: ``git checkout master`` - - Update version macros in ``include/pybind11/detail/common.h`` (set PATCH to - ``0.dev1`` and increment MINOR). - - Update ``_version.py`` to match - - Add a spot for in-development updates in ``docs/changelog.rst``. - - ``git add``, ``git commit``, ``git push`` + - Make sure you are on master, not somewhere else: ``git checkout master`` + - Update version macros in ``include/pybind11/detail/common.h`` (set PATCH to + ``0.dev1`` and increment MINOR). + - Update ``_version.py`` to match + - Add a spot for in-development updates in ``docs/changelog.rst``. + - ``git add``, ``git commit``, ``git push`` If a version branch is updated, remember to set PATCH to ``1.dev1``. diff --git a/include/pybind11/detail/common.h b/include/pybind11/detail/common.h index cf20e4a2eb..52b0193350 100644 --- a/include/pybind11/detail/common.h +++ b/include/pybind11/detail/common.h @@ -11,11 +11,11 @@ #define PYBIND11_VERSION_MAJOR 2 #define PYBIND11_VERSION_MINOR 7 -#define PYBIND11_VERSION_PATCH 0 +#define PYBIND11_VERSION_PATCH 1.dev1 // Similar to Python's convention: https://docs.python.org/3/c-api/apiabiversion.html // Additional convention: 0xD = dev -#define PYBIND11_VERSION_HEX 0x02070000 +#define PYBIND11_VERSION_HEX 0x020701D1 #define PYBIND11_NAMESPACE_BEGIN(name) namespace name { #define PYBIND11_NAMESPACE_END(name) } diff --git a/pybind11/_version.py b/pybind11/_version.py index 5c0fa0b44f..2bb14b963d 100644 --- a/pybind11/_version.py +++ b/pybind11/_version.py @@ -8,5 +8,5 @@ def _to_int(s): return s -__version__ = "2.7.0" +__version__ = "2.7.1.dev1" version_info = tuple(_to_int(s) for s in __version__.split(".")) From 34f587dd2398efb933b5b8d97b73791ddcb4b87b Mon Sep 17 00:00:00 2001 From: "Ralf W. Grosse-Kunstleve" Date: Sat, 17 Jul 2021 08:54:31 -0700 Subject: [PATCH 082/638] Removing all warning pragmas that have not effect. (#3127) --- include/pybind11/pybind11.h | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/include/pybind11/pybind11.h b/include/pybind11/pybind11.h index b4aab51a7c..af8e730d3f 100644 --- a/include/pybind11/pybind11.h +++ b/include/pybind11/pybind11.h @@ -12,29 +12,19 @@ #if defined(__INTEL_COMPILER) # pragma warning push -# pragma warning disable 68 // integer conversion resulted in a change of sign -# pragma warning disable 186 // pointless comparison of unsigned integer with zero # pragma warning disable 878 // incompatible exception specifications -# pragma warning disable 1334 // the "template" keyword used for syntactic disambiguation may only be used within a template -# pragma warning disable 1682 // implicit conversion of a 64-bit integral type to a smaller integral type (potential portability problem) -# pragma warning disable 1786 // function "strdup" was declared deprecated -# pragma warning disable 1875 // offsetof applied to non-POD (Plain Old Data) types is nonstandard # pragma warning disable 2196 // warning #2196: routine is both "inline" and "noinline" #elif defined(_MSC_VER) # pragma warning(push) # pragma warning(disable: 4100) // warning C4100: Unreferenced formal parameter # pragma warning(disable: 4127) // warning C4127: Conditional expression is constant -# pragma warning(disable: 4512) // warning C4512: Assignment operator was implicitly defined as deleted # pragma warning(disable: 4800) // warning C4800: 'int': forcing value to bool 'true' or 'false' (performance warning) # pragma warning(disable: 4996) // warning C4996: The POSIX name for this item is deprecated. Instead, use the ISO C and C++ conformant name -# pragma warning(disable: 4702) // warning C4702: unreachable code # pragma warning(disable: 4522) // warning C4522: multiple assignment operators specified # pragma warning(disable: 4505) // warning C4505: 'PySlice_GetIndicesEx': unreferenced local function has been removed (PyPy only) #elif defined(__GNUG__) && !defined(__clang__) # pragma GCC diagnostic push # pragma GCC diagnostic ignored "-Wunused-but-set-parameter" -# pragma GCC diagnostic ignored "-Wunused-but-set-variable" -# pragma GCC diagnostic ignored "-Wmissing-field-initializers" # pragma GCC diagnostic ignored "-Wattributes" # if __GNUC__ >= 7 # pragma GCC diagnostic ignored "-Wnoexcept-type" From 9626483cdfb1e9d22275d7bd059822e08bdf55c2 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 20 Jul 2021 11:29:42 -0400 Subject: [PATCH 083/638] [pre-commit.ci] pre-commit autoupdate (#3134) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit updates: - [github.com/asottile/pyupgrade: v2.21.0 → v2.21.2](https://github.com/asottile/pyupgrade/compare/v2.21.0...v2.21.2) - [github.com/psf/black: 21.6b0 → 21.7b0](https://github.com/psf/black/compare/21.6b0...21.7b0) - https://gitlab.com/pycqa/flake8 → https://github.com/PyCQA/flake8 --- .pre-commit-config.yaml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index c6f31b280b..aa7b4774e3 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -31,13 +31,13 @@ repos: exclude: ^noxfile.py$ - repo: https://github.com/asottile/pyupgrade - rev: v2.21.0 + rev: v2.21.2 hooks: - id: pyupgrade # Black, the code formatter, natively supports pre-commit - repo: https://github.com/psf/black - rev: 21.6b0 + rev: 21.7b0 hooks: - id: black # By default, this ignores pyi files, though black supports them @@ -51,7 +51,7 @@ repos: - id: remove-tabs # Flake8 also supports pre-commit natively (same author) -- repo: https://gitlab.com/pycqa/flake8 +- repo: https://github.com/PyCQA/flake8 rev: 3.9.2 hooks: - id: flake8 From 6d5d4e738caad61152427f2b3b0c3fa09b52ce03 Mon Sep 17 00:00:00 2001 From: blacktea <42189825+blacktea@users.noreply.github.com> Date: Tue, 20 Jul 2021 18:48:09 +0300 Subject: [PATCH 084/638] Move object in pop method of List. (#3116) * Move item instead of copy. * Make Clang 3.6 happy. Co-authored-by: c99 --- include/pybind11/stl_bind.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/include/pybind11/stl_bind.h b/include/pybind11/stl_bind.h index effac0b931..82317b3791 100644 --- a/include/pybind11/stl_bind.h +++ b/include/pybind11/stl_bind.h @@ -191,7 +191,7 @@ void vector_modifiers(enable_if_t Date: Wed, 21 Jul 2021 08:22:18 -0400 Subject: [PATCH 085/638] Feature/local exception translator (#2650) * Create a module_internals struct Since we now have two things that are going to be module local, it felt correct to add a struct to manage them. * Add local exception translators These are added via the register_local_exception_translator function and are then applied before the global translators * Add unit tests to show the local exception translator works * Fix a bug in the unit test with the string value of KeyError * Fix a formatting issue * Rename registered_local_types_cpp() Rename it to get_registered_local_types_cpp() to disambiguate from the new member of module_internals * Add additional comments to new local exception code path * Add a register_local_exception function * Add additional unit tests for register_local_exception * Use get_local_internals like get_internals * Update documentation for new local exception feature * Add back a missing space * Clean-up some issues in the docs * Remove the code duplication when translating exceptions Separated out the exception processing into a standalone function in the details namespace. Clean-up some comments as per PR notes as well * Remove the code duplication in register_exception * Cleanup some formatting things caught by clang-format * Remove the templates from exception translators But I added a using declaration to alias the type. * Remove the extra local from local_internals variable names * Add an extra explanatory comment to local_internals * Fix a typo in the code --- docs/advanced/exceptions.rst | 72 +++++++++++-- include/pybind11/detail/class.h | 2 +- include/pybind11/detail/internals.h | 27 ++++- include/pybind11/detail/type_caster_base.h | 2 +- include/pybind11/pybind11.h | 119 ++++++++++++++++----- tests/local_bindings.h | 19 ++++ tests/pybind11_cross_module_tests.cpp | 16 ++- tests/test_exceptions.cpp | 31 +++++- tests/test_exceptions.py | 29 ++++- tests/test_local_bindings.cpp | 2 +- 10 files changed, 274 insertions(+), 45 deletions(-) diff --git a/docs/advanced/exceptions.rst b/docs/advanced/exceptions.rst index f70c202fd5..738fb0ba9c 100644 --- a/docs/advanced/exceptions.rst +++ b/docs/advanced/exceptions.rst @@ -75,9 +75,10 @@ Registering custom translators If the default exception conversion policy described above is insufficient, pybind11 also provides support for registering custom exception translators. -To register a simple exception conversion that translates a C++ exception into -a new Python exception using the C++ exception's ``what()`` method, a helper -function is available: +Similar to pybind11 classes, exception translators can be local to the module +they are defined in or global to the entire python session. To register a simple +exception conversion that translates a C++ exception into a new Python exception +using the C++ exception's ``what()`` method, a helper function is available: .. code-block:: cpp @@ -87,12 +88,20 @@ This call creates a Python exception class with the name ``PyExp`` in the given module and automatically converts any encountered exceptions of type ``CppExp`` into Python exceptions of type ``PyExp``. +A matching function is available for registering a local exception translator: + +.. code-block:: cpp + + py::register_local_exception(module, "PyExp"); + + It is possible to specify base class for the exception using the third parameter, a `handle`: .. code-block:: cpp py::register_exception(module, "PyExp", PyExc_RuntimeError); + py::register_local_exception(module, "PyExp", PyExc_RuntimeError); Then `PyExp` can be caught both as `PyExp` and `RuntimeError`. @@ -100,16 +109,18 @@ The class objects of the built-in Python exceptions are listed in the Python documentation on `Standard Exceptions `_. The default base class is `PyExc_Exception`. -When more advanced exception translation is needed, the function -``py::register_exception_translator(translator)`` can be used to register +When more advanced exception translation is needed, the functions +``py::register_exception_translator(translator)`` and +``py::register_local_exception_translator(translator)`` can be used to register functions that can translate arbitrary exception types (and which may include -additional logic to do so). The function takes a stateless callable (e.g. a +additional logic to do so). The functions takes a stateless callable (e.g. a function pointer or a lambda function without captured variables) with the call signature ``void(std::exception_ptr)``. When a C++ exception is thrown, the registered exception translators are tried in reverse order of registration (i.e. the last registered translator gets the -first shot at handling the exception). +first shot at handling the exception). All local translators will be tried +before a global translator is tried. Inside the translator, ``std::rethrow_exception`` should be used within a try block to re-throw the exception. One or more catch clauses to catch @@ -168,6 +179,53 @@ section. with ``-fvisibility=hidden``. Therefore exceptions that are used across ABI boundaries need to be explicitly exported, as exercised in ``tests/test_exceptions.h``. See also: "Problems with C++ exceptions" under `GCC Wiki `_. + +Local vs Global Exception Translators +===================================== + +When a global exception translator is registered, it will be applied across all +modules in the reverse order of registration. This can create behavior where the +order of module import influences how exceptions are translated. + +If module1 has the following translator: + +.. code-block:: cpp + + py::register_exception_translator([](std::exception_ptr p) { + try { + if (p) std::rethrow_exception(p); + } catch (const std::invalid_argument &e) { + PyErr_SetString("module1 handled this") + } + } + +and module2 has the following similar translator: + +.. code-block:: cpp + + py::register_exception_translator([](std::exception_ptr p) { + try { + if (p) std::rethrow_exception(p); + } catch (const std::invalid_argument &e) { + PyErr_SetString("module2 handled this") + } + } + +then which translator handles the invalid_argument will be determined by the +order that module1 and module2 are imported. Since exception translators are +applied in the reverse order of registration, which ever module was imported +last will "win" and that translator will be applied. + +If there are multiple pybind11 modules that share exception types (either +standard built-in or custom) loaded into a single python instance and +consistent error handling behavior is needed, then local translators should be +used. + +Changing the previous example to use ``register_local_exception_translator`` +would mean that when invalid_argument is thrown in the module2 code, the +module2 translator will always handle it, while in module1, the module1 +translator will do the same. + .. _handling_python_exceptions_cpp: Handling exceptions from Python in C++ diff --git a/include/pybind11/detail/class.h b/include/pybind11/detail/class.h index 5fee3318b0..15e09165de 100644 --- a/include/pybind11/detail/class.h +++ b/include/pybind11/detail/class.h @@ -209,7 +209,7 @@ extern "C" inline void pybind11_meta_dealloc(PyObject *obj) { internals.direct_conversions.erase(tindex); if (tinfo->module_local) - registered_local_types_cpp().erase(tindex); + get_local_internals().registered_types_cpp.erase(tindex); else internals.registered_types_cpp.erase(tindex); internals.registered_types_py.erase(tinfo->type); diff --git a/include/pybind11/detail/internals.h b/include/pybind11/detail/internals.h index 5578c65160..a621aed2a6 100644 --- a/include/pybind11/detail/internals.h +++ b/include/pybind11/detail/internals.h @@ -12,7 +12,11 @@ #include "../pytypes.h" PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE) + +using ExceptionTranslator = void (*)(std::exception_ptr); + PYBIND11_NAMESPACE_BEGIN(detail) + // Forward declarations inline PyTypeObject *make_static_property_type(); inline PyTypeObject *make_default_metaclass(); @@ -100,7 +104,7 @@ struct internals { std::unordered_set, override_hash> inactive_override_cache; type_map> direct_conversions; std::unordered_map> patients; - std::forward_list registered_exception_translators; + std::forward_list registered_exception_translators; std::unordered_map shared_data; // Custom data to be shared across extensions std::vector loader_patient_stack; // Used by `loader_life_support` std::forward_list static_strings; // Stores the std::strings backing detail::c_str() @@ -313,12 +317,25 @@ PYBIND11_NOINLINE inline internals &get_internals() { return **internals_pp; } -/// Works like `internals.registered_types_cpp`, but for module-local registered types: -inline type_map ®istered_local_types_cpp() { - static type_map locals{}; - return locals; + +// the internals struct (above) is shared between all the modules. local_internals are only +// for a single module. Any changes made to internals may require an update to +// PYBIND11_INTERNALS_VERSION, breaking backwards compatibility. local_internals is, by design, +// restricted to a single module. Whether a module has local internals or not should not +// impact any other modules, because the only things accessing the local internals is the +// module that contains them. +struct local_internals { + type_map registered_types_cpp; + std::forward_list registered_exception_translators; +}; + +/// Works like `get_internals`, but for things which are locally registered. +inline local_internals &get_local_internals() { + static local_internals locals; + return locals; } + /// Constructs a std::string with the given arguments, stores it in `internals`, and returns its /// `c_str()`. Such strings objects have a long storage duration -- the internal strings are only /// cleared when the program exits or after interpreter shutdown (when embedding), and so are diff --git a/include/pybind11/detail/type_caster_base.h b/include/pybind11/detail/type_caster_base.h index 451690dc73..bdf3a39822 100644 --- a/include/pybind11/detail/type_caster_base.h +++ b/include/pybind11/detail/type_caster_base.h @@ -160,7 +160,7 @@ PYBIND11_NOINLINE inline detail::type_info* get_type_info(PyTypeObject *type) { } inline detail::type_info *get_local_type_info(const std::type_index &tp) { - auto &locals = registered_local_types_cpp(); + auto &locals = get_local_internals().registered_types_cpp; auto it = locals.find(tp); if (it != locals.end()) return it->second; diff --git a/include/pybind11/pybind11.h b/include/pybind11/pybind11.h index af8e730d3f..0c25ca1a23 100644 --- a/include/pybind11/pybind11.h +++ b/include/pybind11/pybind11.h @@ -56,6 +56,29 @@ PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE) +PYBIND11_NAMESPACE_BEGIN(detail) + +// Apply all the extensions translators from a list +// Return true if one of the translators completed without raising an exception +// itself. Return of false indicates that if there are other translators +// available, they should be tried. +inline bool apply_exception_translators(std::forward_list& translators) { + auto last_exception = std::current_exception(); + + for (auto &translator : translators) { + try { + translator(last_exception); + return true; + } catch (...) { + last_exception = std::current_exception(); + } + } + return false; +} + +PYBIND11_NAMESPACE_END(detail) + + /// Wraps an arbitrary C++ function/method/lambda function/.. into a callable Python object class cpp_function : public function { public: @@ -550,6 +573,7 @@ class cpp_function : public function { } } + /// Main dispatch logic for calls to functions bound using pybind11 static PyObject *dispatcher(PyObject *self, PyObject *args_in, PyObject *kwargs_in) { using namespace detail; @@ -830,8 +854,12 @@ class cpp_function : public function { #endif } catch (...) { /* When an exception is caught, give each registered exception - translator a chance to translate it to a Python exception - in reverse order of registration. + translator a chance to translate it to a Python exception. First + all module-local translators will be tried in reverse order of + registration. If none of the module-locale translators handle + the exception (or there are no module-locale translators) then + the global translators will be tried, also in reverse order of + registration. A translator may choose to do one of the following: @@ -840,17 +868,15 @@ class cpp_function : public function { - do nothing and let the exception fall through to the next translator, or - delegate translation to the next translator by throwing a new type of exception. */ - auto last_exception = std::current_exception(); - auto ®istered_exception_translators = get_internals().registered_exception_translators; - for (auto& translator : registered_exception_translators) { - try { - translator(last_exception); - } catch (...) { - last_exception = std::current_exception(); - continue; - } + auto &local_exception_translators = get_local_internals().registered_exception_translators; + if (detail::apply_exception_translators(local_exception_translators)) { + return nullptr; + } + auto &exception_translators = get_internals().registered_exception_translators; + if (detail::apply_exception_translators(exception_translators)) { return nullptr; } + PyErr_SetString(PyExc_SystemError, "Exception escaped from default exception translator!"); return nullptr; } @@ -951,6 +977,7 @@ class cpp_function : public function { } }; + /// Wrapper for Python extension modules class module_ : public object { public: @@ -1124,7 +1151,7 @@ class generic_type : public object { auto tindex = std::type_index(*rec.type); tinfo->direct_conversions = &internals.direct_conversions[tindex]; if (rec.module_local) - registered_local_types_cpp()[tindex] = tinfo; + get_local_internals().registered_types_cpp[tindex] = tinfo; else internals.registered_types_cpp[tindex] = tinfo; internals.registered_types_py[(PyTypeObject *) m_ptr] = { tinfo }; @@ -1310,7 +1337,7 @@ class class_ : public detail::generic_type { generic_type::initialize(record); if (has_alias) { - auto &instances = record.module_local ? registered_local_types_cpp() : get_internals().registered_types_cpp; + auto &instances = record.module_local ? get_local_internals().registered_types_cpp : get_internals().registered_types_cpp; instances[std::type_index(typeid(type_alias))] = instances[std::type_index(typeid(type))]; } } @@ -2010,12 +2037,24 @@ template void implicitly_convertible() pybind11_fail("implicitly_convertible: Unable to find type " + type_id()); } -template -void register_exception_translator(ExceptionTranslator&& translator) { + +inline void register_exception_translator(ExceptionTranslator &&translator) { detail::get_internals().registered_exception_translators.push_front( std::forward(translator)); } + +/** + * Add a new module-local exception translator. Locally registered functions + * will be tried before any globally registered exception translators, which + * will only be invoked if the module-local handlers do not deal with + * the exception. + */ +inline void register_local_exception_translator(ExceptionTranslator &&translator) { + detail::get_local_internals().registered_exception_translators.push_front( + std::forward(translator)); +} + /** * Wrapper to generate a new Python exception type. * @@ -2049,22 +2088,20 @@ PYBIND11_NAMESPACE_BEGIN(detail) // directly in register_exception, but that makes clang <3.5 segfault - issue #1349). template exception &get_exception_object() { static exception ex; return ex; } -PYBIND11_NAMESPACE_END(detail) -/** - * Registers a Python exception in `m` of the given `name` and installs an exception translator to - * translate the C++ exception to the created Python exception using the exceptions what() method. - * This is intended for simple exception translations; for more complex translation, register the - * exception object and translator directly. - */ +// Helper function for register_exception and register_local_exception template -exception ®ister_exception(handle scope, - const char *name, - handle base = PyExc_Exception) { +exception ®ister_exception_impl(handle scope, + const char *name, + handle base, + bool isLocal) { auto &ex = detail::get_exception_object(); if (!ex) ex = exception(scope, name, base); - register_exception_translator([](std::exception_ptr p) { + auto register_func = isLocal ? ®ister_local_exception_translator + : ®ister_exception_translator; + + register_func([](std::exception_ptr p) { if (!p) return; try { std::rethrow_exception(p); @@ -2075,6 +2112,36 @@ exception ®ister_exception(handle scope, return ex; } +PYBIND11_NAMESPACE_END(detail) + +/** + * Registers a Python exception in `m` of the given `name` and installs a translator to + * translate the C++ exception to the created Python exception using the what() method. + * This is intended for simple exception translations; for more complex translation, register the + * exception object and translator directly. + */ +template +exception ®ister_exception(handle scope, + const char *name, + handle base = PyExc_Exception) { + return detail::register_exception_impl(scope, name, base, false /* isLocal */); +} + +/** + * Registers a Python exception in `m` of the given `name` and installs a translator to + * translate the C++ exception to the created Python exception using the what() method. + * This translator will only be used for exceptions that are thrown in this module and will be + * tried before global exception translators, including those registered with register_exception. + * This is intended for simple exception translations; for more complex translation, register the + * exception object and translator directly. + */ +template +exception ®ister_local_exception(handle scope, + const char *name, + handle base = PyExc_Exception) { + return detail::register_exception_impl(scope, name, base, true /* isLocal */); +} + PYBIND11_NAMESPACE_BEGIN(detail) PYBIND11_NOINLINE inline void print(const tuple &args, const dict &kwargs) { auto strings = tuple(args.size()); diff --git a/tests/local_bindings.h b/tests/local_bindings.h index 8629ed7784..f11c08e616 100644 --- a/tests/local_bindings.h +++ b/tests/local_bindings.h @@ -35,6 +35,25 @@ using NonLocalVec2 = std::vector; using NonLocalMap = std::unordered_map; using NonLocalMap2 = std::unordered_map; + +// Exception that will be caught via the module local translator. +class LocalException : public std::exception { +public: + explicit LocalException(const char * m) : message{m} {} + const char * what() const noexcept override {return message.c_str();} +private: + std::string message = ""; +}; + +// Exception that will be registered with register_local_exception_translator +class LocalSimpleException : public std::exception { +public: + explicit LocalSimpleException(const char * m) : message{m} {} + const char * what() const noexcept override {return message.c_str();} +private: + std::string message = ""; +}; + PYBIND11_MAKE_OPAQUE(LocalVec); PYBIND11_MAKE_OPAQUE(LocalVec2); PYBIND11_MAKE_OPAQUE(LocalMap); diff --git a/tests/pybind11_cross_module_tests.cpp b/tests/pybind11_cross_module_tests.cpp index e79701c6c6..4bfd5302d5 100644 --- a/tests/pybind11_cross_module_tests.cpp +++ b/tests/pybind11_cross_module_tests.cpp @@ -29,11 +29,14 @@ PYBIND11_MODULE(pybind11_cross_module_tests, m) { bind_local(m, "ExternalType2", py::module_local()); // test_exceptions.py + py::register_local_exception(m, "LocalSimpleException"); m.def("raise_runtime_error", []() { PyErr_SetString(PyExc_RuntimeError, "My runtime error"); throw py::error_already_set(); }); m.def("raise_value_error", []() { PyErr_SetString(PyExc_ValueError, "My value error"); throw py::error_already_set(); }); m.def("throw_pybind_value_error", []() { throw py::value_error("pybind11 value error"); }); m.def("throw_pybind_type_error", []() { throw py::type_error("pybind11 type error"); }); m.def("throw_stop_iteration", []() { throw py::stop_iteration(); }); + m.def("throw_local_error", []() { throw LocalException("just local"); }); + m.def("throw_local_simple_error", []() { throw LocalSimpleException("external mod"); }); py::register_exception_translator([](std::exception_ptr p) { try { if (p) std::rethrow_exception(p); @@ -42,6 +45,17 @@ PYBIND11_MODULE(pybind11_cross_module_tests, m) { } }); + // translate the local exception into a key error but only in this module + py::register_local_exception_translator([](std::exception_ptr p) { + try { + if (p) { + std::rethrow_exception(p); + } + } catch (const LocalException &e) { + PyErr_SetString(PyExc_KeyError, e.what()); + } + }); + // test_local_bindings.py // Local to both: bind_local(m, "LocalType", py::module_local()) @@ -94,7 +108,7 @@ PYBIND11_MODULE(pybind11_cross_module_tests, m) { m.def("get_mixed_lg", [](int i) { return MixedLocalGlobal(i); }); // test_internal_locals_differ - m.def("local_cpp_types_addr", []() { return (uintptr_t) &py::detail::registered_local_types_cpp(); }); + m.def("local_cpp_types_addr", []() { return (uintptr_t) &py::detail::get_local_internals().registered_types_cpp; }); // test_stl_caster_vs_stl_bind py::bind_vector>(m, "VectorInt"); diff --git a/tests/test_exceptions.cpp b/tests/test_exceptions.cpp index f7bacd07e4..e28f0bb798 100644 --- a/tests/test_exceptions.cpp +++ b/tests/test_exceptions.cpp @@ -6,9 +6,10 @@ All rights reserved. Use of this source code is governed by a BSD-style license that can be found in the LICENSE file. */ - #include "test_exceptions.h" +#include "local_bindings.h" + #include "pybind11_tests.h" #include @@ -68,6 +69,17 @@ class MyException5_1 : public MyException5 { using MyException5::MyException5; }; + +// Exception that will be caught via the module local translator. +class MyException6 : public std::exception { +public: + explicit MyException6(const char * m) : message{m} {} + const char * what() const noexcept override {return message.c_str();} +private: + std::string message = ""; +}; + + struct PythonCallInDestructor { PythonCallInDestructor(const py::dict &d) : d(d) {} ~PythonCallInDestructor() { d["good"] = true; } @@ -138,14 +150,29 @@ TEST_SUBMODULE(exceptions, m) { // A slightly more complicated one that declares MyException5_1 as a subclass of MyException5 py::register_exception(m, "MyException5_1", ex5.ptr()); + //py::register_local_exception(m, "LocalSimpleException") + + py::register_local_exception_translator([](std::exception_ptr p) { + try { + if (p) { + std::rethrow_exception(p); + } + } catch (const MyException6 &e) { + PyErr_SetString(PyExc_RuntimeError, e.what()); + } + }); + m.def("throws1", []() { throw MyException("this error should go to a custom type"); }); m.def("throws2", []() { throw MyException2("this error should go to a standard Python exception"); }); m.def("throws3", []() { throw MyException3("this error cannot be translated"); }); m.def("throws4", []() { throw MyException4("this error is rethrown"); }); m.def("throws5", []() { throw MyException5("this is a helper-defined translated exception"); }); m.def("throws5_1", []() { throw MyException5_1("MyException5 subclass"); }); + m.def("throws6", []() { throw MyException6("MyException6 only handled in this module"); }); m.def("throws_logic_error", []() { throw std::logic_error("this error should fall through to the standard handler"); }); - m.def("throws_overflow_error", []() {throw std::overflow_error(""); }); + m.def("throws_overflow_error", []() { throw std::overflow_error(""); }); + m.def("throws_local_error", []() { throw LocalException("never caught"); }); + m.def("throws_local_simple_error", []() { throw LocalSimpleException("this mod"); }); m.def("exception_matches", []() { py::dict foo; try { diff --git a/tests/test_exceptions.py b/tests/test_exceptions.py index b9fc269381..966ae07fc0 100644 --- a/tests/test_exceptions.py +++ b/tests/test_exceptions.py @@ -25,7 +25,7 @@ def test_error_already_set(msg): assert msg(excinfo.value) == "foo" -def test_cross_module_exceptions(): +def test_cross_module_exceptions(msg): with pytest.raises(RuntimeError) as excinfo: cm.raise_runtime_error() assert str(excinfo.value) == "My runtime error" @@ -45,6 +45,15 @@ def test_cross_module_exceptions(): with pytest.raises(StopIteration) as excinfo: cm.throw_stop_iteration() + with pytest.raises(cm.LocalSimpleException) as excinfo: + cm.throw_local_simple_error() + assert msg(excinfo.value) == "external mod" + + with pytest.raises(KeyError) as excinfo: + cm.throw_local_error() + # KeyError is a repr of the key, so it has an extra set of quotes + assert str(excinfo.value) == "'just local'" + # TODO: FIXME @pytest.mark.xfail( @@ -221,3 +230,21 @@ def __repr__(self): with pytest.raises(TypeError): m.simple_bool_passthrough(MyRepr()) + + +def test_local_translator(msg): + """Tests that a local translator works and that the local translator from + the cross module is not applied""" + with pytest.raises(RuntimeError) as excinfo: + m.throws6() + assert msg(excinfo.value) == "MyException6 only handled in this module" + + with pytest.raises(RuntimeError) as excinfo: + m.throws_local_error() + assert not isinstance(excinfo.value, KeyError) + assert msg(excinfo.value) == "never caught" + + with pytest.raises(Exception) as excinfo: + m.throws_local_simple_error() + assert not isinstance(excinfo.value, cm.LocalSimpleException) + assert msg(excinfo.value) == "this mod" diff --git a/tests/test_local_bindings.cpp b/tests/test_local_bindings.cpp index bfbab3ed3b..8d6e33f790 100644 --- a/tests/test_local_bindings.cpp +++ b/tests/test_local_bindings.cpp @@ -78,7 +78,7 @@ TEST_SUBMODULE(local_bindings, m) { m.def("get_mixed_lg", [](int i) { return MixedLocalGlobal(i); }); // test_internal_locals_differ - m.def("local_cpp_types_addr", []() { return (uintptr_t) &py::detail::registered_local_types_cpp(); }); + m.def("local_cpp_types_addr", []() { return (uintptr_t) &py::detail::get_local_internals().registered_types_cpp; }); // test_stl_caster_vs_stl_bind m.def("load_vector_via_caster", [](std::vector v) { From e58c6897cc7b77b92e1c49df82a3504f777b97c9 Mon Sep 17 00:00:00 2001 From: ka-bo <55587089+ka-bo@users.noreply.github.com> Date: Wed, 21 Jul 2021 18:00:57 +0200 Subject: [PATCH 086/638] Specified encoding in setup.py calls of open() (#3137) * Specified encoding in setup.py calls of open() * Fix for Python2 Co-authored-by: Karsten Bock --- setup.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/setup.py b/setup.py index ced3d69581..7aa151c01b 100644 --- a/setup.py +++ b/setup.py @@ -11,6 +11,7 @@ import subprocess import sys import tempfile +import io import setuptools.command.sdist @@ -70,7 +71,7 @@ def build_expected_version_hex(matches): version = loc["__version__"] # Verify that the version matches the one in C++ -with open("include/pybind11/detail/common.h") as f: +with io.open("include/pybind11/detail/common.h", encoding="utf8") as f: matches = dict(VERSION_REGEX.findall(f.read())) cpp_version = "{MAJOR}.{MINOR}.{PATCH}".format(**matches) if version != cpp_version: From ff97f101d96cc780ce348ac6b4b4464982578929 Mon Sep 17 00:00:00 2001 From: "Ralf W. Grosse-Kunstleve" Date: Mon, 26 Jul 2021 11:28:36 -0700 Subject: [PATCH 087/638] Removing MSVC C4996 from pragma block at the top of pybind11.h (#3129) * Removing MSVC C4996 from pragma block at the top of pybind11.h * localtime_thread_safe, PYBIND11_COMPAT_STRDUP * Adding #include (attempt to fix MSVC 2015, 2017 errors). --- include/pybind11/chrono.h | 33 +++++++++++++++++++++++--------- include/pybind11/detail/common.h | 5 +++++ include/pybind11/pybind11.h | 19 ++++++++++++------ 3 files changed, 42 insertions(+), 15 deletions(-) diff --git a/include/pybind11/chrono.h b/include/pybind11/chrono.h index d32b5b0569..c4e81f5720 100644 --- a/include/pybind11/chrono.h +++ b/include/pybind11/chrono.h @@ -11,9 +11,14 @@ #pragma once #include "pybind11.h" + +#include #include #include -#include +#include + +#include + #include // Backport the PyDateTime_DELTA functions from Python3.3 if required @@ -95,6 +100,22 @@ template class duration_caster { PYBIND11_TYPE_CASTER(type, _("datetime.timedelta")); }; +inline std::tm *localtime_thread_safe(const std::time_t *time, std::tm *buf) { +#if defined(__STDC_WANT_LIB_EXT1__) || defined(_MSC_VER) + if (localtime_s(buf, time)) + return nullptr; + return buf; +#else + static std::mutex mtx; + std::lock_guard lock(mtx); + std::tm *tm_ptr = localtime(time); + if (tm_ptr != nullptr) { + *buf = *tm_ptr; + } + return tm_ptr; +#endif +} + // This is for casting times on the system clock into datetime.datetime instances template class type_caster> { public: @@ -162,16 +183,10 @@ template class type_caster(src - us)); - // std::localtime returns a pointer to a static internal std::tm object on success, - // or null pointer otherwise - std::tm *localtime_ptr = std::localtime(&tt); + std::tm localtime; + std::tm *localtime_ptr = localtime_thread_safe(&tt, &localtime); if (!localtime_ptr) throw cast_error("Unable to represent system_clock in local time"); - - // this function uses static memory so it's best to copy it out asap just in case - // otherwise other code that is using localtime may break this (not just python code) - std::tm localtime = *localtime_ptr; - return PyDateTime_FromDateAndTime(localtime.tm_year + 1900, localtime.tm_mon + 1, localtime.tm_mday, diff --git a/include/pybind11/detail/common.h b/include/pybind11/detail/common.h index 52b0193350..3faf3ea327 100644 --- a/include/pybind11/detail/common.h +++ b/include/pybind11/detail/common.h @@ -125,6 +125,11 @@ # endif #endif +// https://en.cppreference.com/w/c/chrono/localtime +#if defined(__STDC_LIB_EXT1__) && !defined(__STDC_WANT_LIB_EXT1__) +# define __STDC_WANT_LIB_EXT1__ +#endif + #include #include #include diff --git a/include/pybind11/pybind11.h b/include/pybind11/pybind11.h index 0c25ca1a23..696e7cd377 100644 --- a/include/pybind11/pybind11.h +++ b/include/pybind11/pybind11.h @@ -19,7 +19,6 @@ # pragma warning(disable: 4100) // warning C4100: Unreferenced formal parameter # pragma warning(disable: 4127) // warning C4127: Conditional expression is constant # pragma warning(disable: 4800) // warning C4800: 'int': forcing value to bool 'true' or 'false' (performance warning) -# pragma warning(disable: 4996) // warning C4996: The POSIX name for this item is deprecated. Instead, use the ISO C and C++ conformant name # pragma warning(disable: 4522) // warning C4522: multiple assignment operators specified # pragma warning(disable: 4505) // warning C4505: 'PySlice_GetIndicesEx': unreferenced local function has been removed (PyPy only) #elif defined(__GNUG__) && !defined(__clang__) @@ -43,6 +42,8 @@ #include #include +#include + #if defined(__cpp_lib_launder) && !(defined(_MSC_VER) && (_MSC_VER < 1914)) # define PYBIND11_STD_LAUNDER std::launder # define PYBIND11_HAS_STD_LAUNDER 1 @@ -76,8 +77,13 @@ inline bool apply_exception_translators(std::forward_list& return false; } -PYBIND11_NAMESPACE_END(detail) +#if defined(_MSC_VER) +# define PYBIND11_COMPAT_STRDUP _strdup +#else +# define PYBIND11_COMPAT_STRDUP strdup +#endif +PYBIND11_NAMESPACE_END(detail) /// Wraps an arbitrary C++ function/method/lambda function/.. into a callable Python object class cpp_function : public function { @@ -276,7 +282,7 @@ class cpp_function : public function { std::free(s); } char *operator()(const char *s) { - auto t = strdup(s); + auto t = PYBIND11_COMPAT_STRDUP(s); strings.push_back(t); return t; } @@ -520,7 +526,8 @@ class cpp_function : public function { auto *func = (PyCFunctionObject *) m_ptr; std::free(const_cast(func->m_ml->ml_doc)); // Install docstring if it's non-empty (when at least one option is enabled) - func->m_ml->ml_doc = signatures.empty() ? nullptr : strdup(signatures.c_str()); + func->m_ml->ml_doc + = signatures.empty() ? nullptr : PYBIND11_COMPAT_STRDUP(signatures.c_str()); if (rec->is_method) { m_ptr = PYBIND11_INSTANCE_METHOD_NEW(m_ptr, rec->scope.ptr()); @@ -1525,7 +1532,7 @@ class class_ : public detail::generic_type { detail::process_attributes::init(extra..., rec_fget); if (rec_fget->doc && rec_fget->doc != doc_prev) { free(doc_prev); - rec_fget->doc = strdup(rec_fget->doc); + rec_fget->doc = PYBIND11_COMPAT_STRDUP(rec_fget->doc); } } if (rec_fset) { @@ -1533,7 +1540,7 @@ class class_ : public detail::generic_type { detail::process_attributes::init(extra..., rec_fset); if (rec_fset->doc && rec_fset->doc != doc_prev) { free(doc_prev); - rec_fset->doc = strdup(rec_fset->doc); + rec_fset->doc = PYBIND11_COMPAT_STRDUP(rec_fset->doc); } if (! rec_active) rec_active = rec_fset; } From 7904ba1a5c600ea8455b16c1010a1d46181d3148 Mon Sep 17 00:00:00 2001 From: "Ralf W. Grosse-Kunstleve" Date: Mon, 26 Jul 2021 12:02:50 -0700 Subject: [PATCH 088/638] Adding pragma warning(disable: 4522) for MSVC <= 2017. (#3142) --- include/pybind11/pybind11.h | 1 - include/pybind11/pytypes.h | 7 +++++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/include/pybind11/pybind11.h b/include/pybind11/pybind11.h index 696e7cd377..9c0b723839 100644 --- a/include/pybind11/pybind11.h +++ b/include/pybind11/pybind11.h @@ -19,7 +19,6 @@ # pragma warning(disable: 4100) // warning C4100: Unreferenced formal parameter # pragma warning(disable: 4127) // warning C4127: Conditional expression is constant # pragma warning(disable: 4800) // warning C4800: 'int': forcing value to bool 'true' or 'false' (performance warning) -# pragma warning(disable: 4522) // warning C4522: multiple assignment operators specified # pragma warning(disable: 4505) // warning C4505: 'PySlice_GetIndicesEx': unreferenced local function has been removed (PyPy only) #elif defined(__GNUG__) && !defined(__clang__) # pragma GCC diagnostic push diff --git a/include/pybind11/pytypes.h b/include/pybind11/pytypes.h index b483fb323c..3329982777 100644 --- a/include/pybind11/pytypes.h +++ b/include/pybind11/pytypes.h @@ -532,6 +532,10 @@ object object_or_cast(T &&o); // Match a PyObject*, which we want to convert directly to handle via its converting constructor inline handle object_or_cast(PyObject *ptr) { return ptr; } +#if defined(_MSC_VER) && _MSC_VER < 1920 +# pragma warning(push) +# pragma warning(disable: 4522) // warning C4522: multiple assignment operators specified +#endif template class accessor : public object_api> { using key_type = typename Policy::key_type; @@ -580,6 +584,9 @@ class accessor : public object_api> { key_type key; mutable object cache; }; +#if defined(_MSC_VER) && _MSC_VER < 1920 +# pragma warning(pop) +#endif PYBIND11_NAMESPACE_BEGIN(accessor_policies) struct obj_attr { From 7f76d79508475ef2e8f17941e350b1dfa8d828b4 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 26 Jul 2021 15:05:58 -0400 Subject: [PATCH 089/638] [pre-commit.ci] pre-commit autoupdate (#3143) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit updates: - [github.com/asottile/pyupgrade: v2.21.2 → v2.23.0](https://github.com/asottile/pyupgrade/compare/v2.21.2...v2.23.0) Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> --- .pre-commit-config.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index aa7b4774e3..510b841260 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -31,7 +31,7 @@ repos: exclude: ^noxfile.py$ - repo: https://github.com/asottile/pyupgrade - rev: v2.21.2 + rev: v2.23.0 hooks: - id: pyupgrade From a0f862d428acd11dacb344ce9690e420d98cde53 Mon Sep 17 00:00:00 2001 From: "Ralf W. Grosse-Kunstleve" Date: Mon, 26 Jul 2021 13:26:36 -0700 Subject: [PATCH 090/638] Removing MSVC C4800 from pragma block at the top of pybind11.h (#3141) * Adding PYBIND11_COMPAT_BOOL_CAST to appease MSVC 2015 warning C4800. * Replacing PYBIND11_COMPAT_BOOL_CAST with simpler != 0 * Extra parentheses (almost all compilers failed without these). --- include/pybind11/buffer_info.h | 2 +- include/pybind11/cast.h | 2 +- include/pybind11/detail/type_caster_base.h | 4 ++-- include/pybind11/pybind11.h | 1 - include/pybind11/pytypes.h | 6 ++++-- 5 files changed, 8 insertions(+), 7 deletions(-) diff --git a/include/pybind11/buffer_info.h b/include/pybind11/buffer_info.h index 47dc39d4ea..eba68d1aa1 100644 --- a/include/pybind11/buffer_info.h +++ b/include/pybind11/buffer_info.h @@ -83,7 +83,7 @@ struct buffer_info { view->strides ? std::vector(view->strides, view->strides + view->ndim) : detail::c_strides({view->shape, view->shape + view->ndim}, view->itemsize), - view->readonly) { + (view->readonly != 0)) { this->m_view = view; this->ownview = ownview; } diff --git a/include/pybind11/cast.h b/include/pybind11/cast.h index a748c77c0f..1da432358f 100644 --- a/include/pybind11/cast.h +++ b/include/pybind11/cast.h @@ -322,7 +322,7 @@ template <> class type_caster { } #endif if (res == 0 || res == 1) { - value = (bool) res; + value = (res != 0); return true; } PyErr_Clear(); diff --git a/include/pybind11/detail/type_caster_base.h b/include/pybind11/detail/type_caster_base.h index bdf3a39822..a6b6bea6a3 100644 --- a/include/pybind11/detail/type_caster_base.h +++ b/include/pybind11/detail/type_caster_base.h @@ -231,7 +231,7 @@ struct value_and_holder { return reinterpret_cast(vh[0]); } // True if this `value_and_holder` has a non-null value pointer - explicit operator bool() const { return value_ptr(); } + explicit operator bool() const { return value_ptr() != nullptr; } template H &holder() const { return reinterpret_cast(vh[1]); @@ -252,7 +252,7 @@ struct value_and_holder { bool instance_registered() const { return inst->simple_layout ? inst->simple_instance_registered - : inst->nonsimple.status[index] & instance::status_instance_registered; + : ((inst->nonsimple.status[index] & instance::status_instance_registered) != 0); } void set_instance_registered(bool v = true) const { if (inst->simple_layout) diff --git a/include/pybind11/pybind11.h b/include/pybind11/pybind11.h index 9c0b723839..93b920d852 100644 --- a/include/pybind11/pybind11.h +++ b/include/pybind11/pybind11.h @@ -18,7 +18,6 @@ # pragma warning(push) # pragma warning(disable: 4100) // warning C4100: Unreferenced formal parameter # pragma warning(disable: 4127) // warning C4127: Conditional expression is constant -# pragma warning(disable: 4800) // warning C4800: 'int': forcing value to bool 'true' or 'false' (performance warning) # pragma warning(disable: 4505) // warning C4505: 'PySlice_GetIndicesEx': unreferenced local function has been removed (PyPy only) #elif defined(__GNUG__) && !defined(__clang__) # pragma GCC diagnostic push diff --git a/include/pybind11/pytypes.h b/include/pybind11/pytypes.h index 3329982777..cea4e7eb01 100644 --- a/include/pybind11/pytypes.h +++ b/include/pybind11/pytypes.h @@ -367,7 +367,9 @@ class PYBIND11_EXPORT error_already_set : public std::runtime_error { /// Check if the currently trapped error type matches the given Python exception class (or a /// subclass thereof). May also be passed a tuple to search for any exception class matches in /// the given tuple. - bool matches(handle exc) const { return PyErr_GivenExceptionMatches(m_type.ptr(), exc.ptr()); } + bool matches(handle exc) const { + return (PyErr_GivenExceptionMatches(m_type.ptr(), exc.ptr()) != 0); + } const object& type() const { return m_type; } const object& value() const { return m_value; } @@ -853,7 +855,7 @@ PYBIND11_NAMESPACE_END(detail) Name(handle h, borrowed_t) : Parent(h, borrowed_t{}) { } \ Name(handle h, stolen_t) : Parent(h, stolen_t{}) { } \ PYBIND11_DEPRECATED("Use py::isinstance(obj) instead") \ - bool check() const { return m_ptr != nullptr && (bool) CheckFun(m_ptr); } \ + bool check() const { return m_ptr != nullptr && (CheckFun(m_ptr) != 0); } \ static bool check_(handle h) { return h.ptr() != nullptr && CheckFun(h.ptr()); } \ template \ Name(const ::pybind11::detail::accessor &a) : Name(object(a)) { } From a0b975965fe5df66f20a1d29193055f8538ac94e Mon Sep 17 00:00:00 2001 From: David Hewitt <1939362+davidhewitt@users.noreply.github.com> Date: Tue, 27 Jul 2021 19:16:28 +0100 Subject: [PATCH 091/638] Allow python builtins to be used as callbacks (#1413) * Allow python builtins to be used as callbacks * Try to fix pypy segfault * Add expected fail for PyPy * Fix typo * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Add more info to xfail * Add env * Try returning false * Try removing the move for pypy * Fix bugs * Try removing move * Just keep ignoring for PyPy * Add back xfail * Fix ctors * Revert change of std::move * Change to skip * Fix bug and edit comments * Remove clang-tidy bugprone fix skip bug Co-authored-by: Aaron Gokaslan Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> --- include/pybind11/functional.h | 36 ++++++++++++++++++++--------------- tests/test_callbacks.cpp | 6 ++++++ tests/test_callbacks.py | 11 +++++++++++ 3 files changed, 38 insertions(+), 15 deletions(-) diff --git a/include/pybind11/functional.h b/include/pybind11/functional.h index cc7099c6a3..b9bf38919c 100644 --- a/include/pybind11/functional.h +++ b/include/pybind11/functional.h @@ -43,27 +43,33 @@ struct type_caster> { captured variables), in which case the roundtrip can be avoided. */ if (auto cfunc = func.cpp_function()) { - auto c = reinterpret_borrow(PyCFunction_GET_SELF(cfunc.ptr())); - auto rec = (function_record *) c; - - while (rec != nullptr) { - if (rec->is_stateless - && same_type(typeid(function_type), - *reinterpret_cast(rec->data[1]))) { - struct capture { - function_type f; - }; - value = ((capture *) &rec->data)->f; - return true; + auto cfunc_self = PyCFunction_GET_SELF(cfunc.ptr()); + if (isinstance(cfunc_self)) { + auto c = reinterpret_borrow(cfunc_self); + auto rec = (function_record *) c; + + while (rec != nullptr) { + if (rec->is_stateless + && same_type(typeid(function_type), + *reinterpret_cast(rec->data[1]))) { + struct capture { + function_type f; + }; + value = ((capture *) &rec->data)->f; + return true; + } + rec = rec->next; } - rec = rec->next; } + // PYPY segfaults here when passing builtin function like sum. + // Raising an fail exception here works to prevent the segfault, but only on gcc. + // See PR #1413 for full details } // ensure GIL is held during functor destruction struct func_handle { function f; - func_handle(function&& f_) : f(std::move(f_)) {} + func_handle(function &&f_) noexcept : f(std::move(f_)) {} func_handle(const func_handle& f_) { gil_scoped_acquire acq; f = f_.f; @@ -77,7 +83,7 @@ struct type_caster> { // to emulate 'move initialization capture' in C++11 struct func_wrapper { func_handle hfunc; - func_wrapper(func_handle&& hf): hfunc(std::move(hf)) {} + func_wrapper(func_handle &&hf) noexcept : hfunc(std::move(hf)) {} Return operator()(Args... args) const { gil_scoped_acquire acq; object retval(hfunc.f(std::forward(args)...)); diff --git a/tests/test_callbacks.cpp b/tests/test_callbacks.cpp index a208b44d04..a50771038b 100644 --- a/tests/test_callbacks.cpp +++ b/tests/test_callbacks.cpp @@ -156,6 +156,12 @@ TEST_SUBMODULE(callbacks, m) { .def(py::init<>()) .def("triple", [](CppBoundMethodTest &, int val) { return 3 * val; }); + // This checks that builtin functions can be passed as callbacks + // rather than throwing RuntimeError due to trying to extract as capsule + m.def("test_sum_builtin", [](const std::function &sum_builtin, const py::iterable &i) { + return sum_builtin(i); + }); + // test async Python callbacks using callback_f = std::function; m.def("test_async_callback", [](const callback_f &f, const py::list &work) { diff --git a/tests/test_callbacks.py b/tests/test_callbacks.py index 397bf63d24..5bc4d1773d 100644 --- a/tests/test_callbacks.py +++ b/tests/test_callbacks.py @@ -3,6 +3,7 @@ from pybind11_tests import callbacks as m from threading import Thread import time +import env # NOQA: F401 def test_callbacks(): @@ -124,6 +125,16 @@ def test_movable_object(): assert m.callback_with_movable(lambda _: None) is True +@pytest.mark.skipif( + "env.PYPY", + reason="PyPy segfaults on here. See discussion on #1413.", +) +def test_python_builtins(): + """Test if python builtins like sum() can be used as callbacks""" + assert m.test_sum_builtin(sum, [1, 2, 3]) == 6 + assert m.test_sum_builtin(sum, []) == 0 + + def test_async_callbacks(): # serves as state for async callback class Item: From 7cc0ebb4753770ace452d118bb327cdd9e76dcbb Mon Sep 17 00:00:00 2001 From: Henry Schreiner Date: Tue, 27 Jul 2021 14:23:52 -0700 Subject: [PATCH 092/638] fix: the CMake config in Python package had a hard coded path (#3144) --- CMakeLists.txt | 6 ++++++ tests/extra_python_package/test_files.py | 14 ++++++++++++-- tools/pybind11Config.cmake.in | 3 ++- 3 files changed, 20 insertions(+), 3 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index c988ea0b50..b04311fd85 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -203,6 +203,12 @@ if(PYBIND11_INSTALL) "${CMAKE_INSTALL_DATAROOTDIR}/cmake/${PROJECT_NAME}" CACHE STRING "install path for pybind11Config.cmake") + if(IS_ABSOLUTE "${CMAKE_INSTALL_INCLUDEDIR}") + set(pybind11_INCLUDEDIR "${CMAKE_INSTALL_FULL_INCLUDEDIR}") + else() + set(pybind11_INCLUDEDIR "\$\{PACKAGE_PREFIX_DIR\}/${CMAKE_INSTALL_INCLUDEDIR}") + endif() + configure_package_config_file( tools/${PROJECT_NAME}Config.cmake.in "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}Config.cmake" INSTALL_DESTINATION ${PYBIND11_CMAKECONFIG_INSTALL_DIR}) diff --git a/tests/extra_python_package/test_files.py b/tests/extra_python_package/test_files.py index 43e93c2633..337a72dfea 100644 --- a/tests/extra_python_package/test_files.py +++ b/tests/extra_python_package/test_files.py @@ -138,6 +138,16 @@ def test_build_sdist(monkeypatch, tmpdir): ) as f: pyproject_toml = f.read() + with contextlib.closing( + tar.extractfile( + tar.getmember( + start + "pybind11/share/cmake/pybind11/pybind11Config.cmake" + ) + ) + ) as f: + contents = f.read().decode("utf8") + assert 'set(pybind11_INCLUDE_DIR "${PACKAGE_PREFIX_DIR}/include")' in contents + files = {"pybind11/{}".format(n) for n in all_files} files |= sdist_files files |= {"pybind11{}".format(n) for n in local_sdist_files} @@ -151,11 +161,11 @@ def test_build_sdist(monkeypatch, tmpdir): .substitute(version=version, extra_cmd="") .encode() ) - assert setup_py == contents + assert setup_py == contents with open(os.path.join(MAIN_DIR, "tools", "pyproject.toml"), "rb") as f: contents = f.read() - assert pyproject_toml == contents + assert pyproject_toml == contents def test_build_global_dist(monkeypatch, tmpdir): diff --git a/tools/pybind11Config.cmake.in b/tools/pybind11Config.cmake.in index 6fa03a0f24..73ec104a0e 100644 --- a/tools/pybind11Config.cmake.in +++ b/tools/pybind11Config.cmake.in @@ -201,7 +201,8 @@ Using ``find_package`` with version info is not recommended except for release v @PACKAGE_INIT@ # Location of pybind11/pybind11.h -set(pybind11_INCLUDE_DIR "@CMAKE_INSTALL_FULL_INCLUDEDIR@") +# This will be relative unless explicitly set as absolute +set(pybind11_INCLUDE_DIR "@pybind11_INCLUDEDIR@") set(pybind11_LIBRARY "") set(pybind11_DEFINITIONS USING_pybind11) From 9beaa925db49cf4751642a1529d34bef4a3682ce Mon Sep 17 00:00:00 2001 From: Aaron Gokaslan Date: Tue, 27 Jul 2021 18:32:26 -0400 Subject: [PATCH 093/638] maint(clang-tidy): Improve code readability with explicit boolean casts (#3148) * maint(clang-tidy) Improve code readability * Fix minor typos * Revert optimization that removed test case * Fix comment formatting * Revert another optimization to repro an issue * Remove make_unique since it C++14 and newer only * eformat comments * Fix unsignedness of comparison * Update comment --- .clang-tidy | 13 +++++++++++++ include/pybind11/cast.h | 16 ++++++++++------ include/pybind11/detail/class.h | 7 ++++--- include/pybind11/detail/internals.h | 2 +- include/pybind11/detail/type_caster_base.h | 4 ++-- include/pybind11/embed.h | 4 ++-- include/pybind11/numpy.h | 19 +++++++++++-------- include/pybind11/pybind11.h | 15 +++++++++------ include/pybind11/pytypes.h | 12 ++++++++---- include/pybind11/stl/filesystem.h | 4 ++-- tests/test_embed/test_interpreter.cpp | 2 +- tests/test_methods_and_attributes.cpp | 1 + tests/test_numpy_dtypes.cpp | 6 ++++-- tests/test_numpy_vectorize.cpp | 2 +- tests/test_smart_ptr.cpp | 4 ++-- tests/test_stl.cpp | 6 ++---- 16 files changed, 73 insertions(+), 44 deletions(-) diff --git a/.clang-tidy b/.clang-tidy index 72304528a5..db5077c227 100644 --- a/.clang-tidy +++ b/.clang-tidy @@ -5,13 +5,17 @@ cppcoreguidelines-init-variables, clang-analyzer-optin.cplusplus.VirtualCall, llvm-namespace-comment, misc-misplaced-const, +misc-non-copyable-objects, misc-static-assert, misc-throw-by-value-catch-by-reference, misc-uniqueptr-reset-release, +misc-unused-parameters, modernize-avoid-bind, +modernize-make-shared, modernize-redundant-void-arg, modernize-replace-auto-ptr, modernize-replace-disallow-copy-and-assign-macro, +modernize-replace-random-shuffle, modernize-shrink-to-fit, modernize-use-auto, modernize-use-bool-literals, @@ -23,13 +27,20 @@ modernize-use-emplace, modernize-use-override, modernize-use-using, *performance*, +readability-avoid-const-params-in-decls, readability-container-size-empty, readability-else-after-return, +readability-delete-null-pointer, +readability-implicit-bool-conversion, readability-make-member-function-const, +readability-misplaced-array-index, +readability-non-const-parameter, readability-redundant-function-ptr-dereference, readability-redundant-smartptr-get, readability-redundant-string-cstr, readability-simplify-subscript-expr, +readability-static-accessed-through-instance, +readability-static-definition-in-anonymous-namespace, readability-string-compare, readability-uniqueptr-delete-release, ' @@ -39,6 +50,8 @@ CheckOptions: value: true - key: performance-unnecessary-value-param.AllowedTypes value: 'exception_ptr$;' +- key: readability-implicit-bool-conversion.AllowPointerConditions + value: true HeaderFilterRegex: 'pybind11/.*h' diff --git a/include/pybind11/cast.h b/include/pybind11/cast.h index 1da432358f..898047b304 100644 --- a/include/pybind11/cast.h +++ b/include/pybind11/cast.h @@ -181,7 +181,7 @@ struct type_caster::value && !is_std_char_t // Signed/unsigned checks happen elsewhere if (py_err || (std::is_integral::value && sizeof(py_type) != sizeof(T) && py_value != (py_type) (T) py_value)) { PyErr_Clear(); - if (py_err && convert && PyNumber_Check(src.ptr())) { + if (py_err && convert && (PyNumber_Check(src.ptr()) != 0)) { auto tmp = reinterpret_steal(std::is_floating_point::value ? PyNumber_Float(src.ptr()) : PyNumber_Long(src.ptr())); @@ -300,7 +300,7 @@ template <> class type_caster { value = false; return true; } - if (convert || !std::strcmp("numpy.bool_", Py_TYPE(src.ptr())->tp_name)) { + if (convert || (std::strcmp("numpy.bool_", Py_TYPE(src.ptr())->tp_name) == 0)) { // (allow non-implicit conversion for numpy booleans) Py_ssize_t res = -1; @@ -501,10 +501,14 @@ template struct type_caster 1 && str_len <= 4) { auto v0 = static_cast(value[0]); - size_t char0_bytes = !(v0 & 0x80) ? 1 : // low bits only: 0-127 - (v0 & 0xE0) == 0xC0 ? 2 : // 0b110xxxxx - start of 2-byte sequence - (v0 & 0xF0) == 0xE0 ? 3 : // 0b1110xxxx - start of 3-byte sequence - 4; // 0b11110xxx - start of 4-byte sequence + // low bits only: 0-127 + // 0b110xxxxx - start of 2-byte sequence + // 0b1110xxxx - start of 3-byte sequence + // 0b11110xxx - start of 4-byte sequence + size_t char0_bytes = (v0 & 0x80) == 0 ? 1 + : (v0 & 0xE0) == 0xC0 ? 2 + : (v0 & 0xF0) == 0xE0 ? 3 + : 4; if (char0_bytes == str_len) { // If we have a 128-255 value, we can decode it into a single char: diff --git a/include/pybind11/detail/class.h b/include/pybind11/detail/class.h index 15e09165de..f9822c7b35 100644 --- a/include/pybind11/detail/class.h +++ b/include/pybind11/detail/class.h @@ -129,8 +129,9 @@ extern "C" inline int pybind11_meta_setattro(PyObject* obj, PyObject* name, PyOb // 2. `Type.static_prop = other_static_prop` --> setattro: replace existing `static_prop` // 3. `Type.regular_attribute = value` --> setattro: regular attribute assignment const auto static_prop = (PyObject *) get_internals().static_property_type; - const auto call_descr_set = descr && value && PyObject_IsInstance(descr, static_prop) - && !PyObject_IsInstance(value, static_prop); + const auto call_descr_set = (descr != nullptr) && (value != nullptr) + && (PyObject_IsInstance(descr, static_prop) != 0) + && (PyObject_IsInstance(value, static_prop) == 0); if (call_descr_set) { // Call `static_property.__set__()` instead of replacing the `static_property`. #if !defined(PYPY_VERSION) @@ -562,7 +563,7 @@ extern "C" inline int pybind11_getbuffer(PyObject *obj, Py_buffer *view, int fla view->len = view->itemsize; for (auto s : info->shape) view->len *= s; - view->readonly = info->readonly; + view->readonly = static_cast(info->readonly); if ((flags & PyBUF_FORMAT) == PyBUF_FORMAT) view->format = const_cast(info->format.c_str()); if ((flags & PyBUF_STRIDES) == PyBUF_STRIDES) { diff --git a/include/pybind11/detail/internals.h b/include/pybind11/detail/internals.h index a621aed2a6..a93a83e268 100644 --- a/include/pybind11/detail/internals.h +++ b/include/pybind11/detail/internals.h @@ -297,7 +297,7 @@ PYBIND11_NOINLINE inline internals &get_internals() { PyThreadState *tstate = PyThreadState_Get(); #if PY_VERSION_HEX >= 0x03070000 internals_ptr->tstate = PyThread_tss_alloc(); - if (!internals_ptr->tstate || PyThread_tss_create(internals_ptr->tstate)) + if (!internals_ptr->tstate || (PyThread_tss_create(internals_ptr->tstate) != 0)) pybind11_fail("get_internals: could not successfully initialize the TSS key!"); PyThread_tss_set(internals_ptr->tstate, tstate); #else diff --git a/include/pybind11/detail/type_caster_base.h b/include/pybind11/detail/type_caster_base.h index a6b6bea6a3..2a675418a7 100644 --- a/include/pybind11/detail/type_caster_base.h +++ b/include/pybind11/detail/type_caster_base.h @@ -238,8 +238,8 @@ struct value_and_holder { } bool holder_constructed() const { return inst->simple_layout - ? inst->simple_holder_constructed - : inst->nonsimple.status[index] & instance::status_holder_constructed; + ? inst->simple_holder_constructed + : (inst->nonsimple.status[index] & instance::status_holder_constructed) != 0u; } void set_holder_constructed(bool v = true) const { if (inst->simple_layout) diff --git a/include/pybind11/embed.h b/include/pybind11/embed.h index 204aaf989f..abb1cd3cca 100644 --- a/include/pybind11/embed.h +++ b/include/pybind11/embed.h @@ -76,7 +76,7 @@ struct embedded_module { using init_t = void (*)(); #endif embedded_module(const char *name, init_t init) { - if (Py_IsInitialized()) + if (Py_IsInitialized() != 0) pybind11_fail("Can't add new modules after the interpreter has been initialized"); auto result = PyImport_AppendInittab(name, init); @@ -101,7 +101,7 @@ PYBIND11_NAMESPACE_END(detail) .. _Python documentation: https://docs.python.org/3/c-api/init.html#c.Py_InitializeEx \endrst */ inline void initialize_interpreter(bool init_signal_handlers = true) { - if (Py_IsInitialized()) + if (Py_IsInitialized() != 0) pybind11_fail("The interpreter is already running"); Py_InitializeEx(init_signal_handlers ? 1 : 0); diff --git a/include/pybind11/numpy.h b/include/pybind11/numpy.h index edba8bac92..7313897fe1 100644 --- a/include/pybind11/numpy.h +++ b/include/pybind11/numpy.h @@ -465,7 +465,9 @@ class dtype : public object { explicit dtype(const buffer_info &info) { dtype descr(_dtype_from_pep3118()(PYBIND11_STR_TYPE(info.format))); // If info.itemsize == 0, use the value calculated from the format string - m_ptr = descr.strip_padding(info.itemsize ? info.itemsize : descr.itemsize()).release().ptr(); + m_ptr = descr.strip_padding(info.itemsize != 0 ? info.itemsize : descr.itemsize()) + .release() + .ptr(); } explicit dtype(const std::string &format) { @@ -486,7 +488,7 @@ class dtype : public object { /// This is essentially the same as calling numpy.dtype(args) in Python. static dtype from_args(object args) { PyObject *ptr = nullptr; - if (!detail::npy_api::get().PyArray_DescrConverter_(args.ptr(), &ptr) || !ptr) + if ((detail::npy_api::get().PyArray_DescrConverter_(args.ptr(), &ptr) == 0) || !ptr) throw error_already_set(); return reinterpret_steal(ptr); } @@ -542,7 +544,7 @@ class dtype : public object { auto name = spec[0].cast(); auto format = spec[1].cast()[0].cast(); auto offset = spec[1].cast()[1].cast(); - if (!len(name) && format.kind() == 'V') + if ((len(name) == 0u) && format.kind() == 'V') continue; field_descriptors.push_back({(PYBIND11_STR_TYPE) name, format.strip_padding(format.itemsize()), offset}); } @@ -872,11 +874,12 @@ template class array_t : public : array(std::move(shape), std::move(strides), ptr, base) { } explicit array_t(ShapeContainer shape, const T *ptr = nullptr, handle base = handle()) - : array_t(private_ctor{}, std::move(shape), - ExtraFlags & f_style - ? detail::f_strides(*shape, itemsize()) - : detail::c_strides(*shape, itemsize()), - ptr, base) { } + : array_t(private_ctor{}, + std::move(shape), + (ExtraFlags & f_style) != 0 ? detail::f_strides(*shape, itemsize()) + : detail::c_strides(*shape, itemsize()), + ptr, + base) {} explicit array_t(ssize_t count, const T *ptr = nullptr, handle base = handle()) : array({count}, {}, ptr, base) { } diff --git a/include/pybind11/pybind11.h b/include/pybind11/pybind11.h index 93b920d852..e2cbb54493 100644 --- a/include/pybind11/pybind11.h +++ b/include/pybind11/pybind11.h @@ -318,7 +318,8 @@ class cpp_function : public function { a.descr = guarded_strdup(repr(a.value).cast().c_str()); } - rec->is_constructor = !strcmp(rec->name, "__init__") || !strcmp(rec->name, "__setstate__"); + rec->is_constructor + = (strcmp(rec->name, "__init__") == 0) || (strcmp(rec->name, "__setstate__") == 0); #if !defined(NDEBUG) && !defined(PYBIND11_DISABLE_NEW_STYLE_INIT_WARNING) if (rec->is_constructor && !rec->is_new_style_constructor) { @@ -1131,7 +1132,8 @@ class generic_type : public object { pybind11_fail("generic_type: cannot initialize type \"" + std::string(rec.name) + "\": an object with that name is already defined"); - if (rec.module_local ? get_local_type_info(*rec.type) : get_global_type_info(*rec.type)) + if ((rec.module_local ? get_local_type_info(*rec.type) : get_global_type_info(*rec.type)) + != nullptr) pybind11_fail("generic_type: type \"" + std::string(rec.name) + "\" is already registered!"); @@ -1209,8 +1211,9 @@ class generic_type : public object { void def_property_static_impl(const char *name, handle fget, handle fset, detail::function_record *rec_func) { - const auto is_static = rec_func && !(rec_func->is_method && rec_func->scope); - const auto has_doc = rec_func && rec_func->doc && pybind11::options::show_user_defined_docstrings(); + const auto is_static = (rec_func != nullptr) && !(rec_func->is_method && rec_func->scope); + const auto has_doc = (rec_func != nullptr) && (rec_func->doc != nullptr) + && pybind11::options::show_user_defined_docstrings(); auto property = handle((PyObject *) (is_static ? get_internals().static_property_type : &PyProperty_Type)); attr(name) = property(fget.ptr() ? fget : none(), @@ -2220,8 +2223,8 @@ inline function get_type_override(const void *this_ptr, const type_info *this_ty Unfortunately this doesn't work on PyPy. */ #if !defined(PYPY_VERSION) PyFrameObject *frame = PyThreadState_Get()->frame; - if (frame && (std::string) str(frame->f_code->co_name) == name && - frame->f_code->co_argcount > 0) { + if (frame != nullptr && (std::string) str(frame->f_code->co_name) == name + && frame->f_code->co_argcount > 0) { PyFrame_FastToLocals(frame); PyObject *self_caller = dict_getitem( frame->f_locals, PyTuple_GET_ITEM(frame->f_code->co_varnames, 0)); diff --git a/include/pybind11/pytypes.h b/include/pybind11/pytypes.h index cea4e7eb01..161aed06f4 100644 --- a/include/pybind11/pytypes.h +++ b/include/pybind11/pytypes.h @@ -773,7 +773,11 @@ class dict_readonly { dict_readonly(handle obj, ssize_t pos) : obj(obj), pos(pos) { increment(); } reference dereference() const { return {key, value}; } - void increment() { if (!PyDict_Next(obj.ptr(), &pos, &key, &value)) { pos = -1; } } + void increment() { + if (PyDict_Next(obj.ptr(), &pos, &key, &value) == 0) { + pos = -1; + } + } bool equal(const dict_readonly &b) const { return pos == b.pos; } private: @@ -1169,14 +1173,14 @@ class bool_ : public object { bool_() : object(Py_False, borrowed_t{}) { } // Allow implicit conversion from and to `bool`: bool_(bool value) : object(value ? Py_True : Py_False, borrowed_t{}) { } - operator bool() const { return m_ptr && PyLong_AsLong(m_ptr) != 0; } + operator bool() const { return (m_ptr != nullptr) && PyLong_AsLong(m_ptr) != 0; } private: /// Return the truth value of an object -- always returns a new reference static PyObject *raw_bool(PyObject *op) { const auto value = PyObject_IsTrue(op); if (value == -1) return nullptr; - return handle(value ? Py_True : Py_False).inc_ref().ptr(); + return handle(value != 0 ? Py_True : Py_False).inc_ref().ptr(); } }; @@ -1607,7 +1611,7 @@ inline memoryview memoryview::from_buffer( size_t ndim = shape->size(); if (ndim != strides->size()) pybind11_fail("memoryview: shape length doesn't match strides length"); - ssize_t size = ndim ? 1 : 0; + ssize_t size = ndim != 0u ? 1 : 0; for (size_t i = 0; i < ndim; ++i) size *= (*shape)[i]; Py_buffer view; diff --git a/include/pybind11/stl/filesystem.h b/include/pybind11/stl/filesystem.h index 7a8acdb60b..431b94b4f7 100644 --- a/include/pybind11/stl/filesystem.h +++ b/include/pybind11/stl/filesystem.h @@ -67,7 +67,7 @@ template struct path_caster { } PyObject* native = nullptr; if constexpr (std::is_same_v) { - if (PyUnicode_FSConverter(buf, &native)) { + if (PyUnicode_FSConverter(buf, &native) != 0) { if (auto c_str = PyBytes_AsString(native)) { // AsString returns a pointer to the internal buffer, which // must not be free'd. @@ -75,7 +75,7 @@ template struct path_caster { } } } else if constexpr (std::is_same_v) { - if (PyUnicode_FSDecoder(buf, &native)) { + if (PyUnicode_FSDecoder(buf, &native) != 0) { if (auto c_str = PyUnicode_AsWideCharString(native, nullptr)) { // AsWideCharString returns a new string that must be free'd. value = c_str; // Copies the string. diff --git a/tests/test_embed/test_interpreter.cpp b/tests/test_embed/test_interpreter.cpp index 6925ee9782..cd50e952f6 100644 --- a/tests/test_embed/test_interpreter.cpp +++ b/tests/test_embed/test_interpreter.cpp @@ -103,7 +103,7 @@ bool has_pybind11_internals_builtin() { bool has_pybind11_internals_static() { auto **&ipp = py::detail::get_internals_pp(); - return ipp && *ipp; + return (ipp != nullptr) && (*ipp != nullptr); } TEST_CASE("Restart the interpreter") { diff --git a/tests/test_methods_and_attributes.cpp b/tests/test_methods_and_attributes.cpp index 9db31a0b82..4cf6f08b85 100644 --- a/tests/test_methods_and_attributes.cpp +++ b/tests/test_methods_and_attributes.cpp @@ -43,6 +43,7 @@ class ExampleMandA { void add6(int other) { value += other; } // passing by value void add7(int &other) { value += other; } // passing by reference void add8(const int &other) { value += other; } // passing by const reference + // NOLINTNEXTLINE(readability-non-const-parameter) Deliberately non-const for testing void add9(int *other) { value += *other; } // passing by pointer void add10(const int *other) { value += *other; } // passing by const pointer diff --git a/tests/test_numpy_dtypes.cpp b/tests/test_numpy_dtypes.cpp index edf9cf200f..340c972db2 100644 --- a/tests/test_numpy_dtypes.cpp +++ b/tests/test_numpy_dtypes.cpp @@ -108,9 +108,11 @@ PYBIND11_PACKED(struct EnumStruct { std::ostream& operator<<(std::ostream& os, const StringStruct& v) { os << "a='"; - for (size_t i = 0; i < 3 && v.a[i]; i++) os << v.a[i]; + for (size_t i = 0; i < 3 && (v.a[i] != 0); i++) + os << v.a[i]; os << "',b='"; - for (size_t i = 0; i < 3 && v.b[i]; i++) os << v.b[i]; + for (size_t i = 0; i < 3 && (v.b[i] != 0); i++) + os << v.b[i]; return os << "'"; } diff --git a/tests/test_numpy_vectorize.cpp b/tests/test_numpy_vectorize.cpp index ed08a42bef..b08a9f7edd 100644 --- a/tests/test_numpy_vectorize.cpp +++ b/tests/test_numpy_vectorize.cpp @@ -59,7 +59,7 @@ TEST_SUBMODULE(numpy_vectorize, m) { .def(py::init()) .def_readwrite("value", &NonPODClass::value); m.def("vec_passthrough", - py::vectorize([](double *a, + py::vectorize([](const double *a, double b, // Changing this broke things // NOLINTNEXTLINE(performance-unnecessary-value-param) diff --git a/tests/test_smart_ptr.cpp b/tests/test_smart_ptr.cpp index 7fd5a9b36e..57b2d894e2 100644 --- a/tests/test_smart_ptr.cpp +++ b/tests/test_smart_ptr.cpp @@ -101,7 +101,7 @@ class MyObject3 : public std::enable_shared_from_this { // test_unique_nodelete // Object with a private destructor class MyObject4; -static std::unordered_set myobject4_instances; +std::unordered_set myobject4_instances; class MyObject4 { public: MyObject4(int value) : value{value} { @@ -127,7 +127,7 @@ class MyObject4 { // Object with std::unique_ptr where D is not matching the base class // Object with a protected destructor class MyObject4a; -static std::unordered_set myobject4a_instances; +std::unordered_set myobject4a_instances; class MyObject4a { public: MyObject4a(int i) { diff --git a/tests/test_stl.cpp b/tests/test_stl.cpp index eb8cdab7b5..23e2c07b32 100644 --- a/tests/test_stl.cpp +++ b/tests/test_stl.cpp @@ -102,7 +102,7 @@ TEST_SUBMODULE(stl, m) { // test_set m.def("cast_set", []() { return std::set{"key1", "key2"}; }); m.def("load_set", [](const std::set &set) { - return set.count("key1") && set.count("key2") && set.count("key3"); + return (set.count("key1") != 0u) && (set.count("key2") != 0u) && (set.count("key3") != 0u); }); // test_recursive_casting @@ -196,9 +196,7 @@ TEST_SUBMODULE(stl, m) { m.def("double_or_zero", [](const opt_int& x) -> int { return x.value_or(0) * 2; }); - m.def("half_or_none", [](int x) -> opt_int { - return x ? opt_int(x / 2) : opt_int(); - }); + m.def("half_or_none", [](int x) -> opt_int { return x != 0 ? opt_int(x / 2) : opt_int(); }); m.def("test_nullopt", [](opt_int x) { return x.value_or(42); }, py::arg_v("x", std::nullopt, "None")); From 2164c2e0e78ca4bf12123f98349722907732581b Mon Sep 17 00:00:00 2001 From: "Ralf W. Grosse-Kunstleve" Date: Tue, 27 Jul 2021 15:33:31 -0700 Subject: [PATCH 094/638] Removing __INTEL_COMPILER section from pragma block at the top of pybind11.h (#3135) * Fixing `pragma warning pop` for `__INTEL_COMPILER`. * Adding push/pop to 3 tests. Removing #878 from top of pybind11.h (it was/is only needed for 1 test). * Trying again after CI failure, moving the push to the top of 2 tests. * Trying more after CI failure, adding push/pop to pybind11_tests.h, constructor_stats.h. * Moving ICC #2196 suppression to CMakeLists.txt * Fixing condition for `pragma GCC diagnostic push` in pybind11.h * Moving `pragma warning disable 2196` to common.h * Revising #ifdef to be more conservative. * Undoing insertion of notes that will hopefully soon be completely obsolete anyway. --- include/pybind11/detail/common.h | 3 +++ include/pybind11/pybind11.h | 8 ++------ tests/test_constants_and_functions.cpp | 7 +++++++ 3 files changed, 12 insertions(+), 6 deletions(-) diff --git a/include/pybind11/detail/common.h b/include/pybind11/detail/common.h index 3faf3ea327..0b4e30c18b 100644 --- a/include/pybind11/detail/common.h +++ b/include/pybind11/detail/common.h @@ -56,6 +56,9 @@ # elif __INTEL_COMPILER < 1900 && defined(PYBIND11_CPP14) # error pybind11 supports only C++11 with Intel C++ compiler v18. Use v19 or newer for C++14. # endif +/* The following pragma cannot be pop'ed: + https://community.intel.com/t5/Intel-C-Compiler/Inline-and-no-inline-warning/td-p/1216764 */ +# pragma warning disable 2196 // warning #2196: routine is both "inline" and "noinline" #elif defined(__clang__) && !defined(__apple_build_version__) # if __clang_major__ < 3 || (__clang_major__ == 3 && __clang_minor__ < 3) # error pybind11 requires clang 3.3 or newer diff --git a/include/pybind11/pybind11.h b/include/pybind11/pybind11.h index e2cbb54493..b14aff58a2 100644 --- a/include/pybind11/pybind11.h +++ b/include/pybind11/pybind11.h @@ -10,16 +10,12 @@ #pragma once -#if defined(__INTEL_COMPILER) -# pragma warning push -# pragma warning disable 878 // incompatible exception specifications -# pragma warning disable 2196 // warning #2196: routine is both "inline" and "noinline" -#elif defined(_MSC_VER) +#if defined(_MSC_VER) && !defined(__INTEL_COMPILER) # pragma warning(push) # pragma warning(disable: 4100) // warning C4100: Unreferenced formal parameter # pragma warning(disable: 4127) // warning C4127: Conditional expression is constant # pragma warning(disable: 4505) // warning C4505: 'PySlice_GetIndicesEx': unreferenced local function has been removed (PyPy only) -#elif defined(__GNUG__) && !defined(__clang__) +#elif defined(__GNUG__) && !defined(__clang__) && !defined(__INTEL_COMPILER) # pragma GCC diagnostic push # pragma GCC diagnostic ignored "-Wunused-but-set-parameter" # pragma GCC diagnostic ignored "-Wattributes" diff --git a/tests/test_constants_and_functions.cpp b/tests/test_constants_and_functions.cpp index e7f018540c..c0554503fa 100644 --- a/tests/test_constants_and_functions.cpp +++ b/tests/test_constants_and_functions.cpp @@ -133,7 +133,14 @@ TEST_SUBMODULE(constants_and_functions, m) { ; m.def("f1", f1); m.def("f2", f2); +#if defined(__INTEL_COMPILER) +# pragma warning push +# pragma warning disable 878 // incompatible exception specifications +#endif m.def("f3", f3); +#if defined(__INTEL_COMPILER) +# pragma warning pop +#endif m.def("f4", f4); // test_function_record_leaks From f4721a7b44e5513df3681ccb2c08eca50132b4bd Mon Sep 17 00:00:00 2001 From: "Ralf W. Grosse-Kunstleve" Date: Wed, 28 Jul 2021 08:58:36 -0700 Subject: [PATCH 095/638] Accommodating environments that define __STDC_WANT_LIB_EXT1__ even if __STDC_LIB_EXT1__ is not defined by the implementation. (#3151) Follow-on to PR #3129. --- include/pybind11/chrono.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/pybind11/chrono.h b/include/pybind11/chrono.h index c4e81f5720..61bbcbc540 100644 --- a/include/pybind11/chrono.h +++ b/include/pybind11/chrono.h @@ -101,7 +101,7 @@ template class duration_caster { }; inline std::tm *localtime_thread_safe(const std::time_t *time, std::tm *buf) { -#if defined(__STDC_WANT_LIB_EXT1__) || defined(_MSC_VER) +#if (defined(__STDC_LIB_EXT1__) && defined(__STDC_WANT_LIB_EXT1__)) || defined(_MSC_VER) if (localtime_s(buf, time)) return nullptr; return buf; From b72ca7d1bd8d07470b5094017c790f6c763f9b0f Mon Sep 17 00:00:00 2001 From: "Ralf W. Grosse-Kunstleve" Date: Wed, 28 Jul 2021 17:01:21 -0700 Subject: [PATCH 096/638] Removing MSVC C4100 from pragma block at the top of pybind11.h (#3150) * Removing pragma for 4100 (to see what is still broken with the latest code). * Adding --keep-going * Revert "Adding --keep-going" This reverts commit 1c844c6ffd07a6111b644811e7e3b0a50b9d44bb. * Introducing PYBIND11_WORKAROUND_INCORRECT_MSVC_C4100. * _MSC_VER <= 1916 * Replacing simple variadic function with variadic template (attempt to resolve MSVC 2017 failures). * Preserving existing comment (moved from pybind11.h to detail/common.h). * Adding blank lines for readability. --- include/pybind11/attr.h | 5 +++++ include/pybind11/cast.h | 1 + include/pybind11/detail/common.h | 13 +++++++++++++ include/pybind11/detail/descr.h | 1 + include/pybind11/detail/init.h | 3 +++ include/pybind11/detail/type_caster_base.h | 2 ++ include/pybind11/pybind11.h | 3 ++- 7 files changed, 27 insertions(+), 1 deletion(-) diff --git a/include/pybind11/attr.h b/include/pybind11/attr.h index 60ed9fd90e..20d119f0ff 100644 --- a/include/pybind11/attr.h +++ b/include/pybind11/attr.h @@ -516,18 +516,22 @@ template struct process_attribute struct process_attributes { static void init(const Args&... args, function_record *r) { + PYBIND11_WORKAROUND_INCORRECT_MSVC_C4100(r); int unused[] = { 0, (process_attribute::type>::init(args, r), 0) ... }; ignore_unused(unused); } static void init(const Args&... args, type_record *r) { + PYBIND11_WORKAROUND_INCORRECT_MSVC_C4100(r); int unused[] = { 0, (process_attribute::type>::init(args, r), 0) ... }; ignore_unused(unused); } static void precall(function_call &call) { + PYBIND11_WORKAROUND_INCORRECT_MSVC_C4100(call); int unused[] = { 0, (process_attribute::type>::precall(call), 0) ... }; ignore_unused(unused); } static void postcall(function_call &call, handle fn_ret) { + PYBIND11_WORKAROUND_INCORRECT_MSVC_C4100(call, fn_ret); int unused[] = { 0, (process_attribute::type>::postcall(call, fn_ret), 0) ... }; ignore_unused(unused); } @@ -545,6 +549,7 @@ template ::value...), size_t self = constexpr_sum(std::is_same::value...)> constexpr bool expected_num_args(size_t nargs, bool has_args, bool has_kwargs) { + PYBIND11_WORKAROUND_INCORRECT_MSVC_C4100(nargs, has_args, has_kwargs); return named == 0 || (self + named + size_t(has_args) + size_t(has_kwargs)) == nargs; } diff --git a/include/pybind11/cast.h b/include/pybind11/cast.h index 898047b304..5ff0355a6f 100644 --- a/include/pybind11/cast.h +++ b/include/pybind11/cast.h @@ -605,6 +605,7 @@ template class Tuple, typename... Ts> class tuple_caster /* Implementation: Convert a C++ tuple into a Python tuple */ template static handle cast_impl(T &&src, return_value_policy policy, handle parent, index_sequence) { + PYBIND11_WORKAROUND_INCORRECT_MSVC_C4100(src, policy, parent); std::array entries{{ reinterpret_steal(make_caster::cast(std::get(std::forward(src)), policy, parent))... }}; diff --git a/include/pybind11/detail/common.h b/include/pybind11/detail/common.h index 0b4e30c18b..0add6272f5 100644 --- a/include/pybind11/detail/common.h +++ b/include/pybind11/detail/common.h @@ -913,5 +913,18 @@ inline static std::shared_ptr try_get_shared_from_this(std::enable_shared_fro #endif } +#if defined(_MSC_VER) && _MSC_VER <= 1916 + +// warning C4100: Unreferenced formal parameter +template +inline constexpr void workaround_incorrect_msvc_c4100(Args &&...) {} + +# define PYBIND11_WORKAROUND_INCORRECT_MSVC_C4100(...) \ + detail::workaround_incorrect_msvc_c4100(__VA_ARGS__) + +#else +# define PYBIND11_WORKAROUND_INCORRECT_MSVC_C4100(...) +#endif + PYBIND11_NAMESPACE_END(detail) PYBIND11_NAMESPACE_END(PYBIND11_NAMESPACE) diff --git a/include/pybind11/detail/descr.h b/include/pybind11/detail/descr.h index 7cb8350e7b..0acfc7db37 100644 --- a/include/pybind11/detail/descr.h +++ b/include/pybind11/detail/descr.h @@ -42,6 +42,7 @@ struct descr { template constexpr descr plus_impl(const descr &a, const descr &b, index_sequence, index_sequence) { + PYBIND11_WORKAROUND_INCORRECT_MSVC_C4100(b); return {a.text[Is1]..., b.text[Is2]...}; } diff --git a/include/pybind11/detail/init.h b/include/pybind11/detail/init.h index 3269e04254..3ebec041f0 100644 --- a/include/pybind11/detail/init.h +++ b/include/pybind11/detail/init.h @@ -94,6 +94,7 @@ void construct(...) { // construct an Alias from the returned base instance. template void construct(value_and_holder &v_h, Cpp *ptr, bool need_alias) { + PYBIND11_WORKAROUND_INCORRECT_MSVC_C4100(need_alias); no_nullptr(ptr); if (Class::has_alias && need_alias && !is_alias(ptr)) { // We're going to try to construct an alias by moving the cpp type. Whether or not @@ -131,6 +132,7 @@ void construct(value_and_holder &v_h, Alias *alias_ptr, bool) { // derived type (through those holder's implicit conversion from derived class holder constructors). template void construct(value_and_holder &v_h, Holder holder, bool need_alias) { + PYBIND11_WORKAROUND_INCORRECT_MSVC_C4100(need_alias); auto *ptr = holder_helper>::get(holder); no_nullptr(ptr); // If we need an alias, check that the held pointer is actually an alias instance @@ -148,6 +150,7 @@ void construct(value_and_holder &v_h, Holder holder, bool need_alias) { // need it, we simply move-construct the cpp value into a new instance. template void construct(value_and_holder &v_h, Cpp &&result, bool need_alias) { + PYBIND11_WORKAROUND_INCORRECT_MSVC_C4100(need_alias); static_assert(std::is_move_constructible>::value, "pybind11::init() return-by-value factory function requires a movable class"); if (Class::has_alias && need_alias) diff --git a/include/pybind11/detail/type_caster_base.h b/include/pybind11/detail/type_caster_base.h index 2a675418a7..e2d1bcb8cc 100644 --- a/include/pybind11/detail/type_caster_base.h +++ b/include/pybind11/detail/type_caster_base.h @@ -930,6 +930,7 @@ template class type_caster_base : public type_caster_generic { does not have a private operator new implementation. */ template ::value>> static auto make_copy_constructor(const T *x) -> decltype(new T(*x), Constructor{}) { + PYBIND11_WORKAROUND_INCORRECT_MSVC_C4100(x); return [](const void *arg) -> void * { return new T(*reinterpret_cast(arg)); }; @@ -937,6 +938,7 @@ template class type_caster_base : public type_caster_generic { template ::value>> static auto make_move_constructor(const T *x) -> decltype(new T(std::move(*const_cast(x))), Constructor{}) { + PYBIND11_WORKAROUND_INCORRECT_MSVC_C4100(x); return [](const void *arg) -> void * { return new T(std::move(*const_cast(reinterpret_cast(arg)))); }; diff --git a/include/pybind11/pybind11.h b/include/pybind11/pybind11.h index b14aff58a2..139a4111e9 100644 --- a/include/pybind11/pybind11.h +++ b/include/pybind11/pybind11.h @@ -12,7 +12,6 @@ #if defined(_MSC_VER) && !defined(__INTEL_COMPILER) # pragma warning(push) -# pragma warning(disable: 4100) // warning C4100: Unreferenced formal parameter # pragma warning(disable: 4127) // warning C4127: Conditional expression is constant # pragma warning(disable: 4505) // warning C4505: 'PySlice_GetIndicesEx': unreferenced local function has been removed (PyPy only) #elif defined(__GNUG__) && !defined(__clang__) && !defined(__INTEL_COMPILER) @@ -1388,12 +1387,14 @@ class class_ : public detail::generic_type { template class_ &def(const detail::initimpl::constructor &init, const Extra&... extra) { + PYBIND11_WORKAROUND_INCORRECT_MSVC_C4100(init); init.execute(*this, extra...); return *this; } template class_ &def(const detail::initimpl::alias_constructor &init, const Extra&... extra) { + PYBIND11_WORKAROUND_INCORRECT_MSVC_C4100(init); init.execute(*this, extra...); return *this; } From c14b193308d7877b158e9187079078099d1052ee Mon Sep 17 00:00:00 2001 From: Henry Schreiner Date: Wed, 28 Jul 2021 18:04:14 -0700 Subject: [PATCH 097/638] chore: increase CMake upper limit (#3124) --- .github/workflows/configure.yml | 2 +- CMakeLists.txt | 6 +++--- tests/CMakeLists.txt | 4 ++-- tests/test_cmake_build/installed_embed/CMakeLists.txt | 6 ++++-- tests/test_cmake_build/installed_function/CMakeLists.txt | 3 ++- tests/test_cmake_build/installed_target/CMakeLists.txt | 3 ++- tests/test_cmake_build/subdirectory_embed/CMakeLists.txt | 6 ++++-- tests/test_cmake_build/subdirectory_function/CMakeLists.txt | 3 ++- tests/test_cmake_build/subdirectory_target/CMakeLists.txt | 3 ++- tests/test_embed/CMakeLists.txt | 1 + 10 files changed, 23 insertions(+), 14 deletions(-) diff --git a/.github/workflows/configure.yml b/.github/workflows/configure.yml index abcff1dde5..924088fc37 100644 --- a/.github/workflows/configure.yml +++ b/.github/workflows/configure.yml @@ -18,7 +18,7 @@ jobs: matrix: runs-on: [ubuntu-latest, macos-latest, windows-latest] arch: [x64] - cmake: [3.18] + cmake: ["3.21"] include: - runs-on: ubuntu-latest diff --git a/CMakeLists.txt b/CMakeLists.txt index b04311fd85..bb01a861ae 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -7,13 +7,13 @@ cmake_minimum_required(VERSION 3.4) -# The `cmake_minimum_required(VERSION 3.4...3.18)` syntax does not work with +# The `cmake_minimum_required(VERSION 3.4...3.21)` syntax does not work with # some versions of VS that have a patched CMake 3.11. This forces us to emulate # the behavior using the following workaround: -if(${CMAKE_VERSION} VERSION_LESS 3.18) +if(${CMAKE_VERSION} VERSION_LESS 3.21) cmake_policy(VERSION ${CMAKE_MAJOR_VERSION}.${CMAKE_MINOR_VERSION}) else() - cmake_policy(VERSION 3.18) + cmake_policy(VERSION 3.21) endif() # Extract project version from source diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index dc70038dba..d71a51e6a0 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -10,10 +10,10 @@ cmake_minimum_required(VERSION 3.4) # The `cmake_minimum_required(VERSION 3.4...3.18)` syntax does not work with # some versions of VS that have a patched CMake 3.11. This forces us to emulate # the behavior using the following workaround: -if(${CMAKE_VERSION} VERSION_LESS 3.18) +if(${CMAKE_VERSION} VERSION_LESS 3.21) cmake_policy(VERSION ${CMAKE_MAJOR_VERSION}.${CMAKE_MINOR_VERSION}) else() - cmake_policy(VERSION 3.18) + cmake_policy(VERSION 3.21) endif() # Only needed for CMake < 3.5 support diff --git a/tests/test_cmake_build/installed_embed/CMakeLists.txt b/tests/test_cmake_build/installed_embed/CMakeLists.txt index 64ae5c4bff..f7d6939982 100644 --- a/tests/test_cmake_build/installed_embed/CMakeLists.txt +++ b/tests/test_cmake_build/installed_embed/CMakeLists.txt @@ -22,5 +22,7 @@ set_target_properties(test_installed_embed PROPERTIES OUTPUT_NAME test_cmake_bui # This may be needed to resolve header conflicts, e.g. between Python release and debug headers. set_target_properties(test_installed_embed PROPERTIES NO_SYSTEM_FROM_IMPORTED ON) -add_custom_target(check_installed_embed $ - ${PROJECT_SOURCE_DIR}/../test.py) +add_custom_target( + check_installed_embed + $ ${PROJECT_SOURCE_DIR}/../test.py + DEPENDS test_installed_embed) diff --git a/tests/test_cmake_build/installed_function/CMakeLists.txt b/tests/test_cmake_build/installed_function/CMakeLists.txt index 1a502863c0..d7ca4db55d 100644 --- a/tests/test_cmake_build/installed_function/CMakeLists.txt +++ b/tests/test_cmake_build/installed_function/CMakeLists.txt @@ -35,4 +35,5 @@ add_custom_target( PYTHONPATH=$ ${_Python_EXECUTABLE} ${PROJECT_SOURCE_DIR}/../test.py - ${PROJECT_NAME}) + ${PROJECT_NAME} + DEPENDS test_installed_function) diff --git a/tests/test_cmake_build/installed_target/CMakeLists.txt b/tests/test_cmake_build/installed_target/CMakeLists.txt index b38eb77470..bc5e101f1d 100644 --- a/tests/test_cmake_build/installed_target/CMakeLists.txt +++ b/tests/test_cmake_build/installed_target/CMakeLists.txt @@ -42,4 +42,5 @@ add_custom_target( PYTHONPATH=$ ${_Python_EXECUTABLE} ${PROJECT_SOURCE_DIR}/../test.py - ${PROJECT_NAME}) + ${PROJECT_NAME} + DEPENDS test_installed_target) diff --git a/tests/test_cmake_build/subdirectory_embed/CMakeLists.txt b/tests/test_cmake_build/subdirectory_embed/CMakeLists.txt index dfb9cb8f63..58cdd7cfd1 100644 --- a/tests/test_cmake_build/subdirectory_embed/CMakeLists.txt +++ b/tests/test_cmake_build/subdirectory_embed/CMakeLists.txt @@ -23,8 +23,10 @@ add_executable(test_subdirectory_embed ../embed.cpp) target_link_libraries(test_subdirectory_embed PRIVATE pybind11::embed) set_target_properties(test_subdirectory_embed PROPERTIES OUTPUT_NAME test_cmake_build) -add_custom_target(check_subdirectory_embed $ - "${PROJECT_SOURCE_DIR}/../test.py") +add_custom_target( + check_subdirectory_embed + $ "${PROJECT_SOURCE_DIR}/../test.py" + DEPENDS test_subdirectory_embed) # Test custom export group -- PYBIND11_EXPORT_NAME add_library(test_embed_lib ../embed.cpp) diff --git a/tests/test_cmake_build/subdirectory_function/CMakeLists.txt b/tests/test_cmake_build/subdirectory_function/CMakeLists.txt index 34aedcf6a8..01557c439a 100644 --- a/tests/test_cmake_build/subdirectory_function/CMakeLists.txt +++ b/tests/test_cmake_build/subdirectory_function/CMakeLists.txt @@ -31,4 +31,5 @@ add_custom_target( PYTHONPATH=$ ${_Python_EXECUTABLE} ${PROJECT_SOURCE_DIR}/../test.py - ${PROJECT_NAME}) + ${PROJECT_NAME} + DEPENDS test_subdirectory_function) diff --git a/tests/test_cmake_build/subdirectory_target/CMakeLists.txt b/tests/test_cmake_build/subdirectory_target/CMakeLists.txt index 31d862f6ad..ba82fdee2e 100644 --- a/tests/test_cmake_build/subdirectory_target/CMakeLists.txt +++ b/tests/test_cmake_build/subdirectory_target/CMakeLists.txt @@ -37,4 +37,5 @@ add_custom_target( PYTHONPATH=$ ${_Python_EXECUTABLE} ${PROJECT_SOURCE_DIR}/../test.py - ${PROJECT_NAME}) + ${PROJECT_NAME} + DEPENDS test_subdirectory_target) diff --git a/tests/test_embed/CMakeLists.txt b/tests/test_embed/CMakeLists.txt index c960c877a5..3b89d6e584 100644 --- a/tests/test_embed/CMakeLists.txt +++ b/tests/test_embed/CMakeLists.txt @@ -31,6 +31,7 @@ endif() add_custom_target( cpptest COMMAND "$" + DEPENDS test_embed WORKING_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}") pybind11_add_module(external_module THIN_LTO external_module.cpp) From ee3ecb8ae2f0d74fe50912364ec65bce1330af51 Mon Sep 17 00:00:00 2001 From: ka-bo <55587089+ka-bo@users.noreply.github.com> Date: Wed, 21 Jul 2021 18:00:57 +0200 Subject: [PATCH 098/638] Specified encoding in setup.py calls of open() (#3137) * Specified encoding in setup.py calls of open() * Fix for Python2 Co-authored-by: Karsten Bock --- setup.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/setup.py b/setup.py index ced3d69581..7aa151c01b 100644 --- a/setup.py +++ b/setup.py @@ -11,6 +11,7 @@ import subprocess import sys import tempfile +import io import setuptools.command.sdist @@ -70,7 +71,7 @@ def build_expected_version_hex(matches): version = loc["__version__"] # Verify that the version matches the one in C++ -with open("include/pybind11/detail/common.h") as f: +with io.open("include/pybind11/detail/common.h", encoding="utf8") as f: matches = dict(VERSION_REGEX.findall(f.read())) cpp_version = "{MAJOR}.{MINOR}.{PATCH}".format(**matches) if version != cpp_version: From b193d42c32763af3c7e3763657b7cba114010121 Mon Sep 17 00:00:00 2001 From: "Ralf W. Grosse-Kunstleve" Date: Mon, 26 Jul 2021 11:28:36 -0700 Subject: [PATCH 099/638] Removing MSVC C4996 from pragma block at the top of pybind11.h (#3129) * Removing MSVC C4996 from pragma block at the top of pybind11.h * localtime_thread_safe, PYBIND11_COMPAT_STRDUP * Adding #include (attempt to fix MSVC 2015, 2017 errors). --- include/pybind11/chrono.h | 33 +++++++++++++++++++++++--------- include/pybind11/detail/common.h | 5 +++++ include/pybind11/pybind11.h | 18 ++++++++++++----- 3 files changed, 42 insertions(+), 14 deletions(-) diff --git a/include/pybind11/chrono.h b/include/pybind11/chrono.h index d32b5b0569..c4e81f5720 100644 --- a/include/pybind11/chrono.h +++ b/include/pybind11/chrono.h @@ -11,9 +11,14 @@ #pragma once #include "pybind11.h" + +#include #include #include -#include +#include + +#include + #include // Backport the PyDateTime_DELTA functions from Python3.3 if required @@ -95,6 +100,22 @@ template class duration_caster { PYBIND11_TYPE_CASTER(type, _("datetime.timedelta")); }; +inline std::tm *localtime_thread_safe(const std::time_t *time, std::tm *buf) { +#if defined(__STDC_WANT_LIB_EXT1__) || defined(_MSC_VER) + if (localtime_s(buf, time)) + return nullptr; + return buf; +#else + static std::mutex mtx; + std::lock_guard lock(mtx); + std::tm *tm_ptr = localtime(time); + if (tm_ptr != nullptr) { + *buf = *tm_ptr; + } + return tm_ptr; +#endif +} + // This is for casting times on the system clock into datetime.datetime instances template class type_caster> { public: @@ -162,16 +183,10 @@ template class type_caster(src - us)); - // std::localtime returns a pointer to a static internal std::tm object on success, - // or null pointer otherwise - std::tm *localtime_ptr = std::localtime(&tt); + std::tm localtime; + std::tm *localtime_ptr = localtime_thread_safe(&tt, &localtime); if (!localtime_ptr) throw cast_error("Unable to represent system_clock in local time"); - - // this function uses static memory so it's best to copy it out asap just in case - // otherwise other code that is using localtime may break this (not just python code) - std::tm localtime = *localtime_ptr; - return PyDateTime_FromDateAndTime(localtime.tm_year + 1900, localtime.tm_mon + 1, localtime.tm_mday, diff --git a/include/pybind11/detail/common.h b/include/pybind11/detail/common.h index 52b0193350..3faf3ea327 100644 --- a/include/pybind11/detail/common.h +++ b/include/pybind11/detail/common.h @@ -125,6 +125,11 @@ # endif #endif +// https://en.cppreference.com/w/c/chrono/localtime +#if defined(__STDC_LIB_EXT1__) && !defined(__STDC_WANT_LIB_EXT1__) +# define __STDC_WANT_LIB_EXT1__ +#endif + #include #include #include diff --git a/include/pybind11/pybind11.h b/include/pybind11/pybind11.h index af8e730d3f..cd6e411960 100644 --- a/include/pybind11/pybind11.h +++ b/include/pybind11/pybind11.h @@ -19,7 +19,6 @@ # pragma warning(disable: 4100) // warning C4100: Unreferenced formal parameter # pragma warning(disable: 4127) // warning C4127: Conditional expression is constant # pragma warning(disable: 4800) // warning C4800: 'int': forcing value to bool 'true' or 'false' (performance warning) -# pragma warning(disable: 4996) // warning C4996: The POSIX name for this item is deprecated. Instead, use the ISO C and C++ conformant name # pragma warning(disable: 4522) // warning C4522: multiple assignment operators specified # pragma warning(disable: 4505) // warning C4505: 'PySlice_GetIndicesEx': unreferenced local function has been removed (PyPy only) #elif defined(__GNUG__) && !defined(__clang__) @@ -43,6 +42,8 @@ #include #include +#include + #if defined(__cpp_lib_launder) && !(defined(_MSC_VER) && (_MSC_VER < 1914)) # define PYBIND11_STD_LAUNDER std::launder # define PYBIND11_HAS_STD_LAUNDER 1 @@ -56,6 +57,12 @@ PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE) +#if defined(_MSC_VER) +# define PYBIND11_COMPAT_STRDUP _strdup +#else +# define PYBIND11_COMPAT_STRDUP strdup +#endif + /// Wraps an arbitrary C++ function/method/lambda function/.. into a callable Python object class cpp_function : public function { public: @@ -253,7 +260,7 @@ class cpp_function : public function { std::free(s); } char *operator()(const char *s) { - auto t = strdup(s); + auto t = PYBIND11_COMPAT_STRDUP(s); strings.push_back(t); return t; } @@ -497,7 +504,8 @@ class cpp_function : public function { auto *func = (PyCFunctionObject *) m_ptr; std::free(const_cast(func->m_ml->ml_doc)); // Install docstring if it's non-empty (when at least one option is enabled) - func->m_ml->ml_doc = signatures.empty() ? nullptr : strdup(signatures.c_str()); + func->m_ml->ml_doc + = signatures.empty() ? nullptr : PYBIND11_COMPAT_STRDUP(signatures.c_str()); if (rec->is_method) { m_ptr = PYBIND11_INSTANCE_METHOD_NEW(m_ptr, rec->scope.ptr()); @@ -1498,7 +1506,7 @@ class class_ : public detail::generic_type { detail::process_attributes::init(extra..., rec_fget); if (rec_fget->doc && rec_fget->doc != doc_prev) { free(doc_prev); - rec_fget->doc = strdup(rec_fget->doc); + rec_fget->doc = PYBIND11_COMPAT_STRDUP(rec_fget->doc); } } if (rec_fset) { @@ -1506,7 +1514,7 @@ class class_ : public detail::generic_type { detail::process_attributes::init(extra..., rec_fset); if (rec_fset->doc && rec_fset->doc != doc_prev) { free(doc_prev); - rec_fset->doc = strdup(rec_fset->doc); + rec_fset->doc = PYBIND11_COMPAT_STRDUP(rec_fset->doc); } if (! rec_active) rec_active = rec_fset; } From 85b38c69dee5ae57fe23754becd437ebd4e30961 Mon Sep 17 00:00:00 2001 From: "Ralf W. Grosse-Kunstleve" Date: Mon, 26 Jul 2021 12:02:50 -0700 Subject: [PATCH 100/638] Adding pragma warning(disable: 4522) for MSVC <= 2017. (#3142) --- include/pybind11/pybind11.h | 1 - include/pybind11/pytypes.h | 7 +++++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/include/pybind11/pybind11.h b/include/pybind11/pybind11.h index cd6e411960..b5091ff257 100644 --- a/include/pybind11/pybind11.h +++ b/include/pybind11/pybind11.h @@ -19,7 +19,6 @@ # pragma warning(disable: 4100) // warning C4100: Unreferenced formal parameter # pragma warning(disable: 4127) // warning C4127: Conditional expression is constant # pragma warning(disable: 4800) // warning C4800: 'int': forcing value to bool 'true' or 'false' (performance warning) -# pragma warning(disable: 4522) // warning C4522: multiple assignment operators specified # pragma warning(disable: 4505) // warning C4505: 'PySlice_GetIndicesEx': unreferenced local function has been removed (PyPy only) #elif defined(__GNUG__) && !defined(__clang__) # pragma GCC diagnostic push diff --git a/include/pybind11/pytypes.h b/include/pybind11/pytypes.h index b483fb323c..3329982777 100644 --- a/include/pybind11/pytypes.h +++ b/include/pybind11/pytypes.h @@ -532,6 +532,10 @@ object object_or_cast(T &&o); // Match a PyObject*, which we want to convert directly to handle via its converting constructor inline handle object_or_cast(PyObject *ptr) { return ptr; } +#if defined(_MSC_VER) && _MSC_VER < 1920 +# pragma warning(push) +# pragma warning(disable: 4522) // warning C4522: multiple assignment operators specified +#endif template class accessor : public object_api> { using key_type = typename Policy::key_type; @@ -580,6 +584,9 @@ class accessor : public object_api> { key_type key; mutable object cache; }; +#if defined(_MSC_VER) && _MSC_VER < 1920 +# pragma warning(pop) +#endif PYBIND11_NAMESPACE_BEGIN(accessor_policies) struct obj_attr { From c973660d6c5daa2a24bf5629306d640b6447e9ac Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 26 Jul 2021 15:05:58 -0400 Subject: [PATCH 101/638] [pre-commit.ci] pre-commit autoupdate (#3143) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit updates: - [github.com/asottile/pyupgrade: v2.21.2 → v2.23.0](https://github.com/asottile/pyupgrade/compare/v2.21.2...v2.23.0) Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> --- .pre-commit-config.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index aa7b4774e3..510b841260 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -31,7 +31,7 @@ repos: exclude: ^noxfile.py$ - repo: https://github.com/asottile/pyupgrade - rev: v2.21.2 + rev: v2.23.0 hooks: - id: pyupgrade From e93d94594b063682a70ead0b7feb8c1329ae0717 Mon Sep 17 00:00:00 2001 From: "Ralf W. Grosse-Kunstleve" Date: Mon, 26 Jul 2021 13:26:36 -0700 Subject: [PATCH 102/638] Removing MSVC C4800 from pragma block at the top of pybind11.h (#3141) * Adding PYBIND11_COMPAT_BOOL_CAST to appease MSVC 2015 warning C4800. * Replacing PYBIND11_COMPAT_BOOL_CAST with simpler != 0 * Extra parentheses (almost all compilers failed without these). --- include/pybind11/buffer_info.h | 2 +- include/pybind11/cast.h | 2 +- include/pybind11/detail/type_caster_base.h | 4 ++-- include/pybind11/pybind11.h | 1 - include/pybind11/pytypes.h | 6 ++++-- 5 files changed, 8 insertions(+), 7 deletions(-) diff --git a/include/pybind11/buffer_info.h b/include/pybind11/buffer_info.h index 47dc39d4ea..eba68d1aa1 100644 --- a/include/pybind11/buffer_info.h +++ b/include/pybind11/buffer_info.h @@ -83,7 +83,7 @@ struct buffer_info { view->strides ? std::vector(view->strides, view->strides + view->ndim) : detail::c_strides({view->shape, view->shape + view->ndim}, view->itemsize), - view->readonly) { + (view->readonly != 0)) { this->m_view = view; this->ownview = ownview; } diff --git a/include/pybind11/cast.h b/include/pybind11/cast.h index a748c77c0f..1da432358f 100644 --- a/include/pybind11/cast.h +++ b/include/pybind11/cast.h @@ -322,7 +322,7 @@ template <> class type_caster { } #endif if (res == 0 || res == 1) { - value = (bool) res; + value = (res != 0); return true; } PyErr_Clear(); diff --git a/include/pybind11/detail/type_caster_base.h b/include/pybind11/detail/type_caster_base.h index 451690dc73..39d0740ff8 100644 --- a/include/pybind11/detail/type_caster_base.h +++ b/include/pybind11/detail/type_caster_base.h @@ -231,7 +231,7 @@ struct value_and_holder { return reinterpret_cast(vh[0]); } // True if this `value_and_holder` has a non-null value pointer - explicit operator bool() const { return value_ptr(); } + explicit operator bool() const { return value_ptr() != nullptr; } template H &holder() const { return reinterpret_cast(vh[1]); @@ -252,7 +252,7 @@ struct value_and_holder { bool instance_registered() const { return inst->simple_layout ? inst->simple_instance_registered - : inst->nonsimple.status[index] & instance::status_instance_registered; + : ((inst->nonsimple.status[index] & instance::status_instance_registered) != 0); } void set_instance_registered(bool v = true) const { if (inst->simple_layout) diff --git a/include/pybind11/pybind11.h b/include/pybind11/pybind11.h index b5091ff257..6a2bd0f24d 100644 --- a/include/pybind11/pybind11.h +++ b/include/pybind11/pybind11.h @@ -18,7 +18,6 @@ # pragma warning(push) # pragma warning(disable: 4100) // warning C4100: Unreferenced formal parameter # pragma warning(disable: 4127) // warning C4127: Conditional expression is constant -# pragma warning(disable: 4800) // warning C4800: 'int': forcing value to bool 'true' or 'false' (performance warning) # pragma warning(disable: 4505) // warning C4505: 'PySlice_GetIndicesEx': unreferenced local function has been removed (PyPy only) #elif defined(__GNUG__) && !defined(__clang__) # pragma GCC diagnostic push diff --git a/include/pybind11/pytypes.h b/include/pybind11/pytypes.h index 3329982777..cea4e7eb01 100644 --- a/include/pybind11/pytypes.h +++ b/include/pybind11/pytypes.h @@ -367,7 +367,9 @@ class PYBIND11_EXPORT error_already_set : public std::runtime_error { /// Check if the currently trapped error type matches the given Python exception class (or a /// subclass thereof). May also be passed a tuple to search for any exception class matches in /// the given tuple. - bool matches(handle exc) const { return PyErr_GivenExceptionMatches(m_type.ptr(), exc.ptr()); } + bool matches(handle exc) const { + return (PyErr_GivenExceptionMatches(m_type.ptr(), exc.ptr()) != 0); + } const object& type() const { return m_type; } const object& value() const { return m_value; } @@ -853,7 +855,7 @@ PYBIND11_NAMESPACE_END(detail) Name(handle h, borrowed_t) : Parent(h, borrowed_t{}) { } \ Name(handle h, stolen_t) : Parent(h, stolen_t{}) { } \ PYBIND11_DEPRECATED("Use py::isinstance(obj) instead") \ - bool check() const { return m_ptr != nullptr && (bool) CheckFun(m_ptr); } \ + bool check() const { return m_ptr != nullptr && (CheckFun(m_ptr) != 0); } \ static bool check_(handle h) { return h.ptr() != nullptr && CheckFun(h.ptr()); } \ template \ Name(const ::pybind11::detail::accessor &a) : Name(object(a)) { } From fd71bd486d35f8e8a0d37e65e4654c9700990488 Mon Sep 17 00:00:00 2001 From: David Hewitt <1939362+davidhewitt@users.noreply.github.com> Date: Tue, 27 Jul 2021 19:16:28 +0100 Subject: [PATCH 103/638] Allow python builtins to be used as callbacks (#1413) * Allow python builtins to be used as callbacks * Try to fix pypy segfault * Add expected fail for PyPy * Fix typo * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Add more info to xfail * Add env * Try returning false * Try removing the move for pypy * Fix bugs * Try removing move * Just keep ignoring for PyPy * Add back xfail * Fix ctors * Revert change of std::move * Change to skip * Fix bug and edit comments * Remove clang-tidy bugprone fix skip bug Co-authored-by: Aaron Gokaslan Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> --- include/pybind11/functional.h | 36 ++++++++++++++++++++--------------- tests/test_callbacks.cpp | 6 ++++++ tests/test_callbacks.py | 11 +++++++++++ 3 files changed, 38 insertions(+), 15 deletions(-) diff --git a/include/pybind11/functional.h b/include/pybind11/functional.h index cc7099c6a3..b9bf38919c 100644 --- a/include/pybind11/functional.h +++ b/include/pybind11/functional.h @@ -43,27 +43,33 @@ struct type_caster> { captured variables), in which case the roundtrip can be avoided. */ if (auto cfunc = func.cpp_function()) { - auto c = reinterpret_borrow(PyCFunction_GET_SELF(cfunc.ptr())); - auto rec = (function_record *) c; - - while (rec != nullptr) { - if (rec->is_stateless - && same_type(typeid(function_type), - *reinterpret_cast(rec->data[1]))) { - struct capture { - function_type f; - }; - value = ((capture *) &rec->data)->f; - return true; + auto cfunc_self = PyCFunction_GET_SELF(cfunc.ptr()); + if (isinstance(cfunc_self)) { + auto c = reinterpret_borrow(cfunc_self); + auto rec = (function_record *) c; + + while (rec != nullptr) { + if (rec->is_stateless + && same_type(typeid(function_type), + *reinterpret_cast(rec->data[1]))) { + struct capture { + function_type f; + }; + value = ((capture *) &rec->data)->f; + return true; + } + rec = rec->next; } - rec = rec->next; } + // PYPY segfaults here when passing builtin function like sum. + // Raising an fail exception here works to prevent the segfault, but only on gcc. + // See PR #1413 for full details } // ensure GIL is held during functor destruction struct func_handle { function f; - func_handle(function&& f_) : f(std::move(f_)) {} + func_handle(function &&f_) noexcept : f(std::move(f_)) {} func_handle(const func_handle& f_) { gil_scoped_acquire acq; f = f_.f; @@ -77,7 +83,7 @@ struct type_caster> { // to emulate 'move initialization capture' in C++11 struct func_wrapper { func_handle hfunc; - func_wrapper(func_handle&& hf): hfunc(std::move(hf)) {} + func_wrapper(func_handle &&hf) noexcept : hfunc(std::move(hf)) {} Return operator()(Args... args) const { gil_scoped_acquire acq; object retval(hfunc.f(std::forward(args)...)); diff --git a/tests/test_callbacks.cpp b/tests/test_callbacks.cpp index a208b44d04..a50771038b 100644 --- a/tests/test_callbacks.cpp +++ b/tests/test_callbacks.cpp @@ -156,6 +156,12 @@ TEST_SUBMODULE(callbacks, m) { .def(py::init<>()) .def("triple", [](CppBoundMethodTest &, int val) { return 3 * val; }); + // This checks that builtin functions can be passed as callbacks + // rather than throwing RuntimeError due to trying to extract as capsule + m.def("test_sum_builtin", [](const std::function &sum_builtin, const py::iterable &i) { + return sum_builtin(i); + }); + // test async Python callbacks using callback_f = std::function; m.def("test_async_callback", [](const callback_f &f, const py::list &work) { diff --git a/tests/test_callbacks.py b/tests/test_callbacks.py index 397bf63d24..5bc4d1773d 100644 --- a/tests/test_callbacks.py +++ b/tests/test_callbacks.py @@ -3,6 +3,7 @@ from pybind11_tests import callbacks as m from threading import Thread import time +import env # NOQA: F401 def test_callbacks(): @@ -124,6 +125,16 @@ def test_movable_object(): assert m.callback_with_movable(lambda _: None) is True +@pytest.mark.skipif( + "env.PYPY", + reason="PyPy segfaults on here. See discussion on #1413.", +) +def test_python_builtins(): + """Test if python builtins like sum() can be used as callbacks""" + assert m.test_sum_builtin(sum, [1, 2, 3]) == 6 + assert m.test_sum_builtin(sum, []) == 0 + + def test_async_callbacks(): # serves as state for async callback class Item: From 5c6bdb72156f5b290611b33bb2db258a1caa3488 Mon Sep 17 00:00:00 2001 From: Henry Schreiner Date: Tue, 27 Jul 2021 14:23:52 -0700 Subject: [PATCH 104/638] fix: the CMake config in Python package had a hard coded path (#3144) --- CMakeLists.txt | 6 ++++++ tests/extra_python_package/test_files.py | 14 ++++++++++++-- tools/pybind11Config.cmake.in | 3 ++- 3 files changed, 20 insertions(+), 3 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index c988ea0b50..b04311fd85 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -203,6 +203,12 @@ if(PYBIND11_INSTALL) "${CMAKE_INSTALL_DATAROOTDIR}/cmake/${PROJECT_NAME}" CACHE STRING "install path for pybind11Config.cmake") + if(IS_ABSOLUTE "${CMAKE_INSTALL_INCLUDEDIR}") + set(pybind11_INCLUDEDIR "${CMAKE_INSTALL_FULL_INCLUDEDIR}") + else() + set(pybind11_INCLUDEDIR "\$\{PACKAGE_PREFIX_DIR\}/${CMAKE_INSTALL_INCLUDEDIR}") + endif() + configure_package_config_file( tools/${PROJECT_NAME}Config.cmake.in "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}Config.cmake" INSTALL_DESTINATION ${PYBIND11_CMAKECONFIG_INSTALL_DIR}) diff --git a/tests/extra_python_package/test_files.py b/tests/extra_python_package/test_files.py index 43e93c2633..337a72dfea 100644 --- a/tests/extra_python_package/test_files.py +++ b/tests/extra_python_package/test_files.py @@ -138,6 +138,16 @@ def test_build_sdist(monkeypatch, tmpdir): ) as f: pyproject_toml = f.read() + with contextlib.closing( + tar.extractfile( + tar.getmember( + start + "pybind11/share/cmake/pybind11/pybind11Config.cmake" + ) + ) + ) as f: + contents = f.read().decode("utf8") + assert 'set(pybind11_INCLUDE_DIR "${PACKAGE_PREFIX_DIR}/include")' in contents + files = {"pybind11/{}".format(n) for n in all_files} files |= sdist_files files |= {"pybind11{}".format(n) for n in local_sdist_files} @@ -151,11 +161,11 @@ def test_build_sdist(monkeypatch, tmpdir): .substitute(version=version, extra_cmd="") .encode() ) - assert setup_py == contents + assert setup_py == contents with open(os.path.join(MAIN_DIR, "tools", "pyproject.toml"), "rb") as f: contents = f.read() - assert pyproject_toml == contents + assert pyproject_toml == contents def test_build_global_dist(monkeypatch, tmpdir): diff --git a/tools/pybind11Config.cmake.in b/tools/pybind11Config.cmake.in index 6fa03a0f24..73ec104a0e 100644 --- a/tools/pybind11Config.cmake.in +++ b/tools/pybind11Config.cmake.in @@ -201,7 +201,8 @@ Using ``find_package`` with version info is not recommended except for release v @PACKAGE_INIT@ # Location of pybind11/pybind11.h -set(pybind11_INCLUDE_DIR "@CMAKE_INSTALL_FULL_INCLUDEDIR@") +# This will be relative unless explicitly set as absolute +set(pybind11_INCLUDE_DIR "@pybind11_INCLUDEDIR@") set(pybind11_LIBRARY "") set(pybind11_DEFINITIONS USING_pybind11) From 0ac4c8afd6484bc95b84061fb7651ca59e4701e6 Mon Sep 17 00:00:00 2001 From: Aaron Gokaslan Date: Tue, 27 Jul 2021 18:32:26 -0400 Subject: [PATCH 105/638] maint(clang-tidy): Improve code readability with explicit boolean casts (#3148) * maint(clang-tidy) Improve code readability * Fix minor typos * Revert optimization that removed test case * Fix comment formatting * Revert another optimization to repro an issue * Remove make_unique since it C++14 and newer only * eformat comments * Fix unsignedness of comparison * Update comment --- .clang-tidy | 13 +++++++++++++ include/pybind11/cast.h | 16 ++++++++++------ include/pybind11/detail/class.h | 7 ++++--- include/pybind11/detail/internals.h | 2 +- include/pybind11/detail/type_caster_base.h | 4 ++-- include/pybind11/embed.h | 4 ++-- include/pybind11/numpy.h | 19 +++++++++++-------- include/pybind11/pybind11.h | 15 +++++++++------ include/pybind11/pytypes.h | 12 ++++++++---- include/pybind11/stl/filesystem.h | 4 ++-- tests/test_embed/test_interpreter.cpp | 2 +- tests/test_methods_and_attributes.cpp | 1 + tests/test_numpy_dtypes.cpp | 6 ++++-- tests/test_numpy_vectorize.cpp | 2 +- tests/test_smart_ptr.cpp | 4 ++-- tests/test_stl.cpp | 6 ++---- 16 files changed, 73 insertions(+), 44 deletions(-) diff --git a/.clang-tidy b/.clang-tidy index 72304528a5..db5077c227 100644 --- a/.clang-tidy +++ b/.clang-tidy @@ -5,13 +5,17 @@ cppcoreguidelines-init-variables, clang-analyzer-optin.cplusplus.VirtualCall, llvm-namespace-comment, misc-misplaced-const, +misc-non-copyable-objects, misc-static-assert, misc-throw-by-value-catch-by-reference, misc-uniqueptr-reset-release, +misc-unused-parameters, modernize-avoid-bind, +modernize-make-shared, modernize-redundant-void-arg, modernize-replace-auto-ptr, modernize-replace-disallow-copy-and-assign-macro, +modernize-replace-random-shuffle, modernize-shrink-to-fit, modernize-use-auto, modernize-use-bool-literals, @@ -23,13 +27,20 @@ modernize-use-emplace, modernize-use-override, modernize-use-using, *performance*, +readability-avoid-const-params-in-decls, readability-container-size-empty, readability-else-after-return, +readability-delete-null-pointer, +readability-implicit-bool-conversion, readability-make-member-function-const, +readability-misplaced-array-index, +readability-non-const-parameter, readability-redundant-function-ptr-dereference, readability-redundant-smartptr-get, readability-redundant-string-cstr, readability-simplify-subscript-expr, +readability-static-accessed-through-instance, +readability-static-definition-in-anonymous-namespace, readability-string-compare, readability-uniqueptr-delete-release, ' @@ -39,6 +50,8 @@ CheckOptions: value: true - key: performance-unnecessary-value-param.AllowedTypes value: 'exception_ptr$;' +- key: readability-implicit-bool-conversion.AllowPointerConditions + value: true HeaderFilterRegex: 'pybind11/.*h' diff --git a/include/pybind11/cast.h b/include/pybind11/cast.h index 1da432358f..898047b304 100644 --- a/include/pybind11/cast.h +++ b/include/pybind11/cast.h @@ -181,7 +181,7 @@ struct type_caster::value && !is_std_char_t // Signed/unsigned checks happen elsewhere if (py_err || (std::is_integral::value && sizeof(py_type) != sizeof(T) && py_value != (py_type) (T) py_value)) { PyErr_Clear(); - if (py_err && convert && PyNumber_Check(src.ptr())) { + if (py_err && convert && (PyNumber_Check(src.ptr()) != 0)) { auto tmp = reinterpret_steal(std::is_floating_point::value ? PyNumber_Float(src.ptr()) : PyNumber_Long(src.ptr())); @@ -300,7 +300,7 @@ template <> class type_caster { value = false; return true; } - if (convert || !std::strcmp("numpy.bool_", Py_TYPE(src.ptr())->tp_name)) { + if (convert || (std::strcmp("numpy.bool_", Py_TYPE(src.ptr())->tp_name) == 0)) { // (allow non-implicit conversion for numpy booleans) Py_ssize_t res = -1; @@ -501,10 +501,14 @@ template struct type_caster 1 && str_len <= 4) { auto v0 = static_cast(value[0]); - size_t char0_bytes = !(v0 & 0x80) ? 1 : // low bits only: 0-127 - (v0 & 0xE0) == 0xC0 ? 2 : // 0b110xxxxx - start of 2-byte sequence - (v0 & 0xF0) == 0xE0 ? 3 : // 0b1110xxxx - start of 3-byte sequence - 4; // 0b11110xxx - start of 4-byte sequence + // low bits only: 0-127 + // 0b110xxxxx - start of 2-byte sequence + // 0b1110xxxx - start of 3-byte sequence + // 0b11110xxx - start of 4-byte sequence + size_t char0_bytes = (v0 & 0x80) == 0 ? 1 + : (v0 & 0xE0) == 0xC0 ? 2 + : (v0 & 0xF0) == 0xE0 ? 3 + : 4; if (char0_bytes == str_len) { // If we have a 128-255 value, we can decode it into a single char: diff --git a/include/pybind11/detail/class.h b/include/pybind11/detail/class.h index 5fee3318b0..4951830684 100644 --- a/include/pybind11/detail/class.h +++ b/include/pybind11/detail/class.h @@ -129,8 +129,9 @@ extern "C" inline int pybind11_meta_setattro(PyObject* obj, PyObject* name, PyOb // 2. `Type.static_prop = other_static_prop` --> setattro: replace existing `static_prop` // 3. `Type.regular_attribute = value` --> setattro: regular attribute assignment const auto static_prop = (PyObject *) get_internals().static_property_type; - const auto call_descr_set = descr && value && PyObject_IsInstance(descr, static_prop) - && !PyObject_IsInstance(value, static_prop); + const auto call_descr_set = (descr != nullptr) && (value != nullptr) + && (PyObject_IsInstance(descr, static_prop) != 0) + && (PyObject_IsInstance(value, static_prop) == 0); if (call_descr_set) { // Call `static_property.__set__()` instead of replacing the `static_property`. #if !defined(PYPY_VERSION) @@ -562,7 +563,7 @@ extern "C" inline int pybind11_getbuffer(PyObject *obj, Py_buffer *view, int fla view->len = view->itemsize; for (auto s : info->shape) view->len *= s; - view->readonly = info->readonly; + view->readonly = static_cast(info->readonly); if ((flags & PyBUF_FORMAT) == PyBUF_FORMAT) view->format = const_cast(info->format.c_str()); if ((flags & PyBUF_STRIDES) == PyBUF_STRIDES) { diff --git a/include/pybind11/detail/internals.h b/include/pybind11/detail/internals.h index 5578c65160..273a0dbaf6 100644 --- a/include/pybind11/detail/internals.h +++ b/include/pybind11/detail/internals.h @@ -293,7 +293,7 @@ PYBIND11_NOINLINE inline internals &get_internals() { PyThreadState *tstate = PyThreadState_Get(); #if PY_VERSION_HEX >= 0x03070000 internals_ptr->tstate = PyThread_tss_alloc(); - if (!internals_ptr->tstate || PyThread_tss_create(internals_ptr->tstate)) + if (!internals_ptr->tstate || (PyThread_tss_create(internals_ptr->tstate) != 0)) pybind11_fail("get_internals: could not successfully initialize the TSS key!"); PyThread_tss_set(internals_ptr->tstate, tstate); #else diff --git a/include/pybind11/detail/type_caster_base.h b/include/pybind11/detail/type_caster_base.h index 39d0740ff8..7a74133f6e 100644 --- a/include/pybind11/detail/type_caster_base.h +++ b/include/pybind11/detail/type_caster_base.h @@ -238,8 +238,8 @@ struct value_and_holder { } bool holder_constructed() const { return inst->simple_layout - ? inst->simple_holder_constructed - : inst->nonsimple.status[index] & instance::status_holder_constructed; + ? inst->simple_holder_constructed + : (inst->nonsimple.status[index] & instance::status_holder_constructed) != 0u; } void set_holder_constructed(bool v = true) const { if (inst->simple_layout) diff --git a/include/pybind11/embed.h b/include/pybind11/embed.h index 204aaf989f..abb1cd3cca 100644 --- a/include/pybind11/embed.h +++ b/include/pybind11/embed.h @@ -76,7 +76,7 @@ struct embedded_module { using init_t = void (*)(); #endif embedded_module(const char *name, init_t init) { - if (Py_IsInitialized()) + if (Py_IsInitialized() != 0) pybind11_fail("Can't add new modules after the interpreter has been initialized"); auto result = PyImport_AppendInittab(name, init); @@ -101,7 +101,7 @@ PYBIND11_NAMESPACE_END(detail) .. _Python documentation: https://docs.python.org/3/c-api/init.html#c.Py_InitializeEx \endrst */ inline void initialize_interpreter(bool init_signal_handlers = true) { - if (Py_IsInitialized()) + if (Py_IsInitialized() != 0) pybind11_fail("The interpreter is already running"); Py_InitializeEx(init_signal_handlers ? 1 : 0); diff --git a/include/pybind11/numpy.h b/include/pybind11/numpy.h index edba8bac92..7313897fe1 100644 --- a/include/pybind11/numpy.h +++ b/include/pybind11/numpy.h @@ -465,7 +465,9 @@ class dtype : public object { explicit dtype(const buffer_info &info) { dtype descr(_dtype_from_pep3118()(PYBIND11_STR_TYPE(info.format))); // If info.itemsize == 0, use the value calculated from the format string - m_ptr = descr.strip_padding(info.itemsize ? info.itemsize : descr.itemsize()).release().ptr(); + m_ptr = descr.strip_padding(info.itemsize != 0 ? info.itemsize : descr.itemsize()) + .release() + .ptr(); } explicit dtype(const std::string &format) { @@ -486,7 +488,7 @@ class dtype : public object { /// This is essentially the same as calling numpy.dtype(args) in Python. static dtype from_args(object args) { PyObject *ptr = nullptr; - if (!detail::npy_api::get().PyArray_DescrConverter_(args.ptr(), &ptr) || !ptr) + if ((detail::npy_api::get().PyArray_DescrConverter_(args.ptr(), &ptr) == 0) || !ptr) throw error_already_set(); return reinterpret_steal(ptr); } @@ -542,7 +544,7 @@ class dtype : public object { auto name = spec[0].cast(); auto format = spec[1].cast()[0].cast(); auto offset = spec[1].cast()[1].cast(); - if (!len(name) && format.kind() == 'V') + if ((len(name) == 0u) && format.kind() == 'V') continue; field_descriptors.push_back({(PYBIND11_STR_TYPE) name, format.strip_padding(format.itemsize()), offset}); } @@ -872,11 +874,12 @@ template class array_t : public : array(std::move(shape), std::move(strides), ptr, base) { } explicit array_t(ShapeContainer shape, const T *ptr = nullptr, handle base = handle()) - : array_t(private_ctor{}, std::move(shape), - ExtraFlags & f_style - ? detail::f_strides(*shape, itemsize()) - : detail::c_strides(*shape, itemsize()), - ptr, base) { } + : array_t(private_ctor{}, + std::move(shape), + (ExtraFlags & f_style) != 0 ? detail::f_strides(*shape, itemsize()) + : detail::c_strides(*shape, itemsize()), + ptr, + base) {} explicit array_t(ssize_t count, const T *ptr = nullptr, handle base = handle()) : array({count}, {}, ptr, base) { } diff --git a/include/pybind11/pybind11.h b/include/pybind11/pybind11.h index 6a2bd0f24d..679c4beb51 100644 --- a/include/pybind11/pybind11.h +++ b/include/pybind11/pybind11.h @@ -296,7 +296,8 @@ class cpp_function : public function { a.descr = guarded_strdup(repr(a.value).cast().c_str()); } - rec->is_constructor = !strcmp(rec->name, "__init__") || !strcmp(rec->name, "__setstate__"); + rec->is_constructor + = (strcmp(rec->name, "__init__") == 0) || (strcmp(rec->name, "__setstate__") == 0); #if !defined(NDEBUG) && !defined(PYBIND11_DISABLE_NEW_STYLE_INIT_WARNING) if (rec->is_constructor && !rec->is_new_style_constructor) { @@ -1105,7 +1106,8 @@ class generic_type : public object { pybind11_fail("generic_type: cannot initialize type \"" + std::string(rec.name) + "\": an object with that name is already defined"); - if (rec.module_local ? get_local_type_info(*rec.type) : get_global_type_info(*rec.type)) + if ((rec.module_local ? get_local_type_info(*rec.type) : get_global_type_info(*rec.type)) + != nullptr) pybind11_fail("generic_type: type \"" + std::string(rec.name) + "\" is already registered!"); @@ -1183,8 +1185,9 @@ class generic_type : public object { void def_property_static_impl(const char *name, handle fget, handle fset, detail::function_record *rec_func) { - const auto is_static = rec_func && !(rec_func->is_method && rec_func->scope); - const auto has_doc = rec_func && rec_func->doc && pybind11::options::show_user_defined_docstrings(); + const auto is_static = (rec_func != nullptr) && !(rec_func->is_method && rec_func->scope); + const auto has_doc = (rec_func != nullptr) && (rec_func->doc != nullptr) + && pybind11::options::show_user_defined_docstrings(); auto property = handle((PyObject *) (is_static ? get_internals().static_property_type : &PyProperty_Type)); attr(name) = property(fget.ptr() ? fget : none(), @@ -2154,8 +2157,8 @@ inline function get_type_override(const void *this_ptr, const type_info *this_ty Unfortunately this doesn't work on PyPy. */ #if !defined(PYPY_VERSION) PyFrameObject *frame = PyThreadState_Get()->frame; - if (frame && (std::string) str(frame->f_code->co_name) == name && - frame->f_code->co_argcount > 0) { + if (frame != nullptr && (std::string) str(frame->f_code->co_name) == name + && frame->f_code->co_argcount > 0) { PyFrame_FastToLocals(frame); PyObject *self_caller = dict_getitem( frame->f_locals, PyTuple_GET_ITEM(frame->f_code->co_varnames, 0)); diff --git a/include/pybind11/pytypes.h b/include/pybind11/pytypes.h index cea4e7eb01..161aed06f4 100644 --- a/include/pybind11/pytypes.h +++ b/include/pybind11/pytypes.h @@ -773,7 +773,11 @@ class dict_readonly { dict_readonly(handle obj, ssize_t pos) : obj(obj), pos(pos) { increment(); } reference dereference() const { return {key, value}; } - void increment() { if (!PyDict_Next(obj.ptr(), &pos, &key, &value)) { pos = -1; } } + void increment() { + if (PyDict_Next(obj.ptr(), &pos, &key, &value) == 0) { + pos = -1; + } + } bool equal(const dict_readonly &b) const { return pos == b.pos; } private: @@ -1169,14 +1173,14 @@ class bool_ : public object { bool_() : object(Py_False, borrowed_t{}) { } // Allow implicit conversion from and to `bool`: bool_(bool value) : object(value ? Py_True : Py_False, borrowed_t{}) { } - operator bool() const { return m_ptr && PyLong_AsLong(m_ptr) != 0; } + operator bool() const { return (m_ptr != nullptr) && PyLong_AsLong(m_ptr) != 0; } private: /// Return the truth value of an object -- always returns a new reference static PyObject *raw_bool(PyObject *op) { const auto value = PyObject_IsTrue(op); if (value == -1) return nullptr; - return handle(value ? Py_True : Py_False).inc_ref().ptr(); + return handle(value != 0 ? Py_True : Py_False).inc_ref().ptr(); } }; @@ -1607,7 +1611,7 @@ inline memoryview memoryview::from_buffer( size_t ndim = shape->size(); if (ndim != strides->size()) pybind11_fail("memoryview: shape length doesn't match strides length"); - ssize_t size = ndim ? 1 : 0; + ssize_t size = ndim != 0u ? 1 : 0; for (size_t i = 0; i < ndim; ++i) size *= (*shape)[i]; Py_buffer view; diff --git a/include/pybind11/stl/filesystem.h b/include/pybind11/stl/filesystem.h index 7a8acdb60b..431b94b4f7 100644 --- a/include/pybind11/stl/filesystem.h +++ b/include/pybind11/stl/filesystem.h @@ -67,7 +67,7 @@ template struct path_caster { } PyObject* native = nullptr; if constexpr (std::is_same_v) { - if (PyUnicode_FSConverter(buf, &native)) { + if (PyUnicode_FSConverter(buf, &native) != 0) { if (auto c_str = PyBytes_AsString(native)) { // AsString returns a pointer to the internal buffer, which // must not be free'd. @@ -75,7 +75,7 @@ template struct path_caster { } } } else if constexpr (std::is_same_v) { - if (PyUnicode_FSDecoder(buf, &native)) { + if (PyUnicode_FSDecoder(buf, &native) != 0) { if (auto c_str = PyUnicode_AsWideCharString(native, nullptr)) { // AsWideCharString returns a new string that must be free'd. value = c_str; // Copies the string. diff --git a/tests/test_embed/test_interpreter.cpp b/tests/test_embed/test_interpreter.cpp index 6925ee9782..cd50e952f6 100644 --- a/tests/test_embed/test_interpreter.cpp +++ b/tests/test_embed/test_interpreter.cpp @@ -103,7 +103,7 @@ bool has_pybind11_internals_builtin() { bool has_pybind11_internals_static() { auto **&ipp = py::detail::get_internals_pp(); - return ipp && *ipp; + return (ipp != nullptr) && (*ipp != nullptr); } TEST_CASE("Restart the interpreter") { diff --git a/tests/test_methods_and_attributes.cpp b/tests/test_methods_and_attributes.cpp index 9db31a0b82..4cf6f08b85 100644 --- a/tests/test_methods_and_attributes.cpp +++ b/tests/test_methods_and_attributes.cpp @@ -43,6 +43,7 @@ class ExampleMandA { void add6(int other) { value += other; } // passing by value void add7(int &other) { value += other; } // passing by reference void add8(const int &other) { value += other; } // passing by const reference + // NOLINTNEXTLINE(readability-non-const-parameter) Deliberately non-const for testing void add9(int *other) { value += *other; } // passing by pointer void add10(const int *other) { value += *other; } // passing by const pointer diff --git a/tests/test_numpy_dtypes.cpp b/tests/test_numpy_dtypes.cpp index edf9cf200f..340c972db2 100644 --- a/tests/test_numpy_dtypes.cpp +++ b/tests/test_numpy_dtypes.cpp @@ -108,9 +108,11 @@ PYBIND11_PACKED(struct EnumStruct { std::ostream& operator<<(std::ostream& os, const StringStruct& v) { os << "a='"; - for (size_t i = 0; i < 3 && v.a[i]; i++) os << v.a[i]; + for (size_t i = 0; i < 3 && (v.a[i] != 0); i++) + os << v.a[i]; os << "',b='"; - for (size_t i = 0; i < 3 && v.b[i]; i++) os << v.b[i]; + for (size_t i = 0; i < 3 && (v.b[i] != 0); i++) + os << v.b[i]; return os << "'"; } diff --git a/tests/test_numpy_vectorize.cpp b/tests/test_numpy_vectorize.cpp index ed08a42bef..b08a9f7edd 100644 --- a/tests/test_numpy_vectorize.cpp +++ b/tests/test_numpy_vectorize.cpp @@ -59,7 +59,7 @@ TEST_SUBMODULE(numpy_vectorize, m) { .def(py::init()) .def_readwrite("value", &NonPODClass::value); m.def("vec_passthrough", - py::vectorize([](double *a, + py::vectorize([](const double *a, double b, // Changing this broke things // NOLINTNEXTLINE(performance-unnecessary-value-param) diff --git a/tests/test_smart_ptr.cpp b/tests/test_smart_ptr.cpp index 7fd5a9b36e..57b2d894e2 100644 --- a/tests/test_smart_ptr.cpp +++ b/tests/test_smart_ptr.cpp @@ -101,7 +101,7 @@ class MyObject3 : public std::enable_shared_from_this { // test_unique_nodelete // Object with a private destructor class MyObject4; -static std::unordered_set myobject4_instances; +std::unordered_set myobject4_instances; class MyObject4 { public: MyObject4(int value) : value{value} { @@ -127,7 +127,7 @@ class MyObject4 { // Object with std::unique_ptr where D is not matching the base class // Object with a protected destructor class MyObject4a; -static std::unordered_set myobject4a_instances; +std::unordered_set myobject4a_instances; class MyObject4a { public: MyObject4a(int i) { diff --git a/tests/test_stl.cpp b/tests/test_stl.cpp index eb8cdab7b5..23e2c07b32 100644 --- a/tests/test_stl.cpp +++ b/tests/test_stl.cpp @@ -102,7 +102,7 @@ TEST_SUBMODULE(stl, m) { // test_set m.def("cast_set", []() { return std::set{"key1", "key2"}; }); m.def("load_set", [](const std::set &set) { - return set.count("key1") && set.count("key2") && set.count("key3"); + return (set.count("key1") != 0u) && (set.count("key2") != 0u) && (set.count("key3") != 0u); }); // test_recursive_casting @@ -196,9 +196,7 @@ TEST_SUBMODULE(stl, m) { m.def("double_or_zero", [](const opt_int& x) -> int { return x.value_or(0) * 2; }); - m.def("half_or_none", [](int x) -> opt_int { - return x ? opt_int(x / 2) : opt_int(); - }); + m.def("half_or_none", [](int x) -> opt_int { return x != 0 ? opt_int(x / 2) : opt_int(); }); m.def("test_nullopt", [](opt_int x) { return x.value_or(42); }, py::arg_v("x", std::nullopt, "None")); From ed5fb66bd792fb709c0f4259b60b42b7d7101559 Mon Sep 17 00:00:00 2001 From: "Ralf W. Grosse-Kunstleve" Date: Tue, 27 Jul 2021 15:33:31 -0700 Subject: [PATCH 106/638] Removing __INTEL_COMPILER section from pragma block at the top of pybind11.h (#3135) * Fixing `pragma warning pop` for `__INTEL_COMPILER`. * Adding push/pop to 3 tests. Removing #878 from top of pybind11.h (it was/is only needed for 1 test). * Trying again after CI failure, moving the push to the top of 2 tests. * Trying more after CI failure, adding push/pop to pybind11_tests.h, constructor_stats.h. * Moving ICC #2196 suppression to CMakeLists.txt * Fixing condition for `pragma GCC diagnostic push` in pybind11.h * Moving `pragma warning disable 2196` to common.h * Revising #ifdef to be more conservative. * Undoing insertion of notes that will hopefully soon be completely obsolete anyway. --- include/pybind11/detail/common.h | 3 +++ include/pybind11/pybind11.h | 8 ++------ tests/test_constants_and_functions.cpp | 7 +++++++ 3 files changed, 12 insertions(+), 6 deletions(-) diff --git a/include/pybind11/detail/common.h b/include/pybind11/detail/common.h index 3faf3ea327..0b4e30c18b 100644 --- a/include/pybind11/detail/common.h +++ b/include/pybind11/detail/common.h @@ -56,6 +56,9 @@ # elif __INTEL_COMPILER < 1900 && defined(PYBIND11_CPP14) # error pybind11 supports only C++11 with Intel C++ compiler v18. Use v19 or newer for C++14. # endif +/* The following pragma cannot be pop'ed: + https://community.intel.com/t5/Intel-C-Compiler/Inline-and-no-inline-warning/td-p/1216764 */ +# pragma warning disable 2196 // warning #2196: routine is both "inline" and "noinline" #elif defined(__clang__) && !defined(__apple_build_version__) # if __clang_major__ < 3 || (__clang_major__ == 3 && __clang_minor__ < 3) # error pybind11 requires clang 3.3 or newer diff --git a/include/pybind11/pybind11.h b/include/pybind11/pybind11.h index 679c4beb51..f4aa9b8814 100644 --- a/include/pybind11/pybind11.h +++ b/include/pybind11/pybind11.h @@ -10,16 +10,12 @@ #pragma once -#if defined(__INTEL_COMPILER) -# pragma warning push -# pragma warning disable 878 // incompatible exception specifications -# pragma warning disable 2196 // warning #2196: routine is both "inline" and "noinline" -#elif defined(_MSC_VER) +#if defined(_MSC_VER) && !defined(__INTEL_COMPILER) # pragma warning(push) # pragma warning(disable: 4100) // warning C4100: Unreferenced formal parameter # pragma warning(disable: 4127) // warning C4127: Conditional expression is constant # pragma warning(disable: 4505) // warning C4505: 'PySlice_GetIndicesEx': unreferenced local function has been removed (PyPy only) -#elif defined(__GNUG__) && !defined(__clang__) +#elif defined(__GNUG__) && !defined(__clang__) && !defined(__INTEL_COMPILER) # pragma GCC diagnostic push # pragma GCC diagnostic ignored "-Wunused-but-set-parameter" # pragma GCC diagnostic ignored "-Wattributes" diff --git a/tests/test_constants_and_functions.cpp b/tests/test_constants_and_functions.cpp index e7f018540c..c0554503fa 100644 --- a/tests/test_constants_and_functions.cpp +++ b/tests/test_constants_and_functions.cpp @@ -133,7 +133,14 @@ TEST_SUBMODULE(constants_and_functions, m) { ; m.def("f1", f1); m.def("f2", f2); +#if defined(__INTEL_COMPILER) +# pragma warning push +# pragma warning disable 878 // incompatible exception specifications +#endif m.def("f3", f3); +#if defined(__INTEL_COMPILER) +# pragma warning pop +#endif m.def("f4", f4); // test_function_record_leaks From 05852fb6bc2ba9bd89de671faf3cee072e1f7dbd Mon Sep 17 00:00:00 2001 From: "Ralf W. Grosse-Kunstleve" Date: Wed, 28 Jul 2021 08:58:36 -0700 Subject: [PATCH 107/638] Accommodating environments that define __STDC_WANT_LIB_EXT1__ even if __STDC_LIB_EXT1__ is not defined by the implementation. (#3151) Follow-on to PR #3129. --- include/pybind11/chrono.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/pybind11/chrono.h b/include/pybind11/chrono.h index c4e81f5720..61bbcbc540 100644 --- a/include/pybind11/chrono.h +++ b/include/pybind11/chrono.h @@ -101,7 +101,7 @@ template class duration_caster { }; inline std::tm *localtime_thread_safe(const std::time_t *time, std::tm *buf) { -#if defined(__STDC_WANT_LIB_EXT1__) || defined(_MSC_VER) +#if (defined(__STDC_LIB_EXT1__) && defined(__STDC_WANT_LIB_EXT1__)) || defined(_MSC_VER) if (localtime_s(buf, time)) return nullptr; return buf; From b1fdbe6954be5afa0a9003c37731da60ac68baff Mon Sep 17 00:00:00 2001 From: Henry Schreiner Date: Thu, 29 Jul 2021 16:10:18 -0700 Subject: [PATCH 108/638] chore: add discussions link (#3159) --- .github/ISSUE_TEMPLATE/bug-report.md | 28 -------------- .github/ISSUE_TEMPLATE/bug-report.yml | 45 +++++++++++++++++++++++ .github/ISSUE_TEMPLATE/config.yml | 3 ++ .github/ISSUE_TEMPLATE/feature-request.md | 16 -------- .github/ISSUE_TEMPLATE/question.md | 21 ----------- README.rst | 4 +- 6 files changed, 51 insertions(+), 66 deletions(-) delete mode 100644 .github/ISSUE_TEMPLATE/bug-report.md create mode 100644 .github/ISSUE_TEMPLATE/bug-report.yml delete mode 100644 .github/ISSUE_TEMPLATE/feature-request.md delete mode 100644 .github/ISSUE_TEMPLATE/question.md diff --git a/.github/ISSUE_TEMPLATE/bug-report.md b/.github/ISSUE_TEMPLATE/bug-report.md deleted file mode 100644 index ae36ea6508..0000000000 --- a/.github/ISSUE_TEMPLATE/bug-report.md +++ /dev/null @@ -1,28 +0,0 @@ ---- -name: Bug Report -about: File an issue about a bug -title: "[BUG] " ---- - - -Make sure you've completed the following steps before submitting your issue -- thank you! - -1. Make sure you've read the [documentation][]. Your issue may be addressed there. -2. Search the [issue tracker][] to verify that this hasn't already been reported. +1 or comment there if it has. -3. Consider asking first in the [Gitter chat room][]. -4. Include a self-contained and minimal piece of code that reproduces the problem. If that's not possible, try to make the description as clear as possible. - a. If possible, make a PR with a new, failing test to give us a starting point to work on! - -[documentation]: https://pybind11.readthedocs.io -[issue tracker]: https://github.com/pybind/pybind11/issues -[Gitter chat room]: https://gitter.im/pybind/Lobby - -*After reading, remove this checklist and the template text in parentheses below.* - -## Issue description - -(Provide a short description, state the expected behavior and what actually happens.) - -## Reproducible example code - -(The code should be minimal, have no external dependencies, isolate the function(s) that cause breakage. Submit matched and complete C++ and Python snippets that can be easily compiled and run to diagnose the issue.) diff --git a/.github/ISSUE_TEMPLATE/bug-report.yml b/.github/ISSUE_TEMPLATE/bug-report.yml new file mode 100644 index 0000000000..bd6a9a8e22 --- /dev/null +++ b/.github/ISSUE_TEMPLATE/bug-report.yml @@ -0,0 +1,45 @@ +name: Bug Report +description: File an issue about a bug +title: "[BUG]: " +labels: [triage] +body: + - type: markdown + attributes: + value: | + Maintainers will only make a best effort to triage PRs. Please do your best to make the issue as easy to act on as possible, and only open if clearly a problem with pybind11 (ask first if unsure). + - type: checkboxes + id: steps + attributes: + label: Required prerequisites + description: Make sure you've completed the following steps before submitting your issue -- thank you! + options: + - label: Make sure you've read the [documentation](https://pybind11.readthedocs.io). Your issue may be addressed there. + required: true + - label: Search the [issue tracker](https://github.com/pybind/pybind11/issues) and [Discussions](https:/pybind/pybind11/discussions) to verify that this hasn't already been reported. +1 or comment there if it has. + required: true + - label: Consider asking first in the [Gitter chat room](https://gitter.im/pybind/Lobby) or in a [Discussion](https:/pybind/pybind11/discussions/new). + required: false + + - type: textarea + id: description + attributes: + label: Problem description + placeholder: >- + Provide a short description, state the expected behavior and what + actually happens. Include relevant information like what version of + pybind11 you are using, what system you are on, and any useful commands + / output. + validations: + required: true + + - type: textarea + id: code + attributes: + label: Reproducible example code + placeholder: >- + The code should be minimal, have no external dependencies, isolate the + function(s) that cause breakage. Submit matched and complete C++ and + Python snippets that can be easily compiled and run to diagnose the + issue. If possible, make a PR with a new, failing test to give us a + starting point to work on! + render: text diff --git a/.github/ISSUE_TEMPLATE/config.yml b/.github/ISSUE_TEMPLATE/config.yml index 20e743136f..27f9a80441 100644 --- a/.github/ISSUE_TEMPLATE/config.yml +++ b/.github/ISSUE_TEMPLATE/config.yml @@ -1,5 +1,8 @@ blank_issues_enabled: false contact_links: + - name: Ask a question + url: https://github.com/pybind/pybind11/discussions/new + about: Please ask and answer questions here, or propose new ideas. - name: Gitter room url: https://gitter.im/pybind/Lobby about: A room for discussing pybind11 with an active community diff --git a/.github/ISSUE_TEMPLATE/feature-request.md b/.github/ISSUE_TEMPLATE/feature-request.md deleted file mode 100644 index 5f6ec81ec9..0000000000 --- a/.github/ISSUE_TEMPLATE/feature-request.md +++ /dev/null @@ -1,16 +0,0 @@ ---- -name: Feature Request -about: File an issue about adding a feature -title: "[FEAT] " ---- - - -Make sure you've completed the following steps before submitting your issue -- thank you! - -1. Check if your feature has already been mentioned / rejected / planned in other issues. -2. If those resources didn't help, consider asking in the [Gitter chat room][] to see if this is interesting / useful to a larger audience and possible to implement reasonably, -4. If you have a useful feature that passes the previous items (or not suitable for chat), please fill in the details below. - -[Gitter chat room]: https://gitter.im/pybind/Lobby - -*After reading, remove this checklist.* diff --git a/.github/ISSUE_TEMPLATE/question.md b/.github/ISSUE_TEMPLATE/question.md deleted file mode 100644 index b199b6ee8a..0000000000 --- a/.github/ISSUE_TEMPLATE/question.md +++ /dev/null @@ -1,21 +0,0 @@ ---- -name: Question -about: File an issue about unexplained behavior -title: "[QUESTION] " ---- - -If you have a question, please check the following first: - -1. Check if your question has already been answered in the [FAQ][] section. -2. Make sure you've read the [documentation][]. Your issue may be addressed there. -3. If those resources didn't help and you only have a short question (not a bug report), consider asking in the [Gitter chat room][] -4. Search the [issue tracker][], including the closed issues, to see if your question has already been asked/answered. +1 or comment if it has been asked but has no answer. -5. If you have a more complex question which is not answered in the previous items (or not suitable for chat), please fill in the details below. -6. Include a self-contained and minimal piece of code that illustrates your question. If that's not possible, try to make the description as clear as possible. - -[FAQ]: http://pybind11.readthedocs.io/en/latest/faq.html -[documentation]: https://pybind11.readthedocs.io -[issue tracker]: https://github.com/pybind/pybind11/issues -[Gitter chat room]: https://gitter.im/pybind/Lobby - -*After reading, remove this checklist.* diff --git a/README.rst b/README.rst index 69a7797e83..57eb06e557 100644 --- a/README.rst +++ b/README.rst @@ -3,7 +3,7 @@ **pybind11 — Seamless operability between C++11 and Python** -|Latest Documentation Status| |Stable Documentation Status| |Gitter chat| |CI| |Build status| +|Latest Documentation Status| |Stable Documentation Status| |Gitter chat| |GitHub Discussions| |CI| |Build status| |Repology| |PyPI package| |Conda-forge| |Python Versions| @@ -176,3 +176,5 @@ to the terms and conditions of this license. :target: https://repology.org/project/python:pybind11/versions .. |Python Versions| image:: https://img.shields.io/pypi/pyversions/pybind11.svg :target: https://pypi.org/project/pybind11/ +.. |GitHub Discussions| image:: https://img.shields.io/static/v1?label=Discussions&message=Ask&color=blue&logo=github + :target: https://github.com/pybind/pybind11/discussions From b42597291fd59b9bb3af4a9489ca14c910c347d6 Mon Sep 17 00:00:00 2001 From: "Ralf W. Grosse-Kunstleve" Date: Fri, 30 Jul 2021 07:09:55 -0700 Subject: [PATCH 109/638] Limiting pragma for ignoring GCC 7 -Wnoexcept-type to the scope of pybind11.h. (#3161) * Moving pragma for ignoring -Wnoexcept-type to the one location where it is needed. * Trying a second location. * The previous commit worked (GitHub Actions green), but see the added comment about the dicy nature of -Wnoexcept-type ("if and only if"). * Applying reviewer suggestion. --- include/pybind11/pybind11.h | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/include/pybind11/pybind11.h b/include/pybind11/pybind11.h index 139a4111e9..16615421dc 100644 --- a/include/pybind11/pybind11.h +++ b/include/pybind11/pybind11.h @@ -18,9 +18,6 @@ # pragma GCC diagnostic push # pragma GCC diagnostic ignored "-Wunused-but-set-parameter" # pragma GCC diagnostic ignored "-Wattributes" -# if __GNUC__ >= 7 -# pragma GCC diagnostic ignored "-Wnoexcept-type" -# endif #endif #include "attr.h" @@ -48,6 +45,18 @@ # include #endif +/* https://stackoverflow.com/questions/46798456/handling-gccs-noexcept-type-warning + This warning is about ABI compatibility, not code health. + It is only actually needed in a couple places, but apparently GCC 7 "generates this warning if + and only if the first template instantiation ... involves noexcept" [stackoverflow], therefore + it could get triggered from seemingly random places, depending on user code. + No other GCC version generates this warning. + */ +#if defined(__GNUC__) && __GNUC__ == 7 +# pragma GCC diagnostic push +# pragma GCC diagnostic ignored "-Wnoexcept-type" +#endif + PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE) PYBIND11_NAMESPACE_BEGIN(detail) @@ -2376,6 +2385,10 @@ inline function get_overload(const T *this_ptr, const char *name) { PYBIND11_NAMESPACE_END(PYBIND11_NAMESPACE) +#if defined(__GNUC__) && __GNUC__ == 7 +# pragma GCC diagnostic pop // -Wnoexcept-type +#endif + #if defined(_MSC_VER) && !defined(__INTEL_COMPILER) # pragma warning(pop) #elif defined(__GNUG__) && !defined(__clang__) && !defined(__INTEL_COMPILER) From 9e8a741baac7485a7f81904bbb9c06a9ae423c40 Mon Sep 17 00:00:00 2001 From: Jerome Robert Date: Fri, 30 Jul 2021 19:48:41 +0200 Subject: [PATCH 110/638] fix: Mingw64 corrected and add a CI job to test it (#3132) * mingw64 platform string is like mingw_xxx not "mingw" See https://github.com/msys2/MINGW-packages/blob/master/mingw-w64-python/0099-Change-the-get_platform-method-in-sysconfig-and-dist.patch * Mingw: Do not dllexport exceptions This is a fix for errors like: D:/a/pybind11/pybind11/include/pybind11/detail/common.h:735:23: error: 'dllexport' implies default visibility, but 'class pybind11::builtin_exception' has already been declared with a different visibility 735 | class PYBIND11_EXPORT builtin_exception : public std::runtime_error { | ^~~~~~~~~~~~~~~~~ * GHA: Test Mingw64 build * fix: avoid thin binaries on mingw * fix: drop lto on MinGW * Mingw64: disable PYBIND11_DEPRECATED It trigger many warnings for unknown reasons Co-authored-by: Henry Schreiner --- .github/workflows/ci.yml | 31 +++++++++++++++++++++++++++++++ include/pybind11/detail/common.h | 20 +++++++++++++++++--- include/pybind11/pytypes.h | 2 +- pybind11/setup_helpers.py | 3 +-- tests/test_exceptions.h | 2 +- tools/pybind11Common.cmake | 7 ++++++- 6 files changed, 57 insertions(+), 8 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 03c9225c12..73a6fd5199 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -859,3 +859,34 @@ jobs: - name: Run all checks run: cmake --build build -t check + + mingw: + runs-on: windows-latest + defaults: + run: + shell: msys2 {0} + steps: + - uses: msys2/setup-msys2@v2 + with: + install: >- + mingw-w64-x86_64-gcc + mingw-w64-x86_64-python-pip + mingw-w64-x86_64-cmake + mingw-w64-x86_64-make + mingw-w64-x86_64-python-pytest + mingw-w64-x86_64-eigen3 + mingw-w64-x86_64-boost + mingw-w64-x86_64-catch + + - uses: actions/checkout@v1 + + - name: Configure + # LTO leads to many undefined reference like + # `pybind11::detail::function_call::function_call(pybind11::detail::function_call&&) + run: cmake -G "MinGW Makefiles" -S . -B build + + - name: Build + run: cmake --build build -j 2 + + - name: Python tests + run: cmake --build build --target pytest diff --git a/include/pybind11/detail/common.h b/include/pybind11/detail/common.h index 0add6272f5..d63e3e9c78 100644 --- a/include/pybind11/detail/common.h +++ b/include/pybind11/detail/common.h @@ -89,13 +89,27 @@ # endif #endif +#if !defined(PYBIND11_EXPORT_EXCEPTION) +# ifdef __MINGW32__ +// workaround for: +// error: 'dllexport' implies default visibility, but xxx has already been declared with a different visibility +# define PYBIND11_EXPORT_EXCEPTION +# else +# define PYBIND11_EXPORT_EXCEPTION PYBIND11_EXPORT +# endif +#endif + #if defined(_MSC_VER) # define PYBIND11_NOINLINE __declspec(noinline) #else # define PYBIND11_NOINLINE __attribute__ ((noinline)) #endif -#if defined(PYBIND11_CPP14) +#if defined(__MINGW32__) +// For unknown reasons all PYBIND11_DEPRECATED member trigger a warning when declared +// whether it is used or not +# define PYBIND11_DEPRECATED(reason) +#elif defined(PYBIND11_CPP14) # define PYBIND11_DEPRECATED(reason) [[deprecated(reason)]] #else # define PYBIND11_DEPRECATED(reason) __attribute__((deprecated(reason))) @@ -740,7 +754,7 @@ PYBIND11_NAMESPACE_END(detail) # pragma warning(disable: 4275) // warning C4275: An exported class was derived from a class that wasn't exported. Can be ignored when derived from a STL class. #endif /// C++ bindings of builtin Python exceptions -class PYBIND11_EXPORT builtin_exception : public std::runtime_error { +class PYBIND11_EXPORT_EXCEPTION builtin_exception : public std::runtime_error { public: using std::runtime_error::runtime_error; /// Set the error using the Python C API @@ -751,7 +765,7 @@ class PYBIND11_EXPORT builtin_exception : public std::runtime_error { #endif #define PYBIND11_RUNTIME_EXCEPTION(name, type) \ - class PYBIND11_EXPORT name : public builtin_exception { public: \ + class PYBIND11_EXPORT_EXCEPTION name : public builtin_exception { public: \ using builtin_exception::builtin_exception; \ name() : name("") { } \ void set_error() const override { PyErr_SetString(type, what()); } \ diff --git a/include/pybind11/pytypes.h b/include/pybind11/pytypes.h index 161aed06f4..4cf606e8d0 100644 --- a/include/pybind11/pytypes.h +++ b/include/pybind11/pytypes.h @@ -327,7 +327,7 @@ PYBIND11_NAMESPACE_END(detail) /// thrown to propagate python-side errors back through C++ which can either be caught manually or /// else falls back to the function dispatcher (which then raises the captured error back to /// python). -class PYBIND11_EXPORT error_already_set : public std::runtime_error { +class PYBIND11_EXPORT_EXCEPTION error_already_set : public std::runtime_error { public: /// Constructs a new exception from the current Python error indicator, if any. The current /// Python error indicator will be cleared. diff --git a/pybind11/setup_helpers.py b/pybind11/setup_helpers.py index 2b27f1f5ad..0888ab4872 100644 --- a/pybind11/setup_helpers.py +++ b/pybind11/setup_helpers.py @@ -59,8 +59,7 @@ import distutils.errors import distutils.ccompiler - -WIN = sys.platform.startswith("win32") and sysconfig.get_platform() != "mingw" +WIN = sys.platform.startswith("win32") and "mingw" not in sysconfig.get_platform() PY2 = sys.version_info[0] < 3 MACOS = sys.platform.startswith("darwin") STD_TMPL = "/std:c++{}" if WIN else "-std=c++{}" diff --git a/tests/test_exceptions.h b/tests/test_exceptions.h index 5d02d1b35b..9d428312eb 100644 --- a/tests/test_exceptions.h +++ b/tests/test_exceptions.h @@ -4,7 +4,7 @@ // shared exceptions for cross_module_tests -class PYBIND11_EXPORT shared_exception : public pybind11::builtin_exception { +class PYBIND11_EXPORT_EXCEPTION shared_exception : public pybind11::builtin_exception { public: using builtin_exception::builtin_exception; explicit shared_exception() : shared_exception("") {} diff --git a/tools/pybind11Common.cmake b/tools/pybind11Common.cmake index 57e42536e1..7afb0d0b1a 100644 --- a/tools/pybind11Common.cmake +++ b/tools/pybind11Common.cmake @@ -302,13 +302,18 @@ function(_pybind11_return_if_cxx_and_linker_flags_work result cxxflags linkerfla endfunction() function(_pybind11_generate_lto target prefer_thin_lto) + if(MINGW) + message(STATUS "${target} disabled (problems with undefined symbols for MinGW for now)") + return() + endif() + if(CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang") set(cxx_append "") set(linker_append "") if(CMAKE_CXX_COMPILER_ID MATCHES "Clang" AND NOT APPLE) # Clang Gold plugin does not support -Os; append -O3 to MinSizeRel builds to override it set(linker_append ";$<$:-O3>") - elseif(CMAKE_CXX_COMPILER_ID MATCHES "GNU") + elseif(CMAKE_CXX_COMPILER_ID MATCHES "GNU" AND NOT MINGW) set(cxx_append ";-fno-fat-lto-objects") endif() From e2573dc961ec11cd148c50c06abfaee0a6930694 Mon Sep 17 00:00:00 2001 From: "Ralf W. Grosse-Kunstleve" Date: Fri, 30 Jul 2021 10:51:50 -0700 Subject: [PATCH 111/638] Moving pragma for MSVC warning C4505 from pybind11.h to existing list in detail/common.h (#3160) * Moving pragma for C4505 from pybind11.h to existing list in detail/common.h. * Removing 4 existing suppressions to 1. see what is still needed and 2. capture the MSVC messages. * It turns out none of the 4 pragmas are needed anymore. --- include/pybind11/detail/common.h | 3 ++- include/pybind11/pybind11.h | 1 - 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/include/pybind11/detail/common.h b/include/pybind11/detail/common.h index d63e3e9c78..d1e9e6591f 100644 --- a/include/pybind11/detail/common.h +++ b/include/pybind11/detail/common.h @@ -135,7 +135,8 @@ # define HAVE_ROUND 1 # endif # pragma warning(push) -# pragma warning(disable: 4510 4610 4512 4005) +// C4505: 'PySlice_GetIndicesEx': unreferenced local function has been removed (PyPy only) +# pragma warning(disable: 4505) # if defined(_DEBUG) && !defined(Py_DEBUG) # define PYBIND11_DEBUG_MARKER # undef _DEBUG diff --git a/include/pybind11/pybind11.h b/include/pybind11/pybind11.h index 16615421dc..ec6bd8d351 100644 --- a/include/pybind11/pybind11.h +++ b/include/pybind11/pybind11.h @@ -13,7 +13,6 @@ #if defined(_MSC_VER) && !defined(__INTEL_COMPILER) # pragma warning(push) # pragma warning(disable: 4127) // warning C4127: Conditional expression is constant -# pragma warning(disable: 4505) // warning C4505: 'PySlice_GetIndicesEx': unreferenced local function has been removed (PyPy only) #elif defined(__GNUG__) && !defined(__clang__) && !defined(__INTEL_COMPILER) # pragma GCC diagnostic push # pragma GCC diagnostic ignored "-Wunused-but-set-parameter" From 46c51fc03bfc76bfd71e9ab5036bec7efefb4b5d Mon Sep 17 00:00:00 2001 From: "Ralf W. Grosse-Kunstleve" Date: Fri, 30 Jul 2021 07:09:55 -0700 Subject: [PATCH 112/638] Limiting pragma for ignoring GCC 7 -Wnoexcept-type to the scope of pybind11.h. (#3161) * Moving pragma for ignoring -Wnoexcept-type to the one location where it is needed. * Trying a second location. * The previous commit worked (GitHub Actions green), but see the added comment about the dicy nature of -Wnoexcept-type ("if and only if"). * Applying reviewer suggestion. --- include/pybind11/pybind11.h | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/include/pybind11/pybind11.h b/include/pybind11/pybind11.h index f4aa9b8814..3eb71f386a 100644 --- a/include/pybind11/pybind11.h +++ b/include/pybind11/pybind11.h @@ -19,9 +19,6 @@ # pragma GCC diagnostic push # pragma GCC diagnostic ignored "-Wunused-but-set-parameter" # pragma GCC diagnostic ignored "-Wattributes" -# if __GNUC__ >= 7 -# pragma GCC diagnostic ignored "-Wnoexcept-type" -# endif #endif #include "attr.h" @@ -49,6 +46,18 @@ # include #endif +/* https://stackoverflow.com/questions/46798456/handling-gccs-noexcept-type-warning + This warning is about ABI compatibility, not code health. + It is only actually needed in a couple places, but apparently GCC 7 "generates this warning if + and only if the first template instantiation ... involves noexcept" [stackoverflow], therefore + it could get triggered from seemingly random places, depending on user code. + No other GCC version generates this warning. + */ +#if defined(__GNUC__) && __GNUC__ == 7 +# pragma GCC diagnostic push +# pragma GCC diagnostic ignored "-Wnoexcept-type" +#endif + PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE) #if defined(_MSC_VER) @@ -2309,6 +2318,10 @@ inline function get_overload(const T *this_ptr, const char *name) { PYBIND11_NAMESPACE_END(PYBIND11_NAMESPACE) +#if defined(__GNUC__) && __GNUC__ == 7 +# pragma GCC diagnostic pop // -Wnoexcept-type +#endif + #if defined(_MSC_VER) && !defined(__INTEL_COMPILER) # pragma warning(pop) #elif defined(__GNUG__) && !defined(__clang__) && !defined(__INTEL_COMPILER) From c80e0593454411ad38205cc76134d6a0dd70032e Mon Sep 17 00:00:00 2001 From: Jerome Robert Date: Fri, 30 Jul 2021 19:48:41 +0200 Subject: [PATCH 113/638] fix: Mingw64 corrected and add a CI job to test it (#3132) * mingw64 platform string is like mingw_xxx not "mingw" See https://github.com/msys2/MINGW-packages/blob/master/mingw-w64-python/0099-Change-the-get_platform-method-in-sysconfig-and-dist.patch * Mingw: Do not dllexport exceptions This is a fix for errors like: D:/a/pybind11/pybind11/include/pybind11/detail/common.h:735:23: error: 'dllexport' implies default visibility, but 'class pybind11::builtin_exception' has already been declared with a different visibility 735 | class PYBIND11_EXPORT builtin_exception : public std::runtime_error { | ^~~~~~~~~~~~~~~~~ * GHA: Test Mingw64 build * fix: avoid thin binaries on mingw * fix: drop lto on MinGW * Mingw64: disable PYBIND11_DEPRECATED It trigger many warnings for unknown reasons Co-authored-by: Henry Schreiner --- .github/workflows/ci.yml | 31 +++++++++++++++++++++++++++++++ include/pybind11/detail/common.h | 20 +++++++++++++++++--- include/pybind11/pytypes.h | 2 +- pybind11/setup_helpers.py | 3 +-- tests/test_exceptions.h | 2 +- tools/pybind11Common.cmake | 7 ++++++- 6 files changed, 57 insertions(+), 8 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 03c9225c12..73a6fd5199 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -859,3 +859,34 @@ jobs: - name: Run all checks run: cmake --build build -t check + + mingw: + runs-on: windows-latest + defaults: + run: + shell: msys2 {0} + steps: + - uses: msys2/setup-msys2@v2 + with: + install: >- + mingw-w64-x86_64-gcc + mingw-w64-x86_64-python-pip + mingw-w64-x86_64-cmake + mingw-w64-x86_64-make + mingw-w64-x86_64-python-pytest + mingw-w64-x86_64-eigen3 + mingw-w64-x86_64-boost + mingw-w64-x86_64-catch + + - uses: actions/checkout@v1 + + - name: Configure + # LTO leads to many undefined reference like + # `pybind11::detail::function_call::function_call(pybind11::detail::function_call&&) + run: cmake -G "MinGW Makefiles" -S . -B build + + - name: Build + run: cmake --build build -j 2 + + - name: Python tests + run: cmake --build build --target pytest diff --git a/include/pybind11/detail/common.h b/include/pybind11/detail/common.h index 0b4e30c18b..09e8717ce4 100644 --- a/include/pybind11/detail/common.h +++ b/include/pybind11/detail/common.h @@ -89,13 +89,27 @@ # endif #endif +#if !defined(PYBIND11_EXPORT_EXCEPTION) +# ifdef __MINGW32__ +// workaround for: +// error: 'dllexport' implies default visibility, but xxx has already been declared with a different visibility +# define PYBIND11_EXPORT_EXCEPTION +# else +# define PYBIND11_EXPORT_EXCEPTION PYBIND11_EXPORT +# endif +#endif + #if defined(_MSC_VER) # define PYBIND11_NOINLINE __declspec(noinline) #else # define PYBIND11_NOINLINE __attribute__ ((noinline)) #endif -#if defined(PYBIND11_CPP14) +#if defined(__MINGW32__) +// For unknown reasons all PYBIND11_DEPRECATED member trigger a warning when declared +// whether it is used or not +# define PYBIND11_DEPRECATED(reason) +#elif defined(PYBIND11_CPP14) # define PYBIND11_DEPRECATED(reason) [[deprecated(reason)]] #else # define PYBIND11_DEPRECATED(reason) __attribute__((deprecated(reason))) @@ -740,7 +754,7 @@ PYBIND11_NAMESPACE_END(detail) # pragma warning(disable: 4275) // warning C4275: An exported class was derived from a class that wasn't exported. Can be ignored when derived from a STL class. #endif /// C++ bindings of builtin Python exceptions -class PYBIND11_EXPORT builtin_exception : public std::runtime_error { +class PYBIND11_EXPORT_EXCEPTION builtin_exception : public std::runtime_error { public: using std::runtime_error::runtime_error; /// Set the error using the Python C API @@ -751,7 +765,7 @@ class PYBIND11_EXPORT builtin_exception : public std::runtime_error { #endif #define PYBIND11_RUNTIME_EXCEPTION(name, type) \ - class PYBIND11_EXPORT name : public builtin_exception { public: \ + class PYBIND11_EXPORT_EXCEPTION name : public builtin_exception { public: \ using builtin_exception::builtin_exception; \ name() : name("") { } \ void set_error() const override { PyErr_SetString(type, what()); } \ diff --git a/include/pybind11/pytypes.h b/include/pybind11/pytypes.h index 161aed06f4..4cf606e8d0 100644 --- a/include/pybind11/pytypes.h +++ b/include/pybind11/pytypes.h @@ -327,7 +327,7 @@ PYBIND11_NAMESPACE_END(detail) /// thrown to propagate python-side errors back through C++ which can either be caught manually or /// else falls back to the function dispatcher (which then raises the captured error back to /// python). -class PYBIND11_EXPORT error_already_set : public std::runtime_error { +class PYBIND11_EXPORT_EXCEPTION error_already_set : public std::runtime_error { public: /// Constructs a new exception from the current Python error indicator, if any. The current /// Python error indicator will be cleared. diff --git a/pybind11/setup_helpers.py b/pybind11/setup_helpers.py index 2b27f1f5ad..0888ab4872 100644 --- a/pybind11/setup_helpers.py +++ b/pybind11/setup_helpers.py @@ -59,8 +59,7 @@ import distutils.errors import distutils.ccompiler - -WIN = sys.platform.startswith("win32") and sysconfig.get_platform() != "mingw" +WIN = sys.platform.startswith("win32") and "mingw" not in sysconfig.get_platform() PY2 = sys.version_info[0] < 3 MACOS = sys.platform.startswith("darwin") STD_TMPL = "/std:c++{}" if WIN else "-std=c++{}" diff --git a/tests/test_exceptions.h b/tests/test_exceptions.h index 5d02d1b35b..9d428312eb 100644 --- a/tests/test_exceptions.h +++ b/tests/test_exceptions.h @@ -4,7 +4,7 @@ // shared exceptions for cross_module_tests -class PYBIND11_EXPORT shared_exception : public pybind11::builtin_exception { +class PYBIND11_EXPORT_EXCEPTION shared_exception : public pybind11::builtin_exception { public: using builtin_exception::builtin_exception; explicit shared_exception() : shared_exception("") {} diff --git a/tools/pybind11Common.cmake b/tools/pybind11Common.cmake index 57e42536e1..7afb0d0b1a 100644 --- a/tools/pybind11Common.cmake +++ b/tools/pybind11Common.cmake @@ -302,13 +302,18 @@ function(_pybind11_return_if_cxx_and_linker_flags_work result cxxflags linkerfla endfunction() function(_pybind11_generate_lto target prefer_thin_lto) + if(MINGW) + message(STATUS "${target} disabled (problems with undefined symbols for MinGW for now)") + return() + endif() + if(CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang") set(cxx_append "") set(linker_append "") if(CMAKE_CXX_COMPILER_ID MATCHES "Clang" AND NOT APPLE) # Clang Gold plugin does not support -Os; append -O3 to MinSizeRel builds to override it set(linker_append ";$<$:-O3>") - elseif(CMAKE_CXX_COMPILER_ID MATCHES "GNU") + elseif(CMAKE_CXX_COMPILER_ID MATCHES "GNU" AND NOT MINGW) set(cxx_append ";-fno-fat-lto-objects") endif() From b961ac644ff0ec22e3a6edc5925bd9b0f2eff0df Mon Sep 17 00:00:00 2001 From: "Ralf W. Grosse-Kunstleve" Date: Fri, 30 Jul 2021 10:51:50 -0700 Subject: [PATCH 114/638] Moving pragma for MSVC warning C4505 from pybind11.h to existing list in detail/common.h (#3160) * Moving pragma for C4505 from pybind11.h to existing list in detail/common.h. * Removing 4 existing suppressions to 1. see what is still needed and 2. capture the MSVC messages. * It turns out none of the 4 pragmas are needed anymore. --- include/pybind11/detail/common.h | 3 ++- include/pybind11/pybind11.h | 1 - 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/include/pybind11/detail/common.h b/include/pybind11/detail/common.h index 09e8717ce4..9df670fa9d 100644 --- a/include/pybind11/detail/common.h +++ b/include/pybind11/detail/common.h @@ -135,7 +135,8 @@ # define HAVE_ROUND 1 # endif # pragma warning(push) -# pragma warning(disable: 4510 4610 4512 4005) +// C4505: 'PySlice_GetIndicesEx': unreferenced local function has been removed (PyPy only) +# pragma warning(disable: 4505) # if defined(_DEBUG) && !defined(Py_DEBUG) # define PYBIND11_DEBUG_MARKER # undef _DEBUG diff --git a/include/pybind11/pybind11.h b/include/pybind11/pybind11.h index 3eb71f386a..1cb232d3a7 100644 --- a/include/pybind11/pybind11.h +++ b/include/pybind11/pybind11.h @@ -14,7 +14,6 @@ # pragma warning(push) # pragma warning(disable: 4100) // warning C4100: Unreferenced formal parameter # pragma warning(disable: 4127) // warning C4127: Conditional expression is constant -# pragma warning(disable: 4505) // warning C4505: 'PySlice_GetIndicesEx': unreferenced local function has been removed (PyPy only) #elif defined(__GNUG__) && !defined(__clang__) && !defined(__INTEL_COMPILER) # pragma GCC diagnostic push # pragma GCC diagnostic ignored "-Wunused-but-set-parameter" From dcbda8d7ff6e0a207807208ff0364bbb74e02175 Mon Sep 17 00:00:00 2001 From: "Ralf W. Grosse-Kunstleve" Date: Fri, 30 Jul 2021 11:25:29 -0700 Subject: [PATCH 115/638] Removing MSVC C4127 from pragma block at the top of pybind11.h (#3152) * Removing pragma for 4127 (to see what is still broken with the latest code). * Using new constexpr_bool() to suppress warning C4127. * One missed case, Python 2 only. * PYBIND11_SILENCE_MSVC_C4127 (more similar to the approach for C4100). --- include/pybind11/cast.h | 12 ++++++++---- include/pybind11/detail/common.h | 11 +++++++++++ include/pybind11/detail/init.h | 6 +++--- include/pybind11/pybind11.h | 11 +++-------- include/pybind11/pytypes.h | 6 +++--- 5 files changed, 28 insertions(+), 18 deletions(-) diff --git a/include/pybind11/cast.h b/include/pybind11/cast.h index 5ff0355a6f..211cf09bcd 100644 --- a/include/pybind11/cast.h +++ b/include/pybind11/cast.h @@ -384,7 +384,11 @@ template struct string_caster { const auto *buffer = reinterpret_cast(PYBIND11_BYTES_AS_STRING(utfNbytes.ptr())); size_t length = (size_t) PYBIND11_BYTES_SIZE(utfNbytes.ptr()) / sizeof(CharT); - if (UTF_N > 8) { buffer++; length--; } // Skip BOM for UTF-16/32 + // Skip BOM for UTF-16/32 + if (PYBIND11_SILENCE_MSVC_C4127(UTF_N > 8)) { + buffer++; + length--; + } value = StringType(buffer, length); // If we're loading a string_view we need to keep the encoded Python object alive: @@ -499,7 +503,7 @@ template struct type_caster 1 && str_len <= 4) { + if (PYBIND11_SILENCE_MSVC_C4127(StringCaster::UTF_N == 8) && str_len > 1 && str_len <= 4) { auto v0 = static_cast(value[0]); // low bits only: 0-127 // 0b110xxxxx - start of 2-byte sequence @@ -524,7 +528,7 @@ template struct type_caster(value[0]); if (one_char >= 0xD800 && one_char < 0xE000) throw value_error("Character code point not in range(0x10000)"); @@ -778,7 +782,7 @@ struct pyobject_caster { // For Python 2, without this implicit conversion, Python code would // need to be cluttered with six.ensure_text() or similar, only to be // un-cluttered later after Python 2 support is dropped. - if (std::is_same::value && isinstance(src)) { + if (PYBIND11_SILENCE_MSVC_C4127(std::is_same::value) && isinstance(src)) { PyObject *str_from_bytes = PyUnicode_FromEncodedObject(src.ptr(), "utf-8", nullptr); if (!str_from_bytes) throw error_already_set(); value = reinterpret_steal(str_from_bytes); diff --git a/include/pybind11/detail/common.h b/include/pybind11/detail/common.h index d1e9e6591f..e8d83ae0db 100644 --- a/include/pybind11/detail/common.h +++ b/include/pybind11/detail/common.h @@ -941,5 +941,16 @@ inline constexpr void workaround_incorrect_msvc_c4100(Args &&...) {} # define PYBIND11_WORKAROUND_INCORRECT_MSVC_C4100(...) #endif +#if defined(_MSC_VER) // All versions (as of July 2021). + +// warning C4127: Conditional expression is constant +constexpr inline bool silence_msvc_c4127(bool cond) { return cond; } + +# define PYBIND11_SILENCE_MSVC_C4127(...) detail::silence_msvc_c4127(__VA_ARGS__) + +#else +# define PYBIND11_SILENCE_MSVC_C4127(...) __VA_ARGS__ +#endif + PYBIND11_NAMESPACE_END(detail) PYBIND11_NAMESPACE_END(PYBIND11_NAMESPACE) diff --git a/include/pybind11/detail/init.h b/include/pybind11/detail/init.h index 3ebec041f0..e795da7d75 100644 --- a/include/pybind11/detail/init.h +++ b/include/pybind11/detail/init.h @@ -96,7 +96,7 @@ template void construct(value_and_holder &v_h, Cpp *ptr, bool need_alias) { PYBIND11_WORKAROUND_INCORRECT_MSVC_C4100(need_alias); no_nullptr(ptr); - if (Class::has_alias && need_alias && !is_alias(ptr)) { + if (PYBIND11_SILENCE_MSVC_C4127(Class::has_alias) && need_alias && !is_alias(ptr)) { // We're going to try to construct an alias by moving the cpp type. Whether or not // that succeeds, we still need to destroy the original cpp pointer (either the // moved away leftover, if the alias construction works, or the value itself if we @@ -136,7 +136,7 @@ void construct(value_and_holder &v_h, Holder holder, bool need_alias) { auto *ptr = holder_helper>::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(ptr)) + if (PYBIND11_SILENCE_MSVC_C4127(Class::has_alias) && need_alias && !is_alias(ptr)) throw type_error("pybind11::init(): construction failed: returned holder-wrapped instance " "is not an alias instance"); @@ -153,7 +153,7 @@ void construct(value_and_holder &v_h, Cpp &&result, bool need_alias) { PYBIND11_WORKAROUND_INCORRECT_MSVC_C4100(need_alias); static_assert(std::is_move_constructible>::value, "pybind11::init() return-by-value factory function requires a movable class"); - if (Class::has_alias && need_alias) + if (PYBIND11_SILENCE_MSVC_C4127(Class::has_alias) && need_alias) construct_alias_from_cpp(is_alias_constructible{}, v_h, std::move(result)); else v_h.value_ptr() = new Cpp(std::move(result)); diff --git a/include/pybind11/pybind11.h b/include/pybind11/pybind11.h index ec6bd8d351..aeeb3368a8 100644 --- a/include/pybind11/pybind11.h +++ b/include/pybind11/pybind11.h @@ -10,10 +10,7 @@ #pragma once -#if defined(_MSC_VER) && !defined(__INTEL_COMPILER) -# pragma warning(push) -# pragma warning(disable: 4127) // warning C4127: Conditional expression is constant -#elif defined(__GNUG__) && !defined(__clang__) && !defined(__INTEL_COMPILER) +#if defined(__GNUG__) && !defined(__clang__) && !defined(__INTEL_COMPILER) # pragma GCC diagnostic push # pragma GCC diagnostic ignored "-Wunused-but-set-parameter" # pragma GCC diagnostic ignored "-Wattributes" @@ -166,7 +163,7 @@ class cpp_function : public function { auto rec = unique_rec.get(); /* Store the capture object directly in the function record if there is enough space */ - if (sizeof(capture) <= sizeof(rec->data)) { + if (PYBIND11_SILENCE_MSVC_C4127(sizeof(capture) <= sizeof(rec->data))) { /* Without these pragmas, GCC warns that there might not be enough space to use the placement new operator. However, the 'if' statement above ensures that this is the case. */ @@ -2388,8 +2385,6 @@ PYBIND11_NAMESPACE_END(PYBIND11_NAMESPACE) # pragma GCC diagnostic pop // -Wnoexcept-type #endif -#if defined(_MSC_VER) && !defined(__INTEL_COMPILER) -# pragma warning(pop) -#elif defined(__GNUG__) && !defined(__clang__) && !defined(__INTEL_COMPILER) +#if defined(__GNUG__) && !defined(__clang__) && !defined(__INTEL_COMPILER) # pragma GCC diagnostic pop #endif diff --git a/include/pybind11/pytypes.h b/include/pybind11/pytypes.h index 4cf606e8d0..c7b2501feb 100644 --- a/include/pybind11/pytypes.h +++ b/include/pybind11/pytypes.h @@ -1191,9 +1191,9 @@ PYBIND11_NAMESPACE_BEGIN(detail) // unsigned type: (A)-1 != (B)-1 when A and B are unsigned types of different sizes). template Unsigned as_unsigned(PyObject *o) { - if (sizeof(Unsigned) <= sizeof(unsigned long) + if (PYBIND11_SILENCE_MSVC_C4127(sizeof(Unsigned) <= sizeof(unsigned long)) #if PY_VERSION_HEX < 0x03000000 - || PyInt_Check(o) + || PyInt_Check(o) #endif ) { unsigned long v = PyLong_AsUnsignedLong(o); @@ -1212,7 +1212,7 @@ class int_ : public object { template ::value, int> = 0> int_(T value) { - if (sizeof(T) <= sizeof(long)) { + if (PYBIND11_SILENCE_MSVC_C4127(sizeof(T) <= sizeof(long))) { if (std::is_signed::value) m_ptr = PyLong_FromLong((long) value); else From f4f4632e283ce5476964e9f612ecb7d7915fb035 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 3 Aug 2021 10:56:57 -0400 Subject: [PATCH 116/638] [pre-commit.ci] pre-commit autoupdate (#3167) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit updates: - [github.com/asottile/pyupgrade: v2.23.0 → v2.23.1](https://github.com/asottile/pyupgrade/compare/v2.23.0...v2.23.1) Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> --- .pre-commit-config.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 510b841260..57beb77793 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -31,7 +31,7 @@ repos: exclude: ^noxfile.py$ - repo: https://github.com/asottile/pyupgrade - rev: v2.23.0 + rev: v2.23.1 hooks: - id: pyupgrade From c0756ccd932de6e8d52ef102de0e18558f157db9 Mon Sep 17 00:00:00 2001 From: Aaron Gokaslan Date: Tue, 3 Aug 2021 13:15:48 -0400 Subject: [PATCH 117/638] fix: func_handle for rule of two (#3169) * Fix func_handle for rule of two * Apply reviewer suggestion --- include/pybind11/functional.h | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/include/pybind11/functional.h b/include/pybind11/functional.h index b9bf38919c..bc8a8af821 100644 --- a/include/pybind11/functional.h +++ b/include/pybind11/functional.h @@ -70,9 +70,11 @@ struct type_caster> { struct func_handle { function f; func_handle(function &&f_) noexcept : f(std::move(f_)) {} - func_handle(const func_handle& f_) { + func_handle(const func_handle &f_) { operator=(f_); } + func_handle &operator=(const func_handle &f_) { gil_scoped_acquire acq; f = f_.f; + return *this; } ~func_handle() { gil_scoped_acquire acq; From a2b78a8c279e54f01a0502405fd2335a59c43a6d Mon Sep 17 00:00:00 2001 From: Henry Schreiner Date: Tue, 3 Aug 2021 10:16:14 -0700 Subject: [PATCH 118/638] chore: changelog update (#3163) * chore: changelog update * Update docs/changelog.rst --- docs/changelog.rst | 49 ++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 47 insertions(+), 2 deletions(-) diff --git a/docs/changelog.rst b/docs/changelog.rst index 5e8a63258f..2f76abe055 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -6,9 +6,54 @@ Changelog Starting with version 1.8.0, pybind11 releases use a `semantic versioning `_ policy. -Next version (WIP) ------------------- +v2.8.0 (WIP) +------------ +* Allow exception translators to be optionally registered local to a module + instead of applying globally across all pybind11 modules. Use + ``register_local_exception_translator(ExceptionTranslator&& translator)`` + instead of ``register_exception_translator(ExceptionTranslator&& + translator)`` to keep your exception remapping code local to the module. + `#2650 `_ + +v2.7.1 (Aug 3, 2021) +--------------------- + +Minor missing functionality added: + +* Allow Python builtins to be used as callbacks in CPython. + `#1413 `_ + +Bug fixes: + +* Fix regression in CMake Python package config: improper use of absolute path. + `#3144 `_ + +* Fix Mingw64 and add to the CI testing matrix. + `#3132 `_ + +* Specified UTF8-encoding in setup.py calls of open(). + `#3137 `_ + +* Add clang-tidy-readability rules to make boolean casts explicit improving + code readability. Also enabled other misc and readability clang-tidy checks. + `#3148 `_ + +* Move object in ``.pop()`` for list. + `#3116 `_ + +Backend and tidying up: + +* Removed and fixed warning suppressions. + `#3127 `_ + `#3129 `_ + `#3135 `_ + `#3141 `_ + `#3142 `_ + `#3150 `_ + `#3152 `_ + `#3160 `_ + `#3161 `_ v2.7.0 (Jul 16, 2021) From ada6b7910491f5b7fd910d8bf23c474b526f6591 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 3 Aug 2021 10:56:57 -0400 Subject: [PATCH 119/638] [pre-commit.ci] pre-commit autoupdate (#3167) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit updates: - [github.com/asottile/pyupgrade: v2.23.0 → v2.23.1](https://github.com/asottile/pyupgrade/compare/v2.23.0...v2.23.1) Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> --- .pre-commit-config.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 510b841260..57beb77793 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -31,7 +31,7 @@ repos: exclude: ^noxfile.py$ - repo: https://github.com/asottile/pyupgrade - rev: v2.23.0 + rev: v2.23.1 hooks: - id: pyupgrade From 9f204a18573d8a194549ad0fd51afdd3db5596e5 Mon Sep 17 00:00:00 2001 From: Aaron Gokaslan Date: Tue, 3 Aug 2021 13:15:48 -0400 Subject: [PATCH 120/638] fix: func_handle for rule of two (#3169) * Fix func_handle for rule of two * Apply reviewer suggestion --- include/pybind11/functional.h | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/include/pybind11/functional.h b/include/pybind11/functional.h index b9bf38919c..bc8a8af821 100644 --- a/include/pybind11/functional.h +++ b/include/pybind11/functional.h @@ -70,9 +70,11 @@ struct type_caster> { struct func_handle { function f; func_handle(function &&f_) noexcept : f(std::move(f_)) {} - func_handle(const func_handle& f_) { + func_handle(const func_handle &f_) { operator=(f_); } + func_handle &operator=(const func_handle &f_) { gil_scoped_acquire acq; f = f_.f; + return *this; } ~func_handle() { gil_scoped_acquire acq; From 9095984850eb1d0240575a31281d8651ff5e7a2e Mon Sep 17 00:00:00 2001 From: Henry Schreiner Date: Tue, 3 Aug 2021 10:16:14 -0700 Subject: [PATCH 121/638] chore: changelog update (#3163) * chore: changelog update * Update docs/changelog.rst --- docs/changelog.rst | 49 ++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 47 insertions(+), 2 deletions(-) diff --git a/docs/changelog.rst b/docs/changelog.rst index 5e8a63258f..2f76abe055 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -6,9 +6,54 @@ Changelog Starting with version 1.8.0, pybind11 releases use a `semantic versioning `_ policy. -Next version (WIP) ------------------- +v2.8.0 (WIP) +------------ +* Allow exception translators to be optionally registered local to a module + instead of applying globally across all pybind11 modules. Use + ``register_local_exception_translator(ExceptionTranslator&& translator)`` + instead of ``register_exception_translator(ExceptionTranslator&& + translator)`` to keep your exception remapping code local to the module. + `#2650 `_ + +v2.7.1 (Aug 3, 2021) +--------------------- + +Minor missing functionality added: + +* Allow Python builtins to be used as callbacks in CPython. + `#1413 `_ + +Bug fixes: + +* Fix regression in CMake Python package config: improper use of absolute path. + `#3144 `_ + +* Fix Mingw64 and add to the CI testing matrix. + `#3132 `_ + +* Specified UTF8-encoding in setup.py calls of open(). + `#3137 `_ + +* Add clang-tidy-readability rules to make boolean casts explicit improving + code readability. Also enabled other misc and readability clang-tidy checks. + `#3148 `_ + +* Move object in ``.pop()`` for list. + `#3116 `_ + +Backend and tidying up: + +* Removed and fixed warning suppressions. + `#3127 `_ + `#3129 `_ + `#3135 `_ + `#3141 `_ + `#3142 `_ + `#3150 `_ + `#3152 `_ + `#3160 `_ + `#3161 `_ v2.7.0 (Jul 16, 2021) From 078c1167497c81f866b94df5451939077aba0b6f Mon Sep 17 00:00:00 2001 From: Henry Schreiner Date: Tue, 3 Aug 2021 15:05:54 -0400 Subject: [PATCH 122/638] chore: bump to version 2.7.1 --- include/pybind11/detail/common.h | 2 +- pybind11/_version.py | 2 +- setup.cfg | 1 + 3 files changed, 3 insertions(+), 2 deletions(-) diff --git a/include/pybind11/detail/common.h b/include/pybind11/detail/common.h index 9df670fa9d..8006a8df1d 100644 --- a/include/pybind11/detail/common.h +++ b/include/pybind11/detail/common.h @@ -11,7 +11,7 @@ #define PYBIND11_VERSION_MAJOR 2 #define PYBIND11_VERSION_MINOR 7 -#define PYBIND11_VERSION_PATCH 1.dev1 +#define PYBIND11_VERSION_PATCH 1 // Similar to Python's convention: https://docs.python.org/3/c-api/apiabiversion.html // Additional convention: 0xD = dev diff --git a/pybind11/_version.py b/pybind11/_version.py index 2bb14b963d..2bb4fe364d 100644 --- a/pybind11/_version.py +++ b/pybind11/_version.py @@ -8,5 +8,5 @@ def _to_int(s): return s -__version__ = "2.7.1.dev1" +__version__ = "2.7.1" version_info = tuple(_to_int(s) for s in __version__.split(".")) diff --git a/setup.cfg b/setup.cfg index 31038eb026..95963d2f87 100644 --- a/setup.cfg +++ b/setup.cfg @@ -20,6 +20,7 @@ classifiers = Programming Language :: Python :: 3.7 Programming Language :: Python :: 3.8 Programming Language :: Python :: 3.9 + Programming Language :: Python :: 3.10 License :: OSI Approved :: BSD License Programming Language :: Python :: Implementation :: PyPy Programming Language :: Python :: Implementation :: CPython From 5f34c42d70d6af0af546106503da9d045efb904e Mon Sep 17 00:00:00 2001 From: Henry Schreiner Date: Tue, 3 Aug 2021 15:05:54 -0400 Subject: [PATCH 123/638] chore: bump to version 2.7.1 --- include/pybind11/detail/common.h | 2 +- pybind11/_version.py | 2 +- setup.cfg | 1 + 3 files changed, 3 insertions(+), 2 deletions(-) diff --git a/include/pybind11/detail/common.h b/include/pybind11/detail/common.h index e8d83ae0db..1d77143354 100644 --- a/include/pybind11/detail/common.h +++ b/include/pybind11/detail/common.h @@ -11,7 +11,7 @@ #define PYBIND11_VERSION_MAJOR 2 #define PYBIND11_VERSION_MINOR 7 -#define PYBIND11_VERSION_PATCH 1.dev1 +#define PYBIND11_VERSION_PATCH 1 // Similar to Python's convention: https://docs.python.org/3/c-api/apiabiversion.html // Additional convention: 0xD = dev diff --git a/pybind11/_version.py b/pybind11/_version.py index 2bb14b963d..2bb4fe364d 100644 --- a/pybind11/_version.py +++ b/pybind11/_version.py @@ -8,5 +8,5 @@ def _to_int(s): return s -__version__ = "2.7.1.dev1" +__version__ = "2.7.1" version_info = tuple(_to_int(s) for s in __version__.split(".")) diff --git a/setup.cfg b/setup.cfg index 31038eb026..95963d2f87 100644 --- a/setup.cfg +++ b/setup.cfg @@ -20,6 +20,7 @@ classifiers = Programming Language :: Python :: 3.7 Programming Language :: Python :: 3.8 Programming Language :: Python :: 3.9 + Programming Language :: Python :: 3.10 License :: OSI Approved :: BSD License Programming Language :: Python :: Implementation :: PyPy Programming Language :: Python :: Implementation :: CPython From 82adacb31de4d24983e240a60d608dfaed464d1a Mon Sep 17 00:00:00 2001 From: Henry Schreiner Date: Tue, 3 Aug 2021 15:20:23 -0400 Subject: [PATCH 124/638] fix: include hex version in bump --- include/pybind11/detail/common.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/pybind11/detail/common.h b/include/pybind11/detail/common.h index 1d77143354..b918584087 100644 --- a/include/pybind11/detail/common.h +++ b/include/pybind11/detail/common.h @@ -15,7 +15,7 @@ // Similar to Python's convention: https://docs.python.org/3/c-api/apiabiversion.html // Additional convention: 0xD = dev -#define PYBIND11_VERSION_HEX 0x020701D1 +#define PYBIND11_VERSION_HEX 0x02070100 #define PYBIND11_NAMESPACE_BEGIN(name) namespace name { #define PYBIND11_NAMESPACE_END(name) } From 787d2c88cafa4d07fb38c9519c485a86323cfcf4 Mon Sep 17 00:00:00 2001 From: Henry Schreiner Date: Tue, 3 Aug 2021 15:20:23 -0400 Subject: [PATCH 125/638] fix: include hex version in bump --- include/pybind11/detail/common.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/pybind11/detail/common.h b/include/pybind11/detail/common.h index 8006a8df1d..dfd5b5699f 100644 --- a/include/pybind11/detail/common.h +++ b/include/pybind11/detail/common.h @@ -15,7 +15,7 @@ // Similar to Python's convention: https://docs.python.org/3/c-api/apiabiversion.html // Additional convention: 0xD = dev -#define PYBIND11_VERSION_HEX 0x020701D1 +#define PYBIND11_VERSION_HEX 0x02070100 #define PYBIND11_NAMESPACE_BEGIN(name) namespace name { #define PYBIND11_NAMESPACE_END(name) } From c30f57d2ede50c0306db54d5fb215f57c1e465a6 Mon Sep 17 00:00:00 2001 From: Henry Schreiner Date: Tue, 3 Aug 2021 16:11:07 -0400 Subject: [PATCH 126/638] chore: start development for 2.8.0 --- include/pybind11/detail/common.h | 6 +++--- pybind11/_version.py | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/include/pybind11/detail/common.h b/include/pybind11/detail/common.h index b918584087..adb0009383 100644 --- a/include/pybind11/detail/common.h +++ b/include/pybind11/detail/common.h @@ -10,12 +10,12 @@ #pragma once #define PYBIND11_VERSION_MAJOR 2 -#define PYBIND11_VERSION_MINOR 7 -#define PYBIND11_VERSION_PATCH 1 +#define PYBIND11_VERSION_MINOR 8 +#define PYBIND11_VERSION_PATCH 0.dev1 // Similar to Python's convention: https://docs.python.org/3/c-api/apiabiversion.html // Additional convention: 0xD = dev -#define PYBIND11_VERSION_HEX 0x02070100 +#define PYBIND11_VERSION_HEX 0x020800D0 #define PYBIND11_NAMESPACE_BEGIN(name) namespace name { #define PYBIND11_NAMESPACE_END(name) } diff --git a/pybind11/_version.py b/pybind11/_version.py index 2bb4fe364d..610d39bf1c 100644 --- a/pybind11/_version.py +++ b/pybind11/_version.py @@ -8,5 +8,5 @@ def _to_int(s): return s -__version__ = "2.7.1" +__version__ = "2.8.0.dev1" version_info = tuple(_to_int(s) for s in __version__.split(".")) From 5f4d725918d7fb90d441b8c1ee1a2cf464407a6a Mon Sep 17 00:00:00 2001 From: Henry Schreiner Date: Tue, 3 Aug 2021 17:03:11 -0400 Subject: [PATCH 127/638] fix: version number hex --- include/pybind11/detail/common.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/pybind11/detail/common.h b/include/pybind11/detail/common.h index adb0009383..8b8f5da335 100644 --- a/include/pybind11/detail/common.h +++ b/include/pybind11/detail/common.h @@ -15,7 +15,7 @@ // Similar to Python's convention: https://docs.python.org/3/c-api/apiabiversion.html // Additional convention: 0xD = dev -#define PYBIND11_VERSION_HEX 0x020800D0 +#define PYBIND11_VERSION_HEX 0x020800D1 #define PYBIND11_NAMESPACE_BEGIN(name) namespace name { #define PYBIND11_NAMESPACE_END(name) } From 1fafd1b44796f39e56d4c029203c0e2ba18a60dd Mon Sep 17 00:00:00 2001 From: Henry Schreiner Date: Fri, 6 Aug 2021 13:03:26 -0400 Subject: [PATCH 128/638] fix: apply simpler expression with fewer workarounds --- include/pybind11/detail/type_caster_base.h | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/include/pybind11/detail/type_caster_base.h b/include/pybind11/detail/type_caster_base.h index e2d1bcb8cc..a708d87fb4 100644 --- a/include/pybind11/detail/type_caster_base.h +++ b/include/pybind11/detail/type_caster_base.h @@ -927,18 +927,17 @@ template class type_caster_base : public type_caster_generic { using Constructor = void *(*)(const void *); /* Only enabled when the types are {copy,move}-constructible *and* when the type - does not have a private operator new implementation. */ + does not have a private operator new implementation. A comma operator is used in the decltype + argument to apply SFINAE to the public copy/move constructors.*/ template ::value>> - static auto make_copy_constructor(const T *x) -> decltype(new T(*x), Constructor{}) { - PYBIND11_WORKAROUND_INCORRECT_MSVC_C4100(x); + static auto make_copy_constructor(const T *) -> decltype(new T(std::declval()), Constructor{}) { return [](const void *arg) -> void * { return new T(*reinterpret_cast(arg)); }; } template ::value>> - static auto make_move_constructor(const T *x) -> decltype(new T(std::move(*const_cast(x))), Constructor{}) { - PYBIND11_WORKAROUND_INCORRECT_MSVC_C4100(x); + static auto make_move_constructor(const T *) -> decltype(new T(std::declval()), Constructor{}) { return [](const void *arg) -> void * { return new T(std::move(*const_cast(reinterpret_cast(arg)))); }; From 089328f77955685a29db9b9cf05e7c0893ee031b Mon Sep 17 00:00:00 2001 From: Henry Schreiner Date: Fri, 6 Aug 2021 13:09:48 -0400 Subject: [PATCH 129/638] Revert "fix: apply simpler expression with fewer workarounds" This reverts commit 1fafd1b44796f39e56d4c029203c0e2ba18a60dd. --- include/pybind11/detail/type_caster_base.h | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/include/pybind11/detail/type_caster_base.h b/include/pybind11/detail/type_caster_base.h index a708d87fb4..e2d1bcb8cc 100644 --- a/include/pybind11/detail/type_caster_base.h +++ b/include/pybind11/detail/type_caster_base.h @@ -927,17 +927,18 @@ template class type_caster_base : public type_caster_generic { using Constructor = void *(*)(const void *); /* Only enabled when the types are {copy,move}-constructible *and* when the type - does not have a private operator new implementation. A comma operator is used in the decltype - argument to apply SFINAE to the public copy/move constructors.*/ + does not have a private operator new implementation. */ template ::value>> - static auto make_copy_constructor(const T *) -> decltype(new T(std::declval()), Constructor{}) { + static auto make_copy_constructor(const T *x) -> decltype(new T(*x), Constructor{}) { + PYBIND11_WORKAROUND_INCORRECT_MSVC_C4100(x); return [](const void *arg) -> void * { return new T(*reinterpret_cast(arg)); }; } template ::value>> - static auto make_move_constructor(const T *) -> decltype(new T(std::declval()), Constructor{}) { + static auto make_move_constructor(const T *x) -> decltype(new T(std::move(*const_cast(x))), Constructor{}) { + PYBIND11_WORKAROUND_INCORRECT_MSVC_C4100(x); return [](const void *arg) -> void * { return new T(std::move(*const_cast(reinterpret_cast(arg)))); }; From 3893f37bcec9f0e9c8ab2c0069cfec8520591860 Mon Sep 17 00:00:00 2001 From: Aaron Gokaslan Date: Fri, 6 Aug 2021 14:30:28 -0400 Subject: [PATCH 130/638] maint(clang-tidy): Bugprone enable checks (#3166) * Enable bugprone checks * Reset delta and massage config * Start to apply bugprone fixes * try to fix minor bug * Fix later * Fix perfect forwarding bugprone * Remove nolint * undo constructor delete * Fix bugprone-perfect-forwarding again * Remove TODO * Add another nolint for bugprone-exception-escape in scoped interpreter * Fix remaining bugprone errors * Properly apply bugprone-macro-parantheses * Redo formatting and remove bugprone nolint * Add coment and revert more whitespace changes * Fix typo * Fix parsing bug * Add back comma * Fix clang-tidy issue * Apply remaining clang-tidy fixes --- .clang-tidy | 4 +++ include/pybind11/cast.h | 11 ++++---- include/pybind11/detail/common.h | 39 ++++++++++++++--------------- include/pybind11/embed.h | 36 +++++++++++++------------- include/pybind11/numpy.h | 7 ++++-- include/pybind11/pybind11.h | 2 +- tests/object.h | 6 ++++- tests/pybind11_tests.h | 9 +++---- tests/test_buffers.cpp | 6 ++++- tests/test_class.cpp | 8 +++--- tests/test_multiple_inheritance.cpp | 5 +++- tests/test_numpy_dtypes.cpp | 12 +++++---- tests/test_smart_ptr.cpp | 1 + tests/test_tagbased_polymorphic.cpp | 6 ++--- tests/test_virtual_functions.cpp | 1 + 15 files changed, 85 insertions(+), 68 deletions(-) diff --git a/.clang-tidy b/.clang-tidy index db5077c227..c83b9b2f5a 100644 --- a/.clang-tidy +++ b/.clang-tidy @@ -1,6 +1,7 @@ FormatStyle: file Checks: ' +*bugprone*, cppcoreguidelines-init-variables, clang-analyzer-optin.cplusplus.VirtualCall, llvm-namespace-comment, @@ -43,6 +44,9 @@ readability-static-accessed-through-instance, readability-static-definition-in-anonymous-namespace, readability-string-compare, readability-uniqueptr-delete-release, +-bugprone-exception-escape, +-bugprone-reserved-identifier, +-bugprone-unused-raii, ' CheckOptions: diff --git a/include/pybind11/cast.h b/include/pybind11/cast.h index 211cf09bcd..988a236950 100644 --- a/include/pybind11/cast.h +++ b/include/pybind11/cast.h @@ -102,9 +102,9 @@ public: } \ return cast(*src, policy, parent); \ } \ - operator type *() { return &value; } \ - operator type &() { return value; } \ - operator type &&() && { return std::move(value); } \ + operator type *() { return &value; } /* NOLINT(bugprone-macro-parentheses) */ \ + operator type &() { return value; } /* NOLINT(bugprone-macro-parentheses) */ \ + operator type &&() && { return std::move(value); } /* NOLINT(bugprone-macro-parentheses) */ \ template \ using cast_op_type = pybind11::detail::movable_cast_op_type @@ -145,9 +145,8 @@ struct type_caster::value && !is_std_char_t py_value = (py_type) PyFloat_AsDouble(src.ptr()); else return false; - } else if (PyFloat_Check(src.ptr())) { - return false; - } else if (!convert && !PYBIND11_LONG_CHECK(src.ptr()) && !index_check(src.ptr())) { + } else if (PyFloat_Check(src.ptr()) + || (!convert && !PYBIND11_LONG_CHECK(src.ptr()) && !index_check(src.ptr()))) { return false; } else { handle src_or_index = src; diff --git a/include/pybind11/detail/common.h b/include/pybind11/detail/common.h index 8b8f5da335..27a79bfdd5 100644 --- a/include/pybind11/detail/common.h +++ b/include/pybind11/detail/common.h @@ -220,8 +220,8 @@ #define PYBIND11_BYTES_SIZE PyBytes_Size #define PYBIND11_LONG_CHECK(o) PyLong_Check(o) #define PYBIND11_LONG_AS_LONGLONG(o) PyLong_AsLongLong(o) -#define PYBIND11_LONG_FROM_SIGNED(o) PyLong_FromSsize_t((ssize_t) o) -#define PYBIND11_LONG_FROM_UNSIGNED(o) PyLong_FromSize_t((size_t) o) +#define PYBIND11_LONG_FROM_SIGNED(o) PyLong_FromSsize_t((ssize_t) (o)) +#define PYBIND11_LONG_FROM_UNSIGNED(o) PyLong_FromSize_t((size_t) (o)) #define PYBIND11_BYTES_NAME "bytes" #define PYBIND11_STRING_NAME "str" #define PYBIND11_SLICE_OBJECT PyObject @@ -356,24 +356,23 @@ extern "C" { }); } \endrst */ -#define PYBIND11_MODULE(name, variable) \ - static ::pybind11::module_::module_def \ - PYBIND11_CONCAT(pybind11_module_def_, name) PYBIND11_MAYBE_UNUSED; \ - PYBIND11_MAYBE_UNUSED \ - static void PYBIND11_CONCAT(pybind11_init_, name)(::pybind11::module_ &); \ - PYBIND11_PLUGIN_IMPL(name) { \ - PYBIND11_CHECK_PYTHON_VERSION \ - PYBIND11_ENSURE_INTERNALS_READY \ - auto m = ::pybind11::module_::create_extension_module( \ - PYBIND11_TOSTRING(name), nullptr, \ - &PYBIND11_CONCAT(pybind11_module_def_, name)); \ - try { \ - PYBIND11_CONCAT(pybind11_init_, name)(m); \ - return m.ptr(); \ - } PYBIND11_CATCH_INIT_EXCEPTIONS \ - } \ - void PYBIND11_CONCAT(pybind11_init_, name)(::pybind11::module_ &variable) - +#define PYBIND11_MODULE(name, variable) \ + static ::pybind11::module_::module_def PYBIND11_CONCAT(pybind11_module_def_, name) \ + PYBIND11_MAYBE_UNUSED; \ + PYBIND11_MAYBE_UNUSED \ + static void PYBIND11_CONCAT(pybind11_init_, name)(::pybind11::module_ &); \ + PYBIND11_PLUGIN_IMPL(name) { \ + PYBIND11_CHECK_PYTHON_VERSION \ + PYBIND11_ENSURE_INTERNALS_READY \ + auto m = ::pybind11::module_::create_extension_module( \ + PYBIND11_TOSTRING(name), nullptr, &PYBIND11_CONCAT(pybind11_module_def_, name)); \ + try { \ + PYBIND11_CONCAT(pybind11_init_, name)(m); \ + return m.ptr(); \ + } \ + PYBIND11_CATCH_INIT_EXCEPTIONS \ + } \ + void PYBIND11_CONCAT(pybind11_init_, name)(::pybind11::module_ & (variable)) PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE) diff --git a/include/pybind11/embed.h b/include/pybind11/embed.h index abb1cd3cca..6e777830f9 100644 --- a/include/pybind11/embed.h +++ b/include/pybind11/embed.h @@ -45,25 +45,23 @@ }); } \endrst */ -#define PYBIND11_EMBEDDED_MODULE(name, variable) \ - static ::pybind11::module_::module_def \ - PYBIND11_CONCAT(pybind11_module_def_, name); \ - static void PYBIND11_CONCAT(pybind11_init_, name)(::pybind11::module_ &); \ - static PyObject PYBIND11_CONCAT(*pybind11_init_wrapper_, name)() { \ - auto m = ::pybind11::module_::create_extension_module( \ - PYBIND11_TOSTRING(name), nullptr, \ - &PYBIND11_CONCAT(pybind11_module_def_, name)); \ - try { \ - PYBIND11_CONCAT(pybind11_init_, name)(m); \ - return m.ptr(); \ - } PYBIND11_CATCH_INIT_EXCEPTIONS \ - } \ - PYBIND11_EMBEDDED_MODULE_IMPL(name) \ - ::pybind11::detail::embedded_module PYBIND11_CONCAT(pybind11_module_, name) \ - (PYBIND11_TOSTRING(name), \ - PYBIND11_CONCAT(pybind11_init_impl_, name)); \ - void PYBIND11_CONCAT(pybind11_init_, name)(::pybind11::module_ &variable) - +#define PYBIND11_EMBEDDED_MODULE(name, variable) \ + static ::pybind11::module_::module_def PYBIND11_CONCAT(pybind11_module_def_, name); \ + static void PYBIND11_CONCAT(pybind11_init_, name)(::pybind11::module_ &); \ + static PyObject PYBIND11_CONCAT(*pybind11_init_wrapper_, name)() { \ + auto m = ::pybind11::module_::create_extension_module( \ + PYBIND11_TOSTRING(name), nullptr, &PYBIND11_CONCAT(pybind11_module_def_, name)); \ + try { \ + PYBIND11_CONCAT(pybind11_init_, name)(m); \ + return m.ptr(); \ + } \ + PYBIND11_CATCH_INIT_EXCEPTIONS \ + } \ + PYBIND11_EMBEDDED_MODULE_IMPL(name) \ + ::pybind11::detail::embedded_module PYBIND11_CONCAT(pybind11_module_, name)( \ + PYBIND11_TOSTRING(name), PYBIND11_CONCAT(pybind11_init_impl_, name)); \ + void PYBIND11_CONCAT(pybind11_init_, name)(::pybind11::module_ \ + & variable) // NOLINT(bugprone-macro-parentheses) PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE) PYBIND11_NAMESPACE_BEGIN(detail) diff --git a/include/pybind11/numpy.h b/include/pybind11/numpy.h index 7313897fe1..aa8294e167 100644 --- a/include/pybind11/numpy.h +++ b/include/pybind11/numpy.h @@ -1551,8 +1551,11 @@ struct vectorize_helper { "pybind11::vectorize(...) requires a function with at least one vectorizable argument"); public: - template - explicit vectorize_helper(T &&f) : f(std::forward(f)) { } + template ::type>::value>> + explicit vectorize_helper(T &&f) : f(std::forward(f)) {} object operator()(typename vectorize_arg::type... args) { return run(args..., diff --git a/include/pybind11/pybind11.h b/include/pybind11/pybind11.h index aeeb3368a8..54213297dc 100644 --- a/include/pybind11/pybind11.h +++ b/include/pybind11/pybind11.h @@ -1726,7 +1726,7 @@ struct enum_base { m_base.attr(op) = cpp_function( \ [](const object &a, const object &b) { \ if (!type::handle_of(a).is(type::handle_of(b))) \ - strict_behavior; \ + strict_behavior; /* NOLINT(bugprone-macro-parentheses) */ \ return expr; \ }, \ name(op), \ diff --git a/tests/object.h b/tests/object.h index 865a9beae8..6851fc6247 100644 --- a/tests/object.h +++ b/tests/object.h @@ -110,7 +110,11 @@ template class ref { /// Overwrite this reference with another reference ref& operator=(const ref& r) { - print_copy_assigned(this, "pointer", r.m_ptr); track_copy_assigned((ref_tag*) this); + if (this == &r) { + return *this; + } + print_copy_assigned(this, "pointer", r.m_ptr); + track_copy_assigned((ref_tag *) this); if (m_ptr == r.m_ptr) return *this; diff --git a/tests/pybind11_tests.h b/tests/pybind11_tests.h index d970ba8bd4..12d8a777fc 100644 --- a/tests/pybind11_tests.h +++ b/tests/pybind11_tests.h @@ -23,11 +23,10 @@ class test_initializer { test_initializer(const char *submodule_name, Initializer init); }; -#define TEST_SUBMODULE(name, variable) \ - void test_submodule_##name(py::module_ &); \ - test_initializer name(#name, test_submodule_##name); \ - void test_submodule_##name(py::module_ &variable) - +#define TEST_SUBMODULE(name, variable) \ + void test_submodule_##name(py::module_ &); \ + test_initializer name(#name, test_submodule_##name); \ + void test_submodule_##name(py::module_ &(variable)) /// Dummy type which is not exported anywhere -- something to trigger a conversion error struct UnregisteredType { }; diff --git a/tests/test_buffers.cpp b/tests/test_buffers.cpp index e77c626f87..c7e2c7df30 100644 --- a/tests/test_buffers.cpp +++ b/tests/test_buffers.cpp @@ -40,7 +40,11 @@ TEST_SUBMODULE(buffers, m) { } Matrix &operator=(const Matrix &s) { - print_copy_assigned(this, std::to_string(m_rows) + "x" + std::to_string(m_cols) + " matrix"); + if (this == &s) { + return *this; + } + print_copy_assigned(this, + std::to_string(m_rows) + "x" + std::to_string(m_cols) + " matrix"); delete[] m_data; m_rows = s.m_rows; m_cols = s.m_cols; diff --git a/tests/test_class.cpp b/tests/test_class.cpp index 73de2a61aa..dff758392d 100644 --- a/tests/test_class.cpp +++ b/tests/test_class.cpp @@ -492,15 +492,15 @@ using DoesntBreak5 = py::class_>; using DoesntBreak6 = py::class_, std::shared_ptr>, BreaksTramp<6>>; using DoesntBreak7 = py::class_, BreaksTramp<7>, std::shared_ptr>>; using DoesntBreak8 = py::class_, std::shared_ptr>>; -#define CHECK_BASE(N) static_assert(std::is_same>::value, \ +#define CHECK_BASE(N) static_assert(std::is_same>::value, \ "DoesntBreak" #N " has wrong type!") CHECK_BASE(1); CHECK_BASE(2); CHECK_BASE(3); CHECK_BASE(4); CHECK_BASE(5); CHECK_BASE(6); CHECK_BASE(7); CHECK_BASE(8); -#define CHECK_ALIAS(N) static_assert(DoesntBreak##N::has_alias && std::is_same>::value, \ +#define CHECK_ALIAS(N) static_assert(DoesntBreak##N::has_alias && std::is_same>::value, \ "DoesntBreak" #N " has wrong type_alias!") #define CHECK_NOALIAS(N) static_assert(!DoesntBreak##N::has_alias && std::is_void::value, \ "DoesntBreak" #N " has type alias, but shouldn't!") CHECK_ALIAS(1); CHECK_ALIAS(2); CHECK_NOALIAS(3); CHECK_ALIAS(4); CHECK_NOALIAS(5); CHECK_ALIAS(6); CHECK_ALIAS(7); CHECK_NOALIAS(8); -#define CHECK_HOLDER(N, TYPE) static_assert(std::is_same>>::value, \ +#define CHECK_HOLDER(N, TYPE) static_assert(std::is_same>>::value, \ "DoesntBreak" #N " has wrong holder_type!") CHECK_HOLDER(1, unique); CHECK_HOLDER(2, unique); CHECK_HOLDER(3, unique); CHECK_HOLDER(4, unique); CHECK_HOLDER(5, unique); CHECK_HOLDER(6, shared); CHECK_HOLDER(7, shared); CHECK_HOLDER(8, shared); @@ -510,7 +510,7 @@ CHECK_HOLDER(6, shared); CHECK_HOLDER(7, shared); CHECK_HOLDER(8, shared); // failures occurs). // We have to actually look into the type: the typedef alone isn't enough to instantiate the type: -#define CHECK_BROKEN(N) static_assert(std::is_same>::value, \ +#define CHECK_BROKEN(N) static_assert(std::is_same>::value, \ "Breaks1 has wrong type!"); //// Two holder classes: diff --git a/tests/test_multiple_inheritance.cpp b/tests/test_multiple_inheritance.cpp index d6b24d34da..b5ca298d91 100644 --- a/tests/test_multiple_inheritance.cpp +++ b/tests/test_multiple_inheritance.cpp @@ -108,7 +108,10 @@ TEST_SUBMODULE(multiple_inheritance, m) { // test_multiple_inheritance_python_many_bases - #define PYBIND11_BASEN(N) py::class_>(m, "BaseN" #N).def(py::init()).def("f" #N, [](BaseN &b) { return b.i + N; }) +#define PYBIND11_BASEN(N) \ + py::class_>(m, "BaseN" #N).def(py::init()).def("f" #N, [](BaseN &b) { \ + return b.i + (N); \ + }) PYBIND11_BASEN( 1); PYBIND11_BASEN( 2); PYBIND11_BASEN( 3); PYBIND11_BASEN( 4); PYBIND11_BASEN( 5); PYBIND11_BASEN( 6); PYBIND11_BASEN( 7); PYBIND11_BASEN( 8); PYBIND11_BASEN( 9); PYBIND11_BASEN(10); PYBIND11_BASEN(11); PYBIND11_BASEN(12); diff --git a/tests/test_numpy_dtypes.cpp b/tests/test_numpy_dtypes.cpp index 340c972db2..bf4f4cee74 100644 --- a/tests/test_numpy_dtypes.cpp +++ b/tests/test_numpy_dtypes.cpp @@ -148,11 +148,13 @@ py::array mkarray_via_buffer(size_t n) { 1, { n }, { sizeof(T) })); } -#define SET_TEST_VALS(s, i) do { \ - s.bool_ = (i) % 2 != 0; \ - s.uint_ = (uint32_t) (i); \ - s.float_ = (float) (i) * 1.5f; \ - s.ldbl_ = (long double) (i) * -2.5L; } while (0) +#define SET_TEST_VALS(s, i) \ + do { \ + (s).bool_ = (i) % 2 != 0; \ + (s).uint_ = (uint32_t) (i); \ + (s).float_ = (float) (i) *1.5f; \ + (s).ldbl_ = (long double) (i) * -2.5L; \ + } while (0) template py::array_t create_recarray(size_t n) { diff --git a/tests/test_smart_ptr.cpp b/tests/test_smart_ptr.cpp index 57b2d894e2..eeaa44147a 100644 --- a/tests/test_smart_ptr.cpp +++ b/tests/test_smart_ptr.cpp @@ -182,6 +182,7 @@ struct SharedPtrRef { struct SharedFromThisRef { struct B : std::enable_shared_from_this { B() { print_created(this); } + // NOLINTNEXTLINE(bugprone-copy-constructor-init) B(const B &) : std::enable_shared_from_this() { print_copy_created(this); } B(B &&) noexcept : std::enable_shared_from_this() { print_move_created(this); } ~B() { print_destroyed(this); } diff --git a/tests/test_tagbased_polymorphic.cpp b/tests/test_tagbased_polymorphic.cpp index 838a168d2b..90f40e14cc 100644 --- a/tests/test_tagbased_polymorphic.cpp +++ b/tests/test_tagbased_polymorphic.cpp @@ -86,13 +86,13 @@ std::vector> create_zoo() const std::type_info* Animal::type_of_kind(Kind kind) { switch (kind) { - case Kind::Unknown: break; - + case Kind::Unknown: case Kind::Dog: break; + case Kind::Labrador: return &typeid(Labrador); case Kind::Chihuahua: return &typeid(Chihuahua); - case Kind::LastDog: break; + case Kind::LastDog: case Kind::Cat: break; case Kind::Panther: return &typeid(Panther); case Kind::LastCat: break; diff --git a/tests/test_virtual_functions.cpp b/tests/test_virtual_functions.cpp index 5280af8eb6..f83a7364b2 100644 --- a/tests/test_virtual_functions.cpp +++ b/tests/test_virtual_functions.cpp @@ -454,6 +454,7 @@ template class PyB_Tpl : public PyA_Tpl { public: using PyA_Tpl::PyA_Tpl; // Inherit constructors (via PyA_Tpl's inherited constructors) + // NOLINTNEXTLINE(bugprone-parent-virtual-call) int unlucky_number() override { PYBIND11_OVERRIDE(int, Base, unlucky_number, ); } double lucky_number() override { PYBIND11_OVERRIDE(double, Base, lucky_number, ); } }; From af7007331ab5a6f82a1fe38f041b624dea0b4084 Mon Sep 17 00:00:00 2001 From: "Ralf W. Grosse-Kunstleve" Date: Fri, 6 Aug 2021 12:27:11 -0700 Subject: [PATCH 131/638] Removing GCC -Wunused-but-set-parameter from pragma block at the top of pybind11.h (#3164) * Cleanup triggered by work on pragma for GCC -Wunused-but-set-parameter. * Backing out changes to eigen.h (to be worked on later). * Adding PYBIND11_WORKAROUND_INCORRECT_GCC_UNUSED_BUT_SET_PARAMETER in type_caster_base.h (apparently needed only for older GCCs). * Apparently older compilers need a simpler overload for silence_unused_warnings(). * clang C++11 compatibility: removing constexpr * Special case for MSVC 2017: `constexpr void` return * Trying again without the silence_unused_warnings(const int *) overload. * Separate macros for ALL_GCC, OLD_GCC_UNUSED_BUT_SET_PARAMETER * Changing to __GNUC__ <= 2 (turning off) * Refined condition for PYBIND11_WORKAROUND_INCORRECT_OLD_GCC_UNUSED_BUT_SET_PARAMETER. * Quick experiment trying out suggestion by @henryiii * Introducing macro: PYBIND11_INT_ARRAY_WORKING_AROUND_MSVC_CLANG_ISSUES * Trying henryiii@ (void) expander idea. * fix: apply simpler expression with fewer workarounds * Purging new-but-already-obsoleted macro, made possible by @henryiii's commit. * Renaming `ALL_GCC` macro back to just `GCC` (because there is no `OLD` anymore, luckily). * [actions skip] Adding "All GCC versions" to comment, to be clear about it. Co-authored-by: Henry Schreiner --- include/pybind11/attr.h | 23 +++++++++++++------- include/pybind11/cast.h | 5 +++-- include/pybind11/detail/common.h | 25 ++++++++++++++-------- include/pybind11/detail/type_caster_base.h | 9 ++++---- include/pybind11/pybind11.h | 1 - 5 files changed, 38 insertions(+), 25 deletions(-) diff --git a/include/pybind11/attr.h b/include/pybind11/attr.h index 20d119f0ff..7188fd7e3c 100644 --- a/include/pybind11/attr.h +++ b/include/pybind11/attr.h @@ -517,23 +517,30 @@ template struct process_attribute struct process_attributes { static void init(const Args&... args, function_record *r) { PYBIND11_WORKAROUND_INCORRECT_MSVC_C4100(r); - int unused[] = { 0, (process_attribute::type>::init(args, r), 0) ... }; - ignore_unused(unused); + PYBIND11_WORKAROUND_INCORRECT_GCC_UNUSED_BUT_SET_PARAMETER(r); + using expander = int[]; + (void) expander{ + 0, ((void) process_attribute::type>::init(args, r), 0)...}; } static void init(const Args&... args, type_record *r) { PYBIND11_WORKAROUND_INCORRECT_MSVC_C4100(r); - int unused[] = { 0, (process_attribute::type>::init(args, r), 0) ... }; - ignore_unused(unused); + PYBIND11_WORKAROUND_INCORRECT_GCC_UNUSED_BUT_SET_PARAMETER(r); + using expander = int[]; + (void) expander{0, + (process_attribute::type>::init(args, r), 0)...}; } static void precall(function_call &call) { PYBIND11_WORKAROUND_INCORRECT_MSVC_C4100(call); - int unused[] = { 0, (process_attribute::type>::precall(call), 0) ... }; - ignore_unused(unused); + using expander = int[]; + (void) expander{0, + (process_attribute::type>::precall(call), 0)...}; } static void postcall(function_call &call, handle fn_ret) { PYBIND11_WORKAROUND_INCORRECT_MSVC_C4100(call, fn_ret); - int unused[] = { 0, (process_attribute::type>::postcall(call, fn_ret), 0) ... }; - ignore_unused(unused); + PYBIND11_WORKAROUND_INCORRECT_GCC_UNUSED_BUT_SET_PARAMETER(fn_ret); + using expander = int[]; + (void) expander{ + 0, (process_attribute::type>::postcall(call, fn_ret), 0)...}; } }; diff --git a/include/pybind11/cast.h b/include/pybind11/cast.h index 988a236950..718dc2de80 100644 --- a/include/pybind11/cast.h +++ b/include/pybind11/cast.h @@ -609,6 +609,7 @@ template class Tuple, typename... Ts> class tuple_caster template static handle cast_impl(T &&src, return_value_policy policy, handle parent, index_sequence) { PYBIND11_WORKAROUND_INCORRECT_MSVC_C4100(src, policy, parent); + PYBIND11_WORKAROUND_INCORRECT_GCC_UNUSED_BUT_SET_PARAMETER(policy, parent); std::array entries{{ reinterpret_steal(make_caster::cast(std::get(std::forward(src)), policy, parent))... }}; @@ -1235,8 +1236,8 @@ class unpacking_collector { // Tuples aren't (easily) resizable so a list is needed for collection, // but the actual function call strictly requires a tuple. auto args_list = list(); - int _[] = { 0, (process(args_list, std::forward(values)), 0)... }; - ignore_unused(_); + using expander = int[]; + (void) expander{0, (process(args_list, std::forward(values)), 0)...}; m_args = std::move(args_list); } diff --git a/include/pybind11/detail/common.h b/include/pybind11/detail/common.h index 27a79bfdd5..08430b5a2e 100644 --- a/include/pybind11/detail/common.h +++ b/include/pybind11/detail/common.h @@ -734,9 +734,6 @@ using function_signature_t = conditional_t< template using is_lambda = satisfies_none_of, std::is_function, std::is_pointer, std::is_member_pointer>; -/// Ignore that a variable is unused in compiler warnings -inline void ignore_unused(const int *) { } - // [workaround(intel)] Internal error on fold expression /// Apply a function over each element of a parameter pack #if defined(__cpp_fold_expressions) && !defined(__INTEL_COMPILER) @@ -927,19 +924,29 @@ inline static std::shared_ptr try_get_shared_from_this(std::enable_shared_fro #endif } -#if defined(_MSC_VER) && _MSC_VER <= 1916 - -// warning C4100: Unreferenced formal parameter +// For silencing "unused" compiler warnings in special situations. template -inline constexpr void workaround_incorrect_msvc_c4100(Args &&...) {} +#if defined(_MSC_VER) && _MSC_VER >= 1910 && _MSC_VER < 1920 // MSVC 2017 +constexpr +#endif +inline void silence_unused_warnings(Args &&...) {} +// MSVC warning C4100: Unreferenced formal parameter +#if defined(_MSC_VER) && _MSC_VER <= 1916 # define PYBIND11_WORKAROUND_INCORRECT_MSVC_C4100(...) \ - detail::workaround_incorrect_msvc_c4100(__VA_ARGS__) - + detail::silence_unused_warnings(__VA_ARGS__) #else # define PYBIND11_WORKAROUND_INCORRECT_MSVC_C4100(...) #endif +// GCC -Wunused-but-set-parameter All GCC versions (as of July 2021). +#if defined(__GNUG__) && !defined(__clang__) && !defined(__INTEL_COMPILER) +# define PYBIND11_WORKAROUND_INCORRECT_GCC_UNUSED_BUT_SET_PARAMETER(...) \ + detail::silence_unused_warnings(__VA_ARGS__) +#else +# define PYBIND11_WORKAROUND_INCORRECT_GCC_UNUSED_BUT_SET_PARAMETER(...) +#endif + #if defined(_MSC_VER) // All versions (as of July 2021). // warning C4127: Conditional expression is constant diff --git a/include/pybind11/detail/type_caster_base.h b/include/pybind11/detail/type_caster_base.h index e2d1bcb8cc..a708d87fb4 100644 --- a/include/pybind11/detail/type_caster_base.h +++ b/include/pybind11/detail/type_caster_base.h @@ -927,18 +927,17 @@ template class type_caster_base : public type_caster_generic { using Constructor = void *(*)(const void *); /* Only enabled when the types are {copy,move}-constructible *and* when the type - does not have a private operator new implementation. */ + does not have a private operator new implementation. A comma operator is used in the decltype + argument to apply SFINAE to the public copy/move constructors.*/ template ::value>> - static auto make_copy_constructor(const T *x) -> decltype(new T(*x), Constructor{}) { - PYBIND11_WORKAROUND_INCORRECT_MSVC_C4100(x); + static auto make_copy_constructor(const T *) -> decltype(new T(std::declval()), Constructor{}) { return [](const void *arg) -> void * { return new T(*reinterpret_cast(arg)); }; } template ::value>> - static auto make_move_constructor(const T *x) -> decltype(new T(std::move(*const_cast(x))), Constructor{}) { - PYBIND11_WORKAROUND_INCORRECT_MSVC_C4100(x); + static auto make_move_constructor(const T *) -> decltype(new T(std::declval()), Constructor{}) { return [](const void *arg) -> void * { return new T(std::move(*const_cast(reinterpret_cast(arg)))); }; diff --git a/include/pybind11/pybind11.h b/include/pybind11/pybind11.h index 54213297dc..ba29f96474 100644 --- a/include/pybind11/pybind11.h +++ b/include/pybind11/pybind11.h @@ -12,7 +12,6 @@ #if defined(__GNUG__) && !defined(__clang__) && !defined(__INTEL_COMPILER) # pragma GCC diagnostic push -# pragma GCC diagnostic ignored "-Wunused-but-set-parameter" # pragma GCC diagnostic ignored "-Wattributes" #endif From 6ac8efe52dfd8b15caec0e376d677cadd9bec38c Mon Sep 17 00:00:00 2001 From: Eric Cousineau Date: Fri, 6 Aug 2021 15:51:53 -0400 Subject: [PATCH 132/638] test_eval: Show example of working closure (#2743) * test_eval: Show example of working closure * Extend test_eval_closure with weirder examples of closures for py::eval Co-authored-by: Yannick Jadoul Co-authored-by: Aaron Gokaslan --- tests/test_eval.cpp | 18 ++++++++++++++++++ tests/test_eval.py | 17 +++++++++++++++++ 2 files changed, 35 insertions(+) diff --git a/tests/test_eval.cpp b/tests/test_eval.cpp index 16cdf17f22..29366f6798 100644 --- a/tests/test_eval.cpp +++ b/tests/test_eval.cpp @@ -98,4 +98,22 @@ TEST_SUBMODULE(eval_, m) { auto int_class = py::eval("isinstance(42, int)", global); return global; }); + + // test_eval_closure + m.def("test_eval_closure", []() { + py::dict global; + global["closure_value"] = 42; + py::dict local; + local["closure_value"] = 0; + py::exec(R"( + local_value = closure_value + + def func_global(): + return closure_value + + def func_local(): + return local_value + )", global, local); + return std::make_pair(global, local); + }); } diff --git a/tests/test_eval.py b/tests/test_eval.py index 1bb05af05e..601e526f43 100644 --- a/tests/test_eval.py +++ b/tests/test_eval.py @@ -33,3 +33,20 @@ def test_eval_empty_globals(): g = {} assert "__builtins__" in m.eval_empty_globals(g) assert "__builtins__" in g + + +def test_eval_closure(): + global_, local = m.test_eval_closure() + + assert global_["closure_value"] == 42 + assert local["closure_value"] == 0 + + assert "local_value" not in global_ + assert local["local_value"] == 0 + + assert "func_global" not in global_ + assert local["func_global"]() == 42 + + assert "func_local" not in global_ + with pytest.raises(NameError): + local["func_local"]() From 61ee923bb19f38ae7a590ff0e6389d4c62e8360d Mon Sep 17 00:00:00 2001 From: "Ralf W. Grosse-Kunstleve" Date: Sat, 7 Aug 2021 11:51:28 -0700 Subject: [PATCH 133/638] Consistent step name "Python tests". (#3180) --- .github/workflows/ci.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 73a6fd5199..7b0280981d 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -664,7 +664,7 @@ jobs: -DPYTHON_EXECUTABLE=$(python3 -c "import sys; print(sys.executable)") working-directory: /build-tests - - name: Run tests + - name: Python tests run: make pytest -j 2 working-directory: /build-tests @@ -760,7 +760,7 @@ jobs: - name: Build C++11 run: cmake --build build -j 2 - - name: Run tests + - name: Python tests run: cmake --build build -t pytest win32-msvc2015: From ff590c1258abf5b472f64b5ab337c5951922f81d Mon Sep 17 00:00:00 2001 From: Aaron Gokaslan Date: Mon, 9 Aug 2021 12:48:27 -0400 Subject: [PATCH 134/638] maint(perf): Optimize Numpy constructor to remove copies by value. (#3183) * maint(perf): Optimize Numpy Constructor with additional std::move * Add more moves --- include/pybind11/numpy.h | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/include/pybind11/numpy.h b/include/pybind11/numpy.h index aa8294e167..f0f3b60eea 100644 --- a/include/pybind11/numpy.h +++ b/include/pybind11/numpy.h @@ -478,11 +478,11 @@ class dtype : public object { dtype(list names, list formats, list offsets, ssize_t itemsize) { dict args; - args["names"] = names; - args["formats"] = formats; - args["offsets"] = offsets; + args["names"] = std::move(names); + args["formats"] = std::move(formats); + args["offsets"] = std::move(offsets); args["itemsize"] = pybind11::int_(itemsize); - m_ptr = from_args(args).release().ptr(); + m_ptr = from_args(std::move(args)).release().ptr(); } /// This is essentially the same as calling numpy.dtype(args) in Python. @@ -560,7 +560,7 @@ class dtype : public object { formats.append(descr.format); offsets.append(descr.offset); } - return dtype(names, formats, offsets, itemsize); + return dtype(std::move(names), std::move(formats), std::move(offsets), itemsize); } }; @@ -1134,7 +1134,10 @@ inline PYBIND11_NOINLINE void register_structured_dtype( formats.append(field.descr); offsets.append(pybind11::int_(field.offset)); } - auto dtype_ptr = pybind11::dtype(names, formats, offsets, itemsize).release().ptr(); + auto dtype_ptr + = pybind11::dtype(std::move(names), std::move(formats), std::move(offsets), itemsize) + .release() + .ptr(); // There is an existing bug in NumPy (as of v1.11): trailing bytes are // not encoded explicitly into the format string. This will supposedly From 4c7e509fa475d0a0ede726267bc451e4f0583446 Mon Sep 17 00:00:00 2001 From: "Ralf W. Grosse-Kunstleve" Date: Mon, 9 Aug 2021 10:10:38 -0700 Subject: [PATCH 135/638] PYBIND11_NOINLINE-related cleanup. (#3179) * Removing pragma for GCC -Wattributes, fixing forward declarations. * Introducing PYBIND11_NOINLINE_FWD to deal with CUDA, GCC7, GCC8. * Updating PYBIND11_NOINLINE_DCL in Doxyfile. * Trying noinline, noinline for {CUDA, GCC7, GCC8} * Trying noinline, inline for {CUDA, GCC7, GCC8} * Adding GCC -Wattributes `pragma` in 3 header files. * Introducing PYBIND11_NOINLINE_GCC_PRAGMA_ATTRIBUTES_NEEDED, used in 9 header files. * Removing ICC pragma 2196, to see if it is still needed. * Trying noinline, noinline for ICC * Trying noinline, inline for ICC * Restoring ICC pragma 2196, introducing PYBIND11_NOINLINE_FORCED, defined for testing. * Removing code accidentally left in (was for experimentation only). * Removing one-time-test define. * Removing PYBIND11_NOINLINE_FWD macro (after learning that it makes no sense). * Testing with PYBIND11_NOINLINE_DISABLED. Minor non-functional enhancements. * Removing #define PYBIND11_NOINLINE_DISABLED (test was successful). * Removing PYBIND11_NOINLINE_FORCED and enhancing comments for PYBIND11_NOINLINE. * WIP stripping back * Making -Wattributes pragma in pybind11 specific to GCC7, GCC8, CUDA. --- include/pybind11/attr.h | 2 +- include/pybind11/detail/common.h | 15 +++++++++----- include/pybind11/detail/internals.h | 6 +++--- include/pybind11/detail/type_caster_base.h | 24 +++++++++++----------- include/pybind11/detail/typeid.h | 2 +- include/pybind11/numpy.h | 4 ++-- include/pybind11/pybind11.h | 10 ++++----- include/pybind11/pytypes.h | 4 ++-- 8 files changed, 36 insertions(+), 31 deletions(-) diff --git a/include/pybind11/attr.h b/include/pybind11/attr.h index 7188fd7e3c..ab1fe80446 100644 --- a/include/pybind11/attr.h +++ b/include/pybind11/attr.h @@ -124,7 +124,7 @@ enum op_id : int; enum op_type : int; struct undefined_t; template struct op_; -inline void keep_alive_impl(size_t Nurse, size_t Patient, function_call &call, handle ret); +void keep_alive_impl(size_t Nurse, size_t Patient, function_call &call, handle ret); /// Internal data structure which holds metadata about a keyword argument struct argument_record { diff --git a/include/pybind11/detail/common.h b/include/pybind11/detail/common.h index 08430b5a2e..cc24a52fd3 100644 --- a/include/pybind11/detail/common.h +++ b/include/pybind11/detail/common.h @@ -99,10 +99,15 @@ # endif #endif -#if defined(_MSC_VER) -# define PYBIND11_NOINLINE __declspec(noinline) +// The PYBIND11_NOINLINE macro is for function DEFINITIONS. +// In contrast, FORWARD DECLARATIONS should never use this macro: +// https://stackoverflow.com/questions/9317473/forward-declaration-of-inline-functions +#if defined(PYBIND11_NOINLINE_DISABLED) // Option for maximum portability and experimentation. +# define PYBIND11_NOINLINE inline +#elif defined(_MSC_VER) +# define PYBIND11_NOINLINE __declspec(noinline) inline #else -# define PYBIND11_NOINLINE __attribute__ ((noinline)) +# define PYBIND11_NOINLINE __attribute__ ((noinline)) inline #endif #if defined(__MINGW32__) @@ -778,8 +783,8 @@ PYBIND11_RUNTIME_EXCEPTION(import_error, PyExc_ImportError) PYBIND11_RUNTIME_EXCEPTION(cast_error, PyExc_RuntimeError) /// Thrown when pybind11::cast or handle::call fail due to a type casting error PYBIND11_RUNTIME_EXCEPTION(reference_cast_error, PyExc_RuntimeError) /// Used internally -[[noreturn]] PYBIND11_NOINLINE inline void pybind11_fail(const char *reason) { throw std::runtime_error(reason); } -[[noreturn]] PYBIND11_NOINLINE inline void pybind11_fail(const std::string &reason) { throw std::runtime_error(reason); } +[[noreturn]] PYBIND11_NOINLINE void pybind11_fail(const char *reason) { throw std::runtime_error(reason); } +[[noreturn]] PYBIND11_NOINLINE void pybind11_fail(const std::string &reason) { throw std::runtime_error(reason); } template struct format_descriptor { }; diff --git a/include/pybind11/detail/internals.h b/include/pybind11/detail/internals.h index a93a83e268..b177801a11 100644 --- a/include/pybind11/detail/internals.h +++ b/include/pybind11/detail/internals.h @@ -257,7 +257,7 @@ inline void translate_local_exception(std::exception_ptr p) { #endif /// Return a reference to the current `internals` data -PYBIND11_NOINLINE inline internals &get_internals() { +PYBIND11_NOINLINE internals &get_internals() { auto **&internals_pp = get_internals_pp(); if (internals_pp && *internals_pp) return **internals_pp; @@ -352,14 +352,14 @@ PYBIND11_NAMESPACE_END(detail) /// Returns a named pointer that is shared among all extension modules (using the same /// pybind11 version) running in the current interpreter. Names starting with underscores /// are reserved for internal usage. Returns `nullptr` if no matching entry was found. -inline PYBIND11_NOINLINE void *get_shared_data(const std::string &name) { +PYBIND11_NOINLINE void *get_shared_data(const std::string &name) { auto &internals = detail::get_internals(); auto it = internals.shared_data.find(name); return it != internals.shared_data.end() ? it->second : nullptr; } /// Set the shared data that can be later recovered by `get_shared_data()`. -inline PYBIND11_NOINLINE void *set_shared_data(const std::string &name, void *data) { +PYBIND11_NOINLINE void *set_shared_data(const std::string &name, void *data) { detail::get_internals().shared_data[name] = data; return data; } diff --git a/include/pybind11/detail/type_caster_base.h b/include/pybind11/detail/type_caster_base.h index a708d87fb4..14cb27cad2 100644 --- a/include/pybind11/detail/type_caster_base.h +++ b/include/pybind11/detail/type_caster_base.h @@ -81,7 +81,7 @@ class loader_life_support { inline std::pair all_type_info_get_cache(PyTypeObject *type); // Populates a just-created cache entry. -PYBIND11_NOINLINE inline void all_type_info_populate(PyTypeObject *t, std::vector &bases) { +PYBIND11_NOINLINE void all_type_info_populate(PyTypeObject *t, std::vector &bases) { std::vector check; for (handle parent : reinterpret_borrow(t->tp_bases)) check.push_back((PyTypeObject *) parent.ptr()); @@ -150,7 +150,7 @@ inline const std::vector &all_type_info(PyTypeObject *type) * ancestors are pybind11-registered. Throws an exception if there are multiple bases--use * `all_type_info` instead if you want to support multiple bases. */ -PYBIND11_NOINLINE inline detail::type_info* get_type_info(PyTypeObject *type) { +PYBIND11_NOINLINE detail::type_info* get_type_info(PyTypeObject *type) { auto &bases = all_type_info(type); if (bases.empty()) return nullptr; @@ -176,7 +176,7 @@ inline detail::type_info *get_global_type_info(const std::type_index &tp) { } /// Return the type info for a given C++ type; on lookup failure can either throw or return nullptr. -PYBIND11_NOINLINE inline detail::type_info *get_type_info(const std::type_index &tp, +PYBIND11_NOINLINE detail::type_info *get_type_info(const std::type_index &tp, bool throw_if_missing = false) { if (auto ltype = get_local_type_info(tp)) return ltype; @@ -191,13 +191,13 @@ PYBIND11_NOINLINE inline detail::type_info *get_type_info(const std::type_index return nullptr; } -PYBIND11_NOINLINE inline handle get_type_handle(const std::type_info &tp, bool throw_if_missing) { +PYBIND11_NOINLINE handle get_type_handle(const std::type_info &tp, bool throw_if_missing) { detail::type_info *type_info = get_type_info(tp, throw_if_missing); return handle(type_info ? ((PyObject *) type_info->type) : nullptr); } // Searches the inheritance graph for a registered Python instance, using all_type_info(). -PYBIND11_NOINLINE inline handle find_registered_python_instance(void *src, +PYBIND11_NOINLINE handle find_registered_python_instance(void *src, const detail::type_info *tinfo) { auto it_instances = get_internals().registered_instances.equal_range(src); for (auto it_i = it_instances.first; it_i != it_instances.second; ++it_i) { @@ -325,7 +325,7 @@ struct values_and_holders { * The returned object should be short-lived: in particular, it must not outlive the called-upon * instance. */ -PYBIND11_NOINLINE inline value_and_holder instance::get_value_and_holder(const type_info *find_type /*= nullptr default in common.h*/, bool throw_if_missing /*= true in common.h*/) { +PYBIND11_NOINLINE value_and_holder instance::get_value_and_holder(const type_info *find_type /*= nullptr default in common.h*/, bool throw_if_missing /*= true in common.h*/) { // Optimize common case: if (!find_type || Py_TYPE(this) == find_type->type) return value_and_holder(this, find_type, 0, 0); @@ -349,7 +349,7 @@ PYBIND11_NOINLINE inline value_and_holder instance::get_value_and_holder(const t #endif } -PYBIND11_NOINLINE inline void instance::allocate_layout() { +PYBIND11_NOINLINE void instance::allocate_layout() { auto &tinfo = all_type_info(Py_TYPE(this)); const size_t n_types = tinfo.size(); @@ -397,19 +397,19 @@ PYBIND11_NOINLINE inline void instance::allocate_layout() { owned = true; } -PYBIND11_NOINLINE inline void instance::deallocate_layout() const { +PYBIND11_NOINLINE void instance::deallocate_layout() const { if (!simple_layout) PyMem_Free(nonsimple.values_and_holders); } -PYBIND11_NOINLINE inline bool isinstance_generic(handle obj, const std::type_info &tp) { +PYBIND11_NOINLINE bool isinstance_generic(handle obj, const std::type_info &tp) { handle type = detail::get_type_handle(tp, false); if (!type) return false; return isinstance(obj, type); } -PYBIND11_NOINLINE inline std::string error_string() { +PYBIND11_NOINLINE std::string error_string() { if (!PyErr_Occurred()) { PyErr_SetString(PyExc_RuntimeError, "Unknown internal error occurred"); return "Unknown internal error occurred"; @@ -456,7 +456,7 @@ PYBIND11_NOINLINE inline std::string error_string() { return errorString; } -PYBIND11_NOINLINE inline handle get_object_handle(const void *ptr, const detail::type_info *type ) { +PYBIND11_NOINLINE handle get_object_handle(const void *ptr, const detail::type_info *type ) { auto &instances = get_internals().registered_instances; auto range = instances.equal_range(ptr); for (auto it = range.first; it != range.second; ++it) { @@ -483,7 +483,7 @@ inline PyThreadState *get_thread_state_unchecked() { } // Forward declarations -inline void keep_alive_impl(handle nurse, handle patient); +void keep_alive_impl(handle nurse, handle patient); inline PyObject *make_new_instance(PyTypeObject *type); class type_caster_generic { diff --git a/include/pybind11/detail/typeid.h b/include/pybind11/detail/typeid.h index 148889ffef..39ba8ce0f7 100644 --- a/include/pybind11/detail/typeid.h +++ b/include/pybind11/detail/typeid.h @@ -29,7 +29,7 @@ inline void erase_all(std::string &string, const std::string &search) { } } -PYBIND11_NOINLINE inline void clean_type_id(std::string &name) { +PYBIND11_NOINLINE void clean_type_id(std::string &name) { #if defined(__GNUG__) int status = 0; std::unique_ptr res { diff --git a/include/pybind11/numpy.h b/include/pybind11/numpy.h index f0f3b60eea..2dc0f29f0b 100644 --- a/include/pybind11/numpy.h +++ b/include/pybind11/numpy.h @@ -104,7 +104,7 @@ struct numpy_internals { } }; -inline PYBIND11_NOINLINE void load_numpy_internals(numpy_internals* &ptr) { +PYBIND11_NOINLINE void load_numpy_internals(numpy_internals* &ptr) { ptr = &get_or_create_shared_data("_numpy_internals"); } @@ -1110,7 +1110,7 @@ struct field_descriptor { dtype descr; }; -inline PYBIND11_NOINLINE void register_structured_dtype( +PYBIND11_NOINLINE void register_structured_dtype( any_container fields, const std::type_info& tinfo, ssize_t itemsize, bool (*direct_converter)(PyObject *, void *&)) { diff --git a/include/pybind11/pybind11.h b/include/pybind11/pybind11.h index ba29f96474..20df6a01fd 100644 --- a/include/pybind11/pybind11.h +++ b/include/pybind11/pybind11.h @@ -10,7 +10,7 @@ #pragma once -#if defined(__GNUG__) && !defined(__clang__) && !defined(__INTEL_COMPILER) +#if defined(__CUDACC__) || (defined(__GNUC__) && (__GNUC__ == 7 || __GNUC__ == 8)) # pragma GCC diagnostic push # pragma GCC diagnostic ignored "-Wattributes" #endif @@ -1875,7 +1875,7 @@ template class enum_ : public class_ { PYBIND11_NAMESPACE_BEGIN(detail) -inline void keep_alive_impl(handle nurse, handle patient) { +PYBIND11_NOINLINE void keep_alive_impl(handle nurse, handle patient) { if (!nurse || !patient) pybind11_fail("Could not activate keep_alive!"); @@ -1902,7 +1902,7 @@ inline void keep_alive_impl(handle nurse, handle patient) { } } -PYBIND11_NOINLINE inline void keep_alive_impl(size_t Nurse, size_t Patient, function_call &call, handle ret) { +PYBIND11_NOINLINE void keep_alive_impl(size_t Nurse, size_t Patient, function_call &call, handle ret) { auto get_arg = [&](size_t n) { if (n == 0) return ret; @@ -2152,7 +2152,7 @@ exception ®ister_local_exception(handle scope, } PYBIND11_NAMESPACE_BEGIN(detail) -PYBIND11_NOINLINE inline void print(const tuple &args, const dict &kwargs) { +PYBIND11_NOINLINE void print(const tuple &args, const dict &kwargs) { auto strings = tuple(args.size()); for (size_t i = 0; i < args.size(); ++i) { strings[i] = str(args[i]); @@ -2384,6 +2384,6 @@ PYBIND11_NAMESPACE_END(PYBIND11_NAMESPACE) # pragma GCC diagnostic pop // -Wnoexcept-type #endif -#if defined(__GNUG__) && !defined(__clang__) && !defined(__INTEL_COMPILER) +#if defined(__CUDACC__) || (defined(__GNUC__) && (__GNUC__ == 7 || __GNUC__ == 8)) # pragma GCC diagnostic pop #endif diff --git a/include/pybind11/pytypes.h b/include/pybind11/pytypes.h index c7b2501feb..21e14960cf 100644 --- a/include/pybind11/pytypes.h +++ b/include/pybind11/pytypes.h @@ -24,7 +24,7 @@ struct arg; struct arg_v; PYBIND11_NAMESPACE_BEGIN(detail) class args_proxy; -inline bool isinstance_generic(handle obj, const std::type_info &tp); +bool isinstance_generic(handle obj, const std::type_info &tp); // Accessor forward declarations template class accessor; @@ -316,7 +316,7 @@ template T reinterpret_borrow(handle h) { return {h, object::borrow template T reinterpret_steal(handle h) { return {h, object::stolen_t{}}; } PYBIND11_NAMESPACE_BEGIN(detail) -inline std::string error_string(); +std::string error_string(); PYBIND11_NAMESPACE_END(detail) #if defined(_MSC_VER) From 0be2ea065eeaf2983964ecb792d30abbcedcdc9d Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 9 Aug 2021 15:08:19 -0400 Subject: [PATCH 136/638] [pre-commit.ci] pre-commit autoupdate (#3185) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit updates: - [github.com/asottile/pyupgrade: v2.23.1 → v2.23.3](https://github.com/asottile/pyupgrade/compare/v2.23.1...v2.23.3) Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> --- .pre-commit-config.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 57beb77793..ee61258d3f 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -31,7 +31,7 @@ repos: exclude: ^noxfile.py$ - repo: https://github.com/asottile/pyupgrade - rev: v2.23.1 + rev: v2.23.3 hooks: - id: pyupgrade From 7d3b05715bf1c8f9f7232723671bf9b780a173ac Mon Sep 17 00:00:00 2001 From: "Ralf W. Grosse-Kunstleve" Date: Thu, 12 Aug 2021 13:21:49 -0700 Subject: [PATCH 137/638] Improved workaround for Centos 8 failure (follow-on to PR #3030). (#3193) * Rollback of PR #3030 (Working around Centos 8 failure). * Adding `-DCMAKE_BUILD_TYPE=Release` * Improving existing workaround (undoing the originally intended rollback). * Fixing minor documentation bug. --- .github/workflows/ci.yml | 6 +++--- tools/pybind11Config.cmake.in | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 7b0280981d..e1c32b96f0 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -594,13 +594,13 @@ jobs: - name: VAR_BUILD_TYPE 7 if: matrix.centos == 7 - run: echo Release > VAR_BUILD_TYPE + run: echo MinSizeRel > VAR_BUILD_TYPE - # Using Debug to avoid segfault that appeared around 2021-06-04, + # Using Release to avoid segfault that appeared around 2021-06-04, # apparently when the gcc version changed from 8.3 to 8.4. - name: VAR_BUILD_TYPE 8 if: matrix.centos == 8 - run: echo Debug > VAR_BUILD_TYPE + run: echo Release > VAR_BUILD_TYPE - name: Configure shell: bash diff --git a/tools/pybind11Config.cmake.in b/tools/pybind11Config.cmake.in index 73ec104a0e..8f8701be70 100644 --- a/tools/pybind11Config.cmake.in +++ b/tools/pybind11Config.cmake.in @@ -147,7 +147,7 @@ Add a module and setup all helpers. You can select the type of the library; the default is ``MODULE``. There are several options: ``OPT_SIZE`` - Optimize for size, even if the ``CMAKE_BUILD_TYPE`` is not ``RelSize``. + Optimize for size, even if the ``CMAKE_BUILD_TYPE`` is not ``MinSizeRel``. ``THIN_LTO`` Use thin TLO instead of regular if there's a choice (pybind11's selection is disabled if ``CMAKE_INTERPROCEDURAL_OPTIMIZATIONS`` is set). From d6841f60cc6a07105222d3650caa14ef2007f949 Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Fri, 13 Aug 2021 12:27:54 -0400 Subject: [PATCH 138/638] chore(deps): bump jwlawson/actions-setup-cmake from 1.9 to 1.10 (#3196) Bumps [jwlawson/actions-setup-cmake](https://github.com/jwlawson/actions-setup-cmake) from 1.9 to 1.10. - [Release notes](https://github.com/jwlawson/actions-setup-cmake/releases) - [Commits](https://github.com/jwlawson/actions-setup-cmake/compare/v1.9...v1.10) --- updated-dependencies: - dependency-name: jwlawson/actions-setup-cmake dependency-type: direct:production update-type: version-update:semver-minor ... Signed-off-by: dependabot[bot] Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> --- .github/workflows/ci.yml | 12 ++++++------ .github/workflows/configure.yml | 2 +- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index e1c32b96f0..be3fa723c9 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -75,7 +75,7 @@ jobs: run: brew install boost - name: Update CMake - uses: jwlawson/actions-setup-cmake@v1.9 + uses: jwlawson/actions-setup-cmake@v1.10 - name: Cache wheels if: runner.os == 'macOS' @@ -193,7 +193,7 @@ jobs: debug: ${{ matrix.python-debug }} - name: Update CMake - uses: jwlawson/actions-setup-cmake@v1.9 + uses: jwlawson/actions-setup-cmake@v1.10 - name: Valgrind cache if: matrix.valgrind @@ -445,7 +445,7 @@ jobs: run: python3 -m pip install --upgrade pip - name: Update CMake - uses: jwlawson/actions-setup-cmake@v1.9 + uses: jwlawson/actions-setup-cmake@v1.10 - name: Configure shell: bash @@ -738,7 +738,7 @@ jobs: architecture: x86 - name: Update CMake - uses: jwlawson/actions-setup-cmake@v1.9 + uses: jwlawson/actions-setup-cmake@v1.10 - name: Prepare MSVC uses: ilammy/msvc-dev-cmd@v1.9.0 @@ -784,7 +784,7 @@ jobs: python-version: ${{ matrix.python }} - name: Update CMake - uses: jwlawson/actions-setup-cmake@v1.9 + uses: jwlawson/actions-setup-cmake@v1.10 - name: Prepare MSVC uses: ilammy/msvc-dev-cmd@v1.9.0 @@ -838,7 +838,7 @@ jobs: python-version: ${{ matrix.python }} - name: Update CMake - uses: jwlawson/actions-setup-cmake@v1.9 + uses: jwlawson/actions-setup-cmake@v1.10 - name: Prepare env run: python -m pip install -r tests/requirements.txt --prefer-binary diff --git a/.github/workflows/configure.yml b/.github/workflows/configure.yml index 924088fc37..d37fa3c2cc 100644 --- a/.github/workflows/configure.yml +++ b/.github/workflows/configure.yml @@ -55,7 +55,7 @@ jobs: # An action for adding a specific version of CMake: # https://github.com/jwlawson/actions-setup-cmake - name: Setup CMake ${{ matrix.cmake }} - uses: jwlawson/actions-setup-cmake@v1.9 + uses: jwlawson/actions-setup-cmake@v1.10 with: cmake-version: ${{ matrix.cmake }} From 9df2f1ff13ee37c312b5f04fcb77f4339f92b015 Mon Sep 17 00:00:00 2001 From: Aaron Gokaslan Date: Fri, 13 Aug 2021 12:37:05 -0400 Subject: [PATCH 139/638] maint(precommit): Apply isort (#3195) * Apply isort * Tweak isort config * Add env.py as a known_first_party * Add one missing known first party * Make config compat with older isort versions * Add another comment * Revert pyproject setting --- .pre-commit-config.yaml | 5 +++++ docs/benchmark.py | 4 ++-- docs/conf.py | 4 ++-- noxfile.py | 1 - pybind11/__init__.py | 5 ++--- pybind11/__main__.py | 2 +- pybind11/_version.pyi | 2 +- pybind11/commands.py | 1 - pybind11/setup_helpers.py | 8 ++++---- pybind11/setup_helpers.pyi | 9 ++++----- pyproject.toml | 6 ++++++ setup.py | 2 +- tests/extra_setuptools/test_setuphelper.py | 2 +- tests/test_buffers.py | 5 ++--- tests/test_builtin_casters.py | 3 +-- tests/test_call_policies.py | 3 +-- tests/test_callbacks.py | 8 +++++--- tests/test_chrono.py | 3 ++- tests/test_class.py | 3 +-- tests/test_cmake_build/test.py | 1 + tests/test_copy_move.py | 1 + tests/test_custom_type_casters.py | 1 + tests/test_eigen.py | 1 + tests/test_enum.py | 1 + tests/test_eval.py | 1 - tests/test_exceptions.py | 3 +-- tests/test_factory_constructors.py | 6 +++--- tests/test_iostream.py | 4 ++-- tests/test_kwargs_and_defaults.py | 1 - tests/test_local_bindings.py | 1 - tests/test_methods_and_attributes.py | 3 +-- tests/test_modules.py | 8 +++++--- tests/test_multiple_inheritance.py | 1 - tests/test_numpy_array.py | 1 - tests/test_numpy_dtypes.py | 1 - tests/test_numpy_vectorize.py | 1 + tests/test_opaque_types.py | 3 ++- tests/test_operator_overloading.py | 3 ++- tests/test_pickling.py | 1 - tests/test_pytypes.py | 7 ++++--- tests/test_sequences_and_iterators.py | 3 ++- tests/test_stl.py | 3 +-- tests/test_stl_binders.py | 1 - tools/libsize.py | 3 ++- tools/make_changelog.py | 2 -- 45 files changed, 72 insertions(+), 66 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index ee61258d3f..a5900df162 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -35,6 +35,11 @@ repos: hooks: - id: pyupgrade +- repo: https://github.com/PyCQA/isort + rev: 5.9.3 + hooks: + - id: isort + # Black, the code formatter, natively supports pre-commit - repo: https://github.com/psf/black rev: 21.7b0 diff --git a/docs/benchmark.py b/docs/benchmark.py index 33d78fb4e6..369470c81f 100644 --- a/docs/benchmark.py +++ b/docs/benchmark.py @@ -1,8 +1,8 @@ # -*- coding: utf-8 -*- -import random +import datetime as dt import os +import random import time -import datetime as dt nfns = 4 # Functions per class nargs = 4 # Arguments per function diff --git a/docs/conf.py b/docs/conf.py index 6ac054c6b8..458a868866 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -13,12 +13,12 @@ # All configuration values have a default; values that are commented out # serve to show the default. -import sys import os +import re import shlex import subprocess +import sys from pathlib import Path -import re DIR = Path(__file__).parent.resolve() diff --git a/noxfile.py b/noxfile.py index f0f6680159..55b1d180b2 100644 --- a/noxfile.py +++ b/noxfile.py @@ -1,6 +1,5 @@ import nox - nox.options.sessions = ["lint", "tests", "tests_packaging"] diff --git a/pybind11/__init__.py b/pybind11/__init__.py index ad65420893..64e999ba06 100644 --- a/pybind11/__init__.py +++ b/pybind11/__init__.py @@ -1,8 +1,7 @@ # -*- coding: utf-8 -*- -from ._version import version_info, __version__ -from .commands import get_include, get_cmake_dir - +from ._version import __version__, version_info +from .commands import get_cmake_dir, get_include __all__ = ( "version_info", diff --git a/pybind11/__main__.py b/pybind11/__main__.py index 020988c671..3235747bed 100644 --- a/pybind11/__main__.py +++ b/pybind11/__main__.py @@ -5,7 +5,7 @@ import sys import sysconfig -from .commands import get_include, get_cmake_dir +from .commands import get_cmake_dir, get_include def print_includes(): diff --git a/pybind11/_version.pyi b/pybind11/_version.pyi index 970184c770..d45e5dc907 100644 --- a/pybind11/_version.pyi +++ b/pybind11/_version.pyi @@ -1,4 +1,4 @@ -from typing import Union, Tuple +from typing import Tuple, Union def _to_int(s: str) -> Union[int, str]: ... diff --git a/pybind11/commands.py b/pybind11/commands.py index 34dbaf8a89..11f81d2d6d 100644 --- a/pybind11/commands.py +++ b/pybind11/commands.py @@ -1,7 +1,6 @@ # -*- coding: utf-8 -*- import os - DIR = os.path.abspath(os.path.dirname(__file__)) diff --git a/pybind11/setup_helpers.py b/pybind11/setup_helpers.py index 0888ab4872..4ff1a0cb37 100644 --- a/pybind11/setup_helpers.py +++ b/pybind11/setup_helpers.py @@ -41,23 +41,23 @@ import contextlib import os +import platform import shutil import sys +import sysconfig import tempfile import threading -import platform import warnings -import sysconfig try: - from setuptools.command.build_ext import build_ext as _build_ext from setuptools import Extension as _Extension + from setuptools.command.build_ext import build_ext as _build_ext except ImportError: from distutils.command.build_ext import build_ext as _build_ext from distutils.extension import Extension as _Extension -import distutils.errors import distutils.ccompiler +import distutils.errors WIN = sys.platform.startswith("win32") and "mingw" not in sysconfig.get_platform() PY2 = sys.version_info[0] < 3 diff --git a/pybind11/setup_helpers.pyi b/pybind11/setup_helpers.pyi index 8b16c0a137..074744eb82 100644 --- a/pybind11/setup_helpers.pyi +++ b/pybind11/setup_helpers.pyi @@ -1,13 +1,12 @@ # IMPORTANT: Should stay in sync with setup_helpers.py (mostly checked by CI / # pre-commit). -from typing import Any, Callable, Dict, Iterator, List, Optional, Type, TypeVar, Union -from types import TracebackType - +import contextlib +import distutils.ccompiler from distutils.command.build_ext import build_ext as _build_ext # type: ignore from distutils.extension import Extension as _Extension -import distutils.ccompiler -import contextlib +from types import TracebackType +from typing import Any, Callable, Dict, Iterator, List, Optional, Type, TypeVar, Union WIN: bool PY2: bool diff --git a/pyproject.toml b/pyproject.toml index f86470eef8..7d7a1c8213 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -15,6 +15,12 @@ ignore = [ "noxfile.py", ] +[tool.isort] +# Needs the compiled .so modules and env.py from tests +known_first_party = "env,pybind11_cross_module_tests,pybind11_tests," +# For black compatibility +profile = "black" + [tool.mypy] files = "pybind11" python_version = "2.7" diff --git a/setup.py b/setup.py index 7aa151c01b..a2326287d8 100644 --- a/setup.py +++ b/setup.py @@ -4,6 +4,7 @@ # Setup script for PyPI; use CMakeFile.txt to build extension modules import contextlib +import io import os import re import shutil @@ -11,7 +12,6 @@ import subprocess import sys import tempfile -import io import setuptools.command.sdist diff --git a/tests/extra_setuptools/test_setuphelper.py b/tests/extra_setuptools/test_setuphelper.py index fb2d342086..c24f50af8a 100644 --- a/tests/extra_setuptools/test_setuphelper.py +++ b/tests/extra_setuptools/test_setuphelper.py @@ -1,7 +1,7 @@ # -*- coding: utf-8 -*- import os -import sys import subprocess +import sys from textwrap import dedent import pytest diff --git a/tests/test_buffers.py b/tests/test_buffers.py index 50845758b8..e3df7e04f9 100644 --- a/tests/test_buffers.py +++ b/tests/test_buffers.py @@ -1,14 +1,13 @@ # -*- coding: utf-8 -*- +import ctypes import io import struct -import ctypes import pytest import env # noqa: F401 - -from pybind11_tests import buffers as m from pybind11_tests import ConstructorStats +from pybind11_tests import buffers as m np = pytest.importorskip("numpy") diff --git a/tests/test_builtin_casters.py b/tests/test_builtin_casters.py index 1b9dbb2b40..09b4b08c5e 100644 --- a/tests/test_builtin_casters.py +++ b/tests/test_builtin_casters.py @@ -2,9 +2,8 @@ import pytest import env # noqa: F401 - +from pybind11_tests import IncType, UserType from pybind11_tests import builtin_casters as m -from pybind11_tests import UserType, IncType def test_simple_string(): diff --git a/tests/test_call_policies.py b/tests/test_call_policies.py index af017e9d0c..3599cf81af 100644 --- a/tests/test_call_policies.py +++ b/tests/test_call_policies.py @@ -2,9 +2,8 @@ import pytest import env # noqa: F401 - -from pybind11_tests import call_policies as m from pybind11_tests import ConstructorStats +from pybind11_tests import call_policies as m @pytest.mark.xfail("env.PYPY", reason="sometimes comes out 1 off on PyPy", strict=False) diff --git a/tests/test_callbacks.py b/tests/test_callbacks.py index 5bc4d1773d..9dc272a2da 100644 --- a/tests/test_callbacks.py +++ b/tests/test_callbacks.py @@ -1,9 +1,11 @@ # -*- coding: utf-8 -*- -import pytest -from pybind11_tests import callbacks as m -from threading import Thread import time +from threading import Thread + +import pytest + import env # NOQA: F401 +from pybind11_tests import callbacks as m def test_callbacks(): diff --git a/tests/test_chrono.py b/tests/test_chrono.py index 12312c7207..fdd73d6908 100644 --- a/tests/test_chrono.py +++ b/tests/test_chrono.py @@ -1,9 +1,10 @@ # -*- coding: utf-8 -*- -from pybind11_tests import chrono as m import datetime + import pytest import env # noqa: F401 +from pybind11_tests import chrono as m def test_chrono_system_clock(): diff --git a/tests/test_class.py b/tests/test_class.py index 1ff60fe214..85d4531991 100644 --- a/tests/test_class.py +++ b/tests/test_class.py @@ -2,9 +2,8 @@ import pytest import env # noqa: F401 - +from pybind11_tests import ConstructorStats, UserType from pybind11_tests import class_ as m -from pybind11_tests import UserType, ConstructorStats def test_repr(): diff --git a/tests/test_cmake_build/test.py b/tests/test_cmake_build/test.py index 87ed5135ff..d1a290dcc5 100644 --- a/tests/test_cmake_build/test.py +++ b/tests/test_cmake_build/test.py @@ -1,5 +1,6 @@ # -*- coding: utf-8 -*- import sys + import test_cmake_build assert test_cmake_build.add(1, 2) == 3 diff --git a/tests/test_copy_move.py b/tests/test_copy_move.py index 1d98952200..eb1efddd50 100644 --- a/tests/test_copy_move.py +++ b/tests/test_copy_move.py @@ -1,5 +1,6 @@ # -*- coding: utf-8 -*- import pytest + from pybind11_tests import copy_move_policies as m diff --git a/tests/test_custom_type_casters.py b/tests/test_custom_type_casters.py index bb74d54eb3..a10646ff46 100644 --- a/tests/test_custom_type_casters.py +++ b/tests/test_custom_type_casters.py @@ -1,5 +1,6 @@ # -*- coding: utf-8 -*- import pytest + from pybind11_tests import custom_type_casters as m diff --git a/tests/test_eigen.py b/tests/test_eigen.py index a131dc15d6..e53826cbbb 100644 --- a/tests/test_eigen.py +++ b/tests/test_eigen.py @@ -1,5 +1,6 @@ # -*- coding: utf-8 -*- import pytest + from pybind11_tests import ConstructorStats np = pytest.importorskip("numpy") diff --git a/tests/test_enum.py b/tests/test_enum.py index e9732fa74f..62f9426eee 100644 --- a/tests/test_enum.py +++ b/tests/test_enum.py @@ -1,5 +1,6 @@ # -*- coding: utf-8 -*- import pytest + from pybind11_tests import enums as m diff --git a/tests/test_eval.py b/tests/test_eval.py index 601e526f43..1bbd991bc0 100644 --- a/tests/test_eval.py +++ b/tests/test_eval.py @@ -4,7 +4,6 @@ import pytest import env # noqa: F401 - from pybind11_tests import eval_ as m diff --git a/tests/test_exceptions.py b/tests/test_exceptions.py index 966ae07fc0..d1edc39f0e 100644 --- a/tests/test_exceptions.py +++ b/tests/test_exceptions.py @@ -4,9 +4,8 @@ import pytest import env # noqa: F401 - -from pybind11_tests import exceptions as m import pybind11_cross_module_tests as cm +from pybind11_tests import exceptions as m def test_std_exception(msg): diff --git a/tests/test_factory_constructors.py b/tests/test_factory_constructors.py index 8c6ca173ac..8bc0269852 100644 --- a/tests/test_factory_constructors.py +++ b/tests/test_factory_constructors.py @@ -1,12 +1,12 @@ # -*- coding: utf-8 -*- -import pytest import re -import env # noqa: F401 +import pytest +import env # noqa: F401 +from pybind11_tests import ConstructorStats from pybind11_tests import factory_constructors as m from pybind11_tests.factory_constructors import tag -from pybind11_tests import ConstructorStats def test_init_factory_basic(): diff --git a/tests/test_iostream.py b/tests/test_iostream.py index e2b74d01cb..7f18ca65c6 100644 --- a/tests/test_iostream.py +++ b/tests/test_iostream.py @@ -1,9 +1,9 @@ # -*- coding: utf-8 -*- -from pybind11_tests import iostream as m import sys - from contextlib import contextmanager +from pybind11_tests import iostream as m + try: # Python 3 from io import StringIO diff --git a/tests/test_kwargs_and_defaults.py b/tests/test_kwargs_and_defaults.py index 12fe705b4d..ddc387eeb9 100644 --- a/tests/test_kwargs_and_defaults.py +++ b/tests/test_kwargs_and_defaults.py @@ -2,7 +2,6 @@ import pytest import env # noqa: F401 - from pybind11_tests import kwargs_and_defaults as m diff --git a/tests/test_local_bindings.py b/tests/test_local_bindings.py index a38564b6e1..52b1b63358 100644 --- a/tests/test_local_bindings.py +++ b/tests/test_local_bindings.py @@ -2,7 +2,6 @@ import pytest import env # noqa: F401 - from pybind11_tests import local_bindings as m diff --git a/tests/test_methods_and_attributes.py b/tests/test_methods_and_attributes.py index 750ec02f89..21909654a5 100644 --- a/tests/test_methods_and_attributes.py +++ b/tests/test_methods_and_attributes.py @@ -2,9 +2,8 @@ import pytest import env # noqa: F401 - -from pybind11_tests import methods_and_attributes as m from pybind11_tests import ConstructorStats +from pybind11_tests import methods_and_attributes as m def test_methods_and_attributes(): diff --git a/tests/test_modules.py b/tests/test_modules.py index 3390031aff..49e1ea5e30 100644 --- a/tests/test_modules.py +++ b/tests/test_modules.py @@ -1,7 +1,7 @@ # -*- coding: utf-8 -*- +from pybind11_tests import ConstructorStats from pybind11_tests import modules as m from pybind11_tests.modules import subsubmodule as ms -from pybind11_tests import ConstructorStats def test_nested_modules(): @@ -54,18 +54,20 @@ def test_reference_internal(): def test_importing(): - from pybind11_tests.modules import OD from collections import OrderedDict + from pybind11_tests.modules import OD + assert OD is OrderedDict assert str(OD([(1, "a"), (2, "b")])) == "OrderedDict([(1, 'a'), (2, 'b')])" def test_pydoc(): """Pydoc needs to be able to provide help() for everything inside a pybind11 module""" - import pybind11_tests import pydoc + import pybind11_tests + assert pybind11_tests.__name__ == "pybind11_tests" assert pybind11_tests.__doc__ == "pybind11 test module" assert pydoc.text.docmodule(pybind11_tests) diff --git a/tests/test_multiple_inheritance.py b/tests/test_multiple_inheritance.py index e6a7f9762b..a02c313001 100644 --- a/tests/test_multiple_inheritance.py +++ b/tests/test_multiple_inheritance.py @@ -2,7 +2,6 @@ import pytest import env # noqa: F401 - from pybind11_tests import ConstructorStats from pybind11_tests import multiple_inheritance as m diff --git a/tests/test_numpy_array.py b/tests/test_numpy_array.py index 548c84bab1..69ba9d4952 100644 --- a/tests/test_numpy_array.py +++ b/tests/test_numpy_array.py @@ -2,7 +2,6 @@ import pytest import env # noqa: F401 - from pybind11_tests import numpy_array as m np = pytest.importorskip("numpy") diff --git a/tests/test_numpy_dtypes.py b/tests/test_numpy_dtypes.py index 0a5881e49d..6ea064d5bc 100644 --- a/tests/test_numpy_dtypes.py +++ b/tests/test_numpy_dtypes.py @@ -4,7 +4,6 @@ import pytest import env # noqa: F401 - from pybind11_tests import numpy_dtypes as m np = pytest.importorskip("numpy") diff --git a/tests/test_numpy_vectorize.py b/tests/test_numpy_vectorize.py index 4e6b2d1982..de5c9a607d 100644 --- a/tests/test_numpy_vectorize.py +++ b/tests/test_numpy_vectorize.py @@ -1,5 +1,6 @@ # -*- coding: utf-8 -*- import pytest + from pybind11_tests import numpy_vectorize as m np = pytest.importorskip("numpy") diff --git a/tests/test_opaque_types.py b/tests/test_opaque_types.py index 773794634c..5495cb6b4a 100644 --- a/tests/test_opaque_types.py +++ b/tests/test_opaque_types.py @@ -1,7 +1,8 @@ # -*- coding: utf-8 -*- import pytest -from pybind11_tests import opaque_types as m + from pybind11_tests import ConstructorStats, UserType +from pybind11_tests import opaque_types as m def test_string_list(): diff --git a/tests/test_operator_overloading.py b/tests/test_operator_overloading.py index 5dbfb32c8a..b7137d1592 100644 --- a/tests/test_operator_overloading.py +++ b/tests/test_operator_overloading.py @@ -1,7 +1,8 @@ # -*- coding: utf-8 -*- import pytest -from pybind11_tests import operators as m + from pybind11_tests import ConstructorStats +from pybind11_tests import operators as m def test_operator_overloading(): diff --git a/tests/test_pickling.py b/tests/test_pickling.py index 303213068d..e39463d209 100644 --- a/tests/test_pickling.py +++ b/tests/test_pickling.py @@ -2,7 +2,6 @@ import pytest import env # noqa: F401 - from pybind11_tests import pickling as m try: diff --git a/tests/test_pytypes.py b/tests/test_pytypes.py index 66d6d30a0c..8a11b18727 100644 --- a/tests/test_pytypes.py +++ b/tests/test_pytypes.py @@ -1,12 +1,13 @@ # -*- coding: utf-8 -*- from __future__ import division -import pytest + import sys -import env # noqa: F401 +import pytest -from pybind11_tests import pytypes as m +import env # noqa: F401 from pybind11_tests import debug_enabled +from pybind11_tests import pytypes as m def test_int(doc): diff --git a/tests/test_sequences_and_iterators.py b/tests/test_sequences_and_iterators.py index 70fb697f95..a868c542c2 100644 --- a/tests/test_sequences_and_iterators.py +++ b/tests/test_sequences_and_iterators.py @@ -1,7 +1,8 @@ # -*- coding: utf-8 -*- import pytest -from pybind11_tests import sequences_and_iterators as m + from pybind11_tests import ConstructorStats +from pybind11_tests import sequences_and_iterators as m def isclose(a, b, rel_tol=1e-05, abs_tol=0.0): diff --git a/tests/test_stl.py b/tests/test_stl.py index 6f08459043..3f63d6c3a4 100644 --- a/tests/test_stl.py +++ b/tests/test_stl.py @@ -1,9 +1,8 @@ # -*- coding: utf-8 -*- import pytest +from pybind11_tests import ConstructorStats, UserType from pybind11_tests import stl as m -from pybind11_tests import UserType -from pybind11_tests import ConstructorStats def test_vector(doc): diff --git a/tests/test_stl_binders.py b/tests/test_stl_binders.py index 84132a2b4f..475a9ec400 100644 --- a/tests/test_stl_binders.py +++ b/tests/test_stl_binders.py @@ -2,7 +2,6 @@ import pytest import env # noqa: F401 - from pybind11_tests import stl_binders as m diff --git a/tools/libsize.py b/tools/libsize.py index 589c317f0d..1551477e66 100644 --- a/tools/libsize.py +++ b/tools/libsize.py @@ -1,5 +1,6 @@ # -*- coding: utf-8 -*- -from __future__ import print_function, division +from __future__ import division, print_function + import os import sys diff --git a/tools/make_changelog.py b/tools/make_changelog.py index 4e8fbf5b00..629c284d39 100755 --- a/tools/make_changelog.py +++ b/tools/make_changelog.py @@ -4,11 +4,9 @@ import re import ghapi.all - from rich import print from rich.syntax import Syntax - ENTRY = re.compile( r""" Suggested \s changelog \s entry: From 1bcd94c481bba513c4c63a62b1f5da13f5e7978d Mon Sep 17 00:00:00 2001 From: "Ralf W. Grosse-Kunstleve" Date: Sat, 14 Aug 2021 07:41:27 -0700 Subject: [PATCH 140/638] Removing last remnants of pragma block at the top of pybind11.h (#3186) * Removing last remnants of pragma block at the top of pybind11.h, defaulting CUDA, GCC7, GCC8 to PYBIND11_NOINLINE_DISABLED, with the option to define PYBIND11_NOINLINE_FORCED. * Unique SOSIZE prefix to make it easier to extract the sosizes from the GitHub logs. * Commenting out PYBIND11_WERROR block, for noinline testing. * Undoing accidental change. * `#define PYBIND11_NOINLINE_FORCED` * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * `#define PYBIND11_NOINLINE_DISABLED` * Going back to default (removing `#define PYBIND11_NOINLINE_DISABLED`). * `#define PYBIND11_NOINLINE_FORCED` * Undoing all changes releated to measuring sosizes. * Rollback of PR #3030 (Working around Centos 8 failure). * Disabling -Werror for GNU (experiment). * Commenting out the entire `if(PYBIND11_WERROR)` again (although that is not expected to make a difference, but who knows what I am overlooking). * Adding `-DCMAKE_BUILD_TYPE=Release` * Undoing change to tests/CMakeLists.txt (uncommenting `if(PYBIND11_WERROR)` block). * post `git rebase master -X theirs` fixups. * Adding measurements to comment for `PYBIND11_NOINLINE_FORCED`. Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> --- include/pybind11/detail/common.h | 11 +++++++++++ include/pybind11/pybind11.h | 9 --------- 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/include/pybind11/detail/common.h b/include/pybind11/detail/common.h index cc24a52fd3..8daa33903e 100644 --- a/include/pybind11/detail/common.h +++ b/include/pybind11/detail/common.h @@ -99,6 +99,17 @@ # endif #endif +// For CUDA, GCC7, GCC8: +// PYBIND11_NOINLINE_FORCED is incompatible with `-Wattributes -Werror`. +// When defining PYBIND11_NOINLINE_FORCED, it is best to also use `-Wno-attributes`. +// However, the measured shared-library size saving when using noinline are only +// 1.7% for CUDA, -0.2% for GCC7, and 0.0% for GCC8 (using -DCMAKE_BUILD_TYPE=MinSizeRel, +// the default under pybind11/tests). +#if !defined(PYBIND11_NOINLINE_FORCED) && \ + (defined(__CUDACC__) || (defined(__GNUC__) && (__GNUC__ == 7 || __GNUC__ == 8))) +# define PYBIND11_NOINLINE_DISABLED +#endif + // The PYBIND11_NOINLINE macro is for function DEFINITIONS. // In contrast, FORWARD DECLARATIONS should never use this macro: // https://stackoverflow.com/questions/9317473/forward-declaration-of-inline-functions diff --git a/include/pybind11/pybind11.h b/include/pybind11/pybind11.h index 20df6a01fd..7b7b3ca71f 100644 --- a/include/pybind11/pybind11.h +++ b/include/pybind11/pybind11.h @@ -10,11 +10,6 @@ #pragma once -#if defined(__CUDACC__) || (defined(__GNUC__) && (__GNUC__ == 7 || __GNUC__ == 8)) -# pragma GCC diagnostic push -# pragma GCC diagnostic ignored "-Wattributes" -#endif - #include "attr.h" #include "gil.h" #include "options.h" @@ -2383,7 +2378,3 @@ PYBIND11_NAMESPACE_END(PYBIND11_NAMESPACE) #if defined(__GNUC__) && __GNUC__ == 7 # pragma GCC diagnostic pop // -Wnoexcept-type #endif - -#if defined(__CUDACC__) || (defined(__GNUC__) && (__GNUC__ == 7 || __GNUC__ == 8)) -# pragma GCC diagnostic pop -#endif From 617cb653ec513b4e02d7104b05fb75c26d10e79e Mon Sep 17 00:00:00 2001 From: Aaron Gokaslan Date: Sat, 14 Aug 2021 12:25:54 -0400 Subject: [PATCH 141/638] [Bugfix] Fix errant const methods (#3194) * Fix errant const methods * Remove NOLINT since clang-tidy is pretty conservative * Missed one * Fix a few more errors * Add reviewer suggested comments * Run clang-format --- include/pybind11/detail/common.h | 2 +- include/pybind11/detail/type_caster_base.h | 9 ++++++--- include/pybind11/pytypes.h | 10 +++++----- 3 files changed, 12 insertions(+), 9 deletions(-) diff --git a/include/pybind11/detail/common.h b/include/pybind11/detail/common.h index 8daa33903e..0d12d734c7 100644 --- a/include/pybind11/detail/common.h +++ b/include/pybind11/detail/common.h @@ -522,7 +522,7 @@ struct instance { void allocate_layout(); /// Destroys/deallocates all of the above - void deallocate_layout() const; + void deallocate_layout(); /// Returns the value_and_holder wrapper for the given type (or the first, if `find_type` /// omitted). Returns a default-constructed (with `.inst = nullptr`) object on failure if diff --git a/include/pybind11/detail/type_caster_base.h b/include/pybind11/detail/type_caster_base.h index 14cb27cad2..5a5acc2e6a 100644 --- a/include/pybind11/detail/type_caster_base.h +++ b/include/pybind11/detail/type_caster_base.h @@ -241,7 +241,8 @@ struct value_and_holder { ? inst->simple_holder_constructed : (inst->nonsimple.status[index] & instance::status_holder_constructed) != 0u; } - void set_holder_constructed(bool v = true) const { + // NOLINTNEXTLINE(readability-make-member-function-const) + void set_holder_constructed(bool v = true) { if (inst->simple_layout) inst->simple_holder_constructed = v; else if (v) @@ -254,7 +255,8 @@ struct value_and_holder { ? inst->simple_instance_registered : ((inst->nonsimple.status[index] & instance::status_instance_registered) != 0); } - void set_instance_registered(bool v = true) const { + // NOLINTNEXTLINE(readability-make-member-function-const) + void set_instance_registered(bool v = true) { if (inst->simple_layout) inst->simple_instance_registered = v; else if (v) @@ -397,7 +399,8 @@ PYBIND11_NOINLINE void instance::allocate_layout() { owned = true; } -PYBIND11_NOINLINE void instance::deallocate_layout() const { +// NOLINTNEXTLINE(readability-make-member-function-const) +PYBIND11_NOINLINE void instance::deallocate_layout() { if (!simple_layout) PyMem_Free(nonsimple.values_and_holders); } diff --git a/include/pybind11/pytypes.h b/include/pybind11/pytypes.h index 21e14960cf..dc1607ff26 100644 --- a/include/pybind11/pytypes.h +++ b/include/pybind11/pytypes.h @@ -1393,7 +1393,7 @@ class dict : public object { bool empty() const { return size() == 0; } detail::dict_iterator begin() const { return {*this, 0}; } detail::dict_iterator end() const { return {}; } - void clear() const { PyDict_Clear(ptr()); } + void clear() /* py-non-const */ { PyDict_Clear(ptr()); } template bool contains(T &&key) const { return PyDict_Contains(m_ptr, detail::object_or_cast(std::forward(key)).ptr()) == 1; } @@ -1435,10 +1435,10 @@ class list : public object { detail::item_accessor operator[](handle h) const { return object::operator[](h); } detail::list_iterator begin() const { return {*this, 0}; } detail::list_iterator end() const { return {*this, PyList_GET_SIZE(m_ptr)}; } - template void append(T &&val) const { + template void append(T &&val) /* py-non-const */ { PyList_Append(m_ptr, detail::object_or_cast(std::forward(val)).ptr()); } - template void insert(size_t index, T &&val) const { + template void insert(size_t index, T &&val) /* py-non-const */ { PyList_Insert(m_ptr, static_cast(index), detail::object_or_cast(std::forward(val)).ptr()); } @@ -1455,10 +1455,10 @@ class set : public object { } size_t size() const { return (size_t) PySet_Size(m_ptr); } bool empty() const { return size() == 0; } - template bool add(T &&val) const { + template bool add(T &&val) /* py-non-const */ { return PySet_Add(m_ptr, detail::object_or_cast(std::forward(val)).ptr()) == 0; } - void clear() const { PySet_Clear(m_ptr); } + void clear() /* py-non-const */ { PySet_Clear(m_ptr); } template bool contains(T &&val) const { return PySet_Contains(m_ptr, detail::object_or_cast(std::forward(val)).ptr()) == 1; } From 774b5ff90bbc2fac198f03a246e3517a53f8c637 Mon Sep 17 00:00:00 2001 From: "Ralf W. Grosse-Kunstleve" Date: Tue, 17 Aug 2021 16:49:39 -0700 Subject: [PATCH 142/638] Removing obsolete eigen.h warning suppression pragmas. (#3198) * Removing all pragma from eigen.h * Removing -Werror or equivalent from tests/CMakeLists.txt * Restoring tests/CMakeLists.txt from master. * Adding 4 PYBIND11_SILENCE_MSVC_C4127. * Compatibility with -Wconversion, -Wdeprecated * Introducing PYBIND11_COMPATIBILITY_WDEPRECATED_COPY * Systematically using --verbose for compilations where possible (cmake 3.14 or newer). Also changing all `cmake -t` to `--target`, `-v` to `--verbose`, `check` to `pytest`, for consistency (to make it easier to pin-point all commands of a certain type). Also removing one `-j 2` for `pytest` in hopes of reducing flakes due to races in test_iostream and in prints from destructors. * Commenting out pragmas as an experiment to reproduce previous observation. * Removing all (newly added, but already commented-out) pragma code, adding HINT use -isystem (as cmake does). * Restoring ci.yml from master. Those changes are better handled separately. BTW: in the last CI run there was still a test_iostream flake, even without the -j 2 for running the tests (verfied by inspecting the log). --- include/pybind11/eigen.h | 41 ++++++++++------------------------------ 1 file changed, 10 insertions(+), 31 deletions(-) diff --git a/include/pybind11/eigen.h b/include/pybind11/eigen.h index 218fe27034..b03e15f624 100644 --- a/include/pybind11/eigen.h +++ b/include/pybind11/eigen.h @@ -9,29 +9,13 @@ #pragma once -#include "numpy.h" - -#if defined(__INTEL_COMPILER) -# pragma warning(disable: 1682) // implicit conversion of a 64-bit integral type to a smaller integral type (potential portability problem) -#elif defined(__GNUG__) || defined(__clang__) -# pragma GCC diagnostic push -# pragma GCC diagnostic ignored "-Wconversion" -# pragma GCC diagnostic ignored "-Wdeprecated-declarations" -# ifdef __clang__ -// Eigen generates a bunch of implicit-copy-constructor-is-deprecated warnings with -Wdeprecated -// under Clang, so disable that warning here: -# pragma GCC diagnostic ignored "-Wdeprecated" -# endif -# if __GNUC__ >= 7 -# pragma GCC diagnostic ignored "-Wint-in-bool-context" -# endif -#endif +/* HINT: To suppress warnings originating from the Eigen headers, use -isystem. + See also: + https://stackoverflow.com/questions/2579576/i-dir-vs-isystem-dir + https://stackoverflow.com/questions/1741816/isystem-for-ms-visual-studio-c-compiler +*/ -#if defined(_MSC_VER) -# pragma warning(push) -# pragma warning(disable: 4127) // warning C4127: Conditional expression is constant -# pragma warning(disable: 4996) // warning C4996: std::unary_negate is deprecated in C++17 -#endif +#include "numpy.h" #include #include @@ -153,7 +137,8 @@ template struct EigenProps { np_cols = a.shape(1), np_rstride = a.strides(0) / static_cast(sizeof(Scalar)), np_cstride = a.strides(1) / static_cast(sizeof(Scalar)); - if ((fixed_rows && np_rows != rows) || (fixed_cols && np_cols != cols)) + if ((PYBIND11_SILENCE_MSVC_C4127(fixed_rows) && np_rows != rows) || + (PYBIND11_SILENCE_MSVC_C4127(fixed_cols) && np_cols != cols)) return false; return {np_rows, np_cols, np_rstride, np_cstride}; @@ -165,7 +150,7 @@ template struct EigenProps { stride = a.strides(0) / static_cast(sizeof(Scalar)); if (vector) { // Eigen type is a compile-time vector - if (fixed && size != n) + if (PYBIND11_SILENCE_MSVC_C4127(fixed) && size != n) return false; // Vector size mismatch return {rows == 1 ? 1 : n, cols == 1 ? 1 : n, stride}; } @@ -179,7 +164,7 @@ template struct EigenProps { if (cols != n) return false; return {1, n, stride}; } // Otherwise it's either fully dynamic, or column dynamic; both become a column vector - if (fixed_rows && rows != n) return false; + if (PYBIND11_SILENCE_MSVC_C4127(fixed_rows) && rows != n) return false; return {n, 1, stride}; } @@ -596,9 +581,3 @@ struct type_caster::value>> { PYBIND11_NAMESPACE_END(detail) PYBIND11_NAMESPACE_END(PYBIND11_NAMESPACE) - -#if defined(__GNUG__) || defined(__clang__) -# pragma GCC diagnostic pop -#elif defined(_MSC_VER) -# pragma warning(pop) -#endif From 998d45e4313067d752476338d090f388f0a5d9ed Mon Sep 17 00:00:00 2001 From: "Ralf W. Grosse-Kunstleve" Date: Thu, 19 Aug 2021 11:37:04 -0700 Subject: [PATCH 143/638] Cleanup of file-scoped and globally-scoped warning suppression pragmas across pybind11 header files. (#3201) * Removing all MSVC C4127 warning suppression pragmas. * Removing MSVC /WX (WERROR). To get a full list of all warnings. * Inserting PYBIND11_SILENCE_MSVC_C4127. Changing one runtime if to #if. * Changing PYBIND11_SILENCE_MSVC_C4127 macro to use absolute namespace (for use outside pybind11 include directory). * Restoring MSVC /WX (WERROR). * Removing globally-scoped suppression for clang -Wunsequenced. Based on an experiment under PR #3202 it is obsolete and can simply be removed. --- include/pybind11/detail/common.h | 2 +- include/pybind11/numpy.h | 13 ++----------- include/pybind11/operators.h | 14 ++------------ include/pybind11/stl.h | 9 --------- tests/test_builtin_casters.cpp | 14 +++++--------- 5 files changed, 10 insertions(+), 42 deletions(-) diff --git a/include/pybind11/detail/common.h b/include/pybind11/detail/common.h index 0d12d734c7..cb52aa2749 100644 --- a/include/pybind11/detail/common.h +++ b/include/pybind11/detail/common.h @@ -968,7 +968,7 @@ inline void silence_unused_warnings(Args &&...) {} // warning C4127: Conditional expression is constant constexpr inline bool silence_msvc_c4127(bool cond) { return cond; } -# define PYBIND11_SILENCE_MSVC_C4127(...) detail::silence_msvc_c4127(__VA_ARGS__) +# define PYBIND11_SILENCE_MSVC_C4127(...) ::pybind11::detail::silence_msvc_c4127(__VA_ARGS__) #else # define PYBIND11_SILENCE_MSVC_C4127(...) __VA_ARGS__ diff --git a/include/pybind11/numpy.h b/include/pybind11/numpy.h index 2dc0f29f0b..7717059f4a 100644 --- a/include/pybind11/numpy.h +++ b/include/pybind11/numpy.h @@ -25,11 +25,6 @@ #include #include -#if defined(_MSC_VER) -# pragma warning(push) -# pragma warning(disable: 4127) // warning C4127: Conditional expression is constant -#endif - /* This will be true on all flat address space platforms and allows us to reduce the whole npy_intp / ssize_t / Py_intptr_t business down to just ssize_t for all size and dimension types (e.g. shape, strides, indexing), instead of inflicting this @@ -747,7 +742,7 @@ class array : public buffer { * and the caller must take care not to access invalid dimensions or dimension indices. */ template detail::unchecked_mutable_reference mutable_unchecked() & { - if (Dims >= 0 && ndim() != Dims) + if (PYBIND11_SILENCE_MSVC_C4127(Dims >= 0) && ndim() != Dims) throw std::domain_error("array has incorrect number of dimensions: " + std::to_string(ndim()) + "; expected " + std::to_string(Dims)); return detail::unchecked_mutable_reference(mutable_data(), shape(), strides(), ndim()); @@ -761,7 +756,7 @@ class array : public buffer { * invalid dimensions or dimension indices. */ template detail::unchecked_reference unchecked() const & { - if (Dims >= 0 && ndim() != Dims) + if (PYBIND11_SILENCE_MSVC_C4127(Dims >= 0) && ndim() != Dims) throw std::domain_error("array has incorrect number of dimensions: " + std::to_string(ndim()) + "; expected " + std::to_string(Dims)); return detail::unchecked_reference(data(), shape(), strides(), ndim()); @@ -1708,7 +1703,3 @@ Helper vectorize(Return (Class::*f)(Args...) const) { } PYBIND11_NAMESPACE_END(PYBIND11_NAMESPACE) - -#if defined(_MSC_VER) -#pragma warning(pop) -#endif diff --git a/include/pybind11/operators.h b/include/pybind11/operators.h index 086cb4cfd8..2a61531589 100644 --- a/include/pybind11/operators.h +++ b/include/pybind11/operators.h @@ -11,13 +11,6 @@ #include "pybind11.h" -#if defined(__clang__) && !defined(__INTEL_COMPILER) -# pragma clang diagnostic ignored "-Wunsequenced" // multiple unsequenced modifications to 'self' (when using def(py::self OP Type())) -#elif defined(_MSC_VER) -# pragma warning(push) -# pragma warning(disable: 4127) // warning C4127: Conditional expression is constant -#endif - PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE) PYBIND11_NAMESPACE_BEGIN(detail) @@ -58,7 +51,8 @@ template struct op_ { using op = op_impl; cl.def(op::name(), &op::execute, is_operator(), extra...); #if PY_MAJOR_VERSION < 3 - if (id == op_truediv || id == op_itruediv) + if (PYBIND11_SILENCE_MSVC_C4127(id == op_truediv) || + PYBIND11_SILENCE_MSVC_C4127(id == op_itruediv)) cl.def(id == op_itruediv ? "__idiv__" : ot == op_l ? "__div__" : "__rdiv__", &op::execute, is_operator(), extra...); #endif @@ -167,7 +161,3 @@ using detail::self; using detail::hash; PYBIND11_NAMESPACE_END(PYBIND11_NAMESPACE) - -#if defined(_MSC_VER) -# pragma warning(pop) -#endif diff --git a/include/pybind11/stl.h b/include/pybind11/stl.h index 51994c656b..fe391f70bd 100644 --- a/include/pybind11/stl.h +++ b/include/pybind11/stl.h @@ -19,11 +19,6 @@ #include #include -#if defined(_MSC_VER) -#pragma warning(push) -#pragma warning(disable: 4127) // warning C4127: Conditional expression is constant -#endif - #ifdef __has_include // std::optional (but including it in c++14 mode isn't allowed) # if defined(PYBIND11_CPP17) && __has_include() @@ -390,7 +385,3 @@ inline std::ostream &operator<<(std::ostream &os, const handle &obj) { } PYBIND11_NAMESPACE_END(PYBIND11_NAMESPACE) - -#if defined(_MSC_VER) -#pragma warning(pop) -#endif diff --git a/tests/test_builtin_casters.cpp b/tests/test_builtin_casters.cpp index 51e410cadd..be4431aad6 100644 --- a/tests/test_builtin_casters.cpp +++ b/tests/test_builtin_casters.cpp @@ -10,11 +10,6 @@ #include "pybind11_tests.h" #include -#if defined(_MSC_VER) -# pragma warning(push) -# pragma warning(disable: 4127) // warning C4127: Conditional expression is constant -#endif - struct ConstRefCasted { int tag; }; @@ -73,7 +68,7 @@ TEST_SUBMODULE(builtin_casters, m) { std::wstring wstr; wstr.push_back(0x61); // a wstr.push_back(0x2e18); // ⸘ - if (sizeof(wchar_t) == 2) { wstr.push_back(mathbfA16_1); wstr.push_back(mathbfA16_2); } // 𝐀, utf16 + if (PYBIND11_SILENCE_MSVC_C4127(sizeof(wchar_t) == 2)) { wstr.push_back(mathbfA16_1); wstr.push_back(mathbfA16_2); } // 𝐀, utf16 else { wstr.push_back((wchar_t) mathbfA32); } // 𝐀, utf32 wstr.push_back(0x7a); // z @@ -83,11 +78,12 @@ TEST_SUBMODULE(builtin_casters, m) { m.def("good_wchar_string", [=]() { return wstr; }); // a‽𝐀z m.def("bad_utf8_string", []() { return std::string("abc\xd0" "def"); }); m.def("bad_utf16_string", [=]() { return std::u16string({ b16, char16_t(0xd800), z16 }); }); +#if PY_MAJOR_VERSION >= 3 // Under Python 2.7, invalid unicode UTF-32 characters don't appear to trigger UnicodeDecodeError - if (PY_MAJOR_VERSION >= 3) - m.def("bad_utf32_string", [=]() { return std::u32string({ a32, char32_t(0xd800), z32 }); }); - if (PY_MAJOR_VERSION >= 3 || sizeof(wchar_t) == 2) + m.def("bad_utf32_string", [=]() { return std::u32string({ a32, char32_t(0xd800), z32 }); }); + if (PYBIND11_SILENCE_MSVC_C4127(sizeof(wchar_t) == 2)) m.def("bad_wchar_string", [=]() { return std::wstring({ wchar_t(0x61), wchar_t(0xd800) }); }); +#endif m.def("u8_Z", []() -> char { return 'Z'; }); m.def("u8_eacute", []() -> char { return '\xe9'; }); m.def("u16_ibang", [=]() -> char16_t { return ib16; }); From 4f29b8a45b8d1e52d06d920f7a8140811a925fd9 Mon Sep 17 00:00:00 2001 From: Liam Keegan Date: Thu, 19 Aug 2021 20:42:55 +0200 Subject: [PATCH 144/638] ci: extend msys2 mingw CI (#3207) * extend msys2 CI - add 32-bit job - add c++11/17 c++/interface tests copied from standard ci - add numpy/scipy * account for padding of PartialStruct in numpy dtypes test with mingw32 * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * msys2 ci: add c++14 tests Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> --- .github/workflows/ci.yml | 81 +++++++++++++++++++++++++++++++------- tests/test_numpy_dtypes.py | 14 +++++-- 2 files changed, 78 insertions(+), 17 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index be3fa723c9..a3664ddf55 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -861,32 +861,85 @@ jobs: run: cmake --build build -t check mingw: + name: "🐍 3 • windows-latest • ${{ matrix.sys }}" runs-on: windows-latest defaults: run: shell: msys2 {0} + strategy: + fail-fast: false + matrix: + include: + - { sys: mingw64, env: x86_64 } + - { sys: mingw32, env: i686 } steps: - uses: msys2/setup-msys2@v2 with: + msystem: ${{matrix.sys}} install: >- - mingw-w64-x86_64-gcc - mingw-w64-x86_64-python-pip - mingw-w64-x86_64-cmake - mingw-w64-x86_64-make - mingw-w64-x86_64-python-pytest - mingw-w64-x86_64-eigen3 - mingw-w64-x86_64-boost - mingw-w64-x86_64-catch + git + mingw-w64-${{matrix.env}}-gcc + mingw-w64-${{matrix.env}}-python-pip + mingw-w64-${{matrix.env}}-python-numpy + mingw-w64-${{matrix.env}}-python-scipy + mingw-w64-${{matrix.env}}-cmake + mingw-w64-${{matrix.env}}-make + mingw-w64-${{matrix.env}}-python-pytest + mingw-w64-${{matrix.env}}-eigen3 + mingw-w64-${{matrix.env}}-boost + mingw-w64-${{matrix.env}}-catch - - uses: actions/checkout@v1 + - uses: actions/checkout@v2 - - name: Configure + - name: Configure C++11 # LTO leads to many undefined reference like # `pybind11::detail::function_call::function_call(pybind11::detail::function_call&&) - run: cmake -G "MinGW Makefiles" -S . -B build + run: cmake -G "MinGW Makefiles" -DCMAKE_CXX_STANDARD=11 -S . -B build - - name: Build + - name: Build C++11 run: cmake --build build -j 2 - - name: Python tests - run: cmake --build build --target pytest + - name: Python tests C++11 + run: cmake --build build --target pytest -j 2 + + - name: C++11 tests + run: cmake --build build --target cpptest -j 2 + + - name: Interface test C++11 + run: cmake --build build --target test_cmake_build + + - name: Clean directory + run: git clean -fdx + + - name: Configure C++14 + run: cmake -G "MinGW Makefiles" -DCMAKE_CXX_STANDARD=14 -S . -B build2 + + - name: Build C++14 + run: cmake --build build2 -j 2 + + - name: Python tests C++14 + run: cmake --build build2 --target pytest -j 2 + + - name: C++14 tests + run: cmake --build build2 --target cpptest -j 2 + + - name: Interface test C++14 + run: cmake --build build2 --target test_cmake_build + + - name: Clean directory + run: git clean -fdx + + - name: Configure C++17 + run: cmake -G "MinGW Makefiles" -DCMAKE_CXX_STANDARD=17 -S . -B build3 + + - name: Build C++17 + run: cmake --build build3 -j 2 + + - name: Python tests C++17 + run: cmake --build build3 --target pytest -j 2 + + - name: C++17 tests + run: cmake --build build3 --target cpptest -j 2 + + - name: Interface test C++17 + run: cmake --build build3 --target test_cmake_build diff --git a/tests/test_numpy_dtypes.py b/tests/test_numpy_dtypes.py index 6ea064d5bc..06e578329e 100644 --- a/tests/test_numpy_dtypes.py +++ b/tests/test_numpy_dtypes.py @@ -63,14 +63,20 @@ def partial_ld_offset(): def partial_dtype_fmt(): ld = np.dtype("longdouble") partial_ld_off = partial_ld_offset() - return dt_fmt().format(ld.itemsize, partial_ld_off, partial_ld_off + ld.itemsize) + partial_size = partial_ld_off + ld.itemsize + partial_end_padding = partial_size % np.dtype("uint64").alignment + return dt_fmt().format( + ld.itemsize, partial_ld_off, partial_size + partial_end_padding + ) def partial_nested_fmt(): ld = np.dtype("longdouble") partial_nested_off = 8 + 8 * (ld.alignment > 8) partial_ld_off = partial_ld_offset() - partial_nested_size = partial_nested_off * 2 + partial_ld_off + ld.itemsize + partial_size = partial_ld_off + ld.itemsize + partial_end_padding = partial_size % np.dtype("uint64").alignment + partial_nested_size = partial_nested_off * 2 + partial_size + partial_end_padding return "{{'names':['a'], 'formats':[{}], 'offsets':[{}], 'itemsize':{}}}".format( partial_dtype_fmt(), partial_nested_off, partial_nested_size ) @@ -91,10 +97,12 @@ def test_format_descriptors(): ldbl_fmt = ("4x" if ld.alignment > 4 else "") + ld.char ss_fmt = "^T{?:bool_:3xI:uint_:f:float_:" + ldbl_fmt + ":ldbl_:}" dbl = np.dtype("double") + end_padding = ld.itemsize % np.dtype("uint64").alignment partial_fmt = ( "^T{?:bool_:3xI:uint_:f:float_:" + str(4 * (dbl.alignment > 4) + dbl.itemsize + 8 * (ld.alignment > 8)) - + "xg:ldbl_:}" + + "xg:ldbl_:" + + (str(end_padding) + "x}" if end_padding > 0 else "}") ) nested_extra = str(max(8, ld.alignment)) assert m.print_format_descriptors() == [ From b3d18f382f3bc37ec4d95cfe2045db72cf0e950d Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 23 Aug 2021 15:22:12 -0400 Subject: [PATCH 145/638] [pre-commit.ci] pre-commit autoupdate (#3213) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit updates: - [github.com/asottile/pyupgrade: v2.23.3 → v2.24.0](https://github.com/asottile/pyupgrade/compare/v2.23.3...v2.24.0) Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> --- .pre-commit-config.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index a5900df162..96ddcf7469 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -31,7 +31,7 @@ repos: exclude: ^noxfile.py$ - repo: https://github.com/asottile/pyupgrade - rev: v2.23.3 + rev: v2.24.0 hooks: - id: pyupgrade From fdac5fbf7c1c3b63b6f3067734a10db0b409e28c Mon Sep 17 00:00:00 2001 From: Henry Schreiner Date: Mon, 23 Aug 2021 15:05:54 -0700 Subject: [PATCH 146/638] chore: support targeting different Python versions with nox (#3214) --- .github/CONTRIBUTING.md | 4 ++-- noxfile.py | 4 +++- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/.github/CONTRIBUTING.md b/.github/CONTRIBUTING.md index ff1997f989..39c32b2ac5 100644 --- a/.github/CONTRIBUTING.md +++ b/.github/CONTRIBUTING.md @@ -68,8 +68,8 @@ nox -l # Run linters nox -s lint -# Run tests -nox -s tests +# Run tests on Python 3.9 +nox -s tests-3.9 # Build and preview docs nox -s docs -- serve diff --git a/noxfile.py b/noxfile.py index 55b1d180b2..234179821d 100644 --- a/noxfile.py +++ b/noxfile.py @@ -2,6 +2,8 @@ nox.options.sessions = ["lint", "tests", "tests_packaging"] +PYTHON_VERISONS = ["2.7", "3.5", "3.6", "3.7", "3.8", "3.9", "3.10"] + @nox.session(reuse_venv=True) def lint(session: nox.Session) -> None: @@ -12,7 +14,7 @@ def lint(session: nox.Session) -> None: session.run("pre-commit", "run", "-a") -@nox.session +@nox.session(python=PYTHON_VERISONS) def tests(session: nox.Session) -> None: """ Run the tests (requires a compiler). From 6cbabc4b8c86a838216cb9d17dce14167222e6f7 Mon Sep 17 00:00:00 2001 From: Aaron Gokaslan Date: Mon, 23 Aug 2021 18:42:19 -0400 Subject: [PATCH 147/638] maint(clang-tidy): Enable cpp-coreguideline slicing checks (#3210) * maint(clang-tidy): add a clang-tidy slicing check * Add self + touch up readme * Fix typo --- .clang-tidy | 1 + README.rst | 4 ++-- include/pybind11/pybind11.h | 2 +- 3 files changed, 4 insertions(+), 3 deletions(-) diff --git a/.clang-tidy b/.clang-tidy index c83b9b2f5a..cefffba1ea 100644 --- a/.clang-tidy +++ b/.clang-tidy @@ -3,6 +3,7 @@ FormatStyle: file Checks: ' *bugprone*, cppcoreguidelines-init-variables, +cppcoreguidelines-slicing, clang-analyzer-optin.cplusplus.VirtualCall, llvm-namespace-comment, misc-misplaced-const, diff --git a/README.rst b/README.rst index 57eb06e557..7ce57b03ae 100644 --- a/README.rst +++ b/README.rst @@ -134,9 +134,9 @@ About This project was created by `Wenzel Jakob `_. Significant features and/or improvements to the code were contributed by Jonas Adler, Lori A. Burns, -Sylvain Corlay, Eric Cousineau, Ralf Grosse-Kunstleve, Trent Houliston, Axel +Sylvain Corlay, Eric Cousineau, Aaron Gokaslan, Ralf Grosse-Kunstleve, Trent Houliston, Axel Huebl, @hulucc, Yannick Jadoul, Sergey Lyskov Johan Mabille, Tomasz Miąsko, -Dean Moldovan, Ben Pritchard, Jason Rhinelander, Boris Schäling, Pim +Dean Moldovan, Ben Pritchard, Jason Rhinelander, Boris Schäling, Pim Schellart, Henry Schreiner, Ivan Smirnov, Boris Staletic, and Patrick Stewart. We thank Google for a generous financial contribution to the continuous diff --git a/include/pybind11/pybind11.h b/include/pybind11/pybind11.h index 7b7b3ca71f..47b042147b 100644 --- a/include/pybind11/pybind11.h +++ b/include/pybind11/pybind11.h @@ -1664,7 +1664,7 @@ inline str enum_name(handle arg) { } struct enum_base { - enum_base(handle base, handle parent) : m_base(base), m_parent(parent) { } + enum_base(const handle &base, const handle &parent) : m_base(base), m_parent(parent) { } PYBIND11_NOINLINE void init(bool is_arithmetic, bool is_convertible) { m_base.attr("__entries") = dict(); From c8ce4b8df854a630a5f02192305c183299778b84 Mon Sep 17 00:00:00 2001 From: "Ralf W. Grosse-Kunstleve" Date: Mon, 23 Aug 2021 17:30:01 -0700 Subject: [PATCH 148/638] Clone of @virtuald's PR #2112 with minor enhancements. (#3215) * Add py::raise_from to enable chaining exceptions on Python 3.3+ * Use 'raise from' in initialization * Documenting the exact base version of _PyErr_FormatVFromCause, adding back `assert`s. Co-authored-by: Dustin Spicuzza --- docs/advanced/exceptions.rst | 28 ++++++++++++++++++ include/pybind11/detail/common.h | 15 ++++++++++ include/pybind11/pytypes.h | 41 +++++++++++++++++++++++++++ tests/test_embed/test_interpreter.cpp | 16 +++++++++++ tests/test_exceptions.cpp | 20 +++++++++++++ tests/test_exceptions.py | 16 +++++++++++ 6 files changed, 136 insertions(+) diff --git a/docs/advanced/exceptions.rst b/docs/advanced/exceptions.rst index 738fb0ba9c..2aaa0ad323 100644 --- a/docs/advanced/exceptions.rst +++ b/docs/advanced/exceptions.rst @@ -323,6 +323,34 @@ Alternately, to ignore the error, call `PyErr_Clear Any Python error must be thrown or cleared, or Python/pybind11 will be left in an invalid state. +Chaining exceptions ('raise from') +================================== + +In Python 3.3 a mechanism for indicating that exceptions were caused by other +exceptions was introduced: + +.. code-block:: py + + try: + print(1 / 0) + except Exception as exc: + raise RuntimeError("could not divide by zero") from exc + +To do a similar thing in pybind11, you can use the ``py::raise_from`` function. It +sets the current python error indicator, so to continue propagating the exception +you should ``throw py::error_already_set()`` (Python 3 only). + +.. code-block:: cpp + + try { + py::eval("print(1 / 0")); + } catch (py::error_already_set &e) { + py::raise_from(e, PyExc_RuntimeError, "could not divide by zero"); + throw py::error_already_set(); + } + +.. versionadded:: 2.8 + .. _unraisable_exceptions: Handling unraisable exceptions diff --git a/include/pybind11/detail/common.h b/include/pybind11/detail/common.h index cb52aa2749..5050da802a 100644 --- a/include/pybind11/detail/common.h +++ b/include/pybind11/detail/common.h @@ -315,6 +315,19 @@ extern "C" { } \ } +#if PY_VERSION_HEX >= 0x03030000 + +#define PYBIND11_CATCH_INIT_EXCEPTIONS \ + catch (pybind11::error_already_set &e) { \ + pybind11::raise_from(e, PyExc_ImportError, "initialization failed"); \ + return nullptr; \ + } catch (const std::exception &e) { \ + PyErr_SetString(PyExc_ImportError, e.what()); \ + return nullptr; \ + } \ + +#else + #define PYBIND11_CATCH_INIT_EXCEPTIONS \ catch (pybind11::error_already_set &e) { \ PyErr_SetString(PyExc_ImportError, e.what()); \ @@ -324,6 +337,8 @@ extern "C" { return nullptr; \ } \ +#endif + /** \rst ***Deprecated in favor of PYBIND11_MODULE*** diff --git a/include/pybind11/pytypes.h b/include/pybind11/pytypes.h index dc1607ff26..85f6a40a48 100644 --- a/include/pybind11/pytypes.h +++ b/include/pybind11/pytypes.h @@ -382,6 +382,47 @@ class PYBIND11_EXPORT_EXCEPTION error_already_set : public std::runtime_error { # pragma warning(pop) #endif +#if PY_VERSION_HEX >= 0x03030000 + +/// Replaces the current Python error indicator with the chosen error, performing a +/// 'raise from' to indicate that the chosen error was caused by the original error. +inline void raise_from(PyObject *type, const char *message) { + // Based on _PyErr_FormatVFromCause: + // https://github.com/python/cpython/blob/467ab194fc6189d9f7310c89937c51abeac56839/Python/errors.c#L405 + // See https://github.com/pybind/pybind11/pull/2112 for details. + PyObject *exc = nullptr, *val = nullptr, *val2 = nullptr, *tb = nullptr; + + assert(PyErr_Occurred()); + PyErr_Fetch(&exc, &val, &tb); + PyErr_NormalizeException(&exc, &val, &tb); + if (tb != nullptr) { + PyException_SetTraceback(val, tb); + Py_DECREF(tb); + } + Py_DECREF(exc); + assert(!PyErr_Occurred()); + + PyErr_SetString(type, message); + + PyErr_Fetch(&exc, &val2, &tb); + PyErr_NormalizeException(&exc, &val2, &tb); + Py_INCREF(val); + PyException_SetCause(val2, val); + PyException_SetContext(val2, val); + PyErr_Restore(exc, val2, tb); +} + +/// Sets the current Python error indicator with the chosen error, performing a 'raise from' +/// from the error contained in error_already_set to indicate that the chosen error was +/// caused by the original error. After this function is called error_already_set will +/// no longer contain an error. +inline void raise_from(error_already_set& err, PyObject *type, const char *message) { + err.restore(); + raise_from(type, message); +} + +#endif + /** \defgroup python_builtins _ Unless stated otherwise, the following C++ functions behave the same as their Python counterparts. diff --git a/tests/test_embed/test_interpreter.cpp b/tests/test_embed/test_interpreter.cpp index cd50e952f6..b40ff48175 100644 --- a/tests/test_embed/test_interpreter.cpp +++ b/tests/test_embed/test_interpreter.cpp @@ -74,8 +74,24 @@ TEST_CASE("Import error handling") { REQUIRE_NOTHROW(py::module_::import("widget_module")); REQUIRE_THROWS_WITH(py::module_::import("throw_exception"), "ImportError: C++ Error"); +#if PY_VERSION_HEX >= 0x03030000 + REQUIRE_THROWS_WITH(py::module_::import("throw_error_already_set"), + Catch::Contains("ImportError: initialization failed")); + + auto locals = py::dict("is_keyerror"_a=false, "message"_a="not set"); + py::exec(R"( + try: + import throw_error_already_set + except ImportError as e: + is_keyerror = type(e.__cause__) == KeyError + message = str(e.__cause__) + )", py::globals(), locals); + REQUIRE(locals["is_keyerror"].cast() == true); + REQUIRE(locals["message"].cast() == "'missing'"); +#else REQUIRE_THROWS_WITH(py::module_::import("throw_error_already_set"), Catch::Contains("ImportError: KeyError")); +#endif } TEST_CASE("There can be only one interpreter") { diff --git a/tests/test_exceptions.cpp b/tests/test_exceptions.cpp index e28f0bb798..4805a02490 100644 --- a/tests/test_exceptions.cpp +++ b/tests/test_exceptions.cpp @@ -262,4 +262,24 @@ TEST_SUBMODULE(exceptions, m) { m.def("simple_bool_passthrough", [](bool x) {return x;}); m.def("throw_should_be_translated_to_key_error", []() { throw shared_exception(); }); + +#if PY_VERSION_HEX >= 0x03030000 + + m.def("raise_from", []() { + PyErr_SetString(PyExc_ValueError, "inner"); + py::raise_from(PyExc_ValueError, "outer"); + throw py::error_already_set(); + }); + + m.def("raise_from_already_set", []() { + try { + PyErr_SetString(PyExc_ValueError, "inner"); + throw py::error_already_set(); + } catch (py::error_already_set& e) { + py::raise_from(e, PyExc_ValueError, "outer"); + throw py::error_already_set(); + } + }); + +#endif } diff --git a/tests/test_exceptions.py b/tests/test_exceptions.py index d1edc39f0e..3821eadaa4 100644 --- a/tests/test_exceptions.py +++ b/tests/test_exceptions.py @@ -24,6 +24,22 @@ def test_error_already_set(msg): assert msg(excinfo.value) == "foo" +@pytest.mark.skipif("env.PY2") +def test_raise_from(msg): + with pytest.raises(ValueError) as excinfo: + m.raise_from() + assert msg(excinfo.value) == "outer" + assert msg(excinfo.value.__cause__) == "inner" + + +@pytest.mark.skipif("env.PY2") +def test_raise_from_already_set(msg): + with pytest.raises(ValueError) as excinfo: + m.raise_from_already_set() + assert msg(excinfo.value) == "outer" + assert msg(excinfo.value.__cause__) == "inner" + + def test_cross_module_exceptions(msg): with pytest.raises(RuntimeError) as excinfo: cm.raise_runtime_error() From 031a700dfd611efc3949d8427d8c87e91a7b6998 Mon Sep 17 00:00:00 2001 From: Jouke Witteveen Date: Thu, 26 Aug 2021 17:04:22 +0200 Subject: [PATCH 149/638] Add make_simple_namespace function and tests (#2840) Co-authored-by: Jouke Witteveen --- docs/advanced/pycpp/object.rst | 43 +++++++++++++++++++++++++++++++++- include/pybind11/cast.h | 10 ++++++++ tests/test_pytypes.cpp | 13 ++++++++++ tests/test_pytypes.py | 13 ++++++++++ 4 files changed, 78 insertions(+), 1 deletion(-) diff --git a/docs/advanced/pycpp/object.rst b/docs/advanced/pycpp/object.rst index 6c7525ceaf..6fa8d07082 100644 --- a/docs/advanced/pycpp/object.rst +++ b/docs/advanced/pycpp/object.rst @@ -20,6 +20,47 @@ Available types include :class:`handle`, :class:`object`, :class:`bool_`, Be sure to review the :ref:`pytypes_gotchas` before using this heavily in your C++ API. +.. _instantiating_compound_types: + +Instantiating compound Python types from C++ +============================================ + +Dictionaries can be initialized in the :class:`dict` constructor: + +.. code-block:: cpp + + using namespace pybind11::literals; // to bring in the `_a` literal + py::dict d("spam"_a=py::none(), "eggs"_a=42); + +A tuple of python objects can be instantiated using :func:`py::make_tuple`: + +.. code-block:: cpp + + py::tuple tup = py::make_tuple(42, py::none(), "spam"); + +Each element is converted to a supported Python type. + +A `simple namespace`_ can be instantiated using +:func:`py::make_simple_namespace`: + +.. code-block:: cpp + + using namespace pybind11::literals; // to bring in the `_a` literal + py::object ns = py::make_simple_namespace("spam"_a=py::none(), "eggs"_a=42); + +Attributes on a namespace can be modified with the :func:`py::delattr`, +:func:`py::getattr`, and :func:`py::setattr` functions. Simple namespaces can +be useful as lightweight stand-ins for class instances. + +.. note:: + + ``make_simple_namespace`` is not available in Python 2. + +.. versionchanged:: 2.8 + ``make_simple_namespace`` added. + +.. _simple namespace: https://docs.python.org/3/library/types.html#types.SimpleNamespace + .. _casting_back_and_forth: Casting back and forth @@ -30,7 +71,7 @@ types to Python, which can be done using :func:`py::cast`: .. code-block:: cpp - MyClass *cls = ..; + MyClass *cls = ...; py::object obj = py::cast(cls); The reverse direction uses the following syntax: diff --git a/include/pybind11/cast.h b/include/pybind11/cast.h index 718dc2de80..79bf506d81 100644 --- a/include/pybind11/cast.h +++ b/include/pybind11/cast.h @@ -1018,6 +1018,16 @@ template = 0x03030000 +template ()>> +object make_simple_namespace(Args&&... args_) { + PyObject *ns = _PyNamespace_New(dict(std::forward(args_)...).ptr()); + if (!ns) throw error_already_set(); + return reinterpret_steal(ns); +} +#endif + /// \ingroup annotations /// Annotation for arguments struct arg { diff --git a/tests/test_pytypes.cpp b/tests/test_pytypes.cpp index d70536d3f0..15d007a437 100644 --- a/tests/test_pytypes.cpp +++ b/tests/test_pytypes.cpp @@ -70,6 +70,19 @@ TEST_SUBMODULE(pytypes, m) { m.def("dict_contains", [](const py::dict &dict, const char *val) { return dict.contains(val); }); + // test_tuple + m.def("get_tuple", []() { return py::make_tuple(42, py::none(), "spam"); }); + +#if PY_VERSION_HEX >= 0x03030000 + // test_simple_namespace + m.def("get_simple_namespace", []() { + auto ns = py::make_simple_namespace("attr"_a=42, "x"_a="foo", "wrong"_a=1); + py::delattr(ns, "wrong"); + py::setattr(ns, "right", py::int_(2)); + return ns; + }); +#endif + // test_str m.def("str_from_string", []() { return py::str(std::string("baz")); }); m.def("str_from_bytes", []() { return py::str(py::bytes("boo", 3)); }); diff --git a/tests/test_pytypes.py b/tests/test_pytypes.py index 8a11b18727..f873658abe 100644 --- a/tests/test_pytypes.py +++ b/tests/test_pytypes.py @@ -99,6 +99,19 @@ def test_dict(capture, doc): assert m.dict_keyword_constructor() == {"x": 1, "y": 2, "z": 3} +def test_tuple(): + assert m.get_tuple() == (42, None, "spam") + + +@pytest.mark.skipif("env.PY2") +def test_simple_namespace(): + ns = m.get_simple_namespace() + assert ns.attr == 42 + assert ns.x == "foo" + assert ns.right == 2 + assert not hasattr(ns, "wrong") + + def test_str(doc): assert m.str_from_string().encode().decode() == "baz" assert m.str_from_bytes().encode().decode() == "boo" From 59ad1e7d05d96ccc4e7090d5da7a328e4512cb7a Mon Sep 17 00:00:00 2001 From: Nick Cullen Date: Thu, 26 Aug 2021 17:12:35 +0200 Subject: [PATCH 150/638] reshape for numpy arrays (#984) * reshape * more tests * Update numpy.h * Update test_numpy_array.py * Update numpy.h * Update numpy.h * Update test_numpy_array.cpp * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Fix merge bug * Make clang-tidy happy * Add xfail for PyPy * Fix casting issue * Address reviews on additional tests * Fix ordering * Do a little more reordering * Fix typo * Try improving tests * Fix error in reshape * Add one more reshape test * streamlining new tests; removing a few stray msg Co-authored-by: ncullen93 Co-authored-by: NC Cullen Co-authored-by: Aaron Gokaslan Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Ralf Grosse-Kunstleve --- include/pybind11/numpy.h | 21 +++++++++++++++++++-- tests/test_numpy_array.cpp | 7 +++++++ tests/test_numpy_array.py | 31 ++++++++++++++++++++++++++++--- 3 files changed, 54 insertions(+), 5 deletions(-) diff --git a/include/pybind11/numpy.h b/include/pybind11/numpy.h index 7717059f4a..0d0cbdfa89 100644 --- a/include/pybind11/numpy.h +++ b/include/pybind11/numpy.h @@ -198,6 +198,8 @@ struct npy_api { // Unused. Not removed because that affects ABI of the class. int (*PyArray_SetBaseObject_)(PyObject *, PyObject *); PyObject* (*PyArray_Resize_)(PyObject*, PyArray_Dims*, int, int); + PyObject* (*PyArray_Newshape_)(PyObject*, PyArray_Dims*, int); + private: enum functions { API_PyArray_GetNDArrayCFeatureVersion = 211, @@ -212,10 +214,11 @@ struct npy_api { API_PyArray_NewCopy = 85, API_PyArray_NewFromDescr = 94, API_PyArray_DescrNewFromType = 96, + API_PyArray_Newshape = 135, + API_PyArray_Squeeze = 136, API_PyArray_DescrConverter = 174, API_PyArray_EquivTypes = 182, API_PyArray_GetArrayParamsFromObject = 278, - API_PyArray_Squeeze = 136, API_PyArray_SetBaseObject = 282 }; @@ -243,11 +246,13 @@ struct npy_api { DECL_NPY_API(PyArray_NewCopy); DECL_NPY_API(PyArray_NewFromDescr); DECL_NPY_API(PyArray_DescrNewFromType); + DECL_NPY_API(PyArray_Newshape); + DECL_NPY_API(PyArray_Squeeze); DECL_NPY_API(PyArray_DescrConverter); DECL_NPY_API(PyArray_EquivTypes); DECL_NPY_API(PyArray_GetArrayParamsFromObject); - DECL_NPY_API(PyArray_Squeeze); DECL_NPY_API(PyArray_SetBaseObject); + #undef DECL_NPY_API return api; } @@ -785,6 +790,18 @@ class array : public buffer { if (isinstance(new_array)) { *this = std::move(new_array); } } + /// Optional `order` parameter omitted, to be added as needed. + array reshape(ShapeContainer new_shape) { + detail::npy_api::PyArray_Dims d + = {reinterpret_cast(new_shape->data()), int(new_shape->size())}; + auto new_array + = reinterpret_steal(detail::npy_api::get().PyArray_Newshape_(m_ptr, &d, 0)); + if (!new_array) { + throw error_already_set(); + } + return new_array; + } + /// Ensure that the argument is a NumPy array /// In case of an error, nullptr is returned and the Python error is cleared. static array ensure(handle h, int ExtraFlags = 0) { diff --git a/tests/test_numpy_array.cpp b/tests/test_numpy_array.cpp index 5c22a3d25b..4ccfd279b0 100644 --- a/tests/test_numpy_array.cpp +++ b/tests/test_numpy_array.cpp @@ -405,6 +405,13 @@ TEST_SUBMODULE(numpy_array, sm) { return a; }); + sm.def("reshape_initializer_list", [](py::array_t a, size_t N, size_t M, size_t O) { + return a.reshape({N, M, O}); + }); + sm.def("reshape_tuple", [](py::array_t a, const std::vector &new_shape) { + return a.reshape(new_shape); + }); + sm.def("index_using_ellipsis", [](const py::array &a) { return a[py::make_tuple(0, py::ellipsis(), 0)]; }); diff --git a/tests/test_numpy_array.py b/tests/test_numpy_array.py index 69ba9d4952..e96454be48 100644 --- a/tests/test_numpy_array.py +++ b/tests/test_numpy_array.py @@ -411,7 +411,7 @@ def test_array_unchecked_fixed_dims(msg): assert m.proxy_auxiliaries2_const_ref(z1) -def test_array_unchecked_dyn_dims(msg): +def test_array_unchecked_dyn_dims(): z1 = np.array([[1, 2], [3, 4]], dtype="float64") m.proxy_add2_dyn(z1, 10) assert np.all(z1 == [[11, 12], [13, 14]]) @@ -444,7 +444,7 @@ def test_initializer_list(): assert m.array_initializer_list4().shape == (1, 2, 3, 4) -def test_array_resize(msg): +def test_array_resize(): a = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9], dtype="float64") m.array_reshape2(a) assert a.size == 9 @@ -470,12 +470,37 @@ def test_array_resize(msg): @pytest.mark.xfail("env.PYPY") -def test_array_create_and_resize(msg): +def test_array_create_and_resize(): a = m.create_and_resize(2) assert a.size == 4 assert np.all(a == 42.0) +def test_reshape_initializer_list(): + a = np.arange(2 * 7 * 3) + 1 + x = m.reshape_initializer_list(a, 2, 7, 3) + assert x.shape == (2, 7, 3) + assert list(x[1][4]) == [34, 35, 36] + with pytest.raises(ValueError) as excinfo: + m.reshape_initializer_list(a, 1, 7, 3) + assert str(excinfo.value) == "cannot reshape array of size 42 into shape (1,7,3)" + + +def test_reshape_tuple(): + a = np.arange(3 * 7 * 2) + 1 + x = m.reshape_tuple(a, (3, 7, 2)) + assert x.shape == (3, 7, 2) + assert list(x[1][4]) == [23, 24] + y = m.reshape_tuple(x, (x.size,)) + assert y.shape == (42,) + with pytest.raises(ValueError) as excinfo: + m.reshape_tuple(a, (3, 7, 1)) + assert str(excinfo.value) == "cannot reshape array of size 42 into shape (3,7,1)" + with pytest.raises(ValueError) as excinfo: + m.reshape_tuple(a, ()) + assert str(excinfo.value) == "cannot reshape array of size 42 into shape ()" + + def test_index_using_ellipsis(): a = m.index_using_ellipsis(np.zeros((5, 6, 7))) assert a.shape == (6,) From db44afa33b21af09f81c70ee6449c4a4d93fbeca Mon Sep 17 00:00:00 2001 From: Henry Schreiner Date: Thu, 26 Aug 2021 10:52:13 -0700 Subject: [PATCH 151/638] tests: fix pytest usage on Python 3.10 (#3221) --- tests/requirements.txt | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/requirements.txt b/tests/requirements.txt index 00cb5f119f..069122b884 100644 --- a/tests/requirements.txt +++ b/tests/requirements.txt @@ -2,11 +2,11 @@ numpy==1.16.6; python_version<"3.6" and sys_platform!="win32" numpy==1.18.0; platform_python_implementation=="PyPy" and sys_platform=="darwin" and python_version>="3.6" numpy==1.19.3; (platform_python_implementation!="PyPy" or sys_platform=="linux") and python_version=="3.6" -numpy==1.20.0; (platform_python_implementation!="PyPy" or sys_platform=="linux") and python_version>="3.7" and python_version<"3.10" +numpy==1.21.2; (platform_python_implementation!="PyPy" or sys_platform=="linux") and python_version>="3.7" and python_version<"3.10" +numpy==1.21.2; platform_python_implementation!="PyPy" and sys_platform=="linux" and python_version=="3.10" pytest==4.6.9; python_version<"3.5" pytest==6.1.2; python_version=="3.5" -pytest==6.2.1; python_version>="3.6" and python_version<="3.9" -pytest @ git+https://github.com/pytest-dev/pytest@c117bc350ec1e570672fda3b2ad234fd52e72b53; python_version>="3.10" +pytest==6.2.4; python_version>="3.6" pytest-timeout scipy==1.2.3; (platform_python_implementation!="PyPy" or sys_platform=="linux") and python_version<"3.6" scipy==1.5.4; (platform_python_implementation!="PyPy" or sys_platform=="linux") and python_version>="3.6" and python_version<"3.10" From 503ff2a6fbb498138c0b7e85419de491c9860a93 Mon Sep 17 00:00:00 2001 From: Nick Cullen Date: Thu, 26 Aug 2021 23:11:01 +0200 Subject: [PATCH 152/638] view for numpy arrays (#987) * reshape * more tests * Update numpy.h * Update test_numpy_array.py * array view * test * Update test_numpy_array.cpp * Update numpy.h * Update numpy.h * Update test_numpy_array.cpp * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Fix merge bug * Make clang-tidy happy * Add xfail for PyPy * Fix casting issue * Fix formatting * Apply clang-tidy * Address reviews on additional tests * Fix ordering * Do a little more reordering * Fix typo * Try improving tests * Fix error in reshape * Add one more reshape test * Fix bugs and add test * Relax test * streamlining new tests; removing a few stray msg * Fix style revert * Fix clang-tidy * Misc tweaks: * Comment: matching style in file (///), responsibility sentence, consistent punctuation. * Replacing `unsigned char` with `uint8_t` for max consistency. * Removing `1` from `array_view1` because there is only one. * Partial clang-format-diff. Co-authored-by: ncullen93 Co-authored-by: NC Cullen Co-authored-by: Aaron Gokaslan Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Ralf Grosse-Kunstleve --- include/pybind11/numpy.h | 18 ++++++++++++++++++ tests/test_numpy_array.cpp | 3 +++ tests/test_numpy_array.py | 15 +++++++++++++++ 3 files changed, 36 insertions(+) diff --git a/include/pybind11/numpy.h b/include/pybind11/numpy.h index 0d0cbdfa89..fa128efdd0 100644 --- a/include/pybind11/numpy.h +++ b/include/pybind11/numpy.h @@ -199,6 +199,7 @@ struct npy_api { int (*PyArray_SetBaseObject_)(PyObject *, PyObject *); PyObject* (*PyArray_Resize_)(PyObject*, PyArray_Dims*, int, int); PyObject* (*PyArray_Newshape_)(PyObject*, PyArray_Dims*, int); + PyObject* (*PyArray_View_)(PyObject*, PyObject*, PyObject*); private: enum functions { @@ -216,6 +217,7 @@ struct npy_api { API_PyArray_DescrNewFromType = 96, API_PyArray_Newshape = 135, API_PyArray_Squeeze = 136, + API_PyArray_View = 137, API_PyArray_DescrConverter = 174, API_PyArray_EquivTypes = 182, API_PyArray_GetArrayParamsFromObject = 278, @@ -248,6 +250,7 @@ struct npy_api { DECL_NPY_API(PyArray_DescrNewFromType); DECL_NPY_API(PyArray_Newshape); DECL_NPY_API(PyArray_Squeeze); + DECL_NPY_API(PyArray_View); DECL_NPY_API(PyArray_DescrConverter); DECL_NPY_API(PyArray_EquivTypes); DECL_NPY_API(PyArray_GetArrayParamsFromObject); @@ -802,6 +805,21 @@ class array : public buffer { return new_array; } + /// Create a view of an array in a different data type. + /// This function may fundamentally reinterpret the data in the array. + /// It is the responsibility of the caller to ensure that this is safe. + /// Only supports the `dtype` argument, the `type` argument is omitted, + /// to be added as needed. + array view(const std::string &dtype) { + auto &api = detail::npy_api::get(); + auto new_view = reinterpret_steal(api.PyArray_View_( + m_ptr, dtype::from_args(pybind11::str(dtype)).release().ptr(), nullptr)); + if (!new_view) { + throw error_already_set(); + } + return new_view; + } + /// Ensure that the argument is a NumPy array /// In case of an error, nullptr is returned and the Python error is cleared. static array ensure(handle h, int ExtraFlags = 0) { diff --git a/tests/test_numpy_array.cpp b/tests/test_numpy_array.cpp index 4ccfd279b0..30a71acc9b 100644 --- a/tests/test_numpy_array.cpp +++ b/tests/test_numpy_array.cpp @@ -405,6 +405,9 @@ TEST_SUBMODULE(numpy_array, sm) { return a; }); + sm.def("array_view", + [](py::array_t a, const std::string &dtype) { return a.view(dtype); }); + sm.def("reshape_initializer_list", [](py::array_t a, size_t N, size_t M, size_t O) { return a.reshape({N, M, O}); }); diff --git a/tests/test_numpy_array.py b/tests/test_numpy_array.py index e96454be48..e4138f0239 100644 --- a/tests/test_numpy_array.py +++ b/tests/test_numpy_array.py @@ -476,6 +476,21 @@ def test_array_create_and_resize(): assert np.all(a == 42.0) +def test_array_view(): + a = np.ones(100 * 4).astype("uint8") + a_float_view = m.array_view(a, "float32") + assert a_float_view.shape == (100 * 1,) # 1 / 4 bytes = 8 / 32 + + a_int16_view = m.array_view(a, "int16") # 1 / 2 bytes = 16 / 32 + assert a_int16_view.shape == (100 * 2,) + + +def test_array_view_invalid(): + a = np.ones(100 * 4).astype("uint8") + with pytest.raises(TypeError): + m.array_view(a, "deadly_dtype") + + def test_reshape_initializer_list(): a = np.arange(2 * 7 * 3) + 1 x = m.reshape_initializer_list(a, 2, 7, 3) From 930bb16c797af642ed4d216cd8972bbd3276dceb Mon Sep 17 00:00:00 2001 From: Dan Date: Thu, 26 Aug 2021 17:12:54 -0400 Subject: [PATCH 153/638] Call PySys_SetArgv when initializing interpreter. (#2341) * Call PySys_SetArgv when initializing interpreter. * Document argc/argv parameters in initialize_interpreter. * Remove manual memory management from set_interpreter_argv in favor of smart pointers. * Use size_t for indexers in set_interpreter_argv. * Minimize macros for flow control in set_interpreter_argv. * Fix 'unused variable' warning on Py2 * whitespace * Define wide_char_arg_deleter outside set_interpreter_argv. * Do sys.path workaround in C++ rather than eval. * Factor out wchar conversion to a separate function. * Restore widened_argv variable declaration. * Fix undeclared widened_arg variable on some paths. * Use delete[] to match new wchar_t[]. * Fix compiler errors * Use PY_VERSION_HEX for a cleaner CVE-2008-5983 mode check. * Fix typo * Use explicit type for deleter so delete[] works cross-compiler. * Always use PySys_SetArgvEx because pybind11 doesn't support pythons that don't include it. * Remove pointless ternary operator. * Use unique_ptr.reset instead of a second initialization. * Rename add_program_dir_to_path parameter to clarify intent. * Add defined() check before evaluating HAVE_BROKEN_MBSTOWCS. * Apply clang-tidy fixes * Pre-commit * refactor: use const for set_interpreter_argv * Try to fix const issue and allocate vector properly * fix: copy strings on Python 2 * Applying clang-format-diff relative to master. The only manual change is an added empty line between pybind11 and system `#include`s. ``` git diff -U0 --no-color master | python3 $HOME/clone/llvm-project/clang/tools/clang-format/clang-format-diff.py -p1 -style=file -i ``` Co-authored-by: Boris Staletic Co-authored-by: Aaron Gokaslan Co-authored-by: Henry Schreiner Co-authored-by: Ralf W. Grosse-Kunstleve --- include/pybind11/embed.h | 101 ++++++++++++++++++++++++-- tests/test_embed/test_interpreter.cpp | 24 ++++++ tests/test_embed/test_interpreter.py | 5 ++ 3 files changed, 122 insertions(+), 8 deletions(-) diff --git a/include/pybind11/embed.h b/include/pybind11/embed.h index 6e777830f9..7b5d7cd24a 100644 --- a/include/pybind11/embed.h +++ b/include/pybind11/embed.h @@ -12,6 +12,9 @@ #include "pybind11.h" #include "eval.h" +#include +#include + #if defined(PYPY_VERSION) # error Embedding the interpreter is not supported with PyPy #endif @@ -83,29 +86,106 @@ struct embedded_module { } }; +struct wide_char_arg_deleter { + void operator()(wchar_t *ptr) const { +#if PY_VERSION_HEX >= 0x030500f0 + // API docs: https://docs.python.org/3/c-api/sys.html#c.Py_DecodeLocale + PyMem_RawFree(ptr); +#else + delete[] ptr; +#endif + } +}; + +inline wchar_t *widen_chars(const char *safe_arg) { +#if PY_VERSION_HEX >= 0x030500f0 + wchar_t *widened_arg = Py_DecodeLocale(safe_arg, nullptr); +#else + wchar_t *widened_arg = nullptr; +# if defined(HAVE_BROKEN_MBSTOWCS) && HAVE_BROKEN_MBSTOWCS + size_t count = strlen(safe_arg); +# else + size_t count = mbstowcs(nullptr, safe_arg, 0); +# endif + if (count != static_cast(-1)) { + widened_arg = new wchar_t[count + 1]; + mbstowcs(widened_arg, safe_arg, count + 1); + } +#endif + return widened_arg; +} + +/// Python 2.x/3.x-compatible version of `PySys_SetArgv` +inline void set_interpreter_argv(int argc, const char *const *argv, bool add_program_dir_to_path) { + // Before it was special-cased in python 3.8, passing an empty or null argv + // caused a segfault, so we have to reimplement the special case ourselves. + bool special_case = (argv == nullptr || argc <= 0); + + const char *const empty_argv[]{"\0"}; + const char *const *safe_argv = special_case ? empty_argv : argv; + if (special_case) + argc = 1; + + auto argv_size = static_cast(argc); +#if PY_MAJOR_VERSION >= 3 + // SetArgv* on python 3 takes wchar_t, so we have to convert. + std::unique_ptr widened_argv(new wchar_t *[argv_size]); + std::vector> widened_argv_entries; + widened_argv_entries.reserve(argv_size); + for (size_t ii = 0; ii < argv_size; ++ii) { + widened_argv_entries.emplace_back(widen_chars(safe_argv[ii])); + if (!widened_argv_entries.back()) { + // A null here indicates a character-encoding failure or the python + // interpreter out of memory. Give up. + return; + } + widened_argv[ii] = widened_argv_entries.back().get(); + } + + auto pysys_argv = widened_argv.get(); +#else + // python 2.x + std::vector strings{safe_argv, safe_argv + argv_size}; + std::vector char_strings{argv_size}; + for (std::size_t i = 0; i < argv_size; ++i) + char_strings[i] = &strings[i][0]; + char **pysys_argv = char_strings.data(); +#endif + + PySys_SetArgvEx(argc, pysys_argv, static_cast(add_program_dir_to_path)); +} + PYBIND11_NAMESPACE_END(detail) /** \rst Initialize the Python interpreter. No other pybind11 or CPython API functions can be called before this is done; with the exception of `PYBIND11_EMBEDDED_MODULE`. The - optional parameter can be used to skip the registration of signal handlers (see the - `Python documentation`_ for details). Calling this function again after the interpreter - has already been initialized is a fatal error. + optional `init_signal_handlers` parameter can be used to skip the registration of + signal handlers (see the `Python documentation`_ for details). Calling this function + again after the interpreter has already been initialized is a fatal error. If initializing the Python interpreter fails, then the program is terminated. (This is controlled by the CPython runtime and is an exception to pybind11's normal behavior of throwing exceptions on errors.) + The remaining optional parameters, `argc`, `argv`, and `add_program_dir_to_path` are + used to populate ``sys.argv`` and ``sys.path``. + See the |PySys_SetArgvEx documentation|_ for details. + .. _Python documentation: https://docs.python.org/3/c-api/init.html#c.Py_InitializeEx + .. |PySys_SetArgvEx documentation| replace:: ``PySys_SetArgvEx`` documentation + .. _PySys_SetArgvEx documentation: https://docs.python.org/3/c-api/init.html#c.PySys_SetArgvEx \endrst */ -inline void initialize_interpreter(bool init_signal_handlers = true) { +inline void initialize_interpreter(bool init_signal_handlers = true, + int argc = 0, + const char *const *argv = nullptr, + bool add_program_dir_to_path = true) { if (Py_IsInitialized() != 0) pybind11_fail("The interpreter is already running"); Py_InitializeEx(init_signal_handlers ? 1 : 0); - // Make .py files in the working directory available by default - module_::import("sys").attr("path").cast().append("."); + detail::set_interpreter_argv(argc, argv, add_program_dir_to_path); } /** \rst @@ -167,6 +247,8 @@ inline void finalize_interpreter() { Scope guard version of `initialize_interpreter` and `finalize_interpreter`. This a move-only guard and only a single instance can exist. + See `initialize_interpreter` for a discussion of its constructor arguments. + .. code-block:: cpp #include @@ -178,8 +260,11 @@ inline void finalize_interpreter() { \endrst */ class scoped_interpreter { public: - scoped_interpreter(bool init_signal_handlers = true) { - initialize_interpreter(init_signal_handlers); + scoped_interpreter(bool init_signal_handlers = true, + int argc = 0, + const char *const *argv = nullptr, + bool add_program_dir_to_path = true) { + initialize_interpreter(init_signal_handlers, argc, argv, add_program_dir_to_path); } scoped_interpreter(const scoped_interpreter &) = delete; diff --git a/tests/test_embed/test_interpreter.cpp b/tests/test_embed/test_interpreter.cpp index b40ff48175..78b64be6b0 100644 --- a/tests/test_embed/test_interpreter.cpp +++ b/tests/test_embed/test_interpreter.cpp @@ -23,6 +23,7 @@ class Widget { std::string the_message() const { return message; } virtual int the_answer() const = 0; + virtual std::string argv0() const = 0; private: std::string message; @@ -32,6 +33,7 @@ class PyWidget final : public Widget { using Widget::Widget; int the_answer() const override { PYBIND11_OVERRIDE_PURE(int, Widget, the_answer); } + std::string argv0() const override { PYBIND11_OVERRIDE_PURE(std::string, Widget, argv0); } }; PYBIND11_EMBEDDED_MODULE(widget_module, m) { @@ -299,3 +301,25 @@ TEST_CASE("Reload module from file") { result = module_.attr("test")().cast(); REQUIRE(result == 2); } + +TEST_CASE("sys.argv gets initialized properly") { + py::finalize_interpreter(); + { + py::scoped_interpreter default_scope; + auto module = py::module::import("test_interpreter"); + auto py_widget = module.attr("DerivedWidget")("The question"); + const auto &cpp_widget = py_widget.cast(); + REQUIRE(cpp_widget.argv0().empty()); + } + + { + char *argv[] = {strdup("a.out")}; + py::scoped_interpreter argv_scope(true, 1, argv); + free(argv[0]); + auto module = py::module::import("test_interpreter"); + auto py_widget = module.attr("DerivedWidget")("The question"); + const auto &cpp_widget = py_widget.cast(); + REQUIRE(cpp_widget.argv0() == "a.out"); + } + py::initialize_interpreter(); +} diff --git a/tests/test_embed/test_interpreter.py b/tests/test_embed/test_interpreter.py index 6174ede446..5ab55a4b37 100644 --- a/tests/test_embed/test_interpreter.py +++ b/tests/test_embed/test_interpreter.py @@ -1,4 +1,6 @@ # -*- coding: utf-8 -*- +import sys + from widget_module import Widget @@ -8,3 +10,6 @@ def __init__(self, message): def the_answer(self): return 42 + + def argv0(self): + return sys.argv[0] From cb60ed49e46fb31cc578b86b2178d8ce574617ad Mon Sep 17 00:00:00 2001 From: Ye Zhihao Date: Fri, 27 Aug 2021 05:34:24 +0800 Subject: [PATCH 154/638] Fix enum value's __int__ returning non-int when underlying type is bool or of char type (#1334) * Use equivalent_integer for enum's Scalar decision * Add test for char underlying enum * Support translating bool type in enum's Scalar * Add test for bool underlying enum * Fix comment in test * Switch from `PYBIND11_CPP20` macro to `PYBIND11_HAS_U8STRING` * Refine tests Co-authored-by: Aaron Gokaslan --- include/pybind11/pybind11.h | 21 +++++++++++-- tests/test_enum.cpp | 61 +++++++++++++++++++++++++++++++++++++ tests/test_enum.py | 28 +++++++++++++++++ 3 files changed, 108 insertions(+), 2 deletions(-) diff --git a/include/pybind11/pybind11.h b/include/pybind11/pybind11.h index 47b042147b..89f9cbd9b8 100644 --- a/include/pybind11/pybind11.h +++ b/include/pybind11/pybind11.h @@ -1814,6 +1814,19 @@ struct enum_base { handle m_parent; }; +template struct equivalent_integer {}; +template <> struct equivalent_integer { using type = int8_t; }; +template <> struct equivalent_integer { using type = uint8_t; }; +template <> struct equivalent_integer { using type = int16_t; }; +template <> struct equivalent_integer { using type = uint16_t; }; +template <> struct equivalent_integer { using type = int32_t; }; +template <> struct equivalent_integer { using type = uint32_t; }; +template <> struct equivalent_integer { using type = int64_t; }; +template <> struct equivalent_integer { using type = uint64_t; }; + +template +using equivalent_integer_t = typename equivalent_integer::value, sizeof(IntLike)>::type; + PYBIND11_NAMESPACE_END(detail) /// Binds C++ enumerations and enumeration classes to Python @@ -1824,13 +1837,17 @@ template class enum_ : public class_ { using Base::attr; using Base::def_property_readonly; using Base::def_property_readonly_static; - using Scalar = typename std::underlying_type::type; + using Underlying = typename std::underlying_type::type; + // Scalar is the integer representation of underlying type + using Scalar = detail::conditional_t, std::is_same + >::value, detail::equivalent_integer_t, Underlying>; template enum_(const handle &scope, const char *name, const Extra&... extra) : class_(scope, name, extra...), m_base(*this, scope) { constexpr bool is_arithmetic = detail::any_of...>::value; - constexpr bool is_convertible = std::is_convertible::value; + constexpr bool is_convertible = std::is_convertible::value; m_base.init(is_arithmetic, is_convertible); def(init([](Scalar i) { return static_cast(i); }), arg("value")); diff --git a/tests/test_enum.cpp b/tests/test_enum.cpp index 3153089208..40c48d412a 100644 --- a/tests/test_enum.cpp +++ b/tests/test_enum.cpp @@ -84,4 +84,65 @@ TEST_SUBMODULE(enums, m) { .value("ONE", SimpleEnum::THREE) .export_values(); }); + + // test_enum_scalar + enum UnscopedUCharEnum : unsigned char {}; + enum class ScopedShortEnum : short {}; + enum class ScopedLongEnum : long {}; + enum UnscopedUInt64Enum : std::uint64_t {}; + static_assert(py::detail::all_of< + std::is_same::Scalar, unsigned char>, + std::is_same::Scalar, short>, + std::is_same::Scalar, long>, + std::is_same::Scalar, std::uint64_t> + >::value, "Error during the deduction of enum's scalar type with normal integer underlying"); + + // test_enum_scalar_with_char_underlying + enum class ScopedCharEnum : char { Zero, Positive }; + enum class ScopedWCharEnum : wchar_t { Zero, Positive }; + enum class ScopedChar32Enum : char32_t { Zero, Positive }; + enum class ScopedChar16Enum : char16_t { Zero, Positive }; + + // test the scalar of char type enums according to chapter 'Character types' + // from https://en.cppreference.com/w/cpp/language/types + static_assert(py::detail::any_of< + std::is_same::Scalar, signed char>, // e.g. gcc on x86 + std::is_same::Scalar, unsigned char> // e.g. arm linux + >::value, "char should be cast to either signed char or unsigned char"); + static_assert( + sizeof(py::enum_::Scalar) == 2 || + sizeof(py::enum_::Scalar) == 4 + , "wchar_t should be either 16 bits (Windows) or 32 (everywhere else)"); + static_assert(py::detail::all_of< + std::is_same::Scalar, std::uint_least32_t>, + std::is_same::Scalar, std::uint_least16_t> + >::value, "char32_t, char16_t (and char8_t)'s size, signedness, and alignment is determined"); +#if defined(PYBIND11_HAS_U8STRING) + enum class ScopedChar8Enum : char8_t { Zero, Positive }; + static_assert(std::is_same::Scalar, unsigned char>::value); +#endif + + // test_char_underlying_enum + py::enum_(m, "ScopedCharEnum") + .value("Zero", ScopedCharEnum::Zero) + .value("Positive", ScopedCharEnum::Positive); + py::enum_(m, "ScopedWCharEnum") + .value("Zero", ScopedWCharEnum::Zero) + .value("Positive", ScopedWCharEnum::Positive); + py::enum_(m, "ScopedChar32Enum") + .value("Zero", ScopedChar32Enum::Zero) + .value("Positive", ScopedChar32Enum::Positive); + py::enum_(m, "ScopedChar16Enum") + .value("Zero", ScopedChar16Enum::Zero) + .value("Positive", ScopedChar16Enum::Positive); + + // test_bool_underlying_enum + enum class ScopedBoolEnum : bool { FALSE, TRUE }; + + // bool is unsigned (std::is_signed returns false) and 1-byte long, so represented with u8 + static_assert(std::is_same::Scalar, std::uint8_t>::value, ""); + + py::enum_(m, "ScopedBoolEnum") + .value("FALSE", ScopedBoolEnum::FALSE) + .value("TRUE", ScopedBoolEnum::TRUE); } diff --git a/tests/test_enum.py b/tests/test_enum.py index 62f9426eee..11cab6ddf5 100644 --- a/tests/test_enum.py +++ b/tests/test_enum.py @@ -218,10 +218,16 @@ def test_binary_operators(): def test_enum_to_int(): m.test_enum_to_int(m.Flags.Read) m.test_enum_to_int(m.ClassWithUnscopedEnum.EMode.EFirstMode) + m.test_enum_to_int(m.ScopedCharEnum.Positive) + m.test_enum_to_int(m.ScopedBoolEnum.TRUE) m.test_enum_to_uint(m.Flags.Read) m.test_enum_to_uint(m.ClassWithUnscopedEnum.EMode.EFirstMode) + m.test_enum_to_uint(m.ScopedCharEnum.Positive) + m.test_enum_to_uint(m.ScopedBoolEnum.TRUE) m.test_enum_to_long_long(m.Flags.Read) m.test_enum_to_long_long(m.ClassWithUnscopedEnum.EMode.EFirstMode) + m.test_enum_to_long_long(m.ScopedCharEnum.Positive) + m.test_enum_to_long_long(m.ScopedBoolEnum.TRUE) def test_duplicate_enum_name(): @@ -230,6 +236,28 @@ def test_duplicate_enum_name(): assert str(excinfo.value) == 'SimpleEnum: element "ONE" already exists!' +def test_char_underlying_enum(): # Issue #1331/PR #1334: + assert type(m.ScopedCharEnum.Positive.__int__()) is int + assert int(m.ScopedChar16Enum.Zero) == 0 # int() call should successfully return + assert hash(m.ScopedChar32Enum.Positive) == 1 + assert m.ScopedCharEnum.Positive.__getstate__() == 1 # return type is long in py2.x + assert m.ScopedWCharEnum(1) == m.ScopedWCharEnum.Positive + with pytest.raises(TypeError): + # Enum should construct with a int, even with char underlying type + m.ScopedWCharEnum("0") + + +def test_bool_underlying_enum(): + assert type(m.ScopedBoolEnum.TRUE.__int__()) is int + assert int(m.ScopedBoolEnum.FALSE) == 0 + assert hash(m.ScopedBoolEnum.TRUE) == 1 + assert m.ScopedBoolEnum.TRUE.__getstate__() == 1 + assert m.ScopedBoolEnum(1) == m.ScopedBoolEnum.TRUE + # Enum could construct with a bool + # (bool is a strict subclass of int, and False will be converted to 0) + assert m.ScopedBoolEnum(False) == m.ScopedBoolEnum.FALSE + + def test_docstring_signatures(): for enum_type in [m.ScopedEnum, m.UnscopedEnum]: for attr in enum_type.__dict__.values(): From 777352fcd10bd9b57a8f626a6c7397596466ee55 Mon Sep 17 00:00:00 2001 From: "Ralf W. Grosse-Kunstleve" Date: Sat, 28 Aug 2021 16:40:46 -0700 Subject: [PATCH 155/638] Adding `ssize_t_cast` to support passing `size_t` or `ssize_t` values where `ssize_t` is needed. (#3219) * Trivial change to avoid (ssize_t) cast. * Demo for safe_ssize_t idea. * Removing safe_ssize_t.cpp (proof-of-concept code) to not upset the GHA Format workflow. * Completing changes in pytypes.h * New ssize_t_cast (better replacement for safe_ssize_t). * clang-format-diff (no manual changes). * bytes_ssize_t -Wnarrowing reproducer (see PR #2692). * Backing out tuple(), list() ssize_t support, for compatibility with older compilers (to resolve link failures). * Bug fix: missing `py::` for `py::ssize_t` * Restoring tuple(), list() ssize_t support, but passing `size` by value, for compatibility with older compilers (to resolve link failures). * Full test coverage of all functions with modified signatures. --- include/pybind11/detail/common.h | 6 +++ include/pybind11/pytypes.h | 66 +++++++++++++++++++------------ include/pybind11/stl.h | 8 ++-- tests/test_pytypes.cpp | 67 ++++++++++++++++++++++++++++++++ tests/test_pytypes.py | 39 +++++++++++++++++++ 5 files changed, 158 insertions(+), 28 deletions(-) diff --git a/include/pybind11/detail/common.h b/include/pybind11/detail/common.h index 5050da802a..e4f00293ec 100644 --- a/include/pybind11/detail/common.h +++ b/include/pybind11/detail/common.h @@ -410,6 +410,12 @@ PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE) using ssize_t = Py_ssize_t; using size_t = std::size_t; +template +inline ssize_t ssize_t_cast(const IntType &val) { + static_assert(sizeof(IntType) <= sizeof(ssize_t), "Implicit narrowing is not permitted."); + return static_cast(val); +} + /// Approach used to cast a previously unknown C++ instance into a Python object enum class return_value_policy : uint8_t { /** This is the default return value policy, which falls back to the policy diff --git a/include/pybind11/pytypes.h b/include/pybind11/pytypes.h index 85f6a40a48..5e699e53f4 100644 --- a/include/pybind11/pytypes.h +++ b/include/pybind11/pytypes.h @@ -661,15 +661,17 @@ struct generic_item { struct sequence_item { using key_type = size_t; - static object get(handle obj, size_t index) { - PyObject *result = PySequence_GetItem(obj.ptr(), static_cast(index)); + template ::value, int> = 0> + static object get(handle obj, const IdxType &index) { + PyObject *result = PySequence_GetItem(obj.ptr(), ssize_t_cast(index)); if (!result) { throw error_already_set(); } return reinterpret_steal(result); } - static void set(handle obj, size_t index, handle val) { + template ::value, int> = 0> + static void set(handle obj, const IdxType &index, handle val) { // PySequence_SetItem does not steal a reference to 'val' - if (PySequence_SetItem(obj.ptr(), static_cast(index), val.ptr()) != 0) { + if (PySequence_SetItem(obj.ptr(), ssize_t_cast(index), val.ptr()) != 0) { throw error_already_set(); } } @@ -678,15 +680,17 @@ struct sequence_item { struct list_item { using key_type = size_t; - static object get(handle obj, size_t index) { - PyObject *result = PyList_GetItem(obj.ptr(), static_cast(index)); + template ::value, int> = 0> + static object get(handle obj, const IdxType &index) { + PyObject *result = PyList_GetItem(obj.ptr(), ssize_t_cast(index)); if (!result) { throw error_already_set(); } return reinterpret_borrow(result); } - static void set(handle obj, size_t index, handle val) { + template ::value, int> = 0> + static void set(handle obj, const IdxType &index, handle val) { // PyList_SetItem steals a reference to 'val' - if (PyList_SetItem(obj.ptr(), static_cast(index), val.inc_ref().ptr()) != 0) { + if (PyList_SetItem(obj.ptr(), ssize_t_cast(index), val.inc_ref().ptr()) != 0) { throw error_already_set(); } } @@ -695,15 +699,17 @@ struct list_item { struct tuple_item { using key_type = size_t; - static object get(handle obj, size_t index) { - PyObject *result = PyTuple_GetItem(obj.ptr(), static_cast(index)); + template ::value, int> = 0> + static object get(handle obj, const IdxType &index) { + PyObject *result = PyTuple_GetItem(obj.ptr(), ssize_t_cast(index)); if (!result) { throw error_already_set(); } return reinterpret_borrow(result); } - static void set(handle obj, size_t index, handle val) { + template ::value, int> = 0> + static void set(handle obj, const IdxType &index, handle val) { // PyTuple_SetItem steals a reference to 'val' - if (PyTuple_SetItem(obj.ptr(), static_cast(index), val.inc_ref().ptr()) != 0) { + if (PyTuple_SetItem(obj.ptr(), ssize_t_cast(index), val.inc_ref().ptr()) != 0) { throw error_already_set(); } } @@ -1043,8 +1049,9 @@ class str : public object { public: PYBIND11_OBJECT_CVT(str, object, PYBIND11_STR_CHECK_FUN, raw_str) - str(const char *c, size_t n) - : object(PyUnicode_FromStringAndSize(c, (ssize_t) n), stolen_t{}) { + template ::value, int> = 0> + str(const char *c, const SzType &n) + : object(PyUnicode_FromStringAndSize(c, ssize_t_cast(n)), stolen_t{}) { if (!m_ptr) pybind11_fail("Could not allocate string object!"); } @@ -1116,8 +1123,9 @@ class bytes : public object { if (!m_ptr) pybind11_fail("Could not allocate bytes object!"); } - bytes(const char *c, size_t n) - : object(PYBIND11_BYTES_FROM_STRING_AND_SIZE(c, (ssize_t) n), stolen_t{}) { + template ::value, int> = 0> + bytes(const char *c, const SzType &n) + : object(PYBIND11_BYTES_FROM_STRING_AND_SIZE(c, ssize_t_cast(n)), stolen_t{}) { if (!m_ptr) pybind11_fail("Could not allocate bytes object!"); } @@ -1160,7 +1168,7 @@ inline str::str(const bytes& b) { ssize_t length = 0; if (PYBIND11_BYTES_AS_STRING_AND_SIZE(b.ptr(), &buffer, &length)) pybind11_fail("Unable to extract bytes contents!"); - auto obj = reinterpret_steal(PyUnicode_FromStringAndSize(buffer, (ssize_t) length)); + auto obj = reinterpret_steal(PyUnicode_FromStringAndSize(buffer, length)); if (!obj) pybind11_fail("Could not allocate string object!"); m_ptr = obj.release().ptr(); @@ -1172,8 +1180,9 @@ class bytearray : public object { public: PYBIND11_OBJECT_CVT(bytearray, object, PyByteArray_Check, PyByteArray_FromObject) - bytearray(const char *c, size_t n) - : object(PyByteArray_FromStringAndSize(c, (ssize_t) n), stolen_t{}) { + template ::value, int> = 0> + bytearray(const char *c, const SzType &n) + : object(PyByteArray_FromStringAndSize(c, ssize_t_cast(n)), stolen_t{}) { if (!m_ptr) pybind11_fail("Could not allocate bytearray object!"); } @@ -1398,7 +1407,10 @@ class capsule : public object { class tuple : public object { public: PYBIND11_OBJECT_CVT(tuple, object, PyTuple_Check, PySequence_Tuple) - explicit tuple(size_t size = 0) : object(PyTuple_New((ssize_t) size), stolen_t{}) { + template ::value, int> = 0> + // Some compilers generate link errors when using `const SzType &` here: + explicit tuple(SzType size = 0) : object(PyTuple_New(ssize_t_cast(size)), stolen_t{}) { if (!m_ptr) pybind11_fail("Could not allocate tuple object!"); } size_t size() const { return (size_t) PyTuple_Size(m_ptr); } @@ -1467,7 +1479,10 @@ class sequence : public object { class list : public object { public: PYBIND11_OBJECT_CVT(list, object, PyList_Check, PySequence_List) - explicit list(size_t size = 0) : object(PyList_New((ssize_t) size), stolen_t{}) { + template ::value, int> = 0> + // Some compilers generate link errors when using `const SzType &` here: + explicit list(SzType size = 0) : object(PyList_New(ssize_t_cast(size)), stolen_t{}) { if (!m_ptr) pybind11_fail("Could not allocate list object!"); } size_t size() const { return (size_t) PyList_Size(m_ptr); } @@ -1479,9 +1494,12 @@ class list : public object { template void append(T &&val) /* py-non-const */ { PyList_Append(m_ptr, detail::object_or_cast(std::forward(val)).ptr()); } - template void insert(size_t index, T &&val) /* py-non-const */ { - PyList_Insert(m_ptr, static_cast(index), - detail::object_or_cast(std::forward(val)).ptr()); + template ::value, int> = 0> + void insert(const IdxType &index, ValType &&val) /* py-non-const */ { + PyList_Insert( + m_ptr, ssize_t_cast(index), detail::object_or_cast(std::forward(val)).ptr()); } }; diff --git a/include/pybind11/stl.h b/include/pybind11/stl.h index fe391f70bd..99b49d0e2c 100644 --- a/include/pybind11/stl.h +++ b/include/pybind11/stl.h @@ -168,12 +168,12 @@ template struct list_caster { if (!std::is_lvalue_reference::value) policy = return_value_policy_override::policy(policy); list l(src.size()); - size_t index = 0; + ssize_t index = 0; for (auto &&value : src) { auto value_ = reinterpret_steal(value_conv::cast(forward_like(value), policy, parent)); if (!value_) return handle(); - PyList_SET_ITEM(l.ptr(), (ssize_t) index++, value_.release().ptr()); // steals a reference + PyList_SET_ITEM(l.ptr(), index++, value_.release().ptr()); // steals a reference } return l.release(); } @@ -225,12 +225,12 @@ template s template static handle cast(T &&src, return_value_policy policy, handle parent) { list l(src.size()); - size_t index = 0; + ssize_t index = 0; for (auto &&value : src) { auto value_ = reinterpret_steal(value_conv::cast(forward_like(value), policy, parent)); if (!value_) return handle(); - PyList_SET_ITEM(l.ptr(), (ssize_t) index++, value_.release().ptr()); // steals a reference + PyList_SET_ITEM(l.ptr(), index++, value_.release().ptr()); // steals a reference } return l.release(); } diff --git a/tests/test_pytypes.cpp b/tests/test_pytypes.cpp index 15d007a437..96c97351e1 100644 --- a/tests/test_pytypes.cpp +++ b/tests/test_pytypes.cpp @@ -20,6 +20,11 @@ TEST_SUBMODULE(pytypes, m) { // test_iterable m.def("get_iterable", []{return py::iterable();}); // test_list + m.def("list_no_args", []() { return py::list{}; }); + m.def("list_ssize_t", []() { return py::list{(py::ssize_t) 0}; }); + m.def("list_size_t", []() { return py::list{(py::size_t) 0}; }); + m.def("list_insert_ssize_t", [](py::list *l) { return l->insert((py::ssize_t) 1, 83); }); + m.def("list_insert_size_t", [](py::list *l) { return l->insert((py::size_t) 3, 57); }); m.def("get_list", []() { py::list list; list.append("value"); @@ -71,6 +76,9 @@ TEST_SUBMODULE(pytypes, m) { [](const py::dict &dict, const char *val) { return dict.contains(val); }); // test_tuple + m.def("tuple_no_args", []() { return py::tuple{}; }); + m.def("tuple_ssize_t", []() { return py::tuple{(py::ssize_t) 0}; }); + m.def("tuple_size_t", []() { return py::tuple{(py::size_t) 0}; }); m.def("get_tuple", []() { return py::make_tuple(42, py::none(), "spam"); }); #if PY_VERSION_HEX >= 0x03030000 @@ -84,6 +92,8 @@ TEST_SUBMODULE(pytypes, m) { #endif // test_str + m.def("str_from_char_ssize_t", []() { return py::str{"red", (py::ssize_t) 3}; }); + m.def("str_from_char_size_t", []() { return py::str{"blue", (py::size_t) 4}; }); m.def("str_from_string", []() { return py::str(std::string("baz")); }); m.def("str_from_bytes", []() { return py::str(py::bytes("boo", 3)); }); m.def("str_from_object", [](const py::object& obj) { return py::str(obj); }); @@ -100,10 +110,14 @@ TEST_SUBMODULE(pytypes, m) { }); // test_bytes + m.def("bytes_from_char_ssize_t", []() { return py::bytes{"green", (py::ssize_t) 5}; }); + m.def("bytes_from_char_size_t", []() { return py::bytes{"purple", (py::size_t) 6}; }); m.def("bytes_from_string", []() { return py::bytes(std::string("foo")); }); m.def("bytes_from_str", []() { return py::bytes(py::str("bar", 3)); }); // test bytearray + m.def("bytearray_from_char_ssize_t", []() { return py::bytearray{"$%", (py::ssize_t) 2}; }); + m.def("bytearray_from_char_size_t", []() { return py::bytearray{"@$!", (py::size_t) 3}; }); m.def("bytearray_from_string", []() { return py::bytearray(std::string("foo")); }); m.def("bytearray_size", []() { return py::bytearray("foo").size(); }); @@ -447,4 +461,57 @@ TEST_SUBMODULE(pytypes, m) { m.def("weakref_from_object", [](const py::object &o) { return py::weakref(o); }); m.def("weakref_from_object_and_function", [](py::object o, py::function f) { return py::weakref(std::move(o), std::move(f)); }); + + // Tests below this line are for pybind11 IMPLEMENTATION DETAILS: + + m.def("sequence_item_get_ssize_t", [](const py::object &o) { + return py::detail::accessor_policies::sequence_item::get(o, (py::ssize_t) 1); + }); + m.def("sequence_item_set_ssize_t", [](const py::object &o) { + auto s = py::str{"peppa", 5}; + py::detail::accessor_policies::sequence_item::set(o, (py::ssize_t) 1, s); + }); + m.def("sequence_item_get_size_t", [](const py::object &o) { + return py::detail::accessor_policies::sequence_item::get(o, (py::size_t) 2); + }); + m.def("sequence_item_set_size_t", [](const py::object &o) { + auto s = py::str{"george", 6}; + py::detail::accessor_policies::sequence_item::set(o, (py::size_t) 2, s); + }); + m.def("list_item_get_ssize_t", [](const py::object &o) { + return py::detail::accessor_policies::list_item::get(o, (py::ssize_t) 3); + }); + m.def("list_item_set_ssize_t", [](const py::object &o) { + auto s = py::str{"rebecca", 7}; + py::detail::accessor_policies::list_item::set(o, (py::ssize_t) 3, s); + }); + m.def("list_item_get_size_t", [](const py::object &o) { + return py::detail::accessor_policies::list_item::get(o, (py::size_t) 4); + }); + m.def("list_item_set_size_t", [](const py::object &o) { + auto s = py::str{"richard", 7}; + py::detail::accessor_policies::list_item::set(o, (py::size_t) 4, s); + }); + m.def("tuple_item_get_ssize_t", [](const py::object &o) { + return py::detail::accessor_policies::tuple_item::get(o, (py::ssize_t) 5); + }); + m.def("tuple_item_set_ssize_t", []() { + auto s0 = py::str{"emely", 5}; + auto s1 = py::str{"edmond", 6}; + auto o = py::tuple{2}; + py::detail::accessor_policies::tuple_item::set(o, (py::ssize_t) 0, s0); + py::detail::accessor_policies::tuple_item::set(o, (py::ssize_t) 1, s1); + return o; + }); + m.def("tuple_item_get_size_t", [](const py::object &o) { + return py::detail::accessor_policies::tuple_item::get(o, (py::size_t) 6); + }); + m.def("tuple_item_set_size_t", []() { + auto s0 = py::str{"candy", 5}; + auto s1 = py::str{"cat", 3}; + auto o = py::tuple{2}; + py::detail::accessor_policies::tuple_item::set(o, (py::size_t) 1, s1); + py::detail::accessor_policies::tuple_item::set(o, (py::size_t) 0, s0); + return o; + }); } diff --git a/tests/test_pytypes.py b/tests/test_pytypes.py index f873658abe..18847550f2 100644 --- a/tests/test_pytypes.py +++ b/tests/test_pytypes.py @@ -23,6 +23,15 @@ def test_iterable(doc): def test_list(capture, doc): + assert m.list_no_args() == [] + assert m.list_ssize_t() == [] + assert m.list_size_t() == [] + lins = [1, 2] + m.list_insert_ssize_t(lins) + assert lins == [1, 83, 2] + m.list_insert_size_t(lins) + assert lins == [1, 83, 2, 57] + with capture: lst = m.get_list() assert lst == ["inserted-0", "overwritten", "inserted-2"] @@ -100,6 +109,9 @@ def test_dict(capture, doc): def test_tuple(): + assert m.tuple_no_args() == () + assert m.tuple_ssize_t() == () + assert m.tuple_size_t() == () assert m.get_tuple() == (42, None, "spam") @@ -113,6 +125,8 @@ def test_simple_namespace(): def test_str(doc): + assert m.str_from_char_ssize_t().encode().decode() == "red" + assert m.str_from_char_size_t().encode().decode() == "blue" assert m.str_from_string().encode().decode() == "baz" assert m.str_from_bytes().encode().decode() == "boo" @@ -157,6 +171,8 @@ def __repr__(self): def test_bytes(doc): + assert m.bytes_from_char_ssize_t().decode() == "green" + assert m.bytes_from_char_size_t().decode() == "purple" assert m.bytes_from_string().decode() == "foo" assert m.bytes_from_str().decode() == "bar" @@ -166,6 +182,8 @@ def test_bytes(doc): def test_bytearray(doc): + assert m.bytearray_from_char_ssize_t().decode() == "$%" + assert m.bytearray_from_char_size_t().decode() == "@$!" assert m.bytearray_from_string().decode() == "foo" assert m.bytearray_size() == len("foo") @@ -603,3 +621,24 @@ def callback(wr): del obj pytest.gc_collect() assert callback.called + + +def test_implementation_details(): + lst = [39, 43, 92, 49, 22, 29, 93, 98, 26, 57, 8] + tup = tuple(lst) + assert m.sequence_item_get_ssize_t(lst) == 43 + assert m.sequence_item_set_ssize_t(lst) is None + assert lst[1] == "peppa" + assert m.sequence_item_get_size_t(lst) == 92 + assert m.sequence_item_set_size_t(lst) is None + assert lst[2] == "george" + assert m.list_item_get_ssize_t(lst) == 49 + assert m.list_item_set_ssize_t(lst) is None + assert lst[3] == "rebecca" + assert m.list_item_get_size_t(lst) == 22 + assert m.list_item_set_size_t(lst) is None + assert lst[4] == "richard" + assert m.tuple_item_get_ssize_t(tup) == 29 + 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") From 76d939de537dc1d95b8c387ea4633c9c3a8448d2 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 30 Aug 2021 16:00:33 -0400 Subject: [PATCH 156/638] [pre-commit.ci] pre-commit autoupdate (#3231) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit updates: - [github.com/asottile/pyupgrade: v2.24.0 → v2.25.0](https://github.com/asottile/pyupgrade/compare/v2.24.0...v2.25.0) - [github.com/psf/black: 21.7b0 → 21.8b0](https://github.com/psf/black/compare/21.7b0...21.8b0) Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> --- .pre-commit-config.yaml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 96ddcf7469..06e82fe4b1 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -31,7 +31,7 @@ repos: exclude: ^noxfile.py$ - repo: https://github.com/asottile/pyupgrade - rev: v2.24.0 + rev: v2.25.0 hooks: - id: pyupgrade @@ -42,7 +42,7 @@ repos: # Black, the code formatter, natively supports pre-commit - repo: https://github.com/psf/black - rev: 21.7b0 + rev: 21.8b0 hooks: - id: black # By default, this ignores pyi files, though black supports them From d6474ed7d28c03ef79acd2e87ca429072727df86 Mon Sep 17 00:00:00 2001 From: Tailing Yuan Date: Tue, 31 Aug 2021 09:48:33 +0800 Subject: [PATCH 157/638] fix: memory leak in cpp_function (#3228) (#3229) * fix: memory leak in cpp_function (#3228) * add a test case to check objects are deconstructed in cpp_function * update the test case about cpp_function * fix the test case about cpp_function: remove "noexcept" * Actually calling func. CHECK(stat.alive() == 2); Manually verified that the new tests fails without the change in pybind11.h * Moving new test to test_callbacks.cpp,py, with small enhancements. * Removing new test from test_interpreter.cpp (after it was moved to test_callbacks.cpp,py). This restores test_interpreter.cpp to the current state on master. * Using py::detail::silence_unused_warnings(py_func); to make the intent clear. Co-authored-by: Ralf W. Grosse-Kunstleve --- include/pybind11/pybind11.h | 2 +- tests/test_callbacks.cpp | 43 +++++++++++++++++++++++++++++++++++-- tests/test_callbacks.py | 7 +++++- 3 files changed, 48 insertions(+), 4 deletions(-) diff --git a/include/pybind11/pybind11.h b/include/pybind11/pybind11.h index 89f9cbd9b8..990c213049 100644 --- a/include/pybind11/pybind11.h +++ b/include/pybind11/pybind11.h @@ -175,7 +175,7 @@ class cpp_function : public function { #endif // UB without std::launder, but without breaking ABI and/or // a significant refactoring it's "impossible" to solve. - if (!std::is_trivially_destructible::value) + if (!std::is_trivially_destructible::value) rec->free_data = [](function_record *r) { auto data = PYBIND11_STD_LAUNDER((capture *) &r->data); (void) data; diff --git a/tests/test_callbacks.cpp b/tests/test_callbacks.cpp index a50771038b..58688b6e8b 100644 --- a/tests/test_callbacks.cpp +++ b/tests/test_callbacks.cpp @@ -81,16 +81,55 @@ TEST_SUBMODULE(callbacks, m) { }; // Export the payload constructor statistics for testing purposes: m.def("payload_cstats", &ConstructorStats::get); - /* Test cleanup of lambda closure */ - m.def("test_cleanup", []() -> std::function { + m.def("test_lambda_closure_cleanup", []() -> std::function { Payload p; + // In this situation, `Func` in the implementation of + // `cpp_function::initialize` is NOT trivially destructible. return [p]() { /* p should be cleaned up when the returned function is garbage collected */ (void) p; }; }); + class CppCallable { + public: + CppCallable() { track_default_created(this); } + ~CppCallable() { track_destroyed(this); } + CppCallable(const CppCallable &) { track_copy_created(this); } + CppCallable(CppCallable &&) noexcept { track_move_created(this); } + void operator()() {} + }; + + m.def("test_cpp_callable_cleanup", []() { + // Related issue: https://github.com/pybind/pybind11/issues/3228 + // Related PR: https://github.com/pybind/pybind11/pull/3229 + py::list alive_counts; + ConstructorStats &stat = ConstructorStats::get(); + alive_counts.append(stat.alive()); + { + CppCallable cpp_callable; + alive_counts.append(stat.alive()); + { + // In this situation, `Func` in the implementation of + // `cpp_function::initialize` IS trivially destructible, + // only `capture` is not. + py::cpp_function py_func(cpp_callable); + py::detail::silence_unused_warnings(py_func); + alive_counts.append(stat.alive()); + } + alive_counts.append(stat.alive()); + { + py::cpp_function py_func(std::move(cpp_callable)); + py::detail::silence_unused_warnings(py_func); + alive_counts.append(stat.alive()); + } + alive_counts.append(stat.alive()); + } + alive_counts.append(stat.alive()); + return alive_counts; + }); + // test_cpp_function_roundtrip /* Test if passing a function pointer from C++ -> Python -> C++ yields the original pointer */ m.def("dummy_function", &dummy_function); diff --git a/tests/test_callbacks.py b/tests/test_callbacks.py index 9dc272a2da..edbb1890c9 100644 --- a/tests/test_callbacks.py +++ b/tests/test_callbacks.py @@ -79,13 +79,18 @@ def f(*args, **kwargs): def test_lambda_closure_cleanup(): - m.test_cleanup() + m.test_lambda_closure_cleanup() cstats = m.payload_cstats() assert cstats.alive() == 0 assert cstats.copy_constructions == 1 assert cstats.move_constructions >= 1 +def test_cpp_callable_cleanup(): + alive_counts = m.test_cpp_callable_cleanup() + assert alive_counts == [0, 1, 2, 1, 2, 1, 0] + + def test_cpp_function_roundtrip(): """Test if passing a function pointer from C++ -> Python -> C++ yields the original pointer""" From a46f623782df6a92a737146890d9c070fc19b99b Mon Sep 17 00:00:00 2001 From: "Ralf W. Grosse-Kunstleve" Date: Tue, 31 Aug 2021 06:44:14 -0700 Subject: [PATCH 158/638] Minor tweaks. (#3230) * Minor tweaks. * Restoring tests/pybind11_tests.h version from master, removing just the comment and empty line that was added in PR #3087; those were made obsolete by the pragma cleanup that concluded with PR #3186. * [ci skip] Restoring tests/test_enum.py from master. --- tests/pybind11_tests.h | 3 --- 1 file changed, 3 deletions(-) diff --git a/tests/pybind11_tests.h b/tests/pybind11_tests.h index 12d8a777fc..8da0a670f2 100644 --- a/tests/pybind11_tests.h +++ b/tests/pybind11_tests.h @@ -1,9 +1,6 @@ #pragma once -// This must be kept first for MSVC 2015. -// Do not remove the empty line between the #includes. #include - #include #if defined(_MSC_VER) && _MSC_VER < 1910 From 49173e472f9d0f38654ccb20daf203d116b83b39 Mon Sep 17 00:00:00 2001 From: "Ralf W. Grosse-Kunstleve" Date: Tue, 31 Aug 2021 08:52:04 -0700 Subject: [PATCH 159/638] Minor follow-on to PR #1334 (Fix enum value's __int__ returning non-int when underlying type is bool or of char type) (#3232) * Minor tweaks. * Restoring tests/pybind11_tests.h version from master, removing just the comment and empty line that was added in PR #3087; those were made obsolete by the pragma cleanup that concluded with PR #3186. * More-to-the-point test for Python 3. --- tests/test_enum.py | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/tests/test_enum.py b/tests/test_enum.py index 11cab6ddf5..85302b0809 100644 --- a/tests/test_enum.py +++ b/tests/test_enum.py @@ -1,6 +1,7 @@ # -*- coding: utf-8 -*- import pytest +import env from pybind11_tests import enums as m @@ -238,20 +239,26 @@ def test_duplicate_enum_name(): def test_char_underlying_enum(): # Issue #1331/PR #1334: assert type(m.ScopedCharEnum.Positive.__int__()) is int - assert int(m.ScopedChar16Enum.Zero) == 0 # int() call should successfully return + assert int(m.ScopedChar16Enum.Zero) == 0 assert hash(m.ScopedChar32Enum.Positive) == 1 - assert m.ScopedCharEnum.Positive.__getstate__() == 1 # return type is long in py2.x + if env.PY2: + assert m.ScopedCharEnum.Positive.__getstate__() == 1 # long + else: + assert type(m.ScopedCharEnum.Positive.__getstate__()) is int assert m.ScopedWCharEnum(1) == m.ScopedWCharEnum.Positive with pytest.raises(TypeError): - # Enum should construct with a int, even with char underlying type - m.ScopedWCharEnum("0") + # Even if the underlying type is char, only an int can be used to construct the enum: + m.ScopedCharEnum("0") def test_bool_underlying_enum(): assert type(m.ScopedBoolEnum.TRUE.__int__()) is int assert int(m.ScopedBoolEnum.FALSE) == 0 assert hash(m.ScopedBoolEnum.TRUE) == 1 - assert m.ScopedBoolEnum.TRUE.__getstate__() == 1 + if env.PY2: + assert m.ScopedBoolEnum.TRUE.__getstate__() == 1 # long + else: + assert type(m.ScopedBoolEnum.TRUE.__getstate__()) is int assert m.ScopedBoolEnum(1) == m.ScopedBoolEnum.TRUE # Enum could construct with a bool # (bool is a strict subclass of int, and False will be converted to 0) From d71ba0cb73616c493d35699a8a9283aa64ef0f6b Mon Sep 17 00:00:00 2001 From: Aaron Gokaslan Date: Thu, 2 Sep 2021 15:18:42 -0400 Subject: [PATCH 160/638] (perf): Add a missing noexcept to a pytype constructor (#3236) * Add a missing noexcept to pytypes constructor. Adds a few missing noexcept to PyType constructors for perf reasons. * Revert exception ctor --- include/pybind11/pytypes.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/pybind11/pytypes.h b/include/pybind11/pytypes.h index 5e699e53f4..fa50eb1e3a 100644 --- a/include/pybind11/pytypes.h +++ b/include/pybind11/pytypes.h @@ -761,7 +761,7 @@ template struct arrow_proxy { T value; - arrow_proxy(T &&value) : value(std::move(value)) { } + arrow_proxy(T &&value) noexcept : value(std::move(value)) { } T *operator->() const { return &value; } }; From 39a0aac88ea2eb9d08293f71f636edc16e536a50 Mon Sep 17 00:00:00 2001 From: Thomas Ballinger Date: Wed, 8 Sep 2021 13:00:00 -0500 Subject: [PATCH 161/638] docs fix to avoid nonexistent SmartCompile (#3241) --- docs/compiling.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/compiling.rst b/docs/compiling.rst index bf7acfc808..eaf3270e0e 100644 --- a/docs/compiling.rst +++ b/docs/compiling.rst @@ -113,7 +113,7 @@ with the following: from pybind11.setup_helpers import ParallelCompile, naive_recompile - SmartCompile("NPY_NUM_BUILD_JOBS", needs_recompile=naive_recompile).install() + ParallelCompile("NPY_NUM_BUILD_JOBS", needs_recompile=naive_recompile).install() If you have a more complex build, you can implement a smarter function and pass From 6abf2baa62063a39ccc4a9535351de571004ddd4 Mon Sep 17 00:00:00 2001 From: "Ralf W. Grosse-Kunstleve" Date: Wed, 8 Sep 2021 18:53:38 -0700 Subject: [PATCH 162/638] CodeHealth: Enabling clang-tidy google-explicit-constructor (#3250) * Adding google-explicit-constructor to .clang-tidy * clang-tidy explicit attr.h (all automatic) * clang-tidy explicit cast.h (all automatic) * clang-tidy detail/init.h (1 NOLINT) * clang-tidy detail/type_caster_base.h (2 NOLINT) * clang-tidy pybind11.h (7 NOLINT) * clang-tidy detail/common.h (3 NOLINT) * clang-tidy detail/descr.h (2 NOLINT) * clang-tidy pytypes.h (23 NOLINT, only 1 explicit) * clang-tidy eigen.h (7 NOLINT, 0 explicit) * Adding 2 explicit in functional.h * Adding 4 explicit in iostream.h * clang-tidy numpy.h (1 NOLINT, 1 explicit) * clang-tidy embed.h (0 NOLINT, 1 explicit) * clang-tidy tests/local_bindings.h (0 NOLINT, 4 explicit) * clang-tidy tests/pybind11_cross_module_tests.cpp (0 NOLINT, 1 explicit) * clang-tidy tests/pybind11_tests.h (0 NOLINT, 2 explicit) * clang-tidy tests/test_buffers.cpp (0 NOLINT, 2 explicit) * clang-tidy tests/test_builtin_casters.cpp (0 NOLINT, 4 explicit) * clang-tidy tests/test_class.cpp (0 NOLINT, 6 explicit) * clang-tidy tests/test_copy_move.cpp (0 NOLINT, 7 explicit) * clang-tidy tests/test_embed/external_module.cpp (0 NOLINT, 1 explicit) * clang-tidy tests/test_embed/test_interpreter.cpp (0 NOLINT, 1 explicit) * clang-tidy tests/object.h (0 NOLINT, 2 explicit) * clang-tidy batch of fully automatic fixes. * Workaround for MSVC 19.16.27045.0 C++17 Python 2 C++ syntax error. --- .clang-tidy | 1 + include/pybind11/attr.h | 24 ++++++++--- include/pybind11/cast.h | 14 ++++--- include/pybind11/detail/common.h | 3 ++ include/pybind11/detail/descr.h | 2 + include/pybind11/detail/init.h | 5 ++- include/pybind11/detail/type_caster_base.h | 18 ++++---- include/pybind11/eigen.h | 7 ++++ include/pybind11/embed.h | 8 ++-- include/pybind11/functional.h | 6 ++- include/pybind11/iostream.h | 14 ++++--- include/pybind11/numpy.h | 3 +- include/pybind11/pybind11.h | 9 +++- include/pybind11/pytypes.h | 25 ++++++++++- tests/local_bindings.h | 8 ++-- tests/object.h | 4 +- tests/pybind11_cross_module_tests.cpp | 2 +- tests/pybind11_tests.h | 4 +- tests/test_buffers.cpp | 4 +- tests/test_builtin_casters.cpp | 22 +++++++--- tests/test_class.cpp | 12 +++--- tests/test_copy_move.cpp | 14 +++---- tests/test_embed/external_module.cpp | 2 +- tests/test_embed/test_interpreter.cpp | 2 +- tests/test_exceptions.cpp | 4 +- tests/test_factory_constructors.cpp | 49 ++++++++++++++-------- tests/test_local_bindings.cpp | 2 +- tests/test_methods_and_attributes.cpp | 8 ++-- tests/test_modules.cpp | 2 +- tests/test_multiple_inheritance.cpp | 10 ++--- tests/test_numpy_vectorize.cpp | 4 +- tests/test_pickling.cpp | 4 +- tests/test_sequences_and_iterators.cpp | 16 +++---- tests/test_smart_ptr.cpp | 32 +++++++------- tests/test_stl.cpp | 3 +- tests/test_stl_binders.cpp | 2 +- tests/test_tagbased_polymorphic.cpp | 12 +++--- tests/test_virtual_functions.cpp | 2 +- 38 files changed, 231 insertions(+), 132 deletions(-) diff --git a/.clang-tidy b/.clang-tidy index cefffba1ea..a6437dd494 100644 --- a/.clang-tidy +++ b/.clang-tidy @@ -5,6 +5,7 @@ Checks: ' cppcoreguidelines-init-variables, cppcoreguidelines-slicing, clang-analyzer-optin.cplusplus.VirtualCall, +google-explicit-constructor, llvm-namespace-comment, misc-misplaced-const, misc-non-copyable-objects, diff --git a/include/pybind11/attr.h b/include/pybind11/attr.h index ab1fe80446..13f68bbe8f 100644 --- a/include/pybind11/attr.h +++ b/include/pybind11/attr.h @@ -18,7 +18,9 @@ PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE) /// @{ /// Annotation for methods -struct is_method { handle class_; is_method(const handle &c) : class_(c) { } }; +struct is_method { handle class_; + explicit is_method(const handle &c) : class_(c) {} +}; /// Annotation for operators struct is_operator { }; @@ -27,16 +29,24 @@ struct is_operator { }; struct is_final { }; /// Annotation for parent scope -struct scope { handle value; scope(const handle &s) : value(s) { } }; +struct scope { handle value; + explicit scope(const handle &s) : value(s) {} +}; /// Annotation for documentation -struct doc { const char *value; doc(const char *value) : value(value) { } }; +struct doc { const char *value; + explicit doc(const char *value) : value(value) {} +}; /// Annotation for function names -struct name { const char *value; name(const char *value) : value(value) { } }; +struct name { const char *value; + explicit name(const char *value) : value(value) {} +}; /// Annotation indicating that a function is an overload associated with a given "sibling" -struct sibling { handle value; sibling(const handle &value) : value(value.ptr()) { } }; +struct sibling { handle value; + explicit sibling(const handle &value) : value(value.ptr()) {} +}; /// Annotation indicating that a class derives from another given type template struct base { @@ -70,7 +80,9 @@ struct metaclass { }; /// Annotation that marks a class as local to the module: -struct module_local { const bool value; constexpr module_local(bool v = true) : value(v) { } }; +struct module_local { const bool value; + constexpr explicit module_local(bool v = true) : value(v) {} +}; /// Annotation to mark enums as an arithmetic type struct arithmetic { }; diff --git a/include/pybind11/cast.h b/include/pybind11/cast.h index 79bf506d81..db79f57cda 100644 --- a/include/pybind11/cast.h +++ b/include/pybind11/cast.h @@ -82,7 +82,7 @@ template class type_caster> { return caster_t::cast(&src.get(), policy, parent); } template using cast_op_type = std::reference_wrapper; - operator std::reference_wrapper() { return cast_op(subcaster); } + explicit operator std::reference_wrapper() { return cast_op(subcaster); } }; #define PYBIND11_TYPE_CASTER(type, py_name) \ @@ -279,7 +279,7 @@ template <> class type_caster : public type_caster { } template using cast_op_type = void*&; - operator void *&() { return value; } + explicit operator void *&() { return value; } static constexpr auto name = _("capsule"); private: void *value = nullptr; @@ -487,8 +487,10 @@ template struct type_caster(static_cast(str_caster).c_str()); } - operator CharT&() { + explicit operator CharT *() { + return none ? nullptr : const_cast(static_cast(str_caster).c_str()); + } + explicit operator CharT &() { if (none) throw value_error("Cannot convert None to a character"); @@ -581,8 +583,8 @@ template class Tuple, typename... Ts> class tuple_caster template using cast_op_type = type; - operator type() & { return implicit_cast(indices{}); } - operator type() && { return std::move(*this).implicit_cast(indices{}); } + explicit operator type() & { return implicit_cast(indices{}); } + explicit operator type() && { return std::move(*this).implicit_cast(indices{}); } protected: template diff --git a/include/pybind11/detail/common.h b/include/pybind11/detail/common.h index e4f00293ec..a8fd7d917e 100644 --- a/include/pybind11/detail/common.h +++ b/include/pybind11/detail/common.h @@ -918,6 +918,7 @@ class any_container { // Implicit conversion constructor from any arbitrary container type with values convertible to T template ())), T>::value>> + // NOLINTNEXTLINE(google-explicit-constructor) any_container(const Container &c) : any_container(std::begin(c), std::end(c)) { } // initializer_list's aren't deducible, so don't get matched by the above template; we need this @@ -926,9 +927,11 @@ class any_container { any_container(const std::initializer_list &c) : any_container(c.begin(), c.end()) { } // Avoid copying if given an rvalue vector of the correct type. + // NOLINTNEXTLINE(google-explicit-constructor) any_container(std::vector &&v) : v(std::move(v)) { } // Moves the vector out of an rvalue any_container + // NOLINTNEXTLINE(google-explicit-constructor) operator std::vector &&() && { return std::move(v); } // Dereferencing obtains a reference to the underlying vector diff --git a/include/pybind11/detail/descr.h b/include/pybind11/detail/descr.h index 0acfc7db37..0b498e5e72 100644 --- a/include/pybind11/detail/descr.h +++ b/include/pybind11/detail/descr.h @@ -26,12 +26,14 @@ struct descr { char text[N + 1]{'\0'}; constexpr descr() = default; + // NOLINTNEXTLINE(google-explicit-constructor) constexpr descr(char const (&s)[N+1]) : descr(s, make_index_sequence()) { } template constexpr descr(char const (&s)[N+1], index_sequence) : text{s[Is]..., '\0'} { } template + // NOLINTNEXTLINE(google-explicit-constructor) constexpr descr(char c, Chars... cs) : text{c, static_cast(cs)..., '\0'} { } static constexpr std::array types() { diff --git a/include/pybind11/detail/init.h b/include/pybind11/detail/init.h index e795da7d75..cace352964 100644 --- a/include/pybind11/detail/init.h +++ b/include/pybind11/detail/init.h @@ -23,7 +23,7 @@ class type_caster { } template using cast_op_type = value_and_holder &; - operator value_and_holder &() { return *value; } + explicit operator value_and_holder &() { return *value; } static constexpr auto name = _(); private: @@ -222,7 +222,8 @@ template struct factory { remove_reference_t class_factory; - factory(Func &&f) : class_factory(std::forward(f)) { } + // NOLINTNEXTLINE(google-explicit-constructor) + factory(Func &&f) : class_factory(std::forward(f)) {} // The given class either has no alias or has no separate alias factory; // this always constructs the class itself. If the class is registered with an alias diff --git a/include/pybind11/detail/type_caster_base.h b/include/pybind11/detail/type_caster_base.h index 5a5acc2e6a..4c9ac7b8f6 100644 --- a/include/pybind11/detail/type_caster_base.h +++ b/include/pybind11/detail/type_caster_base.h @@ -225,7 +225,7 @@ struct value_and_holder { value_and_holder() = default; // Used for past-the-end iterator - value_and_holder(size_t index) : index{index} {} + explicit value_and_holder(size_t index) : index{index} {} template V *&value_ptr() const { return reinterpret_cast(vh[0]); @@ -274,7 +274,8 @@ struct values_and_holders { const type_vec &tinfo; public: - values_and_holders(instance *inst) : inst{inst}, tinfo(all_type_info(Py_TYPE(inst))) {} + explicit values_and_holders(instance *inst) + : inst{inst}, tinfo(all_type_info(Py_TYPE(inst))) {} struct iterator { private: @@ -290,7 +291,8 @@ struct values_and_holders { 0 /* index */) {} // Past-the-end iterator: - iterator(size_t end) : curr(end) {} + explicit iterator(size_t end) : curr(end) {} + public: bool operator==(const iterator &other) const { return curr.index == other.curr.index; } bool operator!=(const iterator &other) const { return curr.index != other.curr.index; } @@ -491,11 +493,11 @@ inline PyObject *make_new_instance(PyTypeObject *type); class type_caster_generic { public: - PYBIND11_NOINLINE type_caster_generic(const std::type_info &type_info) - : typeinfo(get_type_info(type_info)), cpptype(&type_info) { } + PYBIND11_NOINLINE explicit type_caster_generic(const std::type_info &type_info) + : typeinfo(get_type_info(type_info)), cpptype(&type_info) {} - type_caster_generic(const type_info *typeinfo) - : typeinfo(typeinfo), cpptype(typeinfo ? typeinfo->cpptype : nullptr) { } + explicit type_caster_generic(const type_info *typeinfo) + : typeinfo(typeinfo), cpptype(typeinfo ? typeinfo->cpptype : nullptr) {} bool load(handle src, bool convert) { return load_impl(src, convert); @@ -923,7 +925,9 @@ template class type_caster_base : public type_caster_generic { template using cast_op_type = detail::cast_op_type; + // NOLINTNEXTLINE(google-explicit-constructor) operator itype*() { return (type *) value; } + // NOLINTNEXTLINE(google-explicit-constructor) operator itype&() { if (!value) throw reference_cast_error(); return *((itype *) value); } protected: diff --git a/include/pybind11/eigen.h b/include/pybind11/eigen.h index b03e15f624..c0363827c3 100644 --- a/include/pybind11/eigen.h +++ b/include/pybind11/eigen.h @@ -61,6 +61,7 @@ template struct EigenConformable { EigenDStride stride{0, 0}; // Only valid if negativestrides is false! bool negativestrides = false; // If true, do not use stride! + // NOLINTNEXTLINE(google-explicit-constructor) EigenConformable(bool fits = false) : conformable{fits} {} // Matrix type: EigenConformable(EigenIndex r, EigenIndex c, @@ -88,6 +89,7 @@ template struct EigenConformable { (props::outer_stride == Eigen::Dynamic || props::outer_stride == stride.outer() || (EigenRowMajor ? rows : cols) == 1); } + // NOLINTNEXTLINE(google-explicit-constructor) operator bool() const { return conformable; } }; @@ -326,8 +328,11 @@ struct type_caster::value>> { static constexpr auto name = props::descriptor; + // NOLINTNEXTLINE(google-explicit-constructor) operator Type*() { return &value; } + // NOLINTNEXTLINE(google-explicit-constructor) operator Type&() { return value; } + // NOLINTNEXTLINE(google-explicit-constructor) operator Type&&() && { return std::move(value); } template using cast_op_type = movable_cast_op_type; @@ -451,7 +456,9 @@ struct type_caster< return true; } + // NOLINTNEXTLINE(google-explicit-constructor) operator Type*() { return ref.get(); } + // NOLINTNEXTLINE(google-explicit-constructor) operator Type&() { return *ref; } template using cast_op_type = pybind11::detail::cast_op_type<_T>; diff --git a/include/pybind11/embed.h b/include/pybind11/embed.h index 7b5d7cd24a..9843f0f973 100644 --- a/include/pybind11/embed.h +++ b/include/pybind11/embed.h @@ -260,10 +260,10 @@ inline void finalize_interpreter() { \endrst */ class scoped_interpreter { public: - scoped_interpreter(bool init_signal_handlers = true, - int argc = 0, - const char *const *argv = nullptr, - bool add_program_dir_to_path = true) { + explicit scoped_interpreter(bool init_signal_handlers = true, + int argc = 0, + const char *const *argv = nullptr, + bool add_program_dir_to_path = true) { initialize_interpreter(init_signal_handlers, argc, argv, add_program_dir_to_path); } diff --git a/include/pybind11/functional.h b/include/pybind11/functional.h index bc8a8af821..24141ce388 100644 --- a/include/pybind11/functional.h +++ b/include/pybind11/functional.h @@ -69,6 +69,10 @@ struct type_caster> { // ensure GIL is held during functor destruction struct func_handle { function f; +#if !(defined(_MSC_VER) && _MSC_VER == 1916 && defined(PYBIND11_CPP17) && PY_MAJOR_VERSION < 3) + // This triggers a syntax error under very special conditions (very weird indeed). + explicit +#endif func_handle(function &&f_) noexcept : f(std::move(f_)) {} func_handle(const func_handle &f_) { operator=(f_); } func_handle &operator=(const func_handle &f_) { @@ -85,7 +89,7 @@ struct type_caster> { // to emulate 'move initialization capture' in C++11 struct func_wrapper { func_handle hfunc; - func_wrapper(func_handle &&hf) noexcept : hfunc(std::move(hf)) {} + explicit func_wrapper(func_handle &&hf) noexcept : hfunc(std::move(hf)) {} Return operator()(Args... args) const { gil_scoped_acquire acq; object retval(hfunc.f(std::forward(args)...)); diff --git a/include/pybind11/iostream.h b/include/pybind11/iostream.h index e4d2095857..95449a07ba 100644 --- a/include/pybind11/iostream.h +++ b/include/pybind11/iostream.h @@ -123,7 +123,7 @@ class pythonbuf : public std::streambuf { } public: - pythonbuf(const object &pyostream, size_t buffer_size = 1024) + explicit pythonbuf(const object &pyostream, size_t buffer_size = 1024) : buf_size(buffer_size), d_buffer(new char[buf_size]), pywrite(pyostream.attr("write")), pyflush(pyostream.attr("flush")) { setp(d_buffer.get(), d_buffer.get() + buf_size - 1); @@ -171,8 +171,9 @@ class scoped_ostream_redirect { detail::pythonbuf buffer; public: - scoped_ostream_redirect(std::ostream &costream = std::cout, - const object &pyostream = module_::import("sys").attr("stdout")) + explicit scoped_ostream_redirect(std::ostream &costream = std::cout, + const object &pyostream + = module_::import("sys").attr("stdout")) : costream(costream), buffer(pyostream) { old = costream.rdbuf(&buffer); } @@ -201,8 +202,9 @@ class scoped_ostream_redirect { \endrst */ class scoped_estream_redirect : public scoped_ostream_redirect { public: - scoped_estream_redirect(std::ostream &costream = std::cerr, - const object &pyostream = module_::import("sys").attr("stderr")) + explicit scoped_estream_redirect(std::ostream &costream = std::cerr, + const object &pyostream + = module_::import("sys").attr("stderr")) : scoped_ostream_redirect(costream, pyostream) {} }; @@ -217,7 +219,7 @@ class OstreamRedirect { std::unique_ptr redirect_stderr; public: - OstreamRedirect(bool do_stdout = true, bool do_stderr = true) + explicit OstreamRedirect(bool do_stdout = true, bool do_stderr = true) : do_stdout_(do_stdout), do_stderr_(do_stderr) {} void enter() { diff --git a/include/pybind11/numpy.h b/include/pybind11/numpy.h index fa128efdd0..d55844086d 100644 --- a/include/pybind11/numpy.h +++ b/include/pybind11/numpy.h @@ -477,7 +477,7 @@ class dtype : public object { m_ptr = from_args(pybind11::str(format)).release().ptr(); } - dtype(const char *format) : dtype(std::string(format)) { } + explicit dtype(const char *format) : dtype(std::string(format)) {} dtype(list names, list formats, list offsets, ssize_t itemsize) { dict args; @@ -894,6 +894,7 @@ template class array_t : public if (!is_borrowed) Py_XDECREF(h.ptr()); } + // NOLINTNEXTLINE(google-explicit-constructor) array_t(const object &o) : array(raw_array_t(o.ptr()), stolen_t{}) { if (!m_ptr) throw error_already_set(); } diff --git a/include/pybind11/pybind11.h b/include/pybind11/pybind11.h index 990c213049..82d339751e 100644 --- a/include/pybind11/pybind11.h +++ b/include/pybind11/pybind11.h @@ -81,10 +81,12 @@ PYBIND11_NAMESPACE_END(detail) class cpp_function : public function { public: cpp_function() = default; + // NOLINTNEXTLINE(google-explicit-constructor) cpp_function(std::nullptr_t) { } /// Construct a cpp_function from a vanilla function pointer template + // NOLINTNEXTLINE(google-explicit-constructor) cpp_function(Return (*f)(Args...), const Extra&... extra) { initialize(f, f, extra...); } @@ -92,6 +94,7 @@ class cpp_function : public function { /// Construct a cpp_function from a lambda function (possibly with internal state) template ::value>> + // NOLINTNEXTLINE(google-explicit-constructor) cpp_function(Func &&f, const Extra&... extra) { initialize(std::forward(f), (detail::function_signature_t *) nullptr, extra...); @@ -99,6 +102,7 @@ class cpp_function : public function { /// Construct a cpp_function from a class method (non-const, no ref-qualifier) template + // NOLINTNEXTLINE(google-explicit-constructor) cpp_function(Return (Class::*f)(Arg...), const Extra&... extra) { initialize([f](Class *c, Arg... args) -> Return { return (c->*f)(std::forward(args)...); }, (Return (*) (Class *, Arg...)) nullptr, extra...); @@ -108,6 +112,7 @@ class cpp_function : public function { /// A copy of the overload for non-const functions without explicit ref-qualifier /// but with an added `&`. template + // NOLINTNEXTLINE(google-explicit-constructor) cpp_function(Return (Class::*f)(Arg...)&, const Extra&... extra) { initialize([f](Class *c, Arg... args) -> Return { return (c->*f)(args...); }, (Return (*) (Class *, Arg...)) nullptr, extra...); @@ -115,6 +120,7 @@ class cpp_function : public function { /// Construct a cpp_function from a class method (const, no ref-qualifier) template + // NOLINTNEXTLINE(google-explicit-constructor) cpp_function(Return (Class::*f)(Arg...) const, const Extra&... extra) { initialize([f](const Class *c, Arg... args) -> Return { return (c->*f)(std::forward(args)...); }, (Return (*)(const Class *, Arg ...)) nullptr, extra...); @@ -124,6 +130,7 @@ class cpp_function : public function { /// A copy of the overload for const functions without explicit ref-qualifier /// but with an added `&`. template + // NOLINTNEXTLINE(google-explicit-constructor) cpp_function(Return (Class::*f)(Arg...) const&, const Extra&... extra) { initialize([f](const Class *c, Arg... args) -> Return { return (c->*f)(args...); }, (Return (*)(const Class *, Arg ...)) nullptr, extra...); @@ -2034,7 +2041,7 @@ template void implicitly_convertible() { struct set_flag { bool &flag; - set_flag(bool &flag_) : flag(flag_) { flag_ = true; } + explicit set_flag(bool &flag_) : flag(flag_) { flag_ = true; } ~set_flag() { flag = false; } }; auto implicit_caster = [](PyObject *obj, PyTypeObject *type) -> PyObject * { diff --git a/include/pybind11/pytypes.h b/include/pybind11/pytypes.h index fa50eb1e3a..971db85f37 100644 --- a/include/pybind11/pytypes.h +++ b/include/pybind11/pytypes.h @@ -178,6 +178,7 @@ class handle : public detail::object_api { /// The default constructor creates a handle with a ``nullptr``-valued pointer handle() = default; /// Creates a ``handle`` from the given raw Python object pointer + // NOLINTNEXTLINE(google-explicit-constructor) handle(PyObject *ptr) : m_ptr(ptr) { } // Allow implicit conversion from PyObject* /// Return the underlying ``PyObject *`` pointer @@ -612,6 +613,7 @@ class accessor : public object_api> { return obj.contains(key); } + // NOLINTNEXTLINE(google-explicit-constructor) operator object() const { return get_cache(); } PyObject *ptr() const { return get_cache().ptr(); } template T cast() const { return get_cache().template cast(); } @@ -761,6 +763,7 @@ template struct arrow_proxy { T value; + // NOLINTNEXTLINE(google-explicit-constructor) arrow_proxy(T &&value) noexcept : value(std::move(value)) { } T *operator->() const { return &value; } }; @@ -909,14 +912,17 @@ PYBIND11_NAMESPACE_END(detail) bool check() const { return m_ptr != nullptr && (CheckFun(m_ptr) != 0); } \ static bool check_(handle h) { return h.ptr() != nullptr && CheckFun(h.ptr()); } \ template \ + /* NOLINTNEXTLINE(google-explicit-constructor) */ \ Name(const ::pybind11::detail::accessor &a) : Name(object(a)) { } #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: */ \ + /* NOLINTNEXTLINE(google-explicit-constructor) */ \ Name(const object &o) \ : Parent(check_(o) ? o.inc_ref().ptr() : ConvertFun(o.ptr()), stolen_t{}) \ { if (!m_ptr) throw 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(); } @@ -933,8 +939,10 @@ PYBIND11_NAMESPACE_END(detail) #define PYBIND11_OBJECT(Name, Parent, CheckFun) \ PYBIND11_OBJECT_COMMON(Name, Parent, CheckFun) \ /* This is deliberately not 'explicit' to allow implicit conversion from object: */ \ + /* NOLINTNEXTLINE(google-explicit-constructor) */ \ Name(const object &o) : Parent(o) \ { if (m_ptr && !check_(m_ptr)) throw PYBIND11_OBJECT_CHECK_FAILED(Name, m_ptr); } \ + /* NOLINTNEXTLINE(google-explicit-constructor) */ \ Name(object &&o) : Parent(std::move(o)) \ { if (m_ptr && !check_(m_ptr)) throw PYBIND11_OBJECT_CHECK_FAILED(Name, m_ptr); } @@ -1056,11 +1064,13 @@ class str : public object { } // 'explicit' is explicitly omitted from the following constructors to allow implicit conversion to py::str from C++ string-like objects + // NOLINTNEXTLINE(google-explicit-constructor) str(const char *c = "") : object(PyUnicode_FromString(c), stolen_t{}) { if (!m_ptr) pybind11_fail("Could not allocate string object!"); } + // NOLINTNEXTLINE(google-explicit-constructor) str(const std::string &s) : str(s.data(), s.size()) { } explicit str(const bytes &b); @@ -1071,6 +1081,7 @@ class str : public object { \endrst */ explicit str(handle h) : object(raw_str(h.ptr()), stolen_t{}) { if (!m_ptr) throw error_already_set(); } + // NOLINTNEXTLINE(google-explicit-constructor) operator std::string() const { object temp = *this; if (PyUnicode_Check(m_ptr)) { @@ -1118,6 +1129,7 @@ class bytes : public object { PYBIND11_OBJECT(bytes, object, PYBIND11_BYTES_CHECK) // Allow implicit conversion: + // NOLINTNEXTLINE(google-explicit-constructor) bytes(const char *c = "") : object(PYBIND11_BYTES_FROM_STRING(c), stolen_t{}) { if (!m_ptr) pybind11_fail("Could not allocate bytes object!"); @@ -1130,10 +1142,12 @@ class bytes : public object { } // Allow implicit conversion: + // NOLINTNEXTLINE(google-explicit-constructor) bytes(const std::string &s) : bytes(s.data(), s.size()) { } explicit bytes(const pybind11::str &s); + // NOLINTNEXTLINE(google-explicit-constructor) operator std::string() const { char *buffer = nullptr; ssize_t length = 0; @@ -1222,7 +1236,9 @@ class bool_ : public object { PYBIND11_OBJECT_CVT(bool_, object, PyBool_Check, raw_bool) bool_() : object(Py_False, borrowed_t{}) { } // Allow implicit conversion from and to `bool`: + // NOLINTNEXTLINE(google-explicit-constructor) bool_(bool value) : object(value ? Py_True : Py_False, borrowed_t{}) { } + // NOLINTNEXTLINE(google-explicit-constructor) operator bool() const { return (m_ptr != nullptr) && PyLong_AsLong(m_ptr) != 0; } private: @@ -1261,6 +1277,7 @@ class int_ : public object { // Allow implicit conversion from C++ integral types: template ::value, int> = 0> + // NOLINTNEXTLINE(google-explicit-constructor) int_(T value) { if (PYBIND11_SILENCE_MSVC_C4127(sizeof(T) <= sizeof(long))) { if (std::is_signed::value) @@ -1278,6 +1295,7 @@ class int_ : public object { template ::value, int> = 0> + // NOLINTNEXTLINE(google-explicit-constructor) operator T() const { return std::is_unsigned::value ? detail::as_unsigned(m_ptr) @@ -1291,13 +1309,17 @@ class float_ : public object { public: PYBIND11_OBJECT_CVT(float_, object, PyFloat_Check, PyNumber_Float) // Allow implicit conversion from float/double: + // NOLINTNEXTLINE(google-explicit-constructor) float_(float value) : object(PyFloat_FromDouble((double) value), stolen_t{}) { if (!m_ptr) pybind11_fail("Could not allocate float object!"); } + // NOLINTNEXTLINE(google-explicit-constructor) float_(double value = .0) : object(PyFloat_FromDouble((double) value), stolen_t{}) { if (!m_ptr) pybind11_fail("Could not allocate float object!"); } + // NOLINTNEXTLINE(google-explicit-constructor) operator float() const { return (float) PyFloat_AsDouble(m_ptr); } + // NOLINTNEXTLINE(google-explicit-constructor) operator double() const { return (double) PyFloat_AsDouble(m_ptr); } }; @@ -1372,7 +1394,7 @@ class capsule : public object { pybind11_fail("Could not set capsule context!"); } - capsule(void (*destructor)()) { + explicit capsule(void (*destructor)()) { m_ptr = PyCapsule_New(reinterpret_cast(destructor), nullptr, [](PyObject *o) { auto destructor = reinterpret_cast(PyCapsule_GetPointer(o, nullptr)); destructor(); @@ -1382,6 +1404,7 @@ class capsule : public object { pybind11_fail("Could not allocate capsule object!"); } + // NOLINTNEXTLINE(google-explicit-constructor) template operator T *() const { return get_pointer(); } diff --git a/tests/local_bindings.h b/tests/local_bindings.h index f11c08e616..4c936c19a5 100644 --- a/tests/local_bindings.h +++ b/tests/local_bindings.h @@ -6,7 +6,7 @@ /// Simple class used to test py::local: template class LocalBase { public: - LocalBase(int i) : i(i) { } + explicit LocalBase(int i) : i(i) { } int i = -1; }; @@ -75,11 +75,11 @@ py::class_ bind_local(Args && ...args) { namespace pets { class Pet { public: - Pet(std::string name) : name_(std::move(name)) {} + explicit Pet(std::string name) : name_(std::move(name)) {} std::string name_; const std::string &name() const { return name_; } }; } // namespace pets -struct MixGL { int i; MixGL(int i) : i{i} {} }; -struct MixGL2 { int i; MixGL2(int i) : i{i} {} }; +struct MixGL { int i; explicit MixGL(int i) : i{i} {} }; +struct MixGL2 { int i; explicit MixGL2(int i) : i{i} {} }; diff --git a/tests/object.h b/tests/object.h index 6851fc6247..be21bf6316 100644 --- a/tests/object.h +++ b/tests/object.h @@ -65,7 +65,7 @@ template class ref { ref() : m_ptr(nullptr) { print_default_created(this); track_default_created((ref_tag*) this); } /// Construct a reference from a pointer - ref(T *ptr) : m_ptr(ptr) { + explicit ref(T *ptr) : m_ptr(ptr) { if (m_ptr) ((Object *) m_ptr)->incRef(); print_created(this, "from pointer", m_ptr); track_created((ref_tag*) this, "from pointer"); @@ -165,7 +165,7 @@ template class ref { const T& operator*() const { return *m_ptr; } /// Return a pointer to the referenced object - operator T* () { return m_ptr; } + explicit operator T* () { return m_ptr; } /// Return a const pointer to the referenced object T* get_ptr() { return m_ptr; } diff --git a/tests/pybind11_cross_module_tests.cpp b/tests/pybind11_cross_module_tests.cpp index 4bfd5302d5..5838cb2746 100644 --- a/tests/pybind11_cross_module_tests.cpp +++ b/tests/pybind11_cross_module_tests.cpp @@ -123,7 +123,7 @@ PYBIND11_MODULE(pybind11_cross_module_tests, m) { class Dog : public pets::Pet { public: - Dog(std::string name) : Pet(std::move(name)) {} + explicit Dog(std::string name) : Pet(std::move(name)) {} }; py::class_(m, "Pet", py::module_local()) .def("name", &pets::Pet::name); diff --git a/tests/pybind11_tests.h b/tests/pybind11_tests.h index 8da0a670f2..800ddda48b 100644 --- a/tests/pybind11_tests.h +++ b/tests/pybind11_tests.h @@ -16,7 +16,7 @@ class test_initializer { using Initializer = void (*)(py::module_ &); public: - test_initializer(Initializer init); + explicit test_initializer(Initializer init); test_initializer(const char *submodule_name, Initializer init); }; @@ -32,7 +32,7 @@ struct UnregisteredType { }; class UserType { public: UserType() = default; - UserType(int i) : i(i) { } + explicit UserType(int i) : i(i) { } int value() const { return i; } void set(int set) { i = set; } diff --git a/tests/test_buffers.cpp b/tests/test_buffers.cpp index c7e2c7df30..3a8e3e7b75 100644 --- a/tests/test_buffers.cpp +++ b/tests/test_buffers.cpp @@ -122,7 +122,7 @@ TEST_SUBMODULE(buffers, m) { // test_inherited_protocol class SquareMatrix : public Matrix { public: - SquareMatrix(py::ssize_t n) : Matrix(n, n) { } + explicit SquareMatrix(py::ssize_t n) : Matrix(n, n) {} }; // Derived classes inherit the buffer protocol and the buffer access function py::class_(m, "SquareMatrix") @@ -173,7 +173,7 @@ TEST_SUBMODULE(buffers, m) { struct BufferReadOnly { const uint8_t value = 0; - BufferReadOnly(uint8_t value): value(value) {} + explicit BufferReadOnly(uint8_t value) : value(value) {} py::buffer_info get_buffer_info() { return py::buffer_info(&value, 1); diff --git a/tests/test_builtin_casters.cpp b/tests/test_builtin_casters.cpp index be4431aad6..71c778e8e2 100644 --- a/tests/test_builtin_casters.cpp +++ b/tests/test_builtin_casters.cpp @@ -25,16 +25,28 @@ class type_caster { // cast operator. bool load(handle, bool) { return true; } - operator ConstRefCasted &&() { + explicit operator ConstRefCasted &&() { value = {1}; // NOLINTNEXTLINE(performance-move-const-arg) return std::move(value); } - operator ConstRefCasted&() { value = {2}; return value; } - operator ConstRefCasted*() { value = {3}; return &value; } + explicit operator ConstRefCasted &() { + value = {2}; + return value; + } + explicit operator ConstRefCasted *() { + value = {3}; + return &value; + } - operator const ConstRefCasted&() { value = {4}; return value; } - operator const ConstRefCasted*() { value = {5}; return &value; } + explicit operator const ConstRefCasted &() { + value = {4}; + return value; + } + explicit operator const ConstRefCasted *() { + value = {5}; + return &value; + } // custom cast_op to explicitly propagate types to the conversion operators. template diff --git a/tests/test_class.cpp b/tests/test_class.cpp index dff758392d..246b6c1fea 100644 --- a/tests/test_class.cpp +++ b/tests/test_class.cpp @@ -27,7 +27,7 @@ // test_brace_initialization struct NoBraceInitialization { - NoBraceInitialization(std::vector v) : vec{std::move(v)} {} + explicit NoBraceInitialization(std::vector v) : vec{std::move(v)} {} template NoBraceInitialization(std::initializer_list l) : vec(l) {} @@ -65,18 +65,18 @@ TEST_SUBMODULE(class_, m) { class Dog : public Pet { public: - Dog(const std::string &name) : Pet(name, "dog") {} + explicit Dog(const std::string &name) : Pet(name, "dog") {} std::string bark() const { return "Woof!"; } }; class Rabbit : public Pet { public: - Rabbit(const std::string &name) : Pet(name, "parrot") {} + explicit Rabbit(const std::string &name) : Pet(name, "parrot") {} }; class Hamster : public Pet { public: - Hamster(const std::string &name) : Pet(name, "rodent") {} + explicit Hamster(const std::string &name) : Pet(name, "rodent") {} }; class Chimera : public Pet { @@ -208,7 +208,7 @@ TEST_SUBMODULE(class_, m) { struct ConvertibleFromUserType { int i; - ConvertibleFromUserType(UserType u) : i(u.value()) { } + explicit ConvertibleFromUserType(UserType u) : i(u.value()) {} }; py::class_(m, "AcceptsUserType") @@ -263,7 +263,7 @@ TEST_SUBMODULE(class_, m) { }; struct PyAliasedHasOpNewDelSize : AliasedHasOpNewDelSize { PyAliasedHasOpNewDelSize() = default; - PyAliasedHasOpNewDelSize(int) { } + explicit PyAliasedHasOpNewDelSize(int) {} std::uint64_t j; }; struct HasOpNewDelBoth { diff --git a/tests/test_copy_move.cpp b/tests/test_copy_move.cpp index 859da9df7e..5fb0dd810c 100644 --- a/tests/test_copy_move.cpp +++ b/tests/test_copy_move.cpp @@ -37,7 +37,7 @@ template <> lacking_move_ctor empty::instance_ = {}; class MoveOnlyInt { public: MoveOnlyInt() { print_default_created(this); } - MoveOnlyInt(int v) : value{v} { print_created(this, value); } + explicit MoveOnlyInt(int v) : value{v} { print_created(this, value); } MoveOnlyInt(MoveOnlyInt &&m) noexcept { print_move_created(this, m.value); std::swap(value, m.value); @@ -56,7 +56,7 @@ class MoveOnlyInt { class MoveOrCopyInt { public: MoveOrCopyInt() { print_default_created(this); } - MoveOrCopyInt(int v) : value{v} { print_created(this, value); } + explicit MoveOrCopyInt(int v) : value{v} { print_created(this, value); } MoveOrCopyInt(MoveOrCopyInt &&m) noexcept { print_move_created(this, m.value); std::swap(value, m.value); @@ -75,7 +75,7 @@ class MoveOrCopyInt { class CopyOnlyInt { public: CopyOnlyInt() { print_default_created(this); } - CopyOnlyInt(int v) : value{v} { print_created(this, value); } + explicit CopyOnlyInt(int v) : value{v} { print_created(this, value); } CopyOnlyInt(const CopyOnlyInt &c) { print_copy_created(this, c.value); value = c.value; } CopyOnlyInt &operator=(const CopyOnlyInt &c) { print_copy_assigned(this, c.value); value = c.value; return *this; } ~CopyOnlyInt() { print_destroyed(this); } @@ -107,8 +107,8 @@ template <> struct type_caster { if (!src) return none().release(); return cast(*src, policy, parent); } - operator CopyOnlyInt*() { return &value; } - operator CopyOnlyInt&() { return value; } + explicit operator CopyOnlyInt *() { return &value; } + explicit operator CopyOnlyInt &() { return value; } template using cast_op_type = pybind11::detail::cast_op_type; }; PYBIND11_NAMESPACE_END(detail) @@ -219,7 +219,7 @@ TEST_SUBMODULE(copy_move_policies, m) { // #389: rvp::move should fall-through to copy on non-movable objects struct MoveIssue1 { int v; - MoveIssue1(int v) : v{v} {} + explicit MoveIssue1(int v) : v{v} {} MoveIssue1(const MoveIssue1 &c) = default; MoveIssue1(MoveIssue1 &&) = delete; }; @@ -227,7 +227,7 @@ TEST_SUBMODULE(copy_move_policies, m) { struct MoveIssue2 { int v; - MoveIssue2(int v) : v{v} {} + explicit MoveIssue2(int v) : v{v} {} MoveIssue2(MoveIssue2 &&) = default; }; py::class_(m, "MoveIssue2").def(py::init()).def_readwrite("value", &MoveIssue2::v); diff --git a/tests/test_embed/external_module.cpp b/tests/test_embed/external_module.cpp index e9a6058b17..4909522993 100644 --- a/tests/test_embed/external_module.cpp +++ b/tests/test_embed/external_module.cpp @@ -9,7 +9,7 @@ namespace py = pybind11; PYBIND11_MODULE(external_module, m) { class A { public: - A(int value) : v{value} {}; + explicit A(int value) : v{value} {}; int v; }; diff --git a/tests/test_embed/test_interpreter.cpp b/tests/test_embed/test_interpreter.cpp index 78b64be6b0..fae14f7510 100644 --- a/tests/test_embed/test_interpreter.cpp +++ b/tests/test_embed/test_interpreter.cpp @@ -18,7 +18,7 @@ using namespace py::literals; class Widget { public: - Widget(std::string message) : message(std::move(message)) {} + explicit Widget(std::string message) : message(std::move(message)) {} virtual ~Widget() = default; std::string the_message() const { return message; } diff --git a/tests/test_exceptions.cpp b/tests/test_exceptions.cpp index 4805a02490..25adb32ed1 100644 --- a/tests/test_exceptions.cpp +++ b/tests/test_exceptions.cpp @@ -81,7 +81,7 @@ class MyException6 : public std::exception { struct PythonCallInDestructor { - PythonCallInDestructor(const py::dict &d) : d(d) {} + explicit PythonCallInDestructor(const py::dict &d) : d(d) {} ~PythonCallInDestructor() { d["good"] = true; } py::dict d; @@ -90,7 +90,7 @@ struct PythonCallInDestructor { struct PythonAlreadySetInDestructor { - PythonAlreadySetInDestructor(const py::str &s) : s(s) {} + explicit PythonAlreadySetInDestructor(const py::str &s) : s(s) {} ~PythonAlreadySetInDestructor() { py::dict foo; try { diff --git a/tests/test_factory_constructors.cpp b/tests/test_factory_constructors.cpp index 235ec4dca6..660e2896af 100644 --- a/tests/test_factory_constructors.cpp +++ b/tests/test_factory_constructors.cpp @@ -19,8 +19,9 @@ class TestFactory1 { friend class TestFactoryHelper; TestFactory1() : value("(empty)") { print_default_created(this); } - TestFactory1(int v) : value(std::to_string(v)) { print_created(this, value); } - TestFactory1(std::string v) : value(std::move(v)) { print_created(this, value); } + explicit TestFactory1(int v) : value(std::to_string(v)) { print_created(this, value); } + explicit TestFactory1(std::string v) : value(std::move(v)) { print_created(this, value); } + public: std::string value; TestFactory1(TestFactory1 &&) = delete; @@ -33,8 +34,9 @@ class TestFactory1 { class TestFactory2 { friend class TestFactoryHelper; TestFactory2() : value("(empty2)") { print_default_created(this); } - TestFactory2(int v) : value(std::to_string(v)) { print_created(this, value); } - TestFactory2(std::string v) : value(std::move(v)) { print_created(this, value); } + explicit TestFactory2(int v) : value(std::to_string(v)) { print_created(this, value); } + explicit TestFactory2(std::string v) : value(std::move(v)) { print_created(this, value); } + public: TestFactory2(TestFactory2 &&m) noexcept { value = std::move(m.value); @@ -53,9 +55,10 @@ class TestFactory3 { protected: friend class TestFactoryHelper; TestFactory3() : value("(empty3)") { print_default_created(this); } - TestFactory3(int v) : value(std::to_string(v)) { print_created(this, value); } + explicit TestFactory3(int v) : value(std::to_string(v)) { print_created(this, value); } + public: - TestFactory3(std::string v) : value(std::move(v)) { print_created(this, value); } + explicit TestFactory3(std::string v) : value(std::move(v)) { print_created(this, value); } TestFactory3(TestFactory3 &&m) noexcept { value = std::move(m.value); print_move_created(this); @@ -72,13 +75,13 @@ class TestFactory3 { class TestFactory4 : public TestFactory3 { public: TestFactory4() : TestFactory3() { print_default_created(this); } - TestFactory4(int v) : TestFactory3(v) { print_created(this, v); } + explicit TestFactory4(int v) : TestFactory3(v) { print_created(this, v); } ~TestFactory4() override { print_destroyed(this); } }; // Another class for an invalid downcast test class TestFactory5 : public TestFactory3 { public: - TestFactory5(int i) : TestFactory3(i) { print_created(this, i); } + explicit TestFactory5(int i) : TestFactory3(i) { print_created(this, i); } ~TestFactory5() override { print_destroyed(this); } }; @@ -87,7 +90,7 @@ class TestFactory6 { int value; bool alias = false; public: - TestFactory6(int i) : value{i} { print_created(this, i); } + explicit TestFactory6(int i) : value{i} { print_created(this, i); } TestFactory6(TestFactory6 &&f) noexcept { print_move_created(this); value = f.value; @@ -102,11 +105,20 @@ class PyTF6 : public TestFactory6 { public: // Special constructor that allows the factory to construct a PyTF6 from a TestFactory6 only // when an alias is needed: - PyTF6(TestFactory6 &&base) : TestFactory6(std::move(base)) { alias = true; print_created(this, "move", value); } - PyTF6(int i) : TestFactory6(i) { alias = true; print_created(this, i); } + explicit PyTF6(TestFactory6 &&base) : TestFactory6(std::move(base)) { + alias = true; + print_created(this, "move", value); + } + explicit PyTF6(int i) : TestFactory6(i) { + alias = true; + print_created(this, i); + } PyTF6(PyTF6 &&f) noexcept : TestFactory6(std::move(f)) { print_move_created(this); } PyTF6(const PyTF6 &f) : TestFactory6(f) { print_copy_created(this); } - PyTF6(std::string s) : TestFactory6((int) s.size()) { alias = true; print_created(this, s); } + explicit PyTF6(std::string s) : TestFactory6((int) s.size()) { + alias = true; + print_created(this, s); + } ~PyTF6() override { print_destroyed(this); } int get() override { PYBIND11_OVERRIDE(int, TestFactory6, get, /*no args*/); } }; @@ -116,7 +128,7 @@ class TestFactory7 { int value; bool alias = false; public: - TestFactory7(int i) : value{i} { print_created(this, i); } + explicit TestFactory7(int i) : value{i} { print_created(this, i); } TestFactory7(TestFactory7 &&f) noexcept { print_move_created(this); value = f.value; @@ -129,7 +141,10 @@ class TestFactory7 { }; class PyTF7 : public TestFactory7 { public: - PyTF7(int i) : TestFactory7(i) { alias = true; print_created(this, i); } + explicit PyTF7(int i) : TestFactory7(i) { + alias = true; + print_created(this, i); + } PyTF7(PyTF7 &&f) noexcept : TestFactory7(std::move(f)) { print_move_created(this); } PyTF7(const PyTF7 &f) : TestFactory7(f) { print_copy_created(this); } ~PyTF7() override { print_destroyed(this); } @@ -300,7 +315,7 @@ TEST_SUBMODULE(factory_constructors, m) { // Class with a custom new operator but *without* a placement new operator (issue #948) class NoPlacementNew { public: - NoPlacementNew(int i) : i(i) { } + explicit NoPlacementNew(int i) : i(i) {} static void *operator new(std::size_t s) { auto *p = ::operator new(s); py::print("operator new called, returning", reinterpret_cast(p)); @@ -324,8 +339,8 @@ TEST_SUBMODULE(factory_constructors, m) { // Class that has verbose operator_new/operator_delete calls struct NoisyAlloc { NoisyAlloc(const NoisyAlloc &) = default; - NoisyAlloc(int i) { py::print(py::str("NoisyAlloc(int {})").format(i)); } - NoisyAlloc(double d) { py::print(py::str("NoisyAlloc(double {})").format(d)); } + explicit NoisyAlloc(int i) { py::print(py::str("NoisyAlloc(int {})").format(i)); } + explicit NoisyAlloc(double d) { py::print(py::str("NoisyAlloc(double {})").format(d)); } ~NoisyAlloc() { py::print("~NoisyAlloc()"); } static void *operator new(size_t s) { py::print("noisy new"); return ::operator new(s); } diff --git a/tests/test_local_bindings.cpp b/tests/test_local_bindings.cpp index 8d6e33f790..a5808e2f2a 100644 --- a/tests/test_local_bindings.cpp +++ b/tests/test_local_bindings.cpp @@ -91,7 +91,7 @@ TEST_SUBMODULE(local_bindings, m) { class Cat : public pets::Pet { public: - Cat(std::string name) : Pet(std::move(name)) {} + explicit Cat(std::string name) : Pet(std::move(name)) {} }; py::class_(m, "Pet", py::module_local()) .def("get_name", &pets::Pet::name); diff --git a/tests/test_methods_and_attributes.cpp b/tests/test_methods_and_attributes.cpp index 4cf6f08b85..2d303a44e3 100644 --- a/tests/test_methods_and_attributes.cpp +++ b/tests/test_methods_and_attributes.cpp @@ -19,9 +19,9 @@ using overload_cast_ = pybind11::detail::overload_cast_impl; class ExampleMandA { public: ExampleMandA() { print_default_created(this); } - ExampleMandA(int value) : value(value) { print_created(this, value); } + explicit ExampleMandA(int value) : value(value) { print_created(this, value); } ExampleMandA(const ExampleMandA &e) : value(e.value) { print_copy_created(this); } - ExampleMandA(std::string&&) {} + explicit ExampleMandA(std::string &&) {} ExampleMandA(ExampleMandA &&e) noexcept : value(e.value) { print_move_created(this); } ~ExampleMandA() { print_destroyed(this); } @@ -124,14 +124,14 @@ class NoneCastTester { public: int answer = -1; NoneCastTester() = default; - NoneCastTester(int v) : answer(v) {} + explicit NoneCastTester(int v) : answer(v) {} }; struct StrIssue { int val = -1; StrIssue() = default; - StrIssue(int i) : val{i} {} + explicit StrIssue(int i) : val{i} {} }; // Issues #854, #910: incompatible function args when member function/pointer is in unregistered base class diff --git a/tests/test_modules.cpp b/tests/test_modules.cpp index 5867135404..ce61c1a25c 100644 --- a/tests/test_modules.cpp +++ b/tests/test_modules.cpp @@ -20,7 +20,7 @@ TEST_SUBMODULE(modules, m) { // test_reference_internal class A { public: - A(int v) : v(v) { print_created(this, v); } + explicit A(int v) : v(v) { print_created(this, v); } ~A() { print_destroyed(this); } A(const A&) { print_copy_created(this); } A& operator=(const A ©) { print_copy_assigned(this); v = copy.v; return *this; } diff --git a/tests/test_multiple_inheritance.cpp b/tests/test_multiple_inheritance.cpp index b5ca298d91..6963197a5c 100644 --- a/tests/test_multiple_inheritance.cpp +++ b/tests/test_multiple_inheritance.cpp @@ -16,7 +16,7 @@ namespace { // Many bases for testing that multiple inheritance from many classes (i.e. requiring extra // space for holder constructed flags) works. template struct BaseN { - BaseN(int i) : i(i) { } + explicit BaseN(int i) : i(i) {} int i; }; @@ -47,12 +47,12 @@ int VanillaStaticMix2::static_value = 12; // test_multiple_inheritance_virtbase struct Base1a { - Base1a(int i) : i(i) { } + explicit Base1a(int i) : i(i) {} int foo() const { return i; } int i; }; struct Base2a { - Base2a(int i) : i(i) { } + explicit Base2a(int i) : i(i) {} int bar() const { return i; } int i; }; @@ -77,7 +77,7 @@ TEST_SUBMODULE(multiple_inheritance, m) { // test_multiple_inheritance_mix1 // test_multiple_inheritance_mix2 struct Base1 { - Base1(int i) : i(i) { } + explicit Base1(int i) : i(i) {} int foo() const { return i; } int i; }; @@ -86,7 +86,7 @@ TEST_SUBMODULE(multiple_inheritance, m) { .def("foo", &Base1::foo); struct Base2 { - Base2(int i) : i(i) { } + explicit Base2(int i) : i(i) {} int bar() const { return i; } int i; }; diff --git a/tests/test_numpy_vectorize.cpp b/tests/test_numpy_vectorize.cpp index b08a9f7edd..eb5281fb1d 100644 --- a/tests/test_numpy_vectorize.cpp +++ b/tests/test_numpy_vectorize.cpp @@ -52,7 +52,7 @@ TEST_SUBMODULE(numpy_vectorize, m) { // Passthrough test: references and non-pod types should be automatically passed through (in the // function definition below, only `b`, `d`, and `g` are vectorized): struct NonPODClass { - NonPODClass(int v) : value{v} {} + explicit NonPODClass(int v) : value{v} {} int value; }; py::class_(m, "NonPODClass") @@ -71,7 +71,7 @@ TEST_SUBMODULE(numpy_vectorize, m) { // test_method_vectorization struct VectorizeTestClass { - VectorizeTestClass(int v) : value{v} {}; + explicit VectorizeTestClass(int v) : value{v} {}; float method(int x, float y) const { return y + (float) (x + value); } int value = 0; }; diff --git a/tests/test_pickling.cpp b/tests/test_pickling.cpp index 0d5827315f..b77636dd1a 100644 --- a/tests/test_pickling.cpp +++ b/tests/test_pickling.cpp @@ -67,7 +67,7 @@ TEST_SUBMODULE(pickling, m) { // test_roundtrip class Pickleable { public: - Pickleable(const std::string &value) : m_value(value) { } + explicit Pickleable(const std::string &value) : m_value(value) { } const std::string &value() const { return m_value; } void setExtra1(int extra1) { m_extra1 = extra1; } @@ -132,7 +132,7 @@ TEST_SUBMODULE(pickling, m) { // test_roundtrip_with_dict class PickleableWithDict { public: - PickleableWithDict(const std::string &value) : value(value) { } + explicit PickleableWithDict(const std::string &value) : value(value) { } std::string value; int extra; diff --git a/tests/test_sequences_and_iterators.cpp b/tests/test_sequences_and_iterators.cpp index d49fb1f459..b07fd197a9 100644 --- a/tests/test_sequences_and_iterators.cpp +++ b/tests/test_sequences_and_iterators.cpp @@ -20,7 +20,7 @@ template class NonZeroIterator { const T* ptr_; public: - NonZeroIterator(const T* ptr) : ptr_(ptr) {} + explicit NonZeroIterator(const T *ptr) : ptr_(ptr) {} const T& operator*() const { return *ptr_; } NonZeroIterator& operator++() { ++ptr_; return *this; } }; @@ -77,9 +77,9 @@ TEST_SUBMODULE(sequences_and_iterators, m) { // test_sliceable class Sliceable{ public: - Sliceable(int n): size(n) {} - int start,stop,step; - int size; + explicit Sliceable(int n) : size(n) {} + int start, stop, step; + int size; }; py::class_(m, "Sliceable") .def(py::init()) @@ -96,12 +96,12 @@ TEST_SUBMODULE(sequences_and_iterators, m) { // test_sequence class Sequence { public: - Sequence(size_t size) : m_size(size) { + explicit Sequence(size_t size) : m_size(size) { print_created(this, "of size", m_size); m_data = new float[size]; memset(m_data, 0, sizeof(float) * size); } - Sequence(const std::vector &value) : m_size(value.size()) { + explicit Sequence(const std::vector &value) : m_size(value.size()) { print_created(this, "of size", m_size, "from std::vector"); m_data = new float[m_size]; memcpy(m_data, &value[0], sizeof(float) * m_size); @@ -239,7 +239,7 @@ TEST_SUBMODULE(sequences_and_iterators, m) { class StringMap { public: StringMap() = default; - StringMap(std::unordered_map init) + explicit StringMap(std::unordered_map init) : map(std::move(init)) {} void set(const std::string &key, std::string val) { map[key] = std::move(val); } @@ -276,7 +276,7 @@ TEST_SUBMODULE(sequences_and_iterators, m) { // test_generalized_iterators class IntPairs { public: - IntPairs(std::vector> data) : data_(std::move(data)) {} + explicit IntPairs(std::vector> data) : data_(std::move(data)) {} const std::pair* begin() const { return data_.data(); } private: std::vector> data_; diff --git a/tests/test_smart_ptr.cpp b/tests/test_smart_ptr.cpp index eeaa44147a..94f04330a2 100644 --- a/tests/test_smart_ptr.cpp +++ b/tests/test_smart_ptr.cpp @@ -24,7 +24,7 @@ template class huge_unique_ptr { std::unique_ptr ptr; uint64_t padding[10]; public: - huge_unique_ptr(T *p) : ptr(p) {} + explicit huge_unique_ptr(T *p) : ptr(p) {} T *get() { return ptr.get(); } }; @@ -33,7 +33,7 @@ template class custom_unique_ptr { std::unique_ptr impl; public: - custom_unique_ptr(T* p) : impl(p) { } + explicit custom_unique_ptr(T *p) : impl(p) {} T* get() const { return impl.get(); } T* release_ptr() { return impl.release(); } }; @@ -46,7 +46,7 @@ class shared_ptr_with_addressof_operator { std::shared_ptr impl; public: shared_ptr_with_addressof_operator( ) = default; - shared_ptr_with_addressof_operator(T* p) : impl(p) { } + explicit shared_ptr_with_addressof_operator(T *p) : impl(p) {} T* get() const { return impl.get(); } T** operator&() { throw std::logic_error("Call of overloaded operator& is not expected"); } }; @@ -59,7 +59,7 @@ class unique_ptr_with_addressof_operator { std::unique_ptr impl; public: unique_ptr_with_addressof_operator() = default; - unique_ptr_with_addressof_operator(T* p) : impl(p) { } + explicit unique_ptr_with_addressof_operator(T *p) : impl(p) {} T* get() const { return impl.get(); } T* release_ptr() { return impl.release(); } T** operator&() { throw std::logic_error("Call of overloaded operator& is not expected"); } @@ -68,7 +68,7 @@ class unique_ptr_with_addressof_operator { // Custom object with builtin reference counting (see 'object.h' for the implementation) class MyObject1 : public Object { public: - MyObject1(int value) : value(value) { print_created(this, toString()); } + explicit MyObject1(int value) : value(value) { print_created(this, toString()); } std::string toString() const override { return "MyObject1[" + std::to_string(value) + "]"; } protected: ~MyObject1() override { print_destroyed(this); } @@ -80,7 +80,7 @@ class MyObject1 : public Object { class MyObject2 { public: MyObject2(const MyObject2 &) = default; - MyObject2(int value) : value(value) { print_created(this, toString()); } + explicit MyObject2(int value) : value(value) { print_created(this, toString()); } std::string toString() const { return "MyObject2[" + std::to_string(value) + "]"; } virtual ~MyObject2() { print_destroyed(this); } private: @@ -91,7 +91,7 @@ class MyObject2 { class MyObject3 : public std::enable_shared_from_this { public: MyObject3(const MyObject3 &) = default; - MyObject3(int value) : value(value) { print_created(this, toString()); } + explicit MyObject3(int value) : value(value) { print_created(this, toString()); } std::string toString() const { return "MyObject3[" + std::to_string(value) + "]"; } virtual ~MyObject3() { print_destroyed(this); } private: @@ -104,7 +104,7 @@ class MyObject4; std::unordered_set myobject4_instances; class MyObject4 { public: - MyObject4(int value) : value{value} { + explicit MyObject4(int value) : value{value} { print_created(this); myobject4_instances.insert(this); } @@ -130,7 +130,7 @@ class MyObject4a; std::unordered_set myobject4a_instances; class MyObject4a { public: - MyObject4a(int i) { + explicit MyObject4a(int i) { value = i; print_created(this); myobject4a_instances.insert(this); @@ -153,14 +153,14 @@ class MyObject4a { // Object derived but with public destructor and no Deleter in default holder class MyObject4b : public MyObject4a { public: - MyObject4b(int i) : MyObject4a(i) { print_created(this); } + explicit MyObject4b(int i) : MyObject4a(i) { print_created(this); } ~MyObject4b() override { print_destroyed(this); } }; // test_large_holder class MyObject5 { // managed by huge_unique_ptr public: - MyObject5(int value) : value{value} { print_created(this); } + explicit MyObject5(int value) : value{value} { print_created(this); } ~MyObject5() { print_destroyed(this); } int value; }; @@ -222,7 +222,7 @@ struct TypeForHolderWithAddressOf { // test_move_only_holder_with_addressof_operator struct TypeForMoveOnlyHolderWithAddressOf { - TypeForMoveOnlyHolderWithAddressOf(int value) : value{value} { print_created(this); } + explicit TypeForMoveOnlyHolderWithAddressOf(int value) : value{value} { print_created(this); } ~TypeForMoveOnlyHolderWithAddressOf() { print_destroyed(this); } std::string toString() const { return "MoveOnlyHolderWithAddressOf[" + std::to_string(value) + "]"; @@ -242,7 +242,7 @@ struct ElementBase { }; struct ElementA : ElementBase { - ElementA(int v) : v(v) { } + explicit ElementA(int v) : v(v) {} int value() const { return v; } int v; }; @@ -291,9 +291,9 @@ TEST_SUBMODULE(smart_ptr, m) { py::implicitly_convertible(); m.def("make_object_1", []() -> Object * { return new MyObject1(1); }); - m.def("make_object_2", []() -> ref { return new MyObject1(2); }); + m.def("make_object_2", []() -> ref { return ref(new MyObject1(2)); }); m.def("make_myobject1_1", []() -> MyObject1 * { return new MyObject1(4); }); - m.def("make_myobject1_2", []() -> ref { return new MyObject1(5); }); + m.def("make_myobject1_2", []() -> ref { return ref(new MyObject1(5)); }); m.def("print_object_1", [](const Object *obj) { py::print(obj->toString()); }); m.def("print_object_2", [](ref obj) { py::print(obj->toString()); }); m.def("print_object_3", [](const ref &obj) { py::print(obj->toString()); }); @@ -328,7 +328,7 @@ TEST_SUBMODULE(smart_ptr, m) { // test_smart_ptr_refcounting m.def("test_object1_refcounting", []() { - ref o = new MyObject1(0); + auto o = ref(new MyObject1(0)); bool good = o->getRefCount() == 1; py::object o2 = py::cast(o, py::return_value_policy::reference); // always request (partial) ownership for objects with intrusive diff --git a/tests/test_stl.cpp b/tests/test_stl.cpp index 23e2c07b32..7e3363c5ea 100644 --- a/tests/test_stl.cpp +++ b/tests/test_stl.cpp @@ -45,7 +45,8 @@ PYBIND11_MAKE_OPAQUE(std::vector>); /// Issue #528: templated constructor struct TplCtorClass { - template TplCtorClass(const T &) { } + template + explicit TplCtorClass(const T &) {} bool operator==(const TplCtorClass &) const { return true; } }; diff --git a/tests/test_stl_binders.cpp b/tests/test_stl_binders.cpp index 22847eb7a1..6b23e3529f 100644 --- a/tests/test_stl_binders.cpp +++ b/tests/test_stl_binders.cpp @@ -18,7 +18,7 @@ class El { public: El() = delete; - El(int v) : a(v) { } + explicit El(int v) : a(v) {} int a; }; diff --git a/tests/test_tagbased_polymorphic.cpp b/tests/test_tagbased_polymorphic.cpp index 90f40e14cc..2c7bad8bbc 100644 --- a/tests/test_tagbased_polymorphic.cpp +++ b/tests/test_tagbased_polymorphic.cpp @@ -37,33 +37,35 @@ struct Animal struct Dog : Animal { - Dog(const std::string& _name, Kind _kind = Kind::Dog) : Animal(_name, _kind) {} + explicit Dog(const std::string &_name, Kind _kind = Kind::Dog) : Animal(_name, _kind) {} std::string bark() const { return name_of_kind(kind) + " " + name + " goes " + sound; } std::string sound = "WOOF!"; }; struct Labrador : Dog { - Labrador(const std::string& _name, int _excitement = 9001) + explicit Labrador(const std::string &_name, int _excitement = 9001) : Dog(_name, Kind::Labrador), excitement(_excitement) {} int excitement; }; struct Chihuahua : Dog { - Chihuahua(const std::string& _name) : Dog(_name, Kind::Chihuahua) { sound = "iyiyiyiyiyi"; } + explicit Chihuahua(const std::string &_name) : Dog(_name, Kind::Chihuahua) { + sound = "iyiyiyiyiyi"; + } std::string bark() const { return Dog::bark() + " and runs in circles"; } }; struct Cat : Animal { - Cat(const std::string& _name, Kind _kind = Kind::Cat) : Animal(_name, _kind) {} + explicit Cat(const std::string &_name, Kind _kind = Kind::Cat) : Animal(_name, _kind) {} std::string purr() const { return "mrowr"; } }; struct Panther : Cat { - Panther(const std::string& _name) : Cat(_name, Kind::Panther) {} + explicit Panther(const std::string &_name) : Cat(_name, Kind::Panther) {} std::string purr() const { return "mrrrRRRRRR"; } }; diff --git a/tests/test_virtual_functions.cpp b/tests/test_virtual_functions.cpp index f83a7364b2..1eba534dd2 100644 --- a/tests/test_virtual_functions.cpp +++ b/tests/test_virtual_functions.cpp @@ -15,7 +15,7 @@ /* This is an example class that we'll want to be able to extend from Python */ class ExampleVirt { public: - ExampleVirt(int state) : state(state) { print_created(this, state); } + explicit ExampleVirt(int state) : state(state) { print_created(this, state); } ExampleVirt(const ExampleVirt &e) : state(e.state) { print_copy_created(this); } ExampleVirt(ExampleVirt &&e) noexcept : state(e.state) { print_move_created(this); From 4c6bee3514679cde3b506d5552a3a11fd09cc6d5 Mon Sep 17 00:00:00 2001 From: Aaron Gokaslan Date: Thu, 9 Sep 2021 14:06:33 -0400 Subject: [PATCH 163/638] fix: Set __file__ constant when using eval_file (#1300) (#3233) * Set __file__ constant when using eval_file * Use const ref * Use a move instead * Revert * Improve test * Guard test with Python version * Fix tests * Dont support Python2 API * Drop Python2 eval __file__ support * Hack * Semisupport Python2 * Take2 * Remove Python2 support --- include/pybind11/eval.h | 9 +++++++++ tests/test_cmake_build/test.py | 3 +++ 2 files changed, 12 insertions(+) diff --git a/include/pybind11/eval.h b/include/pybind11/eval.h index 33fcdc09d1..e0f58bcf49 100644 --- a/include/pybind11/eval.h +++ b/include/pybind11/eval.h @@ -136,6 +136,15 @@ object eval_file(str fname, object global = globals(), object local = object()) pybind11_fail("File \"" + fname_str + "\" could not be opened!"); } + // In Python2, this should be encoded by getfilesystemencoding. + // We don't boher setting it since Python2 is past EOL anyway. + // See PR#3233 +#if PY_VERSION_HEX >= 0x03000000 + if (!global.contains("__file__")) { + global["__file__"] = std::move(fname); + } +#endif + #if PY_VERSION_HEX < 0x03000000 && defined(PYPY_VERSION) PyObject *result = PyRun_File(f, fname_str.c_str(), start, global.ptr(), local.ptr()); diff --git a/tests/test_cmake_build/test.py b/tests/test_cmake_build/test.py index d1a290dcc5..972a27bea4 100644 --- a/tests/test_cmake_build/test.py +++ b/tests/test_cmake_build/test.py @@ -3,5 +3,8 @@ import test_cmake_build +if str is not bytes: # If not Python2 + assert isinstance(__file__, str) # Test this is properly set + assert test_cmake_build.add(1, 2) == 3 print("{} imports, runs, and adds: 1 + 2 = 3".format(sys.argv[1])) From 4d5ad03e1fd09b8be26843f7cb165051ab9a9c05 Mon Sep 17 00:00:00 2001 From: Jeremy Maitin-Shepard Date: Thu, 9 Sep 2021 12:56:10 -0700 Subject: [PATCH 164/638] Avoid use of temporary `bytes` object in string_caster for UTF-8 (#3257) Fixes #3252 --- include/pybind11/cast.h | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/include/pybind11/cast.h b/include/pybind11/cast.h index db79f57cda..3e621eba7d 100644 --- a/include/pybind11/cast.h +++ b/include/pybind11/cast.h @@ -377,6 +377,22 @@ template struct string_caster { #endif } +#if PY_VERSION_HEX >= 0x03030000 + // On Python >= 3.3, for UTF-8 we avoid the need for a temporary `bytes` + // object by using `PyUnicode_AsUTF8AndSize`. + if (PYBIND11_SILENCE_MSVC_C4127(UTF_N == 8)) { + Py_ssize_t size = -1; + const auto *buffer + = reinterpret_cast(PyUnicode_AsUTF8AndSize(load_src.ptr(), &size)); + if (!buffer) { + PyErr_Clear(); + return false; + } + value = StringType(buffer, static_cast(size)); + return true; + } +#endif + auto utfNbytes = reinterpret_steal(PyUnicode_AsEncodedString( load_src.ptr(), UTF_N == 8 ? "utf-8" : UTF_N == 16 ? "utf-16" : "utf-32", nullptr)); if (!utfNbytes) { PyErr_Clear(); return false; } From ae07d4c6c663f8cc2590d28ecd6baa09b055537f Mon Sep 17 00:00:00 2001 From: Aaron Gokaslan Date: Fri, 10 Sep 2021 00:27:36 -0400 Subject: [PATCH 165/638] maint(Clang-Tidy): readability-const-return (#3254) * Enable clang-tidy readability-const-return * PyTest functional * Fix regression * Fix actual regression * Remove one more NOLINT * Update comment --- .clang-tidy | 3 ++- include/pybind11/cast.h | 7 +++++-- include/pybind11/detail/descr.h | 3 ++- include/pybind11/pybind11.h | 4 ++-- include/pybind11/pytypes.h | 6 +++--- tests/test_eigen.cpp | 4 ++++ 6 files changed, 18 insertions(+), 9 deletions(-) diff --git a/.clang-tidy b/.clang-tidy index a6437dd494..dbe85a8b47 100644 --- a/.clang-tidy +++ b/.clang-tidy @@ -31,9 +31,10 @@ modernize-use-override, modernize-use-using, *performance*, readability-avoid-const-params-in-decls, +readability-const-return-type, readability-container-size-empty, -readability-else-after-return, readability-delete-null-pointer, +readability-else-after-return, readability-implicit-bool-conversion, readability-make-member-function-const, readability-misplaced-array-index, diff --git a/include/pybind11/cast.h b/include/pybind11/cast.h index 3e621eba7d..17b10157b5 100644 --- a/include/pybind11/cast.h +++ b/include/pybind11/cast.h @@ -1194,13 +1194,15 @@ class argument_loader { } template + // NOLINTNEXTLINE(readability-const-return-type) enable_if_t::value, Return> call(Func &&f) && { - return std::move(*this).template call_impl(std::forward(f), indices{}, Guard{}); + return std::move(*this).template call_impl>(std::forward(f), indices{}, Guard{}); } template + // NOLINTNEXTLINE(readability-const-return-type) enable_if_t::value, void_type> call(Func &&f) && { - std::move(*this).template call_impl(std::forward(f), indices{}, Guard{}); + std::move(*this).template call_impl>(std::forward(f), indices{}, Guard{}); return void_type(); } @@ -1222,6 +1224,7 @@ class argument_loader { } template + // NOLINTNEXTLINE(readability-const-return-type) Return call_impl(Func &&f, index_sequence, Guard &&) && { return std::forward(f)(cast_op(std::move(std::get(argcasters)))...); } diff --git a/include/pybind11/detail/descr.h b/include/pybind11/detail/descr.h index 0b498e5e72..c62e541bda 100644 --- a/include/pybind11/detail/descr.h +++ b/include/pybind11/detail/descr.h @@ -77,7 +77,8 @@ constexpr enable_if_t _(const T1 &d, const T2 &) { return d; } template constexpr enable_if_t _(const T1 &, const T2 &d) { return d; } -template auto constexpr _() -> decltype(int_to_str::digits) { +template +auto constexpr _() -> remove_cv_t::digits)> { return int_to_str::digits; } diff --git a/include/pybind11/pybind11.h b/include/pybind11/pybind11.h index 82d339751e..c4e5ea6e1f 100644 --- a/include/pybind11/pybind11.h +++ b/include/pybind11/pybind11.h @@ -2002,13 +2002,13 @@ template ()).first), #endif typename... Extra> -iterator make_key_iterator(Iterator first, Sentinel last, Extra &&... extra) { +iterator make_key_iterator(Iterator first, Sentinel last, Extra &&...extra) { using state = detail::iterator_state; if (!detail::get_type_info(typeid(state), false)) { class_(handle(), "iterator", pybind11::module_local()) .def("__iter__", [](state &s) -> state& { return s; }) - .def("__next__", [](state &s) -> KeyType { + .def("__next__", [](state &s) -> detail::remove_cv_t { if (!s.first_or_done) ++s.it; else diff --git a/include/pybind11/pytypes.h b/include/pybind11/pytypes.h index 971db85f37..2de3f5f106 100644 --- a/include/pybind11/pytypes.h +++ b/include/pybind11/pytypes.h @@ -773,7 +773,7 @@ class sequence_fast_readonly { protected: using iterator_category = std::random_access_iterator_tag; using value_type = handle; - using reference = const handle; + using reference = handle; using pointer = arrow_proxy; sequence_fast_readonly(handle obj, ssize_t n) : ptr(PySequence_Fast_ITEMS(obj.ptr()) + n) { } @@ -816,7 +816,7 @@ class dict_readonly { protected: using iterator_category = std::forward_iterator_tag; using value_type = std::pair; - using reference = const value_type; + using reference = value_type; using pointer = arrow_proxy; dict_readonly() = default; @@ -966,7 +966,7 @@ class iterator : public object { using iterator_category = std::input_iterator_tag; using difference_type = ssize_t; using value_type = handle; - using reference = const handle; + using reference = handle; using pointer = const handle *; PYBIND11_OBJECT_DEFAULT(iterator, object, PyIter_Check) diff --git a/tests/test_eigen.cpp b/tests/test_eigen.cpp index 651be0575f..ad572b2ad7 100644 --- a/tests/test_eigen.cpp +++ b/tests/test_eigen.cpp @@ -178,6 +178,7 @@ TEST_SUBMODULE(eigen, m) { ReturnTester() { print_created(this); } ~ReturnTester() { print_destroyed(this); } static Eigen::MatrixXd create() { return Eigen::MatrixXd::Ones(10, 10); } + // NOLINTNEXTLINE(readability-const-return-type) static const Eigen::MatrixXd createConst() { return Eigen::MatrixXd::Ones(10, 10); } Eigen::MatrixXd &get() { return mat; } Eigen::MatrixXd *getPtr() { return &mat; } @@ -244,6 +245,9 @@ TEST_SUBMODULE(eigen, m) { // test_fixed, and various other tests m.def("fixed_r", [mat]() -> FixedMatrixR { return FixedMatrixR(mat); }); + // Our Eigen does a hack which respects constness through the numpy writeable flag. + // Therefore, the const return actually affects this type despite being an rvalue. + // NOLINTNEXTLINE(readability-const-return-type) m.def("fixed_r_const", [mat]() -> const FixedMatrixR { return FixedMatrixR(mat); }); m.def("fixed_c", [mat]() -> FixedMatrixC { return FixedMatrixC(mat); }); m.def("fixed_copy_r", [](const FixedMatrixR &m) -> FixedMatrixR { return m; }); From 121b91f99c9044e12ea1f746d7f3e586c41529c8 Mon Sep 17 00:00:00 2001 From: "Ralf W. Grosse-Kunstleve" Date: Fri, 10 Sep 2021 07:16:09 -0700 Subject: [PATCH 166/638] Fixing NOLINT mishap (#3260) * Removing NOLINT pointed out by Aaron. * Removing another NOLINT. --- include/pybind11/cast.h | 2 -- 1 file changed, 2 deletions(-) diff --git a/include/pybind11/cast.h b/include/pybind11/cast.h index 17b10157b5..1ec2080f8a 100644 --- a/include/pybind11/cast.h +++ b/include/pybind11/cast.h @@ -1200,7 +1200,6 @@ class argument_loader { } template - // NOLINTNEXTLINE(readability-const-return-type) enable_if_t::value, void_type> call(Func &&f) && { std::move(*this).template call_impl>(std::forward(f), indices{}, Guard{}); return void_type(); @@ -1224,7 +1223,6 @@ class argument_loader { } template - // NOLINTNEXTLINE(readability-const-return-type) Return call_impl(Func &&f, index_sequence, Guard &&) && { return std::forward(f)(cast_op(std::move(std::get(argcasters)))...); } From 0e599589fe821f86d18635c13636f3042d4c06b9 Mon Sep 17 00:00:00 2001 From: Laramie Leavitt Date: Fri, 10 Sep 2021 09:29:21 -0700 Subject: [PATCH 167/638] Fix thread safety for pybind11 loader_life_support (#3237) * Fix thread safety for pybind11 loader_life_support Fixes issue: https://github.com/pybind/pybind11/issues/2765 This converts the vector of PyObjects to either a single void* or a per-thread void* depending on the WITH_THREAD define. The new field is used by each thread to construct a stack of loader_life_support frames that can extend the life of python objects. The pointer is updated when the loader_life_support object is allocated (which happens before a call) as well as on release. Each loader_life_support maintains a set of PyObject references that need to be lifetime extended; this is done by storing them in a c++ std::unordered_set and clearing the references when the method completes. * Also update the internals version as the internal struct is no longer compatible * Add test demonstrating threading works correctly. It may be appropriate to run this under msan/tsan/etc. * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Update test to use lifetime-extended references rather than std::string_view, as that's a C++ 17 feature. * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Make loader_life_support members private * Update version to dev2 * Update test to use python threading rather than concurrent.futures * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Remove unnecessary env in test * Remove unnecessary pytest in test * Use native C++ thread_local in place of python per-thread data structures to retain compatability * clang-format test_thread.cpp * Add a note about debugging the py::cast() error * thread_test.py now propagates exceptions on join() calls. * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * remove unused sys / merge * Update include order in test_thread.cpp * Remove spurious whitespace * Update comment / whitespace. * Address review comments * lint cleanup * Fix test IntStruct constructor. * Add explicit to constructor Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Aaron Gokaslan --- include/pybind11/detail/common.h | 4 +- include/pybind11/detail/internals.h | 6 +- include/pybind11/detail/type_caster_base.h | 55 ++++++++++-------- pybind11/_version.py | 2 +- tests/CMakeLists.txt | 1 + tests/test_thread.cpp | 66 ++++++++++++++++++++++ tests/test_thread.py | 44 +++++++++++++++ 7 files changed, 148 insertions(+), 30 deletions(-) create mode 100644 tests/test_thread.cpp create mode 100644 tests/test_thread.py diff --git a/include/pybind11/detail/common.h b/include/pybind11/detail/common.h index a8fd7d917e..8aeb79fb7d 100644 --- a/include/pybind11/detail/common.h +++ b/include/pybind11/detail/common.h @@ -11,11 +11,11 @@ #define PYBIND11_VERSION_MAJOR 2 #define PYBIND11_VERSION_MINOR 8 -#define PYBIND11_VERSION_PATCH 0.dev1 +#define PYBIND11_VERSION_PATCH 0.dev2 // Similar to Python's convention: https://docs.python.org/3/c-api/apiabiversion.html // Additional convention: 0xD = dev -#define PYBIND11_VERSION_HEX 0x020800D1 +#define PYBIND11_VERSION_HEX 0x020800D2 #define PYBIND11_NAMESPACE_BEGIN(name) namespace name { #define PYBIND11_NAMESPACE_END(name) } diff --git a/include/pybind11/detail/internals.h b/include/pybind11/detail/internals.h index b177801a11..39c28e7732 100644 --- a/include/pybind11/detail/internals.h +++ b/include/pybind11/detail/internals.h @@ -106,7 +106,7 @@ struct internals { std::unordered_map> patients; std::forward_list registered_exception_translators; std::unordered_map shared_data; // Custom data to be shared across extensions - std::vector loader_patient_stack; // Used by `loader_life_support` + std::vector unused_loader_patient_stack_remove_at_v5; std::forward_list static_strings; // Stores the std::strings backing detail::c_str() PyTypeObject *static_property_type; PyTypeObject *default_metaclass; @@ -298,12 +298,12 @@ PYBIND11_NOINLINE internals &get_internals() { #if PY_VERSION_HEX >= 0x03070000 internals_ptr->tstate = PyThread_tss_alloc(); if (!internals_ptr->tstate || (PyThread_tss_create(internals_ptr->tstate) != 0)) - pybind11_fail("get_internals: could not successfully initialize the TSS key!"); + pybind11_fail("get_internals: could not successfully initialize the tstate TSS key!"); PyThread_tss_set(internals_ptr->tstate, tstate); #else internals_ptr->tstate = PyThread_create_key(); if (internals_ptr->tstate == -1) - pybind11_fail("get_internals: could not successfully initialize the TLS key!"); + pybind11_fail("get_internals: could not successfully initialize the tstate TLS key!"); PyThread_set_key_value(internals_ptr->tstate, tstate); #endif internals_ptr->istate = tstate->interp; diff --git a/include/pybind11/detail/type_caster_base.h b/include/pybind11/detail/type_caster_base.h index 4c9ac7b8f6..fa8433cfec 100644 --- a/include/pybind11/detail/type_caster_base.h +++ b/include/pybind11/detail/type_caster_base.h @@ -31,47 +31,54 @@ PYBIND11_NAMESPACE_BEGIN(detail) /// A life support system for temporary objects created by `type_caster::load()`. /// Adding a patient will keep it alive up until the enclosing function returns. class loader_life_support { +private: + loader_life_support* parent = nullptr; + std::unordered_set keep_alive; + + static loader_life_support** get_stack_pp() { +#if defined(WITH_THREAD) + thread_local static loader_life_support* per_thread_stack = nullptr; + return &per_thread_stack; +#else + static loader_life_support* global_stack = nullptr; + return &global_stack; +#endif + } + public: /// A new patient frame is created when a function is entered loader_life_support() { - get_internals().loader_patient_stack.push_back(nullptr); + loader_life_support** stack = get_stack_pp(); + parent = *stack; + *stack = this; } /// ... and destroyed after it returns ~loader_life_support() { - auto &stack = get_internals().loader_patient_stack; - if (stack.empty()) + loader_life_support** stack = get_stack_pp(); + if (*stack != this) pybind11_fail("loader_life_support: internal error"); - - auto ptr = stack.back(); - stack.pop_back(); - Py_CLEAR(ptr); - - // A heuristic to reduce the stack's capacity (e.g. after long recursive calls) - if (stack.capacity() > 16 && !stack.empty() && stack.capacity() / stack.size() > 2) - stack.shrink_to_fit(); + *stack = parent; + for (auto* item : keep_alive) + Py_DECREF(item); } /// This can only be used inside a pybind11-bound function, either by `argument_loader` /// at argument preparation time or by `py::cast()` at execution time. PYBIND11_NOINLINE static void add_patient(handle h) { - auto &stack = get_internals().loader_patient_stack; - if (stack.empty()) + loader_life_support* frame = *get_stack_pp(); + if (!frame) { + // NOTE: It would be nice to include the stack frames here, as this indicates + // use of pybind11::cast<> outside the normal call framework, finding such + // a location is challenging. Developers could consider printing out + // stack frame addresses here using something like __builtin_frame_address(0) throw cast_error("When called outside a bound function, py::cast() cannot " "do Python -> C++ conversions which require the creation " "of temporary values"); - - auto &list_ptr = stack.back(); - if (list_ptr == nullptr) { - list_ptr = PyList_New(1); - if (!list_ptr) - pybind11_fail("loader_life_support: error allocating list"); - PyList_SET_ITEM(list_ptr, 0, h.inc_ref().ptr()); - } else { - auto result = PyList_Append(list_ptr, h.ptr()); - if (result == -1) - pybind11_fail("loader_life_support: error adding patient"); } + + if (frame->keep_alive.insert(h.ptr()).second) + Py_INCREF(h.ptr()); } }; diff --git a/pybind11/_version.py b/pybind11/_version.py index 610d39bf1c..d212f1dfb7 100644 --- a/pybind11/_version.py +++ b/pybind11/_version.py @@ -8,5 +8,5 @@ def _to_int(s): return s -__version__ = "2.8.0.dev1" +__version__ = "2.8.0.dev2" version_info = tuple(_to_int(s) for s in __version__.split(".")) diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index d71a51e6a0..f014771d54 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -129,6 +129,7 @@ set(PYBIND11_TEST_FILES test_stl.cpp test_stl_binders.cpp test_tagbased_polymorphic.cpp + test_thread.cpp test_union.cpp test_virtual_functions.cpp) diff --git a/tests/test_thread.cpp b/tests/test_thread.cpp new file mode 100644 index 0000000000..19d91768b3 --- /dev/null +++ b/tests/test_thread.cpp @@ -0,0 +1,66 @@ +/* + tests/test_thread.cpp -- call pybind11 bound methods in threads + + Copyright (c) 2021 Laramie Leavitt (Google LLC) + + All rights reserved. Use of this source code is governed by a + BSD-style license that can be found in the LICENSE file. +*/ + +#include +#include + +#include +#include + +#include "pybind11_tests.h" + +namespace py = pybind11; + +namespace { + +struct IntStruct { + explicit IntStruct(int v) : value(v) {}; + ~IntStruct() { value = -value; } + IntStruct(const IntStruct&) = default; + IntStruct& operator=(const IntStruct&) = default; + + int value; +}; + +} // namespace + +TEST_SUBMODULE(thread, m) { + + py::class_(m, "IntStruct").def(py::init([](const int i) { return IntStruct(i); })); + + // implicitly_convertible uses loader_life_support when an implicit + // conversion is required in order to lifetime extend the reference. + // + // This test should be run with ASAN for better effectiveness. + py::implicitly_convertible(); + + m.def("test", [](int expected, const IntStruct &in) { + { + py::gil_scoped_release release; + std::this_thread::sleep_for(std::chrono::milliseconds(5)); + } + + if (in.value != expected) { + throw std::runtime_error("Value changed!!"); + } + }); + + m.def( + "test_no_gil", + [](int expected, const IntStruct &in) { + std::this_thread::sleep_for(std::chrono::milliseconds(5)); + if (in.value != expected) { + throw std::runtime_error("Value changed!!"); + } + }, + py::call_guard()); + + // NOTE: std::string_view also uses loader_life_support to ensure that + // the string contents remain alive, but that's a C++ 17 feature. +} diff --git a/tests/test_thread.py b/tests/test_thread.py new file mode 100644 index 0000000000..f9db1babaf --- /dev/null +++ b/tests/test_thread.py @@ -0,0 +1,44 @@ +# -*- coding: utf-8 -*- + +import threading + +from pybind11_tests import thread as m + + +class Thread(threading.Thread): + def __init__(self, fn): + super(Thread, self).__init__() + self.fn = fn + self.e = None + + def run(self): + try: + for i in range(10): + self.fn(i, i) + except Exception as e: + self.e = e + + def join(self): + super(Thread, self).join() + if self.e: + raise self.e + + +def test_implicit_conversion(): + a = Thread(m.test) + b = Thread(m.test) + c = Thread(m.test) + for x in [a, b, c]: + x.start() + for x in [c, b, a]: + x.join() + + +def test_implicit_conversion_no_gil(): + a = Thread(m.test_no_gil) + b = Thread(m.test_no_gil) + c = Thread(m.test_no_gil) + for x in [a, b, c]: + x.start() + for x in [c, b, a]: + x.join() From 9978ed588baa2cf804794b8ee9f765a316ab16e2 Mon Sep 17 00:00:00 2001 From: Aaron Gokaslan Date: Fri, 10 Sep 2021 14:23:32 -0400 Subject: [PATCH 168/638] Fix capsule bug (#3261) Thanks Aaron for jumping in fixing this! --- include/pybind11/pytypes.h | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/include/pybind11/pytypes.h b/include/pybind11/pytypes.h index 2de3f5f106..a1cf6bef0f 100644 --- a/include/pybind11/pytypes.h +++ b/include/pybind11/pytypes.h @@ -1414,14 +1414,19 @@ class capsule : public object { T* get_pointer() const { auto name = this->name(); T *result = static_cast(PyCapsule_GetPointer(m_ptr, name)); - if (!result) pybind11_fail("Unable to extract capsule contents!"); + if (!result) { + PyErr_Clear(); + pybind11_fail("Unable to extract capsule contents!"); + } return result; } /// Replaces a capsule's pointer *without* calling the destructor on the existing one. void set_pointer(const void *value) { - if (PyCapsule_SetPointer(m_ptr, const_cast(value)) != 0) + if (PyCapsule_SetPointer(m_ptr, const_cast(value)) != 0) { + PyErr_Clear(); pybind11_fail("Could not set capsule pointer"); + } } const char *name() const { return PyCapsule_GetName(m_ptr); } From 6c65ab5950a75271bddced3f4238fff2c9a427a5 Mon Sep 17 00:00:00 2001 From: "Ralf W. Grosse-Kunstleve" Date: Sun, 12 Sep 2021 19:53:26 -0700 Subject: [PATCH 169/638] Follow-on to PR #3254, to address user code breakages. (#3263) * Restoring `const` removed from pytypes.h in PR #3254, adding tests reflective of user code that breaks when those `const` are removed. * clang-tidy NOLINTs (and one collateral fix). * Inserting PYBIND11_CONST_FOR_STRICT_PLATFORMS * Trying `defined(__APPLE__)` * Trying again: `auto it` for strict platforms. * Adding NOLINTNEXTLINE(bugprone-macro-parentheses), expanding comments. * Labeling all changes with `PR #3263`, for easy reference, and to make it easy to undo these changes if we decide to do so in the future. --- include/pybind11/pybind11.h | 1 + include/pybind11/pytypes.h | 11 +++++++--- tests/test_pytypes.cpp | 43 +++++++++++++++++++++++++++++++++++++ tests/test_pytypes.py | 6 ++++++ 4 files changed, 58 insertions(+), 3 deletions(-) diff --git a/include/pybind11/pybind11.h b/include/pybind11/pybind11.h index c4e5ea6e1f..07d2d04363 100644 --- a/include/pybind11/pybind11.h +++ b/include/pybind11/pybind11.h @@ -1987,6 +1987,7 @@ iterator make_iterator(Iterator first, Sentinel last, Extra &&... extra) { throw stop_iteration(); } return *s.it; + // NOLINTNEXTLINE(readability-const-return-type) // PR #3263 }, std::forward(extra)..., Policy); } diff --git a/include/pybind11/pytypes.h b/include/pybind11/pytypes.h index a1cf6bef0f..d1d3dcb05e 100644 --- a/include/pybind11/pytypes.h +++ b/include/pybind11/pytypes.h @@ -733,7 +733,9 @@ class generic_iterator : public Policy { generic_iterator() = default; generic_iterator(handle seq, ssize_t index) : Policy(seq, index) { } + // NOLINTNEXTLINE(readability-const-return-type) // PR #3263 reference operator*() const { return Policy::dereference(); } + // NOLINTNEXTLINE(readability-const-return-type) // PR #3263 reference operator[](difference_type n) const { return *(*this + n); } pointer operator->() const { return **this; } @@ -773,11 +775,12 @@ class sequence_fast_readonly { protected: using iterator_category = std::random_access_iterator_tag; using value_type = handle; - using reference = handle; + using reference = const handle; // PR #3263 using pointer = arrow_proxy; sequence_fast_readonly(handle obj, ssize_t n) : ptr(PySequence_Fast_ITEMS(obj.ptr()) + n) { } + // NOLINTNEXTLINE(readability-const-return-type) // PR #3263 reference dereference() const { return *ptr; } void increment() { ++ptr; } void decrement() { --ptr; } @@ -816,12 +819,13 @@ class dict_readonly { protected: using iterator_category = std::forward_iterator_tag; using value_type = std::pair; - using reference = value_type; + using reference = const value_type; // PR #3263 using pointer = arrow_proxy; dict_readonly() = default; dict_readonly(handle obj, ssize_t pos) : obj(obj), pos(pos) { increment(); } + // NOLINTNEXTLINE(readability-const-return-type) // PR #3263 reference dereference() const { return {key, value}; } void increment() { if (PyDict_Next(obj.ptr(), &pos, &key, &value) == 0) { @@ -966,7 +970,7 @@ class iterator : public object { using iterator_category = std::input_iterator_tag; using difference_type = ssize_t; using value_type = handle; - using reference = handle; + using reference = const handle; // PR #3263 using pointer = const handle *; PYBIND11_OBJECT_DEFAULT(iterator, object, PyIter_Check) @@ -982,6 +986,7 @@ class iterator : public object { return rv; } + // NOLINTNEXTLINE(readability-const-return-type) // PR #3263 reference operator*() const { if (m_ptr && !value.ptr()) { auto& self = const_cast(*this); diff --git a/tests/test_pytypes.cpp b/tests/test_pytypes.cpp index 96c97351e1..2157dc0973 100644 --- a/tests/test_pytypes.cpp +++ b/tests/test_pytypes.cpp @@ -462,6 +462,49 @@ TEST_SUBMODULE(pytypes, m) { m.def("weakref_from_object_and_function", [](py::object o, py::function f) { return py::weakref(std::move(o), std::move(f)); }); +// See PR #3263 for background (https://github.com/pybind/pybind11/pull/3263): +// pytypes.h could be changed to enforce the "most correct" user code below, by removing +// `const` from iterator `reference` using type aliases, but that will break existing +// user code. +#if (defined(__APPLE__) && defined(__clang__)) || defined(PYPY_VERSION) +// This is "most correct" and enforced on these platforms. +# define PYBIND11_AUTO_IT auto it +#else +// This works on many platforms and is (unfortunately) reflective of existing user code. +// NOLINTNEXTLINE(bugprone-macro-parentheses) +# define PYBIND11_AUTO_IT auto &it +#endif + + m.def("tuple_iterator", []() { + auto tup = py::make_tuple(5, 7); + int tup_sum = 0; + for (PYBIND11_AUTO_IT : tup) { + tup_sum += it.cast(); + } + return tup_sum; + }); + + m.def("dict_iterator", []() { + py::dict dct; + dct[py::int_(3)] = 5; + dct[py::int_(7)] = 11; + int kv_sum = 0; + for (PYBIND11_AUTO_IT : dct) { + kv_sum += it.first.cast() * 100 + it.second.cast(); + } + return kv_sum; + }); + + m.def("passed_iterator", [](const py::iterator &py_it) { + int elem_sum = 0; + for (PYBIND11_AUTO_IT : py_it) { + elem_sum += it.cast(); + } + return elem_sum; + }); + +#undef PYBIND11_AUTO_IT + // Tests below this line are for pybind11 IMPLEMENTATION DETAILS: m.def("sequence_item_get_ssize_t", [](const py::object &o) { diff --git a/tests/test_pytypes.py b/tests/test_pytypes.py index 18847550f2..070538cc9f 100644 --- a/tests/test_pytypes.py +++ b/tests/test_pytypes.py @@ -623,6 +623,12 @@ def callback(wr): assert callback.called +def test_cpp_iterators(): + assert m.tuple_iterator() == 12 + assert m.dict_iterator() == 305 + 711 + assert m.passed_iterator(iter((-7, 3))) == -4 + + def test_implementation_details(): lst = [39, 43, 92, 49, 22, 29, 93, 98, 26, 57, 8] tup = tuple(lst) From 3ed31e9289acaecb333e851ecb59912c784cdd3d Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 13 Sep 2021 15:58:30 -0400 Subject: [PATCH 170/638] [pre-commit.ci] pre-commit autoupdate (#3266) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit updates: - [github.com/asottile/pyupgrade: v2.25.0 → v2.26.0](https://github.com/asottile/pyupgrade/compare/v2.25.0...v2.26.0) Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> --- .pre-commit-config.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 06e82fe4b1..fd11cf0cda 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -31,7 +31,7 @@ repos: exclude: ^noxfile.py$ - repo: https://github.com/asottile/pyupgrade - rev: v2.25.0 + rev: v2.26.0 hooks: - id: pyupgrade From e0031bfcebc189b978e650cf67620788454453d3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matthias=20K=C3=B6ppe?= Date: Wed, 15 Sep 2021 10:52:32 -0700 Subject: [PATCH 171/638] include/pybind11/numpy.h: gcc 4.8.4 does not have is_trivially_copyable (#3270) --- include/pybind11/numpy.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/pybind11/numpy.h b/include/pybind11/numpy.h index d55844086d..b7747fae2d 100644 --- a/include/pybind11/numpy.h +++ b/include/pybind11/numpy.h @@ -322,7 +322,7 @@ template using remove_all_extents_t = typename array_info::type; template using is_pod_struct = all_of< std::is_standard_layout, // since we're accessing directly in memory we need a standard layout type -#if defined(__GLIBCXX__) && (__GLIBCXX__ < 20150422 || __GLIBCXX__ == 20150623 || __GLIBCXX__ == 20150626 || __GLIBCXX__ == 20160803) +#if defined(__GLIBCXX__) && (__GLIBCXX__ < 20150422 || __GLIBCXX__ == 20150426 || __GLIBCXX__ == 20150623 || __GLIBCXX__ == 20150626 || __GLIBCXX__ == 20160803) // libstdc++ < 5 (including versions 4.8.5, 4.9.3 and 4.9.4 which were released after 5) // don't implement is_trivially_copyable, so approximate it std::is_trivially_destructible, From 04dd3262f0420d48531a82021fa4895ba14bdcb2 Mon Sep 17 00:00:00 2001 From: Henry Schreiner Date: Fri, 17 Sep 2021 17:28:26 -0400 Subject: [PATCH 172/638] docs: update CHANGELOG (#3276) --- docs/advanced/pycpp/numpy.rst | 39 +++++++++--- docs/changelog.rst | 114 ++++++++++++++++++++++++++++++++++ 2 files changed, 146 insertions(+), 7 deletions(-) diff --git a/docs/advanced/pycpp/numpy.rst b/docs/advanced/pycpp/numpy.rst index 53ec8c1a3c..cae068e910 100644 --- a/docs/advanced/pycpp/numpy.rst +++ b/docs/advanced/pycpp/numpy.rst @@ -171,6 +171,31 @@ template parameter, and it ensures that non-conforming arguments are converted into an array satisfying the specified requirements instead of trying the next function overload. +There are several methods on arrays; the methods listed below under references +work, as well as the following functions based on the NumPy API: + +- ``.dtype()`` returns the type of the contained values. + +- ``.strides()`` returns a pointer to the strides of the array (optionally pass + an integer axis to get a number). + +- ``.flags()`` returns the flag settings. ``.writable()`` and ``.owndata()`` + are directly available. + +- ``.offset_at()`` returns the offset (optionally pass indices). + +- ``.squeeze()`` returns a view with length-1 axes removed. + +- ``.view(dtype)`` returns a view of the array with a different dtype. + +- ``.reshape({i, j, ...})`` returns a view of the array with a different shape. + ``.resize({...})`` is also available. + +- ``.index_at(i, j, ...)`` gets the count from the beginning to a given index. + + +There are also several methods for getting references (described below). + Structured types ================ @@ -345,21 +370,21 @@ The returned proxy object supports some of the same methods as ``py::array`` so that it can be used as a drop-in replacement for some existing, index-checked uses of ``py::array``: -- ``r.ndim()`` returns the number of dimensions +- ``.ndim()`` returns the number of dimensions -- ``r.data(1, 2, ...)`` and ``r.mutable_data(1, 2, ...)``` returns a pointer to +- ``.data(1, 2, ...)`` and ``r.mutable_data(1, 2, ...)``` returns a pointer to the ``const T`` or ``T`` data, respectively, at the given indices. The latter is only available to proxies obtained via ``a.mutable_unchecked()``. -- ``itemsize()`` returns the size of an item in bytes, i.e. ``sizeof(T)``. +- ``.itemsize()`` returns the size of an item in bytes, i.e. ``sizeof(T)``. -- ``ndim()`` returns the number of dimensions. +- ``.ndim()`` returns the number of dimensions. -- ``shape(n)`` returns the size of dimension ``n`` +- ``.shape(n)`` returns the size of dimension ``n`` -- ``size()`` returns the total number of elements (i.e. the product of the shapes). +- ``.size()`` returns the total number of elements (i.e. the product of the shapes). -- ``nbytes()`` returns the number of bytes used by the referenced elements +- ``.nbytes()`` returns the number of bytes used by the referenced elements (i.e. ``itemsize()`` times ``size()``). .. seealso:: diff --git a/docs/changelog.rst b/docs/changelog.rst index 2f76abe055..8b9690caaf 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -9,6 +9,11 @@ Starting with version 1.8.0, pybind11 releases use a `semantic versioning v2.8.0 (WIP) ------------ +New features: + +* Added ``py::raise_from`` to enable chaining exceptions. + `#3215 `_ + * Allow exception translators to be optionally registered local to a module instead of applying globally across all pybind11 modules. Use ``register_local_exception_translator(ExceptionTranslator&& translator)`` @@ -16,6 +21,115 @@ v2.8.0 (WIP) translator)`` to keep your exception remapping code local to the module. `#2650 `_ +* Add ``make_simple_namespace`` function for instantiating Python + ``SimpleNamespace`` objects. + `#2840 `_ + +* ``pybind11::scoped_interpreter`` and ``initialize_interpreter`` have new + arguments to allow ``sys.argv`` initialization. + `#2341 `_ + +* Allow Python builtins to be used as callbacks in CPython. + `#1413 `_ + +* Added ``view`` to view arrays with a different datatype. + `#987 `_ + +* Implemented ``reshape`` on arrays. + `#984 `_ + + +Changes: + +* Set ``__file__`` constant when running ``eval_file`` in an embedded interpreter. + `#3233 `_ + +* The pybind11 proxy types ``str``, ``bytes``, ``bytearray``, ``tuple``, + ``list`` now consistently support passing ``ssize_t`` values for sizes and + indexes. Previously, only ``size_t`` was accepted in several interfaces. + `#3219 `_ + + +Fixes: + +* Bug fix: enum value's ``__int__`` returning non-int when underlying type is bool or of char type. + `#1334 `_ + +* Fixes bug in setting error state in Capsule's pointer methods. + `#3261 `_ + +* A long-standing memory leak in ``py::cpp_function::initialize`` was fixed. + `#3229 `_ + +* Fixes thread safety for some ``pybind11::type_caster`` which require lifetime extension, such as for ``std::string_view``. + `#3237 `_ + +* Restore compatibility with gcc 4.8.4 as distributed by ubuntu-trusty, linuxmint-17. + `#3270 `_ + + +Build system improvements: + +* Fix regression in CMake Python package config: improper use of absolute path. + `#3144 `_ + +* Specified UTF8-encoding in setup.py calls of open(). + `#3137 `_ + + +Backend and tidying up: + +* Optimize NumPy array construction with additional moves. + `#3183 `_ + +* Conversion to ``std::string`` and ``std::string_view`` now avoids making an + extra copy of the data on Python >= 3.3. + `#3257 `_ + +* Remove const modifier from certain C++ methods on Python collections + (``list``, ``set``, ``dict``) such as (``clear()``, ``append()``, + ``insert()``, etc...) and annotated them with ``py-non-const``. + +* Enable readability ``clang-tidy-const-return`` and remove useless consts. + `#3254 `_ + `#3194 `_ + +* The clang-tidy ``google-explicit-constructor`` option was enabled. + `#3250 `_ + +* Mark a pytype move constructor as noexcept (perf). + `#3236 `_ + +* Enable clang-tidy check to guard against inheritance slicing. + `#3210 `_ + +* Legacy warning suppression pragma were removed from eigen.h. On Unix + platforms, please use -isystem for Eigen include directories, to suppress + compiler warnings originating from Eigen headers. Note that CMake does this + by default. No adjustments are needed for Windows. + `#3198 `_ + +* Format pybind11 with isort consistent ordering of imports + `#3195 `_ + +* The warnings-suppression "pragma clamp" at the top/bottom of pybind11 was + removed, clearing the path to refactoring and IWYU cleanup. + `#3186 `_ + +* Enable most bugprone checks in clang-tidy and fix the found potential bugs + and poor coding styles. + `#3166 `_ + +* Add ``clang-tidy-readability`` rules to make boolean casts explicit improving + code readability. Also enabled other misc and readability clang-tidy checks. + `#3148 `_ + +* Move object in ``.pop()`` for list. + `#3116 `_ + + + + v2.7.1 (Aug 3, 2021) --------------------- From 14976c853b8422dc41ce7874742cab122684c97e Mon Sep 17 00:00:00 2001 From: Jeremy Maitin-Shepard Date: Mon, 20 Sep 2021 04:57:38 -0700 Subject: [PATCH 173/638] Eliminate duplicate TLS keys for loader_life_support stack (#3275) * Eliminate duplicate TLS keys for loader_life_support stack This revises the existing fix for https://github.com/pybind/pybind11/issues/2765 in https://github.com/pybind/pybind11/pull/3237 to reduce the amount of TLS storage used. The shared TLS key is stored in two different ways, depending on `PYBIND11_INTERNALS_VERSION`. If `PYBIND11_INTERNALS_VERSION == 4` (as is currently set), the TLS key is stored in the `internal::shared_data` map to avoid breaking ABI compatibility. If `PYBIND11_INTERNALS_VERSION > 4`, the TLS key is stored directly in the `internals` struct. * Fix test_pytypes.py::test_issue2361 failure on PyPy3.7 * Add github actions tests for unstable ABI --- .github/workflows/ci.yml | 18 +++ CMakeLists.txt | 7 + include/pybind11/detail/internals.h | 178 +++++++++++++++------ include/pybind11/detail/type_caster_base.h | 39 +++-- include/pybind11/gil.h | 4 +- tests/test_pytypes.py | 3 +- 6 files changed, 186 insertions(+), 63 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index a3664ddf55..2348212f4b 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -144,6 +144,24 @@ jobs: if: "!(runner.os == 'Windows' && (matrix.python == 3.8 || matrix.python == 3.9 || matrix.python == '3.10-dev'))" run: cmake --build build2 --target cpptest + # Third build - C++17 mode with unstable ABI + - name: Configure (unstable ABI) + run: > + cmake -S . -B build3 + -DPYBIND11_WERROR=ON + -DDOWNLOAD_CATCH=ON + -DDOWNLOAD_EIGEN=ON + -DCMAKE_CXX_STANDARD=17 + -DPYBIND11_INTERNALS_VERSION=10000000 + "-DPYBIND11_TEST_OVERRIDE=test_call_policies.cpp;test_gil_scoped.cpp;test_thread.cpp" + ${{ matrix.args }} + + - name: Build (unstable ABI) + run: cmake --build build3 -j 2 + + - name: Python tests (unstable ABI) + run: cmake --build build3 --target pytest + - name: Interface test run: cmake --build build2 --target test_cmake_build diff --git a/CMakeLists.txt b/CMakeLists.txt index bb01a861ae..25cfcec2f6 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -89,6 +89,9 @@ endif() option(PYBIND11_INSTALL "Install pybind11 header files?" ${PYBIND11_MASTER_PROJECT}) option(PYBIND11_TEST "Build pybind11 test suite?" ${PYBIND11_MASTER_PROJECT}) option(PYBIND11_NOPYTHON "Disable search for Python" OFF) +set(PYBIND11_INTERNALS_VERSION + "" + CACHE STRING "Override the ABI version, may be used to enable the unstable ABI.") cmake_dependent_option( USE_PYTHON_INCLUDE_DIR @@ -183,6 +186,10 @@ if(NOT TARGET pybind11_headers) target_compile_features(pybind11_headers INTERFACE cxx_inheriting_constructors cxx_user_literals cxx_right_angle_brackets) + if(NOT "${PYBIND11_INTERNALS_VERSION}" STREQUAL "") + target_compile_definitions( + pybind11_headers INTERFACE "PYBIND11_INTERNALS_VERSION=${PYBIND11_INTERNALS_VERSION}") + endif() else() # It is invalid to install a target twice, too. set(PYBIND11_INSTALL OFF) diff --git a/include/pybind11/detail/internals.h b/include/pybind11/detail/internals.h index 39c28e7732..7c2e499781 100644 --- a/include/pybind11/detail/internals.h +++ b/include/pybind11/detail/internals.h @@ -11,6 +11,24 @@ #include "../pytypes.h" +/// Tracks the `internals` and `type_info` ABI version independent of the main library version. +/// +/// Some portions of the code use an ABI that is conditional depending on this +/// version number. That allows ABI-breaking changes to be "pre-implemented". +/// Once the default version number is incremented, the conditional logic that +/// no longer applies can be removed. Additionally, users that need not +/// maintain ABI compatibility can increase the version number in order to take +/// advantage of any functionality/efficiency improvements that depend on the +/// newer ABI. +/// +/// WARNING: If you choose to manually increase the ABI version, note that +/// pybind11 may not be tested as thoroughly with a non-default ABI version, and +/// further ABI-incompatible changes may be made before the ABI is officially +/// changed to the new version. +#ifndef PYBIND11_INTERNALS_VERSION +# define PYBIND11_INTERNALS_VERSION 4 +#endif + PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE) using ExceptionTranslator = void (*)(std::exception_ptr); @@ -25,30 +43,58 @@ inline PyObject *make_object_base_type(PyTypeObject *metaclass); // The old Python Thread Local Storage (TLS) API is deprecated in Python 3.7 in favor of the new // Thread Specific Storage (TSS) API. #if PY_VERSION_HEX >= 0x03070000 -# define PYBIND11_TLS_KEY_INIT(var) Py_tss_t *var = nullptr -# define PYBIND11_TLS_GET_VALUE(key) PyThread_tss_get((key)) -# define PYBIND11_TLS_REPLACE_VALUE(key, value) PyThread_tss_set((key), (value)) -# define PYBIND11_TLS_DELETE_VALUE(key) PyThread_tss_set((key), nullptr) -# define PYBIND11_TLS_FREE(key) PyThread_tss_free(key) +// Avoid unnecessary allocation of `Py_tss_t`, since we cannot use +// `Py_LIMITED_API` anyway. +# if PYBIND11_INTERNALS_VERSION > 4 +# define PYBIND11_TLS_KEY_REF Py_tss_t & +# ifdef __GNUC__ +// Clang on macOS warns due to `Py_tss_NEEDS_INIT` not specifying an initializer +// for every field. +# define PYBIND11_TLS_KEY_INIT(var) \ + _Pragma("GCC diagnostic push") /**/ \ + _Pragma("GCC diagnostic ignored \"-Wmissing-field-initializers\"") /**/ \ + Py_tss_t var \ + = Py_tss_NEEDS_INIT; \ + _Pragma("GCC diagnostic pop") +# else +# define PYBIND11_TLS_KEY_INIT(var) Py_tss_t var = Py_tss_NEEDS_INIT; +# endif +# define PYBIND11_TLS_KEY_CREATE(var) (PyThread_tss_create(&(var)) == 0) +# define PYBIND11_TLS_GET_VALUE(key) PyThread_tss_get(&(key)) +# define PYBIND11_TLS_REPLACE_VALUE(key, value) PyThread_tss_set(&(key), (value)) +# define PYBIND11_TLS_DELETE_VALUE(key) PyThread_tss_set(&(key), nullptr) +# define PYBIND11_TLS_FREE(key) PyThread_tss_delete(&(key)) +# else +# define PYBIND11_TLS_KEY_REF Py_tss_t * +# define PYBIND11_TLS_KEY_INIT(var) Py_tss_t *var = nullptr; +# define PYBIND11_TLS_KEY_CREATE(var) \ + (((var) = PyThread_tss_alloc()) != nullptr && (PyThread_tss_create((var)) == 0)) +# define PYBIND11_TLS_GET_VALUE(key) PyThread_tss_get((key)) +# define PYBIND11_TLS_REPLACE_VALUE(key, value) PyThread_tss_set((key), (value)) +# define PYBIND11_TLS_DELETE_VALUE(key) PyThread_tss_set((key), nullptr) +# define PYBIND11_TLS_FREE(key) PyThread_tss_free(key) +# endif #else - // Usually an int but a long on Cygwin64 with Python 3.x -# define PYBIND11_TLS_KEY_INIT(var) decltype(PyThread_create_key()) var = 0 +// Usually an int but a long on Cygwin64 with Python 3.x +# define PYBIND11_TLS_KEY_REF decltype(PyThread_create_key()) +# define PYBIND11_TLS_KEY_INIT(var) PYBIND11_TLS_KEY_REF var = 0; +# define PYBIND11_TLS_KEY_CREATE(var) (((var) = PyThread_create_key()) != -1) # define PYBIND11_TLS_GET_VALUE(key) PyThread_get_key_value((key)) -# if PY_MAJOR_VERSION < 3 -# define PYBIND11_TLS_DELETE_VALUE(key) \ - PyThread_delete_key_value(key) -# define PYBIND11_TLS_REPLACE_VALUE(key, value) \ - do { \ - PyThread_delete_key_value((key)); \ - PyThread_set_key_value((key), (value)); \ - } while (false) +# if PY_MAJOR_VERSION < 3 || defined(PYPY_VERSION) +// On CPython < 3.4 and on PyPy, `PyThread_set_key_value` strangely does not set +// the value if it has already been set. Instead, it must first be deleted and +// then set again. +# define PYBIND11_TLS_DELETE_VALUE(key) PyThread_delete_key_value(key) +# define PYBIND11_TLS_REPLACE_VALUE(key, value) \ + do { \ + PyThread_delete_key_value((key)); \ + PyThread_set_key_value((key), (value)); \ + } while (false) # else -# define PYBIND11_TLS_DELETE_VALUE(key) \ - PyThread_set_key_value((key), nullptr) -# define PYBIND11_TLS_REPLACE_VALUE(key, value) \ - PyThread_set_key_value((key), (value)) +# define PYBIND11_TLS_DELETE_VALUE(key) PyThread_set_key_value((key), nullptr) +# define PYBIND11_TLS_REPLACE_VALUE(key, value) PyThread_set_key_value((key), (value)) # endif -# define PYBIND11_TLS_FREE(key) (void)key +# define PYBIND11_TLS_FREE(key) (void) key #endif // Python loads modules by default with dlopen with the RTLD_LOCAL flag; under libc++ and possibly @@ -106,22 +152,31 @@ struct internals { std::unordered_map> patients; std::forward_list registered_exception_translators; std::unordered_map shared_data; // Custom data to be shared across extensions +#if PYBIND11_INTERNALS_VERSION == 4 std::vector unused_loader_patient_stack_remove_at_v5; +#endif std::forward_list static_strings; // Stores the std::strings backing detail::c_str() PyTypeObject *static_property_type; PyTypeObject *default_metaclass; PyObject *instance_base; #if defined(WITH_THREAD) - PYBIND11_TLS_KEY_INIT(tstate); + PYBIND11_TLS_KEY_INIT(tstate) +# if PYBIND11_INTERNALS_VERSION > 4 + PYBIND11_TLS_KEY_INIT(loader_life_support_tls_key) +# endif // PYBIND11_INTERNALS_VERSION > 4 PyInterpreterState *istate = nullptr; ~internals() { +# if PYBIND11_INTERNALS_VERSION > 4 + PYBIND11_TLS_FREE(loader_life_support_tls_key); +# endif // PYBIND11_INTERNALS_VERSION > 4 + // This destructor is called *after* Py_Finalize() in finalize_interpreter(). - // That *SHOULD BE* fine. The following details what happens when PyThread_tss_free is called. - // PYBIND11_TLS_FREE is PyThread_tss_free on python 3.7+. On older python, it does nothing. - // PyThread_tss_free calls PyThread_tss_delete and PyMem_RawFree. - // PyThread_tss_delete just calls TlsFree (on Windows) or pthread_key_delete (on *NIX). Neither - // of those have anything to do with CPython internals. - // PyMem_RawFree *requires* that the `tstate` be allocated with the CPython allocator. + // That *SHOULD BE* fine. The following details what happens when PyThread_tss_free is + // called. PYBIND11_TLS_FREE is PyThread_tss_free on python 3.7+. On older python, it does + // nothing. PyThread_tss_free calls PyThread_tss_delete and PyMem_RawFree. + // PyThread_tss_delete just calls TlsFree (on Windows) or pthread_key_delete (on *NIX). + // Neither of those have anything to do with CPython internals. PyMem_RawFree *requires* + // that the `tstate` be allocated with the CPython allocator. PYBIND11_TLS_FREE(tstate); } #endif @@ -153,9 +208,6 @@ struct type_info { bool module_local : 1; }; -/// Tracks the `internals` and `type_info` ABI version independent of the main library version -#define PYBIND11_INTERNALS_VERSION 4 - /// On MSVC, debug and release builds are not ABI-compatible! #if defined(_MSC_VER) && defined(_DEBUG) # define PYBIND11_BUILD_TYPE "_debug" @@ -291,21 +343,21 @@ PYBIND11_NOINLINE internals &get_internals() { internals_ptr = new internals(); #if defined(WITH_THREAD) - #if PY_VERSION_HEX < 0x03090000 - PyEval_InitThreads(); - #endif +# if PY_VERSION_HEX < 0x03090000 + PyEval_InitThreads(); +# endif PyThreadState *tstate = PyThreadState_Get(); - #if PY_VERSION_HEX >= 0x03070000 - internals_ptr->tstate = PyThread_tss_alloc(); - if (!internals_ptr->tstate || (PyThread_tss_create(internals_ptr->tstate) != 0)) - pybind11_fail("get_internals: could not successfully initialize the tstate TSS key!"); - PyThread_tss_set(internals_ptr->tstate, tstate); - #else - internals_ptr->tstate = PyThread_create_key(); - if (internals_ptr->tstate == -1) - pybind11_fail("get_internals: could not successfully initialize the tstate TLS key!"); - PyThread_set_key_value(internals_ptr->tstate, tstate); - #endif + if (!PYBIND11_TLS_KEY_CREATE(internals_ptr->tstate)) { + pybind11_fail("get_internals: could not successfully initialize the tstate TSS key!"); + } + PYBIND11_TLS_REPLACE_VALUE(internals_ptr->tstate, tstate); + +# if PYBIND11_INTERNALS_VERSION > 4 + if (!PYBIND11_TLS_KEY_CREATE(internals_ptr->loader_life_support_tls_key)) { + pybind11_fail("get_internals: could not successfully initialize the " + "loader_life_support TSS key!"); + } +# endif internals_ptr->istate = tstate->interp; #endif builtins[id] = capsule(internals_pp); @@ -317,7 +369,6 @@ PYBIND11_NOINLINE internals &get_internals() { return **internals_pp; } - // the internals struct (above) is shared between all the modules. local_internals are only // for a single module. Any changes made to internals may require an update to // PYBIND11_INTERNALS_VERSION, breaking backwards compatibility. local_internals is, by design, @@ -325,8 +376,41 @@ PYBIND11_NOINLINE internals &get_internals() { // impact any other modules, because the only things accessing the local internals is the // module that contains them. struct local_internals { - type_map registered_types_cpp; - std::forward_list registered_exception_translators; + type_map registered_types_cpp; + std::forward_list registered_exception_translators; +#if defined(WITH_THREAD) && PYBIND11_INTERNALS_VERSION == 4 + + // For ABI compatibility, we can't store the loader_life_support TLS key in + // the `internals` struct directly. Instead, we store it in `shared_data` and + // cache a copy in `local_internals`. If we allocated a separate TLS key for + // each instance of `local_internals`, we could end up allocating hundreds of + // TLS keys if hundreds of different pybind11 modules are loaded (which is a + // plausible number). + PYBIND11_TLS_KEY_INIT(loader_life_support_tls_key) + + // Holds the shared TLS key for the loader_life_support stack. + struct shared_loader_life_support_data { + PYBIND11_TLS_KEY_INIT(loader_life_support_tls_key) + shared_loader_life_support_data() { + if (!PYBIND11_TLS_KEY_CREATE(loader_life_support_tls_key)) { + pybind11_fail("local_internals: could not successfully initialize the " + "loader_life_support TLS key!"); + } + } + // We can't help but leak the TLS key, because Python never unloads extension modules. + }; + + local_internals() { + auto &internals = get_internals(); + // Get or create the `loader_life_support_stack_key`. + auto &ptr = internals.shared_data["_life_support"]; + if (!ptr) { + ptr = new shared_loader_life_support_data; + } + loader_life_support_tls_key + = static_cast(ptr)->loader_life_support_tls_key; + } +#endif // defined(WITH_THREAD) && PYBIND11_INTERNALS_VERSION == 4 }; /// Works like `get_internals`, but for things which are locally registered. diff --git a/include/pybind11/detail/type_caster_base.h b/include/pybind11/detail/type_caster_base.h index fa8433cfec..f804d9d10c 100644 --- a/include/pybind11/detail/type_caster_base.h +++ b/include/pybind11/detail/type_caster_base.h @@ -35,30 +35,43 @@ class loader_life_support { loader_life_support* parent = nullptr; std::unordered_set keep_alive; - static loader_life_support** get_stack_pp() { #if defined(WITH_THREAD) - thread_local static loader_life_support* per_thread_stack = nullptr; - return &per_thread_stack; + // Store stack pointer in thread-local storage. + static PYBIND11_TLS_KEY_REF get_stack_tls_key() { +# if PYBIND11_INTERNALS_VERSION == 4 + return get_local_internals().loader_life_support_tls_key; +# else + return get_internals().loader_life_support_tls_key; +# endif + } + static loader_life_support *get_stack_top() { + return static_cast(PYBIND11_TLS_GET_VALUE(get_stack_tls_key())); + } + static void set_stack_top(loader_life_support *value) { + PYBIND11_TLS_REPLACE_VALUE(get_stack_tls_key(), value); + } #else - static loader_life_support* global_stack = nullptr; - return &global_stack; -#endif + // Use single global variable for stack. + static loader_life_support **get_stack_pp() { + static loader_life_support *global_stack = nullptr; + return global_stack; } + static loader_life_support *get_stack_top() { return *get_stack_pp(); } + static void set_stack_top(loader_life_support *value) { *get_stack_pp() = value; } +#endif public: /// A new patient frame is created when a function is entered loader_life_support() { - loader_life_support** stack = get_stack_pp(); - parent = *stack; - *stack = this; + parent = get_stack_top(); + set_stack_top(this); } /// ... and destroyed after it returns ~loader_life_support() { - loader_life_support** stack = get_stack_pp(); - if (*stack != this) + if (get_stack_top() != this) pybind11_fail("loader_life_support: internal error"); - *stack = parent; + set_stack_top(parent); for (auto* item : keep_alive) Py_DECREF(item); } @@ -66,7 +79,7 @@ class loader_life_support { /// This can only be used inside a pybind11-bound function, either by `argument_loader` /// at argument preparation time or by `py::cast()` at execution time. PYBIND11_NOINLINE static void add_patient(handle h) { - loader_life_support* frame = *get_stack_pp(); + loader_life_support *frame = get_stack_top(); if (!frame) { // NOTE: It would be nice to include the stack frames here, as this indicates // use of pybind11::cast<> outside the normal call framework, finding such diff --git a/include/pybind11/gil.h b/include/pybind11/gil.h index 32f1a8963b..b73aaa3f54 100644 --- a/include/pybind11/gil.h +++ b/include/pybind11/gil.h @@ -50,7 +50,7 @@ PYBIND11_NAMESPACE_END(detail) class gil_scoped_acquire { public: PYBIND11_NOINLINE gil_scoped_acquire() { - auto const &internals = detail::get_internals(); + auto &internals = detail::get_internals(); tstate = (PyThreadState *) PYBIND11_TLS_GET_VALUE(internals.tstate); if (!tstate) { @@ -132,7 +132,7 @@ class gil_scoped_release { // `get_internals()` must be called here unconditionally in order to initialize // `internals.tstate` for subsequent `gil_scoped_acquire` calls. Otherwise, an // initialization race could occur as multiple threads try `gil_scoped_acquire`. - const auto &internals = detail::get_internals(); + auto &internals = detail::get_internals(); tstate = PyEval_SaveThread(); if (disassoc) { auto key = internals.tstate; diff --git a/tests/test_pytypes.py b/tests/test_pytypes.py index 070538cc9f..cc27e60a11 100644 --- a/tests/test_pytypes.py +++ b/tests/test_pytypes.py @@ -467,7 +467,8 @@ def test_issue2361(): assert m.issue2361_str_implicit_copy_none() == "None" with pytest.raises(TypeError) as excinfo: assert m.issue2361_dict_implicit_copy_none() - assert "'NoneType' object is not iterable" in str(excinfo.value) + assert "NoneType" in str(excinfo.value) + assert "iterable" in str(excinfo.value) @pytest.mark.parametrize( From d0f3c51f01271b71e6228a1d002bad347dc4aca1 Mon Sep 17 00:00:00 2001 From: Aaron Gokaslan Date: Mon, 20 Sep 2021 10:42:14 -0400 Subject: [PATCH 174/638] Enable defining custom __new__ (#3265) * Enable defining custom __new__ * See if xfail needed * Qualify auto self * Unconditionally defining PYBIND11_DISABLE_NEW_STYLE_INIT_WARNING. Returning pointer from "__init__" instead of reference. * Use new style __init__ * Simplify __new__ creation * Reviewer suggestions * Match indentation Co-authored-by: Ralf W. Grosse-Kunstleve --- include/pybind11/pybind11.h | 3 ++- tests/test_class.cpp | 15 +++++++++++++++ tests/test_class.py | 8 ++++++++ 3 files changed, 25 insertions(+), 1 deletion(-) diff --git a/include/pybind11/pybind11.h b/include/pybind11/pybind11.h index 07d2d04363..b8f5a6bae1 100644 --- a/include/pybind11/pybind11.h +++ b/include/pybind11/pybind11.h @@ -416,7 +416,8 @@ class cpp_function : public function { detail::function_record *chain = nullptr, *chain_start = rec; if (rec->sibling) { if (PyCFunction_Check(rec->sibling.ptr())) { - auto rec_capsule = reinterpret_borrow(PyCFunction_GET_SELF(rec->sibling.ptr())); + auto *self = PyCFunction_GET_SELF(rec->sibling.ptr()); + capsule rec_capsule = isinstance(self) ? reinterpret_borrow(self) : capsule(self); chain = (detail::function_record *) rec_capsule; /* Never append a method to an overload chain of a parent class; instead, hide the parent's overloads in this case */ diff --git a/tests/test_class.cpp b/tests/test_class.cpp index 246b6c1fea..0b998330d7 100644 --- a/tests/test_class.cpp +++ b/tests/test_class.cpp @@ -47,10 +47,25 @@ TEST_SUBMODULE(class_, m) { } ~NoConstructor() { print_destroyed(this); } }; + struct NoConstructorNew { + NoConstructorNew() = default; + NoConstructorNew(const NoConstructorNew &) = default; + NoConstructorNew(NoConstructorNew &&) = default; + static NoConstructorNew *new_instance() { + auto *ptr = new NoConstructorNew(); + print_created(ptr, "via new_instance"); + return ptr; + } + ~NoConstructorNew() { print_destroyed(this); } + }; py::class_(m, "NoConstructor") .def_static("new_instance", &NoConstructor::new_instance, "Return an instance"); + py::class_(m, "NoConstructorNew") + .def(py::init([](NoConstructorNew *self) { return self; })) // Need a NOOP __init__ + .def_static("__new__", [](const py::object *) { return NoConstructorNew::new_instance(); } ); + // test_inheritance class Pet { public: diff --git a/tests/test_class.py b/tests/test_class.py index 85d4531991..caafe2068d 100644 --- a/tests/test_class.py +++ b/tests/test_class.py @@ -25,6 +25,14 @@ def test_instance(msg): assert cstats.alive() == 0 +def test_instance_new(msg): + instance = m.NoConstructorNew() # .__new__(m.NoConstructor.__class__) + cstats = ConstructorStats.get(m.NoConstructorNew) + assert cstats.alive() == 1 + del instance + assert cstats.alive() == 0 + + def test_type(): assert m.check_type(1) == m.DerivedClass1 with pytest.raises(RuntimeError) as execinfo: From 6e6975e2171cd5fa5efc109a708dc81addae1880 Mon Sep 17 00:00:00 2001 From: Aaron Gokaslan Date: Mon, 20 Sep 2021 16:03:21 -0400 Subject: [PATCH 175/638] Fix test case with __new__ (#3285) --- tests/test_class.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/tests/test_class.cpp b/tests/test_class.cpp index 0b998330d7..52a41a3bc0 100644 --- a/tests/test_class.cpp +++ b/tests/test_class.cpp @@ -63,8 +63,9 @@ TEST_SUBMODULE(class_, m) { .def_static("new_instance", &NoConstructor::new_instance, "Return an instance"); py::class_(m, "NoConstructorNew") - .def(py::init([](NoConstructorNew *self) { return self; })) // Need a NOOP __init__ - .def_static("__new__", [](const py::object *) { return NoConstructorNew::new_instance(); } ); + .def(py::init([](const NoConstructorNew &self) { return self; })) // Need a NOOP __init__ + .def_static("__new__", + [](const py::object &) { return NoConstructorNew::new_instance(); }); // test_inheritance class Pet { From 077a16e9d11bb946babbe8001a1dd42704eae3ec Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 20 Sep 2021 16:18:09 -0400 Subject: [PATCH 176/638] [pre-commit.ci] pre-commit autoupdate (#3286) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit updates: - [github.com/psf/black: 21.8b0 → 21.9b0](https://github.com/psf/black/compare/21.8b0...21.9b0) Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> --- .pre-commit-config.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index fd11cf0cda..b9e37e5fe0 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -42,7 +42,7 @@ repos: # Black, the code formatter, natively supports pre-commit - repo: https://github.com/psf/black - rev: 21.8b0 + rev: 21.9b0 hooks: - id: black # By default, this ignores pyi files, though black supports them From ee0c5ee405e7a532410797687da28a20b89cd62b Mon Sep 17 00:00:00 2001 From: Bruce Merry <1963944+bmerry@users.noreply.github.com> Date: Tue, 21 Sep 2021 19:37:19 +0200 Subject: [PATCH 177/638] Add make_value_iterator (#3271) * Add make_value_iterator This is the counterpart to make_key_iterator, and will allow implementing a `value` method in `bind_map` (although doing so is left for a subsequent PR). I made a few design changes to reduce copy-and-paste boilerplate. Previously detail::iterator_state had a boolean template parameter to indicate whether it was being used for make_iterator or make_key_iterator. I replaced the boolean with a class that determines how to dereference the iterator. This allows for a generic implementation of `__next__`. I also added the ValueType and Extra... parameters to the iterator_state template args, because I think it was a bug that they were missing: if make_iterator is called twice with different values of these, only the first set has effect (because the state class is only registered once). There is still a potential issue in that the *values* of the extra arguments are latched on the first call, but since most policies are empty classes this should be even less common. * Add some remove_cv_t to appease clang-tidy * Make iterator_access and friends take reference For some reason I'd accidentally made it take a const value, which caused some issues with third-party packages. * Another attempt to remove remove_cv_t from iterators Some of the return types were const (non-reference) types because of the pecularities of decltype: `decltype((*it).first)` is the *declared* type of the member of the pair, rather than the type of the expression. So if the reference type of the iterator is `pair &`, then the decltype is `const int`. Wrapping an extra set of parentheses to form `decltype(((*it).first))` would instead give `const int &`. This means that the existing make_key_iterator actually returns by value from `__next__`, rather than by reference. Since for mapping types, keys are always const, this probably hasn't been noticed, but it will affect make_value_iterator if the Python code tries to mutate the returned objects. I've changed things to use double parentheses so that make_iterator, make_key_iterator and make_value_iterator should now all return the reference type of the iterator. I'll still need to add a test for that; for now I'm just checking whether I can keep Clang-Tidy happy. * Add back some NOLINTNEXTLINE to appease Clang-Tidy This is favoured over using remove_cv_t because in some cases a const value return type is deliberate (particularly for Eigen). * Add a unit test for iterator referencing Ensure that make_iterator, make_key_iterator and make_value_iterator return references to the container elements, rather than copies. The test for make_key_iterator fails to compile on master, which gives me confidence that this branch has fixed it. * Make the iterator_access etc operator() const I'm actually a little surprised it compiled at all given that the operator() is called on a temporary, but I don't claim to fully understand all the different value types in C++11. * Attempt to work around compiler bugs https://godbolt.org/ shows an example where ICC gets the wrong result for a decltype used as the default for a template argument, and CI also showed problems with PGI. This is a shot in the dark to see if it fixes things. * Make a test constructor explicit (Clang-Tidy) * Fix unit test on GCC 4.8.5 It seems to require the arguments to the std::pair constructor to be implicitly convertible to the types in the pair, rather than just requiring is_constructible. * Remove DOXYGEN_SHOULD_SKIP_THIS guards Now that a complex decltype expression has been replaced by a simpler nested type, I'm hoping Doxygen will be able to build it without issues. * Add comment to explain iterator_state template params --- docs/reference.rst | 3 + include/pybind11/pybind11.h | 118 ++++++++++++++++++------- tests/test_sequences_and_iterators.cpp | 58 ++++++++++++ tests/test_sequences_and_iterators.py | 29 ++++++ 4 files changed, 176 insertions(+), 32 deletions(-) diff --git a/docs/reference.rst b/docs/reference.rst index a678d41c88..e64a03519d 100644 --- a/docs/reference.rst +++ b/docs/reference.rst @@ -63,6 +63,9 @@ Convenience functions converting to Python types .. doxygenfunction:: make_key_iterator(Iterator, Sentinel, Extra &&...) .. doxygenfunction:: make_key_iterator(Type &, Extra&&...) +.. doxygenfunction:: make_value_iterator(Iterator, Sentinel, Extra &&...) +.. doxygenfunction:: make_value_iterator(Type &, Extra&&...) + .. _extras: Passing extra arguments to ``def`` or ``class_`` diff --git a/include/pybind11/pybind11.h b/include/pybind11/pybind11.h index b8f5a6bae1..ac95b3a33c 100644 --- a/include/pybind11/pybind11.h +++ b/include/pybind11/pybind11.h @@ -1955,25 +1955,52 @@ inline std::pair all_t return res; } -template +/* There are a large number of apparently unused template arguments because + * each combination requires a separate py::class_ registration. + */ +template struct iterator_state { Iterator it; Sentinel end; bool first_or_done; }; -PYBIND11_NAMESPACE_END(detail) +// Note: these helpers take the iterator by non-const reference because some +// iterators in the wild can't be dereferenced when const. +template +struct iterator_access { + using result_type = decltype((*std::declval())); + // NOLINTNEXTLINE(readability-const-return-type) // PR #3263 + result_type operator()(Iterator &it) const { + return *it; + } +}; -/// Makes a python iterator from a first and past-the-end C++ InputIterator. -template +struct iterator_key_access { + using result_type = decltype(((*std::declval()).first)); + result_type operator()(Iterator &it) const { + return (*it).first; + } +}; + +template +struct iterator_value_access { + using result_type = decltype(((*std::declval()).second)); + result_type operator()(Iterator &it) const { + return (*it).second; + } +}; + +template ()), -#endif + typename ValueType, typename... Extra> -iterator make_iterator(Iterator first, Sentinel last, Extra &&... extra) { - using state = detail::iterator_state; +iterator make_iterator_impl(Iterator first, Sentinel last, Extra &&... extra) { + using state = detail::iterator_state; + // TODO: state captures only the types of Extra, not the values if (!detail::get_type_info(typeid(state), false)) { class_(handle(), "iterator", pybind11::module_local()) @@ -1987,7 +2014,7 @@ iterator make_iterator(Iterator first, Sentinel last, Extra &&... extra) { s.first_or_done = true; throw stop_iteration(); } - return *s.it; + return Access()(s.it); // NOLINTNEXTLINE(readability-const-return-type) // PR #3263 }, std::forward(extra)..., Policy); } @@ -1995,35 +2022,55 @@ iterator make_iterator(Iterator first, Sentinel last, Extra &&... extra) { return cast(state{first, last, true}); } -/// Makes an python iterator over the keys (`.first`) of a iterator over pairs from a +PYBIND11_NAMESPACE_END(detail) + +/// Makes a python iterator from a first and past-the-end C++ InputIterator. +template ::result_type, + typename... Extra> +iterator make_iterator(Iterator first, Sentinel last, Extra &&... extra) { + return detail::make_iterator_impl< + detail::iterator_access, + Policy, + Iterator, + Sentinel, + ValueType, + Extra...>(first, last, std::forward(extra)...); +} + +/// Makes a python iterator over the keys (`.first`) of a iterator over pairs from a /// first and past-the-end InputIterator. template ()).first), -#endif + typename KeyType = typename detail::iterator_key_access::result_type, typename... Extra> iterator make_key_iterator(Iterator first, Sentinel last, Extra &&...extra) { - using state = detail::iterator_state; - - if (!detail::get_type_info(typeid(state), false)) { - class_(handle(), "iterator", pybind11::module_local()) - .def("__iter__", [](state &s) -> state& { return s; }) - .def("__next__", [](state &s) -> detail::remove_cv_t { - if (!s.first_or_done) - ++s.it; - else - s.first_or_done = false; - if (s.it == s.end) { - s.first_or_done = true; - throw stop_iteration(); - } - return (*s.it).first; - }, std::forward(extra)..., Policy); - } + return detail::make_iterator_impl< + detail::iterator_key_access, + Policy, + Iterator, + Sentinel, + KeyType, + Extra...>(first, last, std::forward(extra)...); +} - return cast(state{first, last, true}); +/// Makes a python iterator over the values (`.second`) of a iterator over pairs from a +/// first and past-the-end InputIterator. +template ::result_type, + typename... Extra> +iterator make_value_iterator(Iterator first, Sentinel last, Extra &&...extra) { + return detail::make_iterator_impl< + detail::iterator_value_access, + Policy, Iterator, + Sentinel, + ValueType, + Extra...>(first, last, std::forward(extra)...); } /// Makes an iterator over values of an stl container or other container supporting @@ -2040,6 +2087,13 @@ template (std::begin(value), std::end(value), extra...); } +/// Makes an iterator over the values (`.second`) of a stl map-like container supporting +/// `std::begin()`/`std::end()` +template iterator make_value_iterator(Type &value, Extra&&... extra) { + return make_value_iterator(std::begin(value), std::end(value), extra...); +} + template void implicitly_convertible() { struct set_flag { bool &flag; diff --git a/tests/test_sequences_and_iterators.cpp b/tests/test_sequences_and_iterators.cpp index b07fd197a9..66d647262e 100644 --- a/tests/test_sequences_and_iterators.cpp +++ b/tests/test_sequences_and_iterators.cpp @@ -15,6 +15,7 @@ #include #include +#include template class NonZeroIterator { @@ -32,6 +33,29 @@ bool operator==(const NonZeroIterator>& it, const NonZeroSentine return !(*it).first || !(*it).second; } +class NonCopyableInt { +public: + explicit NonCopyableInt(int value) : value_(value) {} + NonCopyableInt(const NonCopyableInt &) = delete; + NonCopyableInt(NonCopyableInt &&other) noexcept : value_(other.value_) { + other.value_ = -1; // detect when an unwanted move occurs + } + NonCopyableInt &operator=(const NonCopyableInt &) = delete; + NonCopyableInt &operator=(NonCopyableInt &&other) noexcept { + value_ = other.value_; + other.value_ = -1; // detect when an unwanted move occurs + return *this; + } + int get() const { return value_; } + void set(int value) { value_ = value; } + ~NonCopyableInt() = default; +private: + int value_; +}; +using NonCopyableIntPair = std::pair; +PYBIND11_MAKE_OPAQUE(std::vector); +PYBIND11_MAKE_OPAQUE(std::vector); + template py::list test_random_access_iterator(PythonType x) { if (x.size() < 5) @@ -271,6 +295,10 @@ TEST_SUBMODULE(sequences_and_iterators, m) { .def( "items", [](const StringMap &map) { return py::make_iterator(map.begin(), map.end()); }, + py::keep_alive<0, 1>()) + .def( + "values", + [](const StringMap &map) { return py::make_value_iterator(map.begin(), map.end()); }, py::keep_alive<0, 1>()); // test_generalized_iterators @@ -289,8 +317,38 @@ TEST_SUBMODULE(sequences_and_iterators, m) { .def("nonzero_keys", [](const IntPairs& s) { return py::make_key_iterator(NonZeroIterator>(s.begin()), NonZeroSentinel()); }, py::keep_alive<0, 1>()) + .def("nonzero_values", [](const IntPairs& s) { + return py::make_value_iterator(NonZeroIterator>(s.begin()), NonZeroSentinel()); + }, py::keep_alive<0, 1>()) ; + // test_iterater_referencing + py::class_(m, "NonCopyableInt") + .def(py::init()) + .def("set", &NonCopyableInt::set) + .def("__int__", &NonCopyableInt::get) + ; + py::class_>(m, "VectorNonCopyableInt") + .def(py::init<>()) + .def("append", [](std::vector &vec, int value) { + vec.emplace_back(value); + }) + .def("__iter__", [](std::vector &vec) { + return py::make_iterator(vec.begin(), vec.end()); + }) + ; + py::class_>(m, "VectorNonCopyableIntPair") + .def(py::init<>()) + .def("append", [](std::vector &vec, const std::pair &value) { + vec.emplace_back(NonCopyableInt(value.first), NonCopyableInt(value.second)); + }) + .def("keys", [](std::vector &vec) { + return py::make_key_iterator(vec.begin(), vec.end()); + }) + .def("values", [](std::vector &vec) { + return py::make_value_iterator(vec.begin(), vec.end()); + }) + ; #if 0 // Obsolete: special data structure for exposing custom iterator types to python diff --git a/tests/test_sequences_and_iterators.py b/tests/test_sequences_and_iterators.py index a868c542c2..2c73eff292 100644 --- a/tests/test_sequences_and_iterators.py +++ b/tests/test_sequences_and_iterators.py @@ -25,6 +25,10 @@ def test_generalized_iterators(): assert list(m.IntPairs([(1, 2), (2, 0), (0, 3), (4, 5)]).nonzero_keys()) == [1] assert list(m.IntPairs([(0, 3), (1, 2), (3, 4)]).nonzero_keys()) == [] + assert list(m.IntPairs([(1, 2), (3, 4), (0, 5)]).nonzero_values()) == [2, 4] + assert list(m.IntPairs([(1, 2), (2, 0), (0, 3), (4, 5)]).nonzero_values()) == [2] + assert list(m.IntPairs([(0, 3), (1, 2), (3, 4)]).nonzero_values()) == [] + # __next__ must continue to raise StopIteration it = m.IntPairs([(0, 0)]).nonzero() for _ in range(3): @@ -37,6 +41,30 @@ def test_generalized_iterators(): next(it) +def test_iterator_referencing(): + """Test that iterators reference rather than copy their referents.""" + vec = m.VectorNonCopyableInt() + vec.append(3) + vec.append(5) + assert [int(x) for x in vec] == [3, 5] + # Increment everything to make sure the referents can be mutated + for x in vec: + x.set(int(x) + 1) + assert [int(x) for x in vec] == [4, 6] + + vec = m.VectorNonCopyableIntPair() + vec.append([3, 4]) + vec.append([5, 7]) + assert [int(x) for x in vec.keys()] == [3, 5] + assert [int(x) for x in vec.values()] == [4, 7] + for x in vec.keys(): + x.set(int(x) + 1) + for x in vec.values(): + x.set(int(x) + 10) + assert [int(x) for x in vec.keys()] == [4, 6] + assert [int(x) for x in vec.values()] == [14, 17] + + def test_sliceable(): sliceable = m.Sliceable(100) assert sliceable[::] == (0, 100, 1) @@ -140,6 +168,7 @@ def test_map_iterator(): assert sm[k] == expected[k] for k, v in sm.items(): assert v == expected[k] + assert list(sm.values()) == [expected[k] for k in sm] it = iter(m.StringMap({})) for _ in range(3): # __next__ must continue to raise StopIteration From 0fb981b21966339b811bc770b11f71eddd918f13 Mon Sep 17 00:00:00 2001 From: Aaron Gokaslan Date: Wed, 22 Sep 2021 15:38:50 -0400 Subject: [PATCH 178/638] Add blacken-docs and pycln pre-commit hooks (#3292) * Apply blacken-docs and fix language-hints * Add blacken-docs pre-commit hook * Add pycln pre-commit hook * Enable a few builtin hooks * Black no longer ignores pyi files --- .pre-commit-config.yaml | 30 ++++++++++++++++++++--------- docs/advanced/cast/custom.rst | 2 ++ docs/advanced/cast/eigen.rst | 4 ++-- docs/advanced/cast/strings.rst | 32 +++++++++++++++---------------- docs/advanced/classes.rst | 8 +++++--- docs/advanced/embedding.rst | 1 + docs/advanced/functions.rst | 7 ++++--- docs/advanced/pycpp/numpy.rst | 6 +++--- docs/advanced/pycpp/object.rst | 1 + docs/advanced/pycpp/utilities.rst | 2 +- docs/advanced/smart_ptrs.rst | 1 + docs/benchmark.py | 1 - docs/changelog.rst | 1 + docs/classes.rst | 22 ++++++++++----------- docs/compiling.rst | 11 ++--------- docs/conf.py | 1 - docs/faq.rst | 2 +- 17 files changed, 72 insertions(+), 60 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index b9e37e5fe0..4245641ba3 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -19,8 +19,10 @@ repos: hooks: - id: check-added-large-files - id: check-case-conflict + - id: check-docstring-first - id: check-merge-conflict - id: check-symlinks + - id: check-toml - id: check-yaml - id: debug-statements - id: end-of-file-fixer @@ -42,12 +44,16 @@ repos: # Black, the code formatter, natively supports pre-commit - repo: https://github.com/psf/black - rev: 21.9b0 + rev: 21.9b0 # Keep in sync with blacken-docs hooks: - id: black - # By default, this ignores pyi files, though black supports them - types: [text] - files: \.pyi?$ + +- repo: https://github.com/asottile/blacken-docs + rev: v1.11.0 + hooks: + - id: blacken-docs + additional_dependencies: + - black==21.9b0 # keep in sync with black hook # Changes tabs to spaces - repo: https://github.com/Lucas-C/pre-commit-hooks @@ -55,6 +61,12 @@ repos: hooks: - id: remove-tabs +# Autoremoves unused imports +- repo: https://github.com/hadialqattan/pycln + rev: v1.0.3 + hooks: + - id: pycln + # Flake8 also supports pre-commit natively (same author) - repo: https://github.com/PyCQA/flake8 rev: 3.9.2 @@ -86,7 +98,7 @@ repos: # Checks the manifest for missing files (native support) - repo: https://github.com/mgedmin/check-manifest - rev: "0.46" + rev: "0.47" hooks: - id: check-manifest # This is a slow hook, so only run this if --hook-stage manual is passed @@ -100,10 +112,10 @@ repos: exclude: ".supp$" args: ["-L", "nd,ot,thist"] -- repo: https://github.com/shellcheck-py/shellcheck-py - rev: v0.7.2.1 - hooks: - - id: shellcheck +- repo: https://github.com/shellcheck-py/shellcheck-py + rev: v0.7.2.1 + hooks: + - id: shellcheck # The original pybind11 checks for a few C++ style items - repo: local diff --git a/docs/advanced/cast/custom.rst b/docs/advanced/cast/custom.rst index a779444c24..19b9353476 100644 --- a/docs/advanced/cast/custom.rst +++ b/docs/advanced/cast/custom.rst @@ -26,7 +26,9 @@ The following Python snippet demonstrates the intended usage from the Python sid def __int__(self): return 123 + from example import print + print(A()) To register the necessary conversion routines, it is necessary to add an diff --git a/docs/advanced/cast/eigen.rst b/docs/advanced/cast/eigen.rst index e01472d5ae..80f1013430 100644 --- a/docs/advanced/cast/eigen.rst +++ b/docs/advanced/cast/eigen.rst @@ -112,7 +112,7 @@ example: .. code-block:: python a = MyClass() - m = a.get_matrix() # flags.writeable = True, flags.owndata = False + m = a.get_matrix() # flags.writeable = True, flags.owndata = False v = a.view_matrix() # flags.writeable = False, flags.owndata = False c = a.copy_matrix() # flags.writeable = True, flags.owndata = True # m[5,6] and v[5,6] refer to the same element, c[5,6] does not. @@ -203,7 +203,7 @@ adding the ``order='F'`` option when creating an array: .. code-block:: python - myarray = np.array(source, order='F') + myarray = np.array(source, order="F") Such an object will be passable to a bound function accepting an ``Eigen::Ref`` (or similar column-major Eigen type). diff --git a/docs/advanced/cast/strings.rst b/docs/advanced/cast/strings.rst index e25701ecab..cfd7e7b7a5 100644 --- a/docs/advanced/cast/strings.rst +++ b/docs/advanced/cast/strings.rst @@ -36,13 +36,13 @@ everywhere `_. } ); -.. code-block:: python +.. code-block:: pycon - >>> utf8_test('🎂') + >>> utf8_test("🎂") utf-8 is icing on the cake. 🎂 - >>> utf8_charptr('🍕') + >>> utf8_charptr("🍕") My favorite food is 🍕 @@ -80,7 +80,7 @@ raise a ``UnicodeDecodeError``. } ); -.. code-block:: python +.. code-block:: pycon >>> isinstance(example.std_string_return(), str) True @@ -114,7 +114,7 @@ conversion has the same overhead as implicit conversion. } ); -.. code-block:: python +.. code-block:: pycon >>> str_output() 'Send your résumé to Alice in HR' @@ -143,7 +143,7 @@ returned to Python as ``bytes``, then one can return the data as a } ); -.. code-block:: python +.. code-block:: pycon >>> example.return_bytes() b'\xba\xd0\xba\xd0' @@ -160,7 +160,7 @@ encoding, but cannot convert ``std::string`` back to ``bytes`` implicitly. } ); -.. code-block:: python +.. code-block:: pycon >>> isinstance(example.asymmetry(b"have some bytes"), str) True @@ -229,16 +229,16 @@ character. m.def("pass_char", [](char c) { return c; }); m.def("pass_wchar", [](wchar_t w) { return w; }); -.. code-block:: python +.. code-block:: pycon - >>> example.pass_char('A') + >>> example.pass_char("A") 'A' While C++ will cast integers to character types (``char c = 0x65;``), pybind11 does not convert Python integers to characters implicitly. The Python function ``chr()`` can be used to convert integers to characters. -.. code-block:: python +.. code-block:: pycon >>> example.pass_char(0x65) TypeError @@ -259,17 +259,17 @@ a combining acute accent). The combining character will be lost if the two-character sequence is passed as an argument, even though it renders as a single grapheme. -.. code-block:: python +.. code-block:: pycon - >>> example.pass_wchar('é') + >>> example.pass_wchar("é") 'é' - >>> combining_e_acute = 'e' + '\u0301' + >>> combining_e_acute = "e" + "\u0301" >>> combining_e_acute 'é' - >>> combining_e_acute == 'é' + >>> combining_e_acute == "é" False >>> example.pass_wchar(combining_e_acute) @@ -278,9 +278,9 @@ single grapheme. Normalizing combining characters before passing the character literal to C++ may resolve *some* of these issues: -.. code-block:: python +.. code-block:: pycon - >>> example.pass_wchar(unicodedata.normalize('NFC', combining_e_acute)) + >>> example.pass_wchar(unicodedata.normalize("NFC", combining_e_acute)) 'é' In some languages (Thai for example), there are `graphemes that cannot be diff --git a/docs/advanced/classes.rst b/docs/advanced/classes.rst index 9d1b1f0d9b..7f8fcdf4b7 100644 --- a/docs/advanced/classes.rst +++ b/docs/advanced/classes.rst @@ -136,7 +136,7 @@ a virtual method call. u'woof! woof! woof! ' >>> class Cat(Animal): ... def go(self, n_times): - ... return "meow! " * n_times + ... return "meow! " * n_times ... >>> c = Cat() >>> call_go(c) @@ -159,8 +159,9 @@ Here is an example: class Dachshund(Dog): def __init__(self, name): - Dog.__init__(self) # Without this, a TypeError is raised. + Dog.__init__(self) # Without this, a TypeError is raised. self.name = name + def bark(self): return "yap!" @@ -1153,6 +1154,7 @@ error: >>> class PyFinalChild(IsFinal): ... pass + ... TypeError: type 'IsFinal' is not an acceptable base type .. note:: This attribute is currently ignored on PyPy @@ -1247,7 +1249,7 @@ Accessing the type object You can get the type object from a C++ class that has already been registered using: -.. code-block:: python +.. code-block:: cpp py::type T_py = py::type::of(); diff --git a/docs/advanced/embedding.rst b/docs/advanced/embedding.rst index dfdaad2d7f..a435b8a75a 100644 --- a/docs/advanced/embedding.rst +++ b/docs/advanced/embedding.rst @@ -122,6 +122,7 @@ embedding the interpreter. This makes it easy to import local Python files: """calc.py located in the working directory""" + def add(i, j): return i + j diff --git a/docs/advanced/functions.rst b/docs/advanced/functions.rst index a537cb65ee..1178d07260 100644 --- a/docs/advanced/functions.rst +++ b/docs/advanced/functions.rst @@ -272,7 +272,7 @@ And used in Python as usual: .. code-block:: pycon - >>> print_dict({'foo': 123, 'bar': 'hello'}) + >>> print_dict({"foo": 123, "bar": "hello"}) key=foo, value=123 key=bar, value=hello @@ -377,10 +377,11 @@ argument in a function definition: def f(a, *, b): # a can be positional or via keyword; b must be via keyword pass + f(a=1, b=2) # good f(b=2, a=1) # good - f(1, b=2) # good - f(1, 2) # TypeError: f() takes 1 positional argument but 2 were given + f(1, b=2) # good + f(1, 2) # TypeError: f() takes 1 positional argument but 2 were given Pybind11 provides a ``py::kw_only`` object that allows you to implement the same behaviour by specifying the object between positional and keyword-only diff --git a/docs/advanced/pycpp/numpy.rst b/docs/advanced/pycpp/numpy.rst index cae068e910..30daeefff9 100644 --- a/docs/advanced/pycpp/numpy.rst +++ b/docs/advanced/pycpp/numpy.rst @@ -258,8 +258,8 @@ by the compiler. The result is returned as a NumPy array of type .. code-block:: pycon - >>> x = np.array([[1, 3],[5, 7]]) - >>> y = np.array([[2, 4],[6, 8]]) + >>> x = np.array([[1, 3], [5, 7]]) + >>> y = np.array([[2, 4], [6, 8]]) >>> z = 3 >>> result = vectorized_func(x, y, z) @@ -403,7 +403,7 @@ In Python 2, the syntactic sugar ``...`` is not available, but the singleton .. code-block:: python - a = # a NumPy array + a = ... # a NumPy array b = a[0, ..., 0] The function ``py::ellipsis()`` function can be used to perform the same diff --git a/docs/advanced/pycpp/object.rst b/docs/advanced/pycpp/object.rst index 6fa8d07082..8bffb83e18 100644 --- a/docs/advanced/pycpp/object.rst +++ b/docs/advanced/pycpp/object.rst @@ -173,6 +173,7 @@ Keyword arguments are also supported. In Python, there is the usual call syntax: def f(number, say, to): ... # function code + f(1234, say="hello", to=some_instance) # keyword call in Python In C++, the same call can be made using: diff --git a/docs/advanced/pycpp/utilities.rst b/docs/advanced/pycpp/utilities.rst index 30429e84d7..bf90a62f8c 100644 --- a/docs/advanced/pycpp/utilities.rst +++ b/docs/advanced/pycpp/utilities.rst @@ -66,7 +66,7 @@ extra type, `py::scoped_estream_redirect `, is identica except for defaulting to ``std::cerr`` and ``sys.stderr``; this can be useful with `py::call_guard`, which allows multiple items, but uses the default constructor: -.. code-block:: py +.. code-block:: cpp // Alternative: Call single function using call guard m.def("noisy_func", &call_noisy_function, diff --git a/docs/advanced/smart_ptrs.rst b/docs/advanced/smart_ptrs.rst index da57748ca5..5a22201095 100644 --- a/docs/advanced/smart_ptrs.rst +++ b/docs/advanced/smart_ptrs.rst @@ -77,6 +77,7 @@ segmentation fault). .. code-block:: python from example import Parent + print(Parent().get_child()) The problem is that ``Parent::get_child()`` returns a pointer to an instance of diff --git a/docs/benchmark.py b/docs/benchmark.py index 369470c81f..f190793671 100644 --- a/docs/benchmark.py +++ b/docs/benchmark.py @@ -2,7 +2,6 @@ import datetime as dt import os import random -import time nfns = 4 # Functions per class nargs = 4 # Arguments per function diff --git a/docs/changelog.rst b/docs/changelog.rst index 8b9690caaf..6bdf6a6e45 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -1142,6 +1142,7 @@ v2.2.0 (August 31, 2017) from cpp_module import CppBase1, CppBase2 + class PyDerived(CppBase1, CppBase2): def __init__(self): CppBase1.__init__(self) # C++ bases must be initialized explicitly diff --git a/docs/classes.rst b/docs/classes.rst index f3610ef367..a4463e4179 100644 --- a/docs/classes.rst +++ b/docs/classes.rst @@ -44,12 +44,12 @@ interactive Python session demonstrating this example is shown below: % python >>> import example - >>> p = example.Pet('Molly') + >>> p = example.Pet("Molly") >>> print(p) >>> p.getName() u'Molly' - >>> p.setName('Charly') + >>> p.setName("Charly") >>> p.getName() u'Charly' @@ -122,10 +122,10 @@ This makes it possible to write .. code-block:: pycon - >>> p = example.Pet('Molly') + >>> p = example.Pet("Molly") >>> p.name u'Molly' - >>> p.name = 'Charly' + >>> p.name = "Charly" >>> p.name u'Charly' @@ -174,10 +174,10 @@ Native Python classes can pick up new attributes dynamically: .. code-block:: pycon >>> class Pet: - ... name = 'Molly' + ... name = "Molly" ... >>> p = Pet() - >>> p.name = 'Charly' # overwrite existing + >>> p.name = "Charly" # overwrite existing >>> p.age = 2 # dynamically add a new attribute By default, classes exported from C++ do not support this and the only writable @@ -195,7 +195,7 @@ Trying to set any other attribute results in an error: .. code-block:: pycon >>> p = example.Pet() - >>> p.name = 'Charly' # OK, attribute defined in C++ + >>> p.name = "Charly" # OK, attribute defined in C++ >>> p.age = 2 # fail AttributeError: 'Pet' object has no attribute 'age' @@ -213,7 +213,7 @@ Now everything works as expected: .. code-block:: pycon >>> p = example.Pet() - >>> p.name = 'Charly' # OK, overwrite value in C++ + >>> p.name = "Charly" # OK, overwrite value in C++ >>> p.age = 2 # OK, dynamically add a new attribute >>> p.__dict__ # just like a native Python class {'age': 2} @@ -280,7 +280,7 @@ expose fields and methods of both types: .. code-block:: pycon - >>> p = example.Dog('Molly') + >>> p = example.Dog("Molly") >>> p.name u'Molly' >>> p.bark() @@ -486,7 +486,7 @@ typed enums. .. code-block:: pycon - >>> p = Pet('Lucy', Pet.Cat) + >>> p = Pet("Lucy", Pet.Cat) >>> p.type Kind.Cat >>> int(p.type) @@ -508,7 +508,7 @@ The ``name`` property returns the name of the enum value as a unicode string. .. code-block:: pycon - >>> p = Pet( "Lucy", Pet.Cat ) + >>> p = Pet("Lucy", Pet.Cat) >>> pet_type = p.type >>> pet_type Pet.Cat diff --git a/docs/compiling.rst b/docs/compiling.rst index eaf3270e0e..0738510484 100644 --- a/docs/compiling.rst +++ b/docs/compiling.rst @@ -42,10 +42,7 @@ An example of a ``setup.py`` using pybind11's helpers: ), ] - setup( - ..., - ext_modules=ext_modules - ) + setup(..., ext_modules=ext_modules) If you want to do an automatic search for the highest supported C++ standard, that is supported via a ``build_ext`` command override; it will only affect @@ -64,11 +61,7 @@ that is supported via a ``build_ext`` command override; it will only affect ), ] - setup( - ..., - cmdclass={"build_ext": build_ext}, - ext_modules=ext_modules - ) + setup(..., cmdclass={"build_ext": build_ext}, ext_modules=ext_modules) If you have single-file extension modules that are directly stored in the Python source tree (``foo.cpp`` in the same directory as where a ``foo.py`` diff --git a/docs/conf.py b/docs/conf.py index 458a868866..092e274e09 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -15,7 +15,6 @@ import os import re -import shlex import subprocess import sys from pathlib import Path diff --git a/docs/faq.rst b/docs/faq.rst index d6a048b06f..e2f477b1f5 100644 --- a/docs/faq.rst +++ b/docs/faq.rst @@ -54,7 +54,7 @@ provided by the caller -- in fact, it does nothing at all. .. code-block:: python def increment(i): - i += 1 # nope.. + i += 1 # nope.. pybind11 is also affected by such language-level conventions, which means that binding ``increment`` or ``increment_ptr`` will also create Python functions From b06a6f4f6294dd3550b1a2b6053421d8df145d3c Mon Sep 17 00:00:00 2001 From: Henry Schreiner Date: Wed, 22 Sep 2021 17:41:56 -0400 Subject: [PATCH 179/638] feat: Slice allowing None with py::object or std::optional (#1101) * Adding nullptr slices Using example from #1095 Some fixes from @wjakob's review Stop clang-tidy from complaining New proposal for py::slice constructor Eric's suggested changes: simplify testing; shift def's * chore: drop MSVC pragma (hopefully unneeded) * Apply suggestions from code review --- include/pybind11/detail/common.h | 21 ++++++++++++++++++- include/pybind11/pytypes.h | 25 ++++++++++++++++++++--- include/pybind11/stl.h | 28 ++++++++------------------ tests/test_sequences_and_iterators.cpp | 17 ++++++++++++++++ tests/test_sequences_and_iterators.py | 11 ++++++++++ 5 files changed, 78 insertions(+), 24 deletions(-) diff --git a/include/pybind11/detail/common.h b/include/pybind11/detail/common.h index 8aeb79fb7d..3bd84da576 100644 --- a/include/pybind11/detail/common.h +++ b/include/pybind11/detail/common.h @@ -161,7 +161,26 @@ // https://en.cppreference.com/w/c/chrono/localtime #if defined(__STDC_LIB_EXT1__) && !defined(__STDC_WANT_LIB_EXT1__) -# define __STDC_WANT_LIB_EXT1__ +# define __STDC_WANT_LIB_EXT1__ +#endif + +#ifdef __has_include +// std::optional (but including it in c++14 mode isn't allowed) +# if defined(PYBIND11_CPP17) && __has_include() +# define PYBIND11_HAS_OPTIONAL 1 +# endif +// std::experimental::optional (but not allowed in c++11 mode) +# if defined(PYBIND11_CPP14) && (__has_include() && \ + !__has_include()) +# define PYBIND11_HAS_EXP_OPTIONAL 1 +# endif +// std::variant +# if defined(PYBIND11_CPP17) && __has_include() +# define PYBIND11_HAS_VARIANT 1 +# endif +#elif defined(_MSC_VER) && defined(PYBIND11_CPP17) +# define PYBIND11_HAS_OPTIONAL 1 +# define PYBIND11_HAS_VARIANT 1 #endif #include diff --git a/include/pybind11/pytypes.h b/include/pybind11/pytypes.h index d1d3dcb05e..383663b5ed 100644 --- a/include/pybind11/pytypes.h +++ b/include/pybind11/pytypes.h @@ -14,6 +14,10 @@ #include #include +#if defined(PYBIND11_HAS_OPTIONAL) +# include +#endif + PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE) /* A few forward declarations */ @@ -1345,11 +1349,20 @@ class weakref : public object { class slice : public object { public: PYBIND11_OBJECT_DEFAULT(slice, object, PySlice_Check) - slice(ssize_t start_, ssize_t stop_, ssize_t step_) { - int_ start(start_), stop(stop_), step(step_); + slice(handle start, handle stop, handle step) { m_ptr = PySlice_New(start.ptr(), stop.ptr(), step.ptr()); - if (!m_ptr) pybind11_fail("Could not allocate slice object!"); + if (!m_ptr) + pybind11_fail("Could not allocate slice object!"); } + +#ifdef PYBIND11_HAS_OPTIONAL + slice(std::optional start, std::optional stop, std::optional step) + : slice(index_to_object(start), index_to_object(stop), index_to_object(step)) {} +#else + slice(ssize_t start_, ssize_t stop_, ssize_t step_) + : slice(int_(start_), int_(stop_), int_(step_)) {} +#endif + bool compute(size_t length, size_t *start, size_t *stop, size_t *step, size_t *slicelength) const { return PySlice_GetIndicesEx((PYBIND11_SLICE_OBJECT *) m_ptr, @@ -1364,6 +1377,12 @@ class slice : public object { stop, step, slicelength) == 0; } + +private: + template + static object index_to_object(T index) { + return index ? object(int_(*index)) : object(none()); + } }; class capsule : public object { diff --git a/include/pybind11/stl.h b/include/pybind11/stl.h index 99b49d0e2c..2c017b4fef 100644 --- a/include/pybind11/stl.h +++ b/include/pybind11/stl.h @@ -9,6 +9,7 @@ #pragma once +#include "detail/common.h" #include "pybind11.h" #include #include @@ -19,28 +20,15 @@ #include #include -#ifdef __has_include -// std::optional (but including it in c++14 mode isn't allowed) -# if defined(PYBIND11_CPP17) && __has_include() -# include -# define PYBIND11_HAS_OPTIONAL 1 -# endif -// std::experimental::optional (but not allowed in c++11 mode) -# if defined(PYBIND11_CPP14) && (__has_include() && \ - !__has_include()) -# include -# define PYBIND11_HAS_EXP_OPTIONAL 1 -# endif -// std::variant -# if defined(PYBIND11_CPP17) && __has_include() -# include -# define PYBIND11_HAS_VARIANT 1 -# endif -#elif defined(_MSC_VER) && defined(PYBIND11_CPP17) +// See `detail/common.h` for implementation of these guards. +#if defined(PYBIND11_HAS_OPTIONAL) # include +#elif defined(PYBIND11_HAS_EXP_OPTIONAL) +# include +#endif + +#if defined(PYBIND11_HAS_VARIANT) # include -# define PYBIND11_HAS_OPTIONAL 1 -# define PYBIND11_HAS_VARIANT 1 #endif PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE) diff --git a/tests/test_sequences_and_iterators.cpp b/tests/test_sequences_and_iterators.cpp index 66d647262e..72d96cb441 100644 --- a/tests/test_sequences_and_iterators.cpp +++ b/tests/test_sequences_and_iterators.cpp @@ -17,6 +17,11 @@ #include #include +#ifdef PYBIND11_HAS_OPTIONAL +#include +#endif // PYBIND11_HAS_OPTIONAL + + template class NonZeroIterator { const T* ptr_; @@ -117,6 +122,18 @@ TEST_SUBMODULE(sequences_and_iterators, m) { return std::make_tuple(istart, istop, istep); }); + m.def("make_forward_slice_size_t", []() { return py::slice(0, -1, 1); }); + m.def("make_reversed_slice_object", []() { return py::slice(py::none(), py::none(), py::int_(-1)); }); +#ifdef PYBIND11_HAS_OPTIONAL + m.attr("has_optional") = true; + m.def("make_reversed_slice_size_t_optional_verbose", []() { return py::slice(std::nullopt, std::nullopt, -1); }); + // Warning: The following spelling may still compile if optional<> is not present and give wrong answers. + // Please use with caution. + m.def("make_reversed_slice_size_t_optional", []() { return py::slice({}, {}, -1); }); +#else + m.attr("has_optional") = false; +#endif + // test_sequence class Sequence { public: diff --git a/tests/test_sequences_and_iterators.py b/tests/test_sequences_and_iterators.py index 2c73eff292..79689391a6 100644 --- a/tests/test_sequences_and_iterators.py +++ b/tests/test_sequences_and_iterators.py @@ -16,6 +16,17 @@ def allclose(a_list, b_list, rel_tol=1e-05, abs_tol=0.0): ) +def test_slice_constructors(): + assert m.make_forward_slice_size_t() == slice(0, -1, 1) + assert m.make_reversed_slice_object() == slice(None, None, -1) + + +@pytest.mark.skipif(not m.has_optional, reason="no ") +def test_slice_constructors_explicit_optional(): + assert m.make_reversed_slice_size_t_optional() == slice(None, None, -1) + assert m.make_reversed_slice_size_t_optional_verbose() == slice(None, None, -1) + + def test_generalized_iterators(): assert list(m.IntPairs([(1, 2), (3, 4), (0, 5)]).nonzero()) == [(1, 2), (3, 4)] assert list(m.IntPairs([(1, 2), (2, 0), (0, 3), (4, 5)]).nonzero()) == [(1, 2)] From 1dc9a23caea407db3b7e148b1e9cb962a235b5ed Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Wed, 22 Sep 2021 22:38:04 -0400 Subject: [PATCH 180/638] chore(deps): bump jwlawson/actions-setup-cmake from 1.10 to 1.11 (#3294) Bumps [jwlawson/actions-setup-cmake](https://github.com/jwlawson/actions-setup-cmake) from 1.10 to 1.11. - [Release notes](https://github.com/jwlawson/actions-setup-cmake/releases) - [Commits](https://github.com/jwlawson/actions-setup-cmake/compare/v1.10...v1.11) --- updated-dependencies: - dependency-name: jwlawson/actions-setup-cmake dependency-type: direct:production update-type: version-update:semver-minor ... Signed-off-by: dependabot[bot] Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> --- .github/workflows/ci.yml | 12 ++++++------ .github/workflows/configure.yml | 2 +- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 2348212f4b..0b4968cc83 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -75,7 +75,7 @@ jobs: run: brew install boost - name: Update CMake - uses: jwlawson/actions-setup-cmake@v1.10 + uses: jwlawson/actions-setup-cmake@v1.11 - name: Cache wheels if: runner.os == 'macOS' @@ -211,7 +211,7 @@ jobs: debug: ${{ matrix.python-debug }} - name: Update CMake - uses: jwlawson/actions-setup-cmake@v1.10 + uses: jwlawson/actions-setup-cmake@v1.11 - name: Valgrind cache if: matrix.valgrind @@ -463,7 +463,7 @@ jobs: run: python3 -m pip install --upgrade pip - name: Update CMake - uses: jwlawson/actions-setup-cmake@v1.10 + uses: jwlawson/actions-setup-cmake@v1.11 - name: Configure shell: bash @@ -756,7 +756,7 @@ jobs: architecture: x86 - name: Update CMake - uses: jwlawson/actions-setup-cmake@v1.10 + uses: jwlawson/actions-setup-cmake@v1.11 - name: Prepare MSVC uses: ilammy/msvc-dev-cmd@v1.9.0 @@ -802,7 +802,7 @@ jobs: python-version: ${{ matrix.python }} - name: Update CMake - uses: jwlawson/actions-setup-cmake@v1.10 + uses: jwlawson/actions-setup-cmake@v1.11 - name: Prepare MSVC uses: ilammy/msvc-dev-cmd@v1.9.0 @@ -856,7 +856,7 @@ jobs: python-version: ${{ matrix.python }} - name: Update CMake - uses: jwlawson/actions-setup-cmake@v1.10 + uses: jwlawson/actions-setup-cmake@v1.11 - name: Prepare env run: python -m pip install -r tests/requirements.txt --prefer-binary diff --git a/.github/workflows/configure.yml b/.github/workflows/configure.yml index d37fa3c2cc..d60025e5c5 100644 --- a/.github/workflows/configure.yml +++ b/.github/workflows/configure.yml @@ -55,7 +55,7 @@ jobs: # An action for adding a specific version of CMake: # https://github.com/jwlawson/actions-setup-cmake - name: Setup CMake ${{ matrix.cmake }} - uses: jwlawson/actions-setup-cmake@v1.10 + uses: jwlawson/actions-setup-cmake@v1.11 with: cmake-version: ${{ matrix.cmake }} From 2fa3fcfda5bb1aa1e8efc4d9cf90951e8055375e Mon Sep 17 00:00:00 2001 From: Henry Schreiner Date: Wed, 22 Sep 2021 22:50:29 -0400 Subject: [PATCH 181/638] Revert "Add make_value_iterator (#3271)" This reverts commit ee0c5ee405e7a532410797687da28a20b89cd62b. --- docs/reference.rst | 3 - include/pybind11/pybind11.h | 118 +++++++------------------ tests/test_sequences_and_iterators.cpp | 58 ------------ tests/test_sequences_and_iterators.py | 29 ------ 4 files changed, 32 insertions(+), 176 deletions(-) diff --git a/docs/reference.rst b/docs/reference.rst index e64a03519d..a678d41c88 100644 --- a/docs/reference.rst +++ b/docs/reference.rst @@ -63,9 +63,6 @@ Convenience functions converting to Python types .. doxygenfunction:: make_key_iterator(Iterator, Sentinel, Extra &&...) .. doxygenfunction:: make_key_iterator(Type &, Extra&&...) -.. doxygenfunction:: make_value_iterator(Iterator, Sentinel, Extra &&...) -.. doxygenfunction:: make_value_iterator(Type &, Extra&&...) - .. _extras: Passing extra arguments to ``def`` or ``class_`` diff --git a/include/pybind11/pybind11.h b/include/pybind11/pybind11.h index ac95b3a33c..b8f5a6bae1 100644 --- a/include/pybind11/pybind11.h +++ b/include/pybind11/pybind11.h @@ -1955,52 +1955,25 @@ inline std::pair all_t return res; } -/* There are a large number of apparently unused template arguments because - * each combination requires a separate py::class_ registration. - */ -template +template struct iterator_state { Iterator it; Sentinel end; bool first_or_done; }; -// Note: these helpers take the iterator by non-const reference because some -// iterators in the wild can't be dereferenced when const. -template -struct iterator_access { - using result_type = decltype((*std::declval())); - // NOLINTNEXTLINE(readability-const-return-type) // PR #3263 - result_type operator()(Iterator &it) const { - return *it; - } -}; - -template -struct iterator_key_access { - using result_type = decltype(((*std::declval()).first)); - result_type operator()(Iterator &it) const { - return (*it).first; - } -}; - -template -struct iterator_value_access { - using result_type = decltype(((*std::declval()).second)); - result_type operator()(Iterator &it) const { - return (*it).second; - } -}; +PYBIND11_NAMESPACE_END(detail) -template ()), +#endif typename... Extra> -iterator make_iterator_impl(Iterator first, Sentinel last, Extra &&... extra) { - using state = detail::iterator_state; - // TODO: state captures only the types of Extra, not the values +iterator make_iterator(Iterator first, Sentinel last, Extra &&... extra) { + using state = detail::iterator_state; if (!detail::get_type_info(typeid(state), false)) { class_(handle(), "iterator", pybind11::module_local()) @@ -2014,7 +1987,7 @@ iterator make_iterator_impl(Iterator first, Sentinel last, Extra &&... extra) { s.first_or_done = true; throw stop_iteration(); } - return Access()(s.it); + return *s.it; // NOLINTNEXTLINE(readability-const-return-type) // PR #3263 }, std::forward(extra)..., Policy); } @@ -2022,55 +1995,35 @@ iterator make_iterator_impl(Iterator first, Sentinel last, Extra &&... extra) { return cast(state{first, last, true}); } -PYBIND11_NAMESPACE_END(detail) - -/// Makes a python iterator from a first and past-the-end C++ InputIterator. -template ::result_type, - typename... Extra> -iterator make_iterator(Iterator first, Sentinel last, Extra &&... extra) { - return detail::make_iterator_impl< - detail::iterator_access, - Policy, - Iterator, - Sentinel, - ValueType, - Extra...>(first, last, std::forward(extra)...); -} - -/// Makes a python iterator over the keys (`.first`) of a iterator over pairs from a +/// Makes an python iterator over the keys (`.first`) of a iterator over pairs from a /// first and past-the-end InputIterator. template ::result_type, +#ifndef DOXYGEN_SHOULD_SKIP_THIS // Issue in breathe 4.26.1 + typename KeyType = decltype((*std::declval()).first), +#endif typename... Extra> iterator make_key_iterator(Iterator first, Sentinel last, Extra &&...extra) { - return detail::make_iterator_impl< - detail::iterator_key_access, - Policy, - Iterator, - Sentinel, - KeyType, - Extra...>(first, last, std::forward(extra)...); -} + using state = detail::iterator_state; -/// Makes a python iterator over the values (`.second`) of a iterator over pairs from a -/// first and past-the-end InputIterator. -template ::result_type, - typename... Extra> -iterator make_value_iterator(Iterator first, Sentinel last, Extra &&...extra) { - return detail::make_iterator_impl< - detail::iterator_value_access, - Policy, Iterator, - Sentinel, - ValueType, - Extra...>(first, last, std::forward(extra)...); + if (!detail::get_type_info(typeid(state), false)) { + class_(handle(), "iterator", pybind11::module_local()) + .def("__iter__", [](state &s) -> state& { return s; }) + .def("__next__", [](state &s) -> detail::remove_cv_t { + if (!s.first_or_done) + ++s.it; + else + s.first_or_done = false; + if (s.it == s.end) { + s.first_or_done = true; + throw stop_iteration(); + } + return (*s.it).first; + }, std::forward(extra)..., Policy); + } + + return cast(state{first, last, true}); } /// Makes an iterator over values of an stl container or other container supporting @@ -2087,13 +2040,6 @@ template (std::begin(value), std::end(value), extra...); } -/// Makes an iterator over the values (`.second`) of a stl map-like container supporting -/// `std::begin()`/`std::end()` -template iterator make_value_iterator(Type &value, Extra&&... extra) { - return make_value_iterator(std::begin(value), std::end(value), extra...); -} - template void implicitly_convertible() { struct set_flag { bool &flag; diff --git a/tests/test_sequences_and_iterators.cpp b/tests/test_sequences_and_iterators.cpp index 72d96cb441..f4a78ae998 100644 --- a/tests/test_sequences_and_iterators.cpp +++ b/tests/test_sequences_and_iterators.cpp @@ -15,7 +15,6 @@ #include #include -#include #ifdef PYBIND11_HAS_OPTIONAL #include @@ -38,29 +37,6 @@ bool operator==(const NonZeroIterator>& it, const NonZeroSentine return !(*it).first || !(*it).second; } -class NonCopyableInt { -public: - explicit NonCopyableInt(int value) : value_(value) {} - NonCopyableInt(const NonCopyableInt &) = delete; - NonCopyableInt(NonCopyableInt &&other) noexcept : value_(other.value_) { - other.value_ = -1; // detect when an unwanted move occurs - } - NonCopyableInt &operator=(const NonCopyableInt &) = delete; - NonCopyableInt &operator=(NonCopyableInt &&other) noexcept { - value_ = other.value_; - other.value_ = -1; // detect when an unwanted move occurs - return *this; - } - int get() const { return value_; } - void set(int value) { value_ = value; } - ~NonCopyableInt() = default; -private: - int value_; -}; -using NonCopyableIntPair = std::pair; -PYBIND11_MAKE_OPAQUE(std::vector); -PYBIND11_MAKE_OPAQUE(std::vector); - template py::list test_random_access_iterator(PythonType x) { if (x.size() < 5) @@ -312,10 +288,6 @@ TEST_SUBMODULE(sequences_and_iterators, m) { .def( "items", [](const StringMap &map) { return py::make_iterator(map.begin(), map.end()); }, - py::keep_alive<0, 1>()) - .def( - "values", - [](const StringMap &map) { return py::make_value_iterator(map.begin(), map.end()); }, py::keep_alive<0, 1>()); // test_generalized_iterators @@ -334,38 +306,8 @@ TEST_SUBMODULE(sequences_and_iterators, m) { .def("nonzero_keys", [](const IntPairs& s) { return py::make_key_iterator(NonZeroIterator>(s.begin()), NonZeroSentinel()); }, py::keep_alive<0, 1>()) - .def("nonzero_values", [](const IntPairs& s) { - return py::make_value_iterator(NonZeroIterator>(s.begin()), NonZeroSentinel()); - }, py::keep_alive<0, 1>()) ; - // test_iterater_referencing - py::class_(m, "NonCopyableInt") - .def(py::init()) - .def("set", &NonCopyableInt::set) - .def("__int__", &NonCopyableInt::get) - ; - py::class_>(m, "VectorNonCopyableInt") - .def(py::init<>()) - .def("append", [](std::vector &vec, int value) { - vec.emplace_back(value); - }) - .def("__iter__", [](std::vector &vec) { - return py::make_iterator(vec.begin(), vec.end()); - }) - ; - py::class_>(m, "VectorNonCopyableIntPair") - .def(py::init<>()) - .def("append", [](std::vector &vec, const std::pair &value) { - vec.emplace_back(NonCopyableInt(value.first), NonCopyableInt(value.second)); - }) - .def("keys", [](std::vector &vec) { - return py::make_key_iterator(vec.begin(), vec.end()); - }) - .def("values", [](std::vector &vec) { - return py::make_value_iterator(vec.begin(), vec.end()); - }) - ; #if 0 // Obsolete: special data structure for exposing custom iterator types to python diff --git a/tests/test_sequences_and_iterators.py b/tests/test_sequences_and_iterators.py index 79689391a6..44069fdd15 100644 --- a/tests/test_sequences_and_iterators.py +++ b/tests/test_sequences_and_iterators.py @@ -36,10 +36,6 @@ def test_generalized_iterators(): assert list(m.IntPairs([(1, 2), (2, 0), (0, 3), (4, 5)]).nonzero_keys()) == [1] assert list(m.IntPairs([(0, 3), (1, 2), (3, 4)]).nonzero_keys()) == [] - assert list(m.IntPairs([(1, 2), (3, 4), (0, 5)]).nonzero_values()) == [2, 4] - assert list(m.IntPairs([(1, 2), (2, 0), (0, 3), (4, 5)]).nonzero_values()) == [2] - assert list(m.IntPairs([(0, 3), (1, 2), (3, 4)]).nonzero_values()) == [] - # __next__ must continue to raise StopIteration it = m.IntPairs([(0, 0)]).nonzero() for _ in range(3): @@ -52,30 +48,6 @@ def test_generalized_iterators(): next(it) -def test_iterator_referencing(): - """Test that iterators reference rather than copy their referents.""" - vec = m.VectorNonCopyableInt() - vec.append(3) - vec.append(5) - assert [int(x) for x in vec] == [3, 5] - # Increment everything to make sure the referents can be mutated - for x in vec: - x.set(int(x) + 1) - assert [int(x) for x in vec] == [4, 6] - - vec = m.VectorNonCopyableIntPair() - vec.append([3, 4]) - vec.append([5, 7]) - assert [int(x) for x in vec.keys()] == [3, 5] - assert [int(x) for x in vec.values()] == [4, 7] - for x in vec.keys(): - x.set(int(x) + 1) - for x in vec.values(): - x.set(int(x) + 10) - assert [int(x) for x in vec.keys()] == [4, 6] - assert [int(x) for x in vec.values()] == [14, 17] - - def test_sliceable(): sliceable = m.Sliceable(100) assert sliceable[::] == (0, 100, 1) @@ -179,7 +151,6 @@ def test_map_iterator(): assert sm[k] == expected[k] for k, v in sm.items(): assert v == expected[k] - assert list(sm.values()) == [expected[k] for k in sm] it = iter(m.StringMap({})) for _ in range(3): # __next__ must continue to raise StopIteration From 5f46e47da8ef4c43252a934e9f461296140df1d2 Mon Sep 17 00:00:00 2001 From: Henry Schreiner Date: Thu, 23 Sep 2021 08:01:06 -0400 Subject: [PATCH 182/638] tests: check simple iteration of pairs (#3296) --- tests/test_sequences_and_iterators.cpp | 13 +++++++++++++ tests/test_sequences_and_iterators.py | 9 +++++++++ 2 files changed, 22 insertions(+) diff --git a/tests/test_sequences_and_iterators.cpp b/tests/test_sequences_and_iterators.cpp index f4a78ae998..16f8f5b23f 100644 --- a/tests/test_sequences_and_iterators.cpp +++ b/tests/test_sequences_and_iterators.cpp @@ -295,6 +295,8 @@ TEST_SUBMODULE(sequences_and_iterators, m) { public: explicit IntPairs(std::vector> data) : data_(std::move(data)) {} const std::pair* begin() const { return data_.data(); } + // .end() only required for py::make_iterator(self) overload + const std::pair* end() const { return data_.data() + data_.size(); } private: std::vector> data_; }; @@ -306,6 +308,17 @@ TEST_SUBMODULE(sequences_and_iterators, m) { .def("nonzero_keys", [](const IntPairs& s) { return py::make_key_iterator(NonZeroIterator>(s.begin()), NonZeroSentinel()); }, py::keep_alive<0, 1>()) + .def("simple_iterator", [](IntPairs& self) { + return py::make_iterator(self); + }, py::keep_alive<0, 1>()) + .def("simple_keys", [](IntPairs& self) { + return py::make_key_iterator(self); + }, py::keep_alive<0, 1>()) + + // test iterator with keep_alive (doesn't work so not used at runtime, but tests compile) + .def("make_iterator_keep_alive", [](IntPairs& self) { + return py::make_iterator(self, py::keep_alive<0, 1>()); + }, py::keep_alive<0, 1>()) ; diff --git a/tests/test_sequences_and_iterators.py b/tests/test_sequences_and_iterators.py index 44069fdd15..902f4914c4 100644 --- a/tests/test_sequences_and_iterators.py +++ b/tests/test_sequences_and_iterators.py @@ -48,6 +48,15 @@ def test_generalized_iterators(): next(it) +def test_generalized_iterators_simple(): + assert list(m.IntPairs([(1, 2), (3, 4), (0, 5)]).simple_iterator()) == [ + (1, 2), + (3, 4), + (0, 5), + ] + assert list(m.IntPairs([(1, 2), (3, 4), (0, 5)]).simple_keys()) == [1, 3, 0] + + def test_sliceable(): sliceable = m.Sliceable(100) assert sliceable[::] == (0, 100, 1) From 2a78abffd85904849a1c9f9c6705f2d5a0d27018 Mon Sep 17 00:00:00 2001 From: Jeremy Maitin-Shepard Date: Thu, 23 Sep 2021 10:36:25 -0700 Subject: [PATCH 183/638] Ensure PYBIND11_TLS_REPLACE_VALUE evaluates its arguments only once (#3290) --- include/pybind11/detail/internals.h | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/include/pybind11/detail/internals.h b/include/pybind11/detail/internals.h index 7c2e499781..98d21eb983 100644 --- a/include/pybind11/detail/internals.h +++ b/include/pybind11/detail/internals.h @@ -84,12 +84,13 @@ inline PyObject *make_object_base_type(PyTypeObject *metaclass); // On CPython < 3.4 and on PyPy, `PyThread_set_key_value` strangely does not set // the value if it has already been set. Instead, it must first be deleted and // then set again. +inline void tls_replace_value(PYBIND11_TLS_KEY_REF key, void *value) { + PyThread_delete_key_value(key); + PyThread_set_key_value(key, value); +} # define PYBIND11_TLS_DELETE_VALUE(key) PyThread_delete_key_value(key) # define PYBIND11_TLS_REPLACE_VALUE(key, value) \ - do { \ - PyThread_delete_key_value((key)); \ - PyThread_set_key_value((key), (value)); \ - } while (false) + ::pybind11::detail::tls_replace_value((key), (value)) # else # define PYBIND11_TLS_DELETE_VALUE(key) PyThread_set_key_value((key), nullptr) # define PYBIND11_TLS_REPLACE_VALUE(key, value) PyThread_set_key_value((key), (value)) From 21282e645a638600be9bf44dd535dbae5416005e Mon Sep 17 00:00:00 2001 From: Henry Schreiner Date: Thu, 23 Sep 2021 15:06:07 -0400 Subject: [PATCH 184/638] feat: reapply fixed version of #3271 (#3293) * Add make_value_iterator (#3271) * Add make_value_iterator This is the counterpart to make_key_iterator, and will allow implementing a `value` method in `bind_map` (although doing so is left for a subsequent PR). I made a few design changes to reduce copy-and-paste boilerplate. Previously detail::iterator_state had a boolean template parameter to indicate whether it was being used for make_iterator or make_key_iterator. I replaced the boolean with a class that determines how to dereference the iterator. This allows for a generic implementation of `__next__`. I also added the ValueType and Extra... parameters to the iterator_state template args, because I think it was a bug that they were missing: if make_iterator is called twice with different values of these, only the first set has effect (because the state class is only registered once). There is still a potential issue in that the *values* of the extra arguments are latched on the first call, but since most policies are empty classes this should be even less common. * Add some remove_cv_t to appease clang-tidy * Make iterator_access and friends take reference For some reason I'd accidentally made it take a const value, which caused some issues with third-party packages. * Another attempt to remove remove_cv_t from iterators Some of the return types were const (non-reference) types because of the pecularities of decltype: `decltype((*it).first)` is the *declared* type of the member of the pair, rather than the type of the expression. So if the reference type of the iterator is `pair &`, then the decltype is `const int`. Wrapping an extra set of parentheses to form `decltype(((*it).first))` would instead give `const int &`. This means that the existing make_key_iterator actually returns by value from `__next__`, rather than by reference. Since for mapping types, keys are always const, this probably hasn't been noticed, but it will affect make_value_iterator if the Python code tries to mutate the returned objects. I've changed things to use double parentheses so that make_iterator, make_key_iterator and make_value_iterator should now all return the reference type of the iterator. I'll still need to add a test for that; for now I'm just checking whether I can keep Clang-Tidy happy. * Add back some NOLINTNEXTLINE to appease Clang-Tidy This is favoured over using remove_cv_t because in some cases a const value return type is deliberate (particularly for Eigen). * Add a unit test for iterator referencing Ensure that make_iterator, make_key_iterator and make_value_iterator return references to the container elements, rather than copies. The test for make_key_iterator fails to compile on master, which gives me confidence that this branch has fixed it. * Make the iterator_access etc operator() const I'm actually a little surprised it compiled at all given that the operator() is called on a temporary, but I don't claim to fully understand all the different value types in C++11. * Attempt to work around compiler bugs https://godbolt.org/ shows an example where ICC gets the wrong result for a decltype used as the default for a template argument, and CI also showed problems with PGI. This is a shot in the dark to see if it fixes things. * Make a test constructor explicit (Clang-Tidy) * Fix unit test on GCC 4.8.5 It seems to require the arguments to the std::pair constructor to be implicitly convertible to the types in the pair, rather than just requiring is_constructible. * Remove DOXYGEN_SHOULD_SKIP_THIS guards Now that a complex decltype expression has been replaced by a simpler nested type, I'm hoping Doxygen will be able to build it without issues. * Add comment to explain iterator_state template params * fix: regression in #3271 Co-authored-by: Bruce Merry <1963944+bmerry@users.noreply.github.com> --- docs/reference.rst | 3 + include/pybind11/pybind11.h | 120 ++++++++++++++++++------- tests/test_sequences_and_iterators.cpp | 77 +++++++++++++++- tests/test_sequences_and_iterators.py | 30 +++++++ 4 files changed, 195 insertions(+), 35 deletions(-) diff --git a/docs/reference.rst b/docs/reference.rst index a678d41c88..e64a03519d 100644 --- a/docs/reference.rst +++ b/docs/reference.rst @@ -63,6 +63,9 @@ Convenience functions converting to Python types .. doxygenfunction:: make_key_iterator(Iterator, Sentinel, Extra &&...) .. doxygenfunction:: make_key_iterator(Type &, Extra&&...) +.. doxygenfunction:: make_value_iterator(Iterator, Sentinel, Extra &&...) +.. doxygenfunction:: make_value_iterator(Type &, Extra&&...) + .. _extras: Passing extra arguments to ``def`` or ``class_`` diff --git a/include/pybind11/pybind11.h b/include/pybind11/pybind11.h index b8f5a6bae1..16535f1951 100644 --- a/include/pybind11/pybind11.h +++ b/include/pybind11/pybind11.h @@ -1955,25 +1955,54 @@ inline std::pair all_t return res; } -template +/* There are a large number of apparently unused template arguments because + * each combination requires a separate py::class_ registration. + */ +template struct iterator_state { Iterator it; Sentinel end; bool first_or_done; }; -PYBIND11_NAMESPACE_END(detail) +// Note: these helpers take the iterator by non-const reference because some +// iterators in the wild can't be dereferenced when const. C++ needs the extra parens in decltype +// to enforce an lvalue. The & after Iterator is required for MSVC < 16.9. SFINAE cannot be +// reused for result_type due to bugs in ICC, NVCC, and PGI compilers. See PR #3293. +template ()))> +struct iterator_access { + using result_type = decltype((*std::declval())); + // NOLINTNEXTLINE(readability-const-return-type) // PR #3263 + result_type operator()(Iterator &it) const { + return *it; + } +}; -/// Makes a python iterator from a first and past-the-end C++ InputIterator. -template ()).first)) > +struct iterator_key_access { + using result_type = decltype(((*std::declval()).first)); + result_type operator()(Iterator &it) const { + return (*it).first; + } +}; + +template ()).second))> +struct iterator_value_access { + using result_type = decltype(((*std::declval()).second)); + result_type operator()(Iterator &it) const { + return (*it).second; + } +}; + +template ()), -#endif + typename ValueType, typename... Extra> -iterator make_iterator(Iterator first, Sentinel last, Extra &&... extra) { - using state = detail::iterator_state; +iterator make_iterator_impl(Iterator first, Sentinel last, Extra &&... extra) { + using state = detail::iterator_state; + // TODO: state captures only the types of Extra, not the values if (!detail::get_type_info(typeid(state), false)) { class_(handle(), "iterator", pybind11::module_local()) @@ -1987,7 +2016,7 @@ iterator make_iterator(Iterator first, Sentinel last, Extra &&... extra) { s.first_or_done = true; throw stop_iteration(); } - return *s.it; + return Access()(s.it); // NOLINTNEXTLINE(readability-const-return-type) // PR #3263 }, std::forward(extra)..., Policy); } @@ -1995,35 +2024,55 @@ iterator make_iterator(Iterator first, Sentinel last, Extra &&... extra) { return cast(state{first, last, true}); } -/// Makes an python iterator over the keys (`.first`) of a iterator over pairs from a +PYBIND11_NAMESPACE_END(detail) + +/// Makes a python iterator from a first and past-the-end C++ InputIterator. +template ::result_type, + typename... Extra> +iterator make_iterator(Iterator first, Sentinel last, Extra &&... extra) { + return detail::make_iterator_impl< + detail::iterator_access, + Policy, + Iterator, + Sentinel, + ValueType, + Extra...>(first, last, std::forward(extra)...); +} + +/// Makes a python iterator over the keys (`.first`) of a iterator over pairs from a /// first and past-the-end InputIterator. template ()).first), -#endif + typename KeyType = typename detail::iterator_key_access::result_type, typename... Extra> iterator make_key_iterator(Iterator first, Sentinel last, Extra &&...extra) { - using state = detail::iterator_state; - - if (!detail::get_type_info(typeid(state), false)) { - class_(handle(), "iterator", pybind11::module_local()) - .def("__iter__", [](state &s) -> state& { return s; }) - .def("__next__", [](state &s) -> detail::remove_cv_t { - if (!s.first_or_done) - ++s.it; - else - s.first_or_done = false; - if (s.it == s.end) { - s.first_or_done = true; - throw stop_iteration(); - } - return (*s.it).first; - }, std::forward(extra)..., Policy); - } + return detail::make_iterator_impl< + detail::iterator_key_access, + Policy, + Iterator, + Sentinel, + KeyType, + Extra...>(first, last, std::forward(extra)...); +} - return cast(state{first, last, true}); +/// Makes a python iterator over the values (`.second`) of a iterator over pairs from a +/// first and past-the-end InputIterator. +template ::result_type, + typename... Extra> +iterator make_value_iterator(Iterator first, Sentinel last, Extra &&...extra) { + return detail::make_iterator_impl< + detail::iterator_value_access, + Policy, Iterator, + Sentinel, + ValueType, + Extra...>(first, last, std::forward(extra)...); } /// Makes an iterator over values of an stl container or other container supporting @@ -2040,6 +2089,13 @@ template (std::begin(value), std::end(value), extra...); } +/// Makes an iterator over the values (`.second`) of a stl map-like container supporting +/// `std::begin()`/`std::end()` +template iterator make_value_iterator(Type &value, Extra&&... extra) { + return make_value_iterator(std::begin(value), std::end(value), extra...); +} + template void implicitly_convertible() { struct set_flag { bool &flag; diff --git a/tests/test_sequences_and_iterators.cpp b/tests/test_sequences_and_iterators.cpp index 16f8f5b23f..9de69338bc 100644 --- a/tests/test_sequences_and_iterators.cpp +++ b/tests/test_sequences_and_iterators.cpp @@ -15,6 +15,7 @@ #include #include +#include #ifdef PYBIND11_HAS_OPTIONAL #include @@ -37,6 +38,29 @@ bool operator==(const NonZeroIterator>& it, const NonZeroSentine return !(*it).first || !(*it).second; } +class NonCopyableInt { +public: + explicit NonCopyableInt(int value) : value_(value) {} + NonCopyableInt(const NonCopyableInt &) = delete; + NonCopyableInt(NonCopyableInt &&other) noexcept : value_(other.value_) { + other.value_ = -1; // detect when an unwanted move occurs + } + NonCopyableInt &operator=(const NonCopyableInt &) = delete; + NonCopyableInt &operator=(NonCopyableInt &&other) noexcept { + value_ = other.value_; + other.value_ = -1; // detect when an unwanted move occurs + return *this; + } + int get() const { return value_; } + void set(int value) { value_ = value; } + ~NonCopyableInt() = default; +private: + int value_; +}; +using NonCopyableIntPair = std::pair; +PYBIND11_MAKE_OPAQUE(std::vector); +PYBIND11_MAKE_OPAQUE(std::vector); + template py::list test_random_access_iterator(PythonType x) { if (x.size() < 5) @@ -288,6 +312,10 @@ TEST_SUBMODULE(sequences_and_iterators, m) { .def( "items", [](const StringMap &map) { return py::make_iterator(map.begin(), map.end()); }, + py::keep_alive<0, 1>()) + .def( + "values", + [](const StringMap &map) { return py::make_value_iterator(map.begin(), map.end()); }, py::keep_alive<0, 1>()); // test_generalized_iterators @@ -308,19 +336,62 @@ TEST_SUBMODULE(sequences_and_iterators, m) { .def("nonzero_keys", [](const IntPairs& s) { return py::make_key_iterator(NonZeroIterator>(s.begin()), NonZeroSentinel()); }, py::keep_alive<0, 1>()) + .def("nonzero_values", [](const IntPairs& s) { + return py::make_value_iterator(NonZeroIterator>(s.begin()), NonZeroSentinel()); + }, py::keep_alive<0, 1>()) + + // test single-argument make_iterator .def("simple_iterator", [](IntPairs& self) { return py::make_iterator(self); }, py::keep_alive<0, 1>()) .def("simple_keys", [](IntPairs& self) { return py::make_key_iterator(self); }, py::keep_alive<0, 1>()) + .def("simple_values", [](IntPairs& self) { + return py::make_value_iterator(self); + }, py::keep_alive<0, 1>()) - // test iterator with keep_alive (doesn't work so not used at runtime, but tests compile) - .def("make_iterator_keep_alive", [](IntPairs& self) { - return py::make_iterator(self, py::keep_alive<0, 1>()); + // Test iterator with an Extra (doesn't do anything useful, so not used + // at runtime, but tests need to be able to compile with the correct + // overload. See PR #3293. + .def("_make_iterator_extras", [](IntPairs& self) { + return py::make_iterator(self, py::call_guard()); + }, py::keep_alive<0, 1>()) + .def("_make_key_extras", [](IntPairs& self) { + return py::make_key_iterator(self, py::call_guard()); + }, py::keep_alive<0, 1>()) + .def("_make_value_extras", [](IntPairs& self) { + return py::make_value_iterator(self, py::call_guard()); }, py::keep_alive<0, 1>()) ; + // test_iterater_referencing + py::class_(m, "NonCopyableInt") + .def(py::init()) + .def("set", &NonCopyableInt::set) + .def("__int__", &NonCopyableInt::get) + ; + py::class_>(m, "VectorNonCopyableInt") + .def(py::init<>()) + .def("append", [](std::vector &vec, int value) { + vec.emplace_back(value); + }) + .def("__iter__", [](std::vector &vec) { + return py::make_iterator(vec.begin(), vec.end()); + }) + ; + py::class_>(m, "VectorNonCopyableIntPair") + .def(py::init<>()) + .def("append", [](std::vector &vec, const std::pair &value) { + vec.emplace_back(NonCopyableInt(value.first), NonCopyableInt(value.second)); + }) + .def("keys", [](std::vector &vec) { + return py::make_key_iterator(vec.begin(), vec.end()); + }) + .def("values", [](std::vector &vec) { + return py::make_value_iterator(vec.begin(), vec.end()); + }) + ; #if 0 // Obsolete: special data structure for exposing custom iterator types to python diff --git a/tests/test_sequences_and_iterators.py b/tests/test_sequences_and_iterators.py index 902f4914c4..38e2ab5b75 100644 --- a/tests/test_sequences_and_iterators.py +++ b/tests/test_sequences_and_iterators.py @@ -36,6 +36,10 @@ def test_generalized_iterators(): assert list(m.IntPairs([(1, 2), (2, 0), (0, 3), (4, 5)]).nonzero_keys()) == [1] assert list(m.IntPairs([(0, 3), (1, 2), (3, 4)]).nonzero_keys()) == [] + assert list(m.IntPairs([(1, 2), (3, 4), (0, 5)]).nonzero_values()) == [2, 4] + assert list(m.IntPairs([(1, 2), (2, 0), (0, 3), (4, 5)]).nonzero_values()) == [2] + assert list(m.IntPairs([(0, 3), (1, 2), (3, 4)]).nonzero_values()) == [] + # __next__ must continue to raise StopIteration it = m.IntPairs([(0, 0)]).nonzero() for _ in range(3): @@ -55,6 +59,31 @@ def test_generalized_iterators_simple(): (0, 5), ] assert list(m.IntPairs([(1, 2), (3, 4), (0, 5)]).simple_keys()) == [1, 3, 0] + assert list(m.IntPairs([(1, 2), (3, 4), (0, 5)]).simple_values()) == [2, 4, 5] + + +def test_iterator_referencing(): + """Test that iterators reference rather than copy their referents.""" + vec = m.VectorNonCopyableInt() + vec.append(3) + vec.append(5) + assert [int(x) for x in vec] == [3, 5] + # Increment everything to make sure the referents can be mutated + for x in vec: + x.set(int(x) + 1) + assert [int(x) for x in vec] == [4, 6] + + vec = m.VectorNonCopyableIntPair() + vec.append([3, 4]) + vec.append([5, 7]) + assert [int(x) for x in vec.keys()] == [3, 5] + assert [int(x) for x in vec.values()] == [4, 7] + for x in vec.keys(): + x.set(int(x) + 1) + for x in vec.values(): + x.set(int(x) + 10) + assert [int(x) for x in vec.keys()] == [4, 6] + assert [int(x) for x in vec.values()] == [14, 17] def test_sliceable(): @@ -160,6 +189,7 @@ def test_map_iterator(): assert sm[k] == expected[k] for k, v in sm.items(): assert v == expected[k] + assert list(sm.values()) == [expected[k] for k in sm] it = iter(m.StringMap({})) for _ in range(3): # __next__ must continue to raise StopIteration From 6ad3f874a797ed554b2fea82dd2b798826a83fa8 Mon Sep 17 00:00:00 2001 From: Henry Schreiner Date: Thu, 23 Sep 2021 15:42:16 -0400 Subject: [PATCH 185/638] fix(build): avoid a possible warning about shadowed variables and changing behaviors (#3220) --- tools/pybind11Tools.cmake | 42 +++++++++++++++++---------------------- 1 file changed, 18 insertions(+), 24 deletions(-) diff --git a/tools/pybind11Tools.cmake b/tools/pybind11Tools.cmake index 3231353998..cc5ca21ca3 100644 --- a/tools/pybind11Tools.cmake +++ b/tools/pybind11Tools.cmake @@ -45,31 +45,25 @@ list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}") find_package(PythonLibsNew ${PYBIND11_PYTHON_VERSION} MODULE REQUIRED ${_pybind11_quiet}) list(REMOVE_AT CMAKE_MODULE_PATH -1) +# Makes a normal variable a cached variable +macro(_PYBIND11_PROMOTE_TO_CACHE NAME) + set(_tmp_ptc "${${NAME}}") + # CMake 3.21 complains if a cached variable is shadowed by a normal one + unset(${NAME}) + set(${NAME} + "${_tmp_ptc}" + CACHE INTERNAL "") +endmacro() + # Cache variables so pybind11_add_module can be used in parent projects -set(PYTHON_INCLUDE_DIRS - ${PYTHON_INCLUDE_DIRS} - CACHE INTERNAL "") -set(PYTHON_LIBRARIES - ${PYTHON_LIBRARIES} - CACHE INTERNAL "") -set(PYTHON_MODULE_PREFIX - ${PYTHON_MODULE_PREFIX} - CACHE INTERNAL "") -set(PYTHON_MODULE_EXTENSION - ${PYTHON_MODULE_EXTENSION} - CACHE INTERNAL "") -set(PYTHON_VERSION_MAJOR - ${PYTHON_VERSION_MAJOR} - CACHE INTERNAL "") -set(PYTHON_VERSION_MINOR - ${PYTHON_VERSION_MINOR} - CACHE INTERNAL "") -set(PYTHON_VERSION - ${PYTHON_VERSION} - CACHE INTERNAL "") -set(PYTHON_IS_DEBUG - "${PYTHON_IS_DEBUG}" - CACHE INTERNAL "") +_pybind11_promote_to_cache(PYTHON_INCLUDE_DIRS) +_pybind11_promote_to_cache(PYTHON_LIBRARIES) +_pybind11_promote_to_cache(PYTHON_MODULE_PREFIX) +_pybind11_promote_to_cache(PYTHON_MODULE_EXTENSION) +_pybind11_promote_to_cache(PYTHON_VERSION_MAJOR) +_pybind11_promote_to_cache(PYTHON_VERSION_MINOR) +_pybind11_promote_to_cache(PYTHON_VERSION) +_pybind11_promote_to_cache(PYTHON_IS_DEBUG) if(PYBIND11_MASTER_PROJECT) if(PYTHON_MODULE_EXTENSION MATCHES "pypy") From 409be8336f4c597bcca9d9eaa09df0abc065afff Mon Sep 17 00:00:00 2001 From: Wenzel Jakob Date: Fri, 24 Sep 2021 13:03:57 +0200 Subject: [PATCH 186/638] CMake: react to python version changes The new FindPython-based variant of the CMake scripts caches information about the chosen Python version that can become stale. For example, suppose I configure a simple pybind11-based project as follows ``` cmake -S . -B build -GNinja -DPython_ROOT= ``` which will generate `my_extension.cpython-38-x86_64-linux-gnu.so`. A subsequent change to the python version like ``` cmake -S . -B build -GNinja -DPython_ROOT= ``` does not update all necessary build system information. In particular, the compiled file is still called `my_extension.cpython-38-x86_64-linux-gnu.so`. This commit fixes the problem by detecting changes in `Python_EXECUTABLE` and re-running Python as needed. Note that the previous way of detecting Python does not seem to be affected, it always specifies the right suffix. --- tools/pybind11NewTools.cmake | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/tools/pybind11NewTools.cmake b/tools/pybind11NewTools.cmake index a20803bc74..b93f9145e3 100644 --- a/tools/pybind11NewTools.cmake +++ b/tools/pybind11NewTools.cmake @@ -82,6 +82,15 @@ if(NOT DEFINED ${_Python}_EXECUTABLE) endif() +if(NOT ${_Python}_EXECUTABLE STREQUAL PYTHON_EXECUTABLE_LAST) + # Detect changes to the Python version/binary in subsequent CMake runs, and refresh config if needed + unset(PYTHON_IS_DEBUG CACHE) + unset(PYTHON_MODULE_EXTENSION CACHE) + set(PYTHON_EXECUTABLE_LAST + "${${_Python}_EXECUTABLE}" + CACHE INTERNAL "Python executable during the last CMake run") +endif() + if(NOT DEFINED PYTHON_IS_DEBUG) # Debug check - see https://stackoverflow.com/questions/646518/python-how-to-detect-debug-Interpreter execute_process( From 62c4909cce37829303329da77e427d9d3a907d3a Mon Sep 17 00:00:00 2001 From: Jeremy Maitin-Shepard Date: Fri, 24 Sep 2021 12:08:22 -0700 Subject: [PATCH 187/638] Add `custom_type_setup` attribute (#3287) * Fix `pybind11::object::operator=` to be safe if `*this` is accessible from Python * Add `custom_type_setup` attribute This allows for custom modifications to the PyHeapTypeObject prior to calling `PyType_Ready`. This may be used, for example, to define `tp_traverse` and `tp_clear` functions. --- docs/advanced/classes.rst | 34 ++++++++++++++++++++++ include/pybind11/attr.h | 29 ++++++++++++++++++ include/pybind11/detail/class.h | 6 ++-- include/pybind11/pytypes.h | 5 +++- tests/CMakeLists.txt | 1 + tests/test_custom_type_setup.cpp | 41 ++++++++++++++++++++++++++ tests/test_custom_type_setup.py | 50 ++++++++++++++++++++++++++++++++ 7 files changed, 163 insertions(+), 3 deletions(-) create mode 100644 tests/test_custom_type_setup.cpp create mode 100644 tests/test_custom_type_setup.py diff --git a/docs/advanced/classes.rst b/docs/advanced/classes.rst index 7f8fcdf4b7..5f01a2f11c 100644 --- a/docs/advanced/classes.rst +++ b/docs/advanced/classes.rst @@ -1261,3 +1261,37 @@ object, just like ``type(ob)`` in Python. Other types, like ``py::type::of()``, do not work, see :ref:`type-conversions`. .. versionadded:: 2.6 + +Custom type setup +================= + +For advanced use cases, such as enabling garbage collection support, you may +wish to directly manipulate the `PyHeapTypeObject` corresponding to a +``py::class_`` definition. + +You can do that using ``py::custom_type_setup``: + +.. code-block:: cpp + + struct OwnsPythonObjects { + py::object value = py::none(); + }; + py::class_ cls( + m, "OwnsPythonObjects", py::custom_type_setup([](PyHeapTypeObject *heap_type) { + auto *type = &heap_type->ht_type; + type->tp_flags |= Py_TPFLAGS_HAVE_GC; + type->tp_traverse = [](PyObject *self_base, visitproc visit, void *arg) { + auto &self = py::cast(py::handle(self_base)); + Py_VISIT(self.value.ptr()); + return 0; + }; + type->tp_clear = [](PyObject *self_base) { + auto &self = py::cast(py::handle(self_base)); + self.value = py::none(); + return 0; + }; + })); + cls.def(py::init<>()); + cls.def_readwrite("value", &OwnsPythonObjects::value); + +.. versionadded:: 2.8 diff --git a/include/pybind11/attr.h b/include/pybind11/attr.h index 13f68bbe8f..0dedbc08dd 100644 --- a/include/pybind11/attr.h +++ b/include/pybind11/attr.h @@ -12,6 +12,8 @@ #include "cast.h" +#include + PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE) /// \addtogroup annotations @@ -79,6 +81,23 @@ struct metaclass { explicit metaclass(handle value) : value(value) { } }; +/// Specifies a custom callback with signature `void (PyHeapTypeObject*)` that +/// may be used to customize the Python type. +/// +/// The callback is invoked immediately before `PyType_Ready`. +/// +/// Note: This is an advanced interface, and uses of it may require changes to +/// work with later versions of pybind11. You may wish to consult the +/// implementation of `make_new_python_type` in `detail/classes.h` to understand +/// the context in which the callback will be run. +struct custom_type_setup { + using callback = std::function; + + explicit custom_type_setup(callback value) : value(std::move(value)) {} + + callback value; +}; + /// Annotation that marks a class as local to the module: struct module_local { const bool value; constexpr explicit module_local(bool v = true) : value(v) {} @@ -272,6 +291,9 @@ struct type_record { /// Custom metaclass (optional) handle metaclass; + /// Custom type setup. + custom_type_setup::callback custom_type_setup_callback; + /// Multiple inheritance marker bool multiple_inheritance : 1; @@ -476,6 +498,13 @@ struct process_attribute : process_attribute_default static void init(const dynamic_attr &, type_record *r) { r->dynamic_attr = true; } }; +template <> +struct process_attribute { + static void init(const custom_type_setup &value, type_record *r) { + r->custom_type_setup_callback = value.value; + } +}; + template <> struct process_attribute : process_attribute_default { static void init(const is_final &, type_record *r) { r->is_final = true; } diff --git a/include/pybind11/detail/class.h b/include/pybind11/detail/class.h index f9822c7b35..b9376b4c0b 100644 --- a/include/pybind11/detail/class.h +++ b/include/pybind11/detail/class.h @@ -683,11 +683,13 @@ inline PyObject* make_new_python_type(const type_record &rec) { if (rec.buffer_protocol) enable_buffer_protocol(heap_type); + if (rec.custom_type_setup_callback) + rec.custom_type_setup_callback(heap_type); + if (PyType_Ready(type) < 0) pybind11_fail(std::string(rec.name) + ": PyType_Ready failed (" + error_string() + ")!"); - assert(rec.dynamic_attr ? PyType_HasFeature(type, Py_TPFLAGS_HAVE_GC) - : !PyType_HasFeature(type, Py_TPFLAGS_HAVE_GC)); + assert(!rec.dynamic_attr || PyType_HasFeature(type, Py_TPFLAGS_HAVE_GC)); /* Register type with the parent scope */ if (rec.scope) diff --git a/include/pybind11/pytypes.h b/include/pybind11/pytypes.h index 383663b5ed..f54d5fad61 100644 --- a/include/pybind11/pytypes.h +++ b/include/pybind11/pytypes.h @@ -259,8 +259,11 @@ class object : public handle { object& operator=(const object &other) { other.inc_ref(); - dec_ref(); + // Use temporary variable to ensure `*this` remains valid while + // `Py_XDECREF` executes, in case `*this` is accessible from Python. + handle temp(m_ptr); m_ptr = other.m_ptr; + temp.dec_ref(); return *this; } diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index f014771d54..70303879f3 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -104,6 +104,7 @@ set(PYBIND11_TEST_FILES test_constants_and_functions.cpp test_copy_move.cpp test_custom_type_casters.cpp + test_custom_type_setup.cpp test_docstring_options.cpp test_eigen.cpp test_enum.cpp diff --git a/tests/test_custom_type_setup.cpp b/tests/test_custom_type_setup.cpp new file mode 100644 index 0000000000..42fae05d5d --- /dev/null +++ b/tests/test_custom_type_setup.cpp @@ -0,0 +1,41 @@ +/* + tests/test_custom_type_setup.cpp -- Tests `pybind11::custom_type_setup` + + Copyright (c) Google LLC + + All rights reserved. Use of this source code is governed by a + BSD-style license that can be found in the LICENSE file. +*/ + +#include + +#include "pybind11_tests.h" + +namespace py = pybind11; + +namespace { + +struct OwnsPythonObjects { + py::object value = py::none(); +}; +} // namespace + +TEST_SUBMODULE(custom_type_setup, m) { + py::class_ cls( + m, "OwnsPythonObjects", py::custom_type_setup([](PyHeapTypeObject *heap_type) { + auto *type = &heap_type->ht_type; + type->tp_flags |= Py_TPFLAGS_HAVE_GC; + type->tp_traverse = [](PyObject *self_base, visitproc visit, void *arg) { + auto &self = py::cast(py::handle(self_base)); + Py_VISIT(self.value.ptr()); + return 0; + }; + type->tp_clear = [](PyObject *self_base) { + auto &self = py::cast(py::handle(self_base)); + self.value = py::none(); + return 0; + }; + })); + cls.def(py::init<>()); + cls.def_readwrite("value", &OwnsPythonObjects::value); +} diff --git a/tests/test_custom_type_setup.py b/tests/test_custom_type_setup.py new file mode 100644 index 0000000000..ef96f08141 --- /dev/null +++ b/tests/test_custom_type_setup.py @@ -0,0 +1,50 @@ +# -*- coding: utf-8 -*- + +import gc +import weakref + +import pytest + +import env # noqa: F401 +from pybind11_tests import custom_type_setup as m + + +@pytest.fixture +def gc_tester(): + """Tests that an object is garbage collected. + + Assumes that any unreferenced objects are fully collected after calling + `gc.collect()`. That is true on CPython, but does not appear to reliably + hold on PyPy. + """ + + weak_refs = [] + + def add_ref(obj): + # PyPy does not support `gc.is_tracked`. + if hasattr(gc, "is_tracked"): + assert gc.is_tracked(obj) + weak_refs.append(weakref.ref(obj)) + + yield add_ref + + gc.collect() + for ref in weak_refs: + assert ref() is None + + +# PyPy does not seem to reliably garbage collect. +@pytest.mark.skipif("env.PYPY") +def test_self_cycle(gc_tester): + obj = m.OwnsPythonObjects() + obj.value = obj + gc_tester(obj) + + +# PyPy does not seem to reliably garbage collect. +@pytest.mark.skipif("env.PYPY") +def test_indirect_cycle(gc_tester): + obj = m.OwnsPythonObjects() + obj_list = [obj] + obj.value = obj_list + gc_tester(obj) From d58699c9ab9d20010b15aa38382ad517a8280179 Mon Sep 17 00:00:00 2001 From: Henry Schreiner Date: Fri, 24 Sep 2021 17:47:03 -0400 Subject: [PATCH 188/638] fix(cmake): reduce chance for variable collision (#3302) --- tools/pybind11NewTools.cmake | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tools/pybind11NewTools.cmake b/tools/pybind11NewTools.cmake index b93f9145e3..0a7b84706b 100644 --- a/tools/pybind11NewTools.cmake +++ b/tools/pybind11NewTools.cmake @@ -82,11 +82,11 @@ if(NOT DEFINED ${_Python}_EXECUTABLE) endif() -if(NOT ${_Python}_EXECUTABLE STREQUAL PYTHON_EXECUTABLE_LAST) +if(NOT ${_Python}_EXECUTABLE STREQUAL PYBIND11_PYTHON_EXECUTABLE_LAST) # Detect changes to the Python version/binary in subsequent CMake runs, and refresh config if needed unset(PYTHON_IS_DEBUG CACHE) unset(PYTHON_MODULE_EXTENSION CACHE) - set(PYTHON_EXECUTABLE_LAST + set(PYBIND11_PYTHON_EXECUTABLE_LAST "${${_Python}_EXECUTABLE}" CACHE INTERNAL "Python executable during the last CMake run") endif() From 6bce3bd72e8d0838cdb1586abd6d3cfb9c598a8f Mon Sep 17 00:00:00 2001 From: Henry Schreiner Date: Fri, 24 Sep 2021 23:48:38 -0400 Subject: [PATCH 189/638] docs: update CHANGELOG (#3304) --- docs/changelog.rst | 40 ++++++++++++++++++++++++++++++++++++++-- 1 file changed, 38 insertions(+), 2 deletions(-) diff --git a/docs/changelog.rst b/docs/changelog.rst index 6bdf6a6e45..04e2714fc3 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -38,21 +38,42 @@ New features: * Implemented ``reshape`` on arrays. `#984 `_ +* Enable defining custom ``__new__`` methods on classes by fixing bug + preventing overriding methods if they have non-pybind11 siblings. + `#3265 `_ + +* Add ``make_value_iterator()``, and fix ``make_key_iterator()`` to return + references instead of copies. + `#3293 `_ + +* ``pybind11::custom_type_setup`` was added, for customizing the + ``PyHeapTypeObject`` corresponding to a class, which may be useful for + enabling garbage collection support, among other things. + `#3287 `_ + + Changes: * Set ``__file__`` constant when running ``eval_file`` in an embedded interpreter. `#3233 `_ +* Python objects and (C++17) ``std::optional`` now accepted in ``py::slice`` + constructor. + `#1101 `_ + * The pybind11 proxy types ``str``, ``bytes``, ``bytearray``, ``tuple``, ``list`` now consistently support passing ``ssize_t`` values for sizes and indexes. Previously, only ``size_t`` was accepted in several interfaces. `#3219 `_ +* Avoid evaluating ``PYBIND11_TLS_REPLACE_VALUE`` arguments more than once. + `#3290 `_ Fixes: -* Bug fix: enum value's ``__int__`` returning non-int when underlying type is bool or of char type. +* Bug fix: enum value's ``__int__`` returning non-int when underlying type is + bool or of char type. `#1334 `_ * Fixes bug in setting error state in Capsule's pointer methods. @@ -61,7 +82,8 @@ Fixes: * A long-standing memory leak in ``py::cpp_function::initialize`` was fixed. `#3229 `_ -* Fixes thread safety for some ``pybind11::type_caster`` which require lifetime extension, such as for ``std::string_view``. +* Fixes thread safety for some ``pybind11::type_caster`` which require lifetime + extension, such as for ``std::string_view``. `#3237 `_ * Restore compatibility with gcc 4.8.4 as distributed by ubuntu-trusty, linuxmint-17. @@ -73,12 +95,26 @@ Build system improvements: * Fix regression in CMake Python package config: improper use of absolute path. `#3144 `_ +* Cached Python version information could become stale when CMake was re-run + with a different Python version. The build system now detects this and + updates this information. + `#3299 `_ + * Specified UTF8-encoding in setup.py calls of open(). `#3137 `_ +* Fix a harmless warning from CMake 3.21 with the classic Python discovery. + `#3220 `_ + Backend and tidying up: +* Reduced thread-local storage required for keeping alive temporary data for + type conversion to one key per ABI version, rather than one key per extension + module. This makes the total thread-local storage required by pybind11 2 + keys per ABI version. + `#3275 `_ + * Optimize NumPy array construction with additional moves. `#3183 `_ From 6be64304b7e5cd4792fcd25d031127b99ef474bc Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 28 Sep 2021 10:09:08 -0400 Subject: [PATCH 190/638] [pre-commit.ci] pre-commit autoupdate (#3312) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit updates: - [github.com/asottile/pyupgrade: v2.26.0 → v2.28.0](https://github.com/asottile/pyupgrade/compare/v2.26.0...v2.28.0) Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> --- .pre-commit-config.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 4245641ba3..05bfce8517 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -33,7 +33,7 @@ repos: exclude: ^noxfile.py$ - repo: https://github.com/asottile/pyupgrade - rev: v2.26.0 + rev: v2.28.0 hooks: - id: pyupgrade From 71fd524135cc73f8d6a6c3c0d7aea6cca1605e32 Mon Sep 17 00:00:00 2001 From: Philipp Bucher Date: Tue, 28 Sep 2021 16:09:38 +0200 Subject: [PATCH 191/638] docs: fix minor typo (#3311) --- docs/compiling.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/compiling.rst b/docs/compiling.rst index bf7acfc808..b4072d9737 100644 --- a/docs/compiling.rst +++ b/docs/compiling.rst @@ -347,7 +347,7 @@ standard explicitly with set(CMAKE_CXX_STANDARD 14 CACHE STRING "C++ version selection") # or 11, 14, 17, 20 set(CMAKE_CXX_STANDARD_REQUIRED ON) # optional, ensure standard is supported - set(CMAKE_CXX_EXTENSIONS OFF) # optional, keep compiler extensionsn off + set(CMAKE_CXX_EXTENSIONS OFF) # optional, keep compiler extensions off The variables can also be set when calling CMake from the command line using the ``-D=`` flag. You can also manually set ``CXX_STANDARD`` From c9bbf8d2eed098f3c10ae461bab2c0f9541d38f7 Mon Sep 17 00:00:00 2001 From: Philipp Bucher Date: Tue, 28 Sep 2021 16:09:38 +0200 Subject: [PATCH 192/638] docs: fix minor typo (#3311) --- docs/compiling.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/compiling.rst b/docs/compiling.rst index 0738510484..75608bd576 100644 --- a/docs/compiling.rst +++ b/docs/compiling.rst @@ -340,7 +340,7 @@ standard explicitly with set(CMAKE_CXX_STANDARD 14 CACHE STRING "C++ version selection") # or 11, 14, 17, 20 set(CMAKE_CXX_STANDARD_REQUIRED ON) # optional, ensure standard is supported - set(CMAKE_CXX_EXTENSIONS OFF) # optional, keep compiler extensionsn off + set(CMAKE_CXX_EXTENSIONS OFF) # optional, keep compiler extensions off The variables can also be set when calling CMake from the command line using the ``-D=`` flag. You can also manually set ``CXX_STANDARD`` From a1830d5e6e27cb4c40955399f3826736a59f5cd5 Mon Sep 17 00:00:00 2001 From: Henry Schreiner Date: Wed, 29 Sep 2021 06:18:58 -0400 Subject: [PATCH 193/638] docs: mention title conventions in PR template (#3313) --- .github/pull_request_template.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/.github/pull_request_template.md b/.github/pull_request_template.md index 97a6ff7dda..54b7f5100d 100644 --- a/.github/pull_request_template.md +++ b/.github/pull_request_template.md @@ -1,3 +1,7 @@ + ## Description From b4e1ab8caa483350fbcfc7397b3b5cb33c49050f Mon Sep 17 00:00:00 2001 From: xaedes Date: Thu, 30 Sep 2021 20:45:06 +0200 Subject: [PATCH 194/638] Docs: Demonstrate non-enum internal types in example (#3314) * Docs: Demonstrate non-enum internal types in example Previously example only demonstrated internal enumeration type. To show that it works for other internal types the same way the example was updated with an additional struct Pet::Attributes type. * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> --- docs/classes.rst | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/docs/classes.rst b/docs/classes.rst index a4463e4179..13fa8b5387 100644 --- a/docs/classes.rst +++ b/docs/classes.rst @@ -446,8 +446,7 @@ you can use ``py::detail::overload_cast_impl`` with an additional set of parenth Enumerations and internal types =============================== -Let's now suppose that the example class contains an internal enumeration type, -e.g.: +Let's now suppose that the example class contains internal types like enumerations, e.g.: .. code-block:: cpp @@ -457,10 +456,15 @@ e.g.: Cat }; + struct Attributes { + float age = 0; + }; + Pet(const std::string &name, Kind type) : name(name), type(type) { } std::string name; Kind type; + Attributes attr; }; The binding code for this example looks as follows: @@ -471,15 +475,21 @@ The binding code for this example looks as follows: pet.def(py::init()) .def_readwrite("name", &Pet::name) - .def_readwrite("type", &Pet::type); + .def_readwrite("type", &Pet::type) + .def_readwrite("attr", &Pet::attr); py::enum_(pet, "Kind") .value("Dog", Pet::Kind::Dog) .value("Cat", Pet::Kind::Cat) .export_values(); -To ensure that the ``Kind`` type is created within the scope of ``Pet``, the -``pet`` :class:`class_` instance must be supplied to the :class:`enum_`. + py::class_ attributes(pet, "Attributes") + .def(py::init<>()) + .def_readwrite("age", &Pet::Attributes::age); + + +To ensure that the nested types ``Kind`` and ``Attributes`` are created within the scope of ``Pet``, the +``pet`` :class:`class_` instance must be supplied to the :class:`enum_` and :class:`class_` constructor. The :func:`enum_::export_values` function exports the enum entries into the parent scope, which should be skipped for newer C++11-style strongly typed enums. From b3573ac9615b705d6e5b9bf598a3378edf079f06 Mon Sep 17 00:00:00 2001 From: Bruce Merry <1963944+bmerry@users.noreply.github.com> Date: Fri, 1 Oct 2021 15:24:36 +0200 Subject: [PATCH 195/638] feat: add `.keys` and `.values` to bind_map (#3310) * Add `.keys` and `.values` to bind_map Both of these implement views (rather than just iterators), and `.items` is also upgraded to a view. In practical terms, this allows a view to be iterated multiple times and have its size taken, neither of which works with an iterator. The views implement `__len__`, `__iter__`, and the keys view implements `__contains__`. Testing membership also works in item and value views because Python falls back to iteration. This won't be optimal for item values since it's linear rather than O(log n) or O(1), but I didn't fancy trying to get all the corner cases to match Python behaviour (tuple of wrong types, wrong length tuple, not a tuple etc). Missing relative to Python dictionary views is `__reversed__` (only added to Python in 3.8). Implementing that could break code that binds custom map classes which don't provide `rbegin`/`rend` (at least without doing clever things with SFINAE), so I've not tried. The size increase on my system is 131072 bytes, which is rather large (5%) but also suspiciously round (2^17) and makes me suspect some quantisation effect. * bind_map: support any object in __contains__ Add extra overload of `__contains__` (for both the map itself and KeysView) which takes an arbitrary object and returns false. * Take py::object by const reference in __contains__ To keep clang-tidy happy. * Removing stray `py::` (detected via interactive testing in Google environment). Co-authored-by: Ralf W. Grosse-Kunstleve --- include/pybind11/stl_bind.h | 78 +++++++++++++++++++++++++++++++++++-- tests/test_stl_binders.py | 30 +++++++++++++- 2 files changed, 104 insertions(+), 4 deletions(-) diff --git a/include/pybind11/stl_bind.h b/include/pybind11/stl_bind.h index 82317b3791..050be83cc5 100644 --- a/include/pybind11/stl_bind.h +++ b/include/pybind11/stl_bind.h @@ -595,6 +595,23 @@ template auto map_if_insertion_operator(Class_ & ); } +template +struct keys_view +{ + Map ↦ +}; + +template +struct values_view +{ + Map ↦ +}; + +template +struct items_view +{ + Map ↦ +}; PYBIND11_NAMESPACE_END(detail) @@ -602,6 +619,9 @@ template , typename... class_ bind_map(handle scope, const std::string &name, Args&&... args) { using KeyType = typename Map::key_type; using MappedType = typename Map::mapped_type; + using KeysView = detail::keys_view; + using ValuesView = detail::values_view; + using ItemsView = detail::items_view; using Class_ = class_; // If either type is a non-module-local bound type then make the map binding non-local as well; @@ -615,6 +635,12 @@ class_ bind_map(handle scope, const std::string &name, Args&&. } Class_ cl(scope, name.c_str(), pybind11::module_local(local), std::forward(args)...); + class_ keys_view( + scope, ("KeysView[" + name + "]").c_str(), pybind11::module_local(local)); + class_ values_view( + scope, ("ValuesView[" + name + "]").c_str(), pybind11::module_local(local)); + class_ items_view( + scope, ("ItemsView[" + name + "]").c_str(), pybind11::module_local(local)); cl.def(init<>()); @@ -628,12 +654,22 @@ class_ bind_map(handle scope, const std::string &name, Args&&. cl.def("__iter__", [](Map &m) { return make_key_iterator(m.begin(), m.end()); }, - keep_alive<0, 1>() /* Essential: keep list alive while iterator exists */ + keep_alive<0, 1>() /* Essential: keep map alive while iterator exists */ + ); + + cl.def("keys", + [](Map &m) { return KeysView{m}; }, + keep_alive<0, 1>() /* Essential: keep map alive while view exists */ + ); + + cl.def("values", + [](Map &m) { return ValuesView{m}; }, + keep_alive<0, 1>() /* Essential: keep map alive while view exists */ ); cl.def("items", - [](Map &m) { return make_iterator(m.begin(), m.end()); }, - keep_alive<0, 1>() /* Essential: keep list alive while iterator exists */ + [](Map &m) { return ItemsView{m}; }, + keep_alive<0, 1>() /* Essential: keep map alive while view exists */ ); cl.def("__getitem__", @@ -654,6 +690,8 @@ class_ bind_map(handle scope, const std::string &name, Args&&. return true; } ); + // Fallback for when the object is not of the key type + cl.def("__contains__", [](Map &, const object &) -> bool { return false; }); // Assignment provided only if the type is copyable detail::map_assignment(cl); @@ -669,6 +707,40 @@ class_ bind_map(handle scope, const std::string &name, Args&&. cl.def("__len__", &Map::size); + keys_view.def("__len__", [](KeysView &view) { return view.map.size(); }); + keys_view.def("__iter__", + [](KeysView &view) { + return make_key_iterator(view.map.begin(), view.map.end()); + }, + keep_alive<0, 1>() /* Essential: keep view alive while iterator exists */ + ); + keys_view.def("__contains__", + [](KeysView &view, const KeyType &k) -> bool { + auto it = view.map.find(k); + if (it == view.map.end()) + return false; + return true; + } + ); + // Fallback for when the object is not of the key type + keys_view.def("__contains__", [](KeysView &, const object &) -> bool { return false; }); + + values_view.def("__len__", [](ValuesView &view) { return view.map.size(); }); + values_view.def("__iter__", + [](ValuesView &view) { + return make_value_iterator(view.map.begin(), view.map.end()); + }, + keep_alive<0, 1>() /* Essential: keep view alive while iterator exists */ + ); + + items_view.def("__len__", [](ItemsView &view) { return view.map.size(); }); + items_view.def("__iter__", + [](ItemsView &view) { + return make_iterator(view.map.begin(), view.map.end()); + }, + keep_alive<0, 1>() /* Essential: keep view alive while iterator exists */ + ); + return cl; } diff --git a/tests/test_stl_binders.py b/tests/test_stl_binders.py index 475a9ec400..a68dcd31de 100644 --- a/tests/test_stl_binders.py +++ b/tests/test_stl_binders.py @@ -160,15 +160,43 @@ def test_map_string_double(): mm["b"] = 2.5 assert list(mm) == ["a", "b"] - assert list(mm.items()) == [("a", 1), ("b", 2.5)] assert str(mm) == "MapStringDouble{a: 1, b: 2.5}" + assert "b" in mm + assert "c" not in mm + assert 123 not in mm + + # Check that keys, values, items are views, not merely iterable + keys = mm.keys() + values = mm.values() + items = mm.items() + assert list(keys) == ["a", "b"] + assert len(keys) == 2 + assert "a" in keys + assert "c" not in keys + assert 123 not in keys + assert list(items) == [("a", 1), ("b", 2.5)] + assert len(items) == 2 + assert ("b", 2.5) in items + assert "hello" not in items + assert ("b", 2.5, None) not in items + assert list(values) == [1, 2.5] + assert len(values) == 2 + assert 1 in values + assert 2 not in values + # Check that views update when the map is updated + mm["c"] = -1 + assert list(keys) == ["a", "b", "c"] + assert list(values) == [1, 2.5, -1] + assert list(items) == [("a", 1), ("b", 2.5), ("c", -1)] um = m.UnorderedMapStringDouble() um["ua"] = 1.1 um["ub"] = 2.6 assert sorted(list(um)) == ["ua", "ub"] + assert list(um.keys()) == list(um) assert sorted(list(um.items())) == [("ua", 1.1), ("ub", 2.6)] + assert list(zip(um.keys(), um.values())) == list(um.items()) assert "UnorderedMapStringDouble" in str(um) From ad9665560597a473774ea19dc87a40aef02090b3 Mon Sep 17 00:00:00 2001 From: Aaron Gokaslan Date: Sun, 3 Oct 2021 20:15:37 -0400 Subject: [PATCH 196/638] fix: replace free() with std::free() (#3321) * Disambiguate free() to use std::free() * Add cstdlib include --- include/pybind11/pybind11.h | 5 +++-- tests/test_embed/test_interpreter.cpp | 3 ++- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/include/pybind11/pybind11.h b/include/pybind11/pybind11.h index 16535f1951..370e52cff6 100644 --- a/include/pybind11/pybind11.h +++ b/include/pybind11/pybind11.h @@ -16,6 +16,7 @@ #include "detail/class.h" #include "detail/init.h" +#include #include #include #include @@ -1536,7 +1537,7 @@ class class_ : public detail::generic_type { char *doc_prev = rec_fget->doc; /* 'extra' field may include a property-specific documentation string */ detail::process_attributes::init(extra..., rec_fget); if (rec_fget->doc && rec_fget->doc != doc_prev) { - free(doc_prev); + std::free(doc_prev); rec_fget->doc = PYBIND11_COMPAT_STRDUP(rec_fget->doc); } } @@ -1544,7 +1545,7 @@ class class_ : public detail::generic_type { char *doc_prev = rec_fset->doc; detail::process_attributes::init(extra..., rec_fset); if (rec_fset->doc && rec_fset->doc != doc_prev) { - free(doc_prev); + std::free(doc_prev); rec_fset->doc = PYBIND11_COMPAT_STRDUP(rec_fset->doc); } if (! rec_active) rec_active = rec_fset; diff --git a/tests/test_embed/test_interpreter.cpp b/tests/test_embed/test_interpreter.cpp index fae14f7510..20bcade0ac 100644 --- a/tests/test_embed/test_interpreter.cpp +++ b/tests/test_embed/test_interpreter.cpp @@ -8,6 +8,7 @@ #include +#include #include #include #include @@ -315,7 +316,7 @@ TEST_CASE("sys.argv gets initialized properly") { { char *argv[] = {strdup("a.out")}; py::scoped_interpreter argv_scope(true, 1, argv); - free(argv[0]); + std::free(argv[0]); auto module = py::module::import("test_interpreter"); auto py_widget = module.attr("DerivedWidget")("The question"); const auto &cpp_widget = py_widget.cast(); From d7a7edc12bc4c7aec4afd16aee1ed032bb974164 Mon Sep 17 00:00:00 2001 From: Henry Schreiner Date: Mon, 4 Oct 2021 12:31:53 -0400 Subject: [PATCH 197/638] tests: support Eigen configuration --- tests/CMakeLists.txt | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 70303879f3..6ebcba9b6b 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -171,6 +171,14 @@ set(PYBIND11_CROSS_MODULE_TESTS test_exceptions.py test_local_bindings.py test_s set(PYBIND11_CROSS_MODULE_GIL_TESTS test_gil_scoped.py) +set(PYBIND11_EIGEN_REPO + "https://gitlab.com/libeigen/eigen.git" + CACHE STRING "Eigen repository to use for tests") +# This hash is for 3.3.8, using a hash for security reasons +set(PYBIND11_EIGEN_VERSION + "dc252fbf00079ccab57948a164b1421703fe4361" + CACHE STRING "Eigen version to use for tests") + # Check if Eigen is available; if not, remove from PYBIND11_TEST_FILES (but # keep it in PYBIND11_PYTEST_FILES, so that we get the "eigen is not installed" # skip message). @@ -184,13 +192,11 @@ if(PYBIND11_TEST_FILES_EIGEN_I GREATER -1) message(FATAL_ERROR "CMake 3.11+ required when using DOWNLOAD_EIGEN") endif() - set(EIGEN3_VERSION_STRING "3.3.8") - include(FetchContent) FetchContent_Declare( eigen - GIT_REPOSITORY https://gitlab.com/libeigen/eigen.git - GIT_TAG ${EIGEN3_VERSION_STRING}) + GIT_REPOSITORY "${PYBIND11_EIGEN_REPO}" + GIT_TAG "${PYBIND11_EIGEN_VERSION}") FetchContent_GetProperties(eigen) if(NOT eigen_POPULATED) From 9f146a56227eab9b3bf5e847fc5610853372f587 Mon Sep 17 00:00:00 2001 From: "Ralf W. Grosse-Kunstleve" Date: Sun, 3 Oct 2021 14:57:41 -0700 Subject: [PATCH 198/638] All `-DDOWNLOAD_EIGEN=OFF` (to work around gitlab eigen outage). --- .github/workflows/ci.yml | 8 ++++++++ .github/workflows/format.yml | 1 + 2 files changed, 9 insertions(+) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 0b4968cc83..279a1e7a08 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -102,6 +102,7 @@ jobs: -DPYBIND11_WERROR=ON -DDOWNLOAD_CATCH=ON -DDOWNLOAD_EIGEN=ON + -DPYBIND11_EIGEN_REPO=https://gitlab.com/libeigen/eigen-backup -DCMAKE_CXX_STANDARD=11 ${{ matrix.args }} @@ -129,6 +130,7 @@ jobs: -DPYBIND11_WERROR=ON -DDOWNLOAD_CATCH=ON -DDOWNLOAD_EIGEN=ON + -DPYBIND11_EIGEN_REPO=https://gitlab.com/libeigen/eigen-backup -DCMAKE_CXX_STANDARD=17 ${{ matrix.args }} ${{ matrix.args2 }} @@ -151,6 +153,7 @@ jobs: -DPYBIND11_WERROR=ON -DDOWNLOAD_CATCH=ON -DDOWNLOAD_EIGEN=ON + -DPYBIND11_EIGEN_REPO=https://gitlab.com/libeigen/eigen-backup -DCMAKE_CXX_STANDARD=17 -DPYBIND11_INTERNALS_VERSION=10000000 "-DPYBIND11_TEST_OVERRIDE=test_call_policies.cpp;test_gil_scoped.cpp;test_thread.cpp" @@ -249,6 +252,7 @@ jobs: -DPYBIND11_WERROR=ON -DDOWNLOAD_CATCH=ON -DDOWNLOAD_EIGEN=ON + -DPYBIND11_EIGEN_REPO=https://gitlab.com/libeigen/eigen-backup -DCMAKE_CXX_STANDARD=17 - name: Build @@ -628,6 +632,7 @@ jobs: -DPYBIND11_WERROR=ON -DDOWNLOAD_CATCH=ON -DDOWNLOAD_EIGEN=ON + -DPYBIND11_EIGEN_REPO=https://gitlab.com/libeigen/eigen-backup -DCMAKE_CXX_STANDARD=11 -DPYTHON_EXECUTABLE=$(python3 -c "import sys; print(sys.executable)") @@ -774,6 +779,7 @@ jobs: -DPYBIND11_WERROR=ON -DDOWNLOAD_CATCH=ON -DDOWNLOAD_EIGEN=ON + -DPYBIND11_EIGEN_REPO=https://gitlab.com/libeigen/eigen-backup ${{ matrix.args }} - name: Build C++11 run: cmake --build build -j 2 @@ -820,6 +826,7 @@ jobs: -DPYBIND11_WERROR=ON -DDOWNLOAD_CATCH=ON -DDOWNLOAD_EIGEN=ON + -DPYBIND11_EIGEN_REPO=https://gitlab.com/libeigen/eigen-backup - name: Build C++14 run: cmake --build build -j 2 @@ -869,6 +876,7 @@ jobs: -DPYBIND11_WERROR=ON -DDOWNLOAD_CATCH=ON -DDOWNLOAD_EIGEN=ON + -DPYBIND11_EIGEN_REPO=https://gitlab.com/libeigen/eigen-backup -DCMAKE_CXX_STANDARD=${{ matrix.std }} ${{ matrix.args }} diff --git a/.github/workflows/format.yml b/.github/workflows/format.yml index ab7b40503a..7869912bf6 100644 --- a/.github/workflows/format.yml +++ b/.github/workflows/format.yml @@ -41,6 +41,7 @@ jobs: cmake -S . -B build -DCMAKE_CXX_CLANG_TIDY="$(which clang-tidy)" -DDOWNLOAD_EIGEN=ON + -DPYBIND11_EIGEN_REPO=https://gitlab.com/libeigen/eigen-backup -DDOWNLOAD_CATCH=ON -DCMAKE_CXX_STANDARD=17 From 591db0b930f6739fe276bce9a7d5b6647bd00851 Mon Sep 17 00:00:00 2001 From: Henry Schreiner Date: Fri, 1 Oct 2021 09:30:55 -0400 Subject: [PATCH 199/638] docs: update CHANGELOG for 2.8 --- docs/changelog.rst | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/docs/changelog.rst b/docs/changelog.rst index 04e2714fc3..4a58f2d514 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -6,8 +6,8 @@ Changelog Starting with version 1.8.0, pybind11 releases use a `semantic versioning `_ policy. -v2.8.0 (WIP) ------------- +v2.8.0 (Oct 4, 2021) +-------------------- New features: @@ -46,13 +46,17 @@ New features: references instead of copies. `#3293 `_ +* Improve the classes generated by ``bind_map``: `#3310 `_ + * Change ``.items`` from an iterator to a dictionary view. + * Add ``.keys`` and ``.values`` (both dictionary views). + * Allow ``__contains__`` to take any object. + * ``pybind11::custom_type_setup`` was added, for customizing the ``PyHeapTypeObject`` corresponding to a class, which may be useful for enabling garbage collection support, among other things. `#3287 `_ - Changes: * Set ``__file__`` constant when running ``eval_file`` in an embedded interpreter. @@ -106,6 +110,9 @@ Build system improvements: * Fix a harmless warning from CMake 3.21 with the classic Python discovery. `#3220 `_ +* Eigen repo and version can now be specified as cmake options. + `#3324 `_ + Backend and tidying up: From 20aae3e61a91dbae5d71c3b11677369d94a82884 Mon Sep 17 00:00:00 2001 From: Henry Schreiner Date: Mon, 4 Oct 2021 14:47:45 -0400 Subject: [PATCH 200/638] ci: disable Eigen due to Cert issue on CentOS --- .github/workflows/ci.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 279a1e7a08..416d2a4ec5 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -624,6 +624,7 @@ jobs: if: matrix.centos == 8 run: echo Release > VAR_BUILD_TYPE + # Temporally disabling EIGEN due to SSL issue in CentOS 7 - name: Configure shell: bash run: > @@ -631,8 +632,7 @@ jobs: -DCMAKE_BUILD_TYPE=$(cat VAR_BUILD_TYPE) -DPYBIND11_WERROR=ON -DDOWNLOAD_CATCH=ON - -DDOWNLOAD_EIGEN=ON - -DPYBIND11_EIGEN_REPO=https://gitlab.com/libeigen/eigen-backup + -DDOWNLOAD_EIGEN=OFF -DCMAKE_CXX_STANDARD=11 -DPYTHON_EXECUTABLE=$(python3 -c "import sys; print(sys.executable)") From c9a319c607a5fcddf14417483a451bd85083da72 Mon Sep 17 00:00:00 2001 From: Henry Schreiner Date: Mon, 4 Oct 2021 15:33:58 -0400 Subject: [PATCH 201/638] chore: version 2.8.0 final --- include/pybind11/detail/common.h | 4 ++-- pybind11/_version.py | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/include/pybind11/detail/common.h b/include/pybind11/detail/common.h index 3bd84da576..31716e5bfe 100644 --- a/include/pybind11/detail/common.h +++ b/include/pybind11/detail/common.h @@ -11,11 +11,11 @@ #define PYBIND11_VERSION_MAJOR 2 #define PYBIND11_VERSION_MINOR 8 -#define PYBIND11_VERSION_PATCH 0.dev2 +#define PYBIND11_VERSION_PATCH 0 // Similar to Python's convention: https://docs.python.org/3/c-api/apiabiversion.html // Additional convention: 0xD = dev -#define PYBIND11_VERSION_HEX 0x020800D2 +#define PYBIND11_VERSION_HEX 0x02080000 #define PYBIND11_NAMESPACE_BEGIN(name) namespace name { #define PYBIND11_NAMESPACE_END(name) } diff --git a/pybind11/_version.py b/pybind11/_version.py index d212f1dfb7..704191b98e 100644 --- a/pybind11/_version.py +++ b/pybind11/_version.py @@ -8,5 +8,5 @@ def _to_int(s): return s -__version__ = "2.8.0.dev2" +__version__ = "2.8.0" version_info = tuple(_to_int(s) for s in __version__.split(".")) From 3747dc2c4f807fba952380f7bd9f367ab9c53a0a Mon Sep 17 00:00:00 2001 From: Henry Schreiner Date: Mon, 4 Oct 2021 16:38:15 -0400 Subject: [PATCH 202/638] Revert "All `-DDOWNLOAD_EIGEN=OFF` (to work around gitlab eigen outage)." (#3326) This reverts commit 9f146a56227eab9b3bf5e847fc5610853372f587. --- .github/workflows/ci.yml | 9 +-------- .github/workflows/format.yml | 1 - 2 files changed, 1 insertion(+), 9 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 416d2a4ec5..6843a92430 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -102,7 +102,6 @@ jobs: -DPYBIND11_WERROR=ON -DDOWNLOAD_CATCH=ON -DDOWNLOAD_EIGEN=ON - -DPYBIND11_EIGEN_REPO=https://gitlab.com/libeigen/eigen-backup -DCMAKE_CXX_STANDARD=11 ${{ matrix.args }} @@ -130,7 +129,6 @@ jobs: -DPYBIND11_WERROR=ON -DDOWNLOAD_CATCH=ON -DDOWNLOAD_EIGEN=ON - -DPYBIND11_EIGEN_REPO=https://gitlab.com/libeigen/eigen-backup -DCMAKE_CXX_STANDARD=17 ${{ matrix.args }} ${{ matrix.args2 }} @@ -153,7 +151,6 @@ jobs: -DPYBIND11_WERROR=ON -DDOWNLOAD_CATCH=ON -DDOWNLOAD_EIGEN=ON - -DPYBIND11_EIGEN_REPO=https://gitlab.com/libeigen/eigen-backup -DCMAKE_CXX_STANDARD=17 -DPYBIND11_INTERNALS_VERSION=10000000 "-DPYBIND11_TEST_OVERRIDE=test_call_policies.cpp;test_gil_scoped.cpp;test_thread.cpp" @@ -252,7 +249,6 @@ jobs: -DPYBIND11_WERROR=ON -DDOWNLOAD_CATCH=ON -DDOWNLOAD_EIGEN=ON - -DPYBIND11_EIGEN_REPO=https://gitlab.com/libeigen/eigen-backup -DCMAKE_CXX_STANDARD=17 - name: Build @@ -632,7 +628,7 @@ jobs: -DCMAKE_BUILD_TYPE=$(cat VAR_BUILD_TYPE) -DPYBIND11_WERROR=ON -DDOWNLOAD_CATCH=ON - -DDOWNLOAD_EIGEN=OFF + -DDOWNLOAD_EIGEN=ON -DCMAKE_CXX_STANDARD=11 -DPYTHON_EXECUTABLE=$(python3 -c "import sys; print(sys.executable)") @@ -779,7 +775,6 @@ jobs: -DPYBIND11_WERROR=ON -DDOWNLOAD_CATCH=ON -DDOWNLOAD_EIGEN=ON - -DPYBIND11_EIGEN_REPO=https://gitlab.com/libeigen/eigen-backup ${{ matrix.args }} - name: Build C++11 run: cmake --build build -j 2 @@ -826,7 +821,6 @@ jobs: -DPYBIND11_WERROR=ON -DDOWNLOAD_CATCH=ON -DDOWNLOAD_EIGEN=ON - -DPYBIND11_EIGEN_REPO=https://gitlab.com/libeigen/eigen-backup - name: Build C++14 run: cmake --build build -j 2 @@ -876,7 +870,6 @@ jobs: -DPYBIND11_WERROR=ON -DDOWNLOAD_CATCH=ON -DDOWNLOAD_EIGEN=ON - -DPYBIND11_EIGEN_REPO=https://gitlab.com/libeigen/eigen-backup -DCMAKE_CXX_STANDARD=${{ matrix.std }} ${{ matrix.args }} diff --git a/.github/workflows/format.yml b/.github/workflows/format.yml index 7869912bf6..ab7b40503a 100644 --- a/.github/workflows/format.yml +++ b/.github/workflows/format.yml @@ -41,7 +41,6 @@ jobs: cmake -S . -B build -DCMAKE_CXX_CLANG_TIDY="$(which clang-tidy)" -DDOWNLOAD_EIGEN=ON - -DPYBIND11_EIGEN_REPO=https://gitlab.com/libeigen/eigen-backup -DDOWNLOAD_CATCH=ON -DCMAKE_CXX_STANDARD=17 From 97976c16fb7652f7faf02d76756666ef87adbe7d Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 4 Oct 2021 16:38:31 -0400 Subject: [PATCH 203/638] [pre-commit.ci] pre-commit autoupdate (#3325) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit updates: - [github.com/asottile/pyupgrade: v2.28.0 → v2.29.0](https://github.com/asottile/pyupgrade/compare/v2.28.0...v2.29.0) Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> --- .pre-commit-config.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 05bfce8517..2df3146064 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -33,7 +33,7 @@ repos: exclude: ^noxfile.py$ - repo: https://github.com/asottile/pyupgrade - rev: v2.28.0 + rev: v2.29.0 hooks: - id: pyupgrade From ba9f919b857f12efd18d3fc43fc62e7ad724c6d9 Mon Sep 17 00:00:00 2001 From: Henry Schreiner Date: Mon, 4 Oct 2021 17:37:27 -0400 Subject: [PATCH 204/638] chore: get back to work after 2.8.0 --- docs/changelog.rst | 6 ++++++ docs/release.rst | 2 ++ include/pybind11/detail/common.h | 6 +++--- pybind11/_version.py | 2 +- 4 files changed, 12 insertions(+), 4 deletions(-) diff --git a/docs/changelog.rst b/docs/changelog.rst index 4a58f2d514..1dab8765f2 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -6,6 +6,12 @@ Changelog Starting with version 1.8.0, pybind11 releases use a `semantic versioning `_ policy. + +IN DEVELOPMENT +-------------- + + + v2.8.0 (Oct 4, 2021) -------------------- diff --git a/docs/release.rst b/docs/release.rst index e4f3d1902a..7fa254eaf9 100644 --- a/docs/release.rst +++ b/docs/release.rst @@ -27,6 +27,7 @@ To release a new version of pybind11: ``include/pybind11/detail/common.h``. PATCH should be a simple integer. - Update the version HEX just below, as well. - Update ``pybind11/_version.py`` (match above) + - Run ``nox -s tests_packaging``` to ensure this was done correctly. - Ensure that all the information in ``setup.cfg`` is up-to-date, like supported Python versions. - Add release date in ``docs/changelog.rst``. @@ -64,6 +65,7 @@ To release a new version of pybind11: - Update version macros in ``include/pybind11/detail/common.h`` (set PATCH to ``0.dev1`` and increment MINOR). - Update ``_version.py`` to match + - Run ``nox -s tests_packaging``` to ensure this was done correctly. - Add a spot for in-development updates in ``docs/changelog.rst``. - ``git add``, ``git commit``, ``git push`` diff --git a/include/pybind11/detail/common.h b/include/pybind11/detail/common.h index 31716e5bfe..9ad305a4ee 100644 --- a/include/pybind11/detail/common.h +++ b/include/pybind11/detail/common.h @@ -10,12 +10,12 @@ #pragma once #define PYBIND11_VERSION_MAJOR 2 -#define PYBIND11_VERSION_MINOR 8 -#define PYBIND11_VERSION_PATCH 0 +#define PYBIND11_VERSION_MINOR 9 +#define PYBIND11_VERSION_PATCH 0.dev1 // Similar to Python's convention: https://docs.python.org/3/c-api/apiabiversion.html // Additional convention: 0xD = dev -#define PYBIND11_VERSION_HEX 0x02080000 +#define PYBIND11_VERSION_HEX 0x020900D1 #define PYBIND11_NAMESPACE_BEGIN(name) namespace name { #define PYBIND11_NAMESPACE_END(name) } diff --git a/pybind11/_version.py b/pybind11/_version.py index 704191b98e..ce894a7740 100644 --- a/pybind11/_version.py +++ b/pybind11/_version.py @@ -8,5 +8,5 @@ def _to_int(s): return s -__version__ = "2.8.0" +__version__ = "2.9.0.dev1" version_info = tuple(_to_int(s) for s in __version__.split(".")) From 47ed124f3728c7886d496c28c15b889c78599b99 Mon Sep 17 00:00:00 2001 From: Bruce Merry <1963944+bmerry@users.noreply.github.com> Date: Fri, 8 Oct 2021 14:27:38 +0200 Subject: [PATCH 205/638] Fix some formatting in the v2.8.0 changelog (#3339) [skipci] --- docs/changelog.rst | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/docs/changelog.rst b/docs/changelog.rst index 1dab8765f2..80c580b180 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -53,9 +53,10 @@ New features: `#3293 `_ * Improve the classes generated by ``bind_map``: `#3310 `_ - * Change ``.items`` from an iterator to a dictionary view. - * Add ``.keys`` and ``.values`` (both dictionary views). - * Allow ``__contains__`` to take any object. + + * Change ``.items`` from an iterator to a dictionary view. + * Add ``.keys`` and ``.values`` (both dictionary views). + * Allow ``__contains__`` to take any object. * ``pybind11::custom_type_setup`` was added, for customizing the ``PyHeapTypeObject`` corresponding to a class, which may be useful for From ed09664f063269dcf6a47298944e45fb957b40c4 Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Fri, 8 Oct 2021 08:27:52 -0400 Subject: [PATCH 206/638] chore(deps): bump ilammy/msvc-dev-cmd from 1.9.0 to 1.10.0 (#3338) Bumps [ilammy/msvc-dev-cmd](https://github.com/ilammy/msvc-dev-cmd) from 1.9.0 to 1.10.0. - [Release notes](https://github.com/ilammy/msvc-dev-cmd/releases) - [Commits](https://github.com/ilammy/msvc-dev-cmd/compare/v1.9.0...v1.10.0) --- updated-dependencies: - dependency-name: ilammy/msvc-dev-cmd dependency-type: direct:production update-type: version-update:semver-minor ... Signed-off-by: dependabot[bot] Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> --- .github/workflows/ci.yml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 6843a92430..5d5222f8b6 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -169,7 +169,7 @@ jobs: # MSVC, but for now, this action works: - name: Prepare compiler environment for Windows 🐍 2.7 if: matrix.python == 2.7 && runner.os == 'Windows' - uses: ilammy/msvc-dev-cmd@v1.9.0 + uses: ilammy/msvc-dev-cmd@v1.10.0 with: arch: x64 @@ -760,7 +760,7 @@ jobs: uses: jwlawson/actions-setup-cmake@v1.11 - name: Prepare MSVC - uses: ilammy/msvc-dev-cmd@v1.9.0 + uses: ilammy/msvc-dev-cmd@v1.10.0 with: arch: x86 @@ -806,7 +806,7 @@ jobs: uses: jwlawson/actions-setup-cmake@v1.11 - name: Prepare MSVC - uses: ilammy/msvc-dev-cmd@v1.9.0 + uses: ilammy/msvc-dev-cmd@v1.10.0 with: toolset: 14.0 From f4c81e0877a7d302eb580886f8552bc81d3b3d65 Mon Sep 17 00:00:00 2001 From: Aaron Gokaslan Date: Fri, 8 Oct 2021 08:38:04 -0400 Subject: [PATCH 207/638] maint: Add additional linter-related pre-commit hooks (#3337) * Add additional pygrep pre-commit hooks * Remove useless noqas with hook * Fix all single rst backticks * Simplify mypy pre-commit hook with upstream fixes * Add back missing comment * Add one last pygrep hook --- .pre-commit-config.yaml | 25 ++++++++++++++++++++----- docs/advanced/classes.rst | 2 +- docs/advanced/embedding.rst | 26 +++++++++++++------------- docs/advanced/exceptions.rst | 6 +++--- docs/advanced/functions.rst | 2 +- docs/advanced/misc.rst | 2 +- docs/advanced/pycpp/utilities.rst | 10 +++++----- docs/changelog.rst | 6 +++--- tests/test_buffers.py | 2 +- tests/test_builtin_casters.py | 2 +- tests/test_callbacks.py | 2 +- tests/test_enum.py | 6 +++--- tests/test_methods_and_attributes.py | 4 ++-- tests/test_pickling.py | 2 +- tests/test_pytypes.py | 4 ++-- tests/test_stl_binders.py | 2 +- 16 files changed, 59 insertions(+), 44 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 2df3146064..17e67ded40 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -67,14 +67,32 @@ repos: hooks: - id: pycln +- repo: https://github.com/pre-commit/pygrep-hooks + rev: v1.9.0 + hooks: + - id: python-check-blanket-noqa + - id: python-check-blanket-type-ignore + - id: python-no-log-warn + - id: rst-backticks + - id: rst-directive-colons + - id: rst-inline-touching-normal + # Flake8 also supports pre-commit natively (same author) - repo: https://github.com/PyCQA/flake8 rev: 3.9.2 hooks: - id: flake8 - additional_dependencies: [flake8-bugbear, pep8-naming] + additional_dependencies: &flake8_dependencies + - flake8-bugbear + - pep8-naming exclude: ^(docs/.*|tools/.*)$ +- repo: https://github.com/asottile/yesqa + rev: v1.2.3 + hooks: + - id: yesqa + additional_dependencies: *flake8_dependencies + # CMake formatting - repo: https://github.com/cheshirekow/cmake-format-precommit rev: v0.6.13 @@ -86,12 +104,9 @@ repos: # Check static types with mypy - repo: https://github.com/pre-commit/mirrors-mypy - rev: v0.910 + rev: v0.910-1 hooks: - id: mypy - # The default Python type ignores .pyi files, so let's rerun if detected - types: [text] - files: ^pybind11.*\.pyi?$ # Running per-file misbehaves a bit, so just run on all files, it's fast pass_filenames: false additional_dependencies: [typed_ast] diff --git a/docs/advanced/classes.rst b/docs/advanced/classes.rst index 5f01a2f11c..6330af5eb4 100644 --- a/docs/advanced/classes.rst +++ b/docs/advanced/classes.rst @@ -1266,7 +1266,7 @@ Custom type setup ================= For advanced use cases, such as enabling garbage collection support, you may -wish to directly manipulate the `PyHeapTypeObject` corresponding to a +wish to directly manipulate the ``PyHeapTypeObject`` corresponding to a ``py::class_`` definition. You can do that using ``py::custom_type_setup``: diff --git a/docs/advanced/embedding.rst b/docs/advanced/embedding.rst index a435b8a75a..dd980d483a 100644 --- a/docs/advanced/embedding.rst +++ b/docs/advanced/embedding.rst @@ -40,15 +40,15 @@ The essential structure of the ``main.cpp`` file looks like this: } The interpreter must be initialized before using any Python API, which includes -all the functions and classes in pybind11. The RAII guard class `scoped_interpreter` +all the functions and classes in pybind11. The RAII guard class ``scoped_interpreter`` takes care of the interpreter lifetime. After the guard is destroyed, the interpreter shuts down and clears its memory. No Python functions can be called after this. Executing Python code ===================== -There are a few different ways to run Python code. One option is to use `eval`, -`exec` or `eval_file`, as explained in :ref:`eval`. Here is a quick example in +There are a few different ways to run Python code. One option is to use ``eval``, +``exec`` or ``eval_file``, as explained in :ref:`eval`. Here is a quick example in the context of an executable with an embedded interpreter: .. code-block:: cpp @@ -108,7 +108,7 @@ The two approaches can also be combined: Importing modules ================= -Python modules can be imported using `module_::import()`: +Python modules can be imported using ``module_::import()``: .. code-block:: cpp @@ -134,7 +134,7 @@ embedding the interpreter. This makes it easy to import local Python files: int n = result.cast(); assert(n == 3); -Modules can be reloaded using `module_::reload()` if the source is modified e.g. +Modules can be reloaded using ``module_::reload()`` if the source is modified e.g. by an external process. This can be useful in scenarios where the application imports a user defined data processing script which needs to be updated after changes by the user. Note that this function does not reload modules recursively. @@ -144,7 +144,7 @@ changes by the user. Note that this function does not reload modules recursively Adding embedded modules ======================= -Embedded binary modules can be added using the `PYBIND11_EMBEDDED_MODULE` macro. +Embedded binary modules can be added using the ``PYBIND11_EMBEDDED_MODULE`` macro. Note that the definition must be placed at global scope. They can be imported like any other module. @@ -170,7 +170,7 @@ like any other module. Unlike extension modules where only a single binary module can be created, on the embedded side an unlimited number of modules can be added using multiple -`PYBIND11_EMBEDDED_MODULE` definitions (as long as they have unique names). +``PYBIND11_EMBEDDED_MODULE`` definitions (as long as they have unique names). These modules are added to Python's list of builtins, so they can also be imported in pure Python files loaded by the interpreter. Everything interacts @@ -216,9 +216,9 @@ naturally: Interpreter lifetime ==================== -The Python interpreter shuts down when `scoped_interpreter` is destroyed. After +The Python interpreter shuts down when ``scoped_interpreter`` is destroyed. After this, creating a new instance will restart the interpreter. Alternatively, the -`initialize_interpreter` / `finalize_interpreter` pair of functions can be used +``initialize_interpreter`` / ``finalize_interpreter`` pair of functions can be used to directly set the state at any time. Modules created with pybind11 can be safely re-initialized after the interpreter @@ -230,8 +230,8 @@ global data. All the details can be found in the CPython documentation. .. warning:: - Creating two concurrent `scoped_interpreter` guards is a fatal error. So is - calling `initialize_interpreter` for a second time after the interpreter + Creating two concurrent ``scoped_interpreter`` guards is a fatal error. So is + calling ``initialize_interpreter`` for a second time after the interpreter has already been initialized. Do not use the raw CPython API functions ``Py_Initialize`` and @@ -242,7 +242,7 @@ global data. All the details can be found in the CPython documentation. Sub-interpreter support ======================= -Creating multiple copies of `scoped_interpreter` is not possible because it +Creating multiple copies of ``scoped_interpreter`` is not possible because it represents the main Python interpreter. Sub-interpreters are something different and they do permit the existence of multiple interpreters. This is an advanced feature of the CPython API and should be handled with care. pybind11 does not @@ -258,5 +258,5 @@ We'll just mention a couple of caveats the sub-interpreters support in pybind11: 2. Managing multiple threads, multiple interpreters and the GIL can be challenging and there are several caveats here, even within the pure CPython API (please refer to the Python docs for details). As for - pybind11, keep in mind that `gil_scoped_release` and `gil_scoped_acquire` + pybind11, keep in mind that ``gil_scoped_release`` and ``gil_scoped_acquire`` do not take sub-interpreters into account. diff --git a/docs/advanced/exceptions.rst b/docs/advanced/exceptions.rst index 2aaa0ad323..b4825cbcde 100644 --- a/docs/advanced/exceptions.rst +++ b/docs/advanced/exceptions.rst @@ -96,18 +96,18 @@ A matching function is available for registering a local exception translator: It is possible to specify base class for the exception using the third -parameter, a `handle`: +parameter, a ``handle``: .. code-block:: cpp py::register_exception(module, "PyExp", PyExc_RuntimeError); py::register_local_exception(module, "PyExp", PyExc_RuntimeError); -Then `PyExp` can be caught both as `PyExp` and `RuntimeError`. +Then ``PyExp`` can be caught both as ``PyExp`` and ``RuntimeError``. The class objects of the built-in Python exceptions are listed in the Python documentation on `Standard Exceptions `_. -The default base class is `PyExc_Exception`. +The default base class is ``PyExc_Exception``. When more advanced exception translation is needed, the functions ``py::register_exception_translator(translator)`` and diff --git a/docs/advanced/functions.rst b/docs/advanced/functions.rst index 1178d07260..abd1084ab5 100644 --- a/docs/advanced/functions.rst +++ b/docs/advanced/functions.rst @@ -232,7 +232,7 @@ is equivalent to the following pseudocode: }); The only requirement is that ``T`` is default-constructible, but otherwise any -scope guard will work. This is very useful in combination with `gil_scoped_release`. +scope guard will work. This is very useful in combination with ``gil_scoped_release``. See :ref:`gil`. Multiple guards can also be specified as ``py::call_guard``. The diff --git a/docs/advanced/misc.rst b/docs/advanced/misc.rst index b3f3b2265a..edab15fcb7 100644 --- a/docs/advanced/misc.rst +++ b/docs/advanced/misc.rst @@ -84,7 +84,7 @@ could be realized as follows (important changes highlighted): }); } -The ``call_go`` wrapper can also be simplified using the `call_guard` policy +The ``call_go`` wrapper can also be simplified using the ``call_guard`` policy (see :ref:`call_policies`) which yields the same result: .. code-block:: cpp diff --git a/docs/advanced/pycpp/utilities.rst b/docs/advanced/pycpp/utilities.rst index bf90a62f8c..af0f9cb2b0 100644 --- a/docs/advanced/pycpp/utilities.rst +++ b/docs/advanced/pycpp/utilities.rst @@ -28,7 +28,7 @@ Capturing standard output from ostream Often, a library will use the streams ``std::cout`` and ``std::cerr`` to print, but this does not play well with Python's standard ``sys.stdout`` and ``sys.stderr`` -redirection. Replacing a library's printing with `py::print ` may not +redirection. Replacing a library's printing with ``py::print `` may not be feasible. This can be fixed using a guard around the library function that redirects output to the corresponding Python streams: @@ -62,9 +62,9 @@ This method respects flushes on the output streams and will flush if needed when the scoped guard is destroyed. This allows the output to be redirected in real time, such as to a Jupyter notebook. The two arguments, the C++ stream and the Python output, are optional, and default to standard output if not given. An -extra type, `py::scoped_estream_redirect `, is identical +extra type, ``py::scoped_estream_redirect ``, is identical except for defaulting to ``std::cerr`` and ``sys.stderr``; this can be useful with -`py::call_guard`, which allows multiple items, but uses the default constructor: +``py::call_guard``, which allows multiple items, but uses the default constructor: .. code-block:: cpp @@ -74,7 +74,7 @@ except for defaulting to ``std::cerr`` and ``sys.stderr``; this can be useful wi py::scoped_estream_redirect>()); The redirection can also be done in Python with the addition of a context -manager, using the `py::add_ostream_redirect() ` function: +manager, using the ``py::add_ostream_redirect() `` function: .. code-block:: cpp @@ -103,7 +103,7 @@ arguments to disable one of the streams if needed. Evaluating Python expressions from strings and files ==================================================== -pybind11 provides the `eval`, `exec` and `eval_file` functions to evaluate +pybind11 provides the ``eval``, ``exec`` and ``eval_file`` functions to evaluate Python expressions and statements. The following example illustrates how they can be used. diff --git a/docs/changelog.rst b/docs/changelog.rst index 80c580b180..0721020db8 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -251,8 +251,8 @@ New features: Changes: -* ``py::str`` changed to exclusively hold `PyUnicodeObject`. Previously - ``py::str`` could also hold `bytes`, which is probably surprising, was +* ``py::str`` changed to exclusively hold ``PyUnicodeObject``. Previously + ``py::str`` could also hold ``bytes``, which is probably surprising, was never documented, and can mask bugs (e.g. accidental use of ``py::str`` instead of ``py::bytes``). `#2409 `_ @@ -1405,7 +1405,7 @@ v2.2.0 (August 31, 2017) * Intel C++ compiler compatibility fixes. `#937 `_. -* Fixed implicit conversion of `py::enum_` to integer types on Python 2.7. +* Fixed implicit conversion of ``py::enum_`` to integer types on Python 2.7. `#821 `_. * Added ``py::hash`` to fetch the hash value of Python objects, and diff --git a/tests/test_buffers.py b/tests/test_buffers.py index e3df7e04f9..adf7cadff2 100644 --- a/tests/test_buffers.py +++ b/tests/test_buffers.py @@ -5,7 +5,7 @@ import pytest -import env # noqa: F401 +import env from pybind11_tests import ConstructorStats from pybind11_tests import buffers as m diff --git a/tests/test_builtin_casters.py b/tests/test_builtin_casters.py index 09b4b08c5e..2a061c193e 100644 --- a/tests/test_builtin_casters.py +++ b/tests/test_builtin_casters.py @@ -1,7 +1,7 @@ # -*- coding: utf-8 -*- import pytest -import env # noqa: F401 +import env from pybind11_tests import IncType, UserType from pybind11_tests import builtin_casters as m diff --git a/tests/test_callbacks.py b/tests/test_callbacks.py index edbb1890c9..f41ad86e7f 100644 --- a/tests/test_callbacks.py +++ b/tests/test_callbacks.py @@ -4,7 +4,7 @@ import pytest -import env # NOQA: F401 +import env # noqa: F401 from pybind11_tests import callbacks as m diff --git a/tests/test_enum.py b/tests/test_enum.py index 85302b0809..14c754e726 100644 --- a/tests/test_enum.py +++ b/tests/test_enum.py @@ -96,13 +96,13 @@ def test_unscoped_enum(): y >= object() # noqa: B015 with pytest.raises(TypeError): - y | object() # noqa: B015 + y | object() with pytest.raises(TypeError): - y & object() # noqa: B015 + y & object() with pytest.raises(TypeError): - y ^ object() # noqa: B015 + y ^ object() assert int(m.UnscopedEnum.ETwo) == 2 assert str(m.UnscopedEnum(2)) == "UnscopedEnum.ETwo" diff --git a/tests/test_methods_and_attributes.py b/tests/test_methods_and_attributes.py index 21909654a5..866b3cea18 100644 --- a/tests/test_methods_and_attributes.py +++ b/tests/test_methods_and_attributes.py @@ -102,7 +102,7 @@ def test_properties(): assert instance.def_property == 3 with pytest.raises(AttributeError) as excinfo: - dummy = instance.def_property_writeonly # noqa: F841 unused var + dummy = instance.def_property_writeonly # unused var assert "unreadable attribute" in str(excinfo.value) instance.def_property_writeonly = 4 @@ -127,7 +127,7 @@ def test_static_properties(): assert m.TestProperties.def_readwrite_static == 2 with pytest.raises(AttributeError) as excinfo: - dummy = m.TestProperties.def_writeonly_static # noqa: F841 unused var + dummy = m.TestProperties.def_writeonly_static # unused var assert "unreadable attribute" in str(excinfo.value) m.TestProperties.def_writeonly_static = 3 diff --git a/tests/test_pickling.py b/tests/test_pickling.py index e39463d209..9f68f37dcf 100644 --- a/tests/test_pickling.py +++ b/tests/test_pickling.py @@ -1,7 +1,7 @@ # -*- coding: utf-8 -*- import pytest -import env # noqa: F401 +import env from pybind11_tests import pickling as m try: diff --git a/tests/test_pytypes.py b/tests/test_pytypes.py index cc27e60a11..5215b79bc1 100644 --- a/tests/test_pytypes.py +++ b/tests/test_pytypes.py @@ -5,7 +5,7 @@ import pytest -import env # noqa: F401 +import env from pybind11_tests import debug_enabled from pybind11_tests import pytypes as m @@ -610,7 +610,7 @@ def callback(wr): obj = WeaklyReferenced() assert getweakrefcount(obj) == 0 - wr = create_weakref(obj) # noqa: F841 + wr = create_weakref(obj) assert getweakrefcount(obj) == 1 obj = WeaklyReferenced() diff --git a/tests/test_stl_binders.py b/tests/test_stl_binders.py index a68dcd31de..59c5ab6b5d 100644 --- a/tests/test_stl_binders.py +++ b/tests/test_stl_binders.py @@ -1,7 +1,7 @@ # -*- coding: utf-8 -*- import pytest -import env # noqa: F401 +import env from pybind11_tests import stl_binders as m From 750e38dcfdac59231a615580d4e6cb3647b31779 Mon Sep 17 00:00:00 2001 From: NaDDu Date: Sun, 10 Oct 2021 00:38:34 +0900 Subject: [PATCH 208/638] Update eval.h (#3344) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit typo correction pybind11/exec.h → pybind11/eval.h --- include/pybind11/eval.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/pybind11/eval.h b/include/pybind11/eval.h index e0f58bcf49..6cc672e2d5 100644 --- a/include/pybind11/eval.h +++ b/include/pybind11/eval.h @@ -1,5 +1,5 @@ /* - pybind11/exec.h: Support for evaluating Python expressions and statements + pybind11/eval.h: Support for evaluating Python expressions and statements from strings and files Copyright (c) 2016 Klemens Morgenstern and From 8a7c266d26e5c9de3beb478bedddcee26f026b04 Mon Sep 17 00:00:00 2001 From: Bruce Merry <1963944+bmerry@users.noreply.github.com> Date: Mon, 11 Oct 2021 17:35:39 +0200 Subject: [PATCH 209/638] Fix make_key_iterator/make_value_iterator for prvalue iterators (#3348) * Add a test showing a flaw in make_key_iterator/make_value_iterator If the iterator dereference operator returns a value rather than a reference (and that pair also does not *contain* references), make_key_iterator and make_value_iterator will return a reference to a temporary, causing a segfault. * Fix make_key_iterator/make_value_iterator for prvalue iterators If an iterator returns a pair rather than a reference to a pair or a pair of references, make_key_iterator and make_value_iterator would return a reference to a temporary, typically leading to a segfault. This is because the value category of member access to a prvalue is an xvalue, not a prvalue, so decltype produces an rvalue reference type. Fix the type calculation to handle this case. I also removed some decltype parentheses that weren't needed, either because the expression isn't one of the special cases for decltype or because decltype was only used for SFINAE. Hopefully that makes the code a bit more readable. Closes #3347 * Attempt a workaround for nvcc --- include/pybind11/pybind11.h | 47 ++++++++++++++++++++------ tests/test_sequences_and_iterators.cpp | 27 ++++++++++++++- tests/test_sequences_and_iterators.py | 7 ++++ 3 files changed, 69 insertions(+), 12 deletions(-) diff --git a/include/pybind11/pybind11.h b/include/pybind11/pybind11.h index 370e52cff6..285f3b18cb 100644 --- a/include/pybind11/pybind11.h +++ b/include/pybind11/pybind11.h @@ -1967,29 +1967,54 @@ struct iterator_state { }; // Note: these helpers take the iterator by non-const reference because some -// iterators in the wild can't be dereferenced when const. C++ needs the extra parens in decltype -// to enforce an lvalue. The & after Iterator is required for MSVC < 16.9. SFINAE cannot be -// reused for result_type due to bugs in ICC, NVCC, and PGI compilers. See PR #3293. -template ()))> +// iterators in the wild can't be dereferenced when const. The & after Iterator +// is required for MSVC < 16.9. SFINAE cannot be reused for result_type due to +// bugs in ICC, NVCC, and PGI compilers. See PR #3293. +template ())> struct iterator_access { - using result_type = decltype((*std::declval())); + using result_type = decltype(*std::declval()); // NOLINTNEXTLINE(readability-const-return-type) // PR #3263 result_type operator()(Iterator &it) const { return *it; } }; -template ()).first)) > -struct iterator_key_access { - using result_type = decltype(((*std::declval()).first)); +template ()).first) > +class iterator_key_access { +private: + using pair_type = decltype(*std::declval()); + +public: + /* If either the pair itself or the element of the pair is a reference, we + * want to return a reference, otherwise a value. When the decltype + * expression is parenthesized it is based on the value category of the + * expression; otherwise it is the declared type of the pair member. + * The use of declval in the second branch rather than directly + * using *std::declval() is a workaround for nvcc + * (it's not used in the first branch because going via decltype and back + * through declval does not perfectly preserve references). + */ + using result_type = conditional_t< + std::is_reference())>::value, + decltype(((*std::declval()).first)), + decltype(std::declval().first) + >; result_type operator()(Iterator &it) const { return (*it).first; } }; -template ()).second))> -struct iterator_value_access { - using result_type = decltype(((*std::declval()).second)); +template ()).second)> +class iterator_value_access { +private: + using pair_type = decltype(*std::declval()); + +public: + using result_type = conditional_t< + std::is_reference())>::value, + decltype(((*std::declval()).second)), + decltype(std::declval().second) + >; result_type operator()(Iterator &it) const { return (*it).second; } diff --git a/tests/test_sequences_and_iterators.cpp b/tests/test_sequences_and_iterators.cpp index 9de69338bc..a378128ae2 100644 --- a/tests/test_sequences_and_iterators.cpp +++ b/tests/test_sequences_and_iterators.cpp @@ -38,6 +38,17 @@ bool operator==(const NonZeroIterator>& it, const NonZeroSentine return !(*it).first || !(*it).second; } +/* Iterator where dereferencing returns prvalues instead of references. */ +template +class NonRefIterator { + const T* ptr_; +public: + explicit NonRefIterator(const T *ptr) : ptr_(ptr) {} + T operator*() const { return T(*ptr_); } + NonRefIterator& operator++() { ++ptr_; return *this; } + bool operator==(const NonRefIterator &other) const { return ptr_ == other.ptr_; } +}; + class NonCopyableInt { public: explicit NonCopyableInt(int value) : value_(value) {} @@ -331,7 +342,7 @@ TEST_SUBMODULE(sequences_and_iterators, m) { py::class_(m, "IntPairs") .def(py::init>>()) .def("nonzero", [](const IntPairs& s) { - return py::make_iterator(NonZeroIterator>(s.begin()), NonZeroSentinel()); + return py::make_iterator(NonZeroIterator>(s.begin()), NonZeroSentinel()); }, py::keep_alive<0, 1>()) .def("nonzero_keys", [](const IntPairs& s) { return py::make_key_iterator(NonZeroIterator>(s.begin()), NonZeroSentinel()); @@ -340,6 +351,20 @@ TEST_SUBMODULE(sequences_and_iterators, m) { return py::make_value_iterator(NonZeroIterator>(s.begin()), NonZeroSentinel()); }, py::keep_alive<0, 1>()) + // test iterator that returns values instead of references + .def("nonref", [](const IntPairs& s) { + return py::make_iterator(NonRefIterator>(s.begin()), + NonRefIterator>(s.end())); + }, py::keep_alive<0, 1>()) + .def("nonref_keys", [](const IntPairs& s) { + return py::make_key_iterator(NonRefIterator>(s.begin()), + NonRefIterator>(s.end())); + }, py::keep_alive<0, 1>()) + .def("nonref_values", [](const IntPairs& s) { + return py::make_value_iterator(NonRefIterator>(s.begin()), + NonRefIterator>(s.end())); + }, py::keep_alive<0, 1>()) + // test single-argument make_iterator .def("simple_iterator", [](IntPairs& self) { return py::make_iterator(self); diff --git a/tests/test_sequences_and_iterators.py b/tests/test_sequences_and_iterators.py index 38e2ab5b75..6985918a11 100644 --- a/tests/test_sequences_and_iterators.py +++ b/tests/test_sequences_and_iterators.py @@ -52,6 +52,13 @@ def test_generalized_iterators(): next(it) +def test_nonref_iterators(): + pairs = m.IntPairs([(1, 2), (3, 4), (0, 5)]) + assert list(pairs.nonref()) == [(1, 2), (3, 4), (0, 5)] + assert list(pairs.nonref_keys()) == [1, 3, 0] + assert list(pairs.nonref_values()) == [2, 4, 5] + + def test_generalized_iterators_simple(): assert list(m.IntPairs([(1, 2), (3, 4), (0, 5)]).simple_iterator()) == [ (1, 2), From 02c05573d9f580bb1bfcc351e2e22e0c9f539b2b Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 11 Oct 2021 16:03:19 -0400 Subject: [PATCH 210/638] [pre-commit.ci] pre-commit autoupdate (#3353) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit updates: - [github.com/PyCQA/flake8: 3.9.2 → 4.0.1](https://github.com/PyCQA/flake8/compare/3.9.2...4.0.1) Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> --- .pre-commit-config.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 17e67ded40..c6652d8367 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -79,7 +79,7 @@ repos: # Flake8 also supports pre-commit natively (same author) - repo: https://github.com/PyCQA/flake8 - rev: 3.9.2 + rev: 4.0.1 hooks: - id: flake8 additional_dependencies: &flake8_dependencies From 7c580586f853b5f48603594ff0c11a5211de9007 Mon Sep 17 00:00:00 2001 From: "Ralf W. Grosse-Kunstleve" Date: Mon, 11 Oct 2021 13:13:01 -0700 Subject: [PATCH 211/638] Correct options on Eigen::MappedSparseMatrix & adding MSVC C4127 suppression around Eigen includes. (#3352) * Adding MSVC C4127 suppression around Eigen includes. * For MSVC 2015 only: also adding the C4127 suppression to test_eigen.cpp * Copying original change from PR #3343, with extra line breaks to not run past 99 columns (our desired but currently not enforced limit). --- include/pybind11/eigen.h | 18 +++++++++++++++++- tests/CMakeLists.txt | 4 ++-- tests/test_eigen.cpp | 3 +++ 3 files changed, 22 insertions(+), 3 deletions(-) diff --git a/include/pybind11/eigen.h b/include/pybind11/eigen.h index c0363827c3..97b1d96b09 100644 --- a/include/pybind11/eigen.h +++ b/include/pybind11/eigen.h @@ -17,9 +17,23 @@ #include "numpy.h" +// The C4127 suppression was introduced for Eigen 3.4.0. In theory we could +// make it version specific, or even remove it later, but considering that +// 1. C4127 is generally far more distracting than useful for modern template code, and +// 2. we definitely want to ignore any MSVC warnings originating from Eigen code, +// it is probably best to keep this around indefinitely. +#if defined(_MSC_VER) +# pragma warning(push) +# pragma warning(disable: 4127) // C4127: conditional expression is constant +#endif + #include #include +#if defined(_MSC_VER) +# pragma warning(pop) +#endif + // Eigen prior to 3.2.7 doesn't have proper move constructors--but worse, some classes get implicit // move constructors that break things. We could detect this an explicitly copy, but an extra copy // of matrices seems highly undesirable. @@ -559,7 +573,9 @@ struct type_caster::value>> { if (!values || !innerIndices || !outerIndices) return false; - value = Eigen::MappedSparseMatrix( + value = Eigen::MappedSparseMatrix( shape[0].cast(), shape[1].cast(), nnz, outerIndices.mutable_data(), innerIndices.mutable_data(), values.mutable_data()); diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 6ebcba9b6b..d4e7b71e1d 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -174,9 +174,9 @@ set(PYBIND11_CROSS_MODULE_GIL_TESTS test_gil_scoped.py) set(PYBIND11_EIGEN_REPO "https://gitlab.com/libeigen/eigen.git" CACHE STRING "Eigen repository to use for tests") -# This hash is for 3.3.8, using a hash for security reasons +# This hash is for 3.4.0, using a hash for security reasons set(PYBIND11_EIGEN_VERSION - "dc252fbf00079ccab57948a164b1421703fe4361" + "929bc0e191d0927b1735b9a1ddc0e8b77e3a25ec" CACHE STRING "Eigen version to use for tests") # Check if Eigen is available; if not, remove from PYBIND11_TEST_FILES (but diff --git a/tests/test_eigen.cpp b/tests/test_eigen.cpp index ad572b2ad7..d22a94a1a1 100644 --- a/tests/test_eigen.cpp +++ b/tests/test_eigen.cpp @@ -13,6 +13,9 @@ #include #if defined(_MSC_VER) +#if _MSC_VER < 1910 // VS 2015's MSVC +# pragma warning(disable: 4127) // C4127: conditional expression is constant +#endif # pragma warning(disable: 4996) // C4996: std::unary_negation is deprecated #endif From 56b49c2be27dbe440f4236a5ca711950aa788e63 Mon Sep 17 00:00:00 2001 From: Jerome Robert Date: Mon, 18 Oct 2021 03:38:41 +0200 Subject: [PATCH 212/638] ci: fix mingw checks by pinning (#3375) * Workaround for https://github.com/msys2/setup-msys2/issues/167 * Adapted from https://github.com/cocotb/cocotb/commit/05036cb24d10579647e81dc1a405b126f711a66f --- .github/workflows/ci.yml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 5d5222f8b6..e337770ebd 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -892,7 +892,8 @@ jobs: - { sys: mingw64, env: x86_64 } - { sys: mingw32, env: i686 } steps: - - uses: msys2/setup-msys2@v2 + # Force version because of https://github.com/msys2/setup-msys2/issues/167 + - uses: msys2/setup-msys2@v2.4.2 with: msystem: ${{matrix.sys}} install: >- From 931f66440f932c62a38e8112ea8ec2f7d962e64a Mon Sep 17 00:00:00 2001 From: Henry Schreiner Date: Sun, 17 Oct 2021 21:40:10 -0400 Subject: [PATCH 213/638] ci: cancel in-progress on repeated pushes (#3370) --- .github/workflows/ci.yml | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index e337770ebd..09b4000562 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -9,6 +9,10 @@ on: - stable - v* +concurrency: + group: test-${{ github.ref }} + cancel-in-progress: true + jobs: # This is the "main" test suite, which tests a large number of different # versions of default compilers and Python versions in GitHub Actions. From f791dc8648e1f6ec33f402d679b6b116a76d4e1b Mon Sep 17 00:00:00 2001 From: Henry Schreiner Date: Tue, 19 Oct 2021 14:39:29 -0400 Subject: [PATCH 214/638] fix: deprecate make_simple_namespace, fix Python 3.11 (#3374) * fix: deprecate make_simple_namespace, fix Python 3.11 * docs: update links --- docs/advanced/pycpp/object.rst | 13 +++---------- docs/changelog.rst | 8 ++++++-- docs/upgrade.rst | 13 ++++++++++++- include/pybind11/cast.h | 10 ---------- include/pybind11/pybind11.h | 9 +++++++++ tests/test_pytypes.cpp | 2 +- 6 files changed, 31 insertions(+), 24 deletions(-) diff --git a/docs/advanced/pycpp/object.rst b/docs/advanced/pycpp/object.rst index 8bffb83e18..93e1a94d8f 100644 --- a/docs/advanced/pycpp/object.rst +++ b/docs/advanced/pycpp/object.rst @@ -41,24 +41,17 @@ A tuple of python objects can be instantiated using :func:`py::make_tuple`: Each element is converted to a supported Python type. A `simple namespace`_ can be instantiated using -:func:`py::make_simple_namespace`: .. code-block:: cpp - using namespace pybind11::literals; // to bring in the `_a` literal - py::object ns = py::make_simple_namespace("spam"_a=py::none(), "eggs"_a=42); + using namespace pybind11::literals; // to bring in the `_a` literal + py::object SimpleNamespace = py::module_::import("types").attr("SimpleNamespace"); + py::object ns = SimpleNamespace("spam"_a=py::none(), "eggs"_a=42); Attributes on a namespace can be modified with the :func:`py::delattr`, :func:`py::getattr`, and :func:`py::setattr` functions. Simple namespaces can be useful as lightweight stand-ins for class instances. -.. note:: - - ``make_simple_namespace`` is not available in Python 2. - -.. versionchanged:: 2.8 - ``make_simple_namespace`` added. - .. _simple namespace: https://docs.python.org/3/library/types.html#types.SimpleNamespace .. _casting_back_and_forth: diff --git a/docs/changelog.rst b/docs/changelog.rst index 0721020db8..ee79a18719 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -10,6 +10,10 @@ Starting with version 1.8.0, pybind11 releases use a `semantic versioning IN DEVELOPMENT -------------- +* The simple namespace creation shortcut added in 2.8.0 was deprecated due to + usage of CPython internal API, and will be removed soon. Use + ``py::module_::import("types").attr("SimpleNamespace")``. + `#3374 `_ v2.8.0 (Oct 4, 2021) @@ -25,10 +29,10 @@ New features: ``register_local_exception_translator(ExceptionTranslator&& translator)`` instead of ``register_exception_translator(ExceptionTranslator&& translator)`` to keep your exception remapping code local to the module. - `#2650 `_ + `#2650 `_ * Add ``make_simple_namespace`` function for instantiating Python - ``SimpleNamespace`` objects. + ``SimpleNamespace`` objects. **Deprecated in 2.8.1.** `#2840 `_ * ``pybind11::scoped_interpreter`` and ``initialize_interpreter`` have new diff --git a/docs/upgrade.rst b/docs/upgrade.rst index b2b6b12456..69609ca284 100644 --- a/docs/upgrade.rst +++ b/docs/upgrade.rst @@ -8,7 +8,17 @@ to a new version. But it goes into more detail. This includes things like deprecated APIs and their replacements, build system changes, general code modernization and other useful information. -.. _upgrade-guide-2.6: +.. _upgrade-guide-2.9: + +v2.9 +==== + +* Any usage of the recently added ``py::make_simple_namespace`` should be + converted to using ``py::module_::import("types").attr("SimpleNamespace")`` + instead. + + +.. _upgrade-guide-2.7: v2.7 ==== @@ -34,6 +44,7 @@ to be common: careful review and custom fixes. +.. _upgrade-guide-2.6: v2.6 ==== diff --git a/include/pybind11/cast.h b/include/pybind11/cast.h index 1ec2080f8a..20fbb32587 100644 --- a/include/pybind11/cast.h +++ b/include/pybind11/cast.h @@ -1036,16 +1036,6 @@ template = 0x03030000 -template ()>> -object make_simple_namespace(Args&&... args_) { - PyObject *ns = _PyNamespace_New(dict(std::forward(args_)...).ptr()); - if (!ns) throw error_already_set(); - return reinterpret_steal(ns); -} -#endif - /// \ingroup annotations /// Annotation for arguments struct arg { diff --git a/include/pybind11/pybind11.h b/include/pybind11/pybind11.h index 285f3b18cb..095efd9c31 100644 --- a/include/pybind11/pybind11.h +++ b/include/pybind11/pybind11.h @@ -1124,6 +1124,15 @@ inline dict globals() { return reinterpret_borrow(p ? p : module_::import("__main__").attr("__dict__").ptr()); } +#if PY_VERSION_HEX >= 0x03030000 +template ()>> +PYBIND11_DEPRECATED("make_simple_namespace should be replaced with py::module_::import(\"types\").attr(\"SimpleNamespace\") ") +object make_simple_namespace(Args&&... args_) { + return module_::import("types").attr("SimpleNamespace")(std::forward(args_)...); +} +#endif + PYBIND11_NAMESPACE_BEGIN(detail) /// Generic support for creating new Python heap types class generic_type : public object { diff --git a/tests/test_pytypes.cpp b/tests/test_pytypes.cpp index 2157dc0973..9a1e918818 100644 --- a/tests/test_pytypes.cpp +++ b/tests/test_pytypes.cpp @@ -84,7 +84,7 @@ TEST_SUBMODULE(pytypes, m) { #if PY_VERSION_HEX >= 0x03030000 // test_simple_namespace m.def("get_simple_namespace", []() { - auto ns = py::make_simple_namespace("attr"_a=42, "x"_a="foo", "wrong"_a=1); + auto ns = py::module_::import("types").attr("SimpleNamespace")("attr"_a=42, "x"_a="foo", "wrong"_a=1); py::delattr(ns, "wrong"); py::setattr(ns, "right", py::int_(2)); return ns; From 2d6014e417a9be926eb9a3c490570707be35cd42 Mon Sep 17 00:00:00 2001 From: Geoffrey Gunter <12984092+gmgunter@users.noreply.github.com> Date: Thu, 21 Oct 2021 07:37:54 -0700 Subject: [PATCH 215/638] docs: fix minor typo (#3390) --- include/pybind11/numpy.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/pybind11/numpy.h b/include/pybind11/numpy.h index b7747fae2d..b43a771684 100644 --- a/include/pybind11/numpy.h +++ b/include/pybind11/numpy.h @@ -518,7 +518,7 @@ class dtype : public object { } /// Single-character for dtype's type. - /// For example, ``float`` is 'f', ``double`` 'd', ``int`` 'i', and ``long`` 'd'. + /// For example, ``float`` is 'f', ``double`` 'd', ``int`` 'i', and ``long`` 'l'. char char_() const { // Note: The signature, `dtype::char_` follows the naming of NumPy's // public Python API (i.e., ``dtype.char``), rather than its internal From 606f81a966e56d1ec0f17b0c032e8d6679430d67 Mon Sep 17 00:00:00 2001 From: Henry Schreiner Date: Fri, 22 Oct 2021 16:38:40 -0400 Subject: [PATCH 216/638] style: drop pycln (#3397) --- .pre-commit-config.yaml | 6 ------ 1 file changed, 6 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index c6652d8367..04166de9aa 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -61,12 +61,6 @@ repos: hooks: - id: remove-tabs -# Autoremoves unused imports -- repo: https://github.com/hadialqattan/pycln - rev: v1.0.3 - hooks: - - id: pycln - - repo: https://github.com/pre-commit/pygrep-hooks rev: v1.9.0 hooks: From 076c89fc54d3859bb56581555897fda1c621405c Mon Sep 17 00:00:00 2001 From: Dmitry Yershov Date: Fri, 22 Oct 2021 17:09:15 -0400 Subject: [PATCH 217/638] tests: test recursive dispatch using visitor pattern (#3365) --- tests/test_virtual_functions.cpp | 40 ++++++++++++++++++++++++++++++++ tests/test_virtual_functions.py | 33 ++++++++++++++++++++++++++ 2 files changed, 73 insertions(+) diff --git a/tests/test_virtual_functions.cpp b/tests/test_virtual_functions.cpp index 1eba534dd2..6e06db9fc1 100644 --- a/tests/test_virtual_functions.cpp +++ b/tests/test_virtual_functions.cpp @@ -174,6 +174,25 @@ struct DispatchIssue : Base { } }; +// An abstract adder class that uses visitor pattern to add two data +// objects and send the result to the visitor functor +struct AdderBase { + struct Data {}; + using DataVisitor = std::function; + + virtual void operator()(const Data& first, const Data& second, const DataVisitor& visitor) const = 0; + virtual ~AdderBase() = default; + AdderBase() = default; + AdderBase(const AdderBase&) = delete; +}; + +struct Adder : AdderBase { + void operator()(const Data& first, const Data& second, const DataVisitor& visitor) const override { + PYBIND11_OVERRIDE_PURE_NAME(void, AdderBase, "__call__", operator(), first, second, visitor); + } +}; + + static void test_gil() { { py::gil_scoped_acquire lock; @@ -295,6 +314,27 @@ TEST_SUBMODULE(virtual_functions, m) { m.def("dispatch_issue_go", [](const Base * b) { return b->dispatch(); }); + // test_recursive_dispatch_issue + // #3357: Recursive dispatch fails to find python function override + pybind11::class_(m, "Adder") + .def(pybind11::init<>()) + .def("__call__", &AdderBase::operator()); + + pybind11::class_(m, "Data") + .def(pybind11::init<>()); + + m.def("add2", [](const AdderBase::Data& first, const AdderBase::Data& second, + const AdderBase& adder, const AdderBase::DataVisitor& visitor) { + adder(first, second, visitor); + }); + + m.def("add3", [](const AdderBase::Data& first, const AdderBase::Data& second, const AdderBase::Data& third, + const AdderBase& adder, const AdderBase::DataVisitor& visitor) { + adder(first, second, [&] (const AdderBase::Data& first_plus_second) { + adder(first_plus_second, third, visitor); + }); + }); + // test_override_ref // #392/397: overriding reference-returning functions class OverrideTest { diff --git a/tests/test_virtual_functions.py b/tests/test_virtual_functions.py index f7d3bd1e4b..0b550992f1 100644 --- a/tests/test_virtual_functions.py +++ b/tests/test_virtual_functions.py @@ -257,6 +257,39 @@ def dispatch(self): assert m.dispatch_issue_go(b) == "Yay.." +def test_recursive_dispatch_issue(msg): + """#3357: Recursive dispatch fails to find python function override""" + + class Data(m.Data): + def __init__(self, value): + super(Data, self).__init__() + self.value = value + + class Adder(m.Adder): + def __call__(self, first, second, visitor): + # lambda is a workaround, which adds extra frame to the + # current CPython thread. Removing lambda reveals the bug + # [https://github.com/pybind/pybind11/issues/3357] + (lambda: visitor(Data(first.value + second.value)))() + + class StoreResultVisitor: + def __init__(self): + self.result = None + + def __call__(self, data): + self.result = data.value + + store = StoreResultVisitor() + + m.add2(Data(1), Data(2), Adder(), store) + assert store.result == 3 + + # without lambda in Adder class, this function fails with + # RuntimeError: Tried to call pure virtual function "AdderBase::__call__" + m.add3(Data(1), Data(2), Data(3), Adder(), store) + assert store.result == 6 + + def test_override_ref(): """#392/397: overriding reference-returning functions""" o = m.OverrideTest("asdf") From 78ee782bd494debcbc166f717379a4f9c2093c0f Mon Sep 17 00:00:00 2001 From: Aaron Gokaslan Date: Sat, 23 Oct 2021 00:07:22 -0400 Subject: [PATCH 218/638] feat: Add C++ binding to throw AttributeError (#3387) * Add C++ bindings to throw AttributeError * Fix formatting bug --- docs/advanced/exceptions.rst | 4 +++- include/pybind11/detail/common.h | 1 + 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/docs/advanced/exceptions.rst b/docs/advanced/exceptions.rst index b4825cbcde..40f67d7b83 100644 --- a/docs/advanced/exceptions.rst +++ b/docs/advanced/exceptions.rst @@ -56,7 +56,9 @@ at its exception handler. +--------------------------------------+--------------------------------------+ | :class:`pybind11::buffer_error` | ``BufferError`` | +--------------------------------------+--------------------------------------+ -| :class:`pybind11::import_error` | ``import_error`` | +| :class:`pybind11::import_error` | ``ImportError`` | ++--------------------------------------+--------------------------------------+ +| :class:`pybind11::attribute_error` | ``AttributeError`` | +--------------------------------------+--------------------------------------+ | Any other exception | ``RuntimeError`` | +--------------------------------------+--------------------------------------+ diff --git a/include/pybind11/detail/common.h b/include/pybind11/detail/common.h index 9ad305a4ee..862451fd13 100644 --- a/include/pybind11/detail/common.h +++ b/include/pybind11/detail/common.h @@ -831,6 +831,7 @@ PYBIND11_RUNTIME_EXCEPTION(value_error, PyExc_ValueError) PYBIND11_RUNTIME_EXCEPTION(type_error, PyExc_TypeError) PYBIND11_RUNTIME_EXCEPTION(buffer_error, PyExc_BufferError) PYBIND11_RUNTIME_EXCEPTION(import_error, PyExc_ImportError) +PYBIND11_RUNTIME_EXCEPTION(attribute_error, PyExc_AttributeError) PYBIND11_RUNTIME_EXCEPTION(cast_error, PyExc_RuntimeError) /// Thrown when pybind11::cast or handle::call fail due to a type casting error PYBIND11_RUNTIME_EXCEPTION(reference_cast_error, PyExc_RuntimeError) /// Used internally From 9379b399d99a784f7e226f135fb117aff6db8013 Mon Sep 17 00:00:00 2001 From: Henry Schreiner Date: Mon, 25 Oct 2021 16:01:19 -0400 Subject: [PATCH 219/638] fix: MSVC 2017 C++17 on Python 3 regression (#3407) * fix: MSVC 2017 C++17 on Python 3 regression * ci: add 3.7 job on CI --- .github/workflows/ci.yml | 4 ++++ include/pybind11/functional.h | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 09b4000562..b027640df1 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -851,6 +851,10 @@ jobs: std: 17 args: > -DCMAKE_CXX_FLAGS="/permissive- /EHsc /GR" + - python: 3.7 + std: 17 + args: > + -DCMAKE_CXX_FLAGS="/permissive- /EHsc /GR" steps: - uses: actions/checkout@v2 diff --git a/include/pybind11/functional.h b/include/pybind11/functional.h index 24141ce388..ad5608c255 100644 --- a/include/pybind11/functional.h +++ b/include/pybind11/functional.h @@ -69,7 +69,7 @@ struct type_caster> { // ensure GIL is held during functor destruction struct func_handle { function f; -#if !(defined(_MSC_VER) && _MSC_VER == 1916 && defined(PYBIND11_CPP17) && PY_MAJOR_VERSION < 3) +#if !(defined(_MSC_VER) && _MSC_VER == 1916 && defined(PYBIND11_CPP17)) // This triggers a syntax error under very special conditions (very weird indeed). explicit #endif From d45a88105c3dab1dc2a1da831f1c83a0f3b57f8b Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 25 Oct 2021 16:06:13 -0400 Subject: [PATCH 220/638] [pre-commit.ci] pre-commit autoupdate (#3409) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit updates: - [github.com/asottile/yesqa: v1.2.3 → v1.3.0](https://github.com/asottile/yesqa/compare/v1.2.3...v1.3.0) Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> --- .pre-commit-config.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 04166de9aa..b3e5179055 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -82,7 +82,7 @@ repos: exclude: ^(docs/.*|tools/.*)$ - repo: https://github.com/asottile/yesqa - rev: v1.2.3 + rev: v1.3.0 hooks: - id: yesqa additional_dependencies: *flake8_dependencies From c2d3e220bd1bfcf664e29d95ac2ed774fd6723f4 Mon Sep 17 00:00:00 2001 From: Ryan Cahoon Date: Mon, 25 Oct 2021 19:04:45 -0700 Subject: [PATCH 221/638] fix: the types for return_value_policy_override in optional_caster (#3376) * fix: the types for return_value_policy_override in optional_caster `return_value_policy_override` was not being applied correctly in `optional_caster` in two ways: - The `is_lvalue_reference` condition referenced `T`, which was the `optional` type parameter from the class, when it should have used `T_`, which was the parameter to the `cast` function. `T_` can potentially be a reference type, but `T` will never be. - The type parameter passed to `return_value_policy_override` should be `T::value_type`, not `T`. This matches the way that the other STL container type casters work. The result of these issues was that a method/property definition which used a `reference` or `reference_internal` return value policy would create a Python value that's bound by reference to a temporary C++ object, resulting in undefined behavior. For reasons that I was not able to figure out fully, it seems like this causes problems when using old versions of `boost::optional`, but not with recent versions of `boost::optional` or the `libstdc++` implementation of `std::optional`. The issue (that the override to `return_value_policy::move` is never being applied) is present for all implementations, it just seems like that somehow doesn't result in problems for the some implementation of `optional`. This change includes a regression type with a custom optional-like type which was able to reproduce the issue. Part of the issue with using the wrong types may have stemmed from the type variables `T` and `T_` having very similar names. This also changes the type variables in `optional_caster` to use slightly more descriptive names, which also more closely follow the naming convention used by the other STL casters. Fixes #3330 * Fix clang-tidy complaints * Add missing NOLINT * Apply a couple more fixes * fix: support GCC 4.8 * tests: avoid warning about unknown compiler for compilers missing C++17 * Remove unneeded test module attribute * Change test enum to have more unique int values Co-authored-by: Aaron Gokaslan Co-authored-by: Henry Schreiner --- include/pybind11/stl.h | 16 ++-- tests/CMakeLists.txt | 6 +- tests/test_stl.cpp | 189 ++++++++++++++++++++++++++++++++++++++++- tests/test_stl.py | 67 +++++++++++++++ 4 files changed, 265 insertions(+), 13 deletions(-) diff --git a/include/pybind11/stl.h b/include/pybind11/stl.h index 2c017b4fef..3608d2989c 100644 --- a/include/pybind11/stl.h +++ b/include/pybind11/stl.h @@ -245,17 +245,17 @@ template , Key, Value> { }; // This type caster is intended to be used for std::optional and std::experimental::optional -template struct optional_caster { - using value_conv = make_caster; +template struct optional_caster { + using value_conv = make_caster; - template - static handle cast(T_ &&src, return_value_policy policy, handle parent) { + template + static handle cast(T &&src, return_value_policy policy, handle parent) { if (!src) return none().inc_ref(); if (!std::is_lvalue_reference::value) { - policy = return_value_policy_override::policy(policy); + policy = return_value_policy_override::policy(policy); } - return value_conv::cast(*std::forward(src), policy, parent); + return value_conv::cast(*std::forward(src), policy, parent); } bool load(handle src, bool convert) { @@ -269,11 +269,11 @@ template struct optional_caster { if (!inner_caster.load(src, convert)) return false; - value.emplace(cast_op(std::move(inner_caster))); + value.emplace(cast_op(std::move(inner_caster))); return true; } - PYBIND11_TYPE_CASTER(T, _("Optional[") + value_conv::name + _("]")); + PYBIND11_TYPE_CASTER(Type, _("Optional[") + value_conv::name + _("]")); }; #if defined(PYBIND11_HAS_OPTIONAL) diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index d4e7b71e1d..e1b18dd74b 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -256,7 +256,9 @@ if(Boost_FOUND) endif() # Check if we need to add -lstdc++fs or -lc++fs or nothing -if(MSVC) +if(DEFINED CMAKE_CXX_STANDARD AND CMAKE_CXX_STANDARD LESS 17) + set(STD_FS_NO_LIB_NEEDED TRUE) +elseif(MSVC) set(STD_FS_NO_LIB_NEEDED TRUE) else() file( @@ -286,7 +288,7 @@ elseif(${STD_FS_NEEDS_CXXFS}) elseif(${STD_FS_NO_LIB_NEEDED}) set(STD_FS_LIB "") else() - message(WARNING "Unknown compiler - not passing -lstdc++fs") + message(WARNING "Unknown C++17 compiler - not passing -lstdc++fs") set(STD_FS_LIB "") endif() diff --git a/tests/test_stl.cpp b/tests/test_stl.cpp index 7e3363c5ea..bc5c6553a2 100644 --- a/tests/test_stl.cpp +++ b/tests/test_stl.cpp @@ -19,6 +19,18 @@ #include #include +#if defined(PYBIND11_TEST_BOOST) +#include + +namespace pybind11 { namespace detail { +template +struct type_caster> : optional_caster> {}; + +template <> +struct type_caster : void_caster {}; +}} // namespace pybind11::detail +#endif + // Test with `std::variant` in C++17 mode, or with `boost::variant` in C++11/14 #if defined(PYBIND11_HAS_VARIANT) using std::variant; @@ -59,7 +71,8 @@ namespace std { template