Commit Graph

3040 Commits

Author SHA1 Message Date
b-pass
1107c0936e docs: Add documentation for mod_gil_not_used and multiple_interpreters (#5659)
* Add documentation (advanced/misc) for mod_gil_not_used and multiple_interpreters options

* Add some comparison of free-threading vs sub-interpreters

* Formatting

* Reword for clarity

* style: pre-commit fixes

* Suggested changes, including a long-winded explaination of parallelism in python today

* Slight formatting updates and GPT suggested clarifications

* Better section title

* Update docs/advanced/misc.rst

Copilot suggestion

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* Update docs/advanced/misc.rst

Copilot suggestion

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* Update docs/advanced/misc.rst

* Add a deadlock warning, and minor corrections

* Rewrite the example to avoid free-threading risky behaviors

* Oops, missed a line in the example

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Henry Schreiner <HenrySchreinerIII@gmail.com>
2025-05-16 19:45:38 -04:00
Henry Schreiner
af231a6054 chore: use scikit-build-core for the build (#5598)
* chore: use scikit-build-core for the build

Signed-off-by: Henry Schreiner <henryschreineriii@gmail.com>

* fix: support tests job

Signed-off-by: Henry Schreiner <henryschreineriii@gmail.com>

* refactor: use tomlkit instead of manual parsing

Signed-off-by: Henry Schreiner <henryschreineriii@gmail.com>

* tests: add tests for output

Signed-off-by: Henry Schreiner <henryschreineriii@gmail.com>

* chore: remove more unused files

Signed-off-by: Henry Schreiner <henryschreineriii@gmail.com>

* fix: restore global pin

Signed-off-by: Henry Schreiner <henryschreineriii@gmail.com>

* fix: test and fix pinning

Signed-off-by: Henry Schreiner <henryschreineriii@gmail.com>

---------

Signed-off-by: Henry Schreiner <henryschreineriii@gmail.com>
2025-05-15 14:39:23 -04:00
gentlegiantJGC
6aa3b335f4 fix(types): Buffer type hint (#5662)
* Fix Buffer type hint

collections.abc.Buffer was added in Python 3.12.
The previous behaviour should be used prior to this version.

* Fix comment

* Fix indentation

* style: pre-commit fixes

* Fix test

* Add missing import

* style: pre-commit fixes

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
2025-05-14 10:41:08 -04:00
b-pass
95e8f89be1 Support for sub-interpreters (#5564)
* Allow per-interpreter internals/local_internals

* Significant rewrite to avoid using thread_locals as much as possible.

Since we can avoid them by checking this atomic, the cmake config conditional shouldn't be necessary.

The slower path (with thread_locals and extra checks) only comes in when a second interpreter is actually instanciated.

* Add a test for per-interpreter GIL

Uses two extra threads to demonstrate that neither shares a GIL.

* Fix for nonconforming std::atomic constructors on some compilers

* style: pre-commit fixes

* Fix initializer to make MSVC happy.

* Switch to gil_scoped_acquire_simple, get rid of old copy of it from internals.h

* Use the PyThreadState's interp member rather than the thread state itself.

* Be more explicit about the type of the internalspp

* Suggested renamings and rewordings

* Rename find_internals_pp and change it to take in the state dict reference

* Use the old raise_from instead of pybind11_fail

* Move most of the internals initialization into its constructor.

* Move round_up_to_next_pow2 function upwards

* Remove redundant forward decl

* Add a python-driven subinterpreter test

* Disable the python subinterpreter test on emscripten

Can't load the native-built cpp modules.

* Switch the internals pointer pointer to a unique_ptr pointer

* Spelling

* Fix clang-tidy warning, compare pointer to nullptr

* Rename get_interpreter_counter to get_num_interpreters_seen

* Try simplifying the test's cmake set_target_properties

* Replace mod_* tags with a single tag w/enum

Update tests accordingly

* Add a test for shared-GIL (legacy) subinterpreters

* Update test to work around differences in the various versions of interpreters modules

* Fix unused parameter

* Rename tests and associated test modules.

* Switch get_internals_pp to a template function

* Rename curtstate to cur_tstate

* refactor: use simpler names

Signed-off-by: Henry Schreiner <henryschreineriii@gmail.com>

* style: pre-commit fixes

* fix: return class, not enum

Co-authored-by: Ralf W. Grosse-Kunstleve <rwgkio@gmail.com>
Signed-off-by: Henry Schreiner <henryschreineriii@gmail.com>

* Have to join these threads to make sure they are totally done before the test returns.

* Wrap module_def initialization in a static so it only happens once.

If it happens concurrently in multiple threads, badness ensues....

* style: pre-commit fixes

---------

Signed-off-by: Henry Schreiner <henryschreineriii@gmail.com>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Henry Schreiner <henryschreineriii@gmail.com>
Co-authored-by: Ralf W. Grosse-Kunstleve <rwgkio@gmail.com>
2025-05-14 00:59:44 -07:00
Henry Schreiner
05a6a03e7d chore(cmake): add CMake presets (#5655)
* chore(cmake): add CMake presets

Signed-off-by: Henry Schreiner <henryschreineriii@gmail.com>

* Update .github/workflows/ci.yml

* fix: don't autodetect `.venv` if inside a VIRTUALENV already

Signed-off-by: Henry Schreiner <henryschreineriii@gmail.com>

* Update CMakeLists.txt

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

---------

Signed-off-by: Henry Schreiner <henryschreineriii@gmail.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
2025-05-14 01:29:24 -04:00
Henry Schreiner
853bafa0a2 fix: use original dict (#5658)
* fix: use original dict

Signed-off-by: Henry Schreiner <henryschreineriii@gmail.com>

* refactor: handle unset dict just in case

Signed-off-by: Henry Schreiner <henryschreineriii@gmail.com>

---------

Signed-off-by: Henry Schreiner <henryschreineriii@gmail.com>
2025-05-13 14:09:05 -04:00
Henry Schreiner
c5dc6f9fb7 ci: bump default versions (#5657)
Signed-off-by: Henry Schreiner <henryschreineriii@gmail.com>
2025-05-13 10:08:34 -04:00
Noah Oblath
74b5242713 feat: add py::potentially_slicing_weak_ptr(handle) function (#5624)
* Added weak_ptr test (currently failing)

* Cleanup

* [skip ci] Simplify test case.

* Add test_class_sp_trampoline_weak_ptr.cpp,py (using std::shared_ptr as holder). Tweak test_class_sh_trampoline_weak_ptr.py to pass, with `# THIS NEEDS FIXING` comment.

* Resolve clang-tidy errors

```
/__w/pybind11/pybind11/tests/test_class_sh_trampoline_weak_ptr.cpp:23:43: error: the parameter 'sp' is copied for each invocation but only used as a const reference; consider making it a const reference [performance-unnecessary-value-param,-warnings-as-errors]
   23 |     void set_wp(std::shared_ptr<VirtBase> sp) { wp = sp; }
      |                                           ^
      |                 const                    &
4443 warnings generated.
```

```
/__w/pybind11/pybind11/tests/test_class_sp_trampoline_weak_ptr.cpp:23:43: error: the parameter 'sp' is copied for each invocation but only used as a const reference; consider making it a const reference [performance-unnecessary-value-param,-warnings-as-errors]
   23 |     void set_wp(std::shared_ptr<VirtBase> sp) { wp = sp; }
      |                                           ^
      |                 const                    &
4430 warnings generated.
```

* PYPY, GRAALPY: skip last part of test_weak_ptr_base

* Rename test_weak_ptr_base → test_weak_ptr_owner

* Add SpOwner, test_with_sp_owner, test_with_sp_and_wp_owners

* Modify py::trampoline_self_life_support semantics: if trampoline class does not inherit from this class, preserve established Inheritance Slicing behavior.

rwgk reached this point with the help of ChatGPT:

* https://chatgpt.com/share/68056498-7d94-8008-8ff0-232e2aba451c

The only production code change in this commit is:

```
diff --git a/include/pybind11/detail/type_caster_base.h b/include/pybind11/detail/type_caster_base.h
index d4f9a41e..f3d45301 100644
--- a/include/pybind11/detail/type_caster_base.h
+++ b/include/pybind11/detail/type_caster_base.h
@@ -776,6 +776,14 @@ struct load_helper : value_and_holder_helper {
                 if (released_ptr) {
                     return std::shared_ptr<T>(released_ptr, type_raw_ptr);
                 }
+                auto *self_life_support
+                    = dynamic_raw_ptr_cast_if_possible<trampoline_self_life_support>(type_raw_ptr);
+                if (self_life_support == nullptr) {
+                    std::shared_ptr<void> void_shd_ptr = hld.template as_shared_ptr<void>();
+                    std::shared_ptr<T> to_be_released(void_shd_ptr, type_raw_ptr);
+                    vptr_gd_ptr->released_ptr = to_be_released;
+                    return to_be_released;
+                }
                 std::shared_ptr<T> to_be_released(
                     type_raw_ptr, shared_ptr_trampoline_self_life_support(loaded_v_h.inst));
                 vptr_gd_ptr->released_ptr = to_be_released;
```

* Remove debug printf in include/pybind11/pybind11.h

* Resolve MSVC error

```
    11>D:\a\pybind11\pybind11\tests\test_class_sp_trampoline_weak_ptr.cpp(44,50): error C2220: the following warning is treated as an error [D:\a\pybind11\pybind11\build\tests\pybind11_tests.vcxproj]
    11>D:\a\pybind11\pybind11\tests\test_class_sp_trampoline_weak_ptr.cpp(44,50): warning C4458: declaration of 'sp' hides class member [D:\a\pybind11\pybind11\build\tests\pybind11_tests.vcxproj]
             D:\a\pybind11\pybind11\tests\test_class_sp_trampoline_weak_ptr.cpp(54,31):
             see declaration of 'pybind11_tests::class_sp_trampoline_weak_ptr::SpOwner::sp'
```

* [skip ci] Undo the production code change under 4638e017b6

Also undo the corresponding test change in test_class_sh_trampoline_weak_ptr.py

But keep all extra debugging code for now.

* [skip ci] Introduce lambda in `WpOwner::set_wp` bindings, but simply cast to `std::shared_ptr<VirtBase>` for now.

* Add `py::potentially_slicing_shared_ptr()`

* Add `type_id<T>()` to `py::potentially_slicing_shared_ptr()` error message and add test.

* test_potentially_slicing_shared_ptr.cpp,py (for smart_holder only)

* Generalize test_potentially_slicing_shared_ptr.cpp,py for testing with smart_holder and std::shared_ptr as holder.

* Add back test_potentially_slicing_shared_ptr_not_convertible_error(), it got lost accidentally in commit 56d23dc478

* Add simple trampoline state assertions.

* Resolve clang-tidy errors.

```
/__w/pybind11/pybind11/tests/test_potentially_slicing_shared_ptr.cpp:30:9: error: 'magic_token' should be initialized in a member initializer of the constructor [cppcoreguidelines-prefer-member-initializer,-warnings-as-errors]
   29 |     trampoline_is_alive_simple(const trampoline_is_alive_simple &other) {
      |                                                                         : magic_token(other.magic_token)
   30 |         magic_token = other.magic_token;
      |         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/__w/pybind11/pybind11/tests/test_potentially_slicing_shared_ptr.cpp:33:9: error: 'magic_token' should be initialized in a member initializer of the constructor [cppcoreguidelines-prefer-member-initializer,-warnings-as-errors]
   32 |     trampoline_is_alive_simple(trampoline_is_alive_simple &&other) noexcept {
      |                                                                             : magic_token(other.magic_token)
   33 |         magic_token = other.magic_token;
      |         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
```

* Add a (long) C++ comment for py::potentially_slicing_shared_ptr<>()

* [skip ci] Add new "Avoiding Inheritance Slicing and ``std::weak_ptr`` surprises" section in advanced/classes.rst

* [skip ci] Add introductory comment to test_potentially_slicing_shared_ptr.py

* Minimal (!) changes to have py::potentially_slicing_weak_ptr<T>(handle) as the public API. For CI testing, before changing the names around more widely, and the documentation.

* Rename test_potentially_slicing_shared_ptr.cpp,py → test_potentially_slicing_weak_ptr.cpp,py

* Update docs/advanced/classes.rst and C++ comments → potentially_slicing_weak_ptr

* Write "shared_ptr" instead of just "pointer" in a couple places in docs/advanced/classes.rst

* Add comment for force_potentially_slicing_shared_ptr in type_caster_base.h, to make a direct connection py::potentially_slicing_weak_ptr

---------

Co-authored-by: Ralf W. Grosse-Kunstleve <rgrossekunst@nvidia.com>
2025-05-13 10:07:33 -04:00
Henry Schreiner
ce42c4df48 fix(cmake): avoid message if FINDPYTHON NEW (#5656)
Signed-off-by: Henry Schreiner <henryschreineriii@gmail.com>
2025-05-13 00:44:25 -04:00
Henry Schreiner
d7d782c5e0 fix(regression): support embedded submodule (#5650)
* tests: add test for embedded submodule

Signed-off-by: Henry Schreiner <henryschreineriii@gmail.com>

* tests: adjust

Signed-off-by: Henry Schreiner <henryschreineriii@gmail.com>

* Update test_interpreter.cpp

* Update test_interpreter.cpp

* Update test_interpreter.cpp

* Update test_interpreter.cpp

* Update pybind11.h

* fix: end instead of endif

Signed-off-by: Henry Schreiner <henryschreineriii@gmail.com>

* Update include/pybind11/pybind11.h

---------

Signed-off-by: Henry Schreiner <henryschreineriii@gmail.com>
2025-05-12 05:32:36 -04:00
Xuehai Pan
9a191c245d Fix typos for FindPython compact mode: Python_LIRAR{Y,IES} -> Python_LIBRAR{Y,IES} (#5653) 2025-05-10 23:13:17 -04:00
Ralf W. Grosse-Kunstleve
c125cc789c Collect all #define PYBIND11_HAS_... in pybind11/detail/common.h (#5647) 2025-05-08 23:46:01 -07:00
Xuehai Pan
002c05b17e fix: handle MSVC warning C4866: compiler may not enforce left-to-right evaluation order (#5641) 2025-05-06 21:40:54 -04:00
Tobias Leibner
a265a4cf31 fix: define _DEBUG macro to 1 on redefinition (#5639)
According to the msvc documentation, the macro should be defined to 1:
https://learn.microsoft.com/en-us/cpp/preprocessor/predefined-macros
2025-05-06 21:36:42 -04:00
pre-commit-ci[bot]
099583c5fb chore(deps): update pre-commit hooks (#5642) 2025-05-06 21:01:37 -04:00
Ralf W. Grosse-Kunstleve
c630e22c1c Add static_asserts to enforce that py::smart_holder is combined with py::trampoline_self_life_support (#5633)
* Strictly enforce: trampoline must inherit from trampoline_self_life_support when used in combination with smart_holder

* Simplify test_class_sh_trampoline_basic.cpp,py (only one Abase is needed now)

* Replace obsolete sophisticated `throw value_error()` with a simple `assert()`

* Strictly enforce: trampoline should inherit from trampoline_self_life_support only if used in combination with smart_holder

* Resolve clang-tidy error

```
/__w/pybind11/pybind11/tests/test_class_sh_trampoline_basic.cpp:35:46: error: the parameter 'obj' is copied for each invocation but only used as a const reference; consider making it a const reference [performance-unnecessary-value-param,-warnings-as-errors]
   35 | int AddInCppSharedPtr(std::shared_ptr<Abase> obj, int other_val) {
      |                                              ^
      |                       const                 &
```

* Disable new static_assert if PYBIND11_RUN_TESTING_WITH_SMART_HOLDER_AS_DEFAULT_BUT_NEVER_USE_IN_PRODUCTION_PLEASE is defined.
2025-04-30 22:12:13 -07:00
Robert Haschke
c7f3460f18 Fix gcc compiler warnings (#5523)
* CI: Fail on any warnings with clang 18

* CI: Fail on any warnings with gcc 13

* Fix cmake's try_compile warning

* Guard redundant declarations

* ci.yml: fix syntax error

* Use PYBIND11_WARNING macros

* Fix more redundant declarations

... introduced with merge of smart_holder branch
2025-04-30 10:29:00 -07:00
dependabot[bot]
d3fee42905 chore(deps): bump astral-sh/setup-uv from 5 to 6 in the actions group (#5632)
Bumps the actions group with 1 update: [astral-sh/setup-uv](https://github.com/astral-sh/setup-uv).


Updates `astral-sh/setup-uv` from 5 to 6
- [Release notes](https://github.com/astral-sh/setup-uv/releases)
- [Commits](https://github.com/astral-sh/setup-uv/compare/v5...v6)

---
updated-dependencies:
- dependency-name: astral-sh/setup-uv
  dependency-version: '6'
  dependency-type: direct:production
  update-type: version-update:semver-major
  dependency-group: actions
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2025-04-28 11:28:07 -07:00
Bryn Lloyd
bc4a66dff0 fix: provide useful behavior of default py::slice (#5620)
* Change behavior of default py::slice

* make clang-tidy happy

* Update tests/test_pytypes.py

---------

Co-authored-by: Bryn Lloyd <12702862+dyollb@users.noreply.github.com>
Co-authored-by: Henry Schreiner <HenrySchreinerIII@gmail.com>
2025-04-16 18:42:34 -04:00
Bryn Lloyd
5c498583cc fix: upgrade 20.04 runners to 22.04 (fix for ICC, NVHPC) (#5621)
* Upgrade the deprecated 20.04 runners to 22.04 (ICC, NVHPC)

* fix ICC compile error

---------

Co-authored-by: Bryn Lloyd <12702862+dyollb@users.noreply.github.com>
2025-04-16 08:13:23 -04:00
Bryn Lloyd
223e2e9da2 Fix missing pythonic type hints for native_enum (#5619)
* fix missing pythonic type hints for native_enum

* Fix __qualname__ in native_enum

* Rename `enum class native` → `func_sig_rendering`. Add to `ENUM_TYPES_AND_MEMBERS`. Move new code around to fit in more organically.

"native" is used in so many places here, it would be very difficult to pin-point where the specific enum type is used.

Similarly, "value" is difficult to pin-point. Changed to "nested_value".

---------

Co-authored-by: Bryn Lloyd <12702862+dyollb@users.noreply.github.com>
Co-authored-by: Ralf W. Grosse-Kunstleve <rgrossekunst@nvidia.com>
2025-04-15 21:38:15 -07:00
Bryn Lloyd
3c586340fb Add class doc string to native_enum (#5617)
* add class doc string to native_enum

* adapt doc argument name

* fix test, make class enum doc None by default

* fix other python versions?

* make clang-tidy happy

* rename 'enum_doc' to 'class_doc'

* update documentation

* [skip ci] Polish changed documentation (mostly done by ChatGPT).

---------

Co-authored-by: Bryn Lloyd <12702862+dyollb@users.noreply.github.com>
Co-authored-by: Ralf W. Grosse-Kunstleve <rgrossekunst@nvidia.com>
2025-04-14 23:19:06 -07:00
Henry Schreiner
b3bb31ca51 ci: work on speeding up further (#5613)
* ci: work on speeding up further

Signed-off-by: Henry Schreiner <henryschreineriii@gmail.com>

* chore: move uv's index-strategy to pyproject.toml

Signed-off-by: Henry Schreiner <henryschreineriii@gmail.com>

* Update .github/workflows/ci.yml

---------

Signed-off-by: Henry Schreiner <henryschreineriii@gmail.com>
2025-04-14 17:25:32 -04:00
Ralf W. Grosse-Kunstleve
cbcc23855e Factor out pybind11/gil_simple.h (#5614)
* Factor out pybind11/gil_simple.h

* Remove disarm() member functions in pybind11/gil_simple.h
2025-04-13 19:48:32 -07:00
Tim Ohliger
ee04df0d02 Updated STL casters and py::buffer to use collections.abc (#5566)
* Updated STL type hints use support collections.abc

* Updated array_caster to match numpy/eigen typing.Annotated stlye

* Added support for Mapping, Set and Sequence derived from collections.abc.

* Fixed merge of typing.SupportsInt in new tests

* Integrated collections.abc checks into convertible check functions.

* Changed type hint of py::buffer to collections.abc.Buffer

* Changed convertible check function names

* Added comments to convertible check functions

* Removed checks for methods that are already required by the abstract base class

* Improved mapping caster test using more compact a1b2c3 variable

* Renamed and refactored sequence, mapping and set test classes to reuse implementation

* Added tests for mapping and set casters for noconvert mode

* Added tests for sequence caster for noconvert mode
2025-04-13 19:47:19 -07:00
Ralf W. Grosse-Kunstleve
f3c1913827 Remove obsolete "E501" line in pyproject.toml (#5539)
* [skip ci] Add "E5" to [tool.ruff.lint] extend-select, remove obsolete ignore E501

* [skip ci] Remove "E5" again.
2025-04-10 23:09:02 -07:00
Henry Schreiner
662a88cbc1 ci: speed up ci a bit by thinning out matrix (#5602)
* ci: speed up ci a bit by thinning out matrix

Signed-off-by: Henry Schreiner <henryschreineriii@gmail.com>

* Update configure.yml

* Update configure.yml

* Update configure.yml

* ci: drop third build

* ci: fix minor issues

Signed-off-by: Henry Schreiner <henryschreineriii@gmail.com>

* ci: trim a bit more

Signed-off-by: Henry Schreiner <henryschreineriii@gmail.com>

---------

Signed-off-by: Henry Schreiner <henryschreineriii@gmail.com>
2025-04-10 23:21:57 -04:00
pre-commit-ci[bot]
a2951abbec chore(deps): update pre-commit hooks (#5605)
* chore(deps): update pre-commit hooks

updates:
- [github.com/pre-commit/mirrors-clang-format: v19.1.7 → v20.1.0](https://github.com/pre-commit/mirrors-clang-format/compare/v19.1.7...v20.1.0)
- [github.com/astral-sh/ruff-pre-commit: v0.9.9 → v0.11.4](https://github.com/astral-sh/ruff-pre-commit/compare/v0.9.9...v0.11.4)
- [github.com/PyCQA/pylint: v3.3.4 → v3.3.6](https://github.com/PyCQA/pylint/compare/v3.3.4...v3.3.6)
- [github.com/python-jsonschema/check-jsonschema: 0.31.2 → 0.32.1](https://github.com/python-jsonschema/check-jsonschema/compare/0.31.2...0.32.1)

* style: pre-commit fixes

* Update setup.py

* Update tests/test_enum.py

* Update configure.yml

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Henry Schreiner <HenrySchreinerIII@gmail.com>
2025-04-10 18:31:28 -04:00
Henry Schreiner
d25e91fb8f fix(cmake): warning about missing file (#5612)
Signed-off-by: Henry Schreiner <henryschreineriii@gmail.com>
2025-04-10 17:48:16 -04:00
Ralf W. Grosse-Kunstleve
31e52b2cd3 Add holder caster traits tests in test_smart_ptr.cpp,py (#5603)
* Add test_smart_ptr.cpp,py holder_caster_traits_test

* [skip ci] Resolve clang-tidy error with a NOLINT

Tested with clang-tidy locally.
2025-04-10 12:37:50 -07:00
Ralf W. Grosse-Kunstleve
e38beb96c4 [skip ci] Move "Pitfalls with raw pointers and shared ownership" section to a more prominent location. (#5611) 2025-04-10 12:12:00 -07:00
Ralf W. Grosse-Kunstleve
1bd1d1ced8 Change PyCFunction cast in function_record_pyobject.h to sidestep unhelpful compiler warnings. (#5608)
* Change PyCFunction cast in function_record_pyobject.h to sidestep unhelpful compiler warnings.

* Resolve clang-tidy error

/__w/pybind11/pybind11/include/pybind11/detail/function_record_pyobject.h:41:39: error: do not cast 'PyObject *(PyObject *, PyObject *, PyObject *)' (aka '_object *(_object *, _object *, _object *)') to 'PyCFunction' (aka '_object *(*)(_object *, _object *)') through 'void *' [bugprone-casting-through-void,-warnings-as-errors]
   41 |         reinterpret_cast<PyCFunction>(reinterpret_cast<void *>(reduce_ex_impl)),
      |                                       ^
2025-04-10 12:09:59 -07:00
Michael Šimáček
708ce4d9c7 Fix GraalPy version parsing on dev builds (#5609)
* Fix GraalPy version parsing on dev builds

* Unskip test on GraalPy
2025-04-09 09:09:15 -07:00
Ralf W. Grosse-Kunstleve
a28ea8ccc5 Eliminate pybindit namespace (#5607)
* Factor out pybind11/detail/pybind11_namespace_macros.h from pybind11/detail/common.h

* Eliminate `pybindit` namespace, use pybind11/detail/pybind11_namespace_macros.h instead.

* Fix oversight: add pybind11_namespace_macros.h in tests/extra_python_package/test_files.py

* [skip ci] Add copyright notice.
2025-04-08 20:46:24 -07:00
Henry Schreiner
6d1f28fe2b feat: remove make_simple_namespace (#5597)
Signed-off-by: Henry Schreiner <henryschreineriii@gmail.com>
2025-04-08 23:44:18 -04:00
Henry Schreiner
73ad3099fb fix: fully deprecate get_type_of (deprecated in 2.6 but no warning (#5596)
Signed-off-by: Henry Schreiner <henryschreineriii@gmail.com>
2025-04-08 23:43:49 -04:00
Henry Schreiner
b70b8eb332 chore: require pytest 6 consistently (#5599)
Signed-off-by: Henry Schreiner <henryschreineriii@gmail.com>
2025-04-04 12:35:55 -07:00
Henry Schreiner
fdab860a32 feat: drop PYBIND11_NUMPY_1_ONLY (#5595)
* feat: drop PYBIND11_NUMPY_1_ONLY

Signed-off-by: Henry Schreiner <henryschreineriii@gmail.com>

* Add #error "PYBIND11_NUMPY_1_ONLY is no longer supported." in pybind11/numpy.h

---------

Signed-off-by: Henry Schreiner <henryschreineriii@gmail.com>
Co-authored-by: Ralf W. Grosse-Kunstleve <rgrossekunst@nvidia.com>
2025-04-03 22:08:56 -04:00
Henry Schreiner
55b1357d8e chore: update nox and test deps (#5594)
Signed-off-by: Henry Schreiner <henryschreineriii@gmail.com>
2025-04-02 08:24:15 -04:00
Henry Schreiner
8ef10a0ec9 ci: update to GraalPy 24.2 and mention in README (#5586)
* ci: update to GraalPy 24.2 and mention in README

Signed-off-by: Henry Schreiner <henryschreineriii@gmail.com>

* tests: update for GRAALPY 24.2 and better printout

Signed-off-by: Henry Schreiner <henryschreineriii@gmail.com>

* tests: use __graalpython__ for version number

Signed-off-by: Henry Schreiner <henryschreineriii@gmail.com>

* Update README.rst

---------

Signed-off-by: Henry Schreiner <henryschreineriii@gmail.com>
2025-03-31 22:50:55 -04:00
Henry Schreiner
d27fdaa272 chore: update for CMake 4.0 (#5593)
Signed-off-by: Henry Schreiner <henryschreineriii@gmail.com>
2025-03-31 22:50:40 -04:00
Ralf W. Grosse-Kunstleve
e03ec30632 Squashed function_record_std_launder/manuscript — 57b9a0af815d19b236b74be06a172bc5c9956618 — 2025-03-30 20:14:21 -0700 (#5592)
[Browse function_record_std_launder/manuscript tree](57b9a0af81)

[Browse function_record_std_launder/manuscript commits](57b9a0af81/)
2025-03-31 15:54:38 -07:00
Ralf W. Grosse-Kunstleve
a34fcdc48d [ci skip] Response to question by gh-henryiii (https://github.com/pybind/pybind11/pull/5580#discussion_r2019137216) (#5591) 2025-03-30 19:56:07 -07:00
Ralf W. Grosse-Kunstleve
8726ed22d8 Fix build failure when shared_ptr<T> points to private std::enable_shared_from_this base (#5590)
* Squashed private_esft/manuscript — 3f2b1201b830d9e431448bd8f5fe577afaa02dbf — 2025-03-30 12:38:30 -0700

[Browse private_esft/manuscript tree](3f2b1201b8)

[Browse private_esft/manuscript commits](3f2b1201b8/)

* Remove mention of ChatGPT

Co-authored-by: Henry Schreiner <HenrySchreinerIII@gmail.com>

---------

Co-authored-by: Henry Schreiner <HenrySchreinerIII@gmail.com>
2025-03-30 19:43:40 -07:00
Ralf W. Grosse-Kunstleve
e7e5d6e5bb Make wrapped C++ functions pickleable (#5580)
* Backport of https://github.com/google/pybind11clif/pull/30099

* Add back `PYBIND11_WARNING_DISABLE_CLANG("-Wcast-function-type-mismatch")`

macos-13 • brew install llvm

```
/Users/runner/work/pybind11/pybind11/include/pybind11/detail/function_record_pyobject.h:40:26: error: cast from 'PyObject *(*)(PyObject *, PyObject *, PyObject *)' (aka '_object *(*)(_object *, _object *, _object *)') to 'PyCFunction' (aka '_object *(*)(_object *, _object *)') converts to incompatible function type [-Werror,-Wcast-function-type-mismatch]
   40 |     = {{"__reduce_ex__", (PyCFunction) reduce_ex_impl, METH_VARARGS | METH_KEYWORDS, nullptr},
      |                          ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
```

* Remove obsolete `PY_VERSION_HEX >= 0x03080000` (discovered by gh-henryiii)
2025-03-28 11:50:37 -07:00
Henry Schreiner
8f00d1eea0 fix: set __file__ on submodules (#5584)
* fix: set __file__ on submodules

The docs state ['The caller is responsible for setting a `__file__` attribute'](https://docs.python.org/3/c-api/module.html), but we
were not doing that, which interferes with pickling objects in submodules using cloudpickle. Users previously had to set the `__file__` attributes manually,
such as in 1fbbe1632e/src/boost_histogram/histogram.py (L83-L90).

Signed-off-by: Henry Schreiner <henryschreineriii@gmail.com>

* fix: support GraalPy

Signed-off-by: Henry Schreiner <henryschreineriii@gmail.com>

* fix: workaround only for GraalPython

Signed-off-by: Henry Schreiner <henryschreineriii@gmail.com>

* Add GRAALPY_VERSION_NUM to GraalPy workaround

* Fix "GRAALPY_VERSION_NUM not defined" issue

```
/Users/runner/work/pybind11/pybind11/include/pybind11/pybind11.h:1340:32: error: 'GRAALPY_VERSION_NUM' is not defined, evaluates to 0 [-Werror,-Wundef]
                               ^
```

Related ChatGPT conversation: https://chatgpt.com/share/67e6cb99-84b0-8008-99d6-aadc70242cf3

---------

Signed-off-by: Henry Schreiner <henryschreineriii@gmail.com>
Co-authored-by: Ralf W. Grosse-Kunstleve <rgrossekunst@nvidia.com>
2025-03-28 09:57:39 -07:00
Ralf W. Grosse-Kunstleve
c1cd022f16 tests: Add test for boost::histogram::func_transform situation. (#5582)
* Add test for boost::histogram::func_transform situation.

* Resolve clang-tidy errors

```
/__w/pybind11/pybind11/tests/test_callbacks.cpp:33:9: error: 'auto rec' can be declared as 'auto *rec' [readability-qualified-auto,-warnings-as-errors]
   33 |         auto rec = c.get_pointer<py::detail::function_record>();
      |         ^~~~
      |         auto *
/__w/pybind11/pybind11/tests/test_callbacks.cpp:41:13: error: 'auto cap' can be declared as 'auto *cap' [readability-qualified-auto,-warnings-as-errors]
   41 |             auto cap = reinterpret_cast<capture *>(&rec->data);
      |             ^~~~
      |             auto *
```

* Replace `apply_custom_transform()` implementation using `make_caster<std::function<raw_t>>`

* Add func_is_stateless_with_exact_type feature in pybind11/functional.h

* tests: use std::function::target

Signed-off-by: Henry Schreiner <henryschreineriii@gmail.com>

* style: pre-commit fixes

* Resolve clang-tidy error

```
/__w/pybind11/pybind11/tests/test_callbacks.cpp:33:5: error: 'auto cfunc' can be declared as 'auto *cfunc' [readability-qualified-auto,-warnings-as-errors]
   33 |     auto cfunc = func.target<raw_t *>();
      |     ^~~~
      |     auto *
```

---------

Signed-off-by: Henry Schreiner <henryschreineriii@gmail.com>
Co-authored-by: Henry Schreiner <henryschreineriii@gmail.com>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
2025-03-28 11:36:02 -04:00
Ralf W. Grosse-Kunstleve
f365314ec0 Enable Conversions Between Native Python Enum Types and C++ Enums (#5555)
* Apply smart_holder-branch-based PR #5280 on top of master.

* Add pytest.skip("GraalPy does not raise UnicodeDecodeError")

* Add `parent_scope` as first argument to `py::native_enum` ctor.

* Replace `operator+=` API with `.finalize()` API. The error messages still need cleanup.

* Resolve clang-tidy performance-unnecessary-value-param errors

* Rename (effectively) native_enum_add_to_parent() -> finalize()

* Update error message: pybind11::native_enum<...>("Fake", ...): MISSING .finalize()

* Pass py::module_ by reference to resolve clang-tidy errors (this is entirely inconsequential otherwise for all practical purposes).

* test_native_enum_correct_use_failure -> test_native_enum_missing_finalize_failure

* Add test_native_enum_double_finalize(), test_native_enum_value_after_finalize()

* Clean up public/protected API.

* [ci skip] Update the Enumerations section in classes.rst

* Rename `py::native_enum_kind` → `py::enum_kind` as suggested by gh-henryiii:

https://github.com/pybind/pybind11/pull/5555#issuecomment-2711672335

* Experiment: StrEnum

enum.StrEnum does not map to C++ enum:

* https://chatgpt.com/share/67d5e965-ccb0-8008-95b7-0df2502309b3

```
============================= test session starts ==============================
platform linux -- Python 3.12.3, pytest-8.3.3, pluggy-1.5.0
C++ Info: 13.3.0 C++20 __pybind11_internals_v10000000_system_libstdcpp_gxx_abi_1xxx_use_cxx11_abi_1__ PYBIND11_SIMPLE_GIL_MANAGEMENT=False PYBIND11_NUMPY_1_ONLY=False
configfile: pytest.ini
plugins: parallel-0.1.1, xdist-3.6.1
collected 40 items / 39 deselected / 1 selected

test_native_enum.py F                                                    [100%]

=================================== FAILURES ===================================
________________________ test_native_enum_StrEnum_greek ________________________

    def test_native_enum_StrEnum_greek():
        assert not hasattr(m, "greek")
>       m.native_enum_StrEnum_greek(m)

test_native_enum.py:150:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
/usr/lib/python3.12/enum.py:764: in __call__
    return cls._create_(
        boundary   = None
        cls        = <enum 'StrEnum'>
        module     = None
        names      = [('Alpha', 10), ('Omega', 20)]
        qualname   = None
        start      = 1
        type       = None
        value      = 'greek'
        values     = ()
/usr/lib/python3.12/enum.py:917: in _create_
    return metacls.__new__(metacls, class_name, bases, classdict, boundary=boundary)
        _          = <class 'str'>
        bases      = (<enum 'StrEnum'>,)
        boundary   = None
        class_name = 'greek'
        classdict  = {'_generate_next_value_': <function StrEnum._generate_next_value_ at 0x701ec1711e40>, 'Alpha': 10, 'Omega': 20, '__module__': 'test_native_enum'}
        cls        = <enum 'StrEnum'>
        first_enum = <enum 'StrEnum'>
        item       = ('Omega', 20)
        member_name = 'Omega'
        member_value = 20
        metacls    = <class 'enum.EnumType'>
        module     = 'test_native_enum'
        names      = [('Alpha', 10), ('Omega', 20)]
        qualname   = None
        start      = 1
        type       = None
/usr/lib/python3.12/enum.py:606: in __new__
    raise exc.with_traceback(tb)
        __class__  = <class 'enum.EnumType'>
        __new__    = <function StrEnum.__new__ at 0x701ec1711da0>
        _gnv       = <staticmethod(<function StrEnum._generate_next_value_ at 0x701ec1711e40>)>
        _order_    = None
        _simple    = False
        bases      = (<enum 'StrEnum'>,)
        boundary   = None
        classdict  = {'Alpha': <enum._proto_member object at 0x701ebc74f9b0>, 'Omega': <enum._proto_member object at 0x701ebc74cce0>, '__module__': 'test_native_enum', '_all_bits_': 0, ...}
        cls        = 'greek'
        exc        = TypeError('10 is not a string')
        first_enum = <enum 'StrEnum'>
        ignore     = ['_ignore_']
        invalid_names = set()
        key        = '_ignore_'
        kwds       = {}
        member_names = {'Alpha': None, 'Omega': None}
        member_type = <class 'str'>
        metacls    = <class 'enum.EnumType'>
        name       = 'Omega'
        save_new   = False
        tb         = <traceback object at 0x701ebc7a6cc0>
        use_args   = True
        value      = 20
/usr/lib/python3.12/enum.py:596: in __new__
    enum_class = super().__new__(metacls, cls, bases, classdict, **kwds)
        __class__  = <class 'enum.EnumType'>
        __new__    = <function StrEnum.__new__ at 0x701ec1711da0>
        _gnv       = <staticmethod(<function StrEnum._generate_next_value_ at 0x701ec1711e40>)>
        _order_    = None
        _simple    = False
        bases      = (<enum 'StrEnum'>,)
        boundary   = None
        classdict  = {'Alpha': <enum._proto_member object at 0x701ebc74f9b0>, 'Omega': <enum._proto_member object at 0x701ebc74cce0>, '__module__': 'test_native_enum', '_all_bits_': 0, ...}
        cls        = 'greek'
        exc        = TypeError('10 is not a string')
        first_enum = <enum 'StrEnum'>
        ignore     = ['_ignore_']
        invalid_names = set()
        key        = '_ignore_'
        kwds       = {}
        member_names = {'Alpha': None, 'Omega': None}
        member_type = <class 'str'>
        metacls    = <class 'enum.EnumType'>
        name       = 'Omega'
        save_new   = False
        tb         = <traceback object at 0x701ebc7a6cc0>
        use_args   = True
        value      = 20
/usr/lib/python3.12/enum.py:271: in __set_name__
    enum_member = enum_class._new_member_(enum_class, *args)
        args       = (10,)
        enum_class = <enum 'greek'>
        member_name = 'Alpha'
        self       = <enum._proto_member object at 0x701ebc74f9b0>
        value      = 10
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

cls = <enum 'greek'>, values = (10,)

    def __new__(cls, *values):
        "values must already be of type `str`"
        if len(values) > 3:
            raise TypeError('too many arguments for str(): %r' % (values, ))
        if len(values) == 1:
            # it must be a string
            if not isinstance(values[0], str):
>               raise TypeError('%r is not a string' % (values[0], ))
E               TypeError: 10 is not a string

cls        = <enum 'greek'>
values     = (10,)

/usr/lib/python3.12/enum.py:1322: TypeError
=========================== short test summary info ============================
FAILED test_native_enum.py::test_native_enum_StrEnum_greek - TypeError: 10 is...
======================= 1 failed, 39 deselected in 0.07s =======================

ERROR: completed_process.returncode=1
```

* Remove StrEnum code.

* Make enum_kind::Enum the default kind.

* Catch redundant .export_values() calls.

* [ci skip] Add back original documentation for `py::enum_` under new advanced/deprecated.rst

* [ci skip] Add documentation for `py::enum_kind` and `py::detail::type_caster_enum_type_enabled`

* Rename `Type` to `EnumType` for readability.

* Eliminate py::enum_kind, use "enum.Enum", "enum.IntEnum" directly. This is still WIP.

* EXPERIMENTAL StrEnum code. To be removed.

* Remove experimental StrEnum code:

My judgement: Supporting StrEnum is maybe nice, but not very valuable. I don't think it is worth the extra C++ code.

A level of indirection would need to be managed, e.g.

    RED   ↔ Python "r" ↔ C++ 0
    Green ↔ Python "g" ↔ C++ 1

These mappings would need to be stored and processed.

* Add test with enum.IntFlag (no production code changes required).

* First import_or_getattr() implementation (dedicated tests are still missing).

* Fix import_or_getattr() implementation, add tests, fix clang-tidy errors.

* [ci skip] Update classes.rst: replace `py::enum_kind` with `native_type_name`

* For "constructor similar to that of enum.Enum" point to https://docs.python.org/3/howto/enum.html#functional-api, as suggested by gh-timohl (https://github.com/pybind/pybind11/pull/5555#discussion_r2009277507).

* Advertise Enum, IntEnum, Flag, IntFlags are compatible stdlib enum types in the documentation (as suggested by gh-timohl, https://github.com/pybind/pybind11/pull/5555#pullrequestreview-2708832587); add test for enum.Flag to ensure that is actually true.
2025-03-24 20:31:59 -07:00
Ralf W. Grosse-Kunstleve
48eb5ad9b9 Remove PyPy 3.8 and 3.9 testing. Make a pass through the entire repo to remove obviously obsolete workarounds for PyPy < 3.10. (#5578) 2025-03-23 20:35:14 -07:00
Michael Carlstrom
566894d5f1 Fix null pointer dereference in attr_with_type_hint (#5576)
* Fix regression

Signed-off-by: Michael Carlstrom <rmc@carlstrom.com>

* style: pre-commit fixes

* Request changes and fix for func_rec nullptr

Signed-off-by: Michael Carlstrom <rmc@carlstrom.com>

* style: pre-commit fixes

* Fix function_record call

Signed-off-by: Michael Carlstrom <rmc@carlstrom.com>

* style: pre-commit fixes

* try typedef forward declaration

Signed-off-by: Michael Carlstrom <rmc@carlstrom.com>

* refactor

Signed-off-by: Michael Carlstrom <rmc@carlstrom.com>

* style: pre-commit fixes

* remove from .py file

Signed-off-by: Michael Carlstrom <rmc@carlstrom.com>

* address feedback

Signed-off-by: Michael Carlstrom <rmc@carlstrom.com>

* style: pre-commit fixes

* Fix `e.j.` → `e.g.` typo.

---------

Signed-off-by: Michael Carlstrom <rmc@carlstrom.com>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Ralf W. Grosse-Kunstleve <rgrossekunst@nvidia.com>
2025-03-23 10:09:46 -07:00