Merge branch 'master' into sh_merge_master

This commit is contained in:
Ralf W. Grosse-Kunstleve 2022-10-30 13:26:32 -07:00
commit 4928b03bae
28 changed files with 671 additions and 104 deletions

View File

@ -6,7 +6,8 @@ body:
- type: markdown - type: markdown
attributes: attributes:
value: | value: |
Maintainers will only make a best effort to triage PRs. Please do your best to make the issue as easy to act on as possible, and only open if clearly a problem with pybind11 (ask first if unsure). Please do your best to make the issue as easy to act on as possible, and only submit here if there is clearly a problem with pybind11 (ask first if unsure). **Note that a reproducer in a PR is much more likely to get immediate attention.**
- type: checkboxes - type: checkboxes
id: steps id: steps
attributes: attributes:
@ -20,6 +21,12 @@ body:
- label: Consider asking first in the [Gitter chat room](https://gitter.im/pybind/Lobby) or in a [Discussion](https:/pybind/pybind11/discussions/new). - label: Consider asking first in the [Gitter chat room](https://gitter.im/pybind/Lobby) or in a [Discussion](https:/pybind/pybind11/discussions/new).
required: false required: false
- type: Input
id: version
attributes:
label: What version (or hash if on master) of pybind11 are you using?
required: true
- type: textarea - type: textarea
id: description id: description
attributes: attributes:
@ -40,6 +47,14 @@ body:
The code should be minimal, have no external dependencies, isolate the The code should be minimal, have no external dependencies, isolate the
function(s) that cause breakage. Submit matched and complete C++ and function(s) that cause breakage. Submit matched and complete C++ and
Python snippets that can be easily compiled and run to diagnose the Python snippets that can be easily compiled and run to diagnose the
issue. If possible, make a PR with a new, failing test to give us a issue. — Note that a reproducer in a PR is much more likely to get
starting point to work on! immediate attention: failing tests in the pybind11 CI are the best
starting point for working out fixes.
render: text render: text
- type: Input
id: regression
attributes:
label: Is this a regression? Put the last known working version here if it is.
description: Put the last known working version here if this is a regression.
value: Not a regression

View File

@ -31,7 +31,7 @@ jobs:
- '3.6' - '3.6'
- '3.9' - '3.9'
- '3.10' - '3.10'
- '3.11-dev' - '3.11'
- 'pypy-3.7' - 'pypy-3.7'
- 'pypy-3.8' - 'pypy-3.8'
- 'pypy-3.9' - 'pypy-3.9'
@ -103,10 +103,12 @@ jobs:
run: python -m pip install pytest-github-actions-annotate-failures run: python -m pip install pytest-github-actions-annotate-failures
# First build - C++11 mode and inplace # First build - C++11 mode and inplace
# More-or-less randomly adding -DPYBIND11_SIMPLE_GIL_MANAGEMENT=ON here.
- name: Configure C++11 ${{ matrix.args }} - name: Configure C++11 ${{ matrix.args }}
run: > run: >
cmake -S . -B . cmake -S . -B .
-DPYBIND11_WERROR=ON -DPYBIND11_WERROR=ON
-DPYBIND11_SIMPLE_GIL_MANAGEMENT=ON
-DDOWNLOAD_CATCH=ON -DDOWNLOAD_CATCH=ON
-DDOWNLOAD_EIGEN=ON -DDOWNLOAD_EIGEN=ON
-DCMAKE_CXX_STANDARD=11 -DCMAKE_CXX_STANDARD=11
@ -120,7 +122,7 @@ jobs:
- name: C++11 tests - name: C++11 tests
# TODO: Figure out how to load the DLL on Python 3.8+ # TODO: Figure out how to load the DLL on Python 3.8+
if: "!(runner.os == 'Windows' && (matrix.python == 3.8 || matrix.python == 3.9 || matrix.python == '3.10' || matrix.python == '3.11-dev' || matrix.python == 'pypy-3.8'))" if: "!(runner.os == 'Windows' && (matrix.python == 3.8 || matrix.python == 3.9 || matrix.python == '3.10' || matrix.python == '3.11' || matrix.python == 'pypy-3.8'))"
run: cmake --build . --target cpptest -j 2 run: cmake --build . --target cpptest -j 2
- name: Interface test C++11 - name: Interface test C++11
@ -130,10 +132,12 @@ jobs:
run: git clean -fdx run: git clean -fdx
# Second build - C++17 mode and in a build directory # Second build - C++17 mode and in a build directory
# More-or-less randomly adding -DPYBIND11_SIMPLE_GIL_MANAGEMENT=OFF here.
- name: Configure C++17 - name: Configure C++17
run: > run: >
cmake -S . -B build2 cmake -S . -B build2
-DPYBIND11_WERROR=ON -DPYBIND11_WERROR=ON
-DPYBIND11_SIMPLE_GIL_MANAGEMENT=OFF
-DDOWNLOAD_CATCH=ON -DDOWNLOAD_CATCH=ON
-DDOWNLOAD_EIGEN=ON -DDOWNLOAD_EIGEN=ON
-DCMAKE_CXX_STANDARD=17 -DCMAKE_CXX_STANDARD=17
@ -147,7 +151,7 @@ jobs:
- name: C++ tests - name: C++ tests
# TODO: Figure out how to load the DLL on Python 3.8+ # TODO: Figure out how to load the DLL on Python 3.8+
if: "!(runner.os == 'Windows' && (matrix.python == 3.8 || matrix.python == 3.9 || matrix.python == '3.10' || matrix.python == '3.11-dev' || matrix.python == 'pypy-3.8'))" if: "!(runner.os == 'Windows' && (matrix.python == 3.8 || matrix.python == 3.9 || matrix.python == '3.10' || matrix.python == '3.11' || matrix.python == 'pypy-3.8'))"
run: cmake --build build2 --target cpptest run: cmake --build build2 --target cpptest
# Third build - C++17 mode with unstable ABI # Third build - C++17 mode with unstable ABI
@ -187,7 +191,7 @@ jobs:
- python-version: "3.9" - python-version: "3.9"
python-debug: true python-debug: true
valgrind: true valgrind: true
- python-version: "3.11-dev" - python-version: "3.11"
python-debug: false python-debug: false
name: "🐍 ${{ matrix.python-version }}${{ matrix.python-debug && '-dbg' || '' }} (deadsnakes)${{ matrix.valgrind && ' • Valgrind' || '' }} • x64" name: "🐍 ${{ matrix.python-version }}${{ matrix.python-debug && '-dbg' || '' }} (deadsnakes)${{ matrix.valgrind && ' • Valgrind' || '' }} • x64"
@ -392,7 +396,7 @@ jobs:
# Testing on CentOS 7 + PGI compilers, which seems to require more workarounds # Testing on CentOS 7 + PGI compilers, which seems to require more workarounds
centos-nvhpc7: centos-nvhpc7:
runs-on: ubuntu-latest runs-on: ubuntu-latest
name: "🐍 3 • CentOS7 / PGI 22.3 • x64" name: "🐍 3 • CentOS7 / PGI 22.9 • x64"
container: centos:7 container: centos:7
steps: steps:
@ -402,7 +406,7 @@ jobs:
run: yum update -y && yum install -y epel-release && yum install -y git python3-devel make environment-modules cmake3 yum-utils run: yum update -y && yum install -y epel-release && yum install -y git python3-devel make environment-modules cmake3 yum-utils
- name: Install NVidia HPC SDK - name: Install NVidia HPC SDK
run: yum-config-manager --add-repo https://developer.download.nvidia.com/hpc-sdk/rhel/nvhpc.repo && yum -y install nvhpc-22.3 run: yum-config-manager --add-repo https://developer.download.nvidia.com/hpc-sdk/rhel/nvhpc.repo && yum -y install nvhpc-22.9
# On CentOS 7, we have to filter a few tests (compiler internal error) # On CentOS 7, we have to filter a few tests (compiler internal error)
# and allow deeper template recursion (not needed on CentOS 8 with a newer # and allow deeper template recursion (not needed on CentOS 8 with a newer
@ -412,12 +416,12 @@ jobs:
shell: bash shell: bash
run: | run: |
source /etc/profile.d/modules.sh source /etc/profile.d/modules.sh
module load /opt/nvidia/hpc_sdk/modulefiles/nvhpc/22.3 module load /opt/nvidia/hpc_sdk/modulefiles/nvhpc/22.9
cmake3 -S . -B build -DDOWNLOAD_CATCH=ON \ cmake3 -S . -B build -DDOWNLOAD_CATCH=ON \
-DCMAKE_CXX_STANDARD=11 \ -DCMAKE_CXX_STANDARD=11 \
-DPYTHON_EXECUTABLE=$(python3 -c "import sys; print(sys.executable)") \ -DPYTHON_EXECUTABLE=$(python3 -c "import sys; print(sys.executable)") \
-DCMAKE_CXX_FLAGS="-Wc,--pending_instantiations=0" \ -DCMAKE_CXX_FLAGS="-Wc,--pending_instantiations=0" \
-DPYBIND11_TEST_FILTER="test_smart_ptr.cpp;test_virtual_functions.cpp" -DPYBIND11_TEST_FILTER="test_smart_ptr.cpp"
# Building before installing Pip should produce a warning but not an error # Building before installing Pip should produce a warning but not an error
- name: Build - name: Build

View File

@ -10,7 +10,11 @@ jobs:
steps: steps:
- uses: actions/labeler@main - uses: actions/labeler@main
if: github.event.pull_request.merged == true if: >
github.event.pull_request.merged == true &&
!startsWith(github.event.pull_request.title, 'chore(deps):') &&
!startsWith(github.event.pull_request.title, 'ci(fix):') &&
!startsWith(github.event.pull_request.title, 'docs(changelog):')
with: with:
repo-token: ${{ secrets.GITHUB_TOKEN }} repo-token: ${{ secrets.GITHUB_TOKEN }}
configuration-path: .github/labeler_merged.yml configuration-path: .github/labeler_merged.yml

View File

@ -91,10 +91,16 @@ endif()
option(PYBIND11_INSTALL "Install pybind11 header files?" ${PYBIND11_MASTER_PROJECT}) option(PYBIND11_INSTALL "Install pybind11 header files?" ${PYBIND11_MASTER_PROJECT})
option(PYBIND11_TEST "Build pybind11 test suite?" ${PYBIND11_MASTER_PROJECT}) option(PYBIND11_TEST "Build pybind11 test suite?" ${PYBIND11_MASTER_PROJECT})
option(PYBIND11_NOPYTHON "Disable search for Python" OFF) option(PYBIND11_NOPYTHON "Disable search for Python" OFF)
option(PYBIND11_SIMPLE_GIL_MANAGEMENT
"Use simpler GIL management logic that does not support disassociation" OFF)
set(PYBIND11_INTERNALS_VERSION set(PYBIND11_INTERNALS_VERSION
"" ""
CACHE STRING "Override the ABI version, may be used to enable the unstable ABI.") CACHE STRING "Override the ABI version, may be used to enable the unstable ABI.")
if(PYBIND11_SIMPLE_GIL_MANAGEMENT)
add_compile_definitions(PYBIND11_SIMPLE_GIL_MANAGEMENT)
endif()
cmake_dependent_option( cmake_dependent_option(
USE_PYTHON_INCLUDE_DIR USE_PYTHON_INCLUDE_DIR
"Install pybind11 headers in Python include directory instead of default installation prefix" "Install pybind11 headers in Python include directory instead of default installation prefix"

View File

@ -16,6 +16,104 @@ IN DEVELOPMENT
Changes will be summarized here periodically. Changes will be summarized here periodically.
Version 2.10.1 (Oct 2?, 2022)
-----------------------------
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>`_
Bug fixes:
* 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>`_
Build system improvements:
* 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) Version 2.10.0 (Jul 15, 2022)
----------------------------- -----------------------------

View File

@ -278,7 +278,7 @@ public:
return false; return false;
} }
static handle cast(T, return_value_policy /* policy */, handle /* parent */) { static handle cast(T, return_value_policy /* policy */, handle /* parent */) {
return none().inc_ref(); return none().release();
} }
PYBIND11_TYPE_CASTER(T, const_name("None")); PYBIND11_TYPE_CASTER(T, const_name("None"));
}; };
@ -321,7 +321,7 @@ public:
if (ptr) { if (ptr) {
return capsule(ptr).release(); return capsule(ptr).release();
} }
return none().inc_ref(); return none().release();
} }
template <typename T> template <typename T>
@ -571,7 +571,7 @@ public:
static handle cast(const CharT *src, return_value_policy policy, handle parent) { static handle cast(const CharT *src, return_value_policy policy, handle parent) {
if (src == nullptr) { if (src == nullptr) {
return pybind11::none().inc_ref(); return pybind11::none().release();
} }
return StringCaster::cast(StringType(src), policy, parent); return StringCaster::cast(StringType(src), policy, parent);
} }
@ -1220,11 +1220,9 @@ enable_if_t<cast_is_temporary_value_reference<T>::value, T> cast_safe(object &&)
pybind11_fail("Internal error: cast_safe fallback invoked"); pybind11_fail("Internal error: cast_safe fallback invoked");
} }
template <typename T> template <typename T>
enable_if_t<std::is_same<void, intrinsic_t<T>>::value, void> cast_safe(object &&) {} enable_if_t<std::is_void<T>::value, void> cast_safe(object &&) {}
template <typename T> template <typename T>
enable_if_t<detail::none_of<cast_is_temporary_value_reference<T>, enable_if_t<detail::none_of<cast_is_temporary_value_reference<T>, std::is_void<T>>::value, T>
std::is_same<void, intrinsic_t<T>>>::value,
T>
cast_safe(object &&o) { cast_safe(object &&o) {
return pybind11::cast<T>(std::move(o)); return pybind11::cast<T>(std::move(o));
} }

