Skip to content

Commit 9b7c255

Browse files
Merge branch 'pybind:master' into master
2 parents 22a0b2a + 002c05b commit 9b7c255

14 files changed

Lines changed: 90 additions & 103 deletions

.github/workflows/ci.yml

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ jobs:
199199
run: |
200200
uv pip install --python=python --system setuptools
201201
pytest tests/extra_setuptools
202-
if: "!(matrix.runs-on == 'windows-2022')"
202+
if: matrix.runs-on != 'windows-2022'
203203

204204
manylinux:
205205
name: Manylinux on 🐍 3.13t • GIL
@@ -330,9 +330,10 @@ jobs:
330330
container_suffix: "-bullseye"
331331
- clang: 18
332332
std: 20
333+
cxx_flags: "-Werror -Wall -Wextra -Wwrite-strings -Wunreachable-code -Wpointer-arith -Wredundant-decls"
333334
container_suffix: "-bookworm"
334335

335-
name: "🐍 3 • Clang ${{ matrix.clang }} • C++${{ matrix.std }} • x64"
336+
name: "🐍 3 • Clang ${{ matrix.clang }} • C++${{ matrix.std }} • x64${{ matrix.cxx_flags && ' • cxx_flags' || '' }}"
336337
container: "silkeh/clang:${{ matrix.clang }}${{ matrix.container_suffix }}"
337338

338339
steps:
@@ -348,6 +349,7 @@ jobs:
348349
-DPYBIND11_WERROR=ON
349350
-DDOWNLOAD_CATCH=ON
350351
-DCMAKE_CXX_STANDARD=${{ matrix.std }}
352+
-DCMAKE_CXX_FLAGS="${{ matrix.cxx_flags }}"
351353
-DPYTHON_EXECUTABLE=$(python3 -c "import sys; print(sys.executable)")
352354
353355
- name: Build
@@ -491,9 +493,9 @@ jobs:
491493
- { gcc: 9, std: 20 }
492494
- { gcc: 10, std: 17 }
493495
- { gcc: 10, std: 20 }
494-
- { gcc: 13, std: 20 }
496+
- { gcc: 13, std: 20, cxx_flags: "-Wall -Wextra -Wwrite-strings -Wunreachable-code -Wpointer-arith -Wredundant-decls" }
495497

496-
name: "🐍 3 • GCC ${{ matrix.gcc }} • C++${{ matrix.std }}• x64"
498+
name: "🐍 3 • GCC ${{ matrix.gcc }} • C++${{ matrix.std }} • x64${{ matrix.cxx_flags && ' • cxx_flags' || '' }}"
497499
container: "gcc:${{ matrix.gcc }}"
498500

499501
steps:
@@ -515,6 +517,7 @@ jobs:
515517
-DPYBIND11_WERROR=ON
516518
-DDOWNLOAD_CATCH=ON
517519
-DCMAKE_CXX_STANDARD=${{ matrix.std }}
520+
-DCMAKE_CXX_FLAGS="${{ matrix.cxx_flags }}"
518521
-DPYTHON_EXECUTABLE=$(python3 -c "import sys; print(sys.executable)")
519522
520523
- name: Build

.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: "v20.1.0"
28+
rev: "v20.1.3"
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.11.4
35+
rev: v0.11.8
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.6"
147+
rev: "v3.3.7"
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.32.1
154+
rev: 0.33.0
155155
hooks:
156156
- id: check-readthedocs
157157
- id: check-github-workflows

docs/advanced/classes.rst

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,15 +82,21 @@ helper class that is defined as follows:
8282
8383
The ``py::trampoline_self_life_support`` base class is needed to ensure
8484
that a ``std::unique_ptr`` can safely be passed between Python and C++. To
85-
steer clear of notorious pitfalls (e.g. inheritance slicing), it is best
86-
practice to always use the base class, in combination with
85+
help you steer clear of notorious pitfalls (e.g. inheritance slicing),
86+
pybind11 enforces that trampoline classes inherit from
87+
``py::trampoline_self_life_support`` if used in in combination with
8788
``py::smart_holder``.
8889

