Skip to content

Commit 383dcb5

Browse files
committed
Merge branch 'master' into stl-typing
2 parents db17f2d + f3c1913 commit 383dcb5

30 files changed

+328
-289
lines changed

.github/workflows/ci.yml

Lines changed: 14 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,9 @@ jobs:
3030
strategy:
3131
fail-fast: false
3232
matrix:
33-
runs-on: [ubuntu-20.04, windows-2022, macos-13]
33+
runs-on: [ubuntu-22.04, windows-2022, macos-13]
3434
python:
3535
- '3.8'
36-
- '3.9'
37-
- '3.12'
3836
- '3.13'
3937
- 'pypy-3.10'
4038
- 'pypy-3.11'
@@ -47,28 +45,28 @@ jobs:
4745
# We support an optional key: args, for cmake args
4846
include:
4947
# Just add a key
50-
- runs-on: ubuntu-20.04
48+
- runs-on: ubuntu-22.04
5149
python: '3.8'
5250
args: >
5351
-DPYBIND11_FINDPYTHON=OFF
5452
-DCMAKE_CXX_FLAGS="-D_=1"
5553
-DPYBIND11_NUMPY_1_ONLY=ON
5654
exercise_D_: 1
57-
- runs-on: windows-2019
55+
- runs-on: windows-2022
5856
python: '3.8'
5957
args: >
6058
-DPYBIND11_FINDPYTHON=OFF
61-
# Inject a couple Windows 2019 runs
59+
# Inject a Windows 2019 run
6260
- runs-on: windows-2019
6361
python: '3.9'
6462
# Inject a few runs with different runtime libraries
6563
- runs-on: windows-2022
66-
python: '3.9'
64+
python: '3.8'
6765
args: >
6866
-DCMAKE_MSVC_RUNTIME_LIBRARY=MultiThreaded
6967
-DPYBIND11_NUMPY_1_ONLY=ON
7068
- runs-on: windows-2022
71-
python: '3.10'
69+
python: '3.9'
7270
args: >
7371
-DCMAKE_MSVC_RUNTIME_LIBRARY=MultiThreadedDLL
7472
# This needs a python built with MTd
@@ -77,28 +75,25 @@ jobs:
7775
# args: >
7876
# -DCMAKE_MSVC_RUNTIME_LIBRARY=MultiThreadedDebug
7977
- runs-on: windows-2022
80-
python: '3.12'
78+
python: '3.13'
8179
args: >
8280
-DCMAKE_MSVC_RUNTIME_LIBRARY=MultiThreadedDebugDLL
83-
# Extra ubuntu latest job
84-
- runs-on: ubuntu-latest
85-
python: '3.11'
8681
# Run tests with py::smart_holder as the default holder
8782
# with recent (or ideally latest) released Python version.
8883
- runs-on: ubuntu-latest
8984
python: '3.12'
9085
args: >
9186
-DCMAKE_CXX_FLAGS="-DPYBIND11_RUN_TESTING_WITH_SMART_HOLDER_AS_DEFAULT_BUT_NEVER_USE_IN_PRODUCTION_PLEASE"
9287
- runs-on: macos-13
93-
python: '3.12'
88+
python: '3.11'
9489
args: >
9590
-DCMAKE_CXX_FLAGS="-DPYBIND11_RUN_TESTING_WITH_SMART_HOLDER_AS_DEFAULT_BUT_NEVER_USE_IN_PRODUCTION_PLEASE"
9691
- runs-on: windows-2022
97-
python: '3.12'
92+
python: '3.10'
9893
args: >
9994
-DCMAKE_CXX_FLAGS="/DPYBIND11_RUN_TESTING_WITH_SMART_HOLDER_AS_DEFAULT_BUT_NEVER_USE_IN_PRODUCTION_PLEASE /GR /EHsc"
100-
- python: 'graalpy-24.1'
101-
runs-on: 'ubuntu-latest'
95+
- runs-on: 'ubuntu-latest'
96+
python: 'graalpy-24.1'
10297
exclude:
10398
# The setup-python action currently doesn't have graalpy for windows
10499
# See https://github.com/actions/setup-python/pull/880
@@ -182,7 +177,7 @@ jobs:
182177
# More-or-less randomly adding -DPYBIND11_SIMPLE_GIL_MANAGEMENT=OFF here.
183178
- name: Configure C++17
184179
run: >
185-
cmake -S . -B build2
180+
cmake -S . -B build2 -Werror=dev
186181
-DPYBIND11_WERROR=ON
187182
-DPYBIND11_SIMPLE_GIL_MANAGEMENT=OFF
188183
-DPYBIND11_PYTEST_ARGS=-v
@@ -200,24 +195,6 @@ jobs:
200195
- name: C++ tests
201196
run: cmake --build build2 --target cpptest
202197

