Skip to content

Commit 6d8fa8a

Browse files
committed
Merge remote-tracking branch 'upstream/master' into fix-segfault
2 parents a4a6a1e + 6c83607 commit 6d8fa8a

File tree

9 files changed

+264
-90
lines changed

9 files changed

+264
-90
lines changed

docs/changelog.md

Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,161 @@ Changes will be added here periodically from the "Suggested changelog
1313
entry" block in pull request descriptions.
1414

1515

16+
## Version 3.0.2 (release date TBD)
17+
18+
Bug fixes:
19+
20+
- MSVC 19.16 and earlier were blocked from using `std::launder` due to internal compiler errors.
21+
[#5968](https://github.com/pybind/pybind11/pull/5968)
22+
23+
- Internals destructors were updated to check the owning interpreter before clearing Python objects.
24+
[#5965](https://github.com/pybind/pybind11/pull/5965)
25+
26+
- pybind11 internals were updated to be deallocated during (sub-)interpreter shutdown to avoid memory leaks.
27+
[#5958](https://github.com/pybind/pybind11/pull/5958)
28+
29+
- Fixed ambiguous `str(handle)` construction for `object`-derived types like `kwargs` or `dict` by templatizing the constructor with SFINAE.
30+
[#5949](https://github.com/pybind/pybind11/pull/5949)
31+
32+
- Fixed concurrency consistency for `internals_pp_manager` under multiple-interpreters.
33+
[#5947](https://github.com/pybind/pybind11/pull/5947)
34+
35+
- Fixed MSVC LNK2001 in C++20 builds when /GL (whole program optimization) is enabled.
36+
[#5939](https://github.com/pybind/pybind11/pull/5939)
37+
38+
- Added per-interpreter storage for `gil_safe_call_once_and_store` to make it safe under multi-interpreters.
39+
[#5933](https://github.com/pybind/pybind11/pull/5933)
40+
41+
- A workaround for a GCC `-Warray-bounds` false positive in `argument_vector` was added.
42+
[#5908](https://github.com/pybind/pybind11/pull/5908)
43+
44+
- 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.
45+
[#5891](https://github.com/pybind/pybind11/pull/5891)
46+
47+
- Fixed `*args/**kwargs` return types. Added type hinting to `py::make_tuple`.
48+
[#5881](https://github.com/pybind/pybind11/pull/5881)
49+
50+
- Fixed compiler error in `type_caster_generic` when casting a `T` implicitly convertible from `T*`.
51+
[#5873](https://github.com/pybind/pybind11/pull/5873)
52+
53+
- Updated `py::native_enum` bindings to unregister enum types on destruction, preventing a use-after-free when returning a destroyed enum instance.
54+
[#5871](https://github.com/pybind/pybind11/pull/5871)
55+
56+
- Fixed undefined behavior that occurred when importing pybind11 modules from non-main threads created by C API modules or embedded python interpreters.
57+
[#5870](https://github.com/pybind/pybind11/pull/5870)
58+
59+
- Fixed dangling pointer in `internals::registered_types_cpp_fast`.
60+
[#5867](https://github.com/pybind/pybind11/pull/5867)
61+
62+
- Added support for `std::shared_ptr<T>` when loading module-local or conduit types from other modules.
63+
[#5862](https://github.com/pybind/pybind11/pull/5862)
64+
65+
- Fixed thread-safety issues if types were concurrently registered while `get_local_type_info()` was called in free threaded Python.
66+
[#5856](https://github.com/pybind/pybind11/pull/5856)
67+
68+
- Fixed py::float_ casting and py::int_ and py::float_ type hints.
69+
[#5839](https://github.com/pybind/pybind11/pull/5839)
70+
71+
- Fixed two `smart_holder` bugs in `shared_ptr` and `unique_ptr` adoption with multiple/virtual inheritance:
72+
- `shared_ptr` to-Python caster was updated to register the correct subobject pointer (fixes #5786).
73+
- `unique_ptr` adoption was updated to own the proper object start while aliasing subobject pointers for registration, which fixed MSVC crashes during destruction.
74+
[#5836](https://github.com/pybind/pybind11/pull/5836)
75+
76+
- Constrained `accessor::operator=` templates to avoid obscuring special members.
77+
[#5832](https://github.com/pybind/pybind11/pull/5832)
78+
79+
- Fixed crash that can occur when finalizers acquire and release the GIL.
80+
[#5828](https://github.com/pybind/pybind11/pull/5828)
81+
82+
- Fixed compiler detection in `pybind11/detail/pybind11_namespace_macros.h` for clang-cl on Windows, to address warning suppression macros.
83+
[#5816](https://github.com/pybind/pybind11/pull/5816)
84+
85+
- Fixed compatibility with CMake policy CMP0190 by not always requiring a Python interpreter when cross-compiling.
86+
[#5829](https://github.com/pybind/pybind11/pull/5829)
87+
88+
- Added a static assertion to disallow `keep_alive` and `call_guard` on properties.
89+
[#5533](https://github.com/pybind/pybind11/pull/5533)
90+
91+
Internal:
92+
93+
- CMake policy limit was set to 4.1.
94+
[#5944](https://github.com/pybind/pybind11/pull/5944)
95+
96+
- Improved performance of function calls between Python and C++ by switching to the "vectorcall" calling protocol.
97+
[#5948](https://github.com/pybind/pybind11/pull/5948)
98+
99+
- Many C-style casts were replaced with C++-style casts.
100+
[#5930](https://github.com/pybind/pybind11/pull/5930)
101+
102+
- Added `cast_sources` abstraction to `type_caster_generic`.
103+
[#5866](https://github.com/pybind/pybind11/pull/5866)
104+
105+
- Improved the performance of from-Python conversions of legacy pybind11 enum objects bound by `py::enum_`.
106+
[#5860](https://github.com/pybind/pybind11/pull/5860)
107+
108+
- Reduced size overhead by deduplicating functions' readable signatures and type information.
109+
[#5857](https://github.com/pybind/pybind11/pull/5857)
110+
111+
- Used new Python 3.14 C APIs when available.
112+
[#5854](https://github.com/pybind/pybind11/pull/5854)
113+
114+
- Improved performance of function dispatch and type casting by porting two-level type info lookup strategy from nanobind.
115+
[#5842](https://github.com/pybind/pybind11/pull/5842)
116+
117+
- Updated `.gitignore` to exclude `__pycache__/` directories.
118+
[#5838](https://github.com/pybind/pybind11/pull/5838)
119+
120+
- Changed internals to use `thread_local` instead of `thread_specific_storage` for increased performance.
121+
[#5834](https://github.com/pybind/pybind11/pull/5834)
122+
123+
- Reduced function call overhead by using thread_local for loader_life_support when possible.
124+
[#5830](https://github.com/pybind/pybind11/pull/5830)
125+
126+
- Removed heap allocation for the C++ argument array when dispatching functions with 6 or fewer arguments.
127+
[#5824](https://github.com/pybind/pybind11/pull/5824)
128+
129+
130+
Documentation:
131+
132+
- Fixed docstring for `long double` complex types to use `numpy.clongdouble` instead of the deprecated `numpy.longcomplex` (removed in NumPy 2.0).
133+
[#5952](https://github.com/pybind/pybind11/pull/5952)
134+
135+
- 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.
136+
[#5910](https://github.com/pybind/pybind11/pull/5910)
137+
138+
- Fixed documentation formatting.
139+
[#5903](https://github.com/pybind/pybind11/pull/5903)
140+
141+
- Updated upgrade notes for `py::native_enum`.
142+
[#5885](https://github.com/pybind/pybind11/pull/5885)
143+
144+
- Clarified in the docs to what extent bindings are global.
145+
[#5859](https://github.com/pybind/pybind11/pull/5859)
146+
147+
148+
Tests:
149+
150+
- Calls to `env.deprecated_call()` were replaced with direct calls to `pytest.deprecated_call()`.
151+
[#5893](https://github.com/pybind/pybind11/pull/5893)
152+
153+
- Updated pytest configuration to use `log_level` instead of `log_cli_level`.
154+
[#5890](https://github.com/pybind/pybind11/pull/5890)
155+
156+
157+
CI:
158+
159+
- Added CI tests for windows-11-arm with clang/MSVC (currently python 3.13), windows-11-arm with clang/mingw (currently python 3.12).
160+
[#5932](https://github.com/pybind/pybind11/pull/5932)
161+
162+
- These clang-tidy rules were added: `readability-redundant-casting`, `readability-redundant-inline-specifier`, `readability-redundant-member-init`
163+
[#5924](https://github.com/pybind/pybind11/pull/5924)
164+
165+
- Replaced deprecated macos-13 runners with macos-15-intel in CI.
166+
[#5916](https://github.com/pybind/pybind11/pull/5916)
167+
168+
- Restored `runs-on: windows-latest` in CI.
169+
[#5835](https://github.com/pybind/pybind11/pull/5835)
170+
16171
## Version 3.0.1 (August 22, 2025)
17172

18173
Bug fixes:

include/pybind11/detail/common.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@
8787
# endif
8888
#endif
8989

90-
#if defined(__cpp_lib_launder) && !(defined(_MSC_VER) && (_MSC_VER < 1914))
90+
#if defined(__cpp_lib_launder) && !(defined(_MSC_VER) && (_MSC_VER < 1920)) // See PR #5968
9191
# define PYBIND11_STD_LAUNDER std::launder
9292
# define PYBIND11_HAS_STD_LAUNDER 1
9393
#else

include/pybind11/detail/internals.h

Lines changed: 44 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,38 @@ inline PyTypeObject *make_default_metaclass();
143143
inline PyObject *make_object_base_type(PyTypeObject *metaclass);
144144
inline void translate_exception(std::exception_ptr p);
145145

146+
inline PyThreadState *get_thread_state_unchecked() {
147+
#if defined(PYPY_VERSION) || defined(GRAALVM_PYTHON)
148+
return PyThreadState_GET();
149+
#elif PY_VERSION_HEX < 0x030D0000
150+
return _PyThreadState_UncheckedGet();
151+
#else
152+
return PyThreadState_GetUnchecked();
153+
#endif
154+
}
155+
156+
inline PyInterpreterState *get_interpreter_state_unchecked() {
157+
auto *tstate = get_thread_state_unchecked();
158+
return tstate ? tstate->interp : nullptr;
159+
}
160+
161+
inline object get_python_state_dict() {
162+
object state_dict;
163+
#if defined(PYPY_VERSION) || defined(GRAALVM_PYTHON)
164+
state_dict = reinterpret_borrow<object>(PyEval_GetBuiltins());
165+
#else
166+
auto *istate = get_interpreter_state_unchecked();
167+
if (istate) {
168+
state_dict = reinterpret_borrow<object>(PyInterpreterState_GetDict(istate));
169+
}
170+
#endif
171+
if (!state_dict) {
172+
raise_from(PyExc_SystemError, "pybind11::detail::get_python_state_dict() FAILED");
173+
throw error_already_set();
174+
}
175+
return state_dict;
176+
}
177+
146178
// Python loads modules by default with dlopen with the RTLD_LOCAL flag; under libc++ and possibly
147179
// other STLs, this means `typeid(A)` from one module won't equal `typeid(A)` from another module
148180
// even when `A` is the same, non-hidden-visibility type (e.g. from a common include). Under
@@ -195,14 +227,6 @@ struct override_hash {
195227

196228
using instance_map = std::unordered_multimap<const void *, instance *>;
197229

198-
inline bool is_interpreter_alive() {
199-
#if PY_VERSION_HEX < 0x030D0000
200-
return Py_IsInitialized() != 0 || _Py_IsFinalizing() != 0;
201-
#else
202-
return Py_IsInitialized() != 0 || Py_IsFinalizing() != 0;
203-
#endif
204-
}
205-
206230
#ifdef Py_GIL_DISABLED
207231
// Wrapper around PyMutex to provide BasicLockable semantics
208232
class pymutex {
@@ -293,11 +317,8 @@ struct internals {
293317

294318
internals()
295319
: static_property_type(make_static_property_type()),
296-
default_metaclass(make_default_metaclass()) {
320+
default_metaclass(make_default_metaclass()), istate(get_interpreter_state_unchecked()) {
297321
tstate.set(nullptr); // See PR #5870
298-
PyThreadState *cur_tstate = PyThreadState_Get();
299-
300-
istate = cur_tstate->interp;
301322
registered_exception_translators.push_front(&translate_exception);
302323
#ifdef Py_GIL_DISABLED
303324
// Scale proportional to the number of cores. 2x is a heuristic to reduce contention.
@@ -320,8 +341,10 @@ struct internals {
320341
// Normally this destructor runs during interpreter finalization and it may DECREF things.
321342
// In odd finalization scenarios it might end up running after the interpreter has
322343
// completely shut down, In that case, we should not decref these objects because pymalloc
323-
// is gone.
324-
if (is_interpreter_alive()) {
344+
// is gone. This also applies across sub-interpreters, we should only DECREF when the
345+
// original owning interpreter is active.
346+
auto *cur_istate = get_interpreter_state_unchecked();
347+
if (cur_istate && cur_istate == istate) {
325348
Py_CLEAR(instance_base);
326349
Py_CLEAR(default_metaclass);
327350
Py_CLEAR(static_property_type);
@@ -336,20 +359,25 @@ struct internals {
336359
// impact any other modules, because the only things accessing the local internals is the
337360
// module that contains them.
338361
struct local_internals {
362+
local_internals() : istate(get_interpreter_state_unchecked()) {}
363+
339364
// It should be safe to use fast_type_map here because this entire
340365
// data structure is scoped to our single module, and thus a single
341366
// DSO and single instance of type_info for any particular type.
342367
fast_type_map<type_info *> registered_types_cpp;
343368

344369
std::forward_list<ExceptionTranslator> registered_exception_translators;
345370
PyTypeObject *function_record_py_type = nullptr;
371+
PyInterpreterState *istate = nullptr;
346372

347373
~local_internals() {
348374
// Normally this destructor runs during interpreter finalization and it may DECREF things.
349375
// In odd finalization scenarios it might end up running after the interpreter has
350376
// completely shut down, In that case, we should not decref these objects because pymalloc
351-
// is gone.
352-
if (is_interpreter_alive()) {
377+
// is gone. This also applies across sub-interpreters, we should only DECREF when the
378+
// original owning interpreter is active.
379+
auto *cur_istate = get_interpreter_state_unchecked();
380+
if (cur_istate && cur_istate == istate) {
353381
Py_CLEAR(function_record_py_type);
354382
}
355383
}
@@ -436,16 +464,6 @@ struct native_enum_record {
436464
"__pybind11_module_local_v" PYBIND11_TOSTRING(PYBIND11_INTERNALS_VERSION) \
437465
PYBIND11_COMPILER_TYPE_LEADING_UNDERSCORE PYBIND11_PLATFORM_ABI_ID "__"
438466

439-
inline PyThreadState *get_thread_state_unchecked() {
440-
#if defined(PYPY_VERSION) || defined(GRAALVM_PYTHON)
441-
return PyThreadState_GET();
442-
#elif PY_VERSION_HEX < 0x030D0000
443-
return _PyThreadState_UncheckedGet();
444-
#else
445-
return PyThreadState_GetUnchecked();
446-
#endif
447-
}
448-
449467
/// We use this to figure out if there are or have been multiple subinterpreters active at any
450468
/// point. This must never go from true to false while any interpreter may be running in any
451469
/// thread!
@@ -558,27 +576,6 @@ inline void translate_local_exception(std::exception_ptr p) {
558576
}
559577
#endif
560578

561-
inline object get_python_state_dict() {
562-
object state_dict;
563-
#if defined(PYPY_VERSION) || defined(GRAALVM_PYTHON)
564-
state_dict = reinterpret_borrow<object>(PyEval_GetBuiltins());
565-
#else
566-
# if PY_VERSION_HEX < 0x03090000
567-
PyInterpreterState *istate = _PyInterpreterState_Get();
568-
# else
569-
PyInterpreterState *istate = PyInterpreterState_Get();
570-
# endif
571-
if (istate) {
572-
state_dict = reinterpret_borrow<object>(PyInterpreterState_GetDict(istate));
573-
}
574-
#endif
575-
if (!state_dict) {
576-
raise_from(PyExc_SystemError, "pybind11::detail::get_python_state_dict() FAILED");
577-
throw error_already_set();
578-
}
579-
return state_dict;
580-
}
581-
582579
// Get or create per-storage capsule in the current interpreter's state dict.
583580
// - The storage is interpreter-dependent: different interpreters will have different storage.
584581
// This is important when using multiple-interpreters, to avoid sharing unshareable objects

include/pybind11/subinterpreter.h

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,6 @@
2020
#endif
2121

2222
PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE)
23-
PYBIND11_NAMESPACE_BEGIN(detail)
24-
inline PyInterpreterState *get_interpreter_state_unchecked() {
25-
auto *cur_tstate = get_thread_state_unchecked();
26-
if (cur_tstate) {
27-
return cur_tstate->interp;
28-
}
29-
return nullptr;
30-
}
31-
PYBIND11_NAMESPACE_END(detail)
3223

3324
class subinterpreter;
3425

tests/CMakeLists.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -545,7 +545,9 @@ source_group(
545545
FILES ${PYBIND11_HEADERS})
546546

547547
# Make sure pytest is found or produce a warning
548-
pybind11_find_import(pytest VERSION 3.1)
548+
if(NOT CMAKE_CROSSCOMPILING)
549+
pybind11_find_import(pytest VERSION 3.1)
550+
endif()
549551

550552
if(NOT CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_CURRENT_BINARY_DIR)
551553
# This is not used later in the build, so it's okay to regenerate each time.

tools/pybind11Common.cmake

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,18 @@ set(pybind11_INCLUDE_DIRS
4141
"${pybind11_INCLUDE_DIR}"
4242
CACHE INTERNAL "Include directory for pybind11 (Python not requested)")
4343

44-
if(CMAKE_CROSSCOMPILING AND PYBIND11_USE_CROSSCOMPILING)
44+
# CMP0190 prohibits calling FindPython with both Interpreter and Development components
45+
# when cross-compiling, unless the CMAKE_CROSSCOMPILING_EMULATOR variable is defined.
46+
if(CMAKE_VERSION VERSION_GREATER_EQUAL "4.1")
47+
cmake_policy(GET CMP0190 _pybind11_cmp0190)
48+
if(_pybind11_cmp0190 STREQUAL "NEW")
49+
set(PYBIND11_USE_CROSSCOMPILING "ON")
50+
endif()
51+
endif()
52+
53+
if(CMAKE_CROSSCOMPILING
54+
AND PYBIND11_USE_CROSSCOMPILING
55+
AND NOT DEFINED CMAKE_CROSSCOMPILING_EMULATOR)
4556
set(_PYBIND11_CROSSCOMPILING
4657
ON
4758
CACHE INTERNAL "")

0 commit comments

Comments
 (0)