8990
.. note::
9091
For completeness, the base class has no effect if a holder other than
9192
``py::smart_holder`` used, including the default ``std::unique_ptr<T>``.
92-
Please think twice, though, the pitfalls are very real, and the overhead
93-
for using the safer ``py::smart_holder`` is very likely to be in the noise.
93+
To avoid confusion, pybind11 will fail to compile bindings that combine
94+
``py::trampoline_self_life_support`` with a holder other than
95+
``py::smart_holder``.
96+
97+
Please think twice, though, before deciding to not use the safer
98+
``py::smart_holder``. The pitfalls associated with avoiding it are very
99+
real, and the overhead for using it is very likely in the noise.
94100

95101
The macro :c:macro:`PYBIND11_OVERRIDE_PURE` should be used for pure virtual
96102
functions, and :c:macro:`PYBIND11_OVERRIDE` should be used for functions which have

include/pybind11/conduit/wrap_include_python_h.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050
#endif
5151

5252
#if defined(PYBIND11_DEBUG_MARKER)
53-
# define _DEBUG
53+
# define _DEBUG 1
5454
# undef PYBIND11_DEBUG_MARKER
5555
#endif
5656

include/pybind11/detail/class.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -499,8 +499,13 @@ extern "C" inline void pybind11_object_dealloc(PyObject *self) {
499499
Py_DECREF(type);
500500
}
501501

502+
PYBIND11_WARNING_PUSH
503+
PYBIND11_WARNING_DISABLE_GCC("-Wredundant-decls")
504+
502505
std::string error_string();
503506

507+
PYBIND11_WARNING_POP
508+
504509
/** Create the type which can be used as a common base for all classes. This is
505510
needed in order to satisfy Python's requirements for multiple inheritance.
506511
Return value: New reference. */

include/pybind11/detail/function_record_pyobject.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include "common.h"
1414

1515
#include <cstring>
16+
#include <utility>
1617

1718
PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE)
1819
PYBIND11_NAMESPACE_BEGIN(detail)
@@ -173,9 +174,6 @@ inline int tp_init_impl(PyObject *, PyObject *, PyObject *) {
173174
// return -1; // Unreachable.
174175
}
175176