203-
# Third build - C++17 mode with unstable ABI
204-
- name: Configure (unstable ABI)
205-
run: >
206-
cmake -S . -B build3
207-
-DPYBIND11_WERROR=ON
208-
-DPYBIND11_PYTEST_ARGS=-v
209-
-DDOWNLOAD_CATCH=ON
210-
-DDOWNLOAD_EIGEN=ON
211-
-DCMAKE_CXX_STANDARD=17
212-
-DPYBIND11_INTERNALS_VERSION=10000000
213-
${{ matrix.args }}
214-
215-
- name: Build (unstable ABI)
216-
run: cmake --build build3 -j 2
217-
218-
- name: Python tests (unstable ABI)
219-
run: cmake --build build3 --target pytest
220-
221198
- name: Interface test
222199
run: cmake --build build2 --target test_cmake_build
223200

@@ -273,7 +250,7 @@ jobs:
273250
python-debug: false
274251

275252
name: "🐍 ${{ matrix.python-version }}${{ matrix.python-debug && '-dbg' || '' }} (deadsnakes)${{ matrix.valgrind && ' • Valgrind' || '' }} • x64"
276-
runs-on: ubuntu-20.04
253+
runs-on: ubuntu-latest
277254

278255
steps:
279256
- uses: actions/checkout@v4
@@ -346,28 +323,16 @@ jobs:
346323
strategy:
347324
fail-fast: false
348325
matrix:
349-
container_suffix:
350-
- ""
351326
include:
352327
- clang: 5
353328
std: 14
354329
- clang: 11
355330
std: 20
356-
- clang: 12
357-
std: 20
358-
- clang: 13
359-
std: 20
360331
- clang: 14
361332
std: 20
362-
- clang: 15
363-
std: 20
364-
container_suffix: "-bullseye"
365333
- clang: 16
366334
std: 20
367335
container_suffix: "-bullseye"
368-
- clang: 17
369-
std: 20
370-
container_suffix: "-bookworm"
371336
- clang: 18
372337
std: 20
373338
container_suffix: "-bookworm"
@@ -531,8 +496,6 @@ jobs:
531496
- { gcc: 9, std: 20 }
532497
- { gcc: 10, std: 17 }
533498
- { gcc: 10, std: 20 }
534-
- { gcc: 11, std: 20 }
535-
- { gcc: 12, std: 20 }
536499
- { gcc: 13, std: 20 }
537500

538501
name: "🐍 3 • GCC ${{ matrix.gcc }} • C++${{ matrix.std }}• x64"
@@ -831,20 +794,12 @@ jobs:
831794
matrix:
832795
python:
833796
- '3.8'
834-
- '3.9'
835797
- '3.10'
836-
- '3.11'
837-
- '3.12'
798+
- '3.13'
838799

839800
include:
840-
- python: '3.12'
841-
args: -DCMAKE_CXX_STANDARD=20
842-
- python: '3.11'
843-
args: -DCMAKE_CXX_STANDARD=20
844801
- python: '3.10'
845802
args: -DCMAKE_CXX_STANDARD=20
846-
- python: '3.9'
847-
args: -DCMAKE_CXX_STANDARD=20
848803
- python: '3.8'
849804
args: -DCMAKE_CXX_STANDARD=17
850805

@@ -892,10 +847,6 @@ jobs:
892847
strategy:
893848
fail-fast: false
894849
matrix:
895-
python:
896-
- 3.8
897-
- 3.9
898-
899850
include:
900851
- python: 3.9
901852
args: -DCMAKE_CXX_STANDARD=20

.github/workflows/configure.yml

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -24,38 +24,38 @@ jobs:
2424
strategy:
2525
fail-fast: false
2626
matrix:
27-
runs-on: [ubuntu-20.04, macos-13, windows-latest]
28-
arch: [x64]
29-
cmake: ["3.26"]
30-
3127
include:
32-
- runs-on: ubuntu-20.04
33-
arch: x64
28+
- runs-on: ubuntu-22.04
3429
cmake: "3.15"
3530

36-
- runs-on: ubuntu-20.04
37-
arch: x64
31+
- runs-on: ubuntu-22.04
32+
cmake: "3.26"
33+
34+
- runs-on: ubuntu-22.04
3835
cmake: "3.29"
3936

4037
- runs-on: macos-13
41-
arch: x64
4238
cmake: "3.15"
4339

40+
- runs-on: macos-14
41+
cmake: "4.0"
42+
4443
- runs-on: windows-2019
45-
arch: x64 # x86 compilers seem to be missing on 2019 image
4644
cmake: "3.18"
4745

48-
name: 🐍 3.8 • CMake ${{ matrix.cmake }} • ${{ matrix.runs-on }}
46+
- runs-on: windows-latest
47+
cmake: "4.0"
48+
49+
name: 🐍 3.11 • CMake ${{ matrix.cmake }} • ${{ matrix.runs-on }}
4950
runs-on: ${{ matrix.runs-on }}
5051

5152
steps:
5253
- uses: actions/checkout@v4
5354

54-
- name: Setup Python 3.8
55+
- name: Setup Python 3.11
5556
uses: actions/setup-python@v5
5657
with:
57-
python-version: 3.8
58-
architecture: ${{ matrix.arch }}
58+
python-version: 3.11
5959