View File

@ -205,11 +205,8 @@
# endif # endif
#endif #endif
#if defined(__cpp_lib_char8_t) && __cpp_lib_char8_t >= 201811L
# define PYBIND11_HAS_U8STRING
#endif
#include <Python.h> #include <Python.h>
// Reminder: WITH_THREAD is always defined if PY_VERSION_HEX >= 0x03070000
#if PY_VERSION_HEX < 0x03060000 #if PY_VERSION_HEX < 0x03060000
# error "PYTHON < 3.6 IS UNSUPPORTED. pybind11 v2.9 was the last to support Python 2 and 3.5." # error "PYTHON < 3.6 IS UNSUPPORTED. pybind11 v2.9 was the last to support Python 2 and 3.5."
#endif #endif
@ -233,6 +230,10 @@
# undef copysign # undef copysign
#endif #endif
#if defined(PYPY_VERSION) && !defined(PYBIND11_SIMPLE_GIL_MANAGEMENT)
# define PYBIND11_SIMPLE_GIL_MANAGEMENT
#endif
#if defined(_MSC_VER) #if defined(_MSC_VER)
# if defined(PYBIND11_DEBUG_MARKER) # if defined(PYBIND11_DEBUG_MARKER)
# define _DEBUG # define _DEBUG
@ -259,6 +260,11 @@
# endif # endif
#endif #endif
// Must be after including <version> 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
#endif
// #define PYBIND11_STR_LEGACY_PERMISSIVE // #define PYBIND11_STR_LEGACY_PERMISSIVE
// If DEFINED, pybind11::str can hold PyUnicodeObject or PyBytesObject // If DEFINED, pybind11::str can hold PyUnicodeObject or PyBytesObject
// (probably surprising and never documented, but this was the // (probably surprising and never documented, but this was the

