From 002fbf2b6d7c98cd675113460da493b956782afc Mon Sep 17 00:00:00 2001 From: matekelemen Date: Thu, 12 Jun 2025 15:54:50 +0200 Subject: [PATCH 01/31] set install directories depending on target type --- CMakeLists.txt | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 427c1811..4de8067b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -138,7 +138,11 @@ include(GenerateExportHeader) generate_export_header( co_sim_io EXPORT_MACRO_NAME CO_SIM_IO_API EXPORT_FILE_NAME ${CMAKE_CURRENT_SOURCE_DIR}/co_sim_io/includes/co_sim_io_api.hpp ) -install(TARGETS co_sim_io DESTINATION bin) +install(TARGETS co_sim_io + LIBRARY DESTINATION libs + ARCHIVE DESTINATION libs + RUNTIME DESTINATION bin + INCLUDES DESTINATION include) if (CO_SIM_IO_BUILD_MPI) # optionally enable communication via MPI @@ -157,7 +161,11 @@ if (CO_SIM_IO_BUILD_MPI) target_link_libraries(co_sim_io_mpi co_sim_io ${MPI_LIBRARIES}) - install(TARGETS co_sim_io_mpi DESTINATION bin) + install(TARGETS co_sim_io_mpi + LIBRARY DESTINATION libs + ARCHIVE DESTINATION libs + RUNTIME DESTINATION bin + INCLUDES DESTINATION include) endif() target_include_directories(co_sim_io PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/co_sim_io) From 759cebe07abeb67f7a8332fbbf99959e8877ed4f Mon Sep 17 00:00:00 2001 From: matekelemen Date: Fri, 13 Jun 2025 10:48:16 +0200 Subject: [PATCH 02/31] set rpath for tests --- tests/CMakeLists.txt | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index e6e07a9b..3e17be7c 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -10,6 +10,7 @@ set_target_properties(co_sim_io_tests PROPERTIES CXX_STANDARD 11 CXX_STANDARD_REQUIRED YES CXX_EXTENSIONS NO + INSTALL_RPATH "$ORIGIN/../libs" ) doctest_discover_tests(co_sim_io_tests TEST_PREFIX "cpp_" WORKING_DIRECTORY $) @@ -23,6 +24,7 @@ if (CO_SIM_IO_BUILD_MPI) CXX_STANDARD 11 CXX_STANDARD_REQUIRED YES CXX_EXTENSIONS NO + INSTALL_RPATH "$ORIGIN/../libs" ) doctest_discover_tests(co_sim_io_mpi_tests TEST_PREFIX "cpp_mpi_" WORKING_DIRECTORY $ TEST_EXECUTOR mpiexec TEST_EXECUTOR_ARGS -np 4) @@ -44,6 +46,7 @@ function(add_cpp_executable TEST_SOURCE_FILE) add_executable(${TEST_NAME} ${TEST_SOURCE_FILE}) target_link_libraries(${TEST_NAME} co_sim_io) install(TARGETS ${TEST_NAME} DESTINATION "bin/tests_cpp") + set_target_properties(${TEST_NAME} PROPERTIES INSTALL_RPATH "$ORIGIN/../../libs") endfunction() file(GLOB cpp_test_files "integration_tutorials/cpp/*.cpp") @@ -96,6 +99,7 @@ if (CO_SIM_IO_BUILD_MPI) add_executable(${TEST_NAME} ${TEST_SOURCE_FILE}) target_link_libraries(${TEST_NAME} co_sim_io_mpi) install(TARGETS ${TEST_NAME} DESTINATION "bin/tests_cpp_mpi") + set_target_properties(${TEST_NAME} PROPERTIES INSTALL_RPATH "$ORIGIN/../../libs") endfunction() file(GLOB cpp_mpi_test_files "integration_tutorials/cpp/mpi/*.cpp") @@ -120,6 +124,7 @@ if(CO_SIM_IO_BUILD_C) target_link_libraries(${TEST_NAME} co_sim_io_c) add_test(${TEST_NAME} ${TEST_NAME}) install(TARGETS ${TEST_NAME} DESTINATION "bin/tests_c") + set_target_properties(${TEST_NAME} PROPERTIES INSTALL_RPATH "$ORIGIN/../../libs") endfunction() function(add_c_executable TEST_SOURCE_FILE) @@ -129,6 +134,7 @@ if(CO_SIM_IO_BUILD_C) add_executable(${TEST_NAME} ${TEST_SOURCE_FILE}) target_link_libraries(${TEST_NAME} co_sim_io_c) install(TARGETS ${TEST_NAME} DESTINATION "bin/tests_c") + set_target_properties(${TEST_NAME} PROPERTIES INSTALL_RPATH "$ORIGIN/../../libs") endfunction() file(GLOB c_test_files @@ -162,6 +168,7 @@ if(CO_SIM_IO_BUILD_C) add_executable(${TEST_NAME} ${TEST_SOURCE_FILE}) target_link_libraries(${TEST_NAME} co_sim_io_c_mpi) install(TARGETS ${TEST_NAME} DESTINATION "bin/tests_c_mpi") + set_target_properties(${TEST_NAME} PROPERTIES INSTALL_RPATH "$ORIGIN/../../libs") endfunction() function(add_c_mpi_test TEST_SOURCE_FILE) @@ -172,6 +179,7 @@ if(CO_SIM_IO_BUILD_C) target_link_libraries(${TEST_NAME} co_sim_io_c_mpi) add_test(NAME ${TEST_NAME} COMMAND mpiexec -np 4 ${TEST_NAME}) install(TARGETS ${TEST_NAME} DESTINATION "bin/tests_c_mpi") + set_target_properties(${TEST_NAME} PROPERTIES INSTALL_RPATH "$ORIGIN/../../libs") endfunction() file(GLOB c_mpi_test_files "co_sim_io/c/mpi/*.c") @@ -208,6 +216,7 @@ if(CO_SIM_IO_BUILD_FORTRAN) target_link_libraries(${TEST_NAME} co_sim_io_fortran) add_test(${TEST_NAME} ${TEST_NAME}) install(TARGETS ${TEST_NAME} DESTINATION "bin/tests_fortran") + set_target_properties(${TEST_NAME} PROPERTIES INSTALL_RPATH "$ORIGIN/../../libs") endfunction() function(add_fortran_executable TEST_SOURCE_FILE) @@ -217,6 +226,7 @@ if(CO_SIM_IO_BUILD_FORTRAN) add_executable(${TEST_NAME} ${TEST_SOURCE_FILE}) target_link_libraries(${TEST_NAME} co_sim_io_fortran) install(TARGETS ${TEST_NAME} DESTINATION "bin/tests_fortran") + set_target_properties(${TEST_NAME} PROPERTIES INSTALL_RPATH "$ORIGIN/../../libs") endfunction() file(GLOB fortran_test_files From f93139e435dd1c6344d18ac628294b22290e9e6e Mon Sep 17 00:00:00 2001 From: matekelemen Date: Fri, 13 Jun 2025 14:03:57 +0200 Subject: [PATCH 03/31] set rpath for python bindings --- co_sim_io/python/CMakeLists.txt | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/co_sim_io/python/CMakeLists.txt b/co_sim_io/python/CMakeLists.txt index 021465d6..b7151bce 100644 --- a/co_sim_io/python/CMakeLists.txt +++ b/co_sim_io/python/CMakeLists.txt @@ -7,11 +7,24 @@ pybind11_add_module(PyCoSimIO co_sim_io_python.cpp) target_link_libraries( PyCoSimIO PRIVATE co_sim_io ) -install(TARGETS PyCoSimIO DESTINATION bin) -file(WRITE "${CMAKE_INSTALL_PREFIX}/bin/CoSimIO/__init__.py" "from PyCoSimIO import *\nfrom PyCoSimIO import __version__\n") +if (WIN32) + set(CoSimIOPythonModuleRelativeDir "${CMAKE_INSTALL_PREFIX}/bin/CoSimIO") +else() + set(CoSimIOPythonModuleRelativeDir "${CMAKE_INSTALL_PREFIX}/libs/CoSimIO") +endif() +set(CoSimIOPythonModuleDir "${CMAKE_INSTALL_PREFIX}/${CoSimIOPythonModuleRelativeDir}") + +install(TARGETS PyCoSimIO + LIBRARY DESTINATION "${CoSimIOPythonModuleRelativeDir}" + ARCHIVE DESTINATION "${CoSimIOPythonModuleRelativeDir}" + RUNTIME DESTINATION "${CoSimIOPythonModuleRelativeDir}") +set_target_properties(PyCoSimIO PROPERTIES INSTALL_RPATH "$ORIGIN/..") + +set(CoSimIOInitFile "${CoSimIOPythonModuleDir}/__init__.py") +set(CoSimIOMPIInitFile "${CoSimIOPythonModuleDir}/mpi/__init__.py") +set(mpi4pyInterfaceInitFile "${CoSimIOPythonModuleDir}/mpi/mpi4pyInterface/__init__.py") -set(CoSimIOMPIInitFile "${CMAKE_INSTALL_PREFIX}/bin/CoSimIO/mpi/__init__.py") -set(mpi4pyInterfaceInitFile "${CMAKE_INSTALL_PREFIX}/bin/CoSimIO/mpi/mpi4pyInterface/__init__.py") +file(WRITE "${CoSimIOInitFile}" "from PyCoSimIO import *\nfrom PyCoSimIO import __version__\n") # dummy init files that give proper errors in case something related to MPI was not compiled # These files will be overwritten if the corresponding option is enabled, hence here we can write then unconditionally @@ -24,6 +37,7 @@ if (CO_SIM_IO_BUILD_MPI) target_link_libraries( PyCoSimIOMPI PRIVATE co_sim_io co_sim_io_mpi ) install(TARGETS PyCoSimIOMPI DESTINATION bin) + set_target_properties(PyCoSimIOMPI PROPERTIES INSTALL_RPATH "$ORIGIN") file(WRITE ${CoSimIOMPIInitFile} "from PyCoSimIOMPI import *\n") OPTION ( CO_SIM_IO_BUILD_PYTHON_MPI4PY_INTERFACE "Building the interface to mpi4py MPI communicators" OFF ) @@ -32,6 +46,7 @@ if (CO_SIM_IO_BUILD_MPI) pybind11_add_module(PyCoSimIOMPI_mpi4pyInterface mpi4py_interface.cpp) target_link_libraries( PyCoSimIOMPI_mpi4pyInterface PRIVATE ${MPI_LIBRARIES} ) install(TARGETS PyCoSimIOMPI_mpi4pyInterface DESTINATION bin) + set_target_properties(PyCoSimIOMPI_mpi4pyInterface PROPERTIES INSTALL_RPATH "$ORIGIN") file(WRITE ${mpi4pyInterfaceInitFile} "from PyCoSimIOMPI_mpi4pyInterface import *\n") endif() endif() From ebc54508bb165c00ab98c181e6ea04b6e02cfac2 Mon Sep 17 00:00:00 2001 From: matekelemen Date: Fri, 13 Jun 2025 14:07:29 +0200 Subject: [PATCH 04/31] set install dirs of the C lib --- co_sim_io/c/CMakeLists.txt | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/co_sim_io/c/CMakeLists.txt b/co_sim_io/c/CMakeLists.txt index 557ddfca..adfa770e 100644 --- a/co_sim_io/c/CMakeLists.txt +++ b/co_sim_io/c/CMakeLists.txt @@ -12,7 +12,11 @@ set_target_properties(co_sim_io_c PROPERTIES WINDOWS_EXPORT_ALL_SYMBOLS ON) target_link_libraries( co_sim_io_c co_sim_io ) -install(TARGETS co_sim_io_c DESTINATION bin) +install(TARGETS co_sim_io_c + LIBRARY DESTINATION libs + ARCHIVE DESTINATION libs + RUNTIME DESTINATION bin + INCLUDES DESTINATION include) if (CO_SIM_IO_BUILD_MPI) add_library (co_sim_io_c_mpi SHARED co_sim_io_c_mpi.cpp) From b5083ba362a7672ae033ae2c28d6489e74fbfed1 Mon Sep 17 00:00:00 2001 From: matekelemen Date: Fri, 13 Jun 2025 14:07:38 +0200 Subject: [PATCH 05/31] set install dirs of the fortran lib --- co_sim_io/fortran/CMakeLists.txt | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/co_sim_io/fortran/CMakeLists.txt b/co_sim_io/fortran/CMakeLists.txt index 2998f061..76a5cba1 100644 --- a/co_sim_io/fortran/CMakeLists.txt +++ b/co_sim_io/fortran/CMakeLists.txt @@ -6,4 +6,8 @@ add_library (co_sim_io_fortran SHARED co_sim_io.f90) target_link_libraries( co_sim_io_fortran co_sim_io_c ) -install(TARGETS co_sim_io_fortran DESTINATION bin) +install(TARGETS co_sim_io_fortran + LIBRARY DESTINATION libs + ARCHIVE DESTINATION libs + RUNTIME DESTINATION bin + INCLUDES DESTINATION include) From 4f952571fe4950f5a68f32c3d1daa8e326d969e0 Mon Sep 17 00:00:00 2001 From: matekelemen Date: Fri, 13 Jun 2025 14:24:08 +0200 Subject: [PATCH 06/31] update python and library paths in the CI --- .github/workflows/ci.yml | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index b05271de..ff0a387d 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -166,8 +166,8 @@ jobs: if [ ${{ matrix.compiler }} = ICPX ]; then source /opt/intel/oneapi/setvars.sh fi - export PYTHONPATH=${PYTHONPATH}:${GITHUB_WORKSPACE}/bin - export LD_LIBRARY_PATH=${LD_LIBRARY_PATH}:${GITHUB_WORKSPACE}/bin + export PYTHONPATH=${PYTHONPATH}:${GITHUB_WORKSPACE}/libs + export LD_LIBRARY_PATH=${LD_LIBRARY_PATH}:${GITHUB_WORKSPACE}/libs cd tests # TODO delete build dir to make sure the linking etc works correctly? python run_python_tests.py @@ -177,8 +177,8 @@ jobs: if [ ${{ matrix.compiler }} = ICPX ]; then source /opt/intel/oneapi/setvars.sh fi - export PYTHONPATH=${PYTHONPATH}:${GITHUB_WORKSPACE}/bin - export LD_LIBRARY_PATH=${LD_LIBRARY_PATH}:${GITHUB_WORKSPACE}/bin + export PYTHONPATH=${PYTHONPATH}:${GITHUB_WORKSPACE}/libs + export LD_LIBRARY_PATH=${LD_LIBRARY_PATH}:${GITHUB_WORKSPACE}/libs cd tests #TODO run the tests... # TODO delete build dir to make sure the linking etc works correctly? @@ -237,8 +237,8 @@ jobs: - name: Running tests run: | - export PYTHONPATH=${PYTHONPATH}:${GITHUB_WORKSPACE}/bin - export DYLD_LIBRARY_PATH=${DYLD_LIBRARY_PATH}:${GITHUB_WORKSPACE}/bin + export PYTHONPATH=${PYTHONPATH}:${GITHUB_WORKSPACE}/libs + export DYLD_LIBRARY_PATH=${DYLD_LIBRARY_PATH}:${GITHUB_WORKSPACE}/libs cd tests # TODO delete build dir to make sure the linking etc works correctly? python run_python_tests.py @@ -369,8 +369,8 @@ jobs: - name: Running tests (Python) run: | - export PYTHONPATH=${PYTHONPATH}:${GITHUB_WORKSPACE}/bin - export LD_LIBRARY_PATH=${LD_LIBRARY_PATH}:${GITHUB_WORKSPACE}/bin + export PYTHONPATH=${PYTHONPATH}:${GITHUB_WORKSPACE}/libs + export LD_LIBRARY_PATH=${LD_LIBRARY_PATH}:${GITHUB_WORKSPACE}/libs cd tests # TODO delete build dir to make sure the linking etc works correctly? (Needs to be done after running CTests) python3.8 run_python_tests.py From b196a534e1a54cafdd59b32a33d9d2326b82dadd Mon Sep 17 00:00:00 2001 From: matekelemen Date: Fri, 13 Jun 2025 16:36:27 +0200 Subject: [PATCH 07/31] set c mpi lib install path --- co_sim_io/c/CMakeLists.txt | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/co_sim_io/c/CMakeLists.txt b/co_sim_io/c/CMakeLists.txt index adfa770e..9cfe281a 100644 --- a/co_sim_io/c/CMakeLists.txt +++ b/co_sim_io/c/CMakeLists.txt @@ -23,5 +23,9 @@ if (CO_SIM_IO_BUILD_MPI) target_link_libraries(co_sim_io_c_mpi co_sim_io_c co_sim_io_mpi) - install(TARGETS co_sim_io_c_mpi DESTINATION bin) + install(TARGETS co_sim_io_c_mpi + LIBRARY DESTINATION libs + ARCHIVE DESTINATION libs + RUNTIME DESTINATION bin + INCLUDES DESTINATION include) endif() From ca8c4afda1f2e8dc26f8d0eded4c4ad59bb5fc3c Mon Sep 17 00:00:00 2001 From: matekelemen Date: Fri, 13 Jun 2025 16:36:54 +0200 Subject: [PATCH 08/31] fix python binary module install paths and rpaths --- co_sim_io/python/CMakeLists.txt | 30 ++++++++++++++++++------------ 1 file changed, 18 insertions(+), 12 deletions(-) diff --git a/co_sim_io/python/CMakeLists.txt b/co_sim_io/python/CMakeLists.txt index b7151bce..ab133165 100644 --- a/co_sim_io/python/CMakeLists.txt +++ b/co_sim_io/python/CMakeLists.txt @@ -8,17 +8,17 @@ pybind11_add_module(PyCoSimIO co_sim_io_python.cpp) target_link_libraries( PyCoSimIO PRIVATE co_sim_io ) if (WIN32) - set(CoSimIOPythonModuleRelativeDir "${CMAKE_INSTALL_PREFIX}/bin/CoSimIO") + set(CoSimIOPythonModuleRelativeDir "bin/CoSimIO") else() - set(CoSimIOPythonModuleRelativeDir "${CMAKE_INSTALL_PREFIX}/libs/CoSimIO") + set(CoSimIOPythonModuleRelativeDir "libs/CoSimIO") endif() set(CoSimIOPythonModuleDir "${CMAKE_INSTALL_PREFIX}/${CoSimIOPythonModuleRelativeDir}") install(TARGETS PyCoSimIO - LIBRARY DESTINATION "${CoSimIOPythonModuleRelativeDir}" - ARCHIVE DESTINATION "${CoSimIOPythonModuleRelativeDir}" - RUNTIME DESTINATION "${CoSimIOPythonModuleRelativeDir}") -set_target_properties(PyCoSimIO PROPERTIES INSTALL_RPATH "$ORIGIN/..") + LIBRARY DESTINATION libs + ARCHIVE DESTINATION libs + RUNTIME DESTINATION bin) +set_target_properties(PyCoSimIO PROPERTIES INSTALL_RPATH "$ORIGIN") set(CoSimIOInitFile "${CoSimIOPythonModuleDir}/__init__.py") set(CoSimIOMPIInitFile "${CoSimIOPythonModuleDir}/mpi/__init__.py") @@ -28,25 +28,31 @@ file(WRITE "${CoSimIOInitFile}" "from PyCoSimIO import *\nfrom PyCoSimIO import # dummy init files that give proper errors in case something related to MPI was not compiled # These files will be overwritten if the corresponding option is enabled, hence here we can write then unconditionally -file(WRITE ${CoSimIOMPIInitFile} "raise Exception('CoSimIO was compiled without MPI support! (use \"CO_SIM_IO_BUILD_MPI\" to enable it)')\n") -file(WRITE ${mpi4pyInterfaceInitFile} "raise Exception('The mpi4py interface was not compiled! (use \"CO_SIM_IO_BUILD_PYTHON_MPI4PY_INTERFACE\" to enable it)')\n") +file(WRITE "${CoSimIOMPIInitFile}" "raise Exception('CoSimIO was compiled without MPI support! (use \"CO_SIM_IO_BUILD_MPI\" to enable it)')\n") +file(WRITE "${mpi4pyInterfaceInitFile}" "raise Exception('The mpi4py interface was not compiled! (use \"CO_SIM_IO_BUILD_PYTHON_MPI4PY_INTERFACE\" to enable it)')\n") if (CO_SIM_IO_BUILD_MPI) pybind11_add_module(PyCoSimIOMPI co_sim_io_python_mpi.cpp) target_link_libraries( PyCoSimIOMPI PRIVATE co_sim_io co_sim_io_mpi ) - install(TARGETS PyCoSimIOMPI DESTINATION bin) + install(TARGETS PyCoSimIOMPI + LIBRARY DESTINATION libs + ARCHIVE DESTINATION libs + RUNTIME DESTINATION bin) set_target_properties(PyCoSimIOMPI PROPERTIES INSTALL_RPATH "$ORIGIN") - file(WRITE ${CoSimIOMPIInitFile} "from PyCoSimIOMPI import *\n") + file(WRITE "${CoSimIOMPIInitFile}" "from PyCoSimIOMPI import *\n") OPTION ( CO_SIM_IO_BUILD_PYTHON_MPI4PY_INTERFACE "Building the interface to mpi4py MPI communicators" OFF ) if (CO_SIM_IO_BUILD_PYTHON_MPI4PY_INTERFACE) message("Building the interface to mpi4py MPI communicators") pybind11_add_module(PyCoSimIOMPI_mpi4pyInterface mpi4py_interface.cpp) target_link_libraries( PyCoSimIOMPI_mpi4pyInterface PRIVATE ${MPI_LIBRARIES} ) - install(TARGETS PyCoSimIOMPI_mpi4pyInterface DESTINATION bin) + install(TARGETS PyCoSimIOMPI_mpi4pyInterface + LIBRARY DESTINATION libs + ARCHIVE DESTINATION libs + RUNTIME DESTINATION bin) set_target_properties(PyCoSimIOMPI_mpi4pyInterface PROPERTIES INSTALL_RPATH "$ORIGIN") - file(WRITE ${mpi4pyInterfaceInitFile} "from PyCoSimIOMPI_mpi4pyInterface import *\n") + file(WRITE "${mpi4pyInterfaceInitFile}" "from PyCoSimIOMPI_mpi4pyInterface import *\n") endif() endif() From 4e452ba5054252278c84571ade96df7fff46500e Mon Sep 17 00:00:00 2001 From: matekelemen Date: Fri, 13 Jun 2025 16:55:39 +0200 Subject: [PATCH 09/31] update PYTHONPATH for the windows CI --- .github/workflows/ci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index ff0a387d..3e0c550a 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -334,7 +334,7 @@ jobs: PYTHON_CMD: python # for the tests (subprocess) shell: cmd run: | - set PYTHONPATH=%PYTHONPATH%;%GITHUB_WORKSPACE%/bin + set PYTHONPATH=%PYTHONPATH%;%GITHUB_WORKSPACE%/libs cd tests rem TODO delete build dir to make sure the linking etc works correctly? python run_python_tests.py From d768f342e3ecf247508674b439a7763c15b25617 Mon Sep 17 00:00:00 2001 From: matekelemen Date: Fri, 13 Jun 2025 17:13:57 +0200 Subject: [PATCH 10/31] Revert "update PYTHONPATH for the windows CI" This reverts commit 4e452ba5054252278c84571ade96df7fff46500e. --- .github/workflows/ci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 3e0c550a..ff0a387d 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -334,7 +334,7 @@ jobs: PYTHON_CMD: python # for the tests (subprocess) shell: cmd run: | - set PYTHONPATH=%PYTHONPATH%;%GITHUB_WORKSPACE%/libs + set PYTHONPATH=%PYTHONPATH%;%GITHUB_WORKSPACE%/bin cd tests rem TODO delete build dir to make sure the linking etc works correctly? python run_python_tests.py From 2fae258ee6700e6dbe3f2ed0286660cf69df8ff6 Mon Sep 17 00:00:00 2001 From: matekelemen Date: Fri, 13 Jun 2025 17:21:55 +0200 Subject: [PATCH 11/31] debug print --- .github/workflows/ci.yml | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index ff0a387d..0ec22446 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -335,6 +335,10 @@ jobs: shell: cmd run: | set PYTHONPATH=%PYTHONPATH%;%GITHUB_WORKSPACE%/bin + + @REM show what's on your PYTHONPATH + dir %GITHUB_WORKSPACE%/bin + cd tests rem TODO delete build dir to make sure the linking etc works correctly? python run_python_tests.py From 8e7decdd96828d0b68ac651496ba3a8f2d587dcb Mon Sep 17 00:00:00 2001 From: matekelemen Date: Thu, 28 May 2026 10:21:56 +0200 Subject: [PATCH 12/31] remove direct compiler flags specifying the cpp standard --- CMakeLists.txt | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 4de8067b..da8c17ef 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,6 +1,15 @@ cmake_minimum_required (VERSION 3.15.0) + +if(POLICY CMP0048) + # project command manages version + cmake_policy(SET CMP0048 NEW) +endif(POLICY CMP0048) project(CoSimIO LANGUAGES CXX C VERSION 3.0.0) +if (NOT DEFINED CMAKE_CXX_STANDARD) + set(CMAKE_CXX_STANDARD 17) +endif() + # this has to be specified BEFORE including CTest! # suppressions file has to be included in the options, as using "MEMORYCHECK_SUPPRESSIONS_FILE" doesn't work on all systems set(MEMORYCHECK_COMMAND_OPTIONS "--leak-check=full --show-leak-kinds=all --track-origins=yes --tool=memcheck --error-exitcode=1 --suppressions=${CMAKE_CURRENT_SOURCE_DIR}/tests/valgrind_suppressions.supp --gen-suppressions=all") @@ -76,7 +85,7 @@ if(MSVC) endif() elseif(${CMAKE_COMPILER_IS_GNUCXX}) - set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}") set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=c89") if (CO_SIM_IO_STRICT_COMPILER) @@ -92,7 +101,7 @@ elseif(${CMAKE_COMPILER_IS_GNUCXX}) # Note: This command makes sure that this option comes pretty late on the cmdline. link_libraries("$<$,$,7.0>,$,9.0>>:-lstdc++fs>") elseif(CMAKE_CXX_COMPILER_ID MATCHES "Clang") - set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}") set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=c89") if (CO_SIM_IO_STRICT_COMPILER) @@ -101,7 +110,7 @@ elseif(CMAKE_CXX_COMPILER_ID MATCHES "Clang") endif() elseif(CMAKE_CXX_COMPILER_ID STREQUAL "Intel") - set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}") set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=c89") if (CO_SIM_IO_STRICT_COMPILER) @@ -110,7 +119,7 @@ elseif(CMAKE_CXX_COMPILER_ID STREQUAL "Intel") endif() else() - set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -Wall -Wpedantic") + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wpedantic") set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=c89 -Wall -Wpedantic") endif() From 907b44f549f0c1312344452166dcb3b4069080c9 Mon Sep 17 00:00:00 2001 From: matekelemen Date: Thu, 28 May 2026 10:31:10 +0200 Subject: [PATCH 13/31] update pybind11 to version 3.0.4 --- external_libraries/pybind11/CMakeLists.txt | 199 +- external_libraries/pybind11/CMakePresets.json | 93 + external_libraries/pybind11/MANIFEST.in | 6 - external_libraries/pybind11/README.rst | 85 +- external_libraries/pybind11/docs/Doxyfile | 21 + .../pybind11/docs/_static/css/custom.css | 3 + .../pybind11/docs/advanced/cast/chrono.rst | 81 + .../pybind11/docs/advanced/cast/custom.rst | 137 + .../pybind11/docs/advanced/cast/eigen.rst | 310 ++ .../docs/advanced/cast/functional.rst | 109 + .../pybind11/docs/advanced/cast/index.rst | 43 + .../pybind11/docs/advanced/cast/overview.rst | 170 + .../pybind11/docs/advanced/cast/stl.rst | 249 ++ .../pybind11/docs/advanced/cast/strings.rst | 296 ++ .../pybind11/docs/advanced/classes.rst | 1432 +++++++ .../pybind11/docs/advanced/deadlock.md | 391 ++ .../pybind11/docs/advanced/deprecated.rst | 179 + .../pybind11/docs/advanced/embedding.rst | 499 +++ .../pybind11/docs/advanced/exceptions.rst | 422 ++ .../pybind11/docs/advanced/functions.rst | 616 +++ .../pybind11/docs/advanced/misc.rst | 615 +++ .../pybind11/docs/advanced/pycpp/index.rst | 13 + .../pybind11/docs/advanced/pycpp/numpy.rst | 493 +++ .../pybind11/docs/advanced/pycpp/object.rst | 286 ++ .../docs/advanced/pycpp/utilities.rst | 155 + .../pybind11/docs/advanced/smart_ptrs.rst | 179 + external_libraries/pybind11/docs/basics.rst | 316 ++ external_libraries/pybind11/docs/benchmark.py | 89 + .../pybind11/docs/benchmark.rst | 95 + external_libraries/pybind11/docs/changelog.md | 3488 +++++++++++++++++ external_libraries/pybind11/docs/classes.rst | 652 +++ .../pybind11/docs/cmake/index.rst | 8 + .../pybind11/docs/compiling.rst | 731 ++++ external_libraries/pybind11/docs/conf.py | 369 ++ external_libraries/pybind11/docs/faq.rst | 351 ++ external_libraries/pybind11/docs/index.rst | 49 + .../pybind11/docs/installing.rst | 105 + .../pybind11/docs/limitations.rst | 68 + .../pybind11/docs/pybind11-logo.png | Bin 0 -> 61034 bytes .../docs/pybind11_vs_boost_python1.png | Bin 0 -> 44653 bytes .../docs/pybind11_vs_boost_python1.svg | 427 ++ .../docs/pybind11_vs_boost_python2.png | Bin 0 -> 41121 bytes .../docs/pybind11_vs_boost_python2.svg | 427 ++ .../pybind11/docs/reference.rst | 130 + external_libraries/pybind11/docs/release.rst | 135 + .../pybind11/docs/requirements.in | 7 + .../pybind11/docs/requirements.txt | 87 + external_libraries/pybind11/docs/upgrade.rst | 748 ++++ .../pybind11/include/pybind11/attr.h | 62 +- .../pybind11/include/pybind11/buffer_info.h | 35 +- .../pybind11/include/pybind11/cast.h | 1004 ++++- .../pybind11/include/pybind11/chrono.h | 4 +- .../pybind11/include/pybind11/complex.h | 23 +- .../include/pybind11/conduit/README.txt | 15 + .../pybind11/conduit/pybind11_conduit_v1.h | 116 + .../conduit/pybind11_platform_abi_id.h | 87 + .../pybind11/conduit/wrap_include_python_h.h | 72 + .../include/pybind11/critical_section.h | 56 + .../include/pybind11/detail/argument_vector.h | 417 ++ .../pybind11/include/pybind11/detail/class.h | 351 +- .../pybind11/include/pybind11/detail/common.h | 564 ++- .../include/pybind11/detail/cpp_conduit.h | 75 + .../pybind11/include/pybind11/detail/descr.h | 55 + .../detail/dynamic_raw_ptr_cast_if_possible.h | 39 + .../pybind11/detail/exception_translation.h | 71 + .../detail/function_record_pyobject.h | 192 + .../detail/holder_caster_foreign_helpers.h | 104 + .../pybind11/include/pybind11/detail/init.h | 172 +- .../include/pybind11/detail/internals.h | 1114 ++++-- .../pybind11/detail/native_enum_data.h | 227 ++ .../detail/pybind11_namespace_macros.h | 82 + .../pybind11/detail/struct_smart_holder.h | 398 ++ .../pybind11/detail/type_caster_base.h | 970 ++++- .../pybind11/detail/using_smart_holder.h | 22 + .../pybind11/detail/value_and_holder.h | 92 + .../pybind11/include/pybind11/eigen/matrix.h | 29 +- .../pybind11/include/pybind11/eigen/tensor.h | 28 +- .../pybind11/include/pybind11/embed.h | 96 +- .../pybind11/include/pybind11/eval.h | 17 +- .../pybind11/include/pybind11/functional.h | 106 +- .../pybind11/include/pybind11/gil.h | 118 +- .../include/pybind11/gil_safe_call_once.h | 273 ++ .../pybind11/include/pybind11/gil_simple.h | 37 + .../pybind11/include/pybind11/iostream.h | 44 +- .../pybind11/include/pybind11/native_enum.h | 76 + .../pybind11/include/pybind11/numpy.h | 570 ++- .../pybind11/include/pybind11/pybind11.h | 1926 +++++++-- .../pybind11/include/pybind11/pytypes.h | 277 +- .../pybind11/include/pybind11/stl.h | 333 +- .../include/pybind11/stl/filesystem.h | 48 +- .../pybind11/include/pybind11/stl_bind.h | 135 +- .../include/pybind11/subinterpreter.h | 291 ++ .../pybind11/trampoline_self_life_support.h | 65 + .../pybind11/include/pybind11/typing.h | 295 ++ .../pybind11/include/pybind11/warnings.h | 75 + external_libraries/pybind11/noxfile.py | 126 +- .../pybind11/pybind11/__init__.py | 19 + .../pybind11/pybind11/__main__.py | 97 + .../pybind11/pybind11/_version.py | 34 + .../pybind11/pybind11/commands.py | 39 + external_libraries/pybind11/pybind11/py.typed | 0 .../pybind11/pybind11/setup_helpers.py | 500 +++ external_libraries/pybind11/pyproject.toml | 207 +- external_libraries/pybind11/setup.cfg | 43 - external_libraries/pybind11/setup.py | 150 - .../pybind11/tests/CMakeLists.txt | 694 ++++ external_libraries/pybind11/tests/conftest.py | 313 ++ .../pybind11/tests/constructor_stats.h | 330 ++ .../pybind11/tests/cross_module_gil_utils.cpp | 111 + ...s_module_interleaved_error_already_set.cpp | 54 + .../pybind11/tests/custom_exceptions.py | 10 + .../tests/eigen_tensor_avoid_stl_array.cpp | 16 + external_libraries/pybind11/tests/env.py | 76 + .../pybind11/tests/exo_planet_c_api.cpp | 76 + .../pybind11/tests/exo_planet_pybind11.cpp | 19 + .../tests/extra_python_package/pytest.ini | 0 .../tests/extra_python_package/test_files.py | 385 ++ .../tests/extra_setuptools/pytest.ini | 0 .../extra_setuptools/test_setuphelper.py | 153 + .../home_planet_very_lonely_traveler.cpp | 13 + .../pybind11/tests/local_bindings.h | 93 + .../tests/mod_per_interpreter_gil.cpp | 20 + ...mod_per_interpreter_gil_with_singleton.cpp | 147 + .../tests/mod_shared_interpreter_gil.cpp | 17 + external_libraries/pybind11/tests/object.h | 205 + .../pybind11/tests/pure_cpp/CMakeLists.txt | 22 + .../tests/pure_cpp/smart_holder_poc.h | 56 + .../tests/pure_cpp/smart_holder_poc_test.cpp | 427 ++ .../tests/pybind11_cross_module_tests.cpp | 163 + .../pybind11/tests/pybind11_tests.cpp | 141 + .../pybind11/tests/pybind11_tests.h | 119 + .../pybind11/tests/pyproject.toml | 40 + external_libraries/pybind11/tests/pytest.ini | 22 + .../pybind11/tests/requirements.txt | 20 + .../pybind11/tests/standalone_enum_module.cpp | 13 + .../pybind11/tests/test_async.cpp | 25 + .../pybind11/tests/test_async.py | 31 + .../pybind11/tests/test_buffers.cpp | 571 +++ .../pybind11/tests/test_buffers.py | 471 +++ .../pybind11/tests/test_builtin_casters.cpp | 393 ++ .../pybind11/tests/test_builtin_casters.py | 624 +++ .../pybind11/tests/test_call_policies.cpp | 113 + .../pybind11/tests/test_call_policies.py | 256 ++ .../pybind11/tests/test_callbacks.cpp | 302 ++ .../pybind11/tests/test_callbacks.py | 248 ++ .../pybind11/tests/test_chrono.cpp | 81 + .../pybind11/tests/test_chrono.py | 207 + .../pybind11/tests/test_class.cpp | 700 ++++ .../pybind11/tests/test_class.py | 575 +++ ...ss_module_use_after_one_module_dealloc.cpp | 23 + ...oss_module_use_after_one_module_dealloc.py | 43 + ...ss_release_gil_before_calling_cpp_dtor.cpp | 54 + ...ass_release_gil_before_calling_cpp_dtor.py | 21 + .../pybind11/tests/test_class_sh_basic.cpp | 248 ++ .../pybind11/tests/test_class_sh_basic.py | 248 ++ .../tests/test_class_sh_disowning.cpp | 41 + .../pybind11/tests/test_class_sh_disowning.py | 78 + .../tests/test_class_sh_disowning_mi.cpp | 85 + .../tests/test_class_sh_disowning_mi.py | 246 ++ .../test_class_sh_factory_constructors.cpp | 156 + .../test_class_sh_factory_constructors.py | 53 + .../tests/test_class_sh_inheritance.cpp | 90 + .../tests/test_class_sh_inheritance.py | 63 + .../tests/test_class_sh_mi_thunks.cpp | 230 ++ .../pybind11/tests/test_class_sh_mi_thunks.py | 104 + .../pybind11/tests/test_class_sh_property.cpp | 129 + .../pybind11/tests/test_class_sh_property.py | 218 ++ .../test_class_sh_property_non_owning.cpp | 65 + .../test_class_sh_property_non_owning.py | 30 + .../test_class_sh_shared_ptr_copy_move.cpp | 103 + .../test_class_sh_shared_ptr_copy_move.py | 41 + .../tests/test_class_sh_trampoline_basic.cpp | 57 + .../tests/test_class_sh_trampoline_basic.py | 35 + ..._class_sh_trampoline_self_life_support.cpp | 86 + ...t_class_sh_trampoline_self_life_support.py | 38 + ...t_class_sh_trampoline_shared_from_this.cpp | 137 + ...st_class_sh_trampoline_shared_from_this.py | 247 ++ ...class_sh_trampoline_shared_ptr_cpp_arg.cpp | 92 + ..._class_sh_trampoline_shared_ptr_cpp_arg.py | 154 + .../test_class_sh_trampoline_unique_ptr.cpp | 63 + .../test_class_sh_trampoline_unique_ptr.py | 31 + ...est_class_sh_unique_ptr_custom_deleter.cpp | 30 + ...test_class_sh_unique_ptr_custom_deleter.py | 8 + .../tests/test_class_sh_unique_ptr_member.cpp | 50 + .../tests/test_class_sh_unique_ptr_member.py | 26 + .../test_class_sh_virtual_py_cpp_mix.cpp | 58 + .../tests/test_class_sh_virtual_py_cpp_mix.py | 66 + .../tests/test_cmake_build/CMakeLists.txt | 91 + .../pybind11/tests/test_cmake_build/embed.cpp | 23 + .../installed_embed/CMakeLists.txt | 19 + .../installed_function/CMakeLists.txt | 29 + .../installed_target/CMakeLists.txt | 37 + .../pybind11/tests/test_cmake_build/main.cpp | 6 + .../subdirectory_embed/CMakeLists.txt | 38 + .../subdirectory_function/CMakeLists.txt | 32 + .../subdirectory_target/CMakeLists.txt | 38 + .../pybind11/tests/test_cmake_build/test.py | 10 + .../pybind11/tests/test_const_name.cpp | 55 + .../pybind11/tests/test_const_name.py | 31 + .../tests/test_constants_and_functions.cpp | 158 + .../tests/test_constants_and_functions.py | 58 + .../pybind11/tests/test_copy_move.cpp | 546 +++ .../pybind11/tests/test_copy_move.py | 144 + .../pybind11/tests/test_cpp_conduit.cpp | 22 + .../pybind11/tests/test_cpp_conduit.py | 183 + .../test_cpp_conduit_traveler_bindings.h | 47 + .../tests/test_cpp_conduit_traveler_types.h | 25 + .../test_cross_module_rtti/CMakeLists.txt | 70 + .../tests/test_cross_module_rtti/bindings.cpp | 20 + .../tests/test_cross_module_rtti/catch.cpp | 22 + .../tests/test_cross_module_rtti/lib.cpp | 13 + .../tests/test_cross_module_rtti/lib.h | 31 + .../test_cross_module_rtti.cpp | 50 + .../tests/test_custom_type_casters.cpp | 217 + .../tests/test_custom_type_casters.py | 126 + .../pybind11/tests/test_custom_type_setup.cpp | 104 + .../pybind11/tests/test_custom_type_setup.py | 79 + .../tests/test_docs_advanced_cast_custom.cpp | 69 + .../tests/test_docs_advanced_cast_custom.py | 40 + .../pybind11/tests/test_docstring_options.cpp | 129 + .../pybind11/tests/test_docstring_options.py | 72 + .../pybind11/tests/test_eigen_matrix.cpp | 448 +++ .../pybind11/tests/test_eigen_matrix.py | 839 ++++ .../pybind11/tests/test_eigen_tensor.cpp | 18 + .../pybind11/tests/test_eigen_tensor.inl | 338 ++ .../pybind11/tests/test_eigen_tensor.py | 316 ++ .../pybind11/tests/test_enum.cpp | 149 + .../pybind11/tests/test_enum.py | 344 ++ .../pybind11/tests/test_eval.cpp | 118 + .../pybind11/tests/test_eval.py | 52 + .../pybind11/tests/test_eval_call.py | 5 + .../pybind11/tests/test_exceptions.cpp | 427 ++ .../pybind11/tests/test_exceptions.h | 13 + .../pybind11/tests/test_exceptions.py | 439 +++ .../tests/test_factory_constructors.cpp | 434 ++ .../tests/test_factory_constructors.py | 531 +++ .../pybind11/tests/test_gil_scoped.cpp | 144 + .../pybind11/tests/test_gil_scoped.py | 294 ++ .../pybind11/tests/test_iostream.cpp | 162 + .../pybind11/tests/test_iostream.py | 329 ++ .../tests/test_kwargs_and_defaults.cpp | 331 ++ .../tests/test_kwargs_and_defaults.py | 473 +++ .../pybind11/tests/test_local_bindings.cpp | 131 + .../pybind11/tests/test_local_bindings.py | 291 ++ .../tests/test_methods_and_attributes.cpp | 788 ++++ .../tests/test_methods_and_attributes.py | 715 ++++ .../pybind11/tests/test_modules.cpp | 124 + .../pybind11/tests/test_modules.py | 146 + .../tests/test_multiple_inheritance.cpp | 341 ++ .../tests/test_multiple_inheritance.py | 500 +++ .../tests/test_multiple_interpreters.py | 437 +++ .../pybind11/tests/test_native_enum.cpp | 262 ++ .../pybind11/tests/test_native_enum.py | 377 ++ .../pybind11/tests/test_numpy_array.cpp | 616 +++ .../pybind11/tests/test_numpy_array.py | 749 ++++ .../pybind11/tests/test_numpy_dtypes.cpp | 745 ++++ .../pybind11/tests/test_numpy_dtypes.py | 464 +++ .../pybind11/tests/test_numpy_scalars.cpp | 63 + .../pybind11/tests/test_numpy_scalars.py | 54 + .../pybind11/tests/test_numpy_vectorize.cpp | 137 + .../pybind11/tests/test_numpy_vectorize.py | 319 ++ .../pybind11/tests/test_opaque_types.cpp | 77 + .../pybind11/tests/test_opaque_types.py | 64 + .../tests/test_operator_overloading.cpp | 281 ++ .../tests/test_operator_overloading.py | 161 + .../pybind11/tests/test_pickling.cpp | 191 + .../pybind11/tests/test_pickling.py | 149 + .../test_potentially_slicing_weak_ptr.cpp | 170 + .../test_potentially_slicing_weak_ptr.py | 174 + .../test_python_multiple_inheritance.cpp | 45 + .../tests/test_python_multiple_inheritance.py | 36 + ...est_pytorch_shared_ptr_cast_regression.cpp | 62 + ...test_pytorch_shared_ptr_cast_regression.py | 25 + .../pybind11/tests/test_pytypes.cpp | 1215 ++++++ .../pybind11/tests/test_pytypes.py | 1374 +++++++ .../tests/test_scoped_critical_section.cpp | 274 ++ .../tests/test_scoped_critical_section.py | 30 + .../tests/test_sequences_and_iterators.cpp | 600 +++ .../tests/test_sequences_and_iterators.py | 307 ++ .../pybind11/tests/test_smart_ptr.cpp | 627 +++ .../pybind11/tests/test_smart_ptr.py | 370 ++ .../tests/test_standalone_enum_module.py | 18 + .../pybind11/tests/test_stl.cpp | 667 ++++ external_libraries/pybind11/tests/test_stl.py | 735 ++++ .../pybind11/tests/test_stl_binders.cpp | 275 ++ .../pybind11/tests/test_stl_binders.py | 414 ++ .../tests/test_tagbased_polymorphic.cpp | 150 + .../tests/test_tagbased_polymorphic.py | 30 + .../pybind11/tests/test_thread.cpp | 108 + .../pybind11/tests/test_thread.py | 80 + .../tests/test_type_caster_pyobject_ptr.cpp | 168 + .../tests/test_type_caster_pyobject_ptr.py | 125 + ...pe_caster_std_function_specializations.cpp | 46 + ...ype_caster_std_function_specializations.py | 15 + .../pybind11/tests/test_union.cpp | 22 + .../pybind11/tests/test_union.py | 10 + .../tests/test_unnamed_namespace_a.cpp | 37 + .../tests/test_unnamed_namespace_a.py | 33 + .../tests/test_unnamed_namespace_b.cpp | 13 + .../tests/test_unnamed_namespace_b.py | 7 + .../tests/test_vector_unique_ptr_member.cpp | 54 + .../tests/test_vector_unique_ptr_member.py | 16 + .../pybind11/tests/test_virtual_functions.cpp | 591 +++ .../pybind11/tests/test_virtual_functions.py | 468 +++ .../pybind11/tests/test_warnings.cpp | 46 + .../pybind11/tests/test_warnings.py | 68 + .../tests/test_with_catch/CMakeLists.txt | 64 + .../pybind11/tests/test_with_catch/catch.cpp | 175 + .../tests/test_with_catch/catch_skip.h | 21 + .../tests/test_with_catch/external_module.cpp | 39 + .../test_args_convert_vector.cpp | 80 + .../test_with_catch/test_argument_vector.cpp | 94 + .../test_with_catch/test_interpreter.cpp | 511 +++ .../tests/test_with_catch/test_interpreter.py | 16 + .../test_with_catch/test_subinterpreter.cpp | 579 +++ .../tests/test_with_catch/test_trampoline.py | 18 + .../pybind11/tests/valgrind-numpy-scipy.supp | 140 + .../pybind11/tests/valgrind-python.supp | 117 + .../pybind11/tools/FindCatch.cmake | 2 - .../pybind11/tools/FindPythonLibsNew.cmake | 39 +- .../codespell_ignore_lines_from_errors.py | 5 +- external_libraries/pybind11/tools/libsize.py | 2 + .../pybind11/tools/make_changelog.py | 105 +- .../pybind11/tools/make_global.py | 33 + .../pybind11/tools/pybind11Common.cmake | 195 +- .../pybind11/tools/pybind11Config.cmake.in | 15 +- .../tools/pybind11GuessPythonExtSuffix.cmake | 94 + .../pybind11/tools/pybind11NewTools.cmake | 209 +- .../pybind11/tools/pybind11Tools.cmake | 72 +- .../pybind11/tools/pyproject.toml | 3 - .../pybind11/tools/setup_global.py.in | 63 - .../pybind11/tools/setup_main.py.in | 44 - .../test-pybind11GuessPythonExtSuffix.cmake | 185 + 333 files changed, 66533 insertions(+), 2648 deletions(-) create mode 100644 external_libraries/pybind11/CMakePresets.json delete mode 100644 external_libraries/pybind11/MANIFEST.in create mode 100644 external_libraries/pybind11/docs/Doxyfile create mode 100644 external_libraries/pybind11/docs/_static/css/custom.css create mode 100644 external_libraries/pybind11/docs/advanced/cast/chrono.rst create mode 100644 external_libraries/pybind11/docs/advanced/cast/custom.rst create mode 100644 external_libraries/pybind11/docs/advanced/cast/eigen.rst create mode 100644 external_libraries/pybind11/docs/advanced/cast/functional.rst create mode 100644 external_libraries/pybind11/docs/advanced/cast/index.rst create mode 100644 external_libraries/pybind11/docs/advanced/cast/overview.rst create mode 100644 external_libraries/pybind11/docs/advanced/cast/stl.rst create mode 100644 external_libraries/pybind11/docs/advanced/cast/strings.rst create mode 100644 external_libraries/pybind11/docs/advanced/classes.rst create mode 100644 external_libraries/pybind11/docs/advanced/deadlock.md create mode 100644 external_libraries/pybind11/docs/advanced/deprecated.rst create mode 100644 external_libraries/pybind11/docs/advanced/embedding.rst create mode 100644 external_libraries/pybind11/docs/advanced/exceptions.rst create mode 100644 external_libraries/pybind11/docs/advanced/functions.rst create mode 100644 external_libraries/pybind11/docs/advanced/misc.rst create mode 100644 external_libraries/pybind11/docs/advanced/pycpp/index.rst create mode 100644 external_libraries/pybind11/docs/advanced/pycpp/numpy.rst create mode 100644 external_libraries/pybind11/docs/advanced/pycpp/object.rst create mode 100644 external_libraries/pybind11/docs/advanced/pycpp/utilities.rst create mode 100644 external_libraries/pybind11/docs/advanced/smart_ptrs.rst create mode 100644 external_libraries/pybind11/docs/basics.rst create mode 100644 external_libraries/pybind11/docs/benchmark.py create mode 100644 external_libraries/pybind11/docs/benchmark.rst create mode 100644 external_libraries/pybind11/docs/changelog.md create mode 100644 external_libraries/pybind11/docs/classes.rst create mode 100644 external_libraries/pybind11/docs/cmake/index.rst create mode 100644 external_libraries/pybind11/docs/compiling.rst create mode 100644 external_libraries/pybind11/docs/conf.py create mode 100644 external_libraries/pybind11/docs/faq.rst create mode 100644 external_libraries/pybind11/docs/index.rst create mode 100644 external_libraries/pybind11/docs/installing.rst create mode 100644 external_libraries/pybind11/docs/limitations.rst create mode 100644 external_libraries/pybind11/docs/pybind11-logo.png create mode 100644 external_libraries/pybind11/docs/pybind11_vs_boost_python1.png create mode 100644 external_libraries/pybind11/docs/pybind11_vs_boost_python1.svg create mode 100644 external_libraries/pybind11/docs/pybind11_vs_boost_python2.png create mode 100644 external_libraries/pybind11/docs/pybind11_vs_boost_python2.svg create mode 100644 external_libraries/pybind11/docs/reference.rst create mode 100644 external_libraries/pybind11/docs/release.rst create mode 100644 external_libraries/pybind11/docs/requirements.in create mode 100644 external_libraries/pybind11/docs/requirements.txt create mode 100644 external_libraries/pybind11/docs/upgrade.rst create mode 100644 external_libraries/pybind11/include/pybind11/conduit/README.txt create mode 100644 external_libraries/pybind11/include/pybind11/conduit/pybind11_conduit_v1.h create mode 100644 external_libraries/pybind11/include/pybind11/conduit/pybind11_platform_abi_id.h create mode 100644 external_libraries/pybind11/include/pybind11/conduit/wrap_include_python_h.h create mode 100644 external_libraries/pybind11/include/pybind11/critical_section.h create mode 100644 external_libraries/pybind11/include/pybind11/detail/argument_vector.h create mode 100644 external_libraries/pybind11/include/pybind11/detail/cpp_conduit.h create mode 100644 external_libraries/pybind11/include/pybind11/detail/dynamic_raw_ptr_cast_if_possible.h create mode 100644 external_libraries/pybind11/include/pybind11/detail/exception_translation.h create mode 100644 external_libraries/pybind11/include/pybind11/detail/function_record_pyobject.h create mode 100644 external_libraries/pybind11/include/pybind11/detail/holder_caster_foreign_helpers.h create mode 100644 external_libraries/pybind11/include/pybind11/detail/native_enum_data.h create mode 100644 external_libraries/pybind11/include/pybind11/detail/pybind11_namespace_macros.h create mode 100644 external_libraries/pybind11/include/pybind11/detail/struct_smart_holder.h create mode 100644 external_libraries/pybind11/include/pybind11/detail/using_smart_holder.h create mode 100644 external_libraries/pybind11/include/pybind11/detail/value_and_holder.h create mode 100644 external_libraries/pybind11/include/pybind11/gil_safe_call_once.h create mode 100644 external_libraries/pybind11/include/pybind11/gil_simple.h create mode 100644 external_libraries/pybind11/include/pybind11/native_enum.h create mode 100644 external_libraries/pybind11/include/pybind11/subinterpreter.h create mode 100644 external_libraries/pybind11/include/pybind11/trampoline_self_life_support.h create mode 100644 external_libraries/pybind11/include/pybind11/typing.h create mode 100644 external_libraries/pybind11/include/pybind11/warnings.h create mode 100644 external_libraries/pybind11/pybind11/__init__.py create mode 100644 external_libraries/pybind11/pybind11/__main__.py create mode 100644 external_libraries/pybind11/pybind11/_version.py create mode 100644 external_libraries/pybind11/pybind11/commands.py create mode 100644 external_libraries/pybind11/pybind11/py.typed create mode 100644 external_libraries/pybind11/pybind11/setup_helpers.py delete mode 100644 external_libraries/pybind11/setup.cfg delete mode 100644 external_libraries/pybind11/setup.py create mode 100644 external_libraries/pybind11/tests/CMakeLists.txt create mode 100644 external_libraries/pybind11/tests/conftest.py create mode 100644 external_libraries/pybind11/tests/constructor_stats.h create mode 100644 external_libraries/pybind11/tests/cross_module_gil_utils.cpp create mode 100644 external_libraries/pybind11/tests/cross_module_interleaved_error_already_set.cpp create mode 100644 external_libraries/pybind11/tests/custom_exceptions.py create mode 100644 external_libraries/pybind11/tests/eigen_tensor_avoid_stl_array.cpp create mode 100644 external_libraries/pybind11/tests/env.py create mode 100644 external_libraries/pybind11/tests/exo_planet_c_api.cpp create mode 100644 external_libraries/pybind11/tests/exo_planet_pybind11.cpp create mode 100644 external_libraries/pybind11/tests/extra_python_package/pytest.ini create mode 100644 external_libraries/pybind11/tests/extra_python_package/test_files.py create mode 100644 external_libraries/pybind11/tests/extra_setuptools/pytest.ini create mode 100644 external_libraries/pybind11/tests/extra_setuptools/test_setuphelper.py create mode 100644 external_libraries/pybind11/tests/home_planet_very_lonely_traveler.cpp create mode 100644 external_libraries/pybind11/tests/local_bindings.h create mode 100644 external_libraries/pybind11/tests/mod_per_interpreter_gil.cpp create mode 100644 external_libraries/pybind11/tests/mod_per_interpreter_gil_with_singleton.cpp create mode 100644 external_libraries/pybind11/tests/mod_shared_interpreter_gil.cpp create mode 100644 external_libraries/pybind11/tests/object.h create mode 100644 external_libraries/pybind11/tests/pure_cpp/CMakeLists.txt create mode 100644 external_libraries/pybind11/tests/pure_cpp/smart_holder_poc.h create mode 100644 external_libraries/pybind11/tests/pure_cpp/smart_holder_poc_test.cpp create mode 100644 external_libraries/pybind11/tests/pybind11_cross_module_tests.cpp create mode 100644 external_libraries/pybind11/tests/pybind11_tests.cpp create mode 100644 external_libraries/pybind11/tests/pybind11_tests.h create mode 100644 external_libraries/pybind11/tests/pyproject.toml create mode 100644 external_libraries/pybind11/tests/pytest.ini create mode 100644 external_libraries/pybind11/tests/requirements.txt create mode 100644 external_libraries/pybind11/tests/standalone_enum_module.cpp create mode 100644 external_libraries/pybind11/tests/test_async.cpp create mode 100644 external_libraries/pybind11/tests/test_async.py create mode 100644 external_libraries/pybind11/tests/test_buffers.cpp create mode 100644 external_libraries/pybind11/tests/test_buffers.py create mode 100644 external_libraries/pybind11/tests/test_builtin_casters.cpp create mode 100644 external_libraries/pybind11/tests/test_builtin_casters.py create mode 100644 external_libraries/pybind11/tests/test_call_policies.cpp create mode 100644 external_libraries/pybind11/tests/test_call_policies.py create mode 100644 external_libraries/pybind11/tests/test_callbacks.cpp create mode 100644 external_libraries/pybind11/tests/test_callbacks.py create mode 100644 external_libraries/pybind11/tests/test_chrono.cpp create mode 100644 external_libraries/pybind11/tests/test_chrono.py create mode 100644 external_libraries/pybind11/tests/test_class.cpp create mode 100644 external_libraries/pybind11/tests/test_class.py create mode 100644 external_libraries/pybind11/tests/test_class_cross_module_use_after_one_module_dealloc.cpp create mode 100644 external_libraries/pybind11/tests/test_class_cross_module_use_after_one_module_dealloc.py create mode 100644 external_libraries/pybind11/tests/test_class_release_gil_before_calling_cpp_dtor.cpp create mode 100644 external_libraries/pybind11/tests/test_class_release_gil_before_calling_cpp_dtor.py create mode 100644 external_libraries/pybind11/tests/test_class_sh_basic.cpp create mode 100644 external_libraries/pybind11/tests/test_class_sh_basic.py create mode 100644 external_libraries/pybind11/tests/test_class_sh_disowning.cpp create mode 100644 external_libraries/pybind11/tests/test_class_sh_disowning.py create mode 100644 external_libraries/pybind11/tests/test_class_sh_disowning_mi.cpp create mode 100644 external_libraries/pybind11/tests/test_class_sh_disowning_mi.py create mode 100644 external_libraries/pybind11/tests/test_class_sh_factory_constructors.cpp create mode 100644 external_libraries/pybind11/tests/test_class_sh_factory_constructors.py create mode 100644 external_libraries/pybind11/tests/test_class_sh_inheritance.cpp create mode 100644 external_libraries/pybind11/tests/test_class_sh_inheritance.py create mode 100644 external_libraries/pybind11/tests/test_class_sh_mi_thunks.cpp create mode 100644 external_libraries/pybind11/tests/test_class_sh_mi_thunks.py create mode 100644 external_libraries/pybind11/tests/test_class_sh_property.cpp create mode 100644 external_libraries/pybind11/tests/test_class_sh_property.py create mode 100644 external_libraries/pybind11/tests/test_class_sh_property_non_owning.cpp create mode 100644 external_libraries/pybind11/tests/test_class_sh_property_non_owning.py create mode 100644 external_libraries/pybind11/tests/test_class_sh_shared_ptr_copy_move.cpp create mode 100644 external_libraries/pybind11/tests/test_class_sh_shared_ptr_copy_move.py create mode 100644 external_libraries/pybind11/tests/test_class_sh_trampoline_basic.cpp create mode 100644 external_libraries/pybind11/tests/test_class_sh_trampoline_basic.py create mode 100644 external_libraries/pybind11/tests/test_class_sh_trampoline_self_life_support.cpp create mode 100644 external_libraries/pybind11/tests/test_class_sh_trampoline_self_life_support.py create mode 100644 external_libraries/pybind11/tests/test_class_sh_trampoline_shared_from_this.cpp create mode 100644 external_libraries/pybind11/tests/test_class_sh_trampoline_shared_from_this.py create mode 100644 external_libraries/pybind11/tests/test_class_sh_trampoline_shared_ptr_cpp_arg.cpp create mode 100644 external_libraries/pybind11/tests/test_class_sh_trampoline_shared_ptr_cpp_arg.py create mode 100644 external_libraries/pybind11/tests/test_class_sh_trampoline_unique_ptr.cpp create mode 100644 external_libraries/pybind11/tests/test_class_sh_trampoline_unique_ptr.py create mode 100644 external_libraries/pybind11/tests/test_class_sh_unique_ptr_custom_deleter.cpp create mode 100644 external_libraries/pybind11/tests/test_class_sh_unique_ptr_custom_deleter.py create mode 100644 external_libraries/pybind11/tests/test_class_sh_unique_ptr_member.cpp create mode 100644 external_libraries/pybind11/tests/test_class_sh_unique_ptr_member.py create mode 100644 external_libraries/pybind11/tests/test_class_sh_virtual_py_cpp_mix.cpp create mode 100644 external_libraries/pybind11/tests/test_class_sh_virtual_py_cpp_mix.py create mode 100644 external_libraries/pybind11/tests/test_cmake_build/CMakeLists.txt create mode 100644 external_libraries/pybind11/tests/test_cmake_build/embed.cpp create mode 100644 external_libraries/pybind11/tests/test_cmake_build/installed_embed/CMakeLists.txt create mode 100644 external_libraries/pybind11/tests/test_cmake_build/installed_function/CMakeLists.txt create mode 100644 external_libraries/pybind11/tests/test_cmake_build/installed_target/CMakeLists.txt create mode 100644 external_libraries/pybind11/tests/test_cmake_build/main.cpp create mode 100644 external_libraries/pybind11/tests/test_cmake_build/subdirectory_embed/CMakeLists.txt create mode 100644 external_libraries/pybind11/tests/test_cmake_build/subdirectory_function/CMakeLists.txt create mode 100644 external_libraries/pybind11/tests/test_cmake_build/subdirectory_target/CMakeLists.txt create mode 100644 external_libraries/pybind11/tests/test_cmake_build/test.py create mode 100644 external_libraries/pybind11/tests/test_const_name.cpp create mode 100644 external_libraries/pybind11/tests/test_const_name.py create mode 100644 external_libraries/pybind11/tests/test_constants_and_functions.cpp create mode 100644 external_libraries/pybind11/tests/test_constants_and_functions.py create mode 100644 external_libraries/pybind11/tests/test_copy_move.cpp create mode 100644 external_libraries/pybind11/tests/test_copy_move.py create mode 100644 external_libraries/pybind11/tests/test_cpp_conduit.cpp create mode 100644 external_libraries/pybind11/tests/test_cpp_conduit.py create mode 100644 external_libraries/pybind11/tests/test_cpp_conduit_traveler_bindings.h create mode 100644 external_libraries/pybind11/tests/test_cpp_conduit_traveler_types.h create mode 100644 external_libraries/pybind11/tests/test_cross_module_rtti/CMakeLists.txt create mode 100644 external_libraries/pybind11/tests/test_cross_module_rtti/bindings.cpp create mode 100644 external_libraries/pybind11/tests/test_cross_module_rtti/catch.cpp create mode 100644 external_libraries/pybind11/tests/test_cross_module_rtti/lib.cpp create mode 100644 external_libraries/pybind11/tests/test_cross_module_rtti/lib.h create mode 100644 external_libraries/pybind11/tests/test_cross_module_rtti/test_cross_module_rtti.cpp create mode 100644 external_libraries/pybind11/tests/test_custom_type_casters.cpp create mode 100644 external_libraries/pybind11/tests/test_custom_type_casters.py create mode 100644 external_libraries/pybind11/tests/test_custom_type_setup.cpp create mode 100644 external_libraries/pybind11/tests/test_custom_type_setup.py create mode 100644 external_libraries/pybind11/tests/test_docs_advanced_cast_custom.cpp create mode 100644 external_libraries/pybind11/tests/test_docs_advanced_cast_custom.py create mode 100644 external_libraries/pybind11/tests/test_docstring_options.cpp create mode 100644 external_libraries/pybind11/tests/test_docstring_options.py create mode 100644 external_libraries/pybind11/tests/test_eigen_matrix.cpp create mode 100644 external_libraries/pybind11/tests/test_eigen_matrix.py create mode 100644 external_libraries/pybind11/tests/test_eigen_tensor.cpp create mode 100644 external_libraries/pybind11/tests/test_eigen_tensor.inl create mode 100644 external_libraries/pybind11/tests/test_eigen_tensor.py create mode 100644 external_libraries/pybind11/tests/test_enum.cpp create mode 100644 external_libraries/pybind11/tests/test_enum.py create mode 100644 external_libraries/pybind11/tests/test_eval.cpp create mode 100644 external_libraries/pybind11/tests/test_eval.py create mode 100644 external_libraries/pybind11/tests/test_eval_call.py create mode 100644 external_libraries/pybind11/tests/test_exceptions.cpp create mode 100644 external_libraries/pybind11/tests/test_exceptions.h create mode 100644 external_libraries/pybind11/tests/test_exceptions.py create mode 100644 external_libraries/pybind11/tests/test_factory_constructors.cpp create mode 100644 external_libraries/pybind11/tests/test_factory_constructors.py create mode 100644 external_libraries/pybind11/tests/test_gil_scoped.cpp create mode 100644 external_libraries/pybind11/tests/test_gil_scoped.py create mode 100644 external_libraries/pybind11/tests/test_iostream.cpp create mode 100644 external_libraries/pybind11/tests/test_iostream.py create mode 100644 external_libraries/pybind11/tests/test_kwargs_and_defaults.cpp create mode 100644 external_libraries/pybind11/tests/test_kwargs_and_defaults.py create mode 100644 external_libraries/pybind11/tests/test_local_bindings.cpp create mode 100644 external_libraries/pybind11/tests/test_local_bindings.py create mode 100644 external_libraries/pybind11/tests/test_methods_and_attributes.cpp create mode 100644 external_libraries/pybind11/tests/test_methods_and_attributes.py create mode 100644 external_libraries/pybind11/tests/test_modules.cpp create mode 100644 external_libraries/pybind11/tests/test_modules.py create mode 100644 external_libraries/pybind11/tests/test_multiple_inheritance.cpp create mode 100644 external_libraries/pybind11/tests/test_multiple_inheritance.py create mode 100644 external_libraries/pybind11/tests/test_multiple_interpreters.py create mode 100644 external_libraries/pybind11/tests/test_native_enum.cpp create mode 100644 external_libraries/pybind11/tests/test_native_enum.py create mode 100644 external_libraries/pybind11/tests/test_numpy_array.cpp create mode 100644 external_libraries/pybind11/tests/test_numpy_array.py create mode 100644 external_libraries/pybind11/tests/test_numpy_dtypes.cpp create mode 100644 external_libraries/pybind11/tests/test_numpy_dtypes.py create mode 100644 external_libraries/pybind11/tests/test_numpy_scalars.cpp create mode 100644 external_libraries/pybind11/tests/test_numpy_scalars.py create mode 100644 external_libraries/pybind11/tests/test_numpy_vectorize.cpp create mode 100644 external_libraries/pybind11/tests/test_numpy_vectorize.py create mode 100644 external_libraries/pybind11/tests/test_opaque_types.cpp create mode 100644 external_libraries/pybind11/tests/test_opaque_types.py create mode 100644 external_libraries/pybind11/tests/test_operator_overloading.cpp create mode 100644 external_libraries/pybind11/tests/test_operator_overloading.py create mode 100644 external_libraries/pybind11/tests/test_pickling.cpp create mode 100644 external_libraries/pybind11/tests/test_pickling.py create mode 100644 external_libraries/pybind11/tests/test_potentially_slicing_weak_ptr.cpp create mode 100644 external_libraries/pybind11/tests/test_potentially_slicing_weak_ptr.py create mode 100644 external_libraries/pybind11/tests/test_python_multiple_inheritance.cpp create mode 100644 external_libraries/pybind11/tests/test_python_multiple_inheritance.py create mode 100644 external_libraries/pybind11/tests/test_pytorch_shared_ptr_cast_regression.cpp create mode 100644 external_libraries/pybind11/tests/test_pytorch_shared_ptr_cast_regression.py create mode 100644 external_libraries/pybind11/tests/test_pytypes.cpp create mode 100644 external_libraries/pybind11/tests/test_pytypes.py create mode 100644 external_libraries/pybind11/tests/test_scoped_critical_section.cpp create mode 100644 external_libraries/pybind11/tests/test_scoped_critical_section.py create mode 100644 external_libraries/pybind11/tests/test_sequences_and_iterators.cpp create mode 100644 external_libraries/pybind11/tests/test_sequences_and_iterators.py create mode 100644 external_libraries/pybind11/tests/test_smart_ptr.cpp create mode 100644 external_libraries/pybind11/tests/test_smart_ptr.py create mode 100644 external_libraries/pybind11/tests/test_standalone_enum_module.py create mode 100644 external_libraries/pybind11/tests/test_stl.cpp create mode 100644 external_libraries/pybind11/tests/test_stl.py create mode 100644 external_libraries/pybind11/tests/test_stl_binders.cpp create mode 100644 external_libraries/pybind11/tests/test_stl_binders.py create mode 100644 external_libraries/pybind11/tests/test_tagbased_polymorphic.cpp create mode 100644 external_libraries/pybind11/tests/test_tagbased_polymorphic.py create mode 100644 external_libraries/pybind11/tests/test_thread.cpp create mode 100644 external_libraries/pybind11/tests/test_thread.py create mode 100644 external_libraries/pybind11/tests/test_type_caster_pyobject_ptr.cpp create mode 100644 external_libraries/pybind11/tests/test_type_caster_pyobject_ptr.py create mode 100644 external_libraries/pybind11/tests/test_type_caster_std_function_specializations.cpp create mode 100644 external_libraries/pybind11/tests/test_type_caster_std_function_specializations.py create mode 100644 external_libraries/pybind11/tests/test_union.cpp create mode 100644 external_libraries/pybind11/tests/test_union.py create mode 100644 external_libraries/pybind11/tests/test_unnamed_namespace_a.cpp create mode 100644 external_libraries/pybind11/tests/test_unnamed_namespace_a.py create mode 100644 external_libraries/pybind11/tests/test_unnamed_namespace_b.cpp create mode 100644 external_libraries/pybind11/tests/test_unnamed_namespace_b.py create mode 100644 external_libraries/pybind11/tests/test_vector_unique_ptr_member.cpp create mode 100644 external_libraries/pybind11/tests/test_vector_unique_ptr_member.py create mode 100644 external_libraries/pybind11/tests/test_virtual_functions.cpp create mode 100644 external_libraries/pybind11/tests/test_virtual_functions.py create mode 100644 external_libraries/pybind11/tests/test_warnings.cpp create mode 100644 external_libraries/pybind11/tests/test_warnings.py create mode 100644 external_libraries/pybind11/tests/test_with_catch/CMakeLists.txt create mode 100644 external_libraries/pybind11/tests/test_with_catch/catch.cpp create mode 100644 external_libraries/pybind11/tests/test_with_catch/catch_skip.h create mode 100644 external_libraries/pybind11/tests/test_with_catch/external_module.cpp create mode 100644 external_libraries/pybind11/tests/test_with_catch/test_args_convert_vector.cpp create mode 100644 external_libraries/pybind11/tests/test_with_catch/test_argument_vector.cpp create mode 100644 external_libraries/pybind11/tests/test_with_catch/test_interpreter.cpp create mode 100644 external_libraries/pybind11/tests/test_with_catch/test_interpreter.py create mode 100644 external_libraries/pybind11/tests/test_with_catch/test_subinterpreter.cpp create mode 100644 external_libraries/pybind11/tests/test_with_catch/test_trampoline.py create mode 100644 external_libraries/pybind11/tests/valgrind-numpy-scipy.supp create mode 100644 external_libraries/pybind11/tests/valgrind-python.supp create mode 100755 external_libraries/pybind11/tools/make_global.py create mode 100644 external_libraries/pybind11/tools/pybind11GuessPythonExtSuffix.cmake delete mode 100644 external_libraries/pybind11/tools/pyproject.toml delete mode 100644 external_libraries/pybind11/tools/setup_global.py.in delete mode 100644 external_libraries/pybind11/tools/setup_main.py.in create mode 100644 external_libraries/pybind11/tools/test-pybind11GuessPythonExtSuffix.cmake diff --git a/external_libraries/pybind11/CMakeLists.txt b/external_libraries/pybind11/CMakeLists.txt index 87ec1034..097b4eba 100644 --- a/external_libraries/pybind11/CMakeLists.txt +++ b/external_libraries/pybind11/CMakeLists.txt @@ -5,21 +5,20 @@ # All rights reserved. Use of this source code is governed by a # BSD-style license that can be found in the LICENSE file. -cmake_minimum_required(VERSION 3.5) +# Propagate this policy (FindPythonInterp removal) so it can be detected later +if(NOT CMAKE_VERSION VERSION_LESS "3.27") + cmake_policy(GET CMP0148 _pybind11_cmp0148) +endif() -# The `cmake_minimum_required(VERSION 3.5...3.26)` syntax does not work with -# some versions of VS that have a patched CMake 3.11. This forces us to emulate -# the behavior using the following workaround: -if(${CMAKE_VERSION} VERSION_LESS 3.26) - cmake_policy(VERSION ${CMAKE_MAJOR_VERSION}.${CMAKE_MINOR_VERSION}) -else() - cmake_policy(VERSION 3.26) +cmake_minimum_required(VERSION 3.15...4.2) + +if(_pybind11_cmp0148) + cmake_policy(SET CMP0148 ${_pybind11_cmp0148}) + unset(_pybind11_cmp0148) endif() # Avoid infinite recursion if tests include this as a subdirectory -if(DEFINED PYBIND11_MASTER_PROJECT) - return() -endif() +include_guard(GLOBAL) # Extract project version from source file(STRINGS "${CMAKE_CURRENT_SOURCE_DIR}/include/pybind11/detail/common.h" @@ -64,16 +63,15 @@ if(CMAKE_SOURCE_DIR STREQUAL PROJECT_SOURCE_DIR) set(PYBIND11_MASTER_PROJECT ON) - if(OSX AND CMAKE_VERSION VERSION_LESS 3.7) - # Bug in macOS CMake < 3.7 is unable to download catch - message(WARNING "CMAKE 3.7+ needed on macOS to download catch, and newer HIGHLY recommended") - elseif(WINDOWS AND CMAKE_VERSION VERSION_LESS 3.8) - # Only tested with 3.8+ in CI. - message(WARNING "CMAKE 3.8+ tested on Windows, previous versions untested") - endif() - message(STATUS "CMake ${CMAKE_VERSION}") + if(DEFINED SKBUILD AND DEFINED ENV{PYBIND11_GLOBAL_SDIST}) + message( + FATAL_ERROR + "PYBIND11_GLOBAL_SDIST is not supported, use nox -s build_global or a pybind11-global SDist instead." + ) + endif() + if(CMAKE_CXX_STANDARD) set(CMAKE_CXX_EXTENSIONS OFF) set(CMAKE_CXX_STANDARD_REQUIRED ON) @@ -82,21 +80,33 @@ if(CMAKE_SOURCE_DIR STREQUAL PROJECT_SOURCE_DIR) set(pybind11_system "") set_property(GLOBAL PROPERTY USE_FOLDERS ON) + if(CMAKE_VERSION VERSION_LESS "3.18") + set(_pybind11_findpython_default OFF) + else() + set(_pybind11_findpython_default ON) + endif() else() set(PYBIND11_MASTER_PROJECT OFF) set(pybind11_system SYSTEM) + set(_pybind11_findpython_default COMPAT) endif() # Options option(PYBIND11_INSTALL "Install pybind11 header files?" ${PYBIND11_MASTER_PROJECT}) option(PYBIND11_TEST "Build pybind11 test suite?" ${PYBIND11_MASTER_PROJECT}) option(PYBIND11_NOPYTHON "Disable search for Python" OFF) +option(PYBIND11_DISABLE_HANDLE_TYPE_NAME_DEFAULT_IMPLEMENTATION + "To enforce that a handle_type_name<> specialization exists" OFF) option(PYBIND11_SIMPLE_GIL_MANAGEMENT "Use simpler GIL management logic that does not support disassociation" OFF) set(PYBIND11_INTERNALS_VERSION "" CACHE STRING "Override the ABI version, may be used to enable the unstable ABI.") +option(PYBIND11_USE_CROSSCOMPILING "Respect CMAKE_CROSSCOMPILING" OFF) +if(PYBIND11_DISABLE_HANDLE_TYPE_NAME_DEFAULT_IMPLEMENTATION) + add_compile_definitions(PYBIND11_DISABLE_HANDLE_TYPE_NAME_DEFAULT_IMPLEMENTATION) +endif() if(PYBIND11_SIMPLE_GIL_MANAGEMENT) add_compile_definitions(PYBIND11_SIMPLE_GIL_MANAGEMENT) endif() @@ -106,24 +116,98 @@ cmake_dependent_option( "Install pybind11 headers in Python include directory instead of default installation prefix" OFF "PYBIND11_INSTALL" OFF) -cmake_dependent_option(PYBIND11_FINDPYTHON "Force new FindPython" OFF - "NOT CMAKE_VERSION VERSION_LESS 3.12" OFF) +set(PYBIND11_FINDPYTHON + ${_pybind11_findpython_default} + CACHE STRING "Force new FindPython - NEW, OLD, COMPAT") + +if(PYBIND11_MASTER_PROJECT) + + # Allow PYTHON_EXECUTABLE if in FINDPYTHON mode and building pybind11's tests + # (makes transition easier while we support both modes). + if(PYBIND11_FINDPYTHON + AND DEFINED PYTHON_EXECUTABLE + AND NOT DEFINED Python_EXECUTABLE) + set(Python_EXECUTABLE "${PYTHON_EXECUTABLE}") + endif() + + # This is a shortcut that is primarily for the venv cmake preset, + # but can be used to quickly setup tests manually, too + set(PYBIND11_CREATE_WITH_UV + "" + CACHE STRING "Create a virtualenv if it doesn't exist") + + if(NOT PYBIND11_CREATE_WITH_UV STREQUAL "") + set(Python_ROOT_DIR "${CMAKE_CURRENT_BINARY_DIR}/.venv") + if(EXISTS "${Python_ROOT_DIR}") + if(EXISTS "${CMAKE_BINARY_DIR}/CMakeCache.txt") + message(STATUS "Using existing venv at ${Python_ROOT_DIR}, remove or --fresh to recreate") + else() + # --fresh used to remove the cache + file(REMOVE_RECURSE "${CMAKE_CURRENT_BINARY_DIR}/.venv") + endif() + endif() + if(NOT EXISTS "${Python_ROOT_DIR}") + find_program(UV uv REQUIRED) + # CMake 3.19+ would be able to use COMMAND_ERROR_IS_FATAL + message( + STATUS "Creating venv with ${UV} venv -p ${PYBIND11_CREATE_WITH_UV} '${Python_ROOT_DIR}'") + execute_process(COMMAND ${UV} venv -p ${PYBIND11_CREATE_WITH_UV} "${Python_ROOT_DIR}" + RESULT_VARIABLE _venv_result) + if(_venv_result AND NOT _venv_result EQUAL 0) + message(FATAL_ERROR "uv venv failed with '${_venv_result}'") + endif() + message( + STATUS + "Installing deps with ${UV} pip install -p '${Python_ROOT_DIR}' -r tests/requirements.txt" + ) + execute_process( + COMMAND ${UV} pip install -p "${Python_ROOT_DIR}" -r + "${CMAKE_CURRENT_SOURCE_DIR}/tests/requirements.txt" RESULT_VARIABLE _pip_result) + if(_pip_result AND NOT _pip_result EQUAL 0) + message(FATAL_ERROR "uv pip install failed with '${_pip_result}'") + endif() + endif() + else() + if(NOT DEFINED Python3_EXECUTABLE + AND NOT DEFINED Python_EXECUTABLE + AND NOT DEFINED Python_ROOT_DIR + AND NOT DEFINED ENV{VIRTUALENV} + AND EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/.venv") + message(STATUS "Autodetecting Python in virtual environment") + set(Python_ROOT_DIR "${CMAKE_CURRENT_SOURCE_DIR}/.venv") + endif() + endif() +endif() -# NB: when adding a header don't forget to also add it to setup.py set(PYBIND11_HEADERS + include/pybind11/detail/argument_vector.h include/pybind11/detail/class.h include/pybind11/detail/common.h + include/pybind11/detail/cpp_conduit.h include/pybind11/detail/descr.h + include/pybind11/detail/dynamic_raw_ptr_cast_if_possible.h + include/pybind11/detail/exception_translation.h + include/pybind11/detail/function_record_pyobject.h + include/pybind11/detail/holder_caster_foreign_helpers.h include/pybind11/detail/init.h include/pybind11/detail/internals.h + include/pybind11/detail/native_enum_data.h + include/pybind11/detail/pybind11_namespace_macros.h + include/pybind11/detail/struct_smart_holder.h include/pybind11/detail/type_caster_base.h include/pybind11/detail/typeid.h + include/pybind11/detail/using_smart_holder.h + include/pybind11/detail/value_and_holder.h include/pybind11/attr.h include/pybind11/buffer_info.h include/pybind11/cast.h include/pybind11/chrono.h include/pybind11/common.h include/pybind11/complex.h + include/pybind11/conduit/pybind11_conduit_v1.h + include/pybind11/conduit/pybind11_platform_abi_id.h + include/pybind11/conduit/wrap_include_python_h.h + include/pybind11/critical_section.h include/pybind11/options.h include/pybind11/eigen.h include/pybind11/eigen/common.h @@ -132,19 +216,26 @@ set(PYBIND11_HEADERS include/pybind11/embed.h include/pybind11/eval.h include/pybind11/gil.h + include/pybind11/gil_safe_call_once.h + include/pybind11/gil_simple.h include/pybind11/iostream.h include/pybind11/functional.h + include/pybind11/native_enum.h include/pybind11/numpy.h include/pybind11/operators.h include/pybind11/pybind11.h include/pybind11/pytypes.h + include/pybind11/subinterpreter.h include/pybind11/stl.h include/pybind11/stl_bind.h include/pybind11/stl/filesystem.h - include/pybind11/type_caster_pyobject_ptr.h) + include/pybind11/trampoline_self_life_support.h + include/pybind11/type_caster_pyobject_ptr.h + include/pybind11/typing.h + include/pybind11/warnings.h) # Compare with grep and warn if mismatched -if(PYBIND11_MASTER_PROJECT AND NOT CMAKE_VERSION VERSION_LESS 3.12) +if(PYBIND11_MASTER_PROJECT) file( GLOB_RECURSE _pybind11_header_check LIST_DIRECTORIES false @@ -162,10 +253,7 @@ if(PYBIND11_MASTER_PROJECT AND NOT CMAKE_VERSION VERSION_LESS 3.12) endif() endif() -# CMake 3.12 added list(TRANSFORM PREPEND -# But we can't use it yet -string(REPLACE "include/" "${CMAKE_CURRENT_SOURCE_DIR}/include/" PYBIND11_HEADERS - "${PYBIND11_HEADERS}") +list(TRANSFORM PYBIND11_HEADERS PREPEND "${CMAKE_CURRENT_SOURCE_DIR}/") # Cache variable so this can be used in parent projects set(pybind11_INCLUDE_DIR @@ -220,6 +308,9 @@ elseif(USE_PYTHON_INCLUDE_DIR AND DEFINED PYTHON_INCLUDE_DIR) endif() if(PYBIND11_INSTALL) + if(DEFINED SKBUILD_PROJECT_NAME AND SKBUILD_PROJECT_NAME STREQUAL "pybind11_global") + install(DIRECTORY ${pybind11_INCLUDE_DIR}/pybind11 DESTINATION "${SKBUILD_HEADERS_DIR}") + endif() install(DIRECTORY ${pybind11_INCLUDE_DIR}/pybind11 DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}) set(PYBIND11_CMAKECONFIG_INSTALL_DIR "${CMAKE_INSTALL_DATAROOTDIR}/cmake/${PROJECT_NAME}" @@ -235,25 +326,11 @@ if(PYBIND11_INSTALL) tools/${PROJECT_NAME}Config.cmake.in "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}Config.cmake" INSTALL_DESTINATION ${PYBIND11_CMAKECONFIG_INSTALL_DIR}) - if(CMAKE_VERSION VERSION_LESS 3.14) - # Remove CMAKE_SIZEOF_VOID_P from ConfigVersion.cmake since the library does - # not depend on architecture specific settings or libraries. - set(_PYBIND11_CMAKE_SIZEOF_VOID_P ${CMAKE_SIZEOF_VOID_P}) - unset(CMAKE_SIZEOF_VOID_P) - - write_basic_package_version_file( - ${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake - VERSION ${PROJECT_VERSION} - COMPATIBILITY AnyNewerVersion) - - set(CMAKE_SIZEOF_VOID_P ${_PYBIND11_CMAKE_SIZEOF_VOID_P}) - else() - # CMake 3.14+ natively supports header-only libraries - write_basic_package_version_file( - ${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake - VERSION ${PROJECT_VERSION} - COMPATIBILITY AnyNewerVersion ARCH_INDEPENDENT) - endif() + # CMake natively supports header-only libraries + write_basic_package_version_file( + ${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake + VERSION ${PROJECT_VERSION} + COMPATIBILITY AnyNewerVersion ARCH_INDEPENDENT) install( FILES ${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}Config.cmake @@ -262,6 +339,7 @@ if(PYBIND11_INSTALL) tools/pybind11Common.cmake tools/pybind11Tools.cmake tools/pybind11NewTools.cmake + tools/pybind11GuessPythonExtSuffix.cmake DESTINATION ${PYBIND11_CMAKECONFIG_INSTALL_DIR}) if(NOT PYBIND11_EXPORT_NAME) @@ -277,7 +355,21 @@ if(PYBIND11_INSTALL) # pkg-config support if(NOT prefix_for_pc_file) - set(prefix_for_pc_file "${CMAKE_INSTALL_PREFIX}") + if(IS_ABSOLUTE "${CMAKE_INSTALL_DATAROOTDIR}") + set(prefix_for_pc_file "${CMAKE_INSTALL_PREFIX}") + else() + set(pc_datarootdir "${CMAKE_INSTALL_DATAROOTDIR}") + if(CMAKE_VERSION VERSION_LESS 3.20) + set(prefix_for_pc_file "\${pcfiledir}/..") + while(pc_datarootdir) + get_filename_component(pc_datarootdir "${pc_datarootdir}" DIRECTORY) + string(APPEND prefix_for_pc_file "/..") + endwhile() + else() + cmake_path(RELATIVE_PATH CMAKE_INSTALL_PREFIX BASE_DIRECTORY CMAKE_INSTALL_DATAROOTDIR + OUTPUT_VARIABLE prefix_for_pc_file) + endif() + endif() endif() join_paths(includedir_for_pc_file "\${prefix}" "${CMAKE_INSTALL_INCLUDEDIR}") configure_file("${CMAKE_CURRENT_SOURCE_DIR}/tools/pybind11.pc.in" @@ -285,6 +377,17 @@ if(PYBIND11_INSTALL) install(FILES "${CMAKE_CURRENT_BINARY_DIR}/pybind11.pc" DESTINATION "${CMAKE_INSTALL_DATAROOTDIR}/pkgconfig/") + # When building a wheel, include __init__.py's for modules + # (see https://github.com/pybind/pybind11/pull/5552) + if(DEFINED SKBUILD_PROJECT_NAME AND SKBUILD_PROJECT_NAME STREQUAL "pybind11") + file(MAKE_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/empty") + file(TOUCH "${CMAKE_CURRENT_BINARY_DIR}/empty/__init__.py") + install(FILES "${CMAKE_CURRENT_BINARY_DIR}/empty/__init__.py" + DESTINATION "${CMAKE_INSTALL_DATAROOTDIR}/") + install(FILES "${CMAKE_CURRENT_BINARY_DIR}/empty/__init__.py" + DESTINATION "${CMAKE_INSTALL_DATAROOTDIR}/pkgconfig/") + endif() + # Uninstall target if(PYBIND11_MASTER_PROJECT) configure_file("${CMAKE_CURRENT_SOURCE_DIR}/tools/cmake_uninstall.cmake.in" diff --git a/external_libraries/pybind11/CMakePresets.json b/external_libraries/pybind11/CMakePresets.json new file mode 100644 index 00000000..42bf3ade --- /dev/null +++ b/external_libraries/pybind11/CMakePresets.json @@ -0,0 +1,93 @@ +{ + "version": 6, + "configurePresets": [ + { + "name": "default", + "displayName": "Default", + "binaryDir": "build", + "generator": "Ninja", + "errors": { + "dev": true, + "deprecated": true + }, + "cacheVariables": { + "CMAKE_BUILD_TYPE": "Debug", + "CMAKE_EXPORT_COMPILE_COMMANDS": true, + "DOWNLOAD_CATCH": true, + "DOWNLOAD_EIGEN": true, + "PYBIND11_FINDPYTHON": "NEW", + "PYBIND11_WERROR": true, + "CMAKE_COLOR_DIAGNOSTICS": true + } + }, + { + "name": "venv", + "displayName": "Venv", + "inherits": "default", + "cacheVariables": { + "PYBIND11_CREATE_WITH_UV": "python3", + "Python_ROOT_DIR": ".venv" + } + }, + { + "name": "tidy", + "displayName": "Clang-tidy", + "inherits": "default", + "binaryDir": "build-tidy", + "cacheVariables": { + "CMAKE_CXX_CLANG_TIDY": "clang-tidy;--use-color;--warnings-as-errors=*", + "CMAKE_CXX_STANDARD": "17" + } + } + ], + "buildPresets": [ + { + "name": "default", + "displayName": "Default Build", + "configurePreset": "default" + }, + { + "name": "venv", + "displayName": "Venv Build", + "configurePreset": "venv" + }, + { + "name": "tidy", + "displayName": "Clang-tidy Build", + "configurePreset": "tidy", + "nativeToolOptions": ["-k0"] + }, + { + "name": "tests", + "displayName": "Tests (for workflow)", + "configurePreset": "default", + "targets": ["pytest", "cpptest", "test_cmake_build", "test_cross_module_rtti"] + }, + { + "name": "testsvenv", + "displayName": "Tests Venv (for workflow)", + "configurePreset": "venv", + "targets": ["pytest", "cpptest", "test_cmake_build", "test_cross_module_rtti"] + } + ], + "workflowPresets": [ + { + "name": "default", + "displayName": "Default Workflow", + "steps": [ + { "type": "configure", "name": "default" }, + { "type": "build", "name": "default" }, + { "type": "build", "name": "tests" } + ] + }, + { + "name": "venv", + "displayName": "Default Workflow", + "steps": [ + { "type": "configure", "name": "venv" }, + { "type": "build", "name": "venv" }, + { "type": "build", "name": "testsvenv" } + ] + } + ] +} diff --git a/external_libraries/pybind11/MANIFEST.in b/external_libraries/pybind11/MANIFEST.in deleted file mode 100644 index 7ce83c55..00000000 --- a/external_libraries/pybind11/MANIFEST.in +++ /dev/null @@ -1,6 +0,0 @@ -prune tests -recursive-include pybind11/include/pybind11 *.h -recursive-include pybind11 *.py -recursive-include pybind11 py.typed -include pybind11/share/cmake/pybind11/*.cmake -include LICENSE README.rst SECURITY.md pyproject.toml setup.py setup.cfg diff --git a/external_libraries/pybind11/README.rst b/external_libraries/pybind11/README.rst index 80213a40..26d618f9 100644 --- a/external_libraries/pybind11/README.rst +++ b/external_libraries/pybind11/README.rst @@ -1,9 +1,11 @@ .. figure:: https://github.com/pybind/pybind11/raw/master/docs/pybind11-logo.png :alt: pybind11 logo -**pybind11 — Seamless operability between C++11 and Python** +**pybind11 (v3) — Seamless interoperability between C++ and Python** -|Latest Documentation Status| |Stable Documentation Status| |Gitter chat| |GitHub Discussions| |CI| |Build status| +|Latest Documentation Status| |Stable Documentation Status| |Gitter chat| |GitHub Discussions| + +|CI| |Build status| |SPEC 4 — Using and Creating Nightly Wheels| |Repology| |PyPI package| |Conda-forge| |Python Versions| @@ -34,12 +36,12 @@ dependency. Think of this library as a tiny self-contained version of Boost.Python with everything stripped away that isn't relevant for binding generation. Without comments, the core header files only require ~4K -lines of code and depend on Python (3.6+, or PyPy) and the C++ -standard library. This compact implementation was possible thanks to -some of the new C++11 language features (specifically: tuples, lambda -functions and variadic templates). Since its creation, this library has -grown beyond Boost.Python in many ways, leading to dramatically simpler -binding code in many common situations. +lines of code and depend on Python (CPython 3.8+, PyPy, or GraalPy) and the C++ +standard library. This compact implementation was possible thanks to some C++11 +language features (specifically: tuples, lambda functions and variadic +templates). Since its creation, this library has grown beyond Boost.Python in +many ways, leading to dramatically simpler binding code in many common +situations. Tutorial and reference documentation is provided at `pybind11.readthedocs.io `_. @@ -71,6 +73,7 @@ pybind11 can map the following core C++ features to Python: - Internal references with correct reference counting - C++ classes with virtual (and pure virtual) methods can be extended in Python +- Integrated NumPy support (NumPy 2 requires pybind11 2.12+) Goodies ------- @@ -78,8 +81,9 @@ Goodies In addition to the core functionality, pybind11 provides some extra goodies: -- Python 3.6+, and PyPy3 7.3 are supported with an implementation-agnostic - interface (pybind11 2.9 was the last version to support Python 2 and 3.5). +- CPython 3.8+, PyPy3 7.3.17+, and GraalPy 24.1+ are supported with an + implementation-agnostic interface (see older versions for older CPython + and PyPy versions). - It is possible to bind C++11 lambda functions with captured variables. The lambda capture data is stored inside the resulting @@ -116,28 +120,57 @@ goodies: - With little extra effort, C++ types can be pickled and unpickled similar to regular Python objects. -Supported compilers -------------------- +Supported platforms & compilers +------------------------------- + +pybind11 is exercised in continuous integration across a range of operating +systems, Python versions, C++ standards, and toolchains. For an up-to-date +view of the combinations we currently test, please see the +`pybind11 GitHub Actions `_ +logs. -1. Clang/LLVM 3.3 or newer (for Apple Xcode's clang, this is 5.0.0 or - newer) -2. GCC 4.8 or newer -3. Microsoft Visual Studio 2017 or newer -4. Intel classic C++ compiler 18 or newer (ICC 20.2 tested in CI) -5. Cygwin/GCC (previously tested on 2.5.1) -6. NVCC (CUDA 11.0 tested in CI) -7. NVIDIA PGI (20.9 tested in CI) +The test matrix naturally evolves over time as older platforms and compilers +fall out of use and new ones are added by the community. Closely related +versions of a tested compiler or platform will often work as well in practice, +but we cannot promise to validate every possible combination. If a +configuration you rely on is missing from the matrix or regresses, issues and +pull requests to extend coverage are very welcome. At the same time, we need +to balance the size of the test matrix with the available CI resources, +such as GitHub's limits on concurrent jobs under the free tier. About ----- This project was created by `Wenzel Jakob `_. Significant features and/or -improvements to the code were contributed by Jonas Adler, Lori A. Burns, -Sylvain Corlay, Eric Cousineau, Aaron Gokaslan, Ralf Grosse-Kunstleve, Trent Houliston, Axel -Huebl, @hulucc, Yannick Jadoul, Sergey Lyskov, Johan Mabille, Tomasz Miąsko, -Dean Moldovan, Ben Pritchard, Jason Rhinelander, Boris Schäling, Pim -Schellart, Henry Schreiner, Ivan Smirnov, Boris Staletic, and Patrick Stewart. +improvements to the code were contributed by +Jonas Adler, +Lori A. Burns, +Sylvain Corlay, +Eric Cousineau, +Aaron Gokaslan, +Ralf Grosse-Kunstleve, +Trent Houliston, +Axel Huebl, +@hulucc, +Yannick Jadoul, +Sergey Lyskov, +Johan Mabille, +Tomasz Miąsko, +Dean Moldovan, +Ben Pritchard, +Jason Rhinelander, +Boris Schäling, +Pim Schellart, +Henry Schreiner, +Ivan Smirnov, +Dustin Spicuzza, +Boris Staletic, +Ethan Steinberg, +Patrick Stewart, +Ivor Wanders, +and +Xiaofei Wang. We thank Google for a generous financial contribution to the continuous integration infrastructure used by this project. @@ -178,3 +211,5 @@ to the terms and conditions of this license. :target: https://pypi.org/project/pybind11/ .. |GitHub Discussions| image:: https://img.shields.io/static/v1?label=Discussions&message=Ask&color=blue&logo=github :target: https://github.com/pybind/pybind11/discussions +.. |SPEC 4 — Using and Creating Nightly Wheels| image:: https://img.shields.io/badge/SPEC-4-green?labelColor=%23004811&color=%235CA038 + :target: https://scientific-python.org/specs/spec-0004/ diff --git a/external_libraries/pybind11/docs/Doxyfile b/external_libraries/pybind11/docs/Doxyfile new file mode 100644 index 00000000..09138db3 --- /dev/null +++ b/external_libraries/pybind11/docs/Doxyfile @@ -0,0 +1,21 @@ +PROJECT_NAME = pybind11 +INPUT = ../include/pybind11/ +RECURSIVE = YES + +GENERATE_HTML = NO +GENERATE_LATEX = NO +GENERATE_XML = YES +XML_OUTPUT = .build/doxygenxml +XML_PROGRAMLISTING = YES + +MACRO_EXPANSION = YES +EXPAND_ONLY_PREDEF = YES +EXPAND_AS_DEFINED = PYBIND11_RUNTIME_EXCEPTION + +ALIASES = "rst=\verbatim embed:rst" +ALIASES += "endrst=\endverbatim" + +QUIET = YES +WARNINGS = YES +WARN_IF_UNDOCUMENTED = NO +PREDEFINED = PYBIND11_NOINLINE diff --git a/external_libraries/pybind11/docs/_static/css/custom.css b/external_libraries/pybind11/docs/_static/css/custom.css new file mode 100644 index 00000000..7a49a6ac --- /dev/null +++ b/external_libraries/pybind11/docs/_static/css/custom.css @@ -0,0 +1,3 @@ +.highlight .go { + color: #707070; +} diff --git a/external_libraries/pybind11/docs/advanced/cast/chrono.rst b/external_libraries/pybind11/docs/advanced/cast/chrono.rst new file mode 100644 index 00000000..fbd46057 --- /dev/null +++ b/external_libraries/pybind11/docs/advanced/cast/chrono.rst @@ -0,0 +1,81 @@ +Chrono +====== + +When including the additional header file :file:`pybind11/chrono.h` conversions +from C++11 chrono datatypes to python datetime objects are automatically enabled. +This header also enables conversions of python floats (often from sources such +as ``time.monotonic()``, ``time.perf_counter()`` and ``time.process_time()``) +into durations. + +An overview of clocks in C++11 +------------------------------ + +A point of confusion when using these conversions is the differences between +clocks provided in C++11. There are three clock types defined by the C++11 +standard and users can define their own if needed. Each of these clocks have +different properties and when converting to and from python will give different +results. + +The first clock defined by the standard is ``std::chrono::system_clock``. This +clock measures the current date and time. However, this clock changes with to +updates to the operating system time. For example, if your time is synchronised +with a time server this clock will change. This makes this clock a poor choice +for timing purposes but good for measuring the wall time. + +The second clock defined in the standard is ``std::chrono::steady_clock``. +This clock ticks at a steady rate and is never adjusted. This makes it excellent +for timing purposes, however the value in this clock does not correspond to the +current date and time. Often this clock will be the amount of time your system +has been on, although it does not have to be. This clock will never be the same +clock as the system clock as the system clock can change but steady clocks +cannot. + +The third clock defined in the standard is ``std::chrono::high_resolution_clock``. +This clock is the clock that has the highest resolution out of the clocks in the +system. It is normally a typedef to either the system clock or the steady clock +but can be its own independent clock. This is important as when using these +conversions as the types you get in python for this clock might be different +depending on the system. +If it is a typedef of the system clock, python will get datetime objects, but if +it is a different clock they will be timedelta objects. + +Provided conversions +-------------------- + +.. rubric:: C++ to Python + +- ``std::chrono::system_clock::time_point`` → ``datetime.datetime`` + System clock times are converted to python datetime instances. They are + in the local timezone, but do not have any timezone information attached + to them (they are naive datetime objects). + +- ``std::chrono::duration`` → ``datetime.timedelta`` + Durations are converted to timedeltas, any precision in the duration + greater than microseconds is lost by rounding towards zero. + +- ``std::chrono::[other_clocks]::time_point`` → ``datetime.timedelta`` + Any clock time that is not the system clock is converted to a time delta. + This timedelta measures the time from the clocks epoch to now. + +.. rubric:: Python to C++ + +- ``datetime.datetime`` or ``datetime.date`` or ``datetime.time`` → ``std::chrono::system_clock::time_point`` + Date/time objects are converted into system clock timepoints. Any + timezone information is ignored and the type is treated as a naive + object. + +- ``datetime.timedelta`` → ``std::chrono::duration`` + Time delta are converted into durations with microsecond precision. + +- ``datetime.timedelta`` → ``std::chrono::[other_clocks]::time_point`` + Time deltas that are converted into clock timepoints are treated as + the amount of time from the start of the clocks epoch. + +- ``float`` → ``std::chrono::duration`` + Floats that are passed to C++ as durations be interpreted as a number of + seconds. These will be converted to the duration using ``duration_cast`` + from the float. + +- ``float`` → ``std::chrono::[other_clocks]::time_point`` + Floats that are passed to C++ as time points will be interpreted as the + number of seconds from the start of the clocks epoch. diff --git a/external_libraries/pybind11/docs/advanced/cast/custom.rst b/external_libraries/pybind11/docs/advanced/cast/custom.rst new file mode 100644 index 00000000..786192be --- /dev/null +++ b/external_libraries/pybind11/docs/advanced/cast/custom.rst @@ -0,0 +1,137 @@ +.. _custom_type_caster: + +Custom type casters +=================== + +Some applications may prefer custom type casters that convert between existing +Python types and C++ types, similar to the ``list`` ↔ ``std::vector`` +and ``dict`` ↔ ``std::map`` conversions which are built into pybind11. +Implementing custom type casters is fairly advanced usage. +While it is recommended to use the pybind11 API as much as possible, more complex examples may +require familiarity with the intricacies of the Python C API. +You can refer to the `Python/C API Reference Manual `_ +for more information. + +The following snippets demonstrate how this works for a very simple ``Point2D`` type. +We want this type to be convertible to C++ from Python types implementing the +``Sequence`` protocol and having two elements of type ``float``. +When returned from C++ to Python, it should be converted to a Python ``tuple[float, float]``. +For this type we could provide Python bindings for different arithmetic functions implemented +in C++ (here demonstrated by a simple ``negate`` function). + +.. + PLEASE KEEP THE CODE BLOCKS IN SYNC WITH + tests/test_docs_advanced_cast_custom.cpp + tests/test_docs_advanced_cast_custom.py + Ideally, change the test, run pre-commit (incl. clang-format), + then copy the changed code back here. + Also use TEST_SUBMODULE in tests, but PYBIND11_MODULE in docs. + +.. code-block:: cpp + + namespace user_space { + + struct Point2D { + double x; + double y; + }; + + Point2D negate(const Point2D &point) { return Point2D{-point.x, -point.y}; } + + } // namespace user_space + + +The following Python snippet demonstrates the intended usage of ``negate`` from the Python side: + +.. code-block:: python + + from my_math_module import docs_advanced_cast_custom as m + + point1 = [1.0, -1.0] + point2 = m.negate(point1) + assert point2 == (-1.0, 1.0) + +To register the necessary conversion routines, it is necessary to add an +instantiation of the ``pybind11::detail::type_caster`` template. +Although this is an implementation detail, adding an instantiation of this +type is explicitly allowed. + +.. code-block:: cpp + + namespace pybind11 { + namespace detail { + + template <> + struct type_caster { + // This macro inserts a lot of boilerplate code and sets the type hint. + // `io_name` is used to specify different type hints for arguments and return values. + // The signature of our negate function would then look like: + // `negate(Sequence[float]) -> tuple[float, float]` + PYBIND11_TYPE_CASTER(user_space::Point2D, io_name("Sequence[float]", "tuple[float, float]")); + + // C++ -> Python: convert `Point2D` to `tuple[float, float]`. The second and third arguments + // are used to indicate the return value policy and parent object (for + // return_value_policy::reference_internal) and are often ignored by custom casters. + // The return value should reflect the type hint specified by the second argument of `io_name`. + static handle + cast(const user_space::Point2D &number, return_value_policy /*policy*/, handle /*parent*/) { + return py::make_tuple(number.x, number.y).release(); + } + + // Python -> C++: convert a `PyObject` into a `Point2D` and return false upon failure. The + // second argument indicates whether implicit conversions should be allowed. + // The accepted types should reflect the type hint specified by the first argument of + // `io_name`. + bool load(handle src, bool /*convert*/) { + // Check if handle is a Sequence + if (!py::isinstance(src)) { + return false; + } + auto seq = py::reinterpret_borrow(src); + // Check if exactly two values are in the Sequence + if (seq.size() != 2) { + return false; + } + // Check if each element is either a float or an int + for (auto item : seq) { + if (!py::isinstance(item) && !py::isinstance(item)) { + return false; + } + } + value.x = seq[0].cast(); + value.y = seq[1].cast(); + return true; + } + }; + + } // namespace detail + } // namespace pybind11 + + // Bind the negate function + PYBIND11_MODULE(docs_advanced_cast_custom, m, py::mod_gil_not_used()) { m.def("negate", user_space::negate); } + +.. note:: + + A ``type_caster`` defined with ``PYBIND11_TYPE_CASTER(T, ...)`` requires + that ``T`` is default-constructible (``value`` is first default constructed + and then ``load()`` assigns to it). + +.. note:: + For further information on the ``return_value_policy`` argument of ``cast`` refer to :ref:`return_value_policies`. + To learn about the ``convert`` argument of ``load`` see :ref:`nonconverting_arguments`. + +.. warning:: + + When using custom type casters, it's important to declare them consistently + in every compilation unit of the Python extension module to satisfy the C++ One Definition Rule + (`ODR `_). Otherwise, + undefined behavior can ensue. + +.. note:: + + Using the type hint ``Sequence[float]`` signals to static type checkers, that not only tuples may be + passed, but any type implementing the Sequence protocol, e.g., ``list[float]``. + Unfortunately, that loses the length information ``tuple[float, float]`` provides. + One way of still providing some length information in type hints is using ``typing.Annotated``, e.g., + ``Annotated[Sequence[float], 2]``, or further add libraries like + `annotated-types `_. diff --git a/external_libraries/pybind11/docs/advanced/cast/eigen.rst b/external_libraries/pybind11/docs/advanced/cast/eigen.rst new file mode 100644 index 00000000..894ce97f --- /dev/null +++ b/external_libraries/pybind11/docs/advanced/cast/eigen.rst @@ -0,0 +1,310 @@ +Eigen +##### + +`Eigen `_ is C++ header-based library for dense and +sparse linear algebra. Due to its popularity and widespread adoption, pybind11 +provides transparent conversion and limited mapping support between Eigen and +Scientific Python linear algebra data types. + +To enable the built-in Eigen support you must include the optional header file +:file:`pybind11/eigen.h`. + +Pass-by-value +============= + +When binding a function with ordinary Eigen dense object arguments (for +example, ``Eigen::MatrixXd``), pybind11 will accept any input value that is +already (or convertible to) a ``numpy.ndarray`` with dimensions compatible with +the Eigen type, copy its values into a temporary Eigen variable of the +appropriate type, then call the function with this temporary variable. + +Sparse matrices are similarly copied to or from +``scipy.sparse.csr_matrix``/``scipy.sparse.csc_matrix`` objects. + +Pass-by-reference +================= + +One major limitation of the above is that every data conversion implicitly +involves a copy, which can be both expensive (for large matrices) and disallows +binding functions that change their (Matrix) arguments. Pybind11 allows you to +work around this by using Eigen's ``Eigen::Ref`` class much as you +would when writing a function taking a generic type in Eigen itself (subject to +some limitations discussed below). + +When calling a bound function accepting a ``Eigen::Ref`` +type, pybind11 will attempt to avoid copying by using an ``Eigen::Map`` object +that maps into the source ``numpy.ndarray`` data: this requires both that the +data types are the same (e.g. ``dtype='float64'`` and ``MatrixType::Scalar`` is +``double``); and that the storage is layout compatible. The latter limitation +is discussed in detail in the section below, and requires careful +consideration: by default, numpy matrices and Eigen matrices are *not* storage +compatible. + +If the numpy matrix cannot be used as is (either because its types differ, e.g. +passing an array of integers to an Eigen parameter requiring doubles, or +because the storage is incompatible), pybind11 makes a temporary copy and +passes the copy instead. + +When a bound function parameter is instead ``Eigen::Ref`` (note the +lack of ``const``), pybind11 will only allow the function to be called if it +can be mapped *and* if the numpy array is writeable (that is +``a.flags.writeable`` is true). Any access (including modification) made to +the passed variable will be transparently carried out directly on the +``numpy.ndarray``. + +This means you can write code such as the following and have it work as +expected: + +.. code-block:: cpp + + void scale_by_2(Eigen::Ref v) { + v *= 2; + } + +Note, however, that you will likely run into limitations due to numpy and +Eigen's difference default storage order for data; see the below section on +:ref:`storage_orders` for details on how to bind code that won't run into such +limitations. + +.. note:: + + Passing by reference is not supported for sparse types. + +Returning values to Python +========================== + +When returning an ordinary dense Eigen matrix type to numpy (e.g. +``Eigen::MatrixXd`` or ``Eigen::RowVectorXf``) pybind11 keeps the matrix and +returns a numpy array that directly references the Eigen matrix: no copy of the +data is performed. The numpy array will have ``array.flags.owndata`` set to +``False`` to indicate that it does not own the data, and the lifetime of the +stored Eigen matrix will be tied to the returned ``array``. + +If you bind a function with a non-reference, ``const`` return type (e.g. +``const Eigen::MatrixXd``), the same thing happens except that pybind11 also +sets the numpy array's ``writeable`` flag to false. + +If you return an lvalue reference or pointer, the usual pybind11 rules apply, +as dictated by the binding function's return value policy (see the +documentation on :ref:`return_value_policies` for full details). That means, +without an explicit return value policy, lvalue references will be copied and +pointers will be managed by pybind11. In order to avoid copying, you should +explicitly specify an appropriate return value policy, as in the following +example: + +.. code-block:: cpp + + class MyClass { + Eigen::MatrixXd big_mat = Eigen::MatrixXd::Zero(10000, 10000); + public: + Eigen::MatrixXd &getMatrix() { return big_mat; } + const Eigen::MatrixXd &viewMatrix() { return big_mat; } + }; + + // Later, in binding code: + py::class_(m, "MyClass") + .def(py::init<>()) + .def("copy_matrix", &MyClass::getMatrix) // Makes a copy! + .def("get_matrix", &MyClass::getMatrix, py::return_value_policy::reference_internal) + .def("view_matrix", &MyClass::viewMatrix, py::return_value_policy::reference_internal) + ; + +.. code-block:: python + + a = MyClass() + m = a.get_matrix() # flags.writeable = True, flags.owndata = False + v = a.view_matrix() # flags.writeable = False, flags.owndata = False + c = a.copy_matrix() # flags.writeable = True, flags.owndata = True + # m[5,6] and v[5,6] refer to the same element, c[5,6] does not. + +Note in this example that ``py::return_value_policy::reference_internal`` is +used to tie the life of the MyClass object to the life of the returned arrays. + +You may also return an ``Eigen::Ref``, ``Eigen::Map`` or other map-like Eigen +object (for example, the return value of ``matrix.block()`` and related +methods) that map into a dense Eigen type. When doing so, the default +behaviour of pybind11 is to simply reference the returned data: you must take +care to ensure that this data remains valid! You may ask pybind11 to +explicitly *copy* such a return value by using the +``py::return_value_policy::copy`` policy when binding the function. You may +also use ``py::return_value_policy::reference_internal`` or a +``py::keep_alive`` to ensure the data stays valid as long as the returned numpy +array does. + +When returning such a reference of map, pybind11 additionally respects the +readonly-status of the returned value, marking the numpy array as non-writeable +if the reference or map was itself read-only. + +.. note:: + + Sparse types are always copied when returned. + +.. _storage_orders: + +Storage orders +============== + +Passing arguments via ``Eigen::Ref`` has some limitations that you must be +aware of in order to effectively pass matrices by reference. First and +foremost is that the default ``Eigen::Ref`` class requires +contiguous storage along columns (for column-major types, the default in Eigen) +or rows if ``MatrixType`` is specifically an ``Eigen::RowMajor`` storage type. +The former, Eigen's default, is incompatible with ``numpy``'s default row-major +storage, and so you will not be able to pass numpy arrays to Eigen by reference +without making one of two changes. + +(Note that this does not apply to vectors (or column or row matrices): for such +types the "row-major" and "column-major" distinction is meaningless). + +The first approach is to change the use of ``Eigen::Ref`` to the +more general ``Eigen::Ref>`` (or similar type with a fully dynamic stride type in the +third template argument). Since this is a rather cumbersome type, pybind11 +provides a ``py::EigenDRef`` type alias for your convenience (along +with EigenDMap for the equivalent Map, and EigenDStride for just the stride +type). + +This type allows Eigen to map into any arbitrary storage order. This is not +the default in Eigen for performance reasons: contiguous storage allows +vectorization that cannot be done when storage is not known to be contiguous at +compile time. The default ``Eigen::Ref`` stride type allows non-contiguous +storage along the outer dimension (that is, the rows of a column-major matrix +or columns of a row-major matrix), but not along the inner dimension. + +This type, however, has the added benefit of also being able to map numpy array +slices. For example, the following (contrived) example uses Eigen with a numpy +slice to multiply by 2 all coefficients that are both on even rows (0, 2, 4, +...) and in columns 2, 5, or 8: + +.. code-block:: cpp + + m.def("scale", [](py::EigenDRef m, double c) { m *= c; }); + +.. code-block:: python + + # a = np.array(...) + scale_by_2(myarray[0::2, 2:9:3]) + +The second approach to avoid copying is more intrusive: rearranging the +underlying data types to not run into the non-contiguous storage problem in the +first place. In particular, that means using matrices with ``Eigen::RowMajor`` +storage, where appropriate, such as: + +.. code-block:: cpp + + using RowMatrixXd = Eigen::Matrix; + // Use RowMatrixXd instead of MatrixXd + +Now bound functions accepting ``Eigen::Ref`` arguments will be +callable with numpy's (default) arrays without involving a copying. + +You can, alternatively, change the storage order that numpy arrays use by +adding the ``order='F'`` option when creating an array: + +.. code-block:: python + + myarray = np.array(source, order="F") + +Such an object will be passable to a bound function accepting an +``Eigen::Ref`` (or similar column-major Eigen type). + +One major caveat with this approach, however, is that it is not entirely as +easy as simply flipping all Eigen or numpy usage from one to the other: some +operations may alter the storage order of a numpy array. For example, ``a2 = +array.transpose()`` results in ``a2`` being a view of ``array`` that references +the same data, but in the opposite storage order! + +While this approach allows fully optimized vectorized calculations in Eigen, it +cannot be used with array slices, unlike the first approach. + +When *returning* a matrix to Python (either a regular matrix, a reference via +``Eigen::Ref<>``, or a map/block into a matrix), no special storage +consideration is required: the created numpy array will have the required +stride that allows numpy to properly interpret the array, whatever its storage +order. + +Failing rather than copying +=========================== + +The default behaviour when binding ``Eigen::Ref`` Eigen +references is to copy matrix values when passed a numpy array that does not +conform to the element type of ``MatrixType`` or does not have a compatible +stride layout. If you want to explicitly avoid copying in such a case, you +should bind arguments using the ``py::arg().noconvert()`` annotation (as +described in the :ref:`nonconverting_arguments` documentation). + +The following example shows an example of arguments that don't allow data +copying to take place: + +.. code-block:: cpp + + // The method and function to be bound: + class MyClass { + // ... + double some_method(const Eigen::Ref &matrix) { /* ... */ } + }; + float some_function(const Eigen::Ref &big, + const Eigen::Ref &small) { + // ... + } + + // The associated binding code: + using namespace pybind11::literals; // for "arg"_a + py::class_(m, "MyClass") + // ... other class definitions + .def("some_method", &MyClass::some_method, py::arg().noconvert()); + + m.def("some_function", &some_function, + "big"_a.noconvert(), // <- Don't allow copying for this arg + "small"_a // <- This one can be copied if needed + ); + +With the above binding code, attempting to call the ``some_method(m)`` +method on a ``MyClass`` object, or attempting to call ``some_function(m, m2)`` +will raise a ``RuntimeError`` rather than making a temporary copy of the array. +It will, however, allow the ``m2`` argument to be copied into a temporary if +necessary. + +Note that explicitly specifying ``.noconvert()`` is not required for *mutable* +Eigen references (e.g. ``Eigen::Ref`` without ``const`` on the +``MatrixXd``): mutable references will never be called with a temporary copy. + +Vectors versus column/row matrices +================================== + +Eigen and numpy have fundamentally different notions of a vector. In Eigen, a +vector is simply a matrix with the number of columns or rows set to 1 at +compile time (for a column vector or row vector, respectively). NumPy, in +contrast, has comparable 2-dimensional 1xN and Nx1 arrays, but *also* has +1-dimensional arrays of size N. + +When passing a 2-dimensional 1xN or Nx1 array to Eigen, the Eigen type must +have matching dimensions: That is, you cannot pass a 2-dimensional Nx1 numpy +array to an Eigen value expecting a row vector, or a 1xN numpy array as a +column vector argument. + +On the other hand, pybind11 allows you to pass 1-dimensional arrays of length N +as Eigen parameters. If the Eigen type can hold a column vector of length N it +will be passed as such a column vector. If not, but the Eigen type constraints +will accept a row vector, it will be passed as a row vector. (The column +vector takes precedence when both are supported, for example, when passing a +1D numpy array to a MatrixXd argument). Note that the type need not be +explicitly a vector: it is permitted to pass a 1D numpy array of size 5 to an +Eigen ``Matrix``: you would end up with a 1x5 Eigen matrix. +Passing the same to an ``Eigen::MatrixXd`` would result in a 5x1 Eigen matrix. + +When returning an Eigen vector to numpy, the conversion is ambiguous: a row +vector of length 4 could be returned as either a 1D array of length 4, or as a +2D array of size 1x4. When encountering such a situation, pybind11 compromises +by considering the returned Eigen type: if it is a compile-time vector--that +is, the type has either the number of rows or columns set to 1 at compile +time--pybind11 converts to a 1D numpy array when returning the value. For +instances that are a vector only at run-time (e.g. ``MatrixXd``, +``Matrix``), pybind11 returns the vector as a 2D array to +numpy. If this isn't want you want, you can use ``array.reshape(...)`` to get +a view of the same data in the desired dimensions. + +.. seealso:: + + The file :file:`tests/test_eigen.cpp` contains a complete example that + shows how to pass Eigen sparse and dense data types in more detail. diff --git a/external_libraries/pybind11/docs/advanced/cast/functional.rst b/external_libraries/pybind11/docs/advanced/cast/functional.rst new file mode 100644 index 00000000..c95aa074 --- /dev/null +++ b/external_libraries/pybind11/docs/advanced/cast/functional.rst @@ -0,0 +1,109 @@ +Functional +########## + +The following features must be enabled by including :file:`pybind11/functional.h`. + + +Callbacks and passing anonymous functions +========================================= + +The C++11 standard brought lambda functions and the generic polymorphic +function wrapper ``std::function<>`` to the C++ programming language, which +enable powerful new ways of working with functions. Lambda functions come in +two flavors: stateless lambda function resemble classic function pointers that +link to an anonymous piece of code, while stateful lambda functions +additionally depend on captured variables that are stored in an anonymous +*lambda closure object*. + +Here is a simple example of a C++ function that takes an arbitrary function +(stateful or stateless) with signature ``int -> int`` as an argument and runs +it with the value 10. + +.. code-block:: cpp + + int func_arg(const std::function &f) { + return f(10); + } + +The example below is more involved: it takes a function of signature ``int -> int`` +and returns another function of the same kind. The return value is a stateful +lambda function, which stores the value ``f`` in the capture object and adds 1 to +its return value upon execution. + +.. code-block:: cpp + + std::function func_ret(const std::function &f) { + return [f](int i) { + return f(i) + 1; + }; + } + +This example demonstrates using python named parameters in C++ callbacks which +requires using ``py::cpp_function`` as a wrapper. Usage is similar to defining +methods of classes: + +.. code-block:: cpp + + py::cpp_function func_cpp() { + return py::cpp_function([](int i) { return i+1; }, + py::arg("number")); + } + +After including the extra header file :file:`pybind11/functional.h`, it is almost +trivial to generate binding code for all of these functions. + +.. code-block:: cpp + + #include + + PYBIND11_MODULE(example, m, py::mod_gil_not_used()) { + m.def("func_arg", &func_arg); + m.def("func_ret", &func_ret); + m.def("func_cpp", &func_cpp); + } + +The following interactive session shows how to call them from Python. + +.. code-block:: pycon + + $ python + >>> import example + >>> def square(i): + ... return i * i + ... + >>> example.func_arg(square) + 100L + >>> square_plus_1 = example.func_ret(square) + >>> square_plus_1(4) + 17L + >>> plus_1 = func_cpp() + >>> plus_1(number=43) + 44L + +.. warning:: + + Keep in mind that passing a function from C++ to Python (or vice versa) + will instantiate a piece of wrapper code that translates function + invocations between the two languages. Naturally, this translation + increases the computational cost of each function call somewhat. A + problematic situation can arise when a function is copied back and forth + between Python and C++ many times in a row, in which case the underlying + wrappers will accumulate correspondingly. The resulting long sequence of + C++ -> Python -> C++ -> ... roundtrips can significantly decrease + performance. + + There is one exception: pybind11 detects case where a stateless function + (i.e. a function pointer or a lambda function without captured variables) + is passed as an argument to another C++ function exposed in Python. In this + case, there is no overhead. Pybind11 will extract the underlying C++ + function pointer from the wrapped function to sidestep a potential C++ -> + Python -> C++ roundtrip. This is demonstrated in :file:`tests/test_callbacks.cpp`. + +.. note:: + + This functionality is very useful when generating bindings for callbacks in + C++ libraries (e.g. GUI libraries, asynchronous networking libraries, etc.). + + The file :file:`tests/test_callbacks.cpp` contains a complete example + that demonstrates how to work with callbacks and anonymous functions in + more detail. diff --git a/external_libraries/pybind11/docs/advanced/cast/index.rst b/external_libraries/pybind11/docs/advanced/cast/index.rst new file mode 100644 index 00000000..3ce9ea02 --- /dev/null +++ b/external_libraries/pybind11/docs/advanced/cast/index.rst @@ -0,0 +1,43 @@ +.. _type-conversions: + +Type conversions +################ + +Apart from enabling cross-language function calls, a fundamental problem +that a binding tool like pybind11 must address is to provide access to +native Python types in C++ and vice versa. There are three fundamentally +different ways to do this—which approach is preferable for a particular type +depends on the situation at hand. + +1. Use a native C++ type everywhere. In this case, the type must be wrapped + using pybind11-generated bindings so that Python can interact with it. + +2. Use a native Python type everywhere. It will need to be wrapped so that + C++ functions can interact with it. + +3. Use a native C++ type on the C++ side and a native Python type on the + Python side. pybind11 refers to this as a *type conversion*. + + Type conversions are the most "natural" option in the sense that native + (non-wrapped) types are used everywhere. The main downside is that a copy + of the data must be made on every Python ↔ C++ transition: this is + needed since the C++ and Python versions of the same type generally won't + have the same memory layout. + + pybind11 can perform many kinds of conversions automatically. An overview + is provided in the table ":ref:`conversion_table`". + +The following subsections discuss the differences between these options in more +detail. The main focus in this section is on type conversions, which represent +the last case of the above list. + +.. toctree:: + :maxdepth: 1 + + overview + strings + stl + functional + chrono + eigen + custom diff --git a/external_libraries/pybind11/docs/advanced/cast/overview.rst b/external_libraries/pybind11/docs/advanced/cast/overview.rst new file mode 100644 index 00000000..d5a34ef9 --- /dev/null +++ b/external_libraries/pybind11/docs/advanced/cast/overview.rst @@ -0,0 +1,170 @@ +Overview +######## + +.. rubric:: 1. Native type in C++, wrapper in Python + +Exposing a custom C++ type using :class:`py::class_` was covered in detail +in the :doc:`/classes` section. There, the underlying data structure is +always the original C++ class while the :class:`py::class_` wrapper provides +a Python interface. Internally, when an object like this is sent from C++ to +Python, pybind11 will just add the outer wrapper layer over the native C++ +object. Getting it back from Python is just a matter of peeling off the +wrapper. + +.. rubric:: 2. Wrapper in C++, native type in Python + +This is the exact opposite situation. Now, we have a type which is native to +Python, like a ``tuple`` or a ``list``. One way to get this data into C++ is +with the :class:`py::object` family of wrappers. These are explained in more +detail in the :doc:`/advanced/pycpp/object` section. We'll just give a quick +example here: + +.. code-block:: cpp + + void print_list(py::list my_list) { + for (auto item : my_list) + std::cout << item << " "; + } + +.. code-block:: pycon + + >>> print_list([1, 2, 3]) + 1 2 3 + +The Python ``list`` is not converted in any way -- it's just wrapped in a C++ +:class:`py::list` class. At its core it's still a Python object. Copying a +:class:`py::list` will do the usual reference-counting like in Python. +Returning the object to Python will just remove the thin wrapper. + +.. rubric:: 3. Converting between native C++ and Python types + +In the previous two cases we had a native type in one language and a wrapper in +the other. Now, we have native types on both sides and we convert between them. + +.. code-block:: cpp + + void print_vector(const std::vector &v) { + for (auto item : v) + std::cout << item << "\n"; + } + +.. code-block:: pycon + + >>> print_vector([1, 2, 3]) + 1 2 3 + +In this case, pybind11 will construct a new ``std::vector`` and copy each +element from the Python ``list``. The newly constructed object will be passed +to ``print_vector``. The same thing happens in the other direction: a new +``list`` is made to match the value returned from C++. + +Lots of these conversions are supported out of the box, as shown in the table +below. They are very convenient, but keep in mind that these conversions are +fundamentally based on copying data. This is perfectly fine for small immutable +types but it may become quite expensive for large data structures. This can be +avoided by overriding the automatic conversion with a custom wrapper (i.e. the +above-mentioned approach 1). This requires some manual effort and more details +are available in the :ref:`opaque` section. + +.. _conversion_table: + +List of all builtin conversions +------------------------------- + +The following basic data types are supported out of the box (some may require +an additional extension header to be included). To pass other data structures +as arguments and return values, refer to the section on binding :ref:`classes`. + ++------------------------------------+---------------------------+-----------------------------------+ +| Data type | Description | Header file | ++====================================+===========================+===================================+ +| ``int8_t``, ``uint8_t`` | 8-bit integers | :file:`pybind11/pybind11.h` | ++------------------------------------+---------------------------+-----------------------------------+ +| ``int16_t``, ``uint16_t`` | 16-bit integers | :file:`pybind11/pybind11.h` | ++------------------------------------+---------------------------+-----------------------------------+ +| ``int32_t``, ``uint32_t`` | 32-bit integers | :file:`pybind11/pybind11.h` | ++------------------------------------+---------------------------+-----------------------------------+ +| ``int64_t``, ``uint64_t`` | 64-bit integers | :file:`pybind11/pybind11.h` | ++------------------------------------+---------------------------+-----------------------------------+ +| ``ssize_t``, ``size_t`` | Platform-dependent size | :file:`pybind11/pybind11.h` | ++------------------------------------+---------------------------+-----------------------------------+ +| ``float``, ``double`` | Floating point types | :file:`pybind11/pybind11.h` | ++------------------------------------+---------------------------+-----------------------------------+ +| ``bool`` | Two-state Boolean type | :file:`pybind11/pybind11.h` | ++------------------------------------+---------------------------+-----------------------------------+ +| ``char`` | Character literal | :file:`pybind11/pybind11.h` | ++------------------------------------+---------------------------+-----------------------------------+ +| ``char16_t`` | UTF-16 character literal | :file:`pybind11/pybind11.h` | ++------------------------------------+---------------------------+-----------------------------------+ +| ``char32_t`` | UTF-32 character literal | :file:`pybind11/pybind11.h` | ++------------------------------------+---------------------------+-----------------------------------+ +| ``wchar_t`` | Wide character literal | :file:`pybind11/pybind11.h` | ++------------------------------------+---------------------------+-----------------------------------+ +| ``const char *`` | UTF-8 string literal | :file:`pybind11/pybind11.h` | ++------------------------------------+---------------------------+-----------------------------------+ +| ``const char16_t *`` | UTF-16 string literal | :file:`pybind11/pybind11.h` | ++------------------------------------+---------------------------+-----------------------------------+ +| ``const char32_t *`` | UTF-32 string literal | :file:`pybind11/pybind11.h` | ++------------------------------------+---------------------------+-----------------------------------+ +| ``const wchar_t *`` | Wide string literal | :file:`pybind11/pybind11.h` | ++------------------------------------+---------------------------+-----------------------------------+ +| ``std::string`` | STL dynamic UTF-8 string | :file:`pybind11/pybind11.h` | ++------------------------------------+---------------------------+-----------------------------------+ +| ``std::u16string`` | STL dynamic UTF-16 string | :file:`pybind11/pybind11.h` | ++------------------------------------+---------------------------+-----------------------------------+ +| ``std::u32string`` | STL dynamic UTF-32 string | :file:`pybind11/pybind11.h` | ++------------------------------------+---------------------------+-----------------------------------+ +| ``std::wstring`` | STL dynamic wide string | :file:`pybind11/pybind11.h` | ++------------------------------------+---------------------------+-----------------------------------+ +| ``std::string_view``, | STL C++17 string views | :file:`pybind11/pybind11.h` | +| ``std::u16string_view``, etc. | | | ++------------------------------------+---------------------------+-----------------------------------+ +| ``std::pair`` | Pair of two custom types | :file:`pybind11/pybind11.h` | ++------------------------------------+---------------------------+-----------------------------------+ +| ``std::tuple<...>`` | Arbitrary tuple of types | :file:`pybind11/pybind11.h` | ++------------------------------------+---------------------------+-----------------------------------+ +| ``std::reference_wrapper<...>`` | Reference type wrapper | :file:`pybind11/pybind11.h` | ++------------------------------------+---------------------------+-----------------------------------+ +| ``std::complex`` | Complex numbers | :file:`pybind11/complex.h` | ++------------------------------------+---------------------------+-----------------------------------+ +| ``std::array`` | STL static array | :file:`pybind11/stl.h` | ++------------------------------------+---------------------------+-----------------------------------+ +| ``std::vector`` | STL dynamic array | :file:`pybind11/stl.h` | ++------------------------------------+---------------------------+-----------------------------------+ +| ``std::deque`` | STL double-ended queue | :file:`pybind11/stl.h` | ++------------------------------------+---------------------------+-----------------------------------+ +| ``std::valarray`` | STL value array | :file:`pybind11/stl.h` | ++------------------------------------+---------------------------+-----------------------------------+ +| ``std::list`` | STL linked list | :file:`pybind11/stl.h` | ++------------------------------------+---------------------------+-----------------------------------+ +| ``std::map`` | STL ordered map | :file:`pybind11/stl.h` | ++------------------------------------+---------------------------+-----------------------------------+ +| ``std::unordered_map`` | STL unordered map | :file:`pybind11/stl.h` | ++------------------------------------+---------------------------+-----------------------------------+ +| ``std::set`` | STL ordered set | :file:`pybind11/stl.h` | ++------------------------------------+---------------------------+-----------------------------------+ +| ``std::unordered_set`` | STL unordered set | :file:`pybind11/stl.h` | ++------------------------------------+---------------------------+-----------------------------------+ +| ``std::optional`` | STL optional type (C++17) | :file:`pybind11/stl.h` | ++------------------------------------+---------------------------+-----------------------------------+ +| ``std::experimental::optional`` | STL optional type (exp.) | :file:`pybind11/stl.h` | ++------------------------------------+---------------------------+-----------------------------------+ +| ``std::variant<...>`` | Type-safe union (C++17) | :file:`pybind11/stl.h` | ++------------------------------------+---------------------------+-----------------------------------+ +| ``std::filesystem::path`` | STL path (C++17) [#]_ | :file:`pybind11/stl/filesystem.h` | ++------------------------------------+---------------------------+-----------------------------------+ +| ``std::function<...>`` | STL polymorphic function | :file:`pybind11/functional.h` | ++------------------------------------+---------------------------+-----------------------------------+ +| ``std::chrono::duration<...>`` | STL time duration | :file:`pybind11/chrono.h` | ++------------------------------------+---------------------------+-----------------------------------+ +| ``std::chrono::time_point<...>`` | STL date/time | :file:`pybind11/chrono.h` | ++------------------------------------+---------------------------+-----------------------------------+ +| ``Eigen::Matrix<...>`` | Eigen: dense matrix | :file:`pybind11/eigen.h` | ++------------------------------------+---------------------------+-----------------------------------+ +| ``Eigen::Map<...>`` | Eigen: mapped memory | :file:`pybind11/eigen.h` | ++------------------------------------+---------------------------+-----------------------------------+ +| ``Eigen::SparseMatrix<...>`` | Eigen: sparse matrix | :file:`pybind11/eigen.h` | ++------------------------------------+---------------------------+-----------------------------------+ + +.. [#] ``std::filesystem::path`` is converted to ``pathlib.Path`` and + can be loaded from ``os.PathLike``, ``str``, and ``bytes``. diff --git a/external_libraries/pybind11/docs/advanced/cast/stl.rst b/external_libraries/pybind11/docs/advanced/cast/stl.rst new file mode 100644 index 00000000..1e17bc38 --- /dev/null +++ b/external_libraries/pybind11/docs/advanced/cast/stl.rst @@ -0,0 +1,249 @@ +STL containers +############## + +Automatic conversion +==================== + +When including the additional header file :file:`pybind11/stl.h`, conversions +between ``std::vector<>``/``std::deque<>``/``std::list<>``/``std::array<>``/``std::valarray<>``, +``std::set<>``/``std::unordered_set<>``, and +``std::map<>``/``std::unordered_map<>`` and the Python ``list``, ``set`` and +``dict`` data structures are automatically enabled. The types ``std::pair<>`` +and ``std::tuple<>`` are already supported out of the box with just the core +:file:`pybind11/pybind11.h` header. + +The major downside of these implicit conversions is that containers must be +converted (i.e. copied) on every Python->C++ and C++->Python transition, which +can have implications on the program semantics and performance. Please read the +next sections for more details and alternative approaches that avoid this. + +.. note:: + + Arbitrary nesting of any of these types is possible. + +.. seealso:: + + The file :file:`tests/test_stl.cpp` contains a complete + example that demonstrates how to pass STL data types in more detail. + +.. _cpp17_container_casters: + +C++17 library containers +======================== + +The :file:`pybind11/stl.h` header also includes support for ``std::optional<>`` +and ``std::variant<>``. These require a C++17 compiler and standard library. +In C++14 mode, ``std::experimental::optional<>`` is supported if available. + +Various versions of these containers also exist for C++11 (e.g. in Boost). +pybind11 provides an easy way to specialize the ``type_caster`` for such +types: + +.. code-block:: cpp + + // `boost::optional` as an example -- can be any `std::optional`-like container + namespace PYBIND11_NAMESPACE { namespace detail { + template + struct type_caster> : optional_caster> {}; + }} + +The above should be placed in a header file and included in all translation units +where automatic conversion is needed. Similarly, a specialization can be provided +for custom variant types: + +.. code-block:: cpp + + // `boost::variant` as an example -- can be any `std::variant`-like container + namespace PYBIND11_NAMESPACE { namespace detail { + template + struct type_caster> : variant_caster> {}; + + // Specifies the function used to visit the variant -- `apply_visitor` instead of `visit` + template <> + struct visit_helper { + template + static auto call(Args &&...args) -> decltype(boost::apply_visitor(args...)) { + return boost::apply_visitor(args...); + } + }; + }} // namespace PYBIND11_NAMESPACE::detail + +The ``visit_helper`` specialization is not required if your ``name::variant`` provides +a ``name::visit()`` function. For any other function name, the specialization must be +included to tell pybind11 how to visit the variant. + +.. warning:: + + When converting a ``variant`` type, pybind11 follows the same rules as when + determining which function overload to call (:ref:`overload_resolution`), and + so the same caveats hold. In particular, the order in which the ``variant``'s + alternatives are listed is important, since pybind11 will try conversions in + this order. This means that, for example, when converting ``variant``, + the ``bool`` variant will never be selected, as any Python ``bool`` is already + an ``int`` and is convertible to a C++ ``int``. Changing the order of alternatives + (and using ``variant``, in this example) provides a solution. + +.. note:: + + pybind11 only supports the modern implementation of ``boost::variant`` + which makes use of variadic templates. This requires Boost 1.56 or newer. + +.. _opaque: + +Making opaque types +=================== + +pybind11 heavily relies on a template matching mechanism to convert parameters +and return values that are constructed from STL data types such as vectors, +linked lists, hash tables, etc. This even works in a recursive manner, for +instance to deal with lists of hash maps of pairs of elementary and custom +types, etc. + +However, a fundamental limitation of this approach is that internal conversions +between Python and C++ types involve a copy operation that prevents +pass-by-reference semantics. What does this mean? + +Suppose we bind the following function + +.. code-block:: cpp + + void append_1(std::vector &v) { + v.push_back(1); + } + +and call it from Python, the following happens: + +.. code-block:: pycon + + >>> v = [5, 6] + >>> append_1(v) + >>> print(v) + [5, 6] + +As you can see, when passing STL data structures by reference, modifications +are not propagated back the Python side. A similar situation arises when +exposing STL data structures using the ``def_readwrite`` or ``def_readonly`` +functions: + +.. code-block:: cpp + + /* ... definition ... */ + + class MyClass { + std::vector contents; + }; + + /* ... binding code ... */ + + py::class_(m, "MyClass") + .def(py::init<>()) + .def_readwrite("contents", &MyClass::contents); + +In this case, properties can be read and written in their entirety. However, an +``append`` operation involving such a list type has no effect: + +.. code-block:: pycon + + >>> m = MyClass() + >>> m.contents = [5, 6] + >>> print(m.contents) + [5, 6] + >>> m.contents.append(7) + >>> print(m.contents) + [5, 6] + +Finally, the involved copy operations can be costly when dealing with very +large lists. To deal with all of the above situations, pybind11 provides a +macro named ``PYBIND11_MAKE_OPAQUE(T)`` that disables the template-based +conversion machinery of types, thus rendering them *opaque*. The contents of +opaque objects are never inspected or extracted, hence they *can* be passed by +reference. For instance, to turn ``std::vector`` into an opaque type, add +the declaration + +.. code-block:: cpp + + PYBIND11_MAKE_OPAQUE(std::vector) + +before any binding code (e.g. invocations to ``class_::def()``, etc.). This +macro must be specified at the top level (and outside of any namespaces), since +it adds a template instantiation of ``type_caster``. If your binding code consists of +multiple compilation units, it must be present in every file (typically via a +common header) preceding any usage of ``std::vector``. Opaque types must +also have a corresponding ``py::class_`` declaration to associate them with a +name in Python, and to define a set of available operations, e.g.: + +.. code-block:: cpp + + py::class_>(m, "IntVector") + .def(py::init<>()) + .def("clear", &std::vector::clear) + .def("pop_back", &std::vector::pop_back) + .def("__len__", [](const std::vector &v) { return v.size(); }) + .def("__iter__", [](std::vector &v) { + return py::make_iterator(v.begin(), v.end()); + }, py::keep_alive<0, 1>()) /* Keep vector alive while iterator is used */ + // .... + +.. seealso:: + + The file :file:`tests/test_opaque_types.cpp` contains a complete + example that demonstrates how to create and expose opaque types using + pybind11 in more detail. + +.. _stl_bind: + +Binding STL containers +====================== + +The ability to expose STL containers as native Python objects is a fairly +common request, hence pybind11 also provides an optional header file named +:file:`pybind11/stl_bind.h` that does exactly this. The mapped containers try +to match the behavior of their native Python counterparts as much as possible. + +The following example showcases usage of :file:`pybind11/stl_bind.h`: + +.. code-block:: cpp + + // Don't forget this + #include + + PYBIND11_MAKE_OPAQUE(std::vector) + PYBIND11_MAKE_OPAQUE(std::map) + + // ... + + // later in binding code: + py::bind_vector>(m, "VectorInt"); + py::bind_map>(m, "MapStringDouble"); + +When binding STL containers pybind11 considers the types of the container's +elements to decide whether the container should be confined to the local module +(via the :ref:`module_local` feature). If the container element types are +anything other than already-bound custom types bound without +``py::module_local()`` the container binding will have ``py::module_local()`` +applied. This includes converting types such as numeric types, strings, Eigen +types; and types that have not yet been bound at the time of the stl container +binding. This module-local binding is designed to avoid potential conflicts +between module bindings (for example, from two separate modules each attempting +to bind ``std::vector`` as a python type). + +It is possible to override this behavior to force a definition to be either +module-local or global. To do so, you can pass the attributes +``py::module_local()`` (to make the binding module-local) or +``py::module_local(false)`` (to make the binding global) into the +``py::bind_vector`` or ``py::bind_map`` arguments: + +.. code-block:: cpp + + py::bind_vector>(m, "VectorInt", py::module_local(false)); + +Note, however, that such a global binding would make it impossible to load this +module at the same time as any other pybind module that also attempts to bind +the same container type (``std::vector`` in the above example). + +See :ref:`module_local` for more details on module-local bindings. + +.. seealso:: + + The file :file:`tests/test_stl_binders.cpp` shows how to use the + convenience STL container wrappers. diff --git a/external_libraries/pybind11/docs/advanced/cast/strings.rst b/external_libraries/pybind11/docs/advanced/cast/strings.rst new file mode 100644 index 00000000..271716b4 --- /dev/null +++ b/external_libraries/pybind11/docs/advanced/cast/strings.rst @@ -0,0 +1,296 @@ +Strings, bytes and Unicode conversions +###################################### + +Passing Python strings to C++ +============================= + +When a Python ``str`` is passed from Python to a C++ function that accepts +``std::string`` or ``char *`` as arguments, pybind11 will encode the Python +string to UTF-8. All Python ``str`` can be encoded in UTF-8, so this operation +does not fail. + +The C++ language is encoding agnostic. It is the responsibility of the +programmer to track encodings. It's often easiest to simply `use UTF-8 +everywhere `_. + +.. code-block:: c++ + + m.def("utf8_test", + [](const std::string &s) { + cout << "utf-8 is icing on the cake.\n"; + cout << s; + } + ); + m.def("utf8_charptr", + [](const char *s) { + cout << "My favorite food is\n"; + cout << s; + } + ); + +.. code-block:: pycon + + >>> utf8_test("🎂") + utf-8 is icing on the cake. + 🎂 + + >>> utf8_charptr("🍕") + My favorite food is + 🍕 + +.. note:: + + Some terminal emulators do not support UTF-8 or emoji fonts and may not + display the example above correctly. + +The results are the same whether the C++ function accepts arguments by value or +reference, and whether or not ``const`` is used. + +Passing bytes to C++ +-------------------- + +A Python ``bytes`` object will be passed to C++ functions that accept +``std::string`` or ``char*`` *without* conversion. In order to make a function +*only* accept ``bytes`` (and not ``str``), declare it as taking a ``py::bytes`` +argument. + + +Returning C++ strings to Python +=============================== + +When a C++ function returns a ``std::string`` or ``char*`` to a Python caller, +**pybind11 will assume that the string is valid UTF-8** and will decode it to a +native Python ``str``, using the same API as Python uses to perform +``bytes.decode('utf-8')``. If this implicit conversion fails, pybind11 will +raise a ``UnicodeDecodeError``. + +.. code-block:: c++ + + m.def("std_string_return", + []() { + return std::string("This string needs to be UTF-8 encoded"); + } + ); + +.. code-block:: pycon + + >>> isinstance(example.std_string_return(), str) + True + + +Because UTF-8 is inclusive of pure ASCII, there is never any issue with +returning a pure ASCII string to Python. If there is any possibility that the +string is not pure ASCII, it is necessary to ensure the encoding is valid +UTF-8. + +.. warning:: + + Implicit conversion assumes that a returned ``char *`` is null-terminated. + If there is no null terminator a buffer overrun will occur. + +Explicit conversions +-------------------- + +If some C++ code constructs a ``std::string`` that is not a UTF-8 string, one +can perform a explicit conversion and return a ``py::str`` object. Explicit +conversion has the same overhead as implicit conversion. + +.. code-block:: c++ + + // This uses the Python C API to convert Latin-1 to Unicode + m.def("str_output", + []() { + std::string s = "Send your r\xe9sum\xe9 to Alice in HR"; // Latin-1 + py::handle py_s = PyUnicode_DecodeLatin1(s.data(), s.length(), nullptr); + if (!py_s) { + throw py::error_already_set(); + } + return py::reinterpret_steal(py_s); + } + ); + +.. code-block:: pycon + + >>> str_output() + 'Send your résumé to Alice in HR' + +The `Python C API +`_ provides +several built-in codecs. Note that these all return *new* references, so +use :cpp:func:`reinterpret_steal` when converting them to a :cpp:class:`str`. + + +One could also use a third party encoding library such as libiconv to transcode +to UTF-8. + +Return C++ strings without conversion +------------------------------------- + +If the data in a C++ ``std::string`` does not represent text and should be +returned to Python as ``bytes``, then one can return the data as a +``py::bytes`` object. + +.. code-block:: c++ + + m.def("return_bytes", + []() { + std::string s("\xba\xd0\xba\xd0"); // Not valid UTF-8 + return py::bytes(s); // Return the data without transcoding + } + ); + +.. code-block:: pycon + + >>> example.return_bytes() + b'\xba\xd0\xba\xd0' + + +Note the asymmetry: pybind11 will convert ``bytes`` to ``std::string`` without +encoding, but cannot convert ``std::string`` back to ``bytes`` implicitly. + +.. code-block:: c++ + + m.def("asymmetry", + [](std::string s) { // Accepts str or bytes from Python + return s; // Looks harmless, but implicitly converts to str + } + ); + +.. code-block:: pycon + + >>> isinstance(example.asymmetry(b"have some bytes"), str) + True + + >>> example.asymmetry(b"\xba\xd0\xba\xd0") # invalid utf-8 as bytes + UnicodeDecodeError: 'utf-8' codec can't decode byte 0xba in position 0: invalid start byte + + +Wide character strings +====================== + +When a Python ``str`` is passed to a C++ function expecting ``std::wstring``, +``wchar_t*``, ``std::u16string`` or ``std::u32string``, the ``str`` will be +encoded to UTF-16 or UTF-32 depending on how the C++ compiler implements each +type, in the platform's native endianness. When strings of these types are +returned, they are assumed to contain valid UTF-16 or UTF-32, and will be +decoded to Python ``str``. + +.. code-block:: c++ + + #define UNICODE + #include + + m.def("set_window_text", + [](HWND hwnd, std::wstring s) { + // Call SetWindowText with null-terminated UTF-16 string + ::SetWindowText(hwnd, s.c_str()); + } + ); + m.def("get_window_text", + [](HWND hwnd) { + const int buffer_size = ::GetWindowTextLength(hwnd) + 1; + auto buffer = std::make_unique< wchar_t[] >(buffer_size); + + ::GetWindowText(hwnd, buffer.data(), buffer_size); + + std::wstring text(buffer.get()); + + // wstring will be converted to Python str + return text; + } + ); + +Strings in multibyte encodings such as Shift-JIS must transcoded to a +UTF-8/16/32 before being returned to Python. + + +Character literals +================== + +C++ functions that accept character literals as input will receive the first +character of a Python ``str`` as their input. If the string is longer than one +Unicode character, trailing characters will be ignored. + +When a character literal is returned from C++ (such as a ``char`` or a +``wchar_t``), it will be converted to a ``str`` that represents the single +character. + +.. code-block:: c++ + + m.def("pass_char", [](char c) { return c; }); + m.def("pass_wchar", [](wchar_t w) { return w; }); + +.. code-block:: pycon + + >>> example.pass_char("A") + 'A' + +While C++ will cast integers to character types (``char c = 0x65;``), pybind11 +does not convert Python integers to characters implicitly. The Python function +``chr()`` can be used to convert integers to characters. + +.. code-block:: pycon + + >>> example.pass_char(0x65) + TypeError + + >>> example.pass_char(chr(0x65)) + 'A' + +If the desire is to work with an 8-bit integer, use ``int8_t`` or ``uint8_t`` +as the argument type. + +Grapheme clusters +----------------- + +A single grapheme may be represented by two or more Unicode characters. For +example 'é' is usually represented as U+00E9 but can also be expressed as the +combining character sequence U+0065 U+0301 (that is, the letter 'e' followed by +a combining acute accent). The combining character will be lost if the +two-character sequence is passed as an argument, even though it renders as a +single grapheme. + +.. code-block:: pycon + + >>> example.pass_wchar("é") + 'é' + + >>> combining_e_acute = "e" + "\u0301" + + >>> combining_e_acute + 'é' + + >>> combining_e_acute == "é" + False + + >>> example.pass_wchar(combining_e_acute) + 'e' + +Normalizing combining characters before passing the character literal to C++ +may resolve *some* of these issues: + +.. code-block:: pycon + + >>> example.pass_wchar(unicodedata.normalize("NFC", combining_e_acute)) + 'é' + +In some languages (Thai for example), there are `graphemes that cannot be +expressed as a single Unicode code point +`_, so there is +no way to capture them in a C++ character type. + + +C++17 string views +================== + +C++17 string views are automatically supported when compiling in C++17 mode. +They follow the same rules for encoding and decoding as the corresponding STL +string type (for example, a ``std::u16string_view`` argument will be passed +UTF-16-encoded data, and a returned ``std::string_view`` will be decoded as +UTF-8). + +References +========== + +* `The Absolute Minimum Every Software Developer Absolutely, Positively Must Know About Unicode and Character Sets (No Excuses!) `_ +* `C++ - Using STL Strings at Win32 API Boundaries `_ diff --git a/external_libraries/pybind11/docs/advanced/classes.rst b/external_libraries/pybind11/docs/advanced/classes.rst new file mode 100644 index 00000000..bfbaea60 --- /dev/null +++ b/external_libraries/pybind11/docs/advanced/classes.rst @@ -0,0 +1,1432 @@ +Classes +####### + +This section presents advanced binding code for classes and it is assumed +that you are already familiar with the basics from :doc:`/classes`. + +.. _overriding_virtuals: + +Overriding virtual functions in Python +====================================== + +Suppose that a C++ class or interface has a virtual function that we'd like +to override from within Python (we'll focus on the class ``Animal``; ``Dog`` is +given as a specific example of how one would do this with traditional C++ +code). + +.. code-block:: cpp + + class Animal { + public: + virtual ~Animal() { } + virtual std::string go(int n_times) = 0; + }; + + class Dog : public Animal { + public: + std::string go(int n_times) override { + std::string result; + for (int i=0; igo(3); + } + +Normally, the binding code for these classes would look as follows: + +.. code-block:: cpp + + PYBIND11_MODULE(example, m, py::mod_gil_not_used()) { + py::class_(m, "Animal") + .def("go", &Animal::go); + + py::class_(m, "Dog") + .def(py::init<>()); + + m.def("call_go", &call_go); + } + +However, these bindings are impossible to extend: ``Animal`` is not +constructible, and we clearly require some kind of "trampoline" that +redirects virtual calls back to Python. + +Defining a new type of ``Animal`` from within Python is possible but requires a +helper class that is defined as follows: + +.. code-block:: cpp + + class PyAnimal : public Animal, public py::trampoline_self_life_support { + public: + /* Inherit the constructors */ + using Animal::Animal; + + /* Trampoline (need one for each virtual function) */ + std::string go(int n_times) override { + PYBIND11_OVERRIDE_PURE( + std::string, /* Return type */ + Animal, /* Parent class */ + go, /* Name of function in C++ (must match Python name) */ + n_times /* Argument(s) */ + ); + } + }; + +The ``py::trampoline_self_life_support`` base class is needed to ensure +that a ``std::unique_ptr`` can safely be passed between Python and C++. To +help you steer clear of notorious pitfalls (e.g. inheritance slicing), +pybind11 enforces that trampoline classes inherit from +``py::trampoline_self_life_support`` if used in in combination with +``py::smart_holder``. + +.. note:: + For completeness, the base class has no effect if a holder other than + ``py::smart_holder`` used, including the default ``std::unique_ptr``. + To avoid confusion, pybind11 will fail to compile bindings that combine + ``py::trampoline_self_life_support`` with a holder other than + ``py::smart_holder``. + + Please think twice, though, before deciding to not use the safer + ``py::smart_holder``. The pitfalls associated with avoiding it are very + real, and the overhead for using it is very likely in the noise. + +The macro :c:macro:`PYBIND11_OVERRIDE_PURE` should be used for pure virtual +functions, and :c:macro:`PYBIND11_OVERRIDE` should be used for functions which have +a default implementation. There are also two alternate macros +:c:macro:`PYBIND11_OVERRIDE_PURE_NAME` and :c:macro:`PYBIND11_OVERRIDE_NAME` which +take a string-valued name argument between the *Parent class* and *Name of the +function* slots, which defines the name of function in Python. This is required +when the C++ and Python versions of the +function have different names, e.g. ``operator()`` vs ``__call__``. + +The binding code also needs a few minor adaptations (highlighted): + +.. code-block:: cpp + :emphasize-lines: 2,3 + + PYBIND11_MODULE(example, m, py::mod_gil_not_used()) { + py::class_(m, "Animal") + .def(py::init<>()) + .def("go", &Animal::go); + + py::class_(m, "Dog") + .def(py::init<>()); + + m.def("call_go", &call_go); + } + +Importantly, pybind11 is made aware of the trampoline helper class by +specifying it as an extra template argument to ``py::class_``. (This can also +be combined with other template arguments such as a custom holder type; the +order of template types does not matter). Following this, we are able to +define a constructor as usual. + +Bindings should be made against the actual class, not the trampoline helper class. + +.. code-block:: cpp + :emphasize-lines: 3 + + py::class_(m, "Animal"); + .def(py::init<>()) + .def("go", &Animal::go); /* <--- DO NOT USE &PyAnimal::go HERE */ + +Note, however, that the above is sufficient for allowing python classes to +extend ``Animal``, but not ``Dog``: see :ref:`virtual_and_inheritance` for the +necessary steps required to providing proper overriding support for inherited +classes. + +The Python session below shows how to override ``Animal::go`` and invoke it via +a virtual method call. + +.. code-block:: pycon + + >>> from example import * + >>> d = Dog() + >>> call_go(d) + 'woof! woof! woof! ' + >>> class Cat(Animal): + ... def go(self, n_times): + ... return "meow! " * n_times + ... + >>> c = Cat() + >>> call_go(c) + 'meow! meow! meow! ' + +If you are defining a custom constructor in a derived Python class, you *must* +ensure that you explicitly call the bound C++ constructor using ``__init__``, +*regardless* of whether it is a default constructor or not. Otherwise, the +memory for the C++ portion of the instance will be left uninitialized, which +will generally leave the C++ instance in an invalid state and cause undefined +behavior if the C++ instance is subsequently used. + +.. versionchanged:: 2.6 + The default pybind11 metaclass will throw a ``TypeError`` when it detects + that ``__init__`` was not called by a derived class. + +Here is an example: + +.. code-block:: python + + class Dachshund(Dog): + def __init__(self, name): + Dog.__init__(self) # Without this, a TypeError is raised. + self.name = name + + def bark(self): + return "yap!" + +Note that a direct ``__init__`` constructor *should be called*, and ``super()`` +should not be used. For simple cases of linear inheritance, ``super()`` +may work, but once you begin mixing Python and C++ multiple inheritance, +things will fall apart due to differences between Python's MRO and C++'s +mechanisms. + +Please take a look at the :ref:`macro_notes` before using this feature. + +.. note:: + + When the overridden type returns a reference or pointer to a type that + pybind11 converts from Python (for example, numeric values, std::string, + and other built-in value-converting types), there are some limitations to + be aware of: + + - because in these cases there is no C++ variable to reference (the value + is stored in the referenced Python variable), pybind11 provides one in + the PYBIND11_OVERRIDE macros (when needed) with static storage duration. + Note that this means that invoking the overridden method on *any* + instance will change the referenced value stored in *all* instances of + that type. + + - Attempts to modify a non-const reference will not have the desired + effect: it will change only the static cache variable, but this change + will not propagate to underlying Python instance, and the change will be + replaced the next time the override is invoked. + +.. warning:: + + The :c:macro:`PYBIND11_OVERRIDE` and accompanying macros used to be called + ``PYBIND11_OVERLOAD`` up until pybind11 v2.5.0, and :func:`get_override` + used to be called ``get_overload``. This naming was corrected and the older + macro and function names may soon be deprecated, in order to reduce + confusion with overloaded functions and methods and ``py::overload_cast`` + (see :ref:`classes`). + +.. seealso:: + + The file :file:`tests/test_virtual_functions.cpp` contains a complete + example that demonstrates how to override virtual functions using pybind11 + in more detail. + +.. _virtual_and_inheritance: + +Combining virtual functions and inheritance +=========================================== + +When combining virtual methods with inheritance, you need to be sure to provide +an override for each method for which you want to allow overrides from derived +python classes. For example, suppose we extend the above ``Animal``/``Dog`` +example as follows: + +.. code-block:: cpp + + class Animal { + public: + virtual std::string go(int n_times) = 0; + virtual std::string name() { return "unknown"; } + }; + class Dog : public Animal { + public: + std::string go(int n_times) override { + std::string result; + for (int i=0; i + class PyAnimal : public AnimalBase, public py::trampoline_self_life_support { + public: + using AnimalBase::AnimalBase; // Inherit constructors + std::string go(int n_times) override { PYBIND11_OVERRIDE_PURE(std::string, AnimalBase, go, n_times); } + std::string name() override { PYBIND11_OVERRIDE(std::string, AnimalBase, name, ); } + }; + template + class PyDog : public PyAnimal, public py::trampoline_self_life_support { + public: + using PyAnimal::PyAnimal; // Inherit constructors + // Override PyAnimal's pure virtual go() with a non-pure one: + std::string go(int n_times) override { PYBIND11_OVERRIDE(std::string, DogBase, go, n_times); } + std::string bark() override { PYBIND11_OVERRIDE(std::string, DogBase, bark, ); } + }; + +This technique has the advantage of requiring just one trampoline method to be +declared per virtual method and pure virtual method override. It does, +however, require the compiler to generate at least as many methods (and +possibly more, if both pure virtual and overridden pure virtual methods are +exposed, as above). + +The classes are then registered with pybind11 using: + +.. code-block:: cpp + + py::class_, py::smart_holder> animal(m, "Animal"); + py::class_, py::smart_holder> dog(m, "Dog"); + py::class_, py::smart_holder> husky(m, "Husky"); + // ... add animal, dog, husky definitions + +Note that ``Husky`` did not require a dedicated trampoline template class at +all, since it neither declares any new virtual methods nor provides any pure +virtual method implementations. + +With either the repeated-virtuals or templated trampoline methods in place, you +can now create a python class that inherits from ``Dog``: + +.. code-block:: python + + class ShihTzu(Dog): + def bark(self): + return "yip!" + +.. seealso:: + + See the file :file:`tests/test_virtual_functions.cpp` for complete examples + using both the duplication and templated trampoline approaches. + +.. _extended_aliases: + +Extended trampoline class functionality +======================================= + +.. _extended_class_functionality_forced_trampoline: + +Forced trampoline class initialisation +-------------------------------------- +The trampoline classes described in the previous sections are, by default, only +initialized when needed. More specifically, they are initialized when a python +class actually inherits from a registered type (instead of merely creating an +instance of the registered type), or when a registered constructor is only +valid for the trampoline class but not the registered class. This is primarily +for performance reasons: when the trampoline class is not needed for anything +except virtual method dispatching, not initializing the trampoline class +improves performance by avoiding needing to do a run-time check to see if the +inheriting python instance has an overridden method. + +Sometimes, however, it is useful to always initialize a trampoline class as an +intermediate class that does more than just handle virtual method dispatching. +For example, such a class might perform extra class initialization, extra +destruction operations, and might define new members and methods to enable a +more python-like interface to a class. + +In order to tell pybind11 that it should *always* initialize the trampoline +class when creating new instances of a type, the class constructors should be +declared using ``py::init_alias()`` instead of the usual +``py::init()``. This forces construction via the trampoline class, +ensuring member initialization and (eventual) destruction. + +.. seealso:: + + See the file :file:`tests/test_virtual_functions.cpp` for complete examples + showing both normal and forced trampoline instantiation. + +Different method signatures +--------------------------- +The macro's introduced in :ref:`overriding_virtuals` cover most of the standard +use cases when exposing C++ classes to Python. Sometimes it is hard or unwieldy +to create a direct one-on-one mapping between the arguments and method return +type. + +An example would be when the C++ signature contains output arguments using +references (See also :ref:`faq_reference_arguments`). Another way of solving +this is to use the method body of the trampoline class to do conversions to the +input and return of the Python method. + +The main building block to do so is the :func:`get_override`, this function +allows retrieving a method implemented in Python from within the trampoline's +methods. Consider for example a C++ method which has the signature +``bool myMethod(int32_t& value)``, where the return indicates whether +something should be done with the ``value``. This can be made convenient on the +Python side by allowing the Python function to return ``None`` or an ``int``: + +.. code-block:: cpp + + bool MyClass::myMethod(int32_t& value) + { + pybind11::gil_scoped_acquire gil; // Acquire the GIL while in this scope. + // Try to look up the overridden method on the Python side. + pybind11::function override = pybind11::get_override(this, "myMethod"); + if (override) { // method is found + auto obj = override(value); // Call the Python function. + if (py::isinstance(obj)) { // check if it returned a Python integer type + value = obj.cast(); // Cast it and assign it to the value. + return true; // Return true; value should be used. + } else { + return false; // Python returned none, return false. + } + } + return false; // Alternatively return MyClass::myMethod(value); + } + +Avoiding Inheritance Slicing and ``std::weak_ptr`` surprises +------------------------------------------------------------ + +When working with classes that use virtual functions and are subclassed +in Python, special care must be taken when converting Python objects to +``std::shared_ptr``. Depending on whether the class uses a plain +``std::shared_ptr`` holder or ``py::smart_holder``, the resulting +``shared_ptr`` may either allow inheritance slicing or lead to potentially +surprising behavior when constructing ``std::weak_ptr`` instances. + +This section explains how ``std::shared_ptr`` and ``py::smart_holder`` manage +object lifetimes differently, how these differences affect trampoline-derived +objects, and what options are available to achieve the situation-specific +desired behavior. + +When using ``std::shared_ptr`` as the holder type, converting a Python object +to a ``std::shared_ptr`` (e.g., ``obj.cast>()``, or simply +passing the Python object as an argument to a ``.def()``-ed function) returns +a ``shared_ptr`` that shares ownership with the original ``class_`` holder, +usually preserving object lifetime. However, for Python classes that derive from +a trampoline, if the Python object is destroyed, only the base C++ object may +remain alive, leading to inheritance slicing +(see `#1333 `_). + +In contrast, with ``py::smart_holder``, converting a Python object to +a ``std::shared_ptr`` returns a new ``shared_ptr`` with an independent +control block that keeps the derived Python object alive. This avoids +inheritance slicing but can lead to unintended behavior when creating +``std::weak_ptr`` instances +(see `#5623 `_). + +If it is necessary to obtain a ``std::weak_ptr`` that shares the control block +with the ``smart_holder``—at the cost of reintroducing potential inheritance +slicing—you can use ``py::potentially_slicing_weak_ptr(obj)``. + +When precise lifetime management of derived Python objects is important, +using a Python-side ``weakref`` is the most reliable approach, as it avoids +both inheritance slicing and unintended interactions with ``std::weak_ptr`` +semantics in C++. + +.. seealso:: + + * :func:`potentially_slicing_weak_ptr` C++ documentation + * :file:`tests/test_potentially_slicing_weak_ptr.cpp` + + +.. _custom_constructors: + +Custom constructors +=================== + +The syntax for binding constructors was previously introduced, but it only +works when a constructor of the appropriate arguments actually exists on the +C++ side. To extend this to more general cases, pybind11 makes it possible +to bind factory functions as constructors. For example, suppose you have a +class like this: + +.. code-block:: cpp + + class Example { + private: + Example(int); // private constructor + public: + // Factory function: + static Example create(int a) { return Example(a); } + }; + + py::class_(m, "Example") + .def(py::init(&Example::create)); + +While it is possible to create a straightforward binding of the static +``create`` method, it may sometimes be preferable to expose it as a constructor +on the Python side. This can be accomplished by calling ``.def(py::init(...))`` +with the function reference returning the new instance passed as an argument. +It is also possible to use this approach to bind a function returning a new +instance by raw pointer or by the holder (e.g. ``std::unique_ptr``). + +The following example shows the different approaches: + +.. code-block:: cpp + + class Example { + private: + Example(int); // private constructor + public: + // Factory function - returned by value: + static Example create(int a) { return Example(a); } + + // These constructors are publicly callable: + Example(double); + Example(int, int); + Example(std::string); + }; + + py::class_(m, "Example") + // Bind the factory function as a constructor: + .def(py::init(&Example::create)) + // Bind a lambda function returning a pointer wrapped in a holder: + .def(py::init([](std::string arg) { + return std::unique_ptr(new Example(arg)); + })) + // Return a raw pointer: + .def(py::init([](int a, int b) { return new Example(a, b); })) + // You can mix the above with regular C++ constructor bindings as well: + .def(py::init()) + ; + +When the constructor is invoked from Python, pybind11 will call the factory +function and store the resulting C++ instance in the Python instance. + +When combining factory functions constructors with :ref:`virtual function +trampolines ` there are two approaches. The first is to +add a constructor to the alias class that takes a base value by +rvalue-reference. If such a constructor is available, it will be used to +construct an alias instance from the value returned by the factory function. +The second option is to provide two factory functions to ``py::init()``: the +first will be invoked when no alias class is required (i.e. when the class is +being used but not inherited from in Python), and the second will be invoked +when an alias is required. + +You can also specify a single factory function that always returns an alias +instance: this will result in behaviour similar to ``py::init_alias<...>()``, +as described in the :ref:`extended trampoline class documentation +`. + +The following example shows the different factory approaches for a class with +an alias: + +.. code-block:: cpp + + #include + class Example { + public: + // ... + virtual ~Example() = default; + }; + class PyExample : public Example, public py::trampoline_self_life_support { + public: + using Example::Example; + PyExample(Example &&base) : Example(std::move(base)) {} + }; + py::class_(m, "Example") + // Returns an Example pointer. If a PyExample is needed, the Example + // instance will be moved via the extra constructor in PyExample, above. + .def(py::init([]() { return new Example(); })) + // Two callbacks: + .def(py::init([]() { return new Example(); } /* no alias needed */, + []() { return new PyExample(); } /* alias needed */)) + // *Always* returns an alias instance (like py::init_alias<>()) + .def(py::init([]() { return new PyExample(); })) + ; + +Brace initialization +-------------------- + +``pybind11::init<>`` internally uses C++11 brace initialization to call the +constructor of the target class. This means that it can be used to bind +*implicit* constructors as well: + +.. code-block:: cpp + + struct Aggregate { + int a; + std::string b; + }; + + py::class_(m, "Aggregate") + .def(py::init()); + +.. note:: + + Note that brace initialization preferentially invokes constructor overloads + taking a ``std::initializer_list``. In the rare event that this causes an + issue, you can work around it by using ``py::init(...)`` with a lambda + function that constructs the new object as desired. + +.. _classes_with_non_public_destructors: + +Non-public destructors +====================== + +If a class has a private or protected destructor (as might e.g. be the case in +a singleton pattern), a compile error will occur when creating bindings via +pybind11. The underlying issue is that the ``std::unique_ptr`` holder type that +is responsible for managing the lifetime of instances will reference the +destructor even if no deallocations ever take place. In order to expose classes +with private or protected destructors, it is possible to override the holder +type via a holder type argument to ``py::class_``. Pybind11 provides a helper +class ``py::nodelete`` that disables any destructor invocations. In this case, +it is crucial that instances are deallocated on the C++ side to avoid memory +leaks. + +.. code-block:: cpp + + /* ... definition ... */ + + class MyClass { + private: + ~MyClass() { } + }; + + /* ... binding code ... */ + + py::class_>(m, "MyClass") + .def(py::init<>()) + +.. _destructors_that_call_python: + +Destructors that call Python +============================ + +If a Python function is invoked from a C++ destructor, an exception may be thrown +of type :class:`error_already_set`. If this error is thrown out of a class destructor, +``std::terminate()`` will be called, terminating the process. Class destructors +must catch all exceptions of type :class:`error_already_set` to discard the Python +exception using :func:`error_already_set::discard_as_unraisable`. + +Every Python function should be treated as *possibly throwing*. When a Python generator +stops yielding items, Python will throw a ``StopIteration`` exception, which can pass +though C++ destructors if the generator's stack frame holds the last reference to C++ +objects. + +For more information, see :ref:`the documentation on exceptions `. + +.. code-block:: cpp + + class MyClass { + public: + ~MyClass() { + try { + py::print("Even printing is dangerous in a destructor"); + py::exec("raise ValueError('This is an unraisable exception')"); + } catch (py::error_already_set &e) { + // error_context should be information about where/why the occurred, + // e.g. use __func__ to get the name of the current function + e.discard_as_unraisable(__func__); + } + } + }; + +.. note:: + + pybind11 does not support C++ destructors marked ``noexcept(false)``. + +.. versionadded:: 2.6 + +.. _implicit_conversions: + +Implicit conversions +==================== + +Suppose that instances of two types ``A`` and ``B`` are used in a project, and +that an ``A`` can easily be converted into an instance of type ``B`` (examples of this +could be a fixed and an arbitrary precision number type). + +.. code-block:: cpp + + py::class_(m, "A") + /// ... members ... + + py::class_(m, "B") + .def(py::init()) + /// ... members ... + + m.def("func", + [](const B &) { /* .... */ } + ); + +To invoke the function ``func`` using a variable ``a`` containing an ``A`` +instance, we'd have to write ``func(B(a))`` in Python. On the other hand, C++ +will automatically apply an implicit type conversion, which makes it possible +to directly write ``func(a)``. + +In this situation (i.e. where ``B`` has a constructor that converts from +``A``), the following statement enables similar implicit conversions on the +Python side: + +.. code-block:: cpp + + py::implicitly_convertible(); + +.. note:: + + Implicit conversions from ``A`` to ``B`` only work when ``B`` is a custom + data type that is exposed to Python via pybind11. + + To prevent runaway recursion, implicit conversions are non-reentrant: an + implicit conversion invoked as part of another implicit conversion of the + same type (i.e. from ``A`` to ``B``) will fail. + +.. _static_properties: + +Static properties +================= + +The section on :ref:`properties` discussed the creation of instance properties +that are implemented in terms of C++ getters and setters. + +Static properties can also be created in a similar way to expose getters and +setters of static class attributes. Note that the implicit ``self`` argument +also exists in this case and is used to pass the Python ``type`` subclass +instance. This parameter will often not be needed by the C++ side, and the +following example illustrates how to instantiate a lambda getter function +that ignores it: + +.. code-block:: cpp + + py::class_(m, "Foo") + .def_property_readonly_static("foo", [](py::object /* self */) { return Foo(); }); + +Operator overloading +==================== + +Suppose that we're given the following ``Vector2`` class with a vector addition +and scalar multiplication operation, all implemented using overloaded operators +in C++. + +.. code-block:: cpp + + class Vector2 { + public: + Vector2(float x, float y) : x(x), y(y) { } + + Vector2 operator+(const Vector2 &v) const { return Vector2(x + v.x, y + v.y); } + Vector2 operator*(float value) const { return Vector2(x * value, y * value); } + Vector2& operator+=(const Vector2 &v) { x += v.x; y += v.y; return *this; } + Vector2& operator*=(float v) { x *= v; y *= v; return *this; } + + friend Vector2 operator*(float f, const Vector2 &v) { + return Vector2(f * v.x, f * v.y); + } + + std::string toString() const { + return "[" + std::to_string(x) + ", " + std::to_string(y) + "]"; + } + private: + float x, y; + }; + +The following snippet shows how the above operators can be conveniently exposed +to Python. + +.. code-block:: cpp + + #include + + PYBIND11_MODULE(example, m, py::mod_gil_not_used()) { + py::class_(m, "Vector2") + .def(py::init()) + .def(py::self + py::self) + .def(py::self += py::self) + .def(py::self *= float()) + .def(float() * py::self) + .def(py::self * float()) + .def(-py::self) + .def("__repr__", &Vector2::toString); + } + +Note that a line like + +.. code-block:: cpp + + .def(py::self * float()) + +is really just short hand notation for + +.. code-block:: cpp + + .def("__mul__", [](const Vector2 &a, float b) { + return a * b; + }, py::is_operator()) + +This can be useful for exposing additional operators that don't exist on the +C++ side, or to perform other types of customization. The ``py::is_operator`` +flag marker is needed to inform pybind11 that this is an operator, which +returns ``NotImplemented`` when invoked with incompatible arguments rather than +throwing a type error. + +.. note:: + + To use the more convenient ``py::self`` notation, the additional + header file :file:`pybind11/operators.h` must be included. + +.. seealso:: + + The file :file:`tests/test_operator_overloading.cpp` contains a + complete example that demonstrates how to work with overloaded operators in + more detail. + +.. _pickling: + +Pickling support +================ + +Python's ``pickle`` module provides a powerful facility to serialize and +de-serialize a Python object graph into a binary data stream. To pickle and +unpickle C++ classes using pybind11, a ``py::pickle()`` definition must be +provided. Suppose the class in question has the following signature: + +.. code-block:: cpp + + class Pickleable { + public: + Pickleable(const std::string &value) : m_value(value) { } + const std::string &value() const { return m_value; } + + void setExtra(int extra) { m_extra = extra; } + int extra() const { return m_extra; } + private: + std::string m_value; + int m_extra = 0; + }; + +Pickling support in Python is enabled by defining the ``__setstate__`` and +``__getstate__`` methods [#f3]_. For pybind11 classes, use ``py::pickle()`` +to bind these two functions: + +.. code-block:: cpp + + py::class_(m, "Pickleable") + .def(py::init()) + .def("value", &Pickleable::value) + .def("extra", &Pickleable::extra) + .def("setExtra", &Pickleable::setExtra) + .def(py::pickle( + [](const Pickleable &p) { // __getstate__ + /* Return a tuple that fully encodes the state of the object */ + return py::make_tuple(p.value(), p.extra()); + }, + [](py::tuple t) { // __setstate__ + if (t.size() != 2) + throw std::runtime_error("Invalid state!"); + + /* Create a new C++ instance */ + Pickleable p(t[0].cast()); + + /* Assign any additional state */ + p.setExtra(t[1].cast()); + + return p; + } + )); + +The ``__setstate__`` part of the ``py::pickle()`` definition follows the same +rules as the single-argument version of ``py::init()``. The return type can be +a value, pointer or holder type. See :ref:`custom_constructors` for details. + +An instance can now be pickled as follows: + +.. code-block:: python + + import pickle + + p = Pickleable("test_value") + p.setExtra(15) + data = pickle.dumps(p) + + +.. note:: + If given, the second argument to ``dumps`` must be 2 or larger - 0 and 1 are + not supported. Newer versions are also fine; for instance, specify ``-1`` to + always use the latest available version. Beware: failure to follow these + instructions will cause important pybind11 memory allocation routines to be + skipped during unpickling, which will likely lead to memory corruption + and/or segmentation faults. + +.. seealso:: + + The file :file:`tests/test_pickling.cpp` contains a complete example + that demonstrates how to pickle and unpickle types using pybind11 in more + detail. + +.. [#f3] http://docs.python.org/3/library/pickle.html#pickling-class-instances + +Deepcopy support +================ + +Python normally uses references in assignments. Sometimes a real copy is needed +to prevent changing all copies. The ``copy`` module [#f5]_ provides these +capabilities. + +A class with pickle support is automatically also (deep)copy +compatible. However, performance can be improved by adding custom +``__copy__`` and ``__deepcopy__`` methods. + +For simple classes (deep)copy can be enabled by using the copy constructor, +which should look as follows: + +.. code-block:: cpp + + py::class_(m, "Copyable") + .def("__copy__", [](const Copyable &self) { + return Copyable(self); + }) + .def("__deepcopy__", [](const Copyable &self, py::dict) { + return Copyable(self); + }, "memo"_a); + +.. note:: + + Dynamic attributes will not be copied in this example. + +.. [#f5] https://docs.python.org/3/library/copy.html + +Multiple Inheritance +==================== + +pybind11 can create bindings for types that derive from multiple base types +(aka. *multiple inheritance*). To do so, specify all bases in the template +arguments of the ``py::class_`` declaration: + +.. code-block:: cpp + + py::class_(m, "MyType") + ... + +The base types can be specified in arbitrary order, and they can even be +interspersed with alias types and holder types (discussed earlier in this +document)---pybind11 will automatically find out which is which. The only +requirement is that the first template argument is the type to be declared. + +It is also permitted to inherit multiply from exported C++ classes in Python, +as well as inheriting from multiple Python and/or pybind11-exported classes. + +There is one caveat regarding the implementation of this feature: + +When only one base type is specified for a C++ type that actually has multiple +bases, pybind11 will assume that it does not participate in multiple +inheritance, which can lead to undefined behavior. In such cases, add the tag +``multiple_inheritance`` to the class constructor: + +.. code-block:: cpp + + py::class_(m, "MyType", py::multiple_inheritance()); + +The tag is redundant and does not need to be specified when multiple base types +are listed. + +.. _module_local: + +Module-local class bindings +=========================== + +When creating a binding for a class, pybind11 by default makes that binding +"global" across modules. What this means is that instances whose type is +defined with a ``py::class_`` statement in one module can be passed to or +returned from a function defined in any other module that is "ABI compatible" +with the first, i.e., that was built with sufficiently similar versions of +pybind11 and of the C++ compiler and C++ standard library. The internal data +structures that pybind11 uses to keep track of its types and instances are +shared just as they would be if everything were in the same module. +For example, this allows the following: + +.. code-block:: cpp + + // In the module1.cpp binding code for module1: + py::class_(m, "Pet") + .def(py::init()) + .def_readonly("name", &Pet::name); + +.. code-block:: cpp + + // In the module2.cpp binding code for module2: + m.def("create_pet", [](std::string name) { return new Pet(name); }); + +.. code-block:: pycon + + >>> from module1 import Pet + >>> from module2 import create_pet + >>> pet1 = Pet("Kitty") + >>> pet2 = create_pet("Doggy") + >>> pet2.name() + 'Doggy' + +When writing binding code for a library, this is usually desirable: this +allows, for example, splitting up a complex library into multiple Python +modules. + +In some cases, however, this can cause conflicts. For example, suppose two +unrelated modules make use of an external C++ library and each provide custom +bindings for one of that library's classes. This will result in an error when +a Python program attempts to import both modules (directly or indirectly) +because of conflicting definitions on the external type: + +.. code-block:: cpp + + // dogs.cpp + + // Binding for external library class: + py::class_(m, "Pet") + .def("name", &pets::Pet::name); + + // Binding for local extension class: + py::class_(m, "Dog") + .def(py::init()); + +.. code-block:: cpp + + // cats.cpp, in a completely separate project from the above dogs.cpp. + + // Binding for external library class: + py::class_(m, "Pet") + .def("get_name", &pets::Pet::name); + + // Binding for local extending class: + py::class_(m, "Cat") + .def(py::init()); + +.. code-block:: pycon + + >>> import cats + >>> import dogs + Traceback (most recent call last): + File "", line 1, in + ImportError: generic_type: type "Pet" is already registered! + +To get around this, you can tell pybind11 to keep the external class binding +localized to the module by passing the ``py::module_local()`` attribute into +the ``py::class_`` constructor: + +.. code-block:: cpp + + // Pet binding in dogs.cpp: + py::class_(m, "Pet", py::module_local()) + .def("name", &pets::Pet::name); + +.. code-block:: cpp + + // Pet binding in cats.cpp: + py::class_(m, "Pet", py::module_local()) + .def("get_name", &pets::Pet::name); + +This makes the Python-side ``dogs.Pet`` and ``cats.Pet`` into distinct classes, +avoiding the conflict and allowing both modules to be loaded. C++ code in the +``dogs`` module that casts or returns a ``Pet`` instance will result in a +``dogs.Pet`` Python instance, while C++ code in the ``cats`` module will result +in a ``cats.Pet`` Python instance. + +This does come with two caveats, however: First, external modules cannot return +or cast a ``Pet`` instance to Python (unless they also provide their own local +bindings). Second, from the Python point of view they are two distinct classes. + +Note that the locality only applies in the C++ -> Python direction. When +passing such a ``py::module_local`` type into a C++ function, the module-local +classes are still considered. This means that if the following function is +added to any module (including but not limited to the ``cats`` and ``dogs`` +modules above) it will be callable with either a ``dogs.Pet`` or ``cats.Pet`` +argument: + +.. code-block:: cpp + + m.def("pet_name", [](const pets::Pet &pet) { return pet.name(); }); + +For example, suppose the above function is added to each of ``cats.cpp``, +``dogs.cpp`` and ``frogs.cpp`` (where ``frogs.cpp`` is some other module that +does *not* bind ``Pets`` at all). + +.. code-block:: pycon + + >>> import cats, dogs, frogs # No error because of the added py::module_local() + >>> mycat, mydog = cats.Cat("Fluffy"), dogs.Dog("Rover") + >>> (cats.pet_name(mycat), dogs.pet_name(mydog)) + ('Fluffy', 'Rover') + >>> (cats.pet_name(mydog), dogs.pet_name(mycat), frogs.pet_name(mycat)) + ('Rover', 'Fluffy', 'Fluffy') + +It is possible to use ``py::module_local()`` registrations in one module even +if another module registers the same type globally: within the module with the +module-local definition, all C++ instances will be cast to the associated bound +Python type. In other modules any such values are converted to the global +Python type created elsewhere. + +.. note:: + + STL bindings (as provided via the optional :file:`pybind11/stl_bind.h` + header) apply ``py::module_local`` by default when the bound type might + conflict with other modules; see :ref:`stl_bind` for details. + +.. note:: + + The localization of the bound types is actually tied to the shared object + or binary generated by the compiler/linker. For typical modules created + with ``PYBIND11_MODULE()``, this distinction is not significant. It is + possible, however, when :ref:`embedding` to embed multiple modules in the + same binary (see :ref:`embedding_modules`). In such a case, the + localization will apply across all embedded modules within the same binary. + +.. seealso:: + + The file :file:`tests/test_local_bindings.cpp` contains additional examples + that demonstrate how ``py::module_local()`` works. + +Binding protected member functions +================================== + +It's normally not possible to expose ``protected`` member functions to Python: + +.. code-block:: cpp + + class A { + protected: + int foo() const { return 42; } + }; + + py::class_(m, "A") + .def("foo", &A::foo); // error: 'foo' is a protected member of 'A' + +On one hand, this is good because non-``public`` members aren't meant to be +accessed from the outside. But we may want to make use of ``protected`` +functions in derived Python classes. + +The following pattern makes this possible: + +.. code-block:: cpp + + class A { + protected: + int foo() const { return 42; } + }; + + class Publicist : public A { // helper type for exposing protected functions + public: + using A::foo; // inherited with different access modifier + }; + + py::class_(m, "A") // bind the primary class + .def("foo", &Publicist::foo); // expose protected methods via the publicist + +This works because ``&Publicist::foo`` is exactly the same function as +``&A::foo`` (same signature and address), just with a different access +modifier. The only purpose of the ``Publicist`` helper class is to make +the function name ``public``. + +If the intent is to expose ``protected`` ``virtual`` functions which can be +overridden in Python, the publicist pattern can be combined with the previously +described trampoline: + +.. code-block:: cpp + + class A { + public: + virtual ~A() = default; + + protected: + virtual int foo() const { return 42; } + }; + + class Trampoline : public A, public py::trampoline_self_life_support { + public: + int foo() const override { PYBIND11_OVERRIDE(int, A, foo, ); } + }; + + class Publicist : public A { + public: + using A::foo; + }; + + py::class_(m, "A") // <-- `Trampoline` here + .def("foo", &Publicist::foo); // <-- `Publicist` here, not `Trampoline`! + +Binding final classes +===================== + +Some classes may not be appropriate to inherit from. In C++11, classes can +use the ``final`` specifier to ensure that a class cannot be inherited from. +The ``py::is_final`` attribute can be used to ensure that Python classes +cannot inherit from a specified type. The underlying C++ type does not need +to be declared final. + +.. code-block:: cpp + + class IsFinal final {}; + + py::class_(m, "IsFinal", py::is_final()); + +When you try to inherit from such a class in Python, you will now get this +error: + +.. code-block:: pycon + + >>> class PyFinalChild(IsFinal): + ... pass + ... + TypeError: type 'IsFinal' is not an acceptable base type + +.. note:: This attribute is currently ignored on PyPy + +.. versionadded:: 2.6 + +Binding classes with template parameters +======================================== + +pybind11 can also wrap classes that have template parameters. Consider these classes: + +.. code-block:: cpp + + struct Cat {}; + struct Dog {}; + + template + struct Cage { + Cage(PetType& pet); + PetType& get(); + }; + +C++ templates may only be instantiated at compile time, so pybind11 can only +wrap instantiated templated classes. You cannot wrap a non-instantiated template: + +.. code-block:: cpp + + // BROKEN (this will not compile) + py::class_(m, "Cage"); + .def("get", &Cage::get); + +You must explicitly specify each template/type combination that you want to +wrap separately. + +.. code-block:: cpp + + // ok + py::class_>(m, "CatCage") + .def("get", &Cage::get); + + // ok + py::class_>(m, "DogCage") + .def("get", &Cage::get); + +If your class methods have template parameters you can wrap those as well, +but once again each instantiation must be explicitly specified: + +.. code-block:: cpp + + typename + struct MyClass { + template + T fn(V v); + }; + + py::class_>(m, "MyClassT") + .def("fn", &MyClass::fn); + +Custom automatic downcasters +============================ + +As explained in :ref:`inheritance`, pybind11 comes with built-in +understanding of the dynamic type of polymorphic objects in C++; that +is, returning a Pet to Python produces a Python object that knows it's +wrapping a Dog, if Pet has virtual methods and pybind11 knows about +Dog and this Pet is in fact a Dog. Sometimes, you might want to +provide this automatic downcasting behavior when creating bindings for +a class hierarchy that does not use standard C++ polymorphism, such as +LLVM [#f4]_. As long as there's some way to determine at runtime +whether a downcast is safe, you can proceed by specializing the +``pybind11::polymorphic_type_hook`` template: + +.. code-block:: cpp + + enum class PetKind { Cat, Dog, Zebra }; + struct Pet { // Not polymorphic: has no virtual methods + const PetKind kind; + int age = 0; + protected: + Pet(PetKind _kind) : kind(_kind) {} + }; + struct Dog : Pet { + Dog() : Pet(PetKind::Dog) {} + std::string sound = "woof!"; + std::string bark() const { return sound; } + }; + + namespace PYBIND11_NAMESPACE { + template<> struct polymorphic_type_hook { + static const void *get(const Pet *src, const std::type_info*& type) { + // note that src may be nullptr + if (src && src->kind == PetKind::Dog) { + type = &typeid(Dog); + return static_cast(src); + } + return src; + } + }; + } // namespace PYBIND11_NAMESPACE + +When pybind11 wants to convert a C++ pointer of type ``Base*`` to a +Python object, it calls ``polymorphic_type_hook::get()`` to +determine if a downcast is possible. The ``get()`` function should use +whatever runtime information is available to determine if its ``src`` +parameter is in fact an instance of some class ``Derived`` that +inherits from ``Base``. If it finds such a ``Derived``, it sets ``type += &typeid(Derived)`` and returns a pointer to the ``Derived`` object +that contains ``src``. Otherwise, it just returns ``src``, leaving +``type`` at its default value of nullptr. If you set ``type`` to a +type that pybind11 doesn't know about, no downcasting will occur, and +the original ``src`` pointer will be used with its static type +``Base*``. + +It is critical that the returned pointer and ``type`` argument of +``get()`` agree with each other: if ``type`` is set to something +non-null, the returned pointer must point to the start of an object +whose type is ``type``. If the hierarchy being exposed uses only +single inheritance, a simple ``return src;`` will achieve this just +fine, but in the general case, you must cast ``src`` to the +appropriate derived-class pointer (e.g. using +``static_cast(src)``) before allowing it to be returned as a +``void*``. + +.. [#f4] https://llvm.org/docs/HowToSetUpLLVMStyleRTTI.html + +.. note:: + + pybind11's standard support for downcasting objects whose types + have virtual methods is implemented using + ``polymorphic_type_hook`` too, using the standard C++ ability to + determine the most-derived type of a polymorphic object using + ``typeid()`` and to cast a base pointer to that most-derived type + (even if you don't know what it is) using ``dynamic_cast``. + +.. seealso:: + + The file :file:`tests/test_tagbased_polymorphic.cpp` contains a + more complete example, including a demonstration of how to provide + automatic downcasting for an entire class hierarchy without + writing one get() function for each class. + +Accessing the type object +========================= + +You can get the type object from a C++ class that has already been registered using: + +.. code-block:: cpp + + py::type T_py = py::type::of(); + +You can directly use ``py::type::of(ob)`` to get the type object from any python +object, just like ``type(ob)`` in Python. + +.. note:: + + Other types, like ``py::type::of()``, do not work, see :ref:`type-conversions`. + +.. versionadded:: 2.6 + +Custom type setup +================= + +For advanced use cases, such as enabling garbage collection support, you may +wish to directly manipulate the ``PyHeapTypeObject`` corresponding to a +``py::class_`` definition. + +You can do that using ``py::custom_type_setup``: + +.. code-block:: cpp + + struct ContainerOwnsPythonObjects { + std::vector list; + + void append(const py::object &obj) { list.emplace_back(obj); } + py::object at(py::ssize_t index) const { + if (index >= size() || index < 0) { + throw py::index_error("Index out of range"); + } + return list.at(py::size_t(index)); + } + py::ssize_t size() const { return py::ssize_t_cast(list.size()); } + void clear() { list.clear(); } + }; + + py::class_ cls( + m, "ContainerOwnsPythonObjects", py::custom_type_setup([](PyHeapTypeObject *heap_type) { + auto *type = &heap_type->ht_type; + type->tp_flags |= Py_TPFLAGS_HAVE_GC; + type->tp_traverse = [](PyObject *self_base, visitproc visit, void *arg) { + // https://docs.python.org/3/c-api/typeobj.html#c.PyTypeObject.tp_traverse + #if PY_VERSION_HEX >= 0x03090000 + Py_VISIT(Py_TYPE(self_base)); + #endif + if (py::detail::is_holder_constructed(self_base)) { + auto &self = py::cast(py::handle(self_base)); + for (auto &item : self.list) { + Py_VISIT(item.ptr()); + } + } + return 0; + }; + type->tp_clear = [](PyObject *self_base) { + if (py::detail::is_holder_constructed(self_base)) { + auto &self = py::cast(py::handle(self_base)); + for (auto &item : self.list) { + Py_CLEAR(item.ptr()); + } + self.list.clear(); + } + return 0; + }; + })); + cls.def(py::init<>()); + cls.def("append", &ContainerOwnsPythonObjects::append); + cls.def("at", &ContainerOwnsPythonObjects::at); + cls.def("size", &ContainerOwnsPythonObjects::size); + cls.def("clear", &ContainerOwnsPythonObjects::clear); + +.. versionadded:: 2.8 diff --git a/external_libraries/pybind11/docs/advanced/deadlock.md b/external_libraries/pybind11/docs/advanced/deadlock.md new file mode 100644 index 00000000..f1bab5bd --- /dev/null +++ b/external_libraries/pybind11/docs/advanced/deadlock.md @@ -0,0 +1,391 @@ +# Double locking, deadlocking, GIL + +[TOC] + +## Introduction + +### Overview + +In concurrent programming with locks, *deadlocks* can arise when more than one +mutex is locked at the same time, and careful attention has to be paid to lock +ordering to avoid this. Here we will look at a common situation that occurs in +native extensions for CPython written in C++. + +### Deadlocks + +A deadlock can occur when more than one thread attempts to lock more than one +mutex, and two of the threads lock two of the mutexes in different orders. For +example, consider mutexes `mu1` and `mu2`, and threads T1 and T2, executing: + +| | T1 | T2 | +|--- | ------------------- | -------------------| +|1 | `mu1.lock()`{.good} | `mu2.lock()`{.good}| +|2 | `mu2.lock()`{.bad} | `mu1.lock()`{.bad} | +|3 | `/* work */` | `/* work */` | +|4 | `mu2.unlock()` | `mu1.unlock()` | +|5 | `mu1.unlock()` | `mu2.unlock()` | + +Now if T1 manages to lock `mu1` and T2 manages to lock `mu2` (as indicated in +green), then both threads will block while trying to lock the respective other +mutex (as indicated in red), but they are also unable to release the mutex that +they have locked (step 5). + +**The problem** is that it is possible for one thread to attempt to lock `mu1` +and then `mu2`, and for another thread to attempt to lock `mu2` and then `mu1`. +Note that it does not matter if either mutex is unlocked at any intermediate +point; what matters is only the order of any attempt to *lock* the mutexes. For +example, the following, more complex series of operations is just as prone to +deadlock: + +| | T1 | T2 | +|--- | ------------------- | -------------------| +|1 | `mu1.lock()`{.good} | `mu1.lock()`{.good}| +|2 | waiting for T2 | `mu2.lock()`{.good}| +|3 | waiting for T2 | `/* work */` | +|3 | waiting for T2 | `mu1.unlock()` | +|3 | `mu2.lock()`{.bad} | `/* work */` | +|3 | `/* work */` | `mu1.lock()`{.bad} | +|3 | `/* work */` | `/* work */` | +|4 | `mu2.unlock()` | `mu1.unlock()` | +|5 | `mu1.unlock()` | `mu2.unlock()` | + +When the mutexes involved in a locking sequence are known at compile-time, then +avoiding deadlocks is “merely” a matter of arranging the lock +operations carefully so as to only occur in one single, fixed order. However, it +is also possible for mutexes to only be determined at runtime. A typical example +of this is a database where each row has its own mutex. An operation that +modifies two rows in a single transaction (e.g. “transferring an amount +from one account to another”) must lock two row mutexes, but the locking +order cannot be established at compile time. In this case, a dynamic +“deadlock avoidance algorithm” is needed. (In C++, `std::lock` +provides such an algorithm. An algorithm might use a non-blocking `try_lock` +operation on a mutex, which can either succeed or fail to lock the mutex, but +returns without blocking.) + +Conceptually, one could also consider it a deadlock if _the same_ thread +attempts to lock a mutex that it has already locked (e.g. when some locked +operation accidentally recurses into itself): `mu.lock();`{.good} +`mu.lock();`{.bad} However, this is a slightly separate issue: Typical mutexes +are either of _recursive_ or _non-recursive_ kind. A recursive mutex allows +repeated locking and requires balanced unlocking. A non-recursive mutex can be +implemented more efficiently, and/but for efficiency reasons does not actually +guarantee a deadlock on second lock. Instead, the API simply forbids such use, +making it a precondition that the thread not already hold the mutex, with +undefined behaviour on violation. + +### “Once” initialization + +A common programming problem is to have an operation happen precisely once, even +if requested concurrently. While it is clear that we need to track in some +shared state somewhere whether the operation has already happened, it is worth +noting that this state only ever transitions, once, from `false` to `true`. This +is considerably simpler than a general shared state that can change values +arbitrarily. Next, we also need a mechanism for all but one thread to block +until the initialization has completed, which we can provide with a mutex. The +simplest solution just always locks the mutex: + +```c++ +// The "once" mechanism: +constinit absl::Mutex mu(absl::kConstInit); +constinit bool init_done = false; + +// The operation of interest: +void f(); + +void InitOnceNaive() { + absl::MutexLock lock(&mu); + if (!init_done) { + f(); + init_done = true; + } +} +``` + +This works, but the efficiency-minded reader will observe that once the +operation has completed, all future lock contention on the mutex is +unnecessary. This leads to the (in)famous “double-locking” +algorithm, which was historically hard to write correctly. The idea is to check +the boolean *before* locking the mutex, and avoid locking if the operation has +already completed. However, accessing shared state concurrently when at least +one access is a write is prone to causing a data race and needs to be done +according to an appropriate concurrent programming model. In C++ we use atomic +variables: + +```c++ +// The "once" mechanism: +constinit absl::Mutex mu(absl::kConstInit); +constinit std::atomic init_done = false; + +// The operation of interest: +void f(); + +void InitOnceWithFastPath() { + if (!init_done.load(std::memory_order_acquire)) { + absl::MutexLock lock(&mu); + if (!init_done.load(std::memory_order_relaxed)) { + f(); + init_done.store(true, std::memory_order_release); + } + } +} +``` + +Checking the flag now happens without holding the mutex lock, and if the +operation has already completed, we return immediately. After locking the mutex, +we need to check the flag again, since multiple threads can reach this point. + +*Atomic details.* Since the atomic flag variable is accessed concurrently, we +have to think about the memory order of the accesses. There are two separate +cases: The first, outer check outside the mutex lock, and the second, inner +check under the lock. The outer check and the flag update form an +acquire/release pair: *if* the load sees the value `true` (which must have been +written by the store operation), then it also sees everything that happened +before the store, namely the operation `f()`. By contrast, the inner check can +use relaxed memory ordering, since in that case the mutex operations provide the +necessary ordering: if the inner load sees the value `true`, it happened after +the `lock()`, which happened after the `unlock()`, which happened after the +store. + +The C++ standard library, and Abseil, provide a ready-made solution of this +algorithm called `std::call_once`/`absl::call_once`. (The interface is the same, +but the Abseil implementation is possibly better.) + +```c++ +// The "once" mechanism: +constinit absl::once_flag init_flag; + +// The operation of interest: +void f(); + +void InitOnceWithCallOnce() { + absl::call_once(once_flag, f); +} +``` + +Even though conceptually this is performing the same algorithm, this +implementation has some considerable advantages: The `once_flag` type is a small +and trivial, integer-like type and is trivially destructible. Not only does it +take up less space than a mutex, it also generates less code since it does not +have to run a destructor, which would need to be added to the program's global +destructor list. + +The final clou comes with the C++ semantics of a `static` variable declared at +block scope: According to [[stmt.dcl]](https://eel.is/c++draft/stmt.dcl#3): + +> Dynamic initialization of a block variable with static storage duration or +> thread storage duration is performed the first time control passes through its +> declaration; such a variable is considered initialized upon the completion of +> its initialization. [...] If control enters the declaration concurrently while +> the variable is being initialized, the concurrent execution shall wait for +> completion of the initialization. + +This is saying that the initialization of a local, `static` variable precisely +has the “once” semantics that we have been discussing. We can +therefore write the above example as follows: + +```c++ +// The operation of interest: +void f(); + +void InitOnceWithStatic() { + static int unused = (f(), 0); +} +``` + +This approach is by far the simplest and easiest, but the big difference is that +the mutex (or mutex-like object) in this implementation is no longer visible or +in the user’s control. This is perfectly fine if the initializer is +simple, but if the initializer itself attempts to lock any other mutex +(including by initializing another static variable!), then we have no control +over the lock ordering! + +Finally, you may have noticed the `constinit`s around the earlier code. Both +`constinit` and `constexpr` specifiers on a declaration mean that the variable +is *constant-initialized*, which means that no initialization is performed at +runtime (the initial value is already known at compile time). This in turn means +that a static variable guard mutex may not be needed, and static initialization +never blocks. The difference between the two is that a `constexpr`-specified +variable is also `const`, and a variable cannot be `constexpr` if it has a +non-trivial destructor. Such a destructor also means that the guard mutex is +needed after all, since the destructor must be registered to run at exit, +conditionally on initialization having happened. + +## Python, CPython, GIL + +With CPython, a Python program can call into native code. To this end, the +native code registers callback functions with the Python runtime via the CPython +API. In order to ensure that the internal state of the Python runtime remains +consistent, there is a single, shared mutex called the “global interpreter +lock”, or GIL for short. Upon entry of one of the user-provided callback +functions, the GIL is locked (or “held”), so that no other mutations +of the Python runtime state can occur until the native callback returns. + +Many native extensions do not interact with the Python runtime for at least some +part of them, and so it is common for native extensions to _release_ the GIL, do +some work, and then reacquire the GIL before returning. Similarly, when code is +generally not holding the GIL but needs to interact with the runtime briefly, it +will first reacquire the GIL. The GIL is reentrant, and constructions to acquire +and subsequently release the GIL are common, and often don't worry about whether +the GIL is already held. + +If the native code is written in C++ and contains local, `static` variables, +then we are now dealing with at least _two_ mutexes: the static variable guard +mutex, and the GIL from CPython. + +A common problem in such code is an operation with “only once” +semantics that also ends up requiring the GIL to be held at some point. As per +the above description of “once”-style techniques, one might find a +static variable: + +```c++ +// CPython callback, assumes that the GIL is held on entry. +PyObject* InvokeWidget(PyObject* self) { + static PyObject* impl = CreateWidget(); + return PyObject_CallOneArg(impl, self); +} +``` + +This seems reasonable, but bear in mind that there are two mutexes (the "guard +mutex" and "the GIL"), and we must think about the lock order. Otherwise, if the +callback is called from multiple threads, a deadlock may ensue. + +Let us consider what we can see here: On entry, the GIL is already locked, and +we are locking the guard mutex. This is one lock order. Inside the initializer +`CreateWidget`, with both mutexes already locked, the function can freely access +the Python runtime. + +However, it is entirely possible that `CreateWidget` will want to release the +GIL at one point and reacquire it later: + +```c++ +// Assumes that the GIL is held on entry. +// Ensures that the GIL is held on exit. +PyObject* CreateWidget() { + // ... + Py_BEGIN_ALLOW_THREADS // releases GIL + // expensive work, not accessing the Python runtime + Py_END_ALLOW_THREADS // acquires GIL, #! + // ... + return result; +} +``` + +Now we have a second lock order: the guard mutex is locked, and then the GIL is +locked (at `#!`). To see how this deadlocks, consider threads T1 and T2 both +having the runtime attempt to call `InvokeWidget`. T1 locks the GIL and +proceeds, locking the guard mutex and calling `CreateWidget`; T2 is blocked +waiting for the GIL. Then T1 releases the GIL to do “expensive +work”, and T2 awakes and locks the GIL. Now T2 is blocked trying to +acquire the guard mutex, but T1 is blocked reacquiring the GIL (at `#!`). + +In other words: if we want to support “once-called” functions that +can arbitrarily release and reacquire the GIL, as is very common, then the only +lock order that we can ensure is: guard mutex first, GIL second. + +To implement this, we must rewrite our code. Naively, we could always release +the GIL before a `static` variable with blocking initializer: + +```c++ +// CPython callback, assumes that the GIL is held on entry. +PyObject* InvokeWidget(PyObject* self) { + Py_BEGIN_ALLOW_THREADS // releases GIL + static PyObject* impl = CreateWidget(); + Py_END_ALLOW_THREADS // acquires GIL + + return PyObject_CallOneArg(impl, self); +} +``` + +But similar to the `InitOnceNaive` example above, this code cycles the GIL +(possibly descheduling the thread) even when the static variable has already +been initialized. If we want to avoid this, we need to abandon the use of a +static variable, since we do not control the guard mutex well enough. Instead, +we use an operation whose mutex locking is under our control, such as +`call_once`. For example: + +```c++ +// CPython callback, assumes that the GIL is held on entry. +PyObject* InvokeWidget(PyObject* self) { + static constinit PyObject* impl = nullptr; + static constinit std::atomic init_done = false; + static constinit absl::once_flag init_flag; + + if (!init_done.load(std::memory_order_acquire)) { + Py_BEGIN_ALLOW_THREADS // releases GIL + absl::call_once(init_flag, [&]() { + PyGILState_STATE s = PyGILState_Ensure(); // acquires GIL + impl = CreateWidget(); + PyGILState_Release(s); // releases GIL + init_done.store(true, std::memory_order_release); + }); + Py_END_ALLOW_THREADS // acquires GIL + } + + return PyObject_CallOneArg(impl, self); +} +``` + +The lock order is now always guard mutex first, GIL second. Unfortunately we +have to duplicate the “double-checked done flag”, effectively +leading to triple checking, because the flag state inside the `absl::once_flag` +is not accessible to the user. In other words, we cannot ask `init_flag` whether +it has been used yet. + +However, we can perform one last, minor optimisation: since we assume that the +GIL is held on entry, and again when the initializing operation returns, the GIL +actually serializes access to our done flag variable, which therefore does not +need to be atomic. (The difference to the previous, atomic code may be small, +depending on the architecture. For example, on x86-64, acquire/release on a bool +is nearly free ([demo](https://godbolt.org/z/P9vYWf4fE)).) + +```c++ +// CPython callback, assumes that the GIL is held on entry, and indeed anywhere +// directly in this function (i.e. the GIL can be released inside CreateWidget, +// but must be reaqcuired when that call returns). +PyObject* InvokeWidget(PyObject* self) { + static constinit PyObject* impl = nullptr; + static constinit bool init_done = false; // guarded by GIL + static constinit absl::once_flag init_flag; + + if (!init_done) { + Py_BEGIN_ALLOW_THREADS // releases GIL + // (multiple threads may enter here) + absl::call_once(init_flag, [&]() { + // (only one thread enters here) + PyGILState_STATE s = PyGILState_Ensure(); // acquires GIL + impl = CreateWidget(); + init_done = true; // (GIL is held) + PyGILState_Release(s); // releases GIL + }); + + Py_END_ALLOW_THREADS // acquires GIL + } + + return PyObject_CallOneArg(impl, self); +} +``` + +## Debugging tips + +* Build with symbols. +* Ctrl-C sends `SIGINT`, Ctrl-\\ + sends `SIGQUIT`. Both have their uses. +* Useful `gdb` commands: + * `py-bt` prints a Python backtrace if you are in a Python frame. + * `thread apply all bt 10` prints the top-10 frames for each thread. A + full backtrace can be prohibitively expensive, and the top few frames + are often good enough. + * `p PyGILState_Check()` shows whether a thread is holding the GIL. For + all threads, run `thread apply all p PyGILState_Check()` to find out + which thread is holding the GIL. + * The `static` variable guard mutex is accessed with functions like + `cxa_guard_acquire` (though this depends on ABI details and can vary). + The guard mutex itself contains information about which thread is + currently holding it. + +## Links + +* Article on + [double-checked locking](https://preshing.com/20130930/double-checked-locking-is-fixed-in-cpp11/) +* [The Deadlock Empire](https://deadlockempire.github.io/), hands-on exercises + to construct deadlocks diff --git a/external_libraries/pybind11/docs/advanced/deprecated.rst b/external_libraries/pybind11/docs/advanced/deprecated.rst new file mode 100644 index 00000000..dc07a77b --- /dev/null +++ b/external_libraries/pybind11/docs/advanced/deprecated.rst @@ -0,0 +1,179 @@ +.. _deprecated: + +Deprecated +########## + +Support for Python 3.8 is deprecated and will be removed in 3.1. + +Support for C++11 is deprecated and will be removed in a future version. Please +use at least C++14. + +Support for FindPythonLibs (not available in CMake 3.26+ mode) is deprecated +and will be removed in a future version. The default mode is also going to +change to ``"new"`` from ``"compat"`` in the future. + +The following features were deprecated before pybind11 3.0, and may be removed +in minor releases of pybind11 3.x. + +.. list-table:: Deprecated Features + :header-rows: 1 + :widths: 30 15 10 + + * - Feature + - Deprecated Version + - Year + * - ``py::metaclass()`` + - 2.1 + - 2017 + * - ``PYBIND11_PLUGIN`` + - 2.2 + - 2017 + * - ``py::set_error()`` replacing ``operator()`` + - 2.12 + - 2024 + * - ``get_type_overload`` + - 2.6 + - 2020 + * - ``call()`` + - 2.0 + - 2016 + * - ``.str()`` + - ? + - + * - ``.get_type()`` + - 2.6 + - + * - ``==`` and ``!=`` + - 2.2 + - 2017 + * - ``.check()`` + - ? + - + * - ``object(handle, bool)`` + - ? + - + * - ``error_already_set.clear()`` + - 2.2 + - 2017 + * - ``obj.attr(…)`` as ``bool`` + - ? + - + * - ``.contains`` + - ? (maybe 2.4) + - + * - ``py::capsule`` two-argument with destructor + - ? + - + + + +.. _deprecated_enum: + +``py::enum_`` +============= + +This is the original documentation for ``py::enum_``, which is deprecated +because it is not `PEP 435 compatible `_ +(see also `#2332 `_). +Please prefer ``py::native_enum`` (added with pybind11v3) when writing +new bindings. See :ref:`native_enum` for more information. + +Let's suppose that we have an example class that contains internal types +like enumerations, e.g.: + +.. code-block:: cpp + + struct Pet { + enum Kind { + Dog = 0, + Cat + }; + + struct Attributes { + float age = 0; + }; + + Pet(const std::string &name, Kind type) : name(name), type(type) { } + + std::string name; + Kind type; + Attributes attr; + }; + +The binding code for this example looks as follows: + +.. code-block:: cpp + + py::class_ pet(m, "Pet"); + + pet.def(py::init()) + .def_readwrite("name", &Pet::name) + .def_readwrite("type", &Pet::type) + .def_readwrite("attr", &Pet::attr); + + py::enum_(pet, "Kind") + .value("Dog", Pet::Kind::Dog) + .value("Cat", Pet::Kind::Cat) + .export_values(); + + py::class_(pet, "Attributes") + .def(py::init<>()) + .def_readwrite("age", &Pet::Attributes::age); + + +To ensure that the nested types ``Kind`` and ``Attributes`` are created within the scope of ``Pet``, the +``pet`` ``py::class_`` instance must be supplied to the :class:`enum_` and ``py::class_`` +constructor. The :func:`enum_::export_values` function exports the enum entries +into the parent scope, which should be skipped for newer C++11-style strongly +typed enums. + +.. code-block:: pycon + + >>> p = Pet("Lucy", Pet.Cat) + >>> p.type + Kind.Cat + >>> int(p.type) + 1L + +The entries defined by the enumeration type are exposed in the ``__members__`` property: + +.. code-block:: pycon + + >>> Pet.Kind.__members__ + {'Dog': Kind.Dog, 'Cat': Kind.Cat} + +The ``name`` property returns the name of the enum value as a unicode string. + +.. note:: + + It is also possible to use ``str(enum)``, however these accomplish different + goals. The following shows how these two approaches differ. + + .. code-block:: pycon + + >>> p = Pet("Lucy", Pet.Cat) + >>> pet_type = p.type + >>> pet_type + Pet.Cat + >>> str(pet_type) + 'Pet.Cat' + >>> pet_type.name + 'Cat' + +.. note:: + + When the special tag ``py::arithmetic()`` is specified to the ``enum_`` + constructor, pybind11 creates an enumeration that also supports rudimentary + arithmetic and bit-level operations like comparisons, and, or, xor, negation, + etc. + + .. code-block:: cpp + + py::enum_(pet, "Kind", py::arithmetic()) + ... + + By default, these are omitted to conserve space. + +.. warning:: + + Contrary to Python customs, enum values from the wrappers should not be compared using ``is``, but with ``==`` (see `#1177 `_ for background). diff --git a/external_libraries/pybind11/docs/advanced/embedding.rst b/external_libraries/pybind11/docs/advanced/embedding.rst new file mode 100644 index 00000000..c41aec15 --- /dev/null +++ b/external_libraries/pybind11/docs/advanced/embedding.rst @@ -0,0 +1,499 @@ +.. _embedding: + +Embedding the interpreter +######################### + +While pybind11 is mainly focused on extending Python using C++, it's also +possible to do the reverse: embed the Python interpreter into a C++ program. +All of the other documentation pages still apply here, so refer to them for +general pybind11 usage. This section will cover a few extra things required +for embedding. + +Getting started +=============== + +A basic executable with an embedded interpreter can be created with just a few +lines of CMake and the ``pybind11::embed`` target, as shown below. For more +information, see :doc:`/compiling`. + +.. code-block:: cmake + + cmake_minimum_required(VERSION 3.15...4.2) + project(example) + + find_package(pybind11 REQUIRED) # or `add_subdirectory(pybind11)` + + add_executable(example main.cpp) + target_link_libraries(example PRIVATE pybind11::embed) + +The essential structure of the ``main.cpp`` file looks like this: + +.. code-block:: cpp + + #include // everything needed for embedding + namespace py = pybind11; + + int main() { + py::scoped_interpreter guard{}; // start the interpreter and keep it alive + + py::print("Hello, World!"); // use the Python API + } + +The interpreter must be initialized before using any Python API, which includes +all the functions and classes in pybind11. The RAII guard class ``scoped_interpreter`` +takes care of the interpreter lifetime. After the guard is destroyed, the interpreter +shuts down and clears its memory. No Python functions can be called after this. + +Executing Python code +===================== + +There are a few different ways to run Python code. One option is to use ``eval``, +``exec`` or ``eval_file``, as explained in :ref:`eval`. Here is a quick example in +the context of an executable with an embedded interpreter: + +.. code-block:: cpp + + #include + namespace py = pybind11; + + int main() { + py::scoped_interpreter guard{}; + + py::exec(R"( + kwargs = dict(name="World", number=42) + message = "Hello, {name}! The answer is {number}".format(**kwargs) + print(message) + )"); + } + +Alternatively, similar results can be achieved using pybind11's API (see +:doc:`/advanced/pycpp/index` for more details). + +.. code-block:: cpp + + #include + namespace py = pybind11; + using namespace py::literals; + + int main() { + py::scoped_interpreter guard{}; + + auto kwargs = py::dict("name"_a="World", "number"_a=42); + auto message = "Hello, {name}! The answer is {number}"_s.format(**kwargs); + py::print(message); + } + +The two approaches can also be combined: + +.. code-block:: cpp + + #include + #include + + namespace py = pybind11; + using namespace py::literals; + + int main() { + py::scoped_interpreter guard{}; + + auto locals = py::dict("name"_a="World", "number"_a=42); + py::exec(R"( + message = "Hello, {name}! The answer is {number}".format(**locals()) + )", py::globals(), locals); + + auto message = locals["message"].cast(); + std::cout << message; + } + +Importing modules +================= + +Python modules can be imported using ``module_::import()``: + +.. code-block:: cpp + + py::module_ sys = py::module_::import("sys"); + py::print(sys.attr("path")); + +For convenience, the current working directory is included in ``sys.path`` when +embedding the interpreter. This makes it easy to import local Python files: + +.. code-block:: python + + """calc.py located in the working directory""" + + + def add(i, j): + return i + j + + +.. code-block:: cpp + + py::module_ calc = py::module_::import("calc"); + py::object result = calc.attr("add")(1, 2); + int n = result.cast(); + assert(n == 3); + +Modules can be reloaded using ``module_::reload()`` if the source is modified e.g. +by an external process. This can be useful in scenarios where the application +imports a user defined data processing script which needs to be updated after +changes by the user. Note that this function does not reload modules recursively. + +.. _embedding_modules: + +Adding embedded modules +======================= + +Embedded binary modules can be added using the ``PYBIND11_EMBEDDED_MODULE`` macro. +Note that the definition must be placed at global scope. They can be imported +like any other module. + +.. code-block:: cpp + + #include + namespace py = pybind11; + + PYBIND11_EMBEDDED_MODULE(fast_calc, m) { + // `m` is a `py::module_` which is used to bind functions and classes + m.def("add", [](int i, int j) { + return i + j; + }); + } + + int main() { + py::scoped_interpreter guard{}; + + auto fast_calc = py::module_::import("fast_calc"); + auto result = fast_calc.attr("add")(1, 2).cast(); + assert(result == 3); + } + +Unlike extension modules where only a single binary module can be created, on +the embedded side an unlimited number of modules can be added using multiple +``PYBIND11_EMBEDDED_MODULE`` definitions (as long as they have unique names). + +These modules are added to Python's list of builtins, so they can also be +imported in pure Python files loaded by the interpreter. Everything interacts +naturally: + +.. code-block:: python + + """py_module.py located in the working directory""" + import cpp_module + + a = cpp_module.a + b = a + 1 + + +.. code-block:: cpp + + #include + namespace py = pybind11; + + PYBIND11_EMBEDDED_MODULE(cpp_module, m) { + m.attr("a") = 1; + } + + int main() { + py::scoped_interpreter guard{}; + + auto py_module = py::module_::import("py_module"); + + auto locals = py::dict("fmt"_a="{} + {} = {}", **py_module.attr("__dict__")); + assert(locals["a"].cast() == 1); + assert(locals["b"].cast() == 2); + + py::exec(R"( + c = a + b + message = fmt.format(a, b, c) + )", py::globals(), locals); + + assert(locals["c"].cast() == 3); + assert(locals["message"].cast() == "1 + 2 = 3"); + } + +``PYBIND11_EMBEDDED_MODULE`` also accepts +:func:`py::mod_gil_not_used()`, +:func:`py::multiple_interpreters::per_interpreter_gil()`, and +:func:`py::multiple_interpreters::shared_gil()` tags just like ``PYBIND11_MODULE``. +See :ref:`misc_subinterp` and :ref:`misc_free_threading` for more information. + +Interpreter lifetime +==================== + +The Python interpreter shuts down when ``scoped_interpreter`` is destroyed. After +this, creating a new instance will restart the interpreter. Alternatively, the +``initialize_interpreter`` / ``finalize_interpreter`` pair of functions can be used +to directly set the state at any time. + +Modules created with pybind11 can be safely re-initialized after the interpreter +has been restarted. However, this may not apply to third-party extension modules. +The issue is that Python itself cannot completely unload extension modules and +there are several caveats with regard to interpreter restarting. In short, not +all memory may be freed, either due to Python reference cycles or user-created +global data. All the details can be found in the CPython documentation. + +.. warning:: + + Creating two concurrent ``scoped_interpreter`` guards is a fatal error. So is + calling ``initialize_interpreter`` for a second time after the interpreter + has already been initialized. Use :class:`scoped_subinterpreter` to create + a sub-interpreter. See :ref:`subinterp` for important details on sub-interpreters. + + Do not use the raw CPython API functions ``Py_Initialize`` and + ``Py_Finalize`` as these do not properly handle the lifetime of + pybind11's internal data. + + +.. _subinterp: + +Embedding Sub-interpreters +========================== + +A sub-interpreter is a separate interpreter instance which provides a +separate, isolated interpreter environment within the same process as the main +interpreter. Sub-interpreters are created and managed with a separate API from +the main interpreter. Beginning in Python 3.12, sub-interpreters each have +their own Global Interpreter Lock (GIL), which means that running a +sub-interpreter in a separate thread from the main interpreter can achieve true +concurrency. + +pybind11's sub-interpreter API can be found in ``pybind11/subinterpreter.h``. + +pybind11 :class:`subinterpreter` instances can be safely moved and shared between +threads as needed. However, managing multiple threads and the lifetimes of multiple +interpreters and their GILs can be challenging. +Proceed with caution (and lots of testing)! + +The main interpreter must be initialized before creating a sub-interpreter, and +the main interpreter must outlive all sub-interpreters. Sub-interpreters are +managed through a different API than the main interpreter. + +The :class:`subinterpreter` class manages the lifetime of sub-interpreters. +Instances are movable, but not copyable. Default constructing this class does +*not* create a sub-interpreter (it creates an empty holder). To create a +sub-interpreter, call :func:`subinterpreter::create()`. + +.. warning:: + + Sub-interpreter creation acquires (and subsequently releases) the main + interpreter GIL. If another thread holds the main GIL, the function will + block until the main GIL can be acquired. + + Sub-interpreter destruction temporarily activates the sub-interpreter. The + sub-interpreter must not be active (on any threads) at the time the + :class:`subinterpreter` destructor is called. + + Both actions will re-acquire any interpreter's GIL that was held prior to + the call before returning (or return to no active interpreter if none was + active at the time of the call). + +Each sub-interpreter will import a separate copy of each ``PYBIND11_EMBEDDED_MODULE`` +when those modules specify a ``multiple_interpreters`` tag. If a module does not +specify a ``multiple_interpreters`` tag, then Python will report an ``ImportError`` +if it is imported in a sub-interpreter. + +pybind11 also has a :class:`scoped_subinterpreter` class, which creates and +activates a sub-interpreter when it is constructed, and deactivates and deletes +it when it goes out of scope. + +Activating a Sub-interpreter +^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +Once a sub-interpreter is created, you can "activate" it on a thread (and +acquire its GIL) by creating a :class:`subinterpreter_scoped_activate` +instance and passing it the sub-interpreter to be activated. The function +will acquire the sub-interpreter's GIL and make the sub-interpreter the +current active interpreter on the current thread for the lifetime of the +instance. When the :class:`subinterpreter_scoped_activate` instance goes out +of scope, the sub-interpreter GIL is released and the prior interpreter that +was active on the thread (if any) is reactivated and it's GIL is re-acquired. + +When using ``subinterpreter_scoped_activate``: + +1. If the thread holds any interpreter's GIL: + - That GIL is released +2. The new sub-interpreter's GIL is acquired +3. The new sub-interpreter is made active. +4. When the scope ends: + - The sub-interpreter's GIL is released + - If there was a previous interpreter: + - The old interpreter's GIL is re-acquired + - The old interpreter is made active + - Otherwise, no interpreter is currently active and no GIL is held. + +Example: + +.. code-block:: cpp + + py::initialize_interpreter(); + // Main GIL is held + { + py::subinterpreter sub = py::subinterpreter::create(); + // Main interpreter is still active, main GIL re-acquired + { + py::subinterpreter_scoped_activate guard(sub); + // Sub-interpreter active, thread holds sub's GIL + { + py::subinterpreter_scoped_activate main_guard(py); + // Sub's GIL was automatically released + // Main interpreter active, thread holds main's GIL + } + // Back to sub-interpreter, thread holds sub's GIL again + } + // Main interpreter is active, main's GIL is held + } + + +GIL API for sub-interpreters +^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +:class:`gil_scoped_release` and :class:`gil_scoped_acquire` can be used to +manage the GIL of a sub-interpreter just as they do for the main interpreter. +They both manage the GIL of the currently active interpreter, without the +programmer having to do anything special or different. There is one important +caveat: + +.. note:: + + When no interpreter is active through a + :class:`subinterpreter_scoped_activate` instance (such as on a new thread), + :class:`gil_scoped_acquire` will acquire the **main** GIL and + activate the **main** interpreter. + + +Full Sub-interpreter example +^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +Here is an example showing how to create and activate sub-interpreters: + +.. code-block:: cpp + + #include + #include + #include + + namespace py = pybind11; + + PYBIND11_EMBEDDED_MODULE(printer, m, py::multiple_interpreters::per_interpreter_gil()) { + m.def("which", [](const std::string& when) { + std::cout << when << "; Current Interpreter is " + << py::subinterpreter::current().id() + << std::endl; + }); + } + + int main() { + py::scoped_interpreter main_interp; + + py::module_::import("printer").attr("which")("First init"); + + { + py::subinterpreter sub = py::subinterpreter::create(); + + py::module_::import("printer").attr("which")("Created sub"); + + { + py::subinterpreter_scoped_activate guard(sub); + try { + py::module_::import("printer").attr("which")("Activated sub"); + } + catch (py::error_already_set &e) { + std::cerr << "EXCEPTION " << e.what() << std::endl; + return 1; + } + } + + py::module_::import("printer").attr("which")("Deactivated sub"); + + { + py::gil_scoped_release nogil; + { + py::subinterpreter_scoped_activate guard(sub); + try { + { + py::subinterpreter_scoped_activate main_guard(py::subinterpreter::main()); + try { + py::module_::import("printer").attr("which")("Main within sub"); + } + catch (py::error_already_set &e) { + std::cerr << "EXCEPTION " << e.what() << std::endl; + return 1; + } + } + py::module_::import("printer").attr("which")("After Main, still within sub"); + } + catch (py::error_already_set &e) { + std::cerr << "EXCEPTION " << e.what() << std::endl; + return 1; + } + } + } + } + + py::module_::import("printer").attr("which")("At end"); + + return 0; + } + +Expected output: + +.. code-block:: text + + First init; Current Interpreter is 0 + Created sub; Current Interpreter is 0 + Activated sub; Current Interpreter is 1 + Deactivated sub; Current Interpreter is 0 + Main within sub; Current Interpreter is 0 + After Main, still within sub; Current Interpreter is 1 + At end; Current Interpreter is 0 + +.. warning:: + + In Python 3.12 sub-interpreters must be destroyed in the same OS thread + that created them. Failure to follow this rule may result in deadlocks + or crashes when destroying the sub-interpreter on the wrong thread. + + This constraint is not present in Python 3.13+. + + +Best Practices for sub-interpreter safety +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +- Never share Python objects across different interpreters. + +- :class:`error_already_set` objects contain a reference to the Python exception type, + and :func:`error_already_set::what()` acquires the GIL. So Python exceptions must + **never** be allowed to propagate past the enclosing + :class:`subinterpreter_scoped_activate` instance! + (So your try/catch should be *just inside* the scope covered by the + :class:`subinterpreter_scoped_activate`.) + +- Avoid global/static state whenever possible. Instead, keep state within each interpreter, + such as within the interpreter state dict, which can be accessed via + ``subinterpreter::current().state_dict()``, or within instance members and tied to + Python objects. + +- Avoid trying to "cache" Python objects in C++ variables across function calls (this is an easy + way to accidentally introduce sub-interpreter bugs). In the code example above, note that we + did not save the result of :func:`module_::import`, in order to avoid accidentally using the + resulting Python object when the wrong interpreter was active. + +- Avoid moving or disarming RAII objects managing GIL and sub-interpreter lifetimes. Doing so can + lead to confusion about lifetimes. (For example, accidentally extending a + :class:`subinterpreter_scoped_activate` past the lifetime of it's :class:`subinterpreter`.) + +- While sub-interpreters each have their own GIL, there can now be multiple independent GILs in one + program so you need to consider the possibility of deadlocks caused by multiple GILs and/or the + interactions of the GIL(s) and your C++ code's own locking. + +- When using multiple threads to run independent sub-interpreters, the independent GILs allow + concurrent calls from different interpreters into the same C++ code from different threads. + So you must still consider the thread safety of your C++ code. Remember, in Python 3.12 + sub-interpreters must be destroyed on the same thread that they were created on. + +- When using sub-interpreters in free-threaded python builds, note that creating and destroying + sub-interpreters may initiate a "stop-the-world". Be sure to detach long-running C++ threads + from Python thread state (similar to releasing the GIL) to avoid deadlocks. + +- Familiarize yourself with :ref:`misc_concurrency`. diff --git a/external_libraries/pybind11/docs/advanced/exceptions.rst b/external_libraries/pybind11/docs/advanced/exceptions.rst new file mode 100644 index 00000000..921b4367 --- /dev/null +++ b/external_libraries/pybind11/docs/advanced/exceptions.rst @@ -0,0 +1,422 @@ +Exceptions +########## + +Built-in C++ to Python exception translation +============================================ + +When Python calls C++ code through pybind11, pybind11 provides a C++ exception handler +that will trap C++ exceptions, translate them to the corresponding Python exception, +and raise them so that Python code can handle them. + +pybind11 defines translations for ``std::exception`` and its standard +subclasses, and several special exception classes that translate to specific +Python exceptions. Note that these are not actually Python exceptions, so they +cannot be examined using the Python C API. Instead, they are pure C++ objects +that pybind11 will translate the corresponding Python exception when they arrive +at its exception handler. + +.. tabularcolumns:: |p{0.5\textwidth}|p{0.45\textwidth}| + ++--------------------------------------+--------------------------------------+ +| Exception thrown by C++ | Translated to Python exception type | ++======================================+======================================+ +| :class:`std::exception` | ``RuntimeError`` | ++--------------------------------------+--------------------------------------+ +| :class:`std::bad_alloc` | ``MemoryError`` | ++--------------------------------------+--------------------------------------+ +| :class:`std::domain_error` | ``ValueError`` | ++--------------------------------------+--------------------------------------+ +| :class:`std::invalid_argument` | ``ValueError`` | ++--------------------------------------+--------------------------------------+ +| :class:`std::length_error` | ``ValueError`` | ++--------------------------------------+--------------------------------------+ +| :class:`std::out_of_range` | ``IndexError`` | ++--------------------------------------+--------------------------------------+ +| :class:`std::range_error` | ``ValueError`` | ++--------------------------------------+--------------------------------------+ +| :class:`std::overflow_error` | ``OverflowError`` | ++--------------------------------------+--------------------------------------+ +| :class:`pybind11::stop_iteration` | ``StopIteration`` (used to implement | +| | custom iterators) | ++--------------------------------------+--------------------------------------+ +| :class:`pybind11::index_error` | ``IndexError`` (used to indicate out | +| | of bounds access in ``__getitem__``, | +| | ``__setitem__``, etc.) | ++--------------------------------------+--------------------------------------+ +| :class:`pybind11::key_error` | ``KeyError`` (used to indicate out | +| | of bounds access in ``__getitem__``, | +| | ``__setitem__`` in dict-like | +| | objects, etc.) | ++--------------------------------------+--------------------------------------+ +| :class:`pybind11::value_error` | ``ValueError`` (used to indicate | +| | wrong value passed in | +| | ``container.remove(...)``) | ++--------------------------------------+--------------------------------------+ +| :class:`pybind11::type_error` | ``TypeError`` | ++--------------------------------------+--------------------------------------+ +| :class:`pybind11::buffer_error` | ``BufferError`` | ++--------------------------------------+--------------------------------------+ +| :class:`pybind11::import_error` | ``ImportError`` | ++--------------------------------------+--------------------------------------+ +| :class:`pybind11::attribute_error` | ``AttributeError`` | ++--------------------------------------+--------------------------------------+ +| Any other exception | ``RuntimeError`` | ++--------------------------------------+--------------------------------------+ + +Exception translation is not bidirectional. That is, *catching* the C++ +exceptions defined above will not trap exceptions that originate from +Python. For that, catch :class:`pybind11::error_already_set`. See :ref:`below +` for further details. + +There is also a special exception :class:`cast_error` that is thrown by +:func:`handle::call` when the input arguments cannot be converted to Python +objects. + +Registering custom translators +============================== + +If the default exception conversion policy described above is insufficient, +pybind11 also provides support for registering custom exception translators. +Similar to pybind11 classes, exception translators can be local to the module +they are defined in or global to the entire python session. To register a simple +exception conversion that translates a C++ exception into a new Python exception +using the C++ exception's ``what()`` method, a helper function is available: + +.. code-block:: cpp + + py::register_exception(module, "PyExp"); + +This call creates a Python exception class with the name ``PyExp`` in the given +module and automatically converts any encountered exceptions of type ``CppExp`` +into Python exceptions of type ``PyExp``. + +A matching function is available for registering a local exception translator: + +.. code-block:: cpp + + py::register_local_exception(module, "PyExp"); + + +It is possible to specify base class for the exception using the third +parameter, a ``handle``: + +.. code-block:: cpp + + py::register_exception(module, "PyExp", PyExc_RuntimeError); + py::register_local_exception(module, "PyExp", PyExc_RuntimeError); + +Then ``PyExp`` can be caught both as ``PyExp`` and ``RuntimeError``. + +The class objects of the built-in Python exceptions are listed in the Python +documentation on `Standard Exceptions `_. +The default base class is ``PyExc_Exception``. + +When more advanced exception translation is needed, the functions +``py::register_exception_translator(translator)`` and +``py::register_local_exception_translator(translator)`` can be used to register +functions that can translate arbitrary exception types (and which may include +additional logic to do so). The functions takes a stateless callable (e.g. a +function pointer or a lambda function without captured variables) with the call +signature ``void(std::exception_ptr)``. + +When a C++ exception is thrown, the registered exception translators are tried +in reverse order of registration (i.e. the last registered translator gets the +first shot at handling the exception). All local translators will be tried +before a global translator is tried. + +Inside the translator, ``std::rethrow_exception`` should be used within +a try block to re-throw the exception. One or more catch clauses to catch +the appropriate exceptions should then be used with each clause using +``py::set_error()`` (see below). + +To declare a custom Python exception type, declare a ``py::exception`` variable +and use this in the associated exception translator (note: it is often useful +to make this a static declaration when using it inside a lambda expression +without requiring capturing). + +The following example demonstrates this for a hypothetical exception classes +``MyCustomException`` and ``OtherException``: the first is translated to a +custom python exception ``MyCustomError``, while the second is translated to a +standard python RuntimeError: + +.. code-block:: cpp + + PYBIND11_CONSTINIT static py::gil_safe_call_once_and_store exc_storage; + exc_storage.call_once_and_store_result( + [&]() { return py::exception(m, "MyCustomError"); }); + py::register_exception_translator([](std::exception_ptr p) { + try { + if (p) std::rethrow_exception(p); + } catch (const MyCustomException &e) { + py::set_error(exc_storage.get_stored(), e.what()); + } catch (const OtherException &e) { + py::set_error(PyExc_RuntimeError, e.what()); + } + }); + +Multiple exceptions can be handled by a single translator, as shown in the +example above. If the exception is not caught by the current translator, the +previously registered one gets a chance. + +If none of the registered exception translators is able to handle the +exception, it is handled by the default converter as described in the previous +section. + +.. seealso:: + + The file :file:`tests/test_exceptions.cpp` contains examples + of various custom exception translators and custom exception types. + +.. note:: + + Call ``py::set_error()`` for every exception caught in a custom exception + translator. Failure to do so will cause Python to crash with ``SystemError: + error return without exception set``. + + Exceptions that you do not plan to handle should simply not be caught, or + may be explicitly (re-)thrown to delegate it to the other, + previously-declared existing exception translators. + + Note that ``libc++`` and ``libstdc++`` `behave differently under macOS + `_ + with ``-fvisibility=hidden``. Therefore exceptions that are used across ABI + boundaries need to be explicitly exported, as exercised in + ``tests/test_exceptions.h``. See also: + "Problems with C++ exceptions" under `GCC Wiki `_. + + +Local vs Global Exception Translators +===================================== + +When a global exception translator is registered, it will be applied across all +modules in the reverse order of registration. This can create behavior where the +order of module import influences how exceptions are translated. + +If module1 has the following translator: + +.. code-block:: cpp + + py::register_exception_translator([](std::exception_ptr p) { + try { + if (p) std::rethrow_exception(p); + } catch (const std::invalid_argument &e) { + py::set_error(PyExc_ArgumentError, "module1 handled this"); + } + } + +and module2 has the following similar translator: + +.. code-block:: cpp + + py::register_exception_translator([](std::exception_ptr p) { + try { + if (p) std::rethrow_exception(p); + } catch (const std::invalid_argument &e) { + py::set_error(PyExc_ArgumentError, "module2 handled this"); + } + } + +then which translator handles the invalid_argument will be determined by the +order that module1 and module2 are imported. Since exception translators are +applied in the reverse order of registration, which ever module was imported +last will "win" and that translator will be applied. + +If there are multiple pybind11 modules that share exception types (either +standard built-in or custom) loaded into a single python instance and +consistent error handling behavior is needed, then local translators should be +used. + +Changing the previous example to use ``register_local_exception_translator`` +would mean that when invalid_argument is thrown in the module2 code, the +module2 translator will always handle it, while in module1, the module1 +translator will do the same. + +.. _handling_python_exceptions_cpp: + +Handling exceptions from Python in C++ +====================================== + +When C++ calls Python functions, such as in a callback function or when +manipulating Python objects, and Python raises an ``Exception``, pybind11 +converts the Python exception into a C++ exception of type +:class:`pybind11::error_already_set` whose payload contains a C++ string textual +summary and the actual Python exception. ``error_already_set`` is used to +propagate Python exception back to Python (or possibly, handle them in C++). + +.. tabularcolumns:: |p{0.5\textwidth}|p{0.45\textwidth}| + ++--------------------------------------+--------------------------------------+ +| Exception raised in Python | Thrown as C++ exception type | ++======================================+======================================+ +| Any Python ``Exception`` | :class:`pybind11::error_already_set` | ++--------------------------------------+--------------------------------------+ + +For example: + +.. code-block:: cpp + + try { + // open("missing.txt", "r") + auto file = py::module_::import("io").attr("open")("missing.txt", "r"); + auto text = file.attr("read")(); + file.attr("close")(); + } catch (py::error_already_set &e) { + if (e.matches(PyExc_FileNotFoundError)) { + py::print("missing.txt not found"); + } else if (e.matches(PyExc_PermissionError)) { + py::print("missing.txt found but not accessible"); + } else { + throw; + } + } + +Note that C++ to Python exception translation does not apply here, since that is +a method for translating C++ exceptions to Python, not vice versa. The error raised +from Python is always ``error_already_set``. + +This example illustrates this behavior: + +.. code-block:: cpp + + try { + py::eval("raise ValueError('The Ring')"); + } catch (py::value_error &boromir) { + // Boromir never gets the ring + assert(false); + } catch (py::error_already_set &frodo) { + // Frodo gets the ring + py::print("I will take the ring"); + } + + try { + // py::value_error is a request for pybind11 to raise a Python exception + throw py::value_error("The ball"); + } catch (py::error_already_set &cat) { + // cat won't catch the ball since + // py::value_error is not a Python exception + assert(false); + } catch (py::value_error &dog) { + // dog will catch the ball + py::print("Run Spot run"); + throw; // Throw it again (pybind11 will raise ValueError) + } + +Handling errors from the Python C API +===================================== + +Where possible, use :ref:`pybind11 wrappers ` instead of calling +the Python C API directly. When calling the Python C API directly, in +addition to manually managing reference counts, one must follow the pybind11 +error protocol, which is outlined here. + +After calling the Python C API, if Python returns an error, +``throw py::error_already_set();``, which allows pybind11 to deal with the +exception and pass it back to the Python interpreter. This includes calls to +the error setting functions such as ``py::set_error()``. + +.. code-block:: cpp + + py::set_error(PyExc_TypeError, "C API type error demo"); + throw py::error_already_set(); + + // But it would be easier to simply... + throw py::type_error("pybind11 wrapper type error"); + +Alternately, to ignore the error, call `PyErr_Clear +`_. + +Any Python error must be thrown or cleared, or Python/pybind11 will be left in +an invalid state. + +Handling warnings from the Python C API +======================================= + +Wrappers for handling Python warnings are provided in ``pybind11/warnings.h``. +This header must be included explicitly; it is not transitively included via +``pybind11/pybind11.h``. + +Warnings can be raised with the ``warn`` function: + +.. code-block:: cpp + + py::warnings::warn("This is a warning!", PyExc_Warning); + + // Optionally, a `stack_level` can be specified. + py::warnings::warn("Another one!", PyExc_DeprecationWarning, 3); + +New warning types can be registered at the module level using ``new_warning_type``: + +.. code-block:: cpp + + py::warnings::new_warning_type(m, "CustomWarning", PyExc_RuntimeWarning); + +Chaining exceptions ('raise from') +================================== + +Python has a mechanism for indicating that exceptions were caused by other +exceptions: + +.. code-block:: py + + try: + print(1 / 0) + except Exception as exc: + raise RuntimeError("could not divide by zero") from exc + +To do a similar thing in pybind11, you can use the ``py::raise_from`` function. It +sets the current python error indicator, so to continue propagating the exception +you should ``throw py::error_already_set()``. + +.. code-block:: cpp + + try { + py::eval("print(1 / 0")); + } catch (py::error_already_set &e) { + py::raise_from(e, PyExc_RuntimeError, "could not divide by zero"); + throw py::error_already_set(); + } + +.. versionadded:: 2.8 + +.. _unraisable_exceptions: + +Handling unraisable exceptions +============================== + +If a Python function invoked from a C++ destructor or any function marked +``noexcept(true)`` (collectively, "noexcept functions") throws an exception, there +is no way to propagate the exception, as such functions may not throw. +Should they throw or fail to catch any exceptions in their call graph, +the C++ runtime calls ``std::terminate()`` to abort immediately. + +Similarly, Python exceptions raised in a class's ``__del__`` method do not +propagate, but ``sys.unraisablehook()`` `is triggered +`_ +and an auditing event is logged. + +Any noexcept function should have a try-catch block that traps +class:`error_already_set` (or any other exception that can occur). Note that +pybind11 wrappers around Python exceptions such as +:class:`pybind11::value_error` are *not* Python exceptions; they are C++ +exceptions that pybind11 catches and converts to Python exceptions. Noexcept +functions cannot propagate these exceptions either. A useful approach is to +convert them to Python exceptions and then ``discard_as_unraisable`` as shown +below. + +.. code-block:: cpp + + void nonthrowing_func() noexcept(true) { + try { + // ... + } catch (py::error_already_set &eas) { + // Discard the Python error using Python APIs, using the C++ magic + // variable __func__. Python already knows the type and value and of the + // exception object. + eas.discard_as_unraisable(__func__); + } catch (const std::exception &e) { + // Log and discard C++ exceptions. + third_party::log(e); + } + } + +.. versionadded:: 2.6 diff --git a/external_libraries/pybind11/docs/advanced/functions.rst b/external_libraries/pybind11/docs/advanced/functions.rst new file mode 100644 index 00000000..c647ae0f --- /dev/null +++ b/external_libraries/pybind11/docs/advanced/functions.rst @@ -0,0 +1,616 @@ +Functions +######### + +Before proceeding with this section, make sure that you are already familiar +with the basics of binding functions and classes, as explained in :doc:`/basics` +and :doc:`/classes`. The following guide is applicable to both free and member +functions, i.e. *methods* in Python. + +.. _return_value_policies: + +Return value policies +===================== + +Python and C++ use fundamentally different ways of managing the memory and +lifetime of objects managed by them. This can lead to issues when creating +bindings for functions that return a non-trivial type. Just by looking at the +type information, it is not clear whether Python should take charge of the +returned value and eventually free its resources, or if this is handled on the +C++ side. For this reason, pybind11 provides several *return value policy* +annotations that can be passed to the :func:`module_::def` and +:func:`class_::def` functions. The default policy is +:enum:`return_value_policy::automatic`. + +Return value policies are tricky, and it's very important to get them right. +Just to illustrate what can go wrong, consider the following simple example: + +.. code-block:: cpp + + /* Function declaration */ + Data *get_data() { return _data; /* (pointer to a static data structure) */ } + ... + + /* Binding code */ + m.def("get_data", &get_data); // <-- KABOOM, will cause crash when called from Python + +What's going on here? When ``get_data()`` is called from Python, the return +value (a native C++ type) must be wrapped to turn it into a usable Python type. +In this case, the default return value policy (:enum:`return_value_policy::automatic`) +causes pybind11 to assume ownership of the static ``_data`` instance. + +When Python's garbage collector eventually deletes the Python +wrapper, pybind11 will also attempt to delete the C++ instance (via ``operator +delete()``) due to the implied ownership. At this point, the entire application +will come crashing down, though errors could also be more subtle and involve +silent data corruption. + +In the above example, the policy :enum:`return_value_policy::reference` should have +been specified so that the global data instance is only *referenced* without any +implied transfer of ownership, i.e.: + +.. code-block:: cpp + + m.def("get_data", &get_data, py::return_value_policy::reference); + +On the other hand, this is not the right policy for many other situations, +where ignoring ownership could lead to resource leaks. +As a developer using pybind11, it's important to be familiar with the different +return value policies, including which situation calls for which one of them. +The following table provides an overview of available policies: + +.. tabularcolumns:: |p{0.5\textwidth}|p{0.45\textwidth}| + ++--------------------------------------------------+----------------------------------------------------------------------------+ +| Return value policy | Description | ++==================================================+============================================================================+ +| :enum:`return_value_policy::take_ownership` | Reference an existing object (i.e. do not create a new copy) and take | +| | ownership. Python will call the destructor and delete operator when the | +| | object's reference count reaches zero. Undefined behavior ensues when the | +| | C++ side does the same, or when the data was not dynamically allocated. | ++--------------------------------------------------+----------------------------------------------------------------------------+ +| :enum:`return_value_policy::copy` | Create a new copy of the returned object, which will be owned by Python. | +| | This policy is comparably safe because the lifetimes of the two instances | +| | are decoupled. | ++--------------------------------------------------+----------------------------------------------------------------------------+ +| :enum:`return_value_policy::move` | Use ``std::move`` to move the return value contents into a new instance | +| | that will be owned by Python. This policy is comparably safe because the | +| | lifetimes of the two instances (move source and destination) are decoupled.| ++--------------------------------------------------+----------------------------------------------------------------------------+ +| :enum:`return_value_policy::reference` | Reference an existing object, but do not take ownership. The C++ side is | +| | responsible for managing the object's lifetime and deallocating it when | +| | it is no longer used. Warning: undefined behavior will ensue when the C++ | +| | side deletes an object that is still referenced and used by Python. | ++--------------------------------------------------+----------------------------------------------------------------------------+ +| :enum:`return_value_policy::reference_internal` | If the return value is an lvalue reference or a pointer, the parent object | +| | (the implicit ``this``, or ``self`` argument of the called method or | +| | property) is kept alive for at least the lifespan of the return value. | +| | **Otherwise this policy falls back to** :enum:`return_value_policy::move` | +| | (see #5528). Internally, this policy works just like | +| | :enum:`return_value_policy::reference` but additionally applies a | +| | ``keep_alive<0, 1>`` *call policy* (described in the next section) that | +| | prevents the parent object from being garbage collected as long as the | +| | return value is referenced by Python. This is the default policy for | +| | property getters created via ``def_property``, ``def_readwrite``, etc. | ++--------------------------------------------------+----------------------------------------------------------------------------+ +| :enum:`return_value_policy::automatic` | This policy falls back to the policy | +| | :enum:`return_value_policy::take_ownership` when the return value is a | +| | pointer. Otherwise, it uses :enum:`return_value_policy::move` or | +| | :enum:`return_value_policy::copy` for rvalue and lvalue references, | +| | respectively. See above for a description of what all of these different | +| | policies do. This is the default policy for ``py::class_``-wrapped types. | ++--------------------------------------------------+----------------------------------------------------------------------------+ +| :enum:`return_value_policy::automatic_reference` | As above, but use policy :enum:`return_value_policy::reference` when the | +| | return value is a pointer. This is the default conversion policy for | +| | function arguments when calling Python functions manually from C++ code | +| | (i.e. via ``handle::operator()``) and the casters in ``pybind11/stl.h``. | +| | You probably won't need to use this explicitly. | ++--------------------------------------------------+----------------------------------------------------------------------------+ + +Return value policies can also be applied to properties: + +.. code-block:: cpp + + class_(m, "MyClass") + .def_property("data", &MyClass::getData, &MyClass::setData, + py::return_value_policy::copy); + +Technically, the code above applies the policy to both the getter and the +setter function, however, the setter doesn't really care about *return* +value policies which makes this a convenient terse syntax. Alternatively, +targeted arguments can be passed through the :class:`cpp_function` constructor: + +.. code-block:: cpp + + class_(m, "MyClass") + .def_property("data", + py::cpp_function(&MyClass::getData, py::return_value_policy::copy), + py::cpp_function(&MyClass::setData) + ); + +.. warning:: + + Code with invalid return value policies might access uninitialized memory or + free data structures multiple times, which can lead to hard-to-debug + non-determinism and segmentation faults, hence it is worth spending the + time to understand all the different options in the table above. + +.. note:: + + One important aspect of the above policies is that they only apply to + instances which pybind11 has *not* seen before, in which case the policy + clarifies essential questions about the return value's lifetime and + ownership. When pybind11 knows the instance already (as identified by its + type and address in memory), it will return the existing Python object + wrapper rather than creating a new copy. + +.. note:: + + The next section on :ref:`call_policies` discusses *call policies* that can be + specified *in addition* to a return value policy from the list above. Call + policies indicate reference relationships that can involve both return values + and parameters of functions. + +.. note:: + + As an alternative to elaborate call policies and lifetime management logic, + consider using smart pointers (see the section on :ref:`smart_pointers` for + details). Smart pointers can tell whether an object is still referenced from + C++ or Python, which generally eliminates the kinds of inconsistencies that + can lead to crashes or undefined behavior. For functions returning smart + pointers, it is not necessary to specify a return value policy. + +.. _call_policies: + +Additional call policies +======================== + +In addition to the above return value policies, further *call policies* can be +specified to indicate dependencies between parameters or ensure a certain state +for the function call. + +Keep alive +---------- + +In general, this policy is required when the C++ object is any kind of container +and another object is being added to the container. ``keep_alive`` +indicates that the argument with index ``Patient`` should be kept alive at least +until the argument with index ``Nurse`` is freed by the garbage collector. Argument +indices start at one, while zero refers to the return value. For methods, index +``1`` refers to the implicit ``this`` pointer, while regular arguments begin at +index ``2``. Arbitrarily many call policies can be specified. When a ``Nurse`` +with value ``None`` is detected at runtime, the call policy does nothing. + +When the nurse is not a pybind11-registered type, the implementation internally +relies on the ability to create a *weak reference* to the nurse object. When +the nurse object is not a pybind11-registered type and does not support weak +references, an exception will be thrown. + +If you use an incorrect argument index, you will get a ``RuntimeError`` saying +``Could not activate keep_alive!``. You should review the indices you're using. + +Consider the following example: here, the binding code for a list append +operation ties the lifetime of the newly added element to the underlying +container: + +.. code-block:: cpp + + py::class_(m, "List") + .def("append", &List::append, py::keep_alive<1, 2>()); + +For consistency, the argument indexing is identical for constructors. Index +``1`` still refers to the implicit ``this`` pointer, i.e. the object which is +being constructed. Index ``0`` refers to the return type which is presumed to +be ``void`` when a constructor is viewed like a function. The following example +ties the lifetime of the constructor element to the constructed object: + +.. code-block:: cpp + + py::class_(m, "Nurse") + .def(py::init(), py::keep_alive<1, 2>()); + +.. note:: + + ``keep_alive`` is analogous to the ``with_custodian_and_ward`` (if Nurse, + Patient != 0) and ``with_custodian_and_ward_postcall`` (if Nurse/Patient == + 0) policies from Boost.Python. + +Call guard +---------- + +The ``call_guard`` policy allows any scope guard type ``T`` to be placed +around the function call. For example, this definition: + +.. code-block:: cpp + + m.def("foo", foo, py::call_guard()); + +is equivalent to the following pseudocode: + +.. code-block:: cpp + + m.def("foo", [](args...) { + T scope_guard; + return foo(args...); // forwarded arguments + }); + +The only requirement is that ``T`` is default-constructible, but otherwise any +scope guard will work. This is very useful in combination with ``gil_scoped_release``. +See :ref:`gil`. + +Multiple guards can also be specified as ``py::call_guard``. The +constructor order is left to right and destruction happens in reverse. + +.. seealso:: + + The file :file:`tests/test_call_policies.cpp` contains a complete example + that demonstrates using `keep_alive` and `call_guard` in more detail. + +.. _python_objects_as_args: + +Python objects as arguments +=========================== + +pybind11 exposes all major Python types using thin C++ wrapper classes. These +wrapper classes can also be used as parameters of functions in bindings, which +makes it possible to directly work with native Python types on the C++ side. +For instance, the following statement iterates over a Python ``dict``: + +.. code-block:: cpp + + void print_dict(const py::dict& dict) { + /* Easily interact with Python types */ + for (auto item : dict) + std::cout << "key=" << std::string(py::str(item.first)) << ", " + << "value=" << std::string(py::str(item.second)) << std::endl; + } + +It can be exported: + +.. code-block:: cpp + + m.def("print_dict", &print_dict); + +And used in Python as usual: + +.. code-block:: pycon + + >>> print_dict({"foo": 123, "bar": "hello"}) + key=foo, value=123 + key=bar, value=hello + +For more information on using Python objects in C++, see :doc:`/advanced/pycpp/index`. + +Accepting \*args and \*\*kwargs +=============================== + +Python provides a useful mechanism to define functions that accept arbitrary +numbers of arguments and keyword arguments: + +.. code-block:: python + + def generic(*args, **kwargs): + ... # do something with args and kwargs + +Such functions can also be created using pybind11: + +.. code-block:: cpp + + void generic(py::args args, const py::kwargs& kwargs) { + /// .. do something with args + if (kwargs) + /// .. do something with kwargs + } + + /// Binding code + m.def("generic", &generic); + +The class ``py::args`` derives from ``py::tuple`` and ``py::kwargs`` derives +from ``py::dict``. + +You may also use just one or the other, and may combine these with other +arguments. Note, however, that ``py::kwargs`` must always be the last argument +of the function, and ``py::args`` implies that any further arguments are +keyword-only (see :ref:`keyword_only_arguments`). + +Please refer to the other examples for details on how to iterate over these, +and on how to cast their entries into C++ objects. A demonstration is also +available in ``tests/test_kwargs_and_defaults.cpp``. + +.. note:: + + When combining \*args or \*\*kwargs with :ref:`keyword_args` you should + *not* include ``py::arg`` tags for the ``py::args`` and ``py::kwargs`` + arguments. + +Default arguments revisited +=========================== + +The section on :ref:`default_args` previously discussed basic usage of default +arguments using pybind11. One noteworthy aspect of their implementation is that +default arguments are converted to Python objects right at declaration time. +Consider the following example: + +.. code-block:: cpp + + py::class_("MyClass") + .def("myFunction", py::arg("arg") = SomeType(123)); + +In this case, pybind11 must already be set up to deal with values of the type +``SomeType`` (via a prior instantiation of ``py::class_``), or an +exception will be thrown. + +Another aspect worth highlighting is that the "preview" of the default argument +in the function signature is generated using the object's ``__repr__`` method. +If not available, the signature may not be very helpful, e.g.: + +.. code-block:: pycon + + FUNCTIONS + ... + | myFunction(...) + | Signature : (MyClass, arg : SomeType = ) -> NoneType + ... + +The first way of addressing this is by defining ``SomeType.__repr__``. +Alternatively, it is possible to specify the human-readable preview of the +default argument manually using the ``arg_v`` notation: + +.. code-block:: cpp + + py::class_("MyClass") + .def("myFunction", py::arg_v("arg", SomeType(123), "SomeType(123)")); + +Sometimes it may be necessary to pass a null pointer value as a default +argument. In this case, remember to cast it to the underlying type in question, +like so: + +.. code-block:: cpp + + py::class_("MyClass") + .def("myFunction", py::arg("arg") = static_cast(nullptr)); + +.. _keyword_only_arguments: + +Keyword-only arguments +====================== + +Python implements keyword-only arguments by specifying an unnamed ``*`` +argument in a function definition: + +.. code-block:: python + + def f(a, *, b): # a can be positional or via keyword; b must be via keyword + pass + + + f(a=1, b=2) # good + f(b=2, a=1) # good + f(1, b=2) # good + f(1, 2) # TypeError: f() takes 1 positional argument but 2 were given + +Pybind11 provides a ``py::kw_only`` object that allows you to implement +the same behaviour by specifying the object between positional and keyword-only +argument annotations when registering the function: + +.. code-block:: cpp + + m.def("f", [](int a, int b) { /* ... */ }, + py::arg("a"), py::kw_only(), py::arg("b")); + +.. versionadded:: 2.6 + +A ``py::args`` argument implies that any following arguments are keyword-only, +as if ``py::kw_only()`` had been specified in the same relative location of the +argument list as the ``py::args`` argument. The ``py::kw_only()`` may be +included to be explicit about this, but is not required. + +.. versionchanged:: 2.9 + This can now be combined with ``py::args``. Before, ``py::args`` could only + occur at the end of the argument list, or immediately before a ``py::kwargs`` + argument at the end. + + +Positional-only arguments +========================= + +Python 3.8 introduced a new positional-only argument syntax, using ``/`` in the +function definition (note that this has been a convention for CPython +positional arguments, such as in ``pow()``, since Python 2). You can +do the same thing in any version of Python using ``py::pos_only()``: + +.. code-block:: cpp + + m.def("f", [](int a, int b) { /* ... */ }, + py::arg("a"), py::pos_only(), py::arg("b")); + +You now cannot give argument ``a`` by keyword. This can be combined with +keyword-only arguments, as well. + +.. versionadded:: 2.6 + +.. _nonconverting_arguments: + +Non-converting arguments +======================== + +Certain argument types may support conversion from one type to another. Some +examples of conversions are: + +* :ref:`implicit_conversions` declared using ``py::implicitly_convertible()`` +* Calling a method accepting a double with an integer argument +* Calling a ``std::complex`` argument with a non-complex python type + (for example, with a float). (Requires the optional ``pybind11/complex.h`` + header). +* Calling a function taking an Eigen matrix reference with a numpy array of the + wrong type or of an incompatible data layout. (Requires the optional + ``pybind11/eigen.h`` header). + +This behaviour is sometimes undesirable: the binding code may prefer to raise +an error rather than convert the argument. This behaviour can be obtained +through ``py::arg`` by calling the ``.noconvert()`` method of the ``py::arg`` +object, such as: + +.. code-block:: cpp + + m.def("floats_only", [](double f) { return 0.5 * f; }, py::arg("f").noconvert()); + m.def("floats_preferred", [](double f) { return 0.5 * f; }, py::arg("f")); + +Attempting the call the second function (the one without ``.noconvert()``) with +an integer will succeed, but attempting to call the ``.noconvert()`` version +will fail with a ``TypeError``: + +.. code-block:: pycon + + >>> floats_preferred(4) + 2.0 + >>> floats_only(4) + Traceback (most recent call last): + File "", line 1, in + TypeError: floats_only(): incompatible function arguments. The following argument types are supported: + 1. (f: float) -> float + + Invoked with: 4 + +You may, of course, combine this with the :var:`_a` shorthand notation (see +:ref:`keyword_args`) and/or :ref:`default_args`. It is also permitted to omit +the argument name by using the ``py::arg()`` constructor without an argument +name, i.e. by specifying ``py::arg().noconvert()``. + +.. note:: + + When specifying ``py::arg`` options it is necessary to provide the same + number of options as the bound function has arguments. Thus if you want to + enable no-convert behaviour for just one of several arguments, you will + need to specify a ``py::arg()`` annotation for each argument with the + no-convert argument modified to ``py::arg().noconvert()``. + +.. _none_arguments: + +Allow/Prohibiting None arguments +================================ + +When a C++ type registered with :class:`py::class_` is passed as an argument to +a function taking the instance as pointer or shared holder (e.g. ``shared_ptr`` +or a custom, copyable holder as described in :ref:`smart_pointers`), pybind +allows ``None`` to be passed from Python which results in calling the C++ +function with ``nullptr`` (or an empty holder) for the argument. + +To explicitly enable or disable this behaviour, using the +``.none`` method of the :class:`py::arg` object: + +.. code-block:: cpp + + py::class_(m, "Dog").def(py::init<>()); + py::class_(m, "Cat").def(py::init<>()); + m.def("bark", [](Dog *dog) -> std::string { + if (dog) return "woof!"; /* Called with a Dog instance */ + else return "(no dog)"; /* Called with None, dog == nullptr */ + }, py::arg("dog").none(true)); + m.def("meow", [](Cat *cat) -> std::string { + // Can't be called with None argument + return "meow"; + }, py::arg("cat").none(false)); + +With the above, the Python call ``bark(None)`` will return the string ``"(no +dog)"``, while attempting to call ``meow(None)`` will raise a ``TypeError``: + +.. code-block:: pycon + + >>> from animals import Dog, Cat, bark, meow + >>> bark(Dog()) + 'woof!' + >>> meow(Cat()) + 'meow' + >>> bark(None) + '(no dog)' + >>> meow(None) + Traceback (most recent call last): + File "", line 1, in + TypeError: meow(): incompatible function arguments. The following argument types are supported: + 1. (cat: animals.Cat) -> str + + Invoked with: None + +The default behaviour when the tag is unspecified is to allow ``None``. + +.. note:: + + Even when ``.none(true)`` is specified for an argument, ``None`` will be converted to a + ``nullptr`` *only* for custom and :ref:`opaque ` types. Pointers to built-in types + (``double *``, ``int *``, ...) and STL types (``std::vector *``, ...; if ``pybind11/stl.h`` + is included) are copied when converted to C++ (see :doc:`/advanced/cast/overview`) and will + not allow ``None`` as argument. To pass optional argument of these copied types consider + using ``std::optional`` + +.. _overload_resolution: + +Overload resolution order +========================= + +When a function or method with multiple overloads is called from Python, +pybind11 determines which overload to call in two passes. The first pass +attempts to call each overload without allowing argument conversion (as if +every argument had been specified as ``py::arg().noconvert()`` as described +above). + +If no overload succeeds in the no-conversion first pass, a second pass is +attempted in which argument conversion is allowed (except where prohibited via +an explicit ``py::arg().noconvert()`` attribute in the function definition). + +If the second pass also fails a ``TypeError`` is raised. + +Within each pass, overloads are tried in the order they were registered with +pybind11. If the ``py::prepend()`` tag is added to the definition, a function +can be placed at the beginning of the overload sequence instead, allowing user +overloads to proceed built in functions. + +What this means in practice is that pybind11 will prefer any overload that does +not require conversion of arguments to an overload that does, but otherwise +prefers earlier-defined overloads to later-defined ones. + +.. note:: + + pybind11 does *not* further prioritize based on the number/pattern of + overloaded arguments. That is, pybind11 does not prioritize a function + requiring one conversion over one requiring three, but only prioritizes + overloads requiring no conversion at all to overloads that require + conversion of at least one argument. + +.. versionadded:: 2.6 + + The ``py::prepend()`` tag. + +Binding functions with template parameters +========================================== + +You can bind functions that have template parameters. Here's a function: + +.. code-block:: cpp + + template + void set(T t); + +C++ templates cannot be instantiated at runtime, so you cannot bind the +non-instantiated function: + +.. code-block:: cpp + + // BROKEN (this will not compile) + m.def("set", &set); + +You must bind each instantiated function template separately. You may bind +each instantiation with the same name, which will be treated the same as +an overloaded function: + +.. code-block:: cpp + + m.def("set", &set); + m.def("set", &set); + +Sometimes it's more clear to bind them with separate names, which is also +an option: + +.. code-block:: cpp + + m.def("setInt", &set); + m.def("setString", &set); diff --git a/external_libraries/pybind11/docs/advanced/misc.rst b/external_libraries/pybind11/docs/advanced/misc.rst new file mode 100644 index 00000000..1902ba21 --- /dev/null +++ b/external_libraries/pybind11/docs/advanced/misc.rst @@ -0,0 +1,615 @@ +Miscellaneous +############# + +.. _macro_notes: + +General notes regarding convenience macros +========================================== + +pybind11 provides a few convenience macros such as +:func:`PYBIND11_DECLARE_HOLDER_TYPE` and ``PYBIND11_OVERRIDE_*``. Since these +are "just" macros that are evaluated in the preprocessor (which has no concept +of types), they *will* get confused by commas in a template argument; for +example, consider: + +.. code-block:: cpp + + PYBIND11_OVERRIDE(MyReturnType, Class, func) + +The limitation of the C preprocessor interprets this as five arguments (with new +arguments beginning after each comma) rather than three. To get around this, +there are two alternatives: you can use a type alias, or you can wrap the type +using the ``PYBIND11_TYPE`` macro: + +.. code-block:: cpp + + // Version 1: using a type alias + using ReturnType = MyReturnType; + using ClassType = Class; + PYBIND11_OVERRIDE(ReturnType, ClassType, func); + + // Version 2: using the PYBIND11_TYPE macro: + PYBIND11_OVERRIDE(PYBIND11_TYPE(MyReturnType), + PYBIND11_TYPE(Class), func) + +The ``PYBIND11_MAKE_OPAQUE`` macro does *not* require the above workarounds. + +.. _gil: + +Global Interpreter Lock (GIL) +============================= + +The Python C API dictates that the Global Interpreter Lock (GIL) must always +be held by the current thread to safely access Python objects. As a result, +when Python calls into C++ via pybind11 the GIL must be held, and pybind11 +will never implicitly release the GIL. + +.. code-block:: cpp + + void my_function() { + /* GIL is held when this function is called from Python */ + } + + PYBIND11_MODULE(example, m) { + m.def("my_function", &my_function); + } + +pybind11 will ensure that the GIL is held when it knows that it is calling +Python code. For example, if a Python callback is passed to C++ code via +``std::function``, when C++ code calls the function the built-in wrapper +will acquire the GIL before calling the Python callback. Similarly, the +``PYBIND11_OVERRIDE`` family of macros will acquire the GIL before calling +back into Python. + +When writing C++ code that is called from other C++ code, if that code accesses +Python state, it must explicitly acquire and release the GIL. A separate +document on deadlocks [#f8]_ elaborates on a particularly subtle interaction +with C++'s block-scope static variable initializer guard mutexes. + +.. [#f8] See docs/advanced/deadlock.md + +The classes :class:`gil_scoped_release` and :class:`gil_scoped_acquire` can be +used to acquire and release the global interpreter lock in the body of a C++ +function call. In this way, long-running C++ code can be parallelized using +multiple Python threads, **but great care must be taken** when any +:class:`gil_scoped_release` appear: if there is any way that the C++ code +can access Python objects, :class:`gil_scoped_acquire` should be used to +reacquire the GIL. Taking :ref:`overriding_virtuals` as an example, this +could be realized as follows (important changes highlighted): + +.. code-block:: cpp + :emphasize-lines: 8,30,31 + + class PyAnimal : public Animal, public py::trampoline_self_life_support { + public: + /* Inherit the constructors */ + using Animal::Animal; + + /* Trampoline (need one for each virtual function) */ + std::string go(int n_times) { + /* PYBIND11_OVERRIDE_PURE will acquire the GIL before accessing Python state */ + PYBIND11_OVERRIDE_PURE( + std::string, /* Return type */ + Animal, /* Parent class */ + go, /* Name of function */ + n_times /* Argument(s) */ + ); + } + }; + + PYBIND11_MODULE(example, m) { + py::class_ animal(m, "Animal"); + animal + .def(py::init<>()) + .def("go", &Animal::go); + + py::class_(m, "Dog", animal) + .def(py::init<>()); + + m.def("call_go", [](Animal *animal) -> std::string { + // GIL is held when called from Python code. Release GIL before + // calling into (potentially long-running) C++ code + py::gil_scoped_release release; + return call_go(animal); + }); + } + +The ``call_go`` wrapper can also be simplified using the ``call_guard`` policy +(see :ref:`call_policies`) which yields the same result: + +.. code-block:: cpp + + m.def("call_go", &call_go, py::call_guard()); + + +.. _commongilproblems: + +Common Sources Of Global Interpreter Lock Errors +================================================================== + +Failing to properly hold the Global Interpreter Lock (GIL) is one of the +more common sources of bugs within code that uses pybind11. If you are +running into GIL related errors, we highly recommend you consult the +following checklist. + +- Do you have any global variables that are pybind11 objects or invoke + pybind11 functions in either their constructor or destructor? You are generally + not allowed to invoke any Python function in a global static context. We recommend + using lazy initialization and then intentionally leaking at the end of the program. + +- Do you have any pybind11 objects that are members of other C++ structures? One + commonly overlooked requirement is that pybind11 objects have to increase their reference count + whenever their copy constructor is called. Thus, you need to be holding the GIL to invoke + the copy constructor of any C++ class that has a pybind11 member. This can sometimes be very + tricky to track for complicated programs Think carefully when you make a pybind11 object + a member in another struct. + +- C++ destructors that invoke Python functions can be particularly troublesome as + destructors can sometimes get invoked in weird and unexpected circumstances as a result + of exceptions. + +- C++ static block-scope variable initialization that calls back into Python can + cause deadlocks; see [#f8]_ for a detailed discussion. + +- You should try running your code in a debug build. That will enable additional assertions + within pybind11 that will throw exceptions on certain GIL handling errors + (reference counting operations). + +.. _misc_free_threading: + +Free-threading support +================================================================== + +pybind11 supports the experimental free-threaded builds of Python versions 3.13+. +pybind11's internal data structures are thread safe. To enable your modules to be used with +free-threading, pass the :class:`mod_gil_not_used` tag as the third argument to +``PYBIND11_MODULE``. + +For example: + +.. code-block:: cpp + :emphasize-lines: 1 + + PYBIND11_MODULE(example, m, py::mod_gil_not_used()) { + py::class_ animal(m, "Animal"); + // etc + } + +Importantly, enabling your module to be used with free-threading is also your promise that +your code is thread safe. Modules must still be built against the Python free-threading branch to +enable free-threading, even if they specify this tag. Adding this tag does not break +compatibility with non-free-threaded Python. + +.. _misc_subinterp: + +Sub-interpreter support +================================================================== + +pybind11 supports isolated sub-interpreters, which are stable in Python 3.12+. pybind11's +internal data structures are sub-interpreter safe. To enable your modules to be imported in +isolated sub-interpreters, pass the :func:`multiple_interpreters::per_interpreter_gil()` +tag as the third or later argument to ``PYBIND11_MODULE``. + +For example: + +.. code-block:: cpp + :emphasize-lines: 1 + + PYBIND11_MODULE(example, m, py::multiple_interpreters::per_interpreter_gil()) { + py::class_ animal(m, "Animal"); + // etc + } + +Best Practices for Sub-interpreter Safety: + +- Your initialization function will run for each interpreter that imports your module. + +- Never share Python objects across different sub-interpreters. + +- Avoid global/static state whenever possible. Instead, keep state within each interpreter, + such as in instance members tied to Python objects, :func:`globals()`, and the interpreter + state dict. + +- Modules without any global/static state in their C++ code may already be sub-interpreter safe + without any additional work! + +- Avoid trying to "cache" Python objects in C++ variables across function calls (this is an easy + way to accidentally introduce sub-interpreter bugs). + +- While sub-interpreters each have their own GIL, there can now be multiple independent GILs in one + program, so concurrent calls into a module from two different sub-interpreters are still + possible. Therefore, your module still needs to consider thread safety. + +pybind11 also supports "legacy" sub-interpreters which shared a single global GIL. You can enable +legacy-only behavior by using the :func:`multiple_interpreters::shared_gil()` tag in +``PYBIND11_MODULE``. + +You can explicitly disable sub-interpreter support in your module by using the +:func:`multiple_interpreters::not_supported()` tag. This is the default behavior if you do not +specify a multiple_interpreters tag. + +.. _misc_concurrency: + +Concurrency and Parallelism in Python with pybind11 +=================================================== + +Sub-interpreter support does not imply free-threading support or vice versa. Free-threading safe +modules can still have global/static state (as long as access to them is thread-safe), but +sub-interpreter safe modules cannot. Likewise, sub-interpreter safe modules can still rely on the +GIL, but free-threading safe modules cannot. + +Here is a simple example module which has a function that calculates a value and returns the result +of the previous calculation. + +.. code-block:: cpp + + PYBIND11_MODULE(example, m) { + static size_t seed = 0; + m.def("calc_next", []() { + auto old = seed; + seed = (seed + 1) * 10; + return old; + }); + +This module is not free-threading safe because there is no synchronization on the number variable. +It is relatively easy to make this free-threading safe. One way is by using atomics, like this: + +.. code-block:: cpp + :emphasize-lines: 1,2 + + PYBIND11_MODULE(example, m, py::mod_gil_not_used()) { + static std::atomic seed(0); + m.def("calc_next", []() { + size_t old, next; + do { + old = seed.load(); + next = (old + 1) * 10; + } while (!seed.compare_exchange_weak(old, next)); + return old; + }); + } + +The atomic variable and the compare-exchange guarantee a consistent behavior from this function even +when called currently from multiple threads at the same time. + +However, the global/static integer is not sub-interpreter safe, because the calls in one +sub-interpreter will change what is seen in another. To fix it, the state needs to be specific to +each interpreter. One way to do that is by storing the state on another Python object, such as a +member of a class. For this simple example, we will store it in :func:`globals`. + +.. code-block:: cpp + :emphasize-lines: 1,6 + + PYBIND11_MODULE(example, m, py::multiple_interpreters::per_interpreter_gil()) { + m.def("calc_next", []() { + if (!py::globals().contains("myseed")) + py::globals()["myseed"] = 0; + size_t old = py::globals()["myseed"]; + py::globals()["myseed"] = (old + 1) * 10; + return old; + }); + } + +This module is sub-interpreter safe, for both ``shared_gil`` ("legacy") and +``per_interpreter_gil`` ("default") varieties. Multiple sub-interpreters could each call this same +function concurrently from different threads. This is safe because each sub-interpreter's GIL +protects it's own Python objects from concurrent access. + +However, the module is no longer free-threading safe, for the same reason as +before, because the calculation is not synchronized. We can synchronize it +using a Python critical section. This will do nothing if not in free-threaded +Python. You can have it lock one or two Python objects. You cannot nest it. + +.. warning:: + + When using a ``py::scoped_critical_section``, make sure it is not nested and + that no other synchronization primitives (such as a ``std::mutex``) are + held, which could lead to deadlocks. In 3.13, taking the same lock causes it + to release then reacquire, which means you can't use it to, for example, read + and write to a dictionary, because the dictionary uses a critical section + internally in CPython. Use a ``std::mutex`` instead if you need this on + Python 3.13. In 3.14, taking a lock on a locked object no longer releases + and relocks as an optimization, which also fixes this case. + +.. code-block:: cpp + :emphasize-lines: 1,4,8 + + #include + // ... + + PYBIND11_MODULE(example, m, py::multiple_interpreters::per_interpreter_gil(), py::mod_gil_not_used()) { + m.def("calc_next", []() { + size_t old; + py::dict g = py::globals(); + py::scoped_critical_section guard(g); + if (!g.contains("myseed")) + g["myseed"] = 0; + old = g["myseed"]; + g["myseed"] = (old + 1) * 10; + return old; + }); + } + +The module is now both sub-interpreter safe and free-threading safe. + +Binding sequence data types, iterators, the slicing protocol, etc. +================================================================== + +Please refer to the supplemental example for details. + +.. seealso:: + + The file :file:`tests/test_sequences_and_iterators.cpp` contains a + complete example that shows how to bind a sequence data type, including + length queries (``__len__``), iterators (``__iter__``), the slicing + protocol and other kinds of useful operations. + + +Partitioning code over multiple extension modules +================================================= + +It's straightforward to split binding code over multiple extension modules, +while referencing types that are declared elsewhere. Everything "just" works +without any special precautions. One exception to this rule occurs when +extending a type declared in another extension module. Recall the basic example +from Section :ref:`inheritance`. + +.. code-block:: cpp + + py::class_ pet(m, "Pet"); + pet.def(py::init()) + .def_readwrite("name", &Pet::name); + + py::class_(m, "Dog", pet /* <- specify parent */) + .def(py::init()) + .def("bark", &Dog::bark); + +Suppose now that ``Pet`` bindings are defined in a module named ``basic``, +whereas the ``Dog`` bindings are defined somewhere else. The challenge is of +course that the variable ``pet`` is not available anymore though it is needed +to indicate the inheritance relationship to the constructor of ``py::class_``. +However, it can be acquired as follows: + +.. code-block:: cpp + + py::object pet = (py::object) py::module_::import("basic").attr("Pet"); + + py::class_(m, "Dog", pet) + .def(py::init()) + .def("bark", &Dog::bark); + +Alternatively, you can specify the base class as a template parameter option to +``py::class_``, which performs an automated lookup of the corresponding Python +type. Like the above code, however, this also requires invoking the ``import`` +function once to ensure that the pybind11 binding code of the module ``basic`` +has been executed: + +.. code-block:: cpp + + py::module_::import("basic"); + + py::class_(m, "Dog") + .def(py::init()) + .def("bark", &Dog::bark); + +Naturally, both methods will fail when there are cyclic dependencies. + +Note that pybind11 code compiled with hidden-by-default symbol visibility (e.g. +via the command line flag ``-fvisibility=hidden`` on GCC/Clang), which is +required for proper pybind11 functionality, can interfere with the ability to +access types defined in another extension module. Working around this requires +manually exporting types that are accessed by multiple extension modules; +pybind11 provides a macro to do just this: + +.. code-block:: cpp + + class PYBIND11_EXPORT Dog : public Animal { + ... + }; + +Note also that it is possible (although would rarely be required) to share arbitrary +C++ objects between extension modules at runtime. Internal library data is shared +between modules using capsule machinery [#f6]_ which can be also utilized for +storing, modifying and accessing user-defined data. Note that an extension module +will "see" other extensions' data if and only if they were built with the same +pybind11 version. Consider the following example: + +.. code-block:: cpp + + auto data = reinterpret_cast(py::get_shared_data("mydata")); + if (!data) + data = static_cast(py::set_shared_data("mydata", new MyData(42))); + +If the above snippet was used in several separately compiled extension modules, +the first one to be imported would create a ``MyData`` instance and associate +a ``"mydata"`` key with a pointer to it. Extensions that are imported later +would be then able to access the data behind the same pointer. + +.. [#f6] https://docs.python.org/3/extending/extending.html#using-capsules + +Module Destructors +================== + +pybind11 does not provide an explicit mechanism to invoke cleanup code at +module destruction time. In rare cases where such functionality is required, it +is possible to emulate it using Python capsules or weak references with a +destruction callback. + +.. code-block:: cpp + + auto cleanup_callback = []() { + // perform cleanup here -- this function is called with the GIL held + }; + + m.add_object("_cleanup", py::capsule(cleanup_callback)); + +This approach has the potential downside that instances of classes exposed +within the module may still be alive when the cleanup callback is invoked +(whether this is acceptable will generally depend on the application). + +Alternatively, the capsule may also be stashed within a type object, which +ensures that it not called before all instances of that type have been +collected: + +.. code-block:: cpp + + auto cleanup_callback = []() { /* ... */ }; + m.attr("BaseClass").attr("_cleanup") = py::capsule(cleanup_callback); + +Both approaches also expose a potentially dangerous ``_cleanup`` attribute in +Python, which may be undesirable from an API standpoint (a premature explicit +call from Python might lead to undefined behavior). Yet another approach that +avoids this issue involves weak reference with a cleanup callback: + +.. code-block:: cpp + + // Register a callback function that is invoked when the BaseClass object is collected + py::cpp_function cleanup_callback( + [](py::handle weakref) { + // perform cleanup here -- this function is called with the GIL held + + weakref.dec_ref(); // release weak reference + } + ); + + // Create a weak reference with a cleanup callback and initially leak it + (void) py::weakref(m.attr("BaseClass"), cleanup_callback).release(); + +.. note:: + + PyPy does not garbage collect objects when the interpreter exits. An alternative + approach (which also works on CPython) is to use the :py:mod:`atexit` module [#f7]_, + for example: + + .. code-block:: cpp + + auto atexit = py::module_::import("atexit"); + atexit.attr("register")(py::cpp_function([]() { + // perform cleanup here -- this function is called with the GIL held + })); + + .. [#f7] https://docs.python.org/3/library/atexit.html + + +Generating documentation using Sphinx +===================================== + +Sphinx [#f4]_ has the ability to inspect the signatures and documentation +strings in pybind11-based extension modules to automatically generate beautiful +documentation in a variety formats. The python_example repository [#f5]_ contains a +simple example repository which uses this approach. + +There are two potential gotchas when using this approach: first, make sure that +the resulting strings do not contain any :kbd:`TAB` characters, which break the +docstring parsing routines. You may want to use C++11 raw string literals, +which are convenient for multi-line comments. Conveniently, any excess +indentation will be automatically be removed by Sphinx. However, for this to +work, it is important that all lines are indented consistently, i.e.: + +.. code-block:: cpp + + // ok + m.def("foo", &foo, R"mydelimiter( + The foo function + + Parameters + ---------- + )mydelimiter"); + + // *not ok* + m.def("foo", &foo, R"mydelimiter(The foo function + + Parameters + ---------- + )mydelimiter"); + +By default, pybind11 automatically generates and prepends a signature to the docstring of a function +registered with ``module_::def()`` and ``class_::def()``. Sometimes this +behavior is not desirable, because you want to provide your own signature or remove +the docstring completely to exclude the function from the Sphinx documentation. +The class ``options`` allows you to selectively suppress auto-generated signatures: + +.. code-block:: cpp + + PYBIND11_MODULE(example, m) { + py::options options; + options.disable_function_signatures(); + + m.def("add", [](int a, int b) { return a + b; }, "A function which adds two numbers"); + } + +pybind11 also appends all members of an enum to the resulting enum docstring. +This default behavior can be disabled by using the ``disable_enum_members_docstring()`` +function of the ``options`` class. + +With ``disable_user_defined_docstrings()`` all user defined docstrings of +``module_::def()``, ``class_::def()`` and ``enum_()`` are disabled, but the +function signatures and enum members are included in the docstring, unless they +are disabled separately. + +Note that changes to the settings affect only function bindings created during the +lifetime of the ``options`` instance. When it goes out of scope at the end of the module's init function, +the default settings are restored to prevent unwanted side effects. + +.. [#f4] http://www.sphinx-doc.org +.. [#f5] http://github.com/pybind/python_example + +.. _avoiding-cpp-types-in-docstrings: + +Avoiding C++ types in docstrings +================================ + +Docstrings are generated at the time of the declaration, e.g. when ``.def(...)`` is called. +At this point parameter and return types should be known to pybind11. +If a custom type is not exposed yet through a ``py::class_`` constructor or a custom type caster, +its C++ type name will be used instead to generate the signature in the docstring: + +.. code-block:: text + + | __init__(...) + | __init__(self: example.Foo, arg0: ns::Bar) -> None + ^^^^^^^ + + +This limitation can be circumvented by ensuring that C++ classes are registered with pybind11 +before they are used as a parameter or return type of a function: + +.. code-block:: cpp + + PYBIND11_MODULE(example, m) { + + auto pyFoo = py::class_(m, "Foo"); + auto pyBar = py::class_(m, "Bar"); + + pyFoo.def(py::init()); + pyBar.def(py::init()); + } + +Setting inner type hints in docstrings +====================================== + +When you use pybind11 wrappers for ``list``, ``dict``, and other generic python +types, the docstring will just display the generic type. You can convey the +inner types in the docstring by using a special 'typed' version of the generic +type. + +.. code-block:: cpp + + PYBIND11_MODULE(example, m) { + m.def("pass_list_of_str", [](py::typing::List arg) { + // arg can be used just like py::list + )); + } + +The resulting docstring will be ``pass_list_of_str(arg0: list[str]) -> None``. + +The following special types are available in ``pybind11/typing.h``: + +* ``py::Tuple`` +* ``py::Dict`` +* ``py::List`` +* ``py::Set`` +* ``py::Callable`` + +.. warning:: Just like in python, these are merely hints. They don't actually + enforce the types of their contents at runtime or compile time. diff --git a/external_libraries/pybind11/docs/advanced/pycpp/index.rst b/external_libraries/pybind11/docs/advanced/pycpp/index.rst new file mode 100644 index 00000000..6885bdcf --- /dev/null +++ b/external_libraries/pybind11/docs/advanced/pycpp/index.rst @@ -0,0 +1,13 @@ +Python C++ interface +#################### + +pybind11 exposes Python types and functions using thin C++ wrappers, which +makes it possible to conveniently call Python code from C++ without resorting +to Python's C API. + +.. toctree:: + :maxdepth: 2 + + object + numpy + utilities diff --git a/external_libraries/pybind11/docs/advanced/pycpp/numpy.rst b/external_libraries/pybind11/docs/advanced/pycpp/numpy.rst new file mode 100644 index 00000000..e0b9ff46 --- /dev/null +++ b/external_libraries/pybind11/docs/advanced/pycpp/numpy.rst @@ -0,0 +1,493 @@ +.. _numpy: + +NumPy +##### + +Buffer protocol +=============== + +Python supports an extremely general and convenient approach for exchanging +data between plugin libraries. Types can expose a buffer view [#f2]_, which +provides fast direct access to the raw internal data representation. Suppose we +want to bind the following simplistic Matrix class: + +.. code-block:: cpp + + class Matrix { + public: + Matrix(size_t rows, size_t cols) : m_rows(rows), m_cols(cols) { + m_data = new float[rows*cols]; + } + float *data() { return m_data; } + size_t rows() const { return m_rows; } + size_t cols() const { return m_cols; } + private: + size_t m_rows, m_cols; + float *m_data; + }; + +The following binding code exposes the ``Matrix`` contents as a buffer object, +making it possible to cast Matrices into NumPy arrays. It is even possible to +completely avoid copy operations with Python expressions like +``np.array(matrix_instance, copy = False)``. + +.. code-block:: cpp + + py::class_(m, "Matrix", py::buffer_protocol()) + .def_buffer([](Matrix &m) -> py::buffer_info { + return py::buffer_info( + m.data(), /* Pointer to buffer */ + sizeof(float), /* Size of one scalar */ + py::format_descriptor::format(), /* Python struct-style format descriptor */ + 2, /* Number of dimensions */ + { m.rows(), m.cols() }, /* Buffer dimensions */ + { sizeof(float) * m.cols(), /* Strides (in bytes) for each index */ + sizeof(float) } + ); + }); + +Supporting the buffer protocol in a new type involves specifying the special +``py::buffer_protocol()`` tag in the ``py::class_`` constructor and calling the +``def_buffer()`` method with a lambda function that creates a +``py::buffer_info`` description record on demand describing a given matrix +instance. The contents of ``py::buffer_info`` mirror the Python buffer protocol +specification. + +.. code-block:: cpp + + struct buffer_info { + void *ptr; + py::ssize_t itemsize; + std::string format; + py::ssize_t ndim; + std::vector shape; + std::vector strides; + }; + +To create a C++ function that can take a Python buffer object as an argument, +simply use the type ``py::buffer`` as one of its arguments. Buffers can exist +in a great variety of configurations, hence some safety checks are usually +necessary in the function body. Below, you can see a basic example on how to +define a custom constructor for the Eigen double precision matrix +(``Eigen::MatrixXd``) type, which supports initialization from compatible +buffer objects (e.g. a NumPy matrix). + +.. code-block:: cpp + + /* Bind MatrixXd (or some other Eigen type) to Python */ + typedef Eigen::MatrixXd Matrix; + + typedef Matrix::Scalar Scalar; + constexpr bool rowMajor = Matrix::Flags & Eigen::RowMajorBit; + + py::class_(m, "Matrix", py::buffer_protocol()) + .def(py::init([](py::buffer b) { + typedef Eigen::Stride Strides; + + /* Request a buffer descriptor from Python */ + py::buffer_info info = b.request(); + + /* Some basic validation checks ... */ + if (!info.item_type_is_equivalent_to()) + throw std::runtime_error("Incompatible format: expected a double array!"); + + if (info.ndim != 2) + throw std::runtime_error("Incompatible buffer dimension!"); + + auto strides = Strides( + info.strides[rowMajor ? 0 : 1] / (py::ssize_t)sizeof(Scalar), + info.strides[rowMajor ? 1 : 0] / (py::ssize_t)sizeof(Scalar)); + + auto map = Eigen::Map( + static_cast(info.ptr), info.shape[0], info.shape[1], strides); + + return Matrix(map); + })); + +For reference, the ``def_buffer()`` call for this Eigen data type should look +as follows: + +.. code-block:: cpp + + .def_buffer([](Matrix &m) -> py::buffer_info { + return py::buffer_info( + m.data(), /* Pointer to buffer */ + sizeof(Scalar), /* Size of one scalar */ + py::format_descriptor::format(), /* Python struct-style format descriptor */ + 2, /* Number of dimensions */ + { m.rows(), m.cols() }, /* Buffer dimensions */ + { sizeof(Scalar) * (rowMajor ? m.cols() : 1), + sizeof(Scalar) * (rowMajor ? 1 : m.rows()) } + /* Strides (in bytes) for each index */ + ); + }) + +For a much easier approach of binding Eigen types (although with some +limitations), refer to the section on :doc:`/advanced/cast/eigen`. + +.. seealso:: + + The file :file:`tests/test_buffers.cpp` contains a complete example + that demonstrates using the buffer protocol with pybind11 in more detail. + +.. [#f2] http://docs.python.org/3/c-api/buffer.html + +Arrays +====== + +By exchanging ``py::buffer`` with ``py::array`` in the above snippet, we can +restrict the function so that it only accepts NumPy arrays (rather than any +type of Python object satisfying the buffer protocol). + +In many situations, we want to define a function which only accepts a NumPy +array of a certain data type. This is possible via the ``py::array_t`` +template. For instance, the following function requires the argument to be a +NumPy array containing double precision values. + +.. code-block:: cpp + + void f(py::array_t array); + +When it is invoked with a different type (e.g. an integer or a list of +integers), the binding code will attempt to cast the input into a NumPy array +of the requested type. This feature requires the :file:`pybind11/numpy.h` +header to be included. Note that :file:`pybind11/numpy.h` does not depend on +the NumPy headers, and thus can be used without declaring a build-time +dependency on NumPy; NumPy>=1.7.0 is a runtime dependency. + +Data in NumPy arrays is not guaranteed to packed in a dense manner; +furthermore, entries can be separated by arbitrary column and row strides. +Sometimes, it can be useful to require a function to only accept dense arrays +using either the C (row-major) or Fortran (column-major) ordering. This can be +accomplished via a second template argument with values ``py::array::c_style`` +or ``py::array::f_style``. + +.. code-block:: cpp + + void f(py::array_t array); + +The ``py::array::forcecast`` argument is the default value of the second +template parameter, and it ensures that non-conforming arguments are converted +into an array satisfying the specified requirements instead of trying the next +function overload. + +There are several methods on arrays; the methods listed below under references +work, as well as the following functions based on the NumPy API: + +- ``.dtype()`` returns the type of the contained values. + +- ``.strides()`` returns a pointer to the strides of the array (optionally pass + an integer axis to get a number). + +- ``.flags()`` returns the flag settings. ``.writable()`` and ``.owndata()`` + are directly available. + +- ``.offset_at()`` returns the offset (optionally pass indices). + +- ``.squeeze()`` returns a view with length-1 axes removed. + +- ``.view(dtype)`` returns a view of the array with a different dtype. + +- ``.reshape({i, j, ...})`` returns a view of the array with a different shape. + ``.resize({...})`` is also available. + +- ``.index_at(i, j, ...)`` gets the count from the beginning to a given index. + + +There are also several methods for getting references (described below). + +Structured types +================ + +In order for ``py::array_t`` to work with structured (record) types, we first +need to register the memory layout of the type. This can be done via +``PYBIND11_NUMPY_DTYPE`` macro, called in the plugin definition code, which +expects the type followed by field names: + +.. code-block:: cpp + + struct A { + int x; + double y; + }; + + struct B { + int z; + A a; + }; + + // ... + PYBIND11_MODULE(test, m, py::mod_gil_not_used()) { + // ... + + PYBIND11_NUMPY_DTYPE(A, x, y); + PYBIND11_NUMPY_DTYPE(B, z, a); + /* now both A and B can be used as template arguments to py::array_t */ + } + +The structure should consist of fundamental arithmetic types, ``std::complex``, +previously registered substructures, and arrays of any of the above. Both C++ +arrays and ``std::array`` are supported. While there is a static assertion to +prevent many types of unsupported structures, it is still the user's +responsibility to use only "plain" structures that can be safely manipulated as +raw memory without violating invariants. + +Scalar types +============ + +In some cases we may want to accept or return NumPy scalar values such as +``np.float32`` or ``np.float64``. We hope to be able to handle single-precision +and double-precision on the C-side. However, both are bound to Python's +double-precision builtin float by default, so they cannot be processed separately. +We used the ``py::buffer`` trick to implement the previous approach, which +will cause the readability of the code to drop significantly. + +Luckily, there's a helper type for this occasion - ``py::numpy_scalar``: + +.. code-block:: cpp + + m.def("add", [](py::numpy_scalar a, py::numpy_scalar b) { + return py::make_scalar(a + b); + }); + m.def("add", [](py::numpy_scalar a, py::numpy_scalar b) { + return py::make_scalar(a + b); + }); + +This type is trivially convertible to and from the type it wraps; currently +supported scalar types are NumPy arithmetic types: ``bool_``, ``int8``, +``int16``, ``int32``, ``int64``, ``uint8``, ``uint16``, ``uint32``, +``uint64``, ``float32``, ``float64``, ``complex64``, ``complex128``, all of +them mapping to respective C++ counterparts. + +.. note:: + + ``py::numpy_scalar`` strictly matches NumPy scalar types. For example, + ``py::numpy_scalar`` will accept ``np.int64(123)``, + but **not** a regular Python ``int`` like ``123``. + +.. note:: + + Native C types are mapped to NumPy types in a platform specific way: for + instance, ``char`` may be mapped to either ``np.int8`` or ``np.uint8`` + and ``long`` may use 4 or 8 bytes depending on the platform. Unless you + clearly understand the difference and your needs, please use ````. + +Vectorizing functions +===================== + +Suppose we want to bind a function with the following signature to Python so +that it can process arbitrary NumPy array arguments (vectors, matrices, general +N-D arrays) in addition to its normal arguments: + +.. code-block:: cpp + + double my_func(int x, float y, double z); + +After including the ``pybind11/numpy.h`` header, this is extremely simple: + +.. code-block:: cpp + + m.def("vectorized_func", py::vectorize(my_func)); + +Invoking the function like below causes 4 calls to be made to ``my_func`` with +each of the array elements. The significant advantage of this compared to +solutions like ``numpy.vectorize()`` is that the loop over the elements runs +entirely on the C++ side and can be crunched down into a tight, optimized loop +by the compiler. The result is returned as a NumPy array of type +``numpy.dtype.float64``. + +.. code-block:: pycon + + >>> x = np.array([[1, 3], [5, 7]]) + >>> y = np.array([[2, 4], [6, 8]]) + >>> z = 3 + >>> result = vectorized_func(x, y, z) + +The scalar argument ``z`` is transparently replicated 4 times. The input +arrays ``x`` and ``y`` are automatically converted into the right types (they +are of type ``numpy.dtype.int64`` but need to be ``numpy.dtype.int32`` and +``numpy.dtype.float32``, respectively). + +.. note:: + + Only arithmetic, complex, and POD types passed by value or by ``const &`` + reference are vectorized; all other arguments are passed through as-is. + Functions taking rvalue reference arguments cannot be vectorized. + +In cases where the computation is too complicated to be reduced to +``vectorize``, it will be necessary to create and access the buffer contents +manually. The following snippet contains a complete example that shows how this +works (the code is somewhat contrived, since it could have been done more +simply using ``vectorize``). + +.. code-block:: cpp + + #include + #include + + namespace py = pybind11; + + py::array_t add_arrays(py::array_t input1, py::array_t input2) { + py::buffer_info buf1 = input1.request(), buf2 = input2.request(); + + if (buf1.ndim != 1 || buf2.ndim != 1) + throw std::runtime_error("Number of dimensions must be one"); + + if (buf1.size != buf2.size) + throw std::runtime_error("Input shapes must match"); + + /* No pointer is passed, so NumPy will allocate the buffer */ + auto result = py::array_t(buf1.size); + + py::buffer_info buf3 = result.request(); + + double *ptr1 = static_cast(buf1.ptr); + double *ptr2 = static_cast(buf2.ptr); + double *ptr3 = static_cast(buf3.ptr); + + for (size_t idx = 0; idx < buf1.shape[0]; idx++) + ptr3[idx] = ptr1[idx] + ptr2[idx]; + + return result; + } + + PYBIND11_MODULE(test, m, py::mod_gil_not_used()) { + m.def("add_arrays", &add_arrays, "Add two NumPy arrays"); + } + +.. seealso:: + + The file :file:`tests/test_numpy_vectorize.cpp` contains a complete + example that demonstrates using :func:`vectorize` in more detail. + +Direct access +============= + +For performance reasons, particularly when dealing with very large arrays, it +is often desirable to directly access array elements without internal checking +of dimensions and bounds on every access when indices are known to be already +valid. To avoid such checks, the ``array`` class and ``array_t`` template +class offer an unchecked proxy object that can be used for this unchecked +access through the ``unchecked`` and ``mutable_unchecked`` methods, +where ``N`` gives the required dimensionality of the array: + +.. code-block:: cpp + + m.def("sum_3d", [](py::array_t x) { + auto r = x.unchecked<3>(); // x must have ndim = 3; can be non-writeable + double sum = 0; + for (py::ssize_t i = 0; i < r.shape(0); i++) + for (py::ssize_t j = 0; j < r.shape(1); j++) + for (py::ssize_t k = 0; k < r.shape(2); k++) + sum += r(i, j, k); + return sum; + }); + m.def("increment_3d", [](py::array_t x) { + auto r = x.mutable_unchecked<3>(); // Will throw if ndim != 3 or flags.writeable is false + for (py::ssize_t i = 0; i < r.shape(0); i++) + for (py::ssize_t j = 0; j < r.shape(1); j++) + for (py::ssize_t k = 0; k < r.shape(2); k++) + r(i, j, k) += 1.0; + }, py::arg().noconvert()); + +To obtain the proxy from an ``array`` object, you must specify both the data +type and number of dimensions as template arguments, such as ``auto r = +myarray.mutable_unchecked()``. + +If the number of dimensions is not known at compile time, you can omit the +dimensions template parameter (i.e. calling ``arr_t.unchecked()`` or +``arr.unchecked()``. This will give you a proxy object that works in the +same way, but results in less optimizable code and thus a small efficiency +loss in tight loops. + +Note that the returned proxy object directly references the array's data, and +only reads its shape, strides, and writeable flag when constructed. You must +take care to ensure that the referenced array is not destroyed or reshaped for +the duration of the returned object, typically by limiting the scope of the +returned instance. + +The returned proxy object supports some of the same methods as ``py::array`` so +that it can be used as a drop-in replacement for some existing, index-checked +uses of ``py::array``: + +- ``.ndim()`` returns the number of dimensions + +- ``.data(1, 2, ...)`` and ``r.mutable_data(1, 2, ...)``` returns a pointer to + the ``const T`` or ``T`` data, respectively, at the given indices. The + latter is only available to proxies obtained via ``a.mutable_unchecked()``. + +- ``.itemsize()`` returns the size of an item in bytes, i.e. ``sizeof(T)``. + +- ``.shape(n)`` returns the size of dimension ``n`` + +- ``.size()`` returns the total number of elements (i.e. the product of the shapes). + +- ``.nbytes()`` returns the number of bytes used by the referenced elements + (i.e. ``itemsize()`` times ``size()``). + +.. seealso:: + + The file :file:`tests/test_numpy_array.cpp` contains additional examples + demonstrating the use of this feature. + +Ellipsis +======== + +Python provides a convenient ``...`` ellipsis notation that is often used to +slice multidimensional arrays. For instance, the following snippet extracts the +middle dimensions of a tensor with the first and last index set to zero. + +.. code-block:: python + + a = ... # a NumPy array + b = a[0, ..., 0] + +The function ``py::ellipsis()`` function can be used to perform the same +operation on the C++ side: + +.. code-block:: cpp + + py::array a = /* A NumPy array */; + py::array b = a[py::make_tuple(0, py::ellipsis(), 0)]; + + +Memory view +=========== + +For a case when we simply want to provide a direct accessor to C/C++ buffer +without a concrete class object, we can return a ``memoryview`` object. Suppose +we wish to expose a ``memoryview`` for 2x4 uint8_t array, we can do the +following: + +.. code-block:: cpp + + const uint8_t buffer[] = { + 0, 1, 2, 3, + 4, 5, 6, 7 + }; + m.def("get_memoryview2d", []() { + return py::memoryview::from_buffer( + buffer, // buffer pointer + { 2, 4 }, // shape (rows, cols) + { sizeof(uint8_t) * 4, sizeof(uint8_t) } // strides in bytes + ); + }); + +This approach is meant for providing a ``memoryview`` for a C/C++ buffer not +managed by Python. The user is responsible for managing the lifetime of the +buffer. Using a ``memoryview`` created in this way after deleting the buffer in +C++ side results in undefined behavior. + +We can also use ``memoryview::from_memory`` for a simple 1D contiguous buffer: + +.. code-block:: cpp + + m.def("get_memoryview1d", []() { + return py::memoryview::from_memory( + buffer, // buffer pointer + sizeof(uint8_t) * 8 // buffer size + ); + }); + +.. versionchanged:: 2.6 + ``memoryview::from_memory`` added. diff --git a/external_libraries/pybind11/docs/advanced/pycpp/object.rst b/external_libraries/pybind11/docs/advanced/pycpp/object.rst new file mode 100644 index 00000000..93e1a94d --- /dev/null +++ b/external_libraries/pybind11/docs/advanced/pycpp/object.rst @@ -0,0 +1,286 @@ +Python types +############ + +.. _wrappers: + +Available wrappers +================== + +All major Python types are available as thin C++ wrapper classes. These +can also be used as function parameters -- see :ref:`python_objects_as_args`. + +Available types include :class:`handle`, :class:`object`, :class:`bool_`, +:class:`int_`, :class:`float_`, :class:`str`, :class:`bytes`, :class:`tuple`, +:class:`list`, :class:`dict`, :class:`slice`, :class:`none`, :class:`capsule`, +:class:`iterable`, :class:`iterator`, :class:`function`, :class:`buffer`, +:class:`array`, and :class:`array_t`. + +.. warning:: + + Be sure to review the :ref:`pytypes_gotchas` before using this heavily in + your C++ API. + +.. _instantiating_compound_types: + +Instantiating compound Python types from C++ +============================================ + +Dictionaries can be initialized in the :class:`dict` constructor: + +.. code-block:: cpp + + using namespace pybind11::literals; // to bring in the `_a` literal + py::dict d("spam"_a=py::none(), "eggs"_a=42); + +A tuple of python objects can be instantiated using :func:`py::make_tuple`: + +.. code-block:: cpp + + py::tuple tup = py::make_tuple(42, py::none(), "spam"); + +Each element is converted to a supported Python type. + +A `simple namespace`_ can be instantiated using + +.. code-block:: cpp + + using namespace pybind11::literals; // to bring in the `_a` literal + py::object SimpleNamespace = py::module_::import("types").attr("SimpleNamespace"); + py::object ns = SimpleNamespace("spam"_a=py::none(), "eggs"_a=42); + +Attributes on a namespace can be modified with the :func:`py::delattr`, +:func:`py::getattr`, and :func:`py::setattr` functions. Simple namespaces can +be useful as lightweight stand-ins for class instances. + +.. _simple namespace: https://docs.python.org/3/library/types.html#types.SimpleNamespace + +.. _casting_back_and_forth: + +Casting back and forth +====================== + +In this kind of mixed code, it is often necessary to convert arbitrary C++ +types to Python, which can be done using :func:`py::cast`: + +.. code-block:: cpp + + MyClass *cls = ...; + py::object obj = py::cast(cls); + +The reverse direction uses the following syntax: + +.. code-block:: cpp + + py::object obj = ...; + MyClass *cls = obj.cast(); + +When conversion fails, both directions throw the exception :class:`cast_error`. + +.. _python_libs: + +Accessing Python libraries from C++ +=================================== + +It is also possible to import objects defined in the Python standard +library or available in the current Python environment (``sys.path``) and work +with these in C++. + +This example obtains a reference to the Python ``Decimal`` class. + +.. code-block:: cpp + + // Equivalent to "from decimal import Decimal" + py::object Decimal = py::module_::import("decimal").attr("Decimal"); + +.. code-block:: cpp + + // Try to import scipy + py::object scipy = py::module_::import("scipy"); + return scipy.attr("__version__"); + + +.. _calling_python_functions: + +Calling Python functions +======================== + +It is also possible to call Python classes, functions and methods +via ``operator()``. + +.. code-block:: cpp + + // Construct a Python object of class Decimal + py::object pi = Decimal("3.14159"); + +.. code-block:: cpp + + // Use Python to make our directories + py::object os = py::module_::import("os"); + py::object makedirs = os.attr("makedirs"); + makedirs("/tmp/path/to/somewhere"); + +One can convert the result obtained from Python to a pure C++ version +if a ``py::class_`` or type conversion is defined. + +.. code-block:: cpp + + py::function f = <...>; + py::object result_py = f(1234, "hello", some_instance); + MyClass &result = result_py.cast(); + +.. _calling_python_methods: + +Calling Python methods +======================== + +To call an object's method, one can again use ``.attr`` to obtain access to the +Python method. + +.. code-block:: cpp + + // Calculate e^π in decimal + py::object exp_pi = pi.attr("exp")(); + py::print(py::str(exp_pi)); + +In the example above ``pi.attr("exp")`` is a *bound method*: it will always call +the method for that same instance of the class. Alternately one can create an +*unbound method* via the Python class (instead of instance) and pass the ``self`` +object explicitly, followed by other arguments. + +.. code-block:: cpp + + py::object decimal_exp = Decimal.attr("exp"); + + // Compute the e^n for n=0..4 + for (int n = 0; n < 5; n++) { + py::print(decimal_exp(Decimal(n)); + } + +Keyword arguments +================= + +Keyword arguments are also supported. In Python, there is the usual call syntax: + +.. code-block:: python + + def f(number, say, to): + ... # function code + + + f(1234, say="hello", to=some_instance) # keyword call in Python + +In C++, the same call can be made using: + +.. code-block:: cpp + + using namespace pybind11::literals; // to bring in the `_a` literal + f(1234, "say"_a="hello", "to"_a=some_instance); // keyword call in C++ + +Unpacking arguments +=================== + +Unpacking of ``*args`` and ``**kwargs`` is also possible and can be mixed with +other arguments: + +.. code-block:: cpp + + // * unpacking + py::tuple args = py::make_tuple(1234, "hello", some_instance); + f(*args); + + // ** unpacking + py::dict kwargs = py::dict("number"_a=1234, "say"_a="hello", "to"_a=some_instance); + f(**kwargs); + + // mixed keywords, * and ** unpacking + py::tuple args = py::make_tuple(1234); + py::dict kwargs = py::dict("to"_a=some_instance); + f(*args, "say"_a="hello", **kwargs); + +Generalized unpacking according to PEP448_ is also supported: + +.. code-block:: cpp + + py::dict kwargs1 = py::dict("number"_a=1234); + py::dict kwargs2 = py::dict("to"_a=some_instance); + f(**kwargs1, "say"_a="hello", **kwargs2); + +.. seealso:: + + The file :file:`tests/test_pytypes.cpp` contains a complete + example that demonstrates passing native Python types in more detail. The + file :file:`tests/test_callbacks.cpp` presents a few examples of calling + Python functions from C++, including keywords arguments and unpacking. + +.. _PEP448: https://www.python.org/dev/peps/pep-0448/ + +.. _implicit_casting: + +Implicit casting +================ + +When using the C++ interface for Python types, or calling Python functions, +objects of type :class:`object` are returned. It is possible to invoke implicit +conversions to subclasses like :class:`dict`. The same holds for the proxy objects +returned by ``operator[]`` or ``obj.attr()``. +Casting to subtypes improves code readability and allows values to be passed to +C++ functions that require a specific subtype rather than a generic :class:`object`. + +.. code-block:: cpp + + #include + using namespace pybind11::literals; + + py::module_ os = py::module_::import("os"); + py::module_ path = py::module_::import("os.path"); // like 'import os.path as path' + py::module_ np = py::module_::import("numpy"); // like 'import numpy as np' + + py::str curdir_abs = path.attr("abspath")(path.attr("curdir")); + py::print(py::str("Current directory: ") + curdir_abs); + py::dict environ = os.attr("environ"); + py::print(environ["HOME"]); + py::array_t arr = np.attr("ones")(3, "dtype"_a="float32"); + py::print(py::repr(arr + py::int_(1))); + +These implicit conversions are available for subclasses of :class:`object`; there +is no need to call ``obj.cast()`` explicitly as for custom classes, see +:ref:`casting_back_and_forth`. + +.. note:: + If a trivial conversion via move constructor is not possible, both implicit and + explicit casting (calling ``obj.cast()``) will attempt a "rich" conversion. + For instance, ``py::list env = os.attr("environ");`` will succeed and is + equivalent to the Python code ``env = list(os.environ)`` that produces a + list of the dict keys. + +.. TODO: Adapt text once PR #2349 has landed + +Handling exceptions +=================== + +Python exceptions from wrapper classes will be thrown as a ``py::error_already_set``. +See :ref:`Handling exceptions from Python in C++ +` for more information on handling exceptions +raised when calling C++ wrapper classes. + +.. _pytypes_gotchas: + +Gotchas +======= + +Default-Constructed Wrappers +---------------------------- + +When a wrapper type is default-constructed, it is **not** a valid Python object (i.e. it is not ``py::none()``). It is simply the same as +``PyObject*`` null pointer. To check for this, use +``static_cast(my_wrapper)``. + +Assigning py::none() to wrappers +-------------------------------- + +You may be tempted to use types like ``py::str`` and ``py::dict`` in C++ +signatures (either pure C++, or in bound signatures), and assign them default +values of ``py::none()``. However, in a best case scenario, it will fail fast +because ``None`` is not convertible to that type (e.g. ``py::dict``), or in a +worse case scenario, it will silently work but corrupt the types you want to +work with (e.g. ``py::str(py::none())`` will yield ``"None"`` in Python). diff --git a/external_libraries/pybind11/docs/advanced/pycpp/utilities.rst b/external_libraries/pybind11/docs/advanced/pycpp/utilities.rst new file mode 100644 index 00000000..af0f9cb2 --- /dev/null +++ b/external_libraries/pybind11/docs/advanced/pycpp/utilities.rst @@ -0,0 +1,155 @@ +Utilities +######### + +Using Python's print function in C++ +==================================== + +The usual way to write output in C++ is using ``std::cout`` while in Python one +would use ``print``. Since these methods use different buffers, mixing them can +lead to output order issues. To resolve this, pybind11 modules can use the +:func:`py::print` function which writes to Python's ``sys.stdout`` for consistency. + +Python's ``print`` function is replicated in the C++ API including optional +keyword arguments ``sep``, ``end``, ``file``, ``flush``. Everything works as +expected in Python: + +.. code-block:: cpp + + py::print(1, 2.0, "three"); // 1 2.0 three + py::print(1, 2.0, "three", "sep"_a="-"); // 1-2.0-three + + auto args = py::make_tuple("unpacked", true); + py::print("->", *args, "end"_a="<-"); // -> unpacked True <- + +.. _ostream_redirect: + +Capturing standard output from ostream +====================================== + +Often, a library will use the streams ``std::cout`` and ``std::cerr`` to print, +but this does not play well with Python's standard ``sys.stdout`` and ``sys.stderr`` +redirection. Replacing a library's printing with ``py::print `` may not +be feasible. This can be fixed using a guard around the library function that +redirects output to the corresponding Python streams: + +.. code-block:: cpp + + #include + + ... + + // Add a scoped redirect for your noisy code + m.def("noisy_func", []() { + py::scoped_ostream_redirect stream( + std::cout, // std::ostream& + py::module_::import("sys").attr("stdout") // Python output + ); + call_noisy_func(); + }); + +.. warning:: + + The implementation in ``pybind11/iostream.h`` is NOT thread safe. Multiple + threads writing to a redirected ostream concurrently cause data races + and potentially buffer overflows. Therefore it is currently a requirement + that all (possibly) concurrent redirected ostream writes are protected by + a mutex. #HelpAppreciated: Work on iostream.h thread safety. For more + background see the discussions under + `PR #2982 `_ and + `PR #2995 `_. + +This method respects flushes on the output streams and will flush if needed +when the scoped guard is destroyed. This allows the output to be redirected in +real time, such as to a Jupyter notebook. The two arguments, the C++ stream and +the Python output, are optional, and default to standard output if not given. An +extra type, ``py::scoped_estream_redirect ``, is identical +except for defaulting to ``std::cerr`` and ``sys.stderr``; this can be useful with +``py::call_guard``, which allows multiple items, but uses the default constructor: + +.. code-block:: cpp + + // Alternative: Call single function using call guard + m.def("noisy_func", &call_noisy_function, + py::call_guard()); + +The redirection can also be done in Python with the addition of a context +manager, using the ``py::add_ostream_redirect() `` function: + +.. code-block:: cpp + + py::add_ostream_redirect(m, "ostream_redirect"); + +The name in Python defaults to ``ostream_redirect`` if no name is passed. This +creates the following context manager in Python: + +.. code-block:: python + + with ostream_redirect(stdout=True, stderr=True): + noisy_function() + +It defaults to redirecting both streams, though you can use the keyword +arguments to disable one of the streams if needed. + +.. note:: + + The above methods will not redirect C-level output to file descriptors, such + as ``fprintf``. For those cases, you'll need to redirect the file + descriptors either directly in C or with Python's ``os.dup2`` function + in an operating-system dependent way. + +.. _eval: + +Evaluating Python expressions from strings and files +==================================================== + +pybind11 provides the ``eval``, ``exec`` and ``eval_file`` functions to evaluate +Python expressions and statements. The following example illustrates how they +can be used. + +.. code-block:: cpp + + // At beginning of file + #include + + ... + + // Evaluate in scope of main module + py::object scope = py::module_::import("__main__").attr("__dict__"); + + // Evaluate an isolated expression + int result = py::eval("my_variable + 10", scope).cast(); + + // Evaluate a sequence of statements + py::exec( + "print('Hello')\n" + "print('world!');", + scope); + + // Evaluate the statements in an separate Python file on disk + py::eval_file("script.py", scope); + +C++11 raw string literals are also supported and quite handy for this purpose. +The only requirement is that the first statement must be on a new line following +the raw string delimiter ``R"(``, ensuring all lines have common leading indent: + +.. code-block:: cpp + + py::exec(R"( + x = get_answer() + if x == 42: + print('Hello World!') + else: + print('Bye!') + )", scope + ); + +.. note:: + + `eval` and `eval_file` accept a template parameter that describes how the + string/file should be interpreted. Possible choices include ``eval_expr`` + (isolated expression), ``eval_single_statement`` (a single statement, return + value is always ``none``), and ``eval_statements`` (sequence of statements, + return value is always ``none``). `eval` defaults to ``eval_expr``, + `eval_file` defaults to ``eval_statements`` and `exec` is just a shortcut + for ``eval``. diff --git a/external_libraries/pybind11/docs/advanced/smart_ptrs.rst b/external_libraries/pybind11/docs/advanced/smart_ptrs.rst new file mode 100644 index 00000000..aeb98de0 --- /dev/null +++ b/external_libraries/pybind11/docs/advanced/smart_ptrs.rst @@ -0,0 +1,179 @@ +.. _py_class_holder: + +Smart pointers & ``py::class_`` +############################### + +The binding generator for classes, ``py::class_``, can be passed a template +type that denotes a special *holder* type that is used to manage references to +the object. If no such holder type template argument is given, the default for +a type ``T`` is ``std::unique_ptr``. + +.. note:: + + A ``py::class_`` for a given C++ type ``T`` — and all its derived types — + can only use a single holder type. + + +.. _smart_holder: + +``py::smart_holder`` +==================== + +Starting with pybind11v3, ``py::smart_holder`` is built into pybind11. It is +the recommended ``py::class_`` holder for most situations. However, for +backward compatibility it is **not** the default holder, and there are no +plans to make it the default holder in the future. + +It is extremely easy to use the safer and more versatile ``py::smart_holder``: +simply add ``py::smart_holder`` to ``py::class_``: + +* ``py::class_`` to + +* ``py::class_``. + +.. note:: + + A shorthand, ``py::classh``, is provided for + ``py::class_``. The ``h`` in ``py::classh`` stands + for **smart_holder** but is shortened for brevity, ensuring it has the + same number of characters as ``py::class_``. This design choice facilitates + easy experimentation with ``py::smart_holder`` without introducing + distracting whitespace noise in diffs. + +The ``py::smart_holder`` functionality includes the following: + +* Support for **two-way** Python/C++ conversions for both + ``std::unique_ptr`` and ``std::shared_ptr`` **simultaneously**. + +* Passing a Python object back to C++ via ``std::unique_ptr``, safely + **disowning** the Python object. + +* Safely passing "trampoline" objects (objects with C++ virtual function + overrides implemented in Python, see :ref:`overriding_virtuals`) via + ``std::unique_ptr`` or ``std::shared_ptr`` back to C++: + associated Python objects are automatically kept alive for the lifetime + of the smart-pointer. + +* Full support for ``std::enable_shared_from_this`` (`cppreference + `_). + + +``std::unique_ptr`` +=================== + +This is the default ``py::class_`` holder and works as expected in +most situations. However, handling base-and-derived classes involves a +``reinterpret_cast``, which is, strictly speaking, undefined behavior. +Also note that the ``std::unique_ptr`` holder only supports passing a +``std::unique_ptr`` from C++ to Python, but not the other way around. +For example, the following code works as expected with ``py::class_``: + +.. code-block:: cpp + + std::unique_ptr create_example() { return std::unique_ptr(new Example()); } + +.. code-block:: cpp + + m.def("create_example", &create_example); + +However, this will fail with ``py::class_`` (but works with +``py::class_``): + +.. code-block:: cpp + + void do_something_with_example(std::unique_ptr ex) { ... } + +.. note:: + + The ``reinterpret_cast`` mentioned above is `here + `_. + For completeness: The same cast is also applied to ``py::smart_holder``, + but that is safe, because ``py::smart_holder`` is not templated. + + +``std::shared_ptr`` +=================== + +It is possible to use ``std::shared_ptr`` as the holder, for example: + +.. code-block:: cpp + + py::class_ /* <- holder type */>(m, "Example"); + +Compared to using ``py::class_``, there are two noteworthy disadvantages: + +* Because a ``py::class_`` for a given C++ type ``T`` can only use a + single holder type, ``std::unique_ptr`` cannot even be passed from C++ + to Python. This will become apparent only at runtime, often through a + segmentation fault. + +* Similar to the ``std::unique_ptr`` holder, the handling of base-and-derived + classes involves a ``reinterpret_cast`` that has strictly speaking undefined + behavior, although it works as expected in most situations. + + +.. _smart_pointers: + +Custom smart pointers +===================== + +For custom smart pointers (e.g. ``c10::intrusive_ptr`` in pytorch), transparent +conversions can be enabled using a macro invocation similar to the following. +It must be declared at the top namespace level before any binding code: + +.. code-block:: cpp + + PYBIND11_DECLARE_HOLDER_TYPE(T, SmartPtr) + +The first argument of :func:`PYBIND11_DECLARE_HOLDER_TYPE` should be a +placeholder name that is used as a template parameter of the second argument. +Thus, feel free to use any identifier, but use it consistently on both sides; +also, don't use the name of a type that already exists in your codebase. + +The macro also accepts a third optional boolean parameter that is set to false +by default. Specify + +.. code-block:: cpp + + PYBIND11_DECLARE_HOLDER_TYPE(T, SmartPtr, true) + +if ``SmartPtr`` can always be initialized from a ``T*`` pointer without the +risk of inconsistencies (such as multiple independent ``SmartPtr`` instances +believing that they are the sole owner of the ``T*`` pointer). A common +situation where ``true`` should be passed is when the ``T`` instances use +*intrusive* reference counting. + +Please take a look at the :ref:`macro_notes` before using this feature. + +By default, pybind11 assumes that your custom smart pointer has a standard +interface, i.e. provides a ``.get()`` member function to access the underlying +raw pointer. If this is not the case, pybind11's ``holder_helper`` must be +specialized: + +.. code-block:: cpp + + // Always needed for custom holder types + PYBIND11_DECLARE_HOLDER_TYPE(T, SmartPtr) + + // Only needed if the type's `.get()` goes by another name + namespace PYBIND11_NAMESPACE { namespace detail { + template + struct holder_helper> { // <-- specialization + static const T *get(const SmartPtr &p) { return p.getPointer(); } + }; + }} + +The above specialization informs pybind11 that the custom ``SmartPtr`` class +provides ``.get()`` functionality via ``.getPointer()``. + +.. note:: + + The two noteworthy disadvantages mentioned under the ``std::shared_ptr`` + section apply similarly to custom smart pointer holders, but there is no + established safe alternative in this case. + +.. seealso:: + + The file :file:`tests/test_smart_ptr.cpp` contains a complete example + that demonstrates how to work with custom reference-counting holder types + in more detail. diff --git a/external_libraries/pybind11/docs/basics.rst b/external_libraries/pybind11/docs/basics.rst new file mode 100644 index 00000000..074d9885 --- /dev/null +++ b/external_libraries/pybind11/docs/basics.rst @@ -0,0 +1,316 @@ +.. _basics: + +First steps +########### + +This sections demonstrates the basic features of pybind11. Before getting +started, make sure that development environment is set up to compile the +included set of test cases. + + +Compiling the test cases +======================== + +Linux/macOS +----------- + +On Linux you'll need to install the **python-dev** or **python3-dev** packages as +well as **cmake**. On macOS, the included python version works out of the box, +but **cmake** must still be installed. + +After installing the prerequisites, run + +.. code-block:: bash + + mkdir build + cd build + cmake .. + make check -j 4 + +The last line will both compile and run the tests. + +Windows +------- + +On Windows, only **Visual Studio 2017** and newer are supported. + +.. Note:: + + To use the C++17 in Visual Studio 2017 (MSVC 14.1), pybind11 requires the flag + ``/permissive-`` to be passed to the compiler `to enforce standard conformance`_. When + building with Visual Studio 2019, this is not strictly necessary, but still advised. + +.. _`to enforce standard conformance`: https://docs.microsoft.com/en-us/cpp/build/reference/permissive-standards-conformance?view=vs-2017 + +To compile and run the tests: + +.. code-block:: batch + + mkdir build + cd build + cmake .. + cmake --build . --config Release --target check + +This will create a Visual Studio project, compile and run the target, all from the +command line. + +.. Note:: + + If all tests fail, make sure that the Python binary and the testcases are compiled + for the same processor type and bitness (i.e. either **i386** or **x86_64**). You + can specify **x86_64** as the target architecture for the generated Visual Studio + project using ``cmake -A x64 ..``. + +.. seealso:: + + Advanced users who are already familiar with Boost.Python may want to skip + the tutorial and look at the test cases in the :file:`tests` directory, + which exercise all features of pybind11. + +Header and namespace conventions +================================ + +For brevity, all code examples assume that the following two lines are present: + +.. code-block:: cpp + + #include + + namespace py = pybind11; + +.. note:: + + ``pybind11/pybind11.h`` includes ``Python.h``, as such it must be the first file + included in any source file or header for `the same reasons as Python.h`_. + +.. _`the same reasons as Python.h`: https://docs.python.org/3/extending/extending.html#a-simple-example + +Some features may require additional headers, but those will be specified as needed. + +.. _simple_example: + +Creating bindings for a simple function +======================================= + +Let's start by creating Python bindings for an extremely simple function, which +adds two numbers and returns their result: + +.. code-block:: cpp + + int add(int i, int j) { + return i + j; + } + +For simplicity [#f1]_, we'll put both this function and the binding code into +a file named :file:`example.cpp` with the following contents: + +.. code-block:: cpp + + #include + + namespace py = pybind11; + + int add(int i, int j) { + return i + j; + } + + PYBIND11_MODULE(example, m, py::mod_gil_not_used()) { + m.doc() = "pybind11 example plugin"; // optional module docstring + + m.def("add", &add, "A function that adds two numbers"); + } + +.. [#f1] In practice, implementation and binding code will generally be located + in separate files. + +The :func:`PYBIND11_MODULE` macro creates a function that will be called when an +``import`` statement is issued from within Python. The module name (``example``) +is given as the first macro argument (it should not be in quotes). The second +argument (``m``) defines a variable of type :class:`py::module_ ` which +is the main interface for creating bindings. The method :func:`module_::def` +generates binding code that exposes the ``add()`` function to Python. + +.. note:: + + Notice how little code was needed to expose our function to Python: all + details regarding the function's parameters and return value were + automatically inferred using template metaprogramming. This overall + approach and the used syntax are borrowed from Boost.Python, though the + underlying implementation is very different. + +pybind11 is a header-only library, hence it is not necessary to link against +any special libraries and there are no intermediate (magic) translation steps. +On Linux, the above example can be compiled using the following command: + +.. code-block:: bash + + $ c++ -O3 -Wall -shared -std=c++11 -fPIC $(python3 -m pybind11 --includes) example.cpp -o example$(python3 -m pybind11 --extension-suffix) + +.. note:: + + If you used :ref:`include_as_a_submodule` to get the pybind11 source, then + use ``$(python3-config --includes) -Iextern/pybind11/include`` instead of + ``$(python3 -m pybind11 --includes)`` in the above compilation, as + explained in :ref:`building_manually`. + +For more details on the required compiler flags on Linux and macOS, see +:ref:`building_manually`. For complete cross-platform compilation instructions, +refer to the :ref:`compiling` page. + +The `python_example`_ and `cmake_example`_ repositories are also a good place +to start. They are both complete project examples with cross-platform build +systems. The only difference between the two is that `python_example`_ uses +Python's ``setuptools`` to build the module, while `cmake_example`_ uses CMake +(which may be preferable for existing C++ projects). + +.. _python_example: https://github.com/pybind/python_example +.. _cmake_example: https://github.com/pybind/cmake_example + +Building the above C++ code will produce a binary module file that can be +imported to Python. Assuming that the compiled module is located in the +current directory, the following interactive Python session shows how to +load and execute the example: + +.. code-block:: pycon + + $ python + Python 3.9.10 (main, Jan 15 2022, 11:48:04) + [Clang 13.0.0 (clang-1300.0.29.3)] on darwin + Type "help", "copyright", "credits" or "license" for more information. + >>> import example + >>> example.add(1, 2) + 3 + >>> + +.. _keyword_args: + +Keyword arguments +================= + +With a simple code modification, it is possible to inform Python about the +names of the arguments ("i" and "j" in this case). + +.. code-block:: cpp + + m.def("add", &add, "A function which adds two numbers", + py::arg("i"), py::arg("j")); + +:class:`arg` is one of several special tag classes which can be used to pass +metadata into :func:`module_::def`. With this modified binding code, we can now +call the function using keyword arguments, which is a more readable alternative +particularly for functions taking many parameters: + +.. code-block:: pycon + + >>> import example + >>> example.add(i=1, j=2) + 3L + +The keyword names also appear in the function signatures within the documentation. + +.. code-block:: pycon + + >>> help(example) + + .... + + FUNCTIONS + add(...) + Signature : (i: int, j: int) -> int + + A function which adds two numbers + +A shorter notation for named arguments is also available: + +.. code-block:: cpp + + // regular notation + m.def("add1", &add, py::arg("i"), py::arg("j")); + // shorthand + using namespace pybind11::literals; + m.def("add2", &add, "i"_a, "j"_a); + +The :var:`_a` suffix forms a C++11 literal which is equivalent to :class:`arg`. +Note that the literal operator must first be made visible with the directive +``using namespace pybind11::literals``. This does not bring in anything else +from the ``pybind11`` namespace except for literals. + +.. _default_args: + +Default arguments +================= + +Suppose now that the function to be bound has default arguments, e.g.: + +.. code-block:: cpp + + int add(int i = 1, int j = 2) { + return i + j; + } + +Unfortunately, pybind11 cannot automatically extract these parameters, since they +are not part of the function's type information. However, they are simple to specify +using an extension of :class:`arg`: + +.. code-block:: cpp + + m.def("add", &add, "A function which adds two numbers", + py::arg("i") = 1, py::arg("j") = 2); + +The default values also appear within the documentation. + +.. code-block:: pycon + + >>> help(example) + + .... + + FUNCTIONS + add(...) + Signature : (i: int = 1, j: int = 2) -> int + + A function which adds two numbers + +The shorthand notation is also available for default arguments: + +.. code-block:: cpp + + // regular notation + m.def("add1", &add, py::arg("i") = 1, py::arg("j") = 2); + // shorthand + m.def("add2", &add, "i"_a=1, "j"_a=2); + +Exporting variables +=================== + +To expose a value from C++, use the ``attr`` function to register it in a +module as shown below. Built-in types and general objects (more on that later) +are automatically converted when assigned as attributes, and can be explicitly +converted using the function ``py::cast``. + +.. code-block:: cpp + + PYBIND11_MODULE(example, m, py::mod_gil_not_used()) { + m.attr("the_answer") = 42; + py::object world = py::cast("World"); + m.attr("what") = world; + } + +These are then accessible from Python: + +.. code-block:: pycon + + >>> import example + >>> example.the_answer + 42 + >>> example.what + 'World' + +.. _supported_types: + +Supported data types +==================== + +A large number of data types are supported out of the box and can be used +seamlessly as functions arguments, return values or with ``py::cast`` in general. +For a full overview, see the :doc:`advanced/cast/index` section. diff --git a/external_libraries/pybind11/docs/benchmark.py b/external_libraries/pybind11/docs/benchmark.py new file mode 100644 index 00000000..0b9d08f8 --- /dev/null +++ b/external_libraries/pybind11/docs/benchmark.py @@ -0,0 +1,89 @@ +from __future__ import annotations + +import datetime as dt +import os +import random + +nfns = 4 # Functions per class +nargs = 4 # Arguments per function + + +def generate_dummy_code_pybind11(nclasses=10): + decl = "" + bindings = "" + + for cl in range(nclasses): + decl += f"class cl{cl:03};\n" + decl += "\n" + + for cl in range(nclasses): + decl += f"class {cl:03} {{\n" + decl += "public:\n" + bindings += f' py::class_(m, "cl{cl:03}")\n' + for fn in range(nfns): + ret = random.randint(0, nclasses - 1) + params = [random.randint(0, nclasses - 1) for i in range(nargs)] + decl += f" cl{ret:03} *fn_{fn:03}(" + decl += ", ".join(f"cl{p:03} *" for p in params) + decl += ");\n" + bindings += f' .def("fn_{fn:03}", &cl{cl:03}::fn_{fn:03})\n' + decl += "};\n\n" + bindings += " ;\n" + + result = "#include \n\n" + result += "namespace py = pybind11;\n\n" + result += decl + "\n" + result += "PYBIND11_MODULE(example, m, py::mod_gil_not_used()) {\n" + result += bindings + result += "}" + return result + + +def generate_dummy_code_boost(nclasses=10): + decl = "" + bindings = "" + + for cl in range(nclasses): + decl += f"class cl{cl:03};\n" + decl += "\n" + + for cl in range(nclasses): + decl += f"class cl{cl:03} {{\n" + decl += "public:\n" + bindings += f' py::class_("cl{cl:03}")\n' + for fn in range(nfns): + ret = random.randint(0, nclasses - 1) + params = [random.randint(0, nclasses - 1) for i in range(nargs)] + decl += f" cl{ret:03} *fn_{fn:03}(" + decl += ", ".join(f"cl{p:03} *" for p in params) + decl += ");\n" + bindings += f' .def("fn_{fn:03}", &cl{cl:03}::fn_{fn:03}, py::return_value_policy())\n' + decl += "};\n\n" + bindings += " ;\n" + + result = "#include \n\n" + result += "namespace py = boost::python;\n\n" + result += decl + "\n" + result += "BOOST_PYTHON_MODULE(example) {\n" + result += bindings + result += "}" + return result + + +for codegen in [generate_dummy_code_pybind11, generate_dummy_code_boost]: + print("{") + for i in range(10): + nclasses = 2**i + with open("test.cpp", "w") as f: + f.write(codegen(nclasses)) + n1 = dt.datetime.now() + os.system( + "g++ -Os -shared -rdynamic -undefined dynamic_lookup " + "-fvisibility=hidden -std=c++14 test.cpp -I include " + "-I /System/Library/Frameworks/Python.framework/Headers -o test.so" + ) + n2 = dt.datetime.now() + elapsed = (n2 - n1).total_seconds() + size = os.stat("test.so").st_size + print(f" {{{nclasses * nfns}, {elapsed:.6f}, {size}}},") + print("}") diff --git a/external_libraries/pybind11/docs/benchmark.rst b/external_libraries/pybind11/docs/benchmark.rst new file mode 100644 index 00000000..f1bf3216 --- /dev/null +++ b/external_libraries/pybind11/docs/benchmark.rst @@ -0,0 +1,95 @@ +Benchmark +========= + +The following is the result of a synthetic benchmark comparing both compilation +time and module size of pybind11 against Boost.Python. A detailed report about a +Boost.Python to pybind11 conversion of a real project is available here: [#f1]_. + +.. [#f1] http://graylab.jhu.edu/RosettaCon2016/PyRosetta-4.pdf + +Setup +----- + +A python script (see the ``docs/benchmark.py`` file) was used to generate a set +of files with dummy classes whose count increases for each successive benchmark +(between 1 and 2048 classes in powers of two). Each class has four methods with +a randomly generated signature with a return value and four arguments. (There +was no particular reason for this setup other than the desire to generate many +unique function signatures whose count could be controlled in a simple way.) + +Here is an example of the binding code for one class: + +.. code-block:: cpp + + ... + class cl034 { + public: + cl279 *fn_000(cl084 *, cl057 *, cl065 *, cl042 *); + cl025 *fn_001(cl098 *, cl262 *, cl414 *, cl121 *); + cl085 *fn_002(cl445 *, cl297 *, cl145 *, cl421 *); + cl470 *fn_003(cl200 *, cl323 *, cl332 *, cl492 *); + }; + ... + + PYBIND11_MODULE(example, m, py::mod_gil_not_used()) { + ... + py::class_(m, "cl034") + .def("fn_000", &cl034::fn_000) + .def("fn_001", &cl034::fn_001) + .def("fn_002", &cl034::fn_002) + .def("fn_003", &cl034::fn_003) + ... + } + +The Boost.Python version looks almost identical except that a return value +policy had to be specified as an argument to ``def()``. For both libraries, +compilation was done with + +.. code-block:: bash + + Apple LLVM version 7.0.2 (clang-700.1.81) + +and the following compilation flags + +.. code-block:: bash + + g++ -Os -shared -rdynamic -undefined dynamic_lookup -fvisibility=hidden -std=c++14 + +Compilation time +---------------- + +The following log-log plot shows how the compilation time grows for an +increasing number of class and function declarations. pybind11 includes many +fewer headers, which initially leads to shorter compilation times, but the +performance is ultimately fairly similar (pybind11 is 19.8 seconds faster for +the largest largest file with 2048 classes and a total of 8192 methods -- a +modest **1.2x** speedup relative to Boost.Python, which required 116.35 +seconds). + +.. only:: not latex + + .. image:: pybind11_vs_boost_python1.svg + +.. only:: latex + + .. image:: pybind11_vs_boost_python1.png + +Module size +----------- + +Differences between the two libraries become much more pronounced when +considering the file size of the generated Python plugin: for the largest file, +the binary generated by Boost.Python required 16.8 MiB, which was **2.17 +times** / **9.1 megabytes** larger than the output generated by pybind11. For +very small inputs, Boost.Python has an edge in the plot below -- however, note +that it stores many definitions in an external library, whose size was not +included here, hence the comparison is slightly shifted in Boost.Python's +favor. + +.. only:: not latex + + .. image:: pybind11_vs_boost_python2.svg + +.. only:: latex + + .. image:: pybind11_vs_boost_python2.png diff --git a/external_libraries/pybind11/docs/changelog.md b/external_libraries/pybind11/docs/changelog.md new file mode 100644 index 00000000..ecff9705 --- /dev/null +++ b/external_libraries/pybind11/docs/changelog.md @@ -0,0 +1,3488 @@ +# Changelog + + + +Starting with version 1.8.0, pybind11 releases use a [semantic +versioning](http://semver.org) policy. + +Changes will be added here periodically from the "Suggested changelog +entry" block in pull request descriptions. + + +## Version 3.0.4 (April 18, 2026) + +Bug fixes: + +- Fixed test builds with installed Eigen 5 by improving `Eigen3` CMake package detection. + [#6036](https://github.com/pybind/pybind11/pull/6036) + +- Fixed move semantics of `scoped_ostream_redirect` to preserve buffered output and avoid crashes when moved redirects restore stream buffers. + [#6033](https://github.com/pybind/pybind11/pull/6033) + +- Fixed `py::dynamic_attr()` traversal on Python 3.13+ to correctly propagate `PyObject_VisitManagedDict()` results. + [#6032](https://github.com/pybind/pybind11/pull/6032) + +- Fixed `std::shared_ptr` fallback casting to avoid unnecessary copy-constructor instantiation in `reference_internal` paths. + [#6028](https://github.com/pybind/pybind11/pull/6028) + +CI: + +- Updated `setup-uv` to the maintained GitHub Action tag scheme. + [#6035](https://github.com/pybind/pybind11/pull/6035) + +- Updated pre-commit hooks. + [#6029](https://github.com/pybind/pybind11/pull/6029) + +- Updated GitHub Actions dependencies, including `actions-setup-cmake` and `cibuildwheel`. + [#6027](https://github.com/pybind/pybind11/pull/6027) + + +## Version 3.0.3 (March 31, 2026) + +Bug fixes: + +- Fixed TSS key exhaustion in `implicitly_convertible()` when many implicit conversions are registered across large module sets. + [#6020](https://github.com/pybind/pybind11/pull/6020) + +- Fixed heap-buffer-overflow in `pythonbuf` with undersized buffers by enforcing a minimum buffer size. + [#6019](https://github.com/pybind/pybind11/pull/6019) + +- Fixed virtual-inheritance pointer offset crashes when dispatching inherited methods through virtual bases. + [#6017](https://github.com/pybind/pybind11/pull/6017) + +- Fixed `free(): invalid pointer` crashes during interpreter shutdown with `py::enum_<>` by duplicating late-added `def_property_static` argument strings. + [#6015](https://github.com/pybind/pybind11/pull/6015) + +- Fixed `function_record` heap-type deallocation to call `PyObject_Free()` and decref the type. + [#6010](https://github.com/pybind/pybind11/pull/6010) + +- Hardened `PYBIND11_MODULE_PYINIT` and `get_internals()` against module-initialization crashes. + [#6018](https://github.com/pybind/pybind11/pull/6018) + +- Fixed `static_pointer_cast` build failure with virtual inheritance in `holder_caster_foreign_helpers.h`. + [#6014](https://github.com/pybind/pybind11/pull/6014) + +- Fixed ambiguous `factory` template specialization that caused compilation failures with nvcc + GCC 14. + [#6011](https://github.com/pybind/pybind11/pull/6011) + +- Fixed crash in `def_readwrite` for non-smart-holder properties of smart-holder classes. + [#6008](https://github.com/pybind/pybind11/pull/6008) + +- Fixed memory leak for `py::dynamic_attr()` objects on Python 3.13+ by clearing managed `__dict__` contents during deallocation. + [#5999](https://github.com/pybind/pybind11/pull/5999) + +- Fixed binding of `noexcept` and ref-qualified (`&`, `&&`) methods inherited from unregistered base classes. + [#5992](https://github.com/pybind/pybind11/pull/5992) + +Internal: + +- Moved `tomlkit` dependency to the dev dependency group. + [#5990](https://github.com/pybind/pybind11/pull/5990) + +- Switched to newer public CPython APIs (`PyType_GetFlags` and public vectorcall APIs where available). + [#6005](https://github.com/pybind/pybind11/pull/6005) + +Tests: + +- Made an async callback test deterministic by replacing fixed sleep with bounded waiting. + [#5986](https://github.com/pybind/pybind11/pull/5986) + +CI: + +- Re-enabled Android tests in the cibuildwheel workflow. + [#6001](https://github.com/pybind/pybind11/pull/6001) + + +## Version 3.0.2 (February 16, 2026) + +New Features: + +- Added helper functions to `py::array` that return shape and strides as `std::span` when available. + [#5974](https://github.com/pybind/pybind11/pull/5974) + +Bug fixes: + +- Added fallback locking for Python 3.13t where `PyCriticalSection_BeginMutex` is unavailable. + [#5981](https://github.com/pybind/pybind11/pull/5981) + +- Fixed race condition in `py::make_key_iterator` with free-threaded Python. + [#5971](https://github.com/pybind/pybind11/pull/5971) + +- MSVC 19.16 and earlier were blocked from using `std::launder` due to internal compiler errors. + [#5968](https://github.com/pybind/pybind11/pull/5968) + +- Internals destructors were updated to check the owning interpreter before clearing Python objects. + [#5965](https://github.com/pybind/pybind11/pull/5965) + +- Internals shutdown handling was refined in two iterations before release: an initial finalization-time cleanup was followed by a safety adjustment to avoid late-shutdown `py::cast` segfaults. + [#5958](https://github.com/pybind/pybind11/pull/5958) + [#5972](https://github.com/pybind/pybind11/pull/5972) + +- Fixed ambiguous `str(handle)` construction for `object`-derived types like `kwargs` or `dict` by templatizing the constructor with SFINAE. + [#5949](https://github.com/pybind/pybind11/pull/5949) + +- Fixed concurrency consistency for `internals_pp_manager` under multiple-interpreters. + [#5947](https://github.com/pybind/pybind11/pull/5947) + +- Fixed MSVC LNK2001 in C++20 builds when /GL (whole program optimization) is enabled. + [#5939](https://github.com/pybind/pybind11/pull/5939) + +- Added per-interpreter storage for `gil_safe_call_once_and_store` to make it safe under multi-interpreters. + [#5933](https://github.com/pybind/pybind11/pull/5933) + +- A workaround for a GCC `-Warray-bounds` false positive in `argument_vector` was added. + [#5908](https://github.com/pybind/pybind11/pull/5908) + +- Corrected a mistake where support for `__index__` was added, but the type hints did not reflect acceptance of `SupportsIndex` objects. Also fixed a long-standing bug: the complex-caster did not accept `__index__` in `convert` mode. + [#5891](https://github.com/pybind/pybind11/pull/5891) + +- Fixed `*args/**kwargs` return types. Added type hinting to `py::make_tuple`. + [#5881](https://github.com/pybind/pybind11/pull/5881) + +- Fixed compiler error in `type_caster_generic` when casting a `T` implicitly convertible from `T*`. + [#5873](https://github.com/pybind/pybind11/pull/5873) + +- Updated `py::native_enum` bindings to unregister enum types on destruction, preventing a use-after-free when returning a destroyed enum instance. + [#5871](https://github.com/pybind/pybind11/pull/5871) + +- Fixed undefined behavior that occurred when importing pybind11 modules from non-main threads created by C API modules or embedded python interpreters. + [#5870](https://github.com/pybind/pybind11/pull/5870) + +- Fixed dangling pointer in `internals::registered_types_cpp_fast`. + [#5867](https://github.com/pybind/pybind11/pull/5867) + +- Added support for `std::shared_ptr` when loading module-local or conduit types from other modules. + [#5862](https://github.com/pybind/pybind11/pull/5862) + +- Fixed thread-safety issues if types were concurrently registered while `get_local_type_info()` was called in free threaded Python. + [#5856](https://github.com/pybind/pybind11/pull/5856) + +- Fixed py::float_ casting and py::int_ and py::float_ type hints. + [#5839](https://github.com/pybind/pybind11/pull/5839) + +- Fixed two `smart_holder` bugs in `shared_ptr` and `unique_ptr` adoption with multiple/virtual inheritance: + - `shared_ptr` to-Python caster was updated to register the correct subobject pointer (fixes #5786). + - `unique_ptr` adoption was updated to own the proper object start while aliasing subobject pointers for registration, which fixed MSVC crashes during destruction. + [#5836](https://github.com/pybind/pybind11/pull/5836) + +- Constrained `accessor::operator=` templates to avoid obscuring special members. + [#5832](https://github.com/pybind/pybind11/pull/5832) + +- Fixed crash that can occur when finalizers acquire and release the GIL. + [#5828](https://github.com/pybind/pybind11/pull/5828) + +- Fixed compiler detection in `pybind11/detail/pybind11_namespace_macros.h` for clang-cl on Windows, to address warning suppression macros. + [#5816](https://github.com/pybind/pybind11/pull/5816) + +- Fixed compatibility with CMake policy CMP0190 by not always requiring a Python interpreter when cross-compiling. + [#5829](https://github.com/pybind/pybind11/pull/5829) + +- Added a static assertion to disallow `keep_alive` and `call_guard` on properties. + [#5533](https://github.com/pybind/pybind11/pull/5533) + +Internal: + +- CMake policy limit was set to 4.1. + [#5944](https://github.com/pybind/pybind11/pull/5944) + +- Improved performance of function calls between Python and C++ by switching to the "vectorcall" calling protocol. + [#5948](https://github.com/pybind/pybind11/pull/5948) + +- Many C-style casts were replaced with C++-style casts. + [#5930](https://github.com/pybind/pybind11/pull/5930) + +- Added `cast_sources` abstraction to `type_caster_generic`. + [#5866](https://github.com/pybind/pybind11/pull/5866) + +- Improved the performance of from-Python conversions of legacy pybind11 enum objects bound by `py::enum_`. + [#5860](https://github.com/pybind/pybind11/pull/5860) + +- Reduced size overhead by deduplicating functions' readable signatures and type information. + [#5857](https://github.com/pybind/pybind11/pull/5857) + +- Used new Python 3.14 C APIs when available. + [#5854](https://github.com/pybind/pybind11/pull/5854) + +- Improved performance of function dispatch and type casting by porting two-level type info lookup strategy from nanobind. + [#5842](https://github.com/pybind/pybind11/pull/5842) + +- Updated `.gitignore` to exclude `__pycache__/` directories. + [#5838](https://github.com/pybind/pybind11/pull/5838) + +- Changed internals to use `thread_local` instead of `thread_specific_storage` for increased performance. + [#5834](https://github.com/pybind/pybind11/pull/5834) + +- Reduced function call overhead by using thread_local for loader_life_support when possible. + [#5830](https://github.com/pybind/pybind11/pull/5830) + +- Removed heap allocation for the C++ argument array when dispatching functions with 6 or fewer arguments. + [#5824](https://github.com/pybind/pybind11/pull/5824) + + +Documentation: + +- Fixed docstring for `long double` complex types to use `numpy.clongdouble` instead of the deprecated `numpy.longcomplex` (removed in NumPy 2.0). + [#5952](https://github.com/pybind/pybind11/pull/5952) + +- The "Supported compilers" and "Supported platforms" sections in the main `README.rst` were replaced with a new "Supported platforms & compilers" section that points to the CI test matrix as the living source of truth. + [#5910](https://github.com/pybind/pybind11/pull/5910) + +- Fixed documentation formatting. + [#5903](https://github.com/pybind/pybind11/pull/5903) + +- Updated upgrade notes for `py::native_enum`. + [#5885](https://github.com/pybind/pybind11/pull/5885) + +- Clarified in the docs to what extent bindings are global. + [#5859](https://github.com/pybind/pybind11/pull/5859) + + +Tests: + +- Fixed deadlock in a free-threading test by releasing the GIL while waiting on synchronization. + [#5973](https://github.com/pybind/pybind11/pull/5973) + +- Calls to `env.deprecated_call()` were replaced with direct calls to `pytest.deprecated_call()`. + [#5893](https://github.com/pybind/pybind11/pull/5893) + +- Updated pytest configuration to use `log_level` instead of `log_cli_level`. + [#5890](https://github.com/pybind/pybind11/pull/5890) + + +CI: + +- Added CI tests for windows-11-arm with clang/MSVC (currently python 3.13), windows-11-arm with clang/mingw (currently python 3.12). + [#5932](https://github.com/pybind/pybind11/pull/5932) + +- These clang-tidy rules were added: `readability-redundant-casting`, `readability-redundant-inline-specifier`, `readability-redundant-member-init` + [#5924](https://github.com/pybind/pybind11/pull/5924) + +- Replaced deprecated macos-13 runners with macos-15-intel in CI. + [#5916](https://github.com/pybind/pybind11/pull/5916) + +- Restored `runs-on: windows-latest` in CI. + [#5835](https://github.com/pybind/pybind11/pull/5835) + +## Version 3.0.1 (August 22, 2025) + +Bug fixes: + +- Fixed compilation error in `type_caster_enum_type` when casting + pointer-to-enum types. Added pointer overload to handle dereferencing before + enum conversion. + [#5776](https://github.com/pybind/pybind11/pull/5776) + +- Implement binary version of `make_index_sequence` to reduce template depth + requirements for functions with many parameters. + [#5751](https://github.com/pybind/pybind11/pull/5751) + +- Subinterpreter-specific exception handling code was removed to resolve segfaults. + [#5795](https://github.com/pybind/pybind11/pull/5795) + +- Fixed issue that caused ``PYBIND11_MODULE`` code to run again if the module + was re-imported after being deleted from ``sys.modules``. + [#5782](https://github.com/pybind/pybind11/pull/5782) + +- Prevent concurrent creation of sub-interpreters as a workaround for stdlib + concurrency issues in Python 3.12. + [#5779](https://github.com/pybind/pybind11/pull/5779) + +- Fixed potential crash when using `cpp_function` objects with sub-interpreters. + [#5771](https://github.com/pybind/pybind11/pull/5771) + +- Fixed non-entrant check in `implicitly_convertible()`. + [#5777](https://github.com/pybind/pybind11/pull/5777) + +- Support C++20 on platforms that have older c++ runtimes. + [#5761](https://github.com/pybind/pybind11/pull/5761) + +- Fix compilation with clang on msys2. + [#5757](https://github.com/pybind/pybind11/pull/5757) + +- Avoid `nullptr` dereference warning with GCC 13.3.0 and python 3.11.13. + [#5756](https://github.com/pybind/pybind11/pull/5756) + +- Fix potential warning about number of threads being too large. + [#5807](https://github.com/pybind/pybind11/pull/5807) + + + + +- Fix gcc 11.4+ warning about serial compilation using CMake. + [#5791](https://github.com/pybind/pybind11/pull/5791) + + +Documentation: + +- Improve `buffer_info` type checking in numpy docs. + [#5805](https://github.com/pybind/pybind11/pull/5805) + +- Replace `robotpy-build` with `semiwrap` in the binding tool list. + [#5804](https://github.com/pybind/pybind11/pull/5804) + +- Show nogil in most examples. + [#5770](https://github.com/pybind/pybind11/pull/5770) + +- Fix `py::trampoline_self_life_support` visibility in docs. + [#5766](https://github.com/pybind/pybind11/pull/5766) + + +Tests: + +- Avoid a spurious warning about `DOWNLOAD_CATCH` being manually specified. + [#5803](https://github.com/pybind/pybind11/pull/5803) + +- Fix an IsolatedConfig test. + [#5768](https://github.com/pybind/pybind11/pull/5768) + + +CI: + +- Add CI testing for Android. + [#5714](https://github.com/pybind/pybind11/pull/5714) + + +Internal: + +- Rename internal variables to avoid the word `slots` (reads better). + [#5793](https://github.com/pybind/pybind11/pull/5793) + + +## Version 3.0.0 (July 10, 2025) + +Pybind11 3.0 includes an ABI bump, the first required bump in many years +on Unix (Windows has had required bumps more often). This release contains +the smart-holder branch, multi-phase init and subinterpreter support, +`py::native_enum`, an interface to warnings, typing improvements, and more. +CMake now defaults to FindPython mode. Please check our upgrade guide for +more info on upgrading! + +Support for Python 3.14, 3.14t, GraalPy, and PyPy 3.11 has been added, while +legacy support for Python 3.7, PyPy 3.8/3.9, and CMake \<3.15 has been removed. +Most deprecated features have been kept for this release, but anything +producing a warning in 3.0 may be removed in a future 3.x version. We also now +have a deprecation page. + +New Features: + +- The `smart_holder` branch has been merged, enabling + `py::class_`, which handles two-way conversion + with `std::unique_ptr` and `std::shared_ptr` (simultaneously), + disowning a Python object being passed to `std::unique_ptr`, + trampoline objects, and `std::enable_shared_from_this`. + [#5542](https://github.com/pybind/pybind11/pull/5542) + + - Added support for `std::shared_ptr` in `py::init()` when using + `py::smart_holder`, complementing existing support for + `std::unique_ptr`. + [#5731](https://github.com/pybind/pybind11/pull/5731) + + - Support const-only smart pointers. + [#5718](https://github.com/pybind/pybind11/pull/5718) + + - Eliminate cross-DSO RTTI reliance from `trampoline_self_life_support` functionality, `smart_holder` deleter detection, and other + `smart_holder` bookkeeping. Resolves platform-specific issues on macOS related to cross-DSO `dynamic_cast` and `typeid` mismatches. + [#5728](https://github.com/pybind/pybind11/pull/5728) (replaces [#5700](https://github.com/pybind/pybind11/pull/5700)) + +- Changed `PYBIND11_MODULE` macro implementation to perform multi-phase + module initialization (PEP 489) behind the scenes. + [#5574](https://github.com/pybind/pybind11/pull/5574) and avoid destruction + [#5688](https://github.com/pybind/pybind11/pull/5688) + +- Support for sub-interpreters (both isolated (with separate GILs) and + legacy (with a global GIL). Add the + `py::multiple_interpreters::per_interpreter_gil()` tag (or, + `py::multiple_interpreters::shared_gil()` for legacy interpreter + support) to `PYBIND11_MODULE` calls (as the third parameter) to + indicate that a module supports running with sub-interpreters. + [#5564](https://github.com/pybind/pybind11/pull/5564) + + - Rename macro `PYBIND11_SUBINTERPRETER_SUPPORT` -> `PYBIND11_HAS_SUBINTERPRETER_SUPPORT` to meet naming convention. + [#5682](https://github.com/pybind/pybind11/pull/5682) + + - Allow subinterpreter support to be disabled if defined to 0. This is mostly an emergency workaround, and is not exposed in CMake. + [#5708](https://github.com/pybind/pybind11/pull/5708) and [#5710](https://github.com/pybind/pybind11/pull/5710) + + - Modify internals pointer-to-pointer implementation to not use `thread_local` (better iOS support). + [#5709](https://github.com/pybind/pybind11/pull/5709) + + - Support implementations without subinterpreter support. + [#5732](https://github.com/pybind/pybind11/pull/5732) + +- Changed `PYBIND11_EMBEDDED_MODULE` macro implementation to perform + multi-phase module initialization (PEP 489) behind the scenes and to + support `py::mod_gil_not_used()`, + `py::multiple_interpreters::per_interpreter_gil()` and + `py::multiple_interpreters::shared_gil()`. + [#5665](https://github.com/pybind/pybind11/pull/5665) and consolidate code + [#5670](https://github.com/pybind/pybind11/pull/5670). + +- Added API in `pybind11/subinterpreter.h` for embedding sub-interpreters (requires Python 3.12+). + [#5666](https://github.com/pybind/pybind11/pull/5666) + +- `py::native_enum` was added, for conversions between Python's native + (stdlib) enum types and C++ enums. + [#5555](https://github.com/pybind/pybind11/pull/5555) + + - Add class doc string to `py::native_enum`. + [#5617](https://github.com/pybind/pybind11/pull/5617). + + - Fix signature for functions with a `native_enum` in the signature. + [#5619](https://github.com/pybind/pybind11/pull/5619) + +- Support `py::numpy_scalar<>` / `py::make_scalar()` for NumPy types. + [#5726](https://github.com/pybind/pybind11/pull/5726) + +- A `py::release_gil_before_calling_cpp_dtor` option (for `py::class_`) + was added to resolve the long-standing issue \#1446. + [#5522](https://github.com/pybind/pybind11/pull/5522) + +- Add `dtype::normalized_num` and `dtype::num_of`. + [#5429](https://github.com/pybind/pybind11/pull/5429) + +- Add support for `array_t` and `array_t`. + [#5427](https://github.com/pybind/pybind11/pull/5427) + +- Added `py::warnings` namespace with `py::warnings::warn` and + `py::warnings::new_warning_type` that provides the interface for + Python warnings. + [#5291](https://github.com/pybind/pybind11/pull/5291) + +- `stl.h` `list|set|map_caster` were made more user friendly: it is no + longer necessary to explicitly convert Python iterables to `tuple()`, + `set()`, or `map()` in many common situations. + [#4686](https://github.com/pybind/pybind11/pull/4686) + +- The `array_caster` in pybind11/stl.h was enhanced to support value + types that are not default-constructible. + [#5305](https://github.com/pybind/pybind11/pull/5305) + +- `pybind11/conduit/pybind11_platform_abi_id.h` was factored out, to + maximize reusability of `PYBIND11_PLATFORM_ABI_ID` (for other + Python/C++ binding systems). Separately, a note was added to explain + that the conduit feature only covers from-Python-to-C++ conversions. + [#5375](https://github.com/pybind/pybind11/pull/5375) \| + [#5740](https://github.com/pybind/pybind11/pull/5740) + +- Added support for finding pybind11 using pkgconf distributed on pypi. + [#5552](https://github.com/pybind/pybind11/pull/5552) + +- Support `--extension-suffix` on the pybind11 command. + [#5360](https://github.com/pybind/pybind11/pull/5360) + +- Add semi-public API: `pybind11::detail::is_holder_constructed` and + update example for `pybind11::custom_type_setup` in documentation. + [#5669](https://github.com/pybind/pybind11/pull/5669) + +- Added `py::scoped_critical_section` to support free-threaded mode. + [#5684](https://github.com/pybind/pybind11/pull/5684) \| + [#5706](https://github.com/pybind/pybind11/pull/5706) + +New Features / fixes (typing): + +- Added option for different arg/return type hints to `type_caster`. + Updated `stl/filesystem` to use correct arg/return type hints. Updated + `pybind11::typing` to use correct arg/return type hints for nested + types. [#5450](https://github.com/pybind/pybind11/pull/5450) +- Updated type hint for `py::capsule` to `type.CapsuleType`. + [#5567](https://github.com/pybind/pybind11/pull/5567) +- Adds support for `typing.SupportsInt` and `typing.SupportsFloat`. + Update `Final` to be narrower type hint. Make `std::function` match + `Callable` type. Fix `io_name` bug in `attr_with_type_hint`. + [#5540](https://github.com/pybind/pybind11/pull/5540) +- Rework of arg/return type hints to support `.noconvert()`. + [#5486](https://github.com/pybind/pybind11/pull/5486) +- Add `attr_with_type` for declaring attribute types and `Final`, + `ClassVar` type annotations. + [#5460](https://github.com/pybind/pybind11/pull/5460) +- Allow annotate methods with `py::pos_only` when only have the `self` + argument. Make arguments for auto-generated dunder methods + positional-only. + [#5403](https://github.com/pybind/pybind11/pull/5403) +- Added `py::Args` and `py::KWArgs` to enable custom type hinting of + `*args` and `**kwargs` (see PEP 484). + [#5357](https://github.com/pybind/pybind11/pull/5357) +- Switched to `numpy.typing.NDArray` and `numpy.typing.ArrayLike`. + [#5212](https://github.com/pybind/pybind11/pull/5212) +- Use `numpy.object_` instead of `object`. + [#5571](https://github.com/pybind/pybind11/pull/5571) +- Fix module type hint. + [#5469](https://github.com/pybind/pybind11/pull/5469) +- Fix Buffer type hint. + [#5662](https://github.com/pybind/pybind11/pull/5662) +- Added support for `collections.abc` in type hints and convertible + checks of STL casters and `py::buffer`. + [#5566](https://github.com/pybind/pybind11/pull/5566) +- Fix `typing` and `collections.abc` type hint ambiguity. + [#5663](https://github.com/pybind/pybind11/pull/5663) +- Add `typing_extensions` alternatives for all types that need them. + [#5693](https://github.com/pybind/pybind11/pull/5693) + +Removals: + +- Remove support for pybind11 v2 internals versions (4, 5, 6). (The + internals version number has been bumped for pybind11 v3.) + [#5512](https://github.com/pybind/pybind11/pull/5512) \| + [#5530](https://github.com/pybind/pybind11/pull/5530) +- Remove `make_simple_namespace` (added in 2.8.0, deprecated in 2.8.1). + [#5597](https://github.com/pybind/pybind11/pull/5597) +- Legacy-mode option `PYBIND11_NUMPY_1_ONLY` has been removed. + [#5595](https://github.com/pybind/pybind11/pull/5595) +- Add a deprecation warning to `.get_type` (deprecated in pybind11 2.6 + in 2020). [#5596](https://github.com/pybind/pybind11/pull/5596) + +Bug fixes: + +- Set `__file__` on submodules. + [#5584](https://github.com/pybind/pybind11/pull/5584). Except on + embedded modules. + [#5650](https://github.com/pybind/pybind11/pull/5650) +- pybind11-bound functions are now pickleable. + [#5580](https://github.com/pybind/pybind11/pull/5580) +- Fix bug in `attr_with_type_hint` to allow objects to be in + `attr_with_type_hint`. + [#5576](https://github.com/pybind/pybind11/pull/5576) +- A `-Wmaybe-uninitialized` warning suppression was added in + `pybind11/eigen/matrix.h`. + [#5516](https://github.com/pybind/pybind11/pull/5516) +- `PYBIND11_WARNING_POP` was incorrectly defined as + `PYBIND11_PRAGMA(clang diagnostic push)`. + [#5448](https://github.com/pybind/pybind11/pull/5448) +- `PYBIND11_PLATFORM_ABI_ID` (which is used in composing + `PYBIND11_INTERNALS_ID`) was modernized to reflect actual ABI + compatibility more accurately. + [#4953](https://github.com/pybind/pybind11/pull/4953) \| + [#5439](https://github.com/pybind/pybind11/pull/5439) +- Fix buffer protocol implementation. + [#5407](https://github.com/pybind/pybind11/pull/5407) +- Fix iterator increment operator does not skip first item. + [#5400](https://github.com/pybind/pybind11/pull/5400) +- When getting or deleting an element in a container bound by + `bind_map`, print the key in `KeyError` if it does not exist. + [#5397](https://github.com/pybind/pybind11/pull/5397) +- `pybind11::builtin_exception` is now explicitly exported when linked + to libc++. [#5390](https://github.com/pybind/pybind11/pull/5390) +- Allow subclasses of `py::args` and `py::kwargs`. + [#5381](https://github.com/pybind/pybind11/pull/5381) +- Disable false-positive GCC 12 Bound Check warning. + [#5355](https://github.com/pybind/pybind11/pull/5355) +- Update the dict when restoring pickles, instead of assigning a + replacement dict. + [#5658](https://github.com/pybind/pybind11/pull/5658) +- Properly define `_DEBUG` macro to `1` instead of defining it without + value. [#5639](https://github.com/pybind/pybind11/pull/5639) +- Fix a missing time cast causing a compile error for newer ICC. + [#5621](https://github.com/pybind/pybind11/pull/5621) +- Change the behavior of the default constructor of `py::slice` to be + equivalent to `::` in Python. + [#5620](https://github.com/pybind/pybind11/pull/5620) + +Bug fixes and features (CMake): + +- Enable FindPython mode by default, with a `COMPAT` mode that + sets some of the old variables to ease transition. + [#5553](https://github.com/pybind/pybind11/pull/5553) +- Add an author warning that auto-calculated `PYTHON_MODULE_EXTENSION` + may not respect `SETUPTOOLS_EXT_SUFFIX` during cross-compilation. + [#5495](https://github.com/pybind/pybind11/pull/5495) +- Don't strip with `CMAKE_BUILD_TYPE` None. + [#5392](https://github.com/pybind/pybind11/pull/5392) +- Fix an issue with `NO_EXTRAS` adding `pybind11::windows_extras` + anyway. [#5378](https://github.com/pybind/pybind11/pull/5378) +- Fix issue with NEW/OLD message showing up. + [#5656](https://github.com/pybind/pybind11/pull/5656) +- Use CMake's warnings as errors if available (CMake 3.24+). + [#5612](https://github.com/pybind/pybind11/pull/5612) +- Add support for running pybind11's tests via presets in CMake 3.25+. + [#5655](https://github.com/pybind/pybind11/pull/5655) and support `--fresh`. + [#5668](https://github.com/pybind/pybind11/pull/5668) +- Experimental CMake support for Android. + [#5733](https://github.com/pybind/pybind11/pull/5733) +- Presets now generate `compile_commands.json`. + [#5685](https://github.com/pybind/pybind11/pull/5685) + +Bug fixes (free-threading): + +- Fix data race in free threaded CPython when accessing a shared static + variable. [#5494](https://github.com/pybind/pybind11/pull/5494) +- A free-threading data race in `all_type_info()` was fixed. + [#5419](https://github.com/pybind/pybind11/pull/5419) +- Added exception translator specific mutex used with + `try_translate_exceptions` in the free-threaded build for internal + locking. [#5362](https://github.com/pybind/pybind11/pull/5362) + +Internals: + +- Consolidated all `PYBIND11_HAS_...` feature macros into + `pybind11/detail/common.h` to streamline backward compatibility checks and + simplify internal refactoring. This change ensures consistent macro + availability regardless of header inclusion order. + [#5647](https://github.com/pybind/pybind11/pull/5647) + +- `pybind11/gil_simple.h` was factored out from `pybind11/gil.h`, so + that it can easily be reused. + [#5614](https://github.com/pybind/pybind11/pull/5614) + +- Use CPython macros to construct `PYBIND11_VERSION_HEX`. + [#5683](https://github.com/pybind/pybind11/pull/5683) + +Documentation: + +- Improved `reference_internal` policy documentation. + [#5528](https://github.com/pybind/pybind11/pull/5528) + +- A new "Double locking, deadlocking, GIL" document was added. + [#5394](https://github.com/pybind/pybind11/pull/5394) + +- Add documenting for free-threading and subinterpreters. + [#5659](https://github.com/pybind/pybind11/pull/5659) + +Tests: + +- Download the final Catch2 2.x release if Catch download is requested. + [#5568](https://github.com/pybind/pybind11/pull/5568) + +- Explicitly used `signed char` for two numpy dtype tests. As seen when + compiling using `clang` on Linux with the `-funsigned-char` flag. + [#5545](https://github.com/pybind/pybind11/pull/5545) + +- CI testing now includes + `-Wwrite-strings -Wunreachable-code -Wpointer-arith -Wredundant-decls` + in some jobs. + [#5523](https://github.com/pybind/pybind11/pull/5523) + +- Add nightly wheels to scientific-python's nightly wheelhouse. + [#5675](https://github.com/pybind/pybind11/pull/5675) + +- Expect free-threaded warning when loading a non-free-threaded module. + [#5680](https://github.com/pybind/pybind11/pull/5680) + +- Run pytest under Python devmode. + [#5715](https://github.com/pybind/pybind11/pull/5715) + +New and removed platforms: + +- Support Python 3.14 (beta 1+). + [#5646](https://github.com/pybind/pybind11/pull/5646) + +- Added support for GraalPy Python implementation + (). + [#5380](https://github.com/pybind/pybind11/pull/5380) + +- Support and test iOS in CI. + [#5705](https://github.com/pybind/pybind11/pull/5705) + +- Support for PyPy 3.11 added. + [#5508](https://github.com/pybind/pybind11/pull/5508) + And test in ci. + [#5534](https://github.com/pybind/pybind11/pull/5534) + +- Support for PyPy 3.8 and 3.9 was dropped. + [#5578](https://github.com/pybind/pybind11/pull/5578) + +- Support for Python 3.7 was removed. (Official end-of-life: + 2023-06-27). + [#5191](https://github.com/pybind/pybind11/pull/5191) + +- Support for CMake older than 3.15 removed. CMake 3.15-4.0 supported. + [#5304](https://github.com/pybind/pybind11/pull/5304) and fix regression [#5691](https://github.com/pybind/pybind11/pull/5691). + +- Use scikit-build-core for the build backend for the PyPI `pybind11`. + The CMake generation has been moved to the sdist-\>wheel step. + `PYBIND11_GLOBAL_SDIST` has been removed. + [#5598](https://github.com/pybind/pybind11/pull/5598) and updated + docs/ci. [#5676](https://github.com/pybind/pybind11/pull/5676) + +- clang 20 tested and used for clang-tidy. + [#5692](https://github.com/pybind/pybind11/pull/5692) + +- Drop testing on MSVC 2019 (as it is being removed from GitHub). + [#5712](https://github.com/pybind/pybind11/pull/5712) + +- Support Windows C++20 and Linux C++23 in tests. + [#5707](https://github.com/pybind/pybind11/pull/5707) + +## Version 2.13.6 (September 13, 2024) + +New Features: + +- A new `self._pybind11_conduit_v1_()` method is automatically added to + all `py::class_`-wrapped types, to enable type-safe interoperability + between different independent Python/C++ bindings systems, including + pybind11 versions with different `PYBIND11_INTERNALS_VERSION`'s. + Supported on pybind11 2.11.2, 2.12.1, and 2.13.6+. + [#5296](https://github.com/pybind/pybind11/pull/5296) + +Bug fixes: + +- Using `__cpp_nontype_template_args` instead of + `__cpp_nontype_template_parameter_class`. + [#5330](https://github.com/pybind/pybind11/pull/5330) +- Properly translate C++ exception to Python exception when creating + Python buffer from wrapped object. + [#5324](https://github.com/pybind/pybind11/pull/5324) + +Documentation: + +- Adds an answer (FAQ) for "What is a highly conclusive and simple way + to find memory leaks?". + [#5340](https://github.com/pybind/pybind11/pull/5340) + +## Version 2.13.5 (August 22, 2024) + +Bug fixes: + +- Fix includes when using Windows long paths (`\\?\` prefix). + [#5321](https://github.com/pybind/pybind11/pull/5321) +- Support `-Wpedantic` in C++20 mode. + [#5322](https://github.com/pybind/pybind11/pull/5322) +- Fix and test `` support for `py::tuple` and `py::list`. + [#5314](https://github.com/pybind/pybind11/pull/5314) + +## Version 2.13.4 (August 14, 2024) + +Bug fixes: + +- Fix paths with spaces, including on Windows. (Replaces regression from + [#5302](https://github.com/pybind/pybind11/pull/5302)) + [#4874](https://github.com/pybind/pybind11/pull/4874) + +Documentation: + +- Remove repetitive words. + [#5308](https://github.com/pybind/pybind11/pull/5308) + +## Version 2.13.3 (August 13, 2024) + +Bug fixes: + +- Quote paths from pybind11-config + [#5302](https://github.com/pybind/pybind11/pull/5302) +- Fix typo in Emscripten support when in config mode (CMake) + [#5301](https://github.com/pybind/pybind11/pull/5301) + +## Version 2.13.2 (August 13, 2024) + +New Features: + +- A `pybind11::detail::type_caster_std_function_specializations` feature + was added, to support specializations for `std::function`'s with + return types that require custom to-Python conversion behavior (to + primary use case is to catch and convert exceptions). + [#4597](https://github.com/pybind/pybind11/pull/4597) + +Changes: + +- Use `PyMutex` instead of `std::mutex` for internal locking in the + free-threaded build. + [#5219](https://github.com/pybind/pybind11/pull/5219) +- Add a special type annotation for C++ empty tuple. + [#5214](https://github.com/pybind/pybind11/pull/5214) +- When compiling for WebAssembly, add the required exception flags + (CMake 3.13+). [#5298](https://github.com/pybind/pybind11/pull/5298) + +Bug fixes: + +- Make `gil_safe_call_once_and_store` thread-safe in free-threaded + CPython. [#5246](https://github.com/pybind/pybind11/pull/5246) +- A missing `#include ` in pybind11/typing.h was added to fix + build errors (in case user code does not already depend on that + include). [#5208](https://github.com/pybind/pybind11/pull/5208) +- Fix regression introduced in \#5201 for GCC\<10.3 in C++20 mode. + [#5205](https://github.com/pybind/pybind11/pull/5205) + + + +- Remove extra = when assigning flto value in the case for Clang in + CMake. [#5207](https://github.com/pybind/pybind11/pull/5207) + +Tests: + +- Adding WASM testing to our CI (Pyodide / Emscripten via + scikit-build-core). + [#4745](https://github.com/pybind/pybind11/pull/4745) +- clang-tidy (in GitHub Actions) was updated from clang 15 to clang 18. + [#5272](https://github.com/pybind/pybind11/pull/5272) + +## Version 2.13.1 (June 26, 2024) + +New Features: + +- Add support for `Typing.Callable[..., T]`. + [#5202](https://github.com/pybind/pybind11/pull/5202) + +Bug fixes: + +- Avoid aligned allocation in free-threaded build in order to support + macOS versions before 10.14. + [#5200](https://github.com/pybind/pybind11/pull/5200) + +## Version 2.13.0 (June 25, 2024) + +New Features: + +- Support free-threaded CPython (3.13t). Add `py::mod_gil_not_used()` + tag to indicate if a module supports running with the GIL disabled. + [#5148](https://github.com/pybind/pybind11/pull/5148) +- Support for Python 3.6 was removed. (Official end-of-life: + 2021-12-23). [#5177](https://github.com/pybind/pybind11/pull/5177) +- `py::list` gained a `.clear()` method. + [#5153](https://github.com/pybind/pybind11/pull/5153) + + + +- Support for `Union`, `Optional`, `type[T]`, `typing.TypeGuard`, + `typing.TypeIs`, `typing.Never`, `typing.NoReturn` and + `typing.Literal` was added to `pybind11/typing.h`. + [#5166](https://github.com/pybind/pybind11/pull/5166) + [#5165](https://github.com/pybind/pybind11/pull/5165) + [#5194](https://github.com/pybind/pybind11/pull/5194) + [#5193](https://github.com/pybind/pybind11/pull/5193) + [#5192](https://github.com/pybind/pybind11/pull/5192) + + + +- In CMake, if `PYBIND11_USE_CROSSCOMPILING` is enabled, then + `CMAKE_CROSSCOMPILING` will be respected and will keep pybind11 from + accessing the interpreter during configuration. Several CMake + variables will be required in this case, but can be deduced from the + environment variable `SETUPTOOLS_EXT_SUFFIX`. The default (currently + `OFF`) may be changed in the future. + [#5083](https://github.com/pybind/pybind11/pull/5083) + +Bug fixes: + +- A refcount bug (leading to heap-use-after-free) involving trampoline + functions with `PyObject *` return type was fixed. + [#5156](https://github.com/pybind/pybind11/pull/5156) +- Return `py::ssize_t` from `.ref_count()` instead of `int`. + [#5139](https://github.com/pybind/pybind11/pull/5139) +- A subtle bug involving C++ types with unusual `operator&` overrides + was fixed. [#5189](https://github.com/pybind/pybind11/pull/5189) +- Support Python 3.13 with minor fix, add to CI. + [#5127](https://github.com/pybind/pybind11/pull/5127) + + + +- Fix mistake affecting old cmake and old boost. + [#5149](https://github.com/pybind/pybind11/pull/5149) + +Documentation: + +- Build docs updated to feature scikit-build-core and meson-python, and + updated setuptools instructions. + [#5168](https://github.com/pybind/pybind11/pull/5168) + +Tests: + +- Avoid immortal objects in tests. + [#5150](https://github.com/pybind/pybind11/pull/5150) + +CI: + +- Compile against Python 3.13t in CI. +- Use `macos-13` (Intel) for CI jobs for now (will drop Python 3.7 + soon). [#5109](https://github.com/pybind/pybind11/pull/5109) +- Releases now have artifact attestations, visible at + . + [#5196](https://github.com/pybind/pybind11/pull/5196) + +Other: + +- Some cleanup in preparation for 3.13 support. + [#5137](https://github.com/pybind/pybind11/pull/5137) +- Avoid a warning by ensuring an iterator end check is included in + release mode. [#5129](https://github.com/pybind/pybind11/pull/5129) +- Bump max cmake to 3.29. + [#5075](https://github.com/pybind/pybind11/pull/5075) +- Update docs and noxfile. + [#5071](https://github.com/pybind/pybind11/pull/5071) + +## Version 2.12.1 (September 13, 2024) + +New Features: + +- A new `self._pybind11_conduit_v1_()` method is automatically added to + all `py::class_`-wrapped types, to enable type-safe interoperability + between different independent Python/C++ bindings systems, including + pybind11 versions with different `PYBIND11_INTERNALS_VERSION`'s. + Supported on pybind11 2.11.2, 2.12.1, and 2.13.6+. + [#5296](https://github.com/pybind/pybind11/pull/5296) + +## Version 2.12.0 (March 27, 2024) + +New Features: + +- `pybind11` now supports compiling for [NumPy + 2](https://numpy.org/devdocs/numpy_2_0_migration_guide.html). Most + code shouldn't change (see `upgrade-guide-2.12` for details). However, + if you experience issues you can define `PYBIND11_NUMPY_1_ONLY` to + disable the new support for now, but this will be removed in the + future. [#5050](https://github.com/pybind/pybind11/pull/5050) +- `pybind11/gil_safe_call_once.h` was added (it needs to be included + explicitly). The primary use case is GIL-safe initialization of C++ + `static` variables. + [#4877](https://github.com/pybind/pybind11/pull/4877) +- Support move-only iterators in `py::make_iterator`, + `py::make_key_iterator`, `py::make_value_iterator`. + [#4834](https://github.com/pybind/pybind11/pull/4834) +- Two simple `py::set_error()` functions were added and the + documentation was updated accordingly. In particular, + `py::exception<>::operator()` was deprecated (use one of the new + functions instead). The documentation for `py::exception<>` was + further updated to not suggest code that may result in undefined + behavior. [#4772](https://github.com/pybind/pybind11/pull/4772) + +Bug fixes: + +- Removes potential for Undefined Behavior during process teardown. + [#4897](https://github.com/pybind/pybind11/pull/4897) +- Improve compatibility with the nvcc compiler (especially CUDA + 12.1/12.2). [#4893](https://github.com/pybind/pybind11/pull/4893) +- `pybind11/numpy.h` now imports NumPy's `multiarray` and `_internal` + submodules with paths depending on the installed version of NumPy (for + compatibility with NumPy 2). + [#4857](https://github.com/pybind/pybind11/pull/4857) +- Builtins collections names in docstrings are now consistently rendered + in lowercase (list, set, dict, tuple), in accordance with PEP 585. + [#4833](https://github.com/pybind/pybind11/pull/4833) +- Added `py::typing::Iterator`, `py::typing::Iterable`. + [#4832](https://github.com/pybind/pybind11/pull/4832) +- Render `py::function` as `Callable` in docstring. + [#4829](https://github.com/pybind/pybind11/pull/4829) +- Also bump `PYBIND11_INTERNALS_VERSION` for MSVC, which unlocks two new + features without creating additional incompatibilities. + [#4819](https://github.com/pybind/pybind11/pull/4819) +- Guard against crashes/corruptions caused by modules built with + different MSVC versions. + [#4779](https://github.com/pybind/pybind11/pull/4779) +- A long-standing bug in the handling of Python multiple inheritance was + fixed. See PR \#4762 for the rather complex details. + [#4762](https://github.com/pybind/pybind11/pull/4762) +- Fix `bind_map` with `using` declarations. + [#4952](https://github.com/pybind/pybind11/pull/4952) +- Qualify `py::detail::concat` usage to avoid ADL selecting one from + somewhere else, such as modernjson's concat. + [#4955](https://github.com/pybind/pybind11/pull/4955) +- Use new PyCode API on Python 3.12+. + [#4916](https://github.com/pybind/pybind11/pull/4916) +- Minor cleanup from warnings reported by Clazy. + [#4988](https://github.com/pybind/pybind11/pull/4988) +- Remove typing and duplicate `class_` for + `KeysView`/`ValuesView`/`ItemsView`. + [#4985](https://github.com/pybind/pybind11/pull/4985) +- Use `PyObject_VisitManagedDict()` and `PyObject_ClearManagedDict()` on + Python 3.13 and newer. + [#4973](https://github.com/pybind/pybind11/pull/4973) +- Update `make_static_property_type()` to make it compatible with Python + 3.13. [#4971](https://github.com/pybind/pybind11/pull/4971) + + + +- Render typed iterators for `make_iterator`, `make_key_iterator`, + `make_value_iterator`. + [#4876](https://github.com/pybind/pybind11/pull/4876) +- Add several missing type name specializations. + [#5073](https://github.com/pybind/pybind11/pull/5073) +- Change docstring render for `py::buffer`, `py::sequence` and + `py::handle` (to `Buffer`, `Sequence`, `Any`). + [#4831](https://github.com/pybind/pybind11/pull/4831) +- Fixed `base_enum.__str__` docstring. + [#4827](https://github.com/pybind/pybind11/pull/4827) +- Enforce single line docstring signatures. + [#4735](https://github.com/pybind/pybind11/pull/4735) +- Special 'typed' wrappers now available in `typing.h` to annotate + tuple, dict, list, set, and function. + [#4259](https://github.com/pybind/pybind11/pull/4259) +- Create `handle_type_name` specialization to type-hint variable length + tuples. [#5051](https://github.com/pybind/pybind11/pull/5051) + + + +- Setting `PYBIND11_FINDPYTHON` to OFF will force the old FindPythonLibs + mechanism to be used. + [#5042](https://github.com/pybind/pybind11/pull/5042) +- Skip empty `PYBIND11_PYTHON_EXECUTABLE_LAST` for the first cmake run. + [#4856](https://github.com/pybind/pybind11/pull/4856) +- Fix FindPython mode exports & avoid `pkg_resources` if + `importlib.metadata` available. + [#4941](https://github.com/pybind/pybind11/pull/4941) +- `Python_ADDITIONAL_VERSIONS` (classic search) now includes 3.12. + [#4909](https://github.com/pybind/pybind11/pull/4909) +- `pybind11.pc` is now relocatable by default as long as install + destinations are not absolute paths. + [#4830](https://github.com/pybind/pybind11/pull/4830) +- Correctly detect CMake FindPython removal when used as a subdirectory. + [#4806](https://github.com/pybind/pybind11/pull/4806) +- Don't require the libs component on CMake 3.18+ when using + PYBIND11_FINDPYTHON (fixes manylinux builds). + [#4805](https://github.com/pybind/pybind11/pull/4805) +- `pybind11_strip` is no longer automatically applied when + `CMAKE_BUILD_TYPE` is unset. + [#4780](https://github.com/pybind/pybind11/pull/4780) +- Support `DEBUG_POSFIX` correctly for debug builds. + [#4761](https://github.com/pybind/pybind11/pull/4761) +- Hardcode lto/thin lto for Emscripten cross-compiles. + [#4642](https://github.com/pybind/pybind11/pull/4642) +- Upgrade maximum supported CMake version to 3.27 to fix CMP0148 + warnings. [#4786](https://github.com/pybind/pybind11/pull/4786) + +Documentation: + +- Small fix to grammar in `functions.rst`. + [#4791](https://github.com/pybind/pybind11/pull/4791) +- Remove upper bound in example pyproject.toml for setuptools. + [#4774](https://github.com/pybind/pybind11/pull/4774) + +CI: + +- CI: Update NVHPC to 23.5 and Ubuntu 20.04. + [#4764](https://github.com/pybind/pybind11/pull/4764) +- Test on PyPy 3.10. + [#4714](https://github.com/pybind/pybind11/pull/4714) + +Other: + +- Use Ruff formatter instead of Black. + [#4912](https://github.com/pybind/pybind11/pull/4912) +- An `assert()` was added to help Coverty avoid generating a false + positive. [#4817](https://github.com/pybind/pybind11/pull/4817) + +## Version 2.11.2 (September 13, 2024) + +New Features: + +- A new `self._pybind11_conduit_v1_()` method is automatically added to + all `py::class_`-wrapped types, to enable type-safe interoperability + between different independent Python/C++ bindings systems, including + pybind11 versions with different `PYBIND11_INTERNALS_VERSION`'s. + Supported on pybind11 2.11.2, 2.12.1, and 2.13.6+. + [#5296](https://github.com/pybind/pybind11/pull/5296) + +## Version 2.11.1 (July 17, 2023) + +Changes: + +- `PYBIND11_NO_ASSERT_GIL_HELD_INCREF_DECREF` is now provided as an + option for disabling the default-on `PyGILState_Check()`'s in + `pybind11::handle`'s `inc_ref()` & `dec_ref()`. + [#4753](https://github.com/pybind/pybind11/pull/4753) +- `PYBIND11_ASSERT_GIL_HELD_INCREF_DECREF` was disabled for PyPy in + general (not just PyPy Windows). + [#4751](https://github.com/pybind/pybind11/pull/4751) + +## Version 2.11.0 (July 14, 2023) + +New features: + +- The newly added `pybind11::detail::is_move_constructible` trait can be + specialized for cases in which `std::is_move_constructible` does not + work as needed. This is very similar to the long-established + `pybind11::detail::is_copy_constructible`. + [#4631](https://github.com/pybind/pybind11/pull/4631) +- Introduce `recursive_container_traits`. + [#4623](https://github.com/pybind/pybind11/pull/4623) +- `pybind11/type_caster_pyobject_ptr.h` was added to support automatic + wrapping of APIs that make use of `PyObject *`. This header needs to + included explicitly (i.e. it is not included implicitly with + `pybind/pybind11.h`). + [#4601](https://github.com/pybind/pybind11/pull/4601) +- `format_descriptor<>` & `npy_format_descriptor<>` `PyObject *` + specializations were added. The latter enables + `py::array_t` to/from-python conversions. + [#4674](https://github.com/pybind/pybind11/pull/4674) +- `buffer_info` gained an `item_type_is_equivalent_to()` member + function. [#4674](https://github.com/pybind/pybind11/pull/4674) +- The `capsule` API gained a user-friendly constructor + (`py::capsule(ptr, "name", dtor)`). + [#4720](https://github.com/pybind/pybind11/pull/4720) + +Changes: + +- `PyGILState_Check()`'s in `pybind11::handle`'s `inc_ref()` & + `dec_ref()` are now enabled by default again. + [#4246](https://github.com/pybind/pybind11/pull/4246) +- `py::initialize_interpreter()` using `PyConfig_InitPythonConfig()` + instead of `PyConfig_InitIsolatedConfig()`, to obtain complete + `sys.path`. [#4473](https://github.com/pybind/pybind11/pull/4473) +- Cast errors now always include Python type information, even if + `PYBIND11_DETAILED_ERROR_MESSAGES` is not defined. This increases + binary sizes slightly (~1.5%) but the error messages are much more + informative. [#4463](https://github.com/pybind/pybind11/pull/4463) +- The docstring generation for the `std::array`-list caster was fixed. + Previously, signatures included the size of the list in a + non-standard, non-spec compliant way. The new format conforms to + PEP 593. **Tooling for processing the docstrings may need to be + updated accordingly.** + [#4679](https://github.com/pybind/pybind11/pull/4679) +- Setter return values (which are inaccessible for all practical + purposes) are no longer converted to Python (only to be discarded). + [#4621](https://github.com/pybind/pybind11/pull/4621) +- Allow lambda specified to function definition to be `noexcept(true)` + in C++17. [#4593](https://github.com/pybind/pybind11/pull/4593) +- Get rid of recursive template instantiations for concatenating type + signatures on C++17 and higher. + [#4587](https://github.com/pybind/pybind11/pull/4587) +- Compatibility with Python 3.12 (beta). Note that the minimum pybind11 + ABI version for Python 3.12 is version 5. (The default ABI version for + Python versions up to and including 3.11 is still version 4.). + [#4570](https://github.com/pybind/pybind11/pull/4570) +- With `PYBIND11_INTERNALS_VERSION 5` (default for Python 3.12+), MSVC + builds use `std::hash` and + `std::equal_to` instead of string-based type + comparisons. This resolves issues when binding types defined in the + unnamed namespace. + [#4319](https://github.com/pybind/pybind11/pull/4319) +- Python exception `__notes__` (introduced with Python 3.11) are now + added to the `error_already_set::what()` output. + [#4678](https://github.com/pybind/pybind11/pull/4678) + +Build system improvements: + +- CMake 3.27 support was added, CMake 3.4 support was dropped. + FindPython will be used if `FindPythonInterp` is not present. + [#4719](https://github.com/pybind/pybind11/pull/4719) +- Update clang-tidy to 15 in CI. + [#4387](https://github.com/pybind/pybind11/pull/4387) +- Moved the linting framework over to Ruff. + [#4483](https://github.com/pybind/pybind11/pull/4483) +- Skip `lto` checks and target generation when + `CMAKE_INTERPROCEDURAL_OPTIMIZATION` is defined. + [#4643](https://github.com/pybind/pybind11/pull/4643) +- No longer inject `-stdlib=libc++`, not needed for modern Pythons + (macOS 10.9+). [#4639](https://github.com/pybind/pybind11/pull/4639) +- PyPy 3.10 support was added, PyPy 3.7 support was dropped. + [#4728](https://github.com/pybind/pybind11/pull/4728) +- Testing with Python 3.12 beta releases was added. + [#4713](https://github.com/pybind/pybind11/pull/4713) + +## Version 2.10.4 (Mar 16, 2023) + +Changes: + +- `python3 -m pybind11` gained a `--version` option (prints the version + and exits). [#4526](https://github.com/pybind/pybind11/pull/4526) + +Bug Fixes: + +- Fix a warning when pydebug is enabled on Python 3.11. + [#4461](https://github.com/pybind/pybind11/pull/4461) +- Ensure `gil_scoped_release` RAII is non-copyable. + [#4490](https://github.com/pybind/pybind11/pull/4490) +- Ensure the tests dir does not show up with new versions of setuptools. + [#4510](https://github.com/pybind/pybind11/pull/4510) +- Better stacklevel for a warning in setuptools helpers. + [#4516](https://github.com/pybind/pybind11/pull/4516) + +## Version 2.10.3 (Jan 3, 2023) + +Changes: + +- Temporarily made our GIL status assertions (added in 2.10.2) disabled + by default (re-enable manually by defining + `PYBIND11_ASSERT_GIL_HELD_INCREF_DECREF`, will be enabled in 2.11). + [#4432](https://github.com/pybind/pybind11/pull/4432) +- Improved error messages when `inc_ref`/`dec_ref` are called with an + invalid GIL state. + [#4427](https://github.com/pybind/pybind11/pull/4427) + [#4436](https://github.com/pybind/pybind11/pull/4436) + +Bug Fixes: + +- Some minor touchups found by static analyzers. + [#4440](https://github.com/pybind/pybind11/pull/4440) + +## Version 2.10.2 (Dec 20, 2022) + +Changes: + +- `scoped_interpreter` constructor taking `PyConfig`. + [#4330](https://github.com/pybind/pybind11/pull/4330) +- `pybind11/eigen/tensor.h` adds converters to and from `Eigen::Tensor` + and `Eigen::TensorMap`. + [#4201](https://github.com/pybind/pybind11/pull/4201) +- `PyGILState_Check()`'s were integrated to `pybind11::handle` + `inc_ref()` & `dec_ref()`. The added GIL checks are guarded by + `PYBIND11_ASSERT_GIL_HELD_INCREF_DECREF`, which is the default only if + `NDEBUG` is not defined. (Made non-default in 2.10.3, will be active + in 2.11) [#4246](https://github.com/pybind/pybind11/pull/4246) +- Add option for enable/disable enum members in docstring. + [#2768](https://github.com/pybind/pybind11/pull/2768) +- Fixed typing of `KeysView`, `ValuesView` and `ItemsView` in + `bind_map`. [#4353](https://github.com/pybind/pybind11/pull/4353) + +Bug fixes: + +- Bug fix affecting only Python 3.6 under very specific, uncommon + conditions: move `PyEval_InitThreads()` call to the correct location. + [#4350](https://github.com/pybind/pybind11/pull/4350) +- Fix segfault bug when passing foreign native functions to + functional.h. [#4254](https://github.com/pybind/pybind11/pull/4254) + +Build system improvements: + +- Support setting PYTHON_LIBRARIES manually for Windows ARM + cross-compilation (classic mode). + [#4406](https://github.com/pybind/pybind11/pull/4406) +- Extend IPO/LTO detection for ICX (a.k.a IntelLLVM) compiler. + [#4402](https://github.com/pybind/pybind11/pull/4402) +- Allow calling `find_package(pybind11 CONFIG)` multiple times from + separate directories in the same CMake project and properly link + Python (new mode). + [#4401](https://github.com/pybind/pybind11/pull/4401) +- `multiprocessing_set_spawn` in pytest fixture for added safety. + [#4377](https://github.com/pybind/pybind11/pull/4377) +- Fixed a bug in two pybind11/tools cmake scripts causing "Unknown + arguments specified" errors. + [#4327](https://github.com/pybind/pybind11/pull/4327) + +## Version 2.10.1 (Oct 31, 2022) + +This is the first version to fully support embedding the newly released +Python 3.11. + +Changes: + +- Allow `pybind11::capsule` constructor to take null destructor + pointers. [#4221](https://github.com/pybind/pybind11/pull/4221) +- `embed.h` was changed so that `PYTHONPATH` is used also with Python + 3.11 (established behavior). + [#4119](https://github.com/pybind/pybind11/pull/4119) +- A `PYBIND11_SIMPLE_GIL_MANAGEMENT` option was added (cmake, C++ + define), along with many additional tests in `test_gil_scoped.py`. The + option may be useful to try when debugging GIL-related issues, to + determine if the more complex default implementation is or is not to + blame. See \#4216 for background. WARNING: Please be careful to not + create ODR violations when using the option: everything that is linked + together with mutual symbol visibility needs to be rebuilt. + [#4216](https://github.com/pybind/pybind11/pull/4216) +- `PYBIND11_EXPORT_EXCEPTION` was made non-empty only under macOS. This + makes Linux builds safer, and enables the removal of warning + suppression pragmas for Windows. + [#4298](https://github.com/pybind/pybind11/pull/4298) + +Bug fixes: + +- Fixed a bug where `UnicodeDecodeError` was not propagated from various + `py::str` ctors when decoding surrogate utf characters. + [#4294](https://github.com/pybind/pybind11/pull/4294) +- Revert perfect forwarding for `make_iterator`. This broke at least one + valid use case. May revisit later. + [#4234](https://github.com/pybind/pybind11/pull/4234) +- Fix support for safe casts to `void*` (regression in 2.10.0). + [#4275](https://github.com/pybind/pybind11/pull/4275) +- Fix `char8_t` support (regression in 2.9). + [#4278](https://github.com/pybind/pybind11/pull/4278) +- Unicode surrogate character in Python exception message leads to + process termination in `error_already_set::what()`. + [#4297](https://github.com/pybind/pybind11/pull/4297) +- Fix MSVC 2019 v.1924 & C++14 mode error for `overload_cast`. + [#4188](https://github.com/pybind/pybind11/pull/4188) +- Make augmented assignment operators non-const for the object-api. + Behavior was previously broken for augmented assignment operators. + [#4065](https://github.com/pybind/pybind11/pull/4065) +- Add proper error checking to C++ bindings for Python list append and + insert. [#4208](https://github.com/pybind/pybind11/pull/4208) +- Work-around for Nvidia's CUDA nvcc compiler in versions 11.4.0 - + 11.8.0. [#4220](https://github.com/pybind/pybind11/pull/4220) +- A workaround for PyPy was added in the `py::error_already_set` + implementation, related to PR + [#1895](https://github.com/pybind/pybind11/pull/1895) released with + v2.10.0. [#4079](https://github.com/pybind/pybind11/pull/4079) +- Fixed compiler errors when C++23 `std::forward_like` is available. + [#4136](https://github.com/pybind/pybind11/pull/4136) +- Properly raise exceptions in contains methods (like when an object in + unhashable). [#4209](https://github.com/pybind/pybind11/pull/4209) +- Further improve another error in exception handling. + [#4232](https://github.com/pybind/pybind11/pull/4232) +- `get_local_internals()` was made compatible with + `finalize_interpreter()`, fixing potential freezes during interpreter + finalization. [#4192](https://github.com/pybind/pybind11/pull/4192) + +Performance and style: + +- Reserve space in set and STL map casters if possible. This will + prevent unnecessary rehashing / resizing by knowing the number of keys + ahead of time for Python to C++ casting. This improvement will greatly + speed up the casting of large unordered maps and sets. + [#4194](https://github.com/pybind/pybind11/pull/4194) +- GIL RAII scopes are non-copyable to avoid potential bugs. + [#4183](https://github.com/pybind/pybind11/pull/4183) +- Explicitly default all relevant ctors for pytypes in the + `PYBIND11_OBJECT` macros and enforce the clang-tidy checks + `modernize-use-equals-default` in macros as well. + [#4017](https://github.com/pybind/pybind11/pull/4017) +- Optimize iterator advancement in C++ bindings. + [#4237](https://github.com/pybind/pybind11/pull/4237) +- Use the modern `PyObject_GenericGetDict` and `PyObject_GenericSetDict` + for handling dynamic attribute dictionaries. + [#4106](https://github.com/pybind/pybind11/pull/4106) +- Document that users should use `PYBIND11_NAMESPACE` instead of using + `pybind11` when opening namespaces. Using namespace declarations and + namespace qualification remain the same as `pybind11`. This is done to + ensure consistent symbol visibility. + [#4098](https://github.com/pybind/pybind11/pull/4098) +- Mark `detail::forward_like` as constexpr. + [#4147](https://github.com/pybind/pybind11/pull/4147) +- Optimize unpacking_collector when processing `arg_v` arguments. + [#4219](https://github.com/pybind/pybind11/pull/4219) +- Optimize casting C++ object to `None`. + [#4269](https://github.com/pybind/pybind11/pull/4269) + +Build system improvements: + +- CMake: revert overwrite behavior, now opt-in with + `PYBIND11_PYTHONLIBS_OVERRWRITE OFF`. + [#4195](https://github.com/pybind/pybind11/pull/4195) +- Include a pkg-config file when installing pybind11, such as in the + Python package. [#4077](https://github.com/pybind/pybind11/pull/4077) +- Avoid stripping debug symbols when `CMAKE_BUILD_TYPE` is set to + `DEBUG` instead of `Debug`. + [#4078](https://github.com/pybind/pybind11/pull/4078) +- Followup to [#3948](https://github.com/pybind/pybind11/pull/3948), + fixing vcpkg again. + [#4123](https://github.com/pybind/pybind11/pull/4123) + +## Version 2.10.0 (Jul 15, 2022) + +Removed support for Python 2.7, Python 3.5, and MSVC 2015. Support for +MSVC 2017 is limited due to availability of CI runners; we highly +recommend MSVC 2019 or 2022 be used. Initial support added for Python +3.11. + +New features: + +- `py::anyset` & `py::frozenset` were added, with copying (cast) to + `std::set` (similar to `set`). + [#3901](https://github.com/pybind/pybind11/pull/3901) +- Support bytearray casting to string. + [#3707](https://github.com/pybind/pybind11/pull/3707) +- `type_caster` was added. `std::monostate` is a tag + type that allows `std::variant` to act as an optional, or allows + default construction of a `std::variant` holding a non-default + constructible type. + [#3818](https://github.com/pybind/pybind11/pull/3818) +- `pybind11::capsule::set_name` added to mutate the name of the capsule + instance. [#3866](https://github.com/pybind/pybind11/pull/3866) +- NumPy: dtype constructor from type number added, accessors + corresponding to Python API `dtype.num`, `dtype.byteorder`, + `dtype.flags` and `dtype.alignment` added. + [#3868](https://github.com/pybind/pybind11/pull/3868) + +Changes: + +- Python 3.6 is now the minimum supported version. + [#3688](https://github.com/pybind/pybind11/pull/3688) + [#3719](https://github.com/pybind/pybind11/pull/3719) +- The minimum version for MSVC is now 2017. + [#3722](https://github.com/pybind/pybind11/pull/3722) +- Fix issues with CPython 3.11 betas and add to supported test matrix. + [#3923](https://github.com/pybind/pybind11/pull/3923) +- `error_already_set` is now safer and more performant, especially for + exceptions with long tracebacks, by delaying computation. + [#1895](https://github.com/pybind/pybind11/pull/1895) +- Improve exception handling in python `str` bindings. + [#3826](https://github.com/pybind/pybind11/pull/3826) +- The bindings for capsules now have more consistent exception handling. + [#3825](https://github.com/pybind/pybind11/pull/3825) +- `PYBIND11_OBJECT_CVT` and `PYBIND11_OBJECT_CVT_DEFAULT` macro can now + be used to define classes in namespaces other than pybind11. + [#3797](https://github.com/pybind/pybind11/pull/3797) +- Error printing code now uses `PYBIND11_DETAILED_ERROR_MESSAGES` + instead of requiring `NDEBUG`, allowing use with release builds if + desired. [#3913](https://github.com/pybind/pybind11/pull/3913) +- Implicit conversion of the literal `0` to `pybind11::handle` is now + disabled. [#4008](https://github.com/pybind/pybind11/pull/4008) + +Bug fixes: + +- Fix exception handling when `pybind11::weakref()` fails. + [#3739](https://github.com/pybind/pybind11/pull/3739) +- `module_::def_submodule` was missing proper error handling. This is + fixed now. [#3973](https://github.com/pybind/pybind11/pull/3973) +- The behavior or `error_already_set` was made safer and the highly + opaque "Unknown internal error occurred" message was replaced with a + more helpful message. + [#3982](https://github.com/pybind/pybind11/pull/3982) +- `error_already_set::what()` now handles non-normalized exceptions + correctly. [#3971](https://github.com/pybind/pybind11/pull/3971) +- Support older C++ compilers where filesystem is not yet part of the + standard library and is instead included in + `std::experimental::filesystem`. + [#3840](https://github.com/pybind/pybind11/pull/3840) +- Fix `-Wfree-nonheap-object` warnings produced by GCC by avoiding + returning pointers to static objects with + `return_value_policy::take_ownership`. + [#3946](https://github.com/pybind/pybind11/pull/3946) +- Fix cast from pytype rvalue to another pytype. + [#3949](https://github.com/pybind/pybind11/pull/3949) +- Ensure proper behavior when garbage collecting classes with dynamic + attributes in Python \>=3.9. + [#4051](https://github.com/pybind/pybind11/pull/4051) +- A couple long-standing `PYBIND11_NAMESPACE` + `__attribute__((visibility("hidden")))` inconsistencies are now fixed + (affects only unusual environments). + [#4043](https://github.com/pybind/pybind11/pull/4043) +- `pybind11::detail::get_internals()` is now resilient to in-flight + Python exceptions. + [#3981](https://github.com/pybind/pybind11/pull/3981) +- Arrays with a dimension of size 0 are now properly converted to + dynamic Eigen matrices (more common in NumPy 1.23). + [#4038](https://github.com/pybind/pybind11/pull/4038) +- Avoid catching unrelated errors when importing NumPy. + [#3974](https://github.com/pybind/pybind11/pull/3974) + +Performance and style: + +- Added an accessor overload of `(object &&key)` to reference steal the + object when using python types as keys. This prevents unnecessary + reference count overhead for attr, dictionary, tuple, and sequence + look ups. Added additional regression tests. Fixed a performance bug + the caused accessor assignments to potentially perform unnecessary + copies. [#3970](https://github.com/pybind/pybind11/pull/3970) +- Perfect forward all args of `make_iterator`. + [#3980](https://github.com/pybind/pybind11/pull/3980) +- Avoid potential bug in pycapsule destructor by adding an `error_guard` + to one of the dtors. + [#3958](https://github.com/pybind/pybind11/pull/3958) +- Optimize dictionary access in `strip_padding` for numpy. + [#3994](https://github.com/pybind/pybind11/pull/3994) +- `stl_bind.h` bindings now take slice args as a const-ref. + [#3852](https://github.com/pybind/pybind11/pull/3852) +- Made slice constructor more consistent, and improve performance of + some casters by allowing reference stealing. + [#3845](https://github.com/pybind/pybind11/pull/3845) +- Change numpy dtype from_args method to use const ref. + [#3878](https://github.com/pybind/pybind11/pull/3878) +- Follow rule of three to ensure `PyErr_Restore` is called only once. + [#3872](https://github.com/pybind/pybind11/pull/3872) +- Added missing perfect forwarding for `make_iterator` functions. + [#3860](https://github.com/pybind/pybind11/pull/3860) +- Optimize c++ to python function casting by using the rvalue caster. + [#3966](https://github.com/pybind/pybind11/pull/3966) +- Optimize Eigen sparse matrix casting by removing unnecessary + temporary. [#4064](https://github.com/pybind/pybind11/pull/4064) +- Avoid potential implicit copy/assignment constructors causing double + free in `strdup_guard`. + [#3905](https://github.com/pybind/pybind11/pull/3905) +- Enable clang-tidy checks `misc-definitions-in-headers`, + `modernize-loop-convert`, and `modernize-use-nullptr`. + [#3881](https://github.com/pybind/pybind11/pull/3881) + [#3988](https://github.com/pybind/pybind11/pull/3988) + +Build system improvements: + +- CMake: Fix file extension on Windows with cp36 and cp37 using + FindPython. [#3919](https://github.com/pybind/pybind11/pull/3919) +- CMake: Support multiple Python targets (such as on vcpkg). + [#3948](https://github.com/pybind/pybind11/pull/3948) +- CMake: Fix issue with NVCC on Windows. + [#3947](https://github.com/pybind/pybind11/pull/3947) +- CMake: Drop the bitness check on cross compiles (like targeting + WebAssembly via Emscripten). + [#3959](https://github.com/pybind/pybind11/pull/3959) +- Add MSVC builds in debug mode to CI. + [#3784](https://github.com/pybind/pybind11/pull/3784) +- MSVC 2022 C++20 coverage was added to GitHub Actions, including Eigen. + [#3732](https://github.com/pybind/pybind11/pull/3732), + [#3741](https://github.com/pybind/pybind11/pull/3741) + +Backend and tidying up: + +- New theme for the documentation. + [#3109](https://github.com/pybind/pybind11/pull/3109) +- Remove idioms in code comments. Use more inclusive language. + [#3809](https://github.com/pybind/pybind11/pull/3809) +- `#include ` was removed from the `pybind11/stl.h` header. + Your project may break if it has a transitive dependency on this + include. The fix is to "Include What You Use". + [#3928](https://github.com/pybind/pybind11/pull/3928) +- Avoid `setup.py ` usage in internal tests. + [#3734](https://github.com/pybind/pybind11/pull/3734) + +## Version 2.9.2 (Mar 29, 2022) + +Changes: + +- Enum now has an `__index__` method on Python \<3.8 too. + [#3700](https://github.com/pybind/pybind11/pull/3700) +- Local internals are now cleared after finalizing the interpreter. + [#3744](https://github.com/pybind/pybind11/pull/3744) + +Bug fixes: + +- Better support for Python 3.11 alphas. + [#3694](https://github.com/pybind/pybind11/pull/3694) +- `PYBIND11_TYPE_CASTER` now uses fully qualified symbols, so it can be + used outside of `pybind11::detail`. + [#3758](https://github.com/pybind/pybind11/pull/3758) +- Some fixes for PyPy 3.9. + [#3768](https://github.com/pybind/pybind11/pull/3768) +- Fixed a potential memleak in PyPy in `get_type_override`. + [#3774](https://github.com/pybind/pybind11/pull/3774) +- Fix usage of `VISIBILITY_INLINES_HIDDEN`. + [#3721](https://github.com/pybind/pybind11/pull/3721) + +Build system improvements: + +- Uses `sysconfig` module to determine installation locations on Python + \>= 3.10, instead of `distutils` which has been deprecated. + [#3764](https://github.com/pybind/pybind11/pull/3764) +- Support Catch 2.13.5+ (supporting GLIBC 2.34+). + [#3679](https://github.com/pybind/pybind11/pull/3679) +- Fix test failures with numpy 1.22 by ignoring whitespace when + comparing `str()` of dtypes. + [#3682](https://github.com/pybind/pybind11/pull/3682) + +Backend and tidying up: + +- clang-tidy: added `readability-qualified-auto`, + `readability-braces-around-statements`, + `cppcoreguidelines-prefer-member-initializer`, + `clang-analyzer-optin.performance.Padding`, + `cppcoreguidelines-pro-type-static-cast-downcast`, and + `readability-inconsistent-declaration-parameter-name`. + [#3702](https://github.com/pybind/pybind11/pull/3702), + [#3699](https://github.com/pybind/pybind11/pull/3699), + [#3716](https://github.com/pybind/pybind11/pull/3716), + [#3709](https://github.com/pybind/pybind11/pull/3709) +- clang-format was added to the pre-commit actions, and the entire code + base automatically reformatted (after several iterations preparing for + this leap). [#3713](https://github.com/pybind/pybind11/pull/3713) + +## Version 2.9.1 (Feb 2, 2022) + +Changes: + +- If possible, attach Python exception with `py::raise_from` to + `TypeError` when casting from C++ to Python. This will give additional + info if Python exceptions occur in the caster. Adds a test case of + trying to convert a set from C++ to Python when the hash function is + not defined in Python. + [#3605](https://github.com/pybind/pybind11/pull/3605) +- Add a mapping of C++11 nested exceptions to their Python exception + equivalent using `py::raise_from`. This attaches the nested exceptions + in Python using the `__cause__` field. + [#3608](https://github.com/pybind/pybind11/pull/3608) +- Propagate Python exception traceback using `raise_from` if a pybind11 + function runs out of overloads. + [#3671](https://github.com/pybind/pybind11/pull/3671) +- `py::multiple_inheritance` is now only needed when C++ bases are + hidden from pybind11. + [#3650](https://github.com/pybind/pybind11/pull/3650) and + [#3659](https://github.com/pybind/pybind11/pull/3659) + +Bug fixes: + +- Remove a boolean cast in `numpy.h` that causes MSVC C4800 warnings + when compiling against Python 3.10 or newer. + [#3669](https://github.com/pybind/pybind11/pull/3669) +- Render `py::bool_` and `py::float_` as `bool` and `float` + respectively. [#3622](https://github.com/pybind/pybind11/pull/3622) + +Build system improvements: + +- Fix CMake extension suffix computation on Python 3.10+. + [#3663](https://github.com/pybind/pybind11/pull/3663) +- Allow `CMAKE_ARGS` to override CMake args in pybind11's own + `setup.py`. [#3577](https://github.com/pybind/pybind11/pull/3577) +- Remove a few deprecated c-headers. + [#3610](https://github.com/pybind/pybind11/pull/3610) +- More uniform handling of test targets. + [#3590](https://github.com/pybind/pybind11/pull/3590) +- Add clang-tidy readability check to catch potentially swapped function + args. [#3611](https://github.com/pybind/pybind11/pull/3611) + +## Version 2.9.0 (Dec 28, 2021) + +This is the last version to support Python 2.7 and 3.5. + +New Features: + +- Allow `py::args` to be followed by other arguments; the remaining + arguments are implicitly keyword-only, as if a `py::kw_only{}` + annotation had been used. + [#3402](https://github.com/pybind/pybind11/pull/3402) + +Changes: + +- Make str/bytes/memoryview more interoperable with `std::string_view`. + [#3521](https://github.com/pybind/pybind11/pull/3521) +- Replace `_` with `const_name` in internals, avoid defining `pybind::_` + if `_` defined as macro (common gettext usage) + [#3423](https://github.com/pybind/pybind11/pull/3423) + +Bug fixes: + +- Fix a rare warning about extra copy in an Eigen constructor. + [#3486](https://github.com/pybind/pybind11/pull/3486) +- Fix caching of the C++ overrides. + [#3465](https://github.com/pybind/pybind11/pull/3465) +- Add missing `std::forward` calls to some `cpp_function` overloads. + [#3443](https://github.com/pybind/pybind11/pull/3443) +- Support PyPy 7.3.7 and the PyPy3.8 beta. Test python-3.11 on PRs with + the `python dev` label. + [#3419](https://github.com/pybind/pybind11/pull/3419) +- Replace usage of deprecated `Eigen::MappedSparseMatrix` with + `Eigen::Map>` for Eigen 3.3+. + [#3499](https://github.com/pybind/pybind11/pull/3499) +- Tweaks to support Microsoft Visual Studio 2022. + [#3497](https://github.com/pybind/pybind11/pull/3497) + +Build system improvements: + +- Nicer CMake printout and IDE organisation for pybind11's own tests. + [#3479](https://github.com/pybind/pybind11/pull/3479) +- CMake: report version type as part of the version string to avoid a + spurious space in the package status message. + [#3472](https://github.com/pybind/pybind11/pull/3472) +- Flags starting with `-g` in `$CFLAGS` and `$CPPFLAGS` are no longer + overridden by `.Pybind11Extension`. + [#3436](https://github.com/pybind/pybind11/pull/3436) +- Ensure ThreadPool is closed in `setup_helpers`. + [#3548](https://github.com/pybind/pybind11/pull/3548) +- Avoid LTS on `mips64` and `ppc64le` (reported broken). + [#3557](https://github.com/pybind/pybind11/pull/3557) + +## v2.8.1 (Oct 27, 2021) + +Changes and additions: + +- The simple namespace creation shortcut added in 2.8.0 was deprecated + due to usage of CPython internal API, and will be removed soon. Use + `py::module_::import("types").attr("SimpleNamespace")`. + [#3374](https://github.com/pybinyyd/pybind11/pull/3374) +- Add C++ Exception type to throw and catch `AttributeError`. Useful for + defining custom `__setattr__` and `__getattr__` methods. + [#3387](https://github.com/pybind/pybind11/pull/3387) + +Fixes: + +- Fixed the potential for dangling references when using properties with + `std::optional` types. + [#3376](https://github.com/pybind/pybind11/pull/3376) +- Modernize usage of `PyCodeObject` on Python 3.9+ (moving toward + support for Python 3.11a1) + [#3368](https://github.com/pybind/pybind11/pull/3368) +- A long-standing bug in `eigen.h` was fixed (originally PR \#3343). The + bug was unmasked by newly added `static_assert`'s in the Eigen 3.4.0 + release. [#3352](https://github.com/pybind/pybind11/pull/3352) +- Support multiple raw inclusion of CMake helper files (Conan.io does + this for multi-config generators). + [#3420](https://github.com/pybind/pybind11/pull/3420) +- Fix harmless warning on upcoming CMake 3.22. + [#3368](https://github.com/pybind/pybind11/pull/3368) +- Fix 2.8.0 regression with MSVC 2017 + C++17 mode + Python 3. + [#3407](https://github.com/pybind/pybind11/pull/3407) +- Fix 2.8.0 regression that caused undefined behavior (typically + segfaults) in `make_key_iterator`/`make_value_iterator` if + dereferencing the iterator returned a temporary value instead of a + reference. [#3348](https://github.com/pybind/pybind11/pull/3348) + +## v2.8.0 (Oct 4, 2021) + +New features: + +- Added `py::raise_from` to enable chaining exceptions. + [#3215](https://github.com/pybind/pybind11/pull/3215) +- Allow exception translators to be optionally registered local to a + module instead of applying globally across all pybind11 modules. Use + `register_local_exception_translator(ExceptionTranslator&& translator)` + instead of + `register_exception_translator(ExceptionTranslator&& translator)` to + keep your exception remapping code local to the module. + [#2650](https://github.com/pybinyyd/pybind11/pull/2650) +- Add `make_simple_namespace` function for instantiating Python + `SimpleNamespace` objects. **Deprecated in 2.8.1.** + [#2840](https://github.com/pybind/pybind11/pull/2840) +- `pybind11::scoped_interpreter` and `initialize_interpreter` have new + arguments to allow `sys.argv` initialization. + [#2341](https://github.com/pybind/pybind11/pull/2341) +- Allow Python builtins to be used as callbacks in CPython. + [#1413](https://github.com/pybind/pybind11/pull/1413) +- Added `view` to view arrays with a different datatype. + [#987](https://github.com/pybind/pybind11/pull/987) +- Implemented `reshape` on arrays. + [#984](https://github.com/pybind/pybind11/pull/984) +- Enable defining custom `__new__` methods on classes by fixing bug + preventing overriding methods if they have non-pybind11 siblings. + [#3265](https://github.com/pybind/pybind11/pull/3265) +- Add `make_value_iterator()`, and fix `make_key_iterator()` to return + references instead of copies. + [#3293](https://github.com/pybind/pybind11/pull/3293) +- Improve the classes generated by `bind_map`: + [#3310](https://github.com/pybind/pybind11/pull/3310) + - Change `.items` from an iterator to a dictionary view. + - Add `.keys` and `.values` (both dictionary views). + - Allow `__contains__` to take any object. +- `pybind11::custom_type_setup` was added, for customizing the + `PyHeapTypeObject` corresponding to a class, which may be useful for + enabling garbage collection support, among other things. + [#3287](https://github.com/pybind/pybind11/pull/3287) + +Changes: + +- Set `__file__` constant when running `eval_file` in an embedded + interpreter. [#3233](https://github.com/pybind/pybind11/pull/3233) +- Python objects and (C++17) `std::optional` now accepted in `py::slice` + constructor. [#1101](https://github.com/pybind/pybind11/pull/1101) +- The pybind11 proxy types `str`, `bytes`, `bytearray`, `tuple`, `list` + now consistently support passing `ssize_t` values for sizes and + indexes. Previously, only `size_t` was accepted in several interfaces. + [#3219](https://github.com/pybind/pybind11/pull/3219) +- Avoid evaluating `PYBIND11_TLS_REPLACE_VALUE` arguments more than + once. [#3290](https://github.com/pybind/pybind11/pull/3290) + +Fixes: + +- Bug fix: enum value's `__int__` returning non-int when underlying type + is bool or of char type. + [#1334](https://github.com/pybind/pybind11/pull/1334) +- Fixes bug in setting error state in Capsule's pointer methods. + [#3261](https://github.com/pybind/pybind11/pull/3261) +- A long-standing memory leak in `py::cpp_function::initialize` was + fixed. [#3229](https://github.com/pybind/pybind11/pull/3229) +- Fixes thread safety for some `pybind11::type_caster` which require + lifetime extension, such as for `std::string_view`. + [#3237](https://github.com/pybind/pybind11/pull/3237) +- Restore compatibility with gcc 4.8.4 as distributed by ubuntu-trusty, + linuxmint-17. [#3270](https://github.com/pybind/pybind11/pull/3270) + +Build system improvements: + +- Fix regression in CMake Python package config: improper use of + absolute path. [#3144](https://github.com/pybind/pybind11/pull/3144) +- Cached Python version information could become stale when CMake was + re-run with a different Python version. The build system now detects + this and updates this information. + [#3299](https://github.com/pybind/pybind11/pull/3299) +- Specified UTF8-encoding in setup.py calls of open(). + [#3137](https://github.com/pybind/pybind11/pull/3137) +- Fix a harmless warning from CMake 3.21 with the classic Python + discovery. [#3220](https://github.com/pybind/pybind11/pull/3220) +- Eigen repo and version can now be specified as cmake options. + [#3324](https://github.com/pybind/pybind11/pull/3324) + +Backend and tidying up: + +- Reduced thread-local storage required for keeping alive temporary data + for type conversion to one key per ABI version, rather than one key + per extension module. This makes the total thread-local storage + required by pybind11 2 keys per ABI version. + [#3275](https://github.com/pybind/pybind11/pull/3275) +- Optimize NumPy array construction with additional moves. + [#3183](https://github.com/pybind/pybind11/pull/3183) +- Conversion to `std::string` and `std::string_view` now avoids making + an extra copy of the data on Python \>= 3.3. + [#3257](https://github.com/pybind/pybind11/pull/3257) +- Remove const modifier from certain C++ methods on Python collections + (`list`, `set`, `dict`) such as (`clear()`, `append()`, `insert()`, + etc...) and annotated them with `py-non-const`. +- Enable readability `clang-tidy-const-return` and remove useless + consts. [#3254](https://github.com/pybind/pybind11/pull/3254) + [#3194](https://github.com/pybind/pybind11/pull/3194) +- The clang-tidy `google-explicit-constructor` option was enabled. + [#3250](https://github.com/pybind/pybind11/pull/3250) +- Mark a pytype move constructor as noexcept (perf). + [#3236](https://github.com/pybind/pybind11/pull/3236) +- Enable clang-tidy check to guard against inheritance slicing. + [#3210](https://github.com/pybind/pybind11/pull/3210) +- Legacy warning suppression pragma were removed from eigen.h. On Unix + platforms, please use -isystem for Eigen include directories, to + suppress compiler warnings originating from Eigen headers. Note that + CMake does this by default. No adjustments are needed for Windows. + [#3198](https://github.com/pybind/pybind11/pull/3198) +- Format pybind11 with isort consistent ordering of imports + [#3195](https://github.com/pybind/pybind11/pull/3195) +- The warnings-suppression "pragma clamp" at the top/bottom of pybind11 + was removed, clearing the path to refactoring and IWYU cleanup. + [#3186](https://github.com/pybind/pybind11/pull/3186) +- Enable most bugprone checks in clang-tidy and fix the found potential + bugs and poor coding styles. + [#3166](https://github.com/pybind/pybind11/pull/3166) +- Add `clang-tidy-readability` rules to make boolean casts explicit + improving code readability. Also enabled other misc and readability + clang-tidy checks. + [#3148](https://github.com/pybind/pybind11/pull/3148) +- Move object in `.pop()` for list. + [#3116](https://github.com/pybind/pybind11/pull/3116) + +## v2.7.1 (Aug 3, 2021) + +Minor missing functionality added: + +- Allow Python builtins to be used as callbacks in CPython. + [#1413](https://github.com/pybind/pybind11/pull/1413) + +Bug fixes: + +- Fix regression in CMake Python package config: improper use of + absolute path. [#3144](https://github.com/pybind/pybind11/pull/3144) +- Fix Mingw64 and add to the CI testing matrix. + [#3132](https://github.com/pybind/pybind11/pull/3132) +- Specified UTF8-encoding in setup.py calls of open(). + [#3137](https://github.com/pybind/pybind11/pull/3137) +- Add clang-tidy-readability rules to make boolean casts explicit + improving code readability. Also enabled other misc and readability + clang-tidy checks. + [#3148](https://github.com/pybind/pybind11/pull/3148) +- Move object in `.pop()` for list. + [#3116](https://github.com/pybind/pybind11/pull/3116) + +Backend and tidying up: + +- Removed and fixed warning suppressions. + [#3127](https://github.com/pybind/pybind11/pull/3127) + [#3129](https://github.com/pybind/pybind11/pull/3129) + [#3135](https://github.com/pybind/pybind11/pull/3135) + [#3141](https://github.com/pybind/pybind11/pull/3141) + [#3142](https://github.com/pybind/pybind11/pull/3142) + [#3150](https://github.com/pybind/pybind11/pull/3150) + [#3152](https://github.com/pybind/pybind11/pull/3152) + [#3160](https://github.com/pybind/pybind11/pull/3160) + [#3161](https://github.com/pybind/pybind11/pull/3161) + +## v2.7.0 (Jul 16, 2021) + +New features: + +- Enable `py::implicitly_convertible` for + `py::class_`-wrapped types. + [#3059](https://github.com/pybind/pybind11/pull/3059) +- Allow function pointer extraction from overloaded functions. + [#2944](https://github.com/pybind/pybind11/pull/2944) +- NumPy: added `.char_()` to type which gives the NumPy public `char` + result, which also distinguishes types by bit length (unlike + `.kind()`). [#2864](https://github.com/pybind/pybind11/pull/2864) +- Add `pybind11::bytearray` to manipulate `bytearray` similar to + `bytes`. [#2799](https://github.com/pybind/pybind11/pull/2799) +- `pybind11/stl/filesystem.h` registers a type caster that, on + C++17/Python 3.6+, converts `std::filesystem::path` to `pathlib.Path` + and any `os.PathLike` to `std::filesystem::path`. + [#2730](https://github.com/pybind/pybind11/pull/2730) +- A `PYBIND11_VERSION_HEX` define was added, similar to + `PY_VERSION_HEX`. + [#3120](https://github.com/pybind/pybind11/pull/3120) + +Changes: + +- `py::str` changed to exclusively hold `PyUnicodeObject`. Previously + `py::str` could also hold `bytes`, which is probably surprising, was + never documented, and can mask bugs (e.g. accidental use of `py::str` + instead of `py::bytes`). + [#2409](https://github.com/pybind/pybind11/pull/2409) +- Add a safety guard to ensure that the Python GIL is held when C++ + calls back into Python via `object_api<>::operator()` (e.g. + `py::function` `__call__`). (This feature is available for Python 3.6+ + only.) [#2919](https://github.com/pybind/pybind11/pull/2919) +- Catch a missing `self` argument in calls to `__init__()`. + [#2914](https://github.com/pybind/pybind11/pull/2914) +- Use `std::string_view` if available to avoid a copy when passing an + object to a `std::ostream`. + [#3042](https://github.com/pybind/pybind11/pull/3042) +- An important warning about thread safety was added to the `iostream.h` + documentation; attempts to make `py::scoped_ostream_redirect` thread + safe have been removed, as it was only partially effective. + [#2995](https://github.com/pybind/pybind11/pull/2995) + +Fixes: + +- Performance: avoid unnecessary strlen calls. + [#3058](https://github.com/pybind/pybind11/pull/3058) +- Fix auto-generated documentation string when using `const T` in + `pyarray_t`. [#3020](https://github.com/pybind/pybind11/pull/3020) +- Unify error messages thrown by + `simple_collector`/`unpacking_collector`. + [#3013](https://github.com/pybind/pybind11/pull/3013) +- `pybind11::builtin_exception` is now explicitly exported, which means + the types included/defined in different modules are identical, and + exceptions raised in different modules can be caught correctly. The + documentation was updated to explain that custom exceptions that are + used across module boundaries need to be explicitly exported as well. + [#2999](https://github.com/pybind/pybind11/pull/2999) +- Fixed exception when printing UTF-8 to a `scoped_ostream_redirect`. + [#2982](https://github.com/pybind/pybind11/pull/2982) +- Pickle support enhancement: `setstate` implementation will attempt to + `setattr` `__dict__` only if the unpickled `dict` object is not empty, + to not force use of `py::dynamic_attr()` unnecessarily. + [#2972](https://github.com/pybind/pybind11/pull/2972) +- Allow negative timedelta values to roundtrip. + [#2870](https://github.com/pybind/pybind11/pull/2870) +- Fix unchecked errors could potentially swallow signals/other + exceptions. [#2863](https://github.com/pybind/pybind11/pull/2863) +- Add null pointer check with `std::localtime`. + [#2846](https://github.com/pybind/pybind11/pull/2846) +- Fix the `weakref` constructor from `py::object` to create a new + `weakref` on conversion. + [#2832](https://github.com/pybind/pybind11/pull/2832) +- Avoid relying on exceptions in C++17 when getting a `shared_ptr` + holder from a `shared_from_this` class. + [#2819](https://github.com/pybind/pybind11/pull/2819) +- Allow the codec's exception to be raised instead of `RuntimeError` + when casting from `py::str` to `std::string`. + [#2903](https://github.com/pybind/pybind11/pull/2903) + +Build system improvements: + +- In `setup_helpers.py`, test for platforms that have some + multiprocessing features but lack semaphores, which `ParallelCompile` + requires. [#3043](https://github.com/pybind/pybind11/pull/3043) +- Fix `pybind11_INCLUDE_DIR` in case `CMAKE_INSTALL_INCLUDEDIR` is + absolute. [#3005](https://github.com/pybind/pybind11/pull/3005) +- Fix bug not respecting `WITH_SOABI` or `WITHOUT_SOABI` to CMake. + [#2938](https://github.com/pybind/pybind11/pull/2938) +- Fix the default `Pybind11Extension` compilation flags with a Mingw64 + python. [#2921](https://github.com/pybind/pybind11/pull/2921) +- Clang on Windows: do not pass `/MP` (ignored flag). + [#2824](https://github.com/pybind/pybind11/pull/2824) +- `pybind11.setup_helpers.intree_extensions` can be used to generate + `Pybind11Extension` instances from cpp files placed in the Python + package source tree. + [#2831](https://github.com/pybind/pybind11/pull/2831) + +Backend and tidying up: + +- Enable clang-tidy performance, readability, and modernization checks + throughout the codebase to enforce best coding practices. + [#3046](https://github.com/pybind/pybind11/pull/3046), + [#3049](https://github.com/pybind/pybind11/pull/3049), + [#3051](https://github.com/pybind/pybind11/pull/3051), + [#3052](https://github.com/pybind/pybind11/pull/3052), + [#3080](https://github.com/pybind/pybind11/pull/3080), and + [#3094](https://github.com/pybind/pybind11/pull/3094) +- Checks for common misspellings were added to the pre-commit hooks. + [#3076](https://github.com/pybind/pybind11/pull/3076) +- Changed `Werror` to stricter `Werror-all` for Intel compiler and fixed + minor issues. [#2948](https://github.com/pybind/pybind11/pull/2948) +- Fixed compilation with GCC \< 5 when the user defines + `_GLIBCXX_USE_CXX11_ABI`. + [#2956](https://github.com/pybind/pybind11/pull/2956) +- Added nox support for easier local testing and linting of + contributions. [#3101](https://github.com/pybind/pybind11/pull/3101) + and [#3121](https://github.com/pybind/pybind11/pull/3121) +- Avoid RTD style issue with docutils 0.17+. + [#3119](https://github.com/pybind/pybind11/pull/3119) +- Support pipx run, such as `pipx run pybind11 --include` for a quick + compile. [#3117](https://github.com/pybind/pybind11/pull/3117) + +## v2.6.2 (Jan 26, 2021) + +Minor missing functionality added: + +- enum: add missing Enum.value property. + [#2739](https://github.com/pybind/pybind11/pull/2739) +- Allow thread termination to be avoided during shutdown for CPython + 3.7+ via `.disarm` for `gil_scoped_acquire`/`gil_scoped_release`. + [#2657](https://github.com/pybind/pybind11/pull/2657) + +Fixed or improved behavior in a few special cases: + +- Fix bug where the constructor of `object` subclasses would not throw + on being passed a Python object of the wrong type. + [#2701](https://github.com/pybind/pybind11/pull/2701) +- The `type_caster` for integers does not convert Python objects with + `__int__` anymore with `noconvert` or during the first round of trying + overloads. [#2698](https://github.com/pybind/pybind11/pull/2698) +- When casting to a C++ integer, `__index__` is always called and not + considered as conversion, consistent with Python 3.8+. + [#2801](https://github.com/pybind/pybind11/pull/2801) + +Build improvements: + +- Setup helpers: `extra_compile_args` and `extra_link_args` + automatically set by Pybind11Extension are now prepended, which allows + them to be overridden by user-set `extra_compile_args` and + `extra_link_args`. + [#2808](https://github.com/pybind/pybind11/pull/2808) +- Setup helpers: Don't trigger unused parameter warning. + [#2735](https://github.com/pybind/pybind11/pull/2735) +- CMake: Support running with `--warn-uninitialized` active. + [#2806](https://github.com/pybind/pybind11/pull/2806) +- CMake: Avoid error if included from two submodule directories. + [#2804](https://github.com/pybind/pybind11/pull/2804) +- CMake: Fix `STATIC` / `SHARED` being ignored in FindPython mode. + [#2796](https://github.com/pybind/pybind11/pull/2796) +- CMake: Respect the setting for `CMAKE_CXX_VISIBILITY_PRESET` if + defined. [#2793](https://github.com/pybind/pybind11/pull/2793) +- CMake: Fix issue with FindPython2/FindPython3 not working with + `pybind11::embed`. + [#2662](https://github.com/pybind/pybind11/pull/2662) +- CMake: mixing local and installed pybind11's would prioritize the + installed one over the local one (regression in 2.6.0). + [#2716](https://github.com/pybind/pybind11/pull/2716) + +Bug fixes: + +- Fixed segfault in multithreaded environments when using + `scoped_ostream_redirect`. + [#2675](https://github.com/pybind/pybind11/pull/2675) +- Leave docstring unset when all docstring-related options are disabled, + rather than set an empty string. + [#2745](https://github.com/pybind/pybind11/pull/2745) +- The module key in builtins that pybind11 uses to store its internals + changed from std::string to a python str type (more natural on Python + 2, no change on Python 3). + [#2814](https://github.com/pybind/pybind11/pull/2814) +- Fixed assertion error related to unhandled (later overwritten) + exception in CPython 3.8 and 3.9 debug builds. + [#2685](https://github.com/pybind/pybind11/pull/2685) +- Fix `py::gil_scoped_acquire` assert with CPython 3.9 debug build. + [#2683](https://github.com/pybind/pybind11/pull/2683) +- Fix issue with a test failing on pytest 6.2. + [#2741](https://github.com/pybind/pybind11/pull/2741) + +Warning fixes: + +- Fix warning modifying constructor parameter 'flag' that shadows a + field of 'set_flag' `[-Wshadow-field-in-constructor-modified]`. + [#2780](https://github.com/pybind/pybind11/pull/2780) +- Suppressed some deprecation warnings about old-style + `__init__`/`__setstate__` in the tests. + [#2759](https://github.com/pybind/pybind11/pull/2759) + +Valgrind work: + +- Fix invalid access when calling a pybind11 `__init__` on a + non-pybind11 class instance. + [#2755](https://github.com/pybind/pybind11/pull/2755) +- Fixed various minor memory leaks in pybind11's test suite. + [#2758](https://github.com/pybind/pybind11/pull/2758) +- Resolved memory leak in cpp_function initialization when exceptions + occurred. [#2756](https://github.com/pybind/pybind11/pull/2756) +- Added a Valgrind build, checking for leaks and memory-related UB, + to CI. [#2746](https://github.com/pybind/pybind11/pull/2746) + +Compiler support: + +- Intel compiler was not activating C++14 support due to a broken + define. [#2679](https://github.com/pybind/pybind11/pull/2679) +- Support ICC and NVIDIA HPC SDK in C++17 mode. + [#2729](https://github.com/pybind/pybind11/pull/2729) +- Support Intel OneAPI compiler (ICC 20.2) and add to CI. + [#2573](https://github.com/pybind/pybind11/pull/2573) + +## v2.6.1 (Nov 11, 2020) + +- `py::exec`, `py::eval`, and `py::eval_file` now add the builtins + module as `"__builtins__"` to their `globals` argument, better + matching `exec` and `eval` in pure Python. + [#2616](https://github.com/pybind/pybind11/pull/2616) +- `setup_helpers` will no longer set a minimum macOS version higher than + the current version. + [#2622](https://github.com/pybind/pybind11/pull/2622) +- Allow deleting static properties. + [#2629](https://github.com/pybind/pybind11/pull/2629) +- Seal a leak in `def_buffer`, cleaning up the `capture` object after + the `class_` object goes out of scope. + [#2634](https://github.com/pybind/pybind11/pull/2634) +- `pybind11_INCLUDE_DIRS` was incorrect, potentially causing a + regression if it was expected to include `PYTHON_INCLUDE_DIRS` (please + use targets instead). + [#2636](https://github.com/pybind/pybind11/pull/2636) +- Added parameter names to the `py::enum_` constructor and methods, + avoiding `arg0` in the generated docstrings. + [#2637](https://github.com/pybind/pybind11/pull/2637) +- Added `needs_recompile` optional function to the `ParallelCompiler` + helper, to allow a recompile to be skipped based on a user-defined + function. [#2643](https://github.com/pybind/pybind11/pull/2643) + +## v2.6.0 (Oct 21, 2020) + +See `upgrade-guide-2.6` for help upgrading to the new version. + +New features: + +- Keyword-only arguments supported in Python 2 or 3 with + `py::kw_only()`. + [#2100](https://github.com/pybind/pybind11/pull/2100) +- Positional-only arguments supported in Python 2 or 3 with + `py::pos_only()`. + [#2459](https://github.com/pybind/pybind11/pull/2459) +- `py::is_final()` class modifier to block subclassing (CPython only). + [#2151](https://github.com/pybind/pybind11/pull/2151) +- Added `py::prepend()`, allowing a function to be placed at the + beginning of the overload chain. + [#1131](https://github.com/pybind/pybind11/pull/1131) +- Access to the type object now provided with `py::type::of()` and + `py::type::of(h)`. + [#2364](https://github.com/pybind/pybind11/pull/2364) +- Perfect forwarding support for methods. + [#2048](https://github.com/pybind/pybind11/pull/2048) +- Added `py::error_already_set::discard_as_unraisable()`. + [#2372](https://github.com/pybind/pybind11/pull/2372) +- `py::hash` is now public. + [#2217](https://github.com/pybind/pybind11/pull/2217) +- `py::class_` is now supported. Note that writing to one + data member of the union and reading another (type punning) is UB in + C++. Thus pybind11-bound enums should never be used for such + conversions. [#2320](https://github.com/pybind/pybind11/pull/2320). +- Classes now check local scope when registering members, allowing a + subclass to have a member with the same name as a parent (such as an + enum). [#2335](https://github.com/pybind/pybind11/pull/2335) + +Code correctness features: + +- Error now thrown when `__init__` is forgotten on subclasses. + [#2152](https://github.com/pybind/pybind11/pull/2152) +- Throw error if conversion to a pybind11 type if the Python object + isn't a valid instance of that type, such as `py::bytes(o)` when + `py::object o` isn't a bytes instance. + [#2349](https://github.com/pybind/pybind11/pull/2349) +- Throw if conversion to `str` fails. + [#2477](https://github.com/pybind/pybind11/pull/2477) + +API changes: + +- `py::module` was renamed `py::module_` to avoid issues with C++20 when + used unqualified, but an alias `py::module` is provided for backward + compatibility. [#2489](https://github.com/pybind/pybind11/pull/2489) +- Public constructors for `py::module_` have been deprecated; please use + `pybind11::module_::create_extension_module` if you were using the + public constructor (fairly rare after `PYBIND11_MODULE` was + introduced). [#2552](https://github.com/pybind/pybind11/pull/2552) +- `PYBIND11_OVERLOAD*` macros and `get_overload` function replaced by + correctly-named `PYBIND11_OVERRIDE*` and `get_override`, fixing + inconsistencies in the presence of a closing `;` in these macros. + `get_type_overload` is deprecated. + [#2325](https://github.com/pybind/pybind11/pull/2325) + +Packaging / building improvements: + +- The Python package was reworked to be more powerful and useful. + [#2433](https://github.com/pybind/pybind11/pull/2433) + - `build-setuptools` is easier thanks to a new + `pybind11.setup_helpers` module, which provides utilities to use + setuptools with pybind11. It can be used via PEP 518, + `setup_requires`, or by directly importing or copying + `setup_helpers.py` into your project. + - CMake configuration files are now included in the Python package. + Use `pybind11.get_cmake_dir()` or `python -m pybind11 --cmakedir` to + get the directory with the CMake configuration files, or include the + site-packages location in your `CMAKE_MODULE_PATH`. Or you can use + the new `pybind11[global]` extra when you install `pybind11`, which + installs the CMake files and headers into your base environment in + the standard location. + - `pybind11-config` is another way to write `python -m pybind11` if + you have your PATH set up. + - Added external typing support to the helper module, code from + `import pybind11` can now be type checked. + [#2588](https://github.com/pybind/pybind11/pull/2588) +- Minimum CMake required increased to 3.4. + [#2338](https://github.com/pybind/pybind11/pull/2338) and + [#2370](https://github.com/pybind/pybind11/pull/2370) + - Full integration with CMake's C++ standard system and compile + features replaces `PYBIND11_CPP_STANDARD`. + - Generated config file is now portable to different + Python/compiler/CMake versions. + - Virtual environments prioritized if `PYTHON_EXECUTABLE` is not set + (`venv`, `virtualenv`, and `conda`) (similar to the new FindPython + mode). + - Other CMake features now natively supported, like + `CMAKE_INTERPROCEDURAL_OPTIMIZATION`, + `set(CMAKE_CXX_VISIBILITY_PRESET hidden)`. + - `CUDA` as a language is now supported. + - Helper functions `pybind11_strip`, `pybind11_extension`, + `pybind11_find_import` added, see `cmake/index`. + - Optional `find-python-mode` and `nopython-mode` with CMake. + [#2370](https://github.com/pybind/pybind11/pull/2370) +- Uninstall target added. + [#2265](https://github.com/pybind/pybind11/pull/2265) and + [#2346](https://github.com/pybind/pybind11/pull/2346) +- `pybind11_add_module()` now accepts an optional `OPT_SIZE` flag that + switches the binding target to size-based optimization if the global + build type can not always be fixed to `MinSizeRel` (except in debug + mode, where optimizations remain disabled). `MinSizeRel` or this flag + reduces binary size quite substantially (~25% on some platforms). + [#2463](https://github.com/pybind/pybind11/pull/2463) + +Smaller or developer focused features and fixes: + +- Moved `mkdoc.py` to a new repo, + [pybind11-mkdoc](https://github.com/pybind/pybind11-mkdoc). There are + no longer submodules in the main repo. +- `py::memoryview` segfault fix and update, with new + `py::memoryview::from_memory` in Python 3, and documentation. + [#2223](https://github.com/pybind/pybind11/pull/2223) +- Fix for `buffer_info` on Python 2. + [#2503](https://github.com/pybind/pybind11/pull/2503) +- If `__eq__` defined but not `__hash__`, `__hash__` is now set to + `None`. [#2291](https://github.com/pybind/pybind11/pull/2291) +- `py::ellipsis` now also works on Python 2. + [#2360](https://github.com/pybind/pybind11/pull/2360) +- Pointer to `std::tuple` & `std::pair` supported in cast. + [#2334](https://github.com/pybind/pybind11/pull/2334) +- Small fixes in NumPy support. `py::array` now uses `py::ssize_t` as + first argument type. + [#2293](https://github.com/pybind/pybind11/pull/2293) +- Added missing signature for `py::array`. + [#2363](https://github.com/pybind/pybind11/pull/2363) +- `unchecked_mutable_reference` has access to operator `()` and `[]` + when const. [#2514](https://github.com/pybind/pybind11/pull/2514) +- `py::vectorize` is now supported on functions that return void. + [#1969](https://github.com/pybind/pybind11/pull/1969) +- `py::capsule` supports `get_pointer` and `set_pointer`. + [#1131](https://github.com/pybind/pybind11/pull/1131) +- Fix crash when different instances share the same pointer of the same + type. [#2252](https://github.com/pybind/pybind11/pull/2252) +- Fix for `py::len` not clearing Python's error state when it fails and + throws. [#2575](https://github.com/pybind/pybind11/pull/2575) +- Bugfixes related to more extensive testing, new GitHub Actions CI. + [#2321](https://github.com/pybind/pybind11/pull/2321) +- Bug in timezone issue in Eastern hemisphere midnight fixed. + [#2438](https://github.com/pybind/pybind11/pull/2438) +- `std::chrono::time_point` now works when the resolution is not the + same as the system. + [#2481](https://github.com/pybind/pybind11/pull/2481) +- Bug fixed where `py::array_t` could accept arrays that did not match + the requested ordering. + [#2484](https://github.com/pybind/pybind11/pull/2484) +- Avoid a segfault on some compilers when types are removed in Python. + [#2564](https://github.com/pybind/pybind11/pull/2564) +- `py::arg::none()` is now also respected when passing keyword + arguments. [#2611](https://github.com/pybind/pybind11/pull/2611) +- PyPy fixes, PyPy 7.3.x now supported, including PyPy3. (Known issue + with PyPy2 and Windows + [#2596](https://github.com/pybind/pybind11/issues/2596)). + [#2146](https://github.com/pybind/pybind11/pull/2146) +- CPython 3.9.0 workaround for undefined behavior (macOS segfault). + [#2576](https://github.com/pybind/pybind11/pull/2576) +- CPython 3.9 warning fixes. + [#2253](https://github.com/pybind/pybind11/pull/2253) +- Improved C++20 support, now tested in CI. + [#2489](https://github.com/pybind/pybind11/pull/2489) + [#2599](https://github.com/pybind/pybind11/pull/2599) +- Improved but still incomplete debug Python interpreter support. + [#2025](https://github.com/pybind/pybind11/pull/2025) +- NVCC (CUDA 11) now supported and tested in CI. + [#2461](https://github.com/pybind/pybind11/pull/2461) +- NVIDIA PGI compilers now supported and tested in CI. + [#2475](https://github.com/pybind/pybind11/pull/2475) +- At least Intel 18 now explicitly required when compiling with Intel. + [#2577](https://github.com/pybind/pybind11/pull/2577) +- Extensive style checking in CI, with + [pre-commit](https://pre-commit.com) support. Code modernization, + checked by clang-tidy. +- Expanded docs, including new main page, new installing section, and + CMake helpers page, along with over a dozen new sections on existing + pages. +- In GitHub, new docs for contributing and new issue templates. + +## v2.5.0 (Mar 31, 2020) + +- Use C++17 fold expressions in type casters, if available. This can + improve performance during overload resolution when functions have + multiple arguments. + [#2043](https://github.com/pybind/pybind11/pull/2043). +- Changed include directory resolution in `pybind11/__init__.py` and + installation in `setup.py`. This fixes a number of open issues where + pybind11 headers could not be found in certain environments. + [#1995](https://github.com/pybind/pybind11/pull/1995). +- C++20 `char8_t` and `u8string` support. + [#2026](https://github.com/pybind/pybind11/pull/2026). +- CMake: search for Python 3.9. + [bb9c91](https://github.com/pybind/pybind11/commit/bb9c91). +- Fixes for MSYS-based build environments. + [#2087](https://github.com/pybind/pybind11/pull/2087), + [#2053](https://github.com/pybind/pybind11/pull/2053). +- STL bindings for `std::vector<...>::clear`. + [#2074](https://github.com/pybind/pybind11/pull/2074). +- Read-only flag for `py::buffer`. + [#1466](https://github.com/pybind/pybind11/pull/1466). +- Exception handling during module initialization. + [bf2b031](https://github.com/pybind/pybind11/commit/bf2b031). +- Support linking against a CPython debug build. + [#2025](https://github.com/pybind/pybind11/pull/2025). +- Fixed issues involving the availability and use of aligned `new` and + `delete`. [#1988](https://github.com/pybind/pybind11/pull/1988), + [759221](https://github.com/pybind/pybind11/commit/759221). +- Fixed a resource leak upon interpreter shutdown. + [#2020](https://github.com/pybind/pybind11/pull/2020). +- Fixed error handling in the boolean caster. + [#1976](https://github.com/pybind/pybind11/pull/1976). + +## v2.4.3 (Oct 15, 2019) + +- Adapt pybind11 to a C API convention change in Python 3.8. + [#1950](https://github.com/pybind/pybind11/pull/1950). + +## v2.4.2 (Sep 21, 2019) + +- Replaced usage of a C++14 only construct. + [#1929](https://github.com/pybind/pybind11/pull/1929). +- Made an ifdef future-proof for Python \>= 4. + [f3109d](https://github.com/pybind/pybind11/commit/f3109d). + +## v2.4.1 (Sep 20, 2019) + +- Fixed a problem involving implicit conversion from enumerations to + integers on Python 3.8. + [#1780](https://github.com/pybind/pybind11/pull/1780). + +## v2.4.0 (Sep 19, 2019) + +- Try harder to keep pybind11-internal data structures separate when + there are potential ABI incompatibilities. Fixes crashes that occurred + when loading multiple pybind11 extensions that were e.g. compiled by + GCC (libstdc++) and Clang (libc++). + [#1588](https://github.com/pybind/pybind11/pull/1588) and + [c9f5a](https://github.com/pybind/pybind11/commit/c9f5a). +- Added support for `__await__`, `__aiter__`, and `__anext__` protocols. + [#1842](https://github.com/pybind/pybind11/pull/1842). +- `pybind11_add_module()`: don't strip symbols when compiling in + `RelWithDebInfo` mode. + [#1980](https://github.com/pybind/pybind11/pull/1980). +- `enum_`: Reproduce Python behavior when comparing against invalid + values (e.g. `None`, strings, etc.). Add back support for + `__invert__()`. + [#1912](https://github.com/pybind/pybind11/pull/1912), + [#1907](https://github.com/pybind/pybind11/pull/1907). +- List insertion operation for `py::list`. Added `.empty()` to all + collection types. Added `py::set::contains()` and + `py::dict::contains()`. + [#1887](https://github.com/pybind/pybind11/pull/1887), + [#1884](https://github.com/pybind/pybind11/pull/1884), + [#1888](https://github.com/pybind/pybind11/pull/1888). +- `py::details::overload_cast_impl` is available in C++11 mode, can be + used like `overload_cast` with an additional set of parentheses. + [#1581](https://github.com/pybind/pybind11/pull/1581). +- Fixed `get_include()` on Conda. + [#1877](https://github.com/pybind/pybind11/pull/1877). +- `stl_bind.h`: negative indexing support. + [#1882](https://github.com/pybind/pybind11/pull/1882). +- Minor CMake fix to add MinGW compatibility. + [#1851](https://github.com/pybind/pybind11/pull/1851). +- GIL-related fixes. + [#1836](https://github.com/pybind/pybind11/pull/1836), + [8b90b](https://github.com/pybind/pybind11/commit/8b90b). +- Other very minor/subtle fixes and improvements. + [#1329](https://github.com/pybind/pybind11/pull/1329), + [#1910](https://github.com/pybind/pybind11/pull/1910), + [#1863](https://github.com/pybind/pybind11/pull/1863), + [#1847](https://github.com/pybind/pybind11/pull/1847), + [#1890](https://github.com/pybind/pybind11/pull/1890), + [#1860](https://github.com/pybind/pybind11/pull/1860), + [#1848](https://github.com/pybind/pybind11/pull/1848), + [#1821](https://github.com/pybind/pybind11/pull/1821), + [#1837](https://github.com/pybind/pybind11/pull/1837), + [#1833](https://github.com/pybind/pybind11/pull/1833), + [#1748](https://github.com/pybind/pybind11/pull/1748), + [#1852](https://github.com/pybind/pybind11/pull/1852). + +## v2.3.0 (June 11, 2019) + +- Significantly reduced module binary size (10-20%) when compiled in + C++11 mode with GCC/Clang, or in any mode with MSVC. Function + signatures are now always precomputed at compile time (this was + previously only available in C++14 mode for non-MSVC compilers). + [#934](https://github.com/pybind/pybind11/pull/934). + +- Add basic support for tag-based static polymorphism, where classes + provide a method to returns the desired type of an instance. + [#1326](https://github.com/pybind/pybind11/pull/1326). + +- Python type wrappers (`py::handle`, `py::object`, etc.) now support + map Python's number protocol onto C++ arithmetic operators such as + `operator+`, `operator/=`, etc. + [#1511](https://github.com/pybind/pybind11/pull/1511). + +- A number of improvements related to enumerations: + + > 1. The `enum_` implementation was rewritten from scratch to reduce + > code bloat. Rather than instantiating a full implementation for + > each enumeration, most code is now contained in a generic base + > class. [#1511](https://github.com/pybind/pybind11/pull/1511). + > 2. The `value()` method of `py::enum_` now accepts an optional + > docstring that will be shown in the documentation of the + > associated enumeration. + > [#1160](https://github.com/pybind/pybind11/pull/1160). + > 3. check for already existing enum value and throw an error if + > present. [#1453](https://github.com/pybind/pybind11/pull/1453). + +- Support for over-aligned type allocation via C++17's aligned `new` + statement. [#1582](https://github.com/pybind/pybind11/pull/1582). + +- Added `py::ellipsis()` method for slicing of multidimensional NumPy + arrays [#1502](https://github.com/pybind/pybind11/pull/1502). + +- Numerous Improvements to the `mkdoc.py` script for extracting + documentation from C++ header files. + [#1788](https://github.com/pybind/pybind11/pull/1788). + +- `pybind11_add_module()`: allow including Python as a `SYSTEM` include + path. [#1416](https://github.com/pybind/pybind11/pull/1416). + +- `pybind11/stl.h` does not convert strings to `vector` anymore. + [#1258](https://github.com/pybind/pybind11/issues/1258). + +- Mark static methods as such to fix auto-generated Sphinx + documentation. [#1732](https://github.com/pybind/pybind11/pull/1732). + +- Re-throw forced unwind exceptions (e.g. during pthread termination). + [#1208](https://github.com/pybind/pybind11/pull/1208). + +- Added `__contains__` method to the bindings of maps (`std::map`, + `std::unordered_map`). + [#1767](https://github.com/pybind/pybind11/pull/1767). + +- Improvements to `gil_scoped_acquire`. + [#1211](https://github.com/pybind/pybind11/pull/1211). + +- Type caster support for `std::deque`. + [#1609](https://github.com/pybind/pybind11/pull/1609). + +- Support for `std::unique_ptr` holders, whose deleters differ between a + base and derived class. + [#1353](https://github.com/pybind/pybind11/pull/1353). + +- Construction of STL array/vector-like data structures from iterators. + Added an `extend()` operation. + [#1709](https://github.com/pybind/pybind11/pull/1709), + +- CMake build system improvements for projects that include non-C++ + files (e.g. plain C, CUDA) in `pybind11_add_module` et al. + [#1678](https://github.com/pybind/pybind11/pull/1678). + +- Fixed asynchronous invocation and deallocation of Python functions + wrapped in `std::function`. + [#1595](https://github.com/pybind/pybind11/pull/1595). + +- Fixes regarding return value policy propagation in STL type casters. + [#1603](https://github.com/pybind/pybind11/pull/1603). + +- Fixed scoped enum comparisons. + [#1571](https://github.com/pybind/pybind11/pull/1571). + +- Fixed iostream redirection for code that releases the GIL. + [#1368](https://github.com/pybind/pybind11/pull/1368), + +- A number of CI-related fixes. + [#1757](https://github.com/pybind/pybind11/pull/1757), + [#1744](https://github.com/pybind/pybind11/pull/1744), + [#1670](https://github.com/pybind/pybind11/pull/1670). + +## v2.2.4 (September 11, 2018) + +- Use new Python 3.7 Thread Specific Storage (TSS) implementation if + available. [#1454](https://github.com/pybind/pybind11/pull/1454), + [#1517](https://github.com/pybind/pybind11/pull/1517). +- Fixes for newer MSVC versions and C++17 mode. + [#1347](https://github.com/pybind/pybind11/pull/1347), + [#1462](https://github.com/pybind/pybind11/pull/1462). +- Propagate return value policies to type-specific casters when casting + STL containers. + [#1455](https://github.com/pybind/pybind11/pull/1455). +- Allow ostream-redirection of more than 1024 characters. + [#1479](https://github.com/pybind/pybind11/pull/1479). +- Set `Py_DEBUG` define when compiling against a debug Python build. + [#1438](https://github.com/pybind/pybind11/pull/1438). +- Untangle integer logic in number type caster to work for custom types + that may only be castable to a restricted set of builtin types. + [#1442](https://github.com/pybind/pybind11/pull/1442). +- CMake build system: Remember Python version in cache file. + [#1434](https://github.com/pybind/pybind11/pull/1434). +- Fix for custom smart pointers: use `std::addressof` to obtain holder + address instead of `operator&`. + [#1435](https://github.com/pybind/pybind11/pull/1435). +- Properly report exceptions thrown during module initialization. + [#1362](https://github.com/pybind/pybind11/pull/1362). +- Fixed a segmentation fault when creating empty-shaped NumPy array. + [#1371](https://github.com/pybind/pybind11/pull/1371). +- The version of Intel C++ compiler must be \>= 2017, and this is now + checked by the header files. + [#1363](https://github.com/pybind/pybind11/pull/1363). +- A few minor typo fixes and improvements to the test suite, and patches + that silence compiler warnings. +- Vectors now support construction from generators, as well as + `extend()` from a list or generator. + [#1496](https://github.com/pybind/pybind11/pull/1496). + +## v2.2.3 (April 29, 2018) + +- The pybind11 header location detection was replaced by a new + implementation that no longer depends on `pip` internals (the recently + released `pip` 10 has restricted access to this API). + [#1190](https://github.com/pybind/pybind11/pull/1190). +- Small adjustment to an implementation detail to work around a compiler + segmentation fault in Clang 3.3/3.4. + [#1350](https://github.com/pybind/pybind11/pull/1350). +- The minimal supported version of the Intel compiler was \>= 17.0 since + pybind11 v2.1. This check is now explicit, and a compile-time error is + raised if the compiler meet the requirement. + [#1363](https://github.com/pybind/pybind11/pull/1363). +- Fixed an endianness-related fault in the test suite. + [#1287](https://github.com/pybind/pybind11/pull/1287). + +## v2.2.2 (February 7, 2018) + +- Fixed a segfault when combining embedded interpreter + shutdown/reinitialization with external loaded pybind11 modules. + [#1092](https://github.com/pybind/pybind11/pull/1092). +- Eigen support: fixed a bug where Nx1/1xN numpy inputs couldn't be + passed as arguments to Eigen vectors (which for Eigen are simply + compile-time fixed Nx1/1xN matrices). + [#1106](https://github.com/pybind/pybind11/pull/1106). +- Clarified to license by moving the licensing of contributions from + `LICENSE` into `CONTRIBUTING.md`: the licensing of contributions is + not actually part of the software license as distributed. This isn't + meant to be a substantial change in the licensing of the project, but + addresses concerns that the clause made the license non-standard. + [#1109](https://github.com/pybind/pybind11/issues/1109). +- Fixed a regression introduced in 2.1 that broke binding functions with + lvalue character literal arguments. + [#1128](https://github.com/pybind/pybind11/pull/1128). +- MSVC: fix for compilation failures under /permissive-, and added the + flag to the appveyor test suite. + [#1155](https://github.com/pybind/pybind11/pull/1155). +- Fixed `__qualname__` generation, and in turn, fixes how class names + (especially nested class names) are shown in generated docstrings. + [#1171](https://github.com/pybind/pybind11/pull/1171). +- Updated the FAQ with a suggested project citation reference. + [#1189](https://github.com/pybind/pybind11/pull/1189). +- Added fixes for deprecation warnings when compiled under C++17 with + `-Wdeprecated` turned on, and add `-Wdeprecated` to the test suite + compilation flags. + [#1191](https://github.com/pybind/pybind11/pull/1191). +- Fixed outdated PyPI URLs in `setup.py`. + [#1213](https://github.com/pybind/pybind11/pull/1213). +- Fixed a refcount leak for arguments that end up in a `py::args` + argument for functions with both fixed positional and `py::args` + arguments. [#1216](https://github.com/pybind/pybind11/pull/1216). +- Fixed a potential segfault resulting from possible premature + destruction of `py::args`/`py::kwargs` arguments with overloaded + functions. [#1223](https://github.com/pybind/pybind11/pull/1223). +- Fixed `del map[item]` for a `stl_bind.h` bound stl map. + [#1229](https://github.com/pybind/pybind11/pull/1229). +- Fixed a regression from v2.1.x where the aggregate initialization + could unintentionally end up at a constructor taking a templated + `std::initializer_list` argument. + [#1249](https://github.com/pybind/pybind11/pull/1249). +- Fixed an issue where calling a function with a keep_alive policy on + the same nurse/patient pair would cause the internal patient storage + to needlessly grow (unboundedly, if the nurse is long-lived). + [#1251](https://github.com/pybind/pybind11/issues/1251). +- Various other minor fixes. + +## v2.2.1 (September 14, 2017) + +- Added `py::module_::reload()` member function for reloading a module. + [#1040](https://github.com/pybind/pybind11/pull/1040). +- Fixed a reference leak in the number converter. + [#1078](https://github.com/pybind/pybind11/pull/1078). +- Fixed compilation with Clang on host GCC \< 5 (old libstdc++ which + isn't fully C++11 compliant). + [#1062](https://github.com/pybind/pybind11/pull/1062). +- Fixed a regression where the automatic `std::vector` caster + would fail to compile. The same fix also applies to any container + which returns element proxies instead of references. + [#1053](https://github.com/pybind/pybind11/pull/1053). +- Fixed a regression where the `py::keep_alive` policy could not be + applied to constructors. + [#1065](https://github.com/pybind/pybind11/pull/1065). +- Fixed a nullptr dereference when loading a `py::module_local` type + that's only registered in an external module. + [#1058](https://github.com/pybind/pybind11/pull/1058). +- Fixed implicit conversion of accessors to types derived from + `py::object`. [#1076](https://github.com/pybind/pybind11/pull/1076). +- The `name` in `PYBIND11_MODULE(name, variable)` can now be a macro. + [#1082](https://github.com/pybind/pybind11/pull/1082). +- Relaxed overly strict `py::pickle()` check for matching get and set + types. [#1064](https://github.com/pybind/pybind11/pull/1064). +- Conversion errors now try to be more informative when it's likely that + a missing header is the cause (e.g. forgetting ``). + [#1077](https://github.com/pybind/pybind11/pull/1077). + +## v2.2.0 (August 31, 2017) + +- Support for embedding the Python interpreter. See the + `documentation page ` for a full overview of the + new features. [#774](https://github.com/pybind/pybind11/pull/774), + [#889](https://github.com/pybind/pybind11/pull/889), + [#892](https://github.com/pybind/pybind11/pull/892), + [#920](https://github.com/pybind/pybind11/pull/920). + + ```cpp + #include + namespace py = pybind11; + + int main() { + py::scoped_interpreter guard{}; // start the interpreter and keep it alive + + py::print("Hello, World!"); // use the Python API + } + ``` + +- Support for inheriting from multiple C++ bases in Python. + [#693](https://github.com/pybind/pybind11/pull/693). + + ```python + from cpp_module import CppBase1, CppBase2 + + + class PyDerived(CppBase1, CppBase2): + def __init__(self): + CppBase1.__init__(self) # C++ bases must be initialized explicitly + CppBase2.__init__(self) + ``` + +- `PYBIND11_MODULE` is now the preferred way to create module entry + points. `PYBIND11_PLUGIN` is deprecated. See `macros` for details. + [#879](https://github.com/pybind/pybind11/pull/879). + + ```cpp + // new + PYBIND11_MODULE(example, m) { + m.def("add", [](int a, int b) { return a + b; }); + } + + // old + PYBIND11_PLUGIN(example) { + py::module m("example"); + m.def("add", [](int a, int b) { return a + b; }); + return m.ptr(); + } + ``` + +- pybind11's headers and build system now more strictly enforce hidden + symbol visibility for extension modules. This should be seamless for + most users, but see the `upgrade` if you use a custom build system. + [#995](https://github.com/pybind/pybind11/pull/995). + +- Support for `py::module_local` types which allow multiple modules to + export the same C++ types without conflicts. This is useful for opaque + types like `std::vector`. `py::bind_vector` and `py::bind_map` + now default to `py::module_local` if their elements are builtins or + local types. See `module_local` for details. + [#949](https://github.com/pybind/pybind11/pull/949), + [#981](https://github.com/pybind/pybind11/pull/981), + [#995](https://github.com/pybind/pybind11/pull/995), + [#997](https://github.com/pybind/pybind11/pull/997). + +- Custom constructors can now be added very easily using lambdas or + factory functions which return a class instance by value, pointer or + holder. This supersedes the old placement-new `__init__` technique. + See `custom_constructors` for details. + [#805](https://github.com/pybind/pybind11/pull/805), + [#1014](https://github.com/pybind/pybind11/pull/1014). + + ```cpp + struct Example { + Example(std::string); + }; + + py::class_(m, "Example") + .def(py::init()) // existing constructor + .def(py::init([](int n) { // custom constructor + return std::make_unique(std::to_string(n)); + })); + ``` + +- Similarly to custom constructors, pickling support functions are now + bound using the `py::pickle()` adaptor which improves type safety. See + the `upgrade` and `pickling` for details. + [#1038](https://github.com/pybind/pybind11/pull/1038). + +- Builtin support for converting C++17 standard library types and + general conversion improvements: + + 1. C++17 `std::variant` is supported right out of the box. C++11/14 + equivalents (e.g. `boost::variant`) can also be added with a + simple user-defined specialization. See `cpp17_container_casters` + for details. [#811](https://github.com/pybind/pybind11/pull/811), + [#845](https://github.com/pybind/pybind11/pull/845), + [#989](https://github.com/pybind/pybind11/pull/989). + 2. Out-of-the-box support for C++17 `std::string_view`. + [#906](https://github.com/pybind/pybind11/pull/906). + 3. Improved compatibility of the builtin `optional` converter. + [#874](https://github.com/pybind/pybind11/pull/874). + 4. The `bool` converter now accepts `numpy.bool_` and types which + define `__bool__` (Python 3.x) or `__nonzero__` (Python 2.7). + [#925](https://github.com/pybind/pybind11/pull/925). + 5. C++-to-Python casters are now more efficient and move elements out + of rvalue containers whenever possible. + [#851](https://github.com/pybind/pybind11/pull/851), + [#936](https://github.com/pybind/pybind11/pull/936), + [#938](https://github.com/pybind/pybind11/pull/938). + 6. Fixed `bytes` to `std::string/char*` conversion on Python 3. + [#817](https://github.com/pybind/pybind11/pull/817). + 7. Fixed lifetime of temporary C++ objects created in Python-to-C++ + conversions. [#924](https://github.com/pybind/pybind11/pull/924). + +- Scope guard call policy for RAII types, e.g. + `py::call_guard()`, + `py::call_guard()`. See `call_policies` + for details. [#740](https://github.com/pybind/pybind11/pull/740). + +- Utility for redirecting C++ streams to Python (e.g. `std::cout` -\> + `sys.stdout`). Scope guard `py::scoped_ostream_redirect` in C++ and a + context manager in Python. See `ostream_redirect`. + [#1009](https://github.com/pybind/pybind11/pull/1009). + +- Improved handling of types and exceptions across module boundaries. + [#915](https://github.com/pybind/pybind11/pull/915), + [#951](https://github.com/pybind/pybind11/pull/951), + [#995](https://github.com/pybind/pybind11/pull/995). + +- Fixed destruction order of `py::keep_alive` nurse/patient objects in + reference cycles. + [#856](https://github.com/pybind/pybind11/pull/856). + +- NumPy and buffer protocol related improvements: + + 1. Support for negative strides in Python buffer objects/numpy + arrays. This required changing integers from unsigned to signed + for the related C++ APIs. Note: If you have compiler warnings + enabled, you may notice some new conversion warnings after + upgrading. These can be resolved with `static_cast`. + [#782](https://github.com/pybind/pybind11/pull/782). + 2. Support `std::complex` and arrays inside `PYBIND11_NUMPY_DTYPE`. + [#831](https://github.com/pybind/pybind11/pull/831), + [#832](https://github.com/pybind/pybind11/pull/832). + 3. Support for constructing `py::buffer_info` and `py::arrays` using + arbitrary containers or iterators instead of requiring a + `std::vector`. + [#788](https://github.com/pybind/pybind11/pull/788), + [#822](https://github.com/pybind/pybind11/pull/822), + [#860](https://github.com/pybind/pybind11/pull/860). + 4. Explicitly check numpy version and require \>= 1.7.0. + [#819](https://github.com/pybind/pybind11/pull/819). + +- Support for allowing/prohibiting `None` for specific arguments and + improved `None` overload resolution order. See `none_arguments` for + details. [#843](https://github.com/pybind/pybind11/pull/843). + [#859](https://github.com/pybind/pybind11/pull/859). + +- Added `py::exec()` as a shortcut for `py::eval()` + and support for C++11 raw string literals as input. See `eval`. + [#766](https://github.com/pybind/pybind11/pull/766), + [#827](https://github.com/pybind/pybind11/pull/827). + +- `py::vectorize()` ignores non-vectorizable arguments and supports + member functions. + [#762](https://github.com/pybind/pybind11/pull/762). + +- Support for bound methods as callbacks (`pybind11/functional.h`). + [#815](https://github.com/pybind/pybind11/pull/815). + +- Allow aliasing pybind11 methods: `cls.attr("foo") = cls.attr("bar")`. + [#802](https://github.com/pybind/pybind11/pull/802). + +- Don't allow mixed static/non-static overloads. + [#804](https://github.com/pybind/pybind11/pull/804). + +- Fixed overriding static properties in derived classes. + [#784](https://github.com/pybind/pybind11/pull/784). + +- Added support for write only properties. + [#1144](https://github.com/pybind/pybind11/pull/1144). + +- Improved deduction of member functions of a derived class when its + bases aren't registered with pybind11. + [#855](https://github.com/pybind/pybind11/pull/855). + + ```cpp + struct Base { + int foo() { return 42; } + } + + struct Derived : Base {} + + // Now works, but previously required also binding `Base` + py::class_(m, "Derived") + .def("foo", &Derived::foo); // function is actually from `Base` + ``` + +- The implementation of `py::init<>` now uses C++11 brace initialization + syntax to construct instances, which permits binding implicit + constructors of aggregate types. + [#1015](https://github.com/pybind/pybind11/pull/1015). + + > ```cpp + > struct Aggregate { + > int a; + > std::string b; + > }; + > + > py::class_(m, "Aggregate") + > .def(py::init()); + > ``` + +- Fixed issues with multiple inheritance with offset base/derived + pointers. [#812](https://github.com/pybind/pybind11/pull/812), + [#866](https://github.com/pybind/pybind11/pull/866), + [#960](https://github.com/pybind/pybind11/pull/960). + +- Fixed reference leak of type objects. + [#1030](https://github.com/pybind/pybind11/pull/1030). + +- Improved support for the `/std:c++14` and `/std:c++latest` modes on + MSVC 2017. [#841](https://github.com/pybind/pybind11/pull/841), + [#999](https://github.com/pybind/pybind11/pull/999). + +- Fixed detection of private operator new on MSVC. + [#893](https://github.com/pybind/pybind11/pull/893), + [#918](https://github.com/pybind/pybind11/pull/918). + +- Intel C++ compiler compatibility fixes. + [#937](https://github.com/pybind/pybind11/pull/937). + +- Fixed implicit conversion of `py::enum_` to integer types on Python + 2.7. [#821](https://github.com/pybind/pybind11/pull/821). + +- Added `py::hash` to fetch the hash value of Python objects, and + `.def(hash(py::self))` to provide the C++ `std::hash` as the Python + `__hash__` method. + [#1034](https://github.com/pybind/pybind11/pull/1034). + +- Fixed `__truediv__` on Python 2 and `__itruediv__` on Python 3. + [#867](https://github.com/pybind/pybind11/pull/867). + +- `py::capsule` objects now support the `name` attribute. This is useful + for interfacing with `scipy.LowLevelCallable`. + [#902](https://github.com/pybind/pybind11/pull/902). + +- Fixed `py::make_iterator`'s `__next__()` for past-the-end calls. + [#897](https://github.com/pybind/pybind11/pull/897). + +- Added `error_already_set::matches()` for checking Python exceptions. + [#772](https://github.com/pybind/pybind11/pull/772). + +- Deprecated `py::error_already_set::clear()`. It's no longer needed + following a simplification of the `py::error_already_set` class. + [#954](https://github.com/pybind/pybind11/pull/954). + +- Deprecated `py::handle::operator==()` in favor of `py::handle::is()` + [#825](https://github.com/pybind/pybind11/pull/825). + +- Deprecated `py::object::borrowed`/`py::object::stolen`. Use + `py::object::borrowed_t{}`/`py::object::stolen_t{}` instead. + [#771](https://github.com/pybind/pybind11/pull/771). + +- Changed internal data structure versioning to avoid conflicts between + modules compiled with different revisions of pybind11. + [#1012](https://github.com/pybind/pybind11/pull/1012). + +- Additional compile-time and run-time error checking and more + informative messages. + [#786](https://github.com/pybind/pybind11/pull/786), + [#794](https://github.com/pybind/pybind11/pull/794), + [#803](https://github.com/pybind/pybind11/pull/803). + +- Various minor improvements and fixes. + [#764](https://github.com/pybind/pybind11/pull/764), + [#791](https://github.com/pybind/pybind11/pull/791), + [#795](https://github.com/pybind/pybind11/pull/795), + [#840](https://github.com/pybind/pybind11/pull/840), + [#844](https://github.com/pybind/pybind11/pull/844), + [#846](https://github.com/pybind/pybind11/pull/846), + [#849](https://github.com/pybind/pybind11/pull/849), + [#858](https://github.com/pybind/pybind11/pull/858), + [#862](https://github.com/pybind/pybind11/pull/862), + [#871](https://github.com/pybind/pybind11/pull/871), + [#872](https://github.com/pybind/pybind11/pull/872), + [#881](https://github.com/pybind/pybind11/pull/881), + [#888](https://github.com/pybind/pybind11/pull/888), + [#899](https://github.com/pybind/pybind11/pull/899), + [#928](https://github.com/pybind/pybind11/pull/928), + [#931](https://github.com/pybind/pybind11/pull/931), + [#944](https://github.com/pybind/pybind11/pull/944), + [#950](https://github.com/pybind/pybind11/pull/950), + [#952](https://github.com/pybind/pybind11/pull/952), + [#962](https://github.com/pybind/pybind11/pull/962), + [#965](https://github.com/pybind/pybind11/pull/965), + [#970](https://github.com/pybind/pybind11/pull/970), + [#978](https://github.com/pybind/pybind11/pull/978), + [#979](https://github.com/pybind/pybind11/pull/979), + [#986](https://github.com/pybind/pybind11/pull/986), + [#1020](https://github.com/pybind/pybind11/pull/1020), + [#1027](https://github.com/pybind/pybind11/pull/1027), + [#1037](https://github.com/pybind/pybind11/pull/1037). + +- Testing improvements. + [#798](https://github.com/pybind/pybind11/pull/798), + [#882](https://github.com/pybind/pybind11/pull/882), + [#898](https://github.com/pybind/pybind11/pull/898), + [#900](https://github.com/pybind/pybind11/pull/900), + [#921](https://github.com/pybind/pybind11/pull/921), + [#923](https://github.com/pybind/pybind11/pull/923), + [#963](https://github.com/pybind/pybind11/pull/963). + +## v2.1.1 (April 7, 2017) + +- Fixed minimum version requirement for MSVC 2015u3 + [#773](https://github.com/pybind/pybind11/pull/773). + +## v2.1.0 (March 22, 2017) + +- pybind11 now performs function overload resolution in two phases. The + first phase only considers exact type matches, while the second allows + for implicit conversions to take place. A special `noconvert()` syntax + can be used to completely disable implicit conversions for specific + arguments. [#643](https://github.com/pybind/pybind11/pull/643), + [#634](https://github.com/pybind/pybind11/pull/634), + [#650](https://github.com/pybind/pybind11/pull/650). +- Fixed a regression where static properties no longer worked with + classes using multiple inheritance. The `py::metaclass` attribute is + no longer necessary (and deprecated as of this release) when binding + classes with static properties. + [#679](https://github.com/pybind/pybind11/pull/679), +- Classes bound using `pybind11` can now use custom metaclasses. + [#679](https://github.com/pybind/pybind11/pull/679), +- `py::args` and `py::kwargs` can now be mixed with other positional + arguments when binding functions using pybind11. + [#611](https://github.com/pybind/pybind11/pull/611). +- Improved support for C++11 unicode string and character types; added + extensive documentation regarding pybind11's string conversion + behavior. [#624](https://github.com/pybind/pybind11/pull/624), + [#636](https://github.com/pybind/pybind11/pull/636), + [#715](https://github.com/pybind/pybind11/pull/715). +- pybind11 can now avoid expensive copies when converting Eigen arrays + to NumPy arrays (and vice versa). + [#610](https://github.com/pybind/pybind11/pull/610). +- The "fast path" in `py::vectorize` now works for any full-size group + of C or F-contiguous arrays. The non-fast path is also faster since it + no longer performs copies of the input arguments (except when type + conversions are necessary). + [#610](https://github.com/pybind/pybind11/pull/610). +- Added fast, unchecked access to NumPy arrays via a proxy object. + [#746](https://github.com/pybind/pybind11/pull/746). +- Transparent support for class-specific `operator new` and + `operator delete` implementations. + [#755](https://github.com/pybind/pybind11/pull/755). +- Slimmer and more efficient STL-compatible iterator interface for + sequence types. [#662](https://github.com/pybind/pybind11/pull/662). +- Improved custom holder type support. + [#607](https://github.com/pybind/pybind11/pull/607). +- `nullptr` to `None` conversion fixed in various builtin type casters. + [#732](https://github.com/pybind/pybind11/pull/732). +- `enum_` now exposes its members via a special `__members__` attribute. + [#666](https://github.com/pybind/pybind11/pull/666). +- `std::vector` bindings created using `stl_bind.h` can now optionally + implement the buffer protocol. + [#488](https://github.com/pybind/pybind11/pull/488). +- Automated C++ reference documentation using doxygen and breathe. + [#598](https://github.com/pybind/pybind11/pull/598). +- Added minimum compiler version assertions. + [#727](https://github.com/pybind/pybind11/pull/727). +- Improved compatibility with C++1z. + [#677](https://github.com/pybind/pybind11/pull/677). +- Improved `py::capsule` API. Can be used to implement cleanup callbacks + that are involved at module destruction time. + [#752](https://github.com/pybind/pybind11/pull/752). +- Various minor improvements and fixes. + [#595](https://github.com/pybind/pybind11/pull/595), + [#588](https://github.com/pybind/pybind11/pull/588), + [#589](https://github.com/pybind/pybind11/pull/589), + [#603](https://github.com/pybind/pybind11/pull/603), + [#619](https://github.com/pybind/pybind11/pull/619), + [#648](https://github.com/pybind/pybind11/pull/648), + [#695](https://github.com/pybind/pybind11/pull/695), + [#720](https://github.com/pybind/pybind11/pull/720), + [#723](https://github.com/pybind/pybind11/pull/723), + [#729](https://github.com/pybind/pybind11/pull/729), + [#724](https://github.com/pybind/pybind11/pull/724), + [#742](https://github.com/pybind/pybind11/pull/742), + [#753](https://github.com/pybind/pybind11/pull/753). + +## v2.0.1 (Jan 4, 2017) + +- Fix pointer to reference error in type_caster on MSVC + [#583](https://github.com/pybind/pybind11/pull/583). +- Fixed a segmentation in the test suite due to a typo + [cd7eac](https://github.com/pybind/pybind11/commit/cd7eac). + +## v2.0.0 (Jan 1, 2017) + +- Fixed a reference counting regression affecting types with custom + metaclasses (introduced in v2.0.0-rc1). + [#571](https://github.com/pybind/pybind11/pull/571). +- Quenched a CMake policy warning. + [#570](https://github.com/pybind/pybind11/pull/570). + +## v2.0.0-rc1 (Dec 23, 2016) + +The pybind11 developers are excited to issue a release candidate of +pybind11 with a subsequent v2.0.0 release planned in early January next +year. + +An incredible amount of effort by went into pybind11 over the last ~5 +months, leading to a release that is jam-packed with exciting new +features and numerous usability improvements. The following list links +PRs or individual commits whenever applicable. + +Happy Christmas! + +- Support for binding C++ class hierarchies that make use of multiple + inheritance. [#410](https://github.com/pybind/pybind11/pull/410). + +- PyPy support: pybind11 now supports nightly builds of PyPy and will + interoperate with the future 5.7 release. No code changes are + necessary, everything "just" works as usual. Note that we only target + the Python 2.7 branch for now; support for 3.x will be added once its + `cpyext` extension support catches up. A few minor features remain + unsupported for the time being (notably dynamic attributes in custom + types). [#527](https://github.com/pybind/pybind11/pull/527). + +- Significant work on the documentation -- in particular, the monolithic + `advanced.rst` file was restructured into a easier to read + hierarchical organization. + [#448](https://github.com/pybind/pybind11/pull/448). + +- Many NumPy-related improvements: + + 1. Object-oriented API to access and modify NumPy `ndarray` + instances, replicating much of the corresponding NumPy C API + functionality. + [#402](https://github.com/pybind/pybind11/pull/402). + + 2. NumPy array `dtype` array descriptors are now first-class citizens + and are exposed via a new class `py::dtype`. + + 3. Structured dtypes can be registered using the + `PYBIND11_NUMPY_DTYPE()` macro. Special `array` constructors + accepting dtype objects were also added. + + One potential caveat involving this change: format descriptor + strings should now be accessed via `format_descriptor::format()` + (however, for compatibility purposes, the old syntax + `format_descriptor::value` will still work for non-structured data + types). [#308](https://github.com/pybind/pybind11/pull/308). + + 4. Further improvements to support structured dtypes throughout the + system. [#472](https://github.com/pybind/pybind11/pull/472), + [#474](https://github.com/pybind/pybind11/pull/474), + [#459](https://github.com/pybind/pybind11/pull/459), + [#453](https://github.com/pybind/pybind11/pull/453), + [#452](https://github.com/pybind/pybind11/pull/452), and + [#505](https://github.com/pybind/pybind11/pull/505). + + 5. Fast access operators. + [#497](https://github.com/pybind/pybind11/pull/497). + + 6. Constructors for arrays whose storage is owned by another object. + [#440](https://github.com/pybind/pybind11/pull/440). + + 7. Added constructors for `array` and `array_t` explicitly accepting + shape and strides; if strides are not provided, they are deduced + assuming C-contiguity. Also added simplified constructors for + 1-dimensional case. + + 8. Added buffer/NumPy support for `char[N]` and `std::array` + types. + + 9. Added `memoryview` wrapper type which is constructible from + `buffer_info`. + +- Eigen: many additional conversions and support for non-contiguous + arrays/slices. [#427](https://github.com/pybind/pybind11/pull/427), + [#315](https://github.com/pybind/pybind11/pull/315), + [#316](https://github.com/pybind/pybind11/pull/316), + [#312](https://github.com/pybind/pybind11/pull/312), and + [#267](https://github.com/pybind/pybind11/pull/267) + +- Incompatible changes in `class_<...>::class_()`: + + > 1. Declarations of types that provide access via the buffer + > protocol must now include the `py::buffer_protocol()` annotation + > as an argument to the `class_` constructor. + > 2. Declarations of types that require a custom metaclass (i.e. all + > classes which include static properties via commands such as + > `def_readwrite_static()`) must now include the `py::metaclass()` + > annotation as an argument to the `class_` constructor. + > + > These two changes were necessary to make type definitions in + > pybind11 future-proof, and to support PyPy via its cpyext + > mechanism. [#527](https://github.com/pybind/pybind11/pull/527). + > + > 3. This version of pybind11 uses a redesigned mechanism for + > instantiating trampoline classes that are used to override + > virtual methods from within Python. This led to the following + > user-visible syntax change: instead of + > + > ```cpp + > py::class_("MyClass") + > .alias() + > .... + > ``` + > + > write + > + > ```cpp + > py::class_("MyClass") + > .... + > ``` + > + > Importantly, both the original and the trampoline class are now + > specified as an arguments (in arbitrary order) to the + > `py::class_` template, and the `alias<..>()` call is gone. The + > new scheme has zero overhead in cases when Python doesn't + > override any functions of the underlying C++ class. [rev. + > 86d825](https://github.com/pybind/pybind11/commit/86d825). + +- Added `eval` and `eval_file` functions for evaluating expressions and + statements from a string or file. [rev. + 0d3fc3](https://github.com/pybind/pybind11/commit/0d3fc3). + +- pybind11 can now create types with a modifiable dictionary. + [#437](https://github.com/pybind/pybind11/pull/437) and + [#444](https://github.com/pybind/pybind11/pull/444). + +- Support for translation of arbitrary C++ exceptions to Python + counterparts. [#296](https://github.com/pybind/pybind11/pull/296) and + [#273](https://github.com/pybind/pybind11/pull/273). + +- Report full backtraces through mixed C++/Python code, better reporting + for import errors, fixed GIL management in exception processing. + [#537](https://github.com/pybind/pybind11/pull/537), + [#494](https://github.com/pybind/pybind11/pull/494), [rev. + e72d95](https://github.com/pybind/pybind11/commit/e72d95), and [rev. + 099d6e](https://github.com/pybind/pybind11/commit/099d6e). + +- Support for bit-level operations, comparisons, and serialization of + C++ enumerations. + [#503](https://github.com/pybind/pybind11/pull/503), + [#508](https://github.com/pybind/pybind11/pull/508), + [#380](https://github.com/pybind/pybind11/pull/380), + [#309](https://github.com/pybind/pybind11/pull/309). + [#311](https://github.com/pybind/pybind11/pull/311). + +- The `class_` constructor now accepts its template arguments in any + order. [#385](https://github.com/pybind/pybind11/pull/385). + +- Attribute and item accessors now have a more complete interface which + makes it possible to chain attributes as in + `obj.attr("a")[key].attr("b").attr("method")(1, 2, 3)`. + [#425](https://github.com/pybind/pybind11/pull/425). + +- Major redesign of the default and conversion constructors in + `pytypes.h`. [#464](https://github.com/pybind/pybind11/pull/464). + +- Added built-in support for `std::shared_ptr` holder type. It is no + longer necessary to to include a declaration of the form + `PYBIND11_DECLARE_HOLDER_TYPE(T, std::shared_ptr)` (though + continuing to do so won't cause an error). + [#454](https://github.com/pybind/pybind11/pull/454). + +- New `py::overload_cast` casting operator to select among multiple + possible overloads of a function. An example: + + > ```cpp + > py::class_(m, "Pet") + > .def("set", py::overload_cast(&Pet::set), "Set the pet's age") + > .def("set", py::overload_cast(&Pet::set), "Set the pet's name"); + > ``` + + This feature only works on C++14-capable compilers. + [#541](https://github.com/pybind/pybind11/pull/541). + +- C++ types are automatically cast to Python types, e.g. when assigning + them as an attribute. For instance, the following is now legal: + + > ```cpp + > py::module m = /* ... */ + > m.attr("constant") = 123; + > ``` + + (Previously, a `py::cast` call was necessary to avoid a compilation + error.) [#551](https://github.com/pybind/pybind11/pull/551). + +- Redesigned `pytest`-based test suite. + [#321](https://github.com/pybind/pybind11/pull/321). + +- Instance tracking to detect reference leaks in test suite. + [#324](https://github.com/pybind/pybind11/pull/324) + +- pybind11 can now distinguish between multiple different instances that + are located at the same memory address, but which have different + types. [#329](https://github.com/pybind/pybind11/pull/329). + +- Improved logic in `move` return value policy. + [#510](https://github.com/pybind/pybind11/pull/510), + [#297](https://github.com/pybind/pybind11/pull/297). + +- Generalized unpacking API to permit calling Python functions from C++ + using notation such as + `foo(a1, a2, *args, "ka"_a=1, "kb"_a=2, **kwargs)`. + [#372](https://github.com/pybind/pybind11/pull/372). + +- `py::print()` function whose behavior matches that of the native + Python `print()` function. + [#372](https://github.com/pybind/pybind11/pull/372). + +- Added `py::dict` keyword + constructor:`auto d = dict("number"_a=42, "name"_a="World");`. + [#372](https://github.com/pybind/pybind11/pull/372). + +- Added `py::str::format()` method and `_s` literal: + `py::str s = "1 + 2 = {}"_s.format(3);`. + [#372](https://github.com/pybind/pybind11/pull/372). + +- Added `py::repr()` function which is equivalent to Python's builtin + `repr()`. [#333](https://github.com/pybind/pybind11/pull/333). + +- Improved construction and destruction logic for holder types. It is + now possible to reference instances with smart pointer holder types + without constructing the holder if desired. The + `PYBIND11_DECLARE_HOLDER_TYPE` macro now accepts an optional second + parameter to indicate whether the holder type uses intrusive reference + counting. [#533](https://github.com/pybind/pybind11/pull/533) and + [#561](https://github.com/pybind/pybind11/pull/561). + +- Mapping a stateless C++ function to Python and back is now "for free" + (i.e. no extra indirections or argument conversion overheads). [rev. + 954b79](https://github.com/pybind/pybind11/commit/954b79). + +- Bindings for `std::valarray`. + [#545](https://github.com/pybind/pybind11/pull/545). + +- Improved support for C++17 capable compilers. + [#562](https://github.com/pybind/pybind11/pull/562). + +- Bindings for `std::optional`. + [#475](https://github.com/pybind/pybind11/pull/475), + [#476](https://github.com/pybind/pybind11/pull/476), + [#479](https://github.com/pybind/pybind11/pull/479), + [#499](https://github.com/pybind/pybind11/pull/499), and + [#501](https://github.com/pybind/pybind11/pull/501). + +- `stl_bind.h`: general improvements and support for `std::map` and + `std::unordered_map`. + [#490](https://github.com/pybind/pybind11/pull/490), + [#282](https://github.com/pybind/pybind11/pull/282), + [#235](https://github.com/pybind/pybind11/pull/235). + +- The `std::tuple`, `std::pair`, `std::list`, and `std::vector` type + casters now accept any Python sequence type as input. [rev. + 107285](https://github.com/pybind/pybind11/commit/107285). + +- Improved CMake Python detection on multi-architecture Linux. + [#532](https://github.com/pybind/pybind11/pull/532). + +- Infrastructure to selectively disable or enable parts of the + automatically generated docstrings. + [#486](https://github.com/pybind/pybind11/pull/486). + +- `reference` and `reference_internal` are now the default return value + properties for static and non-static properties, respectively. + [#473](https://github.com/pybind/pybind11/pull/473). (the previous + defaults were `automatic`). + [#473](https://github.com/pybind/pybind11/pull/473). + +- Support for `std::unique_ptr` with non-default deleters or no deleter + at all (`py::nodelete`). + [#384](https://github.com/pybind/pybind11/pull/384). + +- Deprecated `handle::call()` method. The new syntax to call Python + functions is simply `handle()`. It can also be invoked explicitly via + `handle::operator()`, where `X` is an optional return value policy. + +- Print more informative error messages when `make_tuple()` or `cast()` + fail. [#262](https://github.com/pybind/pybind11/pull/262). + +- Creation of holder types for classes deriving from + `std::enable_shared_from_this<>` now also works for `const` values. + [#260](https://github.com/pybind/pybind11/pull/260). + +- `make_iterator()` improvements for better compatibility with various + types (now uses prefix increment operator); it now also accepts + iterators with different begin/end types as long as they are equality + comparable. [#247](https://github.com/pybind/pybind11/pull/247). + +- `arg()` now accepts a wider range of argument types for default + values. [#244](https://github.com/pybind/pybind11/pull/244). + +- Support `keep_alive` where the nurse object may be `None`. + [#341](https://github.com/pybind/pybind11/pull/341). + +- Added constructors for `str` and `bytes` from zero-terminated char + pointers, and from char pointers and length. Added constructors for + `str` from `bytes` and for `bytes` from `str`, which will perform + UTF-8 decoding/encoding as required. + +- Many other improvements of library internals without user-visible + changes + +## 1.8.1 (July 12, 2016) + +- Fixed a rare but potentially very severe issue when the garbage + collector ran during pybind11 type creation. + +## 1.8.0 (June 14, 2016) + +- Redesigned CMake build system which exports a convenient + `pybind11_add_module` function to parent projects. +- `std::vector<>` type bindings analogous to Boost.Python's + `indexing_suite` +- Transparent conversion of sparse and dense Eigen matrices and vectors + (`eigen.h`) +- Added an `ExtraFlags` template argument to the NumPy `array_t<>` + wrapper to disable an enforced cast that may lose precision, e.g. to + create overloads for different precisions and complex vs real-valued + matrices. +- Prevent implicit conversion of floating point values to integral types + in function arguments +- Fixed incorrect default return value policy for functions returning a + shared pointer +- Don't allow registering a type via `class_` twice +- Don't allow casting a `None` value into a C++ lvalue reference +- Fixed a crash in `enum_::operator==` that was triggered by the + `help()` command +- Improved detection of whether or not custom C++ types can be + copy/move-constructed +- Extended `str` type to also work with `bytes` instances +- Added a `"name"_a` user defined string literal that is equivalent to + `py::arg("name")`. +- When specifying function arguments via `py::arg`, the test that + verifies the number of arguments now runs at compile time. +- Added `[[noreturn]]` attribute to `pybind11_fail()` to quench some + compiler warnings +- List function arguments in exception text when the dispatch code + cannot find a matching overload +- Added `PYBIND11_OVERLOAD_NAME` and `PYBIND11_OVERLOAD_PURE_NAME` + macros which can be used to override virtual methods whose name + differs in C++ and Python (e.g. `__call__` and `operator()`) +- Various minor `iterator` and `make_iterator()` improvements +- Transparently support `__bool__` on Python 2.x and Python 3.x +- Fixed issue with destructor of unpickled object not being called +- Minor CMake build system improvements on Windows +- New `pybind11::args` and `pybind11::kwargs` types to create functions + which take an arbitrary number of arguments and keyword arguments +- New syntax to call a Python function from C++ using `*args` and + `*kwargs` +- The functions `def_property_*` now correctly process docstring + arguments (these formerly caused a segmentation fault) +- Many `mkdoc.py` improvements (enumerations, template arguments, + `DOC()` macro accepts more arguments) +- Cygwin support +- Documentation improvements (pickling support, `keep_alive`, macro + usage) + +## 1.7 (April 30, 2016) + +- Added a new `move` return value policy that triggers C++11 move + semantics. The automatic return value policy falls back to this case + whenever a rvalue reference is encountered +- Significantly more general GIL state routines that are used instead of + Python's troublesome `PyGILState_Ensure` and `PyGILState_Release` API +- Redesign of opaque types that drastically simplifies their usage +- Extended ability to pass values of type `[const] void *` +- `keep_alive` fix: don't fail when there is no patient +- `functional.h`: acquire the GIL before calling a Python function +- Added Python RAII type wrappers `none` and `iterable` +- Added `*args` and `*kwargs` pass-through parameters to + `pybind11.get_include()` function +- Iterator improvements and fixes +- Documentation on return value policies and opaque types improved + +## 1.6 (April 30, 2016) + +- Skipped due to upload to PyPI gone wrong and inability to recover + () + +## 1.5 (April 21, 2016) + +- For polymorphic types, use RTTI to try to return the closest type + registered with pybind11 +- Pickling support for serializing and unserializing C++ instances to a + byte stream in Python +- Added a convenience routine `make_iterator()` which turns a range + indicated by a pair of C++ iterators into a iterable Python object +- Added `len()` and a variadic `make_tuple()` function +- Addressed a rare issue that could confuse the current virtual function + dispatcher and another that could lead to crashes in multi-threaded + applications +- Added a `get_include()` function to the Python module that returns the + path of the directory containing the installed pybind11 header files +- Documentation improvements: import issues, symbol visibility, + pickling, limitations +- Added casting support for `std::reference_wrapper<>` + +## 1.4 (April 7, 2016) + +- Transparent type conversion for `std::wstring` and `wchar_t` +- Allow passing `nullptr`-valued strings +- Transparent passing of `void *` pointers using capsules +- Transparent support for returning values wrapped in + `std::unique_ptr<>` +- Improved docstring generation for compatibility with Sphinx +- Nicer debug error message when default parameter construction fails +- Support for "opaque" types that bypass the transparent conversion + layer for STL containers +- Redesigned type casting interface to avoid ambiguities that could + occasionally cause compiler errors +- Redesigned property implementation; fixes crashes due to an + unfortunate default return value policy +- Anaconda package generation support + +## 1.3 (March 8, 2016) + +- Added support for the Intel C++ compiler (v15+) +- Added support for the STL unordered set/map data structures +- Added support for the STL linked list data structure +- NumPy-style broadcasting support in `pybind11::vectorize` +- pybind11 now displays more verbose error messages when + `arg::operator=()` fails +- pybind11 internal data structures now live in a version-dependent + namespace to avoid ABI issues +- Many, many bugfixes involving corner cases and advanced usage + +## 1.2 (February 7, 2016) + +- Optional: efficient generation of function signatures at compile time + using C++14 +- Switched to a simpler and more general way of dealing with function + default arguments. Unused keyword arguments in function calls are now + detected and cause errors as expected +- New `keep_alive` call policy analogous to Boost.Python's + `with_custodian_and_ward` +- New `pybind11::base<>` attribute to indicate a subclass relationship +- Improved interface for RAII type wrappers in `pytypes.h` +- Use RAII type wrappers consistently within pybind11 itself. This fixes + various potential refcount leaks when exceptions occur +- Added new `bytes` RAII type wrapper (maps to `string` in Python 2.7) +- Made handle and related RAII classes const correct, using them more + consistently everywhere now +- Got rid of the ugly `__pybind11__` attributes on the Python + side---they are now stored in a C++ hash table that is not visible in + Python +- Fixed refcount leaks involving NumPy arrays and bound functions +- Vastly improved handling of shared/smart pointers +- Removed an unnecessary copy operation in `pybind11::vectorize` +- Fixed naming clashes when both pybind11 and NumPy headers are included +- Added conversions for additional exception types +- Documentation improvements (using multiple extension modules, smart + pointers, other minor clarifications) +- unified infrastructure for parsing variadic arguments in `class_` and + cpp_function +- Fixed license text (was: ZLIB, should have been: 3-clause BSD) +- Python 3.2 compatibility +- Fixed remaining issues when accessing types in another plugin module +- Added enum comparison and casting methods +- Improved SFINAE-based detection of whether types are + copy-constructible +- Eliminated many warnings about unused variables and the use of + `offsetof()` +- Support for `std::array<>` conversions + +## 1.1 (December 7, 2015) + +- Documentation improvements (GIL, wrapping functions, casting, fixed + many typos) +- Generalized conversion of integer types +- Improved support for casting function objects +- Improved support for `std::shared_ptr<>` conversions +- Initial support for `std::set<>` conversions +- Fixed type resolution issue for types defined in a separate plugin + module +- CMake build system improvements +- Factored out generic functionality to non-templated code (smaller code + size) +- Added a code size / compile time benchmark vs Boost.Python +- Added an appveyor CI script + +## 1.0 (October 15, 2015) + +- Initial release diff --git a/external_libraries/pybind11/docs/classes.rst b/external_libraries/pybind11/docs/classes.rst new file mode 100644 index 00000000..97ad72f3 --- /dev/null +++ b/external_libraries/pybind11/docs/classes.rst @@ -0,0 +1,652 @@ +.. _classes: + +Object-oriented code +#################### + +Creating bindings for a custom type +=================================== + +Let's now look at a more complex example where we'll create bindings for a +custom C++ data structure named ``Pet``. Its definition is given below: + +.. code-block:: cpp + + struct Pet { + Pet(const std::string &name) : name(name) { } + void setName(const std::string &name_) { name = name_; } + const std::string &getName() const { return name; } + + std::string name; + }; + +The binding code for ``Pet`` looks as follows: + +.. code-block:: cpp + + #include + + namespace py = pybind11; + + PYBIND11_MODULE(example, m, py::mod_gil_not_used()) { + py::class_(m, "Pet") + .def(py::init()) + .def("setName", &Pet::setName) + .def("getName", &Pet::getName); + } + +``py::class_`` creates bindings for a C++ *class* or *struct*-style data +structure. :func:`init` is a convenience function that takes the types of a +constructor's parameters as template arguments and wraps the corresponding +constructor (see the :ref:`custom_constructors` section for details). + +.. note:: + + Starting with pybind11v3, it is recommended to include `py::smart_holder` + in most situations for safety, especially if you plan to support conversions + to C++ smart pointers. See :ref:`smart_holder` for more information. + +An interactive Python session demonstrating this example is shown below: + +.. code-block:: pycon + + % python + >>> import example + >>> p = example.Pet("Molly") + >>> print(p) + + >>> p.getName() + 'Molly' + >>> p.setName("Charly") + >>> p.getName() + 'Charly' + +.. seealso:: + + Static member functions can be bound in the same way using + :func:`class_::def_static`. + +.. note:: + + Binding C++ types in unnamed namespaces (also known as anonymous namespaces) + works reliably on many platforms, but not all. The `XFAIL_CONDITION` in + tests/test_unnamed_namespace_a.py encodes the currently known conditions. + For background see `#4319 `_. + If portability is a concern, it is therefore not recommended to bind C++ + types in unnamed namespaces. It will be safest to manually pick unique + namespace names. + +Keyword and default arguments +============================= +It is possible to specify keyword and default arguments using the syntax +discussed in the previous chapter. Refer to the sections :ref:`keyword_args` +and :ref:`default_args` for details. + +Binding lambda functions +======================== + +Note how ``print(p)`` produced a rather useless summary of our data structure in the example above: + +.. code-block:: pycon + + >>> print(p) + + +To address this, we could bind a utility function that returns a human-readable +summary to the special method slot named ``__repr__``. Unfortunately, there is no +suitable functionality in the ``Pet`` data structure, and it would be nice if +we did not have to change it. This can easily be accomplished by binding a +Lambda function instead: + +.. code-block:: cpp + + py::class_(m, "Pet") + .def(py::init()) + .def("setName", &Pet::setName) + .def("getName", &Pet::getName) + .def("__repr__", + [](const Pet &a) { + return ""; + } + ); + +Both stateless [#f1]_ and stateful lambda closures are supported by pybind11. +With the above change, the same Python code now produces the following output: + +.. code-block:: pycon + + >>> print(p) + + +.. [#f1] Stateless closures are those with an empty pair of brackets ``[]`` as the capture object. + +.. _properties: + +Instance and static fields +========================== + +We can also directly expose the ``name`` field using the +:func:`class_::def_readwrite` method. A similar :func:`class_::def_readonly` +method also exists for ``const`` fields. + +.. code-block:: cpp + + py::class_(m, "Pet") + .def(py::init()) + .def_readwrite("name", &Pet::name) + // ... remainder ... + +This makes it possible to write + +.. code-block:: pycon + + >>> p = example.Pet("Molly") + >>> p.name + 'Molly' + >>> p.name = "Charly" + >>> p.name + 'Charly' + +Now suppose that ``Pet::name`` was a private internal variable +that can only be accessed via setters and getters. + +.. code-block:: cpp + + class Pet { + public: + Pet(const std::string &name) : name(name) { } + void setName(const std::string &name_) { name = name_; } + const std::string &getName() const { return name; } + private: + std::string name; + }; + +In this case, the method :func:`class_::def_property` +(:func:`class_::def_property_readonly` for read-only data) can be used to +provide a field-like interface within Python that will transparently call +the setter and getter functions: + +.. code-block:: cpp + + py::class_(m, "Pet") + .def(py::init()) + .def_property("name", &Pet::getName, &Pet::setName) + // ... remainder ... + +Write only properties can be defined by passing ``nullptr`` as the +input for the read function. + +.. seealso:: + + Similar functions :func:`class_::def_readwrite_static`, + :func:`class_::def_readonly_static` :func:`class_::def_property_static`, + and :func:`class_::def_property_readonly_static` are provided for binding + static variables and properties. Please also see the section on + :ref:`static_properties` in the advanced part of the documentation. + +Dynamic attributes +================== + +Native Python classes can pick up new attributes dynamically: + +.. code-block:: pycon + + >>> class Pet: + ... name = "Molly" + ... + >>> p = Pet() + >>> p.name = "Charly" # overwrite existing + >>> p.age = 2 # dynamically add a new attribute + +By default, classes exported from C++ do not support this and the only writable +attributes are the ones explicitly defined using :func:`class_::def_readwrite` +or :func:`class_::def_property`. + +.. code-block:: cpp + + py::class_(m, "Pet") + .def(py::init<>()) + .def_readwrite("name", &Pet::name); + +Trying to set any other attribute results in an error: + +.. code-block:: pycon + + >>> p = example.Pet() + >>> p.name = "Charly" # OK, attribute defined in C++ + >>> p.age = 2 # fail + AttributeError: 'Pet' object has no attribute 'age' + +To enable dynamic attributes for C++ classes, the :class:`py::dynamic_attr` tag +must be added to the :class:`py::class_` constructor: + +.. code-block:: cpp + + py::class_(m, "Pet", py::dynamic_attr()) + .def(py::init<>()) + .def_readwrite("name", &Pet::name); + +Now everything works as expected: + +.. code-block:: pycon + + >>> p = example.Pet() + >>> p.name = "Charly" # OK, overwrite value in C++ + >>> p.age = 2 # OK, dynamically add a new attribute + >>> p.__dict__ # just like a native Python class + {'age': 2} + +Note that there is a small runtime cost for a class with dynamic attributes. +Not only because of the addition of a ``__dict__``, but also because of more +expensive garbage collection tracking which must be activated to resolve +possible circular references. Native Python classes incur this same cost by +default, so this is not anything to worry about. By default, pybind11 classes +are more efficient than native Python classes. Enabling dynamic attributes +just brings them on par. + +.. _inheritance: + +Inheritance and automatic downcasting +===================================== + +Suppose now that the example consists of two data structures with an +inheritance relationship: + +.. code-block:: cpp + + struct Pet { + Pet(const std::string &name) : name(name) { } + std::string name; + }; + + struct Dog : Pet { + Dog(const std::string &name) : Pet(name) { } + std::string bark() const { return "woof!"; } + }; + +There are two different ways of indicating a hierarchical relationship to +pybind11: the first specifies the C++ base class as an extra template +parameter of the ``py::class_``: + +.. code-block:: cpp + + py::class_(m, "Pet") + .def(py::init()) + .def_readwrite("name", &Pet::name); + + // Method 1: template parameter: + py::class_(m, "Dog") + .def(py::init()) + .def("bark", &Dog::bark); + +Alternatively, we can also assign a name to the previously bound ``Pet`` +``py::class_`` object and reference it when binding the ``Dog`` class: + +.. code-block:: cpp + + py::class_ pet(m, "Pet"); + pet.def(py::init()) + .def_readwrite("name", &Pet::name); + + // Method 2: pass parent class_ object: + py::class_(m, "Dog", pet /* <- specify Python parent type */) + .def(py::init()) + .def("bark", &Dog::bark); + +Functionality-wise, both approaches are equivalent. Afterwards, instances will +expose fields and methods of both types: + +.. code-block:: pycon + + >>> p = example.Dog("Molly") + >>> p.name + 'Molly' + >>> p.bark() + 'woof!' + +The C++ classes defined above are regular non-polymorphic types with an +inheritance relationship. This is reflected in Python: + +.. code-block:: cpp + + // Return a base pointer to a derived instance + m.def("pet_store", []() { return std::unique_ptr(new Dog("Molly")); }); + +.. code-block:: pycon + + >>> p = example.pet_store() + >>> type(p) # `Dog` instance behind `Pet` pointer + Pet # no pointer downcasting for regular non-polymorphic types + >>> p.bark() + AttributeError: 'Pet' object has no attribute 'bark' + +The function returned a ``Dog`` instance, but because it's a non-polymorphic +type behind a base pointer, Python only sees a ``Pet``. In C++, a type is only +considered polymorphic if it has at least one virtual function and pybind11 +will automatically recognize this: + +.. code-block:: cpp + + struct PolymorphicPet { + virtual ~PolymorphicPet() = default; + }; + + struct PolymorphicDog : PolymorphicPet { + std::string bark() const { return "woof!"; } + }; + + // Same binding code + py::class_(m, "PolymorphicPet"); + py::class_(m, "PolymorphicDog") + .def(py::init<>()) + .def("bark", &PolymorphicDog::bark); + + // Again, return a base pointer to a derived instance + m.def("pet_store2", []() { return std::unique_ptr(new PolymorphicDog); }); + +.. code-block:: pycon + + >>> p = example.pet_store2() + >>> type(p) + PolymorphicDog # automatically downcast + >>> p.bark() + 'woof!' + +Given a pointer to a polymorphic base, pybind11 performs automatic downcasting +to the actual derived type. Note that this goes beyond the usual situation in +C++: we don't just get access to the virtual functions of the base, we get the +concrete derived type including functions and attributes that the base type may +not even be aware of. + +.. seealso:: + + For more information about polymorphic behavior see :ref:`overriding_virtuals`. + + +Overloaded methods +================== + +Sometimes there are several overloaded C++ methods with the same name taking +different kinds of input arguments: + +.. code-block:: cpp + + struct Pet { + Pet(const std::string &name, int age) : name(name), age(age) { } + + void set(int age_) { age = age_; } + void set(const std::string &name_) { name = name_; } + + std::string name; + int age; + }; + +Attempting to bind ``Pet::set`` will cause an error since the compiler does not +know which method the user intended to select. We can disambiguate by casting +them to function pointers. Binding multiple functions to the same Python name +automatically creates a chain of function overloads that will be tried in +sequence. + +.. code-block:: cpp + + py::class_(m, "Pet") + .def(py::init()) + .def("set", static_cast(&Pet::set), "Set the pet's age") + .def("set", static_cast(&Pet::set), "Set the pet's name"); + +The overload signatures are also visible in the method's docstring: + +.. code-block:: pycon + + >>> help(example.Pet) + + class Pet(__builtin__.object) + | Methods defined here: + | + | __init__(...) + | Signature : (Pet, str, int) -> NoneType + | + | set(...) + | 1. Signature : (Pet, int) -> NoneType + | + | Set the pet's age + | + | 2. Signature : (Pet, str) -> NoneType + | + | Set the pet's name + +If you have a C++14 compatible compiler [#cpp14]_, you can use an alternative +syntax to cast the overloaded function: + +.. code-block:: cpp + + py::class_(m, "Pet") + .def("set", py::overload_cast(&Pet::set), "Set the pet's age") + .def("set", py::overload_cast(&Pet::set), "Set the pet's name"); + +Here, ``py::overload_cast`` only requires the parameter types to be specified. +The return type and class are deduced. This avoids the additional noise of +``void (Pet::*)()`` as seen in the raw cast. If a function is overloaded based +on constness, the ``py::const_`` tag should be used: + +.. code-block:: cpp + + struct Widget { + int foo(int x, float y); + int foo(int x, float y) const; + }; + + py::class_(m, "Widget") + .def("foo_mutable", py::overload_cast(&Widget::foo)) + .def("foo_const", py::overload_cast(&Widget::foo, py::const_)); + +If you prefer the ``py::overload_cast`` syntax but have a C++11 compatible compiler only, +you can use ``py::detail::overload_cast_impl`` with an additional set of parentheses: + +.. code-block:: cpp + + template + using overload_cast_ = pybind11::detail::overload_cast_impl; + + py::class_(m, "Pet") + .def("set", overload_cast_()(&Pet::set), "Set the pet's age") + .def("set", overload_cast_()(&Pet::set), "Set the pet's name"); + +.. [#cpp14] A compiler which supports the ``-std=c++14`` flag. + +.. note:: + + To define multiple overloaded constructors, simply declare one after the + other using the ``.def(py::init<...>())`` syntax. The existing machinery + for specifying keyword and default arguments also works. + +☝️ Pitfalls with raw pointers and shared ownership +================================================== + +``py::class_``-wrapped objects automatically manage the lifetime of the +wrapped C++ object, in collaboration with the chosen holder type (see +:ref:`py_class_holder`). When wrapping C++ functions involving raw pointers, +care needs to be taken to not accidentally undermine automatic lifetime +management. For example, ownership is inadvertently transferred here: + +.. code-block:: cpp + + class Child { }; + + class Parent { + public: + Parent() : child(std::make_shared()) { } + Child *get_child() { return child.get(); } /* DANGER */ + private: + std::shared_ptr child; + }; + + PYBIND11_MODULE(example, m, py::mod_gil_not_used()) { + py::class_>(m, "Child"); + + py::class_>(m, "Parent") + .def(py::init<>()) + .def("get_child", &Parent::get_child); /* PROBLEM */ + } + +The following Python code leads to undefined behavior, likely resulting in +a segmentation fault. + +.. code-block:: python + + from example import Parent + + print(Parent().get_child()) + +Part of the ``/* PROBLEM */`` here is that pybind11 falls back to using +``return_value_policy::take_ownership`` as the default (see +:ref:`return_value_policies`). The fact that the ``Child`` instance is +already managed by ``std::shared_ptr`` is lost. Therefore pybind11 +will create a second independent ``std::shared_ptr`` that also +claims ownership of the pointer, eventually leading to heap-use-after-free +or double-free errors. + +There are various ways to resolve this issue, either by changing +the ``Child`` or ``Parent`` C++ implementations (e.g. using +``std::enable_shared_from_this`` as a base class for +``Child``, or adding a member function to ``Parent`` that returns +``std::shared_ptr``), or if that is not feasible, by using +``return_value_policy::reference_internal``. What is the best approach +depends on the exact situation. + +A highly effective way to stay in the clear — even in pure C++, but +especially when binding C++ code to Python — is to consistently prefer +``std::shared_ptr`` or ``std::unique_ptr`` over passing raw pointers. + +.. _native_enum: + +Enumerations and internal types +=============================== + +Let's now suppose that the example class contains internal types like enumerations, e.g.: + +.. code-block:: cpp + + struct Pet { + enum Kind { + Dog = 0, + Cat + }; + + struct Attributes { + float age = 0; + }; + + Pet(const std::string &name, Kind type) : name(name), type(type) { } + + std::string name; + Kind type; + Attributes attr; + }; + +The binding code for this example looks as follows: + +.. code-block:: cpp + + #include // Not already included with pybind11.h + + py::class_ pet(m, "Pet"); + + pet.def(py::init()) + .def_readwrite("name", &Pet::name) + .def_readwrite("type", &Pet::type) + .def_readwrite("attr", &Pet::attr); + + py::native_enum(pet, "Kind", "enum.Enum") + .value("Dog", Pet::Kind::Dog) + .value("Cat", Pet::Kind::Cat) + .export_values() + .finalize(); + + py::class_(pet, "Attributes") + .def(py::init<>()) + .def_readwrite("age", &Pet::Attributes::age); + + +To ensure that the nested types ``Kind`` and ``Attributes`` are created +within the scope of ``Pet``, the ``pet`` ``py::class_`` instance must be +supplied to the ``py::native_enum`` and ``py::class_`` constructors. The +``.export_values()`` function is available for exporting the enum entries +into the parent scope, if desired. + +``py::native_enum`` was introduced with pybind11v3. It binds C++ enum types +to native Python enum types, typically types in Python's +`stdlib enum `_ module, +which are `PEP 435 compatible `_. +This is the recommended way to bind C++ enums. +The older ``py::enum_`` is not PEP 435 compatible +(see `issue #2332 `_) +but remains supported indefinitely for backward compatibility. +New bindings should prefer ``py::native_enum``. + +.. note:: + + The deprecated ``py::enum_`` is :ref:`documented here `. + +The ``.finalize()`` call above is needed because Python's native enums +cannot be built incrementally — all name/value pairs need to be passed at +once. To achieve this, ``py::native_enum`` acts as a buffer to collect the +name/value pairs. The ``.finalize()`` call uses the accumulated name/value +pairs to build the arguments for constructing a native Python enum type. + +The ``py::native_enum`` constructor takes a third argument, +``native_type_name``, which specifies the fully qualified name of the Python +base class to use — e.g., ``"enum.Enum"`` or ``"enum.IntEnum"``. A fourth +optional argument, ``class_doc``, provides the docstring for the generated +class. + +For example: + +.. code-block:: cpp + + py::native_enum(pet, "Kind", "enum.IntEnum", "Constant specifying the kind of pet") + +You may use any fully qualified Python name for ``native_type_name``. +The only requirement is that the named type is similar to +`enum.Enum `_ +in these ways: + +* Has a `constructor similar to that of enum.Enum + `_:: + + Colors = enum.Enum("Colors", (("Red", 0), ("Green", 1))) + +* A `C++ underlying `_ + enum value can be passed to the constructor for the Python enum value:: + + red = Colors(0) + +* The enum values have a ``.value`` property yielding a value that + can be cast to the C++ underlying type:: + + underlying = red.value + +As of Python 3.13, the compatible `types in the stdlib enum module +`_ are: +``Enum``, ``IntEnum``, ``Flag``, ``IntFlag``. + +.. note:: + + In rare cases, a C++ enum may be bound to Python via a + :ref:`custom type caster `. In such cases, a + template specialization like this may be required: + + .. code-block:: cpp + + #if defined(PYBIND11_HAS_NATIVE_ENUM) + namespace pybind11::detail { + template + struct type_caster_enum_type_enabled< + FancyEnum, + enable_if_t::value>> : std::false_type {}; + } + #endif + + This specialization is needed only if the custom type caster is templated. + + The ``PYBIND11_HAS_NATIVE_ENUM`` guard is needed only if backward + compatibility with pybind11v2 is required. diff --git a/external_libraries/pybind11/docs/cmake/index.rst b/external_libraries/pybind11/docs/cmake/index.rst new file mode 100644 index 00000000..eaf66d70 --- /dev/null +++ b/external_libraries/pybind11/docs/cmake/index.rst @@ -0,0 +1,8 @@ +CMake helpers +------------- + +Pybind11 can be used with ``add_subdirectory(extern/pybind11)``, or from an +install with ``find_package(pybind11 CONFIG)``. The interface provided in +either case is functionally identical. + +.. cmake-module:: ../../tools/pybind11Config.cmake.in diff --git a/external_libraries/pybind11/docs/compiling.rst b/external_libraries/pybind11/docs/compiling.rst new file mode 100644 index 00000000..b693bd58 --- /dev/null +++ b/external_libraries/pybind11/docs/compiling.rst @@ -0,0 +1,731 @@ +.. _compiling: + +Build systems +############# + +For an overview of Python packaging including compiled packaging with a pybind11 +example, along with a cookiecutter that includes several pybind11 options, see +the `Scientific Python Development Guide`_. + +.. _Scientific Python Development Guide: https://learn.scientific-python.org/development/guides/packaging-compiled/ + +.. scikit-build-core: + +Modules with CMake +================== + +A Python extension module can be created with just a few lines of code: + +.. code-block:: cmake + + cmake_minimum_required(VERSION 3.15...4.2) + project(example LANGUAGES CXX) + + set(PYBIND11_FINDPYTHON ON) + find_package(pybind11 CONFIG REQUIRED) + + pybind11_add_module(example example.cpp) + install(TARGETS example DESTINATION .) + +(You use the ``add_subdirectory`` instead, see the example in :ref:`cmake`.) In +this example, the code is located in a file named :file:`example.cpp`. Either +method will import the pybind11 project which provides the +``pybind11_add_module`` function. It will take care of all the details needed +to build a Python extension module on any platform. + +To build with pip, build, cibuildwheel, uv, or other Python tools, you can +add a ``pyproject.toml`` file like this: + +.. code-block:: toml + + [build-system] + requires = ["scikit-build-core", "pybind11"] + build-backend = "scikit_build_core.build" + + [project] + name = "example" + version = "0.1.0" + +You don't need setuptools files like ``MANIFEST.in``, ``setup.py``, or +``setup.cfg``, as this is not setuptools. See `scikit-build-core`_ for details. +For projects you plan to upload to PyPI, be sure to fill out the ``[project]`` +table with other important metadata as well (see `Writing pyproject.toml`_). + +A working sample project can be found in the [scikit_build_example]_ +repository. An older and harder-to-maintain method is in [cmake_example]_. More +details about our cmake support can be found below in :ref:`cmake`. + +.. _scikit-build-core: https://scikit-build-core.readthedocs.io + +.. [scikit_build_example] https://github.com/pybind/scikit_build_example + +.. [cmake_example] https://github.com/pybind/cmake_example + +.. _modules-meson-python: + +Modules with meson-python +========================= + +You can also build a package with `Meson`_ using `meson-python`_, if you prefer +that. Your ``meson.build`` file would look something like this: + +.. _meson-example: + +.. code-block:: meson + + project( + 'example', + 'cpp', + version: '0.1.0', + default_options: [ + 'cpp_std=c++11', + ], + ) + + py = import('python').find_installation(pure: false) + pybind11_dep = dependency('pybind11') + + py.extension_module('example', + 'example.cpp', + install: true, + dependencies : [pybind11_dep], + ) + + +And you would need a ``pyproject.toml`` file like this: + +.. code-block:: toml + + [build-system] + requires = ["meson-python", "pybind11"] + build-backend = "mesonpy" + +Meson-python *requires* your project to be in git (or mercurial) as it uses it +for the SDist creation. For projects you plan to upload to PyPI, be sure to fill out the +``[project]`` table as well (see `Writing pyproject.toml`_). + + +.. _Writing pyproject.toml: https://packaging.python.org/en/latest/guides/writing-pyproject-toml + +.. _meson: https://mesonbuild.com + +.. _meson-python: https://meson-python.readthedocs.io/en/latest + +.. _build-setuptools: + +Modules with setuptools +======================= + +For projects on PyPI, a historically popular option is setuptools. Sylvain +Corlay has kindly provided an example project which shows how to set up +everything, including automatic generation of documentation using Sphinx. +Please refer to the [python_example]_ repository. + +.. [python_example] https://github.com/pybind/python_example + +A helper file is provided with pybind11 that can simplify usage with setuptools. + +To use pybind11 inside your ``setup.py``, you have to have some system to +ensure that ``pybind11`` is installed when you build your package. There are +four possible ways to do this, and pybind11 supports all four: You can ask all +users to install pybind11 beforehand (bad), you can use +:ref:`setup_helpers-pep518` (good), ``setup_requires=`` (discouraged), or you +can :ref:`setup_helpers-copy-manually` (works but you have to manually sync +your copy to get updates). Third party packagers like conda-forge generally +strongly prefer the ``pyproject.toml`` method, as it gives them control over +the ``pybind11`` version, and they may apply patches, etc. + +An example of a ``setup.py`` using pybind11's helpers: + +.. code-block:: python + + from glob import glob + from setuptools import setup + from pybind11.setup_helpers import Pybind11Extension + + ext_modules = [ + Pybind11Extension( + "python_example", + sorted(glob("src/*.cpp")), # Sort source files for reproducibility + ), + ] + + setup(..., ext_modules=ext_modules) + +If you want to do an automatic search for the highest supported C++ standard, +that is supported via a ``build_ext`` command override; it will only affect +``Pybind11Extensions``: + +.. code-block:: python + + from glob import glob + from setuptools import setup + from pybind11.setup_helpers import Pybind11Extension, build_ext + + ext_modules = [ + Pybind11Extension( + "python_example", + sorted(glob("src/*.cpp")), + ), + ] + + setup(..., cmdclass={"build_ext": build_ext}, ext_modules=ext_modules) + +If you have single-file extension modules that are directly stored in the +Python source tree (``foo.cpp`` in the same directory as where a ``foo.py`` +would be located), you can also generate ``Pybind11Extensions`` using +``setup_helpers.intree_extensions``: ``intree_extensions(["path/to/foo.cpp", +...])`` returns a list of ``Pybind11Extensions`` which can be passed to +``ext_modules``, possibly after further customizing their attributes +(``libraries``, ``include_dirs``, etc.). By doing so, a ``foo.*.so`` extension +module will be generated and made available upon installation. + +``intree_extension`` will automatically detect if you are using a ``src``-style +layout (as long as no namespace packages are involved), but you can also +explicitly pass ``package_dir`` to it (as in ``setuptools.setup``). + +Since pybind11 does not require NumPy when building, a light-weight replacement +for NumPy's parallel compilation distutils tool is included. Use it like this: + +.. code-block:: python + + from pybind11.setup_helpers import ParallelCompile + + # Optional multithreaded build + ParallelCompile("NPY_NUM_BUILD_JOBS").install() + + setup(...) + +The argument is the name of an environment variable to control the number of +threads, such as ``NPY_NUM_BUILD_JOBS`` (as used by NumPy), though you can set +something different if you want; ``CMAKE_BUILD_PARALLEL_LEVEL`` is another choice +a user might expect. You can also pass ``default=N`` to set the default number +of threads (0 will take the number of threads available) and ``max=N``, the +maximum number of threads; if you have a large extension you may want set this +to a memory dependent number. + +If you are developing rapidly and have a lot of C++ files, you may want to +avoid rebuilding files that have not changed. For simple cases were you are +using ``pip install -e .`` and do not have local headers, you can skip the +rebuild if an object file is newer than its source (headers are not checked!) +with the following: + +.. code-block:: python + + from pybind11.setup_helpers import ParallelCompile, naive_recompile + + ParallelCompile("NPY_NUM_BUILD_JOBS", needs_recompile=naive_recompile).install() + + +If you have a more complex build, you can implement a smarter function and pass +it to ``needs_recompile``, or you can use [Ccache]_ instead. ``CXX="cache g++" +pip install -e .`` would be the way to use it with GCC, for example. Unlike the +simple solution, this even works even when not compiling in editable mode, but +it does require Ccache to be installed. + +Keep in mind that Pip will not even attempt to rebuild if it thinks it has +already built a copy of your code, which it deduces from the version number. +One way to avoid this is to use [setuptools_scm]_, which will generate a +version number that includes the number of commits since your last tag and a +hash for a dirty directory. Another way to force a rebuild is purge your cache +or use Pip's ``--no-cache-dir`` option. + +You also need a ``MANIFEST.in`` file to include all relevant files so that you +can make an SDist. If you use `pypa-build`_, that will build an SDist then a +wheel from that SDist by default, so you can look inside those files (wheels +are just zip files with a ``.whl`` extension) to make sure you aren't missing +files. `check-manifest`_ (setuptools specific) or `check-sdist`_ (general) are +CLI tools that can compare the SDist contents with your source control. + +.. [Ccache] https://ccache.dev + +.. [setuptools_scm] https://github.com/pypa/setuptools_scm + +.. _setup_helpers-pep518: + +Build requirements +------------------ + +With a ``pyproject.toml`` file, you can ensure that ``pybind11`` is available +during the compilation of your project. When this file exists, Pip will make a +new virtual environment, download just the packages listed here in +``requires=``, and build a wheel (binary Python package). It will then throw +away the environment, and install your wheel. + +Your ``pyproject.toml`` file will likely look something like this: + +.. code-block:: toml + + [build-system] + requires = ["setuptools", "pybind11"] + build-backend = "setuptools.build_meta" + +.. _PEP 517: https://www.python.org/dev/peps/pep-0517/ +.. _cibuildwheel: https://cibuildwheel.pypa.io +.. _pypa-build: https://build.pypa.io/en/latest/ +.. _check-manifest: https://pypi.io/project/check-manifest +.. _check-sdist: https://pypi.io/project/check-sdist + +.. _setup_helpers-copy-manually: + +Copy manually +------------- + +You can also copy ``setup_helpers.py`` directly to your project; it was +designed to be usable standalone, like the old example ``setup.py``. You can +set ``include_pybind11=False`` to skip including the pybind11 package headers, +so you can use it with git submodules and a specific git version. If you use +this, you will need to import from a local file in ``setup.py`` and ensure the +helper file is part of your MANIFEST. + + +Closely related, if you include pybind11 as a subproject, you can run the +``setup_helpers.py`` inplace. If loaded correctly, this should even pick up +the correct include for pybind11, though you can turn it off as shown above if +you want to input it manually. + +Suggested usage if you have pybind11 as a submodule in ``extern/pybind11``: + +.. code-block:: python + + DIR = os.path.abspath(os.path.dirname(__file__)) + + sys.path.append(os.path.join(DIR, "extern", "pybind11")) + from pybind11.setup_helpers import Pybind11Extension # noqa: E402 + + del sys.path[-1] + + +.. versionchanged:: 2.6 + + Added ``setup_helpers`` file. + +Building with cppimport +======================== + +[cppimport]_ is a small Python import hook that determines whether there is a C++ +source file whose name matches the requested module. If there is, the file is +compiled as a Python extension using pybind11 and placed in the same folder as +the C++ source file. Python is then able to find the module and load it. + +.. [cppimport] https://github.com/tbenthompson/cppimport + + + +.. _cmake: + +Building with CMake +=================== + +For C++ codebases that have an existing CMake-based build system, a Python +extension module can be created with just a few lines of code, as seen above in +the module section. Pybind11 currently defaults to the old mechanism, though be +aware that CMake 3.27 removed the old mechanism, so pybind11 will automatically +switch if the old mechanism is not available. Please opt into the new mechanism +if at all possible. Our default may change in future versions. This is the +minimum required: + + + +.. versionchanged:: 2.6 + CMake 3.4+ is required. + +.. versionchanged:: 2.11 + CMake 3.5+ is required. + +.. versionchanged:: 2.14 + CMake 3.15+ is required. + + +Further information can be found at :doc:`cmake/index`. + +pybind11_add_module +------------------- + +To ease the creation of Python extension modules, pybind11 provides a CMake +function with the following signature: + +.. code-block:: cmake + + pybind11_add_module( [MODULE | SHARED] [EXCLUDE_FROM_ALL] + [NO_EXTRAS] [THIN_LTO] [OPT_SIZE] source1 [source2 ...]) + +This function behaves very much like CMake's builtin ``add_library`` (in fact, +it's a wrapper function around that command). It will add a library target +called ```` to be built from the listed source files. In addition, it +will take care of all the Python-specific compiler and linker flags as well +as the OS- and Python-version-specific file extension. The produced target +```` can be further manipulated with regular CMake commands. + +``MODULE`` or ``SHARED`` may be given to specify the type of library. If no +type is given, ``MODULE`` is used by default which ensures the creation of a +Python-exclusive module. Specifying ``SHARED`` will create a more traditional +dynamic library which can also be linked from elsewhere. ``EXCLUDE_FROM_ALL`` +removes this target from the default build (see CMake docs for details). + +Since pybind11 is a template library, ``pybind11_add_module`` adds compiler +flags to ensure high quality code generation without bloat arising from long +symbol names and duplication of code in different translation units. It +sets default visibility to *hidden*, which is required for some pybind11 +features and functionality when attempting to load multiple pybind11 modules +compiled under different pybind11 versions. It also adds additional flags +enabling LTO (Link Time Optimization) and strip unneeded symbols. See the +:ref:`FAQ entry ` for a more detailed explanation. These +latter optimizations are never applied in ``Debug`` mode. If ``NO_EXTRAS`` is +given, they will always be disabled, even in ``Release`` mode. However, this +will result in code bloat and is generally not recommended. + +As stated above, LTO is enabled by default. Some newer compilers also support +different flavors of LTO such as `ThinLTO`_. Setting ``THIN_LTO`` will cause +the function to prefer this flavor if available. The function falls back to +regular LTO if ``-flto=thin`` is not available. If +``CMAKE_INTERPROCEDURAL_OPTIMIZATION`` is set (either ``ON`` or ``OFF``), then +that will be respected instead of the built-in flag search. + +.. note:: + + If you want to set the property form on targets or the + ``CMAKE_INTERPROCEDURAL_OPTIMIZATION_`` versions of this, you should + still use ``set(CMAKE_INTERPROCEDURAL_OPTIMIZATION OFF)`` (otherwise a + no-op) to disable pybind11's ipo flags. + +The ``OPT_SIZE`` flag enables size-based optimization equivalent to the +standard ``/Os`` or ``-Os`` compiler flags and the ``MinSizeRel`` build type, +which avoid optimizations that can substantially increase the size of the +resulting binary. This flag is particularly useful in projects that are split +into performance-critical parts and associated bindings. In this case, we can +compile the project in release mode (and hence, optimize performance globally), +and specify ``OPT_SIZE`` for the binding target, where size might be the main +concern as performance is often less critical here. A ~25% size reduction has +been observed in practice. This flag only changes the optimization behavior at +a per-target level and takes precedence over the global CMake build type +(``Release``, ``RelWithDebInfo``) except for ``Debug`` builds, where +optimizations remain disabled. + +.. _ThinLTO: http://clang.llvm.org/docs/ThinLTO.html + +Configuration variables +----------------------- + +By default, pybind11 will compile modules with the compiler default or the +minimum standard required by pybind11, whichever is higher. You can set the +standard explicitly with +`CMAKE_CXX_STANDARD `_: + +.. code-block:: cmake + + set(CMAKE_CXX_STANDARD 14 CACHE STRING "C++ version selection") # or 11, 14, 17, 20 + set(CMAKE_CXX_STANDARD_REQUIRED ON) # optional, ensure standard is supported + set(CMAKE_CXX_EXTENSIONS OFF) # optional, keep compiler extensions off + +The variables can also be set when calling CMake from the command line using +the ``-D=`` flag. You can also manually set ``CXX_STANDARD`` +on a target or use ``target_compile_features`` on your targets - anything that +CMake supports. + +Classic Python support: The target Python version can be selected by setting +``PYBIND11_PYTHON_VERSION`` or an exact Python installation can be specified +with ``PYTHON_EXECUTABLE``. For example: + +.. code-block:: bash + + cmake -DPYBIND11_PYTHON_VERSION=3.8 .. + + # Another method: + cmake -DPYTHON_EXECUTABLE=/path/to/python .. + + # This often is a good way to get the current Python, works in environments: + cmake -DPYTHON_EXECUTABLE=$(python3 -c "import sys; print(sys.executable)") .. + + +find_package vs. add_subdirectory +--------------------------------- + +For CMake-based projects that don't include the pybind11 repository internally, +an external installation can be detected through ``find_package(pybind11)``. +See the `Config file`_ docstring for details of relevant CMake variables. + +.. code-block:: cmake + + cmake_minimum_required(VERSION 3.15...4.2) + project(example LANGUAGES CXX) + + find_package(pybind11 REQUIRED) + pybind11_add_module(example example.cpp) + +Note that ``find_package(pybind11)`` will only work correctly if pybind11 +has been correctly installed on the system, e. g. after downloading or cloning +the pybind11 repository : + +.. code-block:: bash + + # Classic CMake + cd pybind11 + mkdir build + cd build + cmake .. + make install + + # CMake 3.15+ + cd pybind11 + cmake -S . -B build + cmake --build build -j 2 # Build on 2 cores + cmake --install build + +Once detected, the aforementioned ``pybind11_add_module`` can be employed as +before. The function usage and configuration variables are identical no matter +if pybind11 is added as a subdirectory or found as an installed package. You +can refer to the same [cmake_example]_ repository for a full sample project +-- just swap out ``add_subdirectory`` for ``find_package``. + +.. _Config file: https://github.com/pybind/pybind11/blob/master/tools/pybind11Config.cmake.in + + +.. _find-python-mode: + +FindPython mode +--------------- + +Modern CMake (3.18.2+ ideal) added a new module called FindPython that had a +highly improved search algorithm and modern targets and tools. If you use +FindPython, pybind11 will detect this and use the existing targets instead: + +.. code-block:: cmake + + cmake_minimum_required(VERSION 3.15...4.2) + project(example LANGUAGES CXX) + + find_package(Python 3.8 COMPONENTS Interpreter Development REQUIRED) + find_package(pybind11 CONFIG REQUIRED) + # or add_subdirectory(pybind11) + + pybind11_add_module(example example.cpp) + +You can also use the targets (as listed below) with FindPython. If you define +``PYBIND11_FINDPYTHON``, pybind11 will perform the FindPython step for you +(mostly useful when building pybind11's own tests, or as a way to change search +algorithms from the CMake invocation, with ``-DPYBIND11_FINDPYTHON=ON``. + +.. warning:: + + If you use FindPython to multi-target Python versions, use the individual + targets listed below, and avoid targets that directly include Python parts. + +There are `many ways to hint or force a discovery of a specific Python +installation `_), +setting ``Python_ROOT_DIR`` may be the most common one (though with +virtualenv/venv support, and Conda support, this tends to find the correct +Python version more often than the old system did). + +.. warning:: + + When the Python libraries (i.e. ``libpythonXX.a`` and ``libpythonXX.so`` + on Unix) are not available, as is the case on a manylinux image, the + ``Development`` component will not be resolved by ``FindPython``. When not + using the embedding functionality, CMake 3.18+ allows you to specify + ``Development.Module`` instead of ``Development`` to resolve this issue. + +.. versionadded:: 2.6 + +Advanced: interface library targets +----------------------------------- + +Pybind11 supports modern CMake usage patterns with a set of interface targets, +available in all modes. The targets provided are: + + ``pybind11::headers`` + Just the pybind11 headers and minimum compile requirements + + ``pybind11::pybind11`` + Python headers + ``pybind11::headers`` + + ``pybind11::python_link_helper`` + Just the "linking" part of pybind11:module + + ``pybind11::module`` + Everything for extension modules - ``pybind11::pybind11`` + ``Python::Module`` (FindPython) or ``pybind11::python_link_helper`` + + ``pybind11::embed`` + Everything for embedding the Python interpreter - ``pybind11::pybind11`` + ``Python::Python`` (FindPython) or Python libs + + ``pybind11::lto`` / ``pybind11::thin_lto`` + An alternative to `INTERPROCEDURAL_OPTIMIZATION` for adding link-time optimization. + + ``pybind11::windows_extras`` + ``/bigobj`` and ``/mp`` for MSVC. + + ``pybind11::opt_size`` + ``/Os`` for MSVC, ``-Os`` for other compilers. Does nothing for debug builds. + +Two helper functions are also provided: + + ``pybind11_strip(target)`` + Strips a target (uses ``CMAKE_STRIP`` after the target is built) + + ``pybind11_extension(target)`` + Sets the correct extension (with SOABI) for a target. + +You can use these targets to build complex applications. For example, the +``add_python_module`` function is identical to: + +.. code-block:: cmake + + cmake_minimum_required(VERSION 3.15...4.2) + project(example LANGUAGES CXX) + + find_package(pybind11 REQUIRED) # or add_subdirectory(pybind11) + + add_library(example MODULE main.cpp) + + target_link_libraries(example PRIVATE pybind11::module pybind11::lto pybind11::windows_extras) + + pybind11_extension(example) + if(NOT MSVC AND NOT ${CMAKE_BUILD_TYPE} MATCHES Debug|RelWithDebInfo) + # Strip unnecessary sections of the binary on Linux/macOS + pybind11_strip(example) + endif() + + set_target_properties(example PROPERTIES CXX_VISIBILITY_PRESET "hidden" + CUDA_VISIBILITY_PRESET "hidden") + +Instead of setting properties, you can set ``CMAKE_*`` variables to initialize these correctly. + +.. warning:: + + Since pybind11 is a metatemplate library, it is crucial that certain + compiler flags are provided to ensure high quality code generation. In + contrast to the ``pybind11_add_module()`` command, the CMake interface + provides a *composable* set of targets to ensure that you retain flexibility. + It can be especially important to provide or set these properties; the + :ref:`FAQ ` contains an explanation on why these are needed. + +.. versionadded:: 2.6 + +.. _nopython-mode: + +Advanced: NOPYTHON mode +----------------------- + +If you want complete control, you can set ``PYBIND11_NOPYTHON`` to completely +disable Python integration (this also happens if you run ``FindPython2`` and +``FindPython3`` without running ``FindPython``). This gives you complete +freedom to integrate into an existing system (like `Scikit-Build's +`_ ``PythonExtensions``). +``pybind11_add_module`` and ``pybind11_extension`` will be unavailable, and the +targets will be missing any Python specific behavior. + +.. versionadded:: 2.6 + +Embedding the Python interpreter +-------------------------------- + +In addition to extension modules, pybind11 also supports embedding Python into +a C++ executable or library. In CMake, simply link with the ``pybind11::embed`` +target. It provides everything needed to get the interpreter running. The Python +headers and libraries are attached to the target. Unlike ``pybind11::module``, +there is no need to manually set any additional properties here. For more +information about usage in C++, see :doc:`/advanced/embedding`. + +.. code-block:: cmake + + cmake_minimum_required(VERSION 3.15...4.2) + project(example LANGUAGES CXX) + + find_package(pybind11 REQUIRED) # or add_subdirectory(pybind11) + + add_executable(example main.cpp) + target_link_libraries(example PRIVATE pybind11::embed) + +.. _building_manually: + +Building manually +================= + +pybind11 is a header-only library, hence it is not necessary to link against +any special libraries and there are no intermediate (magic) translation steps. + +On Linux, you can compile an example such as the one given in +:ref:`simple_example` using the following command: + +.. code-block:: bash + + $ c++ -O3 -Wall -shared -std=c++11 -fPIC $(python3 -m pybind11 --includes) example.cpp -o example$(python3-config --extension-suffix) + +The ``python3 -m pybind11 --includes`` command fetches the include paths for +both pybind11 and Python headers. This assumes that pybind11 has been installed +using ``pip`` or ``conda``. If it hasn't, you can also manually specify +``-I /include`` together with the Python includes path +``python3-config --includes``. + +On macOS: the build command is almost the same but it also requires passing +the ``-undefined dynamic_lookup`` flag so as to ignore missing symbols when +building the module: + +.. code-block:: bash + + $ c++ -O3 -Wall -shared -std=c++11 -undefined dynamic_lookup $(python3 -m pybind11 --includes) example.cpp -o example$(python3-config --extension-suffix) + +In general, it is advisable to include several additional build parameters +that can considerably reduce the size of the created binary. Refer to section +:ref:`cmake` for a detailed example of a suitable cross-platform CMake-based +build system that works on all platforms including Windows. + +.. note:: + + On Linux and macOS, it's better to (intentionally) not link against + ``libpython``. The symbols will be resolved when the extension library + is loaded into a Python binary. This is preferable because you might + have several different installations of a given Python version (e.g. the + system-provided Python, and one that ships with a piece of commercial + software). In this way, the plugin will work with both versions, instead + of possibly importing a second Python library into a process that already + contains one (which will lead to a segfault). + + +Building with Bazel +=================== + +You can build with the Bazel build system using the `pybind11_bazel +`_ repository. + +Building with Meson +=================== + +You can use Meson, which has support for ``pybind11`` as a dependency (internally +relying on our ``pkg-config`` support). See the :ref:`module example above `. + + +Generating binding code automatically +===================================== + +The ``Binder`` project is a tool for automatic generation of pybind11 binding +code by introspecting existing C++ codebases using LLVM/Clang. See the +[binder]_ documentation for details. + +.. [binder] http://cppbinder.readthedocs.io/en/latest/about.html + +[AutoWIG]_ is a Python library that wraps automatically compiled libraries into +high-level languages. It parses C++ code using LLVM/Clang technologies and +generates the wrappers using the Mako templating engine. The approach is automatic, +extensible, and applies to very complex C++ libraries, composed of thousands of +classes or incorporating modern meta-programming constructs. + +.. [AutoWIG] https://github.com/StatisKit/AutoWIG + +[semiwrap]_ is a build tool that makes it simpler to wrap C/C++ libraries with +pybind11 by automating large portions of the wrapping process and handling some +of the more complex aspects of creating pybind11 based wrappers (especially with +trampolines to allow inheriting from C++ classes from Python). It includes a +hatchling plugin that autogenerates meson.build files that can be built using +meson, and those build files parse your wrapped headers and generate/compile +pybind11 based wrappers into python extension modules. + +.. [semiwrap] https://semiwrap.readthedocs.io + +[litgen]_ is an automatic python bindings generator with a focus on generating +documented and discoverable bindings: bindings will nicely reproduce the documentation +found in headers. It is based on srcML (srcml.org), a highly scalable, multi-language +parsing tool with a developer centric approach. The API that you want to expose to python +must be C++14 compatible (but your implementation can use more modern constructs). + +.. [litgen] https://pthom.github.io/litgen diff --git a/external_libraries/pybind11/docs/conf.py b/external_libraries/pybind11/docs/conf.py new file mode 100644 index 00000000..5f216bff --- /dev/null +++ b/external_libraries/pybind11/docs/conf.py @@ -0,0 +1,369 @@ +#!/usr/bin/env python3 +# +# pybind11 documentation build configuration file, created by +# sphinx-quickstart on Sun Oct 11 19:23:48 2015. +# +# This file is execfile()d with the current directory set to its +# containing dir. +# +# Note that not all possible configuration values are present in this +# autogenerated file. +# +# All configuration values have a default; values that are commented out +# serve to show the default. +from __future__ import annotations + +import os +import re +import subprocess +import sys +from pathlib import Path + +DIR = Path(__file__).parent.resolve() + +# If extensions (or modules to document with autodoc) are in another directory, +# add these directories to sys.path here. If the directory is relative to the +# documentation root, use os.path.abspath to make it absolute, like shown here. +# sys.path.insert(0, os.path.abspath('.')) + +# -- General configuration ------------------------------------------------ + +# If your documentation needs a minimal Sphinx version, state it here. +# needs_sphinx = '1.0' + +# Add any Sphinx extension module names here, as strings. They can be +# extensions coming with Sphinx (named 'sphinx.ext.*') or your custom +# ones. +extensions = [ + "breathe", + "myst_parser", + "sphinx_copybutton", + "sphinxcontrib.rsvgconverter", + "sphinxcontrib.moderncmakedomain", +] + +breathe_projects = {"pybind11": ".build/doxygenxml/"} +breathe_default_project = "pybind11" +breathe_domain_by_extension = {"h": "cpp"} + +# Add any paths that contain templates here, relative to this directory. +templates_path = [".templates"] + +# The suffix(es) of source filenames. +source_suffix = [".rst", ".md"] + +# The encoding of source files. +# source_encoding = 'utf-8-sig' + +# The master toctree document. +master_doc = "index" + +# General information about the project. +project = "pybind11" +copyright = "2017, Wenzel Jakob" +author = "Wenzel Jakob" + +# The version info for the project you're documenting, acts as replacement for +# |version| and |release|, also used in various other places throughout the +# built documents. + +# Read the listed version +version_file = DIR.parent / "pybind11/_version.py" +with version_file.open(encoding="utf-8") as f: + code = compile(f.read(), version_file, "exec") +loc = {"__file__": str(version_file)} +exec(code, loc) + +# The full version, including alpha/beta/rc tags. +version = loc["__version__"] + +# The language for content autogenerated by Sphinx. Refer to documentation +# for a list of supported languages. +# +# This is also used if you do content translation via gettext catalogs. +# Usually you set "language" from the command line for these cases. +language = "en" + +# There are two options for replacing |today|: either, you set today to some +# non-false value, then it is used: +# today = '' +# Else, today_fmt is used as the format for a strftime call. +# today_fmt = '%B %d, %Y' + +# List of patterns, relative to source directory, that match files and +# directories to ignore when looking for source files. +exclude_patterns = [".build", "release.rst"] + +# The reST default role (used for this markup: `text`) to use for all +# documents. +default_role = "any" + +# If true, '()' will be appended to :func: etc. cross-reference text. +# add_function_parentheses = True + +# If true, the current module name will be prepended to all description +# unit titles (such as .. function::). +# add_module_names = True + +# If true, sectionauthor and moduleauthor directives will be shown in the +# output. They are ignored by default. +# show_authors = False + +# The name of the Pygments (syntax highlighting) style to use. +# pygments_style = 'monokai' + +# A list of ignored prefixes for module index sorting. +# modindex_common_prefix = [] + +# If true, keep warnings as "system message" paragraphs in the built documents. +# keep_warnings = False + +# If true, `todo` and `todoList` produce output, else they produce nothing. +todo_include_todos = False + + +# -- Options for HTML output ---------------------------------------------- + +# The theme to use for HTML and HTML Help pages. See the documentation for +# a list of builtin themes. + +html_theme = "furo" + +# Theme options are theme-specific and customize the look and feel of a theme +# further. For a list of options available for each theme, see the +# documentation. +# html_theme_options = {} + +# Add any paths that contain custom themes here, relative to this directory. +# html_theme_path = [] + +# The name for this set of Sphinx documents. If None, it defaults to +# " v documentation". +# html_title = None + +# A shorter title for the navigation bar. Default is the same as html_title. +# html_short_title = None + +# The name of an image file (relative to this directory) to place at the top +# of the sidebar. +# html_logo = None + +# The name of an image file (within the static path) to use as favicon of the +# docs. This file should be a Windows icon file (.ico) being 16x16 or 32x32 +# pixels large. +# html_favicon = None + +# Add any paths that contain custom static files (such as style sheets) here, +# relative to this directory. They are copied after the builtin static files, +# so a file named "default.css" will overwrite the builtin "default.css". +html_static_path = ["_static"] + +html_css_files = [ + "css/custom.css", +] + +# Add any extra paths that contain custom files (such as robots.txt or +# .htaccess) here, relative to this directory. These files are copied +# directly to the root of the documentation. +# html_extra_path = [] + +# If not '', a 'Last updated on:' timestamp is inserted at every page bottom, +# using the given strftime format. +# html_last_updated_fmt = '%b %d, %Y' + +# If true, SmartyPants will be used to convert quotes and dashes to +# typographically correct entities. +# html_use_smartypants = True + +# Custom sidebar templates, maps document names to template names. +# html_sidebars = {} + +# Additional templates that should be rendered to pages, maps page names to +# template names. +# html_additional_pages = {} + +# If false, no module index is generated. +# html_domain_indices = True + +# If false, no index is generated. +# html_use_index = True + +# If true, the index is split into individual pages for each letter. +# html_split_index = False + +# If true, links to the reST sources are added to the pages. +# html_show_sourcelink = True + +# If true, "Created using Sphinx" is shown in the HTML footer. Default is True. +# html_show_sphinx = True + +# If true, "(C) Copyright ..." is shown in the HTML footer. Default is True. +# html_show_copyright = True + +# If true, an OpenSearch description file will be output, and all pages will +# contain a tag referring to it. The value of this option must be the +# base URL from which the finished HTML is served. +# html_use_opensearch = '' + +# This is the file name suffix for HTML files (e.g. ".xhtml"). +# html_file_suffix = None + +# Language to be used for generating the HTML full-text search index. +# Sphinx supports the following languages: +# 'da', 'de', 'en', 'es', 'fi', 'fr', 'h', 'it', 'ja' +# 'nl', 'no', 'pt', 'ro', 'r', 'sv', 'tr' +# html_search_language = 'en' + +# A dictionary with options for the search language support, empty by default. +# Now only 'ja' uses this config value +# html_search_options = {'type': 'default'} + +# The name of a javascript file (relative to the configuration directory) that +# implements a search results scorer. If empty, the default will be used. +# html_search_scorer = 'scorer.js' + +# Output file base name for HTML help builder. +htmlhelp_basename = "pybind11doc" + +# -- Options for LaTeX output --------------------------------------------- + +latex_engine = "pdflatex" + +latex_elements = { + # The paper size ('letterpaper' or 'a4paper'). + # 'papersize': 'letterpaper', + # + # The font size ('10pt', '11pt' or '12pt'). + # 'pointsize': '10pt', + # + # Additional stuff for the LaTeX preamble. + # remove blank pages (between the title page and the TOC, etc.) + "classoptions": ",openany,oneside", + "preamble": r""" +\usepackage{fontawesome} +\usepackage{textgreek} +\DeclareUnicodeCharacter{00A0}{} +\DeclareUnicodeCharacter{2194}{\faArrowsH} +\DeclareUnicodeCharacter{1F382}{\faBirthdayCake} +\DeclareUnicodeCharacter{1F355}{\faAdjust} +\DeclareUnicodeCharacter{0301}{'} +\DeclareUnicodeCharacter{03C0}{\textpi} + +""", + # Latex figure (float) alignment + # 'figure_align': 'htbp', +} + +# Grouping the document tree into LaTeX files. List of tuples +# (source start file, target name, title, +# author, documentclass [howto, manual, or own class]). +latex_documents = [ + (master_doc, "pybind11.tex", "pybind11 Documentation", "Wenzel Jakob", "manual"), +] + +# The name of an image file (relative to this directory) to place at the top of +# the title page. +# latex_logo = 'pybind11-logo.png' + +# For "manual" documents, if this is true, then toplevel headings are parts, +# not chapters. +# latex_use_parts = False + +# If true, show page references after internal links. +# latex_show_pagerefs = False + +# If true, show URL addresses after external links. +# latex_show_urls = False + +# Documents to append as an appendix to all manuals. +# latex_appendices = [] + +# If false, no module index is generated. +# latex_domain_indices = True + + +# -- Options for manual page output --------------------------------------- + +# One entry per manual page. List of tuples +# (source start file, name, description, authors, manual section). +man_pages = [(master_doc, "pybind11", "pybind11 Documentation", [author], 1)] + +# If true, show URL addresses after external links. +# man_show_urls = False + + +# -- Options for Texinfo output ------------------------------------------- + +# Grouping the document tree into Texinfo files. List of tuples +# (source start file, target name, title, author, +# dir menu entry, description, category) +texinfo_documents = [ + ( + master_doc, + "pybind11", + "pybind11 Documentation", + author, + "pybind11", + "One line description of project.", + "Miscellaneous", + ), +] + +# Documents to append as an appendix to all manuals. +# texinfo_appendices = [] + +# If false, no module index is generated. +# texinfo_domain_indices = True + +# How to display URL addresses: 'footnote', 'no', or 'inline'. +# texinfo_show_urls = 'footnote' + +# If true, do not generate a @detailmenu in the "Top" node's menu. +# texinfo_no_detailmenu = False + +primary_domain = "cpp" +highlight_language = "cpp" + + +def generate_doxygen_xml(app): + build_dir = os.path.join(app.confdir, ".build") + if not os.path.exists(build_dir): + os.mkdir(build_dir) + + try: + subprocess.call(["doxygen", "--version"]) + retcode = subprocess.call(["doxygen"], cwd=app.confdir) + if retcode < 0: + sys.stderr.write(f"doxygen error code: {-retcode}\n") + except OSError as e: + sys.stderr.write(f"doxygen execution failed: {e}\n") + + +def prepare(app): + with open(DIR.parent / "README.rst") as f: + contents = f.read() + + if app.builder.name == "latex": + # Remove badges and stuff from start + contents = contents[contents.find(r".. start") :] + + # Filter out section titles for index.rst for LaTeX + contents = re.sub(r"^(.*)\n[-~]{3,}$", r"**\1**", contents, flags=re.MULTILINE) + + with open(DIR / "readme.rst", "w") as f: + f.write(contents) + + +def clean_up(app, exception): # noqa: ARG001 + (DIR / "readme.rst").unlink() + + +def setup(app): + # Add hook for building doxygen xml when needed + app.connect("builder-inited", generate_doxygen_xml) + + # Copy the readme in + app.connect("builder-inited", prepare) + + # Clean up the generated readme + app.connect("build-finished", clean_up) diff --git a/external_libraries/pybind11/docs/faq.rst b/external_libraries/pybind11/docs/faq.rst new file mode 100644 index 00000000..2b89d203 --- /dev/null +++ b/external_libraries/pybind11/docs/faq.rst @@ -0,0 +1,351 @@ +Frequently asked questions +########################## + +"ImportError: dynamic module does not define init function" +=========================================================== + +1. Make sure that the name specified in PYBIND11_MODULE is identical to the +filename of the extension library (without suffixes such as ``.so``). + +2. If the above did not fix the issue, you are likely using an incompatible +version of Python that does not match what you compiled with. + +"Symbol not found: ``__Py_ZeroStruct`` / ``_PyInstanceMethod_Type``" +======================================================================== + +See the first answer. + +"SystemError: dynamic module not initialized properly" +====================================================== + +See the first answer. + +The Python interpreter immediately crashes when importing my module +=================================================================== + +See the first answer. + +.. _faq_reference_arguments: + +Limitations involving reference arguments +========================================= + +In C++, it's fairly common to pass arguments using mutable references or +mutable pointers, which allows both read and write access to the value +supplied by the caller. This is sometimes done for efficiency reasons, or to +realize functions that have multiple return values. Here are two very basic +examples: + +.. code-block:: cpp + + void increment(int &i) { i++; } + void increment_ptr(int *i) { (*i)++; } + +In Python, all arguments are passed by reference, so there is no general +issue in binding such code from Python. + +However, certain basic Python types (like ``str``, ``int``, ``bool``, +``float``, etc.) are **immutable**. This means that the following attempt +to port the function to Python doesn't have the same effect on the value +provided by the caller -- in fact, it does nothing at all. + +.. code-block:: python + + def increment(i): + i += 1 # nope.. + +pybind11 is also affected by such language-level conventions, which means that +binding ``increment`` or ``increment_ptr`` will also create Python functions +that don't modify their arguments. + +Although inconvenient, one workaround is to encapsulate the immutable types in +a custom type that does allow modifications. + +An other alternative involves binding a small wrapper lambda function that +returns a tuple with all output arguments (see the remainder of the +documentation for examples on binding lambda functions). An example: + +.. code-block:: cpp + + int foo(int &i) { i++; return 123; } + +and the binding code + +.. code-block:: cpp + + m.def("foo", [](int i) { int rv = foo(i); return std::make_tuple(rv, i); }); + + +How can I reduce the build time? +================================ + +It's good practice to split binding code over multiple files, as in the +following example: + +:file:`example.cpp`: + +.. code-block:: cpp + + void init_ex1(py::module_ &); + void init_ex2(py::module_ &); + /* ... */ + + PYBIND11_MODULE(example, m, py::mod_gil_not_used()) { + init_ex1(m); + init_ex2(m); + /* ... */ + } + +:file:`ex1.cpp`: + +.. code-block:: cpp + + void init_ex1(py::module_ &m) { + m.def("add", [](int a, int b) { return a + b; }); + } + +:file:`ex2.cpp`: + +.. code-block:: cpp + + void init_ex2(py::module_ &m) { + m.def("sub", [](int a, int b) { return a - b; }); + } + +:command:`python`: + +.. code-block:: pycon + + >>> import example + >>> example.add(1, 2) + 3 + >>> example.sub(1, 1) + 0 + +As shown above, the various ``init_ex`` functions should be contained in +separate files that can be compiled independently from one another, and then +linked together into the same final shared object. Following this approach +will: + +1. reduce memory requirements per compilation unit. + +2. enable parallel builds (if desired). + +3. allow for faster incremental builds. For instance, when a single class + definition is changed, only a subset of the binding code will generally need + to be recompiled. + +"recursive template instantiation exceeded maximum depth of 256" +================================================================ + +If you receive an error about excessive recursive template evaluation, try +specifying a larger value, e.g. ``-ftemplate-depth=1024`` on GCC/Clang. The +culprit is generally the generation of function signatures at compile time +using C++14 template metaprogramming. + +.. _`faq:hidden_visibility`: + +"'SomeClass' declared with greater visibility than the type of its field 'SomeClass::member' [-Wattributes]" +============================================================================================================ + +This error typically indicates that you are compiling without the required +``-fvisibility`` flag. pybind11 code internally forces hidden visibility on +all internal code, but if non-hidden (and thus *exported*) code attempts to +include a pybind type (for example, ``py::object`` or ``py::list``) you can run +into this warning. + +To avoid it, make sure you are specifying ``-fvisibility=hidden`` when +compiling pybind code. + +As to why ``-fvisibility=hidden`` is necessary, because pybind modules could +have been compiled under different versions of pybind itself, it is also +important that the symbols defined in one module do not clash with the +potentially-incompatible symbols defined in another. While Python extension +modules are usually loaded with localized symbols (under POSIX systems +typically using ``dlopen`` with the ``RTLD_LOCAL`` flag), this Python default +can be changed, but even if it isn't it is not always enough to guarantee +complete independence of the symbols involved when not using +``-fvisibility=hidden``. + +Additionally, ``-fvisibility=hidden`` can deliver considerably binary size +savings. (See the following section for more details.) + + +.. _`faq:symhidden`: + +How can I create smaller binaries? +================================== + +To do its job, pybind11 extensively relies on a programming technique known as +*template metaprogramming*, which is a way of performing computation at compile +time using type information. Template metaprogramming usually instantiates code +involving significant numbers of deeply nested types that are either completely +removed or reduced to just a few instructions during the compiler's optimization +phase. However, due to the nested nature of these types, the resulting symbol +names in the compiled extension library can be extremely long. For instance, +the included test suite contains the following symbol: + +.. only:: html + + .. code-block:: none + + _​_​Z​N​8​p​y​b​i​n​d​1​1​1​2​c​p​p​_​f​u​n​c​t​i​o​n​C​1​I​v​8​E​x​a​m​p​l​e​2​J​R​N​S​t​3​_​_​1​6​v​e​c​t​o​r​I​N​S​3​_​1​2​b​a​s​i​c​_​s​t​r​i​n​g​I​w​N​S​3​_​1​1​c​h​a​r​_​t​r​a​i​t​s​I​w​E​E​N​S​3​_​9​a​l​l​o​c​a​t​o​r​I​w​E​E​E​E​N​S​8​_​I​S​A​_​E​E​E​E​E​J​N​S​_​4​n​a​m​e​E​N​S​_​7​s​i​b​l​i​n​g​E​N​S​_​9​i​s​_​m​e​t​h​o​d​E​A​2​8​_​c​E​E​E​M​T​0​_​F​T​_​D​p​T​1​_​E​D​p​R​K​T​2​_ + +.. only:: not html + + .. code-block:: cpp + + __ZN8pybind1112cpp_functionC1Iv8Example2JRNSt3__16vectorINS3_12basic_stringIwNS3_11char_traitsIwEENS3_9allocatorIwEEEENS8_ISA_EEEEEJNS_4nameENS_7siblingENS_9is_methodEA28_cEEEMT0_FT_DpT1_EDpRKT2_ + +which is the mangled form of the following function type: + +.. code-block:: cpp + + pybind11::cpp_function::cpp_function, std::__1::allocator >, std::__1::allocator, std::__1::allocator > > >&, pybind11::name, pybind11::sibling, pybind11::is_method, char [28]>(void (Example2::*)(std::__1::vector, std::__1::allocator >, std::__1::allocator, std::__1::allocator > > >&), pybind11::name const&, pybind11::sibling const&, pybind11::is_method const&, char const (&) [28]) + +The memory needed to store just the mangled name of this function (196 bytes) +is larger than the actual piece of code (111 bytes) it represents! On the other +hand, it's silly to even give this function a name -- after all, it's just a +tiny cog in a bigger piece of machinery that is not exposed to the outside +world. So we'll generally only want to export symbols for those functions which +are actually called from the outside. + +This can be achieved by specifying the parameter ``-fvisibility=hidden`` to GCC +and Clang, which sets the default symbol visibility to *hidden*, which has a +tremendous impact on the final binary size of the resulting extension library. +(On Visual Studio, symbols are already hidden by default, so nothing needs to +be done there.) + +In addition to decreasing binary size, ``-fvisibility=hidden`` also avoids +potential serious issues when loading multiple modules and is required for +proper pybind operation. See the previous FAQ entry for more details. + +How can I properly handle Ctrl-C in long-running functions? +=========================================================== + +Ctrl-C is received by the Python interpreter, and holds it until the GIL +is released, so a long-running function won't be interrupted. + +To interrupt from inside your function, you can use the ``PyErr_CheckSignals()`` +function, that will tell if a signal has been raised on the Python side. This +function merely checks a flag, so its impact is negligible. When a signal has +been received, you must either explicitly interrupt execution by throwing +``py::error_already_set`` (which will propagate the existing +``KeyboardInterrupt``), or clear the error (which you usually will not want): + +.. code-block:: cpp + + PYBIND11_MODULE(example, m, py::mod_gil_not_used()) { + m.def("long running_func", []() + { + for (;;) { + if (PyErr_CheckSignals() != 0) + throw py::error_already_set(); + // Long running iteration + } + }); + } + +What is a highly conclusive and simple way to find memory leaks (e.g. in pybind11 bindings)? +============================================================================================ + +Use ``while True`` & ``top`` (Linux, macOS). + +For example, locally change tests/test_type_caster_pyobject_ptr.py like this: + +.. code-block:: diff + + def test_return_list_pyobject_ptr_reference(): + + while True: + vec_obj = m.return_list_pyobject_ptr_reference(ValueHolder) + assert [e.value for e in vec_obj] == [93, 186] + # Commenting out the next `assert` will leak the Python references. + # An easy way to see evidence of the leaks: + # Insert `while True:` as the first line of this function and monitor the + # process RES (Resident Memory Size) with the Unix top command. + - assert m.dec_ref_each_pyobject_ptr(vec_obj) == 2 + + # assert m.dec_ref_each_pyobject_ptr(vec_obj) == 2 + +Then run the test as you would normally do, which will go into the infinite loop. + +**In another shell, but on the same machine** run: + +.. code-block:: bash + + top + +This will show: + +.. code-block:: + + PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND + 1266095 rwgk 20 0 5207496 611372 45696 R 100.0 0.3 0:08.01 test_type_caste + +Look for the number under ``RES`` there. You'll see it going up very quickly. + +**Don't forget to Ctrl-C the test command** before your machine becomes +unresponsive due to swapping. + +This method only takes a couple minutes of effort and is very conclusive. +What you want to see is that the ``RES`` number is stable after a couple +seconds. + +CMake doesn't detect the right Python version +============================================= + +The CMake-based build system will try to automatically detect the installed +version of Python and link against that. When this fails, or when there are +multiple versions of Python and it finds the wrong one, delete +``CMakeCache.txt`` and then add ``-DPYTHON_EXECUTABLE=$(which python)`` to your +CMake configure line. (Replace ``$(which python)`` with a path to python if +your prefer.) + +You can alternatively try ``-DPYBIND11_FINDPYTHON=ON``, which will activate the +new CMake FindPython support instead of pybind11's custom search. Newer CMake, +like, 3.18.2+, is recommended. You can set this in your ``CMakeLists.txt`` +before adding or finding pybind11, as well. + +Inconsistent detection of Python version in CMake and pybind11 +============================================================== + +The functions ``find_package(PythonInterp)`` and ``find_package(PythonLibs)`` +provided by CMake for Python version detection are modified by pybind11 due to +unreliability and limitations that make them unsuitable for pybind11's needs. +Instead pybind11 provides its own, more reliable Python detection CMake code. +Conflicts can arise, however, when using pybind11 in a project that *also* uses +the CMake Python detection in a system with several Python versions installed. + +This difference may cause inconsistencies and errors if *both* mechanisms are +used in the same project. + +There are three possible solutions: + +1. Avoid using ``find_package(PythonInterp)`` and ``find_package(PythonLibs)`` + from CMake and rely on pybind11 in detecting Python version. If this is not + possible, the CMake machinery should be called *before* including pybind11. +2. Set ``PYBIND11_FINDPYTHON`` to ``True`` or use ``find_package(Python + COMPONENTS Interpreter Development)`` on modern CMake ( 3.18.2+ best). + Pybind11 in these cases uses the new CMake FindPython instead of the old, + deprecated search tools, and these modules are much better at finding the + correct Python. If FindPythonLibs/Interp are not available (CMake 3.27+), + then this will be ignored and FindPython will be used. +3. Set ``PYBIND11_NOPYTHON`` to ``TRUE``. Pybind11 will not search for Python. + However, you will have to use the target-based system, and do more setup + yourself, because it does not know about or include things that depend on + Python, like ``pybind11_add_module``. This might be ideal for integrating + into an existing system, like scikit-build's Python helpers. + +How to cite this project? +========================= + +We suggest the following BibTeX template to cite pybind11 in scientific +discourse: + +.. code-block:: bash + + @misc{pybind11, + author = {Wenzel Jakob and Jason Rhinelander and Dean Moldovan}, + year = {2017}, + note = {https://github.com/pybind/pybind11}, + title = {pybind11 -- Seamless operability between C++11 and Python} + } diff --git a/external_libraries/pybind11/docs/index.rst b/external_libraries/pybind11/docs/index.rst new file mode 100644 index 00000000..77b097c5 --- /dev/null +++ b/external_libraries/pybind11/docs/index.rst @@ -0,0 +1,49 @@ +.. only:: latex + + Intro + ===== + +.. include:: readme.rst + +.. only:: not latex + + Contents: + +.. toctree:: + :maxdepth: 1 + + changelog + upgrade + +.. toctree:: + :caption: The Basics + :maxdepth: 2 + + installing + basics + classes + compiling + +.. toctree:: + :caption: Advanced Topics + :maxdepth: 2 + + advanced/functions + advanced/classes + advanced/exceptions + advanced/smart_ptrs + advanced/cast/index + advanced/pycpp/index + advanced/embedding + advanced/misc + advanced/deprecated + +.. toctree:: + :caption: Extra Information + :maxdepth: 1 + + faq + benchmark + limitations + reference + cmake/index diff --git a/external_libraries/pybind11/docs/installing.rst b/external_libraries/pybind11/docs/installing.rst new file mode 100644 index 00000000..1817372f --- /dev/null +++ b/external_libraries/pybind11/docs/installing.rst @@ -0,0 +1,105 @@ +.. _installing: + +Installing the library +###################### + +There are several ways to get the pybind11 source, which lives at +`pybind/pybind11 on GitHub `_. The pybind11 +developers recommend one of the first three ways listed here, submodule, PyPI, +or conda-forge, for obtaining pybind11. + +.. _include_as_a_submodule: + +Include as a submodule +====================== + +When you are working on a project in Git, you can use the pybind11 repository +as a submodule. From your git repository, use: + +.. code-block:: bash + + git submodule add -b stable ../../pybind/pybind11 extern/pybind11 + git submodule update --init + +This assumes you are placing your dependencies in ``extern/``, and that you are +using GitHub; if you are not using GitHub, use the full https or ssh URL +instead of the relative URL ``../../pybind/pybind11`` above. Some other servers +also require the ``.git`` extension (GitHub does not). + +From here, you can now include ``extern/pybind11/include``, or you can use +the various integration tools (see :ref:`compiling`) pybind11 provides directly +from the local folder. + +Include with PyPI +================= + +You can download the sources and CMake files as a Python package from PyPI +using Pip. Just use: + +.. code-block:: bash + + pip install pybind11 + +This will provide pybind11 in a standard Python package format. If you want +pybind11 available directly in your environment root, you can use: + +.. code-block:: bash + + pip install "pybind11[global]" + +This is not recommended if you are installing with your system Python, as it +will add files to ``/usr/local/include/pybind11`` and +``/usr/local/share/cmake/pybind11``, so unless that is what you want, it is +recommended only for use in virtual environments or your ``pyproject.toml`` +file (see :ref:`compiling`). + +Include with conda-forge +======================== + +You can use pybind11 with conda packaging via `conda-forge +`_: + +.. code-block:: bash + + conda install -c conda-forge pybind11 + + +Include with vcpkg +================== +You can download and install pybind11 using the Microsoft `vcpkg +`_ dependency manager: + +.. code-block:: bash + + git clone https://github.com/Microsoft/vcpkg.git + cd vcpkg + ./bootstrap-vcpkg.sh + ./vcpkg integrate install + vcpkg install pybind11 + +The pybind11 port in vcpkg is kept up to date by Microsoft team members and +community contributors. If the version is out of date, please `create an issue +or pull request `_ on the vcpkg +repository. + +Global install with brew +======================== + +The brew package manager (Homebrew on macOS, or Linuxbrew on Linux) has a +`pybind11 package +`_. +To install: + +.. code-block:: bash + + brew install pybind11 + +.. We should list Conan, and possibly a few other C++ package managers (hunter, +.. perhaps). Conan has a very clean CMake integration that would be good to show. + +Other options +============= + +Other locations you can find pybind11 are `listed here +`_; these are maintained +by various packagers and the community. diff --git a/external_libraries/pybind11/docs/limitations.rst b/external_libraries/pybind11/docs/limitations.rst new file mode 100644 index 00000000..1b06ea87 --- /dev/null +++ b/external_libraries/pybind11/docs/limitations.rst @@ -0,0 +1,68 @@ +Limitations +########### + +Design choices +^^^^^^^^^^^^^^ + +pybind11 strives to be a general solution to binding generation, but it also has +certain limitations: + +- pybind11 casts away ``const``-ness in function arguments and return values. + This is in line with the Python language, which has no concept of ``const`` + values. This means that some additional care is needed to avoid bugs that + would be caught by the type checker in a traditional C++ program. + +- The NumPy interface ``pybind11::array`` greatly simplifies accessing + numerical data from C++ (and vice versa), but it's not a full-blown array + class like ``Eigen::Array`` or ``boost.multi_array``. ``Eigen`` objects are + directly supported, however, with ``pybind11/eigen.h``. + +Large but useful features could be implemented in pybind11 but would lead to a +significant increase in complexity. Pybind11 strives to be simple and compact. +Users who require large new features are encouraged to write an extension to +pybind11; see `pybind11_json `_ for an +example. + + +Known bugs +^^^^^^^^^^ + +These are issues that hopefully will one day be fixed, but currently are +unsolved. If you know how to help with one of these issues, contributions +are welcome! + +- Intel 20.2 is currently having an issue with the test suite. + `#2573 `_ + +- Debug mode Python does not support 1-5 tests in the test suite currently. + `#2422 `_ + +- PyPy3 7.3.1 and 7.3.2 have issues with several tests on 32-bit Windows. + +Known limitations +^^^^^^^^^^^^^^^^^ + +These are issues that are probably solvable, but have not been fixed yet. A +clean, well written patch would likely be accepted to solve them. + +- Type casters are not kept alive recursively. + `#2527 `_ + One consequence is that containers of ``char *`` are currently not supported. + `#2245 `_ + +Python 3.9.0 warning +^^^^^^^^^^^^^^^^^^^^ + +Combining older versions of pybind11 (< 2.6.0) with Python on exactly 3.9.0 +will trigger undefined behavior that typically manifests as crashes during +interpreter shutdown (but could also destroy your data. **You have been +warned**). + +This issue was `fixed in Python `_. +As a mitigation for this bug, pybind11 2.6.0 or newer includes a workaround +specifically when Python 3.9.0 is detected at runtime, leaking about 50 bytes +of memory when a callback function is garbage collected. For reference, the +pybind11 test suite has about 2,000 such callbacks, but only 49 are garbage +collected before the end-of-process. Wheels (even if built with Python 3.9.0) +will correctly avoid the leak when run in Python 3.9.1, and this does not +affect other 3.X versions. diff --git a/external_libraries/pybind11/docs/pybind11-logo.png b/external_libraries/pybind11/docs/pybind11-logo.png new file mode 100644 index 0000000000000000000000000000000000000000..2d633a4d0c129d6bdd94b4134c9516fdc92bb192 GIT binary patch literal 61034 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 literal 0 HcmV?d00001 diff --git a/external_libraries/pybind11/docs/pybind11_vs_boost_python1.png b/external_libraries/pybind11/docs/pybind11_vs_boost_python1.png new file mode 100644 index 0000000000000000000000000000000000000000..833231f240809884fb6eb4079db528b9b3c0a9ac GIT binary patch literal 44653 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 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/external_libraries/pybind11/docs/pybind11_vs_boost_python2.png b/external_libraries/pybind11/docs/pybind11_vs_boost_python2.png new file mode 100644 index 0000000000000000000000000000000000000000..9f17272c50663957d6ae6d8e23fdd5a15757e71f GIT binary patch literal 41121 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 BtT6xp literal 0 HcmV?d00001 diff --git a/external_libraries/pybind11/docs/pybind11_vs_boost_python2.svg b/external_libraries/pybind11/docs/pybind11_vs_boost_python2.svg new file mode 100644 index 00000000..5ed6530c --- /dev/null +++ b/external_libraries/pybind11/docs/pybind11_vs_boost_python2.svg @@ -0,0 +1,427 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/external_libraries/pybind11/docs/reference.rst b/external_libraries/pybind11/docs/reference.rst new file mode 100644 index 00000000..c2757988 --- /dev/null +++ b/external_libraries/pybind11/docs/reference.rst @@ -0,0 +1,130 @@ +.. _reference: + +.. warning:: + + Please be advised that the reference documentation discussing pybind11 + internals is currently incomplete. Please refer to the previous sections + and the pybind11 header files for the nitty gritty details. + +Reference +######### + +.. _macros: + +Macros +====== + +.. doxygendefine:: PYBIND11_MODULE + +.. _core_types: + +Convenience classes for arbitrary Python types +============================================== + +Common member functions +----------------------- + +.. doxygenclass:: object_api + :members: + +Without reference counting +-------------------------- + +.. doxygenclass:: handle + :members: + +With reference counting +----------------------- + +.. doxygenclass:: object + :members: + +.. doxygenfunction:: reinterpret_borrow + +.. doxygenfunction:: reinterpret_steal + +Convenience classes for specific Python types +============================================= + +.. doxygenclass:: module_ + :members: + +.. doxygengroup:: pytypes + :members: + +Convenience functions converting to Python types +================================================ + +.. doxygenfunction:: make_tuple(Args&&...) + +.. doxygenfunction:: make_iterator(Iterator, Sentinel, Extra &&...) +.. doxygenfunction:: make_iterator(Type &, Extra&&...) + +.. doxygenfunction:: make_key_iterator(Iterator, Sentinel, Extra &&...) +.. doxygenfunction:: make_key_iterator(Type &, Extra&&...) + +.. doxygenfunction:: make_value_iterator(Iterator, Sentinel, Extra &&...) +.. doxygenfunction:: make_value_iterator(Type &, Extra&&...) + +.. _extras: + +Passing extra arguments to ``def`` or ``py::class_`` +==================================================== + +.. doxygengroup:: annotations + :members: + +Embedding the interpreter +========================= + +.. doxygendefine:: PYBIND11_EMBEDDED_MODULE + +.. doxygenfunction:: initialize_interpreter + +.. doxygenfunction:: finalize_interpreter + +.. doxygenclass:: scoped_interpreter + +Redirecting C++ streams +======================= + +.. doxygenclass:: scoped_ostream_redirect + +.. doxygenclass:: scoped_estream_redirect + +.. doxygenfunction:: add_ostream_redirect + +Python built-in functions +========================= + +.. doxygengroup:: python_builtins + :members: + +Inheritance +=========== + +See :doc:`/classes` and :doc:`/advanced/classes` for more detail. + +.. doxygendefine:: PYBIND11_OVERRIDE + +.. doxygendefine:: PYBIND11_OVERRIDE_PURE + +.. doxygendefine:: PYBIND11_OVERRIDE_NAME + +.. doxygendefine:: PYBIND11_OVERRIDE_PURE_NAME + +.. doxygenfunction:: get_override + +Exceptions +========== + +.. doxygenclass:: error_already_set + :members: + +.. doxygenclass:: builtin_exception + :members: + +Literals +======== + +.. doxygennamespace:: literals diff --git a/external_libraries/pybind11/docs/release.rst b/external_libraries/pybind11/docs/release.rst new file mode 100644 index 00000000..98b97d95 --- /dev/null +++ b/external_libraries/pybind11/docs/release.rst @@ -0,0 +1,135 @@ +On version numbers +^^^^^^^^^^^^^^^^^^ + +The version number must be a valid `PEP 440 +`_ version number. + +For example: + +.. code-block:: C++ + + #define PYBIND11_VERSION_MAJOR X + #define PYBIND11_VERSION_MINOR Y + #define PYBIND11_VERSION_MICRO Z + #define PYBIND11_VERSION_RELEASE_LEVEL PY_RELEASE_LEVEL_ALPHA + #define PYBIND11_VERSION_RELEASE_SERIAL 0 + #define PYBIND11_VERSION_PATCH Za0 + +For beta, ``PYBIND11_VERSION_PATCH`` should be ``Zb1``. RC's can be ``Zrc1``. +For a final release, this must be a simple integer. + + +To release a new version of pybind11: +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +If you don't have nox, you should either use ``pipx run nox`` instead, or use +``uv tool install nox``, ``pipx install nox``, or ``brew install nox`` (Unix). + +- Update the version number + + - Update ``PYBIND11_VERSION_MAJOR`` etc. in + ``include/pybind11/detail/common.h``. MICRO should be a simple integer. + + - Run ``nox -s tests_packaging`` to ensure this was done correctly. + +- Ensure that all the information in ``pyproject.toml`` is up-to-date, like + supported Python versions. + +- Add release date in ``docs/changelog.md`` and integrate the output of + ``nox -s make_changelog``. + + - Note that the ``nox -s make_changelog`` command inspects + `needs changelog `_. + + - Manually clear the ``needs changelog`` labels using the GitHub web + interface (very easy: start by clicking the link above). + +- ``git add`` and ``git commit``, ``git push``. **Ensure CI passes**. (If it + fails due to a known flake issue, either ignore or restart CI.) + +- Add a release branch if this is a new MINOR version, or update the existing + release branch if it is a patch version + + - NOTE: This documentation assumes your ``upstream`` is ``https://github.com/pybind/pybind11.git`` + + - New branch: ``git checkout -b vX.Y``, ``git push -u upstream vX.Y`` + + - Update branch: ``git checkout vX.Y``, ``git merge ``, ``git push`` + +- Update tags (optional; if you skip this, the GitHub release makes a + non-annotated tag for you) + + - ``git tag -a vX.Y.Z -m 'vX.Y.Z release'`` + + - ``git grep PYBIND11_VERSION include/pybind11/detail/common.h`` + + - Last-minute consistency check: same as tag? + + - Push the new tag: ``git push upstream vX.Y.Z`` + +- Update stable + + - ``git checkout stable`` + + - ``git merge -X theirs vX.Y.Z`` + + - ``git diff vX.Y.Z`` + + - Carefully review and reconcile any diffs. There should be none. + + - ``git push`` + +- Make a GitHub release (this shows up in the UI, sends new release + notifications to users watching releases, and also uploads PyPI packages). + (Note: if you do not use an existing tag, this creates a new lightweight tag + for you, so you could skip the above step.) + + - GUI method: Under `releases `_ + click "Draft a new release" on the far right, fill in the tag name + (if you didn't tag above, it will be made here), fill in a release name + like "Version X.Y.Z", and copy-and-paste the markdown-formatted (!) changelog + into the description. You can remove line breaks and optionally strip links + to PRs and issues, e.g. to a bare ``#1234`` without the hyperlink markup. + Check "pre-release" if this is an alpha/beta/RC. + + - CLI method: with ``gh`` installed, run ``gh release create vX.Y.Z -t "Version X.Y.Z"`` + If this is a pre-release, add ``-p``. + +- Get back to work + + - Make sure you are on master, not somewhere else: ``git checkout master`` + + - Update version macros in ``include/pybind11/detail/common.h`` (set PATCH to + ``0a0`` and increment MINOR). + + - Update ``pybind11/_version.py`` to match. + + - Run ``nox -s tests_packaging`` to ensure this was done correctly. + + - If the release was a new MINOR version, add a new ``IN DEVELOPMENT`` + section in ``docs/changelog.md``. + + - ``git add``, ``git commit``, ``git push`` + +If a version branch is updated, remember to set PATCH to ``1a0``. + +Conda-forge should automatically make a PR in a few hours, and automatically +merge it if there are no issues. Homebrew should be automatic, too. + + +Manual packaging +^^^^^^^^^^^^^^^^ + +If you need to manually upload releases, you can download the releases from +the job artifacts and upload them with twine. You can also make the files +locally (not recommended in general, as your local directory is more likely +to be "dirty" and SDists love picking up random unrelated/hidden files); +this is the procedure: + +.. code-block:: bash + + nox -s build + nox -s build_global + twine upload dist/* + +This makes SDists and wheels, and the final line uploads them. diff --git a/external_libraries/pybind11/docs/requirements.in b/external_libraries/pybind11/docs/requirements.in new file mode 100644 index 00000000..cb15ac5d --- /dev/null +++ b/external_libraries/pybind11/docs/requirements.in @@ -0,0 +1,7 @@ +breathe +furo +myst_parser +sphinx +sphinx-copybutton +sphinxcontrib-moderncmakedomain +sphinxcontrib-svg2pdfconverter diff --git a/external_libraries/pybind11/docs/requirements.txt b/external_libraries/pybind11/docs/requirements.txt new file mode 100644 index 00000000..c19fbf27 --- /dev/null +++ b/external_libraries/pybind11/docs/requirements.txt @@ -0,0 +1,87 @@ +# This file was autogenerated by uv via the following command: +# uv pip compile docs/requirements.in -o docs/requirements.txt +alabaster==0.7.16 + # via sphinx +babel==2.14.0 + # via sphinx +beautifulsoup4==4.12.3 + # via furo +breathe==4.35.0 + # via -r requirements.in +certifi==2024.7.4 + # via requests +charset-normalizer==3.3.2 + # via requests +docutils==0.20.1 + # via + # breathe + # myst-parser + # sphinx +furo==2024.1.29 + # via -r requirements.in +idna==3.7 + # via requests +imagesize==1.4.1 + # via sphinx +jinja2==3.1.6 + # via + # myst-parser + # sphinx +markdown-it-py==3.0.0 + # via + # mdit-py-plugins + # myst-parser +markupsafe==2.1.5 + # via jinja2 +mdit-py-plugins==0.4.2 + # via myst-parser +mdurl==0.1.2 + # via markdown-it-py +myst-parser==3.0.1 + # via -r requirements.in +packaging==24.0 + # via sphinx +pygments==2.20.0 + # via + # furo + # sphinx +pyyaml==6.0.2 + # via myst-parser +requests==2.33.0 + # via sphinx +snowballstemmer==2.2.0 + # via sphinx +soupsieve==2.5 + # via beautifulsoup4 +sphinx==7.2.6 + # via + # -r requirements.in + # breathe + # furo + # myst-parser + # sphinx-basic-ng + # sphinx-copybutton + # sphinxcontrib-moderncmakedomain + # sphinxcontrib-svg2pdfconverter +sphinx-basic-ng==1.0.0b2 + # via furo +sphinx-copybutton==0.5.2 + # via -r requirements.in +sphinxcontrib-applehelp==1.0.8 + # via sphinx +sphinxcontrib-devhelp==1.0.6 + # via sphinx +sphinxcontrib-htmlhelp==2.0.5 + # via sphinx +sphinxcontrib-jsmath==1.0.1 + # via sphinx +sphinxcontrib-moderncmakedomain==3.27.0 + # via -r requirements.in +sphinxcontrib-qthelp==1.0.7 + # via sphinx +sphinxcontrib-serializinghtml==1.1.10 + # via sphinx +sphinxcontrib-svg2pdfconverter==1.2.2 + # via -r requirements.in +urllib3==2.6.3 + # via requests diff --git a/external_libraries/pybind11/docs/upgrade.rst b/external_libraries/pybind11/docs/upgrade.rst new file mode 100644 index 00000000..966a319a --- /dev/null +++ b/external_libraries/pybind11/docs/upgrade.rst @@ -0,0 +1,748 @@ +Upgrade guide +############# + +This is a companion guide to the :doc:`changelog`. While the changelog briefly +lists all of the new features, improvements and bug fixes, this upgrade guide +focuses only the subset which directly impacts your experience when upgrading +to a new version. But it goes into more detail. This includes things like +deprecated APIs and their replacements, build system changes, general code +modernization and other useful information. + +.. _upgrade-guide-3.0: + +v3.0 +==== + +pybind11 v3.0 introduces major new features, but the vast majority of +existing extensions are expected to build and run without modification. Minor +adjustments may be needed in rare cases, and any such changes can be easily +wrapped in preprocessor conditionals to maintain compatibility with the +2.x series. + +However, due to new features and modernizations, extensions built with +pybind11 v3.0 are not ABI-compatible with those built using v2.13. To ensure +cross-extension-module compatibility, it is recommended to rebuild all +pybind11-based extensions with v3.0. + +CMake support now defaults to the modern FindPython module. If you haven't +updated yet, we provide some backward compatibility for ``PYTHON_*`` variables, +but you should switch to using ``Python_*`` variables instead. Note that +setting ``PYTHON_*`` variables no longer affects the build. + +A major new feature in this release is the integration of +``py::smart_holder``, which improves support for ``std::unique_ptr`` +and ``std::shared_ptr``, resolving several long-standing issues. See +:ref:`smart_holder` for details. Closely related is the addition +of ``py::trampoline_self_life_support``, documented under +:ref:`overriding_virtuals`. + +This release includes a major modernization of cross-extension-module +ABI compatibility handling. The new implementation reflects actual ABI +compatibility much more accurately than in previous versions. The details +are subtle and complex; see +`#4953 `_ and +`#5439 `_. + +Also new in v3.0 is ``py::native_enum``, a modern API for exposing +C++ enumerations as native Python types — typically standard-library +``enum.Enum`` or related subclasses. This provides improved integration with +Python's enum system, compared to the older (now deprecated) ``py::enum_``. +See `#5555 `_ for details. +Note that ``#include `` is not included automatically +and must be added explicitly. + +Functions exposed with pybind11 are now pickleable. This removes a +long-standing obstacle when using pybind11-bound functions with Python features +that rely on pickling, such as multiprocessing and caching tools. +See `#5580 `_ for details. + +Anything producing a deprecation warning in the 2.x series may be removed in a +future minor release of 3.x. Most of these are still present in 3.0 in order to ease +transition. The new :ref:`deprecated` page details deprecations. + +Migration Recommendations +------------------------- + +We recommend migrating to pybind11 v3.0 promptly, while keeping initial +changes to a minimum. Most projects can upgrade simply by updating the +pybind11 version, without altering existing binding code. + +After a short stabilization period — enough to surface any subtle issues — +you may incrementally adopt new features where appropriate: + +* Use ``py::smart_holder`` and ``py::trampoline_self_life_support`` as needed, + or to improve code health. Note that ``py::classh`` is available as a + shortcut — for example, ``py::classh`` is shorthand for + ``py::class_``. This is designed to enable easy + experimentation with ``py::smart_holder`` without introducing distracting + whitespace changes. In many cases, a global replacement of ``py::class_`` + with ``py::classh`` can be an effective first step. Build failures will + quickly identify places where ``std::shared_ptr<...>`` holders need to be + removed. Runtime failures (assuming good unit test coverage) will highlight + base-and-derived class situations that require coordinated changes. + + Note that ``py::bind_vector`` and ``py::bind_map`` (in pybind11/stl_bind.h) + have a ``holder_type`` template parameter that defaults to + ``std::unique_ptr``. If ``py::smart_holder`` functionality is desired or + required, use e.g. ``py::bind_vector``. + +* Gradually migrate from ``py::enum_`` to ``py::native_enum`` to improve + integration with Python's standard enum types. + +There is no urgency to refactor existing, working bindings — adopt new +features as the need arises or as part of ongoing maintenance efforts. + +If you are using CMake, update to FindPython variables (mostly changing +variables from ``PYTHON_*`` -> ``Python_*``). You should see if you can use +``set(PYBIND11_FINDPYTHON ON)``, which has been supported for years and will +avoid setting the compatibility mode variables (and will avoid a warning). + +Potential stumbling blocks when migrating to v3.0 +------------------------------------------------- + +The following issues are very unlikely to arise, and easy to work around: + +* In rare cases, a C++ enum may be bound to Python via a + :ref:`custom type caster `. In such cases, a + template specialization like this may be required: + + .. code-block:: cpp + + #if defined(PYBIND11_HAS_NATIVE_ENUM) + namespace pybind11::detail { + template + struct type_caster_enum_type_enabled< + FancyEnum, + enable_if_t::value>> : std::false_type {}; + } + #endif + + This specialization is needed only if the custom type caster is templated. + + The ``PYBIND11_HAS_NATIVE_ENUM`` guard is needed only + if backward compatibility with pybind11v2 is required. + +* Similarly, template specializations like the following may be required + if there are custom + + * ``pybind11::detail::copyable_holder_caster`` or + + * ``pybind11::detail::move_only_holder_caster`` + + implementations that are used for ``std::shared_ptr`` or ``std::unique_ptr`` + conversions: + + .. code-block:: cpp + + #if defined(PYBIND11_HAS_INTERNALS_WITH_SMART_HOLDER_SUPPORT) + namespace pybind11::detail { + template + struct copyable_holder_caster_shared_ptr_with_smart_holder_support_enabled< + ExampleType, + enable_if_t::value>> : std::false_type {}; + } + #endif + + .. code-block:: cpp + + #if defined(PYBIND11_HAS_INTERNALS_WITH_SMART_HOLDER_SUPPORT) + namespace pybind11::detail { + template + struct move_only_holder_caster_unique_ptr_with_smart_holder_support_enabled< + ExampleType, + enable_if_t::value>> : std::false_type {}; + } + #endif + + The ``PYBIND11_HAS_INTERNALS_WITH_SMART_HOLDER_SUPPORT`` guard is needed only + if backward compatibility with pybind11v2 is required. + + (Note that ``copyable_holder_caster`` and ``move_only_holder_caster`` are not + documented, although they existed since 2017.) + + +.. _upgrade-guide-2.12: + +v2.12 +===== + +NumPy support has been upgraded to support the 2.x series too. The two relevant +changes are that: + +* ``dtype.flags()`` is now a ``uint64`` and ``dtype.alignment()`` an + ``ssize_t`` (and NumPy may return an larger than integer value for + ``itemsize()`` in NumPy 2.x). + +* The long deprecated NumPy function ``PyArray_GetArrayParamsFromObject`` + function is not available anymore. + +Due to NumPy changes, you may experience difficulties updating to NumPy 2. +Please see the `NumPy 2 migration guide `_ +for details. +For example, a more direct change could be that the default integer ``"int_"`` +(and ``"uint"``) is now ``ssize_t`` and not ``long`` (affects 64bit windows). + +If you want to only support NumPy 1.x for now and are having problems due to +the two internal changes listed above, you can define +``PYBIND11_NUMPY_1_ONLY`` to disable the new support for now. Make sure you +define this on all pybind11 compile units, since it could be a source of ODR +violations if used inconsistently. This option will be removed in the future, +so adapting your code is highly recommended. + + +.. _upgrade-guide-2.11: + +v2.11 +===== + +* The minimum version of CMake is now 3.5. A future version will likely move to + requiring something like CMake 3.15. Note that CMake 3.27 is removing the + long-deprecated support for ``FindPythonInterp`` if you set 3.27 as the + minimum or maximum supported version. To prepare for that future, CMake 3.15+ + using ``FindPython`` or setting ``PYBIND11_FINDPYTHON`` is highly recommended, + otherwise pybind11 will automatically switch to using ``FindPython`` if + ``FindPythonInterp`` is not available. + + +.. _upgrade-guide-2.9: + +v2.9 +==== + +* Any usage of the recently added ``py::make_simple_namespace`` should be + converted to using ``py::module_::import("types").attr("SimpleNamespace")`` + instead. + +* The use of ``_`` in custom type casters can now be replaced with the more + readable ``const_name`` instead. The old ``_`` shortcut has been retained + unless it is being used as a macro (like for gettext). + + +.. _upgrade-guide-2.7: + +v2.7 +==== + +*Before* v2.7, ``py::str`` can hold ``PyUnicodeObject`` or ``PyBytesObject``, +and ``py::isinstance()`` is ``true`` for both ``py::str`` and +``py::bytes``. Starting with v2.7, ``py::str`` exclusively holds +``PyUnicodeObject`` (`#2409 `_), +and ``py::isinstance()`` is ``true`` only for ``py::str``. To help in +the transition of user code, the ``PYBIND11_STR_LEGACY_PERMISSIVE`` macro +is provided as an escape hatch to go back to the legacy behavior. This macro +will be removed in future releases. Two types of required fixes are expected +to be common: + +* Accidental use of ``py::str`` instead of ``py::bytes``, masked by the legacy + behavior. These are probably very easy to fix, by changing from + ``py::str`` to ``py::bytes``. + +* Reliance on py::isinstance(obj) being ``true`` for + ``py::bytes``. This is likely to be easy to fix in most cases by adding + ``|| py::isinstance(obj)``, but a fix may be more involved, e.g. if + ``py::isinstance`` appears in a template. Such situations will require + careful review and custom fixes. + + +.. _upgrade-guide-2.6: + +v2.6 +==== + +Usage of the ``PYBIND11_OVERLOAD*`` macros and ``get_overload`` function should +be replaced by ``PYBIND11_OVERRIDE*`` and ``get_override``. In the future, the +old macros may be deprecated and removed. + +``py::module`` has been renamed ``py::module_``, but a backward compatible +typedef has been included. This change was to avoid a language change in C++20 +that requires unqualified ``module`` not be placed at the start of a logical +line. Qualified usage is unaffected and the typedef will remain unless the +C++ language rules change again. + +The public constructors of ``py::module_`` have been deprecated. Use +``PYBIND11_MODULE`` or ``module_::create_extension_module`` instead. + +An error is now thrown when ``__init__`` is forgotten on subclasses. This was +incorrect before, but was not checked. Add a call to ``__init__`` if it is +missing. + +A ``py::type_error`` is now thrown when casting to a subclass (like +``py::bytes`` from ``py::object``) if the conversion is not valid. Make a valid +conversion instead. + +The undocumented ``h.get_type()`` method has been deprecated and replaced by +``py::type::of(h)``. + +Enums now have a ``__str__`` method pre-defined; if you want to override it, +the simplest fix is to add the new ``py::prepend()`` tag when defining +``"__str__"``. + +If ``__eq__`` defined but not ``__hash__``, ``__hash__`` is now set to +``None``, as in normal CPython. You should add ``__hash__`` if you intended the +class to be hashable, possibly using the new ``py::hash`` shortcut. + +The constructors for ``py::array`` now always take signed integers for size, +for consistency. This may lead to compiler warnings on some systems. Cast to +``py::ssize_t`` instead of ``std::size_t``. + +The ``tools/clang`` submodule and ``tools/mkdoc.py`` have been moved to a +standalone package, `pybind11-mkdoc`_. If you were using those tools, please +use them via a pip install from the new location. + +The ``pybind11`` package on PyPI no longer fills the wheel "headers" slot - if +you were using the headers from this slot, they are available by requesting the +``global`` extra, that is, ``pip install "pybind11[global]"``. (Most users will +be unaffected, as the ``pybind11/include`` location is reported by ``python -m +pybind11 --includes`` and ``pybind11.get_include()`` is still correct and has +not changed since 2.5). + +.. _pybind11-mkdoc: https://github.com/pybind/pybind11-mkdoc + +CMake support: +-------------- + +The minimum required version of CMake is now 3.4. Several details of the CMake +support have been deprecated; warnings will be shown if you need to change +something. The changes are: + +* ``PYBIND11_CPP_STANDARD=`` is deprecated, please use + ``CMAKE_CXX_STANDARD=`` instead, or any other valid CMake CXX or CUDA + standard selection method, like ``target_compile_features``. + +* If you do not request a standard, pybind11 targets will compile with the + compiler default, but not less than C++11, instead of forcing C++14 always. + If you depend on the old behavior, please use ``set(CMAKE_CXX_STANDARD 14 CACHE STRING "")`` + instead. + +* Direct ``pybind11::module`` usage should always be accompanied by at least + ``set(CMAKE_CXX_VISIBILITY_PRESET hidden)`` or similar - it used to try to + manually force this compiler flag (but not correctly on all compilers or with + CUDA). + +* ``pybind11_add_module``'s ``SYSTEM`` argument is deprecated and does nothing; + linking now behaves like other imported libraries consistently in both + config and submodule mode, and behaves like a ``SYSTEM`` library by + default. + +* If ``PYTHON_EXECUTABLE`` is not set, virtual environments (``venv``, + ``virtualenv``, and ``conda``) are prioritized over the standard search + (similar to the new FindPython mode). + +In addition, the following changes may be of interest: + +* ``CMAKE_INTERPROCEDURAL_OPTIMIZATION`` will be respected by + ``pybind11_add_module`` if set instead of linking to ``pybind11::lto`` or + ``pybind11::thin_lto``. + +* Using ``find_package(Python COMPONENTS Interpreter Development)`` before + pybind11 will cause pybind11 to use the new Python mechanisms instead of its + own custom search, based on a patched version of classic ``FindPythonInterp`` + / ``FindPythonLibs``. In the future, this may become the default. A recent + (3.15+ or 3.18.2+) version of CMake is recommended. + + + +v2.5 +==== + +The Python package now includes the headers as data in the package itself, as +well as in the "headers" wheel slot. ``pybind11 --includes`` and +``pybind11.get_include()`` report the new location, which is always correct +regardless of how pybind11 was installed, making the old ``user=`` argument +meaningless. If you are not using the function to get the location already, you +are encouraged to switch to the package location. + + +v2.2 +==== + +Deprecation of the ``PYBIND11_PLUGIN`` macro +-------------------------------------------- + +``PYBIND11_MODULE`` is now the preferred way to create module entry points. +The old macro emits a compile-time deprecation warning. + +.. code-block:: cpp + + // old + PYBIND11_PLUGIN(example) { + py::module m("example", "documentation string"); + + m.def("add", [](int a, int b) { return a + b; }); + + return m.ptr(); + } + + // new + PYBIND11_MODULE(example, m) { + m.doc() = "documentation string"; // optional + + m.def("add", [](int a, int b) { return a + b; }); + } + + +New API for defining custom constructors and pickling functions +--------------------------------------------------------------- + +The old placement-new custom constructors have been deprecated. The new approach +uses ``py::init()`` and factory functions to greatly improve type safety. + +Placement-new can be called accidentally with an incompatible type (without any +compiler errors or warnings), or it can initialize the same object multiple times +if not careful with the Python-side ``__init__`` calls. The new-style custom +constructors prevent such mistakes. See :ref:`custom_constructors` for details. + +.. code-block:: cpp + + // old -- deprecated (runtime warning shown only in debug mode) + py::class(m, "Foo") + .def("__init__", [](Foo &self, ...) { + new (&self) Foo(...); // uses placement-new + }); + + // new + py::class(m, "Foo") + .def(py::init([](...) { // Note: no `self` argument + return new Foo(...); // return by raw pointer + // or: return std::make_unique(...); // return by holder + // or: return Foo(...); // return by value (move constructor) + })); + +Mirroring the custom constructor changes, ``py::pickle()`` is now the preferred +way to get and set object state. See :ref:`pickling` for details. + +.. code-block:: cpp + + // old -- deprecated (runtime warning shown only in debug mode) + py::class(m, "Foo") + ... + .def("__getstate__", [](const Foo &self) { + return py::make_tuple(self.value1(), self.value2(), ...); + }) + .def("__setstate__", [](Foo &self, py::tuple t) { + new (&self) Foo(t[0].cast(), ...); + }); + + // new + py::class(m, "Foo") + ... + .def(py::pickle( + [](const Foo &self) { // __getstate__ + return py::make_tuple(self.value1(), self.value2(), ...); // unchanged + }, + [](py::tuple t) { // __setstate__, note: no `self` argument + return new Foo(t[0].cast(), ...); + // or: return std::make_unique(...); // return by holder + // or: return Foo(...); // return by value (move constructor) + } + )); + +For both the constructors and pickling, warnings are shown at module +initialization time (on import, not when the functions are called). +They're only visible when compiled in debug mode. Sample warning: + +.. code-block:: none + + pybind11-bound class 'mymodule.Foo' is using an old-style placement-new '__init__' + which has been deprecated. See the upgrade guide in pybind11's docs. + + +Stricter enforcement of hidden symbol visibility for pybind11 modules +--------------------------------------------------------------------- + +pybind11 now tries to actively enforce hidden symbol visibility for modules. +If you're using either one of pybind11's :doc:`CMake or Python build systems +` (the two example repositories) and you haven't been exporting any +symbols, there's nothing to be concerned about. All the changes have been done +transparently in the background. If you were building manually or relied on +specific default visibility, read on. + +Setting default symbol visibility to *hidden* has always been recommended for +pybind11 (see :ref:`faq:symhidden`). On Linux and macOS, hidden symbol +visibility (in conjunction with the ``strip`` utility) yields much smaller +module binaries. `CPython's extension docs`_ also recommend hiding symbols +by default, with the goal of avoiding symbol name clashes between modules. +Starting with v2.2, pybind11 enforces this more strictly: (1) by declaring +all symbols inside the ``pybind11`` namespace as hidden and (2) by including +the ``-fvisibility=hidden`` flag on Linux and macOS (only for extension +modules, not for embedding the interpreter). + +.. _CPython's extension docs: https://docs.python.org/3/extending/extending.html#providing-a-c-api-for-an-extension-module + +The namespace-scope hidden visibility is done automatically in pybind11's +headers and it's generally transparent to users. It ensures that: + +* Modules compiled with different pybind11 versions don't clash with each other. + +* Some new features, like ``py::module_local`` bindings, can work as intended. + +The ``-fvisibility=hidden`` flag applies the same visibility to user bindings +outside of the ``pybind11`` namespace. It's now set automatic by pybind11's +CMake and Python build systems, but this needs to be done manually by users +of other build systems. Adding this flag: + +* Minimizes the chances of symbol conflicts between modules. E.g. if two + unrelated modules were statically linked to different (ABI-incompatible) + versions of the same third-party library, a symbol clash would be likely + (and would end with unpredictable results). + +* Produces smaller binaries on Linux and macOS, as pointed out previously. + +Within pybind11's CMake build system, ``pybind11_add_module`` has always been +setting the ``-fvisibility=hidden`` flag in release mode. From now on, it's +being applied unconditionally, even in debug mode and it can no longer be opted +out of with the ``NO_EXTRAS`` option. The ``pybind11::module`` target now also +adds this flag to its interface. The ``pybind11::embed`` target is unchanged. + +The most significant change here is for the ``pybind11::module`` target. If you +were previously relying on default visibility, i.e. if your Python module was +doubling as a shared library with dependents, you'll need to either export +symbols manually (recommended for cross-platform libraries) or factor out the +shared library (and have the Python module link to it like the other +dependents). As a temporary workaround, you can also restore default visibility +using the CMake code below, but this is not recommended in the long run: + +.. code-block:: cmake + + target_link_libraries(mymodule PRIVATE pybind11::module) + + add_library(restore_default_visibility INTERFACE) + target_compile_options(restore_default_visibility INTERFACE -fvisibility=default) + target_link_libraries(mymodule PRIVATE restore_default_visibility) + + +Local STL container bindings +---------------------------- + +Previous pybind11 versions could only bind types globally -- all pybind11 +modules, even unrelated ones, would have access to the same exported types. +However, this would also result in a conflict if two modules exported the +same C++ type, which is especially problematic for very common types, e.g. +``std::vector``. :ref:`module_local` were added to resolve this (see +that section for a complete usage guide). + +``py::class_`` still defaults to global bindings (because these types are +usually unique across modules), however in order to avoid clashes of opaque +types, ``py::bind_vector`` and ``py::bind_map`` will now bind STL containers +as ``py::module_local`` if their elements are: builtins (``int``, ``float``, +etc.), not bound using ``py::class_``, or bound as ``py::module_local``. For +example, this change allows multiple modules to bind ``std::vector`` +without causing conflicts. See :ref:`stl_bind` for more details. + +When upgrading to this version, if you have multiple modules which depend on +a single global binding of an STL container, note that all modules can still +accept foreign ``py::module_local`` types in the direction of Python-to-C++. +The locality only affects the C++-to-Python direction. If this is needed in +multiple modules, you'll need to either: + +* Add a copy of the same STL binding to all of the modules which need it. + +* Restore the global status of that single binding by marking it + ``py::module_local(false)``. + +The latter is an easy workaround, but in the long run it would be best to +localize all common type bindings in order to avoid conflicts with +third-party modules. + + +Negative strides for Python buffer objects and numpy arrays +----------------------------------------------------------- + +Support for negative strides required changing the integer type from unsigned +to signed in the interfaces of ``py::buffer_info`` and ``py::array``. If you +have compiler warnings enabled, you may notice some new conversion warnings +after upgrading. These can be resolved using ``static_cast``. + + +Deprecation of some ``py::object`` APIs +--------------------------------------- + +To compare ``py::object`` instances by pointer, you should now use +``obj1.is(obj2)`` which is equivalent to ``obj1 is obj2`` in Python. +Previously, pybind11 used ``operator==`` for this (``obj1 == obj2``), but +that could be confusing and is now deprecated (so that it can eventually +be replaced with proper rich object comparison in a future release). + +For classes which inherit from ``py::object``, ``borrowed`` and ``stolen`` +were previously available as protected constructor tags. Now the types +should be used directly instead: ``borrowed_t{}`` and ``stolen_t{}`` +(`#771 `_). + + +Stricter compile-time error checking +------------------------------------ + +Some error checks have been moved from run time to compile time. Notably, +automatic conversion of ``std::shared_ptr`` is not possible when ``T`` is +not directly registered with ``py::class_`` (e.g. ``std::shared_ptr`` +or ``std::shared_ptr>`` are not automatically convertible). +Attempting to bind a function with such arguments now results in a compile-time +error instead of waiting to fail at run time. + +``py::init<...>()`` constructor definitions are also stricter and now prevent +bindings which could cause unexpected behavior: + +.. code-block:: cpp + + struct Example { + Example(int &); + }; + + py::class_(m, "Example") + .def(py::init()); // OK, exact match + // .def(py::init()); // compile-time error, mismatch + +A non-``const`` lvalue reference is not allowed to bind to an rvalue. However, +note that a constructor taking ``const T &`` can still be registered using +``py::init()`` because a ``const`` lvalue reference can bind to an rvalue. + +v2.1 +==== + +Minimum compiler versions are enforced at compile time +------------------------------------------------------ + +The minimums also apply to v2.0 but the check is now explicit and a compile-time +error is raised if the compiler does not meet the requirements: + +* GCC >= 4.8 +* clang >= 3.3 (appleclang >= 5.0) +* MSVC >= 2015u3 +* Intel C++ >= 15.0 + + +The ``py::metaclass`` attribute is not required for static properties +--------------------------------------------------------------------- + +Binding classes with static properties is now possible by default. The +zero-parameter version of ``py::metaclass()`` is deprecated. However, a new +one-parameter ``py::metaclass(python_type)`` version was added for rare +cases when a custom metaclass is needed to override pybind11's default. + +.. code-block:: cpp + + // old -- emits a deprecation warning + py::class_(m, "Foo", py::metaclass()) + .def_property_readonly_static("foo", ...); + + // new -- static properties work without the attribute + py::class_(m, "Foo") + .def_property_readonly_static("foo", ...); + + // new -- advanced feature, override pybind11's default metaclass + py::class_(m, "Bar", py::metaclass(custom_python_type)) + ... + + +v2.0 +==== + +Breaking changes in ``py::class_`` +---------------------------------- + +These changes were necessary to make type definitions in pybind11 +future-proof, to support PyPy via its ``cpyext`` mechanism (`#527 +`_), and to improve efficiency +(`rev. 86d825 `_). + +1. Declarations of types that provide access via the buffer protocol must + now include the ``py::buffer_protocol()`` annotation as an argument to + the ``py::class_`` constructor. + + .. code-block:: cpp + + py::class_("Matrix", py::buffer_protocol()) + .def(py::init<...>()) + .def_buffer(...); + +2. Classes which include static properties (e.g. ``def_readwrite_static()``) + must now include the ``py::metaclass()`` attribute. Note: this requirement + has since been removed in v2.1. If you're upgrading from 1.x, it's + recommended to skip directly to v2.1 or newer. + +3. This version of pybind11 uses a redesigned mechanism for instantiating + trampoline classes that are used to override virtual methods from within + Python. This led to the following user-visible syntax change: + + .. code-block:: cpp + + // old v1.x syntax + py::class_("MyClass") + .alias() + ... + + // new v2.x syntax + py::class_("MyClass") + ... + + Importantly, both the original and the trampoline class are now specified + as arguments to the ``py::class_`` template, and the ``alias<..>()`` call + is gone. The new scheme has zero overhead in cases when Python doesn't + override any functions of the underlying C++ class. + `rev. 86d825 `_. + + The class type must be the first template argument given to ``py::class_`` + while the trampoline can be mixed in arbitrary order with other arguments + (see the following section). + + +Deprecation of the ``py::base()`` attribute +---------------------------------------------- + +``py::base()`` was deprecated in favor of specifying ``T`` as a template +argument to ``py::class_``. This new syntax also supports multiple inheritance. +Note that, while the type being exported must be the first argument in the +``py::class_`` template, the order of the following types (bases, +holder and/or trampoline) is not important. + +.. code-block:: cpp + + // old v1.x + py::class_("Derived", py::base()); + + // new v2.x + py::class_("Derived"); + + // new -- multiple inheritance + py::class_("Derived"); + + // new -- apart from `Derived` the argument order can be arbitrary + py::class_("Derived"); + + +Out-of-the-box support for ``std::shared_ptr`` +---------------------------------------------- + +The relevant type caster is now built in, so it's no longer necessary to +include a declaration of the form: + +.. code-block:: cpp + + PYBIND11_DECLARE_HOLDER_TYPE(T, std::shared_ptr) + +Continuing to do so won't cause an error or even a deprecation warning, +but it's completely redundant. + + +Deprecation of a few ``py::object`` APIs +---------------------------------------- + +All of the old-style calls emit deprecation warnings. + ++---------------------------------------+---------------------------------------------+ +| Old syntax | New syntax | ++=======================================+=============================================+ +| ``obj.call(args...)`` | ``obj(args...)`` | ++---------------------------------------+---------------------------------------------+ +| ``obj.str()`` | ``py::str(obj)`` | ++---------------------------------------+---------------------------------------------+ +| ``auto l = py::list(obj); l.check()`` | ``py::isinstance(obj)`` | ++---------------------------------------+---------------------------------------------+ +| ``py::object(ptr, true)`` | ``py::reinterpret_borrow(ptr)`` | ++---------------------------------------+---------------------------------------------+ +| ``py::object(ptr, false)`` | ``py::reinterpret_steal(ptr)`` | ++---------------------------------------+---------------------------------------------+ +| ``if (obj.attr("foo"))`` | ``if (py::hasattr(obj, "foo"))`` | ++---------------------------------------+---------------------------------------------+ +| ``if (obj["bar"])`` | ``if (obj.contains("bar"))`` | ++---------------------------------------+---------------------------------------------+ diff --git a/external_libraries/pybind11/include/pybind11/attr.h b/external_libraries/pybind11/include/pybind11/attr.h index 1044db94..d337595a 100644 --- a/external_libraries/pybind11/include/pybind11/attr.h +++ b/external_libraries/pybind11/include/pybind11/attr.h @@ -12,6 +12,7 @@ #include "detail/common.h" #include "cast.h" +#include "trampoline_self_life_support.h" #include @@ -81,6 +82,10 @@ struct dynamic_attr {}; /// Annotation which enables the buffer protocol for a type struct buffer_protocol {}; +/// Annotation which enables releasing the GIL before calling the C++ destructor of wrapped +/// instances (pybind/pybind11#1446). +struct release_gil_before_calling_cpp_dtor {}; + /// Annotation which requests that a special metaclass is created for a type struct metaclass { handle value; @@ -188,6 +193,7 @@ struct argument_record { /// Internal data structure which holds metadata about a bound function (signature, overloads, /// etc.) +#define PYBIND11_DETAIL_FUNCTION_RECORD_ABI_ID "v1" // PLEASE UPDATE if the struct is changed. struct function_record { function_record() : is_constructor(false), is_new_style_constructor(false), is_stateless(false), @@ -267,12 +273,18 @@ struct function_record { /// Pointer to next overload function_record *next = nullptr; }; +// The main purpose of this macro is to make it easy to pin-point the critically related code +// sections. +#define PYBIND11_ENSURE_PRECONDITION_FOR_FUNCTIONAL_H_PERFORMANCE_OPTIMIZATIONS(...) \ + static_assert( \ + __VA_ARGS__, \ + "Violation of precondition for pybind11/functional.h performance optimizations!") /// Special data structure which (temporarily) holds metadata about a bound class struct type_record { PYBIND11_NOINLINE type_record() : multiple_inheritance(false), dynamic_attr(false), buffer_protocol(false), - default_holder(true), module_local(false), is_final(false) {} + module_local(false), is_final(false), release_gil_before_calling_cpp_dtor(false) {} /// Handle to the parent scope handle scope; @@ -301,6 +313,12 @@ struct type_record { /// Function pointer to class_<..>::dealloc void (*dealloc)(detail::value_and_holder &) = nullptr; + /// Function pointer for casting alias class (aka trampoline) pointer to + /// trampoline_self_life_support pointer. Sidesteps cross-DSO RTTI issues + /// on platforms like macOS (see PR #5728 for details). + get_trampoline_self_life_support_fn get_trampoline_self_life_support + = [](void *) -> trampoline_self_life_support * { return nullptr; }; + /// List of base classes of the newly created type list bases; @@ -322,15 +340,17 @@ struct type_record { /// Does the class implement the buffer protocol? bool buffer_protocol : 1; - /// Is the default (unique_ptr) holder type used? - bool default_holder : 1; - /// Is the class definition local to the module shared object? bool module_local : 1; /// Is the class inheritable from python classes? bool is_final : 1; + /// Solves pybind/pybind11#1446 + bool release_gil_before_calling_cpp_dtor : 1; + + holder_enum_t holder_enum_v = holder_enum_t::undefined; + PYBIND11_NOINLINE void add_base(const std::type_info &base, void *(*caster)(void *) ) { auto *base_info = detail::get_type_info(base, false); if (!base_info) { @@ -340,21 +360,25 @@ struct type_record { + "\" referenced unknown base type \"" + tname + "\""); } - if (default_holder != base_info->default_holder) { + // SMART_HOLDER_BAKEIN_FOLLOW_ON: Refine holder compatibility checks. + bool this_has_unique_ptr_holder = (holder_enum_v == holder_enum_t::std_unique_ptr); + bool base_has_unique_ptr_holder + = (base_info->holder_enum_v == holder_enum_t::std_unique_ptr); + if (this_has_unique_ptr_holder != base_has_unique_ptr_holder) { std::string tname(base.name()); detail::clean_type_id(tname); pybind11_fail("generic_type: type \"" + std::string(name) + "\" " - + (default_holder ? "does not have" : "has") + + (this_has_unique_ptr_holder ? "does not have" : "has") + " a non-default holder type while its base \"" + tname + "\" " - + (base_info->default_holder ? "does not" : "does")); + + (base_has_unique_ptr_holder ? "does not" : "does")); } - bases.append((PyObject *) base_info->type); + bases.append(reinterpret_cast(base_info->type)); -#if PY_VERSION_HEX < 0x030B0000 +#ifdef PYBIND11_BACKWARD_COMPATIBILITY_TP_DICTOFFSET dynamic_attr |= base_info->type->tp_dictoffset != 0; #else - dynamic_attr |= (base_info->type->tp_flags & Py_TPFLAGS_MANAGED_DICT) != 0; + dynamic_attr |= (PyType_GetFlags(base_info->type) & Py_TPFLAGS_MANAGED_DICT) != 0; #endif if (caster) { @@ -603,6 +627,14 @@ struct process_attribute : process_attribute_default static void init(const module_local &l, type_record *r) { r->module_local = l.value; } }; +template <> +struct process_attribute + : process_attribute_default { + static void init(const release_gil_before_calling_cpp_dtor &, type_record *r) { + r->release_gil_before_calling_cpp_dtor = true; + } +}; + /// Process a 'prepend' attribute, putting this at the beginning of the overload chain template <> struct process_attribute : process_attribute_default { @@ -670,6 +702,12 @@ struct process_attributes { } }; +template +struct is_keep_alive : std::false_type {}; + +template +struct is_keep_alive> : std::true_type {}; + template using is_call_guard = is_instantiation; @@ -683,7 +721,9 @@ template ::value...)> constexpr bool expected_num_args(size_t nargs, bool has_args, bool has_kwargs) { PYBIND11_WORKAROUND_INCORRECT_MSVC_C4100(nargs, has_args, has_kwargs); - return named == 0 || (self + named + size_t(has_args) + size_t(has_kwargs)) == nargs; + return named == 0 + || (self + named + static_cast(has_args) + static_cast(has_kwargs)) + == nargs; } PYBIND11_NAMESPACE_END(detail) diff --git a/external_libraries/pybind11/include/pybind11/buffer_info.h b/external_libraries/pybind11/include/pybind11/buffer_info.h index b99ee8be..10fa825a 100644 --- a/external_libraries/pybind11/include/pybind11/buffer_info.h +++ b/external_libraries/pybind11/include/pybind11/buffer_info.h @@ -66,10 +66,11 @@ struct buffer_info { bool readonly = false) : ptr(ptr), itemsize(itemsize), size(1), format(format), ndim(ndim), shape(std::move(shape_in)), strides(std::move(strides_in)), readonly(readonly) { - if (ndim != (ssize_t) shape.size() || ndim != (ssize_t) strides.size()) { + if (ndim != static_cast(shape.size()) + || ndim != static_cast(strides.size())) { pybind11_fail("buffer_info: ndim doesn't match shape and/or strides length"); } - for (size_t i = 0; i < (size_t) ndim; ++i) { + for (size_t i = 0; i < static_cast(ndim); ++i) { size *= shape[i]; } } @@ -102,22 +103,22 @@ struct buffer_info { template buffer_info(const T *ptr, ssize_t size, bool readonly = true) : buffer_info( - const_cast(ptr), sizeof(T), format_descriptor::format(), size, readonly) {} + const_cast(ptr), sizeof(T), format_descriptor::format(), size, readonly) {} explicit buffer_info(Py_buffer *view, bool ownview = true) : buffer_info( - view->buf, - view->itemsize, - view->format, - view->ndim, - {view->shape, view->shape + view->ndim}, - /* Though buffer::request() requests PyBUF_STRIDES, ctypes objects - * ignore this flag and return a view with NULL strides. - * When strides are NULL, build them manually. */ - view->strides - ? std::vector(view->strides, view->strides + view->ndim) - : detail::c_strides({view->shape, view->shape + view->ndim}, view->itemsize), - (view->readonly != 0)) { + view->buf, + view->itemsize, + view->format, + view->ndim, + {view->shape, view->shape + view->ndim}, + /* Though buffer::request() requests PyBUF_STRIDES, ctypes objects + * ignore this flag and return a view with NULL strides. + * When strides are NULL, build them manually. */ + view->strides + ? std::vector(view->strides, view->strides + view->ndim) + : detail::c_strides({view->shape, view->shape + view->ndim}, view->itemsize), + (view->readonly != 0)) { // NOLINTNEXTLINE(cppcoreguidelines-prefer-member-initializer) this->m_view = view; // NOLINTNEXTLINE(cppcoreguidelines-prefer-member-initializer) @@ -176,7 +177,7 @@ struct buffer_info { detail::any_container &&strides_in, bool readonly) : buffer_info( - ptr, itemsize, format, ndim, std::move(shape_in), std::move(strides_in), readonly) {} + ptr, itemsize, format, ndim, std::move(shape_in), std::move(strides_in), readonly) {} Py_buffer *m_view = nullptr; bool ownview = false; @@ -195,7 +196,7 @@ struct compare_buffer_info { template struct compare_buffer_info::value>> { static bool compare(const buffer_info &b) { - return (size_t) b.itemsize == sizeof(T) + return static_cast(b.itemsize) == sizeof(T) && (b.format == format_descriptor::value || ((sizeof(T) == sizeof(long)) && b.format == (std::is_unsigned::value ? "L" : "l")) diff --git a/external_libraries/pybind11/include/pybind11/cast.h b/external_libraries/pybind11/include/pybind11/cast.h index db393411..9ebabceb 100644 --- a/external_libraries/pybind11/include/pybind11/cast.h +++ b/external_libraries/pybind11/include/pybind11/cast.h @@ -10,8 +10,11 @@ #pragma once +#include "detail/argument_vector.h" #include "detail/common.h" #include "detail/descr.h" +#include "detail/holder_caster_foreign_helpers.h" +#include "detail/native_enum_data.h" #include "detail/type_caster_base.h" #include "detail/typeid.h" #include "pytypes.h" @@ -42,13 +45,118 @@ using make_caster = type_caster>; // Shortcut for calling a caster's `cast_op_type` cast operator for casting a type_caster to a T template typename make_caster::template cast_op_type cast_op(make_caster &caster) { - return caster.operator typename make_caster::template cast_op_type(); + using result_t = typename make_caster::template cast_op_type; // See PR #4893 + return caster.operator result_t(); } template typename make_caster::template cast_op_type::type> cast_op(make_caster &&caster) { - return std::move(caster).operator typename make_caster:: - template cast_op_type::type>(); + using result_t = typename make_caster::template cast_op_type< + typename std::add_rvalue_reference::type>; // See PR #4893 + return std::move(caster).operator result_t(); +} + +template +class type_caster_enum_type { +private: + using Underlying = typename std::underlying_type::type; + +public: + static constexpr auto name = const_name(); + + template + static handle cast(SrcType &&src, return_value_policy, handle parent) { + handle native_enum + = global_internals_native_enum_type_map_get_item(std::type_index(typeid(EnumType))); + if (native_enum) { + return native_enum(static_cast(src)).release(); + } + return type_caster_base::cast( + std::forward(src), + // Fixes https://github.com/pybind/pybind11/pull/3643#issuecomment-1022987818: + return_value_policy::copy, + parent); + } + + template + static handle cast(SrcType *src, return_value_policy policy, handle parent) { + return cast(*src, policy, parent); + } + + bool load(handle src, bool convert) { + handle native_enum + = global_internals_native_enum_type_map_get_item(std::type_index(typeid(EnumType))); + if (native_enum) { + if (!isinstance(src, native_enum)) { + return false; + } + type_caster underlying_caster; + if (!underlying_caster.load(src.attr("value"), convert)) { + pybind11_fail("native_enum internal consistency failure."); + } + native_value = static_cast(static_cast(underlying_caster)); + native_loaded = true; + return true; + } + + type_caster_base legacy_caster; + if (legacy_caster.load(src, convert)) { + legacy_ptr = static_cast(legacy_caster); + return true; + } + return false; + } + + template + using cast_op_type = detail::cast_op_type; + + // NOLINTNEXTLINE(google-explicit-constructor) + operator EnumType *() { return native_loaded ? &native_value : legacy_ptr; } + + // NOLINTNEXTLINE(google-explicit-constructor) + operator EnumType &() { + if (!native_loaded && !legacy_ptr) { + throw reference_cast_error(); + } + return native_loaded ? native_value : *legacy_ptr; + } + +private: + EnumType native_value; // if loading a py::native_enum + bool native_loaded = false; + EnumType *legacy_ptr = nullptr; // if loading a py::enum_ +}; + +template +struct type_caster_enum_type_enabled : std::true_type {}; + +template +struct type_uses_type_caster_enum_type { + static constexpr bool value + = std::is_enum::value && type_caster_enum_type_enabled::value; +}; + +template +class type_caster::value>> + : public type_caster_enum_type {}; + +template ::value, int> = 0> +bool isinstance_native_enum_impl(handle obj, const std::type_info &tp) { + handle native_enum = global_internals_native_enum_type_map_get_item(tp); + if (!native_enum) { + return false; + } + return isinstance(obj, native_enum); +} + +template ::value, int> = 0> +bool isinstance_native_enum_impl(handle, const std::type_info &) { + return false; +} + +template +bool isinstance_native_enum(handle obj, const std::type_info &tp) { + return isinstance_native_enum_impl>(obj, tp); } template @@ -91,8 +199,7 @@ public: template >::value, \ - int> \ - = 0> \ + int> = 0> \ static ::pybind11::handle cast( \ T_ *src, ::pybind11::return_value_policy policy, ::pybind11::handle parent) { \ if (!src) \ @@ -156,7 +263,7 @@ struct type_caster::value && !is_std_char_t } else { handle src_or_index = src; // PyPy: 7.3.7's 3.8 does not implement PyLong_*'s __index__ calls. -#if PY_VERSION_HEX < 0x03080000 || defined(PYPY_VERSION) +#if defined(PYPY_VERSION) object index; if (!PYBIND11_LONG_CHECK(src.ptr())) { // So: index_check(src.ptr()) index = reinterpret_steal(PyNumber_Index(src.ptr())); @@ -239,7 +346,12 @@ struct type_caster::value && !is_std_char_t return PyLong_FromUnsignedLongLong((unsigned long long) src); } - PYBIND11_TYPE_CASTER(T, const_name::value>("int", "float")); + PYBIND11_TYPE_CASTER( + T, + io_name::value>("typing.SupportsInt | typing.SupportsIndex", + "int", + "typing.SupportsFloat | typing.SupportsIndex", + "float")); }; template @@ -281,7 +393,8 @@ class type_caster : public type_caster { } /* Check if this is a C++ type */ - const auto &bases = all_type_info((PyTypeObject *) type::handle_of(h).ptr()); + const auto &bases + = all_type_info(reinterpret_cast(type::handle_of(h).ptr())); if (bases.size() == 1) { // Only allowing loading from a single-value type value = values_and_holders(reinterpret_cast(h.ptr())).begin()->value_ptr(); return true; @@ -301,7 +414,7 @@ class type_caster : public type_caster { template using cast_op_type = void *&; explicit operator void *&() { return value; } - static constexpr auto name = const_name("capsule"); + static constexpr auto name = const_name(PYBIND11_CAPSULE_TYPE_TYPE_HINT); private: void *value = nullptr; @@ -325,8 +438,9 @@ class type_caster { value = false; return true; } - if (convert || (std::strcmp("numpy.bool_", Py_TYPE(src.ptr())->tp_name) == 0)) { - // (allow non-implicit conversion for numpy booleans) + if (convert || is_numpy_bool(src)) { + // (allow non-implicit conversion for numpy booleans), use strncmp + // since NumPy 1.x had an additional trailing underscore. Py_ssize_t res = -1; if (src.is_none()) { @@ -340,7 +454,7 @@ class type_caster { #else // Alternate approach for CPython: this does the same as the above, but optimized // using the CPython API so as to avoid an unneeded attribute lookup. - else if (auto *tp_as_number = src.ptr()->ob_type->tp_as_number) { + else if (auto *tp_as_number = Py_TYPE(src.ptr())->tp_as_number) { if (PYBIND11_NB_BOOL(tp_as_number)) { res = (*PYBIND11_NB_BOOL(tp_as_number))(src.ptr()); } @@ -358,6 +472,15 @@ class type_caster { return handle(src ? Py_True : Py_False).inc_ref(); } PYBIND11_TYPE_CASTER(bool, const_name("bool")); + +private: + // Test if an object is a NumPy boolean (without fetching the type). + static bool is_numpy_bool(handle object) { + const char *type_name = Py_TYPE(object.ptr())->tp_name; + // Name changed to `numpy.bool` in NumPy 2, `numpy.bool_` is needed for 1.x support + return std::strcmp("numpy.bool", type_name) == 0 + || std::strcmp("numpy.bool_", type_name) == 0; + } }; // Helper class for UTF-{8,16,32} C++ stl strings: @@ -418,7 +541,7 @@ struct string_caster { const auto *buffer = reinterpret_cast(PYBIND11_BYTES_AS_STRING(utfNbytes.ptr())); - size_t length = (size_t) PYBIND11_BYTES_SIZE(utfNbytes.ptr()) / sizeof(CharT); + size_t length = static_cast(PYBIND11_BYTES_SIZE(utfNbytes.ptr())) / sizeof(CharT); // Skip BOM for UTF-16/32 if (UTF_N > 8) { buffer++; @@ -660,8 +783,9 @@ class tuple_caster { return cast(*src, policy, parent); } - static constexpr auto name - = const_name("Tuple[") + concat(make_caster::name...) + const_name("]"); + static constexpr auto name = const_name("tuple[") + + ::pybind11::detail::concat(make_caster::name...) + + const_name("]"); template using cast_op_type = type; @@ -703,7 +827,9 @@ class tuple_caster { 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( + // NOLINTNEXTLINE(bugprone-use-after-move) make_caster::cast(std::get(std::forward(src)), policy, parent))...}}; for (const auto &entry : entries) { if (!entry) { @@ -727,6 +853,13 @@ class type_caster> : public tuple_caster {} template class type_caster> : public tuple_caster {}; +template <> +class type_caster> : public tuple_caster { +public: + // PEP 484 specifies this syntax for an empty tuple + static constexpr auto name = const_name("tuple[()]"); +}; + /// Helper class which abstracts away certain actions. Users can provide specializations for /// custom holders, but it's only necessary if the type has a non-standard interface. template @@ -734,6 +867,7 @@ struct holder_helper { static auto get(const T &p) -> decltype(p.get()) { return p.get(); } }; +// SMART_HOLDER_BAKEIN_FOLLOW_ON: Rewrite comment, with reference to shared_ptr specialization. /// Type caster for holder types like std::shared_ptr, etc. /// The SFINAE hook is provided to help work around the current lack of support /// for smart-pointer interoperability. Please consider it an implementation @@ -769,16 +903,23 @@ struct copyable_holder_caster : public type_caster_base { protected: friend class type_caster_generic; void check_holder_compat() { - if (typeinfo->default_holder) { + // SMART_HOLDER_BAKEIN_FOLLOW_ON: Refine holder compatibility checks. + bool inst_has_unique_ptr_holder + = (typeinfo->holder_enum_v == holder_enum_t::std_unique_ptr); + if (inst_has_unique_ptr_holder) { throw cast_error("Unable to load a custom holder type from a default-holder instance"); } } - bool load_value(value_and_holder &&v_h) { + bool set_foreign_holder(handle src) { + return holder_caster_foreign_helpers::set_foreign_holder(src, (type *) value, &holder); + } + + void load_value(value_and_holder &&v_h) { if (v_h.holder_constructed()) { value = v_h.value_ptr(); holder = v_h.template holder(); - return true; + return; } throw cast_error("Unable to cast from non-held to held instance (T& to Holder) " #if !defined(PYBIND11_DETAILED_ERROR_MESSAGES) @@ -815,10 +956,220 @@ struct copyable_holder_caster : public type_caster_base { holder_type holder; }; +template +struct copyable_holder_caster_shared_ptr_with_smart_holder_support_enabled : std::true_type {}; + +// SMART_HOLDER_BAKEIN_FOLLOW_ON: Refactor copyable_holder_caster to reduce code duplication. +template +struct copyable_holder_caster< + type, + std::shared_ptr, + enable_if_t::value>> + : public type_caster_base { +public: + using base = type_caster_base; + static_assert(std::is_base_of>::value, + "Holder classes are only supported for custom types"); + using base::base; + using base::cast; + using base::typeinfo; + using base::value; + + bool load(handle src, bool convert) { + if (base::template load_impl>>( + src, convert)) { + sh_load_helper.maybe_set_python_instance_is_alias(src); + return true; + } + return false; + } + + explicit operator std::shared_ptr *() { + if (sh_load_helper.was_populated) { + pybind11_fail("Passing `std::shared_ptr *` from Python to C++ is not supported " + "(inherently unsafe)."); + } + return std::addressof(shared_ptr_storage); + } + + explicit operator std::shared_ptr &() { + if (sh_load_helper.was_populated) { + shared_ptr_storage = sh_load_helper.load_as_shared_ptr(typeinfo, value); + } + return shared_ptr_storage; + } + + std::weak_ptr potentially_slicing_weak_ptr() { + if (sh_load_helper.was_populated) { + // Reusing shared_ptr code to minimize code complexity. + shared_ptr_storage + = sh_load_helper.load_as_shared_ptr(typeinfo, + value, + /*responsible_parent=*/nullptr, + /*force_potentially_slicing_shared_ptr=*/true); + } + return shared_ptr_storage; + } + + static handle + cast(const std::shared_ptr &src, return_value_policy policy, handle parent) { + const auto *ptr = src.get(); + typename type_caster_base::cast_sources srcs{ptr}; + if (srcs.creates_smart_holder()) { + return smart_holder_type_caster_support::smart_holder_from_shared_ptr( + src, policy, parent, srcs.result); + } + + auto *tinfo = srcs.result.tinfo; + if (tinfo != nullptr && tinfo->holder_enum_v == holder_enum_t::std_shared_ptr) { + return type_caster_base::cast_holder(srcs, &src); + } + + if (parent) { + return type_caster_generic::cast_non_owning( + srcs, return_value_policy::reference_internal, parent); + } + + throw cast_error("Unable to convert std::shared_ptr to Python when the bound type " + "does not use std::shared_ptr or py::smart_holder as its holder type"); + } + + // This function will succeed even if the `responsible_parent` does not own the + // wrapped C++ object directly. + // It is the responsibility of the caller to ensure that the `responsible_parent` + // has a `keep_alive` relationship with the owner of the wrapped C++ object, or + // that the wrapped C++ object lives for the duration of the process. + static std::shared_ptr shared_ptr_with_responsible_parent(handle responsible_parent) { + copyable_holder_caster loader; + loader.load(responsible_parent, /*convert=*/false); + assert(loader.typeinfo->holder_enum_v == detail::holder_enum_t::smart_holder); + return loader.sh_load_helper.load_as_shared_ptr( + loader.typeinfo, loader.value, responsible_parent); + } + +protected: + friend class type_caster_generic; + void check_holder_compat() { + // SMART_HOLDER_BAKEIN_FOLLOW_ON: Refine holder compatibility checks. + bool inst_has_unique_ptr_holder + = (typeinfo->holder_enum_v == holder_enum_t::std_unique_ptr); + if (inst_has_unique_ptr_holder) { + throw cast_error("Unable to load a custom holder type from a default-holder instance"); + } + } + + bool set_foreign_holder(handle src) { + return holder_caster_foreign_helpers::set_foreign_holder( + src, (type *) value, &shared_ptr_storage); + } + + void load_value(value_and_holder &&v_h) { + if (typeinfo->holder_enum_v == detail::holder_enum_t::smart_holder) { + sh_load_helper.loaded_v_h = v_h; + sh_load_helper.was_populated = true; + value = sh_load_helper.get_void_ptr_or_nullptr(); + return; + } + if (v_h.holder_constructed()) { + value = v_h.value_ptr(); + shared_ptr_storage = v_h.template holder>(); + return; + } + throw cast_error("Unable to cast from non-held to held instance (T& to Holder) " +#if !defined(PYBIND11_DETAILED_ERROR_MESSAGES) + "(#define PYBIND11_DETAILED_ERROR_MESSAGES or compile in debug mode for " + "type information)"); +#else + "of type '" + + type_id>() + "''"); +#endif + } + + template , + detail::enable_if_t::value, int> = 0> + bool try_implicit_casts(handle, bool) { + return false; + } + + template , + detail::enable_if_t::value, int> = 0> + bool try_implicit_casts(handle src, bool convert) { + for (auto &cast : typeinfo->implicit_casts) { + copyable_holder_caster sub_caster(*cast.first); + if (sub_caster.load(src, convert)) { + value = cast.second(sub_caster.value); + if (typeinfo->holder_enum_v == detail::holder_enum_t::smart_holder) { + sh_load_helper.loaded_v_h = sub_caster.sh_load_helper.loaded_v_h; + sh_load_helper.was_populated = true; + } else { + shared_ptr_storage + = std::shared_ptr(sub_caster.shared_ptr_storage, (type *) value); + } + return true; + } + } + return false; + } + + static bool try_direct_conversions(handle) { return false; } + + smart_holder_type_caster_support::load_helper> sh_load_helper; // Const2Mutbl + std::shared_ptr shared_ptr_storage; +}; + /// Specialize for the common std::shared_ptr, so users don't need to template class type_caster> : public copyable_holder_caster> {}; +PYBIND11_NAMESPACE_END(detail) + +/// Return a std::shared_ptr with the SAME CONTROL BLOCK as the std::shared_ptr owned by the +/// class_ holder. For class_-wrapped types with trampolines, the returned std::shared_ptr +/// does NOT keep any derived Python objects alive (see issue #1333). +/// +/// For class_-wrapped types using std::shared_ptr as the holder, the following expressions +/// produce equivalent results (see tests/test_potentially_slicing_weak_ptr.cpp,py): +/// +/// - obj.cast>() +/// - py::potentially_slicing_weak_ptr(obj).lock() +/// +/// For class_-wrapped types with trampolines and using py::smart_holder, obj.cast<>() +/// produces a std::shared_ptr that keeps any derived Python objects alive for its own lifetime, +/// but this is achieved by introducing a std::shared_ptr control block that is independent of +/// the one owned by the py::smart_holder. This can lead to surprising std::weak_ptr behavior +/// (see issue #5623). An easy solution is to use py::potentially_slicing_weak_ptr<>(obj), +/// as exercised in tests/test_potentially_slicing_weak_ptr.cpp,py (look for +/// "set_wp_potentially_slicing"). Note, however, that this reintroduces the inheritance +/// slicing issue (see issue #1333). The ideal — but usually more involved — solution is to use +/// a Python weakref to the derived Python object, instead of a C++ base-class std::weak_ptr. +/// +/// It is not possible (at least no known approach exists at the time of this writing) to +/// simultaneously achieve both desirable properties: +/// +/// - the same std::shared_ptr control block as the class_ holder +/// - automatic lifetime extension of any derived Python objects +/// +/// The reason is that this would introduce a reference cycle that cannot be garbage collected: +/// +/// - the derived Python object owns the class_ holder +/// - the class_ holder owns the std::shared_ptr +/// - the std::shared_ptr would own a reference to the derived Python object, +/// completing the cycle +template +std::weak_ptr potentially_slicing_weak_ptr(handle obj) { + detail::make_caster> caster; + if (caster.load(obj, /*convert=*/true)) { + return caster.potentially_slicing_weak_ptr(); + } + const char *obj_type_name = detail::obj_class_name(obj.ptr()); + throw type_error("\"" + std::string(obj_type_name) + + "\" object is not convertible to std::weak_ptr (with T = " + type_id() + + ")"); +} + +PYBIND11_NAMESPACE_BEGIN(detail) + +// SMART_HOLDER_BAKEIN_FOLLOW_ON: Rewrite comment, with reference to unique_ptr specialization. /// Type caster for holder types like std::unique_ptr. /// Please consider the SFINAE hook an implementation detail, as explained /// in the comment for the copyable_holder_caster. @@ -834,6 +1185,141 @@ struct move_only_holder_caster { static constexpr auto name = type_caster_base::name; }; +template +struct move_only_holder_caster_unique_ptr_with_smart_holder_support_enabled : std::true_type {}; + +// SMART_HOLDER_BAKEIN_FOLLOW_ON: Refactor move_only_holder_caster to reduce code duplication. +template +struct move_only_holder_caster< + type, + std::unique_ptr, + enable_if_t::value>> + : public type_caster_base { +public: + using base = type_caster_base; + static_assert(std::is_base_of>::value, + "Holder classes are only supported for custom types"); + using base::base; + using base::cast; + using base::typeinfo; + using base::value; + + static handle + cast(std::unique_ptr &&src, return_value_policy policy, handle parent) { + auto *ptr = src.get(); + typename type_caster_base::cast_sources srcs{ptr}; + if (srcs.creates_smart_holder()) { + return smart_holder_type_caster_support::smart_holder_from_unique_ptr( + std::move(src), policy, parent, srcs.result); + } + return type_caster_base::cast_holder(srcs, &src); + } + + static handle + cast(const std::unique_ptr &src, return_value_policy policy, handle parent) { + if (!src) { + return none().release(); + } + if (policy == return_value_policy::automatic) { + policy = return_value_policy::reference_internal; + } + if (policy != return_value_policy::reference_internal) { + throw cast_error("Invalid return_value_policy for const unique_ptr&"); + } + return type_caster_base::cast(src.get(), policy, parent); + } + + bool load(handle src, bool convert) { + if (base::template load_impl< + move_only_holder_caster>>(src, convert)) { + sh_load_helper.maybe_set_python_instance_is_alias(src); + return true; + } + return false; + } + + bool set_foreign_holder(handle) { + throw cast_error("Foreign instance cannot be converted to std::unique_ptr " + "because we don't know how to make it relinquish " + "ownership"); + } + + void load_value(value_and_holder &&v_h) { + if (typeinfo->holder_enum_v == detail::holder_enum_t::smart_holder) { + sh_load_helper.loaded_v_h = v_h; + sh_load_helper.loaded_v_h.type = typeinfo; + sh_load_helper.was_populated = true; + value = sh_load_helper.get_void_ptr_or_nullptr(); + return; + } + pybind11_fail("Passing `std::unique_ptr` from Python to C++ requires `py::class_` (with T = " + + clean_type_id(typeinfo->cpptype->name()) + ")"); + } + + template + using cast_op_type + = conditional_t::type, + const std::unique_ptr &>::value + || std::is_same::type, + const std::unique_ptr &>::value, + const std::unique_ptr &, + std::unique_ptr>; + + explicit operator std::unique_ptr() { + if (typeinfo->holder_enum_v == detail::holder_enum_t::smart_holder) { + return sh_load_helper.template load_as_unique_ptr(typeinfo, value); + } + pybind11_fail("Expected to be UNREACHABLE: " __FILE__ ":" PYBIND11_TOSTRING(__LINE__)); + } + + explicit operator const std::unique_ptr &() { + if (typeinfo->holder_enum_v == detail::holder_enum_t::smart_holder) { + // Get shared_ptr to ensure that the Python object is not disowned elsewhere. + shared_ptr_storage = sh_load_helper.load_as_shared_ptr(typeinfo, value); + // Build a temporary unique_ptr that is meant to never expire. + unique_ptr_storage = std::shared_ptr>( + new std::unique_ptr{ + sh_load_helper.template load_as_const_unique_ptr( + typeinfo, shared_ptr_storage.get())}, + [](std::unique_ptr *ptr) { + if (!ptr) { + pybind11_fail("FATAL: `const std::unique_ptr &` was disowned " + "(EXPECT UNDEFINED BEHAVIOR)."); + } + (void) ptr->release(); + delete ptr; + }); + return *unique_ptr_storage; + } + pybind11_fail("Expected to be UNREACHABLE: " __FILE__ ":" PYBIND11_TOSTRING(__LINE__)); + } + + bool try_implicit_casts(handle src, bool convert) { + for (auto &cast : typeinfo->implicit_casts) { + move_only_holder_caster sub_caster(*cast.first); + if (sub_caster.load(src, convert)) { + value = cast.second(sub_caster.value); + if (typeinfo->holder_enum_v == detail::holder_enum_t::smart_holder) { + sh_load_helper.loaded_v_h = sub_caster.sh_load_helper.loaded_v_h; + sh_load_helper.was_populated = true; + } else { + pybind11_fail("Expected to be UNREACHABLE: " __FILE__ + ":" PYBIND11_TOSTRING(__LINE__)); + } + return true; + } + } + return false; + } + + static bool try_direct_conversions(handle) { return false; } + + smart_holder_type_caster_support::load_helper> sh_load_helper; // Const2Mutbl + std::shared_ptr shared_ptr_storage; // Serves as a pseudo lock. + std::shared_ptr> unique_ptr_storage; +}; + template class type_caster> : public move_only_holder_caster> {}; @@ -843,18 +1329,20 @@ using type_caster_holder = conditional_t::val copyable_holder_caster, move_only_holder_caster>; -template -struct always_construct_holder { +template +struct always_construct_holder_value { static constexpr bool value = Value; }; +template +struct always_construct_holder : always_construct_holder_value {}; + /// Create a specialization for custom holder types (silently ignores std::shared_ptr) #define PYBIND11_DECLARE_HOLDER_TYPE(type, holder_type, ...) \ PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE) \ namespace detail { \ template \ - struct always_construct_holder : always_construct_holder { \ - }; \ + struct always_construct_holder : always_construct_holder_value<__VA_ARGS__> {}; \ template \ class type_caster::value>> \ : public type_caster_holder {}; \ @@ -865,14 +1353,61 @@ struct always_construct_holder { template struct is_holder_type : std::is_base_of, detail::type_caster> {}; -// Specialization for always-supported unique_ptr holders: + +// Specializations for always-supported holders: template struct is_holder_type> : std::true_type {}; +template +struct is_holder_type : std::true_type {}; + +#ifdef PYBIND11_DISABLE_HANDLE_TYPE_NAME_DEFAULT_IMPLEMENTATION // See PR #4888 + +// This leads to compilation errors if a specialization is missing. +template +struct handle_type_name; + +#else + template struct handle_type_name { static constexpr auto name = const_name(); }; + +#endif + +template <> +struct handle_type_name { + static constexpr auto name = const_name("object"); +}; +template <> +struct handle_type_name { + static constexpr auto name = const_name("list"); +}; +template <> +struct handle_type_name { + static constexpr auto name = const_name("dict"); +}; +template <> +struct handle_type_name { + static constexpr auto name = const_name("set | frozenset"); +}; +template <> +struct handle_type_name { + static constexpr auto name = const_name("set"); +}; +template <> +struct handle_type_name { + static constexpr auto name = const_name("frozenset"); +}; +template <> +struct handle_type_name { + static constexpr auto name = const_name("str"); +}; +template <> +struct handle_type_name { + static constexpr auto name = const_name("tuple"); +}; template <> struct handle_type_name { static constexpr auto name = const_name("bool"); @@ -882,32 +1417,111 @@ struct handle_type_name { static constexpr auto name = const_name(PYBIND11_BYTES_NAME); }; template <> +struct handle_type_name { + static constexpr auto name = const_name(PYBIND11_BUFFER_TYPE_HINT); +}; +template <> struct handle_type_name { static constexpr auto name = const_name("int"); }; template <> struct handle_type_name { - static constexpr auto name = const_name("Iterable"); + static constexpr auto name = const_name("collections.abc.Iterable"); }; template <> struct handle_type_name { - static constexpr auto name = const_name("Iterator"); + static constexpr auto name = const_name("collections.abc.Iterator"); }; template <> struct handle_type_name { static constexpr auto name = const_name("float"); }; template <> +struct handle_type_name { + static constexpr auto name = const_name("collections.abc.Callable"); +}; +template <> +struct handle_type_name { + static constexpr auto name = handle_type_name::name; +}; +template <> struct handle_type_name { static constexpr auto name = const_name("None"); }; template <> +struct handle_type_name { + static constexpr auto name = const_name("collections.abc.Sequence"); +}; +template <> +struct handle_type_name { + static constexpr auto name = const_name("bytearray"); +}; +template <> +struct handle_type_name { + static constexpr auto name = const_name("memoryview"); +}; +template <> +struct handle_type_name { + static constexpr auto name = const_name("slice"); +}; +template <> +struct handle_type_name { + static constexpr auto name = const_name("type"); +}; +template <> +struct handle_type_name { + static constexpr auto name = const_name(PYBIND11_CAPSULE_TYPE_TYPE_HINT); +}; +template <> +struct handle_type_name { + static constexpr auto name = const_name("ellipsis"); +}; +template <> +struct handle_type_name { + static constexpr auto name = const_name("weakref.ReferenceType"); +}; +// args/Args/kwargs/KWArgs have name as well as typehint included +template <> struct handle_type_name { - static constexpr auto name = const_name("*args"); + static constexpr auto name = io_name("*args", "tuple"); +}; +template +struct handle_type_name> { + static constexpr auto name + = io_name("*args: ", "tuple[") + make_caster::name + io_name("", ", ...]"); }; template <> struct handle_type_name { - static constexpr auto name = const_name("**kwargs"); + static constexpr auto name = io_name("**kwargs", "dict[str, typing.Any]"); +}; +template +struct handle_type_name> { + static constexpr auto name + = io_name("**kwargs: ", "dict[str, ") + make_caster::name + io_name("", "]"); +}; +template <> +struct handle_type_name { + static constexpr auto name = const_name(); +}; +template <> +struct handle_type_name { + static constexpr auto name = const_name(); +}; +template <> +struct handle_type_name { + static constexpr auto name = const_name(); +}; +template <> +struct handle_type_name { + static constexpr auto name = const_name(); +}; +template <> +struct handle_type_name { + static constexpr auto name = const_name(); +}; +template <> +struct handle_type_name { + static constexpr auto name = const_name(); }; template @@ -944,6 +1558,21 @@ struct pyobject_caster { template class type_caster::value>> : public pyobject_caster {}; +template <> +class type_caster : public pyobject_caster { +public: + bool load(handle src, bool /* convert */) { + if (isinstance(src)) { + value = reinterpret_borrow(src); + } else if (isinstance(src)) { + value = float_(reinterpret_borrow(src)); + } else { + return false; + } + return true; + } +}; + // Our conditions for enabling moving are quite restrictive: // At compile time: // - T needs to be a non-const, non-pointer, non-reference type @@ -1044,12 +1673,20 @@ PYBIND11_NAMESPACE_END(detail) template ::value && !detail::is_same_ignoring_cvref::value, - int> - = 0> + int> = 0> T cast(const handle &handle) { using namespace detail; - static_assert(!cast_is_temporary_value_reference::value, + constexpr bool is_enum_cast = type_uses_type_caster_enum_type>::value; + static_assert(!cast_is_temporary_value_reference::value || is_enum_cast, "Unable to cast type to reference: value is local to type caster"); +#ifndef NDEBUG + if (is_enum_cast && cast_is_temporary_value_reference::value) { + if (detail::global_internals_native_enum_type_map_contains( + std::type_index(typeid(intrinsic_t)))) { + pybind11_fail("Unable to cast native enum type to reference"); + } + } +#endif return cast_op(load_type(handle)); } @@ -1071,8 +1708,7 @@ template ::value && detail::is_same_ignoring_cvref::value, - int> - = 0> + int> = 0> T cast(Handle &&handle) { return handle.inc_ref().ptr(); } @@ -1081,8 +1717,7 @@ template ::value && detail::is_same_ignoring_cvref::value, - int> - = 0> + int> = 0> T cast(Object &&obj) { return obj.release().ptr(); } @@ -1184,12 +1819,42 @@ inline void object::cast() && { PYBIND11_NAMESPACE_BEGIN(detail) +// forward declaration (definition in pybind11.h) +template +std::string generate_type_signature(); + // Declared in pytypes.h: template ::value, int>> object object_or_cast(T &&o) { return pybind11::cast(std::forward(o)); } +// Declared in pytypes.h: +// Implemented here so that make_caster can be used. +template +template +str_attr_accessor object_api::attr_with_type_hint(const char *key) const { +#if !defined(__cpp_inline_variables) + static_assert(always_false::value, + "C++17 feature __cpp_inline_variables not available: " + "https://en.cppreference.com/w/cpp/language/static#Static_data_members"); +#endif + object ann = annotations(); + if (ann.contains(key)) { + throw std::runtime_error("__annotations__[\"" + std::string(key) + "\"] was set already."); + } + + ann[key] = generate_type_signature(); + return {derived(), key}; +} + +template +template +obj_attr_accessor object_api::attr_with_type_hint(handle key) const { + (void) attr_with_type_hint(key.cast().c_str()); + return {derived(), reinterpret_borrow(key)}; +} + // Placeholder type for the unneeded (and dead code) static variable in the // PYBIND11_OVERRIDE_OVERRIDE macro struct override_unused {}; @@ -1215,13 +1880,24 @@ enable_if_t::value, T> cast_ref(object &&, // static_assert, even though if it's in dead code, so we provide a "trampoline" to pybind11::cast // that only does anything in cases where pybind11::cast is valid. template -enable_if_t::value, T> cast_safe(object &&) { +enable_if_t::value + && !detail::is_same_ignoring_cvref::value, + T> +cast_safe(object &&) { pybind11_fail("Internal error: cast_safe fallback invoked"); } template enable_if_t::value, void> cast_safe(object &&) {} template -enable_if_t, std::is_void>::value, T> +enable_if_t::value, PyObject *> +cast_safe(object &&o) { + return o.release().ptr(); +} +template +enable_if_t, + detail::is_same_ignoring_cvref, + std::is_void>::value, + T> cast_safe(object &&o) { return pybind11::cast(std::move(o)); } @@ -1244,13 +1920,20 @@ inline cast_error cast_error_unable_to_convert_call_arg(const std::string &name, } #endif +namespace typing { +template +class Tuple : public tuple { + using tuple::tuple; +}; +} // namespace typing + template -tuple make_tuple() { +typing::Tuple<> make_tuple() { return tuple(0); } template -tuple make_tuple(Args &&...args_) { +typing::Tuple make_tuple(Args &&...args_) { constexpr size_t size = sizeof...(Args); std::array args{{reinterpret_steal( detail::make_caster::cast(std::forward(args_), policy, nullptr))...}}; @@ -1269,7 +1952,12 @@ tuple make_tuple(Args &&...args_) { for (auto &arg_value : args) { PyTuple_SET_ITEM(result.ptr(), counter++, arg_value.release().ptr()); } + PYBIND11_WARNING_PUSH +#ifdef PYBIND11_DETECTED_CLANG_WITH_MISLEADING_CALL_STD_MOVE_EXPLICITLY_WARNING + PYBIND11_WARNING_DISABLE_CLANG("-Wreturn-std-move") +#endif return result; + PYBIND11_WARNING_POP } /// \ingroup annotations @@ -1361,7 +2049,7 @@ struct kw_only {}; /// \ingroup annotations /// Annotation indicating that all previous arguments are positional-only; the is the equivalent of -/// an unnamed '/' argument (in Python 3.8) +/// an unnamed '/' argument struct pos_only {}; template @@ -1377,7 +2065,15 @@ inline namespace literals { /** \rst String literal version of `arg` \endrst */ -constexpr arg operator"" _a(const char *name, size_t) { return arg(name); } +constexpr arg +#if !defined(__clang__) && defined(__GNUC__) && __GNUC__ < 5 +operator"" _a // gcc 4.8.5 insists on having a space (hard error). +#else +operator""_a // clang 17 generates a deprecation warning if there is a space. +#endif + (const char *name, size_t) { + return arg(name); +} } // namespace literals PYBIND11_NAMESPACE_BEGIN(detail) @@ -1390,6 +2086,9 @@ using is_pos_only = std::is_same, pos_only>; // forward declaration (definition in attr.h) struct function_record; +/// Inline size chosen mostly arbitrarily. +constexpr std::size_t arg_vector_small_size = 6; + /// Internal data associated with a single function call struct function_call { function_call(const function_record &f, handle p); // Implementation in attr.h @@ -1398,10 +2097,10 @@ struct function_call { const function_record &func; /// Arguments passed to the function: - std::vector args; + argument_vector args; /// The `convert` value the arguments should be loaded with - std::vector args_convert; + args_convert_vector args_convert; /// Extra references for the optional `py::args` and/or `py::kwargs` arguments (which, if /// present, are also in `args` but without a reference). @@ -1414,15 +2113,24 @@ struct function_call { handle init_self; }; +// See PR #5396 for the discussion that led to this +template +struct is_same_or_base_of : std::is_same {}; + +// Only evaluate is_base_of if Derived is complete. +// is_base_of raises a compiler error if Derived is incomplete. +template +struct is_same_or_base_of + : any_of, std::is_base_of> {}; + /// Helper class which loads arguments for C++ functions called from Python template class argument_loader { using indices = make_index_sequence; - template - using argument_is_args = std::is_same, args>; + using argument_is_args = is_same_or_base_of>; template - using argument_is_kwargs = std::is_same, kwargs>; + using argument_is_kwargs = is_same_or_base_of>; // Get kwargs argument position, or -1 if not present: static constexpr auto kwargs_pos = constexpr_last(); @@ -1438,7 +2146,8 @@ class argument_loader { static_assert(args_pos == -1 || args_pos == constexpr_first(), "py::args cannot be specified more than once"); - static constexpr auto arg_names = concat(type_descr(make_caster::name)...); + static constexpr auto arg_names + = ::pybind11::detail::concat(type_descr(make_caster::name)...); bool load_args(function_call &call) { return load_impl_sequence(call, indices{}); } @@ -1461,6 +2170,11 @@ class argument_loader { template bool load_impl_sequence(function_call &call, index_sequence) { + PYBIND11_WARNING_PUSH +#if !defined(__clang__) && defined(__GNUC__) && __GNUC__ >= 13 + // Work around a GCC -Warray-bounds false positive in argument_vector usage. + PYBIND11_WARNING_DISABLE_GCC("-Warray-bounds") +#endif #ifdef __cpp_fold_expressions if ((... || !std::get(argcasters).load(call.args[Is], call.args_convert[Is]))) { return false; @@ -1472,6 +2186,7 @@ class argument_loader { } } #endif + PYBIND11_WARNING_POP return true; } @@ -1483,86 +2198,126 @@ class argument_loader { std::tuple...> argcasters; }; -/// Helper class which collects only positional arguments for a Python function call. -/// A fancier version below can collect any argument, but this one is optimal for simple calls. +// [workaround(intel)] Separate function required here +// We need to put this into a separate function because the Intel compiler +// fails to compile enable_if_t...>::value> +// (tested with ICC 2021.1 Beta 20200827). +template +constexpr bool args_has_keyword_or_ds() { + return any_of...>::value; +} + +/// Helper class which collects positional, keyword, * and ** arguments for a Python function call template -class simple_collector { +class unpacking_collector { public: template - explicit simple_collector(Ts &&...values) - : m_args(pybind11::make_tuple(std::forward(values)...)) {} - - const tuple &args() const & { return m_args; } - dict kwargs() const { return {}; } + explicit unpacking_collector(Ts &&...values) + : m_names(reinterpret_steal( + handle())) // initialize to null to avoid useless allocation of 0-length tuple + { + /* + Python can sometimes utilize an extra space before the arguments to prepend `self`. + This is important enough that there is a special flag for it: + PY_VECTORCALL_ARGUMENTS_OFFSET. + All we have to do is allocate an extra space at the beginning of this array, and set the + flag. Note that the extra space is not passed directly in to vectorcall. + */ + m_args.reserve(sizeof...(values) + 1); + m_args.push_back_null(); + + if (args_has_keyword_or_ds()) { + list names_list; + + // collect_arguments guarantees this can't be constructed with kwargs before the last + // positional so we don't need to worry about Ts... being in anything but normal python + // order. + using expander = int[]; + (void) expander{0, (process(names_list, std::forward(values)), 0)...}; + + m_names = reinterpret_steal(PyList_AsTuple(names_list.ptr())); + } else { + auto not_used + = reinterpret_steal(handle()); // initialize as null (to avoid an allocation) - tuple args() && { return std::move(m_args); } + using expander = int[]; + (void) expander{0, (process(not_used, std::forward(values)), 0)...}; + } + } /// Call a Python function and pass the collected arguments object call(PyObject *ptr) const { - PyObject *result = PyObject_CallObject(ptr, m_args.ptr()); + size_t nargs = m_args.size() - 1; // -1 for PY_VECTORCALL_ARGUMENTS_OFFSET (see ctor) + if (m_names) { + nargs -= m_names.size(); + } + PyObject *result = +#if PY_VERSION_HEX >= 0x03090000 + PyObject_Vectorcall( +#else + _PyObject_Vectorcall( +#endif + ptr, m_args.data() + 1, nargs | PY_VECTORCALL_ARGUMENTS_OFFSET, m_names.ptr()); if (!result) { throw error_already_set(); } return reinterpret_steal(result); } -private: - tuple m_args; -}; - -/// Helper class which collects positional, keyword, * and ** arguments for a Python function call -template -class unpacking_collector { -public: - template - explicit unpacking_collector(Ts &&...values) { - // 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(); - using expander = int[]; - (void) expander{0, (process(args_list, std::forward(values)), 0)...}; - - m_args = std::move(args_list); + tuple args() const { + size_t nargs = m_args.size() - 1; // -1 for PY_VECTORCALL_ARGUMENTS_OFFSET (see ctor) + if (m_names) { + nargs -= m_names.size(); + } + tuple val(nargs); + for (size_t i = 0; i < nargs; ++i) { + // +1 for PY_VECTORCALL_ARGUMENTS_OFFSET (see ctor) + val[i] = reinterpret_borrow(m_args[i + 1]); + } + return val; } - const tuple &args() const & { return m_args; } - const dict &kwargs() const & { return m_kwargs; } - - tuple args() && { return std::move(m_args); } - dict kwargs() && { return std::move(m_kwargs); } - - /// Call a Python function and pass the collected arguments - object call(PyObject *ptr) const { - PyObject *result = PyObject_Call(ptr, m_args.ptr(), m_kwargs.ptr()); - if (!result) { - throw error_already_set(); + dict kwargs() const { + dict val; + if (m_names) { + size_t offset = m_args.size() - m_names.size(); + for (size_t i = 0; i < m_names.size(); ++i, ++offset) { + val[m_names[i]] = reinterpret_borrow(m_args[offset]); + } } - return reinterpret_steal(result); + return val; } private: + // normal argument, possibly needing conversion template - void process(list &args_list, T &&x) { - auto o = reinterpret_steal( - detail::make_caster::cast(std::forward(x), policy, {})); - if (!o) { + void process(list & /*names_list*/, T &&x) { + handle h = detail::make_caster::cast(std::forward(x), policy, {}); + if (!h) { #if !defined(PYBIND11_DETAILED_ERROR_MESSAGES) - throw cast_error_unable_to_convert_call_arg(std::to_string(args_list.size())); + throw cast_error_unable_to_convert_call_arg(std::to_string(m_args.size() - 1)); #else - throw cast_error_unable_to_convert_call_arg(std::to_string(args_list.size()), + throw cast_error_unable_to_convert_call_arg(std::to_string(m_args.size() - 1), type_id()); #endif } - args_list.append(std::move(o)); + m_args.push_back_steal(h.ptr()); // cast returns a new reference } - void process(list &args_list, detail::args_proxy ap) { + // * unpacking + void process(list & /*names_list*/, detail::args_proxy ap) { + if (!ap) { + return; + } for (auto a : ap) { - args_list.append(a); + m_args.push_back_borrow(a.ptr()); } } - void process(list & /*args_list*/, arg_v a) { + // named argument + // NOLINTNEXTLINE(performance-unnecessary-value-param) + void process(list &names_list, arg_v a) { + assert(names_list); if (!a.name) { #if !defined(PYBIND11_DETAILED_ERROR_MESSAGES) nameless_argument_error(); @@ -1570,7 +2325,8 @@ class unpacking_collector { nameless_argument_error(a.type); #endif } - if (m_kwargs.contains(a.name)) { + auto name = str(a.name); + if (names_list.contains(name)) { #if !defined(PYBIND11_DETAILED_ERROR_MESSAGES) multiple_values_error(); #else @@ -1584,22 +2340,27 @@ class unpacking_collector { throw cast_error_unable_to_convert_call_arg(a.name, a.type); #endif } - m_kwargs[a.name] = std::move(a.value); + names_list.append(std::move(name)); + m_args.push_back_borrow(a.value.ptr()); } - void process(list & /*args_list*/, detail::kwargs_proxy kp) { + // ** unpacking + void process(list &names_list, detail::kwargs_proxy kp) { if (!kp) { return; } - for (auto k : reinterpret_borrow(kp)) { - if (m_kwargs.contains(k.first)) { + assert(names_list); + for (auto &&k : reinterpret_borrow(kp)) { + auto name = str(k.first); + if (names_list.contains(name)) { #if !defined(PYBIND11_DETAILED_ERROR_MESSAGES) multiple_values_error(); #else - multiple_values_error(str(k.first)); + multiple_values_error(name); #endif } - m_kwargs[k.first] = k.second; + names_list.append(std::move(name)); + m_args.push_back_borrow(k.second.ptr()); } } @@ -1625,39 +2386,20 @@ class unpacking_collector { } private: - tuple m_args; - dict m_kwargs; + ref_small_vector m_args; + tuple m_names; }; -// [workaround(intel)] Separate function required here -// We need to put this into a separate function because the Intel compiler -// fails to compile enable_if_t...>::value> -// (tested with ICC 2021.1 Beta 20200827). -template -constexpr bool args_are_all_positional() { - return all_of...>::value; -} - -/// Collect only positional arguments for a Python function call -template ()>> -simple_collector collect_arguments(Args &&...args) { - return simple_collector(std::forward(args)...); -} - -/// Collect all arguments, including keywords and unpacking (only instantiated when needed) -template ()>> +/// Collect all arguments, including keywords and unpacking +template unpacking_collector collect_arguments(Args &&...args) { // Following argument order rules for generalized unpacking according to PEP 448 - static_assert(constexpr_last() - < constexpr_first() - && constexpr_last() - < constexpr_first(), - "Invalid function call: positional args must precede keywords and ** unpacking; " - "* unpacking must precede ** unpacking"); + static_assert( + constexpr_last() < constexpr_first(), + "Invalid function call: positional args must precede keywords and */** unpacking;"); + static_assert(constexpr_last() + < constexpr_first(), + "Invalid function call: * unpacking must precede ** unpacking"); return unpacking_collector(std::forward(args)...); } diff --git a/external_libraries/pybind11/include/pybind11/chrono.h b/external_libraries/pybind11/include/pybind11/chrono.h index 167ea0e3..668e458e 100644 --- a/external_libraries/pybind11/include/pybind11/chrono.h +++ b/external_libraries/pybind11/include/pybind11/chrono.h @@ -63,6 +63,8 @@ class duration_caster { get_duration(const std::chrono::duration &src) { return src; } + static const std::chrono::duration & + get_duration(const std::chrono::duration &&) = delete; // If this is a time_point get the time_since_epoch template @@ -185,7 +187,7 @@ class type_caster> using us_t = duration; auto us = duration_cast(src.time_since_epoch() % seconds(1)); if (us.count() < 0) { - us += seconds(1); + us += duration_cast(seconds(1)); } // Subtract microseconds BEFORE `system_clock::to_time_t`, because: diff --git a/external_libraries/pybind11/include/pybind11/complex.h b/external_libraries/pybind11/include/pybind11/complex.h index 8a831c12..0b6f4936 100644 --- a/external_libraries/pybind11/include/pybind11/complex.h +++ b/external_libraries/pybind11/include/pybind11/complex.h @@ -54,7 +54,23 @@ class type_caster> { if (!convert && !PyComplex_Check(src.ptr())) { return false; } - Py_complex result = PyComplex_AsCComplex(src.ptr()); + handle src_or_index = src; + // PyPy: 7.3.7's 3.8 does not implement PyLong_*'s __index__ calls. + // The same logic is used in numeric_caster for ints and floats +#if defined(PYPY_VERSION) + object index; + if (PYBIND11_INDEX_CHECK(src.ptr())) { + index = reinterpret_steal(PyNumber_Index(src.ptr())); + if (!index) { + PyErr_Clear(); + if (!convert) + return false; + } else { + src_or_index = index; + } + } +#endif + Py_complex result = PyComplex_AsCComplex(src_or_index.ptr()); if (result.real == -1.0 && PyErr_Occurred()) { PyErr_Clear(); return false; @@ -68,7 +84,10 @@ class type_caster> { return PyComplex_FromDoubles((double) src.real(), (double) src.imag()); } - PYBIND11_TYPE_CASTER(std::complex, const_name("complex")); + PYBIND11_TYPE_CASTER( + std::complex, + io_name("typing.SupportsComplex | typing.SupportsFloat | typing.SupportsIndex", + "complex")); }; PYBIND11_NAMESPACE_END(detail) PYBIND11_NAMESPACE_END(PYBIND11_NAMESPACE) diff --git a/external_libraries/pybind11/include/pybind11/conduit/README.txt b/external_libraries/pybind11/include/pybind11/conduit/README.txt new file mode 100644 index 00000000..9a2c53ba --- /dev/null +++ b/external_libraries/pybind11/include/pybind11/conduit/README.txt @@ -0,0 +1,15 @@ +NOTE +---- + +The C++ code here + +** only depends on ** + +and nothing else. + +DO NOT ADD CODE WITH OTHER EXTERNAL DEPENDENCIES TO THIS DIRECTORY. + +Read on: + +pybind11_conduit_v1.h — Type-safe interoperability between different + independent Python/C++ bindings systems. diff --git a/external_libraries/pybind11/include/pybind11/conduit/pybind11_conduit_v1.h b/external_libraries/pybind11/include/pybind11/conduit/pybind11_conduit_v1.h new file mode 100644 index 00000000..6d5d0efc --- /dev/null +++ b/external_libraries/pybind11/include/pybind11/conduit/pybind11_conduit_v1.h @@ -0,0 +1,116 @@ +// Copyright (c) 2024 The pybind Community. + +/* The pybind11_conduit_v1 feature enables type-safe interoperability between + +* different independent Python/C++ bindings systems, + +* including pybind11 versions with different PYBIND11_INTERNALS_VERSION's. + + * NOTE: The conduit feature + only covers from-Python-to-C++ conversions, it + does not cover from-C++-to-Python conversions. + (For the latter, a different feature would have to be added.) + +The naming of the feature is a bit misleading: + +* The feature is in no way tied to pybind11 internals. + +* It just happens to originate from pybind11 and currently still lives there. + +* The only external dependency is . + +The implementation is a VERY light-weight dependency. It is designed to be +compatible with any ISO C++11 (or higher) compiler, and does NOT require +C++ Exception Handling to be enabled. + +Please see https://github.com/pybind/pybind11/pull/5296 for more background. + +The implementation involves a + +def _pybind11_conduit_v1_( + self, + pybind11_platform_abi_id: bytes, + cpp_type_info_capsule: capsule, + pointer_kind: bytes) -> capsule + +method that is meant to be added to Python objects wrapping C++ objects +(e.g. pybind11::class_-wrapped types). + +The design of the _pybind11_conduit_v1_ feature provides two layers of +protection against C++ ABI mismatches: + +* The first and most important layer is that the pybind11_platform_abi_id's + must match between extensions. — This will never be perfect, but is the same + pragmatic approach used in pybind11 since 2017 + (https://github.com/pybind/pybind11/commit/96997a4b9d4ec3d389a570604394af5d5eee2557, + PYBIND11_INTERNALS_ID). + +* The second layer is that the typeid(std::type_info).name()'s must match + between extensions. + +The implementation below (which is shorter than this comment!), serves as a +battle-tested specification. The main API is this one function: + +auto *cpp_pointer = pybind11_conduit_v1::get_type_pointer_ephemeral(py_obj); + +It is meant to be a minimalistic reference implementation, intentionally +without comprehensive error reporting. It is expected that major bindings +systems will roll their own, compatible implementations, potentially with +system-specific error reporting. The essential specifications all bindings +systems need to agree on are merely: + +* PYBIND11_PLATFORM_ABI_ID (const char* literal). + +* The cpp_type_info capsule (see below: a void *ptr and a const char *name). + +* The cpp_conduit capsule (see below: a void *ptr and a const char *name). + +* "raw_pointer_ephemeral" means: the lifetime of the pointer is the lifetime + of the py_obj. + +*/ + +// THIS MUST STAY AT THE TOP! +#include "pybind11_platform_abi_id.h" + +#include +#include + +namespace pybind11_conduit_v1 { + +inline void *get_raw_pointer_ephemeral(PyObject *py_obj, const std::type_info *cpp_type_info) { + PyObject *cpp_type_info_capsule + = PyCapsule_New(const_cast(static_cast(cpp_type_info)), + typeid(std::type_info).name(), + nullptr); + if (cpp_type_info_capsule == nullptr) { + return nullptr; + } + PyObject *cpp_conduit = PyObject_CallMethod(py_obj, + "_pybind11_conduit_v1_", + "yOy", + PYBIND11_PLATFORM_ABI_ID, + cpp_type_info_capsule, + "raw_pointer_ephemeral"); + Py_DECREF(cpp_type_info_capsule); + if (cpp_conduit == nullptr) { + return nullptr; + } + void *raw_ptr = PyCapsule_GetPointer(cpp_conduit, cpp_type_info->name()); + Py_DECREF(cpp_conduit); + if (PyErr_Occurred()) { + return nullptr; + } + return raw_ptr; +} + +template +T *get_type_pointer_ephemeral(PyObject *py_obj) { + void *raw_ptr = get_raw_pointer_ephemeral(py_obj, &typeid(T)); + if (raw_ptr == nullptr) { + return nullptr; + } + return static_cast(raw_ptr); +} + +} // namespace pybind11_conduit_v1 diff --git a/external_libraries/pybind11/include/pybind11/conduit/pybind11_platform_abi_id.h b/external_libraries/pybind11/include/pybind11/conduit/pybind11_platform_abi_id.h new file mode 100644 index 00000000..d21fdc56 --- /dev/null +++ b/external_libraries/pybind11/include/pybind11/conduit/pybind11_platform_abi_id.h @@ -0,0 +1,87 @@ +#pragma once + +// Copyright (c) 2024 The pybind Community. + +// To maximize reusability: +// DO NOT ADD CODE THAT REQUIRES C++ EXCEPTION HANDLING. + +#include "wrap_include_python_h.h" + +// Implementation details. DO NOT USE ELSEWHERE. (Unfortunately we cannot #undef them.) +// This is duplicated here to maximize portability. +#define PYBIND11_PLATFORM_ABI_ID_STRINGIFY(x) #x +#define PYBIND11_PLATFORM_ABI_ID_TOSTRING(x) PYBIND11_PLATFORM_ABI_ID_STRINGIFY(x) + +#ifdef PYBIND11_COMPILER_TYPE +// // To maintain backward compatibility (see PR #5439). +# define PYBIND11_COMPILER_TYPE_LEADING_UNDERSCORE "" +#else +# define PYBIND11_COMPILER_TYPE_LEADING_UNDERSCORE "_" +# if defined(__MINGW32__) +# define PYBIND11_COMPILER_TYPE "mingw" +# elif defined(__CYGWIN__) +# define PYBIND11_COMPILER_TYPE "gcc_cygwin" +# elif defined(_MSC_VER) +# define PYBIND11_COMPILER_TYPE "msvc" +# elif defined(__clang__) || defined(__GNUC__) +# define PYBIND11_COMPILER_TYPE "system" // Assumed compatible with system compiler. +# else +# error "Unknown PYBIND11_COMPILER_TYPE: PLEASE REVISE THIS CODE." +# endif +#endif + +// PR #5439 made this macro obsolete. However, there are many manipulations of this macro in the +// wild. Therefore, to maintain backward compatibility, it is kept around. +#ifndef PYBIND11_STDLIB +# define PYBIND11_STDLIB "" +#endif + +#ifndef PYBIND11_BUILD_ABI +# if defined(_MSC_VER) // See PR #4953. +# if defined(_MT) && defined(_DLL) // Corresponding to CL command line options /MD or /MDd. +# if (_MSC_VER) / 100 == 19 +# define PYBIND11_BUILD_ABI "_md_mscver19" +# else +# error "Unknown major version for MSC_VER: PLEASE REVISE THIS CODE." +# endif +# elif defined(_MT) // Corresponding to CL command line options /MT or /MTd. +# define PYBIND11_BUILD_ABI "_mt_mscver" PYBIND11_PLATFORM_ABI_ID_TOSTRING(_MSC_VER) +# else +# if (_MSC_VER) / 100 == 19 +# define PYBIND11_BUILD_ABI "_none_mscver19" +# else +# error "Unknown major version for MSC_VER: PLEASE REVISE THIS CODE." +# endif +# endif +# elif defined(_LIBCPP_ABI_VERSION) // https://libcxx.llvm.org/DesignDocs/ABIVersioning.html +# define PYBIND11_BUILD_ABI \ + "_libcpp_abi" PYBIND11_PLATFORM_ABI_ID_TOSTRING(_LIBCPP_ABI_VERSION) +# elif defined(_GLIBCXX_USE_CXX11_ABI) // See PR #5439. +# if defined(__NVCOMPILER) +// // Assume that NVHPC is in the 1xxx ABI family. +// // THIS ASSUMPTION IS NOT FUTURE PROOF but apparently the best we can do. +// // Please let us know if there is a way to validate the assumption here. +# elif !defined(__GXX_ABI_VERSION) +# error \ + "Unknown platform or compiler (_GLIBCXX_USE_CXX11_ABI): PLEASE REVISE THIS CODE." +# endif +# if defined(__GXX_ABI_VERSION) && __GXX_ABI_VERSION < 1002 || __GXX_ABI_VERSION >= 2000 +# error "Unknown platform or compiler (__GXX_ABI_VERSION): PLEASE REVISE THIS CODE." +# endif +# define PYBIND11_BUILD_ABI \ + "_libstdcpp_gxx_abi_1xxx_use_cxx11_abi_" PYBIND11_PLATFORM_ABI_ID_TOSTRING( \ + _GLIBCXX_USE_CXX11_ABI) +# else +# error "Unknown platform or compiler: PLEASE REVISE THIS CODE." +# endif +#endif + +// On MSVC, debug and release builds are not ABI-compatible! +#if defined(_MSC_VER) && defined(_DEBUG) +# define PYBIND11_BUILD_TYPE "_debug" +#else +# define PYBIND11_BUILD_TYPE "" +#endif + +#define PYBIND11_PLATFORM_ABI_ID \ + PYBIND11_COMPILER_TYPE PYBIND11_STDLIB PYBIND11_BUILD_ABI PYBIND11_BUILD_TYPE diff --git a/external_libraries/pybind11/include/pybind11/conduit/wrap_include_python_h.h b/external_libraries/pybind11/include/pybind11/conduit/wrap_include_python_h.h new file mode 100644 index 00000000..713dc4bb --- /dev/null +++ b/external_libraries/pybind11/include/pybind11/conduit/wrap_include_python_h.h @@ -0,0 +1,72 @@ +#pragma once + +// Copyright (c) 2024 The pybind Community. + +// STRONG REQUIREMENT: +// This header is a wrapper around `#include `, therefore it +// MUST BE INCLUDED BEFORE ANY STANDARD HEADERS are included. +// See also: +// https://docs.python.org/3/c-api/intro.html#include-files +// Quoting from there: +// Note: Since Python may define some pre-processor definitions which affect +// the standard headers on some systems, you must include Python.h before +// any standard headers are included. + +// To maximize reusability: +// DO NOT ADD CODE THAT REQUIRES C++ EXCEPTION HANDLING. + +// Disable linking to pythonX_d.lib on Windows in debug mode. +#if defined(_MSC_VER) && defined(_DEBUG) && !defined(Py_DEBUG) +// Workaround for a VS 2022 issue. +// See https://github.com/pybind/pybind11/pull/3497 for full context. +// NOTE: This workaround knowingly violates the Python.h include order +// requirement (see above). +# include +# if _MSVC_STL_VERSION >= 143 +# include +# endif +# define PYBIND11_DEBUG_MARKER +# undef _DEBUG +#endif + +// Don't let Python.h #define (v)snprintf as macro because they are implemented +// properly in Visual Studio since 2015. +#if defined(_MSC_VER) +# define HAVE_SNPRINTF 1 +#endif + +#if defined(_MSC_VER) +# pragma warning(push) +# pragma warning(disable : 4505) +// C4505: 'PySlice_GetIndicesEx': unreferenced local function has been removed +#endif + +#include +#include +#include + +#if defined(_MSC_VER) +# pragma warning(pop) +#endif + +#if defined(PYBIND11_DEBUG_MARKER) +# define _DEBUG 1 +# undef PYBIND11_DEBUG_MARKER +#endif + +// Python #defines overrides on all sorts of core functions, which +// tends to wreak havok in C++ codebases that expect these to work +// like regular functions (potentially with several overloads). +#if defined(isalnum) +# undef isalnum +# undef isalpha +# undef islower +# undef isspace +# undef isupper +# undef tolower +# undef toupper +#endif + +#if defined(copysign) +# undef copysign +#endif diff --git a/external_libraries/pybind11/include/pybind11/critical_section.h b/external_libraries/pybind11/include/pybind11/critical_section.h new file mode 100644 index 00000000..2d264120 --- /dev/null +++ b/external_libraries/pybind11/include/pybind11/critical_section.h @@ -0,0 +1,56 @@ +// Copyright (c) 2016-2025 The Pybind Development Team. +// All rights reserved. Use of this source code is governed by a +// BSD-style license that can be found in the LICENSE file. + +#pragma once + +#include "pytypes.h" + +PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE) + +/// This does not do anything if there's a GIL. On free-threaded Python, +/// it locks an object. This uses the CPython API, which has limits +class scoped_critical_section { +public: +#ifdef Py_GIL_DISABLED + explicit scoped_critical_section(handle obj1, handle obj2 = handle{}) { + if (obj1) { + if (obj2) { + PyCriticalSection2_Begin(§ion2, obj1.ptr(), obj2.ptr()); + rank = 2; + } else { + PyCriticalSection_Begin(§ion, obj1.ptr()); + rank = 1; + } + } else if (obj2) { + PyCriticalSection_Begin(§ion, obj2.ptr()); + rank = 1; + } + } + + ~scoped_critical_section() { + if (rank == 1) { + PyCriticalSection_End(§ion); + } else if (rank == 2) { + PyCriticalSection2_End(§ion2); + } + } +#else + explicit scoped_critical_section(handle, handle = handle{}) {}; + ~scoped_critical_section() = default; +#endif + + scoped_critical_section(const scoped_critical_section &) = delete; + scoped_critical_section &operator=(const scoped_critical_section &) = delete; + +private: +#ifdef Py_GIL_DISABLED + int rank{0}; + union { + PyCriticalSection section; + PyCriticalSection2 section2; + }; +#endif +}; + +PYBIND11_NAMESPACE_END(PYBIND11_NAMESPACE) diff --git a/external_libraries/pybind11/include/pybind11/detail/argument_vector.h b/external_libraries/pybind11/include/pybind11/detail/argument_vector.h new file mode 100644 index 00000000..6e2c2ec4 --- /dev/null +++ b/external_libraries/pybind11/include/pybind11/detail/argument_vector.h @@ -0,0 +1,417 @@ +/* + pybind11/detail/argument_vector.h: small_vector-like containers to + avoid heap allocation of arguments during function call dispatch. + + Copyright (c) Meta Platforms, Inc. and affiliates. + + All rights reserved. Use of this source code is governed by a + BSD-style license that can be found in the LICENSE file. +*/ + +#pragma once + +#include + +#include "common.h" + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE) + +PYBIND11_WARNING_DISABLE_MSVC(4127) + +PYBIND11_NAMESPACE_BEGIN(detail) + +// Shared implementation utility for our small_vector-like containers. +// We support C++11 and C++14, so we cannot use +// std::variant. Union with the tag packed next to the inline +// array's size is smaller anyway, allowing 1 extra handle of +// inline storage for free. Compare the layouts (1 line per +// size_t/void*, assuming a 64-bit machine): +// With variant, total is N + 2 for N >= 2: +// - variant tag (cannot be packed with the array size) +// - array size (or first pointer of 3 in std::vector) +// - N pointers of inline storage (or 2 remaining pointers of std::vector) +// Custom union, total is N + 1 for N >= 3: +// - variant tag & array size if applicable +// - N pointers of inline storage (or 3 pointers of std::vector) +// +// NOTE: this is a low-level representational convenience; the two +// use cases of this union are materially different and in particular +// have different semantics for inline_array::size. All that is being +// shared is the memory management behavior. +template +union inline_array_or_vector { + struct inline_array { + bool is_inline = true; + std::uint32_t size = 0; + std::array arr; + }; + struct heap_vector { + bool is_inline = false; + std::vector vec; + + heap_vector() = default; + heap_vector(std::size_t count, VectorT value) : vec(count, value) {} + }; + + inline_array iarray; + heap_vector hvector; + + inline_array_or_vector() : iarray() {} + + ~inline_array_or_vector() { + if (is_inline()) { + iarray.~inline_array(); + } else { + hvector.~heap_vector(); + } + } + + // Disable copy ctor and assignment. + inline_array_or_vector(const inline_array_or_vector &) = delete; + inline_array_or_vector &operator=(const inline_array_or_vector &) = delete; + + inline_array_or_vector(inline_array_or_vector &&rhs) noexcept { + if (rhs.is_inline()) { + new (&iarray) inline_array(std::move(rhs.iarray)); + } else { + new (&hvector) heap_vector(std::move(rhs.hvector)); + } + assert(is_inline() == rhs.is_inline()); + } + + inline_array_or_vector &operator=(inline_array_or_vector &&rhs) noexcept { + if (this == &rhs) { + return *this; + } + + if (is_inline()) { + iarray.~inline_array(); + } else { + hvector.~heap_vector(); + } + + if (rhs.is_inline()) { + new (&iarray) inline_array(std::move(rhs.iarray)); + } else { + new (&hvector) heap_vector(std::move(rhs.hvector)); + } + return *this; + } + + bool is_inline() const { + // It is undefined behavior to access the inactive member of a + // union directly. However, it is well-defined to reinterpret_cast any + // pointer into a pointer to char and examine it as an array + // of bytes. See + // https://dev-discuss.pytorch.org/t/unionizing-for-profit-how-to-exploit-the-power-of-unions-in-c/444#the-memcpy-loophole-4 + bool result = false; + static_assert(offsetof(inline_array, is_inline) == 0, + "untagged union implementation relies on this"); + static_assert(offsetof(heap_vector, is_inline) == 0, + "untagged union implementation relies on this"); + std::memcpy(&result, reinterpret_cast(this), sizeof(bool)); + return result; + } +}; + +template +struct small_vector { +public: + small_vector() = default; + + // Disable copy ctor and assignment. + small_vector(const small_vector &) = delete; + small_vector &operator=(const small_vector &) = delete; + small_vector(small_vector &&) noexcept = default; + small_vector &operator=(small_vector &&) noexcept = default; + + std::size_t size() const { + if (is_inline()) { + return m_repr.iarray.size; + } + return m_repr.hvector.vec.size(); + } + + T const *data() const { + if (is_inline()) { + return m_repr.iarray.arr.data(); + } + return m_repr.hvector.vec.data(); + } + + T &operator[](std::size_t idx) { + assert(idx < size()); + if (is_inline()) { + return m_repr.iarray.arr[idx]; + } + return m_repr.hvector.vec[idx]; + } + + T const &operator[](std::size_t idx) const { + assert(idx < size()); + if (is_inline()) { + return m_repr.iarray.arr[idx]; + } + return m_repr.hvector.vec[idx]; + } + + void push_back(const T &x) { emplace_back(x); } + + void push_back(T &&x) { emplace_back(std::move(x)); } + + template + void emplace_back(Args &&...x) { + if (is_inline()) { + auto &ha = m_repr.iarray; + if (ha.size == InlineSize) { + move_to_heap_vector_with_reserved_size(InlineSize + 1); + m_repr.hvector.vec.emplace_back(std::forward(x)...); + } else { + ha.arr[ha.size++] = T(std::forward(x)...); + } + } else { + m_repr.hvector.vec.emplace_back(std::forward(x)...); + } + } + + void reserve(std::size_t sz) { + if (is_inline()) { + if (sz > InlineSize) { + move_to_heap_vector_with_reserved_size(sz); + } + } else { + reserve_slow_path(sz); + } + } + +private: + using repr_type = inline_array_or_vector; + repr_type m_repr; + + PYBIND11_NOINLINE void move_to_heap_vector_with_reserved_size(std::size_t reserved_size) { + assert(is_inline()); + auto &ha = m_repr.iarray; + using heap_vector = typename repr_type::heap_vector; + heap_vector hv; + hv.vec.reserve(reserved_size); + static_assert(std::is_nothrow_move_constructible::value, + "this conversion is not exception safe"); + static_assert(std::is_nothrow_move_constructible::value, + "this conversion is not exception safe"); + std::move(ha.arr.begin(), ha.arr.begin() + ha.size, std::back_inserter(hv.vec)); + new (&m_repr.hvector) heap_vector(std::move(hv)); + } + + PYBIND11_NOINLINE void reserve_slow_path(std::size_t sz) { m_repr.hvector.vec.reserve(sz); } + + bool is_inline() const { return m_repr.is_inline(); } +}; + +// Container to avoid heap allocation for kRequestedInlineSize or fewer booleans. +template +struct small_vector { +private: +public: + small_vector() = default; + + // Disable copy ctor and assignment. + small_vector(const small_vector &) = delete; + small_vector &operator=(const small_vector &) = delete; + small_vector(small_vector &&) noexcept = default; + small_vector &operator=(small_vector &&) noexcept = default; + + small_vector(std::size_t count, bool value) { + if (count > kInlineSize) { + new (&m_repr.hvector) typename repr_type::heap_vector(count, value); + } else { + auto &inline_arr = m_repr.iarray; + inline_arr.arr.fill(value ? static_cast(-1) : 0); + inline_arr.size = static_cast(count); + } + } + + std::size_t size() const { + if (is_inline()) { + return m_repr.iarray.size; + } + return m_repr.hvector.vec.size(); + } + + void reserve(std::size_t sz) { + if (is_inline()) { + if (sz > kInlineSize) { + move_to_heap_vector_with_reserved_size(sz); + } + } else { + m_repr.hvector.vec.reserve(sz); + } + } + + bool operator[](std::size_t idx) const { + if (is_inline()) { + return inline_index(idx); + } + assert(idx < m_repr.hvector.vec.size()); + return m_repr.hvector.vec[idx]; + } + + void push_back(bool b) { + if (is_inline()) { + auto &ha = m_repr.iarray; + if (ha.size == kInlineSize) { + move_to_heap_vector_with_reserved_size(kInlineSize + 1); + push_back_slow_path(b); + } else { + assert(ha.size < kInlineSize); + const auto wbi = word_and_bit_index(ha.size++); + assert(wbi.word < kWords); + assert(wbi.bit < kBitsPerWord); + if (b) { + ha.arr[wbi.word] |= (static_cast(1) << wbi.bit); + } else { + ha.arr[wbi.word] &= ~(static_cast(1) << wbi.bit); + } + assert(operator[](ha.size - 1) == b); + } + } else { + push_back_slow_path(b); + } + } + + void set(std::size_t idx, bool value = true) { + if (is_inline()) { + auto &ha = m_repr.iarray; + assert(ha.size < kInlineSize); + const auto wbi = word_and_bit_index(idx); + assert(wbi.word < kWords); + assert(wbi.bit < kBitsPerWord); + if (value) { + ha.arr[wbi.word] |= (static_cast(1) << wbi.bit); + } else { + ha.arr[wbi.word] &= ~(static_cast(1) << wbi.bit); + } + } else { + m_repr.hvector.vec[idx] = value; + } + } + + void swap(small_vector &rhs) noexcept { std::swap(m_repr, rhs.m_repr); } + +private: + struct WordAndBitIndex { + std::size_t word; + std::size_t bit; + }; + + static WordAndBitIndex word_and_bit_index(std::size_t idx) { + return WordAndBitIndex{idx / kBitsPerWord, idx % kBitsPerWord}; + } + + bool inline_index(std::size_t idx) const { + const auto wbi = word_and_bit_index(idx); + assert(wbi.word < kWords); + assert(wbi.bit < kBitsPerWord); + return m_repr.iarray.arr[wbi.word] & (static_cast(1) << wbi.bit); + } + + PYBIND11_NOINLINE void move_to_heap_vector_with_reserved_size(std::size_t reserved_size) { + auto &inline_arr = m_repr.iarray; + using heap_vector = typename repr_type::heap_vector; + heap_vector hv; + hv.vec.reserve(reserved_size); + for (std::size_t ii = 0; ii < inline_arr.size; ++ii) { + hv.vec.push_back(inline_index(ii)); + } + new (&m_repr.hvector) heap_vector(std::move(hv)); + } + + PYBIND11_NOINLINE void push_back_slow_path(bool b) { m_repr.hvector.vec.push_back(b); } + + static constexpr auto kBitsPerWord = 8 * sizeof(std::size_t); + static constexpr auto kWords = (kRequestedInlineSize + kBitsPerWord - 1) / kBitsPerWord; + static constexpr auto kInlineSize = kWords * kBitsPerWord; + + using repr_type = inline_array_or_vector; + repr_type m_repr; + + bool is_inline() const { return m_repr.is_inline(); } +}; + +// Container to avoid heap allocation for N or fewer arguments. +template +using argument_vector = small_vector; + +// Container to avoid heap allocation for N or fewer booleans. +template +using args_convert_vector = small_vector; + +/// A small_vector of PyObject* that holds references and releases them on destruction. +/// This provides explicit ownership semantics without relying on py::object's +/// destructor, and avoids the need for reinterpret_cast when passing to vectorcall. +template +class ref_small_vector { +public: + ref_small_vector() = default; + + ~ref_small_vector() { + for (std::size_t i = 0; i < m_ptrs.size(); ++i) { + Py_XDECREF(m_ptrs[i]); + } + } + + // Disable copy (prevent accidental double-decref) + ref_small_vector(const ref_small_vector &) = delete; + ref_small_vector &operator=(const ref_small_vector &) = delete; + + // Move is allowed + ref_small_vector(ref_small_vector &&other) noexcept : m_ptrs(std::move(other.m_ptrs)) { + // other.m_ptrs is now empty, so its destructor won't decref anything + } + + ref_small_vector &operator=(ref_small_vector &&other) noexcept { + if (this != &other) { + // Decref our current contents + for (std::size_t i = 0; i < m_ptrs.size(); ++i) { + Py_XDECREF(m_ptrs[i]); + } + m_ptrs = std::move(other.m_ptrs); + } + return *this; + } + + /// Add a pointer, taking ownership (no incref, will decref on destruction) + void push_back_steal(PyObject *p) { m_ptrs.push_back(p); } + + /// Add a pointer, borrowing (increfs now, will decref on destruction) + void push_back_borrow(PyObject *p) { + Py_XINCREF(p); + m_ptrs.push_back(p); + } + + /// Add a null pointer (for PY_VECTORCALL_ARGUMENTS_OFFSET slot) + void push_back_null() { m_ptrs.push_back(nullptr); } + + void reserve(std::size_t sz) { m_ptrs.reserve(sz); } + + std::size_t size() const { return m_ptrs.size(); } + + PyObject *operator[](std::size_t idx) const { return m_ptrs[idx]; } + + PyObject *const *data() const { return m_ptrs.data(); } + +private: + small_vector m_ptrs; +}; + +PYBIND11_NAMESPACE_END(detail) +PYBIND11_NAMESPACE_END(PYBIND11_NAMESPACE) diff --git a/external_libraries/pybind11/include/pybind11/detail/class.h b/external_libraries/pybind11/include/pybind11/detail/class.h index bc2b40c5..8b9d0b8e 100644 --- a/external_libraries/pybind11/include/pybind11/detail/class.h +++ b/external_libraries/pybind11/include/pybind11/detail/class.h @@ -9,8 +9,10 @@ #pragma once -#include "../attr.h" -#include "../options.h" +#include +#include + +#include "exception_translation.h" PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE) PYBIND11_NAMESPACE_BEGIN(detail) @@ -69,7 +71,7 @@ inline PyTypeObject *make_static_property_type() { issue no Python C API calls which could potentially invoke the garbage collector (the GC will call type_traverse(), which will in turn find the newly constructed type in an invalid state) */ - auto *heap_type = (PyHeapTypeObject *) PyType_Type.tp_alloc(&PyType_Type, 0); + auto *heap_type = reinterpret_cast(PyType_Type.tp_alloc(&PyType_Type, 0)); if (!heap_type) { pybind11_fail("make_static_property_type(): error allocating type!"); } @@ -86,18 +88,17 @@ inline PyTypeObject *make_static_property_type() { type->tp_descr_get = pybind11_static_get; type->tp_descr_set = pybind11_static_set; - if (PyType_Ready(type) < 0) { - pybind11_fail("make_static_property_type(): failure in PyType_Ready()!"); - } - # if PY_VERSION_HEX >= 0x030C0000 - // PRE 3.12 FEATURE FREEZE. PLEASE REVIEW AFTER FREEZE. // Since Python-3.12 property-derived types are required to // have dynamic attributes (to set `__doc__`) enable_dynamic_attributes(heap_type); # endif - setattr((PyObject *) type, "__module__", str("pybind11_builtins")); + if (PyType_Ready(type) < 0) { + pybind11_fail("make_static_property_type(): failure in PyType_Ready()!"); + } + + setattr(reinterpret_cast(type), "__module__", str(PYBIND11_DUMMY_MODULE_NAME)); PYBIND11_SET_OLDPY_QUALNAME(type, name_obj); return type; @@ -189,12 +190,10 @@ extern "C" inline PyObject *pybind11_meta_call(PyObject *type, PyObject *args, P return nullptr; } - // This must be a pybind11 instance - auto *instance = reinterpret_cast(self); - // Ensure that the base __init__ function(s) were called - for (const auto &vh : values_and_holders(instance)) { - if (!vh.holder_constructed()) { + values_and_holders vhs(self); + for (const auto &vh : vhs) { + if (!vh.holder_constructed() && !vhs.is_redundant_value_and_holder(vh)) { PyErr_Format(PyExc_TypeError, "%.200s.__init__() must be called when overriding __init__", get_fully_qualified_tp_name(vh.type->type).c_str()); @@ -208,39 +207,49 @@ extern "C" inline PyObject *pybind11_meta_call(PyObject *type, PyObject *args, P /// Cleanup the type-info for a pybind11-registered type. extern "C" inline void pybind11_meta_dealloc(PyObject *obj) { - auto *type = (PyTypeObject *) obj; - auto &internals = get_internals(); - - // A pybind11-registered type will: - // 1) be found in internals.registered_types_py - // 2) have exactly one associated `detail::type_info` - auto found_type = internals.registered_types_py.find(type); - if (found_type != internals.registered_types_py.end() && found_type->second.size() == 1 - && found_type->second[0]->type == type) { - - auto *tinfo = found_type->second[0]; - auto tindex = std::type_index(*tinfo->cpptype); - internals.direct_conversions.erase(tindex); - - if (tinfo->module_local) { - get_local_internals().registered_types_cpp.erase(tindex); - } else { - internals.registered_types_cpp.erase(tindex); - } - internals.registered_types_py.erase(tinfo->type); - - // Actually just `std::erase_if`, but that's only available in C++20 - auto &cache = internals.inactive_override_cache; - for (auto it = cache.begin(), last = cache.end(); it != last;) { - if (it->first == (PyObject *) tinfo->type) { - it = cache.erase(it); + with_internals_if_internals([obj](internals &internals) { + auto *type = (PyTypeObject *) obj; + + // A pybind11-registered type will: + // 1) be found in internals.registered_types_py + // 2) have exactly one associated `detail::type_info` + auto found_type = internals.registered_types_py.find(type); + if (found_type != internals.registered_types_py.end() && found_type->second.size() == 1 + && found_type->second[0]->type == type) { + + auto *tinfo = found_type->second[0]; + auto tindex = std::type_index(*tinfo->cpptype); + internals.direct_conversions.erase(tindex); + + auto &local_internals = get_local_internals(); + if (tinfo->module_local) { + local_internals.registered_types_cpp.erase(tinfo->cpptype); } else { - ++it; + internals.registered_types_cpp.erase(tindex); +#if PYBIND11_INTERNALS_VERSION >= 12 + internals.registered_types_cpp_fast.erase(tinfo->cpptype); + for (const std::type_info *alias : tinfo->alias_chain) { + auto num_erased = internals.registered_types_cpp_fast.erase(alias); + (void) num_erased; + assert(num_erased > 0); + } +#endif + } + internals.registered_types_py.erase(tinfo->type); + + // Actually just `std::erase_if`, but that's only available in C++20 + auto &cache = internals.inactive_override_cache; + for (auto it = cache.begin(), last = cache.end(); it != last;) { + if (it->first == (PyObject *) tinfo->type) { + it = cache.erase(it); + } else { + ++it; + } } - } - delete tinfo; - } + delete tinfo; + } + }); PyType_Type.tp_dealloc(obj); } @@ -256,7 +265,7 @@ inline PyTypeObject *make_default_metaclass() { issue no Python C API calls which could potentially invoke the garbage collector (the GC will call type_traverse(), which will in turn find the newly constructed type in an invalid state) */ - auto *heap_type = (PyHeapTypeObject *) PyType_Type.tp_alloc(&PyType_Type, 0); + auto *heap_type = reinterpret_cast(PyType_Type.tp_alloc(&PyType_Type, 0)); if (!heap_type) { pybind11_fail("make_default_metaclass(): error allocating metaclass!"); } @@ -282,7 +291,7 @@ inline PyTypeObject *make_default_metaclass() { pybind11_fail("make_default_metaclass(): failure in PyType_Ready()!"); } - setattr((PyObject *) type, "__module__", str("pybind11_builtins")); + setattr(reinterpret_cast(type), "__module__", str(PYBIND11_DUMMY_MODULE_NAME)); PYBIND11_SET_OLDPY_QUALNAME(type, name_obj); return type; @@ -297,7 +306,7 @@ inline void traverse_offset_bases(void *valueptr, instance *self, bool (*f)(void * /*parentptr*/, instance * /*self*/)) { for (handle h : reinterpret_borrow(tinfo->type->tp_bases)) { - if (auto *parent_tinfo = get_type_info((PyTypeObject *) h.ptr())) { + if (auto *parent_tinfo = get_type_info(reinterpret_cast(h.ptr()))) { for (auto &c : parent_tinfo->implicit_casts) { if (c.first == tinfo->cpptype) { auto *parentptr = c.second(valueptr); @@ -312,20 +321,49 @@ inline void traverse_offset_bases(void *valueptr, } } +#ifdef Py_GIL_DISABLED +inline void enable_try_inc_ref(PyObject *obj) { +# if PY_VERSION_HEX >= 0x030E00A4 + PyUnstable_EnableTryIncRef(obj); +# else + if (_Py_IsImmortal(obj)) { + return; + } + for (;;) { + Py_ssize_t shared = _Py_atomic_load_ssize_relaxed(&obj->ob_ref_shared); + if ((shared & _Py_REF_SHARED_FLAG_MASK) != 0) { + // Nothing to do if it's in WEAKREFS, QUEUED, or MERGED states. + return; + } + if (_Py_atomic_compare_exchange_ssize( + &obj->ob_ref_shared, &shared, shared | _Py_REF_MAYBE_WEAKREF)) { + return; + } + } +# endif +} +#endif + inline bool register_instance_impl(void *ptr, instance *self) { - get_internals().registered_instances.emplace(ptr, self); + assert(ptr); +#ifdef Py_GIL_DISABLED + enable_try_inc_ref(reinterpret_cast(self)); +#endif + with_instance_map(ptr, [&](instance_map &instances) { instances.emplace(ptr, self); }); return true; // unused, but gives the same signature as the deregister func } inline bool deregister_instance_impl(void *ptr, instance *self) { - auto ®istered_instances = get_internals().registered_instances; - auto range = registered_instances.equal_range(ptr); - for (auto it = range.first; it != range.second; ++it) { - if (self == it->second) { - registered_instances.erase(it); - return true; + assert(ptr); + return with_instance_map(ptr, [&](instance_map &instances) { + auto range = instances.equal_range(ptr); + for (auto it = range.first; it != range.second; ++it) { + if (self == it->second) { + instances.erase(it); + return true; + } } - } - return false; + return false; + }); } inline void register_instance(instance *self, void *valptr, const type_info *tinfo) { @@ -375,28 +413,37 @@ extern "C" inline PyObject *pybind11_object_new(PyTypeObject *type, PyObject *, extern "C" inline int pybind11_object_init(PyObject *self, PyObject *, PyObject *) { PyTypeObject *type = Py_TYPE(self); std::string msg = get_fully_qualified_tp_name(type) + ": No constructor defined!"; - PyErr_SetString(PyExc_TypeError, msg.c_str()); + set_error(PyExc_TypeError, msg.c_str()); return -1; } inline void add_patient(PyObject *nurse, PyObject *patient) { - auto &internals = get_internals(); auto *instance = reinterpret_cast(nurse); instance->has_patients = true; Py_INCREF(patient); - internals.patients[nurse].push_back(patient); + + with_internals([&](internals &internals) { internals.patients[nurse].push_back(patient); }); } inline void clear_patients(PyObject *self) { auto *instance = reinterpret_cast(self); - auto &internals = get_internals(); - auto pos = internals.patients.find(self); - assert(pos != internals.patients.end()); - // Clearing the patients can cause more Python code to run, which - // can invalidate the iterator. Extract the vector of patients - // from the unordered_map first. - auto patients = std::move(pos->second); - internals.patients.erase(pos); + std::vector patients; + + with_internals([&](internals &internals) { + auto pos = internals.patients.find(self); + + if (pos == internals.patients.end()) { + pybind11_fail( + "FATAL: Internal consistency check failed: Invalid clear_patients() call."); + } + + // Clearing the patients can cause more Python code to run, which + // can invalidate the iterator. Extract the vector of patients + // from the unordered_map first. + patients = std::move(pos->second); + internals.patients.erase(pos); + }); + instance->has_patients = false; for (PyObject *&patient : patients) { Py_CLEAR(patient); @@ -423,6 +470,8 @@ inline void clear_instance(PyObject *self) { if (instance->owned || v_h.holder_constructed()) { v_h.type->dealloc(v_h); } + } else if (v_h.holder_constructed()) { + v_h.type->dealloc(v_h); // Disowned instance. } } // Deallocate the value/holder layout internals: @@ -454,27 +503,33 @@ extern "C" inline void pybind11_object_dealloc(PyObject *self) { PyObject_GC_UnTrack(self); } +#if PY_VERSION_HEX >= 0x030D0000 + // PyObject_ClearManagedDict() is available from Python 3.13+. It must be + // called before tp_free() because on Python 3.14+ tp_free no longer + // implicitly clears the managed dict, which would abandon the refcounts of + // objects stored in __dict__ of py::dynamic_attr() types, causing permanent + // memory leaks. + if (PyType_HasFeature(type, Py_TPFLAGS_MANAGED_DICT)) { + PyObject_ClearManagedDict(self); + } +#endif + clear_instance(self); type->tp_free(self); -#if PY_VERSION_HEX < 0x03080000 - // `type->tp_dealloc != pybind11_object_dealloc` means that we're being called - // as part of a derived type's dealloc, in which case we're not allowed to decref - // the type here. For cross-module compatibility, we shouldn't compare directly - // with `pybind11_object_dealloc`, but with the common one stashed in internals. - auto pybind11_object_type = (PyTypeObject *) get_internals().instance_base; - if (type->tp_dealloc == pybind11_object_type->tp_dealloc) - Py_DECREF(type); -#else // This was not needed before Python 3.8 (Python issue 35810) // https://github.com/pybind/pybind11/issues/1946 Py_DECREF(type); -#endif } +PYBIND11_WARNING_PUSH +PYBIND11_WARNING_DISABLE_GCC("-Wredundant-decls") + std::string error_string(); +PYBIND11_WARNING_POP + /** Create the type which can be used as a common base for all classes. This is needed in order to satisfy Python's requirements for multiple inheritance. Return value: New reference. */ @@ -486,7 +541,7 @@ inline PyObject *make_object_base_type(PyTypeObject *metaclass) { issue no Python C API calls which could potentially invoke the garbage collector (the GC will call type_traverse(), which will in turn find the newly constructed type in an invalid state) */ - auto *heap_type = (PyHeapTypeObject *) metaclass->tp_alloc(metaclass, 0); + auto *heap_type = reinterpret_cast(metaclass->tp_alloc(metaclass, 0)); if (!heap_type) { pybind11_fail("make_object_base_type(): error allocating type!"); } @@ -513,17 +568,24 @@ inline PyObject *make_object_base_type(PyTypeObject *metaclass) { pybind11_fail("PyType_Ready failed in make_object_base_type(): " + error_string()); } - setattr((PyObject *) type, "__module__", str("pybind11_builtins")); + setattr(reinterpret_cast(type), "__module__", str(PYBIND11_DUMMY_MODULE_NAME)); PYBIND11_SET_OLDPY_QUALNAME(type, name_obj); assert(!PyType_HasFeature(type, Py_TPFLAGS_HAVE_GC)); - return (PyObject *) heap_type; + return reinterpret_cast(heap_type); } /// dynamic_attr: Allow the garbage collector to traverse the internal instance `__dict__`. extern "C" inline int pybind11_traverse(PyObject *self, visitproc visit, void *arg) { +#if PY_VERSION_HEX >= 0x030D0000 + int ret = PyObject_VisitManagedDict(self, visit, arg); + if (ret) { + return ret; + } +#else PyObject *&dict = *_PyObject_GetDictPtr(self); Py_VISIT(dict); +#endif // https://docs.python.org/3/c-api/typeobj.html#c.PyTypeObject.tp_traverse #if PY_VERSION_HEX >= 0x03090000 Py_VISIT(Py_TYPE(self)); @@ -533,8 +595,12 @@ extern "C" inline int pybind11_traverse(PyObject *self, visitproc visit, void *a /// dynamic_attr: Allow the GC to clear the dictionary. extern "C" inline int pybind11_clear(PyObject *self) { +#if PY_VERSION_HEX >= 0x030D0000 + PyObject_ClearManagedDict(self); +#else PyObject *&dict = *_PyObject_GetDictPtr(self); Py_CLEAR(dict); +#endif return 0; } @@ -542,7 +608,7 @@ extern "C" inline int pybind11_clear(PyObject *self) { inline void enable_dynamic_attributes(PyHeapTypeObject *heap_type) { auto *type = &heap_type->ht_type; type->tp_flags |= Py_TPFLAGS_HAVE_GC; -#if PY_VERSION_HEX < 0x030B0000 +#ifdef PYBIND11_BACKWARD_COMPATIBILITY_TP_DICTOFFSET type->tp_dictoffset = type->tp_basicsize; // place dict at the end type->tp_basicsize += (ssize_t) sizeof(PyObject *); // and allocate enough space for it #else @@ -551,17 +617,9 @@ inline void enable_dynamic_attributes(PyHeapTypeObject *heap_type) { type->tp_traverse = pybind11_traverse; type->tp_clear = pybind11_clear; - static PyGetSetDef getset[] = {{ -#if PY_VERSION_HEX < 0x03070000 - const_cast("__dict__"), -#else - "__dict__", -#endif - PyObject_GenericGetDict, - PyObject_GenericSetDict, - nullptr, - nullptr}, - {nullptr, nullptr, nullptr, nullptr, nullptr}}; + static PyGetSetDef getset[] + = {{"__dict__", PyObject_GenericGetDict, PyObject_GenericSetDict, nullptr, nullptr}, + {nullptr, nullptr, nullptr, nullptr, nullptr}}; type->tp_getset = getset; } @@ -579,35 +637,89 @@ extern "C" inline int pybind11_getbuffer(PyObject *obj, Py_buffer *view, int fla if (view) { view->obj = nullptr; } - PyErr_SetString(PyExc_BufferError, "pybind11_getbuffer(): Internal error"); + set_error(PyExc_BufferError, "pybind11_getbuffer(): Internal error"); return -1; } std::memset(view, 0, sizeof(Py_buffer)); - buffer_info *info = tinfo->get_buffer(obj, tinfo->get_buffer_data); + std::unique_ptr info = nullptr; + try { + info.reset(tinfo->get_buffer(obj, tinfo->get_buffer_data)); + } catch (...) { + try_translate_exceptions(); + raise_from(PyExc_BufferError, "Error getting buffer"); + return -1; + } + if (info == nullptr) { + pybind11_fail("FATAL UNEXPECTED SITUATION: tinfo->get_buffer() returned nullptr."); + } + if ((flags & PyBUF_WRITABLE) == PyBUF_WRITABLE && info->readonly) { - delete info; // view->obj = nullptr; // Was just memset to 0, so not necessary - PyErr_SetString(PyExc_BufferError, "Writable buffer requested for readonly storage"); + set_error(PyExc_BufferError, "Writable buffer requested for readonly storage"); return -1; } - view->obj = obj; - view->ndim = 1; - view->internal = info; - view->buf = info->ptr; + + // Fill in all the information, and then downgrade as requested by the caller, or raise an + // error if that's not possible. view->itemsize = info->itemsize; view->len = view->itemsize; for (auto s : info->shape) { view->len *= s; } + view->ndim = static_cast(info->ndim); + view->shape = info->shape.data(); + view->strides = info->strides.data(); view->readonly = static_cast(info->readonly); if ((flags & PyBUF_FORMAT) == PyBUF_FORMAT) { view->format = const_cast(info->format.c_str()); } - if ((flags & PyBUF_STRIDES) == PyBUF_STRIDES) { - view->ndim = (int) info->ndim; - view->strides = info->strides.data(); - view->shape = info->shape.data(); + + // Note, all contiguity flags imply PyBUF_STRIDES and lower. + if ((flags & PyBUF_C_CONTIGUOUS) == PyBUF_C_CONTIGUOUS) { + if (PyBuffer_IsContiguous(view, 'C') == 0) { + std::memset(view, 0, sizeof(Py_buffer)); + set_error(PyExc_BufferError, + "C-contiguous buffer requested for discontiguous storage"); + return -1; + } + } else if ((flags & PyBUF_F_CONTIGUOUS) == PyBUF_F_CONTIGUOUS) { + if (PyBuffer_IsContiguous(view, 'F') == 0) { + std::memset(view, 0, sizeof(Py_buffer)); + set_error(PyExc_BufferError, + "Fortran-contiguous buffer requested for discontiguous storage"); + return -1; + } + } else if ((flags & PyBUF_ANY_CONTIGUOUS) == PyBUF_ANY_CONTIGUOUS) { + if (PyBuffer_IsContiguous(view, 'A') == 0) { + std::memset(view, 0, sizeof(Py_buffer)); + set_error(PyExc_BufferError, "Contiguous buffer requested for discontiguous storage"); + return -1; + } + + } else if ((flags & PyBUF_STRIDES) != PyBUF_STRIDES) { + // If no strides are requested, the buffer must be C-contiguous. + // https://docs.python.org/3/c-api/buffer.html#contiguity-requests + if (PyBuffer_IsContiguous(view, 'C') == 0) { + std::memset(view, 0, sizeof(Py_buffer)); + set_error(PyExc_BufferError, + "C-contiguous buffer requested for discontiguous storage"); + return -1; + } + + view->strides = nullptr; + + // Since this is a contiguous buffer, it can also pretend to be 1D. + if ((flags & PyBUF_ND) != PyBUF_ND) { + view->shape = nullptr; + view->ndim = 0; + } } + + // Set these after all checks so they don't leak out into the caller, and can be automatically + // cleaned up on error. + view->buf = info->ptr; + view->internal = info.release(); + view->obj = obj; Py_INCREF(view->obj); return 0; } @@ -636,15 +748,7 @@ inline PyObject *make_new_python_type(const type_record &rec) { PyUnicode_FromFormat("%U.%U", rec.scope.attr("__qualname__").ptr(), name.ptr())); } - object module_; - if (rec.scope) { - if (hasattr(rec.scope, "__module__")) { - module_ = rec.scope.attr("__module__"); - } else if (hasattr(rec.scope, "__name__")) { - module_ = rec.scope.attr("__name__"); - } - } - + object module_ = get_module_name_if_available(rec.scope); const auto *full_name = c_str( #if !defined(PYPY_VERSION) module_ ? str(module_).cast() + "." + rec.name : @@ -653,10 +757,13 @@ inline PyObject *make_new_python_type(const type_record &rec) { char *tp_doc = nullptr; if (rec.doc && options::show_user_defined_docstrings()) { - /* Allocate memory for docstring (using PyObject_MALLOC, since - Python will free this later on) */ + /* Allocate memory for docstring (Python will free this later on) */ size_t size = std::strlen(rec.doc) + 1; +#if PY_VERSION_HEX >= 0x030D0000 + tp_doc = static_cast(PyMem_MALLOC(size)); +#else tp_doc = (char *) PyObject_MALLOC(size); +#endif std::memcpy((void *) tp_doc, rec.doc, size); } @@ -668,10 +775,10 @@ inline PyObject *make_new_python_type(const type_record &rec) { issue no Python C API calls which could potentially invoke the garbage collector (the GC will call type_traverse(), which will in turn find the newly constructed type in an invalid state) */ - auto *metaclass - = rec.metaclass.ptr() ? (PyTypeObject *) rec.metaclass.ptr() : internals.default_metaclass; + auto *metaclass = rec.metaclass.ptr() ? reinterpret_cast(rec.metaclass.ptr()) + : internals.default_metaclass; - auto *heap_type = (PyHeapTypeObject *) metaclass->tp_alloc(metaclass, 0); + auto *heap_type = reinterpret_cast(metaclass->tp_alloc(metaclass, 0)); if (!heap_type) { pybind11_fail(std::string(rec.name) + ": Unable to create type object!"); } @@ -684,7 +791,7 @@ inline PyObject *make_new_python_type(const type_record &rec) { auto *type = &heap_type->ht_type; type->tp_name = full_name; type->tp_doc = tp_doc; - type->tp_base = type_incref((PyTypeObject *) base); + type->tp_base = type_incref(reinterpret_cast(base)); type->tp_basicsize = static_cast(sizeof(instance)); if (!bases.empty()) { type->tp_bases = bases.release().ptr(); @@ -725,18 +832,18 @@ inline PyObject *make_new_python_type(const type_record &rec) { /* Register type with the parent scope */ if (rec.scope) { - setattr(rec.scope, rec.name, (PyObject *) type); + setattr(rec.scope, rec.name, reinterpret_cast(type)); } else { Py_INCREF(type); // Keep it alive forever (reference leak) } if (module_) { // Needed by pydoc - setattr((PyObject *) type, "__module__", module_); + setattr(reinterpret_cast(type), "__module__", module_); } PYBIND11_SET_OLDPY_QUALNAME(type, qualname); - return (PyObject *) type; + return reinterpret_cast(type); } PYBIND11_NAMESPACE_END(detail) diff --git a/external_libraries/pybind11/include/pybind11/detail/common.h b/external_libraries/pybind11/include/pybind11/detail/common.h index 31a54c77..38cb01d7 100644 --- a/external_libraries/pybind11/include/pybind11/detail/common.h +++ b/external_libraries/pybind11/include/pybind11/detail/common.h @@ -9,88 +9,42 @@ #pragma once -#define PYBIND11_VERSION_MAJOR 2 -#define PYBIND11_VERSION_MINOR 11 -#define PYBIND11_VERSION_PATCH 1 - -// Similar to Python's convention: https://docs.python.org/3/c-api/apiabiversion.html -// Additional convention: 0xD = dev -#define PYBIND11_VERSION_HEX 0x020B0100 - -// Define some generic pybind11 helper macros for warning management. -// -// Note that compiler-specific push/pop pairs are baked into the -// PYBIND11_NAMESPACE_BEGIN/PYBIND11_NAMESPACE_END pair of macros. Therefore manual -// PYBIND11_WARNING_PUSH/PYBIND11_WARNING_POP are usually only needed in `#include` sections. -// -// If you find you need to suppress a warning, please try to make the suppression as local as -// possible using these macros. Please also be sure to push/pop with the pybind11 macros. Please -// only use compiler specifics if you need to check specific versions, e.g. Apple Clang vs. vanilla -// Clang. -#if defined(_MSC_VER) -# define PYBIND11_COMPILER_MSVC -# define PYBIND11_PRAGMA(...) __pragma(__VA_ARGS__) -# define PYBIND11_WARNING_PUSH PYBIND11_PRAGMA(warning(push)) -# define PYBIND11_WARNING_POP PYBIND11_PRAGMA(warning(pop)) -#elif defined(__INTEL_COMPILER) -# define PYBIND11_COMPILER_INTEL -# define PYBIND11_PRAGMA(...) _Pragma(#__VA_ARGS__) -# define PYBIND11_WARNING_PUSH PYBIND11_PRAGMA(warning push) -# define PYBIND11_WARNING_POP PYBIND11_PRAGMA(warning pop) -#elif defined(__clang__) -# define PYBIND11_COMPILER_CLANG -# define PYBIND11_PRAGMA(...) _Pragma(#__VA_ARGS__) -# define PYBIND11_WARNING_PUSH PYBIND11_PRAGMA(clang diagnostic push) -# define PYBIND11_WARNING_POP PYBIND11_PRAGMA(clang diagnostic push) -#elif defined(__GNUC__) -# define PYBIND11_COMPILER_GCC -# define PYBIND11_PRAGMA(...) _Pragma(#__VA_ARGS__) -# define PYBIND11_WARNING_PUSH PYBIND11_PRAGMA(GCC diagnostic push) -# define PYBIND11_WARNING_POP PYBIND11_PRAGMA(GCC diagnostic pop) -#endif - -#ifdef PYBIND11_COMPILER_MSVC -# define PYBIND11_WARNING_DISABLE_MSVC(name) PYBIND11_PRAGMA(warning(disable : name)) -#else -# define PYBIND11_WARNING_DISABLE_MSVC(name) +#include +#if PY_VERSION_HEX < 0x03080000 +# error "PYTHON < 3.8 IS UNSUPPORTED. pybind11 v2.13 was the last to support Python 3.7." #endif -#ifdef PYBIND11_COMPILER_CLANG -# define PYBIND11_WARNING_DISABLE_CLANG(name) PYBIND11_PRAGMA(clang diagnostic ignored name) -#else -# define PYBIND11_WARNING_DISABLE_CLANG(name) -#endif - -#ifdef PYBIND11_COMPILER_GCC -# define PYBIND11_WARNING_DISABLE_GCC(name) PYBIND11_PRAGMA(GCC diagnostic ignored name) -#else -# define PYBIND11_WARNING_DISABLE_GCC(name) -#endif - -#ifdef PYBIND11_COMPILER_INTEL -# define PYBIND11_WARNING_DISABLE_INTEL(name) PYBIND11_PRAGMA(warning disable name) -#else -# define PYBIND11_WARNING_DISABLE_INTEL(name) +// Similar to Python's convention: https://docs.python.org/3/c-api/apiabiversion.html +// See also: https://github.com/python/cpython/blob/HEAD/Include/patchlevel.h +/* -- start version constants -- */ +#define PYBIND11_VERSION_MAJOR 3 +#define PYBIND11_VERSION_MINOR 0 +#define PYBIND11_VERSION_MICRO 4 +// ALPHA = 0xA, BETA = 0xB, GAMMA = 0xC (release candidate), FINAL = 0xF (stable release) +// - The release level is set to "alpha" for development versions. +// Use 0xA0 (LEVEL=0xA, SERIAL=0) for development versions. +// - For stable releases, set the serial to 0. +#define PYBIND11_VERSION_RELEASE_LEVEL PY_RELEASE_LEVEL_FINAL +#define PYBIND11_VERSION_RELEASE_SERIAL 0 +// String version of (micro, release level, release serial), e.g.: 0a0, 0b1, 0rc1, 0 +#define PYBIND11_VERSION_PATCH 4 +/* -- end version constants -- */ + +#if !defined(Py_PACK_FULL_VERSION) +// Stable API since Python 3.14.0a4 +# define Py_PACK_FULL_VERSION(X, Y, Z, LEVEL, SERIAL) \ + ((((X) & 0xff) << 24) | (((Y) & 0xff) << 16) | (((Z) & 0xff) << 8) \ + | (((LEVEL) & 0xf) << 4) | (((SERIAL) & 0xf) << 0)) #endif +// Version as a single 4-byte hex number, e.g. 0x030C04B5 == 3.12.4b5. +#define PYBIND11_VERSION_HEX \ + Py_PACK_FULL_VERSION(PYBIND11_VERSION_MAJOR, \ + PYBIND11_VERSION_MINOR, \ + PYBIND11_VERSION_MICRO, \ + PYBIND11_VERSION_RELEASE_LEVEL, \ + PYBIND11_VERSION_RELEASE_SERIAL) -#define PYBIND11_NAMESPACE_BEGIN(name) \ - namespace name { \ - PYBIND11_WARNING_PUSH - -#define PYBIND11_NAMESPACE_END(name) \ - PYBIND11_WARNING_POP \ - } - -// Robust support for some features and loading modules compiled against different pybind versions -// requires forcing hidden visibility on pybind code, so we enforce this by setting the attribute -// on the main `pybind11` namespace. -#if !defined(PYBIND11_NAMESPACE) -# ifdef __GNUG__ -# define PYBIND11_NAMESPACE pybind11 __attribute__((visibility("hidden"))) -# else -# define PYBIND11_NAMESPACE pybind11 -# endif -#endif +#include "pybind11_namespace_macros.h" #if !(defined(_MSC_VER) && __cplusplus == 199711L) # if __cplusplus >= 201402L @@ -118,6 +72,41 @@ # endif #endif +// These PYBIND11_HAS_... macros are consolidated in pybind11/detail/common.h +// to simplify backward compatibility handling for users (e.g., via #ifdef checks): +#define PYBIND11_HAS_TYPE_CASTER_STD_FUNCTION_SPECIALIZATIONS 1 +#define PYBIND11_HAS_INTERNALS_WITH_SMART_HOLDER_SUPPORT 1 +#define PYBIND11_HAS_CPP_CONDUIT 1 +#define PYBIND11_HAS_NATIVE_ENUM 1 + +#if defined(PYBIND11_CPP17) && defined(__has_include) +# if __has_include() +# define PYBIND11_HAS_FILESYSTEM 1 +# elif __has_include() +# define PYBIND11_HAS_EXPERIMENTAL_FILESYSTEM 1 +# endif +#endif + +#if defined(__cpp_lib_launder) && !(defined(_MSC_VER) && (_MSC_VER < 1920)) // See PR #5968 +# define PYBIND11_STD_LAUNDER std::launder +# define PYBIND11_HAS_STD_LAUNDER 1 +#else +# define PYBIND11_STD_LAUNDER +# define PYBIND11_HAS_STD_LAUNDER 0 +#endif + +#if defined(PYBIND11_CPP20) +# define PYBIND11_CONSTINIT constinit +# define PYBIND11_DTOR_CONSTEXPR constexpr +#else +# define PYBIND11_CONSTINIT +# define PYBIND11_DTOR_CONSTEXPR +#endif + +#if defined(PYBIND11_CPP20) && defined(__has_include) && __has_include() +# define PYBIND11_HAS_STD_BARRIER 1 +#endif + // Compiler version assertions #if defined(__INTEL_COMPILER) # if __INTEL_COMPILER < 1800 @@ -156,14 +145,6 @@ # endif #endif -#if !defined(PYBIND11_EXPORT_EXCEPTION) -# if defined(__apple_build_version__) -# define PYBIND11_EXPORT_EXCEPTION PYBIND11_EXPORT -# else -# define PYBIND11_EXPORT_EXCEPTION -# 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`. @@ -186,6 +167,14 @@ # define PYBIND11_NOINLINE __attribute__((noinline)) inline #endif +#if defined(_MSC_VER) +# define PYBIND11_ALWAYS_INLINE __forceinline +#elif defined(__GNUC__) +# define PYBIND11_ALWAYS_INLINE __attribute__((__always_inline__)) inline +#else +# define PYBIND11_ALWAYS_INLINE inline +#endif + #if defined(__MINGW32__) // For unknown reasons all PYBIND11_DEPRECATED member trigger a warning when declared // whether it is used or not @@ -204,31 +193,6 @@ # define PYBIND11_MAYBE_UNUSED __attribute__((__unused__)) #endif -/* Don't let Python.h #define (v)snprintf as macro because they are implemented - properly in Visual Studio since 2015. */ -#if defined(_MSC_VER) -# define HAVE_SNPRINTF 1 -#endif - -/// Include Python header, disable linking to pythonX_d.lib on Windows in debug mode -#if defined(_MSC_VER) -PYBIND11_WARNING_PUSH -PYBIND11_WARNING_DISABLE_MSVC(4505) -// C4505: 'PySlice_GetIndicesEx': unreferenced local function has been removed (PyPy only) -# if defined(_DEBUG) && !defined(Py_DEBUG) -// Workaround for a VS 2022 issue. -// NOTE: This workaround knowingly violates the Python.h include order requirement: -// https://docs.python.org/3/c-api/intro.html#include-files -// See https://github.com/pybind/pybind11/pull/3497 for full context. -# include -# if _MSVC_STL_VERSION >= 143 -# include -# endif -# define PYBIND11_DEBUG_MARKER -# undef _DEBUG -# endif -#endif - // https://en.cppreference.com/w/c/chrono/localtime #if defined(__STDC_LIB_EXT1__) && !defined(__STDC_WANT_LIB_EXT1__) # define __STDC_WANT_LIB_EXT1__ @@ -253,53 +217,15 @@ PYBIND11_WARNING_DISABLE_MSVC(4505) # define PYBIND11_HAS_VARIANT 1 #endif -#if defined(PYBIND11_CPP17) -# if defined(__has_include) -# if __has_include() -# define PYBIND11_HAS_STRING_VIEW -# endif -# elif defined(_MSC_VER) -# define PYBIND11_HAS_STRING_VIEW -# endif +#if defined(PYBIND11_CPP17) \ + && ((defined(__has_include) && __has_include()) || defined(_MSC_VER)) +# define PYBIND11_HAS_STRING_VIEW 1 #endif -#include -// Reminder: WITH_THREAD is always defined if PY_VERSION_HEX >= 0x03070000 -#if PY_VERSION_HEX < 0x03060000 -# error "PYTHON < 3.6 IS UNSUPPORTED. pybind11 v2.9 was the last to support Python 2 and 3.5." -#endif -#include -#include - -/* Python #defines overrides on all sorts of core functions, which - tends to weak havok in C++ codebases that expect these to work - like regular functions (potentially with several overloads) */ -#if defined(isalnum) -# undef isalnum -# undef isalpha -# undef islower -# undef isspace -# undef isupper -# undef tolower -# undef toupper -#endif - -#if defined(copysign) -# undef copysign -#endif - -#if defined(PYPY_VERSION) && !defined(PYBIND11_SIMPLE_GIL_MANAGEMENT) +#if (defined(PYPY_VERSION) || defined(GRAALVM_PYTHON)) && !defined(PYBIND11_SIMPLE_GIL_MANAGEMENT) # define PYBIND11_SIMPLE_GIL_MANAGEMENT #endif -#if defined(_MSC_VER) -# if defined(PYBIND11_DEBUG_MARKER) -# define _DEBUG -# undef PYBIND11_DEBUG_MARKER -# endif -PYBIND11_WARNING_POP -#endif - #include #include #include @@ -318,9 +244,24 @@ PYBIND11_WARNING_POP # endif #endif +// For libc++, the exceptions should be exported, +// otherwise, the exception translation would be incorrect. +// IMPORTANT: This code block must stay BELOW the #include above (see PR #5390). +#if !defined(PYBIND11_EXPORT_EXCEPTION) +# if defined(_LIBCPP_EXCEPTION) +# define PYBIND11_EXPORT_EXCEPTION PYBIND11_EXPORT +# else +# define PYBIND11_EXPORT_EXCEPTION +# endif +#endif + // Must be after including or one of the other headers specified by the standard #if defined(__cpp_lib_char8_t) && __cpp_lib_char8_t >= 201811L -# define PYBIND11_HAS_U8STRING +# define PYBIND11_HAS_U8STRING 1 +#endif + +#if defined(PYBIND11_CPP20) && defined(__cpp_lib_span) && __cpp_lib_span >= 202002L +# define PYBIND11_HAS_SPAN 1 #endif // See description of PR #4246: @@ -329,6 +270,50 @@ PYBIND11_WARNING_POP # define PYBIND11_ASSERT_GIL_HELD_INCREF_DECREF #endif +// Slightly faster code paths are available when PYBIND11_HAS_SUBINTERPRETER_SUPPORT is *not* +// defined, so avoid defining it for implementations that do not support subinterpreters. However, +// defining it unnecessarily is not expected to break anything. +// This can be overridden by the user with -DPYBIND11_HAS_SUBINTERPRETER_SUPPORT=1 or 0 +#ifndef PYBIND11_HAS_SUBINTERPRETER_SUPPORT +# if PY_VERSION_HEX >= 0x030C0000 && !defined(PYPY_VERSION) && !defined(GRAALVM_PYTHON) +# define PYBIND11_HAS_SUBINTERPRETER_SUPPORT 1 +# endif +#else +# if PYBIND11_HAS_SUBINTERPRETER_SUPPORT == 0 +# undef PYBIND11_HAS_SUBINTERPRETER_SUPPORT +# endif +#endif + +// 3.13 Compatibility +#if 0x030D0000 <= PY_VERSION_HEX +# define PYBIND11_TYPE_IS_TYPE_HINT "typing.TypeIs" +# define PYBIND11_CAPSULE_TYPE_TYPE_HINT "types.CapsuleType" +#else +# define PYBIND11_TYPE_IS_TYPE_HINT "typing_extensions.TypeIs" +# define PYBIND11_CAPSULE_TYPE_TYPE_HINT "typing_extensions.CapsuleType" +#endif + +// 3.12 Compatibility +#if 0x030C0000 <= PY_VERSION_HEX +# define PYBIND11_BUFFER_TYPE_HINT "collections.abc.Buffer" +#else +# define PYBIND11_BUFFER_TYPE_HINT "typing_extensions.Buffer" +#endif + +// 3.11 Compatibility +#if 0x030B0000 <= PY_VERSION_HEX +# define PYBIND11_NEVER_TYPE_HINT "typing.Never" +#else +# define PYBIND11_NEVER_TYPE_HINT "typing_extensions.Never" +#endif + +// 3.10 Compatibility +#if 0x030A0000 <= PY_VERSION_HEX +# define PYBIND11_TYPE_GUARD_TYPE_HINT "typing.TypeGuard" +#else +# define PYBIND11_TYPE_GUARD_TYPE_HINT "typing_extensions.TypeGuard" +#endif + // #define PYBIND11_STR_LEGACY_PERMISSIVE // If DEFINED, pybind11::str can hold PyUnicodeObject or PyBytesObject // (probably surprising and never documented, but this was the @@ -353,6 +338,13 @@ PYBIND11_WARNING_POP #define PYBIND11_BYTES_AS_STRING PyBytes_AsString #define PYBIND11_BYTES_SIZE PyBytes_Size #define PYBIND11_LONG_CHECK(o) PyLong_Check(o) +// In PyPy 7.3.3, `PyIndex_Check` is implemented by calling `__index__`, +// while CPython only considers the existence of `nb_index`/`__index__`. +#if !defined(PYPY_VERSION) +# define PYBIND11_INDEX_CHECK(o) PyIndex_Check(o) +#else +# define PYBIND11_INDEX_CHECK(o) hasattr(o, "__index__") +#endif #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)) @@ -366,15 +358,35 @@ PYBIND11_WARNING_POP #define PYBIND11_BUILTINS_MODULE "builtins" // Providing a separate declaration to make Clang's -Wmissing-prototypes happy. // See comment for PYBIND11_MODULE below for why this is marked "maybe unused". +#define PYBIND11_PLUGIN_DECL(name) \ + extern "C" PYBIND11_MAYBE_UNUSED PYBIND11_EXPORT PyObject *PyInit_##name(); #define PYBIND11_PLUGIN_IMPL(name) \ - extern "C" PYBIND11_MAYBE_UNUSED PYBIND11_EXPORT PyObject *PyInit_##name(); \ + PYBIND11_PLUGIN_DECL(name) \ extern "C" PYBIND11_EXPORT PyObject *PyInit_##name() #define PYBIND11_TRY_NEXT_OVERLOAD ((PyObject *) 1) // special failure return code #define PYBIND11_STRINGIFY(x) #x #define PYBIND11_TOSTRING(x) PYBIND11_STRINGIFY(x) #define PYBIND11_CONCAT(first, second) first##second -#define PYBIND11_ENSURE_INTERNALS_READY pybind11::detail::get_internals(); +#define PYBIND11_ENSURE_INTERNALS_READY \ + { \ + pybind11::detail::get_internals_pp_manager().unref(); \ + pybind11::detail::get_internals(); \ + } + +#if !defined(GRAALVM_PYTHON) +# define PYBIND11_PYCFUNCTION_GET_DOC(func) ((func)->m_ml->ml_doc) +# define PYBIND11_PYCFUNCTION_SET_DOC(func, doc) \ + do { \ + (func)->m_ml->ml_doc = (doc); \ + } while (0) +#else +# define PYBIND11_PYCFUNCTION_GET_DOC(func) (GraalPyCFunction_GetDoc((PyObject *) (func))) +# define PYBIND11_PYCFUNCTION_SET_DOC(func, doc) \ + do { \ + GraalPyCFunction_SetDoc((PyObject *) (func), (doc)); \ + } while (0) +#endif #define PYBIND11_CHECK_PYTHON_VERSION \ { \ @@ -396,11 +408,9 @@ PYBIND11_WARNING_POP #define PYBIND11_CATCH_INIT_EXCEPTIONS \ catch (pybind11::error_already_set & e) { \ pybind11::raise_from(e, PyExc_ImportError, "initialization failed"); \ - return nullptr; \ } \ catch (const std::exception &e) { \ - PyErr_SetString(PyExc_ImportError, e.what()); \ - return nullptr; \ + ::pybind11::set_error(PyExc_ImportError, e.what()); \ } /** \rst @@ -428,14 +438,68 @@ PYBIND11_WARNING_POP return pybind11_init(); \ } \ PYBIND11_CATCH_INIT_EXCEPTIONS \ + return nullptr; \ } \ PyObject *pybind11_init() +// this push is for the next several macros +PYBIND11_WARNING_PUSH +PYBIND11_WARNING_DISABLE_CLANG("-Wgnu-zero-variadic-macro-arguments") + +/** +Create a PyInit_ function for this module. + +Note that this is run once for each (sub-)interpreter the module is imported into, including +possibly concurrently. The PyModuleDef is allowed to be static, but the PyObject* resulting from +PyModuleDef_Init should be treated like any other PyObject (so not shared across interpreters). + */ +#define PYBIND11_MODULE_PYINIT(name, ...) \ + static int PYBIND11_CONCAT(pybind11_exec_, name)(PyObject *); \ + PYBIND11_PLUGIN_IMPL(name) { \ + PYBIND11_CHECK_PYTHON_VERSION \ + try { \ + pybind11::detail::ensure_internals(); \ + static ::pybind11::detail::slots_array mod_def_slots \ + = ::pybind11::detail::init_slots(&PYBIND11_CONCAT(pybind11_exec_, name), \ + ##__VA_ARGS__); \ + static PyModuleDef def{/* m_base */ PyModuleDef_HEAD_INIT, \ + /* m_name */ PYBIND11_TOSTRING(name), \ + /* m_doc */ nullptr, \ + /* m_size */ 0, \ + /* m_methods */ nullptr, \ + /* m_slots */ mod_def_slots.data(), \ + /* m_traverse */ nullptr, \ + /* m_clear */ nullptr, \ + /* m_free */ nullptr}; \ + return PyModuleDef_Init(&def); \ + } \ + PYBIND11_CATCH_INIT_EXCEPTIONS \ + return nullptr; \ + } + +#define PYBIND11_MODULE_EXEC(name, variable) \ + static void PYBIND11_CONCAT(pybind11_init_, name)(::pybind11::module_ &); \ + int PYBIND11_CONCAT(pybind11_exec_, name)(PyObject * pm) { \ + try { \ + pybind11::detail::ensure_internals(); \ + auto m = pybind11::reinterpret_borrow<::pybind11::module_>(pm); \ + if (!pybind11::detail::get_cached_module(m.attr("__spec__").attr("name"))) { \ + PYBIND11_CONCAT(pybind11_init_, name)(m); \ + pybind11::detail::cache_completed_module(m); \ + } \ + return 0; \ + } \ + PYBIND11_CATCH_INIT_EXCEPTIONS \ + return -1; \ + } \ + void PYBIND11_CONCAT(pybind11_init_, name)(::pybind11::module_ \ + & variable) // NOLINT(bugprone-macro-parentheses) + /** \rst This macro creates the entry point that will be invoked when the Python interpreter imports an extension module. The module name is given as the first argument and it should not be in quotes. The second macro argument defines a variable of type - `py::module_` which can be used to initialize the module. + ``py::module_`` which can be used to initialize the module. The entry point is marked as "maybe unused" to aid dead-code detection analysis: since the entry point is typically only looked up at runtime and not referenced @@ -451,24 +515,31 @@ PYBIND11_WARNING_POP return "Hello, World!"; }); } + + The third and subsequent macro arguments are optional (available since 2.13.0), and + can be used to mark the extension module as supporting various Python features. + + - ``mod_gil_not_used()`` + - ``multiple_interpreters::per_interpreter_gil()`` + - ``multiple_interpreters::shared_gil()`` + - ``multiple_interpreters::not_supported()`` + + .. code-block:: cpp + + PYBIND11_MODULE(example, m, py::mod_gil_not_used()) { + m.doc() = "pybind11 example module safe to run without the GIL"; + m.def("foo", []() { + return "Hello, Free-threaded World!"; + }); + } + \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, ...) \ + PYBIND11_MODULE_PYINIT(name, ##__VA_ARGS__) \ + PYBIND11_MODULE_EXEC(name, variable) + +// pop gnu-zero-variadic-macro-arguments +PYBIND11_WARNING_POP PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE) @@ -524,7 +595,7 @@ enum class return_value_policy : uint8_t { object without taking ownership similar to the above return_value_policy::reference policy. In contrast to that policy, the function or property's implicit this argument (called the parent) is - considered to be the the owner of the return value (the child). + considered to be the owner of the return value (the child). pybind11 then couples the lifetime of the parent to the child via a reference relationship that ensures that the parent cannot be garbage collected while Python is still using the child. More advanced @@ -535,14 +606,10 @@ enum class return_value_policy : uint8_t { PYBIND11_NAMESPACE_BEGIN(detail) -inline static constexpr int log2(size_t n, int k = 0) { - return (n <= 1) ? k : log2(n >> 1, k + 1); -} +static constexpr int log2(size_t n, int k = 0) { return (n <= 1) ? k : log2(n >> 1, k + 1); } // Returns the size as a multiple of sizeof(void *), rounded up. -inline static constexpr size_t size_in_ptrs(size_t s) { - return 1 + ((s - 1) >> log2(sizeof(void *))); -} +static constexpr size_t size_in_ptrs(size_t s) { return 1 + ((s - 1) >> log2(sizeof(void *))); } /** * The space to allocate for simple layout instance holders (see below) in multiple of the size of @@ -607,6 +674,8 @@ struct instance { bool simple_instance_registered : 1; /// If true, get_internals().patients has an entry for this object bool has_patients : 1; + /// If true, this Python object needs to be kept alive for the lifetime of the C++ value. + bool is_alias : 1; /// Initializes all of the above type/values/holders data (but not the instance values /// themselves) @@ -629,6 +698,14 @@ struct instance { static_assert(std::is_standard_layout::value, "Internal error: `pybind11::detail::instance` is not standard layout!"); +// Some older compilers (e.g. gcc 9.4.0) require +// static_assert(always_false::value, "..."); +// instead of +// static_assert(false, "..."); +// to trigger the static_assert() in a template only if it is actually instantiated. +template +struct always_false : std::false_type {}; + /// from __cpp_future__ import (convenient aliases from C++14/17) #if defined(PYBIND11_CPP14) using std::conditional_t; @@ -646,7 +723,7 @@ template using remove_reference_t = typename std::remove_reference::type; #endif -#if defined(PYBIND11_CPP20) +#if defined(PYBIND11_CPP20) && defined(__cpp_lib_remove_cvref) using std::remove_cvref; using std::remove_cvref_t; #else @@ -669,14 +746,49 @@ using std::make_index_sequence; #else template struct index_sequence {}; -template -struct make_index_sequence_impl : make_index_sequence_impl {}; -template -struct make_index_sequence_impl<0, S...> { +// Comments about the algorithm below. +// +// Credit: This is based on an algorithm by taocpp here: +// https://github.com/taocpp/sequences/blob/main/include/tao/seq/make_integer_sequence.hpp +// but significantly simplified. +// +// We build up a sequence S by repeatedly doubling its length and sometimes adding 1 to the end. +// E.g. if the current S is 0...3, then we either go to 0...7 or 0...8 on the next pass. +// The goal is to end with S = 0...N-1. +// The key insight is that the times we need to add an additional digit to S correspond +// exactly to the 1's in the binary representation of the number N. +// +// Invariants: +// - digit is a power of 2 +// - N_digit_is_1 is whether N's binary representation has a 1 in that digit's position. +// - end <= N +// - S is 0...end-1. +// - if digit > 0, end * digit * 2 <= N < (end+1) * digit * 2 +// +// The process starts with digit > N, end = 0, and S is empty. +// The process concludes with digit=0, in which case, end == N and S is 0...N-1. + +template // N_digit_is_1=false +struct make_index_sequence_impl + : make_index_sequence_impl { +}; +template +struct make_index_sequence_impl + : make_index_sequence_impl {}; +template +struct make_index_sequence_impl<0, false, N, end, S...> { using type = index_sequence; }; +constexpr size_t next_power_of_2(size_t N) { return N == 0 ? 1 : next_power_of_2(N >> 1) << 1; } template -using make_index_sequence = typename make_index_sequence_impl::type; +using make_index_sequence = + typename make_index_sequence_impl::type; #endif /// Make an index sequence of the indices of true arguments @@ -913,8 +1025,7 @@ using is_template_base_of = decltype(is_template_base_of_impl::check((intrinsic_t *) nullptr)); #else struct is_template_base_of - : decltype(is_template_base_of_impl::check((intrinsic_t *) nullptr)) { -}; + : decltype(is_template_base_of_impl::check((intrinsic_t *) nullptr)){}; #endif /// Check if T is an instantiation of the template `Class`. For example: @@ -928,6 +1039,17 @@ struct is_instantiation> : std::true_type {}; template using is_shared_ptr = is_instantiation; +/// Detects whether static_cast(Base*) is valid, i.e. the inheritance is non-virtual. +/// Used to detect virtual bases: if this is false, pointer adjustments require the implicit_casts +/// chain rather than reinterpret_cast. +template +struct is_static_downcastable : std::false_type {}; +template +struct is_static_downcastable(std::declval()))>> + : std::true_type {}; + /// Check if T looks like an input iterator template struct is_input_iterator : std::false_type {}; @@ -950,14 +1072,30 @@ struct strip_function_object { using type = typename remove_class::type; }; +// Strip noexcept from a free function type (C++17: noexcept is part of the type). +template +struct remove_noexcept { + using type = T; +}; +#ifdef __cpp_noexcept_function_type +template +struct remove_noexcept { + using type = R(A...); +}; +#endif +template +using remove_noexcept_t = typename remove_noexcept::type; + // Extracts the function signature from a function, function pointer or lambda. +// Strips noexcept from the result so that factory/pickle_factory partial specializations +// (which match plain Return(Args...)) work correctly with noexcept callables (issue #2234). template > -using function_signature_t = conditional_t< +using function_signature_t = remove_noexcept_t::value, F, typename conditional_t::value || std::is_member_pointer::value, std::remove_pointer, - strip_function_object>::type>; + strip_function_object>::type>>; /// Returns true if the type looks like a lambda: that is, isn't a function, pointer or member /// pointer. Note that this can catch all sorts of other things, too; this is intended to be used @@ -1106,6 +1244,36 @@ struct overload_cast_impl { -> decltype(pmf) { return pmf; } + + // Define const/non-const member-pointer selector pairs for qualifier combinations. + // The `qualifiers` parameter is used in type position, where extra parentheses are invalid. + // NOLINTBEGIN(bugprone-macro-parentheses) +#define PYBIND11_OVERLOAD_CAST_MEMBER_PTR(qualifiers) \ + template \ + constexpr auto operator()(Return (Class::*pmf)(Args...) qualifiers, std::false_type = {}) \ + const noexcept -> decltype(pmf) { \ + return pmf; \ + } \ + template \ + constexpr auto operator()(Return (Class::*pmf)(Args...) const qualifiers, std::true_type) \ + const noexcept -> decltype(pmf) { \ + return pmf; \ + } + PYBIND11_OVERLOAD_CAST_MEMBER_PTR(&) + PYBIND11_OVERLOAD_CAST_MEMBER_PTR(&&) + +#ifdef __cpp_noexcept_function_type + template + constexpr auto operator()(Return (*pf)(Args...) noexcept) const noexcept -> decltype(pf) { + return pf; + } + + PYBIND11_OVERLOAD_CAST_MEMBER_PTR(noexcept) + PYBIND11_OVERLOAD_CAST_MEMBER_PTR(& noexcept) + PYBIND11_OVERLOAD_CAST_MEMBER_PTR(&& noexcept) +#endif +#undef PYBIND11_OVERLOAD_CAST_MEMBER_PTR + // NOLINTEND(bugprone-macro-parentheses) }; PYBIND11_NAMESPACE_END(detail) @@ -1205,8 +1373,7 @@ template #if defined(_MSC_VER) && _MSC_VER < 1920 // MSVC 2017 constexpr #endif - inline void - silence_unused_warnings(Args &&...) { + inline void silence_unused_warnings(Args &&...) { } // MSVC warning C4100: Unreferenced formal parameter @@ -1251,5 +1418,10 @@ constexpr # define PYBIND11_DETAILED_ERROR_MESSAGES #endif +// CPython 3.11+ provides Py_TPFLAGS_MANAGED_DICT, but PyPy3.11 does not, see PR #5508. +#if PY_VERSION_HEX < 0x030B0000 || defined(PYPY_VERSION) +# define PYBIND11_BACKWARD_COMPATIBILITY_TP_DICTOFFSET +#endif + PYBIND11_NAMESPACE_END(detail) PYBIND11_NAMESPACE_END(PYBIND11_NAMESPACE) diff --git a/external_libraries/pybind11/include/pybind11/detail/cpp_conduit.h b/external_libraries/pybind11/include/pybind11/detail/cpp_conduit.h new file mode 100644 index 00000000..49c199e1 --- /dev/null +++ b/external_libraries/pybind11/include/pybind11/detail/cpp_conduit.h @@ -0,0 +1,75 @@ +// Copyright (c) 2024 The pybind Community. + +#pragma once + +#include + +#include "common.h" +#include "internals.h" + +#include + +PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE) +PYBIND11_NAMESPACE_BEGIN(detail) + +// Forward declaration needed here: Refactoring opportunity. +extern "C" inline PyObject *pybind11_object_new(PyTypeObject *type, PyObject *, PyObject *); + +inline bool type_is_managed_by_our_internals(PyTypeObject *type_obj) { +#if defined(PYPY_VERSION) + auto &internals = get_internals(); + return bool(internals.registered_types_py.find(type_obj) + != internals.registered_types_py.end()); +#else + return (type_obj->tp_new == pybind11_object_new); +#endif +} + +inline bool is_instance_method_of_type(PyTypeObject *type_obj, PyObject *attr_name) { + PyObject *descr = _PyType_Lookup(type_obj, attr_name); + return ((descr != nullptr) && PyInstanceMethod_Check(descr)); +} + +inline object try_get_cpp_conduit_method(PyObject *obj) { + if (PyType_Check(obj)) { + return object(); + } + PyTypeObject *type_obj = Py_TYPE(obj); + str attr_name("_pybind11_conduit_v1_"); + bool assumed_to_be_callable = false; + if (type_is_managed_by_our_internals(type_obj)) { + if (!is_instance_method_of_type(type_obj, attr_name.ptr())) { + return object(); + } + assumed_to_be_callable = true; + } + PyObject *method = PyObject_GetAttr(obj, attr_name.ptr()); + if (method == nullptr) { + PyErr_Clear(); + return object(); + } + if (!assumed_to_be_callable && PyCallable_Check(method) == 0) { + Py_DECREF(method); + return object(); + } + return reinterpret_steal(method); +} + +inline void *try_raw_pointer_ephemeral_from_cpp_conduit(handle src, + const std::type_info *cpp_type_info) { + object method = try_get_cpp_conduit_method(src.ptr()); + if (method) { + capsule cpp_type_info_capsule(const_cast(static_cast(cpp_type_info)), + typeid(std::type_info).name()); + object cpp_conduit = method(bytes(PYBIND11_PLATFORM_ABI_ID), + cpp_type_info_capsule, + bytes("raw_pointer_ephemeral")); + if (isinstance(cpp_conduit)) { + return reinterpret_borrow(cpp_conduit).get_pointer(); + } + } + return nullptr; +} + +PYBIND11_NAMESPACE_END(detail) +PYBIND11_NAMESPACE_END(PYBIND11_NAMESPACE) diff --git a/external_libraries/pybind11/include/pybind11/detail/descr.h b/external_libraries/pybind11/include/pybind11/detail/descr.h index 635614b0..701662c4 100644 --- a/external_libraries/pybind11/include/pybind11/detail/descr.h +++ b/external_libraries/pybind11/include/pybind11/detail/descr.h @@ -99,6 +99,26 @@ constexpr descr<1, Type> const_name() { return {'%'}; } +// Use a different name based on whether the parameter is used as input or output +template +constexpr descr io_name(char const (&text1)[N1], char const (&text2)[N2]) { + return const_name("@") + const_name(text1) + const_name("@") + const_name(text2) + + const_name("@"); +} + +// Ternary description for io_name (like the numeric type_caster) +template +constexpr enable_if_t> +io_name(char const (&text1)[N1], char const (&text2)[N2], char const (&)[N3], char const (&)[N4]) { + return io_name(text1, text2); +} + +template +constexpr enable_if_t> +io_name(char const (&)[N1], char const (&)[N2], char const (&text3)[N3], char const (&text4)[N4]) { + return io_name(text3, text4); +} + // If "_" is defined as a macro, py::detail::_ cannot be provided. // It is therefore best to use py::detail::const_name universally. // This block is for backward compatibility only. @@ -137,12 +157,24 @@ constexpr descr<1, Type> _() { #endif // #ifndef _ constexpr descr<0> concat() { return {}; } +constexpr descr<0> union_concat() { return {}; } template constexpr descr concat(const descr &descr) { return descr; } +template +constexpr descr union_concat(const descr &descr) { + return descr; +} + +template +constexpr descr operator|(const descr &a, + const descr &b) { + return a + const_name(" | ") + b; +} + #ifdef __cpp_fold_expressions template constexpr descr operator,(const descr &a, @@ -154,12 +186,25 @@ template constexpr auto concat(const descr &d, const Args &...args) { return (d, ..., args); } + +template +constexpr auto union_concat(const descr &d, const Args &...args) { + return (d | ... | args); +} + #else template constexpr auto concat(const descr &d, const Args &...args) -> decltype(std::declval>() + concat(args...)) { return d + const_name(", ") + concat(args...); } + +template +constexpr auto union_concat(const descr &d, const Args &...args) + -> decltype(std::declval>() + union_concat(args...)) { + return d + const_name(" | ") + union_concat(args...); +} + #endif template @@ -167,5 +212,15 @@ constexpr descr type_descr(const descr &descr) { return const_name("{") + descr + const_name("}"); } +template +constexpr descr arg_descr(const descr &descr) { + return const_name("@^") + descr + const_name("@!"); +} + +template +constexpr descr return_descr(const descr &descr) { + return const_name("@$") + descr + const_name("@!"); +} + PYBIND11_NAMESPACE_END(detail) PYBIND11_NAMESPACE_END(PYBIND11_NAMESPACE) diff --git a/external_libraries/pybind11/include/pybind11/detail/dynamic_raw_ptr_cast_if_possible.h b/external_libraries/pybind11/include/pybind11/detail/dynamic_raw_ptr_cast_if_possible.h new file mode 100644 index 00000000..7c00fe98 --- /dev/null +++ b/external_libraries/pybind11/include/pybind11/detail/dynamic_raw_ptr_cast_if_possible.h @@ -0,0 +1,39 @@ +// Copyright (c) 2021 The Pybind Development Team. +// All rights reserved. Use of this source code is governed by a +// BSD-style license that can be found in the LICENSE file. + +#pragma once + +#include "common.h" + +#include + +PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE) +PYBIND11_NAMESPACE_BEGIN(detail) + +template +struct dynamic_raw_ptr_cast_is_possible : std::false_type {}; + +template +struct dynamic_raw_ptr_cast_is_possible< + To, + From, + detail::enable_if_t::value && std::is_polymorphic::value>> + : std::true_type {}; + +template ::value, int> = 0> +To *dynamic_raw_ptr_cast_if_possible(From * /*ptr*/) { + return nullptr; +} + +template ::value, int> = 0> +To *dynamic_raw_ptr_cast_if_possible(From *ptr) { + return dynamic_cast(ptr); +} + +PYBIND11_NAMESPACE_END(detail) +PYBIND11_NAMESPACE_END(PYBIND11_NAMESPACE) diff --git a/external_libraries/pybind11/include/pybind11/detail/exception_translation.h b/external_libraries/pybind11/include/pybind11/detail/exception_translation.h new file mode 100644 index 00000000..22ae8a1c --- /dev/null +++ b/external_libraries/pybind11/include/pybind11/detail/exception_translation.h @@ -0,0 +1,71 @@ +/* + pybind11/detail/exception_translation.h: means to translate C++ exceptions to Python exceptions + + Copyright (c) 2024 The Pybind Development Team. + + All rights reserved. Use of this source code is governed by a + BSD-style license that can be found in the LICENSE file. +*/ + +#pragma once + +#include "common.h" +#include "internals.h" + +PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE) +PYBIND11_NAMESPACE_BEGIN(detail) + +// Apply all the extensions translators from a list +// Return true if one of the translators completed without raising an exception +// itself. Return of false indicates that if there are other translators +// available, they should be tried. +inline bool apply_exception_translators(std::forward_list &translators) { + auto last_exception = std::current_exception(); + + for (auto &translator : translators) { + try { + translator(last_exception); + return true; + } catch (...) { + last_exception = std::current_exception(); + } + } + return false; +} + +inline void try_translate_exceptions() { + /* When an exception is caught, give each registered exception + translator a chance to translate it to a Python exception. First + all module-local translators will be tried in reverse order of + registration. If none of the module-locale translators handle + the exception (or there are no module-locale translators) then + the global translators will be tried, also in reverse order of + registration. + + A translator may choose to do one of the following: + + - catch the exception and call py::set_error() + to set a standard (or custom) Python exception, or + - do nothing and let the exception fall through to the next translator, or + - delegate translation to the next translator by throwing a new type of exception. + */ + + bool handled = with_exception_translators( + [&](std::forward_list &exception_translators, + std::forward_list &local_exception_translators) { + if (detail::apply_exception_translators(local_exception_translators)) { + return true; + } + if (detail::apply_exception_translators(exception_translators)) { + return true; + } + return false; + }); + + if (!handled) { + set_error(PyExc_SystemError, "Exception escaped from default exception translator!"); + } +} + +PYBIND11_NAMESPACE_END(detail) +PYBIND11_NAMESPACE_END(PYBIND11_NAMESPACE) diff --git a/external_libraries/pybind11/include/pybind11/detail/function_record_pyobject.h b/external_libraries/pybind11/include/pybind11/detail/function_record_pyobject.h new file mode 100644 index 00000000..94d27ad1 --- /dev/null +++ b/external_libraries/pybind11/include/pybind11/detail/function_record_pyobject.h @@ -0,0 +1,192 @@ +// Copyright (c) 2024-2025 The Pybind Development Team. +// All rights reserved. Use of this source code is governed by a +// BSD-style license that can be found in the LICENSE file. + +// For background see the description of PR google/pybind11clif#30099. + +#pragma once + +#include +#include +#include + +#include "common.h" + +#include +#include + +PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE) +PYBIND11_NAMESPACE_BEGIN(detail) + +struct function_record_PyObject { + PyObject_HEAD + function_record *cpp_func_rec; +}; + +PYBIND11_NAMESPACE_BEGIN(function_record_PyTypeObject_methods) + +PyObject *tp_new_impl(PyTypeObject *type, PyObject *args, PyObject *kwds); +PyObject *tp_alloc_impl(PyTypeObject *type, Py_ssize_t nitems); +int tp_init_impl(PyObject *self, PyObject *args, PyObject *kwds); +void tp_dealloc_impl(PyObject *self); +void tp_free_impl(void *self); + +static PyObject *reduce_ex_impl(PyObject *self, PyObject *, PyObject *); + +static PyMethodDef tp_methods_impl[] + = {{"__reduce_ex__", + // reduce_ex_impl is a PyCFunctionWithKeywords, but PyMethodDef + // requires a PyCFunction. The cast through void* is safe and + // idiomatic with METH_KEYWORDS, and it successfully sidesteps + // unhelpful compiler warnings. + // NOLINTNEXTLINE(bugprone-casting-through-void) + reinterpret_cast(reinterpret_cast(reduce_ex_impl)), + METH_VARARGS | METH_KEYWORDS, + nullptr}, + {nullptr, nullptr, 0, nullptr}}; + +// Python 3.12+ emits a DeprecationWarning for heap types whose tp_name does +// not contain a dot ('.') and that lack a __module__ attribute. For pybind11's +// internal function_record type, we do not have an actual module object to +// attach, so we cannot use PyType_FromModuleAndSpec (introduced in Python 3.9) +// to set __module__ automatically. +// +// As a workaround, we define a "qualified" type name that includes a dummy +// module name (PYBIND11_DUMMY_MODULE_NAME). This is non‑idiomatic but avoids +// the deprecation warning, and results in reprs like +// +// +// +// even though no real pybind11_builtins module exists. If pybind11 gains an +// actual module object in the future, this code should switch to +// PyType_FromModuleAndSpec for Python 3.9+ and drop the dummy module +// workaround. +// +// Note that this name is versioned. +#define PYBIND11_DETAIL_FUNCTION_RECORD_TP_PLAINNAME \ + "pybind11_detail_function_record_" PYBIND11_DETAIL_FUNCTION_RECORD_ABI_ID \ + "_" PYBIND11_PLATFORM_ABI_ID +constexpr char tp_plainname_impl[] = PYBIND11_DETAIL_FUNCTION_RECORD_TP_PLAINNAME; +constexpr char tp_qualname_impl[] + = PYBIND11_DUMMY_MODULE_NAME "." PYBIND11_DETAIL_FUNCTION_RECORD_TP_PLAINNAME; + +PYBIND11_NAMESPACE_END(function_record_PyTypeObject_methods) + +static PyType_Slot function_record_PyType_Slots[] = { + {Py_tp_dealloc, + reinterpret_cast(function_record_PyTypeObject_methods::tp_dealloc_impl)}, + {Py_tp_methods, + reinterpret_cast(function_record_PyTypeObject_methods::tp_methods_impl)}, + {Py_tp_init, reinterpret_cast(function_record_PyTypeObject_methods::tp_init_impl)}, + {Py_tp_alloc, reinterpret_cast(function_record_PyTypeObject_methods::tp_alloc_impl)}, + {Py_tp_new, reinterpret_cast(function_record_PyTypeObject_methods::tp_new_impl)}, + {Py_tp_free, reinterpret_cast(function_record_PyTypeObject_methods::tp_free_impl)}, + {0, nullptr}}; + +static PyType_Spec function_record_PyType_Spec + = {function_record_PyTypeObject_methods::tp_qualname_impl, + sizeof(function_record_PyObject), + 0, + Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HEAPTYPE, + function_record_PyType_Slots}; + +inline PyTypeObject *get_function_record_PyTypeObject() { + PYBIND11_LOCK_INTERNALS(get_internals()); + PyTypeObject *&py_type_obj = detail::get_local_internals().function_record_py_type; + if (!py_type_obj) { + PyObject *py_obj = PyType_FromSpec(&function_record_PyType_Spec); + if (py_obj == nullptr) { + throw error_already_set(); + } + py_type_obj = reinterpret_cast(py_obj); + } + return py_type_obj; +} + +inline bool is_function_record_PyObject(PyObject *obj) { + if (PyType_Check(obj) != 0) { + return false; + } + PyTypeObject *obj_type = Py_TYPE(obj); + + PyTypeObject *frtype = get_function_record_PyTypeObject(); + + // Fast path (pointer comparison). + if (obj_type == frtype) { + return true; + } + // This works across extension modules. Note that tp_name is versioned. + if (strcmp(obj_type->tp_name, function_record_PyTypeObject_methods::tp_qualname_impl) == 0 + || strcmp(obj_type->tp_name, function_record_PyTypeObject_methods::tp_plainname_impl) + == 0) { + return true; + } + return false; +} + +inline function_record *function_record_ptr_from_PyObject(PyObject *obj) { + if (is_function_record_PyObject(obj)) { + return (reinterpret_cast(obj))->cpp_func_rec; + } + return nullptr; +} + +inline object function_record_PyObject_New() { + auto *py_func_rec = PyObject_New(function_record_PyObject, get_function_record_PyTypeObject()); + if (py_func_rec == nullptr) { + throw error_already_set(); + } + py_func_rec->cpp_func_rec = nullptr; // For clarity/purity. Redundant in practice. + return reinterpret_steal(reinterpret_cast(py_func_rec)); +} + +PYBIND11_NAMESPACE_BEGIN(function_record_PyTypeObject_methods) + +// Guard against accidents & oversights, in particular when porting to future Python versions. +inline PyObject *tp_new_impl(PyTypeObject *, PyObject *, PyObject *) { + pybind11_fail("UNEXPECTED CALL OF function_record_PyTypeObject_methods::tp_new_impl"); + // return nullptr; // Unreachable. +} + +inline PyObject *tp_alloc_impl(PyTypeObject *, Py_ssize_t) { + pybind11_fail("UNEXPECTED CALL OF function_record_PyTypeObject_methods::tp_alloc_impl"); + // return nullptr; // Unreachable. +} + +inline int tp_init_impl(PyObject *, PyObject *, PyObject *) { + pybind11_fail("UNEXPECTED CALL OF function_record_PyTypeObject_methods::tp_init_impl"); + // return -1; // Unreachable. +} + +inline void tp_free_impl(void *) { + pybind11_fail("UNEXPECTED CALL OF function_record_PyTypeObject_methods::tp_free_impl"); +} + +inline PyObject *reduce_ex_impl(PyObject *self, PyObject *, PyObject *) { + // Deliberately ignoring the arguments for simplicity (expected is `protocol: int`). + const function_record *rec = function_record_ptr_from_PyObject(self); + if (rec == nullptr) { + pybind11_fail( + "FATAL: function_record_PyTypeObject reduce_ex_impl(): cannot obtain cpp_func_rec."); + } + if (rec->name != nullptr && rec->name[0] != '\0' && rec->scope + && PyModule_Check(rec->scope.ptr()) != 0) { + object scope_module = get_scope_module(rec->scope); + if (scope_module) { + auto builtins = reinterpret_borrow(PyEval_GetBuiltins()); + auto builtins_eval = builtins["eval"]; + auto reconstruct_args = make_tuple(str("__import__('importlib').import_module('") + + scope_module + str("')")); + return make_tuple(std::move(builtins_eval), std::move(reconstruct_args)) + .release() + .ptr(); + } + } + set_error(PyExc_RuntimeError, repr(self) + str(" is not pickleable.")); + return nullptr; +} + +PYBIND11_NAMESPACE_END(function_record_PyTypeObject_methods) + +PYBIND11_NAMESPACE_END(detail) +PYBIND11_NAMESPACE_END(PYBIND11_NAMESPACE) diff --git a/external_libraries/pybind11/include/pybind11/detail/holder_caster_foreign_helpers.h b/external_libraries/pybind11/include/pybind11/detail/holder_caster_foreign_helpers.h new file mode 100644 index 00000000..cae571b6 --- /dev/null +++ b/external_libraries/pybind11/include/pybind11/detail/holder_caster_foreign_helpers.h @@ -0,0 +1,104 @@ +/* + pybind11/detail/holder_caster_foreign_helpers.h: Logic to implement + set_foreign_holder() in copyable_ and movable_holder_caster. + + Copyright (c) 2025 Hudson River Trading LLC + + All rights reserved. Use of this source code is governed by a + BSD-style license that can be found in the LICENSE file. +*/ + +#pragma once + +#include + +#include "common.h" + +PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE) +PYBIND11_NAMESPACE_BEGIN(detail) + +struct holder_caster_foreign_helpers { + struct py_deleter { + void operator()(const void *) const noexcept { + // Don't run the deleter if the interpreter has been shut down + if (Py_IsInitialized() == 0) { + return; + } + gil_scoped_acquire guard; + Py_DECREF(o); + } + + PyObject *o; + }; + + // Downcast shared_ptr from the enable_shared_from_this base to the target type. + // SFINAE probe: use static_pointer_cast when the static downcast is valid (common case), + // fall back to dynamic_pointer_cast when it isn't (virtual inheritance — issue #5989). + // We can't use dynamic_pointer_cast unconditionally because it requires polymorphic types; + // we can't use is_polymorphic to choose because that's orthogonal to virtual inheritance. + // (The implementation uses the "tag dispatch via overload priority" trick.) + template + static auto esft_downcast(const std::shared_ptr &existing, int /*preferred*/) + -> decltype(static_cast(std::declval()), std::shared_ptr()) { + return std::static_pointer_cast(existing); + } + + template + static std::shared_ptr esft_downcast(const std::shared_ptr &existing, + ... /*fallback*/) { + return std::dynamic_pointer_cast(existing); + } + + template + static auto set_via_shared_from_this(type *value, std::shared_ptr *holder_out) + -> decltype(value->shared_from_this(), bool()) { + // object derives from enable_shared_from_this; + // try to reuse an existing shared_ptr if one is known + if (auto existing = try_get_shared_from_this(value)) { + *holder_out = esft_downcast(existing, 0); + return true; + } + return false; + } + + template + static bool set_via_shared_from_this(void *, std::shared_ptr *) { + return false; + } + + template + static bool set_foreign_holder(handle src, type *value, std::shared_ptr *holder_out) { + // We only support using std::shared_ptr for foreign T, and + // it's done by creating a new shared_ptr control block that + // owns a reference to the original Python object. + if (value == nullptr) { + *holder_out = {}; + return true; + } + if (set_via_shared_from_this(value, holder_out)) { + return true; + } + *holder_out = std::shared_ptr(value, py_deleter{src.inc_ref().ptr()}); + return true; + } + + template + static bool + set_foreign_holder(handle src, const type *value, std::shared_ptr *holder_out) { + std::shared_ptr holder_mut; + if (set_foreign_holder(src, const_cast(value), &holder_mut)) { + *holder_out = holder_mut; + return true; + } + return false; + } + + template + static bool set_foreign_holder(handle, type *, ...) { + throw cast_error("Unable to cast foreign type to held instance -- " + "only std::shared_ptr is supported in this case"); + } +}; + +PYBIND11_NAMESPACE_END(detail) +PYBIND11_NAMESPACE_END(PYBIND11_NAMESPACE) diff --git a/external_libraries/pybind11/include/pybind11/detail/init.h b/external_libraries/pybind11/include/pybind11/detail/init.h index e2117168..a1083f84 100644 --- a/external_libraries/pybind11/include/pybind11/detail/init.h +++ b/external_libraries/pybind11/include/pybind11/detail/init.h @@ -10,6 +10,7 @@ #pragma once #include "class.h" +#include "using_smart_holder.h" PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE) @@ -36,7 +37,7 @@ class type_caster { PYBIND11_NAMESPACE_BEGIN(initimpl) -inline void no_nullptr(void *ptr) { +inline void no_nullptr(const void *ptr) { if (!ptr) { throw type_error("pybind11::init(): factory function returned nullptr"); } @@ -60,12 +61,12 @@ bool is_alias(Cpp *ptr) { } // Failing fallback version of the above for a no-alias class (always returns false) template -constexpr bool is_alias(void *) { +constexpr bool is_alias(const void *) { return false; } // Constructs and returns a new object; if the given arguments don't map to a constructor, we fall -// back to brace aggregate initiailization so that for aggregate initialization can be used with +// back to brace aggregate initialization so that for aggregate initialization can be used with // py::init, e.g. `py::init` to initialize a `struct T { int a; int b; }`. For // non-aggregate types, we need to use an ordinary T(...) constructor (invoking as `T{...}` usually // works, but will not do the expected thing when `T` has an `initializer_list` constructor). @@ -128,11 +129,13 @@ void construct(value_and_holder &v_h, Cpp *ptr, bool need_alias) { // the holder and destruction happens when we leave the C++ scope, and the holder // class gets to handle the destruction however it likes. v_h.value_ptr() = ptr; - v_h.set_instance_registered(true); // To prevent init_instance from registering it - v_h.type->init_instance(v_h.inst, nullptr); // Set up the holder + v_h.set_instance_registered(true); // Trick to prevent init_instance from registering it + // DANGER ZONE BEGIN: exceptions will leave v_h in an invalid state. + v_h.type->init_instance(v_h.inst, nullptr); // Set up the holder Holder temp_holder(std::move(v_h.holder>())); // Steal the holder v_h.type->dealloc(v_h); // Destroys the moved-out holder remains, resets value ptr to null v_h.set_instance_registered(false); + // DANGER ZONE END. construct_alias_from_cpp(is_alias_constructible{}, v_h, std::move(*ptr)); } else { @@ -153,7 +156,7 @@ void construct(value_and_holder &v_h, Alias *alias_ptr, bool) { // holder. This also handles types like std::shared_ptr and std::unique_ptr where T is a // derived type (through those holder's implicit conversion from derived class holder // constructors). -template +template >::value, int> = 0> void construct(value_and_holder &v_h, Holder holder, bool need_alias) { PYBIND11_WORKAROUND_INCORRECT_MSVC_C4100(need_alias); auto *ptr = holder_helper>::get(holder); @@ -164,7 +167,12 @@ void construct(value_and_holder &v_h, Holder holder, bool need_alias) { "is not an alias instance"); } - v_h.value_ptr() = ptr; + // Cast away constness to store in void* storage. + // The value_and_holder storage is fundamentally untyped (void**), so we lose + // const-correctness here by design. The const qualifier will be restored + // when the pointer is later retrieved and cast back to the original type. + // This explicit const_cast makes the const-removal clearly visible. + v_h.value_ptr() = const_cast(static_cast(ptr)); v_h.type->init_instance(v_h.inst, &holder); } @@ -195,6 +203,92 @@ void construct(value_and_holder &v_h, Alias &&result, bool) { v_h.value_ptr() = new Alias(std::move(result)); } +template +smart_holder init_smart_holder_from_unique_ptr(std::unique_ptr &&unq_ptr, + bool void_cast_raw_ptr) { + void *void_ptr = void_cast_raw_ptr ? static_cast(unq_ptr.get()) : nullptr; + return smart_holder::from_unique_ptr(std::move(unq_ptr), void_ptr); +} + +template >, + detail::enable_if_t>::value, int> = 0> +void construct(value_and_holder &v_h, std::unique_ptr, D> &&unq_ptr, bool need_alias) { + PYBIND11_WORKAROUND_INCORRECT_MSVC_C4100(need_alias); + auto *ptr = unq_ptr.get(); + no_nullptr(ptr); + if (Class::has_alias && need_alias && !is_alias(ptr)) { + throw type_error("pybind11::init(): construction failed: returned std::unique_ptr pointee " + "is not an alias instance"); + } + // Here and below: if the new object is a trampoline, the shared_from_this mechanism needs + // to be prevented from accessing the smart_holder vptr, because it does not keep the + // trampoline Python object alive. For types that don't inherit from enable_shared_from_this + // it does not matter if void_cast_raw_ptr is true or false, therefore it's not necessary + // to also inspect the type. + auto smhldr = init_smart_holder_from_unique_ptr( + std::move(unq_ptr), /*void_cast_raw_ptr*/ Class::has_alias && is_alias(ptr)); + v_h.value_ptr() = ptr; + v_h.type->init_instance(v_h.inst, &smhldr); +} + +template >, + detail::enable_if_t>::value, int> = 0> +void construct(value_and_holder &v_h, + std::unique_ptr, D> &&unq_ptr, + bool /*need_alias*/) { + auto *ptr = unq_ptr.get(); + no_nullptr(ptr); + auto smhldr + = init_smart_holder_from_unique_ptr(std::move(unq_ptr), /*void_cast_raw_ptr*/ true); + v_h.value_ptr() = ptr; + v_h.type->init_instance(v_h.inst, &smhldr); +} + +template +void construct_from_shared_ptr(value_and_holder &v_h, + std::shared_ptr &&shd_ptr, + bool need_alias) { + static_assert(std::is_same>::value + || std::is_same>::value, + "Expected (const) Cpp as shared_ptr pointee"); + auto *ptr = shd_ptr.get(); + no_nullptr(ptr); + if (Class::has_alias && need_alias && !is_alias(ptr)) { + throw type_error("pybind11::init(): construction failed: returned std::shared_ptr pointee " + "is not an alias instance"); + } + // Cast to non-const if needed, consistent with internal design + auto smhldr + = smart_holder::from_shared_ptr(std::const_pointer_cast>(std::move(shd_ptr))); + v_h.value_ptr() = const_cast *>(ptr); + v_h.type->init_instance(v_h.inst, &smhldr); +} + +template >::value, int> = 0> +void construct(value_and_holder &v_h, std::shared_ptr> &&shd_ptr, bool need_alias) { + construct_from_shared_ptr, Class>(v_h, std::move(shd_ptr), need_alias); +} + +template >::value, int> = 0> +void construct(value_and_holder &v_h, + std::shared_ptr> &&shd_ptr, + bool need_alias) { + construct_from_shared_ptr, Class>(v_h, std::move(shd_ptr), need_alias); +} + +template >::value, int> = 0> +void construct(value_and_holder &v_h, + std::shared_ptr> &&shd_ptr, + bool /*need_alias*/) { + auto *ptr = shd_ptr.get(); + no_nullptr(ptr); + auto smhldr = smart_holder::from_shared_ptr(shd_ptr); + v_h.value_ptr() = ptr; + v_h.type->init_instance(v_h.inst, &smhldr); +} + // Implementing class for py::init<...>() template struct constructor { @@ -202,18 +296,18 @@ struct constructor { static void execute(Class &cl, const Extra &...extra) { cl.def( "__init__", - [](value_and_holder &v_h, Args... args) { + [](value_and_holder &v_h, + Args... args) { // NOLINT(performance-unnecessary-value-param) v_h.value_ptr() = construct_or_initialize>(std::forward(args)...); }, is_new_style_constructor(), extra...); } - template < - typename Class, - typename... Extra, - enable_if_t, Args...>::value, int> - = 0> + template , Args...>::value, + int> = 0> static void execute(Class &cl, const Extra &...extra) { cl.def( "__init__", @@ -230,11 +324,10 @@ struct constructor { extra...); } - template < - typename Class, - typename... Extra, - enable_if_t, Args...>::value, int> - = 0> + template , Args...>::value, + int> = 0> static void execute(Class &cl, const Extra &...extra) { cl.def( "__init__", @@ -250,11 +343,10 @@ struct constructor { // Implementing class for py::init_alias<...>() template struct alias_constructor { - template < - typename Class, - typename... Extra, - enable_if_t, Args...>::value, int> - = 0> + template , Args...>::value, + int> = 0> static void execute(Class &cl, const Extra &...extra) { cl.def( "__init__", @@ -275,8 +367,15 @@ template ` = `void_type()`, which the dual-factory +// specialization can also decompose as `AReturn(AArgs...)` with `AReturn=void_type` +// and `AArgs={}`. template -struct factory { +struct factory { remove_reference_t class_factory; // NOLINTNEXTLINE(google-explicit-constructor) @@ -380,7 +479,16 @@ void setstate(value_and_holder &v_h, std::pair &&result, bool need_alias) // See PR #2972 for details. return; } - setattr((PyObject *) v_h.inst, "__dict__", d); + // Our tests never run into an unset dict, but being careful here for now (see #5658) + auto dict = getattr(reinterpret_cast(v_h.inst), "__dict__", none()); + if (dict.is_none()) { + setattr(reinterpret_cast(v_h.inst), "__dict__", d); + } else { + // Keep the original object dict and just update it + if (PyDict_Update(dict.ptr(), d.ptr()) < 0) { + throw error_already_set(); + } + } } /// Implementation for py::pickle(GetState, SetState) @@ -397,9 +505,15 @@ template struct pickle_factory { - static_assert(std::is_same, intrinsic_t>::value, - "The type returned by `__getstate__` must be the same " - "as the argument accepted by `__setstate__`"); + using Ret = intrinsic_t; + using Arg = intrinsic_t; + + // Subclasses are now allowed for support between type hint and generic versions of types + // (e.g.) typing::List <--> list + static_assert(std::is_same::value || std::is_base_of::value + || std::is_base_of::value, + "The type returned by `__getstate__` must be the same or subclass of the " + "argument accepted by `__setstate__`"); remove_reference_t get; remove_reference_t set; @@ -408,7 +522,7 @@ struct pickle_factory { template void execute(Class &cl, const Extra &...extra) && { - cl.def("__getstate__", std::move(get)); + cl.def("__getstate__", std::move(get), pos_only()); #if defined(PYBIND11_CPP14) cl.def( diff --git a/external_libraries/pybind11/include/pybind11/detail/internals.h b/external_libraries/pybind11/include/pybind11/detail/internals.h index aaa7f868..a49cdaa4 100644 --- a/external_libraries/pybind11/include/pybind11/detail/internals.h +++ b/external_libraries/pybind11/include/pybind11/detail/internals.h @@ -9,15 +9,20 @@ #pragma once -#include "common.h" - -#if defined(WITH_THREAD) && defined(PYBIND11_SIMPLE_GIL_MANAGEMENT) -# include "../gil.h" -#endif +#include +#include +#include +#include -#include "../pytypes.h" +#include "common.h" +#include "struct_smart_holder.h" +#include +#include #include +#include +#include +#include /// Tracks the `internals` and `type_info` ABI version independent of the main library version. /// @@ -34,88 +39,139 @@ /// further ABI-incompatible changes may be made before the ABI is officially /// changed to the new version. #ifndef PYBIND11_INTERNALS_VERSION -# if PY_VERSION_HEX >= 0x030C0000 -// Version bump for Python 3.12+, before first 3.12 beta release. -# define PYBIND11_INTERNALS_VERSION 5 -# else -# define PYBIND11_INTERNALS_VERSION 4 -# endif +# define PYBIND11_INTERNALS_VERSION 11 #endif -// This requirement is mainly to reduce the support burden (see PR #4570). -static_assert(PY_VERSION_HEX < 0x030C0000 || PYBIND11_INTERNALS_VERSION >= 5, - "pybind11 ABI version 5 is the minimum for Python 3.12+"); +#if PYBIND11_INTERNALS_VERSION < 11 +# error "PYBIND11_INTERNALS_VERSION 11 is the minimum for all platforms for pybind11v3." +#endif PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE) using ExceptionTranslator = void (*)(std::exception_ptr); +// The old Python Thread Local Storage (TLS) API is deprecated in Python 3.7 in favor of the new +// Thread Specific Storage (TSS) API. +// Avoid unnecessary allocation of `Py_tss_t`, since we cannot use +// `Py_LIMITED_API` anyway. +#define PYBIND11_TLS_KEY_REF Py_tss_t & +#if defined(__clang__) +# define PYBIND11_TLS_KEY_INIT(var) \ + _Pragma("clang diagnostic push") /**/ \ + _Pragma("clang diagnostic ignored \"-Wmissing-field-initializers\"") /**/ \ + Py_tss_t var = Py_tss_NEEDS_INIT; \ + _Pragma("clang diagnostic pop") +#elif defined(__GNUC__) && !defined(__INTEL_COMPILER) +# define PYBIND11_TLS_KEY_INIT(var) \ + _Pragma("GCC diagnostic push") /**/ \ + _Pragma("GCC diagnostic ignored \"-Wmissing-field-initializers\"") /**/ \ + Py_tss_t var = Py_tss_NEEDS_INIT; \ + _Pragma("GCC diagnostic pop") +#else +# define PYBIND11_TLS_KEY_INIT(var) Py_tss_t var = Py_tss_NEEDS_INIT; +#endif +#define PYBIND11_TLS_KEY_CREATE(var) (PyThread_tss_create(&(var)) == 0) +#define PYBIND11_TLS_GET_VALUE(key) PyThread_tss_get(&(key)) +#define PYBIND11_TLS_REPLACE_VALUE(key, value) PyThread_tss_set(&(key), (value)) +#define PYBIND11_TLS_DELETE_VALUE(key) PyThread_tss_set(&(key), nullptr) +#define PYBIND11_TLS_FREE(key) PyThread_tss_delete(&(key)) + +/// A smart-pointer-like wrapper around a thread-specific value. get/set of the pointer applies to +/// the current thread only. +template +class thread_specific_storage { +public: + thread_specific_storage() { + // NOLINTNEXTLINE(bugprone-assignment-in-if-condition) + if (!PYBIND11_TLS_KEY_CREATE(key_)) { + pybind11_fail( + "thread_specific_storage constructor: could not initialize the TSS key!"); + } + } + + ~thread_specific_storage() { + // This destructor is often called *after* Py_Finalize(). That *SHOULD BE* fine on most + // platforms. The following details what happens when PyThread_tss_free is called in + // CPython. PYBIND11_TLS_FREE is PyThread_tss_free on python 3.7+. On older python, it does + // nothing. PyThread_tss_free calls PyThread_tss_delete and PyMem_RawFree. + // PyThread_tss_delete just calls TlsFree (on Windows) or pthread_key_delete (on *NIX). + // Neither of those have anything to do with CPython internals. PyMem_RawFree *requires* + // that the `key` be allocated with the CPython allocator (as it is by + // PyThread_tss_create). + // However, in GraalPy (as of v24.2 or older), TSS is implemented by Java and this call + // requires a living Python interpreter. +#ifdef GRAALVM_PYTHON + if (Py_IsInitialized() == 0 || _Py_IsFinalizing() != 0) { + return; + } +#endif + PYBIND11_TLS_FREE(key_); + } + + thread_specific_storage(thread_specific_storage const &) = delete; + thread_specific_storage(thread_specific_storage &&) = delete; + thread_specific_storage &operator=(thread_specific_storage const &) = delete; + thread_specific_storage &operator=(thread_specific_storage &&) = delete; + + T *get() const { return reinterpret_cast(PYBIND11_TLS_GET_VALUE(key_)); } + + T &operator*() const { return *get(); } + explicit operator T *() const { return get(); } + explicit operator bool() const { return get() != nullptr; } + + void set(T *val) { PYBIND11_TLS_REPLACE_VALUE(key_, reinterpret_cast(val)); } + void reset(T *p = nullptr) { set(p); } + thread_specific_storage &operator=(T *pval) { + set(pval); + return *this; + } + +private: + PYBIND11_TLS_KEY_INIT(mutable key_) +}; + PYBIND11_NAMESPACE_BEGIN(detail) -constexpr const char *internals_function_record_capsule_name = "pybind11_function_record_capsule"; +// This does NOT actually exist as a module. +#define PYBIND11_DUMMY_MODULE_NAME "pybind11_builtins" // Forward declarations inline PyTypeObject *make_static_property_type(); inline PyTypeObject *make_default_metaclass(); inline PyObject *make_object_base_type(PyTypeObject *metaclass); +inline void translate_exception(std::exception_ptr p); -// The old Python Thread Local Storage (TLS) API is deprecated in Python 3.7 in favor of the new -// Thread Specific Storage (TSS) API. -#if PY_VERSION_HEX >= 0x03070000 -// Avoid unnecessary allocation of `Py_tss_t`, since we cannot use -// `Py_LIMITED_API` anyway. -# if PYBIND11_INTERNALS_VERSION > 4 -# define PYBIND11_TLS_KEY_REF Py_tss_t & -# if defined(__GNUC__) && !defined(__INTEL_COMPILER) -// Clang on macOS warns due to `Py_tss_NEEDS_INIT` not specifying an initializer -// for every field. -# define PYBIND11_TLS_KEY_INIT(var) \ - _Pragma("GCC diagnostic push") /**/ \ - _Pragma("GCC diagnostic ignored \"-Wmissing-field-initializers\"") /**/ \ - Py_tss_t var \ - = Py_tss_NEEDS_INIT; \ - _Pragma("GCC diagnostic pop") -# else -# define PYBIND11_TLS_KEY_INIT(var) Py_tss_t var = Py_tss_NEEDS_INIT; -# endif -# define PYBIND11_TLS_KEY_CREATE(var) (PyThread_tss_create(&(var)) == 0) -# define PYBIND11_TLS_GET_VALUE(key) PyThread_tss_get(&(key)) -# define PYBIND11_TLS_REPLACE_VALUE(key, value) PyThread_tss_set(&(key), (value)) -# define PYBIND11_TLS_DELETE_VALUE(key) PyThread_tss_set(&(key), nullptr) -# define PYBIND11_TLS_FREE(key) PyThread_tss_delete(&(key)) -# else -# define PYBIND11_TLS_KEY_REF Py_tss_t * -# define PYBIND11_TLS_KEY_INIT(var) Py_tss_t *var = nullptr; -# define PYBIND11_TLS_KEY_CREATE(var) \ - (((var) = PyThread_tss_alloc()) != nullptr && (PyThread_tss_create((var)) == 0)) -# define PYBIND11_TLS_GET_VALUE(key) PyThread_tss_get((key)) -# define PYBIND11_TLS_REPLACE_VALUE(key, value) PyThread_tss_set((key), (value)) -# define PYBIND11_TLS_DELETE_VALUE(key) PyThread_tss_set((key), nullptr) -# define PYBIND11_TLS_FREE(key) PyThread_tss_free(key) -# endif +inline PyThreadState *get_thread_state_unchecked() { +#if defined(PYPY_VERSION) || defined(GRAALVM_PYTHON) + return PyThreadState_GET(); +#elif PY_VERSION_HEX < 0x030D0000 + return _PyThreadState_UncheckedGet(); #else -// Usually an int but a long on Cygwin64 with Python 3.x -# define PYBIND11_TLS_KEY_REF decltype(PyThread_create_key()) -# define PYBIND11_TLS_KEY_INIT(var) PYBIND11_TLS_KEY_REF var = 0; -# define PYBIND11_TLS_KEY_CREATE(var) (((var) = PyThread_create_key()) != -1) -# define PYBIND11_TLS_GET_VALUE(key) PyThread_get_key_value((key)) -# if defined(PYPY_VERSION) -// On CPython < 3.4 and on PyPy, `PyThread_set_key_value` strangely does not set -// the value if it has already been set. Instead, it must first be deleted and -// then set again. -inline void tls_replace_value(PYBIND11_TLS_KEY_REF key, void *value) { - PyThread_delete_key_value(key); - PyThread_set_key_value(key, value); + return PyThreadState_GetUnchecked(); +#endif } -# define PYBIND11_TLS_DELETE_VALUE(key) PyThread_delete_key_value(key) -# define PYBIND11_TLS_REPLACE_VALUE(key, value) \ - ::pybind11::detail::tls_replace_value((key), (value)) -# else -# define PYBIND11_TLS_DELETE_VALUE(key) PyThread_set_key_value((key), nullptr) -# define PYBIND11_TLS_REPLACE_VALUE(key, value) PyThread_set_key_value((key), (value)) -# endif -# define PYBIND11_TLS_FREE(key) (void) key + +inline PyInterpreterState *get_interpreter_state_unchecked() { + auto *tstate = get_thread_state_unchecked(); + return tstate ? tstate->interp : nullptr; +} + +inline object get_python_state_dict() { + object state_dict; +#if defined(PYPY_VERSION) || defined(GRAALVM_PYTHON) + state_dict = reinterpret_borrow(PyEval_GetBuiltins()); +#else + auto *istate = get_interpreter_state_unchecked(); + if (istate) { + state_dict = reinterpret_borrow(PyInterpreterState_GetDict(istate)); + } #endif + if (!state_dict) { + raise_from(PyExc_SystemError, "pybind11::detail::get_python_state_dict() FAILED"); + throw error_already_set(); + } + return state_dict; +} // Python loads modules by default with dlopen with the RTLD_LOCAL flag; under libc++ and possibly // other STLs, this means `typeid(A)` from one module won't equal `typeid(A)` from another module @@ -123,8 +179,7 @@ inline void tls_replace_value(PYBIND11_TLS_KEY_REF key, void *value) { // libstdc++, this doesn't happen: equality and the type_index hash are based on the type name, // which works. If not under a known-good stl, provide our own name-based hash and equality // functions that use the type name. -#if (PYBIND11_INTERNALS_VERSION <= 4 && defined(__GLIBCXX__)) \ - || (PYBIND11_INTERNALS_VERSION >= 5 && !defined(_LIBCPP_VERSION)) +#if !defined(_LIBCPP_VERSION) inline bool same_type(const std::type_info &lhs, const std::type_info &rhs) { return lhs == rhs; } using type_hash = std::hash; using type_equal_to = std::equal_to; @@ -151,26 +206,123 @@ struct type_equal_to { }; #endif +// For now, we don't bother adding a fancy hash for pointers and just +// let the standard library use the identity hash function if that's +// what it wants to do (e.g., as in libstdc++). +template +using fast_type_map = std::unordered_map; + template using type_map = std::unordered_map; struct override_hash { - inline size_t operator()(const std::pair &v) const { + size_t operator()(const std::pair &v) const { size_t value = std::hash()(v.first); value ^= std::hash()(v.second) + 0x9e3779b9 + (value << 6) + (value >> 2); return value; } }; +using instance_map = std::unordered_multimap; + +#ifdef Py_GIL_DISABLED +// Wrapper around PyMutex to provide BasicLockable semantics +class pymutex { + friend class pycritical_section; + PyMutex mutex; + +public: + pymutex() : mutex({}) {} + void lock() { PyMutex_Lock(&mutex); } + void unlock() { PyMutex_Unlock(&mutex); } +}; + +class pycritical_section { + pymutex &mutex; +# if PY_VERSION_HEX >= 0x030E00C1 // 3.14.0rc1 + PyCriticalSection cs; +# endif + +public: + explicit pycritical_section(pymutex &m) : mutex(m) { + // PyCriticalSection_BeginMutex was added in Python 3.15.0a1 and backported to 3.14.0rc1 +# if PY_VERSION_HEX >= 0x030E00C1 // 3.14.0rc1 + PyCriticalSection_BeginMutex(&cs, &mutex.mutex); +# else + // Fall back to direct mutex locking for older free-threaded Python versions + mutex.lock(); +# endif + } + ~pycritical_section() { +# if PY_VERSION_HEX >= 0x030E00C1 // 3.14.0rc1 + PyCriticalSection_End(&cs); +# else + mutex.unlock(); +# endif + } + + // Non-copyable and non-movable to prevent double-unlock + pycritical_section(const pycritical_section &) = delete; + pycritical_section &operator=(const pycritical_section &) = delete; + pycritical_section(pycritical_section &&) = delete; + pycritical_section &operator=(pycritical_section &&) = delete; +}; + +// Instance map shards are used to reduce mutex contention in free-threaded Python. +struct instance_map_shard { + instance_map registered_instances; + pymutex mutex; + // alignas(64) would be better, but causes compile errors in macOS before 10.14 (see #5200) + char padding[64 - (sizeof(instance_map) + sizeof(pymutex)) % 64]; +}; + +static_assert(sizeof(instance_map_shard) % 64 == 0, + "instance_map_shard size is not a multiple of 64 bytes"); + +inline uint64_t round_up_to_next_pow2(uint64_t x) { + // Round-up to the next power of two. + // See https://graphics.stanford.edu/~seander/bithacks.html#RoundUpPowerOf2 + x--; + x |= (x >> 1); + x |= (x >> 2); + x |= (x >> 4); + x |= (x >> 8); + x |= (x >> 16); + x |= (x >> 32); + x++; + return x; +} +#endif + +class loader_life_support; + /// Internal data structure used to track registered instances and types. /// Whenever binary incompatible changes are made to this structure, /// `PYBIND11_INTERNALS_VERSION` must be incremented. struct internals { +#ifdef Py_GIL_DISABLED + pymutex mutex; + pymutex exception_translator_mutex; +#endif +#if PYBIND11_INTERNALS_VERSION >= 12 + // non-normative but fast "hint" for registered_types_cpp. Meant + // to be used as the first level of a two-level lookup: successful + // lookups are correct, but unsuccessful lookups need to try + // registered_types_cpp and then backfill this map if they find + // anything. + fast_type_map registered_types_cpp_fast; +#endif + // std::type_index -> pybind11's type information type_map registered_types_cpp; // PyTypeObject* -> base type_info(s) std::unordered_map> registered_types_py; - std::unordered_multimap registered_instances; // void * -> instance* +#ifdef Py_GIL_DISABLED + std::unique_ptr instance_shards; // void * -> instance* + size_t instance_shards_mask = 0; +#else + instance_map registered_instances; // void * -> instance* +#endif std::unordered_set, override_hash> inactive_override_cache; type_map> direct_conversions; @@ -178,47 +330,68 @@ struct internals { std::forward_list registered_exception_translators; std::unordered_map shared_data; // Custom data to be shared across // extensions -#if PYBIND11_INTERNALS_VERSION == 4 - std::vector unused_loader_patient_stack_remove_at_v5; -#endif - std::forward_list static_strings; // Stores the std::strings backing - // detail::c_str() - PyTypeObject *static_property_type; - PyTypeObject *default_metaclass; - PyObject *instance_base; -#if defined(WITH_THREAD) + std::forward_list static_strings; // Stores the std::strings backing + // detail::c_str() + PyTypeObject *static_property_type = nullptr; + PyTypeObject *default_metaclass = nullptr; + PyObject *instance_base = nullptr; // Unused if PYBIND11_SIMPLE_GIL_MANAGEMENT is defined: - PYBIND11_TLS_KEY_INIT(tstate) -# if PYBIND11_INTERNALS_VERSION > 4 - PYBIND11_TLS_KEY_INIT(loader_life_support_tls_key) -# endif // PYBIND11_INTERNALS_VERSION > 4 + thread_specific_storage tstate; +#if PYBIND11_INTERNALS_VERSION <= 11 + thread_specific_storage loader_life_support_tls; // OBSOLETE (PR #5830) +#endif // Unused if PYBIND11_SIMPLE_GIL_MANAGEMENT is defined: PyInterpreterState *istate = nullptr; -# if PYBIND11_INTERNALS_VERSION > 4 - // Note that we have to use a std::string to allocate memory to ensure a unique address - // We want unique addresses since we use pointer equality to compare function records - std::string function_record_capsule_name = internals_function_record_capsule_name; -# endif + type_map native_enum_type_map; - internals() = default; + internals() + : static_property_type(make_static_property_type()), + default_metaclass(make_default_metaclass()), istate(get_interpreter_state_unchecked()) { + tstate.set(nullptr); // See PR #5870 + registered_exception_translators.push_front(&translate_exception); +#ifdef Py_GIL_DISABLED + // Scale proportional to the number of cores. 2x is a heuristic to reduce contention. + // Make sure the number isn't unreasonable by limiting it to 16 bits (65K) + auto num_shards = static_cast( + std::min(round_up_to_next_pow2(2 * std::thread::hardware_concurrency()), + std::numeric_limits::max())); + if (num_shards == 0) { + num_shards = 1; + } + instance_shards.reset(new instance_map_shard[num_shards]); + instance_shards_mask = num_shards - 1; +#endif + } internals(const internals &other) = delete; + internals(internals &&other) = delete; internals &operator=(const internals &other) = delete; - ~internals() { -# if PYBIND11_INTERNALS_VERSION > 4 - PYBIND11_TLS_FREE(loader_life_support_tls_key); -# endif // PYBIND11_INTERNALS_VERSION > 4 - - // This destructor is called *after* Py_Finalize() in finalize_interpreter(). - // That *SHOULD BE* fine. The following details what happens when PyThread_tss_free is - // called. PYBIND11_TLS_FREE is PyThread_tss_free on python 3.7+. On older python, it does - // nothing. PyThread_tss_free calls PyThread_tss_delete and PyMem_RawFree. - // PyThread_tss_delete just calls TlsFree (on Windows) or pthread_key_delete (on *NIX). - // Neither of those have anything to do with CPython internals. PyMem_RawFree *requires* - // that the `tstate` be allocated with the CPython allocator. - PYBIND11_TLS_FREE(tstate); - } -#endif + internals &operator=(internals &&other) = delete; + ~internals() = default; +}; + +// the internals struct (above) is shared between all the modules. local_internals are only +// for a single module. Any changes made to internals may require an update to +// PYBIND11_INTERNALS_VERSION, breaking backwards compatibility. local_internals is, by design, +// restricted to a single module. Whether a module has local internals or not should not +// impact any other modules, because the only things accessing the local internals is the +// module that contains them. +struct local_internals { + // It should be safe to use fast_type_map here because this entire + // data structure is scoped to our single module, and thus a single + // DSO and single instance of type_info for any particular type. + fast_type_map registered_types_cpp; + + std::forward_list registered_exception_translators; + PyTypeObject *function_record_py_type = nullptr; +}; + +enum class holder_enum_t : uint8_t { + undefined, + std_unique_ptr, // Default, lacking interop with std::shared_ptr. + std_shared_ptr, // Lacking interop with std::unique_ptr. + smart_holder, // Full std::unique_ptr / std::shared_ptr interop. + custom_holder, }; /// Additional type information which does not fit into the PyTypeObject. @@ -230,12 +403,33 @@ struct type_info { void *(*operator_new)(size_t); void (*init_instance)(instance *, const void *); void (*dealloc)(value_and_holder &v_h); + + // Cross-DSO-safe function pointers, to sidestep cross-DSO RTTI issues + // on platforms like macOS (see PR #5728 for details): + memory::get_guarded_delete_fn get_memory_guarded_delete = memory::get_guarded_delete; + get_trampoline_self_life_support_fn get_trampoline_self_life_support = nullptr; + std::vector implicit_conversions; std::vector> implicit_casts; std::vector *direct_conversions; buffer_info *(*get_buffer)(PyObject *, void *) = nullptr; void *get_buffer_data = nullptr; void *(*module_local_load)(PyObject *, const type_info *) = nullptr; + holder_enum_t holder_enum_v = holder_enum_t::undefined; + +#if PYBIND11_INTERNALS_VERSION >= 12 + // When a type appears in multiple DSOs, + // internals::registered_types_cpp_fast will have multiple distinct + // keys (the std::type_info from each DSO) mapped to the same + // detail::type_info*. We need to keep track of these aliases so that we clean + // them up when our type is deallocated. A linked list is appropriate + // because it is expected to be 1) usually empty and 2) + // when it's not empty, usually very small. See also `struct + // nb_alias_chain` added in + // https://github.com/wjakob/nanobind/commit/b515b1f7f2f4ecc0357818e6201c94a9f4cbfdc2 + std::forward_list alias_chain; +#endif + /* A simple type never occurs as a (direct or indirect) parent * of a class that makes use of multiple inheritance. * A type can be simple even if it has non-simple ancestors as long as it has no descendants. @@ -243,89 +437,43 @@ struct type_info { bool simple_type : 1; /* True if there is no multiple inheritance in this type's inheritance tree */ bool simple_ancestors : 1; - /* for base vs derived holder_type checks */ - bool default_holder : 1; /* true if this is a type registered with py::module_local */ bool module_local : 1; }; -/// On MSVC, debug and release builds are not ABI-compatible! -#if defined(_MSC_VER) && defined(_DEBUG) -# define PYBIND11_BUILD_TYPE "_debug" -#else -# define PYBIND11_BUILD_TYPE "" -#endif - -/// Let's assume that different compilers are ABI-incompatible. -/// A user can manually set this string if they know their -/// compiler is compatible. -#ifndef PYBIND11_COMPILER_TYPE -# if defined(_MSC_VER) -# define PYBIND11_COMPILER_TYPE "_msvc" -# elif defined(__INTEL_COMPILER) -# define PYBIND11_COMPILER_TYPE "_icc" -# elif defined(__clang__) -# define PYBIND11_COMPILER_TYPE "_clang" -# elif defined(__PGI) -# define PYBIND11_COMPILER_TYPE "_pgi" -# elif defined(__MINGW32__) -# define PYBIND11_COMPILER_TYPE "_mingw" -# elif defined(__CYGWIN__) -# define PYBIND11_COMPILER_TYPE "_gcc_cygwin" -# elif defined(__GNUC__) -# define PYBIND11_COMPILER_TYPE "_gcc" -# else -# define PYBIND11_COMPILER_TYPE "_unknown" -# endif -#endif - -/// Also standard libs -#ifndef PYBIND11_STDLIB -# if defined(_LIBCPP_VERSION) -# define PYBIND11_STDLIB "_libcpp" -# elif defined(__GLIBCXX__) || defined(__GLIBCPP__) -# define PYBIND11_STDLIB "_libstdcpp" -# else -# define PYBIND11_STDLIB "" -# endif -#endif - -/// On Linux/OSX, changes in __GXX_ABI_VERSION__ indicate ABI incompatibility. -#ifndef PYBIND11_BUILD_ABI -# if defined(__GXX_ABI_VERSION) -# define PYBIND11_BUILD_ABI "_cxxabi" PYBIND11_TOSTRING(__GXX_ABI_VERSION) -# else -# define PYBIND11_BUILD_ABI "" -# endif -#endif +/// Information stored in a capsule on py::native_enum() types. Since we don't +/// create a type_info record for native enums, we must store here any +/// information we will need about the enum at runtime. +/// +/// If you make backward-incompatible changes to this structure, you must +/// change the `attribute_name()` so that native enums from older version of +/// pybind11 don't have their records reinterpreted. Better would be to keep +/// the changes backward-compatible (i.e., only add new fields at the end) +/// and detect/indicate their presence using the currently-unused `version`. +struct native_enum_record { + const std::type_info *cpptype; + uint32_t size_bytes; + bool is_signed; + const uint8_t version = 1; -#ifndef PYBIND11_INTERNALS_KIND -# if defined(WITH_THREAD) -# define PYBIND11_INTERNALS_KIND "" -# else -# define PYBIND11_INTERNALS_KIND "_without_thread" -# endif -#endif + static const char *attribute_name() { return "__pybind11_native_enum__"; } +}; #define PYBIND11_INTERNALS_ID \ "__pybind11_internals_v" PYBIND11_TOSTRING(PYBIND11_INTERNALS_VERSION) \ - PYBIND11_INTERNALS_KIND PYBIND11_COMPILER_TYPE PYBIND11_STDLIB PYBIND11_BUILD_ABI \ - PYBIND11_BUILD_TYPE "__" + PYBIND11_COMPILER_TYPE_LEADING_UNDERSCORE PYBIND11_PLATFORM_ABI_ID "__" #define PYBIND11_MODULE_LOCAL_ID \ "__pybind11_module_local_v" PYBIND11_TOSTRING(PYBIND11_INTERNALS_VERSION) \ - PYBIND11_INTERNALS_KIND PYBIND11_COMPILER_TYPE PYBIND11_STDLIB PYBIND11_BUILD_ABI \ - PYBIND11_BUILD_TYPE "__" - -/// Each module locally stores a pointer to the `internals` data. The data -/// itself is shared among modules with the same `PYBIND11_INTERNALS_ID`. -inline internals **&get_internals_pp() { - static internals **internals_pp = nullptr; - return internals_pp; -} + PYBIND11_COMPILER_TYPE_LEADING_UNDERSCORE PYBIND11_PLATFORM_ABI_ID "__" -// forward decl -inline void translate_exception(std::exception_ptr); +/// We use this to figure out if there are or have been multiple subinterpreters active at any +/// point. This must never go from true to false while any interpreter may be running in any +/// thread! +inline std::atomic_bool &has_seen_non_main_interpreter() { + static std::atomic_bool multi(false); + return multi; +} template >::value, int> = 0> @@ -352,7 +500,7 @@ inline bool raise_err(PyObject *exc_type, const char *msg) { raise_from(exc_type, msg); return true; } - PyErr_SetString(exc_type, msg); + set_error(exc_type, msg); return false; } @@ -431,164 +579,453 @@ inline void translate_local_exception(std::exception_ptr p) { } #endif -inline object get_python_state_dict() { - object state_dict; -#if PYBIND11_INTERNALS_VERSION <= 4 || PY_VERSION_HEX < 0x03080000 || defined(PYPY_VERSION) - state_dict = reinterpret_borrow(PyEval_GetBuiltins()); -#else -# if PY_VERSION_HEX < 0x03090000 - PyInterpreterState *istate = _PyInterpreterState_Get(); -# else - PyInterpreterState *istate = PyInterpreterState_Get(); -# endif - if (istate) { - state_dict = reinterpret_borrow(PyInterpreterState_GetDict(istate)); +// Sentinel value for the `dtor` parameter of `atomic_get_or_create_in_state_dict`. +// Indicates no destructor was explicitly provided (distinct from nullptr, which means "leak"). +#define PYBIND11_DTOR_USE_DELETE (reinterpret_cast(1)) + +// Get or create per-storage capsule in the current interpreter's state dict. +// - The storage is interpreter-dependent: different interpreters will have different storage. +// This is important when using multiple-interpreters, to avoid sharing unshareable objects +// between interpreters. +// - There is one storage per `key` in an interpreter and it is accessible between all extensions +// in the same interpreter. +// - The life span of the storage is tied to the interpreter: it will be kept alive until the +// interpreter shuts down. +// +// Use test-and-set pattern with `PyDict_SetDefault` for thread-safe concurrent access. +// WARNING: There can be multiple threads creating the storage at the same time, while only one +// will succeed in inserting its capsule into the dict. Therefore, the deleter will be +// used to clean up the storage of the unused capsules. +// +// Returns: pair of (pointer to storage, bool indicating if newly created). +// The bool follows std::map::insert convention: true = created, false = existed. +// `dtor`: optional destructor called when the interpreter shuts down. +// - If not provided: the storage will be deleted using `delete`. +// - If nullptr: the storage will be leaked (useful for singletons that outlive the interpreter). +// - If a function: that function will be called with the capsule object. +template +std::pair atomic_get_or_create_in_state_dict(const char *key, + void (*dtor)(PyObject *) + = PYBIND11_DTOR_USE_DELETE) { + error_scope err_scope; // preserve any existing Python error states + + auto state_dict = reinterpret_borrow(get_python_state_dict()); + PyObject *capsule_obj = nullptr; + bool created = false; + + // Try to get existing storage (fast path). + capsule_obj = dict_getitemstring(state_dict.ptr(), key); + if (capsule_obj == nullptr) { + if (PyErr_Occurred()) { + throw error_already_set(); + } + // Storage doesn't exist yet, create a new one. + // Use unique_ptr for exception safety: if capsule creation throws, the storage is + // automatically deleted. + auto storage_ptr = std::unique_ptr(new Payload{}); + auto new_capsule + = capsule(storage_ptr.get(), + // The destructor will be called when the capsule is GC'ed. + // If the insert below fails (entry already in the dict), then this + // destructor will be called on the newly created capsule at the end of this + // function, and we want to just release this memory. + /*destructor=*/[](void *v) { delete static_cast(v); }); + // At this point, the capsule object is created successfully. + // Release the unique_ptr and let the capsule object own the storage to avoid double-free. + (void) storage_ptr.release(); + + // Use `PyDict_SetDefault` for atomic test-and-set: + // - If key doesn't exist, inserts our capsule and returns it. + // - If key exists (another thread inserted first), returns the existing value. + // This is thread-safe because `PyDict_SetDefault` will hold a lock on the dict. + // + // NOTE: Here we use `PyDict_SetDefault` instead of `PyDict_SetDefaultRef` because the + // capsule is kept alive until interpreter shutdown, so we do not need to handle + // incref and decref here. + capsule_obj = dict_setdefaultstring(state_dict.ptr(), key, new_capsule.ptr()); + if (capsule_obj == nullptr) { + throw error_already_set(); + } + created = (capsule_obj == new_capsule.ptr()); + // - If key already existed, our `new_capsule` is not inserted, it will be destructed when + // going out of scope here, and will call the destructor set above. + // - Otherwise, our `new_capsule` is now in the dict, and it owns the storage and the state + // dict will incref it. We need to set the caller's destructor on it, which will be + // called when the interpreter shuts down. + if (created && dtor != PYBIND11_DTOR_USE_DELETE) { + if (PyCapsule_SetDestructor(capsule_obj, dtor) < 0) { + throw error_already_set(); + } + } } -#endif - if (!state_dict) { - raise_from(PyExc_SystemError, "pybind11::detail::get_python_state_dict() FAILED"); + + // Get the storage pointer from the capsule. + void *raw_ptr = PyCapsule_GetPointer(capsule_obj, /*name=*/nullptr); + if (!raw_ptr) { + raise_from(PyExc_SystemError, + "pybind11::detail::atomic_get_or_create_in_state_dict() FAILED"); + throw error_already_set(); } - return state_dict; + return std::pair(static_cast(raw_ptr), created); } -inline object get_internals_obj_from_state_dict(handle state_dict) { - return reinterpret_borrow(dict_getitemstring(state_dict.ptr(), PYBIND11_INTERNALS_ID)); -} +#undef PYBIND11_DTOR_USE_DELETE -inline internals **get_internals_pp_from_capsule(handle obj) { - void *raw_ptr = PyCapsule_GetPointer(obj.ptr(), /*name=*/nullptr); - if (raw_ptr == nullptr) { - raise_from(PyExc_SystemError, "pybind11::detail::get_internals_pp_from_capsule() FAILED"); - } - return static_cast(raw_ptr); -} +template +class internals_pp_manager { +public: + using on_fetch_function = void(InternalsType *); -/// Return a reference to the current `internals` data -PYBIND11_NOINLINE internals &get_internals() { - auto **&internals_pp = get_internals_pp(); - if (internals_pp && *internals_pp) { - return **internals_pp; + static internals_pp_manager &get_instance(char const *id, on_fetch_function *on_fetch) { + static internals_pp_manager instance(id, on_fetch); + return instance; } -#if defined(WITH_THREAD) -# if defined(PYBIND11_SIMPLE_GIL_MANAGEMENT) - gil_scoped_acquire gil; -# else - // Ensure that the GIL is held since we will need to make Python calls. - // Cannot use py::gil_scoped_acquire here since that constructor calls get_internals. - struct gil_scoped_acquire_local { - gil_scoped_acquire_local() : state(PyGILState_Ensure()) {} - gil_scoped_acquire_local(const gil_scoped_acquire_local &) = delete; - gil_scoped_acquire_local &operator=(const gil_scoped_acquire_local &) = delete; - ~gil_scoped_acquire_local() { PyGILState_Release(state); } - const PyGILState_STATE state; - } gil; -# endif + /// Get the current pointer-to-pointer, allocating it if it does not already exist. May + /// acquire the GIL. Will never return nullptr. + std::unique_ptr *get_pp() { +#ifdef PYBIND11_HAS_SUBINTERPRETER_SUPPORT + if (has_seen_non_main_interpreter()) { + // Whenever the interpreter changes on the current thread we need to invalidate the + // internals_pp so that it can be pulled from the interpreter's state dict. That is + // slow, so we use the current PyThreadState to check if it is necessary. + auto *tstate = get_thread_state_unchecked(); + if (!tstate || tstate->interp != last_istate_tls()) { + gil_scoped_acquire_simple gil; + if (!tstate) { + tstate = get_thread_state_unchecked(); + } + last_istate_tls() = tstate->interp; + internals_p_tls() = get_or_create_pp_in_state_dict(); + } + return internals_p_tls(); + } #endif - error_scope err_scope; + if (!internals_singleton_pp_) { + gil_scoped_acquire_simple gil; + internals_singleton_pp_ = get_or_create_pp_in_state_dict(); + } + return internals_singleton_pp_; + } - dict state_dict = get_python_state_dict(); - if (object internals_obj = get_internals_obj_from_state_dict(state_dict)) { - internals_pp = get_internals_pp_from_capsule(internals_obj); + /// Drop all the references we're currently holding. + void unref() { +#ifdef PYBIND11_HAS_SUBINTERPRETER_SUPPORT + if (has_seen_non_main_interpreter()) { + last_istate_tls() = nullptr; + internals_p_tls() = nullptr; + return; + } +#endif + internals_singleton_pp_ = nullptr; } - if (internals_pp && *internals_pp) { - // We loaded the internals through `state_dict`, which means that our `error_already_set` - // and `builtin_exception` may be different local classes than the ones set up in the - // initial exception translator, below, so add another for our local exception classes. - // - // libstdc++ doesn't require this (types there are identified only by name) - // libc++ with CPython doesn't require this (types are explicitly exported) - // libc++ with PyPy still need it, awaiting further investigation -#if !defined(__GLIBCXX__) - (*internals_pp)->registered_exception_translators.push_front(&translate_local_exception); + + void destroy() { +#ifdef PYBIND11_HAS_SUBINTERPRETER_SUPPORT + if (has_seen_non_main_interpreter()) { + auto *tstate = get_thread_state_unchecked(); + // this could be called without an active interpreter, just use what was cached + if (!tstate || tstate->interp == last_istate_tls()) { + auto tpp = internals_p_tls(); + { + std::lock_guard lock(pp_set_mutex_); + pps_have_created_content_.erase(tpp); // untrack deleted pp + } + delete tpp; // may call back into Python + } + unref(); + return; + } #endif - } else { - if (!internals_pp) { - internals_pp = new internals *(); + { + std::lock_guard lock(pp_set_mutex_); + pps_have_created_content_.erase(internals_singleton_pp_); // untrack deleted pp } - auto *&internals_ptr = *internals_pp; - internals_ptr = new internals(); -#if defined(WITH_THREAD) + delete internals_singleton_pp_; // may call back into Python + unref(); + } - PyThreadState *tstate = PyThreadState_Get(); - // NOLINTNEXTLINE(bugprone-assignment-in-if-condition) - if (!PYBIND11_TLS_KEY_CREATE(internals_ptr->tstate)) { - pybind11_fail("get_internals: could not successfully initialize the tstate TSS key!"); + void create_pp_content_once(std::unique_ptr *const pp) { + // Assume the GIL is held here. May call back into Python. We cannot hold the lock with our + // mutex here. So there may be multiple threads creating the content at the same time. Only + // one will install its content to pp below. Others will be freed when going out of scope. + auto tmp = std::unique_ptr(new InternalsType()); + + { + // Lock scope must not include Python calls, which may require the GIL and cause + // deadlocks. + std::lock_guard lock(pp_set_mutex_); + + if (*pp) { + // Already created in another thread. + return; + } + + // At this point, pp->get() is nullptr. + // The content is either not yet created, or was previously destroyed via pp->reset(). + + // Detect re-creation of internals after destruction during interpreter shutdown. + // If pybind11 code (e.g., tp_traverse/tp_clear calling py::cast) runs after internals + // have been destroyed, a new empty internals would be created, causing type lookup + // failures. See also get_or_create_pp_in_state_dict() comments. + if (pps_have_created_content_.find(pp) != pps_have_created_content_.end()) { + pybind11_fail( + "pybind11::detail::internals_pp_manager::create_pp_content_once() " + "FAILED: reentrant call detected while fetching pybind11 internals!"); + } + + // Each interpreter can only create its internals once. + pps_have_created_content_.insert(pp); + // Install the created content. + pp->swap(tmp); } - PYBIND11_TLS_REPLACE_VALUE(internals_ptr->tstate, tstate); + } -# if PYBIND11_INTERNALS_VERSION > 4 - // NOLINTNEXTLINE(bugprone-assignment-in-if-condition) - if (!PYBIND11_TLS_KEY_CREATE(internals_ptr->loader_life_support_tls_key)) { - pybind11_fail("get_internals: could not successfully initialize the " - "loader_life_support TSS key!"); +private: + internals_pp_manager(char const *id, on_fetch_function *on_fetch) + : holder_id_(id), on_fetch_(on_fetch) {} + + std::unique_ptr *get_or_create_pp_in_state_dict() { + // The `unique_ptr` is intentionally leaked on interpreter shutdown. + // Once an instance is created, it will never be deleted until the process exits (compare + // to interpreter shutdown in multiple-interpreter scenarios). + // We cannot guarantee the destruction order of capsules in the interpreter state dict on + // interpreter shutdown, so deleting internals too early could cause undefined behavior + // when other pybind11 objects access `get_internals()` during finalization (which would + // recreate empty internals). See also create_pp_content_once() above. + // See https://github.com/pybind/pybind11/pull/5958#discussion_r2717645230. + auto result = atomic_get_or_create_in_state_dict>( + holder_id_, /*dtor=*/nullptr /* leak the capsule content */); + auto *pp = result.first; + bool created = result.second; + // Only call on_fetch_ when fetching existing internals, not when creating new ones. + if (!created && on_fetch_ && pp) { + on_fetch_(pp->get()); } -# endif - internals_ptr->istate = tstate->interp; -#endif - state_dict[PYBIND11_INTERNALS_ID] = capsule(internals_pp); - internals_ptr->registered_exception_translators.push_front(&translate_exception); - internals_ptr->static_property_type = make_static_property_type(); - internals_ptr->default_metaclass = make_default_metaclass(); - internals_ptr->instance_base = make_object_base_type(internals_ptr->default_metaclass); + return pp; } - return **internals_pp; -} -// the internals struct (above) is shared between all the modules. local_internals are only -// for a single module. Any changes made to internals may require an update to -// PYBIND11_INTERNALS_VERSION, breaking backwards compatibility. local_internals is, by design, -// restricted to a single module. Whether a module has local internals or not should not -// impact any other modules, because the only things accessing the local internals is the -// module that contains them. -struct local_internals { - type_map registered_types_cpp; - std::forward_list registered_exception_translators; -#if defined(WITH_THREAD) && PYBIND11_INTERNALS_VERSION == 4 - - // For ABI compatibility, we can't store the loader_life_support TLS key in - // the `internals` struct directly. Instead, we store it in `shared_data` and - // cache a copy in `local_internals`. If we allocated a separate TLS key for - // each instance of `local_internals`, we could end up allocating hundreds of - // TLS keys if hundreds of different pybind11 modules are loaded (which is a - // plausible number). - PYBIND11_TLS_KEY_INIT(loader_life_support_tls_key) - - // Holds the shared TLS key for the loader_life_support stack. - struct shared_loader_life_support_data { - PYBIND11_TLS_KEY_INIT(loader_life_support_tls_key) - shared_loader_life_support_data() { - // NOLINTNEXTLINE(bugprone-assignment-in-if-condition) - if (!PYBIND11_TLS_KEY_CREATE(loader_life_support_tls_key)) { - pybind11_fail("local_internals: could not successfully initialize the " - "loader_life_support TLS key!"); +#ifdef PYBIND11_HAS_SUBINTERPRETER_SUPPORT + static PyInterpreterState *&last_istate_tls() { + static thread_local PyInterpreterState *last_istate = nullptr; + return last_istate; + } + + static std::unique_ptr *&internals_p_tls() { + static thread_local std::unique_ptr *internals_p = nullptr; + return internals_p; + } +#endif + + char const *holder_id_ = nullptr; + on_fetch_function *on_fetch_ = nullptr; + // Pointer-to-pointer to the singleton internals for the first seen interpreter (may not be the + // main interpreter) + std::unique_ptr *internals_singleton_pp_ = nullptr; + + // Track pointer-to-pointers whose internals have been created, to detect re-entrancy. + // Use instance member over static due to singleton pattern of this class. + std::unordered_set *> pps_have_created_content_; + std::mutex pp_set_mutex_; +}; + +// If We loaded the internals through `state_dict`, our `error_already_set` +// and `builtin_exception` may be different local classes than the ones set up in the +// initial exception translator, below, so add another for our local exception classes. +// +// libstdc++ doesn't require this (types there are identified only by name) +// libc++ with CPython doesn't require this (types are explicitly exported) +// libc++ with PyPy still need it, awaiting further investigation +#if !defined(__GLIBCXX__) +inline void check_internals_local_exception_translator(internals *internals_ptr) { + if (internals_ptr) { + for (auto et : internals_ptr->registered_exception_translators) { + if (et == &translate_local_exception) { + return; } } - // We can't help but leak the TLS key, because Python never unloads extension modules. - }; + internals_ptr->registered_exception_translators.push_front(&translate_local_exception); + } +} +#endif - local_internals() { - auto &internals = get_internals(); - // Get or create the `loader_life_support_stack_key`. - auto &ptr = internals.shared_data["_life_support"]; - if (!ptr) { - ptr = new shared_loader_life_support_data; +inline internals_pp_manager &get_internals_pp_manager() { +#if defined(__GLIBCXX__) +# define ON_FETCH_FN nullptr +#else +# define ON_FETCH_FN &check_internals_local_exception_translator +#endif + return internals_pp_manager::get_instance(PYBIND11_INTERNALS_ID, ON_FETCH_FN); +#undef ON_FETCH_FN +} + +/// Return a reference to the current `internals` data +PYBIND11_NOINLINE internals &get_internals() { + auto &ppmgr = get_internals_pp_manager(); + auto *pp = ppmgr.get_pp(); + if (!pp) { + pybind11_fail("get_internals: get_pp() returned nullptr"); + } + auto &internals_ptr = *pp; + if (!internals_ptr) { + // Slow path, something needs fetched from the state dict or created + gil_scoped_acquire_simple gil; + error_scope err_scope; + + ppmgr.create_pp_content_once(&internals_ptr); + + if (!internals_ptr) { + pybind11_fail("get_internals: create_pp_content_once() produced nullptr"); + } + if (!internals_ptr->instance_base) { + // This calls get_internals, so cannot be called from within the internals constructor + // called above because internals_ptr must be set before get_internals is called again + internals_ptr->instance_base = make_object_base_type(internals_ptr->default_metaclass); } - loader_life_support_tls_key - = static_cast(ptr)->loader_life_support_tls_key; } -#endif // defined(WITH_THREAD) && PYBIND11_INTERNALS_VERSION == 4 -}; + return *internals_ptr; +} + +/// Return the PyObject* for the internals capsule (borrowed reference). +/// Returns nullptr if the capsule doesn't exist yet. +inline PyObject *get_internals_capsule() { + auto state_dict = reinterpret_borrow(get_python_state_dict()); + return dict_getitemstring(state_dict.ptr(), PYBIND11_INTERNALS_ID); +} + +/// Return the key used for local_internals in the state dict. +/// This function ensures a consistent key is used across all call sites within the same +/// compilation unit. The key includes the address of a static variable to make it unique per +/// module (DSO), matching the behavior of get_local_internals_pp_manager(). +inline const std::string &get_local_internals_key() { + static const std::string key + = PYBIND11_MODULE_LOCAL_ID + std::to_string(reinterpret_cast(&key)); + return key; +} + +/// Return the PyObject* for the local_internals capsule (borrowed reference). +/// Returns nullptr if the capsule doesn't exist yet. +inline PyObject *get_local_internals_capsule() { + const auto &key = get_local_internals_key(); + auto state_dict = reinterpret_borrow(get_python_state_dict()); + return dict_getitemstring(state_dict.ptr(), key.c_str()); +} + +inline void ensure_internals() { + pybind11::detail::get_internals_pp_manager().unref(); +#ifdef PYBIND11_HAS_SUBINTERPRETER_SUPPORT + if (PyInterpreterState_Get() != PyInterpreterState_Main()) { + has_seen_non_main_interpreter() = true; + } +#endif + pybind11::detail::get_internals(); +} + +inline internals_pp_manager &get_local_internals_pp_manager() { + // Use the address of a static variable as part of the key, so that the value is uniquely tied + // to where the module is loaded in memory + return internals_pp_manager::get_instance(get_local_internals_key().c_str(), + nullptr); +} /// Works like `get_internals`, but for things which are locally registered. inline local_internals &get_local_internals() { - // Current static can be created in the interpreter finalization routine. If the later will be - // destroyed in another static variable destructor, creation of this static there will cause - // static deinitialization fiasco. In order to avoid it we avoid destruction of the - // local_internals static. One can read more about the problem and current solution here: - // https://google.github.io/styleguide/cppguide.html#Static_and_Global_Variables - static auto *locals = new local_internals(); - return *locals; + auto &ppmgr = get_local_internals_pp_manager(); + auto &internals_ptr = *ppmgr.get_pp(); + if (!internals_ptr) { + gil_scoped_acquire_simple gil; + error_scope err_scope; + + ppmgr.create_pp_content_once(&internals_ptr); + } + return *internals_ptr; +} + +#ifdef Py_GIL_DISABLED +# define PYBIND11_LOCK_INTERNALS(internals) pycritical_section lock((internals).mutex) +#else +# define PYBIND11_LOCK_INTERNALS(internals) +#endif + +template +inline auto with_internals(const F &cb) -> decltype(cb(get_internals())) { + auto &internals = get_internals(); + PYBIND11_LOCK_INTERNALS(internals); + return cb(internals); +} + +template +inline void with_internals_if_internals(const F &cb) { + auto &ppmgr = get_internals_pp_manager(); + auto &internals_ptr = *ppmgr.get_pp(); + if (internals_ptr) { + auto &internals = *internals_ptr; + PYBIND11_LOCK_INTERNALS(internals); + cb(internals); + } +} + +template +inline auto with_exception_translators(const F &cb) + -> decltype(cb(get_internals().registered_exception_translators, + get_local_internals().registered_exception_translators)) { + auto &internals = get_internals(); +#ifdef Py_GIL_DISABLED + pycritical_section lock((internals).exception_translator_mutex); +#endif + auto &local_internals = get_local_internals(); + return cb(internals.registered_exception_translators, + local_internals.registered_exception_translators); +} + +inline std::uint64_t mix64(std::uint64_t z) { + // David Stafford's variant 13 of the MurmurHash3 finalizer popularized + // by the SplitMix PRNG. + // https://zimbry.blogspot.com/2011/09/better-bit-mixing-improving-on.html + z = (z ^ (z >> 30)) * 0xbf58476d1ce4e5b9; + z = (z ^ (z >> 27)) * 0x94d049bb133111eb; + return z ^ (z >> 31); +} + +template +inline auto with_instance_map(const void *ptr, const F &cb) + -> decltype(cb(std::declval())) { + auto &internals = get_internals(); + +#ifdef Py_GIL_DISABLED + // Hash address to compute shard, but ignore low bits. We'd like allocations + // from the same thread/core to map to the same shard and allocations from + // other threads/cores to map to other shards. Using the high bits is a good + // heuristic because memory allocators often have a per-thread + // arena/superblock/segment from which smaller allocations are served. + auto addr = reinterpret_cast(ptr); + auto hash = mix64(static_cast(addr >> 20)); + auto idx = static_cast(hash & internals.instance_shards_mask); + + auto &shard = internals.instance_shards[idx]; + std::unique_lock lock(shard.mutex); + return cb(shard.registered_instances); +#else + (void) ptr; + return cb(internals.registered_instances); +#endif +} + +// Returns the number of registered instances for testing purposes. The result may not be +// consistent if other threads are registering or unregistering instances concurrently. +inline size_t num_registered_instances() { + auto &internals = get_internals(); +#ifdef Py_GIL_DISABLED + size_t count = 0; + for (size_t i = 0; i <= internals.instance_shards_mask; ++i) { + auto &shard = internals.instance_shards[i]; + std::unique_lock lock(shard.mutex); + count += shard.registered_instances.size(); + } + return count; +#else + return internals.registered_instances.size(); +#endif } /// Constructs a std::string with the given arguments, stores it in `internals`, and returns its @@ -597,45 +1034,33 @@ inline local_internals &get_local_internals() { /// suitable for c-style strings needed by Python internals (such as PyTypeObject's tp_name). template const char *c_str(Args &&...args) { - auto &strings = get_internals().static_strings; + // GCC 4.8 doesn't like parameter unpack within lambda capture, so use + // PYBIND11_LOCK_INTERNALS. + auto &internals = get_internals(); + PYBIND11_LOCK_INTERNALS(internals); + auto &strings = internals.static_strings; strings.emplace_front(std::forward(args)...); return strings.front().c_str(); } -inline const char *get_function_record_capsule_name() { -#if PYBIND11_INTERNALS_VERSION > 4 - return get_internals().function_record_capsule_name.c_str(); -#else - return nullptr; -#endif -} - -// Determine whether or not the following capsule contains a pybind11 function record. -// Note that we use `internals` to make sure that only ABI compatible records are touched. -// -// This check is currently used in two places: -// - An important optimization in functional.h to avoid overhead in C++ -> Python -> C++ -// - The sibling feature of cpp_function to allow overloads -inline bool is_function_record_capsule(const capsule &cap) { - // Pointer equality as we rely on internals() to ensure unique pointers - return cap.name() == get_function_record_capsule_name(); -} - 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. 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; + return detail::with_internals([&](detail::internals &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()`. PYBIND11_NOINLINE void *set_shared_data(const std::string &name, void *data) { - detail::get_internals().shared_data[name] = data; - return data; + return detail::with_internals([&](detail::internals &internals) { + internals.shared_data[name] = data; + return data; + }); } /// Returns a typed reference to a shared data entry (by using `get_shared_data()`) if @@ -643,14 +1068,15 @@ PYBIND11_NOINLINE void *set_shared_data(const std::string &name, void *data) { /// added to the shared data under the given name and a reference to it is returned. template T &get_or_create_shared_data(const std::string &name) { - auto &internals = detail::get_internals(); - auto it = internals.shared_data.find(name); - T *ptr = (T *) (it != internals.shared_data.end() ? it->second : nullptr); - if (!ptr) { - ptr = new T(); - internals.shared_data[name] = ptr; - } - return *ptr; + return *detail::with_internals([&](detail::internals &internals) { + auto it = internals.shared_data.find(name); + T *ptr = (T *) (it != internals.shared_data.end() ? it->second : nullptr); + if (!ptr) { + ptr = new T(); + internals.shared_data[name] = ptr; + } + return ptr; + }); } PYBIND11_NAMESPACE_END(PYBIND11_NAMESPACE) diff --git a/external_libraries/pybind11/include/pybind11/detail/native_enum_data.h b/external_libraries/pybind11/include/pybind11/detail/native_enum_data.h new file mode 100644 index 00000000..6770378f --- /dev/null +++ b/external_libraries/pybind11/include/pybind11/detail/native_enum_data.h @@ -0,0 +1,227 @@ +// Copyright (c) 2022-2025 The pybind Community. +// All rights reserved. Use of this source code is governed by a +// BSD-style license that can be found in the LICENSE file. + +#pragma once + +#include "../pytypes.h" +#include "common.h" +#include "internals.h" + +#include +#include +#include +#include + +PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE) +PYBIND11_NAMESPACE_BEGIN(detail) + +// This is a separate function only to enable easy unit testing. +inline std::string +native_enum_missing_finalize_error_message(const std::string &enum_name_encoded) { + return "pybind11::native_enum<...>(\"" + enum_name_encoded + "\", ...): MISSING .finalize()"; +} + +// Internals for pybind11::native_enum; one native_enum_data object exists +// inside each pybind11::native_enum and lives only for the duration of the +// native_enum binding statement. +class native_enum_data { +public: + native_enum_data(handle parent_scope_, + const char *enum_name, + const char *native_type_name, + const char *class_doc, + const native_enum_record &enum_record_) + : enum_name_encoded{enum_name}, native_type_name_encoded{native_type_name}, + enum_type_index{*enum_record_.cpptype}, + parent_scope(reinterpret_borrow(parent_scope_)), enum_name{enum_name}, + native_type_name{native_type_name}, class_doc(class_doc), export_values_flag{false}, + finalize_needed{false} { + // Create the enum record capsule. It will be installed on the enum + // type object during finalize(). Its destructor removes the enum + // mapping from our internals, so that we won't try to convert to an + // enum type that's been destroyed. + enum_record = capsule( + new native_enum_record{enum_record_}, + native_enum_record::attribute_name(), + +[](void *record_) { + auto *record = static_cast(record_); + with_internals([&](internals &internals) { + internals.native_enum_type_map.erase(*record->cpptype); + }); + delete record; + }); + } + + void finalize(); + + native_enum_data(const native_enum_data &) = delete; + native_enum_data &operator=(const native_enum_data &) = delete; + +#if !defined(NDEBUG) + // This dtor cannot easily be unit tested because it terminates the process. + ~native_enum_data() { + if (finalize_needed) { + pybind11_fail(native_enum_missing_finalize_error_message(enum_name_encoded)); + } + } +#endif + +protected: + void disarm_finalize_check(const char *error_context) { + if (!finalize_needed) { + pybind11_fail("pybind11::native_enum<...>(\"" + enum_name_encoded + + "\"): " + error_context); + } + finalize_needed = false; + } + + void arm_finalize_check() { + assert(!finalize_needed); // Catch redundant calls. + finalize_needed = true; + } + + std::string enum_name_encoded; + std::string native_type_name_encoded; + std::type_index enum_type_index; + +private: + object parent_scope; + str enum_name; + str native_type_name; + std::string class_doc; + capsule enum_record; + +protected: + list members; + list member_docs; + bool export_values_flag : 1; // Attention: It is best to keep the bools together. + +private: + bool finalize_needed : 1; +}; + +inline handle +global_internals_native_enum_type_map_get_item(const std::type_index &enum_type_index) { + return with_internals([&](internals &internals) { + auto found = internals.native_enum_type_map.find(enum_type_index); + if (found != internals.native_enum_type_map.end()) { + return handle(found->second); + } + return handle(); + }); +} + +inline bool +global_internals_native_enum_type_map_contains(const std::type_index &enum_type_index) { + return with_internals([&](internals &internals) { + return internals.native_enum_type_map.count(enum_type_index) != 0; + }); +} + +inline object import_or_getattr(const std::string &fully_qualified_name, + const std::string &append_to_exception_message) { + std::istringstream stream(fully_qualified_name); + std::string part; + + if (!std::getline(stream, part, '.') || part.empty()) { + std::string msg = "Invalid fully-qualified name `"; + msg += fully_qualified_name; + msg += "`"; + msg += append_to_exception_message; + throw value_error(msg); + } + + auto curr_scope = reinterpret_steal(PyImport_ImportModule(part.c_str())); + if (!curr_scope) { + std::string msg = "Failed to import top-level module `"; + msg += part; + msg += "`"; + msg += append_to_exception_message; + raise_from(PyExc_ImportError, msg.c_str()); + throw error_already_set(); + } + + // Now recursively getattr or import remaining parts + std::string curr_path = part; + while (std::getline(stream, part, '.')) { + if (part.empty()) { + std::string msg = "Invalid fully-qualified name `"; + msg += fully_qualified_name; + msg += "`"; + msg += append_to_exception_message; + throw value_error(msg); + } + std::string next_path = curr_path; + next_path += "."; + next_path += part; + auto next_scope + = reinterpret_steal(PyObject_GetAttrString(curr_scope.ptr(), part.c_str())); + if (!next_scope) { + error_fetch_and_normalize stored_getattr_error("getattr"); + // Try importing the next level + next_scope = reinterpret_steal(PyImport_ImportModule(next_path.c_str())); + if (!next_scope) { + error_fetch_and_normalize stored_import_error("import"); + std::string msg = "Failed to import or getattr `"; + msg += part; + msg += "` from `"; + msg += curr_path; + msg += "`"; + msg += append_to_exception_message; + msg += "\n-------- getattr exception --------\n"; + msg += stored_getattr_error.error_string(); + msg += "\n-------- import exception --------\n"; + msg += stored_import_error.error_string(); + throw import_error(msg.c_str()); + } + } + curr_scope = next_scope; + curr_path = next_path; + } + return curr_scope; +} + +inline void native_enum_data::finalize() { + disarm_finalize_check("DOUBLE finalize"); + if (hasattr(parent_scope, enum_name)) { + pybind11_fail("pybind11::native_enum<...>(\"" + enum_name_encoded + + "\"): an object with that name is already defined"); + } + auto py_enum_type = import_or_getattr(native_type_name, " (native_type_name)"); + auto py_enum = py_enum_type(enum_name, members); + object module_name = get_module_name_if_available(parent_scope); + if (module_name) { + py_enum.attr("__module__") = module_name; + } + if (hasattr(parent_scope, "__qualname__")) { + const auto parent_qualname = parent_scope.attr("__qualname__").cast(); + py_enum.attr("__qualname__") = str(parent_qualname + "." + enum_name.cast()); + } + parent_scope.attr(enum_name) = py_enum; + if (export_values_flag) { + for (auto member : members) { + auto member_name = member[int_(0)]; + if (hasattr(parent_scope, member_name)) { + pybind11_fail("pybind11::native_enum<...>(\"" + enum_name_encoded + "\").value(\"" + + member_name.cast() + + "\"): an object with that name is already defined"); + } + parent_scope.attr(member_name) = py_enum[member_name]; + } + } + if (!class_doc.empty()) { + py_enum.attr("__doc__") = class_doc.c_str(); + } + for (auto doc : member_docs) { + py_enum[doc[int_(0)]].attr("__doc__") = doc[int_(1)]; + } + + py_enum.attr(native_enum_record::attribute_name()) = enum_record; + with_internals([&](internals &internals) { + internals.native_enum_type_map[enum_type_index] = py_enum.ptr(); + }); +} + +PYBIND11_NAMESPACE_END(detail) +PYBIND11_NAMESPACE_END(PYBIND11_NAMESPACE) diff --git a/external_libraries/pybind11/include/pybind11/detail/pybind11_namespace_macros.h b/external_libraries/pybind11/include/pybind11/detail/pybind11_namespace_macros.h new file mode 100644 index 00000000..6f74bf85 --- /dev/null +++ b/external_libraries/pybind11/include/pybind11/detail/pybind11_namespace_macros.h @@ -0,0 +1,82 @@ +// Copyright (c) 2016-2025 The Pybind Development Team. +// All rights reserved. Use of this source code is governed by a +// BSD-style license that can be found in the LICENSE file. + +#pragma once + +// PLEASE DO NOT ADD ANY INCLUDES HERE + +// Define some generic pybind11 helper macros for warning management. +// +// Note that compiler-specific push/pop pairs are baked into the +// PYBIND11_NAMESPACE_BEGIN/PYBIND11_NAMESPACE_END pair of macros. Therefore manual +// PYBIND11_WARNING_PUSH/PYBIND11_WARNING_POP are usually only needed in `#include` sections. +// +// If you find you need to suppress a warning, please try to make the suppression as local as +// possible using these macros. Please also be sure to push/pop with the pybind11 macros. Please +// only use compiler specifics if you need to check specific versions, e.g. Apple Clang vs. vanilla +// Clang. +#if defined(__INTEL_COMPILER) +# define PYBIND11_COMPILER_INTEL +# define PYBIND11_PRAGMA(...) _Pragma(#__VA_ARGS__) +# define PYBIND11_WARNING_PUSH PYBIND11_PRAGMA(warning push) +# define PYBIND11_WARNING_POP PYBIND11_PRAGMA(warning pop) +#elif defined(__clang__) +# define PYBIND11_COMPILER_CLANG +# define PYBIND11_PRAGMA(...) _Pragma(#__VA_ARGS__) +# define PYBIND11_WARNING_PUSH PYBIND11_PRAGMA(clang diagnostic push) +# define PYBIND11_WARNING_POP PYBIND11_PRAGMA(clang diagnostic pop) +#elif defined(__GNUC__) +# define PYBIND11_COMPILER_GCC +# define PYBIND11_PRAGMA(...) _Pragma(#__VA_ARGS__) +# define PYBIND11_WARNING_PUSH PYBIND11_PRAGMA(GCC diagnostic push) +# define PYBIND11_WARNING_POP PYBIND11_PRAGMA(GCC diagnostic pop) +#elif defined(_MSC_VER) // Must be after the clang branch because clang-cl also defines _MSC_VER +# define PYBIND11_COMPILER_MSVC +# define PYBIND11_PRAGMA(...) __pragma(__VA_ARGS__) +# define PYBIND11_WARNING_PUSH PYBIND11_PRAGMA(warning(push)) +# define PYBIND11_WARNING_POP PYBIND11_PRAGMA(warning(pop)) +#endif + +#ifdef PYBIND11_COMPILER_MSVC +# define PYBIND11_WARNING_DISABLE_MSVC(name) PYBIND11_PRAGMA(warning(disable : name)) +#else +# define PYBIND11_WARNING_DISABLE_MSVC(name) +#endif + +#ifdef PYBIND11_COMPILER_CLANG +# define PYBIND11_WARNING_DISABLE_CLANG(name) PYBIND11_PRAGMA(clang diagnostic ignored name) +#else +# define PYBIND11_WARNING_DISABLE_CLANG(name) +#endif + +#ifdef PYBIND11_COMPILER_GCC +# define PYBIND11_WARNING_DISABLE_GCC(name) PYBIND11_PRAGMA(GCC diagnostic ignored name) +#else +# define PYBIND11_WARNING_DISABLE_GCC(name) +#endif + +#ifdef PYBIND11_COMPILER_INTEL +# define PYBIND11_WARNING_DISABLE_INTEL(name) PYBIND11_PRAGMA(warning disable name) +#else +# define PYBIND11_WARNING_DISABLE_INTEL(name) +#endif + +#define PYBIND11_NAMESPACE_BEGIN(name) \ + namespace name { \ + PYBIND11_WARNING_PUSH + +#define PYBIND11_NAMESPACE_END(name) \ + PYBIND11_WARNING_POP \ + } + +// Robust support for some features and loading modules compiled against different pybind versions +// requires forcing hidden visibility on pybind code, so we enforce this by setting the attribute +// on the main `pybind11` namespace. +#if !defined(PYBIND11_NAMESPACE) +# if defined(__GNUG__) && !defined(_WIN32) +# define PYBIND11_NAMESPACE pybind11 __attribute__((visibility("hidden"))) +# else +# define PYBIND11_NAMESPACE pybind11 +# endif +#endif diff --git a/external_libraries/pybind11/include/pybind11/detail/struct_smart_holder.h b/external_libraries/pybind11/include/pybind11/detail/struct_smart_holder.h new file mode 100644 index 00000000..5d7c31cd --- /dev/null +++ b/external_libraries/pybind11/include/pybind11/detail/struct_smart_holder.h @@ -0,0 +1,398 @@ +// Copyright (c) 2020-2024 The Pybind Development Team. +// All rights reserved. Use of this source code is governed by a +// BSD-style license that can be found in the LICENSE file. + +/* Proof-of-Concept for smart pointer interoperability. + +High-level aspects: + +* Support all `unique_ptr`, `shared_ptr` interops that are feasible. + +* Cleanly and clearly report all interops that are infeasible. + +* Meant to fit into a `PyObject`, as a holder for C++ objects. + +* Support a system design that makes it impossible to trigger + C++ Undefined Behavior, especially from Python. + +* Support a system design with clean runtime inheritance casting. From this + it follows that the `smart_holder` needs to be type-erased (`void*`). + +* Handling of RTTI for the type-erased held pointer is NOT implemented here. + It is the responsibility of the caller to ensure that `static_cast` + is well-formed when calling `as_*` member functions. Inheritance casting + needs to be handled in a different layer (similar to the code organization + in boost/python/object/inheritance.hpp). + +Details: + +* The "root holder" chosen here is a `shared_ptr` (named `vptr` in this + implementation). This choice is practically inevitable because `shared_ptr` + has only very limited support for inspecting and accessing its deleter. + +* If created from a raw pointer, or a `unique_ptr` without a custom deleter, + `vptr` always uses a custom deleter, to support `unique_ptr`-like disowning. + The custom deleters could be extended to included life-time management for + external objects (e.g. `PyObject`). + +* If created from an external `shared_ptr`, or a `unique_ptr` with a custom + deleter, including life-time management for external objects is infeasible. + +* By choice, the smart_holder is movable but not copyable, to keep the design + simple, and to guard against accidental copying overhead. + +* The `void_cast_raw_ptr` option is needed to make the `smart_holder` `vptr` + member invisible to the `shared_from_this` mechanism, in case the lifetime + of a `PyObject` is tied to the pointee. +*/ + +#pragma once + +#include "pybind11_namespace_macros.h" + +#include +#include +#include +#include +#include +#include +#include +#include + +PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE) +PYBIND11_NAMESPACE_BEGIN(memory) + +// Default fallback. +static constexpr bool type_has_shared_from_this(...) { return false; } + +// This overload uses SFINAE to skip enable_shared_from_this checks when the +// base is inaccessible (e.g. private inheritance). +template +static auto type_has_shared_from_this(const T *ptr) + -> decltype(static_cast *>(ptr), true) { + return true; +} + +// Inaccessible base → substitution failure → fallback overload selected +template +static constexpr bool type_has_shared_from_this(const void *) { + return false; +} + +struct guarded_delete { + // NOTE: PYBIND11_INTERNALS_VERSION needs to be bumped if changes are made to this struct. + std::weak_ptr released_ptr; // Trick to keep the smart_holder memory footprint small. + std::function del_fun; // Rare case. + void (*del_ptr)(void *); // Common case. + bool use_del_fun; + bool armed_flag; + guarded_delete(std::function &&del_fun, bool armed_flag) + : del_fun{std::move(del_fun)}, del_ptr{nullptr}, use_del_fun{true}, + armed_flag{armed_flag} {} + guarded_delete(void (*del_ptr)(void *), bool armed_flag) + : del_ptr{del_ptr}, use_del_fun{false}, armed_flag{armed_flag} {} + void operator()(void *raw_ptr) const { + if (armed_flag) { + if (use_del_fun) { + del_fun(raw_ptr); + } else { + del_ptr(raw_ptr); + } + } + } +}; + +inline guarded_delete *get_guarded_delete(const std::shared_ptr &ptr) { + return std::get_deleter(ptr); +} + +using get_guarded_delete_fn = guarded_delete *(*) (const std::shared_ptr &); + +template ::value, int>::type = 0> +inline void std_default_delete_if_destructible(void *raw_ptr) { + std::default_delete{}(static_cast(raw_ptr)); +} + +template ::value, int>::type = 0> +inline void std_default_delete_if_destructible(void *) { + // This noop operator is needed to avoid a compilation error (for `delete raw_ptr;`), but + // throwing an exception from a destructor will std::terminate the process. Therefore the + // runtime check for lifetime-management correctness is implemented elsewhere (in + // ensure_pointee_is_destructible()). +} + +template +guarded_delete make_guarded_std_default_delete(bool armed_flag) { + return guarded_delete(std_default_delete_if_destructible, armed_flag); +} + +template +struct custom_deleter { + // NOTE: PYBIND11_INTERNALS_VERSION needs to be bumped if changes are made to this struct. + D deleter; + explicit custom_deleter(D &&deleter) : deleter{std::forward(deleter)} {} + void operator()(void *raw_ptr) { deleter(static_cast(raw_ptr)); } +}; + +template +guarded_delete make_guarded_custom_deleter(D &&uqp_del, bool armed_flag) { + return guarded_delete( + std::function(custom_deleter(std::forward(uqp_del))), armed_flag); +} + +template +constexpr bool uqp_del_is_std_default_delete() { + return std::is_same>::value + || std::is_same>::value; +} + +inline bool type_info_equal_across_dso_boundaries(const std::type_info &a, + const std::type_info &b) { + // RTTI pointer comparison may fail across DSOs (e.g., macOS libc++). + // Fallback to name comparison, which is generally safe and ABI-stable enough for our use. + return a == b || std::strcmp(a.name(), b.name()) == 0; +} + +struct smart_holder { + // NOTE: PYBIND11_INTERNALS_VERSION needs to be bumped if changes are made to this struct. + const std::type_info *rtti_uqp_del = nullptr; + std::shared_ptr vptr; + bool vptr_is_using_noop_deleter : 1; + bool vptr_is_using_std_default_delete : 1; + bool vptr_is_external_shared_ptr : 1; + bool is_populated : 1; + bool is_disowned : 1; + + // Design choice: smart_holder is movable but not copyable. + smart_holder(smart_holder &&) = default; + smart_holder(const smart_holder &) = delete; + smart_holder &operator=(smart_holder &&) = delete; + smart_holder &operator=(const smart_holder &) = delete; + + smart_holder() + : vptr_is_using_noop_deleter{false}, vptr_is_using_std_default_delete{false}, + vptr_is_external_shared_ptr{false}, is_populated{false}, is_disowned{false} {} + + bool has_pointee() const { return vptr != nullptr; } + + template + static void ensure_pointee_is_destructible(const char *context) { + if (!std::is_destructible::value) { + throw std::invalid_argument(std::string("Pointee is not destructible (") + context + + ")."); + } + } + + void ensure_is_populated(const char *context) const { + if (!is_populated) { + throw std::runtime_error(std::string("Unpopulated holder (") + context + ")."); + } + } + void ensure_is_not_disowned(const char *context) const { + if (is_disowned) { + throw std::runtime_error(std::string("Holder was disowned already (") + context + + ")."); + } + } + + void ensure_vptr_is_using_std_default_delete(const char *context) const { + if (vptr_is_external_shared_ptr) { + throw std::invalid_argument(std::string("Cannot disown external shared_ptr (") + + context + ")."); + } + if (vptr_is_using_noop_deleter) { + throw std::invalid_argument(std::string("Cannot disown non-owning holder (") + context + + ")."); + } + if (!vptr_is_using_std_default_delete) { + throw std::invalid_argument(std::string("Cannot disown custom deleter (") + context + + ")."); + } + } + + template + void ensure_compatible_uqp_del(const char *context) const { + if (!rtti_uqp_del) { + if (!uqp_del_is_std_default_delete()) { + throw std::invalid_argument(std::string("Missing unique_ptr deleter (") + context + + ")."); + } + ensure_vptr_is_using_std_default_delete(context); + return; + } + if (uqp_del_is_std_default_delete() && vptr_is_using_std_default_delete) { + return; + } + if (!type_info_equal_across_dso_boundaries(typeid(D), *rtti_uqp_del)) { + throw std::invalid_argument(std::string("Incompatible unique_ptr deleter (") + context + + ")."); + } + } + + void ensure_has_pointee(const char *context) const { + if (!has_pointee()) { + throw std::invalid_argument(std::string("Disowned holder (") + context + ")."); + } + } + + void ensure_use_count_1(const char *context) const { + if (vptr == nullptr) { + throw std::invalid_argument(std::string("Cannot disown nullptr (") + context + ")."); + } + // In multithreaded environments accessing use_count can lead to + // race conditions, but in the context of Python it is a bug (elsewhere) + // if the Global Interpreter Lock (GIL) is not being held when this code + // is reached. + // PYBIND11:REMINDER: This may need to be protected by a mutex in free-threaded Python. + if (vptr.use_count() != 1) { + throw std::invalid_argument(std::string("Cannot disown use_count != 1 (") + context + + ")."); + } + } + + void reset_vptr_deleter_armed_flag(const get_guarded_delete_fn ggd_fn, bool armed_flag) const { + auto *gd = ggd_fn(vptr); + if (gd == nullptr) { + throw std::runtime_error( + "smart_holder::reset_vptr_deleter_armed_flag() called in an invalid context."); + } + gd->armed_flag = armed_flag; + } + + // Caller is responsible for precondition: ensure_compatible_uqp_del() must succeed. + template + std::unique_ptr extract_deleter(const char *context, + const get_guarded_delete_fn ggd_fn) const { + auto *gd = ggd_fn(vptr); + if (gd && gd->use_del_fun) { + const auto &custom_deleter_ptr = gd->del_fun.template target>(); + if (custom_deleter_ptr == nullptr) { + throw std::runtime_error( + std::string("smart_holder::extract_deleter() precondition failure (") + context + + ")."); + } + static_assert(std::is_copy_constructible::value, + "Required for compatibility with smart_holder functionality."); + return std::unique_ptr(new D(custom_deleter_ptr->deleter)); + } + return nullptr; + } + + static smart_holder from_raw_ptr_unowned(void *raw_ptr) { + smart_holder hld; + hld.vptr.reset(raw_ptr, [](void *) {}); + hld.vptr_is_using_noop_deleter = true; + hld.is_populated = true; + return hld; + } + + template + T *as_raw_ptr_unowned() const { + return static_cast(vptr.get()); + } + + template + static smart_holder from_raw_ptr_take_ownership(T *raw_ptr, bool void_cast_raw_ptr = false) { + ensure_pointee_is_destructible("from_raw_ptr_take_ownership"); + smart_holder hld; + auto gd = make_guarded_std_default_delete(true); + if (void_cast_raw_ptr) { + hld.vptr.reset(static_cast(raw_ptr), std::move(gd)); + } else { + hld.vptr.reset(raw_ptr, std::move(gd)); + } + hld.vptr_is_using_std_default_delete = true; + hld.is_populated = true; + return hld; + } + + // Caller is responsible for ensuring the complex preconditions + // (see `smart_holder_type_caster_support::load_helper`). + void disown(const get_guarded_delete_fn ggd_fn) { + reset_vptr_deleter_armed_flag(ggd_fn, false); + is_disowned = true; + } + + // Caller is responsible for ensuring the complex preconditions + // (see `smart_holder_type_caster_support::load_helper`). + void reclaim_disowned(const get_guarded_delete_fn ggd_fn) { + reset_vptr_deleter_armed_flag(ggd_fn, true); + is_disowned = false; + } + + // Caller is responsible for ensuring the complex preconditions + // (see `smart_holder_type_caster_support::load_helper`). + void release_disowned() { vptr.reset(); } + + void ensure_can_release_ownership(const char *context = "ensure_can_release_ownership") const { + ensure_is_not_disowned(context); + ensure_vptr_is_using_std_default_delete(context); + ensure_use_count_1(context); + } + + // Caller is responsible for ensuring the complex preconditions + // (see `smart_holder_type_caster_support::load_helper`). + void release_ownership(const get_guarded_delete_fn ggd_fn) { + reset_vptr_deleter_armed_flag(ggd_fn, false); + release_disowned(); + } + + template + static smart_holder from_unique_ptr(std::unique_ptr &&unq_ptr, + void *mi_subobject_ptr = nullptr) { + smart_holder hld; + hld.rtti_uqp_del = &typeid(D); + hld.vptr_is_using_std_default_delete = uqp_del_is_std_default_delete(); + + // Build the owning control block on the *real object start* (T*). + guarded_delete gd + = hld.vptr_is_using_std_default_delete + ? make_guarded_std_default_delete(true) + : make_guarded_custom_deleter(std::move(unq_ptr.get_deleter()), true); + // Critical: construct owner with pointer we intend to delete + std::shared_ptr owner(unq_ptr.get(), std::move(gd)); + // Relinquish ownership only after successful construction of owner + (void) unq_ptr.release(); + + // Publish either the MI/VI subobject pointer (if provided) or the full object. + // Why this is needed: + // * The `owner` shared_ptr must always manage the true object start (T*). + // That ensures the deleter is invoked on a valid object header, so the + // virtual destructor can dispatch safely (critical on MSVC with virtual + // inheritance, where base subobjects are not at offset 0). + // * However, pybind11 needs to *register* and expose the subobject pointer + // appropriate for the type being bound. + // This pointer may differ from the T* object start under multiple/virtual + // inheritance. + // This is achieved by using an aliasing shared_ptr: + // - `owner` retains lifetime of the actual T* object start for deletion. + // - `vptr` points at the adjusted subobject (mi_subobject_ptr), giving + // Python the correct identity/registration address. + // If no subobject pointer is passed, we simply publish the full object. + if (mi_subobject_ptr) { + hld.vptr = std::shared_ptr(owner, mi_subobject_ptr); + } else { + hld.vptr = std::static_pointer_cast(owner); + } + + hld.is_populated = true; + return hld; + } + + template + static smart_holder from_shared_ptr(const std::shared_ptr &shd_ptr) { + smart_holder hld; + hld.vptr = std::static_pointer_cast(shd_ptr); + hld.vptr_is_external_shared_ptr = true; + hld.is_populated = true; + return hld; + } + + template + std::shared_ptr as_shared_ptr() const { + return std::static_pointer_cast(vptr); + } +}; + +PYBIND11_NAMESPACE_END(memory) +PYBIND11_NAMESPACE_END(PYBIND11_NAMESPACE) diff --git a/external_libraries/pybind11/include/pybind11/detail/type_caster_base.h b/external_libraries/pybind11/include/pybind11/detail/type_caster_base.h index 16387506..8fbf700e 100644 --- a/external_libraries/pybind11/include/pybind11/detail/type_caster_base.h +++ b/external_libraries/pybind11/include/pybind11/detail/type_caster_base.h @@ -9,15 +9,24 @@ #pragma once -#include "../pytypes.h" +#include +#include +#include + #include "common.h" +#include "cpp_conduit.h" #include "descr.h" +#include "dynamic_raw_ptr_cast_if_possible.h" #include "internals.h" #include "typeid.h" +#include "using_smart_holder.h" +#include "value_and_holder.h" #include +#include #include #include +#include #include #include #include @@ -33,44 +42,40 @@ PYBIND11_NAMESPACE_BEGIN(detail) /// Adding a patient will keep it alive up until the enclosing function returns. class loader_life_support { private: + // Thread-local top-of-stack for loader_life_support frames (linked via parent). + // Observation: loader_life_support needs to be thread-local, + // but we don't need to go to extra effort to keep it + // per-interpreter (i.e., by putting it in internals) since + // individual function calls are already isolated to a single + // interpreter, even though they could potentially call into a + // different interpreter later in the same call chain. This + // saves a significant cost per function call spent in + // loader_life_support destruction. + // Note for future C++17 simplification: + // inline static thread_local loader_life_support *tls_current_frame = nullptr; + static loader_life_support *&tls_current_frame() { + static thread_local loader_life_support *frame_ptr = nullptr; + return frame_ptr; + } + loader_life_support *parent = nullptr; std::unordered_set keep_alive; -#if defined(WITH_THREAD) - // Store stack pointer in thread-local storage. - static PYBIND11_TLS_KEY_REF get_stack_tls_key() { -# if PYBIND11_INTERNALS_VERSION == 4 - return get_local_internals().loader_life_support_tls_key; -# else - return get_internals().loader_life_support_tls_key; -# endif - } - static loader_life_support *get_stack_top() { - return static_cast(PYBIND11_TLS_GET_VALUE(get_stack_tls_key())); - } - static void set_stack_top(loader_life_support *value) { - PYBIND11_TLS_REPLACE_VALUE(get_stack_tls_key(), value); - } -#else - // Use single global variable for stack. - static loader_life_support **get_stack_pp() { - static loader_life_support *global_stack = nullptr; - return global_stack; - } - static loader_life_support *get_stack_top() { return *get_stack_pp(); } - static void set_stack_top(loader_life_support *value) { *get_stack_pp() = value; } -#endif - public: /// A new patient frame is created when a function is entered - loader_life_support() : parent{get_stack_top()} { set_stack_top(this); } + loader_life_support() { + auto &frame = tls_current_frame(); + parent = frame; + frame = this; + } /// ... and destroyed after it returns ~loader_life_support() { - if (get_stack_top() != this) { + auto &frame = tls_current_frame(); + if (frame != this) { pybind11_fail("loader_life_support: internal error"); } - set_stack_top(parent); + frame = parent; for (auto *item : keep_alive) { Py_DECREF(item); } @@ -79,7 +84,7 @@ class loader_life_support { /// This can only be used inside a pybind11-bound function, either by `argument_loader` /// at argument preparation time or by `py::cast()` at execution time. PYBIND11_NOINLINE static void add_patient(handle h) { - loader_life_support *frame = get_stack_top(); + loader_life_support *frame = tls_current_frame(); if (!frame) { // NOTE: It would be nice to include the stack frames here, as this indicates // use of pybind11::cast<> outside the normal call framework, finding such @@ -102,13 +107,26 @@ class loader_life_support { inline std::pair all_type_info_get_cache(PyTypeObject *type); +// Band-aid workaround to fix a subtle but serious bug in a minimalistic fashion. See PR #4762. +inline void all_type_info_add_base_most_derived_first(std::vector &bases, + type_info *addl_base) { + for (auto it = bases.begin(); it != bases.end(); it++) { + type_info *existing_base = *it; + if (PyType_IsSubtype(addl_base->type, existing_base->type) != 0) { + bases.insert(it, addl_base); + return; + } + } + bases.push_back(addl_base); +} + // Populates a just-created cache entry. PYBIND11_NOINLINE void all_type_info_populate(PyTypeObject *t, std::vector &bases) { + assert(bases.empty()); std::vector check; for (handle parent : reinterpret_borrow(t->tp_bases)) { - check.push_back((PyTypeObject *) parent.ptr()); + check.push_back(reinterpret_cast(parent.ptr())); } - auto const &type_dict = get_internals().registered_types_py; for (size_t i = 0; i < check.size(); i++) { auto *type = check[i]; @@ -136,7 +154,7 @@ PYBIND11_NOINLINE void all_type_info_populate(PyTypeObject *t, std::vectortp_bases) { @@ -150,7 +168,7 @@ PYBIND11_NOINLINE void all_type_info_populate(PyTypeObject *t, std::vector(type->tp_bases)) { - check.push_back((PyTypeObject *) parent.ptr()); + check.push_back(reinterpret_cast(parent.ptr())); } } } @@ -167,13 +185,7 @@ PYBIND11_NOINLINE void all_type_info_populate(PyTypeObject *t, std::vector &all_type_info(PyTypeObject *type) { - auto ins = all_type_info_get_cache(type); - if (ins.second) { - // New cache entry: populate it - all_type_info_populate(type, ins.first->second); - } - - return ins.first->second; + return all_type_info_get_cache(type).first->second; } /** @@ -193,32 +205,73 @@ PYBIND11_NOINLINE detail::type_info *get_type_info(PyTypeObject *type) { return bases.front(); } -inline detail::type_info *get_local_type_info(const std::type_index &tp) { - auto &locals = get_local_internals().registered_types_cpp; - auto it = locals.find(tp); +inline detail::type_info *get_local_type_info_lock_held(const std::type_info &tp) { + const auto &locals = get_local_internals().registered_types_cpp; + auto it = locals.find(&tp); if (it != locals.end()) { return it->second; } return nullptr; } -inline detail::type_info *get_global_type_info(const std::type_index &tp) { - auto &types = get_internals().registered_types_cpp; - auto it = types.find(tp); +inline detail::type_info *get_local_type_info(const std::type_info &tp) { + // NB: internals and local_internals share a single mutex + PYBIND11_LOCK_INTERNALS(get_internals()); + return get_local_type_info_lock_held(tp); +} + +inline detail::type_info *get_global_type_info_lock_held(const std::type_info &tp) { + // This is a two-level lookup. Hopefully we find the type info in + // registered_types_cpp_fast, but if not we try + // registered_types_cpp and fill registered_types_cpp_fast for + // next time. + detail::type_info *type_info = nullptr; + auto &internals = get_internals(); +#if PYBIND11_INTERNALS_VERSION >= 12 + auto &fast_types = internals.registered_types_cpp_fast; +#endif + auto &types = internals.registered_types_cpp; +#if PYBIND11_INTERNALS_VERSION >= 12 + auto fast_it = fast_types.find(&tp); + if (fast_it != fast_types.end()) { +# ifndef NDEBUG + auto types_it = types.find(std::type_index(tp)); + assert(types_it != types.end()); + assert(types_it->second == fast_it->second); +# endif + return fast_it->second; + } +#endif // PYBIND11_INTERNALS_VERSION >= 12 + + auto it = types.find(std::type_index(tp)); if (it != types.end()) { - return it->second; +#if PYBIND11_INTERNALS_VERSION >= 12 + // We found the type in the slow map but not the fast one, so + // some other DSO added it (otherwise it would be in the fast + // map under &tp) and therefore we must be an alias. Record + // that. + it->second->alias_chain.push_front(&tp); + fast_types.emplace(&tp, it->second); +#endif + type_info = it->second; } - return nullptr; + return type_info; +} + +inline detail::type_info *get_global_type_info(const std::type_info &tp) { + PYBIND11_LOCK_INTERNALS(get_internals()); + return get_global_type_info_lock_held(tp); } /// Return the type info for a given C++ type; on lookup failure can either throw or return /// nullptr. -PYBIND11_NOINLINE detail::type_info *get_type_info(const std::type_index &tp, +PYBIND11_NOINLINE detail::type_info *get_type_info(const std::type_info &tp, bool throw_if_missing = false) { - if (auto *ltype = get_local_type_info(tp)) { + PYBIND11_LOCK_INTERNALS(get_internals()); + if (auto *ltype = get_local_type_info_lock_held(tp)) { return ltype; } - if (auto *gtype = get_global_type_info(tp)) { + if (auto *gtype = get_global_type_info_lock_held(tp)) { return gtype; } @@ -233,83 +286,70 @@ PYBIND11_NOINLINE detail::type_info *get_type_info(const std::type_index &tp, 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); + return handle(type_info ? (reinterpret_cast(type_info->type)) : nullptr); } -// Searches the inheritance graph for a registered Python instance, using all_type_info(). -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) { - for (auto *instance_type : detail::all_type_info(Py_TYPE(it_i->second))) { - if (instance_type && same_type(*instance_type->cpptype, *tinfo->cpptype)) { - return handle((PyObject *) it_i->second).inc_ref(); - } - } +inline bool try_incref(PyObject *obj) { + // Tries to increment the reference count of an object if it's not zero. +#if defined(Py_GIL_DISABLED) && PY_VERSION_HEX >= 0x030E00A4 + return PyUnstable_TryIncRef(obj); +#elif defined(Py_GIL_DISABLED) + // See + // https://github.com/python/cpython/blob/d05140f9f77d7dfc753dd1e5ac3a5962aaa03eff/Include/internal/pycore_object.h#L761 + uint32_t local = _Py_atomic_load_uint32_relaxed(&obj->ob_ref_local); + local += 1; + if (local == 0) { + // immortal + return true; } - return handle(); -} - -struct value_and_holder { - instance *inst = nullptr; - size_t index = 0u; - const detail::type_info *type = nullptr; - void **vh = nullptr; - - // Main constructor for a found value/holder: - value_and_holder(instance *i, const detail::type_info *type, size_t vpos, size_t index) - : inst{i}, index{index}, type{type}, - vh{inst->simple_layout ? inst->simple_value_holder - : &inst->nonsimple.values_and_holders[vpos]} {} - - // Default constructor (used to signal a value-and-holder not found by get_value_and_holder()) - value_and_holder() = default; - - // Used for past-the-end iterator - explicit value_and_holder(size_t index) : index{index} {} - - template - V *&value_ptr() const { - return reinterpret_cast(vh[0]); + if (_Py_IsOwnedByCurrentThread(obj)) { + _Py_atomic_store_uint32_relaxed(&obj->ob_ref_local, local); +# ifdef Py_REF_DEBUG + _Py_INCREF_IncRefTotal(); +# endif + return true; } - // True if this `value_and_holder` has a non-null value pointer - explicit operator bool() const { return value_ptr() != nullptr; } + Py_ssize_t shared = _Py_atomic_load_ssize_relaxed(&obj->ob_ref_shared); + for (;;) { + // If the shared refcount is zero and the object is either merged + // or may not have weak references, then we cannot incref it. + if (shared == 0 || shared == _Py_REF_MERGED) { + return false; + } - template - H &holder() const { - return reinterpret_cast(vh[1]); - } - bool holder_constructed() const { - return inst->simple_layout - ? inst->simple_holder_constructed - : (inst->nonsimple.status[index] & instance::status_holder_constructed) != 0u; - } - // 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) { - inst->nonsimple.status[index] |= instance::status_holder_constructed; - } else { - inst->nonsimple.status[index] &= (std::uint8_t) ~instance::status_holder_constructed; + if (_Py_atomic_compare_exchange_ssize( + &obj->ob_ref_shared, &shared, shared + (1 << _Py_REF_SHARED_SHIFT))) { +# ifdef Py_REF_DEBUG + _Py_INCREF_IncRefTotal(); +# endif + return true; } } - bool instance_registered() const { - return inst->simple_layout - ? inst->simple_instance_registered - : ((inst->nonsimple.status[index] & instance::status_instance_registered) != 0); - } - // 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) { - inst->nonsimple.status[index] |= instance::status_instance_registered; - } else { - inst->nonsimple.status[index] &= (std::uint8_t) ~instance::status_instance_registered; +#else + assert(Py_REFCNT(obj) > 0); + Py_INCREF(obj); + return true; +#endif +} + +// Searches the inheritance graph for a registered Python instance, using all_type_info(). +PYBIND11_NOINLINE handle find_registered_python_instance(void *src, + const detail::type_info *tinfo) { + return with_instance_map(src, [&](instance_map &instances) { + auto it_instances = instances.equal_range(src); + for (auto it_i = it_instances.first; it_i != it_instances.second; ++it_i) { + for (auto *instance_type : detail::all_type_info(Py_TYPE(it_i->second))) { + if (instance_type && same_type(*instance_type->cpptype, *tinfo->cpptype)) { + auto *wrapper = reinterpret_cast(it_i->second); + if (try_incref(wrapper)) { + return handle(wrapper); + } + } + } } - } -}; + return handle(); + }); +} // Container for accessing and iterating over an instance's values/holders struct values_and_holders { @@ -322,18 +362,29 @@ struct values_and_holders { explicit values_and_holders(instance *inst) : inst{inst}, tinfo(all_type_info(Py_TYPE(inst))) {} + explicit values_and_holders(PyObject *obj) + : inst{nullptr}, tinfo(all_type_info(Py_TYPE(obj))) { + if (!tinfo.empty()) { + inst = reinterpret_cast(obj); + } + } + struct iterator { private: instance *inst = nullptr; const type_vec *types = nullptr; value_and_holder curr; friend struct values_and_holders; - iterator(instance *inst, const type_vec *tinfo) - : inst{inst}, types{tinfo}, - curr(inst /* instance */, - types->empty() ? nullptr : (*types)[0] /* type info */, - 0, /* vpos: (non-simple types only): the first vptr comes first */ - 0 /* index */) {} + iterator(instance *inst, const type_vec *tinfo) : inst{inst}, types{tinfo} { + if (inst != nullptr) { + assert(!types->empty()); + curr = value_and_holder( + inst /* instance */, + (*types)[0] /* type info */, + 0, /* vpos: (non-simple types only): the first vptr comes first */ + 0 /* index */); + } + } // Past-the-end iterator: explicit iterator(size_t end) : curr(end) {} @@ -364,6 +415,16 @@ struct values_and_holders { } size_t size() { return tinfo.size(); } + + // Band-aid workaround to fix a subtle but serious bug in a minimalistic fashion. See PR #4762. + bool is_redundant_value_and_holder(const value_and_holder &vh) { + for (size_t i = 0; i < vh.index; i++) { + if (PyType_IsSubtype(tinfo[i]->type, tinfo[vh.index]->type) != 0) { + return true; + } + } + return false; + } }; /** @@ -445,7 +506,7 @@ PYBIND11_NOINLINE void instance::allocate_layout() { // efficient for small allocations like the one we're doing here; // for larger allocations they are just wrappers around malloc. // TODO: is this still true for pure Python 3.6? - nonsimple.values_and_holders = (void **) PyMem_Calloc(space, sizeof(void *)); + nonsimple.values_and_holders = static_cast(PyMem_Calloc(space, sizeof(void *))); if (!nonsimple.values_and_holders) { throw std::bad_alloc(); } @@ -458,7 +519,7 @@ PYBIND11_NOINLINE void instance::allocate_layout() { // NOLINTNEXTLINE(readability-make-member-function-const) PYBIND11_NOINLINE void instance::deallocate_layout() { if (!simple_layout) { - PyMem_Free(nonsimple.values_and_holders); + PyMem_Free(reinterpret_cast(nonsimple.values_and_holders)); } } @@ -471,30 +532,457 @@ PYBIND11_NOINLINE bool isinstance_generic(handle obj, const std::type_info &tp) } 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) { - for (const auto &vh : values_and_holders(it->second)) { - if (vh.type == type) { - return handle((PyObject *) it->second); + return with_instance_map(ptr, [&](instance_map &instances) { + auto range = instances.equal_range(ptr); + for (auto it = range.first; it != range.second; ++it) { + for (const auto &vh : values_and_holders(it->second)) { + if (vh.type == type) { + return handle(reinterpret_cast(it->second)); + } } } - } - return handle(); + return handle(); + }); } -inline PyThreadState *get_thread_state_unchecked() { -#if defined(PYPY_VERSION) - return PyThreadState_GET(); -#else - return _PyThreadState_UncheckedGet(); -#endif -} +// Information about how type_caster_generic::cast() can obtain its source object +struct cast_sources { + // A type-erased pointer and the type it points to + struct raw_source { + const void *cppobj; + const std::type_info *cpptype; + }; + + // A C++ pointer and the Python type info we will convert it to; + // we expect that cppobj points to something of type tinfo->cpptype + struct resolved_source { + const void *cppobj; + const type_info *tinfo; + }; + + // Use the given pointer with its compile-time type, possibly downcast + // via polymorphic_type_hook() + template + explicit cast_sources(const itype *ptr); + + // Use the given pointer and type + // NOLINTNEXTLINE(google-explicit-constructor) + cast_sources(const raw_source &orig) : original(orig) { result = resolve(); } + + // Use the given object and pybind11 type info. NB: if tinfo is null, + // this does not provide enough information to use a foreign type or + // to render a useful error message + cast_sources(const void *obj, const detail::type_info *tinfo) + : original{obj, tinfo ? tinfo->cpptype : nullptr}, result{obj, tinfo} {} + + // The object passed to cast(), with its static type. + // original.type must not be null if resolve() will be called. + // original.obj may be null if we're converting nullptr to a Python None + raw_source original; + + // A more-derived version of `original` provided by a + // polymorphic_type_hook. downcast.type may be null if this is not + // a relevant concept for the current cast. + raw_source downcast{}; + + // The source to use for this cast, and the corresponding pybind11 + // type_info. If the type_info is null, then pybind11 doesn't know + // about this type. + resolved_source result; + + // Returns true if the cast will use a pybind11 type that uses + // a smart holder. + bool creates_smart_holder() const { + return result.tinfo != nullptr + && result.tinfo->holder_enum_v == detail::holder_enum_t::smart_holder; + } + +private: + resolved_source resolve() { + if (downcast.cpptype) { + if (same_type(*original.cpptype, *downcast.cpptype)) { + downcast.cpptype = nullptr; + } else if (const auto *tpi = get_type_info(*downcast.cpptype)) { + return {downcast.cppobj, tpi}; + } + } + if (const auto *tpi = get_type_info(*original.cpptype)) { + return {original.cppobj, tpi}; + } + return {nullptr, nullptr}; + } +}; // Forward declarations void keep_alive_impl(handle nurse, handle patient); inline PyObject *make_new_instance(PyTypeObject *type); +PYBIND11_WARNING_PUSH +PYBIND11_WARNING_DISABLE_GCC("-Wredundant-decls") + +// PYBIND11:REMINDER: Needs refactoring of existing pybind11 code. +inline bool deregister_instance(instance *self, void *valptr, const type_info *tinfo); + +PYBIND11_WARNING_POP + +PYBIND11_NAMESPACE_BEGIN(smart_holder_type_caster_support) + +struct value_and_holder_helper { + value_and_holder loaded_v_h; + + bool have_holder() const { + return loaded_v_h.vh != nullptr && loaded_v_h.holder_constructed(); + } + + smart_holder &holder() const { return loaded_v_h.holder(); } + + void throw_if_uninitialized_or_disowned_holder(const char *typeid_name) const { + static const std::string missing_value_msg = "Missing value for wrapped C++ type `"; + if (!holder().is_populated) { + throw value_error(missing_value_msg + clean_type_id(typeid_name) + + "`: Python instance is uninitialized."); + } + if (!holder().has_pointee()) { + throw value_error(missing_value_msg + clean_type_id(typeid_name) + + "`: Python instance was disowned."); + } + } + + void throw_if_uninitialized_or_disowned_holder(const std::type_info &type_info) const { + throw_if_uninitialized_or_disowned_holder(type_info.name()); + } + + // have_holder() must be true or this function will fail. + void throw_if_instance_is_currently_owned_by_shared_ptr(const type_info *tinfo) const { + auto *vptr_gd_ptr = tinfo->get_memory_guarded_delete(holder().vptr); + if (vptr_gd_ptr != nullptr && !vptr_gd_ptr->released_ptr.expired()) { + throw value_error("Python instance is currently owned by a std::shared_ptr."); + } + } + + void *get_void_ptr_or_nullptr() const { + if (have_holder()) { + auto &hld = holder(); + if (hld.is_populated && hld.has_pointee()) { + return hld.template as_raw_ptr_unowned(); + } + } + return nullptr; + } +}; + +template +handle smart_holder_from_unique_ptr(std::unique_ptr &&src, + return_value_policy policy, + handle parent, + const cast_sources::resolved_source &cs) { + if (policy == return_value_policy::copy) { + throw cast_error("return_value_policy::copy is invalid for unique_ptr."); + } + if (!src) { + return none().release(); + } + // cs.cppobj is the subobject pointer appropriate for tinfo (may differ from src.get() + // under MI/VI). Use this for Python identity/registration, but keep ownership on T*. + void *src_raw_void_ptr = const_cast(cs.cppobj); + assert(cs.tinfo != nullptr); + const detail::type_info *tinfo = cs.tinfo; + if (handle existing_inst = find_registered_python_instance(src_raw_void_ptr, tinfo)) { + auto *self_life_support = tinfo->get_trampoline_self_life_support(src.get()); + if (self_life_support != nullptr) { + value_and_holder &v_h = self_life_support->v_h; + if (v_h.inst != nullptr && v_h.vh != nullptr) { + auto &holder = v_h.holder(); + if (!holder.is_disowned) { + pybind11_fail("smart_holder_from_unique_ptr: unexpected " + "smart_holder.is_disowned failure."); + } + // Critical transfer-of-ownership section. This must stay together. + self_life_support->deactivate_life_support(); + holder.reclaim_disowned(tinfo->get_memory_guarded_delete); + (void) src.release(); + // Critical section end. + return existing_inst; + } + } + throw cast_error("Invalid unique_ptr: another instance owns this pointer already."); + } + + auto inst = reinterpret_steal(make_new_instance(tinfo->type)); + auto *inst_raw_ptr = reinterpret_cast(inst.ptr()); + inst_raw_ptr->owned = true; + void *&valueptr = values_and_holders(inst_raw_ptr).begin()->value_ptr(); + valueptr = src_raw_void_ptr; + + if (static_cast(src.get()) == src_raw_void_ptr) { + // This is a multiple-inheritance situation that is incompatible with the current + // shared_from_this handling (see PR #3023). Is there a better solution? + src_raw_void_ptr = nullptr; + } + auto smhldr = smart_holder::from_unique_ptr(std::move(src), src_raw_void_ptr); + tinfo->init_instance(inst_raw_ptr, static_cast(&smhldr)); + + if (policy == return_value_policy::reference_internal) { + keep_alive_impl(inst, parent); + } + + return inst.release(); +} + +template +handle smart_holder_from_unique_ptr(std::unique_ptr &&src, + return_value_policy policy, + handle parent, + const cast_sources::resolved_source &cs) { + return smart_holder_from_unique_ptr( + std::unique_ptr(const_cast(src.release()), + std::move(src.get_deleter())), // Const2Mutbl + policy, + parent, + cs); +} + +template +handle smart_holder_from_shared_ptr(const std::shared_ptr &src, + return_value_policy policy, + handle parent, + const cast_sources::resolved_source &cs) { + switch (policy) { + case return_value_policy::automatic: + 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: + case return_value_policy::move: + break; + case return_value_policy::reference: + throw cast_error("Invalid return_value_policy for shared_ptr (reference)."); + case return_value_policy::reference_internal: + break; + } + if (!src) { + return none().release(); + } + + // cs.cppobj is the subobject pointer appropriate for tinfo (may differ from src.get() + // under MI/VI). Use this for Python identity/registration, but keep ownership on T*. + void *src_raw_void_ptr = const_cast(cs.cppobj); + assert(cs.tinfo != nullptr); + const detail::type_info *tinfo = cs.tinfo; + if (handle existing_inst = find_registered_python_instance(src_raw_void_ptr, tinfo)) { + // PYBIND11:REMINDER: MISSING: Enforcement of consistency with existing smart_holder. + // PYBIND11:REMINDER: MISSING: keep_alive. + return existing_inst; + } + + auto inst = reinterpret_steal(make_new_instance(tinfo->type)); + auto *inst_raw_ptr = reinterpret_cast(inst.ptr()); + inst_raw_ptr->owned = true; + void *&valueptr = values_and_holders(inst_raw_ptr).begin()->value_ptr(); + valueptr = src_raw_void_ptr; + + auto smhldr = smart_holder::from_shared_ptr(std::shared_ptr(src, src_raw_void_ptr)); + tinfo->init_instance(inst_raw_ptr, static_cast(&smhldr)); + + if (policy == return_value_policy::reference_internal) { + keep_alive_impl(inst, parent); + } + + return inst.release(); +} + +template +handle smart_holder_from_shared_ptr(const std::shared_ptr &src, + return_value_policy policy, + handle parent, + const cast_sources::resolved_source &cs) { + return smart_holder_from_shared_ptr(std::const_pointer_cast(src), // Const2Mutbl + policy, + parent, + cs); +} + +struct shared_ptr_parent_life_support { + PyObject *parent; + explicit shared_ptr_parent_life_support(PyObject *parent) : parent{parent} { + Py_INCREF(parent); + } + // NOLINTNEXTLINE(readability-make-member-function-const) + void operator()(void *) { + gil_scoped_acquire gil; + Py_DECREF(parent); + } +}; + +struct shared_ptr_trampoline_self_life_support { + PyObject *self; + explicit shared_ptr_trampoline_self_life_support(instance *inst) + : self{reinterpret_cast(inst)} { + gil_scoped_acquire gil; + Py_INCREF(self); + } + // NOLINTNEXTLINE(readability-make-member-function-const) + void operator()(void *) { + gil_scoped_acquire gil; + Py_DECREF(self); + } +}; + +template ::value, int>::type = 0> +inline std::unique_ptr unique_with_deleter(T *raw_ptr, std::unique_ptr &&deleter) { + if (deleter == nullptr) { + return std::unique_ptr(raw_ptr); + } + return std::unique_ptr(raw_ptr, std::move(*deleter)); +} + +template ::value, int>::type = 0> +inline std::unique_ptr unique_with_deleter(T *raw_ptr, std::unique_ptr &&deleter) { + if (deleter == nullptr) { + pybind11_fail("smart_holder_type_casters: deleter is not default constructible and no" + " instance available to return."); + } + return std::unique_ptr(raw_ptr, std::move(*deleter)); +} + +template +struct load_helper : value_and_holder_helper { + bool was_populated = false; + bool python_instance_is_alias = false; + + void maybe_set_python_instance_is_alias(handle src) { + if (was_populated) { + python_instance_is_alias = reinterpret_cast(src.ptr())->is_alias; + } + } + + static std::shared_ptr make_shared_ptr_with_responsible_parent(T *raw_ptr, handle parent) { + return std::shared_ptr(raw_ptr, shared_ptr_parent_life_support(parent.ptr())); + } + + std::shared_ptr load_as_shared_ptr(const type_info *tinfo, + void *void_raw_ptr, + handle responsible_parent = nullptr, + // to support py::potentially_slicing_weak_ptr + // with minimal added code complexity: + bool force_potentially_slicing_shared_ptr + = false) const { + if (!have_holder()) { + return nullptr; + } + throw_if_uninitialized_or_disowned_holder(typeid(T)); + smart_holder &hld = holder(); + hld.ensure_is_not_disowned("load_as_shared_ptr"); + if (hld.vptr_is_using_noop_deleter) { + if (responsible_parent) { + return make_shared_ptr_with_responsible_parent(static_cast(void_raw_ptr), + responsible_parent); + } + throw std::runtime_error("Non-owning holder (load_as_shared_ptr)."); + } + auto *type_raw_ptr = static_cast(void_raw_ptr); + if (python_instance_is_alias && !force_potentially_slicing_shared_ptr) { + auto *vptr_gd_ptr = tinfo->get_memory_guarded_delete(holder().vptr); + if (vptr_gd_ptr != nullptr) { + std::shared_ptr released_ptr = vptr_gd_ptr->released_ptr.lock(); + if (released_ptr) { + return std::shared_ptr(released_ptr, type_raw_ptr); + } + std::shared_ptr to_be_released( + type_raw_ptr, shared_ptr_trampoline_self_life_support(loaded_v_h.inst)); + vptr_gd_ptr->released_ptr = to_be_released; + return to_be_released; + } + auto *sptsls_ptr = std::get_deleter(hld.vptr); + if (sptsls_ptr != nullptr) { + // This code is reachable only if there are multiple registered_instances for the + // same pointee. + if (reinterpret_cast(loaded_v_h.inst) == sptsls_ptr->self) { + pybind11_fail("smart_holder_type_caster_support load_as_shared_ptr failure: " + "loaded_v_h.inst == sptsls_ptr->self"); + } + } + if (sptsls_ptr != nullptr || !memory::type_has_shared_from_this(type_raw_ptr)) { + return std::shared_ptr( + type_raw_ptr, shared_ptr_trampoline_self_life_support(loaded_v_h.inst)); + } + if (hld.vptr_is_external_shared_ptr) { + pybind11_fail("smart_holder_type_casters load_as_shared_ptr failure: not " + "implemented: trampoline-self-life-support for external shared_ptr " + "to type inheriting from std::enable_shared_from_this."); + } + pybind11_fail( + "smart_holder_type_casters: load_as_shared_ptr failure: internal inconsistency."); + } + std::shared_ptr void_shd_ptr = hld.template as_shared_ptr(); + return std::shared_ptr(void_shd_ptr, type_raw_ptr); + } + + template + std::unique_ptr load_as_unique_ptr(const type_info *tinfo, + void *raw_void_ptr, + const char *context = "load_as_unique_ptr") { + if (!have_holder()) { + return unique_with_deleter(nullptr, std::unique_ptr()); + } + throw_if_uninitialized_or_disowned_holder(typeid(T)); + throw_if_instance_is_currently_owned_by_shared_ptr(tinfo); + holder().ensure_is_not_disowned(context); + holder().template ensure_compatible_uqp_del(context); + holder().ensure_use_count_1(context); + + T *raw_type_ptr = static_cast(raw_void_ptr); + + auto *self_life_support = tinfo->get_trampoline_self_life_support(raw_type_ptr); + // This is enforced indirectly by a static_assert in the class_ implementation: + assert(!python_instance_is_alias || self_life_support); + + std::unique_ptr extracted_deleter + = holder().template extract_deleter(context, tinfo->get_memory_guarded_delete); + + // Critical transfer-of-ownership section. This must stay together. + if (self_life_support != nullptr) { + holder().disown(tinfo->get_memory_guarded_delete); + } else { + holder().release_ownership(tinfo->get_memory_guarded_delete); + } + auto result = unique_with_deleter(raw_type_ptr, std::move(extracted_deleter)); + if (self_life_support != nullptr) { + self_life_support->activate_life_support(loaded_v_h); + } else { + void *value_void_ptr = loaded_v_h.value_ptr(); + loaded_v_h.value_ptr() = nullptr; + deregister_instance(loaded_v_h.inst, value_void_ptr, loaded_v_h.type); + } + // Critical section end. + + return result; + } + + // This assumes load_as_shared_ptr succeeded(), and the returned shared_ptr is still alive. + // The returned unique_ptr is meant to never expire (the behavior is undefined otherwise). + template + std::unique_ptr load_as_const_unique_ptr(const type_info *tinfo, + T *raw_type_ptr, + const char *context + = "load_as_const_unique_ptr") { + if (!have_holder()) { + return unique_with_deleter(nullptr, std::unique_ptr()); + } + holder().template ensure_compatible_uqp_del(context); + return unique_with_deleter(raw_type_ptr, + std::move(holder().template extract_deleter( + context, tinfo->get_memory_guarded_delete))); + } +}; + +PYBIND11_NAMESPACE_END(smart_holder_type_caster_support) + class type_caster_generic { public: PYBIND11_NOINLINE explicit type_caster_generic(const std::type_info &type_info) @@ -505,21 +993,51 @@ class type_caster_generic { bool load(handle src, bool convert) { return load_impl(src, convert); } - PYBIND11_NOINLINE static handle cast(const void *_src, + static handle cast(const void *src, + return_value_policy policy, + handle parent, + const detail::type_info *tinfo, + void *(*copy_constructor)(const void *), + void *(*move_constructor)(const void *), + const void *existing_holder = nullptr) { + cast_sources srcs{src, tinfo}; + return cast(srcs, policy, parent, copy_constructor, move_constructor, existing_holder); + } + + static handle cast_non_owning(const cast_sources &srcs, + return_value_policy policy, + handle parent, + const void *existing_holder = nullptr) { + // Reference-like policies alias an existing C++ object instead of creating + // a new one, so copy/move constructor callbacks must remain null here. + assert(policy == return_value_policy::reference + || policy == return_value_policy::reference_internal + || policy == return_value_policy::automatic_reference); + return cast(srcs, policy, parent, nullptr, nullptr, existing_holder); + } + + PYBIND11_NOINLINE static handle cast(const cast_sources &srcs, return_value_policy policy, handle parent, - const detail::type_info *tinfo, void *(*copy_constructor)(const void *), void *(*move_constructor)(const void *), const void *existing_holder = nullptr) { - if (!tinfo) { // no type info: error will be set already + if (!srcs.result.tinfo) { + // No pybind11 type info. Raise an exception. + std::string tname = srcs.downcast.cpptype ? srcs.downcast.cpptype->name() + : srcs.original.cpptype ? srcs.original.cpptype->name() + : ""; + detail::clean_type_id(tname); + std::string msg = "Unregistered type : " + tname; + set_error(PyExc_TypeError, msg.c_str()); return handle(); } - void *src = const_cast(_src); + void *src = const_cast(srcs.result.cppobj); if (src == nullptr) { return none().release(); } + const type_info *tinfo = srcs.result.tinfo; if (handle registered_inst = find_registered_python_instance(src, tinfo)) { return registered_inst; @@ -599,6 +1117,15 @@ class type_caster_generic { // Base methods for generic caster; there are overridden in copyable_holder_caster void load_value(value_and_holder &&v_h) { + if (typeinfo->holder_enum_v == detail::holder_enum_t::smart_holder) { + smart_holder_type_caster_support::value_and_holder_helper v_h_helper; + v_h_helper.loaded_v_h = v_h; + if (v_h_helper.have_holder()) { + v_h_helper.throw_if_uninitialized_or_disowned_holder(cpptype->name()); + value = v_h_helper.holder().template as_raw_ptr_unowned(); + return; + } + } auto *&vptr = v_h.value_ptr(); // Lazy allocation for unallocated values: if (vptr == nullptr) { @@ -637,7 +1164,15 @@ class type_caster_generic { } return false; } + bool try_cpp_conduit(handle src) { + value = try_raw_pointer_ephemeral_from_cpp_conduit(src, cpptype); + if (value != nullptr) { + return true; + } + return false; + } void check_holder_compat() {} + bool set_foreign_holder(handle) { return true; } PYBIND11_NOINLINE static void *local_load(PyObject *src, const type_info *ti) { auto caster = type_caster_generic(ti); @@ -676,14 +1211,14 @@ class type_caster_generic { // logic (without having to resort to virtual inheritance). template PYBIND11_NOINLINE bool load_impl(handle src, bool convert) { + auto &this_ = static_cast(*this); if (!src) { return false; } if (!typeinfo) { - return try_load_foreign_module_local(src); + return try_load_foreign_module_local(src) && this_.set_foreign_holder(src); } - auto &this_ = static_cast(*this); this_.check_holder_compat(); PyTypeObject *srctype = Py_TYPE(src.ptr()); @@ -749,13 +1284,13 @@ class type_caster_generic { if (typeinfo->module_local) { if (auto *gtype = get_global_type_info(*typeinfo->cpptype)) { typeinfo = gtype; - return load(src, false); + return load_impl(src, false); } } // Global typeinfo has precedence over foreign module_local if (try_load_foreign_module_local(src)) { - return true; + return this_.set_foreign_holder(src); } // Custom converters didn't take None, now we convert None to nullptr. @@ -768,26 +1303,11 @@ class type_caster_generic { return true; } - return false; - } - - // Called to do type lookup and wrap the pointer and type in a pair when a dynamic_cast - // isn't needed or can't be used. If the type is unknown, sets the error and returns a pair - // with .second = nullptr. (p.first = nullptr is not an error: it becomes None). - PYBIND11_NOINLINE static std::pair - src_and_type(const void *src, - const std::type_info &cast_type, - const std::type_info *rtti_type = nullptr) { - if (auto *tpi = get_type_info(cast_type)) { - return {src, const_cast(tpi)}; + if (convert && cpptype && this_.try_cpp_conduit(src)) { + return this_.set_foreign_holder(src); } - // Not found, set error: - std::string tname = rtti_type ? rtti_type->name() : cast_type.name(); - detail::clean_type_id(tname); - std::string msg = "Unregistered type : " + tname; - PyErr_SetString(PyExc_TypeError, msg.c_str()); - return {nullptr, nullptr}; + return false; } const type_info *typeinfo = nullptr; @@ -795,6 +1315,32 @@ class type_caster_generic { void *value = nullptr; }; +inline object cpp_conduit_method(handle self, + const bytes &pybind11_platform_abi_id, + const capsule &cpp_type_info_capsule, + const bytes &pointer_kind) { +#ifdef PYBIND11_HAS_STRING_VIEW + using cpp_str = std::string_view; +#else + using cpp_str = std::string; +#endif + if (cpp_str(pybind11_platform_abi_id) != PYBIND11_PLATFORM_ABI_ID) { + return none(); + } + if (std::strcmp(cpp_type_info_capsule.name(), typeid(std::type_info).name()) != 0) { + return none(); + } + if (cpp_str(pointer_kind) != "raw_pointer_ephemeral") { + throw std::runtime_error("Invalid pointer_kind: \"" + std::string(pointer_kind) + "\""); + } + const auto *cpp_type_info = cpp_type_info_capsule.get_pointer(); + type_caster_generic caster(*cpp_type_info); + if (!caster.load(self, false)) { + return none(); + } + return capsule(caster.value, cpp_type_info->name()); +} + /** * Determine suitable casting operator for pointer-or-lvalue-casting type casters. The type caster * needs to provide `operator T*()` and `operator T&()` operators. @@ -1058,6 +1604,20 @@ struct polymorphic_type_hook : public polymorphic_type_hook_base {}; PYBIND11_NAMESPACE_BEGIN(detail) +template +cast_sources::cast_sources(const itype *ptr) : original{ptr, &typeid(itype)} { + // If this is a base pointer to a derived type, and the derived type is + // registered with pybind11, we want to make the full derived object + // available. In the typical case where itype is polymorphic, we get the + // correct derived pointer (which may be != base pointer) by a dynamic_cast + // to most derived type. If itype is not polymorphic, a user-provided + // specialization of polymorphic_type_hook can do the same thing. + // If there is no downcast to perform, then the default hook will leave + // derived.type set to nullptr, which causes us to ignore derived.obj. + downcast.cppobj = polymorphic_type_hook::get(ptr, downcast.cpptype); + result = resolve(); +} + /// Generic type caster for objects stored on the heap template class type_caster_base : public type_caster_generic { @@ -1069,62 +1629,44 @@ class type_caster_base : public type_caster_generic { type_caster_base() : type_caster_base(typeid(type)) {} explicit type_caster_base(const std::type_info &info) : type_caster_generic(info) {} + // Wrap the generic cast_sources to be only constructible from the type + // that's correct in this context, so you can't use type_caster_base + // to convert an unrelated B* to Python. + struct cast_sources : detail::cast_sources { + explicit cast_sources(const itype *ptr) : detail::cast_sources(ptr) {} + }; + static handle cast(const itype &src, return_value_policy policy, handle parent) { if (policy == return_value_policy::automatic || policy == return_value_policy::automatic_reference) { policy = return_value_policy::copy; } - return cast(&src, policy, parent); + return cast(std::addressof(src), policy, parent); } static handle cast(itype &&src, return_value_policy, handle parent) { - return cast(&src, return_value_policy::move, parent); - } - - // Returns a (pointer, type_info) pair taking care of necessary type lookup for a - // polymorphic type (using RTTI by default, but can be overridden by specializing - // polymorphic_type_hook). If the instance isn't derived, returns the base version. - static std::pair src_and_type(const itype *src) { - const auto &cast_type = typeid(itype); - const std::type_info *instance_type = nullptr; - const void *vsrc = polymorphic_type_hook::get(src, instance_type); - if (instance_type && !same_type(cast_type, *instance_type)) { - // This is a base pointer to a derived type. If the derived type is registered - // with pybind11, we want to make the full derived object available. - // In the typical case where itype is polymorphic, we get the correct - // derived pointer (which may be != base pointer) by a dynamic_cast to - // most derived type. If itype is not polymorphic, we won't get here - // except via a user-provided specialization of polymorphic_type_hook, - // and the user has promised that no this-pointer adjustment is - // required in that case, so it's OK to use static_cast. - if (const auto *tpi = get_type_info(*instance_type)) { - return {vsrc, tpi}; - } - } - // Otherwise we have either a nullptr, an `itype` pointer, or an unknown derived pointer, - // so don't do a cast - return type_caster_generic::src_and_type(src, cast_type, instance_type); + return cast(std::addressof(src), return_value_policy::move, parent); } static handle cast(const itype *src, return_value_policy policy, handle parent) { - auto st = src_and_type(src); - return type_caster_generic::cast(st.first, + return cast(cast_sources{src}, policy, parent); + } + + static handle cast(const cast_sources &srcs, return_value_policy policy, handle parent) { + return type_caster_generic::cast(srcs, policy, parent, - st.second, - make_copy_constructor(src), - make_move_constructor(src)); + make_copy_constructor((const itype *) nullptr), + make_move_constructor((const itype *) nullptr)); } static handle cast_holder(const itype *src, const void *holder) { - auto st = src_and_type(src); - return type_caster_generic::cast(st.first, - return_value_policy::take_ownership, - {}, - st.second, - nullptr, - nullptr, - holder); + return cast_holder(cast_sources{src}, holder); + } + + static handle cast_holder(const cast_sources &srcs, const void *holder) { + auto policy = return_value_policy::take_ownership; + return type_caster_generic::cast(srcs, policy, {}, nullptr, nullptr, holder); } template @@ -1164,13 +1706,17 @@ class type_caster_base : public type_caster_generic { static Constructor make_move_constructor(...) { return nullptr; } }; +inline std::string quote_cpp_type_name(const std::string &cpp_type_name) { + return cpp_type_name; // No-op for now. See PR #4888 +} + PYBIND11_NOINLINE std::string type_info_description(const std::type_info &ti) { if (auto *type_data = get_type_info(ti)) { - handle th((PyObject *) type_data->type); + handle th(reinterpret_cast(type_data->type)); return th.attr("__module__").cast() + '.' + th.attr("__qualname__").cast(); } - return clean_type_id(ti.name()); + return quote_cpp_type_name(clean_type_id(ti.name())); } PYBIND11_NAMESPACE_END(detail) diff --git a/external_libraries/pybind11/include/pybind11/detail/using_smart_holder.h b/external_libraries/pybind11/include/pybind11/detail/using_smart_holder.h new file mode 100644 index 00000000..90314896 --- /dev/null +++ b/external_libraries/pybind11/include/pybind11/detail/using_smart_holder.h @@ -0,0 +1,22 @@ +// Copyright (c) 2024 The Pybind Development Team. +// All rights reserved. Use of this source code is governed by a +// BSD-style license that can be found in the LICENSE file. + +#pragma once + +#include "common.h" +#include "struct_smart_holder.h" + +#include + +PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE) + +using pybind11::memory::smart_holder; + +PYBIND11_NAMESPACE_BEGIN(detail) + +template +using is_smart_holder = std::is_same; + +PYBIND11_NAMESPACE_END(detail) +PYBIND11_NAMESPACE_END(PYBIND11_NAMESPACE) diff --git a/external_libraries/pybind11/include/pybind11/detail/value_and_holder.h b/external_libraries/pybind11/include/pybind11/detail/value_and_holder.h new file mode 100644 index 00000000..b24551e6 --- /dev/null +++ b/external_libraries/pybind11/include/pybind11/detail/value_and_holder.h @@ -0,0 +1,92 @@ +// Copyright (c) 2016-2024 The Pybind Development Team. +// All rights reserved. Use of this source code is governed by a +// BSD-style license that can be found in the LICENSE file. + +#pragma once + +#include "common.h" + +#include +#include +#include + +PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE) +PYBIND11_NAMESPACE_BEGIN(detail) + +struct value_and_holder { + instance *inst = nullptr; + size_t index = 0u; + const detail::type_info *type = nullptr; + void **vh = nullptr; + + // Main constructor for a found value/holder: + value_and_holder(instance *i, const detail::type_info *type, size_t vpos, size_t index) + : inst{i}, index{index}, type{type}, + vh{inst->simple_layout ? inst->simple_value_holder + : &inst->nonsimple.values_and_holders[vpos]} {} + + // Default constructor (used to signal a value-and-holder not found by get_value_and_holder()) + value_and_holder() = default; + + // Used for past-the-end iterator + explicit value_and_holder(size_t index) : index{index} {} + + template + V *&value_ptr() const { + return reinterpret_cast(vh[0]); + } + // True if this `value_and_holder` has a non-null value pointer + explicit operator bool() const { return value_ptr() != nullptr; } + + template + H &holder() const { + return reinterpret_cast(vh[1]); + } + bool holder_constructed() const { + return inst->simple_layout + ? inst->simple_holder_constructed + : (inst->nonsimple.status[index] & instance::status_holder_constructed) != 0u; + } + // 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) { + inst->nonsimple.status[index] |= instance::status_holder_constructed; + } else { + inst->nonsimple.status[index] + &= static_cast(~instance::status_holder_constructed); + } + } + bool instance_registered() const { + return inst->simple_layout + ? inst->simple_instance_registered + : ((inst->nonsimple.status[index] & instance::status_instance_registered) != 0); + } + // 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) { + inst->nonsimple.status[index] |= instance::status_instance_registered; + } else { + inst->nonsimple.status[index] + &= static_cast(~instance::status_instance_registered); + } + } +}; + +// This is a semi-public API to check if the corresponding instance has been constructed with a +// holder. That is, if the instance has been constructed with a holder, the `__init__` method is +// called and the C++ object is valid. Otherwise, the C++ object might only be allocated, but not +// initialized. This will lead to **SEGMENTATION FAULTS** if the C++ object is used in any way. +// Example usage: https://pybind11.readthedocs.io/en/stable/advanced/classes.html#custom-type-setup +// for `tp_traverse` and `tp_clear` implementations. +// WARNING: The caller is responsible for ensuring that the `reinterpret_cast` is valid. +inline bool is_holder_constructed(PyObject *obj) { + auto *const instance = reinterpret_cast(obj); + return instance->get_value_and_holder().holder_constructed(); +} + +PYBIND11_NAMESPACE_END(detail) +PYBIND11_NAMESPACE_END(PYBIND11_NAMESPACE) diff --git a/external_libraries/pybind11/include/pybind11/eigen/matrix.h b/external_libraries/pybind11/include/pybind11/eigen/matrix.h index 8d4342f8..ca599c95 100644 --- a/external_libraries/pybind11/include/pybind11/eigen/matrix.h +++ b/external_libraries/pybind11/include/pybind11/eigen/matrix.h @@ -9,7 +9,8 @@ #pragma once -#include "../numpy.h" +#include + #include "common.h" /* HINT: To suppress warnings originating from the Eigen headers, use -isystem. @@ -224,19 +225,22 @@ struct EigenProps { = !show_c_contiguous && show_order && requires_col_major; static constexpr auto descriptor - = const_name("numpy.ndarray[") + npy_format_descriptor::name + const_name("[") + = const_name("typing.Annotated[") + + io_name("numpy.typing.ArrayLike, ", "numpy.typing.NDArray[") + + npy_format_descriptor::name + io_name("", "]") + const_name(", \"[") + const_name(const_name<(size_t) rows>(), const_name("m")) + const_name(", ") - + const_name(const_name<(size_t) cols>(), const_name("n")) + const_name("]") - + + + const_name(const_name<(size_t) cols>(), const_name("n")) + + const_name("]\"") // For a reference type (e.g. Ref) we have other constraints that might need to // be satisfied: writeable=True (for a mutable reference), and, depending on the map's // stride options, possibly f_contiguous or c_contiguous. We include them in the // descriptor output to provide some hint as to why a TypeError is occurring (otherwise - // it can be confusing to see that a function accepts a 'numpy.ndarray[float64[3,2]]' and - // an error message that you *gave* a numpy.ndarray of the right type and dimensions. - const_name(", flags.writeable", "") - + const_name(", flags.c_contiguous", "") - + const_name(", flags.f_contiguous", "") + const_name("]"); + // it can be confusing to see that a function accepts a + // 'typing.Annotated[numpy.typing.NDArray[numpy.float64], "[3,2]"]' and an error message + // that you *gave* a numpy.ndarray of the right type and dimensions. + + const_name(", \"flags.writeable\"", "") + + const_name(", \"flags.c_contiguous\"", "") + + const_name(", \"flags.f_contiguous\"", "") + const_name("]"); }; // Casts an Eigen type to numpy array. If given a base, the numpy array references the src data, @@ -315,8 +319,11 @@ struct type_caster::value>> { return false; } + PYBIND11_WARNING_PUSH + PYBIND11_WARNING_DISABLE_GCC("-Wmaybe-uninitialized") // See PR #5516 // Allocate the new type, then build a numpy reference into it value = Type(fits.rows, fits.cols); + PYBIND11_WARNING_POP auto ref = reinterpret_steal(eigen_ref_array(value)); if (dims == 1) { ref = ref.squeeze(); @@ -437,7 +444,9 @@ struct eigen_map_caster { } } - static constexpr auto name = props::descriptor; + // return_descr forces the use of NDArray instead of ArrayLike in args + // since Ref<...> args can only accept arrays. + static constexpr auto name = return_descr(props::descriptor); // Explicitly delete these: support python -> C++ conversion on these (i.e. these can be return // types but not bound arguments). We still provide them (with an explicitly delete) so that diff --git a/external_libraries/pybind11/include/pybind11/eigen/tensor.h b/external_libraries/pybind11/include/pybind11/eigen/tensor.h index 25d12bac..e5c84410 100644 --- a/external_libraries/pybind11/include/pybind11/eigen/tensor.h +++ b/external_libraries/pybind11/include/pybind11/eigen/tensor.h @@ -7,7 +7,8 @@ #pragma once -#include "../numpy.h" +#include + #include "common.h" #if defined(__GNUC__) && !defined(__clang__) && !defined(__INTEL_COMPILER) @@ -70,7 +71,7 @@ struct eigen_tensor_helper struct helper> { - static constexpr auto value = concat(const_name(((void) Is, "?"))...); + static constexpr auto value = ::pybind11::detail::concat(const_name(((void) Is, "?"))...); }; static constexpr auto dimensions_descriptor @@ -104,7 +105,8 @@ struct eigen_tensor_helper< return get_shape() == shape; } - static constexpr auto dimensions_descriptor = concat(const_name()...); + static constexpr auto dimensions_descriptor + = ::pybind11::detail::concat(const_name()...); template static Type *alloc(Args &&...args) { @@ -122,13 +124,15 @@ struct eigen_tensor_helper< template struct get_tensor_descriptor { static constexpr auto details - = const_name(", flags.writeable", "") + = const_name(", \"flags.writeable\"", "") + const_name(Type::Layout) == static_cast(Eigen::RowMajor)>( - ", flags.c_contiguous", ", flags.f_contiguous"); + ", \"flags.c_contiguous\"", ", \"flags.f_contiguous\""); static constexpr auto value - = const_name("numpy.ndarray[") + npy_format_descriptor::name - + const_name("[") + eigen_tensor_helper>::dimensions_descriptor - + const_name("]") + const_name(details, const_name("")) + const_name("]"); + = const_name("typing.Annotated[") + + io_name("numpy.typing.ArrayLike, ", "numpy.typing.NDArray[") + + npy_format_descriptor::name + io_name("", "]") + + const_name(", \"[") + eigen_tensor_helper>::dimensions_descriptor + + const_name("]\"") + const_name(details, const_name("")) + const_name("]"); }; // When EIGEN_AVOID_STL_ARRAY is defined, Eigen::DSizes does not have the begin() member @@ -468,9 +472,6 @@ struct type_caster, parent_object = reinterpret_borrow(parent); break; - case return_value_policy::take_ownership: - delete src; - // fallthrough default: // move, take_ownership don't make any sense for a ref/map: pybind11_fail("Invalid return_value_policy for Eigen Map type, must be either " @@ -503,7 +504,10 @@ struct type_caster, std::unique_ptr value; public: - static constexpr auto name = get_tensor_descriptor::value; + // return_descr forces the use of NDArray instead of ArrayLike since refs can only reference + // arrays + static constexpr auto name + = return_descr(get_tensor_descriptor::value); explicit operator MapType *() { return value.get(); } explicit operator MapType &() { return *value; } explicit operator MapType &&() && { return std::move(*value); } diff --git a/external_libraries/pybind11/include/pybind11/embed.h b/external_libraries/pybind11/include/pybind11/embed.h index caa14f4a..c05887c3 100644 --- a/external_libraries/pybind11/include/pybind11/embed.h +++ b/external_libraries/pybind11/include/pybind11/embed.h @@ -37,24 +37,32 @@ return "Hello, World!"; }); } + + The third and subsequent macro arguments are optional, and can be used to + mark the module as supporting various Python features. + + - ``mod_gil_not_used()`` + - ``multiple_interpreters::per_interpreter_gil()`` + - ``multiple_interpreters::shared_gil()`` + - ``multiple_interpreters::not_supported()`` + + .. code-block:: cpp + + PYBIND11_EMBEDDED_MODULE(example, m, py::mod_gil_not_used()) { + m.def("foo", []() { + return "Hello, Free-threaded World!"; + }); + } + \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_WARNING_PUSH +PYBIND11_WARNING_DISABLE_CLANG("-Wgnu-zero-variadic-macro-arguments") +#define PYBIND11_EMBEDDED_MODULE(name, variable, ...) \ + PYBIND11_MODULE_PYINIT(name, ##__VA_ARGS__) \ ::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_TOSTRING(name), PYBIND11_CONCAT(PyInit_, name)); \ + PYBIND11_MODULE_EXEC(name, variable) +PYBIND11_WARNING_POP PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE) PYBIND11_NAMESPACE_BEGIN(detail) @@ -103,19 +111,6 @@ inline void initialize_interpreter_pre_pyconfig(bool init_signal_handlers, bool add_program_dir_to_path) { detail::precheck_interpreter(); Py_InitializeEx(init_signal_handlers ? 1 : 0); -# if defined(WITH_THREAD) && PY_VERSION_HEX < 0x03070000 - PyEval_InitThreads(); -# endif - - // Before it was special-cased in python 3.8, passing an empty or null argv - // caused a segfault, so we have to reimplement the special case ourselves. - bool special_case = (argv == nullptr || argc <= 0); - - const char *const empty_argv[]{"\0"}; - const char *const *safe_argv = special_case ? empty_argv : argv; - if (special_case) { - argc = 1; - } auto argv_size = static_cast(argc); // SetArgv* on python 3 takes wchar_t, so we have to convert. @@ -123,7 +118,7 @@ inline void initialize_interpreter_pre_pyconfig(bool init_signal_handlers, std::vector> widened_argv_entries; widened_argv_entries.reserve(argv_size); for (size_t ii = 0; ii < argv_size; ++ii) { - widened_argv_entries.emplace_back(detail::widen_chars(safe_argv[ii])); + widened_argv_entries.emplace_back(detail::widen_chars(argv[ii])); if (!widened_argv_entries.back()) { // A null here indicates a character-encoding failure or the python // interpreter out of memory. Give up. @@ -205,6 +200,9 @@ inline void initialize_interpreter(bool init_signal_handlers = true, config.install_signal_handlers = init_signal_handlers ? 1 : 0; initialize_interpreter(&config, argc, argv, add_program_dir_to_path); #endif + + // There is exactly one interpreter alive currently. + detail::has_seen_non_main_interpreter() = false; } /** \rst @@ -243,26 +241,32 @@ inline void initialize_interpreter(bool init_signal_handlers = true, \endrst */ inline void finalize_interpreter() { - // Get the internals pointer (without creating it if it doesn't exist). It's possible for the - // internals to be created during Py_Finalize() (e.g. if a py::capsule calls `get_internals()` - // during destruction), so we get the pointer-pointer here and check it after Py_Finalize(). - detail::internals **internals_ptr_ptr = detail::get_internals_pp(); - // It could also be stashed in state_dict, so look there too: - if (object internals_obj - = get_internals_obj_from_state_dict(detail::get_python_state_dict())) { - internals_ptr_ptr = detail::get_internals_pp_from_capsule(internals_obj); + // get rid of any thread-local interpreter cache that currently exists + if (detail::has_seen_non_main_interpreter()) { + detail::get_internals_pp_manager().unref(); + detail::get_local_internals_pp_manager().unref(); + + // We know there can be no other interpreter alive now + detail::has_seen_non_main_interpreter() = false; } - // Local internals contains data managed by the current interpreter, so we must clear them to - // avoid undefined behaviors when initializing another interpreter - detail::get_local_internals().registered_types_cpp.clear(); - detail::get_local_internals().registered_exception_translators.clear(); + + // Re-fetch the internals pointer-to-pointer (but not the internals itself, which might not + // exist). It's possible for the internals to be created during Py_Finalize() (e.g. if a + // py::capsule calls `get_internals()` during destruction), so we get the pointer-pointer here + // and check it after Py_Finalize(). + detail::get_internals_pp_manager().get_pp(); + detail::get_local_internals_pp_manager().get_pp(); Py_Finalize(); - if (internals_ptr_ptr) { - delete *internals_ptr_ptr; - *internals_ptr_ptr = nullptr; - } + detail::get_internals_pp_manager().destroy(); + + // Local internals contains data managed by the current interpreter, so we must clear them to + // avoid undefined behaviors when initializing another interpreter + detail::get_local_internals_pp_manager().destroy(); + + // We know there is no interpreter alive now, so we can reset the multi-flag + detail::has_seen_non_main_interpreter() = false; } /** \rst diff --git a/external_libraries/pybind11/include/pybind11/eval.h b/external_libraries/pybind11/include/pybind11/eval.h index bd5f981f..a5887291 100644 --- a/external_libraries/pybind11/include/pybind11/eval.h +++ b/external_libraries/pybind11/include/pybind11/eval.h @@ -19,7 +19,7 @@ PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE) PYBIND11_NAMESPACE_BEGIN(detail) inline void ensure_builtins_in_globals(object &global) { -#if defined(PYPY_VERSION) || PY_VERSION_HEX < 0x03080000 +#if defined(PYPY_VERSION) // Running exec and eval adds `builtins` module under `__builtins__` key to // globals if not yet present. Python 3.8 made PyRun_String behave // similarly. Let's also do that for older versions, for consistency. This @@ -94,18 +94,18 @@ void exec(const char (&s)[N], object global = globals(), object local = object() eval(s, std::move(global), std::move(local)); } -#if defined(PYPY_VERSION) +#if defined(PYPY_VERSION) || defined(GRAALVM_PYTHON) template object eval_file(str, object, object) { - pybind11_fail("eval_file not supported in PyPy3. Use eval"); + pybind11_fail("eval_file not supported in this interpreter. Use eval"); } template object eval_file(str, object) { - pybind11_fail("eval_file not supported in PyPy3. Use eval"); + pybind11_fail("eval_file not supported in this interpreter. Use eval"); } template object eval_file(str) { - pybind11_fail("eval_file not supported in PyPy3. Use eval"); + pybind11_fail("eval_file not supported in this interpreter. Use eval"); } #else template @@ -133,7 +133,12 @@ object eval_file(str fname, object global = globals(), object local = object()) int closeFile = 1; std::string fname_str = (std::string) fname; - FILE *f = _Py_fopen_obj(fname.ptr(), "r"); + FILE *f = +# if PY_VERSION_HEX >= 0x030E0000 + Py_fopen(fname.ptr(), "r"); +# else + _Py_fopen_obj(fname.ptr(), "r"); +# endif if (!f) { PyErr_Clear(); pybind11_fail("File \"" + fname_str + "\" could not be opened!"); diff --git a/external_libraries/pybind11/include/pybind11/functional.h b/external_libraries/pybind11/include/pybind11/functional.h index 87ec4d10..8f59f5fe 100644 --- a/external_libraries/pybind11/include/pybind11/functional.h +++ b/external_libraries/pybind11/include/pybind11/functional.h @@ -15,6 +15,47 @@ PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE) PYBIND11_NAMESPACE_BEGIN(detail) +PYBIND11_NAMESPACE_BEGIN(type_caster_std_function_specializations) + +// ensure GIL is held during functor destruction +struct func_handle { + function f; +#if !(defined(_MSC_VER) && _MSC_VER == 1916 && defined(PYBIND11_CPP17)) + // This triggers a syntax error under very special conditions (very weird indeed). + explicit +#endif + func_handle(function &&f_) noexcept + : f(std::move(f_)) { + } + func_handle(const func_handle &f_) { operator=(f_); } + func_handle &operator=(const func_handle &f_) { + gil_scoped_acquire acq; + f = f_.f; + return *this; + } + ~func_handle() { + gil_scoped_acquire acq; + function kill_f(std::move(f)); + } +}; + +// to emulate 'move initialization capture' in C++11 +struct func_wrapper_base { + func_handle hfunc; + explicit func_wrapper_base(func_handle &&hf) noexcept : hfunc(hf) {} +}; + +template +struct func_wrapper : func_wrapper_base { + using func_wrapper_base::func_wrapper_base; + Return operator()(Args... args) const { // NOLINT(performance-unnecessary-value-param) + gil_scoped_acquire acq; + // casts the returned object as a rvalue to the return type + return hfunc.f(std::forward(args)...).template cast(); + } +}; + +PYBIND11_NAMESPACE_END(type_caster_std_function_specializations) template struct type_caster> { @@ -50,23 +91,22 @@ struct type_caster> { auto *cfunc_self = PyCFunction_GET_SELF(cfunc.ptr()); if (cfunc_self == nullptr) { PyErr_Clear(); - } else if (isinstance(cfunc_self)) { - auto c = reinterpret_borrow(cfunc_self); - - function_record *rec = nullptr; - // Check that we can safely reinterpret the capsule into a function_record - if (detail::is_function_record_capsule(c)) { - rec = c.get_pointer(); - } - + } else { + function_record *rec = function_record_ptr_from_PyObject(cfunc_self); while (rec != nullptr) { if (rec->is_stateless && same_type(typeid(function_type), *reinterpret_cast(rec->data[1]))) { struct capture { function_type f; + + static capture *from_data(void **data) { + return PYBIND11_STD_LAUNDER(reinterpret_cast(data)); + } }; - value = ((capture *) &rec->data)->f; + PYBIND11_ENSURE_PRECONDITION_FOR_FUNCTIONAL_H_PERFORMANCE_OPTIMIZATIONS( + std::is_standard_layout::value); + value = capture::from_data(rec->data)->f; return true; } rec = rec->next; @@ -77,40 +117,8 @@ struct type_caster> { // See PR #1413 for full details } - // ensure GIL is held during functor destruction - struct func_handle { - function f; -#if !(defined(_MSC_VER) && _MSC_VER == 1916 && defined(PYBIND11_CPP17)) - // This triggers a syntax error under very special conditions (very weird indeed). - explicit -#endif - func_handle(function &&f_) noexcept - : f(std::move(f_)) { - } - func_handle(const func_handle &f_) { operator=(f_); } - func_handle &operator=(const func_handle &f_) { - gil_scoped_acquire acq; - f = f_.f; - return *this; - } - ~func_handle() { - gil_scoped_acquire acq; - function kill_f(std::move(f)); - } - }; - - // to emulate 'move initialization capture' in C++11 - struct func_wrapper { - func_handle hfunc; - explicit func_wrapper(func_handle &&hf) noexcept : hfunc(std::move(hf)) {} - Return operator()(Args... args) const { - gil_scoped_acquire acq; - // casts the returned object as a rvalue to the return type - return hfunc.f(std::forward(args)...).template cast(); - } - }; - - value = func_wrapper(func_handle(std::move(func))); + value = type_caster_std_function_specializations::func_wrapper( + type_caster_std_function_specializations::func_handle(std::move(func))); return true; } @@ -127,10 +135,12 @@ struct type_caster> { return cpp_function(std::forward(f_), policy).release(); } - PYBIND11_TYPE_CASTER(type, - const_name("Callable[[") + concat(make_caster::name...) - + const_name("], ") + make_caster::name - + const_name("]")); + PYBIND11_TYPE_CASTER( + type, + const_name("collections.abc.Callable[[") + + ::pybind11::detail::concat(::pybind11::detail::arg_descr(make_caster::name)...) + + const_name("], ") + ::pybind11::detail::return_descr(make_caster::name) + + const_name("]")); }; PYBIND11_NAMESPACE_END(detail) diff --git a/external_libraries/pybind11/include/pybind11/gil.h b/external_libraries/pybind11/include/pybind11/gil.h index 570a5581..9e799b3c 100644 --- a/external_libraries/pybind11/include/pybind11/gil.h +++ b/external_libraries/pybind11/include/pybind11/gil.h @@ -9,24 +9,38 @@ #pragma once -#include "detail/common.h" +#if defined(PYBIND11_SIMPLE_GIL_MANAGEMENT) -#if defined(WITH_THREAD) && !defined(PYBIND11_SIMPLE_GIL_MANAGEMENT) +# include "detail/common.h" +# include "gil_simple.h" + +PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE) + +using gil_scoped_acquire = gil_scoped_acquire_simple; +using gil_scoped_release = gil_scoped_release_simple; + +PYBIND11_NAMESPACE_END(PYBIND11_NAMESPACE) + +#else + +# include "detail/common.h" # include "detail/internals.h" -#endif + +# include PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE) PYBIND11_NAMESPACE_BEGIN(detail) +PYBIND11_WARNING_PUSH +PYBIND11_WARNING_DISABLE_GCC("-Wredundant-decls") + // forward declarations PyThreadState *get_thread_state_unchecked(); -PYBIND11_NAMESPACE_END(detail) - -#if defined(WITH_THREAD) +PYBIND11_WARNING_POP -# if !defined(PYBIND11_SIMPLE_GIL_MANAGEMENT) +PYBIND11_NAMESPACE_END(detail) /* The functions below essentially reproduce the PyGILState_* API using a RAII * pattern, but there are a few important differences: @@ -54,7 +68,7 @@ class gil_scoped_acquire { public: PYBIND11_NOINLINE gil_scoped_acquire() { auto &internals = detail::get_internals(); - tstate = (PyThreadState *) PYBIND11_TLS_GET_VALUE(internals.tstate); + tstate = internals.tstate.get(); if (!tstate) { /* Check if the GIL was acquired using the PyGILState_* API instead (e.g. if @@ -67,13 +81,13 @@ class gil_scoped_acquire { if (!tstate) { tstate = PyThreadState_New(internals.istate); -# if defined(PYBIND11_DETAILED_ERROR_MESSAGES) +# if defined(PYBIND11_DETAILED_ERROR_MESSAGES) if (!tstate) { pybind11_fail("scoped_acquire: could not create thread state!"); } -# endif +# endif tstate->gilstate_counter = 0; - PYBIND11_TLS_REPLACE_VALUE(internals.tstate, tstate); + internals.tstate = tstate; } else { release = detail::get_thread_state_unchecked() != tstate; } @@ -92,31 +106,35 @@ class gil_scoped_acquire { PYBIND11_NOINLINE void dec_ref() { --tstate->gilstate_counter; -# if defined(PYBIND11_DETAILED_ERROR_MESSAGES) +# if defined(PYBIND11_DETAILED_ERROR_MESSAGES) if (detail::get_thread_state_unchecked() != tstate) { pybind11_fail("scoped_acquire::dec_ref(): thread state must be current!"); } if (tstate->gilstate_counter < 0) { pybind11_fail("scoped_acquire::dec_ref(): reference count underflow!"); } -# endif +# endif if (tstate->gilstate_counter == 0) { -# if defined(PYBIND11_DETAILED_ERROR_MESSAGES) +# if defined(PYBIND11_DETAILED_ERROR_MESSAGES) if (!release) { pybind11_fail("scoped_acquire::dec_ref(): internal error!"); } -# endif +# endif + // Make sure that PyThreadState_Clear is not recursively called by finalizers. + // See issue #5827 + ++tstate->gilstate_counter; PyThreadState_Clear(tstate); + --tstate->gilstate_counter; if (active) { PyThreadState_DeleteCurrent(); } - PYBIND11_TLS_DELETE_VALUE(detail::get_internals().tstate); + detail::get_internals().tstate.reset(); release = false; } } /// This method will disable the PyThreadState_DeleteCurrent call and the - /// GIL won't be acquired. This method should be used if the interpreter + /// GIL won't be released. This method should be used if the interpreter /// could be shutting down when this is called, as thread deletion is not /// allowed during shutdown. Check _Py_IsFinalizing() on Python 3.7+, and /// protect subsequent code. @@ -137,7 +155,9 @@ class gil_scoped_acquire { class gil_scoped_release { public: + // PRECONDITION: The GIL must be held when this constructor is called. explicit gil_scoped_release(bool disassoc = false) : disassoc(disassoc) { + assert(PyGILState_Check()); // `get_internals()` must be called here unconditionally in order to initialize // `internals.tstate` for subsequent `gil_scoped_acquire` calls. Otherwise, an // initialization race could occur as multiple threads try `gil_scoped_acquire`. @@ -145,10 +165,7 @@ class gil_scoped_release { // NOLINTNEXTLINE(cppcoreguidelines-prefer-member-initializer) tstate = PyEval_SaveThread(); if (disassoc) { - // Python >= 3.7 can remove this, it's an int before 3.7 - // NOLINTNEXTLINE(readability-qualified-auto) - auto key = internals.tstate; - PYBIND11_TLS_DELETE_VALUE(key); + internals.tstate.reset(); } } @@ -171,10 +188,7 @@ class gil_scoped_release { PyEval_RestoreThread(tstate); } if (disassoc) { - // Python >= 3.7 can remove this, it's an int before 3.7 - // NOLINTNEXTLINE(readability-qualified-auto) - auto key = detail::get_internals().tstate; - PYBIND11_TLS_REPLACE_VALUE(key, tstate); + detail::get_internals().tstate = tstate; } } @@ -184,56 +198,6 @@ class gil_scoped_release { bool active = true; }; -# else // PYBIND11_SIMPLE_GIL_MANAGEMENT - -class gil_scoped_acquire { - PyGILState_STATE state; - -public: - gil_scoped_acquire() : state{PyGILState_Ensure()} {} - gil_scoped_acquire(const gil_scoped_acquire &) = delete; - gil_scoped_acquire &operator=(const gil_scoped_acquire &) = delete; - ~gil_scoped_acquire() { PyGILState_Release(state); } - void disarm() {} -}; - -class gil_scoped_release { - PyThreadState *state; - -public: - gil_scoped_release() : state{PyEval_SaveThread()} {} - gil_scoped_release(const gil_scoped_release &) = delete; - gil_scoped_release &operator=(const gil_scoped_release &) = delete; - ~gil_scoped_release() { PyEval_RestoreThread(state); } - void disarm() {} -}; - -# endif // PYBIND11_SIMPLE_GIL_MANAGEMENT - -#else // WITH_THREAD - -class gil_scoped_acquire { -public: - gil_scoped_acquire() { - // Trick to suppress `unused variable` error messages (at call sites). - (void) (this != (this + 1)); - } - gil_scoped_acquire(const gil_scoped_acquire &) = delete; - gil_scoped_acquire &operator=(const gil_scoped_acquire &) = delete; - void disarm() {} -}; - -class gil_scoped_release { -public: - gil_scoped_release() { - // Trick to suppress `unused variable` error messages (at call sites). - (void) (this != (this + 1)); - } - gil_scoped_release(const gil_scoped_release &) = delete; - gil_scoped_release &operator=(const gil_scoped_release &) = delete; - void disarm() {} -}; - -#endif // WITH_THREAD - PYBIND11_NAMESPACE_END(PYBIND11_NAMESPACE) + +#endif // !PYBIND11_SIMPLE_GIL_MANAGEMENT diff --git a/external_libraries/pybind11/include/pybind11/gil_safe_call_once.h b/external_libraries/pybind11/include/pybind11/gil_safe_call_once.h new file mode 100644 index 00000000..770ed499 --- /dev/null +++ b/external_libraries/pybind11/include/pybind11/gil_safe_call_once.h @@ -0,0 +1,273 @@ +// Copyright (c) 2023 The pybind Community. + +#pragma once + +#include "detail/common.h" +#include "detail/internals.h" +#include "gil.h" + +#include +#include + +#if defined(Py_GIL_DISABLED) || defined(PYBIND11_HAS_SUBINTERPRETER_SUPPORT) +# include +#endif +#ifdef PYBIND11_HAS_SUBINTERPRETER_SUPPORT +# include +# include +# include +#endif + +PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE) + +PYBIND11_NAMESPACE_BEGIN(detail) +#if defined(Py_GIL_DISABLED) || defined(PYBIND11_HAS_SUBINTERPRETER_SUPPORT) +using atomic_bool = std::atomic_bool; +#else +using atomic_bool = bool; +#endif +PYBIND11_NAMESPACE_END(detail) + +// Use the `gil_safe_call_once_and_store` class below instead of the naive +// +// static auto imported_obj = py::module_::import("module_name"); // BAD, DO NOT USE! +// +// which has two serious issues: +// +// 1. Py_DECREF() calls potentially after the Python interpreter was finalized already, and +// 2. deadlocks in multi-threaded processes (because of missing lock ordering). +// +// The following alternative avoids both problems: +// +// PYBIND11_CONSTINIT static py::gil_safe_call_once_and_store storage; +// auto &imported_obj = storage // Do NOT make this `static`! +// .call_once_and_store_result([]() { +// return py::module_::import("module_name"); +// }) +// .get_stored(); +// +// The parameter of `call_once_and_store_result()` must be callable. It can make +// CPython API calls, and in particular, it can temporarily release the GIL. +// +// `T` can be any C++ type, it does not have to involve CPython API types. +// +// The behavior with regard to signals, e.g. `SIGINT` (`KeyboardInterrupt`), +// is not ideal. If the main thread is the one to actually run the `Callable`, +// then a `KeyboardInterrupt` will interrupt it if it is running normal Python +// code. The situation is different if a non-main thread runs the +// `Callable`, and then the main thread starts waiting for it to complete: +// a `KeyboardInterrupt` will not interrupt the non-main thread, but it will +// get processed only when it is the main thread's turn again and it is running +// normal Python code. However, this will be unnoticeable for quick call-once +// functions, which is usually the case. +// +// For in-depth background, see docs/advanced/deadlock.md +#ifndef PYBIND11_HAS_SUBINTERPRETER_SUPPORT +// Subinterpreter support is disabled. +// In this case, we can store the result globally, because there is only a single interpreter. +// +// The life span of the stored result is the entire process lifetime. It is leaked on process +// termination to avoid destructor calls after the Python interpreter was finalized. +template +class gil_safe_call_once_and_store { +public: + // PRECONDITION: The GIL must be held when `call_once_and_store_result()` is called. + // + // NOTE: The second parameter (finalize callback) is intentionally unused when subinterpreter + // support is disabled. In that case, storage is process-global and intentionally leaked to + // avoid calling destructors after the Python interpreter has been finalized. + template + gil_safe_call_once_and_store &call_once_and_store_result(Callable &&fn, + void (*)(T &) /*unused*/ = nullptr) { + if (!is_initialized_) { // This read is guarded by the GIL. + // Multiple threads may enter here, because the GIL is released in the next line and + // CPython API calls in the `fn()` call below may release and reacquire the GIL. + gil_scoped_release gil_rel; // Needed to establish lock ordering. + std::call_once(once_flag_, [&] { + // Only one thread will ever enter here. + gil_scoped_acquire gil_acq; + ::new (storage_) T(fn()); // fn may release, but will reacquire, the GIL. + is_initialized_ = true; // This write is guarded by the GIL. + }); + // All threads will observe `is_initialized_` as true here. + } + // Intentionally not returning `T &` to ensure the calling code is self-documenting. + return *this; + } + + // This must only be called after `call_once_and_store_result()` was called. + T &get_stored() { + assert(is_initialized_); + PYBIND11_WARNING_PUSH +# if !defined(__clang__) && defined(__GNUC__) && __GNUC__ < 5 + // Needed for gcc 4.8.5 + PYBIND11_WARNING_DISABLE_GCC("-Wstrict-aliasing") +# endif + return *reinterpret_cast(storage_); + PYBIND11_WARNING_POP + } + + constexpr gil_safe_call_once_and_store() = default; + // The instance is a global static, so its destructor runs when the process + // is terminating. Therefore, do nothing here because the Python interpreter + // may have been finalized already. + PYBIND11_DTOR_CONSTEXPR ~gil_safe_call_once_and_store() = default; + + // Disable copy and move operations. + gil_safe_call_once_and_store(const gil_safe_call_once_and_store &) = delete; + gil_safe_call_once_and_store(gil_safe_call_once_and_store &&) = delete; + gil_safe_call_once_and_store &operator=(const gil_safe_call_once_and_store &) = delete; + gil_safe_call_once_and_store &operator=(gil_safe_call_once_and_store &&) = delete; + +private: + // The global static storage (per-process) when subinterpreter support is disabled. + alignas(T) char storage_[sizeof(T)] = {}; + std::once_flag once_flag_; + + // The `is_initialized_`-`storage_` pair is very similar to `std::optional`, + // but the latter does not have the triviality properties of former, + // therefore `std::optional` is not a viable alternative here. + detail::atomic_bool is_initialized_{false}; +}; +#else +// Subinterpreter support is enabled. +// In this case, we should store the result per-interpreter instead of globally, because each +// subinterpreter has its own separate state. The cached result may not shareable across +// interpreters (e.g., imported modules and their members). + +PYBIND11_NAMESPACE_BEGIN(detail) + +template +struct call_once_storage { + alignas(T) char storage[sizeof(T)] = {}; + std::once_flag once_flag; + void (*finalize)(T &) = nullptr; + std::atomic_bool is_initialized{false}; + + call_once_storage() = default; + ~call_once_storage() { + if (is_initialized) { + if (finalize != nullptr) { + finalize(*reinterpret_cast(storage)); + } else { + reinterpret_cast(storage)->~T(); + } + } + } + call_once_storage(const call_once_storage &) = delete; + call_once_storage(call_once_storage &&) = delete; + call_once_storage &operator=(const call_once_storage &) = delete; + call_once_storage &operator=(call_once_storage &&) = delete; +}; + +PYBIND11_NAMESPACE_END(detail) + +// Prefix for storage keys in the interpreter state dict. +# define PYBIND11_CALL_ONCE_STORAGE_KEY_PREFIX PYBIND11_INTERNALS_ID "_call_once_storage__" + +// The life span of the stored result is the entire interpreter lifetime. An additional +// `finalize_fn` can be provided to clean up the stored result when the interpreter is destroyed. +template +class gil_safe_call_once_and_store { +public: + // PRECONDITION: The GIL must be held when `call_once_and_store_result()` is called. + template + gil_safe_call_once_and_store &call_once_and_store_result(Callable &&fn, + void (*finalize_fn)(T &) = nullptr) { + if (!is_last_storage_valid()) { + // Multiple threads may enter here, because the GIL is released in the next line and + // CPython API calls in the `fn()` call below may release and reacquire the GIL. + gil_scoped_release gil_rel; // Needed to establish lock ordering. + // There can be multiple threads going through here. + storage_type *value = nullptr; + { + gil_scoped_acquire gil_acq; // Restore lock ordering. + // This function is thread-safe under free-threading. + value = get_or_create_storage_in_state_dict(); + } + assert(value != nullptr); + std::call_once(value->once_flag, [&] { + // Only one thread will ever enter here. + gil_scoped_acquire gil_acq; + // fn may release, but will reacquire, the GIL. + ::new (value->storage) T(fn()); + value->finalize = finalize_fn; + value->is_initialized = true; + last_storage_ptr_ = reinterpret_cast(value->storage); + is_initialized_by_at_least_one_interpreter_ = true; + }); + // All threads will observe `is_initialized_by_at_least_one_interpreter_` as true here. + } + // Intentionally not returning `T &` to ensure the calling code is self-documenting. + return *this; + } + + // This must only be called after `call_once_and_store_result()` was called. + T &get_stored() { + T *result = last_storage_ptr_; + if (!is_last_storage_valid()) { + gil_scoped_acquire gil_acq; + auto *value = get_or_create_storage_in_state_dict(); + result = last_storage_ptr_ = reinterpret_cast(value->storage); + } + assert(result != nullptr); + return *result; + } + + constexpr gil_safe_call_once_and_store() = default; + // The instance is a global static, so its destructor runs when the process + // is terminating. Therefore, do nothing here because the Python interpreter + // may have been finalized already. + PYBIND11_DTOR_CONSTEXPR ~gil_safe_call_once_and_store() = default; + + // Disable copy and move operations because the memory address is used as key. + gil_safe_call_once_and_store(const gil_safe_call_once_and_store &) = delete; + gil_safe_call_once_and_store(gil_safe_call_once_and_store &&) = delete; + gil_safe_call_once_and_store &operator=(const gil_safe_call_once_and_store &) = delete; + gil_safe_call_once_and_store &operator=(gil_safe_call_once_and_store &&) = delete; + +private: + using storage_type = detail::call_once_storage; + + // Indicator of fast path for single-interpreter case. + bool is_last_storage_valid() const { + return is_initialized_by_at_least_one_interpreter_ + && !detail::has_seen_non_main_interpreter(); + } + + // Get the unique key for this storage instance in the interpreter's state dict. + // The return type should not be `py::str` because PyObject is interpreter-dependent. + std::string get_storage_key() const { + // The instance is expected to be global static, so using its address as unique identifier. + // The typical usage is like: + // + // PYBIND11_CONSTINIT static gil_safe_call_once_and_store storage; + // + return PYBIND11_CALL_ONCE_STORAGE_KEY_PREFIX + + std::to_string(reinterpret_cast(this)); + } + + // Get or create per-storage capsule in the current interpreter's state dict. + // The storage is interpreter-dependent and will not be shared across interpreters. + storage_type *get_or_create_storage_in_state_dict() { + return detail::atomic_get_or_create_in_state_dict(get_storage_key().c_str()) + .first; + } + + // No storage needed when subinterpreter support is enabled. + // The actual storage is stored in the per-interpreter state dict via + // `get_or_create_storage_in_state_dict()`. + + // Fast local cache to avoid repeated lookups when there are no multiple interpreters. + // This is only valid if there is a single interpreter. Otherwise, it is not used. + // WARNING: We cannot use thread local cache similar to `internals_pp_manager::internals_p_tls` + // because the thread local storage cannot be explicitly invalidated when interpreters + // are destroyed (unlike `internals_pp_manager` which has explicit hooks for that). + T *last_storage_ptr_ = nullptr; + // This flag is true if the value has been initialized by any interpreter (may not be the + // current one). + detail::atomic_bool is_initialized_by_at_least_one_interpreter_{false}; +}; +#endif + +PYBIND11_NAMESPACE_END(PYBIND11_NAMESPACE) diff --git a/external_libraries/pybind11/include/pybind11/gil_simple.h b/external_libraries/pybind11/include/pybind11/gil_simple.h new file mode 100644 index 00000000..9ff428ad --- /dev/null +++ b/external_libraries/pybind11/include/pybind11/gil_simple.h @@ -0,0 +1,37 @@ +// Copyright (c) 2016-2025 The Pybind Development Team. +// All rights reserved. Use of this source code is governed by a +// BSD-style license that can be found in the LICENSE file. + +#pragma once + +#include "detail/common.h" + +#include + +PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE) + +class gil_scoped_acquire_simple { + PyGILState_STATE state; + +public: + gil_scoped_acquire_simple() : state{PyGILState_Ensure()} {} + gil_scoped_acquire_simple(const gil_scoped_acquire_simple &) = delete; + gil_scoped_acquire_simple &operator=(const gil_scoped_acquire_simple &) = delete; + ~gil_scoped_acquire_simple() { PyGILState_Release(state); } +}; + +class gil_scoped_release_simple { + PyThreadState *state; + +public: + // PRECONDITION: The GIL must be held when this constructor is called. + gil_scoped_release_simple() { + assert(PyGILState_Check()); + state = PyEval_SaveThread(); + } + gil_scoped_release_simple(const gil_scoped_release_simple &) = delete; + gil_scoped_release_simple &operator=(const gil_scoped_release_simple &) = delete; + ~gil_scoped_release_simple() { PyEval_RestoreThread(state); } +}; + +PYBIND11_NAMESPACE_END(PYBIND11_NAMESPACE) diff --git a/external_libraries/pybind11/include/pybind11/iostream.h b/external_libraries/pybind11/include/pybind11/iostream.h index 1878089e..df7fa3c3 100644 --- a/external_libraries/pybind11/include/pybind11/iostream.h +++ b/external_libraries/pybind11/include/pybind11/iostream.h @@ -117,13 +117,36 @@ class pythonbuf : public std::streambuf { int sync() override { return _sync(); } public: + // Minimum buffer size must accommodate the largest incomplete UTF-8 sequence + // (3 bytes) plus one position reserved for overflow(), i.e. 4 bytes total. + static constexpr size_t minimum_buffer_size = 4; + explicit pythonbuf(const object &pyostream, size_t buffer_size = 1024) - : buf_size(buffer_size), d_buffer(new char[buf_size]), pywrite(pyostream.attr("write")), + : buf_size(buffer_size < minimum_buffer_size // ternary avoids C++14 std::max ODR-use of + // static constexpr + ? minimum_buffer_size + : buffer_size), + d_buffer(new char[buf_size]), pywrite(pyostream.attr("write")), pyflush(pyostream.attr("flush")) { setp(d_buffer.get(), d_buffer.get() + buf_size - 1); } - pythonbuf(pythonbuf &&) = default; + pythonbuf(pythonbuf &&other) noexcept + : buf_size(other.buf_size), d_buffer(std::move(other.d_buffer)), + pywrite(std::move(other.pywrite)), pyflush(std::move(other.pyflush)) { + const auto pending = (other.pbase() != nullptr && other.pptr() != nullptr) + ? static_cast(other.pptr() - other.pbase()) + : 0; + if (d_buffer != nullptr) { + // Rebuild the put area from the transferred storage. + setp(d_buffer.get(), d_buffer.get() + buf_size - 1); + pbump(pending); + } else { + setp(nullptr, nullptr); + } + // Prevent the moved-from destructor from flushing through moved-out handles. + other.setp(nullptr, nullptr); + } /// Sync before destroy ~pythonbuf() override { _sync(); } @@ -161,6 +184,7 @@ class scoped_ostream_redirect { std::streambuf *old; std::ostream &costream; detail::pythonbuf buffer; + bool active = true; public: explicit scoped_ostream_redirect(std::ostream &costream = std::cout, @@ -170,10 +194,22 @@ class scoped_ostream_redirect { old = costream.rdbuf(&buffer); } - ~scoped_ostream_redirect() { costream.rdbuf(old); } + ~scoped_ostream_redirect() { + if (active) { + costream.rdbuf(old); + } + } scoped_ostream_redirect(const scoped_ostream_redirect &) = delete; - scoped_ostream_redirect(scoped_ostream_redirect &&other) = default; + // NOLINTNEXTLINE(performance-noexcept-move-constructor) + scoped_ostream_redirect(scoped_ostream_redirect &&other) + : old(other.old), costream(other.costream), buffer(std::move(other.buffer)), + active(other.active) { + if (active) { + costream.rdbuf(&buffer); // Re-point stream to our buffer + other.active = false; + } + } scoped_ostream_redirect &operator=(const scoped_ostream_redirect &) = delete; scoped_ostream_redirect &operator=(scoped_ostream_redirect &&) = delete; }; diff --git a/external_libraries/pybind11/include/pybind11/native_enum.h b/external_libraries/pybind11/include/pybind11/native_enum.h new file mode 100644 index 00000000..af166d0c --- /dev/null +++ b/external_libraries/pybind11/include/pybind11/native_enum.h @@ -0,0 +1,76 @@ +// Copyright (c) 2022-2025 The pybind Community. +// All rights reserved. Use of this source code is governed by a +// BSD-style license that can be found in the LICENSE file. + +#pragma once + +#include "detail/common.h" +#include "detail/native_enum_data.h" +#include "detail/type_caster_base.h" +#include "cast.h" + +#include +#include +#include +#include + +PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE) + +/// Conversions between Python's native (stdlib) enum types and C++ enums. +template +class native_enum : public detail::native_enum_data { +public: + using Underlying = typename std::underlying_type::type; + + native_enum(handle parent_scope, + const char *name, + const char *native_type_name, + const char *class_doc = "") + : detail::native_enum_data( + parent_scope, name, native_type_name, class_doc, make_record()) { + if (detail::get_local_type_info(typeid(EnumType)) != nullptr + || detail::get_global_type_info(typeid(EnumType)) != nullptr) { + pybind11_fail( + "pybind11::native_enum<...>(\"" + enum_name_encoded + + "\") is already registered as a `pybind11::enum_` or `pybind11::class_`!"); + } + if (detail::global_internals_native_enum_type_map_contains(enum_type_index)) { + pybind11_fail("pybind11::native_enum<...>(\"" + enum_name_encoded + + "\") is already registered!"); + } + arm_finalize_check(); + } + + /// Export enumeration entries into the parent scope + native_enum &export_values() { + assert(!export_values_flag); // Catch redundant calls. + export_values_flag = true; + return *this; + } + + /// Add an enumeration entry + native_enum &value(char const *name, EnumType value, const char *doc = nullptr) { + // Disarm for the case that the native_enum_data dtor runs during exception unwinding. + disarm_finalize_check("value after finalize"); + members.append(make_tuple(name, static_cast(value))); + if (doc) { + member_docs.append(make_tuple(name, doc)); + } + arm_finalize_check(); // There was no exception. + return *this; + } + + native_enum(const native_enum &) = delete; + native_enum &operator=(const native_enum &) = delete; + +private: + static detail::native_enum_record make_record() { + detail::native_enum_record ret; + ret.cpptype = &typeid(EnumType); + ret.size_bytes = sizeof(EnumType); + ret.is_signed = std::is_signed::value; + return ret; + } +}; + +PYBIND11_NAMESPACE_END(PYBIND11_NAMESPACE) diff --git a/external_libraries/pybind11/include/pybind11/numpy.h b/external_libraries/pybind11/include/pybind11/numpy.h index 36077ec0..10c0c944 100644 --- a/external_libraries/pybind11/include/pybind11/numpy.h +++ b/external_libraries/pybind11/include/pybind11/numpy.h @@ -10,7 +10,10 @@ #pragma once #include "pybind11.h" +#include "detail/common.h" #include "complex.h" +#include "gil_safe_call_once.h" +#include "pytypes.h" #include #include @@ -26,10 +29,19 @@ #include #include +#ifdef PYBIND11_HAS_SPAN +# include +#endif + +#if defined(PYBIND11_NUMPY_1_ONLY) +# error "PYBIND11_NUMPY_1_ONLY is no longer supported (see PR #5595)." +#endif + /* This will be true on all flat address space platforms and allows us to reduce the whole npy_intp / ssize_t / Py_intptr_t business down to just ssize_t for all size and dimension types (e.g. shape, strides, indexing), instead of inflicting this - upon the library user. */ + upon the library user. + Note that NumPy 2 now uses ssize_t for `npy_intp` to simplify this. */ static_assert(sizeof(::pybind11::ssize_t) == sizeof(Py_intptr_t), "ssize_t != Py_intptr_t"); static_assert(std::is_signed::value, "Py_intptr_t must be signed"); // We now can reinterpret_cast between py::ssize_t and Py_intptr_t (MSVC + PyPy cares) @@ -38,10 +50,19 @@ PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE) PYBIND11_WARNING_DISABLE_MSVC(4127) +class dtype; // Forward declaration class array; // Forward declaration +template +struct numpy_scalar; // Forward declaration + PYBIND11_NAMESPACE_BEGIN(detail) +template <> +struct handle_type_name { + static constexpr auto name = const_name("numpy.dtype"); +}; + template <> struct handle_type_name { static constexpr auto name = const_name("numpy.ndarray"); @@ -50,7 +71,8 @@ struct handle_type_name { template struct npy_format_descriptor; -struct PyArrayDescr_Proxy { +/* NumPy 1 proxy (always includes legacy fields) */ +struct PyArrayDescr1_Proxy { PyObject_HEAD PyObject *typeobj; char kind; @@ -65,6 +87,38 @@ struct PyArrayDescr_Proxy { PyObject *names; }; +struct PyArrayDescr_Proxy { + PyObject_HEAD + PyObject *typeobj; + char kind; + char type; + char byteorder; + char _former_flags; + int type_num; + /* Additional fields are NumPy version specific. */ +}; + +/* NumPy 2 proxy, including legacy fields */ +struct PyArrayDescr2_Proxy { + PyObject_HEAD + PyObject *typeobj; + char kind; + char type; + char byteorder; + char _former_flags; + int type_num; + std::uint64_t flags; + ssize_t elsize; + ssize_t alignment; + PyObject *metadata; + Py_hash_t hash; + void *reserved_null[2]; + /* The following fields only exist if 0 <= type_num < 2056 */ + char *subarray; + PyObject *fields; + PyObject *names; +}; + struct PyArray_Proxy { PyObject_HEAD char *data; @@ -120,6 +174,19 @@ inline numpy_internals &get_numpy_internals() { return *ptr; } +PYBIND11_NOINLINE module_ import_numpy_core_submodule(const char *submodule_name) { + module_ numpy = module_::import("numpy"); + str version_string = numpy.attr("__version__"); + module_ numpy_lib = module_::import("numpy.lib"); + object numpy_version = numpy_lib.attr("NumpyVersion")(version_string); + int major_version = numpy_version.attr("major").cast(); + + /* `numpy.core` was renamed to `numpy._core` in NumPy 2.0 as it officially + became a private module. */ + std::string numpy_core_path = major_version >= 2 ? "numpy._core" : "numpy.core"; + return module_::import((numpy_core_path + "." + submodule_name).c_str()); +} + template struct same_size { template @@ -138,6 +205,7 @@ constexpr int platform_lookup(int I, Ints... Is) { } struct npy_api { + // If you change this code, please review `normalized_dtype_num` below. enum constants { NPY_ARRAY_C_CONTIGUOUS_ = 0x0001, NPY_ARRAY_F_CONTIGUOUS_ = 0x0002, @@ -184,16 +252,33 @@ struct npy_api { NPY_UINT64_ = platform_lookup( NPY_ULONG_, NPY_ULONGLONG_, NPY_UINT_), + NPY_FLOAT32_ = platform_lookup( + NPY_DOUBLE_, NPY_FLOAT_, NPY_LONGDOUBLE_), + NPY_FLOAT64_ = platform_lookup( + NPY_DOUBLE_, NPY_FLOAT_, NPY_LONGDOUBLE_), + NPY_COMPLEX64_ + = platform_lookup, + std::complex, + std::complex, + std::complex>(NPY_DOUBLE_, NPY_FLOAT_, NPY_LONGDOUBLE_), + NPY_COMPLEX128_ + = platform_lookup, + std::complex, + std::complex, + std::complex>(NPY_DOUBLE_, NPY_FLOAT_, NPY_LONGDOUBLE_), + NPY_CHAR_ = std::is_signed::value ? NPY_BYTE_ : NPY_UBYTE_, }; + unsigned int PyArray_RUNTIME_VERSION_; + struct PyArray_Dims { Py_intptr_t *ptr; int len; }; static npy_api &get() { - static npy_api api = lookup(); - return api; + PYBIND11_CONSTINIT static gil_safe_call_once_and_store storage; + return storage.call_once_and_store_result(lookup).get_stored(); } bool PyArray_Check_(PyObject *obj) const { @@ -205,6 +290,7 @@ struct npy_api { unsigned int (*PyArray_GetNDArrayCFeatureVersion_)(); PyObject *(*PyArray_DescrFromType_)(int); + PyObject *(*PyArray_TypeObjectFromType_)(int); PyObject *(*PyArray_NewFromDescr_)(PyTypeObject *, PyObject *, int, @@ -221,17 +307,11 @@ struct npy_api { PyTypeObject *PyVoidArrType_Type_; PyTypeObject *PyArrayDescr_Type_; PyObject *(*PyArray_DescrFromScalar_)(PyObject *); + PyObject *(*PyArray_Scalar_)(void *, PyObject *, PyObject *); + void (*PyArray_ScalarAsCtype_)(PyObject *, void *); PyObject *(*PyArray_FromAny_)(PyObject *, PyObject *, int, int, int, PyObject *); int (*PyArray_DescrConverter_)(PyObject *, PyObject **); bool (*PyArray_EquivTypes_)(PyObject *, PyObject *); - int (*PyArray_GetArrayParamsFromObject_)(PyObject *, - PyObject *, - unsigned char, - PyObject **, - int *, - Py_intptr_t *, - PyObject **, - PyObject *); PyObject *(*PyArray_Squeeze_)(PyObject *); // Unused. Not removed because that affects ABI of the class. int (*PyArray_SetBaseObject_)(PyObject *, PyObject *); @@ -246,10 +326,14 @@ struct npy_api { API_PyArrayDescr_Type = 3, API_PyVoidArrType_Type = 39, API_PyArray_DescrFromType = 45, + API_PyArray_TypeObjectFromType = 46, API_PyArray_DescrFromScalar = 57, + API_PyArray_Scalar = 60, + API_PyArray_ScalarAsCtype = 62, API_PyArray_FromAny = 69, API_PyArray_Resize = 80, - API_PyArray_CopyInto = 82, + // CopyInto was slot 82 and 50 was effectively an alias. NumPy 2 removed 82. + API_PyArray_CopyInto = 50, API_PyArray_NewCopy = 85, API_PyArray_NewFromDescr = 94, API_PyArray_DescrNewFromType = 96, @@ -258,25 +342,32 @@ struct npy_api { API_PyArray_View = 137, API_PyArray_DescrConverter = 174, API_PyArray_EquivTypes = 182, - API_PyArray_GetArrayParamsFromObject = 278, API_PyArray_SetBaseObject = 282 }; static npy_api lookup() { - module_ m = module_::import("numpy.core.multiarray"); + module_ m = detail::import_numpy_core_submodule("multiarray"); auto c = m.attr("_ARRAY_API"); void **api_ptr = (void **) PyCapsule_GetPointer(c.ptr(), nullptr); + if (api_ptr == nullptr) { + raise_from(PyExc_SystemError, "FAILURE obtaining numpy _ARRAY_API pointer."); + throw error_already_set(); + } npy_api api; #define DECL_NPY_API(Func) api.Func##_ = (decltype(api.Func##_)) api_ptr[API_##Func]; DECL_NPY_API(PyArray_GetNDArrayCFeatureVersion); - if (api.PyArray_GetNDArrayCFeatureVersion_() < 0x7) { + api.PyArray_RUNTIME_VERSION_ = api.PyArray_GetNDArrayCFeatureVersion_(); + if (api.PyArray_RUNTIME_VERSION_ < 0x7) { pybind11_fail("pybind11 numpy support requires numpy >= 1.7.0"); } DECL_NPY_API(PyArray_Type); DECL_NPY_API(PyVoidArrType_Type); DECL_NPY_API(PyArrayDescr_Type); DECL_NPY_API(PyArray_DescrFromType); + DECL_NPY_API(PyArray_TypeObjectFromType); DECL_NPY_API(PyArray_DescrFromScalar); + DECL_NPY_API(PyArray_Scalar); + DECL_NPY_API(PyArray_ScalarAsCtype); DECL_NPY_API(PyArray_FromAny); DECL_NPY_API(PyArray_Resize); DECL_NPY_API(PyArray_CopyInto); @@ -288,7 +379,6 @@ struct npy_api { DECL_NPY_API(PyArray_View); DECL_NPY_API(PyArray_DescrConverter); DECL_NPY_API(PyArray_EquivTypes); - DECL_NPY_API(PyArray_GetArrayParamsFromObject); DECL_NPY_API(PyArray_SetBaseObject); #undef DECL_NPY_API @@ -296,6 +386,151 @@ struct npy_api { } }; +template +struct is_complex : std::false_type {}; +template +struct is_complex> : std::true_type {}; + +template +struct npy_format_descriptor_name; + +template +struct npy_format_descriptor_name::value>> { + static constexpr auto name = const_name::value>( + const_name("numpy.bool"), + const_name::value>("numpy.int", "numpy.uint") + + const_name()); +}; + +template +struct npy_format_descriptor_name::value>> { + static constexpr auto name = const_name < std::is_same::value + || std::is_same::value + || std::is_same::value + || std::is_same::value + > (const_name("numpy.float") + const_name(), + const_name("numpy.longdouble")); +}; + +template +struct npy_format_descriptor_name::value>> { + static constexpr auto name = const_name < std::is_same::value + || std::is_same::value + || std::is_same::value + || std::is_same::value + > (const_name("numpy.complex") + + const_name(), + const_name("numpy.clongdouble")); +}; + +template +struct numpy_scalar_info {}; + +#define PYBIND11_NUMPY_SCALAR_IMPL(ctype_, typenum_) \ + template <> \ + struct numpy_scalar_info { \ + static constexpr auto name = npy_format_descriptor_name::name; \ + static constexpr int typenum = npy_api::typenum_##_; \ + } + +// boolean type +PYBIND11_NUMPY_SCALAR_IMPL(bool, NPY_BOOL); + +// character types +PYBIND11_NUMPY_SCALAR_IMPL(char, NPY_CHAR); +PYBIND11_NUMPY_SCALAR_IMPL(signed char, NPY_BYTE); +PYBIND11_NUMPY_SCALAR_IMPL(unsigned char, NPY_UBYTE); + +// signed integer types +PYBIND11_NUMPY_SCALAR_IMPL(std::int16_t, NPY_INT16); +PYBIND11_NUMPY_SCALAR_IMPL(std::int32_t, NPY_INT32); +PYBIND11_NUMPY_SCALAR_IMPL(std::int64_t, NPY_INT64); + +// unsigned integer types +PYBIND11_NUMPY_SCALAR_IMPL(std::uint16_t, NPY_UINT16); +PYBIND11_NUMPY_SCALAR_IMPL(std::uint32_t, NPY_UINT32); +PYBIND11_NUMPY_SCALAR_IMPL(std::uint64_t, NPY_UINT64); + +// floating point types +PYBIND11_NUMPY_SCALAR_IMPL(float, NPY_FLOAT); +PYBIND11_NUMPY_SCALAR_IMPL(double, NPY_DOUBLE); +PYBIND11_NUMPY_SCALAR_IMPL(long double, NPY_LONGDOUBLE); + +// complex types +PYBIND11_NUMPY_SCALAR_IMPL(std::complex, NPY_CFLOAT); +PYBIND11_NUMPY_SCALAR_IMPL(std::complex, NPY_CDOUBLE); +PYBIND11_NUMPY_SCALAR_IMPL(std::complex, NPY_CLONGDOUBLE); + +#undef PYBIND11_NUMPY_SCALAR_IMPL + +// This table normalizes typenums by mapping NPY_INT_, NPY_LONG, ... to NPY_INT32_, NPY_INT64, ... +// This is needed to correctly handle situations where multiple typenums map to the same type, +// e.g. NPY_LONG_ may be equivalent to NPY_INT_ or NPY_LONGLONG_ despite having a different +// typenum. The normalized typenum should always match the values used in npy_format_descriptor. +// If you change this code, please review `enum constants` above. +static constexpr int normalized_dtype_num[npy_api::NPY_VOID_ + 1] = { + // NPY_BOOL_ => + npy_api::NPY_BOOL_, + // NPY_BYTE_ => + npy_api::NPY_BYTE_, + // NPY_UBYTE_ => + npy_api::NPY_UBYTE_, + // NPY_SHORT_ => + npy_api::NPY_INT16_, + // NPY_USHORT_ => + npy_api::NPY_UINT16_, + // NPY_INT_ => + sizeof(int) == sizeof(std::int16_t) ? npy_api::NPY_INT16_ + : sizeof(int) == sizeof(std::int32_t) ? npy_api::NPY_INT32_ + : sizeof(int) == sizeof(std::int64_t) ? npy_api::NPY_INT64_ + : npy_api::NPY_INT_, + // NPY_UINT_ => + sizeof(unsigned int) == sizeof(std::uint16_t) ? npy_api::NPY_UINT16_ + : sizeof(unsigned int) == sizeof(std::uint32_t) ? npy_api::NPY_UINT32_ + : sizeof(unsigned int) == sizeof(std::uint64_t) ? npy_api::NPY_UINT64_ + : npy_api::NPY_UINT_, + // NPY_LONG_ => + sizeof(long) == sizeof(std::int16_t) ? npy_api::NPY_INT16_ + : sizeof(long) == sizeof(std::int32_t) ? npy_api::NPY_INT32_ + : sizeof(long) == sizeof(std::int64_t) ? npy_api::NPY_INT64_ + : npy_api::NPY_LONG_, + // NPY_ULONG_ => + sizeof(unsigned long) == sizeof(std::uint16_t) ? npy_api::NPY_UINT16_ + : sizeof(unsigned long) == sizeof(std::uint32_t) ? npy_api::NPY_UINT32_ + : sizeof(unsigned long) == sizeof(std::uint64_t) ? npy_api::NPY_UINT64_ + : npy_api::NPY_ULONG_, + // NPY_LONGLONG_ => + sizeof(long long) == sizeof(std::int16_t) ? npy_api::NPY_INT16_ + : sizeof(long long) == sizeof(std::int32_t) ? npy_api::NPY_INT32_ + : sizeof(long long) == sizeof(std::int64_t) ? npy_api::NPY_INT64_ + : npy_api::NPY_LONGLONG_, + // NPY_ULONGLONG_ => + sizeof(unsigned long long) == sizeof(std::uint16_t) ? npy_api::NPY_UINT16_ + : sizeof(unsigned long long) == sizeof(std::uint32_t) ? npy_api::NPY_UINT32_ + : sizeof(unsigned long long) == sizeof(std::uint64_t) ? npy_api::NPY_UINT64_ + : npy_api::NPY_ULONGLONG_, + // NPY_FLOAT_ => + npy_api::NPY_FLOAT_, + // NPY_DOUBLE_ => + npy_api::NPY_DOUBLE_, + // NPY_LONGDOUBLE_ => + npy_api::NPY_LONGDOUBLE_, + // NPY_CFLOAT_ => + npy_api::NPY_CFLOAT_, + // NPY_CDOUBLE_ => + npy_api::NPY_CDOUBLE_, + // NPY_CLONGDOUBLE_ => + npy_api::NPY_CLONGDOUBLE_, + // NPY_OBJECT_ => + npy_api::NPY_OBJECT_, + // NPY_STRING_ => + npy_api::NPY_STRING_, + // NPY_UNICODE_ => + npy_api::NPY_UNICODE_, + // NPY_VOID_ => + npy_api::NPY_VOID_, +}; + inline PyArray_Proxy *array_proxy(void *ptr) { return reinterpret_cast(ptr); } inline const PyArray_Proxy *array_proxy(const void *ptr) { @@ -310,6 +545,14 @@ inline const PyArrayDescr_Proxy *array_descriptor_proxy(const PyObject *ptr) { return reinterpret_cast(ptr); } +inline const PyArrayDescr1_Proxy *array_descriptor1_proxy(const PyObject *ptr) { + return reinterpret_cast(ptr); +} + +inline const PyArrayDescr2_Proxy *array_descriptor2_proxy(const PyObject *ptr) { + return reinterpret_cast(ptr); +} + inline bool check_flags(const void *ptr, int flag) { return (flag == (array_proxy(ptr)->flags & flag)); } @@ -318,10 +561,6 @@ template struct is_std_array : std::false_type {}; template struct is_std_array> : std::true_type {}; -template -struct is_complex : std::false_type {}; -template -struct is_complex> : std::true_type {}; template struct array_info_scalar { @@ -350,7 +589,7 @@ struct array_info> { } static constexpr auto extents = const_name::is_array>( - concat(const_name(), array_info::extents), const_name()); + ::pybind11::detail::concat(const_name(), array_info::extents), const_name()); }; // For numpy we have special handling for arrays of characters, so we don't include // the size in the array extents. @@ -535,8 +774,65 @@ template struct type_caster> : type_caster> {}; +template +struct type_caster> { + using value_type = T; + using type_info = numpy_scalar_info; + + PYBIND11_TYPE_CASTER(numpy_scalar, type_info::name); + + static handle &target_type() { + static handle tp = npy_api::get().PyArray_TypeObjectFromType_(type_info::typenum); + return tp; + } + + static handle &target_dtype() { + static handle tp = npy_api::get().PyArray_DescrFromType_(type_info::typenum); + return tp; + } + + bool load(handle src, bool) { + if (isinstance(src, target_type())) { + npy_api::get().PyArray_ScalarAsCtype_(src.ptr(), &value.value); + return true; + } + return false; + } + + static handle cast(numpy_scalar src, return_value_policy, handle) { + return npy_api::get().PyArray_Scalar_(&src.value, target_dtype().ptr(), nullptr); + } +}; + PYBIND11_NAMESPACE_END(detail) +template +struct numpy_scalar { + using value_type = T; + + value_type value; + + numpy_scalar() = default; + explicit numpy_scalar(value_type value) : value(value) {} + + explicit operator value_type() const { return value; } + numpy_scalar &operator=(value_type value) { + this->value = value; + return *this; + } + + friend bool operator==(const numpy_scalar &a, const numpy_scalar &b) { + return a.value == b.value; + } + + friend bool operator!=(const numpy_scalar &a, const numpy_scalar &b) { return !(a == b); } +}; + +template +numpy_scalar make_scalar(T value) { + return numpy_scalar(value); +} + class dtype : public object { public: PYBIND11_OBJECT_DEFAULT(dtype, object, detail::npy_api::get().PyArrayDescr_Check_) @@ -588,11 +884,32 @@ class dtype : public object { return detail::npy_format_descriptor::type>::dtype(); } + /// Return the type number associated with a C++ type. + /// This is the constexpr equivalent of `dtype::of().num()`. + template + static constexpr int num_of() { + return detail::npy_format_descriptor::type>::value; + } + /// Size of the data type in bytes. - ssize_t itemsize() const { return detail::array_descriptor_proxy(m_ptr)->elsize; } + ssize_t itemsize() const { + if (detail::npy_api::get().PyArray_RUNTIME_VERSION_ < 0x12) { + return detail::array_descriptor1_proxy(m_ptr)->elsize; + } + return detail::array_descriptor2_proxy(m_ptr)->elsize; + } /// Returns true for structured data types. - bool has_fields() const { return detail::array_descriptor_proxy(m_ptr)->names != nullptr; } + bool has_fields() const { + if (detail::npy_api::get().PyArray_RUNTIME_VERSION_ < 0x12) { + return detail::array_descriptor1_proxy(m_ptr)->names != nullptr; + } + const auto *proxy = detail::array_descriptor2_proxy(m_ptr); + if (proxy->type_num < 0 || proxy->type_num >= 2056) { + return false; + } + return proxy->names != nullptr; + } /// Single-character code for dtype's kind. /// For example, floating point types are 'f' and integral types are 'i'. @@ -607,7 +924,9 @@ class dtype : public object { return detail::array_descriptor_proxy(m_ptr)->type; } - /// type number of dtype. + /// Type number of dtype. Note that different values may be returned for equivalent types, + /// e.g. even though ``long`` may be equivalent to ``int`` or ``long long``, they still have + /// different type numbers. Consider using `normalized_num` to avoid this. int num() const { // Note: The signature, `dtype::num` follows the naming of NumPy's public // Python API (i.e., ``dtype.num``), rather than its internal @@ -615,23 +934,45 @@ class dtype : public object { return detail::array_descriptor_proxy(m_ptr)->type_num; } + /// Type number of dtype, normalized to match the return value of `num_of` for equivalent + /// types. This function can be used to write switch statements that correctly handle + /// equivalent types with different type numbers. + int normalized_num() const { + int value = num(); + if (value >= 0 && value <= detail::npy_api::NPY_VOID_) { + return detail::normalized_dtype_num[value]; + } + return value; + } + /// Single character for byteorder char byteorder() const { return detail::array_descriptor_proxy(m_ptr)->byteorder; } /// Alignment of the data type - int alignment() const { return detail::array_descriptor_proxy(m_ptr)->alignment; } + ssize_t alignment() const { + if (detail::npy_api::get().PyArray_RUNTIME_VERSION_ < 0x12) { + return detail::array_descriptor1_proxy(m_ptr)->alignment; + } + return detail::array_descriptor2_proxy(m_ptr)->alignment; + } /// Flags for the array descriptor - char flags() const { return detail::array_descriptor_proxy(m_ptr)->flags; } + std::uint64_t flags() const { + if (detail::npy_api::get().PyArray_RUNTIME_VERSION_ < 0x12) { + return (unsigned char) detail::array_descriptor1_proxy(m_ptr)->flags; + } + return detail::array_descriptor2_proxy(m_ptr)->flags; + } private: - static object _dtype_from_pep3118() { - static PyObject *obj = module_::import("numpy.core._internal") - .attr("_dtype_from_pep3118") - .cast() - .release() - .ptr(); - return reinterpret_borrow(obj); + static object &_dtype_from_pep3118() { + PYBIND11_CONSTINIT static gil_safe_call_once_and_store storage; + return storage + .call_once_and_store_result([]() { + return detail::import_numpy_core_submodule("_internal") + .attr("_dtype_from_pep3118"); + }) + .get_stored(); } dtype strip_padding(ssize_t itemsize) { @@ -764,7 +1105,11 @@ class array : public buffer { template array(ShapeContainer shape, StridesContainer strides, const T *ptr, handle base = handle()) - : array(pybind11::dtype::of(), std::move(shape), std::move(strides), ptr, base) {} + : array(pybind11::dtype::of(), + std::move(shape), + std::move(strides), + reinterpret_cast(ptr), + base) {} template array(ShapeContainer shape, const T *ptr, handle base = handle()) @@ -788,9 +1133,7 @@ class array : public buffer { } /// Byte size of a single element - ssize_t itemsize() const { - return detail::array_descriptor_proxy(detail::array_proxy(m_ptr)->descr)->elsize; - } + ssize_t itemsize() const { return dtype().itemsize(); } /// Total number of bytes ssize_t nbytes() const { return size() * itemsize(); } @@ -804,6 +1147,13 @@ class array : public buffer { /// Dimensions of the array const ssize_t *shape() const { return detail::array_proxy(m_ptr)->dimensions; } +#ifdef PYBIND11_HAS_SPAN + /// Dimensions of the array as a span + std::span shape_span() const { + return std::span(shape(), static_cast(ndim())); + } +#endif + /// Dimension along a given axis ssize_t shape(ssize_t dim) const { if (dim >= ndim()) { @@ -815,6 +1165,13 @@ class array : public buffer { /// Strides of the array const ssize_t *strides() const { return detail::array_proxy(m_ptr)->strides; } +#ifdef PYBIND11_HAS_SPAN + /// Strides of the array as a span + std::span strides_span() const { + return std::span(strides(), static_cast(ndim())); + } +#endif + /// Stride along a given axis ssize_t strides(ssize_t dim) const { if (dim >= ndim()) { @@ -1008,7 +1365,7 @@ class array : public buffer { /// Create array from any object -- always returns a new reference static PyObject *raw_array(PyObject *ptr, int ExtraFlags = 0) { if (ptr == nullptr) { - PyErr_SetString(PyExc_ValueError, "cannot create a pybind11::array from a nullptr"); + set_error(PyExc_ValueError, "cannot create a pybind11::array from a nullptr"); return nullptr; } return detail::npy_api::get().PyArray_FromAny_( @@ -1155,7 +1512,7 @@ class array_t : public array { /// Create array from any object -- always returns a new reference static PyObject *raw_array_t(PyObject *ptr) { if (ptr == nullptr) { - PyErr_SetString(PyExc_ValueError, "cannot create a pybind11::array_t from a nullptr"); + set_error(PyExc_ValueError, "cannot create a pybind11::array_t from a nullptr"); return nullptr; } return detail::npy_api::get().PyArray_FromAny_(ptr, @@ -1227,38 +1584,6 @@ struct compare_buffer_info::valu } }; -template -struct npy_format_descriptor_name; - -template -struct npy_format_descriptor_name::value>> { - static constexpr auto name = const_name::value>( - const_name("bool"), - const_name::value>("numpy.int", "numpy.uint") - + const_name()); -}; - -template -struct npy_format_descriptor_name::value>> { - static constexpr auto name = const_name < std::is_same::value - || std::is_same::value - || std::is_same::value - || std::is_same::value - > (const_name("numpy.float") + const_name(), - const_name("numpy.longdouble")); -}; - -template -struct npy_format_descriptor_name::value>> { - static constexpr auto name = const_name < std::is_same::value - || std::is_same::value - || std::is_same::value - || std::is_same::value - > (const_name("numpy.complex") - + const_name(), - const_name("numpy.longcomplex")); -}; - template struct npy_format_descriptor< T, @@ -1289,8 +1614,12 @@ struct npy_format_descriptor< }; template -struct npy_format_descriptor::value>> { - static constexpr auto name = const_name("object"); +struct npy_format_descriptor< + T, + enable_if_t::value + || ((std::is_same::value || std::is_same::value) + && sizeof(T) == sizeof(PyObject *))>> { + static constexpr auto name = const_name("numpy.object_"); static constexpr int value = npy_api::NPY_OBJECT_; @@ -1418,7 +1747,9 @@ PYBIND11_NOINLINE void register_structured_dtype(any_container auto tindex = std::type_index(tinfo); numpy_internals.registered_dtypes[tindex] = {dtype_ptr, std::move(format_str)}; - get_internals().direct_conversions[tindex].push_back(direct_converter); + with_internals([tindex, &direct_converter](internals &internals) { + internals.direct_conversions[tindex].push_back(direct_converter); + }); } template @@ -1547,7 +1878,7 @@ class common_iterator { using value_type = container_type::value_type; using size_type = container_type::size_type; - common_iterator() : m_strides() {} + common_iterator() = default; common_iterator(void *ptr, const container_type &strides, const container_type &shape) : p_ptr(reinterpret_cast(ptr)), m_strides(strides.size()) { @@ -1849,7 +2180,7 @@ struct vectorize_helper { // Pointers to values the function was called with; the vectorized ones set here will start // out as array_t pointers, but they will be changed them to T pointers before we make // call the wrapped function. Non-vectorized pointers are left as-is. - std::array params{{&args...}}; + std::array params{{reinterpret_cast(&args)...}}; // The array of `buffer_info`s of vectorized arguments: std::array buffers{ @@ -1949,7 +2280,8 @@ vectorize_helper vectorize_extractor(const Func &f, Retur template struct handle_type_name> { static constexpr auto name - = const_name("numpy.ndarray[") + npy_format_descriptor::name + const_name("]"); + = io_name("typing.Annotated[numpy.typing.ArrayLike, ", "numpy.typing.NDArray[") + + npy_format_descriptor::name + const_name("]"); }; PYBIND11_NAMESPACE_END(detail) @@ -1995,4 +2327,86 @@ Helper vectorize(Return (Class::*f)(Args...) const) { return Helper(std::mem_fn(f)); } +// Intentionally no &&/const&& overloads: vectorized method calls operate on the bound Python +// instance and should not consume/move-from self. +// Vectorize a class method (non-const, lvalue ref-qualified): +template ())), + Return, + Class *, + Args...>> +Helper vectorize(Return (Class::*f)(Args...) &) { + return Helper(std::mem_fn(f)); +} + +// Vectorize a class method (const, lvalue ref-qualified): +template ())), + Return, + const Class *, + Args...>> +Helper vectorize(Return (Class::*f)(Args...) const &) { + return Helper(std::mem_fn(f)); +} + +#ifdef __cpp_noexcept_function_type +// Vectorize a class method (non-const, noexcept): +template ())), + Return, + Class *, + Args...>> +Helper vectorize(Return (Class::*f)(Args...) noexcept) { + return Helper(std::mem_fn(f)); +} + +// Vectorize a class method (const, noexcept): +template ())), + Return, + const Class *, + Args...>> +Helper vectorize(Return (Class::*f)(Args...) const noexcept) { + return Helper(std::mem_fn(f)); +} + +// Vectorize a class method (non-const, lvalue ref-qualified, noexcept): +template ())), + Return, + Class *, + Args...>> +Helper vectorize(Return (Class::*f)(Args...) & noexcept) { + return Helper(std::mem_fn(f)); +} + +// Vectorize a class method (const, lvalue ref-qualified, noexcept): +template ())), + Return, + const Class *, + Args...>> +Helper vectorize(Return (Class::*f)(Args...) const & noexcept) { + return Helper(std::mem_fn(f)); +} +#endif + PYBIND11_NAMESPACE_END(PYBIND11_NAMESPACE) diff --git a/external_libraries/pybind11/include/pybind11/pybind11.h b/external_libraries/pybind11/include/pybind11/pybind11.h index 3bce1a01..b5a97db8 100644 --- a/external_libraries/pybind11/include/pybind11/pybind11.h +++ b/external_libraries/pybind11/include/pybind11/pybind11.h @@ -9,32 +9,46 @@ */ #pragma once - #include "detail/class.h" +#include "detail/dynamic_raw_ptr_cast_if_possible.h" +#include "detail/exception_translation.h" +#include "detail/function_record_pyobject.h" #include "detail/init.h" +#include "detail/native_enum_data.h" +#include "detail/using_smart_holder.h" #include "attr.h" #include "gil.h" +#include "gil_safe_call_once.h" #include "options.h" +#include "trampoline_self_life_support.h" +#include "typing.h" +#include #include #include #include #include +#include #include #include #include -#if defined(__cpp_lib_launder) && !(defined(_MSC_VER) && (_MSC_VER < 1914)) -# define PYBIND11_STD_LAUNDER std::launder -# define PYBIND11_HAS_STD_LAUNDER 1 -#else -# define PYBIND11_STD_LAUNDER -# define PYBIND11_HAS_STD_LAUNDER 0 +// See PR #5448. This warning suppression is needed for the PYBIND11_OVERRIDE macro family. +// NOTE that this is NOT embedded in a push/pop pair because that is very difficult to achieve. +#if defined(__clang_major__) && __clang_major__ < 14 +PYBIND11_WARNING_DISABLE_CLANG("-Wgnu-zero-variadic-macro-arguments") #endif + #if defined(__GNUG__) && !defined(__clang__) # include #endif +#if defined(__cpp_if_constexpr) && __cpp_if_constexpr >= 201606 +# define PYBIND11_MAYBE_CONSTEXPR constexpr +#else +# define PYBIND11_MAYBE_CONSTEXPR +#endif + PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE) /* https://stackoverflow.com/questions/46798456/handling-gccs-noexcept-type-warning @@ -52,22 +66,187 @@ PYBIND11_WARNING_DISABLE_MSVC(4127) PYBIND11_NAMESPACE_BEGIN(detail) -// Apply all the extensions translators from a list -// Return true if one of the translators completed without raising an exception -// itself. Return of false indicates that if there are other translators -// available, they should be tried. -inline bool apply_exception_translators(std::forward_list &translators) { - auto last_exception = std::current_exception(); +inline std::string replace_newlines_and_squash(const char *text) { + const char *whitespaces = " \t\n\r\f\v"; + std::string result(text); + bool previous_is_whitespace = false; - for (auto &translator : translators) { - try { - translator(last_exception); - return true; - } catch (...) { - last_exception = std::current_exception(); + if (result.size() >= 2) { + // Do not modify string representations + char first_char = result[0]; + char last_char = result[result.size() - 1]; + if (first_char == last_char && first_char == '\'') { + return result; } } - return false; + result.clear(); + + // Replace characters in whitespaces array with spaces and squash consecutive spaces + while (*text != '\0') { + if (std::strchr(whitespaces, *text)) { + if (!previous_is_whitespace) { + result += ' '; + previous_is_whitespace = true; + } + } else { + result += *text; + previous_is_whitespace = false; + } + ++text; + } + + // Strip leading and trailing whitespaces + const size_t str_begin = result.find_first_not_of(whitespaces); + if (str_begin == std::string::npos) { + return ""; + } + + const size_t str_end = result.find_last_not_of(whitespaces); + const size_t str_range = str_end - str_begin + 1; + + return result.substr(str_begin, str_range); +} + +/* Generate a proper function signature */ +inline std::string generate_function_signature(const char *type_caster_name_field, + detail::function_record *func_rec, + const std::type_info *const *types, + size_t &type_index, + size_t &arg_index) { + std::string signature; + bool is_starred = false; + // `is_return_value.top()` is true if we are currently inside the return type of the + // signature. Using `@^`/`@$` we can force types to be arg/return types while `@!` pops + // back to the previous state. + std::stack is_return_value({false}); + // The following characters have special meaning in the signature parsing. Literals + // containing these are escaped with `!`. + std::string special_chars("!@%{}-"); + for (const auto *pc = type_caster_name_field; *pc != '\0'; ++pc) { + const auto c = *pc; + if (c == '{') { + // Write arg name for everything except *args and **kwargs. + // Detect {@*args...} or {@**kwargs...} + is_starred = *(pc + 1) == '@' && *(pc + 2) == '*'; + if (is_starred) { + continue; + } + // Separator for keyword-only arguments, placed before the kw + // arguments start (unless we are already putting an *args) + if (!func_rec->has_args && arg_index == func_rec->nargs_pos) { + signature += "*, "; + } + if (arg_index < func_rec->args.size() && func_rec->args[arg_index].name) { + signature += func_rec->args[arg_index].name; + } else if (arg_index == 0 && func_rec->is_method) { + signature += "self"; + } else { + signature += "arg" + std::to_string(arg_index - (func_rec->is_method ? 1 : 0)); + } + signature += ": "; + } else if (c == '}') { + // Write default value if available. + if (!is_starred && arg_index < func_rec->args.size() + && func_rec->args[arg_index].descr) { + signature += " = "; + signature += detail::replace_newlines_and_squash(func_rec->args[arg_index].descr); + } + // Separator for positional-only arguments (placed after the + // argument, rather than before like * + if (func_rec->nargs_pos_only > 0 && (arg_index + 1) == func_rec->nargs_pos_only) { + signature += ", /"; + } + if (!is_starred) { + arg_index++; + } + } else if (c == '%') { + const std::type_info *t = types[type_index++]; + if (!t) { + pybind11_fail("Internal error while parsing type signature (1)"); + } + if (auto *tinfo = detail::get_type_info(*t)) { + handle th(reinterpret_cast(tinfo->type)); + signature += th.attr("__module__").cast() + "." + + th.attr("__qualname__").cast(); + } else if (auto th = detail::global_internals_native_enum_type_map_get_item(*t)) { + signature += th.attr("__module__").cast() + "." + + th.attr("__qualname__").cast(); + } else if (func_rec->is_new_style_constructor && arg_index == 0) { + // A new-style `__init__` takes `self` as `value_and_holder`. + // Rewrite it to the proper class type. + signature += func_rec->scope.attr("__module__").cast() + "." + + func_rec->scope.attr("__qualname__").cast(); + } else { + signature += detail::quote_cpp_type_name(detail::clean_type_id(t->name())); + } + } else if (c == '!' && special_chars.find(*(pc + 1)) != std::string::npos) { + // typing::Literal escapes special characters with ! + signature += *++pc; + } else if (c == '@') { + // `@^ ... @!` and `@$ ... @!` are used to force arg/return value type (see + // typing::Callable/detail::arg_descr/detail::return_descr) + if (*(pc + 1) == '^') { + is_return_value.emplace(false); + ++pc; + continue; + } + if (*(pc + 1) == '$') { + is_return_value.emplace(true); + ++pc; + continue; + } + if (*(pc + 1) == '!') { + is_return_value.pop(); + ++pc; + continue; + } + // Handle types that differ depending on whether they appear + // in an argument or a return value position (see io_name). + // For named arguments (py::arg()) with noconvert set, return value type is used. + ++pc; + if (!is_return_value.top() + && (!(arg_index < func_rec->args.size() && !func_rec->args[arg_index].convert))) { + while (*pc != '\0' && *pc != '@') { + signature += *pc++; + } + if (*pc == '@') { + ++pc; + } + while (*pc != '\0' && *pc != '@') { + ++pc; + } + } else { + while (*pc != '\0' && *pc != '@') { + ++pc; + } + if (*pc == '@') { + ++pc; + } + while (*pc != '\0' && *pc != '@') { + signature += *pc++; + } + } + } else { + if (c == '-' && *(pc + 1) == '>') { + is_return_value.emplace(true); + } + signature += c; + } + } + return signature; +} + +template +inline std::string generate_type_signature() { + static constexpr auto caster_name_field = make_caster::name; + PYBIND11_DESCR_CONSTEXPR auto descr_types = decltype(caster_name_field)::types(); + // Create a default function_record to ensure the function signature has the proper + // configuration e.g. no_convert. + auto func_rec = function_record(); + size_t type_index = 0; + size_t arg_index = 0; + return generate_function_signature( + caster_name_field.text, &func_rec, descr_types.data(), type_index, arg_index); } #if defined(_MSC_VER) @@ -76,6 +255,49 @@ inline bool apply_exception_translators(std::forward_list & # define PYBIND11_COMPAT_STRDUP strdup #endif +#define PYBIND11_READABLE_FUNCTION_SIGNATURE_EXPR \ + detail::const_name("(") + cast_in::arg_names + detail::const_name(") -> ") + cast_out::name + +// We factor out readable function signatures to a specific template +// so that they don't get duplicated across different instantiations of +// cpp_function::initialize (which is templated on more types). +template +class ReadableFunctionSignature { +public: + using sig_type = decltype(PYBIND11_READABLE_FUNCTION_SIGNATURE_EXPR); + +private: + // We have to repeat PYBIND11_READABLE_FUNCTION_SIGNATURE_EXPR in decltype() + // because C++11 doesn't allow functions to return `auto`. (We don't + // know the type because it's some variant of detail::descr with + // unknown N.) + static constexpr sig_type sig() { return PYBIND11_READABLE_FUNCTION_SIGNATURE_EXPR; } + +public: + static constexpr sig_type kSig = sig(); + // We can only stash the result of detail::descr::types() in a + // constexpr variable if we aren't on MSVC (see + // PYBIND11_DESCR_CONSTEXPR). +#if !defined(_MSC_VER) + using types_type = decltype(sig_type::types()); + static constexpr types_type kTypes = sig_type::types(); +#endif +}; +#undef PYBIND11_READABLE_FUNCTION_SIGNATURE_EXPR + +// Prior to C++17, we don't have inline variables, so we have to +// provide an out-of-line definition of the class member. +#if !defined(PYBIND11_CPP17) +template +constexpr typename ReadableFunctionSignature::sig_type + ReadableFunctionSignature::kSig; +# if !defined(_MSC_VER) +template +constexpr typename ReadableFunctionSignature::types_type + ReadableFunctionSignature::kTypes; +# endif +#endif + PYBIND11_NAMESPACE_END(detail) /// Wraps an arbitrary C++ function/method/lambda function/.. into a callable Python object @@ -109,7 +331,7 @@ class cpp_function : public function { cpp_function(Return (Class::*f)(Arg...), const Extra &...extra) { initialize( [f](Class *c, Arg... args) -> Return { return (c->*f)(std::forward(args)...); }, - (Return(*)(Class *, Arg...)) nullptr, + (Return (*)(Class *, Arg...)) nullptr, extra...); } @@ -121,7 +343,7 @@ class cpp_function : public function { cpp_function(Return (Class::*f)(Arg...) &, const Extra &...extra) { initialize( [f](Class *c, Arg... args) -> Return { return (c->*f)(std::forward(args)...); }, - (Return(*)(Class *, Arg...)) nullptr, + (Return (*)(Class *, Arg...)) nullptr, extra...); } @@ -131,7 +353,7 @@ class cpp_function : public function { cpp_function(Return (Class::*f)(Arg...) const, const Extra &...extra) { initialize([f](const Class *c, Arg... args) -> Return { return (c->*f)(std::forward(args)...); }, - (Return(*)(const Class *, Arg...)) nullptr, + (Return (*)(const Class *, Arg...)) nullptr, extra...); } @@ -143,10 +365,100 @@ class cpp_function : public function { cpp_function(Return (Class::*f)(Arg...) const &, const Extra &...extra) { initialize([f](const Class *c, Arg... args) -> Return { return (c->*f)(std::forward(args)...); }, - (Return(*)(const Class *, Arg...)) nullptr, + (Return (*)(const Class *, Arg...)) nullptr, extra...); } + /// Construct a cpp_function from a class method (non-const, rvalue ref-qualifier) + template + // NOLINTNEXTLINE(google-explicit-constructor) + cpp_function(Return (Class::*f)(Arg...) &&, const Extra &...extra) { + initialize( + [f](Class *c, Arg... args) -> Return { + return (std::move(*c).*f)(std::forward(args)...); + }, + (Return (*)(Class *, Arg...)) nullptr, + extra...); + } + + /// Construct a cpp_function from a class method (const, rvalue ref-qualifier) + template + // NOLINTNEXTLINE(google-explicit-constructor) + cpp_function(Return (Class::*f)(Arg...) const &&, const Extra &...extra) { + initialize( + [f](const Class *c, Arg... args) -> Return { + return (std::move(*c).*f)(std::forward(args)...); + }, + (Return (*)(const Class *, Arg...)) nullptr, + extra...); + } + +#ifdef __cpp_noexcept_function_type + /// Construct a cpp_function from a class method (non-const, no ref-qualifier, noexcept) + template + // NOLINTNEXTLINE(google-explicit-constructor) + cpp_function(Return (Class::*f)(Arg...) noexcept, const Extra &...extra) { + initialize( + [f](Class *c, Arg... args) -> Return { return (c->*f)(std::forward(args)...); }, + (Return (*)(Class *, Arg...)) nullptr, + extra...); + } + + /// Construct a cpp_function from a class method (non-const, lvalue ref-qualifier, noexcept) + template + // NOLINTNEXTLINE(google-explicit-constructor) + cpp_function(Return (Class::*f)(Arg...) & noexcept, const Extra &...extra) { + initialize( + [f](Class *c, Arg... args) -> Return { return (c->*f)(std::forward(args)...); }, + (Return (*)(Class *, Arg...)) nullptr, + extra...); + } + + /// Construct a cpp_function from a class method (const, no ref-qualifier, noexcept) + template + // NOLINTNEXTLINE(google-explicit-constructor) + cpp_function(Return (Class::*f)(Arg...) const noexcept, const Extra &...extra) { + initialize([f](const Class *c, + Arg... args) -> Return { return (c->*f)(std::forward(args)...); }, + (Return (*)(const Class *, Arg...)) nullptr, + extra...); + } + + /// Construct a cpp_function from a class method (const, lvalue ref-qualifier, noexcept) + template + // NOLINTNEXTLINE(google-explicit-constructor) + cpp_function(Return (Class::*f)(Arg...) const & noexcept, const Extra &...extra) { + initialize([f](const Class *c, + Arg... args) -> Return { return (c->*f)(std::forward(args)...); }, + (Return (*)(const Class *, Arg...)) nullptr, + extra...); + } + + /// Construct a cpp_function from a class method (non-const, rvalue ref-qualifier, noexcept) + template + // NOLINTNEXTLINE(google-explicit-constructor) + cpp_function(Return (Class::*f)(Arg...) && noexcept, const Extra &...extra) { + initialize( + [f](Class *c, Arg... args) -> Return { + return (std::move(*c).*f)(std::forward(args)...); + }, + (Return (*)(Class *, Arg...)) nullptr, + extra...); + } + + /// Construct a cpp_function from a class method (const, rvalue ref-qualifier, noexcept) + template + // NOLINTNEXTLINE(google-explicit-constructor) + cpp_function(Return (Class::*f)(Arg...) const && noexcept, const Extra &...extra) { + initialize( + [f](const Class *c, Arg... args) -> Return { + return (std::move(*c).*f)(std::forward(args)...); + }, + (Return (*)(const Class *, Arg...)) nullptr, + extra...); + } +#endif + /// Return the function name object name() const { return attr("__name__"); } @@ -170,6 +482,10 @@ class cpp_function : public function { using namespace detail; struct capture { remove_reference_t f; + + static capture *from_data(void **data) { + return PYBIND11_STD_LAUNDER(reinterpret_cast(data)); + } }; /* Store the function including any extra state it might have (e.g. a lambda capture @@ -189,7 +505,7 @@ class cpp_function : public function { PYBIND11_WARNING_DISABLE_GCC("-Wplacement-new") #endif - new ((capture *) &rec->data) capture{std::forward(f)}; + new (capture::from_data(rec->data)) capture{std::forward(f)}; #if !PYBIND11_HAS_STD_LAUNDER PYBIND11_WARNING_DISABLE_GCC("-Wstrict-aliasing") @@ -199,8 +515,8 @@ class cpp_function : public function { // a significant refactoring it's "impossible" to solve. if (!std::is_trivially_destructible::value) { rec->free_data = [](function_record *r) { - auto data = PYBIND11_STD_LAUNDER((capture *) &r->data); - (void) data; + auto data = capture::from_data(r->data); + (void) data; // suppress "unused variable" warnings data->~capture(); }; } @@ -276,9 +592,20 @@ class cpp_function : public function { constexpr bool has_kw_only_args = any_of...>::value, has_pos_only_args = any_of...>::value, has_arg_annotations = any_of...>::value; + constexpr bool has_is_method = any_of...>::value; + // The implicit `self` argument is not present and not counted in method definitions. + constexpr bool has_args = cast_in::args_pos >= 0; + constexpr bool is_method_with_self_arg_only = has_is_method && !has_args; static_assert(has_arg_annotations || !has_kw_only_args, "py::kw_only requires the use of argument annotations"); - static_assert(has_arg_annotations || !has_pos_only_args, + static_assert(((/* Need `py::arg("arg_name")` annotation in function/method. */ + has_arg_annotations) + || (/* Allow methods with no arguments `def method(self, /): ...`. + * A method has at least one argument `self`. There can be no + * `py::arg` annotation. E.g. `class.def("method", py::pos_only())`. + */ + is_method_with_self_arg_only)) + || !has_pos_only_args, "py::pos_only requires the use of argument annotations (for docstrings " "and aligning the annotations to the argument)"); @@ -294,9 +621,14 @@ class cpp_function : public function { /* Generate a readable signature describing the function's arguments and return value types */ - static constexpr auto signature - = const_name("(") + cast_in::arg_names + const_name(") -> ") + cast_out::name; - PYBIND11_DESCR_CONSTEXPR auto types = decltype(signature)::types(); + static constexpr const auto &signature + = detail::ReadableFunctionSignature::kSig; +#if !defined(_MSC_VER) + static constexpr const auto &types + = detail::ReadableFunctionSignature::kTypes; +#else + PYBIND11_DESCR_CONSTEXPR auto types = std::decay::type::types(); +#endif /* Register the function with Python from generic (non-templated) code */ // Pass on the ownership over the `unique_rec` to `initialize_generic`. `rec` stays valid. @@ -306,6 +638,8 @@ class cpp_function : public function { using FunctionType = Return (*)(Args...); constexpr bool is_function_ptr = std::is_convertible::value && sizeof(capture) == sizeof(void *); + PYBIND11_ENSURE_PRECONDITION_FOR_FUNCTIONAL_H_PERFORMANCE_OPTIMIZATIONS( + !is_function_ptr || std::is_standard_layout::value); if (is_function_ptr) { rec->is_stateless = true; rec->data[1] @@ -394,69 +728,9 @@ class cpp_function : public function { } #endif - /* Generate a proper function signature */ - std::string signature; size_t type_index = 0, arg_index = 0; - bool is_starred = false; - for (const auto *pc = text; *pc != '\0'; ++pc) { - const auto c = *pc; - - if (c == '{') { - // Write arg name for everything except *args and **kwargs. - is_starred = *(pc + 1) == '*'; - if (is_starred) { - continue; - } - // Separator for keyword-only arguments, placed before the kw - // arguments start (unless we are already putting an *args) - if (!rec->has_args && arg_index == rec->nargs_pos) { - signature += "*, "; - } - if (arg_index < rec->args.size() && rec->args[arg_index].name) { - signature += rec->args[arg_index].name; - } else if (arg_index == 0 && rec->is_method) { - signature += "self"; - } else { - signature += "arg" + std::to_string(arg_index - (rec->is_method ? 1 : 0)); - } - signature += ": "; - } else if (c == '}') { - // Write default value if available. - if (!is_starred && arg_index < rec->args.size() && rec->args[arg_index].descr) { - signature += " = "; - signature += rec->args[arg_index].descr; - } - // Separator for positional-only arguments (placed after the - // argument, rather than before like * - if (rec->nargs_pos_only > 0 && (arg_index + 1) == rec->nargs_pos_only) { - signature += ", /"; - } - if (!is_starred) { - arg_index++; - } - } else if (c == '%') { - const std::type_info *t = types[type_index++]; - if (!t) { - pybind11_fail("Internal error while parsing type signature (1)"); - } - if (auto *tinfo = detail::get_type_info(*t)) { - handle th((PyObject *) tinfo->type); - signature += th.attr("__module__").cast() + "." - + th.attr("__qualname__").cast(); - } else if (rec->is_new_style_constructor && arg_index == 0) { - // A new-style `__init__` takes `self` as `value_and_holder`. - // Rewrite it to the proper class type. - signature += rec->scope.attr("__module__").cast() + "." - + rec->scope.attr("__qualname__").cast(); - } else { - std::string tname(t->name()); - detail::clean_type_id(tname); - signature += tname; - } - } else { - signature += c; - } - } + std::string signature + = detail::generate_function_signature(text, rec, types, type_index, arg_index); if (arg_index != args - rec->has_args - rec->has_kwargs || types[type_index] != nullptr) { pybind11_fail("Internal error while parsing type signature (2)"); @@ -464,7 +738,7 @@ class cpp_function : public function { rec->signature = guarded_strdup(signature.c_str()); rec->args.shrink_to_fit(); - rec->nargs = (std::uint16_t) args; + rec->nargs = static_cast(args); if (rec->sibling && PYBIND11_INSTANCE_METHOD_CHECK(rec->sibling.ptr())) { rec->sibling = PYBIND11_INSTANCE_METHOD_GET_FUNCTION(rec->sibling.ptr()); @@ -474,20 +748,15 @@ class cpp_function : public function { if (rec->sibling) { if (PyCFunction_Check(rec->sibling.ptr())) { auto *self = PyCFunction_GET_SELF(rec->sibling.ptr()); - if (!isinstance(self)) { + if (self == nullptr) { + pybind11_fail( + "initialize_generic: Unexpected nullptr from PyCFunction_GET_SELF"); + } + chain = detail::function_record_ptr_from_PyObject(self); + if (chain && !chain->scope.is(rec->scope)) { + /* Never append a method to an overload chain of a parent class; + instead, hide the parent's overloads in this case */ chain = nullptr; - } else { - auto rec_capsule = reinterpret_borrow(self); - if (detail::is_function_record_capsule(rec_capsule)) { - chain = rec_capsule.get_pointer(); - /* Never append a method to an overload chain of a parent class; - instead, hide the parent's overloads in this case */ - if (!chain->scope.is(rec->scope)) { - chain = nullptr; - } - } else { - chain = nullptr; - } } } // Don't trigger for things like the default __init__, which are wrapper_descriptors @@ -505,23 +774,15 @@ class cpp_function : public function { rec->def->ml_name = rec->name; rec->def->ml_meth = reinterpret_cast(reinterpret_cast(dispatcher)); - rec->def->ml_flags = METH_VARARGS | METH_KEYWORDS; + rec->def->ml_flags = METH_FASTCALL | METH_KEYWORDS; - capsule rec_capsule(unique_rec.release(), - detail::get_function_record_capsule_name(), - [](void *ptr) { destruct((detail::function_record *) ptr); }); + object py_func_rec = detail::function_record_PyObject_New(); + (reinterpret_cast(py_func_rec.ptr()))->cpp_func_rec + = unique_rec.release(); guarded_strdup.release(); - object scope_module; - if (rec->scope) { - if (hasattr(rec->scope, "__module__")) { - scope_module = rec->scope.attr("__module__"); - } else if (hasattr(rec->scope, "__name__")) { - scope_module = rec->scope.attr("__name__"); - } - } - - m_ptr = PyCFunction_NewEx(rec->def, rec_capsule.ptr(), scope_module.ptr()); + object scope_module = detail::get_scope_module(rec->scope); + m_ptr = PyCFunction_NewEx(rec->def, py_func_rec.ptr(), scope_module.ptr()); if (!m_ptr) { pybind11_fail("cpp_function::cpp_function(): Could not allocate function object"); } @@ -550,9 +811,9 @@ class cpp_function : public function { // chain. chain_start = rec; rec->next = chain; - auto rec_capsule - = reinterpret_borrow(((PyCFunctionObject *) m_ptr)->m_self); - rec_capsule.set_pointer(unique_rec.release()); + auto *py_func_rec = reinterpret_cast( + PyCFunction_GET_SELF(m_ptr)); + py_func_rec->cpp_func_rec = unique_rec.release(); guarded_strdup.release(); } else { // Or end of chain (normal behavior) @@ -569,7 +830,8 @@ class cpp_function : public function { int index = 0; /* Create a nice pydoc rec including all signatures and docstrings of the functions in the overload chain */ - if (chain && options::show_function_signatures()) { + if (chain && options::show_function_signatures() + && std::strcmp(rec->name, "_pybind11_conduit_v1_") != 0) { // First a generic signature signatures += rec->name; signatures += "(*args, **kwargs)\n"; @@ -578,7 +840,8 @@ class cpp_function : public function { // Then specific overload signatures bool first_user_def = true; for (auto *it = chain_start; it != nullptr; it = it->next) { - if (options::show_function_signatures()) { + if (options::show_function_signatures() + && std::strcmp(rec->name, "_pybind11_conduit_v1_") != 0) { if (index > 0) { signatures += '\n'; } @@ -609,12 +872,11 @@ class cpp_function : public function { } } - /* Install docstring */ - auto *func = (PyCFunctionObject *) m_ptr; - std::free(const_cast(func->m_ml->ml_doc)); + auto *func = reinterpret_cast(m_ptr); // Install docstring if it's non-empty (when at least one option is enabled) - func->m_ml->ml_doc - = signatures.empty() ? nullptr : PYBIND11_COMPAT_STRDUP(signatures.c_str()); + auto *doc = signatures.empty() ? nullptr : PYBIND11_COMPAT_STRDUP(signatures.c_str()); + std::free(const_cast(PYBIND11_PYCFUNCTION_GET_DOC(func))); + PYBIND11_PYCFUNCTION_SET_DOC(func, doc); if (rec->is_method) { m_ptr = PYBIND11_INSTANCE_METHOD_NEW(m_ptr, rec->scope.ptr()); @@ -626,6 +888,8 @@ class cpp_function : public function { } } + friend void detail::function_record_PyTypeObject_methods::tp_dealloc_impl(PyObject *); + /// When a cpp_function is GCed, release any memory allocated by pybind11 static void destruct(detail::function_record *rec, bool free_strings = true) { // If on Python 3.9, check the interpreter "MICRO" (patch) version. @@ -643,9 +907,9 @@ class cpp_function : public function { // so they cannot be freed. Once the function has been created, they can. // Check `make_function_record` for more details. if (free_strings) { - std::free((char *) rec->name); - std::free((char *) rec->doc); - std::free((char *) rec->signature); + std::free(rec->name); + std::free(rec->doc); + std::free(rec->signature); for (auto &arg : rec->args) { std::free(const_cast(arg.name)); std::free(const_cast(arg.descr)); @@ -673,34 +937,33 @@ class cpp_function : public function { } /// Main dispatch logic for calls to functions bound using pybind11 - static PyObject *dispatcher(PyObject *self, PyObject *args_in, PyObject *kwargs_in) { + static PyObject * + dispatcher(PyObject *self, PyObject *const *args_in_arr, size_t nargsf, PyObject *kwnames_in) { using namespace detail; - assert(isinstance(self)); + const function_record *overloads = function_record_ptr_from_PyObject(self); + assert(overloads != nullptr); /* Iterator over the list of potentially admissible overloads */ - const function_record *overloads = reinterpret_cast( - PyCapsule_GetPointer(self, get_function_record_capsule_name())), - *it = overloads; - assert(overloads != nullptr); + const function_record *current_overload = overloads; /* Need to know how many arguments + keyword arguments there are to pick the right overload */ - const auto n_args_in = (size_t) PyTuple_GET_SIZE(args_in); + const auto n_args_in = static_cast(PyVectorcall_NARGS(nargsf)); - handle parent = n_args_in > 0 ? PyTuple_GET_ITEM(args_in, 0) : nullptr, + handle parent = n_args_in > 0 ? args_in_arr[0] : nullptr, result = PYBIND11_TRY_NEXT_OVERLOAD; auto self_value_and_holder = value_and_holder(); if (overloads->is_constructor) { if (!parent || !PyObject_TypeCheck(parent.ptr(), (PyTypeObject *) overloads->scope.ptr())) { - PyErr_SetString( - PyExc_TypeError, - "__init__(self, ...) called with invalid or missing `self` argument"); + set_error(PyExc_TypeError, + "__init__(self, ...) called with invalid or missing `self` argument"); return nullptr; } - auto *const tinfo = get_type_info((PyTypeObject *) overloads->scope.ptr()); + auto *const tinfo + = get_type_info(reinterpret_cast(overloads->scope.ptr())); auto *const pi = reinterpret_cast(parent.ptr()); self_value_and_holder = pi->get_value_and_holder(tinfo, true); @@ -719,9 +982,10 @@ class cpp_function : public function { std::vector second_pass; // However, if there are no overloads, we can just skip the no-convert pass entirely - const bool overloaded = it != nullptr && it->next != nullptr; + const bool overloaded + = current_overload != nullptr && current_overload->next != nullptr; - for (; it != nullptr; it = it->next) { + for (; current_overload != nullptr; current_overload = current_overload->next) { /* For each overload: 1. Copy all positional arguments we were given, also checking to make sure that @@ -742,7 +1006,7 @@ class cpp_function : public function { a result other than PYBIND11_TRY_NEXT_OVERLOAD. */ - const function_record &func = *it; + const function_record &func = *current_overload; size_t num_args = func.nargs; // Number of positional arguments that we need if (func.has_args) { --num_args; // (but don't count py::args @@ -764,7 +1028,7 @@ class cpp_function : public function { function_call call(func, parent); // Protect std::min with parentheses - size_t args_to_copy = (std::min)(pos_args, n_args_in); + size_t args_to_copy = (std::min) (pos_args, n_args_in); size_t args_copied = 0; // 0. Inject new-style `self` argument @@ -775,7 +1039,7 @@ class cpp_function : public function { self_value_and_holder.type->dealloc(self_value_and_holder); } - call.init_self = PyTuple_GET_ITEM(args_in, 0); + call.init_self = args_in_arr[0]; call.args.emplace_back(reinterpret_cast(&self_value_and_holder)); call.args_convert.push_back(false); ++args_copied; @@ -786,17 +1050,24 @@ class cpp_function : public function { for (; args_copied < args_to_copy; ++args_copied) { const argument_record *arg_rec = args_copied < func.args.size() ? &func.args[args_copied] : nullptr; - if (kwargs_in && arg_rec && arg_rec->name - && dict_getitemstring(kwargs_in, arg_rec->name)) { + + /* if the argument is listed in the call site's kwargs, but the argument is + also fulfilled positionally, then the call can't match this overload. for + example, the call site is: foo(0, key=1) but our overload is foo(key:int) then + this call can't be for us, because it would be invalid. + */ + if (kwnames_in && arg_rec && arg_rec->name + && keyword_index(kwnames_in, arg_rec->name) >= 0) { bad_arg = true; break; } - handle arg(PyTuple_GET_ITEM(args_in, args_copied)); + handle arg(args_in_arr[args_copied]); if (arg_rec && !arg_rec->none && arg.is_none()) { bad_arg = true; break; } + call.args.push_back(arg); call.args_convert.push_back(arg_rec ? arg_rec->convert : true); } @@ -808,20 +1079,12 @@ class cpp_function : public function { // to copy the rest into a py::args argument. size_t positional_args_copied = args_copied; - // We'll need to copy this if we steal some kwargs for defaults - dict kwargs = reinterpret_borrow(kwargs_in); - // 1.5. Fill in any missing pos_only args from defaults if they exist if (args_copied < func.nargs_pos_only) { for (; args_copied < func.nargs_pos_only; ++args_copied) { const auto &arg_rec = func.args[args_copied]; - handle value; - if (arg_rec.value) { - value = arg_rec.value; - } - if (value) { - call.args.push_back(value); + call.args.push_back(arg_rec.value); call.args_convert.push_back(arg_rec.convert); } else { break; @@ -834,46 +1097,42 @@ class cpp_function : public function { } // 2. Check kwargs and, failing that, defaults that may help complete the list + small_vector used_kwargs( + kwnames_in ? static_cast(PyTuple_GET_SIZE(kwnames_in)) : 0, false); + size_t used_kwargs_count = 0; if (args_copied < num_args) { - bool copied_kwargs = false; - for (; args_copied < num_args; ++args_copied) { const auto &arg_rec = func.args[args_copied]; handle value; - if (kwargs_in && arg_rec.name) { - value = dict_getitemstring(kwargs.ptr(), arg_rec.name); + if (kwnames_in && arg_rec.name) { + ssize_t i = keyword_index(kwnames_in, arg_rec.name); + if (i >= 0) { + value = args_in_arr[n_args_in + static_cast(i)]; + used_kwargs.set(static_cast(i), true); + used_kwargs_count++; + } } - if (value) { - // Consume a kwargs value - if (!copied_kwargs) { - kwargs = reinterpret_steal(PyDict_Copy(kwargs.ptr())); - copied_kwargs = true; - } - if (PyDict_DelItemString(kwargs.ptr(), arg_rec.name) == -1) { - throw error_already_set(); - } - } else if (arg_rec.value) { + if (!value) { value = arg_rec.value; + if (!value) { + break; + } } if (!arg_rec.none && value.is_none()) { break; } - if (value) { - // If we're at the py::args index then first insert a stub for it to be - // replaced later - if (func.has_args && call.args.size() == func.nargs_pos) { - call.args.push_back(none()); - } - - call.args.push_back(value); - call.args_convert.push_back(arg_rec.convert); - } else { - break; + // If we're at the py::args index then first insert a stub for it to be + // replaced later + if (func.has_args && call.args.size() == func.nargs_pos) { + call.args.push_back(none()); } + + call.args.push_back(value); + call.args_convert.push_back(arg_rec.convert); } if (args_copied < num_args) { @@ -883,47 +1142,50 @@ class cpp_function : public function { } // 3. Check everything was consumed (unless we have a kwargs arg) - if (kwargs && !kwargs.empty() && !func.has_kwargs) { + if (!func.has_kwargs && used_kwargs_count < used_kwargs.size()) { continue; // Unconsumed kwargs, but no py::kwargs argument to accept them } // 4a. If we have a py::args argument, create a new tuple with leftovers if (func.has_args) { - tuple extra_args; - if (args_to_copy == 0) { - // We didn't copy out any position arguments from the args_in tuple, so we - // can reuse it directly without copying: - extra_args = reinterpret_borrow(args_in); - } else if (positional_args_copied >= n_args_in) { - extra_args = tuple(0); + if (positional_args_copied >= n_args_in) { + call.args_ref = tuple(0); } else { size_t args_size = n_args_in - positional_args_copied; - extra_args = tuple(args_size); + tuple extra_args(args_size); for (size_t i = 0; i < args_size; ++i) { - extra_args[i] = PyTuple_GET_ITEM(args_in, positional_args_copied + i); + extra_args[i] = args_in_arr[positional_args_copied + i]; } + call.args_ref = std::move(extra_args); } if (call.args.size() <= func.nargs_pos) { - call.args.push_back(extra_args); + call.args.push_back(call.args_ref); } else { - call.args[func.nargs_pos] = extra_args; + call.args[func.nargs_pos] = call.args_ref; } call.args_convert.push_back(false); - call.args_ref = std::move(extra_args); } // 4b. If we have a py::kwargs, pass on any remaining kwargs if (func.has_kwargs) { - if (!kwargs.ptr()) { - kwargs = dict(); // If we didn't get one, send an empty one + dict kwargs; + for (size_t i = 0; i < used_kwargs.size(); ++i) { + if (!used_kwargs[i]) { + // Cast values into handles before indexing into kwargs to ensure + // well-defined evaluation order (MSVC C4866). + handle arg_in_arr = args_in_arr[n_args_in + i], + kwname = PyTuple_GET_ITEM(kwnames_in, i); + kwargs[kwname] = arg_in_arr; + } } call.args.push_back(kwargs); call.args_convert.push_back(false); call.kwargs_ref = std::move(kwargs); } -// 5. Put everything in a vector. Not technically step 5, we've been building it -// in `call.args` all along. + // 5. Put everything in a vector. Not technically step 5, we've been building it + // in `call.args` all along. + #if defined(PYBIND11_DETAILED_ERROR_MESSAGES) if (call.args.size() != func.nargs || call.args_convert.size() != func.nargs) { pybind11_fail("Internal error: function call dispatcher inserted wrong number " @@ -931,13 +1193,14 @@ class cpp_function : public function { } #endif - std::vector second_pass_convert; + args_convert_vector second_pass_convert; if (overloaded) { // We're in the first no-convert pass, so swap out the conversion flags for a // set of all-false flags. If the call fails, we'll swap the flags back in for // the conversion-allowed call below. - second_pass_convert.resize(func.nargs, false); - call.args_convert.swap(second_pass_convert); + second_pass_convert = std::move(call.args_convert); + call.args_convert + = args_convert_vector(func.nargs, false); } // 6. Call the function. @@ -980,10 +1243,10 @@ class cpp_function : public function { } if (result.ptr() != PYBIND11_TRY_NEXT_OVERLOAD) { - // The error reporting logic below expects 'it' to be valid, as it would be - // if we'd encountered this failure in the first-pass loop. + // The error reporting logic below expects 'current_overload' to be valid, + // as it would be if we'd encountered this failure in the first-pass loop. if (!result) { - it = &call.func; + current_overload = &call.func; } break; } @@ -997,34 +1260,7 @@ class cpp_function : public function { throw; #endif } catch (...) { - /* When an exception is caught, give each registered exception - translator a chance to translate it to a Python exception. First - all module-local translators will be tried in reverse order of - registration. If none of the module-locale translators handle - the exception (or there are no module-locale translators) then - the global translators will be tried, also in reverse order of - registration. - - A translator may choose to do one of the following: - - - catch the exception and call PyErr_SetString or PyErr_SetObject - to set a standard (or custom) Python exception, or - - do nothing and let the exception fall through to the next translator, or - - delegate translation to the next translator by throwing a new type of exception. - */ - - auto &local_exception_translators - = get_local_internals().registered_exception_translators; - if (detail::apply_exception_translators(local_exception_translators)) { - return nullptr; - } - auto &exception_translators = get_internals().registered_exception_translators; - if (detail::apply_exception_translators(exception_translators)) { - return nullptr; - } - - PyErr_SetString(PyExc_SystemError, - "Exception escaped from default exception translator!"); + try_translate_exceptions(); return nullptr; } @@ -1080,40 +1316,37 @@ class cpp_function : public function { msg += '\n'; } msg += "\nInvoked with: "; - auto args_ = reinterpret_borrow(args_in); bool some_args = false; - for (size_t ti = overloads->is_constructor ? 1 : 0; ti < args_.size(); ++ti) { + for (size_t ti = overloads->is_constructor ? 1 : 0; ti < n_args_in; ++ti) { if (!some_args) { some_args = true; } else { msg += ", "; } try { - msg += pybind11::repr(args_[ti]); + msg += pybind11::repr(args_in_arr[ti]); } catch (const error_already_set &) { msg += ""; } } - if (kwargs_in) { - auto kwargs = reinterpret_borrow(kwargs_in); - if (!kwargs.empty()) { - if (some_args) { - msg += "; "; + if (kwnames_in && PyTuple_GET_SIZE(kwnames_in) > 0) { + if (some_args) { + msg += "; "; + } + msg += "kwargs: "; + bool first = true; + for (size_t i = 0; i < static_cast(PyTuple_GET_SIZE(kwnames_in)); ++i) { + if (first) { + first = false; + } else { + msg += ", "; } - msg += "kwargs: "; - bool first = true; - for (auto kwarg : kwargs) { - if (first) { - first = false; - } else { - msg += ", "; - } - msg += pybind11::str("{}=").format(kwarg.first); - try { - msg += pybind11::repr(kwarg.second); - } catch (const error_already_set &) { - msg += ""; - } + msg += reinterpret_borrow(PyTuple_GET_ITEM(kwnames_in, i)); + msg += '='; + try { + msg += pybind11::repr(args_in_arr[n_args_in + i]); + } catch (const error_already_set &) { + msg += ""; } } } @@ -1125,20 +1358,21 @@ class cpp_function : public function { raise_from(PyExc_TypeError, msg.c_str()); return nullptr; } - PyErr_SetString(PyExc_TypeError, msg.c_str()); + set_error(PyExc_TypeError, msg.c_str()); return nullptr; } if (!result) { std::string msg = "Unable to convert function return value to a " "Python type! The signature was\n\t"; - msg += it->signature; + assert(current_overload != nullptr); + msg += current_overload->signature; append_note_if_missing_header_is_suspected(msg); // Attach additional error info to the exception if supported if (PyErr_Occurred()) { raise_from(PyExc_TypeError, msg.c_str()); return nullptr; } - PyErr_SetString(PyExc_TypeError, msg.c_str()); + set_error(PyExc_TypeError, msg.c_str()); return nullptr; } if (overloads->is_constructor && !self_value_and_holder.holder_constructed()) { @@ -1147,8 +1381,211 @@ class cpp_function : public function { } return result.ptr(); } + + static ssize_t keyword_index(PyObject *haystack, char const *needle) { + /* kwargs is usually very small (<= 5 entries). The arg strings are typically interned. + * CPython itself implements the search this way, first comparing all pointers ... which is + * cheap and will work if the strings are interned. If it fails, then it falls back to a + * second lexicographic check. This is wildly expensive for huge argument lists, but those + * are incredibly rare so we optimize for the vastly common case of just a couple of args. + */ + auto n = PyTuple_GET_SIZE(haystack); + auto s = reinterpret_steal(PyUnicode_InternFromString(needle)); + for (ssize_t i = 0; i < n; ++i) { + if (PyTuple_GET_ITEM(haystack, i) == s.ptr()) { + return i; + } + } + for (ssize_t i = 0; i < n; ++i) { + if (PyUnicode_Compare(PyTuple_GET_ITEM(haystack, i), s.ptr()) == 0) { + return i; + } + } + return -1; + } }; +PYBIND11_NAMESPACE_BEGIN(detail) + +PYBIND11_NAMESPACE_BEGIN(function_record_PyTypeObject_methods) + +// This implementation needs the definition of `class cpp_function`. +inline void tp_dealloc_impl(PyObject *self) { + // Save type before PyObject_Free invalidates self. + auto *type = Py_TYPE(self); + auto *py_func_rec = reinterpret_cast(self); + cpp_function::destruct(py_func_rec->cpp_func_rec); + py_func_rec->cpp_func_rec = nullptr; + // PyObject_New increments the heap type refcount and allocates via + // PyObject_Malloc; balance both here + PyObject_Free(self); + Py_DECREF(type); +} + +PYBIND11_NAMESPACE_END(function_record_PyTypeObject_methods) + +template <> +struct handle_type_name { + static constexpr auto name = const_name("collections.abc.Callable"); +}; + +PYBIND11_NAMESPACE_END(detail) + +// Use to activate Py_MOD_GIL_NOT_USED. +class mod_gil_not_used { +public: + explicit mod_gil_not_used(bool flag = true) : flag_(flag) {} + bool flag() const { return flag_; } + +private: + bool flag_; +}; + +class multiple_interpreters { +public: + enum class level { + not_supported, /// Use to activate Py_MOD_MULTIPLE_INTERPRETERS_NOT_SUPPORTED + shared_gil, /// Use to activate Py_MOD_MULTIPLE_INTERPRETERS_SUPPORTED + per_interpreter_gil /// Use to activate Py_MOD_PER_INTERPRETER_GIL_SUPPORTED + }; + + static multiple_interpreters not_supported() { + return multiple_interpreters(level::not_supported); + } + static multiple_interpreters shared_gil() { return multiple_interpreters(level::shared_gil); } + static multiple_interpreters per_interpreter_gil() { + return multiple_interpreters(level::per_interpreter_gil); + } + + explicit constexpr multiple_interpreters(level l) : level_(l) {} + level value() const { return level_; } + +private: + level level_; +}; + +PYBIND11_NAMESPACE_BEGIN(detail) + +inline bool gil_not_used_option() { return false; } +template +bool gil_not_used_option(F &&, O &&...o); +template +inline bool gil_not_used_option(mod_gil_not_used f, O &&...o) { + return f.flag() || gil_not_used_option(o...); +} +template +inline bool gil_not_used_option(F &&, O &&...o) { + return gil_not_used_option(o...); +} + +#ifdef Py_mod_multiple_interpreters +inline void *multi_interp_slot() { return Py_MOD_MULTIPLE_INTERPRETERS_NOT_SUPPORTED; } +template +inline void *multi_interp_slot(multiple_interpreters mi, O &&...o) { + switch (mi.value()) { + case multiple_interpreters::level::per_interpreter_gil: + return Py_MOD_PER_INTERPRETER_GIL_SUPPORTED; + case multiple_interpreters::level::shared_gil: + return Py_MOD_MULTIPLE_INTERPRETERS_SUPPORTED; + case multiple_interpreters::level::not_supported: + return Py_MOD_MULTIPLE_INTERPRETERS_NOT_SUPPORTED; + } + // silence warnings with this unreachable line: + return multi_interp_slot(o...); +} +template +inline void *multi_interp_slot(F &&, O &&...o) { + return multi_interp_slot(o...); +} +#endif + +/* +Return a borrowed reference to the named module if it has been successfully initialized within this +interpreter before. nullptr if it has not been successfully initialized. +*/ +inline PyObject *get_cached_module(pybind11::str const &nameobj) { + dict state = detail::get_python_state_dict(); + if (!state.contains("__pybind11_module_cache")) { + return nullptr; + } + dict cache = state["__pybind11_module_cache"]; + if (!cache.contains(nameobj)) { + return nullptr; + } + return cache[nameobj].ptr(); +} + +/* +Add successfully initialized a module object to the internal cache. + +The module must have a __spec__ attribute with a name attribute. +*/ +inline void cache_completed_module(pybind11::object const &mod) { + dict state = detail::get_python_state_dict(); + if (!state.contains("__pybind11_module_cache")) { + state["__pybind11_module_cache"] = dict(); + } + state["__pybind11_module_cache"][mod.attr("__spec__").attr("name")] = mod; +} + +/* +A Py_mod_create slot function which will return the previously created module from the cache if one +exists, and otherwise will create a new module object. +*/ +inline PyObject *cached_create_module(PyObject *spec, PyModuleDef *) { + (void) &cache_completed_module; // silence unused-function warnings, it is used in a macro + + auto nameobj = getattr(reinterpret_borrow(spec), "name", none()); + if (nameobj.is_none()) { + set_error(PyExc_ImportError, "module spec is missing a name"); + return nullptr; + } + + auto *mod = get_cached_module(nameobj); + if (mod) { + Py_INCREF(mod); + } else { + mod = PyModule_NewObject(nameobj.ptr()); + } + return mod; +} + +/// Must be a POD type, and must hold enough entries for all of the possible slots PLUS ONE for +/// the sentinel (0) end slot. +using slots_array = std::array; + +/// Initialize an array of slots based on the supplied exec slot and options. +template +inline slots_array init_slots(int (*exec_fn)(PyObject *), Options &&...options) noexcept { + /* NOTE: slots_array MUST be large enough to hold all possible options. If you add an option + here, you MUST also increase the size of slots_array in the type alias above! */ + slots_array mod_def_slots; + size_t next_slot = 0; + + mod_def_slots[next_slot++] = {Py_mod_create, reinterpret_cast(&cached_create_module)}; + + if (exec_fn != nullptr) { + mod_def_slots[next_slot++] = {Py_mod_exec, reinterpret_cast(exec_fn)}; + } + +#ifdef Py_mod_multiple_interpreters + mod_def_slots[next_slot++] = {Py_mod_multiple_interpreters, multi_interp_slot(options...)}; +#endif + + if (gil_not_used_option(options...)) { +#if defined(Py_mod_gil) && defined(Py_GIL_DISABLED) + mod_def_slots[next_slot++] = {Py_mod_gil, Py_MOD_GIL_NOT_USED}; +#endif + } + + // slots must have a zero end sentinel + mod_def_slots[next_slot++] = {0, nullptr}; + + return mod_def_slots; +} + +PYBIND11_NAMESPACE_END(detail) + /// Wrapper for Python extension modules class module_ : public object { public: @@ -1203,6 +1640,24 @@ class module_ : public object { if (doc && options::show_user_defined_docstrings()) { result.attr("__doc__") = pybind11::str(doc); } + +#if defined(GRAALVM_PYTHON) && (!defined(GRAALPY_VERSION_NUM) || GRAALPY_VERSION_NUM < 0x190000) + // GraalPy doesn't support PyModule_GetFilenameObject, + // so getting by attribute (see PR #5584) + handle this_module = m_ptr; + if (object this_file = getattr(this_module, "__file__", none())) { + result.attr("__file__") = this_file; + } +#else + handle this_file = PyModule_GetFilenameObject(m_ptr); + if (this_file) { + result.attr("__file__") = this_file; + } else if (PyErr_ExceptionMatches(PyExc_SystemError) != 0) { + PyErr_Clear(); + } else { + throw error_already_set(); + } +#endif attr(name) = result; return result; } @@ -1242,26 +1697,29 @@ class module_ : public object { PyModule_AddObject(ptr(), name, obj.inc_ref().ptr() /* steals a reference */); } - using module_def = PyModuleDef; // TODO: Can this be removed (it was needed only for Python 2)? + // DEPRECATED (since PR #5688): Use PyModuleDef directly instead. + using module_def = PyModuleDef; /** \rst Create a new top-level module that can be used as the main module of a C extension. - ``def`` should point to a statically allocated module_def. + ``def`` should point to a statically allocated PyModuleDef. \endrst */ - static module_ create_extension_module(const char *name, const char *doc, module_def *def) { - // module_def is PyModuleDef + static module_ create_extension_module(const char *name, + const char *doc, + PyModuleDef *def, + mod_gil_not_used gil_not_used + = mod_gil_not_used(false)) { // Placement new (not an allocation). - def = new (def) - PyModuleDef{/* m_base */ PyModuleDef_HEAD_INIT, - /* m_name */ name, - /* m_doc */ options::show_user_defined_docstrings() ? doc : nullptr, - /* m_size */ -1, - /* m_methods */ nullptr, - /* m_slots */ nullptr, - /* m_traverse */ nullptr, - /* m_clear */ nullptr, - /* m_free */ nullptr}; + new (def) PyModuleDef{/* m_base */ PyModuleDef_HEAD_INIT, + /* m_name */ name, + /* m_doc */ options::show_user_defined_docstrings() ? doc : nullptr, + /* m_size */ -1, + /* m_methods */ nullptr, + /* m_slots */ nullptr, + /* m_traverse */ nullptr, + /* m_clear */ nullptr, + /* m_free */ nullptr}; auto *m = PyModule_Create(def); if (m == nullptr) { if (PyErr_Occurred()) { @@ -1269,6 +1727,11 @@ class module_ : public object { } pybind11_fail("Internal error in module_::create_extension_module()"); } + if (gil_not_used.flag()) { +#ifdef Py_GIL_DISABLED + PyUnstable_Module_SetGIL(m, Py_MOD_GIL_NOT_USED); +#endif + } // TODO: Should be reinterpret_steal for Python 3, but Python also steals it again when // returned from PyInit_... // For Python 2, reinterpret_borrow was correct. @@ -1276,6 +1739,15 @@ class module_ : public object { } }; +PYBIND11_NAMESPACE_BEGIN(detail) + +template <> +struct handle_type_name { + static constexpr auto name = const_name("types.ModuleType"); +}; + +PYBIND11_NAMESPACE_END(detail) + // When inside a namespace (or anywhere as long as it's not the first item on a line), // C++20 allows "module" to be used. This is provided for backward compatibility, and for // simplicity, if someone wants to use py::module for example, that is perfectly safe. @@ -1285,15 +1757,14 @@ using module = module_; /// Return a dictionary representing the global variables in the current execution frame, /// or ``__main__.__dict__`` if there is no frame (usually when the interpreter is embedded). inline dict globals() { +#if PY_VERSION_HEX >= 0x030d0000 + PyObject *p = PyEval_GetFrameGlobals(); + return p ? reinterpret_steal(p) + : reinterpret_borrow(module_::import("__main__").attr("__dict__").ptr()); +#else PyObject *p = PyEval_GetGlobals(); return reinterpret_borrow(p ? p : module_::import("__main__").attr("__dict__").ptr()); -} - -template ()>> -PYBIND11_DEPRECATED("make_simple_namespace should be replaced with " - "py::module_::import(\"types\").attr(\"SimpleNamespace\") ") -object make_simple_namespace(Args &&...args_) { - return module_::import("types").attr("SimpleNamespace")(std::forward(args_)...); +#endif } PYBIND11_NAMESPACE_BEGIN(detail) @@ -1319,7 +1790,7 @@ class generic_type : public object { /* Register supplemental type information in C++ dict */ auto *tinfo = new detail::type_info(); - tinfo->type = (PyTypeObject *) m_ptr; + tinfo->type = reinterpret_cast(m_ptr); tinfo->cpptype = rec.type; tinfo->type_size = rec.type_size; tinfo->type_align = rec.type_align; @@ -1327,26 +1798,43 @@ class generic_type : public object { tinfo->holder_size_in_ptrs = size_in_ptrs(rec.holder_size); tinfo->init_instance = rec.init_instance; tinfo->dealloc = rec.dealloc; + tinfo->get_trampoline_self_life_support = rec.get_trampoline_self_life_support; tinfo->simple_type = true; tinfo->simple_ancestors = true; - tinfo->default_holder = rec.default_holder; tinfo->module_local = rec.module_local; + tinfo->holder_enum_v = rec.holder_enum_v; + + with_internals([&](internals &internals) { + auto tindex = std::type_index(*rec.type); + tinfo->direct_conversions = &internals.direct_conversions[tindex]; + auto &local_internals = get_local_internals(); + if (rec.module_local) { + local_internals.registered_types_cpp[rec.type] = tinfo; + } else { + internals.registered_types_cpp[tindex] = tinfo; +#if PYBIND11_INTERNALS_VERSION >= 12 + internals.registered_types_cpp_fast[rec.type] = tinfo; +#endif + } - auto &internals = get_internals(); - auto tindex = std::type_index(*rec.type); - tinfo->direct_conversions = &internals.direct_conversions[tindex]; - if (rec.module_local) { - get_local_internals().registered_types_cpp[tindex] = tinfo; - } else { - internals.registered_types_cpp[tindex] = tinfo; - } - internals.registered_types_py[(PyTypeObject *) m_ptr] = {tinfo}; + PYBIND11_WARNING_PUSH +#if defined(__GNUC__) && __GNUC__ == 12 + // When using GCC 12 these warnings are disabled as they trigger + // false positive warnings. Discussed here: + // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=115824. + PYBIND11_WARNING_DISABLE_GCC("-Warray-bounds") + PYBIND11_WARNING_DISABLE_GCC("-Wstringop-overread") +#endif + internals.registered_types_py[reinterpret_cast(m_ptr)] = {tinfo}; + PYBIND11_WARNING_POP + }); if (rec.bases.size() > 1 || rec.multiple_inheritance) { mark_parents_nonsimple(tinfo->type); tinfo->simple_ancestors = false; } else if (rec.bases.size() == 1) { - auto *parent_tinfo = get_type_info((PyTypeObject *) rec.bases[0].ptr()); + auto *parent_tinfo + = get_type_info(reinterpret_cast(rec.bases[0].ptr())); assert(parent_tinfo != nullptr); bool parent_simple_ancestors = parent_tinfo->simple_ancestors; tinfo->simple_ancestors = parent_simple_ancestors; @@ -1365,17 +1853,17 @@ class generic_type : public object { void mark_parents_nonsimple(PyTypeObject *value) { auto t = reinterpret_borrow(value->tp_bases); for (handle h : t) { - auto *tinfo2 = get_type_info((PyTypeObject *) h.ptr()); + auto *tinfo2 = get_type_info(reinterpret_cast(h.ptr())); if (tinfo2) { tinfo2->simple_type = false; } - mark_parents_nonsimple((PyTypeObject *) h.ptr()); + mark_parents_nonsimple(reinterpret_cast(h.ptr())); } } void install_buffer_funcs(buffer_info *(*get_buffer)(PyObject *, void *), void *get_buffer_data) { - auto *type = (PyHeapTypeObject *) m_ptr; + auto *type = reinterpret_cast(m_ptr); auto *tinfo = detail::get_type_info(&type->ht_type); if (!type->ht_type.tp_as_buffer) { @@ -1397,8 +1885,8 @@ class generic_type : public object { const auto is_static = (rec_func != nullptr) && !(rec_func->is_method && rec_func->scope); const auto has_doc = (rec_func != nullptr) && (rec_func->doc != nullptr) && pybind11::options::show_user_defined_docstrings(); - auto property = handle( - (PyObject *) (is_static ? get_internals().static_property_type : &PyProperty_Type)); + auto property = handle(reinterpret_cast( + is_static ? get_internals().static_property_type : &PyProperty_Type)); attr(name) = property(fget.ptr() ? fget : none(), fset.ptr() ? fset : none(), /*deleter*/ none(), @@ -1467,30 +1955,323 @@ inline void add_class_method(object &cls, const char *name_, const cpp_function } } +/// Type trait to rebind a member function pointer's class to `Derived`, preserving all +/// cv/ref/noexcept qualifiers. The primary template has no `type` member, providing SFINAE +/// failure for unsupported member function pointer types. `source_class` holds the original +/// class for use in `is_accessible_base_of` checks. +template +struct rebind_member_ptr {}; + +// Define one specialization per supported qualifier combination via a local macro. +// The qualifiers argument appears in type position, not expression position, so +// parenthesizing it would produce invalid C++. +// The no-qualifier specialization is written out explicitly to avoid invoking the macro with an +// empty argument, which triggers MSVC warning C4003. +template +struct rebind_member_ptr { + using type = Return (Derived::*)(Args...); + using source_class = Class; +}; +// NOLINTBEGIN(bugprone-macro-parentheses) +#define PYBIND11_REBIND_MEMBER_PTR(qualifiers) \ + template \ + struct rebind_member_ptr { \ + using type = Return (Derived::*)(Args...) qualifiers; \ + using source_class = Class; \ + } +PYBIND11_REBIND_MEMBER_PTR(const); +PYBIND11_REBIND_MEMBER_PTR(&); +PYBIND11_REBIND_MEMBER_PTR(const &); +PYBIND11_REBIND_MEMBER_PTR(&&); +PYBIND11_REBIND_MEMBER_PTR(const &&); +#ifdef __cpp_noexcept_function_type +PYBIND11_REBIND_MEMBER_PTR(noexcept); +PYBIND11_REBIND_MEMBER_PTR(const noexcept); +PYBIND11_REBIND_MEMBER_PTR(& noexcept); +PYBIND11_REBIND_MEMBER_PTR(const & noexcept); +PYBIND11_REBIND_MEMBER_PTR(&& noexcept); +PYBIND11_REBIND_MEMBER_PTR(const && noexcept); +#endif +#undef PYBIND11_REBIND_MEMBER_PTR +// NOLINTEND(bugprone-macro-parentheses) + +/// Shared implementation body for all method_adaptor member-function-pointer overloads. +/// Asserts Base is accessible from Derived, then casts the member pointer. +template , + typename Adapted = typename Traits::type> +constexpr PYBIND11_ALWAYS_INLINE Adapted adapt_member_ptr(T pmf) { + static_assert( + detail::is_accessible_base_of::value, + "Cannot bind an inaccessible base class method; use a lambda definition instead"); + return pmf; +} + PYBIND11_NAMESPACE_END(detail) /// Given a pointer to a member function, cast it to its `Derived` version. -/// Forward everything else unchanged. -template -auto method_adaptor(F &&f) -> decltype(std::forward(f)) { +/// For all other callables (lambdas, function pointers, etc.), forward unchanged. +/// +/// Two overloads cover all cases without explicit per-qualifier instantiations: +/// +/// (1) Generic fallback — disabled for member function pointers so that (2) wins +/// without any partial-ordering ambiguity. +/// (2) MFP overload — SFINAE on rebind_member_ptr::type, which exists for every +/// supported qualifier combination (const, &, &&, noexcept, ...). A single +/// template therefore covers all combinations that rebind_member_ptr handles. +template < + typename /*Derived*/, + typename F, + detail::enable_if_t>::value, + int> = 0> +constexpr auto method_adaptor(F &&f) -> decltype(std::forward(f)) { return std::forward(f); } -template -auto method_adaptor(Return (Class::*pmf)(Args...)) -> Return (Derived::*)(Args...) { - static_assert( - detail::is_accessible_base_of::value, - "Cannot bind an inaccessible base class method; use a lambda definition instead"); - return pmf; +template ::type> +constexpr Adapted method_adaptor(T pmf) { + // Expected to be redundant (SFINAE on rebind_member_ptr) but cheap and makes the intent + // explicit. + static_assert(std::is_member_function_pointer::value, + "method_adaptor: T must be a member function pointer"); + return detail::adapt_member_ptr(pmf); } -template -auto method_adaptor(Return (Class::*pmf)(Args...) const) -> Return (Derived::*)(Args...) const { - static_assert( - detail::is_accessible_base_of::value, - "Cannot bind an inaccessible base class method; use a lambda definition instead"); - return pmf; -} +PYBIND11_NAMESPACE_BEGIN(detail) + +// Helper for the property_cpp_function static member functions below. +// The only purpose of these functions is to support .def_readonly & .def_readwrite. +// In this context, the PM template parameter is certain to be a Pointer to a Member. +// The main purpose of must_be_member_function_pointer is to make this obvious, and to guard +// against accidents. As a side-effect, it also explains why the syntactical overhead for +// perfect forwarding is not needed. +template +using must_be_member_function_pointer = enable_if_t::value, int>; + +// Note that property_cpp_function is intentionally in the main pybind11 namespace, +// because user-defined specializations could be useful. + +// Classic (non-smart_holder) implementations for .def_readonly and .def_readwrite +// getter and setter functions. +// WARNING: This classic implementation can lead to dangling pointers for raw pointer members. +// See test_ptr() in tests/test_class_sh_property.py +// However, this implementation works as-is (and safely) for smart_holder std::shared_ptr members. +template +struct property_cpp_function_classic { + template = 0> + static cpp_function readonly(PM pm, const handle &hdl) { + return cpp_function([pm](const T &c) -> const D & { return c.*pm; }, is_method(hdl)); + } + + template = 0> + static cpp_function read(PM pm, const handle &hdl) { + return readonly(pm, hdl); + } + + template = 0> + static cpp_function write(PM pm, const handle &hdl) { + return cpp_function([pm](T &c, const D &value) { c.*pm = value; }, is_method(hdl)); + } +}; + +PYBIND11_NAMESPACE_END(detail) + +template +struct property_cpp_function : detail::property_cpp_function_classic {}; + +PYBIND11_NAMESPACE_BEGIN(detail) + +template +struct both_t_and_d_use_type_caster_base : std::false_type {}; + +// `T` is assumed to be equivalent to `intrinsic_t`. +// `D` is may or may not be equivalent to `intrinsic_t`. +template +struct both_t_and_d_use_type_caster_base< + T, + D, + enable_if_t, type_caster>, + std::is_base_of>, make_caster>>::value>> + : std::true_type {}; + +// Specialization for raw pointer members, using smart_holder if that is the class_ holder, +// or falling back to the classic implementation if not. +// WARNING: Like the classic implementation, this implementation can lead to dangling pointers. +// See test_ptr() in tests/test_class_sh_property.py +// However, the read functions return a shared_ptr to the member, emulating the PyCLIF approach: +// https://github.com/google/clif/blob/c371a6d4b28d25d53a16e6d2a6d97305fb1be25a/clif/python/instance.h#L233 +// This prevents disowning of the Python object owning the raw pointer member. +template +struct property_cpp_function_sh_raw_ptr_member { + using drp = typename std::remove_pointer::type; + + template = 0> + static cpp_function readonly(PM pm, const handle &hdl) { + type_info *tinfo = get_type_info(typeid(T), /*throw_if_missing=*/true); + if (tinfo->holder_enum_v == holder_enum_t::smart_holder) { + return cpp_function( + [pm](handle c_hdl) -> std::shared_ptr { + std::shared_ptr c_sp + = type_caster>::shared_ptr_with_responsible_parent( + c_hdl); + D ptr = (*c_sp).*pm; + return std::shared_ptr(c_sp, ptr); + }, + is_method(hdl)); + } + return property_cpp_function_classic::readonly(pm, hdl); + } + + template = 0> + static cpp_function read(PM pm, const handle &hdl) { + return readonly(pm, hdl); + } + + template = 0> + static cpp_function write(PM pm, const handle &hdl) { + type_info *tinfo = get_type_info(typeid(T), /*throw_if_missing=*/true); + if (tinfo->holder_enum_v == holder_enum_t::smart_holder) { + return cpp_function([pm](T &c, D value) { c.*pm = std::forward(std::move(value)); }, + is_method(hdl)); + } + return property_cpp_function_classic::write(pm, hdl); + } +}; + +// Specialization for members held by-value, using smart_holder if that is the class_ holder, +// or falling back to the classic implementation if not. +// The read functions return a shared_ptr to the member, emulating the PyCLIF approach: +// https://github.com/google/clif/blob/c371a6d4b28d25d53a16e6d2a6d97305fb1be25a/clif/python/instance.h#L233 +// This prevents disowning of the Python object owning the member. +template +struct property_cpp_function_sh_member_held_by_value { + template = 0> + static cpp_function readonly(PM pm, const handle &hdl) { + type_info *tinfo = get_type_info(typeid(T), /*throw_if_missing=*/true); + if (tinfo->holder_enum_v == holder_enum_t::smart_holder) { + return cpp_function( + [pm](handle c_hdl) -> std::shared_ptr::type> { + std::shared_ptr c_sp + = type_caster>::shared_ptr_with_responsible_parent( + c_hdl); + return std::shared_ptr::type>(c_sp, + &(c_sp.get()->*pm)); + }, + is_method(hdl)); + } + return property_cpp_function_classic::readonly(pm, hdl); + } + + template = 0> + static cpp_function read(PM pm, const handle &hdl) { + type_info *tinfo = get_type_info(typeid(T), /*throw_if_missing=*/true); + if (tinfo->holder_enum_v == holder_enum_t::smart_holder) { + return cpp_function( + [pm](handle c_hdl) -> std::shared_ptr { + std::shared_ptr c_sp + = type_caster>::shared_ptr_with_responsible_parent( + c_hdl); + return std::shared_ptr(c_sp, &(c_sp.get()->*pm)); + }, + is_method(hdl)); + } + return property_cpp_function_classic::read(pm, hdl); + } + + template = 0> + static cpp_function write(PM pm, const handle &hdl) { + type_info *tinfo = get_type_info(typeid(T), /*throw_if_missing=*/true); + if (tinfo->holder_enum_v == holder_enum_t::smart_holder) { + return cpp_function([pm](T &c, const D &value) { c.*pm = value; }, is_method(hdl)); + } + return property_cpp_function_classic::write(pm, hdl); + } +}; + +// Specialization for std::unique_ptr members, using smart_holder if that is the class_ holder, +// or falling back to the classic implementation if not. +// read disowns the member unique_ptr. +// write disowns the passed Python object. +// readonly is disabled (static_assert) because there is no safe & intuitive way to make the member +// accessible as a Python object without disowning the member unique_ptr. A .def_readonly disowning +// the unique_ptr member is deemed highly prone to misunderstandings. +template +struct property_cpp_function_sh_unique_ptr_member { + template = 0> + static cpp_function readonly(PM, const handle &) { + static_assert(!is_instantiation::value, + "def_readonly cannot be used for std::unique_ptr members."); + return cpp_function{}; // Unreachable. + } + + template = 0> + static cpp_function read(PM pm, const handle &hdl) { + type_info *tinfo = get_type_info(typeid(T), /*throw_if_missing=*/true); + if (tinfo->holder_enum_v == holder_enum_t::smart_holder) { + return cpp_function( + [pm](handle c_hdl) -> D { + std::shared_ptr c_sp + = type_caster>::shared_ptr_with_responsible_parent( + c_hdl); + return D{std::move(c_sp.get()->*pm)}; + }, + is_method(hdl)); + } + return property_cpp_function_classic::read(pm, hdl); + } + + template = 0> + static cpp_function write(PM pm, const handle &hdl) { + return cpp_function([pm](T &c, D &&value) { c.*pm = std::move(value); }, is_method(hdl)); + } +}; + +PYBIND11_NAMESPACE_END(detail) + +template +struct property_cpp_function< + T, + D, + detail::enable_if_t, + detail::both_t_and_d_use_type_caster_base>::value>> + : detail::property_cpp_function_sh_raw_ptr_member {}; + +template +struct property_cpp_function, + std::is_array, + detail::is_instantiation, + detail::is_instantiation>, + detail::both_t_and_d_use_type_caster_base>::value>> + : detail::property_cpp_function_sh_member_held_by_value {}; + +template +struct property_cpp_function< + T, + D, + detail::enable_if_t, + detail::both_t_and_d_use_type_caster_base>::value>> + : detail::property_cpp_function_sh_unique_ptr_member {}; + +#ifdef PYBIND11_RUN_TESTING_WITH_SMART_HOLDER_AS_DEFAULT_BUT_NEVER_USE_IN_PRODUCTION_PLEASE +// NOTE: THIS IS MEANT FOR STRESS-TESTING OR TRIAGING ONLY! +// Running the pybind11 unit tests with smart_holder as the default holder is to ensure +// that `py::smart_holder` / `py::classh` is backward-compatible with all pre-existing +// functionality. +// Be careful not to link translation units compiled with different default holders, because +// this will cause ODR violations (https://en.wikipedia.org/wiki/One_Definition_Rule). +template +using default_holder_type = smart_holder; +#else +template +using default_holder_type = std::unique_ptr; +#endif template class class_ : public detail::generic_type { @@ -1508,13 +2289,27 @@ class class_ : public detail::generic_type { using type = type_; using type_alias = detail::exactly_one_t; constexpr static bool has_alias = !std::is_void::value; - using holder_type = detail::exactly_one_t, options...>; + using holder_type = detail::exactly_one_t, options...>; static_assert(detail::all_of...>::value, "Unknown/invalid class_ template parameters provided"); static_assert(!has_alias || std::is_polymorphic::value, - "Cannot use an alias class with a non-polymorphic type"); + "Cannot use an alias class (aka trampoline) with a non-polymorphic type"); + +#ifndef PYBIND11_RUN_TESTING_WITH_SMART_HOLDER_AS_DEFAULT_BUT_NEVER_USE_IN_PRODUCTION_PLEASE + static_assert(!has_alias || !detail::is_smart_holder::value + || std::is_base_of::value, + "Alias class (aka trampoline) must inherit from" + " pybind11::trampoline_self_life_support if used in combination with" + " pybind11::smart_holder"); +#endif + static_assert(!has_alias || detail::is_smart_holder::value + || !std::is_base_of::value, + "pybind11::trampoline_self_life_support is a smart_holder feature, therefore" + " an alias class (aka trampoline) should inherit from" + " pybind11::trampoline_self_life_support only if used in combination with" + " pybind11::smart_holder"); PYBIND11_OBJECT(class_, generic_type, PyType_Check) @@ -1539,8 +2334,16 @@ class class_ : public detail::generic_type { record.type_align = alignof(conditional_t &); record.holder_size = sizeof(holder_type); record.init_instance = init_instance; - record.dealloc = dealloc; - record.default_holder = detail::is_instantiation::value; + + if (detail::is_instantiation::value) { + record.holder_enum_v = detail::holder_enum_t::std_unique_ptr; + } else if (detail::is_instantiation::value) { + record.holder_enum_v = detail::holder_enum_t::std_shared_ptr; + } else if (std::is_same::value) { + record.holder_enum_v = detail::holder_enum_t::smart_holder; + } else { + record.holder_enum_v = detail::holder_enum_t::custom_holder; + } set_operator_new(&record); @@ -1550,14 +2353,41 @@ class class_ : public detail::generic_type { /* Process optional arguments, if any */ process_attributes::init(extra..., &record); + if (record.release_gil_before_calling_cpp_dtor) { + record.dealloc = dealloc_release_gil_before_calling_cpp_dtor; + } else { + record.dealloc = dealloc_without_manipulating_gil; + } + + if (std::is_base_of::value) { + // Store a cross-DSO-safe getter. + // This lambda is defined in the same DSO that instantiates + // class_, but it can be called safely from any other DSO. + record.get_trampoline_self_life_support = [](void *type_ptr) { + return dynamic_raw_ptr_cast_if_possible( + static_cast(type_ptr)); + }; + } + generic_type::initialize(record); if (has_alias) { - auto &instances = record.module_local ? get_local_internals().registered_types_cpp - : get_internals().registered_types_cpp; - instances[std::type_index(typeid(type_alias))] - = instances[std::type_index(typeid(type))]; + with_internals([&](internals &internals) { + auto &local_internals = get_local_internals(); + if (record.module_local) { + local_internals.registered_types_cpp[&typeid(type_alias)] + = local_internals.registered_types_cpp[&typeid(type)]; + } else { + type_info *const val + = internals.registered_types_cpp[std::type_index(typeid(type))]; + internals.registered_types_cpp[std::type_index(typeid(type_alias))] = val; +#if PYBIND11_INTERNALS_VERSION >= 12 + internals.registered_types_cpp_fast[&typeid(type_alias)] = val; +#endif + } + }); } + def("_pybind11_conduit_v1_", cpp_conduit_method); } template ::value, int> = 0> @@ -1565,6 +2395,13 @@ class class_ : public detail::generic_type { rec.add_base(typeid(Base), [](void *src) -> void * { return static_cast(reinterpret_cast(src)); }); + // Virtual inheritance means the base subobject is at a dynamic offset, + // so the reinterpret_cast shortcut in load_impl Case 2a is invalid. + // Force the MI path (implicit_casts) for correct pointer adjustment. + // Detection: static_cast(Base*) is ill-formed for virtual bases. + if PYBIND11_MAYBE_CONSTEXPR (!detail::is_static_downcastable::value) { + rec.multiple_inheritance = true; + } } template ::value, int> = 0> @@ -1666,13 +2503,49 @@ class class_ : public detail::generic_type { return def_buffer([func](const type &obj) { return (obj.*func)(); }); } + // Intentionally no &&/const&& overloads: buffer protocol callbacks are invoked on an + // existing Python object and should not move-from self. + template + class_ &def_buffer(Return (Class::*func)(Args...) &) { + return def_buffer([func](type &obj) { return (obj.*func)(); }); + } + + template + class_ &def_buffer(Return (Class::*func)(Args...) const &) { + return def_buffer([func](const type &obj) { return (obj.*func)(); }); + } + +#ifdef __cpp_noexcept_function_type + template + class_ &def_buffer(Return (Class::*func)(Args...) noexcept) { + return def_buffer([func](type &obj) { return (obj.*func)(); }); + } + + template + class_ &def_buffer(Return (Class::*func)(Args...) const noexcept) { + return def_buffer([func](const type &obj) { return (obj.*func)(); }); + } + + template + class_ &def_buffer(Return (Class::*func)(Args...) & noexcept) { + return def_buffer([func](type &obj) { return (obj.*func)(); }); + } + + template + class_ &def_buffer(Return (Class::*func)(Args...) const & noexcept) { + return def_buffer([func](const type &obj) { return (obj.*func)(); }); + } +#endif + template class_ &def_readwrite(const char *name, D C::*pm, const Extra &...extra) { static_assert(std::is_same::value || std::is_base_of::value, "def_readwrite() requires a class member (or base class member)"); - cpp_function fget([pm](const type &c) -> const D & { return c.*pm; }, is_method(*this)), - fset([pm](type &c, const D &value) { c.*pm = value; }, is_method(*this)); - def_property(name, fget, fset, return_value_policy::reference_internal, extra...); + def_property(name, + property_cpp_function::read(pm, *this), + property_cpp_function::write(pm, *this), + return_value_policy::reference_internal, + extra...); return *this; } @@ -1680,8 +2553,10 @@ class class_ : public detail::generic_type { class_ &def_readonly(const char *name, const D C::*pm, const Extra &...extra) { static_assert(std::is_same::value || std::is_base_of::value, "def_readonly() requires a class member (or base class member)"); - cpp_function fget([pm](const type &c) -> const D & { return c.*pm; }, is_method(*this)); - def_property_readonly(name, fget, return_value_policy::reference_internal, extra...); + def_property_readonly(name, + property_cpp_function::readonly(pm, *this), + return_value_policy::reference_internal, + extra...); return *this; } @@ -1778,24 +2653,52 @@ class class_ : public detail::generic_type { const Extra &...extra) { static_assert(0 == detail::constexpr_sum(std::is_base_of::value...), "Argument annotations are not allowed for properties"); + static_assert(0 == detail::constexpr_sum(detail::is_call_guard::value...), + "def_property family does not currently support call_guard. Use a " + "py::cpp_function instead."); + static_assert(0 == detail::constexpr_sum(detail::is_keep_alive::value...), + "def_property family does not currently support keep_alive. Use a " + "py::cpp_function instead."); auto rec_fget = get_function_record(fget), rec_fset = get_function_record(fset); auto *rec_active = rec_fget; if (rec_fget) { char *doc_prev = rec_fget->doc; /* 'extra' field may include a property-specific documentation string */ + auto args_before = rec_fget->args.size(); detail::process_attributes::init(extra..., rec_fget); if (rec_fget->doc && rec_fget->doc != doc_prev) { std::free(doc_prev); rec_fget->doc = PYBIND11_COMPAT_STRDUP(rec_fget->doc); } + // Args added by process_attributes (e.g. "self" via is_method + pos_only/kw_only) + // need their strings strdup'd: initialize_generic's strdup loop already ran during + // cpp_function construction, so it won't process these late additions. Without this, + // destruct() would call free() on string literals. See gh-5976. + for (auto i = args_before; i < rec_fget->args.size(); ++i) { + if (rec_fget->args[i].name) { + rec_fget->args[i].name = PYBIND11_COMPAT_STRDUP(rec_fget->args[i].name); + } + if (rec_fget->args[i].descr) { + rec_fget->args[i].descr = PYBIND11_COMPAT_STRDUP(rec_fget->args[i].descr); + } + } } if (rec_fset) { char *doc_prev = rec_fset->doc; + auto args_before = rec_fset->args.size(); detail::process_attributes::init(extra..., rec_fset); if (rec_fset->doc && rec_fset->doc != doc_prev) { std::free(doc_prev); rec_fset->doc = PYBIND11_COMPAT_STRDUP(rec_fset->doc); } + for (auto i = args_before; i < rec_fset->args.size(); ++i) { + if (rec_fset->args[i].name) { + rec_fset->args[i].name = PYBIND11_COMPAT_STRDUP(rec_fset->args[i].name); + } + if (rec_fset->args[i].descr) { + rec_fset->args[i].descr = PYBIND11_COMPAT_STRDUP(rec_fset->args[i].descr); + } + } if (!rec_active) { rec_active = rec_fset; } @@ -1828,8 +2731,7 @@ class class_ : public detail::generic_type { static void init_holder_from_existing(const detail::value_and_holder &v_h, const holder_type *holder_ptr, std::true_type /*is_copy_constructible*/) { - new (std::addressof(v_h.holder())) - holder_type(*reinterpret_cast(holder_ptr)); + new (std::addressof(v_h.holder())) holder_type(*holder_ptr); } static void init_holder_from_existing(const detail::value_and_holder &v_h, @@ -1858,6 +2760,8 @@ class class_ : public detail::generic_type { /// instance. Should be called as soon as the `type` value_ptr is set for an instance. Takes /// an optional pointer to an existing holder to use; if not specified and the instance is /// `.owned`, a new holder will be constructed to manage the value pointer. + template ::value, int> = 0> static void init_instance(detail::instance *inst, const void *holder_ptr) { auto v_h = inst->get_value_and_holder(detail::get_type_info(typeid(type))); if (!v_h.instance_registered()) { @@ -1867,15 +2771,73 @@ class class_ : public detail::generic_type { init_holder(inst, v_h, (const holder_type *) holder_ptr, v_h.value_ptr()); } - /// Deallocates an instance; via holder, if constructed; otherwise via operator delete. - static void dealloc(detail::value_and_holder &v_h) { - // We could be deallocating because we are cleaning up after a Python exception. - // If so, the Python error indicator will be set. We need to clear that before - // running the destructor, in case the destructor code calls more Python. - // If we don't, the Python API will exit with an exception, and pybind11 will - // throw error_already_set from the C++ destructor which is forbidden and triggers - // std::terminate(). - error_scope scope; + template + static bool try_initialization_using_shared_from_this(holder_type *, WrappedType *, ...) { + return false; + } + + // Adopting existing approach used by type_caster_base, although it leads to somewhat fuzzy + // ownership semantics: if we detected via shared_from_this that a shared_ptr exists already, + // it is reused, irrespective of the return_value_policy in effect. + // "SomeBaseOfWrappedType" is needed because std::enable_shared_from_this is not necessarily a + // direct base of WrappedType. + template + static bool try_initialization_using_shared_from_this( + holder_type *uninitialized_location, + WrappedType *value_ptr_w_t, + const std::enable_shared_from_this *) { + auto shd_ptr = std::dynamic_pointer_cast( + detail::try_get_shared_from_this(value_ptr_w_t)); + if (!shd_ptr) { + return false; + } + // Note: inst->owned ignored. + new (uninitialized_location) holder_type(holder_type::from_shared_ptr(shd_ptr)); + return true; + } + + template ::value, int> = 0> + static void init_instance(detail::instance *inst, const void *holder_const_void_ptr) { + // Need for const_cast is a consequence of the type_info::init_instance type: + // void (*init_instance)(instance *, const void *); + auto *holder_void_ptr = const_cast(holder_const_void_ptr); + + auto v_h = inst->get_value_and_holder(detail::get_type_info(typeid(type))); + if (!v_h.instance_registered()) { + register_instance(inst, v_h.value_ptr(), v_h.type); + v_h.set_instance_registered(); + } + auto *uninitialized_location = std::addressof(v_h.holder()); + auto *value_ptr_w_t = v_h.value_ptr(); + // Try downcast from `type` to `type_alias`: + inst->is_alias + = detail::dynamic_raw_ptr_cast_if_possible(value_ptr_w_t) != nullptr; + if (holder_void_ptr) { + // Note: inst->owned ignored. + auto *holder_ptr = static_cast(holder_void_ptr); + new (uninitialized_location) holder_type(std::move(*holder_ptr)); + } else if (!try_initialization_using_shared_from_this( + uninitialized_location, value_ptr_w_t, value_ptr_w_t)) { + if (inst->owned) { + new (uninitialized_location) holder_type(holder_type::from_raw_ptr_take_ownership( + value_ptr_w_t, /*void_cast_raw_ptr*/ inst->is_alias)); + } else { + new (uninitialized_location) + holder_type(holder_type::from_raw_ptr_unowned(value_ptr_w_t)); + } + } + v_h.set_holder_constructed(); + } + + // Deallocates an instance; via holder, if constructed; otherwise via operator delete. + // NOTE: The Python error indicator needs to cleared BEFORE this function is called. + // This is because we could be deallocating while cleaning up after a Python exception. + // If the error indicator is not cleared but the C++ destructor code makes Python C API + // calls, those calls are likely to generate a new exception, and pybind11 will then + // throw `error_already_set` from the C++ destructor. This is forbidden and will + // trigger std::terminate(). + static void dealloc_impl(detail::value_and_holder &v_h) { if (v_h.holder_constructed()) { v_h.holder().~holder_type(); v_h.set_holder_constructed(false); @@ -1886,6 +2848,32 @@ class class_ : public detail::generic_type { v_h.value_ptr() = nullptr; } + static void dealloc_without_manipulating_gil(detail::value_and_holder &v_h) { + error_scope scope; + dealloc_impl(v_h); + } + + static void dealloc_release_gil_before_calling_cpp_dtor(detail::value_and_holder &v_h) { + error_scope scope; + // Intentionally not using `gil_scoped_release` because the non-simple + // version unconditionally calls `get_internals()`. + // `Py_BEGIN_ALLOW_THREADS`, `Py_END_ALLOW_THREADS` cannot be used + // because those macros include `{` and `}`. + PyThreadState *py_ts = PyEval_SaveThread(); + try { + dealloc_impl(v_h); + } catch (...) { + // This code path is expected to be unreachable unless there is a + // bug in pybind11 itself. + // An alternative would be to mark this function, or + // `dealloc_impl()`, with `nothrow`, but that would be a subtle + // behavior change and could make debugging more difficult. + PyEval_RestoreThread(py_ts); + throw; + } + PyEval_RestoreThread(py_ts); + } + static detail::function_record *get_function_record(handle h) { h = detail::get_function(h); if (!h) { @@ -1896,17 +2884,15 @@ class class_ : public detail::generic_type { if (!func_self) { throw error_already_set(); } - if (!isinstance(func_self)) { - return nullptr; - } - auto cap = reinterpret_borrow(func_self); - if (!detail::is_function_record_capsule(cap)) { - return nullptr; - } - return cap.get_pointer(); + return detail::function_record_ptr_from_PyObject(func_self.ptr()); } }; +// Supports easier switching between py::class_ and py::class_: +// users can simply replace the `_` in `class_` with `h` or vice versa. +template +using classh = class_; + /// Binds an existing constructor taking arguments Args... template detail::initimpl::constructor init() { @@ -1943,7 +2929,7 @@ detail::initimpl::pickle_factory pickle(GetState &&g, SetSta PYBIND11_NAMESPACE_BEGIN(detail) inline str enum_name(handle arg) { - dict entries = arg.get_type().attr("__entries"); + dict entries = type::handle_of(arg).attr("__entries"); for (auto kv : entries) { if (handle(kv.second[int_(0)]).equal(arg)) { return pybind11::str(kv.first); @@ -1957,8 +2943,9 @@ struct enum_base { PYBIND11_NOINLINE void init(bool is_arithmetic, bool is_convertible) { m_base.attr("__entries") = dict(); - auto property = handle((PyObject *) &PyProperty_Type); - auto static_property = handle((PyObject *) get_internals().static_property_type); + auto property = handle(reinterpret_cast(&PyProperty_Type)); + auto static_property + = handle(reinterpret_cast(get_internals().static_property_type)); m_base.attr("__repr__") = cpp_function( [](const object &arg) -> str { @@ -1968,17 +2955,20 @@ struct enum_base { .format(std::move(type_name), enum_name(arg), int_(arg)); }, name("__repr__"), - is_method(m_base)); + is_method(m_base), + pos_only()); - m_base.attr("name") = property(cpp_function(&enum_name, name("name"), is_method(m_base))); + m_base.attr("name") + = property(cpp_function(&enum_name, name("name"), is_method(m_base), pos_only())); m_base.attr("__str__") = cpp_function( [](handle arg) -> str { object type_name = type::handle_of(arg).attr("__name__"); return pybind11::str("{}.{}").format(std::move(type_name), enum_name(arg)); }, - name("name"), - is_method(m_base)); + name("__str__"), + is_method(m_base), + pos_only()); if (options::show_enum_members_docstring()) { m_base.attr("__doc__") = static_property( @@ -1986,7 +2976,7 @@ struct enum_base { [](handle arg) -> std::string { std::string docstring; dict entries = arg.attr("__entries"); - if (((PyTypeObject *) arg.ptr())->tp_doc) { + if ((reinterpret_cast(arg.ptr()))->tp_doc) { docstring += std::string( reinterpret_cast(arg.ptr())->tp_doc); docstring += "\n\n"; @@ -2033,7 +3023,8 @@ struct enum_base { }, \ name(op), \ is_method(m_base), \ - arg("other")) + arg("other"), \ + pos_only()) #define PYBIND11_ENUM_OP_CONV(op, expr) \ m_base.attr(op) = cpp_function( \ @@ -2043,7 +3034,8 @@ struct enum_base { }, \ name(op), \ is_method(m_base), \ - arg("other")) + arg("other"), \ + pos_only()) #define PYBIND11_ENUM_OP_CONV_LHS(op, expr) \ m_base.attr(op) = cpp_function( \ @@ -2053,7 +3045,8 @@ struct enum_base { }, \ name(op), \ is_method(m_base), \ - arg("other")) + arg("other"), \ + pos_only()) if (is_convertible) { PYBIND11_ENUM_OP_CONV_LHS("__eq__", !b.is_none() && a.equal(b)); @@ -2073,7 +3066,8 @@ struct enum_base { m_base.attr("__invert__") = cpp_function([](const object &arg) { return ~(int_(arg)); }, name("__invert__"), - is_method(m_base)); + is_method(m_base), + pos_only()); } } else { PYBIND11_ENUM_OP_STRICT("__eq__", int_(a).equal(int_(b)), return false); @@ -2093,18 +3087,22 @@ struct enum_base { #undef PYBIND11_ENUM_OP_CONV #undef PYBIND11_ENUM_OP_STRICT - m_base.attr("__getstate__") = cpp_function( - [](const object &arg) { return int_(arg); }, name("__getstate__"), is_method(m_base)); + m_base.attr("__getstate__") = cpp_function([](const object &arg) { return int_(arg); }, + name("__getstate__"), + is_method(m_base), + pos_only()); - m_base.attr("__hash__") = cpp_function( - [](const object &arg) { return int_(arg); }, name("__hash__"), is_method(m_base)); + m_base.attr("__hash__") = cpp_function([](const object &arg) { return int_(arg); }, + name("__hash__"), + is_method(m_base), + pos_only()); } PYBIND11_NOINLINE void value(char const *name_, object value, const char *doc = nullptr) { dict entries = m_base.attr("__entries"); str name(name_); if (entries.contains(name)) { - std::string type_name = (std::string) str(m_base.attr("__name__")); + std::string type_name = std::string(str(m_base.attr("__name__"))); throw value_error(std::move(type_name) + ": element \"" + std::string(name_) + "\" already exists!"); } @@ -2184,14 +3182,22 @@ class enum_ : public class_ { template enum_(const handle &scope, const char *name, const Extra &...extra) : class_(scope, name, extra...), m_base(*this, scope) { + { + if (detail::global_internals_native_enum_type_map_contains( + std::type_index(typeid(Type)))) { + pybind11_fail("pybind11::enum_ \"" + std::string(name) + + "\" is already registered as a pybind11::native_enum!"); + } + } + constexpr bool is_arithmetic = detail::any_of...>::value; constexpr bool is_convertible = std::is_convertible::value; m_base.init(is_arithmetic, is_convertible); def(init([](Scalar i) { return static_cast(i); }), arg("value")); - def_property_readonly("value", [](Type value) { return (Scalar) value; }); - def("__int__", [](Type value) { return (Scalar) value; }); - def("__index__", [](Type value) { return (Scalar) value; }); + def_property_readonly("value", [](Type value) { return (Scalar) value; }, pos_only()); + def("__int__", [](Type value) { return (Scalar) value; }, pos_only()); + def("__index__", [](Type value) { return (Scalar) value; }, pos_only()); attr("__setstate__") = cpp_function( [](detail::value_and_holder &v_h, Scalar arg) { detail::initimpl::setstate( @@ -2200,7 +3206,8 @@ class enum_ : public class_ { detail::is_new_style_constructor(), pybind11::name("__setstate__"), is_method(*this), - arg("state")); + arg("state"), + pos_only()); } /// Export enumeration entries into the parent scope @@ -2271,28 +3278,39 @@ keep_alive_impl(size_t Nurse, size_t Patient, function_call &call, handle ret) { inline std::pair all_type_info_get_cache(PyTypeObject *type) { - auto res = get_internals() - .registered_types_py + auto res = with_internals([type](internals &internals) { + auto ins = internals + .registered_types_py #ifdef __cpp_lib_unordered_map_try_emplace - .try_emplace(type); + .try_emplace(type); #else - .emplace(type, std::vector()); + .emplace(type, std::vector()); #endif + if (ins.second) { + // For free-threading mode, this call must be under + // the with_internals() mutex lock, to avoid that other threads + // continue running with the empty ins.first->second. + all_type_info_populate(type, ins.first->second); + } + return ins; + }); if (res.second) { // New cache entry created; set up a weak reference to automatically remove it if the type // gets destroyed: - weakref((PyObject *) type, cpp_function([type](handle wr) { - get_internals().registered_types_py.erase(type); - - // TODO consolidate the erasure code in pybind11_meta_dealloc() in class.h - auto &cache = get_internals().inactive_override_cache; - for (auto it = cache.begin(), last = cache.end(); it != last;) { - if (it->first == reinterpret_cast(type)) { - it = cache.erase(it); - } else { - ++it; + weakref(reinterpret_cast(type), cpp_function([type](handle wr) { + with_internals([type](internals &internals) { + internals.registered_types_py.erase(type); + + // TODO consolidate the erasure code in pybind11_meta_dealloc() in class.h + auto &cache = internals.inactive_override_cache; + for (auto it = cache.begin(), last = cache.end(); it != last;) { + if (it->first == reinterpret_cast(type)) { + it = cache.erase(it); + } else { + ++it; + } } - } + }); wr.dec_ref(); })) @@ -2369,13 +3387,20 @@ template +// NOLINTNEXTLINE(performance-unnecessary-value-param) iterator make_iterator_impl(Iterator first, Sentinel last, Extra &&...extra) { using state = detail::iterator_state; // TODO: state captures only the types of Extra, not the values + // For Python < 3.14.0rc1, pycritical_section uses direct mutex locking (same as a unique + // lock), which may deadlock during type registration. See detail/internals.h for details. +#if PY_VERSION_HEX >= 0x030E00C1 // 3.14.0rc1 + PYBIND11_LOCK_INTERNALS(get_internals()); +#endif if (!detail::get_type_info(typeid(state), false)) { class_(handle(), "iterator", pybind11::module_local()) - .def("__iter__", [](state &s) -> state & { return s; }) + .def( + "__iter__", [](state &s) -> state & { return s; }, pos_only()) .def( "__next__", [](state &s) -> ValueType { @@ -2392,10 +3417,11 @@ iterator make_iterator_impl(Iterator first, Sentinel last, Extra &&...extra) { // NOLINTNEXTLINE(readability-const-return-type) // PR #3263 }, std::forward(extra)..., + pos_only(), Policy); } - return cast(state{first, last, true}); + return cast(state{std::forward(first), std::forward(last), true}); } PYBIND11_NAMESPACE_END(detail) @@ -2406,13 +3432,16 @@ template ::result_type, typename... Extra> -iterator make_iterator(Iterator first, Sentinel last, Extra &&...extra) { +// NOLINTNEXTLINE(performance-unnecessary-value-param) +typing::Iterator make_iterator(Iterator first, Sentinel last, Extra &&...extra) { return detail::make_iterator_impl, Policy, Iterator, Sentinel, ValueType, - Extra...>(first, last, std::forward(extra)...); + Extra...>(std::forward(first), + std::forward(last), + std::forward(extra)...); } /// Makes a python iterator over the keys (`.first`) of a iterator over pairs from a @@ -2422,13 +3451,15 @@ template ::result_type, typename... Extra> -iterator make_key_iterator(Iterator first, Sentinel last, Extra &&...extra) { +typing::Iterator make_key_iterator(Iterator first, Sentinel last, Extra &&...extra) { return detail::make_iterator_impl, Policy, Iterator, Sentinel, KeyType, - Extra...>(first, last, std::forward(extra)...); + Extra...>(std::forward(first), + std::forward(last), + std::forward(extra)...); } /// Makes a python iterator over the values (`.second`) of a iterator over pairs from a @@ -2438,21 +3469,25 @@ template ::result_type, typename... Extra> -iterator make_value_iterator(Iterator first, Sentinel last, Extra &&...extra) { +typing::Iterator make_value_iterator(Iterator first, Sentinel last, Extra &&...extra) { return detail::make_iterator_impl, Policy, Iterator, Sentinel, ValueType, - Extra...>(first, last, std::forward(extra)...); + Extra...>(std::forward(first), + std::forward(last), + std::forward(extra)...); } /// Makes an iterator over values of an stl container or other container supporting /// `std::begin()`/`std::end()` template ()))>::result_type, typename... Extra> -iterator make_iterator(Type &value, Extra &&...extra) { +typing::Iterator make_iterator(Type &value, Extra &&...extra) { return make_iterator( std::begin(value), std::end(value), std::forward(extra)...); } @@ -2461,8 +3496,10 @@ iterator make_iterator(Type &value, Extra &&...extra) { /// `std::begin()`/`std::end()` template ()))>::result_type, typename... Extra> -iterator make_key_iterator(Type &value, Extra &&...extra) { +typing::Iterator make_key_iterator(Type &value, Extra &&...extra) { return make_key_iterator( std::begin(value), std::end(value), std::forward(extra)...); } @@ -2471,8 +3508,10 @@ iterator make_key_iterator(Type &value, Extra &&...extra) { /// `std::begin()`/`std::end()` template ()))>::result_type, typename... Extra> -iterator make_value_iterator(Type &value, Extra &&...extra) { +typing::Iterator make_value_iterator(Type &value, Extra &&...extra) { return make_value_iterator( std::begin(value), std::end(value), std::forward(extra)...); } @@ -2483,9 +3522,15 @@ void implicitly_convertible() { bool &flag; explicit set_flag(bool &flag_) : flag(flag_) { flag_ = true; } ~set_flag() { flag = false; } + + // Prevent copying/moving to ensure RAII guard is used safely + set_flag(const set_flag &) = delete; + set_flag(set_flag &&) = delete; + set_flag &operator=(const set_flag &) = delete; + set_flag &operator=(set_flag &&) = delete; }; auto implicit_caster = [](PyObject *obj, PyTypeObject *type) -> PyObject * { - static bool currently_used = false; + thread_local bool currently_used = false; if (currently_used) { // implicit conversions are non-reentrant return nullptr; } @@ -2495,7 +3540,7 @@ void implicitly_convertible() { } tuple args(1); args[0] = obj; - PyObject *result = PyObject_Call((PyObject *) type, args.ptr(), nullptr); + PyObject *result = PyObject_Call(reinterpret_cast(type), args.ptr(), nullptr); if (result == nullptr) { PyErr_Clear(); } @@ -2510,8 +3555,12 @@ void implicitly_convertible() { } inline void register_exception_translator(ExceptionTranslator &&translator) { - detail::get_internals().registered_exception_translators.push_front( - std::forward(translator)); + detail::with_exception_translators( + [&](std::forward_list &exception_translators, + std::forward_list &local_exception_translators) { + (void) local_exception_translators; + exception_translators.push_front(std::forward(translator)); + }); } /** @@ -2521,14 +3570,18 @@ inline void register_exception_translator(ExceptionTranslator &&translator) { * the exception. */ inline void register_local_exception_translator(ExceptionTranslator &&translator) { - detail::get_local_internals().registered_exception_translators.push_front( - std::forward(translator)); + detail::with_exception_translators( + [&](std::forward_list &exception_translators, + std::forward_list &local_exception_translators) { + (void) exception_translators; + local_exception_translators.push_front(std::forward(translator)); + }); } /** * Wrapper to generate a new Python exception type. * - * This should only be used with PyErr_SetString for now. + * This should only be used with py::set_error() for now. * It is not (yet) possible to use as a py::base. * Template type argument is reserved for future use. */ @@ -2549,27 +3602,25 @@ class exception : public object { } // Sets the current python exception to this exception object with the given message - void operator()(const char *message) { PyErr_SetString(m_ptr, message); } + PYBIND11_DEPRECATED("Please use py::set_error() instead " + "(https://github.com/pybind/pybind11/pull/4772)") + void operator()(const char *message) const { set_error(*this, message); } }; PYBIND11_NAMESPACE_BEGIN(detail) -// Returns a reference to a function-local static exception object used in the simple -// register_exception approach below. (It would be simpler to have the static local variable -// directly in register_exception, but that makes clang <3.5 segfault - issue #1349). -template -exception &get_exception_object() { - static exception ex; - return ex; -} + +template <> +struct handle_type_name> { + static constexpr auto name = const_name("Exception"); +}; // Helper function for register_exception and register_local_exception template exception & register_exception_impl(handle scope, const char *name, handle base, bool isLocal) { - auto &ex = detail::get_exception_object(); - if (!ex) { - ex = exception(scope, name, base); - } + PYBIND11_CONSTINIT static gil_safe_call_once_and_store> exc_storage; + exc_storage.call_once_and_store_result( + [&]() { return exception(scope, name, base); }); auto register_func = isLocal ? ®ister_local_exception_translator : ®ister_exception_translator; @@ -2581,10 +3632,10 @@ register_exception_impl(handle scope, const char *name, handle base, bool isLoca try { std::rethrow_exception(p); } catch (const CppException &e) { - detail::get_exception_object()(e.what()); + set_error(exc_storage.get_stored(), e.what()); } }); - return ex; + return exc_storage.get_stored(); } PYBIND11_NAMESPACE_END(detail) @@ -2681,32 +3732,47 @@ get_type_override(const void *this_ptr, const type_info *this_type, const char * /* Cache functions that aren't overridden in Python to avoid many costly Python dictionary lookups below */ - auto &cache = get_internals().inactive_override_cache; - if (cache.find(key) != cache.end()) { + bool not_overridden = with_internals([&key](internals &internals) { + auto &cache = internals.inactive_override_cache; + return cache.find(key) != cache.end(); + }); + if (not_overridden) { return function(); } function override = getattr(self, name, function()); if (override.is_cpp_function()) { - cache.insert(std::move(key)); + with_internals([&](internals &internals) { + internals.inactive_override_cache.insert(std::move(key)); + }); return function(); } /* Don't call dispatch code if invoked from overridden function. - Unfortunately this doesn't work on PyPy. */ -#if !defined(PYPY_VERSION) + Unfortunately this doesn't work on PyPy and GraalPy. */ +#if !defined(PYPY_VERSION) && !defined(GRAALVM_PYTHON) # if PY_VERSION_HEX >= 0x03090000 PyFrameObject *frame = PyThreadState_GetFrame(PyThreadState_Get()); if (frame != nullptr) { PyCodeObject *f_code = PyFrame_GetCode(frame); // f_code is guaranteed to not be NULL - if ((std::string) str(f_code->co_name) == name && f_code->co_argcount > 0) { + if (std::string(str(f_code->co_name)) == name && f_code->co_argcount > 0) { +# if PY_VERSION_HEX >= 0x030d0000 + PyObject *locals = PyEval_GetFrameLocals(); +# else PyObject *locals = PyEval_GetLocals(); + Py_XINCREF(locals); +# endif if (locals != nullptr) { +# if PY_VERSION_HEX >= 0x030b0000 + PyObject *co_varnames = PyCode_GetVarnames(f_code); +# else PyObject *co_varnames = PyObject_GetAttrString((PyObject *) f_code, "co_varnames"); +# endif PyObject *self_arg = PyTuple_GET_ITEM(co_varnames, 0); Py_DECREF(co_varnames); PyObject *self_caller = dict_getitem(locals, self_arg); + Py_DECREF(locals); if (self_caller == self.ptr()) { Py_DECREF(f_code); Py_DECREF(frame); @@ -2783,11 +3849,17 @@ function get_override(const T *this_ptr, const char *name) { = pybind11::get_override(static_cast(this), name); \ if (override) { \ auto o = override(__VA_ARGS__); \ - if (pybind11::detail::cast_is_temporary_value_reference::value) { \ + PYBIND11_WARNING_PUSH \ + PYBIND11_WARNING_DISABLE_MSVC(4127) \ + if PYBIND11_MAYBE_CONSTEXPR ( \ + pybind11::detail::cast_is_temporary_value_reference::value \ + && !pybind11::detail::is_same_ignoring_cvref::value) { \ static pybind11::detail::override_caster_t caster; \ return pybind11::detail::cast_ref(std::move(o), caster); \ + } else { \ + return pybind11::detail::cast_safe(std::move(o)); \ } \ - return pybind11::detail::cast_safe(std::move(o)); \ + PYBIND11_WARNING_POP \ } \ } while (false) diff --git a/external_libraries/pybind11/include/pybind11/pytypes.h b/external_libraries/pybind11/include/pybind11/pytypes.h index 64aad634..ff4be6a5 100644 --- a/external_libraries/pybind11/include/pybind11/pytypes.h +++ b/external_libraries/pybind11/include/pybind11/pytypes.h @@ -48,6 +48,9 @@ PYBIND11_NAMESPACE_BEGIN(detail) class args_proxy; bool isinstance_generic(handle obj, const std::type_info &tp); +template +bool isinstance_native_enum(handle obj, const std::type_info &tp); + // Accessor forward declarations template class accessor; @@ -59,6 +62,7 @@ struct sequence_item; struct list_item; struct tuple_item; } // namespace accessor_policies +// PLEASE KEEP handle_type_name SPECIALIZATIONS IN SYNC. using obj_attr_accessor = accessor; using str_attr_accessor = accessor; using item_accessor = accessor; @@ -77,7 +81,9 @@ using is_pyobject = std::is_base_of>; \endrst */ template class object_api : public pyobject_tag { + object_api() = default; const Derived &derived() const { return static_cast(*this); } + friend Derived; public: /** \rst @@ -112,6 +118,17 @@ class object_api : public pyobject_tag { /// See above (the only difference is that the key is provided as a string literal) str_attr_accessor attr(const char *key) const; + /** \rst + Similar to the above attr functions with the difference that the templated Type + is used to set the `__annotations__` dict value to the corresponding key. Worth noting + that attr_with_type_hint is implemented in cast.h. + \endrst */ + template + obj_attr_accessor attr_with_type_hint(handle key) const; + /// See above (the only difference is that the key is provided as a string literal) + template + str_attr_accessor attr_with_type_hint(const char *key) const; + /** \rst Matches * unpacking in Python, e.g. to unpack arguments out of a ``tuple`` or ``list`` for a function call. Applying another * to the result yields @@ -181,11 +198,21 @@ class object_api : public pyobject_tag { /// Get or set the object's docstring, i.e. ``obj.__doc__``. str_attr_accessor doc() const; + /// Get or set the object's annotations, i.e. ``obj.__annotations__``. + object annotations() const; + /// Return the object's current reference count - int ref_count() const { return static_cast(Py_REFCNT(derived().ptr())); } + ssize_t ref_count() const { +#ifdef PYPY_VERSION + // PyPy uses the top few bits for REFCNT_FROM_PYPY & REFCNT_FROM_PYPY_LIGHT + // Following pybind11 2.12.1 and older behavior and removing this part + return static_cast(static_cast(Py_REFCNT(derived().ptr()))); +#else + return Py_REFCNT(derived().ptr()); +#endif + } - // TODO PYBIND11_DEPRECATED( - // "Call py::type::handle_of(h) or py::type::of(h) instead of h.get_type()") + PYBIND11_DEPRECATED("Call py::type::handle_of(h) or py::type::of(h) instead of h.get_type()") handle get_type() const; private: @@ -232,8 +259,7 @@ class handle : public detail::object_api { detail::enable_if_t, detail::is_pyobj_ptr_or_nullptr_t>, std::is_convertible>::value, - int> - = 0> + int> = 0> // NOLINTNEXTLINE(google-explicit-constructor) handle(T &obj) : m_ptr(obj) {} @@ -251,7 +277,7 @@ class handle : public detail::object_api { inc_ref_counter(1); #endif #ifdef PYBIND11_ASSERT_GIL_HELD_INCREF_DECREF - if (m_ptr != nullptr && !PyGILState_Check()) { + if (m_ptr != nullptr && PyGILState_Check() == 0) { throw_gilstate_error("pybind11::handle::inc_ref()"); } #endif @@ -266,7 +292,7 @@ class handle : public detail::object_api { \endrst */ const handle &dec_ref() const & { #ifdef PYBIND11_ASSERT_GIL_HELD_INCREF_DECREF - if (m_ptr != nullptr && !PyGILState_Check()) { + if (m_ptr != nullptr && PyGILState_Check() == 0) { throw_gilstate_error("pybind11::handle::dec_ref()"); } #endif @@ -305,19 +331,19 @@ class handle : public detail::object_api { "https://pybind11.readthedocs.io/en/stable/advanced/" "misc.html#common-sources-of-global-interpreter-lock-errors for debugging advice.\n" "If you are convinced there is no bug in your code, you can #define " - "PYBIND11_NO_ASSERT_GIL_HELD_INCREF_DECREF" + "PYBIND11_NO_ASSERT_GIL_HELD_INCREF_DECREF " "to disable this check. In that case you have to ensure this #define is consistently " "used for all translation units linked into a given pybind11 extension, otherwise " "there will be ODR violations.", function_name.c_str()); - fflush(stderr); if (Py_TYPE(m_ptr)->tp_name != nullptr) { fprintf(stderr, - "The failing %s call was triggered on a %s object.\n", + " The failing %s call was triggered on a %s object.", function_name.c_str(), Py_TYPE(m_ptr)->tp_name); - fflush(stderr); } + fprintf(stderr, "\n"); + fflush(stderr); throw std::runtime_error(function_name + " PyGILState_Check() failure."); } #endif @@ -334,6 +360,14 @@ class handle : public detail::object_api { #endif }; +inline void set_error(const handle &type, const char *message) { + PyErr_SetString(type.ptr(), message); +} + +inline void set_error(const handle &type, const handle &value) { + PyErr_SetObject(type.ptr(), value.ptr()); +} + /** \rst Holds a reference to a Python object (with reference counting) @@ -512,8 +546,13 @@ struct error_fetch_and_normalize { // The presence of __notes__ is likely due to exception normalization // errors, although that is not necessarily true, therefore insert a // hint only: - if (PyObject_HasAttrString(m_value.ptr(), "__notes__")) { + const int has_notes = PyObject_HasAttrString(m_value.ptr(), "__notes__"); + if (has_notes == 1) { m_lazy_error_string += "[WITH __notes__]"; + } else if (has_notes == -1) { + // Ignore secondary errors when probing for __notes__ to avoid leaking a + // spurious exception while still reporting the original error. + PyErr_Clear(); } #else // PyErr_NormalizeException() may change the exception type if there are cascading @@ -530,12 +569,6 @@ struct error_fetch_and_normalize { + " failed to obtain the name " "of the normalized active exception type."); } -# if defined(PYPY_VERSION_NUM) && PYPY_VERSION_NUM < 0x07030a00 - // This behavior runs the risk of masking errors in the error handling, but avoids a - // conflict with PyPy, which relies on the normalization here to change OSError to - // FileNotFoundError (https://github.com/pybind/pybind11/issues/4075). - m_lazy_error_string = exc_type_name_norm; -# else if (exc_type_name_norm != m_lazy_error_string) { std::string msg = std::string(called) + ": MISMATCH of original and normalized " @@ -547,7 +580,6 @@ struct error_fetch_and_normalize { msg += ": " + format_value_and_trace(); pybind11_fail(msg); } -# endif #endif } @@ -626,7 +658,7 @@ struct error_fetch_and_normalize { bool have_trace = false; if (m_trace) { -#if !defined(PYPY_VERSION) +#if !defined(PYPY_VERSION) && !defined(GRAALVM_PYTHON) auto *tb = reinterpret_cast(m_trace.ptr()); // Get the deepest trace possible. @@ -828,11 +860,12 @@ bool isinstance(handle obj) { template ::value, int> = 0> bool isinstance(handle obj) { - return detail::isinstance_generic(obj, typeid(T)); + return detail::isinstance_native_enum(obj, typeid(T)) + || detail::isinstance_generic(obj, typeid(T)); } template <> -inline bool isinstance(handle) = delete; +bool isinstance(handle) = delete; template <> inline bool isinstance(handle obj) { return obj.ptr() != nullptr; @@ -963,6 +996,65 @@ inline PyObject *dict_getitem(PyObject *v, PyObject *key) { return rv; } +// PyDict_GetItemStringRef was added in Python 3.13.0a1. +// See also: https://github.com/python/pythoncapi-compat/blob/main/pythoncapi_compat.h +inline PyObject *dict_getitemstringref(PyObject *v, const char *key) { +#if PY_VERSION_HEX >= 0x030D00A1 + PyObject *rv = nullptr; + if (PyDict_GetItemStringRef(v, key, &rv) < 0) { + throw error_already_set(); + } + return rv; +#else + PyObject *rv = dict_getitemstring(v, key); + if (rv == nullptr && PyErr_Occurred()) { + throw error_already_set(); + } + Py_XINCREF(rv); + return rv; +#endif +} + +inline PyObject *dict_setdefaultstring(PyObject *v, const char *key, PyObject *defaultobj) { + PyObject *kv = PyUnicode_FromString(key); + if (kv == nullptr) { + throw error_already_set(); + } + + PyObject *rv = PyDict_SetDefault(v, kv, defaultobj); + Py_DECREF(kv); + if (rv == nullptr) { + throw error_already_set(); + } + return rv; +} + +// PyDict_SetDefaultRef was added in Python 3.13.0a4. +// See also: https://github.com/python/pythoncapi-compat/blob/main/pythoncapi_compat.h +inline PyObject *dict_setdefaultstringref(PyObject *v, const char *key, PyObject *defaultobj) { +#if PY_VERSION_HEX >= 0x030D00A4 + PyObject *kv = PyUnicode_FromString(key); + if (kv == nullptr) { + throw error_already_set(); + } + + PyObject *rv = nullptr; + if (PyDict_SetDefaultRef(v, kv, defaultobj, &rv) < 0) { + Py_DECREF(kv); + throw error_already_set(); + } + Py_DECREF(kv); + return rv; +#else + PyObject *rv = dict_setdefaultstring(v, key, defaultobj); + if (rv == nullptr || PyErr_Occurred()) { + throw error_already_set(); + } + Py_XINCREF(rv); + return rv; +#endif +} + // Helper aliases/functions to support implicit casting of values given to python // accessors/methods. When given a pyobject, this simply returns the pyobject as-is; for other C++ // type, the value goes through pybind11::cast(obj) to convert it to an `object`. @@ -993,11 +1085,11 @@ class accessor : public object_api> { void operator=(const accessor &a) & { operator=(handle(a)); } template - void operator=(T &&value) && { + enable_if_t>::value> operator=(T &&value) && { Policy::set(obj, key, object_or_cast(std::forward(value))); } template - void operator=(T &&value) & { + enable_if_t>::value> operator=(T &&value) & { get_cache() = ensure_object(object_or_cast(std::forward(value))); } @@ -1225,6 +1317,7 @@ class sequence_fast_readonly { using pointer = arrow_proxy; sequence_fast_readonly(handle obj, ssize_t n) : ptr(PySequence_Fast_ITEMS(obj.ptr()) + n) {} + sequence_fast_readonly() = default; // NOLINTNEXTLINE(readability-const-return-type) // PR #3263 reference dereference() const { return *ptr; } @@ -1247,6 +1340,7 @@ class sequence_slow_readwrite { using pointer = arrow_proxy; sequence_slow_readwrite(handle obj, ssize_t index) : obj(obj), index(index) {} + sequence_slow_readwrite() = default; reference dereference() const { return {obj, static_cast(index)}; } void increment() { ++index; } @@ -1320,7 +1414,7 @@ inline bool PyUnicode_Check_Permissive(PyObject *o) { # define PYBIND11_STR_CHECK_FUN PyUnicode_Check #endif -inline bool PyStaticMethod_Check(PyObject *o) { return o->ob_type == &PyStaticMethod_Type; } +inline bool PyStaticMethod_Check(PyObject *o) { return Py_TYPE(o) == &PyStaticMethod_Type; } class kwargs_proxy : public handle { public: @@ -1351,6 +1445,18 @@ class simple_collector; template class unpacking_collector; +inline object get_scope_module(handle scope) { + if (scope) { + if (hasattr(scope, "__module__")) { + return scope.attr("__module__"); + } + if (hasattr(scope, "__name__")) { + return scope.attr("__name__"); + } + } + return object(); +} + PYBIND11_NAMESPACE_END(detail) // TODO: After the deprecated constructors are removed, this macro can be simplified by @@ -1434,11 +1540,17 @@ class iterator : public object { PYBIND11_OBJECT_DEFAULT(iterator, object, PyIter_Check) iterator &operator++() { + init(); advance(); return *this; } iterator operator++(int) { + // Note: We must call init() first so that rv.value is + // the same as this->value just before calling advance(). + // Otherwise, dereferencing the returned iterator may call + // advance() again and return the 3rd item instead of the 1st. + init(); auto rv = *this; advance(); return rv; @@ -1446,15 +1558,12 @@ class iterator : public object { // NOLINTNEXTLINE(readability-const-return-type) // PR #3263 reference operator*() const { - if (m_ptr && !value.ptr()) { - auto &self = const_cast(*this); - self.advance(); - } + init(); return value; } pointer operator->() const { - operator*(); + init(); return &value; } @@ -1477,6 +1586,13 @@ class iterator : public object { friend bool operator!=(const iterator &a, const iterator &b) { return a->ptr() != b->ptr(); } private: + void init() const { + if (m_ptr && !value.ptr()) { + auto &self = const_cast(*this); + self.advance(); + } + } + void advance() { value = reinterpret_steal(PyIter_Next(m_ptr)); if (value.ptr() == nullptr && PyErr_Occurred()) { @@ -1485,7 +1601,7 @@ class iterator : public object { } private: - object value = {}; + object value; }; class type : public object { @@ -1493,7 +1609,9 @@ class type : public object { PYBIND11_OBJECT(type, object, PyType_Check) /// Return a type handle from a handle or an object - static handle handle_of(handle h) { return handle((PyObject *) Py_TYPE(h.ptr())); } + static handle handle_of(handle h) { + return handle(reinterpret_cast(Py_TYPE(h.ptr()))); + } /// Return a type object from a handle or an object static type of(handle h) { return type(type::handle_of(h), borrowed_t{}); } @@ -1571,7 +1689,12 @@ class str : public object { Return a string representation of the object. This is analogous to the ``str()`` function in Python. \endrst */ - explicit str(handle h) : object(raw_str(h.ptr()), stolen_t{}) { + // Templatized to avoid ambiguity with str(const object&) for object-derived types. + template >::value + && std::is_constructible::value, + int> = 0> + explicit str(T &&h) : object(raw_str(handle(std::forward(h)).ptr()), stolen_t{}) { if (!m_ptr) { throw error_already_set(); } @@ -1591,7 +1714,7 @@ class str : public object { if (PyBytes_AsStringAndSize(temp.ptr(), &buffer, &length) != 0) { throw error_already_set(); } - return std::string(buffer, (size_t) length); + return std::string(buffer, static_cast(length)); } template @@ -1612,7 +1735,15 @@ inline namespace literals { /** \rst String literal version of `str` \endrst */ -inline str operator"" _s(const char *s, size_t size) { return {s, size}; } +inline str +#if !defined(__clang__) && defined(__GNUC__) && __GNUC__ < 5 +operator"" _s // gcc 4.8.5 insists on having a space (hard error). +#else +operator""_s // clang 17 generates a deprecation warning if there is a space. +#endif + (const char *s, size_t size) { + return {s, size}; +} } // namespace literals /// \addtogroup pytypes @@ -1783,10 +1914,12 @@ template Unsigned as_unsigned(PyObject *o) { if (sizeof(Unsigned) <= sizeof(unsigned long)) { unsigned long v = PyLong_AsUnsignedLong(o); - return v == (unsigned long) -1 && PyErr_Occurred() ? (Unsigned) -1 : (Unsigned) v; + return v == static_cast(-1) && PyErr_Occurred() ? (Unsigned) -1 + : (Unsigned) v; } unsigned long long v = PyLong_AsUnsignedLongLong(o); - return v == (unsigned long long) -1 && PyErr_Occurred() ? (Unsigned) -1 : (Unsigned) v; + return v == static_cast(-1) && PyErr_Occurred() ? (Unsigned) -1 + : (Unsigned) v; } PYBIND11_NAMESPACE_END(detail) @@ -1830,21 +1963,21 @@ class float_ : public object { PYBIND11_OBJECT_CVT(float_, object, PyFloat_Check, PyNumber_Float) // Allow implicit conversion from float/double: // NOLINTNEXTLINE(google-explicit-constructor) - float_(float value) : object(PyFloat_FromDouble((double) value), stolen_t{}) { + float_(float value) : object(PyFloat_FromDouble(static_cast(value)), stolen_t{}) { if (!m_ptr) { pybind11_fail("Could not allocate float object!"); } } // NOLINTNEXTLINE(google-explicit-constructor) - float_(double value = .0) : object(PyFloat_FromDouble((double) value), stolen_t{}) { + float_(double value = .0) : object(PyFloat_FromDouble(value), stolen_t{}) { if (!m_ptr) { pybind11_fail("Could not allocate float object!"); } } // NOLINTNEXTLINE(google-explicit-constructor) - operator float() const { return (float) PyFloat_AsDouble(m_ptr); } + operator float() const { return static_cast(PyFloat_AsDouble(m_ptr)); } // NOLINTNEXTLINE(google-explicit-constructor) - operator double() const { return (double) PyFloat_AsDouble(m_ptr); } + operator double() const { return PyFloat_AsDouble(m_ptr); } }; class weakref : public object { @@ -1866,13 +1999,14 @@ class weakref : public object { class slice : public object { public: - PYBIND11_OBJECT_DEFAULT(slice, object, PySlice_Check) + PYBIND11_OBJECT(slice, object, PySlice_Check) slice(handle start, handle stop, handle step) : object(PySlice_New(start.ptr(), stop.ptr(), step.ptr()), stolen_t{}) { if (!m_ptr) { pybind11_fail("Could not allocate slice object!"); } } + slice() : slice(none(), none(), none()) {} #ifdef PYBIND11_HAS_OPTIONAL slice(std::optional start, std::optional stop, std::optional step) @@ -2043,7 +2177,7 @@ class tuple : public object { pybind11_fail("Could not allocate tuple object!"); } } - size_t size() const { return (size_t) PyTuple_Size(m_ptr); } + size_t size() const { return static_cast(PyTuple_Size(m_ptr)); } bool empty() const { return size() == 0; } detail::tuple_accessor operator[](size_t index) const { return {*this, index}; } template ::value, int> = 0> @@ -2077,7 +2211,7 @@ class dict : public object { typename collector = detail::deferred_t, Args...>> explicit dict(Args &&...args) : dict(collector(std::forward(args)...).kwargs()) {} - size_t size() const { return (size_t) PyDict_Size(m_ptr); } + size_t size() const { return static_cast(PyDict_Size(m_ptr)); } bool empty() const { return size() == 0; } detail::dict_iterator begin() const { return {*this, 0}; } detail::dict_iterator end() const { return {}; } @@ -2097,7 +2231,8 @@ class dict : public object { if (PyDict_Check(op)) { return handle(op).inc_ref().ptr(); } - return PyObject_CallFunctionObjArgs((PyObject *) &PyDict_Type, op, nullptr); + return PyObject_CallFunctionObjArgs( + reinterpret_cast(&PyDict_Type), op, nullptr); } }; @@ -2109,7 +2244,7 @@ class sequence : public object { if (result == -1) { throw error_already_set(); } - return (size_t) result; + return static_cast(result); } bool empty() const { return size() == 0; } detail::sequence_accessor operator[](size_t index) const { return {*this, index}; } @@ -2132,7 +2267,7 @@ class list : public object { pybind11_fail("Could not allocate list object!"); } } - size_t size() const { return (size_t) PyList_Size(m_ptr); } + size_t size() const { return static_cast(PyList_Size(m_ptr)); } bool empty() const { return size() == 0; } detail::list_accessor operator[](size_t index) const { return {*this, index}; } template ::value, int> = 0> @@ -2158,6 +2293,11 @@ class list : public object { throw error_already_set(); } } + void clear() /* py-non-const */ { + if (PyList_SetSlice(m_ptr, 0, PyList_Size(m_ptr), nullptr) == -1) { + throw error_already_set(); + } + } }; class args : public tuple { @@ -2167,6 +2307,18 @@ class kwargs : public dict { PYBIND11_OBJECT_DEFAULT(kwargs, dict, PyDict_Check) }; +// Subclasses of args and kwargs to support type hinting +// as defined in PEP 484. See #5357 for more info. +template +class Args : public args { + using args::args; +}; + +template +class KWArgs : public kwargs { + using kwargs::kwargs; +}; + class anyset : public object { public: PYBIND11_OBJECT(anyset, object, PyAnySet_Check) @@ -2401,7 +2553,7 @@ inline size_t len(handle h) { if (result < 0) { throw error_already_set(); } - return (size_t) result; + return static_cast(result); } /// Get the length hint of a Python object. @@ -2414,7 +2566,7 @@ inline size_t len_hint(handle h) { PyErr_Clear(); return 0; } - return (size_t) result; + return static_cast(result); } inline str repr(handle h) { @@ -2487,6 +2639,20 @@ str_attr_accessor object_api::doc() const { return attr("__doc__"); } +template +object object_api::annotations() const { +// This is needed again because of the lazy annotations added in 3.14+ +#if PY_VERSION_HEX < 0x030A0000 || PY_VERSION_HEX >= 0x030E0000 + // https://docs.python.org/3/howto/annotations.html#accessing-the-annotations-dict-of-an-object-in-python-3-9-and-older + if (!hasattr(derived(), "__annotations__")) { + setattr(derived(), "__annotations__", dict()); + } + return attr("__annotations__"); +#else + return getattr(derived(), "__annotations__", dict()); +#endif +} + template handle object_api::get_type() const { return type::handle_of(derived()); @@ -2553,5 +2719,18 @@ PYBIND11_MATH_OPERATOR_BINARY_INPLACE(operator>>=, PyNumber_InPlaceRshift) #undef PYBIND11_MATH_OPERATOR_BINARY #undef PYBIND11_MATH_OPERATOR_BINARY_INPLACE +// Meant to return a Python str, but this is not checked. +inline object get_module_name_if_available(handle scope) { + if (scope) { + if (hasattr(scope, "__module__")) { + return scope.attr("__module__"); + } + if (hasattr(scope, "__name__")) { + return scope.attr("__name__"); + } + } + return object(); +} + PYBIND11_NAMESPACE_END(detail) PYBIND11_NAMESPACE_END(PYBIND11_NAMESPACE) diff --git a/external_libraries/pybind11/include/pybind11/stl.h b/external_libraries/pybind11/include/pybind11/stl.h index f39f44f7..01be0b47 100644 --- a/external_libraries/pybind11/include/pybind11/stl.h +++ b/external_libraries/pybind11/include/pybind11/stl.h @@ -11,10 +11,14 @@ #include "pybind11.h" #include "detail/common.h" +#include "detail/descr.h" +#include "detail/type_caster_base.h" #include +#include #include #include +#include #include #include #include @@ -35,6 +39,103 @@ PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE) PYBIND11_NAMESPACE_BEGIN(detail) +// +// Begin: Equivalent of +// https://github.com/google/clif/blob/ae4eee1de07cdf115c0c9bf9fec9ff28efce6f6c/clif/python/runtime.cc#L388-L438 +/* +The three `object_is_convertible_to_*()` functions below are +the result of converging the behaviors of pybind11 and PyCLIF +(http://github.com/google/clif). + +Originally PyCLIF was extremely far on the permissive side of the spectrum, +while pybind11 was very far on the strict side. Originally PyCLIF accepted any +Python iterable as input for a C++ `vector`/`set`/`map` argument, as long as +the elements were convertible. The obvious (in hindsight) problem was that +any empty Python iterable could be passed to any of these C++ types, e.g. `{}` +was accepted for C++ `vector`/`set` arguments, or `[]` for C++ `map` arguments. + +The functions below strike a practical permissive-vs-strict compromise, +informed by tens of thousands of use cases in the wild. A main objective is +to prevent accidents and improve readability: + +- Python literals must match the C++ types. + +- For C++ `set`: The potentially reducing conversion from a Python sequence + (e.g. Python `list` or `tuple`) to a C++ `set` must be explicit, by going + through a Python `set`. + +- However, a Python `set` can still be passed to a C++ `vector`. The rationale + is that this conversion is not reducing. Implicit conversions of this kind + are also fairly commonly used, therefore enforcing explicit conversions + would have an unfavorable cost : benefit ratio; more sloppily speaking, + such an enforcement would be more annoying than helpful. + +Additional checks have been added to allow types derived from `collections.abc.Set` and +`collections.abc.Mapping` (`collections.abc.Sequence` is already allowed by `PySequence_Check`). +*/ + +inline bool object_is_instance_with_one_of_tp_names(PyObject *obj, + std::initializer_list tp_names) { + if (PyType_Check(obj)) { + return false; + } + const char *obj_tp_name = Py_TYPE(obj)->tp_name; + for (const auto *tp_name : tp_names) { + if (std::strcmp(obj_tp_name, tp_name) == 0) { + return true; + } + } + return false; +} + +inline bool object_is_convertible_to_std_vector(const handle &src) { + // Allow sequence-like objects, but not (byte-)string-like objects. + if (PySequence_Check(src.ptr()) != 0) { + return !PyUnicode_Check(src.ptr()) && !PyBytes_Check(src.ptr()); + } + // Allow generators, set/frozenset and several common iterable types. + return (PyGen_Check(src.ptr()) != 0) || (PyAnySet_Check(src.ptr()) != 0) + || object_is_instance_with_one_of_tp_names( + src.ptr(), {"dict_keys", "dict_values", "dict_items", "map", "zip"}); +} + +inline bool object_is_convertible_to_std_set(const handle &src, bool convert) { + // Allow set/frozenset and dict keys. + // In convert mode: also allow types derived from collections.abc.Set. + return ((PyAnySet_Check(src.ptr()) != 0) + || object_is_instance_with_one_of_tp_names(src.ptr(), {"dict_keys"})) + || (convert && isinstance(src, module_::import("collections.abc").attr("Set"))); +} + +inline bool object_is_convertible_to_std_map(const handle &src, bool convert) { + // Allow dict. + if (PyDict_Check(src.ptr())) { + return true; + } + // Allow types conforming to Mapping Protocol. + // According to https://docs.python.org/3/c-api/mapping.html, `PyMappingCheck()` checks for + // `__getitem__()` without checking the type of keys. In order to restrict the allowed types + // closer to actual Mapping-like types, we also check for the `items()` method. + if (PyMapping_Check(src.ptr()) != 0) { + PyObject *items = PyObject_GetAttrString(src.ptr(), "items"); + if (items != nullptr) { + bool is_convertible = (PyCallable_Check(items) != 0); + Py_DECREF(items); + if (is_convertible) { + return true; + } + } else { + PyErr_Clear(); + } + } + // In convert mode: Allow types derived from collections.abc.Mapping + return convert && isinstance(src, module_::import("collections.abc").attr("Mapping")); +} + +// +// End: Equivalent of clif/python/runtime.cc +// + /// Extracts an const lvalue reference or rvalue reference for U based on the type of T (e.g. for /// forwarding a container element). Typically used indirect via forwarded_type(), below. template @@ -66,17 +167,10 @@ struct set_caster { } void reserve_maybe(const anyset &, void *) {} -public: - bool load(handle src, bool convert) { - if (!isinstance(src)) { - return false; - } - auto s = reinterpret_borrow(src); - value.clear(); - reserve_maybe(s, &value); - for (auto entry : s) { + bool convert_iterable(const iterable &itbl, bool convert) { + for (const auto &it : itbl) { key_conv conv; - if (!conv.load(entry, convert)) { + if (!conv.load(it, convert)) { return false; } value.insert(cast_op(std::move(conv))); @@ -84,6 +178,29 @@ struct set_caster { return true; } + bool convert_anyset(const anyset &s, bool convert) { + value.clear(); + reserve_maybe(s, &value); + return convert_iterable(s, convert); + } + +public: + bool load(handle src, bool convert) { + if (!object_is_convertible_to_std_set(src, convert)) { + return false; + } + if (isinstance(src)) { + value.clear(); + return convert_anyset(reinterpret_borrow(src), convert); + } + if (!convert) { + return false; + } + assert(isinstance(src)); + value.clear(); + return convert_iterable(reinterpret_borrow(src), convert); + } + template static handle cast(T &&src, return_value_policy policy, handle parent) { if (!std::is_lvalue_reference::value) { @@ -100,7 +217,9 @@ struct set_caster { return s.release(); } - PYBIND11_TYPE_CASTER(type, const_name("Set[") + key_conv::name + const_name("]")); + PYBIND11_TYPE_CASTER(type, + io_name("collections.abc.Set", "set") + const_name("[") + key_conv::name + + const_name("]")); }; template @@ -115,15 +234,10 @@ struct map_caster { } void reserve_maybe(const dict &, void *) {} -public: - bool load(handle src, bool convert) { - if (!isinstance(src)) { - return false; - } - auto d = reinterpret_borrow(src); + bool convert_elements(const dict &d, bool convert) { value.clear(); reserve_maybe(d, &value); - for (auto it : d) { + for (const auto &it : d) { key_conv kconv; value_conv vconv; if (!kconv.load(it.first.ptr(), convert) || !vconv.load(it.second.ptr(), convert)) { @@ -134,6 +248,25 @@ struct map_caster { return true; } +public: + bool load(handle src, bool convert) { + if (!object_is_convertible_to_std_map(src, convert)) { + return false; + } + if (isinstance(src)) { + return convert_elements(reinterpret_borrow(src), convert); + } + if (!convert) { + return false; + } + auto items = reinterpret_steal(PyMapping_Items(src.ptr())); + if (!items) { + throw error_already_set(); + } + assert(isinstance(items)); + return convert_elements(dict(reinterpret_borrow(items)), convert); + } + template static handle cast(T &&src, return_value_policy policy, handle parent) { dict d; @@ -157,7 +290,8 @@ struct map_caster { } PYBIND11_TYPE_CASTER(Type, - const_name("Dict[") + key_conv::name + const_name(", ") + value_conv::name + io_name("collections.abc.Mapping", "dict") + const_name("[") + + key_conv::name + const_name(", ") + value_conv::name + const_name("]")); }; @@ -166,13 +300,35 @@ struct list_caster { using value_conv = make_caster; bool load(handle src, bool convert) { - if (!isinstance(src) || isinstance(src) || isinstance(src)) { + if (!object_is_convertible_to_std_vector(src)) { return false; } - auto s = reinterpret_borrow(src); + if (isinstance(src)) { + return convert_elements(src, convert); + } + if (!convert) { + return false; + } + // Designed to be behavior-equivalent to passing tuple(src) from Python: + // The conversion to a tuple will first exhaust the generator object, to ensure that + // the generator is not left in an unpredictable (to the caller) partially-consumed + // state. + assert(isinstance(src)); + return convert_elements(tuple(reinterpret_borrow(src)), convert); + } + +private: + template ::value, int> = 0> + void reserve_maybe(const sequence &s, Type *) { + value.reserve(s.size()); + } + void reserve_maybe(const sequence &, void *) {} + + bool convert_elements(handle seq, bool convert) { + auto s = reinterpret_borrow(seq); value.clear(); reserve_maybe(s, &value); - for (auto it : s) { + for (const auto &it : seq) { value_conv conv; if (!conv.load(it, convert)) { return false; @@ -182,13 +338,6 @@ struct list_caster { return true; } -private: - template ::value, int> = 0> - void reserve_maybe(const sequence &s, Type *) { - value.reserve(s.size()); - } - void reserve_maybe(const sequence &, void *) {} - public: template static handle cast(T &&src, return_value_policy policy, handle parent) { @@ -208,7 +357,9 @@ struct list_caster { return l.release(); } - PYBIND11_TYPE_CASTER(Type, const_name("List[") + value_conv::name + const_name("]")); + PYBIND11_TYPE_CASTER(Type, + io_name("collections.abc.Sequence", "list") + const_name("[") + + value_conv::name + const_name("]")); }; template @@ -220,43 +371,87 @@ struct type_caster> : list_caster struct type_caster> : list_caster, Type> {}; +template +ArrayType vector_to_array_impl(V &&v, index_sequence) { + return {{std::move(v[I])...}}; +} + +// Based on https://en.cppreference.com/w/cpp/container/array/to_array +template +ArrayType vector_to_array(V &&v) { + return vector_to_array_impl(std::forward(v), make_index_sequence{}); +} + template struct array_caster { using value_conv = make_caster; private: - template - bool require_size(enable_if_t size) { - if (value.size() != size) { - value.resize(size); + std::unique_ptr value; + + template = 0> + bool convert_elements(handle seq, bool convert) { + auto l = reinterpret_borrow(seq); + value.reset(new ArrayType{}); + // Using `resize` to preserve the behavior exactly as it was before PR #5305 + // For the `resize` to work, `Value` must be default constructible. + // For `std::valarray`, this is a requirement: + // https://en.cppreference.com/w/cpp/named_req/NumericType + value->resize(l.size()); + size_t ctr = 0; + for (const auto &it : l) { + value_conv conv; + if (!conv.load(it, convert)) { + return false; + } + (*value)[ctr++] = cast_op(std::move(conv)); } return true; } - template - bool require_size(enable_if_t size) { - return size == Size; - } -public: - bool load(handle src, bool convert) { - if (!isinstance(src)) { + template = 0> + bool convert_elements(handle seq, bool convert) { + auto l = reinterpret_borrow(seq); + if (l.size() != Size) { return false; } - auto l = reinterpret_borrow(src); - if (!require_size(l.size())) { - return false; - } - size_t ctr = 0; + // The `temp` storage is needed to support `Value` types that are not + // default-constructible. + // Deliberate choice: no template specializations, for simplicity, and + // because the compile time overhead for the specializations is deemed + // more significant than the runtime overhead for the `temp` storage. + std::vector temp; + temp.reserve(l.size()); for (auto it : l) { value_conv conv; if (!conv.load(it, convert)) { return false; } - value[ctr++] = cast_op(std::move(conv)); + temp.emplace_back(cast_op(std::move(conv))); } + value.reset(new ArrayType(vector_to_array(std::move(temp)))); return true; } +public: + bool load(handle src, bool convert) { + if (!object_is_convertible_to_std_vector(src)) { + return false; + } + if (isinstance(src)) { + return convert_elements(src, convert); + } + if (!convert) { + return false; + } + // Designed to be behavior-equivalent to passing tuple(src) from Python: + // The conversion to a tuple will first exhaust the generator object, to ensure that + // the generator is not left in an unpredictable (to the caller) partially-consumed + // state. + assert(isinstance(src)); + return convert_elements(tuple(reinterpret_borrow(src)), convert); + } + template static handle cast(T &&src, return_value_policy policy, handle parent) { list l(src.size()); @@ -272,12 +467,38 @@ struct array_caster { return l.release(); } - PYBIND11_TYPE_CASTER(ArrayType, - const_name(const_name(""), const_name("Annotated[")) - + const_name("List[") + value_conv::name + const_name("]") - + const_name(const_name(""), - const_name(", FixedSize(") - + const_name() + const_name(")]"))); + // Code copied from PYBIND11_TYPE_CASTER macro. + // Intentionally preserving the behavior exactly as it was before PR #5305 + template >::value, int> = 0> + static handle cast(T_ *src, return_value_policy policy, handle parent) { + if (!src) { + return none().release(); + } + if (policy == return_value_policy::take_ownership) { + auto h = cast(std::move(*src), policy, parent); + delete src; // WARNING: Assumes `src` was allocated with `new`. + return h; + } + return cast(*src, policy, parent); + } + + // NOLINTNEXTLINE(google-explicit-constructor) + operator ArrayType *() { return &(*value); } + // NOLINTNEXTLINE(google-explicit-constructor) + operator ArrayType &() { return *value; } + // NOLINTNEXTLINE(google-explicit-constructor) + operator ArrayType &&() && { return std::move(*value); } + + template + using cast_op_type = movable_cast_op_type; + + static constexpr auto name + = const_name(const_name(""), const_name("typing.Annotated[")) + + io_name("collections.abc.Sequence", "list") + const_name("[") + value_conv::name + + const_name("]") + + const_name(const_name(""), + const_name(", \"FixedSize(") + const_name() + + const_name(")\"]")); }; template @@ -336,7 +557,7 @@ struct optional_caster { return true; } - PYBIND11_TYPE_CASTER(Type, const_name("Optional[") + value_conv::name + const_name("]")); + PYBIND11_TYPE_CASTER(Type, value_conv::name | make_caster::name); }; #if defined(PYBIND11_HAS_OPTIONAL) @@ -420,9 +641,7 @@ struct variant_caster> { } using Type = V; - PYBIND11_TYPE_CASTER(Type, - const_name("Union[") + detail::concat(make_caster::name...) - + const_name("]")); + PYBIND11_TYPE_CASTER(Type, ::pybind11::detail::union_concat(make_caster::name...)); }; #if defined(PYBIND11_HAS_VARIANT) diff --git a/external_libraries/pybind11/include/pybind11/stl/filesystem.h b/external_libraries/pybind11/include/pybind11/stl/filesystem.h index e26f4217..52d29621 100644 --- a/external_libraries/pybind11/include/pybind11/stl/filesystem.h +++ b/external_libraries/pybind11/include/pybind11/stl/filesystem.h @@ -4,36 +4,32 @@ #pragma once -#include "../pybind11.h" -#include "../detail/common.h" -#include "../detail/descr.h" -#include "../cast.h" -#include "../pytypes.h" +#include +#include +#include +#include +#include #include -#ifdef __has_include -# if defined(PYBIND11_CPP17) -# if __has_include() && \ - PY_VERSION_HEX >= 0x03060000 -# include -# define PYBIND11_HAS_FILESYSTEM 1 -# elif __has_include() -# include -# define PYBIND11_HAS_EXPERIMENTAL_FILESYSTEM 1 -# endif -# endif -#endif - -#if !defined(PYBIND11_HAS_FILESYSTEM) && !defined(PYBIND11_HAS_EXPERIMENTAL_FILESYSTEM) \ - && !defined(PYBIND11_HAS_FILESYSTEM_IS_OPTIONAL) -# error \ - "Neither #include nor #include +#elif defined(PYBIND11_HAS_EXPERIMENTAL_FILESYSTEM) +# include +#else +# error "Neither #include nor #include is available." #endif PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE) PYBIND11_NAMESPACE_BEGIN(detail) +#ifdef PYPY_VERSION +# define PYBIND11_REINTERPRET_CAST_VOID_PTR_IF_NOT_PYPY(...) (__VA_ARGS__) +#else +# define PYBIND11_REINTERPRET_CAST_VOID_PTR_IF_NOT_PYPY(...) \ + (reinterpret_cast(__VA_ARGS__)) +#endif + #if defined(PYBIND11_HAS_FILESYSTEM) || defined(PYBIND11_HAS_EXPERIMENTAL_FILESYSTEM) template struct path_caster { @@ -73,7 +69,8 @@ struct path_caster { } PyObject *native = nullptr; if constexpr (std::is_same_v) { - if (PyUnicode_FSConverter(buf, &native) != 0) { + if (PyUnicode_FSConverter(buf, PYBIND11_REINTERPRET_CAST_VOID_PTR_IF_NOT_PYPY(&native)) + != 0) { if (auto *c_str = PyBytes_AsString(native)) { // AsString returns a pointer to the internal buffer, which // must not be free'd. @@ -81,7 +78,8 @@ struct path_caster { } } } else if constexpr (std::is_same_v) { - if (PyUnicode_FSDecoder(buf, &native) != 0) { + if (PyUnicode_FSDecoder(buf, PYBIND11_REINTERPRET_CAST_VOID_PTR_IF_NOT_PYPY(&native)) + != 0) { if (auto *c_str = PyUnicode_AsWideCharString(native, nullptr)) { // AsWideCharString returns a new string that must be free'd. value = c_str; // Copies the string. @@ -98,7 +96,7 @@ struct path_caster { return true; } - PYBIND11_TYPE_CASTER(T, const_name("os.PathLike")); + PYBIND11_TYPE_CASTER(T, io_name("os.PathLike | str | bytes", "pathlib.Path")); }; #endif // PYBIND11_HAS_FILESYSTEM || defined(PYBIND11_HAS_EXPERIMENTAL_FILESYSTEM) diff --git a/external_libraries/pybind11/include/pybind11/stl_bind.h b/external_libraries/pybind11/include/pybind11/stl_bind.h index 49f1b778..8202300c 100644 --- a/external_libraries/pybind11/include/pybind11/stl_bind.h +++ b/external_libraries/pybind11/include/pybind11/stl_bind.h @@ -158,8 +158,7 @@ void vector_modifiers( return v.release(); })); - cl.def( - "clear", [](Vector &v) { v.clear(); }, "Clear the contents"); + cl.def("clear", [](Vector &v) { v.clear(); }, "Clear the contents"); cl.def( "extend", @@ -181,7 +180,7 @@ void vector_modifiers( v.end()); try { v.shrink_to_fit(); - } catch (const std::exception &) { + } catch (const std::exception &) { // NOLINT(bugprone-empty-catch) // Do nothing } throw; @@ -245,7 +244,7 @@ void vector_modifiers( } auto *seq = new Vector(); - seq->reserve((size_t) slicelength); + seq->reserve(slicelength); for (size_t i = 0; i < slicelength; ++i) { seq->push_back(v[start]); @@ -488,7 +487,7 @@ PYBIND11_NAMESPACE_END(detail) // // std::vector // -template , typename... Args> +template , typename... Args> class_ bind_vector(handle scope, std::string const &name, Args &&...args) { using Class_ = class_; @@ -525,7 +524,7 @@ class_ bind_vector(handle scope, std::string const &name, A [](const Vector &v) -> bool { return !v.empty(); }, "Check whether the list is nonempty"); - cl.def("__len__", &Vector::size); + cl.def("__len__", [](const Vector &vec) { return vec.size(); }); #if 0 // C++ style functions deprecated, leaving it here as an example @@ -645,66 +644,99 @@ auto map_if_insertion_operator(Class_ &cl, std::string const &name) "Return the canonical string representation of this map."); } -template struct keys_view { virtual size_t len() = 0; virtual iterator iter() = 0; - virtual bool contains(const KeyType &k) = 0; - virtual bool contains(const object &k) = 0; + virtual bool contains(const handle &k) = 0; virtual ~keys_view() = default; }; -template struct values_view { virtual size_t len() = 0; virtual iterator iter() = 0; virtual ~values_view() = default; }; -template struct items_view { virtual size_t len() = 0; virtual iterator iter() = 0; virtual ~items_view() = default; }; -template -struct KeysViewImpl : public KeysView { +template +struct KeysViewImpl : public detail::keys_view { explicit KeysViewImpl(Map &map) : map(map) {} size_t len() override { return map.size(); } iterator iter() override { return make_key_iterator(map.begin(), map.end()); } - bool contains(const typename Map::key_type &k) override { return map.find(k) != map.end(); } - bool contains(const object &) override { return false; } + bool contains(const handle &k) override { + try { + return map.find(k.template cast()) != map.end(); + } catch (const cast_error &) { + return false; + } + } Map ↦ }; -template -struct ValuesViewImpl : public ValuesView { +template +struct ValuesViewImpl : public detail::values_view { explicit ValuesViewImpl(Map &map) : map(map) {} size_t len() override { return map.size(); } iterator iter() override { return make_value_iterator(map.begin(), map.end()); } Map ↦ }; -template -struct ItemsViewImpl : public ItemsView { +template +struct ItemsViewImpl : public detail::items_view { explicit ItemsViewImpl(Map &map) : map(map) {} size_t len() override { return map.size(); } iterator iter() override { return make_iterator(map.begin(), map.end()); } Map ↦ }; +inline str format_message_key_error_key_object(handle py_key) { + str message = "pybind11::bind_map key"; + if (!py_key) { + return message; + } + try { + message = str(py_key); + } catch (const std::exception &) { + try { + message = repr(py_key); + } catch (const std::exception &) { + return message; + } + } + const ssize_t cut_length = 100; + if (len(message) > 2 * cut_length + 3) { + return str(message[slice(0, cut_length, 1)]) + str("✄✄✄") + + str(message[slice(-cut_length, static_cast(len(message)), 1)]); + } + return message; +} + +template +str format_message_key_error(const KeyType &key) { + object py_key; + try { + py_key = cast(key); + } catch (const std::exception &) { + do { // Trick to avoid "empty catch" warning/error. + } while (false); + } + return format_message_key_error_key_object(py_key); +} + PYBIND11_NAMESPACE_END(detail) -template , typename... Args> +template , typename... Args> class_ bind_map(handle scope, const std::string &name, Args &&...args) { using KeyType = typename Map::key_type; using MappedType = typename Map::mapped_type; - using StrippedKeyType = detail::remove_cvref_t; - using StrippedMappedType = detail::remove_cvref_t; - using KeysView = detail::keys_view; - using ValuesView = detail::values_view; - using ItemsView = detail::items_view; + using KeysView = detail::keys_view; + using ValuesView = detail::values_view; + using ItemsView = detail::items_view; using Class_ = class_; // If either type is a non-module-local bound type then make the map binding non-local as well; @@ -718,39 +750,20 @@ class_ bind_map(handle scope, const std::string &name, Args && } Class_ cl(scope, name.c_str(), pybind11::module_local(local), std::forward(args)...); - static constexpr auto key_type_descr = detail::make_caster::name; - static constexpr auto mapped_type_descr = detail::make_caster::name; - std::string key_type_name(key_type_descr.text), mapped_type_name(mapped_type_descr.text); - // If key type isn't properly wrapped, fall back to C++ names - if (key_type_name == "%") { - key_type_name = detail::type_info_description(typeid(KeyType)); - } - // Similarly for value type: - if (mapped_type_name == "%") { - mapped_type_name = detail::type_info_description(typeid(MappedType)); - } - - // Wrap KeysView[KeyType] if it wasn't already wrapped + // Wrap KeysView if it wasn't already wrapped if (!detail::get_type_info(typeid(KeysView))) { - class_ keys_view( - scope, ("KeysView[" + key_type_name + "]").c_str(), pybind11::module_local(local)); + class_ keys_view(scope, "KeysView", pybind11::module_local(local)); keys_view.def("__len__", &KeysView::len); keys_view.def("__iter__", &KeysView::iter, keep_alive<0, 1>() /* Essential: keep view alive while iterator exists */ ); - keys_view.def("__contains__", - static_cast(&KeysView::contains)); - // Fallback for when the object is not of the key type - keys_view.def("__contains__", - static_cast(&KeysView::contains)); + keys_view.def("__contains__", &KeysView::contains); } // Similarly for ValuesView: if (!detail::get_type_info(typeid(ValuesView))) { - class_ values_view(scope, - ("ValuesView[" + mapped_type_name + "]").c_str(), - pybind11::module_local(local)); + class_ values_view(scope, "ValuesView", pybind11::module_local(local)); values_view.def("__len__", &ValuesView::len); values_view.def("__iter__", &ValuesView::iter, @@ -759,10 +772,7 @@ class_ bind_map(handle scope, const std::string &name, Args && } // Similarly for ItemsView: if (!detail::get_type_info(typeid(ItemsView))) { - class_ items_view( - scope, - ("ItemsView[" + key_type_name + ", ").append(mapped_type_name + "]").c_str(), - pybind11::module_local(local)); + class_ items_view(scope, "ItemsView", pybind11::module_local(local)); items_view.def("__len__", &ItemsView::len); items_view.def("__iter__", &ItemsView::iter, @@ -788,25 +798,19 @@ class_ bind_map(handle scope, const std::string &name, Args && cl.def( "keys", - [](Map &m) { - return std::unique_ptr(new detail::KeysViewImpl(m)); - }, + [](Map &m) { return std::unique_ptr(new detail::KeysViewImpl(m)); }, keep_alive<0, 1>() /* Essential: keep map alive while view exists */ ); cl.def( "values", - [](Map &m) { - return std::unique_ptr(new detail::ValuesViewImpl(m)); - }, + [](Map &m) { return std::unique_ptr(new detail::ValuesViewImpl(m)); }, keep_alive<0, 1>() /* Essential: keep map alive while view exists */ ); cl.def( "items", - [](Map &m) { - return std::unique_ptr(new detail::ItemsViewImpl(m)); - }, + [](Map &m) { return std::unique_ptr(new detail::ItemsViewImpl(m)); }, keep_alive<0, 1>() /* Essential: keep map alive while view exists */ ); @@ -815,7 +819,8 @@ class_ bind_map(handle scope, const std::string &name, Args && [](Map &m, const KeyType &k) -> MappedType & { auto it = m.find(k); if (it == m.end()) { - throw key_error(); + set_error(PyExc_KeyError, detail::format_message_key_error(k)); + throw error_already_set(); } return it->second; }, @@ -838,12 +843,14 @@ class_ bind_map(handle scope, const std::string &name, Args && cl.def("__delitem__", [](Map &m, const KeyType &k) { auto it = m.find(k); if (it == m.end()) { - throw key_error(); + set_error(PyExc_KeyError, detail::format_message_key_error(k)); + throw error_already_set(); } m.erase(it); }); - cl.def("__len__", &Map::size); + // Always use a lambda in case of `using` declaration + cl.def("__len__", [](const Map &m) { return m.size(); }); return cl; } diff --git a/external_libraries/pybind11/include/pybind11/subinterpreter.h b/external_libraries/pybind11/include/pybind11/subinterpreter.h new file mode 100644 index 00000000..54754526 --- /dev/null +++ b/external_libraries/pybind11/include/pybind11/subinterpreter.h @@ -0,0 +1,291 @@ +/* + pybind11/subinterpreter.h: Support for creating and using subinterpreters + + Copyright (c) 2025 The Pybind Development Team. + + All rights reserved. Use of this source code is governed by a + BSD-style license that can be found in the LICENSE file. +*/ + +#pragma once + +#include "detail/common.h" +#include "detail/internals.h" +#include "gil.h" + +#include + +#ifndef PYBIND11_HAS_SUBINTERPRETER_SUPPORT +# error "This platform does not support subinterpreters, do not include this file." +#endif + +PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE) + +class subinterpreter; + +/// Activate the subinterpreter and acquire its GIL, while also releasing any GIL and interpreter +/// currently held. Upon exiting the scope, the previous subinterpreter (if any) and its +/// associated GIL are restored to their state as they were before the scope was entered. +class subinterpreter_scoped_activate { +public: + explicit subinterpreter_scoped_activate(subinterpreter const &si); + ~subinterpreter_scoped_activate(); + + subinterpreter_scoped_activate(subinterpreter_scoped_activate &&) = delete; + subinterpreter_scoped_activate(subinterpreter_scoped_activate const &) = delete; + subinterpreter_scoped_activate &operator=(subinterpreter_scoped_activate &) = delete; + subinterpreter_scoped_activate &operator=(subinterpreter_scoped_activate const &) = delete; + +private: + PyThreadState *old_tstate_ = nullptr; + PyThreadState *tstate_ = nullptr; + PyGILState_STATE gil_state_; + bool simple_gil_ = false; +}; + +/// Holds a Python subinterpreter instance +class subinterpreter { +public: + /// empty/unusable, but move-assignable. use create() to create a subinterpreter. + subinterpreter() = default; + + subinterpreter(subinterpreter const ©) = delete; + subinterpreter &operator=(subinterpreter const ©) = delete; + + subinterpreter(subinterpreter &&old) noexcept + : istate_(old.istate_), creation_tstate_(old.creation_tstate_) { + old.istate_ = nullptr; + old.creation_tstate_ = nullptr; + } + + subinterpreter &operator=(subinterpreter &&old) noexcept { + std::swap(old.istate_, istate_); + std::swap(old.creation_tstate_, creation_tstate_); + return *this; + } + + /// Create a new subinterpreter with the specified configuration + /// @note This function acquires (and then releases) the main interpreter GIL, but the main + /// interpreter and its GIL are not required to be held prior to calling this function. + static subinterpreter create(PyInterpreterConfig const &cfg) { + + error_scope err_scope; + subinterpreter result; + { + // we must hold the main GIL in order to create a subinterpreter + subinterpreter_scoped_activate main_guard(main()); + + auto *prev_tstate = PyThreadState_Get(); + + PyStatus status; + + { + /* + Several internal CPython modules are lacking proper subinterpreter support in 3.12 + even though it is "stable" in that version. This most commonly seems to cause + crashes when two interpreters concurrently initialize, which imports several things + (like builtins, unicode, codecs). + */ +#if PY_VERSION_HEX < 0x030D0000 && defined(Py_MOD_PER_INTERPRETER_GIL_SUPPORTED) + static std::mutex one_at_a_time; + std::lock_guard guard(one_at_a_time); +#endif + status = Py_NewInterpreterFromConfig(&result.creation_tstate_, &cfg); + } + + // this doesn't raise a normal Python exception, it provides an exit() status code. + if (PyStatus_Exception(status) != 0) { + pybind11_fail("failed to create new sub-interpreter"); + } + + // upon success, the new interpreter is activated in this thread + result.istate_ = result.creation_tstate_->interp; + detail::has_seen_non_main_interpreter() = true; + detail::get_internals(); // initialize internals.tstate, amongst other things... + + // In 3.13+ this state should be deleted right away, and the memory will be reused for + // the next threadstate on this interpreter. However, on 3.12 we cannot do that, we + // must keep it around (but not use it) ... see destructor. +#if PY_VERSION_HEX >= 0x030D0000 + PyThreadState_Clear(result.creation_tstate_); + PyThreadState_DeleteCurrent(); +#endif + + // we have to switch back to main, and then the scopes will handle cleanup + PyThreadState_Swap(prev_tstate); + } + return result; + } + + /// Calls create() with a default configuration of an isolated interpreter that disallows fork, + /// exec, and Python threads. + static subinterpreter create() { + // same as the default config in the python docs + PyInterpreterConfig cfg; + std::memset(&cfg, 0, sizeof(cfg)); + cfg.allow_threads = 1; + cfg.check_multi_interp_extensions = 1; + cfg.gil = PyInterpreterConfig_OWN_GIL; + return create(cfg); + } + + ~subinterpreter() { + if (!creation_tstate_) { + // non-owning wrapper, do nothing. + return; + } + + PyThreadState *destroy_tstate = nullptr; + PyThreadState *old_tstate = nullptr; + + // Python 3.12 requires us to keep the original PyThreadState alive until we are ready to + // destroy the interpreter. We prefer to use that to destroy the interpreter. +#if PY_VERSION_HEX < 0x030D0000 + // The tstate passed to Py_EndInterpreter MUST have been created on the current OS thread. + bool same_thread = false; +# ifdef PY_HAVE_THREAD_NATIVE_ID + same_thread = PyThread_get_thread_native_id() == creation_tstate_->native_thread_id; +# endif + if (same_thread) { + // OK it is safe to use the creation state here + destroy_tstate = creation_tstate_; + old_tstate = PyThreadState_Swap(destroy_tstate); + } else { + // We have to make a new tstate on this thread and use that. + destroy_tstate = PyThreadState_New(istate_); + old_tstate = PyThreadState_Swap(destroy_tstate); + + // We can use the one we just created, so we must delete the creation state. + PyThreadState_Clear(creation_tstate_); + PyThreadState_Delete(creation_tstate_); + } +#else + destroy_tstate = PyThreadState_New(istate_); + old_tstate = PyThreadState_Swap(destroy_tstate); +#endif + + bool switch_back = (old_tstate != nullptr) && old_tstate->interp != istate_; + + // Internals always exists in the subinterpreter, this class enforces it when it creates + // the subinterpreter. Even if it didn't, this only creates the pointer-to-pointer, not the + // internals themselves. + detail::get_internals_pp_manager().get_pp(); + detail::get_local_internals_pp_manager().get_pp(); + + // End it + Py_EndInterpreter(destroy_tstate); + + // It's possible for the internals to be created during endinterpreter (e.g. if a + // py::capsule calls `get_internals()` during destruction), so we destroy afterward. + detail::get_internals_pp_manager().destroy(); + detail::get_local_internals_pp_manager().destroy(); + + // switch back to the old tstate and old GIL (if there was one) + if (switch_back) { + PyThreadState_Swap(old_tstate); + } + } + + /// Get a handle to the main interpreter that can be used with subinterpreter_scoped_activate + /// Note that destructing the handle is a noop, the main interpreter can only be ended by + /// py::finalize_interpreter() + static subinterpreter main() { + subinterpreter m; + m.istate_ = PyInterpreterState_Main(); + m.disarm(); // make destruct a noop + return m; + } + + /// Get a non-owning wrapper of the currently active interpreter (if any) + static subinterpreter current() { + subinterpreter c; + c.istate_ = detail::get_interpreter_state_unchecked(); + c.disarm(); // make destruct a noop, we don't own this... + return c; + } + + /// Get the numerical identifier for the sub-interpreter + int64_t id() const { + if (istate_ != nullptr) { + return PyInterpreterState_GetID(istate_); + } + return -1; // CPython uses one-up numbers from 0, so negative should be safe to return + // here. + } + + /// Get the interpreter's state dict. This interpreter's GIL must be held before calling! + dict state_dict() { return reinterpret_borrow(PyInterpreterState_GetDict(istate_)); } + + /// abandon cleanup of this subinterpreter (leak it). this might be needed during + /// finalization... + void disarm() { creation_tstate_ = nullptr; } + + /// An empty wrapper cannot be activated + bool empty() const { return istate_ == nullptr; } + + /// Is this wrapper non-empty + explicit operator bool() const { return !empty(); } + +private: + friend class subinterpreter_scoped_activate; + PyInterpreterState *istate_ = nullptr; + PyThreadState *creation_tstate_ = nullptr; +}; + +class scoped_subinterpreter { +public: + scoped_subinterpreter() : si_(subinterpreter::create()), scope_(si_) {} + + explicit scoped_subinterpreter(PyInterpreterConfig const &cfg) + : si_(subinterpreter::create(cfg)), scope_(si_) {} + +private: + subinterpreter si_; + subinterpreter_scoped_activate scope_; +}; + +inline subinterpreter_scoped_activate::subinterpreter_scoped_activate(subinterpreter const &si) { + if (!si.istate_) { + pybind11_fail("null subinterpreter"); + } + + if (detail::get_interpreter_state_unchecked() == si.istate_) { + // we are already on this interpreter, make sure we hold the GIL + simple_gil_ = true; + gil_state_ = PyGILState_Ensure(); + return; + } + + // we can't really interact with the interpreter at all until we switch to it + // not even to, for example, look in its state dict or touch its internals + tstate_ = PyThreadState_New(si.istate_); + + // make the interpreter active and acquire the GIL + old_tstate_ = PyThreadState_Swap(tstate_); + + // save this in internals for scoped_gil calls (see also: PR #5870) + detail::get_internals().tstate = tstate_; +} + +inline subinterpreter_scoped_activate::~subinterpreter_scoped_activate() { + if (simple_gil_) { + // We were on this interpreter already, so just make sure the GIL goes back as it was + PyGILState_Release(gil_state_); + } else { + if (tstate_) { +#if defined(PYBIND11_DETAILED_ERROR_MESSAGES) + if (detail::get_thread_state_unchecked() != tstate_) { + pybind11_fail("~subinterpreter_scoped_activate: thread state must be current!"); + } +#endif + detail::get_internals().tstate.reset(); + PyThreadState_Clear(tstate_); + PyThreadState_DeleteCurrent(); + } + + // Go back the previous interpreter (if any) and acquire THAT gil + PyThreadState_Swap(old_tstate_); + } +} + +PYBIND11_NAMESPACE_END(PYBIND11_NAMESPACE) diff --git a/external_libraries/pybind11/include/pybind11/trampoline_self_life_support.h b/external_libraries/pybind11/include/pybind11/trampoline_self_life_support.h new file mode 100644 index 00000000..cbfec7f9 --- /dev/null +++ b/external_libraries/pybind11/include/pybind11/trampoline_self_life_support.h @@ -0,0 +1,65 @@ +// Copyright (c) 2021 The Pybind Development Team. +// All rights reserved. Use of this source code is governed by a +// BSD-style license that can be found in the LICENSE file. + +#pragma once + +#include "detail/common.h" +#include "detail/using_smart_holder.h" +#include "detail/value_and_holder.h" + +PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE) + +PYBIND11_NAMESPACE_BEGIN(detail) +// PYBIND11:REMINDER: Needs refactoring of existing pybind11 code. +inline bool deregister_instance(instance *self, void *valptr, const type_info *tinfo); +PYBIND11_NAMESPACE_END(detail) + +// The original core idea for this struct goes back to PyCLIF: +// https://github.com/google/clif/blob/07f95d7e69dca2fcf7022978a55ef3acff506c19/clif/python/runtime.cc#L37 +// URL provided here mainly to give proper credit. +struct trampoline_self_life_support { + // NOTE: PYBIND11_INTERNALS_VERSION needs to be bumped if changes are made to this struct. + detail::value_and_holder v_h; + + trampoline_self_life_support() = default; + + void activate_life_support(const detail::value_and_holder &v_h_) { + Py_INCREF((PyObject *) v_h_.inst); + v_h = v_h_; + } + + void deactivate_life_support() { + Py_DECREF((PyObject *) v_h.inst); + v_h = detail::value_and_holder(); + } + + ~trampoline_self_life_support() { + if (v_h.inst != nullptr && v_h.vh != nullptr) { + void *value_void_ptr = v_h.value_ptr(); + if (value_void_ptr != nullptr) { + PyGILState_STATE threadstate = PyGILState_Ensure(); + v_h.value_ptr() = nullptr; + v_h.holder().release_disowned(); + detail::deregister_instance(v_h.inst, value_void_ptr, v_h.type); + Py_DECREF((PyObject *) v_h.inst); // Must be after deregister. + PyGILState_Release(threadstate); + } + } + } + + // For the next two, the default implementations generate undefined behavior (ASAN failures + // manually verified). The reason is that v_h needs to be kept default-initialized. + trampoline_self_life_support(const trampoline_self_life_support &) {} + trampoline_self_life_support(trampoline_self_life_support &&) noexcept {} + + // These should never be needed (please provide test cases if you think they are). + trampoline_self_life_support &operator=(const trampoline_self_life_support &) = delete; + trampoline_self_life_support &operator=(trampoline_self_life_support &&) = delete; +}; + +PYBIND11_NAMESPACE_BEGIN(detail) +using get_trampoline_self_life_support_fn = trampoline_self_life_support *(*) (void *); +PYBIND11_NAMESPACE_END(detail) + +PYBIND11_NAMESPACE_END(PYBIND11_NAMESPACE) diff --git a/external_libraries/pybind11/include/pybind11/typing.h b/external_libraries/pybind11/include/pybind11/typing.h new file mode 100644 index 00000000..43e2187b --- /dev/null +++ b/external_libraries/pybind11/include/pybind11/typing.h @@ -0,0 +1,295 @@ +/* + pybind11/typing.h: Convenience wrapper classes for basic Python types + with more explicit annotations. + + Copyright (c) 2023 Dustin Spicuzza + + All rights reserved. Use of this source code is governed by a + BSD-style license that can be found in the LICENSE file. +*/ + +#pragma once + +#include "detail/common.h" +#include "cast.h" +#include "pytypes.h" + +#include + +#if defined(__cpp_nontype_template_args) && __cpp_nontype_template_args >= 201911L +# define PYBIND11_TYPING_H_HAS_STRING_LITERAL +# include +# include +# include +#endif + +PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE) +PYBIND11_NAMESPACE_BEGIN(typing) + +/* + The following types can be used to direct pybind11-generated docstrings + to have have more explicit types (e.g., `list[str]` instead of `list`). + Just use these in place of existing types. + + There is no additional enforcement of types at runtime. +*/ + +// Tuple type hint defined in cast.h for use in py::make_tuple to avoid circular includes + +template +class Dict : public dict { + using dict::dict; +}; + +template +class List : public list { + using list::list; +}; + +template +class Set : public set { + using set::set; +}; + +template +class Iterable : public iterable { + using iterable::iterable; +}; + +template +class Iterator : public iterator { + using iterator::iterator; +}; + +template +class Callable; + +template +class Callable : public function { + using function::function; +}; + +template +class Type : public type { + using type::type; +}; + +template +class Union : public object { + PYBIND11_OBJECT_DEFAULT(Union, object, PyObject_Type) + using object::object; +}; + +template +class Optional : public object { + PYBIND11_OBJECT_DEFAULT(Optional, object, PyObject_Type) + using object::object; +}; + +template +class Final : public object { + PYBIND11_OBJECT_DEFAULT(Final, object, PyObject_Type) + using object::object; +}; + +template +class ClassVar : public object { + PYBIND11_OBJECT_DEFAULT(ClassVar, object, PyObject_Type) + using object::object; +}; + +template +class TypeGuard : public bool_ { + using bool_::bool_; +}; + +template +class TypeIs : public bool_ { + using bool_::bool_; +}; + +class NoReturn : public none { + using none::none; +}; + +class Never : public none { + using none::none; +}; + +#if defined(PYBIND11_TYPING_H_HAS_STRING_LITERAL) +template +struct StringLiteral { + constexpr StringLiteral(const char (&str)[N]) { std::copy_n(str, N, name); } + char name[N]; +}; + +template +class Literal : public object { + PYBIND11_OBJECT_DEFAULT(Literal, object, PyObject_Type) +}; + +// Example syntax for creating a TypeVar. +// typedef typing::TypeVar<"T"> TypeVarT; +template +class TypeVar : public object { + PYBIND11_OBJECT_DEFAULT(TypeVar, object, PyObject_Type) + using object::object; +}; +#endif + +PYBIND11_NAMESPACE_END(typing) + +PYBIND11_NAMESPACE_BEGIN(detail) + +template +struct handle_type_name> { + static constexpr auto name = const_name("tuple[") + + ::pybind11::detail::concat(make_caster::name...) + + const_name("]"); +}; + +template <> +struct handle_type_name> { + // PEP 484 specifies this syntax for an empty tuple + static constexpr auto name = const_name("tuple[()]"); +}; + +template +struct handle_type_name> { + // PEP 484 specifies this syntax for a variable-length tuple + static constexpr auto name + = const_name("tuple[") + make_caster::name + const_name(", ...]"); +}; + +template +struct handle_type_name> { + static constexpr auto name = const_name("dict[") + make_caster::name + const_name(", ") + + make_caster::name + const_name("]"); +}; + +template +struct handle_type_name> { + static constexpr auto name = const_name("list[") + make_caster::name + const_name("]"); +}; + +template +struct handle_type_name> { + static constexpr auto name = const_name("set[") + make_caster::name + const_name("]"); +}; + +template +struct handle_type_name> { + static constexpr auto name + = const_name("collections.abc.Iterable[") + make_caster::name + const_name("]"); +}; + +template +struct handle_type_name> { + static constexpr auto name + = const_name("collections.abc.Iterator[") + make_caster::name + const_name("]"); +}; + +template +struct handle_type_name> { + using retval_type = conditional_t::value, void_type, Return>; + static constexpr auto name + = const_name("collections.abc.Callable[[") + + ::pybind11::detail::concat(::pybind11::detail::arg_descr(make_caster::name)...) + + const_name("], ") + ::pybind11::detail::return_descr(make_caster::name) + + const_name("]"); +}; + +template +struct handle_type_name> { + // PEP 484 specifies this syntax for defining only return types of callables + using retval_type = conditional_t::value, void_type, Return>; + static constexpr auto name = const_name("collections.abc.Callable[..., ") + + ::pybind11::detail::return_descr(make_caster::name) + + const_name("]"); +}; + +template +struct handle_type_name> { + static constexpr auto name = const_name("type[") + make_caster::name + const_name("]"); +}; + +template +struct handle_type_name> { + static constexpr auto name = ::pybind11::detail::union_concat(make_caster::name...); +}; + +template +struct handle_type_name> { + static constexpr auto name = make_caster::name | make_caster::name; +}; + +template +struct handle_type_name> { + static constexpr auto name = const_name("typing.Final[") + + ::pybind11::detail::return_descr(make_caster::name) + + const_name("]"); +}; + +template +struct handle_type_name> { + static constexpr auto name + = const_name("typing.ClassVar[") + make_caster::name + const_name("]"); +}; + +template +struct handle_type_name> { + static constexpr auto name = const_name(PYBIND11_TYPE_GUARD_TYPE_HINT) + const_name("[") + + make_caster::name + const_name("]"); +}; + +template +struct handle_type_name> { + static constexpr auto name = const_name(PYBIND11_TYPE_IS_TYPE_HINT) + const_name("[") + + make_caster::name + const_name("]"); +}; + +template <> +struct handle_type_name { + static constexpr auto name = const_name("typing.NoReturn"); +}; + +template <> +struct handle_type_name { + static constexpr auto name = const_name(PYBIND11_NEVER_TYPE_HINT); +}; + +#if defined(PYBIND11_TYPING_H_HAS_STRING_LITERAL) +template +consteval auto sanitize_string_literal() { + constexpr std::string_view v(StrLit.name); + constexpr std::string_view special_chars("!@%{}-"); + constexpr auto num_special_chars = std::accumulate( + special_chars.begin(), special_chars.end(), (size_t) 0, [&v](auto acc, const char &c) { + return std::move(acc) + std::ranges::count(v, c); + }); + char result[v.size() + num_special_chars + 1]; + size_t i = 0; + for (auto c : StrLit.name) { + if (special_chars.find(c) != std::string_view::npos) { + result[i++] = '!'; + } + result[i++] = c; + } + return typing::StringLiteral(result); +} + +template +struct handle_type_name> { + static constexpr auto name + = const_name("typing.Literal[") + + pybind11::detail::concat(const_name(sanitize_string_literal().name)...) + + const_name("]"); +}; +template +struct handle_type_name> { + static constexpr auto name = const_name(sanitize_string_literal().name); +}; +#endif + +PYBIND11_NAMESPACE_END(detail) +PYBIND11_NAMESPACE_END(PYBIND11_NAMESPACE) diff --git a/external_libraries/pybind11/include/pybind11/warnings.h b/external_libraries/pybind11/include/pybind11/warnings.h new file mode 100644 index 00000000..263b2990 --- /dev/null +++ b/external_libraries/pybind11/include/pybind11/warnings.h @@ -0,0 +1,75 @@ +/* + pybind11/warnings.h: Python warnings wrappers. + + Copyright (c) 2024 Jan Iwaszkiewicz + + All rights reserved. Use of this source code is governed by a + BSD-style license that can be found in the LICENSE file. +*/ + +#pragma once + +#include "pybind11.h" +#include "detail/common.h" + +PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE) + +PYBIND11_NAMESPACE_BEGIN(detail) + +inline bool PyWarning_Check(PyObject *obj) { + int result = PyObject_IsSubclass(obj, PyExc_Warning); + if (result == 1) { + return true; + } + if (result == -1) { + raise_from(PyExc_SystemError, + "pybind11::detail::PyWarning_Check(): PyObject_IsSubclass() call failed."); + throw error_already_set(); + } + return false; +} + +PYBIND11_NAMESPACE_END(detail) + +PYBIND11_NAMESPACE_BEGIN(warnings) + +inline object +new_warning_type(handle scope, const char *name, handle base = PyExc_RuntimeWarning) { + if (!detail::PyWarning_Check(base.ptr())) { + pybind11_fail("pybind11::warnings::new_warning_type(): cannot create custom warning, base " + "must be a subclass of " + "PyExc_Warning!"); + } + if (hasattr(scope, name)) { + pybind11_fail("pybind11::warnings::new_warning_type(): an attribute with name \"" + + std::string(name) + "\" exists already."); + } + std::string full_name = scope.attr("__name__").cast() + std::string(".") + name; + handle h(PyErr_NewException(full_name.c_str(), base.ptr(), nullptr)); + if (!h) { + raise_from(PyExc_SystemError, + "pybind11::warnings::new_warning_type(): PyErr_NewException() call failed."); + throw error_already_set(); + } + auto obj = reinterpret_steal(h); + scope.attr(name) = obj; + return obj; +} + +// Similar to Python `warnings.warn()` +inline void +warn(const char *message, handle category = PyExc_RuntimeWarning, int stack_level = 2) { + if (!detail::PyWarning_Check(category.ptr())) { + pybind11_fail( + "pybind11::warnings::warn(): cannot raise warning, category must be a subclass of " + "PyExc_Warning!"); + } + + if (PyErr_WarnEx(category.ptr(), message, stack_level) == -1) { + throw error_already_set(); + } +} + +PYBIND11_NAMESPACE_END(warnings) + +PYBIND11_NAMESPACE_END(PYBIND11_NAMESPACE) diff --git a/external_libraries/pybind11/noxfile.py b/external_libraries/pybind11/noxfile.py index 021ced24..77802522 100644 --- a/external_libraries/pybind11/noxfile.py +++ b/external_libraries/pybind11/noxfile.py @@ -1,24 +1,24 @@ -import os +#!/usr/bin/env -S uv run -import nox +# /// script +# dependencies = ["nox>=2025.2.9"] +# /// -nox.needs_version = ">=2022.1.7" -nox.options.sessions = ["lint", "tests", "tests_packaging"] +from __future__ import annotations -PYTHON_VERSIONS = [ - "3.6", - "3.7", - "3.8", - "3.9", - "3.10", - "3.11", - "pypy3.7", - "pypy3.8", - "pypy3.9", -] +import argparse +import contextlib +import os +from pathlib import Path +from typing import TYPE_CHECKING -if os.environ.get("CI", None): - nox.options.error_on_missing_interpreters = True +if TYPE_CHECKING: + from collections.abc import Generator + +import nox + +nox.needs_version = ">=2025.2.9" +nox.options.default_venv_backend = "uv|virtualenv" @nox.session(reuse_venv=True) @@ -30,7 +30,7 @@ def lint(session: nox.Session) -> None: session.run("pre-commit", "run", "-a", *session.posargs) -@nox.session(python=PYTHON_VERSIONS) +@nox.session def tests(session: nox.Session) -> None: """ Run the tests (requires a compiler). @@ -57,51 +57,95 @@ def tests_packaging(session: nox.Session) -> None: Run the packaging tests. """ - session.install("-r", "tests/requirements.txt", "--prefer-binary") + session.install("-r", "tests/requirements.txt", "pip") session.run("pytest", "tests/extra_python_package", *session.posargs) -@nox.session(reuse_venv=True) +@nox.session(reuse_venv=True, default=False) def docs(session: nox.Session) -> None: """ - Build the docs. Pass "serve" to serve. + Build the docs. Pass --non-interactive to avoid serving. """ - session.install("-r", "docs/requirements.txt") - session.chdir("docs") + parser = argparse.ArgumentParser() + parser.add_argument( + "-b", dest="builder", default="html", help="Build target (default: html)" + ) + args, posargs = parser.parse_known_args(session.posargs) + serve = args.builder == "html" and session.interactive - if "pdf" in session.posargs: - session.run("sphinx-build", "-M", "latexpdf", ".", "_build") - return + extra_installs = ["sphinx-autobuild"] if serve else [] + session.install("-r", "docs/requirements.txt", *extra_installs) + session.chdir("docs") - session.run("sphinx-build", "-M", "html", ".", "_build") + shared_args = ( + "-n", # nitpicky mode + "-T", # full tracebacks + f"-b={args.builder}", + ".", + f"_build/{args.builder}", + *posargs, + ) - if "serve" in session.posargs: - session.log("Launching docs at http://localhost:8000/ - use Ctrl-C to quit") - session.run("python", "-m", "http.server", "8000", "-d", "_build/html") - elif session.posargs: - session.error("Unsupported argument to docs") + if serve: + session.run( + "sphinx-autobuild", "--open-browser", "--ignore=.build", *shared_args + ) + else: + session.run("sphinx-build", "--keep-going", *shared_args) -@nox.session(reuse_venv=True) +@nox.session(reuse_venv=True, default=False) def make_changelog(session: nox.Session) -> None: """ Inspect the closed issues and make entries for a changelog. """ - session.install("ghapi", "rich") - session.run("python", "tools/make_changelog.py") + session.install_and_run_script("tools/make_changelog.py") -@nox.session(reuse_venv=True) +@nox.session(reuse_venv=True, default=False) def build(session: nox.Session) -> None: """ - Build SDists and wheels. + Build SDist and wheel. """ session.install("build") session.log("Building normal files") session.run("python", "-m", "build", *session.posargs) - session.log("Building pybind11-global files (PYBIND11_GLOBAL_SDIST=1)") - session.run( - "python", "-m", "build", *session.posargs, env={"PYBIND11_GLOBAL_SDIST": "1"} - ) + + +@contextlib.contextmanager +def preserve_file(filename: Path) -> Generator[str, None, None]: + """ + Causes a file to be stored and preserved when the context manager exits. + """ + old_stat = filename.stat() + old_file = filename.read_text(encoding="utf-8") + try: + yield old_file + finally: + filename.write_text(old_file, encoding="utf-8") + os.utime(filename, (old_stat.st_atime, old_stat.st_mtime)) + + +@nox.session(reuse_venv=True) +def build_global(session: nox.Session) -> None: + """ + Build global SDist and wheel. + """ + + installer = ["--installer=uv"] if session.venv_backend == "uv" else [] + session.install("build", "tomlkit") + session.log("Building pybind11-global files") + pyproject = Path("pyproject.toml") + with preserve_file(pyproject): + newer_txt = session.run("python", "tools/make_global.py", silent=True) + assert isinstance(newer_txt, str) + pyproject.write_text(newer_txt, encoding="utf-8") + session.run( + "python", + "-m", + "build", + *installer, + *session.posargs, + ) diff --git a/external_libraries/pybind11/pybind11/__init__.py b/external_libraries/pybind11/pybind11/__init__.py new file mode 100644 index 00000000..e9d033c4 --- /dev/null +++ b/external_libraries/pybind11/pybind11/__init__.py @@ -0,0 +1,19 @@ +from __future__ import annotations + +import sys + +if sys.version_info < (3, 8): # noqa: UP036 + msg = "pybind11 does not support Python < 3.8. v2.13 was the last release supporting Python 3.7." + raise ImportError(msg) + + +from ._version import __version__, version_info +from .commands import get_cmake_dir, get_include, get_pkgconfig_dir + +__all__ = ( + "version_info", + "__version__", + "get_include", + "get_cmake_dir", + "get_pkgconfig_dir", +) diff --git a/external_libraries/pybind11/pybind11/__main__.py b/external_libraries/pybind11/pybind11/__main__.py new file mode 100644 index 00000000..21c3cd9a --- /dev/null +++ b/external_libraries/pybind11/pybind11/__main__.py @@ -0,0 +1,97 @@ +# pylint: disable=missing-function-docstring +from __future__ import annotations + +import argparse +import functools +import re +import sys +import sysconfig + +from ._version import __version__ +from .commands import get_cmake_dir, get_include, get_pkgconfig_dir + +# This is the conditional used for os.path being posixpath +if "posix" in sys.builtin_module_names: + from shlex import quote +elif "nt" in sys.builtin_module_names: + # See https://github.com/mesonbuild/meson/blob/db22551ed9d2dd7889abea01cc1c7bba02bf1c75/mesonbuild/utils/universal.py#L1092-L1121 + # and the original documents: + # https://docs.microsoft.com/en-us/cpp/c-language/parsing-c-command-line-arguments and + # https://blogs.msdn.microsoft.com/twistylittlepassagesallalike/2011/04/23/everyone-quotes-command-line-arguments-the-wrong-way/ + UNSAFE = re.compile("[ \t\n\r]") + + def quote(s: str) -> str: + if s and not UNSAFE.search(s): + return s + + # Paths cannot contain a '"' on Windows, so we don't need to worry + # about nuanced counting here. + return f'"{s}\\"' if s.endswith("\\") else f'"{s}"' +else: + + def quote(s: str) -> str: + return s + + +def print_includes() -> None: + dirs = [ + sysconfig.get_path("include"), + sysconfig.get_path("platinclude"), + get_include(), + ] + + # Make unique but preserve order + unique_dirs = [] + for d in dirs: + if d and d not in unique_dirs: + unique_dirs.append(d) + + print(" ".join(quote(f"-I{d}") for d in unique_dirs)) + + +def main() -> None: + make_parser = functools.partial(argparse.ArgumentParser, allow_abbrev=False) + if sys.version_info >= (3, 14): + make_parser = functools.partial(make_parser, color=True, suggest_on_error=True) + parser = make_parser() + parser.add_argument( + "--version", + action="version", + version=__version__, + help="Print the version and exit.", + ) + parser.add_argument( + "--includes", + action="store_true", + help="Include flags for both pybind11 and Python headers.", + ) + parser.add_argument( + "--cmakedir", + action="store_true", + help="Print the CMake module directory, ideal for setting -Dpybind11_ROOT in CMake.", + ) + parser.add_argument( + "--pkgconfigdir", + action="store_true", + help="Print the pkgconfig directory, ideal for setting $PKG_CONFIG_PATH.", + ) + parser.add_argument( + "--extension-suffix", + action="store_true", + help="Print the extension for a Python module", + ) + args = parser.parse_args() + if not sys.argv[1:]: + parser.print_help() + if args.includes: + print_includes() + if args.cmakedir: + print(quote(get_cmake_dir())) + if args.pkgconfigdir: + print(quote(get_pkgconfig_dir())) + if args.extension_suffix: + print(sysconfig.get_config_var("EXT_SUFFIX")) + + +if __name__ == "__main__": + main() diff --git a/external_libraries/pybind11/pybind11/_version.py b/external_libraries/pybind11/pybind11/_version.py new file mode 100644 index 00000000..d31cadb0 --- /dev/null +++ b/external_libraries/pybind11/pybind11/_version.py @@ -0,0 +1,34 @@ +# This file will be replaced in the wheel with a hard-coded version. This only +# exists to allow running directly from source without installing (not +# recommended, but supported). + +from __future__ import annotations + +import re +from pathlib import Path + +DIR = Path(__file__).parent.resolve() + +input_file = DIR.parent / "include/pybind11/detail/common.h" +regex = re.compile( + r""" +\#define \s+ PYBIND11_VERSION_MAJOR \s+ (?P\d+) .*? +\#define \s+ PYBIND11_VERSION_MINOR \s+ (?P\d+) .*? +\#define \s+ PYBIND11_VERSION_PATCH \s+ (?P\S+) +""", + re.MULTILINE | re.DOTALL | re.VERBOSE, +) + +match = regex.search(input_file.read_text(encoding="utf-8")) +assert match, "Unable to find version in pybind11/detail/common.h" +__version__ = "{major}.{minor}.{patch}".format(**match.groupdict()) + + +def _to_int(s: str) -> int | str: + try: + return int(s) + except ValueError: + return s + + +version_info = tuple(_to_int(s) for s in __version__.split(".")) diff --git a/external_libraries/pybind11/pybind11/commands.py b/external_libraries/pybind11/pybind11/commands.py new file mode 100644 index 00000000..d535b6cc --- /dev/null +++ b/external_libraries/pybind11/pybind11/commands.py @@ -0,0 +1,39 @@ +from __future__ import annotations + +import os + +DIR = os.path.abspath(os.path.dirname(__file__)) + + +def get_include(user: bool = False) -> str: # noqa: ARG001 + """ + Return the path to the pybind11 include directory. The historical "user" + argument is unused, and may be removed. + """ + installed_path = os.path.join(DIR, "include") + source_path = os.path.join(os.path.dirname(DIR), "include") + return installed_path if os.path.exists(installed_path) else source_path + + +def get_cmake_dir() -> str: + """ + Return the path to the pybind11 CMake module directory. + """ + cmake_installed_path = os.path.join(DIR, "share", "cmake", "pybind11") + if os.path.exists(cmake_installed_path): + return cmake_installed_path + + msg = "pybind11 not installed, installation required to access the CMake files" + raise ImportError(msg) + + +def get_pkgconfig_dir() -> str: + """ + Return the path to the pybind11 pkgconfig directory. + """ + pkgconfig_installed_path = os.path.join(DIR, "share", "pkgconfig") + if os.path.exists(pkgconfig_installed_path): + return pkgconfig_installed_path + + msg = "pybind11 not installed, installation required to access the pkgconfig files" + raise ImportError(msg) diff --git a/external_libraries/pybind11/pybind11/py.typed b/external_libraries/pybind11/pybind11/py.typed new file mode 100644 index 00000000..e69de29b diff --git a/external_libraries/pybind11/pybind11/setup_helpers.py b/external_libraries/pybind11/pybind11/setup_helpers.py new file mode 100644 index 00000000..f2429181 --- /dev/null +++ b/external_libraries/pybind11/pybind11/setup_helpers.py @@ -0,0 +1,500 @@ +""" +This module provides helpers for C++11+ projects using pybind11. + +LICENSE: + +Copyright (c) 2016 Wenzel Jakob , All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: + +1. Redistributions of source code must retain the above copyright notice, this + list of conditions and the following disclaimer. + +2. Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + +3. Neither the name of the copyright holder nor the names of its contributors + may be used to endorse or promote products derived from this software + without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND +ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE +FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR +SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER +CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, +OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +""" + +# IMPORTANT: If you change this file in the pybind11 repo, also review +# setup_helpers.pyi for matching changes. +# +# If you copy this file in, you don't +# need the .pyi file; it's just an interface file for static type checkers. +from __future__ import annotations + +import contextlib +import os +import platform +import shlex +import shutil +import sys +import sysconfig +import tempfile +import threading +import warnings +from functools import lru_cache +from pathlib import Path +from typing import ( + Any, + Callable, + Iterable, + Iterator, + List, + Optional, + Tuple, + TypeVar, + Union, +) + +try: + 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 ( # type: ignore[assignment] + build_ext as _build_ext, + ) + from distutils.extension import Extension as _Extension # type: ignore[assignment] + +import distutils.ccompiler +import distutils.errors + +WIN = sys.platform.startswith("win32") and "mingw" not in sysconfig.get_platform() +MACOS = sys.platform.startswith("darwin") +STD_TMPL = "/std:c++{}" if WIN else "-std=c++{}" + + +# It is recommended to use PEP 518 builds if using this module. However, this +# file explicitly supports being copied into a user's project directory +# standalone, and pulling pybind11 with the deprecated setup_requires feature. +# If you copy the file, remember to add it to your MANIFEST.in, and add the current +# directory into your path if it sits beside your setup.py. + + +class Pybind11Extension(_Extension): + """ + Build a C++11+ Extension module with pybind11. This automatically adds the + recommended flags when you init the extension and assumes C++ sources - you + can further modify the options yourself. + + The customizations are: + + * ``/EHsc`` and ``/bigobj`` on Windows + * ``stdlib=libc++`` on macOS + * ``visibility=hidden`` and ``-g0`` on Unix + + Finally, you can set ``cxx_std`` via constructor or afterwards to enable + flags for C++ std, and a few extra helper flags related to the C++ standard + level. It is _highly_ recommended you either set this, or use the provided + ``build_ext``, which will search for the highest supported extension for + you if the ``cxx_std`` property is not set. Do not set the ``cxx_std`` + property more than once, as flags are added when you set it. Set the + property to None to disable the addition of C++ standard flags. + + If you want to add pybind11 headers manually, for example for an exact + git checkout, then set ``include_pybind11=False``. + """ + + # flags are prepended, so that they can be further overridden, e.g. by + # ``extra_compile_args=["-g"]``. + + def _add_cflags(self, flags: list[str]) -> None: + self.extra_compile_args[:0] = flags + + def _add_ldflags(self, flags: list[str]) -> None: + self.extra_link_args[:0] = flags + + def __init__(self, *args: Any, **kwargs: Any) -> None: + self._cxx_level = 0 + cxx_std = kwargs.pop("cxx_std", 0) + + if "language" not in kwargs: + kwargs["language"] = "c++" + + include_pybind11 = kwargs.pop("include_pybind11", True) + + super().__init__(*args, **kwargs) + + # Include the installed package pybind11 headers + if include_pybind11: + # If using setup_requires, this fails the first time - that's okay + try: + import pybind11 + + pyinc = pybind11.get_include() + + if pyinc not in self.include_dirs: + self.include_dirs.append(pyinc) + except ModuleNotFoundError: + pass + + self.cxx_std = cxx_std + + cflags = [] + if WIN: + cflags += ["/EHsc", "/bigobj"] + else: + cflags += ["-fvisibility=hidden"] + env_cflags = os.environ.get("CFLAGS", "") + env_cppflags = os.environ.get("CPPFLAGS", "") + c_cpp_flags = shlex.split(env_cflags) + shlex.split(env_cppflags) + if not any(opt.startswith("-g") for opt in c_cpp_flags): + cflags += ["-g0"] + self._add_cflags(cflags) + + @property + def cxx_std(self) -> int: + """ + The CXX standard level. If set, will add the required flags. If left at + 0, it will trigger an automatic search when pybind11's build_ext is + used. If None, will have no effect. Besides just the flags, this may + add a macos-min 10.9 or 10.14 flag if MACOSX_DEPLOYMENT_TARGET is + unset. + """ + return self._cxx_level + + @cxx_std.setter + def cxx_std(self, level: int) -> None: + if self._cxx_level: + warnings.warn( + "You cannot safely change the cxx_level after setting it!", stacklevel=2 + ) + + # MSVC 2015 Update 3 and later only have 14 (and later 17) modes, so + # force a valid flag here. + if WIN and level == 11: + level = 14 + + self._cxx_level = level + + if not level: + return + + cflags = [STD_TMPL.format(level)] + ldflags = [] + + if MACOS and "MACOSX_DEPLOYMENT_TARGET" not in os.environ: + # C++17 requires a higher min version of macOS. An earlier version + # (10.12 or 10.13) can be set manually via environment variable if + # you are careful in your feature usage, but 10.14 is the safest + # setting for general use. However, never set higher than the + # current macOS version! + current_macos = tuple(int(x) for x in platform.mac_ver()[0].split(".")[:2]) + desired_macos = (10, 9) if level < 17 else (10, 14) + macos_string = ".".join(str(x) for x in min(current_macos, desired_macos)) + macosx_min = f"-mmacosx-version-min={macos_string}" + cflags += [macosx_min] + ldflags += [macosx_min] + + self._add_cflags(cflags) + self._add_ldflags(ldflags) + + +# Just in case someone clever tries to multithread +tmp_chdir_lock = threading.Lock() + + +@contextlib.contextmanager +def tmp_chdir() -> Iterator[str]: + "Prepare and enter a temporary directory, cleanup when done" + + # Threadsafe + with tmp_chdir_lock: + olddir = os.getcwd() + try: + tmpdir = tempfile.mkdtemp() + os.chdir(tmpdir) + yield tmpdir + finally: + os.chdir(olddir) + shutil.rmtree(tmpdir) + + +# cf http://bugs.python.org/issue26689 +def has_flag(compiler: Any, flag: str) -> bool: + """ + Return the flag if a flag name is supported on the + specified compiler, otherwise None (can be used as a boolean). + If multiple flags are passed, return the first that matches. + """ + + with tmp_chdir(): + fname = Path("flagcheck.cpp") + # Don't trigger -Wunused-parameter. + fname.write_text("int main (int, char **) { return 0; }", encoding="utf-8") + + try: + compiler.compile([str(fname)], extra_postargs=[flag]) + except distutils.errors.CompileError: + return False + return True + + +# Every call will cache the result +cpp_flag_cache = None + + +@lru_cache +def auto_cpp_level(compiler: Any) -> str | int: + """ + Return the max supported C++ std level (17, 14, or 11). Returns latest on Windows. + """ + + if WIN: + return "latest" + + levels = [17, 14, 11] + + for level in levels: + if has_flag(compiler, STD_TMPL.format(level)): + return level + + msg = "Unsupported compiler -- at least C++11 support is needed!" + raise RuntimeError(msg) + + +class build_ext(_build_ext): # noqa: N801 + """ + Customized build_ext that allows an auto-search for the highest supported + C++ level for Pybind11Extension. This is only needed for the auto-search + for now, and is completely optional otherwise. + """ + + def build_extensions(self) -> None: + """ + Build extensions, injecting C++ std for Pybind11Extension if needed. + """ + + for ext in self.extensions: + if hasattr(ext, "_cxx_level") and ext._cxx_level == 0: + ext.cxx_std = auto_cpp_level(self.compiler) + + super().build_extensions() + + +def intree_extensions( + paths: Iterable[str], package_dir: dict[str, str] | None = None +) -> list[Pybind11Extension]: + """ + Generate Pybind11Extensions from source files directly located in a Python + source tree. + + ``package_dir`` behaves as in ``setuptools.setup``. If unset, the Python + package root parent is determined as the first parent directory that does + not contain an ``__init__.py`` file. + """ + exts = [] + + if package_dir is None: + for path in paths: + parent, _ = os.path.split(path) + while os.path.exists(os.path.join(parent, "__init__.py")): + parent, _ = os.path.split(parent) + relname, _ = os.path.splitext(os.path.relpath(path, parent)) + qualified_name = relname.replace(os.path.sep, ".") + exts.append(Pybind11Extension(qualified_name, [path])) + return exts + + for path in paths: + for prefix, parent in package_dir.items(): + if path.startswith(parent): + relname, _ = os.path.splitext(os.path.relpath(path, parent)) + qualified_name = relname.replace(os.path.sep, ".") + if prefix: + qualified_name = prefix + "." + qualified_name + exts.append(Pybind11Extension(qualified_name, [path])) + break + else: + msg = ( + f"path {path} is not a child of any of the directories listed " + f"in 'package_dir' ({package_dir})" + ) + raise ValueError(msg) + + return exts + + +def naive_recompile(obj: str, src: str) -> bool: + """ + This will recompile only if the source file changes. It does not check + header files, so a more advanced function or Ccache is better if you have + editable header files in your package. + """ + return os.stat(obj).st_mtime < os.stat(src).st_mtime + + +def no_recompile(obg: str, src: str) -> bool: # noqa: ARG001 + """ + This is the safest but slowest choice (and is the default) - will always + recompile sources. + """ + return True + + +S = TypeVar("S", bound="ParallelCompile") + +CCompilerMethod = Callable[ + [ + distutils.ccompiler.CCompiler, + List[str], + Optional[str], + Optional[List[Union[Tuple[str], Tuple[str, Optional[str]]]]], + Optional[List[str]], + bool, + Optional[List[str]], + Optional[List[str]], + Optional[List[str]], + ], + List[str], +] + + +# Optional parallel compile utility +# inspired by: http://stackoverflow.com/questions/11013851/speeding-up-build-process-with-distutils +# and: https://github.com/tbenthompson/cppimport/blob/stable/cppimport/build_module.py +# and NumPy's parallel distutils module: +# https://github.com/numpy/numpy/blob/master/numpy/distutils/ccompiler.py +class ParallelCompile: + """ + Make a parallel compile function. Inspired by + numpy.distutils.ccompiler.CCompiler.compile and cppimport. + + This takes several arguments that allow you to customize the compile + function created: + + envvar: + Set an environment variable to control the compilation threads, like + NPY_NUM_BUILD_JOBS + default: + 0 will automatically multithread, or 1 will only multithread if the + envvar is set. + max: + The limit for automatic multithreading if non-zero + needs_recompile: + A function of (obj, src) that returns True when recompile is needed. No + effect in isolated mode; use ccache instead, see + https://github.com/matplotlib/matplotlib/issues/1507/ + + To use:: + + ParallelCompile("NPY_NUM_BUILD_JOBS").install() + + or:: + + with ParallelCompile("NPY_NUM_BUILD_JOBS"): + setup(...) + + By default, this assumes all files need to be recompiled. A smarter + function can be provided via needs_recompile. If the output has not yet + been generated, the compile will always run, and this function is not + called. + """ + + __slots__ = ("envvar", "default", "max", "_old", "needs_recompile") + + def __init__( + self, + envvar: str | None = None, + default: int = 0, + max: int = 0, # pylint: disable=redefined-builtin + needs_recompile: Callable[[str, str], bool] = no_recompile, + ) -> None: + self.envvar = envvar + self.default = default + self.max = max + self.needs_recompile = needs_recompile + self._old: list[CCompilerMethod] = [] + + def function(self) -> CCompilerMethod: + """ + Builds a function object usable as distutils.ccompiler.CCompiler.compile. + """ + + def compile_function( + compiler: distutils.ccompiler.CCompiler, + sources: list[str], + output_dir: str | None = None, + macros: list[tuple[str] | tuple[str, str | None]] | None = None, + include_dirs: list[str] | None = None, + debug: bool = False, + extra_preargs: list[str] | None = None, + extra_postargs: list[str] | None = None, + depends: list[str] | None = None, + ) -> Any: + # These lines are directly from distutils.ccompiler.CCompiler + macros, objects, extra_postargs, pp_opts, build = compiler._setup_compile( # type: ignore[attr-defined] + output_dir, macros, include_dirs, sources, depends, extra_postargs + ) + cc_args = compiler._get_cc_args(pp_opts, debug, extra_preargs) # type: ignore[attr-defined] + + # The number of threads; start with default. + threads = self.default + + # Determine the number of compilation threads, unless set by an environment variable. + if self.envvar is not None: + threads = int(os.environ.get(self.envvar, self.default)) + + def _single_compile(obj: Any) -> None: + try: + src, ext = build[obj] + except KeyError: + return + + if not os.path.exists(obj) or self.needs_recompile(obj, src): + compiler._compile(obj, src, ext, cc_args, extra_postargs, pp_opts) # type: ignore[attr-defined] + + try: + # Importing .synchronize checks for platforms that have some multiprocessing + # capabilities but lack semaphores, such as AWS Lambda and Android Termux. + import multiprocessing.synchronize + from multiprocessing.pool import ThreadPool + except ImportError: + threads = 1 + + if threads == 0: + try: + threads = multiprocessing.cpu_count() + threads = self.max if self.max and self.max < threads else threads + except NotImplementedError: + threads = 1 + + if threads > 1: + with ThreadPool(threads) as pool: + for _ in pool.imap_unordered(_single_compile, objects): + pass + else: + for ob in objects: + _single_compile(ob) + + return objects + + return compile_function + + def install(self: S) -> S: + """ + Installs the compile function into distutils.ccompiler.CCompiler.compile. + """ + distutils.ccompiler.CCompiler.compile = self.function() # type: ignore[assignment] + return self + + def __enter__(self: S) -> S: + self._old.append(distutils.ccompiler.CCompiler.compile) + return self.install() + + def __exit__(self, *args: Any) -> None: + distutils.ccompiler.CCompiler.compile = self._old.pop() # type: ignore[assignment] diff --git a/external_libraries/pybind11/pyproject.toml b/external_libraries/pybind11/pyproject.toml index 59c15ea6..03214c6f 100644 --- a/external_libraries/pybind11/pyproject.toml +++ b/external_libraries/pybind11/pyproject.toml @@ -1,49 +1,136 @@ [build-system] -requires = ["setuptools>=42", "cmake>=3.18", "ninja"] -build-backend = "setuptools.build_meta" +requires = ["scikit-build-core >=0.11.2"] +build-backend = "scikit_build_core.build" +[project] +name = "pybind11" +description = "Seamless operability between C++11 and Python" +authors = [{name = "Wenzel Jakob", email = "wenzel.jakob@epfl.ch"}] +license = "BSD-3-Clause" +license-files = ["LICENSE"] +readme = "README.rst" +classifiers = [ + "Development Status :: 5 - Production/Stable", + "Intended Audience :: Developers", + "Topic :: Software Development :: Libraries :: Python Modules", + "Topic :: Utilities", + "Programming Language :: C++", + "Programming Language :: Python :: 3 :: Only", + "Programming Language :: Python :: 3.8", + "Programming Language :: Python :: 3.9", + "Programming Language :: Python :: 3.10", + "Programming Language :: Python :: 3.11", + "Programming Language :: Python :: 3.12", + "Programming Language :: Python :: 3.13", + "Programming Language :: Python :: 3.14", + "Programming Language :: Python :: Implementation :: PyPy", + "Programming Language :: Python :: Implementation :: CPython", + "Programming Language :: C++", + "Topic :: Software Development :: Libraries :: Python Modules", +] +keywords = [ + "C++11", + "Python bindings", +] +dynamic = ["version", "optional-dependencies"] +requires-python = ">=3.8" -[tool.check-manifest] -ignore = [ - "tests/**", - "docs/**", - "tools/**", - "include/**", - ".*", - "pybind11/include/**", - "pybind11/share/**", - "CMakeLists.txt", - "noxfile.py", +[project.urls] +Homepage = "https://github.com/pybind/pybind11" +Documentation = "https://pybind11.readthedocs.io/" +"Issue Tracker" = "https://github.com/pybind/pybind11/issues" +Discussions = "https://github.com/pybind/pybind11/discussions" +Changelog = "https://pybind11.readthedocs.io/en/latest/changelog.html" +Chat = "https://gitter.im/pybind/Lobby" + +[project.scripts] +pybind11-config = "pybind11.__main__:main" + +[project.entry-points."pipx.run"] +pybind11 = "pybind11.__main__:main" + +[project.entry-points.pkg_config] +pybind11 = "pybind11.share.pkgconfig" + + +[dependency-groups] +test = [ + "pytest", + "build", +] +dev = [ + "tomlkit", + { include-group = "test" } ] +[tool.scikit-build] +minimum-version = "build-system.requires" +sdist.exclude = [ + "/docs/**", + "/.**", +] +wheel.install-dir = "pybind11" +wheel.platlib = false + +[tool.scikit-build.cmake.define] +BUILD_TESTING = false +PYBIND11_NOPYTHON = true +prefix_for_pc_file = "${pcfiledir}/../../" + +[tool.scikit-build.metadata.version] +provider = "scikit_build_core.metadata.regex" +input = "include/pybind11/detail/common.h" +regex = '''(?sx) +\#define \s+ PYBIND11_VERSION_MAJOR \s+ (?P\d+) .*? +\#define \s+ PYBIND11_VERSION_MINOR \s+ (?P\d+) .*? +\#define \s+ PYBIND11_VERSION_PATCH \s+ (?P\S+) +''' +result = "{major}.{minor}.{patch}" + +[tool.scikit-build.metadata.optional-dependencies] +provider = "scikit_build_core.metadata.template" +result = { global = ["pybind11-global=={project[version]}"]} + +[[tool.scikit-build.generate]] +path = "pybind11/_version.py" +template = ''' +from __future__ import annotations + + +def _to_int(s: str) -> int | str: + try: + return int(s) + except ValueError: + return s + + +__version__ = "$version" +version_info = tuple(_to_int(s) for s in __version__.split(".")) +''' + + +[tool.uv] +# Can't use tool.uv.sources with requirements.txt +index-strategy = "unsafe-best-match" +# This extra confuses uv +override-dependencies = ["pybind11-global"] + + [tool.mypy] files = ["pybind11"] -python_version = "3.6" +python_version = "3.8" strict = true -show_error_codes = true enable_error_code = ["ignore-without-code", "redundant-expr", "truthy-bool"] warn_unreachable = true [[tool.mypy.overrides]] -module = ["ghapi.*"] +module = ["ghapi.*", "tomlkit"] # tomlkit has types, but not very helpful ignore_missing_imports = true -[tool.pytest.ini_options] -minversion = "6.0" -addopts = ["-ra", "--showlocals", "--strict-markers", "--strict-config"] -xfail_strict = true -filterwarnings = ["error"] -log_cli_level = "info" -testpaths = [ - "tests", -] -timeout=300 - - [tool.pylint] -master.py-version = "3.6" +master.py-version = "3.8" reports.output-format = "colorized" messages_control.disable = [ "design", @@ -55,12 +142,11 @@ messages_control.disable = [ "protected-access", "missing-module-docstring", "unused-argument", # covered by Ruff ARG + "consider-using-f-string", # triggers in _version.py incorrectly ] - -[tool.ruff] -select = [ - "E", "F", "W", # flake8 +[tool.ruff.lint] +extend-select = [ "B", # flake8-bugbear "I", # isort "N", # pep8-naming @@ -69,6 +155,7 @@ select = [ "EM", # flake8-errmsg "ICN", # flake8-import-conventions "ISC", # flake8-implicit-str-concat + "PERF", # Perflint "PGH", # pygrep-hooks "PIE", # flake8-pie "PL", # pylint @@ -80,19 +167,49 @@ select = [ "YTT", # flake8-2020 ] ignore = [ - "PLR", # Design related pylint - "E501", # Line too long (Black is enough) - "PT011", # Too broad with raises in pytest - "PT004", # Fixture that doesn't return needs underscore (no, it is fine) - "SIM118", # iter(x) is not always the same as iter(x.keys()) + "PLR", # Design related pylint + "PT011", # Too broad with raises in pytest + "SIM118", # iter(x) is not always the same as iter(x.keys()) + "PLC0415", # We import in functions for various reasons ] -target-version = "py37" -src = ["src"] -unfixable = ["T20"] -exclude = [] -line-length = 120 isort.known-first-party = ["env", "pybind11_cross_module_tests", "pybind11_tests"] +isort.required-imports = ["from __future__ import annotations"] -[tool.ruff.per-file-ignores] -"tests/**" = ["EM", "N"] + +[tool.ruff.lint.per-file-ignores] +"tests/**" = [ + "EM", + "N", + "E721", +] "tests/test_call_policies.py" = ["PLC1901"] + +[tool.repo-review] +ignore = ["PP"] + +[tool.typos] +files.extend-exclude = ["/cpython"] + +[tool.typos.default.extend-identifiers] +ser_no = "ser_no" +SerNo = "SerNo" +StrLits = "StrLits" + +[tool.typos.default.extend-words] +nd = "nd" +valu = "valu" +fo = "fo" +quater = "quater" +optin = "optin" +othr = "othr" +# NumPy uses "writeable" in public API names and flags. +writeable = "writeable" +Writeable = "Writeable" +WRITEABLE = "WRITEABLE" + +#[tool.typos.type.cpp.extend-words] +setp = "setp" +ot = "ot" + +[tool.typos.type.json.extend-words] +ba = "ba" diff --git a/external_libraries/pybind11/setup.cfg b/external_libraries/pybind11/setup.cfg deleted file mode 100644 index 92e6c953..00000000 --- a/external_libraries/pybind11/setup.cfg +++ /dev/null @@ -1,43 +0,0 @@ -[metadata] -long_description = file: README.rst -long_description_content_type = text/x-rst -description = Seamless operability between C++11 and Python -author = Wenzel Jakob -author_email = wenzel.jakob@epfl.ch -url = https://github.com/pybind/pybind11 -license = BSD - -classifiers = - Development Status :: 5 - Production/Stable - Intended Audience :: Developers - Topic :: Software Development :: Libraries :: Python Modules - Topic :: Utilities - Programming Language :: C++ - Programming Language :: Python :: 3 :: Only - Programming Language :: Python :: 3.6 - Programming Language :: Python :: 3.7 - Programming Language :: Python :: 3.8 - Programming Language :: Python :: 3.9 - Programming Language :: Python :: 3.10 - Programming Language :: Python :: 3.11 - Programming Language :: Python :: 3.12 - License :: OSI Approved :: BSD License - Programming Language :: Python :: Implementation :: PyPy - Programming Language :: Python :: Implementation :: CPython - Programming Language :: C++ - Topic :: Software Development :: Libraries :: Python Modules - -keywords = - C++11 - Python bindings - -project_urls = - Documentation = https://pybind11.readthedocs.io/ - Bug Tracker = https://github.com/pybind/pybind11/issues - Discussions = https://github.com/pybind/pybind11/discussions - Changelog = https://pybind11.readthedocs.io/en/latest/changelog.html - Chat = https://gitter.im/pybind/Lobby - -[options] -python_requires = >=3.6 -zip_safe = False diff --git a/external_libraries/pybind11/setup.py b/external_libraries/pybind11/setup.py deleted file mode 100644 index 9fea7d35..00000000 --- a/external_libraries/pybind11/setup.py +++ /dev/null @@ -1,150 +0,0 @@ -#!/usr/bin/env python3 - -# Setup script for PyPI; use CMakeFile.txt to build extension modules - -import contextlib -import os -import re -import shutil -import string -import subprocess -import sys -from pathlib import Path -from tempfile import TemporaryDirectory -from typing import Dict, Iterator, List, Union - -import setuptools.command.sdist - -DIR = Path(__file__).parent.absolute() -VERSION_REGEX = re.compile( - r"^\s*#\s*define\s+PYBIND11_VERSION_([A-Z]+)\s+(.*)$", re.MULTILINE -) -VERSION_FILE = Path("pybind11/_version.py") -COMMON_FILE = Path("include/pybind11/detail/common.h") - - -def build_expected_version_hex(matches: Dict[str, str]) -> str: - patch_level_serial = matches["PATCH"] - serial = None - major = int(matches["MAJOR"]) - minor = int(matches["MINOR"]) - flds = patch_level_serial.split(".") - if flds: - patch = int(flds[0]) - if len(flds) == 1: - level = "0" - serial = 0 - elif len(flds) == 2: - level_serial = flds[1] - for level in ("a", "b", "c", "dev"): - if level_serial.startswith(level): - serial = int(level_serial[len(level) :]) - break - if serial is None: - msg = f'Invalid PYBIND11_VERSION_PATCH: "{patch_level_serial}"' - raise RuntimeError(msg) - version_hex_str = f"{major:02x}{minor:02x}{patch:02x}{level[:1]}{serial:x}" - return f"0x{version_hex_str.upper()}" - - -# PYBIND11_GLOBAL_SDIST will build a different sdist, with the python-headers -# files, and the sys.prefix files (CMake and headers). - -global_sdist = os.environ.get("PYBIND11_GLOBAL_SDIST", False) - -setup_py = Path( - "tools/setup_global.py.in" if global_sdist else "tools/setup_main.py.in" -) -extra_cmd = 'cmdclass["sdist"] = SDist\n' - -to_src = ( - (Path("pyproject.toml"), Path("tools/pyproject.toml")), - (Path("setup.py"), setup_py), -) - - -# Read the listed version -loc: Dict[str, str] = {} -code = compile(VERSION_FILE.read_text(encoding="utf-8"), "pybind11/_version.py", "exec") -exec(code, loc) -version = loc["__version__"] - -# Verify that the version matches the one in C++ -matches = dict(VERSION_REGEX.findall(COMMON_FILE.read_text(encoding="utf8"))) -cpp_version = "{MAJOR}.{MINOR}.{PATCH}".format(**matches) -if version != cpp_version: - msg = f"Python version {version} does not match C++ version {cpp_version}!" - raise RuntimeError(msg) - -version_hex = matches.get("HEX", "MISSING") -exp_version_hex = build_expected_version_hex(matches) -if version_hex != exp_version_hex: - msg = f"PYBIND11_VERSION_HEX {version_hex} does not match expected value {exp_version_hex}!" - raise RuntimeError(msg) - - -# TODO: use literals & overload (typing extensions or Python 3.8) -def get_and_replace( - filename: Path, binary: bool = False, **opts: str -) -> Union[bytes, str]: - if binary: - contents = filename.read_bytes() - return string.Template(contents.decode()).substitute(opts).encode() - - return string.Template(filename.read_text()).substitute(opts) - - -# Use our input files instead when making the SDist (and anything that depends -# on it, like a wheel) -class SDist(setuptools.command.sdist.sdist): - def make_release_tree(self, base_dir: str, files: List[str]) -> None: - super().make_release_tree(base_dir, files) - - for to, src in to_src: - txt = get_and_replace(src, binary=True, version=version, extra_cmd="") - - dest = Path(base_dir) / to - - # This is normally linked, so unlink before writing! - dest.unlink() - dest.write_bytes(txt) # type: ignore[arg-type] - - -# Remove the CMake install directory when done -@contextlib.contextmanager -def remove_output(*sources: str) -> Iterator[None]: - try: - yield - finally: - for src in sources: - shutil.rmtree(src) - - -with remove_output("pybind11/include", "pybind11/share"): - # Generate the files if they are not present. - with TemporaryDirectory() as tmpdir: - cmd = ["cmake", "-S", ".", "-B", tmpdir] + [ - "-DCMAKE_INSTALL_PREFIX=pybind11", - "-DBUILD_TESTING=OFF", - "-DPYBIND11_NOPYTHON=ON", - "-Dprefix_for_pc_file=${pcfiledir}/../../", - ] - if "CMAKE_ARGS" in os.environ: - fcommand = [ - c - for c in os.environ["CMAKE_ARGS"].split() - if "DCMAKE_INSTALL_PREFIX" not in c - ] - cmd += fcommand - subprocess.run(cmd, check=True, cwd=DIR, stdout=sys.stdout, stderr=sys.stderr) - subprocess.run( - ["cmake", "--install", tmpdir], - check=True, - cwd=DIR, - stdout=sys.stdout, - stderr=sys.stderr, - ) - - txt = get_and_replace(setup_py, version=version, extra_cmd=extra_cmd) - code = compile(txt, setup_py, "exec") - exec(code, {"SDist": SDist}) diff --git a/external_libraries/pybind11/tests/CMakeLists.txt b/external_libraries/pybind11/tests/CMakeLists.txt new file mode 100644 index 00000000..fc08a905 --- /dev/null +++ b/external_libraries/pybind11/tests/CMakeLists.txt @@ -0,0 +1,694 @@ +# CMakeLists.txt -- Build system for the pybind11 test suite +# +# Copyright (c) 2015 Wenzel Jakob +# +# All rights reserved. Use of this source code is governed by a +# BSD-style license that can be found in the LICENSE file. + +cmake_minimum_required(VERSION 3.15...4.2) + +# Filter out items; print an optional message if any items filtered. This ignores extensions. +# +# Usage: +# pybind11_filter_tests(LISTNAME file1.cpp file2.cpp ... MESSAGE "") +# +macro(pybind11_filter_tests LISTNAME) + cmake_parse_arguments(ARG "" "MESSAGE" "" ${ARGN}) + set(PYBIND11_FILTER_TESTS_FOUND OFF) + # Make a list of the test without any extensions, for easier filtering. + set(_TMP_ACTUAL_LIST "${${LISTNAME}};") # enforce ';' at the end to allow matching last item. + string(REGEX REPLACE "\\.[^.;]*;" ";" LIST_WITHOUT_EXTENSIONS "${_TMP_ACTUAL_LIST}") + foreach(filename IN LISTS ARG_UNPARSED_ARGUMENTS) + string(REGEX REPLACE "\\.[^.]*$" "" filename_no_ext ${filename}) + # Search in the list without extensions. + list(FIND LIST_WITHOUT_EXTENSIONS ${filename_no_ext} _FILE_FOUND) + if(_FILE_FOUND GREATER -1) + list(REMOVE_AT ${LISTNAME} ${_FILE_FOUND}) # And remove from the list with extensions. + list(REMOVE_AT LIST_WITHOUT_EXTENSIONS ${_FILE_FOUND} + )# And our search list, to ensure it is in sync. + set(PYBIND11_FILTER_TESTS_FOUND ON) + endif() + endforeach() + if(PYBIND11_FILTER_TESTS_FOUND AND ARG_MESSAGE) + message(STATUS "${ARG_MESSAGE}") + endif() +endmacro() + +macro(possibly_uninitialized) + foreach(VARNAME ${ARGN}) + if(NOT DEFINED "${VARNAME}") + set("${VARNAME}" "") + endif() + endforeach() +endmacro() + +# Function to add additional targets if any of the provided tests are found. +# Needles; Specifies the test names to look for. +# Additions; Specifies the additional test targets to add when any of the needles are found. +macro(tests_extra_targets needles additions) + # Add the index for this relation to the index extra targets map. + list(LENGTH PYBIND11_TEST_EXTRA_TARGETS PYBIND11_TEST_EXTRA_TARGETS_LEN) + list(APPEND PYBIND11_TEST_EXTRA_TARGETS ${PYBIND11_TEST_EXTRA_TARGETS_LEN}) + # Add the test names to look for, and the associated test target additions. + set(PYBIND11_TEST_EXTRA_TARGETS_NEEDLES_${PYBIND11_TEST_EXTRA_TARGETS_LEN} ${needles}) + set(PYBIND11_TEST_EXTRA_TARGETS_ADDITION_${PYBIND11_TEST_EXTRA_TARGETS_LEN} ${additions}) +endmacro() + +# New Python support +if(DEFINED Python_EXECUTABLE) + set(PYTHON_EXECUTABLE "${Python_EXECUTABLE}") + set(PYTHON_VERSION "${Python_VERSION}") +endif() + +# There's no harm in including a project in a project +project(pybind11_tests CXX) + +# Access FindCatch and more +list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/../tools") + +option(PYBIND11_WERROR "Report all warnings as errors" OFF) +option(DOWNLOAD_CATCH "Download catch2 if not found" OFF) +option(DOWNLOAD_EIGEN "Download EIGEN" OFF) +option(PYBIND11_CUDA_TESTS "Enable building CUDA tests" OFF) +option(PYBIND11_TEST_SMART_HOLDER "Change the default to smart holder" OFF) +set(PYBIND11_TEST_OVERRIDE + "" + CACHE STRING "Tests from ;-separated list of *.cpp files will be built instead of all tests") +set(PYBIND11_TEST_FILTER + "" + CACHE STRING "Tests from ;-separated list of *.cpp files will be removed from all tests") + +if(CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_SOURCE_DIR) + # We're being loaded directly, i.e. not via add_subdirectory, so make this + # work as its own project and load the pybind11Config to get the tools we need + + if(SKBUILD) + add_subdirectory(.. pybind11_src) + else() + find_package(pybind11 REQUIRED CONFIG) + endif() +endif() + +if(NOT CMAKE_BUILD_TYPE AND NOT DEFINED CMAKE_CONFIGURATION_TYPES) + message(STATUS "Setting tests build type to MinSizeRel as none was specified") + set(CMAKE_BUILD_TYPE + MinSizeRel + CACHE STRING "Choose the type of build." FORCE) + set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS "Debug" "Release" "MinSizeRel" + "RelWithDebInfo") +endif() + +if(PYBIND11_CUDA_TESTS) + enable_language(CUDA) + if(DEFINED CMAKE_CXX_STANDARD) + set(CMAKE_CUDA_STANDARD ${CMAKE_CXX_STANDARD}) + endif() + set(CMAKE_CUDA_STANDARD_REQUIRED ON) +endif() + +# Full set of test files (you can override these; see below, overrides ignore extension) +# Any test that has no extension is both .py and .cpp, so 'foo' will add 'foo.cpp' and 'foo.py'. +# Any test that has an extension is exclusively that and handled as such. +set(PYBIND11_TEST_FILES + test_async + test_buffers + test_builtin_casters + test_call_policies + test_callbacks + test_chrono + test_class + test_class_cross_module_use_after_one_module_dealloc + test_class_release_gil_before_calling_cpp_dtor + test_class_sh_basic + test_class_sh_disowning + test_class_sh_disowning_mi + test_class_sh_factory_constructors + test_class_sh_inheritance + test_class_sh_mi_thunks + test_class_sh_property + test_class_sh_property_non_owning + test_class_sh_shared_ptr_copy_move + test_class_sh_trampoline_basic + test_class_sh_trampoline_self_life_support + test_class_sh_trampoline_shared_from_this + test_class_sh_trampoline_shared_ptr_cpp_arg + test_class_sh_trampoline_unique_ptr + test_class_sh_unique_ptr_custom_deleter + test_class_sh_unique_ptr_member + test_class_sh_virtual_py_cpp_mix + test_const_name + test_constants_and_functions + test_copy_move + test_cpp_conduit + test_custom_type_casters + test_custom_type_setup + test_docstring_options + test_docs_advanced_cast_custom + test_eigen_matrix + test_eigen_tensor + test_enum + test_eval + test_exceptions + test_factory_constructors + test_gil_scoped + test_iostream + test_kwargs_and_defaults + test_local_bindings + test_methods_and_attributes + test_modules + test_multiple_inheritance + test_multiple_interpreters.py + test_native_enum + test_numpy_array + test_numpy_dtypes + test_numpy_scalars + test_numpy_vectorize + test_opaque_types + test_operator_overloading + test_pickling + test_potentially_slicing_weak_ptr + test_pytorch_shared_ptr_cast_regression + test_python_multiple_inheritance + test_pytypes + test_scoped_critical_section + test_sequences_and_iterators + test_smart_ptr + test_standalone_enum_module.py + test_stl + test_stl_binders + test_tagbased_polymorphic + test_thread + test_type_caster_pyobject_ptr + test_type_caster_std_function_specializations + test_union + test_unnamed_namespace_a + test_unnamed_namespace_b + test_vector_unique_ptr_member + test_virtual_functions + test_warnings) + +# Invoking cmake with something like: +# cmake -DPYBIND11_TEST_OVERRIDE="test_callbacks.cpp;test_pickling.cpp" .. +# lets you override the tests that get compiled and run. You can restore to all tests with: +# cmake -DPYBIND11_TEST_OVERRIDE= .. +if(PYBIND11_TEST_OVERRIDE) + # Instead of doing a direct override here, we iterate over the overrides without extension and + # match them against entries from the PYBIND11_TEST_FILES, anything that not matches goes into the filter list. + string(REGEX REPLACE "\\.[^.;]*;" ";" TEST_OVERRIDE_NO_EXT "${PYBIND11_TEST_OVERRIDE};") + string(REGEX REPLACE "\\.[^.;]*;" ";" TEST_FILES_NO_EXT "${PYBIND11_TEST_FILES};") + # This allows the override to be done with extensions, preserving backwards compatibility. + foreach(test_name ${TEST_FILES_NO_EXT}) + if(NOT ${test_name} IN_LIST TEST_OVERRIDE_NO_EXT + )# If not in the allowlist, add to be filtered out. + list(APPEND PYBIND11_TEST_FILTER ${test_name}) + endif() + endforeach() +endif() + +# You can also filter tests: +if(PYBIND11_TEST_FILTER) + pybind11_filter_tests(PYBIND11_TEST_FILES ${PYBIND11_TEST_FILTER}) +endif() + +# Skip tests for CUDA check: +# /pybind11/tests/test_constants_and_functions.cpp(125): +# error: incompatible exception specifications +if(PYBIND11_CUDA_TESTS) + pybind11_filter_tests( + PYBIND11_TEST_FILES test_constants_and_functions.cpp MESSAGE + "Skipping test_constants_and_functions due to incompatible exception specifications") +endif() + +# Now that the test filtering is complete, we need to split the list into the test for PYTEST +# and the list for the cpp targets. +set(PYBIND11_CPPTEST_FILES "") +set(PYBIND11_PYTEST_FILES "") + +foreach(test_name ${PYBIND11_TEST_FILES}) + if(test_name MATCHES "\\.py$") # Ends in .py, purely python test. + list(APPEND PYBIND11_PYTEST_FILES ${test_name}) + elseif(test_name MATCHES "\\.cpp$") # Ends in .cpp, purely cpp test. + list(APPEND PYBIND11_CPPTEST_FILES ${test_name}) + elseif(NOT test_name MATCHES "\\.") # No extension specified, assume both, add extension. + list(APPEND PYBIND11_PYTEST_FILES ${test_name}.py) + list(APPEND PYBIND11_CPPTEST_FILES ${test_name}.cpp) + else() + message(WARNING "Unhanded test extension in test: ${test_name}") + endif() +endforeach() +set(PYBIND11_TEST_FILES ${PYBIND11_CPPTEST_FILES}) +list(SORT PYBIND11_PYTEST_FILES) + +# Contains the set of test files that require pybind11_cross_module_tests to be +# built; if none of these are built (i.e. because TEST_OVERRIDE is used and +# doesn't include them) the second module doesn't get built. +tests_extra_targets( + "test_class_cross_module_use_after_one_module_dealloc.py;test_exceptions.py;test_local_bindings.py;test_stl.py;test_stl_binders.py" + "pybind11_cross_module_tests") + +# And add additional targets for other tests. +tests_extra_targets("test_exceptions.py" "cross_module_interleaved_error_already_set") +tests_extra_targets("test_gil_scoped.py" "cross_module_gil_utils") +tests_extra_targets("test_cpp_conduit.py" + "exo_planet_pybind11;exo_planet_c_api;home_planet_very_lonely_traveler") +tests_extra_targets("test_standalone_enum_module.py" "standalone_enum_module") + +set(PYBIND11_EIGEN_REPO + "https://gitlab.com/libeigen/eigen.git" + CACHE STRING "Eigen repository to use for tests") +# Always use a hash for reconfigure speed and security reasons +# Include the version number for pretty printing (keep in sync) +set(PYBIND11_EIGEN_VERSION_AND_HASH + "3.4.0;929bc0e191d0927b1735b9a1ddc0e8b77e3a25ec" + CACHE STRING "Eigen version to use for tests, format: VERSION;HASH") + +list(GET PYBIND11_EIGEN_VERSION_AND_HASH 0 PYBIND11_EIGEN_VERSION_STRING) +list(GET PYBIND11_EIGEN_VERSION_AND_HASH 1 PYBIND11_EIGEN_VERSION_HASH) + +# Check if Eigen is available; if not, remove from PYBIND11_TEST_FILES (but +# keep it in PYBIND11_PYTEST_FILES, so that we get the "eigen is not installed" +# skip message). +list(FIND PYBIND11_TEST_FILES test_eigen_matrix.cpp PYBIND11_TEST_FILES_EIGEN_I) +if(PYBIND11_TEST_FILES_EIGEN_I EQUAL -1) + list(FIND PYBIND11_TEST_FILES test_eigen_tensor.cpp PYBIND11_TEST_FILES_EIGEN_I) +endif() +if(PYBIND11_TEST_FILES_EIGEN_I GREATER -1) + # Try loading via newer Eigen's Eigen3Config first (bypassing tools/FindEigen3.cmake). + # Eigen 3.3.1+ exports a cmake 3.0+ target for handling dependency requirements, but also + if(DOWNLOAD_EIGEN) + if(CMAKE_VERSION VERSION_LESS 3.18) + set(_opts) + else() + set(_opts SOURCE_SUBDIR no-cmake-build) + endif() + include(FetchContent) + FetchContent_Declare( + eigen + GIT_REPOSITORY "${PYBIND11_EIGEN_REPO}" + GIT_TAG "${PYBIND11_EIGEN_VERSION_HASH}" + ${_opts}) + FetchContent_MakeAvailable(eigen) + if(NOT CMAKE_VERSION VERSION_LESS 3.18) + set(EIGEN3_INCLUDE_DIR "${eigen_SOURCE_DIR}") + endif() + + set(EIGEN3_INCLUDE_DIR ${eigen_SOURCE_DIR}) + set(EIGEN3_FOUND TRUE) + # When getting locally, the version is not visible from a superprojet, + # so just force it. + set(EIGEN3_VERSION "${PYBIND11_EIGEN_VERSION_STRING}") + + else() + find_package(Eigen3 3.2.7 QUIET CONFIG) + if(NOT Eigen3_FOUND) + find_package(Eigen3 5 QUIET CONFIG) + endif() + set(EIGEN3_FOUND ${Eigen3_FOUND}) + set(EIGEN3_VERSION ${Eigen3_VERSION}) + + if(NOT EIGEN3_FOUND) + # Couldn't load via target, so fall back to allowing module mode finding, which will pick up + # tools/FindEigen3.cmake + # This MODULE-mode fallback is for older Eigen 3 setups; Eigen 5 is expected to be found + # via the CONFIG-mode probes above. + find_package(Eigen3 3.2.7 QUIET) + endif() + endif() + + if(EIGEN3_FOUND) + if(NOT TARGET Eigen3::Eigen) + add_library(Eigen3::Eigen IMPORTED INTERFACE) + set_property(TARGET Eigen3::Eigen PROPERTY INTERFACE_INCLUDE_DIRECTORIES + "${EIGEN3_INCLUDE_DIR}") + endif() + + # Eigen 3.3.1+ cmake sets EIGEN3_VERSION_STRING (and hard codes the version when installed + # rather than looking it up in the cmake script); older versions, and the + # tools/FindEigen3.cmake, set EIGEN3_VERSION instead. + if(NOT EIGEN3_VERSION AND EIGEN3_VERSION_STRING) + set(EIGEN3_VERSION ${EIGEN3_VERSION_STRING}) + endif() + message(STATUS "Building tests with Eigen v${EIGEN3_VERSION}") + + if(NOT (CMAKE_CXX_COMPILER_ID STREQUAL "GNU" AND CMAKE_CXX_COMPILER_VERSION VERSION_LESS 5.0)) + tests_extra_targets("test_eigen_tensor.py" "eigen_tensor_avoid_stl_array") + endif() + + else() + list(FIND PYBIND11_TEST_FILES test_eigen_matrix.cpp PYBIND11_TEST_FILES_EIGEN_I) + if(PYBIND11_TEST_FILES_EIGEN_I GREATER -1) + list(REMOVE_AT PYBIND11_TEST_FILES ${PYBIND11_TEST_FILES_EIGEN_I}) + endif() + + list(FIND PYBIND11_TEST_FILES test_eigen_tensor.cpp PYBIND11_TEST_FILES_EIGEN_I) + if(PYBIND11_TEST_FILES_EIGEN_I GREATER -1) + list(REMOVE_AT PYBIND11_TEST_FILES ${PYBIND11_TEST_FILES_EIGEN_I}) + endif() + message(STATUS "Building tests WITHOUT Eigen, use -DDOWNLOAD_EIGEN=ON to download") + endif() +endif() + +# Some code doesn't support gcc 4 +if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU" AND CMAKE_CXX_COMPILER_VERSION VERSION_LESS 5.0) + list(FIND PYBIND11_TEST_FILES test_eigen_tensor.cpp PYBIND11_TEST_FILES_EIGEN_I) + if(PYBIND11_TEST_FILES_EIGEN_I GREATER -1) + list(REMOVE_AT PYBIND11_TEST_FILES ${PYBIND11_TEST_FILES_EIGEN_I}) + endif() +endif() + +# Optional dependency for some tests (boost::variant is only supported with version >= 1.56) +find_package(Boost 1.56) + +if(Boost_FOUND) + if(NOT TARGET Boost::headers) + add_library(Boost::headers IMPORTED INTERFACE) + if(TARGET Boost::boost) + # Classic FindBoost + set_property(TARGET Boost::headers PROPERTY INTERFACE_LINK_LIBRARIES Boost::boost) + else() + # Very old FindBoost, or newer Boost than CMake in older CMakes + set_property(TARGET Boost::headers PROPERTY INTERFACE_INCLUDE_DIRECTORIES + ${Boost_INCLUDE_DIRS}) + endif() + endif() +endif() + +# Check if we need to add -lstdc++fs or -lc++fs or nothing +if(DEFINED CMAKE_CXX_STANDARD AND CMAKE_CXX_STANDARD LESS 17) + set(STD_FS_NO_LIB_NEEDED TRUE) +elseif(MSVC) + set(STD_FS_NO_LIB_NEEDED TRUE) +else() + file( + WRITE ${CMAKE_CURRENT_BINARY_DIR}/main.cpp + "#include \nint main(int /*argc*/, char ** argv) {\n std::filesystem::path p(argv[0]);\n return p.string().length();\n}" + ) + try_compile( + STD_FS_NO_LIB_NEEDED ${CMAKE_CURRENT_BINARY_DIR} + SOURCES ${CMAKE_CURRENT_BINARY_DIR}/main.cpp + COMPILE_DEFINITIONS -std=c++17) + try_compile( + STD_FS_NEEDS_STDCXXFS ${CMAKE_CURRENT_BINARY_DIR} + SOURCES ${CMAKE_CURRENT_BINARY_DIR}/main.cpp + COMPILE_DEFINITIONS -std=c++17 + LINK_LIBRARIES stdc++fs) + try_compile( + STD_FS_NEEDS_CXXFS ${CMAKE_CURRENT_BINARY_DIR} + SOURCES ${CMAKE_CURRENT_BINARY_DIR}/main.cpp + COMPILE_DEFINITIONS -std=c++17 + LINK_LIBRARIES c++fs) +endif() + +if(${STD_FS_NEEDS_STDCXXFS}) + set(STD_FS_LIB stdc++fs) +elseif(${STD_FS_NEEDS_CXXFS}) + set(STD_FS_LIB c++fs) +elseif(${STD_FS_NO_LIB_NEEDED}) + set(STD_FS_LIB "") +else() + message(WARNING "Unknown C++17 compiler - not passing -lstdc++fs") + set(STD_FS_LIB "") +endif() + +# Compile with compiler warnings turned on +function(pybind11_enable_warnings target_name) + if(MSVC) + 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} + PRIVATE -Wall + -Wextra + -Wconversion + -Wcast-qual + -Wdeprecated + -Wundef + -Wnon-virtual-dtor) + if(DEFINED CMAKE_CXX_STANDARD AND NOT CMAKE_CXX_STANDARD VERSION_LESS 20) + target_compile_options(${target_name} PRIVATE -Wpedantic) + endif() + endif() + + if(PYBIND11_WERROR) + if(CMAKE_CXX_COMPILER_ID STREQUAL "Intel") + if(CMAKE_CXX_STANDARD EQUAL 17) # See PR #3570 + target_compile_options(${target_name} PRIVATE -Wno-conversion) + endif() + # "Inlining inhibited by limit max-size", "Inlining inhibited by limit max-total-size" + target_compile_options(${target_name} PRIVATE -diag-disable 11074,11076) + endif() + + if(CMAKE_VERSION VERSION_LESS "3.24") + if(MSVC) + target_compile_options(${target_name} PRIVATE /WX) + elseif(PYBIND11_CUDA_TESTS) + target_compile_options(${target_name} PRIVATE "SHELL:-Werror all-warnings") + elseif(CMAKE_CXX_COMPILER_ID MATCHES "(GNU|Clang|IntelLLVM)") + target_compile_options(${target_name} PRIVATE -Werror) + elseif(CMAKE_CXX_COMPILER_ID STREQUAL "Intel") + target_compile_options(${target_name} PRIVATE -Werror-all) + endif() + else() + set_target_properties(${target_name} PROPERTIES COMPILE_WARNING_AS_ERROR ON) + endif() + endif() +endfunction() + +set(test_targets pybind11_tests) + +# Check if any tests need extra targets by iterating through the mappings registered. +foreach(i ${PYBIND11_TEST_EXTRA_TARGETS}) + foreach(needle ${PYBIND11_TEST_EXTRA_TARGETS_NEEDLES_${i}}) + if(needle IN_LIST PYBIND11_PYTEST_FILES) + # Add all the additional targets to the test list. List join in newer cmake. + foreach(extra_target ${PYBIND11_TEST_EXTRA_TARGETS_ADDITION_${i}}) + list(APPEND test_targets ${extra_target}) + endforeach() + break() # Breaks out of the needle search, continues with the next mapping. + endif() + endforeach() +endforeach() + +# Support CUDA testing by forcing the target file to compile with NVCC +if(PYBIND11_CUDA_TESTS) + set_property(SOURCE ${PYBIND11_TEST_FILES} PROPERTY LANGUAGE CUDA) +endif() + +foreach(target ${test_targets}) + set(test_files ${PYBIND11_TEST_FILES}) + if(NOT "${target}" STREQUAL "pybind11_tests") + set(test_files "") + endif() + + # Support CUDA testing by forcing the target file to compile with NVCC + if(PYBIND11_CUDA_TESTS) + set_property(SOURCE ${target}.cpp PROPERTY LANGUAGE CUDA) + endif() + + # Create the binding library + pybind11_add_module(${target} THIN_LTO ${target}.cpp ${test_files} ${PYBIND11_HEADERS}) + pybind11_enable_warnings(${target}) + + if(NOT CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_CURRENT_BINARY_DIR) + get_property( + suffix + TARGET ${target} + PROPERTY SUFFIX) + set(source_output "${CMAKE_CURRENT_SOURCE_DIR}/${target}${suffix}") + if(suffix AND EXISTS "${source_output}") + message(WARNING "Output file also in source directory; " + "please remove to avoid confusion: ${source_output}") + endif() + endif() + + if(MSVC) + target_compile_options(${target} PRIVATE /utf-8) + endif() + + if(EIGEN3_FOUND) + target_link_libraries(${target} PRIVATE Eigen3::Eigen) + target_compile_definitions(${target} PRIVATE -DPYBIND11_TEST_EIGEN) + endif() + + if(Boost_FOUND) + target_link_libraries(${target} PRIVATE Boost::headers) + target_compile_definitions(${target} PRIVATE -DPYBIND11_TEST_BOOST) + endif() + + if(PYBIND11_TEST_SMART_HOLDER) + target_compile_definitions( + ${target} + PUBLIC -DPYBIND11_RUN_TESTING_WITH_SMART_HOLDER_AS_DEFAULT_BUT_NEVER_USE_IN_PRODUCTION_PLEASE + ) + endif() + + target_link_libraries(${target} PRIVATE ${STD_FS_LIB}) + + # Always write the output file directly into the 'tests' directory (even on MSVC) + if(NOT CMAKE_LIBRARY_OUTPUT_DIRECTORY) + set_target_properties(${target} PROPERTIES LIBRARY_OUTPUT_DIRECTORY + "${CMAKE_CURRENT_BINARY_DIR}") + + if(DEFINED CMAKE_CONFIGURATION_TYPES) + foreach(config ${CMAKE_CONFIGURATION_TYPES}) + string(TOUPPER ${config} config) + set_target_properties(${target} PROPERTIES LIBRARY_OUTPUT_DIRECTORY_${config} + "${CMAKE_CURRENT_BINARY_DIR}") + endforeach() + endif() + endif() + if(SKBUILD) + install(TARGETS ${target} LIBRARY DESTINATION .) + endif() + + if("${target}" STREQUAL "exo_planet_c_api") + if(CMAKE_CXX_COMPILER_ID MATCHES "(GNU|Intel|Clang|NVHPC)") + target_compile_options(${target} PRIVATE -fno-exceptions) + endif() + endif() +endforeach() + +# Provide nice organisation in IDEs +source_group( + TREE "${CMAKE_CURRENT_SOURCE_DIR}/../include" + PREFIX "Header Files" + FILES ${PYBIND11_HEADERS}) + +# Make sure pytest is found or produce a warning +if(NOT CMAKE_CROSSCOMPILING) + pybind11_find_import(pytest VERSION 3.1) +endif() + +if(NOT CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_CURRENT_BINARY_DIR) + # This is not used later in the build, so it's okay to regenerate each time. + configure_file("${CMAKE_CURRENT_SOURCE_DIR}/pytest.ini" "${CMAKE_CURRENT_BINARY_DIR}/pytest.ini" + COPYONLY) + file(APPEND "${CMAKE_CURRENT_BINARY_DIR}/pytest.ini" + "\ntestpaths = \"${CMAKE_CURRENT_SOURCE_DIR}\"") + +endif() + +# Convert relative to full file names +list(TRANSFORM PYBIND11_PYTEST_FILES PREPEND "${CMAKE_CURRENT_SOURCE_DIR}/" + OUTPUT_VARIABLE PYBIND11_ABS_PYTEST_FILES) + +set(PYBIND11_TEST_PREFIX_COMMAND + "" + CACHE STRING "Put this before pytest, use for checkers and such") + +set(PYBIND11_PYTEST_ARGS + "" + CACHE STRING "Extra arguments for pytest") + +# A single command to compile and run the tests +add_custom_target( + pytest + COMMAND ${PYBIND11_TEST_PREFIX_COMMAND} ${PYTHON_EXECUTABLE} -m pytest + ${PYBIND11_ABS_PYTEST_FILES} ${PYBIND11_PYTEST_ARGS} + DEPENDS ${test_targets} + WORKING_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}" + USES_TERMINAL) + +if(NOT PYBIND11_CUDA_TESTS) + set(PYBIND11_MULTIPLE_INTERPRETERS_TEST_MODULES + mod_per_interpreter_gil mod_shared_interpreter_gil mod_per_interpreter_gil_with_singleton) + + # These modules don't get mixed with other test modules because those aren't subinterpreter safe. + foreach(mod IN LISTS PYBIND11_MULTIPLE_INTERPRETERS_TEST_MODULES) + pybind11_add_module("${mod}" THIN_LTO "${mod}.cpp") + pybind11_enable_warnings("${mod}") + endforeach() + + # Put the built modules next to `pybind11_tests.so` so that the test scripts can find them. + get_target_property(pybind11_tests_output_directory pybind11_tests LIBRARY_OUTPUT_DIRECTORY) + foreach(mod IN LISTS PYBIND11_MULTIPLE_INTERPRETERS_TEST_MODULES) + set_target_properties("${mod}" PROPERTIES LIBRARY_OUTPUT_DIRECTORY + "${pybind11_tests_output_directory}") + # Also set config-specific output directories for multi-configuration generators (MSVC) + if(DEFINED CMAKE_CONFIGURATION_TYPES) + foreach(config ${CMAKE_CONFIGURATION_TYPES}) + string(TOUPPER ${config} config) + set_target_properties("${mod}" PROPERTIES LIBRARY_OUTPUT_DIRECTORY_${config} + "${pybind11_tests_output_directory}") + endforeach() + endif() + endforeach() + + if(SKBUILD) + foreach(mod IN LISTS PYBIND11_MULTIPLE_INTERPRETERS_TEST_MODULES) + install(TARGETS "${mod}" LIBRARY DESTINATION .) + endforeach() + endif() + + if(PYBIND11_TEST_SMART_HOLDER) + foreach(mod IN LISTS PYBIND11_MULTIPLE_INTERPRETERS_TEST_MODULES) + target_compile_definitions( + "${mod}" + PUBLIC + -DPYBIND11_RUN_TESTING_WITH_SMART_HOLDER_AS_DEFAULT_BUT_NEVER_USE_IN_PRODUCTION_PLEASE) + endforeach() + endif() + + add_dependencies(pytest ${PYBIND11_MULTIPLE_INTERPRETERS_TEST_MODULES}) +endif() + +if(PYBIND11_TEST_OVERRIDE) + add_custom_command( + TARGET pytest + POST_BUILD + COMMAND ${CMAKE_COMMAND} -E echo + "Note: not all tests run: -DPYBIND11_TEST_OVERRIDE is in effect") +endif() + +# cmake-format: off +add_custom_target( + memcheck + COMMAND + PYTHONMALLOC=malloc + valgrind + --leak-check=full + --show-leak-kinds=definite,indirect + --errors-for-leak-kinds=definite,indirect + --error-exitcode=1 + --read-var-info=yes + --track-origins=yes + --suppressions="${CMAKE_CURRENT_SOURCE_DIR}/valgrind-python.supp" + --suppressions="${CMAKE_CURRENT_SOURCE_DIR}/valgrind-numpy-scipy.supp" + --gen-suppressions=all + ${PYTHON_EXECUTABLE} -m pytest ${PYBIND11_ABS_PYTEST_FILES} + DEPENDS ${test_targets} + WORKING_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}" + USES_TERMINAL) +# cmake-format: on + +# Add a check target to run all the tests, starting with pytest (we add dependencies to this below) +add_custom_target(check DEPENDS pytest) + +# The remaining tests only apply when being built as part of the pybind11 project, but not if the +# tests are being built independently. +if(CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_SOURCE_DIR) + return() +endif() + +# Add a post-build comment to show the primary test suite .so size and, if a previous size, compare it: +add_custom_command( + TARGET pybind11_tests + POST_BUILD + COMMAND + ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/../tools/libsize.py + $ + ${CMAKE_CURRENT_BINARY_DIR}/sosize-$.txt) + +if(NOT PYBIND11_CUDA_TESTS) + # Test pure C++ code (not depending on Python). Provides the `test_pure_cpp` target. + add_subdirectory(pure_cpp) + + # Test C++ code that depends on Python, such as embedding the interpreter. Provides the `cpptest` target. + add_subdirectory(test_with_catch) + + # Test CMake build using functions and targets from subdirectory or installed location + add_subdirectory(test_cmake_build) + + # Test visibility of common symbols across shared libraries + add_subdirectory(test_cross_module_rtti) +endif() diff --git a/external_libraries/pybind11/tests/conftest.py b/external_libraries/pybind11/tests/conftest.py new file mode 100644 index 00000000..9d9815b8 --- /dev/null +++ b/external_libraries/pybind11/tests/conftest.py @@ -0,0 +1,313 @@ +"""pytest configuration + +Extends output capture as needed by pybind11: ignore constructors, optional unordered lines. +Adds docstring and exceptions message sanitizers. +""" + +from __future__ import annotations + +import contextlib +import difflib +import gc +import importlib.metadata +import multiprocessing +import re +import sys +import sysconfig +import textwrap +import traceback +import weakref +from typing import Callable + +import pytest + +# Early diagnostic for failed imports +try: + import pybind11_tests +except Exception: + # pytest does not show the traceback without this. + traceback.print_exc() + raise + + +@pytest.fixture(scope="session", autouse=True) +def use_multiprocessing_forkserver_on_linux(): + if sys.platform != "linux" or sys.implementation.name == "graalpy": + # The default on Windows, macOS and GraalPy is "spawn": If it's not broken, don't fix it. + return + + # Full background: https://github.com/pybind/pybind11/issues/4105#issuecomment-1301004592 + # In a nutshell: fork() after starting threads == flakiness in the form of deadlocks. + # It is actually a well-known pitfall, unfortunately without guard rails. + # "forkserver" is more performant than "spawn" (~9s vs ~13s for tests/test_gil_scoped.py, + # visit the issuecomment link above for details). + multiprocessing.set_start_method("forkserver") + + +_long_marker = re.compile(r"([0-9])L") +_hexadecimal = re.compile(r"0x[0-9a-fA-F]+") + +# Avoid collecting Python3 only files +collect_ignore = [] + + +def _strip_and_dedent(s): + """For triple-quote strings""" + return textwrap.dedent(s.lstrip("\n").rstrip()) + + +def _split_and_sort(s): + """For output which does not require specific line order""" + return sorted(_strip_and_dedent(s).splitlines()) + + +def _make_explanation(a, b): + """Explanation for a failed assert -- the a and b arguments are List[str]""" + return ["--- actual / +++ expected"] + [ + line.strip("\n") for line in difflib.ndiff(a, b) + ] + + +class Output: + """Basic output post-processing and comparison""" + + def __init__(self, string): + self.string = string + self.explanation = [] + + def __str__(self): + return self.string + + __hash__ = None + + def __eq__(self, other): + # Ignore constructor/destructor output which is prefixed with "###" + a = [ + line + for line in self.string.strip().splitlines() + if not line.startswith("###") + ] + b = _strip_and_dedent(other).splitlines() + if a == b: + return True + self.explanation = _make_explanation(a, b) + return False + + +class Unordered(Output): + """Custom comparison for output without strict line ordering""" + + __hash__ = None + + def __eq__(self, other): + a = _split_and_sort(self.string) + b = _split_and_sort(other) + if a == b: + return True + self.explanation = _make_explanation(a, b) + return False + + +class Capture: + def __init__(self, capfd): + self.capfd = capfd + self.out = "" + self.err = "" + + def __enter__(self): + self.capfd.readouterr() + return self + + def __exit__(self, *args): + self.out, self.err = self.capfd.readouterr() + + __hash__ = None + + def __eq__(self, other): + a = Output(self.out) + b = other + if a == b: + return True + self.explanation = a.explanation + return False + + def __str__(self): + return self.out + + def __contains__(self, item): + return item in self.out + + @property + def unordered(self): + return Unordered(self.out) + + @property + def stderr(self): + return Output(self.err) + + +@pytest.fixture +def capture(capsys): + """Extended `capsys` with context manager and custom equality operators""" + return Capture(capsys) + + +class SanitizedString: + def __init__(self, sanitizer): + self.sanitizer = sanitizer + self.string = "" + self.explanation = [] + + def __call__(self, thing): + self.string = self.sanitizer(thing) + return self + + __hash__ = None + + def __eq__(self, other): + a = self.string + b = _strip_and_dedent(other) + if a == b: + return True + self.explanation = _make_explanation(a.splitlines(), b.splitlines()) + return False + + +def _sanitize_general(s): + s = s.strip() + s = s.replace("pybind11_tests.", "m.") + return _long_marker.sub(r"\1", s) + + +def _sanitize_docstring(thing): + s = thing.__doc__ + return _sanitize_general(s) + + +@pytest.fixture +def doc(): + """Sanitize docstrings and add custom failure explanation""" + return SanitizedString(_sanitize_docstring) + + +def _sanitize_message(thing): + s = str(thing) + s = _sanitize_general(s) + return _hexadecimal.sub("0", s) + + +@pytest.fixture +def msg(): + """Sanitize messages and add custom failure explanation""" + return SanitizedString(_sanitize_message) + + +def pytest_assertrepr_compare(op, left, right): # noqa: ARG001 + """Hook to insert custom failure explanation""" + if hasattr(left, "explanation"): + return left.explanation + return None + + +# Number of times we think repeatedly collecting garbage might do anything. +# The only reason to do more than once is because finalizers executed during +# one GC pass could create garbage that can't be collected until a future one. +# This quickly produces diminishing returns, and GC passes can be slow, so this +# value is a tradeoff between non-flakiness and fast tests. (It errs on the +# side of non-flakiness; many uses of this idiom only do 3 passes.) +num_gc_collect = 5 + + +def gc_collect(): + """Run the garbage collector several times (needed when running + reference counting tests with PyPy)""" + for _ in range(num_gc_collect): + gc.collect() + + +def delattr_and_ensure_destroyed(*specs): + """For each of the given *specs* (a tuple of the form ``(scope, name)``), + perform ``delattr(scope, name)``, then do enough GC collections that the + deleted reference has actually caused the target to be destroyed. This is + typically used to test what happens when a type object is destroyed; if you + use it for that, you should be aware that extension types, or all types, + are immortal on some Python versions. See ``env.TYPES_ARE_IMMORTAL``. + """ + wrs = [] + for mod, name in specs: + wrs.append(weakref.ref(getattr(mod, name))) + delattr(mod, name) + + for _ in range(num_gc_collect): + gc.collect() + if all(wr() is None for wr in wrs): + break + else: + # If this fires, most likely something is still holding a reference + # to the object you tried to destroy - for example, it's a type that + # still has some instances alive. Try setting a breakpoint here and + # examining `gc.get_referrers(wrs[0]())`. It's vaguely possible that + # num_gc_collect needs to be increased also. + pytest.fail( + f"Could not delete bindings such as {next(wr for wr in wrs if wr() is not None)!r}" + ) + + +def pytest_configure(): + pytest.suppress = contextlib.suppress + pytest.gc_collect = gc_collect + pytest.delattr_and_ensure_destroyed = delattr_and_ensure_destroyed + + +def pytest_report_header(): + assert pybind11_tests.compiler_info is not None, ( + "Please update pybind11_tests.cpp if this assert fails." + ) + interesting_packages = ("pybind11", "numpy", "scipy", "build") + valid = [] + for package in sorted(interesting_packages): + with contextlib.suppress(ModuleNotFoundError): + valid.append(f"{package}=={importlib.metadata.version(package)}") + reqs = " ".join(valid) + + cpp_info = [ + "C++ Info:", + f"{pybind11_tests.compiler_info}", + f"{pybind11_tests.cpp_std}", + f"{pybind11_tests.PYBIND11_INTERNALS_ID}", + f"PYBIND11_SIMPLE_GIL_MANAGEMENT={pybind11_tests.PYBIND11_SIMPLE_GIL_MANAGEMENT}", + ] + if "__graalpython__" in sys.modules: + cpp_info.append( + f"GraalPy version: {sys.modules['__graalpython__'].get_graalvm_version()}" + ) + lines = [ + f"installed packages of interest: {reqs}", + " ".join(cpp_info), + ] + if sysconfig.get_config_var("Py_GIL_DISABLED"): + lines.append("free-threaded Python build") + + return lines + + +@pytest.fixture +def backport_typehints() -> Callable[[SanitizedString], SanitizedString]: + d = {} + if sys.version_info < (3, 13): + d["typing_extensions.TypeIs"] = "typing.TypeIs" + d["typing_extensions.CapsuleType"] = "types.CapsuleType" + if sys.version_info < (3, 12): + d["typing_extensions.Buffer"] = "collections.abc.Buffer" + if sys.version_info < (3, 11): + d["typing_extensions.Never"] = "typing.Never" + if sys.version_info < (3, 10): + d["typing_extensions.TypeGuard"] = "typing.TypeGuard" + + def backport(sanitized_string: SanitizedString) -> SanitizedString: + for old, new in d.items(): + sanitized_string.string = sanitized_string.string.replace(old, new) + + return sanitized_string + + return backport diff --git a/external_libraries/pybind11/tests/constructor_stats.h b/external_libraries/pybind11/tests/constructor_stats.h new file mode 100644 index 00000000..352b1b6c --- /dev/null +++ b/external_libraries/pybind11/tests/constructor_stats.h @@ -0,0 +1,330 @@ +#pragma once +/* + tests/constructor_stats.h -- framework for printing and tracking object + instance lifetimes in example/test code. + + Copyright (c) 2016 Jason Rhinelander + + All rights reserved. Use of this source code is governed by a + BSD-style license that can be found in the LICENSE file. + +This header provides a few useful tools for writing examples or tests that want to check and/or +display object instance lifetimes. It requires that you include this header and add the following +function calls to constructors: + + class MyClass { + MyClass() { ...; print_default_created(this); } + ~MyClass() { ...; print_destroyed(this); } + MyClass(const MyClass &c) { ...; print_copy_created(this); } + MyClass(MyClass &&c) { ...; print_move_created(this); } + MyClass(int a, int b) { ...; print_created(this, a, b); } + MyClass &operator=(const MyClass &c) { ...; print_copy_assigned(this); } + MyClass &operator=(MyClass &&c) { ...; print_move_assigned(this); } + + ... + } + +You can find various examples of these in several of the existing testing .cpp files. (Of course +you don't need to add any of the above constructors/operators that you don't actually have, except +for the destructor). + +Each of these will print an appropriate message such as: + + ### MyClass @ 0x2801910 created via default constructor + ### MyClass @ 0x27fa780 created 100 200 + ### MyClass @ 0x2801910 destroyed + ### MyClass @ 0x27fa780 destroyed + +You can also include extra arguments (such as the 100, 200 in the output above, coming from the +value constructor) for all of the above methods which will be included in the output. + +For testing, each of these also keeps track the created instances and allows you to check how many +of the various constructors have been invoked from the Python side via code such as: + + from pybind11_tests import ConstructorStats + cstats = ConstructorStats.get(MyClass) + print(cstats.alive()) + print(cstats.default_constructions) + +Note that `.alive()` should usually be the first thing you call as it invokes Python's garbage +collector to actually destroy objects that aren't yet referenced. + +For everything except copy and move constructors and destructors, any extra values given to the +print_...() function is stored in a class-specific values list which you can retrieve and inspect +from the ConstructorStats instance `.values()` method. + +In some cases, when you need to track instances of a C++ class not registered with pybind11, you +need to add a function returning the ConstructorStats for the C++ class; this can be done with: + + m.def("get_special_cstats", &ConstructorStats::get, +py::return_value_policy::reference) + +Finally, you can suppress the output messages, but keep the constructor tracking (for +inspection/testing in python) by using the functions with `print_` replaced with `track_` (e.g. +`track_copy_created(this)`). + +*/ + +#include "pybind11_tests.h" + +#include +#include +#include +#include + +class ConstructorStats { +protected: + std::unordered_map _instances; // Need a map rather than set because members can + // shared address with parents + std::list _values; // Used to track values + // (e.g. of value constructors) +public: + int default_constructions = 0; + int copy_constructions = 0; + int move_constructions = 0; + int copy_assignments = 0; + int move_assignments = 0; + + void copy_created(void *inst) { + created(inst); + copy_constructions++; + } + + void move_created(void *inst) { + created(inst); + move_constructions++; + } + + void default_created(void *inst) { + created(inst); + default_constructions++; + } + + void created(void *inst) { ++_instances[inst]; } + + void destroyed(void *inst) { + if (--_instances[inst] < 0) { + throw std::runtime_error("cstats.destroyed() called with unknown " + "instance; potential double-destruction " + "or a missing cstats.created()"); + } + } + + static void gc() { + // Force garbage collection to ensure any pending destructors are invoked: +#if defined(PYPY_VERSION) + PyObject *globals = PyEval_GetGlobals(); + PyObject *result = PyRun_String("import gc\n" + "for i in range(2):\n" + " gc.collect()\n", + Py_file_input, + globals, + globals); + if (result == nullptr) + throw py::error_already_set(); + Py_DECREF(result); +#else + py::module_::import("gc").attr("collect")(); +#endif + } + + int alive() { + gc(); + int total = 0; + for (const auto &p : _instances) { + if (p.second > 0) { + total += p.second; + } + } + return total; + } + + void value() {} // Recursion terminator + // Takes one or more values, converts them to strings, then stores them. + template + void value(const T &v, Tmore &&...args) { + std::ostringstream oss; + oss << v; + _values.push_back(oss.str()); + value(std::forward(args)...); + } + + // Move out stored values + py::list values() { + py::list l; + for (const auto &v : _values) { + l.append(py::cast(v)); + } + _values.clear(); + return l; + } + + // Gets constructor stats from a C++ type index + static ConstructorStats &get(std::type_index type) { + static std::unordered_map all_cstats; + return all_cstats[type]; + } + + // Gets constructor stats from a C++ type + template + static ConstructorStats &get() { +#if defined(PYPY_VERSION) + gc(); +#endif + return get(typeid(T)); + } + + // Gets constructor stats from a Python class + static ConstructorStats &get(py::object class_) { + auto &internals = py::detail::get_internals(); + const std::type_index *t1 = nullptr, *t2 = nullptr; + try { + auto *type_info + = internals.registered_types_py.at((PyTypeObject *) class_.ptr()).at(0); + for (auto &p : internals.registered_types_cpp) { + if (p.second == type_info) { + if (t1) { + t2 = &p.first; + break; + } + t1 = &p.first; + } + } + } catch (const std::out_of_range &) { // NOLINT(bugprone-empty-catch) + } + if (!t1) { + throw std::runtime_error("Unknown class passed to ConstructorStats::get()"); + } + auto &cs1 = get(*t1); + // If we have both a t1 and t2 match, one is probably the trampoline class; return + // whichever has more constructions (typically one or the other will be 0) + if (t2) { + auto &cs2 = get(*t2); + int cs1_total = cs1.default_constructions + cs1.copy_constructions + + cs1.move_constructions + (int) cs1._values.size(); + int cs2_total = cs2.default_constructions + cs2.copy_constructions + + cs2.move_constructions + (int) cs2._values.size(); + if (cs2_total > cs1_total) { + return cs2; + } + } + return cs1; + } +}; + +// To track construction/destruction, you need to call these methods from the various +// constructors/operators. The ones that take extra values record the given values in the +// constructor stats values for later inspection. +template +void track_copy_created(T *inst) { + ConstructorStats::get().copy_created(inst); +} +template +void track_move_created(T *inst) { + ConstructorStats::get().move_created(inst); +} +template +void track_copy_assigned(T *, Values &&...values) { + auto &cst = ConstructorStats::get(); + cst.copy_assignments++; + cst.value(std::forward(values)...); +} +template +void track_move_assigned(T *, Values &&...values) { + auto &cst = ConstructorStats::get(); + cst.move_assignments++; + cst.value(std::forward(values)...); +} +template +void track_default_created(T *inst, Values &&...values) { + auto &cst = ConstructorStats::get(); + cst.default_created(inst); + cst.value(std::forward(values)...); +} +template +void track_created(T *inst, Values &&...values) { + auto &cst = ConstructorStats::get(); + cst.created(inst); + cst.value(std::forward(values)...); +} +template +void track_destroyed(T *inst) { + ConstructorStats::get().destroyed(inst); +} +template +void track_values(T *, Values &&...values) { + ConstructorStats::get().value(std::forward(values)...); +} + +/// Don't cast pointers to Python, print them as strings +inline const char *format_ptrs(const char *p) { return p; } +template +py::str format_ptrs(T *p) { + return "{:#x}"_s.format(reinterpret_cast(p)); +} +template +auto format_ptrs(T &&x) -> decltype(std::forward(x)) { + return std::forward(x); +} + +template +void print_constr_details(T *inst, const std::string &action, Output &&...output) { + py::print("###", + py::type_id(), + "@", + format_ptrs(inst), + action, + format_ptrs(std::forward(output))...); +} + +// Verbose versions of the above: +template +void print_copy_created(T *inst, + Values &&...values) { // NB: this prints, but doesn't store, given values + print_constr_details(inst, "created via copy constructor", values...); + track_copy_created(inst); +} +template +void print_move_created(T *inst, + Values &&...values) { // NB: this prints, but doesn't store, given values + print_constr_details(inst, "created via move constructor", values...); + track_move_created(inst); +} +template +void print_copy_assigned(T *inst, Values &&...values) { + print_constr_details(inst, "assigned via copy assignment", values...); + track_copy_assigned(inst, values...); +} +template +void print_move_assigned(T *inst, Values &&...values) { + print_constr_details(inst, "assigned via move assignment", values...); + track_move_assigned(inst, values...); +} +template +void print_default_created(T *inst, Values &&...values) { + print_constr_details(inst, "created via default constructor", values...); + track_default_created(inst, values...); +} +template +void print_created(T *inst, Values &&...values) { + print_constr_details(inst, "created", values...); + track_created(inst, values...); +} +template +void print_destroyed(T *inst, Values &&...values) { // Prints but doesn't store given values + /* + * On GraalPy, destructors can trigger anywhere and this can cause random + * failures in unrelated tests. + */ +#if !defined(GRAALVM_PYTHON) + print_constr_details(inst, "destroyed", values...); + track_destroyed(inst); +#else + py::detail::silence_unused_warnings(inst, values...); +#endif +} +template +void print_values(T *inst, Values &&...values) { + print_constr_details(inst, ":", values...); + track_values(inst, values...); +} diff --git a/external_libraries/pybind11/tests/cross_module_gil_utils.cpp b/external_libraries/pybind11/tests/cross_module_gil_utils.cpp new file mode 100644 index 00000000..39112668 --- /dev/null +++ b/external_libraries/pybind11/tests/cross_module_gil_utils.cpp @@ -0,0 +1,111 @@ +/* + tests/cross_module_gil_utils.cpp -- tools for acquiring GIL from a different module + + Copyright (c) 2019 Google LLC + + All rights reserved. Use of this source code is governed by a + BSD-style license that can be found in the LICENSE file. +*/ +#if defined(PYBIND11_INTERNALS_VERSION) +# undef PYBIND11_INTERNALS_VERSION +#endif +#define PYBIND11_INTERNALS_VERSION 21814642 // Ensure this module has its own `internals` instance. +#include + +#include +#include +#include + +// This file mimics a DSO that makes pybind11 calls but does not define a +// PYBIND11_MODULE. The purpose is to test that such a DSO can create a +// py::gil_scoped_acquire when the running thread is in a GIL-released state. +// +// Note that we define a Python module here for convenience, but in general +// this need not be the case. The typical scenario would be a DSO that implements +// shared logic used internally by multiple pybind11 modules. + +namespace { + +namespace py = pybind11; + +void gil_acquire() { py::gil_scoped_acquire gil; } + +std::string gil_multi_acquire_release(unsigned bits) { + if ((bits & 0x1u) != 0u) { + py::gil_scoped_acquire gil; + } + if ((bits & 0x2u) != 0u) { + py::gil_scoped_release gil; + } + if ((bits & 0x4u) != 0u) { + py::gil_scoped_acquire gil; + } + if ((bits & 0x8u) != 0u) { + py::gil_scoped_release gil; + } + return PYBIND11_INTERNALS_ID; +} + +struct CustomAutoGIL { + CustomAutoGIL() : gstate(PyGILState_Ensure()) {} + ~CustomAutoGIL() { PyGILState_Release(gstate); } + + PyGILState_STATE gstate; +}; +struct CustomAutoNoGIL { + CustomAutoNoGIL() : save(PyEval_SaveThread()) {} + ~CustomAutoNoGIL() { PyEval_RestoreThread(save); } + + PyThreadState *save; +}; + +template +void gil_acquire_inner() { + Acquire acquire_outer; + Acquire acquire_inner; + Release release; +} + +template +void gil_acquire_nested() { + Acquire acquire_outer; + Acquire acquire_inner; + Release release; + auto thread = std::thread(&gil_acquire_inner); + thread.join(); +} + +constexpr char kModuleName[] = "cross_module_gil_utils"; + +struct PyModuleDef moduledef = { + PyModuleDef_HEAD_INIT, kModuleName, nullptr, 0, nullptr, nullptr, nullptr, nullptr, nullptr}; + +} // namespace + +#define ADD_FUNCTION(Name, ...) \ + PyModule_AddObject(m, Name, PyLong_FromVoidPtr(reinterpret_cast(&__VA_ARGS__))); + +extern "C" PYBIND11_EXPORT PyObject *PyInit_cross_module_gil_utils() { + + PyObject *m = PyModule_Create(&moduledef); + + if (m != nullptr) { + static_assert(sizeof(&gil_acquire) == sizeof(void *), + "Function pointer must have the same size as void*"); +#ifdef Py_GIL_DISABLED + PyUnstable_Module_SetGIL(m, Py_MOD_GIL_NOT_USED); +#endif + ADD_FUNCTION("gil_acquire_funcaddr", gil_acquire) + ADD_FUNCTION("gil_multi_acquire_release_funcaddr", gil_multi_acquire_release) + ADD_FUNCTION("gil_acquire_inner_custom_funcaddr", + gil_acquire_inner) + ADD_FUNCTION("gil_acquire_nested_custom_funcaddr", + gil_acquire_nested) + ADD_FUNCTION("gil_acquire_inner_pybind11_funcaddr", + gil_acquire_inner) + ADD_FUNCTION("gil_acquire_nested_pybind11_funcaddr", + gil_acquire_nested) + } + + return m; +} diff --git a/external_libraries/pybind11/tests/cross_module_interleaved_error_already_set.cpp b/external_libraries/pybind11/tests/cross_module_interleaved_error_already_set.cpp new file mode 100644 index 00000000..65a4463b --- /dev/null +++ b/external_libraries/pybind11/tests/cross_module_interleaved_error_already_set.cpp @@ -0,0 +1,54 @@ +/* + Copyright (c) 2022 Google LLC + + All rights reserved. Use of this source code is governed by a + BSD-style license that can be found in the LICENSE file. +*/ + +#include + +// This file mimics a DSO that makes pybind11 calls but does not define a PYBIND11_MODULE, +// so that the first call of cross_module_error_already_set() triggers the first call of +// pybind11::detail::get_internals(). + +namespace { + +namespace py = pybind11; + +void interleaved_error_already_set() { + py::set_error(PyExc_RuntimeError, "1st error."); + try { + throw py::error_already_set(); + } catch (const py::error_already_set &) { + // The 2nd error could be conditional in a real application. + py::set_error(PyExc_RuntimeError, "2nd error."); + } // Here the 1st error is destroyed before the 2nd error is fetched. + // The error_already_set dtor triggers a pybind11::detail::get_internals() + // call via pybind11::gil_scoped_acquire. + if (PyErr_Occurred()) { + throw py::error_already_set(); + } +} + +constexpr char kModuleName[] = "cross_module_interleaved_error_already_set"; + +struct PyModuleDef moduledef = { + PyModuleDef_HEAD_INIT, kModuleName, nullptr, 0, nullptr, nullptr, nullptr, nullptr, nullptr}; + +} // namespace + +extern "C" PYBIND11_EXPORT PyObject *PyInit_cross_module_interleaved_error_already_set() { + PyObject *m = PyModule_Create(&moduledef); + if (m != nullptr) { + static_assert(sizeof(&interleaved_error_already_set) == sizeof(void *), + "Function pointer must have the same size as void *"); +#ifdef Py_GIL_DISABLED + PyUnstable_Module_SetGIL(m, Py_MOD_GIL_NOT_USED); +#endif + PyModule_AddObject( + m, + "funcaddr", + PyLong_FromVoidPtr(reinterpret_cast(&interleaved_error_already_set))); + } + return m; +} diff --git a/external_libraries/pybind11/tests/custom_exceptions.py b/external_libraries/pybind11/tests/custom_exceptions.py new file mode 100644 index 00000000..b1a092d7 --- /dev/null +++ b/external_libraries/pybind11/tests/custom_exceptions.py @@ -0,0 +1,10 @@ +from __future__ import annotations + + +class PythonMyException7(Exception): + def __init__(self, message): + self.message = message + super().__init__(message) + + def __str__(self): + return "[PythonMyException7]: " + self.message.a diff --git a/external_libraries/pybind11/tests/eigen_tensor_avoid_stl_array.cpp b/external_libraries/pybind11/tests/eigen_tensor_avoid_stl_array.cpp new file mode 100644 index 00000000..5aca66c5 --- /dev/null +++ b/external_libraries/pybind11/tests/eigen_tensor_avoid_stl_array.cpp @@ -0,0 +1,16 @@ +/* + tests/eigen_tensor.cpp -- automatic conversion of Eigen Tensor + + All rights reserved. Use of this source code is governed by a + BSD-style license that can be found in the LICENSE file. +*/ + +#ifndef EIGEN_AVOID_STL_ARRAY +# define EIGEN_AVOID_STL_ARRAY +#endif + +#include "test_eigen_tensor.inl" + +PYBIND11_MODULE(eigen_tensor_avoid_stl_array, m, pybind11::mod_gil_not_used()) { + eigen_tensor_test::test_module(m); +} diff --git a/external_libraries/pybind11/tests/env.py b/external_libraries/pybind11/tests/env.py new file mode 100644 index 00000000..12acde7b --- /dev/null +++ b/external_libraries/pybind11/tests/env.py @@ -0,0 +1,76 @@ +from __future__ import annotations + +import platform +import sys +import sysconfig + +import pytest + +ANDROID = sys.platform.startswith("android") +IOS = sys.platform.startswith("ios") +LINUX = sys.platform.startswith("linux") +MACOS = sys.platform.startswith("darwin") +WIN = sys.platform.startswith("win32") or sys.platform.startswith("cygwin") +FREEBSD = sys.platform.startswith("freebsd") + +MUSLLINUX = False +MANYLINUX = False +if LINUX: + + def _is_musl() -> bool: + libc, _ = platform.libc_ver() + return libc == "musl" or (libc != "glibc" and libc != "") + + MUSLLINUX = _is_musl() + MANYLINUX = not MUSLLINUX + del _is_musl + +CPYTHON = platform.python_implementation() == "CPython" +PYPY = platform.python_implementation() == "PyPy" +GRAALPY = sys.implementation.name == "graalpy" +_graalpy_version = ( + sys.modules["__graalpython__"].get_graalvm_version() if GRAALPY else "0.0.0" +) +GRAALPY_VERSION = tuple(int(t) for t in _graalpy_version.split("-")[0].split(".")[:3]) + +# Compile-time config (what the binary was built for) +PY_GIL_DISABLED = bool(sysconfig.get_config_var("Py_GIL_DISABLED")) +# Runtime state (what's actually happening now) +sys_is_gil_enabled = getattr(sys, "_is_gil_enabled", lambda: True) + +TYPES_ARE_IMMORTAL = ( + PYPY + or GRAALPY + or (CPYTHON and PY_GIL_DISABLED and (3, 13) <= sys.version_info < (3, 14)) +) + + +def check_script_success_in_subprocess(code: str, *, rerun: int = 8) -> None: + """Runs the given code in a subprocess.""" + import os + import subprocess + import sys + import textwrap + + if ANDROID or IOS or sys.platform.startswith("emscripten"): + pytest.skip("Requires subprocess support") + + code = textwrap.dedent(code).strip() + try: + for _ in range(rerun): # run flakily failing test multiple times + subprocess.check_output( + [sys.executable, "-c", code], + cwd=os.getcwd(), + stderr=subprocess.STDOUT, + text=True, + ) + except subprocess.CalledProcessError as ex: + raise RuntimeError( + f"Subprocess failed with exit code {ex.returncode}.\n\n" + f"Code:\n" + f"```python\n" + f"{code}\n" + f"```\n\n" + f"Output:\n" + f"{ex.output}" + ) from None diff --git a/external_libraries/pybind11/tests/exo_planet_c_api.cpp b/external_libraries/pybind11/tests/exo_planet_c_api.cpp new file mode 100644 index 00000000..15101071 --- /dev/null +++ b/external_libraries/pybind11/tests/exo_planet_c_api.cpp @@ -0,0 +1,76 @@ +// Copyright (c) 2024 The pybind Community. + +// In production situations it is totally fine to build with +// C++ Exception Handling enabled. However, here we want to ensure that +// C++ Exception Handling is not required. +#if defined(_MSC_VER) || defined(__EMSCRIPTEN__) +// Too much trouble making the required cmake changes (see PR #5375). +#else +# ifdef __cpp_exceptions +// https://isocpp.org/std/standing-documents/sd-6-sg10-feature-test-recommendations#__cpp_exceptions +# error This test is meant to be built with C++ Exception Handling disabled, but __cpp_exceptions is defined. +# endif +# ifdef __EXCEPTIONS +// https://gcc.gnu.org/onlinedocs/cpp/Common-Predefined-Macros.html +# error This test is meant to be built with C++ Exception Handling disabled, but __EXCEPTIONS is defined. +# endif +#endif + +// THIS MUST STAY AT THE TOP! +#include // VERY light-weight dependency. + +#include "test_cpp_conduit_traveler_types.h" + +#include + +namespace { + +extern "C" PyObject *wrapGetLuggage(PyObject * /*self*/, PyObject *traveler) { + const auto *cpp_traveler = pybind11_conduit_v1::get_type_pointer_ephemeral< + pybind11_tests::test_cpp_conduit::Traveler>(traveler); + if (cpp_traveler == nullptr) { + return nullptr; + } + return PyUnicode_FromString(cpp_traveler->luggage.c_str()); +} + +extern "C" PyObject *wrapGetPoints(PyObject * /*self*/, PyObject *premium_traveler) { + const auto *cpp_premium_traveler = pybind11_conduit_v1::get_type_pointer_ephemeral< + pybind11_tests::test_cpp_conduit::PremiumTraveler>(premium_traveler); + if (cpp_premium_traveler == nullptr) { + return nullptr; + } + return PyLong_FromLong(static_cast(cpp_premium_traveler->points)); +} + +PyMethodDef ThisMethodDef[] = {{"GetLuggage", wrapGetLuggage, METH_O, nullptr}, + {"GetPoints", wrapGetPoints, METH_O, nullptr}, + {nullptr, nullptr, 0, nullptr}}; + +struct PyModuleDef ThisModuleDef = { + PyModuleDef_HEAD_INIT, // m_base + "exo_planet_c_api", // m_name + nullptr, // m_doc + -1, // m_size + ThisMethodDef, // m_methods + nullptr, // m_slots + nullptr, // m_traverse + nullptr, // m_clear + nullptr // m_free +}; + +} // namespace + +#if defined(WIN32) || defined(_WIN32) +# define EXO_PLANET_C_API_EXPORT __declspec(dllexport) +#else +# define EXO_PLANET_C_API_EXPORT __attribute__((visibility("default"))) +#endif + +extern "C" EXO_PLANET_C_API_EXPORT PyObject *PyInit_exo_planet_c_api() { + PyObject *m = PyModule_Create(&ThisModuleDef); + if (m == nullptr) { + return nullptr; + } + return m; +} diff --git a/external_libraries/pybind11/tests/exo_planet_pybind11.cpp b/external_libraries/pybind11/tests/exo_planet_pybind11.cpp new file mode 100644 index 00000000..9d1a2b84 --- /dev/null +++ b/external_libraries/pybind11/tests/exo_planet_pybind11.cpp @@ -0,0 +1,19 @@ +// Copyright (c) 2024 The pybind Community. + +#if defined(PYBIND11_INTERNALS_VERSION) +# undef PYBIND11_INTERNALS_VERSION +#endif +#define PYBIND11_INTERNALS_VERSION 900000001 + +#include "test_cpp_conduit_traveler_bindings.h" + +namespace pybind11_tests { +namespace test_cpp_conduit { + +PYBIND11_MODULE(exo_planet_pybind11, m) { + wrap_traveler(m); + m.def("wrap_very_lonely_traveler", [m]() { wrap_very_lonely_traveler(m); }); +} + +} // namespace test_cpp_conduit +} // namespace pybind11_tests diff --git a/external_libraries/pybind11/tests/extra_python_package/pytest.ini b/external_libraries/pybind11/tests/extra_python_package/pytest.ini new file mode 100644 index 00000000..e69de29b diff --git a/external_libraries/pybind11/tests/extra_python_package/test_files.py b/external_libraries/pybind11/tests/extra_python_package/test_files.py new file mode 100644 index 00000000..1539b171 --- /dev/null +++ b/external_libraries/pybind11/tests/extra_python_package/test_files.py @@ -0,0 +1,385 @@ +from __future__ import annotations + +import contextlib +import os +import re +import shutil +import subprocess +import sys +import tarfile +import zipfile +from pathlib import Path +from typing import Generator + +# These tests must be run explicitly + +DIR = Path(__file__).parent.resolve() +MAIN_DIR = DIR.parent.parent + +FILENAME_VERSION = re.compile(r"[-_]((\d+\.\d+\.\d+)(?:[a-z]+\d*)?)(?:-|\.tar\.gz$)") + +# Newer pytest has global path setting, but keeping old pytest for now +sys.path.append(str(MAIN_DIR / "tools")) + +from make_global import get_global # noqa: E402 + +HAS_UV = shutil.which("uv") is not None +UV_ARGS = ["--installer=uv"] if HAS_UV else [] + +PKGCONFIG = """\ +prefix=${{pcfiledir}}/../../ +includedir=${{prefix}}/include + +Name: pybind11 +Description: Seamless operability between C++11 and Python +Version: {VERSION} +Cflags: -I${{includedir}} +""" + + +main_headers = { + "include/pybind11/attr.h", + "include/pybind11/buffer_info.h", + "include/pybind11/cast.h", + "include/pybind11/chrono.h", + "include/pybind11/common.h", + "include/pybind11/complex.h", + "include/pybind11/critical_section.h", + "include/pybind11/eigen.h", + "include/pybind11/embed.h", + "include/pybind11/eval.h", + "include/pybind11/functional.h", + "include/pybind11/gil.h", + "include/pybind11/gil_safe_call_once.h", + "include/pybind11/gil_simple.h", + "include/pybind11/iostream.h", + "include/pybind11/native_enum.h", + "include/pybind11/numpy.h", + "include/pybind11/operators.h", + "include/pybind11/options.h", + "include/pybind11/pybind11.h", + "include/pybind11/pytypes.h", + "include/pybind11/subinterpreter.h", + "include/pybind11/stl.h", + "include/pybind11/stl_bind.h", + "include/pybind11/trampoline_self_life_support.h", + "include/pybind11/type_caster_pyobject_ptr.h", + "include/pybind11/typing.h", + "include/pybind11/warnings.h", +} + +conduit_headers = { + "include/pybind11/conduit/README.txt", + "include/pybind11/conduit/pybind11_conduit_v1.h", + "include/pybind11/conduit/pybind11_platform_abi_id.h", + "include/pybind11/conduit/wrap_include_python_h.h", +} + +detail_headers = { + "include/pybind11/detail/argument_vector.h", + "include/pybind11/detail/class.h", + "include/pybind11/detail/common.h", + "include/pybind11/detail/cpp_conduit.h", + "include/pybind11/detail/descr.h", + "include/pybind11/detail/dynamic_raw_ptr_cast_if_possible.h", + "include/pybind11/detail/function_record_pyobject.h", + "include/pybind11/detail/holder_caster_foreign_helpers.h", + "include/pybind11/detail/init.h", + "include/pybind11/detail/internals.h", + "include/pybind11/detail/native_enum_data.h", + "include/pybind11/detail/pybind11_namespace_macros.h", + "include/pybind11/detail/struct_smart_holder.h", + "include/pybind11/detail/type_caster_base.h", + "include/pybind11/detail/typeid.h", + "include/pybind11/detail/using_smart_holder.h", + "include/pybind11/detail/value_and_holder.h", + "include/pybind11/detail/exception_translation.h", +} + +eigen_headers = { + "include/pybind11/eigen/common.h", + "include/pybind11/eigen/matrix.h", + "include/pybind11/eigen/tensor.h", +} + +stl_headers = { + "include/pybind11/stl/filesystem.h", +} + +cmake_files = { + "share/cmake/pybind11/FindPythonLibsNew.cmake", + "share/cmake/pybind11/pybind11Common.cmake", + "share/cmake/pybind11/pybind11Config.cmake", + "share/cmake/pybind11/pybind11ConfigVersion.cmake", + "share/cmake/pybind11/pybind11GuessPythonExtSuffix.cmake", + "share/cmake/pybind11/pybind11NewTools.cmake", + "share/cmake/pybind11/pybind11Targets.cmake", + "share/cmake/pybind11/pybind11Tools.cmake", +} + +pkgconfig_files = { + "share/pkgconfig/pybind11.pc", +} + +py_files = { + "__init__.py", + "__main__.py", + "_version.py", + "commands.py", + "py.typed", + "setup_helpers.py", + "share/__init__.py", + "share/pkgconfig/__init__.py", +} + +headers = main_headers | conduit_headers | detail_headers | eigen_headers | stl_headers +generated_files = cmake_files | pkgconfig_files +all_files = headers | generated_files | py_files + +sdist_files = { + "pyproject.toml", + "LICENSE", + "README.rst", + "PKG-INFO", + "SECURITY.md", +} + + +@contextlib.contextmanager +def preserve_file(filename: Path) -> Generator[str, None, None]: + old_stat = filename.stat() + old_file = filename.read_text(encoding="utf-8") + try: + yield old_file + finally: + filename.write_text(old_file, encoding="utf-8") + os.utime(filename, (old_stat.st_atime, old_stat.st_mtime)) + + +@contextlib.contextmanager +def build_global() -> Generator[None, None, None]: + """ + Build global SDist and wheel. + """ + + pyproject = MAIN_DIR / "pyproject.toml" + with preserve_file(pyproject): + newer_txt = get_global() + pyproject.write_text(newer_txt, encoding="utf-8") + yield + + +def read_tz_file(tar: tarfile.TarFile, name: str) -> bytes: + start = tar.getnames()[0].split("/")[0] + "/" + inner_file = tar.extractfile(tar.getmember(f"{start}{name}")) + assert inner_file + with contextlib.closing(inner_file) as f: + return f.read() + + +def normalize_line_endings(value: bytes) -> bytes: + return value.replace(os.linesep.encode("utf-8"), b"\n") + + +def test_build_sdist(monkeypatch, tmpdir): + monkeypatch.chdir(MAIN_DIR) + + subprocess.run( + [sys.executable, "-m", "build", "--sdist", f"--outdir={tmpdir}", *UV_ARGS], + check=True, + ) + + (sdist,) = tmpdir.visit("*.tar.gz") + version = FILENAME_VERSION.search(sdist.basename).group(1) + + with tarfile.open(str(sdist), "r:gz") as tar: + simpler = {n.split("/", 1)[-1] for n in tar.getnames()[1:]} + (pkg_info_path,) = (n for n in simpler if n.endswith("PKG-INFO")) + + pyproject_toml = read_tz_file(tar, "pyproject.toml") + pkg_info = read_tz_file(tar, pkg_info_path).decode("utf-8") + + files = headers | sdist_files + assert files <= simpler + + assert b'name = "pybind11"' in pyproject_toml + assert f"Version: {version}" in pkg_info + assert "License-Expression: BSD-3-Clause" in pkg_info + assert "License-File: LICENSE" in pkg_info + assert "Provides-Extra: global" in pkg_info + assert f'Requires-Dist: pybind11-global=={version}; extra == "global"' in pkg_info + + +def test_build_global_dist(monkeypatch, tmpdir): + monkeypatch.chdir(MAIN_DIR) + with build_global(): + subprocess.run( + [ + sys.executable, + "-m", + "build", + "--sdist", + "--outdir", + str(tmpdir), + *UV_ARGS, + ], + check=True, + ) + + (sdist,) = tmpdir.visit("*.tar.gz") + version = FILENAME_VERSION.search(sdist.basename).group(2) + + with tarfile.open(str(sdist), "r:gz") as tar: + simpler = {n.split("/", 1)[-1] for n in tar.getnames()[1:]} + (pkg_info_path,) = (n for n in simpler if n.endswith("PKG-INFO")) + + pyproject_toml = read_tz_file(tar, "pyproject.toml") + pkg_info = read_tz_file(tar, pkg_info_path).decode("utf-8") + + files = headers | sdist_files + assert files <= simpler + + assert b'name = "pybind11-global"' in pyproject_toml + assert f"Version: {version}" in pkg_info + assert "License-Expression: BSD-3-Clause" in pkg_info + assert "License-File: LICENSE" in pkg_info + assert "Provides-Extra: global" not in pkg_info + assert 'Requires-Dist: pybind11-global; extra == "global"' not in pkg_info + + +def tests_build_wheel(monkeypatch, tmpdir): + monkeypatch.chdir(MAIN_DIR) + + subprocess.run( + [sys.executable, "-m", "build", "--wheel", "--outdir", str(tmpdir), *UV_ARGS], + check=True, + ) + + (wheel,) = tmpdir.visit("*.whl") + version, simple_version = FILENAME_VERSION.search(wheel.basename).groups() + + files = {f"pybind11/{n}" for n in all_files} + files |= { + "dist-info/licenses/LICENSE", + "dist-info/METADATA", + "dist-info/RECORD", + "dist-info/WHEEL", + "dist-info/entry_points.txt", + } + + with zipfile.ZipFile(str(wheel)) as z: + names = z.namelist() + share = zipfile.Path(z, "pybind11/share") + pkgconfig = (share / "pkgconfig/pybind11.pc").read_text(encoding="utf-8") + cmakeconfig = (share / "cmake/pybind11/pybind11Config.cmake").read_text( + encoding="utf-8" + ) + (pkg_info_path,) = (n for n in names if n.endswith("METADATA")) + pkg_info = zipfile.Path(z, pkg_info_path).read_text(encoding="utf-8") + + trimmed = {n for n in names if "dist-info" not in n} + trimmed |= {f"dist-info/{n.split('/', 1)[-1]}" for n in names if "dist-info" in n} + + assert files == trimmed + + assert 'set(pybind11_INCLUDE_DIR "${PACKAGE_PREFIX_DIR}/include")' in cmakeconfig + + pkgconfig_expected = PKGCONFIG.format(VERSION=simple_version) + assert pkgconfig_expected == pkgconfig + + assert f"Version: {version}" in pkg_info + assert "License-Expression: BSD-3-Clause" in pkg_info + assert "License-File: LICENSE" in pkg_info + assert "Provides-Extra: global" in pkg_info + assert f'Requires-Dist: pybind11-global=={version}; extra == "global"' in pkg_info + + +def tests_build_global_wheel(monkeypatch, tmpdir): + monkeypatch.chdir(MAIN_DIR) + with build_global(): + subprocess.run( + [ + sys.executable, + "-m", + "build", + "--wheel", + "--outdir", + str(tmpdir), + *UV_ARGS, + ], + check=True, + ) + + (wheel,) = tmpdir.visit("*.whl") + version, simple_version = FILENAME_VERSION.search(wheel.basename).groups() + + files = {f"data/data/{n}" for n in headers} + files |= {f"data/headers/{n[8:]}" for n in headers} + files |= {f"data/data/{n}" for n in generated_files} + files |= { + "dist-info/licenses/LICENSE", + "dist-info/METADATA", + "dist-info/WHEEL", + "dist-info/RECORD", + } + + with zipfile.ZipFile(str(wheel)) as z: + names = z.namelist() + beginning = names[0].split("/", 1)[0].rsplit(".", 1)[0] + + share = zipfile.Path(z, f"{beginning}.data/data/share") + pkgconfig = (share / "pkgconfig/pybind11.pc").read_text(encoding="utf-8") + cmakeconfig = (share / "cmake/pybind11/pybind11Config.cmake").read_text( + encoding="utf-8" + ) + + (pkg_info_path,) = (n for n in names if n.endswith("METADATA")) + pkg_info = zipfile.Path(z, pkg_info_path).read_text(encoding="utf-8") + + assert f"Version: {version}" in pkg_info + assert "License-Expression: BSD-3-Clause" in pkg_info + assert "License-File: LICENSE" in pkg_info + assert "Provides-Extra: global" not in pkg_info + assert 'Requires-Dist: pybind11-global; extra == "global"' not in pkg_info + + trimmed = {n[len(beginning) + 1 :] for n in names} + + assert files == trimmed + + assert 'set(pybind11_INCLUDE_DIR "${PACKAGE_PREFIX_DIR}/include")' in cmakeconfig + + pkgconfig_expected = PKGCONFIG.format(VERSION=simple_version) + assert pkgconfig_expected == pkgconfig + + +def test_version_matches(): + header = MAIN_DIR / "include/pybind11/detail/common.h" + text = header.read_text() + + # Extract the relevant macro values + regex_prefix = r"#\s*define\s+PYBIND11_VERSION_" + micro = re.search(rf"{regex_prefix}MICRO\s+(\d+)\b", text).group(1) + release_level = re.search(rf"{regex_prefix}RELEASE_LEVEL\s+(\w+)\b", text).group(1) + release_serial = re.search( + rf"{regex_prefix}RELEASE_SERIAL\s+(\d+)\b", + text, + ).group(1) + patch = re.search(rf"{regex_prefix}PATCH\s+([\w.-]+)\b", text).group(1) + + # Map release level macro to string + level_map = { + "PY_RELEASE_LEVEL_ALPHA": "a", + "PY_RELEASE_LEVEL_BETA": "b", + "PY_RELEASE_LEVEL_GAMMA": "rc", + "PY_RELEASE_LEVEL_FINAL": "", + } + level_str = level_map[release_level] + + if release_level == "PY_RELEASE_LEVEL_FINAL": + assert level_str == "" + assert release_serial == "0" + expected_patch = micro + else: + expected_patch = f"{micro}{level_str}{release_serial}" + + assert patch == expected_patch diff --git a/external_libraries/pybind11/tests/extra_setuptools/pytest.ini b/external_libraries/pybind11/tests/extra_setuptools/pytest.ini new file mode 100644 index 00000000..e69de29b diff --git a/external_libraries/pybind11/tests/extra_setuptools/test_setuphelper.py b/external_libraries/pybind11/tests/extra_setuptools/test_setuphelper.py new file mode 100644 index 00000000..2c069adf --- /dev/null +++ b/external_libraries/pybind11/tests/extra_setuptools/test_setuphelper.py @@ -0,0 +1,153 @@ +from __future__ import annotations + +import os +import subprocess +import sys +from textwrap import dedent + +import pytest + +DIR = os.path.abspath(os.path.dirname(__file__)) +MAIN_DIR = os.path.dirname(os.path.dirname(DIR)) +WIN = sys.platform.startswith("win32") or sys.platform.startswith("cygwin") + + +@pytest.mark.parametrize("parallel", [False, True]) +@pytest.mark.parametrize("std", [11, 0]) +def test_simple_setup_py(monkeypatch, tmpdir, parallel, std): + monkeypatch.chdir(tmpdir) + monkeypatch.syspath_prepend(MAIN_DIR) + + (tmpdir / "setup.py").write_text( + dedent( + f"""\ + import sys + sys.path.append({MAIN_DIR!r}) + + from setuptools import setup, Extension + from pybind11.setup_helpers import build_ext, Pybind11Extension + + std = {std} + + ext_modules = [ + Pybind11Extension( + "simple_setup", + sorted(["main.cpp"]), + cxx_std=std, + ), + ] + + cmdclass = dict() + if std == 0: + cmdclass["build_ext"] = build_ext + + + parallel = {parallel} + if parallel: + from pybind11.setup_helpers import ParallelCompile + ParallelCompile().install() + + setup( + name="simple_setup_package", + cmdclass=cmdclass, + ext_modules=ext_modules, + ) + """ + ), + encoding="ascii", + ) + + (tmpdir / "main.cpp").write_text( + dedent( + """\ + #include + + int f(int x) { + return x * 3; + } + PYBIND11_MODULE(simple_setup, m) { + m.def("f", &f); + } + """ + ), + encoding="ascii", + ) + + out = subprocess.check_output( + [sys.executable, "setup.py", "build_ext", "--inplace"], + ) + if not WIN: + assert b"-g0" in out + out = subprocess.check_output( + [sys.executable, "setup.py", "build_ext", "--inplace", "--force"], + env=dict(os.environ, CFLAGS="-g"), + ) + if not WIN: + assert b"-g0" not in out + + # Debug helper printout, normally hidden + print(out) + for item in tmpdir.listdir(): + print(item.basename) + + assert ( + len([f for f in tmpdir.listdir() if f.basename.startswith("simple_setup")]) == 1 + ) + assert len(list(tmpdir.listdir())) == 4 # two files + output + build_dir + + (tmpdir / "test.py").write_text( + dedent( + """\ + import simple_setup + assert simple_setup.f(3) == 9 + """ + ), + encoding="ascii", + ) + + subprocess.check_call( + [sys.executable, "test.py"], stdout=sys.stdout, stderr=sys.stderr + ) + + +def test_intree_extensions(monkeypatch, tmpdir): + monkeypatch.syspath_prepend(MAIN_DIR) + + from pybind11.setup_helpers import intree_extensions + + monkeypatch.chdir(tmpdir) + root = tmpdir + root.ensure_dir() + subdir = root / "dir" + subdir.ensure_dir() + src = subdir / "ext.cpp" + src.ensure() + relpath = src.relto(tmpdir) + (ext,) = intree_extensions([relpath]) + assert ext.name == "ext" + subdir.ensure("__init__.py") + (ext,) = intree_extensions([relpath]) + assert ext.name == "dir.ext" + + +def test_intree_extensions_package_dir(monkeypatch, tmpdir): + monkeypatch.syspath_prepend(MAIN_DIR) + + from pybind11.setup_helpers import intree_extensions + + monkeypatch.chdir(tmpdir) + root = tmpdir / "src" + root.ensure_dir() + subdir = root / "dir" + subdir.ensure_dir() + src = subdir / "ext.cpp" + src.ensure() + (ext,) = intree_extensions([src.relto(tmpdir)], package_dir={"": "src"}) + assert ext.name == "dir.ext" + (ext,) = intree_extensions([src.relto(tmpdir)], package_dir={"foo": "src"}) + assert ext.name == "foo.dir.ext" + subdir.ensure("__init__.py") + (ext,) = intree_extensions([src.relto(tmpdir)], package_dir={"": "src"}) + assert ext.name == "dir.ext" + (ext,) = intree_extensions([src.relto(tmpdir)], package_dir={"foo": "src"}) + assert ext.name == "foo.dir.ext" diff --git a/external_libraries/pybind11/tests/home_planet_very_lonely_traveler.cpp b/external_libraries/pybind11/tests/home_planet_very_lonely_traveler.cpp new file mode 100644 index 00000000..78d50cff --- /dev/null +++ b/external_libraries/pybind11/tests/home_planet_very_lonely_traveler.cpp @@ -0,0 +1,13 @@ +// Copyright (c) 2024 The pybind Community. + +#include "test_cpp_conduit_traveler_bindings.h" + +namespace pybind11_tests { +namespace test_cpp_conduit { + +PYBIND11_MODULE(home_planet_very_lonely_traveler, m) { + m.def("wrap_very_lonely_traveler", [m]() { wrap_very_lonely_traveler(m); }); +} + +} // namespace test_cpp_conduit +} // namespace pybind11_tests diff --git a/external_libraries/pybind11/tests/local_bindings.h b/external_libraries/pybind11/tests/local_bindings.h new file mode 100644 index 00000000..23d46eb2 --- /dev/null +++ b/external_libraries/pybind11/tests/local_bindings.h @@ -0,0 +1,93 @@ +#pragma once +#include "pybind11_tests.h" + +#include + +/// Simple class used to test py::local: +template +class LocalBase { +public: + explicit LocalBase(int i) : i(i) {} + int i = -1; +}; + +/// Registered with py::module_local in both main and secondary modules: +using LocalType = LocalBase<0>; +/// Registered without py::module_local in both modules: +using NonLocalType = LocalBase<1>; +/// A second non-local type (for stl_bind tests): +using NonLocal2 = LocalBase<2>; +/// Tests within-module, different-compilation-unit local definition conflict: +using LocalExternal = LocalBase<3>; +/// Mixed: registered local first, then global +using MixedLocalGlobal = LocalBase<4>; +/// Mixed: global first, then local +using MixedGlobalLocal = LocalBase<5>; + +/// Registered with py::module_local only in the secondary module: +using ExternalType1 = LocalBase<6>; // default holder +using ExternalType2 = LocalBase<7>; // held by std::shared_ptr +using ExternalType3 = LocalBase<8>; // held by smart_holder + +using LocalVec = std::vector; +using LocalVec2 = std::vector; +using LocalMap = std::unordered_map; +using NonLocalVec = std::vector; +using NonLocalVec2 = std::vector; +using NonLocalMap = std::unordered_map; +using NonLocalMap2 = std::unordered_map; + +// Exception that will be caught via the module local translator. +class LocalException : public std::exception { +public: + explicit LocalException(const char *m) : message{m} {} + const char *what() const noexcept override { return message.c_str(); } + +private: + std::string message = ""; +}; + +// Exception that will be registered with register_local_exception_translator +class LocalSimpleException : public std::exception { +public: + explicit LocalSimpleException(const char *m) : message{m} {} + const char *what() const noexcept override { return message.c_str(); } + +private: + std::string message = ""; +}; + +PYBIND11_MAKE_OPAQUE(LocalVec) +PYBIND11_MAKE_OPAQUE(LocalVec2) +PYBIND11_MAKE_OPAQUE(LocalMap) +PYBIND11_MAKE_OPAQUE(NonLocalVec) +// PYBIND11_MAKE_OPAQUE(NonLocalVec2) // same type as LocalVec2 +PYBIND11_MAKE_OPAQUE(NonLocalMap) +PYBIND11_MAKE_OPAQUE(NonLocalMap2) + +// Simple bindings (used with the above): +template , typename... Args> +py::class_ bind_local(Args &&...args) { + return py::class_(std::forward(args)...) + .def(py::init()) + .def("get", [](T &i) { return i.i + Adjust; }); +} + +// Simulate a foreign library base class (to match the example in the docs): +namespace pets { +class Pet { +public: + explicit Pet(std::string name) : name_(std::move(name)) {} + std::string name_; + const std::string &name() const { return name_; } +}; +} // namespace pets + +struct MixGL { + int i; + explicit MixGL(int i) : i{i} {} +}; +struct MixGL2 { + int i; + explicit MixGL2(int i) : i{i} {} +}; diff --git a/external_libraries/pybind11/tests/mod_per_interpreter_gil.cpp b/external_libraries/pybind11/tests/mod_per_interpreter_gil.cpp new file mode 100644 index 00000000..614967c5 --- /dev/null +++ b/external_libraries/pybind11/tests/mod_per_interpreter_gil.cpp @@ -0,0 +1,20 @@ +#include + +namespace py = pybind11; + +/* Simple test module/test class to check that the referenced internals data of external pybind11 + * modules are different across subinterpreters + */ + +PYBIND11_MODULE(mod_per_interpreter_gil, + m, + py::mod_gil_not_used(), + py::multiple_interpreters::per_interpreter_gil()) { + m.def("internals_at", + []() { return reinterpret_cast(&py::detail::get_internals()); }); +#ifdef PYBIND11_HAS_SUBINTERPRETER_SUPPORT + m.attr("defined_PYBIND11_HAS_SUBINTERPRETER_SUPPORT") = true; +#else + m.attr("defined_PYBIND11_HAS_SUBINTERPRETER_SUPPORT") = false; +#endif +} diff --git a/external_libraries/pybind11/tests/mod_per_interpreter_gil_with_singleton.cpp b/external_libraries/pybind11/tests/mod_per_interpreter_gil_with_singleton.cpp new file mode 100644 index 00000000..90e2ec83 --- /dev/null +++ b/external_libraries/pybind11/tests/mod_per_interpreter_gil_with_singleton.cpp @@ -0,0 +1,147 @@ +#include +#include + +#include + +namespace py = pybind11; + +#ifdef PYBIND11_HAS_NATIVE_ENUM +# include +#endif + +namespace pybind11_tests { +namespace mod_per_interpreter_gil_with_singleton { +// A singleton class that holds references to certain Python objects +// This singleton is per-interpreter using gil_safe_call_once_and_store +class MySingleton { +public: + MySingleton() = default; + ~MySingleton() = default; + MySingleton(const MySingleton &) = delete; + MySingleton &operator=(const MySingleton &) = delete; + MySingleton(MySingleton &&) = default; + MySingleton &operator=(MySingleton &&) = default; + + static MySingleton &get_instance() { + PYBIND11_CONSTINIT static py::gil_safe_call_once_and_store storage; + return storage + .call_once_and_store_result([]() -> MySingleton { + MySingleton instance{}; + + auto emplace = [&instance](const py::handle &obj) -> void { + obj.inc_ref(); // Ensure the object is not GC'd while interpreter is alive + instance.objects.emplace_back(obj); + }; + + // Example objects to store in the singleton + emplace(py::type::handle_of(py::none())); // static type + emplace(py::type::handle_of(py::tuple())); // static type + emplace(py::type::handle_of(py::list())); // static type + emplace(py::type::handle_of(py::dict())); // static type + emplace(py::module_::import("collections").attr("OrderedDict")); // static type + emplace(py::module_::import("collections").attr("defaultdict")); // heap type + emplace(py::module_::import("collections").attr("deque")); // heap type + + assert(instance.objects.size() == 7); + return instance; + }) + .get_stored(); + } + + std::vector &get_objects() { return objects; } + + static void init() { + // Ensure the singleton is created + auto &instance = get_instance(); + (void) instance; // suppress unused variable warning + assert(instance.objects.size() == 7); + // Register cleanup at interpreter exit + py::module_::import("atexit").attr("register")(py::cpp_function(&MySingleton::clear)); + } + + static void clear() { + auto &instance = get_instance(); + (void) instance; // suppress unused variable warning + assert(instance.objects.size() == 7); + for (const auto &obj : instance.objects) { + obj.dec_ref(); + } + instance.objects.clear(); + } + +private: + std::vector objects; +}; + +class MyClass { +public: + explicit MyClass(py::ssize_t v) : value(v) {} + py::ssize_t get_value() const { return value; } + +private: + py::ssize_t value; +}; + +class MyGlobalError : public std::runtime_error { +public: + using std::runtime_error::runtime_error; +}; + +class MyLocalError : public std::runtime_error { +public: + using std::runtime_error::runtime_error; +}; + +enum class MyEnum : int { + ONE = 1, + TWO = 2, + THREE = 3, +}; +} // namespace mod_per_interpreter_gil_with_singleton +} // namespace pybind11_tests + +PYBIND11_MODULE(mod_per_interpreter_gil_with_singleton, + m, + py::mod_gil_not_used(), + py::multiple_interpreters::per_interpreter_gil()) { + using namespace pybind11_tests::mod_per_interpreter_gil_with_singleton; + +#ifdef PYBIND11_HAS_SUBINTERPRETER_SUPPORT + m.attr("defined_PYBIND11_HAS_SUBINTERPRETER_SUPPORT") = true; +#else + m.attr("defined_PYBIND11_HAS_SUBINTERPRETER_SUPPORT") = false; +#endif + + MySingleton::init(); + + // Ensure py::multiple_interpreters::per_interpreter_gil() works with singletons using + // py::gil_safe_call_once_and_store + m.def( + "get_objects_in_singleton", + []() -> std::vector { return MySingleton::get_instance().get_objects(); }, + "Get the list of objects stored in the singleton"); + + // Ensure py::multiple_interpreters::per_interpreter_gil() works with class bindings + py::class_(m, "MyClass") + .def(py::init()) + .def("get_value", &MyClass::get_value); + + // Ensure py::multiple_interpreters::per_interpreter_gil() works with global exceptions + py::register_exception(m, "MyGlobalError"); + // Ensure py::multiple_interpreters::per_interpreter_gil() works with local exceptions + py::register_local_exception(m, "MyLocalError"); + +#ifdef PYBIND11_HAS_NATIVE_ENUM + // Ensure py::multiple_interpreters::per_interpreter_gil() works with native_enum + py::native_enum(m, "MyEnum", "enum.IntEnum") + .value("ONE", MyEnum::ONE) + .value("TWO", MyEnum::TWO) + .value("THREE", MyEnum::THREE) + .finalize(); +#else + py::enum_(m, "MyEnum") + .value("ONE", MyEnum::ONE) + .value("TWO", MyEnum::TWO) + .value("THREE", MyEnum::THREE); +#endif +} diff --git a/external_libraries/pybind11/tests/mod_shared_interpreter_gil.cpp b/external_libraries/pybind11/tests/mod_shared_interpreter_gil.cpp new file mode 100644 index 00000000..c6ae1725 --- /dev/null +++ b/external_libraries/pybind11/tests/mod_shared_interpreter_gil.cpp @@ -0,0 +1,17 @@ +#include + +namespace py = pybind11; + +/* Simple test module/test class to check that the referenced internals data of external pybind11 + * modules are different across subinterpreters + */ + +PYBIND11_MODULE(mod_shared_interpreter_gil, m, py::multiple_interpreters::shared_gil()) { + m.def("internals_at", + []() { return reinterpret_cast(&py::detail::get_internals()); }); +#ifdef PYBIND11_HAS_SUBINTERPRETER_SUPPORT + m.attr("defined_PYBIND11_HAS_SUBINTERPRETER_SUPPORT") = true; +#else + m.attr("defined_PYBIND11_HAS_SUBINTERPRETER_SUPPORT") = false; +#endif +} diff --git a/external_libraries/pybind11/tests/object.h b/external_libraries/pybind11/tests/object.h new file mode 100644 index 00000000..564dd4a7 --- /dev/null +++ b/external_libraries/pybind11/tests/object.h @@ -0,0 +1,205 @@ +#if !defined(__OBJECT_H) +# define __OBJECT_H + +# include "constructor_stats.h" + +# include + +/// Reference counted object base class +class Object { +public: + /// Default constructor + Object() { print_default_created(this); } + + /// Copy constructor + Object(const Object &) : m_refCount(0) { print_copy_created(this); } + + /// Return the current reference count + int getRefCount() const { return m_refCount; }; + + /// Increase the object's reference count by one + void incRef() const { ++m_refCount; } + + /** \brief Decrease the reference count of + * the object and possibly deallocate it. + * + * The object will automatically be deallocated once + * the reference count reaches zero. + */ + void decRef(bool dealloc = true) const { + --m_refCount; + if (m_refCount == 0 && dealloc) { + delete this; + } else if (m_refCount < 0) { + throw std::runtime_error("Internal error: reference count < 0!"); + } + } + + virtual std::string toString() const = 0; + +protected: + /** \brief Virtual protected deconstructor. + * (Will only be called by \ref ref) + */ + virtual ~Object() { print_destroyed(this); } + +private: + mutable std::atomic m_refCount{0}; +}; + +// Tag class used to track constructions of ref objects. When we track constructors, below, we +// track and print out the actual class (e.g. ref), and *also* add a fake tracker for +// ref_tag. This lets us check that the total number of ref constructors/destructors is +// correct without having to check each individual ref type individually. +class ref_tag {}; + +/** + * \brief Reference counting helper + * + * The \a ref refeference template is a simple wrapper to store a + * pointer to an object. It takes care of increasing and decreasing + * the reference count of the object. When the last reference goes + * out of scope, the associated object will be deallocated. + * + * \ingroup libcore + */ +template +class ref { +public: + /// Create a nullptr reference + ref() : m_ptr(nullptr) { + print_default_created(this); + track_default_created((ref_tag *) this); + } + + /// Construct a reference from a pointer + explicit ref(T *ptr) : m_ptr(ptr) { + if (m_ptr) { + ((Object *) m_ptr)->incRef(); + } + + print_created(this, "from pointer", m_ptr); + track_created((ref_tag *) this, "from pointer"); + } + + /// Copy constructor + ref(const ref &r) : m_ptr(r.m_ptr) { + if (m_ptr) { + ((Object *) m_ptr)->incRef(); + } + + print_copy_created(this, "with pointer", m_ptr); + track_copy_created((ref_tag *) this); + } + + /// Move constructor + ref(ref &&r) noexcept : m_ptr(r.m_ptr) { + r.m_ptr = nullptr; + + print_move_created(this, "with pointer", m_ptr); + track_move_created((ref_tag *) this); + } + + /// Destroy this reference + ~ref() { + if (m_ptr) { + ((Object *) m_ptr)->decRef(); + } + + print_destroyed(this); + track_destroyed((ref_tag *) this); + } + + /// Move another reference into the current one + ref &operator=(ref &&r) noexcept { + print_move_assigned(this, "pointer", r.m_ptr); + track_move_assigned((ref_tag *) this); + + if (*this == r) { + return *this; + } + if (m_ptr) { + ((Object *) m_ptr)->decRef(); + } + m_ptr = r.m_ptr; + r.m_ptr = nullptr; + return *this; + } + + /// Overwrite this reference with another reference + ref &operator=(const ref &r) { + 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; + } + if (m_ptr) { + ((Object *) m_ptr)->decRef(); + } + m_ptr = r.m_ptr; + if (m_ptr) { + ((Object *) m_ptr)->incRef(); + } + return *this; + } + + /// Overwrite this reference with a pointer to another object + ref &operator=(T *ptr) { + print_values(this, "assigned pointer"); + track_values((ref_tag *) this, "assigned pointer"); + + if (m_ptr == ptr) { + return *this; + } + if (m_ptr) { + ((Object *) m_ptr)->decRef(); + } + m_ptr = ptr; + if (m_ptr) { + ((Object *) m_ptr)->incRef(); + } + return *this; + } + + /// Compare this reference with another reference + bool operator==(const ref &r) const { return m_ptr == r.m_ptr; } + + /// Compare this reference with another reference + bool operator!=(const ref &r) const { return m_ptr != r.m_ptr; } + + /// Compare this reference with a pointer + bool operator==(const T *ptr) const { return m_ptr == ptr; } + + /// Compare this reference with a pointer + bool operator!=(const T *ptr) const { return m_ptr != ptr; } + + /// Access the object referenced by this reference + T *operator->() { return m_ptr; } + + /// Access the object referenced by this reference + const T *operator->() const { return m_ptr; } + + /// Return a C++ reference to the referenced object + T &operator*() { return *m_ptr; } + + /// Return a const C++ reference to the referenced object + const T &operator*() const { return *m_ptr; } + + /// Return a pointer to the referenced object + explicit operator T *() { return m_ptr; } + + /// Return a const pointer to the referenced object + T *get_ptr() { return m_ptr; } + + /// Return a pointer to the referenced object + const T *get_ptr() const { return m_ptr; } + +private: + T *m_ptr; +}; + +#endif /* __OBJECT_H */ diff --git a/external_libraries/pybind11/tests/pure_cpp/CMakeLists.txt b/external_libraries/pybind11/tests/pure_cpp/CMakeLists.txt new file mode 100644 index 00000000..d2757db7 --- /dev/null +++ b/external_libraries/pybind11/tests/pure_cpp/CMakeLists.txt @@ -0,0 +1,22 @@ +find_package(Catch 2.13.10) + +if(CATCH_FOUND) + message(STATUS "Building pure C++ tests (not depending on Python) using Catch v${CATCH_VERSION}") +else() + message(STATUS "Catch not detected. Interpreter tests will be skipped. Install Catch headers" + " manually or use `cmake -DDOWNLOAD_CATCH=ON` to fetch them automatically.") + return() +endif() + +add_executable(smart_holder_poc_test smart_holder_poc_test.cpp) +pybind11_enable_warnings(smart_holder_poc_test) +target_link_libraries(smart_holder_poc_test PRIVATE pybind11::headers Catch2::Catch2) + +add_custom_target( + test_pure_cpp + COMMAND "$" + WORKING_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}" + USES_TERMINAL # Ensures output is shown immediately (not buffered and possibly lost on crash) +) + +add_dependencies(check test_pure_cpp) diff --git a/external_libraries/pybind11/tests/pure_cpp/smart_holder_poc.h b/external_libraries/pybind11/tests/pure_cpp/smart_holder_poc.h new file mode 100644 index 00000000..038cddc7 --- /dev/null +++ b/external_libraries/pybind11/tests/pure_cpp/smart_holder_poc.h @@ -0,0 +1,56 @@ +// Copyright (c) 2020-2024 The Pybind Development Team. +// All rights reserved. Use of this source code is governed by a +// BSD-style license that can be found in the LICENSE file. + +#pragma once + +#include "pybind11/detail/struct_smart_holder.h" + +PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE) +PYBIND11_NAMESPACE_BEGIN(memory) +PYBIND11_NAMESPACE_BEGIN(smart_holder_poc) // Proof-of-Concept implementations. + +// NOLINTNEXTLINE(bugprone-incorrect-enable-shared-from-this) +struct PrivateESFT : private std::enable_shared_from_this {}; +static_assert(!type_has_shared_from_this(static_cast(nullptr)), + "should detect inaccessible base"); + +template +T &as_lvalue_ref(const smart_holder &hld) { + static const char *context = "as_lvalue_ref"; + hld.ensure_is_populated(context); + hld.ensure_has_pointee(context); + return *hld.as_raw_ptr_unowned(); +} + +template +T &&as_rvalue_ref(const smart_holder &hld) { + static const char *context = "as_rvalue_ref"; + hld.ensure_is_populated(context); + hld.ensure_has_pointee(context); + return std::move(*hld.as_raw_ptr_unowned()); +} + +template +T *as_raw_ptr_release_ownership(smart_holder &hld, + const char *context = "as_raw_ptr_release_ownership") { + hld.ensure_can_release_ownership(context); + T *raw_ptr = hld.as_raw_ptr_unowned(); + hld.release_ownership(get_guarded_delete); + return raw_ptr; +} + +template > +std::unique_ptr as_unique_ptr(smart_holder &hld) { + static const char *context = "as_unique_ptr"; + hld.ensure_compatible_uqp_del(context); + hld.ensure_use_count_1(context); + T *raw_ptr = hld.as_raw_ptr_unowned(); + hld.release_ownership(get_guarded_delete); + // KNOWN DEFECT (see PR #4850): Does not copy the deleter. + return std::unique_ptr(raw_ptr); +} + +PYBIND11_NAMESPACE_END(smart_holder_poc) +PYBIND11_NAMESPACE_END(memory) +PYBIND11_NAMESPACE_END(PYBIND11_NAMESPACE) diff --git a/external_libraries/pybind11/tests/pure_cpp/smart_holder_poc_test.cpp b/external_libraries/pybind11/tests/pure_cpp/smart_holder_poc_test.cpp new file mode 100644 index 00000000..09720432 --- /dev/null +++ b/external_libraries/pybind11/tests/pure_cpp/smart_holder_poc_test.cpp @@ -0,0 +1,427 @@ +#include "smart_holder_poc.h" + +#include +#include +#include +#include +#include + +// Catch uses _ internally, which breaks gettext style defines +#ifdef _ +# undef _ +#endif + +#define CATCH_CONFIG_MAIN +#include "catch.hpp" + +using pybind11::memory::guarded_delete; +using pybind11::memory::smart_holder; +namespace poc = pybind11::memory::smart_holder_poc; + +namespace helpers { + +struct movable_int { + int valu; + explicit movable_int(int v) : valu{v} {} + movable_int(movable_int &&other) noexcept : valu(other.valu) { other.valu = 91; } +}; + +template +struct functor_builtin_delete { + void operator()(T *ptr) { delete ptr; } +#if (defined(__GNUC__) && __GNUC__ == 4 && __GNUC_MINOR__ == 8) \ + || (defined(__clang_major__) && __clang_major__ == 3 && __clang_minor__ == 6) + // Workaround for these errors: + // gcc 4.8.5: too many initializers for 'helpers::functor_builtin_delete' + // clang 3.6: excess elements in struct initializer + functor_builtin_delete() = default; + functor_builtin_delete(const functor_builtin_delete &) {} + functor_builtin_delete(functor_builtin_delete &&) {} +#endif +}; + +template +struct functor_other_delete : functor_builtin_delete {}; + +struct indestructible_int { + int valu; + explicit indestructible_int(int v) : valu{v} {} + +private: + ~indestructible_int() = default; +}; + +struct base { + virtual int get() { return 10; } + virtual ~base() = default; +}; + +struct derived : public base { + int get() override { return 100; } +}; + +} // namespace helpers + +TEST_CASE("from_raw_ptr_unowned+as_raw_ptr_unowned", "[S]") { + static int value = 19; + auto hld = smart_holder::from_raw_ptr_unowned(&value); + REQUIRE(*hld.as_raw_ptr_unowned() == 19); +} + +TEST_CASE("from_raw_ptr_unowned+as_lvalue_ref", "[S]") { + static int value = 19; + auto hld = smart_holder::from_raw_ptr_unowned(&value); + REQUIRE(poc::as_lvalue_ref(hld) == 19); +} + +TEST_CASE("from_raw_ptr_unowned+as_rvalue_ref", "[S]") { + helpers::movable_int orig(19); + { + auto hld = smart_holder::from_raw_ptr_unowned(&orig); + helpers::movable_int othr(poc::as_rvalue_ref(hld)); + REQUIRE(othr.valu == 19); + REQUIRE(orig.valu == 91); + } +} + +TEST_CASE("from_raw_ptr_unowned+as_raw_ptr_release_ownership", "[E]") { + static int value = 19; + auto hld = smart_holder::from_raw_ptr_unowned(&value); + REQUIRE_THROWS_WITH(poc::as_raw_ptr_release_ownership(hld), + "Cannot disown non-owning holder (as_raw_ptr_release_ownership)."); +} + +TEST_CASE("from_raw_ptr_unowned+as_unique_ptr", "[E]") { + static int value = 19; + auto hld = smart_holder::from_raw_ptr_unowned(&value); + REQUIRE_THROWS_WITH(poc::as_unique_ptr(hld), + "Cannot disown non-owning holder (as_unique_ptr)."); +} + +TEST_CASE("from_raw_ptr_unowned+as_unique_ptr_with_deleter", "[E]") { + static int value = 19; + auto hld = smart_holder::from_raw_ptr_unowned(&value); + REQUIRE_THROWS_WITH((poc::as_unique_ptr>(hld)), + "Missing unique_ptr deleter (as_unique_ptr)."); +} + +TEST_CASE("from_raw_ptr_unowned+as_shared_ptr", "[S]") { + static int value = 19; + auto hld = smart_holder::from_raw_ptr_unowned(&value); + REQUIRE(*hld.as_shared_ptr() == 19); +} + +TEST_CASE("from_raw_ptr_take_ownership+as_lvalue_ref", "[S]") { + auto hld = smart_holder::from_raw_ptr_take_ownership(new int(19)); + REQUIRE(hld.has_pointee()); + REQUIRE(poc::as_lvalue_ref(hld) == 19); +} + +TEST_CASE("from_raw_ptr_take_ownership+as_raw_ptr_release_ownership1", "[S]") { + auto hld = smart_holder::from_raw_ptr_take_ownership(new int(19)); + auto new_owner = std::unique_ptr(poc::as_raw_ptr_release_ownership(hld)); + REQUIRE(!hld.has_pointee()); + REQUIRE(*new_owner == 19); +} + +TEST_CASE("from_raw_ptr_take_ownership+as_raw_ptr_release_ownership2", "[E]") { + auto hld = smart_holder::from_raw_ptr_take_ownership(new int(19)); + auto shd_ptr = hld.as_shared_ptr(); + REQUIRE_THROWS_WITH(poc::as_raw_ptr_release_ownership(hld), + "Cannot disown use_count != 1 (as_raw_ptr_release_ownership)."); +} + +TEST_CASE("from_raw_ptr_take_ownership+as_unique_ptr1", "[S]") { + auto hld = smart_holder::from_raw_ptr_take_ownership(new int(19)); + std::unique_ptr new_owner = poc::as_unique_ptr(hld); + REQUIRE(!hld.has_pointee()); + REQUIRE(*new_owner == 19); +} + +TEST_CASE("from_raw_ptr_take_ownership+as_unique_ptr2", "[E]") { + auto hld = smart_holder::from_raw_ptr_take_ownership(new int(19)); + auto shd_ptr = hld.as_shared_ptr(); + REQUIRE_THROWS_WITH(poc::as_unique_ptr(hld), + "Cannot disown use_count != 1 (as_unique_ptr)."); +} + +TEST_CASE("from_raw_ptr_take_ownership+as_unique_ptr_with_deleter", "[E]") { + auto hld = smart_holder::from_raw_ptr_take_ownership(new int(19)); + REQUIRE_THROWS_WITH((poc::as_unique_ptr>(hld)), + "Missing unique_ptr deleter (as_unique_ptr)."); +} + +TEST_CASE("from_raw_ptr_take_ownership+as_shared_ptr", "[S]") { + auto hld = smart_holder::from_raw_ptr_take_ownership(new int(19)); + std::shared_ptr new_owner = hld.as_shared_ptr(); + REQUIRE(hld.has_pointee()); + REQUIRE(*new_owner == 19); +} + +TEST_CASE("from_raw_ptr_take_ownership+disown+reclaim_disowned", "[S]") { + auto hld = smart_holder::from_raw_ptr_take_ownership(new int(19)); + std::unique_ptr new_owner(hld.as_raw_ptr_unowned()); + hld.disown(pybind11::memory::get_guarded_delete); + REQUIRE(poc::as_lvalue_ref(hld) == 19); + REQUIRE(*new_owner == 19); + // Manually verified: without this, clang++ -fsanitize=address reports + // "detected memory leaks". + hld.reclaim_disowned(pybind11::memory::get_guarded_delete); + // NOLINTNEXTLINE(bugprone-unused-return-value) + (void) new_owner.release(); // Manually verified: without this, clang++ -fsanitize=address + // reports "attempting double-free". + REQUIRE(poc::as_lvalue_ref(hld) == 19); + REQUIRE(new_owner.get() == nullptr); +} + +TEST_CASE("from_raw_ptr_take_ownership+disown+release_disowned", "[S]") { + auto hld = smart_holder::from_raw_ptr_take_ownership(new int(19)); + std::unique_ptr new_owner(hld.as_raw_ptr_unowned()); + hld.disown(pybind11::memory::get_guarded_delete); + REQUIRE(poc::as_lvalue_ref(hld) == 19); + REQUIRE(*new_owner == 19); + hld.release_disowned(); + REQUIRE(!hld.has_pointee()); +} + +TEST_CASE("from_raw_ptr_take_ownership+disown+ensure_is_not_disowned", "[E]") { + const char *context = "test_case"; + auto hld = smart_holder::from_raw_ptr_take_ownership(new int(19)); + hld.ensure_is_not_disowned(context); // Does not throw. + std::unique_ptr new_owner(hld.as_raw_ptr_unowned()); + hld.disown(pybind11::memory::get_guarded_delete); + REQUIRE_THROWS_WITH(hld.ensure_is_not_disowned(context), + "Holder was disowned already (test_case)."); +} + +TEST_CASE("from_unique_ptr+as_lvalue_ref", "[S]") { + std::unique_ptr orig_owner(new int(19)); + auto hld = smart_holder::from_unique_ptr(std::move(orig_owner)); + REQUIRE(orig_owner.get() == nullptr); + REQUIRE(poc::as_lvalue_ref(hld) == 19); +} + +TEST_CASE("from_unique_ptr+as_raw_ptr_release_ownership1", "[S]") { + std::unique_ptr orig_owner(new int(19)); + auto hld = smart_holder::from_unique_ptr(std::move(orig_owner)); + REQUIRE(orig_owner.get() == nullptr); + auto new_owner = std::unique_ptr(poc::as_raw_ptr_release_ownership(hld)); + REQUIRE(!hld.has_pointee()); + REQUIRE(*new_owner == 19); +} + +TEST_CASE("from_unique_ptr+as_raw_ptr_release_ownership2", "[E]") { + std::unique_ptr orig_owner(new int(19)); + auto hld = smart_holder::from_unique_ptr(std::move(orig_owner)); + REQUIRE(orig_owner.get() == nullptr); + auto shd_ptr = hld.as_shared_ptr(); + REQUIRE_THROWS_WITH(poc::as_raw_ptr_release_ownership(hld), + "Cannot disown use_count != 1 (as_raw_ptr_release_ownership)."); +} + +TEST_CASE("from_unique_ptr+as_unique_ptr1", "[S]") { + std::unique_ptr orig_owner(new int(19)); + auto hld = smart_holder::from_unique_ptr(std::move(orig_owner)); + REQUIRE(orig_owner.get() == nullptr); + std::unique_ptr new_owner = poc::as_unique_ptr(hld); + REQUIRE(!hld.has_pointee()); + REQUIRE(*new_owner == 19); +} + +TEST_CASE("from_unique_ptr+as_unique_ptr2", "[E]") { + std::unique_ptr orig_owner(new int(19)); + auto hld = smart_holder::from_unique_ptr(std::move(orig_owner)); + REQUIRE(orig_owner.get() == nullptr); + auto shd_ptr = hld.as_shared_ptr(); + REQUIRE_THROWS_WITH(poc::as_unique_ptr(hld), + "Cannot disown use_count != 1 (as_unique_ptr)."); +} + +TEST_CASE("from_unique_ptr+as_unique_ptr_with_deleter", "[E]") { + std::unique_ptr orig_owner(new int(19)); + auto hld = smart_holder::from_unique_ptr(std::move(orig_owner)); + REQUIRE(orig_owner.get() == nullptr); + REQUIRE_THROWS_WITH((poc::as_unique_ptr>(hld)), + "Incompatible unique_ptr deleter (as_unique_ptr)."); +} + +TEST_CASE("from_unique_ptr+as_shared_ptr", "[S]") { + std::unique_ptr orig_owner(new int(19)); + auto hld = smart_holder::from_unique_ptr(std::move(orig_owner)); + REQUIRE(orig_owner.get() == nullptr); + std::shared_ptr new_owner = hld.as_shared_ptr(); + REQUIRE(hld.has_pointee()); + REQUIRE(*new_owner == 19); +} + +TEST_CASE("from_unique_ptr_derived+as_unique_ptr_base", "[S]") { + std::unique_ptr orig_owner(new helpers::derived()); + auto hld = smart_holder::from_unique_ptr(std::move(orig_owner)); + REQUIRE(orig_owner.get() == nullptr); + std::unique_ptr new_owner = poc::as_unique_ptr(hld); + REQUIRE(!hld.has_pointee()); + REQUIRE(new_owner->get() == 100); +} + +TEST_CASE("from_unique_ptr_derived+as_unique_ptr_base2", "[E]") { + std::unique_ptr> orig_owner( + new helpers::derived()); + auto hld = smart_holder::from_unique_ptr(std::move(orig_owner)); + REQUIRE(orig_owner.get() == nullptr); + REQUIRE_THROWS_WITH( + (poc::as_unique_ptr>(hld)), + "Incompatible unique_ptr deleter (as_unique_ptr)."); +} + +TEST_CASE("from_unique_ptr_with_deleter+as_lvalue_ref", "[S]") { + std::unique_ptr> orig_owner(new int(19)); + auto hld = smart_holder::from_unique_ptr(std::move(orig_owner)); + REQUIRE(orig_owner.get() == nullptr); + REQUIRE(poc::as_lvalue_ref(hld) == 19); +} + +TEST_CASE("from_unique_ptr_with_std_function_deleter+as_lvalue_ref", "[S]") { + std::unique_ptr> orig_owner( + new int(19), [](const int *raw_ptr) { delete raw_ptr; }); + auto hld = smart_holder::from_unique_ptr(std::move(orig_owner)); + REQUIRE(orig_owner.get() == nullptr); + REQUIRE(poc::as_lvalue_ref(hld) == 19); +} + +TEST_CASE("from_unique_ptr_with_deleter+as_raw_ptr_release_ownership", "[E]") { + std::unique_ptr> orig_owner(new int(19)); + auto hld = smart_holder::from_unique_ptr(std::move(orig_owner)); + REQUIRE(orig_owner.get() == nullptr); + REQUIRE_THROWS_WITH(poc::as_raw_ptr_release_ownership(hld), + "Cannot disown custom deleter (as_raw_ptr_release_ownership)."); +} + +TEST_CASE("from_unique_ptr_with_deleter+as_unique_ptr", "[E]") { + std::unique_ptr> orig_owner(new int(19)); + auto hld = smart_holder::from_unique_ptr(std::move(orig_owner)); + REQUIRE(orig_owner.get() == nullptr); + REQUIRE_THROWS_WITH(poc::as_unique_ptr(hld), + "Incompatible unique_ptr deleter (as_unique_ptr)."); +} + +TEST_CASE("from_unique_ptr_with_deleter+as_unique_ptr_with_deleter1", "[S]") { + std::unique_ptr> orig_owner(new int(19)); + auto hld = smart_holder::from_unique_ptr(std::move(orig_owner)); + REQUIRE(orig_owner.get() == nullptr); + std::unique_ptr> new_owner + = poc::as_unique_ptr>(hld); + REQUIRE(!hld.has_pointee()); + REQUIRE(*new_owner == 19); +} + +TEST_CASE("from_unique_ptr_with_deleter+as_unique_ptr_with_deleter2", "[E]") { + std::unique_ptr> orig_owner(new int(19)); + auto hld = smart_holder::from_unique_ptr(std::move(orig_owner)); + REQUIRE(orig_owner.get() == nullptr); + REQUIRE_THROWS_WITH((poc::as_unique_ptr>(hld)), + "Incompatible unique_ptr deleter (as_unique_ptr)."); +} + +TEST_CASE("from_unique_ptr_with_deleter+as_shared_ptr", "[S]") { + std::unique_ptr> orig_owner(new int(19)); + auto hld = smart_holder::from_unique_ptr(std::move(orig_owner)); + REQUIRE(orig_owner.get() == nullptr); + std::shared_ptr new_owner = hld.as_shared_ptr(); + REQUIRE(hld.has_pointee()); + REQUIRE(*new_owner == 19); +} + +TEST_CASE("from_shared_ptr+as_lvalue_ref", "[S]") { + std::shared_ptr orig_owner(new int(19)); + auto hld = smart_holder::from_shared_ptr(orig_owner); + REQUIRE(poc::as_lvalue_ref(hld) == 19); +} + +TEST_CASE("from_shared_ptr+as_raw_ptr_release_ownership", "[E]") { + std::shared_ptr orig_owner(new int(19)); + auto hld = smart_holder::from_shared_ptr(orig_owner); + REQUIRE_THROWS_WITH(poc::as_raw_ptr_release_ownership(hld), + "Cannot disown external shared_ptr (as_raw_ptr_release_ownership)."); +} + +TEST_CASE("from_shared_ptr+as_unique_ptr", "[E]") { + std::shared_ptr orig_owner(new int(19)); + auto hld = smart_holder::from_shared_ptr(orig_owner); + REQUIRE_THROWS_WITH(poc::as_unique_ptr(hld), + "Cannot disown external shared_ptr (as_unique_ptr)."); +} + +TEST_CASE("from_shared_ptr+as_unique_ptr_with_deleter", "[E]") { + std::shared_ptr orig_owner(new int(19)); + auto hld = smart_holder::from_shared_ptr(orig_owner); + REQUIRE_THROWS_WITH((poc::as_unique_ptr>(hld)), + "Missing unique_ptr deleter (as_unique_ptr)."); +} + +TEST_CASE("from_shared_ptr+as_shared_ptr", "[S]") { + std::shared_ptr orig_owner(new int(19)); + auto hld = smart_holder::from_shared_ptr(orig_owner); + REQUIRE(*hld.as_shared_ptr() == 19); +} + +TEST_CASE("error_unpopulated_holder", "[E]") { + smart_holder hld; + REQUIRE_THROWS_WITH(poc::as_lvalue_ref(hld), "Unpopulated holder (as_lvalue_ref)."); +} + +TEST_CASE("error_disowned_holder", "[E]") { + auto hld = smart_holder::from_raw_ptr_take_ownership(new int(19)); + poc::as_unique_ptr(hld); + REQUIRE_THROWS_WITH(poc::as_lvalue_ref(hld), "Disowned holder (as_lvalue_ref)."); +} + +TEST_CASE("error_cannot_disown_nullptr", "[E]") { + auto hld = smart_holder::from_raw_ptr_take_ownership(new int(19)); + poc::as_unique_ptr(hld); + REQUIRE_THROWS_WITH(poc::as_unique_ptr(hld), "Cannot disown nullptr (as_unique_ptr)."); +} + +TEST_CASE("indestructible_int-from_raw_ptr_unowned+as_raw_ptr_unowned", "[S]") { + using zombie = helpers::indestructible_int; + +// This is from C++17 +#ifdef __cpp_lib_byte + using raw_byte = std::byte; +#else + using raw_byte = char; +#endif + + // Using placement new instead of plain new, to not trigger leak sanitizer errors. + alignas(zombie) raw_byte memory_block[sizeof(zombie)]; + + auto *value = new (memory_block) zombie(19); + auto hld = smart_holder::from_raw_ptr_unowned(value); + REQUIRE(hld.as_raw_ptr_unowned()->valu == 19); +} + +TEST_CASE("indestructible_int-from_raw_ptr_take_ownership", "[E]") { + helpers::indestructible_int *value = nullptr; + REQUIRE_THROWS_WITH(smart_holder::from_raw_ptr_take_ownership(value), + "Pointee is not destructible (from_raw_ptr_take_ownership)."); +} + +TEST_CASE("from_raw_ptr_take_ownership+as_shared_ptr-outliving_smart_holder", "[S]") { + // Exercises guarded_builtin_delete flag_ptr validity past destruction of smart_holder. + std::shared_ptr longer_living; + { + auto hld = smart_holder::from_raw_ptr_take_ownership(new int(19)); + longer_living = hld.as_shared_ptr(); + } + REQUIRE(*longer_living == 19); +} + +TEST_CASE("from_unique_ptr_with_deleter+as_shared_ptr-outliving_smart_holder", "[S]") { + // Exercises guarded_custom_deleter flag_ptr validity past destruction of smart_holder. + std::shared_ptr longer_living; + { + std::unique_ptr> orig_owner(new int(19)); + auto hld = smart_holder::from_unique_ptr(std::move(orig_owner)); + longer_living = hld.as_shared_ptr(); + } + REQUIRE(*longer_living == 19); +} diff --git a/external_libraries/pybind11/tests/pybind11_cross_module_tests.cpp b/external_libraries/pybind11/tests/pybind11_cross_module_tests.cpp new file mode 100644 index 00000000..9a00c00d --- /dev/null +++ b/external_libraries/pybind11/tests/pybind11_cross_module_tests.cpp @@ -0,0 +1,163 @@ +/* + tests/pybind11_cross_module_tests.cpp -- contains tests that require multiple modules + + Copyright (c) 2017 Jason Rhinelander + + All rights reserved. Use of this source code is governed by a + BSD-style license that can be found in the LICENSE file. +*/ + +#include + +#include "local_bindings.h" +#include "pybind11_tests.h" +#include "test_exceptions.h" + +#include +#include + +class CrossDSOClass { +public: + CrossDSOClass() = default; + virtual ~CrossDSOClass(); + CrossDSOClass(const CrossDSOClass &) = default; +}; + +CrossDSOClass::~CrossDSOClass() = default; + +PYBIND11_MODULE(pybind11_cross_module_tests, m, py::mod_gil_not_used()) { + m.doc() = "pybind11 cross-module test module"; + + // test_local_bindings.py tests: + // + // Definitions here are tested by importing both this module and the + // relevant pybind11_tests submodule from a test_whatever.py + + // test_load_external + bind_local(m, "ExternalType1", py::module_local()); + bind_local>( + m, "ExternalType2", py::module_local()); + bind_local(m, "ExternalType3", py::module_local()); + + // test_exceptions.py + py::register_local_exception(m, "LocalSimpleException"); + m.def("raise_runtime_error", []() { + py::set_error(PyExc_RuntimeError, "My runtime error"); + throw py::error_already_set(); + }); + m.def("raise_value_error", []() { + py::set_error(PyExc_ValueError, "My value error"); + throw py::error_already_set(); + }); + m.def("throw_pybind_value_error", []() { throw py::value_error("pybind11 value error"); }); + m.def("throw_pybind_type_error", []() { throw py::type_error("pybind11 type error"); }); + m.def("throw_stop_iteration", []() { throw py::stop_iteration(); }); + m.def("throw_local_error", []() { throw LocalException("just local"); }); + m.def("throw_local_simple_error", []() { throw LocalSimpleException("external mod"); }); + py::register_exception_translator([](std::exception_ptr p) { + try { + if (p) { + std::rethrow_exception(p); + } + } catch (const shared_exception &e) { + py::set_error(PyExc_KeyError, e.what()); + } + }); + + // translate the local exception into a key error but only in this module + py::register_local_exception_translator([](std::exception_ptr p) { + try { + if (p) { + std::rethrow_exception(p); + } + } catch (const LocalException &e) { + py::set_error(PyExc_KeyError, e.what()); + } + }); + + // test_local_bindings.py + // Local to both: + bind_local(m, "LocalType", py::module_local()).def("get2", [](LocalType &t) { + return t.i + 2; + }); + + // Can only be called with our python type: + m.def("local_value", [](LocalType &l) { return l.i; }); + + // test_nonlocal_failure + // This registration will fail (global registration when LocalFail is already registered + // globally in the main test module): + m.def("register_nonlocal", [m]() { bind_local(m, "NonLocalType"); }); + + // test_stl_bind_local + // stl_bind.h binders defaults to py::module_local if the types are local or converting: + py::bind_vector(m, "LocalVec"); + py::bind_map(m, "LocalMap"); + + // test_stl_bind_global + // and global if the type (or one of the types, for the map) is global (so these will fail, + // assuming pybind11_tests is already loaded): + m.def("register_nonlocal_vec", [m]() { py::bind_vector(m, "NonLocalVec"); }); + m.def("register_nonlocal_map", [m]() { py::bind_map(m, "NonLocalMap"); }); + // The default can, however, be overridden to global using `py::module_local()` or + // `py::module_local(false)`. + // Explicitly made local: + py::bind_vector(m, "NonLocalVec2", py::module_local()); + // Explicitly made global (and so will fail to bind): + m.def("register_nonlocal_map2", + [m]() { py::bind_map(m, "NonLocalMap2", py::module_local(false)); }); + + // test_mixed_local_global + // We try this both with the global type registered first and vice versa (the order shouldn't + // matter). + m.def("register_mixed_global_local", + [m]() { bind_local(m, "MixedGlobalLocal", py::module_local()); }); + m.def("register_mixed_local_global", [m]() { + bind_local(m, "MixedLocalGlobal", py::module_local(false)); + }); + m.def("get_mixed_gl", [](int i) { return MixedGlobalLocal(i); }); + m.def("get_mixed_lg", [](int i) { return MixedLocalGlobal(i); }); + + // test_internal_locals_differ + m.def("local_cpp_types_addr", + []() { return (uintptr_t) &py::detail::get_local_internals().registered_types_cpp; }); + + // test_stl_caster_vs_stl_bind + py::bind_vector>(m, "VectorInt"); + + m.def("load_vector_via_binding", + [](std::vector &v) { return std::accumulate(v.begin(), v.end(), 0); }); + + // test_cross_module_calls + m.def("return_self", [](LocalVec *v) { return v; }); + m.def("return_copy", [](const LocalVec &v) { return LocalVec(v); }); + + class Dog : public pets::Pet { + public: + explicit Dog(std::string name) : Pet(std::move(name)) {} + }; + py::class_(m, "Pet", py::module_local()).def("name", &pets::Pet::name); + // Binding for local extending class: + py::class_(m, "Dog").def(py::init()); + m.def("pet_name", [](pets::Pet &p) { return p.name(); }); + + py::class_(m, "MixGL", py::module_local()).def(py::init()); + m.def("get_gl_value", [](MixGL &o) { return o.i + 100; }); + + py::class_(m, "MixGL2", py::module_local()).def(py::init()); + + // test_vector_bool + // We can't test both stl.h and stl_bind.h conversions of `std::vector` within + // the same module (it would be an ODR violation). Therefore `bind_vector` of `bool` + // is defined here and tested in `test_stl_binders.py`. + py::bind_vector>(m, "VectorBool"); + + // test_missing_header_message + // The main module already includes stl.h, but we need to test the error message + // which appears when this header is missing. + m.def("missing_header_arg", [](const std::vector &) {}); + m.def("missing_header_return", []() { return std::vector(); }); + + // test_class_cross_module_use_after_one_module_dealloc + m.def("consume_cross_dso_class", [](const CrossDSOClass &) {}); +} diff --git a/external_libraries/pybind11/tests/pybind11_tests.cpp b/external_libraries/pybind11/tests/pybind11_tests.cpp new file mode 100644 index 00000000..5dacd7ae --- /dev/null +++ b/external_libraries/pybind11/tests/pybind11_tests.cpp @@ -0,0 +1,141 @@ +/* + tests/pybind11_tests.cpp -- pybind example plugin + + Copyright (c) 2016 Wenzel Jakob + + All rights reserved. Use of this source code is governed by a + BSD-style license that can be found in the LICENSE file. +*/ + +#include "pybind11_tests.h" + +#include "constructor_stats.h" + +#include +#include + +/* +For testing purposes, we define a static global variable here in a function that each individual +test .cpp calls with its initialization lambda. It's convenient here because we can just not +compile some test files to disable/ignore some of the test code. + +It is NOT recommended as a way to use pybind11 in practice, however: the initialization order will +be essentially random, which is okay for our test scripts (there are no dependencies between the +individual pybind11 test .cpp files), but most likely not what you want when using pybind11 +productively. + +Instead, see the "How can I reduce the build time?" question in the "Frequently asked questions" +section of the documentation for good practice on splitting binding code over multiple files. +*/ +std::list> &initializers() { + static std::list> inits; + return inits; +} + +test_initializer::test_initializer(Initializer init) { initializers().emplace_back(init); } + +test_initializer::test_initializer(const char *submodule_name, Initializer init) { + initializers().emplace_back([=](py::module_ &parent) { + auto m = parent.def_submodule(submodule_name); + init(m); + }); +} + +void bind_ConstructorStats(py::module_ &m) { + py::class_(m, "ConstructorStats") + .def("alive", &ConstructorStats::alive) + .def("values", &ConstructorStats::values) + .def_readwrite("default_constructions", &ConstructorStats::default_constructions) + .def_readwrite("copy_assignments", &ConstructorStats::copy_assignments) + .def_readwrite("move_assignments", &ConstructorStats::move_assignments) + .def_readwrite("copy_constructions", &ConstructorStats::copy_constructions) + .def_readwrite("move_constructions", &ConstructorStats::move_constructions) + .def_static("get", + (ConstructorStats & (*) (py::object)) & ConstructorStats::get, + py::return_value_policy::reference_internal) + + // Not exactly ConstructorStats, but related: expose the internal pybind number of + // registered instances to allow instance cleanup checks (invokes a GC first) + .def_static("detail_reg_inst", []() { + ConstructorStats::gc(); + return py::detail::num_registered_instances(); + }); +} + +const char *cpp_std() { + return +#if defined(PYBIND11_CPP20) + "C++20"; +#elif defined(PYBIND11_CPP17) + "C++17"; +#elif defined(PYBIND11_CPP14) + "C++14"; +#else + "C++11"; +#endif +} + +PYBIND11_MODULE(pybind11_tests, m, py::mod_gil_not_used()) { + m.doc() = "pybind11 test module"; + + // Intentionally kept minimal to not create a maintenance chore + // ("just enough" to be conclusive). +#if defined(__VERSION__) + m.attr("compiler_info") = __VERSION__; +#elif defined(_MSC_FULL_VER) + m.attr("compiler_info") = "MSVC " PYBIND11_TOSTRING(_MSC_FULL_VER); +#else + m.attr("compiler_info") = py::none(); +#endif + m.attr("cpp_std") = cpp_std(); + m.attr("PYBIND11_INTERNALS_ID") = PYBIND11_INTERNALS_ID; + // Free threaded Python uses UINT32_MAX for immortal objects. + m.attr("PYBIND11_SIMPLE_GIL_MANAGEMENT") = +#if defined(PYBIND11_SIMPLE_GIL_MANAGEMENT) + true; +#else + false; +#endif + m.attr("PYBIND11_TEST_SMART_HOLDER") = +#if defined(PYBIND11_RUN_TESTING_WITH_SMART_HOLDER_AS_DEFAULT_BUT_NEVER_USE_IN_PRODUCTION_PLEASE) + true; +#else + false; +#endif + + bind_ConstructorStats(m); + +#if defined(PYBIND11_DETAILED_ERROR_MESSAGES) + m.attr("detailed_error_messages_enabled") = true; +#else + m.attr("detailed_error_messages_enabled") = false; +#endif + +#if defined(__cpp_noexcept_function_type) + m.attr("defined___cpp_noexcept_function_type") = true; +#else + m.attr("defined___cpp_noexcept_function_type") = false; +#endif + + py::class_(m, "UserType", "A `py::class_` type for testing") + .def(py::init<>()) + .def(py::init()) + .def("get_value", &UserType::value, "Get value using a method") + .def("set_value", &UserType::set, "Set value using a method") + .def_property("value", &UserType::value, &UserType::set, "Get/set value using a property") + .def("__repr__", [](const UserType &u) { return "UserType({})"_s.format(u.value()); }); + + py::class_(m, "IncType") + .def(py::init<>()) + .def(py::init()) + .def("__repr__", [](const IncType &u) { return "IncType({})"_s.format(u.value()); }); + + for (const auto &initializer : initializers()) { + initializer(m); + } + + py::class_(m, "TestContext") + .def(py::init<>(&TestContext::createNewContextForInit)) + .def("__enter__", &TestContext::contextEnter) + .def("__exit__", &TestContext::contextExit); +} diff --git a/external_libraries/pybind11/tests/pybind11_tests.h b/external_libraries/pybind11/tests/pybind11_tests.h new file mode 100644 index 00000000..0eb0398d --- /dev/null +++ b/external_libraries/pybind11/tests/pybind11_tests.h @@ -0,0 +1,119 @@ +#pragma once + +#include +#include + +#include + +namespace py = pybind11; +using namespace pybind11::literals; + +class test_initializer { + using Initializer = void (*)(py::module_ &); + +public: + explicit test_initializer(Initializer init); + 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)) + +/// Dummy type which is not exported anywhere -- something to trigger a conversion error +struct UnregisteredType {}; + +/// A user-defined type which is exported and can be used by any test +class UserType { +public: + UserType() = default; + explicit UserType(int i) : i(i) {} + + int value() const { return i; } + void set(int set) { i = set; } + +private: + int i = -1; +}; + +/// Like UserType, but increments `value` on copy for quick reference vs. copy tests +class IncType : public UserType { +public: + using UserType::UserType; + IncType() = default; + IncType(const IncType &other) : IncType(other.value() + 1) {} + IncType(IncType &&) = delete; + IncType &operator=(const IncType &) = delete; + IncType &operator=(IncType &&) = delete; +}; + +/// A simple union for basic testing +union IntFloat { + int i; + float f; +}; + +class UnusualOpRef { +public: + using NonTrivialType = std::shared_ptr; // Almost any non-trivial type will do. + // Overriding operator& should not break pybind11. + NonTrivialType operator&() { return non_trivial_member; } + NonTrivialType operator&() const { return non_trivial_member; } + +private: + NonTrivialType non_trivial_member; +}; + +/// Custom cast-only type that casts to a string "rvalue" or "lvalue" depending on the cast +/// context. Used to test recursive casters (e.g. std::tuple, stl containers). +struct RValueCaster {}; +PYBIND11_NAMESPACE_BEGIN(pybind11) +PYBIND11_NAMESPACE_BEGIN(detail) +template <> +class type_caster { +public: + PYBIND11_TYPE_CASTER(RValueCaster, const_name("RValueCaster")); + static handle cast(RValueCaster &&, return_value_policy, handle) { + return py::str("rvalue").release(); + } + static handle cast(const RValueCaster &, return_value_policy, handle) { + return py::str("lvalue").release(); + } +}; +PYBIND11_NAMESPACE_END(detail) +PYBIND11_NAMESPACE_END(pybind11) + +template +void ignoreOldStyleInitWarnings(F &&body) { + py::exec(R"( + message = "pybind11-bound class '.+' is using an old-style placement-new '(?:__init__|__setstate__)' which has been deprecated" + + import warnings + with warnings.catch_warnings(): + warnings.filterwarnings("ignore", message=message, category=FutureWarning) + body() + )", + py::dict(py::arg("body") = py::cpp_function(body))); +} + +// See PR #5419 for background. +class TestContext { +public: + TestContext() = delete; + TestContext(const TestContext &) = delete; + TestContext(TestContext &&) = delete; + static TestContext *createNewContextForInit() { return new TestContext("new-context"); } + + pybind11::object contextEnter() { + py::object contextObj = py::cast(*this); + return contextObj; + } + void contextExit(const pybind11::object & /*excType*/, + const pybind11::object & /*excVal*/, + const pybind11::object & /*excTb*/) {} + +private: + explicit TestContext(const std::string &context) : context(context) {} + std::string context; +}; diff --git a/external_libraries/pybind11/tests/pyproject.toml b/external_libraries/pybind11/tests/pyproject.toml new file mode 100644 index 00000000..8821ea3f --- /dev/null +++ b/external_libraries/pybind11/tests/pyproject.toml @@ -0,0 +1,40 @@ +# Warning: this is currently used to test cross-compilation, and is not a general +# out-of-tree builder for the tests (yet). Specifically, wheels can't be built from +# SDists. + +[build-system] +requires = ["scikit-build-core"] +build-backend = "scikit_build_core.build" + +[project] +name = "pybind11_tests" +version = "0.0.1" +dependencies = ["pytest", "pytest-timeout"] + + +[dependency-groups] +numpy = ["numpy"] +scipy = ["scipy"] + + +[tool.scikit-build] +build.verbose = true +logging.level = "INFO" + +[tool.scikit-build.cmake.define] +PYBIND11_FINDPYTHON = true + + +[tool.cibuildwheel] +test-sources = ["tests", "pyproject.toml"] +test-command = "python -m pytest -v -o timeout=120 -p no:cacheprovider tests" +# Pyodide doesn't have signal.setitimer, so pytest-timeout can't work with timeout > 0 +pyodide.test-command = "python -m pytest -v -o timeout=0 -p no:cacheprovider tests" +environment.PIP_ONLY_BINARY = "numpy" +environment.PIP_PREFER_BINARY = "1" + +android.environment.ANDROID_API_LEVEL = "24" # Needed to include libc++ in the wheel. +pyodide.test-groups = ["numpy", "scipy"] +ios.test-groups = ["numpy"] +ios.xbuild-tools = ["cmake", "ninja"] +ios.environment.PIP_EXTRA_INDEX_URL = "https://pypi.anaconda.org/beeware/simple" diff --git a/external_libraries/pybind11/tests/pytest.ini b/external_libraries/pybind11/tests/pytest.ini new file mode 100644 index 00000000..6147c6be --- /dev/null +++ b/external_libraries/pybind11/tests/pytest.ini @@ -0,0 +1,22 @@ +[pytest] +minversion = 6 +norecursedirs = test_* extra_* +xfail_strict = True +addopts = + # show summary of tests + -ra + # capture only Python print and C++ py::print, but not C output (low-level Python errors) + --capture=sys + # Show local info when a failure occurs + --showlocals +log_level = INFO +filterwarnings = + # make warnings into errors but ignore certain third-party extension issues + error + # somehow, some DeprecationWarnings do not get turned into errors + always::DeprecationWarning + # importing scipy submodules on some version of Python + ignore::ImportWarning + # bogus numpy ABI warning (see numpy/#432) + ignore:.*numpy.dtype size changed.*:RuntimeWarning + ignore:.*numpy.ufunc size changed.*:RuntimeWarning diff --git a/external_libraries/pybind11/tests/requirements.txt b/external_libraries/pybind11/tests/requirements.txt new file mode 100644 index 00000000..39bf8678 --- /dev/null +++ b/external_libraries/pybind11/tests/requirements.txt @@ -0,0 +1,20 @@ +--extra-index-url=https://www.graalvm.org/python/wheels +--only-binary=:all: +build>=1 +numpy~=1.23.0; python_version=="3.8" and platform_python_implementation=="PyPy" +numpy~=1.25.0; python_version=="3.9" and platform_python_implementation=="PyPy" +numpy~=2.2.0; python_version=="3.10" and platform_python_implementation=="PyPy" +numpy~=1.26.0; platform_python_implementation=="GraalVM" and sys_platform=="linux" +numpy~=1.21.5; platform_python_implementation=="CPython" and python_version>="3.8" and python_version<"3.10" +numpy~=1.22.2; platform_python_implementation=="CPython" and python_version=="3.10" +numpy~=1.26.0; platform_python_implementation=="CPython" and python_version>="3.11" and python_version<"3.13" and platform_machine!="ARM64" +numpy>=2.3.0; platform_python_implementation=="CPython" and python_version>="3.11" and platform_machine=="ARM64" +numpy~=2.2.0; platform_python_implementation=="CPython" and python_version=="3.13" and platform_machine!="ARM64" +numpy>=2.4.0; platform_python_implementation=="CPython" and python_version>="3.14" +pytest>=6 +pytest-timeout +scipy~=1.5.4; platform_python_implementation=="CPython" and python_version<"3.10" +scipy~=1.8.0; platform_python_implementation=="CPython" and python_version=="3.10" and sys_platform!="win32" +scipy~=1.11.1; platform_python_implementation=="CPython" and python_version>="3.11" and python_version<"3.13" and sys_platform!="win32" +scipy~=1.15.2; platform_python_implementation=="CPython" and python_version=="3.13" and sys_platform!="win32" +tomlkit diff --git a/external_libraries/pybind11/tests/standalone_enum_module.cpp b/external_libraries/pybind11/tests/standalone_enum_module.cpp new file mode 100644 index 00000000..7e917b97 --- /dev/null +++ b/external_libraries/pybind11/tests/standalone_enum_module.cpp @@ -0,0 +1,13 @@ +// Copyright (c) 2026 The pybind Community. + +#include + +namespace standalone_enum_module_ns { +enum SomeEnum {}; +} // namespace standalone_enum_module_ns + +using namespace standalone_enum_module_ns; + +PYBIND11_MODULE(standalone_enum_module, m) { // Added in PR #6015 + pybind11::enum_ some_enum_wrapper(m, "SomeEnum"); +} diff --git a/external_libraries/pybind11/tests/test_async.cpp b/external_libraries/pybind11/tests/test_async.cpp new file mode 100644 index 00000000..a5d72246 --- /dev/null +++ b/external_libraries/pybind11/tests/test_async.cpp @@ -0,0 +1,25 @@ +/* + tests/test_async.cpp -- __await__ support + + Copyright (c) 2019 Google Inc. + + All rights reserved. Use of this source code is governed by a + BSD-style license that can be found in the LICENSE file. +*/ + +#include "pybind11_tests.h" + +TEST_SUBMODULE(async_module, m) { + struct DoesNotSupportAsync {}; + py::class_(m, "DoesNotSupportAsync").def(py::init<>()); + struct SupportsAsync {}; + py::class_(m, "SupportsAsync") + .def(py::init<>()) + .def("__await__", [](const SupportsAsync &self) -> py::object { + static_cast(self); + py::object loop = py::module_::import("asyncio.events").attr("get_event_loop")(); + py::object f = loop.attr("create_future")(); + f.attr("set_result")(5); + return f.attr("__await__")(); + }); +} diff --git a/external_libraries/pybind11/tests/test_async.py b/external_libraries/pybind11/tests/test_async.py new file mode 100644 index 00000000..64f4d6a7 --- /dev/null +++ b/external_libraries/pybind11/tests/test_async.py @@ -0,0 +1,31 @@ +from __future__ import annotations + +import sys + +import pytest + +asyncio = pytest.importorskip("asyncio") +m = pytest.importorskip("pybind11_tests.async_module") + +if sys.platform.startswith("emscripten"): + pytest.skip("Can't run a new event_loop in pyodide", allow_module_level=True) + + +@pytest.fixture +def event_loop(): + loop = asyncio.new_event_loop() + yield loop + loop.close() + + +async def get_await_result(x): + return await x + + +def test_await(event_loop): + assert event_loop.run_until_complete(get_await_result(m.SupportsAsync())) == 5 + + +def test_await_missing(event_loop): + with pytest.raises(TypeError): + event_loop.run_until_complete(get_await_result(m.DoesNotSupportAsync())) diff --git a/external_libraries/pybind11/tests/test_buffers.cpp b/external_libraries/pybind11/tests/test_buffers.cpp new file mode 100644 index 00000000..5ac4553a --- /dev/null +++ b/external_libraries/pybind11/tests/test_buffers.cpp @@ -0,0 +1,571 @@ +/* + tests/test_buffers.cpp -- supporting Pythons' buffer protocol + + Copyright (c) 2016 Wenzel Jakob + + All rights reserved. Use of this source code is governed by a + BSD-style license that can be found in the LICENSE file. +*/ + +#include +#include + +#include "constructor_stats.h" +#include "pybind11_tests.h" + +TEST_SUBMODULE(buffers, m) { + m.attr("long_double_and_double_have_same_size") = (sizeof(long double) == sizeof(double)); + + m.def("format_descriptor_format_buffer_info_equiv", + [](const std::string &cpp_name, const py::buffer &buffer) { + // https://google.github.io/styleguide/cppguide.html#Static_and_Global_Variables + static auto *format_table = new std::map; + static auto *equiv_table + = new std::map; + if (format_table->empty()) { +#define PYBIND11_ASSIGN_HELPER(...) \ + (*format_table)[#__VA_ARGS__] = py::format_descriptor<__VA_ARGS__>::format(); \ + (*equiv_table)[#__VA_ARGS__] = &py::buffer_info::item_type_is_equivalent_to<__VA_ARGS__>; + PYBIND11_ASSIGN_HELPER(PyObject *) + PYBIND11_ASSIGN_HELPER(bool) + PYBIND11_ASSIGN_HELPER(std::int8_t) + PYBIND11_ASSIGN_HELPER(std::uint8_t) + PYBIND11_ASSIGN_HELPER(std::int16_t) + PYBIND11_ASSIGN_HELPER(std::uint16_t) + PYBIND11_ASSIGN_HELPER(std::int32_t) + PYBIND11_ASSIGN_HELPER(std::uint32_t) + PYBIND11_ASSIGN_HELPER(std::int64_t) + PYBIND11_ASSIGN_HELPER(std::uint64_t) + PYBIND11_ASSIGN_HELPER(float) + PYBIND11_ASSIGN_HELPER(double) + PYBIND11_ASSIGN_HELPER(long double) + PYBIND11_ASSIGN_HELPER(std::complex) + PYBIND11_ASSIGN_HELPER(std::complex) + PYBIND11_ASSIGN_HELPER(std::complex) +#undef PYBIND11_ASSIGN_HELPER + } + return std::pair( + (*format_table)[cpp_name], (buffer.request().*((*equiv_table)[cpp_name]))()); + }); + + // test_from_python / test_to_python: + class Matrix { + public: + Matrix(py::ssize_t rows, py::ssize_t cols) : m_rows(rows), m_cols(cols) { + print_created(this, std::to_string(m_rows) + "x" + std::to_string(m_cols) + " matrix"); + // NOLINTNEXTLINE(cppcoreguidelines-prefer-member-initializer) + m_data = new float[(size_t) (rows * cols)]; + memset(m_data, 0, sizeof(float) * (size_t) (rows * cols)); + } + + Matrix(const Matrix &s) : m_rows(s.m_rows), m_cols(s.m_cols) { + print_copy_created(this, + std::to_string(m_rows) + "x" + std::to_string(m_cols) + " matrix"); + // NOLINTNEXTLINE(cppcoreguidelines-prefer-member-initializer) + m_data = new float[(size_t) (m_rows * m_cols)]; + memcpy(m_data, s.m_data, sizeof(float) * (size_t) (m_rows * m_cols)); + } + + Matrix(Matrix &&s) noexcept : m_rows(s.m_rows), m_cols(s.m_cols), m_data(s.m_data) { + print_move_created(this); + s.m_rows = 0; + s.m_cols = 0; + s.m_data = nullptr; + } + + ~Matrix() { + print_destroyed(this, + std::to_string(m_rows) + "x" + std::to_string(m_cols) + " matrix"); + delete[] m_data; + } + + Matrix &operator=(const Matrix &s) { + 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; + m_data = new float[(size_t) (m_rows * m_cols)]; + memcpy(m_data, s.m_data, sizeof(float) * (size_t) (m_rows * m_cols)); + return *this; + } + + Matrix &operator=(Matrix &&s) noexcept { + print_move_assigned(this, + std::to_string(m_rows) + "x" + std::to_string(m_cols) + " matrix"); + if (&s != this) { + delete[] m_data; + m_rows = s.m_rows; + m_cols = s.m_cols; + m_data = s.m_data; + s.m_rows = 0; + s.m_cols = 0; + s.m_data = nullptr; + } + return *this; + } + + float operator()(py::ssize_t i, py::ssize_t j) const { + return m_data[(size_t) (i * m_cols + j)]; + } + + float &operator()(py::ssize_t i, py::ssize_t j) { + return m_data[(size_t) (i * m_cols + j)]; + } + + float *data() { return m_data; } + + py::ssize_t rows() const { return m_rows; } + py::ssize_t cols() const { return m_cols; } + + private: + py::ssize_t m_rows; + py::ssize_t m_cols; + float *m_data; + }; + py::class_(m, "Matrix", py::buffer_protocol()) + .def(py::init()) + /// Construct from a buffer + .def(py::init([](const py::buffer &b) { + py::buffer_info info = b.request(); + if (info.format != py::format_descriptor::format() || info.ndim != 2) { + throw std::runtime_error("Incompatible buffer format!"); + } + + auto *v = new Matrix(info.shape[0], info.shape[1]); + memcpy(v->data(), info.ptr, sizeof(float) * (size_t) (v->rows() * v->cols())); + return v; + })) + + .def("rows", &Matrix::rows) + .def("cols", &Matrix::cols) + + /// Bare bones interface + .def("__getitem__", + [](const Matrix &m, std::pair i) { + if (i.first >= m.rows() || i.second >= m.cols()) { + throw py::index_error(); + } + return m(i.first, i.second); + }) + .def("__setitem__", + [](Matrix &m, std::pair i, float v) { + if (i.first >= m.rows() || i.second >= m.cols()) { + throw py::index_error(); + } + m(i.first, i.second) = v; + }) + /// Provide buffer access + .def_buffer([](Matrix &m) -> py::buffer_info { + return py::buffer_info( + m.data(), /* Pointer to buffer */ + {m.rows(), m.cols()}, /* Buffer dimensions */ + {sizeof(float) * size_t(m.cols()), /* Strides (in bytes) for each index */ + sizeof(float)}); + }); + + // A matrix that uses Fortran storage order. + class FortranMatrix : public Matrix { + public: + FortranMatrix(py::ssize_t rows, py::ssize_t cols) : Matrix(cols, rows) { + print_created(this, + std::to_string(rows) + "x" + std::to_string(cols) + " Fortran matrix"); + } + + float operator()(py::ssize_t i, py::ssize_t j) const { return Matrix::operator()(j, i); } + + float &operator()(py::ssize_t i, py::ssize_t j) { return Matrix::operator()(j, i); } + + using Matrix::data; + + py::ssize_t rows() const { return Matrix::cols(); } + py::ssize_t cols() const { return Matrix::rows(); } + }; + py::class_(m, "FortranMatrix", py::buffer_protocol()) + .def(py::init()) + + .def("rows", &FortranMatrix::rows) + .def("cols", &FortranMatrix::cols) + + /// Bare bones interface + .def("__getitem__", + [](const FortranMatrix &m, std::pair i) { + if (i.first >= m.rows() || i.second >= m.cols()) { + throw py::index_error(); + } + return m(i.first, i.second); + }) + .def("__setitem__", + [](FortranMatrix &m, std::pair i, float v) { + if (i.first >= m.rows() || i.second >= m.cols()) { + throw py::index_error(); + } + m(i.first, i.second) = v; + }) + /// Provide buffer access + .def_buffer([](FortranMatrix &m) -> py::buffer_info { + return py::buffer_info(m.data(), /* Pointer to buffer */ + {m.rows(), m.cols()}, /* Buffer dimensions */ + /* Strides (in bytes) for each index */ + {sizeof(float), sizeof(float) * size_t(m.rows())}); + }); + + // A matrix that uses a discontiguous underlying memory block. + class DiscontiguousMatrix : public Matrix { + public: + DiscontiguousMatrix(py::ssize_t rows, + py::ssize_t cols, + py::ssize_t row_factor, + py::ssize_t col_factor) + : Matrix(rows * row_factor, cols * col_factor), m_row_factor(row_factor), + m_col_factor(col_factor) { + print_created(this, + std::to_string(rows) + "(*" + std::to_string(row_factor) + ")x" + + std::to_string(cols) + "(*" + std::to_string(col_factor) + + ") matrix"); + } + DiscontiguousMatrix(const DiscontiguousMatrix &) = delete; + ~DiscontiguousMatrix() { + print_destroyed(this, + std::to_string(rows() / m_row_factor) + "(*" + + std::to_string(m_row_factor) + ")x" + + std::to_string(cols() / m_col_factor) + "(*" + + std::to_string(m_col_factor) + ") matrix"); + } + + float operator()(py::ssize_t i, py::ssize_t j) const { + return Matrix::operator()(i * m_row_factor, j * m_col_factor); + } + + float &operator()(py::ssize_t i, py::ssize_t j) { + return Matrix::operator()(i * m_row_factor, j * m_col_factor); + } + + using Matrix::data; + + py::ssize_t rows() const { return Matrix::rows() / m_row_factor; } + py::ssize_t cols() const { return Matrix::cols() / m_col_factor; } + py::ssize_t row_factor() const { return m_row_factor; } + py::ssize_t col_factor() const { return m_col_factor; } + + private: + py::ssize_t m_row_factor; + py::ssize_t m_col_factor; + }; + py::class_(m, "DiscontiguousMatrix", py::buffer_protocol()) + .def(py::init()) + + .def("rows", &DiscontiguousMatrix::rows) + .def("cols", &DiscontiguousMatrix::cols) + + /// Bare bones interface + .def("__getitem__", + [](const DiscontiguousMatrix &m, std::pair i) { + if (i.first >= m.rows() || i.second >= m.cols()) { + throw py::index_error(); + } + return m(i.first, i.second); + }) + .def("__setitem__", + [](DiscontiguousMatrix &m, std::pair i, float v) { + if (i.first >= m.rows() || i.second >= m.cols()) { + throw py::index_error(); + } + m(i.first, i.second) = v; + }) + /// Provide buffer access + .def_buffer([](DiscontiguousMatrix &m) -> py::buffer_info { + return py::buffer_info(m.data(), /* Pointer to buffer */ + {m.rows(), m.cols()}, /* Buffer dimensions */ + /* Strides (in bytes) for each index */ + {size_t(m.col_factor()) * sizeof(float) * size_t(m.cols()) + * size_t(m.row_factor()), + size_t(m.col_factor()) * sizeof(float)}); + }); + + class BrokenMatrix : public Matrix { + public: + BrokenMatrix(py::ssize_t rows, py::ssize_t cols) : Matrix(rows, cols) {} + void throw_runtime_error() { throw std::runtime_error("See PR #5324 for context."); } + }; + py::class_(m, "BrokenMatrix", py::buffer_protocol()) + .def(py::init()) + .def_buffer([](BrokenMatrix &m) { + m.throw_runtime_error(); + return py::buffer_info(); + }); + + // test_inherited_protocol + class SquareMatrix : public Matrix { + public: + explicit SquareMatrix(py::ssize_t n) : Matrix(n, n) {} + }; + // Derived classes inherit the buffer protocol and the buffer access function + py::class_(m, "SquareMatrix").def(py::init()); + + // test_pointer_to_member_fn + // Tests that passing a pointer to member to the base class works in + // the derived class. + struct Buffer { + int32_t value = 0; + + py::buffer_info get_buffer_info() { + return py::buffer_info( + &value, sizeof(value), py::format_descriptor::format(), 1); + } + }; + py::class_(m, "Buffer", py::buffer_protocol()) + .def(py::init<>()) + .def_readwrite("value", &Buffer::value) + .def_buffer(&Buffer::get_buffer_info); + + class ConstBuffer { + std::unique_ptr value; + + public: + int32_t get_value() const { return *value; } + void set_value(int32_t v) { *value = v; } + + py::buffer_info get_buffer_info() const { + return py::buffer_info( + value.get(), sizeof(*value), py::format_descriptor::format(), 1); + } + + ConstBuffer() : value(new int32_t{0}) {} + }; + py::class_(m, "ConstBuffer", py::buffer_protocol()) + .def(py::init<>()) + .def_property("value", &ConstBuffer::get_value, &ConstBuffer::set_value) + .def_buffer(&ConstBuffer::get_buffer_info); + + struct DerivedBuffer : public Buffer {}; + py::class_(m, "DerivedBuffer", py::buffer_protocol()) + .def(py::init<>()) + .def_readwrite("value", (int32_t DerivedBuffer::*) &DerivedBuffer::value) + .def_buffer(&DerivedBuffer::get_buffer_info); + + struct BufferReadOnly { + const uint8_t value = 0; + explicit BufferReadOnly(uint8_t value) : value(value) {} + + py::buffer_info get_buffer_info() { return py::buffer_info(&value, 1); } + }; + py::class_(m, "BufferReadOnly", py::buffer_protocol()) + .def(py::init()) + .def_buffer(&BufferReadOnly::get_buffer_info); + + struct BufferReadOnlySelect { + uint8_t value = 0; + bool readonly = false; + + py::buffer_info get_buffer_info() { return py::buffer_info(&value, 1, readonly); } + }; + py::class_(m, "BufferReadOnlySelect", py::buffer_protocol()) + .def(py::init<>()) + .def_readwrite("value", &BufferReadOnlySelect::value) + .def_readwrite("readonly", &BufferReadOnlySelect::readonly) + .def_buffer(&BufferReadOnlySelect::get_buffer_info); + + // Expose buffer_info for testing. + py::class_(m, "buffer_info") + .def(py::init<>()) + .def_readonly("itemsize", &py::buffer_info::itemsize) + .def_readonly("size", &py::buffer_info::size) + .def_readonly("format", &py::buffer_info::format) + .def_readonly("ndim", &py::buffer_info::ndim) + .def_readonly("shape", &py::buffer_info::shape) + .def_readonly("strides", &py::buffer_info::strides) + .def_readonly("readonly", &py::buffer_info::readonly) + .def("__repr__", [](py::handle self) { + return py::str("itemsize={0.itemsize!r}, size={0.size!r}, format={0.format!r}, " + "ndim={0.ndim!r}, shape={0.shape!r}, strides={0.strides!r}, " + "readonly={0.readonly!r}") + .format(self); + }); + + m.def("get_buffer_info", [](const py::buffer &buffer) { return buffer.request(); }); + + // Expose Py_buffer for testing. + m.attr("PyBUF_FORMAT") = PyBUF_FORMAT; + m.attr("PyBUF_SIMPLE") = PyBUF_SIMPLE; + m.attr("PyBUF_ND") = PyBUF_ND; + m.attr("PyBUF_STRIDES") = PyBUF_STRIDES; + m.attr("PyBUF_INDIRECT") = PyBUF_INDIRECT; + m.attr("PyBUF_C_CONTIGUOUS") = PyBUF_C_CONTIGUOUS; + m.attr("PyBUF_F_CONTIGUOUS") = PyBUF_F_CONTIGUOUS; + m.attr("PyBUF_ANY_CONTIGUOUS") = PyBUF_ANY_CONTIGUOUS; + + m.def("get_py_buffer", [](const py::object &object, int flags) { + Py_buffer buffer; + memset(&buffer, 0, sizeof(Py_buffer)); + if (PyObject_GetBuffer(object.ptr(), &buffer, flags) == -1) { + throw py::error_already_set(); + } + + auto SimpleNamespace = py::module_::import("types").attr("SimpleNamespace"); + py::object result = SimpleNamespace("len"_a = buffer.len, + "readonly"_a = buffer.readonly, + "itemsize"_a = buffer.itemsize, + "format"_a = buffer.format, + "ndim"_a = buffer.ndim, + "shape"_a = py::none(), + "strides"_a = py::none(), + "suboffsets"_a = py::none()); + if (buffer.shape != nullptr) { + py::list l; + for (auto i = 0; i < buffer.ndim; i++) { + l.append(buffer.shape[i]); + } + py::setattr(result, "shape", l); + } + if (buffer.strides != nullptr) { + py::list l; + for (auto i = 0; i < buffer.ndim; i++) { + l.append(buffer.strides[i]); + } + py::setattr(result, "strides", l); + } + if (buffer.suboffsets != nullptr) { + py::list l; + for (auto i = 0; i < buffer.ndim; i++) { + l.append(buffer.suboffsets[i]); + } + py::setattr(result, "suboffsets", l); + } + + PyBuffer_Release(&buffer); + return result; + }); + + // test_noexcept_def_buffer (issue #2234) + // def_buffer(Return (Class::*)(Args...) noexcept) and + // def_buffer(Return (Class::*)(Args...) const noexcept) must compile and work correctly. + struct OneDBuffer { + // Declare m_data before m_n to match initialiser list order below. + float *m_data; + py::ssize_t m_n; + explicit OneDBuffer(py::ssize_t n) : m_data(new float[(size_t) n]()), m_n(n) {} + ~OneDBuffer() { delete[] m_data; } + // Exercises def_buffer(Return (Class::*)(Args...) noexcept) + py::buffer_info get_buffer() noexcept { + return py::buffer_info(m_data, + sizeof(float), + py::format_descriptor::format(), + 1, + {m_n}, + {(py::ssize_t) sizeof(float)}); + } + }; + + // non-const noexcept member function form + py::class_(m, "OneDBuffer", py::buffer_protocol()) + .def(py::init()) + .def_buffer(&OneDBuffer::get_buffer); + + // const noexcept member function form (separate class to avoid ambiguity) + struct OneDBufferConst { + float *m_data; + py::ssize_t m_n; + explicit OneDBufferConst(py::ssize_t n) : m_data(new float[(size_t) n]()), m_n(n) {} + ~OneDBufferConst() { delete[] m_data; } + // Exercises def_buffer(Return (Class::*)(Args...) const noexcept) + py::buffer_info get_buffer() const noexcept { + return py::buffer_info(m_data, + sizeof(float), + py::format_descriptor::format(), + 1, + {m_n}, + {(py::ssize_t) sizeof(float)}, + /*readonly=*/true); + } + }; + py::class_(m, "OneDBufferConst", py::buffer_protocol()) + .def(py::init()) + .def_buffer(&OneDBufferConst::get_buffer); + + // test_ref_qualified_def_buffer + struct OneDBufferLRef { + float *m_data; + py::ssize_t m_n; + explicit OneDBufferLRef(py::ssize_t n) : m_data(new float[(size_t) n]()), m_n(n) {} + ~OneDBufferLRef() { delete[] m_data; } + // Exercises def_buffer(Return (Class::*)(Args...) &) + py::buffer_info get_buffer() & { + return py::buffer_info(m_data, + sizeof(float), + py::format_descriptor::format(), + 1, + {m_n}, + {(py::ssize_t) sizeof(float)}); + } + }; + py::class_(m, "OneDBufferLRef", py::buffer_protocol()) + .def(py::init()) + .def_buffer(&OneDBufferLRef::get_buffer); + + struct OneDBufferConstLRef { + float *m_data; + py::ssize_t m_n; + explicit OneDBufferConstLRef(py::ssize_t n) : m_data(new float[(size_t) n]()), m_n(n) {} + ~OneDBufferConstLRef() { delete[] m_data; } + // Exercises def_buffer(Return (Class::*)(Args...) const &) + py::buffer_info get_buffer() const & { + return py::buffer_info(m_data, + sizeof(float), + py::format_descriptor::format(), + 1, + {m_n}, + {(py::ssize_t) sizeof(float)}, + /*readonly=*/true); + } + }; + py::class_(m, "OneDBufferConstLRef", py::buffer_protocol()) + .def(py::init()) + .def_buffer(&OneDBufferConstLRef::get_buffer); + +#ifdef __cpp_noexcept_function_type + struct OneDBufferLRefNoexcept { + float *m_data; + py::ssize_t m_n; + explicit OneDBufferLRefNoexcept(py::ssize_t n) : m_data(new float[(size_t) n]()), m_n(n) {} + ~OneDBufferLRefNoexcept() { delete[] m_data; } + // Exercises def_buffer(Return (Class::*)(Args...) & noexcept) + py::buffer_info get_buffer() & noexcept { + return py::buffer_info(m_data, + sizeof(float), + py::format_descriptor::format(), + 1, + {m_n}, + {(py::ssize_t) sizeof(float)}); + } + }; + py::class_(m, "OneDBufferLRefNoexcept", py::buffer_protocol()) + .def(py::init()) + .def_buffer(&OneDBufferLRefNoexcept::get_buffer); + + struct OneDBufferConstLRefNoexcept { + float *m_data; + py::ssize_t m_n; + explicit OneDBufferConstLRefNoexcept(py::ssize_t n) + : m_data(new float[(size_t) n]()), m_n(n) {} + ~OneDBufferConstLRefNoexcept() { delete[] m_data; } + // Exercises def_buffer(Return (Class::*)(Args...) const & noexcept) + py::buffer_info get_buffer() const & noexcept { + return py::buffer_info(m_data, + sizeof(float), + py::format_descriptor::format(), + 1, + {m_n}, + {(py::ssize_t) sizeof(float)}, + /*readonly=*/true); + } + }; + py::class_( + m, "OneDBufferConstLRefNoexcept", py::buffer_protocol()) + .def(py::init()) + .def_buffer(&OneDBufferConstLRefNoexcept::get_buffer); +#endif +} diff --git a/external_libraries/pybind11/tests/test_buffers.py b/external_libraries/pybind11/tests/test_buffers.py new file mode 100644 index 00000000..c1d5770a --- /dev/null +++ b/external_libraries/pybind11/tests/test_buffers.py @@ -0,0 +1,471 @@ +from __future__ import annotations + +import ctypes +import io +import struct + +import pytest + +import env +from pybind11_tests import ConstructorStats, defined___cpp_noexcept_function_type +from pybind11_tests import buffers as m + +np = pytest.importorskip("numpy") + +if m.long_double_and_double_have_same_size: + # Determined by the compiler used to build the pybind11 tests + # (e.g. MSVC gets here, but MinGW might not). + np_float128 = None + np_complex256 = None +else: + # Determined by the compiler used to build numpy (e.g. MinGW). + np_float128 = getattr(np, *["float128"] * 2) + np_complex256 = getattr(np, *["complex256"] * 2) + +CPP_NAME_FORMAT_NP_DTYPE_TABLE = [ + ("PyObject *", "O", object), + ("bool", "?", np.bool_), + ("std::int8_t", "b", np.int8), + ("std::uint8_t", "B", np.uint8), + ("std::int16_t", "h", np.int16), + ("std::uint16_t", "H", np.uint16), + ("std::int32_t", "i", np.int32), + ("std::uint32_t", "I", np.uint32), + ("std::int64_t", "q", np.int64), + ("std::uint64_t", "Q", np.uint64), + ("float", "f", np.float32), + ("double", "d", np.float64), + ("long double", "g", np_float128), + ("std::complex", "Zf", np.complex64), + ("std::complex", "Zd", np.complex128), + ("std::complex", "Zg", np_complex256), +] +CPP_NAME_FORMAT_TABLE = [ + (cpp_name, format) + for cpp_name, format, np_dtype in CPP_NAME_FORMAT_NP_DTYPE_TABLE + if np_dtype is not None +] +CPP_NAME_NP_DTYPE_TABLE = [ + (cpp_name, np_dtype) for cpp_name, _, np_dtype in CPP_NAME_FORMAT_NP_DTYPE_TABLE +] + + +@pytest.mark.parametrize(("cpp_name", "np_dtype"), CPP_NAME_NP_DTYPE_TABLE) +def test_format_descriptor_format_buffer_info_equiv(cpp_name, np_dtype): + if np_dtype is None: + pytest.skip( + f"cpp_name=`{cpp_name}`: `long double` and `double` have same size." + ) + if isinstance(np_dtype, str): + pytest.skip(f"np.{np_dtype} does not exist.") + np_array = np.array([], dtype=np_dtype) + for other_cpp_name, expected_format in CPP_NAME_FORMAT_TABLE: + format, np_array_is_matching = m.format_descriptor_format_buffer_info_equiv( + other_cpp_name, np_array + ) + assert format == expected_format + if other_cpp_name == cpp_name: + assert np_array_is_matching + else: + assert not np_array_is_matching + + +def test_from_python(): + with pytest.raises(RuntimeError) as excinfo: + m.Matrix(np.array([1, 2, 3])) # trying to assign a 1D array + assert str(excinfo.value) == "Incompatible buffer format!" + + m3 = np.array([[1, 2, 3], [4, 5, 6]]).astype(np.float32) + m4 = m.Matrix(m3) + + for i in range(m4.rows()): + for j in range(m4.cols()): + assert m3[i, j] == m4[i, j] + + if env.GRAALPY: + pytest.skip("ConstructorStats is incompatible with GraalPy.") + cstats = ConstructorStats.get(m.Matrix) + assert cstats.alive() == 1 + del m3, m4 + assert cstats.alive() == 0 + assert cstats.values() == ["2x3 matrix"] + assert cstats.copy_constructions == 0 + # assert cstats.move_constructions >= 0 # Don't invoke any + assert cstats.copy_assignments == 0 + assert cstats.move_assignments == 0 + + +# https://foss.heptapod.net/pypy/pypy/-/issues/2444 +# TODO: fix on recent PyPy +@pytest.mark.xfail( + env.PYPY, reason="PyPy 7.3.7 doesn't clear this anymore", strict=False +) +def test_to_python(): + mat = m.Matrix(5, 4) + assert memoryview(mat).shape == (5, 4) + + assert mat[2, 3] == 0 + mat[2, 3] = 4.0 + mat[3, 2] = 7.0 + assert mat[2, 3] == 4 + assert mat[3, 2] == 7 + assert struct.unpack_from("f", mat, (3 * 4 + 2) * 4) == (7,) + assert struct.unpack_from("f", mat, (2 * 4 + 3) * 4) == (4,) + + mat2 = np.array(mat, copy=False) + assert mat2.shape == (5, 4) + assert abs(mat2).sum() == 11 + assert mat2[2, 3] == 4 + assert mat2[3, 2] == 7 + mat2[2, 3] = 5 + assert mat2[2, 3] == 5 + + if env.GRAALPY: + pytest.skip("ConstructorStats is incompatible with GraalPy.") + cstats = ConstructorStats.get(m.Matrix) + assert cstats.alive() == 1 + del mat + pytest.gc_collect() + assert cstats.alive() == 1 + del mat2 # holds a mat reference + pytest.gc_collect() + assert cstats.alive() == 0 + assert cstats.values() == ["5x4 matrix"] + assert cstats.copy_constructions == 0 + # assert cstats.move_constructions >= 0 # Don't invoke any + assert cstats.copy_assignments == 0 + assert cstats.move_assignments == 0 + + +def test_inherited_protocol(): + """SquareMatrix is derived from Matrix and inherits the buffer protocol""" + + matrix = m.SquareMatrix(5) + assert memoryview(matrix).shape == (5, 5) + assert np.asarray(matrix).shape == (5, 5) + + +def test_pointer_to_member_fn(): + for cls in [m.Buffer, m.ConstBuffer, m.DerivedBuffer]: + buf = cls() + buf.value = 0x12345678 + value = struct.unpack("i", bytearray(buf))[0] + assert value == 0x12345678 + + +def test_readonly_buffer(): + buf = m.BufferReadOnly(0x64) + view = memoryview(buf) + assert view[0] == 0x64 + assert view.readonly + with pytest.raises(TypeError): + view[0] = 0 + + +def test_selective_readonly_buffer(): + buf = m.BufferReadOnlySelect() + + memoryview(buf)[0] = 0x64 + assert buf.value == 0x64 + + io.BytesIO(b"A").readinto(buf) + assert buf.value == ord(b"A") + + buf.readonly = True + with pytest.raises(TypeError): + memoryview(buf)[0] = 0 + with pytest.raises(TypeError): + io.BytesIO(b"1").readinto(buf) + + +def test_ctypes_array_1d(): + char1d = (ctypes.c_char * 10)() + int1d = (ctypes.c_int * 15)() + long1d = (ctypes.c_long * 7)() + + for carray in (char1d, int1d, long1d): + info = m.get_buffer_info(carray) + assert info.itemsize == ctypes.sizeof(carray._type_) + assert info.size == len(carray) + assert info.ndim == 1 + assert info.shape == [info.size] + assert info.strides == [info.itemsize] + assert not info.readonly + + +def test_ctypes_array_2d(): + char2d = ((ctypes.c_char * 10) * 4)() + int2d = ((ctypes.c_int * 15) * 3)() + long2d = ((ctypes.c_long * 7) * 2)() + + for carray in (char2d, int2d, long2d): + info = m.get_buffer_info(carray) + assert info.itemsize == ctypes.sizeof(carray[0]._type_) + assert info.size == len(carray) * len(carray[0]) + assert info.ndim == 2 + assert info.shape == [len(carray), len(carray[0])] + assert info.strides == [info.itemsize * len(carray[0]), info.itemsize] + assert not info.readonly + + +def test_ctypes_from_buffer(): + test_pystr = b"0123456789" + for pyarray in (test_pystr, bytearray(test_pystr)): + pyinfo = m.get_buffer_info(pyarray) + + if pyinfo.readonly: + cbytes = (ctypes.c_char * len(pyarray)).from_buffer_copy(pyarray) + cinfo = m.get_buffer_info(cbytes) + else: + cbytes = (ctypes.c_char * len(pyarray)).from_buffer(pyarray) + cinfo = m.get_buffer_info(cbytes) + + assert cinfo.size == pyinfo.size + assert cinfo.ndim == pyinfo.ndim + assert cinfo.shape == pyinfo.shape + assert cinfo.strides == pyinfo.strides + assert not cinfo.readonly + + +def test_buffer_docstring(doc, backport_typehints): + assert ( + backport_typehints(doc(m.get_buffer_info)) + == "get_buffer_info(arg0: collections.abc.Buffer) -> m.buffers.buffer_info" + ) + + +def test_buffer_exception(): + with pytest.raises(BufferError, match="Error getting buffer") as excinfo: + memoryview(m.BrokenMatrix(1, 1)) + assert isinstance(excinfo.value.__cause__, RuntimeError) + assert "for context" in str(excinfo.value.__cause__) + + +@pytest.mark.parametrize("type", ["pybind11", "numpy"]) +def test_c_contiguous_to_pybuffer(type): + if type == "pybind11": + mat = m.Matrix(5, 4) + elif type == "numpy": + mat = np.empty((5, 4), dtype=np.float32) + else: + raise ValueError(f"Unknown parametrization {type}") + + info = m.get_py_buffer(mat, m.PyBUF_SIMPLE) + assert info.format is None + assert info.itemsize == ctypes.sizeof(ctypes.c_float) + assert info.len == 5 * 4 * info.itemsize + assert info.ndim == 0 # See discussion on PR #5407. + assert info.shape is None + assert info.strides is None + assert info.suboffsets is None + assert not info.readonly + info = m.get_py_buffer(mat, m.PyBUF_SIMPLE | m.PyBUF_FORMAT) + assert info.format == "f" + assert info.itemsize == ctypes.sizeof(ctypes.c_float) + assert info.len == 5 * 4 * info.itemsize + assert info.ndim == 0 # See discussion on PR #5407. + assert info.shape is None + assert info.strides is None + assert info.suboffsets is None + assert not info.readonly + info = m.get_py_buffer(mat, m.PyBUF_ND) + assert info.itemsize == ctypes.sizeof(ctypes.c_float) + assert info.len == 5 * 4 * info.itemsize + assert info.ndim == 2 + assert info.shape == [5, 4] + assert info.strides is None + assert info.suboffsets is None + assert not info.readonly + info = m.get_py_buffer(mat, m.PyBUF_STRIDES) + assert info.itemsize == ctypes.sizeof(ctypes.c_float) + assert info.len == 5 * 4 * info.itemsize + assert info.ndim == 2 + assert info.shape == [5, 4] + assert info.strides == [4 * info.itemsize, info.itemsize] + assert info.suboffsets is None + assert not info.readonly + info = m.get_py_buffer(mat, m.PyBUF_INDIRECT) + assert info.itemsize == ctypes.sizeof(ctypes.c_float) + assert info.len == 5 * 4 * info.itemsize + assert info.ndim == 2 + assert info.shape == [5, 4] + assert info.strides == [4 * info.itemsize, info.itemsize] + assert info.suboffsets is None # Should be filled in here, but we don't use it. + assert not info.readonly + + +@pytest.mark.parametrize("type", ["pybind11", "numpy"]) +def test_fortran_contiguous_to_pybuffer(type): + if type == "pybind11": + mat = m.FortranMatrix(5, 4) + elif type == "numpy": + mat = np.empty((5, 4), dtype=np.float32, order="F") + else: + raise ValueError(f"Unknown parametrization {type}") + + # A Fortran-shaped buffer can only be accessed at PyBUF_STRIDES level or higher. + info = m.get_py_buffer(mat, m.PyBUF_STRIDES) + assert info.itemsize == ctypes.sizeof(ctypes.c_float) + assert info.len == 5 * 4 * info.itemsize + assert info.ndim == 2 + assert info.shape == [5, 4] + assert info.strides == [info.itemsize, 5 * info.itemsize] + assert info.suboffsets is None + assert not info.readonly + info = m.get_py_buffer(mat, m.PyBUF_INDIRECT) + assert info.itemsize == ctypes.sizeof(ctypes.c_float) + assert info.len == 5 * 4 * info.itemsize + assert info.ndim == 2 + assert info.shape == [5, 4] + assert info.strides == [info.itemsize, 5 * info.itemsize] + assert info.suboffsets is None # Should be filled in here, but we don't use it. + assert not info.readonly + + +@pytest.mark.parametrize("type", ["pybind11", "numpy"]) +def test_discontiguous_to_pybuffer(type): + if type == "pybind11": + mat = m.DiscontiguousMatrix(5, 4, 2, 3) + elif type == "numpy": + mat = np.empty((5 * 2, 4 * 3), dtype=np.float32)[::2, ::3] + else: + raise ValueError(f"Unknown parametrization {type}") + + info = m.get_py_buffer(mat, m.PyBUF_STRIDES) + assert info.itemsize == ctypes.sizeof(ctypes.c_float) + assert info.len == 5 * 4 * info.itemsize + assert info.ndim == 2 + assert info.shape == [5, 4] + assert info.strides == [2 * 4 * 3 * info.itemsize, 3 * info.itemsize] + assert info.suboffsets is None + assert not info.readonly + + +@pytest.mark.parametrize("type", ["pybind11", "numpy"]) +def test_to_pybuffer_contiguity(type): + def check_strides(mat): + # The full block is memset to 0, so fill it with non-zero in real spots. + expected = np.arange(1, 5 * 4 + 1).reshape((5, 4)) + for i in range(5): + for j in range(4): + mat[i, j] = expected[i, j] + # If all strides are correct, the exposed buffer should match the input. + np.testing.assert_array_equal(np.array(mat), expected) + + if type == "pybind11": + cmat = m.Matrix(5, 4) # C contiguous. + fmat = m.FortranMatrix(5, 4) # Fortran contiguous. + dmat = m.DiscontiguousMatrix(5, 4, 2, 3) # Not contiguous. + expected_exception = BufferError + elif type == "numpy": + cmat = np.empty((5, 4), dtype=np.float32) # C contiguous. + fmat = np.empty((5, 4), dtype=np.float32, order="F") # Fortran contiguous. + dmat = np.empty((5 * 2, 4 * 3), dtype=np.float32)[::2, ::3] # Not contiguous. + # NumPy incorrectly raises ValueError; when the minimum NumPy requirement is + # above the version that fixes https://github.com/numpy/numpy/issues/3634 then + # BufferError can be used everywhere. + expected_exception = (BufferError, ValueError) + else: + raise ValueError(f"Unknown parametrization {type}") + + check_strides(cmat) + # Should work in C-contiguous mode, but not Fortran order. + m.get_py_buffer(cmat, m.PyBUF_C_CONTIGUOUS) + m.get_py_buffer(cmat, m.PyBUF_ANY_CONTIGUOUS) + with pytest.raises(expected_exception): + m.get_py_buffer(cmat, m.PyBUF_F_CONTIGUOUS) + + check_strides(fmat) + # These flags imply C-contiguity, so won't work. + with pytest.raises(expected_exception): + m.get_py_buffer(fmat, m.PyBUF_SIMPLE) + with pytest.raises(expected_exception): + m.get_py_buffer(fmat, m.PyBUF_ND) + # Should work in Fortran-contiguous mode, but not C order. + with pytest.raises(expected_exception): + m.get_py_buffer(fmat, m.PyBUF_C_CONTIGUOUS) + m.get_py_buffer(fmat, m.PyBUF_ANY_CONTIGUOUS) + m.get_py_buffer(fmat, m.PyBUF_F_CONTIGUOUS) + + check_strides(dmat) + # Should never work. + with pytest.raises(expected_exception): + m.get_py_buffer(dmat, m.PyBUF_SIMPLE) + with pytest.raises(expected_exception): + m.get_py_buffer(dmat, m.PyBUF_ND) + with pytest.raises(expected_exception): + m.get_py_buffer(dmat, m.PyBUF_C_CONTIGUOUS) + with pytest.raises(expected_exception): + m.get_py_buffer(dmat, m.PyBUF_ANY_CONTIGUOUS) + with pytest.raises(expected_exception): + m.get_py_buffer(dmat, m.PyBUF_F_CONTIGUOUS) + + +def test_noexcept_def_buffer(): + """Test issue #2234: def_buffer with noexcept member function pointers. + + Covers both new def_buffer specialisations: + - def_buffer(Return (Class::*)(Args...) noexcept) + - def_buffer(Return (Class::*)(Args...) const noexcept) + """ + # non-const noexcept member function form + buf = m.OneDBuffer(5) + arr = np.frombuffer(buf, dtype=np.float32) + assert arr.shape == (5,) + arr[2] = 3.14 + arr2 = np.frombuffer(buf, dtype=np.float32) + assert arr2[2] == pytest.approx(3.14) + + # const noexcept member function form + cbuf = m.OneDBufferConst(4) + carr = np.frombuffer(cbuf, dtype=np.float32) + assert carr.shape == (4,) + assert carr.flags["WRITEABLE"] is False + + +def test_ref_qualified_def_buffer(): + """Test issue #2234 follow-up: def_buffer with ref-qualified member function pointers. + + Covers: + - def_buffer(Return (Class::*)(Args...) &) + - def_buffer(Return (Class::*)(Args...) const &) + - def_buffer(Return (Class::*)(Args...) & noexcept) + - def_buffer(Return (Class::*)(Args...) const & noexcept) + """ + # non-const lvalue ref-qualified member function form + buf = m.OneDBufferLRef(5) + arr = np.frombuffer(buf, dtype=np.float32) + assert arr.shape == (5,) + arr[1] = 2.5 + arr2 = np.frombuffer(buf, dtype=np.float32) + assert arr2[1] == pytest.approx(2.5) + + # const lvalue ref-qualified member function form + cbuf = m.OneDBufferConstLRef(4) + carr = np.frombuffer(cbuf, dtype=np.float32) + assert carr.shape == (4,) + assert carr.flags["WRITEABLE"] is False + + +@pytest.mark.skipif( + not defined___cpp_noexcept_function_type, + reason="Requires __cpp_noexcept_function_type", +) +def test_ref_qualified_noexcept_def_buffer(): + """Test issue #2234 follow-up: def_buffer with noexcept ref-qualified member pointers. + + Covers: + - def_buffer(Return (Class::*)(Args...) & noexcept) + - def_buffer(Return (Class::*)(Args...) const & noexcept) + """ + nbuf = m.OneDBufferLRefNoexcept(3) + narr = np.frombuffer(nbuf, dtype=np.float32) + assert narr.shape == (3,) + narr[2] = 7.0 + narr2 = np.frombuffer(nbuf, dtype=np.float32) + assert narr2[2] == pytest.approx(7.0) + + ncbuf = m.OneDBufferConstLRefNoexcept(2) + ncarr = np.frombuffer(ncbuf, dtype=np.float32) + assert ncarr.shape == (2,) + assert ncarr.flags["WRITEABLE"] is False diff --git a/external_libraries/pybind11/tests/test_builtin_casters.cpp b/external_libraries/pybind11/tests/test_builtin_casters.cpp new file mode 100644 index 00000000..6cde727a --- /dev/null +++ b/external_libraries/pybind11/tests/test_builtin_casters.cpp @@ -0,0 +1,393 @@ +/* + tests/test_builtin_casters.cpp -- Casters available without any additional headers + + Copyright (c) 2017 Wenzel Jakob + + All rights reserved. Use of this source code is governed by a + BSD-style license that can be found in the LICENSE file. +*/ + +#include + +#include "pybind11_tests.h" + +struct ConstRefCasted { + int tag; +}; + +PYBIND11_NAMESPACE_BEGIN(pybind11) +PYBIND11_NAMESPACE_BEGIN(detail) +template <> +class type_caster { +public: + static constexpr auto name = const_name(); + + // Input is unimportant, a new value will always be constructed based on the + // cast operator. + bool load(handle, bool) { return true; } + + explicit operator ConstRefCasted &&() { + value = {1}; + // NOLINTNEXTLINE(performance-move-const-arg) + return std::move(value); + } + explicit operator ConstRefCasted &() { + value = {2}; + return value; + } + explicit operator ConstRefCasted *() { + value = {3}; + return &value; + } + + explicit operator const ConstRefCasted &() { + value = {4}; + return value; + } + explicit operator const ConstRefCasted *() { + value = {5}; + return &value; + } + + // custom cast_op to explicitly propagate types to the conversion operators. + template + using cast_op_type = + /// const + conditional_t< + std::is_same, const ConstRefCasted *>::value, + const ConstRefCasted *, + conditional_t< + std::is_same::value, + const ConstRefCasted &, + /// non-const + conditional_t, ConstRefCasted *>::value, + ConstRefCasted *, + conditional_t::value, + ConstRefCasted &, + /* else */ ConstRefCasted &&>>>>; + +private: + ConstRefCasted value = {0}; +}; +PYBIND11_NAMESPACE_END(detail) +PYBIND11_NAMESPACE_END(pybind11) + +TEST_SUBMODULE(builtin_casters, m) { + PYBIND11_WARNING_PUSH + PYBIND11_WARNING_DISABLE_MSVC(4127) + + // test_simple_string + m.def("string_roundtrip", [](const char *s) { return s; }); + + // test_unicode_conversion + // Some test characters in utf16 and utf32 encodings. The last one (the 𝐀) contains a null + // byte + char32_t a32 = 0x61 /*a*/, z32 = 0x7a /*z*/, ib32 = 0x203d /*‽*/, cake32 = 0x1f382 /*🎂*/, + mathbfA32 = 0x1d400 /*𝐀*/; + char16_t b16 = 0x62 /*b*/, z16 = 0x7a, ib16 = 0x203d, cake16_1 = 0xd83c, cake16_2 = 0xdf82, + mathbfA16_1 = 0xd835, mathbfA16_2 = 0xdc00; + std::wstring wstr; + wstr.push_back(0x61); // a + wstr.push_back(0x2e18); // ⸘ + if (sizeof(wchar_t) == 2) { + wstr.push_back(mathbfA16_1); + wstr.push_back(mathbfA16_2); + } // 𝐀, utf16 + else { + wstr.push_back((wchar_t) mathbfA32); + } // 𝐀, utf32 + wstr.push_back(0x7a); // z + + m.def("good_utf8_string", []() { + return std::string((const char *) u8"Say utf8\u203d \U0001f382 \U0001d400"); + }); // Say utf8‽ 🎂 𝐀 + m.def("good_utf16_string", [=]() { + return std::u16string({b16, ib16, cake16_1, cake16_2, mathbfA16_1, mathbfA16_2, z16}); + }); // b‽🎂𝐀z + m.def("good_utf32_string", + [=]() { return std::u32string({a32, mathbfA32, cake32, ib32, z32}); }); // a𝐀🎂‽z + m.def("good_wchar_string", [=]() { return wstr; }); // a‽𝐀z + m.def("bad_utf8_string", []() { + return std::string("abc\xd0" + "def"); + }); + m.def("bad_utf16_string", [=]() { return std::u16string({b16, char16_t(0xd800), z16}); }); + // Under Python 2.7, invalid unicode UTF-32 characters didn't appear to trigger + // UnicodeDecodeError + m.def("bad_utf32_string", [=]() { return std::u32string({a32, char32_t(0xd800), z32}); }); + if (sizeof(wchar_t) == 2) { + m.def("bad_wchar_string", + [=]() { return std::wstring({wchar_t(0x61), wchar_t(0xd800)}); }); + } + m.def("u8_Z", []() -> char { return 'Z'; }); + m.def("u8_eacute", []() -> char { return '\xe9'; }); + m.def("u16_ibang", [=]() -> char16_t { return ib16; }); + m.def("u32_mathbfA", [=]() -> char32_t { return mathbfA32; }); + m.def("wchar_heart", []() -> wchar_t { return 0x2665; }); + + // test_single_char_arguments + m.attr("wchar_size") = py::cast(sizeof(wchar_t)); + m.def("ord_char", [](char c) -> int { return static_cast(c); }); + m.def("ord_char_lv", [](char &c) -> int { return static_cast(c); }); + m.def("ord_char16", [](char16_t c) -> uint16_t { return c; }); + m.def("ord_char16_lv", [](char16_t &c) -> uint16_t { return c; }); + m.def("ord_char32", [](char32_t c) -> uint32_t { return c; }); + m.def("ord_wchar", [](wchar_t c) -> int { return c; }); + + // test_bytes_to_string + m.def("strlen", [](char *s) { return strlen(s); }); + m.def("string_length", [](const std::string &s) { return s.length(); }); + +#ifdef PYBIND11_HAS_U8STRING + m.attr("has_u8string") = true; + m.def("good_utf8_u8string", []() { + return std::u8string(u8"Say utf8\u203d \U0001f382 \U0001d400"); + }); // Say utf8‽ 🎂 𝐀 + m.def("bad_utf8_u8string", []() { + return std::u8string((const char8_t *) "abc\xd0" + "def"); + }); + + m.def("u8_char8_Z", []() -> char8_t { return u8'Z'; }); + + // test_single_char_arguments + m.def("ord_char8", [](char8_t c) -> int { return static_cast(c); }); + m.def("ord_char8_lv", [](char8_t &c) -> int { return static_cast(c); }); +#endif + + // test_string_view +#ifdef PYBIND11_HAS_STRING_VIEW + m.attr("has_string_view") = true; + m.def("string_view_print", [](std::string_view s) { py::print(s, s.size()); }); + m.def("string_view16_print", [](std::u16string_view s) { py::print(s, s.size()); }); + m.def("string_view32_print", [](std::u32string_view s) { py::print(s, s.size()); }); + m.def("string_view_chars", [](std::string_view s) { + py::list l; + for (auto c : s) { + l.append((std::uint8_t) c); + } + return l; + }); + m.def("string_view16_chars", [](std::u16string_view s) { + py::list l; + for (auto c : s) { + l.append((int) c); + } + return l; + }); + m.def("string_view32_chars", [](std::u32string_view s) { + py::list l; + for (auto c : s) { + l.append((int) c); + } + return l; + }); + m.def("string_view_return", + []() { return std::string_view((const char *) u8"utf8 secret \U0001f382"); }); + m.def("string_view16_return", + []() { return std::u16string_view(u"utf16 secret \U0001f382"); }); + m.def("string_view32_return", + []() { return std::u32string_view(U"utf32 secret \U0001f382"); }); + + // The inner lambdas here are to also test implicit conversion + using namespace std::literals; + m.def("string_view_bytes", + []() { return [](py::bytes b) { return b; }("abc \x80\x80 def"sv); }); + m.def("string_view_str", + []() { return [](py::str s) { return s; }("abc \342\200\275 def"sv); }); + m.def("string_view_from_bytes", + [](const py::bytes &b) { return [](std::string_view s) { return s; }(b); }); + m.def("string_view_memoryview", []() { + static constexpr auto val = "Have some \360\237\216\202"sv; + return py::memoryview::from_memory(val); + }); + +# ifdef PYBIND11_HAS_U8STRING + m.def("string_view8_print", [](std::u8string_view s) { py::print(s, s.size()); }); + m.def("string_view8_chars", [](std::u8string_view s) { + py::list l; + for (auto c : s) + l.append((std::uint8_t) c); + return l; + }); + m.def("string_view8_return", []() { return std::u8string_view(u8"utf8 secret \U0001f382"); }); + m.def("string_view8_str", []() { return py::str{std::u8string_view{u8"abc ‽ def"}}; }); +# endif + + struct TypeWithBothOperatorStringAndStringView { + // NOLINTNEXTLINE(google-explicit-constructor) + operator std::string() const { return "success"; } + // NOLINTNEXTLINE(google-explicit-constructor) + operator std::string_view() const { return "failure"; } + }; + m.def("bytes_from_type_with_both_operator_string_and_string_view", + []() { return py::bytes(TypeWithBothOperatorStringAndStringView()); }); + m.def("str_from_type_with_both_operator_string_and_string_view", + []() { return py::str(TypeWithBothOperatorStringAndStringView()); }); +#endif + + // test_integer_casting + m.def("i32_str", [](std::int32_t v) { return std::to_string(v); }); + m.def("u32_str", [](std::uint32_t v) { return std::to_string(v); }); + m.def("i64_str", [](std::int64_t v) { return std::to_string(v); }); + m.def("u64_str", [](std::uint64_t v) { return std::to_string(v); }); + + // test_int_convert + m.def("int_passthrough", [](int arg) { return arg; }); + m.def("int_passthrough_noconvert", [](int arg) { return arg; }, py::arg{}.noconvert()); + + // test_float_convert + m.def("float_passthrough", [](float arg) { return arg; }); + m.def("float_passthrough_noconvert", [](float arg) { return arg; }, py::arg{}.noconvert()); + + // test_tuple + m.def( + "pair_passthrough", + [](const std::pair &input) { + return std::make_pair(input.second, input.first); + }, + "Return a pair in reversed order"); + m.def( + "tuple_passthrough", + [](std::tuple input) { + return std::make_tuple(std::get<2>(input), std::get<1>(input), std::get<0>(input)); + }, + "Return a triple in reversed order"); + m.def("empty_tuple", []() { return std::tuple<>(); }); + static std::pair lvpair; + static std::tuple lvtuple; + static std::pair>> + lvnested; + m.def("rvalue_pair", []() { return std::make_pair(RValueCaster{}, RValueCaster{}); }); + m.def("lvalue_pair", []() -> const decltype(lvpair) & { return lvpair; }); + m.def("rvalue_tuple", + []() { return std::make_tuple(RValueCaster{}, RValueCaster{}, RValueCaster{}); }); + m.def("lvalue_tuple", []() -> const decltype(lvtuple) & { return lvtuple; }); + m.def("rvalue_nested", []() { + return std::make_pair( + RValueCaster{}, + std::make_tuple(RValueCaster{}, std::make_pair(RValueCaster{}, RValueCaster{}))); + }); + m.def("lvalue_nested", []() -> const decltype(lvnested) & { return lvnested; }); + + m.def( + "int_string_pair", + []() { + // Using no-destructor idiom to side-step warnings from overzealous compilers. + static auto *int_string_pair = new std::pair{2, "items"}; + return int_string_pair; + }, + py::return_value_policy::reference); + + // test_builtins_cast_return_none + m.def("return_none_string", []() -> std::string * { return nullptr; }); + m.def("return_none_char", []() -> const char * { return nullptr; }); + m.def("return_none_bool", []() -> bool * { return nullptr; }); + m.def("return_none_int", []() -> int * { return nullptr; }); + m.def("return_none_float", []() -> float * { return nullptr; }); + m.def("return_none_pair", []() -> std::pair * { return nullptr; }); + + // test_none_deferred + m.def("defer_none_cstring", [](char *) { return false; }); + m.def("defer_none_cstring", [](const py::none &) { return true; }); + m.def("defer_none_custom", [](UserType *) { return false; }); + m.def("defer_none_custom", [](const py::none &) { return true; }); + m.def("nodefer_none_void", [](void *) { return true; }); + m.def("nodefer_none_void", [](const py::none &) { return false; }); + + // test_void_caster + m.def("load_nullptr_t", [](std::nullptr_t) {}); // not useful, but it should still compile + m.def("cast_nullptr_t", []() { return std::nullptr_t{}; }); + + // [workaround(intel)] ICC 20/21 breaks with py::arg().stuff, using py::arg{}.stuff works. + + // test_bool_caster + m.def("bool_passthrough", [](bool arg) { return arg; }); + m.def("bool_passthrough_noconvert", [](bool arg) { return arg; }, py::arg{}.noconvert()); + + // TODO: This should be disabled and fixed in future Intel compilers +#if !defined(__INTEL_COMPILER) + // Test "bool_passthrough_noconvert" again, but using () instead of {} to construct py::arg + // When compiled with the Intel compiler, this results in segmentation faults when importing + // the module. Tested with icc (ICC) 2021.1 Beta 20200827, this should be tested again when + // a newer version of icc is available. + m.def("bool_passthrough_noconvert2", [](bool arg) { return arg; }, py::arg().noconvert()); +#endif + + // test_reference_wrapper + m.def("refwrap_builtin", [](std::reference_wrapper p) { return 10 * p.get(); }); + m.def("refwrap_usertype", [](std::reference_wrapper p) { return p.get().value(); }); + m.def("refwrap_usertype_const", + [](std::reference_wrapper p) { return p.get().value(); }); + + m.def("refwrap_lvalue", []() -> std::reference_wrapper { + static UserType x(1); + return std::ref(x); + }); + m.def("refwrap_lvalue_const", []() -> std::reference_wrapper { + static UserType x(1); + return std::cref(x); + }); + + // Not currently supported (std::pair caster has return-by-value cast operator); + // triggers static_assert failure. + // m.def("refwrap_pair", [](std::reference_wrapper>) { }); + + m.def( + "refwrap_list", + [](bool copy) { + static IncType x1(1), x2(2); + py::list l; + for (const auto &f : {std::ref(x1), std::ref(x2)}) { + l.append(py::cast( + f, copy ? py::return_value_policy::copy : py::return_value_policy::reference)); + } + return l; + }, + "copy"_a); + + m.def("refwrap_iiw", [](const IncType &w) { return w.value(); }); + m.def("refwrap_call_iiw", [](IncType &w, const py::function &f) { + py::list l; + l.append(f(std::ref(w))); + l.append(f(std::cref(w))); + IncType x(w.value()); + l.append(f(std::ref(x))); + IncType y(w.value()); + auto r3 = std::ref(y); + l.append(f(r3)); + return l; + }); + + // test_complex + m.def("complex_cast", [](float x) { return "{}"_s.format(x); }); + m.def("complex_cast", + [](std::complex x) { return "({}, {})"_s.format(x.real(), x.imag()); }); + m.def("complex_convert", [](std::complex x) { return x; }); + m.def("complex_noconvert", [](std::complex x) { return x; }, py::arg{}.noconvert()); + + // test int vs. long (Python 2) + m.def("int_cast", []() { return 42; }); + m.def("long_cast", []() { return (long) 42; }); + m.def("longlong_cast", []() { return ULLONG_MAX; }); + + /// test void* cast operator + m.def("test_void_caster", []() -> bool { + void *v = (void *) 0xabcd; + py::object o = py::cast(v); + return py::cast(o) == v; + }); + + // Tests const/non-const propagation in cast_op. + m.def("takes", [](ConstRefCasted x) { return x.tag; }); + m.def("takes_move", [](ConstRefCasted &&x) { return x.tag; }); + m.def("takes_ptr", [](ConstRefCasted *x) { return x->tag; }); + m.def("takes_ref", [](ConstRefCasted &x) { return x.tag; }); + m.def("takes_ref_wrap", [](std::reference_wrapper x) { return x.get().tag; }); + m.def("takes_const_ptr", [](const ConstRefCasted *x) { return x->tag; }); + m.def("takes_const_ref", [](const ConstRefCasted &x) { return x.tag; }); + m.def("takes_const_ref_wrap", + [](std::reference_wrapper x) { return x.get().tag; }); + + PYBIND11_WARNING_POP +} diff --git a/external_libraries/pybind11/tests/test_builtin_casters.py b/external_libraries/pybind11/tests/test_builtin_casters.py new file mode 100644 index 00000000..23c191ce --- /dev/null +++ b/external_libraries/pybind11/tests/test_builtin_casters.py @@ -0,0 +1,624 @@ +from __future__ import annotations + +import sys + +import pytest + +import env +from pybind11_tests import IncType, UserType +from pybind11_tests import builtin_casters as m + + +def test_simple_string(): + assert m.string_roundtrip("const char *") == "const char *" + + +def test_unicode_conversion(): + """Tests unicode conversion and error reporting.""" + assert m.good_utf8_string() == "Say utf8‽ 🎂 𝐀" + assert m.good_utf16_string() == "b‽🎂𝐀z" + assert m.good_utf32_string() == "a𝐀🎂‽z" + assert m.good_wchar_string() == "a⸘𝐀z" + if hasattr(m, "has_u8string"): + assert m.good_utf8_u8string() == "Say utf8‽ 🎂 𝐀" + + with pytest.raises(UnicodeDecodeError): + m.bad_utf8_string() + + with pytest.raises(UnicodeDecodeError): + m.bad_utf16_string() + + # These are provided only if they actually fail (they don't when 32-bit) + if hasattr(m, "bad_utf32_string"): + with pytest.raises(UnicodeDecodeError): + m.bad_utf32_string() + if hasattr(m, "bad_wchar_string"): + with pytest.raises(UnicodeDecodeError): + m.bad_wchar_string() + if hasattr(m, "has_u8string"): + with pytest.raises(UnicodeDecodeError): + m.bad_utf8_u8string() + + assert m.u8_Z() == "Z" + assert m.u8_eacute() == "é" + assert m.u16_ibang() == "‽" + assert m.u32_mathbfA() == "𝐀" + assert m.wchar_heart() == "♥" + if hasattr(m, "has_u8string"): + assert m.u8_char8_Z() == "Z" + + +def test_single_char_arguments(): + """Tests failures for passing invalid inputs to char-accepting functions""" + + def toobig_message(r): + return f"Character code point not in range({r:#x})" + + toolong_message = "Expected a character, but multi-character string found" + + assert m.ord_char("a") == 0x61 # simple ASCII + assert m.ord_char_lv("b") == 0x62 + assert ( + m.ord_char("é") == 0xE9 + ) # requires 2 bytes in utf-8, but can be stuffed in a char + with pytest.raises(ValueError) as excinfo: + assert m.ord_char("Ā") == 0x100 # requires 2 bytes, doesn't fit in a char + assert str(excinfo.value) == toobig_message(0x100) + with pytest.raises(ValueError) as excinfo: + assert m.ord_char("ab") + assert str(excinfo.value) == toolong_message + + assert m.ord_char16("a") == 0x61 + assert m.ord_char16("é") == 0xE9 + assert m.ord_char16_lv("ê") == 0xEA + assert m.ord_char16("Ā") == 0x100 + assert m.ord_char16("‽") == 0x203D + assert m.ord_char16("♥") == 0x2665 + assert m.ord_char16_lv("♡") == 0x2661 + with pytest.raises(ValueError) as excinfo: + assert m.ord_char16("🎂") == 0x1F382 # requires surrogate pair + assert str(excinfo.value) == toobig_message(0x10000) + with pytest.raises(ValueError) as excinfo: + assert m.ord_char16("aa") + assert str(excinfo.value) == toolong_message + + assert m.ord_char32("a") == 0x61 + assert m.ord_char32("é") == 0xE9 + assert m.ord_char32("Ā") == 0x100 + assert m.ord_char32("‽") == 0x203D + assert m.ord_char32("♥") == 0x2665 + assert m.ord_char32("🎂") == 0x1F382 + with pytest.raises(ValueError) as excinfo: + assert m.ord_char32("aa") + assert str(excinfo.value) == toolong_message + + assert m.ord_wchar("a") == 0x61 + assert m.ord_wchar("é") == 0xE9 + assert m.ord_wchar("Ā") == 0x100 + assert m.ord_wchar("‽") == 0x203D + assert m.ord_wchar("♥") == 0x2665 + if m.wchar_size == 2: + with pytest.raises(ValueError) as excinfo: + assert m.ord_wchar("🎂") == 0x1F382 # requires surrogate pair + assert str(excinfo.value) == toobig_message(0x10000) + else: + assert m.ord_wchar("🎂") == 0x1F382 + with pytest.raises(ValueError) as excinfo: + assert m.ord_wchar("aa") + assert str(excinfo.value) == toolong_message + + if hasattr(m, "has_u8string"): + assert m.ord_char8("a") == 0x61 # simple ASCII + assert m.ord_char8_lv("b") == 0x62 + assert ( + m.ord_char8("é") == 0xE9 + ) # requires 2 bytes in utf-8, but can be stuffed in a char + with pytest.raises(ValueError) as excinfo: + assert m.ord_char8("Ā") == 0x100 # requires 2 bytes, doesn't fit in a char + assert str(excinfo.value) == toobig_message(0x100) + with pytest.raises(ValueError) as excinfo: + assert m.ord_char8("ab") + assert str(excinfo.value) == toolong_message + + +def test_bytes_to_string(): + """Tests the ability to pass bytes to C++ string-accepting functions. Note that this is + one-way: the only way to return bytes to Python is via the pybind11::bytes class.""" + # Issue #816 + + assert m.strlen(b"hi") == 2 + assert m.string_length(b"world") == 5 + assert m.string_length(b"a\x00b") == 3 + assert m.strlen(b"a\x00b") == 1 # C-string limitation + + # passing in a utf8 encoded string should work + assert m.string_length("💩".encode()) == 4 + + +def test_bytearray_to_string(): + """Tests the ability to pass bytearray to C++ string-accepting functions""" + assert m.string_length(bytearray(b"Hi")) == 2 + assert m.strlen(bytearray(b"bytearray")) == 9 + assert m.string_length(bytearray()) == 0 + assert m.string_length(bytearray("🦜", "utf-8", "strict")) == 4 + assert m.string_length(bytearray(b"\x80")) == 1 + + +@pytest.mark.skipif(not hasattr(m, "has_string_view"), reason="no ") +def test_string_view(capture): + """Tests support for C++17 string_view arguments and return values""" + assert m.string_view_chars("Hi") == [72, 105] + assert m.string_view_chars("Hi 🎂") == [72, 105, 32, 0xF0, 0x9F, 0x8E, 0x82] + assert m.string_view16_chars("Hi 🎂") == [72, 105, 32, 0xD83C, 0xDF82] + assert m.string_view32_chars("Hi 🎂") == [72, 105, 32, 127874] + if hasattr(m, "has_u8string"): + assert m.string_view8_chars("Hi") == [72, 105] + assert m.string_view8_chars("Hi 🎂") == [72, 105, 32, 0xF0, 0x9F, 0x8E, 0x82] + + assert m.string_view_return() == "utf8 secret 🎂" + assert m.string_view16_return() == "utf16 secret 🎂" + assert m.string_view32_return() == "utf32 secret 🎂" + if hasattr(m, "has_u8string"): + assert m.string_view8_return() == "utf8 secret 🎂" + + with capture: + m.string_view_print("Hi") + m.string_view_print("utf8 🎂") + m.string_view16_print("utf16 🎂") + m.string_view32_print("utf32 🎂") + assert ( + capture + == """ + Hi 2 + utf8 🎂 9 + utf16 🎂 8 + utf32 🎂 7 + """ + ) + if hasattr(m, "has_u8string"): + with capture: + m.string_view8_print("Hi") + m.string_view8_print("utf8 🎂") + assert ( + capture + == """ + Hi 2 + utf8 🎂 9 + """ + ) + + with capture: + m.string_view_print("Hi, ascii") + m.string_view_print("Hi, utf8 🎂") + m.string_view16_print("Hi, utf16 🎂") + m.string_view32_print("Hi, utf32 🎂") + assert ( + capture + == """ + Hi, ascii 9 + Hi, utf8 🎂 13 + Hi, utf16 🎂 12 + Hi, utf32 🎂 11 + """ + ) + if hasattr(m, "has_u8string"): + with capture: + m.string_view8_print("Hi, ascii") + m.string_view8_print("Hi, utf8 🎂") + assert ( + capture + == """ + Hi, ascii 9 + Hi, utf8 🎂 13 + """ + ) + + assert m.string_view_bytes() == b"abc \x80\x80 def" + assert m.string_view_str() == "abc ‽ def" + assert m.string_view_from_bytes("abc ‽ def".encode()) == "abc ‽ def" + if hasattr(m, "has_u8string"): + assert m.string_view8_str() == "abc ‽ def" + assert m.string_view_memoryview() == "Have some 🎂".encode() + + assert m.bytes_from_type_with_both_operator_string_and_string_view() == b"success" + assert m.str_from_type_with_both_operator_string_and_string_view() == "success" + + +def test_integer_casting(): + """Issue #929 - out-of-range integer values shouldn't be accepted""" + assert m.i32_str(-1) == "-1" + assert m.i64_str(-1) == "-1" + assert m.i32_str(2000000000) == "2000000000" + assert m.u32_str(2000000000) == "2000000000" + assert m.i64_str(-999999999999) == "-999999999999" + assert m.u64_str(999999999999) == "999999999999" + + with pytest.raises(TypeError) as excinfo: + m.u32_str(-1) + assert "incompatible function arguments" in str(excinfo.value) + with pytest.raises(TypeError) as excinfo: + m.u64_str(-1) + assert "incompatible function arguments" in str(excinfo.value) + with pytest.raises(TypeError) as excinfo: + m.i32_str(-3000000000) + assert "incompatible function arguments" in str(excinfo.value) + with pytest.raises(TypeError) as excinfo: + m.i32_str(3000000000) + assert "incompatible function arguments" in str(excinfo.value) + + +def test_int_convert(doc): + class Int: + def __int__(self): + return 42 + + class NotInt: + pass + + class Float: + def __float__(self): + return 41.99999 + + class Index: + def __index__(self): + return 42 + + class IntAndIndex: + def __int__(self): + return 42 + + def __index__(self): + return 0 + + class RaisingTypeErrorOnIndex: + def __index__(self): + raise TypeError + + def __int__(self): + return 42 + + class RaisingValueErrorOnIndex: + def __index__(self): + raise ValueError + + def __int__(self): + return 42 + + convert, noconvert = m.int_passthrough, m.int_passthrough_noconvert + + assert ( + doc(convert) + == "int_passthrough(arg0: typing.SupportsInt | typing.SupportsIndex) -> int" + ) + assert doc(noconvert) == "int_passthrough_noconvert(arg0: int) -> int" + + def requires_conversion(v): + pytest.raises(TypeError, noconvert, v) + + def cant_convert(v): + pytest.raises(TypeError, convert, v) + + assert convert(7) == 7 + assert noconvert(7) == 7 + cant_convert(3.14159) + # TODO: Avoid DeprecationWarning in `PyLong_AsLong` (and similar) + # TODO: PyPy 3.8 does not behave like CPython 3.8 here yet (7.3.7) + if sys.version_info < (3, 10) and env.CPYTHON: + with pytest.deprecated_call(): + assert convert(Int()) == 42 + else: + assert convert(Int()) == 42 + requires_conversion(Int()) + cant_convert(NotInt()) + cant_convert(Float()) + + # Before Python 3.8, `PyLong_AsLong` does not pick up on `obj.__index__`, + # but pybind11 "backports" this behavior. + assert convert(Index()) == 42 + assert noconvert(Index()) == 42 + assert convert(IntAndIndex()) == 0 # Fishy; `int(DoubleThought)` == 42 + assert noconvert(IntAndIndex()) == 0 + assert convert(RaisingTypeErrorOnIndex()) == 42 + requires_conversion(RaisingTypeErrorOnIndex()) + assert convert(RaisingValueErrorOnIndex()) == 42 + requires_conversion(RaisingValueErrorOnIndex()) + + +def test_float_convert(doc): + class Int: + def __int__(self): + return -5 + + class Index: + def __index__(self) -> int: + return -7 + + class Float: + def __float__(self): + return 41.45 + + convert, noconvert = m.float_passthrough, m.float_passthrough_noconvert + assert ( + doc(convert) + == "float_passthrough(arg0: typing.SupportsFloat | typing.SupportsIndex) -> float" + ) + assert doc(noconvert) == "float_passthrough_noconvert(arg0: float) -> float" + + def requires_conversion(v): + pytest.raises(TypeError, noconvert, v) + + def cant_convert(v): + pytest.raises(TypeError, convert, v) + + requires_conversion(Float()) + requires_conversion(Index()) + assert pytest.approx(convert(Float())) == 41.45 + assert pytest.approx(convert(Index())) == -7.0 + assert isinstance(convert(Float()), float) + assert pytest.approx(convert(3)) == 3.0 + requires_conversion(3) + cant_convert(Int()) + + +def test_numpy_int_convert(): + np = pytest.importorskip("numpy") + + convert, noconvert = m.int_passthrough, m.int_passthrough_noconvert + + def require_implicit(v): + pytest.raises(TypeError, noconvert, v) + + # `np.intc` is an alias that corresponds to a C++ `int` + assert convert(np.intc(42)) == 42 + assert noconvert(np.intc(42)) == 42 + + # The implicit conversion from np.float32 is undesirable but currently accepted. + # TODO: Avoid DeprecationWarning in `PyLong_AsLong` (and similar) + # TODO: PyPy 3.8 does not behave like CPython 3.8 here yet (7.3.7) + # https://github.com/pybind/pybind11/issues/3408 + if (3, 8) <= sys.version_info < (3, 10) and env.CPYTHON: + with pytest.deprecated_call(): + assert convert(np.float32(3.14159)) == 3 + else: + assert convert(np.float32(3.14159)) == 3 + require_implicit(np.float32(3.14159)) + + +def test_tuple(doc): + """std::pair <-> tuple & std::tuple <-> tuple""" + assert m.pair_passthrough((True, "test")) == ("test", True) + assert m.tuple_passthrough((True, "test", 5)) == (5, "test", True) + # Any sequence can be cast to a std::pair or std::tuple + assert m.pair_passthrough([True, "test"]) == ("test", True) + assert m.tuple_passthrough([True, "test", 5]) == (5, "test", True) + assert m.empty_tuple() == () + + assert ( + doc(m.pair_passthrough) + == """ + pair_passthrough(arg0: tuple[bool, str]) -> tuple[str, bool] + + Return a pair in reversed order + """ + ) + assert ( + doc(m.tuple_passthrough) + == """ + tuple_passthrough(arg0: tuple[bool, str, typing.SupportsInt | typing.SupportsIndex]) -> tuple[int, str, bool] + + Return a triple in reversed order + """ + ) + + assert doc(m.empty_tuple) == """empty_tuple() -> tuple[()]""" + + assert m.rvalue_pair() == ("rvalue", "rvalue") + assert m.lvalue_pair() == ("lvalue", "lvalue") + assert m.rvalue_tuple() == ("rvalue", "rvalue", "rvalue") + assert m.lvalue_tuple() == ("lvalue", "lvalue", "lvalue") + assert m.rvalue_nested() == ("rvalue", ("rvalue", ("rvalue", "rvalue"))) + assert m.lvalue_nested() == ("lvalue", ("lvalue", ("lvalue", "lvalue"))) + + assert m.int_string_pair() == (2, "items") + + +def test_builtins_cast_return_none(): + """Casters produced with PYBIND11_TYPE_CASTER() should convert nullptr to None""" + assert m.return_none_string() is None + assert m.return_none_char() is None + assert m.return_none_bool() is None + assert m.return_none_int() is None + assert m.return_none_float() is None + assert m.return_none_pair() is None + + +def test_none_deferred(): + """None passed as various argument types should defer to other overloads""" + assert not m.defer_none_cstring("abc") + assert m.defer_none_cstring(None) + assert not m.defer_none_custom(UserType()) + assert m.defer_none_custom(None) + assert m.nodefer_none_void(None) + + +def test_void_caster(): + assert m.load_nullptr_t(None) is None + assert m.cast_nullptr_t() is None + + +def test_reference_wrapper(): + """std::reference_wrapper for builtin and user types""" + assert m.refwrap_builtin(42) == 420 + assert m.refwrap_usertype(UserType(42)) == 42 + assert m.refwrap_usertype_const(UserType(42)) == 42 + + with pytest.raises(TypeError) as excinfo: + m.refwrap_builtin(None) + assert "incompatible function arguments" in str(excinfo.value) + + with pytest.raises(TypeError) as excinfo: + m.refwrap_usertype(None) + assert "incompatible function arguments" in str(excinfo.value) + + assert m.refwrap_lvalue().value == 1 + assert m.refwrap_lvalue_const().value == 1 + + a1 = m.refwrap_list(copy=True) + a2 = m.refwrap_list(copy=True) + assert [x.value for x in a1] == [2, 3] + assert [x.value for x in a2] == [2, 3] + assert a1[0] is not a2[0] + assert a1[1] is not a2[1] + + b1 = m.refwrap_list(copy=False) + b2 = m.refwrap_list(copy=False) + assert [x.value for x in b1] == [1, 2] + assert [x.value for x in b2] == [1, 2] + assert b1[0] is b2[0] + assert b1[1] is b2[1] + + assert m.refwrap_iiw(IncType(5)) == 5 + assert m.refwrap_call_iiw(IncType(10), m.refwrap_iiw) == [10, 10, 10, 10] + + +def test_complex_cast(doc): + """std::complex casts""" + + class Complex: + def __complex__(self) -> complex: + return complex(5, 4) + + class Float: + def __float__(self) -> float: + return 5.0 + + class Int: + def __int__(self) -> int: + return 3 + + class Index: + def __index__(self) -> int: + return 1 + + assert m.complex_cast(1) == "1.0" + assert m.complex_cast(1.0) == "1.0" + assert m.complex_cast(Complex()) == "(5.0, 4.0)" + assert m.complex_cast(2j) == "(0.0, 2.0)" + + convert, noconvert = m.complex_convert, m.complex_noconvert + + def requires_conversion(v): + pytest.raises(TypeError, noconvert, v) + + def cant_convert(v): + pytest.raises(TypeError, convert, v) + + assert ( + doc(convert) + == "complex_convert(arg0: typing.SupportsComplex | typing.SupportsFloat | typing.SupportsIndex) -> complex" + ) + assert doc(noconvert) == "complex_noconvert(arg0: complex) -> complex" + + assert convert(1) == 1.0 + assert convert(2.0) == 2.0 + assert convert(1 + 5j) == 1.0 + 5.0j + assert convert(Complex()) == 5.0 + 4j + assert convert(Float()) == 5.0 + assert isinstance(convert(Float()), complex) + cant_convert(Int()) + assert convert(Index()) == 1 + assert isinstance(convert(Index()), complex) + + requires_conversion(1) + requires_conversion(2.0) + assert noconvert(1 + 5j) == 1.0 + 5.0j + requires_conversion(Complex()) + requires_conversion(Float()) + requires_conversion(Index()) + + +def test_bool_caster(): + """Test bool caster implicit conversions.""" + convert, noconvert = m.bool_passthrough, m.bool_passthrough_noconvert + + def require_implicit(v): + pytest.raises(TypeError, noconvert, v) + + def cant_convert(v): + pytest.raises(TypeError, convert, v) + + # straight up bool + assert convert(True) is True + assert convert(False) is False + assert noconvert(True) is True + assert noconvert(False) is False + + # None requires implicit conversion + require_implicit(None) + assert convert(None) is False + + class A: + def __init__(self, x): + self.x = x + + def __nonzero__(self): + return self.x + + def __bool__(self): + return self.x + + class B: + pass + + # Arbitrary objects are not accepted + cant_convert(object()) + cant_convert(B()) + + # Objects with __nonzero__ / __bool__ defined can be converted + require_implicit(A(True)) + assert convert(A(True)) is True + assert convert(A(False)) is False + + +def test_numpy_bool(): + np = pytest.importorskip("numpy") + + convert, noconvert = m.bool_passthrough, m.bool_passthrough_noconvert + + def cant_convert(v): + pytest.raises(TypeError, convert, v) + + # np.bool_ is not considered implicit + assert convert(np.bool_(True)) is True + assert convert(np.bool_(False)) is False + assert noconvert(np.bool_(True)) is True + assert noconvert(np.bool_(False)) is False + cant_convert(np.zeros(2, dtype="int")) + + +def test_int_long(): + assert isinstance(m.int_cast(), int) + assert isinstance(m.long_cast(), int) + assert isinstance(m.longlong_cast(), int) + + +def test_void_caster_2(): + assert m.test_void_caster() + + +def test_const_ref_caster(): + """Verifies that const-ref is propagated through type_caster cast_op. + The returned ConstRefCasted type is a minimal type that is constructed to + reference the casting mode used. + """ + x = False + assert m.takes(x) == 1 + assert m.takes_move(x) == 1 + + assert m.takes_ptr(x) == 3 + assert m.takes_ref(x) == 2 + assert m.takes_ref_wrap(x) == 2 + + assert m.takes_const_ptr(x) == 5 + assert m.takes_const_ref(x) == 4 + assert m.takes_const_ref_wrap(x) == 4 diff --git a/external_libraries/pybind11/tests/test_call_policies.cpp b/external_libraries/pybind11/tests/test_call_policies.cpp new file mode 100644 index 00000000..9140f7e9 --- /dev/null +++ b/external_libraries/pybind11/tests/test_call_policies.cpp @@ -0,0 +1,113 @@ +/* + tests/test_call_policies.cpp -- keep_alive and call_guard + + Copyright (c) 2016 Wenzel Jakob + + All rights reserved. Use of this source code is governed by a + BSD-style license that can be found in the LICENSE file. +*/ + +#include "pybind11_tests.h" + +struct CustomGuard { + static bool enabled; + + CustomGuard() { enabled = true; } + ~CustomGuard() { enabled = false; } + + static const char *report_status() { return enabled ? "guarded" : "unguarded"; } +}; +bool CustomGuard::enabled = false; + +struct DependentGuard { + static bool enabled; + + DependentGuard() { enabled = CustomGuard::enabled; } + ~DependentGuard() { enabled = false; } + + static const char *report_status() { return enabled ? "guarded" : "unguarded"; } +}; +bool DependentGuard::enabled = false; + +TEST_SUBMODULE(call_policies, m) { + // Parent/Child are used in: + // test_keep_alive_argument, test_keep_alive_return_value, test_alive_gc_derived, + // test_alive_gc_multi_derived, test_return_none, test_keep_alive_constructor + class Child { + public: + Child() { py::print("Allocating child."); } + Child(const Child &) = default; + Child(Child &&) = default; + ~Child() { py::print("Releasing child."); } + }; + py::class_(m, "Child").def(py::init<>()); + + class Parent { + public: + Parent() { py::print("Allocating parent."); } + Parent(const Parent &parent) = default; + ~Parent() { py::print("Releasing parent."); } + void addChild(Child *) {} + Child *returnChild() { return new Child(); } + Child *returnNullChild() { return nullptr; } + static Child *staticFunction(Parent *) { return new Child(); } + }; + py::class_(m, "Parent") + .def(py::init<>()) + .def(py::init([](Child *) { return new Parent(); }), py::keep_alive<1, 2>()) + .def("addChild", &Parent::addChild) + .def("addChildKeepAlive", &Parent::addChild, py::keep_alive<1, 2>()) + .def("returnChild", &Parent::returnChild) + .def("returnChildKeepAlive", &Parent::returnChild, py::keep_alive<1, 0>()) + .def("returnNullChildKeepAliveChild", &Parent::returnNullChild, py::keep_alive<1, 0>()) + .def("returnNullChildKeepAliveParent", &Parent::returnNullChild, py::keep_alive<0, 1>()) + .def_static("staticFunction", &Parent::staticFunction, py::keep_alive<1, 0>()); + + m.def("free_function", [](Parent *, Child *) {}, py::keep_alive<1, 2>()); + m.def("invalid_arg_index", [] {}, py::keep_alive<0, 1>()); + +#if !defined(PYPY_VERSION) + // test_alive_gc + class ParentGC : public Parent { + public: + using Parent::Parent; + }; + py::class_(m, "ParentGC", py::dynamic_attr()).def(py::init<>()); +#endif + + // test_call_guard + m.def("unguarded_call", &CustomGuard::report_status); + m.def("guarded_call", &CustomGuard::report_status, py::call_guard()); + + m.def( + "multiple_guards_correct_order", + []() { + return CustomGuard::report_status() + std::string(" & ") + + DependentGuard::report_status(); + }, + py::call_guard()); + + m.def( + "multiple_guards_wrong_order", + []() { + return DependentGuard::report_status() + std::string(" & ") + + CustomGuard::report_status(); + }, + py::call_guard()); + +#if !defined(PYPY_VERSION) && !defined(GRAALVM_PYTHON) + // `py::call_guard()` should work in PyPy/GraalPy as well, + // but it's unclear how to test it without `PyGILState_GetThisThreadState`. + auto report_gil_status = []() { + auto is_gil_held = false; + if (auto *tstate = py::detail::get_thread_state_unchecked()) { + is_gil_held = (tstate == PyGILState_GetThisThreadState()); + } + + return is_gil_held ? "GIL held" : "GIL released"; + }; + + m.def("with_gil", report_gil_status); + m.def("without_gil", report_gil_status, py::call_guard()); +#endif +} diff --git a/external_libraries/pybind11/tests/test_call_policies.py b/external_libraries/pybind11/tests/test_call_policies.py new file mode 100644 index 00000000..11aab9fd --- /dev/null +++ b/external_libraries/pybind11/tests/test_call_policies.py @@ -0,0 +1,256 @@ +from __future__ import annotations + +import pytest + +import env # noqa: F401 +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) +@pytest.mark.skipif("env.GRAALPY", reason="Cannot reliably trigger GC") +def test_keep_alive_argument(capture): + n_inst = ConstructorStats.detail_reg_inst() + with capture: + p = m.Parent() + assert capture == "Allocating parent." + with capture: + p.addChild(m.Child()) + assert ConstructorStats.detail_reg_inst() == n_inst + 1 + assert ( + capture + == """ + Allocating child. + Releasing child. + """ + ) + with capture: + del p + assert ConstructorStats.detail_reg_inst() == n_inst + assert capture == "Releasing parent." + + with capture: + p = m.Parent() + assert capture == "Allocating parent." + with capture: + p.addChildKeepAlive(m.Child()) + assert ConstructorStats.detail_reg_inst() == n_inst + 2 + assert capture == "Allocating child." + with capture: + del p + assert ConstructorStats.detail_reg_inst() == n_inst + assert ( + capture + == """ + Releasing parent. + Releasing child. + """ + ) + + p = m.Parent() + c = m.Child() + assert ConstructorStats.detail_reg_inst() == n_inst + 2 + m.free_function(p, c) + del c + assert ConstructorStats.detail_reg_inst() == n_inst + 2 + del p + assert ConstructorStats.detail_reg_inst() == n_inst + + with pytest.raises(RuntimeError) as excinfo: + m.invalid_arg_index() + assert str(excinfo.value) == "Could not activate keep_alive!" + + +@pytest.mark.skipif("env.GRAALPY", reason="Cannot reliably trigger GC") +def test_keep_alive_return_value(capture): + n_inst = ConstructorStats.detail_reg_inst() + with capture: + p = m.Parent() + assert capture == "Allocating parent." + with capture: + p.returnChild() + assert ConstructorStats.detail_reg_inst() == n_inst + 1 + assert ( + capture + == """ + Allocating child. + Releasing child. + """ + ) + with capture: + del p + assert ConstructorStats.detail_reg_inst() == n_inst + assert capture == "Releasing parent." + + with capture: + p = m.Parent() + assert capture == "Allocating parent." + with capture: + p.returnChildKeepAlive() + assert ConstructorStats.detail_reg_inst() == n_inst + 2 + assert capture == "Allocating child." + with capture: + del p + assert ConstructorStats.detail_reg_inst() == n_inst + assert ( + capture + == """ + Releasing parent. + Releasing child. + """ + ) + + p = m.Parent() + assert ConstructorStats.detail_reg_inst() == n_inst + 1 + with capture: + m.Parent.staticFunction(p) + assert ConstructorStats.detail_reg_inst() == n_inst + 2 + assert capture == "Allocating child." + with capture: + del p + assert ConstructorStats.detail_reg_inst() == n_inst + assert ( + capture + == """ + Releasing parent. + Releasing child. + """ + ) + + +# https://foss.heptapod.net/pypy/pypy/-/issues/2447 +@pytest.mark.xfail("env.PYPY", reason="_PyObject_GetDictPtr is unimplemented") +@pytest.mark.skipif("env.GRAALPY", reason="Cannot reliably trigger GC") +def test_alive_gc(capture): + n_inst = ConstructorStats.detail_reg_inst() + p = m.ParentGC() + p.addChildKeepAlive(m.Child()) + assert ConstructorStats.detail_reg_inst() == n_inst + 2 + lst = [p] + lst.append(lst) # creates a circular reference + with capture: + del p, lst + assert ConstructorStats.detail_reg_inst() == n_inst + assert ( + capture + == """ + Releasing parent. + Releasing child. + """ + ) + + +@pytest.mark.skipif("env.GRAALPY", reason="Cannot reliably trigger GC") +def test_alive_gc_derived(capture): + class Derived(m.Parent): + pass + + n_inst = ConstructorStats.detail_reg_inst() + p = Derived() + p.addChildKeepAlive(m.Child()) + assert ConstructorStats.detail_reg_inst() == n_inst + 2 + lst = [p] + lst.append(lst) # creates a circular reference + with capture: + del p, lst + assert ConstructorStats.detail_reg_inst() == n_inst + assert ( + capture + == """ + Releasing parent. + Releasing child. + """ + ) + + +@pytest.mark.skipif("env.GRAALPY", reason="Cannot reliably trigger GC") +def test_alive_gc_multi_derived(capture): + class Derived(m.Parent, m.Child): + def __init__(self): + m.Parent.__init__(self) + m.Child.__init__(self) + + n_inst = ConstructorStats.detail_reg_inst() + p = Derived() + p.addChildKeepAlive(m.Child()) + # +3 rather than +2 because Derived corresponds to two registered instances + assert ConstructorStats.detail_reg_inst() == n_inst + 3 + lst = [p] + lst.append(lst) # creates a circular reference + with capture: + del p, lst + assert ConstructorStats.detail_reg_inst() == n_inst + assert ( + capture + == """ + Releasing parent. + Releasing child. + Releasing child. + """ + ) + + +@pytest.mark.skipif("env.GRAALPY", reason="Cannot reliably trigger GC") +def test_return_none(capture): + n_inst = ConstructorStats.detail_reg_inst() + with capture: + p = m.Parent() + assert capture == "Allocating parent." + with capture: + p.returnNullChildKeepAliveChild() + assert ConstructorStats.detail_reg_inst() == n_inst + 1 + assert capture == "" + with capture: + del p + assert ConstructorStats.detail_reg_inst() == n_inst + assert capture == "Releasing parent." + + with capture: + p = m.Parent() + assert capture == "Allocating parent." + with capture: + p.returnNullChildKeepAliveParent() + assert ConstructorStats.detail_reg_inst() == n_inst + 1 + assert capture == "" + with capture: + del p + assert ConstructorStats.detail_reg_inst() == n_inst + assert capture == "Releasing parent." + + +@pytest.mark.skipif("env.GRAALPY", reason="Cannot reliably trigger GC") +def test_keep_alive_constructor(capture): + n_inst = ConstructorStats.detail_reg_inst() + + with capture: + p = m.Parent(m.Child()) + assert ConstructorStats.detail_reg_inst() == n_inst + 2 + assert ( + capture + == """ + Allocating child. + Allocating parent. + """ + ) + with capture: + del p + assert ConstructorStats.detail_reg_inst() == n_inst + assert ( + capture + == """ + Releasing parent. + Releasing child. + """ + ) + + +def test_call_guard(): + assert m.unguarded_call() == "unguarded" + assert m.guarded_call() == "guarded" + + assert m.multiple_guards_correct_order() == "guarded & guarded" + assert m.multiple_guards_wrong_order() == "unguarded & guarded" + + if hasattr(m, "with_gil"): + assert m.with_gil() == "GIL held" + assert m.without_gil() == "GIL released" diff --git a/external_libraries/pybind11/tests/test_callbacks.cpp b/external_libraries/pybind11/tests/test_callbacks.cpp new file mode 100644 index 00000000..9361451a --- /dev/null +++ b/external_libraries/pybind11/tests/test_callbacks.cpp @@ -0,0 +1,302 @@ +/* + tests/test_callbacks.cpp -- callbacks + + Copyright (c) 2016 Wenzel Jakob + + All rights reserved. Use of this source code is governed by a + BSD-style license that can be found in the LICENSE file. +*/ + +#include + +#include "constructor_stats.h" +#include "pybind11_tests.h" + +#include + +namespace test_callbacks { +namespace boost_histogram { // See PR #5580 + +double custom_transform_double(double value) { return value * 3; } +int custom_transform_int(int value) { return value; } + +// Originally derived from +// https://github.com/scikit-hep/boost-histogram/blob/460ef90905d6a8a9e6dd3beddfe7b4b49b364579/include/bh_python/transform.hpp#L68-L85 +double apply_custom_transform(const py::object &src, double value) { + using raw_t = double(double); + + py::detail::make_caster> func_caster; + if (!func_caster.load(src, /*convert*/ false)) { + return -100; + } + auto func = static_cast &>(func_caster); + auto *cfunc = func.target(); + if (cfunc == nullptr) { + return -200; + } + return (*cfunc)(value); +} + +} // namespace boost_histogram +} // namespace test_callbacks + +int dummy_function(int i) { return i + 1; } + +TEST_SUBMODULE(callbacks, m) { + // test_callbacks, test_function_signatures + m.def("test_callback1", [](const py::object &func) { return func(); }); + m.def("test_callback2", [](const py::object &func) { return func("Hello", 'x', true, 5); }); + m.def("test_callback3", [](const std::function &func) { + return "func(43) = " + std::to_string(func(43)); + }); + m.def("test_callback4", + []() -> std::function { return [](int i) { return i + 1; }; }); + m.def("test_callback5", + []() { return py::cpp_function([](int i) { return i + 1; }, py::arg("number")); }); + + // test_keyword_args_and_generalized_unpacking + m.def("test_tuple_unpacking", [](const py::function &f) { + auto t1 = py::make_tuple(2, 3); + auto t2 = py::make_tuple(5, 6); + return f("positional", 1, *t1, 4, *t2); + }); + + m.def("test_dict_unpacking", [](const py::function &f) { + auto d1 = py::dict("key"_a = "value", "a"_a = 1); + auto d2 = py::dict(); + auto d3 = py::dict("b"_a = 2); + return f("positional", 1, **d1, **d2, **d3); + }); + + m.def("test_keyword_args", [](const py::function &f) { return f("x"_a = 10, "y"_a = 20); }); + + m.def("test_unpacking_and_keywords1", [](const py::function &f) { + auto args = py::make_tuple(2); + auto kwargs = py::dict("d"_a = 4); + return f(1, *args, "c"_a = 3, **kwargs); + }); + + m.def("test_unpacking_and_keywords2", [](const py::function &f) { + auto kwargs1 = py::dict("a"_a = 1); + auto kwargs2 = py::dict("c"_a = 3, "d"_a = 4); + return f("positional", + *py::make_tuple(1), + 2, + *py::make_tuple(3, 4), + 5, + "key"_a = "value", + **kwargs1, + "b"_a = 2, + **kwargs2, + "e"_a = 5); + }); + + m.def("test_unpacking_error1", [](const py::function &f) { + auto kwargs = py::dict("x"_a = 3); + return f("x"_a = 1, "y"_a = 2, **kwargs); // duplicate ** after keyword + }); + + m.def("test_unpacking_error2", [](const py::function &f) { + auto kwargs = py::dict("x"_a = 3); + return f(**kwargs, "x"_a = 1); // duplicate keyword after ** + }); + + m.def("test_arg_conversion_error1", + [](const py::function &f) { f(234, UnregisteredType(), "kw"_a = 567); }); + + m.def("test_arg_conversion_error2", [](const py::function &f) { + f(234, "expected_name"_a = UnregisteredType(), "kw"_a = 567); + }); + + // test_lambda_closure_cleanup + struct Payload { + Payload() { print_default_created(this); } + ~Payload() { print_destroyed(this); } + Payload(const Payload &) { print_copy_created(this); } + Payload(Payload &&) noexcept { print_move_created(this); } + }; + // Export the payload constructor statistics for testing purposes: + m.def("payload_cstats", &ConstructorStats::get); + m.def("test_lambda_closure_cleanup", []() -> std::function { + Payload p; + + // In this situation, `Func` in the implementation of + // `cpp_function::initialize` is NOT trivially destructible. + return [p]() { + /* p should be cleaned up when the returned function is garbage collected */ + (void) p; + }; + }); + + class CppCallable { + public: + CppCallable() { track_default_created(this); } + ~CppCallable() { track_destroyed(this); } + CppCallable(const CppCallable &) { track_copy_created(this); } + CppCallable(CppCallable &&) noexcept { track_move_created(this); } + void operator()() {} + }; + + m.def("test_cpp_callable_cleanup", []() { + // Related issue: https://github.com/pybind/pybind11/issues/3228 + // Related PR: https://github.com/pybind/pybind11/pull/3229 + py::list alive_counts; + ConstructorStats &stat = ConstructorStats::get(); + alive_counts.append(stat.alive()); + { + CppCallable cpp_callable; + alive_counts.append(stat.alive()); + { + // In this situation, `Func` in the implementation of + // `cpp_function::initialize` IS trivially destructible, + // only `capture` is not. + py::cpp_function py_func(cpp_callable); + py::detail::silence_unused_warnings(py_func); + alive_counts.append(stat.alive()); + } + alive_counts.append(stat.alive()); + { + py::cpp_function py_func(std::move(cpp_callable)); + py::detail::silence_unused_warnings(py_func); + alive_counts.append(stat.alive()); + } + alive_counts.append(stat.alive()); + } + alive_counts.append(stat.alive()); + return alive_counts; + }); + + // test_cpp_function_roundtrip + /* Test if passing a function pointer from C++ -> Python -> C++ yields the original pointer */ + m.def("dummy_function", &dummy_function); + m.def("dummy_function_overloaded", [](int i, int j) { return i + j; }); + m.def("dummy_function_overloaded", &dummy_function); + m.def("dummy_function2", [](int i, int j) { return i + j; }); + m.def( + "roundtrip", + [](std::function f, bool expect_none) { + if (expect_none && f) { + throw std::runtime_error("Expected None to be converted to empty std::function"); + } + return f; + }, + py::arg("f"), + py::arg("expect_none") = false); + m.def("test_dummy_function", [](const std::function &f) -> std::string { + using fn_type = int (*)(int); + const auto *result = f.target(); + if (!result) { + auto r = f(1); + return "can't convert to function pointer: eval(1) = " + std::to_string(r); + } + if (*result == dummy_function) { + auto r = (*result)(1); + return "matches dummy_function: eval(1) = " + std::to_string(r); + } + return "argument does NOT match dummy_function. This should never happen!"; + }); + + class AbstractBase { + public: + // [workaround(intel)] = default does not work here + // Defaulting this destructor results in linking errors with the Intel compiler + // (in Debug builds only, tested with icpc (ICC) 2021.1 Beta 20200827) + virtual ~AbstractBase() {} // NOLINT(modernize-use-equals-default) + virtual unsigned int func() = 0; + }; + m.def("func_accepting_func_accepting_base", + [](const std::function &) {}); + + struct MovableObject { + bool valid = true; + + MovableObject() = default; + MovableObject(const MovableObject &) = default; + MovableObject &operator=(const MovableObject &) = default; + MovableObject(MovableObject &&o) noexcept : valid(o.valid) { o.valid = false; } + MovableObject &operator=(MovableObject &&o) noexcept { + valid = o.valid; + o.valid = false; + return *this; + } + }; + py::class_(m, "MovableObject"); + + // test_movable_object + m.def("callback_with_movable", [](const std::function &f) { + auto x = MovableObject(); + f(x); // lvalue reference shouldn't move out object + return x.valid; // must still return `true` + }); + + // test_bound_method_callback + struct CppBoundMethodTest {}; + py::class_(m, "CppBoundMethodTest") + .def(py::init<>()) + .def("triple", [](CppBoundMethodTest &, int val) { return 3 * val; }); + + // This checks that builtin functions can be passed as callbacks + // rather than throwing RuntimeError due to trying to extract as capsule + m.def("test_sum_builtin", + [](const std::function &sum_builtin, const py::iterable &i) { + return sum_builtin(i); + }); + + // test async Python callbacks + using callback_f = std::function; + m.def("test_async_callback", [](const callback_f &f, const py::list &work) { + // make detached thread that calls `f` with piece of work after a little delay + auto start_f = [f](int j) { + auto invoke_f = [f, j] { + std::this_thread::sleep_for(std::chrono::milliseconds(50)); + f(j); + }; + auto t = std::thread(std::move(invoke_f)); + t.detach(); + }; + + // spawn worker threads + for (auto i : work) { + start_f(py::cast(i)); + } + }); + + m.def("callback_num_times", [](const py::function &f, std::size_t num) { + for (std::size_t i = 0; i < num; i++) { + f(); + } + }); + + auto *custom_def = []() { + static PyMethodDef def; + def.ml_name = "example_name"; + def.ml_doc = "Example doc"; + def.ml_meth = [](PyObject *, PyObject *args) -> PyObject * { + if (PyTuple_Size(args) != 1) { + throw std::runtime_error("Invalid number of arguments for example_name"); + } + PyObject *first = PyTuple_GetItem(args, 0); + if (!PyLong_Check(first)) { + throw std::runtime_error("Invalid argument to example_name"); + } + auto result = py::cast(PyLong_AsLong(first) * 9); + return result.release().ptr(); + }; + def.ml_flags = METH_VARARGS; + return &def; + }(); + + py::capsule rec_capsule(std::malloc(1), [](void *data) { std::free(data); }); + m.add_object("custom_function", PyCFunction_New(custom_def, rec_capsule.ptr())); + + // rec_capsule with nullptr name + py::capsule rec_capsule2(std::malloc(1), [](void *data) { std::free(data); }); + m.add_object("custom_function2", PyCFunction_New(custom_def, rec_capsule2.ptr())); + + m.def("boost_histogram_custom_transform_double", + test_callbacks::boost_histogram::custom_transform_double); + m.def("boost_histogram_custom_transform_int", + test_callbacks::boost_histogram::custom_transform_int); + m.def("boost_histogram_apply_custom_transform", + test_callbacks::boost_histogram::apply_custom_transform); +} diff --git a/external_libraries/pybind11/tests/test_callbacks.py b/external_libraries/pybind11/tests/test_callbacks.py new file mode 100644 index 00000000..327e41eb --- /dev/null +++ b/external_libraries/pybind11/tests/test_callbacks.py @@ -0,0 +1,248 @@ +from __future__ import annotations + +import sys +import time +from threading import Thread + +import pytest + +import env # noqa: F401 +from pybind11_tests import callbacks as m +from pybind11_tests import detailed_error_messages_enabled + + +def test_callbacks(): + from functools import partial + + def func1(): + return "func1" + + def func2(a, b, c, d): + return "func2", a, b, c, d + + def func3(a): + return f"func3({a})" + + assert m.test_callback1(func1) == "func1" + assert m.test_callback2(func2) == ("func2", "Hello", "x", True, 5) + assert m.test_callback1(partial(func2, 1, 2, 3, 4)) == ("func2", 1, 2, 3, 4) + assert m.test_callback1(partial(func3, "partial")) == "func3(partial)" + assert m.test_callback3(lambda i: i + 1) == "func(43) = 44" + + f = m.test_callback4() + assert f(43) == 44 + f = m.test_callback5() + assert f(number=43) == 44 + + +def test_bound_method_callback(): + # Bound Python method: + class MyClass: + def double(self, val): + return 2 * val + + z = MyClass() + assert m.test_callback3(z.double) == "func(43) = 86" + + z = m.CppBoundMethodTest() + assert m.test_callback3(z.triple) == "func(43) = 129" + + +def test_keyword_args_and_generalized_unpacking(): + def f(*args, **kwargs): + return args, kwargs + + assert m.test_tuple_unpacking(f) == (("positional", 1, 2, 3, 4, 5, 6), {}) + assert m.test_dict_unpacking(f) == ( + ("positional", 1), + {"key": "value", "a": 1, "b": 2}, + ) + assert m.test_keyword_args(f) == ((), {"x": 10, "y": 20}) + assert m.test_unpacking_and_keywords1(f) == ((1, 2), {"c": 3, "d": 4}) + assert m.test_unpacking_and_keywords2(f) == ( + ("positional", 1, 2, 3, 4, 5), + {"key": "value", "a": 1, "b": 2, "c": 3, "d": 4, "e": 5}, + ) + + with pytest.raises(TypeError) as excinfo: + m.test_unpacking_error1(f) + assert "Got multiple values for keyword argument" in str(excinfo.value) + + with pytest.raises(TypeError) as excinfo: + m.test_unpacking_error2(f) + assert "Got multiple values for keyword argument" in str(excinfo.value) + + with pytest.raises(RuntimeError) as excinfo: + m.test_arg_conversion_error1(f) + assert str(excinfo.value) == "Unable to convert call argument " + ( + "'1' of type 'UnregisteredType' to Python object" + if detailed_error_messages_enabled + else "'1' to Python object (#define PYBIND11_DETAILED_ERROR_MESSAGES or compile in debug mode for details)" + ) + + with pytest.raises(RuntimeError) as excinfo: + m.test_arg_conversion_error2(f) + assert str(excinfo.value) == "Unable to convert call argument " + ( + "'expected_name' of type 'UnregisteredType' to Python object" + if detailed_error_messages_enabled + else "'expected_name' to Python object " + "(#define PYBIND11_DETAILED_ERROR_MESSAGES or compile in debug mode for details)" + ) + + +@pytest.mark.skipif("env.GRAALPY", reason="Cannot reliably trigger GC") +def test_lambda_closure_cleanup(): + m.test_lambda_closure_cleanup() + cstats = m.payload_cstats() + assert cstats.alive() == 0 + assert cstats.copy_constructions == 1 + assert cstats.move_constructions >= 1 + + +@pytest.mark.skipif("env.GRAALPY", reason="Cannot reliably trigger GC") +def test_cpp_callable_cleanup(): + alive_counts = m.test_cpp_callable_cleanup() + assert alive_counts == [0, 1, 2, 1, 2, 1, 0] + + +def test_cpp_function_roundtrip(): + """Test if passing a function pointer from C++ -> Python -> C++ yields the original pointer""" + + assert ( + m.test_dummy_function(m.dummy_function) == "matches dummy_function: eval(1) = 2" + ) + assert ( + m.test_dummy_function(m.roundtrip(m.dummy_function)) + == "matches dummy_function: eval(1) = 2" + ) + assert ( + m.test_dummy_function(m.dummy_function_overloaded) + == "matches dummy_function: eval(1) = 2" + ) + assert m.roundtrip(None, expect_none=True) is None + assert ( + m.test_dummy_function(lambda x: x + 2) + == "can't convert to function pointer: eval(1) = 3" + ) + + with pytest.raises(TypeError) as excinfo: + m.test_dummy_function(m.dummy_function2) + assert "incompatible function arguments" in str(excinfo.value) + + with pytest.raises(TypeError) as excinfo: + m.test_dummy_function(lambda x, y: x + y) + assert any( + s in str(excinfo.value) + for s in ("missing 1 required positional argument", "takes exactly 2 arguments") + ) + + +def test_function_signatures(doc): + assert ( + doc(m.test_callback3) + == "test_callback3(arg0: collections.abc.Callable[[typing.SupportsInt | typing.SupportsIndex], int]) -> str" + ) + assert ( + doc(m.test_callback4) + == "test_callback4() -> collections.abc.Callable[[typing.SupportsInt | typing.SupportsIndex], int]" + ) + + +def test_movable_object(): + assert m.callback_with_movable(lambda _: None) is True + + +@pytest.mark.skipif( + "env.PYPY", + reason="PyPy segfaults on here. See discussion on #1413.", +) +def test_python_builtins(): + """Test if python builtins like sum() can be used as callbacks""" + assert m.test_sum_builtin(sum, [1, 2, 3]) == 6 + assert m.test_sum_builtin(sum, []) == 0 + + +@pytest.mark.skipif(sys.platform.startswith("emscripten"), reason="Requires threads") +def test_async_callbacks(): + # serves as state for async callback + class Item: + def __init__(self, value): + self.value = value + + res = [] + + # generate stateful lambda that will store result in `res` + def gen_f(): + s = Item(3) + return lambda j: res.append(s.value + j) + + # do some work async + work = [1, 2, 3, 4] + m.test_async_callback(gen_f(), work) + # Wait for all detached worker threads to finish. + deadline = time.monotonic() + 5.0 + while len(res) < len(work) and time.monotonic() < deadline: + time.sleep(0.01) + assert len(res) == len(work), f"Timed out waiting for callbacks: res={res!r}" + assert sum(res) == sum(x + 3 for x in work) + + +@pytest.mark.skipif(sys.platform.startswith("emscripten"), reason="Requires threads") +def test_async_async_callbacks(): + t = Thread(target=test_async_callbacks) + t.start() + t.join() + + +def test_callback_num_times(): + # Super-simple micro-benchmarking related to PR #2919. + # Example runtimes (Intel Xeon 2.2GHz, fully optimized): + # num_millions 1, repeats 2: 0.1 secs + # num_millions 20, repeats 10: 11.5 secs + one_million = 1000000 + num_millions = 1 # Try 20 for actual micro-benchmarking. + repeats = 2 # Try 10. + rates = [] + for rep in range(repeats): + t0 = time.time() + m.callback_num_times(lambda: None, num_millions * one_million) + td = time.time() - t0 + rate = num_millions / td if td else 0 + rates.append(rate) + if not rep: + print() + print( + f"callback_num_times: {num_millions:d} million / {td:.3f} seconds = {rate:.3f} million / second" + ) + if len(rates) > 1: + print("Min Mean Max") + print(f"{min(rates):6.3f} {sum(rates) / len(rates):6.3f} {max(rates):6.3f}") + + +def test_custom_func(): + assert m.custom_function(4) == 36 + assert m.roundtrip(m.custom_function)(4) == 36 + + +def test_custom_func2(): + assert m.custom_function2(3) == 27 + assert m.roundtrip(m.custom_function2)(3) == 27 + + +def test_callback_docstring(): + assert ( + m.test_tuple_unpacking.__doc__.strip() + == "test_tuple_unpacking(arg0: collections.abc.Callable) -> object" + ) + + +def test_boost_histogram_apply_custom_transform(): + ctd = m.boost_histogram_custom_transform_double + cti = m.boost_histogram_custom_transform_int + apply = m.boost_histogram_apply_custom_transform + assert apply(ctd, 5) == 15 + assert apply(cti, 0) == -200 + assert apply(None, 0) == -100 + assert apply(lambda value: value, 9) == -200 + assert apply({}, 0) == -100 + assert apply("", 0) == -100 diff --git a/external_libraries/pybind11/tests/test_chrono.cpp b/external_libraries/pybind11/tests/test_chrono.cpp new file mode 100644 index 00000000..8be0ffd1 --- /dev/null +++ b/external_libraries/pybind11/tests/test_chrono.cpp @@ -0,0 +1,81 @@ +/* + tests/test_chrono.cpp -- test conversions to/from std::chrono types + + Copyright (c) 2016 Trent Houliston and + Wenzel Jakob + + All rights reserved. Use of this source code is governed by a + BSD-style license that can be found in the LICENSE file. +*/ + +#include + +#include "pybind11_tests.h" + +#include + +struct different_resolutions { + using time_point_h = std::chrono::time_point; + using time_point_m = std::chrono::time_point; + using time_point_s = std::chrono::time_point; + using time_point_ms + = std::chrono::time_point; + using time_point_us + = std::chrono::time_point; + time_point_h timestamp_h; + time_point_m timestamp_m; + time_point_s timestamp_s; + time_point_ms timestamp_ms; + time_point_us timestamp_us; +}; + +TEST_SUBMODULE(chrono, m) { + using system_time = std::chrono::system_clock::time_point; + using steady_time = std::chrono::steady_clock::time_point; + + using timespan = std::chrono::duration; + using timestamp = std::chrono::time_point; + + // test_chrono_system_clock + // Return the current time off the wall clock + m.def("test_chrono1", []() { return std::chrono::system_clock::now(); }); + + // test_chrono_system_clock_roundtrip + // Round trip the passed in system clock time + m.def("test_chrono2", [](system_time t) { return t; }); + + // test_chrono_duration_roundtrip + // Round trip the passed in duration + m.def("test_chrono3", [](std::chrono::system_clock::duration d) { return d; }); + + // test_chrono_duration_subtraction_equivalence + // Difference between two passed in time_points + m.def("test_chrono4", [](system_time a, system_time b) { return a - b; }); + + // test_chrono_steady_clock + // Return the current time off the steady_clock + m.def("test_chrono5", []() { return std::chrono::steady_clock::now(); }); + + // test_chrono_steady_clock_roundtrip + // Round trip a steady clock timepoint + m.def("test_chrono6", [](steady_time t) { return t; }); + + // test_floating_point_duration + // Roundtrip a duration in microseconds from a float argument + m.def("test_chrono7", [](std::chrono::microseconds t) { return t; }); + // Float durations (issue #719) + m.def("test_chrono_float_diff", + [](std::chrono::duration a, std::chrono::duration b) { return a - b; }); + + m.def("test_nano_timepoint", + [](timestamp start, timespan delta) -> timestamp { return start + delta; }); + + // Test different resolutions + py::class_(m, "different_resolutions") + .def(py::init<>()) + .def_readwrite("timestamp_h", &different_resolutions::timestamp_h) + .def_readwrite("timestamp_m", &different_resolutions::timestamp_m) + .def_readwrite("timestamp_s", &different_resolutions::timestamp_s) + .def_readwrite("timestamp_ms", &different_resolutions::timestamp_ms) + .def_readwrite("timestamp_us", &different_resolutions::timestamp_us); +} diff --git a/external_libraries/pybind11/tests/test_chrono.py b/external_libraries/pybind11/tests/test_chrono.py new file mode 100644 index 00000000..ed889fbd --- /dev/null +++ b/external_libraries/pybind11/tests/test_chrono.py @@ -0,0 +1,207 @@ +from __future__ import annotations + +import datetime + +import pytest + +import env # noqa: F401 +from pybind11_tests import chrono as m + + +def test_chrono_system_clock(): + # Get the time from both c++ and datetime + date0 = datetime.datetime.today() + date1 = m.test_chrono1() + date2 = datetime.datetime.today() + + # The returned value should be a datetime + assert isinstance(date1, datetime.datetime) + + # The numbers should vary by a very small amount (time it took to execute) + diff_python = abs(date2 - date0) + diff = abs(date1 - date2) + + # There should never be a days difference + assert diff.days == 0 + + # Since datetime.datetime.today() calls time.time(), and on some platforms + # that has 1 second accuracy, we compare this way + assert diff.seconds <= diff_python.seconds + + +def test_chrono_system_clock_roundtrip(): + date1 = datetime.datetime.today() + + # Roundtrip the time + date2 = m.test_chrono2(date1) + + # The returned value should be a datetime + assert isinstance(date2, datetime.datetime) + + # They should be identical (no information lost on roundtrip) + diff = abs(date1 - date2) + assert diff == datetime.timedelta(0) + + +def test_chrono_system_clock_roundtrip_date(): + date1 = datetime.date.today() + + # Roundtrip the time + datetime2 = m.test_chrono2(date1) + date2 = datetime2.date() + time2 = datetime2.time() + + # The returned value should be a datetime + assert isinstance(datetime2, datetime.datetime) + assert isinstance(date2, datetime.date) + assert isinstance(time2, datetime.time) + + # They should be identical (no information lost on roundtrip) + diff = abs(date1 - date2) + assert diff.days == 0 + assert diff.seconds == 0 + assert diff.microseconds == 0 + + # Year, Month & Day should be the same after the round trip + assert date1 == date2 + + # There should be no time information + assert time2.hour == 0 + assert time2.minute == 0 + assert time2.second == 0 + assert time2.microsecond == 0 + + +SKIP_TZ_ENV_ON_WIN = pytest.mark.skipif( + "env.WIN", reason="TZ environment variable only supported on POSIX" +) + + +@pytest.mark.parametrize( + "time1", + [ + datetime.datetime.today().time(), + datetime.time(0, 0, 0), + datetime.time(0, 0, 0, 1), + datetime.time(0, 28, 45, 109827), + datetime.time(0, 59, 59, 999999), + datetime.time(1, 0, 0), + datetime.time(5, 59, 59, 0), + datetime.time(5, 59, 59, 1), + ], +) +@pytest.mark.parametrize( + "tz", + [ + None, + pytest.param("Europe/Brussels", marks=SKIP_TZ_ENV_ON_WIN), + pytest.param("Asia/Pyongyang", marks=SKIP_TZ_ENV_ON_WIN), + pytest.param("America/New_York", marks=SKIP_TZ_ENV_ON_WIN), + ], +) +def test_chrono_system_clock_roundtrip_time(time1, tz, monkeypatch): + if tz is not None: + monkeypatch.setenv("TZ", f"/usr/share/zoneinfo/{tz}") + + # Roundtrip the time + datetime2 = m.test_chrono2(time1) + date2 = datetime2.date() + time2 = datetime2.time() + + # The returned value should be a datetime + assert isinstance(datetime2, datetime.datetime) + assert isinstance(date2, datetime.date) + assert isinstance(time2, datetime.time) + + # Hour, Minute, Second & Microsecond should be the same after the round trip + assert time1 == time2 + + # There should be no date information (i.e. date = python base date) + assert date2.year == 1970 + assert date2.month == 1 + assert date2.day == 1 + + +def test_chrono_duration_roundtrip(): + # Get the difference between two times (a timedelta) + date1 = datetime.datetime.today() + date2 = datetime.datetime.today() + diff = date2 - date1 + + # Make sure this is a timedelta + assert isinstance(diff, datetime.timedelta) + + cpp_diff = m.test_chrono3(diff) + + assert cpp_diff == diff + + # Negative timedelta roundtrip + diff = datetime.timedelta(microseconds=-1) + cpp_diff = m.test_chrono3(diff) + + assert cpp_diff == diff + + +def test_chrono_duration_subtraction_equivalence(): + date1 = datetime.datetime.today() + date2 = datetime.datetime.today() + + diff = date2 - date1 + cpp_diff = m.test_chrono4(date2, date1) + + assert cpp_diff == diff + + +def test_chrono_duration_subtraction_equivalence_date(): + date1 = datetime.date.today() + date2 = datetime.date.today() + + diff = date2 - date1 + cpp_diff = m.test_chrono4(date2, date1) + + assert cpp_diff == diff + + +def test_chrono_steady_clock(): + time1 = m.test_chrono5() + assert isinstance(time1, datetime.timedelta) + + +def test_chrono_steady_clock_roundtrip(): + time1 = datetime.timedelta(days=10, seconds=10, microseconds=100) + time2 = m.test_chrono6(time1) + + assert isinstance(time2, datetime.timedelta) + + # They should be identical (no information lost on roundtrip) + assert time1 == time2 + + +def test_floating_point_duration(): + # Test using a floating point number in seconds + time = m.test_chrono7(35.525123) + + assert isinstance(time, datetime.timedelta) + + assert time.seconds == 35 + assert 525122 <= time.microseconds <= 525123 + + diff = m.test_chrono_float_diff(43.789012, 1.123456) + assert diff.seconds == 42 + assert 665556 <= diff.microseconds <= 665557 + + +def test_nano_timepoint(): + time = datetime.datetime.now() + time1 = m.test_nano_timepoint(time, datetime.timedelta(seconds=60)) + assert time1 == time + datetime.timedelta(seconds=60) + + +def test_chrono_different_resolutions(): + resolutions = m.different_resolutions() + time = datetime.datetime.now() + resolutions.timestamp_h = time + resolutions.timestamp_m = time + resolutions.timestamp_s = time + resolutions.timestamp_ms = time + resolutions.timestamp_us = time diff --git a/external_libraries/pybind11/tests/test_class.cpp b/external_libraries/pybind11/tests/test_class.cpp new file mode 100644 index 00000000..84efb800 --- /dev/null +++ b/external_libraries/pybind11/tests/test_class.cpp @@ -0,0 +1,700 @@ +/* + tests/test_class.cpp -- test py::class_ definitions and basic functionality + + Copyright (c) 2016 Wenzel Jakob + + All rights reserved. Use of this source code is governed by a + BSD-style license that can be found in the LICENSE file. +*/ + +#if defined(__INTEL_COMPILER) && __cplusplus >= 201703L +// Intel compiler requires a separate header file to support aligned new operators +// and does not set the __cpp_aligned_new feature macro. +// This header needs to be included before pybind11. +# include +#endif + +#include + +#include "constructor_stats.h" +#include "local_bindings.h" +#include "pybind11_tests.h" + +#include + +PYBIND11_WARNING_DISABLE_MSVC(4324) +// warning C4324: structure was padded due to alignment specifier + +// test_brace_initialization +struct NoBraceInitialization { + explicit NoBraceInitialization(std::vector v) : vec{std::move(v)} {} + template + NoBraceInitialization(std::initializer_list l) : vec(l) {} + + std::vector vec; +}; + +namespace test_class { +namespace pr4220_tripped_over_this { // PR #4227 + +template +struct SoEmpty {}; + +template +std::string get_msg(const T &) { + return "This is really only meant to exercise successful compilation."; +} + +using Empty0 = SoEmpty<0x0>; + +void bind_empty0(py::module_ &m) { + py::class_(m, "Empty0").def(py::init<>()).def("get_msg", get_msg); +} + +} // namespace pr4220_tripped_over_this + +namespace pr5396_forward_declared_class { +class ForwardClass; +class Args : public py::args {}; +} // namespace pr5396_forward_declared_class + +struct ConvertibleFromAnything { + ConvertibleFromAnything() = default; + template + // NOLINTNEXTLINE(bugprone-forwarding-reference-overload,google-explicit-constructor) + ConvertibleFromAnything(T &&) {} +}; + +} // namespace test_class + +static_assert(py::detail::is_same_or_base_of::value, ""); +static_assert( + py::detail::is_same_or_base_of::value, + ""); +static_assert(!py::detail::is_same_or_base_of< + py::args, + test_class::pr5396_forward_declared_class::ForwardClass>::value, + ""); + +TEST_SUBMODULE(class_, m) { + m.def("obj_class_name", [](py::handle obj) { return py::detail::obj_class_name(obj.ptr()); }); + + // test_instance + struct NoConstructor { + NoConstructor() = default; + NoConstructor(const NoConstructor &) = default; + NoConstructor(NoConstructor &&) = default; + static NoConstructor *new_instance() { + auto *ptr = new NoConstructor(); + print_created(ptr, "via new_instance"); + return ptr; + } + ~NoConstructor() { print_destroyed(this); } + }; + struct NoConstructorNew { + NoConstructorNew() = default; + NoConstructorNew(const NoConstructorNew &) = default; + NoConstructorNew(NoConstructorNew &&) = default; + static NoConstructorNew *new_instance() { + auto *ptr = new NoConstructorNew(); + print_created(ptr, "via new_instance"); + return ptr; + } + ~NoConstructorNew() { print_destroyed(this); } + }; + + struct DynamicAttr { + DynamicAttr() = default; + }; + + py::class_(m, "NoConstructor") + .def_static("new_instance", &NoConstructor::new_instance, "Return an instance"); + + py::class_(m, "NoConstructorNew") + .def(py::init([]() { return nullptr; })) // Need a NOOP __init__ + .def_static("__new__", + [](const py::object &) { return NoConstructorNew::new_instance(); }); + + py::class_(m, "DynamicAttr", py::dynamic_attr()).def(py::init<>()); + + // test_pass_unique_ptr + struct ToBeHeldByUniquePtr {}; + py::class_>(m, "ToBeHeldByUniquePtr") + .def(py::init<>()); + m.def("pass_unique_ptr", [](std::unique_ptr &&) {}); + + // test_inheritance + class Pet { + public: + Pet(const std::string &name, const std::string &species) + : m_name(name), m_species(species) {} + std::string name() const { return m_name; } + std::string species() const { return m_species; } + + private: + std::string m_name; + std::string m_species; + }; + + class Dog : public Pet { + public: + explicit Dog(const std::string &name) : Pet(name, "dog") {} + std::string bark() const { return "Woof!"; } + }; + + class Rabbit : public Pet { + public: + explicit Rabbit(const std::string &name) : Pet(name, "parrot") {} + }; + + class Hamster : public Pet { + public: + explicit Hamster(const std::string &name) : Pet(name, "rodent") {} + }; + + class Chimera : public Pet { + Chimera() : Pet("Kimmy", "chimera") {} + }; + + py::class_ pet_class(m, "Pet"); + pet_class.def(py::init()) + .def("name", &Pet::name) + .def("species", &Pet::species); + + /* One way of declaring a subclass relationship: reference parent's class_ object */ + py::class_(m, "Dog", pet_class).def(py::init()); + + /* Another way of declaring a subclass relationship: reference parent's C++ type */ + py::class_(m, "Rabbit").def(py::init()); + + /* And another: list parent in class template arguments */ + py::class_(m, "Hamster").def(py::init()); + + /* Constructors are not inherited by default */ + py::class_(m, "Chimera"); + + m.def("pet_name_species", + [](const Pet &pet) { return pet.name() + " is a " + pet.species(); }); + m.def("dog_bark", [](const Dog &dog) { return dog.bark(); }); + + // test_automatic_upcasting + struct BaseClass { + BaseClass() = default; + BaseClass(const BaseClass &) = default; + BaseClass(BaseClass &&) = default; + virtual ~BaseClass() = default; + }; + struct DerivedClass1 : BaseClass {}; + struct DerivedClass2 : BaseClass {}; + + py::class_(m, "BaseClass").def(py::init<>()); + py::class_(m, "DerivedClass1").def(py::init<>()); + py::class_(m, "DerivedClass2").def(py::init<>()); + + m.def("return_class_1", []() -> BaseClass * { return new DerivedClass1(); }); + m.def("return_class_2", []() -> BaseClass * { return new DerivedClass2(); }); + m.def("return_class_n", [](int n) -> BaseClass * { + if (n == 1) { + return new DerivedClass1(); + } + if (n == 2) { + return new DerivedClass2(); + } + return new BaseClass(); + }); + m.def("return_none", []() -> BaseClass * { return nullptr; }); + + // test_isinstance + m.def("check_instances", [](const py::list &l) { + return py::make_tuple(py::isinstance(l[0]), + py::isinstance(l[1]), + py::isinstance(l[2]), + py::isinstance(l[3]), + py::isinstance(l[4]), + py::isinstance(l[5]), + py::isinstance(l[6])); + }); + + struct Invalid {}; + + // test_type + m.def("check_type", [](int category) { + // Currently not supported (via a fail at compile time) + // See https://github.com/pybind/pybind11/issues/2486 + // if (category == 2) + // return py::type::of(); + if (category == 1) { + return py::type::of(); + } + return py::type::of(); + }); + + m.def("get_type_of", [](py::object ob) { return py::type::of(std::move(ob)); }); + + m.def("get_type_classic", [](py::handle h) { return py::type::handle_of(h); }); + + m.def("as_type", [](const py::object &ob) { return py::type(ob); }); + + // test_mismatched_holder + struct MismatchBase1 {}; + struct MismatchDerived1 : MismatchBase1 {}; + + struct MismatchBase2 {}; + struct MismatchDerived2 : MismatchBase2 {}; + + m.def("mismatched_holder_1", []() { + auto mod = py::module_::import("__main__"); + py::class_>(mod, "MismatchBase1"); + py::class_, MismatchBase1>( + mod, "MismatchDerived1"); + }); + m.def("mismatched_holder_2", []() { + auto mod = py::module_::import("__main__"); + py::class_>(mod, "MismatchBase2"); + py::class_, MismatchBase2>( + mod, "MismatchDerived2"); + }); + + // test_override_static + // #511: problem with inheritance + overwritten def_static + struct MyBase { + static std::unique_ptr make() { return std::unique_ptr(new MyBase()); } + }; + + struct MyDerived : MyBase { + static std::unique_ptr make() { + return std::unique_ptr(new MyDerived()); + } + }; + + py::class_(m, "MyBase").def_static("make", &MyBase::make); + + py::class_(m, "MyDerived") + .def_static("make", &MyDerived::make) + .def_static("make2", &MyDerived::make); + + // test_implicit_conversion_life_support + struct ConvertibleFromUserType { + int i; + + explicit ConvertibleFromUserType(UserType u) : i(u.value()) {} + }; + + py::class_(m, "AcceptsUserType").def(py::init()); + py::implicitly_convertible(); + + m.def("implicitly_convert_argument", [](const ConvertibleFromUserType &r) { return r.i; }); + m.def("implicitly_convert_variable", [](const py::object &o) { + // `o` is `UserType` and `r` is a reference to a temporary created by implicit + // conversion. This is valid when called inside a bound function because the temp + // object is attached to the same life support system as the arguments. + const auto &r = o.cast(); + return r.i; + }); + m.add_object("implicitly_convert_variable_fail", [&] { + auto f = [](PyObject *, PyObject *args) -> PyObject * { + auto o = py::reinterpret_borrow(args)[0]; + try { // It should fail here because there is no life support. + o.cast(); + } catch (const py::cast_error &e) { + return py::str(e.what()).release().ptr(); + } + return py::str().release().ptr(); + }; + + auto *def = new PyMethodDef{"f", f, METH_VARARGS, nullptr}; + py::capsule def_capsule(def, + [](void *ptr) { delete reinterpret_cast(ptr); }); + return py::reinterpret_steal( + PyCFunction_NewEx(def, def_capsule.ptr(), m.ptr())); + }()); + + // test_operator_new_delete + struct HasOpNewDel { + std::uint64_t i; + static void *operator new(size_t s) { + py::print("A new", s); + return ::operator new(s); + } + static void *operator new(size_t s, void *ptr) { + py::print("A placement-new", s); + return ptr; + } + static void operator delete(void *p) { + py::print("A delete"); + return ::operator delete(p); + } + }; + struct HasOpNewDelSize { + std::uint32_t i; + static void *operator new(size_t s) { + py::print("B new", s); + return ::operator new(s); + } + static void *operator new(size_t s, void *ptr) { + py::print("B placement-new", s); + return ptr; + } + static void operator delete(void *p, size_t s) { + py::print("B delete", s); + return ::operator delete(p); + } + }; + struct AliasedHasOpNewDelSize { + std::uint64_t i; + static void *operator new(size_t s) { + py::print("C new", s); + return ::operator new(s); + } + static void *operator new(size_t s, void *ptr) { + py::print("C placement-new", s); + return ptr; + } + static void operator delete(void *p, size_t s) { + py::print("C delete", s); + return ::operator delete(p); + } + virtual ~AliasedHasOpNewDelSize() = default; + AliasedHasOpNewDelSize() = default; + AliasedHasOpNewDelSize(const AliasedHasOpNewDelSize &) = delete; + }; + struct PyAliasedHasOpNewDelSize : AliasedHasOpNewDelSize { + PyAliasedHasOpNewDelSize() = default; + explicit PyAliasedHasOpNewDelSize(int) {} + std::uint64_t j; + }; + struct HasOpNewDelBoth { + std::uint32_t i[8]; + static void *operator new(size_t s) { + py::print("D new", s); + return ::operator new(s); + } + static void *operator new(size_t s, void *ptr) { + py::print("D placement-new", s); + return ptr; + } + static void operator delete(void *p) { + py::print("D delete"); + return ::operator delete(p); + } + static void operator delete(void *p, size_t s) { + py::print("D wrong delete", s); + return ::operator delete(p); + } + }; + py::class_(m, "HasOpNewDel").def(py::init<>()); + py::class_(m, "HasOpNewDelSize").def(py::init<>()); + py::class_(m, "HasOpNewDelBoth").def(py::init<>()); + py::class_ aliased(m, + "AliasedHasOpNewDelSize"); + aliased.def(py::init<>()); + aliased.attr("size_noalias") = py::int_(sizeof(AliasedHasOpNewDelSize)); + aliased.attr("size_alias") = py::int_(sizeof(PyAliasedHasOpNewDelSize)); + + // This test is actually part of test_local_bindings (test_duplicate_local), but we need a + // definition in a different compilation unit within the same module: + bind_local(m, "LocalExternal", py::module_local()); + + // test_bind_protected_functions + class ProtectedA { + protected: + int foo() const { return value; } + + private: + int value = 42; + }; + + class PublicistA : public ProtectedA { + public: + using ProtectedA::foo; + }; + + py::class_(m, "ProtectedA").def(py::init<>()).def("foo", &PublicistA::foo); + + class ProtectedB { + public: + virtual ~ProtectedB() = default; + ProtectedB() = default; + ProtectedB(const ProtectedB &) = delete; + + protected: + virtual int foo() const { return value; } + virtual void *void_foo() { return static_cast(&value); } + virtual void *get_self() { return static_cast(this); } + + private: + int value = 42; + }; + + class TrampolineB : public ProtectedB { + public: + int foo() const override { PYBIND11_OVERRIDE(int, ProtectedB, foo, ); } + void *void_foo() override { PYBIND11_OVERRIDE(void *, ProtectedB, void_foo, ); } + void *get_self() override { PYBIND11_OVERRIDE(void *, ProtectedB, get_self, ); } + }; + + class PublicistB : public ProtectedB { + public: + // [workaround(intel)] = default does not work here + // Removing or defaulting this destructor results in linking errors with the Intel compiler + // (in Debug builds only, tested with icpc (ICC) 2021.1 Beta 20200827) + ~PublicistB() override {}; // NOLINT(modernize-use-equals-default) + using ProtectedB::foo; + using ProtectedB::get_self; + using ProtectedB::void_foo; + }; + + m.def("read_foo", [](const void *original) { + const int *ptr = reinterpret_cast(original); + return *ptr; + }); + + m.def("pointers_equal", + [](const void *original, const void *comparison) { return original == comparison; }); + + py::class_(m, "ProtectedB") + .def(py::init<>()) + .def("foo", &PublicistB::foo) + .def("void_foo", &PublicistB::void_foo) + .def("get_self", &PublicistB::get_self); + + // test_brace_initialization + struct BraceInitialization { + int field1; + std::string field2; + }; + + py::class_(m, "BraceInitialization") + .def(py::init()) + .def_readwrite("field1", &BraceInitialization::field1) + .def_readwrite("field2", &BraceInitialization::field2); + // We *don't* want to construct using braces when the given constructor argument maps to a + // constructor, because brace initialization could go to the wrong place (in particular when + // there is also an `initializer_list`-accept constructor): + py::class_(m, "NoBraceInitialization") + .def(py::init>()) + .def_readonly("vec", &NoBraceInitialization::vec); + + // test_reentrant_implicit_conversion_failure + // #1035: issue with runaway reentrant implicit conversion + struct BogusImplicitConversion { + BogusImplicitConversion(const BogusImplicitConversion &) = default; + }; + + py::class_(m, "BogusImplicitConversion") + .def(py::init()); + + py::implicitly_convertible(); + + // test_qualname + // #1166: nested class docstring doesn't show nested name + // Also related: tests that __qualname__ is set properly + struct NestBase {}; + struct Nested {}; + py::class_ base(m, "NestBase"); + base.def(py::init<>()); + py::class_(base, "Nested") + .def(py::init<>()) + .def("fn", [](Nested &, int, NestBase &, Nested &) {}) + .def("fa", [](Nested &, int, NestBase &, Nested &) {}, "a"_a, "b"_a, "c"_a); + base.def("g", [](NestBase &, Nested &) {}); + base.def("h", []() { return NestBase(); }); + + // test_error_after_conversion + // The second-pass path through dispatcher() previously didn't + // remember which overload was used, and would crash trying to + // generate a useful error message + + struct NotRegistered {}; + struct StringWrapper { + std::string str; + }; + m.def("test_error_after_conversions", [](int) {}); + m.def("test_error_after_conversions", + [](const StringWrapper &) -> NotRegistered { return {}; }); + py::class_(m, "StringWrapper").def(py::init()); + py::implicitly_convertible(); + +#if defined(PYBIND11_CPP17) + struct alignas(1024) Aligned { + std::uintptr_t ptr() const { return (uintptr_t) this; } + }; + py::class_(m, "Aligned").def(py::init<>()).def("ptr", &Aligned::ptr); +#endif + + // test_final + struct IsFinal final {}; + py::class_(m, "IsFinal", py::is_final()); + + // test_non_final_final + struct IsNonFinalFinal {}; + py::class_(m, "IsNonFinalFinal", py::is_final()); + + // test_exception_rvalue_abort + struct PyPrintDestructor { + PyPrintDestructor() = default; + PyPrintDestructor(const PyPrintDestructor &) = default; + ~PyPrintDestructor() { py::print("Print from destructor"); } + void throw_something() { throw std::runtime_error("error"); } + }; + py::class_(m, "PyPrintDestructor") + .def(py::init<>()) + .def("throw_something", &PyPrintDestructor::throw_something); + + // test_multiple_instances_with_same_pointer + struct SamePointer {}; + static SamePointer samePointer; + py::class_>(m, "SamePointer") + .def(py::init([]() { return &samePointer; })); + + struct Empty {}; + py::class_(m, "Empty").def(py::init<>()); + + // test_base_and_derived_nested_scope + struct BaseWithNested { + struct Nested {}; + }; + + struct DerivedWithNested : BaseWithNested { + struct Nested {}; + }; + + py::class_ baseWithNested_class(m, "BaseWithNested"); + py::class_ derivedWithNested_class(m, "DerivedWithNested"); + py::class_(baseWithNested_class, "Nested") + .def_static("get_name", []() { return "BaseWithNested::Nested"; }); + py::class_(derivedWithNested_class, "Nested") + .def_static("get_name", []() { return "DerivedWithNested::Nested"; }); + + // test_register_duplicate_class + struct Duplicate {}; + struct OtherDuplicate {}; + struct DuplicateNested {}; + struct OtherDuplicateNested {}; + + m.def("register_duplicate_class_name", [](const py::module_ &m) { + py::class_(m, "Duplicate"); + py::class_(m, "Duplicate"); + }); + m.def("register_duplicate_class_type", [](const py::module_ &m) { + py::class_(m, "OtherDuplicate"); + py::class_(m, "YetAnotherDuplicate"); + }); + m.def("register_duplicate_nested_class_name", [](const py::object >) { + py::class_(gt, "DuplicateNested"); + py::class_(gt, "DuplicateNested"); + }); + m.def("register_duplicate_nested_class_type", [](const py::object >) { + py::class_(gt, "OtherDuplicateNested"); + py::class_(gt, "YetAnotherDuplicateNested"); + }); + + test_class::pr4220_tripped_over_this::bind_empty0(m); + + // Regression test for compiler error that showed up in #5866 + m.def("return_universal_recipient", []() -> test_class::ConvertibleFromAnything { + return test_class::ConvertibleFromAnything{}; + }); +} + +template +class BreaksBase { +public: + virtual ~BreaksBase() = default; + BreaksBase() = default; + BreaksBase(const BreaksBase &) = delete; +}; +template +class BreaksTramp : public BreaksBase {}; +// These should all compile just fine: +using DoesntBreak1 = py::class_, std::unique_ptr>, BreaksTramp<1>>; +using DoesntBreak2 = py::class_, BreaksTramp<2>, std::unique_ptr>>; +using DoesntBreak3 = py::class_, std::unique_ptr>>; +using DoesntBreak4 = py::class_, BreaksTramp<4>>; +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, \ + "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, \ + "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, \ + "DoesntBreak" #N " has wrong holder_type!") +CHECK_HOLDER(1, unique); +CHECK_HOLDER(2, unique); +CHECK_HOLDER(3, unique); +#ifndef PYBIND11_RUN_TESTING_WITH_SMART_HOLDER_AS_DEFAULT_BUT_NEVER_USE_IN_PRODUCTION_PLEASE +CHECK_HOLDER(4, unique); +CHECK_HOLDER(5, unique); +#endif +CHECK_HOLDER(6, shared); +CHECK_HOLDER(7, shared); +CHECK_HOLDER(8, shared); + +// There's no nice way to test that these fail because they fail to compile; leave them here, +// though, so that they can be manually tested by uncommenting them (and seeing that compilation +// 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, \ + "Breaks1 has wrong type!"); + +#ifdef PYBIND11_NEVER_DEFINED_EVER +// Two holder classes: +typedef py:: + class_, std::unique_ptr>, std::unique_ptr>> + Breaks1; +CHECK_BROKEN(1); +// Two aliases: +typedef py::class_, BreaksTramp<-2>, BreaksTramp<-2>> Breaks2; +CHECK_BROKEN(2); +// Holder + 2 aliases +typedef py:: + class_, std::unique_ptr>, BreaksTramp<-3>, BreaksTramp<-3>> + Breaks3; +CHECK_BROKEN(3); +// Alias + 2 holders +typedef py::class_, + std::unique_ptr>, + BreaksTramp<-4>, + std::shared_ptr>> + Breaks4; +CHECK_BROKEN(4); +// Invalid option (not a subclass or holder) +typedef py::class_, BreaksTramp<-4>> Breaks5; +CHECK_BROKEN(5); +// Invalid option: multiple inheritance not supported: +template <> +struct BreaksBase<-8> : BreaksBase<-6>, BreaksBase<-7> {}; +typedef py::class_, BreaksBase<-6>, BreaksBase<-7>> Breaks8; +CHECK_BROKEN(8); +#endif diff --git a/external_libraries/pybind11/tests/test_class.py b/external_libraries/pybind11/tests/test_class.py new file mode 100644 index 00000000..201c7e33 --- /dev/null +++ b/external_libraries/pybind11/tests/test_class.py @@ -0,0 +1,575 @@ +from __future__ import annotations + +import gc +import sys +from unittest import mock + +import pytest + +import env +from pybind11_tests import ConstructorStats, UserType +from pybind11_tests import class_ as m + +UINT32MAX = 2**32 - 1 + + +def refcount_immortal(ob: object) -> int: + if _is_immortal := getattr(sys, "_is_immortal", None): + return UINT32MAX if _is_immortal(ob) else sys.getrefcount(ob) + return sys.getrefcount(ob) + + +MANAGED_DICT_GET_REFERRERS_SUPPORTED = ( + env.CPYTHON + and sys.version_info >= (3, 13, 13) + and (sys.version_info < (3, 14) or sys.version_info >= (3, 14, 4)) +) + + +def test_obj_class_name(): + expected_name = "UserType" if env.PYPY else "pybind11_tests.UserType" + assert m.obj_class_name(UserType(1)) == expected_name + assert m.obj_class_name(UserType) == expected_name + + +def test_repr(): + assert "pybind11_type" in repr(type(UserType)) + assert "UserType" in repr(UserType) + + +def test_instance(msg): + with pytest.raises(TypeError) as excinfo: + m.NoConstructor() + assert msg(excinfo.value) == "m.class_.NoConstructor: No constructor defined!" + + instance = m.NoConstructor.new_instance() + + if env.GRAALPY: + pytest.skip("ConstructorStats is incompatible with GraalPy.") + + cstats = ConstructorStats.get(m.NoConstructor) + assert cstats.alive() == 1 + del instance + assert cstats.alive() == 0 + + +@pytest.mark.skipif( + not MANAGED_DICT_GET_REFERRERS_SUPPORTED, + reason="Requires CPython 3.13.13+ or 3.14.4+ managed dict traversal support", +) +def test_get_referrers(): + instance = m.DynamicAttr() + instance.a = "test" + assert instance in gc.get_referrers(instance.__dict__) + + +def test_instance_new(): + instance = m.NoConstructorNew() # .__new__(m.NoConstructor.__class__) + + if env.GRAALPY: + pytest.skip("ConstructorStats is incompatible with GraalPy.") + + cstats = ConstructorStats.get(m.NoConstructorNew) + assert cstats.alive() == 1 + del instance + assert cstats.alive() == 0 + + +def test_pass_unique_ptr(): + obj = m.ToBeHeldByUniquePtr() + with pytest.raises(RuntimeError) as execinfo: + m.pass_unique_ptr(obj) + assert str(execinfo.value).startswith( + "Passing `std::unique_ptr` from Python to C++ requires `py::class_` (with T = " + ) + assert "ToBeHeldByUniquePtr" in str(execinfo.value) + + +def test_type(): + assert m.check_type(1) == m.DerivedClass1 + with pytest.raises(RuntimeError) as execinfo: + m.check_type(0) + + assert "pybind11::detail::get_type_info: unable to find type info" in str( + execinfo.value + ) + assert "Invalid" in str(execinfo.value) + + # Currently not supported + # See https://github.com/pybind/pybind11/issues/2486 + # assert m.check_type(2) == int + + +def test_type_of_py(): + assert m.get_type_of(1) == int + assert m.get_type_of(m.DerivedClass1()) == m.DerivedClass1 + assert m.get_type_of(int) == type + + +def test_type_of_classic(): + assert m.get_type_classic(1) == int + assert m.get_type_classic(m.DerivedClass1()) == m.DerivedClass1 + assert m.get_type_classic(int) == type + + +def test_type_of_py_nodelete(): + # If the above test deleted the class, this will segfault + assert m.get_type_of(m.DerivedClass1()) == m.DerivedClass1 + + +def test_as_type_py(): + assert m.as_type(int) == int + + with pytest.raises(TypeError): + assert m.as_type(1) == int + + with pytest.raises(TypeError): + assert m.as_type(m.DerivedClass1()) == m.DerivedClass1 + + +def test_docstrings(doc): + assert doc(UserType) == "A `py::class_` type for testing" + assert UserType.__name__ == "UserType" + assert UserType.__module__ == "pybind11_tests" + assert UserType.get_value.__name__ == "get_value" + assert UserType.get_value.__module__ == "pybind11_tests" + + assert ( + doc(UserType.get_value) + == """ + get_value(self: m.UserType) -> int + + Get value using a method + """ + ) + assert doc(UserType.value) == "Get/set value using a property" + + assert ( + doc(m.NoConstructor.new_instance) + == """ + new_instance() -> m.class_.NoConstructor + + Return an instance + """ + ) + + +def test_qualname(doc): + """Tests that a properly qualified name is set in __qualname__ and that + generated docstrings properly use it and the module name""" + assert m.NestBase.__qualname__ == "NestBase" + assert m.NestBase.Nested.__qualname__ == "NestBase.Nested" + + assert ( + doc(m.NestBase.__init__) + == """ + __init__(self: m.class_.NestBase) -> None + """ + ) + assert ( + doc(m.NestBase.g) + == """ + g(self: m.class_.NestBase, arg0: m.class_.NestBase.Nested) -> None + """ + ) + assert ( + doc(m.NestBase.Nested.__init__) + == """ + __init__(self: m.class_.NestBase.Nested) -> None + """ + ) + assert ( + doc(m.NestBase.Nested.fn) + == """ + fn(self: m.class_.NestBase.Nested, arg0: typing.SupportsInt | typing.SupportsIndex, arg1: m.class_.NestBase, arg2: m.class_.NestBase.Nested) -> None + """ + ) + assert ( + doc(m.NestBase.Nested.fa) + == """ + fa(self: m.class_.NestBase.Nested, a: typing.SupportsInt | typing.SupportsIndex, b: m.class_.NestBase, c: m.class_.NestBase.Nested) -> None + """ + ) + assert m.NestBase.__module__ == "pybind11_tests.class_" + assert m.NestBase.Nested.__module__ == "pybind11_tests.class_" + + +def test_inheritance(msg): + roger = m.Rabbit("Rabbit") + assert roger.name() + " is a " + roger.species() == "Rabbit is a parrot" + assert m.pet_name_species(roger) == "Rabbit is a parrot" + + polly = m.Pet("Polly", "parrot") + assert polly.name() + " is a " + polly.species() == "Polly is a parrot" + assert m.pet_name_species(polly) == "Polly is a parrot" + + molly = m.Dog("Molly") + assert molly.name() + " is a " + molly.species() == "Molly is a dog" + assert m.pet_name_species(molly) == "Molly is a dog" + + fred = m.Hamster("Fred") + assert fred.name() + " is a " + fred.species() == "Fred is a rodent" + + assert m.dog_bark(molly) == "Woof!" + + with pytest.raises(TypeError) as excinfo: + m.dog_bark(polly) + assert ( + msg(excinfo.value) + == """ + dog_bark(): incompatible function arguments. The following argument types are supported: + 1. (arg0: m.class_.Dog) -> str + + Invoked with: + """ + ) + + with pytest.raises(TypeError) as excinfo: + m.Chimera("lion", "goat") + assert "No constructor defined!" in str(excinfo.value) + + +def test_inheritance_init(msg): + # Single base + class Python(m.Pet): + def __init__(self): + pass + + with pytest.raises(TypeError) as exc_info: + Python() + expected = "m.class_.Pet.__init__() must be called when overriding __init__" + assert msg(exc_info.value) == expected + + # Multiple bases + class RabbitHamster(m.Rabbit, m.Hamster): + def __init__(self): + m.Rabbit.__init__(self, "RabbitHamster") + + with pytest.raises(TypeError) as exc_info: + RabbitHamster() + expected = "m.class_.Hamster.__init__() must be called when overriding __init__" + assert msg(exc_info.value) == expected + + +@pytest.mark.parametrize( + "mock_return_value", [None, (1, 2, 3), m.Pet("Polly", "parrot"), m.Dog("Molly")] +) +def test_mock_new(mock_return_value): + with mock.patch.object( + m.Pet, "__new__", return_value=mock_return_value + ) as mock_new: + obj = m.Pet("Noname", "Nospecies") + assert obj is mock_return_value + mock_new.assert_called_once_with(m.Pet, "Noname", "Nospecies") + + +def test_automatic_upcasting(): + assert type(m.return_class_1()).__name__ == "DerivedClass1" + assert type(m.return_class_2()).__name__ == "DerivedClass2" + assert type(m.return_none()).__name__ == "NoneType" + # Repeat these a few times in a random order to ensure no invalid caching is applied + assert type(m.return_class_n(1)).__name__ == "DerivedClass1" + assert type(m.return_class_n(2)).__name__ == "DerivedClass2" + assert type(m.return_class_n(0)).__name__ == "BaseClass" + assert type(m.return_class_n(2)).__name__ == "DerivedClass2" + assert type(m.return_class_n(2)).__name__ == "DerivedClass2" + assert type(m.return_class_n(0)).__name__ == "BaseClass" + assert type(m.return_class_n(1)).__name__ == "DerivedClass1" + + +def test_isinstance(): + objects = [(), {}, m.Pet("Polly", "parrot")] + [m.Dog("Molly")] * 4 + expected = (True, True, True, True, True, False, False) + assert m.check_instances(objects) == expected + + +def test_mismatched_holder(): + import re + + with pytest.raises(RuntimeError) as excinfo: + m.mismatched_holder_1() + assert re.match( + 'generic_type: type ".*MismatchDerived1" does not have a non-default ' + 'holder type while its base ".*MismatchBase1" does', + str(excinfo.value), + ) + + with pytest.raises(RuntimeError) as excinfo: + m.mismatched_holder_2() + assert re.match( + 'generic_type: type ".*MismatchDerived2" has a non-default holder type ' + 'while its base ".*MismatchBase2" does not', + str(excinfo.value), + ) + + +def test_override_static(): + """#511: problem with inheritance + overwritten def_static""" + b = m.MyBase.make() + d1 = m.MyDerived.make2() + d2 = m.MyDerived.make() + + assert isinstance(b, m.MyBase) + assert isinstance(d1, m.MyDerived) + assert isinstance(d2, m.MyDerived) + + +def test_implicit_conversion_life_support(): + """Ensure the lifetime of temporary objects created for implicit conversions""" + assert m.implicitly_convert_argument(UserType(5)) == 5 + assert m.implicitly_convert_variable(UserType(5)) == 5 + + assert "outside a bound function" in m.implicitly_convert_variable_fail(UserType(5)) + + +def test_operator_new_delete(capture): + """Tests that class-specific operator new/delete functions are invoked""" + + class SubAliased(m.AliasedHasOpNewDelSize): + pass + + with capture: + a = m.HasOpNewDel() + b = m.HasOpNewDelSize() + d = m.HasOpNewDelBoth() + assert ( + capture + == """ + A new 8 + B new 4 + D new 32 + """ + ) + sz_alias = str(m.AliasedHasOpNewDelSize.size_alias) + sz_noalias = str(m.AliasedHasOpNewDelSize.size_noalias) + with capture: + c = m.AliasedHasOpNewDelSize() + c2 = SubAliased() + assert capture == ("C new " + sz_noalias + "\n" + "C new " + sz_alias + "\n") + + with capture: + del a + pytest.gc_collect() + del b + pytest.gc_collect() + del d + pytest.gc_collect() + assert ( + capture + == """ + A delete + B delete 4 + D delete + """ + ) + + with capture: + del c + pytest.gc_collect() + del c2 + pytest.gc_collect() + assert capture == ("C delete " + sz_noalias + "\n" + "C delete " + sz_alias + "\n") + + +def test_bind_protected_functions(): + """Expose protected member functions to Python using a helper class""" + a = m.ProtectedA() + assert a.foo() == 42 + + b = m.ProtectedB() + assert b.foo() == 42 + assert m.read_foo(b.void_foo()) == 42 + assert m.pointers_equal(b.get_self(), b) + + class C(m.ProtectedB): + def __init__(self): + m.ProtectedB.__init__(self) + + def foo(self): + return 0 + + c = C() + assert c.foo() == 0 + + +def test_brace_initialization(): + """Tests that simple POD classes can be constructed using C++11 brace initialization""" + a = m.BraceInitialization(123, "test") + assert a.field1 == 123 + assert a.field2 == "test" + + # Tests that a non-simple class doesn't get brace initialization (if the + # class defines an initializer_list constructor, in particular, it would + # win over the expected constructor). + b = m.NoBraceInitialization([123, 456]) + assert b.vec == [123, 456] + + +@pytest.mark.xfail("env.PYPY or env.GRAALPY") +def test_class_refcount(): + """Instances must correctly increase/decrease the reference count of their types (#1029)""" + + class PyDog(m.Dog): + pass + + for cls in m.Dog, PyDog: + refcount_1 = refcount_immortal(cls) + molly = [cls("Molly") for _ in range(10)] + refcount_2 = refcount_immortal(cls) + + del molly + pytest.gc_collect() + refcount_3 = refcount_immortal(cls) + + # Python may report a large value here (above 30 bits), that's also fine + assert refcount_1 == refcount_3 + assert (refcount_2 > refcount_1) or ( + refcount_2 == refcount_1 and refcount_1 >= 2**29 + ) + + +def test_reentrant_implicit_conversion_failure(msg): + # ensure that there is no runaway reentrant implicit conversion (#1035) + with pytest.raises(TypeError) as excinfo: + m.BogusImplicitConversion(0) + assert ( + msg(excinfo.value) + == """ + __init__(): incompatible constructor arguments. The following argument types are supported: + 1. m.class_.BogusImplicitConversion(arg0: m.class_.BogusImplicitConversion) + + Invoked with: 0 + """ + ) + + +def test_error_after_conversions(): + with pytest.raises(TypeError) as exc_info: + m.test_error_after_conversions("hello") + assert str(exc_info.value).startswith( + "Unable to convert function return value to a Python type!" + ) + + +def test_aligned(): + if hasattr(m, "Aligned"): + p = m.Aligned().ptr() + assert p % 1024 == 0 + + +# https://foss.heptapod.net/pypy/pypy/-/issues/2742 +@pytest.mark.xfail("env.PYPY") +def test_final(): + with pytest.raises(TypeError) as exc_info: + + class PyFinalChild(m.IsFinal): + pass + + assert str(exc_info.value).endswith("is not an acceptable base type") + + +# https://foss.heptapod.net/pypy/pypy/-/issues/2742 +@pytest.mark.xfail("env.PYPY") +def test_non_final_final(): + with pytest.raises(TypeError) as exc_info: + + class PyNonFinalFinalChild(m.IsNonFinalFinal): + pass + + assert str(exc_info.value).endswith("is not an acceptable base type") + + +# https://github.com/pybind/pybind11/issues/1878 +def test_exception_rvalue_abort(): + with pytest.raises(RuntimeError): + m.PyPrintDestructor().throw_something() + + +# https://github.com/pybind/pybind11/issues/1568 +def test_multiple_instances_with_same_pointer(): + n = 100 + instances = [m.SamePointer() for _ in range(n)] + for i in range(n): + # We need to reuse the same allocated memory for with a different type, + # to ensure the bug in `deregister_instance_impl` is detected. Otherwise + # `Py_TYPE(self) == Py_TYPE(it->second)` will still succeed, even though + # the `instance` is already deleted. + instances[i] = m.Empty() + # No assert: if this does not trigger the error + # pybind11_fail("pybind11_object_dealloc(): Tried to deallocate unregistered instance!"); + # and just completes without crashing, we're good. + + +# https://github.com/pybind/pybind11/issues/1624 +def test_base_and_derived_nested_scope(): + assert issubclass(m.DerivedWithNested, m.BaseWithNested) + assert m.BaseWithNested.Nested != m.DerivedWithNested.Nested + assert m.BaseWithNested.Nested.get_name() == "BaseWithNested::Nested" + assert m.DerivedWithNested.Nested.get_name() == "DerivedWithNested::Nested" + + +def test_register_duplicate_class(): + import types + + module_scope = types.ModuleType("module_scope") + with pytest.raises(RuntimeError) as exc_info: + m.register_duplicate_class_name(module_scope) + expected = ( + 'generic_type: cannot initialize type "Duplicate": ' + "an object with that name is already defined" + ) + assert str(exc_info.value) == expected + with pytest.raises(RuntimeError) as exc_info: + m.register_duplicate_class_type(module_scope) + expected = 'generic_type: type "YetAnotherDuplicate" is already registered!' + assert str(exc_info.value) == expected + + class ClassScope: + pass + + with pytest.raises(RuntimeError) as exc_info: + m.register_duplicate_nested_class_name(ClassScope) + expected = ( + 'generic_type: cannot initialize type "DuplicateNested": ' + "an object with that name is already defined" + ) + assert str(exc_info.value) == expected + with pytest.raises(RuntimeError) as exc_info: + m.register_duplicate_nested_class_type(ClassScope) + expected = 'generic_type: type "YetAnotherDuplicateNested" is already registered!' + assert str(exc_info.value) == expected + + +def test_pr4220_tripped_over_this(): + assert ( + m.Empty0().get_msg() + == "This is really only meant to exercise successful compilation." + ) + + +@pytest.mark.skipif(sys.platform.startswith("emscripten"), reason="Requires threads") +def test_all_type_info_multithreaded(): + # See PR #5419 for background. + import threading + + from pybind11_tests import TestContext + + class Context(TestContext): + pass + + num_runs = 10 + num_threads = 4 + barrier = threading.Barrier(num_threads) + + def func(): + barrier.wait() + with Context(): + pass + + for _ in range(num_runs): + threads = [threading.Thread(target=func) for _ in range(num_threads)] + for thread in threads: + thread.start() + + for thread in threads: + thread.join() diff --git a/external_libraries/pybind11/tests/test_class_cross_module_use_after_one_module_dealloc.cpp b/external_libraries/pybind11/tests/test_class_cross_module_use_after_one_module_dealloc.cpp new file mode 100644 index 00000000..6f6e62de --- /dev/null +++ b/external_libraries/pybind11/tests/test_class_cross_module_use_after_one_module_dealloc.cpp @@ -0,0 +1,23 @@ +#include "pybind11_tests.h" + +#include + +class CrossDSOClass { +public: + CrossDSOClass() = default; + virtual ~CrossDSOClass(); + CrossDSOClass(const CrossDSOClass &) = default; +}; + +CrossDSOClass::~CrossDSOClass() = default; + +struct UnrelatedClass {}; + +TEST_SUBMODULE(class_cross_module_use_after_one_module_dealloc, m) { + m.def("register_and_instantiate_cross_dso_class", [](const py::module_ &m) { + py::class_(m, "CrossDSOClass").def(py::init<>()); + return CrossDSOClass(); + }); + m.def("register_unrelated_class", + [](const py::module_ &m) { py::class_(m, "UnrelatedClass"); }); +} diff --git a/external_libraries/pybind11/tests/test_class_cross_module_use_after_one_module_dealloc.py b/external_libraries/pybind11/tests/test_class_cross_module_use_after_one_module_dealloc.py new file mode 100644 index 00000000..58c8f72b --- /dev/null +++ b/external_libraries/pybind11/tests/test_class_cross_module_use_after_one_module_dealloc.py @@ -0,0 +1,43 @@ +from __future__ import annotations + +import types + +import pytest + +import env +from pybind11_tests import class_cross_module_use_after_one_module_dealloc as m + + +@pytest.mark.skipif( + env.TYPES_ARE_IMMORTAL, reason="can't GC type objects on this platform" +) +def test_cross_module_use_after_one_module_dealloc(): + # This is a regression test for a bug that occurred during development of + # internals::registered_types_cpp_fast (see #5842). registered_types_cpp_fast maps + # &typeid(T) to a raw non-owning pointer to a Python type object. If two DSOs both + # look up the same global type, they will create two separate entries in + # registered_types_cpp_fast, which will look like: + # +=========================================+ + # |&typeid(T) from DSO 1|type object pointer| + # |&typeid(T) from DSO 2|type object pointer| + # +=========================================+ + # + # Then, if the type object is destroyed and we don't take extra steps to clean up + # the table thoroughly, the first row of the table will be cleaned up but the second + # one will contain a dangling pointer to the old type object. Further lookups from + # DSO 2 will then return that dangling pointer, which will cause use-after-frees. + + import pybind11_cross_module_tests as cm + + module_scope = types.ModuleType("module_scope") + instance = m.register_and_instantiate_cross_dso_class(module_scope) + cm.consume_cross_dso_class(instance) + + del instance + pytest.delattr_and_ensure_destroyed((module_scope, "CrossDSOClass")) + + # Make sure that CrossDSOClass gets allocated at a different address. + m.register_unrelated_class(module_scope) + + instance = m.register_and_instantiate_cross_dso_class(module_scope) + cm.consume_cross_dso_class(instance) diff --git a/external_libraries/pybind11/tests/test_class_release_gil_before_calling_cpp_dtor.cpp b/external_libraries/pybind11/tests/test_class_release_gil_before_calling_cpp_dtor.cpp new file mode 100644 index 00000000..7349c416 --- /dev/null +++ b/external_libraries/pybind11/tests/test_class_release_gil_before_calling_cpp_dtor.cpp @@ -0,0 +1,54 @@ +#include + +#include "pybind11_tests.h" + +#include +#include + +namespace pybind11_tests { +namespace class_release_gil_before_calling_cpp_dtor { + +using RegistryType = std::unordered_map; + +static RegistryType &PyGILState_Check_Results() { + static RegistryType singleton; // Local static variables have thread-safe initialization. + return singleton; +} + +template // Using int as a trick to easily generate a series of types. +struct ProbeType { +private: + std::string unique_key; + +public: + explicit ProbeType(const std::string &unique_key) : unique_key{unique_key} {} + ProbeType(const ProbeType &) = default; + + ~ProbeType() { + RegistryType ® = PyGILState_Check_Results(); + assert(reg.count(unique_key) == 0); + reg[unique_key] = PyGILState_Check(); + } +}; + +} // namespace class_release_gil_before_calling_cpp_dtor +} // namespace pybind11_tests + +TEST_SUBMODULE(class_release_gil_before_calling_cpp_dtor, m) { + using namespace pybind11_tests::class_release_gil_before_calling_cpp_dtor; + + py::class_>(m, "ProbeType0").def(py::init()); + + py::class_>(m, "ProbeType1", py::release_gil_before_calling_cpp_dtor()) + .def(py::init()); + + m.def("PopPyGILState_Check_Result", [](const std::string &unique_key) -> std::string { + RegistryType ® = PyGILState_Check_Results(); + if (reg.count(unique_key) == 0) { + return "MISSING"; + } + int res = reg[unique_key]; + reg.erase(unique_key); + return std::to_string(res); + }); +} diff --git a/external_libraries/pybind11/tests/test_class_release_gil_before_calling_cpp_dtor.py b/external_libraries/pybind11/tests/test_class_release_gil_before_calling_cpp_dtor.py new file mode 100644 index 00000000..0b1f246b --- /dev/null +++ b/external_libraries/pybind11/tests/test_class_release_gil_before_calling_cpp_dtor.py @@ -0,0 +1,21 @@ +from __future__ import annotations + +import gc + +import pytest + +from pybind11_tests import class_release_gil_before_calling_cpp_dtor as m + + +@pytest.mark.parametrize( + ("probe_type", "unique_key", "expected_result"), + [ + (m.ProbeType0, "without_manipulating_gil", "1"), + (m.ProbeType1, "release_gil_before_calling_cpp_dtor", "0"), + ], +) +def test_gil_state_check_results(probe_type, unique_key, expected_result): + probe_type(unique_key) + gc.collect() + result = m.PopPyGILState_Check_Result(unique_key) + assert result == expected_result diff --git a/external_libraries/pybind11/tests/test_class_sh_basic.cpp b/external_libraries/pybind11/tests/test_class_sh_basic.cpp new file mode 100644 index 00000000..d9cbe1f6 --- /dev/null +++ b/external_libraries/pybind11/tests/test_class_sh_basic.cpp @@ -0,0 +1,248 @@ +#include "pybind11_tests.h" + +#include +#include +#include + +namespace pybind11_tests { +namespace class_sh_basic { + +struct atyp { // Short for "any type". + std::string mtxt; + atyp() : mtxt("DefaultConstructor") {} + explicit atyp(const std::string &mtxt_) : mtxt(mtxt_) {} + atyp(const atyp &other) { mtxt = other.mtxt + "_CpCtor"; } + atyp(atyp &&other) noexcept { mtxt = other.mtxt + "_MvCtor"; } +}; + +struct uconsumer { // unique_ptr consumer + std::unique_ptr held; + bool valid() const { return static_cast(held); } + + void pass_valu(std::unique_ptr obj) { held = std::move(obj); } + void pass_rref(std::unique_ptr &&obj) { held = std::move(obj); } + std::unique_ptr rtrn_valu() { return std::move(held); } + std::unique_ptr &rtrn_lref() { return held; } + const std::unique_ptr &rtrn_cref() const { return held; } +}; + +/// Custom deleter that is default constructible. +struct custom_deleter { + std::string trace_txt; + + custom_deleter() = default; + explicit custom_deleter(const std::string &trace_txt_) : trace_txt(trace_txt_) {} + + custom_deleter(const custom_deleter &other) { trace_txt = other.trace_txt + "_CpCtor"; } + + custom_deleter &operator=(const custom_deleter &rhs) { + trace_txt = rhs.trace_txt + "_CpLhs"; + return *this; + } + + custom_deleter(custom_deleter &&other) noexcept { + trace_txt = other.trace_txt + "_MvCtorTo"; + other.trace_txt += "_MvCtorFrom"; + } + + custom_deleter &operator=(custom_deleter &&rhs) noexcept { + trace_txt = rhs.trace_txt + "_MvLhs"; + rhs.trace_txt += "_MvRhs"; + return *this; + } + + void operator()(atyp *p) const { std::default_delete()(p); } + void operator()(const atyp *p) const { std::default_delete()(p); } +}; +static_assert(std::is_default_constructible::value, ""); + +/// Custom deleter that is not default constructible. +struct custom_deleter_nd : custom_deleter { + custom_deleter_nd() = delete; + explicit custom_deleter_nd(const std::string &trace_txt_) : custom_deleter(trace_txt_) {} +}; +static_assert(!std::is_default_constructible::value, ""); + +// clang-format off + +atyp rtrn_valu() { atyp obj{"rtrn_valu"}; return obj; } +atyp&& rtrn_rref() { static atyp obj; obj.mtxt = "rtrn_rref"; return std::move(obj); } +atyp const& rtrn_cref() { static atyp obj; obj.mtxt = "rtrn_cref"; return obj; } +atyp& rtrn_mref() { static atyp obj; obj.mtxt = "rtrn_mref"; return obj; } +atyp const* rtrn_cptr() { return new atyp{"rtrn_cptr"}; } +atyp* rtrn_mptr() { return new atyp{"rtrn_mptr"}; } + +std::string pass_valu(atyp obj) { return "pass_valu:" + obj.mtxt; } // NOLINT +std::string pass_cref(atyp const& obj) { return "pass_cref:" + obj.mtxt; } +std::string pass_mref(atyp& obj) { return "pass_mref:" + obj.mtxt; } +std::string pass_cptr(atyp const* obj) { return "pass_cptr:" + obj->mtxt; } +std::string pass_mptr(atyp* obj) { return "pass_mptr:" + obj->mtxt; } + +std::shared_ptr rtrn_shmp() { return std::make_shared("rtrn_shmp"); } +std::shared_ptr rtrn_shcp() { return std::shared_ptr(new atyp{"rtrn_shcp"}); } + +std::string pass_shmp(std::shared_ptr obj) { return "pass_shmp:" + obj->mtxt; } // NOLINT +std::string pass_shcp(std::shared_ptr obj) { return "pass_shcp:" + obj->mtxt; } // NOLINT + +std::unique_ptr rtrn_uqmp() { return std::unique_ptr(new atyp{"rtrn_uqmp"}); } +std::unique_ptr rtrn_uqcp() { return std::unique_ptr(new atyp{"rtrn_uqcp"}); } + +std::string pass_uqmp(std::unique_ptr obj) { return "pass_uqmp:" + obj->mtxt; } +std::string pass_uqcp(std::unique_ptr obj) { return "pass_uqcp:" + obj->mtxt; } + +struct sddm : std::default_delete {}; +struct sddc : std::default_delete {}; + +std::unique_ptr rtrn_udmp() { return std::unique_ptr(new atyp{"rtrn_udmp"}); } +std::unique_ptr rtrn_udcp() { return std::unique_ptr(new atyp{"rtrn_udcp"}); } + +std::string pass_udmp(std::unique_ptr obj) { return "pass_udmp:" + obj->mtxt; } +std::string pass_udcp(std::unique_ptr obj) { return "pass_udcp:" + obj->mtxt; } + +std::unique_ptr rtrn_udmp_del() { return std::unique_ptr(new atyp{"rtrn_udmp_del"}, custom_deleter{"udmp_deleter"}); } +std::unique_ptr rtrn_udcp_del() { return std::unique_ptr(new atyp{"rtrn_udcp_del"}, custom_deleter{"udcp_deleter"}); } + +std::string pass_udmp_del(std::unique_ptr obj) { return "pass_udmp_del:" + obj->mtxt + "," + obj.get_deleter().trace_txt; } +std::string pass_udcp_del(std::unique_ptr obj) { return "pass_udcp_del:" + obj->mtxt + "," + obj.get_deleter().trace_txt; } + +std::unique_ptr rtrn_udmp_del_nd() { return std::unique_ptr(new atyp{"rtrn_udmp_del_nd"}, custom_deleter_nd{"udmp_deleter_nd"}); } +std::unique_ptr rtrn_udcp_del_nd() { return std::unique_ptr(new atyp{"rtrn_udcp_del_nd"}, custom_deleter_nd{"udcp_deleter_nd"}); } + +std::string pass_udmp_del_nd(std::unique_ptr obj) { return "pass_udmp_del_nd:" + obj->mtxt + "," + obj.get_deleter().trace_txt; } +std::string pass_udcp_del_nd(std::unique_ptr obj) { return "pass_udcp_del_nd:" + obj->mtxt + "," + obj.get_deleter().trace_txt; } + +// clang-format on + +// Helpers for testing. +std::string get_mtxt(atyp const &obj) { return obj.mtxt; } +std::ptrdiff_t get_ptr(atyp const &obj) { return reinterpret_cast(&obj); } + +std::unique_ptr unique_ptr_roundtrip(std::unique_ptr obj) { return obj; } + +std::string pass_unique_ptr_cref(const std::unique_ptr &obj) { return obj->mtxt; } + +const std::unique_ptr &rtrn_unique_ptr_cref(const std::string &mtxt) { + static std::unique_ptr obj{new atyp{"static_ctor_arg"}}; + if (!mtxt.empty()) { + obj->mtxt = mtxt; + } + return obj; +} + +const std::unique_ptr &unique_ptr_cref_roundtrip(const std::unique_ptr &obj) { + // This is passed in, so it can't be given a temporary + return obj; // NOLINT(bugprone-return-const-ref-from-parameter) +} + +struct SharedPtrStash { + std::vector> stash; + void Add(const std::shared_ptr &obj) { stash.push_back(obj); } +}; + +class LocalUnusualOpRef : UnusualOpRef {}; // To avoid clashing with `py::class_`. +py::object CastUnusualOpRefConstRef(const LocalUnusualOpRef &cref) { return py::cast(cref); } +py::object CastUnusualOpRefMovable(LocalUnusualOpRef &&mvbl) { return py::cast(std::move(mvbl)); } + +TEST_SUBMODULE(class_sh_basic, m) { + namespace py = pybind11; + + py::classh(m, "atyp").def(py::init<>()).def(py::init([](const std::string &mtxt) { + atyp obj; + obj.mtxt = mtxt; + return obj; + })); + + m.def("rtrn_valu", rtrn_valu); + m.def("rtrn_rref", rtrn_rref); + m.def("rtrn_cref", rtrn_cref); + m.def("rtrn_mref", rtrn_mref); + m.def("rtrn_cptr", rtrn_cptr); + m.def("rtrn_mptr", rtrn_mptr); + + m.def("pass_valu", pass_valu); + m.def("pass_cref", pass_cref); + m.def("pass_mref", pass_mref); + m.def("pass_cptr", pass_cptr); + m.def("pass_mptr", pass_mptr); + + m.def("rtrn_shmp", rtrn_shmp); + m.def("rtrn_shcp", rtrn_shcp); + + m.def("pass_shmp", pass_shmp); + m.def("pass_shcp", pass_shcp); + + m.def("rtrn_uqmp", rtrn_uqmp); + m.def("rtrn_uqcp", rtrn_uqcp); + + m.def("pass_uqmp", pass_uqmp); + m.def("pass_uqcp", pass_uqcp); + + m.def("rtrn_udmp", rtrn_udmp); + m.def("rtrn_udcp", rtrn_udcp); + + m.def("pass_udmp", pass_udmp); + m.def("pass_udcp", pass_udcp); + + m.def("rtrn_udmp_del", rtrn_udmp_del); + m.def("rtrn_udcp_del", rtrn_udcp_del); + + m.def("pass_udmp_del", pass_udmp_del); + m.def("pass_udcp_del", pass_udcp_del); + + m.def("rtrn_udmp_del_nd", rtrn_udmp_del_nd); + m.def("rtrn_udcp_del_nd", rtrn_udcp_del_nd); + + m.def("pass_udmp_del_nd", pass_udmp_del_nd); + m.def("pass_udcp_del_nd", pass_udcp_del_nd); + + py::classh(m, "uconsumer") + .def(py::init<>()) + .def("valid", &uconsumer::valid) + .def("pass_valu", &uconsumer::pass_valu) + .def("pass_rref", &uconsumer::pass_rref) + .def("rtrn_valu", &uconsumer::rtrn_valu) + .def("rtrn_lref", &uconsumer::rtrn_lref) + .def("rtrn_cref", &uconsumer::rtrn_cref); + + // Helpers for testing. + // These require selected functions above to work first, as indicated: + m.def("get_mtxt", get_mtxt); // pass_cref + m.def("get_ptr", get_ptr); // pass_cref + + m.def("unique_ptr_roundtrip", unique_ptr_roundtrip); // pass_uqmp, rtrn_uqmp + + m.def("pass_unique_ptr_cref", pass_unique_ptr_cref); + m.def("rtrn_unique_ptr_cref", rtrn_unique_ptr_cref); + m.def("unique_ptr_cref_roundtrip", unique_ptr_cref_roundtrip); + + py::classh(m, "SharedPtrStash") + .def(py::init<>()) + .def("Add", &SharedPtrStash::Add, py::arg("obj")); + + m.def("py_type_handle_of_atyp", []() { + return py::type::handle_of(); // Exercises static_cast in this function. + }); + + // Checks for type names used as arguments + m.def("args_shared_ptr", [](std::shared_ptr p) { return p; }); + m.def("args_shared_ptr_const", [](std::shared_ptr p) { return p; }); + m.def("args_unique_ptr", [](std::unique_ptr p) { return p; }); + m.def("args_unique_ptr_const", [](std::unique_ptr p) { return p; }); + + // Make sure unique_ptr type caster accept automatic_reference return value policy. + m.def( + "rtrn_uq_automatic_reference", + []() { return std::unique_ptr(new atyp("rtrn_uq_automatic_reference")); }, + pybind11::return_value_policy::automatic_reference); + + m.def("pass_shared_ptr_ptr", [](std::shared_ptr *) {}); + + py::classh(m, "LocalUnusualOpRef"); + m.def("CallCastUnusualOpRefConstRef", + []() { return CastUnusualOpRefConstRef(LocalUnusualOpRef()); }); + m.def("CallCastUnusualOpRefMovable", + []() { return CastUnusualOpRefMovable(LocalUnusualOpRef()); }); +} + +} // namespace class_sh_basic +} // namespace pybind11_tests diff --git a/external_libraries/pybind11/tests/test_class_sh_basic.py b/external_libraries/pybind11/tests/test_class_sh_basic.py new file mode 100644 index 00000000..aaea87b8 --- /dev/null +++ b/external_libraries/pybind11/tests/test_class_sh_basic.py @@ -0,0 +1,248 @@ +# Importing re before pytest after observing a PyPy CI flake when importing pytest first. +from __future__ import annotations + +import re + +import pytest + +from pybind11_tests import class_sh_basic as m + + +def test_atyp_constructors(): + obj = m.atyp() + assert obj.__class__.__name__ == "atyp" + obj = m.atyp("") + assert obj.__class__.__name__ == "atyp" + obj = m.atyp("txtm") + assert obj.__class__.__name__ == "atyp" + + +@pytest.mark.parametrize( + ("rtrn_f", "expected"), + [ + (m.rtrn_valu, "rtrn_valu(_MvCtor)*_MvCtor"), + (m.rtrn_rref, "rtrn_rref(_MvCtor)*_MvCtor"), + (m.rtrn_cref, "rtrn_cref(_MvCtor)*_CpCtor"), + (m.rtrn_mref, "rtrn_mref(_MvCtor)*_CpCtor"), + (m.rtrn_cptr, "rtrn_cptr"), + (m.rtrn_mptr, "rtrn_mptr"), + (m.rtrn_shmp, "rtrn_shmp"), + (m.rtrn_shcp, "rtrn_shcp"), + (m.rtrn_uqmp, "rtrn_uqmp"), + (m.rtrn_uqcp, "rtrn_uqcp"), + (m.rtrn_udmp, "rtrn_udmp"), + (m.rtrn_udcp, "rtrn_udcp"), + ], +) +def test_cast(rtrn_f, expected): + assert re.match(expected, m.get_mtxt(rtrn_f())) + + +@pytest.mark.parametrize( + ("pass_f", "mtxt", "expected"), + [ + (m.pass_valu, "Valu", "pass_valu:Valu(_MvCtor)*_CpCtor"), + (m.pass_cref, "Cref", "pass_cref:Cref(_MvCtor)*_MvCtor"), + (m.pass_mref, "Mref", "pass_mref:Mref(_MvCtor)*_MvCtor"), + (m.pass_cptr, "Cptr", "pass_cptr:Cptr(_MvCtor)*_MvCtor"), + (m.pass_mptr, "Mptr", "pass_mptr:Mptr(_MvCtor)*_MvCtor"), + (m.pass_shmp, "Shmp", "pass_shmp:Shmp(_MvCtor)*_MvCtor"), + (m.pass_shcp, "Shcp", "pass_shcp:Shcp(_MvCtor)*_MvCtor"), + (m.pass_uqmp, "Uqmp", "pass_uqmp:Uqmp(_MvCtor)*_MvCtor"), + (m.pass_uqcp, "Uqcp", "pass_uqcp:Uqcp(_MvCtor)*_MvCtor"), + ], +) +def test_load_with_mtxt(pass_f, mtxt, expected): + assert re.match(expected, pass_f(m.atyp(mtxt))) + + +@pytest.mark.parametrize( + ("pass_f", "rtrn_f", "expected"), + [ + (m.pass_udmp, m.rtrn_udmp, "pass_udmp:rtrn_udmp"), + (m.pass_udcp, m.rtrn_udcp, "pass_udcp:rtrn_udcp"), + ], +) +def test_load_with_rtrn_f(pass_f, rtrn_f, expected): + assert pass_f(rtrn_f()) == expected + + +@pytest.mark.parametrize( + ("pass_f", "rtrn_f", "regex_expected"), + [ + ( + m.pass_udmp_del, + m.rtrn_udmp_del, + "pass_udmp_del:rtrn_udmp_del,udmp_deleter(_MvCtorTo)*_MvCtorTo", + ), + ( + m.pass_udcp_del, + m.rtrn_udcp_del, + "pass_udcp_del:rtrn_udcp_del,udcp_deleter(_MvCtorTo)*_MvCtorTo", + ), + ( + m.pass_udmp_del_nd, + m.rtrn_udmp_del_nd, + "pass_udmp_del_nd:rtrn_udmp_del_nd,udmp_deleter_nd(_MvCtorTo)*_MvCtorTo", + ), + ( + m.pass_udcp_del_nd, + m.rtrn_udcp_del_nd, + "pass_udcp_del_nd:rtrn_udcp_del_nd,udcp_deleter_nd(_MvCtorTo)*_MvCtorTo", + ), + ], +) +def test_deleter_roundtrip(pass_f, rtrn_f, regex_expected): + assert re.match(regex_expected, pass_f(rtrn_f())) + + +@pytest.mark.parametrize( + ("pass_f", "rtrn_f", "expected"), + [ + (m.pass_uqmp, m.rtrn_uqmp, "pass_uqmp:rtrn_uqmp"), + (m.pass_uqcp, m.rtrn_uqcp, "pass_uqcp:rtrn_uqcp"), + (m.pass_udmp, m.rtrn_udmp, "pass_udmp:rtrn_udmp"), + (m.pass_udcp, m.rtrn_udcp, "pass_udcp:rtrn_udcp"), + ], +) +def test_pass_unique_ptr_disowns(pass_f, rtrn_f, expected): + obj = rtrn_f() + assert pass_f(obj) == expected + with pytest.raises(ValueError) as exc_info: + pass_f(obj) + assert str(exc_info.value) == ( + "Missing value for wrapped C++ type" + " `pybind11_tests::class_sh_basic::atyp`:" + " Python instance was disowned." + ) + + +@pytest.mark.parametrize( + ("pass_f", "rtrn_f"), + [ + (m.pass_uqmp, m.rtrn_uqmp), + (m.pass_uqcp, m.rtrn_uqcp), + (m.pass_udmp, m.rtrn_udmp), + (m.pass_udcp, m.rtrn_udcp), + ], +) +def test_cannot_disown_use_count_ne_1(pass_f, rtrn_f): + obj = rtrn_f() + stash = m.SharedPtrStash() + stash.Add(obj) + with pytest.raises(ValueError) as exc_info: + pass_f(obj) + assert str(exc_info.value) == ("Cannot disown use_count != 1 (load_as_unique_ptr).") + + +def test_unique_ptr_roundtrip(): + # Multiple roundtrips to stress-test instance registration/deregistration. + num_round_trips = 1000 + recycled = m.atyp("passenger") + for _ in range(num_round_trips): + id_orig = id(recycled) + recycled = m.unique_ptr_roundtrip(recycled) + assert re.match("passenger(_MvCtor)*_MvCtor", m.get_mtxt(recycled)) + id_rtrn = id(recycled) + # Ensure the returned object is a different Python instance. + assert id_rtrn != id_orig + id_orig = id_rtrn + + +def test_pass_unique_ptr_cref(): + obj = m.atyp("ctor_arg") + assert re.match("ctor_arg(_MvCtor)*_MvCtor", m.get_mtxt(obj)) + assert re.match("ctor_arg(_MvCtor)*_MvCtor", m.pass_unique_ptr_cref(obj)) + assert re.match("ctor_arg(_MvCtor)*_MvCtor", m.get_mtxt(obj)) + + +def test_rtrn_unique_ptr_cref(): + obj0 = m.rtrn_unique_ptr_cref("") + assert m.get_mtxt(obj0) == "static_ctor_arg" + obj1 = m.rtrn_unique_ptr_cref("passed_mtxt_1") + assert m.get_mtxt(obj1) == "passed_mtxt_1" + assert m.get_mtxt(obj0) == "passed_mtxt_1" + assert obj0 is obj1 + + +def test_unique_ptr_cref_roundtrip(): + # Multiple roundtrips to stress-test implementation. + num_round_trips = 1000 + orig = m.atyp("passenger") + mtxt_orig = m.get_mtxt(orig) + recycled = orig + for _ in range(num_round_trips): + recycled = m.unique_ptr_cref_roundtrip(recycled) + assert recycled is orig + assert m.get_mtxt(recycled) == mtxt_orig + + +@pytest.mark.parametrize( + ("pass_f", "rtrn_f", "moved_out", "moved_in"), + [ + (m.uconsumer.pass_valu, m.uconsumer.rtrn_valu, True, True), + (m.uconsumer.pass_rref, m.uconsumer.rtrn_valu, True, True), + (m.uconsumer.pass_valu, m.uconsumer.rtrn_lref, True, False), + (m.uconsumer.pass_valu, m.uconsumer.rtrn_cref, True, False), + ], +) +def test_unique_ptr_consumer_roundtrip(pass_f, rtrn_f, moved_out, moved_in): + c = m.uconsumer() + assert not c.valid() + recycled = m.atyp("passenger") + mtxt_orig = m.get_mtxt(recycled) + assert re.match("passenger_(MvCtor){1,2}", mtxt_orig) + + pass_f(c, recycled) + if moved_out: + with pytest.raises(ValueError) as excinfo: + m.get_mtxt(recycled) + assert "Python instance was disowned" in str(excinfo.value) + + recycled = rtrn_f(c) + assert c.valid() != moved_in + assert m.get_mtxt(recycled) == mtxt_orig + + +def test_py_type_handle_of_atyp(): + obj = m.py_type_handle_of_atyp() + assert obj.__class__.__name__ == "pybind11_type" + + +def test_function_signatures(doc): + assert ( + doc(m.args_shared_ptr) + == "args_shared_ptr(arg0: m.class_sh_basic.atyp) -> m.class_sh_basic.atyp" + ) + assert ( + doc(m.args_shared_ptr_const) + == "args_shared_ptr_const(arg0: m.class_sh_basic.atyp) -> m.class_sh_basic.atyp" + ) + assert ( + doc(m.args_unique_ptr) + == "args_unique_ptr(arg0: m.class_sh_basic.atyp) -> m.class_sh_basic.atyp" + ) + assert ( + doc(m.args_unique_ptr_const) + == "args_unique_ptr_const(arg0: m.class_sh_basic.atyp) -> m.class_sh_basic.atyp" + ) + + +def test_unique_ptr_return_value_policy_automatic_reference(): + assert m.get_mtxt(m.rtrn_uq_automatic_reference()) == "rtrn_uq_automatic_reference" + + +def test_pass_shared_ptr_ptr(): + obj = m.atyp() + with pytest.raises(RuntimeError) as excinfo: + m.pass_shared_ptr_ptr(obj) + assert str(excinfo.value) == ( + "Passing `std::shared_ptr *` from Python to C++ is not supported" + " (inherently unsafe)." + ) + + +def test_unusual_op_ref(): + # Merely to test that this still exists and built successfully. + assert m.CallCastUnusualOpRefConstRef().__class__.__name__ == "LocalUnusualOpRef" + assert m.CallCastUnusualOpRefMovable().__class__.__name__ == "LocalUnusualOpRef" diff --git a/external_libraries/pybind11/tests/test_class_sh_disowning.cpp b/external_libraries/pybind11/tests/test_class_sh_disowning.cpp new file mode 100644 index 00000000..490e6d59 --- /dev/null +++ b/external_libraries/pybind11/tests/test_class_sh_disowning.cpp @@ -0,0 +1,41 @@ +#include "pybind11_tests.h" + +#include + +namespace pybind11_tests { +namespace class_sh_disowning { + +template // Using int as a trick to easily generate a series of types. +struct Atype { + int val = 0; + explicit Atype(int val_) : val{val_} {} + int get() const { return val * 10 + SerNo; } +}; + +int same_twice(std::unique_ptr> at1a, std::unique_ptr> at1b) { + return at1a->get() * 100 + at1b->get() * 10; +} + +int mixed(std::unique_ptr> at1, std::unique_ptr> at2) { + return at1->get() * 200 + at2->get() * 20; +} + +int overloaded(std::unique_ptr> at1, int i) { return at1->get() * 30 + i; } +int overloaded(std::unique_ptr> at2, int i) { return at2->get() * 40 + i; } + +} // namespace class_sh_disowning +} // namespace pybind11_tests + +TEST_SUBMODULE(class_sh_disowning, m) { + using namespace pybind11_tests::class_sh_disowning; + + py::classh>(m, "Atype1").def(py::init()).def("get", &Atype<1>::get); + py::classh>(m, "Atype2").def(py::init()).def("get", &Atype<2>::get); + + m.def("same_twice", same_twice); + + m.def("mixed", mixed); + + m.def("overloaded", (int (*)(std::unique_ptr>, int)) &overloaded); + m.def("overloaded", (int (*)(std::unique_ptr>, int)) &overloaded); +} diff --git a/external_libraries/pybind11/tests/test_class_sh_disowning.py b/external_libraries/pybind11/tests/test_class_sh_disowning.py new file mode 100644 index 00000000..b9e64899 --- /dev/null +++ b/external_libraries/pybind11/tests/test_class_sh_disowning.py @@ -0,0 +1,78 @@ +from __future__ import annotations + +import pytest + +from pybind11_tests import class_sh_disowning as m + + +def is_disowned(obj): + try: + obj.get() + except ValueError: + return True + return False + + +def test_same_twice(): + while True: + obj1a = m.Atype1(57) + obj1b = m.Atype1(62) + assert m.same_twice(obj1a, obj1b) == (57 * 10 + 1) * 100 + (62 * 10 + 1) * 10 + assert is_disowned(obj1a) + assert is_disowned(obj1b) + obj1c = m.Atype1(0) + with pytest.raises(ValueError): + # Disowning works for one argument, but not both. + m.same_twice(obj1c, obj1c) + assert is_disowned(obj1c) + return # Comment out for manual leak checking (use `top` command). + + +def test_mixed(): + first_pass = True + while True: + obj1a = m.Atype1(90) + obj2a = m.Atype2(25) + assert m.mixed(obj1a, obj2a) == (90 * 10 + 1) * 200 + (25 * 10 + 2) * 20 + assert is_disowned(obj1a) + assert is_disowned(obj2a) + + # The C++ order of evaluation of function arguments is (unfortunately) unspecified: + # https://en.cppreference.com/w/cpp/language/eval_order + # Read on. + obj1b = m.Atype1(0) + with pytest.raises(ValueError): + # If the 1st argument is evaluated first, obj1b is disowned before the conversion for + # the already disowned obj2a fails as expected. + m.mixed(obj1b, obj2a) + obj2b = m.Atype2(0) + with pytest.raises(ValueError): + # If the 2nd argument is evaluated first, obj2b is disowned before the conversion for + # the already disowned obj1a fails as expected. + m.mixed(obj1a, obj2b) + + # Either obj1b or obj2b was disowned in the expected failed m.mixed() calls above, but not + # both. + is_disowned_results = (is_disowned(obj1b), is_disowned(obj2b)) + assert is_disowned_results.count(True) == 1 + if first_pass: + first_pass = False + ix = is_disowned_results.index(True) + 1 + print(f"\nC++ function argument {ix} is evaluated first.") + + return # Comment out for manual leak checking (use `top` command). + + +def test_overloaded(): + while True: + obj1 = m.Atype1(81) + obj2 = m.Atype2(60) + with pytest.raises(TypeError): + m.overloaded(obj1, "NotInt") + assert obj1.get() == 81 * 10 + 1 # Not disowned. + assert m.overloaded(obj1, 3) == (81 * 10 + 1) * 30 + 3 + with pytest.raises(TypeError): + m.overloaded(obj2, "NotInt") + assert obj2.get() == 60 * 10 + 2 # Not disowned. + assert m.overloaded(obj2, 2) == (60 * 10 + 2) * 40 + 2 + return # Comment out for manual leak checking (use `top` command). diff --git a/external_libraries/pybind11/tests/test_class_sh_disowning_mi.cpp b/external_libraries/pybind11/tests/test_class_sh_disowning_mi.cpp new file mode 100644 index 00000000..d0ffd45e --- /dev/null +++ b/external_libraries/pybind11/tests/test_class_sh_disowning_mi.cpp @@ -0,0 +1,85 @@ +#include "pybind11_tests.h" + +#include + +namespace pybind11_tests { +namespace class_sh_disowning_mi { + +// Diamond inheritance (copied from test_multiple_inheritance.cpp). +struct B { + int val_b = 10; + B() = default; + B(const B &) = default; + virtual ~B() = default; +}; + +struct C0 : public virtual B { + int val_c0 = 20; +}; + +struct C1 : public virtual B { + int val_c1 = 21; +}; + +struct D : public C0, public C1 { + int val_d = 30; +}; + +void disown_b(std::unique_ptr) {} + +// test_multiple_inheritance_python +struct Base1 { + explicit Base1(int i) : i(i) {} + int foo() const { return i; } + int i; +}; + +struct Base2 { + explicit Base2(int j) : j(j) {} + int bar() const { return j; } + int j; +}; + +int disown_base1(std::unique_ptr b1) { return b1->i * 2000 + 1; } +int disown_base2(std::unique_ptr b2) { return b2->j * 2000 + 2; } + +} // namespace class_sh_disowning_mi +} // namespace pybind11_tests + +TEST_SUBMODULE(class_sh_disowning_mi, m) { + using namespace pybind11_tests::class_sh_disowning_mi; + + py::classh(m, "B") + .def(py::init<>()) + .def_readonly("val_b", &D::val_b) + .def("b", [](B *self) { return self; }) + .def("get", [](const B &self) { return self.val_b; }); + + py::classh(m, "C0") + .def(py::init<>()) + .def_readonly("val_c0", &D::val_c0) + .def("c0", [](C0 *self) { return self; }) + .def("get", [](const C0 &self) { return self.val_b * 100 + self.val_c0; }); + + py::classh(m, "C1") + .def(py::init<>()) + .def_readonly("val_c1", &D::val_c1) + .def("c1", [](C1 *self) { return self; }) + .def("get", [](const C1 &self) { return self.val_b * 100 + self.val_c1; }); + + py::classh(m, "D") + .def(py::init<>()) + .def_readonly("val_d", &D::val_d) + .def("d", [](D *self) { return self; }) + .def("get", [](const D &self) { + return self.val_b * 1000000 + self.val_c0 * 10000 + self.val_c1 * 100 + self.val_d; + }); + + m.def("disown_b", disown_b); + + // test_multiple_inheritance_python + py::classh(m, "Base1").def(py::init()).def("foo", &Base1::foo); + py::classh(m, "Base2").def(py::init()).def("bar", &Base2::bar); + m.def("disown_base1", disown_base1); + m.def("disown_base2", disown_base2); +} diff --git a/external_libraries/pybind11/tests/test_class_sh_disowning_mi.py b/external_libraries/pybind11/tests/test_class_sh_disowning_mi.py new file mode 100644 index 00000000..4a4beecc --- /dev/null +++ b/external_libraries/pybind11/tests/test_class_sh_disowning_mi.py @@ -0,0 +1,246 @@ +from __future__ import annotations + +import pytest + +import env # noqa: F401 +from pybind11_tests import class_sh_disowning_mi as m + + +def test_diamond_inheritance(): + # Very similar to test_multiple_inheritance.py:test_diamond_inheritance. + d = m.D() + assert d is d.d() + assert d is d.c0() + assert d is d.c1() + assert d is d.b() + assert d is d.c0().b() + assert d is d.c1().b() + assert d is d.c0().c1().b().c0().b() + + +def is_disowned(callable_method): + try: + callable_method() + except ValueError as e: + assert "Python instance was disowned" in str(e) # noqa: PT017 + return True + return False + + +def test_disown_b(): + b = m.B() + assert b.get() == 10 + m.disown_b(b) + assert is_disowned(b.get) + + +@pytest.mark.parametrize("var_to_disown", ["c0", "b"]) +def test_disown_c0(var_to_disown): + c0 = m.C0() + assert c0.get() == 1020 + b = c0.b() + m.disown_b(locals()[var_to_disown]) + assert is_disowned(c0.get) + assert is_disowned(b.get) + + +@pytest.mark.parametrize("var_to_disown", ["c1", "b"]) +def test_disown_c1(var_to_disown): + c1 = m.C1() + assert c1.get() == 1021 + b = c1.b() + m.disown_b(locals()[var_to_disown]) + assert is_disowned(c1.get) + assert is_disowned(b.get) + + +@pytest.mark.parametrize("var_to_disown", ["d", "c1", "c0", "b"]) +def test_disown_d(var_to_disown): + d = m.D() + assert d.get() == 10202130 + b = d.b() + c0 = d.c0() + c1 = d.c1() + m.disown_b(locals()[var_to_disown]) + assert is_disowned(d.get) + assert is_disowned(c1.get) + assert is_disowned(c0.get) + assert is_disowned(b.get) + + +# Based on test_multiple_inheritance.py:test_multiple_inheritance_python. +class MI1(m.Base1, m.Base2): + def __init__(self, i, j): + m.Base1.__init__(self, i) + m.Base2.__init__(self, j) + + +class B1: + def v(self): + return 1 + + +class MI2(B1, m.Base1, m.Base2): + def __init__(self, i, j): + B1.__init__(self) + m.Base1.__init__(self, i) + m.Base2.__init__(self, j) + + +class MI3(MI2): + def __init__(self, i, j): + MI2.__init__(self, i, j) + + +class MI4(MI3, m.Base2): + def __init__(self, i, j): + MI3.__init__(self, i, j) + # This should be ignored (Base2 is already initialized via MI2): + m.Base2.__init__(self, i + 100) + + +class MI5(m.Base2, B1, m.Base1): + def __init__(self, i, j): + B1.__init__(self) + m.Base1.__init__(self, i) + m.Base2.__init__(self, j) + + +class MI6(m.Base2, B1): + def __init__(self, i): + m.Base2.__init__(self, i) + B1.__init__(self) + + +class B2(B1): + def v(self): + return 2 + + +class B3: + def v(self): + return 3 + + +class B4(B3, B2): + def v(self): + return 4 + + +class MI7(B4, MI6): + def __init__(self, i): + B4.__init__(self) + MI6.__init__(self, i) + + +class MI8(MI6, B3): + def __init__(self, i): + MI6.__init__(self, i) + B3.__init__(self) + + +class MI8b(B3, MI6): + def __init__(self, i): + B3.__init__(self) + MI6.__init__(self, i) + + +@pytest.mark.xfail("env.PYPY") +def test_multiple_inheritance_python(): + # Based on test_multiple_inheritance.py:test_multiple_inheritance_python. + # Exercises values_and_holders with 2 value_and_holder instances. + + mi1 = MI1(1, 2) + assert mi1.foo() == 1 + assert mi1.bar() == 2 + + mi2 = MI2(3, 4) + assert mi2.v() == 1 + assert mi2.foo() == 3 + assert mi2.bar() == 4 + + mi3 = MI3(5, 6) + assert mi3.v() == 1 + assert mi3.foo() == 5 + assert mi3.bar() == 6 + + mi4 = MI4(7, 8) + assert mi4.v() == 1 + assert mi4.foo() == 7 + assert mi4.bar() == 8 + + mi5 = MI5(10, 11) + assert mi5.v() == 1 + assert mi5.foo() == 10 + assert mi5.bar() == 11 + + mi6 = MI6(12) + assert mi6.v() == 1 + assert mi6.bar() == 12 + + mi7 = MI7(13) + assert mi7.v() == 4 + assert mi7.bar() == 13 + + mi8 = MI8(14) + assert mi8.v() == 1 + assert mi8.bar() == 14 + + mi8b = MI8b(15) + assert mi8b.v() == 3 + assert mi8b.bar() == 15 + + +DISOWN_CLS_I_J_V_LIST = [ + (MI1, 1, 2, None), + (MI2, 3, 4, 1), + (MI3, 5, 6, 1), + (MI4, 7, 8, 1), + (MI5, 10, 11, 1), +] + + +@pytest.mark.xfail("env.PYPY", strict=False) +@pytest.mark.parametrize(("cls", "i", "j", "v"), DISOWN_CLS_I_J_V_LIST) +def test_disown_base1_first(cls, i, j, v): + obj = cls(i, j) + assert obj.foo() == i + assert m.disown_base1(obj) == 2000 * i + 1 + assert is_disowned(obj.foo) + assert obj.bar() == j + assert m.disown_base2(obj) == 2000 * j + 2 + assert is_disowned(obj.bar) + if v is not None: + assert obj.v() == v + + +@pytest.mark.xfail("env.PYPY", strict=False) +@pytest.mark.parametrize(("cls", "i", "j", "v"), DISOWN_CLS_I_J_V_LIST) +def test_disown_base2_first(cls, i, j, v): + obj = cls(i, j) + assert obj.bar() == j + assert m.disown_base2(obj) == 2000 * j + 2 + assert is_disowned(obj.bar) + assert obj.foo() == i + assert m.disown_base1(obj) == 2000 * i + 1 + assert is_disowned(obj.foo) + if v is not None: + assert obj.v() == v + + +@pytest.mark.xfail("env.PYPY", strict=False) +@pytest.mark.parametrize( + ("cls", "j", "v"), + [ + (MI6, 12, 1), + (MI7, 13, 4), + (MI8, 14, 1), + (MI8b, 15, 3), + ], +) +def test_disown_base2(cls, j, v): + obj = cls(j) + assert obj.bar() == j + assert m.disown_base2(obj) == 2000 * j + 2 + assert is_disowned(obj.bar) + assert obj.v() == v diff --git a/external_libraries/pybind11/tests/test_class_sh_factory_constructors.cpp b/external_libraries/pybind11/tests/test_class_sh_factory_constructors.cpp new file mode 100644 index 00000000..0718b569 --- /dev/null +++ b/external_libraries/pybind11/tests/test_class_sh_factory_constructors.cpp @@ -0,0 +1,156 @@ +#include "pybind11_tests.h" + +#include +#include + +namespace pybind11_tests { +namespace class_sh_factory_constructors { + +template // Using int as a trick to easily generate a series of types. +struct atyp { // Short for "any type". + std::string mtxt; +}; + +template +std::string get_mtxt(const T &obj) { + return obj.mtxt; +} + +using atyp_valu = atyp<0x0>; +using atyp_rref = atyp<0x1>; +using atyp_cref = atyp<0x2>; +using atyp_mref = atyp<0x3>; +using atyp_cptr = atyp<0x4>; +using atyp_mptr = atyp<0x5>; +using atyp_shmp = atyp<0x6>; +using atyp_shcp = atyp<0x7>; +using atyp_uqmp = atyp<0x8>; +using atyp_uqcp = atyp<0x9>; +using atyp_udmp = atyp<0xA>; +using atyp_udcp = atyp<0xB>; + +// clang-format off + +atyp_valu rtrn_valu() { atyp_valu obj{"Valu"}; return obj; } +atyp_rref&& rtrn_rref() { static atyp_rref obj; obj.mtxt = "Rref"; return std::move(obj); } +atyp_cref const& rtrn_cref() { static atyp_cref obj; obj.mtxt = "Cref"; return obj; } +atyp_mref& rtrn_mref() { static atyp_mref obj; obj.mtxt = "Mref"; return obj; } +atyp_cptr const* rtrn_cptr() { return new atyp_cptr{"Cptr"}; } +atyp_mptr* rtrn_mptr() { return new atyp_mptr{"Mptr"}; } + +std::shared_ptr rtrn_shmp() { return std::make_shared(atyp_shmp{"Shmp"}); } +std::shared_ptr rtrn_shcp() { return std::shared_ptr(new atyp_shcp{"Shcp"}); } + +std::unique_ptr rtrn_uqmp() { return std::unique_ptr(new atyp_uqmp{"Uqmp"}); } +std::unique_ptr rtrn_uqcp() { return std::unique_ptr(new atyp_uqcp{"Uqcp"}); } + +struct sddm : std::default_delete {}; +struct sddc : std::default_delete {}; + +std::unique_ptr rtrn_udmp() { return std::unique_ptr(new atyp_udmp{"Udmp"}); } +std::unique_ptr rtrn_udcp() { return std::unique_ptr(new atyp_udcp{"Udcp"}); } + +// clang-format on + +// Minimalistic approach to achieve full coverage of construct() overloads for constructing +// smart_holder from unique_ptr and shared_ptr returns. +struct with_alias { + int val = 0; + virtual ~with_alias() = default; + // Some compilers complain about implicitly defined versions of some of the following: + with_alias() = default; + with_alias(const with_alias &) = default; + with_alias(with_alias &&) = default; + with_alias &operator=(const with_alias &) = default; + with_alias &operator=(with_alias &&) = default; +}; +struct with_alias_alias : with_alias, py::trampoline_self_life_support {}; +struct sddwaa : std::default_delete {}; + +} // namespace class_sh_factory_constructors +} // namespace pybind11_tests + +TEST_SUBMODULE(class_sh_factory_constructors, m) { + using namespace pybind11_tests::class_sh_factory_constructors; + + py::classh(m, "atyp_valu") + .def(py::init(&rtrn_valu)) + .def("get_mtxt", get_mtxt); + + py::classh(m, "atyp_rref") + .def(py::init(&rtrn_rref)) + .def("get_mtxt", get_mtxt); + + py::classh(m, "atyp_cref") + // class_: ... must return a compatible ... + // classh: ... cannot pass object of non-trivial type ... + // .def(py::init(&rtrn_cref)) + .def("get_mtxt", get_mtxt); + + py::classh(m, "atyp_mref") + // class_: ... must return a compatible ... + // classh: ... cannot pass object of non-trivial type ... + // .def(py::init(&rtrn_mref)) + .def("get_mtxt", get_mtxt); + + py::classh(m, "atyp_cptr") + // class_: ... must return a compatible ... + // classh: ... must return a compatible ... + // .def(py::init(&rtrn_cptr)) + .def("get_mtxt", get_mtxt); + + py::classh(m, "atyp_mptr") + .def(py::init(&rtrn_mptr)) + .def("get_mtxt", get_mtxt); + + py::classh(m, "atyp_shmp") + .def(py::init(&rtrn_shmp)) + .def("get_mtxt", get_mtxt); + + py::classh(m, "atyp_shcp") + .def(py::init(&rtrn_shcp)) + .def("get_mtxt", get_mtxt); + + py::classh(m, "atyp_uqmp") + .def(py::init(&rtrn_uqmp)) + .def("get_mtxt", get_mtxt); + + py::classh(m, "atyp_uqcp") + .def(py::init(&rtrn_uqcp)) + .def("get_mtxt", get_mtxt); + + py::classh(m, "atyp_udmp") + .def(py::init(&rtrn_udmp)) + .def("get_mtxt", get_mtxt); + + py::classh(m, "atyp_udcp") + .def(py::init(&rtrn_udcp)) + .def("get_mtxt", get_mtxt); + + py::classh(m, "with_alias") + .def_readonly("val", &with_alias::val) + .def(py::init([](int i) { + auto p = std::unique_ptr(new with_alias_alias); + p->val = i * 100; + return p; + })) + .def(py::init([](int i, int j) { + auto p = std::unique_ptr(new with_alias_alias); + p->val = i * 100 + j * 10; + return p; + })) + .def(py::init([](int i, int j, int k) { + auto p = std::make_shared(); + p->val = i * 100 + j * 10 + k; + return p; + })) + .def(py::init( + [](int, int, int, int) { return std::unique_ptr(new with_alias); }, + [](int, int, int, int) { + return std::unique_ptr(new with_alias); // Invalid alias factory. + })) + .def(py::init([](int, int, int, int, int) { return std::make_shared(); }, + [](int, int, int, int, int) { + return std::make_shared(); // Invalid alias factory. + })); +} diff --git a/external_libraries/pybind11/tests/test_class_sh_factory_constructors.py b/external_libraries/pybind11/tests/test_class_sh_factory_constructors.py new file mode 100644 index 00000000..6288c001 --- /dev/null +++ b/external_libraries/pybind11/tests/test_class_sh_factory_constructors.py @@ -0,0 +1,53 @@ +from __future__ import annotations + +import pytest + +from pybind11_tests import class_sh_factory_constructors as m + + +def test_atyp_factories(): + assert m.atyp_valu().get_mtxt() == "Valu" + assert m.atyp_rref().get_mtxt() == "Rref" + # sert m.atyp_cref().get_mtxt() == "Cref" + # sert m.atyp_mref().get_mtxt() == "Mref" + # sert m.atyp_cptr().get_mtxt() == "Cptr" + assert m.atyp_mptr().get_mtxt() == "Mptr" + assert m.atyp_shmp().get_mtxt() == "Shmp" + assert m.atyp_shcp().get_mtxt() == "Shcp" + assert m.atyp_uqmp().get_mtxt() == "Uqmp" + assert m.atyp_uqcp().get_mtxt() == "Uqcp" + assert m.atyp_udmp().get_mtxt() == "Udmp" + assert m.atyp_udcp().get_mtxt() == "Udcp" + + +@pytest.mark.parametrize( + ("init_args", "expected"), + [ + ((3,), 300), + ((5, 7), 570), + ((9, 11, 13), 1023), + ], +) +def test_with_alias_success(init_args, expected): + assert m.with_alias(*init_args).val == expected + + +@pytest.mark.parametrize( + ("num_init_args", "smart_ptr"), + [ + (4, "std::unique_ptr"), + (5, "std::shared_ptr"), + ], +) +def test_with_alias_invalid(num_init_args, smart_ptr): + class PyDrvdWithAlias(m.with_alias): + pass + + with pytest.raises(TypeError) as excinfo: + PyDrvdWithAlias(*((0,) * num_init_args)) + assert ( + str(excinfo.value) + == "pybind11::init(): construction failed: returned " + + smart_ptr + + " pointee is not an alias instance" + ) diff --git a/external_libraries/pybind11/tests/test_class_sh_inheritance.cpp b/external_libraries/pybind11/tests/test_class_sh_inheritance.cpp new file mode 100644 index 00000000..8bdd0a7f --- /dev/null +++ b/external_libraries/pybind11/tests/test_class_sh_inheritance.cpp @@ -0,0 +1,90 @@ +#include "pybind11_tests.h" + +#include + +namespace pybind11_tests { +namespace class_sh_inheritance { + +template +struct base_template { + base_template() : base_id(Id) {} + virtual ~base_template() = default; + virtual int id() const { return base_id; } + int base_id; + + // Some compilers complain about implicitly defined versions of some of the following: + base_template(const base_template &) = default; + base_template(base_template &&) noexcept = default; + base_template &operator=(const base_template &) = default; + base_template &operator=(base_template &&) noexcept = default; +}; + +using base = base_template<100>; + +struct drvd : base { + int id() const override { return 2 * base_id; } +}; + +// clang-format off +inline drvd *rtrn_mptr_drvd() { return new drvd; } +inline base *rtrn_mptr_drvd_up_cast() { return new drvd; } + +inline int pass_cptr_base(base const *b) { return b->id() + 11; } +inline int pass_cptr_drvd(drvd const *d) { return d->id() + 12; } + +inline std::shared_ptr rtrn_shmp_drvd() { return std::make_shared(); } +inline std::shared_ptr rtrn_shmp_drvd_up_cast() { return std::make_shared(); } + +inline int pass_shcp_base(const std::shared_ptr& b) { return b->id() + 21; } +inline int pass_shcp_drvd(const std::shared_ptr& d) { return d->id() + 22; } +// clang-format on + +using base1 = base_template<110>; +using base2 = base_template<120>; + +// Not reusing base here because it would interfere with the single-inheritance test. +struct drvd2 : base1, base2 { + int id() const override { return 3 * base1::base_id + 4 * base2::base_id; } +}; + +// clang-format off +inline drvd2 *rtrn_mptr_drvd2() { return new drvd2; } +inline base1 *rtrn_mptr_drvd2_up_cast1() { return new drvd2; } +inline base2 *rtrn_mptr_drvd2_up_cast2() { return new drvd2; } + +inline int pass_cptr_base1(base1 const *b) { return b->id() + 21; } +inline int pass_cptr_base2(base2 const *b) { return b->id() + 22; } +inline int pass_cptr_drvd2(drvd2 const *d) { return d->id() + 23; } +// clang-format on + +TEST_SUBMODULE(class_sh_inheritance, m) { + py::classh(m, "base"); + py::classh(m, "drvd"); + + auto rvto = py::return_value_policy::take_ownership; + + m.def("rtrn_mptr_drvd", rtrn_mptr_drvd, rvto); + m.def("rtrn_mptr_drvd_up_cast", rtrn_mptr_drvd_up_cast, rvto); + m.def("pass_cptr_base", pass_cptr_base); + m.def("pass_cptr_drvd", pass_cptr_drvd); + + m.def("rtrn_shmp_drvd", rtrn_shmp_drvd); + m.def("rtrn_shmp_drvd_up_cast", rtrn_shmp_drvd_up_cast); + m.def("pass_shcp_base", pass_shcp_base); + m.def("pass_shcp_drvd", pass_shcp_drvd); + + // __init__ needed for Python inheritance. + py::classh(m, "base1").def(py::init<>()); + py::classh(m, "base2").def(py::init<>()); + py::classh(m, "drvd2"); + + m.def("rtrn_mptr_drvd2", rtrn_mptr_drvd2, rvto); + m.def("rtrn_mptr_drvd2_up_cast1", rtrn_mptr_drvd2_up_cast1, rvto); + m.def("rtrn_mptr_drvd2_up_cast2", rtrn_mptr_drvd2_up_cast2, rvto); + m.def("pass_cptr_base1", pass_cptr_base1); + m.def("pass_cptr_base2", pass_cptr_base2); + m.def("pass_cptr_drvd2", pass_cptr_drvd2); +} + +} // namespace class_sh_inheritance +} // namespace pybind11_tests diff --git a/external_libraries/pybind11/tests/test_class_sh_inheritance.py b/external_libraries/pybind11/tests/test_class_sh_inheritance.py new file mode 100644 index 00000000..cd9d6f47 --- /dev/null +++ b/external_libraries/pybind11/tests/test_class_sh_inheritance.py @@ -0,0 +1,63 @@ +from __future__ import annotations + +from pybind11_tests import class_sh_inheritance as m + + +def test_rtrn_mptr_drvd_pass_cptr_base(): + d = m.rtrn_mptr_drvd() + i = m.pass_cptr_base(d) # load_impl Case 2a + assert i == 2 * 100 + 11 + + +def test_rtrn_shmp_drvd_pass_shcp_base(): + d = m.rtrn_shmp_drvd() + i = m.pass_shcp_base(d) # load_impl Case 2a + assert i == 2 * 100 + 21 + + +def test_rtrn_mptr_drvd_up_cast_pass_cptr_drvd(): + b = m.rtrn_mptr_drvd_up_cast() + # the base return is down-cast immediately. + assert b.__class__.__name__ == "drvd" + i = m.pass_cptr_drvd(b) + assert i == 2 * 100 + 12 + + +def test_rtrn_shmp_drvd_up_cast_pass_shcp_drvd(): + b = m.rtrn_shmp_drvd_up_cast() + # the base return is down-cast immediately. + assert b.__class__.__name__ == "drvd" + i = m.pass_shcp_drvd(b) + assert i == 2 * 100 + 22 + + +def test_rtrn_mptr_drvd2_pass_cptr_bases(): + d = m.rtrn_mptr_drvd2() + i1 = m.pass_cptr_base1(d) # load_impl Case 2c + assert i1 == 3 * 110 + 4 * 120 + 21 + i2 = m.pass_cptr_base2(d) + assert i2 == 3 * 110 + 4 * 120 + 22 + + +def test_rtrn_mptr_drvd2_up_casts_pass_cptr_drvd2(): + b1 = m.rtrn_mptr_drvd2_up_cast1() + assert b1.__class__.__name__ == "drvd2" + i1 = m.pass_cptr_drvd2(b1) + assert i1 == 3 * 110 + 4 * 120 + 23 + b2 = m.rtrn_mptr_drvd2_up_cast2() + assert b2.__class__.__name__ == "drvd2" + i2 = m.pass_cptr_drvd2(b2) + assert i2 == 3 * 110 + 4 * 120 + 23 + + +def test_python_drvd2(): + class Drvd2(m.base1, m.base2): + def __init__(self): + m.base1.__init__(self) + m.base2.__init__(self) + + d = Drvd2() + i1 = m.pass_cptr_base1(d) # load_impl Case 2b + assert i1 == 110 + 21 + i2 = m.pass_cptr_base2(d) + assert i2 == 120 + 22 diff --git a/external_libraries/pybind11/tests/test_class_sh_mi_thunks.cpp b/external_libraries/pybind11/tests/test_class_sh_mi_thunks.cpp new file mode 100644 index 00000000..0d1672cf --- /dev/null +++ b/external_libraries/pybind11/tests/test_class_sh_mi_thunks.cpp @@ -0,0 +1,230 @@ +#include "pybind11_tests.h" + +#include +#include +#include + +namespace test_class_sh_mi_thunks { + +// For general background: https://shaharmike.com/cpp/vtable-part2/ +// C++ vtables - Part 2 - Multiple Inheritance +// ... the compiler creates a 'thunk' method that corrects `this` ... + +// This test was added under PR #4380 + +struct Base0 { + virtual ~Base0() = default; + Base0() = default; + Base0(const Base0 &) = delete; +}; + +struct Base1 { + virtual ~Base1() = default; + // Using `vector` here because it is known to make this test very sensitive to bugs. + std::vector vec = {1, 2, 3, 4, 5}; + Base1() = default; + Base1(const Base1 &) = delete; +}; + +struct Derived : Base1, Base0 { + ~Derived() override = default; + Derived() = default; + Derived(const Derived &) = delete; +}; + +// ChatGPT-generated Diamond added under PR #5836 + +struct VBase { + VBase() = default; + VBase(const VBase &) = default; // silence -Wdeprecated-copy-with-dtor + VBase &operator=(const VBase &) = default; + VBase(VBase &&) = default; + VBase &operator=(VBase &&) = default; + virtual ~VBase() = default; + virtual int ping() const { return 1; } + int vbase_tag = 42; // ensure it's not empty +}; + +// Make the virtual bases non-empty and (likely) differently sized. +// The test does *not* require different sizes; we only want to avoid "all at offset 0". +// If a compiler/ABI still places the virtual base at offset 0, our test logs that via +// test_virtual_base_at_offset_0() and continues. +struct Left : virtual VBase { + char pad_l[4]; // small, typically 4 + padding + ~Left() override = default; +}; +struct Right : virtual VBase { + char pad_r[24]; // larger, to differ from Left + ~Right() override = default; +}; + +struct Diamond : Left, Right { + Diamond() = default; + Diamond(const Diamond &) = default; + ~Diamond() override = default; + int ping() const override { return 7; } + int self_tag = 99; +}; + +VBase *make_diamond_as_vbase_raw_ptr() { + auto *ptr = new Diamond; + return ptr; // upcast +} + +std::shared_ptr make_diamond_as_vbase_shared_ptr() { + auto shptr = std::make_shared(); + return shptr; // upcast +} + +std::unique_ptr make_diamond_as_vbase_unique_ptr() { + auto uqptr = std::unique_ptr(new Diamond); + return uqptr; // upcast +} + +// For diagnostics +struct DiamondAddrs { + uintptr_t as_self; + uintptr_t as_vbase; + uintptr_t as_left; + uintptr_t as_right; +}; + +DiamondAddrs diamond_addrs() { + auto sp = std::make_shared(); + return DiamondAddrs{reinterpret_cast(sp.get()), + reinterpret_cast(static_cast(sp.get())), + reinterpret_cast(static_cast(sp.get())), + reinterpret_cast(static_cast(sp.get()))}; +} + +// Animal-Cat-Tiger reproducer copied from PR #5796 +// clone_raw_ptr, clone_unique_ptr added under PR #5836 + +class Animal { +public: + Animal() = default; + Animal(const Animal &) = default; + Animal &operator=(const Animal &) = default; + virtual Animal *clone_raw_ptr() const = 0; + virtual std::shared_ptr clone_shared_ptr() const = 0; + virtual std::unique_ptr clone_unique_ptr() const = 0; + virtual ~Animal() = default; +}; + +class Cat : virtual public Animal { +public: + Cat() = default; + Cat(const Cat &) = default; + Cat &operator=(const Cat &) = default; + ~Cat() override = default; +}; + +class Tiger : virtual public Cat { +public: + Tiger() = default; + Tiger(const Tiger &) = default; + Tiger &operator=(const Tiger &) = default; + ~Tiger() override = default; + Animal *clone_raw_ptr() const override { + return new Tiger(*this); // upcast + } + std::shared_ptr clone_shared_ptr() const override { + return std::make_shared(*this); // upcast + } + std::unique_ptr clone_unique_ptr() const override { + return std::unique_ptr(new Tiger(*this)); // upcast + } +}; + +} // namespace test_class_sh_mi_thunks + +TEST_SUBMODULE(class_sh_mi_thunks, m) { + using namespace test_class_sh_mi_thunks; + + m.def("ptrdiff_drvd_base0", []() { + auto drvd = std::unique_ptr(new Derived); + auto *base0 = dynamic_cast(drvd.get()); + return std::ptrdiff_t(reinterpret_cast(drvd.get()) + - reinterpret_cast(base0)); + }); + + py::classh(m, "Base0"); + py::classh(m, "Base1"); + py::classh(m, "Derived"); + + m.def( + "get_drvd_as_base0_raw_ptr", + []() { + auto *drvd = new Derived; + auto *base0 = dynamic_cast(drvd); + return base0; + }, + py::return_value_policy::take_ownership); + + m.def("get_drvd_as_base0_shared_ptr", []() { + auto drvd = std::make_shared(); + auto base0 = std::dynamic_pointer_cast(drvd); + return base0; + }); + + m.def("get_drvd_as_base0_unique_ptr", []() { + auto drvd = std::unique_ptr(new Derived); + auto base0 = std::unique_ptr(std::move(drvd)); + return base0; + }); + + m.def("vec_size_base0_raw_ptr", [](const Base0 *obj) { + const auto *obj_der = dynamic_cast(obj); + if (obj_der == nullptr) { + return std::size_t(0); + } + return obj_der->vec.size(); + }); + + m.def("vec_size_base0_shared_ptr", [](const std::shared_ptr &obj) -> std::size_t { + const auto obj_der = std::dynamic_pointer_cast(obj); + if (!obj_der) { + return std::size_t(0); + } + return obj_der->vec.size(); + }); + + m.def("vec_size_base0_unique_ptr", [](std::unique_ptr obj) -> std::size_t { + const auto *obj_der = dynamic_cast(obj.get()); + if (obj_der == nullptr) { + return std::size_t(0); + } + return obj_der->vec.size(); + }); + + py::class_(m, "VBase").def("ping", &VBase::ping); + + py::class_(m, "Left"); + py::class_(m, "Right"); + + py::class_(m, "Diamond", py::multiple_inheritance()) + .def(py::init<>()) + .def("ping", &Diamond::ping); + + m.def("make_diamond_as_vbase_raw_ptr", + &make_diamond_as_vbase_raw_ptr, + py::return_value_policy::take_ownership); + m.def("make_diamond_as_vbase_shared_ptr", &make_diamond_as_vbase_shared_ptr); + m.def("make_diamond_as_vbase_unique_ptr", &make_diamond_as_vbase_unique_ptr); + + py::class_(m, "DiamondAddrs") + .def_readonly("as_self", &DiamondAddrs::as_self) + .def_readonly("as_vbase", &DiamondAddrs::as_vbase) + .def_readonly("as_left", &DiamondAddrs::as_left) + .def_readonly("as_right", &DiamondAddrs::as_right); + + m.def("diamond_addrs", &diamond_addrs); + + py::classh(m, "Animal"); + py::classh(m, "Cat"); + py::classh(m, "Tiger", py::multiple_inheritance()) + .def(py::init<>()) + .def("clone_raw_ptr", &Tiger::clone_raw_ptr) + .def("clone_shared_ptr", &Tiger::clone_shared_ptr) + .def("clone_unique_ptr", &Tiger::clone_unique_ptr); +} diff --git a/external_libraries/pybind11/tests/test_class_sh_mi_thunks.py b/external_libraries/pybind11/tests/test_class_sh_mi_thunks.py new file mode 100644 index 00000000..7fa164d4 --- /dev/null +++ b/external_libraries/pybind11/tests/test_class_sh_mi_thunks.py @@ -0,0 +1,104 @@ +from __future__ import annotations + +import pytest + +from pybind11_tests import class_sh_mi_thunks as m + + +def test_ptrdiff_drvd_base0(): + ptrdiff = m.ptrdiff_drvd_base0() + # A failure here does not (necessarily) mean that there is a bug, but that + # test_class_sh_mi_thunks is not exercising what it is supposed to. + # If this ever fails on some platforms: use pytest.skip() + # If this ever fails on all platforms: don't know, seems extremely unlikely. + assert ptrdiff != 0 + + +@pytest.mark.parametrize( + "vec_size_fn", + [ + m.vec_size_base0_raw_ptr, + m.vec_size_base0_shared_ptr, + ], +) +@pytest.mark.parametrize( + "get_fn", + [ + m.get_drvd_as_base0_raw_ptr, + m.get_drvd_as_base0_shared_ptr, + m.get_drvd_as_base0_unique_ptr, + ], +) +def test_get_vec_size_raw_shared(get_fn, vec_size_fn): + obj = get_fn() + assert vec_size_fn(obj) == 5 + + +@pytest.mark.parametrize( + "get_fn", [m.get_drvd_as_base0_raw_ptr, m.get_drvd_as_base0_unique_ptr] +) +def test_get_vec_size_unique(get_fn): + obj = get_fn() + assert m.vec_size_base0_unique_ptr(obj) == 5 + with pytest.raises(ValueError, match="Python instance was disowned"): + m.vec_size_base0_unique_ptr(obj) + + +def test_get_shared_vec_size_unique(): + obj = m.get_drvd_as_base0_shared_ptr() + with pytest.raises(ValueError) as exc_info: + m.vec_size_base0_unique_ptr(obj) + assert ( + str(exc_info.value) == "Cannot disown external shared_ptr (load_as_unique_ptr)." + ) + + +def test_virtual_base_not_at_offset_0(): + # This test ensures that the Diamond fixture actually exercises a non-zero + # virtual-base subobject offset on our supported platforms/ABIs. + # + # If this assert ever fails on some platform/toolchain, please adjust the + # C++ fixture so the virtual base is *not* at offset 0: + # - Keep VBase non-empty. + # - Make Left and Right non-empty and asymmetrically sized and, if + # needed, nudge with a modest alignment. + # - The goal is to achieve a non-zero address delta between `Diamond*` + # and `static_cast(Diamond*)`. + # + # Rationale: certain smart_holder features are exercised only when the + # registered subobject address differs from the most-derived object start, + # so this check guards test efficacy across compilers. + addrs = m.diamond_addrs() + assert addrs.as_vbase - addrs.as_self != 0, ( + "Diamond VBase at offset 0 on this platform; to ensure test efficacy, " + "tweak fixtures (VBase/Left/Right) to ensure non-zero subobject offset." + ) + + +@pytest.mark.parametrize( + "make_fn", + [ + m.make_diamond_as_vbase_raw_ptr, # exercises smart_holder::from_raw_ptr_take_ownership + m.make_diamond_as_vbase_shared_ptr, # exercises smart_holder_from_shared_ptr + m.make_diamond_as_vbase_unique_ptr, # exercises smart_holder_from_unique_ptr + ], +) +def test_make_diamond_as_vbase(make_fn): + # Added under PR #5836 + vb = make_fn() + assert vb.ping() == 7 + + +@pytest.mark.parametrize( + "clone_fn", + [ + m.Tiger.clone_raw_ptr, + m.Tiger.clone_shared_ptr, + m.Tiger.clone_unique_ptr, + ], +) +def test_animal_cat_tiger(clone_fn): + # Based on Animal-Cat-Tiger reproducer under PR #5796 + tiger = m.Tiger() + cloned = clone_fn(tiger) + assert isinstance(cloned, m.Tiger) diff --git a/external_libraries/pybind11/tests/test_class_sh_property.cpp b/external_libraries/pybind11/tests/test_class_sh_property.cpp new file mode 100644 index 00000000..8777bdc9 --- /dev/null +++ b/external_libraries/pybind11/tests/test_class_sh_property.cpp @@ -0,0 +1,129 @@ +// The compact 4-character naming matches that in test_class_sh_basic.cpp +// Variable names are intentionally terse, to not distract from the more important C++ type names: +// valu(e), ref(erence), ptr or p (pointer), r = rvalue, m = mutable, c = const, +// sh = shared_ptr, uq = unique_ptr. + +#include "pybind11_tests.h" + +#include + +namespace test_class_sh_property { + +struct ClassicField { + int num = -88; +}; + +struct ClassicOuter { + ClassicField *m_mptr = nullptr; + const ClassicField *m_cptr = nullptr; +}; + +struct Field { + int num = -99; +}; + +struct Outer { + Field m_valu; + Field *m_mptr = nullptr; + const Field *m_cptr = nullptr; + std::unique_ptr m_uqmp; + std::unique_ptr m_uqcp; + std::shared_ptr m_shmp; + std::shared_ptr m_shcp; +}; + +inline void DisownOuter(std::unique_ptr) {} + +struct WithCharArrayMember { + WithCharArrayMember() { std::memcpy(char6_member, "Char6", 6); } + char char6_member[6]; +}; + +struct WithConstCharPtrMember { + const char *const_char_ptr_member = "ConstChar*"; +}; + +// See PR #6008 +enum class EnumAB { + A = 0, + B = 1, +}; + +struct ShWithEnumABMember { + EnumAB level = EnumAB::A; +}; + +struct SimpleStruct { + int value = 7; +}; + +struct ShWithSimpleStructMember { + SimpleStruct legacy; +}; + +} // namespace test_class_sh_property + +TEST_SUBMODULE(class_sh_property, m) { + using namespace test_class_sh_property; + + py::class_>(m, "ClassicField") + .def(py::init<>()) + .def_readwrite("num", &ClassicField::num); + + py::class_>(m, "ClassicOuter") + .def(py::init<>()) + .def_readonly("m_mptr_readonly", &ClassicOuter::m_mptr) + .def_readwrite("m_mptr_readwrite", &ClassicOuter::m_mptr) + .def_readwrite("m_cptr_readonly", &ClassicOuter::m_cptr) + .def_readwrite("m_cptr_readwrite", &ClassicOuter::m_cptr); + + py::classh(m, "Field").def(py::init<>()).def_readwrite("num", &Field::num); + + py::classh(m, "Outer") + .def(py::init<>()) + + .def_readonly("m_valu_readonly", &Outer::m_valu) + .def_readwrite("m_valu_readwrite", &Outer::m_valu) + + .def_readonly("m_mptr_readonly", &Outer::m_mptr) + .def_readwrite("m_mptr_readwrite", &Outer::m_mptr) + .def_readonly("m_cptr_readonly", &Outer::m_cptr) + .def_readwrite("m_cptr_readwrite", &Outer::m_cptr) + + // .def_readonly("m_uqmp_readonly", &Outer::m_uqmp) // Custom compilation Error. + .def_readwrite("m_uqmp_readwrite", &Outer::m_uqmp) + // .def_readonly("m_uqcp_readonly", &Outer::m_uqcp) // Custom compilation Error. + .def_readwrite("m_uqcp_readwrite", &Outer::m_uqcp) + + .def_readwrite("m_shmp_readonly", &Outer::m_shmp) + .def_readwrite("m_shmp_readwrite", &Outer::m_shmp) + .def_readwrite("m_shcp_readonly", &Outer::m_shcp) + .def_readwrite("m_shcp_readwrite", &Outer::m_shcp); + + m.def("DisownOuter", DisownOuter); + + py::classh(m, "WithCharArrayMember") + .def(py::init<>()) + .def_readonly("char6_member", &WithCharArrayMember::char6_member); + + py::classh(m, "WithConstCharPtrMember") + .def(py::init<>()) + .def_readonly("const_char_ptr_member", &WithConstCharPtrMember::const_char_ptr_member); + + // See PR #6008 + py::enum_(m, "EnumAB").value("A", EnumAB::A).value("B", EnumAB::B); + + py::classh(m, "ShWithEnumABMember") + .def(py::init<>()) + .def_readwrite("level", &ShWithEnumABMember::level); + + py::class_(m, "SimpleStruct") + .def(py::init<>()) + .def_readwrite("value", &SimpleStruct::value); + + py::classh(m, "ShWithSimpleStructMember") + .def(py::init<>()) + .def_readwrite("legacy", &ShWithSimpleStructMember::legacy); + + m.def("getSimpleStructAsShared", []() { return std::make_shared(); }); +} diff --git a/external_libraries/pybind11/tests/test_class_sh_property.py b/external_libraries/pybind11/tests/test_class_sh_property.py new file mode 100644 index 00000000..b8a5933e --- /dev/null +++ b/external_libraries/pybind11/tests/test_class_sh_property.py @@ -0,0 +1,218 @@ +# The compact 4-character naming scheme (e.g. mptr, cptr, shcp) is explained at the top of +# test_class_sh_property.cpp. +from __future__ import annotations + +import pytest + +import env # noqa: F401 +import pybind11_tests +from pybind11_tests import class_sh_property as m + + +@pytest.mark.skipif( + "env.PYPY or env.GRAALPY", reason="gc after `del field` is apparently deferred" +) +@pytest.mark.parametrize("m_attr", ["m_valu_readonly", "m_valu_readwrite"]) +def test_valu_getter(m_attr): + # Reduced from PyCLIF test: + # https://github.com/google/clif/blob/c371a6d4b28d25d53a16e6d2a6d97305fb1be25a/clif/testing/python/nested_fields_test.py#L56 + outer = m.Outer() + field = getattr(outer, m_attr) + assert field.num == -99 + with pytest.raises(ValueError) as excinfo: + m.DisownOuter(outer) + assert str(excinfo.value) == "Cannot disown use_count != 1 (load_as_unique_ptr)." + del field + m.DisownOuter(outer) + with pytest.raises(ValueError, match="Python instance was disowned") as excinfo: + getattr(outer, m_attr) + + +def test_valu_setter(): + outer = m.Outer() + assert outer.m_valu_readonly.num == -99 + assert outer.m_valu_readwrite.num == -99 + field = m.Field() + field.num = 35 + outer.m_valu_readwrite = field + assert outer.m_valu_readonly.num == 35 + assert outer.m_valu_readwrite.num == 35 + + +@pytest.mark.parametrize("m_attr", ["m_shmp", "m_shcp"]) +def test_shp(m_attr): + m_attr_readonly = m_attr + "_readonly" + m_attr_readwrite = m_attr + "_readwrite" + outer = m.Outer() + assert getattr(outer, m_attr_readonly) is None + assert getattr(outer, m_attr_readwrite) is None + field = m.Field() + field.num = 43 + setattr(outer, m_attr_readwrite, field) + assert getattr(outer, m_attr_readonly).num == 43 + assert getattr(outer, m_attr_readwrite).num == 43 + getattr(outer, m_attr_readonly).num = 57 + getattr(outer, m_attr_readwrite).num = 57 + assert field.num == 57 + del field + assert getattr(outer, m_attr_readonly).num == 57 + assert getattr(outer, m_attr_readwrite).num == 57 + + +@pytest.mark.parametrize( + ("field_type", "num_default", "outer_type"), + [ + (m.ClassicField, -88, m.ClassicOuter), + (m.Field, -99, m.Outer), + ], +) +@pytest.mark.parametrize("m_attr", ["m_mptr", "m_cptr"]) +@pytest.mark.parametrize("r_kind", ["_readonly", "_readwrite"]) +def test_ptr(field_type, num_default, outer_type, m_attr, r_kind): + m_attr_r_kind = m_attr + r_kind + outer = outer_type() + assert getattr(outer, m_attr_r_kind) is None + field = field_type() + assert field.num == num_default + setattr(outer, m_attr + "_readwrite", field) + assert getattr(outer, m_attr_r_kind).num == num_default + field.num = 76 + assert getattr(outer, m_attr_r_kind).num == 76 + # Change to -88 or -99 to demonstrate Undefined Behavior (dangling pointer). + if num_default == 88 and m_attr == "m_mptr": + del field + assert getattr(outer, m_attr_r_kind).num == 76 + + +@pytest.mark.parametrize("m_attr_readwrite", ["m_uqmp_readwrite", "m_uqcp_readwrite"]) +def test_uqp(m_attr_readwrite): + outer = m.Outer() + assert getattr(outer, m_attr_readwrite) is None + field_orig = m.Field() + field_orig.num = 39 + setattr(outer, m_attr_readwrite, field_orig) + with pytest.raises(ValueError, match="Python instance was disowned"): + _ = field_orig.num + field_retr1 = getattr(outer, m_attr_readwrite) + assert getattr(outer, m_attr_readwrite) is None + assert field_retr1.num == 39 + field_retr1.num = 93 + setattr(outer, m_attr_readwrite, field_retr1) + with pytest.raises(ValueError): + _ = field_retr1.num + field_retr2 = getattr(outer, m_attr_readwrite) + assert field_retr2.num == 93 + + +# Proof-of-concept (POC) for safe & intuitive Python access to unique_ptr members. +# The C++ member unique_ptr is disowned to a temporary Python object for accessing +# an attribute of the member. After the attribute was accessed, the Python object +# is disowned back to the C++ member unique_ptr. +# Productizing this POC is left for a future separate PR, as needed. +class unique_ptr_field_proxy_poc: + def __init__(self, obj, field_name): + object.__setattr__(self, "__obj", obj) + object.__setattr__(self, "__field_name", field_name) + + def __getattr__(self, *args, **kwargs): + return _proxy_dereference(self, getattr, *args, **kwargs) + + def __setattr__(self, *args, **kwargs): + return _proxy_dereference(self, setattr, *args, **kwargs) + + def __delattr__(self, *args, **kwargs): + return _proxy_dereference(self, delattr, *args, **kwargs) + + +def _proxy_dereference(proxy, xxxattr, *args, **kwargs): + obj = object.__getattribute__(proxy, "__obj") + field_name = object.__getattribute__(proxy, "__field_name") + field = getattr(obj, field_name) # Disowns the C++ unique_ptr member. + assert field is not None + try: + return xxxattr(field, *args, **kwargs) + finally: + setattr(obj, field_name, field) # Disowns the temporary Python object (field). + + +@pytest.mark.parametrize("m_attr", ["m_uqmp", "m_uqcp"]) +def test_unique_ptr_field_proxy_poc(m_attr): + m_attr_readwrite = m_attr + "_readwrite" + outer = m.Outer() + field_orig = m.Field() + field_orig.num = 45 + setattr(outer, m_attr_readwrite, field_orig) + field_proxy = unique_ptr_field_proxy_poc(outer, m_attr_readwrite) + assert field_proxy.num == 45 + assert field_proxy.num == 45 + with pytest.raises(AttributeError): + _ = field_proxy.xyz + assert field_proxy.num == 45 + field_proxy.num = 82 + assert field_proxy.num == 82 + field_proxy = unique_ptr_field_proxy_poc(outer, m_attr_readwrite) + assert field_proxy.num == 82 + with pytest.raises(AttributeError): + del field_proxy.num + assert field_proxy.num == 82 + + +def test_readonly_char6_member(): + obj = m.WithCharArrayMember() + assert obj.char6_member == "Char6" + + +def test_readonly_const_char_ptr_member(): + obj = m.WithConstCharPtrMember() + assert obj.const_char_ptr_member == "ConstChar*" + + +# See PR #6008 +def test_enum_member_with_smart_holder_def_readwrite(): + obj = m.ShWithEnumABMember() + assert obj.level == m.EnumAB.A + for _ in range(100): + v = obj.level + assert v == m.EnumAB.A + del v + + +# See PR #6008 +def test_non_smart_holder_member_type_with_smart_holder_owner(): + obj = m.ShWithSimpleStructMember() + for _ in range(1000): + v = obj.legacy + assert v.value == 7 + del v + + +# See PR #6008, previously this was UB +@pytest.mark.skipif( + pybind11_tests.PYBIND11_TEST_SMART_HOLDER, + reason="PYBIND11_TEST_SMART_HOLDER changes the default holder", +) +def test_shared_ptr_return_for_unique_ptr_holder(): + with pytest.raises( + RuntimeError, + match="Unable to convert std::shared_ptr to Python when the bound type does not use std::shared_ptr or py::smart_holder as its holder type", + ): + m.getSimpleStructAsShared() + + +def test_non_smart_holder_member_type_with_smart_holder_owner_aliases_member(): + obj = m.ShWithSimpleStructMember() + legacy = obj.legacy + legacy.value = 13 + assert obj.legacy.value == 13 + + +def test_non_smart_holder_member_type_with_smart_holder_owner_aliases_member_multiple_reads(): + obj = m.ShWithSimpleStructMember() + + a = obj.legacy + b = obj.legacy + + a.value = 13 + + assert b.value == 13 + assert obj.legacy.value == 13 diff --git a/external_libraries/pybind11/tests/test_class_sh_property_non_owning.cpp b/external_libraries/pybind11/tests/test_class_sh_property_non_owning.cpp new file mode 100644 index 00000000..5a5877e4 --- /dev/null +++ b/external_libraries/pybind11/tests/test_class_sh_property_non_owning.cpp @@ -0,0 +1,65 @@ +#include "pybind11_tests.h" + +#include +#include + +namespace test_class_sh_property_non_owning { + +struct CoreField { + explicit CoreField(int int_value = -99) : int_value{int_value} {} + int int_value; +}; + +struct DataField { + DataField(int i_value, int i_shared, int i_unique) + : core_fld_value{i_value}, core_fld_shared_ptr{new CoreField{i_shared}}, + core_fld_raw_ptr{core_fld_shared_ptr.get()}, + core_fld_unique_ptr{new CoreField{i_unique}} {} + CoreField core_fld_value; + std::shared_ptr core_fld_shared_ptr; + CoreField *core_fld_raw_ptr; + std::unique_ptr core_fld_unique_ptr; +}; + +struct DataFieldsHolder { +private: + std::vector vec; + +public: + explicit DataFieldsHolder(std::size_t vec_size) { + for (std::size_t i = 0; i < vec_size; i++) { + int i11 = static_cast(i) * 11; + vec.emplace_back(13 + i11, 14 + i11, 15 + i11); + } + } + + DataFieldsHolder(DataFieldsHolder &&) noexcept = default; + + DataField *vec_at(std::size_t index) { + if (index >= vec.size()) { + return nullptr; + } + return &vec[index]; + } +}; + +} // namespace test_class_sh_property_non_owning + +using namespace test_class_sh_property_non_owning; + +TEST_SUBMODULE(class_sh_property_non_owning, m) { + py::classh(m, "CoreField").def_readwrite("int_value", &CoreField::int_value); + + py::classh(m, "DataField") + .def_readonly("core_fld_value_ro", &DataField::core_fld_value) + .def_readwrite("core_fld_value_rw", &DataField::core_fld_value) + .def_readonly("core_fld_shared_ptr_ro", &DataField::core_fld_shared_ptr) + .def_readwrite("core_fld_shared_ptr_rw", &DataField::core_fld_shared_ptr) + .def_readonly("core_fld_raw_ptr_ro", &DataField::core_fld_raw_ptr) + .def_readwrite("core_fld_raw_ptr_rw", &DataField::core_fld_raw_ptr) + .def_readwrite("core_fld_unique_ptr_rw", &DataField::core_fld_unique_ptr); + + py::classh(m, "DataFieldsHolder") + .def(py::init()) + .def("vec_at", &DataFieldsHolder::vec_at, py::return_value_policy::reference_internal); +} diff --git a/external_libraries/pybind11/tests/test_class_sh_property_non_owning.py b/external_libraries/pybind11/tests/test_class_sh_property_non_owning.py new file mode 100644 index 00000000..33a9d450 --- /dev/null +++ b/external_libraries/pybind11/tests/test_class_sh_property_non_owning.py @@ -0,0 +1,30 @@ +from __future__ import annotations + +import pytest + +from pybind11_tests import class_sh_property_non_owning as m + + +@pytest.mark.parametrize("persistent_holder", [True, False]) +@pytest.mark.parametrize( + ("core_fld", "expected"), + [ + ("core_fld_value_ro", (13, 24)), + ("core_fld_value_rw", (13, 24)), + ("core_fld_shared_ptr_ro", (14, 25)), + ("core_fld_shared_ptr_rw", (14, 25)), + ("core_fld_raw_ptr_ro", (14, 25)), + ("core_fld_raw_ptr_rw", (14, 25)), + ("core_fld_unique_ptr_rw", (15, 26)), + ], +) +def test_core_fld_common(core_fld, expected, persistent_holder): + if persistent_holder: + h = m.DataFieldsHolder(2) + for i, exp in enumerate(expected): + c = getattr(h.vec_at(i), core_fld) + assert c.int_value == exp + else: + for i, exp in enumerate(expected): + c = getattr(m.DataFieldsHolder(2).vec_at(i), core_fld) + assert c.int_value == exp diff --git a/external_libraries/pybind11/tests/test_class_sh_shared_ptr_copy_move.cpp b/external_libraries/pybind11/tests/test_class_sh_shared_ptr_copy_move.cpp new file mode 100644 index 00000000..889425a0 --- /dev/null +++ b/external_libraries/pybind11/tests/test_class_sh_shared_ptr_copy_move.cpp @@ -0,0 +1,103 @@ +#include "pybind11_tests.h" + +#include +#include +#include + +namespace pybind11_tests { +namespace { + +const std::string fooNames[] = {"ShPtr_", "SmHld_"}; + +template +struct Foo { + std::string history; + explicit Foo(const std::string &history_) : history(history_) {} + Foo(const Foo &other) : history(other.history + "_CpCtor") {} + Foo(Foo &&other) noexcept : history(other.history + "_MvCtor") {} + Foo &operator=(const Foo &other) { + history = other.history + "_OpEqLv"; + return *this; + } + Foo &operator=(Foo &&other) noexcept { + history = other.history + "_OpEqRv"; + return *this; + } + std::string get_history() const { return "Foo" + fooNames[SerNo] + history; } +}; + +using FooShPtr = Foo<0>; +using FooSmHld = Foo<1>; + +struct Outer { + std::shared_ptr ShPtr; + std::shared_ptr SmHld; + Outer() + : ShPtr(std::make_shared("Outer")), SmHld(std::make_shared("Outer")) {} + std::shared_ptr getShPtr() const { return ShPtr; } + std::shared_ptr getSmHld() const { return SmHld; } +}; + +} // namespace + +TEST_SUBMODULE(class_sh_shared_ptr_copy_move, m) { + namespace py = pybind11; + + py::class_>(m, "FooShPtr") + .def("get_history", &FooShPtr::get_history); + py::classh(m, "FooSmHld").def("get_history", &FooSmHld::get_history); + + auto outer = py::class_(m, "Outer").def(py::init()); +#define MAKE_PROP(PropTyp) \ + MAKE_PROP_FOO(ShPtr, PropTyp) \ + MAKE_PROP_FOO(SmHld, PropTyp) + +#define MAKE_PROP_FOO(FooTyp, PropTyp) \ + .def_##PropTyp(#FooTyp "_" #PropTyp "_default", &Outer::FooTyp) \ + .def_##PropTyp( \ + #FooTyp "_" #PropTyp "_copy", &Outer::FooTyp, py::return_value_policy::copy) \ + .def_##PropTyp( \ + #FooTyp "_" #PropTyp "_move", &Outer::FooTyp, py::return_value_policy::move) + outer MAKE_PROP(readonly) MAKE_PROP(readwrite); +#undef MAKE_PROP_FOO + +#define MAKE_PROP_FOO(FooTyp, PropTyp) \ + .def_##PropTyp(#FooTyp "_property_" #PropTyp "_default", &Outer::FooTyp) \ + .def_property_##PropTyp(#FooTyp "_property_" #PropTyp "_copy", \ + &Outer::get##FooTyp, \ + py::return_value_policy::copy) \ + .def_property_##PropTyp(#FooTyp "_property_" #PropTyp "_move", \ + &Outer::get##FooTyp, \ + py::return_value_policy::move) + outer MAKE_PROP(readonly); +#undef MAKE_PROP_FOO +#undef MAKE_PROP + + m.def("test_ShPtr_copy", []() { + auto o = std::make_shared("copy"); + auto l = py::list(); + l.append(o); + return l; + }); + m.def("test_SmHld_copy", []() { + auto o = std::make_shared("copy"); + auto l = py::list(); + l.append(o); + return l; + }); + + m.def("test_ShPtr_move", []() { + auto o = std::make_shared("move"); + auto l = py::list(); + l.append(std::move(o)); + return l; + }); + m.def("test_SmHld_move", []() { + auto o = std::make_shared("move"); + auto l = py::list(); + l.append(std::move(o)); + return l; + }); +} + +} // namespace pybind11_tests diff --git a/external_libraries/pybind11/tests/test_class_sh_shared_ptr_copy_move.py b/external_libraries/pybind11/tests/test_class_sh_shared_ptr_copy_move.py new file mode 100644 index 00000000..067bb47d --- /dev/null +++ b/external_libraries/pybind11/tests/test_class_sh_shared_ptr_copy_move.py @@ -0,0 +1,41 @@ +from __future__ import annotations + +from pybind11_tests import class_sh_shared_ptr_copy_move as m + + +def test_shptr_copy(): + txt = m.test_ShPtr_copy()[0].get_history() + assert txt == "FooShPtr_copy" + + +def test_smhld_copy(): + txt = m.test_SmHld_copy()[0].get_history() + assert txt == "FooSmHld_copy" + + +def test_shptr_move(): + txt = m.test_ShPtr_move()[0].get_history() + assert txt == "FooShPtr_move" + + +def test_smhld_move(): + txt = m.test_SmHld_move()[0].get_history() + assert txt == "FooSmHld_move" + + +def _check_property(foo_typ, prop_typ, policy): + o = m.Outer() + name = f"{foo_typ}_{prop_typ}_{policy}" + history = f"Foo{foo_typ}_Outer" + f = getattr(o, name) + assert f.get_history() == history + # and try again to check that o did not get changed + f = getattr(o, name) + assert f.get_history() == history + + +def test_properties(): + for prop_typ in ("readonly", "readwrite", "property_readonly"): + for foo_typ in ("ShPtr", "SmHld"): + for policy in ("default", "copy", "move"): + _check_property(foo_typ, prop_typ, policy) diff --git a/external_libraries/pybind11/tests/test_class_sh_trampoline_basic.cpp b/external_libraries/pybind11/tests/test_class_sh_trampoline_basic.cpp new file mode 100644 index 00000000..1ceb11ba --- /dev/null +++ b/external_libraries/pybind11/tests/test_class_sh_trampoline_basic.cpp @@ -0,0 +1,57 @@ +#include "pybind11_tests.h" + +#include + +namespace pybind11_tests { +namespace class_sh_trampoline_basic { + +struct Abase { + int val = 0; + virtual ~Abase() = default; + explicit Abase(int val_) : val{val_} {} + int Get() const { return val * 10 + 3; } + virtual int Add(int other_val) const = 0; + + // Some compilers complain about implicitly defined versions of some of the following: + Abase(const Abase &) = default; + Abase(Abase &&) noexcept = default; + Abase &operator=(const Abase &) = default; + Abase &operator=(Abase &&) noexcept = default; +}; + +struct AbaseAlias : Abase, py::trampoline_self_life_support { + using Abase::Abase; + + int Add(int other_val) const override { + PYBIND11_OVERRIDE_PURE(int, /* Return type */ + Abase, /* Parent class */ + Add, /* Name of function in C++ (must match Python name) */ + other_val); + } +}; + +int AddInCppRawPtr(const Abase *obj, int other_val) { return obj->Add(other_val) * 10 + 7; } + +int AddInCppSharedPtr(const std::shared_ptr &obj, int other_val) { + return obj->Add(other_val) * 100 + 11; +} + +int AddInCppUniquePtr(std::unique_ptr obj, int other_val) { + return obj->Add(other_val) * 100 + 13; +} + +} // namespace class_sh_trampoline_basic +} // namespace pybind11_tests + +using namespace pybind11_tests::class_sh_trampoline_basic; + +TEST_SUBMODULE(class_sh_trampoline_basic, m) { + py::classh(m, "Abase") + .def(py::init(), py::arg("val")) + .def("Get", &Abase::Get) + .def("Add", &Abase::Add, py::arg("other_val")); + + m.def("AddInCppRawPtr", AddInCppRawPtr, py::arg("obj"), py::arg("other_val")); + m.def("AddInCppSharedPtr", AddInCppSharedPtr, py::arg("obj"), py::arg("other_val")); + m.def("AddInCppUniquePtr", AddInCppUniquePtr, py::arg("obj"), py::arg("other_val")); +} diff --git a/external_libraries/pybind11/tests/test_class_sh_trampoline_basic.py b/external_libraries/pybind11/tests/test_class_sh_trampoline_basic.py new file mode 100644 index 00000000..0020a14a --- /dev/null +++ b/external_libraries/pybind11/tests/test_class_sh_trampoline_basic.py @@ -0,0 +1,35 @@ +from __future__ import annotations + +from pybind11_tests import class_sh_trampoline_basic as m + + +class PyDrvd(m.Abase): + def __init__(self, val): + super().__init__(val) + + def Add(self, other_val): + return self.Get() * 100 + other_val + + +def test_drvd_add(): + drvd = PyDrvd(74) + assert drvd.Add(38) == (74 * 10 + 3) * 100 + 38 + + +def test_drvd_add_in_cpp_raw_ptr(): + drvd = PyDrvd(52) + assert m.AddInCppRawPtr(drvd, 27) == ((52 * 10 + 3) * 100 + 27) * 10 + 7 + + +def test_drvd_add_in_cpp_shared_ptr(): + while True: + drvd = PyDrvd(36) + assert m.AddInCppSharedPtr(drvd, 56) == ((36 * 10 + 3) * 100 + 56) * 100 + 11 + return # Comment out for manual leak checking (use `top` command). + + +def test_drvd_add_in_cpp_unique_ptr(): + while True: + drvd = PyDrvd(25) + assert m.AddInCppUniquePtr(drvd, 83) == ((25 * 10 + 3) * 100 + 83) * 100 + 13 + return # Comment out for manual leak checking (use `top` command). diff --git a/external_libraries/pybind11/tests/test_class_sh_trampoline_self_life_support.cpp b/external_libraries/pybind11/tests/test_class_sh_trampoline_self_life_support.cpp new file mode 100644 index 00000000..22b728e2 --- /dev/null +++ b/external_libraries/pybind11/tests/test_class_sh_trampoline_self_life_support.cpp @@ -0,0 +1,86 @@ +// Copyright (c) 2021 The Pybind Development Team. +// All rights reserved. Use of this source code is governed by a +// BSD-style license that can be found in the LICENSE file. + +#include "pybind11/trampoline_self_life_support.h" +#include "pybind11_tests.h" + +#include +#include +#include + +namespace pybind11_tests { +namespace class_sh_trampoline_self_life_support { + +struct Big5 { // Also known as "rule of five". + std::string history; + + explicit Big5(std::string history_start) : history{std::move(history_start)} {} + + Big5(const Big5 &other) { history = other.history + "_CpCtor"; } + + Big5(Big5 &&other) noexcept { history = other.history + "_MvCtor"; } + + Big5 &operator=(const Big5 &other) { + history = other.history + "_OpEqLv"; + return *this; + } + + Big5 &operator=(Big5 &&other) noexcept { + history = other.history + "_OpEqRv"; + return *this; + } + + virtual ~Big5() = default; + +protected: + Big5() : history{"DefaultConstructor"} {} +}; + +struct Big5Trampoline : Big5, py::trampoline_self_life_support { + using Big5::Big5; +}; + +} // namespace class_sh_trampoline_self_life_support +} // namespace pybind11_tests + +using namespace pybind11_tests::class_sh_trampoline_self_life_support; + +TEST_SUBMODULE(class_sh_trampoline_self_life_support, m) { + py::classh(m, "Big5") + .def(py::init()) + .def_readonly("history", &Big5::history); + + m.def("action", [](std::unique_ptr obj, int action_id) { + py::object o2 = py::none(); + // This is very unusual, but needed to directly exercise the trampoline_self_life_support + // CpCtor, MvCtor, operator= lvalue, operator= rvalue. + auto *obj_trampoline = dynamic_cast(obj.get()); + if (obj_trampoline != nullptr) { + switch (action_id) { + case 0: { // CpCtor + std::unique_ptr cp(new Big5Trampoline(*obj_trampoline)); + o2 = py::cast(std::move(cp)); + } break; + case 1: { // MvCtor + std::unique_ptr mv(new Big5Trampoline(std::move(*obj_trampoline))); + o2 = py::cast(std::move(mv)); + } break; + case 2: { // operator= lvalue + std::unique_ptr lv(new Big5Trampoline); + *lv = *obj_trampoline; // NOLINT clang-tidy cppcoreguidelines-slicing + o2 = py::cast(std::move(lv)); + } break; + case 3: { // operator= rvalue + std::unique_ptr rv(new Big5Trampoline); + *rv = std::move(*obj_trampoline); + o2 = py::cast(std::move(rv)); + } break; + default: + break; + } + } + py::object o1 = py::cast(std::move(obj)); + return py::make_tuple(o1, o2); + }); +} diff --git a/external_libraries/pybind11/tests/test_class_sh_trampoline_self_life_support.py b/external_libraries/pybind11/tests/test_class_sh_trampoline_self_life_support.py new file mode 100644 index 00000000..d4af2ab9 --- /dev/null +++ b/external_libraries/pybind11/tests/test_class_sh_trampoline_self_life_support.py @@ -0,0 +1,38 @@ +from __future__ import annotations + +import pytest + +import pybind11_tests.class_sh_trampoline_self_life_support as m + + +class PyBig5(m.Big5): + pass + + +def test_m_big5(): + obj = m.Big5("Seed") + assert obj.history == "Seed" + o1, o2 = m.action(obj, 0) + assert o1 is not obj + assert o1.history == "Seed" + with pytest.raises(ValueError) as excinfo: + _ = obj.history + assert "Python instance was disowned" in str(excinfo.value) + assert o2 is None + + +@pytest.mark.parametrize( + ("action_id", "expected_history"), + [ + (0, "Seed_CpCtor"), + (1, "Seed_MvCtor"), + (2, "Seed_OpEqLv"), + (3, "Seed_OpEqRv"), + ], +) +def test_py_big5(action_id, expected_history): + obj = PyBig5("Seed") + assert obj.history == "Seed" + o1, o2 = m.action(obj, action_id) + assert o1 is obj + assert o2.history == expected_history diff --git a/external_libraries/pybind11/tests/test_class_sh_trampoline_shared_from_this.cpp b/external_libraries/pybind11/tests/test_class_sh_trampoline_shared_from_this.cpp new file mode 100644 index 00000000..dc6bf1c7 --- /dev/null +++ b/external_libraries/pybind11/tests/test_class_sh_trampoline_shared_from_this.cpp @@ -0,0 +1,137 @@ +// Copyright (c) 2021 The Pybind Development Team. +// All rights reserved. Use of this source code is governed by a +// BSD-style license that can be found in the LICENSE file. + +#include "pybind11_tests.h" + +#include +#include + +namespace pybind11_tests { +namespace class_sh_trampoline_shared_from_this { + +struct Sft : std::enable_shared_from_this { + std::string history; + explicit Sft(const std::string &history_seed) : history{history_seed} {} + virtual ~Sft() = default; + +#if defined(__clang__) + // "Group of 4" begin. + // This group is not meant to be used, but will leave a trace in the + // 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) : enable_shared_from_this(other) { history = other.history + "_CpCtor"; } + + Sft(Sft &&other) noexcept { history = other.history + "_MvCtor"; } + + Sft &operator=(const Sft &other) { + history = other.history + "_OpEqLv"; + return *this; + } + + Sft &operator=(Sft &&other) noexcept { + history = other.history + "_OpEqRv"; + return *this; + } + // "Group of 4" end. +#endif +}; + +struct SftSharedPtrStash { + int ser_no; + std::vector> stash; + explicit SftSharedPtrStash(int ser_no) : ser_no{ser_no} {} + void Clear() { stash.clear(); } + void Add(const std::shared_ptr &obj) { + if (!obj->history.empty()) { + obj->history += "_Stash" + std::to_string(ser_no) + "Add"; + } + stash.push_back(obj); + } + void AddSharedFromThis(Sft *obj) { + auto sft = obj->shared_from_this(); + if (!sft->history.empty()) { + sft->history += "_Stash" + std::to_string(ser_no) + "AddSharedFromThis"; + } + stash.push_back(sft); + } + std::string history(unsigned i) { + if (i < stash.size()) { + return stash[i]->history; + } + return "OutOfRange"; + } + long use_count(unsigned i) { + if (i < stash.size()) { + return stash[i].use_count(); + } + return -1; + } +}; + +struct SftTrampoline : Sft, py::trampoline_self_life_support { + using Sft::Sft; +}; + +long use_count(const std::shared_ptr &obj) { return obj.use_count(); } + +long pass_shared_ptr(const std::shared_ptr &obj) { + auto sft = obj->shared_from_this(); + if (!sft->history.empty()) { + sft->history += "_PassSharedPtr"; + } + return sft.use_count(); +} + +std::string pass_unique_ptr_cref(const std::unique_ptr &obj) { + return obj ? obj->history : ""; +} +void pass_unique_ptr_rref(std::unique_ptr &&) { + throw std::runtime_error("Expected to not be reached."); +} + +Sft *make_pure_cpp_sft_raw_ptr(const std::string &history_seed) { return new Sft{history_seed}; } + +std::unique_ptr make_pure_cpp_sft_unq_ptr(const std::string &history_seed) { + return std::unique_ptr(new Sft{history_seed}); +} + +std::shared_ptr make_pure_cpp_sft_shd_ptr(const std::string &history_seed) { + return std::make_shared(history_seed); +} + +std::shared_ptr pass_through_shd_ptr(const std::shared_ptr &obj) { return obj; } + +} // namespace class_sh_trampoline_shared_from_this +} // namespace pybind11_tests + +using namespace pybind11_tests::class_sh_trampoline_shared_from_this; + +TEST_SUBMODULE(class_sh_trampoline_shared_from_this, m) { + py::classh(m, "Sft") + .def(py::init()) + .def(py::init([](const std::string &history, int) { + return std::make_shared(history); + })) + .def_readonly("history", &Sft::history) + // This leads to multiple entries in registered_instances: + .def(py::init([](const std::shared_ptr &existing) { return existing; })); + + py::classh(m, "SftSharedPtrStash") + .def(py::init()) + .def("Clear", &SftSharedPtrStash::Clear) + .def("Add", &SftSharedPtrStash::Add) + .def("AddSharedFromThis", &SftSharedPtrStash::AddSharedFromThis) + .def("history", &SftSharedPtrStash::history) + .def("use_count", &SftSharedPtrStash::use_count); + + m.def("use_count", use_count); + m.def("pass_shared_ptr", pass_shared_ptr); + m.def("pass_unique_ptr_cref", pass_unique_ptr_cref); + m.def("pass_unique_ptr_rref", pass_unique_ptr_rref); + m.def("make_pure_cpp_sft_raw_ptr", make_pure_cpp_sft_raw_ptr); + m.def("make_pure_cpp_sft_unq_ptr", make_pure_cpp_sft_unq_ptr); + m.def("make_pure_cpp_sft_shd_ptr", make_pure_cpp_sft_shd_ptr); + m.def("pass_through_shd_ptr", pass_through_shd_ptr); +} diff --git a/external_libraries/pybind11/tests/test_class_sh_trampoline_shared_from_this.py b/external_libraries/pybind11/tests/test_class_sh_trampoline_shared_from_this.py new file mode 100644 index 00000000..fbe31387 --- /dev/null +++ b/external_libraries/pybind11/tests/test_class_sh_trampoline_shared_from_this.py @@ -0,0 +1,247 @@ +from __future__ import annotations + +import sys +import weakref + +import pytest + +import env +import pybind11_tests.class_sh_trampoline_shared_from_this as m + + +class PySft(m.Sft): + pass + + +def test_release_and_shared_from_this(): + # Exercises the most direct path from building a shared_from_this-visible + # shared_ptr to calling shared_from_this. + obj = PySft("PySft") + assert obj.history == "PySft" + assert m.use_count(obj) == 1 + assert m.pass_shared_ptr(obj) == 2 + assert obj.history == "PySft_PassSharedPtr" + assert m.use_count(obj) == 1 + assert m.pass_shared_ptr(obj) == 2 + assert obj.history == "PySft_PassSharedPtr_PassSharedPtr" + assert m.use_count(obj) == 1 + + +def test_release_and_shared_from_this_leak(): + obj = PySft("") + while True: + m.pass_shared_ptr(obj) + assert not obj.history + assert m.use_count(obj) == 1 + break # Comment out for manual leak checking (use `top` command). + + +def test_release_and_stash(): + # Exercises correct functioning of guarded_delete weak_ptr. + obj = PySft("PySft") + stash1 = m.SftSharedPtrStash(1) + stash1.Add(obj) + exp_hist = "PySft_Stash1Add" + assert obj.history == exp_hist + assert m.use_count(obj) == 2 + assert stash1.history(0) == exp_hist + assert stash1.use_count(0) == 1 + assert m.pass_shared_ptr(obj) == 3 + exp_hist += "_PassSharedPtr" + assert obj.history == exp_hist + assert m.use_count(obj) == 2 + assert stash1.history(0) == exp_hist + assert stash1.use_count(0) == 1 + stash2 = m.SftSharedPtrStash(2) + stash2.Add(obj) + exp_hist += "_Stash2Add" + assert obj.history == exp_hist + assert m.use_count(obj) == 3 + assert stash2.history(0) == exp_hist + assert stash2.use_count(0) == 2 + stash2.Add(obj) + exp_hist += "_Stash2Add" + assert obj.history == exp_hist + assert m.use_count(obj) == 4 + assert stash1.history(0) == exp_hist + assert stash1.use_count(0) == 3 + assert stash2.history(0) == exp_hist + assert stash2.use_count(0) == 3 + assert stash2.history(1) == exp_hist + assert stash2.use_count(1) == 3 + del obj + assert stash2.history(0) == exp_hist + assert stash2.use_count(0) == 3 + assert stash2.history(1) == exp_hist + assert stash2.use_count(1) == 3 + stash2.Clear() + assert stash1.history(0) == exp_hist + assert stash1.use_count(0) == 1 + + +def test_release_and_stash_leak(): + obj = PySft("") + while True: + stash1 = m.SftSharedPtrStash(1) + stash1.Add(obj) + assert not obj.history + assert m.use_count(obj) == 2 + assert stash1.use_count(0) == 1 + stash1.Add(obj) + assert not obj.history + assert m.use_count(obj) == 3 + assert stash1.use_count(0) == 2 + assert stash1.use_count(1) == 2 + break # Comment out for manual leak checking (use `top` command). + + +def test_release_and_stash_via_shared_from_this(): + # Exercises that the smart_holder vptr is invisible to the shared_from_this mechanism. + obj = PySft("PySft") + stash1 = m.SftSharedPtrStash(1) + with pytest.raises(RuntimeError) as exc_info: + stash1.AddSharedFromThis(obj) + assert str(exc_info.value) == "bad_weak_ptr" + stash1.Add(obj) + assert obj.history == "PySft_Stash1Add" + assert stash1.use_count(0) == 1 + stash1.AddSharedFromThis(obj) + assert obj.history == "PySft_Stash1Add_Stash1AddSharedFromThis" + assert stash1.use_count(0) == 2 + assert stash1.use_count(1) == 2 + + +def test_release_and_stash_via_shared_from_this_leak(): + obj = PySft("") + while True: + stash1 = m.SftSharedPtrStash(1) + with pytest.raises(RuntimeError) as exc_info: + stash1.AddSharedFromThis(obj) + assert str(exc_info.value) == "bad_weak_ptr" + stash1.Add(obj) + assert not obj.history + assert stash1.use_count(0) == 1 + stash1.AddSharedFromThis(obj) + assert not obj.history + assert stash1.use_count(0) == 2 + assert stash1.use_count(1) == 2 + break # Comment out for manual leak checking (use `top` command). + + +def test_pass_released_shared_ptr_as_unique_ptr(): + # Exercises that returning a unique_ptr fails while a shared_from_this + # visible shared_ptr exists. + obj = PySft("PySft") + stash1 = m.SftSharedPtrStash(1) + stash1.Add(obj) # Releases shared_ptr to C++. + assert m.pass_unique_ptr_cref(obj) == "PySft_Stash1Add" + assert obj.history == "PySft_Stash1Add" + with pytest.raises(ValueError) as exc_info: + m.pass_unique_ptr_rref(obj) + assert str(exc_info.value) == ( + "Python instance is currently owned by a std::shared_ptr." + ) + assert obj.history == "PySft_Stash1Add" + + +@pytest.mark.parametrize( + "make_f", + [ + m.make_pure_cpp_sft_raw_ptr, + m.make_pure_cpp_sft_unq_ptr, + m.make_pure_cpp_sft_shd_ptr, + ], +) +def test_pure_cpp_sft_raw_ptr(make_f): + # Exercises void_cast_raw_ptr logic for different situations. + obj = make_f("PureCppSft") + assert m.pass_shared_ptr(obj) == 3 + assert obj.history == "PureCppSft_PassSharedPtr" + obj = make_f("PureCppSft") + stash1 = m.SftSharedPtrStash(1) + stash1.AddSharedFromThis(obj) + assert obj.history == "PureCppSft_Stash1AddSharedFromThis" + + +def test_multiple_registered_instances_for_same_pointee(): + obj0 = PySft("PySft") + obj0.attachment_in_dict = "Obj0" + assert m.pass_through_shd_ptr(obj0) is obj0 + while True: + obj = m.Sft(obj0) + assert obj is not obj0 + obj_pt = m.pass_through_shd_ptr(obj) + # Unpredictable! Because registered_instances is as std::unordered_multimap. + assert obj_pt is obj0 or obj_pt is obj + # Multiple registered_instances for the same pointee can lead to unpredictable results: + if obj_pt is obj0: + assert obj_pt.attachment_in_dict == "Obj0" + else: + assert not hasattr(obj_pt, "attachment_in_dict") + assert obj0.history == "PySft" + break # Comment out for manual leak checking (use `top` command). + + +def test_multiple_registered_instances_for_same_pointee_leak(): + obj0 = PySft("") + while True: + stash1 = m.SftSharedPtrStash(1) + stash1.Add(m.Sft(obj0)) + assert stash1.use_count(0) == 1 + stash1.Add(m.Sft(obj0)) + assert stash1.use_count(0) == 1 + assert stash1.use_count(1) == 1 + assert not obj0.history + break # Comment out for manual leak checking (use `top` command). + + +def test_multiple_registered_instances_for_same_pointee_recursive(): + while True: + obj0 = PySft("PySft") + if not env.PYPY: + obj0_wr = weakref.ref(obj0) + obj = obj0 + # This loop creates a chain of instances linked by shared_ptrs. + for _ in range(10): + obj_next = m.Sft(obj) + assert obj_next is not obj + obj = obj_next + del obj_next + assert obj.history == "PySft" + del obj0 + if not env.PYPY and not env.GRAALPY: + assert obj0_wr() is not None + del obj # This releases the chain recursively. + if not env.PYPY and not env.GRAALPY: + assert obj0_wr() is None + break # Comment out for manual leak checking (use `top` command). + + +# As of 2021-07-10 the pybind11 GitHub Actions valgrind build uses Python 3.9. +WORKAROUND_ENABLING_ROLLBACK_OF_PR3068 = env.LINUX and sys.version_info == (3, 9) + + +def test_std_make_shared_factory(): + class PySftMakeShared(m.Sft): + def __init__(self, history): + super().__init__(history, 0) + + obj = PySftMakeShared("PySftMakeShared") + assert obj.history == "PySftMakeShared" + if WORKAROUND_ENABLING_ROLLBACK_OF_PR3068: + try: + m.pass_through_shd_ptr(obj) + except RuntimeError as e: + str_exc_info_value = str(e) + else: + str_exc_info_value = "RuntimeError NOT RAISED" + else: + with pytest.raises(RuntimeError) as exc_info: + m.pass_through_shd_ptr(obj) + str_exc_info_value = str(exc_info.value) + assert ( + str_exc_info_value + == "smart_holder_type_casters load_as_shared_ptr failure: not implemented:" + " trampoline-self-life-support for external shared_ptr to type inheriting" + " from std::enable_shared_from_this." + ) diff --git a/external_libraries/pybind11/tests/test_class_sh_trampoline_shared_ptr_cpp_arg.cpp b/external_libraries/pybind11/tests/test_class_sh_trampoline_shared_ptr_cpp_arg.cpp new file mode 100644 index 00000000..6936379c --- /dev/null +++ b/external_libraries/pybind11/tests/test_class_sh_trampoline_shared_ptr_cpp_arg.cpp @@ -0,0 +1,92 @@ +// Copyright (c) 2021 The Pybind Development Team. +// All rights reserved. Use of this source code is governed by a +// BSD-style license that can be found in the LICENSE file. + +#include "pybind11_tests.h" + +#include + +namespace pybind11_tests { +namespace class_sh_trampoline_shared_ptr_cpp_arg { + +// For testing whether a python subclass of a C++ object dies when the +// last python reference is lost +struct SpBase { + // returns true if the base virtual function is called + virtual bool is_base_used() { return true; } + + // returns true if there's an associated python instance + bool has_python_instance() { + auto *tinfo = py::detail::get_type_info(typeid(SpBase)); + return (bool) py::detail::get_object_handle(this, tinfo); + } + + SpBase() = default; + SpBase(const SpBase &) = delete; + virtual ~SpBase() = default; +}; + +std::shared_ptr pass_through_shd_ptr(const std::shared_ptr &obj) { return obj; } + +struct PySpBase : SpBase, py::trampoline_self_life_support { + using SpBase::SpBase; + bool is_base_used() override { PYBIND11_OVERRIDE(bool, SpBase, is_base_used); } +}; + +struct SpBaseTester { + std::shared_ptr get_object() const { return m_obj; } + void set_object(std::shared_ptr obj) { m_obj = std::move(obj); } + bool is_base_used() const { return m_obj->is_base_used(); } + bool has_instance() const { return (bool) m_obj; } + bool has_python_instance() const { return m_obj && m_obj->has_python_instance(); } + void set_nonpython_instance() { m_obj = std::make_shared(); } + std::shared_ptr m_obj; +}; + +// For testing that a C++ class without an alias does not retain the python +// portion of the object +struct SpGoAway {}; + +struct SpGoAwayTester { + std::shared_ptr m_obj; +}; + +} // namespace class_sh_trampoline_shared_ptr_cpp_arg +} // namespace pybind11_tests + +using namespace pybind11_tests::class_sh_trampoline_shared_ptr_cpp_arg; + +TEST_SUBMODULE(class_sh_trampoline_shared_ptr_cpp_arg, m) { + // For testing whether a python subclass of a C++ object dies when the + // last python reference is lost + + py::classh(m, "SpBase") + .def(py::init<>()) + .def(py::init([](int) { return std::make_shared(); })) + .def("is_base_used", &SpBase::is_base_used) + .def("has_python_instance", &SpBase::has_python_instance); + + m.def("pass_through_shd_ptr", pass_through_shd_ptr); + m.def("pass_through_shd_ptr_release_gil", + pass_through_shd_ptr, + py::call_guard()); // PR #4196 + + py::classh(m, "SpBaseTester") + .def(py::init<>()) + .def("get_object", &SpBaseTester::get_object) + .def("set_object", &SpBaseTester::set_object) + .def("is_base_used", &SpBaseTester::is_base_used) + .def("has_instance", &SpBaseTester::has_instance) + .def("has_python_instance", &SpBaseTester::has_python_instance) + .def("set_nonpython_instance", &SpBaseTester::set_nonpython_instance) + .def_readwrite("obj", &SpBaseTester::m_obj); + + // For testing that a C++ class without an alias does not retain the python + // portion of the object + + py::classh(m, "SpGoAway").def(py::init<>()); + + py::classh(m, "SpGoAwayTester") + .def(py::init<>()) + .def_readwrite("obj", &SpGoAwayTester::m_obj); +} diff --git a/external_libraries/pybind11/tests/test_class_sh_trampoline_shared_ptr_cpp_arg.py b/external_libraries/pybind11/tests/test_class_sh_trampoline_shared_ptr_cpp_arg.py new file mode 100644 index 00000000..a693621e --- /dev/null +++ b/external_libraries/pybind11/tests/test_class_sh_trampoline_shared_ptr_cpp_arg.py @@ -0,0 +1,154 @@ +from __future__ import annotations + +import pytest + +import env # noqa: F401 +import pybind11_tests.class_sh_trampoline_shared_ptr_cpp_arg as m + + +@pytest.mark.skipif("env.GRAALPY", reason="Cannot reliably trigger GC") +def test_shared_ptr_cpp_arg(): + import weakref + + class PyChild(m.SpBase): + def is_base_used(self): + return False + + tester = m.SpBaseTester() + + obj = PyChild() + objref = weakref.ref(obj) + + # Pass the last python reference to the C++ function + tester.set_object(obj) + del obj + pytest.gc_collect() + + # python reference is still around since C++ has it now + assert objref() is not None + assert tester.is_base_used() is False + assert tester.obj.is_base_used() is False + assert tester.get_object() is objref() + + +@pytest.mark.skipif("env.GRAALPY", reason="Cannot reliably trigger GC") +def test_shared_ptr_cpp_prop(): + class PyChild(m.SpBase): + def is_base_used(self): + return False + + tester = m.SpBaseTester() + + # Set the last python reference as a property of the C++ object + tester.obj = PyChild() + pytest.gc_collect() + + # python reference is still around since C++ has it now + assert tester.is_base_used() is False + assert tester.has_python_instance() is True + assert tester.obj.is_base_used() is False + assert tester.obj.has_python_instance() is True + + +@pytest.mark.skipif("env.GRAALPY", reason="Cannot reliably trigger GC") +def test_shared_ptr_arg_identity(): + import weakref + + tester = m.SpBaseTester() + + obj = m.SpBase() + objref = weakref.ref(obj) + + tester.set_object(obj) + del obj + pytest.gc_collect() + + # NOTE: the behavior below is DIFFERENT from PR #2839 + # python reference is gone because it is not an Alias instance + assert objref() is None + assert tester.has_python_instance() is False + + +@pytest.mark.skipif("env.GRAALPY", reason="Cannot reliably trigger GC") +def test_shared_ptr_alias_nonpython(): + tester = m.SpBaseTester() + + # C++ creates the object, a python instance shouldn't exist + tester.set_nonpython_instance() + assert tester.is_base_used() is True + assert tester.has_instance() is True + assert tester.has_python_instance() is False + + # Now a python instance exists + cobj = tester.get_object() + assert cobj.has_python_instance() + assert tester.has_instance() is True + assert tester.has_python_instance() is True + + # Now it's gone + del cobj + pytest.gc_collect() + assert tester.has_instance() is True + assert tester.has_python_instance() is False + + # When we pass it as an arg to a new tester the python instance should + # disappear because it wasn't created with an alias + new_tester = m.SpBaseTester() + + cobj = tester.get_object() + assert cobj.has_python_instance() + + new_tester.set_object(cobj) + assert tester.has_python_instance() is True + assert new_tester.has_python_instance() is True + + del cobj + pytest.gc_collect() + + # Gone! + assert tester.has_instance() is True + assert tester.has_python_instance() is False + assert new_tester.has_instance() is True + assert new_tester.has_python_instance() is False + + +@pytest.mark.skipif("env.GRAALPY", reason="Cannot reliably trigger GC") +def test_shared_ptr_goaway(): + import weakref + + tester = m.SpGoAwayTester() + + obj = m.SpGoAway() + objref = weakref.ref(obj) + + assert tester.obj is None + + tester.obj = obj + del obj + pytest.gc_collect() + + # python reference is no longer around + assert objref() is None + # C++ reference is still around + assert tester.obj is not None + + +def test_infinite(): + tester = m.SpBaseTester() + while True: + tester.set_object(m.SpBase()) + break # Comment out for manual leak checking (use `top` command). + + +@pytest.mark.parametrize( + "pass_through_func", [m.pass_through_shd_ptr, m.pass_through_shd_ptr_release_gil] +) +def test_std_make_shared_factory(pass_through_func): + class PyChild(m.SpBase): + def __init__(self): + super().__init__(0) + + obj = PyChild() + while True: + assert pass_through_func(obj) is obj + break # Comment out for manual leak checking (use `top` command). diff --git a/external_libraries/pybind11/tests/test_class_sh_trampoline_unique_ptr.cpp b/external_libraries/pybind11/tests/test_class_sh_trampoline_unique_ptr.cpp new file mode 100644 index 00000000..debe3324 --- /dev/null +++ b/external_libraries/pybind11/tests/test_class_sh_trampoline_unique_ptr.cpp @@ -0,0 +1,63 @@ +// Copyright (c) 2021 The Pybind Development Team. +// All rights reserved. Use of this source code is governed by a +// BSD-style license that can be found in the LICENSE file. + +#include "pybind11/trampoline_self_life_support.h" +#include "pybind11_tests.h" + +#include + +namespace pybind11_tests { +namespace class_sh_trampoline_unique_ptr { + +class Class { +public: + virtual ~Class() = default; + + void setVal(std::uint64_t val) { val_ = val; } + std::uint64_t getVal() const { return val_; } + + virtual std::unique_ptr clone() const = 0; + virtual int foo() const = 0; + +protected: + Class() = default; + + // Some compilers complain about implicitly defined versions of some of the following: + Class(const Class &) = default; + +private: + std::uint64_t val_ = 0; +}; + +} // namespace class_sh_trampoline_unique_ptr +} // namespace pybind11_tests + +namespace pybind11_tests { +namespace class_sh_trampoline_unique_ptr { + +class PyClass : public Class, public py::trampoline_self_life_support { +public: + std::unique_ptr clone() const override { + PYBIND11_OVERRIDE_PURE(std::unique_ptr, Class, clone); + } + + int foo() const override { PYBIND11_OVERRIDE_PURE(int, Class, foo); } +}; + +} // namespace class_sh_trampoline_unique_ptr +} // namespace pybind11_tests + +TEST_SUBMODULE(class_sh_trampoline_unique_ptr, m) { + using namespace pybind11_tests::class_sh_trampoline_unique_ptr; + + py::classh(m, "Class") + .def(py::init<>()) + .def("set_val", &Class::setVal) + .def("get_val", &Class::getVal) + .def("clone", &Class::clone) + .def("foo", &Class::foo); + + m.def("clone", [](const Class &obj) { return obj.clone(); }); + m.def("clone_and_foo", [](const Class &obj) { return obj.clone()->foo(); }); +} diff --git a/external_libraries/pybind11/tests/test_class_sh_trampoline_unique_ptr.py b/external_libraries/pybind11/tests/test_class_sh_trampoline_unique_ptr.py new file mode 100644 index 00000000..7799df6d --- /dev/null +++ b/external_libraries/pybind11/tests/test_class_sh_trampoline_unique_ptr.py @@ -0,0 +1,31 @@ +from __future__ import annotations + +import pybind11_tests.class_sh_trampoline_unique_ptr as m + + +class MyClass(m.Class): + def foo(self): + return 10 + self.get_val() + + def clone(self): + cloned = MyClass() + cloned.set_val(self.get_val() + 3) + return cloned + + +def test_m_clone(): + obj = MyClass() + while True: + obj.set_val(5) + obj = m.clone(obj) + assert obj.get_val() == 5 + 3 + assert obj.foo() == 10 + 5 + 3 + return # Comment out for manual leak checking (use `top` command). + + +def test_m_clone_and_foo(): + obj = MyClass() + obj.set_val(7) + while True: + assert m.clone_and_foo(obj) == 10 + 7 + 3 + return # Comment out for manual leak checking (use `top` command). diff --git a/external_libraries/pybind11/tests/test_class_sh_unique_ptr_custom_deleter.cpp b/external_libraries/pybind11/tests/test_class_sh_unique_ptr_custom_deleter.cpp new file mode 100644 index 00000000..adaa2e47 --- /dev/null +++ b/external_libraries/pybind11/tests/test_class_sh_unique_ptr_custom_deleter.cpp @@ -0,0 +1,30 @@ +#include "pybind11_tests.h" + +#include + +namespace pybind11_tests { +namespace class_sh_unique_ptr_custom_deleter { + +// Reduced from a PyCLIF use case in the wild by @wangxf123456. +class Pet { +public: + using Ptr = std::unique_ptr>; + + std::string name; + + static Ptr New(const std::string &name) { + return Ptr(new Pet(name), std::default_delete()); + } + +private: + explicit Pet(const std::string &name) : name(name) {} +}; + +TEST_SUBMODULE(class_sh_unique_ptr_custom_deleter, m) { + py::classh(m, "Pet").def_readwrite("name", &Pet::name); + + m.def("create", &Pet::New); +} + +} // namespace class_sh_unique_ptr_custom_deleter +} // namespace pybind11_tests diff --git a/external_libraries/pybind11/tests/test_class_sh_unique_ptr_custom_deleter.py b/external_libraries/pybind11/tests/test_class_sh_unique_ptr_custom_deleter.py new file mode 100644 index 00000000..34aa5206 --- /dev/null +++ b/external_libraries/pybind11/tests/test_class_sh_unique_ptr_custom_deleter.py @@ -0,0 +1,8 @@ +from __future__ import annotations + +from pybind11_tests import class_sh_unique_ptr_custom_deleter as m + + +def test_create(): + pet = m.create("abc") + assert pet.name == "abc" diff --git a/external_libraries/pybind11/tests/test_class_sh_unique_ptr_member.cpp b/external_libraries/pybind11/tests/test_class_sh_unique_ptr_member.cpp new file mode 100644 index 00000000..50c505a6 --- /dev/null +++ b/external_libraries/pybind11/tests/test_class_sh_unique_ptr_member.cpp @@ -0,0 +1,50 @@ +#include "pybind11_tests.h" + +#include + +namespace pybind11_tests { +namespace class_sh_unique_ptr_member { + +class pointee { // NOT copyable. +public: + pointee() = default; + + int get_int() const { return 213; } + + pointee(const pointee &) = delete; + pointee(pointee &&) = delete; + pointee &operator=(const pointee &) = delete; + pointee &operator=(pointee &&) = delete; +}; + +inline std::unique_ptr make_unique_pointee() { + return std::unique_ptr(new pointee); +} + +class ptr_owner { +public: + explicit ptr_owner(std::unique_ptr ptr) : ptr_(std::move(ptr)) {} + + bool is_owner() const { return bool(ptr_); } + + std::unique_ptr give_up_ownership_via_unique_ptr() { return std::move(ptr_); } + std::shared_ptr give_up_ownership_via_shared_ptr() { return std::move(ptr_); } + +private: + std::unique_ptr ptr_; +}; + +TEST_SUBMODULE(class_sh_unique_ptr_member, m) { + py::classh(m, "pointee").def(py::init<>()).def("get_int", &pointee::get_int); + + m.def("make_unique_pointee", make_unique_pointee); + + py::class_(m, "ptr_owner") + .def(py::init>(), py::arg("ptr")) + .def("is_owner", &ptr_owner::is_owner) + .def("give_up_ownership_via_unique_ptr", &ptr_owner::give_up_ownership_via_unique_ptr) + .def("give_up_ownership_via_shared_ptr", &ptr_owner::give_up_ownership_via_shared_ptr); +} + +} // namespace class_sh_unique_ptr_member +} // namespace pybind11_tests diff --git a/external_libraries/pybind11/tests/test_class_sh_unique_ptr_member.py b/external_libraries/pybind11/tests/test_class_sh_unique_ptr_member.py new file mode 100644 index 00000000..a5d2ccd2 --- /dev/null +++ b/external_libraries/pybind11/tests/test_class_sh_unique_ptr_member.py @@ -0,0 +1,26 @@ +from __future__ import annotations + +import pytest + +from pybind11_tests import class_sh_unique_ptr_member as m + + +def test_make_unique_pointee(): + obj = m.make_unique_pointee() + assert obj.get_int() == 213 + + +@pytest.mark.parametrize( + "give_up_ownership_via", + ["give_up_ownership_via_unique_ptr", "give_up_ownership_via_shared_ptr"], +) +def test_pointee_and_ptr_owner(give_up_ownership_via): + obj = m.pointee() + assert obj.get_int() == 213 + owner = m.ptr_owner(obj) + with pytest.raises(ValueError, match="Python instance was disowned"): + obj.get_int() + assert owner.is_owner() + reclaimed = getattr(owner, give_up_ownership_via)() + assert not owner.is_owner() + assert reclaimed.get_int() == 213 diff --git a/external_libraries/pybind11/tests/test_class_sh_virtual_py_cpp_mix.cpp b/external_libraries/pybind11/tests/test_class_sh_virtual_py_cpp_mix.cpp new file mode 100644 index 00000000..df8af19e --- /dev/null +++ b/external_libraries/pybind11/tests/test_class_sh_virtual_py_cpp_mix.cpp @@ -0,0 +1,58 @@ +#include "pybind11_tests.h" + +#include + +namespace pybind11_tests { +namespace class_sh_virtual_py_cpp_mix { + +class Base { +public: + virtual ~Base() = default; + virtual int get() const { return 101; } + + // Some compilers complain about implicitly defined versions of some of the following: + Base() = default; + Base(const Base &) = default; +}; + +class CppDerivedPlain : public Base { +public: + int get() const override { return 202; } +}; + +class CppDerived : public Base { +public: + int get() const override { return 212; } +}; + +int get_from_cpp_plainc_ptr(const Base *b) { return b->get() + 4000; } + +int get_from_cpp_unique_ptr(std::unique_ptr b) { return b->get() + 5000; } + +struct BaseVirtualOverrider : Base, py::trampoline_self_life_support { + using Base::Base; + + int get() const override { PYBIND11_OVERRIDE(int, Base, get); } +}; + +struct CppDerivedVirtualOverrider : CppDerived, py::trampoline_self_life_support { + using CppDerived::CppDerived; + + int get() const override { PYBIND11_OVERRIDE(int, CppDerived, get); } +}; + +} // namespace class_sh_virtual_py_cpp_mix +} // namespace pybind11_tests + +using namespace pybind11_tests::class_sh_virtual_py_cpp_mix; + +TEST_SUBMODULE(class_sh_virtual_py_cpp_mix, m) { + py::classh(m, "Base").def(py::init<>()).def("get", &Base::get); + + py::classh(m, "CppDerivedPlain").def(py::init<>()); + + py::classh(m, "CppDerived").def(py::init<>()); + + m.def("get_from_cpp_plainc_ptr", get_from_cpp_plainc_ptr, py::arg("b")); + m.def("get_from_cpp_unique_ptr", get_from_cpp_unique_ptr, py::arg("b")); +} diff --git a/external_libraries/pybind11/tests/test_class_sh_virtual_py_cpp_mix.py b/external_libraries/pybind11/tests/test_class_sh_virtual_py_cpp_mix.py new file mode 100644 index 00000000..33133eb8 --- /dev/null +++ b/external_libraries/pybind11/tests/test_class_sh_virtual_py_cpp_mix.py @@ -0,0 +1,66 @@ +from __future__ import annotations + +import pytest + +from pybind11_tests import class_sh_virtual_py_cpp_mix as m + + +class PyBase(m.Base): # Avoiding name PyDerived, for more systematic naming. + def __init__(self): + m.Base.__init__(self) + + def get(self): + return 323 + + +class PyCppDerived(m.CppDerived): + def __init__(self): + m.CppDerived.__init__(self) + + def get(self): + return 434 + + +@pytest.mark.parametrize( + ("ctor", "expected"), + [ + (m.Base, 101), + (PyBase, 323), + (m.CppDerivedPlain, 202), + (m.CppDerived, 212), + (PyCppDerived, 434), + ], +) +def test_base_get(ctor, expected): + obj = ctor() + assert obj.get() == expected + + +@pytest.mark.parametrize( + ("ctor", "expected"), + [ + (m.Base, 4101), + (PyBase, 4323), + (m.CppDerivedPlain, 4202), + (m.CppDerived, 4212), + (PyCppDerived, 4434), + ], +) +def test_get_from_cpp_plainc_ptr(ctor, expected): + obj = ctor() + assert m.get_from_cpp_plainc_ptr(obj) == expected + + +@pytest.mark.parametrize( + ("ctor", "expected"), + [ + (m.Base, 5101), + (PyBase, 5323), + (m.CppDerivedPlain, 5202), + (m.CppDerived, 5212), + (PyCppDerived, 5434), + ], +) +def test_get_from_cpp_unique_ptr(ctor, expected): + obj = ctor() + assert m.get_from_cpp_unique_ptr(obj) == expected diff --git a/external_libraries/pybind11/tests/test_cmake_build/CMakeLists.txt b/external_libraries/pybind11/tests/test_cmake_build/CMakeLists.txt new file mode 100644 index 00000000..ce63a690 --- /dev/null +++ b/external_libraries/pybind11/tests/test_cmake_build/CMakeLists.txt @@ -0,0 +1,91 @@ +add_custom_target(test_cmake_build) + +function(pybind11_add_build_test name) + cmake_parse_arguments(ARG "INSTALL" "" "" ${ARGN}) + + set(build_options "-DCMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER}") + + list(APPEND build_options "-DPYBIND11_FINDPYTHON=${PYBIND11_FINDPYTHON}") + if(PYBIND11_FINDPYTHON) + if(DEFINED Python_ROOT_DIR) + list(APPEND build_options "-DPython_ROOT_DIR=${Python_ROOT_DIR}") + endif() + + list(APPEND build_options "-DPython_EXECUTABLE=${Python_EXECUTABLE}") + else() + list(APPEND build_options "-DPYTHON_EXECUTABLE=${PYTHON_EXECUTABLE}") + endif() + + if(DEFINED CMAKE_CXX_STANDARD) + list(APPEND build_options "-DCMAKE_CXX_STANDARD=${CMAKE_CXX_STANDARD}") + endif() + + if(NOT ARG_INSTALL) + list(APPEND build_options "-Dpybind11_SOURCE_DIR=${pybind11_SOURCE_DIR}") + else() + list(APPEND build_options "-DCMAKE_PREFIX_PATH=${pybind11_BINARY_DIR}/mock_install") + endif() + + add_custom_target( + test_build_${name} + ${CMAKE_CTEST_COMMAND} + --build-and-test + "${CMAKE_CURRENT_SOURCE_DIR}/${name}" + "${CMAKE_CURRENT_BINARY_DIR}/${name}" + --build-config + Release + --build-noclean + --build-generator + ${CMAKE_GENERATOR} + $<$:--build-generator-platform> + ${CMAKE_GENERATOR_PLATFORM} + --build-makeprogram + ${CMAKE_MAKE_PROGRAM} + --build-target + check_${name} + --build-options + ${build_options}) + if(ARG_INSTALL) + add_dependencies(test_build_${name} mock_install) + endif() + add_dependencies(test_cmake_build test_build_${name}) +endfunction() + +if(PYBIND11_TEST_SMART_HOLDER) + add_compile_definitions( + -DPYBIND11_RUN_TESTING_WITH_SMART_HOLDER_AS_DEFAULT_BUT_NEVER_USE_IN_PRODUCTION_PLEASE) +endif() + +possibly_uninitialized(PYTHON_MODULE_EXTENSION Python_INTERPRETER_ID) + +pybind11_add_build_test(subdirectory_function) +pybind11_add_build_test(subdirectory_target) +if("${PYTHON_MODULE_EXTENSION}" MATCHES "pypy" + OR "${Python_INTERPRETER_ID}" STREQUAL "PyPy" + OR "${PYTHON_MODULE_EXTENSION}" MATCHES "graalpy") + message(STATUS "Skipping embed test on PyPy or GraalPy") +else() + pybind11_add_build_test(subdirectory_embed) +endif() + +if(PYBIND11_INSTALL) + add_custom_target( + mock_install ${CMAKE_COMMAND} "-DCMAKE_INSTALL_PREFIX=${pybind11_BINARY_DIR}/mock_install" -P + "${pybind11_BINARY_DIR}/cmake_install.cmake") + + if(NOT "${PYTHON_MODULE_EXTENSION}" MATCHES "graalpy") + pybind11_add_build_test(installed_function INSTALL) + endif() + pybind11_add_build_test(installed_target INSTALL) + if(NOT + ("${PYTHON_MODULE_EXTENSION}" MATCHES "pypy" + OR "${Python_INTERPRETER_ID}" STREQUAL "PyPy" + OR "${PYTHON_MODULE_EXTENSION}" MATCHES "graalpy")) + pybind11_add_build_test(installed_embed INSTALL) + endif() +endif() + +add_dependencies(check test_cmake_build) + +add_subdirectory(subdirectory_target EXCLUDE_FROM_ALL) +add_subdirectory(subdirectory_embed EXCLUDE_FROM_ALL) diff --git a/external_libraries/pybind11/tests/test_cmake_build/embed.cpp b/external_libraries/pybind11/tests/test_cmake_build/embed.cpp new file mode 100644 index 00000000..30bc4f1e --- /dev/null +++ b/external_libraries/pybind11/tests/test_cmake_build/embed.cpp @@ -0,0 +1,23 @@ +#include +namespace py = pybind11; + +PYBIND11_EMBEDDED_MODULE(test_cmake_build, m) { + m.def("add", [](int i, int j) { return i + j; }); +} + +int main(int argc, char *argv[]) { + if (argc != 2) { + throw std::runtime_error("Expected test.py file as the first argument"); + } + auto *test_py_file = argv[1]; + + py::scoped_interpreter guard{}; + + auto m = py::module_::import("test_cmake_build"); + if (m.attr("add")(1, 2).cast() != 3) { + throw std::runtime_error("embed.cpp failed"); + } + + py::module_::import("sys").attr("argv") = py::make_tuple("test.py", "embed.cpp"); + py::eval_file(test_py_file, py::globals()); +} diff --git a/external_libraries/pybind11/tests/test_cmake_build/installed_embed/CMakeLists.txt b/external_libraries/pybind11/tests/test_cmake_build/installed_embed/CMakeLists.txt new file mode 100644 index 00000000..a3649665 --- /dev/null +++ b/external_libraries/pybind11/tests/test_cmake_build/installed_embed/CMakeLists.txt @@ -0,0 +1,19 @@ +cmake_minimum_required(VERSION 3.15...4.2) + +project(test_installed_embed CXX) + +find_package(pybind11 CONFIG REQUIRED) +message(STATUS "Found pybind11 v${pybind11_VERSION}: ${pybind11_INCLUDE_DIRS}") + +add_executable(test_installed_embed ../embed.cpp) +target_link_libraries(test_installed_embed PRIVATE pybind11::embed) +set_target_properties(test_installed_embed PROPERTIES OUTPUT_NAME test_cmake_build) + +# Do not treat includes from IMPORTED target as SYSTEM (Python headers in pybind11::embed). +# This may be needed to resolve header conflicts, e.g. between Python release and debug headers. +set_target_properties(test_installed_embed PROPERTIES NO_SYSTEM_FROM_IMPORTED ON) + +add_custom_target( + check_installed_embed + $ ${PROJECT_SOURCE_DIR}/../test.py + DEPENDS test_installed_embed) diff --git a/external_libraries/pybind11/tests/test_cmake_build/installed_function/CMakeLists.txt b/external_libraries/pybind11/tests/test_cmake_build/installed_function/CMakeLists.txt new file mode 100644 index 00000000..d2774112 --- /dev/null +++ b/external_libraries/pybind11/tests/test_cmake_build/installed_function/CMakeLists.txt @@ -0,0 +1,29 @@ +cmake_minimum_required(VERSION 3.15...4.2) + +project(test_installed_function CXX) + +find_package(pybind11 CONFIG REQUIRED) +message( + STATUS "Found pybind11 v${pybind11_VERSION} ${pybind11_VERSION_TYPE}: ${pybind11_INCLUDE_DIRS}") + +pybind11_add_module(test_installed_function SHARED NO_EXTRAS ../main.cpp) +set_target_properties(test_installed_function PROPERTIES OUTPUT_NAME test_cmake_build) + +if(DEFINED Python_EXECUTABLE) + set(_Python_EXECUTABLE "${Python_EXECUTABLE}") +elseif(DEFINED PYTHON_EXECUTABLE) + set(_Python_EXECUTABLE "${PYTHON_EXECUTABLE}") +else() + message(FATAL_ERROR "No Python executable defined (should not be possible at this stage)") +endif() + +add_custom_target( + check_installed_function + ${CMAKE_COMMAND} + -E + env + PYTHONPATH=$ + ${_Python_EXECUTABLE} + ${PROJECT_SOURCE_DIR}/../test.py + ${PROJECT_NAME} + DEPENDS test_installed_function) diff --git a/external_libraries/pybind11/tests/test_cmake_build/installed_target/CMakeLists.txt b/external_libraries/pybind11/tests/test_cmake_build/installed_target/CMakeLists.txt new file mode 100644 index 00000000..6ee01693 --- /dev/null +++ b/external_libraries/pybind11/tests/test_cmake_build/installed_target/CMakeLists.txt @@ -0,0 +1,37 @@ +cmake_minimum_required(VERSION 3.15...4.2) + +project(test_installed_target CXX) + +find_package(pybind11 CONFIG REQUIRED) +message(STATUS "Found pybind11 v${pybind11_VERSION}: ${pybind11_INCLUDE_DIRS}") + +add_library(test_installed_target MODULE ../main.cpp) + +target_link_libraries(test_installed_target PRIVATE pybind11::module) +set_target_properties(test_installed_target PROPERTIES OUTPUT_NAME test_cmake_build) + +# Make sure result is, for example, test_installed_target.so, not libtest_installed_target.dylib +pybind11_extension(test_installed_target) + +# Do not treat includes from IMPORTED target as SYSTEM (Python headers in pybind11::module). +# This may be needed to resolve header conflicts, e.g. between Python release and debug headers. +set_target_properties(test_installed_target PROPERTIES NO_SYSTEM_FROM_IMPORTED ON) + +if(DEFINED Python_EXECUTABLE) + set(_Python_EXECUTABLE "${Python_EXECUTABLE}") +elseif(DEFINED PYTHON_EXECUTABLE) + set(_Python_EXECUTABLE "${PYTHON_EXECUTABLE}") +else() + message(FATAL_ERROR "No Python executable defined (should not be possible at this stage)") +endif() + +add_custom_target( + check_installed_target + ${CMAKE_COMMAND} + -E + env + PYTHONPATH=$ + ${_Python_EXECUTABLE} + ${PROJECT_SOURCE_DIR}/../test.py + ${PROJECT_NAME} + DEPENDS test_installed_target) diff --git a/external_libraries/pybind11/tests/test_cmake_build/main.cpp b/external_libraries/pybind11/tests/test_cmake_build/main.cpp new file mode 100644 index 00000000..640449c3 --- /dev/null +++ b/external_libraries/pybind11/tests/test_cmake_build/main.cpp @@ -0,0 +1,6 @@ +#include +namespace py = pybind11; + +PYBIND11_MODULE(test_cmake_build, m, py::mod_gil_not_used()) { + m.def("add", [](int i, int j) { return i + j; }); +} diff --git a/external_libraries/pybind11/tests/test_cmake_build/subdirectory_embed/CMakeLists.txt b/external_libraries/pybind11/tests/test_cmake_build/subdirectory_embed/CMakeLists.txt new file mode 100644 index 00000000..81b44581 --- /dev/null +++ b/external_libraries/pybind11/tests/test_cmake_build/subdirectory_embed/CMakeLists.txt @@ -0,0 +1,38 @@ +cmake_minimum_required(VERSION 3.15...4.2) + +project(test_subdirectory_embed CXX) + +set(PYBIND11_INSTALL + ON + CACHE BOOL "") +set(PYBIND11_EXPORT_NAME test_export) + +# Allow PYTHON_EXECUTABLE if in FINDPYTHON mode and building pybind11's tests +# (makes transition easier while we support both modes). +if(DEFINED PYTHON_EXECUTABLE AND NOT DEFINED Python_EXECUTABLE) + set(Python_EXECUTABLE "${PYTHON_EXECUTABLE}") +endif() + +add_subdirectory("${pybind11_SOURCE_DIR}" pybind11) + +# Test basic target functionality +add_executable(test_subdirectory_embed ../embed.cpp) +target_link_libraries(test_subdirectory_embed PRIVATE pybind11::embed) +set_target_properties(test_subdirectory_embed PROPERTIES OUTPUT_NAME test_cmake_build) + +add_custom_target( + check_subdirectory_embed + $ "${PROJECT_SOURCE_DIR}/../test.py" + DEPENDS test_subdirectory_embed) + +# Test custom export group -- PYBIND11_EXPORT_NAME +add_library(test_with_catch_lib ../embed.cpp) +target_link_libraries(test_with_catch_lib PRIVATE pybind11::embed) + +install( + TARGETS test_with_catch_lib + EXPORT test_export + ARCHIVE DESTINATION bin + LIBRARY DESTINATION lib + RUNTIME DESTINATION lib) +install(EXPORT test_export DESTINATION lib/cmake/test_export/test_export-Targets.cmake) diff --git a/external_libraries/pybind11/tests/test_cmake_build/subdirectory_function/CMakeLists.txt b/external_libraries/pybind11/tests/test_cmake_build/subdirectory_function/CMakeLists.txt new file mode 100644 index 00000000..10b283de --- /dev/null +++ b/external_libraries/pybind11/tests/test_cmake_build/subdirectory_function/CMakeLists.txt @@ -0,0 +1,32 @@ +cmake_minimum_required(VERSION 3.15...4.2) + +project(test_subdirectory_function CXX) + +# Allow PYTHON_EXECUTABLE if in FINDPYTHON mode and building pybind11's tests +# (makes transition easier while we support both modes). +if(DEFINED PYTHON_EXECUTABLE AND NOT DEFINED Python_EXECUTABLE) + set(Python_EXECUTABLE "${PYTHON_EXECUTABLE}") +endif() + +add_subdirectory("${pybind11_SOURCE_DIR}" pybind11) +pybind11_add_module(test_subdirectory_function ../main.cpp) +set_target_properties(test_subdirectory_function PROPERTIES OUTPUT_NAME test_cmake_build) + +if(DEFINED Python_EXECUTABLE) + set(_Python_EXECUTABLE "${Python_EXECUTABLE}") +elseif(DEFINED PYTHON_EXECUTABLE) + set(_Python_EXECUTABLE "${PYTHON_EXECUTABLE}") +else() + message(FATAL_ERROR "No Python executable defined (should not be possible at this stage)") +endif() + +add_custom_target( + check_subdirectory_function + ${CMAKE_COMMAND} + -E + env + PYTHONPATH=$ + ${_Python_EXECUTABLE} + ${PROJECT_SOURCE_DIR}/../test.py + ${PROJECT_NAME} + DEPENDS test_subdirectory_function) diff --git a/external_libraries/pybind11/tests/test_cmake_build/subdirectory_target/CMakeLists.txt b/external_libraries/pybind11/tests/test_cmake_build/subdirectory_target/CMakeLists.txt new file mode 100644 index 00000000..88d73f60 --- /dev/null +++ b/external_libraries/pybind11/tests/test_cmake_build/subdirectory_target/CMakeLists.txt @@ -0,0 +1,38 @@ +cmake_minimum_required(VERSION 3.15...4.2) + +project(test_subdirectory_target CXX) + +# Allow PYTHON_EXECUTABLE if in FINDPYTHON mode and building pybind11's tests +# (makes transition easier while we support both modes). +if(DEFINED PYTHON_EXECUTABLE AND NOT DEFINED Python_EXECUTABLE) + set(Python_EXECUTABLE "${PYTHON_EXECUTABLE}") +endif() + +add_subdirectory("${pybind11_SOURCE_DIR}" pybind11) + +add_library(test_subdirectory_target MODULE ../main.cpp) +set_target_properties(test_subdirectory_target PROPERTIES OUTPUT_NAME test_cmake_build) + +target_link_libraries(test_subdirectory_target PRIVATE pybind11::module) + +# Make sure result is, for example, test_installed_target.so, not libtest_installed_target.dylib +pybind11_extension(test_subdirectory_target) + +if(DEFINED Python_EXECUTABLE) + set(_Python_EXECUTABLE "${Python_EXECUTABLE}") +elseif(DEFINED PYTHON_EXECUTABLE) + set(_Python_EXECUTABLE "${PYTHON_EXECUTABLE}") +else() + message(FATAL_ERROR "No Python executable defined (should not be possible at this stage)") +endif() + +add_custom_target( + check_subdirectory_target + ${CMAKE_COMMAND} + -E + env + PYTHONPATH=$ + ${_Python_EXECUTABLE} + ${PROJECT_SOURCE_DIR}/../test.py + ${PROJECT_NAME} + DEPENDS test_subdirectory_target) diff --git a/external_libraries/pybind11/tests/test_cmake_build/test.py b/external_libraries/pybind11/tests/test_cmake_build/test.py new file mode 100644 index 00000000..bb4c20e0 --- /dev/null +++ b/external_libraries/pybind11/tests/test_cmake_build/test.py @@ -0,0 +1,10 @@ +from __future__ import annotations + +import sys + +import test_cmake_build + +assert isinstance(__file__, str) # Test this is properly set + +assert test_cmake_build.add(1, 2) == 3 +print(f"{sys.argv[1]} imports, runs, and adds: 1 + 2 = 3") diff --git a/external_libraries/pybind11/tests/test_const_name.cpp b/external_libraries/pybind11/tests/test_const_name.cpp new file mode 100644 index 00000000..2ad01e68 --- /dev/null +++ b/external_libraries/pybind11/tests/test_const_name.cpp @@ -0,0 +1,55 @@ +// Copyright (c) 2021 The Pybind Development Team. +// All rights reserved. Use of this source code is governed by a +// BSD-style license that can be found in the LICENSE file. + +#include "pybind11_tests.h" + +// IUT = Implementation Under Test +#define CONST_NAME_TESTS(TEST_FUNC, IUT) \ + std::string TEST_FUNC(int selector) { \ + switch (selector) { \ + case 0: \ + return IUT("").text; \ + case 1: \ + return IUT("A").text; \ + case 2: \ + return IUT("Bd").text; \ + case 3: \ + return IUT("Cef").text; \ + case 4: \ + return IUT().text; /*NOLINT(bugprone-macro-parentheses)*/ \ + case 5: \ + return IUT().text; /*NOLINT(bugprone-macro-parentheses)*/ \ + case 6: \ + return IUT("T1", "T2").text; /*NOLINT(bugprone-macro-parentheses)*/ \ + case 7: \ + return IUT("U1", "U2").text; /*NOLINT(bugprone-macro-parentheses)*/ \ + case 8: \ + /*NOLINTNEXTLINE(bugprone-macro-parentheses)*/ \ + return IUT(IUT("D1"), IUT("D2")).text; \ + case 9: \ + /*NOLINTNEXTLINE(bugprone-macro-parentheses)*/ \ + return IUT(IUT("E1"), IUT("E2")).text; \ + case 10: \ + return IUT("KeepAtEnd").text; \ + default: \ + break; \ + } \ + throw std::runtime_error("Invalid selector value."); \ + } + +CONST_NAME_TESTS(const_name_tests, py::detail::const_name) + +#ifdef PYBIND11_DETAIL_UNDERSCORE_BACKWARD_COMPATIBILITY +CONST_NAME_TESTS(underscore_tests, py::detail::_) +#endif + +TEST_SUBMODULE(const_name, m) { + m.def("const_name_tests", const_name_tests); + +#if defined(PYBIND11_DETAIL_UNDERSCORE_BACKWARD_COMPATIBILITY) + m.def("underscore_tests", underscore_tests); +#else + m.attr("underscore_tests") = "PYBIND11_DETAIL_UNDERSCORE_BACKWARD_COMPATIBILITY not defined."; +#endif +} diff --git a/external_libraries/pybind11/tests/test_const_name.py b/external_libraries/pybind11/tests/test_const_name.py new file mode 100644 index 00000000..f5202494 --- /dev/null +++ b/external_libraries/pybind11/tests/test_const_name.py @@ -0,0 +1,31 @@ +from __future__ import annotations + +import pytest + +from pybind11_tests import const_name as m + + +@pytest.mark.parametrize("func", [m.const_name_tests, m.underscore_tests]) +@pytest.mark.parametrize( + ("selector", "expected"), + enumerate( + ( + "", + "A", + "Bd", + "Cef", + "%", + "%", + "T1", + "U2", + "D1", + "E2", + "KeepAtEnd", + ) + ), +) +def test_const_name(func, selector, expected): + if isinstance(func, str): + pytest.skip(func) + text = func(selector) + assert text == expected diff --git a/external_libraries/pybind11/tests/test_constants_and_functions.cpp b/external_libraries/pybind11/tests/test_constants_and_functions.cpp new file mode 100644 index 00000000..bf42b415 --- /dev/null +++ b/external_libraries/pybind11/tests/test_constants_and_functions.cpp @@ -0,0 +1,158 @@ +/* + tests/test_constants_and_functions.cpp -- global constants and functions, enumerations, raw + byte strings + + Copyright (c) 2016 Wenzel Jakob + + All rights reserved. Use of this source code is governed by a + BSD-style license that can be found in the LICENSE file. +*/ + +#include "pybind11_tests.h" + +enum MyEnum { EFirstEntry = 1, ESecondEntry }; + +std::string test_function1() { return "test_function()"; } + +std::string test_function2(MyEnum k) { return "test_function(enum=" + std::to_string(k) + ")"; } + +std::string test_function3(int i) { return "test_function(" + std::to_string(i) + ")"; } + +py::str test_function4() { return "test_function()"; } +py::str test_function4(char *) { return "test_function(char *)"; } +py::str test_function4(int, float) { return "test_function(int, float)"; } +py::str test_function4(float, int) { return "test_function(float, int)"; } + +py::bytes return_bytes() { + const char *data = "\x01\x00\x02\x00"; + return std::string(data, 4); +} + +std::string print_bytes(const py::bytes &bytes) { + std::string ret = "bytes["; + const auto value = static_cast(bytes); + for (char c : value) { + ret += std::to_string(static_cast(c)) + ' '; + } + ret.back() = ']'; + return ret; +} + +// Test that we properly handle C++17 exception specifiers (which are part of the function +// signature in C++17). These should all still work before C++17, but don't affect the function +// signature. +namespace test_exc_sp { +// [workaround(intel)] Unable to use noexcept instead of noexcept(true) +// Make the f1 test basically the same as the f2 test in C++17 mode for the Intel compiler as +// it fails to compile with a plain noexcept (tested with icc (ICC) 2021.1 Beta 20200827). +#if defined(__INTEL_COMPILER) && defined(PYBIND11_CPP17) +int f1(int x) noexcept(true) { return x + 1; } +#else +int f1(int x) noexcept { return x + 1; } +#endif +int f2(int x) noexcept(true) { return x + 2; } +int f3(int x) noexcept(false) { return x + 3; } +PYBIND11_WARNING_PUSH +PYBIND11_WARNING_DISABLE_GCC("-Wdeprecated") +#if defined(__clang_major__) && __clang_major__ >= 5 +PYBIND11_WARNING_DISABLE_CLANG("-Wdeprecated-dynamic-exception-spec") +#else +PYBIND11_WARNING_DISABLE_CLANG("-Wdeprecated") +#endif +// NOLINTNEXTLINE(modernize-use-noexcept) +int f4(int x) throw() { return x + 4; } // Deprecated equivalent to noexcept(true) +PYBIND11_WARNING_POP +struct C { + int m1(int x) noexcept { return x - 1; } + int m2(int x) const noexcept { return x - 2; } + int m3(int x) noexcept(true) { return x - 3; } + int m4(int x) const noexcept(true) { return x - 4; } + int m5(int x) noexcept(false) { return x - 5; } + int m6(int x) const noexcept(false) { return x - 6; } + PYBIND11_WARNING_PUSH + PYBIND11_WARNING_DISABLE_GCC("-Wdeprecated") + PYBIND11_WARNING_DISABLE_CLANG("-Wdeprecated") + // NOLINTNEXTLINE(modernize-use-noexcept) + int m7(int x) throw() { return x - 7; } + // NOLINTNEXTLINE(modernize-use-noexcept) + int m8(int x) const throw() { return x - 8; } + PYBIND11_WARNING_POP +}; +} // namespace test_exc_sp + +TEST_SUBMODULE(constants_and_functions, m) { + // test_constants + m.attr("some_constant") = py::int_(14); + + // test_function_overloading + m.def("test_function", &test_function1); + m.def("test_function", &test_function2); + m.def("test_function", &test_function3); + +#if defined(PYBIND11_OVERLOAD_CAST) + m.def("test_function", py::overload_cast<>(&test_function4)); + m.def("test_function", py::overload_cast(&test_function4)); + m.def("test_function", py::overload_cast(&test_function4)); + m.def("test_function", py::overload_cast(&test_function4)); +#else + m.def("test_function", static_cast(&test_function4)); + m.def("test_function", static_cast(&test_function4)); + m.def("test_function", static_cast(&test_function4)); + m.def("test_function", static_cast(&test_function4)); +#endif + + py::enum_(m, "MyEnum") + .value("EFirstEntry", EFirstEntry) + .value("ESecondEntry", ESecondEntry) + .export_values(); + + // test_bytes + m.def("return_bytes", &return_bytes); + m.def("print_bytes", &print_bytes); + + // test_exception_specifiers + using namespace test_exc_sp; + py::class_(m, "C") + .def(py::init<>()) + .def("m1", &C::m1) + .def("m2", &C::m2) + .def("m3", &C::m3) + .def("m4", &C::m4) + .def("m5", &C::m5) + .def("m6", &C::m6) + .def("m7", &C::m7) + .def("m8", &C::m8); + m.def("f1", f1); + m.def("f2", f2); + + PYBIND11_WARNING_PUSH + PYBIND11_WARNING_DISABLE_INTEL(878) // incompatible exception specifications + m.def("f3", f3); + PYBIND11_WARNING_POP + + m.def("f4", f4); + + // test_function_record_leaks + m.def("register_large_capture_with_invalid_arguments", [](py::module_ m) { + // This should always be enough to trigger the alternative branch + // where `sizeof(capture) > sizeof(rec->data)` + uint64_t capture[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; +#if defined(__GNUC__) && __GNUC__ == 4 // CentOS7 + py::detail::silence_unused_warnings(capture); +#endif + m.def( + "should_raise", [capture](int) { return capture[9] + 33; }, py::kw_only(), py::arg()); + }); + m.def("register_with_raising_repr", [](py::module_ m, const py::object &default_value) { + m.def( + "should_raise", + [](int, int, const py::object &) { return 42; }, + "some docstring", + py::arg_v("x", 42), + py::arg_v("y", 42, ""), + py::arg_v("z", default_value)); + }); + + // test noexcept(true) lambda (#4565) + m.def("l1", []() noexcept(true) { return 0; }); +} diff --git a/external_libraries/pybind11/tests/test_constants_and_functions.py b/external_libraries/pybind11/tests/test_constants_and_functions.py new file mode 100644 index 00000000..63004e1b --- /dev/null +++ b/external_libraries/pybind11/tests/test_constants_and_functions.py @@ -0,0 +1,58 @@ +from __future__ import annotations + +import pytest + +m = pytest.importorskip("pybind11_tests.constants_and_functions") + + +def test_constants(): + assert m.some_constant == 14 + + +def test_function_overloading(): + assert m.test_function() == "test_function()" + assert m.test_function(7) == "test_function(7)" + assert m.test_function(m.MyEnum.EFirstEntry) == "test_function(enum=1)" + assert m.test_function(m.MyEnum.ESecondEntry) == "test_function(enum=2)" + + assert m.test_function() == "test_function()" + assert m.test_function("abcd") == "test_function(char *)" + assert m.test_function(1, 1.0) == "test_function(int, float)" + assert m.test_function(1, 1.0) == "test_function(int, float)" + assert m.test_function(2.0, 2) == "test_function(float, int)" + + +def test_bytes(): + assert m.print_bytes(m.return_bytes()) == "bytes[1 0 2 0]" + + +def test_exception_specifiers(): + c = m.C() + assert c.m1(2) == 1 + assert c.m2(3) == 1 + assert c.m3(5) == 2 + assert c.m4(7) == 3 + assert c.m5(10) == 5 + assert c.m6(14) == 8 + assert c.m7(20) == 13 + assert c.m8(29) == 21 + + assert m.f1(33) == 34 + assert m.f2(53) == 55 + assert m.f3(86) == 89 + assert m.f4(140) == 144 + + +def test_function_record_leaks(): + class RaisingRepr: + def __repr__(self): + raise RuntimeError("Surprise!") + + with pytest.raises(RuntimeError): + m.register_large_capture_with_invalid_arguments(m) + with pytest.raises(RuntimeError): + m.register_with_raising_repr(m, RaisingRepr()) + + +def test_noexcept_lambda(): + assert m.l1() == 0 diff --git a/external_libraries/pybind11/tests/test_copy_move.cpp b/external_libraries/pybind11/tests/test_copy_move.cpp new file mode 100644 index 00000000..b163406a --- /dev/null +++ b/external_libraries/pybind11/tests/test_copy_move.cpp @@ -0,0 +1,546 @@ +/* + tests/test_copy_move_policies.cpp -- 'copy' and 'move' return value policies + and related tests + + Copyright (c) 2016 Ben North + + All rights reserved. Use of this source code is governed by a + BSD-style license that can be found in the LICENSE file. +*/ + +#include + +#include "constructor_stats.h" +#include "pybind11_tests.h" + +#include + +template +struct empty { + static const derived &get_one() { return instance_; } + static derived instance_; + +private: + empty() = default; + friend derived; +}; + +struct lacking_copy_ctor : public empty { + lacking_copy_ctor() = default; + lacking_copy_ctor(const lacking_copy_ctor &other) = delete; +}; + +template <> +lacking_copy_ctor empty::instance_ = {}; + +struct lacking_move_ctor : public empty { + lacking_move_ctor() = default; + lacking_move_ctor(const lacking_move_ctor &other) = delete; + lacking_move_ctor(lacking_move_ctor &&other) = delete; +}; + +template <> +lacking_move_ctor empty::instance_ = {}; + +/* Custom type caster move/copy test classes */ +class MoveOnlyInt { +public: + MoveOnlyInt() { print_default_created(this); } + explicit MoveOnlyInt(int v) : value{v} { print_created(this, value); } + MoveOnlyInt(MoveOnlyInt &&m) noexcept { + print_move_created(this, m.value); + std::swap(value, m.value); + } + MoveOnlyInt &operator=(MoveOnlyInt &&m) noexcept { + print_move_assigned(this, m.value); + std::swap(value, m.value); + return *this; + } + MoveOnlyInt(const MoveOnlyInt &) = delete; + MoveOnlyInt &operator=(const MoveOnlyInt &) = delete; + ~MoveOnlyInt() { print_destroyed(this); } + + int value; +}; +class MoveOrCopyInt { +public: + MoveOrCopyInt() { print_default_created(this); } + explicit MoveOrCopyInt(int v) : value{v} { print_created(this, value); } + MoveOrCopyInt(MoveOrCopyInt &&m) noexcept { + print_move_created(this, m.value); + std::swap(value, m.value); + } + MoveOrCopyInt &operator=(MoveOrCopyInt &&m) noexcept { + print_move_assigned(this, m.value); + std::swap(value, m.value); + return *this; + } + MoveOrCopyInt(const MoveOrCopyInt &c) { + print_copy_created(this, c.value); + // NOLINTNEXTLINE(cppcoreguidelines-prefer-member-initializer) + value = c.value; + } + MoveOrCopyInt &operator=(const MoveOrCopyInt &c) { + print_copy_assigned(this, c.value); + value = c.value; + return *this; + } + ~MoveOrCopyInt() { print_destroyed(this); } + + int value; +}; +class CopyOnlyInt { +public: + CopyOnlyInt() { print_default_created(this); } + explicit CopyOnlyInt(int v) : value{v} { print_created(this, value); } + CopyOnlyInt(const CopyOnlyInt &c) { + print_copy_created(this, c.value); + // NOLINTNEXTLINE(cppcoreguidelines-prefer-member-initializer) + value = c.value; + } + CopyOnlyInt &operator=(const CopyOnlyInt &c) { + print_copy_assigned(this, c.value); + value = c.value; + return *this; + } + ~CopyOnlyInt() { print_destroyed(this); } + + int value; +}; +PYBIND11_NAMESPACE_BEGIN(pybind11) +PYBIND11_NAMESPACE_BEGIN(detail) +template <> +struct type_caster { + PYBIND11_TYPE_CASTER(MoveOnlyInt, const_name("MoveOnlyInt")); + bool load(handle src, bool) { + value = MoveOnlyInt(src.cast()); + return true; + } + static handle cast(const MoveOnlyInt &m, return_value_policy r, handle p) { + return pybind11::cast(m.value, r, p); + } +}; + +template <> +struct type_caster { + PYBIND11_TYPE_CASTER(MoveOrCopyInt, const_name("MoveOrCopyInt")); + bool load(handle src, bool) { + value = MoveOrCopyInt(src.cast()); + return true; + } + static handle cast(const MoveOrCopyInt &m, return_value_policy r, handle p) { + return pybind11::cast(m.value, r, p); + } +}; + +template <> +struct type_caster { +protected: + CopyOnlyInt value; + +public: + static constexpr auto name = const_name("CopyOnlyInt"); + bool load(handle src, bool) { + value = CopyOnlyInt(src.cast()); + return true; + } + static handle cast(const CopyOnlyInt &m, return_value_policy r, handle p) { + return pybind11::cast(m.value, r, p); + } + static handle cast(const CopyOnlyInt *src, return_value_policy policy, handle parent) { + if (!src) { + return none().release(); + } + return cast(*src, policy, parent); + } + explicit operator CopyOnlyInt *() { return &value; } + explicit operator CopyOnlyInt &() { return value; } + template + using cast_op_type = pybind11::detail::cast_op_type; +}; +PYBIND11_NAMESPACE_END(detail) +PYBIND11_NAMESPACE_END(pybind11) + +namespace { + +py::object CastUnusualOpRefConstRef(const UnusualOpRef &cref) { return py::cast(cref); } +py::object CastUnusualOpRefMovable(UnusualOpRef &&mvbl) { return py::cast(std::move(mvbl)); } + +} // namespace + +TEST_SUBMODULE(copy_move_policies, m) { + // test_lacking_copy_ctor + py::class_(m, "lacking_copy_ctor") + .def_static("get_one", &lacking_copy_ctor::get_one, py::return_value_policy::copy); + // test_lacking_move_ctor + py::class_(m, "lacking_move_ctor") + .def_static("get_one", &lacking_move_ctor::get_one, py::return_value_policy::move); + + // test_move_and_copy_casts + // NOLINTNEXTLINE(performance-unnecessary-value-param) + m.def("move_and_copy_casts", [](const py::object &o) { + int r = 0; + r += py::cast(o).value; /* moves */ + r += py::cast(o).value; /* moves */ + r += py::cast(o).value; /* copies */ + auto m1(py::cast(o)); /* moves */ + auto m2(py::cast(o)); /* moves */ + auto m3(py::cast(o)); /* copies */ + r += m1.value + m2.value + m3.value; + + return r; + }); + + // test_move_and_copy_loads + m.def("move_only", [](MoveOnlyInt m) { return m.value; }); + // NOLINTNEXTLINE(performance-unnecessary-value-param): we want to test copying + m.def("move_or_copy", [](MoveOrCopyInt m) { return m.value; }); + // NOLINTNEXTLINE(performance-unnecessary-value-param): we want to test copying + m.def("copy_only", [](CopyOnlyInt m) { return m.value; }); + m.def("move_pair", + [](std::pair p) { return p.first.value + p.second.value; }); + m.def("move_tuple", [](std::tuple t) { + return std::get<0>(t).value + std::get<1>(t).value + std::get<2>(t).value; + }); + m.def("copy_tuple", [](std::tuple t) { + return std::get<0>(t).value + std::get<1>(t).value; + }); + m.def("move_copy_nested", + [](std::pair>, + MoveOrCopyInt>> x) { + return x.first.value + std::get<0>(x.second.first).value + + std::get<1>(x.second.first).value + + std::get<0>(std::get<2>(x.second.first)).value + x.second.second.value; + }); + m.def("move_and_copy_cstats", []() { + ConstructorStats::gc(); + // Reset counts to 0 so that previous tests don't affect later ones: + auto &mc = ConstructorStats::get(); + mc.move_assignments = mc.move_constructions = mc.copy_assignments = mc.copy_constructions + = 0; + auto &mo = ConstructorStats::get(); + mo.move_assignments = mo.move_constructions = mo.copy_assignments = mo.copy_constructions + = 0; + auto &co = ConstructorStats::get(); + co.move_assignments = co.move_constructions = co.copy_assignments = co.copy_constructions + = 0; + py::dict d; + d["MoveOrCopyInt"] = py::cast(mc, py::return_value_policy::reference); + d["MoveOnlyInt"] = py::cast(mo, py::return_value_policy::reference); + d["CopyOnlyInt"] = py::cast(co, py::return_value_policy::reference); + return d; + }); +#ifdef PYBIND11_HAS_OPTIONAL + // test_move_and_copy_load_optional + m.attr("has_optional") = true; + m.def("move_optional", [](std::optional o) { return o->value; }); + m.def("move_or_copy_optional", [](std::optional o) { return o->value; }); + m.def("copy_optional", [](std::optional o) { return o->value; }); + m.def("move_optional_tuple", + [](std::optional> x) { + return std::get<0>(*x).value + std::get<1>(*x).value + std::get<2>(*x).value; + }); +#else + m.attr("has_optional") = false; +#endif + + // #70 compilation issue if operator new is not public - simple body added + // but not needed on most compilers; MSVC and nvcc don't like a local + // struct not having a method defined when declared, since it can not be + // added later. + struct PrivateOpNew { + int value = 1; + + private: + void *operator new(size_t bytes) { + void *ptr = std::malloc(bytes); + if (ptr) { + return ptr; + } + throw std::bad_alloc{}; + } + }; + py::class_(m, "PrivateOpNew").def_readonly("value", &PrivateOpNew::value); + m.def("private_op_new_value", []() { return PrivateOpNew(); }); + m.def( + "private_op_new_reference", + []() -> const PrivateOpNew & { + static PrivateOpNew x{}; + return x; + }, + py::return_value_policy::reference); + + // test_move_fallback + // #389: rvp::move should fall-through to copy on non-movable objects + struct MoveIssue1 { + int v; + explicit MoveIssue1(int v) : v{v} {} + MoveIssue1(const MoveIssue1 &c) = default; + MoveIssue1(MoveIssue1 &&) = delete; + }; + py::class_(m, "MoveIssue1") + .def(py::init()) + .def_readwrite("value", &MoveIssue1::v); + + struct MoveIssue2 { + int v; + explicit MoveIssue2(int v) : v{v} {} + MoveIssue2(MoveIssue2 &&) = default; + }; + py::class_(m, "MoveIssue2") + .def(py::init()) + .def_readwrite("value", &MoveIssue2::v); + + // #2742: Don't expect ownership of raw pointer to `new`ed object to be transferred with + // `py::return_value_policy::move` + m.def( + "get_moveissue1", + [](int i) { return std::unique_ptr(new MoveIssue1(i)); }, + 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(); }); + + py::class_(m, "UnusualOpRef"); + m.def("CallCastUnusualOpRefConstRef", + []() { return CastUnusualOpRefConstRef(UnusualOpRef()); }); + m.def("CallCastUnusualOpRefMovable", []() { return CastUnusualOpRefMovable(UnusualOpRef()); }); +} + +/* + * Rest of the file: + * static_assert based tests for pybind11 adaptations of + * std::is_move_constructible, std::is_copy_constructible and + * std::is_copy_assignable (no adaptation of std::is_move_assignable). + * Difference between pybind11 and std traits: pybind11 traits will also check + * the contained value_types. + */ + +struct NotMovable { + NotMovable() = default; + NotMovable(NotMovable const &) = default; + NotMovable(NotMovable &&) = delete; + NotMovable &operator=(NotMovable const &) = default; + NotMovable &operator=(NotMovable &&) = delete; +}; +static_assert(!std::is_move_constructible::value, + "!std::is_move_constructible::value"); +static_assert(std::is_copy_constructible::value, + "std::is_copy_constructible::value"); +static_assert(!pybind11::detail::is_move_constructible::value, + "!pybind11::detail::is_move_constructible::value"); +static_assert(pybind11::detail::is_copy_constructible::value, + "pybind11::detail::is_copy_constructible::value"); +static_assert(!std::is_move_assignable::value, + "!std::is_move_assignable::value"); +static_assert(std::is_copy_assignable::value, + "std::is_copy_assignable::value"); +// pybind11 does not have this +// static_assert(!pybind11::detail::is_move_assignable::value, +// "!pybind11::detail::is_move_assignable::value"); +static_assert(pybind11::detail::is_copy_assignable::value, + "pybind11::detail::is_copy_assignable::value"); + +struct NotCopyable { + NotCopyable() = default; + NotCopyable(NotCopyable const &) = delete; + NotCopyable(NotCopyable &&) = default; + NotCopyable &operator=(NotCopyable const &) = delete; + NotCopyable &operator=(NotCopyable &&) = default; +}; +static_assert(std::is_move_constructible::value, + "std::is_move_constructible::value"); +static_assert(!std::is_copy_constructible::value, + "!std::is_copy_constructible::value"); +static_assert(pybind11::detail::is_move_constructible::value, + "pybind11::detail::is_move_constructible::value"); +static_assert(!pybind11::detail::is_copy_constructible::value, + "!pybind11::detail::is_copy_constructible::value"); +static_assert(std::is_move_assignable::value, + "std::is_move_assignable::value"); +static_assert(!std::is_copy_assignable::value, + "!std::is_copy_assignable::value"); +// pybind11 does not have this +// static_assert(!pybind11::detail::is_move_assignable::value, +// "!pybind11::detail::is_move_assignable::value"); +static_assert(!pybind11::detail::is_copy_assignable::value, + "!pybind11::detail::is_copy_assignable::value"); + +struct NotCopyableNotMovable { + NotCopyableNotMovable() = default; + NotCopyableNotMovable(NotCopyableNotMovable const &) = delete; + NotCopyableNotMovable(NotCopyableNotMovable &&) = delete; + NotCopyableNotMovable &operator=(NotCopyableNotMovable const &) = delete; + NotCopyableNotMovable &operator=(NotCopyableNotMovable &&) = delete; +}; +static_assert(!std::is_move_constructible::value, + "!std::is_move_constructible::value"); +static_assert(!std::is_copy_constructible::value, + "!std::is_copy_constructible::value"); +static_assert(!pybind11::detail::is_move_constructible::value, + "!pybind11::detail::is_move_constructible::value"); +static_assert(!pybind11::detail::is_copy_constructible::value, + "!pybind11::detail::is_copy_constructible::value"); +static_assert(!std::is_move_assignable::value, + "!std::is_move_assignable::value"); +static_assert(!std::is_copy_assignable::value, + "!std::is_copy_assignable::value"); +// pybind11 does not have this +// static_assert(!pybind11::detail::is_move_assignable::value, +// "!pybind11::detail::is_move_assignable::value"); +static_assert(!pybind11::detail::is_copy_assignable::value, + "!pybind11::detail::is_copy_assignable::value"); + +struct NotMovableVector : std::vector {}; +static_assert(std::is_move_constructible::value, + "std::is_move_constructible::value"); +static_assert(std::is_copy_constructible::value, + "std::is_copy_constructible::value"); +static_assert(!pybind11::detail::is_move_constructible::value, + "!pybind11::detail::is_move_constructible::value"); +static_assert(pybind11::detail::is_copy_constructible::value, + "pybind11::detail::is_copy_constructible::value"); +static_assert(std::is_move_assignable::value, + "std::is_move_assignable::value"); +static_assert(std::is_copy_assignable::value, + "std::is_copy_assignable::value"); +// pybind11 does not have this +// static_assert(!pybind11::detail::is_move_assignable::value, +// "!pybind11::detail::is_move_assignable::value"); +static_assert(pybind11::detail::is_copy_assignable::value, + "pybind11::detail::is_copy_assignable::value"); + +struct NotCopyableVector : std::vector {}; +static_assert(std::is_move_constructible::value, + "std::is_move_constructible::value"); +static_assert(std::is_copy_constructible::value, + "std::is_copy_constructible::value"); +static_assert(pybind11::detail::is_move_constructible::value, + "pybind11::detail::is_move_constructible::value"); +static_assert(!pybind11::detail::is_copy_constructible::value, + "!pybind11::detail::is_copy_constructible::value"); +static_assert(std::is_move_assignable::value, + "std::is_move_assignable::value"); +static_assert(std::is_copy_assignable::value, + "std::is_copy_assignable::value"); +// pybind11 does not have this +// static_assert(!pybind11::detail::is_move_assignable::value, +// "!pybind11::detail::is_move_assignable::value"); +static_assert(!pybind11::detail::is_copy_assignable::value, + "!pybind11::detail::is_copy_assignable::value"); + +struct NotCopyableNotMovableVector : std::vector {}; +static_assert(std::is_move_constructible::value, + "std::is_move_constructible::value"); +static_assert(std::is_copy_constructible::value, + "std::is_copy_constructible::value"); +static_assert(!pybind11::detail::is_move_constructible::value, + "!pybind11::detail::is_move_constructible::value"); +static_assert(!pybind11::detail::is_copy_constructible::value, + "!pybind11::detail::is_copy_constructible::value"); +static_assert(std::is_move_assignable::value, + "std::is_move_assignable::value"); +static_assert(std::is_copy_assignable::value, + "std::is_copy_assignable::value"); +// pybind11 does not have this +// static_assert(!pybind11::detail::is_move_assignable::value, +// "!pybind11::detail::is_move_assignable::value"); +static_assert(!pybind11::detail::is_copy_assignable::value, + "!pybind11::detail::is_copy_assignable::value"); + +struct NotMovableMap : std::map {}; +static_assert(std::is_move_constructible::value, + "std::is_move_constructible::value"); +static_assert(std::is_copy_constructible::value, + "std::is_copy_constructible::value"); +static_assert(!pybind11::detail::is_move_constructible::value, + "!pybind11::detail::is_move_constructible::value"); +static_assert(pybind11::detail::is_copy_constructible::value, + "pybind11::detail::is_copy_constructible::value"); +static_assert(std::is_move_assignable::value, + "std::is_move_assignable::value"); +static_assert(std::is_copy_assignable::value, + "std::is_copy_assignable::value"); +// pybind11 does not have this +// static_assert(!pybind11::detail::is_move_assignable::value, +// "!pybind11::detail::is_move_assignable::value"); +static_assert(pybind11::detail::is_copy_assignable::value, + "pybind11::detail::is_copy_assignable::value"); + +struct NotCopyableMap : std::map {}; +static_assert(std::is_move_constructible::value, + "std::is_move_constructible::value"); +static_assert(std::is_copy_constructible::value, + "std::is_copy_constructible::value"); +static_assert(pybind11::detail::is_move_constructible::value, + "pybind11::detail::is_move_constructible::value"); +static_assert(!pybind11::detail::is_copy_constructible::value, + "!pybind11::detail::is_copy_constructible::value"); +static_assert(std::is_move_assignable::value, + "std::is_move_assignable::value"); +static_assert(std::is_copy_assignable::value, + "std::is_copy_assignable::value"); +// pybind11 does not have this +// static_assert(!pybind11::detail::is_move_assignable::value, +// "!pybind11::detail::is_move_assignable::value"); +static_assert(!pybind11::detail::is_copy_assignable::value, + "!pybind11::detail::is_copy_assignable::value"); + +struct NotCopyableNotMovableMap : std::map {}; +static_assert(std::is_move_constructible::value, + "std::is_move_constructible::value"); +static_assert(std::is_copy_constructible::value, + "std::is_copy_constructible::value"); +static_assert(!pybind11::detail::is_move_constructible::value, + "!pybind11::detail::is_move_constructible::value"); +static_assert(!pybind11::detail::is_copy_constructible::value, + "!pybind11::detail::is_copy_constructible::value"); +static_assert(std::is_move_assignable::value, + "std::is_move_assignable::value"); +static_assert(std::is_copy_assignable::value, + "std::is_copy_assignable::value"); +// pybind11 does not have this +// static_assert(!pybind11::detail::is_move_assignable::value, +// "!pybind11::detail::is_move_assignable::value"); +static_assert(!pybind11::detail::is_copy_assignable::value, + "!pybind11::detail::is_copy_assignable::value"); + +struct RecursiveVector : std::vector {}; +static_assert(std::is_move_constructible::value, + "std::is_move_constructible::value"); +static_assert(std::is_copy_constructible::value, + "std::is_copy_constructible::value"); +static_assert(pybind11::detail::is_move_constructible::value, + "pybind11::detail::is_move_constructible::value"); +static_assert(pybind11::detail::is_copy_constructible::value, + "pybind11::detail::is_copy_constructible::value"); +static_assert(std::is_move_assignable::value, + "std::is_move_assignable::value"); +static_assert(std::is_copy_assignable::value, + "std::is_copy_assignable::value"); +// pybind11 does not have this +// static_assert(!pybind11::detail::is_move_assignable::value, +// "!pybind11::detail::is_move_assignable::value"); +static_assert(pybind11::detail::is_copy_assignable::value, + "pybind11::detail::is_copy_assignable::value"); + +struct RecursiveMap : std::map {}; +static_assert(std::is_move_constructible::value, + "std::is_move_constructible::value"); +static_assert(std::is_copy_constructible::value, + "std::is_copy_constructible::value"); +static_assert(pybind11::detail::is_move_constructible::value, + "pybind11::detail::is_move_constructible::value"); +static_assert(pybind11::detail::is_copy_constructible::value, + "pybind11::detail::is_copy_constructible::value"); +static_assert(std::is_move_assignable::value, + "std::is_move_assignable::value"); +static_assert(std::is_copy_assignable::value, + "std::is_copy_assignable::value"); +// pybind11 does not have this +// static_assert(!pybind11::detail::is_move_assignable::value, +// "!pybind11::detail::is_move_assignable::value"); +static_assert(pybind11::detail::is_copy_assignable::value, + "pybind11::detail::is_copy_assignable::value"); diff --git a/external_libraries/pybind11/tests/test_copy_move.py b/external_libraries/pybind11/tests/test_copy_move.py new file mode 100644 index 00000000..3a3f2934 --- /dev/null +++ b/external_libraries/pybind11/tests/test_copy_move.py @@ -0,0 +1,144 @@ +from __future__ import annotations + +import pytest + +import env # noqa: F401 +from pybind11_tests import copy_move_policies as m + + +def test_lacking_copy_ctor(): + with pytest.raises(RuntimeError) as excinfo: + m.lacking_copy_ctor.get_one() + assert "is non-copyable!" in str(excinfo.value) + + +def test_lacking_move_ctor(): + with pytest.raises(RuntimeError) as excinfo: + m.lacking_move_ctor.get_one() + assert "is neither movable nor copyable!" in str(excinfo.value) + + +@pytest.mark.skipif("env.GRAALPY", reason="Cannot reliably trigger GC") +def test_move_and_copy_casts(): + """Cast some values in C++ via custom type casters and count the number of moves/copies.""" + + cstats = m.move_and_copy_cstats() + c_m, c_mc, c_c = ( + cstats["MoveOnlyInt"], + cstats["MoveOrCopyInt"], + cstats["CopyOnlyInt"], + ) + + # The type move constructions/assignments below each get incremented: the move assignment comes + # from the type_caster load; the move construction happens when extracting that via a cast or + # loading into an argument. + assert m.move_and_copy_casts(3) == 18 + assert c_m.copy_assignments + c_m.copy_constructions == 0 + assert c_m.move_assignments == 2 + assert c_m.move_constructions >= 2 + assert c_mc.alive() == 0 + assert c_mc.copy_assignments + c_mc.copy_constructions == 0 + assert c_mc.move_assignments == 2 + assert c_mc.move_constructions >= 2 + assert c_c.alive() == 0 + assert c_c.copy_assignments == 2 + assert c_c.copy_constructions >= 2 + assert c_m.alive() + c_mc.alive() + c_c.alive() == 0 + + +@pytest.mark.skipif("env.GRAALPY", reason="Cannot reliably trigger GC") +def test_move_and_copy_loads(): + """Call some functions that load arguments via custom type casters and count the number of + moves/copies.""" + + cstats = m.move_and_copy_cstats() + c_m, c_mc, c_c = ( + cstats["MoveOnlyInt"], + cstats["MoveOrCopyInt"], + cstats["CopyOnlyInt"], + ) + + assert m.move_only(10) == 10 # 1 move, c_m + assert m.move_or_copy(11) == 11 # 1 move, c_mc + assert m.copy_only(12) == 12 # 1 copy, c_c + assert m.move_pair((13, 14)) == 27 # 1 c_m move, 1 c_mc move + assert m.move_tuple((15, 16, 17)) == 48 # 2 c_m moves, 1 c_mc move + assert m.copy_tuple((18, 19)) == 37 # 2 c_c copies + # Direct constructions: 2 c_m moves, 2 c_mc moves, 1 c_c copy + # Extra moves/copies when moving pairs/tuples: 3 c_m, 3 c_mc, 2 c_c + assert m.move_copy_nested((1, ((2, 3, (4,)), 5))) == 15 + + assert c_m.copy_assignments + c_m.copy_constructions == 0 + assert c_m.move_assignments == 6 + assert c_m.move_constructions == 9 + assert c_mc.copy_assignments + c_mc.copy_constructions == 0 + assert c_mc.move_assignments == 5 + assert c_mc.move_constructions == 8 + assert c_c.copy_assignments == 4 + assert c_c.copy_constructions == 6 + assert c_m.alive() + c_mc.alive() + c_c.alive() == 0 + + +@pytest.mark.skipif(not m.has_optional, reason="no ") +@pytest.mark.skipif("env.GRAALPY", reason="Cannot reliably trigger GC") +def test_move_and_copy_load_optional(): + """Tests move/copy loads of std::optional arguments""" + + cstats = m.move_and_copy_cstats() + c_m, c_mc, c_c = ( + cstats["MoveOnlyInt"], + cstats["MoveOrCopyInt"], + cstats["CopyOnlyInt"], + ) + + # The extra move/copy constructions below come from the std::optional move (which has to move + # its arguments): + assert m.move_optional(10) == 10 # c_m: 1 move assign, 2 move construct + assert m.move_or_copy_optional(11) == 11 # c_mc: 1 move assign, 2 move construct + assert m.copy_optional(12) == 12 # c_c: 1 copy assign, 2 copy construct + # 1 move assign + move construct moves each of c_m, c_mc, 1 c_c copy + # +1 move/copy construct each from moving the tuple + # +1 move/copy construct each from moving the optional (which moves the tuple again) + assert m.move_optional_tuple((3, 4, 5)) == 12 + + assert c_m.copy_assignments + c_m.copy_constructions == 0 + assert c_m.move_assignments == 2 + assert c_m.move_constructions == 5 + assert c_mc.copy_assignments + c_mc.copy_constructions == 0 + assert c_mc.move_assignments == 2 + assert c_mc.move_constructions == 5 + assert c_c.copy_assignments == 2 + assert c_c.copy_constructions == 5 + assert c_m.alive() + c_mc.alive() + c_c.alive() == 0 + + +def test_private_op_new(): + """An object with a private `operator new` cannot be returned by value""" + + with pytest.raises(RuntimeError) as excinfo: + m.private_op_new_value() + assert "is neither movable nor copyable" in str(excinfo.value) + + assert m.private_op_new_reference().value == 1 + + +def test_move_fallback(): + """#389: rvp::move should fall-through to copy on non-movable objects""" + + m1 = m.get_moveissue1(1) + 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 + + +def test_unusual_op_ref(): + # Merely to test that this still exists and built successfully. + assert m.CallCastUnusualOpRefConstRef().__class__.__name__ == "UnusualOpRef" + assert m.CallCastUnusualOpRefMovable().__class__.__name__ == "UnusualOpRef" diff --git a/external_libraries/pybind11/tests/test_cpp_conduit.cpp b/external_libraries/pybind11/tests/test_cpp_conduit.cpp new file mode 100644 index 00000000..4ee4f069 --- /dev/null +++ b/external_libraries/pybind11/tests/test_cpp_conduit.cpp @@ -0,0 +1,22 @@ +// Copyright (c) 2024 The pybind Community. + +#include "pybind11_tests.h" +#include "test_cpp_conduit_traveler_bindings.h" + +#include + +namespace pybind11_tests { +namespace test_cpp_conduit { + +TEST_SUBMODULE(cpp_conduit, m) { + m.attr("PYBIND11_PLATFORM_ABI_ID") = py::bytes(PYBIND11_PLATFORM_ABI_ID); + m.attr("cpp_type_info_capsule_Traveler") + = py::capsule(&typeid(Traveler), typeid(std::type_info).name()); + m.attr("cpp_type_info_capsule_int") = py::capsule(&typeid(int), typeid(std::type_info).name()); + + wrap_traveler(m); + wrap_lonely_traveler(m); +} + +} // namespace test_cpp_conduit +} // namespace pybind11_tests diff --git a/external_libraries/pybind11/tests/test_cpp_conduit.py b/external_libraries/pybind11/tests/test_cpp_conduit.py new file mode 100644 index 00000000..652b9b9c --- /dev/null +++ b/external_libraries/pybind11/tests/test_cpp_conduit.py @@ -0,0 +1,183 @@ +# Copyright (c) 2024 The pybind Community. + +from __future__ import annotations + +import importlib +import sys + +import pytest + +import env +from pybind11_tests import cpp_conduit as home_planet + + +def import_warns_freethreaded(name): + if name not in sys.modules and not env.sys_is_gil_enabled(): + with pytest.warns( + RuntimeWarning, match=f"has been enabled to load module '{name}'" + ): + return importlib.import_module(name) + + return importlib.import_module(name) + + +exo_planet_c_api = import_warns_freethreaded("exo_planet_c_api") +exo_planet_pybind11 = import_warns_freethreaded("exo_planet_pybind11") +home_planet_very_lonely_traveler = import_warns_freethreaded( + "home_planet_very_lonely_traveler" +) + + +def test_traveler_getattr_actually_exists(): + t_h = home_planet.Traveler("home") + assert t_h.any_name == "Traveler GetAttr: any_name luggage: home" + + +def test_premium_traveler_getattr_actually_exists(): + t_h = home_planet.PremiumTraveler("home", 7) + assert t_h.secret_name == "PremiumTraveler GetAttr: secret_name points: 7" + + +def test_call_cpp_conduit_success(): + t_h = home_planet.Traveler("home") + cap = t_h._pybind11_conduit_v1_( + home_planet.PYBIND11_PLATFORM_ABI_ID, + home_planet.cpp_type_info_capsule_Traveler, + b"raw_pointer_ephemeral", + ) + assert cap.__class__.__name__ == "PyCapsule" or ( + # Note: this will become unnecessary in the next GraalPy release + env.GRAALPY and cap.__class__.__name__ == "capsule" + ) + + +def test_call_cpp_conduit_platform_abi_id_mismatch(): + t_h = home_planet.Traveler("home") + cap = t_h._pybind11_conduit_v1_( + home_planet.PYBIND11_PLATFORM_ABI_ID + b"MISMATCH", + home_planet.cpp_type_info_capsule_Traveler, + b"raw_pointer_ephemeral", + ) + assert cap is None + + +def test_call_cpp_conduit_cpp_type_info_capsule_mismatch(): + t_h = home_planet.Traveler("home") + cap = t_h._pybind11_conduit_v1_( + home_planet.PYBIND11_PLATFORM_ABI_ID, + home_planet.cpp_type_info_capsule_int, + b"raw_pointer_ephemeral", + ) + assert cap is None + + +def test_call_cpp_conduit_pointer_kind_invalid(): + t_h = home_planet.Traveler("home") + with pytest.raises( + RuntimeError, match='^Invalid pointer_kind: "raw_pointer_ephemreal"$' + ): + t_h._pybind11_conduit_v1_( + home_planet.PYBIND11_PLATFORM_ABI_ID, + home_planet.cpp_type_info_capsule_Traveler, + b"raw_pointer_ephemreal", + ) + + +def test_home_only_basic(): + t_h = home_planet.Traveler("home") + assert t_h.luggage == "home" + assert home_planet.get_luggage(t_h) == "home" + + +def test_home_only_premium(): + p_h = home_planet.PremiumTraveler("home", 2) + assert p_h.luggage == "home" + assert home_planet.get_luggage(p_h) == "home" + assert home_planet.get_points(p_h) == 2 + + +def test_exo_only_basic(): + t_e = exo_planet_pybind11.Traveler("exo") + assert t_e.luggage == "exo" + assert exo_planet_pybind11.get_luggage(t_e) == "exo" + + +def test_exo_only_premium(): + p_e = exo_planet_pybind11.PremiumTraveler("exo", 3) + assert p_e.luggage == "exo" + assert exo_planet_pybind11.get_luggage(p_e) == "exo" + assert exo_planet_pybind11.get_points(p_e) == 3 + + +def test_home_passed_to_exo_basic(): + t_h = home_planet.Traveler("home") + assert exo_planet_pybind11.get_luggage(t_h) == "home" + + +def test_exo_passed_to_home_basic(): + t_e = exo_planet_pybind11.Traveler("exo") + assert home_planet.get_luggage(t_e) == "exo" + + +def test_home_passed_to_exo_premium(): + p_h = home_planet.PremiumTraveler("home", 2) + assert exo_planet_pybind11.get_luggage(p_h) == "home" + assert exo_planet_pybind11.get_points(p_h) == 2 + + +def test_exo_passed_to_home_premium(): + p_e = exo_planet_pybind11.PremiumTraveler("exo", 3) + assert home_planet.get_luggage(p_e) == "exo" + assert home_planet.get_points(p_e) == 3 + + +@pytest.mark.parametrize( + "traveler_type", [home_planet.Traveler, exo_planet_pybind11.Traveler] +) +def test_exo_planet_c_api_traveler(traveler_type): + t = traveler_type("socks") + assert exo_planet_c_api.GetLuggage(t) == "socks" + + +@pytest.mark.parametrize( + "premium_traveler_type", + [home_planet.PremiumTraveler, exo_planet_pybind11.PremiumTraveler], +) +def test_exo_planet_c_api_premium_traveler(premium_traveler_type): + pt = premium_traveler_type("gucci", 5) + assert exo_planet_c_api.GetLuggage(pt) == "gucci" + assert exo_planet_c_api.GetPoints(pt) == 5 + + +def test_home_planet_wrap_very_lonely_traveler(): + # This does not exercise the cpp_conduit feature, but is here to + # demonstrate that the cpp_conduit feature does not solve + # cross-extension base-and-derived class interoperability issues. + # Here is the proof that the following works for extensions with + # matching `PYBIND11_INTERNALS_ID`s: + # test_cpp_conduit.cpp: + # py::class_ + # home_planet_very_lonely_traveler.cpp: + # py::class_ + # See test_exo_planet_pybind11_wrap_very_lonely_traveler() for the negative + # test. + assert home_planet.LonelyTraveler is not None # Verify that the base class exists. + home_planet_very_lonely_traveler.wrap_very_lonely_traveler() + # Ensure that the derived class exists. + assert home_planet_very_lonely_traveler.VeryLonelyTraveler is not None + + +def test_exo_planet_pybind11_wrap_very_lonely_traveler(): + # See comment under test_home_planet_wrap_very_lonely_traveler() first. + # Here the `PYBIND11_INTERNALS_ID`s don't match between: + # test_cpp_conduit.cpp: + # py::class_ + # exo_planet_pybind11.cpp: + # py::class_ + assert home_planet.LonelyTraveler is not None # Verify that the base class exists. + with pytest.raises( + RuntimeError, + match='^generic_type: type "VeryLonelyTraveler" referenced unknown base type ' + '"pybind11_tests::test_cpp_conduit::LonelyTraveler"$', + ): + exo_planet_pybind11.wrap_very_lonely_traveler() diff --git a/external_libraries/pybind11/tests/test_cpp_conduit_traveler_bindings.h b/external_libraries/pybind11/tests/test_cpp_conduit_traveler_bindings.h new file mode 100644 index 00000000..4e52c90c --- /dev/null +++ b/external_libraries/pybind11/tests/test_cpp_conduit_traveler_bindings.h @@ -0,0 +1,47 @@ +// Copyright (c) 2024 The pybind Community. + +#pragma once + +#include + +#include "test_cpp_conduit_traveler_types.h" + +#include + +namespace pybind11_tests { +namespace test_cpp_conduit { + +namespace py = pybind11; + +inline void wrap_traveler(py::module_ m) { + py::class_(m, "Traveler") + .def(py::init()) + .def_readwrite("luggage", &Traveler::luggage) + // See issue #3788: + .def("__getattr__", [](const Traveler &self, const std::string &key) { + return "Traveler GetAttr: " + key + " luggage: " + self.luggage; + }); + + m.def("get_luggage", [](const Traveler &person) { return person.luggage; }); + + py::class_(m, "PremiumTraveler") + .def(py::init()) + .def_readwrite("points", &PremiumTraveler::points) + // See issue #3788: + .def("__getattr__", [](const PremiumTraveler &self, const std::string &key) { + return "PremiumTraveler GetAttr: " + key + " points: " + std::to_string(self.points); + }); + + m.def("get_points", [](const PremiumTraveler &person) { return person.points; }); +} + +inline void wrap_lonely_traveler(py::module_ m) { + py::class_(std::move(m), "LonelyTraveler"); +} + +inline void wrap_very_lonely_traveler(py::module_ m) { + py::class_(std::move(m), "VeryLonelyTraveler"); +} + +} // namespace test_cpp_conduit +} // namespace pybind11_tests diff --git a/external_libraries/pybind11/tests/test_cpp_conduit_traveler_types.h b/external_libraries/pybind11/tests/test_cpp_conduit_traveler_types.h new file mode 100644 index 00000000..b8e6a5a7 --- /dev/null +++ b/external_libraries/pybind11/tests/test_cpp_conduit_traveler_types.h @@ -0,0 +1,25 @@ +// Copyright (c) 2024 The pybind Community. + +#pragma once + +#include + +namespace pybind11_tests { +namespace test_cpp_conduit { + +struct Traveler { + explicit Traveler(const std::string &luggage) : luggage(luggage) {} + std::string luggage; +}; + +struct PremiumTraveler : Traveler { + explicit PremiumTraveler(const std::string &luggage, int points) + : Traveler(luggage), points(points) {} + int points; +}; + +struct LonelyTraveler {}; +struct VeryLonelyTraveler : LonelyTraveler {}; + +} // namespace test_cpp_conduit +} // namespace pybind11_tests diff --git a/external_libraries/pybind11/tests/test_cross_module_rtti/CMakeLists.txt b/external_libraries/pybind11/tests/test_cross_module_rtti/CMakeLists.txt new file mode 100644 index 00000000..c9b95bfb --- /dev/null +++ b/external_libraries/pybind11/tests/test_cross_module_rtti/CMakeLists.txt @@ -0,0 +1,70 @@ +possibly_uninitialized(PYTHON_MODULE_EXTENSION Python_INTERPRETER_ID) + +set(CMAKE_CXX_VISIBILITY_PRESET hidden) + +if("${PYTHON_MODULE_EXTENSION}" MATCHES "pypy" + OR "${Python_INTERPRETER_ID}" STREQUAL "PyPy" + OR "${PYTHON_MODULE_EXTENSION}" MATCHES "graalpy") + message(STATUS "Skipping visibility test on PyPy or GraalPy") + add_custom_target(test_cross_module_rtti + )# Dummy target on PyPy or GraalPy. Embedding is not supported. + set(_suppress_unused_variable_warning "${DOWNLOAD_CATCH}") + return() +endif() + +if(TARGET Python::Module AND NOT TARGET Python::Python) + message(STATUS "Skipping visibility test since no embed libs found") + add_custom_target(test_cross_module_rtti) # Dummy target since embedding is not supported. + set(_suppress_unused_variable_warning "${DOWNLOAD_CATCH}") + return() +endif() + +find_package(Catch 2.13.10) + +if(CATCH_FOUND) + message(STATUS "Building interpreter tests using Catch v${CATCH_VERSION}") +else() + message(STATUS "Catch not detected. Interpreter tests will be skipped. Install Catch headers" + " manually or use `cmake -DDOWNLOAD_CATCH=ON` to fetch them automatically.") + return() +endif() + +include(GenerateExportHeader) + +add_library(test_cross_module_rtti_lib SHARED lib.h lib.cpp) +add_library(test_cross_module_rtti_lib::test_cross_module_rtti_lib ALIAS + test_cross_module_rtti_lib) +target_include_directories(test_cross_module_rtti_lib PUBLIC ${CMAKE_CURRENT_BINARY_DIR}) +target_include_directories(test_cross_module_rtti_lib PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}) +target_compile_features(test_cross_module_rtti_lib PUBLIC cxx_std_11) + +generate_export_header(test_cross_module_rtti_lib) + +pybind11_add_module(test_cross_module_rtti_bindings SHARED bindings.cpp) +target_link_libraries(test_cross_module_rtti_bindings + PUBLIC test_cross_module_rtti_lib::test_cross_module_rtti_lib) + +add_executable(test_cross_module_rtti_main catch.cpp test_cross_module_rtti.cpp) +target_link_libraries( + test_cross_module_rtti_main PUBLIC test_cross_module_rtti_lib::test_cross_module_rtti_lib + pybind11::embed Catch2::Catch2) + +# Ensure that we have built the python bindings since we load them in main +add_dependencies(test_cross_module_rtti_main test_cross_module_rtti_bindings) + +pybind11_enable_warnings(test_cross_module_rtti_main) +pybind11_enable_warnings(test_cross_module_rtti_bindings) +pybind11_enable_warnings(test_cross_module_rtti_lib) + +add_custom_target( + test_cross_module_rtti + COMMAND "$" + DEPENDS test_cross_module_rtti_main + WORKING_DIRECTORY "$" + USES_TERMINAL # Ensures output is shown immediately (not buffered and possibly lost on crash) +) + +set_target_properties(test_cross_module_rtti_bindings PROPERTIES LIBRARY_OUTPUT_DIRECTORY + "${CMAKE_CURRENT_BINARY_DIR}") + +add_dependencies(check test_cross_module_rtti) diff --git a/external_libraries/pybind11/tests/test_cross_module_rtti/bindings.cpp b/external_libraries/pybind11/tests/test_cross_module_rtti/bindings.cpp new file mode 100644 index 00000000..94fa6874 --- /dev/null +++ b/external_libraries/pybind11/tests/test_cross_module_rtti/bindings.cpp @@ -0,0 +1,20 @@ +#include + +#include + +class BaseTrampoline : public lib::Base, public pybind11::trampoline_self_life_support { +public: + using lib::Base::Base; + int get() const override { PYBIND11_OVERLOAD(int, lib::Base, get); } +}; + +PYBIND11_MODULE(test_cross_module_rtti_bindings, m) { + pybind11::classh(m, "Base") + .def(pybind11::init()) + .def_readwrite("a", &lib::Base::a) + .def_readwrite("b", &lib::Base::b); + + m.def("get_foo", [](int a, int b) -> std::shared_ptr { + return std::make_shared(a, b); + }); +} diff --git a/external_libraries/pybind11/tests/test_cross_module_rtti/catch.cpp b/external_libraries/pybind11/tests/test_cross_module_rtti/catch.cpp new file mode 100644 index 00000000..2debc5ff --- /dev/null +++ b/external_libraries/pybind11/tests/test_cross_module_rtti/catch.cpp @@ -0,0 +1,22 @@ +// The Catch implementation is compiled here. This is a standalone +// translation unit to avoid recompiling it for every test change. + +#include + +// Silence MSVC C++17 deprecation warning from Catch regarding std::uncaught_exceptions (up to +// catch 2.0.1; this should be fixed in the next catch release after 2.0.1). +PYBIND11_WARNING_DISABLE_MSVC(4996) + +// Catch uses _ internally, which breaks gettext style defines +#ifdef _ +# undef _ +#endif + +#define CATCH_CONFIG_RUNNER +#include + +int main(int argc, char *argv[]) { + pybind11::scoped_interpreter guard{}; + auto result = Catch::Session().run(argc, argv); + return result < 0xff ? result : 0xff; +} diff --git a/external_libraries/pybind11/tests/test_cross_module_rtti/lib.cpp b/external_libraries/pybind11/tests/test_cross_module_rtti/lib.cpp new file mode 100644 index 00000000..927ed044 --- /dev/null +++ b/external_libraries/pybind11/tests/test_cross_module_rtti/lib.cpp @@ -0,0 +1,13 @@ +#include + +namespace lib { + +Base::Base(int a, int b) : a(a), b(b) {} + +int Base::get() const { return a + b; } + +Foo::Foo(int a, int b) : Base{a, b} {} + +int Foo::get() const { return 2 * a + b; } + +} // namespace lib diff --git a/external_libraries/pybind11/tests/test_cross_module_rtti/lib.h b/external_libraries/pybind11/tests/test_cross_module_rtti/lib.h new file mode 100644 index 00000000..2f76043c --- /dev/null +++ b/external_libraries/pybind11/tests/test_cross_module_rtti/lib.h @@ -0,0 +1,31 @@ +#pragma once + +#include +#include + +#if defined(_MSC_VER) +__pragma(warning(disable : 4251)) +#endif + + namespace lib { + + class TEST_CROSS_MODULE_RTTI_LIB_EXPORT Base : public std::enable_shared_from_this { + public: + Base(int a, int b); + Base(const Base &) = default; + virtual ~Base() = default; + + virtual int get() const; + + int a; + int b; + }; + + class TEST_CROSS_MODULE_RTTI_LIB_EXPORT Foo : public Base { + public: + Foo(int a, int b); + + int get() const override; + }; + +} // namespace lib diff --git a/external_libraries/pybind11/tests/test_cross_module_rtti/test_cross_module_rtti.cpp b/external_libraries/pybind11/tests/test_cross_module_rtti/test_cross_module_rtti.cpp new file mode 100644 index 00000000..64988b77 --- /dev/null +++ b/external_libraries/pybind11/tests/test_cross_module_rtti/test_cross_module_rtti.cpp @@ -0,0 +1,50 @@ + +#include +#include + +#include +#include + +static constexpr auto script = R"( +import test_cross_module_rtti_bindings + +class Bar(test_cross_module_rtti_bindings.Base): + def __init__(self, a, b): + test_cross_module_rtti_bindings.Base.__init__(self, a, b) + + def get(self): + return 4 * self.a + self.b + + +def get_bar(a, b): + return Bar(a, b) + +)"; + +TEST_CASE("Simple case where without is_alias") { + // "Simple" case this will not have `python_instance_is_alias` set in type_cast_base.h:771 + auto bindings = pybind11::module_::import("test_cross_module_rtti_bindings"); + auto holder = bindings.attr("get_foo")(1, 2); + auto foo = holder.cast>(); + REQUIRE(foo->get() == 4); // 2 * 1 + 2 = 4 +} + +TEST_CASE("Complex case where with it_alias") { + // "Complex" case this will have `python_instance_is_alias` set in type_cast_base.h:771 + pybind11::exec(script); + auto main = pybind11::module::import("__main__"); + + // The critical part of "Bar" is that it will have the `is_alias` `instance` flag set. + // I'm not quite sure what is required to get that flag, this code is derived from a + // larger code where this issue was observed. + auto holder2 = main.attr("get_bar")(1, 2); + + // this will trigger `std::get_deleter` in type_cast_base.h:772 + // This will fail since the program will see two different typeids for `memory::guarded_delete` + // on from the bindings module and one from "main", which will both have + // `__is_type_name_unique` as true and but still have different values. Hence we will not find + // the deleter and the cast fill fail. See "__eq(__type_name_t __lhs, __type_name_t __rhs)" in + // typeinfo in libc++ + auto bar = holder2.cast>(); + REQUIRE(bar->get() == 6); // 4 * 1 + 2 = 6 +} diff --git a/external_libraries/pybind11/tests/test_custom_type_casters.cpp b/external_libraries/pybind11/tests/test_custom_type_casters.cpp new file mode 100644 index 00000000..0ca2d254 --- /dev/null +++ b/external_libraries/pybind11/tests/test_custom_type_casters.cpp @@ -0,0 +1,217 @@ +/* + tests/test_custom_type_casters.cpp -- tests type_caster + + Copyright (c) 2016 Wenzel Jakob + + All rights reserved. Use of this source code is governed by a + BSD-style license that can be found in the LICENSE file. +*/ + +#include "constructor_stats.h" +#include "pybind11_tests.h" + +// py::arg/py::arg_v testing: these arguments just record their argument when invoked +class ArgInspector1 { +public: + std::string arg = "(default arg inspector 1)"; +}; +class ArgInspector2 { +public: + std::string arg = "(default arg inspector 2)"; +}; +class ArgAlwaysConverts {}; + +namespace PYBIND11_NAMESPACE { +namespace detail { +template <> +struct type_caster { +public: + // Classic +#ifdef PYBIND11_DETAIL_UNDERSCORE_BACKWARD_COMPATIBILITY + PYBIND11_TYPE_CASTER(ArgInspector1, _("ArgInspector1")); +#else + PYBIND11_TYPE_CASTER(ArgInspector1, const_name("ArgInspector1")); +#endif + + bool load(handle src, bool convert) { + value.arg = "loading ArgInspector1 argument " + std::string(convert ? "WITH" : "WITHOUT") + + " conversion allowed. " + "Argument value = " + + (std::string) str(src); + return true; + } + + static handle cast(const ArgInspector1 &src, return_value_policy, handle) { + return str(src.arg).release(); + } +}; +template <> +struct type_caster { +public: + PYBIND11_TYPE_CASTER(ArgInspector2, const_name("ArgInspector2")); + + bool load(handle src, bool convert) { + value.arg = "loading ArgInspector2 argument " + std::string(convert ? "WITH" : "WITHOUT") + + " conversion allowed. " + "Argument value = " + + (std::string) str(src); + return true; + } + + static handle cast(const ArgInspector2 &src, return_value_policy, handle) { + return str(src.arg).release(); + } +}; +template <> +struct type_caster { +public: + PYBIND11_TYPE_CASTER(ArgAlwaysConverts, const_name("ArgAlwaysConverts")); + + bool load(handle, bool convert) { return convert; } + + static handle cast(const ArgAlwaysConverts &, return_value_policy, handle) { + return py::none().release(); + } +}; +} // namespace detail +} // namespace PYBIND11_NAMESPACE + +// test_custom_caster_destruction +class DestructionTester { +public: + DestructionTester() { print_default_created(this); } + ~DestructionTester() { print_destroyed(this); } + DestructionTester(const DestructionTester &) { print_copy_created(this); } + DestructionTester(DestructionTester &&) noexcept { print_move_created(this); } + DestructionTester &operator=(const DestructionTester &) { + print_copy_assigned(this); + return *this; + } + DestructionTester &operator=(DestructionTester &&) noexcept { + print_move_assigned(this); + return *this; + } +}; +namespace PYBIND11_NAMESPACE { +namespace detail { +template <> +struct type_caster { + PYBIND11_TYPE_CASTER(DestructionTester, const_name("DestructionTester")); + bool load(handle, bool) { return true; } + + static handle cast(const DestructionTester &, return_value_policy, handle) { + return py::bool_(true).release(); + } +}; +} // namespace detail +} // namespace PYBIND11_NAMESPACE + +// Define type caster outside of `pybind11::detail` and then alias it. +namespace other_lib { +struct MyType {}; +// Corrupt `py` shorthand alias for surrounding context. +namespace py {} +// Corrupt unqualified relative `pybind11` namespace. +namespace PYBIND11_NAMESPACE {} +// Correct alias. +namespace py_ = ::pybind11; +// Define caster. This is effectively no-op, we only ensure it compiles and we +// don't have any symbol collision when using macro mixin. +struct my_caster { + PYBIND11_TYPE_CASTER(MyType, py_::detail::const_name("MyType")); + bool load(py_::handle, bool) { return true; } + + static py_::handle cast(const MyType &, py_::return_value_policy, py_::handle) { + return py_::bool_(true).release(); + } +}; +} // namespace other_lib +// Effectively "alias" it into correct namespace (via inheritance). +namespace PYBIND11_NAMESPACE { +namespace detail { +template <> +struct type_caster : public other_lib::my_caster {}; +} // namespace detail +} // namespace PYBIND11_NAMESPACE + +// This simply is required to compile +namespace ADL_issue { +template +OutStringType concat(Args &&...) { + return OutStringType(); +} + +struct test {}; +} // namespace ADL_issue + +TEST_SUBMODULE(custom_type_casters, m) { + // test_custom_type_casters + + // test_noconvert_args + // + // Test converting. The ArgAlwaysConverts is just there to make the first no-conversion pass + // fail so that our call always ends up happening via the second dispatch (the one that allows + // some conversion). + class ArgInspector { + public: + ArgInspector1 f(ArgInspector1 a, ArgAlwaysConverts) { return a; } + std::string g(const ArgInspector1 &a, + const ArgInspector1 &b, + int c, + ArgInspector2 *d, + ArgAlwaysConverts) { + return a.arg + "\n" + b.arg + "\n" + std::to_string(c) + "\n" + d->arg; + } + static ArgInspector2 h(ArgInspector2 a, ArgAlwaysConverts) { return a; } + }; + // [workaround(intel)] ICC 20/21 breaks with py::arg().stuff, using py::arg{}.stuff works. + py::class_(m, "ArgInspector") + .def(py::init<>()) + .def("f", &ArgInspector::f, py::arg(), py::arg() = ArgAlwaysConverts()) + .def("g", + &ArgInspector::g, + "a"_a.noconvert(), + "b"_a, + "c"_a.noconvert() = 13, + "d"_a = ArgInspector2(), + py::arg() = ArgAlwaysConverts()) + .def_static("h", &ArgInspector::h, py::arg{}.noconvert(), py::arg() = ArgAlwaysConverts()); + m.def( + "arg_inspect_func", + [](const ArgInspector2 &a, const ArgInspector1 &b, ArgAlwaysConverts) { + return a.arg + "\n" + b.arg; + }, + py::arg{}.noconvert(false), + py::arg_v(nullptr, ArgInspector1()).noconvert(true), + py::arg() = ArgAlwaysConverts()); + + m.def("floats_preferred", [](double f) { return 0.5 * f; }, "f"_a); + m.def("floats_only", [](double f) { return 0.5 * f; }, "f"_a.noconvert()); + m.def("ints_preferred", [](int i) { return i / 2; }, "i"_a); + m.def("ints_only", [](int i) { return i / 2; }, "i"_a.noconvert()); + + // test_custom_caster_destruction + // Test that `take_ownership` works on types with a custom type caster when given a pointer + + // default policy: don't take ownership: + m.def("custom_caster_no_destroy", []() { + static auto *dt = new DestructionTester(); + return dt; + }); + + m.def( + "custom_caster_destroy", + []() { return new DestructionTester(); }, + py::return_value_policy::take_ownership); // Takes ownership: destroy when finished + m.def( + "custom_caster_destroy_const", + []() -> const DestructionTester * { return new DestructionTester(); }, + py::return_value_policy::take_ownership); // Likewise (const doesn't inhibit destruction) + m.def("destruction_tester_cstats", + &ConstructorStats::get, + py::return_value_policy::reference); + + m.def("other_lib_type", [](other_lib::MyType x) { return x; }); + + m.def("_adl_issue", [](const ADL_issue::test &) {}); +} diff --git a/external_libraries/pybind11/tests/test_custom_type_casters.py b/external_libraries/pybind11/tests/test_custom_type_casters.py new file mode 100644 index 00000000..6ed1c564 --- /dev/null +++ b/external_libraries/pybind11/tests/test_custom_type_casters.py @@ -0,0 +1,126 @@ +from __future__ import annotations + +import pytest + +import env # noqa: F401 +from pybind11_tests import custom_type_casters as m + + +def test_noconvert_args(msg): + a = m.ArgInspector() + assert ( + msg(a.f("hi")) + == """ + loading ArgInspector1 argument WITH conversion allowed. Argument value = hi + """ + ) + assert ( + msg(a.g("this is a", "this is b")) + == """ + loading ArgInspector1 argument WITHOUT conversion allowed. Argument value = this is a + loading ArgInspector1 argument WITH conversion allowed. Argument value = this is b + 13 + loading ArgInspector2 argument WITH conversion allowed. Argument value = (default arg inspector 2) + """ + ) + assert ( + msg(a.g("this is a", "this is b", 42)) + == """ + loading ArgInspector1 argument WITHOUT conversion allowed. Argument value = this is a + loading ArgInspector1 argument WITH conversion allowed. Argument value = this is b + 42 + loading ArgInspector2 argument WITH conversion allowed. Argument value = (default arg inspector 2) + """ + ) + assert ( + msg(a.g("this is a", "this is b", 42, "this is d")) + == """ + loading ArgInspector1 argument WITHOUT conversion allowed. Argument value = this is a + loading ArgInspector1 argument WITH conversion allowed. Argument value = this is b + 42 + loading ArgInspector2 argument WITH conversion allowed. Argument value = this is d + """ + ) + assert ( + a.h("arg 1") + == "loading ArgInspector2 argument WITHOUT conversion allowed. Argument value = arg 1" + ) + assert ( + msg(m.arg_inspect_func("A1", "A2")) + == """ + loading ArgInspector2 argument WITH conversion allowed. Argument value = A1 + loading ArgInspector1 argument WITHOUT conversion allowed. Argument value = A2 + """ + ) + + assert m.floats_preferred(4) == 2.0 + assert m.floats_only(4.0) == 2.0 + with pytest.raises(TypeError) as excinfo: + m.floats_only(4) + assert ( + msg(excinfo.value) + == """ + floats_only(): incompatible function arguments. The following argument types are supported: + 1. (f: float) -> float + + Invoked with: 4 + """ + ) + + assert m.ints_preferred(4) == 2 + assert m.ints_preferred(True) == 0 + with pytest.raises(TypeError) as excinfo: + m.ints_preferred(4.0) + assert ( + msg(excinfo.value) + == """ + ints_preferred(): incompatible function arguments. The following argument types are supported: + 1. (i: typing.SupportsInt | typing.SupportsIndex) -> int + + Invoked with: 4.0 + """ + ) + + assert m.ints_only(4) == 2 + with pytest.raises(TypeError) as excinfo: + m.ints_only(4.0) + assert ( + msg(excinfo.value) + == """ + ints_only(): incompatible function arguments. The following argument types are supported: + 1. (i: int) -> int + + Invoked with: 4.0 + """ + ) + + +@pytest.mark.skipif("env.GRAALPY", reason="Cannot reliably trigger GC") +def test_custom_caster_destruction(): + """Tests that returning a pointer to a type that gets converted with a custom type caster gets + destroyed when the function has py::return_value_policy::take_ownership policy applied. + """ + + cstats = m.destruction_tester_cstats() + # This one *doesn't* have take_ownership: the pointer should be used but not destroyed: + z = m.custom_caster_no_destroy() + assert cstats.alive() == 1 + assert cstats.default_constructions == 1 + assert z + + # take_ownership applied: this constructs a new object, casts it, then destroys it: + z = m.custom_caster_destroy() + assert z + assert cstats.default_constructions == 2 + + # Same, but with a const pointer return (which should *not* inhibit destruction): + z = m.custom_caster_destroy_const() + assert z + assert cstats.default_constructions == 3 + + # Make sure we still only have the original object (from ..._no_destroy()) alive: + assert cstats.alive() == 1 + + +def test_custom_caster_other_lib(): + assert m.other_lib_type(True) diff --git a/external_libraries/pybind11/tests/test_custom_type_setup.cpp b/external_libraries/pybind11/tests/test_custom_type_setup.cpp new file mode 100644 index 00000000..15516b21 --- /dev/null +++ b/external_libraries/pybind11/tests/test_custom_type_setup.cpp @@ -0,0 +1,104 @@ +/* + tests/test_custom_type_setup.cpp -- Tests `pybind11::custom_type_setup` + + Copyright (c) Google LLC + + All rights reserved. Use of this source code is governed by a + BSD-style license that can be found in the LICENSE file. +*/ + +#include +#include + +#include "pybind11_tests.h" + +#include + +namespace py = pybind11; + +namespace { +struct ContainerOwnsPythonObjects { + std::vector list; + + void append(const py::object &obj) { list.emplace_back(obj); } + py::object at(py::ssize_t index) const { + if (index >= size() || index < 0) { + throw py::index_error("Index out of range"); + } + return list.at(py::size_t(index)); + } + py::ssize_t size() const { return py::ssize_t_cast(list.size()); } + void clear() { list.clear(); } +}; + +void add_gc_checkers_with_weakrefs(const py::object &obj) { + py::handle global_capsule = py::detail::get_internals_capsule(); + if (!global_capsule) { + throw std::runtime_error("No global internals capsule found"); + } + (void) py::weakref(obj, py::cpp_function([global_capsule](py::handle weakref) -> void { + py::handle current_global_capsule = py::detail::get_internals_capsule(); + if (!current_global_capsule.is(global_capsule)) { + throw std::runtime_error( + "Global internals capsule was destroyed prematurely"); + } + weakref.dec_ref(); + })) + .release(); + + py::handle local_capsule = py::detail::get_local_internals_capsule(); + if (!local_capsule) { + throw std::runtime_error("No local internals capsule found"); + } + (void) py::weakref( + obj, py::cpp_function([local_capsule](py::handle weakref) -> void { + py::handle current_local_capsule = py::detail::get_local_internals_capsule(); + if (!current_local_capsule.is(local_capsule)) { + throw std::runtime_error("Local internals capsule was destroyed prematurely"); + } + weakref.dec_ref(); + })) + .release(); +} +} // namespace + +TEST_SUBMODULE(custom_type_setup, m) { + py::class_ cls( + m, + "ContainerOwnsPythonObjects", + // Please review/update docs/advanced/classes.rst after making changes here. + py::custom_type_setup([](PyHeapTypeObject *heap_type) { + auto *type = &heap_type->ht_type; + type->tp_flags |= Py_TPFLAGS_HAVE_GC; + type->tp_traverse = [](PyObject *self_base, visitproc visit, void *arg) { +// https://docs.python.org/3/c-api/typeobj.html#c.PyTypeObject.tp_traverse +#if PY_VERSION_HEX >= 0x03090000 + Py_VISIT(Py_TYPE(self_base)); +#endif + if (py::detail::is_holder_constructed(self_base)) { + auto &self = py::cast(py::handle(self_base)); + for (auto &item : self.list) { + Py_VISIT(item.ptr()); + } + } + return 0; + }; + type->tp_clear = [](PyObject *self_base) { + if (py::detail::is_holder_constructed(self_base)) { + auto &self = py::cast(py::handle(self_base)); + for (auto &item : self.list) { + Py_CLEAR(item.ptr()); + } + self.list.clear(); + } + return 0; + }; + })); + cls.def(py::init<>()); + cls.def("append", &ContainerOwnsPythonObjects::append); + cls.def("at", &ContainerOwnsPythonObjects::at); + cls.def("size", &ContainerOwnsPythonObjects::size); + cls.def("clear", &ContainerOwnsPythonObjects::clear); + + m.def("add_gc_checkers_with_weakrefs", &add_gc_checkers_with_weakrefs); +} diff --git a/external_libraries/pybind11/tests/test_custom_type_setup.py b/external_libraries/pybind11/tests/test_custom_type_setup.py new file mode 100644 index 00000000..a200b02b --- /dev/null +++ b/external_libraries/pybind11/tests/test_custom_type_setup.py @@ -0,0 +1,79 @@ +from __future__ import annotations + +import gc +import os +import weakref + +import pytest + +import env +import pybind11_tests +from pybind11_tests import custom_type_setup as m + + +@pytest.fixture +def gc_tester(): + """Tests that an object is garbage collected. + + Assumes that any unreferenced objects are fully collected after calling + `gc.collect()`. That is true on CPython, but does not appear to reliably + hold on PyPy. + """ + + weak_refs = [] + + def add_ref(obj): + # PyPy does not support `gc.is_tracked`. + if hasattr(gc, "is_tracked"): + assert gc.is_tracked(obj) + weak_refs.append(weakref.ref(obj)) + + yield add_ref + + gc.collect() + for ref in weak_refs: + assert ref() is None + + +# PyPy does not seem to reliably garbage collect. +@pytest.mark.skipif("env.PYPY or env.GRAALPY") +def test_self_cycle(gc_tester): + obj = m.ContainerOwnsPythonObjects() + obj.append(obj) + gc_tester(obj) + + +# PyPy does not seem to reliably garbage collect. +@pytest.mark.skipif("env.PYPY or env.GRAALPY") +def test_indirect_cycle(gc_tester): + obj = m.ContainerOwnsPythonObjects() + obj.append([obj]) + gc_tester(obj) + + +@pytest.mark.skipif("env.PYPY or env.GRAALPY") +def test_py_cast_useable_on_shutdown(): + """Test that py::cast works during interpreter shutdown. + + See PR #5972 and https://github.com/pybind/pybind11/pull/5958#discussion_r2717645230. + """ + env.check_script_success_in_subprocess( + f""" + import sys + + sys.path.insert(0, {os.path.dirname(env.__file__)!r}) + sys.path.insert(0, {os.path.dirname(pybind11_tests.__file__)!r}) + + from pybind11_tests import custom_type_setup as m + + # Create a self-referential cycle that will be collected during shutdown. + # The tp_traverse and tp_clear callbacks call py::cast, which requires + # internals to still be valid. + obj = m.ContainerOwnsPythonObjects() + obj.append(obj) + + # Add weakref callbacks that verify the capsule is still alive when the + # pybind11 object is garbage collected during shutdown. + m.add_gc_checkers_with_weakrefs(obj) + """ + ) diff --git a/external_libraries/pybind11/tests/test_docs_advanced_cast_custom.cpp b/external_libraries/pybind11/tests/test_docs_advanced_cast_custom.cpp new file mode 100644 index 00000000..d4114ad9 --- /dev/null +++ b/external_libraries/pybind11/tests/test_docs_advanced_cast_custom.cpp @@ -0,0 +1,69 @@ +// ######################################################################### +// PLEASE UPDATE docs/advanced/cast/custom.rst IF ANY CHANGES ARE MADE HERE. +// ######################################################################### + +#include "pybind11_tests.h" + +namespace user_space { + +struct Point2D { + double x; + double y; +}; + +Point2D negate(const Point2D &point) { return Point2D{-point.x, -point.y}; } + +} // namespace user_space + +namespace pybind11 { +namespace detail { + +template <> +struct type_caster { + // This macro inserts a lot of boilerplate code and sets the type hint. + // `io_name` is used to specify different type hints for arguments and return values. + // The signature of our negate function would then look like: + // `negate(collections.abc.Sequence[float]) -> tuple[float, float]` + PYBIND11_TYPE_CASTER(user_space::Point2D, + io_name("collections.abc.Sequence[float]", "tuple[float, float]")); + + // C++ -> Python: convert `Point2D` to `tuple[float, float]`. The second and third arguments + // are used to indicate the return value policy and parent object (for + // return_value_policy::reference_internal) and are often ignored by custom casters. + // The return value should reflect the type hint specified by the second argument of `io_name`. + static handle + cast(const user_space::Point2D &number, return_value_policy /*policy*/, handle /*parent*/) { + return py::make_tuple(number.x, number.y).release(); + } + + // Python -> C++: convert a `PyObject` into a `Point2D` and return false upon failure. The + // second argument indicates whether implicit conversions should be allowed. + // The accepted types should reflect the type hint specified by the first argument of + // `io_name`. + bool load(handle src, bool /*convert*/) { + // Check if handle is a Sequence + if (!py::isinstance(src)) { + return false; + } + auto seq = py::reinterpret_borrow(src); + // Check if exactly two values are in the Sequence + if (seq.size() != 2) { + return false; + } + // Check if each element is either a float or an int + for (auto item : seq) { + if (!py::isinstance(item) && !py::isinstance(item)) { + return false; + } + } + value.x = seq[0].cast(); + value.y = seq[1].cast(); + return true; + } +}; + +} // namespace detail +} // namespace pybind11 + +// Bind the negate function +TEST_SUBMODULE(docs_advanced_cast_custom, m) { m.def("negate", user_space::negate); } diff --git a/external_libraries/pybind11/tests/test_docs_advanced_cast_custom.py b/external_libraries/pybind11/tests/test_docs_advanced_cast_custom.py new file mode 100644 index 00000000..5374cbce --- /dev/null +++ b/external_libraries/pybind11/tests/test_docs_advanced_cast_custom.py @@ -0,0 +1,40 @@ +from __future__ import annotations + +from typing import TYPE_CHECKING, Sequence + +if TYPE_CHECKING: + from conftest import SanitizedString + +from pybind11_tests import docs_advanced_cast_custom as m + + +def assert_negate_function( + input_sequence: Sequence[float], + target: tuple[float, float], +) -> None: + output = m.negate(input_sequence) + assert isinstance(output, tuple) + assert len(output) == 2 + assert isinstance(output[0], float) + assert isinstance(output[1], float) + assert output == target + + +def test_negate(doc: SanitizedString) -> None: + assert ( + doc(m.negate) + == "negate(arg0: collections.abc.Sequence[float]) -> tuple[float, float]" + ) + assert_negate_function([1.0, -1.0], (-1.0, 1.0)) + assert_negate_function((1.0, -1.0), (-1.0, 1.0)) + assert_negate_function([1, -1], (-1.0, 1.0)) + assert_negate_function((1, -1), (-1.0, 1.0)) + + +def test_docs() -> None: + ########################################################################### + # PLEASE UPDATE docs/advanced/cast/custom.rst IF ANY CHANGES ARE MADE HERE. + ########################################################################### + point1 = [1.0, -1.0] + point2 = m.negate(point1) + assert point2 == (-1.0, 1.0) diff --git a/external_libraries/pybind11/tests/test_docstring_options.cpp b/external_libraries/pybind11/tests/test_docstring_options.cpp new file mode 100644 index 00000000..de045a7c --- /dev/null +++ b/external_libraries/pybind11/tests/test_docstring_options.cpp @@ -0,0 +1,129 @@ +/* + tests/test_docstring_options.cpp -- generation of docstrings and signatures + + Copyright (c) 2016 Wenzel Jakob + + All rights reserved. Use of this source code is governed by a + BSD-style license that can be found in the LICENSE file. +*/ + +#include "pybind11_tests.h" + +TEST_SUBMODULE(docstring_options, m) { + // test_docstring_options + { + py::options options; + options.disable_function_signatures(); + + m.def("test_function1", [](int, int) {}, py::arg("a"), py::arg("b")); + m.def("test_function2", [](int, int) {}, py::arg("a"), py::arg("b"), "A custom docstring"); + + m.def("test_overloaded1", [](int) {}, py::arg("i"), "Overload docstring"); + m.def("test_overloaded1", [](double) {}, py::arg("d")); + + m.def("test_overloaded2", [](int) {}, py::arg("i"), "overload docstring 1"); + m.def("test_overloaded2", [](double) {}, py::arg("d"), "overload docstring 2"); + + m.def("test_overloaded3", [](int) {}, py::arg("i")); + m.def("test_overloaded3", [](double) {}, py::arg("d"), "Overload docstr"); + + options.enable_function_signatures(); + + m.def("test_function3", [](int, int) {}, py::arg("a"), py::arg("b")); + m.def("test_function4", [](int, int) {}, py::arg("a"), py::arg("b"), "A custom docstring"); + + options.disable_function_signatures().disable_user_defined_docstrings(); + + m.def("test_function5", [](int, int) {}, py::arg("a"), py::arg("b"), "A custom docstring"); + + { + py::options nested_options; + nested_options.enable_user_defined_docstrings(); + m.def( + "test_function6", + [](int, int) {}, + py::arg("a"), + py::arg("b"), + "A custom docstring"); + } + } + + m.def("test_function7", [](int, int) {}, py::arg("a"), py::arg("b"), "A custom docstring"); + + { + py::options options; + options.disable_user_defined_docstrings(); + options.disable_function_signatures(); + + m.def("test_function8", []() {}); + } + + { + py::options options; + options.disable_user_defined_docstrings(); + + struct DocstringTestFoo { + int value; + void setValue(int v) { value = v; } + int getValue() const { return value; } + }; + py::class_(m, "DocstringTestFoo", "This is a class docstring") + .def_property("value_prop", + &DocstringTestFoo::getValue, + &DocstringTestFoo::setValue, + "This is a property docstring"); + } + + { + enum class DocstringTestEnum1 { Member1, Member2 }; + + py::enum_(m, "DocstringTestEnum1", "Enum docstring") + .value("Member1", DocstringTestEnum1::Member1) + .value("Member2", DocstringTestEnum1::Member2); + } + + { + py::options options; + options.enable_enum_members_docstring(); + + enum class DocstringTestEnum2 { Member1, Member2 }; + + py::enum_(m, "DocstringTestEnum2", "Enum docstring") + .value("Member1", DocstringTestEnum2::Member1) + .value("Member2", DocstringTestEnum2::Member2); + } + + { + py::options options; + options.disable_enum_members_docstring(); + + enum class DocstringTestEnum3 { Member1, Member2 }; + + py::enum_(m, "DocstringTestEnum3", "Enum docstring") + .value("Member1", DocstringTestEnum3::Member1) + .value("Member2", DocstringTestEnum3::Member2); + } + + { + py::options options; + options.disable_user_defined_docstrings(); + + enum class DocstringTestEnum4 { Member1, Member2 }; + + py::enum_(m, "DocstringTestEnum4", "Enum docstring") + .value("Member1", DocstringTestEnum4::Member1) + .value("Member2", DocstringTestEnum4::Member2); + } + + { + py::options options; + options.disable_user_defined_docstrings(); + options.disable_enum_members_docstring(); + + enum class DocstringTestEnum5 { Member1, Member2 }; + + py::enum_(m, "DocstringTestEnum5", "Enum docstring") + .value("Member1", DocstringTestEnum5::Member1) + .value("Member2", DocstringTestEnum5::Member2); + } +} diff --git a/external_libraries/pybind11/tests/test_docstring_options.py b/external_libraries/pybind11/tests/test_docstring_options.py new file mode 100644 index 00000000..ffd1a739 --- /dev/null +++ b/external_libraries/pybind11/tests/test_docstring_options.py @@ -0,0 +1,72 @@ +from __future__ import annotations + +from pybind11_tests import docstring_options as m + + +def test_docstring_options(): + # options.disable_function_signatures() + assert not m.test_function1.__doc__ + + assert m.test_function2.__doc__ == "A custom docstring" + + # docstring specified on just the first overload definition: + assert m.test_overloaded1.__doc__ == "Overload docstring" + + # docstring on both overloads: + assert m.test_overloaded2.__doc__ == "overload docstring 1\noverload docstring 2" + + # docstring on only second overload: + assert m.test_overloaded3.__doc__ == "Overload docstr" + + # options.enable_function_signatures() + assert m.test_function3.__doc__.startswith( + "test_function3(a: typing.SupportsInt | typing.SupportsIndex, b: typing.SupportsInt | typing.SupportsIndex) -> None" + ) + + assert m.test_function4.__doc__.startswith( + "test_function4(a: typing.SupportsInt | typing.SupportsIndex, b: typing.SupportsInt | typing.SupportsIndex) -> None" + ) + assert m.test_function4.__doc__.endswith("A custom docstring\n") + + # options.disable_function_signatures() + # options.disable_user_defined_docstrings() + assert not m.test_function5.__doc__ + + # nested options.enable_user_defined_docstrings() + assert m.test_function6.__doc__ == "A custom docstring" + + # RAII destructor + assert m.test_function7.__doc__.startswith( + "test_function7(a: typing.SupportsInt | typing.SupportsIndex, b: typing.SupportsInt | typing.SupportsIndex) -> None" + ) + assert m.test_function7.__doc__.endswith("A custom docstring\n") + + # when all options are disabled, no docstring (instead of an empty one) should be generated + assert m.test_function8.__doc__ is None + + # Suppression of user-defined docstrings for non-function objects + assert not m.DocstringTestFoo.__doc__ + assert not m.DocstringTestFoo.value_prop.__doc__ + + # Check existing behaviour of enum docstings + assert ( + m.DocstringTestEnum1.__doc__ + == "Enum docstring\n\nMembers:\n\n Member1\n\n Member2" + ) + + # options.enable_enum_members_docstring() + assert ( + m.DocstringTestEnum2.__doc__ + == "Enum docstring\n\nMembers:\n\n Member1\n\n Member2" + ) + + # options.disable_enum_members_docstring() + assert m.DocstringTestEnum3.__doc__ == "Enum docstring" + + # options.disable_user_defined_docstrings() + assert m.DocstringTestEnum4.__doc__ == "Members:\n\n Member1\n\n Member2" + + # options.disable_user_defined_docstrings() + # options.disable_enum_members_docstring() + # When all options are disabled, no docstring (instead of an empty one) should be generated + assert m.DocstringTestEnum5.__doc__ is None diff --git a/external_libraries/pybind11/tests/test_eigen_matrix.cpp b/external_libraries/pybind11/tests/test_eigen_matrix.cpp new file mode 100644 index 00000000..96a96c2b --- /dev/null +++ b/external_libraries/pybind11/tests/test_eigen_matrix.cpp @@ -0,0 +1,448 @@ +/* + tests/eigen.cpp -- automatic conversion of Eigen types + + Copyright (c) 2016 Wenzel Jakob + + All rights reserved. Use of this source code is governed by a + BSD-style license that can be found in the LICENSE file. +*/ + +#include +#include + +#include "constructor_stats.h" +#include "pybind11_tests.h" + +PYBIND11_WARNING_DISABLE_MSVC(4996) + +#include + +using MatrixXdR = Eigen::Matrix; + +// Sets/resets a testing reference matrix to have values of 10*r + c, where r and c are the +// (1-based) row/column number. +template +void reset_ref(M &x) { + for (int i = 0; i < x.rows(); i++) { + for (int j = 0; j < x.cols(); j++) { + x(i, j) = 11 + 10 * i + j; + } + } +} + +// Returns a static, column-major matrix +Eigen::MatrixXd &get_cm() { + static Eigen::MatrixXd *x; + if (!x) { + x = new Eigen::MatrixXd(3, 3); + reset_ref(*x); + } + return *x; +} +// Likewise, but row-major +MatrixXdR &get_rm() { + static MatrixXdR *x; + if (!x) { + x = new MatrixXdR(3, 3); + reset_ref(*x); + } + return *x; +} +// Resets the values of the static matrices returned by get_cm()/get_rm() +void reset_refs() { + reset_ref(get_cm()); + reset_ref(get_rm()); +} + +// Returns element 2,1 from a matrix (used to test copy/nocopy) +double get_elem(const Eigen::Ref &m) { return m(2, 1); } + +// Returns a matrix with 10*r + 100*c added to each matrix element (to help test that the matrix +// reference is referencing rows/columns correctly). +template +Eigen::MatrixXd adjust_matrix(MatrixArgType m) { + Eigen::MatrixXd ret(m); + for (int c = 0; c < m.cols(); c++) { + for (int r = 0; r < m.rows(); r++) { + ret(r, c) += 10 * r + 100 * c; // NOLINT(clang-analyzer-core.uninitialized.Assign) + } + } + return ret; +} + +struct CustomOperatorNew { + CustomOperatorNew() = default; + + Eigen::Matrix4d a = Eigen::Matrix4d::Zero(); + Eigen::Matrix4d b = Eigen::Matrix4d::Identity(); + + EIGEN_MAKE_ALIGNED_OPERATOR_NEW +}; + +TEST_SUBMODULE(eigen_matrix, m) { + using FixedMatrixR = Eigen::Matrix; + using FixedMatrixC = Eigen::Matrix; + using DenseMatrixR = Eigen::Matrix; + using DenseMatrixC = Eigen::Matrix; + using FourRowMatrixC = Eigen::Matrix; + using FourColMatrixC = Eigen::Matrix; + using FourRowMatrixR = Eigen::Matrix; + using FourColMatrixR = Eigen::Matrix; + using SparseMatrixR = Eigen::SparseMatrix; + using SparseMatrixC = Eigen::SparseMatrix; + + // various tests + m.def("double_col", [](const Eigen::VectorXf &x) -> Eigen::VectorXf { return 2.0f * x; }); + m.def("double_row", + [](const Eigen::RowVectorXf &x) -> Eigen::RowVectorXf { return 2.0f * x; }); + m.def("double_complex", + [](const Eigen::VectorXcf &x) -> Eigen::VectorXcf { return 2.0f * x; }); + m.def("double_threec", [](py::EigenDRef x) { x *= 2; }); + m.def("double_threer", [](py::EigenDRef x) { x *= 2; }); + m.def("double_mat_cm", [](const Eigen::MatrixXf &x) -> Eigen::MatrixXf { return 2.0f * x; }); + m.def("double_mat_rm", [](const DenseMatrixR &x) -> DenseMatrixR { return 2.0f * x; }); + + // test_eigen_ref_to_python + // Different ways of passing via Eigen::Ref; the first and second are the Eigen-recommended + m.def("cholesky1", + [](const Eigen::Ref &x) -> Eigen::MatrixXd { return x.llt().matrixL(); }); + m.def("cholesky2", [](const Eigen::Ref &x) -> Eigen::MatrixXd { + return x.llt().matrixL(); + }); + m.def("cholesky3", + [](const Eigen::Ref &x) -> Eigen::MatrixXd { return x.llt().matrixL(); }); + m.def("cholesky4", [](const Eigen::Ref &x) -> Eigen::MatrixXd { + return x.llt().matrixL(); + }); + + // test_eigen_ref_mutators + // Mutators: these add some value to the given element using Eigen, but Eigen should be mapping + // into the numpy array data and so the result should show up there. There are three versions: + // one that works on a contiguous-row matrix (numpy's default), one for a contiguous-column + // matrix, and one for any matrix. + auto add_rm = [](Eigen::Ref x, int r, int c, double v) { x(r, c) += v; }; + auto add_cm = [](Eigen::Ref x, int r, int c, double v) { x(r, c) += v; }; + + // Mutators (Eigen maps into numpy variables): + m.def("add_rm", add_rm); // Only takes row-contiguous + m.def("add_cm", add_cm); // Only takes column-contiguous + // Overloaded versions that will accept either row or column contiguous: + m.def("add1", add_rm); + m.def("add1", add_cm); + m.def("add2", add_cm); + m.def("add2", add_rm); + // This one accepts a matrix of any stride: + m.def("add_any", + [](py::EigenDRef x, int r, int c, double v) { x(r, c) += v; }); + + // Return mutable references (numpy maps into eigen variables) + m.def("get_cm_ref", []() { return Eigen::Ref(get_cm()); }); + m.def("get_rm_ref", []() { return Eigen::Ref(get_rm()); }); + // The same references, but non-mutable (numpy maps into eigen variables, but is !writeable) + m.def("get_cm_const_ref", []() { return Eigen::Ref(get_cm()); }); + m.def("get_rm_const_ref", []() { return Eigen::Ref(get_rm()); }); + + m.def("reset_refs", reset_refs); // Restores get_{cm,rm}_ref to original values + + // Increments and returns ref to (same) matrix + m.def( + "incr_matrix", + [](Eigen::Ref m, double v) { + m += Eigen::MatrixXd::Constant(m.rows(), m.cols(), v); + return m; + }, + py::return_value_policy::reference); + + // Same, but accepts a matrix of any strides + m.def( + "incr_matrix_any", + [](py::EigenDRef m, double v) { + m += Eigen::MatrixXd::Constant(m.rows(), m.cols(), v); + return m; + }, + py::return_value_policy::reference); + + // Returns an eigen slice of even rows + m.def( + "even_rows", + [](py::EigenDRef m) { + return py::EigenDMap( + m.data(), + (m.rows() + 1) / 2, + m.cols(), + py::EigenDStride(m.outerStride(), 2 * m.innerStride())); + }, + py::return_value_policy::reference); + + // Returns an eigen slice of even columns + m.def( + "even_cols", + [](py::EigenDRef m) { + return py::EigenDMap( + m.data(), + m.rows(), + (m.cols() + 1) / 2, + py::EigenDStride(2 * m.outerStride(), m.innerStride())); + }, + py::return_value_policy::reference); + + // Returns diagonals: a vector-like object with an inner stride != 1 + m.def("diagonal", [](const Eigen::Ref &x) { return x.diagonal(); }); + m.def("diagonal_1", + [](const Eigen::Ref &x) { return x.diagonal<1>(); }); + m.def("diagonal_n", + [](const Eigen::Ref &x, int index) { return x.diagonal(index); }); + + // Return a block of a matrix (gives non-standard strides) + m.def("block", + [m](const py::object &x_obj, + int start_row, + int start_col, + int block_rows, + int block_cols) { + return m.attr("_block")(x_obj, x_obj, start_row, start_col, block_rows, block_cols); + }); + + m.def( + "_block", + [](const py::object &x_obj, + const Eigen::Ref &x, + int start_row, + int start_col, + int block_rows, + int block_cols) { + // See PR #4217 for background. This test is a bit over the top, but might be useful + // as a concrete example to point to when explaining the dangling reference trap. + auto i0 = py::make_tuple(0, 0); + auto x0_orig = x_obj[*i0].cast(); + if (x(0, 0) != x0_orig) { + throw std::runtime_error( + "Something in the type_caster for Eigen::Ref is terribly wrong."); + } + double x0_mod = x0_orig + 1; + x_obj[*i0] = x0_mod; + auto copy_detected = (x(0, 0) != x0_mod); + x_obj[*i0] = x0_orig; + if (copy_detected) { + throw std::runtime_error("type_caster for Eigen::Ref made a copy."); + } + return x.block(start_row, start_col, block_rows, block_cols); + }, + py::keep_alive<0, 1>()); + + // test_eigen_return_references, test_eigen_keepalive + // return value referencing/copying tests: + class ReturnTester { + Eigen::MatrixXd mat = create(); + + public: + ReturnTester() { print_created(this); } + ReturnTester(const ReturnTester &) = default; + ~ReturnTester() { print_destroyed(this); } + static Eigen::MatrixXd create() { return Eigen::MatrixXd::Ones(10, 10); } + // NOLINTNEXTLINE(readability-const-return-type) + static const Eigen::MatrixXd createConst() { return Eigen::MatrixXd::Ones(10, 10); } + Eigen::MatrixXd &get() { return mat; } + Eigen::MatrixXd *getPtr() { return &mat; } + const Eigen::MatrixXd &view() { return mat; } + const Eigen::MatrixXd *viewPtr() { return &mat; } + Eigen::Ref ref() { return mat; } + Eigen::Ref refConst() { return mat; } + Eigen::Block block(int r, int c, int nrow, int ncol) { + return mat.block(r, c, nrow, ncol); + } + Eigen::Block blockConst(int r, int c, int nrow, int ncol) const { + return mat.block(r, c, nrow, ncol); + } + py::EigenDMap corners() { + return py::EigenDMap( + mat.data(), + py::EigenDStride(mat.outerStride() * (mat.outerSize() - 1), + mat.innerStride() * (mat.innerSize() - 1))); + } + py::EigenDMap cornersConst() const { + return py::EigenDMap( + mat.data(), + py::EigenDStride(mat.outerStride() * (mat.outerSize() - 1), + mat.innerStride() * (mat.innerSize() - 1))); + } + }; + using rvp = py::return_value_policy; + py::class_(m, "ReturnTester") + .def(py::init<>()) + .def_static("create", &ReturnTester::create) + .def_static("create_const", &ReturnTester::createConst) + .def("get", &ReturnTester::get, rvp::reference_internal) + .def("get_ptr", &ReturnTester::getPtr, rvp::reference_internal) + .def("view", &ReturnTester::view, rvp::reference_internal) + .def("view_ptr", &ReturnTester::view, rvp::reference_internal) + .def("copy_get", &ReturnTester::get) // Default rvp: copy + .def("copy_view", &ReturnTester::view) // " + .def("ref", &ReturnTester::ref) // Default for Ref is to reference + .def("ref_const", &ReturnTester::refConst) // Likewise, but const + .def("ref_safe", &ReturnTester::ref, rvp::reference_internal) + .def("ref_const_safe", &ReturnTester::refConst, rvp::reference_internal) + .def("copy_ref", &ReturnTester::ref, rvp::copy) + .def("copy_ref_const", &ReturnTester::refConst, rvp::copy) + .def("block", &ReturnTester::block) + .def("block_safe", &ReturnTester::block, rvp::reference_internal) + .def("block_const", &ReturnTester::blockConst, rvp::reference_internal) + .def("copy_block", &ReturnTester::block, rvp::copy) + .def("corners", &ReturnTester::corners, rvp::reference_internal) + .def("corners_const", &ReturnTester::cornersConst, rvp::reference_internal); + + // test_special_matrix_objects + // Returns a DiagonalMatrix with diagonal (1,2,3,...) + m.def("incr_diag", [](int k) { + Eigen::DiagonalMatrix m(k); + for (int i = 0; i < k; i++) { + m.diagonal()[i] = i + 1; + } + return m; + }); + + // Returns a SelfAdjointView referencing the lower triangle of m + m.def("symmetric_lower", + [](const Eigen::MatrixXi &m) { return m.selfadjointView(); }); + // Returns a SelfAdjointView referencing the lower triangle of m + m.def("symmetric_upper", + [](const Eigen::MatrixXi &m) { return m.selfadjointView(); }); + + // Test matrix for various functions below. + Eigen::MatrixXf mat(5, 6); + mat << 0, 3, 0, 0, 0, 11, 22, 0, 0, 0, 17, 11, 7, 5, 0, 1, 0, 11, 0, 0, 0, 0, 0, 11, 0, 0, 14, + 0, 8, 11; + + // test_fixed, and various other tests + m.def("fixed_r", [mat]() -> FixedMatrixR { return FixedMatrixR(mat); }); + // Our Eigen does a hack which respects constness through the numpy writeable flag. + // Therefore, the const return actually affects this type despite being an rvalue. + // NOLINTNEXTLINE(readability-const-return-type) + m.def("fixed_r_const", [mat]() -> const FixedMatrixR { return FixedMatrixR(mat); }); + m.def("fixed_c", [mat]() -> FixedMatrixC { return FixedMatrixC(mat); }); + m.def("fixed_copy_r", [](const FixedMatrixR &m) -> FixedMatrixR { return m; }); + m.def("fixed_copy_c", [](const FixedMatrixC &m) -> FixedMatrixC { return m; }); + // test_mutator_descriptors + m.def("fixed_mutator_r", [](const Eigen::Ref &) {}); + m.def("fixed_mutator_c", [](const Eigen::Ref &) {}); + m.def("fixed_mutator_a", [](const py::EigenDRef &) {}); + // test_dense + m.def("dense_r", [mat]() -> DenseMatrixR { return DenseMatrixR(mat); }); + m.def("dense_c", [mat]() -> DenseMatrixC { return DenseMatrixC(mat); }); + m.def("dense_copy_r", [](const DenseMatrixR &m) -> DenseMatrixR { return m; }); + m.def("dense_copy_c", [](const DenseMatrixC &m) -> DenseMatrixC { return m; }); + // test_defaults + bool have_numpy = true; + try { + py::module_::import("numpy"); + } catch (const py::error_already_set &) { + have_numpy = false; + } + if (have_numpy) { + py::module_::import("numpy"); + Eigen::Matrix defaultMatrix = Eigen::Matrix3d::Identity(); + m.def("defaults_mat", [](const Eigen::Matrix3d &) {}, py::arg("mat") = defaultMatrix); + + Eigen::VectorXd defaultVector = Eigen::VectorXd::Ones(32); + m.def("defaults_vec", [](const Eigen::VectorXd &) {}, py::arg("vec") = defaultMatrix); + } + // test_sparse, test_sparse_signature + m.def("sparse_r", [mat]() -> SparseMatrixR { + // NOLINTNEXTLINE(clang-analyzer-core.uninitialized.UndefReturn) + return Eigen::SparseView(mat); + }); + m.def("sparse_c", + [mat]() -> SparseMatrixC { return Eigen::SparseView(mat); }); + m.def("sparse_copy_r", [](const SparseMatrixR &m) -> SparseMatrixR { return m; }); + m.def("sparse_copy_c", [](const SparseMatrixC &m) -> SparseMatrixC { return m; }); + // test_partially_fixed + m.def("partial_copy_four_rm_r", [](const FourRowMatrixR &m) -> FourRowMatrixR { return m; }); + m.def("partial_copy_four_rm_c", [](const FourColMatrixR &m) -> FourColMatrixR { return m; }); + m.def("partial_copy_four_cm_r", [](const FourRowMatrixC &m) -> FourRowMatrixC { return m; }); + m.def("partial_copy_four_cm_c", [](const FourColMatrixC &m) -> FourColMatrixC { return m; }); + + // test_cpp_casting + // Test that we can cast a numpy object to a Eigen::MatrixXd explicitly + m.def("cpp_copy", [](py::handle m) { return m.cast()(1, 0); }); + m.def("cpp_ref_c", [](py::handle m) { return m.cast>()(1, 0); }); + m.def("cpp_ref_r", [](py::handle m) { return m.cast>()(1, 0); }); + m.def("cpp_ref_any", + [](py::handle m) { return m.cast>()(1, 0); }); + + // [workaround(intel)] ICC 20/21 breaks with py::arg().stuff, using py::arg{}.stuff works. + + // test_nocopy_wrapper + // Test that we can prevent copying into an argument that would normally copy: First a version + // that would allow copying (if types or strides don't match) for comparison: + m.def("get_elem", &get_elem); + // Now this alternative that calls the tells pybind to fail rather than copy: + m.def( + "get_elem_nocopy", + [](const Eigen::Ref &m) -> double { return get_elem(m); }, + py::arg{}.noconvert()); + // Also test a row-major-only no-copy const ref: + m.def( + "get_elem_rm_nocopy", + [](Eigen::Ref> &m) -> long { + return m(2, 1); + }, + py::arg{}.noconvert()); + + // test_issue738, test_zero_length + // Issue #738: 1×N or N×1 2D matrices were neither accepted nor properly copied with an + // incompatible stride value on the length-1 dimension--but that should be allowed (without + // requiring a copy!) because the stride value can be safely ignored on a size-1 dimension. + // Similarly, 0×N or N×0 matrices were not accepted--again, these should be allowed since + // they contain no data. This particularly affects numpy ≥ 1.23, which sets the strides to + // 0 if any dimension size is 0. + m.def("iss738_f1", + &adjust_matrix &>, + py::arg{}.noconvert()); + m.def("iss738_f2", + &adjust_matrix> &>, + py::arg{}.noconvert()); + + // test_issue1105 + // Issue #1105: when converting from a numpy two-dimensional (Nx1) or (1xN) value into a dense + // eigen Vector or RowVector, the argument would fail to load because the numpy copy would + // fail: numpy won't broadcast a Nx1 into a 1-dimensional vector. + m.def("iss1105_col", [](const Eigen::VectorXd &) { return true; }); + m.def("iss1105_row", [](const Eigen::RowVectorXd &) { return true; }); + + // test_named_arguments + // Make sure named arguments are working properly: + m.def( + "matrix_multiply", + [](const py::EigenDRef &A, + const py::EigenDRef &B) -> Eigen::MatrixXd { + if (A.cols() != B.rows()) { + throw std::domain_error("Nonconformable matrices!"); + } + return A * B; + }, + py::arg("A"), + py::arg("B")); + + // test_custom_operator_new + py::class_(m, "CustomOperatorNew") + .def(py::init<>()) + .def_readonly("a", &CustomOperatorNew::a) + .def_readonly("b", &CustomOperatorNew::b); + + // test_eigen_ref_life_support + // In case of a failure (the caster's temp array does not live long enough), creating + // a new array (np.ones(10)) increases the chances that the temp array will be garbage + // collected and/or that its memory will be overridden with different values. + m.def("get_elem_direct", [](const Eigen::Ref &v) { + py::module_::import("numpy").attr("ones")(10); + return v(5); + }); + m.def("get_elem_indirect", [](std::vector> v) { + py::module_::import("numpy").attr("ones")(10); + return v[0](5); + }); + m.def("round_trip_vector", [](const Eigen::VectorXf &x) -> Eigen::VectorXf { return x; }); + m.def("round_trip_dense", [](const DenseMatrixR &m) -> DenseMatrixR { return m; }); + m.def("round_trip_dense_ref", + [](const Eigen::Ref &m) -> Eigen::Ref { return m; }); +} diff --git a/external_libraries/pybind11/tests/test_eigen_matrix.py b/external_libraries/pybind11/tests/test_eigen_matrix.py new file mode 100644 index 00000000..9324c2a7 --- /dev/null +++ b/external_libraries/pybind11/tests/test_eigen_matrix.py @@ -0,0 +1,839 @@ +from __future__ import annotations + +import pytest + +import env # noqa: F401 +from pybind11_tests import ConstructorStats + +np = pytest.importorskip("numpy") +m = pytest.importorskip("pybind11_tests.eigen_matrix") + + +ref = np.array( + [ + [0.0, 3, 0, 0, 0, 11], + [22, 0, 0, 0, 17, 11], + [7, 5, 0, 1, 0, 11], + [0, 0, 0, 0, 0, 11], + [0, 0, 14, 0, 8, 11], + ] +) + + +def assert_equal_ref(mat): + np.testing.assert_array_equal(mat, ref) + + +def assert_sparse_equal_ref(sparse_mat): + assert_equal_ref(sparse_mat.toarray()) + + +def test_fixed(): + assert_equal_ref(m.fixed_c()) + assert_equal_ref(m.fixed_r()) + assert_equal_ref(m.fixed_copy_r(m.fixed_r())) + assert_equal_ref(m.fixed_copy_c(m.fixed_c())) + assert_equal_ref(m.fixed_copy_r(m.fixed_c())) + assert_equal_ref(m.fixed_copy_c(m.fixed_r())) + + +def test_dense(): + assert_equal_ref(m.dense_r()) + assert_equal_ref(m.dense_c()) + assert_equal_ref(m.dense_copy_r(m.dense_r())) + assert_equal_ref(m.dense_copy_c(m.dense_c())) + assert_equal_ref(m.dense_copy_r(m.dense_c())) + assert_equal_ref(m.dense_copy_c(m.dense_r())) + + +def test_partially_fixed(): + ref2 = np.array([[0.0, 1, 2, 3], [4, 5, 6, 7], [8, 9, 10, 11], [12, 13, 14, 15]]) + np.testing.assert_array_equal(m.partial_copy_four_rm_r(ref2), ref2) + np.testing.assert_array_equal(m.partial_copy_four_rm_c(ref2), ref2) + np.testing.assert_array_equal(m.partial_copy_four_rm_r(ref2[:, 1]), ref2[:, [1]]) + np.testing.assert_array_equal(m.partial_copy_four_rm_c(ref2[0, :]), ref2[[0], :]) + np.testing.assert_array_equal( + m.partial_copy_four_rm_r(ref2[:, (0, 2)]), ref2[:, (0, 2)] + ) + np.testing.assert_array_equal( + m.partial_copy_four_rm_c(ref2[(3, 1, 2), :]), ref2[(3, 1, 2), :] + ) + + np.testing.assert_array_equal(m.partial_copy_four_cm_r(ref2), ref2) + np.testing.assert_array_equal(m.partial_copy_four_cm_c(ref2), ref2) + np.testing.assert_array_equal(m.partial_copy_four_cm_r(ref2[:, 1]), ref2[:, [1]]) + np.testing.assert_array_equal(m.partial_copy_four_cm_c(ref2[0, :]), ref2[[0], :]) + np.testing.assert_array_equal( + m.partial_copy_four_cm_r(ref2[:, (0, 2)]), ref2[:, (0, 2)] + ) + np.testing.assert_array_equal( + m.partial_copy_four_cm_c(ref2[(3, 1, 2), :]), ref2[(3, 1, 2), :] + ) + + # TypeError should be raise for a shape mismatch + functions = [ + m.partial_copy_four_rm_r, + m.partial_copy_four_rm_c, + m.partial_copy_four_cm_r, + m.partial_copy_four_cm_c, + ] + matrix_with_wrong_shape = [[1, 2], [3, 4]] + for f in functions: + with pytest.raises(TypeError) as excinfo: + f(matrix_with_wrong_shape) + assert "incompatible function arguments" in str(excinfo.value) + + +def test_mutator_descriptors(): + zr = np.arange(30, dtype="float32").reshape(5, 6) # row-major + zc = zr.reshape(6, 5).transpose() # column-major + + m.fixed_mutator_r(zr) + m.fixed_mutator_c(zc) + m.fixed_mutator_a(zr) + m.fixed_mutator_a(zc) + with pytest.raises(TypeError) as excinfo: + m.fixed_mutator_r(zc) + assert ( + '(arg0: typing.Annotated[numpy.typing.NDArray[numpy.float32], "[5, 6]",' + ' "flags.writeable", "flags.c_contiguous"]) -> None' in str(excinfo.value) + ) + with pytest.raises(TypeError) as excinfo: + m.fixed_mutator_c(zr) + assert ( + '(arg0: typing.Annotated[numpy.typing.NDArray[numpy.float32], "[5, 6]",' + ' "flags.writeable", "flags.f_contiguous"]) -> None' in str(excinfo.value) + ) + with pytest.raises(TypeError) as excinfo: + m.fixed_mutator_a(np.array([[1, 2], [3, 4]], dtype="float32")) + assert ( + '(arg0: typing.Annotated[numpy.typing.NDArray[numpy.float32], "[5, 6]", "flags.writeable"]) -> None' + in str(excinfo.value) + ) + zr.flags.writeable = False + with pytest.raises(TypeError): + m.fixed_mutator_r(zr) + with pytest.raises(TypeError): + m.fixed_mutator_a(zr) + + +def test_cpp_casting(): + assert m.cpp_copy(m.fixed_r()) == 22.0 + assert m.cpp_copy(m.fixed_c()) == 22.0 + z = np.array([[5.0, 6], [7, 8]]) + assert m.cpp_copy(z) == 7.0 + assert m.cpp_copy(m.get_cm_ref()) == 21.0 + assert m.cpp_copy(m.get_rm_ref()) == 21.0 + assert m.cpp_ref_c(m.get_cm_ref()) == 21.0 + assert m.cpp_ref_r(m.get_rm_ref()) == 21.0 + with pytest.raises(RuntimeError) as excinfo: + # Can't reference m.fixed_c: it contains floats, m.cpp_ref_any wants doubles + m.cpp_ref_any(m.fixed_c()) + assert "Unable to cast Python instance" in str(excinfo.value) + with pytest.raises(RuntimeError) as excinfo: + # Can't reference m.fixed_r: it contains floats, m.cpp_ref_any wants doubles + m.cpp_ref_any(m.fixed_r()) + assert "Unable to cast Python instance" in str(excinfo.value) + assert m.cpp_ref_any(m.ReturnTester.create()) == 1.0 + + assert m.cpp_ref_any(m.get_cm_ref()) == 21.0 + assert m.cpp_ref_any(m.get_cm_ref()) == 21.0 + + +def test_pass_readonly_array(): + z = np.full((5, 6), 42.0) + z.flags.writeable = False + np.testing.assert_array_equal(z, m.fixed_copy_r(z)) + np.testing.assert_array_equal(m.fixed_r_const(), m.fixed_r()) + assert not m.fixed_r_const().flags.writeable + np.testing.assert_array_equal(m.fixed_copy_r(m.fixed_r_const()), m.fixed_r_const()) + + +def test_nonunit_stride_from_python(): + counting_mat = np.arange(9.0, dtype=np.float32).reshape((3, 3)) + second_row = counting_mat[1, :] + second_col = counting_mat[:, 1] + np.testing.assert_array_equal(m.double_row(second_row), 2.0 * second_row) + np.testing.assert_array_equal(m.double_col(second_row), 2.0 * second_row) + np.testing.assert_array_equal(m.double_complex(second_row), 2.0 * second_row) + np.testing.assert_array_equal(m.double_row(second_col), 2.0 * second_col) + np.testing.assert_array_equal(m.double_col(second_col), 2.0 * second_col) + np.testing.assert_array_equal(m.double_complex(second_col), 2.0 * second_col) + + counting_3d = np.arange(27.0, dtype=np.float32).reshape((3, 3, 3)) + slices = [counting_3d[0, :, :], counting_3d[:, 0, :], counting_3d[:, :, 0]] + for ref_mat in slices: + np.testing.assert_array_equal(m.double_mat_cm(ref_mat), 2.0 * ref_mat) + np.testing.assert_array_equal(m.double_mat_rm(ref_mat), 2.0 * ref_mat) + + # Mutator: + m.double_threer(second_row) + m.double_threec(second_col) + np.testing.assert_array_equal(counting_mat, [[0.0, 2, 2], [6, 16, 10], [6, 14, 8]]) + + +def test_negative_stride_from_python(msg): + """Eigen doesn't support (as of yet) negative strides. When a function takes an Eigen matrix by + copy or const reference, we can pass a numpy array that has negative strides. Otherwise, an + exception will be thrown as Eigen will not be able to map the numpy array.""" + + counting_mat = np.arange(9.0, dtype=np.float32).reshape((3, 3)) + counting_mat = counting_mat[::-1, ::-1] + second_row = counting_mat[1, :] + second_col = counting_mat[:, 1] + np.testing.assert_array_equal(m.double_row(second_row), 2.0 * second_row) + np.testing.assert_array_equal(m.double_col(second_row), 2.0 * second_row) + np.testing.assert_array_equal(m.double_complex(second_row), 2.0 * second_row) + np.testing.assert_array_equal(m.double_row(second_col), 2.0 * second_col) + np.testing.assert_array_equal(m.double_col(second_col), 2.0 * second_col) + np.testing.assert_array_equal(m.double_complex(second_col), 2.0 * second_col) + + counting_3d = np.arange(27.0, dtype=np.float32).reshape((3, 3, 3)) + counting_3d = counting_3d[::-1, ::-1, ::-1] + slices = [counting_3d[0, :, :], counting_3d[:, 0, :], counting_3d[:, :, 0]] + for ref_mat in slices: + np.testing.assert_array_equal(m.double_mat_cm(ref_mat), 2.0 * ref_mat) + np.testing.assert_array_equal(m.double_mat_rm(ref_mat), 2.0 * ref_mat) + + # Mutator: + with pytest.raises(TypeError) as excinfo: + m.double_threer(second_row) + assert ( + msg(excinfo.value) + == """ + double_threer(): incompatible function arguments. The following argument types are supported: + 1. (arg0: typing.Annotated[numpy.typing.NDArray[numpy.float32], "[1, 3]", "flags.writeable"]) -> None + + Invoked with: """ + + repr(np.array([5.0, 4.0, 3.0], dtype="float32")) + ) + + with pytest.raises(TypeError) as excinfo: + m.double_threec(second_col) + assert ( + msg(excinfo.value) + == """ + double_threec(): incompatible function arguments. The following argument types are supported: + 1. (arg0: typing.Annotated[numpy.typing.NDArray[numpy.float32], "[3, 1]", "flags.writeable"]) -> None + + Invoked with: """ + + repr(np.array([7.0, 4.0, 1.0], dtype="float32")) + ) + + +def test_block_runtime_error_type_caster_eigen_ref_made_a_copy(): + with pytest.raises(RuntimeError) as excinfo: + m.block(ref, 0, 0, 0, 0) + assert str(excinfo.value) == "type_caster for Eigen::Ref made a copy." + + +def test_nonunit_stride_to_python(): + assert np.all(m.diagonal(ref) == ref.diagonal()) + assert np.all(m.diagonal_1(ref) == ref.diagonal(1)) + for i in range(-5, 7): + assert np.all(m.diagonal_n(ref, i) == ref.diagonal(i)), f"m.diagonal_n({i})" + + # Must be order="F", otherwise the type_caster will make a copy and + # m.block() will return a dangling reference (heap-use-after-free). + rof = np.asarray(ref, order="F") + assert np.all(m.block(rof, 2, 1, 3, 3) == rof[2:5, 1:4]) + assert np.all(m.block(rof, 1, 4, 4, 2) == rof[1:, 4:]) + assert np.all(m.block(rof, 1, 4, 3, 2) == rof[1:4, 4:]) + + +def test_eigen_ref_to_python(): + chols = [m.cholesky1, m.cholesky2, m.cholesky3, m.cholesky4] + for i, chol in enumerate(chols, start=1): + mymat = chol(np.array([[1.0, 2, 4], [2, 13, 23], [4, 23, 77]])) + assert np.all(mymat == np.array([[1, 0, 0], [2, 3, 0], [4, 5, 6]])), ( + f"cholesky{i}" + ) + + +def assign_both(a1, a2, r, c, v): + a1[r, c] = v + a2[r, c] = v + + +def array_copy_but_one(a, r, c, v): + z = np.array(a, copy=True) + z[r, c] = v + return z + + +def test_eigen_return_references(): + """Tests various ways of returning references and non-referencing copies""" + + primary = np.ones((10, 10)) + a = m.ReturnTester() + a_get1 = a.get() + assert not a_get1.flags.owndata + assert a_get1.flags.writeable + assign_both(a_get1, primary, 3, 3, 5) + a_get2 = a.get_ptr() + assert not a_get2.flags.owndata + assert a_get2.flags.writeable + assign_both(a_get1, primary, 2, 3, 6) + + a_view1 = a.view() + assert not a_view1.flags.owndata + assert not a_view1.flags.writeable + with pytest.raises(ValueError): + a_view1[2, 3] = 4 + a_view2 = a.view_ptr() + assert not a_view2.flags.owndata + assert not a_view2.flags.writeable + with pytest.raises(ValueError): + a_view2[2, 3] = 4 + + a_copy1 = a.copy_get() + assert a_copy1.flags.owndata + assert a_copy1.flags.writeable + np.testing.assert_array_equal(a_copy1, primary) + a_copy1[7, 7] = -44 # Shouldn't affect anything else + c1want = array_copy_but_one(primary, 7, 7, -44) + a_copy2 = a.copy_view() + assert a_copy2.flags.owndata + assert a_copy2.flags.writeable + np.testing.assert_array_equal(a_copy2, primary) + a_copy2[4, 4] = -22 # Shouldn't affect anything else + c2want = array_copy_but_one(primary, 4, 4, -22) + + a_ref1 = a.ref() + assert not a_ref1.flags.owndata + assert a_ref1.flags.writeable + assign_both(a_ref1, primary, 1, 1, 15) + a_ref2 = a.ref_const() + assert not a_ref2.flags.owndata + assert not a_ref2.flags.writeable + with pytest.raises(ValueError): + a_ref2[5, 5] = 33 + a_ref3 = a.ref_safe() + assert not a_ref3.flags.owndata + assert a_ref3.flags.writeable + assign_both(a_ref3, primary, 0, 7, 99) + a_ref4 = a.ref_const_safe() + assert not a_ref4.flags.owndata + assert not a_ref4.flags.writeable + with pytest.raises(ValueError): + a_ref4[7, 0] = 987654321 + + a_copy3 = a.copy_ref() + assert a_copy3.flags.owndata + assert a_copy3.flags.writeable + np.testing.assert_array_equal(a_copy3, primary) + a_copy3[8, 1] = 11 + c3want = array_copy_but_one(primary, 8, 1, 11) + a_copy4 = a.copy_ref_const() + assert a_copy4.flags.owndata + assert a_copy4.flags.writeable + np.testing.assert_array_equal(a_copy4, primary) + a_copy4[8, 4] = 88 + c4want = array_copy_but_one(primary, 8, 4, 88) + + a_block1 = a.block(3, 3, 2, 2) + assert not a_block1.flags.owndata + assert a_block1.flags.writeable + a_block1[0, 0] = 55 + primary[3, 3] = 55 + a_block2 = a.block_safe(2, 2, 3, 2) + assert not a_block2.flags.owndata + assert a_block2.flags.writeable + a_block2[2, 1] = -123 + primary[4, 3] = -123 + a_block3 = a.block_const(6, 7, 4, 3) + assert not a_block3.flags.owndata + assert not a_block3.flags.writeable + with pytest.raises(ValueError): + a_block3[2, 2] = -44444 + + a_copy5 = a.copy_block(2, 2, 2, 3) + assert a_copy5.flags.owndata + assert a_copy5.flags.writeable + np.testing.assert_array_equal(a_copy5, primary[2:4, 2:5]) + a_copy5[1, 1] = 777 + c5want = array_copy_but_one(primary[2:4, 2:5], 1, 1, 777) + + a_corn1 = a.corners() + assert not a_corn1.flags.owndata + assert a_corn1.flags.writeable + a_corn1 *= 50 + a_corn1[1, 1] = 999 + primary[0, 0] = 50 + primary[0, 9] = 50 + primary[9, 0] = 50 + primary[9, 9] = 999 + a_corn2 = a.corners_const() + assert not a_corn2.flags.owndata + assert not a_corn2.flags.writeable + with pytest.raises(ValueError): + a_corn2[1, 0] = 51 + + # All of the changes made all the way along should be visible everywhere + # now (except for the copies, of course) + np.testing.assert_array_equal(a_get1, primary) + np.testing.assert_array_equal(a_get2, primary) + np.testing.assert_array_equal(a_view1, primary) + np.testing.assert_array_equal(a_view2, primary) + np.testing.assert_array_equal(a_ref1, primary) + np.testing.assert_array_equal(a_ref2, primary) + np.testing.assert_array_equal(a_ref3, primary) + np.testing.assert_array_equal(a_ref4, primary) + np.testing.assert_array_equal(a_block1, primary[3:5, 3:5]) + np.testing.assert_array_equal(a_block2, primary[2:5, 2:4]) + np.testing.assert_array_equal(a_block3, primary[6:10, 7:10]) + np.testing.assert_array_equal( + a_corn1, primary[0 :: primary.shape[0] - 1, 0 :: primary.shape[1] - 1] + ) + np.testing.assert_array_equal( + a_corn2, primary[0 :: primary.shape[0] - 1, 0 :: primary.shape[1] - 1] + ) + + np.testing.assert_array_equal(a_copy1, c1want) + np.testing.assert_array_equal(a_copy2, c2want) + np.testing.assert_array_equal(a_copy3, c3want) + np.testing.assert_array_equal(a_copy4, c4want) + np.testing.assert_array_equal(a_copy5, c5want) + + +@pytest.mark.skipif("env.GRAALPY", reason="Cannot reliably trigger GC") +def assert_keeps_alive(cl, method, *args): + cstats = ConstructorStats.get(cl) + start_with = cstats.alive() + a = cl() + assert cstats.alive() == start_with + 1 + z = method(a, *args) + assert cstats.alive() == start_with + 1 + del a + # Here's the keep alive in action: + assert cstats.alive() == start_with + 1 + del z + # Keep alive should have expired: + assert cstats.alive() == start_with + + +@pytest.mark.skipif("env.GRAALPY", reason="Cannot reliably trigger GC") +def test_eigen_keepalive(): + a = m.ReturnTester() + cstats = ConstructorStats.get(m.ReturnTester) + assert cstats.alive() == 1 + unsafe = [a.ref(), a.ref_const(), a.block(1, 2, 3, 4)] + copies = [ + a.copy_get(), + a.copy_view(), + a.copy_ref(), + a.copy_ref_const(), + a.copy_block(4, 3, 2, 1), + ] + del a + assert cstats.alive() == 0 + del unsafe + del copies + + for meth in [ + m.ReturnTester.get, + m.ReturnTester.get_ptr, + m.ReturnTester.view, + m.ReturnTester.view_ptr, + m.ReturnTester.ref_safe, + m.ReturnTester.ref_const_safe, + m.ReturnTester.corners, + m.ReturnTester.corners_const, + ]: + assert_keeps_alive(m.ReturnTester, meth) + + for meth in [m.ReturnTester.block_safe, m.ReturnTester.block_const]: + assert_keeps_alive(m.ReturnTester, meth, 4, 3, 2, 1) + + +def test_eigen_ref_mutators(): + """Tests Eigen's ability to mutate numpy values""" + + orig = np.array([[1.0, 2, 3], [4, 5, 6], [7, 8, 9]]) + zr = np.array(orig) + zc = np.array(orig, order="F") + m.add_rm(zr, 1, 0, 100) + assert np.all(zr == np.array([[1.0, 2, 3], [104, 5, 6], [7, 8, 9]])) + m.add_cm(zc, 1, 0, 200) + assert np.all(zc == np.array([[1.0, 2, 3], [204, 5, 6], [7, 8, 9]])) + + m.add_any(zr, 1, 0, 20) + assert np.all(zr == np.array([[1.0, 2, 3], [124, 5, 6], [7, 8, 9]])) + m.add_any(zc, 1, 0, 10) + assert np.all(zc == np.array([[1.0, 2, 3], [214, 5, 6], [7, 8, 9]])) + + # Can't reference a col-major array with a row-major Ref, and vice versa: + with pytest.raises(TypeError): + m.add_rm(zc, 1, 0, 1) + with pytest.raises(TypeError): + m.add_cm(zr, 1, 0, 1) + + # Overloads: + m.add1(zr, 1, 0, -100) + m.add2(zr, 1, 0, -20) + assert np.all(zr == orig) + m.add1(zc, 1, 0, -200) + m.add2(zc, 1, 0, -10) + assert np.all(zc == orig) + + # a non-contiguous slice (this won't work on either the row- or + # column-contiguous refs, but should work for the any) + cornersr = zr[0::2, 0::2] + cornersc = zc[0::2, 0::2] + + assert np.all(cornersr == np.array([[1.0, 3], [7, 9]])) + assert np.all(cornersc == np.array([[1.0, 3], [7, 9]])) + + with pytest.raises(TypeError): + m.add_rm(cornersr, 0, 1, 25) + with pytest.raises(TypeError): + m.add_cm(cornersr, 0, 1, 25) + with pytest.raises(TypeError): + m.add_rm(cornersc, 0, 1, 25) + with pytest.raises(TypeError): + m.add_cm(cornersc, 0, 1, 25) + m.add_any(cornersr, 0, 1, 25) + m.add_any(cornersc, 0, 1, 44) + assert np.all(zr == np.array([[1.0, 2, 28], [4, 5, 6], [7, 8, 9]])) + assert np.all(zc == np.array([[1.0, 2, 47], [4, 5, 6], [7, 8, 9]])) + + # You shouldn't be allowed to pass a non-writeable array to a mutating Eigen method: + zro = zr[0:4, 0:4] + zro.flags.writeable = False + with pytest.raises(TypeError): + m.add_rm(zro, 0, 0, 0) + with pytest.raises(TypeError): + m.add_any(zro, 0, 0, 0) + with pytest.raises(TypeError): + m.add1(zro, 0, 0, 0) + with pytest.raises(TypeError): + m.add2(zro, 0, 0, 0) + + # integer array shouldn't be passable to a double-matrix-accepting mutating func: + zi = np.array([[1, 2], [3, 4]]) + with pytest.raises(TypeError): + m.add_rm(zi) + + +def test_numpy_ref_mutators(): + """Tests numpy mutating Eigen matrices (for returned Eigen::Ref<...>s)""" + + m.reset_refs() # In case another test already changed it + + zc = m.get_cm_ref() + zcro = m.get_cm_const_ref() + zr = m.get_rm_ref() + zrro = m.get_rm_const_ref() + + assert [zc[1, 2], zcro[1, 2], zr[1, 2], zrro[1, 2]] == [23] * 4 + + assert not zc.flags.owndata + assert zc.flags.writeable + assert not zr.flags.owndata + assert zr.flags.writeable + assert not zcro.flags.owndata + assert not zcro.flags.writeable + assert not zrro.flags.owndata + assert not zrro.flags.writeable + + zc[1, 2] = 99 + expect = np.array([[11.0, 12, 13], [21, 22, 99], [31, 32, 33]]) + # We should have just changed zc, of course, but also zcro and the original eigen matrix + assert np.all(zc == expect) + assert np.all(zcro == expect) + assert np.all(m.get_cm_ref() == expect) + + zr[1, 2] = 99 + assert np.all(zr == expect) + assert np.all(zrro == expect) + assert np.all(m.get_rm_ref() == expect) + + # Make sure the readonly ones are numpy-readonly: + with pytest.raises(ValueError): + zcro[1, 2] = 6 + with pytest.raises(ValueError): + zrro[1, 2] = 6 + + # We should be able to explicitly copy like this (and since we're copying, + # the const should drop away) + y1 = np.array(m.get_cm_const_ref()) + + assert y1.flags.owndata + assert y1.flags.writeable + # We should get copies of the eigen data, which was modified above: + assert y1[1, 2] == 99 + y1[1, 2] += 12 + assert y1[1, 2] == 111 + assert zc[1, 2] == 99 # Make sure we aren't referencing the original + + +def test_both_ref_mutators(): + """Tests a complex chain of nested eigen/numpy references""" + + m.reset_refs() # In case another test already changed it + + z = m.get_cm_ref() # numpy -> eigen + z[0, 2] -= 3 + z2 = m.incr_matrix(z, 1) # numpy -> eigen -> numpy -> eigen + z2[1, 1] += 6 + z3 = m.incr_matrix(z, 2) # (numpy -> eigen)^3 + z3[2, 2] += -5 + z4 = m.incr_matrix(z, 3) # (numpy -> eigen)^4 + z4[1, 1] -= 1 + z5 = m.incr_matrix(z, 4) # (numpy -> eigen)^5 + z5[0, 0] = 0 + assert np.all(z == z2) + assert np.all(z == z3) + assert np.all(z == z4) + assert np.all(z == z5) + expect = np.array([[0.0, 22, 20], [31, 37, 33], [41, 42, 38]]) + assert np.all(z == expect) + + y = np.array(range(100), dtype="float64").reshape(10, 10) + y2 = m.incr_matrix_any(y, 10) # np -> eigen -> np + y3 = m.incr_matrix_any( + y2[0::2, 0::2], -33 + ) # np -> eigen -> np slice -> np -> eigen -> np + y4 = m.even_rows(y3) # numpy -> eigen slice -> (... y3) + y5 = m.even_cols(y4) # numpy -> eigen slice -> (... y4) + y6 = m.incr_matrix_any(y5, 1000) # numpy -> eigen -> (... y5) + + # Apply same mutations using just numpy: + yexpect = np.array(range(100), dtype="float64").reshape(10, 10) + yexpect += 10 + yexpect[0::2, 0::2] -= 33 + yexpect[0::4, 0::4] += 1000 + assert np.all(y6 == yexpect[0::4, 0::4]) + assert np.all(y5 == yexpect[0::4, 0::4]) + assert np.all(y4 == yexpect[0::4, 0::2]) + assert np.all(y3 == yexpect[0::2, 0::2]) + assert np.all(y2 == yexpect) + assert np.all(y == yexpect) + + +def test_nocopy_wrapper(): + # get_elem requires a column-contiguous matrix reference, but should be + # callable with other types of matrix (via copying): + int_matrix_colmajor = np.array( + [[1, 2, 3], [4, 5, 6], [7, 8, 9]], dtype="l", order="F" + ) + dbl_matrix_colmajor = np.array( + int_matrix_colmajor, dtype="double", order="F", copy=True + ) + int_matrix_rowmajor = np.array(int_matrix_colmajor, order="C", copy=True) + dbl_matrix_rowmajor = np.array( + int_matrix_rowmajor, dtype="double", order="C", copy=True + ) + + # All should be callable via get_elem: + assert m.get_elem(int_matrix_colmajor) == 8 + assert m.get_elem(dbl_matrix_colmajor) == 8 + assert m.get_elem(int_matrix_rowmajor) == 8 + assert m.get_elem(dbl_matrix_rowmajor) == 8 + + # All but the second should fail with m.get_elem_nocopy: + with pytest.raises(TypeError) as excinfo: + m.get_elem_nocopy(int_matrix_colmajor) + assert "get_elem_nocopy(): incompatible function arguments." in str(excinfo.value) + assert ', "flags.f_contiguous"' in str(excinfo.value) + assert m.get_elem_nocopy(dbl_matrix_colmajor) == 8 + with pytest.raises(TypeError) as excinfo: + m.get_elem_nocopy(int_matrix_rowmajor) + assert "get_elem_nocopy(): incompatible function arguments." in str(excinfo.value) + assert ', "flags.f_contiguous"' in str(excinfo.value) + with pytest.raises(TypeError) as excinfo: + m.get_elem_nocopy(dbl_matrix_rowmajor) + assert "get_elem_nocopy(): incompatible function arguments." in str(excinfo.value) + assert ', "flags.f_contiguous"' in str(excinfo.value) + + # For the row-major test, we take a long matrix in row-major, so only the third is allowed: + with pytest.raises(TypeError) as excinfo: + m.get_elem_rm_nocopy(int_matrix_colmajor) + assert "get_elem_rm_nocopy(): incompatible function arguments." in str( + excinfo.value + ) + assert ', "flags.c_contiguous"' in str(excinfo.value) + with pytest.raises(TypeError) as excinfo: + m.get_elem_rm_nocopy(dbl_matrix_colmajor) + assert "get_elem_rm_nocopy(): incompatible function arguments." in str( + excinfo.value + ) + assert ', "flags.c_contiguous"' in str(excinfo.value) + assert m.get_elem_rm_nocopy(int_matrix_rowmajor) == 8 + with pytest.raises(TypeError) as excinfo: + m.get_elem_rm_nocopy(dbl_matrix_rowmajor) + assert "get_elem_rm_nocopy(): incompatible function arguments." in str( + excinfo.value + ) + assert ', "flags.c_contiguous"' in str(excinfo.value) + + +def test_eigen_ref_life_support(): + """Ensure the lifetime of temporary arrays created by the `Ref` caster + + The `Ref` caster sometimes creates a copy which needs to stay alive. This needs to + happen both for directs casts (just the array) or indirectly (e.g. list of arrays). + """ + + a = np.full(shape=10, fill_value=8, dtype=np.int8) + assert m.get_elem_direct(a) == 8 + + list_of_a = [a] + assert m.get_elem_indirect(list_of_a) == 8 + + +def test_special_matrix_objects(): + assert np.all(m.incr_diag(7) == np.diag([1.0, 2, 3, 4, 5, 6, 7])) + + asymm = np.array([[1.0, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]]) + symm_lower = np.array(asymm) + symm_upper = np.array(asymm) + for i in range(4): + for j in range(i + 1, 4): + symm_lower[i, j] = symm_lower[j, i] + symm_upper[j, i] = symm_upper[i, j] + + assert np.all(m.symmetric_lower(asymm) == symm_lower) + assert np.all(m.symmetric_upper(asymm) == symm_upper) + + +def test_dense_signature(doc): + assert ( + doc(m.double_col) + == """ + double_col(arg0: typing.Annotated[numpy.typing.ArrayLike, numpy.float32, "[m, 1]"]) -> typing.Annotated[numpy.typing.NDArray[numpy.float32], "[m, 1]"] + """ + ) + assert ( + doc(m.double_row) + == """ + double_row(arg0: typing.Annotated[numpy.typing.ArrayLike, numpy.float32, "[1, n]"]) -> typing.Annotated[numpy.typing.NDArray[numpy.float32], "[1, n]"] + """ + ) + assert doc(m.double_complex) == ( + """ + double_complex(arg0: typing.Annotated[numpy.typing.ArrayLike, numpy.complex64, "[m, 1]"])""" + """ -> typing.Annotated[numpy.typing.NDArray[numpy.complex64], "[m, 1]"] + """ + ) + assert doc(m.double_mat_rm) == ( + """ + double_mat_rm(arg0: typing.Annotated[numpy.typing.ArrayLike, numpy.float32, "[m, n]"])""" + """ -> typing.Annotated[numpy.typing.NDArray[numpy.float32], "[m, n]"] + """ + ) + + +def test_defaults(doc): + assert "\n" not in str(doc(m.defaults_mat)) + assert "\n" not in str(doc(m.defaults_vec)) + + +def test_named_arguments(): + a = np.array([[1.0, 2], [3, 4], [5, 6]]) + b = np.ones((2, 1)) + + assert np.all(m.matrix_multiply(a, b) == np.array([[3.0], [7], [11]])) + assert np.all(m.matrix_multiply(A=a, B=b) == np.array([[3.0], [7], [11]])) + assert np.all(m.matrix_multiply(B=b, A=a) == np.array([[3.0], [7], [11]])) + + with pytest.raises(ValueError) as excinfo: + m.matrix_multiply(b, a) + assert str(excinfo.value) == "Nonconformable matrices!" + + with pytest.raises(ValueError) as excinfo: + m.matrix_multiply(A=b, B=a) + assert str(excinfo.value) == "Nonconformable matrices!" + + with pytest.raises(ValueError) as excinfo: + m.matrix_multiply(B=a, A=b) + assert str(excinfo.value) == "Nonconformable matrices!" + + +def test_sparse(): + pytest.importorskip("scipy") + assert_sparse_equal_ref(m.sparse_r()) + assert_sparse_equal_ref(m.sparse_c()) + assert_sparse_equal_ref(m.sparse_copy_r(m.sparse_r())) + assert_sparse_equal_ref(m.sparse_copy_c(m.sparse_c())) + assert_sparse_equal_ref(m.sparse_copy_r(m.sparse_c())) + assert_sparse_equal_ref(m.sparse_copy_c(m.sparse_r())) + + +def test_sparse_signature(doc): + pytest.importorskip("scipy") + assert ( + doc(m.sparse_copy_r) + == """ + sparse_copy_r(arg0: scipy.sparse.csr_matrix[numpy.float32]) -> scipy.sparse.csr_matrix[numpy.float32] + """ + ) + assert ( + doc(m.sparse_copy_c) + == """ + sparse_copy_c(arg0: scipy.sparse.csc_matrix[numpy.float32]) -> scipy.sparse.csc_matrix[numpy.float32] + """ + ) + + +def test_issue738(): + """Ignore strides on a length-1 dimension (even if they would be incompatible length > 1)""" + assert np.all(m.iss738_f1(np.array([[1.0, 2, 3]])) == np.array([[1.0, 102, 203]])) + assert np.all( + m.iss738_f1(np.array([[1.0], [2], [3]])) == np.array([[1.0], [12], [23]]) + ) + + assert np.all(m.iss738_f2(np.array([[1.0, 2, 3]])) == np.array([[1.0, 102, 203]])) + assert np.all( + m.iss738_f2(np.array([[1.0], [2], [3]])) == np.array([[1.0], [12], [23]]) + ) + + +@pytest.mark.parametrize("func", [m.iss738_f1, m.iss738_f2]) +@pytest.mark.parametrize("sizes", [(0, 2), (2, 0)]) +def test_zero_length(func, sizes): + """Ignore strides on a length-0 dimension (even if they would be incompatible length > 1)""" + assert np.all(func(np.zeros(sizes)) == np.zeros(sizes)) + + +def test_issue1105(): + """Issue 1105: 1xN or Nx1 input arrays weren't accepted for eigen + compile-time row vectors or column vector""" + assert m.iss1105_row(np.ones((1, 7))) + assert m.iss1105_col(np.ones((7, 1))) + + # These should still fail (incompatible dimensions): + with pytest.raises(TypeError) as excinfo: + m.iss1105_row(np.ones((7, 1))) + assert "incompatible function arguments" in str(excinfo.value) + with pytest.raises(TypeError) as excinfo: + m.iss1105_col(np.ones((1, 7))) + assert "incompatible function arguments" in str(excinfo.value) + + +def test_custom_operator_new(): + """Using Eigen types as member variables requires a class-specific + operator new with proper alignment""" + + o = m.CustomOperatorNew() + np.testing.assert_allclose(o.a, 0.0) + np.testing.assert_allclose(o.b.diagonal(), 1.0) + + +def test_arraylike_signature(doc): + assert doc(m.round_trip_vector) == ( + 'round_trip_vector(arg0: typing.Annotated[numpy.typing.ArrayLike, numpy.float32, "[m, 1]"])' + ' -> typing.Annotated[numpy.typing.NDArray[numpy.float32], "[m, 1]"]' + ) + assert doc(m.round_trip_dense) == ( + 'round_trip_dense(arg0: typing.Annotated[numpy.typing.ArrayLike, numpy.float32, "[m, n]"])' + ' -> typing.Annotated[numpy.typing.NDArray[numpy.float32], "[m, n]"]' + ) + assert doc(m.round_trip_dense_ref) == ( + 'round_trip_dense_ref(arg0: typing.Annotated[numpy.typing.NDArray[numpy.float32], "[m, n]", "flags.writeable", "flags.c_contiguous"])' + ' -> typing.Annotated[numpy.typing.NDArray[numpy.float32], "[m, n]", "flags.writeable", "flags.c_contiguous"]' + ) + m.round_trip_vector([1.0, 2.0]) + m.round_trip_dense([[1.0, 2.0], [3.0, 4.0]]) + with pytest.raises(TypeError, match="incompatible function arguments"): + m.round_trip_dense_ref([[1.0, 2.0], [3.0, 4.0]]) diff --git a/external_libraries/pybind11/tests/test_eigen_tensor.cpp b/external_libraries/pybind11/tests/test_eigen_tensor.cpp new file mode 100644 index 00000000..503c69c7 --- /dev/null +++ b/external_libraries/pybind11/tests/test_eigen_tensor.cpp @@ -0,0 +1,18 @@ +/* + tests/eigen_tensor.cpp -- automatic conversion of Eigen Tensor + + All rights reserved. Use of this source code is governed by a + BSD-style license that can be found in the LICENSE file. +*/ + +#define PYBIND11_TEST_EIGEN_TENSOR_NAMESPACE eigen_tensor + +#ifdef EIGEN_AVOID_STL_ARRAY +# undef EIGEN_AVOID_STL_ARRAY +#endif + +#include "test_eigen_tensor.inl" + +#include "pybind11_tests.h" + +test_initializer egien_tensor("eigen_tensor", eigen_tensor_test::test_module); diff --git a/external_libraries/pybind11/tests/test_eigen_tensor.inl b/external_libraries/pybind11/tests/test_eigen_tensor.inl new file mode 100644 index 00000000..3b3641e8 --- /dev/null +++ b/external_libraries/pybind11/tests/test_eigen_tensor.inl @@ -0,0 +1,338 @@ +/* + tests/eigen_tensor.cpp -- automatic conversion of Eigen Tensor + + All rights reserved. Use of this source code is governed by a + BSD-style license that can be found in the LICENSE file. +*/ + +#include + +PYBIND11_NAMESPACE_BEGIN(eigen_tensor_test) + +namespace py = pybind11; + +PYBIND11_WARNING_DISABLE_MSVC(4127) + +template +void reset_tensor(M &x) { + for (int i = 0; i < x.dimension(0); i++) { + for (int j = 0; j < x.dimension(1); j++) { + for (int k = 0; k < x.dimension(2); k++) { + x(i, j, k) = i * (5 * 2) + j * 2 + k; + } + } + } +} + +template +bool check_tensor(M &x) { + for (int i = 0; i < x.dimension(0); i++) { + for (int j = 0; j < x.dimension(1); j++) { + for (int k = 0; k < x.dimension(2); k++) { + if (x(i, j, k) != (i * (5 * 2) + j * 2 + k)) { + return false; + } + } + } + } + return true; +} + +template +Eigen::Tensor &get_tensor() { + static Eigen::Tensor *x; + + if (!x) { + x = new Eigen::Tensor(3, 5, 2); + reset_tensor(*x); + } + + return *x; +} + +template +Eigen::TensorMap> &get_tensor_map() { + static Eigen::TensorMap> *x; + + if (!x) { + x = new Eigen::TensorMap>(get_tensor()); + } + + return *x; +} + +template +Eigen::TensorFixedSize, Options> &get_fixed_tensor() { + static Eigen::TensorFixedSize, Options> *x; + + if (!x) { + Eigen::aligned_allocator, Options>> + allocator; + x = new (allocator.allocate(1)) + Eigen::TensorFixedSize, Options>(); + reset_tensor(*x); + } + + return *x; +} + +template +const Eigen::Tensor &get_const_tensor() { + return get_tensor(); +} + +template +struct CustomExample { + CustomExample() : member(get_tensor()), view_member(member) {} + + Eigen::Tensor member; + Eigen::TensorMap> view_member; +}; + +template +void init_tensor_module(pybind11::module &m) { + const char *needed_options = ""; + if (Options == Eigen::ColMajor) { + needed_options = "F"; + } else { + needed_options = "C"; + } + m.attr("needed_options") = needed_options; + + m.def("setup", []() { + reset_tensor(get_tensor()); + reset_tensor(get_fixed_tensor()); + }); + + m.def("is_ok", []() { + return check_tensor(get_tensor()) && check_tensor(get_fixed_tensor()); + }); + + py::class_>(m, "CustomExample", py::module_local()) + .def(py::init<>()) + .def_readonly( + "member", &CustomExample::member, py::return_value_policy::reference_internal) + .def_readonly("member_view", + &CustomExample::view_member, + py::return_value_policy::reference_internal); + + m.def( + "copy_fixed_tensor", + []() { return &get_fixed_tensor(); }, + py::return_value_policy::copy); + + m.def("copy_tensor", []() { return &get_tensor(); }, py::return_value_policy::copy); + + m.def( + "copy_const_tensor", + []() { return &get_const_tensor(); }, + py::return_value_policy::copy); + + m.def( + "move_fixed_tensor_copy", + []() -> Eigen::TensorFixedSize, Options> { + return get_fixed_tensor(); + }, + py::return_value_policy::move); + + m.def( + "move_tensor_copy", + []() -> Eigen::Tensor { return get_tensor(); }, + py::return_value_policy::move); + + m.def( + "move_const_tensor", + []() -> const Eigen::Tensor & { return get_const_tensor(); }, + py::return_value_policy::move); + + m.def( + "take_fixed_tensor", + []() { + Eigen::aligned_allocator< + Eigen::TensorFixedSize, Options>> + allocator; + static auto *obj = new (allocator.allocate(1)) + Eigen::TensorFixedSize, Options>( + get_fixed_tensor()); + return obj; // take_ownership will fail. + }, + py::return_value_policy::take_ownership); + + m.def( + "take_tensor", + []() { + static auto *obj = new Eigen::Tensor(get_tensor()); + return obj; // take_ownership will fail. + }, + py::return_value_policy::take_ownership); + + m.def( + "take_const_tensor", + []() -> const Eigen::Tensor * { + static auto *obj = new Eigen::Tensor(get_tensor()); + return obj; // take_ownership will fail. + }, + py::return_value_policy::take_ownership); + + m.def( + "take_view_tensor", + []() -> const Eigen::TensorMap> * { + static auto *obj + = new Eigen::TensorMap>(get_tensor()); + return obj; // take_ownership will fail. + }, + py::return_value_policy::take_ownership); + + m.def( + "reference_tensor", + []() { return &get_tensor(); }, + py::return_value_policy::reference); + + m.def( + "reference_tensor_v2", + []() -> Eigen::Tensor & { return get_tensor(); }, + py::return_value_policy::reference); + + m.def( + "reference_tensor_internal", + []() { return &get_tensor(); }, + py::return_value_policy::reference_internal); + + m.def( + "reference_fixed_tensor", + []() { return &get_tensor(); }, + py::return_value_policy::reference); + + m.def( + "reference_const_tensor", + []() { return &get_const_tensor(); }, + py::return_value_policy::reference); + + m.def( + "reference_const_tensor_v2", + []() -> const Eigen::Tensor & { return get_const_tensor(); }, + py::return_value_policy::reference); + + m.def( + "reference_view_of_tensor", + []() -> Eigen::TensorMap> { + return get_tensor_map(); + }, + py::return_value_policy::reference); + + m.def( + "reference_view_of_tensor_v2", + // NOLINTNEXTLINE(readability-const-return-type) + []() -> const Eigen::TensorMap> { + return get_tensor_map(); // NOLINT(readability-const-return-type) + }, // NOLINT(readability-const-return-type) + py::return_value_policy::reference); + + m.def( + "reference_view_of_tensor_v3", + []() -> Eigen::TensorMap> * { + return &get_tensor_map(); + }, + py::return_value_policy::reference); + + m.def( + "reference_view_of_tensor_v4", + []() -> const Eigen::TensorMap> * { + return &get_tensor_map(); + }, + py::return_value_policy::reference); + + m.def( + "reference_view_of_tensor_v5", + []() -> Eigen::TensorMap> & { + return get_tensor_map(); + }, + py::return_value_policy::reference); + + m.def( + "reference_view_of_tensor_v6", + []() -> const Eigen::TensorMap> & { + return get_tensor_map(); + }, + py::return_value_policy::reference); + + m.def( + "reference_view_of_fixed_tensor", + []() { + return Eigen::TensorMap< + Eigen::TensorFixedSize, Options>>( + get_fixed_tensor()); + }, + py::return_value_policy::reference); + + m.def("round_trip_tensor", + [](const Eigen::Tensor &tensor) { return tensor; }); + + m.def( + "round_trip_tensor_noconvert", + [](const Eigen::Tensor &tensor) { return tensor; }, + py::arg("tensor").noconvert()); + + m.def("round_trip_tensor2", + [](const Eigen::Tensor &tensor) { return tensor; }); + + m.def("round_trip_fixed_tensor", + [](const Eigen::TensorFixedSize, Options> &tensor) { + return tensor; + }); + + m.def( + "round_trip_view_tensor", + [](Eigen::TensorMap> view) { return view; }, + py::return_value_policy::reference); + + m.def( + "round_trip_view_tensor_ref", + [](Eigen::TensorMap> &view) { return view; }, + py::return_value_policy::reference); + + m.def( + "round_trip_view_tensor_ptr", + [](Eigen::TensorMap> *view) { return view; }, + py::return_value_policy::reference); + + m.def( + "round_trip_aligned_view_tensor", + [](Eigen::TensorMap, Eigen::Aligned> view) { + return view; + }, + py::return_value_policy::reference); + + m.def( + "round_trip_const_view_tensor", + [](Eigen::TensorMap> view) { + return Eigen::Tensor(view); + }, + py::return_value_policy::move); + + m.def( + "round_trip_rank_0", + [](const Eigen::Tensor &tensor) { return tensor; }, + py::return_value_policy::move); + + m.def( + "round_trip_rank_0_noconvert", + [](const Eigen::Tensor &tensor) { return tensor; }, + py::arg("tensor").noconvert(), + py::return_value_policy::move); + + m.def( + "round_trip_rank_0_view", + [](Eigen::TensorMap> &tensor) { return tensor; }, + py::return_value_policy::reference); +} + +void test_module(py::module_ &m) { + auto f_style = m.def_submodule("f_style"); + auto c_style = m.def_submodule("c_style"); + + init_tensor_module(f_style); + init_tensor_module(c_style); +} + +PYBIND11_NAMESPACE_END(eigen_tensor_test) diff --git a/external_libraries/pybind11/tests/test_eigen_tensor.py b/external_libraries/pybind11/tests/test_eigen_tensor.py new file mode 100644 index 00000000..4b018551 --- /dev/null +++ b/external_libraries/pybind11/tests/test_eigen_tensor.py @@ -0,0 +1,316 @@ +from __future__ import annotations + +import sys + +import pytest + +import env # noqa: F401 + +np = pytest.importorskip("numpy") +eigen_tensor = pytest.importorskip("pybind11_tests.eigen_tensor") +submodules = [eigen_tensor.c_style, eigen_tensor.f_style] +try: + import eigen_tensor_avoid_stl_array as avoid + + submodules += [avoid.c_style, avoid.f_style] +except ImportError as e: + # Ensure config, build, toolchain, etc. issues are not masked here: + msg = ( + "import eigen_tensor_avoid_stl_array FAILED, while " + "import pybind11_tests.eigen_tensor succeeded. " + "Please ensure that " + "test_eigen_tensor.cpp & " + "eigen_tensor_avoid_stl_array.cpp " + "are built together (or both are not built if Eigen is not available)." + ) + raise RuntimeError(msg) from e + +tensor_ref = np.empty((3, 5, 2), dtype=np.int64) + +for i in range(tensor_ref.shape[0]): + for j in range(tensor_ref.shape[1]): + for k in range(tensor_ref.shape[2]): + tensor_ref[i, j, k] = i * (5 * 2) + j * 2 + k + +indices = (2, 3, 1) + + +@pytest.fixture(autouse=True) +def cleanup(): + for module in submodules: + module.setup() + + yield + + for module in submodules: + assert module.is_ok() + + +def test_import_avoid_stl_array(): + pytest.importorskip("eigen_tensor_avoid_stl_array") + assert len(submodules) == 4 + + +def assert_equal_tensor_ref(mat, writeable=True, modified=None): + assert mat.flags.writeable == writeable + + copy = np.array(tensor_ref) + if modified is not None: + copy[indices] = modified + + np.testing.assert_array_equal(mat, copy) + + +@pytest.mark.parametrize("m", submodules) +@pytest.mark.parametrize("member_name", ["member", "member_view"]) +@pytest.mark.skipif("env.GRAALPY", reason="Different refcounting mechanism") +def test_reference_internal(m, member_name): + if not hasattr(sys, "getrefcount"): + pytest.skip("No reference counting") + foo = m.CustomExample() + counts = sys.getrefcount(foo) + mem = getattr(foo, member_name) + assert_equal_tensor_ref(mem, writeable=False) + new_counts = sys.getrefcount(foo) + assert new_counts == counts + 1 + assert_equal_tensor_ref(mem, writeable=False) + del mem + assert sys.getrefcount(foo) == counts + + +assert_equal_funcs = [ + "copy_tensor", + "copy_fixed_tensor", + "copy_const_tensor", + "move_tensor_copy", + "move_fixed_tensor_copy", + "take_tensor", + "take_fixed_tensor", + "reference_tensor", + "reference_tensor_v2", + "reference_fixed_tensor", + "reference_view_of_tensor", + "reference_view_of_tensor_v3", + "reference_view_of_tensor_v5", + "reference_view_of_fixed_tensor", +] + +assert_equal_const_funcs = [ + "reference_view_of_tensor_v2", + "reference_view_of_tensor_v4", + "reference_view_of_tensor_v6", + "reference_const_tensor", + "reference_const_tensor_v2", +] + + +@pytest.mark.parametrize("m", submodules) +@pytest.mark.parametrize("func_name", assert_equal_funcs + assert_equal_const_funcs) +def test_convert_tensor_to_py(m, func_name): + writeable = func_name in assert_equal_funcs + assert_equal_tensor_ref(getattr(m, func_name)(), writeable=writeable) + + +@pytest.mark.parametrize("m", submodules) +def test_bad_cpp_to_python_casts(m): + with pytest.raises( + RuntimeError, match="Cannot use reference internal when there is no parent" + ): + m.reference_tensor_internal() + + with pytest.raises(RuntimeError, match="Cannot move from a constant reference"): + m.move_const_tensor() + + with pytest.raises( + RuntimeError, match="Cannot take ownership of a const reference" + ): + m.take_const_tensor() + + with pytest.raises( + RuntimeError, + match="Invalid return_value_policy for Eigen Map type, must be either reference or reference_internal", + ): + m.take_view_tensor() + + +@pytest.mark.parametrize("m", submodules) +def test_bad_python_to_cpp_casts(m): + with pytest.raises( + TypeError, match=r"^round_trip_tensor\(\): incompatible function arguments" + ): + m.round_trip_tensor(np.zeros((2, 3))) + + with pytest.raises(TypeError, match=r"^Cannot cast array data from dtype"): + m.round_trip_tensor(np.zeros(dtype=np.str_, shape=(2, 3, 1))) + + with pytest.raises( + TypeError, + match=r"^round_trip_tensor_noconvert\(\): incompatible function arguments", + ): + m.round_trip_tensor_noconvert(tensor_ref) + + assert_equal_tensor_ref( + m.round_trip_tensor_noconvert(tensor_ref.astype(np.float64)) + ) + + bad_options = "C" if m.needed_options == "F" else "F" + # Shape, dtype and the order need to be correct for a TensorMap cast + with pytest.raises( + TypeError, match=r"^round_trip_view_tensor\(\): incompatible function arguments" + ): + m.round_trip_view_tensor( + np.zeros((3, 5, 2), dtype=np.float64, order=bad_options) + ) + + with pytest.raises( + TypeError, match=r"^round_trip_view_tensor\(\): incompatible function arguments" + ): + m.round_trip_view_tensor( + np.zeros((3, 5, 2), dtype=np.float32, order=m.needed_options) + ) + + with pytest.raises( + TypeError, match=r"^round_trip_view_tensor\(\): incompatible function arguments" + ): + m.round_trip_view_tensor( + np.zeros((3, 5), dtype=np.float64, order=m.needed_options) + ) + + temp = np.zeros((3, 5, 2), dtype=np.float64, order=m.needed_options) + with pytest.raises( + TypeError, match=r"^round_trip_view_tensor\(\): incompatible function arguments" + ): + m.round_trip_view_tensor( + temp[:, ::-1, :], + ) + + temp = np.zeros((3, 5, 2), dtype=np.float64, order=m.needed_options) + temp.setflags(write=False) + with pytest.raises( + TypeError, match=r"^round_trip_view_tensor\(\): incompatible function arguments" + ): + m.round_trip_view_tensor(temp) + + +@pytest.mark.parametrize("m", submodules) +def test_references_actually_refer(m): + a = m.reference_tensor() + temp = a[indices] + a[indices] = 100 + assert_equal_tensor_ref(m.copy_const_tensor(), modified=100) + a[indices] = temp + assert_equal_tensor_ref(m.copy_const_tensor()) + + a = m.reference_view_of_tensor() + a[indices] = 100 + assert_equal_tensor_ref(m.copy_const_tensor(), modified=100) + a[indices] = temp + assert_equal_tensor_ref(m.copy_const_tensor()) + + +@pytest.mark.parametrize("m", submodules) +def test_round_trip(m): + assert_equal_tensor_ref(m.round_trip_tensor(tensor_ref)) + + with pytest.raises(TypeError, match="^Cannot cast array data from"): + assert_equal_tensor_ref(m.round_trip_tensor2(tensor_ref)) + + assert_equal_tensor_ref(m.round_trip_tensor2(np.array(tensor_ref, dtype=np.int32))) + assert_equal_tensor_ref(m.round_trip_fixed_tensor(tensor_ref)) + assert_equal_tensor_ref(m.round_trip_aligned_view_tensor(m.reference_tensor())) + + copy = np.array(tensor_ref, dtype=np.float64, order=m.needed_options) + assert_equal_tensor_ref(m.round_trip_view_tensor(copy)) + assert_equal_tensor_ref(m.round_trip_view_tensor_ref(copy)) + assert_equal_tensor_ref(m.round_trip_view_tensor_ptr(copy)) + copy.setflags(write=False) + assert_equal_tensor_ref(m.round_trip_const_view_tensor(copy)) + + np.testing.assert_array_equal( + tensor_ref[:, ::-1, :], m.round_trip_tensor(tensor_ref[:, ::-1, :]) + ) + + assert m.round_trip_rank_0(np.float64(3.5)) == 3.5 + assert m.round_trip_rank_0(3.5) == 3.5 + + with pytest.raises( + TypeError, + match=r"^round_trip_rank_0_noconvert\(\): incompatible function arguments", + ): + m.round_trip_rank_0_noconvert(np.float64(3.5)) + + with pytest.raises( + TypeError, + match=r"^round_trip_rank_0_noconvert\(\): incompatible function arguments", + ): + m.round_trip_rank_0_noconvert(3.5) + + with pytest.raises( + TypeError, match=r"^round_trip_rank_0_view\(\): incompatible function arguments" + ): + m.round_trip_rank_0_view(np.float64(3.5)) + + with pytest.raises( + TypeError, match=r"^round_trip_rank_0_view\(\): incompatible function arguments" + ): + m.round_trip_rank_0_view(3.5) + + +@pytest.mark.parametrize("m", submodules) +def test_round_trip_references_actually_refer(m): + # Need to create a copy that matches the type on the C side + copy = np.array(tensor_ref, dtype=np.float64, order=m.needed_options) + a = m.round_trip_view_tensor(copy) + temp = a[indices] + a[indices] = 100 + assert_equal_tensor_ref(copy, modified=100) + a[indices] = temp + assert_equal_tensor_ref(copy) + + +@pytest.mark.parametrize("m", submodules) +def test_doc_string(m, doc): + assert ( + doc(m.copy_tensor) + == 'copy_tensor() -> typing.Annotated[numpy.typing.NDArray[numpy.float64], "[?, ?, ?]"]' + ) + assert ( + doc(m.copy_fixed_tensor) + == 'copy_fixed_tensor() -> typing.Annotated[numpy.typing.NDArray[numpy.float64], "[3, 5, 2]"]' + ) + assert ( + doc(m.reference_const_tensor) + == 'reference_const_tensor() -> typing.Annotated[numpy.typing.NDArray[numpy.float64], "[?, ?, ?]"]' + ) + + order_flag = f'"flags.{m.needed_options.lower()}_contiguous"' + assert doc(m.round_trip_view_tensor) == ( + f'round_trip_view_tensor(arg0: typing.Annotated[numpy.typing.NDArray[numpy.float64], "[?, ?, ?]", "flags.writeable", {order_flag}])' + f' -> typing.Annotated[numpy.typing.NDArray[numpy.float64], "[?, ?, ?]", "flags.writeable", {order_flag}]' + ) + assert doc(m.round_trip_const_view_tensor) == ( + f'round_trip_const_view_tensor(arg0: typing.Annotated[numpy.typing.NDArray[numpy.float64], "[?, ?, ?]", {order_flag}])' + ' -> typing.Annotated[numpy.typing.NDArray[numpy.float64], "[?, ?, ?]"]' + ) + + +@pytest.mark.parametrize("m", submodules) +def test_arraylike_signature(m, doc): + order_flag = f'"flags.{m.needed_options.lower()}_contiguous"' + assert doc(m.round_trip_tensor) == ( + 'round_trip_tensor(arg0: typing.Annotated[numpy.typing.ArrayLike, numpy.float64, "[?, ?, ?]"])' + ' -> typing.Annotated[numpy.typing.NDArray[numpy.float64], "[?, ?, ?]"]' + ) + assert doc(m.round_trip_tensor_noconvert) == ( + 'round_trip_tensor_noconvert(tensor: typing.Annotated[numpy.typing.NDArray[numpy.float64], "[?, ?, ?]"])' + ' -> typing.Annotated[numpy.typing.NDArray[numpy.float64], "[?, ?, ?]"]' + ) + assert doc(m.round_trip_view_tensor) == ( + f'round_trip_view_tensor(arg0: typing.Annotated[numpy.typing.NDArray[numpy.float64], "[?, ?, ?]", "flags.writeable", {order_flag}])' + f' -> typing.Annotated[numpy.typing.NDArray[numpy.float64], "[?, ?, ?]", "flags.writeable", {order_flag}]' + ) + m.round_trip_tensor(tensor_ref.tolist()) + with pytest.raises(TypeError, match="incompatible function arguments"): + m.round_trip_tensor_noconvert(tensor_ref.tolist()) + with pytest.raises(TypeError, match="incompatible function arguments"): + m.round_trip_view_tensor(tensor_ref.tolist()) diff --git a/external_libraries/pybind11/tests/test_enum.cpp b/external_libraries/pybind11/tests/test_enum.cpp new file mode 100644 index 00000000..4ec0af72 --- /dev/null +++ b/external_libraries/pybind11/tests/test_enum.cpp @@ -0,0 +1,149 @@ +/* + tests/test_enums.cpp -- enumerations + + Copyright (c) 2016 Wenzel Jakob + + All rights reserved. Use of this source code is governed by a + BSD-style license that can be found in the LICENSE file. +*/ + +#include "pybind11_tests.h" + +TEST_SUBMODULE(enums, m) { + // test_unscoped_enum + enum UnscopedEnum { EOne = 1, ETwo, EThree }; + py::enum_(m, "UnscopedEnum", py::arithmetic(), "An unscoped enumeration") + .value("EOne", EOne, "Docstring for EOne") + .value("ETwo", ETwo, "Docstring for ETwo") + .value("EThree", EThree, "Docstring for EThree") + .export_values(); + + // test_scoped_enum + enum class ScopedEnum { Two = 2, Three }; + py::enum_(m, "ScopedEnum", py::arithmetic()) + .value("Two", ScopedEnum::Two) + .value("Three", ScopedEnum::Three); + + m.def("test_scoped_enum", [](ScopedEnum z) { + return "ScopedEnum::" + std::string(z == ScopedEnum::Two ? "Two" : "Three"); + }); + + // test_binary_operators + enum Flags { Read = 4, Write = 2, Execute = 1 }; + py::enum_(m, "Flags", py::arithmetic()) + .value("Read", Flags::Read) + .value("Write", Flags::Write) + .value("Execute", Flags::Execute) + .export_values(); + + // test_implicit_conversion + class ClassWithUnscopedEnum { + public: + enum EMode { EFirstMode = 1, ESecondMode }; + + static EMode test_function(EMode mode) { return mode; } + }; + py::class_ exenum_class(m, "ClassWithUnscopedEnum"); + exenum_class.def_static("test_function", &ClassWithUnscopedEnum::test_function); + py::enum_(exenum_class, "EMode") + .value("EFirstMode", ClassWithUnscopedEnum::EFirstMode) + .value("ESecondMode", ClassWithUnscopedEnum::ESecondMode) + .export_values(); + + // test_enum_to_int + m.def("test_enum_to_int", [](int) {}); + m.def("test_enum_to_uint", [](uint32_t) {}); + m.def("test_enum_to_long_long", [](long long) {}); + + // test_duplicate_enum_name + enum SimpleEnum { ONE, TWO, THREE }; + + m.def("register_bad_enum", [m]() { + py::enum_(m, "SimpleEnum") + .value("ONE", SimpleEnum::ONE) // NOTE: all value function calls are called with the + // same first parameter value + .value("ONE", SimpleEnum::TWO) + .value("ONE", SimpleEnum::THREE) + .export_values(); + }); + + // test_enum_scalar + enum UnscopedUCharEnum : unsigned char {}; + enum class ScopedShortEnum : short {}; + enum class ScopedLongEnum : long {}; + enum UnscopedUInt64Enum : std::uint64_t {}; + static_assert( + py::detail::all_of< + std::is_same::Scalar, unsigned char>, + std::is_same::Scalar, short>, + std::is_same::Scalar, long>, + std::is_same::Scalar, std::uint64_t>>::value, + "Error during the deduction of enum's scalar type with normal integer underlying"); + + // test_enum_scalar_with_char_underlying + enum class ScopedCharEnum : char { Zero, Positive }; + enum class ScopedWCharEnum : wchar_t { Zero, Positive }; + enum class ScopedChar32Enum : char32_t { Zero, Positive }; + enum class ScopedChar16Enum : char16_t { Zero, Positive }; + + // test the scalar of char type enums according to chapter 'Character types' + // from https://en.cppreference.com/w/cpp/language/types + static_assert( + py::detail::any_of< + std::is_same::Scalar, signed char>, // e.g. gcc on x86 + std::is_same::Scalar, unsigned char> // e.g. arm linux + >::value, + "char should be cast to either signed char or unsigned char"); + static_assert(sizeof(py::enum_::Scalar) == 2 + || sizeof(py::enum_::Scalar) == 4, + "wchar_t should be either 16 bits (Windows) or 32 (everywhere else)"); + static_assert( + py::detail::all_of< + std::is_same::Scalar, std::uint_least32_t>, + std::is_same::Scalar, std::uint_least16_t>>::value, + "char32_t, char16_t (and char8_t)'s size, signedness, and alignment is determined"); +#if defined(PYBIND11_HAS_U8STRING) + enum class ScopedChar8Enum : char8_t { Zero, Positive }; + static_assert(std::is_same::Scalar, unsigned char>::value); +#endif + + // test_char_underlying_enum + py::enum_(m, "ScopedCharEnum") + .value("Zero", ScopedCharEnum::Zero) + .value("Positive", ScopedCharEnum::Positive); + py::enum_(m, "ScopedWCharEnum") + .value("Zero", ScopedWCharEnum::Zero) + .value("Positive", ScopedWCharEnum::Positive); + py::enum_(m, "ScopedChar32Enum") + .value("Zero", ScopedChar32Enum::Zero) + .value("Positive", ScopedChar32Enum::Positive); + py::enum_(m, "ScopedChar16Enum") + .value("Zero", ScopedChar16Enum::Zero) + .value("Positive", ScopedChar16Enum::Positive); + + // test_bool_underlying_enum + enum class ScopedBoolEnum : bool { FALSE, TRUE }; + + // bool is unsigned (std::is_signed returns false) and 1-byte long, so represented with u8 + static_assert(std::is_same::Scalar, std::uint8_t>::value, ""); + + py::enum_(m, "ScopedBoolEnum") + .value("FALSE", ScopedBoolEnum::FALSE) + .value("TRUE", ScopedBoolEnum::TRUE); + +#if defined(__MINGW32__) + m.attr("obj_cast_UnscopedEnum_ptr") = "MinGW: dangling pointer to an unnamed temporary may be " + "used [-Werror=dangling-pointer=]"; +#else + m.def("obj_cast_UnscopedEnum_ptr", [](const py::object &obj) { + // https://github.com/OpenImageIO/oiio/blob/30ea4ebdfab11aec291befbaff446f2a7d24835b/src/python/py_oiio.h#L300 + if (py::isinstance(obj)) { + if (*obj.cast() == UnscopedEnum::ETwo) { + return 2; + } + return 1; + } + return 0; + }); +#endif +} diff --git a/external_libraries/pybind11/tests/test_enum.py b/external_libraries/pybind11/tests/test_enum.py new file mode 100644 index 00000000..f295b014 --- /dev/null +++ b/external_libraries/pybind11/tests/test_enum.py @@ -0,0 +1,344 @@ +# ruff: noqa: SIM201 SIM300 SIM202 +from __future__ import annotations + +import re + +import pytest + +import env +from pybind11_tests import enums as m + + +@pytest.mark.xfail( + env.GRAALPY and env.GRAALPY_VERSION < (24, 2), reason="Fixed in GraalPy 24.2" +) +def test_unscoped_enum(): + assert str(m.UnscopedEnum.EOne) == "UnscopedEnum.EOne" + assert str(m.UnscopedEnum.ETwo) == "UnscopedEnum.ETwo" + assert str(m.EOne) == "UnscopedEnum.EOne" + assert repr(m.UnscopedEnum.EOne) == "" + assert repr(m.UnscopedEnum.ETwo) == "" + assert repr(m.EOne) == "" + + # name property + assert m.UnscopedEnum.EOne.name == "EOne" + assert m.UnscopedEnum.EOne.value == 1 + assert m.UnscopedEnum.ETwo.name == "ETwo" + assert m.UnscopedEnum.ETwo.value == 2 + assert m.EOne is m.UnscopedEnum.EOne + # name, value readonly + with pytest.raises(AttributeError): + m.UnscopedEnum.EOne.name = "" + with pytest.raises(AttributeError): + m.UnscopedEnum.EOne.value = 10 + # name, value returns a copy + # TODO: Neither the name nor value tests actually check against aliasing. + # Use a mutable type that has reference semantics. + nonaliased_name = m.UnscopedEnum.EOne.name + nonaliased_name = "bar" # noqa: F841 + assert m.UnscopedEnum.EOne.name == "EOne" + nonaliased_value = m.UnscopedEnum.EOne.value + nonaliased_value = 10 # noqa: F841 + assert m.UnscopedEnum.EOne.value == 1 + + # __members__ property + assert m.UnscopedEnum.__members__ == { + "EOne": m.UnscopedEnum.EOne, + "ETwo": m.UnscopedEnum.ETwo, + "EThree": m.UnscopedEnum.EThree, + } + # __members__ readonly + with pytest.raises(AttributeError): + m.UnscopedEnum.__members__ = {} + # __members__ returns a copy + nonaliased_members = m.UnscopedEnum.__members__ + nonaliased_members["bar"] = "baz" + assert m.UnscopedEnum.__members__ == { + "EOne": m.UnscopedEnum.EOne, + "ETwo": m.UnscopedEnum.ETwo, + "EThree": m.UnscopedEnum.EThree, + } + + for docstring_line in [ + "An unscoped enumeration", + "Members:", + " EOne : Docstring for EOne", + " ETwo : Docstring for ETwo", + " EThree : Docstring for EThree", + ]: + assert docstring_line in m.UnscopedEnum.__doc__ + + # Unscoped enums will accept ==/!= int comparisons + y = m.UnscopedEnum.ETwo + assert y == 2 + assert 2 == y + assert y != 3 + assert 3 != y + # Compare with None + assert y != None # noqa: E711 + assert not (y == None) # noqa: E711 + # Compare with an object + assert y != object() + assert not (y == object()) + # Compare with string + assert y != "2" + assert "2" != y + assert not ("2" == y) + assert not (y == "2") + + with pytest.raises(TypeError): + y < object() # noqa: B015 + + with pytest.raises(TypeError): + y <= object() # noqa: B015 + + with pytest.raises(TypeError): + y > object() # noqa: B015 + + with pytest.raises(TypeError): + y >= object() # noqa: B015 + + with pytest.raises(TypeError): + y | object() + + with pytest.raises(TypeError): + y & object() + + with pytest.raises(TypeError): + y ^ object() + + assert int(m.UnscopedEnum.ETwo) == 2 + assert str(m.UnscopedEnum(2)) == "UnscopedEnum.ETwo" + + # order + assert m.UnscopedEnum.EOne < m.UnscopedEnum.ETwo + assert m.UnscopedEnum.EOne < 2 + assert m.UnscopedEnum.ETwo > m.UnscopedEnum.EOne + assert m.UnscopedEnum.ETwo > 1 + assert m.UnscopedEnum.ETwo <= 2 + assert m.UnscopedEnum.ETwo >= 2 + assert m.UnscopedEnum.EOne <= m.UnscopedEnum.ETwo + assert m.UnscopedEnum.EOne <= 2 + assert m.UnscopedEnum.ETwo >= m.UnscopedEnum.EOne + assert m.UnscopedEnum.ETwo >= 1 + assert not (m.UnscopedEnum.ETwo < m.UnscopedEnum.EOne) + assert not (2 < m.UnscopedEnum.EOne) + + # arithmetic + assert m.UnscopedEnum.EOne & m.UnscopedEnum.EThree == m.UnscopedEnum.EOne + assert m.UnscopedEnum.EOne | m.UnscopedEnum.ETwo == m.UnscopedEnum.EThree + assert m.UnscopedEnum.EOne ^ m.UnscopedEnum.EThree == m.UnscopedEnum.ETwo + + +def test_scoped_enum(): + assert m.test_scoped_enum(m.ScopedEnum.Three) == "ScopedEnum::Three" + z = m.ScopedEnum.Two + assert m.test_scoped_enum(z) == "ScopedEnum::Two" + + # Scoped enums will *NOT* accept ==/!= int comparisons (Will always return False) + assert not z == 3 + assert not 3 == z + assert z != 3 + assert 3 != z + # Compare with None + assert z != None # noqa: E711 + assert not (z == None) # noqa: E711 + # Compare with an object + assert z != object() + assert not (z == object()) + # Scoped enums will *NOT* accept >, <, >= and <= int comparisons (Will throw exceptions) + with pytest.raises(TypeError): + z > 3 # noqa: B015 + with pytest.raises(TypeError): + z < 3 # noqa: B015 + with pytest.raises(TypeError): + z >= 3 # noqa: B015 + with pytest.raises(TypeError): + z <= 3 # noqa: B015 + + # order + assert m.ScopedEnum.Two < m.ScopedEnum.Three + assert m.ScopedEnum.Three > m.ScopedEnum.Two + assert m.ScopedEnum.Two <= m.ScopedEnum.Three + assert m.ScopedEnum.Two <= m.ScopedEnum.Two + assert m.ScopedEnum.Two >= m.ScopedEnum.Two + assert m.ScopedEnum.Three >= m.ScopedEnum.Two + + +def test_implicit_conversion(): + assert str(m.ClassWithUnscopedEnum.EMode.EFirstMode) == "EMode.EFirstMode" + assert str(m.ClassWithUnscopedEnum.EFirstMode) == "EMode.EFirstMode" + assert repr(m.ClassWithUnscopedEnum.EMode.EFirstMode) == "" + assert repr(m.ClassWithUnscopedEnum.EFirstMode) == "" + + f = m.ClassWithUnscopedEnum.test_function + first = m.ClassWithUnscopedEnum.EFirstMode + second = m.ClassWithUnscopedEnum.ESecondMode + + assert f(first) == 1 + + assert f(first) == f(first) + assert not f(first) != f(first) + + assert f(first) != f(second) + assert not f(first) == f(second) + + assert f(first) == int(f(first)) + assert not f(first) != int(f(first)) + + assert f(first) != int(f(second)) + assert not f(first) == int(f(second)) + + # noinspection PyDictCreation + x = {f(first): 1, f(second): 2} + x[f(first)] = 3 + x[f(second)] = 4 + # Hashing test + assert repr(x) == "{: 3, : 4}" + + +@pytest.mark.xfail( + env.GRAALPY and env.GRAALPY_VERSION < (24, 2), reason="Fixed in GraalPy 24.2" +) +def test_binary_operators(): + assert int(m.Flags.Read) == 4 + assert int(m.Flags.Write) == 2 + assert int(m.Flags.Execute) == 1 + assert int(m.Flags.Read | m.Flags.Write | m.Flags.Execute) == 7 + assert int(m.Flags.Read | m.Flags.Write) == 6 + assert int(m.Flags.Read | m.Flags.Execute) == 5 + assert int(m.Flags.Write | m.Flags.Execute) == 3 + assert int(m.Flags.Write | 1) == 3 + assert ~m.Flags.Write == -3 + + state = m.Flags.Read | m.Flags.Write + assert (state & m.Flags.Read) != 0 + assert (state & m.Flags.Write) != 0 + assert (state & m.Flags.Execute) == 0 + assert (state & 1) == 0 + + state2 = ~state + assert state2 == -7 + assert int(state ^ state2) == -1 + + +def test_enum_to_int(): + m.test_enum_to_int(m.Flags.Read) + m.test_enum_to_int(m.ClassWithUnscopedEnum.EMode.EFirstMode) + m.test_enum_to_int(m.ScopedCharEnum.Positive) + m.test_enum_to_int(m.ScopedBoolEnum.TRUE) + m.test_enum_to_uint(m.Flags.Read) + m.test_enum_to_uint(m.ClassWithUnscopedEnum.EMode.EFirstMode) + m.test_enum_to_uint(m.ScopedCharEnum.Positive) + m.test_enum_to_uint(m.ScopedBoolEnum.TRUE) + m.test_enum_to_long_long(m.Flags.Read) + m.test_enum_to_long_long(m.ClassWithUnscopedEnum.EMode.EFirstMode) + m.test_enum_to_long_long(m.ScopedCharEnum.Positive) + m.test_enum_to_long_long(m.ScopedBoolEnum.TRUE) + + +def test_duplicate_enum_name(): + with pytest.raises(ValueError) as excinfo: + m.register_bad_enum() + assert str(excinfo.value) == 'SimpleEnum: element "ONE" already exists!' + + +def test_char_underlying_enum(): # Issue #1331/PR #1334: + assert type(m.ScopedCharEnum.Positive.__int__()) is int + assert int(m.ScopedChar16Enum.Zero) == 0 + assert hash(m.ScopedChar32Enum.Positive) == 1 + assert type(m.ScopedCharEnum.Positive.__getstate__()) is int + assert m.ScopedWCharEnum(1) == m.ScopedWCharEnum.Positive + with pytest.raises(TypeError): + # Even if the underlying type is char, only an int can be used to construct the enum: + m.ScopedCharEnum("0") + + +def test_bool_underlying_enum(): + assert type(m.ScopedBoolEnum.TRUE.__int__()) is int + assert int(m.ScopedBoolEnum.FALSE) == 0 + assert hash(m.ScopedBoolEnum.TRUE) == 1 + assert type(m.ScopedBoolEnum.TRUE.__getstate__()) is int + assert m.ScopedBoolEnum(1) == m.ScopedBoolEnum.TRUE + # Enum could construct with a bool + # (bool is a strict subclass of int, and False will be converted to 0) + assert m.ScopedBoolEnum(False) == m.ScopedBoolEnum.FALSE + + +def test_docstring_signatures(): + for enum_type in [m.ScopedEnum, m.UnscopedEnum]: + for attr in enum_type.__dict__.values(): + # Issue #2623/PR #2637: Add argument names to enum_ methods + assert "arg0" not in (attr.__doc__ or "") + + +def test_str_signature(): + for enum_type in [m.ScopedEnum, m.UnscopedEnum]: + assert enum_type.__str__.__doc__.startswith("__str__") + + +def test_generated_dunder_methods_pos_only(): + for enum_type in [m.ScopedEnum, m.UnscopedEnum]: + for binary_op in [ + "__eq__", + "__ne__", + "__ge__", + "__gt__", + "__lt__", + "__le__", + "__and__", + "__rand__", + # "__or__", # fail with some compilers (__doc__ = "Return self|value.") + # "__ror__", # fail with some compilers (__doc__ = "Return value|self.") + "__xor__", + "__rxor__", + "__rxor__", + ]: + method = getattr(enum_type, binary_op, None) + if method is not None: + assert ( + re.match( + rf"^{binary_op}\(self: [\w\.]+, other: [\w\.]+, /\)", + method.__doc__, + ) + is not None + ) + for unary_op in [ + "__int__", + "__index__", + "__hash__", + "__str__", + "__repr__", + ]: + method = getattr(enum_type, unary_op, None) + if method is not None: + assert ( + re.match( + rf"^{unary_op}\(self: [\w\.]+, /\)", + method.__doc__, + ) + is not None + ) + assert ( + re.match( + r"^__getstate__\(self: [\w\.]+, /\)", + enum_type.__getstate__.__doc__, + ) + is not None + ) + assert ( + re.match( + r"^__setstate__\(self: [\w\.]+, state: [\w\. \|]+, /\)", + enum_type.__setstate__.__doc__, + ) + is not None + ) + + +@pytest.mark.skipif( + isinstance(m.obj_cast_UnscopedEnum_ptr, str), reason=m.obj_cast_UnscopedEnum_ptr +) +def test_obj_cast_unscoped_enum_ptr(): + assert m.obj_cast_UnscopedEnum_ptr(m.UnscopedEnum.ETwo) == 2 + assert m.obj_cast_UnscopedEnum_ptr(m.UnscopedEnum.EOne) == 1 + assert m.obj_cast_UnscopedEnum_ptr(None) == 0 diff --git a/external_libraries/pybind11/tests/test_eval.cpp b/external_libraries/pybind11/tests/test_eval.cpp new file mode 100644 index 00000000..cd2903f0 --- /dev/null +++ b/external_libraries/pybind11/tests/test_eval.cpp @@ -0,0 +1,118 @@ +/* + tests/test_eval.cpp -- Usage of eval() and eval_file() + + Copyright (c) 2016 Klemens D. Morgenstern + + All rights reserved. Use of this source code is governed by a + BSD-style license that can be found in the LICENSE file. +*/ + +#include + +#include "pybind11_tests.h" + +#include + +TEST_SUBMODULE(eval_, m) { + // test_evals + + auto global = py::dict(py::module_::import("__main__").attr("__dict__")); + + m.def("test_eval_statements", [global]() { + auto local = py::dict(); + local["call_test"] = py::cpp_function([&]() -> int { return 42; }); + + // Regular string literal + py::exec("message = 'Hello World!'\n" + "x = call_test()", + global, + local); + + // Multi-line raw string literal + py::exec(R"( + if x == 42: + print(message) + else: + raise RuntimeError + )", + global, + local); + auto x = local["x"].cast(); + + return x == 42; + }); + + m.def("test_eval", [global]() { + auto local = py::dict(); + local["x"] = py::int_(42); + auto x = py::eval("x", global, local); + return x.cast() == 42; + }); + + m.def("test_eval_single_statement", []() { + auto local = py::dict(); + local["call_test"] = py::cpp_function([&]() -> int { return 42; }); + + auto result = py::eval("x = call_test()", py::dict(), local); + auto x = local["x"].cast(); + return result.is_none() && x == 42; + }); + + m.def("test_eval_file", [global](py::str filename) { + auto local = py::dict(); + local["y"] = py::int_(43); + + int val_out = 0; + local["call_test2"] = py::cpp_function([&](int value) { val_out = value; }); + + auto result = py::eval_file(std::move(filename), global, local); + return val_out == 43 && result.is_none(); + }); + + m.def("test_eval_failure", []() { + try { + py::eval("nonsense code ..."); + } catch (py::error_already_set &) { + return true; + } + return false; + }); + + m.def("test_eval_file_failure", []() { + try { + py::eval_file("non-existing file"); + } catch (std::exception &) { + return true; + } + return false; + }); + + // test_eval_empty_globals + m.def("eval_empty_globals", [](py::object global) { + if (global.is_none()) { + global = py::dict(); + } + 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/external_libraries/pybind11/tests/test_eval.py b/external_libraries/pybind11/tests/test_eval.py new file mode 100644 index 00000000..8ac1907c --- /dev/null +++ b/external_libraries/pybind11/tests/test_eval.py @@ -0,0 +1,52 @@ +from __future__ import annotations + +import os + +import pytest + +import env # noqa: F401 +from pybind11_tests import eval_ as m + + +def test_evals(capture): + with capture: + assert m.test_eval_statements() + assert capture == "Hello World!" + + assert m.test_eval() + assert m.test_eval_single_statement() + + assert m.test_eval_failure() + + +@pytest.mark.xfail("env.PYPY or env.GRAALPY", raises=RuntimeError) +def test_eval_file(): + filename = os.path.join(os.path.dirname(__file__), "test_eval_call.py") + assert m.test_eval_file(filename) + + assert m.test_eval_file_failure() + + +def test_eval_empty_globals(): + assert "__builtins__" in m.eval_empty_globals(None) + + 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"]() diff --git a/external_libraries/pybind11/tests/test_eval_call.py b/external_libraries/pybind11/tests/test_eval_call.py new file mode 100644 index 00000000..a677249d --- /dev/null +++ b/external_libraries/pybind11/tests/test_eval_call.py @@ -0,0 +1,5 @@ +# This file is called from 'test_eval.py' +from __future__ import annotations + +if "call_test2" in locals(): + call_test2(y) # noqa: F821 undefined name diff --git a/external_libraries/pybind11/tests/test_exceptions.cpp b/external_libraries/pybind11/tests/test_exceptions.cpp new file mode 100644 index 00000000..0a970065 --- /dev/null +++ b/external_libraries/pybind11/tests/test_exceptions.cpp @@ -0,0 +1,427 @@ +/* + tests/test_custom-exceptions.cpp -- exception translation + + Copyright (c) 2016 Pim Schellart + + All rights reserved. Use of this source code is governed by a + BSD-style license that can be found in the LICENSE file. +*/ +#include + +#include "test_exceptions.h" + +#include "local_bindings.h" +#include "pybind11_tests.h" + +#include +#include +#include + +// A type that should be raised as an exception in Python +class MyException : public std::exception { +public: + explicit MyException(const char *m) : message{m} {} + const char *what() const noexcept override { return message.c_str(); } + +private: + std::string message = ""; +}; + +class MyExceptionUseDeprecatedOperatorCall : public MyException { + using MyException::MyException; +}; + +// A type that should be translated to a standard Python exception +class MyException2 : public std::exception { +public: + explicit MyException2(const char *m) : message{m} {} + const char *what() const noexcept override { return message.c_str(); } + +private: + std::string message = ""; +}; + +// A type that is not derived from std::exception (and is thus unknown) +class MyException3 { +public: + explicit MyException3(const char *m) : message{m} {} + virtual const char *what() const noexcept { return message.c_str(); } + // Rule of 5 BEGIN: to preempt compiler warnings. + MyException3(const MyException3 &) = default; + MyException3(MyException3 &&) = default; + MyException3 &operator=(const MyException3 &) = default; + MyException3 &operator=(MyException3 &&) = default; + virtual ~MyException3() = default; + // Rule of 5 END. +private: + std::string message = ""; +}; + +// A type that should be translated to MyException +// and delegated to its exception translator +class MyException4 : public std::exception { +public: + explicit MyException4(const char *m) : message{m} {} + const char *what() const noexcept override { return message.c_str(); } + +private: + std::string message = ""; +}; + +// Like the above, but declared via the helper function +class MyException5 : public std::logic_error { +public: + explicit MyException5(const std::string &what) : std::logic_error(what) {} +}; + +// Inherits from MyException5 +class MyException5_1 : public MyException5 { + using MyException5::MyException5; +}; + +// Exception that will be caught via the module local translator. +class MyException6 : public std::exception { +public: + explicit MyException6(const char *m) : message{m} {} + const char *what() const noexcept override { return message.c_str(); } + +private: + std::string message = ""; +}; + +struct PythonCallInDestructor { + explicit PythonCallInDestructor(const py::dict &d) : d(d) {} + ~PythonCallInDestructor() { d["good"] = true; } + + py::dict d; +}; + +struct PythonAlreadySetInDestructor { + explicit PythonAlreadySetInDestructor(const py::str &s) : s(s) {} + ~PythonAlreadySetInDestructor() { + py::dict foo; + try { + // Assign to a py::object to force read access of nonexistent dict entry + py::object o = foo["bar"]; + } catch (py::error_already_set &ex) { + ex.discard_as_unraisable(s); + } + } + + py::str s; +}; + +struct CustomData { + explicit CustomData(const std::string &a) : a(a) {} + std::string a; +}; + +struct MyException7 { + explicit MyException7(const CustomData &message) : message(message) {} + CustomData message; +}; + +TEST_SUBMODULE(exceptions, m) { + m.def("throw_std_exception", + []() { throw std::runtime_error("This exception was intentionally thrown."); }); + + // PLEASE KEEP IN SYNC with docs/advanced/exceptions.rst + PYBIND11_CONSTINIT static py::gil_safe_call_once_and_store ex_storage; + ex_storage.call_once_and_store_result( + [&]() { return py::exception(m, "MyException"); }); + py::register_exception_translator([](std::exception_ptr p) { + try { + if (p) { + std::rethrow_exception(p); + } + } catch (const MyException &e) { + // Set MyException as the active python error + py::set_error(ex_storage.get_stored(), e.what()); + } + }); + + // Same as above, but using the deprecated `py::exception<>::operator()` + // We want to be sure it still works, until it's removed. + static const auto *const exd = new py::exception( + m, "MyExceptionUseDeprecatedOperatorCall"); + py::register_exception_translator([](std::exception_ptr p) { + try { + if (p) { + std::rethrow_exception(p); + } + } catch (const MyExceptionUseDeprecatedOperatorCall &e) { +#if defined(__INTEL_COMPILER) || defined(__NVCOMPILER) + // It is not worth the trouble dealing with warning suppressions for these compilers. + // Falling back to the recommended approach to keep the test code simple. + py::set_error(*exd, e.what()); +#else + PYBIND11_WARNING_PUSH + PYBIND11_WARNING_DISABLE_CLANG("-Wdeprecated-declarations") + PYBIND11_WARNING_DISABLE_GCC("-Wdeprecated-declarations") + PYBIND11_WARNING_DISABLE_MSVC(4996) + (*exd)(e.what()); + PYBIND11_WARNING_POP +#endif + } + }); + + // register new translator for MyException2 + // no need to store anything here because this type will + // never by visible from Python + py::register_exception_translator([](std::exception_ptr p) { + try { + if (p) { + std::rethrow_exception(p); + } + } catch (const MyException2 &e) { + // Translate this exception to a standard RuntimeError + py::set_error(PyExc_RuntimeError, e.what()); + } + }); + + // register new translator for MyException4 + // which will catch it and delegate to the previously registered + // translator for MyException by throwing a new exception + py::register_exception_translator([](std::exception_ptr p) { + try { + if (p) { + std::rethrow_exception(p); + } + } catch (const MyException4 &e) { + throw MyException(e.what()); + } + }); + + // A simple exception translation: + auto ex5 = py::register_exception(m, "MyException5"); + // A slightly more complicated one that declares MyException5_1 as a subclass of MyException5 + py::register_exception(m, "MyException5_1", ex5.ptr()); + + // py::register_local_exception(m, "LocalSimpleException") + + py::register_local_exception_translator([](std::exception_ptr p) { + try { + if (p) { + std::rethrow_exception(p); + } + } catch (const MyException6 &e) { + py::set_error(PyExc_RuntimeError, e.what()); + } + }); + + m.def("throws1", + []() { throw MyException("this error should go to py::exception"); }); + m.def("throws1d", []() { + throw MyExceptionUseDeprecatedOperatorCall( + "this error should go to py::exception"); + }); + m.def("throws2", + []() { throw MyException2("this error should go to a standard Python exception"); }); + m.def("throws3", []() { throw MyException3("this error cannot be translated"); }); + m.def("throws4", []() { throw MyException4("this error is rethrown"); }); + m.def("throws5", + []() { throw MyException5("this is a helper-defined translated exception"); }); + m.def("throws5_1", []() { throw MyException5_1("MyException5 subclass"); }); + m.def("throws6", []() { throw MyException6("MyException6 only handled in this module"); }); + m.def("throws_logic_error", []() { + throw std::logic_error("this error should fall through to the standard handler"); + }); + m.def("throws_overflow_error", []() { throw std::overflow_error(""); }); + m.def("throws_local_error", []() { throw LocalException("never caught"); }); + m.def("throws_local_simple_error", []() { throw LocalSimpleException("this mod"); }); + m.def("exception_matches", []() { + py::dict foo; + try { + // Assign to a py::object to force read access of nonexistent dict entry + py::object o = foo["bar"]; + } catch (py::error_already_set &ex) { + if (!ex.matches(PyExc_KeyError)) { + throw; + } + return true; + } + return false; + }); + m.def("exception_matches_base", []() { + py::dict foo; + try { + // Assign to a py::object to force read access of nonexistent dict entry + py::object o = foo["bar"]; + } catch (py::error_already_set &ex) { + if (!ex.matches(PyExc_Exception)) { + throw; + } + return true; + } + return false; + }); + m.def("modulenotfound_exception_matches_base", []() { + try { + // On Python >= 3.6, this raises a ModuleNotFoundError, a subclass of ImportError + py::module_::import("nonexistent"); + } catch (py::error_already_set &ex) { + if (!ex.matches(PyExc_ImportError)) { + throw; + } + return true; + } + return false; + }); + + m.def("throw_already_set", [](bool err) { + if (err) { + py::set_error(PyExc_ValueError, "foo"); + } + try { + throw py::error_already_set(); + } catch (const std::runtime_error &e) { + if ((err && e.what() != std::string("ValueError: foo")) + || (!err + && e.what() + != std::string("Internal error: pybind11::error_already_set called " + "while Python error indicator not set."))) { + PyErr_Clear(); + throw std::runtime_error("error message mismatch"); + } + } + PyErr_Clear(); + if (err) { + py::set_error(PyExc_ValueError, "foo"); + } + throw py::error_already_set(); + }); + + m.def("python_call_in_destructor", [](const py::dict &d) { + bool retval = false; + try { + PythonCallInDestructor set_dict_in_destructor(d); + py::set_error(PyExc_ValueError, "foo"); + throw py::error_already_set(); + } catch (const py::error_already_set &) { + retval = true; + } + return retval; + }); + + m.def("python_alreadyset_in_destructor", [](const py::str &s) { + PythonAlreadySetInDestructor alreadyset_in_destructor(s); + return true; + }); + + // test_nested_throws + m.def("try_catch", + [m](const py::object &exc_type, const py::function &f, const py::args &args) { + try { + f(*args); + } catch (py::error_already_set &ex) { + if (ex.matches(exc_type)) { + py::print(ex.what()); + } else { + // Simply `throw;` also works and is better, but using `throw ex;` + // here to cover that situation (as observed in the wild). + throw ex; // Invokes the copy ctor. + } + } + }); + + // Test repr that cannot be displayed + m.def("simple_bool_passthrough", [](bool x) { return x; }); + + m.def("throw_should_be_translated_to_key_error", []() { throw shared_exception(); }); + + m.def("raise_from", []() { + py::set_error(PyExc_ValueError, "inner"); + py::raise_from(PyExc_ValueError, "outer"); + throw py::error_already_set(); + }); + + m.def("raise_from_already_set", []() { + try { + py::set_error(PyExc_ValueError, "inner"); + throw py::error_already_set(); + } catch (py::error_already_set &e) { + py::raise_from(e, PyExc_ValueError, "outer"); + throw py::error_already_set(); + } + }); + + m.def("throw_nested_exception", []() { + try { + throw std::runtime_error("Inner Exception"); + } catch (const std::runtime_error &) { + std::throw_with_nested(std::runtime_error("Outer Exception")); + } + }); + + m.def("error_already_set_what", [](const py::object &exc_type, const py::object &exc_value) { + py::set_error(exc_type, exc_value); + std::string what = py::error_already_set().what(); + bool py_err_set_after_what = (PyErr_Occurred() != nullptr); + PyErr_Clear(); + return py::make_tuple(std::move(what), py_err_set_after_what); + }); + + m.def("test_cross_module_interleaved_error_already_set", []() { + auto cm = py::module_::import("cross_module_interleaved_error_already_set"); + auto interleaved_error_already_set + = reinterpret_cast(PyLong_AsVoidPtr(cm.attr("funcaddr").ptr())); + interleaved_error_already_set(); + }); + + m.def("test_error_already_set_double_restore", [](bool dry_run) { + py::set_error(PyExc_ValueError, "Random error."); + py::error_already_set e; + e.restore(); + PyErr_Clear(); + if (!dry_run) { + e.restore(); + } + }); + + // https://github.com/pybind/pybind11/issues/4075 + m.def("test_pypy_oserror_normalization", []() { + try { + py::module_::import("io").attr("open")("this_filename_must_not_exist", "r"); + } catch (const py::error_already_set &e) { + return py::str(e.what()); // str must be built before e goes out of scope. + } + return py::str("UNEXPECTED"); + }); + + m.def("test_fn_cast_int", [](const py::function &fn) { + // function returns None instead of int, should give a useful error message + fn().cast(); + }); + + // m.def("pass_exception_void", [](const py::exception&) {}); // Does not compile. + m.def("return_exception_void", []() { return py::exception(); }); + + m.def("throws7", []() { + auto data = CustomData("abc"); + throw MyException7(data); + }); + + py::class_(m, "CustomData", py::module_local()) + .def(py::init()) + .def_readwrite("a", &CustomData::a); + + PYBIND11_CONSTINIT static py::gil_safe_call_once_and_store + PythonMyException7_storage; + PythonMyException7_storage.call_once_and_store_result([&]() { + auto mod = py::module_::import("custom_exceptions"); + py::object obj = mod.attr("PythonMyException7"); + return obj; + }); + + py::register_local_exception_translator([](std::exception_ptr p) { + try { + if (p) { + std::rethrow_exception(p); + } + } catch (const MyException7 &e) { + auto exc_type = PythonMyException7_storage.get_stored(); + py::object exc_inst = exc_type(e.message); + PyErr_SetObject(PyExc_Exception, exc_inst.ptr()); + } + }); +} diff --git a/external_libraries/pybind11/tests/test_exceptions.h b/external_libraries/pybind11/tests/test_exceptions.h new file mode 100644 index 00000000..2eaa3d3d --- /dev/null +++ b/external_libraries/pybind11/tests/test_exceptions.h @@ -0,0 +1,13 @@ +#pragma once +#include "pybind11_tests.h" + +#include + +// shared exceptions for cross_module_tests + +class PYBIND11_EXPORT_EXCEPTION shared_exception : public pybind11::builtin_exception { +public: + using builtin_exception::builtin_exception; + explicit shared_exception() : shared_exception("") {} + void set_error() const override { py::set_error(PyExc_RuntimeError, what()); } +}; diff --git a/external_libraries/pybind11/tests/test_exceptions.py b/external_libraries/pybind11/tests/test_exceptions.py new file mode 100644 index 00000000..59845b44 --- /dev/null +++ b/external_libraries/pybind11/tests/test_exceptions.py @@ -0,0 +1,439 @@ +from __future__ import annotations + +import sys + +import pytest +from custom_exceptions import PythonMyException7 + +import env +import pybind11_cross_module_tests as cm +import pybind11_tests +from pybind11_tests import exceptions as m + + +def test_std_exception(msg): + with pytest.raises(RuntimeError) as excinfo: + m.throw_std_exception() + assert msg(excinfo.value) == "This exception was intentionally thrown." + + +def test_error_already_set(msg): + with pytest.raises(RuntimeError) as excinfo: + m.throw_already_set(False) + assert ( + msg(excinfo.value) + == "Internal error: pybind11::error_already_set called while Python error indicator not set." + ) + + with pytest.raises(ValueError) as excinfo: + m.throw_already_set(True) + assert msg(excinfo.value) == "foo" + + +def test_raise_from(msg): + with pytest.raises(ValueError) as excinfo: + m.raise_from() + assert msg(excinfo.value) == "outer" + assert msg(excinfo.value.__cause__) == "inner" + + +def test_raise_from_already_set(msg): + with pytest.raises(ValueError) as excinfo: + m.raise_from_already_set() + assert msg(excinfo.value) == "outer" + assert msg(excinfo.value.__cause__) == "inner" + + +def test_cross_module_exceptions(msg): + with pytest.raises(RuntimeError) as excinfo: + cm.raise_runtime_error() + assert str(excinfo.value) == "My runtime error" + + with pytest.raises(ValueError) as excinfo: + cm.raise_value_error() + assert str(excinfo.value) == "My value error" + + with pytest.raises(ValueError) as excinfo: + cm.throw_pybind_value_error() + assert str(excinfo.value) == "pybind11 value error" + + with pytest.raises(TypeError) as excinfo: + cm.throw_pybind_type_error() + assert str(excinfo.value) == "pybind11 type error" + + with pytest.raises(StopIteration) as excinfo: + cm.throw_stop_iteration() + + with pytest.raises(cm.LocalSimpleException) as excinfo: + cm.throw_local_simple_error() + assert msg(excinfo.value) == "external mod" + + with pytest.raises(KeyError) as excinfo: + cm.throw_local_error() + # KeyError is a repr of the key, so it has an extra set of quotes + assert str(excinfo.value) == "'just local'" + + +# TODO: FIXME +@pytest.mark.xfail( + "(env.MACOS and env.PYPY) or env.ANDROID or env.FREEBSD", + raises=RuntimeError, + reason="See Issue #2847, PR #2999, PR #4324, PR #5925", + strict=not env.PYPY, # PR 5569 +) +def test_cross_module_exception_translator(): + with pytest.raises(KeyError): + # translator registered in cross_module_tests + m.throw_should_be_translated_to_key_error() + + +def test_python_call_in_catch(): + d = {} + assert m.python_call_in_destructor(d) is True + assert d["good"] is True + + +def ignore_pytest_unraisable_warning(f): + unraisable = "PytestUnraisableExceptionWarning" + if hasattr(pytest, unraisable): # Python >= 3.8 and pytest >= 6 + dec = pytest.mark.filterwarnings(f"ignore::pytest.{unraisable}") + return dec(f) + return f + + +# TODO: find out why this fails on PyPy, https://foss.heptapod.net/pypy/pypy/-/issues/3583 +@pytest.mark.xfail(env.PYPY, reason="Failure on PyPy 3.8 (7.3.7)", strict=False) +@ignore_pytest_unraisable_warning +def test_python_alreadyset_in_destructor(monkeypatch, capsys): + triggered = False + + # Don't take `sys.unraisablehook`, as that's overwritten by pytest + default_hook = sys.__unraisablehook__ + + def hook(unraisable_hook_args): + exc_type, exc_value, exc_tb, err_msg, obj = unraisable_hook_args + if obj == "already_set demo": + nonlocal triggered + triggered = True + default_hook(unraisable_hook_args) + return + + # Use monkeypatch so pytest can apply and remove the patch as appropriate + monkeypatch.setattr(sys, "unraisablehook", hook) + + assert m.python_alreadyset_in_destructor("already_set demo") is True + assert triggered is True + + _, captured_stderr = capsys.readouterr() + assert captured_stderr.startswith("Exception ignored in: 'already_set demo'") + assert captured_stderr.rstrip().endswith("KeyError: 'bar'") + + +def test_exception_matches(): + assert m.exception_matches() + assert m.exception_matches_base() + assert m.modulenotfound_exception_matches_base() + + +def test_custom(msg): + # Can we catch a MyException? + with pytest.raises(m.MyException) as excinfo: + m.throws1() + assert msg(excinfo.value) == "this error should go to py::exception" + + # Can we catch a MyExceptionUseDeprecatedOperatorCall? + with pytest.raises(m.MyExceptionUseDeprecatedOperatorCall) as excinfo: + m.throws1d() + assert ( + msg(excinfo.value) + == "this error should go to py::exception" + ) + + # Can we translate to standard Python exceptions? + with pytest.raises(RuntimeError) as excinfo: + m.throws2() + assert msg(excinfo.value) == "this error should go to a standard Python exception" + + # Can we handle unknown exceptions? + with pytest.raises(RuntimeError) as excinfo: + m.throws3() + assert msg(excinfo.value) == "Caught an unknown exception!" + + # Can we delegate to another handler by rethrowing? + with pytest.raises(m.MyException) as excinfo: + m.throws4() + assert msg(excinfo.value) == "this error is rethrown" + + # Can we fall-through to the default handler? + with pytest.raises(RuntimeError) as excinfo: + m.throws_logic_error() + assert ( + msg(excinfo.value) == "this error should fall through to the standard handler" + ) + + # OverFlow error translation. + with pytest.raises(OverflowError) as excinfo: + m.throws_overflow_error() + + # Can we handle a helper-declared exception? + with pytest.raises(m.MyException5) as excinfo: + m.throws5() + assert msg(excinfo.value) == "this is a helper-defined translated exception" + + # Exception subclassing: + with pytest.raises(m.MyException5) as excinfo: + m.throws5_1() + assert msg(excinfo.value) == "MyException5 subclass" + assert isinstance(excinfo.value, m.MyException5_1) + + with pytest.raises(m.MyException5_1) as excinfo: + m.throws5_1() + assert msg(excinfo.value) == "MyException5 subclass" + + with pytest.raises(m.MyException5) as excinfo: # noqa: PT012 + try: + m.throws5() + except m.MyException5_1 as err: + raise RuntimeError("Exception error: caught child from parent") from err + assert msg(excinfo.value) == "this is a helper-defined translated exception" + + with pytest.raises(PythonMyException7) as excinfo: + m.throws7() + assert msg(excinfo.value) == "[PythonMyException7]: abc" + + +@pytest.mark.xfail("env.GRAALPY", reason="TODO should get fixed on GraalPy side") +def test_nested_throws(capture): + """Tests nested (e.g. C++ -> Python -> C++) exception handling""" + + def throw_myex(): + raise m.MyException("nested error") + + def throw_myex5(): + raise m.MyException5("nested error 5") + + # In the comments below, the exception is caught in the first step, thrown in the last step + + # C++ -> Python + with capture: + m.try_catch(m.MyException5, throw_myex5) + assert str(capture).startswith("MyException5: nested error 5") + + # Python -> C++ -> Python + with pytest.raises(m.MyException) as excinfo: + m.try_catch(m.MyException5, throw_myex) + assert str(excinfo.value) == "nested error" + + def pycatch(exctype, f, *args): # noqa: ARG001 + try: + f(*args) + except m.MyException as e: + print(e) + + # C++ -> Python -> C++ -> Python + with capture: + m.try_catch( + m.MyException5, + pycatch, + m.MyException, + m.try_catch, + m.MyException, + throw_myex5, + ) + assert str(capture).startswith("MyException5: nested error 5") + + # C++ -> Python -> C++ + with capture: + m.try_catch(m.MyException, pycatch, m.MyException5, m.throws4) + assert capture == "this error is rethrown" + + # Python -> C++ -> Python -> C++ + with pytest.raises(m.MyException5) as excinfo: + m.try_catch(m.MyException, pycatch, m.MyException, m.throws5) + assert str(excinfo.value) == "this is a helper-defined translated exception" + + +# TODO: Investigate this crash, see pybind/pybind11#5062 for background +@pytest.mark.skipif( + sys.platform.startswith("win32") and "Clang" in pybind11_tests.compiler_info, + reason="Started segfaulting February 2024", +) +def test_throw_nested_exception(): + with pytest.raises(RuntimeError) as excinfo: + m.throw_nested_exception() + assert str(excinfo.value) == "Outer Exception" + assert str(excinfo.value.__cause__) == "Inner Exception" + + +# This can often happen if you wrap a pybind11 class in a Python wrapper +def test_invalid_repr(): + class MyRepr: + def __repr__(self): + raise AttributeError("Example error") + + with pytest.raises(TypeError): + m.simple_bool_passthrough(MyRepr()) + + +def test_local_translator(msg): + """Tests that a local translator works and that the local translator from + the cross module is not applied""" + with pytest.raises(RuntimeError) as excinfo: + m.throws6() + assert msg(excinfo.value) == "MyException6 only handled in this module" + + with pytest.raises(RuntimeError) as excinfo: + m.throws_local_error() + assert not isinstance(excinfo.value, KeyError) + assert msg(excinfo.value) == "never caught" + + with pytest.raises(Exception) as excinfo: + m.throws_local_simple_error() + assert not isinstance(excinfo.value, cm.LocalSimpleException) + assert msg(excinfo.value) == "this mod" + + +def test_error_already_set_message_with_unicode_surrogate(): # Issue #4288 + assert m.error_already_set_what(RuntimeError, "\ud927") == ( + "RuntimeError: \\ud927", + False, + ) + + +def test_error_already_set_message_with_malformed_utf8(): + assert m.error_already_set_what(RuntimeError, b"\x80") == ( + "RuntimeError: b'\\x80'", + False, + ) + + +class FlakyException(Exception): + def __init__(self, failure_point): + if failure_point == "failure_point_init": + raise ValueError("triggered_failure_point_init") + self.failure_point = failure_point + + def __str__(self): + if self.failure_point == "failure_point_str": + raise ValueError("triggered_failure_point_str") + return "FlakyException.__str__" + + +@pytest.mark.parametrize( + ("exc_type", "exc_value", "expected_what"), + [ + (ValueError, "plain_str", "ValueError: plain_str"), + (ValueError, ("tuple_elem",), "ValueError: tuple_elem"), + (FlakyException, ("happy",), "FlakyException: FlakyException.__str__"), + ], +) +def test_error_already_set_what_with_happy_exceptions( + exc_type, exc_value, expected_what +): + what, py_err_set_after_what = m.error_already_set_what(exc_type, exc_value) + assert not py_err_set_after_what + assert what == expected_what + + +def _test_flaky_exception_failure_point_init_before_py_3_12(): + with pytest.raises(RuntimeError) as excinfo: + m.error_already_set_what(FlakyException, ("failure_point_init",)) + lines = str(excinfo.value).splitlines() + # PyErr_NormalizeException replaces the original FlakyException with ValueError: + assert lines[:3] == [ + "pybind11::error_already_set: MISMATCH of original and normalized active exception types:" + " ORIGINAL FlakyException REPLACED BY ValueError: triggered_failure_point_init", + "", + "At:", + ] + # Checking the first two lines of the traceback as formatted in error_string(): + assert "test_exceptions.py(" in lines[3] + assert lines[3].endswith("): __init__") + assert lines[4].endswith( + "): _test_flaky_exception_failure_point_init_before_py_3_12" + ) + + +def _test_flaky_exception_failure_point_init_py_3_12(): + # Behavior change in Python 3.12: https://github.com/python/cpython/issues/102594 + what, py_err_set_after_what = m.error_already_set_what( + FlakyException, ("failure_point_init",) + ) + assert not py_err_set_after_what + lines = what.splitlines() + assert lines[0].endswith("ValueError[WITH __notes__]: triggered_failure_point_init") + assert lines[1] == "__notes__ (len=1):" + assert "Normalization failed:" in lines[2] + assert "FlakyException" in lines[2] + + +@pytest.mark.skipif( + "env.PYPY and sys.version_info[:2] < (3, 12)", + reason="PyErr_NormalizeException Segmentation fault", +) +@pytest.mark.xfail("env.GRAALPY", reason="TODO should be fixed on GraalPy side") +def test_flaky_exception_failure_point_init(): + if sys.version_info[:2] < (3, 12): + _test_flaky_exception_failure_point_init_before_py_3_12() + else: + _test_flaky_exception_failure_point_init_py_3_12() + + +@pytest.mark.xfail("env.GRAALPY", reason="TODO should be fixed on GraalPy side") +def test_flaky_exception_failure_point_str(): + what, py_err_set_after_what = m.error_already_set_what( + FlakyException, ("failure_point_str",) + ) + assert not py_err_set_after_what + lines = what.splitlines() + n = 3 if env.PYPY and len(lines) == 3 else 5 + assert ( + lines[:n] + == [ + "FlakyException: ", + "", + "MESSAGE UNAVAILABLE DUE TO EXCEPTION: ValueError: triggered_failure_point_str", + "", + "At:", + ][:n] + ) + + +def test_cross_module_interleaved_error_already_set(): + with pytest.raises(RuntimeError) as excinfo: + m.test_cross_module_interleaved_error_already_set() + assert str(excinfo.value) in ( + "2nd error.", # Almost all platforms. + "RuntimeError: 2nd error.", # Some PyPy builds (seen under macOS). + ) + + +def test_error_already_set_double_restore(): + m.test_error_already_set_double_restore(True) # dry_run + with pytest.raises(RuntimeError) as excinfo: + m.test_error_already_set_double_restore(False) + assert str(excinfo.value) == ( + "Internal error: pybind11::detail::error_fetch_and_normalize::restore()" + " called a second time. ORIGINAL ERROR: ValueError: Random error." + ) + + +def test_pypy_oserror_normalization(): + # https://github.com/pybind/pybind11/issues/4075 + what = m.test_pypy_oserror_normalization() + assert "this_filename_must_not_exist" in what + + +def test_fn_cast_int_exception(): + with pytest.raises(RuntimeError) as excinfo: + m.test_fn_cast_int(lambda: None) + + assert str(excinfo.value).startswith( + "Unable to cast Python instance of type to C++ type" + ) + + +def test_return_exception_void(): + with pytest.raises(TypeError) as excinfo: + m.return_exception_void() + assert "Exception" in str(excinfo.value) diff --git a/external_libraries/pybind11/tests/test_factory_constructors.cpp b/external_libraries/pybind11/tests/test_factory_constructors.cpp new file mode 100644 index 00000000..c96a3a31 --- /dev/null +++ b/external_libraries/pybind11/tests/test_factory_constructors.cpp @@ -0,0 +1,434 @@ +/* + tests/test_factory_constructors.cpp -- tests construction from a factory function + via py::init_factory() + + Copyright (c) 2017 Jason Rhinelander + + All rights reserved. Use of this source code is governed by a + BSD-style license that can be found in the LICENSE file. +*/ + +#include "constructor_stats.h" +#include "pybind11_tests.h" + +#include +#include +#include + +// Classes for testing python construction via C++ factory function: +// Not publicly constructible, copyable, or movable: +class TestFactory1 { + friend class TestFactoryHelper; + TestFactory1() : value("(empty)") { print_default_created(this); } + explicit TestFactory1(int v) : value(std::to_string(v)) { print_created(this, value); } + explicit TestFactory1(std::string v) : value(std::move(v)) { print_created(this, value); } + +public: + std::string value; + TestFactory1(TestFactory1 &&) = delete; + TestFactory1(const TestFactory1 &) = delete; + TestFactory1 &operator=(TestFactory1 &&) = delete; + TestFactory1 &operator=(const TestFactory1 &) = delete; + ~TestFactory1() { print_destroyed(this); } +}; +// Non-public construction, but moveable: +class TestFactory2 { + friend class TestFactoryHelper; + TestFactory2() : value("(empty2)") { print_default_created(this); } + explicit TestFactory2(int v) : value(std::to_string(v)) { print_created(this, value); } + explicit TestFactory2(std::string v) : value(std::move(v)) { print_created(this, value); } + +public: + TestFactory2(TestFactory2 &&m) noexcept : value{std::move(m.value)} { + print_move_created(this); + } + TestFactory2 &operator=(TestFactory2 &&m) noexcept { + value = std::move(m.value); + print_move_assigned(this); + return *this; + } + std::string value; + ~TestFactory2() { print_destroyed(this); } +}; +// Mixed direct/factory construction: +class TestFactory3 { +protected: + friend class TestFactoryHelper; + TestFactory3() : value("(empty3)") { print_default_created(this); } + explicit TestFactory3(int v) : value(std::to_string(v)) { print_created(this, value); } + +public: + explicit TestFactory3(std::string v) : value(std::move(v)) { print_created(this, value); } + TestFactory3(TestFactory3 &&m) noexcept : value{std::move(m.value)} { + print_move_created(this); + } + TestFactory3 &operator=(TestFactory3 &&m) noexcept { + value = std::move(m.value); + print_move_assigned(this); + return *this; + } + std::string value; + virtual ~TestFactory3() { print_destroyed(this); } +}; +// Inheritance test +class TestFactory4 : public TestFactory3 { +public: + TestFactory4() { print_default_created(this); } + explicit TestFactory4(int v) : TestFactory3(v) { print_created(this, v); } + ~TestFactory4() override { print_destroyed(this); } +}; +// Another class for an invalid downcast test +class TestFactory5 : public TestFactory3 { +public: + explicit TestFactory5(int i) : TestFactory3(i) { print_created(this, i); } + ~TestFactory5() override { print_destroyed(this); } +}; + +class TestFactory6 { +protected: + int value; + bool alias = false; + +public: + explicit TestFactory6(int i) : value{i} { print_created(this, i); } + TestFactory6(TestFactory6 &&f) noexcept { + print_move_created(this); + // NOLINTNEXTLINE(cppcoreguidelines-prefer-member-initializer) + value = f.value; + // NOLINTNEXTLINE(cppcoreguidelines-prefer-member-initializer) + alias = f.alias; + } + TestFactory6(const TestFactory6 &f) { + print_copy_created(this); + // NOLINTNEXTLINE(cppcoreguidelines-prefer-member-initializer) + value = f.value; + // NOLINTNEXTLINE(cppcoreguidelines-prefer-member-initializer) + alias = f.alias; + } + virtual ~TestFactory6() { print_destroyed(this); } + virtual int get() { return value; } + bool has_alias() const { return alias; } +}; +class PyTF6 : public TestFactory6 { +public: + // Special constructor that allows the factory to construct a PyTF6 from a TestFactory6 only + // when an alias is needed: + explicit PyTF6(TestFactory6 &&base) : TestFactory6(std::move(base)) { + alias = true; + print_created(this, "move", value); + } + explicit PyTF6(int i) : TestFactory6(i) { + alias = true; + print_created(this, i); + } + PyTF6(PyTF6 &&f) noexcept : TestFactory6(std::move(f)) { print_move_created(this); } + PyTF6(const PyTF6 &f) : TestFactory6(f) { print_copy_created(this); } + explicit PyTF6(std::string s) : TestFactory6((int) s.size()) { + alias = true; + print_created(this, s); + } + ~PyTF6() override { print_destroyed(this); } + int get() override { PYBIND11_OVERRIDE(int, TestFactory6, get, /*no args*/); } +}; + +class TestFactory7 { +protected: + int value; + bool alias = false; + +public: + explicit TestFactory7(int i) : value{i} { print_created(this, i); } + TestFactory7(TestFactory7 &&f) noexcept { + print_move_created(this); + // NOLINTNEXTLINE(cppcoreguidelines-prefer-member-initializer) + value = f.value; + // NOLINTNEXTLINE(cppcoreguidelines-prefer-member-initializer) + alias = f.alias; + } + TestFactory7(const TestFactory7 &f) { + print_copy_created(this); + // NOLINTNEXTLINE(cppcoreguidelines-prefer-member-initializer) + value = f.value; + // NOLINTNEXTLINE(cppcoreguidelines-prefer-member-initializer) + alias = f.alias; + } + virtual ~TestFactory7() { print_destroyed(this); } + virtual int get() { return value; } + bool has_alias() const { return alias; } +}; +class PyTF7 : public TestFactory7 { +public: + explicit PyTF7(int i) : TestFactory7(i) { + // NOLINTNEXTLINE(cppcoreguidelines-prefer-member-initializer) + alias = true; + print_created(this, i); + } + PyTF7(PyTF7 &&f) noexcept : TestFactory7(std::move(f)) { print_move_created(this); } + PyTF7(const PyTF7 &f) : TestFactory7(f) { print_copy_created(this); } + ~PyTF7() override { print_destroyed(this); } + int get() override { PYBIND11_OVERRIDE(int, TestFactory7, get, /*no args*/); } +}; + +class TestFactoryHelper { +public: + // Non-movable, non-copyable type: + // Return via pointer: + static TestFactory1 *construct1() { return new TestFactory1(); } + // Holder: + static std::unique_ptr construct1(int a) { + return std::unique_ptr(new TestFactory1(a)); + } + // pointer again + static TestFactory1 *construct1_string(std::string a) { + return new TestFactory1(std::move(a)); + } + + // Moveable type: + // pointer: + static TestFactory2 *construct2() { return new TestFactory2(); } + // holder: + static std::unique_ptr construct2(int a) { + return std::unique_ptr(new TestFactory2(a)); + } + // by value moving: + static TestFactory2 construct2(std::string a) { return TestFactory2(std::move(a)); } + + // shared_ptr holder type: + // pointer: + static TestFactory3 *construct3() { return new TestFactory3(); } + // holder: + static std::shared_ptr construct3(int a) { + return std::shared_ptr(new TestFactory3(a)); + } +}; + +TEST_SUBMODULE(factory_constructors, m) { + + // Define various trivial types to allow simpler overload resolution: + py::module_ m_tag = m.def_submodule("tag"); +#define MAKE_TAG_TYPE(Name) \ + struct Name##_tag {}; \ + py::class_(m_tag, #Name "_tag").def(py::init<>()); \ + m_tag.attr(#Name) = py::cast(Name##_tag{}) + MAKE_TAG_TYPE(pointer); + MAKE_TAG_TYPE(unique_ptr); + MAKE_TAG_TYPE(move); + MAKE_TAG_TYPE(shared_ptr); + MAKE_TAG_TYPE(derived); + MAKE_TAG_TYPE(TF4); + MAKE_TAG_TYPE(TF5); + MAKE_TAG_TYPE(null_ptr); + MAKE_TAG_TYPE(null_unique_ptr); + MAKE_TAG_TYPE(null_shared_ptr); + MAKE_TAG_TYPE(base); + MAKE_TAG_TYPE(invalid_base); + MAKE_TAG_TYPE(alias); + MAKE_TAG_TYPE(unaliasable); + MAKE_TAG_TYPE(mixed); + + // test_init_factory_basic, test_bad_type + py::class_(m, "TestFactory1") + .def(py::init([](unique_ptr_tag, int v) { return TestFactoryHelper::construct1(v); })) + .def(py::init(&TestFactoryHelper::construct1_string)) // raw function pointer + .def(py::init([](pointer_tag) { return TestFactoryHelper::construct1(); })) + .def(py::init( + [](py::handle, int v, py::handle) { return TestFactoryHelper::construct1(v); })) + .def_readwrite("value", &TestFactory1::value); + py::class_(m, "TestFactory2") + .def(py::init([](pointer_tag, int v) { return TestFactoryHelper::construct2(v); })) + .def(py::init([](unique_ptr_tag, std::string v) { + return TestFactoryHelper::construct2(std::move(v)); + })) + .def(py::init([](move_tag) { return TestFactoryHelper::construct2(); })) + .def_readwrite("value", &TestFactory2::value); + + // Stateful & reused: + int c = 1; + auto c4a = [c](pointer_tag, TF4_tag, int a) { + (void) c; + return new TestFactory4(a); + }; + + // test_init_factory_basic, test_init_factory_casting + py::class_> pyTestFactory3(m, "TestFactory3"); + pyTestFactory3 + .def(py::init([](pointer_tag, int v) { return TestFactoryHelper::construct3(v); })) + .def(py::init([](shared_ptr_tag) { return TestFactoryHelper::construct3(); })); + ignoreOldStyleInitWarnings([&pyTestFactory3]() { + pyTestFactory3.def("__init__", [](TestFactory3 &self, std::string v) { + new (&self) TestFactory3(std::move(v)); + }); // placement-new ctor + }); + pyTestFactory3 + // factories returning a derived type: + .def(py::init(c4a)) // derived ptr + .def(py::init([](pointer_tag, TF5_tag, int a) { return new TestFactory5(a); })) + // derived shared ptr: + .def(py::init( + [](shared_ptr_tag, TF4_tag, int a) { return std::make_shared(a); })) + .def(py::init( + [](shared_ptr_tag, TF5_tag, int a) { return std::make_shared(a); })) + + // Returns nullptr: + .def(py::init([](null_ptr_tag) { return (TestFactory3 *) nullptr; })) + .def(py::init([](null_unique_ptr_tag) { return std::unique_ptr(); })) + .def(py::init([](null_shared_ptr_tag) { return std::shared_ptr(); })) + + .def_readwrite("value", &TestFactory3::value); + + // test_init_factory_casting + py::class_>(m, "TestFactory4") + .def(py::init(c4a)) // pointer + ; + + // Doesn't need to be registered, but registering makes getting ConstructorStats easier: + py::class_>(m, "TestFactory5"); + + // test_init_factory_alias + // Alias testing + py::class_(m, "TestFactory6") + .def(py::init([](base_tag, int i) { return TestFactory6(i); })) + .def(py::init([](alias_tag, int i) { return PyTF6(i); })) + .def(py::init([](alias_tag, std::string s) { return PyTF6(std::move(s)); })) + .def(py::init([](alias_tag, pointer_tag, int i) { return new PyTF6(i); })) + .def(py::init([](base_tag, pointer_tag, int i) { return new TestFactory6(i); })) + .def(py::init( + [](base_tag, alias_tag, pointer_tag, int i) { return (TestFactory6 *) new PyTF6(i); })) + + .def("get", &TestFactory6::get) + .def("has_alias", &TestFactory6::has_alias) + + .def_static( + "get_cstats", &ConstructorStats::get, py::return_value_policy::reference) + .def_static( + "get_alias_cstats", &ConstructorStats::get, py::return_value_policy::reference); + + // test_init_factory_dual + // Separate alias constructor testing + py::class_>(m, "TestFactory7") + .def(py::init([](int i) { return TestFactory7(i); }, [](int i) { return PyTF7(i); })) + .def(py::init([](pointer_tag, int i) { return new TestFactory7(i); }, + [](pointer_tag, int i) { return new PyTF7(i); })) + .def(py::init([](mixed_tag, int i) { return new TestFactory7(i); }, + [](mixed_tag, int i) { return PyTF7(i); })) + .def(py::init([](mixed_tag, const std::string &s) { return TestFactory7((int) s.size()); }, + [](mixed_tag, const std::string &s) { return new PyTF7((int) s.size()); })) + .def(py::init([](base_tag, pointer_tag, int i) { return new TestFactory7(i); }, + [](base_tag, pointer_tag, int i) { return (TestFactory7 *) new PyTF7(i); })) + .def(py::init([](alias_tag, pointer_tag, int i) { return new PyTF7(i); }, + [](alias_tag, pointer_tag, int i) { return new PyTF7(10 * i); })) + .def(py::init( + [](shared_ptr_tag, base_tag, int i) { return std::make_shared(i); }, + [](shared_ptr_tag, base_tag, int i) { + auto *p = new PyTF7(i); + return std::shared_ptr(p); + })) + .def(py::init([](shared_ptr_tag, + invalid_base_tag, + int i) { return std::make_shared(i); }, + [](shared_ptr_tag, invalid_base_tag, int i) { + return std::make_shared(i); + })) // <-- invalid alias factory + + .def("get", &TestFactory7::get) + .def("has_alias", &TestFactory7::has_alias) + + .def_static( + "get_cstats", &ConstructorStats::get, py::return_value_policy::reference) + .def_static( + "get_alias_cstats", &ConstructorStats::get, py::return_value_policy::reference); + + // test_placement_new_alternative + // Class with a custom new operator but *without* a placement new operator (issue #948) + class NoPlacementNew { + public: + explicit NoPlacementNew(int i) : i(i) {} + static void *operator new(std::size_t s) { + auto *p = ::operator new(s); + py::print("operator new called, returning", reinterpret_cast(p)); + return p; + } + static void operator delete(void *p) { + py::print("operator delete called on", reinterpret_cast(p)); + ::operator delete(p); + } + int i; + }; + // As of 2.2, `py::init` no longer requires placement new + py::class_(m, "NoPlacementNew") + .def(py::init()) + .def(py::init([]() { return new NoPlacementNew(100); })) + .def_readwrite("i", &NoPlacementNew::i); + + // test_reallocations + // Class that has verbose operator_new/operator_delete calls + struct NoisyAlloc { + NoisyAlloc(const NoisyAlloc &) = default; + explicit NoisyAlloc(int i) { py::print(py::str("NoisyAlloc(int {})").format(i)); } + explicit NoisyAlloc(double d) { py::print(py::str("NoisyAlloc(double {})").format(d)); } + ~NoisyAlloc() { py::print("~NoisyAlloc()"); } + + static void *operator new(size_t s) { + py::print("noisy new"); + return ::operator new(s); + } + static void *operator new(size_t, void *p) { + py::print("noisy placement new"); + return p; + } + static void operator delete(void *p) noexcept { + py::print("noisy delete"); + ::operator delete(p); + } + static void operator delete(void *p, size_t) { + py::print("noisy delete size"); + ::operator delete(p); + } + static void operator delete(void *, void *) { py::print("noisy placement delete"); } + }; + + py::class_ pyNoisyAlloc(m, "NoisyAlloc"); + // Since these overloads have the same number of arguments, the dispatcher will try each of + // them until the arguments convert. Thus we can get a pre-allocation here when passing a + // single non-integer: + ignoreOldStyleInitWarnings([&pyNoisyAlloc]() { + pyNoisyAlloc.def("__init__", [](NoisyAlloc *a, int i) { + new (a) NoisyAlloc(i); + }); // Regular constructor, runs first, requires preallocation + }); + + pyNoisyAlloc.def(py::init([](double d) { return new NoisyAlloc(d); })); + + // The two-argument version: first the factory pointer overload. + pyNoisyAlloc.def(py::init([](int i, int) { return new NoisyAlloc(i); })); + // Return-by-value: + pyNoisyAlloc.def(py::init([](double d, int) { return NoisyAlloc(d); })); + // Old-style placement new init; requires preallocation + ignoreOldStyleInitWarnings([&pyNoisyAlloc]() { + pyNoisyAlloc.def("__init__", + [](NoisyAlloc &a, double d, double) { new (&a) NoisyAlloc(d); }); + }); + // Requires deallocation of previous overload preallocated value: + pyNoisyAlloc.def(py::init([](int i, double) { return new NoisyAlloc(i); })); + // Regular again: requires yet another preallocation + ignoreOldStyleInitWarnings([&pyNoisyAlloc]() { + pyNoisyAlloc.def( + "__init__", [](NoisyAlloc &a, int i, const std::string &) { new (&a) NoisyAlloc(i); }); + }); + + // static_assert testing (the following def's should all fail with appropriate compilation + // errors): +#if 0 + struct BadF1Base {}; + struct BadF1 : BadF1Base {}; + struct PyBadF1 : BadF1 {}; + py::class_> bf1(m, "BadF1"); + // wrapped factory function must return a compatible pointer, holder, or value + bf1.def(py::init([]() { return 3; })); + // incompatible factory function pointer return type + bf1.def(py::init([]() { static int three = 3; return &three; })); + // incompatible factory function std::shared_ptr return type: cannot convert shared_ptr to holder + // (non-polymorphic base) + bf1.def(py::init([]() { return std::shared_ptr(new BadF1()); })); +#endif +} diff --git a/external_libraries/pybind11/tests/test_factory_constructors.py b/external_libraries/pybind11/tests/test_factory_constructors.py new file mode 100644 index 00000000..c6ae98c7 --- /dev/null +++ b/external_libraries/pybind11/tests/test_factory_constructors.py @@ -0,0 +1,531 @@ +from __future__ import annotations + +import re + +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 + + +@pytest.mark.skipif("env.GRAALPY", reason="Cannot reliably trigger GC") +def test_init_factory_basic(): + """Tests py::init_factory() wrapper around various ways of returning the object""" + + cstats = [ + ConstructorStats.get(c) + for c in [m.TestFactory1, m.TestFactory2, m.TestFactory3] + ] + cstats[0].alive() # force gc + n_inst = ConstructorStats.detail_reg_inst() + + x1 = m.TestFactory1(tag.unique_ptr, 3) + assert x1.value == "3" + y1 = m.TestFactory1(tag.pointer) + assert y1.value == "(empty)" + z1 = m.TestFactory1("hi!") + assert z1.value == "hi!" + + assert ConstructorStats.detail_reg_inst() == n_inst + 3 + + x2 = m.TestFactory2(tag.move) + assert x2.value == "(empty2)" + y2 = m.TestFactory2(tag.pointer, 7) + assert y2.value == "7" + z2 = m.TestFactory2(tag.unique_ptr, "hi again") + assert z2.value == "hi again" + + assert ConstructorStats.detail_reg_inst() == n_inst + 6 + + x3 = m.TestFactory3(tag.shared_ptr) + assert x3.value == "(empty3)" + y3 = m.TestFactory3(tag.pointer, 42) + assert y3.value == "42" + z3 = m.TestFactory3("bye") + assert z3.value == "bye" + + for null_ptr_kind in [tag.null_ptr, tag.null_unique_ptr, tag.null_shared_ptr]: + with pytest.raises(TypeError) as excinfo: + m.TestFactory3(null_ptr_kind) + assert ( + str(excinfo.value) == "pybind11::init(): factory function returned nullptr" + ) + + assert [i.alive() for i in cstats] == [3, 3, 3] + assert ConstructorStats.detail_reg_inst() == n_inst + 9 + + del x1, y2, y3, z3 + assert [i.alive() for i in cstats] == [2, 2, 1] + assert ConstructorStats.detail_reg_inst() == n_inst + 5 + del x2, x3, y1, z1, z2 + assert [i.alive() for i in cstats] == [0, 0, 0] + assert ConstructorStats.detail_reg_inst() == n_inst + + assert [i.values() for i in cstats] == [ + ["3", "hi!"], + ["7", "hi again"], + ["42", "bye"], + ] + assert [i.default_constructions for i in cstats] == [1, 1, 1] + + +def test_init_factory_signature(msg): + with pytest.raises(TypeError) as excinfo: + m.TestFactory1("invalid", "constructor", "arguments") + assert ( + msg(excinfo.value) + == """ + __init__(): incompatible constructor arguments. The following argument types are supported: + 1. m.factory_constructors.TestFactory1(arg0: m.factory_constructors.tag.unique_ptr_tag, arg1: typing.SupportsInt | typing.SupportsIndex) + 2. m.factory_constructors.TestFactory1(arg0: str) + 3. m.factory_constructors.TestFactory1(arg0: m.factory_constructors.tag.pointer_tag) + 4. m.factory_constructors.TestFactory1(arg0: object, arg1: typing.SupportsInt | typing.SupportsIndex, arg2: object) + + Invoked with: 'invalid', 'constructor', 'arguments' + """ + ) + + assert ( + msg(m.TestFactory1.__init__.__doc__) + == """ + __init__(*args, **kwargs) + Overloaded function. + + 1. __init__(self: m.factory_constructors.TestFactory1, arg0: m.factory_constructors.tag.unique_ptr_tag, arg1: typing.SupportsInt | typing.SupportsIndex) -> None + + 2. __init__(self: m.factory_constructors.TestFactory1, arg0: str) -> None + + 3. __init__(self: m.factory_constructors.TestFactory1, arg0: m.factory_constructors.tag.pointer_tag) -> None + + 4. __init__(self: m.factory_constructors.TestFactory1, arg0: object, arg1: typing.SupportsInt | typing.SupportsIndex, arg2: object) -> None + """ + ) + + +@pytest.mark.skipif("env.GRAALPY", reason="Cannot reliably trigger GC") +def test_init_factory_casting(): + """Tests py::init_factory() wrapper with various upcasting and downcasting returns""" + + cstats = [ + ConstructorStats.get(c) + for c in [m.TestFactory3, m.TestFactory4, m.TestFactory5] + ] + cstats[0].alive() # force gc + n_inst = ConstructorStats.detail_reg_inst() + + # Construction from derived references: + a = m.TestFactory3(tag.pointer, tag.TF4, 4) + assert a.value == "4" + b = m.TestFactory3(tag.shared_ptr, tag.TF4, 5) + assert b.value == "5" + c = m.TestFactory3(tag.pointer, tag.TF5, 6) + assert c.value == "6" + d = m.TestFactory3(tag.shared_ptr, tag.TF5, 7) + assert d.value == "7" + + assert ConstructorStats.detail_reg_inst() == n_inst + 4 + + # Shared a lambda with TF3: + e = m.TestFactory4(tag.pointer, tag.TF4, 8) + assert e.value == "8" + + assert ConstructorStats.detail_reg_inst() == n_inst + 5 + assert [i.alive() for i in cstats] == [5, 3, 2] + + del a + assert [i.alive() for i in cstats] == [4, 2, 2] + assert ConstructorStats.detail_reg_inst() == n_inst + 4 + + del b, c, e + assert [i.alive() for i in cstats] == [1, 0, 1] + assert ConstructorStats.detail_reg_inst() == n_inst + 1 + + del d + assert [i.alive() for i in cstats] == [0, 0, 0] + assert ConstructorStats.detail_reg_inst() == n_inst + + assert [i.values() for i in cstats] == [ + ["4", "5", "6", "7", "8"], + ["4", "5", "8"], + ["6", "7"], + ] + + +@pytest.mark.skipif("env.GRAALPY", reason="Cannot reliably trigger GC") +def test_init_factory_alias(): + """Tests py::init_factory() wrapper with value conversions and alias types""" + + cstats = [m.TestFactory6.get_cstats(), m.TestFactory6.get_alias_cstats()] + cstats[0].alive() # force gc + n_inst = ConstructorStats.detail_reg_inst() + + a = m.TestFactory6(tag.base, 1) + assert a.get() == 1 + assert not a.has_alias() + b = m.TestFactory6(tag.alias, "hi there") + assert b.get() == 8 + assert b.has_alias() + c = m.TestFactory6(tag.alias, 3) + assert c.get() == 3 + assert c.has_alias() + d = m.TestFactory6(tag.alias, tag.pointer, 4) + assert d.get() == 4 + assert d.has_alias() + e = m.TestFactory6(tag.base, tag.pointer, 5) + assert e.get() == 5 + assert not e.has_alias() + f = m.TestFactory6(tag.base, tag.alias, tag.pointer, 6) + assert f.get() == 6 + assert f.has_alias() + + assert ConstructorStats.detail_reg_inst() == n_inst + 6 + assert [i.alive() for i in cstats] == [6, 4] + + del a, b, e + assert [i.alive() for i in cstats] == [3, 3] + assert ConstructorStats.detail_reg_inst() == n_inst + 3 + del f, c, d + assert [i.alive() for i in cstats] == [0, 0] + assert ConstructorStats.detail_reg_inst() == n_inst + + class MyTest(m.TestFactory6): + def __init__(self, *args): + m.TestFactory6.__init__(self, *args) + + def get(self): + return -5 + m.TestFactory6.get(self) + + # Return Class by value, moved into new alias: + z = MyTest(tag.base, 123) + assert z.get() == 118 + assert z.has_alias() + + # Return alias by value, moved into new alias: + y = MyTest(tag.alias, "why hello!") + assert y.get() == 5 + assert y.has_alias() + + # Return Class by pointer, moved into new alias then original destroyed: + x = MyTest(tag.base, tag.pointer, 47) + assert x.get() == 42 + assert x.has_alias() + + assert ConstructorStats.detail_reg_inst() == n_inst + 3 + assert [i.alive() for i in cstats] == [3, 3] + del x, y, z + assert [i.alive() for i in cstats] == [0, 0] + assert ConstructorStats.detail_reg_inst() == n_inst + + assert [i.values() for i in cstats] == [ + ["1", "8", "3", "4", "5", "6", "123", "10", "47"], + ["hi there", "3", "4", "6", "move", "123", "why hello!", "move", "47"], + ] + + +@pytest.mark.skipif("env.GRAALPY", reason="Cannot reliably trigger GC") +def test_init_factory_dual(): + """Tests init factory functions with dual main/alias factory functions""" + from pybind11_tests.factory_constructors import TestFactory7 + + cstats = [TestFactory7.get_cstats(), TestFactory7.get_alias_cstats()] + cstats[0].alive() # force gc + n_inst = ConstructorStats.detail_reg_inst() + + class PythFactory7(TestFactory7): + def get(self): + return 100 + TestFactory7.get(self) + + a1 = TestFactory7(1) + a2 = PythFactory7(2) + assert a1.get() == 1 + assert a2.get() == 102 + assert not a1.has_alias() + assert a2.has_alias() + + b1 = TestFactory7(tag.pointer, 3) + b2 = PythFactory7(tag.pointer, 4) + assert b1.get() == 3 + assert b2.get() == 104 + assert not b1.has_alias() + assert b2.has_alias() + + c1 = TestFactory7(tag.mixed, 5) + c2 = PythFactory7(tag.mixed, 6) + assert c1.get() == 5 + assert c2.get() == 106 + assert not c1.has_alias() + assert c2.has_alias() + + d1 = TestFactory7(tag.base, tag.pointer, 7) + d2 = PythFactory7(tag.base, tag.pointer, 8) + assert d1.get() == 7 + assert d2.get() == 108 + assert not d1.has_alias() + assert d2.has_alias() + + # Both return an alias; the second multiplies the value by 10: + e1 = TestFactory7(tag.alias, tag.pointer, 9) + e2 = PythFactory7(tag.alias, tag.pointer, 10) + assert e1.get() == 9 + assert e2.get() == 200 + assert e1.has_alias() + assert e2.has_alias() + + f1 = TestFactory7(tag.shared_ptr, tag.base, 11) + f2 = PythFactory7(tag.shared_ptr, tag.base, 12) + assert f1.get() == 11 + assert f2.get() == 112 + assert not f1.has_alias() + assert f2.has_alias() + + g1 = TestFactory7(tag.shared_ptr, tag.invalid_base, 13) + assert g1.get() == 13 + assert not g1.has_alias() + with pytest.raises(TypeError) as excinfo: + PythFactory7(tag.shared_ptr, tag.invalid_base, 14) + assert ( + str(excinfo.value) + == "pybind11::init(): construction failed: returned holder-wrapped instance is not an " + "alias instance" + ) + + assert [i.alive() for i in cstats] == [13, 7] + assert ConstructorStats.detail_reg_inst() == n_inst + 13 + + del a1, a2, b1, d1, e1, e2 + assert [i.alive() for i in cstats] == [7, 4] + assert ConstructorStats.detail_reg_inst() == n_inst + 7 + del b2, c1, c2, d2, f1, f2, g1 + assert [i.alive() for i in cstats] == [0, 0] + assert ConstructorStats.detail_reg_inst() == n_inst + + assert [i.values() for i in cstats] == [ + ["1", "2", "3", "4", "5", "6", "7", "8", "9", "100", "11", "12", "13", "14"], + ["2", "4", "6", "8", "9", "100", "12"], + ] + + +@pytest.mark.skipif("env.GRAALPY", reason="Cannot reliably trigger GC") +def test_no_placement_new(capture): + """Prior to 2.2, `py::init<...>` relied on the type supporting placement + new; this tests a class without placement new support.""" + with capture: + a = m.NoPlacementNew(123) + + found = re.search(r"^operator new called, returning (\d+)\n$", str(capture)) + assert found + assert a.i == 123 + with capture: + del a + pytest.gc_collect() + assert capture == "operator delete called on " + found.group(1) + + with capture: + b = m.NoPlacementNew() + + found = re.search(r"^operator new called, returning (\d+)\n$", str(capture)) + assert found + assert b.i == 100 + with capture: + del b + pytest.gc_collect() + assert capture == "operator delete called on " + found.group(1) + + +def test_multiple_inheritance(): + class MITest(m.TestFactory1, m.TestFactory2): + def __init__(self): + m.TestFactory1.__init__(self, tag.unique_ptr, 33) + m.TestFactory2.__init__(self, tag.move) + + a = MITest() + assert m.TestFactory1.value.fget(a) == "33" + assert m.TestFactory2.value.fget(a) == "(empty2)" + + +def create_and_destroy(*args): + a = m.NoisyAlloc(*args) + print("---") + del a + pytest.gc_collect() + + +def strip_comments(s): + return re.sub(r"\s+#.*", "", s) + + +@pytest.mark.skipif("env.GRAALPY", reason="Cannot reliably trigger GC") +def test_reallocation_a(capture, msg): + """When the constructor is overloaded, previous overloads can require a preallocated value. + This test makes sure that such preallocated values only happen when they might be necessary, + and that they are deallocated properly.""" + + pytest.gc_collect() + + with capture: + create_and_destroy(1) + assert ( + msg(capture) + == """ + noisy new + noisy placement new + NoisyAlloc(int 1) + --- + ~NoisyAlloc() + noisy delete + """ + ) + + +@pytest.mark.skipif("env.GRAALPY", reason="Cannot reliably trigger GC") +def test_reallocation_b(capture, msg): + with capture: + create_and_destroy(1.5) + assert msg(capture) == strip_comments( + """ + noisy new # allocation required to attempt first overload + noisy delete # have to dealloc before considering factory init overload + noisy new # pointer factory calling "new", part 1: allocation + NoisyAlloc(double 1.5) # ... part two, invoking constructor + --- + ~NoisyAlloc() # Destructor + noisy delete # operator delete + """ + ) + + +@pytest.mark.skipif("env.GRAALPY", reason="Cannot reliably trigger GC") +def test_reallocation_c(capture, msg): + with capture: + create_and_destroy(2, 3) + assert msg(capture) == strip_comments( + """ + noisy new # pointer factory calling "new", allocation + NoisyAlloc(int 2) # constructor + --- + ~NoisyAlloc() # Destructor + noisy delete # operator delete + """ + ) + + +@pytest.mark.skipif("env.GRAALPY", reason="Cannot reliably trigger GC") +def test_reallocation_d(capture, msg): + with capture: + create_and_destroy(2.5, 3) + assert msg(capture) == strip_comments( + """ + NoisyAlloc(double 2.5) # construction (local func variable: operator_new not called) + noisy new # return-by-value "new" part 1: allocation + ~NoisyAlloc() # moved-away local func variable destruction + --- + ~NoisyAlloc() # Destructor + noisy delete # operator delete + """ + ) + + +@pytest.mark.skipif("env.GRAALPY", reason="Cannot reliably trigger GC") +def test_reallocation_e(capture, msg): + with capture: + create_and_destroy(3.5, 4.5) + assert msg(capture) == strip_comments( + """ + noisy new # preallocation needed before invoking placement-new overload + noisy placement new # Placement new + NoisyAlloc(double 3.5) # construction + --- + ~NoisyAlloc() # Destructor + noisy delete # operator delete + """ + ) + + +@pytest.mark.skipif("env.GRAALPY", reason="Cannot reliably trigger GC") +def test_reallocation_f(capture, msg): + with capture: + create_and_destroy(4, 0.5) + assert msg(capture) == strip_comments( + """ + noisy new # preallocation needed before invoking placement-new overload + noisy delete # deallocation of preallocated storage + noisy new # Factory pointer allocation + NoisyAlloc(int 4) # factory pointer construction + --- + ~NoisyAlloc() # Destructor + noisy delete # operator delete + """ + ) + + +@pytest.mark.skipif("env.GRAALPY", reason="Cannot reliably trigger GC") +def test_reallocation_g(capture, msg): + with capture: + create_and_destroy(5, "hi") + assert msg(capture) == strip_comments( + """ + noisy new # preallocation needed before invoking first placement new + noisy delete # delete before considering new-style constructor + noisy new # preallocation for second placement new + noisy placement new # Placement new in the second placement new overload + NoisyAlloc(int 5) # construction + --- + ~NoisyAlloc() # Destructor + noisy delete # operator delete + """ + ) + + +def test_invalid_self(): + """Tests invocation of the pybind-registered base class with an invalid `self` argument.""" + + class NotPybindDerived: + pass + + # Attempts to initialize with an invalid type passed as `self`: + class BrokenTF1(m.TestFactory1): + def __init__(self, bad): + if bad == 1: + a = m.TestFactory2(tag.pointer, 1) + m.TestFactory1.__init__(a, tag.pointer) + elif bad == 2: + a = NotPybindDerived() + m.TestFactory1.__init__(a, tag.pointer) + + # Same as above, but for a class with an alias: + class BrokenTF6(m.TestFactory6): + def __init__(self, bad): + if bad == 0: + m.TestFactory6.__init__() + elif bad == 1: + a = m.TestFactory2(tag.pointer, 1) + m.TestFactory6.__init__(a, tag.base, 1) + elif bad == 2: + a = m.TestFactory2(tag.pointer, 1) + m.TestFactory6.__init__(a, tag.alias, 1) + elif bad == 3: + m.TestFactory6.__init__( + NotPybindDerived.__new__(NotPybindDerived), tag.base, 1 + ) + elif bad == 4: + m.TestFactory6.__init__( + NotPybindDerived.__new__(NotPybindDerived), tag.alias, 1 + ) + + for arg in (1, 2): + with pytest.raises(TypeError) as excinfo: + BrokenTF1(arg) + assert ( + str(excinfo.value) + == "__init__(self, ...) called with invalid or missing `self` argument" + ) + + for arg in (0, 1, 2, 3, 4): + with pytest.raises(TypeError) as excinfo: + BrokenTF6(arg) + assert ( + str(excinfo.value) + == "__init__(self, ...) called with invalid or missing `self` argument" + ) diff --git a/external_libraries/pybind11/tests/test_gil_scoped.cpp b/external_libraries/pybind11/tests/test_gil_scoped.cpp new file mode 100644 index 00000000..f136086e --- /dev/null +++ b/external_libraries/pybind11/tests/test_gil_scoped.cpp @@ -0,0 +1,144 @@ +/* + tests/test_gil_scoped.cpp -- acquire and release gil + + Copyright (c) 2017 Borja Zarco (Google LLC) + + All rights reserved. Use of this source code is governed by a + BSD-style license that can be found in the LICENSE file. +*/ + +#include + +#include "pybind11_tests.h" + +#include +#include + +#define CROSS_MODULE(Function) \ + auto cm = py::module_::import("cross_module_gil_utils"); \ + auto target = reinterpret_cast(PyLong_AsVoidPtr(cm.attr(Function).ptr())); + +class VirtClass { +public: + virtual ~VirtClass() = default; + VirtClass() = default; + VirtClass(const VirtClass &) = delete; + virtual void virtual_func() {} + virtual void pure_virtual_func() = 0; +}; + +class PyVirtClass : public VirtClass { + void virtual_func() override { PYBIND11_OVERRIDE(void, VirtClass, virtual_func, ); } + void pure_virtual_func() override { + PYBIND11_OVERRIDE_PURE(void, VirtClass, pure_virtual_func, ); + } +}; + +TEST_SUBMODULE(gil_scoped, m) { + m.attr("defined_THREAD_SANITIZER") = +#if defined(THREAD_SANITIZER) + true; +#else + false; +#endif + + m.def("intentional_deadlock", + []() { std::thread([]() { py::gil_scoped_acquire gil_acquired; }).join(); }); + + py::class_(m, "VirtClass") + .def(py::init<>()) + .def("virtual_func", &VirtClass::virtual_func) + .def("pure_virtual_func", &VirtClass::pure_virtual_func); + + m.def("test_callback_py_obj", [](py::object &func) { func(); }); + m.def("test_callback_std_func", [](const std::function &func) { func(); }); + m.def("test_callback_virtual_func", [](VirtClass &virt) { virt.virtual_func(); }); + m.def("test_callback_pure_virtual_func", [](VirtClass &virt) { virt.pure_virtual_func(); }); + m.def("test_cross_module_gil_released", []() { + CROSS_MODULE("gil_acquire_funcaddr") + py::gil_scoped_release gil_release; + target(); + }); + m.def("test_cross_module_gil_acquired", []() { + CROSS_MODULE("gil_acquire_funcaddr") + py::gil_scoped_acquire gil_acquire; + target(); + }); + m.def("test_cross_module_gil_inner_custom_released", []() { + CROSS_MODULE("gil_acquire_inner_custom_funcaddr") + py::gil_scoped_release gil_release; + target(); + }); + m.def("test_cross_module_gil_inner_custom_acquired", []() { + CROSS_MODULE("gil_acquire_inner_custom_funcaddr") + py::gil_scoped_acquire gil_acquire; + target(); + }); + m.def("test_cross_module_gil_inner_pybind11_released", []() { + CROSS_MODULE("gil_acquire_inner_pybind11_funcaddr") + py::gil_scoped_release gil_release; + target(); + }); + m.def("test_cross_module_gil_inner_pybind11_acquired", []() { + CROSS_MODULE("gil_acquire_inner_pybind11_funcaddr") + py::gil_scoped_acquire gil_acquire; + target(); + }); + m.def("test_cross_module_gil_nested_custom_released", []() { + CROSS_MODULE("gil_acquire_nested_custom_funcaddr") + py::gil_scoped_release gil_release; + target(); + }); + m.def("test_cross_module_gil_nested_custom_acquired", []() { + CROSS_MODULE("gil_acquire_nested_custom_funcaddr") + py::gil_scoped_acquire gil_acquire; + target(); + }); + m.def("test_cross_module_gil_nested_pybind11_released", []() { + CROSS_MODULE("gil_acquire_nested_pybind11_funcaddr") + py::gil_scoped_release gil_release; + target(); + }); + m.def("test_cross_module_gil_nested_pybind11_acquired", []() { + CROSS_MODULE("gil_acquire_nested_pybind11_funcaddr") + py::gil_scoped_acquire gil_acquire; + target(); + }); + m.def("test_release_acquire", [](const py::object &obj) { + py::gil_scoped_release gil_released; + py::gil_scoped_acquire gil_acquired; + return py::str(obj); + }); + m.def("test_nested_acquire", [](const py::object &obj) { + py::gil_scoped_release gil_released; + py::gil_scoped_acquire gil_acquired_outer; + py::gil_scoped_acquire gil_acquired_inner; + return py::str(obj); + }); + m.def("test_multi_acquire_release_cross_module", [](unsigned bits) { + py::set internals_ids; + internals_ids.add(PYBIND11_INTERNALS_ID); + { + py::gil_scoped_release gil_released; + auto thread_f = [bits, &internals_ids]() { + py::gil_scoped_acquire gil_acquired; + auto cm = py::module_::import("cross_module_gil_utils"); + auto target = reinterpret_cast( + PyLong_AsVoidPtr(cm.attr("gil_multi_acquire_release_funcaddr").ptr())); + std::string cm_internals_id = target(bits >> 3); + internals_ids.add(cm_internals_id); + }; + if ((bits & 0x1u) != 0u) { + thread_f(); + } + if ((bits & 0x2u) != 0u) { + std::thread non_python_thread(thread_f); + non_python_thread.join(); + } + if ((bits & 0x4u) != 0u) { + thread_f(); + } + } + return internals_ids; + }); +} diff --git a/external_libraries/pybind11/tests/test_gil_scoped.py b/external_libraries/pybind11/tests/test_gil_scoped.py new file mode 100644 index 00000000..fc998b0e --- /dev/null +++ b/external_libraries/pybind11/tests/test_gil_scoped.py @@ -0,0 +1,294 @@ +from __future__ import annotations + +import multiprocessing +import sys +import sysconfig +import threading +import time + +import pytest + +import env +from pybind11_tests import gil_scoped as m + +# Test collection seems to hold the gil +# These tests have rare flakes in nogil; since they +# are testing the gil, they are skipped at the moment. +skipif_not_free_threaded = pytest.mark.skipif( + sysconfig.get_config_var("Py_GIL_DISABLED"), + reason="Flaky without the GIL", +) + + +class ExtendedVirtClass(m.VirtClass): + def virtual_func(self): + pass + + def pure_virtual_func(self): + pass + + +def test_callback_py_obj(): + m.test_callback_py_obj(lambda: None) + + +def test_callback_std_func(): + m.test_callback_std_func(lambda: None) + + +def test_callback_virtual_func(): + extended = ExtendedVirtClass() + m.test_callback_virtual_func(extended) + + +def test_callback_pure_virtual_func(): + extended = ExtendedVirtClass() + m.test_callback_pure_virtual_func(extended) + + +def test_cross_module_gil_released(): + """Makes sure that the GIL can be acquired by another module from a GIL-released state.""" + m.test_cross_module_gil_released() # Should not raise a SIGSEGV + + +def test_cross_module_gil_acquired(): + """Makes sure that the GIL can be acquired by another module from a GIL-acquired state.""" + m.test_cross_module_gil_acquired() # Should not raise a SIGSEGV + + +def test_cross_module_gil_inner_custom_released(): + """Makes sure that the GIL can be acquired/released by another module + from a GIL-released state using custom locking logic.""" + m.test_cross_module_gil_inner_custom_released() + + +def test_cross_module_gil_inner_custom_acquired(): + """Makes sure that the GIL can be acquired/acquired by another module + from a GIL-acquired state using custom locking logic.""" + m.test_cross_module_gil_inner_custom_acquired() + + +def test_cross_module_gil_inner_pybind11_released(): + """Makes sure that the GIL can be acquired/released by another module + from a GIL-released state using pybind11 locking logic.""" + m.test_cross_module_gil_inner_pybind11_released() + + +def test_cross_module_gil_inner_pybind11_acquired(): + """Makes sure that the GIL can be acquired/acquired by another module + from a GIL-acquired state using pybind11 locking logic.""" + m.test_cross_module_gil_inner_pybind11_acquired() + + +@pytest.mark.skipif(sys.platform.startswith("emscripten"), reason="Requires threads") +def test_cross_module_gil_nested_custom_released(): + """Makes sure that the GIL can be nested acquired/released by another module + from a GIL-released state using custom locking logic.""" + m.test_cross_module_gil_nested_custom_released() + + +@pytest.mark.skipif(sys.platform.startswith("emscripten"), reason="Requires threads") +def test_cross_module_gil_nested_custom_acquired(): + """Makes sure that the GIL can be nested acquired/acquired by another module + from a GIL-acquired state using custom locking logic.""" + m.test_cross_module_gil_nested_custom_acquired() + + +@pytest.mark.skipif(sys.platform.startswith("emscripten"), reason="Requires threads") +def test_cross_module_gil_nested_pybind11_released(): + """Makes sure that the GIL can be nested acquired/released by another module + from a GIL-released state using pybind11 locking logic.""" + m.test_cross_module_gil_nested_pybind11_released() + + +@pytest.mark.skipif(sys.platform.startswith("emscripten"), reason="Requires threads") +def test_cross_module_gil_nested_pybind11_acquired(): + """Makes sure that the GIL can be nested acquired/acquired by another module + from a GIL-acquired state using pybind11 locking logic.""" + m.test_cross_module_gil_nested_pybind11_acquired() + + +def test_release_acquire(): + assert m.test_release_acquire(0xAB) == "171" + + +def test_nested_acquire(): + assert m.test_nested_acquire(0xAB) == "171" + + +@pytest.mark.skipif(sys.platform.startswith("emscripten"), reason="Requires threads") +@pytest.mark.skipif( + env.GRAALPY and sys.platform == "darwin", + reason="Transiently crashes on GraalPy on OS X", +) +def test_multi_acquire_release_cross_module(): + for bits in range(16 * 8): + internals_ids = m.test_multi_acquire_release_cross_module(bits) + assert len(internals_ids) == 2 if bits % 8 else 1 + + +# Intentionally putting human review in the loop here, to guard against accidents. +VARS_BEFORE_ALL_BASIC_TESTS = dict(vars()) # Make a copy of the dict (critical). +ALL_BASIC_TESTS = ( + test_callback_py_obj, + test_callback_std_func, + test_callback_virtual_func, + test_callback_pure_virtual_func, + test_cross_module_gil_released, + test_cross_module_gil_acquired, + test_cross_module_gil_inner_custom_released, + test_cross_module_gil_inner_custom_acquired, + test_cross_module_gil_inner_pybind11_released, + test_cross_module_gil_inner_pybind11_acquired, + test_cross_module_gil_nested_custom_released, + test_cross_module_gil_nested_custom_acquired, + test_cross_module_gil_nested_pybind11_released, + test_cross_module_gil_nested_pybind11_acquired, + test_release_acquire, + test_nested_acquire, + test_multi_acquire_release_cross_module, +) + + +def test_all_basic_tests_completeness(): + num_found = 0 + for key, value in VARS_BEFORE_ALL_BASIC_TESTS.items(): + if not key.startswith("test_"): + continue + assert value in ALL_BASIC_TESTS + num_found += 1 + assert len(ALL_BASIC_TESTS) == num_found + + +def _intentional_deadlock(): + m.intentional_deadlock() + + +ALL_BASIC_TESTS_PLUS_INTENTIONAL_DEADLOCK = (*ALL_BASIC_TESTS, _intentional_deadlock) + + +def _run_in_process(target, *args, **kwargs): + if env.ANDROID or env.IOS or sys.platform.startswith("emscripten"): + pytest.skip("Requires subprocess support") + + test_fn = target if len(args) == 0 else args[0] + # Do not need to wait much, 10s should be more than enough. + timeout = 0.1 if test_fn is _intentional_deadlock else 10 + process = multiprocessing.Process(target=target, args=args, kwargs=kwargs) + process.daemon = True + try: + t_start = time.time() + process.start() + if timeout >= 100: # For debugging. + print( + "\nprocess.pid STARTED", process.pid, (sys.argv, target, args, kwargs) + ) + print(f"COPY-PASTE-THIS: gdb {sys.argv[0]} -p {process.pid}", flush=True) + process.join(timeout=timeout) + if timeout >= 100: + print("\nprocess.pid JOINED", process.pid, flush=True) + t_delta = time.time() - t_start + if process.exitcode == 66 and m.defined_THREAD_SANITIZER: # Issue #2754 + # WOULD-BE-NICE-TO-HAVE: Check that the message below is actually in the output. + # Maybe this could work: + # https://gist.github.com/alexeygrigorev/01ce847f2e721b513b42ea4a6c96905e + pytest.skip( + "ThreadSanitizer: starting new threads after multi-threaded fork is not supported." + ) + elif test_fn is _intentional_deadlock: + assert process.exitcode is None + return 0 + + if process.exitcode is None: + assert t_delta > 0.9 * timeout + msg = "DEADLOCK, most likely, exactly what this test is meant to detect." + soabi = sysconfig.get_config_var("SOABI") + if env.WIN and env.PYPY: + pytest.xfail(f"[TEST-GIL-SCOPED] {soabi} PyPy: " + msg) + if env.MACOS: + if not env.sys_is_gil_enabled(): + pytest.xfail(f"[TEST-GIL-SCOPED] {soabi} with GIL disabled: " + msg) + if env.PY_GIL_DISABLED: + pytest.xfail(f"[TEST-GIL-SCOPED] {soabi}: " + msg) + raise RuntimeError(msg) + return process.exitcode + finally: + if process.is_alive(): + process.terminate() + + +def _run_in_threads(test_fn, num_threads, parallel): + threads = [] + for _ in range(num_threads): + thread = threading.Thread(target=test_fn) + thread.daemon = True + thread.start() + if parallel: + threads.append(thread) + else: + thread.join() + for thread in threads: + thread.join() + + +@pytest.mark.skipif(sys.platform.startswith("emscripten"), reason="Requires threads") +@pytest.mark.parametrize("test_fn", ALL_BASIC_TESTS_PLUS_INTENTIONAL_DEADLOCK) +@pytest.mark.skipif( + "env.GRAALPY", + reason="GraalPy transiently complains about unfinished threads at process exit", +) +def test_run_in_process_one_thread(test_fn): + """Makes sure there is no GIL deadlock when running in a thread. + + It runs in a separate process to be able to stop and assert if it deadlocks. + """ + assert _run_in_process(_run_in_threads, test_fn, num_threads=1, parallel=False) == 0 + + +@skipif_not_free_threaded +@pytest.mark.skipif(sys.platform.startswith("emscripten"), reason="Requires threads") +@pytest.mark.parametrize("test_fn", ALL_BASIC_TESTS_PLUS_INTENTIONAL_DEADLOCK) +@pytest.mark.skipif( + "env.GRAALPY", + reason="GraalPy transiently complains about unfinished threads at process exit", +) +def test_run_in_process_multiple_threads_parallel(test_fn): + """Makes sure there is no GIL deadlock when running in a thread multiple times in parallel. + + It runs in a separate process to be able to stop and assert if it deadlocks. + """ + assert _run_in_process(_run_in_threads, test_fn, num_threads=8, parallel=True) == 0 + + +@pytest.mark.skipif(sys.platform.startswith("emscripten"), reason="Requires threads") +@pytest.mark.parametrize("test_fn", ALL_BASIC_TESTS_PLUS_INTENTIONAL_DEADLOCK) +@pytest.mark.skipif( + "env.GRAALPY", + reason="GraalPy transiently complains about unfinished threads at process exit", +) +def test_run_in_process_multiple_threads_sequential(test_fn): + """Makes sure there is no GIL deadlock when running in a thread multiple times sequentially. + + It runs in a separate process to be able to stop and assert if it deadlocks. + """ + assert _run_in_process(_run_in_threads, test_fn, num_threads=8, parallel=False) == 0 + + +@pytest.mark.skipif(sys.platform.startswith("emscripten"), reason="Requires threads") +@pytest.mark.parametrize( + "test_fn", + [ + *ALL_BASIC_TESTS, + pytest.param(_intentional_deadlock, marks=skipif_not_free_threaded), + ], +) +@pytest.mark.skipif( + "env.GRAALPY", + reason="GraalPy transiently complains about unfinished threads at process exit", +) +def test_run_in_process_direct(test_fn): + """Makes sure there is no GIL deadlock when using processes. + + This test is for completion, but it was never an issue. + """ + assert _run_in_process(test_fn) == 0 diff --git a/external_libraries/pybind11/tests/test_iostream.cpp b/external_libraries/pybind11/tests/test_iostream.cpp new file mode 100644 index 00000000..7484e734 --- /dev/null +++ b/external_libraries/pybind11/tests/test_iostream.cpp @@ -0,0 +1,162 @@ +/* + tests/test_iostream.cpp -- Usage of scoped_output_redirect + + Copyright (c) 2017 Henry F. Schreiner + + All rights reserved. Use of this source code is governed by a + BSD-style license that can be found in the LICENSE file. +*/ + +#include + +#include "pybind11_tests.h" + +#include +#include +#include +#include +#include + +void noisy_function(const std::string &msg, bool flush) { + + std::cout << msg; + if (flush) { + std::cout << std::flush; + } +} + +void noisy_funct_dual(const std::string &msg, const std::string &emsg) { + std::cout << msg; + std::cerr << emsg; +} + +// object to manage C++ thread +// simply repeatedly write to std::cerr until stopped +// redirect is called at some point to test the safety of scoped_estream_redirect +struct TestThread { + TestThread() : stop_{false} { + auto thread_f = [this] { + static std::mutex cout_mutex; + while (!stop_) { + { + // #HelpAppreciated: Work on iostream.h thread safety. + // Without this lock, the clang ThreadSanitizer (tsan) reliably reports a + // data race, and this test is predictably flakey on Windows. + // For more background see the discussion under + // https://github.com/pybind/pybind11/pull/2982 and + // https://github.com/pybind/pybind11/pull/2995. + const std::lock_guard lock(cout_mutex); + std::cout << "x" << std::flush; + } + std::this_thread::sleep_for(std::chrono::microseconds(50)); + } + }; + t_ = new std::thread(std::move(thread_f)); + } + + ~TestThread() { delete t_; } + + void stop() { stop_ = true; } + + void join() const { + py::gil_scoped_release gil_lock; + t_->join(); + } + + void sleep() { + py::gil_scoped_release gil_lock; + std::this_thread::sleep_for(std::chrono::milliseconds(50)); + } + + std::thread *t_{nullptr}; + std::atomic stop_; +}; + +TEST_SUBMODULE(iostream, m) { + + add_ostream_redirect(m); + + // test_evals + + m.def("captured_output_default", [](const std::string &msg) { + py::scoped_ostream_redirect redir; + std::cout << msg << std::flush; + }); + + m.def("captured_output", [](const std::string &msg) { + py::scoped_ostream_redirect redir(std::cout, py::module_::import("sys").attr("stdout")); + std::cout << msg << std::flush; + }); + + m.def("guard_output", + &noisy_function, + py::call_guard(), + py::arg("msg"), + py::arg("flush") = true); + + m.def("captured_err", [](const std::string &msg) { + py::scoped_ostream_redirect redir(std::cerr, py::module_::import("sys").attr("stderr")); + std::cerr << msg << std::flush; + }); + + m.def("noisy_function", &noisy_function, py::arg("msg"), py::arg("flush") = true); + + m.def("dual_guard", + &noisy_funct_dual, + py::call_guard(), + py::arg("msg"), + py::arg("emsg")); + + m.def("raw_output", [](const std::string &msg) { std::cout << msg << std::flush; }); + + m.def("raw_err", [](const std::string &msg) { std::cerr << msg << std::flush; }); + + m.def("captured_dual", [](const std::string &msg, const std::string &emsg) { + py::scoped_ostream_redirect redirout(std::cout, py::module_::import("sys").attr("stdout")); + py::scoped_ostream_redirect redirerr(std::cerr, py::module_::import("sys").attr("stderr")); + std::cout << msg << std::flush; + std::cerr << emsg << std::flush; + }); + + py::class_(m, "TestThread") + .def(py::init<>()) + .def("stop", &TestThread::stop) + .def("join", &TestThread::join) + .def("sleep", &TestThread::sleep); + + m.def("move_redirect_output", [](const std::string &msg_before, const std::string &msg_after) { + py::scoped_ostream_redirect redir1(std::cout, py::module_::import("sys").attr("stdout")); + std::cout << msg_before << std::flush; + py::scoped_ostream_redirect redir2(std::move(redir1)); + std::cout << msg_after << std::flush; + }); + + m.def("move_redirect_output_unflushed", + [](const std::string &msg_before, const std::string &msg_after) { + py::scoped_ostream_redirect redir1(std::cout, + py::module_::import("sys").attr("stdout")); + std::cout << msg_before; + py::scoped_ostream_redirect redir2(std::move(redir1)); + std::cout << msg_after << std::flush; + }); + + // Redirect a stream whose original rdbuf is nullptr, then move the redirect. + // Verifies that nullptr is correctly restored (not confused with a moved-from sentinel). + m.def("move_redirect_null_rdbuf", [](const std::string &msg) { + std::ostream os(nullptr); + py::scoped_ostream_redirect redir1(os, py::module_::import("sys").attr("stdout")); + os << msg << std::flush; + py::scoped_ostream_redirect redir2(std::move(redir1)); + os << msg << std::flush; + // After redir2 goes out of scope, os.rdbuf() should be restored to nullptr. + }); + + m.def("get_null_rdbuf_restored", [](const std::string &msg) -> bool { + std::ostream os(nullptr); + { + py::scoped_ostream_redirect redir(os, py::module_::import("sys").attr("stdout")); + os << msg << std::flush; + } + return os.rdbuf() == nullptr; + }); +} diff --git a/external_libraries/pybind11/tests/test_iostream.py b/external_libraries/pybind11/tests/test_iostream.py new file mode 100644 index 00000000..857e0b5f --- /dev/null +++ b/external_libraries/pybind11/tests/test_iostream.py @@ -0,0 +1,329 @@ +from __future__ import annotations + +import sys +from contextlib import redirect_stderr, redirect_stdout +from io import StringIO + +import pytest + +import env +from pybind11_tests import iostream as m + +if env.WIN: + wv_build = sys.getwindowsversion().build + skip_if_ge = 26100 + if wv_build >= skip_if_ge: + pytest.skip( + f"Windows build {wv_build} >= {skip_if_ge}:" + " Skipping iostream capture (redirection regression needs investigation)", + allow_module_level=True, + ) + + +def test_captured(capsys): + msg = "I've been redirected to Python, I hope!" + m.captured_output(msg) + stdout, stderr = capsys.readouterr() + assert stdout == msg + assert not stderr + + m.captured_output_default(msg) + stdout, stderr = capsys.readouterr() + assert stdout == msg + assert not stderr + + m.captured_err(msg) + stdout, stderr = capsys.readouterr() + assert not stdout + assert stderr == msg + + +def test_captured_large_string(capsys): + # Make this bigger than the buffer used on the C++ side: 1024 chars + msg = "I've been redirected to Python, I hope!" + msg = msg * (1024 // len(msg) + 1) + + m.captured_output_default(msg) + stdout, stderr = capsys.readouterr() + assert stdout == msg + assert not stderr + + +def test_captured_utf8_2byte_offset0(capsys): + msg = "\u07ff" + msg = "" + msg * (1024 // len(msg) + 1) + + m.captured_output_default(msg) + stdout, stderr = capsys.readouterr() + assert stdout == msg + assert not stderr + + +def test_captured_utf8_2byte_offset1(capsys): + msg = "\u07ff" + msg = "1" + msg * (1024 // len(msg) + 1) + + m.captured_output_default(msg) + stdout, stderr = capsys.readouterr() + assert stdout == msg + assert not stderr + + +def test_captured_utf8_3byte_offset0(capsys): + msg = "\uffff" + msg = "" + msg * (1024 // len(msg) + 1) + + m.captured_output_default(msg) + stdout, stderr = capsys.readouterr() + assert stdout == msg + assert not stderr + + +def test_captured_utf8_3byte_offset1(capsys): + msg = "\uffff" + msg = "1" + msg * (1024 // len(msg) + 1) + + m.captured_output_default(msg) + stdout, stderr = capsys.readouterr() + assert stdout == msg + assert not stderr + + +def test_captured_utf8_3byte_offset2(capsys): + msg = "\uffff" + msg = "12" + msg * (1024 // len(msg) + 1) + + m.captured_output_default(msg) + stdout, stderr = capsys.readouterr() + assert stdout == msg + assert not stderr + + +def test_captured_utf8_4byte_offset0(capsys): + msg = "\U0010ffff" + msg = "" + msg * (1024 // len(msg) + 1) + + m.captured_output_default(msg) + stdout, stderr = capsys.readouterr() + assert stdout == msg + assert not stderr + + +def test_captured_utf8_4byte_offset1(capsys): + msg = "\U0010ffff" + msg = "1" + msg * (1024 // len(msg) + 1) + + m.captured_output_default(msg) + stdout, stderr = capsys.readouterr() + assert stdout == msg + assert not stderr + + +def test_captured_utf8_4byte_offset2(capsys): + msg = "\U0010ffff" + msg = "12" + msg * (1024 // len(msg) + 1) + + m.captured_output_default(msg) + stdout, stderr = capsys.readouterr() + assert stdout == msg + assert not stderr + + +def test_captured_utf8_4byte_offset3(capsys): + msg = "\U0010ffff" + msg = "123" + msg * (1024 // len(msg) + 1) + + m.captured_output_default(msg) + stdout, stderr = capsys.readouterr() + assert stdout == msg + assert not stderr + + +def test_guard_capture(capsys): + msg = "I've been redirected to Python, I hope!" + m.guard_output(msg) + stdout, stderr = capsys.readouterr() + assert stdout == msg + assert not stderr + + +def test_series_captured(capture): + with capture: + m.captured_output("a") + m.captured_output("b") + assert capture == "ab" + + +def test_flush(capfd): + msg = "(not flushed)" + msg2 = "(flushed)" + + with m.ostream_redirect(): + m.noisy_function(msg, flush=False) + stdout, stderr = capfd.readouterr() + assert not stdout + + m.noisy_function(msg2, flush=True) + stdout, stderr = capfd.readouterr() + assert stdout == msg + msg2 + + m.noisy_function(msg, flush=False) + + stdout, stderr = capfd.readouterr() + assert stdout == msg + + +def test_not_captured(capfd): + msg = "Something that should not show up in log" + stream = StringIO() + with redirect_stdout(stream): + m.raw_output(msg) + stdout, stderr = capfd.readouterr() + assert stdout == msg + assert not stderr + assert not stream.getvalue() + + stream = StringIO() + with redirect_stdout(stream): + m.captured_output(msg) + stdout, stderr = capfd.readouterr() + assert not stdout + assert not stderr + assert stream.getvalue() == msg + + +def test_err(capfd): + msg = "Something that should not show up in log" + stream = StringIO() + with redirect_stderr(stream): + m.raw_err(msg) + stdout, stderr = capfd.readouterr() + assert not stdout + assert stderr == msg + assert not stream.getvalue() + + stream = StringIO() + with redirect_stderr(stream): + m.captured_err(msg) + stdout, stderr = capfd.readouterr() + assert not stdout + assert not stderr + assert stream.getvalue() == msg + + +def test_multi_captured(capfd): + stream = StringIO() + with redirect_stdout(stream): + m.captured_output("a") + m.raw_output("b") + m.captured_output("c") + m.raw_output("d") + stdout, stderr = capfd.readouterr() + assert stdout == "bd" + assert stream.getvalue() == "ac" + + +def test_dual(capsys): + m.captured_dual("a", "b") + stdout, stderr = capsys.readouterr() + assert stdout == "a" + assert stderr == "b" + + +def test_redirect(capfd): + msg = "Should not be in log!" + stream = StringIO() + with redirect_stdout(stream): + m.raw_output(msg) + stdout, stderr = capfd.readouterr() + assert stdout == msg + assert not stream.getvalue() + + stream = StringIO() + with redirect_stdout(stream), m.ostream_redirect(): + m.raw_output(msg) + stdout, stderr = capfd.readouterr() + assert not stdout + assert stream.getvalue() == msg + + stream = StringIO() + with redirect_stdout(stream): + m.raw_output(msg) + stdout, stderr = capfd.readouterr() + assert stdout == msg + assert not stream.getvalue() + + +def test_redirect_err(capfd): + msg = "StdOut" + msg2 = "StdErr" + + stream = StringIO() + with redirect_stderr(stream), m.ostream_redirect(stdout=False): + m.raw_output(msg) + m.raw_err(msg2) + stdout, stderr = capfd.readouterr() + assert stdout == msg + assert not stderr + assert stream.getvalue() == msg2 + + +def test_redirect_both(capfd): + msg = "StdOut" + msg2 = "StdErr" + + stream = StringIO() + stream2 = StringIO() + with redirect_stdout(stream), redirect_stderr(stream2), m.ostream_redirect(): + m.raw_output(msg) + m.raw_err(msg2) + stdout, stderr = capfd.readouterr() + assert not stdout + assert not stderr + assert stream.getvalue() == msg + assert stream2.getvalue() == msg2 + + +def test_move_redirect(capsys): + m.move_redirect_output("before_move", "after_move") + stdout, stderr = capsys.readouterr() + assert stdout == "before_moveafter_move" + assert not stderr + + +def test_move_redirect_unflushed(capsys): + m.move_redirect_output_unflushed("before_move", "after_move") + stdout, stderr = capsys.readouterr() + assert stdout == "before_moveafter_move" + assert not stderr + + +def test_move_redirect_null_rdbuf(capsys): + m.move_redirect_null_rdbuf("hello") + stdout, stderr = capsys.readouterr() + assert stdout == "hellohello" + assert not stderr + + +def test_null_rdbuf_restored(): + assert m.get_null_rdbuf_restored("test") + + +@pytest.mark.skipif(sys.platform.startswith("emscripten"), reason="Requires threads") +def test_threading(): + with m.ostream_redirect(stdout=True, stderr=False): + # start some threads + threads = [m.TestThread() for _j in range(20)] + + # give the threads some time to fail + threads[0].sleep() + + # stop all the threads + for t in threads: + t.stop() + + for t in threads: + t.join() + + # if a thread segfaults, we don't get here + assert True diff --git a/external_libraries/pybind11/tests/test_kwargs_and_defaults.cpp b/external_libraries/pybind11/tests/test_kwargs_and_defaults.cpp new file mode 100644 index 00000000..831947f1 --- /dev/null +++ b/external_libraries/pybind11/tests/test_kwargs_and_defaults.cpp @@ -0,0 +1,331 @@ +/* + tests/test_kwargs_and_defaults.cpp -- keyword arguments and default values + + Copyright (c) 2016 Wenzel Jakob + + All rights reserved. Use of this source code is governed by a + BSD-style license that can be found in the LICENSE file. +*/ + +#include + +#include "constructor_stats.h" +#include "pybind11_tests.h" + +#include + +TEST_SUBMODULE(kwargs_and_defaults, m) { + auto kw_func + = [](int x, int y) { return "x=" + std::to_string(x) + ", y=" + std::to_string(y); }; + + // test_named_arguments + m.def("kw_func0", kw_func); + m.def("kw_func1", kw_func, py::arg("x"), py::arg("y")); + m.def("kw_func2", kw_func, py::arg("x") = 100, py::arg("y") = 200); + m.def("kw_func3", [](const char *) {}, py::arg("data") = std::string("Hello world!")); + + /* A fancier default argument */ + std::vector list{{13, 17}}; + m.def( + "kw_func4", + [](const std::vector &entries) { + std::string ret = "{"; + for (int i : entries) { + ret += std::to_string(i) + " "; + } + ret.back() = '}'; + return ret; + }, + py::arg("myList") = list); + + m.def("kw_func_udl", kw_func, "x"_a, "y"_a = 300); + m.def("kw_func_udl_z", kw_func, "x"_a, "y"_a = 0); + + // test line breaks in default argument representation + struct CustomRepr { + std::string repr_string; + + explicit CustomRepr(const std::string &repr) : repr_string(repr) {} + + std::string __repr__() const { return repr_string; } + }; + + py::class_(m, "CustomRepr") + .def(py::init()) + .def("__repr__", &CustomRepr::__repr__); + + m.def( + "kw_lb_func0", + [](const CustomRepr &) {}, + py::arg("custom") = CustomRepr(" array([[A, B], [C, D]]) ")); + m.def( + "kw_lb_func1", + [](const CustomRepr &) {}, + py::arg("custom") = CustomRepr(" array([[A, B],\n[C, D]]) ")); + m.def( + "kw_lb_func2", + [](const CustomRepr &) {}, + py::arg("custom") = CustomRepr("\v\n array([[A, B], [C, D]])")); + m.def( + "kw_lb_func3", + [](const CustomRepr &) {}, + py::arg("custom") = CustomRepr("array([[A, B], [C, D]]) \f\n")); + m.def( + "kw_lb_func4", + [](const CustomRepr &) {}, + py::arg("custom") = CustomRepr("array([[A, B],\n\f\n[C, D]])")); + m.def( + "kw_lb_func5", + [](const CustomRepr &) {}, + py::arg("custom") = CustomRepr("array([[A, B],\r [C, D]])")); + m.def("kw_lb_func6", [](const CustomRepr &) {}, py::arg("custom") = CustomRepr(" \v\t ")); + m.def( + "kw_lb_func7", + [](const std::string &) {}, + py::arg("str_arg") = "First line.\n Second line."); + m.def("kw_lb_func8", [](const CustomRepr &) {}, py::arg("custom") = CustomRepr("")); + + // test_args_and_kwargs + m.def("args_function", [](py::args args) -> py::tuple { + PYBIND11_WARNING_PUSH + +#ifdef PYBIND11_DETECTED_CLANG_WITH_MISLEADING_CALL_STD_MOVE_EXPLICITLY_WARNING + PYBIND11_WARNING_DISABLE_CLANG("-Wreturn-std-move") +#endif + return args; + PYBIND11_WARNING_POP + }); + m.def("args_kwargs_function", [](const py::args &args, const py::kwargs &kwargs) { + return py::make_tuple(args, kwargs); + }); + + // test_mixed_args_and_kwargs + m.def("mixed_plus_args", + [](int i, double j, const py::args &args) { return py::make_tuple(i, j, args); }); + m.def("mixed_plus_kwargs", + [](int i, double j, const py::kwargs &kwargs) { return py::make_tuple(i, j, kwargs); }); + auto mixed_plus_both = [](int i, double j, const py::args &args, const py::kwargs &kwargs) { + return py::make_tuple(i, j, args, kwargs); + }; + m.def("mixed_plus_args_kwargs", mixed_plus_both); + + m.def("mixed_plus_args_kwargs_defaults", + mixed_plus_both, + py::arg("i") = 1, + py::arg("j") = 3.14159); + + m.def( + "args_kwonly", + [](int i, double j, const py::args &args, int z) { return py::make_tuple(i, j, args, z); }, + "i"_a, + "j"_a, + "z"_a); + m.def( + "args_kwonly_kwargs", + [](int i, double j, const py::args &args, int z, const py::kwargs &kwargs) { + return py::make_tuple(i, j, args, z, kwargs); + }, + "i"_a, + "j"_a, + py::kw_only{}, + "z"_a); + m.def( + "args_kwonly_kwargs_defaults", + [](int i, double j, const py::args &args, int z, const py::kwargs &kwargs) { + return py::make_tuple(i, j, args, z, kwargs); + }, + "i"_a = 1, + "j"_a = 3.14159, + "z"_a = 42); + m.def( + "args_kwonly_full_monty", + [](int h, int i, double j, const py::args &args, int z, const py::kwargs &kwargs) { + return py::make_tuple(h, i, j, args, z, kwargs); + }, + py::arg() = 1, + py::arg() = 2, + py::pos_only{}, + "j"_a = 3.14159, + "z"_a = 42); + +// test_args_refcount +// PyPy needs a garbage collection to get the reference count values to match CPython's behaviour +// PyPy uses the top few bits for REFCNT_FROM_PYPY & REFCNT_FROM_PYPY_LIGHT, so truncate +#ifdef PYPY_VERSION +# define GC_IF_NEEDED ConstructorStats::gc() +# define REFCNT(x) (int) Py_REFCNT(x) +#else +# define GC_IF_NEEDED +# define REFCNT(x) Py_REFCNT(x) +#endif + m.def("arg_refcount_h", [](py::handle h) { + GC_IF_NEEDED; + return h.ref_count(); + }); + m.def("arg_refcount_h", [](py::handle h, py::handle, py::handle) { + GC_IF_NEEDED; + return h.ref_count(); + }); + m.def("arg_refcount_o", [](const py::object &o) { + GC_IF_NEEDED; + return o.ref_count(); + }); + m.def("args_refcount", [](py::args a) { + GC_IF_NEEDED; + py::tuple t(a.size()); + for (size_t i = 0; i < a.size(); i++) { + // Use raw Python API here to avoid an extra, intermediate incref on the tuple item: + t[i] = REFCNT(PyTuple_GET_ITEM(a.ptr(), static_cast(i))); + } + return t; + }); + m.def("mixed_args_refcount", [](const py::object &o, py::args a) { + GC_IF_NEEDED; + py::tuple t(a.size() + 1); + t[0] = o.ref_count(); + for (size_t i = 0; i < a.size(); i++) { + // Use raw Python API here to avoid an extra, intermediate incref on the tuple item: + t[i + 1] = REFCNT(PyTuple_GET_ITEM(a.ptr(), static_cast(i))); + } + return t; + }); + + // pybind11 won't allow these to be bound: args and kwargs, if present, must be at the end. + // Uncomment these to test that the static_assert is indeed working: + // m.def("bad_args1", [](py::args, int) {}); + // m.def("bad_args2", [](py::kwargs, int) {}); + // m.def("bad_args3", [](py::kwargs, py::args) {}); + // m.def("bad_args4", [](py::args, int, py::kwargs) {}); + // m.def("bad_args5", [](py::args, py::kwargs, int) {}); + // m.def("bad_args6", [](py::args, py::args) {}); + // m.def("bad_args7", [](py::kwargs, py::kwargs) {}); + + // test_keyword_only_args + m.def( + "kw_only_all", + [](int i, int j) { return py::make_tuple(i, j); }, + py::kw_only(), + py::arg("i"), + py::arg("j")); + m.def( + "kw_only_some", + [](int i, int j, int k) { return py::make_tuple(i, j, k); }, + py::arg(), + py::kw_only(), + py::arg("j"), + py::arg("k")); + m.def( + "kw_only_with_defaults", + [](int i, int j, int k, int z) { return py::make_tuple(i, j, k, z); }, + py::arg() = 3, + "j"_a = 4, + py::kw_only(), + "k"_a = 5, + "z"_a); + m.def( + "kw_only_mixed", + [](int i, int j) { return py::make_tuple(i, j); }, + "i"_a, + py::kw_only(), + "j"_a); + m.def( + "kw_only_plus_more", + [](int i, int j, int k, const py::kwargs &kwargs) { + return py::make_tuple(i, j, k, kwargs); + }, + py::arg() /* positional */, + py::arg("j") = -1 /* both */, + py::kw_only(), + py::arg("k") /* kw-only */); + + m.def("register_invalid_kw_only", [](py::module_ m) { + m.def( + "bad_kw_only", + [](int i, int j) { return py::make_tuple(i, j); }, + py::kw_only(), + py::arg() /* invalid unnamed argument */, + "j"_a); + }); + + // test_positional_only_args + m.def( + "pos_only_all", + [](int i, int j) { return py::make_tuple(i, j); }, + py::arg("i"), + py::arg("j"), + py::pos_only()); + m.def( + "pos_only_mix", + [](int i, int j) { return py::make_tuple(i, j); }, + py::arg("i"), + py::pos_only(), + py::arg("j")); + m.def( + "pos_kw_only_mix", + [](int i, int j, int k) { return py::make_tuple(i, j, k); }, + py::arg("i"), + py::pos_only(), + py::arg("j"), + py::kw_only(), + py::arg("k")); + m.def( + "pos_only_def_mix", + [](int i, int j, int k) { return py::make_tuple(i, j, k); }, + py::arg("i"), + py::arg("j") = 2, + py::pos_only(), + py::arg("k") = 3); + + // These should fail to compile: +#ifdef PYBIND11_NEVER_DEFINED_EVER + // argument annotations are required when using kw_only + m.def("bad_kw_only1", [](int) {}, py::kw_only()); + // can't specify both `py::kw_only` and a `py::args` argument + m.def("bad_kw_only2", [](int i, py::args) {}, py::kw_only(), "i"_a); +#endif + + // test_function_signatures (along with most of the above) + struct KWClass { + void foo(int, float) {} + }; + py::class_(m, "KWClass") + .def("foo0", &KWClass::foo) + .def("foo1", &KWClass::foo, "x"_a, "y"_a); + + // Make sure a class (not an instance) can be used as a default argument. + // The return value doesn't matter, only that the module is importable. + m.def( + "class_default_argument", + [](py::object a) { return py::repr(std::move(a)); }, + "a"_a = py::module_::import("decimal").attr("Decimal")); + + // Initial implementation of kw_only was broken when used on a method/constructor before any + // other arguments + // https://github.com/pybind/pybind11/pull/3402#issuecomment-963341987 + + struct first_arg_kw_only {}; + py::class_(m, "first_arg_kw_only") + .def(py::init([](int) { return first_arg_kw_only(); }), + py::kw_only(), // This being before any args was broken + py::arg("i") = 0) + .def( + "method", + [](first_arg_kw_only &, int, int) {}, + py::kw_only(), // and likewise here + py::arg("i") = 1, + py::arg("j") = 2) + // Closely related: pos_only marker didn't show up properly when it was before any other + // arguments (although that is fairly useless in practice). + .def( + "pos_only", + [](first_arg_kw_only &, int, int) {}, + py::pos_only{}, + py::arg("i"), + py::arg("j")); + + // Test support for args and kwargs subclasses + m.def("args_kwargs_subclass_function", + [](const py::Args &args, const py::KWArgs &kwargs) { + return py::make_tuple(args, kwargs); + }); +} diff --git a/external_libraries/pybind11/tests/test_kwargs_and_defaults.py b/external_libraries/pybind11/tests/test_kwargs_and_defaults.py new file mode 100644 index 00000000..a7745d1e --- /dev/null +++ b/external_libraries/pybind11/tests/test_kwargs_and_defaults.py @@ -0,0 +1,473 @@ +from __future__ import annotations + +import pytest + +import env # noqa: F401 +from pybind11_tests import kwargs_and_defaults as m + + +def test_function_signatures(doc): + assert ( + doc(m.kw_func0) + == "kw_func0(arg0: typing.SupportsInt | typing.SupportsIndex, arg1: typing.SupportsInt | typing.SupportsIndex) -> str" + ) + assert ( + doc(m.kw_func1) + == "kw_func1(x: typing.SupportsInt | typing.SupportsIndex, y: typing.SupportsInt | typing.SupportsIndex) -> str" + ) + assert ( + doc(m.kw_func2) + == "kw_func2(x: typing.SupportsInt | typing.SupportsIndex = 100, y: typing.SupportsInt | typing.SupportsIndex = 200) -> str" + ) + assert doc(m.kw_func3) == "kw_func3(data: str = 'Hello world!') -> None" + assert ( + doc(m.kw_func4) + == "kw_func4(myList: collections.abc.Sequence[typing.SupportsInt | typing.SupportsIndex] = [13, 17]) -> str" + ) + assert ( + doc(m.kw_func_udl) + == "kw_func_udl(x: typing.SupportsInt | typing.SupportsIndex, y: typing.SupportsInt | typing.SupportsIndex = 300) -> str" + ) + assert ( + doc(m.kw_func_udl_z) + == "kw_func_udl_z(x: typing.SupportsInt | typing.SupportsIndex, y: typing.SupportsInt | typing.SupportsIndex = 0) -> str" + ) + assert doc(m.args_function) == "args_function(*args) -> tuple" + assert ( + doc(m.args_kwargs_function) + == "args_kwargs_function(*args, **kwargs) -> tuple[tuple, dict[str, typing.Any]]" + ) + assert ( + doc(m.args_kwargs_subclass_function) + == "args_kwargs_subclass_function(*args: str, **kwargs: str) -> tuple[tuple[str, ...], dict[str, str]]" + ) + assert ( + doc(m.KWClass.foo0) + == "foo0(self: m.kwargs_and_defaults.KWClass, arg0: typing.SupportsInt | typing.SupportsIndex, arg1: typing.SupportsFloat | typing.SupportsIndex) -> None" + ) + assert ( + doc(m.KWClass.foo1) + == "foo1(self: m.kwargs_and_defaults.KWClass, x: typing.SupportsInt | typing.SupportsIndex, y: typing.SupportsFloat | typing.SupportsIndex) -> None" + ) + assert ( + doc(m.kw_lb_func0) + == "kw_lb_func0(custom: m.kwargs_and_defaults.CustomRepr = array([[A, B], [C, D]])) -> None" + ) + assert ( + doc(m.kw_lb_func1) + == "kw_lb_func1(custom: m.kwargs_and_defaults.CustomRepr = array([[A, B], [C, D]])) -> None" + ) + assert ( + doc(m.kw_lb_func2) + == "kw_lb_func2(custom: m.kwargs_and_defaults.CustomRepr = array([[A, B], [C, D]])) -> None" + ) + assert ( + doc(m.kw_lb_func3) + == "kw_lb_func3(custom: m.kwargs_and_defaults.CustomRepr = array([[A, B], [C, D]])) -> None" + ) + assert ( + doc(m.kw_lb_func4) + == "kw_lb_func4(custom: m.kwargs_and_defaults.CustomRepr = array([[A, B], [C, D]])) -> None" + ) + assert ( + doc(m.kw_lb_func5) + == "kw_lb_func5(custom: m.kwargs_and_defaults.CustomRepr = array([[A, B], [C, D]])) -> None" + ) + assert ( + doc(m.kw_lb_func6) + == "kw_lb_func6(custom: m.kwargs_and_defaults.CustomRepr = ) -> None" + ) + assert ( + doc(m.kw_lb_func7) + == "kw_lb_func7(str_arg: str = 'First line.\\n Second line.') -> None" + ) + assert ( + doc(m.kw_lb_func8) + == "kw_lb_func8(custom: m.kwargs_and_defaults.CustomRepr = ) -> None" + ) + + +def test_named_arguments(): + assert m.kw_func0(5, 10) == "x=5, y=10" + + assert m.kw_func1(5, 10) == "x=5, y=10" + assert m.kw_func1(5, y=10) == "x=5, y=10" + assert m.kw_func1(y=10, x=5) == "x=5, y=10" + + assert m.kw_func2() == "x=100, y=200" + assert m.kw_func2(5) == "x=5, y=200" + assert m.kw_func2(x=5) == "x=5, y=200" + assert m.kw_func2(y=10) == "x=100, y=10" + assert m.kw_func2(5, 10) == "x=5, y=10" + assert m.kw_func2(x=5, y=10) == "x=5, y=10" + + with pytest.raises(TypeError) as excinfo: + # noinspection PyArgumentList + m.kw_func2(x=5, y=10, z=12) + assert excinfo.match( + r"(?s)^kw_func2\(\): incompatible.*Invoked with: kwargs: ((x=5|y=10|z=12)(, |$)){3}$" + ) + + assert m.kw_func4() == "{13 17}" + assert m.kw_func4(myList=[1, 2, 3]) == "{1 2 3}" + + assert m.kw_func_udl(x=5, y=10) == "x=5, y=10" + assert m.kw_func_udl_z(x=5) == "x=5, y=0" + + +def test_arg_and_kwargs(): + args = "arg1_value", "arg2_value", 3 + assert m.args_function(*args) == args + + args = "a1", "a2" + kwargs = {"arg3": "a3", "arg4": 4} + assert m.args_kwargs_function(*args, **kwargs) == (args, kwargs) + assert m.args_kwargs_subclass_function(*args, **kwargs) == (args, kwargs) + + +def test_mixed_args_and_kwargs(msg): + mpa = m.mixed_plus_args + mpk = m.mixed_plus_kwargs + mpak = m.mixed_plus_args_kwargs + mpakd = m.mixed_plus_args_kwargs_defaults + + assert mpa(1, 2.5, 4, 99.5, None) == (1, 2.5, (4, 99.5, None)) + assert mpa(1, 2.5) == (1, 2.5, ()) + with pytest.raises(TypeError) as excinfo: + assert mpa(1) + assert ( + msg(excinfo.value) + == """ + mixed_plus_args(): incompatible function arguments. The following argument types are supported: + 1. (arg0: typing.SupportsInt | typing.SupportsIndex, arg1: typing.SupportsFloat | typing.SupportsIndex, *args) -> tuple[int, float, tuple] + + Invoked with: 1 + """ + ) + with pytest.raises(TypeError) as excinfo: + assert mpa() + assert ( + msg(excinfo.value) + == """ + mixed_plus_args(): incompatible function arguments. The following argument types are supported: + 1. (arg0: typing.SupportsInt | typing.SupportsIndex, arg1: typing.SupportsFloat | typing.SupportsIndex, *args) -> tuple[int, float, tuple] + + Invoked with: + """ + ) + + assert mpk(-2, 3.5, pi=3.14159, e=2.71828) == ( + -2, + 3.5, + {"e": 2.71828, "pi": 3.14159}, + ) + assert mpak(7, 7.7, 7.77, 7.777, 7.7777, minusseven=-7) == ( + 7, + 7.7, + (7.77, 7.777, 7.7777), + {"minusseven": -7}, + ) + assert mpakd() == (1, 3.14159, (), {}) + assert mpakd(3) == (3, 3.14159, (), {}) + assert mpakd(j=2.71828) == (1, 2.71828, (), {}) + assert mpakd(k=42) == (1, 3.14159, (), {"k": 42}) + assert mpakd(1, 1, 2, 3, 5, 8, then=13, followedby=21) == ( + 1, + 1, + (2, 3, 5, 8), + {"then": 13, "followedby": 21}, + ) + # Arguments specified both positionally and via kwargs should fail: + with pytest.raises(TypeError) as excinfo: + assert mpakd(1, i=1) + assert ( + msg(excinfo.value) + == """ + mixed_plus_args_kwargs_defaults(): incompatible function arguments. The following argument types are supported: + 1. (i: typing.SupportsInt | typing.SupportsIndex = 1, j: typing.SupportsFloat | typing.SupportsIndex = 3.14159, *args, **kwargs) -> tuple[int, float, tuple, dict[str, typing.Any]] + + Invoked with: 1; kwargs: i=1 + """ + ) + with pytest.raises(TypeError) as excinfo: + assert mpakd(1, 2, j=1) + assert ( + msg(excinfo.value) + == """ + mixed_plus_args_kwargs_defaults(): incompatible function arguments. The following argument types are supported: + 1. (i: typing.SupportsInt | typing.SupportsIndex = 1, j: typing.SupportsFloat | typing.SupportsIndex = 3.14159, *args, **kwargs) -> tuple[int, float, tuple, dict[str, typing.Any]] + + Invoked with: 1, 2; kwargs: j=1 + """ + ) + + # Arguments after a py::args are automatically keyword-only (pybind 2.9+) + assert m.args_kwonly(2, 2.5, z=22) == (2, 2.5, (), 22) + assert m.args_kwonly(2, 2.5, "a", "b", "c", z=22) == (2, 2.5, ("a", "b", "c"), 22) + assert m.args_kwonly(z=22, i=4, j=16) == (4, 16, (), 22) + + with pytest.raises(TypeError) as excinfo: + assert m.args_kwonly(2, 2.5, 22) # missing z= keyword + assert ( + msg(excinfo.value) + == """ + args_kwonly(): incompatible function arguments. The following argument types are supported: + 1. (i: typing.SupportsInt | typing.SupportsIndex, j: typing.SupportsFloat | typing.SupportsIndex, *args, z: typing.SupportsInt | typing.SupportsIndex) -> tuple[int, float, tuple, int] + + Invoked with: 2, 2.5, 22 + """ + ) + + assert m.args_kwonly_kwargs(i=1, k=4, j=10, z=-1, y=9) == ( + 1, + 10, + (), + -1, + {"k": 4, "y": 9}, + ) + assert m.args_kwonly_kwargs(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, z=11, y=12) == ( + 1, + 2, + (3, 4, 5, 6, 7, 8, 9, 10), + 11, + {"y": 12}, + ) + assert ( + m.args_kwonly_kwargs.__doc__ + == "args_kwonly_kwargs(i: typing.SupportsInt | typing.SupportsIndex, j: typing.SupportsFloat | typing.SupportsIndex, *args, z: typing.SupportsInt | typing.SupportsIndex, **kwargs) -> tuple[int, float, tuple, int, dict[str, typing.Any]]\n" + ) + + assert ( + m.args_kwonly_kwargs_defaults.__doc__ + == "args_kwonly_kwargs_defaults(i: typing.SupportsInt | typing.SupportsIndex = 1, j: typing.SupportsFloat | typing.SupportsIndex = 3.14159, *args, z: typing.SupportsInt | typing.SupportsIndex = 42, **kwargs) -> tuple[int, float, tuple, int, dict[str, typing.Any]]\n" + ) + assert m.args_kwonly_kwargs_defaults() == (1, 3.14159, (), 42, {}) + assert m.args_kwonly_kwargs_defaults(2) == (2, 3.14159, (), 42, {}) + assert m.args_kwonly_kwargs_defaults(z=-99) == (1, 3.14159, (), -99, {}) + assert m.args_kwonly_kwargs_defaults(5, 6, 7, 8) == (5, 6, (7, 8), 42, {}) + assert m.args_kwonly_kwargs_defaults(5, 6, 7, m=8) == (5, 6, (7,), 42, {"m": 8}) + assert m.args_kwonly_kwargs_defaults(5, 6, 7, m=8, z=9) == (5, 6, (7,), 9, {"m": 8}) + + +def test_keyword_only_args(msg): + assert m.kw_only_all(i=1, j=2) == (1, 2) + assert m.kw_only_all(j=1, i=2) == (2, 1) + + with pytest.raises(TypeError) as excinfo: + assert m.kw_only_all(i=1) == (1,) + assert "incompatible function arguments" in str(excinfo.value) + + with pytest.raises(TypeError) as excinfo: + assert m.kw_only_all(1, 2) == (1, 2) + assert "incompatible function arguments" in str(excinfo.value) + + assert m.kw_only_some(1, k=3, j=2) == (1, 2, 3) + + assert m.kw_only_with_defaults(z=8) == (3, 4, 5, 8) + assert m.kw_only_with_defaults(2, z=8) == (2, 4, 5, 8) + assert m.kw_only_with_defaults(2, j=7, k=8, z=9) == (2, 7, 8, 9) + assert m.kw_only_with_defaults(2, 7, z=9, k=8) == (2, 7, 8, 9) + + assert m.kw_only_mixed(1, j=2) == (1, 2) + assert m.kw_only_mixed(j=2, i=3) == (3, 2) + assert m.kw_only_mixed(i=2, j=3) == (2, 3) + + assert m.kw_only_plus_more(4, 5, k=6, extra=7) == (4, 5, 6, {"extra": 7}) + assert m.kw_only_plus_more(3, k=5, j=4, extra=6) == (3, 4, 5, {"extra": 6}) + assert m.kw_only_plus_more(2, k=3, extra=4) == (2, -1, 3, {"extra": 4}) + + with pytest.raises(TypeError) as excinfo: + assert m.kw_only_mixed(i=1) == (1,) + assert "incompatible function arguments" in str(excinfo.value) + + with pytest.raises(RuntimeError) as excinfo: + m.register_invalid_kw_only(m) + assert ( + msg(excinfo.value) + == """ + arg(): cannot specify an unnamed argument after a kw_only() annotation or args() argument + """ + ) + + # https://github.com/pybind/pybind11/pull/3402#issuecomment-963341987 + x = m.first_arg_kw_only(i=1) + x.method() + x.method(i=1, j=2) + assert ( + m.first_arg_kw_only.__init__.__doc__ + == "__init__(self: pybind11_tests.kwargs_and_defaults.first_arg_kw_only, *, i: typing.SupportsInt | typing.SupportsIndex = 0) -> None\n" + ) + assert ( + m.first_arg_kw_only.method.__doc__ + == "method(self: pybind11_tests.kwargs_and_defaults.first_arg_kw_only, *, i: typing.SupportsInt | typing.SupportsIndex = 1, j: typing.SupportsInt | typing.SupportsIndex = 2) -> None\n" + ) + + +def test_positional_only_args(): + assert m.pos_only_all(1, 2) == (1, 2) + assert m.pos_only_all(2, 1) == (2, 1) + + with pytest.raises(TypeError) as excinfo: + m.pos_only_all(i=1, j=2) + assert "incompatible function arguments" in str(excinfo.value) + + assert m.pos_only_mix(1, 2) == (1, 2) + assert m.pos_only_mix(2, j=1) == (2, 1) + + with pytest.raises(TypeError) as excinfo: + m.pos_only_mix(i=1, j=2) + assert "incompatible function arguments" in str(excinfo.value) + + assert m.pos_kw_only_mix(1, 2, k=3) == (1, 2, 3) + assert m.pos_kw_only_mix(1, j=2, k=3) == (1, 2, 3) + + with pytest.raises(TypeError) as excinfo: + m.pos_kw_only_mix(i=1, j=2, k=3) + assert "incompatible function arguments" in str(excinfo.value) + + with pytest.raises(TypeError) as excinfo: + m.pos_kw_only_mix(1, 2, 3) + assert "incompatible function arguments" in str(excinfo.value) + + with pytest.raises(TypeError) as excinfo: + m.pos_only_def_mix() + assert "incompatible function arguments" in str(excinfo.value) + + assert m.pos_only_def_mix(1) == (1, 2, 3) + assert m.pos_only_def_mix(1, 4) == (1, 4, 3) + assert m.pos_only_def_mix(1, 4, 7) == (1, 4, 7) + assert m.pos_only_def_mix(1, 4, k=7) == (1, 4, 7) + + with pytest.raises(TypeError) as excinfo: + m.pos_only_def_mix(1, j=4) + assert "incompatible function arguments" in str(excinfo.value) + + # Mix it with args and kwargs: + assert ( + m.args_kwonly_full_monty.__doc__ + == "args_kwonly_full_monty(arg0: typing.SupportsInt | typing.SupportsIndex = 1, arg1: typing.SupportsInt | typing.SupportsIndex = 2, /, j: typing.SupportsFloat | typing.SupportsIndex = 3.14159, *args, z: typing.SupportsInt | typing.SupportsIndex = 42, **kwargs) -> tuple[int, int, float, tuple, int, dict[str, typing.Any]]\n" + ) + assert m.args_kwonly_full_monty() == (1, 2, 3.14159, (), 42, {}) + assert m.args_kwonly_full_monty(8) == (8, 2, 3.14159, (), 42, {}) + assert m.args_kwonly_full_monty(8, 9) == (8, 9, 3.14159, (), 42, {}) + assert m.args_kwonly_full_monty(8, 9, 10) == (8, 9, 10.0, (), 42, {}) + assert m.args_kwonly_full_monty(3, 4, 5, 6, 7, m=8, z=9) == ( + 3, + 4, + 5.0, + ( + 6, + 7, + ), + 9, + {"m": 8}, + ) + assert m.args_kwonly_full_monty(3, 4, 5, 6, 7, m=8, z=9) == ( + 3, + 4, + 5.0, + ( + 6, + 7, + ), + 9, + {"m": 8}, + ) + assert m.args_kwonly_full_monty(5, j=7, m=8, z=9) == (5, 2, 7.0, (), 9, {"m": 8}) + assert m.args_kwonly_full_monty(i=5, j=7, m=8, z=9) == ( + 1, + 2, + 7.0, + (), + 9, + {"i": 5, "m": 8}, + ) + + # pos_only at the beginning of the argument list was "broken" in how it was displayed (though + # this is fairly useless in practice). Related to: + # https://github.com/pybind/pybind11/pull/3402#issuecomment-963341987 + assert ( + m.first_arg_kw_only.pos_only.__doc__ + == "pos_only(self: pybind11_tests.kwargs_and_defaults.first_arg_kw_only, /, i: typing.SupportsInt | typing.SupportsIndex, j: typing.SupportsInt | typing.SupportsIndex) -> None\n" + ) + + +def test_signatures(): + assert ( + m.kw_only_all.__doc__ + == "kw_only_all(*, i: typing.SupportsInt | typing.SupportsIndex, j: typing.SupportsInt | typing.SupportsIndex) -> tuple[int, int]\n" + ) + assert ( + m.kw_only_mixed.__doc__ + == "kw_only_mixed(i: typing.SupportsInt | typing.SupportsIndex, *, j: typing.SupportsInt | typing.SupportsIndex) -> tuple[int, int]\n" + ) + assert ( + m.pos_only_all.__doc__ + == "pos_only_all(i: typing.SupportsInt | typing.SupportsIndex, j: typing.SupportsInt | typing.SupportsIndex, /) -> tuple[int, int]\n" + ) + assert ( + m.pos_only_mix.__doc__ + == "pos_only_mix(i: typing.SupportsInt | typing.SupportsIndex, /, j: typing.SupportsInt | typing.SupportsIndex) -> tuple[int, int]\n" + ) + assert ( + m.pos_kw_only_mix.__doc__ + == "pos_kw_only_mix(i: typing.SupportsInt | typing.SupportsIndex, /, j: typing.SupportsInt | typing.SupportsIndex, *, k: typing.SupportsInt | typing.SupportsIndex) -> tuple[int, int, int]\n" + ) + + +@pytest.mark.skipif("env.GRAALPY", reason="Different refcounting mechanism") +def test_args_refcount(): + """Issue/PR #1216 - py::args elements get double-inc_ref()ed when combined with regular + arguments""" + refcount = m.arg_refcount_h + + myval = object() + expected = refcount(myval) + assert m.arg_refcount_h(myval) == expected + assert m.arg_refcount_o(myval) == expected + 1 + assert m.arg_refcount_h(myval) == expected + assert refcount(myval) == expected + + assert m.mixed_plus_args(1, 2.0, "a", myval) == (1, 2.0, ("a", myval)) + assert refcount(myval) == expected + + assert m.mixed_plus_kwargs(3, 4.0, a=1, b=myval) == (3, 4.0, {"a": 1, "b": myval}) + assert refcount(myval) == expected + + assert m.args_function(-1, myval) == (-1, myval) + assert refcount(myval) == expected + + assert m.mixed_plus_args_kwargs(5, 6.0, myval, a=myval) == ( + 5, + 6.0, + (myval,), + {"a": myval}, + ) + assert refcount(myval) == expected + + assert m.args_kwargs_function(7, 8, myval, a=1, b=myval) == ( + (7, 8, myval), + {"a": 1, "b": myval}, + ) + assert refcount(myval) == expected + + assert m.args_kwargs_subclass_function(7, 8, myval, a=1, b=myval) == ( + (7, 8, myval), + {"a": 1, "b": myval}, + ) + assert refcount(myval) == expected + + exp3 = refcount(myval, myval, myval) + # if we have to create a new tuple internally, then it will hold an extra reference for each item in it. + assert m.args_refcount(myval, myval, myval) == (exp3 + 3, exp3 + 3, exp3 + 3) + assert refcount(myval) == expected + + # This function takes the first arg as a `py::object` and the rest as a `py::args`. Unlike the + # previous case, when we have both positional and `py::args` we need to construct a new tuple + # for the `py::args`; in the previous case, we could simply inc_ref and pass on Python's input + # tuple without having to inc_ref the individual elements, but here we can't, hence the extra + # refs. + exp3_3 = exp3 + 3 + assert m.mixed_args_refcount(myval, myval, myval) == (exp3_3, exp3_3, exp3_3) + + assert m.class_default_argument() == "" diff --git a/external_libraries/pybind11/tests/test_local_bindings.cpp b/external_libraries/pybind11/tests/test_local_bindings.cpp new file mode 100644 index 00000000..5118b25d --- /dev/null +++ b/external_libraries/pybind11/tests/test_local_bindings.cpp @@ -0,0 +1,131 @@ +/* + tests/test_local_bindings.cpp -- tests the py::module_local class feature which makes a class + binding local to the module in which it is defined. + + Copyright (c) 2017 Jason Rhinelander + + All rights reserved. Use of this source code is governed by a + BSD-style license that can be found in the LICENSE file. +*/ + +#include +#include + +#include "local_bindings.h" +#include "pybind11_tests.h" + +#include +#include + +TEST_SUBMODULE(local_bindings, m) { + // test_load_external + m.def("load_external1", [](ExternalType1 &e) { return e.i; }); + m.def("load_external2", [](ExternalType2 &e) { return e.i; }); + m.def("load_external3", [](ExternalType3 &e) { return e.i; }); + + struct SharedKeepAlive { + std::shared_ptr contents; + int value() const { return contents ? *contents : -20251012; } + long use_count() const { return contents.use_count(); } + }; + py::class_(m, "SharedKeepAlive") + .def_property_readonly("value", &SharedKeepAlive::value) + .def_property_readonly("use_count", &SharedKeepAlive::use_count); + m.def("load_external2_shared", [](const std::shared_ptr &p) { + return SharedKeepAlive{std::shared_ptr(p, &p->i)}; + }); + m.def("load_external3_shared", [](const std::shared_ptr &p) { + return SharedKeepAlive{std::shared_ptr(p, &p->i)}; + }); + m.def("load_external1_unique", [](std::unique_ptr p) { return p->i; }); + m.def("load_external3_unique", [](std::unique_ptr p) { return p->i; }); + + // Aspects of set_foreign_holder that are not covered: + // - loading a foreign instance into a custom holder should fail + // - we're only covering the case where the local module doesn't know + // about the type; the paths where it does (e.g., if both global and + // foreign-module-local bindings exist for the same type) should work + // the same way (they use the same code so they very likely do) + + // test_local_bindings + // Register a class with py::module_local: + bind_local(m, "LocalType", py::module_local()).def("get3", [](LocalType &t) { + return t.i + 3; + }); + + m.def("local_value", [](LocalType &l) { return l.i; }); + + // test_nonlocal_failure + // The main pybind11 test module is loaded first, so this registration will succeed (the second + // one, in pybind11_cross_module_tests.cpp, is designed to fail): + bind_local(m, "NonLocalType") + .def(py::init()) + .def("get", [](LocalType &i) { return i.i; }); + + // test_duplicate_local + // py::module_local declarations should be visible across compilation units that get linked + // together; this tries to register a duplicate local. It depends on a definition in + // test_class.cpp and should raise a runtime error from the duplicate definition attempt. If + // test_class isn't available it *also* throws a runtime error (with "test_class not enabled" + // as value). + m.def("register_local_external", [m]() { + auto main = py::module_::import("pybind11_tests"); + if (py::hasattr(main, "class_")) { + bind_local(m, "LocalExternal", py::module_local()); + } else { + throw std::runtime_error("test_class not enabled"); + } + }); + + // test_stl_bind_local + // stl_bind.h binders defaults to py::module_local if the types are local or converting: + py::bind_vector(m, "LocalVec"); + py::bind_map(m, "LocalMap"); + // and global if the type (or one of the types, for the map) is global: + py::bind_vector(m, "NonLocalVec"); + py::bind_map(m, "NonLocalMap"); + + // test_stl_bind_global + // They can, however, be overridden to global using `py::module_local(false)`: + bind_local(m, "NonLocal2"); + py::bind_vector(m, "LocalVec2", py::module_local()); + py::bind_map(m, "NonLocalMap2", py::module_local(false)); + + // test_mixed_local_global + // We try this both with the global type registered first and vice versa (the order shouldn't + // matter). + m.def("register_mixed_global", [m]() { + bind_local(m, "MixedGlobalLocal", py::module_local(false)); + }); + m.def("register_mixed_local", [m]() { + bind_local(m, "MixedLocalGlobal", py::module_local()); + }); + m.def("get_mixed_gl", [](int i) { return MixedGlobalLocal(i); }); + m.def("get_mixed_lg", [](int i) { return MixedLocalGlobal(i); }); + + // test_internal_locals_differ + m.def("local_cpp_types_addr", + []() { return (uintptr_t) &py::detail::get_local_internals().registered_types_cpp; }); + + // test_stl_caster_vs_stl_bind + m.def("load_vector_via_caster", + [](std::vector v) { return std::accumulate(v.begin(), v.end(), 0); }); + + // test_cross_module_calls + m.def("return_self", [](LocalVec *v) { return v; }); + m.def("return_copy", [](const LocalVec &v) { return LocalVec(v); }); + + class Cat : public pets::Pet { + public: + explicit Cat(std::string name) : Pet(std::move(name)) {} + }; + py::class_(m, "Pet", py::module_local()).def("get_name", &pets::Pet::name); + // Binding for local extending class: + py::class_(m, "Cat").def(py::init()); + m.def("pet_name", [](pets::Pet &p) { return p.name(); }); + + py::class_(m, "MixGL").def(py::init()); + m.def("get_gl_value", [](MixGL &o) { return o.i + 10; }); + + py::class_(m, "MixGL2").def(py::init()); +} diff --git a/external_libraries/pybind11/tests/test_local_bindings.py b/external_libraries/pybind11/tests/test_local_bindings.py new file mode 100644 index 00000000..cac89d0d --- /dev/null +++ b/external_libraries/pybind11/tests/test_local_bindings.py @@ -0,0 +1,291 @@ +from __future__ import annotations + +import sys +from contextlib import suppress + +import pytest + +from pybind11_tests import local_bindings as m + + +def test_load_external(): + """Load a `py::module_local` type that's only registered in an external module""" + import pybind11_cross_module_tests as cm + + assert m.load_external1(cm.ExternalType1(11)) == 11 + assert m.load_external2(cm.ExternalType2(22)) == 22 + assert m.load_external3(cm.ExternalType3(33)) == 33 + + with pytest.raises(TypeError) as excinfo: + assert m.load_external2(cm.ExternalType1(21)) == 21 + assert "incompatible function arguments" in str(excinfo.value) + + with pytest.raises(TypeError) as excinfo: + assert m.load_external1(cm.ExternalType2(12)) == 12 + assert "incompatible function arguments" in str(excinfo.value) + + def test_shared(val, ctor, loader): + obj = ctor(val) + with suppress(AttributeError): # non-cpython VMs don't have getrefcount + rc_before = sys.getrefcount(obj) + wrapper = loader(obj) + # wrapper holds a shared_ptr that keeps obj alive + assert wrapper.use_count == 1 + assert wrapper.value == val + with suppress(AttributeError): + rc_after = sys.getrefcount(obj) + assert rc_after > rc_before + + test_shared(220, cm.ExternalType2, m.load_external2_shared) + test_shared(330, cm.ExternalType3, m.load_external3_shared) + + with pytest.raises(TypeError, match="incompatible function arguments"): + test_shared(320, cm.ExternalType2, m.load_external3_shared) + with pytest.raises(TypeError, match="incompatible function arguments"): + test_shared(230, cm.ExternalType3, m.load_external2_shared) + + with pytest.raises( + RuntimeError, match="Foreign instance cannot be converted to std::unique_ptr" + ): + m.load_external1_unique(cm.ExternalType1(2200)) + + with pytest.raises( + RuntimeError, match="Foreign instance cannot be converted to std::unique_ptr" + ): + m.load_external3_unique(cm.ExternalType3(3300)) + + +def test_local_bindings(): + """Tests that duplicate `py::module_local` class bindings work across modules""" + + # Make sure we can load the second module with the conflicting (but local) definition: + import pybind11_cross_module_tests as cm + + i1 = m.LocalType(5) + assert i1.get() == 4 + assert i1.get3() == 8 + + i2 = cm.LocalType(10) + assert i2.get() == 11 + assert i2.get2() == 12 + + assert not hasattr(i1, "get2") + assert not hasattr(i2, "get3") + + # Loading within the local module + assert m.local_value(i1) == 5 + assert cm.local_value(i2) == 10 + + # Cross-module loading works as well (on failure, the type loader looks for + # external module-local converters): + assert m.local_value(i2) == 10 + assert cm.local_value(i1) == 5 + + +def test_nonlocal_failure(): + """Tests that attempting to register a non-local type in multiple modules fails""" + import pybind11_cross_module_tests as cm + + with pytest.raises(RuntimeError) as excinfo: + cm.register_nonlocal() + assert ( + str(excinfo.value) == 'generic_type: type "NonLocalType" is already registered!' + ) + + +def test_duplicate_local(): + """Tests expected failure when registering a class twice with py::local in the same module""" + with pytest.raises(RuntimeError) as excinfo: + m.register_local_external() + import pybind11_tests + + assert str(excinfo.value) == ( + 'generic_type: type "LocalExternal" is already registered!' + if hasattr(pybind11_tests, "class_") + else "test_class not enabled" + ) + + +def test_stl_bind_local(): + import pybind11_cross_module_tests as cm + + v1, v2 = m.LocalVec(), cm.LocalVec() + v1.append(m.LocalType(1)) + v1.append(m.LocalType(2)) + v2.append(cm.LocalType(1)) + v2.append(cm.LocalType(2)) + + # Cross module value loading: + v1.append(cm.LocalType(3)) + v2.append(m.LocalType(3)) + + assert [i.get() for i in v1] == [0, 1, 2] + assert [i.get() for i in v2] == [2, 3, 4] + + v3, v4 = m.NonLocalVec(), cm.NonLocalVec2() + v3.append(m.NonLocalType(1)) + v3.append(m.NonLocalType(2)) + v4.append(m.NonLocal2(3)) + v4.append(m.NonLocal2(4)) + + assert [i.get() for i in v3] == [1, 2] + assert [i.get() for i in v4] == [13, 14] + + d1, d2 = m.LocalMap(), cm.LocalMap() + d1["a"] = v1[0] + d1["b"] = v1[1] + d2["c"] = v2[0] + d2["d"] = v2[1] + assert {i: d1[i].get() for i in d1} == {"a": 0, "b": 1} + assert {i: d2[i].get() for i in d2} == {"c": 2, "d": 3} + + +def test_stl_bind_global(): + import pybind11_cross_module_tests as cm + + with pytest.raises(RuntimeError) as excinfo: + cm.register_nonlocal_map() + assert ( + str(excinfo.value) == 'generic_type: type "NonLocalMap" is already registered!' + ) + + with pytest.raises(RuntimeError) as excinfo: + cm.register_nonlocal_vec() + assert ( + str(excinfo.value) == 'generic_type: type "NonLocalVec" is already registered!' + ) + + with pytest.raises(RuntimeError) as excinfo: + cm.register_nonlocal_map2() + assert ( + str(excinfo.value) == 'generic_type: type "NonLocalMap2" is already registered!' + ) + + +def test_mixed_local_global(): + """Local types take precedence over globally registered types: a module with a `module_local` + type can be registered even if the type is already registered globally. With the module, + casting will go to the local type; outside the module casting goes to the global type. + """ + import pybind11_cross_module_tests as cm + + m.register_mixed_global() + m.register_mixed_local() + + a = [] + a.append(m.MixedGlobalLocal(1)) + a.append(m.MixedLocalGlobal(2)) + a.append(m.get_mixed_gl(3)) + a.append(m.get_mixed_lg(4)) + + assert [x.get() for x in a] == [101, 1002, 103, 1004] + + cm.register_mixed_global_local() + cm.register_mixed_local_global() + a.append(m.MixedGlobalLocal(5)) + a.append(m.MixedLocalGlobal(6)) + a.append(cm.MixedGlobalLocal(7)) + a.append(cm.MixedLocalGlobal(8)) + a.append(m.get_mixed_gl(9)) + a.append(m.get_mixed_lg(10)) + a.append(cm.get_mixed_gl(11)) + a.append(cm.get_mixed_lg(12)) + + assert [x.get() for x in a] == [ + 101, + 1002, + 103, + 1004, + 105, + 1006, + 207, + 2008, + 109, + 1010, + 211, + 2012, + ] + + +def test_internal_locals_differ(): + """Makes sure the internal local type map differs across the two modules""" + import pybind11_cross_module_tests as cm + + assert m.local_cpp_types_addr() != cm.local_cpp_types_addr() + + +def test_stl_caster_vs_stl_bind(msg): + """One module uses a generic vector caster from `` while the other + exports `std::vector` via `py:bind_vector` and `py::module_local`""" + import pybind11_cross_module_tests as cm + + v1 = cm.VectorInt([1, 2, 3]) + assert m.load_vector_via_caster(v1) == 6 + assert cm.load_vector_via_binding(v1) == 6 + + v2 = [1, 2, 3] + assert m.load_vector_via_caster(v2) == 6 + with pytest.raises(TypeError) as excinfo: + cm.load_vector_via_binding(v2) + assert ( + msg(excinfo.value) + == """ + load_vector_via_binding(): incompatible function arguments. The following argument types are supported: + 1. (arg0: pybind11_cross_module_tests.VectorInt) -> int + + Invoked with: [1, 2, 3] + """ + ) + + +def test_cross_module_calls(): + import pybind11_cross_module_tests as cm + + v1 = m.LocalVec() + v1.append(m.LocalType(1)) + v2 = cm.LocalVec() + v2.append(cm.LocalType(2)) + + # Returning the self pointer should get picked up as returning an existing + # instance (even when that instance is of a foreign, non-local type). + assert m.return_self(v1) is v1 + assert cm.return_self(v2) is v2 + assert m.return_self(v2) is v2 + assert cm.return_self(v1) is v1 + + assert m.LocalVec is not cm.LocalVec + # Returning a copy, on the other hand, always goes to the local type, + # regardless of where the source type came from. + assert type(m.return_copy(v1)) is m.LocalVec + assert type(m.return_copy(v2)) is m.LocalVec + assert type(cm.return_copy(v1)) is cm.LocalVec + assert type(cm.return_copy(v2)) is cm.LocalVec + + # Test the example given in the documentation (which also tests inheritance casting): + mycat = m.Cat("Fluffy") + mydog = cm.Dog("Rover") + assert mycat.get_name() == "Fluffy" + assert mydog.name() == "Rover" + assert m.Cat.__base__.__name__ == "Pet" + assert cm.Dog.__base__.__name__ == "Pet" + assert m.Cat.__base__ is not cm.Dog.__base__ + assert m.pet_name(mycat) == "Fluffy" + assert m.pet_name(mydog) == "Rover" + assert cm.pet_name(mycat) == "Fluffy" + assert cm.pet_name(mydog) == "Rover" + + assert m.MixGL is not cm.MixGL + a = m.MixGL(1) + b = cm.MixGL(2) + assert m.get_gl_value(a) == 11 + assert m.get_gl_value(b) == 12 + assert cm.get_gl_value(a) == 101 + assert cm.get_gl_value(b) == 102 + + c, d = m.MixGL2(3), cm.MixGL2(4) + with pytest.raises(TypeError) as excinfo: + m.get_gl_value(c) + assert "incompatible function arguments" in str(excinfo.value) + with pytest.raises(TypeError) as excinfo: + m.get_gl_value(d) + assert "incompatible function arguments" in str(excinfo.value) diff --git a/external_libraries/pybind11/tests/test_methods_and_attributes.cpp b/external_libraries/pybind11/tests/test_methods_and_attributes.cpp new file mode 100644 index 00000000..86b74931 --- /dev/null +++ b/external_libraries/pybind11/tests/test_methods_and_attributes.cpp @@ -0,0 +1,788 @@ +/* + tests/test_methods_and_attributes.cpp -- constructors, deconstructors, attribute access, + __str__, argument and return value conventions + + Copyright (c) 2016 Wenzel Jakob + + All rights reserved. Use of this source code is governed by a + BSD-style license that can be found in the LICENSE file. +*/ + +#include "constructor_stats.h" +#include "pybind11_tests.h" + +#if !defined(PYPY_VERSION) +// Flag set by the capsule destructor in test_dynamic_attr_dealloc_frees_dict_contents. +// File scope so the captureless capsule destructor (void(*)(void*)) can access it. +static bool s_dynamic_attr_capsule_freed = false; +#endif + +#if !defined(PYBIND11_OVERLOAD_CAST) +template +using overload_cast_ = pybind11::detail::overload_cast_impl; +#endif + +class ExampleMandA { +public: + ExampleMandA() { print_default_created(this); } + explicit ExampleMandA(int value) : value(value) { print_created(this, value); } + ExampleMandA(const ExampleMandA &e) : value(e.value) { print_copy_created(this); } + explicit ExampleMandA(std::string &&) {} + ExampleMandA(ExampleMandA &&e) noexcept : value(e.value) { print_move_created(this); } + ~ExampleMandA() { print_destroyed(this); } + + std::string toString() const { return "ExampleMandA[value=" + std::to_string(value) + "]"; } + + void operator=(const ExampleMandA &e) { + print_copy_assigned(this); + value = e.value; + } + void operator=(ExampleMandA &&e) noexcept { + print_move_assigned(this); + value = e.value; + } + + // NOLINTNEXTLINE(performance-unnecessary-value-param) + void add1(ExampleMandA other) { value += other.value; } // passing by value + void add2(ExampleMandA &other) { value += other.value; } // passing by reference + void add3(const ExampleMandA &other) { value += other.value; } // passing by const reference + void add4(ExampleMandA *other) { value += other->value; } // passing by pointer + void add5(const ExampleMandA *other) { value += other->value; } // passing by const pointer + + void add6(int other) { value += other; } // passing by value + void add7(int &other) { value += other; } // passing by reference + void add8(const int &other) { value += other; } // passing by const reference + // NOLINTNEXTLINE(readability-non-const-parameter) Deliberately non-const for testing + void add9(int *other) { value += *other; } // passing by pointer + void add10(const int *other) { value += *other; } // passing by const pointer + + void consume_str(std::string &&) {} + + ExampleMandA self1() { return *this; } // return by value + ExampleMandA &self2() { return *this; } // return by reference + const ExampleMandA &self3() const { return *this; } // return by const reference + ExampleMandA *self4() { return this; } // return by pointer + const ExampleMandA *self5() const { return this; } // return by const pointer + + int internal1() const { return value; } // return by value + int &internal2() { return value; } // return by reference + const int &internal3() const { return value; } // return by const reference + int *internal4() { return &value; } // return by pointer + const int *internal5() { return &value; } // return by const pointer + + py::str overloaded() { return "()"; } + py::str overloaded(int) { return "(int)"; } + py::str overloaded(int, float) { return "(int, float)"; } + py::str overloaded(float, int) { return "(float, int)"; } + py::str overloaded(int, int) { return "(int, int)"; } + py::str overloaded(float, float) { return "(float, float)"; } + py::str overloaded(int) const { return "(int) const"; } + py::str overloaded(int, float) const { return "(int, float) const"; } + py::str overloaded(float, int) const { return "(float, int) const"; } + py::str overloaded(int, int) const { return "(int, int) const"; } + py::str overloaded(float, float) const { return "(float, float) const"; } + + static py::str overloaded(float) { return "static float"; } + + int value = 0; +}; + +struct TestProperties { + int value = 1; + static int static_value; + + int get() const { return value; } + void set(int v) { value = v; } + + static int static_get() { return static_value; } + static void static_set(int v) { static_value = v; } +}; +int TestProperties::static_value = 1; + +struct TestPropertiesOverride : TestProperties { + int value = 99; + static int static_value; +}; +int TestPropertiesOverride::static_value = 99; + +struct TestPropRVP { + UserType v1{1}; + UserType v2{1}; + static UserType sv1; + static UserType sv2; + + const UserType &get1() const { return v1; } + const UserType &get2() const { return v2; } + UserType get_rvalue() const { return v2; } + void set1(int v) { v1.set(v); } + void set2(int v) { v2.set(v); } +}; +UserType TestPropRVP::sv1(1); +UserType TestPropRVP::sv2(1); + +// Test None-allowed py::arg argument policy +class NoneTester { +public: + int answer = 42; +}; +int none1(const NoneTester &obj) { return obj.answer; } +int none2(NoneTester *obj) { return obj ? obj->answer : -1; } +int none3(std::shared_ptr &obj) { return obj ? obj->answer : -1; } +int none4(std::shared_ptr *obj) { return obj && *obj ? (*obj)->answer : -1; } +int none5(const std::shared_ptr &obj) { return obj ? obj->answer : -1; } + +// Issue #2778: implicit casting from None to object (not pointer) +class NoneCastTester { +public: + int answer = -1; + NoneCastTester() = default; + explicit NoneCastTester(int v) : answer(v) {} +}; + +struct StrIssue { + int val = -1; + + StrIssue() = default; + explicit StrIssue(int i) : val{i} {} +}; + +// Issues #854, #910: incompatible function args when member function/pointer is in unregistered +// base class +class UnregisteredBase { +public: + void do_nothing() const {} + void increase_value() { + rw_value++; + ro_value += 0.25; + } + void set_int(int v) { rw_value = v; } + int get_int() const { return rw_value; } + double get_double() const { return ro_value; } + int rw_value = 42; + double ro_value = 1.25; +}; +class RegisteredDerived : public UnregisteredBase { +public: + using UnregisteredBase::UnregisteredBase; + double sum() const { return rw_value + ro_value; } +}; + +// Issue #2234: noexcept methods in an unregistered base should be bindable on the derived class. +// In C++17, noexcept is part of the function type, so &Derived::method resolves to +// a Base member function pointer with noexcept, requiring explicit template specializations. +class NoexceptUnregisteredBase { +public: + // Exercises cpp_function(Return (Class::*)(Args...) const noexcept, ...) + int value() const noexcept { return m_value; } + // Exercises cpp_function(Return (Class::*)(Args...) noexcept, ...) + void set_value(int v) noexcept { m_value = v; } + // Exercises cpp_function(Return (Class::*)(Args...) & noexcept, ...) + void increment() & noexcept { ++m_value; } + // Exercises cpp_function(Return (Class::*)(Args...) const & noexcept, ...) + int capped_value() const & noexcept { return m_value < 100 ? m_value : 100; } + +private: + int m_value = 99; +}; +class NoexceptDerived : public NoexceptUnregisteredBase { +public: + using NoexceptUnregisteredBase::NoexceptUnregisteredBase; +}; + +// Exercises cpp_function(Return (Class::*)(Args...) &&, ...) and +// cpp_function(Return (Class::*)(Args...) const &&, ...) via an unregistered base. +class RValueRefUnregisteredBase { +public: + // Exercises cpp_function(Return (Class::*)(Args...) &&, ...). + // Moves m_payload to verify that std::move(*c).*f is used in the lambda body. + std::string take() && { // NOLINT(readability-make-member-function-const) + return std::move(m_payload); + } + // Exercises cpp_function(Return (Class::*)(Args...) const &&, ...) + int peek() const && { return m_value; } +#ifdef __cpp_noexcept_function_type + // Exercises cpp_function(Return (Class::*)(Args...) && noexcept, ...) + std::string take_noexcept() && noexcept { // NOLINT(readability-make-member-function-const) + return std::move(m_payload); + } + // Exercises cpp_function(Return (Class::*)(Args...) const && noexcept, ...) + int peek_noexcept() const && noexcept { return m_value; } +#endif + +private: + int m_value = 77; + std::string m_payload{"rref_payload"}; +}; +class RValueRefDerived : public RValueRefUnregisteredBase { +public: + using RValueRefUnregisteredBase::RValueRefUnregisteredBase; +}; + +// Exercises overload_cast with noexcept member function pointers (issue #2234). +// In C++17, overload_cast must have noexcept variants to resolve noexcept overloads. +struct NoexceptOverloaded { + py::str method(int) noexcept { return "(int)"; } + py::str method(int) const noexcept { return "(int) const"; } + py::str method(float) noexcept { return "(float)"; } +}; +// Exercises overload_cast with noexcept free function pointers. +int noexcept_free_func(int x) noexcept { return x + 1; } +int noexcept_free_func(float x) noexcept { return static_cast(x) + 2; } + +// Exercises overload_cast with ref-qualified member function pointers. +struct RefQualifiedOverloaded { + py::str method(int) & { return "(int) &"; } + py::str method(int) const & { return "(int) const &"; } + py::str method(float) && { return "(float) &&"; } + py::str method(float) const && { return "(float) const &&"; } +#ifdef __cpp_noexcept_function_type + py::str method(long) & noexcept { return "(long) & noexcept"; } + py::str method(long) const & noexcept { return "(long) const & noexcept"; } + py::str method(double) && noexcept { return "(double) && noexcept"; } + py::str method(double) const && noexcept { return "(double) const && noexcept"; } +#endif +}; + +// Compile-only guard: catch overload_cast resolution regressions/ambiguities early. +using RefQualifiedOverloadedIntCast = py::detail::overload_cast_impl; +using RefQualifiedOverloadedFloatCast = py::detail::overload_cast_impl; +static_assert( + std::is_same::value, + ""); +static_assert(std::is_same::value, + ""); +static_assert( + std::is_same::value, + ""); +static_assert(std::is_same::value, + ""); + +#ifdef __cpp_noexcept_function_type +using RefQualifiedOverloadedLongCast = py::detail::overload_cast_impl; +using RefQualifiedOverloadedDoubleCast = py::detail::overload_cast_impl; +static_assert( + std::is_same::value, + ""); +static_assert(std::is_same::value, + ""); +static_assert( + std::is_same::value, + ""); +static_assert(std::is_same::value, + ""); +#endif + +// Test explicit lvalue ref-qualification +struct RefQualified { + int value = 0; + + void refQualified(int other) & { value += other; } + int constRefQualified(int other) const & { return value + other; } +}; + +// Test rvalue ref param +struct RValueRefParam { + std::size_t func1(std::string &&s) { return s.size(); } + std::size_t func2(std::string &&s) const { return s.size(); } + std::size_t func3(std::string &&s) & { return s.size(); } + std::size_t func4(std::string &&s) const & { return s.size(); } +}; + +namespace pybind11_tests { +namespace exercise_is_setter { + +struct FieldBase { + int int_value() const { return int_value_; } + + FieldBase &SetIntValue(int int_value) { + int_value_ = int_value; + return *this; + } + +private: + int int_value_ = -99; +}; + +struct Field : FieldBase {}; + +void add_bindings(py::module &m) { + py::module sm = m.def_submodule("exercise_is_setter"); + // NOTE: FieldBase is not wrapped, therefore ... + py::class_(sm, "Field") + .def(py::init<>()) + .def_property( + "int_value", + &Field::int_value, + &Field::SetIntValue // ... the `FieldBase &` return value here cannot be converted. + ); +} + +} // namespace exercise_is_setter +} // namespace pybind11_tests + +TEST_SUBMODULE(methods_and_attributes, m) { + // test_methods_and_attributes + py::class_ emna(m, "ExampleMandA"); + emna.def(py::init<>()) + .def(py::init()) + .def(py::init()) + .def(py::init()) + .def("add1", &ExampleMandA::add1) + .def("add2", &ExampleMandA::add2) + .def("add3", &ExampleMandA::add3) + .def("add4", &ExampleMandA::add4) + .def("add5", &ExampleMandA::add5) + .def("add6", &ExampleMandA::add6) + .def("add7", &ExampleMandA::add7) + .def("add8", &ExampleMandA::add8) + .def("add9", &ExampleMandA::add9) + .def("add10", &ExampleMandA::add10) + .def("consume_str", &ExampleMandA::consume_str) + .def("self1", &ExampleMandA::self1) + .def("self2", &ExampleMandA::self2) + .def("self3", &ExampleMandA::self3) + .def("self4", &ExampleMandA::self4) + .def("self5", &ExampleMandA::self5) + .def("internal1", &ExampleMandA::internal1) + .def("internal2", &ExampleMandA::internal2) + .def("internal3", &ExampleMandA::internal3) + .def("internal4", &ExampleMandA::internal4) + .def("internal5", &ExampleMandA::internal5) +#if defined(PYBIND11_OVERLOAD_CAST) + .def("overloaded", py::overload_cast<>(&ExampleMandA::overloaded)) + .def("overloaded", py::overload_cast(&ExampleMandA::overloaded)) + .def("overloaded", py::overload_cast(&ExampleMandA::overloaded)) + .def("overloaded", py::overload_cast(&ExampleMandA::overloaded)) + .def("overloaded", py::overload_cast(&ExampleMandA::overloaded)) + .def("overloaded", py::overload_cast(&ExampleMandA::overloaded)) + .def("overloaded_float", py::overload_cast(&ExampleMandA::overloaded)) + .def("overloaded_const", py::overload_cast(&ExampleMandA::overloaded, py::const_)) + .def("overloaded_const", + py::overload_cast(&ExampleMandA::overloaded, py::const_)) + .def("overloaded_const", + py::overload_cast(&ExampleMandA::overloaded, py::const_)) + .def("overloaded_const", + py::overload_cast(&ExampleMandA::overloaded, py::const_)) + .def("overloaded_const", + py::overload_cast(&ExampleMandA::overloaded, py::const_)) +#else + // Use both the traditional static_cast method and the C++11 compatible overload_cast_ + .def("overloaded", overload_cast_<>()(&ExampleMandA::overloaded)) + .def("overloaded", overload_cast_()(&ExampleMandA::overloaded)) + .def("overloaded", overload_cast_()(&ExampleMandA::overloaded)) + .def("overloaded", static_cast(&ExampleMandA::overloaded)) + .def("overloaded", static_cast(&ExampleMandA::overloaded)) + .def("overloaded", static_cast(&ExampleMandA::overloaded)) + .def("overloaded_float", overload_cast_()(&ExampleMandA::overloaded)) + .def("overloaded_const", overload_cast_()(&ExampleMandA::overloaded, py::const_)) + .def("overloaded_const", overload_cast_()(&ExampleMandA::overloaded, py::const_)) + .def("overloaded_const", static_cast(&ExampleMandA::overloaded)) + .def("overloaded_const", static_cast(&ExampleMandA::overloaded)) + .def("overloaded_const", static_cast(&ExampleMandA::overloaded)) +#endif + // test_no_mixed_overloads + // Raise error if trying to mix static/non-static overloads on the same name: + .def_static("add_mixed_overloads1", + []() { + auto emna = py::reinterpret_borrow>( + py::module_::import("pybind11_tests.methods_and_attributes") + .attr("ExampleMandA")); + emna.def("overload_mixed1", + static_cast( + &ExampleMandA::overloaded)) + .def_static( + "overload_mixed1", + static_cast(&ExampleMandA::overloaded)); + }) + .def_static("add_mixed_overloads2", + []() { + auto emna = py::reinterpret_borrow>( + py::module_::import("pybind11_tests.methods_and_attributes") + .attr("ExampleMandA")); + emna.def_static("overload_mixed2", + static_cast(&ExampleMandA::overloaded)) + .def("overload_mixed2", + static_cast( + &ExampleMandA::overloaded)); + }) + .def("__str__", &ExampleMandA::toString, py::pos_only()) + .def_readwrite("value", &ExampleMandA::value); + + // test_copy_method + // Issue #443: can't call copied methods in Python 3 + emna.attr("add2b") = emna.attr("add2"); + + // test_properties, test_static_properties, test_static_cls + py::class_(m, "TestProperties") + .def(py::init<>()) + .def_readonly("def_readonly", &TestProperties::value) + .def_readwrite("def_readwrite", &TestProperties::value) + .def_property("def_writeonly", nullptr, [](TestProperties &s, int v) { s.value = v; }) + .def_property("def_property_writeonly", nullptr, &TestProperties::set) + .def_property_readonly("def_property_readonly", &TestProperties::get) + .def_property("def_property", &TestProperties::get, &TestProperties::set) + .def_property("def_property_impossible", nullptr, nullptr) + .def_readonly_static("def_readonly_static", &TestProperties::static_value) + .def_readwrite_static("def_readwrite_static", &TestProperties::static_value) + .def_property_static("def_writeonly_static", + nullptr, + [](const py::object &, int v) { TestProperties::static_value = v; }) + .def_property_readonly_static( + "def_property_readonly_static", + [](const py::object &) { return TestProperties::static_get(); }) + .def_property_static( + "def_property_writeonly_static", + nullptr, + [](const py::object &, int v) { return TestProperties::static_set(v); }) + .def_property_static( + "def_property_static", + [](const py::object &) { return TestProperties::static_get(); }, + [](const py::object &, int v) { TestProperties::static_set(v); }) + .def_property_static( + "static_cls", + [](py::object cls) { return cls; }, + [](const py::object &cls, const py::function &f) { f(cls); }); + + py::class_(m, "TestPropertiesOverride") + .def(py::init<>()) + .def_readonly("def_readonly", &TestPropertiesOverride::value) + .def_readonly_static("def_readonly_static", &TestPropertiesOverride::static_value); + + auto static_get1 = [](const py::object &) -> const UserType & { return TestPropRVP::sv1; }; + auto static_get2 = [](const py::object &) -> const UserType & { return TestPropRVP::sv2; }; + auto static_set1 = [](const py::object &, int v) { TestPropRVP::sv1.set(v); }; + auto static_set2 = [](const py::object &, int v) { TestPropRVP::sv2.set(v); }; + auto rvp_copy = py::return_value_policy::copy; + + // test_property_return_value_policies + py::class_(m, "TestPropRVP") + .def(py::init<>()) + .def_property_readonly("ro_ref", &TestPropRVP::get1) + .def_property_readonly("ro_copy", &TestPropRVP::get2, rvp_copy) + .def_property_readonly("ro_func", py::cpp_function(&TestPropRVP::get2, rvp_copy)) + .def_property("rw_ref", &TestPropRVP::get1, &TestPropRVP::set1) + .def_property("rw_copy", &TestPropRVP::get2, &TestPropRVP::set2, rvp_copy) + .def_property( + "rw_func", py::cpp_function(&TestPropRVP::get2, rvp_copy), &TestPropRVP::set2) + .def_property_readonly_static("static_ro_ref", static_get1) + .def_property_readonly_static("static_ro_copy", static_get2, rvp_copy) + .def_property_readonly_static("static_ro_func", py::cpp_function(static_get2, rvp_copy)) + .def_property_static("static_rw_ref", static_get1, static_set1) + .def_property_static("static_rw_copy", static_get2, static_set2, rvp_copy) + .def_property_static( + "static_rw_func", py::cpp_function(static_get2, rvp_copy), static_set2) + // test_property_rvalue_policy + .def_property_readonly("rvalue", &TestPropRVP::get_rvalue) + .def_property_readonly_static("static_rvalue", + [](const py::object &) { return UserType(1); }); + + // test_metaclass_override + struct MetaclassOverride {}; + py::class_(m, "MetaclassOverride", py::metaclass((PyObject *) &PyType_Type)) + .def_property_readonly_static("readonly", [](const py::object &) { return 1; }); + + // test_overload_ordering + m.def("overload_order", [](const std::string &) { return 1; }); + m.def("overload_order", [](const std::string &) { return 2; }); + m.def("overload_order", [](int) { return 3; }); + m.def("overload_order", [](int) { return 4; }, py::prepend{}); + +#if !defined(PYPY_VERSION) + // test_dynamic_attributes + class DynamicClass { + public: + DynamicClass() { print_default_created(this); } + DynamicClass(const DynamicClass &) = delete; + ~DynamicClass() { print_destroyed(this); } + }; + py::class_(m, "DynamicClass", py::dynamic_attr()).def(py::init()); + + class CppDerivedDynamicClass : public DynamicClass {}; + py::class_(m, "CppDerivedDynamicClass").def(py::init()); + + // test_dynamic_attr_dealloc_frees_dict_contents + // Regression test: pybind11_object_dealloc() must call PyObject_ClearManagedDict() + // before tp_free() so that objects stored in a py::dynamic_attr() instance __dict__ + // have their refcounts decremented when the pybind11 object is freed. On Python 3.14+ + // tp_free no longer implicitly clears the managed dict, causing permanent leaks. + m.def("make_dynamic_attr_with_capsule", []() -> py::object { + s_dynamic_attr_capsule_freed = false; + auto *dummy = new int(0); + py::capsule cap(dummy, [](void *ptr) { + delete static_cast(ptr); + s_dynamic_attr_capsule_freed = true; + }); + py::object obj = py::cast(new DynamicClass(), py::return_value_policy::take_ownership); + obj.attr("data") = cap; + return obj; + }); + m.def("is_dynamic_attr_capsule_freed", []() { return s_dynamic_attr_capsule_freed; }); +#endif + + // test_bad_arg_default + // Issue/PR #648: bad arg default debugging output +#if defined(PYBIND11_DETAILED_ERROR_MESSAGES) + m.attr("detailed_error_messages_enabled") = true; +#else + m.attr("detailed_error_messages_enabled") = false; +#endif + m.def("bad_arg_def_named", [] { + auto m = py::module_::import("pybind11_tests"); + m.def( + "should_fail", + [](int, UnregisteredType) {}, + py::arg(), + py::arg("a") = UnregisteredType()); + }); + m.def("bad_arg_def_unnamed", [] { + auto m = py::module_::import("pybind11_tests"); + m.def( + "should_fail", + [](int, UnregisteredType) {}, + py::arg(), + py::arg() = UnregisteredType()); + }); + + // [workaround(intel)] ICC 20/21 breaks with py::arg().stuff, using py::arg{}.stuff works. + + // test_accepts_none + py::class_>(m, "NoneTester").def(py::init<>()); + m.def("no_none1", &none1, py::arg{}.none(false)); + m.def("no_none2", &none2, py::arg{}.none(false)); + m.def("no_none3", &none3, py::arg{}.none(false)); + m.def("no_none4", &none4, py::arg{}.none(false)); + m.def("no_none5", &none5, py::arg{}.none(false)); + m.def("ok_none1", &none1); + m.def("ok_none2", &none2, py::arg{}.none(true)); + m.def("ok_none3", &none3); + m.def("ok_none4", &none4, py::arg{}.none(true)); + m.def("ok_none5", &none5); + + m.def("no_none_kwarg", &none2, "a"_a.none(false)); + m.def("no_none_kwarg_kw_only", &none2, py::kw_only(), "a"_a.none(false)); + + // test_casts_none + // Issue #2778: implicit casting from None to object (not pointer) + py::class_(m, "NoneCastTester") + .def(py::init<>()) + .def(py::init()) + .def(py::init([](py::none const &) { return NoneCastTester{}; })); + py::implicitly_convertible(); + m.def("ok_obj_or_none", [](NoneCastTester const &foo) { return foo.answer; }); + + // test_str_issue + // Issue #283: __str__ called on uninitialized instance when constructor arguments invalid + py::class_(m, "StrIssue") + .def(py::init()) + .def(py::init<>()) + .def("__str__", + [](const StrIssue &si) { return "StrIssue[" + std::to_string(si.val) + "]"; }); + + // test_unregistered_base_implementations + // + // Issues #854/910: incompatible function args when member function/pointer is in unregistered + // base class The methods and member pointers below actually resolve to members/pointers in + // UnregisteredBase; before this test/fix they would be registered via lambda with a first + // argument of an unregistered type, and thus uncallable. + py::class_(m, "RegisteredDerived") + .def(py::init<>()) + .def("do_nothing", &RegisteredDerived::do_nothing) + .def("increase_value", &RegisteredDerived::increase_value) + .def_readwrite("rw_value", &RegisteredDerived::rw_value) + .def_readonly("ro_value", &RegisteredDerived::ro_value) + // Uncommenting the next line should trigger a static_assert: + // .def_readwrite("fails", &UserType::value) + // Uncommenting the next line should trigger a static_assert: + // .def_readonly("fails", &UserType::value) + .def_property("rw_value_prop", &RegisteredDerived::get_int, &RegisteredDerived::set_int) + .def_property_readonly("ro_value_prop", &RegisteredDerived::get_double) + // This one is in the registered class: + .def("sum", &RegisteredDerived::sum); + + using Adapted + = decltype(py::method_adaptor(&RegisteredDerived::do_nothing)); + static_assert(std::is_same::value, ""); + + // test_noexcept_base (issue #2234) + // In C++17, noexcept is part of the function type. Binding a noexcept method from an + // unregistered base class must resolve `self` to the derived type, not the base type. + py::class_(m, "NoexceptDerived") + .def(py::init<>()) + // cpp_function(Return (Class::*)(Args...) const noexcept, ...) + .def("value", &NoexceptDerived::value) + // cpp_function(Return (Class::*)(Args...) noexcept, ...) + .def("set_value", &NoexceptDerived::set_value) + // cpp_function(Return (Class::*)(Args...) & noexcept, ...) + .def("increment", &NoexceptDerived::increment) + // cpp_function(Return (Class::*)(Args...) const & noexcept, ...) + .def("capped_value", &NoexceptDerived::capped_value); + + // test_rvalue_ref_qualified_methods: rvalue-ref-qualified methods from an unregistered base. + // method_adaptor must rebind &&/const&& member pointers to the derived type. + py::class_(m, "RValueRefDerived") + .def(py::init<>()) + // cpp_function(Return (Class::*)(Args...) &&, ...) + .def("take", &RValueRefDerived::take) + // cpp_function(Return (Class::*)(Args...) const &&, ...) + .def("peek", &RValueRefDerived::peek) +#ifdef __cpp_noexcept_function_type + // cpp_function(Return (Class::*)(Args...) && noexcept, ...) + .def("take_noexcept", &RValueRefDerived::take_noexcept) + // cpp_function(Return (Class::*)(Args...) const && noexcept, ...) + .def("peek_noexcept", &RValueRefDerived::peek_noexcept) +#endif + ; + + // Verify that &&-qualified methods cannot be called on lvalues, only on rvalues. + // This confirms that the cpp_function lambda must use std::move(*c).*f, not c->*f. +#if __cplusplus >= 201703L + static_assert(!std::is_invocable::value, + "&&-qualified method must not be callable on lvalue"); + static_assert(std::is_invocable::value, + "&&-qualified method must be callable on rvalue"); +#endif + + // Verify method_adaptor preserves &&/const&& qualifiers when rebinding. + using AdaptedRRef = decltype(py::method_adaptor(&RValueRefDerived::take)); + static_assert(std::is_same::value, ""); + using AdaptedConstRRef + = decltype(py::method_adaptor(&RValueRefDerived::peek)); + static_assert(std::is_same::value, ""); + +#ifdef __cpp_noexcept_function_type + // method_adaptor must also handle noexcept member function pointers (issue #2234). + // Verify the noexcept specifier is preserved in the resulting Derived pointer type. + using AdaptedConstNoexcept + = decltype(py::method_adaptor(&NoexceptDerived::value)); + static_assert( + std::is_same::value, ""); + using AdaptedNoexcept + = decltype(py::method_adaptor(&NoexceptDerived::set_value)); + static_assert(std::is_same::value, + ""); + using AdaptedRRefNoexcept + = decltype(py::method_adaptor(&RValueRefDerived::take_noexcept)); + static_assert(std::is_same < AdaptedRRefNoexcept, + std::string (RValueRefDerived::*)() && noexcept > ::value, + ""); + using AdaptedConstRRefNoexcept + = decltype(py::method_adaptor(&RValueRefDerived::peek_noexcept)); + static_assert(std::is_same < AdaptedConstRRefNoexcept, + int (RValueRefDerived::*)() const && noexcept > ::value, + ""); +#endif + + // test_noexcept_overload_cast (issue #2234) + // overload_cast must have noexcept operator() overloads so it can resolve noexcept methods. +#ifdef PYBIND11_OVERLOAD_CAST + py::class_(m, "NoexceptOverloaded") + .def(py::init<>()) + // overload_cast_impl::operator()(Return (Class::*)(Args...) noexcept, false_type) + .def("method", py::overload_cast(&NoexceptOverloaded::method)) + // overload_cast_impl::operator()(Return (Class::*)(Args...) const noexcept, true_type) + .def("method_const", py::overload_cast(&NoexceptOverloaded::method, py::const_)) + // overload_cast_impl::operator()(Return (Class::*)(Args...) noexcept, false_type) float + .def("method_float", py::overload_cast(&NoexceptOverloaded::method)); + // overload_cast_impl::operator()(Return (*)(Args...) noexcept) + m.def("noexcept_free_func", py::overload_cast(noexcept_free_func)); + m.def("noexcept_free_func_float", py::overload_cast(noexcept_free_func)); + + py::class_(m, "RefQualifiedOverloaded") + .def(py::init<>()) + // overload_cast_impl::operator()(Return (Class::*)(Args...) &, false_type) + .def("method_lref", py::overload_cast(&RefQualifiedOverloaded::method)) + // overload_cast_impl::operator()(Return (Class::*)(Args...) const &, true_type) + .def("method_const_lref", + py::overload_cast(&RefQualifiedOverloaded::method, py::const_)) + // overload_cast_impl::operator()(Return (Class::*)(Args...) &&, false_type) + .def("method_rref", py::overload_cast(&RefQualifiedOverloaded::method)) + // overload_cast_impl::operator()(Return (Class::*)(Args...) const &&, true_type) + .def("method_const_rref", + py::overload_cast(&RefQualifiedOverloaded::method, py::const_)) +# ifdef __cpp_noexcept_function_type + // overload_cast_impl::operator()(Return (Class::*)(Args...) & noexcept, false_type) + .def("method_lref_noexcept", py::overload_cast(&RefQualifiedOverloaded::method)) + // overload_cast_impl::operator()(Return (Class::*)(Args...) const & noexcept, true_type) + .def("method_const_lref_noexcept", + py::overload_cast(&RefQualifiedOverloaded::method, py::const_)) + // overload_cast_impl::operator()(Return (Class::*)(Args...) && noexcept, false_type) + .def("method_rref_noexcept", py::overload_cast(&RefQualifiedOverloaded::method)) + // overload_cast_impl::operator()(Return (Class::*)(Args...) const && noexcept, true_type) + .def("method_const_rref_noexcept", + py::overload_cast(&RefQualifiedOverloaded::method, py::const_)) +# endif + ; +#else + // Fallback using explicit static_cast for C++11/14 + py::class_(m, "NoexceptOverloaded") + .def(py::init<>()) + .def("method", + static_cast(&NoexceptOverloaded::method)) + .def("method_const", + static_cast(&NoexceptOverloaded::method)) + .def("method_float", + static_cast(&NoexceptOverloaded::method)); + m.def("noexcept_free_func", static_cast(noexcept_free_func)); + m.def("noexcept_free_func_float", static_cast(noexcept_free_func)); + + py::class_(m, "RefQualifiedOverloaded") + .def(py::init<>()) + .def("method_lref", + static_cast( + &RefQualifiedOverloaded::method)) + .def("method_const_lref", + static_cast( + &RefQualifiedOverloaded::method)) + .def("method_rref", + static_cast( + &RefQualifiedOverloaded::method)) + .def("method_const_rref", + static_cast( + &RefQualifiedOverloaded::method)) +# ifdef __cpp_noexcept_function_type + .def("method_lref_noexcept", + static_cast( + &RefQualifiedOverloaded::method)) + .def("method_const_lref_noexcept", + static_cast( + &RefQualifiedOverloaded::method)) + .def("method_rref_noexcept", + static_cast < py::str (RefQualifiedOverloaded::*)(double) + && noexcept > (&RefQualifiedOverloaded::method)) + .def("method_const_rref_noexcept", + static_cast < py::str (RefQualifiedOverloaded::*)(double) const && noexcept + > (&RefQualifiedOverloaded::method)) +# endif + ; +#endif + + // test_methods_and_attributes + py::class_(m, "RefQualified") + .def(py::init<>()) + .def_readonly("value", &RefQualified::value) + .def("refQualified", &RefQualified::refQualified) + .def("constRefQualified", &RefQualified::constRefQualified); + + py::class_(m, "RValueRefParam") + .def(py::init<>()) + .def("func1", &RValueRefParam::func1) + .def("func2", &RValueRefParam::func2) + .def("func3", &RValueRefParam::func3) + .def("func4", &RValueRefParam::func4); + + pybind11_tests::exercise_is_setter::add_bindings(m); +} diff --git a/external_libraries/pybind11/tests/test_methods_and_attributes.py b/external_libraries/pybind11/tests/test_methods_and_attributes.py new file mode 100644 index 00000000..c0943742 --- /dev/null +++ b/external_libraries/pybind11/tests/test_methods_and_attributes.py @@ -0,0 +1,715 @@ +from __future__ import annotations + +import sys + +import pytest + +import env +from pybind11_tests import ConstructorStats, defined___cpp_noexcept_function_type +from pybind11_tests import methods_and_attributes as m + +NO_GETTER_MSG = ( + "unreadable attribute" if sys.version_info < (3, 11) else "object has no getter" +) +NO_SETTER_MSG = ( + "can't set attribute" if sys.version_info < (3, 11) else "object has no setter" +) +NO_DELETER_MSG = ( + "can't delete attribute" if sys.version_info < (3, 11) else "object has no deleter" +) + + +def test_self_only_pos_only(): + assert ( + m.ExampleMandA.__str__.__doc__ + == "__str__(self: pybind11_tests.methods_and_attributes.ExampleMandA, /) -> str\n" + ) + + +def test_methods_and_attributes(): + instance1 = m.ExampleMandA() + instance2 = m.ExampleMandA(32) + + instance1.add1(instance2) + instance1.add2(instance2) + instance1.add3(instance2) + instance1.add4(instance2) + instance1.add5(instance2) + instance1.add6(32) + instance1.add7(32) + instance1.add8(32) + instance1.add9(32) + instance1.add10(32) + + assert str(instance1) == "ExampleMandA[value=320]" + assert str(instance2) == "ExampleMandA[value=32]" + assert str(instance1.self1()) == "ExampleMandA[value=320]" + assert str(instance1.self2()) == "ExampleMandA[value=320]" + assert str(instance1.self3()) == "ExampleMandA[value=320]" + assert str(instance1.self4()) == "ExampleMandA[value=320]" + assert str(instance1.self5()) == "ExampleMandA[value=320]" + + assert instance1.internal1() == 320 + assert instance1.internal2() == 320 + assert instance1.internal3() == 320 + assert instance1.internal4() == 320 + assert instance1.internal5() == 320 + + assert instance1.overloaded() == "()" + assert instance1.overloaded(0) == "(int)" + assert instance1.overloaded(1, 1.0) == "(int, float)" + assert instance1.overloaded(2.0, 2) == "(float, int)" + assert instance1.overloaded(3, 3) == "(int, int)" + assert instance1.overloaded(4.0, 4.0) == "(float, float)" + assert instance1.overloaded_const(-3) == "(int) const" + assert instance1.overloaded_const(5, 5.0) == "(int, float) const" + assert instance1.overloaded_const(6.0, 6) == "(float, int) const" + assert instance1.overloaded_const(7, 7) == "(int, int) const" + assert instance1.overloaded_const(8.0, 8.0) == "(float, float) const" + assert instance1.overloaded_float(1, 1) == "(float, float)" + assert instance1.overloaded_float(1, 1.0) == "(float, float)" + assert instance1.overloaded_float(1.0, 1) == "(float, float)" + assert instance1.overloaded_float(1.0, 1.0) == "(float, float)" + + assert instance1.value == 320 + instance1.value = 100 + assert str(instance1) == "ExampleMandA[value=100]" + + if env.GRAALPY: + pytest.skip("ConstructorStats is incompatible with GraalPy.") + + cstats = ConstructorStats.get(m.ExampleMandA) + assert cstats.alive() == 2 + del instance1, instance2 + assert cstats.alive() == 0 + assert cstats.values() == ["32"] + assert cstats.default_constructions == 1 + assert cstats.copy_constructions == 2 + assert cstats.move_constructions >= 2 + assert cstats.copy_assignments == 0 + assert cstats.move_assignments == 0 + + +def test_copy_method(): + """Issue #443: calling copied methods fails in Python 3""" + + m.ExampleMandA.add2c = m.ExampleMandA.add2 + m.ExampleMandA.add2d = m.ExampleMandA.add2b + a = m.ExampleMandA(123) + assert a.value == 123 + a.add2(m.ExampleMandA(-100)) + assert a.value == 23 + a.add2b(m.ExampleMandA(20)) + assert a.value == 43 + a.add2c(m.ExampleMandA(6)) + assert a.value == 49 + a.add2d(m.ExampleMandA(-7)) + assert a.value == 42 + + +def test_properties(): + instance = m.TestProperties() + + assert instance.def_readonly == 1 + with pytest.raises(AttributeError): + instance.def_readonly = 2 + + instance.def_readwrite = 2 + assert instance.def_readwrite == 2 + + assert instance.def_property_readonly == 2 + with pytest.raises(AttributeError): + instance.def_property_readonly = 3 + + instance.def_property = 3 + assert instance.def_property == 3 + + with pytest.raises(AttributeError) as excinfo: + dummy = instance.def_property_writeonly # unused var + assert NO_GETTER_MSG in str(excinfo.value) + + instance.def_property_writeonly = 4 + assert instance.def_property_readonly == 4 + + with pytest.raises(AttributeError) as excinfo: + dummy = instance.def_property_impossible # noqa: F841 unused var + assert NO_GETTER_MSG in str(excinfo.value) + + with pytest.raises(AttributeError) as excinfo: + instance.def_property_impossible = 5 + assert NO_SETTER_MSG in str(excinfo.value) + + +def test_static_properties(): + assert m.TestProperties.def_readonly_static == 1 + with pytest.raises(AttributeError) as excinfo: + m.TestProperties.def_readonly_static = 2 + assert NO_SETTER_MSG in str(excinfo.value) + + m.TestProperties.def_readwrite_static = 2 + assert m.TestProperties.def_readwrite_static == 2 + + with pytest.raises(AttributeError) as excinfo: + dummy = m.TestProperties.def_writeonly_static # unused var + assert NO_GETTER_MSG in str(excinfo.value) + + m.TestProperties.def_writeonly_static = 3 + assert m.TestProperties.def_readonly_static == 3 + + assert m.TestProperties.def_property_readonly_static == 3 + with pytest.raises(AttributeError) as excinfo: + m.TestProperties.def_property_readonly_static = 99 + assert NO_SETTER_MSG in str(excinfo.value) + + m.TestProperties.def_property_static = 4 + assert m.TestProperties.def_property_static == 4 + + with pytest.raises(AttributeError) as excinfo: + dummy = m.TestProperties.def_property_writeonly_static + assert NO_GETTER_MSG in str(excinfo.value) + + m.TestProperties.def_property_writeonly_static = 5 + assert m.TestProperties.def_property_static == 5 + + # Static property read and write via instance + instance = m.TestProperties() + + m.TestProperties.def_readwrite_static = 0 + assert m.TestProperties.def_readwrite_static == 0 + assert instance.def_readwrite_static == 0 + + instance.def_readwrite_static = 2 + assert m.TestProperties.def_readwrite_static == 2 + assert instance.def_readwrite_static == 2 + + with pytest.raises(AttributeError) as excinfo: + dummy = instance.def_property_writeonly_static # noqa: F841 unused var + assert NO_GETTER_MSG in str(excinfo.value) + + instance.def_property_writeonly_static = 4 + assert instance.def_property_static == 4 + + # It should be possible to override properties in derived classes + assert m.TestPropertiesOverride().def_readonly == 99 + assert m.TestPropertiesOverride.def_readonly_static == 99 + + # Only static attributes can be deleted + del m.TestPropertiesOverride.def_readonly_static + assert hasattr(m.TestPropertiesOverride, "def_readonly_static") + assert ( + m.TestPropertiesOverride.def_readonly_static + is m.TestProperties.def_readonly_static + ) + assert "def_readonly_static" not in m.TestPropertiesOverride.__dict__ + properties_override = m.TestPropertiesOverride() + with pytest.raises(AttributeError) as excinfo: + del properties_override.def_readonly + assert NO_DELETER_MSG in str(excinfo.value) + + +def test_static_cls(): + """Static property getter and setters expect the type object as the their only argument""" + + instance = m.TestProperties() + assert m.TestProperties.static_cls is m.TestProperties + assert instance.static_cls is m.TestProperties + + def check_self(self): + assert self is m.TestProperties + + m.TestProperties.static_cls = check_self + instance.static_cls = check_self + + +def test_metaclass_override(): + """Overriding pybind11's default metaclass changes the behavior of `static_property`""" + + assert type(m.ExampleMandA).__name__ == "pybind11_type" + assert type(m.MetaclassOverride).__name__ == "type" + + assert m.MetaclassOverride.readonly == 1 + assert ( + type(m.MetaclassOverride.__dict__["readonly"]).__name__ + == "pybind11_static_property" + ) + + # Regular `type` replaces the property instead of calling `__set__()` + m.MetaclassOverride.readonly = 2 + assert m.MetaclassOverride.readonly == 2 + assert isinstance(m.MetaclassOverride.__dict__["readonly"], int) + + +def test_no_mixed_overloads(): + from pybind11_tests import detailed_error_messages_enabled + + with pytest.raises(RuntimeError) as excinfo: + m.ExampleMandA.add_mixed_overloads1() + assert ( + str(excinfo.value) + == "overloading a method with both static and instance methods is not supported; " + + ( + "#define PYBIND11_DETAILED_ERROR_MESSAGES or compile in debug mode for more details" + if not detailed_error_messages_enabled + else "error while attempting to bind static method ExampleMandA.overload_mixed1" + "(arg0: typing.SupportsFloat | typing.SupportsIndex) -> str" + ) + ) + + with pytest.raises(RuntimeError) as excinfo: + m.ExampleMandA.add_mixed_overloads2() + assert ( + str(excinfo.value) + == "overloading a method with both static and instance methods is not supported; " + + ( + "#define PYBIND11_DETAILED_ERROR_MESSAGES or compile in debug mode for more details" + if not detailed_error_messages_enabled + else "error while attempting to bind instance method ExampleMandA.overload_mixed2" + "(self: pybind11_tests.methods_and_attributes.ExampleMandA, arg0: typing.SupportsInt | typing.SupportsIndex, arg1: typing.SupportsInt | typing.SupportsIndex)" + " -> str" + ) + ) + + +@pytest.mark.parametrize("access", ["ro", "rw", "static_ro", "static_rw"]) +def test_property_return_value_policies(access): + obj = m.TestPropRVP() if not access.startswith("static") else m.TestPropRVP + + ref = getattr(obj, access + "_ref") + assert ref.value == 1 + ref.value = 2 + assert getattr(obj, access + "_ref").value == 2 + ref.value = 1 # restore original value for static properties + + copy = getattr(obj, access + "_copy") + assert copy.value == 1 + copy.value = 2 + assert getattr(obj, access + "_copy").value == 1 + + copy = getattr(obj, access + "_func") + assert copy.value == 1 + copy.value = 2 + assert getattr(obj, access + "_func").value == 1 + + +def test_property_rvalue_policy(): + """When returning an rvalue, the return value policy is automatically changed from + `reference(_internal)` to `move`. The following would not work otherwise.""" + + instance = m.TestPropRVP() + o = instance.rvalue + assert o.value == 1 + + os = m.TestPropRVP.static_rvalue + assert os.value == 1 + + +# https://foss.heptapod.net/pypy/pypy/-/issues/2447 +@pytest.mark.xfail("env.PYPY") +@pytest.mark.skipif( + sys.version_info in ((3, 14, 0, "beta", 1), (3, 14, 0, "beta", 2)), + reason="3.14.0b1/2 managed dict bug: https://github.com/python/cpython/issues/133912", +) +def test_dynamic_attributes(): + instance = m.DynamicClass() + assert not hasattr(instance, "foo") + assert "foo" not in dir(instance) + + # Dynamically add attribute + instance.foo = 42 + assert hasattr(instance, "foo") + assert instance.foo == 42 + assert "foo" in dir(instance) + + # __dict__ should be accessible and replaceable + assert "foo" in instance.__dict__ + instance.__dict__ = {"bar": True} + assert not hasattr(instance, "foo") + assert hasattr(instance, "bar") + + with pytest.raises(TypeError) as excinfo: + instance.__dict__ = [] + assert str(excinfo.value) == "__dict__ must be set to a dictionary, not a 'list'" + + if env.GRAALPY: + pytest.skip("ConstructorStats is incompatible with GraalPy.") + cstats = ConstructorStats.get(m.DynamicClass) + assert cstats.alive() == 1 + del instance + pytest.gc_collect() + assert cstats.alive() == 0 + + # Derived classes should work as well + class PythonDerivedDynamicClass(m.DynamicClass): + pass + + for cls in (m.CppDerivedDynamicClass, PythonDerivedDynamicClass): + derived = cls() + derived.foobar = 100 + assert derived.foobar == 100 + + assert cstats.alive() == 1 + del derived + pytest.gc_collect() + assert cstats.alive() == 0 + + +# https://foss.heptapod.net/pypy/pypy/-/issues/2447 +@pytest.mark.xfail("env.PYPY") +@pytest.mark.skipif("env.GRAALPY", reason="Cannot reliably trigger GC") +@pytest.mark.skipif( + sys.version_info in ((3, 14, 0, "beta", 1), (3, 14, 0, "beta", 2)), + reason="3.14.0b1/2 managed dict bug: https://github.com/python/cpython/issues/133912", +) +def test_cyclic_gc(): + # One object references itself + instance = m.DynamicClass() + instance.circular_reference = instance + + cstats = ConstructorStats.get(m.DynamicClass) + assert cstats.alive() == 1 + del instance + pytest.gc_collect() + assert cstats.alive() == 0 + + # Two object reference each other + i1 = m.DynamicClass() + i2 = m.DynamicClass() + i1.cycle = i2 + i2.cycle = i1 + + assert cstats.alive() == 2 + del i1, i2 + pytest.gc_collect() + assert cstats.alive() == 0 + + +@pytest.mark.xfail("env.PYPY", strict=False) +@pytest.mark.skipif("env.GRAALPY", reason="Cannot reliably trigger GC") +def test_dynamic_attr_dealloc_frees_dict_contents(): + """Regression: py::dynamic_attr() objects must free __dict__ contents on dealloc. + + pybind11_object_dealloc() did not call PyObject_ClearManagedDict() before tp_free(), + causing objects stored in __dict__ to have their refcounts permanently abandoned on + Python 3.14+ (where tp_free no longer implicitly clears the managed dict). + This caused capsule destructors to never run, leaking the underlying C++ data. + """ + instance = m.make_dynamic_attr_with_capsule() + assert not m.is_dynamic_attr_capsule_freed() + del instance + pytest.gc_collect() + assert m.is_dynamic_attr_capsule_freed() + + +def test_bad_arg_default(msg): + from pybind11_tests import detailed_error_messages_enabled + + with pytest.raises(RuntimeError) as excinfo: + m.bad_arg_def_named() + assert msg(excinfo.value) == ( + "arg(): could not convert default argument 'a: UnregisteredType' in function " + "'should_fail' into a Python object (type not registered yet?)" + if detailed_error_messages_enabled + else "arg(): could not convert default argument into a Python object (type not registered " + "yet?). #define PYBIND11_DETAILED_ERROR_MESSAGES or compile in debug mode for more information." + ) + + with pytest.raises(RuntimeError) as excinfo: + m.bad_arg_def_unnamed() + assert msg(excinfo.value) == ( + "arg(): could not convert default argument 'UnregisteredType' in function " + "'should_fail' into a Python object (type not registered yet?)" + if detailed_error_messages_enabled + else "arg(): could not convert default argument into a Python object (type not registered " + "yet?). #define PYBIND11_DETAILED_ERROR_MESSAGES or compile in debug mode for more information." + ) + + +def test_accepts_none(msg): + a = m.NoneTester() + assert m.no_none1(a) == 42 + assert m.no_none2(a) == 42 + assert m.no_none3(a) == 42 + assert m.no_none4(a) == 42 + assert m.no_none5(a) == 42 + assert m.ok_none1(a) == 42 + assert m.ok_none2(a) == 42 + assert m.ok_none3(a) == 42 + assert m.ok_none4(a) == 42 + assert m.ok_none5(a) == 42 + + with pytest.raises(TypeError) as excinfo: + m.no_none1(None) + assert "incompatible function arguments" in str(excinfo.value) + with pytest.raises(TypeError) as excinfo: + m.no_none2(None) + assert "incompatible function arguments" in str(excinfo.value) + with pytest.raises(TypeError) as excinfo: + m.no_none3(None) + assert "incompatible function arguments" in str(excinfo.value) + with pytest.raises(TypeError) as excinfo: + m.no_none4(None) + assert "incompatible function arguments" in str(excinfo.value) + with pytest.raises(TypeError) as excinfo: + m.no_none5(None) + assert "incompatible function arguments" in str(excinfo.value) + + # The first one still raises because you can't pass None as a lvalue reference arg: + with pytest.raises(TypeError) as excinfo: + assert m.ok_none1(None) == -1 + assert ( + msg(excinfo.value) + == """ + ok_none1(): incompatible function arguments. The following argument types are supported: + 1. (arg0: m.methods_and_attributes.NoneTester) -> int + + Invoked with: None + """ + ) + + # The rest take the argument as pointer or holder, and accept None: + assert m.ok_none2(None) == -1 + assert m.ok_none3(None) == -1 + assert m.ok_none4(None) == -1 + assert m.ok_none5(None) == -1 + + with pytest.raises(TypeError) as excinfo: + m.no_none_kwarg(None) + assert "incompatible function arguments" in str(excinfo.value) + with pytest.raises(TypeError) as excinfo: + m.no_none_kwarg(a=None) + assert "incompatible function arguments" in str(excinfo.value) + with pytest.raises(TypeError) as excinfo: + m.no_none_kwarg_kw_only(None) + assert "incompatible function arguments" in str(excinfo.value) + with pytest.raises(TypeError) as excinfo: + m.no_none_kwarg_kw_only(a=None) + assert "incompatible function arguments" in str(excinfo.value) + + +def test_casts_none(): + """#2778: implicit casting from None to object (not pointer)""" + a = m.NoneCastTester() + assert m.ok_obj_or_none(a) == -1 + a = m.NoneCastTester(4) + assert m.ok_obj_or_none(a) == 4 + a = m.NoneCastTester(None) + assert m.ok_obj_or_none(a) == -1 + assert m.ok_obj_or_none(None) == -1 + + +def test_str_issue(msg): + """#283: __str__ called on uninitialized instance when constructor arguments invalid""" + + assert str(m.StrIssue(3)) == "StrIssue[3]" + + with pytest.raises(TypeError) as excinfo: + str(m.StrIssue("no", "such", "constructor")) + assert ( + msg(excinfo.value) + == """ + __init__(): incompatible constructor arguments. The following argument types are supported: + 1. m.methods_and_attributes.StrIssue(arg0: typing.SupportsInt | typing.SupportsIndex) + 2. m.methods_and_attributes.StrIssue() + + Invoked with: 'no', 'such', 'constructor' + """ + ) + + +def test_unregistered_base_implementations(): + a = m.RegisteredDerived() + a.do_nothing() + assert a.rw_value == 42 + assert a.ro_value == 1.25 + a.rw_value += 5 + assert a.sum() == 48.25 + a.increase_value() + assert a.rw_value == 48 + assert a.ro_value == 1.5 + assert a.sum() == 49.5 + assert a.rw_value_prop == 48 + a.rw_value_prop += 1 + assert a.rw_value_prop == 49 + a.increase_value() + assert a.ro_value_prop == 1.75 + + +def test_noexcept_base(): + """Test issue #2234: binding noexcept methods inherited from an unregistered base class. + + In C++17 noexcept is part of the function type, so &Derived::noexcept_method resolves + to a Base member-function pointer with noexcept specifier. pybind11 must use the Derived + type as `self`, not the Base type, otherwise the call raises TypeError at runtime. + + Covers all four new cpp_function constructor specialisations: + - Return (Class::*)(Args...) noexcept (set_value) + - Return (Class::*)(Args...) const noexcept (value) + - Return (Class::*)(Args...) & noexcept (increment) + - Return (Class::*)(Args...) const & noexcept (capped_value) + """ + obj = m.NoexceptDerived() + # const noexcept + assert obj.value() == 99 + # noexcept (non-const) + obj.set_value(7) + assert obj.value() == 7 + # & noexcept (non-const lvalue ref-qualified) + obj.increment() + assert obj.value() == 8 + # const & noexcept (const lvalue ref-qualified) + assert obj.capped_value() == 8 + obj.set_value(200) + assert obj.capped_value() == 100 # capped at 100 + + +def test_rvalue_ref_qualified_methods(): + """Test that rvalue-ref-qualified (&&/const&&) methods from an unregistered base bind + correctly with `self` resolved to the derived type. + + take() moves m_payload out on each call, so the second call returns "". + This confirms that the cpp_function lambda uses std::move(*c).*f rather than c->*f. + + Covers: + - Return (Class::*)(Args...) && (take) + - Return (Class::*)(Args...) const && (peek) + """ + obj = m.RValueRefDerived() + # && moves m_payload: first call gets the value, second gets empty string + assert obj.take() == "rref_payload" + assert obj.take() == "" + # const && doesn't move: peek() is stable across calls + assert obj.peek() == 77 + assert obj.peek() == 77 + + +@pytest.mark.skipif( + not defined___cpp_noexcept_function_type, + reason="Requires __cpp_noexcept_function_type", +) +def test_noexcept_rvalue_ref_qualified_methods(): + """Test noexcept rvalue-ref-qualified methods from an unregistered base. + + Covers: + - Return (Class::*)(Args...) && noexcept (take_noexcept) + - Return (Class::*)(Args...) const && noexcept (peek_noexcept) + """ + obj = m.RValueRefDerived() + assert obj.take_noexcept() == "rref_payload" + assert obj.take_noexcept() == "" + assert obj.peek_noexcept() == 77 + assert obj.peek_noexcept() == 77 + + +def test_noexcept_overload_cast(): + """Test issue #2234: overload_cast must handle noexcept member and free function pointers. + + In C++17 noexcept is part of the function type, so overload_cast_impl needs dedicated + operator() overloads for noexcept free functions and non-const/const member functions. + """ + obj = m.NoexceptOverloaded() + # overload_cast_impl::operator()(Return (Class::*)(Args...) noexcept, false_type) + assert obj.method(1) == "(int)" + # overload_cast_impl::operator()(Return (Class::*)(Args...) const noexcept, true_type) + assert obj.method_const(2) == "(int) const" + # overload_cast_impl::operator()(Return (Class::*)(Args...) noexcept, false_type) float + assert obj.method_float(3.0) == "(float)" + # overload_cast_impl::operator()(Return (*)(Args...) noexcept) + assert m.noexcept_free_func(10) == 11 + assert m.noexcept_free_func_float(10.0) == 12 + + +def test_ref_qualified_overload_cast(): + """Test issue #2234 follow-up: overload_cast with ref-qualified member pointers. + + Covers: + - overload_cast_impl::operator()(Return (Class::*)(Args...) &, false_type) + - overload_cast_impl::operator()(Return (Class::*)(Args...) const &, true_type) + - overload_cast_impl::operator()(Return (Class::*)(Args...) &&, false_type) + - overload_cast_impl::operator()(Return (Class::*)(Args...) const &&, true_type) + - overload_cast_impl::operator()(Return (Class::*)(Args...) & noexcept, false_type) + - overload_cast_impl::operator()(Return (Class::*)(Args...) const & noexcept, true_type) + - overload_cast_impl::operator()(Return (Class::*)(Args...) && noexcept, false_type) + - overload_cast_impl::operator()(Return (Class::*)(Args...) const && noexcept, true_type) + """ + obj = m.RefQualifiedOverloaded() + assert obj.method_lref(1) == "(int) &" + assert obj.method_const_lref(1) == "(int) const &" + assert obj.method_rref(1.0) == "(float) &&" + assert obj.method_const_rref(1.0) == "(float) const &&" + + +@pytest.mark.skipif( + not defined___cpp_noexcept_function_type, + reason="Requires __cpp_noexcept_function_type", +) +def test_noexcept_ref_qualified_overload_cast(): + """Test issue #2234 follow-up: overload_cast with noexcept ref-qualified member pointers. + + Covers: + - overload_cast_impl::operator()(Return (Class::*)(Args...) & noexcept, false_type) + - overload_cast_impl::operator()(Return (Class::*)(Args...) const & noexcept, true_type) + - overload_cast_impl::operator()(Return (Class::*)(Args...) && noexcept, false_type) + - overload_cast_impl::operator()(Return (Class::*)(Args...) const && noexcept, true_type) + """ + obj = m.RefQualifiedOverloaded() + assert obj.method_lref_noexcept(1) == "(long) & noexcept" + assert obj.method_const_lref_noexcept(1) == "(long) const & noexcept" + assert obj.method_rref_noexcept(1.0) == "(double) && noexcept" + assert obj.method_const_rref_noexcept(1.0) == "(double) const && noexcept" + + +def test_ref_qualified(): + """Tests that explicit lvalue ref-qualified methods can be called just like their + non ref-qualified counterparts.""" + + r = m.RefQualified() + assert r.value == 0 + r.refQualified(17) + assert r.value == 17 + assert r.constRefQualified(23) == 40 + + +def test_overload_ordering(): + "Check to see if the normal overload order (first defined) and prepend overload order works" + assert m.overload_order("string") == 1 + assert m.overload_order(0) == 4 + + assert ( + "1. overload_order(arg0: typing.SupportsInt | typing.SupportsIndex) -> int" + in m.overload_order.__doc__ + ) + assert "2. overload_order(arg0: str) -> int" in m.overload_order.__doc__ + assert "3. overload_order(arg0: str) -> int" in m.overload_order.__doc__ + assert ( + "4. overload_order(arg0: typing.SupportsInt | typing.SupportsIndex) -> int" + in m.overload_order.__doc__ + ) + + with pytest.raises(TypeError) as err: + m.overload_order(1.1) + + assert "1. (arg0: typing.SupportsInt | typing.SupportsIndex) -> int" in str( + err.value + ) + assert "2. (arg0: str) -> int" in str(err.value) + assert "3. (arg0: str) -> int" in str(err.value) + assert "4. (arg0: typing.SupportsInt | typing.SupportsIndex) -> int" in str( + err.value + ) + + +def test_rvalue_ref_param(): + r = m.RValueRefParam() + assert r.func1("123") == 3 + assert r.func2("1234") == 4 + assert r.func3("12345") == 5 + assert r.func4("123456") == 6 + + +def test_is_setter(): + fld = m.exercise_is_setter.Field() + assert fld.int_value == -99 + setter_return = fld.int_value = 100 + assert isinstance(setter_return, int) + assert setter_return == 100 + assert fld.int_value == 100 diff --git a/external_libraries/pybind11/tests/test_modules.cpp b/external_libraries/pybind11/tests/test_modules.cpp new file mode 100644 index 00000000..842a3bc4 --- /dev/null +++ b/external_libraries/pybind11/tests/test_modules.cpp @@ -0,0 +1,124 @@ +/* + tests/test_modules.cpp -- nested modules, importing modules, and + internal references + + Copyright (c) 2016 Wenzel Jakob + + All rights reserved. Use of this source code is governed by a + BSD-style license that can be found in the LICENSE file. +*/ + +#include "constructor_stats.h" +#include "pybind11_tests.h" + +TEST_SUBMODULE(modules, m) { + // test_nested_modules + // This is intentionally "py::module" to verify it still can be used in place of "py::module_" + py::module m_sub = m.def_submodule("subsubmodule"); + m_sub.def("submodule_func", []() { return "submodule_func()"; }); + + // test_reference_internal + class A { + public: + explicit A(int v) : v(v) { print_created(this, v); } + ~A() { print_destroyed(this); } + A(const A &) { print_copy_created(this); } + A &operator=(const A ©) { + print_copy_assigned(this); + v = copy.v; + return *this; + } + std::string toString() const { return "A[" + std::to_string(v) + "]"; } + + private: + int v; + }; + py::class_(m_sub, "A").def(py::init()).def("__repr__", &A::toString); + + class B { + public: + B() { print_default_created(this); } + ~B() { print_destroyed(this); } + B(const B &) { print_copy_created(this); } + B &operator=(const B ©) { + print_copy_assigned(this); + a1 = copy.a1; + a2 = copy.a2; + return *this; + } + A &get_a1() { return a1; } + A &get_a2() { return a2; } + + A a1{1}; + A a2{2}; + }; + py::class_(m_sub, "B") + .def(py::init<>()) + .def("get_a1", + &B::get_a1, + "Return the internal A 1", + py::return_value_policy::reference_internal) + .def("get_a2", + &B::get_a2, + "Return the internal A 2", + py::return_value_policy::reference_internal) + .def_readwrite("a1", &B::a1) // def_readonly uses an internal + // reference return policy by default + .def_readwrite("a2", &B::a2); + + // This is intentionally "py::module" to verify it still can be used in place of "py::module_" + m.attr("OD") = py::module::import("collections").attr("OrderedDict"); + + // test_duplicate_registration + // Registering two things with the same name + m.def("duplicate_registration", []() { + class Dupe1 {}; + class Dupe2 {}; + class Dupe3 {}; + class DupeException {}; + + // Go ahead and leak, until we have a non-leaking py::module_ constructor + auto dm = py::module_::create_extension_module("dummy", nullptr, new PyModuleDef); + auto failures = py::list(); + + py::class_(dm, "Dupe1"); + py::class_(dm, "Dupe2"); + dm.def("dupe1_factory", []() { return Dupe1(); }); + py::exception(dm, "DupeException"); + + try { + py::class_(dm, "Dupe1"); + failures.append("Dupe1 class"); + } catch (std::runtime_error &) { // NOLINT(bugprone-empty-catch) + } + try { + dm.def("Dupe1", []() { return Dupe1(); }); + failures.append("Dupe1 function"); + } catch (std::runtime_error &) { // NOLINT(bugprone-empty-catch) + } + try { + py::class_(dm, "dupe1_factory"); + failures.append("dupe1_factory"); + } catch (std::runtime_error &) { // NOLINT(bugprone-empty-catch) + } + try { + py::exception(dm, "Dupe2"); + failures.append("Dupe2"); + } catch (std::runtime_error &) { // NOLINT(bugprone-empty-catch) + } + try { + dm.def("DupeException", []() { return 30; }); + failures.append("DupeException1"); + } catch (std::runtime_error &) { // NOLINT(bugprone-empty-catch) + } + try { + py::class_(dm, "DupeException"); + failures.append("DupeException2"); + } catch (std::runtime_error &) { // NOLINT(bugprone-empty-catch) + } + + return failures; + }); + + m.def("def_submodule", [](py::module_ m, const char *name) { return m.def_submodule(name); }); +} diff --git a/external_libraries/pybind11/tests/test_modules.py b/external_libraries/pybind11/tests/test_modules.py new file mode 100644 index 00000000..ea8ca7e5 --- /dev/null +++ b/external_libraries/pybind11/tests/test_modules.py @@ -0,0 +1,146 @@ +from __future__ import annotations + +import builtins + +import pytest + +import env +from pybind11_tests import ConstructorStats +from pybind11_tests import modules as m +from pybind11_tests.modules import subsubmodule as ms + + +def test_nested_modules(): + import pybind11_tests + + assert pybind11_tests.__name__ == "pybind11_tests" + assert pybind11_tests.modules.__name__ == "pybind11_tests.modules" + assert ( + pybind11_tests.modules.subsubmodule.__name__ + == "pybind11_tests.modules.subsubmodule" + ) + assert m.__name__ == "pybind11_tests.modules" + assert ms.__name__ == "pybind11_tests.modules.subsubmodule" + assert m.__file__ == ms.__file__ + + assert ms.submodule_func() == "submodule_func()" + + +def test_reference_internal(): + b = ms.B() + assert str(b.get_a1()) == "A[1]" + assert str(b.a1) == "A[1]" + assert str(b.get_a2()) == "A[2]" + assert str(b.a2) == "A[2]" + + b.a1 = ms.A(42) + b.a2 = ms.A(43) + assert str(b.get_a1()) == "A[42]" + assert str(b.a1) == "A[42]" + assert str(b.get_a2()) == "A[43]" + assert str(b.a2) == "A[43]" + + if env.GRAALPY: + pytest.skip("ConstructorStats is incompatible with GraalPy.") + + astats, bstats = ConstructorStats.get(ms.A), ConstructorStats.get(ms.B) + assert astats.alive() == 2 + assert bstats.alive() == 1 + del b + assert astats.alive() == 0 + assert bstats.alive() == 0 + assert astats.values() == ["1", "2", "42", "43"] + assert bstats.values() == [] + assert astats.default_constructions == 0 + assert bstats.default_constructions == 1 + assert astats.copy_constructions == 0 + assert bstats.copy_constructions == 0 + # assert astats.move_constructions >= 0 # Don't invoke any + # assert bstats.move_constructions >= 0 # Don't invoke any + assert astats.copy_assignments == 2 + assert bstats.copy_assignments == 0 + assert astats.move_assignments == 0 + assert bstats.move_assignments == 0 + + +def test_importing(): + from collections import OrderedDict + + from pybind11_tests.modules import OD + + assert OD is OrderedDict + + +def test_reimport(): + import sys + + import pybind11_tests as x + + del sys.modules["pybind11_tests"] + + # exercise pybind11::detail::get_cached_module() + import pybind11_tests as y + + assert x is y + + +@pytest.mark.xfail( + "env.GRAALPY", + reason="TODO should be fixed on GraalPy side (failure was introduced by pr #5782)", +) +def test_pydoc(): + """Pydoc needs to be able to provide help() for everything inside a pybind11 module""" + import pydoc + + import pybind11_tests + + assert pybind11_tests.__name__ == "pybind11_tests" + assert pybind11_tests.__doc__ == "pybind11 test module" + assert pydoc.text.docmodule(pybind11_tests) + + +def test_module_handle_type_name(): + assert ( + m.def_submodule.__doc__ + == "def_submodule(arg0: types.ModuleType, arg1: str) -> types.ModuleType\n" + ) + + +def test_duplicate_registration(): + """Registering two things with the same name""" + + assert m.duplicate_registration() == [] + + +def test_builtin_key_type(): + """Test that all the keys in the builtin modules have type str. + + Previous versions of pybind11 would add a unicode key in python 2. + """ + assert all(type(k) == str for k in dir(builtins)) + + +@pytest.mark.xfail("env.PYPY", reason="PyModule_GetName()") +def test_def_submodule_failures(): + sm = m.def_submodule(m, b"ScratchSubModuleName") # Using bytes to show it works. + assert sm.__name__ == m.__name__ + "." + "ScratchSubModuleName" + malformed_utf8 = b"\x80" + if env.PYPY or env.GRAALPY: + # It is not worth the effort finding a trigger for a failure when running with PyPy. + pytest.skip("Sufficiently exercised on platforms other than PyPy/GraalPy.") + else: + # Meant to trigger PyModule_GetName() failure: + sm_name_orig = sm.__name__ + sm.__name__ = malformed_utf8 + try: + # We want to assert that a bad __name__ causes some kind of failure, although we do not want to exercise + # the internals of PyModule_GetName(). Currently all supported Python versions raise SystemError. If that + # changes in future Python versions, simply add the new expected exception types here. + with pytest.raises(SystemError): + m.def_submodule(sm, b"SubSubModuleName") + finally: + # Clean up to ensure nothing gets upset by a module with an invalid __name__. + sm.__name__ = sm_name_orig # Purely precautionary. + # Meant to trigger PyImport_AddModule() failure: + with pytest.raises(UnicodeDecodeError): + m.def_submodule(sm, malformed_utf8) diff --git a/external_libraries/pybind11/tests/test_multiple_inheritance.cpp b/external_libraries/pybind11/tests/test_multiple_inheritance.cpp new file mode 100644 index 00000000..5916ae90 --- /dev/null +++ b/external_libraries/pybind11/tests/test_multiple_inheritance.cpp @@ -0,0 +1,341 @@ +/* + tests/test_multiple_inheritance.cpp -- multiple inheritance, + implicit MI casts + + Copyright (c) 2016 Wenzel Jakob + + All rights reserved. Use of this source code is governed by a + BSD-style license that can be found in the LICENSE file. +*/ + +#include "constructor_stats.h" +#include "pybind11_tests.h" + +namespace { + +// Many bases for testing that multiple inheritance from many classes (i.e. requiring extra +// space for holder constructed flags) works. +template +struct BaseN { + explicit BaseN(int i) : i(i) {} + int i; +}; + +// test_mi_static_properties +struct Vanilla { + std::string vanilla() { return "Vanilla"; }; +}; +struct WithStatic1 { + static std::string static_func1() { return "WithStatic1"; }; + static int static_value1; +}; +struct WithStatic2 { + static std::string static_func2() { return "WithStatic2"; }; + static int static_value2; +}; +struct VanillaStaticMix1 : Vanilla, WithStatic1, WithStatic2 { + static std::string static_func() { return "VanillaStaticMix1"; } + static int static_value; +}; +struct VanillaStaticMix2 : WithStatic1, Vanilla, WithStatic2 { + static std::string static_func() { return "VanillaStaticMix2"; } + static int static_value; +}; +int WithStatic1::static_value1 = 1; +int WithStatic2::static_value2 = 2; +int VanillaStaticMix1::static_value = 12; +int VanillaStaticMix2::static_value = 12; + +// test_multiple_inheritance_virtbase +struct Base1a { + explicit Base1a(int i) : i(i) {} + int foo() const { return i; } + int i; +}; +struct Base2a { + explicit Base2a(int i) : i(i) {} + int bar() const { return i; } + int i; +}; +struct Base12a : Base1a, Base2a { + Base12a(int i, int j) : Base1a(i), Base2a(j) {} +}; + +// test_mi_unaligned_base +// test_mi_base_return +struct I801B1 { + int a = 1; + I801B1() = default; + I801B1(const I801B1 &) = default; + virtual ~I801B1() = default; +}; +struct I801B2 { + int b = 2; + I801B2() = default; + I801B2(const I801B2 &) = default; + virtual ~I801B2() = default; +}; +struct I801C : I801B1, I801B2 {}; +struct I801D : I801C {}; // Indirect MI + +} // namespace + +TEST_SUBMODULE(multiple_inheritance, m) { + // Please do not interleave `struct` and `class` definitions with bindings code, + // but implement `struct`s and `class`es in the anonymous namespace above. + // This helps keeping the smart_holder branch in sync with master. + + // test_multiple_inheritance_mix1 + // test_multiple_inheritance_mix2 + struct Base1 { + explicit Base1(int i) : i(i) {} + int foo() const { return i; } + int i; + }; + py::class_ b1(m, "Base1"); + b1.def(py::init()).def("foo", &Base1::foo); + + struct Base2 { + explicit Base2(int i) : i(i) {} + int bar() const { return i; } + int i; + }; + py::class_ b2(m, "Base2"); + b2.def(py::init()).def("bar", &Base2::bar); + + // test_multiple_inheritance_cpp + struct Base12 : Base1, Base2 { + Base12(int i, int j) : Base1(i), Base2(j) {} + }; + struct MIType : Base12 { + MIType(int i, int j) : Base12(i, j) {} + }; + py::class_(m, "Base12"); + py::class_(m, "MIType").def(py::init()); + + // 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); \ + }) + 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); + PYBIND11_BASEN(13); + PYBIND11_BASEN(14); + PYBIND11_BASEN(15); + PYBIND11_BASEN(16); + PYBIND11_BASEN(17); + + // Uncommenting this should result in a compile time failure (MI can only be specified via + // template parameters because pybind has to know the types involved; see discussion in #742 + // for details). + // struct Base12v2 : Base1, Base2 { + // Base12v2(int i, int j) : Base1(i), Base2(j) { } + // }; + // py::class_(m, "Base12v2", b1, b2) + // .def(py::init()); + + // test_multiple_inheritance_virtbase + // Test the case where not all base classes are specified, and where pybind11 requires the + // py::multiple_inheritance flag to perform proper casting between types. + py::class_>(m, "Base1a") + .def(py::init()) + .def("foo", &Base1a::foo); + + py::class_>(m, "Base2a") + .def(py::init()) + .def("bar", &Base2a::bar); + + py::class_>( + m, "Base12a", py::multiple_inheritance()) + .def(py::init()); + + m.def("bar_base2a", [](Base2a *b) { return b->bar(); }); + m.def("bar_base2a_sharedptr", [](const std::shared_ptr &b) { return b->bar(); }); + + // test_mi_unaligned_base + // test_mi_base_return + // Issue #801: invalid casting to derived type with MI bases + // Unregistered classes: + struct I801B3 { + int c = 3; + virtual ~I801B3() = default; + }; + struct I801E : I801B3, I801D {}; + + py::class_>(m, "I801B1") + .def(py::init<>()) + .def_readonly("a", &I801B1::a); + py::class_>(m, "I801B2") + .def(py::init<>()) + .def_readonly("b", &I801B2::b); + py::class_>(m, "I801C").def(py::init<>()); + py::class_>(m, "I801D").def(py::init<>()); + + // Two separate issues here: first, we want to recognize a pointer to a base type as being a + // known instance even when the pointer value is unequal (i.e. due to a non-first + // multiple-inheritance base class): + m.def("i801b1_c", [](I801C *c) { return static_cast(c); }); + m.def("i801b2_c", [](I801C *c) { return static_cast(c); }); + m.def("i801b1_d", [](I801D *d) { return static_cast(d); }); + m.def("i801b2_d", [](I801D *d) { return static_cast(d); }); + + // Second, when returned a base class pointer to a derived instance, we cannot assume that the + // pointer is `reinterpret_cast`able to the derived pointer because, like above, the base class + // pointer could be offset. + m.def("i801c_b1", []() -> I801B1 * { return new I801C(); }); + m.def("i801c_b2", []() -> I801B2 * { return new I801C(); }); + m.def("i801d_b1", []() -> I801B1 * { return new I801D(); }); + m.def("i801d_b2", []() -> I801B2 * { return new I801D(); }); + + // Return a base class pointer to a pybind-registered type when the actual derived type + // isn't pybind-registered (and uses multiple-inheritance to offset the pybind base) + m.def("i801e_c", []() -> I801C * { return new I801E(); }); + m.def("i801e_b2", []() -> I801B2 * { return new I801E(); }); + + // test_mi_static_properties + py::class_(m, "Vanilla").def(py::init<>()).def("vanilla", &Vanilla::vanilla); + + py::class_(m, "WithStatic1") + .def(py::init<>()) + .def_static("static_func1", &WithStatic1::static_func1) + .def_readwrite_static("static_value1", &WithStatic1::static_value1); + + py::class_(m, "WithStatic2") + .def(py::init<>()) + .def_static("static_func2", &WithStatic2::static_func2) + .def_readwrite_static("static_value2", &WithStatic2::static_value2); + + py::class_(m, "VanillaStaticMix1") + .def(py::init<>()) + .def_static("static_func", &VanillaStaticMix1::static_func) + .def_readwrite_static("static_value", &VanillaStaticMix1::static_value); + + py::class_(m, "VanillaStaticMix2") + .def(py::init<>()) + .def_static("static_func", &VanillaStaticMix2::static_func) + .def_readwrite_static("static_value", &VanillaStaticMix2::static_value); + + struct WithDict {}; + struct VanillaDictMix1 : Vanilla, WithDict {}; + struct VanillaDictMix2 : WithDict, Vanilla {}; + py::class_(m, "WithDict", py::dynamic_attr()).def(py::init<>()); + py::class_(m, "VanillaDictMix1").def(py::init<>()); + py::class_(m, "VanillaDictMix2").def(py::init<>()); + + // test_diamond_inheritance + // Issue #959: segfault when constructing diamond inheritance instance + // All of these have int members so that there will be various unequal pointers involved. + struct B { + int b; + B() = default; + B(const B &) = default; + virtual ~B() = default; + }; + struct C0 : public virtual B { + int c0; + }; + struct C1 : public virtual B { + int c1; + }; + struct D : public C0, public C1 { + int d; + }; + py::class_(m, "B").def("b", [](B *self) { return self; }); + py::class_(m, "C0").def("c0", [](C0 *self) { return self; }); + py::class_(m, "C1").def("c1", [](C1 *self) { return self; }); + py::class_(m, "D").def(py::init<>()); + + // test_pr3635_diamond_* + // - functions are get_{base}_{var}, return {var} + struct MVB { + MVB() = default; + MVB(const MVB &) = default; + virtual ~MVB() = default; + + int b = 1; + int get_b_b() const { return b; } + }; + struct MVC : virtual MVB { + int c = 2; + int get_c_b() const { return b; } + int get_c_c() const { return c; } + }; + struct MVD0 : virtual MVC { + int d0 = 3; + int get_d0_b() const { return b; } + int get_d0_c() const { return c; } + int get_d0_d0() const { return d0; } + }; + struct MVD1 : virtual MVC { + int d1 = 4; + int get_d1_b() const { return b; } + int get_d1_c() const { return c; } + int get_d1_d1() const { return d1; } + }; + struct MVE : virtual MVD0, virtual MVD1 { + int e = 5; + int get_e_b() const { return b; } + int get_e_c() const { return c; } + int get_e_d0() const { return d0; } + int get_e_d1() const { return d1; } + int get_e_e() const { return e; } + }; + struct MVF : virtual MVE { + int f = 6; + int get_f_b() const { return b; } + int get_f_c() const { return c; } + int get_f_d0() const { return d0; } + int get_f_d1() const { return d1; } + int get_f_e() const { return e; } + int get_f_f() const { return f; } + }; + py::class_(m, "MVB") + .def(py::init<>()) + .def("get_b_b", &MVB::get_b_b) + .def_readwrite("b", &MVB::b); + py::class_(m, "MVC") + .def(py::init<>()) + .def("get_c_b", &MVC::get_c_b) + .def("get_c_c", &MVC::get_c_c) + .def_readwrite("c", &MVC::c); + py::class_(m, "MVD0") + .def(py::init<>()) + .def("get_d0_b", &MVD0::get_d0_b) + .def("get_d0_c", &MVD0::get_d0_c) + .def("get_d0_d0", &MVD0::get_d0_d0) + .def_readwrite("d0", &MVD0::d0); + py::class_(m, "MVD1") + .def(py::init<>()) + .def("get_d1_b", &MVD1::get_d1_b) + .def("get_d1_c", &MVD1::get_d1_c) + .def("get_d1_d1", &MVD1::get_d1_d1) + .def_readwrite("d1", &MVD1::d1); + py::class_(m, "MVE") + .def(py::init<>()) + .def("get_e_b", &MVE::get_e_b) + .def("get_e_c", &MVE::get_e_c) + .def("get_e_d0", &MVE::get_e_d0) + .def("get_e_d1", &MVE::get_e_d1) + .def("get_e_e", &MVE::get_e_e) + .def_readwrite("e", &MVE::e); + py::class_(m, "MVF") + .def(py::init<>()) + .def("get_f_b", &MVF::get_f_b) + .def("get_f_c", &MVF::get_f_c) + .def("get_f_d0", &MVF::get_f_d0) + .def("get_f_d1", &MVF::get_f_d1) + .def("get_f_e", &MVF::get_f_e) + .def("get_f_f", &MVF::get_f_f) + .def_readwrite("f", &MVF::f); +} diff --git a/external_libraries/pybind11/tests/test_multiple_inheritance.py b/external_libraries/pybind11/tests/test_multiple_inheritance.py new file mode 100644 index 00000000..c127036c --- /dev/null +++ b/external_libraries/pybind11/tests/test_multiple_inheritance.py @@ -0,0 +1,500 @@ +from __future__ import annotations + +import pytest + +import env +from pybind11_tests import ConstructorStats +from pybind11_tests import multiple_inheritance as m + + +def test_multiple_inheritance_cpp(): + mt = m.MIType(3, 4) + + assert mt.foo() == 3 + assert mt.bar() == 4 + + +@pytest.mark.xfail("env.PYPY") +def test_multiple_inheritance_mix1(): + class Base1: + def __init__(self, i): + self.i = i + + def foo(self): + return self.i + + class MITypePy(Base1, m.Base2): + def __init__(self, i, j): + Base1.__init__(self, i) + m.Base2.__init__(self, j) + + mt = MITypePy(3, 4) + + assert mt.foo() == 3 + assert mt.bar() == 4 + + +def test_multiple_inheritance_mix2(): + class Base2: + def __init__(self, i): + self.i = i + + def bar(self): + return self.i + + class MITypePy(m.Base1, Base2): + def __init__(self, i, j): + m.Base1.__init__(self, i) + Base2.__init__(self, j) + + mt = MITypePy(3, 4) + + assert mt.foo() == 3 + assert mt.bar() == 4 + + +@pytest.mark.xfail("env.PYPY") +def test_multiple_inheritance_python(): + class MI1(m.Base1, m.Base2): + def __init__(self, i, j): + m.Base1.__init__(self, i) + m.Base2.__init__(self, j) + + class B1: + def v(self): + return 1 + + class MI2(B1, m.Base1, m.Base2): + def __init__(self, i, j): + B1.__init__(self) + m.Base1.__init__(self, i) + m.Base2.__init__(self, j) + + class MI3(MI2): + def __init__(self, i, j): + MI2.__init__(self, i, j) + + class MI4(MI3, m.Base2): + def __init__(self, i, j): + MI3.__init__(self, i, j) + # This should be ignored (Base2 is already initialized via MI2): + m.Base2.__init__(self, i + 100) + + class MI5(m.Base2, B1, m.Base1): + def __init__(self, i, j): + B1.__init__(self) + m.Base1.__init__(self, i) + m.Base2.__init__(self, j) + + class MI6(m.Base2, B1): + def __init__(self, i): + m.Base2.__init__(self, i) + B1.__init__(self) + + class B2(B1): + def v(self): + return 2 + + class B3: + def v(self): + return 3 + + class B4(B3, B2): + def v(self): + return 4 + + class MI7(B4, MI6): + def __init__(self, i): + B4.__init__(self) + MI6.__init__(self, i) + + class MI8(MI6, B3): + def __init__(self, i): + MI6.__init__(self, i) + B3.__init__(self) + + class MI8b(B3, MI6): + def __init__(self, i): + B3.__init__(self) + MI6.__init__(self, i) + + mi1 = MI1(1, 2) + assert mi1.foo() == 1 + assert mi1.bar() == 2 + + mi2 = MI2(3, 4) + assert mi2.v() == 1 + assert mi2.foo() == 3 + assert mi2.bar() == 4 + + mi3 = MI3(5, 6) + assert mi3.v() == 1 + assert mi3.foo() == 5 + assert mi3.bar() == 6 + + mi4 = MI4(7, 8) + assert mi4.v() == 1 + assert mi4.foo() == 7 + assert mi4.bar() == 8 + + mi5 = MI5(10, 11) + assert mi5.v() == 1 + assert mi5.foo() == 10 + assert mi5.bar() == 11 + + mi6 = MI6(12) + assert mi6.v() == 1 + assert mi6.bar() == 12 + + mi7 = MI7(13) + assert mi7.v() == 4 + assert mi7.bar() == 13 + + mi8 = MI8(14) + assert mi8.v() == 1 + assert mi8.bar() == 14 + + mi8b = MI8b(15) + assert mi8b.v() == 3 + assert mi8b.bar() == 15 + + +def test_multiple_inheritance_python_many_bases(): + class MIMany14(m.BaseN1, m.BaseN2, m.BaseN3, m.BaseN4): + def __init__(self): + m.BaseN1.__init__(self, 1) + m.BaseN2.__init__(self, 2) + m.BaseN3.__init__(self, 3) + m.BaseN4.__init__(self, 4) + + class MIMany58(m.BaseN5, m.BaseN6, m.BaseN7, m.BaseN8): + def __init__(self): + m.BaseN5.__init__(self, 5) + m.BaseN6.__init__(self, 6) + m.BaseN7.__init__(self, 7) + m.BaseN8.__init__(self, 8) + + class MIMany916( + m.BaseN9, + m.BaseN10, + m.BaseN11, + m.BaseN12, + m.BaseN13, + m.BaseN14, + m.BaseN15, + m.BaseN16, + ): + def __init__(self): + m.BaseN9.__init__(self, 9) + m.BaseN10.__init__(self, 10) + m.BaseN11.__init__(self, 11) + m.BaseN12.__init__(self, 12) + m.BaseN13.__init__(self, 13) + m.BaseN14.__init__(self, 14) + m.BaseN15.__init__(self, 15) + m.BaseN16.__init__(self, 16) + + class MIMany19(MIMany14, MIMany58, m.BaseN9): + def __init__(self): + MIMany14.__init__(self) + MIMany58.__init__(self) + m.BaseN9.__init__(self, 9) + + class MIMany117(MIMany14, MIMany58, MIMany916, m.BaseN17): + def __init__(self): + MIMany14.__init__(self) + MIMany58.__init__(self) + MIMany916.__init__(self) + m.BaseN17.__init__(self, 17) + + # Inherits from 4 registered C++ classes: can fit in one pointer on any modern arch: + a = MIMany14() + for i in range(1, 4): + assert getattr(a, "f" + str(i))() == 2 * i + + # Inherits from 8: requires 1/2 pointers worth of holder flags on 32/64-bit arch: + b = MIMany916() + for i in range(9, 16): + assert getattr(b, "f" + str(i))() == 2 * i + + # Inherits from 9: requires >= 2 pointers worth of holder flags + c = MIMany19() + for i in range(1, 9): + assert getattr(c, "f" + str(i))() == 2 * i + + # Inherits from 17: requires >= 3 pointers worth of holder flags + d = MIMany117() + for i in range(1, 17): + assert getattr(d, "f" + str(i))() == 2 * i + + +def test_multiple_inheritance_virtbase(): + class MITypePy(m.Base12a): + def __init__(self, i, j): + m.Base12a.__init__(self, i, j) + + mt = MITypePy(3, 4) + assert mt.bar() == 4 + assert m.bar_base2a(mt) == 4 + assert m.bar_base2a_sharedptr(mt) == 4 + + +def test_mi_static_properties(): + """Mixing bases with and without static properties should be possible + and the result should be independent of base definition order""" + + for d in (m.VanillaStaticMix1(), m.VanillaStaticMix2()): + assert d.vanilla() == "Vanilla" + assert d.static_func1() == "WithStatic1" + assert d.static_func2() == "WithStatic2" + assert d.static_func() == d.__class__.__name__ + + m.WithStatic1.static_value1 = 1 + m.WithStatic2.static_value2 = 2 + assert d.static_value1 == 1 + assert d.static_value2 == 2 + assert d.static_value == 12 + + d.static_value1 = 0 + assert d.static_value1 == 0 + d.static_value2 = 0 + assert d.static_value2 == 0 + d.static_value = 0 + assert d.static_value == 0 + + +def test_mi_dynamic_attributes(): + """Mixing bases with and without dynamic attribute support""" + + for d in (m.VanillaDictMix1(), m.VanillaDictMix2()): + d.dynamic = 1 + assert d.dynamic == 1 + + +def test_mi_unaligned_base(): + """Returning an offset (non-first MI) base class pointer should recognize the instance""" + + n_inst = ConstructorStats.detail_reg_inst() + + c = m.I801C() + d = m.I801D() + if not env.GRAALPY: + # + 4 below because we have the two instances, and each instance has offset base I801B2 + assert ConstructorStats.detail_reg_inst() == n_inst + 4 + b1c = m.i801b1_c(c) + assert b1c is c + b2c = m.i801b2_c(c) + assert b2c is c + b1d = m.i801b1_d(d) + assert b1d is d + b2d = m.i801b2_d(d) + assert b2d is d + + if env.GRAALPY: + pytest.skip("ConstructorStats is incompatible with GraalPy.") + + assert ConstructorStats.detail_reg_inst() == n_inst + 4 # no extra instances + del c, b1c, b2c + assert ConstructorStats.detail_reg_inst() == n_inst + 2 + del d, b1d, b2d + assert ConstructorStats.detail_reg_inst() == n_inst + + +def test_mi_base_return(): + """Tests returning an offset (non-first MI) base class pointer to a derived instance""" + + n_inst = ConstructorStats.detail_reg_inst() + + c1 = m.i801c_b1() + assert type(c1) is m.I801C + assert c1.a == 1 + assert c1.b == 2 + + d1 = m.i801d_b1() + assert type(d1) is m.I801D + assert d1.a == 1 + assert d1.b == 2 + + if not env.GRAALPY: + assert ConstructorStats.detail_reg_inst() == n_inst + 4 + + c2 = m.i801c_b2() + assert type(c2) is m.I801C + assert c2.a == 1 + assert c2.b == 2 + + d2 = m.i801d_b2() + assert type(d2) is m.I801D + assert d2.a == 1 + assert d2.b == 2 + + if not env.GRAALPY: + assert ConstructorStats.detail_reg_inst() == n_inst + 8 + + del c2 + assert ConstructorStats.detail_reg_inst() == n_inst + 6 + del c1, d1, d2 + assert ConstructorStats.detail_reg_inst() == n_inst + + # Returning an unregistered derived type with a registered base; we won't + # pick up the derived type, obviously, but should still work (as an object + # of whatever type was returned). + e1 = m.i801e_c() + assert type(e1) is m.I801C + assert e1.a == 1 + assert e1.b == 2 + + e2 = m.i801e_b2() + assert type(e2) is m.I801B2 + assert e2.b == 2 + + +def test_diamond_inheritance(): + """Tests that diamond inheritance works as expected (issue #959)""" + + # Issue #959: this shouldn't segfault: + d = m.D() + + # Make sure all the various distinct pointers are all recognized as registered instances: + assert d is d.c0() + assert d is d.c1() + assert d is d.b() + assert d is d.c0().b() + assert d is d.c1().b() + assert d is d.c0().c1().b().c0().b() + + +def test_pr3635_diamond_b(): + o = m.MVB() + assert o.b == 1 + + assert o.get_b_b() == 1 + + +def test_pr3635_diamond_c(): + o = m.MVC() + assert o.b == 1 + assert o.c == 2 + + assert o.get_b_b() == 1 + assert o.get_c_b() == 1 + + assert o.get_c_c() == 2 + + +def test_pr3635_diamond_d0(): + o = m.MVD0() + assert o.b == 1 + assert o.c == 2 + assert o.d0 == 3 + + assert o.get_b_b() == 1 + assert o.get_c_b() == 1 + assert o.get_d0_b() == 1 + + assert o.get_c_c() == 2 + assert o.get_d0_c() == 2 + + assert o.get_d0_d0() == 3 + + +def test_pr3635_diamond_d1(): + o = m.MVD1() + assert o.b == 1 + assert o.c == 2 + assert o.d1 == 4 + + assert o.get_b_b() == 1 + assert o.get_c_b() == 1 + assert o.get_d1_b() == 1 + + assert o.get_c_c() == 2 + assert o.get_d1_c() == 2 + + assert o.get_d1_d1() == 4 + + +def test_pr3635_diamond_e(): + o = m.MVE() + assert o.b == 1 + assert o.c == 2 + assert o.d0 == 3 + assert o.d1 == 4 + assert o.e == 5 + + assert o.get_b_b() == 1 + assert o.get_c_b() == 1 + assert o.get_d0_b() == 1 + assert o.get_d1_b() == 1 + assert o.get_e_b() == 1 + + assert o.get_c_c() == 2 + assert o.get_d0_c() == 2 + assert o.get_d1_c() == 2 + assert o.get_e_c() == 2 + + assert o.get_d0_d0() == 3 + assert o.get_e_d0() == 3 + + assert o.get_d1_d1() == 4 + assert o.get_e_d1() == 4 + + assert o.get_e_e() == 5 + + +def test_pr3635_diamond_f(): + o = m.MVF() + assert o.b == 1 + assert o.c == 2 + assert o.d0 == 3 + assert o.d1 == 4 + assert o.e == 5 + assert o.f == 6 + + assert o.get_b_b() == 1 + assert o.get_c_b() == 1 + assert o.get_d0_b() == 1 + assert o.get_d1_b() == 1 + assert o.get_e_b() == 1 + assert o.get_f_b() == 1 + + assert o.get_c_c() == 2 + assert o.get_d0_c() == 2 + assert o.get_d1_c() == 2 + assert o.get_e_c() == 2 + assert o.get_f_c() == 2 + + assert o.get_d0_d0() == 3 + assert o.get_e_d0() == 3 + assert o.get_f_d0() == 3 + + assert o.get_d1_d1() == 4 + assert o.get_e_d1() == 4 + assert o.get_f_d1() == 4 + + assert o.get_e_e() == 5 + assert o.get_f_e() == 5 + + assert o.get_f_f() == 6 + + +def test_python_inherit_from_mi(): + """Tests extending a Python class from a single inheritor of a MI class""" + + class PyMVF(m.MVF): + g = 7 + + def get_g_g(self): + return self.g + + o = PyMVF() + + assert o.b == 1 + assert o.c == 2 + assert o.d0 == 3 + assert o.d1 == 4 + assert o.e == 5 + assert o.f == 6 + assert o.g == 7 + + assert o.get_g_g() == 7 diff --git a/external_libraries/pybind11/tests/test_multiple_interpreters.py b/external_libraries/pybind11/tests/test_multiple_interpreters.py new file mode 100644 index 00000000..65b27331 --- /dev/null +++ b/external_libraries/pybind11/tests/test_multiple_interpreters.py @@ -0,0 +1,437 @@ +from __future__ import annotations + +import contextlib +import os +import pickle +import sys +import textwrap + +import pytest + +import env +import pybind11_tests + +if env.IOS: + pytest.skip("Subinterpreters not supported on iOS", allow_module_level=True) + +# 3.14.0b3+, though sys.implementation.supports_isolated_interpreters is being added in b4 +# Can be simplified when we drop support for the first three betas +CONCURRENT_INTERPRETERS_SUPPORT = ( + sys.version_info >= (3, 14) + and ( + sys.version_info != (3, 14, 0, "beta", 1) + and sys.version_info != (3, 14, 0, "beta", 2) + ) + and ( + sys.version_info == (3, 14, 0, "beta", 3) + or sys.implementation.supports_isolated_interpreters + ) +) + + +def get_interpreters(*, modern: bool): + if modern and CONCURRENT_INTERPRETERS_SUPPORT: + from concurrent import interpreters + + def create(): + return contextlib.closing(interpreters.create()) + + def run_string( + interp: interpreters.Interpreter, + code: str, + *, + shared: dict[str, object] | None = None, + ) -> Exception | None: + if shared: + interp.prepare_main(**shared) + try: + interp.exec(code) + return None + except interpreters.ExecutionFailed as err: + return err + + return run_string, create + + if sys.version_info >= (3, 12): + interpreters = pytest.importorskip( + "_interpreters" if sys.version_info >= (3, 13) else "_xxsubinterpreters" + ) + + @contextlib.contextmanager + def create(config: str = ""): + try: + if config: + interp = interpreters.create(config) + else: + interp = interpreters.create() + except TypeError: + pytest.skip(f"interpreters module needs to support {config} config") + + try: + yield interp + finally: + interpreters.destroy(interp) + + def run_string( + interp: int, code: str, shared: dict[str, object] | None = None + ) -> Exception | None: + kwargs = {"shared": shared} if shared else {} + return interpreters.run_string(interp, code, **kwargs) + + return run_string, create + + pytest.skip("Test requires the interpreters stdlib module") + + +@pytest.mark.skipif( + sys.platform.startswith("emscripten"), reason="Requires loadable modules" +) +def test_independent_subinterpreters(): + """Makes sure the internals object differs across independent subinterpreters""" + + sys.path.insert(0, os.path.dirname(pybind11_tests.__file__)) + + run_string, create = get_interpreters(modern=True) + + import mod_per_interpreter_gil as m + + if not m.defined_PYBIND11_HAS_SUBINTERPRETER_SUPPORT: + pytest.skip("Does not have subinterpreter support compiled in") + + code = textwrap.dedent( + """ + import mod_per_interpreter_gil as m + import pickle + with open(pipeo, 'wb') as f: + pickle.dump(m.internals_at(), f) + """ + ).strip() + + with create() as interp1, create() as interp2: + try: + res0 = run_string(interp1, "import mod_shared_interpreter_gil") + if res0 is not None: + res0 = str(res0) + except Exception as e: + res0 = str(e) + + pipei, pipeo = os.pipe() + run_string(interp1, code, shared={"pipeo": pipeo}) + with open(pipei, "rb") as f: + res1 = pickle.load(f) + + pipei, pipeo = os.pipe() + run_string(interp2, code, shared={"pipeo": pipeo}) + with open(pipei, "rb") as f: + res2 = pickle.load(f) + + assert "does not support loading in subinterpreters" in res0, ( + "cannot use shared_gil in a default subinterpreter" + ) + assert res1 != m.internals_at(), "internals should differ from main interpreter" + assert res2 != m.internals_at(), "internals should differ from main interpreter" + assert res1 != res2, "internals should differ between interpreters" + + +@pytest.mark.skipif( + sys.platform.startswith("emscripten"), reason="Requires loadable modules" +) +@pytest.mark.skipif(not CONCURRENT_INTERPRETERS_SUPPORT, reason="Requires 3.14.0b3+") +def test_independent_subinterpreters_modern(): + """Makes sure the internals object differs across independent subinterpreters. Modern (3.14+) syntax.""" + + sys.path.insert(0, os.path.dirname(pybind11_tests.__file__)) + + import mod_per_interpreter_gil as m + + if not m.defined_PYBIND11_HAS_SUBINTERPRETER_SUPPORT: + pytest.skip("Does not have subinterpreter support compiled in") + + from concurrent import interpreters + + code = textwrap.dedent( + """ + import mod_per_interpreter_gil as m + + values.put_nowait(m.internals_at()) + """ + ).strip() + + with contextlib.closing(interpreters.create()) as interp1, contextlib.closing( + interpreters.create() + ) as interp2: + with pytest.raises( + interpreters.ExecutionFailed, + match="does not support loading in subinterpreters", + ): + interp1.exec("import mod_shared_interpreter_gil") + + values = interpreters.create_queue() + interp1.prepare_main(values=values) + interp1.exec(code) + res1 = values.get_nowait() + + interp2.prepare_main(values=values) + interp2.exec(code) + res2 = values.get_nowait() + + assert res1 != m.internals_at(), "internals should differ from main interpreter" + assert res2 != m.internals_at(), "internals should differ from main interpreter" + assert res1 != res2, "internals should differ between interpreters" + + +@pytest.mark.skipif( + sys.platform.startswith("emscripten"), reason="Requires loadable modules" +) +def test_dependent_subinterpreters(): + """Makes sure the internals object differs across subinterpreters""" + + sys.path.insert(0, os.path.dirname(pybind11_tests.__file__)) + + run_string, create = get_interpreters(modern=False) + + import mod_shared_interpreter_gil as m + + if not m.defined_PYBIND11_HAS_SUBINTERPRETER_SUPPORT: + pytest.skip("Does not have subinterpreter support compiled in") + + code = textwrap.dedent( + """ + import mod_shared_interpreter_gil as m + import pickle + with open(pipeo, 'wb') as f: + pickle.dump(m.internals_at(), f) + """ + ).strip() + + with create("legacy") as interp1: + pipei, pipeo = os.pipe() + run_string(interp1, code, shared={"pipeo": pipeo}) + with open(pipei, "rb") as f: + res1 = pickle.load(f) + + assert res1 != m.internals_at(), "internals should differ from main interpreter" + + +PREAMBLE_CODE = textwrap.dedent( + f""" + def test(): + import sys + + sys.path.insert(0, {os.path.dirname(env.__file__)!r}) + sys.path.insert(0, {os.path.dirname(pybind11_tests.__file__)!r}) + + import collections + import mod_per_interpreter_gil_with_singleton as m + + objects = m.get_objects_in_singleton() + expected = [ + type(None), # static type: shared between interpreters + tuple, # static type: shared between interpreters + list, # static type: shared between interpreters + dict, # static type: shared between interpreters + collections.OrderedDict, # static type: shared between interpreters + collections.defaultdict, # heap type: dynamically created per interpreter + collections.deque, # heap type: dynamically created per interpreter + ] + # Check that we have the expected objects. Avoid IndexError by checking lengths first. + assert len(objects) == len(expected), ( + f"Expected {{expected!r}} ({{len(expected)}}), got {{objects!r}} ({{len(objects)}})." + ) + # The first ones are static types shared between interpreters. + assert objects[:-2] == expected[:-2], ( + f"Expected static objects {{expected[:-2]!r}}, got {{objects[:-2]!r}}." + ) + # The last two are heap types created per-interpreter. + # The expected objects are dynamically imported from `collections`. + assert objects[-2:] == expected[-2:], ( + f"Expected heap objects {{expected[-2:]!r}}, got {{objects[-2:]!r}}." + ) + + assert hasattr(m, 'MyClass'), "Module missing MyClass" + assert hasattr(m, 'MyGlobalError'), "Module missing MyGlobalError" + assert hasattr(m, 'MyLocalError'), "Module missing MyLocalError" + assert hasattr(m, 'MyEnum'), "Module missing MyEnum" + """ +).lstrip() + + +@pytest.mark.skipif( + sys.platform.startswith("emscripten"), reason="Requires loadable modules" +) +@pytest.mark.skipif(not CONCURRENT_INTERPRETERS_SUPPORT, reason="Requires 3.14.0b3+") +def test_import_module_with_singleton_per_interpreter(): + """Tests that a singleton storing Python objects works correctly per-interpreter""" + from concurrent import interpreters + + code = f"{PREAMBLE_CODE.strip()}\n\ntest()\n" + with contextlib.closing(interpreters.create()) as interp: + interp.exec(code) + + +@pytest.mark.skipif( + sys.platform.startswith("emscripten"), reason="Requires loadable modules" +) +@pytest.mark.skipif(not CONCURRENT_INTERPRETERS_SUPPORT, reason="Requires 3.14.0b3+") +def test_import_in_subinterpreter_after_main(): + """Tests that importing a module in a subinterpreter after the main interpreter works correctly""" + env.check_script_success_in_subprocess( + PREAMBLE_CODE + + textwrap.dedent( + """ + import contextlib + import gc + from concurrent import interpreters + + test() + + interp = None + with contextlib.closing(interpreters.create()) as interp: + interp.call(test) + + del interp + for _ in range(5): + gc.collect() + """ + ) + ) + + env.check_script_success_in_subprocess( + PREAMBLE_CODE + + textwrap.dedent( + """ + import contextlib + import gc + import random + from concurrent import interpreters + + test() + + interps = interp = None + with contextlib.ExitStack() as stack: + interps = [ + stack.enter_context(contextlib.closing(interpreters.create())) + for _ in range(8) + ] + random.shuffle(interps) + for interp in interps: + interp.call(test) + + del interps, interp, stack + for _ in range(5): + gc.collect() + """ + ) + ) + + +@pytest.mark.skipif( + sys.platform.startswith("emscripten"), reason="Requires loadable modules" +) +@pytest.mark.skipif(not CONCURRENT_INTERPRETERS_SUPPORT, reason="Requires 3.14.0b3+") +def test_import_in_subinterpreter_before_main(): + """Tests that importing a module in a subinterpreter before the main interpreter works correctly""" + env.check_script_success_in_subprocess( + PREAMBLE_CODE + + textwrap.dedent( + """ + import contextlib + import gc + from concurrent import interpreters + + interp = None + with contextlib.closing(interpreters.create()) as interp: + interp.call(test) + + test() + + del interp + for _ in range(5): + gc.collect() + """ + ) + ) + + env.check_script_success_in_subprocess( + PREAMBLE_CODE + + textwrap.dedent( + """ + import contextlib + import gc + from concurrent import interpreters + + interps = interp = None + with contextlib.ExitStack() as stack: + interps = [ + stack.enter_context(contextlib.closing(interpreters.create())) + for _ in range(8) + ] + for interp in interps: + interp.call(test) + + test() + + del interps, interp, stack + for _ in range(5): + gc.collect() + """ + ) + ) + + env.check_script_success_in_subprocess( + PREAMBLE_CODE + + textwrap.dedent( + """ + import contextlib + import gc + from concurrent import interpreters + + interps = interp = None + with contextlib.ExitStack() as stack: + interps = [ + stack.enter_context(contextlib.closing(interpreters.create())) + for _ in range(8) + ] + for interp in interps: + interp.call(test) + + test() + + del interps, interp, stack + for _ in range(5): + gc.collect() + """ + ) + ) + + +@pytest.mark.skipif( + sys.platform.startswith("emscripten"), reason="Requires loadable modules" +) +@pytest.mark.xfail( + env.MUSLLINUX, + reason="Flaky on musllinux, see also: https://github.com/pybind/pybind11/pull/5972#discussion_r2755283335", + strict=False, +) +@pytest.mark.skipif(not CONCURRENT_INTERPRETERS_SUPPORT, reason="Requires 3.14.0b3+") +def test_import_in_subinterpreter_concurrently(): + """Tests that importing a module in multiple subinterpreters concurrently works correctly""" + env.check_script_success_in_subprocess( + PREAMBLE_CODE + + textwrap.dedent( + """ + import gc + from concurrent.futures import InterpreterPoolExecutor, as_completed + + futures = future = None + with InterpreterPoolExecutor(max_workers=16) as executor: + futures = [executor.submit(test) for _ in range(32)] + for future in as_completed(futures): + future.result() + del futures, future, executor + + for _ in range(5): + gc.collect() + """ + ) + ) diff --git a/external_libraries/pybind11/tests/test_native_enum.cpp b/external_libraries/pybind11/tests/test_native_enum.cpp new file mode 100644 index 00000000..816ace01 --- /dev/null +++ b/external_libraries/pybind11/tests/test_native_enum.cpp @@ -0,0 +1,262 @@ +#include + +#include "pybind11_tests.h" + +#include + +namespace test_native_enum { + +// https://en.cppreference.com/w/cpp/language/enum + +// enum that takes 16 bits +enum smallenum : std::int16_t { a, b, c }; + +// color may be red (value 0), yellow (value 1), green (value 20), or blue (value 21) +enum color { red, yellow, green = 20, blue }; + +// altitude may be altitude::high or altitude::low +enum class altitude : char { + high = 'h', + low = 'l', // trailing comma only allowed after CWG518 +}; + +enum class flags_uchar : unsigned char { bit0 = 0x1u, bit1 = 0x2u, bit2 = 0x4u }; +enum class flags_uint : unsigned int { bit0 = 0x1u, bit1 = 0x2u, bit2 = 0x4u }; + +enum class export_values { exv0, exv1 }; + +enum class member_doc { mem0, mem1, mem2 }; + +struct class_with_enum { + enum class in_class { one, two }; + + in_class nested_value = in_class::one; +}; + +// https://github.com/protocolbuffers/protobuf/blob/d70b5c5156858132decfdbae0a1103e6a5cb1345/src/google/protobuf/generated_enum_util.h#L52-L53 +template +struct is_proto_enum : std::false_type {}; + +enum some_proto_enum : int { Zero, One }; + +template <> +struct is_proto_enum : std::true_type {}; + +enum class func_sig_rendering {}; + +} // namespace test_native_enum + +PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE) +PYBIND11_NAMESPACE_BEGIN(detail) + +// Negate this condition to demonstrate "ambiguous template instantiation" compilation error: +#if defined(PYBIND11_HAS_NATIVE_ENUM) +template +struct type_caster_enum_type_enabled< + ProtoEnumType, + enable_if_t::value>> : std::false_type {}; +#endif + +// https://github.com/pybind/pybind11_protobuf/blob/a50899c2eb604fc5f25deeb8901eff6231b8b3c0/pybind11_protobuf/enum_type_caster.h#L101-L105 +template +struct type_caster::value>> { + static handle + cast(const ProtoEnumType & /*src*/, return_value_policy /*policy*/, handle /*parent*/) { + return py::none(); + } + + bool load(handle /*src*/, bool /*convert*/) { + value = static_cast(0); + return true; + } + + PYBIND11_TYPE_CASTER(ProtoEnumType, const_name()); +}; + +PYBIND11_NAMESPACE_END(detail) +PYBIND11_NAMESPACE_END(PYBIND11_NAMESPACE) + +TEST_SUBMODULE(native_enum, m) { + using namespace test_native_enum; + + py::native_enum(m, "smallenum", "enum.IntEnum", "doc smallenum") + .value("a", smallenum::a) + .value("b", smallenum::b) + .value("c", smallenum::c) + .finalize(); + + py::native_enum(m, "color", "enum.IntEnum") + .value("red", color::red) + .value("yellow", color::yellow) + .value("green", color::green) + .value("blue", color::blue) + .finalize(); + + m.def("bind_altitude", [](const py::module_ &mod) { + py::native_enum(mod, "altitude", "enum.Enum") + .value("high", altitude::high) + .value("low", altitude::low) + .finalize(); + }); + m.def("is_high_altitude", [](altitude alt) { return alt == altitude::high; }); + m.def("get_altitude", []() -> altitude { return altitude::high; }); + + py::native_enum(m, "flags_uchar", "enum.Flag") + .value("bit0", flags_uchar::bit0) + .value("bit1", flags_uchar::bit1) + .value("bit2", flags_uchar::bit2) + .finalize(); + + py::native_enum(m, "flags_uint", "enum.IntFlag") + .value("bit0", flags_uint::bit0) + .value("bit1", flags_uint::bit1) + .value("bit2", flags_uint::bit2) + .finalize(); + + py::native_enum(m, "export_values", "enum.IntEnum") + .value("exv0", export_values::exv0) + .value("exv1", export_values::exv1) + .export_values() + .finalize(); + + py::native_enum(m, "member_doc", "enum.IntEnum") + .value("mem0", member_doc::mem0, "docA") + .value("mem1", member_doc::mem1) + .value("mem2", member_doc::mem2, "docC") + .finalize(); + + py::class_ py_class_with_enum(m, "class_with_enum"); + py::native_enum(py_class_with_enum, "in_class", "enum.IntEnum") + .value("one", class_with_enum::in_class::one) + .value("two", class_with_enum::in_class::two) + .finalize(); + + py_class_with_enum.def(py::init()) + .def_readwrite("nested_value", &class_with_enum::nested_value); + + m.def("isinstance_color", [](const py::object &obj) { return py::isinstance(obj); }); + + m.def("pass_color", [](color e) { return static_cast(e); }); + m.def("return_color", [](int i) { return static_cast(i); }); + + m.def("return_color_const_ptr", []() -> const color * { + static const color test_color = color::red; + return &test_color; + }); + m.def("return_color_mutbl_ptr", []() -> color * { + static color test_color = color::green; + return &test_color; + }); + + py::native_enum(m, "func_sig_rendering", "enum.Enum").finalize(); + m.def( + "pass_and_return_func_sig_rendering", + [](func_sig_rendering e) { return e; }, + py::arg("e")); + + m.def("pass_some_proto_enum", [](some_proto_enum) { return py::none(); }); + m.def("return_some_proto_enum", []() { return some_proto_enum::Zero; }); + +#if defined(__MINGW32__) + m.attr("obj_cast_color_ptr") = "MinGW: dangling pointer to an unnamed temporary may be used " + "[-Werror=dangling-pointer=]"; +#elif defined(NDEBUG) + m.attr("obj_cast_color_ptr") = "NDEBUG disables cast safety check"; +#else + m.def("obj_cast_color_ptr", [](const py::object &obj) { obj.cast(); }); +#endif + + m.def("py_cast_color_handle", [](py::handle obj) { + // Exercises `if (is_enum_cast && cast_is_temporary_value_reference::value)` + // in `T cast(const handle &handle)` + auto e = py::cast(obj); + return static_cast(e); + }); + + m.def("exercise_import_or_getattr", [](py::module_ &m, const char *native_type_name) { + enum fake { x }; + py::native_enum(m, "fake_import_or_getattr", native_type_name) + .value("x", fake::x) + .finalize(); + }); + + m.def("native_enum_data_missing_finalize_error_message", + [](const std::string &enum_name_encoded) { + return py::detail::native_enum_missing_finalize_error_message(enum_name_encoded); + }); + + m.def("native_enum_ctor_malformed_utf8", [](const char *malformed_utf8) { + enum fake { x }; + py::native_enum{py::none(), malformed_utf8, "enum.IntEnum"}; + }); + + m.def("native_enum_double_finalize", [](py::module_ &m) { + enum fake { x }; + py::native_enum ne(m, "fake_native_enum_double_finalize", "enum.IntEnum"); + ne.finalize(); + ne.finalize(); + }); + + m.def("native_enum_value_after_finalize", [](py::module_ &m) { + enum fake { x }; + py::native_enum ne(m, "fake_native_enum_value_after_finalize", "enum.IntEnum"); + ne.finalize(); + ne.value("x", fake::x); + }); + + m.def("native_enum_value_malformed_utf8", [](const char *malformed_utf8) { + enum fake { x }; + py::native_enum(py::none(), "fake", "enum.IntEnum").value(malformed_utf8, fake::x); + }); + + m.def("double_registration_native_enum", [](py::module_ &m) { + enum fake { x }; + py::native_enum(m, "fake_double_registration_native_enum", "enum.IntEnum") + .value("x", fake::x) + .finalize(); + py::native_enum(m, "fake_double_registration_native_enum", "enum.Enum"); + }); + + m.def("native_enum_name_clash", [](py::module_ &m) { + enum fake { x }; + py::native_enum(m, "fake_native_enum_name_clash", "enum.IntEnum") + .value("x", fake::x) + .finalize(); + }); + + m.def("native_enum_value_name_clash", [](py::module_ &m) { + enum fake { x }; + py::native_enum(m, "fake_native_enum_value_name_clash", "enum.IntEnum") + .value("fake_native_enum_value_name_clash_x", fake::x) + .export_values() + .finalize(); + }); + + m.def("double_registration_enum_before_native_enum", [](py::module_ &m) { + enum fake { x }; + py::enum_(m, "fake_enum_first").value("x", fake::x); + py::native_enum(m, "fake_enum_first", "enum.IntEnum").value("x", fake::x).finalize(); + }); + + m.def("double_registration_native_enum_before_enum", [](py::module_ &m) { + enum fake { x }; + py::native_enum(m, "fake_native_enum_first", "enum.IntEnum") + .value("x", fake::x) + .finalize(); + py::enum_(m, "name_must_be_different_to_reach_desired_code_path"); + }); + +#if defined(PYBIND11_NEGATE_THIS_CONDITION_FOR_LOCAL_TESTING) && !defined(NDEBUG) + m.def("native_enum_missing_finalize_failure", []() { + enum fake { x }; + py::native_enum( + py::none(), "fake_native_enum_missing_finalize_failure", "enum.IntEnum") + .value("x", fake::x) + // .finalize() missing + ; + }); +#else + m.attr("native_enum_missing_finalize_failure") = "For local testing only: terminates process"; +#endif +} diff --git a/external_libraries/pybind11/tests/test_native_enum.py b/external_libraries/pybind11/tests/test_native_enum.py new file mode 100644 index 00000000..42622001 --- /dev/null +++ b/external_libraries/pybind11/tests/test_native_enum.py @@ -0,0 +1,377 @@ +from __future__ import annotations + +import enum +import pickle + +import pytest + +import env +from pybind11_tests import native_enum as m + +SMALLENUM_MEMBERS = ( + ("a", 0), + ("b", 1), + ("c", 2), +) + +COLOR_MEMBERS = ( + ("red", 0), + ("yellow", 1), + ("green", 20), + ("blue", 21), +) + +ALTITUDE_MEMBERS = ( + ("high", "h"), + ("low", "l"), +) + +FLAGS_UCHAR_MEMBERS = ( + ("bit0", 0x1), + ("bit1", 0x2), + ("bit2", 0x4), +) + +FLAGS_UINT_MEMBERS = ( + ("bit0", 0x1), + ("bit1", 0x2), + ("bit2", 0x4), +) + +CLASS_WITH_ENUM_IN_CLASS_MEMBERS = ( + ("one", 0), + ("two", 1), +) + +EXPORT_VALUES_MEMBERS = ( + ("exv0", 0), + ("exv1", 1), +) + +MEMBER_DOC_MEMBERS = ( + ("mem0", 0), + ("mem1", 1), + ("mem2", 2), +) + +FUNC_SIG_RENDERING_MEMBERS = () + +ENUM_TYPES_AND_MEMBERS = ( + (m.smallenum, SMALLENUM_MEMBERS), + (m.color, COLOR_MEMBERS), + (m.flags_uchar, FLAGS_UCHAR_MEMBERS), + (m.flags_uint, FLAGS_UINT_MEMBERS), + (m.export_values, EXPORT_VALUES_MEMBERS), + (m.member_doc, MEMBER_DOC_MEMBERS), + (m.func_sig_rendering, FUNC_SIG_RENDERING_MEMBERS), + (m.class_with_enum.in_class, CLASS_WITH_ENUM_IN_CLASS_MEMBERS), +) + +ENUM_TYPES = [_[0] for _ in ENUM_TYPES_AND_MEMBERS] + + +@pytest.mark.parametrize("enum_type", ENUM_TYPES) +def test_enum_type(enum_type): + assert isinstance(enum_type, enum.EnumMeta) + assert enum_type.__module__ == m.__name__ + + +@pytest.mark.parametrize(("enum_type", "members"), ENUM_TYPES_AND_MEMBERS) +def test_enum_members(enum_type, members): + for name, value in members: + assert enum_type[name].value == value + + +@pytest.mark.parametrize(("enum_type", "members"), ENUM_TYPES_AND_MEMBERS) +def test_pickle_roundtrip(enum_type, members): + for name, _ in members: + orig = enum_type[name] + # This only works if __module__ is correct. + serialized = pickle.dumps(orig) + restored = pickle.loads(serialized) + assert restored == orig + + +@pytest.mark.parametrize("enum_type", [m.flags_uchar, m.flags_uint]) +def test_enum_flag(enum_type): + bits02 = enum_type.bit0 | enum_type.bit2 + assert enum_type.bit0 in bits02 + assert enum_type.bit1 not in bits02 + assert enum_type.bit2 in bits02 + + +def test_export_values(): + assert m.exv0 is m.export_values.exv0 + assert m.exv1 is m.export_values.exv1 + + +def test_class_doc(): + pure_native = enum.IntEnum("pure_native", (("mem", 0),)) + assert m.smallenum.__doc__ == "doc smallenum" + assert m.color.__doc__ == pure_native.__doc__ + + +def test_member_doc(): + pure_native = enum.IntEnum("pure_native", (("mem", 0),)) + assert m.member_doc.mem0.__doc__ == "docA" + assert m.member_doc.mem1.__doc__ == pure_native.mem.__doc__ + assert m.member_doc.mem2.__doc__ == "docC" + + +def test_pybind11_isinstance_color(): + for name, _ in COLOR_MEMBERS: + assert m.isinstance_color(m.color[name]) + assert not m.isinstance_color(m.color) + for name, _ in SMALLENUM_MEMBERS: + assert not m.isinstance_color(m.smallenum[name]) + assert not m.isinstance_color(m.smallenum) + assert not m.isinstance_color(None) + + +def test_pass_color_success(): + for name, value in COLOR_MEMBERS: + assert m.pass_color(m.color[name]) == value + + +def test_pass_color_fail(): + with pytest.raises(TypeError) as excinfo: + m.pass_color(None) + assert "pybind11_tests.native_enum.color" in str(excinfo.value) + + +def test_return_color_success(): + for name, value in COLOR_MEMBERS: + assert m.return_color(value) == m.color[name] + + +def test_return_color_fail(): + with pytest.raises(ValueError) as excinfo_direct: + m.color(2) + with pytest.raises(ValueError) as excinfo_cast: + m.return_color(2) + assert str(excinfo_cast.value) == str(excinfo_direct.value) + + +def test_return_color_ptr(): + assert m.return_color_const_ptr() == m.color.red + assert m.return_color_mutbl_ptr() == m.color.green + + +def test_property_type_hint(): + prop = m.class_with_enum.__dict__["nested_value"] + assert isinstance(prop, property) + assert prop.fget.__doc__.startswith( + "(self: pybind11_tests.native_enum.class_with_enum)" + " -> pybind11_tests.native_enum.class_with_enum.in_class" + ) + + +def test_func_sig_rendering(): + assert m.pass_and_return_func_sig_rendering.__doc__.startswith( + "pass_and_return_func_sig_rendering(e: pybind11_tests.native_enum.func_sig_rendering)" + " -> pybind11_tests.native_enum.func_sig_rendering" + ) + + +def test_type_caster_enum_type_enabled_false(): + # This is really only a "does it compile" test. + assert m.pass_some_proto_enum(None) is None + assert m.return_some_proto_enum() is None + + +@pytest.mark.skipif(isinstance(m.obj_cast_color_ptr, str), reason=m.obj_cast_color_ptr) +def test_obj_cast_color_ptr(): + with pytest.raises(RuntimeError) as excinfo: + m.obj_cast_color_ptr(m.color.red) + assert str(excinfo.value) == "Unable to cast native enum type to reference" + + +def test_py_cast_color_handle(): + for name, value in COLOR_MEMBERS: + assert m.py_cast_color_handle(m.color[name]) == value + + +def test_exercise_import_or_getattr_leading_dot(): + with pytest.raises(ValueError) as excinfo: + m.exercise_import_or_getattr(m, ".") + assert str(excinfo.value) == "Invalid fully-qualified name `.` (native_type_name)" + + +def test_exercise_import_or_getattr_bad_top_level(): + with pytest.raises(ImportError) as excinfo: + m.exercise_import_or_getattr(m, "NeVeRLaNd") + assert ( + str(excinfo.value) + == "Failed to import top-level module `NeVeRLaNd` (native_type_name)" + ) + + +def test_exercise_import_or_getattr_dot_dot(): + with pytest.raises(ValueError) as excinfo: + m.exercise_import_or_getattr(m, "enum..") + assert ( + str(excinfo.value) == "Invalid fully-qualified name `enum..` (native_type_name)" + ) + + +def test_exercise_import_or_getattr_bad_enum_attr(): + with pytest.raises(ImportError) as excinfo: + m.exercise_import_or_getattr(m, "enum.NoNeXiStInG") + lines = str(excinfo.value).splitlines() + assert len(lines) >= 5 + assert ( + lines[0] + == "Failed to import or getattr `NoNeXiStInG` from `enum` (native_type_name)" + ) + assert lines[1] == "-------- getattr exception --------" + ix = lines.index("-------- import exception --------") + assert ix >= 3 + assert len(lines) > ix + 0 + + +def test_native_enum_data_missing_finalize_error_message(): + msg = m.native_enum_data_missing_finalize_error_message("Fake") + assert msg == 'pybind11::native_enum<...>("Fake", ...): MISSING .finalize()' + + +@pytest.mark.parametrize( + "func", [m.native_enum_ctor_malformed_utf8, m.native_enum_value_malformed_utf8] +) +def test_native_enum_malformed_utf8(func): + if env.GRAALPY and func is m.native_enum_ctor_malformed_utf8: + pytest.skip("GraalPy does not raise UnicodeDecodeError") + malformed_utf8 = b"\x80" + with pytest.raises(UnicodeDecodeError): + func(malformed_utf8) + + +def test_native_enum_double_finalize(): + with pytest.raises(RuntimeError) as excinfo: + m.native_enum_double_finalize(m) + assert ( + str(excinfo.value) + == 'pybind11::native_enum<...>("fake_native_enum_double_finalize"): DOUBLE finalize' + ) + + +def test_native_enum_value_after_finalize(): + with pytest.raises(RuntimeError) as excinfo: + m.native_enum_value_after_finalize(m) + assert ( + str(excinfo.value) + == 'pybind11::native_enum<...>("fake_native_enum_value_after_finalize"): value after finalize' + ) + + +def test_double_registration_native_enum(): + with pytest.raises(RuntimeError) as excinfo: + m.double_registration_native_enum(m) + assert ( + str(excinfo.value) + == 'pybind11::native_enum<...>("fake_double_registration_native_enum") is already registered!' + ) + + +def test_native_enum_name_clash(): + m.fake_native_enum_name_clash = None + with pytest.raises(RuntimeError) as excinfo: + m.native_enum_name_clash(m) + assert ( + str(excinfo.value) + == 'pybind11::native_enum<...>("fake_native_enum_name_clash"):' + " an object with that name is already defined" + ) + + +def test_native_enum_value_name_clash(): + m.fake_native_enum_value_name_clash_x = None + with pytest.raises(RuntimeError) as excinfo: + m.native_enum_value_name_clash(m) + assert ( + str(excinfo.value) + == 'pybind11::native_enum<...>("fake_native_enum_value_name_clash")' + '.value("fake_native_enum_value_name_clash_x"):' + " an object with that name is already defined" + ) + + +def test_double_registration_enum_before_native_enum(): + with pytest.raises(RuntimeError) as excinfo: + m.double_registration_enum_before_native_enum(m) + assert ( + str(excinfo.value) + == 'pybind11::native_enum<...>("fake_enum_first") is already registered' + " as a `pybind11::enum_` or `pybind11::class_`!" + ) + + +def test_double_registration_native_enum_before_enum(): + with pytest.raises(RuntimeError) as excinfo: + m.double_registration_native_enum_before_enum(m) + assert ( + str(excinfo.value) + == 'pybind11::enum_ "name_must_be_different_to_reach_desired_code_path"' + " is already registered as a pybind11::native_enum!" + ) + + +def test_native_enum_missing_finalize_failure(): + if not isinstance(m.native_enum_missing_finalize_failure, str): + m.native_enum_missing_finalize_failure() + pytest.fail("Process termination expected.") + + +def test_unregister_native_enum_when_destroyed(): + # For stability when running tests in parallel, this test should be the + # only one that touches `m.altitude` or calls `m.bind_altitude`. + + def test_altitude_enum(): + # Logic copied from test_enum_type / test_enum_members. + # We don't test altitude there to avoid possible clashes if + # parallelizing against other tests in this file, and we also + # don't want to hold any references to the enumerators that + # would prevent GCing the enum type below. + assert isinstance(m.altitude, enum.EnumMeta) + assert m.altitude.__module__ == m.__name__ + for name, value in ALTITUDE_MEMBERS: + assert m.altitude[name].value == value + + def test_altitude_binding(): + assert m.is_high_altitude(m.altitude.high) + assert not m.is_high_altitude(m.altitude.low) + assert m.get_altitude() is m.altitude.high + with pytest.raises(TypeError, match="incompatible function arguments"): + m.is_high_altitude("oops") + + m.bind_altitude(m) + test_altitude_enum() + test_altitude_binding() + + if env.TYPES_ARE_IMMORTAL: + pytest.skip("can't GC type objects on this platform") + + # Delete the enum type. Returning an instance from Python should fail + # rather than accessing a deleted object. + pytest.delattr_and_ensure_destroyed((m, "altitude")) + with pytest.raises(TypeError, match="Unable to convert function return"): + m.get_altitude() + with pytest.raises(TypeError, match="incompatible function arguments"): + m.is_high_altitude("oops") + + # Recreate the enum type; should not have any duplicate-binding error + m.bind_altitude(m) + test_altitude_enum() + test_altitude_binding() + + # Remove the pybind11 capsule without removing the type; enum is still + # usable but can't be passed to/from bound functions + del m.altitude.__pybind11_native_enum__ + pytest.gc_collect() + test_altitude_enum() # enum itself still works + + with pytest.raises(TypeError, match="Unable to convert function return"): + m.get_altitude() + with pytest.raises(TypeError, match="incompatible function arguments"): + m.is_high_altitude(m.altitude.high) + + del m.altitude diff --git a/external_libraries/pybind11/tests/test_numpy_array.cpp b/external_libraries/pybind11/tests/test_numpy_array.cpp new file mode 100644 index 00000000..3bce9883 --- /dev/null +++ b/external_libraries/pybind11/tests/test_numpy_array.cpp @@ -0,0 +1,616 @@ +/* + tests/test_numpy_array.cpp -- test core array functionality + + Copyright (c) 2016 Ivan Smirnov + + All rights reserved. Use of this source code is governed by a + BSD-style license that can be found in the LICENSE file. +*/ + +#include +#include + +#include "pybind11_tests.h" + +#include +#include +#include + +// Size / dtype checks. +struct DtypeCheck { + py::dtype numpy; + py::dtype pybind11; +}; + +template +DtypeCheck get_dtype_check(const char *name) { + py::module_ np = py::module_::import("numpy"); + DtypeCheck check{}; + check.numpy = np.attr("dtype")(np.attr(name)); + check.pybind11 = py::dtype::of(); + return check; +} + +std::vector get_concrete_dtype_checks() { + return {// Normalization + get_dtype_check("int8"), + get_dtype_check("uint8"), + get_dtype_check("int16"), + get_dtype_check("uint16"), + get_dtype_check("int32"), + get_dtype_check("uint32"), + get_dtype_check("int64"), + get_dtype_check("uint64")}; +} + +struct DtypeSizeCheck { + std::string name; + int size_cpp{}; + int size_numpy{}; + // For debugging. + py::dtype dtype; +}; + +template +DtypeSizeCheck get_dtype_size_check() { + DtypeSizeCheck check{}; + check.name = py::type_id(); + check.size_cpp = sizeof(T); + check.dtype = py::dtype::of(); + check.size_numpy = check.dtype.attr("itemsize").template cast(); + return check; +} + +std::vector get_platform_dtype_size_checks() { + return { + get_dtype_size_check(), + get_dtype_size_check(), + get_dtype_size_check(), + get_dtype_size_check(), + get_dtype_size_check(), + get_dtype_size_check(), + get_dtype_size_check(), + get_dtype_size_check(), + }; +} + +// Arrays. +using arr = py::array; +using arr_t = py::array_t; +static_assert(std::is_same::value, ""); + +template +arr data(const arr &a, Ix... index) { + return arr(a.nbytes() - a.offset_at(index...), (const uint8_t *) a.data(index...)); +} + +template +arr data_t(const arr_t &a, Ix... index) { + return arr(a.size() - a.index_at(index...), a.data(index...)); +} + +template +arr &mutate_data(arr &a, Ix... index) { + auto *ptr = (uint8_t *) a.mutable_data(index...); + for (py::ssize_t i = 0; i < a.nbytes() - a.offset_at(index...); i++) { + ptr[i] = (uint8_t) (ptr[i] * 2); + } + return a; +} + +template +arr_t &mutate_data_t(arr_t &a, Ix... index) { + auto ptr = a.mutable_data(index...); + for (py::ssize_t i = 0; i < a.size() - a.index_at(index...); i++) { + ptr[i]++; + } + return a; +} + +template +py::ssize_t index_at(const arr &a, Ix... idx) { + return a.index_at(idx...); +} +template +py::ssize_t index_at_t(const arr_t &a, Ix... idx) { + return a.index_at(idx...); +} +template +py::ssize_t offset_at(const arr &a, Ix... idx) { + return a.offset_at(idx...); +} +template +py::ssize_t offset_at_t(const arr_t &a, Ix... idx) { + return a.offset_at(idx...); +} +template +py::ssize_t at_t(const arr_t &a, Ix... idx) { + return a.at(idx...); +} +template +arr_t &mutate_at_t(arr_t &a, Ix... idx) { + a.mutable_at(idx...)++; + return a; +} + +#define def_index_fn(name, type) \ + sm.def(#name, [](type a) { return name(a); }); \ + sm.def(#name, [](type a, int i) { return name(a, i); }); \ + sm.def(#name, [](type a, int i, int j) { return name(a, i, j); }); \ + sm.def(#name, [](type a, int i, int j, int k) { return name(a, i, j, k); }); + +template +py::handle auxiliaries(T &&r, T2 &&r2) { + if (r.ndim() != 2) { + throw std::domain_error("error: ndim != 2"); + } + py::list l; + l.append(*r.data(0, 0)); + l.append(*r2.mutable_data(0, 0)); + l.append(r.data(0, 1) == r2.mutable_data(0, 1)); + l.append(r.ndim()); + l.append(r.itemsize()); + l.append(r.shape(0)); + l.append(r.shape(1)); + l.append(r.size()); + l.append(r.nbytes()); + return l.release(); +} + +template +PyObjectType convert_to_pyobjecttype(py::object obj); + +template <> +PyObject *convert_to_pyobjecttype(py::object obj) { + return obj.release().ptr(); +} + +template <> +py::handle convert_to_pyobjecttype(py::object obj) { + return obj.release(); +} + +template <> +py::object convert_to_pyobjecttype(py::object obj) { + return obj; +} + +template +std::string pass_array_return_sum_str_values(const py::array_t &objs) { + std::string sum_str_values; + for (const auto &obj : objs) { + sum_str_values += py::str(obj.attr("value")); + } + return sum_str_values; +} + +template +py::list pass_array_return_as_list(const py::array_t &objs) { + return objs; +} + +template +py::array_t return_array_cpp_loop(const py::list &objs) { + py::size_t arr_size = py::len(objs); + py::array_t arr_from_list(static_cast(arr_size)); + PyObjectType *data = arr_from_list.mutable_data(); + for (py::size_t i = 0; i < arr_size; i++) { + assert(!data[i]); + data[i] = convert_to_pyobjecttype(objs[i].attr("value")); + } + return arr_from_list; +} + +template +py::array_t return_array_from_list(const py::list &objs) { + return objs; +} + +// note: declaration at local scope would create a dangling reference! +static int data_i = 42; + +TEST_SUBMODULE(numpy_array, sm) { + try { + py::module_::import("numpy"); + } catch (const py::error_already_set &) { + return; + } + + // test_dtypes + py::class_(sm, "DtypeCheck") + .def_readonly("numpy", &DtypeCheck::numpy) + .def_readonly("pybind11", &DtypeCheck::pybind11) + .def("__repr__", [](const DtypeCheck &self) { + return py::str("").format(self.numpy, self.pybind11); + }); + sm.def("get_concrete_dtype_checks", &get_concrete_dtype_checks); + + py::class_(sm, "DtypeSizeCheck") + .def_readonly("name", &DtypeSizeCheck::name) + .def_readonly("size_cpp", &DtypeSizeCheck::size_cpp) + .def_readonly("size_numpy", &DtypeSizeCheck::size_numpy) + .def("__repr__", [](const DtypeSizeCheck &self) { + return py::str("") + .format(self.name, self.size_cpp, self.size_numpy, self.dtype); + }); + sm.def("get_platform_dtype_size_checks", &get_platform_dtype_size_checks); + + // test_array_attributes + sm.def("ndim", [](const arr &a) { return a.ndim(); }); + sm.def("shape", [](const arr &a) { return arr(a.ndim(), a.shape()); }); + sm.def("shape", [](const arr &a, py::ssize_t dim) { return a.shape(dim); }); + sm.def("strides", [](const arr &a) { return arr(a.ndim(), a.strides()); }); + sm.def("strides", [](const arr &a, py::ssize_t dim) { return a.strides(dim); }); + sm.def("writeable", [](const arr &a) { return a.writeable(); }); + sm.def("size", [](const arr &a) { return a.size(); }); + sm.def("itemsize", [](const arr &a) { return a.itemsize(); }); + sm.def("nbytes", [](const arr &a) { return a.nbytes(); }); + sm.def("owndata", [](const arr &a) { return a.owndata(); }); + +#ifdef PYBIND11_HAS_SPAN + // test_shape_strides_span + sm.def("shape_span", [](const arr &a) { + auto span = a.shape_span(); + return std::vector(span.begin(), span.end()); + }); + sm.def("strides_span", [](const arr &a) { + auto span = a.strides_span(); + return std::vector(span.begin(), span.end()); + }); + // Test that spans can be used to construct new arrays + sm.def("array_from_spans", [](const arr &a) { + return py::array(a.dtype(), a.shape_span(), a.strides_span(), a.data(), a); + }); +#endif + + // test_index_offset + def_index_fn(index_at, const arr &); + def_index_fn(index_at_t, const arr_t &); + def_index_fn(offset_at, const arr &); + def_index_fn(offset_at_t, const arr_t &); + // test_data + def_index_fn(data, const arr &); + def_index_fn(data_t, const arr_t &); + // test_mutate_data, test_mutate_readonly + def_index_fn(mutate_data, arr &); + def_index_fn(mutate_data_t, arr_t &); + def_index_fn(at_t, const arr_t &); + def_index_fn(mutate_at_t, arr_t &); + + // test_make_c_f_array + sm.def("make_f_array", [] { return py::array_t({2, 2}, {4, 8}); }); + sm.def("make_c_array", [] { return py::array_t({2, 2}, {8, 4}); }); + + // test_empty_shaped_array + sm.def("make_empty_shaped_array", [] { return py::array(py::dtype("f"), {}, {}); }); + // test numpy scalars (empty shape, ndim==0) + sm.def("scalar_int", []() { return py::array(py::dtype("i"), {}, {}, &data_i); }); + + // test_wrap + sm.def("wrap", [](const py::array &a) { + return py::array(a.dtype(), + {a.shape(), a.shape() + a.ndim()}, + {a.strides(), a.strides() + a.ndim()}, + a.data(), + a); + }); + + // test_numpy_view + struct ArrayClass { + int data[2] = {1, 2}; + ArrayClass() { py::print("ArrayClass()"); } + ArrayClass(const ArrayClass &) = default; + ~ArrayClass() { py::print("~ArrayClass()"); } + }; + py::class_(sm, "ArrayClass") + .def(py::init<>()) + .def("numpy_view", [](py::object &obj) { + py::print("ArrayClass::numpy_view()"); + auto &a = obj.cast(); + return py::array_t({2}, {4}, a.data, obj); + }); + + // test_cast_numpy_int64_to_uint64 + sm.def("function_taking_uint64", [](uint64_t) {}); + + // test_isinstance + sm.def("isinstance_untyped", [](py::object yes, py::object no) { + return py::isinstance(std::move(yes)) + && !py::isinstance(std::move(no)); + }); + sm.def("isinstance_typed", [](const py::object &o) { + return py::isinstance>(o) && !py::isinstance>(o); + }); + + // test_constructors + sm.def("default_constructors", []() { + return py::dict("array"_a = py::array(), + "array_t"_a = py::array_t(), + "array_t"_a = py::array_t()); + }); + sm.def("converting_constructors", [](const py::object &o) { + return py::dict("array"_a = py::array(o), + "array_t"_a = py::array_t(o), + "array_t"_a = py::array_t(o)); + }); + + // test_overload_resolution + sm.def("overloaded", [](const py::array_t &) { return "double"; }); + sm.def("overloaded", [](const py::array_t &) { return "float"; }); + sm.def("overloaded", [](const py::array_t &) { return "int"; }); + sm.def("overloaded", [](const py::array_t &) { return "unsigned short"; }); + sm.def("overloaded", [](const py::array_t &) { return "long long"; }); + sm.def("overloaded", + [](const py::array_t> &) { return "double complex"; }); + sm.def("overloaded", [](const py::array_t> &) { return "float complex"; }); + + sm.def("overloaded2", + [](const py::array_t> &) { return "double complex"; }); + sm.def("overloaded2", [](const py::array_t &) { return "double"; }); + sm.def("overloaded2", + [](const py::array_t> &) { return "float complex"; }); + sm.def("overloaded2", [](const py::array_t &) { return "float"; }); + + // [workaround(intel)] ICC 20/21 breaks with py::arg().stuff, using py::arg{}.stuff works. + + // Only accept the exact types: + sm.def("overloaded3", [](const py::array_t &) { return "int"; }, py::arg{}.noconvert()); + sm.def( + "overloaded3", + [](const py::array_t &) { return "double"; }, + py::arg{}.noconvert()); + + // Make sure we don't do unsafe coercion (e.g. float to int) when not using forcecast, but + // rather that float gets converted via the safe (conversion to double) overload: + sm.def("overloaded4", [](const py::array_t &) { return "long long"; }); + sm.def("overloaded4", [](const py::array_t &) { return "double"; }); + + // But we do allow conversion to int if forcecast is enabled (but only if no overload matches + // without conversion) + sm.def("overloaded5", [](const py::array_t &) { return "unsigned int"; }); + sm.def("overloaded5", [](const py::array_t &) { return "double"; }); + + // test_greedy_string_overload + // Issue 685: ndarray shouldn't go to std::string overload + sm.def("issue685", [](const std::string &) { return "string"; }); + sm.def("issue685", [](const py::array &) { return "array"; }); + sm.def("issue685", [](const py::object &) { return "other"; }); + + // test_array_unchecked_fixed_dims + sm.def( + "proxy_add2", + [](py::array_t a, double v) { + auto r = a.mutable_unchecked<2>(); + for (py::ssize_t i = 0; i < r.shape(0); i++) { + for (py::ssize_t j = 0; j < r.shape(1); j++) { + r(i, j) += v; + } + } + }, + py::arg{}.noconvert(), + py::arg()); + + sm.def("proxy_init3", [](double start) { + py::array_t a({3, 3, 3}); + auto r = a.mutable_unchecked<3>(); + for (py::ssize_t i = 0; i < r.shape(0); i++) { + for (py::ssize_t j = 0; j < r.shape(1); j++) { + for (py::ssize_t k = 0; k < r.shape(2); k++) { + r(i, j, k) = start++; + } + } + } + return a; + }); + sm.def("proxy_init3F", [](double start) { + py::array_t a({3, 3, 3}); + auto r = a.mutable_unchecked<3>(); + for (py::ssize_t k = 0; k < r.shape(2); k++) { + for (py::ssize_t j = 0; j < r.shape(1); j++) { + for (py::ssize_t i = 0; i < r.shape(0); i++) { + r(i, j, k) = start++; + } + } + } + return a; + }); + sm.def("proxy_squared_L2_norm", [](const py::array_t &a) { + auto r = a.unchecked<1>(); + double sumsq = 0; + for (py::ssize_t i = 0; i < r.shape(0); i++) { + sumsq += r[i] * r(i); // Either notation works for a 1D array + } + return sumsq; + }); + + sm.def("proxy_auxiliaries2", [](py::array_t a) { + auto r = a.unchecked<2>(); + auto r2 = a.mutable_unchecked<2>(); + return auxiliaries(r, r2); + }); + + sm.def("proxy_auxiliaries1_const_ref", [](py::array_t a) { + const auto &r = a.unchecked<1>(); + const auto &r2 = a.mutable_unchecked<1>(); + return r(0) == r2(0) && r[0] == r2[0]; + }); + + sm.def("proxy_auxiliaries2_const_ref", [](py::array_t a) { + const auto &r = a.unchecked<2>(); + const auto &r2 = a.mutable_unchecked<2>(); + return r(0, 0) == r2(0, 0); + }); + + // test_array_unchecked_dyn_dims + // Same as the above, but without a compile-time dimensions specification: + sm.def( + "proxy_add2_dyn", + [](py::array_t a, double v) { + auto r = a.mutable_unchecked(); + if (r.ndim() != 2) { + throw std::domain_error("error: ndim != 2"); + } + for (py::ssize_t i = 0; i < r.shape(0); i++) { + for (py::ssize_t j = 0; j < r.shape(1); j++) { + r(i, j) += v; + } + } + }, + py::arg{}.noconvert(), + py::arg()); + sm.def("proxy_init3_dyn", [](double start) { + py::array_t a({3, 3, 3}); + auto r = a.mutable_unchecked(); + if (r.ndim() != 3) { + throw std::domain_error("error: ndim != 3"); + } + for (py::ssize_t i = 0; i < r.shape(0); i++) { + for (py::ssize_t j = 0; j < r.shape(1); j++) { + for (py::ssize_t k = 0; k < r.shape(2); k++) { + r(i, j, k) = start++; + } + } + } + return a; + }); + sm.def("proxy_auxiliaries2_dyn", [](py::array_t a) { + return auxiliaries(a.unchecked(), a.mutable_unchecked()); + }); + + sm.def("array_auxiliaries2", [](py::array_t a) { return auxiliaries(a, a); }); + + // test_array_failures + // Issue #785: Uninformative "Unknown internal error" exception when constructing array from + // empty object: + sm.def("array_fail_test", []() { return py::array(py::object()); }); + sm.def("array_t_fail_test", []() { return py::array_t(py::object()); }); + // Make sure the error from numpy is being passed through: + sm.def("array_fail_test_negative_size", []() { + int c = 0; + return py::array(-1, &c); + }); + + // test_initializer_list + // Issue (unnumbered; reported in #788): regression: initializer lists can be ambiguous + sm.def("array_initializer_list1", []() { return py::array_t(1); }); + // { 1 } also works for the above, but clang warns about it + sm.def("array_initializer_list2", []() { return py::array_t({1, 2}); }); + sm.def("array_initializer_list3", []() { return py::array_t({1, 2, 3}); }); + sm.def("array_initializer_list4", []() { return py::array_t({1, 2, 3, 4}); }); + + // test_array_resize + // reshape array to 2D without changing size + sm.def("array_reshape2", [](py::array_t a) { + const auto dim_sz = (py::ssize_t) std::sqrt(a.size()); + if (dim_sz * dim_sz != a.size()) { + throw std::domain_error( + "array_reshape2: input array total size is not a squared integer"); + } + a.resize({dim_sz, dim_sz}); + }); + + // resize to 3D array with each dimension = N + sm.def("array_resize3", + [](py::array_t a, size_t N, bool refcheck) { a.resize({N, N, N}, refcheck); }); + + // test_array_create_and_resize + // return 2D array with Nrows = Ncols = N + sm.def("create_and_resize", [](size_t N) { + py::array_t a; + a.resize({N, N}); + std::fill(a.mutable_data(), a.mutable_data() + a.size(), 42.); + return a; + }); + + sm.def("array_view", + [](py::array_t a, const std::string &dtype) { return a.view(dtype); }); + + sm.def("reshape_initializer_list", + [](py::array_t a, size_t N, size_t M, size_t O) { return a.reshape({N, M, O}); }); + sm.def("reshape_tuple", [](py::array_t a, const std::vector &new_shape) { + return a.reshape(new_shape); + }); + + sm.def("index_using_ellipsis", + [](const py::array &a) { return a[py::make_tuple(0, py::ellipsis(), 0)]; }); + + // test_argument_conversions + sm.def("accept_double", [](const py::array_t &) {}, py::arg("a")); + sm.def( + "accept_double_forcecast", + [](const py::array_t &) {}, + py::arg("a")); + sm.def( + "accept_double_c_style", + [](const py::array_t &) {}, + py::arg("a")); + sm.def( + "accept_double_c_style_forcecast", + [](const py::array_t &) {}, + py::arg("a")); + sm.def( + "accept_double_f_style", + [](const py::array_t &) {}, + py::arg("a")); + sm.def( + "accept_double_f_style_forcecast", + [](const py::array_t &) {}, + py::arg("a")); + sm.def("accept_double_noconvert", [](const py::array_t &) {}, "a"_a.noconvert()); + sm.def( + "accept_double_forcecast_noconvert", + [](const py::array_t &) {}, + "a"_a.noconvert()); + sm.def( + "accept_double_c_style_noconvert", + [](const py::array_t &) {}, + "a"_a.noconvert()); + sm.def( + "accept_double_c_style_forcecast_noconvert", + [](const py::array_t &) {}, + "a"_a.noconvert()); + sm.def( + "accept_double_f_style_noconvert", + [](const py::array_t &) {}, + "a"_a.noconvert()); + sm.def( + "accept_double_f_style_forcecast_noconvert", + [](const py::array_t &) {}, + "a"_a.noconvert()); + + // Check that types returns correct npy format descriptor + sm.def("test_fmt_desc_float", [](const py::array_t &) {}); + sm.def("test_fmt_desc_double", [](const py::array_t &) {}); + sm.def("test_fmt_desc_const_float", [](const py::array_t &) {}); + sm.def("test_fmt_desc_const_double", [](const py::array_t &) {}); + + sm.def("round_trip_float", [](double d) { return d; }); + + sm.def("pass_array_pyobject_ptr_return_sum_str_values", + pass_array_return_sum_str_values); + sm.def("pass_array_handle_return_sum_str_values", + pass_array_return_sum_str_values); + sm.def("pass_array_object_return_sum_str_values", + pass_array_return_sum_str_values); + + sm.def("pass_array_pyobject_ptr_return_as_list", pass_array_return_as_list); + sm.def("pass_array_handle_return_as_list", pass_array_return_as_list); + sm.def("pass_array_object_return_as_list", pass_array_return_as_list); + + sm.def("return_array_pyobject_ptr_cpp_loop", return_array_cpp_loop); + sm.def("return_array_handle_cpp_loop", return_array_cpp_loop); + sm.def("return_array_object_cpp_loop", return_array_cpp_loop); + + sm.def("return_array_pyobject_ptr_from_list", return_array_from_list); + sm.def("return_array_handle_from_list", return_array_from_list); + sm.def("return_array_object_from_list", return_array_from_list); + + sm.def( + "round_trip_array_t", + [](const py::array_t &x) -> py::array_t { return x; }, + py::arg("x")); + sm.def( + "round_trip_array_t_noconvert", + [](const py::array_t &x) -> py::array_t { return x; }, + py::arg("x").noconvert()); +} diff --git a/external_libraries/pybind11/tests/test_numpy_array.py b/external_libraries/pybind11/tests/test_numpy_array.py new file mode 100644 index 00000000..93477aa2 --- /dev/null +++ b/external_libraries/pybind11/tests/test_numpy_array.py @@ -0,0 +1,749 @@ +from __future__ import annotations + +import pytest + +import env # noqa: F401 +from pybind11_tests import numpy_array as m + +np = pytest.importorskip("numpy") + + +def test_dtypes(): + # See issue #1328. + # - Platform-dependent sizes. + for size_check in m.get_platform_dtype_size_checks(): + print(size_check) + assert size_check.size_cpp == size_check.size_numpy, size_check + # - Concrete sizes. + for check in m.get_concrete_dtype_checks(): + print(check) + assert check.numpy == check.pybind11, check + if check.numpy.num != check.pybind11.num: + print( + f"NOTE: typenum mismatch for {check}: {check.numpy.num} != {check.pybind11.num}" + ) + + +@pytest.fixture +def arr(): + return np.array([[1, 2, 3], [4, 5, 6]], "=u2") + + +def test_array_attributes(): + a = np.array(0, "f8") + assert m.ndim(a) == 0 + assert all(m.shape(a) == []) + assert all(m.strides(a) == []) + with pytest.raises(IndexError) as excinfo: + m.shape(a, 0) + assert str(excinfo.value) == "invalid axis: 0 (ndim = 0)" + with pytest.raises(IndexError) as excinfo: + m.strides(a, 0) + assert str(excinfo.value) == "invalid axis: 0 (ndim = 0)" + assert m.writeable(a) + assert m.size(a) == 1 + assert m.itemsize(a) == 8 + assert m.nbytes(a) == 8 + assert m.owndata(a) + + a = np.array([[1, 2, 3], [4, 5, 6]], "u2").view() + a.flags.writeable = False + assert m.ndim(a) == 2 + assert all(m.shape(a) == [2, 3]) + assert m.shape(a, 0) == 2 + assert m.shape(a, 1) == 3 + assert all(m.strides(a) == [6, 2]) + assert m.strides(a, 0) == 6 + assert m.strides(a, 1) == 2 + with pytest.raises(IndexError) as excinfo: + m.shape(a, 2) + assert str(excinfo.value) == "invalid axis: 2 (ndim = 2)" + with pytest.raises(IndexError) as excinfo: + m.strides(a, 2) + assert str(excinfo.value) == "invalid axis: 2 (ndim = 2)" + assert not m.writeable(a) + assert m.size(a) == 6 + assert m.itemsize(a) == 2 + assert m.nbytes(a) == 12 + assert not m.owndata(a) + + +@pytest.mark.skipif(not hasattr(m, "shape_span"), reason="std::span not available") +def test_shape_strides_span(): + # Test 0-dimensional array (scalar) + a = np.array(42, "f8") + assert m.ndim(a) == 0 + assert m.shape_span(a) == [] + assert m.strides_span(a) == [] + + # Test 1-dimensional array + a = np.array([1, 2, 3, 4], "u2") + assert m.ndim(a) == 1 + assert m.shape_span(a) == [4] + assert m.strides_span(a) == [2] + + # Test 2-dimensional array + a = np.array([[1, 2, 3], [4, 5, 6]], "u2").view() + a.flags.writeable = False + assert m.ndim(a) == 2 + assert m.shape_span(a) == [2, 3] + assert m.strides_span(a) == [6, 2] + + # Test 3-dimensional array + a = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]], "i4") + assert m.ndim(a) == 3 + assert m.shape_span(a) == [2, 2, 2] + # Verify spans match regular shape/strides + assert list(m.shape_span(a)) == list(m.shape(a)) + assert list(m.strides_span(a)) == list(m.strides(a)) + + # Test that spans can be used to construct new arrays + original = np.array([[1, 2, 3], [4, 5, 6]], "f4") + new_array = m.array_from_spans(original) + assert new_array.shape == original.shape + assert new_array.strides == original.strides + assert new_array.dtype == original.dtype + # Verify data is shared (since we pass the same data pointer) + np.testing.assert_array_equal(new_array, original) + + +@pytest.mark.parametrize( + ("args", "ret"), [([], 0), ([0], 0), ([1], 3), ([0, 1], 1), ([1, 2], 5)] +) +def test_index_offset(arr, args, ret): + assert m.index_at(arr, *args) == ret + assert m.index_at_t(arr, *args) == ret + assert m.offset_at(arr, *args) == ret * arr.dtype.itemsize + assert m.offset_at_t(arr, *args) == ret * arr.dtype.itemsize + + +def test_dim_check_fail(arr): + for func in ( + m.index_at, + m.index_at_t, + m.offset_at, + m.offset_at_t, + m.data, + m.data_t, + m.mutate_data, + m.mutate_data_t, + ): + with pytest.raises(IndexError) as excinfo: + func(arr, 1, 2, 3) + assert str(excinfo.value) == "too many indices for an array: 3 (ndim = 2)" + + +@pytest.mark.parametrize( + ("args", "ret"), + [ + ([], [1, 2, 3, 4, 5, 6]), + ([1], [4, 5, 6]), + ([0, 1], [2, 3, 4, 5, 6]), + ([1, 2], [6]), + ], +) +def test_data(arr, args, ret): + from sys import byteorder + + assert all(m.data_t(arr, *args) == ret) + assert all(m.data(arr, *args)[(0 if byteorder == "little" else 1) :: 2] == ret) + assert all(m.data(arr, *args)[(1 if byteorder == "little" else 0) :: 2] == 0) + + +@pytest.mark.parametrize("dim", [0, 1, 3]) +def test_at_fail(arr, dim): + for func in m.at_t, m.mutate_at_t: + with pytest.raises(IndexError) as excinfo: + func(arr, *([0] * dim)) + assert str(excinfo.value) == f"index dimension mismatch: {dim} (ndim = 2)" + + +def test_at(arr): + assert m.at_t(arr, 0, 2) == 3 + assert m.at_t(arr, 1, 0) == 4 + + assert all(m.mutate_at_t(arr, 0, 2).ravel() == [1, 2, 4, 4, 5, 6]) + assert all(m.mutate_at_t(arr, 1, 0).ravel() == [1, 2, 4, 5, 5, 6]) + + +def test_mutate_readonly(arr): + arr.flags.writeable = False + for func, args in ( + (m.mutate_data, ()), + (m.mutate_data_t, ()), + (m.mutate_at_t, (0, 0)), + ): + with pytest.raises(ValueError) as excinfo: + func(arr, *args) + assert str(excinfo.value) == "array is not writeable" + + +def test_mutate_data(arr): + assert all(m.mutate_data(arr).ravel() == [2, 4, 6, 8, 10, 12]) + assert all(m.mutate_data(arr).ravel() == [4, 8, 12, 16, 20, 24]) + assert all(m.mutate_data(arr, 1).ravel() == [4, 8, 12, 32, 40, 48]) + assert all(m.mutate_data(arr, 0, 1).ravel() == [4, 16, 24, 64, 80, 96]) + assert all(m.mutate_data(arr, 1, 2).ravel() == [4, 16, 24, 64, 80, 192]) + + assert all(m.mutate_data_t(arr).ravel() == [5, 17, 25, 65, 81, 193]) + assert all(m.mutate_data_t(arr).ravel() == [6, 18, 26, 66, 82, 194]) + assert all(m.mutate_data_t(arr, 1).ravel() == [6, 18, 26, 67, 83, 195]) + assert all(m.mutate_data_t(arr, 0, 1).ravel() == [6, 19, 27, 68, 84, 196]) + assert all(m.mutate_data_t(arr, 1, 2).ravel() == [6, 19, 27, 68, 84, 197]) + + +def test_bounds_check(arr): + for func in ( + m.index_at, + m.index_at_t, + m.data, + m.data_t, + m.mutate_data, + m.mutate_data_t, + m.at_t, + m.mutate_at_t, + ): + with pytest.raises(IndexError) as excinfo: + func(arr, 2, 0) + assert str(excinfo.value) == "index 2 is out of bounds for axis 0 with size 2" + with pytest.raises(IndexError) as excinfo: + func(arr, 0, 4) + assert str(excinfo.value) == "index 4 is out of bounds for axis 1 with size 3" + + +def test_make_c_f_array(): + assert m.make_c_array().flags.c_contiguous + assert not m.make_c_array().flags.f_contiguous + assert m.make_f_array().flags.f_contiguous + assert not m.make_f_array().flags.c_contiguous + + +def test_make_empty_shaped_array(): + m.make_empty_shaped_array() + + # empty shape means numpy scalar, PEP 3118 + assert m.scalar_int().ndim == 0 + assert m.scalar_int().shape == () + assert m.scalar_int() == 42 + + +def test_wrap(): + def assert_references(a, b, base=None): + if base is None: + base = a + assert a is not b + assert a.__array_interface__["data"][0] == b.__array_interface__["data"][0] + assert a.shape == b.shape + assert a.strides == b.strides + assert a.flags.c_contiguous == b.flags.c_contiguous + assert a.flags.f_contiguous == b.flags.f_contiguous + assert a.flags.writeable == b.flags.writeable + assert a.flags.aligned == b.flags.aligned + assert a.flags.writebackifcopy == b.flags.writebackifcopy + assert np.all(a == b) + assert not b.flags.owndata + assert b.base is base + if a.flags.writeable and a.ndim == 2: + a[0, 0] = 1234 + assert b[0, 0] == 1234 + + a1 = np.array([1, 2], dtype=np.int16) + assert a1.flags.owndata + assert a1.base is None + a2 = m.wrap(a1) + assert_references(a1, a2) + + a1 = np.array([[1, 2], [3, 4]], dtype=np.float32, order="F") + assert a1.flags.owndata + assert a1.base is None + a2 = m.wrap(a1) + assert_references(a1, a2) + + a1 = np.array([[1, 2], [3, 4]], dtype=np.float32, order="C") + a1.flags.writeable = False + a2 = m.wrap(a1) + assert_references(a1, a2) + + a1 = np.random.random((4, 4, 4)) + a2 = m.wrap(a1) + assert_references(a1, a2) + + a1t = a1.transpose() + a2 = m.wrap(a1t) + assert_references(a1t, a2, a1) + + a1d = a1.diagonal() + a2 = m.wrap(a1d) + assert_references(a1d, a2, a1) + + a1m = a1[::-1, ::-1, ::-1] + a2 = m.wrap(a1m) + assert_references(a1m, a2, a1) + + +@pytest.mark.skipif("env.GRAALPY", reason="Cannot reliably trigger GC") +def test_numpy_view(capture): + with capture: + ac = m.ArrayClass() + ac_view_1 = ac.numpy_view() + ac_view_2 = ac.numpy_view() + assert np.all(ac_view_1 == np.array([1, 2], dtype=np.int32)) + del ac + pytest.gc_collect() + assert ( + capture + == """ + ArrayClass() + ArrayClass::numpy_view() + ArrayClass::numpy_view() + """ + ) + ac_view_1[0] = 4 + ac_view_1[1] = 3 + assert ac_view_2[0] == 4 + assert ac_view_2[1] == 3 + with capture: + del ac_view_1 + del ac_view_2 + pytest.gc_collect() + pytest.gc_collect() + assert ( + capture + == """ + ~ArrayClass() + """ + ) + + +def test_cast_numpy_int64_to_uint64(): + m.function_taking_uint64(123) + m.function_taking_uint64(np.uint64(123)) + + +def test_isinstance(): + assert m.isinstance_untyped(np.array([1, 2, 3]), "not an array") + assert m.isinstance_typed(np.array([1.0, 2.0, 3.0])) + + +def test_constructors(): + defaults = m.default_constructors() + for a in defaults.values(): + assert a.size == 0 + assert defaults["array"].dtype == np.array([]).dtype + assert defaults["array_t"].dtype == np.int32 + assert defaults["array_t"].dtype == np.float64 + + results = m.converting_constructors([1, 2, 3]) + for a in results.values(): + np.testing.assert_array_equal(a, [1, 2, 3]) + assert results["array"].dtype == np.dtype(int) + assert results["array_t"].dtype == np.int32 + assert results["array_t"].dtype == np.float64 + + +def test_array_object_type(doc): + assert ( + doc(m.pass_array_object_return_as_list) + == "pass_array_object_return_as_list(arg0: typing.Annotated[numpy.typing.ArrayLike, numpy.object_]) -> list" + ) + + +def test_overload_resolution(msg): + # Exact overload matches: + assert m.overloaded(np.array([1], dtype="float64")) == "double" + assert m.overloaded(np.array([1], dtype="float32")) == "float" + assert m.overloaded(np.array([1], dtype="ushort")) == "unsigned short" + assert m.overloaded(np.array([1], dtype="intc")) == "int" + assert m.overloaded(np.array([1], dtype="longlong")) == "long long" + assert m.overloaded(np.array([1], dtype="complex")) == "double complex" + assert m.overloaded(np.array([1], dtype="csingle")) == "float complex" + + # No exact match, should call first convertible version: + assert m.overloaded(np.array([1], dtype="uint8")) == "double" + + with pytest.raises(TypeError) as excinfo: + m.overloaded("not an array") + assert ( + msg(excinfo.value) + == """ + overloaded(): incompatible function arguments. The following argument types are supported: + 1. (arg0: typing.Annotated[numpy.typing.ArrayLike, numpy.float64]) -> str + 2. (arg0: typing.Annotated[numpy.typing.ArrayLike, numpy.float32]) -> str + 3. (arg0: typing.Annotated[numpy.typing.ArrayLike, numpy.int32]) -> str + 4. (arg0: typing.Annotated[numpy.typing.ArrayLike, numpy.uint16]) -> str + 5. (arg0: typing.Annotated[numpy.typing.ArrayLike, numpy.int64]) -> str + 6. (arg0: typing.Annotated[numpy.typing.ArrayLike, numpy.complex128]) -> str + 7. (arg0: typing.Annotated[numpy.typing.ArrayLike, numpy.complex64]) -> str + + Invoked with: 'not an array' + """ + ) + + assert m.overloaded2(np.array([1], dtype="float64")) == "double" + assert m.overloaded2(np.array([1], dtype="float32")) == "float" + assert m.overloaded2(np.array([1], dtype="complex64")) == "float complex" + assert m.overloaded2(np.array([1], dtype="complex128")) == "double complex" + assert m.overloaded2(np.array([1], dtype="float32")) == "float" + + assert m.overloaded3(np.array([1], dtype="float64")) == "double" + assert m.overloaded3(np.array([1], dtype="intc")) == "int" + expected_exc = """ + overloaded3(): incompatible function arguments. The following argument types are supported: + 1. (arg0: numpy.typing.NDArray[numpy.int32]) -> str + 2. (arg0: numpy.typing.NDArray[numpy.float64]) -> str + + Invoked with: """ + + with pytest.raises(TypeError) as excinfo: + m.overloaded3(np.array([1], dtype="uintc")) + assert msg(excinfo.value) == expected_exc + repr(np.array([1], dtype="uint32")) + with pytest.raises(TypeError) as excinfo: + m.overloaded3(np.array([1], dtype="float32")) + assert msg(excinfo.value) == expected_exc + repr(np.array([1.0], dtype="float32")) + with pytest.raises(TypeError) as excinfo: + m.overloaded3(np.array([1], dtype="complex")) + assert msg(excinfo.value) == expected_exc + repr(np.array([1.0 + 0.0j])) + + # Exact matches: + assert m.overloaded4(np.array([1], dtype="double")) == "double" + assert m.overloaded4(np.array([1], dtype="longlong")) == "long long" + # Non-exact matches requiring conversion. Since float to integer isn't a + # save conversion, it should go to the double overload, but short can go to + # either (and so should end up on the first-registered, the long long). + assert m.overloaded4(np.array([1], dtype="float32")) == "double" + assert m.overloaded4(np.array([1], dtype="short")) == "long long" + + assert m.overloaded5(np.array([1], dtype="double")) == "double" + assert m.overloaded5(np.array([1], dtype="uintc")) == "unsigned int" + assert m.overloaded5(np.array([1], dtype="float32")) == "unsigned int" + + +def test_greedy_string_overload(): + """Tests fix for #685 - ndarray shouldn't go to std::string overload""" + + assert m.issue685("abc") == "string" + assert m.issue685(np.array([97, 98, 99], dtype="b")) == "array" + assert m.issue685(123) == "other" + + +def test_array_unchecked_fixed_dims(msg): + z1 = np.array([[1, 2], [3, 4]], dtype="float64") + m.proxy_add2(z1, 10) + assert np.all(z1 == [[11, 12], [13, 14]]) + + with pytest.raises(ValueError) as excinfo: + m.proxy_add2(np.array([1.0, 2, 3]), 5.0) + assert ( + msg(excinfo.value) == "array has incorrect number of dimensions: 1; expected 2" + ) + + expect_c = np.ndarray(shape=(3, 3, 3), buffer=np.array(range(3, 30)), dtype="int") + assert np.all(m.proxy_init3(3.0) == expect_c) + expect_f = np.transpose(expect_c) + assert np.all(m.proxy_init3F(3.0) == expect_f) + + assert m.proxy_squared_L2_norm(np.array(range(6))) == 55 + assert m.proxy_squared_L2_norm(np.array(range(6), dtype="float64")) == 55 + + assert m.proxy_auxiliaries2(z1) == [11, 11, True, 2, 8, 2, 2, 4, 32] + assert m.proxy_auxiliaries2(z1) == m.array_auxiliaries2(z1) + + assert m.proxy_auxiliaries1_const_ref(z1[0, :]) + assert m.proxy_auxiliaries2_const_ref(z1) + + +def test_array_unchecked_dyn_dims(): + z1 = np.array([[1, 2], [3, 4]], dtype="float64") + m.proxy_add2_dyn(z1, 10) + assert np.all(z1 == [[11, 12], [13, 14]]) + + expect_c = np.ndarray(shape=(3, 3, 3), buffer=np.array(range(3, 30)), dtype="int") + assert np.all(m.proxy_init3_dyn(3.0) == expect_c) + + assert m.proxy_auxiliaries2_dyn(z1) == [11, 11, True, 2, 8, 2, 2, 4, 32] + assert m.proxy_auxiliaries2_dyn(z1) == m.array_auxiliaries2(z1) + + +def test_array_failure(): + with pytest.raises(ValueError) as excinfo: + m.array_fail_test() + assert str(excinfo.value) == "cannot create a pybind11::array from a nullptr" + + with pytest.raises(ValueError) as excinfo: + m.array_t_fail_test() + assert str(excinfo.value) == "cannot create a pybind11::array_t from a nullptr" + + with pytest.raises(ValueError) as excinfo: + m.array_fail_test_negative_size() + assert str(excinfo.value) == "negative dimensions are not allowed" + + +def test_initializer_list(): + assert m.array_initializer_list1().shape == (1,) + assert m.array_initializer_list2().shape == (1, 2) + assert m.array_initializer_list3().shape == (1, 2, 3) + assert m.array_initializer_list4().shape == (1, 2, 3, 4) + + +def test_array_resize(): + a = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9], dtype="float64") + m.array_reshape2(a) + assert a.size == 9 + assert np.all(a == [[1, 2, 3], [4, 5, 6], [7, 8, 9]]) + + # total size change should succced with refcheck off + m.array_resize3(a, 4, False) + assert a.size == 64 + # ... and fail with refcheck on + try: + m.array_resize3(a, 3, True) + except ValueError as e: + assert str(e).startswith("cannot resize an array") # noqa: PT017 + # transposed array doesn't own data + b = a.transpose() + try: + m.array_resize3(b, 3, False) + except ValueError as e: + assert str(e).startswith( # noqa: PT017 + "cannot resize this array: it does not own its data" + ) + # ... but reshape should be fine + m.array_reshape2(b) + assert b.shape == (8, 8) + + +@pytest.mark.xfail("env.PYPY or env.GRAALPY") +def test_array_create_and_resize(): + a = m.create_and_resize(2) + assert a.size == 4 + assert np.all(a == 42.0) + + +def test_array_view(): + a = np.ones(100 * 4).astype("uint8") + a_float_view = m.array_view(a, "float32") + assert a_float_view.shape == (100 * 1,) # 1 / 4 bytes = 8 / 32 + + a_int16_view = m.array_view(a, "int16") # 1 / 2 bytes = 16 / 32 + assert a_int16_view.shape == (100 * 2,) + + +def test_array_view_invalid(): + a = np.ones(100 * 4).astype("uint8") + with pytest.raises(TypeError): + m.array_view(a, "deadly_dtype") + + +def test_reshape_initializer_list(): + a = np.arange(2 * 7 * 3) + 1 + x = m.reshape_initializer_list(a, 2, 7, 3) + assert x.shape == (2, 7, 3) + assert list(x[1][4]) == [34, 35, 36] + with pytest.raises(ValueError) as excinfo: + m.reshape_initializer_list(a, 1, 7, 3) + assert str(excinfo.value) == "cannot reshape array of size 42 into shape (1,7,3)" + + +def test_reshape_tuple(): + a = np.arange(3 * 7 * 2) + 1 + x = m.reshape_tuple(a, (3, 7, 2)) + assert x.shape == (3, 7, 2) + assert list(x[1][4]) == [23, 24] + y = m.reshape_tuple(x, (x.size,)) + assert y.shape == (42,) + with pytest.raises(ValueError) as excinfo: + m.reshape_tuple(a, (3, 7, 1)) + assert str(excinfo.value) == "cannot reshape array of size 42 into shape (3,7,1)" + with pytest.raises(ValueError) as excinfo: + m.reshape_tuple(a, ()) + assert str(excinfo.value) == "cannot reshape array of size 42 into shape ()" + + +def test_index_using_ellipsis(): + a = m.index_using_ellipsis(np.zeros((5, 6, 7))) + assert a.shape == (6,) + + +@pytest.mark.parametrize( + "test_func", + [ + m.test_fmt_desc_float, + m.test_fmt_desc_double, + m.test_fmt_desc_const_float, + m.test_fmt_desc_const_double, + ], +) +def test_format_descriptors_for_floating_point_types(test_func): + assert "numpy.typing.ArrayLike, numpy.float" in test_func.__doc__ + + +@pytest.mark.parametrize("forcecast", [False, True]) +@pytest.mark.parametrize("contiguity", [None, "C", "F"]) +@pytest.mark.parametrize("noconvert", [False, True]) +@pytest.mark.filterwarnings( + "ignore:Casting complex values to real discards the imaginary part:" + + ( + "numpy.exceptions.ComplexWarning" + if hasattr(np, "exceptions") + else "numpy.ComplexWarning" + ) +) +def test_argument_conversions(forcecast, contiguity, noconvert): + function_name = "accept_double" + if contiguity == "C": + function_name += "_c_style" + elif contiguity == "F": + function_name += "_f_style" + if forcecast: + function_name += "_forcecast" + if noconvert: + function_name += "_noconvert" + function = getattr(m, function_name) + + for dtype in [np.dtype("float32"), np.dtype("float64"), np.dtype("complex128")]: + for order in ["C", "F"]: + for shape in [(2, 2), (1, 3, 1, 1), (1, 1, 1), (0,)]: + if not noconvert: + # If noconvert is not passed, only complex128 needs to be truncated and + # "cannot be safely obtained". So without `forcecast`, the argument shouldn't + # be accepted. + should_raise = dtype.name == "complex128" and not forcecast + else: + # If noconvert is passed, only float64 and the matching order is accepted. + # If at most one dimension has a size greater than 1, the array is also + # trivially contiguous. + trivially_contiguous = sum(1 for d in shape if d > 1) <= 1 + should_raise = dtype.name != "float64" or ( + contiguity is not None + and contiguity != order + and not trivially_contiguous + ) + + array = np.zeros(shape, dtype=dtype, order=order) + if not should_raise: + function(array) + else: + with pytest.raises( + TypeError, match="incompatible function arguments" + ): + function(array) + + +@pytest.mark.xfail("env.PYPY") +def test_dtype_refcount_leak(): + from sys import getrefcount + + # Was np.float_ but that alias for float64 was removed in NumPy 2. + dtype = np.dtype(np.float64) + a = np.array([1], dtype=dtype) + before = getrefcount(dtype) + m.ndim(a) + after = getrefcount(dtype) + assert after == before + + +def test_round_trip_float(): + arr = np.zeros((), np.float64) + arr[()] = 37.2 + assert m.round_trip_float(arr) == 37.2 + + +# HINT: An easy and robust way (although only manual unfortunately) to check for +# ref-count leaks in the test_.*pyobject_ptr.* functions below is to +# * temporarily insert `while True:` (one-by-one), +# * run this test, and +# * run the Linux `top` command in another shell to visually monitor +# `RES` for a minute or two. +# If there is a leak, it is usually evident in seconds because the `RES` +# value increases without bounds. (Don't forget to Ctrl-C the test!) + + +# For use as a temporary user-defined object, to maximize sensitivity of the tests below: +# * Ref-count leaks will be immediately evident. +# * Sanitizers are much more likely to detect heap-use-after-free due to +# other ref-count bugs. +class PyValueHolder: + def __init__(self, value): + self.value = value + + +def WrapWithPyValueHolder(*values): + return [PyValueHolder(v) for v in values] + + +def UnwrapPyValueHolder(vhs): + return [vh.value for vh in vhs] + + +PASS_ARRAY_PYOBJECT_RETURN_SUM_STR_VALUES_FUNCTIONS = [ + m.pass_array_pyobject_ptr_return_sum_str_values, + m.pass_array_handle_return_sum_str_values, + m.pass_array_object_return_sum_str_values, +] + + +@pytest.mark.parametrize( + "pass_array", PASS_ARRAY_PYOBJECT_RETURN_SUM_STR_VALUES_FUNCTIONS +) +def test_pass_array_object_return_sum_str_values_ndarray(pass_array): + # Intentionally all temporaries, do not change. + assert ( + pass_array(np.array(WrapWithPyValueHolder(-3, "four", 5.0), dtype=object)) + == "-3four5.0" + ) + + +@pytest.mark.parametrize( + "pass_array", PASS_ARRAY_PYOBJECT_RETURN_SUM_STR_VALUES_FUNCTIONS +) +def test_pass_array_object_return_sum_str_values_list(pass_array): + # Intentionally all temporaries, do not change. + assert pass_array(WrapWithPyValueHolder(2, "three", -4.0)) == "2three-4.0" + + +@pytest.mark.parametrize( + "pass_array", + [ + m.pass_array_pyobject_ptr_return_as_list, + m.pass_array_handle_return_as_list, + m.pass_array_object_return_as_list, + ], +) +def test_pass_array_object_return_as_list(pass_array): + # Intentionally all temporaries, do not change. + assert UnwrapPyValueHolder( + pass_array(np.array(WrapWithPyValueHolder(-1, "two", 3.0), dtype=object)) + ) == [-1, "two", 3.0] + + +@pytest.mark.parametrize( + ("return_array", "unwrap"), + [ + (m.return_array_pyobject_ptr_cpp_loop, list), + (m.return_array_handle_cpp_loop, list), + (m.return_array_object_cpp_loop, list), + (m.return_array_pyobject_ptr_from_list, UnwrapPyValueHolder), + (m.return_array_handle_from_list, UnwrapPyValueHolder), + (m.return_array_object_from_list, UnwrapPyValueHolder), + ], +) +def test_return_array_object_cpp_loop(return_array, unwrap): + # Intentionally all temporaries, do not change. + arr_from_list = return_array(WrapWithPyValueHolder(6, "seven", -8.0)) + assert isinstance(arr_from_list, np.ndarray) + assert arr_from_list.dtype == np.dtype("O") + assert unwrap(arr_from_list) == [6, "seven", -8.0] + + +def test_arraylike_signature(doc): + assert ( + doc(m.round_trip_array_t) + == "round_trip_array_t(x: typing.Annotated[numpy.typing.ArrayLike, numpy.float32]) -> numpy.typing.NDArray[numpy.float32]" + ) + assert ( + doc(m.round_trip_array_t_noconvert) + == "round_trip_array_t_noconvert(x: numpy.typing.NDArray[numpy.float32]) -> numpy.typing.NDArray[numpy.float32]" + ) + m.round_trip_array_t([1, 2, 3]) + with pytest.raises(TypeError, match="incompatible function arguments"): + m.round_trip_array_t_noconvert([1, 2, 3]) diff --git a/external_libraries/pybind11/tests/test_numpy_dtypes.cpp b/external_libraries/pybind11/tests/test_numpy_dtypes.cpp new file mode 100644 index 00000000..f206da73 --- /dev/null +++ b/external_libraries/pybind11/tests/test_numpy_dtypes.cpp @@ -0,0 +1,745 @@ +/* + tests/test_numpy_dtypes.cpp -- Structured and compound NumPy dtypes + + Copyright (c) 2016 Ivan Smirnov + + All rights reserved. Use of this source code is governed by a + BSD-style license that can be found in the LICENSE file. +*/ + +#include + +#include "pybind11_tests.h" + +#include +#include + +#ifdef __GNUC__ +# define PYBIND11_PACKED(cls) cls __attribute__((__packed__)) +#else +# define PYBIND11_PACKED(cls) __pragma(pack(push, 1)) cls __pragma(pack(pop)) +#endif + +namespace py = pybind11; + +struct SimpleStruct { + bool bool_; + uint32_t uint_; + float float_; + long double ldbl_; +}; + +std::ostream &operator<<(std::ostream &os, const SimpleStruct &v) { + return os << "s:" << v.bool_ << "," << v.uint_ << "," << v.float_ << "," << v.ldbl_; +} + +struct SimpleStructReordered { + bool bool_; + float float_; + uint32_t uint_; + long double ldbl_; +}; + +PYBIND11_PACKED(struct PackedStruct { + bool bool_; + uint32_t uint_; + float float_; + long double ldbl_; +}); + +std::ostream &operator<<(std::ostream &os, const PackedStruct &v) { + return os << "p:" << v.bool_ << "," << v.uint_ << "," << v.float_ << "," << v.ldbl_; +} + +PYBIND11_PACKED(struct NestedStruct { + SimpleStruct a; + PackedStruct b; +}); + +std::ostream &operator<<(std::ostream &os, const NestedStruct &v) { + return os << "n:a=" << v.a << ";b=" << v.b; +} + +struct PartialStruct { + bool bool_; + uint32_t uint_; + float float_; + uint64_t dummy2; + long double ldbl_; +}; + +struct PartialNestedStruct { + uint64_t dummy1; + PartialStruct a; + uint64_t dummy2; +}; + +struct UnboundStruct {}; + +struct StringStruct { + char a[3]; + std::array b; +}; + +struct ComplexStruct { + std::complex cflt; + std::complex cdbl; +}; + +std::ostream &operator<<(std::ostream &os, const ComplexStruct &v) { + return os << "c:" << v.cflt << "," << v.cdbl; +} + +struct ArrayStruct { + char a[3][4]; + int32_t b[2]; + std::array c; + std::array d[4]; +}; + +PYBIND11_PACKED(struct StructWithUglyNames { + int8_t __x__; + uint64_t __y__; +}); + +enum class E1 : int64_t { A = -1, B = 1 }; +enum E2 : uint8_t { X = 1, Y = 2 }; + +PYBIND11_PACKED(struct EnumStruct { + E1 e1; + E2 e2; +}); + +std::ostream &operator<<(std::ostream &os, const StringStruct &v) { + os << "a='"; + for (size_t i = 0; i < 3 && (v.a[i] != 0); i++) { + os << v.a[i]; + } + os << "',b='"; + for (size_t i = 0; i < 3 && (v.b[i] != 0); i++) { + os << v.b[i]; + } + return os << "'"; +} + +std::ostream &operator<<(std::ostream &os, const ArrayStruct &v) { + os << "a={"; + for (int i = 0; i < 3; i++) { + if (i > 0) { + os << ','; + } + os << '{'; + for (int j = 0; j < 3; j++) { + os << v.a[i][j] << ','; + } + os << v.a[i][3] << '}'; + } + os << "},b={" << v.b[0] << ',' << v.b[1]; + os << "},c={" << int(v.c[0]) << ',' << int(v.c[1]) << ',' << int(v.c[2]); + os << "},d={"; + for (int i = 0; i < 4; i++) { + if (i > 0) { + os << ','; + } + os << '{' << v.d[i][0] << ',' << v.d[i][1] << '}'; + } + return os << '}'; +} + +std::ostream &operator<<(std::ostream &os, const EnumStruct &v) { + return os << "e1=" << (v.e1 == E1::A ? "A" : "B") << ",e2=" << (v.e2 == E2::X ? "X" : "Y"); +} + +template +py::array mkarray_via_buffer(size_t n) { + return py::array(py::buffer_info( + nullptr, sizeof(T), py::format_descriptor::format(), 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) + +template +py::array_t create_recarray(size_t n) { + auto arr = mkarray_via_buffer(n); + auto req = arr.request(); + auto *ptr = static_cast(req.ptr); + for (size_t i = 0; i < n; i++) { + SET_TEST_VALS(ptr[i], i); + } + return arr; +} + +template +py::list print_recarray(const py::array_t &arr) { + const auto req = arr.request(); + auto *const ptr = static_cast(req.ptr); + auto l = py::list(); + for (py::ssize_t i = 0; i < req.size; i++) { + std::stringstream ss; + ss << ptr[i]; + l.append(py::str(ss.str())); + } + return l; +} + +py::array_t test_array_ctors(int i) { + using arr_t = py::array_t; + + std::vector data{1, 2, 3, 4, 5, 6}; + std::vector shape{3, 2}; + std::vector strides{8, 4}; + + auto *ptr = data.data(); + auto *vptr = (void *) ptr; + auto dtype = py::dtype("int32"); + + py::buffer_info buf_ndim1(vptr, 4, "i", 6); + py::buffer_info buf_ndim1_null(nullptr, 4, "i", 6); + py::buffer_info buf_ndim2(vptr, 4, "i", 2, shape, strides); + py::buffer_info buf_ndim2_null(nullptr, 4, "i", 2, shape, strides); + + auto fill = [](py::array arr) { + auto req = arr.request(); + for (int i = 0; i < 6; i++) { + ((int32_t *) req.ptr)[i] = i + 1; + } + return arr; + }; + + switch (i) { + // shape: (3, 2) + case 10: + return arr_t(shape, strides, ptr); + case 11: + return py::array(shape, strides, ptr); + case 12: + return py::array(dtype, shape, strides, vptr); + case 13: + return arr_t(shape, ptr); + case 14: + return py::array(shape, ptr); + case 15: + return py::array(dtype, shape, vptr); + case 16: + return arr_t(buf_ndim2); + case 17: + return py::array(buf_ndim2); + // shape: (3, 2) - post-fill + case 20: + return fill(arr_t(shape, strides)); + case 21: + return py::array(shape, strides, ptr); // can't have nullptr due to templated ctor + case 22: + return fill(py::array(dtype, shape, strides)); + case 23: + return fill(arr_t(shape)); + case 24: + return py::array(shape, ptr); // can't have nullptr due to templated ctor + case 25: + return fill(py::array(dtype, shape)); + case 26: + return fill(arr_t(buf_ndim2_null)); + case 27: + return fill(py::array(buf_ndim2_null)); + // shape: (6, ) + case 30: + return arr_t(6, ptr); + case 31: + return py::array(6, ptr); + case 32: + return py::array(dtype, 6, vptr); + case 33: + return arr_t(buf_ndim1); + case 34: + return py::array(buf_ndim1); + // shape: (6, ) + case 40: + return fill(arr_t(6)); + case 41: + return py::array(6, ptr); // can't have nullptr due to templated ctor + case 42: + return fill(py::array(dtype, 6)); + case 43: + return fill(arr_t(buf_ndim1_null)); + case 44: + return fill(py::array(buf_ndim1_null)); + default: + break; + } + return arr_t(); +} + +py::list test_dtype_ctors() { + py::list list; + list.append(py::dtype("int32")); + list.append(py::dtype(std::string("float64"))); + list.append(py::dtype::from_args(py::str("bool"))); + py::list names, offsets, formats; + py::dict dict; + names.append(py::str("a")); + names.append(py::str("b")); + dict["names"] = names; + offsets.append(py::int_(1)); + offsets.append(py::int_(10)); + dict["offsets"] = offsets; + formats.append(py::dtype("int32")); + formats.append(py::dtype("float64")); + dict["formats"] = formats; + dict["itemsize"] = py::int_(20); + list.append(py::dtype::from_args(dict)); + list.append(py::dtype(names, formats, offsets, 20)); + list.append(py::dtype(py::buffer_info((void *) nullptr, sizeof(unsigned int), "I", 1))); + list.append(py::dtype(py::buffer_info((void *) nullptr, 0, "T{i:a:f:b:}", 1))); + list.append(py::dtype(py::detail::npy_api::NPY_DOUBLE_)); + return list; +} + +template +py::array_t dispatch_array_increment(const py::array_t &arr) { + py::array_t res(arr.shape(0)); + for (py::ssize_t i = 0; i < arr.shape(0); ++i) { + res.mutable_at(i) = T(arr.at(i) + 1); + } + return res; +} + +struct A {}; +struct B {}; + +TEST_SUBMODULE(numpy_dtypes, m) { + try { + py::module_::import("numpy"); + } catch (const py::error_already_set &) { + return; + } + + // typeinfo may be registered before the dtype descriptor for scalar casts to work... + py::class_(m, "SimpleStruct") + // Explicit construct to ensure zero-valued initialization. + .def(py::init([]() { return SimpleStruct(); })) + .def_readwrite("bool_", &SimpleStruct::bool_) + .def_readwrite("uint_", &SimpleStruct::uint_) + .def_readwrite("float_", &SimpleStruct::float_) + .def_readwrite("ldbl_", &SimpleStruct::ldbl_) + .def("astuple", + [](const SimpleStruct &self) { + return py::make_tuple(self.bool_, self.uint_, self.float_, self.ldbl_); + }) + .def_static("fromtuple", [](const py::tuple &tup) { + if (py::len(tup) != 4) { + throw py::cast_error("Invalid size"); + } + return SimpleStruct{tup[0].cast(), + tup[1].cast(), + tup[2].cast(), + tup[3].cast()}; + }); + + PYBIND11_NUMPY_DTYPE(SimpleStruct, bool_, uint_, float_, ldbl_); + PYBIND11_NUMPY_DTYPE(SimpleStructReordered, bool_, uint_, float_, ldbl_); + PYBIND11_NUMPY_DTYPE(PackedStruct, bool_, uint_, float_, ldbl_); + PYBIND11_NUMPY_DTYPE(NestedStruct, a, b); + PYBIND11_NUMPY_DTYPE(PartialStruct, bool_, uint_, float_, ldbl_); + PYBIND11_NUMPY_DTYPE(PartialNestedStruct, a); + PYBIND11_NUMPY_DTYPE(StringStruct, a, b); + PYBIND11_NUMPY_DTYPE(ArrayStruct, a, b, c, d); + PYBIND11_NUMPY_DTYPE(EnumStruct, e1, e2); + PYBIND11_NUMPY_DTYPE(ComplexStruct, cflt, cdbl); + + // ... or after + py::class_(m, "PackedStruct"); + + PYBIND11_NUMPY_DTYPE_EX(StructWithUglyNames, __x__, "x", __y__, "y"); + +#ifdef PYBIND11_NEVER_DEFINED_EVER + // If enabled, this should produce a static_assert failure telling the user that the struct + // is not a POD type + struct NotPOD { + std::string v; + NotPOD() : v("hi") {}; + }; + PYBIND11_NUMPY_DTYPE(NotPOD, v); +#endif + + // Check that dtypes can be registered programmatically, both from + // initializer lists of field descriptors and from other containers. + py::detail::npy_format_descriptor::register_dtype({}); + py::detail::npy_format_descriptor::register_dtype( + std::vector{}); + + // test_recarray, test_scalar_conversion + m.def("create_rec_simple", &create_recarray); + m.def("create_rec_packed", &create_recarray); + m.def("create_rec_nested", [](size_t n) { // test_signature + py::array_t arr = mkarray_via_buffer(n); + auto req = arr.request(); + auto *ptr = static_cast(req.ptr); + for (size_t i = 0; i < n; i++) { + SET_TEST_VALS(ptr[i].a, i); + SET_TEST_VALS(ptr[i].b, i + 1); + } + return arr; + }); + m.def("create_rec_partial", &create_recarray); + m.def("create_rec_partial_nested", [](size_t n) { + py::array_t arr = mkarray_via_buffer(n); + auto req = arr.request(); + auto *ptr = static_cast(req.ptr); + for (size_t i = 0; i < n; i++) { + SET_TEST_VALS(ptr[i].a, i); + } + return arr; + }); + m.def("print_rec_simple", &print_recarray); + m.def("print_rec_packed", &print_recarray); + m.def("print_rec_nested", &print_recarray); + + // test_format_descriptors + m.def("get_format_unbound", []() { return py::format_descriptor::format(); }); + m.def("print_format_descriptors", []() { + py::list l; + for (const auto &fmt : {py::format_descriptor::format(), + py::format_descriptor::format(), + py::format_descriptor::format(), + py::format_descriptor::format(), + py::format_descriptor::format(), + py::format_descriptor::format(), + py::format_descriptor::format(), + py::format_descriptor::format(), + py::format_descriptor::format()}) { + l.append(py::cast(fmt)); + } + return l; + }); + + // test_dtype + // Below we use `L` for unsigned long as unfortunately the only name that + // works reliably on Both NumPy 2.x and old NumPy 1.x. + std::vector dtype_names{ + "byte", + "short", + "intc", + "long", + "longlong", + "ubyte", + "ushort", + "uintc", + "L", + "ulonglong", + "half", + "single", + "double", + "longdouble", + "csingle", + "cdouble", + "clongdouble", + "bool_", + "datetime64", + "timedelta64", + "object_", + // platform dependent aliases (int_ and uint are also NumPy version dependent on windows) + "int_", + "uint", + "intp", + "uintp"}; + + m.def("print_dtypes", []() { + py::list l; + for (const py::handle &d : {py::dtype::of(), + py::dtype::of(), + py::dtype::of(), + py::dtype::of(), + py::dtype::of(), + py::dtype::of(), + py::dtype::of(), + py::dtype::of(), + py::dtype::of(), + py::dtype::of()}) { + l.append(py::str(d)); + } + return l; + }); + m.def("test_dtype_ctors", &test_dtype_ctors); + m.def("test_dtype_kind", [dtype_names]() { + py::list list; + for (const auto &dt_name : dtype_names) { + list.append(py::dtype(dt_name).kind()); + } + return list; + }); + m.def("test_dtype_char_", [dtype_names]() { + py::list list; + for (const auto &dt_name : dtype_names) { + list.append(py::dtype(dt_name).char_()); + } + return list; + }); + m.def("test_dtype_num", [dtype_names]() { + py::list list; + for (const auto &dt_name : dtype_names) { + list.append(py::dtype(dt_name).num()); + } + return list; + }); + m.def("test_dtype_byteorder", [dtype_names]() { + py::list list; + for (const auto &dt_name : dtype_names) { + list.append(py::dtype(dt_name).byteorder()); + } + return list; + }); + m.def("test_dtype_alignment", [dtype_names]() { + py::list list; + for (const auto &dt_name : dtype_names) { + list.append(py::dtype(dt_name).alignment()); + } + return list; + }); + m.def("test_dtype_flags", [dtype_names]() { + py::list list; + for (const auto &dt_name : dtype_names) { + list.append(py::dtype(dt_name).flags()); + } + return list; + }); + m.def("test_dtype_num_of", []() -> py::list { + py::list res; +#define TEST_DTYPE(T) res.append(py::make_tuple(py::dtype::of().num(), py::dtype::num_of())); + TEST_DTYPE(bool) + TEST_DTYPE(signed char) + TEST_DTYPE(unsigned char) + TEST_DTYPE(short) + TEST_DTYPE(unsigned short) + TEST_DTYPE(int) + TEST_DTYPE(unsigned int) + TEST_DTYPE(long) + TEST_DTYPE(unsigned long) + TEST_DTYPE(long long) + TEST_DTYPE(unsigned long long) + TEST_DTYPE(float) + TEST_DTYPE(double) + TEST_DTYPE(long double) + TEST_DTYPE(std::complex) + TEST_DTYPE(std::complex) + TEST_DTYPE(std::complex) + TEST_DTYPE(int8_t) + TEST_DTYPE(uint8_t) + TEST_DTYPE(int16_t) + TEST_DTYPE(uint16_t) + TEST_DTYPE(int32_t) + TEST_DTYPE(uint32_t) + TEST_DTYPE(int64_t) + TEST_DTYPE(uint64_t) +#undef TEST_DTYPE + return res; + }); + m.def("test_dtype_normalized_num", []() -> py::list { + py::list res; +#define TEST_DTYPE(NT, T) \ + res.append(py::make_tuple(py::dtype(py::detail::npy_api::NT).normalized_num(), \ + py::dtype::num_of())); + TEST_DTYPE(NPY_BOOL_, bool) + TEST_DTYPE(NPY_BYTE_, signed char); + TEST_DTYPE(NPY_UBYTE_, unsigned char); + TEST_DTYPE(NPY_SHORT_, short); + TEST_DTYPE(NPY_USHORT_, unsigned short); + TEST_DTYPE(NPY_INT_, int); + TEST_DTYPE(NPY_UINT_, unsigned int); + TEST_DTYPE(NPY_LONG_, long); + TEST_DTYPE(NPY_ULONG_, unsigned long); + TEST_DTYPE(NPY_LONGLONG_, long long); + TEST_DTYPE(NPY_ULONGLONG_, unsigned long long); + TEST_DTYPE(NPY_FLOAT_, float); + TEST_DTYPE(NPY_DOUBLE_, double); + TEST_DTYPE(NPY_LONGDOUBLE_, long double); + TEST_DTYPE(NPY_CFLOAT_, std::complex); + TEST_DTYPE(NPY_CDOUBLE_, std::complex); + TEST_DTYPE(NPY_CLONGDOUBLE_, std::complex); + TEST_DTYPE(NPY_INT8_, int8_t); + TEST_DTYPE(NPY_UINT8_, uint8_t); + TEST_DTYPE(NPY_INT16_, int16_t); + TEST_DTYPE(NPY_UINT16_, uint16_t); + TEST_DTYPE(NPY_INT32_, int32_t); + TEST_DTYPE(NPY_UINT32_, uint32_t); + TEST_DTYPE(NPY_INT64_, int64_t); + TEST_DTYPE(NPY_UINT64_, uint64_t); +#undef TEST_DTYPE + return res; + }); + m.def("test_dtype_switch", [](const py::array &arr) -> py::array { + switch (arr.dtype().normalized_num()) { + case py::dtype::num_of(): + return dispatch_array_increment(arr); + case py::dtype::num_of(): + return dispatch_array_increment(arr); + case py::dtype::num_of(): + return dispatch_array_increment(arr); + case py::dtype::num_of(): + return dispatch_array_increment(arr); + case py::dtype::num_of(): + return dispatch_array_increment(arr); + case py::dtype::num_of(): + return dispatch_array_increment(arr); + case py::dtype::num_of(): + return dispatch_array_increment(arr); + case py::dtype::num_of(): + return dispatch_array_increment(arr); + case py::dtype::num_of(): + return dispatch_array_increment(arr); + case py::dtype::num_of(): + return dispatch_array_increment(arr); + case py::dtype::num_of(): + return dispatch_array_increment(arr); + default: + throw std::runtime_error("Unsupported dtype"); + } + }); + m.def("test_dtype_methods", []() { + py::list list; + auto dt1 = py::dtype::of(); + auto dt2 = py::dtype::of(); + list.append(dt1); + list.append(dt2); + list.append(py::bool_(dt1.has_fields())); + list.append(py::bool_(dt2.has_fields())); + list.append(py::int_(dt1.itemsize())); + list.append(py::int_(dt2.itemsize())); + return list; + }); + struct TrailingPaddingStruct { + int32_t a; + char b; + }; + PYBIND11_NUMPY_DTYPE(TrailingPaddingStruct, a, b); + m.def("trailing_padding_dtype", []() { return py::dtype::of(); }); + + // test_string_array + m.def("create_string_array", [](bool non_empty) { + py::array_t arr = mkarray_via_buffer(non_empty ? 4 : 0); + if (non_empty) { + auto req = arr.request(); + auto *ptr = static_cast(req.ptr); + for (py::ssize_t i = 0; i < req.size * req.itemsize; i++) { + static_cast(req.ptr)[i] = 0; + } + ptr[1].a[0] = 'a'; + ptr[1].b[0] = 'a'; + ptr[2].a[0] = 'a'; + ptr[2].b[0] = 'a'; + ptr[3].a[0] = 'a'; + ptr[3].b[0] = 'a'; + + ptr[2].a[1] = 'b'; + ptr[2].b[1] = 'b'; + ptr[3].a[1] = 'b'; + ptr[3].b[1] = 'b'; + + ptr[3].a[2] = 'c'; + ptr[3].b[2] = 'c'; + } + return arr; + }); + m.def("print_string_array", &print_recarray); + + // test_array_array + m.def("create_array_array", [](size_t n) { + py::array_t arr = mkarray_via_buffer(n); + auto *ptr = (ArrayStruct *) arr.mutable_data(); + for (size_t i = 0; i < n; i++) { + for (size_t j = 0; j < 3; j++) { + for (size_t k = 0; k < 4; k++) { + ptr[i].a[j][k] = char('A' + (i * 100 + j * 10 + k) % 26); + } + } + for (size_t j = 0; j < 2; j++) { + ptr[i].b[j] = int32_t(i * 1000 + j); + } + for (size_t j = 0; j < 3; j++) { + ptr[i].c[j] = uint8_t(i * 10 + j); + } + for (size_t j = 0; j < 4; j++) { + for (size_t k = 0; k < 2; k++) { + ptr[i].d[j][k] = float(i) * 100.0f + float(j) * 10.0f + float(k); + } + } + } + return arr; + }); + m.def("print_array_array", &print_recarray); + + // test_enum_array + m.def("create_enum_array", [](size_t n) { + py::array_t arr = mkarray_via_buffer(n); + auto *ptr = (EnumStruct *) arr.mutable_data(); + for (size_t i = 0; i < n; i++) { + ptr[i].e1 = static_cast(-1 + ((int) i % 2) * 2); + ptr[i].e2 = static_cast(1 + (i % 2)); + } + return arr; + }); + m.def("print_enum_array", &print_recarray); + + // test_complex_array + m.def("create_complex_array", [](size_t n) { + py::array_t arr = mkarray_via_buffer(n); + auto *ptr = (ComplexStruct *) arr.mutable_data(); + for (size_t i = 0; i < n; i++) { + ptr[i].cflt.real(float(i)); + ptr[i].cflt.imag(float(i) + 0.25f); + ptr[i].cdbl.real(double(i) + 0.5); + ptr[i].cdbl.imag(double(i) + 0.75); + } + return arr; + }); + m.def("print_complex_array", &print_recarray); + + // test_array_constructors + m.def("test_array_ctors", &test_array_ctors); + + // test_compare_buffer_info + struct CompareStruct { + bool x; + uint32_t y; + float z; + }; + PYBIND11_NUMPY_DTYPE(CompareStruct, x, y, z); + m.def("compare_buffer_info", []() { + py::list list; + list.append(py::bool_(py::detail::compare_buffer_info::compare( + py::buffer_info(nullptr, sizeof(float), "f", 1)))); + list.append(py::bool_(py::detail::compare_buffer_info::compare( + py::buffer_info(nullptr, sizeof(int), "I", 1)))); + list.append(py::bool_(py::detail::compare_buffer_info::compare( + py::buffer_info(nullptr, sizeof(long), "l", 1)))); + list.append(py::bool_(py::detail::compare_buffer_info::compare( + py::buffer_info(nullptr, sizeof(long), sizeof(long) == sizeof(int) ? "i" : "q", 1)))); + list.append(py::bool_(py::detail::compare_buffer_info::compare( + py::buffer_info(nullptr, sizeof(CompareStruct), "T{?:x:3xI:y:f:z:}", 1)))); + return list; + }); + m.def("buffer_to_dtype", [](py::buffer &buf) { return py::dtype(buf.request()); }); + + // test_scalar_conversion + auto f_simple = [](SimpleStruct s) { return s.uint_ * 10; }; + m.def("f_simple", f_simple); + m.def("f_packed", [](PackedStruct s) { return s.uint_ * 10; }); + m.def("f_nested", [](NestedStruct s) { return s.a.uint_ * 10; }); + + // test_vectorize + m.def("f_simple_vectorized", py::vectorize(f_simple)); + auto f_simple_pass_thru = [](SimpleStruct s) { return s; }; + m.def("f_simple_pass_thru_vectorized", py::vectorize(f_simple_pass_thru)); + + // test_register_dtype + m.def("register_dtype", + []() { PYBIND11_NUMPY_DTYPE(SimpleStruct, bool_, uint_, float_, ldbl_); }); + + // test_str_leak + m.def("dtype_wrapper", [](const py::object &d) { return py::dtype::from_args(d); }); +} diff --git a/external_libraries/pybind11/tests/test_numpy_dtypes.py b/external_libraries/pybind11/tests/test_numpy_dtypes.py new file mode 100644 index 00000000..22814aba --- /dev/null +++ b/external_libraries/pybind11/tests/test_numpy_dtypes.py @@ -0,0 +1,464 @@ +from __future__ import annotations + +import re + +import pytest + +import env # noqa: F401 +from pybind11_tests import numpy_dtypes as m + +np = pytest.importorskip("numpy") + + +@pytest.fixture(scope="module") +def simple_dtype(): + ld = np.dtype("longdouble") + return np.dtype( + { + "names": ["bool_", "uint_", "float_", "ldbl_"], + "formats": ["?", "u4", "f4", f"f{ld.itemsize}"], + "offsets": [0, 4, 8, (16 if ld.alignment > 4 else 12)], + } + ) + + +@pytest.fixture(scope="module") +def packed_dtype(): + return np.dtype([("bool_", "?"), ("uint_", "u4"), ("float_", "f4"), ("ldbl_", "g")]) + + +def dt_fmt(): + from sys import byteorder + + e = "<" if byteorder == "little" else ">" + return ( + "{{'names':['bool_','uint_','float_','ldbl_']," + "'formats':['?','" + e + "u4','" + e + "f4','" + e + "f{}']," + "'offsets':[0,4,8,{}],'itemsize':{}}}" + ) + + +def simple_dtype_fmt(): + ld = np.dtype("longdouble") + simple_ld_off = 12 + 4 * (ld.alignment > 4) + return dt_fmt().format(ld.itemsize, simple_ld_off, simple_ld_off + ld.itemsize) + + +def packed_dtype_fmt(): + from sys import byteorder + + return "[('bool_','?'),('uint_','{e}u4'),('float_','{e}f4'),('ldbl_','{e}f{}')]".format( + np.dtype("longdouble").itemsize, e="<" if byteorder == "little" else ">" + ) + + +def partial_ld_offset(): + return ( + 12 + + 4 * (np.dtype("uint64").alignment > 4) + + 8 + + 8 * (np.dtype("longdouble").alignment > 8) + ) + + +def partial_dtype_fmt(): + ld = np.dtype("longdouble") + partial_ld_off = partial_ld_offset() + partial_size = partial_ld_off + ld.itemsize + partial_end_padding = partial_size % np.dtype("uint64").alignment + return dt_fmt().format( + ld.itemsize, partial_ld_off, partial_size + partial_end_padding + ) + + +def partial_nested_fmt(): + ld = np.dtype("longdouble") + partial_nested_off = 8 + 8 * (ld.alignment > 8) + partial_ld_off = partial_ld_offset() + partial_size = partial_ld_off + ld.itemsize + partial_end_padding = partial_size % np.dtype("uint64").alignment + partial_nested_size = partial_nested_off * 2 + partial_size + partial_end_padding + return f"{{'names':['a'],'formats':[{partial_dtype_fmt()}],'offsets':[{partial_nested_off}],'itemsize':{partial_nested_size}}}" + + +def assert_equal(actual, expected_data, expected_dtype): + np.testing.assert_equal(actual, np.array(expected_data, dtype=expected_dtype)) + + +def test_format_descriptors(): + with pytest.raises(RuntimeError) as excinfo: + m.get_format_unbound() + assert re.match( + "^NumPy type info missing for .*UnboundStruct.*$", str(excinfo.value) + ) + + ld = np.dtype("longdouble") + ldbl_fmt = ("4x" if ld.alignment > 4 else "") + ld.char + ss_fmt = "^T{?:bool_:3xI:uint_:f:float_:" + ldbl_fmt + ":ldbl_:}" + dbl = np.dtype("double") + end_padding = ld.itemsize % np.dtype("uint64").alignment + partial_fmt = ( + "^T{?:bool_:3xI:uint_:f:float_:" + + str(4 * (dbl.alignment > 4) + dbl.itemsize + 8 * (ld.alignment > 8)) + + "xg:ldbl_:" + + (str(end_padding) + "x}" if end_padding > 0 else "}") + ) + nested_extra = str(max(8, ld.alignment)) + assert m.print_format_descriptors() == [ + ss_fmt, + "^T{?:bool_:I:uint_:f:float_:g:ldbl_:}", + "^T{" + ss_fmt + ":a:^T{?:bool_:I:uint_:f:float_:g:ldbl_:}:b:}", + partial_fmt, + "^T{" + nested_extra + "x" + partial_fmt + ":a:" + nested_extra + "x}", + "^T{3s:a:3s:b:}", + "^T{(3)4s:a:(2)i:b:(3)B:c:1x(4, 2)f:d:}", + "^T{q:e1:B:e2:}", + "^T{Zf:cflt:Zd:cdbl:}", + ] + + +def test_dtype(simple_dtype): + from sys import byteorder + + e = "<" if byteorder == "little" else ">" + + assert [x.replace(" ", "") for x in m.print_dtypes()] == [ + simple_dtype_fmt(), + packed_dtype_fmt(), + f"[('a',{simple_dtype_fmt()}),('b',{packed_dtype_fmt()})]", + partial_dtype_fmt(), + partial_nested_fmt(), + "[('a','S3'),('b','S3')]", + ( + "{'names':['a','b','c','d']," + f"'formats':[('S4',(3,)),('{e}i4',(2,)),('u1',(3,)),('{e}f4',(4,2))]," + "'offsets':[0,12,20,24],'itemsize':56}" + ), + "[('e1','" + e + "i8'),('e2','u1')]", + "[('x','i1'),('y','" + e + "u8')]", + "[('cflt','" + e + "c8'),('cdbl','" + e + "c16')]", + ] + + d1 = np.dtype( + { + "names": ["a", "b"], + "formats": ["int32", "float64"], + "offsets": [1, 10], + "itemsize": 20, + } + ) + d2 = np.dtype([("a", "i4"), ("b", "f4")]) + assert m.test_dtype_ctors() == [ + np.dtype("int32"), + np.dtype("float64"), + np.dtype("bool"), + d1, + d1, + np.dtype("uint32"), + d2, + np.dtype("d"), + ] + + assert m.test_dtype_methods() == [ + np.dtype("int32"), + simple_dtype, + False, + True, + np.dtype("int32").itemsize, + simple_dtype.itemsize, + ] + + assert m.trailing_padding_dtype() == m.buffer_to_dtype( + np.zeros(1, m.trailing_padding_dtype()) + ) + + expected_chars = list("bhilqBHILQefdgFDG?MmO") + # Note that int_ and uint size and mapping is NumPy version dependent: + expected_chars += [np.dtype(_).char for _ in ("int_", "uint", "intp", "uintp")] + assert m.test_dtype_kind() == list("iiiiiuuuuuffffcccbMmOiuiu") + assert m.test_dtype_char_() == list(expected_chars) + assert m.test_dtype_num() == [np.dtype(ch).num for ch in expected_chars] + assert m.test_dtype_byteorder() == [np.dtype(ch).byteorder for ch in expected_chars] + assert m.test_dtype_alignment() == [np.dtype(ch).alignment for ch in expected_chars] + assert m.test_dtype_flags() == [np.dtype(ch).flags for ch in expected_chars] + + for a, b in m.test_dtype_num_of(): + assert a == b + + for a, b in m.test_dtype_normalized_num(): + assert a == b + + arr = np.array([4, 84, 21, 36]) + # Note: "ulong" does not work in NumPy 1.x, so we use "L" + assert (m.test_dtype_switch(arr.astype("byte")) == arr + 1).all() + assert (m.test_dtype_switch(arr.astype("ubyte")) == arr + 1).all() + assert (m.test_dtype_switch(arr.astype("short")) == arr + 1).all() + assert (m.test_dtype_switch(arr.astype("ushort")) == arr + 1).all() + assert (m.test_dtype_switch(arr.astype("intc")) == arr + 1).all() + assert (m.test_dtype_switch(arr.astype("uintc")) == arr + 1).all() + assert (m.test_dtype_switch(arr.astype("long")) == arr + 1).all() + assert (m.test_dtype_switch(arr.astype("L")) == arr + 1).all() + assert (m.test_dtype_switch(arr.astype("longlong")) == arr + 1).all() + assert (m.test_dtype_switch(arr.astype("ulonglong")) == arr + 1).all() + assert (m.test_dtype_switch(arr.astype("single")) == arr + 1).all() + assert (m.test_dtype_switch(arr.astype("double")) == arr + 1).all() + assert (m.test_dtype_switch(arr.astype("longdouble")) == arr + 1).all() + + +def test_recarray(simple_dtype, packed_dtype): + elements = [(False, 0, 0.0, -0.0), (True, 1, 1.5, -2.5), (False, 2, 3.0, -5.0)] + + for func, dtype in [ + (m.create_rec_simple, simple_dtype), + (m.create_rec_packed, packed_dtype), + ]: + arr = func(0) + assert arr.dtype == dtype + assert_equal(arr, [], simple_dtype) + assert_equal(arr, [], packed_dtype) + + arr = func(3) + assert arr.dtype == dtype + assert_equal(arr, elements, simple_dtype) + assert_equal(arr, elements, packed_dtype) + + # Show what recarray's look like in NumPy. + assert type(arr[0]) == np.void + assert type(arr[0].item()) == tuple + + if dtype == simple_dtype: + assert m.print_rec_simple(arr) == [ + "s:0,0,0,-0", + "s:1,1,1.5,-2.5", + "s:0,2,3,-5", + ] + else: + assert m.print_rec_packed(arr) == [ + "p:0,0,0,-0", + "p:1,1,1.5,-2.5", + "p:0,2,3,-5", + ] + + nested_dtype = np.dtype([("a", simple_dtype), ("b", packed_dtype)]) + + arr = m.create_rec_nested(0) + assert arr.dtype == nested_dtype + assert_equal(arr, [], nested_dtype) + + arr = m.create_rec_nested(3) + assert arr.dtype == nested_dtype + assert_equal( + arr, + [ + ((False, 0, 0.0, -0.0), (True, 1, 1.5, -2.5)), + ((True, 1, 1.5, -2.5), (False, 2, 3.0, -5.0)), + ((False, 2, 3.0, -5.0), (True, 3, 4.5, -7.5)), + ], + nested_dtype, + ) + assert m.print_rec_nested(arr) == [ + "n:a=s:0,0,0,-0;b=p:1,1,1.5,-2.5", + "n:a=s:1,1,1.5,-2.5;b=p:0,2,3,-5", + "n:a=s:0,2,3,-5;b=p:1,3,4.5,-7.5", + ] + + arr = m.create_rec_partial(3) + assert str(arr.dtype).replace(" ", "") == partial_dtype_fmt() + partial_dtype = arr.dtype + assert "" not in arr.dtype.fields + assert partial_dtype.itemsize > simple_dtype.itemsize + assert_equal(arr, elements, simple_dtype) + assert_equal(arr, elements, packed_dtype) + + arr = m.create_rec_partial_nested(3) + assert str(arr.dtype).replace(" ", "") == partial_nested_fmt() + assert "" not in arr.dtype.fields + assert "" not in arr.dtype.fields["a"][0].fields + assert arr.dtype.itemsize > partial_dtype.itemsize + np.testing.assert_equal(arr["a"], m.create_rec_partial(3)) + + +def test_array_constructors(): + data = np.arange(1, 7, dtype="int32") + for i in range(8): + np.testing.assert_array_equal(m.test_array_ctors(10 + i), data.reshape((3, 2))) + np.testing.assert_array_equal(m.test_array_ctors(20 + i), data.reshape((3, 2))) + for i in range(5): + np.testing.assert_array_equal(m.test_array_ctors(30 + i), data) + np.testing.assert_array_equal(m.test_array_ctors(40 + i), data) + + +def test_string_array(): + arr = m.create_string_array(True) + assert str(arr.dtype) == "[('a', 'S3'), ('b', 'S3')]" + assert m.print_string_array(arr) == [ + "a='',b=''", + "a='a',b='a'", + "a='ab',b='ab'", + "a='abc',b='abc'", + ] + dtype = arr.dtype + assert arr["a"].tolist() == [b"", b"a", b"ab", b"abc"] + assert arr["b"].tolist() == [b"", b"a", b"ab", b"abc"] + arr = m.create_string_array(False) + assert dtype == arr.dtype + + +def test_array_array(): + from sys import byteorder + + e = "<" if byteorder == "little" else ">" + + arr = m.create_array_array(3) + assert str(arr.dtype).replace(" ", "") == ( + "{'names':['a','b','c','d']," + f"'formats':[('S4',(3,)),('{e}i4',(2,)),('u1',(3,)),('{e}f4',(4,2))]," + "'offsets':[0,12,20,24],'itemsize':56}" + ) + assert m.print_array_array(arr) == [ + "a={{A,B,C,D},{K,L,M,N},{U,V,W,X}},b={0,1}," + "c={0,1,2},d={{0,1},{10,11},{20,21},{30,31}}", + "a={{W,X,Y,Z},{G,H,I,J},{Q,R,S,T}},b={1000,1001}," + "c={10,11,12},d={{100,101},{110,111},{120,121},{130,131}}", + "a={{S,T,U,V},{C,D,E,F},{M,N,O,P}},b={2000,2001}," + "c={20,21,22},d={{200,201},{210,211},{220,221},{230,231}}", + ] + assert arr["a"].tolist() == [ + [b"ABCD", b"KLMN", b"UVWX"], + [b"WXYZ", b"GHIJ", b"QRST"], + [b"STUV", b"CDEF", b"MNOP"], + ] + assert arr["b"].tolist() == [[0, 1], [1000, 1001], [2000, 2001]] + assert m.create_array_array(0).dtype == arr.dtype + + +def test_enum_array(): + from sys import byteorder + + e = "<" if byteorder == "little" else ">" + + arr = m.create_enum_array(3) + dtype = arr.dtype + assert dtype == np.dtype([("e1", e + "i8"), ("e2", "u1")]) + assert m.print_enum_array(arr) == ["e1=A,e2=X", "e1=B,e2=Y", "e1=A,e2=X"] + assert arr["e1"].tolist() == [-1, 1, -1] + assert arr["e2"].tolist() == [1, 2, 1] + assert m.create_enum_array(0).dtype == dtype + + +def test_complex_array(): + from sys import byteorder + + e = "<" if byteorder == "little" else ">" + + arr = m.create_complex_array(3) + dtype = arr.dtype + assert dtype == np.dtype([("cflt", e + "c8"), ("cdbl", e + "c16")]) + assert m.print_complex_array(arr) == [ + "c:(0,0.25),(0.5,0.75)", + "c:(1,1.25),(1.5,1.75)", + "c:(2,2.25),(2.5,2.75)", + ] + assert arr["cflt"].tolist() == [0.0 + 0.25j, 1.0 + 1.25j, 2.0 + 2.25j] + assert arr["cdbl"].tolist() == [0.5 + 0.75j, 1.5 + 1.75j, 2.5 + 2.75j] + assert m.create_complex_array(0).dtype == dtype + + +def test_signature(doc): + assert ( + doc(m.create_rec_nested) + == "create_rec_nested(arg0: typing.SupportsInt | typing.SupportsIndex) -> numpy.typing.NDArray[NestedStruct]" + ) + + +def test_scalar_conversion(): + n = 3 + arrays = [ + m.create_rec_simple(n), + m.create_rec_packed(n), + m.create_rec_nested(n), + m.create_enum_array(n), + ] + funcs = [m.f_simple, m.f_packed, m.f_nested] + + for i, func in enumerate(funcs): + for j, arr in enumerate(arrays): + if i == j and i < 2: + assert [func(arr[k]) for k in range(n)] == [k * 10 for k in range(n)] + else: + with pytest.raises(TypeError) as excinfo: + func(arr[0]) + assert "incompatible function arguments" in str(excinfo.value) + + +def test_vectorize(): + n = 3 + array = m.create_rec_simple(n) + values = m.f_simple_vectorized(array) + np.testing.assert_array_equal(values, [0, 10, 20]) + array_2 = m.f_simple_pass_thru_vectorized(array) + np.testing.assert_array_equal(array, array_2) + + +def test_cls_and_dtype_conversion(simple_dtype): + s = m.SimpleStruct() + assert s.astuple() == (False, 0, 0.0, 0.0) + assert m.SimpleStruct.fromtuple(s.astuple()).astuple() == s.astuple() + + s.uint_ = 2 + assert m.f_simple(s) == 20 + + # Try as recarray of shape==(1,). + s_recarray = np.array([(False, 2, 0.0, 0.0)], dtype=simple_dtype) + # Show that this will work for vectorized case. + np.testing.assert_array_equal(m.f_simple_vectorized(s_recarray), [20]) + + # Show as a scalar that inherits from np.generic. + s_scalar = s_recarray[0] + assert isinstance(s_scalar, np.void) + assert m.f_simple(s_scalar) == 20 + + # Show that an *array* scalar (np.ndarray.shape == ()) does not convert. + # More specifically, conversion to SimpleStruct is not implicit. + s_recarray_scalar = s_recarray.reshape(()) + assert isinstance(s_recarray_scalar, np.ndarray) + assert s_recarray_scalar.dtype == simple_dtype + with pytest.raises(TypeError) as excinfo: + m.f_simple(s_recarray_scalar) + assert "incompatible function arguments" in str(excinfo.value) + # Explicitly convert to m.SimpleStruct. + assert m.f_simple(m.SimpleStruct.fromtuple(s_recarray_scalar.item())) == 20 + + # Show that an array of dtype=object does *not* convert. + s_array_object = np.array([s]) + assert s_array_object.dtype == object + with pytest.raises(TypeError) as excinfo: + m.f_simple_vectorized(s_array_object) + assert "incompatible function arguments" in str(excinfo.value) + # Explicitly convert to `np.array(..., dtype=simple_dtype)` + s_array = np.array([s.astuple()], dtype=simple_dtype) + np.testing.assert_array_equal(m.f_simple_vectorized(s_array), [20]) + + +def test_register_dtype(): + with pytest.raises(RuntimeError) as excinfo: + m.register_dtype() + assert "dtype is already registered" in str(excinfo.value) + + +@pytest.mark.xfail("env.PYPY") +def test_str_leak(): + from sys import getrefcount + + fmt = "f4" + pytest.gc_collect() + start = getrefcount(fmt) + d = m.dtype_wrapper(fmt) + assert d is np.dtype("f4") + del d + pytest.gc_collect() + assert getrefcount(fmt) == start + + +def test_compare_buffer_info(): + assert all(m.compare_buffer_info()) diff --git a/external_libraries/pybind11/tests/test_numpy_scalars.cpp b/external_libraries/pybind11/tests/test_numpy_scalars.cpp new file mode 100644 index 00000000..79393ebd --- /dev/null +++ b/external_libraries/pybind11/tests/test_numpy_scalars.cpp @@ -0,0 +1,63 @@ +/* + tests/test_numpy_scalars.cpp -- strict NumPy scalars + + Copyright (c) 2021 Steve R. Sun + + All rights reserved. Use of this source code is governed by a + BSD-style license that can be found in the LICENSE file. +*/ + +#include + +#include "pybind11_tests.h" + +#include +#include + +namespace py = pybind11; + +namespace pybind11_test_numpy_scalars { + +template +struct add { + T x; + explicit add(T x) : x(x) {} + T operator()(T y) const { return static_cast(x + y); } +}; + +template +void register_test(py::module &m, const char *name, F &&func) { + m.def((std::string("test_") + name).c_str(), + [=](py::numpy_scalar v) { + return std::make_tuple(name, py::make_scalar(static_cast(func(v.value)))); + }, + py::arg("x")); +} + +} // namespace pybind11_test_numpy_scalars + +using namespace pybind11_test_numpy_scalars; + +TEST_SUBMODULE(numpy_scalars, m) { + using cfloat = std::complex; + using cdouble = std::complex; + + register_test(m, "bool", [](bool x) { return !x; }); + register_test(m, "int8", add(-8)); + register_test(m, "int16", add(-16)); + register_test(m, "int32", add(-32)); + register_test(m, "int64", add(-64)); + register_test(m, "uint8", add(8)); + register_test(m, "uint16", add(16)); + register_test(m, "uint32", add(32)); + register_test(m, "uint64", add(64)); + register_test(m, "float32", add(0.125f)); + register_test(m, "float64", add(0.25f)); + register_test(m, "complex64", add({0, -0.125f})); + register_test(m, "complex128", add({0, -0.25f})); + + m.def("test_eq", + [](py::numpy_scalar a, py::numpy_scalar b) { return a == b; }); + m.def("test_ne", + [](py::numpy_scalar a, py::numpy_scalar b) { return a != b; }); +} diff --git a/external_libraries/pybind11/tests/test_numpy_scalars.py b/external_libraries/pybind11/tests/test_numpy_scalars.py new file mode 100644 index 00000000..fe9b71f2 --- /dev/null +++ b/external_libraries/pybind11/tests/test_numpy_scalars.py @@ -0,0 +1,54 @@ +from __future__ import annotations + +import pytest + +from pybind11_tests import numpy_scalars as m + +np = pytest.importorskip("numpy") + +NPY_SCALAR_TYPES = { + np.bool_: False, + np.int8: -7, + np.int16: -15, + np.int32: -31, + np.int64: -63, + np.uint8: 9, + np.uint16: 17, + np.uint32: 33, + np.uint64: 65, + np.single: 1.125, + np.double: 1.25, + np.complex64: 1 - 0.125j, + np.complex128: 1 - 0.25j, +} + +ALL_SCALAR_TYPES = tuple(NPY_SCALAR_TYPES.keys()) + (int, bool, float, bytes, str) + + +@pytest.mark.parametrize( + ("npy_scalar_type", "expected_value"), NPY_SCALAR_TYPES.items() +) +def test_numpy_scalars(npy_scalar_type, expected_value): + tpnm = npy_scalar_type.__name__.rstrip("_") + test_tpnm = getattr(m, "test_" + tpnm) + assert ( + test_tpnm.__doc__ + == f"test_{tpnm}(x: numpy.{tpnm}) -> tuple[str, numpy.{tpnm}]\n" + ) + for tp in ALL_SCALAR_TYPES: + value = tp(1) + if tp is npy_scalar_type: + result_tpnm, result_value = test_tpnm(value) + assert result_tpnm == tpnm + assert isinstance(result_value, npy_scalar_type) + assert result_value == tp(expected_value) + else: + with pytest.raises(TypeError): + test_tpnm(value) + + +def test_eq_ne(): + assert m.test_eq(np.int32(3), np.int32(3)) + assert not m.test_eq(np.int32(3), np.int32(5)) + assert not m.test_ne(np.int32(3), np.int32(3)) + assert m.test_ne(np.int32(3), np.int32(5)) diff --git a/external_libraries/pybind11/tests/test_numpy_vectorize.cpp b/external_libraries/pybind11/tests/test_numpy_vectorize.cpp new file mode 100644 index 00000000..23345216 --- /dev/null +++ b/external_libraries/pybind11/tests/test_numpy_vectorize.cpp @@ -0,0 +1,137 @@ +/* + tests/test_numpy_vectorize.cpp -- auto-vectorize functions over NumPy array + arguments + + Copyright (c) 2016 Wenzel Jakob + + All rights reserved. Use of this source code is governed by a + BSD-style license that can be found in the LICENSE file. +*/ + +#include + +#include "pybind11_tests.h" + +#include + +double my_func(int x, float y, double z) { + py::print("my_func(x:int={}, y:float={:.0f}, z:float={:.0f})"_s.format(x, y, z)); + return (float) x * y * z; +} + +TEST_SUBMODULE(numpy_vectorize, m) { + try { + py::module_::import("numpy"); + } catch (const py::error_already_set &) { + return; + } + + // test_vectorize, test_docs, test_array_collapse + // Vectorize all arguments of a function (though non-vector arguments are also allowed) + m.def("vectorized_func", py::vectorize(my_func)); + + // Vectorize a lambda function with a capture object (e.g. to exclude some arguments from the + // vectorization) + m.def("vectorized_func2", [](py::array_t x, py::array_t y, float z) { + return py::vectorize([z](int x, float y) { return my_func(x, y, z); })(std::move(x), + std::move(y)); + }); + + // Vectorize a complex-valued function + m.def("vectorized_func3", + py::vectorize([](std::complex c) { return c * std::complex(2.f); })); + + // test_type_selection + // NumPy function which only accepts specific data types + // A lot of these no lints could be replaced with const refs, and probably should at some + // point. + m.def("selective_func", + [](const py::array_t &) { return "Int branch taken."; }); + m.def("selective_func", + [](const py::array_t &) { return "Float branch taken."; }); + m.def("selective_func", [](const py::array_t, py::array::c_style> &) { + return "Complex float branch taken."; + }); + + // test_passthrough_arguments + // Passthrough test: references and non-pod types should be automatically passed through (in + // the function definition below, only `b`, `d`, and `g` are vectorized): + struct NonPODClass { + explicit NonPODClass(int v) : value{v} {} + int value; + }; + py::class_(m, "NonPODClass") + .def(py::init()) + .def_readwrite("value", &NonPODClass::value); + m.def("vec_passthrough", + py::vectorize([](const double *a, + double b, + // Changing this broke things + // NOLINTNEXTLINE(performance-unnecessary-value-param) + py::array_t c, + const int &d, + int &e, + NonPODClass f, + const double g) { return *a + b + c.at(0) + d + e + f.value + g; })); + + // test_method_vectorization + struct VectorizeTestClass { + explicit VectorizeTestClass(int v) : value{v} {}; + float method(int x, float y) const { return y + (float) (x + value); } + // Exercises vectorize(Return (Class::*)(Args...) &) + // NOLINTNEXTLINE(readability-make-member-function-const) + float method_lref(int x, float y) & { return y + (float) (x + value); } + // Exercises vectorize(Return (Class::*)(Args...) const &) + float method_const_lref(int x, float y) const & { return y + (float) (x + value); } + // Exercises vectorize(Return (Class::*)(Args...) noexcept) + // NOLINTNEXTLINE(readability-make-member-function-const) + float method_noexcept(int x, float y) noexcept { return y + (float) (x + value); } + // Exercises vectorize(Return (Class::*)(Args...) const noexcept) + float method_const_noexcept(int x, float y) const noexcept { + return y + (float) (x + value); + } +#ifdef __cpp_noexcept_function_type + // Exercises vectorize(Return (Class::*)(Args...) & noexcept) + // NOLINTNEXTLINE(readability-make-member-function-const) + float method_lref_noexcept(int x, float y) & noexcept { return y + (float) (x + value); } + // Exercises vectorize(Return (Class::*)(Args...) const & noexcept) + float method_const_lref_noexcept(int x, float y) const & noexcept { + return y + (float) (x + value); + } +#endif + int value = 0; + }; + py::class_ vtc(m, "VectorizeTestClass"); + vtc.def(py::init()).def_readwrite("value", &VectorizeTestClass::value); + + // Automatic vectorizing of methods + vtc.def("method", py::vectorize(&VectorizeTestClass::method)); + vtc.def("method_lref", py::vectorize(&VectorizeTestClass::method_lref)); + vtc.def("method_const_lref", py::vectorize(&VectorizeTestClass::method_const_lref)); + vtc.def("method_noexcept", py::vectorize(&VectorizeTestClass::method_noexcept)); + vtc.def("method_const_noexcept", py::vectorize(&VectorizeTestClass::method_const_noexcept)); +#ifdef __cpp_noexcept_function_type + vtc.def("method_lref_noexcept", py::vectorize(&VectorizeTestClass::method_lref_noexcept)); + vtc.def("method_const_lref_noexcept", + py::vectorize(&VectorizeTestClass::method_const_lref_noexcept)); +#endif + + // test_trivial_broadcasting + // Internal optimization test for whether the input is trivially broadcastable: + py::enum_(m, "trivial") + .value("f_trivial", py::detail::broadcast_trivial::f_trivial) + .value("c_trivial", py::detail::broadcast_trivial::c_trivial) + .value("non_trivial", py::detail::broadcast_trivial::non_trivial); + m.def("vectorized_is_trivial", + [](const py::array_t &arg1, + const py::array_t &arg2, + const py::array_t &arg3) { + py::ssize_t ndim = 0; + std::vector shape; + std::array buffers{ + {arg1.request(), arg2.request(), arg3.request()}}; + return py::detail::broadcast(buffers, ndim, shape); + }); + + m.def("add_to", py::vectorize([](NonPODClass &x, int a) { x.value += a; })); +} diff --git a/external_libraries/pybind11/tests/test_numpy_vectorize.py b/external_libraries/pybind11/tests/test_numpy_vectorize.py new file mode 100644 index 00000000..030abece --- /dev/null +++ b/external_libraries/pybind11/tests/test_numpy_vectorize.py @@ -0,0 +1,319 @@ +from __future__ import annotations + +import pytest + +from pybind11_tests import defined___cpp_noexcept_function_type +from pybind11_tests import numpy_vectorize as m + +np = pytest.importorskip("numpy") + + +def test_vectorize(capture): + assert np.isclose(m.vectorized_func3(np.array(3 + 7j)), [6 + 14j]) + + for f in [m.vectorized_func, m.vectorized_func2]: + with capture: + assert np.isclose(f(1, 2, 3), 6) + assert capture == "my_func(x:int=1, y:float=2, z:float=3)" + with capture: + assert np.isclose(f(np.array(1), np.array(2), 3), 6) + assert capture == "my_func(x:int=1, y:float=2, z:float=3)" + with capture: + assert np.allclose(f(np.array([1, 3]), np.array([2, 4]), 3), [6, 36]) + assert ( + capture + == """ + my_func(x:int=1, y:float=2, z:float=3) + my_func(x:int=3, y:float=4, z:float=3) + """ + ) + with capture: + a = np.array([[1, 2], [3, 4]], order="F") + b = np.array([[10, 20], [30, 40]], order="F") + c = 3 + result = f(a, b, c) + assert np.allclose(result, a * b * c) + assert result.flags.f_contiguous + # All inputs are F order and full or singletons, so we the result is in col-major order: + assert ( + capture + == """ + my_func(x:int=1, y:float=10, z:float=3) + my_func(x:int=3, y:float=30, z:float=3) + my_func(x:int=2, y:float=20, z:float=3) + my_func(x:int=4, y:float=40, z:float=3) + """ + ) + with capture: + a, b, c = ( + np.array([[1, 3, 5], [7, 9, 11]]), + np.array([[2, 4, 6], [8, 10, 12]]), + 3, + ) + assert np.allclose(f(a, b, c), a * b * c) + assert ( + capture + == """ + my_func(x:int=1, y:float=2, z:float=3) + my_func(x:int=3, y:float=4, z:float=3) + my_func(x:int=5, y:float=6, z:float=3) + my_func(x:int=7, y:float=8, z:float=3) + my_func(x:int=9, y:float=10, z:float=3) + my_func(x:int=11, y:float=12, z:float=3) + """ + ) + with capture: + a, b, c = np.array([[1, 2, 3], [4, 5, 6]]), np.array([2, 3, 4]), 2 + assert np.allclose(f(a, b, c), a * b * c) + assert ( + capture + == """ + my_func(x:int=1, y:float=2, z:float=2) + my_func(x:int=2, y:float=3, z:float=2) + my_func(x:int=3, y:float=4, z:float=2) + my_func(x:int=4, y:float=2, z:float=2) + my_func(x:int=5, y:float=3, z:float=2) + my_func(x:int=6, y:float=4, z:float=2) + """ + ) + with capture: + a, b, c = np.array([[1, 2, 3], [4, 5, 6]]), np.array([[2], [3]]), 2 + assert np.allclose(f(a, b, c), a * b * c) + assert ( + capture + == """ + my_func(x:int=1, y:float=2, z:float=2) + my_func(x:int=2, y:float=2, z:float=2) + my_func(x:int=3, y:float=2, z:float=2) + my_func(x:int=4, y:float=3, z:float=2) + my_func(x:int=5, y:float=3, z:float=2) + my_func(x:int=6, y:float=3, z:float=2) + """ + ) + with capture: + a, b, c = ( + np.array([[1, 2, 3], [4, 5, 6]], order="F"), + np.array([[2], [3]]), + 2, + ) + assert np.allclose(f(a, b, c), a * b * c) + assert ( + capture + == """ + my_func(x:int=1, y:float=2, z:float=2) + my_func(x:int=2, y:float=2, z:float=2) + my_func(x:int=3, y:float=2, z:float=2) + my_func(x:int=4, y:float=3, z:float=2) + my_func(x:int=5, y:float=3, z:float=2) + my_func(x:int=6, y:float=3, z:float=2) + """ + ) + with capture: + a, b, c = np.array([[1, 2, 3], [4, 5, 6]])[::, ::2], np.array([[2], [3]]), 2 + assert np.allclose(f(a, b, c), a * b * c) + assert ( + capture + == """ + my_func(x:int=1, y:float=2, z:float=2) + my_func(x:int=3, y:float=2, z:float=2) + my_func(x:int=4, y:float=3, z:float=2) + my_func(x:int=6, y:float=3, z:float=2) + """ + ) + with capture: + a, b, c = ( + np.array([[1, 2, 3], [4, 5, 6]], order="F")[::, ::2], + np.array([[2], [3]]), + 2, + ) + assert np.allclose(f(a, b, c), a * b * c) + assert ( + capture + == """ + my_func(x:int=1, y:float=2, z:float=2) + my_func(x:int=3, y:float=2, z:float=2) + my_func(x:int=4, y:float=3, z:float=2) + my_func(x:int=6, y:float=3, z:float=2) + """ + ) + + +def test_type_selection(): + assert m.selective_func(np.array([1], dtype=np.int32)) == "Int branch taken." + assert m.selective_func(np.array([1.0], dtype=np.float32)) == "Float branch taken." + assert ( + m.selective_func(np.array([1.0j], dtype=np.complex64)) + == "Complex float branch taken." + ) + + +def test_docs(doc): + assert ( + doc(m.vectorized_func) + == """ + vectorized_func(arg0: typing.Annotated[numpy.typing.ArrayLike, numpy.int32], arg1: typing.Annotated[numpy.typing.ArrayLike, numpy.float32], arg2: typing.Annotated[numpy.typing.ArrayLike, numpy.float64]) -> object + """ + ) + + +def test_trivial_broadcasting(): + trivial, vectorized_is_trivial = m.trivial, m.vectorized_is_trivial + + assert vectorized_is_trivial(1, 2, 3) == trivial.c_trivial + assert vectorized_is_trivial(np.array(1), np.array(2), 3) == trivial.c_trivial + assert ( + vectorized_is_trivial(np.array([1, 3]), np.array([2, 4]), 3) + == trivial.c_trivial + ) + assert trivial.c_trivial == vectorized_is_trivial( + np.array([[1, 3, 5], [7, 9, 11]]), np.array([[2, 4, 6], [8, 10, 12]]), 3 + ) + assert ( + vectorized_is_trivial(np.array([[1, 2, 3], [4, 5, 6]]), np.array([2, 3, 4]), 2) + == trivial.non_trivial + ) + assert ( + vectorized_is_trivial(np.array([[1, 2, 3], [4, 5, 6]]), np.array([[2], [3]]), 2) + == trivial.non_trivial + ) + z1 = np.array([[1, 2, 3, 4], [5, 6, 7, 8]], dtype="int32") + z2 = np.array(z1, dtype="float32") + z3 = np.array(z1, dtype="float64") + assert vectorized_is_trivial(z1, z2, z3) == trivial.c_trivial + assert vectorized_is_trivial(1, z2, z3) == trivial.c_trivial + assert vectorized_is_trivial(z1, 1, z3) == trivial.c_trivial + assert vectorized_is_trivial(z1, z2, 1) == trivial.c_trivial + assert vectorized_is_trivial(z1[::2, ::2], 1, 1) == trivial.non_trivial + assert vectorized_is_trivial(1, 1, z1[::2, ::2]) == trivial.c_trivial + assert vectorized_is_trivial(1, 1, z3[::2, ::2]) == trivial.non_trivial + assert vectorized_is_trivial(z1, 1, z3[1::4, 1::4]) == trivial.c_trivial + + y1 = np.array(z1, order="F") + y2 = np.array(y1) + y3 = np.array(y1) + assert vectorized_is_trivial(y1, y2, y3) == trivial.f_trivial + assert vectorized_is_trivial(y1, 1, 1) == trivial.f_trivial + assert vectorized_is_trivial(1, y2, 1) == trivial.f_trivial + assert vectorized_is_trivial(1, 1, y3) == trivial.f_trivial + assert vectorized_is_trivial(y1, z2, 1) == trivial.non_trivial + assert vectorized_is_trivial(z1[1::4, 1::4], y2, 1) == trivial.f_trivial + assert vectorized_is_trivial(y1[1::4, 1::4], z2, 1) == trivial.c_trivial + + assert m.vectorized_func(z1, z2, z3).flags.c_contiguous + assert m.vectorized_func(y1, y2, y3).flags.f_contiguous + assert m.vectorized_func(z1, 1, 1).flags.c_contiguous + assert m.vectorized_func(1, y2, 1).flags.f_contiguous + assert m.vectorized_func(z1[1::4, 1::4], y2, 1).flags.f_contiguous + assert m.vectorized_func(y1[1::4, 1::4], z2, 1).flags.c_contiguous + + +def test_passthrough_arguments(doc): + assert doc(m.vec_passthrough) == ( + "vec_passthrough(" + + ", ".join( + [ + "arg0: typing.SupportsFloat | typing.SupportsIndex", + "arg1: typing.Annotated[numpy.typing.ArrayLike, numpy.float64]", + "arg2: typing.Annotated[numpy.typing.ArrayLike, numpy.float64]", + "arg3: typing.Annotated[numpy.typing.ArrayLike, numpy.int32]", + "arg4: typing.SupportsInt | typing.SupportsIndex", + "arg5: m.numpy_vectorize.NonPODClass", + "arg6: typing.Annotated[numpy.typing.ArrayLike, numpy.float64]", + ] + ) + + ") -> object" + ) + + b = np.array([[10, 20, 30]], dtype="float64") + c = np.array([100, 200]) # NOT a vectorized argument + d = np.array([[1000], [2000], [3000]], dtype="int") + g = np.array([[1000000, 2000000, 3000000]], dtype="int") # requires casting + assert np.all( + m.vec_passthrough(1, b, c, d, 10000, m.NonPODClass(100000), g) + == np.array( + [ + [1111111, 2111121, 3111131], + [1112111, 2112121, 3112131], + [1113111, 2113121, 3113131], + ] + ) + ) + + +def test_method_vectorization(): + o = m.VectorizeTestClass(3) + x = np.array([1, 2], dtype="int") + y = np.array([[10], [20]], dtype="float32") + assert np.all(o.method(x, y) == [[14, 15], [24, 25]]) + + +def test_ref_qualified_method_vectorization(): + """Test issue #2234 follow-up: vectorize with lvalue-ref-qualified member pointers. + + Covers: + - vectorize(Return (Class::*)(Args...) &) + - vectorize(Return (Class::*)(Args...) const &) + - vectorize(Return (Class::*)(Args...) & noexcept) + - vectorize(Return (Class::*)(Args...) const & noexcept) + """ + o = m.VectorizeTestClass(3) + x = np.array([1, 2], dtype="int") + y = np.array([[10], [20]], dtype="float32") + assert np.all(o.method_lref(x, y) == [[14, 15], [24, 25]]) + assert np.all(o.method_const_lref(x, y) == [[14, 15], [24, 25]]) + + +@pytest.mark.skipif( + not defined___cpp_noexcept_function_type, + reason="Requires __cpp_noexcept_function_type", +) +def test_noexcept_ref_qualified_method_vectorization(): + """Test issue #2234 follow-up: vectorize with noexcept lvalue-ref-qualified member pointers. + + Covers: + - vectorize(Return (Class::*)(Args...) & noexcept) + - vectorize(Return (Class::*)(Args...) const & noexcept) + """ + o = m.VectorizeTestClass(3) + x = np.array([1, 2], dtype="int") + y = np.array([[10], [20]], dtype="float32") + assert np.all(o.method_lref_noexcept(x, y) == [[14, 15], [24, 25]]) + assert np.all(o.method_const_lref_noexcept(x, y) == [[14, 15], [24, 25]]) + + +def test_noexcept_method_vectorization(): + """Test issue #2234: vectorize must handle noexcept member function pointers. + + Covers both new vectorize specialisations: + - vectorize(Return (Class::*)(Args...) noexcept) + - vectorize(Return (Class::*)(Args...) const noexcept) + """ + o = m.VectorizeTestClass(3) + x = np.array([1, 2], dtype="int") + y = np.array([[10], [20]], dtype="float32") + # vectorize(Return (Class::*)(Args...) noexcept) + assert np.all(o.method_noexcept(x, y) == [[14, 15], [24, 25]]) + # vectorize(Return (Class::*)(Args...) const noexcept) + assert np.all(o.method_const_noexcept(x, y) == [[14, 15], [24, 25]]) + + +def test_array_collapse(): + assert not isinstance(m.vectorized_func(1, 2, 3), np.ndarray) + assert not isinstance(m.vectorized_func(np.array(1), 2, 3), np.ndarray) + z = m.vectorized_func([1], 2, 3) + assert isinstance(z, np.ndarray) + assert z.shape == (1,) + z = m.vectorized_func(1, [[[2]]], 3) + assert isinstance(z, np.ndarray) + assert z.shape == (1, 1, 1) + + +def test_vectorized_noreturn(): + x = m.NonPODClass(0) + assert x.value == 0 + m.add_to(x, [1, 2, 3, 4]) + assert x.value == 10 + m.add_to(x, 1) + assert x.value == 11 + m.add_to(x, [[1, 1], [2, 3]]) + assert x.value == 18 diff --git a/external_libraries/pybind11/tests/test_opaque_types.cpp b/external_libraries/pybind11/tests/test_opaque_types.cpp new file mode 100644 index 00000000..2e972d0b --- /dev/null +++ b/external_libraries/pybind11/tests/test_opaque_types.cpp @@ -0,0 +1,77 @@ +/* + tests/test_opaque_types.cpp -- opaque types, passing void pointers + + Copyright (c) 2016 Wenzel Jakob + + All rights reserved. Use of this source code is governed by a + BSD-style license that can be found in the LICENSE file. +*/ + +#include + +#include "pybind11_tests.h" + +#include + +// IMPORTANT: Disable internal pybind11 translation mechanisms for STL data structures +// +// This also deliberately doesn't use the below StringList type alias to test +// that MAKE_OPAQUE can handle a type containing a `,`. (The `std::allocator` +// bit is just the default `std::vector` allocator). +PYBIND11_MAKE_OPAQUE(std::vector>) + +using StringList = std::vector>; + +TEST_SUBMODULE(opaque_types, m) { + // test_string_list + py::class_(m, "StringList") + .def(py::init<>()) + .def("pop_back", &StringList::pop_back) + /* There are multiple versions of push_back(), etc. Select the right ones. */ + .def("push_back", (void (StringList::*)(const std::string &)) &StringList::push_back) + .def("back", (std::string & (StringList::*) ()) & StringList::back) + .def("__len__", [](const StringList &v) { return v.size(); }) + .def( + "__iter__", + [](StringList &v) { return py::make_iterator(v.begin(), v.end()); }, + py::keep_alive<0, 1>()); + + class ClassWithSTLVecProperty { + public: + StringList stringList; + }; + py::class_(m, "ClassWithSTLVecProperty") + .def(py::init<>()) + .def_readwrite("stringList", &ClassWithSTLVecProperty::stringList); + + m.def("print_opaque_list", [](const StringList &l) { + std::string ret = "Opaque list: ["; + bool first = true; + for (const auto &entry : l) { + if (!first) { + ret += ", "; + } + ret += entry; + first = false; + } + return ret + "]"; + }); + + // test_pointers + m.def("return_void_ptr", []() { return (void *) 0x1234; }); + m.def("get_void_ptr_value", [](void *ptr) { return reinterpret_cast(ptr); }); + m.def("return_null_str", []() { return (char *) nullptr; }); + m.def("get_null_str_value", [](char *ptr) { return reinterpret_cast(ptr); }); + + m.def("return_unique_ptr", []() -> std::unique_ptr { + auto *result = new StringList(); + result->emplace_back("some value"); + return std::unique_ptr(result); + }); + + // test unions + py::class_(m, "IntFloat") + .def(py::init<>()) + .def_readwrite("i", &IntFloat::i) + .def_readwrite("f", &IntFloat::f); +} diff --git a/external_libraries/pybind11/tests/test_opaque_types.py b/external_libraries/pybind11/tests/test_opaque_types.py new file mode 100644 index 00000000..7a4d7a43 --- /dev/null +++ b/external_libraries/pybind11/tests/test_opaque_types.py @@ -0,0 +1,64 @@ +from __future__ import annotations + +import pytest + +import env +from pybind11_tests import ConstructorStats, UserType +from pybind11_tests import opaque_types as m + + +def test_string_list(): + lst = m.StringList() + lst.push_back("Element 1") + lst.push_back("Element 2") + assert m.print_opaque_list(lst) == "Opaque list: [Element 1, Element 2]" + assert lst.back() == "Element 2" + + for i, k in enumerate(lst, start=1): + assert k == f"Element {i}" + lst.pop_back() + assert m.print_opaque_list(lst) == "Opaque list: [Element 1]" + + cvp = m.ClassWithSTLVecProperty() + assert m.print_opaque_list(cvp.stringList) == "Opaque list: []" + + cvp.stringList = lst + cvp.stringList.push_back("Element 3") + assert m.print_opaque_list(cvp.stringList) == "Opaque list: [Element 1, Element 3]" + + +def test_pointers(msg, backport_typehints): + living_before = ConstructorStats.get(UserType).alive() + assert m.get_void_ptr_value(m.return_void_ptr()) == 0x1234 + assert m.get_void_ptr_value(UserType()) # Should also work for other C++ types + + if not env.GRAALPY: + assert ConstructorStats.get(UserType).alive() == living_before + + with pytest.raises(TypeError) as excinfo: + m.get_void_ptr_value([1, 2, 3]) # This should not work + + assert ( + backport_typehints(msg(excinfo.value)) + == """ + get_void_ptr_value(): incompatible function arguments. The following argument types are supported: + 1. (arg0: types.CapsuleType) -> int + + Invoked with: [1, 2, 3] + """ + ) + + assert m.return_null_str() is None + assert m.get_null_str_value(m.return_null_str()) is not None + + ptr = m.return_unique_ptr() + assert "StringList" in repr(ptr) + assert m.print_opaque_list(ptr) == "Opaque list: [some value]" + + +def test_unions(): + int_float_union = m.IntFloat() + int_float_union.i = 42 + assert int_float_union.i == 42 + int_float_union.f = 3.0 + assert int_float_union.f == 3.0 diff --git a/external_libraries/pybind11/tests/test_operator_overloading.cpp b/external_libraries/pybind11/tests/test_operator_overloading.cpp new file mode 100644 index 00000000..112a363b --- /dev/null +++ b/external_libraries/pybind11/tests/test_operator_overloading.cpp @@ -0,0 +1,281 @@ +/* + tests/test_operator_overloading.cpp -- operator overloading + + Copyright (c) 2016 Wenzel Jakob + + All rights reserved. Use of this source code is governed by a + BSD-style license that can be found in the LICENSE file. +*/ + +#include +#include + +#include "constructor_stats.h" +#include "pybind11_tests.h" + +#include + +class Vector2 { +public: + Vector2(float x, float y) : x(x), y(y) { print_created(this, toString()); } + Vector2(const Vector2 &v) : x(v.x), y(v.y) { print_copy_created(this); } + Vector2(Vector2 &&v) noexcept : x(v.x), y(v.y) { + print_move_created(this); + v.x = v.y = 0; + } + Vector2 &operator=(const Vector2 &v) { + x = v.x; + y = v.y; + print_copy_assigned(this); + return *this; + } + Vector2 &operator=(Vector2 &&v) noexcept { + x = v.x; + y = v.y; + v.x = v.y = 0; + print_move_assigned(this); + return *this; + } + ~Vector2() { print_destroyed(this); } + + std::string toString() const { + return "[" + std::to_string(x) + ", " + std::to_string(y) + "]"; + } + + Vector2 operator-() const { return Vector2(-x, -y); } + Vector2 operator+(const Vector2 &v) const { return Vector2(x + v.x, y + v.y); } + Vector2 operator-(const Vector2 &v) const { return Vector2(x - v.x, y - v.y); } + Vector2 operator-(float value) const { return Vector2(x - value, y - value); } + Vector2 operator+(float value) const { return Vector2(x + value, y + value); } + Vector2 operator*(float value) const { return Vector2(x * value, y * value); } + Vector2 operator/(float value) const { return Vector2(x / value, y / value); } + Vector2 operator*(const Vector2 &v) const { return Vector2(x * v.x, y * v.y); } + Vector2 operator/(const Vector2 &v) const { return Vector2(x / v.x, y / v.y); } + Vector2 &operator+=(const Vector2 &v) { + x += v.x; + y += v.y; + return *this; + } + Vector2 &operator-=(const Vector2 &v) { + x -= v.x; + y -= v.y; + return *this; + } + Vector2 &operator*=(float v) { + x *= v; + y *= v; + return *this; + } + Vector2 &operator/=(float v) { + x /= v; + y /= v; + return *this; + } + Vector2 &operator*=(const Vector2 &v) { + x *= v.x; + y *= v.y; + return *this; + } + Vector2 &operator/=(const Vector2 &v) { + x /= v.x; + y /= v.y; + return *this; + } + + friend Vector2 operator+(float f, const Vector2 &v) { return Vector2(f + v.x, f + v.y); } + friend Vector2 operator-(float f, const Vector2 &v) { return Vector2(f - v.x, f - v.y); } + friend Vector2 operator*(float f, const Vector2 &v) { return Vector2(f * v.x, f * v.y); } + friend Vector2 operator/(float f, const Vector2 &v) { return Vector2(f / v.x, f / v.y); } + + bool operator==(const Vector2 &v) const { return x == v.x && y == v.y; } + bool operator!=(const Vector2 &v) const { return x != v.x || y != v.y; } + +private: + float x, y; +}; + +class C1 {}; +class C2 {}; + +int operator+(const C1 &, const C1 &) { return 11; } +int operator+(const C2 &, const C2 &) { return 22; } +int operator+(const C2 &, const C1 &) { return 21; } +int operator+(const C1 &, const C2 &) { return 12; } + +struct HashMe { + std::string member; +}; + +bool operator==(const HashMe &lhs, const HashMe &rhs) { return lhs.member == rhs.member; } + +// Note: Specializing explicit within `namespace std { ... }` is done due to a +// bug in GCC<7. If you are supporting compilers later than this, consider +// specializing `using template<> struct std::hash<...>` in the global +// namespace instead, per this recommendation: +// https://en.cppreference.com/w/cpp/language/extending_std#Adding_template_specializations +namespace std { +template <> +struct hash { + // Not a good hash function, but easy to test + size_t operator()(const Vector2 &) { return 4; } +}; + +// HashMe has a hash function in C++ but no `__hash__` for Python. +template <> +struct hash { + std::size_t operator()(const HashMe &selector) const { + return std::hash()(selector.member); + } +}; +} // namespace std + +// Not a good abs function, but easy to test. +std::string abs(const Vector2 &) { return "abs(Vector2)"; } + +// clang 7.0.0 and Apple LLVM 10.0.1 introduce `-Wself-assign-overloaded` to +// `-Wall`, which is used here for overloading (e.g. `py::self += py::self `). +// Here, we suppress the warning +// Taken from: https://github.com/RobotLocomotion/drake/commit/aaf84b46 +// TODO(eric): This could be resolved using a function / functor (e.g. `py::self()`). +#if defined(__APPLE__) && defined(__clang__) +# if (__clang_major__ >= 10) +PYBIND11_WARNING_DISABLE_CLANG("-Wself-assign-overloaded") +# endif +#elif defined(__clang__) +# if (__clang_major__ >= 7) +PYBIND11_WARNING_DISABLE_CLANG("-Wself-assign-overloaded") +# endif +#endif + +TEST_SUBMODULE(operators, m) { + + // test_operator_overloading + py::class_(m, "Vector2") + .def(py::init()) + .def(py::self + py::self) + .def(py::self + float()) + .def(py::self - py::self) + .def(py::self - float()) + .def(py::self * float()) + .def(py::self / float()) + .def(py::self * py::self) + .def(py::self / py::self) + .def(py::self += py::self) + .def(py::self -= py::self) + .def(py::self *= float()) + .def(py::self /= float()) + .def(py::self *= py::self) + .def(py::self /= py::self) + .def(float() + py::self) + .def(float() - py::self) + .def(float() * py::self) + .def(float() / py::self) + .def(-py::self) + .def("__str__", &Vector2::toString) + .def("__repr__", &Vector2::toString) + .def(py::self == py::self) + .def(py::self != py::self) + .def(py::hash(py::self)) + // N.B. See warning about usage of `py::detail::abs(py::self)` in + // `operators.h`. + .def("__abs__", [](const Vector2 &v) { return abs(v); }); + + m.attr("Vector") = m.attr("Vector2"); + + // test_operators_notimplemented + // #393: need to return NotSupported to ensure correct arithmetic operator behavior + py::class_(m, "C1").def(py::init<>()).def(py::self + py::self); + + py::class_(m, "C2") + .def(py::init<>()) + .def(py::self + py::self) + .def("__add__", [](const C2 &c2, const C1 &c1) { return c2 + c1; }) + .def("__radd__", [](const C2 &c2, const C1 &c1) { return c1 + c2; }); + + // test_nested + // #328: first member in a class can't be used in operators + struct NestABase { + int value = -2; + }; + py::class_(m, "NestABase") + .def(py::init<>()) + .def_readwrite("value", &NestABase::value); + + struct NestA : NestABase { + int value = 3; + NestA &operator+=(int i) { + value += i; + return *this; + } + }; + py::class_(m, "NestA") + .def(py::init<>()) + .def(py::self += int()) + .def( + "as_base", + [](NestA &a) -> NestABase & { return (NestABase &) a; }, + py::return_value_policy::reference_internal); + m.def("get_NestA", [](const NestA &a) { return a.value; }); + + struct NestB { + NestA a; + int value = 4; + NestB &operator-=(int i) { + value -= i; + return *this; + } + }; + py::class_(m, "NestB") + .def(py::init<>()) + .def(py::self -= int()) + .def_readwrite("a", &NestB::a); + m.def("get_NestB", [](const NestB &b) { return b.value; }); + + struct NestC { + NestB b; + int value = 5; + NestC &operator*=(int i) { + value *= i; + return *this; + } + }; + py::class_(m, "NestC") + .def(py::init<>()) + .def(py::self *= int()) + .def_readwrite("b", &NestC::b); + m.def("get_NestC", [](const NestC &c) { return c.value; }); + + // test_overriding_eq_reset_hash + // #2191 Overriding __eq__ should set __hash__ to None + struct Comparable { + int value; + bool operator==(const Comparable &rhs) const { return value == rhs.value; } + }; + + struct Hashable : Comparable { + explicit Hashable(int value) : Comparable{value} {}; + size_t hash() const { return static_cast(value); } + }; + + struct Hashable2 : Hashable { + using Hashable::Hashable; + }; + + py::class_(m, "Comparable").def(py::init()).def(py::self == py::self); + + py::class_(m, "Hashable") + .def(py::init()) + .def(py::self == py::self) + .def("__hash__", &Hashable::hash); + + // define __hash__ before __eq__ + py::class_(m, "Hashable2") + .def("__hash__", &Hashable::hash) + .def(py::init()) + .def(py::self == py::self); + + // define __eq__ but not __hash__ + py::class_(m, "HashMe").def(py::self == py::self); + + m.def("get_unhashable_HashMe_set", []() { return std::unordered_set{{"one"}}; }); +} diff --git a/external_libraries/pybind11/tests/test_operator_overloading.py b/external_libraries/pybind11/tests/test_operator_overloading.py new file mode 100644 index 00000000..19c4bbbb --- /dev/null +++ b/external_libraries/pybind11/tests/test_operator_overloading.py @@ -0,0 +1,161 @@ +from __future__ import annotations + +import pytest + +import env +from pybind11_tests import ConstructorStats +from pybind11_tests import operators as m + + +@pytest.mark.xfail("env.GRAALPY", reason="TODO should get fixed on GraalPy side") +def test_operator_overloading(): + v1 = m.Vector2(1, 2) + v2 = m.Vector(3, -1) + v3 = m.Vector2(1, 2) # Same value as v1, but different instance. + assert v1 is not v3 + + assert str(v1) == "[1.000000, 2.000000]" + assert str(v2) == "[3.000000, -1.000000]" + + assert str(-v2) == "[-3.000000, 1.000000]" + + assert str(v1 + v2) == "[4.000000, 1.000000]" + assert str(v1 - v2) == "[-2.000000, 3.000000]" + assert str(v1 - 8) == "[-7.000000, -6.000000]" + assert str(v1 + 8) == "[9.000000, 10.000000]" + assert str(v1 * 8) == "[8.000000, 16.000000]" + assert str(v1 / 8) == "[0.125000, 0.250000]" + assert str(8 - v1) == "[7.000000, 6.000000]" + assert str(8 + v1) == "[9.000000, 10.000000]" + assert str(8 * v1) == "[8.000000, 16.000000]" + assert str(8 / v1) == "[8.000000, 4.000000]" + assert str(v1 * v2) == "[3.000000, -2.000000]" + assert str(v2 / v1) == "[3.000000, -0.500000]" + + assert v1 == v3 + assert v1 != v2 + assert hash(v1) == 4 + # TODO(eric.cousineau): Make this work. + # assert abs(v1) == "abs(Vector2)" + + v1 += 2 * v2 + assert str(v1) == "[7.000000, 0.000000]" + v1 -= v2 + assert str(v1) == "[4.000000, 1.000000]" + v1 *= 2 + assert str(v1) == "[8.000000, 2.000000]" + v1 /= 16 + assert str(v1) == "[0.500000, 0.125000]" + v1 *= v2 + assert str(v1) == "[1.500000, -0.125000]" + v2 /= v1 + assert str(v2) == "[2.000000, 8.000000]" + + if env.GRAALPY: + pytest.skip("ConstructorStats is incompatible with GraalPy.") + + cstats = ConstructorStats.get(m.Vector2) + assert cstats.alive() == 3 + del v1 + assert cstats.alive() == 2 + del v2 + assert cstats.alive() == 1 + del v3 + assert cstats.alive() == 0 + assert cstats.values() == [ + "[1.000000, 2.000000]", + "[3.000000, -1.000000]", + "[1.000000, 2.000000]", + "[-3.000000, 1.000000]", + "[4.000000, 1.000000]", + "[-2.000000, 3.000000]", + "[-7.000000, -6.000000]", + "[9.000000, 10.000000]", + "[8.000000, 16.000000]", + "[0.125000, 0.250000]", + "[7.000000, 6.000000]", + "[9.000000, 10.000000]", + "[8.000000, 16.000000]", + "[8.000000, 4.000000]", + "[3.000000, -2.000000]", + "[3.000000, -0.500000]", + "[6.000000, -2.000000]", + ] + assert cstats.default_constructions == 0 + assert cstats.copy_constructions == 0 + assert cstats.move_constructions >= 10 + assert cstats.copy_assignments == 0 + assert cstats.move_assignments == 0 + + +@pytest.mark.xfail( + env.GRAALPY and env.GRAALPY_VERSION < (24, 2), reason="Fixed in GraalPy 24.2" +) +def test_operators_notimplemented(): + """#393: need to return NotSupported to ensure correct arithmetic operator behavior""" + + c1, c2 = m.C1(), m.C2() + assert c1 + c1 == 11 + assert c2 + c2 == 22 + assert c2 + c1 == 21 + assert c1 + c2 == 12 + + +def test_nested(): + """#328: first member in a class can't be used in operators""" + + a = m.NestA() + b = m.NestB() + c = m.NestC() + + a += 10 + assert m.get_NestA(a) == 13 + b.a += 100 + assert m.get_NestA(b.a) == 103 + c.b.a += 1000 + assert m.get_NestA(c.b.a) == 1003 + b -= 1 + assert m.get_NestB(b) == 3 + c.b -= 3 + assert m.get_NestB(c.b) == 1 + c *= 7 + assert m.get_NestC(c) == 35 + + abase = a.as_base() + assert abase.value == -2 + a.as_base().value += 44 + assert abase.value == 42 + assert c.b.a.as_base().value == -2 + c.b.a.as_base().value += 44 + assert c.b.a.as_base().value == 42 + + del c + pytest.gc_collect() + del a # Shouldn't delete while abase is still alive + pytest.gc_collect() + + assert abase.value == 42 + del abase, b + pytest.gc_collect() + + +def test_overriding_eq_reset_hash(): + assert m.Comparable(15) is not m.Comparable(15) + assert m.Comparable(15) == m.Comparable(15) + + with pytest.raises(TypeError) as excinfo: + hash(m.Comparable(15)) + assert str(excinfo.value).startswith("unhashable type:") + + for hashable in (m.Hashable, m.Hashable2): + assert hashable(15) is not hashable(15) + assert hashable(15) == hashable(15) + + assert hash(hashable(15)) == 15 + assert hash(hashable(15)) == hash(hashable(15)) + + +def test_return_set_of_unhashable(): + with pytest.raises(TypeError) as excinfo: + m.get_unhashable_HashMe_set() + assert "unhashable type" in str(excinfo.value.__cause__) diff --git a/external_libraries/pybind11/tests/test_pickling.cpp b/external_libraries/pybind11/tests/test_pickling.cpp new file mode 100644 index 00000000..6ba3d30e --- /dev/null +++ b/external_libraries/pybind11/tests/test_pickling.cpp @@ -0,0 +1,191 @@ +/* + tests/test_pickling.cpp -- pickle support + + Copyright (c) 2016 Wenzel Jakob + Copyright (c) 2021 The Pybind Development Team. + + All rights reserved. Use of this source code is governed by a + BSD-style license that can be found in the LICENSE file. +*/ + +#include "pybind11_tests.h" + +#include +#include +#include + +namespace exercise_trampoline { + +struct SimpleBase { + int num = 0; + virtual ~SimpleBase() = default; + + // For compatibility with old clang versions: + SimpleBase() = default; + SimpleBase(const SimpleBase &) = default; +}; + +struct SimpleBaseTrampoline : SimpleBase {}; + +struct SimpleCppDerived : SimpleBase {}; + +void wrap(py::module m) { + py::class_(m, "SimpleBase") + .def(py::init<>()) + .def_readwrite("num", &SimpleBase::num) + .def(py::pickle( + [](const py::object &self) { + py::dict d = py::getattr(self, "__dict__", py::dict()); + return py::make_tuple(self.attr("num"), d); + }, + [](const py::tuple &t) { + if (t.size() != 2) { + throw std::runtime_error("Invalid state!"); + } + auto cpp_state = std::unique_ptr(new SimpleBaseTrampoline); + cpp_state->num = t[0].cast(); + auto py_state = t[1].cast(); + return std::make_pair(std::move(cpp_state), py_state); + })); + + m.def("make_SimpleCppDerivedAsBase", + []() { return std::unique_ptr(new SimpleCppDerived); }); + m.def("check_dynamic_cast_SimpleCppDerived", [](const SimpleBase *base_ptr) { + return dynamic_cast(base_ptr) != nullptr; + }); +} + +} // namespace exercise_trampoline + +TEST_SUBMODULE(pickling, m) { + m.def("simple_callable", []() { return 20220426; }); + + // test_roundtrip + class Pickleable { + public: + explicit Pickleable(const std::string &value) : m_value(value) {} + const std::string &value() const { return m_value; } + + void setExtra1(int extra1) { m_extra1 = extra1; } + void setExtra2(int extra2) { m_extra2 = extra2; } + int extra1() const { return m_extra1; } + int extra2() const { return m_extra2; } + + private: + std::string m_value; + int m_extra1 = 0; + int m_extra2 = 0; + }; + + class PickleableNew : public Pickleable { + public: + using Pickleable::Pickleable; + }; + + py::class_ pyPickleable(m, "Pickleable"); + pyPickleable.def(py::init()) + .def("value", &Pickleable::value) + .def("extra1", &Pickleable::extra1) + .def("extra2", &Pickleable::extra2) + .def("setExtra1", &Pickleable::setExtra1) + .def("setExtra2", &Pickleable::setExtra2) + // For details on the methods below, refer to + // http://docs.python.org/3/library/pickle.html#pickling-class-instances + .def("__getstate__", [](const Pickleable &p) { + /* Return a tuple that fully encodes the state of the object */ + return py::make_tuple(p.value(), p.extra1(), p.extra2()); + }); + ignoreOldStyleInitWarnings([&pyPickleable]() { + pyPickleable.def("__setstate__", [](Pickleable &p, const py::tuple &t) { + if (t.size() != 3) { + throw std::runtime_error("Invalid state!"); + } + /* Invoke the constructor (need to use in-place version) */ + new (&p) Pickleable(t[0].cast()); + + /* Assign any additional state */ + p.setExtra1(t[1].cast()); + p.setExtra2(t[2].cast()); + }); + }); + + py::class_(m, "PickleableNew") + .def(py::init()) + .def(py::pickle( + [](const PickleableNew &p) { + return py::make_tuple(p.value(), p.extra1(), p.extra2()); + }, + [](const py::tuple &t) { + if (t.size() != 3) { + throw std::runtime_error("Invalid state!"); + } + auto p = PickleableNew(t[0].cast()); + + p.setExtra1(t[1].cast()); + p.setExtra2(t[2].cast()); + return p; + })); + +#if !defined(PYPY_VERSION) + // test_roundtrip_with_dict + class PickleableWithDict { + public: + explicit PickleableWithDict(const std::string &value) : value(value) {} + + std::string value; + int extra; + }; + + class PickleableWithDictNew : public PickleableWithDict { + public: + using PickleableWithDict::PickleableWithDict; + }; + + py::class_ pyPickleableWithDict( + m, "PickleableWithDict", py::dynamic_attr()); + pyPickleableWithDict.def(py::init()) + .def_readwrite("value", &PickleableWithDict::value) + .def_readwrite("extra", &PickleableWithDict::extra) + .def("__getstate__", [](const py::object &self) { + /* Also include __dict__ in state */ + return py::make_tuple(self.attr("value"), self.attr("extra"), self.attr("__dict__")); + }); + ignoreOldStyleInitWarnings([&pyPickleableWithDict]() { + pyPickleableWithDict.def("__setstate__", [](const py::object &self, const py::tuple &t) { + if (t.size() != 3) { + throw std::runtime_error("Invalid state!"); + } + /* Cast and construct */ + auto &p = self.cast(); + new (&p) PickleableWithDict(t[0].cast()); + + /* Assign C++ state */ + p.extra = t[1].cast(); + + /* Assign Python state */ + self.attr("__dict__") = t[2]; + }); + }); + + py::class_(m, "PickleableWithDictNew") + .def(py::init()) + .def(py::pickle( + [](const py::object &self) { + return py::make_tuple( + self.attr("value"), self.attr("extra"), self.attr("__dict__")); + }, + [](const py::tuple &t) { + if (t.size() != 3) { + throw std::runtime_error("Invalid state!"); + } + + auto cpp_state = PickleableWithDictNew(t[0].cast()); + cpp_state.extra = t[1].cast(); + + auto py_state = t[2].cast(); + return std::make_pair(cpp_state, py_state); + })); +#endif + + exercise_trampoline::wrap(m); +} diff --git a/external_libraries/pybind11/tests/test_pickling.py b/external_libraries/pybind11/tests/test_pickling.py new file mode 100644 index 00000000..9febc3f6 --- /dev/null +++ b/external_libraries/pybind11/tests/test_pickling.py @@ -0,0 +1,149 @@ +from __future__ import annotations + +import pickle +import re +import sys + +import pytest + +import env +from pybind11_tests import pickling as m + + +def all_pickle_protocols(): + assert pickle.HIGHEST_PROTOCOL >= 0 + return range(pickle.HIGHEST_PROTOCOL + 1) + + +@pytest.mark.parametrize("protocol", all_pickle_protocols()) +def test_pickle_simple_callable(protocol): + assert m.simple_callable() == 20220426 + serialized = pickle.dumps(m.simple_callable, protocol=protocol) + assert b"pybind11_tests.pickling" in serialized + assert b"simple_callable" in serialized + deserialized = pickle.loads(serialized) + assert deserialized() == 20220426 + assert deserialized is m.simple_callable + + # UNUSUAL: function record pickle roundtrip returns a module, not a function record object: + if not env.PYPY: + assert ( + pickle.loads(pickle.dumps(m.simple_callable.__self__, protocol=protocol)) + is m + ) + # This is not expected to create issues because the only purpose of + # `m.simple_callable.__self__` is to enable pickling: the only method it has is + # `__reduce_ex__`. Direct access for any other purpose is not supported. + # Note that `repr(m.simple_callable.__self__)` shows, e.g.: + # `` + # It is considered to be as much an implementation detail as the + # `pybind11::detail::function_record` C++ type is. + + # @rainwoodman suggested that the unusual pickle roundtrip behavior could be + # avoided by changing `reduce_ex_impl()` to produce, e.g.: + # `"__import__('importlib').import_module('pybind11_tests.pickling').simple_callable.__self__"` + # as the argument for the `eval()` function, and adding a getter to the + # `function_record_PyTypeObject` that returns `self`. However, the additional code complexity + # for this is deemed warranted only if the unusual pickle roundtrip behavior actually + # creates issues. + + +@pytest.mark.parametrize("cls_name", ["Pickleable", "PickleableNew"]) +def test_roundtrip(cls_name): + cls = getattr(m, cls_name) + p = cls("test_value") + p.setExtra1(15) + p.setExtra2(48) + + data = pickle.dumps(p, 2) # Must use pickle protocol >= 2 + p2 = pickle.loads(data) + assert p2.value() == p.value() + assert p2.extra1() == p.extra1() + assert p2.extra2() == p.extra2() + + +@pytest.mark.xfail("env.PYPY") +@pytest.mark.parametrize( + "cls_name", + [ + pytest.param( + "PickleableWithDict", + marks=pytest.mark.skipif( + sys.version_info in ((3, 14, 0, "beta", 1), (3, 14, 0, "beta", 2)), + reason="3.14.0b1/2 managed dict bug: https://github.com/python/cpython/issues/133912", + ), + ), + "PickleableWithDictNew", + ], +) +def test_roundtrip_with_dict(cls_name): + cls = getattr(m, cls_name) + p = cls("test_value") + p.extra = 15 + p.dynamic = "Attribute" + + data = pickle.dumps(p, pickle.HIGHEST_PROTOCOL) + p2 = pickle.loads(data) + assert p2.value == p.value + assert p2.extra == p.extra + assert p2.dynamic == p.dynamic + + +def test_enum_pickle(): + from pybind11_tests import enums as e + + data = pickle.dumps(e.EOne, 2) + assert e.EOne == pickle.loads(data) + + +# +# exercise_trampoline +# +class SimplePyDerived(m.SimpleBase): + pass + + +def test_roundtrip_simple_py_derived(): + p = SimplePyDerived() + p.num = 202 + p.stored_in_dict = 303 + data = pickle.dumps(p, pickle.HIGHEST_PROTOCOL) + p2 = pickle.loads(data) + assert isinstance(p2, SimplePyDerived) + assert p2.num == 202 + assert p2.stored_in_dict == 303 + + +def test_roundtrip_simple_cpp_derived(): + p = m.make_SimpleCppDerivedAsBase() + assert m.check_dynamic_cast_SimpleCppDerived(p) + p.num = 404 + if not env.PYPY: + # To ensure that this unit test is not accidentally invalidated. + with pytest.raises(AttributeError): + # Mimics the `setstate` C++ implementation. + setattr(p, "__dict__", {}) # noqa: B010 + data = pickle.dumps(p, pickle.HIGHEST_PROTOCOL) + p2 = pickle.loads(data) + assert isinstance(p2, m.SimpleBase) + assert p2.num == 404 + # Issue #3062: pickleable base C++ classes can incur object slicing + # if derived typeid is not registered with pybind11 + assert not m.check_dynamic_cast_SimpleCppDerived(p2) + + +def test_new_style_pickle_getstate_pos_only(): + assert ( + re.match( + r"^__getstate__\(self: [\w\.]+, /\)", m.PickleableNew.__getstate__.__doc__ + ) + is not None + ) + if hasattr(m, "PickleableWithDictNew"): + assert ( + re.match( + r"^__getstate__\(self: [\w\.]+, /\)", + m.PickleableWithDictNew.__getstate__.__doc__, + ) + is not None + ) diff --git a/external_libraries/pybind11/tests/test_potentially_slicing_weak_ptr.cpp b/external_libraries/pybind11/tests/test_potentially_slicing_weak_ptr.cpp new file mode 100644 index 00000000..c1bf36f1 --- /dev/null +++ b/external_libraries/pybind11/tests/test_potentially_slicing_weak_ptr.cpp @@ -0,0 +1,170 @@ +// Copyright (c) 2025 The pybind Community. +// All rights reserved. Use of this source code is governed by a +// BSD-style license that can be found in the LICENSE file. + +#include "pybind11_tests.h" + +#include + +namespace pybind11_tests { +namespace potentially_slicing_weak_ptr { + +template // Using int as a trick to easily generate multiple types. +struct VirtBase { + VirtBase() = default; + virtual ~VirtBase() = default; + VirtBase(const VirtBase &) = delete; + virtual int get_code() { return 100; } +}; + +using VirtBaseSH = VirtBase<0>; // for testing with py::smart_holder +using VirtBaseSP = VirtBase<1>; // for testing with std::shared_ptr as holder + +// Similar to trampoline_self_life_support +struct trampoline_is_alive_simple { + std::uint64_t magic_token = 197001010000u; + + trampoline_is_alive_simple() = default; + + ~trampoline_is_alive_simple() { magic_token = 20380118191407u; } + + trampoline_is_alive_simple(const trampoline_is_alive_simple &other) = default; + trampoline_is_alive_simple(trampoline_is_alive_simple &&other) noexcept + : magic_token(other.magic_token) { + other.magic_token = 20380118191407u; + } + + trampoline_is_alive_simple &operator=(const trampoline_is_alive_simple &) = delete; + trampoline_is_alive_simple &operator=(trampoline_is_alive_simple &&) = delete; +}; + +template +const char *determine_trampoline_state(const std::shared_ptr &sp) { + if (!sp) { + return "sp nullptr"; + } + auto *tias = dynamic_cast(sp.get()); + if (!tias) { + return "dynamic_cast failed"; + } + if (tias->magic_token == 197001010000u) { + return "trampoline alive"; + } + if (tias->magic_token == 20380118191407u) { + return "trampoline DEAD"; + } + return "UNDEFINED BEHAVIOR"; +} + +struct PyVirtBaseSH : VirtBaseSH, py::trampoline_self_life_support, trampoline_is_alive_simple { + using VirtBaseSH::VirtBaseSH; + int get_code() override { PYBIND11_OVERRIDE(int, VirtBaseSH, get_code); } +}; + +struct PyVirtBaseSP : VirtBaseSP, trampoline_is_alive_simple { // self-life-support not available + using VirtBaseSP::VirtBaseSP; + int get_code() override { PYBIND11_OVERRIDE(int, VirtBaseSP, get_code); } +}; + +template +std::shared_ptr rtrn_obj_cast_shared_ptr(py::handle obj) { + return obj.cast>(); +} + +// There is no type_caster>, and to minimize code complexity +// we do not want to add one, therefore we have to return a shared_ptr here. +template +std::shared_ptr rtrn_potentially_slicing_shared_ptr(py::handle obj) { + return py::potentially_slicing_weak_ptr(obj).lock(); +} + +template +struct SpOwner { + void set_sp(const std::shared_ptr &sp_) { sp = sp_; } + + int get_code() const { + if (!sp) { + return -888; + } + return sp->get_code(); + } + + const char *get_trampoline_state() const { return determine_trampoline_state(sp); } + +private: + std::shared_ptr sp; +}; + +template +struct WpOwner { + void set_wp(const std::weak_ptr &wp_) { wp = wp_; } + + int get_code() const { + auto sp = wp.lock(); + if (!sp) { + return -999; + } + return sp->get_code(); + } + + const char *get_trampoline_state() const { return determine_trampoline_state(wp.lock()); } + +private: + std::weak_ptr wp; +}; + +template +void wrap(py::module_ &m, + const char *roc_pyname, + const char *rps_pyname, + const char *spo_pyname, + const char *wpo_pyname) { + m.def(roc_pyname, rtrn_obj_cast_shared_ptr); + m.def(rps_pyname, rtrn_potentially_slicing_shared_ptr); + + py::classh>(m, spo_pyname) + .def(py::init<>()) + .def("set_sp", &SpOwner::set_sp) + .def("get_code", &SpOwner::get_code) + .def("get_trampoline_state", &SpOwner::get_trampoline_state); + + py::classh>(m, wpo_pyname) + .def(py::init<>()) + .def("set_wp", + [](WpOwner &self, py::handle obj) { + self.set_wp(obj.cast>()); + }) + .def("set_wp_potentially_slicing", + [](WpOwner &self, py::handle obj) { + self.set_wp(py::potentially_slicing_weak_ptr(obj)); + }) + .def("get_code", &WpOwner::get_code) + .def("get_trampoline_state", &WpOwner::get_trampoline_state); +} + +} // namespace potentially_slicing_weak_ptr +} // namespace pybind11_tests + +using namespace pybind11_tests::potentially_slicing_weak_ptr; + +TEST_SUBMODULE(potentially_slicing_weak_ptr, m) { + py::classh(m, "VirtBaseSH") + .def(py::init<>()) + .def("get_code", &VirtBaseSH::get_code); + + py::class_, PyVirtBaseSP>(m, "VirtBaseSP") + .def(py::init<>()) + .def("get_code", &VirtBaseSP::get_code); + + wrap(m, + "SH_rtrn_obj_cast_shared_ptr", + "SH_rtrn_potentially_slicing_shared_ptr", + "SH_SpOwner", + "SH_WpOwner"); + + wrap(m, + "SP_rtrn_obj_cast_shared_ptr", + "SP_rtrn_potentially_slicing_shared_ptr", + "SP_SpOwner", + "SP_WpOwner"); +} diff --git a/external_libraries/pybind11/tests/test_potentially_slicing_weak_ptr.py b/external_libraries/pybind11/tests/test_potentially_slicing_weak_ptr.py new file mode 100644 index 00000000..d442f99a --- /dev/null +++ b/external_libraries/pybind11/tests/test_potentially_slicing_weak_ptr.py @@ -0,0 +1,174 @@ +# Copyright (c) 2025 The pybind Community. +# All rights reserved. Use of this source code is governed by a +# BSD-style license that can be found in the LICENSE file. + +# This module tests the interaction of pybind11's shared_ptr and smart_holder +# mechanisms with trampoline object lifetime management and inheritance slicing. +# +# The following combinations are covered: +# +# - Holder type: std::shared_ptr (class_ holder) vs. +# py::smart_holder +# - Conversion function: obj.cast>() vs. +# py::potentially_slicing_weak_ptr(obj) +# - Python object type: C++ base class vs. +# Python-derived trampoline class +# +# The tests verify +# +# - that casting or passing Python objects into functions returns usable +# std::shared_ptr instances. +# - that inheritance slicing occurs as expected in controlled cases +# (issue #1333). +# - that surprising weak_ptr behavior (issue #5623) can be reproduced when +# smart_holder is used. +# - that the trampoline object remains alive in all situations +# (no use-after-free) as long as the C++ shared_ptr exists. +# +# Where applicable, trampoline state is introspected to confirm whether the +# C++ object retains knowledge of the Python override or has fallen back to +# the base implementation. + +from __future__ import annotations + +import gc +import weakref + +import pytest + +import env +import pybind11_tests.potentially_slicing_weak_ptr as m + + +class PyDrvdSH(m.VirtBaseSH): + def get_code(self): + return 200 + + +class PyDrvdSP(m.VirtBaseSP): + def get_code(self): + return 200 + + +VIRT_BASE_TYPES = { + "SH": {100: m.VirtBaseSH, 200: PyDrvdSH}, + "SP": {100: m.VirtBaseSP, 200: PyDrvdSP}, +} + +RTRN_FUNCS = { + "SH": { + "oc": m.SH_rtrn_obj_cast_shared_ptr, + "ps": m.SH_rtrn_potentially_slicing_shared_ptr, + }, + "SP": { + "oc": m.SP_rtrn_obj_cast_shared_ptr, + "ps": m.SP_rtrn_potentially_slicing_shared_ptr, + }, +} + +SP_OWNER_TYPES = { + "SH": m.SH_SpOwner, + "SP": m.SP_SpOwner, +} + +WP_OWNER_TYPES = { + "SH": m.SH_WpOwner, + "SP": m.SP_WpOwner, +} + +GC_IS_RELIABLE = not (env.PYPY or env.GRAALPY) + + +@pytest.mark.parametrize("expected_code", [100, 200]) +@pytest.mark.parametrize("rtrn_kind", ["oc", "ps"]) +@pytest.mark.parametrize("holder_kind", ["SH", "SP"]) +def test_rtrn_obj_cast_shared_ptr(holder_kind, rtrn_kind, expected_code): + obj = VIRT_BASE_TYPES[holder_kind][expected_code]() + ptr = RTRN_FUNCS[holder_kind][rtrn_kind](obj) + assert ptr.get_code() == expected_code + objref = weakref.ref(obj) + del obj + gc.collect() + assert ptr.get_code() == expected_code # the ptr Python object keeps obj alive + assert objref() is not None + del ptr + gc.collect() + if GC_IS_RELIABLE: + assert objref() is None + + +@pytest.mark.parametrize("expected_code", [100, 200]) +@pytest.mark.parametrize("holder_kind", ["SH", "SP"]) +def test_with_sp_owner(holder_kind, expected_code): + spo = SP_OWNER_TYPES[holder_kind]() + assert spo.get_code() == -888 + assert spo.get_trampoline_state() == "sp nullptr" + + obj = VIRT_BASE_TYPES[holder_kind][expected_code]() + assert obj.get_code() == expected_code + + spo.set_sp(obj) + assert spo.get_code() == expected_code + expected_trampoline_state = ( + "dynamic_cast failed" if expected_code == 100 else "trampoline alive" + ) + assert spo.get_trampoline_state() == expected_trampoline_state + + del obj + gc.collect() + if holder_kind == "SH": + assert spo.get_code() == expected_code + elif GC_IS_RELIABLE: + assert ( + spo.get_code() == 100 + ) # see issue #1333 (inheritance slicing) and PR #5624 + assert spo.get_trampoline_state() == expected_trampoline_state + + +@pytest.mark.parametrize("expected_code", [100, 200]) +@pytest.mark.parametrize("set_meth", ["set_wp", "set_wp_potentially_slicing"]) +@pytest.mark.parametrize("holder_kind", ["SH", "SP"]) +def test_with_wp_owner(holder_kind, set_meth, expected_code): + wpo = WP_OWNER_TYPES[holder_kind]() + assert wpo.get_code() == -999 + assert wpo.get_trampoline_state() == "sp nullptr" + + obj = VIRT_BASE_TYPES[holder_kind][expected_code]() + assert obj.get_code() == expected_code + + getattr(wpo, set_meth)(obj) + if ( + holder_kind == "SP" + or expected_code == 100 + or set_meth == "set_wp_potentially_slicing" + ): + assert wpo.get_code() == expected_code + else: + assert wpo.get_code() == -999 # see issue #5623 (weak_ptr expired) and PR #5624 + if expected_code == 100: + expected_trampoline_state = "dynamic_cast failed" + elif holder_kind == "SH" and set_meth == "set_wp": + expected_trampoline_state = "sp nullptr" + else: + expected_trampoline_state = "trampoline alive" + assert wpo.get_trampoline_state() == expected_trampoline_state + + del obj + gc.collect() + if GC_IS_RELIABLE: + assert wpo.get_code() == -999 + + +def test_potentially_slicing_weak_ptr_not_convertible_error(): + with pytest.raises(Exception) as excinfo: + m.SH_rtrn_potentially_slicing_shared_ptr("") + assert str(excinfo.value) == ( + '"str" object is not convertible to std::weak_ptr' + " (with T = pybind11_tests::potentially_slicing_weak_ptr::VirtBase<0>)" + ) + with pytest.raises(Exception) as excinfo: + m.SP_rtrn_potentially_slicing_shared_ptr([]) + assert str(excinfo.value) == ( + '"list" object is not convertible to std::weak_ptr' + " (with T = pybind11_tests::potentially_slicing_weak_ptr::VirtBase<1>)" + ) diff --git a/external_libraries/pybind11/tests/test_python_multiple_inheritance.cpp b/external_libraries/pybind11/tests/test_python_multiple_inheritance.cpp new file mode 100644 index 00000000..68991715 --- /dev/null +++ b/external_libraries/pybind11/tests/test_python_multiple_inheritance.cpp @@ -0,0 +1,45 @@ +#include "pybind11_tests.h" + +namespace test_python_multiple_inheritance { + +// Copied from: +// https://github.com/google/clif/blob/5718e4d0807fd3b6a8187dde140069120b81ecef/clif/testing/python_multiple_inheritance.h + +struct CppBase { + explicit CppBase(int value) : base_value(value) {} + int get_base_value() const { return base_value; } + void reset_base_value(int new_value) { base_value = new_value; } + +private: + int base_value; +}; + +struct CppDrvd : CppBase { + explicit CppDrvd(int value) : CppBase(value), drvd_value(value * 3) {} + int get_drvd_value() const { return drvd_value; } + void reset_drvd_value(int new_value) { drvd_value = new_value; } + + int get_base_value_from_drvd() const { return get_base_value(); } + void reset_base_value_from_drvd(int new_value) { reset_base_value(new_value); } + +private: + int drvd_value; +}; + +} // namespace test_python_multiple_inheritance + +TEST_SUBMODULE(python_multiple_inheritance, m) { + using namespace test_python_multiple_inheritance; + + py::class_(m, "CppBase") + .def(py::init()) + .def("get_base_value", &CppBase::get_base_value) + .def("reset_base_value", &CppBase::reset_base_value); + + py::class_(m, "CppDrvd") + .def(py::init()) + .def("get_drvd_value", &CppDrvd::get_drvd_value) + .def("reset_drvd_value", &CppDrvd::reset_drvd_value) + .def("get_base_value_from_drvd", &CppDrvd::get_base_value_from_drvd) + .def("reset_base_value_from_drvd", &CppDrvd::reset_base_value_from_drvd); +} diff --git a/external_libraries/pybind11/tests/test_python_multiple_inheritance.py b/external_libraries/pybind11/tests/test_python_multiple_inheritance.py new file mode 100644 index 00000000..12216283 --- /dev/null +++ b/external_libraries/pybind11/tests/test_python_multiple_inheritance.py @@ -0,0 +1,36 @@ +# Adapted from: +# https://github.com/google/clif/blob/5718e4d0807fd3b6a8187dde140069120b81ecef/clif/testing/python/python_multiple_inheritance_test.py +from __future__ import annotations + +from pybind11_tests import python_multiple_inheritance as m + + +class PC(m.CppBase): + pass + + +class PPCC(PC, m.CppDrvd): + pass + + +def test_PC(): + d = PC(11) + assert d.get_base_value() == 11 + d.reset_base_value(13) + assert d.get_base_value() == 13 + + +def test_PPCC(): + d = PPCC(11) + assert d.get_drvd_value() == 33 + d.reset_drvd_value(55) + assert d.get_drvd_value() == 55 + + assert d.get_base_value() == 11 + assert d.get_base_value_from_drvd() == 11 + d.reset_base_value(20) + assert d.get_base_value() == 20 + assert d.get_base_value_from_drvd() == 20 + d.reset_base_value_from_drvd(30) + assert d.get_base_value() == 30 + assert d.get_base_value_from_drvd() == 30 diff --git a/external_libraries/pybind11/tests/test_pytorch_shared_ptr_cast_regression.cpp b/external_libraries/pybind11/tests/test_pytorch_shared_ptr_cast_regression.cpp new file mode 100644 index 00000000..14259622 --- /dev/null +++ b/external_libraries/pybind11/tests/test_pytorch_shared_ptr_cast_regression.cpp @@ -0,0 +1,62 @@ +#include "pybind11_tests.h" + +#include +#include + +#if defined(__clang__) +# if __has_warning("-Wdeprecated-copy-with-user-provided-dtor") +# pragma clang diagnostic error "-Wdeprecated-copy-with-user-provided-dtor" +# endif +# if __has_warning("-Wdeprecated-copy-with-dtor") +# pragma clang diagnostic error "-Wdeprecated-copy-with-dtor" +# endif +#endif + +namespace test_pytorch_regressions { + +// Directly extracted from PyTorch patterns that regressed in CI. +struct TracingState : std::enable_shared_from_this { + TracingState() = default; + ~TracingState() = default; + int value = 0; +}; + +const std::shared_ptr &get_tracing_state() { + static std::shared_ptr state = std::make_shared(); + return state; +} + +struct InterfaceType { + ~InterfaceType() = default; + int value = 0; +}; +using InterfaceTypePtr = std::shared_ptr; + +struct CompilationUnit { + InterfaceTypePtr iface = std::make_shared(); + + InterfaceTypePtr get_interface(const std::string &) const { return iface; } +}; + +} // namespace test_pytorch_regressions + +TEST_SUBMODULE(pybind11_pytorch_regressions, m) { + using namespace test_pytorch_regressions; + + py::class_>(m, "TracingState") + .def(py::init<>()) + .def_readwrite("value", &TracingState::value); + + m.def("_get_tracing_state", []() { return get_tracing_state(); }); + + py::class_(m, "InterfaceType") + .def(py::init<>()) + .def_readwrite("value", &InterfaceType::value); + + py::class_>(m, "CompilationUnit") + .def(py::init<>()) + .def("get_interface", + [](const std::shared_ptr &self, const std::string &name) { + return self->get_interface(name); + }); +} diff --git a/external_libraries/pybind11/tests/test_pytorch_shared_ptr_cast_regression.py b/external_libraries/pybind11/tests/test_pytorch_shared_ptr_cast_regression.py new file mode 100644 index 00000000..b7c393b3 --- /dev/null +++ b/external_libraries/pybind11/tests/test_pytorch_shared_ptr_cast_regression.py @@ -0,0 +1,25 @@ +from __future__ import annotations + +from pybind11_tests import pybind11_pytorch_regressions as m + + +def test_pytorch_like_get_tracing_state_aliases_singleton_shared_ptr(): + a = m._get_tracing_state() + b = m._get_tracing_state() + + a.value = 17 + + assert b.value == 17 + assert m._get_tracing_state().value == 17 + + +def test_pytorch_like_compilation_unit_get_interface_aliases_member_shared_ptr(): + cu = m.CompilationUnit() + + a = cu.get_interface("iface") + b = cu.get_interface("iface") + + a.value = 23 + + assert b.value == 23 + assert cu.get_interface("iface").value == 23 diff --git a/external_libraries/pybind11/tests/test_pytypes.cpp b/external_libraries/pybind11/tests/test_pytypes.cpp new file mode 100644 index 00000000..ff779409 --- /dev/null +++ b/external_libraries/pybind11/tests/test_pytypes.cpp @@ -0,0 +1,1215 @@ +/* + tests/test_pytypes.cpp -- Python type casters + + Copyright (c) 2017 Wenzel Jakob + + All rights reserved. Use of this source code is governed by a + BSD-style license that can be found in the LICENSE file. +*/ + +#include +#include + +#include "pybind11_tests.h" + +#include + +//__has_include has been part of C++17, no need to check it +#if defined(PYBIND11_CPP20) && __has_include() +# if !defined(PYBIND11_COMPILER_CLANG) || __clang_major__ >= 16 // llvm/llvm-project#52696 +# define PYBIND11_TEST_PYTYPES_HAS_RANGES +# include +# endif +#endif + +namespace external { +namespace detail { +bool check(PyObject *o) { return PyFloat_Check(o) != 0; } + +PyObject *conv(PyObject *o) { + PyObject *ret = nullptr; + if (PyLong_Check(o)) { + double v = PyLong_AsDouble(o); + if (!(v == -1.0 && PyErr_Occurred())) { + ret = PyFloat_FromDouble(v); + } + } else { + py::set_error(PyExc_TypeError, "Unexpected type"); + } + return ret; +} + +PyObject *default_constructed() { return PyFloat_FromDouble(0.0); } +} // namespace detail +class float_ : public py::object { + PYBIND11_OBJECT_CVT(float_, py::object, external::detail::check, external::detail::conv) + + float_() : py::object(external::detail::default_constructed(), stolen_t{}) {} + + double get_value() const { return PyFloat_AsDouble(this->ptr()); } +}; +} // namespace external + +namespace pybind11 { +namespace detail { +template <> +struct handle_type_name { + static constexpr auto name = const_name("float"); +}; +} // namespace detail +} // namespace pybind11 + +namespace implicit_conversion_from_0_to_handle { +// Uncomment to trigger compiler error. Note: Before PR #4008 this used to compile successfully. +// void expected_to_trigger_compiler_error() { py::handle(0); } +} // namespace implicit_conversion_from_0_to_handle + +// Used to validate systematically that PR #4008 does/did NOT change the behavior. +void pure_compile_tests_for_handle_from_PyObject_pointers() { + { + PyObject *ptr = Py_None; + py::handle{ptr}; + } + { + PyObject *const ptr = Py_None; + py::handle{ptr}; + } + // Uncomment to trigger compiler errors. + // PyObject const * ptr = Py_None; py::handle{ptr}; + // PyObject const *const ptr = Py_None; py::handle{ptr}; + // PyObject volatile * ptr = Py_None; py::handle{ptr}; + // PyObject volatile *const ptr = Py_None; py::handle{ptr}; + // PyObject const volatile * ptr = Py_None; py::handle{ptr}; + // PyObject const volatile *const ptr = Py_None; py::handle{ptr}; +} + +namespace handle_from_move_only_type_with_operator_PyObject { + +// Reduced from +// https://github.com/pytorch/pytorch/blob/279634f384662b7c3a9f8bf7ccc3a6afd2f05657/torch/csrc/utils/object_ptr.h +struct operator_ncnst { + operator_ncnst() = default; + operator_ncnst(operator_ncnst &&) = default; + operator PyObject *() /* */ { return Py_None; } // NOLINT(google-explicit-constructor) +}; + +struct operator_const { + operator_const() = default; + operator_const(operator_const &&) = default; + operator PyObject *() const { return Py_None; } // NOLINT(google-explicit-constructor) +}; + +bool from_ncnst() { + operator_ncnst obj; + auto h = py::handle(obj); // Critical part of test: does this compile? + return h.ptr() == Py_None; // Just something. +} + +bool from_const() { + operator_const obj; + auto h = py::handle(obj); // Critical part of test: does this compile? + return h.ptr() == Py_None; // Just something. +} + +void m_defs(py::module_ &m) { + m.def("handle_from_move_only_type_with_operator_PyObject_ncnst", from_ncnst); + m.def("handle_from_move_only_type_with_operator_PyObject_const", from_const); +} + +} // namespace handle_from_move_only_type_with_operator_PyObject + +#if defined(PYBIND11_TYPING_H_HAS_STRING_LITERAL) +namespace literals { +enum Color { RED = 0, BLUE = 1 }; + +typedef py::typing::Literal<"26", + "0x1A", + "\"hello world\"", + "b\"hello world\"", + "u\"hello world\"", + "True", + "Color.RED", + "None"> + LiteralFoo; +} // namespace literals +namespace typevar { +typedef py::typing::TypeVar<"T"> TypeVarT; +typedef py::typing::TypeVar<"V"> TypeVarV; +} // namespace typevar +#endif + +// Custom type for testing arg_name/return_name type hints +// RealNumber: +// * in arguments -> float | int +// * in return -> float +// The choice of types is not really useful, but just made different for testing purposes. +// According to `PEP 484 – Type Hints` annotating with `float` also allows `int`, +// so using `float | int` could be replaced by just `float`. + +struct RealNumber { + double value; +}; + +namespace pybind11 { +namespace detail { + +template <> +struct type_caster { + PYBIND11_TYPE_CASTER(RealNumber, io_name("float | int", "float")); + + static handle cast(const RealNumber &number, return_value_policy, handle) { + return py::float_(number.value).release(); + } + + bool load(handle src, bool convert) { + // If we're in no-convert mode, only load if given a float + if (!convert && !py::isinstance(src)) { + return false; + } + if (!py::isinstance(src) && !py::isinstance(src)) { + return false; + } + value.value = src.cast(); + return true; + } +}; + +} // namespace detail +} // namespace pybind11 + +TEST_SUBMODULE(pytypes, m) { + m.def("obj_class_name", [](py::handle obj) { return py::detail::obj_class_name(obj.ptr()); }); + + handle_from_move_only_type_with_operator_PyObject::m_defs(m); + + // test_bool + m.def("get_bool", [] { return py::bool_(false); }); + // test_int + m.def("get_int", [] { return py::int_(0); }); + // test_iterator + m.def("get_iterator", [] { return py::iterator(); }); + // test_iterable + m.def("get_iterable", [] { return py::iterable(); }); + m.def("get_first_item_from_iterable", [](const py::iterable &iter) { + // This tests the postfix increment operator + py::iterator it = iter.begin(); + py::iterator it2 = it++; + return *it2; + }); + m.def("get_second_item_from_iterable", [](const py::iterable &iter) { + // This tests the prefix increment operator + py::iterator it = iter.begin(); + ++it; + return *it; + }); + m.def("get_frozenset_from_iterable", + [](const py::iterable &iter) { return py::frozenset(iter); }); + m.def("get_list_from_iterable", [](const py::iterable &iter) { return py::list(iter); }); + m.def("get_set_from_iterable", [](const py::iterable &iter) { return py::set(iter); }); + m.def("get_tuple_from_iterable", [](const py::iterable &iter) { return py::tuple(iter); }); + // test_float + m.def("get_float", [] { return py::float_(0.0f); }); + m.def("float_roundtrip", [](py::float_ f) { return f; }); + // test_list + m.def("list_no_args", []() { return py::list{}; }); + m.def("list_ssize_t", []() { return py::list{(py::ssize_t) 0}; }); + m.def("list_size_t", []() { return py::list{(py::size_t) 0}; }); + m.def("list_insert_ssize_t", [](py::list *l) { return l->insert((py::ssize_t) 1, 83); }); + m.def("list_insert_size_t", [](py::list *l) { return l->insert((py::size_t) 3, 57); }); + m.def("list_clear", [](py::list *l) { l->clear(); }); + m.def("get_list", []() { + py::list list; + list.append("value"); + py::print("Entry at position 0:", list[0]); + list[0] = py::str("overwritten"); + list.insert(0, "inserted-0"); + list.insert(2, "inserted-2"); + return list; + }); + m.def("print_list", [](const py::list &list) { + int index = 0; + for (auto item : list) { + py::print("list item {}: {}"_s.format(index++, item)); + } + }); + // test_none + m.def("get_none", [] { return py::none(); }); + m.def("print_none", [](const py::none &none) { py::print("none: {}"_s.format(none)); }); + + // test_set, test_frozenset + m.def("get_set", []() { + py::set set; + set.add(py::str("key1")); + set.add("key2"); + set.add(std::string("key3")); + return set; + }); + m.def("get_frozenset", []() { + py::set set; + set.add(py::str("key1")); + set.add("key2"); + set.add(std::string("key3")); + return py::frozenset(set); + }); + m.def("print_anyset", [](const py::anyset &set) { + for (auto item : set) { + py::print("key:", item); + } + }); + m.def("anyset_size", [](const py::anyset &set) { return set.size(); }); + m.def("anyset_empty", [](const py::anyset &set) { return set.empty(); }); + m.def("anyset_contains", + [](const py::anyset &set, const py::object &key) { return set.contains(key); }); + m.def("anyset_contains", + [](const py::anyset &set, const char *key) { return set.contains(key); }); + m.def("set_add", [](py::set &set, const py::object &key) { set.add(key); }); + m.def("set_clear", [](py::set &set) { set.clear(); }); + + // test_dict + m.def("get_dict", []() { return py::dict("key"_a = "value"); }); + m.def("print_dict", [](const py::dict &dict) { + for (auto item : dict) { + py::print("key: {}, value={}"_s.format(item.first, item.second)); + } + }); + m.def("dict_keyword_constructor", []() { + auto d1 = py::dict("x"_a = 1, "y"_a = 2); + auto d2 = py::dict("z"_a = 3, **d1); + return d2; + }); + m.def("dict_contains", + [](const py::dict &dict, const py::object &val) { return dict.contains(val); }); + m.def("dict_contains", + [](const py::dict &dict, const char *val) { return dict.contains(val); }); + + // test_tuple + m.def("tuple_no_args", []() { return py::tuple{}; }); + m.def("tuple_ssize_t", []() { return py::tuple{(py::ssize_t) 0}; }); + m.def("tuple_size_t", []() { return py::tuple{(py::size_t) 0}; }); + m.def("get_tuple", []() { return py::make_tuple(42, py::none(), "spam"); }); + + // test_simple_namespace + m.def("get_simple_namespace", []() { + auto ns = py::module_::import("types").attr("SimpleNamespace")( + "attr"_a = 42, "x"_a = "foo", "wrong"_a = 1); + py::delattr(ns, "wrong"); + py::setattr(ns, "right", py::int_(2)); + return ns; + }); + + // test_str + m.def("str_from_char_ssize_t", []() { return py::str{"red", (py::ssize_t) 3}; }); + m.def("str_from_char_size_t", []() { return py::str{"blue", (py::size_t) 4}; }); + m.def("str_from_string", []() { return py::str(std::string("baz")); }); + m.def("str_from_std_string_input", [](const std::string &stri) { return py::str(stri); }); + m.def("str_from_cstr_input", [](const char *c_str) { return py::str(c_str); }); + m.def("str_from_bytes", []() { return py::str(py::bytes("boo", 3)); }); + m.def("str_from_bytes_input", + [](const py::bytes &encoded_str) { return py::str(encoded_str); }); + + m.def("str_from_object", [](const py::object &obj) { return py::str(obj); }); + m.def("repr_from_object", [](const py::object &obj) { return py::repr(obj); }); + m.def("str_from_handle", [](py::handle h) { return py::str(h); }); + m.def("str_from_string_from_str", + [](const py::str &obj) { return py::str(static_cast(obj)); }); + + m.def("str_format", []() { + auto s1 = "{} + {} = {}"_s.format(1, 2, 3); + auto s2 = "{a} + {b} = {c}"_s.format("a"_a = 1, "b"_a = 2, "c"_a = 3); + return py::make_tuple(s1, s2); + }); + + // test_bytes + m.def("bytes_from_char_ssize_t", []() { return py::bytes{"green", (py::ssize_t) 5}; }); + m.def("bytes_from_char_size_t", []() { return py::bytes{"purple", (py::size_t) 6}; }); + m.def("bytes_from_string", []() { return py::bytes(std::string("foo")); }); + m.def("bytes_from_str", []() { return py::bytes(py::str("bar", 3)); }); + + // test bytearray + m.def("bytearray_from_char_ssize_t", []() { return py::bytearray{"$%", (py::ssize_t) 2}; }); + m.def("bytearray_from_char_size_t", []() { return py::bytearray{"@$!", (py::size_t) 3}; }); + m.def("bytearray_from_string", []() { return py::bytearray(std::string("foo")); }); + m.def("bytearray_size", []() { return py::bytearray("foo").size(); }); + + // test_capsule + m.def("return_capsule_with_destructor", []() { + py::print("creating capsule"); + return py::capsule([]() { py::print("destructing capsule"); }); + }); + + m.def("return_renamed_capsule_with_destructor", []() { + py::print("creating capsule"); + auto cap = py::capsule([]() { py::print("destructing capsule"); }); + static const char *capsule_name = "test_name1"; + py::print("renaming capsule"); + cap.set_name(capsule_name); + return cap; + }); + + m.def("return_capsule_with_destructor_2", []() { + py::print("creating capsule"); + return py::capsule((void *) 1234, [](void *ptr) { + py::print("destructing capsule: {}"_s.format((size_t) ptr)); + }); + }); + + m.def("return_capsule_with_destructor_3", []() { + py::print("creating capsule"); + auto cap = py::capsule((void *) 1233, "oname", [](void *ptr) { + py::print("destructing capsule: {}"_s.format((size_t) ptr)); + }); + py::print("original name: {}"_s.format(cap.name())); + return cap; + }); + + m.def("return_renamed_capsule_with_destructor_2", []() { + py::print("creating capsule"); + auto cap = py::capsule((void *) 1234, [](void *ptr) { + py::print("destructing capsule: {}"_s.format((size_t) ptr)); + }); + static const char *capsule_name = "test_name2"; + py::print("renaming capsule"); + cap.set_name(capsule_name); + return cap; + }); + + m.def("return_capsule_with_name_and_destructor", []() { + auto capsule = py::capsule((void *) 12345, "pointer type description", [](PyObject *ptr) { + if (ptr) { + const auto *name = PyCapsule_GetName(ptr); + py::print("destructing capsule ({}, '{}')"_s.format( + (size_t) PyCapsule_GetPointer(ptr, name), name)); + } + }); + + capsule.set_pointer((void *) 1234); + + // Using get_pointer() + void *contents1 = static_cast(capsule); + void *contents2 = capsule.get_pointer(); + void *contents3 = capsule.get_pointer(); + + auto result1 = reinterpret_cast(contents1); + auto result2 = reinterpret_cast(contents2); + auto result3 = reinterpret_cast(contents3); + + py::print( + "created capsule ({}, '{}')"_s.format(result1 & result2 & result3, capsule.name())); + return capsule; + }); + + m.def("return_capsule_with_explicit_nullptr_dtor", []() { + py::print("creating capsule with explicit nullptr dtor"); + return py::capsule(reinterpret_cast(1234), + static_cast(nullptr)); // PR #4221 + }); + + // test_accessors + m.def("accessor_api", [](const py::object &o) { + auto d = py::dict(); + + d["basic_attr"] = o.attr("basic_attr"); + + auto l = py::list(); + for (auto item : o.attr("begin_end")) { + l.append(item); + } + d["begin_end"] = l; + + d["operator[object]"] = o.attr("d")["operator[object]"_s]; + d["operator[char *]"] = o.attr("d")["operator[char *]"]; + + d["attr(object)"] = o.attr("sub").attr("attr_obj"); + d["attr(char *)"] = o.attr("sub").attr("attr_char"); + try { + o.attr("sub").attr("missing").ptr(); + } catch (const py::error_already_set &) { + d["missing_attr_ptr"] = "raised"_s; + } + try { + o.attr("missing").attr("doesn't matter"); + } catch (const py::error_already_set &) { + d["missing_attr_chain"] = "raised"_s; + } + + d["is_none"] = o.attr("basic_attr").is_none(); + + d["operator()"] = o.attr("func")(1); + d["operator*"] = o.attr("func")(*o.attr("begin_end")); + + // Test implicit conversion + py::list implicit_list = o.attr("begin_end"); + d["implicit_list"] = implicit_list; + py::dict implicit_dict = o.attr("__dict__"); + d["implicit_dict"] = implicit_dict; + + return d; + }); + + m.def("tuple_accessor", [](const py::tuple &existing_t) { + try { + existing_t[0] = 1; + } catch (const py::error_already_set &) { + // --> Python system error + // Only new tuples (refcount == 1) are mutable + auto new_t = py::tuple(3); + for (size_t i = 0; i < new_t.size(); ++i) { + new_t[i] = i; + } + return new_t; + } + return py::tuple(); + }); + + m.def("accessor_assignment", []() { + auto l = py::list(1); + l[0] = 0; + + auto d = py::dict(); + d["get"] = l[0]; + auto var = l[0]; + d["deferred_get"] = var; + l[0] = 1; + d["set"] = l[0]; + var = 99; // this assignment should not overwrite l[0] + d["deferred_set"] = l[0]; + d["var"] = var; + + return d; + }); + + m.def("accessor_moves", []() { // See PR #3970 + py::list return_list; +#ifdef PYBIND11_HANDLE_REF_DEBUG + py::int_ py_int_0(0); + py::int_ py_int_42(42); + py::str py_str_count("count"); + + auto tup = py::make_tuple(0); + + py::sequence seq(tup); + + py::list lst; + lst.append(0); + +# define PYBIND11_LOCAL_DEF(...) \ + { \ + std::size_t inc_refs = py::handle::inc_ref_counter(); \ + __VA_ARGS__; \ + inc_refs = py::handle::inc_ref_counter() - inc_refs; \ + return_list.append(inc_refs); \ + } + + PYBIND11_LOCAL_DEF(tup[py_int_0]) // l-value (to have a control) + PYBIND11_LOCAL_DEF(tup[py::int_(0)]) // r-value + + PYBIND11_LOCAL_DEF(tup.attr(py_str_count)) // l-value + PYBIND11_LOCAL_DEF(tup.attr(py::str("count"))) // r-value + + PYBIND11_LOCAL_DEF(seq[py_int_0]) // l-value + PYBIND11_LOCAL_DEF(seq[py::int_(0)]) // r-value + + PYBIND11_LOCAL_DEF(seq.attr(py_str_count)) // l-value + PYBIND11_LOCAL_DEF(seq.attr(py::str("count"))) // r-value + + PYBIND11_LOCAL_DEF(lst[py_int_0]) // l-value + PYBIND11_LOCAL_DEF(lst[py::int_(0)]) // r-value + + PYBIND11_LOCAL_DEF(lst.attr(py_str_count)) // l-value + PYBIND11_LOCAL_DEF(lst.attr(py::str("count"))) // r-value + + auto lst_acc = lst[py::int_(0)]; + lst_acc = py::int_(42); // Detaches lst_acc from lst. + PYBIND11_LOCAL_DEF(lst_acc = py_int_42) // l-value + PYBIND11_LOCAL_DEF(lst_acc = py::int_(42)) // r-value +# undef PYBIND11_LOCAL_DEF +#endif + return return_list; + }); + + // test_constructors + m.def("default_constructors", []() { + return py::dict("bytes"_a = py::bytes(), + "bytearray"_a = py::bytearray(), + "str"_a = py::str(), + "bool"_a = py::bool_(), + "int"_a = py::int_(), + "float"_a = py::float_(), + "tuple"_a = py::tuple(), + "list"_a = py::list(), + "dict"_a = py::dict(), + "set"_a = py::set()); + }); + + m.def("converting_constructors", [](const py::dict &d) { + return py::dict("bytes"_a = py::bytes(d["bytes"]), + "bytearray"_a = py::bytearray(d["bytearray"]), + "str"_a = py::str(d["str"]), + "bool"_a = py::bool_(d["bool"]), + "int"_a = py::int_(d["int"]), + "float"_a = py::float_(d["float"]), + "tuple"_a = py::tuple(d["tuple"]), + "list"_a = py::list(d["list"]), + "dict"_a = py::dict(d["dict"]), + "set"_a = py::set(d["set"]), + "frozenset"_a = py::frozenset(d["frozenset"]), + "memoryview"_a = py::memoryview(d["memoryview"])); + }); + + m.def("cast_functions", [](const py::dict &d) { + // When converting between Python types, obj.cast() should be the same as T(obj) + return py::dict("bytes"_a = d["bytes"].cast(), + "bytearray"_a = d["bytearray"].cast(), + "str"_a = d["str"].cast(), + "bool"_a = d["bool"].cast(), + "int"_a = d["int"].cast(), + "float"_a = d["float"].cast(), + "tuple"_a = d["tuple"].cast(), + "list"_a = d["list"].cast(), + "dict"_a = d["dict"].cast(), + "set"_a = d["set"].cast(), + "frozenset"_a = d["frozenset"].cast(), + "memoryview"_a = d["memoryview"].cast()); + }); + + m.def("convert_to_pybind11_str", [](const py::object &o) { return py::str(o); }); + + m.def("nonconverting_constructor", + [](const std::string &type, py::object value, bool move) -> py::object { + if (type == "bytes") { + return move ? py::bytes(std::move(value)) : py::bytes(value); + } + if (type == "none") { + return move ? py::none(std::move(value)) : py::none(value); + } + if (type == "ellipsis") { + return move ? py::ellipsis(std::move(value)) : py::ellipsis(value); + } + if (type == "type") { + return move ? py::type(std::move(value)) : py::type(value); + } + throw std::runtime_error("Invalid type"); + }); + + m.def("get_implicit_casting", []() { + py::dict d; + d["char*_i1"] = "abc"; + const char *c2 = "abc"; + d["char*_i2"] = c2; + d["char*_e"] = py::cast(c2); + d["char*_p"] = py::str(c2); + + d["int_i1"] = 42; + int i = 42; + d["int_i2"] = i; + i++; + d["int_e"] = py::cast(i); + i++; + d["int_p"] = py::int_(i); + + d["str_i1"] = std::string("str"); + std::string s2("str1"); + d["str_i2"] = s2; + s2[3] = '2'; + d["str_e"] = py::cast(s2); + s2[3] = '3'; + d["str_p"] = py::str(s2); + + py::list l(2); + l[0] = 3; + l[1] = py::cast(6); + l.append(9); + l.append(py::cast(12)); + l.append(py::int_(15)); + + return py::dict("d"_a = d, "l"_a = l); + }); + + // test_print + m.def("print_function", []() { + py::print("Hello, World!"); + py::print(1, 2.0, "three", true, std::string("-- multiple args")); + auto args = py::make_tuple("and", "a", "custom", "separator"); + py::print("*args", *args, "sep"_a = "-"); + py::print("no new line here", "end"_a = " -- "); + py::print("next print"); + + auto py_stderr = py::module_::import("sys").attr("stderr"); + py::print("this goes to stderr", "file"_a = py_stderr); + + py::print("flush", "flush"_a = true); + + py::print( + "{a} + {b} = {c}"_s.format("a"_a = "py::print", "b"_a = "str.format", "c"_a = "this")); + }); + + m.def("print_failure", []() { py::print(42, UnregisteredType()); }); + + m.def("hash_function", [](py::object obj) { return py::hash(std::move(obj)); }); + + m.def("obj_contains", + [](py::object &obj, const py::object &key) { return obj.contains(key); }); + + m.def("test_number_protocol", [](const py::object &a, const py::object &b) { + py::list l; + l.append(a.equal(b)); + l.append(a.not_equal(b)); + l.append(a < b); + l.append(a <= b); + l.append(a > b); + l.append(a >= b); + l.append(a + b); + l.append(a - b); + l.append(a * b); + l.append(a / b); + l.append(a | b); + l.append(a & b); + l.append(a ^ b); + l.append(a >> b); + l.append(a << b); + return l; + }); + + m.def("test_list_slicing", [](const py::list &a) { return a[py::slice(0, -1, 2)]; }); + + m.def("test_list_slicing_default", [](const py::list &a) { return a[py::slice()]; }); + + // See #2361 + m.def("issue2361_str_implicit_copy_none", []() { + py::str is_this_none = py::none(); + return is_this_none; + }); + m.def("issue2361_dict_implicit_copy_none", []() { + py::dict is_this_none = py::none(); + return is_this_none; + }); + + m.def("test_memoryview_object", [](const py::buffer &b) { return py::memoryview(b); }); + + m.def("test_memoryview_buffer_info", + [](const py::buffer &b) { return py::memoryview(b.request()); }); + + m.def("test_memoryview_from_buffer", [](bool is_unsigned) { + static const int16_t si16[] = {3, 1, 4, 1, 5}; + static const uint16_t ui16[] = {2, 7, 1, 8}; + if (is_unsigned) { + return py::memoryview::from_buffer(ui16, {4}, {sizeof(uint16_t)}); + } + return py::memoryview::from_buffer(si16, {5}, {sizeof(int16_t)}); + }); + + m.def("test_memoryview_from_buffer_nativeformat", []() { + static const char *format = "@i"; + static const int32_t arr[] = {4, 7, 5}; + return py::memoryview::from_buffer(arr, sizeof(int32_t), format, {3}, {sizeof(int32_t)}); + }); + + m.def("test_memoryview_from_buffer_empty_shape", []() { + static const char *buf = ""; + return py::memoryview::from_buffer(buf, 1, "B", {}, {}); + }); + + m.def("test_memoryview_from_buffer_invalid_strides", []() { + static const char *buf = "\x02\x03\x04"; + return py::memoryview::from_buffer(buf, 1, "B", {3}, {}); + }); + + m.def("test_memoryview_from_buffer_nullptr", []() { + return py::memoryview::from_buffer(static_cast(nullptr), 1, "B", {}, {}); + }); + + m.def("test_memoryview_from_memory", []() { + const char *buf = "\xff\xe1\xab\x37"; + return py::memoryview::from_memory(buf, static_cast(strlen(buf))); + }); + + // test_builtin_functions + m.def("get_len", [](py::handle h) { return py::len(h); }); + +#ifdef PYBIND11_STR_LEGACY_PERMISSIVE + m.attr("PYBIND11_STR_LEGACY_PERMISSIVE") = true; +#endif + + m.def("isinstance_pybind11_bytes", + [](py::object o) { return py::isinstance(std::move(o)); }); + m.def("isinstance_pybind11_str", + [](py::object o) { return py::isinstance(std::move(o)); }); + + m.def("pass_to_pybind11_bytes", [](py::bytes b) { return py::len(std::move(b)); }); + m.def("pass_to_pybind11_str", [](py::str s) { return py::len(std::move(s)); }); + m.def("pass_to_std_string", [](const std::string &s) { return s.size(); }); + + // test_weakref + m.def("weakref_from_handle", [](py::handle h) { return py::weakref(h); }); + m.def("weakref_from_handle_and_function", + [](py::handle h, py::function f) { return py::weakref(h, std::move(f)); }); + m.def("weakref_from_object", [](const py::object &o) { return py::weakref(o); }); + m.def("weakref_from_object_and_function", + [](py::object o, py::function f) { return py::weakref(std::move(o), std::move(f)); }); + +// See PR #3263 for background (https://github.com/pybind/pybind11/pull/3263): +// pytypes.h could be changed to enforce the "most correct" user code below, by removing +// `const` from iterator `reference` using type aliases, but that will break existing +// user code. +#if (defined(__APPLE__) && defined(__clang__)) || defined(PYPY_VERSION) +// This is "most correct" and enforced on these platforms. +# define PYBIND11_AUTO_IT auto it +#else + // This works on many platforms and is (unfortunately) reflective of existing user code. + // NOLINTNEXTLINE(bugprone-macro-parentheses) +# define PYBIND11_AUTO_IT auto &it +#endif + + m.def("tuple_iterator", []() { + auto tup = py::make_tuple(5, 7); + int tup_sum = 0; + for (PYBIND11_AUTO_IT : tup) { + tup_sum += it.cast(); + } + return tup_sum; + }); + + m.def("dict_iterator", []() { + py::dict dct; + dct[py::int_(3)] = 5; + dct[py::int_(7)] = 11; + int kv_sum = 0; + for (PYBIND11_AUTO_IT : dct) { + kv_sum += it.first.cast() * 100 + it.second.cast(); + } + return kv_sum; + }); + + m.def("passed_iterator", [](const py::iterator &py_it) { + int elem_sum = 0; + for (PYBIND11_AUTO_IT : py_it) { + elem_sum += it.cast(); + } + return elem_sum; + }); + +#undef PYBIND11_AUTO_IT + + // Tests below this line are for pybind11 IMPLEMENTATION DETAILS: + + m.def("sequence_item_get_ssize_t", [](const py::object &o) { + return py::detail::accessor_policies::sequence_item::get(o, (py::ssize_t) 1); + }); + m.def("sequence_item_set_ssize_t", [](const py::object &o) { + auto s = py::str{"peppa", 5}; + py::detail::accessor_policies::sequence_item::set(o, (py::ssize_t) 1, s); + }); + m.def("sequence_item_get_size_t", [](const py::object &o) { + return py::detail::accessor_policies::sequence_item::get(o, (py::size_t) 2); + }); + m.def("sequence_item_set_size_t", [](const py::object &o) { + auto s = py::str{"george", 6}; + py::detail::accessor_policies::sequence_item::set(o, (py::size_t) 2, s); + }); + m.def("list_item_get_ssize_t", [](const py::object &o) { + return py::detail::accessor_policies::list_item::get(o, (py::ssize_t) 3); + }); + m.def("list_item_set_ssize_t", [](const py::object &o) { + auto s = py::str{"rebecca", 7}; + py::detail::accessor_policies::list_item::set(o, (py::ssize_t) 3, s); + }); + m.def("list_item_get_size_t", [](const py::object &o) { + return py::detail::accessor_policies::list_item::get(o, (py::size_t) 4); + }); + m.def("list_item_set_size_t", [](const py::object &o) { + auto s = py::str{"richard", 7}; + py::detail::accessor_policies::list_item::set(o, (py::size_t) 4, s); + }); + m.def("tuple_item_get_ssize_t", [](const py::object &o) { + return py::detail::accessor_policies::tuple_item::get(o, (py::ssize_t) 5); + }); + m.def("tuple_item_set_ssize_t", []() { + auto s0 = py::str{"emely", 5}; + auto s1 = py::str{"edmond", 6}; + auto o = py::tuple{2}; + py::detail::accessor_policies::tuple_item::set(o, (py::ssize_t) 0, s0); + py::detail::accessor_policies::tuple_item::set(o, (py::ssize_t) 1, s1); + return o; + }); + m.def("tuple_item_get_size_t", [](const py::object &o) { + return py::detail::accessor_policies::tuple_item::get(o, (py::size_t) 6); + }); + m.def("tuple_item_set_size_t", []() { + auto s0 = py::str{"candy", 5}; + auto s1 = py::str{"cat", 3}; + auto o = py::tuple{2}; + py::detail::accessor_policies::tuple_item::set(o, (py::size_t) 1, s1); + py::detail::accessor_policies::tuple_item::set(o, (py::size_t) 0, s0); + return o; + }); + + m.def("square_float_", [](const external::float_ &x) -> double { + double v = x.get_value(); + return v * v; + }); + + m.def("tuple_rvalue_getter", [](const py::tuple &tup) { + // tests accessing tuple object with rvalue int + for (size_t i = 0; i < tup.size(); i++) { + auto o = py::handle(tup[py::int_(i)]); + if (!o) { + throw py::value_error("tuple is malformed"); + } + } + return tup; + }); + m.def("list_rvalue_getter", [](const py::list &l) { + // tests accessing list with rvalue int + for (size_t i = 0; i < l.size(); i++) { + auto o = py::handle(l[py::int_(i)]); + if (!o) { + throw py::value_error("list is malformed"); + } + } + return l; + }); + m.def("populate_dict_rvalue", [](int population) { + auto d = py::dict(); + for (int i = 0; i < population; i++) { + d[py::int_(i)] = py::int_(i); + } + return d; + }); + m.def("populate_obj_str_attrs", [](py::object &o, int population) { + for (int i = 0; i < population; i++) { + o.attr(py::str(py::int_(i))) = py::str(py::int_(i)); + } + return o; + }); + + // testing immutable object augmented assignment: #issue 3812 + m.def("inplace_append", [](py::object &a, const py::object &b) { + a += b; + return a; + }); + m.def("inplace_subtract", [](py::object &a, const py::object &b) { + a -= b; + return a; + }); + m.def("inplace_multiply", [](py::object &a, const py::object &b) { + a *= b; + return a; + }); + m.def("inplace_divide", [](py::object &a, const py::object &b) { + a /= b; + return a; + }); + m.def("inplace_or", [](py::object &a, const py::object &b) { + a |= b; + return a; + }); + m.def("inplace_and", [](py::object &a, const py::object &b) { + a &= b; + return a; + }); + m.def("inplace_lshift", [](py::object &a, const py::object &b) { + a <<= b; + return a; + }); + m.def("inplace_rshift", [](py::object &a, const py::object &b) { + a >>= b; + return a; + }); + + m.def("annotate_tuple_float_str", [](const py::typing::Tuple &) {}); + m.def("annotate_tuple_empty", [](const py::typing::Tuple<> &) {}); + m.def("annotate_tuple_variable_length", + [](const py::typing::Tuple &) {}); + m.def("annotate_dict_str_int", [](const py::typing::Dict &) {}); + m.def("annotate_list_int", [](const py::typing::List &) {}); + m.def("annotate_set_str", [](const py::typing::Set &) {}); + m.def("annotate_iterable_str", [](const py::typing::Iterable &) {}); + m.def("annotate_iterator_int", [](const py::typing::Iterator &) {}); + m.def("annotate_fn", + [](const py::typing::Callable, py::str)> &) {}); + + m.def("annotate_fn_only_return", [](const py::typing::Callable &) {}); + m.def("annotate_type", [](const py::typing::Type &t) -> py::type { return t; }); + + m.def("annotate_union", + [](py::typing::List> l, + py::str a, + py::int_ b, + py::object c) -> py::typing::List> { + l.append(a); + l.append(b); + l.append(c); + return l; + }); + + m.def("union_typing_only", + [](py::typing::List> &l) + -> py::typing::List> { return l; }); + + m.def("annotate_union_to_object", + [](py::typing::Union &o) -> py::object { return o; }); + + m.def("annotate_optional", + [](py::list &list) -> py::typing::List> { + list.append(py::str("hi")); + list.append(py::none()); + return list; + }); + + m.def("annotate_type_guard", [](py::object &o) -> py::typing::TypeGuard { + return py::isinstance(o); + }); + m.def("annotate_type_is", + [](py::object &o) -> py::typing::TypeIs { return py::isinstance(o); }); + + m.def("annotate_no_return", []() -> py::typing::NoReturn { throw 0; }); + m.def("annotate_never", []() -> py::typing::Never { throw 0; }); + + m.def("annotate_optional_to_object", + [](py::typing::Optional &o) -> py::object { return o; }); + +#if defined(PYBIND11_TYPING_H_HAS_STRING_LITERAL) + py::enum_(m, "Color") + .value("RED", literals::Color::RED) + .value("BLUE", literals::Color::BLUE); + + m.def("annotate_literal", [](literals::LiteralFoo &o) -> py::object { return o; }); + // Literal with `@`, `%`, `{`, `}`, and `->` + m.def("identity_literal_exclamation", [](const py::typing::Literal<"\"!\""> &x) { return x; }); + m.def("identity_literal_at", [](const py::typing::Literal<"\"@\""> &x) { return x; }); + m.def("identity_literal_percent", [](const py::typing::Literal<"\"%\""> &x) { return x; }); + m.def("identity_literal_curly_open", [](const py::typing::Literal<"\"{\""> &x) { return x; }); + m.def("identity_literal_curly_close", [](const py::typing::Literal<"\"}\""> &x) { return x; }); + m.def("identity_literal_arrow_with_io_name", + [](const py::typing::Literal<"\"->\""> &x, const RealNumber &) { return x; }); + m.def("identity_literal_arrow_with_callable", + [](const py::typing::Callable\""> &, + const RealNumber &)> &x) { return x; }); + m.def("identity_literal_all_special_chars", + [](const py::typing::Literal<"\"!@!!->{%}\""> &x) { return x; }); + m.def("annotate_generic_containers", + [](const py::typing::List &l) -> py::typing::List { + return l; + }); + + m.def("annotate_listT_to_T", + [](const py::typing::List &l) -> typevar::TypeVarT { return l[0]; }); + m.def("annotate_object_to_T", [](const py::object &o) -> typevar::TypeVarT { return o; }); + m.attr("defined_PYBIND11_TYPING_H_HAS_STRING_LITERAL") = true; +#else + m.attr("defined_PYBIND11_TYPING_H_HAS_STRING_LITERAL") = false; +#endif + +#if defined(PYBIND11_TEST_PYTYPES_HAS_RANGES) + + // test_tuple_ranges + m.def("tuple_iterator_default_initialization", []() { + using TupleIterator = decltype(std::declval().begin()); + static_assert(std::random_access_iterator); + return TupleIterator{} == TupleIterator{}; + }); + + m.def("transform_tuple_plus_one", [](py::tuple &tpl) { + py::list ret{}; + for (auto &&it : + tpl | std::views::transform([](const auto &o) { return py::cast(o) + 1; })) { + ret.append(py::int_(it)); + } + return ret; + }); + + // test_list_ranges + m.def("list_iterator_default_initialization", []() { + using ListIterator = decltype(std::declval().begin()); + static_assert(std::random_access_iterator); + return ListIterator{} == ListIterator{}; + }); + + m.def("transform_list_plus_one", [](py::list &lst) { + py::list ret{}; + for (auto &&it : + lst | std::views::transform([](const auto &o) { return py::cast(o) + 1; })) { + ret.append(py::int_(it)); + } + return ret; + }); + + // test_dict_ranges + m.def("dict_iterator_default_initialization", []() { + using DictIterator = decltype(std::declval().begin()); + static_assert(std::forward_iterator); + return DictIterator{} == DictIterator{}; + }); + + m.def("transform_dict_plus_one", [](py::dict &dct) { + py::list ret{}; + for (auto it : dct | std::views::transform([](auto &o) { + return std::pair{py::cast(o.first) + 1, + py::cast(o.second) + 1}; + })) { + ret.append(py::make_tuple(py::int_(it.first), py::int_(it.second))); + } + return ret; + }); + + m.attr("defined_PYBIND11_TEST_PYTYPES_HAS_RANGES") = true; +#else + m.attr("defined_PYBIND11_TEST_PYTYPES_HAS_RANGES") = false; +#endif + +#if defined(__cpp_inline_variables) + // Exercises const char* overload: + m.attr_with_type_hint>("list_int") = py::list(); + // Exercises py::handle overload: + m.attr_with_type_hint>(py::str("set_str")) = py::set(); + + struct foo_t {}; + struct foo2 {}; + struct foo3 {}; + + pybind11::class_(m, "foo"); + pybind11::class_(m, "foo2"); + pybind11::class_(m, "foo3"); + m.attr_with_type_hint("foo") = foo_t{}; + + m.attr_with_type_hint>("foo_union") = foo_t{}; + + // Include to ensure this does not crash + struct foo4 {}; + m.attr_with_type_hint("foo4") = 3; + + struct Empty {}; + py::class_(m, "EmptyAnnotationClass"); + + struct Static {}; + auto static_class = py::class_(m, "Static"); + static_class.def(py::init()); + static_class.attr_with_type_hint>("x") = 1.0; + static_class.attr_with_type_hint>>( + "dict_str_int") = py::dict(); + + struct Instance {}; + auto instance = py::class_(m, "Instance", py::dynamic_attr()); + instance.def(py::init()); + instance.attr_with_type_hint("y"); + + m.def("attr_with_type_hint_float_x", + [](py::handle obj) { obj.attr_with_type_hint("x"); }); + + m.attr_with_type_hint>("CONST_INT") = 3; + + m.attr("defined___cpp_inline_variables") = true; +#else + m.attr("defined___cpp_inline_variables") = false; +#endif + m.def("half_of_number", [](const RealNumber &x) { return RealNumber{x.value / 2}; }); + m.def( + "half_of_number_convert", + [](const RealNumber &x) { return RealNumber{x.value / 2}; }, + py::arg("x")); + m.def( + "half_of_number_noconvert", + [](const RealNumber &x) { return RealNumber{x.value / 2}; }, + py::arg("x").noconvert()); + // std::vector + m.def("half_of_number_vector", [](const std::vector &x) { + std::vector result; + result.reserve(x.size()); + for (auto num : x) { + result.push_back(RealNumber{num.value / 2}); + } + return result; + }); + // Tuple + m.def("half_of_number_tuple", [](const py::typing::Tuple &x) { + py::typing::Tuple result + = py::make_tuple(RealNumber{x[0].cast().value / 2}, + RealNumber{x[1].cast().value / 2}); + return result; + }); + // Tuple + m.def("half_of_number_tuple_ellipsis", + [](const py::typing::Tuple &x) { + py::typing::Tuple result(x.size()); + for (size_t i = 0; i < x.size(); ++i) { + result[i] = x[i].cast().value / 2; + } + return result; + }); + // Dict + m.def("half_of_number_dict", [](const py::typing::Dict &x) { + py::typing::Dict result; + for (auto it : x) { + result[it.first] = RealNumber{it.second.cast().value / 2}; + } + return result; + }); + // List + m.def("half_of_number_list", [](const py::typing::List &x) { + py::typing::List result; + for (auto num : x) { + result.append(RealNumber{num.cast().value / 2}); + } + return result; + }); + // List> + m.def("half_of_number_nested_list", + [](const py::typing::List> &x) { + py::typing::List> result_lists; + for (auto nums : x) { + py::typing::List result; + for (auto num : nums) { + result.append(RealNumber{num.cast().value / 2}); + } + result_lists.append(result); + } + return result_lists; + }); + // Set + m.def("identity_set", [](const py::typing::Set &x) { return x; }); + // Iterable + m.def("identity_iterable", [](const py::typing::Iterable &x) { return x; }); + // Iterator + m.def("identity_iterator", [](const py::typing::Iterator &x) { return x; }); + // Callable identity + m.def("identity_callable", + [](const py::typing::Callable &x) { return x; }); + // Callable identity + m.def("identity_callable_ellipsis", + [](const py::typing::Callable &x) { return x; }); + // Nested Callable identity + m.def("identity_nested_callable", + [](const py::typing::Callable( + py::typing::Callable)> &x) { return x; }); + // Callable + m.def("apply_callable", + [](const RealNumber &x, const py::typing::Callable &f) { + return f(x).cast(); + }); + // Callable + m.def("apply_callable_ellipsis", + [](const RealNumber &x, const py::typing::Callable &f) { + return f(x).cast(); + }); + // Union + m.def("identity_union", [](const py::typing::Union &x) { return x; }); + // Optional + m.def("identity_optional", [](const py::typing::Optional &x) { return x; }); + // TypeGuard + m.def("check_type_guard", + [](const py::typing::List &x) + -> py::typing::TypeGuard> { + for (const auto &item : x) { + if (!py::isinstance(item)) { + return false; + } + } + return true; + }); + // TypeIs + m.def("check_type_is", [](const py::object &x) -> py::typing::TypeIs { + return py::isinstance(x); + }); + + m.def("const_kwargs_ref_to_str", [](const py::kwargs &kwargs) { return py::str(kwargs); }); +} diff --git a/external_libraries/pybind11/tests/test_pytypes.py b/external_libraries/pybind11/tests/test_pytypes.py new file mode 100644 index 00000000..580371f0 --- /dev/null +++ b/external_libraries/pybind11/tests/test_pytypes.py @@ -0,0 +1,1374 @@ +from __future__ import annotations + +import contextlib +import sys +import types + +import pytest + +import env +from pybind11_tests import detailed_error_messages_enabled +from pybind11_tests import pytypes as m + + +def test_obj_class_name(): + assert m.obj_class_name(None) == "NoneType" + assert m.obj_class_name(list) == "list" + assert m.obj_class_name([]) == "list" + + +def test_handle_from_move_only_type_with_operator_PyObject(): + assert m.handle_from_move_only_type_with_operator_PyObject_ncnst() + assert m.handle_from_move_only_type_with_operator_PyObject_const() + + +def test_bool(doc): + assert doc(m.get_bool) == "get_bool() -> bool" + + +def test_int(doc): + assert doc(m.get_int) == "get_int() -> int" + + +def test_iterator(doc): + assert doc(m.get_iterator) == "get_iterator() -> collections.abc.Iterator" + + +@pytest.mark.parametrize( + ("pytype", "from_iter_func"), + [ + (frozenset, m.get_frozenset_from_iterable), + (list, m.get_list_from_iterable), + (set, m.get_set_from_iterable), + (tuple, m.get_tuple_from_iterable), + ], +) +def test_from_iterable(pytype, from_iter_func): + my_iter = iter(range(10)) + s = from_iter_func(my_iter) + assert type(s) == pytype + assert s == pytype(range(10)) + + +def test_iterable(doc): + assert doc(m.get_iterable) == "get_iterable() -> collections.abc.Iterable" + lst = [1, 2, 3] + i = m.get_first_item_from_iterable(lst) + assert i == 1 + i = m.get_second_item_from_iterable(lst) + assert i == 2 + + +def test_float(doc): + assert doc(m.get_float) == "get_float() -> float" + assert doc(m.float_roundtrip) == "float_roundtrip(arg0: float) -> float" + f1 = m.float_roundtrip(5.5) + assert isinstance(f1, float) + assert f1 == 5.5 + f2 = m.float_roundtrip(5) + assert isinstance(f2, float) + assert f2 == 5.0 + + +def test_list(capture, doc): + assert m.list_no_args() == [] + assert m.list_ssize_t() == [] + assert m.list_size_t() == [] + lst = [1, 2] + m.list_insert_ssize_t(lst) + assert lst == [1, 83, 2] + m.list_insert_size_t(lst) + assert lst == [1, 83, 2, 57] + m.list_clear(lst) + assert lst == [] + + with capture: + lst = m.get_list() + assert lst == ["inserted-0", "overwritten", "inserted-2"] + + lst.append("value2") + m.print_list(lst) + assert ( + capture.unordered + == """ + Entry at position 0: value + list item 0: inserted-0 + list item 1: overwritten + list item 2: inserted-2 + list item 3: value2 + """ + ) + + assert doc(m.get_list) == "get_list() -> list" + assert doc(m.print_list) == "print_list(arg0: list) -> None" + + +def test_none(doc): + assert doc(m.get_none) == "get_none() -> None" + assert doc(m.print_none) == "print_none(arg0: None) -> None" + + +def test_set(capture, doc): + s = m.get_set() + assert isinstance(s, set) + assert s == {"key1", "key2", "key3"} + + s.add("key4") + with capture: + m.print_anyset(s) + assert ( + capture.unordered + == """ + key: key1 + key: key2 + key: key3 + key: key4 + """ + ) + + m.set_add(s, "key5") + assert m.anyset_size(s) == 5 + + m.set_clear(s) + assert m.anyset_empty(s) + + assert not m.anyset_contains(set(), 42) + assert m.anyset_contains({42}, 42) + assert m.anyset_contains({"foo"}, "foo") + + assert doc(m.get_set) == "get_set() -> set" + assert doc(m.print_anyset) == "print_anyset(arg0: set | frozenset) -> None" + + +def test_frozenset(capture, doc): + s = m.get_frozenset() + assert isinstance(s, frozenset) + assert s == frozenset({"key1", "key2", "key3"}) + + with capture: + m.print_anyset(s) + assert ( + capture.unordered + == """ + key: key1 + key: key2 + key: key3 + """ + ) + assert m.anyset_size(s) == 3 + assert not m.anyset_empty(s) + + assert not m.anyset_contains(frozenset(), 42) + assert m.anyset_contains(frozenset({42}), 42) + assert m.anyset_contains(frozenset({"foo"}), "foo") + + assert doc(m.get_frozenset) == "get_frozenset() -> frozenset" + + +def test_dict(capture, doc): + d = m.get_dict() + assert d == {"key": "value"} + + with capture: + d["key2"] = "value2" + m.print_dict(d) + assert ( + capture.unordered + == """ + key: key, value=value + key: key2, value=value2 + """ + ) + + assert not m.dict_contains({}, 42) + assert m.dict_contains({42: None}, 42) + assert m.dict_contains({"foo": None}, "foo") + + assert doc(m.get_dict) == "get_dict() -> dict" + assert doc(m.print_dict) == "print_dict(arg0: dict) -> None" + + assert m.dict_keyword_constructor() == {"x": 1, "y": 2, "z": 3} + + +class CustomContains: + d = {"key": None} + + def __contains__(self, m): + return m in self.d + + +@pytest.mark.parametrize( + ("arg", "func"), + [ + (set(), m.anyset_contains), + ({}, m.dict_contains), + (CustomContains(), m.obj_contains), + ], +) +def test_unhashable_exceptions(arg, func): + class Unhashable: + __hash__ = None + + with pytest.raises(TypeError) as exc_info: + func(arg, Unhashable()) + assert "unhashable type:" in str(exc_info.value) + + +def test_tuple(): + assert m.tuple_no_args() == () + assert m.tuple_ssize_t() == () + assert m.tuple_size_t() == () + assert m.get_tuple() == (42, None, "spam") + + +def test_simple_namespace(): + ns = m.get_simple_namespace() + assert ns.attr == 42 + assert ns.x == "foo" + assert ns.right == 2 + assert not hasattr(ns, "wrong") + + +def test_str(doc): + assert m.str_from_char_ssize_t().encode().decode() == "red" + assert m.str_from_char_size_t().encode().decode() == "blue" + assert m.str_from_string().encode().decode() == "baz" + assert m.str_from_bytes().encode().decode() == "boo" + + assert doc(m.str_from_bytes) == "str_from_bytes() -> str" + + class A: + def __str__(self): + return "this is a str" + + def __repr__(self): + return "this is a repr" + + assert m.str_from_object(A()) == "this is a str" + assert m.repr_from_object(A()) == "this is a repr" + assert m.str_from_handle(A()) == "this is a str" + + s1, s2 = m.str_format() + assert s1 == "1 + 2 = 3" + assert s1 == s2 + + malformed_utf8 = b"\x80" + if hasattr(m, "PYBIND11_STR_LEGACY_PERMISSIVE"): + assert m.str_from_object(malformed_utf8) is malformed_utf8 + else: + assert m.str_from_object(malformed_utf8) == "b'\\x80'" + assert m.str_from_handle(malformed_utf8) == "b'\\x80'" + + assert m.str_from_string_from_str("this is a str") == "this is a str" + ucs_surrogates_str = "\udcc3" + with pytest.raises(UnicodeEncodeError): + m.str_from_string_from_str(ucs_surrogates_str) + + +@pytest.mark.parametrize( + "func", + [ + m.str_from_bytes_input, + m.str_from_cstr_input, + m.str_from_std_string_input, + ], +) +@pytest.mark.xfail("env.GRAALPY", reason="TODO should be fixed on GraalPy side") +def test_surrogate_pairs_unicode_error(func): + input_str = "\ud83d\ude4f".encode("utf-8", "surrogatepass") + with pytest.raises(UnicodeDecodeError): + func(input_str) + + +def test_bytes(doc): + assert m.bytes_from_char_ssize_t().decode() == "green" + assert m.bytes_from_char_size_t().decode() == "purple" + assert m.bytes_from_string().decode() == "foo" + assert m.bytes_from_str().decode() == "bar" + + assert doc(m.bytes_from_str) == "bytes_from_str() -> bytes" + + +def test_bytearray(): + assert m.bytearray_from_char_ssize_t().decode() == "$%" + assert m.bytearray_from_char_size_t().decode() == "@$!" + assert m.bytearray_from_string().decode() == "foo" + assert m.bytearray_size() == len("foo") + + +def test_capsule(capture): + pytest.gc_collect() + with capture: + a = m.return_capsule_with_destructor() + del a + pytest.gc_collect() + assert ( + capture.unordered + == """ + creating capsule + destructing capsule + """ + ) + + with capture: + a = m.return_renamed_capsule_with_destructor() + del a + pytest.gc_collect() + assert ( + capture.unordered + == """ + creating capsule + renaming capsule + destructing capsule + """ + ) + + with capture: + a = m.return_capsule_with_destructor_2() + del a + pytest.gc_collect() + assert ( + capture.unordered + == """ + creating capsule + destructing capsule: 1234 + """ + ) + + with capture: + a = m.return_capsule_with_destructor_3() + del a + pytest.gc_collect() + assert ( + capture.unordered + == """ + creating capsule + destructing capsule: 1233 + original name: oname + """ + ) + + with capture: + a = m.return_renamed_capsule_with_destructor_2() + del a + pytest.gc_collect() + assert ( + capture.unordered + == """ + creating capsule + renaming capsule + destructing capsule: 1234 + """ + ) + + with capture: + a = m.return_capsule_with_name_and_destructor() + del a + pytest.gc_collect() + assert ( + capture.unordered + == """ + created capsule (1234, 'pointer type description') + destructing capsule (1234, 'pointer type description') + """ + ) + + with capture: + a = m.return_capsule_with_explicit_nullptr_dtor() + del a + pytest.gc_collect() + assert ( + capture.unordered + == """ + creating capsule with explicit nullptr dtor + """ + ) + + +def test_accessors(): + class SubTestObject: + attr_obj = 1 + attr_char = 2 + + class TestObject: + basic_attr = 1 + begin_end = [1, 2, 3] + d = {"operator[object]": 1, "operator[char *]": 2} + sub = SubTestObject() + + def func(self, x, *args): + return self.basic_attr + x + sum(args) + + d = m.accessor_api(TestObject()) + assert d["basic_attr"] == 1 + assert d["begin_end"] == [1, 2, 3] + assert d["operator[object]"] == 1 + assert d["operator[char *]"] == 2 + assert d["attr(object)"] == 1 + assert d["attr(char *)"] == 2 + assert d["missing_attr_ptr"] == "raised" + assert d["missing_attr_chain"] == "raised" + assert d["is_none"] is False + assert d["operator()"] == 2 + assert d["operator*"] == 7 + assert d["implicit_list"] == [1, 2, 3] + assert all(x in TestObject.__dict__ for x in d["implicit_dict"]) + + assert m.tuple_accessor(()) == (0, 1, 2) + + d = m.accessor_assignment() + assert d["get"] == 0 + assert d["deferred_get"] == 0 + assert d["set"] == 1 + assert d["deferred_set"] == 1 + assert d["var"] == 99 + + +def test_accessor_moves(): + inc_refs = m.accessor_moves() + if inc_refs: + assert inc_refs == [1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0] + else: + pytest.skip("Not defined: PYBIND11_HANDLE_REF_DEBUG") + + +@pytest.mark.xfail("env.GRAALPY", reason="TODO should be fixed on GraalPy side") +def test_constructors(): + """C++ default and converting constructors are equivalent to type calls in Python""" + types = [bytes, bytearray, str, bool, int, float, tuple, list, dict, set] + expected = {t.__name__: t() for t in types} + assert m.default_constructors() == expected + + data = { + bytes: b"41", # Currently no supported or working conversions. + bytearray: bytearray(b"41"), + str: 42, + bool: "Not empty", + int: "42", + float: "+1e3", + tuple: range(3), + list: range(3), + dict: [("two", 2), ("one", 1), ("three", 3)], + set: [4, 4, 5, 6, 6, 6], + frozenset: [4, 4, 5, 6, 6, 6], + memoryview: b"abc", + } + inputs = {k.__name__: v for k, v in data.items()} + expected = {k.__name__: k(v) for k, v in data.items()} + + assert m.converting_constructors(inputs) == expected + assert m.cast_functions(inputs) == expected + + # Converting constructors and cast functions should just reference rather + # than copy when no conversion is needed: + noconv1 = m.converting_constructors(expected) + for k in noconv1: + assert noconv1[k] is expected[k] + + noconv2 = m.cast_functions(expected) + for k in noconv2: + assert noconv2[k] is expected[k] + + +def test_non_converting_constructors(): + non_converting_test_cases = [ + ("bytes", range(10)), + ("none", 42), + ("ellipsis", 42), + ("type", 42), + ] + for t, v in non_converting_test_cases: + for move in [True, False]: + with pytest.raises(TypeError) as excinfo: + m.nonconverting_constructor(t, v, move) + expected_error = ( + f"Object of type '{type(v).__name__}' is not an instance of '{t}'" + ) + assert str(excinfo.value) == expected_error + + +def test_pybind11_str_raw_str(): + # specifically to exercise pybind11::str::raw_str + cvt = m.convert_to_pybind11_str + assert cvt("Str") == "Str" + assert cvt(b"Bytes") == "b'Bytes'" + assert cvt(None) == "None" + assert cvt(False) == "False" + assert cvt(True) == "True" + assert cvt(42) == "42" + assert cvt(2**65) == "36893488147419103232" + assert cvt(-1.50) == "-1.5" + assert cvt(()) == "()" + assert cvt((18,)) == "(18,)" + assert cvt([]) == "[]" + assert cvt([28]) == "[28]" + assert cvt({}) == "{}" + assert cvt({3: 4}) == "{3: 4}" + assert cvt(set()) == "set()" + assert cvt({3}) == "{3}" + + valid_orig = "DZ" + valid_utf8 = valid_orig.encode("utf-8") + valid_cvt = cvt(valid_utf8) + if hasattr(m, "PYBIND11_STR_LEGACY_PERMISSIVE"): + assert valid_cvt is valid_utf8 + else: + assert type(valid_cvt) is str + assert valid_cvt == "b'\\xc7\\xb1'" + + malformed_utf8 = b"\x80" + if hasattr(m, "PYBIND11_STR_LEGACY_PERMISSIVE"): + assert cvt(malformed_utf8) is malformed_utf8 + else: + malformed_cvt = cvt(malformed_utf8) + assert type(malformed_cvt) is str + assert malformed_cvt == "b'\\x80'" + + +def test_implicit_casting(): + """Tests implicit casting when assigning or appending to dicts and lists.""" + z = m.get_implicit_casting() + assert z["d"] == { + "char*_i1": "abc", + "char*_i2": "abc", + "char*_e": "abc", + "char*_p": "abc", + "str_i1": "str", + "str_i2": "str1", + "str_e": "str2", + "str_p": "str3", + "int_i1": 42, + "int_i2": 42, + "int_e": 43, + "int_p": 44, + } + assert z["l"] == [3, 6, 9, 12, 15] + + +def test_print(capture): + with capture: + m.print_function() + assert ( + capture + == """ + Hello, World! + 1 2.0 three True -- multiple args + *args-and-a-custom-separator + no new line here -- next print + flush + py::print + str.format = this + """ + ) + assert capture.stderr == "this goes to stderr" + + with pytest.raises(RuntimeError) as excinfo: + m.print_failure() + assert str(excinfo.value) == "Unable to convert call argument " + ( + "'1' of type 'UnregisteredType' to Python object" + if detailed_error_messages_enabled + else "'1' to Python object (#define PYBIND11_DETAILED_ERROR_MESSAGES or compile in debug mode for details)" + ) + + +def test_hash(): + class Hashable: + def __init__(self, value): + self.value = value + + def __hash__(self): + return self.value + + class Unhashable: + __hash__ = None + + assert m.hash_function(Hashable(42)) == 42 + with pytest.raises(TypeError): + m.hash_function(Unhashable()) + + +def test_number_protocol(): + for a, b in [(1, 1), (3, 5)]: + li = [ + a == b, + a != b, + a < b, + a <= b, + a > b, + a >= b, + a + b, + a - b, + a * b, + a / b, + a | b, + a & b, + a ^ b, + a >> b, + a << b, + ] + assert m.test_number_protocol(a, b) == li + + +def test_list_slicing(): + li = list(range(100)) + assert li[0:-1:2] == m.test_list_slicing(li) + assert li[::] == m.test_list_slicing_default(li) + + +def test_issue2361(): + # See issue #2361 + assert m.issue2361_str_implicit_copy_none() == "None" + with pytest.raises(TypeError) as excinfo: + assert m.issue2361_dict_implicit_copy_none() + assert "NoneType" in str(excinfo.value) + assert "iterable" in str(excinfo.value) + + +@pytest.mark.parametrize( + ("method", "args", "fmt", "expected_view"), + [ + (m.test_memoryview_object, (b"red",), "B", b"red"), + (m.test_memoryview_buffer_info, (b"green",), "B", b"green"), + (m.test_memoryview_from_buffer, (False,), "h", [3, 1, 4, 1, 5]), + (m.test_memoryview_from_buffer, (True,), "H", [2, 7, 1, 8]), + (m.test_memoryview_from_buffer_nativeformat, (), "@i", [4, 7, 5]), + ], +) +def test_memoryview(method, args, fmt, expected_view): + view = method(*args) + assert isinstance(view, memoryview) + assert view.format == fmt + assert list(view) == list(expected_view) + + +@pytest.mark.xfail("env.PYPY", reason="getrefcount is not available") +@pytest.mark.parametrize( + "method", + [ + m.test_memoryview_object, + m.test_memoryview_buffer_info, + ], +) +def test_memoryview_refcount(method): + # Avoiding a literal to avoid an immortal object in free-threaded builds + buf = "\x0a\x0b\x0c\x0d".encode("ascii") + ref_before = sys.getrefcount(buf) + view = method(buf) + ref_after = sys.getrefcount(buf) + assert ref_before < ref_after + assert list(view) == list(buf) + + +def test_memoryview_from_buffer_empty_shape(): + view = m.test_memoryview_from_buffer_empty_shape() + assert isinstance(view, memoryview) + assert view.format == "B" + assert bytes(view) == b"" + + +def test_test_memoryview_from_buffer_invalid_strides(): + with pytest.raises(RuntimeError): + m.test_memoryview_from_buffer_invalid_strides() + + +def test_test_memoryview_from_buffer_nullptr(): + with pytest.raises(ValueError): + m.test_memoryview_from_buffer_nullptr() + + +def test_memoryview_from_memory(): + view = m.test_memoryview_from_memory() + assert isinstance(view, memoryview) + assert view.format == "B" + assert bytes(view) == b"\xff\xe1\xab\x37" + + +def test_builtin_functions(): + assert m.get_len(list(range(42))) == 42 + with pytest.raises(TypeError) as exc_info: + m.get_len(i for i in range(42)) + assert str(exc_info.value) in [ + "object of type 'generator' has no len()", + "'generator' has no length", + ] # PyPy + + +def test_isinstance_string_types(): + assert m.isinstance_pybind11_bytes(b"") + assert not m.isinstance_pybind11_bytes("") + + assert m.isinstance_pybind11_str("") + if hasattr(m, "PYBIND11_STR_LEGACY_PERMISSIVE"): + assert m.isinstance_pybind11_str(b"") + else: + assert not m.isinstance_pybind11_str(b"") + + +def test_pass_bytes_or_unicode_to_string_types(): + assert m.pass_to_pybind11_bytes(b"Bytes") == 5 + with pytest.raises(TypeError): + m.pass_to_pybind11_bytes("Str") + + if hasattr(m, "PYBIND11_STR_LEGACY_PERMISSIVE"): + assert m.pass_to_pybind11_str(b"Bytes") == 5 + else: + with pytest.raises(TypeError): + m.pass_to_pybind11_str(b"Bytes") + assert m.pass_to_pybind11_str("Str") == 3 + + assert m.pass_to_std_string(b"Bytes") == 5 + assert m.pass_to_std_string("Str") == 3 + + malformed_utf8 = b"\x80" + if hasattr(m, "PYBIND11_STR_LEGACY_PERMISSIVE"): + assert m.pass_to_pybind11_str(malformed_utf8) == 1 + else: + with pytest.raises(TypeError): + m.pass_to_pybind11_str(malformed_utf8) + + +@pytest.mark.skipif("env.GRAALPY", reason="Cannot reliably trigger GC") +@pytest.mark.parametrize( + ("create_weakref", "create_weakref_with_callback"), + [ + (m.weakref_from_handle, m.weakref_from_handle_and_function), + (m.weakref_from_object, m.weakref_from_object_and_function), + ], +) +def test_weakref(create_weakref, create_weakref_with_callback): + from weakref import getweakrefcount + + # Apparently, you cannot weakly reference an object() + class WeaklyReferenced: + pass + + callback_called = False + + def callback(_): + nonlocal callback_called + callback_called = True + + obj = WeaklyReferenced() + assert getweakrefcount(obj) == 0 + wr = create_weakref(obj) + assert getweakrefcount(obj) == 1 + + obj = WeaklyReferenced() + assert getweakrefcount(obj) == 0 + wr = create_weakref_with_callback(obj, callback) # noqa: F841 + assert getweakrefcount(obj) == 1 + assert not callback_called + del obj + pytest.gc_collect() + assert callback_called + + +@pytest.mark.parametrize( + ("create_weakref", "has_callback"), + [ + (m.weakref_from_handle, False), + (m.weakref_from_object, False), + (m.weakref_from_handle_and_function, True), + (m.weakref_from_object_and_function, True), + ], +) +def test_weakref_err(create_weakref, has_callback): + class C: + __slots__ = [] + + def callback(_): + pass + + ob = C() + # Should raise TypeError on CPython + cm = pytest.raises(TypeError) + if env.PYPY or env.GRAALPY: + cm = contextlib.nullcontext() + with cm: + _ = create_weakref(ob, callback) if has_callback else create_weakref(ob) + + +def test_cpp_iterators(): + assert m.tuple_iterator() == 12 + assert m.dict_iterator() == 305 + 711 + assert m.passed_iterator(iter((-7, 3))) == -4 + + +def test_implementation_details(): + lst = [39, 43, 92, 49, 22, 29, 93, 98, 26, 57, 8] + tup = tuple(lst) + assert m.sequence_item_get_ssize_t(lst) == 43 + assert m.sequence_item_set_ssize_t(lst) is None + assert lst[1] == "peppa" + assert m.sequence_item_get_size_t(lst) == 92 + assert m.sequence_item_set_size_t(lst) is None + assert lst[2] == "george" + assert m.list_item_get_ssize_t(lst) == 49 + assert m.list_item_set_ssize_t(lst) is None + assert lst[3] == "rebecca" + assert m.list_item_get_size_t(lst) == 22 + assert m.list_item_set_size_t(lst) is None + assert lst[4] == "richard" + assert m.tuple_item_get_ssize_t(tup) == 29 + assert m.tuple_item_set_ssize_t() == ("emely", "edmond") + assert m.tuple_item_get_size_t(tup) == 93 + assert m.tuple_item_set_size_t() == ("candy", "cat") + + +def test_external_float_(): + r1 = m.square_float_(2.0) + assert r1 == 4.0 + + +def test_tuple_rvalue_getter(): + pop = 1000 + tup = tuple(range(pop)) + m.tuple_rvalue_getter(tup) + + +def test_list_rvalue_getter(): + pop = 1000 + my_list = list(range(pop)) + m.list_rvalue_getter(my_list) + + +def test_populate_dict_rvalue(): + pop = 1000 + my_dict = {i: i for i in range(pop)} + assert m.populate_dict_rvalue(pop) == my_dict + + +def test_populate_obj_str_attrs(): + pop = 1000 + o = types.SimpleNamespace(**{str(i): i for i in range(pop)}) + new_o = m.populate_obj_str_attrs(o, pop) + new_attrs = {k: v for k, v in new_o.__dict__.items() if not k.startswith("_")} + assert all(isinstance(v, str) for v in new_attrs.values()) + assert len(new_attrs) == pop + + +@pytest.mark.parametrize( + ("a", "b"), + [("foo", "bar"), (1, 2), (1.0, 2.0), (list(range(3)), list(range(3, 6)))], +) +def test_inplace_append(a, b): + expected = a + b + assert m.inplace_append(a, b) == expected + + +@pytest.mark.parametrize( + ("a", "b"), [(3, 2), (3.0, 2.0), (set(range(3)), set(range(2)))] +) +def test_inplace_subtract(a, b): + expected = a - b + assert m.inplace_subtract(a, b) == expected + + +@pytest.mark.parametrize(("a", "b"), [(3, 2), (3.0, 2.0), ([1], 3)]) +def test_inplace_multiply(a, b): + expected = a * b + assert m.inplace_multiply(a, b) == expected + + +@pytest.mark.parametrize(("a", "b"), [(6, 3), (6.0, 3.0)]) +def test_inplace_divide(a, b): + expected = a / b + assert m.inplace_divide(a, b) == expected + + +@pytest.mark.parametrize( + ("a", "b"), + [ + (False, True), + ( + set(), + { + 1, + }, + ), + ], +) +def test_inplace_or(a, b): + expected = a | b + assert m.inplace_or(a, b) == expected + + +@pytest.mark.parametrize( + ("a", "b"), + [ + (True, False), + ( + {1, 2, 3}, + { + 1, + }, + ), + ], +) +def test_inplace_and(a, b): + expected = a & b + assert m.inplace_and(a, b) == expected + + +@pytest.mark.parametrize(("a", "b"), [(8, 1), (-3, 2)]) +def test_inplace_lshift(a, b): + expected = a << b + assert m.inplace_lshift(a, b) == expected + + +@pytest.mark.parametrize(("a", "b"), [(8, 1), (-2, 2)]) +def test_inplace_rshift(a, b): + expected = a >> b + assert m.inplace_rshift(a, b) == expected + + +def test_tuple_nonempty_annotations(doc): + assert ( + doc(m.annotate_tuple_float_str) + == "annotate_tuple_float_str(arg0: tuple[float, str]) -> None" + ) + + +def test_tuple_empty_annotations(doc): + assert ( + doc(m.annotate_tuple_empty) == "annotate_tuple_empty(arg0: tuple[()]) -> None" + ) + + +def test_tuple_variable_length_annotations(doc): + assert ( + doc(m.annotate_tuple_variable_length) + == "annotate_tuple_variable_length(arg0: tuple[float, ...]) -> None" + ) + + +def test_dict_annotations(doc): + assert ( + doc(m.annotate_dict_str_int) + == "annotate_dict_str_int(arg0: dict[str, typing.SupportsInt | typing.SupportsIndex]) -> None" + ) + + +def test_list_annotations(doc): + assert ( + doc(m.annotate_list_int) + == "annotate_list_int(arg0: list[typing.SupportsInt | typing.SupportsIndex]) -> None" + ) + + +def test_set_annotations(doc): + assert doc(m.annotate_set_str) == "annotate_set_str(arg0: set[str]) -> None" + + +def test_iterable_annotations(doc): + assert ( + doc(m.annotate_iterable_str) + == "annotate_iterable_str(arg0: collections.abc.Iterable[str]) -> None" + ) + + +def test_iterator_annotations(doc): + assert ( + doc(m.annotate_iterator_int) + == "annotate_iterator_int(arg0: collections.abc.Iterator[typing.SupportsInt | typing.SupportsIndex]) -> None" + ) + + +def test_fn_annotations(doc): + assert ( + doc(m.annotate_fn) + == "annotate_fn(arg0: collections.abc.Callable[[list[str], str], int]) -> None" + ) + + +def test_fn_return_only(doc): + assert ( + doc(m.annotate_fn_only_return) + == "annotate_fn_only_return(arg0: collections.abc.Callable[..., int]) -> None" + ) + + +def test_type_annotation(doc): + assert ( + doc(m.annotate_type) + == "annotate_type(arg0: type[typing.SupportsInt | typing.SupportsIndex]) -> type" + ) + + +def test_union_annotations(doc): + assert ( + doc(m.annotate_union) + == "annotate_union(arg0: list[str | int | object], arg1: str, arg2: int, arg3: object) -> list[str | int | object]" + ) + + +def test_union_typing_only(doc): + assert doc(m.union_typing_only) == "union_typing_only(arg0: list[str]) -> list[int]" + + +def test_union_object_annotations(doc): + assert ( + doc(m.annotate_union_to_object) + == "annotate_union_to_object(arg0: typing.SupportsInt | typing.SupportsIndex | str) -> object" + ) + + +def test_optional_annotations(doc): + assert ( + doc(m.annotate_optional) == "annotate_optional(arg0: list) -> list[str | None]" + ) + + +def test_type_guard_annotations(doc, backport_typehints): + assert ( + backport_typehints(doc(m.annotate_type_guard)) + == "annotate_type_guard(arg0: object) -> typing.TypeGuard[str]" + ) + + +def test_type_is_annotations(doc, backport_typehints): + assert ( + backport_typehints(doc(m.annotate_type_is)) + == "annotate_type_is(arg0: object) -> typing.TypeIs[str]" + ) + + +def test_no_return_annotation(doc): + assert doc(m.annotate_no_return) == "annotate_no_return() -> typing.NoReturn" + + +def test_never_annotation(doc, backport_typehints): + assert ( + backport_typehints(doc(m.annotate_never)) == "annotate_never() -> typing.Never" + ) + + +def test_optional_object_annotations(doc): + assert ( + doc(m.annotate_optional_to_object) + == "annotate_optional_to_object(arg0: typing.SupportsInt | typing.SupportsIndex | None) -> object" + ) + + +@pytest.mark.skipif( + not m.defined_PYBIND11_TYPING_H_HAS_STRING_LITERAL, + reason="C++20 non-type template args feature not available.", +) +def test_literal(doc): + assert ( + doc(m.annotate_literal) + == 'annotate_literal(arg0: typing.Literal[26, 0x1A, "hello world", b"hello world", u"hello world", True, Color.RED, None]) -> object' + ) + # The characters !, @, %, {, } and -> are used in the signature parser as special characters, but Literal should escape those for the parser to work. + assert ( + doc(m.identity_literal_exclamation) + == 'identity_literal_exclamation(arg0: typing.Literal["!"]) -> typing.Literal["!"]' + ) + assert ( + doc(m.identity_literal_at) + == 'identity_literal_at(arg0: typing.Literal["@"]) -> typing.Literal["@"]' + ) + assert ( + doc(m.identity_literal_percent) + == 'identity_literal_percent(arg0: typing.Literal["%"]) -> typing.Literal["%"]' + ) + assert ( + doc(m.identity_literal_curly_open) + == 'identity_literal_curly_open(arg0: typing.Literal["{"]) -> typing.Literal["{"]' + ) + assert ( + doc(m.identity_literal_curly_close) + == 'identity_literal_curly_close(arg0: typing.Literal["}"]) -> typing.Literal["}"]' + ) + assert ( + doc(m.identity_literal_arrow_with_io_name) + == 'identity_literal_arrow_with_io_name(arg0: typing.Literal["->"], arg1: float | int) -> typing.Literal["->"]' + ) + assert ( + doc(m.identity_literal_arrow_with_callable) + == 'identity_literal_arrow_with_callable(arg0: collections.abc.Callable[[typing.Literal["->"], float | int], float]) -> collections.abc.Callable[[typing.Literal["->"], float | int], float]' + ) + assert ( + doc(m.identity_literal_all_special_chars) + == 'identity_literal_all_special_chars(arg0: typing.Literal["!@!!->{%}"]) -> typing.Literal["!@!!->{%}"]' + ) + + +@pytest.mark.skipif( + not m.defined_PYBIND11_TYPING_H_HAS_STRING_LITERAL, + reason="C++20 non-type template args feature not available.", +) +def test_typevar(doc): + assert ( + doc(m.annotate_generic_containers) + == "annotate_generic_containers(arg0: list[T]) -> list[V]" + ) + + assert doc(m.annotate_listT_to_T) == "annotate_listT_to_T(arg0: list[T]) -> T" + + assert doc(m.annotate_object_to_T) == "annotate_object_to_T(arg0: object) -> T" + + +@pytest.mark.skipif( + not m.defined_PYBIND11_TEST_PYTYPES_HAS_RANGES, + reason=" not available.", +) +@pytest.mark.parametrize( + ("tested_tuple", "expected"), + [((1,), [2]), ((3, 4), [4, 5]), ((7, 8, 9), [8, 9, 10])], +) +def test_tuple_ranges(tested_tuple, expected): + assert m.tuple_iterator_default_initialization() + assert m.transform_tuple_plus_one(tested_tuple) == expected + + +@pytest.mark.skipif( + not m.defined_PYBIND11_TEST_PYTYPES_HAS_RANGES, + reason=" not available.", +) +@pytest.mark.parametrize( + ("tested_list", "expected"), [([1], [2]), ([3, 4], [4, 5]), ([7, 8, 9], [8, 9, 10])] +) +def test_list_ranges(tested_list, expected): + assert m.list_iterator_default_initialization() + assert m.transform_list_plus_one(tested_list) == expected + + +@pytest.mark.skipif( + not m.defined_PYBIND11_TEST_PYTYPES_HAS_RANGES, + reason=" not available.", +) +@pytest.mark.parametrize( + ("tested_dict", "expected"), + [ + ({1: 2}, [(2, 3)]), + ({3: 4, 5: 6}, [(4, 5), (6, 7)]), + ({7: 8, 9: 10, 11: 12}, [(8, 9), (10, 11), (12, 13)]), + ], +) +def test_dict_ranges(tested_dict, expected): + assert m.dict_iterator_default_initialization() + assert m.transform_dict_plus_one(tested_dict) == expected + + +# https://docs.python.org/3/howto/annotations.html#accessing-the-annotations-dict-of-an-object-in-python-3-9-and-older +def get_annotations_helper(o): + if sys.version_info >= (3, 14): + import annotationlib + + return annotationlib.get_annotations(o) or None + if isinstance(o, type): + return o.__dict__.get("__annotations__", None) + return getattr(o, "__annotations__", None) + + +@pytest.mark.skipif( + not m.defined___cpp_inline_variables, + reason="C++17 feature __cpp_inline_variables not available.", +) +def test_module_attribute_types() -> None: + module_annotations = get_annotations_helper(m) + + assert ( + module_annotations["list_int"] + == "list[typing.SupportsInt | typing.SupportsIndex]" + ) + assert module_annotations["set_str"] == "set[str]" + assert module_annotations["foo"] == "pybind11_tests.pytypes.foo" + + assert ( + module_annotations["foo_union"] + == "pybind11_tests.pytypes.foo | pybind11_tests.pytypes.foo2 | pybind11_tests.pytypes.foo3" + ) + + +@pytest.mark.skipif( + not m.defined___cpp_inline_variables, + reason="C++17 feature __cpp_inline_variables not available.", +) +@pytest.mark.skipif( + sys.version_info < (3, 10), + reason="get_annotations function does not exist until Python3.10", +) +def test_get_annotations_compliance() -> None: + from inspect import get_annotations + + module_annotations = get_annotations(m) + + assert ( + module_annotations["list_int"] + == "list[typing.SupportsInt | typing.SupportsIndex]" + ) + assert module_annotations["set_str"] == "set[str]" + + +@pytest.mark.skipif( + not m.defined___cpp_inline_variables, + reason="C++17 feature __cpp_inline_variables not available.", +) +def test_class_attribute_types() -> None: + empty_annotations = get_annotations_helper(m.EmptyAnnotationClass) + static_annotations = get_annotations_helper(m.Static) + instance_annotations = get_annotations_helper(m.Instance) + + assert empty_annotations is None + assert ( + static_annotations["x"] + == "typing.ClassVar[typing.SupportsFloat | typing.SupportsIndex]" + ) + assert ( + static_annotations["dict_str_int"] + == "typing.ClassVar[dict[str, typing.SupportsInt | typing.SupportsIndex]]" + ) + + assert m.Static.x == 1.0 + + m.Static.x = 3.0 + static = m.Static() + assert static.x == 3.0 + + static.dict_str_int["hi"] = 3 + assert m.Static().dict_str_int == {"hi": 3} + + assert instance_annotations["y"] == "typing.SupportsFloat | typing.SupportsIndex" + instance1 = m.Instance() + instance1.y = 4.0 + + instance2 = m.Instance() + instance2.y = 5.0 + + assert instance1.y != instance2.y + + +@pytest.mark.skipif( + not m.defined___cpp_inline_variables, + reason="C++17 feature __cpp_inline_variables not available.", +) +def test_redeclaration_attr_with_type_hint() -> None: + obj = m.Instance() + m.attr_with_type_hint_float_x(obj) + assert ( + get_annotations_helper(obj)["x"] + == "typing.SupportsFloat | typing.SupportsIndex" + ) + with pytest.raises( + RuntimeError, match=r'^__annotations__\["x"\] was set already\.$' + ): + m.attr_with_type_hint_float_x(obj) + + +@pytest.mark.skipif( + not m.defined___cpp_inline_variables, + reason="C++17 feature __cpp_inline_variables not available.", +) +def test_final_annotation() -> None: + module_annotations = get_annotations_helper(m) + assert module_annotations["CONST_INT"] == "typing.Final[int]" + + +def test_arg_return_type_hints(doc, backport_typehints): + assert doc(m.half_of_number) == "half_of_number(arg0: float | int) -> float" + assert ( + doc(m.half_of_number_convert) + == "half_of_number_convert(x: float | int) -> float" + ) + assert ( + doc(m.half_of_number_noconvert) == "half_of_number_noconvert(x: float) -> float" + ) + assert m.half_of_number(2.0) == 1.0 + assert m.half_of_number(2) == 1.0 + assert m.half_of_number(0) == 0 + assert isinstance(m.half_of_number(0), float) + assert not isinstance(m.half_of_number(0), int) + + # std::vector + assert ( + doc(m.half_of_number_vector) + == "half_of_number_vector(arg0: collections.abc.Sequence[float | int]) -> list[float]" + ) + # Tuple + assert ( + doc(m.half_of_number_tuple) + == "half_of_number_tuple(arg0: tuple[float | int, float | int]) -> tuple[float, float]" + ) + # Tuple + assert ( + doc(m.half_of_number_tuple_ellipsis) + == "half_of_number_tuple_ellipsis(arg0: tuple[float | int, ...]) -> tuple[float, ...]" + ) + # Dict + assert ( + doc(m.half_of_number_dict) + == "half_of_number_dict(arg0: dict[str, float | int]) -> dict[str, float]" + ) + # List + assert ( + doc(m.half_of_number_list) + == "half_of_number_list(arg0: list[float | int]) -> list[float]" + ) + # List> + assert ( + doc(m.half_of_number_nested_list) + == "half_of_number_nested_list(arg0: list[list[float | int]]) -> list[list[float]]" + ) + # Set + assert doc(m.identity_set) == "identity_set(arg0: set[float | int]) -> set[float]" + # Iterable + assert ( + doc(m.identity_iterable) + == "identity_iterable(arg0: collections.abc.Iterable[float | int]) -> collections.abc.Iterable[float]" + ) + # Iterator + assert ( + doc(m.identity_iterator) + == "identity_iterator(arg0: collections.abc.Iterator[float | int]) -> collections.abc.Iterator[float]" + ) + # Callable identity + assert ( + doc(m.identity_callable) + == "identity_callable(arg0: collections.abc.Callable[[float | int], float]) -> collections.abc.Callable[[float | int], float]" + ) + # Callable identity + assert ( + doc(m.identity_callable_ellipsis) + == "identity_callable_ellipsis(arg0: collections.abc.Callable[..., float]) -> collections.abc.Callable[..., float]" + ) + # Nested Callable identity + assert ( + doc(m.identity_nested_callable) + == "identity_nested_callable(arg0: collections.abc.Callable[[collections.abc.Callable[[float | int], float]], collections.abc.Callable[[float | int], float]]) -> collections.abc.Callable[[collections.abc.Callable[[float | int], float]], collections.abc.Callable[[float | int], float]]" + ) + # Callable + assert ( + doc(m.apply_callable) + == "apply_callable(arg0: float | int, arg1: collections.abc.Callable[[float | int], float]) -> float" + ) + # Callable + assert ( + doc(m.apply_callable_ellipsis) + == "apply_callable_ellipsis(arg0: float | int, arg1: collections.abc.Callable[..., float]) -> float" + ) + # Union + assert ( + doc(m.identity_union) + == "identity_union(arg0: float | int | str) -> float | str" + ) + # Optional + assert ( + doc(m.identity_optional) + == "identity_optional(arg0: float | int | None) -> float | None" + ) + # TypeIs + assert ( + backport_typehints(doc(m.check_type_is)) + == "check_type_is(arg0: object) -> typing.TypeIs[float]" + ) + # TypeGuard + assert ( + backport_typehints(doc(m.check_type_guard)) + == "check_type_guard(arg0: list[object]) -> typing.TypeGuard[list[float]]" + ) + + +def test_const_kwargs_ref_to_str(): + assert m.const_kwargs_ref_to_str() == "{}" + assert m.const_kwargs_ref_to_str(a=1) == "{'a': 1}" diff --git a/external_libraries/pybind11/tests/test_scoped_critical_section.cpp b/external_libraries/pybind11/tests/test_scoped_critical_section.cpp new file mode 100644 index 00000000..7401eb09 --- /dev/null +++ b/external_libraries/pybind11/tests/test_scoped_critical_section.cpp @@ -0,0 +1,274 @@ +#include + +#include "pybind11_tests.h" + +#include +#include +#include +#include + +#if defined(PYBIND11_HAS_STD_BARRIER) +# include +#endif + +namespace test_scoped_critical_section_ns { + +void test_one_nullptr() { py::scoped_critical_section lock{py::handle{}}; } + +void test_two_nullptrs() { py::scoped_critical_section lock{py::handle{}, py::handle{}}; } + +void test_first_nullptr() { + py::dict d; + py::scoped_critical_section lock{py::handle{}, d}; +} + +void test_second_nullptr() { + py::dict d; + py::scoped_critical_section lock{d, py::handle{}}; +} + +// Referenced test implementation: https://github.com/PyO3/pyo3/blob/v0.25.0/src/sync.rs#L874 +class BoolWrapper { +public: + explicit BoolWrapper(bool value) : value_{value} {} + bool get() const { return value_.load(std::memory_order_acquire); } + void set(bool value) { value_.store(value, std::memory_order_release); } + +private: + std::atomic_bool value_{false}; +}; + +#if defined(PYBIND11_HAS_STD_BARRIER) + +// Modifying the C/C++ members of a Python object from multiple threads requires a critical section +// to ensure thread safety and data integrity. +// These tests use a scoped critical section to ensure that the Python object is accessed in a +// thread-safe manner. + +void test_scoped_critical_section(const py::handle &cls) { + auto barrier = std::barrier(2); + auto bool_wrapper = cls(false); + bool output = false; + + { + // Release the GIL to allow run threads in parallel. + py::gil_scoped_release gil_release{}; + + std::thread t1([&]() { + // Use gil_scoped_acquire to ensure we have a valid Python thread state + // before entering the critical section. Otherwise, the critical section + // will cause a segmentation fault. + py::gil_scoped_acquire ensure_tstate{}; + // Enter the critical section with the same object as the second thread. + py::scoped_critical_section lock{bool_wrapper}; + // At this point, the object is locked by this thread via the scoped_critical_section. + // This barrier will ensure that the second thread waits until this thread has released + // the critical section before proceeding. + barrier.arrive_and_wait(); + // Sleep for a short time to simulate some work in the critical section. + // This sleep is necessary to test the locking mechanism properly. + std::this_thread::sleep_for(std::chrono::milliseconds(10)); + auto *bw = bool_wrapper.cast(); + bw->set(true); + }); + + std::thread t2([&]() { + // This thread will wait until the first thread has entered the critical section due to + // the barrier. + barrier.arrive_and_wait(); + { + // Use gil_scoped_acquire to ensure we have a valid Python thread state + // before entering the critical section. Otherwise, the critical section + // will cause a segmentation fault. + py::gil_scoped_acquire ensure_tstate{}; + // Enter the critical section with the same object as the first thread. + py::scoped_critical_section lock{bool_wrapper}; + // At this point, the critical section is released by the first thread, the value + // is set to true. + auto *bw = bool_wrapper.cast(); + output = bw->get(); + } + }); + + t1.join(); + t2.join(); + } + + if (!output) { + throw std::runtime_error("Scoped critical section test failed: output is false"); + } +} + +void test_scoped_critical_section2(const py::handle &cls) { + auto barrier = std::barrier(3); + auto bool_wrapper1 = cls(false); + auto bool_wrapper2 = cls(false); + std::pair output{false, false}; + + { + // Release the GIL to allow run threads in parallel. + py::gil_scoped_release gil_release{}; + + std::thread t1([&]() { + // Use gil_scoped_acquire to ensure we have a valid Python thread state + // before entering the critical section. Otherwise, the critical section + // will cause a segmentation fault. + py::gil_scoped_acquire ensure_tstate{}; + // Enter the critical section with two different objects. + // This will ensure that the critical section is locked for both objects. + py::scoped_critical_section lock{bool_wrapper1, bool_wrapper2}; + // At this point, objects are locked by this thread via the scoped_critical_section. + // This barrier will ensure that other threads wait until this thread has released + // the critical section before proceeding. + barrier.arrive_and_wait(); + // Sleep for a short time to simulate some work in the critical section. + // This sleep is necessary to test the locking mechanism properly. + std::this_thread::sleep_for(std::chrono::milliseconds(10)); + auto *bw1 = bool_wrapper1.cast(); + auto *bw2 = bool_wrapper2.cast(); + bw1->set(true); + bw2->set(true); + }); + + std::thread t2([&]() { + // This thread will wait until the first thread has entered the critical section due to + // the barrier. + barrier.arrive_and_wait(); + { + // Use gil_scoped_acquire to ensure we have a valid Python thread state + // before entering the critical section. Otherwise, the critical section + // will cause a segmentation fault. + py::gil_scoped_acquire ensure_tstate{}; + // Enter the critical section with the same object as the first thread. + py::scoped_critical_section lock{bool_wrapper1}; + // At this point, the critical section is released by the first thread, the value + // is set to true. + auto *bw1 = bool_wrapper1.cast(); + output.first = bw1->get(); + } + }); + + std::thread t3([&]() { + // This thread will wait until the first thread has entered the critical section due to + // the barrier. + barrier.arrive_and_wait(); + { + // Use gil_scoped_acquire to ensure we have a valid Python thread state + // before entering the critical section. Otherwise, the critical section + // will cause a segmentation fault. + py::gil_scoped_acquire ensure_tstate{}; + // Enter the critical section with the same object as the first thread. + py::scoped_critical_section lock{bool_wrapper2}; + // At this point, the critical section is released by the first thread, the value + // is set to true. + auto *bw2 = bool_wrapper2.cast(); + output.second = bw2->get(); + } + }); + + t1.join(); + t2.join(); + t3.join(); + } + + if (!output.first || !output.second) { + throw std::runtime_error( + "Scoped critical section test with two objects failed: output is false"); + } +} + +void test_scoped_critical_section2_same_object_no_deadlock(const py::handle &cls) { + auto barrier = std::barrier(2); + auto bool_wrapper = cls(false); + bool output = false; + + { + // Release the GIL to allow run threads in parallel. + py::gil_scoped_release gil_release{}; + + std::thread t1([&]() { + // Use gil_scoped_acquire to ensure we have a valid Python thread state + // before entering the critical section. Otherwise, the critical section + // will cause a segmentation fault. + py::gil_scoped_acquire ensure_tstate{}; + // Enter the critical section with the same object as the second thread. + py::scoped_critical_section lock{bool_wrapper, bool_wrapper}; // same object used here + // At this point, the object is locked by this thread via the scoped_critical_section. + // This barrier will ensure that the second thread waits until this thread has released + // the critical section before proceeding. + barrier.arrive_and_wait(); + // Sleep for a short time to simulate some work in the critical section. + // This sleep is necessary to test the locking mechanism properly. + std::this_thread::sleep_for(std::chrono::milliseconds(10)); + auto *bw = bool_wrapper.cast(); + bw->set(true); + }); + + std::thread t2([&]() { + // This thread will wait until the first thread has entered the critical section due to + // the barrier. + barrier.arrive_and_wait(); + { + // Use gil_scoped_acquire to ensure we have a valid Python thread state + // before entering the critical section. Otherwise, the critical section + // will cause a segmentation fault. + py::gil_scoped_acquire ensure_tstate{}; + // Enter the critical section with the same object as the first thread. + py::scoped_critical_section lock{bool_wrapper}; + // At this point, the critical section is released by the first thread, the value + // is set to true. + auto *bw = bool_wrapper.cast(); + output = bw->get(); + } + }); + + t1.join(); + t2.join(); + } + + if (!output) { + throw std::runtime_error( + "Scoped critical section test with same object failed: output is false"); + } +} + +#else + +void test_scoped_critical_section(const py::handle &) {} +void test_scoped_critical_section2(const py::handle &) {} +void test_scoped_critical_section2_same_object_no_deadlock(const py::handle &) {} + +#endif + +} // namespace test_scoped_critical_section_ns + +TEST_SUBMODULE(scoped_critical_section, m) { + using namespace test_scoped_critical_section_ns; + + m.def("test_one_nullptr", test_one_nullptr); + m.def("test_two_nullptrs", test_two_nullptrs); + m.def("test_first_nullptr", test_first_nullptr); + m.def("test_second_nullptr", test_second_nullptr); + + auto BoolWrapperClass = py::class_(m, "BoolWrapper") + .def(py::init()) + .def("get", &BoolWrapper::get) + .def("set", &BoolWrapper::set); + auto BoolWrapperHandle = py::handle(BoolWrapperClass); + (void) BoolWrapperHandle.ptr(); // suppress unused variable warning + + m.attr("has_barrier") = +#ifdef PYBIND11_HAS_STD_BARRIER + true; +#else + false; +#endif + + m.def("test_scoped_critical_section", + [BoolWrapperHandle]() -> void { test_scoped_critical_section(BoolWrapperHandle); }); + m.def("test_scoped_critical_section2", + [BoolWrapperHandle]() -> void { test_scoped_critical_section2(BoolWrapperHandle); }); + m.def("test_scoped_critical_section2_same_object_no_deadlock", [BoolWrapperHandle]() -> void { + test_scoped_critical_section2_same_object_no_deadlock(BoolWrapperHandle); + }); +} diff --git a/external_libraries/pybind11/tests/test_scoped_critical_section.py b/external_libraries/pybind11/tests/test_scoped_critical_section.py new file mode 100644 index 00000000..5703e3d5 --- /dev/null +++ b/external_libraries/pybind11/tests/test_scoped_critical_section.py @@ -0,0 +1,30 @@ +from __future__ import annotations + +import pytest + +from pybind11_tests import scoped_critical_section as m + + +def test_nullptr_combinations(): + m.test_one_nullptr() + m.test_two_nullptrs() + m.test_first_nullptr() + m.test_second_nullptr() + + +@pytest.mark.skipif(not m.has_barrier, reason="no ") +def test_scoped_critical_section() -> None: + for _ in range(64): + m.test_scoped_critical_section() + + +@pytest.mark.skipif(not m.has_barrier, reason="no ") +def test_scoped_critical_section2() -> None: + for _ in range(64): + m.test_scoped_critical_section2() + + +@pytest.mark.skipif(not m.has_barrier, reason="no ") +def test_scoped_critical_section2_same_object_no_deadlock() -> None: + for _ in range(64): + m.test_scoped_critical_section2_same_object_no_deadlock() diff --git a/external_libraries/pybind11/tests/test_sequences_and_iterators.cpp b/external_libraries/pybind11/tests/test_sequences_and_iterators.cpp new file mode 100644 index 00000000..3f03daf3 --- /dev/null +++ b/external_libraries/pybind11/tests/test_sequences_and_iterators.cpp @@ -0,0 +1,600 @@ +/* + tests/test_sequences_and_iterators.cpp -- supporting Pythons' sequence protocol, iterators, + etc. + + Copyright (c) 2016 Wenzel Jakob + + All rights reserved. Use of this source code is governed by a + BSD-style license that can be found in the LICENSE file. +*/ + +#include +#include + +#include "constructor_stats.h" +#include "pybind11_tests.h" + +#include +#include +#include + +#ifdef PYBIND11_HAS_OPTIONAL +# include +#endif // PYBIND11_HAS_OPTIONAL + +template +class NonZeroIterator { + const T *ptr_; + +public: + explicit NonZeroIterator(const T *ptr) : ptr_(ptr) {} + + // Make the iterator non-copyable and movable + NonZeroIterator(const NonZeroIterator &) = delete; + NonZeroIterator(NonZeroIterator &&) noexcept = default; + NonZeroIterator &operator=(const NonZeroIterator &) = delete; + NonZeroIterator &operator=(NonZeroIterator &&) noexcept = default; + + const T &operator*() const { return *ptr_; } + NonZeroIterator &operator++() { + ++ptr_; + return *this; + } +}; + +class NonZeroSentinel {}; + +template +bool operator==(const NonZeroIterator> &it, const NonZeroSentinel &) { + return !(*it).first || !(*it).second; +} + +/* Iterator where dereferencing returns prvalues instead of references. */ +template +class NonRefIterator { + const T *ptr_; + +public: + explicit NonRefIterator(const T *ptr) : ptr_(ptr) {} + T operator*() const { return T(*ptr_); } + NonRefIterator &operator++() { + ++ptr_; + return *this; + } + bool operator==(const NonRefIterator &other) const { return ptr_ == other.ptr_; } +}; + +class NonCopyableInt { +public: + explicit NonCopyableInt(int value) : value_(value) {} + NonCopyableInt(const NonCopyableInt &) = delete; + NonCopyableInt(NonCopyableInt &&other) noexcept : value_(other.value_) { + other.value_ = -1; // detect when an unwanted move occurs + } + NonCopyableInt &operator=(const NonCopyableInt &) = delete; + NonCopyableInt &operator=(NonCopyableInt &&other) noexcept { + value_ = other.value_; + other.value_ = -1; // detect when an unwanted move occurs + return *this; + } + int get() const { return value_; } + void set(int value) { value_ = value; } + ~NonCopyableInt() = default; + +private: + int value_; +}; +using NonCopyableIntPair = std::pair; + +PYBIND11_MAKE_OPAQUE(std::vector) +PYBIND11_MAKE_OPAQUE(std::vector) + +template +py::list test_random_access_iterator(const PythonType &x) { + if (x.size() < 5) { + throw py::value_error("Please provide at least 5 elements for testing."); + } + + auto checks = py::list(); + auto assert_equal = [&checks](py::handle a, py::handle b) { + auto result = PyObject_RichCompareBool(a.ptr(), b.ptr(), Py_EQ); + if (result == -1) { + throw py::error_already_set(); + } + checks.append(result != 0); + }; + + auto it = x.begin(); + assert_equal(x[0], *it); + assert_equal(x[0], it[0]); + assert_equal(x[1], it[1]); + + assert_equal(x[1], *(++it)); + assert_equal(x[1], *(it++)); + assert_equal(x[2], *it); + assert_equal(x[3], *(it += 1)); + assert_equal(x[2], *(--it)); + assert_equal(x[2], *(it--)); + assert_equal(x[1], *it); + assert_equal(x[0], *(it -= 1)); + + assert_equal(it->attr("real"), x[0].attr("real")); + assert_equal((it + 1)->attr("real"), x[1].attr("real")); + + assert_equal(x[1], *(it + 1)); + assert_equal(x[1], *(1 + it)); + it += 3; + assert_equal(x[1], *(it - 2)); + + checks.append(static_cast(x.end() - x.begin()) == x.size()); + checks.append((x.begin() + static_cast(x.size())) == x.end()); + checks.append(x.begin() < x.end()); + + return checks; +} + +TEST_SUBMODULE(sequences_and_iterators, m) { + // test_sliceable + class Sliceable { + public: + explicit Sliceable(int n) : size(n) {} + int start, stop, step; + int size; + }; + py::class_(m, "Sliceable") + .def(py::init()) + .def("__getitem__", [](const Sliceable &s, const py::slice &slice) { + py::ssize_t start = 0, stop = 0, step = 0, slicelength = 0; + if (!slice.compute(s.size, &start, &stop, &step, &slicelength)) { + throw py::error_already_set(); + } + int istart = static_cast(start); + int istop = static_cast(stop); + int istep = static_cast(step); + return std::make_tuple(istart, istop, istep); + }); + + m.def("make_forward_slice_size_t", []() { return py::slice(0, -1, 1); }); + m.def("make_reversed_slice_object", + []() { return py::slice(py::none(), py::none(), py::int_(-1)); }); +#ifdef PYBIND11_HAS_OPTIONAL + m.attr("has_optional") = true; + m.def("make_reversed_slice_size_t_optional_verbose", + []() { return py::slice(std::nullopt, std::nullopt, -1); }); + // Warning: The following spelling may still compile if optional<> is not present and give + // wrong answers. Please use with caution. + m.def("make_reversed_slice_size_t_optional", []() { return py::slice({}, {}, -1); }); +#else + m.attr("has_optional") = false; +#endif + + // test_sequence + class Sequence { + public: + explicit Sequence(size_t size) : m_size(size) { + print_created(this, "of size", m_size); + // NOLINTNEXTLINE(cppcoreguidelines-prefer-member-initializer) + m_data = new float[size]; + memset(m_data, 0, sizeof(float) * size); + } + explicit Sequence(const std::vector &value) : m_size(value.size()) { + print_created(this, "of size", m_size, "from std::vector"); + // NOLINTNEXTLINE(cppcoreguidelines-prefer-member-initializer) + m_data = new float[m_size]; + memcpy(m_data, &value[0], sizeof(float) * m_size); + } + Sequence(const Sequence &s) : m_size(s.m_size) { + print_copy_created(this); + // NOLINTNEXTLINE(cppcoreguidelines-prefer-member-initializer) + m_data = new float[m_size]; + memcpy(m_data, s.m_data, sizeof(float) * m_size); + } + Sequence(Sequence &&s) noexcept : m_size(s.m_size), m_data(s.m_data) { + print_move_created(this); + s.m_size = 0; + s.m_data = nullptr; + } + + ~Sequence() { + print_destroyed(this); + delete[] m_data; + } + + Sequence &operator=(const Sequence &s) { + if (&s != this) { + delete[] m_data; + m_size = s.m_size; + m_data = new float[m_size]; + memcpy(m_data, s.m_data, sizeof(float) * m_size); + } + print_copy_assigned(this); + return *this; + } + + Sequence &operator=(Sequence &&s) noexcept { + if (&s != this) { + delete[] m_data; + m_size = s.m_size; + m_data = s.m_data; + s.m_size = 0; + s.m_data = nullptr; + } + print_move_assigned(this); + return *this; + } + + bool operator==(const Sequence &s) const { + if (m_size != s.size()) { + return false; + } + for (size_t i = 0; i < m_size; ++i) { + if (m_data[i] != s[i]) { + return false; + } + } + return true; + } + bool operator!=(const Sequence &s) const { return !operator==(s); } + + float operator[](size_t index) const { return m_data[index]; } + float &operator[](size_t index) { return m_data[index]; } + + bool contains(float v) const { + for (size_t i = 0; i < m_size; ++i) { + if (v == m_data[i]) { + return true; + } + } + return false; + } + + Sequence reversed() const { + Sequence result(m_size); + for (size_t i = 0; i < m_size; ++i) { + result[m_size - i - 1] = m_data[i]; + } + return result; + } + + size_t size() const { return m_size; } + + const float *begin() const { return m_data; } + const float *end() const { return m_data + m_size; } + + private: + size_t m_size; + float *m_data; + }; + py::class_(m, "Sequence") + .def(py::init()) + .def(py::init &>()) + /// Bare bones interface + .def("__getitem__", + [](const Sequence &s, size_t i) { + if (i >= s.size()) { + throw py::index_error(); + } + return s[i]; + }) + .def("__setitem__", + [](Sequence &s, size_t i, float v) { + if (i >= s.size()) { + throw py::index_error(); + } + s[i] = v; + }) + .def("__len__", &Sequence::size) + /// Optional sequence protocol operations + .def( + "__iter__", + [](const Sequence &s) { return py::make_iterator(s.begin(), s.end()); }, + py::keep_alive<0, 1>() /* Essential: keep object alive while iterator exists */) + .def("__contains__", [](const Sequence &s, float v) { return s.contains(v); }) + .def("__reversed__", [](const Sequence &s) -> Sequence { return s.reversed(); }) + /// Slicing protocol (optional) + .def("__getitem__", + [](const Sequence &s, const py::slice &slice) -> Sequence * { + size_t start = 0, stop = 0, step = 0, slicelength = 0; + if (!slice.compute(s.size(), &start, &stop, &step, &slicelength)) { + throw py::error_already_set(); + } + auto *seq = new Sequence(slicelength); + for (size_t i = 0; i < slicelength; ++i) { + (*seq)[i] = s[start]; + start += step; + } + return seq; + }) + .def("__setitem__", + [](Sequence &s, const py::slice &slice, const Sequence &value) { + size_t start = 0, stop = 0, step = 0, slicelength = 0; + if (!slice.compute(s.size(), &start, &stop, &step, &slicelength)) { + throw py::error_already_set(); + } + if (slicelength != value.size()) { + throw std::runtime_error( + "Left and right hand size of slice assignment have different sizes!"); + } + for (size_t i = 0; i < slicelength; ++i) { + s[start] = value[i]; + start += step; + } + }) + /// Comparisons + .def(py::self == py::self) + .def(py::self != py::self) + // Could also define py::self + py::self for concatenation, etc. + ; + + // test_map_iterator + // Interface of a map-like object that isn't (directly) an unordered_map, but provides some + // basic map-like functionality. + class StringMap { + public: + StringMap() = default; + explicit StringMap(std::unordered_map init) + : map(std::move(init)) {} + + void set(const std::string &key, std::string val) { map[key] = std::move(val); } + std::string get(const std::string &key) const { return map.at(key); } + size_t size() const { return map.size(); } + + private: + std::unordered_map map; + + public: + decltype(map.cbegin()) begin() const { return map.cbegin(); } + decltype(map.cend()) end() const { return map.cend(); } + }; + py::class_(m, "StringMap") + .def(py::init<>()) + .def(py::init>()) + .def("__getitem__", + [](const StringMap &map, const std::string &key) { + try { + return map.get(key); + } catch (const std::out_of_range &) { + throw py::key_error("key '" + key + "' does not exist"); + } + }) + .def("__setitem__", &StringMap::set) + .def("__len__", &StringMap::size) + .def( + "__iter__", + [](const StringMap &map) { return py::make_key_iterator(map.begin(), map.end()); }, + py::keep_alive<0, 1>()) + .def( + "items", + [](const StringMap &map) { return py::make_iterator(map.begin(), map.end()); }, + py::keep_alive<0, 1>()) + .def( + "values", + [](const StringMap &map) { return py::make_value_iterator(map.begin(), map.end()); }, + py::keep_alive<0, 1>()); + + // test_generalized_iterators + class IntPairs { + public: + explicit IntPairs(std::vector> data) : data_(std::move(data)) {} + const std::pair *begin() const { return data_.data(); } + // .end() only required for py::make_iterator(self) overload + const std::pair *end() const { return data_.data() + data_.size(); } + + private: + std::vector> data_; + }; + + { + // #4383 : Make sure `py::make_*iterator` functions work with move-only iterators + using iterator_t = NonZeroIterator>; + + static_assert(std::is_move_assignable::value, ""); + static_assert(std::is_move_constructible::value, ""); + static_assert(!std::is_copy_assignable::value, ""); + static_assert(!std::is_copy_constructible::value, ""); + } + + py::class_(m, "IntPairs") + .def(py::init>>()) + .def( + "nonzero", + [](const IntPairs &s) { + return py::make_iterator(NonZeroIterator>(s.begin()), + NonZeroSentinel()); + }, + py::keep_alive<0, 1>()) + .def( + "nonzero_keys", + [](const IntPairs &s) { + return py::make_key_iterator(NonZeroIterator>(s.begin()), + NonZeroSentinel()); + }, + py::keep_alive<0, 1>()) + .def( + "nonzero_values", + [](const IntPairs &s) { + return py::make_value_iterator(NonZeroIterator>(s.begin()), + NonZeroSentinel()); + }, + py::keep_alive<0, 1>()) + + // test iterator that returns values instead of references + .def( + "nonref", + [](const IntPairs &s) { + return py::make_iterator(NonRefIterator>(s.begin()), + NonRefIterator>(s.end())); + }, + py::keep_alive<0, 1>()) + .def( + "nonref_keys", + [](const IntPairs &s) { + return py::make_key_iterator(NonRefIterator>(s.begin()), + NonRefIterator>(s.end())); + }, + py::keep_alive<0, 1>()) + .def( + "nonref_values", + [](const IntPairs &s) { + return py::make_value_iterator(NonRefIterator>(s.begin()), + NonRefIterator>(s.end())); + }, + py::keep_alive<0, 1>()) + + // test single-argument make_iterator + .def( + "simple_iterator", + [](IntPairs &self) { return py::make_iterator(self); }, + py::keep_alive<0, 1>()) + .def( + "simple_keys", + [](IntPairs &self) { return py::make_key_iterator(self); }, + py::keep_alive<0, 1>()) + .def( + "simple_values", + [](IntPairs &self) { return py::make_value_iterator(self); }, + py::keep_alive<0, 1>()) + + // Test iterator with an Extra (doesn't do anything useful, so not used + // at runtime, but tests need to be able to compile with the correct + // overload. See PR #3293. + .def( + "_make_iterator_extras", + [](IntPairs &self) { return py::make_iterator(self, py::call_guard()); }, + py::keep_alive<0, 1>()) + .def( + "_make_key_extras", + [](IntPairs &self) { return py::make_key_iterator(self, py::call_guard()); }, + py::keep_alive<0, 1>()) + .def( + "_make_value_extras", + [](IntPairs &self) { return py::make_value_iterator(self, py::call_guard()); }, + py::keep_alive<0, 1>()); + + // test_iterator_referencing + py::class_(m, "NonCopyableInt") + .def(py::init()) + .def("set", &NonCopyableInt::set) + .def("__int__", &NonCopyableInt::get); + py::class_>(m, "VectorNonCopyableInt") + .def(py::init<>()) + .def("append", + [](std::vector &vec, int value) { vec.emplace_back(value); }) + .def("__iter__", [](std::vector &vec) { + return py::make_iterator(vec.begin(), vec.end()); + }); + py::class_>(m, "VectorNonCopyableIntPair") + .def(py::init<>()) + .def("append", + [](std::vector &vec, const std::pair &value) { + vec.emplace_back(NonCopyableInt(value.first), NonCopyableInt(value.second)); + }) + .def("keys", + [](std::vector &vec) { + return py::make_key_iterator(vec.begin(), vec.end()); + }) + .def("values", [](std::vector &vec) { + return py::make_value_iterator(vec.begin(), vec.end()); + }); + +#if 0 + // Obsolete: special data structure for exposing custom iterator types to python + // kept here for illustrative purposes because there might be some use cases which + // are not covered by the much simpler py::make_iterator + + struct PySequenceIterator { + PySequenceIterator(const Sequence &seq, py::object ref) : seq(seq), ref(ref) { } + + float next() { + if (index == seq.size()) + throw py::stop_iteration(); + return seq[index++]; + } + + const Sequence &seq; + py::object ref; // keep a reference + size_t index = 0; + }; + + py::class_(seq, "Iterator") + .def("__iter__", [](PySequenceIterator &it) -> PySequenceIterator& { return it; }) + .def("__next__", &PySequenceIterator::next); + + On the actual Sequence object, the iterator would be constructed as follows: + .def("__iter__", [](py::object s) { return PySequenceIterator(s.cast(), s); }) +#endif + + // test_python_iterator_in_cpp + m.def("object_to_list", [](const py::object &o) { + auto l = py::list(); + for (auto item : o) { + l.append(item); + } + return l; + }); + + m.def("iterator_to_list", [](py::iterator it) { + auto l = py::list(); + while (it != py::iterator::sentinel()) { + l.append(*it); + ++it; + } + return l; + }); + + // test_sequence_length: check that Python sequences can be converted to py::sequence. + m.def("sequence_length", [](const py::sequence &seq) { return seq.size(); }); + + // Make sure that py::iterator works with std algorithms + m.def("count_none", [](const py::object &o) { + return std::count_if(o.begin(), o.end(), [](py::handle h) { return h.is_none(); }); + }); + + m.def("find_none", [](const py::object &o) { + auto it = std::find_if(o.begin(), o.end(), [](py::handle h) { return h.is_none(); }); + return it->is_none(); + }); + + m.def("count_nonzeros", [](const py::dict &d) { + return std::count_if(d.begin(), d.end(), [](const std::pair &p) { + return p.second.cast() != 0; + }); + }); + + m.def("tuple_iterator", &test_random_access_iterator); + m.def("list_iterator", &test_random_access_iterator); + m.def("sequence_iterator", &test_random_access_iterator); + + // test_iterator_passthrough + // #181: iterator passthrough did not compile + m.def("iterator_passthrough", [](py::iterator s) -> py::iterator { + return py::make_iterator(std::begin(s), std::end(s)); + }); + + // test_iterator_rvp + // #388: Can't make iterators via make_iterator() with different r/v policies + static std::vector list = {1, 2, 3}; + m.def("make_iterator_1", + []() { return py::make_iterator(list); }); + m.def("make_iterator_2", + []() { return py::make_iterator(list); }); + + // test_iterator on c arrays + // #4100: ensure lvalue required as increment operand + class CArrayHolder { + public: + CArrayHolder(double x, double y, double z) { + values[0] = x; + values[1] = y; + values[2] = z; + }; + double values[3]; + }; + + py::class_(m, "CArrayHolder") + .def(py::init()) + .def( + "__iter__", + [](const CArrayHolder &v) { return py::make_iterator(v.values, v.values + 3); }, + py::keep_alive<0, 1>()); +} diff --git a/external_libraries/pybind11/tests/test_sequences_and_iterators.py b/external_libraries/pybind11/tests/test_sequences_and_iterators.py new file mode 100644 index 00000000..41c80e98 --- /dev/null +++ b/external_libraries/pybind11/tests/test_sequences_and_iterators.py @@ -0,0 +1,307 @@ +from __future__ import annotations + +import re + +import pytest +from pytest import approx # noqa: PT013 + +import env +from pybind11_tests import ConstructorStats +from pybind11_tests import sequences_and_iterators as m + + +def test_slice_constructors(): + assert m.make_forward_slice_size_t() == slice(0, -1, 1) + assert m.make_reversed_slice_object() == slice(None, None, -1) + + +@pytest.mark.skipif(not m.has_optional, reason="no ") +def test_slice_constructors_explicit_optional(): + assert m.make_reversed_slice_size_t_optional() == slice(None, None, -1) + assert m.make_reversed_slice_size_t_optional_verbose() == slice(None, None, -1) + + +def test_generalized_iterators(): + assert list(m.IntPairs([(1, 2), (3, 4), (0, 5)]).nonzero()) == [(1, 2), (3, 4)] + assert list(m.IntPairs([(1, 2), (2, 0), (0, 3), (4, 5)]).nonzero()) == [(1, 2)] + assert list(m.IntPairs([(0, 3), (1, 2), (3, 4)]).nonzero()) == [] + + assert list(m.IntPairs([(1, 2), (3, 4), (0, 5)]).nonzero_keys()) == [1, 3] + assert list(m.IntPairs([(1, 2), (2, 0), (0, 3), (4, 5)]).nonzero_keys()) == [1] + assert list(m.IntPairs([(0, 3), (1, 2), (3, 4)]).nonzero_keys()) == [] + + assert list(m.IntPairs([(1, 2), (3, 4), (0, 5)]).nonzero_values()) == [2, 4] + assert list(m.IntPairs([(1, 2), (2, 0), (0, 3), (4, 5)]).nonzero_values()) == [2] + assert list(m.IntPairs([(0, 3), (1, 2), (3, 4)]).nonzero_values()) == [] + + # __next__ must continue to raise StopIteration + it = m.IntPairs([(0, 0)]).nonzero() + for _ in range(3): + with pytest.raises(StopIteration): + next(it) + + it = m.IntPairs([(0, 0)]).nonzero_keys() + for _ in range(3): + with pytest.raises(StopIteration): + next(it) + + +def test_nonref_iterators(): + pairs = m.IntPairs([(1, 2), (3, 4), (0, 5)]) + assert list(pairs.nonref()) == [(1, 2), (3, 4), (0, 5)] + assert list(pairs.nonref_keys()) == [1, 3, 0] + assert list(pairs.nonref_values()) == [2, 4, 5] + + +def test_generalized_iterators_simple(): + assert list(m.IntPairs([(1, 2), (3, 4), (0, 5)]).simple_iterator()) == [ + (1, 2), + (3, 4), + (0, 5), + ] + assert list(m.IntPairs([(1, 2), (3, 4), (0, 5)]).simple_keys()) == [1, 3, 0] + assert list(m.IntPairs([(1, 2), (3, 4), (0, 5)]).simple_values()) == [2, 4, 5] + + +def test_iterator_doc_annotations(): + assert m.IntPairs.nonref.__doc__.endswith( + "-> collections.abc.Iterator[tuple[int, int]]\n" + ) + assert m.IntPairs.nonref_keys.__doc__.endswith("-> collections.abc.Iterator[int]\n") + assert m.IntPairs.nonref_values.__doc__.endswith( + "-> collections.abc.Iterator[int]\n" + ) + assert m.IntPairs.simple_iterator.__doc__.endswith( + "-> collections.abc.Iterator[tuple[int, int]]\n" + ) + assert m.IntPairs.simple_keys.__doc__.endswith("-> collections.abc.Iterator[int]\n") + assert m.IntPairs.simple_values.__doc__.endswith( + "-> collections.abc.Iterator[int]\n" + ) + + +def test_iterator_referencing(): + """Test that iterators reference rather than copy their referents.""" + vec = m.VectorNonCopyableInt() + vec.append(3) + vec.append(5) + assert [int(x) for x in vec] == [3, 5] + # Increment everything to make sure the referents can be mutated + for x in vec: + x.set(int(x) + 1) + assert [int(x) for x in vec] == [4, 6] + + vec = m.VectorNonCopyableIntPair() + vec.append([3, 4]) + vec.append([5, 7]) + assert [int(x) for x in vec.keys()] == [3, 5] + assert [int(x) for x in vec.values()] == [4, 7] + for x in vec.keys(): + x.set(int(x) + 1) + for x in vec.values(): + x.set(int(x) + 10) + assert [int(x) for x in vec.keys()] == [4, 6] + assert [int(x) for x in vec.values()] == [14, 17] + + +def test_sliceable(): + sliceable = m.Sliceable(100) + assert sliceable[::] == (0, 100, 1) + assert sliceable[10::] == (10, 100, 1) + assert sliceable[:10:] == (0, 10, 1) + assert sliceable[::10] == (0, 100, 10) + assert sliceable[-10::] == (90, 100, 1) + assert sliceable[:-10:] == (0, 90, 1) + assert sliceable[::-10] == (99, -1, -10) + assert sliceable[50:60:1] == (50, 60, 1) + assert sliceable[50:60:-1] == (50, 60, -1) + + +def test_sequence(): + cstats = ConstructorStats.get(m.Sequence) + + s = m.Sequence(5) + if not env.GRAALPY: + assert cstats.values() == ["of size", "5"] + + assert "Sequence" in repr(s) + assert len(s) == 5 + assert s[0] == 0 + assert s[3] == 0 + assert 12.34 not in s + s[0], s[3] = 12.34, 56.78 + assert 12.34 in s + assert s[0] == approx(12.34, rel=1e-05) + assert s[3] == approx(56.78, rel=1e-05) + + rev = reversed(s) + if not env.GRAALPY: + assert cstats.values() == ["of size", "5"] + + rev2 = s[::-1] + if not env.GRAALPY: + assert cstats.values() == ["of size", "5"] + + it = iter(m.Sequence(0)) + for _ in range(3): # __next__ must continue to raise StopIteration + with pytest.raises(StopIteration): + next(it) + if not env.GRAALPY: + assert cstats.values() == ["of size", "0"] + + expected = [0, 56.78, 0, 0, 12.34] + assert rev == approx(expected, rel=1e-05) + assert rev2 == approx(expected, rel=1e-05) + assert rev == rev2 + + rev[0::2] = m.Sequence([2.0, 2.0, 2.0]) + if not env.GRAALPY: + assert cstats.values() == ["of size", "3", "from std::vector"] + + assert rev == approx([2, 56.78, 2, 0, 2], rel=1e-05) + + if env.GRAALPY: + pytest.skip("ConstructorStats is incompatible with GraalPy.") + + assert cstats.alive() == 4 + del it + assert cstats.alive() == 3 + del s + assert cstats.alive() == 2 + del rev + assert cstats.alive() == 1 + del rev2 + assert cstats.alive() == 0 + + assert cstats.values() == [] + assert cstats.default_constructions == 0 + assert cstats.copy_constructions == 0 + assert cstats.move_constructions >= 1 + assert cstats.copy_assignments == 0 + assert cstats.move_assignments == 0 + + +def test_sequence_length(): + """#2076: Exception raised by len(arg) should be propagated""" + + class BadLen(RuntimeError): + pass + + class SequenceLike: + def __getitem__(self, i): + return None + + def __len__(self): + raise BadLen() + + with pytest.raises(BadLen): + m.sequence_length(SequenceLike()) + + assert m.sequence_length([1, 2, 3]) == 3 + assert m.sequence_length("hello") == 5 + + +def test_sequence_doc(): + assert ( + m.sequence_length.__doc__.strip() + == "sequence_length(arg0: collections.abc.Sequence) -> int" + ) + + +def test_map_iterator(): + sm = m.StringMap({"hi": "bye", "black": "white"}) + assert sm["hi"] == "bye" + assert len(sm) == 2 + assert sm["black"] == "white" + + with pytest.raises(KeyError): + assert sm["orange"] + sm["orange"] = "banana" + assert sm["orange"] == "banana" + + expected = {"hi": "bye", "black": "white", "orange": "banana"} + for k in sm: + assert sm[k] == expected[k] + for k, v in sm.items(): + assert v == expected[k] + assert list(sm.values()) == [expected[k] for k in sm] + + it = iter(m.StringMap({})) + for _ in range(3): # __next__ must continue to raise StopIteration + with pytest.raises(StopIteration): + next(it) + + +def test_python_iterator_in_cpp(): + t = (1, 2, 3) + assert m.object_to_list(t) == [1, 2, 3] + assert m.object_to_list(iter(t)) == [1, 2, 3] + assert m.iterator_to_list(iter(t)) == [1, 2, 3] + + with pytest.raises(TypeError) as excinfo: + m.object_to_list(1) + assert "object is not iterable" in str(excinfo.value) + + with pytest.raises(TypeError) as excinfo: + m.iterator_to_list(1) + assert "incompatible function arguments" in str(excinfo.value) + + def bad_next_call(): + raise RuntimeError("py::iterator::advance() should propagate errors") + + with pytest.raises(RuntimeError) as excinfo: + m.iterator_to_list(iter(bad_next_call, None)) + assert str(excinfo.value) == "py::iterator::advance() should propagate errors" + + lst = [1, None, 0, None] + assert m.count_none(lst) == 2 + assert m.find_none(lst) is True + assert m.count_nonzeros({"a": 0, "b": 1, "c": 2}) == 2 + + r = range(5) + assert all(m.tuple_iterator(tuple(r))) + assert all(m.list_iterator(list(r))) + assert all(m.sequence_iterator(r)) + + +def test_iterator_passthrough(): + """#181: iterator passthrough did not compile""" + values = [3, 5, 7, 9, 11, 13, 15] + assert list(m.iterator_passthrough(iter(values))) == values + + +def test_iterator_rvp(): + """#388: Can't make iterators via make_iterator() with different r/v policies""" + assert list(m.make_iterator_1()) == [1, 2, 3] + assert list(m.make_iterator_2()) == [1, 2, 3] + assert not isinstance(m.make_iterator_1(), type(m.make_iterator_2())) + + +def test_carray_iterator(): + """#4100: Check for proper iterator overload with C-Arrays""" + args_gt = [float(i) for i in range(3)] + arr_h = m.CArrayHolder(*args_gt) + args = list(arr_h) + assert args_gt == args + + +def test_generated_dunder_methods_pos_only(): + string_map = m.StringMap({"hi": "bye", "black": "white"}) + for it in ( + m.make_iterator_1(), + m.make_iterator_2(), + m.iterator_passthrough(iter([3, 5, 7])), + iter(m.Sequence(5)), + iter(string_map), + string_map.items(), + string_map.values(), + iter(m.CArrayHolder(*[float(i) for i in range(3)])), + ): + assert ( + re.match(r"^__iter__\(self: [\w\.]+, /\)", type(it).__iter__.__doc__) + is not None + ) + assert ( + re.match(r"^__next__\(self: [\w\.]+, /\)", type(it).__next__.__doc__) + is not None + ) diff --git a/external_libraries/pybind11/tests/test_smart_ptr.cpp b/external_libraries/pybind11/tests/test_smart_ptr.cpp new file mode 100644 index 00000000..3617fa3e --- /dev/null +++ b/external_libraries/pybind11/tests/test_smart_ptr.cpp @@ -0,0 +1,627 @@ +/* + tests/test_smart_ptr.cpp -- binding classes with custom reference counting, + implicit conversions between types + + Copyright (c) 2016 Wenzel Jakob + + All rights reserved. Use of this source code is governed by a + BSD-style license that can be found in the LICENSE file. +*/ + +#include "object.h" +#include "pybind11_tests.h" + +// This breaks on PYBIND11_DECLARE_HOLDER_TYPE +PYBIND11_WARNING_DISABLE_GCC("-Wpedantic") + +namespace { + +// This is just a wrapper around unique_ptr, but with extra fields to deliberately bloat up the +// holder size to trigger the non-simple-layout internal instance layout for single inheritance +// with large holder type: +template +class huge_unique_ptr { + std::unique_ptr ptr; + uint64_t padding[10]; + +public: + explicit huge_unique_ptr(T *p) : ptr(p) {} + T *get() { return ptr.get(); } +}; + +// Simple custom holder that works like unique_ptr +template +class custom_unique_ptr { + std::unique_ptr impl; + +public: + explicit custom_unique_ptr(T *p) : impl(p) {} + T *get() const { return impl.get(); } + T *release_ptr() { return impl.release(); } +}; + +// Simple custom holder that works like shared_ptr and has operator& overload +// To obtain address of an instance of this holder pybind should use std::addressof +// Attempt to get address via operator& may leads to segmentation fault +template +class shared_ptr_with_addressof_operator { + std::shared_ptr impl; + +public: + shared_ptr_with_addressof_operator() = default; + explicit shared_ptr_with_addressof_operator(T *p) : impl(p) {} + T *get() const { return impl.get(); } + T **operator&() { throw std::logic_error("Call of overloaded operator& is not expected"); } +}; + +// Simple custom holder that works like unique_ptr and has operator& overload +// To obtain address of an instance of this holder pybind should use std::addressof +// Attempt to get address via operator& may leads to segmentation fault +template +class unique_ptr_with_addressof_operator { + std::unique_ptr impl; + +public: + unique_ptr_with_addressof_operator() = default; + explicit unique_ptr_with_addressof_operator(T *p) : impl(p) {} + T *get() const { return impl.get(); } + T *release_ptr() { return impl.release(); } + T **operator&() { throw std::logic_error("Call of overloaded operator& is not expected"); } +}; + +// Simple custom holder that imitates smart pointer, that always stores cpointer to const +template +class const_only_shared_ptr { + std::shared_ptr ptr_; + +public: + const_only_shared_ptr() = default; + explicit const_only_shared_ptr(const T *ptr) : ptr_(ptr) {} + const T *get() const { return ptr_.get(); } + +private: + // for demonstration purpose only, this imitates smart pointer with a const-only pointer +}; + +// Custom object with builtin reference counting (see 'object.h' for the implementation) +class MyObject1 : public Object { +public: + explicit MyObject1(int value) : value(value) { print_created(this, toString()); } + std::string toString() const override { return "MyObject1[" + std::to_string(value) + "]"; } + +protected: + ~MyObject1() override { print_destroyed(this); } + +private: + int value; +}; + +// Object managed by a std::shared_ptr<> +class MyObject2 { +public: + MyObject2(const MyObject2 &) = default; + explicit MyObject2(int value) : value(value) { print_created(this, toString()); } + std::string toString() const { return "MyObject2[" + std::to_string(value) + "]"; } + virtual ~MyObject2() { print_destroyed(this); } + +private: + int value; +}; + +// Object managed by a std::shared_ptr<>, additionally derives from std::enable_shared_from_this<> +class MyObject3 : public std::enable_shared_from_this { +public: + MyObject3(const MyObject3 &) = default; + explicit MyObject3(int value) : value(value) { print_created(this, toString()); } + std::string toString() const { return "MyObject3[" + std::to_string(value) + "]"; } + virtual ~MyObject3() { print_destroyed(this); } + +private: + int value; +}; + +template +std::unordered_set &pointer_set() { + // https://google.github.io/styleguide/cppguide.html#Static_and_Global_Variables + static auto singleton = new std::unordered_set(); + return *singleton; +} + +// test_unique_nodelete +// Object with a private destructor +class MyObject4 { +public: + explicit MyObject4(int value) : value{value} { + print_created(this); + pointer_set().insert(this); + } + int value; + + static void cleanupAllInstances() { + auto tmp = std::move(pointer_set()); + pointer_set().clear(); + for (auto *o : tmp) { + delete o; + } + } + +private: + ~MyObject4() { + pointer_set().erase(this); + print_destroyed(this); + } +}; + +// test_unique_deleter +// Object with std::unique_ptr where D is not matching the base class +// Object with a protected destructor +class MyObject4a { +public: + explicit MyObject4a(int i) : value{i} { + print_created(this); + pointer_set().insert(this); + }; + MyObject4a(const MyObject4a &) = delete; + + int value; + + static void cleanupAllInstances() { + auto tmp = std::move(pointer_set()); + pointer_set().clear(); + for (auto *o : tmp) { + delete o; + } + } + +protected: + virtual ~MyObject4a() { + pointer_set().erase(this); + print_destroyed(this); + } +}; + +// Object derived but with public destructor and no Deleter in default holder +class MyObject4b : public MyObject4a { +public: + explicit MyObject4b(int i) : MyObject4a(i) { print_created(this); } + MyObject4b(const MyObject4b &) = delete; + ~MyObject4b() override { print_destroyed(this); } +}; + +// test_large_holder +class MyObject5 { // managed by huge_unique_ptr +public: + explicit MyObject5(int value) : value{value} { print_created(this); } + MyObject5(const MyObject5 &) = delete; + ~MyObject5() { print_destroyed(this); } + int value; +}; + +// test const_only_shared_ptr +class MyObject6 { +public: + static const_only_shared_ptr createObject(std::string value) { + return const_only_shared_ptr(new MyObject6(std::move(value))); + } + + const std::string &value() const { return value_; } + +private: + explicit MyObject6(std::string &&value) : value_{std::move(value)} {} + std::string value_; +}; + +// test_shared_ptr_and_references +struct SharedPtrRef { + struct A { + A() { print_created(this); } + A(const A &) { print_copy_created(this); } + A(A &&) noexcept { print_move_created(this); } + ~A() { print_destroyed(this); } + }; + + A value; + std::shared_ptr shared = std::make_shared(); +}; + +// test_shared_ptr_from_this_and_references +struct SharedFromThisRef { + struct B : std::enable_shared_from_this { + B() { print_created(this); } + // NOLINTNEXTLINE(bugprone-copy-constructor-init, readability-redundant-member-init) + B(const B &) : std::enable_shared_from_this() { print_copy_created(this); } + // NOLINTNEXTLINE(readability-redundant-member-init) + B(B &&) noexcept : std::enable_shared_from_this() { print_move_created(this); } + ~B() { print_destroyed(this); } + }; + + B value; + std::shared_ptr shared = std::make_shared(); +}; + +// Issue #865: shared_from_this doesn't work with virtual inheritance +struct SharedFromThisVBase : std::enable_shared_from_this { + SharedFromThisVBase() = default; + SharedFromThisVBase(const SharedFromThisVBase &) = default; + virtual ~SharedFromThisVBase() = default; +}; +struct SharedFromThisVirt : virtual SharedFromThisVBase {}; + +// Issue #5989: static_pointer_cast where dynamic_pointer_cast is needed +// (virtual inheritance with shared_ptr holder) +struct SftVirtBase : std::enable_shared_from_this { + SftVirtBase() = default; + virtual ~SftVirtBase() = default; + static std::shared_ptr create() { return std::make_shared(); } + virtual std::string name() { return "SftVirtBase"; } +}; +struct SftVirtDerived : SftVirtBase { + using SftVirtBase::SftVirtBase; + static std::shared_ptr create() { return std::make_shared(); } + std::string name() override { return "SftVirtDerived"; } +}; +struct SftVirtDerived2 : virtual SftVirtDerived { + using SftVirtDerived::SftVirtDerived; + static std::shared_ptr create() { + return std::make_shared(); + } + std::string name() override { return "SftVirtDerived2"; } + std::string call_name(const std::shared_ptr &d2) { return d2->name(); } +}; + +// test_move_only_holder +struct C { + C() { print_created(this); } + C(const C &) = delete; + ~C() { print_destroyed(this); } +}; + +// test_holder_with_addressof_operator +struct TypeForHolderWithAddressOf { + TypeForHolderWithAddressOf() { print_created(this); } + TypeForHolderWithAddressOf(const TypeForHolderWithAddressOf &) { print_copy_created(this); } + TypeForHolderWithAddressOf(TypeForHolderWithAddressOf &&) noexcept { + print_move_created(this); + } + ~TypeForHolderWithAddressOf() { print_destroyed(this); } + std::string toString() const { + return "TypeForHolderWithAddressOf[" + std::to_string(value) + "]"; + } + int value = 42; +}; + +// test_move_only_holder_with_addressof_operator +struct TypeForMoveOnlyHolderWithAddressOf { + explicit TypeForMoveOnlyHolderWithAddressOf(int value) : value{value} { print_created(this); } + TypeForMoveOnlyHolderWithAddressOf(const TypeForMoveOnlyHolderWithAddressOf &) = delete; + ~TypeForMoveOnlyHolderWithAddressOf() { print_destroyed(this); } + std::string toString() const { + return "MoveOnlyHolderWithAddressOf[" + std::to_string(value) + "]"; + } + int value; +}; + +// test_smart_ptr_from_default +struct HeldByDefaultHolder {}; + +// test_shared_ptr_gc +// #187: issue involving std::shared_ptr<> return value policy & garbage collection +struct ElementBase { + virtual ~ElementBase() = default; /* Force creation of virtual table */ + ElementBase() = default; + ElementBase(const ElementBase &) = delete; +}; + +struct ElementA : ElementBase { + explicit ElementA(int v) : v(v) {} + int value() const { return v; } + int v; +}; + +struct ElementList { + void add(const std::shared_ptr &e) { l.push_back(e); } + std::vector> l; +}; + +} // namespace + +// ref is a wrapper for 'Object' which uses intrusive reference counting +// It is always possible to construct a ref from an Object* pointer without +// possible inconsistencies, hence the 'true' argument at the end. +// Make pybind11 aware of the non-standard getter member function +namespace PYBIND11_NAMESPACE { +namespace detail { +template +struct holder_helper> { + static const T *get(const ref &p) { return p.get_ptr(); } +}; +} // namespace detail +} // namespace PYBIND11_NAMESPACE + +// Make pybind aware of the ref-counted wrapper type (s): +PYBIND11_DECLARE_HOLDER_TYPE(T, ref, true) +PYBIND11_DECLARE_HOLDER_TYPE(T, const_only_shared_ptr, true) +// The following is not required anymore for std::shared_ptr, but it should compile without error: +PYBIND11_DECLARE_HOLDER_TYPE(T, std::shared_ptr) +PYBIND11_DECLARE_HOLDER_TYPE(T, huge_unique_ptr) +PYBIND11_DECLARE_HOLDER_TYPE(T, custom_unique_ptr) +PYBIND11_DECLARE_HOLDER_TYPE(T, shared_ptr_with_addressof_operator) +PYBIND11_DECLARE_HOLDER_TYPE(T, unique_ptr_with_addressof_operator) + +namespace holder_caster_traits_test { +struct example_base {}; +} // namespace holder_caster_traits_test + +PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE) +PYBIND11_NAMESPACE_BEGIN(detail) + +// Negate this condition to demonstrate "ambiguous template instantiation" compilation error: +#if defined(PYBIND11_HAS_INTERNALS_WITH_SMART_HOLDER_SUPPORT) +template +struct copyable_holder_caster_shared_ptr_with_smart_holder_support_enabled< + ExampleType, + enable_if_t::value>> + : std::false_type {}; +#endif + +template +struct copyable_holder_caster< + ExampleType, + HolderType, + enable_if_t::value>> { + static constexpr auto name = const_name(); + + static handle cast(const HolderType &, return_value_policy, handle) { + return str("copyable_holder_caster_traits_test").release(); + } +}; + +// Negate this condition to demonstrate "ambiguous template instantiation" compilation error: +#if defined(PYBIND11_HAS_INTERNALS_WITH_SMART_HOLDER_SUPPORT) +template +struct move_only_holder_caster_unique_ptr_with_smart_holder_support_enabled< + ExampleType, + enable_if_t::value>> + : std::false_type {}; +#endif + +template +struct move_only_holder_caster< + ExampleType, + HolderType, + enable_if_t::value>> { + static constexpr auto name = const_name(); + + static handle cast(const HolderType &, return_value_policy, handle) { + return str("move_only_holder_caster_traits_test").release(); + } +}; + +PYBIND11_NAMESPACE_END(detail) +PYBIND11_NAMESPACE_END(PYBIND11_NAMESPACE) + +namespace holder_caster_traits_test { + +struct example_drvd : example_base {}; + +void wrap(py::module_ &m) { + m.def("return_std_shared_ptr_example_drvd", + // NOLINTNEXTLINE(modernize-make-shared) + []() { return std::shared_ptr(new example_drvd()); }); + m.def("return_std_unique_ptr_example_drvd", + []() { return std::unique_ptr(new example_drvd()); }); +} + +} // namespace holder_caster_traits_test + +TEST_SUBMODULE(smart_ptr, m) { + // Please do not interleave `struct` and `class` definitions with bindings code, + // but implement `struct`s and `class`es in the anonymous namespace above. + // This helps keeping the smart_holder branch in sync with master. + + // test_smart_ptr + + // Object implementation in `object.h` + py::class_> obj(m, "Object"); + obj.def("getRefCount", &Object::getRefCount); + + py::class_>(m, "MyObject1", obj).def(py::init()); + py::implicitly_convertible(); + + m.def("make_object_1", []() -> Object * { return new MyObject1(1); }); + m.def("make_object_2", []() -> ref { return ref(new MyObject1(2)); }); + m.def("make_myobject1_1", []() -> MyObject1 * { return new MyObject1(4); }); + m.def("make_myobject1_2", []() -> ref { return ref(new MyObject1(5)); }); + m.def("print_object_1", [](const Object *obj) { py::print(obj->toString()); }); + m.def("print_object_2", [](ref obj) { py::print(obj->toString()); }); + m.def("print_object_3", [](const ref &obj) { py::print(obj->toString()); }); + m.def("print_object_4", [](const ref *obj) { py::print((*obj)->toString()); }); + m.def("print_myobject1_1", [](const MyObject1 *obj) { py::print(obj->toString()); }); + m.def("print_myobject1_2", [](ref obj) { py::print(obj->toString()); }); + m.def("print_myobject1_3", [](const ref &obj) { py::print(obj->toString()); }); + m.def("print_myobject1_4", [](const ref *obj) { py::print((*obj)->toString()); }); + + // Expose constructor stats for the ref type + m.def("cstats_ref", &ConstructorStats::get); + + py::class_>(m, "MyObject2").def(py::init()); + m.def("make_myobject2_1", []() { return new MyObject2(6); }); + m.def("make_myobject2_2", []() { return std::make_shared(7); }); + m.def("print_myobject2_1", [](const MyObject2 *obj) { py::print(obj->toString()); }); + // NOLINTNEXTLINE(performance-unnecessary-value-param) + m.def("print_myobject2_2", [](std::shared_ptr obj) { py::print(obj->toString()); }); + m.def("print_myobject2_3", + [](const std::shared_ptr &obj) { py::print(obj->toString()); }); + m.def("print_myobject2_4", + [](const std::shared_ptr *obj) { py::print((*obj)->toString()); }); + + py::class_>(m, "MyObject3").def(py::init()); + m.def("make_myobject3_1", []() { return new MyObject3(8); }); + m.def("make_myobject3_2", []() { return std::make_shared(9); }); + m.def("print_myobject3_1", [](const MyObject3 *obj) { py::print(obj->toString()); }); + // NOLINTNEXTLINE(performance-unnecessary-value-param) + m.def("print_myobject3_2", [](std::shared_ptr obj) { py::print(obj->toString()); }); + m.def("print_myobject3_3", + [](const std::shared_ptr &obj) { py::print(obj->toString()); }); + m.def("print_myobject3_4", + [](const std::shared_ptr *obj) { py::print((*obj)->toString()); }); + + // test_smart_ptr_refcounting + m.def("test_object1_refcounting", []() { + auto o = ref(new MyObject1(0)); + bool good = o->getRefCount() == 1; + py::object o2 = py::cast(o, py::return_value_policy::reference); + // always request (partial) ownership for objects with intrusive + // reference counting even when using the 'reference' RVP + good &= o->getRefCount() == 2; + return good; + }); + + // test_unique_nodelete + py::class_>(m, "MyObject4") + .def(py::init()) + .def_readwrite("value", &MyObject4::value) + .def_static("cleanup_all_instances", &MyObject4::cleanupAllInstances); + + // test_unique_deleter + py::class_>(m, "MyObject4a") + .def(py::init()) + .def_readwrite("value", &MyObject4a::value) + .def_static("cleanup_all_instances", &MyObject4a::cleanupAllInstances); + + py::class_>(m, "MyObject4b") + .def(py::init()); + + // test_large_holder + py::class_>(m, "MyObject5") + .def(py::init()) + .def_readwrite("value", &MyObject5::value); + + py::class_>(m, "MyObject6") + .def(py::init([](const std::string &value) { return MyObject6::createObject(value); })) + .def_property_readonly("value", &MyObject6::value); + + // test_shared_ptr_and_references + using A = SharedPtrRef::A; + py::class_>(m, "A"); + py::class_>(m, "SharedPtrRef") + .def(py::init<>()) + .def_readonly("ref", &SharedPtrRef::value) + .def_property_readonly( + "copy", [](const SharedPtrRef &s) { return s.value; }, py::return_value_policy::copy) + .def_readonly("holder_ref", &SharedPtrRef::shared) + .def_property_readonly( + "holder_copy", + [](const SharedPtrRef &s) { return s.shared; }, + py::return_value_policy::copy) + .def("set_ref", [](SharedPtrRef &, const A &) { return true; }) + // NOLINTNEXTLINE(performance-unnecessary-value-param) + .def("set_holder", [](SharedPtrRef &, std::shared_ptr) { return true; }); + + // test_shared_ptr_from_this_and_references + using B = SharedFromThisRef::B; + py::class_>(m, "B"); + py::class_>(m, "SharedFromThisRef") + .def(py::init<>()) + .def_readonly("bad_wp", &SharedFromThisRef::value) + .def_property_readonly("ref", + [](const SharedFromThisRef &s) -> const B & { return *s.shared; }) + .def_property_readonly( + "copy", + [](const SharedFromThisRef &s) { return s.value; }, + py::return_value_policy::copy) + .def_readonly("holder_ref", &SharedFromThisRef::shared) + .def_property_readonly( + "holder_copy", + [](const SharedFromThisRef &s) { return s.shared; }, + py::return_value_policy::copy) + .def("set_ref", [](SharedFromThisRef &, const B &) { return true; }) + // NOLINTNEXTLINE(performance-unnecessary-value-param) + .def("set_holder", [](SharedFromThisRef &, std::shared_ptr) { return true; }); + + // Issue #865: shared_from_this doesn't work with virtual inheritance + static std::shared_ptr sft(new SharedFromThisVirt()); + py::class_>(m, "SharedFromThisVirt") + .def_static("get", []() { return sft.get(); }); + + // Issue #5989: static_pointer_cast where dynamic_pointer_cast is needed + py::class_>(m, "SftVirtBase") + .def(py::init<>(&SftVirtBase::create)) + .def("name", &SftVirtBase::name); + py::class_>(m, "SftVirtDerived") + .def(py::init<>(&SftVirtDerived::create)); + py::class_>( + m, "SftVirtDerived2") + .def(py::init<>(&SftVirtDerived2::create)) + .def("call_name", &SftVirtDerived2::call_name, py::arg("d2")); + + // test_move_only_holder + py::class_>(m, "TypeWithMoveOnlyHolder") + .def_static("make", []() { return custom_unique_ptr(new C); }) + .def_static("make_as_object", []() { return py::cast(custom_unique_ptr(new C)); }); + + // test_holder_with_addressof_operator + using HolderWithAddressOf = shared_ptr_with_addressof_operator; + py::class_(m, "TypeForHolderWithAddressOf") + .def_static("make", []() { return HolderWithAddressOf(new TypeForHolderWithAddressOf); }) + .def("get", [](const HolderWithAddressOf &self) { return self.get(); }) + .def("print_object_1", + [](const TypeForHolderWithAddressOf *obj) { py::print(obj->toString()); }) + // NOLINTNEXTLINE(performance-unnecessary-value-param) + .def("print_object_2", [](HolderWithAddressOf obj) { py::print(obj.get()->toString()); }) + .def("print_object_3", + [](const HolderWithAddressOf &obj) { py::print(obj.get()->toString()); }) + .def("print_object_4", + [](const HolderWithAddressOf *obj) { py::print((*obj).get()->toString()); }); + + // test_move_only_holder_with_addressof_operator + using MoveOnlyHolderWithAddressOf + = unique_ptr_with_addressof_operator; + py::class_( + m, "TypeForMoveOnlyHolderWithAddressOf") + .def_static("make", + []() { + return MoveOnlyHolderWithAddressOf( + new TypeForMoveOnlyHolderWithAddressOf(0)); + }) + .def_readwrite("value", &TypeForMoveOnlyHolderWithAddressOf::value) + .def("print_object", + [](const TypeForMoveOnlyHolderWithAddressOf *obj) { py::print(obj->toString()); }); + + // test_smart_ptr_from_default + py::class_>(m, "HeldByDefaultHolder") + .def(py::init<>()) + // NOLINTNEXTLINE(performance-unnecessary-value-param) + .def_static("load_shared_ptr", [](std::shared_ptr) {}); + + // test_shared_ptr_gc + // #187: issue involving std::shared_ptr<> return value policy & garbage collection + py::class_>(m, "ElementBase"); + + py::class_>(m, "ElementA") + .def(py::init()) + .def("value", &ElementA::value); + + py::class_>(m, "ElementList") + .def(py::init<>()) + .def("add", &ElementList::add) + .def("get", [](ElementList &el) { + py::list list; + for (auto &e : el.l) { + list.append(py::cast(e)); + } + return list; + }); + + // NOLINTNEXTLINE(bugprone-incorrect-enable-shared-from-this) + class PrivateESFT : /* implicit private */ std::enable_shared_from_this {}; + struct ContainerUsingPrivateESFT { + std::shared_ptr ptr; + }; + py::class_(m, "ContainerUsingPrivateESFT") + .def(py::init<>()) + .def_readwrite("ptr", + &ContainerUsingPrivateESFT::ptr); // <- access ESFT through shared_ptr + + holder_caster_traits_test::wrap(m); +} diff --git a/external_libraries/pybind11/tests/test_smart_ptr.py b/external_libraries/pybind11/tests/test_smart_ptr.py new file mode 100644 index 00000000..76ebd8cf --- /dev/null +++ b/external_libraries/pybind11/tests/test_smart_ptr.py @@ -0,0 +1,370 @@ +from __future__ import annotations + +import pytest + +import env # noqa: F401 + +m = pytest.importorskip("pybind11_tests.smart_ptr") +from pybind11_tests import ConstructorStats # noqa: E402 + + +@pytest.mark.skipif("env.GRAALPY", reason="Cannot reliably trigger GC") +def test_smart_ptr(capture): + # Object1 + for i, o in enumerate( + [m.make_object_1(), m.make_object_2(), m.MyObject1(3)], start=1 + ): + assert o.getRefCount() == 1 + with capture: + m.print_object_1(o) + m.print_object_2(o) + m.print_object_3(o) + m.print_object_4(o) + assert capture == f"MyObject1[{i}]\n" * 4 + + for i, o in enumerate( + [m.make_myobject1_1(), m.make_myobject1_2(), m.MyObject1(6), 7], start=4 + ): + print(o) + with capture: + if not isinstance(o, int): + m.print_object_1(o) + m.print_object_2(o) + m.print_object_3(o) + m.print_object_4(o) + m.print_myobject1_1(o) + m.print_myobject1_2(o) + m.print_myobject1_3(o) + m.print_myobject1_4(o) + + times = 4 if isinstance(o, int) else 8 + assert capture == f"MyObject1[{i}]\n" * times + + cstats = ConstructorStats.get(m.MyObject1) + assert cstats.alive() == 0 + expected_values = [f"MyObject1[{i}]" for i in range(1, 7)] + ["MyObject1[7]"] * 4 + assert cstats.values() == expected_values + assert cstats.default_constructions == 0 + assert cstats.copy_constructions == 0 + # assert cstats.move_constructions >= 0 # Doesn't invoke any + assert cstats.copy_assignments == 0 + assert cstats.move_assignments == 0 + + # Object2 + for i, o in zip( + [8, 6, 7], [m.MyObject2(8), m.make_myobject2_1(), m.make_myobject2_2()] + ): + print(o) + with capture: + m.print_myobject2_1(o) + m.print_myobject2_2(o) + m.print_myobject2_3(o) + m.print_myobject2_4(o) + assert capture == f"MyObject2[{i}]\n" * 4 + + cstats = ConstructorStats.get(m.MyObject2) + assert cstats.alive() == 1 + o = None + assert cstats.alive() == 0 + assert cstats.values() == ["MyObject2[8]", "MyObject2[6]", "MyObject2[7]"] + assert cstats.default_constructions == 0 + assert cstats.copy_constructions == 0 + # assert cstats.move_constructions >= 0 # Doesn't invoke any + assert cstats.copy_assignments == 0 + assert cstats.move_assignments == 0 + + # Object3 + for i, o in zip( + [9, 8, 9], [m.MyObject3(9), m.make_myobject3_1(), m.make_myobject3_2()] + ): + print(o) + with capture: + m.print_myobject3_1(o) + m.print_myobject3_2(o) + m.print_myobject3_3(o) + m.print_myobject3_4(o) + assert capture == f"MyObject3[{i}]\n" * 4 + + cstats = ConstructorStats.get(m.MyObject3) + assert cstats.alive() == 1 + o = None + assert cstats.alive() == 0 + assert cstats.values() == ["MyObject3[9]", "MyObject3[8]", "MyObject3[9]"] + assert cstats.default_constructions == 0 + assert cstats.copy_constructions == 0 + # assert cstats.move_constructions >= 0 # Doesn't invoke any + assert cstats.copy_assignments == 0 + assert cstats.move_assignments == 0 + + # Object + cstats = ConstructorStats.get(m.Object) + assert cstats.alive() == 0 + assert cstats.values() == [] + assert cstats.default_constructions == 10 + assert cstats.copy_constructions == 0 + # assert cstats.move_constructions >= 0 # Doesn't invoke any + assert cstats.copy_assignments == 0 + assert cstats.move_assignments == 0 + + # ref<> + cstats = m.cstats_ref() + assert cstats.alive() == 0 + assert cstats.values() == ["from pointer"] * 10 + assert cstats.default_constructions == 30 + assert cstats.copy_constructions == 12 + # assert cstats.move_constructions >= 0 # Doesn't invoke any + assert cstats.copy_assignments == 30 + assert cstats.move_assignments == 0 + + +def test_smart_ptr_refcounting(): + assert m.test_object1_refcounting() + + +@pytest.mark.skipif("env.GRAALPY", reason="Cannot reliably trigger GC") +def test_unique_nodelete(): + o = m.MyObject4(23) + assert o.value == 23 + cstats = ConstructorStats.get(m.MyObject4) + assert cstats.alive() == 1 + del o + assert cstats.alive() == 1 + m.MyObject4.cleanup_all_instances() + assert cstats.alive() == 0 + + +@pytest.mark.skipif("env.GRAALPY", reason="Cannot reliably trigger GC") +def test_unique_nodelete4a(): + o = m.MyObject4a(23) + assert o.value == 23 + cstats = ConstructorStats.get(m.MyObject4a) + assert cstats.alive() == 1 + del o + assert cstats.alive() == 1 + m.MyObject4a.cleanup_all_instances() + assert cstats.alive() == 0 + + +@pytest.mark.skipif("env.GRAALPY", reason="Cannot reliably trigger GC") +def test_unique_deleter(): + m.MyObject4a(0) + o = m.MyObject4b(23) + assert o.value == 23 + cstats4a = ConstructorStats.get(m.MyObject4a) + assert cstats4a.alive() == 2 + cstats4b = ConstructorStats.get(m.MyObject4b) + assert cstats4b.alive() == 1 + del o + assert cstats4a.alive() == 1 # Should now only be one leftover + assert cstats4b.alive() == 0 # Should be deleted + m.MyObject4a.cleanup_all_instances() + assert cstats4a.alive() == 0 + assert cstats4b.alive() == 0 + + +@pytest.mark.skipif("env.GRAALPY", reason="Cannot reliably trigger GC") +def test_large_holder(): + o = m.MyObject5(5) + assert o.value == 5 + cstats = ConstructorStats.get(m.MyObject5) + assert cstats.alive() == 1 + del o + assert cstats.alive() == 0 + + +@pytest.mark.skipif("env.GRAALPY", reason="Cannot reliably trigger GC") +def test_shared_ptr_and_references(): + s = m.SharedPtrRef() + stats = ConstructorStats.get(m.A) + assert stats.alive() == 2 + + ref = s.ref # init_holder_helper(holder_ptr=false, owned=false) + assert stats.alive() == 2 + assert s.set_ref(ref) + with pytest.raises(RuntimeError) as excinfo: + assert s.set_holder(ref) + assert "Unable to cast from non-held to held instance" in str(excinfo.value) + + copy = s.copy # init_holder_helper(holder_ptr=false, owned=true) + assert stats.alive() == 3 + assert s.set_ref(copy) + assert s.set_holder(copy) + + holder_ref = s.holder_ref # init_holder_helper(holder_ptr=true, owned=false) + assert stats.alive() == 3 + assert s.set_ref(holder_ref) + assert s.set_holder(holder_ref) + + holder_copy = s.holder_copy # init_holder_helper(holder_ptr=true, owned=true) + assert stats.alive() == 3 + assert s.set_ref(holder_copy) + assert s.set_holder(holder_copy) + + del ref, copy, holder_ref, holder_copy, s + assert stats.alive() == 0 + + +@pytest.mark.skipif("env.GRAALPY", reason="Cannot reliably trigger GC") +def test_shared_ptr_from_this_and_references(): + s = m.SharedFromThisRef() + stats = ConstructorStats.get(m.B) + assert stats.alive() == 2 + + ref = s.ref # init_holder_helper(holder_ptr=false, owned=false, bad_wp=false) + assert stats.alive() == 2 + assert s.set_ref(ref) + assert s.set_holder( + ref + ) # std::enable_shared_from_this can create a holder from a reference + + bad_wp = s.bad_wp # init_holder_helper(holder_ptr=false, owned=false, bad_wp=true) + assert stats.alive() == 2 + assert s.set_ref(bad_wp) + with pytest.raises(RuntimeError) as excinfo: + assert s.set_holder(bad_wp) + assert "Unable to cast from non-held to held instance" in str(excinfo.value) + + copy = s.copy # init_holder_helper(holder_ptr=false, owned=true, bad_wp=false) + assert stats.alive() == 3 + assert s.set_ref(copy) + assert s.set_holder(copy) + + holder_ref = ( + s.holder_ref + ) # init_holder_helper(holder_ptr=true, owned=false, bad_wp=false) + assert stats.alive() == 3 + assert s.set_ref(holder_ref) + assert s.set_holder(holder_ref) + + holder_copy = ( + s.holder_copy + ) # init_holder_helper(holder_ptr=true, owned=true, bad_wp=false) + assert stats.alive() == 3 + assert s.set_ref(holder_copy) + assert s.set_holder(holder_copy) + + del ref, bad_wp, copy, holder_ref, holder_copy, s + assert stats.alive() == 0 + + z = m.SharedFromThisVirt.get() + y = m.SharedFromThisVirt.get() + assert y is z + + +def test_shared_from_this_virt_shared_ptr_arg(): + """Issue #5989: static_pointer_cast fails with virtual inheritance.""" + b = m.SftVirtBase() + assert b.name() == "SftVirtBase" + + d = m.SftVirtDerived() + assert d.name() == "SftVirtDerived" + + d2 = m.SftVirtDerived2() + assert d2.name() == "SftVirtDerived2" + assert d2.call_name(d2) == "SftVirtDerived2" + + +@pytest.mark.skipif("env.GRAALPY", reason="Cannot reliably trigger GC") +def test_move_only_holder(): + a = m.TypeWithMoveOnlyHolder.make() + b = m.TypeWithMoveOnlyHolder.make_as_object() + stats = ConstructorStats.get(m.TypeWithMoveOnlyHolder) + assert stats.alive() == 2 + del b + assert stats.alive() == 1 + del a + assert stats.alive() == 0 + + +@pytest.mark.skipif("env.GRAALPY", reason="Cannot reliably trigger GC") +def test_holder_with_addressof_operator(): + # this test must not throw exception from c++ + a = m.TypeForHolderWithAddressOf.make() + a.print_object_1() + a.print_object_2() + a.print_object_3() + a.print_object_4() + + stats = ConstructorStats.get(m.TypeForHolderWithAddressOf) + assert stats.alive() == 1 + + np = m.TypeForHolderWithAddressOf.make() + assert stats.alive() == 2 + del a + assert stats.alive() == 1 + del np + assert stats.alive() == 0 + + b = m.TypeForHolderWithAddressOf.make() + c = b + assert b.get() is c.get() + assert stats.alive() == 1 + + del b + assert stats.alive() == 1 + + del c + assert stats.alive() == 0 + + +@pytest.mark.skipif("env.GRAALPY", reason="Cannot reliably trigger GC") +def test_move_only_holder_with_addressof_operator(): + a = m.TypeForMoveOnlyHolderWithAddressOf.make() + a.print_object() + + stats = ConstructorStats.get(m.TypeForMoveOnlyHolderWithAddressOf) + assert stats.alive() == 1 + + a.value = 42 + assert a.value == 42 + + del a + assert stats.alive() == 0 + + +def test_smart_ptr_from_default(): + instance = m.HeldByDefaultHolder() + with pytest.raises(RuntimeError) as excinfo: + m.HeldByDefaultHolder.load_shared_ptr(instance) + assert "Unable to load a custom holder type from a default-holder instance" in str( + excinfo.value + ) + + +def test_shared_ptr_gc(): + """#187: issue involving std::shared_ptr<> return value policy & garbage collection""" + el = m.ElementList() + for i in range(10): + el.add(m.ElementA(i)) + pytest.gc_collect() + for i, v in enumerate(el.get()): + assert i == v.value() + + +def test_private_esft_tolerance(): + # Regression test: binding a shared_ptr member where T privately inherits + # enable_shared_from_this must not cause a C++ compile error. + c = m.ContainerUsingPrivateESFT() + # The ptr member is not actually usable in any way, but this is how the + # pybind11 v2 release series worked. + with pytest.raises(TypeError): + _ = c.ptr # getattr + with pytest.raises(TypeError): + c.ptr = None # setattr + + +def test_copyable_holder_caster_shared_ptr_with_smart_holder_support_enabled(): + assert ( + m.return_std_shared_ptr_example_drvd() == "copyable_holder_caster_traits_test" + ) + + +def test_move_only_holder_caster_shared_ptr_with_smart_holder_support_enabled(): + assert ( + m.return_std_unique_ptr_example_drvd() == "move_only_holder_caster_traits_test" + ) + + +def test_const_only_holder(): + o = m.MyObject6("my_data") + assert o.value == "my_data" diff --git a/external_libraries/pybind11/tests/test_standalone_enum_module.py b/external_libraries/pybind11/tests/test_standalone_enum_module.py new file mode 100644 index 00000000..3358b887 --- /dev/null +++ b/external_libraries/pybind11/tests/test_standalone_enum_module.py @@ -0,0 +1,18 @@ +from __future__ import annotations + +import os + +import env + + +def test_enum_import_exit_no_crash(): + # Added in PR #6015. Modeled after reproducer under issue #5976 + env.check_script_success_in_subprocess( + f""" + import sys + sys.path.insert(0, {os.path.dirname(env.__file__)!r}) + import standalone_enum_module as m + assert m.SomeEnum.__class__.__name__ == "pybind11_type" + """, + rerun=1, + ) diff --git a/external_libraries/pybind11/tests/test_stl.cpp b/external_libraries/pybind11/tests/test_stl.cpp new file mode 100644 index 00000000..6084d517 --- /dev/null +++ b/external_libraries/pybind11/tests/test_stl.cpp @@ -0,0 +1,667 @@ +/* + tests/test_stl.cpp -- STL type casters + + Copyright (c) 2017 Wenzel Jakob + + All rights reserved. Use of this source code is governed by a + BSD-style license that can be found in the LICENSE file. +*/ + +#include + +#include "constructor_stats.h" +#include "pybind11_tests.h" + +#if defined(PYBIND11_HAS_FILESYSTEM) || defined(PYBIND11_HAS_EXPERIMENTAL_FILESYSTEM) +# include +#endif + +#include + +#include +#include + +#if defined(PYBIND11_TEST_BOOST) +# include + +namespace PYBIND11_NAMESPACE { +namespace detail { +template +struct type_caster> : optional_caster> {}; + +template <> +struct type_caster : void_caster {}; +} // namespace detail +} // namespace PYBIND11_NAMESPACE +#endif + +// Test with `std::variant` in C++17 mode, or with `boost::variant` in C++11/14 +#if defined(PYBIND11_HAS_VARIANT) +using std::variant; +# define PYBIND11_TEST_VARIANT 1 +#elif defined(PYBIND11_TEST_BOOST) +# include +# define PYBIND11_TEST_VARIANT 1 +using boost::variant; + +namespace PYBIND11_NAMESPACE { +namespace detail { +template +struct type_caster> : variant_caster> {}; + +template <> +struct visit_helper { + template + static auto call(Args &&...args) -> decltype(boost::apply_visitor(args...)) { + return boost::apply_visitor(args...); + } +}; +} // namespace detail +} // namespace PYBIND11_NAMESPACE +#endif + +PYBIND11_MAKE_OPAQUE(std::vector>) + +/// Issue #528: templated constructor +struct TplCtorClass { + template + explicit TplCtorClass(const T &) {} + bool operator==(const TplCtorClass &) const { return true; } +}; + +namespace std { +template <> +struct hash { + size_t operator()(const TplCtorClass &) const { return 0; } +}; +} // namespace std + +template