176-
// The implementation needs the definition of `class cpp_function`.
177-
void tp_dealloc_impl(PyObject *self);
178-
179177
inline void tp_free_impl(void *) {
180178
pybind11_fail("UNEXPECTED CALL OF function_record_PyTypeObject_methods::tp_free_impl");
181179
}
@@ -191,9 +189,11 @@ inline PyObject *reduce_ex_impl(PyObject *self, PyObject *, PyObject *) {
191189
&& PyModule_Check(rec->scope.ptr()) != 0) {
192190
object scope_module = get_scope_module(rec->scope);
193191
if (scope_module) {
194-
return make_tuple(reinterpret_borrow<object>(PyEval_GetBuiltins())["eval"],
195-
make_tuple(str("__import__('importlib').import_module('")
196-
+ scope_module + str("')")))
192+
auto builtins = reinterpret_borrow<dict>(PyEval_GetBuiltins());
193+
auto builtins_eval = builtins["eval"];
194+
auto reconstruct_args = make_tuple(str("__import__('importlib').import_module('")
195+
+ scope_module + str("')"));
196+
return make_tuple(std::move(builtins_eval), std::move(reconstruct_args))
197197
.release()
198198
.ptr();
199199
}

include/pybind11/detail/type_caster_base.h

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -511,9 +511,14 @@ inline PyThreadState *get_thread_state_unchecked() {
511511
void keep_alive_impl(handle nurse, handle patient);
512512
inline PyObject *make_new_instance(PyTypeObject *type);
513513

514+
PYBIND11_WARNING_PUSH
515+
PYBIND11_WARNING_DISABLE_GCC("-Wredundant-decls")
516+
514517
// PYBIND11:REMINDER: Needs refactoring of existing pybind11 code.
515518
inline bool deregister_instance(instance *self, void *valptr, const type_info *tinfo);
516519

520+
PYBIND11_WARNING_POP
521+
517522
PYBIND11_NAMESPACE_BEGIN(smart_holder_type_caster_support)
518523

519524
struct value_and_holder_helper {
@@ -822,11 +827,8 @@ struct load_helper : value_and_holder_helper {
822827

823828
auto *self_life_support
824829
= dynamic_raw_ptr_cast_if_possible<trampoline_self_life_support>(raw_type_ptr);
825-
if (self_life_support == nullptr && python_instance_is_alias) {
826-
throw value_error("Alias class (also known as trampoline) does not inherit from "
827-
"py::trampoline_self_life_support, therefore the ownership of this "
828-
"instance cannot safely be transferred to C++.");
829-
}
830+
// This is enforced indirectly by a static_assert in the class_ implementation:
831+
assert(!python_instance_is_alias || self_life_support);
830832

831833
std::unique_ptr<D> extracted_deleter = holder().template extract_deleter<T, D>(context);
832834

include/pybind11/gil.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,14 @@ PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE)
3232

3333
PYBIND11_NAMESPACE_BEGIN(detail)
3434

35+
PYBIND11_WARNING_PUSH
36+
PYBIND11_WARNING_DISABLE_GCC("-Wredundant-decls")
37+
3538
// forward declarations
3639
PyThreadState *get_thread_state_unchecked();
3740

41+
PYBIND11_WARNING_POP
42+
3843
PYBIND11_NAMESPACE_END(detail)
3944

4045
/* The functions below essentially reproduce the PyGILState_* API using a RAII

include/pybind11/pybind11.h

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include "gil.h"
2121
#include "gil_safe_call_once.h"
2222
#include "options.h"
23+
#include "trampoline_self_life_support.h"
2324
#include "typing.h"
2425

2526
#include <cassert>
@@ -1982,7 +1983,21 @@ class class_ : public detail::generic_type {
19821983
"Unknown/invalid class_ template parameters provided");
19831984

19841985
static_assert(!has_alias || std::is_polymorphic<type>::value,
1985-
"Cannot use an alias class with a non-polymorphic type");
1986+
"Cannot use an alias class (aka trampoline) with a non-polymorphic type");
1987+
1988+
#ifndef PYBIND11_RUN_TESTING_WITH_SMART_HOLDER_AS_DEFAULT_BUT_NEVER_USE_IN_PRODUCTION_PLEASE
1989+
static_assert(!has_alias || !detail::is_smart_holder<holder_type>::value
1990+
|| std::is_base_of<trampoline_self_life_support, type_alias>::value,
1991+
"Alias class (aka trampoline) must inherit from"
1992+
" pybind11::trampoline_self_life_support if used in combination with"
1993+
" pybind11::smart_holder");
1994+
#endif
1995+
static_assert(!has_alias || detail::is_smart_holder<holder_type>::value
1996+
|| !std::is_base_of<trampoline_self_life_support, type_alias>::value,
1997+
"pybind11::trampoline_self_life_support is a smart_holder feature, therefore"
1998+
" an alias class (aka trampoline) should inherit from"
1999+
" pybind11::trampoline_self_life_support only if used in combination with"
2000+
" pybind11::smart_holder");
19862001

19872002
PYBIND11_OBJECT(class_, generic_type, PyType_Check)
19882003

tests/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -364,7 +364,7 @@ elseif(MSVC)
364364
else()
365365
file(
366366
WRITE ${CMAKE_CURRENT_BINARY_DIR}/main.cpp
367-
"#include <filesystem>\nint main(int argc, char ** argv) {\n std::filesystem::path p(argv[0]);\n return p.string().length();\n}"
367+
"#include <filesystem>\nint main(int /*argc*/, char ** argv) {\n std::filesystem::path p(argv[0]);\n return p.string().length();\n}"
368368
)
369369
try_compile(
370370
STD_FS_NO_LIB_NEEDED ${CMAKE_CURRENT_BINARY_DIR}

0 commit comments

Comments
 (0)