View File

@ -9,6 +9,12 @@
#pragma once #pragma once
#include "common.h"
#if defined(WITH_THREAD) && defined(PYBIND11_SIMPLE_GIL_MANAGEMENT)
# include "../gil.h"
#endif
#include "../pytypes.h" #include "../pytypes.h"
#include "smart_holder_sfinae_hooks_only.h" #include "smart_holder_sfinae_hooks_only.h"
@ -50,7 +56,7 @@ inline PyObject *make_object_base_type(PyTypeObject *metaclass);
// `Py_LIMITED_API` anyway. // `Py_LIMITED_API` anyway.
# if PYBIND11_INTERNALS_VERSION > 4 # if PYBIND11_INTERNALS_VERSION > 4
# define PYBIND11_TLS_KEY_REF Py_tss_t & # define PYBIND11_TLS_KEY_REF Py_tss_t &
# ifdef __GNUC__ # if defined(__GNUC__) && !defined(__INTEL_COMPILER)
// Clang on macOS warns due to `Py_tss_NEEDS_INIT` not specifying an initializer // Clang on macOS warns due to `Py_tss_NEEDS_INIT` not specifying an initializer
// for every field. // for every field.
# define PYBIND11_TLS_KEY_INIT(var) \ # define PYBIND11_TLS_KEY_INIT(var) \
@ -170,10 +176,12 @@ struct internals {
PyTypeObject *default_metaclass; PyTypeObject *default_metaclass;
PyObject *instance_base; PyObject *instance_base;
#if defined(WITH_THREAD) #if defined(WITH_THREAD)
// Unused if PYBIND11_SIMPLE_GIL_MANAGEMENT is defined:
PYBIND11_TLS_KEY_INIT(tstate) PYBIND11_TLS_KEY_INIT(tstate)
# if PYBIND11_INTERNALS_VERSION > 4 # if PYBIND11_INTERNALS_VERSION > 4
PYBIND11_TLS_KEY_INIT(loader_life_support_tls_key) PYBIND11_TLS_KEY_INIT(loader_life_support_tls_key)
# endif // PYBIND11_INTERNALS_VERSION > 4 # endif // PYBIND11_INTERNALS_VERSION > 4
// Unused if PYBIND11_SIMPLE_GIL_MANAGEMENT is defined:
PyInterpreterState *istate = nullptr; PyInterpreterState *istate = nullptr;
~internals() { ~internals() {
# if PYBIND11_INTERNALS_VERSION > 4 # if PYBIND11_INTERNALS_VERSION > 4
@ -419,6 +427,10 @@ PYBIND11_NOINLINE internals &get_internals() {
return **internals_pp; return **internals_pp;
} }
#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. // 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. // Cannot use py::gil_scoped_acquire here since that constructor calls get_internals.
struct gil_scoped_acquire_local { struct gil_scoped_acquire_local {
@ -428,6 +440,8 @@ PYBIND11_NOINLINE internals &get_internals() {
~gil_scoped_acquire_local() { PyGILState_Release(state); } ~gil_scoped_acquire_local() { PyGILState_Release(state); }
const PyGILState_STATE state; const PyGILState_STATE state;
} gil; } gil;
# endif
#endif
error_scope err_scope; error_scope err_scope;
PYBIND11_STR_TYPE id(PYBIND11_INTERNALS_ID); PYBIND11_STR_TYPE id(PYBIND11_INTERNALS_ID);

View File

@ -11,7 +11,6 @@
#include "../numpy.h" #include "../numpy.h"
// Similar to comments & pragma block in eigen_tensor.h. PLEASE KEEP IN SYNC.
/* HINT: To suppress warnings originating from the Eigen headers, use -isystem. /* HINT: To suppress warnings originating from the Eigen headers, use -isystem.
See also: See also:
https://stackoverflow.com/questions/2579576/i-dir-vs-isystem-dir https://stackoverflow.com/questions/2579576/i-dir-vs-isystem-dir

View File

@ -110,7 +110,7 @@ public:
template <typename Func> template <typename Func>
static handle cast(Func &&f_, return_value_policy policy, handle /* parent */) { static handle cast(Func &&f_, return_value_policy policy, handle /* parent */) {
if (!f_) { if (!f_) {
return none().inc_ref(); return none().release();
} }
auto result = f_.template target<function_type>(); auto result = f_.template target<function_type>();

View File

@ -10,7 +10,10 @@
#pragma once #pragma once
#include "detail/common.h" #include "detail/common.h"
#include "detail/internals.h"
#if defined(WITH_THREAD) && !defined(PYBIND11_SIMPLE_GIL_MANAGEMENT)
# include "detail/internals.h"
#endif
PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE) PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE)
@ -21,7 +24,9 @@ PyThreadState *get_thread_state_unchecked();
PYBIND11_NAMESPACE_END(detail) PYBIND11_NAMESPACE_END(detail)
#if defined(WITH_THREAD) && !defined(PYPY_VERSION) #if defined(WITH_THREAD)
# if !defined(PYBIND11_SIMPLE_GIL_MANAGEMENT)
/* The functions below essentially reproduce the PyGILState_* API using a RAII /* The functions below essentially reproduce the PyGILState_* API using a RAII
* pattern, but there are a few important differences: * pattern, but there are a few important differences:
@ -62,11 +67,11 @@ public:
if (!tstate) { if (!tstate) {
tstate = PyThreadState_New(internals.istate); tstate = PyThreadState_New(internals.istate);
# if defined(PYBIND11_DETAILED_ERROR_MESSAGES) # if defined(PYBIND11_DETAILED_ERROR_MESSAGES)
if (!tstate) { if (!tstate) {
pybind11_fail("scoped_acquire: could not create thread state!"); pybind11_fail("scoped_acquire: could not create thread state!");
} }
# endif # endif
tstate->gilstate_counter = 0; tstate->gilstate_counter = 0;
PYBIND11_TLS_REPLACE_VALUE(internals.tstate, tstate); PYBIND11_TLS_REPLACE_VALUE(internals.tstate, tstate);
} else { } else {
@ -87,20 +92,20 @@ public:
PYBIND11_NOINLINE void dec_ref() { PYBIND11_NOINLINE void dec_ref() {
--tstate->gilstate_counter; --tstate->gilstate_counter;
# if defined(PYBIND11_DETAILED_ERROR_MESSAGES) # if defined(PYBIND11_DETAILED_ERROR_MESSAGES)
if (detail::get_thread_state_unchecked() != tstate) { if (detail::get_thread_state_unchecked() != tstate) {
pybind11_fail("scoped_acquire::dec_ref(): thread state must be current!"); pybind11_fail("scoped_acquire::dec_ref(): thread state must be current!");
} }
if (tstate->gilstate_counter < 0) { if (tstate->gilstate_counter < 0) {
pybind11_fail("scoped_acquire::dec_ref(): reference count underflow!"); pybind11_fail("scoped_acquire::dec_ref(): reference count underflow!");
} }
# endif # endif
if (tstate->gilstate_counter == 0) { if (tstate->gilstate_counter == 0) {
# if defined(PYBIND11_DETAILED_ERROR_MESSAGES) # if defined(PYBIND11_DETAILED_ERROR_MESSAGES)
if (!release) { if (!release) {
pybind11_fail("scoped_acquire::dec_ref(): internal error!"); pybind11_fail("scoped_acquire::dec_ref(): internal error!");
} }
# endif # endif
PyThreadState_Clear(tstate); PyThreadState_Clear(tstate);
if (active) { if (active) {
PyThreadState_DeleteCurrent(); PyThreadState_DeleteCurrent();
@ -178,12 +183,14 @@ private:
bool disassoc; bool disassoc;
bool active = true; bool active = true;
}; };
#elif defined(PYPY_VERSION)
# else // PYBIND11_SIMPLE_GIL_MANAGEMENT
class gil_scoped_acquire { class gil_scoped_acquire {
PyGILState_STATE state; PyGILState_STATE state;
public: public:
gil_scoped_acquire() { state = PyGILState_Ensure(); } gil_scoped_acquire() : state{PyGILState_Ensure()} {}
gil_scoped_acquire(const gil_scoped_acquire &) = delete; gil_scoped_acquire(const gil_scoped_acquire &) = delete;
gil_scoped_acquire &operator=(const gil_scoped_acquire &) = delete; gil_scoped_acquire &operator=(const gil_scoped_acquire &) = delete;
~gil_scoped_acquire() { PyGILState_Release(state); } ~gil_scoped_acquire() { PyGILState_Release(state); }
@ -194,19 +201,39 @@ class gil_scoped_release {
PyThreadState *state; PyThreadState *state;
public: public:
gil_scoped_release() { state = PyEval_SaveThread(); } gil_scoped_release() : state{PyEval_SaveThread()} {}
gil_scoped_release(const gil_scoped_release &) = delete; gil_scoped_release(const gil_scoped_release &) = delete;
gil_scoped_release &operator=(const gil_scoped_acquire &) = delete; gil_scoped_release &operator=(const gil_scoped_acquire &) = delete;
~gil_scoped_release() { PyEval_RestoreThread(state); } ~gil_scoped_release() { PyEval_RestoreThread(state); }
void disarm() {} void disarm() {}
}; };
#else
# endif // PYBIND11_SIMPLE_GIL_MANAGEMENT
#else // WITH_THREAD
class gil_scoped_acquire { 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() {} void disarm() {}
}; };
class gil_scoped_release { 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_acquire &) = delete;
void disarm() {} void disarm() {}
}; };
#endif
#endif // WITH_THREAD
PYBIND11_NAMESPACE_END(PYBIND11_NAMESPACE) PYBIND11_NAMESPACE_END(PYBIND11_NAMESPACE)

View File

@ -2588,7 +2588,7 @@ template <typename Access,
typename Sentinel, typename Sentinel,
typename ValueType, typename ValueType,
typename... Extra> typename... Extra>
iterator make_iterator_impl(Iterator &&first, Sentinel &&last, Extra &&...extra) { iterator make_iterator_impl(Iterator first, Sentinel last, Extra &&...extra) {
using state = detail::iterator_state<Access, Policy, Iterator, Sentinel, ValueType, Extra...>; using state = detail::iterator_state<Access, Policy, Iterator, Sentinel, ValueType, Extra...>;
// TODO: state captures only the types of Extra, not the values // TODO: state captures only the types of Extra, not the values
@ -2614,7 +2614,7 @@ iterator make_iterator_impl(Iterator &&first, Sentinel &&last, Extra &&...extra)
Policy); Policy);
} }
return cast(state{std::forward<Iterator>(first), std::forward<Sentinel>(last), true}); return cast(state{first, last, true});
} }
PYBIND11_NAMESPACE_END(detail) PYBIND11_NAMESPACE_END(detail)
@ -2625,15 +2625,13 @@ template <return_value_policy Policy = return_value_policy::reference_internal,
typename Sentinel, typename Sentinel,
typename ValueType = typename detail::iterator_access<Iterator>::result_type, typename ValueType = typename detail::iterator_access<Iterator>::result_type,
typename... Extra> typename... Extra>
iterator make_iterator(Iterator &&first, Sentinel &&last, Extra &&...extra) { iterator make_iterator(Iterator first, Sentinel last, Extra &&...extra) {
return detail::make_iterator_impl<detail::iterator_access<Iterator>, return detail::make_iterator_impl<detail::iterator_access<Iterator>,
Policy, Policy,
Iterator, Iterator,
Sentinel, Sentinel,
ValueType, ValueType,
Extra...>(std::forward<Iterator>(first), Extra...>(first, last, std::forward<Extra>(extra)...);
std::forward<Sentinel>(last),
std::forward<Extra>(extra)...);
} }
/// Makes a python iterator over the keys (`.first`) of a iterator over pairs from a /// Makes a python iterator over the keys (`.first`) of a iterator over pairs from a
@ -2643,15 +2641,13 @@ template <return_value_policy Policy = return_value_policy::reference_internal,
typename Sentinel, typename Sentinel,
typename KeyType = typename detail::iterator_key_access<Iterator>::result_type, typename KeyType = typename detail::iterator_key_access<Iterator>::result_type,
typename... Extra> typename... Extra>
iterator make_key_iterator(Iterator &&first, Sentinel &&last, Extra &&...extra) { iterator make_key_iterator(Iterator first, Sentinel last, Extra &&...extra) {
return detail::make_iterator_impl<detail::iterator_key_access<Iterator>, return detail::make_iterator_impl<detail::iterator_key_access<Iterator>,
Policy, Policy,
Iterator, Iterator,
Sentinel, Sentinel,
KeyType, KeyType,
Extra...>(std::forward<Iterator>(first), Extra...>(first, last, std::forward<Extra>(extra)...);
std::forward<Sentinel>(last),
std::forward<Extra>(extra)...);
} }
/// Makes a python iterator over the values (`.second`) of a iterator over pairs from a /// Makes a python iterator over the values (`.second`) of a iterator over pairs from a
@ -2661,15 +2657,13 @@ template <return_value_policy Policy = return_value_policy::reference_internal,
typename Sentinel, typename Sentinel,
typename ValueType = typename detail::iterator_value_access<Iterator>::result_type, typename ValueType = typename detail::iterator_value_access<Iterator>::result_type,
typename... Extra> typename... Extra>
iterator make_value_iterator(Iterator &&first, Sentinel &&last, Extra &&...extra) { iterator make_value_iterator(Iterator first, Sentinel last, Extra &&...extra) {
return detail::make_iterator_impl<detail::iterator_value_access<Iterator>, return detail::make_iterator_impl<detail::iterator_value_access<Iterator>,
Policy, Policy,
Iterator, Iterator,
Sentinel, Sentinel,
ValueType, ValueType,
Extra...>(std::forward<Iterator>(first), Extra...>(first, last, std::forward<Extra>(extra)...);
std::forward<Sentinel>(last),
std::forward<Extra>(extra)...);
} }
/// Makes an iterator over values of an stl container or other container supporting /// Makes an iterator over values of an stl container or other container supporting

View File

@ -1432,6 +1432,9 @@ public:
str(const char *c, const SzType &n) str(const char *c, const SzType &n)
: object(PyUnicode_FromStringAndSize(c, ssize_t_cast(n)), stolen_t{}) { : object(PyUnicode_FromStringAndSize(c, ssize_t_cast(n)), stolen_t{}) {
if (!m_ptr) { if (!m_ptr) {
if (PyErr_Occurred()) {
throw error_already_set();
}
pybind11_fail("Could not allocate string object!"); pybind11_fail("Could not allocate string object!");
} }
} }
@ -1441,6 +1444,9 @@ public:
// NOLINTNEXTLINE(google-explicit-constructor) // NOLINTNEXTLINE(google-explicit-constructor)
str(const char *c = "") : object(PyUnicode_FromString(c), stolen_t{}) { str(const char *c = "") : object(PyUnicode_FromString(c), stolen_t{}) {
if (!m_ptr) { if (!m_ptr) {
if (PyErr_Occurred()) {
throw error_already_set();
}
pybind11_fail("Could not allocate string object!"); pybind11_fail("Could not allocate string object!");
} }
} }
@ -1598,6 +1604,9 @@ inline str::str(const bytes &b) {
} }
auto obj = reinterpret_steal<object>(PyUnicode_FromStringAndSize(buffer, length)); auto obj = reinterpret_steal<object>(PyUnicode_FromStringAndSize(buffer, length));
if (!obj) { if (!obj) {
if (PyErr_Occurred()) {
throw error_already_set();
}
pybind11_fail("Could not allocate string object!"); pybind11_fail("Could not allocate string object!");
} }
m_ptr = obj.release().ptr(); m_ptr = obj.release().ptr();

View File

@ -311,7 +311,7 @@ struct optional_caster {
template <typename T> template <typename T>
static handle cast(T &&src, return_value_policy policy, handle parent) { static handle cast(T &&src, return_value_policy policy, handle parent) {
if (!src) { if (!src) {
return none().inc_ref(); return none().release();
} }
if (!std::is_lvalue_reference<T>::value) { if (!std::is_lvalue_reference<T>::value) {
policy = return_value_policy_override<Value>::policy(policy); policy = return_value_policy_override<Value>::policy(policy);

View File

@ -210,4 +210,5 @@ def pytest_report_header(config):
f" {pybind11_tests.compiler_info}" f" {pybind11_tests.compiler_info}"
f" {pybind11_tests.cpp_std}" f" {pybind11_tests.cpp_std}"
f" {pybind11_tests.PYBIND11_INTERNALS_ID}" f" {pybind11_tests.PYBIND11_INTERNALS_ID}"
f" PYBIND11_SIMPLE_GIL_MANAGEMENT={pybind11_tests.PYBIND11_SIMPLE_GIL_MANAGEMENT}"
) )

View File

@ -6,9 +6,15 @@
All rights reserved. Use of this source code is governed by a All rights reserved. Use of this source code is governed by a
BSD-style license that can be found in the LICENSE file. 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 <pybind11/pybind11.h> #include <pybind11/pybind11.h>
#include <cstdint> #include <cstdint>
#include <string>
#include <thread>
// This file mimics a DSO that makes pybind11 calls but does not define a // 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 // PYBIND11_MODULE. The purpose is to test that such a DSO can create a
@ -21,8 +27,54 @@
namespace { namespace {
namespace py = pybind11; namespace py = pybind11;
void gil_acquire() { py::gil_scoped_acquire gil; } 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 <typename Acquire, typename Release>
void gil_acquire_inner() {
Acquire acquire_outer;
Acquire acquire_inner;
Release release;
}
template <typename Acquire, typename Release>
void gil_acquire_nested() {
Acquire acquire_outer;
Acquire acquire_inner;
Release release;
auto thread = std::thread(&gil_acquire_inner<Acquire, Release>);
thread.join();
}
constexpr char kModuleName[] = "cross_module_gil_utils"; constexpr char kModuleName[] = "cross_module_gil_utils";
struct PyModuleDef moduledef = { struct PyModuleDef moduledef = {
@ -30,6 +82,9 @@ struct PyModuleDef moduledef = {
} // namespace } // namespace
#define ADD_FUNCTION(Name, ...) \
PyModule_AddObject(m, Name, PyLong_FromVoidPtr(reinterpret_cast<void *>(&__VA_ARGS__)));
extern "C" PYBIND11_EXPORT PyObject *PyInit_cross_module_gil_utils() { extern "C" PYBIND11_EXPORT PyObject *PyInit_cross_module_gil_utils() {
PyObject *m = PyModule_Create(&moduledef); PyObject *m = PyModule_Create(&moduledef);
@ -37,8 +92,16 @@ extern "C" PYBIND11_EXPORT PyObject *PyInit_cross_module_gil_utils() {
if (m != nullptr) { if (m != nullptr) {
static_assert(sizeof(&gil_acquire) == sizeof(void *), static_assert(sizeof(&gil_acquire) == sizeof(void *),
"Function pointer must have the same size as void*"); "Function pointer must have the same size as void*");
PyModule_AddObject( ADD_FUNCTION("gil_acquire_funcaddr", gil_acquire)
m, "gil_acquire_funcaddr", PyLong_FromVoidPtr(reinterpret_cast<void *>(&gil_acquire))); ADD_FUNCTION("gil_multi_acquire_release_funcaddr", gil_multi_acquire_release)
ADD_FUNCTION("gil_acquire_inner_custom_funcaddr",
gil_acquire_inner<CustomAutoGIL, CustomAutoNoGIL>)
ADD_FUNCTION("gil_acquire_nested_custom_funcaddr",
gil_acquire_nested<CustomAutoGIL, CustomAutoNoGIL>)
ADD_FUNCTION("gil_acquire_inner_pybind11_funcaddr",
gil_acquire_inner<py::gil_scoped_acquire, py::gil_scoped_release>)
ADD_FUNCTION("gil_acquire_nested_pybind11_funcaddr",
gil_acquire_nested<py::gil_scoped_acquire, py::gil_scoped_release>)
} }
return m; return m;

View File

@ -89,6 +89,12 @@ PYBIND11_MODULE(pybind11_tests, m) {
#endif #endif
m.attr("cpp_std") = cpp_std(); m.attr("cpp_std") = cpp_std();
m.attr("PYBIND11_INTERNALS_ID") = PYBIND11_INTERNALS_ID; m.attr("PYBIND11_INTERNALS_ID") = PYBIND11_INTERNALS_ID;
m.attr("PYBIND11_SIMPLE_GIL_MANAGEMENT") =
#if defined(PYBIND11_SIMPLE_GIL_MANAGEMENT)
true;
#else
false;
#endif
bind_ConstructorStats(m); bind_ConstructorStats(m);

View File

@ -398,6 +398,8 @@ TEST_SUBMODULE(class_, m) {
protected: protected:
virtual int foo() const { return value; } virtual int foo() const { return value; }
virtual void *void_foo() { return static_cast<void *>(&value); }
virtual void *get_self() { return static_cast<void *>(this); }
private: private:
int value = 42; int value = 42;
@ -406,6 +408,8 @@ TEST_SUBMODULE(class_, m) {
class TrampolineB : public ProtectedB { class TrampolineB : public ProtectedB {
public: public:
int foo() const override { PYBIND11_OVERRIDE(int, ProtectedB, foo, ); } 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 { class PublicistB : public ProtectedB {
@ -415,11 +419,23 @@ TEST_SUBMODULE(class_, m) {
// (in Debug builds only, tested with icpc (ICC) 2021.1 Beta 20200827) // (in Debug builds only, tested with icpc (ICC) 2021.1 Beta 20200827)
~PublicistB() override{}; // NOLINT(modernize-use-equals-default) ~PublicistB() override{}; // NOLINT(modernize-use-equals-default)
using ProtectedB::foo; using ProtectedB::foo;
using ProtectedB::get_self;
using ProtectedB::void_foo;
}; };
m.def("read_foo", [](const void *original) {
const int *ptr = reinterpret_cast<const int *>(original);
return *ptr;
});
m.def("pointers_equal",
[](const void *original, const void *comparison) { return original == comparison; });
py::class_<ProtectedB, TrampolineB>(m, "ProtectedB") py::class_<ProtectedB, TrampolineB>(m, "ProtectedB")
.def(py::init<>()) .def(py::init<>())
.def("foo", &PublicistB::foo); .def("foo", &PublicistB::foo)
.def("void_foo", &PublicistB::void_foo)
.def("get_self", &PublicistB::get_self);
// test_brace_initialization // test_brace_initialization
struct BraceInitialization { struct BraceInitialization {

View File

@ -313,6 +313,8 @@ def test_bind_protected_functions():
b = m.ProtectedB() b = m.ProtectedB()
assert b.foo() == 42 assert b.foo() == 42
assert m.read_foo(b.void_foo()) == 42
assert m.pointers_equal(b.get_self(), b)
class C(m.ProtectedB): class C(m.ProtectedB):
def __init__(self): def __init__(self):

View File

@ -9,8 +9,16 @@ try:
from pybind11_tests import eigen_tensor_avoid_stl_array as avoid from pybind11_tests import eigen_tensor_avoid_stl_array as avoid
submodules += [avoid.c_style, avoid.f_style] submodules += [avoid.c_style, avoid.f_style]
except ImportError: except ImportError as e:
pass # Ensure config, build, toolchain, etc. issues are not masked here:
raise RuntimeError(
"import pybind11_tests.eigen_tensor_avoid_stl_array FAILED, while "
"import pybind11_tests.eigen_tensor succeeded. "
"Please ensure that "
"test_eigen_tensor.cpp & "
"test_eigen_tensor_avoid_stl_array.cpp "
"are built together (or both are not built if Eigen is not available)."
) from e
tensor_ref = np.empty((3, 5, 2), dtype=np.int64) tensor_ref = np.empty((3, 5, 2), dtype=np.int64)

View File

@ -293,7 +293,6 @@ TEST_CASE("Threads") {
{ {
py::gil_scoped_release gil_release{}; py::gil_scoped_release gil_release{};
REQUIRE(has_pybind11_internals_static());
auto threads = std::vector<std::thread>(); auto threads = std::vector<std::thread>();
for (auto i = 0; i < num_threads; ++i) { for (auto i = 0; i < num_threads; ++i) {

View File

@ -11,6 +11,13 @@
#include "pybind11_tests.h" #include "pybind11_tests.h"
#include <string>
#include <thread>
#define CROSS_MODULE(Function) \
auto cm = py::module_::import("cross_module_gil_utils"); \
auto target = reinterpret_cast<void (*)()>(PyLong_AsVoidPtr(cm.attr(Function).ptr()));
class VirtClass { class VirtClass {
public: public:
virtual ~VirtClass() = default; virtual ~VirtClass() = default;
@ -28,6 +35,16 @@ class PyVirtClass : public VirtClass {
}; };
TEST_SUBMODULE(gil_scoped, m) { 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_<VirtClass, PyVirtClass>(m, "VirtClass") py::class_<VirtClass, PyVirtClass>(m, "VirtClass")
.def(py::init<>()) .def(py::init<>())
.def("virtual_func", &VirtClass::virtual_func) .def("virtual_func", &VirtClass::virtual_func)
@ -37,11 +54,91 @@ TEST_SUBMODULE(gil_scoped, m) {
m.def("test_callback_std_func", [](const std::function<void()> &func) { func(); }); m.def("test_callback_std_func", [](const std::function<void()> &func) { func(); });
m.def("test_callback_virtual_func", [](VirtClass &virt) { virt.virtual_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_callback_pure_virtual_func", [](VirtClass &virt) { virt.pure_virtual_func(); });
m.def("test_cross_module_gil", []() { m.def("test_cross_module_gil_released", []() {
auto cm = py::module_::import("cross_module_gil_utils"); CROSS_MODULE("gil_acquire_funcaddr")
auto gil_acquire = reinterpret_cast<void (*)()>(
PyLong_AsVoidPtr(cm.attr("gil_acquire_funcaddr").ptr()));
py::gil_scoped_release gil_release; py::gil_scoped_release gil_release;
gil_acquire(); 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<std::string (*)(unsigned)>(
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;
}); });
} }

View File

@ -1,45 +1,199 @@
import multiprocessing import multiprocessing
import sys
import threading import threading
import time
import pytest
from pybind11_tests import gil_scoped as m from pybind11_tests import gil_scoped as m
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()
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()
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()
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()
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"
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,)
SKIP_IF_DEADLOCK = True # See PR #4216
def _run_in_process(target, *args, **kwargs): def _run_in_process(target, *args, **kwargs):
"""Runs target in process and returns its exitcode after 10s (None if still alive).""" if len(args) == 0:
test_fn = target
else:
test_fn = 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 = multiprocessing.Process(target=target, args=args, kwargs=kwargs)
process.daemon = True process.daemon = True
try: try:
t_start = time.time()
process.start() process.start()
# Do not need to wait much, 10s should be more than enough. if timeout >= 100: # For debugging.
process.join(timeout=10) 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
elif process.exitcode is None:
assert t_delta > 0.9 * timeout
msg = "DEADLOCK, most likely, exactly what this test is meant to detect."
if SKIP_IF_DEADLOCK:
pytest.skip(msg)
raise RuntimeError(msg)
return process.exitcode return process.exitcode
finally: finally:
if process.is_alive(): if process.is_alive():
process.terminate() process.terminate()
def _python_to_cpp_to_python(): def _run_in_threads(test_fn, num_threads, parallel):
"""Calls different C++ functions that come back to Python."""
class ExtendedVirtClass(m.VirtClass):
def virtual_func(self):
pass
def pure_virtual_func(self):
pass
extended = ExtendedVirtClass()
m.test_callback_py_obj(lambda: None)
m.test_callback_std_func(lambda: None)
m.test_callback_virtual_func(extended)
m.test_callback_pure_virtual_func(extended)
def _python_to_cpp_to_python_from_threads(num_threads, parallel=False):
"""Calls different C++ functions that come back to Python, from Python threads."""
threads = [] threads = []
for _ in range(num_threads): for _ in range(num_threads):
thread = threading.Thread(target=_python_to_cpp_to_python) thread = threading.Thread(target=test_fn)
thread.daemon = True thread.daemon = True
thread.start() thread.start()
if parallel: if parallel:
@ -51,43 +205,40 @@ def _python_to_cpp_to_python_from_threads(num_threads, parallel=False):
# TODO: FIXME, sometimes returns -11 (segfault) instead of 0 on macOS Python 3.9 # TODO: FIXME, sometimes returns -11 (segfault) instead of 0 on macOS Python 3.9
def test_python_to_cpp_to_python_from_thread(): @pytest.mark.parametrize("test_fn", ALL_BASIC_TESTS_PLUS_INTENTIONAL_DEADLOCK)
def test_run_in_process_one_thread(test_fn):
"""Makes sure there is no GIL deadlock when running in a thread. """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. It runs in a separate process to be able to stop and assert if it deadlocks.
""" """
assert _run_in_process(_python_to_cpp_to_python_from_threads, 1) == 0 assert _run_in_process(_run_in_threads, test_fn, num_threads=1, parallel=False) == 0
# TODO: FIXME on macOS Python 3.9 # TODO: FIXME on macOS Python 3.9
def test_python_to_cpp_to_python_from_thread_multiple_parallel(): @pytest.mark.parametrize("test_fn", ALL_BASIC_TESTS_PLUS_INTENTIONAL_DEADLOCK)
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. """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. It runs in a separate process to be able to stop and assert if it deadlocks.
""" """
assert _run_in_process(_python_to_cpp_to_python_from_threads, 8, parallel=True) == 0 assert _run_in_process(_run_in_threads, test_fn, num_threads=8, parallel=True) == 0
# TODO: FIXME on macOS Python 3.9 # TODO: FIXME on macOS Python 3.9
def test_python_to_cpp_to_python_from_thread_multiple_sequential(): @pytest.mark.parametrize("test_fn", ALL_BASIC_TESTS_PLUS_INTENTIONAL_DEADLOCK)
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. """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. It runs in a separate process to be able to stop and assert if it deadlocks.
""" """
assert ( assert _run_in_process(_run_in_threads, test_fn, num_threads=8, parallel=False) == 0
_run_in_process(_python_to_cpp_to_python_from_threads, 8, parallel=False) == 0
)
# TODO: FIXME on macOS Python 3.9 # TODO: FIXME on macOS Python 3.9
def test_python_to_cpp_to_python_from_process(): @pytest.mark.parametrize("test_fn", ALL_BASIC_TESTS_PLUS_INTENTIONAL_DEADLOCK)
def test_run_in_process_direct(test_fn):
"""Makes sure there is no GIL deadlock when using processes. """Makes sure there is no GIL deadlock when using processes.
This test is for completion, but it was never an issue. This test is for completion, but it was never an issue.
""" """
assert _run_in_process(_python_to_cpp_to_python) == 0 assert _run_in_process(test_fn) == 0
def test_cross_module_gil():
"""Makes sure that the GIL can be acquired by another module from a GIL-released state."""
m.test_cross_module_gil() # Should not raise a SIGSEGV

View File

@ -206,7 +206,12 @@ TEST_SUBMODULE(pytypes, m) {
m.def("str_from_char_ssize_t", []() { return py::str{"red", (py::ssize_t) 3}; }); 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_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_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", []() { 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("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("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_handle", [](py::handle h) { return py::str(h); });

View File

@ -244,6 +244,20 @@ def test_str(doc):
m.str_from_string_from_str(ucs_surrogates_str) 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,
],
)
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): def test_bytes(doc):
assert m.bytes_from_char_ssize_t().decode() == "green" assert m.bytes_from_char_ssize_t().decode() == "green"
assert m.bytes_from_char_size_t().decode() == "purple" assert m.bytes_from_char_size_t().decode() == "purple"

View File

@ -559,4 +559,23 @@ TEST_SUBMODULE(sequences_and_iterators, m) {
[]() { return py::make_iterator<py::return_value_policy::copy>(list); }); []() { return py::make_iterator<py::return_value_policy::copy>(list); });
m.def("make_iterator_2", m.def("make_iterator_2",
[]() { return py::make_iterator<py::return_value_policy::automatic>(list); }); []() { return py::make_iterator<py::return_value_policy::automatic>(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_<CArrayHolder>(m, "CArrayHolder")
.def(py::init<double, double, double>())
.def(
"__iter__",
[](const CArrayHolder &v) { return py::make_iterator(v.values, v.values + 3); },
py::keep_alive<0, 1>());
} }

View File

@ -241,3 +241,11 @@ def test_iterator_rvp():
assert list(m.make_iterator_1()) == [1, 2, 3] assert list(m.make_iterator_1()) == [1, 2, 3]
assert list(m.make_iterator_2()) == [1, 2, 3] assert list(m.make_iterator_2()) == [1, 2, 3]
assert not isinstance(m.make_iterator_1(), type(m.make_iterator_2())) 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 = list(float(i) for i in range(3))
arr_h = m.CArrayHolder(*args_gt)
args = list(arr_h)
assert args_gt == args

View File

@ -151,9 +151,13 @@ if(NOT _PYTHON_SUCCESS MATCHES 0)
return() return()
endif() endif()
option(
PYBIND11_PYTHONLIBS_OVERWRITE
"Overwrite cached values read from Python library (classic search). Turn off if cross-compiling and manually setting these values."
ON)
# Can manually set values when cross-compiling # Can manually set values when cross-compiling
macro(_PYBIND11_GET_IF_UNDEF lst index name) macro(_PYBIND11_GET_IF_UNDEF lst index name)
if(NOT DEFINED "${name}") if(PYBIND11_PYTHONLIBS_OVERWRITE OR NOT DEFINED "${name}")
list(GET "${lst}" "${index}" "${name}") list(GET "${lst}" "${index}" "${name}")
endif() endif()
endmacro() endmacro()