Commit Graph

3186 Commits

Author SHA1 Message Date
Dimitri Papadopoulos Orfanos
6651530963 Enforce more ruff rules (#5922)
* Apply ruff/flake8-implicit-str-concat rule ISC003

ISC003 Explicitly concatenated string should be implicitly concatenated

* Enforce ruff/flake8-implicit-str-concat rules (ISC)

* Apply ruff/Perflint rule PERF102

PERF102 When using only the values of a dict use the `values()` method

* Apply ruff/Perflint rule PERF401

PERF401 Use a list comprehension to create a transformed list

* Enforce ruff/Perflint rules (PERF)
2025-12-08 09:25:25 -08:00
Dimitri Papadopoulos Orfanos
1c1d01c8a1 Use well-known labels in project.urls (#5921)
https://packaging.python.org/en/latest/specifications/well-known-project-urls/#well-known-labels
2025-12-08 09:17:17 -08:00
dependabot[bot]
1dc76208d5 chore(deps): bump urllib3 from 2.5.0 to 2.6.0 in /docs (#5920)
Bumps [urllib3](https://github.com/urllib3/urllib3) from 2.5.0 to 2.6.0.
- [Release notes](https://github.com/urllib3/urllib3/releases)
- [Changelog](https://github.com/urllib3/urllib3/blob/main/CHANGES.rst)
- [Commits](https://github.com/urllib3/urllib3/compare/2.5.0...2.6.0)

---
updated-dependencies:
- dependency-name: urllib3
  dependency-version: 2.6.0
  dependency-type: indirect
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2025-12-06 09:18:17 -08:00
pre-commit-ci[bot]
d810d4f039 chore(deps): update pre-commit hooks (#5918)
* chore(deps): update pre-commit hooks

updates:
- [github.com/pre-commit/mirrors-clang-format: v21.1.2 → v21.1.6](https://github.com/pre-commit/mirrors-clang-format/compare/v21.1.2...v21.1.6)
- [github.com/astral-sh/ruff-pre-commit: v0.14.3 → v0.14.7](https://github.com/astral-sh/ruff-pre-commit/compare/v0.14.3...v0.14.7)
- [github.com/pre-commit/mirrors-mypy: v1.18.2 → v1.19.0](https://github.com/pre-commit/mirrors-mypy/compare/v1.18.2...v1.19.0)
- [github.com/PyCQA/pylint: v4.0.2 → v4.0.4](https://github.com/PyCQA/pylint/compare/v4.0.2...v4.0.4)
- [github.com/python-jsonschema/check-jsonschema: 0.34.1 → 0.35.0](https://github.com/python-jsonschema/check-jsonschema/compare/0.34.1...0.35.0)

* style: pre-commit fixes

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
2025-12-03 01:34:04 -05:00
Ralf W. Grosse-Kunstleve
55e4bb9135 Work around GCC -Warray-bounds false positive in argument_vector (#5908) 2025-11-30 10:01:36 -08:00
Ralf W. Grosse-Kunstleve
734c29b25e Point main README.rst to CI for supported platforms and compilers (#5910)
* [skip ci] Point main README.rst to CI for supported platforms and compilers

- **Replace** the hard-coded “Supported compilers” and “Supported platforms” lists in `README.rst`.
- **Point** readers to the current GitHub Actions matrix as the source of truth for tested platforms, compilers, and Python/C++ versions.
- **Clarify** that the matrix evolves over time and that configurations users care about can be kept working via contributions.

- **Avoid stale documentation**: Enumerating specific compiler and platform versions in the README is both burdensome and error-prone, and tends to drift out of sync with reality.
- **Align “supported” with “tested”**: In practice, the CI configuration is the only place where we can say with confidence which combinations are exercised. Nearby versions (e.g., adjacent compiler minor releases) will often work, but we cannot test every variant.
- **Reflect actual maintenance capacity**: pybind11 is maintained by a small, volunteer-based community, so support is necessarily best-effort. Pointing to CI and inviting contributions better matches how support is provided in practice.

- **No behavior change**: This PR updates documentation only.
- **Living source of truth**: As CI jobs are added or removed, the linked Actions view will automatically reflect the set of configurations we actively test. Keeping a configuration in CI is the best way to keep it “supported”.

* [skip ci] Slight rewording: point out GitHub's limits on concurrent jobs under the free tier (rather than free minutes).
2025-11-29 13:16:39 -08:00
Ralf W. Grosse-Kunstleve
ab9ac90fcd Replace deprecated macos-13 runners with macos-15-intel (#5916) 2025-11-29 13:16:01 -08:00
Ralf W. Grosse-Kunstleve
28ecc9b6a0 Disable Android cibuildwheel tests only on ubuntu-latest (#5915)
* [skip ci] Re-enable Android cibuildwheel tests (refs #5913)

* Disable Android cibuildwheel tests only on ubuntu-latest (see #5913, #5914)

* [skip ci] Refer to PR 5914 instead of issue 5913
2025-11-29 12:00:15 -08:00
dependabot[bot]
1fa9fad6d1 chore(deps): bump the actions group with 5 updates (#5912)
* chore(deps): bump the actions group with 5 updates

Bumps the actions group with 5 updates:

| Package | From | To |
| --- | --- | --- |
| [actions/checkout](https://github.com/actions/checkout) | `1` | `6` |
| [astral-sh/setup-uv](https://github.com/astral-sh/setup-uv) | `6` | `7` |
| [actions/upload-artifact](https://github.com/actions/upload-artifact) | `4` | `5` |
| [actions/download-artifact](https://github.com/actions/download-artifact) | `5` | `6` |
| [pypa/cibuildwheel](https://github.com/pypa/cibuildwheel) | `3.1` | `3.3` |


Updates `actions/checkout` from 1 to 6
- [Release notes](https://github.com/actions/checkout/releases)
- [Changelog](https://github.com/actions/checkout/blob/main/CHANGELOG.md)
- [Commits](https://github.com/actions/checkout/compare/v1...v6)

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

Updates `actions/upload-artifact` from 4 to 5
- [Release notes](https://github.com/actions/upload-artifact/releases)
- [Commits](https://github.com/actions/upload-artifact/compare/v4...v5)

Updates `actions/download-artifact` from 5 to 6
- [Release notes](https://github.com/actions/download-artifact/releases)
- [Commits](https://github.com/actions/download-artifact/compare/v5...v6)

Updates `pypa/cibuildwheel` from 3.1 to 3.3
- [Release notes](https://github.com/pypa/cibuildwheel/releases)
- [Changelog](https://github.com/pypa/cibuildwheel/blob/main/docs/changelog.md)
- [Commits](https://github.com/pypa/cibuildwheel/compare/v3.1...v3.3)

---
updated-dependencies:
- dependency-name: actions/checkout
  dependency-version: '6'
  dependency-type: direct:production
  update-type: version-update:semver-major
  dependency-group: actions
- dependency-name: astral-sh/setup-uv
  dependency-version: '7'
  dependency-type: direct:production
  update-type: version-update:semver-major
  dependency-group: actions
- dependency-name: actions/upload-artifact
  dependency-version: '5'
  dependency-type: direct:production
  update-type: version-update:semver-major
  dependency-group: actions
- dependency-name: actions/download-artifact
  dependency-version: '6'
  dependency-type: direct:production
  update-type: version-update:semver-major
  dependency-group: actions
- dependency-name: pypa/cibuildwheel
  dependency-version: '3.3'
  dependency-type: direct:production
  update-type: version-update:semver-minor
  dependency-group: actions
...

Signed-off-by: dependabot[bot] <support@github.com>

* Revert to actions/checkout@v1 for Debian

* Temporarily disable Android cibuildwheel tests with warning (see #5913)

* Changes from quotes to double-quotes (attempt to resolve job failures).

* double-quotes in single-quotes

* single-quotes, remove hash in string

---------

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: Ralf W. Grosse-Kunstleve <rgrossekunst@nvidia.com>
2025-11-29 09:57:32 -08:00
gentlegiantJGC
665461d063 Remove enum from bold in doc (#5903)
* Remove enum from bold in doc

* [skip ci] Remove bold formatting around (see #5528)

---------

Co-authored-by: Ralf W. Grosse-Kunstleve <rgrossekunst@nvidia.com>
2025-11-28 22:15:01 -08:00
gentlegiantJGC
af796d0a99 Don't allow keep_alive or call_guard on properties (#5533)
* Don't allow keep_alive or call_guard on properties

The def_property family blindly ignore the keep_alive and call_guard arguments passed to them making them confusing to use.
This adds a static_assert if either is passed to make it clear it doesn't work.
I would prefer this to be a compiler warning but I can't find a way to do that. Is that even possible?

* style: pre-commit fixes

* Re-run tests

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
2025-11-15 08:53:15 -08:00
Michael Carlstrom
42cda7570e Fix *args/**kwargs return types. Add type hinting to py::make_tuple (#5881)
* Type hint make_tuple / fix *args/**kwargs return type

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

* add back commented out panic

* ignore return std move clang

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

* fix for mingmw

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

* added missing case

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

---------

Signed-off-by: Michael Carlstrom <rmc@carlstrom.com>
2025-11-13 21:03:53 -08:00
Rostan
8ecf10e8cc Fix crash in gil_scoped_acquire (#5828)
* Add a test reproducing the #5827 crash

Signed-off-by: Rostan Tabet <rtabet@nvidia.com>

* Fix #5827

Signed-off-by: Rostan Tabet <rtabet@nvidia.com>

* Rename PYBIND11_HAS_BARRIER and move it to common.h

Signed-off-by: Rostan Tabet <rtabet@nvidia.com>

* In test_thread.{cpp,py}, rename has_barrier

Signed-off-by: Rostan Tabet <rtabet@nvidia.com>

---------

Signed-off-by: Rostan Tabet <rtabet@nvidia.com>
2025-11-13 16:29:02 -08:00
Ralf W. Grosse-Kunstleve
b30e72c6f6 Replace env.deprecated_call() with pytest.deprecated_call() (#5893)
Since we already require pytest>=6 (see tests/requirements.txt), the
old compatibility function is obsolete and pytest.deprecated_call() can
be used directly.

Extracted from PR #5879

Co-authored-by: Michael Carlstrom <rmc@carlstrom.com>
Co-authored-by: gentlegiantJGC <gentlegiantJGC@users.noreply.github.com>
2025-11-11 19:27:53 -08:00
Rangsiman Ketkaew
3370fe14b7 Enhance: edit doc py::native_enum feature in upgrade.rst (#5885)
* Enhance: edit doc py::native_enum feature in upgrade.rst

Added information about the inclusion requirement for py::native_enum feature.

* [skip ci] Polish wording

---------

Co-authored-by: Ralf W. Grosse-Kunstleve <rgrossekunst@nvidia.com>
2025-11-10 20:28:23 -08:00
Henry Schreiner
1ccaad5b12 chore: log_level is better than log_cli_level (#5890) 2025-11-10 20:28:09 -08:00
Ralf W. Grosse-Kunstleve
9f1187f97c Add typing.SupportsIndex to int/float/complex type hints (#5891)
* Add typing.SupportsIndex to int/float/complex type hints

This corrects a mistake where these types were supported but the type
hint was not updated to reflect that SupportsIndex objects are accepted.

To track the resulting test failures:

The output of

"$(cat PYROOT)"/bin/python3 $HOME/clone/pybind11_scons/run_tests.py $HOME/forked/pybind11 -v

is in

~/logs/pybind11_pr5879_scons_run_tests_v_log_2025-11-10+122217.txt

* Cursor auto-fixes (partial) plus pre-commit cleanup. 7 test failures left to do.

* Fix remaining test failures, partially done by cursor, partially manually.

* Cursor-generated commit: Added the Index() tests from PR 5879.

Summary:

  Changes Made

  1. **C++ Bindings** (`tests/test_builtin_casters.cpp`)

  • Added complex_convert and complex_noconvert functions needed for the tests

  2. **Python Tests** (`tests/test_builtin_casters.py`)

  `test_float_convert`:
  • Added Index class with __index__ returning -7
  • Added Int class with __int__ returning -5
  • Added test showing Index() works with convert mode: assert pytest.approx(convert(Index())) == -7.0
  • Added test showing Index() doesn't work with noconvert mode: requires_conversion(Index())
  • Added additional assertions for int literals and Int() class

  `test_complex_cast`:
  • Expanded the test to include convert and noconvert functionality
  • Added Index, Complex, Float, and Int classes
  • Added test showing Index() works with convert mode: assert convert(Index()) == 1 and assert isinstance(convert(Index()), complex)
  • Added test showing Index() doesn't work with noconvert mode: requires_conversion(Index())
  • Added type hint assertions matching the SupportsIndex additions

  These tests demonstrate that custom __index__ objects work with float and complex in convert mode, matching the typing.SupportsIndex type hint added in PR
  5891.

* Reflect behavior changes going back from PR 5879 to master. This diff will have to be reapplied under PR 5879.

* Add PyPy-specific __index__ handling for complex caster

Extract PyPy-specific __index__ backporting from PR 5879 to fix PyPy 3.10
test failures in PR 5891. This adds:

1. PYBIND11_INDEX_CHECK macro in detail/common.h:
   - Uses PyIndex_Check on CPython
   - Uses hasattr check on PyPy (workaround for PyPy 7.3.3 behavior)

2. PyPy-specific __index__ handling in complex.h:
   - Handles __index__ objects on PyPy 7.3.7's 3.8 which doesn't
     implement PyLong_*'s __index__ calls
   - Mirrors the logic used in numeric_caster for ints and floats

This backports __index__ handling for PyPy, matching the approach
used in PR 5879's expand-float-strict branch.
2025-11-10 20:26:50 -08:00
pre-commit-ci[bot]
73da78c3e4 chore(deps): update pre-commit hooks (#5888)
updates:
- [github.com/astral-sh/ruff-pre-commit: v0.13.3 → v0.14.3](https://github.com/astral-sh/ruff-pre-commit/compare/v0.13.3...v0.14.3)
- [github.com/PyCQA/pylint: v3.3.9 → v4.0.2](https://github.com/PyCQA/pylint/compare/v3.3.9...v4.0.2)
- [github.com/python-jsonschema/check-jsonschema: 0.34.0 → 0.34.1](https://github.com/python-jsonschema/check-jsonschema/compare/0.34.0...0.34.1)

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
2025-11-10 16:52:13 -05:00
Joshua Oreman
e6984c805e native_enum: add capsule containing enum information and cleanup logic (#5871)
* native_enum: add capsule containing enum information and cleanup logic

* style: pre-commit fixes

* Updates from code review

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
2025-10-18 11:07:00 -06:00
daltairwalter
15943963b3 fix dangling thread state issue (#5870)
* fix dangling thread state issue

* formatting rules

* use tstate .set(nullptr) to pass clang-tidy check

* fix spelling mistake

* improve comments for maintainability
2025-10-16 08:40:50 -07:00
Scott Wolchok
1e5bc66e38 Factor out readable function signatures to avoid duplication (#5857)
* Centralize readable function signatures to avoid duplication

This seems to reduce size costs of adding enum_-specific implementations of dunder methods, but also should provide a nice to have size optimization for programs that use pybind11 in general.

* gate disabling of -Wdeprecated-redundant-constexpr-static-def to clang 17+

* fix gating to include Apple Clang 15

* Make GCC happy with types

* fix apple clang gating again. suppress -Wdeprecated for GCC

* Gate warning suppressions to C++17. Suppress -Wdeprecated for clang as well.

* hopefully fix last straggler CI job

* attempt to address readability review feedback from @rwgk

* drop warning suppressions and instead just gate compilation the pre-C++17 compat code
2025-10-15 21:12:44 -07:00
Joshua Oreman
cc36ac51a0 type_caster_generic: fix compiler error when casting a T that is implicitly convertible from T* (#5873)
* type_caster_generic: fix compiler error when casting a T that is implicitly convertible from T*

* style: pre-commit fixes

* Placate clang-tidy

* Expand NOLINT to specify Clang-Tidy check names

---------

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-10-15 09:10:50 -07:00
Joshua Oreman
a2c59711b2 type_caster_generic: add cast_sources abstraction (#5866)
* type_caster_generic: add cast_sources abstraction

* Respond to code review comments

* style: pre-commit fixes

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
2025-10-14 18:42:04 -06:00
Scott Wolchok
fc423c948a Fix dangling pointer in internals::registered_types_cpp_fast from #5842 (#5867)
* Fix dangling pointer in internals::registered_types_cpp_fast from #5842

@oremanj pointed out in a comment on #5842 that I missed part
of the nanobind PR I was porting in such a way that we could have
dangling pointers in internals::registered_types_cpp_fast. This PR
adds a test that reproed the bug and then fixes the test.

* review feedback, attempt to fix -Werror in CI

* use const ref, skip test on python 3.13 free-threaded

* Skip test on 3.13t more robustly

* style: pre-commit fixes

* CI fix

---------

Co-authored-by: Joshua Oreman <oremanj@gmail.com>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
2025-10-14 16:52:03 -06:00
Joshua Oreman
c7b4f66a73 type_caster_generic: add set_foreign_holder method for subclasses to implement (#5862)
* type_caster_generic: add set_foreign_holder method for subclasses to implement

* style: pre-commit fixes

* Rename try_shared_from_this -> set_via_shared_from_this to avoid confusion against try_get_shared_from_this

* Add comment explaining the limits of the test

* CI

* style: pre-commit fixes

* Fixes from code review

* style: pre-commit fixes

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
2025-10-13 18:00:28 -06:00
Sam Gross
9f75202191 Fix thread-safety in get_local_type_info() (#5856)
Fixes potential thread-safety issues if types are concurrently
registered while `get_local_type_info()` is called in free threaded
Python.

Use the `internals` mutex to also protect `local_internals`. This
keeps the locking strategy simpler, and we already follow this pattern
in some places, such as `pybind11_meta_dealloc`.
2025-10-12 14:37:48 -07:00
pre-commit-ci[bot]
aa4259b4f8 chore(deps): update pre-commit hooks (#5820)
* chore(deps): update pre-commit hooks

updates:
- [github.com/pre-commit/mirrors-clang-format: v20.1.8 → v21.1.2](https://github.com/pre-commit/mirrors-clang-format/compare/v20.1.8...v21.1.2)
- [github.com/astral-sh/ruff-pre-commit: v0.12.7 → v0.13.3](https://github.com/astral-sh/ruff-pre-commit/compare/v0.12.7...v0.13.3)
- [github.com/pre-commit/mirrors-mypy: v1.17.1 → v1.18.2](https://github.com/pre-commit/mirrors-mypy/compare/v1.17.1...v1.18.2)
- [github.com/pre-commit/pre-commit-hooks: v5.0.0 → v6.0.0](https://github.com/pre-commit/pre-commit-hooks/compare/v5.0.0...v6.0.0)
- [github.com/adamchainz/blacken-docs: 1.19.1 → 1.20.0](https://github.com/adamchainz/blacken-docs/compare/1.19.1...1.20.0)
- [github.com/shellcheck-py/shellcheck-py: v0.10.0.1 → v0.11.0.1](https://github.com/shellcheck-py/shellcheck-py/compare/v0.10.0.1...v0.11.0.1)
- [github.com/PyCQA/pylint: v3.3.7 → v3.3.9](https://github.com/PyCQA/pylint/compare/v3.3.7...v3.3.9)
- [github.com/python-jsonschema/check-jsonschema: 0.33.2 → 0.34.0](https://github.com/python-jsonschema/check-jsonschema/compare/0.33.2...0.34.0)

* style: pre-commit fixes

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
2025-10-11 10:23:50 -07:00
Joshua Oreman
1cf0948d34 Avoid a heap allocation on every legacy py::enum_ load (#5860) 2025-10-11 10:19:52 -07:00
Joshua Oreman
cae4ae083e docs: clarify to what extent bindings are actually global (#5859) 2025-10-11 10:09:14 -07:00
Scott Wolchok
3262000195 Add fast_type_map, use it authoritatively for local types and as a hint for global types (ABI breaking) (#5842)
* Add fast_type_map, use it authoritatively for local types and as a hint for global types

nanobind has a similar two-level lookup strategy, added and explained
by
b515b1f7f2

In this PR I've ported this approach to pybind11. To avoid an ABI
break, I've kept the fast maps to the `local_internals`. I think this
should be safe because any particular module should see its
`local_internals` reset at least as often as the global `internals`,
and misses in the fast "hint" map for global types fall back to the
global `internals`.

Performance seems to have improved. Using my patched fork of
pybind11_benchmark
(https://github.com/swolchok/pybind11_benchmark/tree/benchmark-updates,
specifically commit hash b6613d12607104d547b1c10a8145d1b3e9937266), I
run bench.py and observe the MyInt case. Each time, I do 3 runs and
just report all 3.

master, Mac: 75.9, 76.9, 75.3 nsec/loop
this PR, Mac: 73.8, 73.8, 73.6 nsec/loop
master, Linux box: 188, 187, 188 nsec/loop
this PR, Linux box: 164, 165, 164 nsec/loop

Note that the "real" percentage improvement is larger than implied by the
above because master does not yet include #5824.

* simplify unsafe_reset_local_internals in test

* pre-implement PYBIND11_INTERNALS_VERSION 12

* use PYBIND11_INTERNALS_VERSION 12 on Python 3.14 per suggestion

* Implement reviewer comments: revert PY_VERSION_HEX change, fix REVIEW comment, add two-level lookup comments. ci.yml coming separately

* Use the inplace build to smoke test ABI bump?

* [skip ci] Remove "smoke" from comment. This is full testing, just only on a few platforms.

---------

Co-authored-by: Ralf W. Grosse-Kunstleve <rgrossekunst@nvidia.com>
2025-10-05 11:07:25 -07:00
Sam Gross
9ea197627d Use new 3.14 C APIs when available (#5854)
* Use new 3.14 C APIs when available

Use the new "unstable" C APIs for the functions added in #5494.

* style: pre-commit fixes

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
2025-10-05 09:58:13 -07:00
Ralf W. Grosse-Kunstleve
4dc33d6524 Fix smart_holder multiple/virtual inheritance bugs in shared_ptr and unique_ptr to-Python conversions (#5836)
* ChatGPT-generated diamond virtual-inheritance test case.

* Report "virtual base at offset 0" but don't skip test.

* Remove Left/Right virtual default dtors, to resolve clang-tidy errors:

```
/__w/pybind11/pybind11/tests/test_class_sh_mi_thunks.cpp:44:13: error: prefer using 'override' or (rarely) 'final' instead of 'virtual' [modernize-use-override,-warnings-as-errors]
   44 |     virtual ~Left() = default;
      |     ~~~~~~~ ^
      |                     override
/__w/pybind11/pybind11/tests/test_class_sh_mi_thunks.cpp:48:13: error: prefer using 'override' or (rarely) 'final' instead of 'virtual' [modernize-use-override,-warnings-as-errors]
   48 |     virtual ~Right() = default;
      |     ~~~~~~~ ^
      |                      override
```

* Add assert(ptr) in register_instance_impl, deregister_instance_impl

* Proper bug fix

* Also exercise smart_holder_from_unique_ptr

* [skip ci] ChatGPT-generated bug fix: smart_holder::from_unique_ptr()

* Exception-safe ownership transfer from unique_ptr to shared_ptr

ChatGPT:

* shared_ptr’s ctor can throw (control-block alloc). Using get() keeps unique_ptr owning the memory if that happens, so no leak.

* Only after the shared_ptr is successfully constructed do you release(), transferring ownership exactly once.

* [skip ci] Rename alias_ptr to mi_subobject_ptr to distinguish from trampoline code (which often uses the term "alias", too)

* [skip ci] Also exercise smart_holder::from_raw_ptr_take_ownership

* [skip ci] Add st.first comments (generated by ChatGPT)

* [skip ci] Copy and extend (raw_ptr, unique_ptr) reproducer from PR #5796

* Some polishing: comments, add back Left/Right dtors for consistency within test_class_sh_mi_thunks.cpp

* explicitly default copy/move for VBase to silence -Wdeprecated-copy-with-dtor

* Resolve clang-tidy error:

```
/__w/pybind11/pybind11/tests/test_class_sh_mi_thunks.cpp:67:5: error: 'auto ptr' can be declared as 'auto *ptr' [readability-qualified-auto,-warnings-as-errors]
   67 |     auto ptr = new Diamond;
      |     ^~~~
      |     auto *
```

* Expand comment in `smart_holder::from_unique_ptr()`

* Better Left/Right padding to make it more likely that we avoid "all at offset 0". Clarify comment.

* Give up on `alignas(16)` to resolve MSVC warning:

```
       "D:\a\pybind11\pybind11\build\ALL_BUILD.vcxproj" (default target) (1) ->
       "D:\a\pybind11\pybind11\build\tests\pybind11_tests.vcxproj" (default target) (13) ->
       (ClCompile target) ->
         D:\a\pybind11\pybind11\tests\test_class_sh_mi_thunks.cpp(70,17): warning C4316: 'test_class_sh_mi_thunks::Diamond': object allocated on the heap may not be aligned 16 [D:\a\pybind11\pybind11\build\tests\pybind11_tests.vcxproj]
         D:\a\pybind11\pybind11\tests\test_class_sh_mi_thunks.cpp(80,43): warning C4316: 'test_class_sh_mi_thunks::Diamond': object allocated on the heap may not be aligned 16 [D:\a\pybind11\pybind11\build\tests\pybind11_tests.vcxproj]
         C:\Program Files\Microsoft Visual Studio\2022\Enterprise\VC\Tools\MSVC\14.44.35207\include\memory(2913,46): warning C4316: 'std::_Ref_count_obj2<_Ty>': object allocated on the heap may not be aligned 16 [D:\a\pybind11\pybind11\build\tests\pybind11_tests.vcxproj]
       C:\Program Files\Microsoft Visual Studio\2022\Enterprise\VC\Tools\MSVC\14.44.35207\include\memory(2913,46): warning C4316:         with [D:\a\pybind11\pybind11\build\tests\pybind11_tests.vcxproj]
       C:\Program Files\Microsoft Visual Studio\2022\Enterprise\VC\Tools\MSVC\14.44.35207\include\memory(2913,46): warning C4316:         [ [D:\a\pybind11\pybind11\build\tests\pybind11_tests.vcxproj]
       C:\Program Files\Microsoft Visual Studio\2022\Enterprise\VC\Tools\MSVC\14.44.35207\include\memory(2913,46): warning C4316:             _Ty=test_class_sh_mi_thunks::Diamond [D:\a\pybind11\pybind11\build\tests\pybind11_tests.vcxproj]
       C:\Program Files\Microsoft Visual Studio\2022\Enterprise\VC\Tools\MSVC\14.44.35207\include\memory(2913,46): warning C4316:         ] [D:\a\pybind11\pybind11\build\tests\pybind11_tests.vcxproj]
         D:\a\pybind11\pybind11\include\pybind11\detail\init.h(77,21): warning C4316: 'test_class_sh_mi_thunks::Diamond': object allocated on the heap may not be aligned 16 [D:\a\pybind11\pybind11\build\tests\pybind11_tests.vcxproj]
```

The warning came from alignas(16) making Diamond over-aligned, while regular new/make_shared aren’t guaranteed to return 16-byte aligned memory on MSVC (hence C4316). I’ve removed the explicit alignment and switched to asymmetric payload sizes (char[4] vs char[24]), which still nudges MI layout without relying on over-alignment. This keeps the test goal and eliminates the warning across all MSVC builds. If we ever want to stress over-alignment explicitly, we can add aligned operator new/delete under __cpp_aligned_new, but that’s more than we need here.

* Rename test_virtual_base_at_offset_0() → test_virtual_base_not_at_offset_0() and replace pytest.skip() with assert. Add helpful comment for future maintainers.
2025-10-01 11:21:47 -07:00
Ralf W. Grosse-Kunstleve
0161da9d6d [skip ci] .gitignore: exclude __pycache__ directories (#5838)
Python may leave behind temporary `.pyc.*` files inside `__pycache__`
on some filesystems (e.g. WSL2 mounts). Adding `__pycache__/` ensures
these directories and any leftover files are consistently ignored.

Background: Python writes bytecode to a temp file with an extra suffix
before renaming it to `.pyc`. If the process is interrupted or the
filesystem rename isn’t fully atomic, those temp files may remain.

See: https://docs.python.org/3/library/py_compile.html#py_compile.compile
2025-09-27 13:12:56 -07:00
gentlegiantJGC
81ffb1d5cc Add 90 minute limit for tests (#5851)
Occasionally a test will get stuck and run for 6 hours until Github cancels the workflow.
This reduces the timeout to 90 minutes to not waste resources.
Pybind11's tests seem to run in 30 minutes so this should be plenty of time.
2025-09-27 12:53:47 -07:00
gentlegiantJGC
8ed0dab67f Add float type caster and revert type hint changes to int_ and float_ (#5839)
* Revert type hint changes to int_ and float_

These two types do not support casting from int-like and float-like types.

* Fix tests

* Add a custom py::float_ caster

The default py::object caster only works if the object is an instance of the type.
py::float_ should accept python int objects as well as float.
This caster will pass through float as usual and cast int to float.
The caster handles the type name so the custom one is not required.

* style: pre-commit fixes

* Fix name

* Fix variable

* Try satisfying the formatter

* Rename test function

* Simplify type caster

* Fix reference counting issue

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
2025-09-27 09:13:21 -07:00
Scott Wolchok
30748f863f Avoid heap allocation for function calls with a small number of args (#5824)
* Avoid heap allocation for function calls with a small number of arguments

We don't have access to llvm::SmallVector or similar, but given the
limited subset of the `std::vector` API that
`function_call::args{,_convert}` need and the "reserve-then-fill"
usage pattern, it is relatively straightforward to implement custom
containers that get the job done.

Seems to improves time to call the collatz function in
pybind/pybind11_benchmark significantly; numbers are a little noisy
but there's a clear improvement from "about 60 ns per call" to "about
45 ns per call" on my machine (M4 Max Mac), as measured with
`timeit.repeat('collatz(4)', 'from pybind11_benchmark import
collatz')`.

* clang-tidy

* more clang-tidy

* clang-tidy NOLINTBEGIN/END instead of NOLINTNEXTLINE

* forgot to increase inline size after removing std::variant

* constexpr arg_vector_small_size, use move instead of swap to hopefully clarify second_pass_convert

* rename test_embed to test_low_level

* rename test_low_level to test_with_catch

* Be careful to NOINLINE slow paths

* rename array/vector members to iarray/hvector. Move comment per request. Add static_asserts for our untagged union implementation per request.

* drop is_standard_layout assertions; see https://github.com/pybind/pybind11/pull/5824#issuecomment-3308616072
2025-09-19 13:44:40 -07:00
b-pass
326b10637a Use thread_local instead of thread_specific_storage for internals (#5834)
* Use thread_local instead of thread_specific_storage for internals mangement

thread_local is faster.

* Make the pp manager a singleton.

Strictly speaking, since the members are static, the instances must also be singletons or this wouldn't work.  They already are, but we can make the class enforce it to be more 'self-documenting'.
2025-09-14 09:07:08 -07:00
Ralf W. Grosse-Kunstleve
d4d555d9e0 Restore runs-on: windows-latest (#5835)
* Revert "s/windows-2022/windows-latest/ in .github/workflows/{ci,pip}.yml (#5826)"

This reverts commit 852a4b5010.

* Add module-level skip for Windows build >= 26100 in test_iostream.py

* Changes suggested by at-henryiii
2025-09-12 21:52:44 -07:00
Scott Wolchok
937552f0ad Use thread_local for loader_life_support to improve performance (#5830)
* Use thread_local for loader_life_support to improve performance

As explained in a new code comment, `loader_life_support` needs to be
`thread_local` but does not need to be isolated to a particular
interpreter because any given function call is already going to only
happen on a single interpreter by definiton.

Performance before:
- on M4 Max using pybind/pybind11_benchmark unmodified repo:
```
> python -m timeit --setup 'from pybind11_benchmark import collatz' 'collatz(4)'
5000000 loops, best of 5: 63.8 nsec per loop
```

- Linux server:
```
python -m timeit --setup 'from pybind11_benchmark import collatz' 'collatz(4)'                                                                                                                                        (pytorch)
2000000 loops, best of 5: 120 nsec per loop
```

After:
- M4 Max:
```
python -m timeit --setup 'from pybind11_benchmark import collatz' 'collatz(4)'
5000000 loops, best of 5: 53.1 nsec per loop
```

- Linux server:
```
> python -m timeit --setup 'from pybind11_benchmark import collatz' 'collatz(4)'                                                                                                                                        (pytorch)
2000000 loops, best of 5: 101 nsec per loop
```

A quick profile with perf shows that pthread_setspecific and pthread_getspecific are gone.

Open questions:

- How do we determine whether we can safely use `thread_local`? I see
  concerns about old iOS versions on
  https://github.com/pybind/pybind11/pull/5705#issuecomment-2922858880
  and https://github.com/pybind/pybind11/pull/5709; is there anything
  else?
- Do we have a test that covers "function called in one interpreter
  calls a C++ function that causes a function call in another
  interpreter"? I think it's fine, but can it happen?
- Are we happy with what we think will happen in the case where
  multiple extensions compiled with and without this PR interoperate?
  I think it's fine -- each dispatch pushes and cleans up its own
  state -- but a second opinion is certainly welcome.

* Remove PYBIND11_CAN_USE_THREAD_LOCAL

* clarify comment

* Simplify loader_life_support TLS storage

Replace the `fake_thread_specific_storage` struct with a direct
thread-local pointer managed via a function-local static:

    static loader_life_support *& tls_current_frame()

This retains the "stack of frames" behavior via the `parent` link. It also
reduces indirection and clarifies intent.

Note: this form is C++11-compatible; once pybind11 requires C++17, the
helper can be simplified to:

    inline static thread_local loader_life_support *tls_current_frame = nullptr;

* loader_life_support: avoid duplicate tls_current_frame() calls

Replace repeated calls with a single local reference:

    auto &frame = tls_current_frame();

This ensures the thread_local initialization guard is checked only once
per constructor/destructor call site, avoids potential clang-tidy
complaints, and makes the code more readable. Functional behavior is
unchanged.

* Add REMINDER for next version bump in internals.h

---------

Co-authored-by: Ralf W. Grosse-Kunstleve <rgrossekunst@nvidia.com>
2025-09-12 14:37:01 -07:00
Joshua Oreman
68cbae6641 tests: add or delete copy/move ctors where needed to make type traits match reality (#5833) 2025-09-08 15:47:24 -07:00
Thomas Köppe
a6581eee89 pytypes.h: constrain accessor::operator= templates so that they do not obscure special members (#5832)
* pytypes.h: constrain accessor::operator= templates so that they do not match calls that should use the special member functions.

Found by an experimental, new clang-tidy check. While we may not know the exact design decisions now, it seems unlikely that the special members were deliberately meant to not be selected (for otherwise they could have been defined differently to make this clear). Rather, it seems like an oversight that the operator templates win in overload resolution, and we should restore the intended resolution.

* Use C++11-compatible facilities

* Use C++11-compatible facilities

* style: pre-commit fixes

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
2025-09-08 11:52:41 -07:00
dependabot[bot]
7fb54e3065 chore(deps): bump the actions group with 3 updates (#5831)
* chore(deps): bump the actions group with 3 updates

Bumps the actions group with 3 updates: [actions/checkout](https://github.com/actions/checkout), [actions/setup-python](https://github.com/actions/setup-python) and [actions/labeler](https://github.com/actions/labeler).


Updates `actions/checkout` from 1 to 5
- [Release notes](https://github.com/actions/checkout/releases)
- [Changelog](https://github.com/actions/checkout/blob/main/CHANGELOG.md)
- [Commits](https://github.com/actions/checkout/compare/v1...v5)

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

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

---
updated-dependencies:
- dependency-name: actions/checkout
  dependency-version: '5'
  dependency-type: direct:production
  update-type: version-update:semver-major
  dependency-group: actions
- dependency-name: actions/setup-python
  dependency-version: '6'
  dependency-type: direct:production
  update-type: version-update:semver-major
  dependency-group: actions
- dependency-name: actions/labeler
  dependency-version: '6'
  dependency-type: direct:production
  update-type: version-update:semver-major
  dependency-group: actions
...

Signed-off-by: dependabot[bot] <support@github.com>

* Manually reset "🐍 3.9 • Debian • x86 •  Install" back to `actions/checkout@v1`

---------

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: Ralf W. Grosse-Kunstleve <rgrossekunst@nvidia.com>
2025-09-07 20:22:29 -07:00
Scott Wolchok
852a4b5010 s/windows-2022/windows-latest/ in .github/workflows/{ci,pip}.yml (#5826)
Per request from @rwgk: https://github.com/pybind/pybind11/pull/5825#issuecomment-3256438901
2025-09-04 23:39:33 -04:00
dependabot[bot]
ef0f1ff5f1 chore(deps): bump the actions group across 1 directory with 2 updates (#5818)
* chore(deps): bump the actions group across 1 directory with 2 updates

Bumps the actions group with 2 updates in the / directory: [actions/checkout](https://github.com/actions/checkout) and [actions/attest-build-provenance](https://github.com/actions/attest-build-provenance).


Updates `actions/checkout` from 1 to 5
- [Release notes](https://github.com/actions/checkout/releases)
- [Changelog](https://github.com/actions/checkout/blob/main/CHANGELOG.md)
- [Commits](https://github.com/actions/checkout/compare/v1...v5)

Updates `actions/attest-build-provenance` from 2 to 3
- [Release notes](https://github.com/actions/attest-build-provenance/releases)
- [Changelog](https://github.com/actions/attest-build-provenance/blob/main/RELEASE.md)
- [Commits](https://github.com/actions/attest-build-provenance/compare/v2...v3)

---
updated-dependencies:
- dependency-name: actions/checkout
  dependency-version: '5'
  dependency-type: direct:production
  update-type: version-update:semver-major
  dependency-group: actions
- dependency-name: actions/attest-build-provenance
  dependency-version: '3'
  dependency-type: direct:production
  update-type: version-update:semver-major
  dependency-group: actions
...

Signed-off-by: dependabot[bot] <support@github.com>

* Update .github/workflows/ci.yml

---------

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: Henry Schreiner <HenrySchreinerIII@gmail.com>
2025-09-03 18:25:27 -07:00
Plamen Totev
bf2d56e8ac Fix the first example in the first steps guide not compiling (#5823)
The alias of the pybind11 namespace is missing
2025-09-03 09:07:22 -07:00
Ralf W. Grosse-Kunstleve
cd56888c89 Bring CI back to all-working condition (#5822)
* Fix "🐍 3 • windows-latest • mingw64" job (apparently msys2/setup-msys2@v2 cannot be run twice anymore):

https://github.com/pybind/pybind11/actions/runs/17394902023/job/49417376616?pr=5796

```
Run msys2/setup-msys2@v2
  with:
    msystem: mingw64
    install: mingw-w64-x86_64-python-numpy mingw-w64-x86_64-python-scipy mingw-w64-x86_64-eigen3
    path-type: minimal
    update: false
    pacboy: false
    release: true
    location: RUNNER_TEMP
    platform-check-severity: fatal
    cache: true
  env:
    PYTHONDEVMODE: 1
    PIP_BREAK_SYSTEM_PACKAGES: 1
    PIP_ONLY_BINARY: numpy
    FORCE_COLOR: 3
    PYTEST_TIMEOUT: 300
    VERBOSE: 1
    CMAKE_COLOR_DIAGNOSTICS: 1
    MSYSTEM: MINGW64
Error: Trying to install MSYS2 to D:\a\_temp\msys64 but that already exists, cannot continue.
```

* Add `pytest.xfail("[TEST-GIL-SCOPED] macOS free-threading...)`

* Change env.SYS_IS_GIL_ENABLED constant to env.sys_is_gil_enabled function

* Change install_mingw64_only → extra_install

* Also xfail if macOS and PY_GIL_DISABLED, show SOABI

* build-ios: brew upgrade|install cmake

* Revert "build-ios: brew upgrade|install cmake"

This reverts commit bd3900ee79.

See also:

https://github.com/pybind/pybind11/pull/5822#issuecomment-3247827317

* Disable build-ios job in tests-cibw.yml

* Remove macos_brew_install_llvm job because it started failing, to reduce our maintenance overhead:

Failures tracked here: https://github.com/pybind/pybind11/pull/5822#issuecomment-3247998220

* Fix iOS build step for cmake installation

Replaced brew upgrade with brew install for cmake.

* Update cmake installation steps in CI workflow

Uninstall cmake before installing the latest version due to GitHub's local tap changes.

* Update .github/workflows/tests-cibw.yml

---------

Co-authored-by: Henry Schreiner <HenrySchreinerIII@gmail.com>
2025-09-03 09:06:41 -07:00
MoonE
3878c23f8d Fix typo in error message (#5817) 2025-08-30 23:07:03 -07:00
Tobias Leibner
3c0ee89716 Fix compiler detection with clang-cl (#5816)
* Fix compiler detection with clang-cl

* Follow review suggestion
2025-08-27 11:06:29 -07:00
Henry Schreiner
6e0d1c2400 chore: back to work
Signed-off-by: Henry Schreiner <henryschreineriii@gmail.com>
2025-08-22 16:08:51 -04:00
Henry Schreiner
f5fbe867d2 chore: bump to 3.0.1 (#5810)
* docs: prepare for 3.0.1

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

* chore: bump for 3.0.1

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

* Update docs/changelog.md

---------

Signed-off-by: Henry Schreiner <henryschreineriii@gmail.com>
v3.0.1
2025-08-22 15:57:09 -04:00