Skip to content

Commit 5a34137

Browse files
authored
Merge pull request #30122 from rwgk/pybind11k_merge_sh
git merge smart_holder (pybind/pybind11#5157)
2 parents db56b9e + e128adf commit 5a34137

22 files changed

+480
-96
lines changed

.github/workflows/ci.yml

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,35 @@ jobs:
197197
pytest tests/extra_setuptools
198198
if: "!(matrix.runs-on == 'windows-2022')"
199199

200+
manylinux:
201+
name: Manylinux on 🐍 3.13t • GIL
202+
runs-on: ubuntu-latest
203+
timeout-minutes: 40
204+
container: quay.io/pypa/musllinux_1_2_x86_64:latest
205+
steps:
206+
- uses: actions/checkout@v4
207+
with:
208+
fetch-depth: 0
209+
210+
- name: Prepare venv
211+
run: python3.13t -m venv .venv
212+
213+
- name: Install Python deps
214+
run: .venv/bin/pip install -r tests/requirements.txt
215+
216+
- name: Configure C++11
217+
run: >
218+
cmake -S. -Bbuild
219+
-DPYBIND11_WERROR=ON
220+
-DDOWNLOAD_CATCH=ON
221+
-DDOWNLOAD_EIGEN=ON
222+
-DPython_ROOT_DIR=.venv
223+
224+
- name: Build C++11
225+
run: cmake --build build -j2
226+
227+
- name: Python tests C++11
228+
run: cmake --build build --target pytest -j2
200229

201230
deadsnakes:
202231
strategy:

.github/workflows/ci_sh_def.yml

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,35 @@ jobs:
212212
pytest tests/extra_setuptools
213213
if: "!(matrix.runs-on == 'windows-2022')"
214214

215+
manylinux:
216+
name: Manylinux on 🐍 3.13t • GIL
217+
runs-on: ubuntu-latest
218+
timeout-minutes: 40
219+
container: quay.io/pypa/musllinux_1_2_x86_64:latest
220+
steps:
221+
- uses: actions/checkout@v4
222+
with:
223+
fetch-depth: 0
224+
225+
- name: Prepare venv
226+
run: python3.13t -m venv .venv
227+
228+
- name: Install Python deps
229+
run: .venv/bin/pip install -r tests/requirements.txt
230+
231+
- name: Configure C++11
232+
run: >
233+
cmake -S. -Bbuild
234+
-DPYBIND11_WERROR=ON
235+
-DDOWNLOAD_CATCH=ON
236+
-DDOWNLOAD_EIGEN=ON
237+
-DPython_ROOT_DIR=.venv
238+
239+
- name: Build C++11
240+
run: cmake --build build -j2
241+
242+
- name: Python tests C++11
243+
run: cmake --build build --target pytest -j2
215244

216245
deadsnakes:
217246
strategy:

.github/workflows/ci_sh_def.yml.patch

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
--- ci.yml 2024-05-26 21:14:20.025983967 -0700
2-
+++ ci_sh_def.yml 2024-05-26 21:14:44.673959335 -0700
1+
--- ci.yml 2024-06-09 23:55:50.405348417 -0700
2+
+++ ci_sh_def.yml 2024-06-09 23:56:22.061331136 -0700
33
@@ -1,4 +1,16 @@
44
-name: CI
55
+# PLEASE KEEP THIS GROUP OF FILES IN SYNC AT ALL TIMES:
@@ -51,23 +51,23 @@
5151
-DPYBIND11_INTERNALS_VERSION=10000000
5252
${{ matrix.args }}
5353

54-
@@ -264,6 +279,7 @@
54+
@@ -293,6 +308,7 @@
5555
-DDOWNLOAD_CATCH=ON
5656
-DDOWNLOAD_EIGEN=ON
5757
-DCMAKE_CXX_STANDARD=17
5858
+ -DCMAKE_CXX_FLAGS="-DPYBIND11_USE_SMART_HOLDER_AS_DEFAULT"
5959

6060
- name: Build
6161
run: cmake --build build -j 2
62-
@@ -332,6 +348,7 @@
62+
@@ -361,6 +377,7 @@
6363
-DPYBIND11_WERROR=ON
6464
-DDOWNLOAD_CATCH=ON
6565
-DCMAKE_CXX_STANDARD=${{ matrix.std }}
6666
+ -DCMAKE_CXX_FLAGS="-DPYBIND11_USE_SMART_HOLDER_AS_DEFAULT"
6767
-DPYTHON_EXECUTABLE=$(python3 -c "import sys; print(sys.executable)")
6868

6969
- name: Build
70-
@@ -361,7 +378,7 @@
70+
@@ -390,7 +407,7 @@
7171
run: apt-get update && DEBIAN_FRONTEND="noninteractive" apt-get install -y cmake git python3-dev python3-pytest python3-numpy
7272

7373
- name: Configure
@@ -76,7 +76,7 @@
7676

7777
- name: Build
7878
run: cmake --build build -j2 --verbose
79-
@@ -449,7 +466,7 @@
79+
@@ -478,7 +495,7 @@
8080
cmake -S . -B build -DDOWNLOAD_CATCH=ON \
8181
-DCMAKE_CXX_STANDARD=17 \
8282
-DPYTHON_EXECUTABLE=$(python3 -c "import sys; print(sys.executable)") \
@@ -85,127 +85,127 @@
8585
-DPYBIND11_TEST_FILTER="test_smart_ptr.cpp"
8686

8787
- name: Build
88-
@@ -503,6 +520,7 @@
88+
@@ -532,6 +549,7 @@
8989
-DPYBIND11_WERROR=ON
9090
-DDOWNLOAD_CATCH=ON
9191
-DCMAKE_CXX_STANDARD=${{ matrix.std }}
9292
+ -DCMAKE_CXX_FLAGS="-DPYBIND11_USE_SMART_HOLDER_AS_DEFAULT"
9393
-DPYTHON_EXECUTABLE=$(python3 -c "import sys; print(sys.executable)")
9494

9595
- name: Build
96-
@@ -525,6 +543,7 @@
96+
@@ -554,6 +572,7 @@
9797
-DPYBIND11_WERROR=ON
9898
-DDOWNLOAD_CATCH=ON
9999
-DCMAKE_CXX_STANDARD=${{ matrix.std }}
100100
+ -DCMAKE_CXX_FLAGS="-DPYBIND11_USE_SMART_HOLDER_AS_DEFAULT"
101101
-DPYTHON_EXECUTABLE=$(python3 -c "import sys; print(sys.executable)")
102102
"-DPYBIND11_TEST_OVERRIDE=test_call_policies.cpp;test_gil_scoped.cpp;test_thread.cpp"
103103

104-
@@ -574,6 +593,7 @@
104+
@@ -603,6 +622,7 @@
105105
-DDOWNLOAD_CATCH=ON \
106106
-DDOWNLOAD_EIGEN=OFF \
107107
-DCMAKE_CXX_STANDARD=11 \
108108
+ -DCMAKE_CXX_FLAGS="-DPYBIND11_USE_SMART_HOLDER_AS_DEFAULT" \
109109
-DCMAKE_CXX_COMPILER=$(which icpc) \
110110
-DPYTHON_EXECUTABLE=$(python3 -c "import sys; print(sys.executable)")
111111

112-
@@ -606,6 +626,7 @@
112+
@@ -635,6 +655,7 @@
113113
-DDOWNLOAD_CATCH=ON \
114114
-DDOWNLOAD_EIGEN=OFF \
115115
-DCMAKE_CXX_STANDARD=17 \
116116
+ -DCMAKE_CXX_FLAGS="-DPYBIND11_USE_SMART_HOLDER_AS_DEFAULT" \
117117
-DCMAKE_CXX_COMPILER=$(which icpc) \
118118
-DPYTHON_EXECUTABLE=$(python3 -c "import sys; print(sys.executable)")
119119

120-
@@ -684,6 +705,7 @@
120+
@@ -713,6 +734,7 @@
121121
-DDOWNLOAD_CATCH=ON
122122
-DDOWNLOAD_EIGEN=ON
123123
-DCMAKE_CXX_STANDARD=11
124124
+ -DCMAKE_CXX_FLAGS="-DPYBIND11_USE_SMART_HOLDER_AS_DEFAULT"
125125
-DPYTHON_EXECUTABLE=$(python3 -c "import sys; print(sys.executable)")
126126

127127
- name: Build
128-
@@ -734,6 +756,7 @@
128+
@@ -763,6 +785,7 @@
129129
cmake ../pybind11-tests
130130
-DDOWNLOAD_CATCH=ON
131131
-DPYBIND11_WERROR=ON
132132
+ -DCMAKE_CXX_FLAGS="-DPYBIND11_USE_SMART_HOLDER_AS_DEFAULT"
133133
-DPYTHON_EXECUTABLE=$(python3 -c "import sys; print(sys.executable)")
134134
working-directory: /build-tests
135135

136-
@@ -829,6 +852,7 @@
136+
@@ -858,6 +881,7 @@
137137
-DPYBIND11_WERROR=ON
138138
-DDOWNLOAD_CATCH=ON
139139
-DDOWNLOAD_EIGEN=ON
140140
+ -DCMAKE_CXX_FLAGS="/GR /EHsc /DPYBIND11_USE_SMART_HOLDER_AS_DEFAULT"
141141
${{ matrix.args }}
142142
- name: Build C++11
143143
run: cmake --build build -j 2
144-
@@ -883,6 +907,7 @@
144+
@@ -912,6 +936,7 @@
145145
-DPYBIND11_WERROR=ON
146146
-DDOWNLOAD_CATCH=ON
147147
-DDOWNLOAD_EIGEN=ON
148148
+ -DCMAKE_CXX_FLAGS="/GR /EHsc /DPYBIND11_USE_SMART_HOLDER_AS_DEFAULT"
149149
${{ matrix.args }}
150150
- name: Build C++11
151151
run: cmake --build build --config Debug -j 2
152-
@@ -925,6 +950,7 @@
152+
@@ -954,6 +979,7 @@
153153
-DDOWNLOAD_CATCH=ON
154154
-DDOWNLOAD_EIGEN=ON
155155
-DCMAKE_CXX_STANDARD=20
156156
+ -DCMAKE_CXX_FLAGS="/GR /EHsc /DPYBIND11_USE_SMART_HOLDER_AS_DEFAULT"
157157

158158
- name: Build C++20
159159
run: cmake --build build -j 2
160-
@@ -945,6 +971,7 @@
160+
@@ -974,6 +1000,7 @@
161161
-DDOWNLOAD_CATCH=ON
162162
-DDOWNLOAD_EIGEN=ON
163163
-DCMAKE_CXX_STANDARD=20
164164
+ -DCMAKE_CXX_FLAGS="/GR /EHsc /DPYBIND11_USE_SMART_HOLDER_AS_DEFAULT"
165165
"-DPYBIND11_TEST_OVERRIDE=test_call_policies.cpp;test_gil_scoped.cpp;test_thread.cpp"
166166

167167
- name: Build C++20 - Exercise cmake -DPYBIND11_TEST_OVERRIDE
168-
@@ -997,6 +1024,7 @@
168+
@@ -1026,6 +1053,7 @@
169169
run: >-
170170
cmake -G "MinGW Makefiles" -DCMAKE_CXX_STANDARD=11 -DPYBIND11_WERROR=ON -DDOWNLOAD_CATCH=ON
171171
-DPYTHON_EXECUTABLE=$(python -c "import sys; print(sys.executable)")
172172
+ -DCMAKE_CXX_FLAGS="-DPYBIND11_USE_SMART_HOLDER_AS_DEFAULT"
173173
-S . -B build
174174

175175
- name: Build C++11
176-
@@ -1018,6 +1046,7 @@
176+
@@ -1047,6 +1075,7 @@
177177
run: >-
178178
cmake -G "MinGW Makefiles" -DCMAKE_CXX_STANDARD=14 -DPYBIND11_WERROR=ON -DDOWNLOAD_CATCH=ON
179179
-DPYTHON_EXECUTABLE=$(python -c "import sys; print(sys.executable)")
180180
+ -DCMAKE_CXX_FLAGS="-DPYBIND11_USE_SMART_HOLDER_AS_DEFAULT"
181181
-S . -B build2
182182

183183
- name: Build C++14
184-
@@ -1039,6 +1068,7 @@
184+
@@ -1068,6 +1097,7 @@
185185
run: >-
186186
cmake -G "MinGW Makefiles" -DCMAKE_CXX_STANDARD=17 -DPYBIND11_WERROR=ON -DDOWNLOAD_CATCH=ON
187187
-DPYTHON_EXECUTABLE=$(python -c "import sys; print(sys.executable)")
188188
+ -DCMAKE_CXX_FLAGS="-DPYBIND11_USE_SMART_HOLDER_AS_DEFAULT"
189189
-S . -B build3
190190

191191
- name: Build C++17
192-
@@ -1106,6 +1136,7 @@
192+
@@ -1135,6 +1165,7 @@
193193
-DDOWNLOAD_EIGEN=ON
194194
-DCMAKE_CXX_COMPILER=clang++
195195
-DCMAKE_CXX_STANDARD=17
196196
+ -DCMAKE_CXX_FLAGS="-DPYBIND11_USE_SMART_HOLDER_AS_DEFAULT"
197197

198198
- name: Build
199199
run: cmake --build . -j 2
200-
@@ -1171,6 +1202,7 @@
200+
@@ -1200,6 +1231,7 @@
201201
-DDOWNLOAD_EIGEN=ON
202202
-DCMAKE_CXX_COMPILER=clang++
203203
-DCMAKE_CXX_STANDARD=17
204204
+ -DCMAKE_CXX_FLAGS="-DPYBIND11_USE_SMART_HOLDER_AS_DEFAULT"
205205
-DPYTHON_EXECUTABLE=$(python3 -c "import sys; print(sys.executable)")
206206

207207
- name: Build
208-
@@ -1194,6 +1226,7 @@
208+
@@ -1223,6 +1255,7 @@
209209
-DDOWNLOAD_EIGEN=ON
210210
-DCMAKE_CXX_COMPILER=clang++
211211
-DCMAKE_CXX_STANDARD=17

.pre-commit-config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ repos:
3232

3333
# Ruff, the Python auto-correcting linter/formatter written in Rust
3434
- repo: https://github.com/astral-sh/ruff-pre-commit
35-
rev: v0.4.5
35+
rev: v0.4.7
3636
hooks:
3737
- id: ruff
3838
args: ["--fix", "--show-fixes"]

CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ option(PYBIND11_NUMPY_1_ONLY
116116
set(PYBIND11_INTERNALS_VERSION
117117
""
118118
CACHE STRING "Override the ABI version, may be used to enable the unstable ABI.")
119+
option(PYBIND11_USE_CROSSCOMPILING "Respect CMAKE_CROSSCOMPILING" OFF)
119120

120121
if(PYBIND11_DISABLE_HANDLE_TYPE_NAME_DEFAULT_IMPLEMENTATION)
121122
add_compile_definitions(PYBIND11_DISABLE_HANDLE_TYPE_NAME_DEFAULT_IMPLEMENTATION)
@@ -312,6 +313,7 @@ if(PYBIND11_INSTALL)
312313
tools/pybind11Common.cmake
313314
tools/pybind11Tools.cmake
314315
tools/pybind11NewTools.cmake
316+
tools/pybind11GuessPythonExtSuffix.cmake
315317
DESTINATION ${PYBIND11_CMAKECONFIG_INSTALL_DIR})
316318

317319
if(NOT PYBIND11_EXPORT_NAME)

include/pybind11/detail/class.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -440,7 +440,11 @@ inline void clear_patients(PyObject *self) {
440440
auto *instance = reinterpret_cast<detail::instance *>(self);
441441
auto &internals = get_internals();
442442
auto pos = internals.patients.find(self);
443-
assert(pos != internals.patients.end());
443+
444+
if (pos == internals.patients.end()) {
445+
pybind11_fail("FATAL: Internal consistency check failed: Invalid clear_patients() call.");
446+
}
447+
444448
// Clearing the patients can cause more Python code to run, which
445449
// can invalidate the iterator. Extract the vector of patients
446450
// from the unordered_map first.

include/pybind11/pybind11.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3124,7 +3124,7 @@ get_type_override(const void *this_ptr, const type_info *this_type, const char *
31243124
PyObject *locals = PyEval_GetFrameLocals();
31253125
# else
31263126
PyObject *locals = PyEval_GetLocals();
3127-
Py_INCREF(locals);
3127+
Py_XINCREF(locals);
31283128
# endif
31293129
if (locals != nullptr) {
31303130
# if PY_VERSION_HEX >= 0x030b0000

include/pybind11/pytypes.h

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,15 @@ class object_api : public pyobject_tag {
190190
str_attr_accessor doc() const;
191191

192192
/// Return the object's current reference count
193-
int ref_count() const { return static_cast<int>(Py_REFCNT(derived().ptr())); }
193+
ssize_t ref_count() const {
194+
#ifdef PYPY_VERSION
195+
// PyPy uses the top few bits for REFCNT_FROM_PYPY & REFCNT_FROM_PYPY_LIGHT
196+
// Following pybind11 2.12.1 and older behavior and removing this part
197+
return static_cast<ssize_t>(static_cast<int>(Py_REFCNT(derived().ptr())));
198+
#else
199+
return Py_REFCNT(derived().ptr());
200+
#endif
201+
}
194202

195203
// TODO PYBIND11_DEPRECATED(
196204
// "Call py::type::handle_of(h) or py::type::of(h) instead of h.get_type()")
@@ -2195,6 +2203,11 @@ class list : public object {
21952203
throw error_already_set();
21962204
}
21972205
}
2206+
void clear() /* py-non-const */ {
2207+
if (PyList_SetSlice(m_ptr, 0, PyList_Size(m_ptr), nullptr) == -1) {
2208+
throw error_already_set();
2209+
}
2210+
}
21982211
};
21992212

22002213
class args : public tuple {

tests/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -363,7 +363,7 @@ if(Boost_FOUND)
363363
add_library(Boost::headers IMPORTED INTERFACE)
364364
if(TARGET Boost::boost)
365365
# Classic FindBoost
366-
set_property(TARGET Boost::boost PROPERTY INTERFACE_LINK_LIBRARIES Boost::boost)
366+
set_property(TARGET Boost::headers PROPERTY INTERFACE_LINK_LIBRARIES Boost::boost)
367367
else()
368368
# Very old FindBoost, or newer Boost than CMake in older CMakes
369369
set_property(TARGET Boost::headers PROPERTY INTERFACE_INCLUDE_DIRECTORIES

tests/extra_python_package/test_files.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@
8686
"share/cmake/pybind11/pybind11Common.cmake",
8787
"share/cmake/pybind11/pybind11Config.cmake",
8888
"share/cmake/pybind11/pybind11ConfigVersion.cmake",
89+
"share/cmake/pybind11/pybind11GuessPythonExtSuffix.cmake",
8990
"share/cmake/pybind11/pybind11NewTools.cmake",
9091
"share/cmake/pybind11/pybind11Targets.cmake",
9192
"share/cmake/pybind11/pybind11Tools.cmake",

tests/pybind11_tests.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,8 @@ PYBIND11_MODULE(pybind11_tests, m) {
8989
#endif
9090
m.attr("cpp_std") = cpp_std();
9191
m.attr("PYBIND11_INTERNALS_ID") = PYBIND11_INTERNALS_ID;
92+
// Free threaded Python uses UINT32_MAX for immortal objects.
93+
m.attr("PYBIND11_REFCNT_IMMORTAL") = UINT32_MAX;
9294
m.attr("PYBIND11_SIMPLE_GIL_MANAGEMENT") =
9395
#if defined(PYBIND11_SIMPLE_GIL_MANAGEMENT)
9496
true;

tests/test_class.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import pytest
44

55
import env
6-
from pybind11_tests import ConstructorStats, UserType
6+
from pybind11_tests import PYBIND11_REFCNT_IMMORTAL, ConstructorStats, UserType
77
from pybind11_tests import class_ as m
88

99

@@ -377,7 +377,9 @@ class PyDog(m.Dog):
377377
refcount_3 = getrefcount(cls)
378378

379379
assert refcount_1 == refcount_3
380-
assert refcount_2 > refcount_1
380+
assert (refcount_2 > refcount_1) or (
381+
refcount_2 == refcount_1 == PYBIND11_REFCNT_IMMORTAL
382+
)
381383

382384

383385
def test_reentrant_implicit_conversion_failure(msg):

tests/test_embed/CMakeLists.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,13 @@ if("${PYTHON_MODULE_EXTENSION}" MATCHES "pypy" OR "${Python_INTERPRETER_ID}" STR
77
return()
88
endif()
99

10+
if(TARGET Python::Module AND NOT TARGET Python::Python)
11+
message(STATUS "Skipping embed test since no embed libs found")
12+
add_custom_target(cpptest) # Dummy target since embedding is not supported.
13+
set(_suppress_unused_variable_warning "${DOWNLOAD_CATCH}")
14+
return()
15+
endif()
16+
1017
find_package(Catch 2.13.9)
1118

1219
if(CATCH_FOUND)

0 commit comments

Comments
 (0)