6060
- name: Prepare env
6161
run: python -m pip install -r tests/requirements.txt

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,4 @@ pybind11Targets.cmake
4444
/docs/_build/*
4545
.ipynb_checkpoints/
4646
tests/main.cpp
47+
CMakeUserPresents.json

.pre-commit-config.yaml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,14 @@ repos:
2525

2626
# Clang format the codebase automatically
2727
- repo: https://github.com/pre-commit/mirrors-clang-format
28-
rev: "v19.1.7"
28+
rev: "v20.1.0"
2929
hooks:
3030
- id: clang-format
3131
types_or: [c++, c, cuda]
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.9.9
35+
rev: v0.11.4
3636
hooks:
3737
- id: ruff
3838
args: ["--fix", "--show-fixes"]
@@ -144,14 +144,14 @@ repos:
144144

145145
# PyLint has native support - not always usable, but works for us
146146
- repo: https://github.com/PyCQA/pylint
147-
rev: "v3.3.4"
147+
rev: "v3.3.6"
148148
hooks:
149149
- id: pylint
150150
files: ^pybind11
151151

152152
# Check schemas on some of our YAML files
153153
- repo: https://github.com/python-jsonschema/check-jsonschema
154-
rev: 0.31.2
154+
rev: 0.32.1
155155
hooks:
156156
- id: check-readthedocs
157157
- id: check-github-workflows

CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,16 +129,17 @@ set(PYBIND11_HEADERS
129129
include/pybind11/detail/cpp_conduit.h
130130
include/pybind11/detail/descr.h
131131
include/pybind11/detail/dynamic_raw_ptr_cast_if_possible.h
132+
include/pybind11/detail/exception_translation.h
132133
include/pybind11/detail/function_record_pyobject.h
133134
include/pybind11/detail/init.h
134135
include/pybind11/detail/internals.h
135136
include/pybind11/detail/native_enum_data.h
137+
include/pybind11/detail/pybind11_namespace_macros.h
136138
include/pybind11/detail/struct_smart_holder.h
137139
include/pybind11/detail/type_caster_base.h
138140
include/pybind11/detail/typeid.h
139141
include/pybind11/detail/using_smart_holder.h
140142
include/pybind11/detail/value_and_holder.h
141-
include/pybind11/detail/exception_translation.h
142143
include/pybind11/attr.h
143144
include/pybind11/buffer_info.h
144145
include/pybind11/cast.h

docs/advanced/smart_ptrs.rst

Lines changed: 2 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
.. _py_class_holder:
2+
13
Smart pointers & ``py::class_``
24
###############################
35

@@ -175,59 +177,3 @@ provides ``.get()`` functionality via ``.getPointer()``.
175177
The file :file:`tests/test_smart_ptr.cpp` contains a complete example
176178
that demonstrates how to work with custom reference-counting holder types
177179
in more detail.
178-
179-
180-
Be careful not to accidentally undermine automatic lifetime management
181-
======================================================================
182-
183-
``py::class_``-wrapped objects automatically manage the lifetime of the
184-
wrapped C++ object, in collaboration with the chosen holder type.
185-
When wrapping C++ functions involving raw pointers, care needs to be taken
186-
to not inadvertently transfer ownership, resulting in multiple Python
187-
objects acting as owners, causing heap-use-after-free or double-free errors.
188-
For example:
189-
190-
.. code-block:: cpp
191-
192-
class Child { };
193-
194-
class Parent {
195-
public:
196-
Parent() : child(std::make_shared<Child>()) { }
197-
Child *get_child() { return child.get(); } /* DANGER */
198-
private:
199-
std::shared_ptr<Child> child;
200-
};
201-
202-
PYBIND11_MODULE(example, m) {
203-
py::class_<Child, std::shared_ptr<Child>>(m, "Child");
204-
205-
py::class_<Parent, std::shared_ptr<Parent>>(m, "Parent")
206-
.def(py::init<>())
207-
.def("get_child", &Parent::get_child); /* PROBLEM */
208-
}
209-
210-
The following Python code leads to undefined behavior, likely resulting in
211-
a segmentation fault.
212-
213-
.. code-block:: python
214-
215-
from example import Parent
216-
217-
print(Parent().get_child())
218-
219-
Part of the ``/* PROBLEM */`` here is that pybind11 falls back to using
220-
``return_value_policy::take_ownership`` as the default (see
221-
:ref:`return_value_policies`). The fact that the ``Child`` instance is
222-
already managed by ``std::shared_ptr<Child>`` is lost. Therefore pybind11
223-
will create a second independent ``std::shared_ptr<Child>`` that also
224-
claims ownership of the pointer, eventually leading to heap-use-after-free
225-
or double-free errors.
226-
227-
There are various ways to resolve this issue, either by changing
228-
the ``Child`` or ``Parent`` C++ implementations (e.g. using
229-
``std::enable_shared_from_this<Child>`` as a base class for
230-
``Child``, or adding a member function to ``Parent`` that returns
231-
``std::shared_ptr<Child>``), or if that is not feasible, by using
232-
``return_value_policy::reference_internal``. What is the best approach
233-
depends on the exact situation.

0 commit comments

Comments
 (0)