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 01/25] [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 02/25] 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 03/25] 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 5f34c42d70d6af0af546106503da9d045efb904e Mon Sep 17 00:00:00 2001 From: Henry Schreiner Date: Tue, 3 Aug 2021 15:05:54 -0400 Subject: [PATCH 04/25] 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 05/25] 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 c30f57d2ede50c0306db54d5fb215f57c1e465a6 Mon Sep 17 00:00:00 2001 From: Henry Schreiner Date: Tue, 3 Aug 2021 16:11:07 -0400 Subject: [PATCH 06/25] 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 07/25] 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 08/25] 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 09/25] 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 10/25] 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 11/25] 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 12/25] 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 13/25] 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 14/25] 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 15/25] 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 16/25] [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 17/25] 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 18/25] 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 19/25] 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 20/25] 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 191769c29bd26d65de4edaf41a8b252a3174a903 Mon Sep 17 00:00:00 2001 From: "Ralf W. Grosse-Kunstleve" Date: Sat, 14 Aug 2021 08:34:52 -0700 Subject: [PATCH 21/25] Automatic isort fixes. --- tests/test_class_sh_disowning_mi.py | 1 - tests/test_class_sh_module_local.py | 3 +-- tests/test_class_sh_trampoline_shared_from_this.py | 5 ++--- ubench/holder_comparison.py | 8 +++----- ubench/holder_comparison_extract_sheet_data.py | 4 +--- 5 files changed, 7 insertions(+), 14 deletions(-) diff --git a/tests/test_class_sh_disowning_mi.py b/tests/test_class_sh_disowning_mi.py index 61cb9c6a25..80c938f218 100644 --- a/tests/test_class_sh_disowning_mi.py +++ b/tests/test_class_sh_disowning_mi.py @@ -2,7 +2,6 @@ import pytest import env # noqa: F401 - from pybind11_tests import class_sh_disowning_mi as m diff --git a/tests/test_class_sh_module_local.py b/tests/test_class_sh_module_local.py index aa4d1f3c5d..98b161d6cf 100644 --- a/tests/test_class_sh_module_local.py +++ b/tests/test_class_sh_module_local.py @@ -1,9 +1,8 @@ # -*- coding: utf-8 -*- -import pytest - import class_sh_module_local_0 as m0 import class_sh_module_local_1 as m1 import class_sh_module_local_2 as m2 +import pytest def test_cross_module_get_mtxt(): diff --git a/tests/test_class_sh_trampoline_shared_from_this.py b/tests/test_class_sh_trampoline_shared_from_this.py index d71a72068c..bb3c832525 100644 --- a/tests/test_class_sh_trampoline_shared_from_this.py +++ b/tests/test_class_sh_trampoline_shared_from_this.py @@ -1,10 +1,9 @@ # -*- coding: utf-8 -*- +import weakref + import pytest import env # noqa: F401 - -import weakref - import pybind11_tests.class_sh_trampoline_shared_from_this as m diff --git a/ubench/holder_comparison.py b/ubench/holder_comparison.py index 0dbdbbaf09..0fb3289026 100644 --- a/ubench/holder_comparison.py +++ b/ubench/holder_comparison.py @@ -1,16 +1,14 @@ # -*- coding: utf-8 -*- """Simple comparison of holder performances, relative to unique_ptr holder.""" -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -import pybind11_ubench_holder_comparison as m +from __future__ import absolute_import, division, print_function import collections import sys import time +import pybind11_ubench_holder_comparison as m + number_bucket_pc = None diff --git a/ubench/holder_comparison_extract_sheet_data.py b/ubench/holder_comparison_extract_sheet_data.py index 64ee27fc48..bae11b9a60 100644 --- a/ubench/holder_comparison_extract_sheet_data.py +++ b/ubench/holder_comparison_extract_sheet_data.py @@ -1,9 +1,7 @@ # -*- coding: utf-8 -*- """Extract mean ratios from holder_comparison.py output.""" -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function +from __future__ import absolute_import, division, print_function import sys From 68d1aa83aca566f29eb10a287372ea07e0bd1f18 Mon Sep 17 00:00:00 2001 From: "Ralf W. Grosse-Kunstleve" Date: Sat, 14 Aug 2021 08:38:56 -0700 Subject: [PATCH 22/25] Tracking ci.yml changes from master. --- .github/workflows/ci_sh_def.yml | 22 +++++++++++----------- .github/workflows/ci_sh_def.yml.patch | 4 ++-- 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/.github/workflows/ci_sh_def.yml b/.github/workflows/ci_sh_def.yml index 09bd9a9140..39cd36d701 100644 --- a/.github/workflows/ci_sh_def.yml +++ b/.github/workflows/ci_sh_def.yml @@ -88,7 +88,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' @@ -208,7 +208,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 @@ -463,7 +463,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 @@ -615,13 +615,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 @@ -687,7 +687,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 @@ -761,7 +761,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: - name: Build C++11 run: cmake --build build -j 2 - - name: Run tests + - name: Python tests run: cmake --build build -t pytest win32-msvc2015: @@ -808,7 +808,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 @@ -864,7 +864,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/ci_sh_def.yml.patch b/.github/workflows/ci_sh_def.yml.patch index cf3520d96d..83aeaa60cd 100644 --- a/.github/workflows/ci_sh_def.yml.patch +++ b/.github/workflows/ci_sh_def.yml.patch @@ -1,5 +1,5 @@ ---- ci.yml 2021-07-30 11:29:33.606510028 -0700 -+++ ci_sh_def.yml 2021-07-30 12:20:02.822962583 -0700 +--- ci.yml 2021-08-14 07:51:13.991347947 -0700 ++++ ci_sh_def.yml 2021-08-14 08:36:43.163463393 -0700 @@ -1,4 +1,16 @@ -name: CI-SH-AVL +# PLEASE KEEP THIS GROUP OF FILES IN SYNC AT ALL TIMES: From e3e1d29fdb25429d9febf2e2f61e616ec79b7dad Mon Sep 17 00:00:00 2001 From: "Ralf W. Grosse-Kunstleve" Date: Sat, 14 Aug 2021 08:42:38 -0700 Subject: [PATCH 23/25] Removing obsolete detail/pragma_warning_block.h --- CMakeLists.txt | 1 - include/pybind11/detail/dynamic_raw_ptr_cast_if_possible.h | 2 -- include/pybind11/detail/pragma_warning_block.h | 7 ------- include/pybind11/detail/smart_holder_sfinae_hooks_only.h | 2 -- include/pybind11/detail/smart_holder_type_casters.h | 2 -- include/pybind11/smart_holder.h | 2 -- include/pybind11/trampoline_self_life_support.h | 2 -- tests/extra_python_package/test_files.py | 1 - 8 files changed, 19 deletions(-) delete mode 100644 include/pybind11/detail/pragma_warning_block.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 0e955df76e..45d85e7a66 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -106,7 +106,6 @@ set(PYBIND11_HEADERS include/pybind11/detail/dynamic_raw_ptr_cast_if_possible.h include/pybind11/detail/init.h include/pybind11/detail/internals.h - include/pybind11/detail/pragma_warning_block.h include/pybind11/detail/smart_holder_poc.h include/pybind11/detail/smart_holder_sfinae_hooks_only.h include/pybind11/detail/smart_holder_type_casters.h diff --git a/include/pybind11/detail/dynamic_raw_ptr_cast_if_possible.h b/include/pybind11/detail/dynamic_raw_ptr_cast_if_possible.h index 29fded53b3..7c00fe98c1 100644 --- a/include/pybind11/detail/dynamic_raw_ptr_cast_if_possible.h +++ b/include/pybind11/detail/dynamic_raw_ptr_cast_if_possible.h @@ -4,8 +4,6 @@ #pragma once -#include "pragma_warning_block.h" - #include "common.h" #include diff --git a/include/pybind11/detail/pragma_warning_block.h b/include/pybind11/detail/pragma_warning_block.h deleted file mode 100644 index bedc65da9d..0000000000 --- a/include/pybind11/detail/pragma_warning_block.h +++ /dev/null @@ -1,7 +0,0 @@ -#pragma once - -#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 diff --git a/include/pybind11/detail/smart_holder_sfinae_hooks_only.h b/include/pybind11/detail/smart_holder_sfinae_hooks_only.h index d0a52b9a27..f324854751 100644 --- a/include/pybind11/detail/smart_holder_sfinae_hooks_only.h +++ b/include/pybind11/detail/smart_holder_sfinae_hooks_only.h @@ -4,8 +4,6 @@ #pragma once -#include "pragma_warning_block.h" - #include "common.h" #include diff --git a/include/pybind11/detail/smart_holder_type_casters.h b/include/pybind11/detail/smart_holder_type_casters.h index a72de90e01..7ac9a5d351 100644 --- a/include/pybind11/detail/smart_holder_type_casters.h +++ b/include/pybind11/detail/smart_holder_type_casters.h @@ -4,8 +4,6 @@ #pragma once -#include "pragma_warning_block.h" - #include "../gil.h" #include "../pytypes.h" #include "../trampoline_self_life_support.h" diff --git a/include/pybind11/smart_holder.h b/include/pybind11/smart_holder.h index 7d0e389d3e..f852f77e20 100644 --- a/include/pybind11/smart_holder.h +++ b/include/pybind11/smart_holder.h @@ -4,8 +4,6 @@ #pragma once -#include "detail/pragma_warning_block.h" - #include "detail/common.h" #include "detail/smart_holder_type_casters.h" #include "pybind11.h" diff --git a/include/pybind11/trampoline_self_life_support.h b/include/pybind11/trampoline_self_life_support.h index c37e2226fc..b7e1f12c42 100644 --- a/include/pybind11/trampoline_self_life_support.h +++ b/include/pybind11/trampoline_self_life_support.h @@ -4,8 +4,6 @@ #pragma once -#include "detail/pragma_warning_block.h" - #include "detail/common.h" #include "detail/smart_holder_poc.h" #include "detail/type_caster_base.h" diff --git a/tests/extra_python_package/test_files.py b/tests/extra_python_package/test_files.py index 456c188a53..a176126d8b 100644 --- a/tests/extra_python_package/test_files.py +++ b/tests/extra_python_package/test_files.py @@ -45,7 +45,6 @@ "include/pybind11/detail/dynamic_raw_ptr_cast_if_possible.h", "include/pybind11/detail/init.h", "include/pybind11/detail/internals.h", - "include/pybind11/detail/pragma_warning_block.h", "include/pybind11/detail/smart_holder_poc.h", "include/pybind11/detail/smart_holder_sfinae_hooks_only.h", "include/pybind11/detail/smart_holder_type_casters.h", From 617cb653ec513b4e02d7104b05fb75c26d10e79e Mon Sep 17 00:00:00 2001 From: Aaron Gokaslan Date: Sat, 14 Aug 2021 12:25:54 -0400 Subject: [PATCH 24/25] [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 e41fb99e7e93eb371c2c988075b245f4a1e78dec Mon Sep 17 00:00:00 2001 From: "Ralf W. Grosse-Kunstleve" Date: Sat, 14 Aug 2021 12:45:50 -0700 Subject: [PATCH 25/25] clang-tidy fixes (mostly manual) related to PR #3166 --- include/pybind11/detail/smart_holder_poc.h | 2 +- include/pybind11/detail/smart_holder_type_casters.h | 4 +--- tests/pure_cpp/smart_holder_poc_test.cpp | 4 ++-- tests/test_class_sh_trampoline_shared_from_this.cpp | 2 +- 4 files changed, 5 insertions(+), 7 deletions(-) diff --git a/include/pybind11/detail/smart_holder_poc.h b/include/pybind11/detail/smart_holder_poc.h index a0c8f8dbba..4a2dc034c2 100644 --- a/include/pybind11/detail/smart_holder_poc.h +++ b/include/pybind11/detail/smart_holder_poc.h @@ -309,7 +309,7 @@ struct smart_holder { hld.vptr.reset(static_cast(unq_ptr.get()), std::move(gd)); else hld.vptr.reset(unq_ptr.get(), std::move(gd)); - unq_ptr.release(); + (void) unq_ptr.release(); hld.is_populated = true; return hld; } diff --git a/include/pybind11/detail/smart_holder_type_casters.h b/include/pybind11/detail/smart_holder_type_casters.h index 7ac9a5d351..1612035987 100644 --- a/include/pybind11/detail/smart_holder_type_casters.h +++ b/include/pybind11/detail/smart_holder_type_casters.h @@ -704,13 +704,11 @@ struct smart_holder_type_caster> : smart_holder_type_caster_l static handle cast(const std::shared_ptr &src, return_value_policy policy, handle parent) { switch (policy) { case return_value_policy::automatic: - break; case return_value_policy::automatic_reference: break; case return_value_policy::take_ownership: throw cast_error("Invalid return_value_policy for shared_ptr (take_ownership)."); case return_value_policy::copy: - break; case return_value_policy::move: break; case return_value_policy::reference: @@ -809,7 +807,7 @@ struct smart_holder_type_caster> : smart_holder_type_caste // Critical transfer-of-ownership section. This must stay together. self_life_support->deactivate_life_support(); holder.reclaim_disowned(); - src.release(); + (void) src.release(); // Critical section end. return existing_inst; } diff --git a/tests/pure_cpp/smart_holder_poc_test.cpp b/tests/pure_cpp/smart_holder_poc_test.cpp index d795f41d3f..fa57c38bf0 100644 --- a/tests/pure_cpp/smart_holder_poc_test.cpp +++ b/tests/pure_cpp/smart_holder_poc_test.cpp @@ -137,8 +137,8 @@ TEST_CASE("from_raw_ptr_take_ownership+disown+reclaim_disowned", "[S]") { REQUIRE(*new_owner == 19); hld.reclaim_disowned(); // Manually veriified: without this, clang++ -fsanitize=address reports // "detected memory leaks". - new_owner.release(); // Manually verified: without this, clang++ -fsanitize=address reports - // "attempting double-free". + (void) new_owner.release(); // Manually verified: without this, clang++ -fsanitize=address + // reports "attempting double-free". REQUIRE(hld.as_lvalue_ref() == 19); REQUIRE(new_owner.get() == nullptr); } diff --git a/tests/test_class_sh_trampoline_shared_from_this.cpp b/tests/test_class_sh_trampoline_shared_from_this.cpp index d6b0e8eed0..2703c60b5d 100644 --- a/tests/test_class_sh_trampoline_shared_from_this.cpp +++ b/tests/test_class_sh_trampoline_shared_from_this.cpp @@ -21,7 +21,7 @@ struct Sft : std::enable_shared_from_this { // history in case something goes wrong. // However, compilers other than clang have a variety of issues. It is not // worth the trouble covering all platforms. - Sft(const Sft &other) { history = other.history + "_CpCtor"; } + Sft(const Sft &other) : enable_shared_from_this(other) { history = other.history + "_CpCtor"; } Sft(Sft &&other) noexcept { history = other.history + "_MvCtor"; }