* Change PYBIND11_MODULE to use multi-phase init
Use slots to specify advanced init options (currently just Py_GIL_NOT_USED).
Adds a new function `initialize_multiphase_module_def` to module_, similar to the existing (unchanged) create_extension_module.
* Avoid dynamic allocation and non-trivial destruction
... by using an std::array for the slots.
* Oops, stray cut and paste character
* Remove assignment from placement new, change size fo slots array
* style: pre-commit fixes
---------
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
* test passes on PyPy macOS
* revert, set strict=False instead
* Tweak condition for strict
Co-authored-by: Henry Schreiner <HenrySchreinerIII@gmail.com>
---------
Co-authored-by: Henry Schreiner <HenrySchreinerIII@gmail.com>
* init
Signed-off-by: Michael Carlstrom <rmc@carlstrom.com>
* remove import
Signed-off-by: Michael Carlstrom <rmc@carlstrom.com>
* remove uneeded function
Signed-off-by: Michael Carlstrom <rmc@carlstrom.com>
* style: pre-commit fixes
* Add missing import
Signed-off-by: Michael Carlstrom <rmc@carlstrom.com>
* style: pre-commit fixes
* Fix type behind detailed_message_enabled flag
Signed-off-by: Michael Carlstrom <rmc@carlstrom.com>
* Fix type behind detailed_message_enabled flag
Signed-off-by: Michael Carlstrom <rmc@carlstrom.com>
* Add io_name comment
Signed-off-by: Michael Carlstrom <rmc@carlstrom.com>
* Extra loops to single function
Signed-off-by: Michael Carlstrom <rmc@carlstrom.com>
* style: pre-commit fixes
* Remove unneeded forward declaration
Signed-off-by: Michael Carlstrom <rmc@carlstrom.com>
* Switch variable name away from macro
Signed-off-by: Michael Carlstrom <rmc@carlstrom.com>
* Switch variable name away from macro
Signed-off-by: Michael Carlstrom <rmc@carlstrom.com>
* Switch variable name away from macro
Signed-off-by: Michael Carlstrom <rmc@carlstrom.com>
* clang-tidy
Signed-off-by: Michael Carlstrom <rmc@carlstrom.com>
* remove stack import
* Fix bug in std::function Callable type
Signed-off-by: Michael Carlstrom <rmc@carlstrom.com>
* style: pre-commit fixes
* remove is_annotation argument
Signed-off-by: Michael Carlstrom <rmc@carlstrom.com>
* style: pre-commit fixes
* Update function name and arg names
Signed-off-by: Michael Carlstrom <rmc@carlstrom.com>
* style: pre-commit fixes
---------
Signed-off-by: Michael Carlstrom <rmc@carlstrom.com>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
When run on a platform where `char` is unsigned by default — which is
implementation-defined.
This explicitly uses `signed char` in two tests where the sign of the
char is important (though only `test_dtype_normalized_num` fails without
this change).
* Pure `git merge --squash smart_holder` (no manual interventions).
* Remove ubench/ directory.
* Remove include/pybind11/smart_holder.h
* [ci skip] smart_ptrs.rst updates [WIP/unfinished]
* [ci skip] smart_ptrs.rst updates continued; also updating classes.rst, advanced/classes.rst
* Remove README_smart_holder.rst
* Restore original README.rst from master
* [ci skip] Minimal change to README.rst, to leave a hint that this is pybind11v3
* [ci skip] Work in ChatGPT suggestions.
* Change macro name to PYBIND11_RUN_TESTING_WITH_SMART_HOLDER_AS_DEFAULT_BUT_NEVER_USE_IN_PRODUCTION_PLEASE
* Add a note pointing to the holder reinterpret_cast.
* Incorporate suggestion by @virtuald: https://github.com/pybind/pybind11/pull/5542#discussion_r1967000989
* Systematically change most py::class_ to py::classh under docs/
* Remove references to README_smart_holder.rst
This should have been part of commit eb550d03d3.
* [ci skip] Fix minor oversight (``class_`` -> ``py::class_``) noticed by chance.
* [ci skip] Resolve suggestion by @virtuald
https://github.com/pybind/pybind11/pull/5542#discussion_r1969940605
* [ci skip] Apply suggestions by @timohl (thanks!)
* https://github.com/pybind/pybind11/pull/5542#discussion_r1970714551
* https://github.com/pybind/pybind11/pull/5542#discussion_r1971315329
* https://github.com/pybind/pybind11/pull/5542#discussion_r1971322821
* Replace `classh : class_` inhertance with `using`, as suggested by @henryiii
https://github.com/pybind/pybind11/pull/5542#issuecomment-2689034104
* Revert "Systematically change most py::class_ to py::classh under docs/"
This reverts commit ac9d31e13f.
* docs: focus on py::smart_holder instead of py::classh
Signed-off-by: Henry Schreiner <henryschreineriii@gmail.com>
* Restore minor general fixes that got lost when ac9d31e13f was reverted.
* Remove `- smart_holder` from list of branches in all .github/workflows
* Extend classh note to explain whitespace noise motivation.
* Suggest `py::smart_holder` for "most situations for safety"
* Add back PYBIND11_HAS_INTERNALS_WITH_SMART_HOLDER_SUPPORT
This define was
* introduced with https://github.com/pybind/pybind11/pull/5286
* removed with https://github.com/pybind/pybind11/pull/5531
It is has been in use here:
* f02a2b7653/pybind11_protobuf/native_proto_caster.h (L89-L101)
Currently pybind11 unit tests for the two holder caster backwards compatibility traits
* `copyable_holder_caster_shared_ptr_with_smart_holder_support_enabled`
* `move_only_holder_caster_unique_ptr_with_smart_holder_support_enabled`
are missing.
* Add py::trampoline_self_life_support to all trampoline examples under docs/.
Address suggestion by @timohl:
* https://github.com/pybind/pybind11/pull/5542#issuecomment-2686452062
Add to the "please think twice" note: the overhead for safety is likely in the noise.
Also fix a two-fold inconsistency introduced by revert-commit 1e646c91b4:
1.
py::trampoline_self_life_support is mentioned in a note, but is missing in the example right before.
2.
The section starting with
To enable safely passing a ``std::unique_ptr`` to a trampoline object between
is obsolete.
* Fix whitespace accident (indentation) introduced with 1e646c91b4
Apparently the mis-indentation was introduced when resolving merge conflicts for what became 1e646c91b4
* WHITESPACE CHANGES ONLY in README.rst (list of people that made significant contributions)
* Add Ethan Steinberg to list of people that made significant contributions (for completeness, unrelated to smart_holder work).
* [ci skip] Add to list of people that made significant contributions: major and/or influential contributors to smart_holder branch
* #2904 by @rhaschke was merged on Mar 16, 2021
* #3012 by @rhaschke was merged on May 28, 2021
* #3039 by @jakobandersen was merged on Jun 29, 2021
* #3048 by @Skylion007 was merged on Jun 18, 2021
* #3588 by @virtuald was merged on Jan 3, 2022
* #3633 by @wangxf123456 was merged on Jan 25, 2022
* #3635 by @virtuald was merged on Jan 26, 2022
* #3645 by @wangxf123456 was merged on Jan 25, 2022
* #3796 by @wangxf123456 was merged on Mar 10, 2022
* #3807 by @wangxf123456 was merged on Mar 18, 2022
* #3838 by @wangxf123456 was merged on Apr 15, 2022
* #3929 by @tomba was merged on May 7, 2022
* #4031 by @wangxf123456 was merged on Jun 27, 2022
* #4343 by @wangxf123456 was merged on Nov 18, 2022
* #4381 by @wangxf123456 was merged on Dec 5, 2022
* #4539 by @wangxf123456 was merged on Feb 28, 2023
* #4609 by @wangxf123456 was merged on Apr 6, 2023
* #4775 by @wangxf123456 was merged on Aug 3, 2023
* #4921 by @iwanders was merged on Nov 7, 2023
* #4924 by @iwanders was merged on Nov 6, 2023
* #5401 by @msimacek was merged on Oct 8, 2024
Co-authored-by: Aaron Gokaslan <aaronGokaslan@gmail.com>
Co-authored-by: Dustin Spicuzza <dustin@virtualroadside.com>
Co-authored-by: Ivor Wanders <iwanders@users.noreply.github.com>
Co-authored-by: Jakob Lykke Andersen <Jakob@caput.dk>
Co-authored-by: Michael Šimáček <michael.simacek@oracle.com>
Co-authored-by: Robert Haschke <rhaschke@users.noreply.github.com>
Co-authored-by: Tomi Valkeinen <tomi.valkeinen@iki.fi>
Co-authored-by: Xiaofei Wang <6218006+wangxf123456@users.noreply.github.com>
---------
Signed-off-by: Henry Schreiner <henryschreineriii@gmail.com>
Co-authored-by: Henry Schreiner <henryschreineriii@gmail.com>
Co-authored-by: Aaron Gokaslan <aaronGokaslan@gmail.com>
Co-authored-by: Dustin Spicuzza <dustin@virtualroadside.com>
Co-authored-by: Ivor Wanders <iwanders@users.noreply.github.com>
Co-authored-by: Jakob Lykke Andersen <Jakob@caput.dk>
Co-authored-by: Michael Šimáček <michael.simacek@oracle.com>
Co-authored-by: Robert Haschke <rhaschke@users.noreply.github.com>
Co-authored-by: Tomi Valkeinen <tomi.valkeinen@iki.fi>
Co-authored-by: Xiaofei Wang <6218006+wangxf123456@users.noreply.github.com>
* Sync `Py_TPFLAGS_MANAGED_DICT` for PyPy3.11 across the codebase
Adjust the `Py_TPFLAGS_MANAGED_DICT` logic in `include/pybind11/attr.h`
to match the one used in `include/pybind11/detail/class.h`.
This is a followup to #5508.
* Use a common `#define` for pre-`Py_TPFLAGS_MANAGED_DICT` Pythons
* style: pre-commit fixes
---------
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
* Improved reference_internal documentation
Improved the wording to make it easier to understand and added a note about the policy falling back to move if the return value is not an lvalue reference or pointer.
* Added bold and ticket number
* Start pybind11v3
* Remove all code for PYBIND11_INTERNALS_VERSION 4 and 5
* Fix oversight: pybind11/_version.py needs to be updated as well.
* Add @pytest.mark.skipif("env.GRAALPY", reason="TODO debug segfault")
* Backport of https://github.com/google/pybind11clif/pull/30088 (main PR) and https://github.com/google/pybind11clif/pull/30092 (minor fixes).
Note for completeness:
These are identical to the current versions on the pybind11clif main branch (@ commit 4841661df5daf26ecdedaace54e64d0782e63f64):
* test_class_release_gil_before_calling_cpp_dtor.cpp
* test_class_release_gil_before_calling_cpp_dtor.py
* Fix potential data race in test_class_release_gil_before_calling_cpp_dtor.cpp
The original intent was to let the singleton leak, but making that tread-safe is slightly more involved than this solution. It's totally fine in this case if the RegistryType destructor runs on process teardown.
See also: https://github.com/pybind/pybind11/pull/5522#issuecomment-2661068351
---------
Co-authored-by: Ralf W. Grosse-Kunstleve <rwgk@google.com>
* Remove some maybe-uninitialized warnings
In the Eigen matrix type_caster, resize the matrix instead of assigning with a new one when the matrix size needs to be adjusted.
This can remove lots of compiling warnings about "maybe-uninitialized".
* Revert "Remove some maybe-uninitialized warnings"
This reverts commit 7d5a9b41aa.
* Suppress `-Wmaybe-uninitialized` warning
Reproducer: https://github.com/pybind/pybind11/pull/5516#issuecomment-2645846295
---------
Co-authored-by: Ralf W. Grosse-Kunstleve <rgrossekunst@nvidia.com>
* fix docs build command on different global and venv python versions
* feat: add --extension-suffix
Signed-off-by: Henry Schreiner <henryschreineriii@gmail.com>
---------
Signed-off-by: Henry Schreiner <henryschreineriii@gmail.com>
Co-authored-by: Nikul Patel <nikul@z830>
Co-authored-by: Henry Schreiner <henryschreineriii@gmail.com>
* Added rework of arg/return typing
* Changed `Path` to `pathlib.Path` for compatibility with pybind11-stubgen
* Removed old arg/return type hint implementation
* Added noconvert support for arg/return type hints
* Added commented failing tests for Literals with special characters
* Added return_descr/arg_descr for correct typing in typing::Callable
* Fixed clang-tidy issues
* Changed io_name to have explicit return type (for C++11 support)
* style: pre-commit fixes
* Added support for nested callables
* Fixed missing include
* Fixed is_return_value constructor call
* Fixed clang-tidy issue
* Uncommented test cases for special characters in literals
* Moved literal tests to correct test case
* Added escaping of special characters in typing::Literal
* Readded mistakenly deleted bracket
* Moved sanitize_string_literal to correct namespace
* Added test for Literal with `!` and changed StringLiteral template param name
* Added test for Literal with multiple and repeated special chars
* Simplified string literal sanitization function
* Added test for `->` in literal
* Added test for `->` with io_name
* Removed unused parameter name to prevent warning
* Added escaping of `-` in literal to prevent processing of `->`
* Fixed wrong computation of sanitized string literal length
* Added cast to prevent error with MSVC
* Simplified special character check
---------
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
* Fix data race when using shared variables (free threading)
In the free threading build, there's a race between wrapper re-use and
wrapper deallocation. This can happen with a static variable accessed by
multiple threads.
Fixing this requires using some private CPython APIs: _Py_TryIncref and
_PyObject_SetMaybeWeakref. The implementations of these functions are
included until they're made available as public ("unstable") APIs.
Fixes#5489
* style: pre-commit fixes
* Avoid unused parameter
* Add missing return for default build
* Changes from review
* Assign result to local variable
* s/clang-tidy/ruff
* clang-tidy: static is redundant
* Use 'noqa: B018'
---------
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
* Fix module type hint
"module" is not a valid python value.
The correct type hint for a module object is "types.ModuleType" which has existed since at least Python 2.6
* Added module type hint test
* style: pre-commit fixes
* Remove doc function
* Fixed type hint
---------
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
* THIS IS JUST A START: First attempt to combine information from PR #4953 and PR #5437
* Include GXX_ABI and USE_CXX in the identifier
Further constrain to GXX_ABI 1002 or greater and less than 2000,
hopefully future proof by summarizing that as `1` along with CXX11 on or
off.
* style: pre-commit fixes
* Use `gxx_abi_1xxx` and simplify the Clang string
After discussions with Ralf Grosse-Kunstleve we think these would make
good identifiers that are concise and clear.
* Error if `_GLIBCXX_USE_CXX11_ABI` is not defined
Within the `__GXX_ABI_VERSION` block this should always be defined,
guard against unexpected defines and make the error obvious.
* Change `usecxx11` to `use_cxx11_abi` for correspondence with `_GLIBCXX_USE_CXX11_ABI` (similarly to `gxx_abi` for `__GXX_ABI_VERSION`).
* `PYBIND11_COMPILER_TYPE` overhaul, mainly: replace `_icc`, `_clang`, `_gcc` with `_system`
* Add NVHPC (__PGI) to the list of compilers compatible with system compiler.
See comment by @robertmaynard: https://github.com/pybind/pybind11/pull/5439#issuecomment-2498839010
* Fix oversight: remove __NVCOMPILER elif branch in PYBIND11_BUILD_ABI block.
Also add comment pointing to this PR (#5439).
* Revert "Fix oversight: remove __NVCOMPILER elif branch in PYBIND11_BUILD_ABI block."
This reverts commit d412303e72.
* Revert "Add NVHPC (__PGI) to the list of compilers compatible with system compiler."
This reverts commit 9fc9515885.
* Define NVHPC PYBIND11_BUILD_ABI using __GNUC__, __GNUC_MINOR__, _GLIBCXX_USE_CXX11_ABI
* Use _GLIBCXX_USE_CXX11_ABI to detect libstdc++, then assume that NVHPC is always in the 1xxx ABI family.
* Enhance NVHPC comment and limited future proofing.
* The `PYBIND11_STDLIB` is obsolete but kept around to maintain backward compatibility.
* Move `PYBIND11_BUILD_TYPE` down in the file, so that the order of macro definitions is the same as in the list defining `PYBIND11_PLATFORM_ABI_ID`
* Introduce `PYBIND11_COMPILER_TYPE_LEADING_UNDERSCORE`:
This makes it possible to achieve these two goals:
* Avoid the leading underscore in `PYBIND11_PLATFORM_ABI_ID` (see https://github.com/pybind/pybind11/pull/5439#issuecomment-2503762161)
* Maintain backward compatibility for use cases as reported under https://github.com/pybind/pybind11/pull/5439#issuecomment-2510212677
`PYBIND11_INTERNALS_KIND` is removed in this commit to ensure that `PYBIND11_COMPILER_TYPE` is the first element of the `PYBIND11_PLATFORM_ABI_ID`, so that `PYBIND11_COMPILER_TYPE_LEADING_UNDERSCORE` can meaningfully be used as a prefix for `PYBIND11_PLATFORM_ABI_ID` in pybind11/detail/internals.h.
* Apply suggestion by @isuruf, with revised comments (code is as suggested).
* Make determination of `PYBIND11_COMPILER_TYPE` `"macos"` or `"glibc"` more general.
The main motivation is to resolve these "Manylinux on 🐍 3.13t • GIL" and "Pyodide wheel" failures:
```
/__w/pybind11/pybind11/include/pybind11/conduit/pybind11_platform_abi_id.h:35:10: error: #error "Unknown PYBIND11_COMPILER_TYPE: PLEASE REVISE THIS CODE."
35 | # error "Unknown PYBIND11_COMPILER_TYPE: PLEASE REVISE THIS CODE."
| ^~~~~
```
(All other CI jobs succeeded.)
Further thought:
Essentially, under Linux and macOS the `PYBIND11_COMPILER_TYPE` is only for informational purposes.
ABI compatibility is determined by the libstdc++ or libc++ ABI version.
* Add `PYBIND11_COMPILER_TYPE` `emscripten`
* Add `PYBIND11_COMPILER_TYPE` `graalvm`
* Revert "Add `PYBIND11_COMPILER_TYPE` `graalvm`"
This reverts commit 75da5fbfd9.
* Revert "Add `PYBIND11_COMPILER_TYPE` `emscripten`"
This reverts commit e34dc8b511.
* Revert "Make determination of `PYBIND11_COMPILER_TYPE` `"macos"` or `"glibc"` more general."
This reverts commit 41daaa41fa.
* Revert "Apply suggestion by @isuruf, with revised comments (code is as suggested)."
This reverts commit ca9e6990de.
* Remove `defined(__INTEL_COMPILER)` as suggested by @hpkfft under https://github.com/pybind/pybind11/pull/5439#discussion_r1889156543
---------
Co-authored-by: Marcus D. Hanwell <marcus@cryos.net>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
* Added arg/return type handling.
* Added support for nested arg/return type in py::typing::List
* Added support for arg/return type in stl/filesystem
* Added tests for arg/return type in stl/filesystem and py::typing::List
* Added arg/return name to more py::typing classes
* Added arg/return type to Callable[...]
* Added tests for typing container classes (also nested)
* Changed typing classes to avoid using C++14 auto return type deduction.
* Fixed clang-tidy errors.
* Changed Enable to SFINAE
* Added test for Tuple[T, ...]
* Added RealNumber with custom caster for testing typing classes.
* Added tests for Set, Iterable, Iterator, Union, and Optional
* Added tests for Callable
* Fixed Callable with ellipsis test
* Changed TypeGuard/TypeIs to use return type (being the narrower type) + Tests
* Added test for use of fallback type name with stl vector
* Updated documentation.
* Fixed unnecessary constructor call in test.
* Fixed reference counting in example type caster.
* Fixed clang-tidy issues.
* Fix for clang-tidy
* Updated cast method to use pybind11 API rather than Python C API in custom caster example
* Updated load to use pybind11 API rather than Python C API in custom caster example
* Changed test of arg/return name to use pybind11 API instead of Python C API
* Updated code in adcanced/cast example and improved documentation text
* Fixed references in custom type caster docs
* Fixed wrong logical and operator in test
* Fixed wrong logical operator in doc example
* Added comment to test about `float` vs `float | int`
* Updated std::filesystem::path docs in cast/overview section
* Remove one stray dot.
---------
Co-authored-by: Ralf W. Grosse-Kunstleve <rgrossekunst@nvidia.com>
* fix: make PYBIND11_WARNING_POP actually pop clang diagnostics
* fix: ignore -Wgnu-zero-variadic-macro-arguments on clang
* Revert "fix: ignore -Wgnu-zero-variadic-macro-arguments on clang"
This reverts commit d310959bf5.
* C++20 modernization: Use `__VA_OPT__(, ) __VA_ARGS__` in `PYBIND11_DECLARE_HOLDER_TYPE()`
* Disable `__VA_OPT__(, ) __VA_ARGS__` usage for MSVC (it is unclear to rwgk why it does not work).
This is the beginning of the error message:
```
D:\a\pybind11\pybind11\tests\test_smart_ptr.cpp(285,1): error C2143: syntax error: missing ')' before ',' [D:\a\pybind11\pybind11\build\tests\pybind11_tests.vcxproj]
D:\a\pybind11\pybind11\tests\test_smart_ptr.cpp(285,1): error C2059: syntax error: ')' [D:\a\pybind11\pybind11\build\tests\pybind11_tests.vcxproj]
```
* Add `PYBIND11_WARNING_DISABLE_CLANG("-Wgnu-zero-variadic-macro-arguments")` in test_smart_ptr.cpp
This is the error message:
```
/__w/pybind11/pybind11/tests/test_smart_ptr.cpp:287:51: error: must specify at least one argument for '...' parameter of variadic macro [-Werror,-Wgnu-zero-variadic-macro-arguments]
PYBIND11_DECLARE_HOLDER_TYPE(T, std::shared_ptr<T>)
^
/__w/pybind11/pybind11/include/pybind11/cast.h:885:13: note: macro 'PYBIND11_DECLARE_HOLDER_TYPE' defined here
^
```
* Also add `PYBIND11_WARNING_DISABLE_CLANG("-Wgnu-zero-variadic-macro-arguments")` in test_virtual_functions.cpp
* Also add `PYBIND11_WARNING_DISABLE_CLANG("-Wgnu-zero-variadic-macro-arguments")` in test_embed/test_interpreter.cpp
* Undo all changes except the original push -> pop fix.
* 1. Add `PYBIND11_WARNING_DISABLE_CLANG("-Wgnu-zero-variadic-macro-arguments")` near the top of pybind11/pybind11.h; 2. Change `PYBIND11_DECLARE_HOLDER_TYPE` macro to side-step the only remaining clang warning-as-error (this is still needed even for clang 18).
Alternatively the warning suppression could be moved into pybind11/cast.h, but this commit limits the warning suppression to smaller scope within include/pybind11.
---------
Co-authored-by: Ralf W. Grosse-Kunstleve <rgrossekunst@nvidia.com>