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 1/4] 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 2/4] 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 3/4] 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 4/4] 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}