From 2e331308d38a521c087e7fc0cfee227cd29f3f71 Mon Sep 17 00:00:00 2001 From: Aaron Gokaslan Date: Fri, 6 May 2022 16:57:39 -0400 Subject: [PATCH 1/8] chore: remove unused include from stl.h (#3928) * change iostream include to ostream include --- include/pybind11/stl.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/pybind11/stl.h b/include/pybind11/stl.h index 625fb210f..597bce61d 100644 --- a/include/pybind11/stl.h +++ b/include/pybind11/stl.h @@ -13,9 +13,9 @@ #include "detail/common.h" #include -#include #include #include +#include #include #include #include From ad146b2a1877e8ba3803f94a7837969835a297a7 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 9 May 2022 18:44:21 -0400 Subject: [PATCH 2/8] [pre-commit.ci] pre-commit autoupdate (#3933) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit updates: - [github.com/asottile/pyupgrade: v2.32.0 → v2.32.1](https://github.com/asottile/pyupgrade/compare/v2.32.0...v2.32.1) - [github.com/pre-commit/mirrors-clang-format: v14.0.1 → v14.0.3](https://github.com/pre-commit/mirrors-clang-format/compare/v14.0.1...v14.0.3) Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> --- .pre-commit-config.yaml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 2a7dd01cf..03f829c25 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -32,7 +32,7 @@ repos: # Upgrade old Python syntax - repo: https://github.com/asottile/pyupgrade - rev: "v2.32.0" + rev: "v2.32.1" hooks: - id: pyupgrade args: [--py36-plus] @@ -164,7 +164,7 @@ repos: # Clang format the codebase automatically - repo: https://github.com/pre-commit/mirrors-clang-format - rev: "v14.0.1" + rev: "v14.0.3" hooks: - id: clang-format types_or: [c++, c, cuda] From 5621ab853a60ad48bce08487cc6e220930178b79 Mon Sep 17 00:00:00 2001 From: "Ralf W. Grosse-Kunstleve" Date: Mon, 16 May 2022 04:26:35 -0700 Subject: [PATCH 3/8] Do we have a unit test for the traceback code in error_string()? --- include/pybind11/detail/type_caster_base.h | 40 ---------------------- 1 file changed, 40 deletions(-) diff --git a/include/pybind11/detail/type_caster_base.h b/include/pybind11/detail/type_caster_base.h index a7b977132..36d56e23c 100644 --- a/include/pybind11/detail/type_caster_base.h +++ b/include/pybind11/detail/type_caster_base.h @@ -493,46 +493,6 @@ PYBIND11_NOINLINE std::string error_string() { PyException_SetTraceback(scope.value, scope.trace); } -#if !defined(PYPY_VERSION) - if (scope.trace) { - auto *trace = (PyTracebackObject *) scope.trace; - - /* Get the deepest trace possible */ - while (trace->tb_next) { - trace = trace->tb_next; - } - - PyFrameObject *frame = trace->tb_frame; - Py_XINCREF(frame); - errorString += "\n\nAt:\n"; - while (frame) { -# if PY_VERSION_HEX >= 0x030900B1 - PyCodeObject *f_code = PyFrame_GetCode(frame); -# else - PyCodeObject *f_code = frame->f_code; - Py_INCREF(f_code); -# endif - int lineno = PyFrame_GetLineNumber(frame); - errorString += " "; - errorString += handle(f_code->co_filename).cast(); - errorString += '('; - errorString += std::to_string(lineno); - errorString += "): "; - errorString += handle(f_code->co_name).cast(); - errorString += '\n'; - Py_DECREF(f_code); -# if PY_VERSION_HEX >= 0x030900B1 - auto *b_frame = PyFrame_GetBack(frame); -# else - auto *b_frame = frame->f_back; - Py_XINCREF(b_frame); -# endif - Py_DECREF(frame); - frame = b_frame; - } - } -#endif - return errorString; } From 48c7be4a5643cdf48a1228de05f6279ec95e99d3 Mon Sep 17 00:00:00 2001 From: "Ralf W. Grosse-Kunstleve" Date: Mon, 16 May 2022 04:30:10 -0700 Subject: [PATCH 4/8] Undoing previous accidental commit. Sorry I forgot to git branch. --- include/pybind11/detail/type_caster_base.h | 40 ++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/include/pybind11/detail/type_caster_base.h b/include/pybind11/detail/type_caster_base.h index 36d56e23c..a7b977132 100644 --- a/include/pybind11/detail/type_caster_base.h +++ b/include/pybind11/detail/type_caster_base.h @@ -493,6 +493,46 @@ PYBIND11_NOINLINE std::string error_string() { PyException_SetTraceback(scope.value, scope.trace); } +#if !defined(PYPY_VERSION) + if (scope.trace) { + auto *trace = (PyTracebackObject *) scope.trace; + + /* Get the deepest trace possible */ + while (trace->tb_next) { + trace = trace->tb_next; + } + + PyFrameObject *frame = trace->tb_frame; + Py_XINCREF(frame); + errorString += "\n\nAt:\n"; + while (frame) { +# if PY_VERSION_HEX >= 0x030900B1 + PyCodeObject *f_code = PyFrame_GetCode(frame); +# else + PyCodeObject *f_code = frame->f_code; + Py_INCREF(f_code); +# endif + int lineno = PyFrame_GetLineNumber(frame); + errorString += " "; + errorString += handle(f_code->co_filename).cast(); + errorString += '('; + errorString += std::to_string(lineno); + errorString += "): "; + errorString += handle(f_code->co_name).cast(); + errorString += '\n'; + Py_DECREF(f_code); +# if PY_VERSION_HEX >= 0x030900B1 + auto *b_frame = PyFrame_GetBack(frame); +# else + auto *b_frame = frame->f_back; + Py_XINCREF(b_frame); +# endif + Py_DECREF(frame); + frame = b_frame; + } + } +#endif + return errorString; } From 72eea20afd51e363fe115265043c2b2b6bcc523a Mon Sep 17 00:00:00 2001 From: Maarten Baert Date: Mon, 16 May 2022 22:51:01 +0200 Subject: [PATCH 5/8] Fix py::cast from pytype rvalue to pytype (#3949) * Fix py::cast from pytype rvalue to pytype Previously, py::cast blindly assumed that the destination type was a C++ type rather than a python type when the source type was an rvalue. * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> --- include/pybind11/cast.h | 17 ++++++++++++++--- tests/test_copy_move.cpp | 3 +++ tests/test_copy_move.py | 7 +++++++ 3 files changed, 24 insertions(+), 3 deletions(-) diff --git a/include/pybind11/cast.h b/include/pybind11/cast.h index 376a67954..9a971704e 100644 --- a/include/pybind11/cast.h +++ b/include/pybind11/cast.h @@ -1009,6 +1009,8 @@ struct return_value_policy_override< // Basic python -> C++ casting; throws if casting fails template type_caster &load_type(type_caster &conv, const handle &handle) { + static_assert(!detail::is_pyobject::value, + "Internal error: type_caster should only be used for C++ types"); if (!conv.load(handle, true)) { #if !defined(PYBIND11_DETAILED_ERROR_MESSAGES) throw cast_error("Unable to cast Python instance to C++ type (#define " @@ -1099,21 +1101,30 @@ detail::enable_if_t::value, T> move(object &&obj) { // - If both movable and copyable, check ref count: if 1, move; otherwise copy // - Otherwise (not movable), copy. template -detail::enable_if_t::value, T> cast(object &&object) { +detail::enable_if_t::value && detail::move_always::value, T> +cast(object &&object) { return move(std::move(object)); } template -detail::enable_if_t::value, T> cast(object &&object) { +detail::enable_if_t::value && detail::move_if_unreferenced::value, T> +cast(object &&object) { if (object.ref_count() > 1) { return cast(object); } return move(std::move(object)); } template -detail::enable_if_t::value, T> cast(object &&object) { +detail::enable_if_t::value && detail::move_never::value, T> +cast(object &&object) { return cast(object); } +// pytype rvalue -> pytype (calls converting constructor) +template +detail::enable_if_t::value, T> cast(object &&object) { + return T(std::move(object)); +} + template T object::cast() const & { return pybind11::cast(*this); diff --git a/tests/test_copy_move.cpp b/tests/test_copy_move.cpp index 6e1bdd856..28c244564 100644 --- a/tests/test_copy_move.cpp +++ b/tests/test_copy_move.cpp @@ -289,4 +289,7 @@ TEST_SUBMODULE(copy_move_policies, m) { py::return_value_policy::move); m.def( "get_moveissue2", [](int i) { return MoveIssue2(i); }, py::return_value_policy::move); + + // Make sure that cast from pytype rvalue to other pytype works + m.def("get_pytype_rvalue_castissue", [](double i) { return py::float_(i).cast(); }); } diff --git a/tests/test_copy_move.py b/tests/test_copy_move.py index f2c0cdceb..9fef08933 100644 --- a/tests/test_copy_move.py +++ b/tests/test_copy_move.py @@ -123,3 +123,10 @@ def test_move_fallback(): assert m1.value == 1 m2 = m.get_moveissue2(2) assert m2.value == 2 + + +def test_pytype_rvalue_cast(): + """Make sure that cast from pytype rvalue to other pytype works""" + + value = m.get_pytype_rvalue_castissue(1.0) + assert value == 1 From 1a7b12983e09f698be3007b5868bfdf931d9a4d1 Mon Sep 17 00:00:00 2001 From: Henry Schreiner Date: Mon, 16 May 2022 17:27:19 -0400 Subject: [PATCH 6/8] ci: fix cuda issue & MSVC spurious warning (#3950) * ci: fix cuda issue * ci: cuda 11.3-11.4 produce warnings -> errors * tests: ignore unused warning for MSVC * Update tests/CMakeLists.txt --- .github/workflows/ci.yml | 4 ++-- tests/CMakeLists.txt | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 2f295614e..8b537e6e1 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -315,8 +315,8 @@ jobs: # Testing NVCC; forces sources to behave like .cu files cuda: runs-on: ubuntu-latest - name: "🐍 3.8 • CUDA 11 • Ubuntu 20.04" - container: nvidia/cuda:11.0-devel-ubuntu20.04 + name: "🐍 3.8 • CUDA 11.2 • Ubuntu 20.04" + container: nvidia/cuda:11.2.2-devel-ubuntu20.04 steps: - uses: actions/checkout@v3 diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index bce8cceb1..a14b520a5 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -351,7 +351,7 @@ endif() # Compile with compiler warnings turned on function(pybind11_enable_warnings target_name) if(MSVC) - target_compile_options(${target_name} PRIVATE /W4) + target_compile_options(${target_name} PRIVATE /W4 /wd4189) elseif(CMAKE_CXX_COMPILER_ID MATCHES "(GNU|Intel|Clang)" AND NOT PYBIND11_CUDA_TESTS) target_compile_options( ${target_name} From dff6fa0554bf6efe98a8da3f932b749cff4d76a8 Mon Sep 17 00:00:00 2001 From: Henry Schreiner Date: Mon, 16 May 2022 23:45:54 -0400 Subject: [PATCH 7/8] fix(cmake): avoid issue with NVCC + Windows (#3947) --- tools/pybind11Common.cmake | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tools/pybind11Common.cmake b/tools/pybind11Common.cmake index bbb722ac7..e1fb601ac 100644 --- a/tools/pybind11Common.cmake +++ b/tools/pybind11Common.cmake @@ -96,7 +96,7 @@ if(MSVC) # That's also clang-cl set_property( TARGET pybind11::windows_extras APPEND - PROPERTY INTERFACE_COMPILE_OPTIONS /bigobj) + PROPERTY INTERFACE_COMPILE_OPTIONS $<$:/bigobj>) # /MP enables multithreaded builds (relevant when there are many files) for MSVC if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC") # no Clang no Intel From a8b3ff30f9649459021adc80f98a945d3ac675a5 Mon Sep 17 00:00:00 2001 From: Aaron Gokaslan Date: Tue, 17 May 2022 11:29:49 -0400 Subject: [PATCH 8/8] chore: add a couple of moves in pybind11.h (#3941) * Add missing std::moves in enum_base methods * Remove useless move * Add one more std::move in print * Further optimize print * Some more misc optimizations --- include/pybind11/pybind11.h | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/include/pybind11/pybind11.h b/include/pybind11/pybind11.h index cd86152e5..ca94aaeb3 100644 --- a/include/pybind11/pybind11.h +++ b/include/pybind11/pybind11.h @@ -1926,7 +1926,8 @@ struct enum_base { [](const object &arg) -> str { handle type = type::handle_of(arg); object type_name = type.attr("__name__"); - return pybind11::str("<{}.{}: {}>").format(type_name, enum_name(arg), int_(arg)); + return pybind11::str("<{}.{}: {}>") + .format(std::move(type_name), enum_name(arg), int_(arg)); }, name("__repr__"), is_method(m_base)); @@ -1936,7 +1937,7 @@ struct enum_base { m_base.attr("__str__") = cpp_function( [](handle arg) -> str { object type_name = type::handle_of(arg).attr("__name__"); - return pybind11::str("{}.{}").format(type_name, enum_name(arg)); + return pybind11::str("{}.{}").format(std::move(type_name), enum_name(arg)); }, name("name"), is_method(m_base)); @@ -2576,8 +2577,8 @@ PYBIND11_NOINLINE void print(const tuple &args, const dict &kwargs) { for (size_t i = 0; i < args.size(); ++i) { strings[i] = str(args[i]); } - auto sep = kwargs.contains("sep") ? kwargs["sep"] : cast(" "); - auto line = sep.attr("join")(strings); + auto sep = kwargs.contains("sep") ? kwargs["sep"] : str(" "); + auto line = sep.attr("join")(std::move(strings)); object file; if (kwargs.contains("file")) { @@ -2596,7 +2597,7 @@ PYBIND11_NOINLINE void print(const tuple &args, const dict &kwargs) { auto write = file.attr("write"); write(line); - write(kwargs.contains("end") ? kwargs["end"] : cast("\n")); + write(kwargs.contains("end") ? kwargs["end"] : str("\n")); if (kwargs.contains("flush") && kwargs["flush"].cast()) { file.attr("flush")(); @@ -2639,7 +2640,7 @@ get_type_override(const void *this_ptr, const type_info *this_type, const char * function override = getattr(self, name, function()); if (override.is_cpp_function()) { - cache.insert(key); + cache.insert(std::move(key)); return function(); }