* Add support for array_t<handle> and array_t<object>
* style: pre-commit fixes
* Remove loops that aren't strictly needed
* Fix compiler warning
* Disable GC-dependent checks when running on pypy or graalpy
* style: pre-commit fixes
* Remove PyValueHolder counter again
* Move tests to templates to avoid code duplication
* Rerun pre-commit
* Restore import that was erroneously removed by pre-commit
* Reduce code duplication with more template magic
* Bring back `.attr("value")` in `return_array_cpp_loop()`
This was meant to further stress-test correctness of refcount handling.
All modified test functions were manually leak-checked (`while True`, top command, Python 3.12.3, Ubuntu 24.01, gcc 13.2.0).
---------
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>
* feat: allow annotate methods with `pos_only` when only have the `self` argument
* chore(typing): make arguments for auto-generated dunder methods positional-only
* docs: add more comments to improve readability
* style: fix nit suggestions
* Add test_self_only_pos_only() in tests/test_methods_and_attributes
* test: add docstring tests for generated dunder methods
* test: remove failed tests
* fix(test): run `gc.collect()` three times for refcount tests
---------
Co-authored-by: Ralf W. Grosse-Kunstleve <rgrossekunst@nvidia.com>
* Allow subclasses of args and kwargs
The current implementation disallows subclasses of args and kwargs
* Added object type hint to args and kwargs
* Added type hinted args and kwargs classes
* Changed default type hint to typing.Any
* Removed args and kwargs type hint
* Updated tests
Modified the tests from #5381 to use the real Args and KWArgs classes
* Added comment
* Factor out pybind11/compat/wrap_include_python_h.h
* Fixes to resolve tests_packaging failures.
* Factor out pybind11/compat/pybind11_platform_abi_id.h
* Add pybind11/compat/README.txt and a couple source code comments.
* Minor changes to comments.
* Factor out pybind11/compat/pybind11_conduit_v1.h
* Add long comment to pybind11/compat/pybind11_conduit_v1.h
* Add pybind11/compat/README.txt to wheels.
* Add `-fno-exceptions` to compiler options for exo_planet_c_api
* 1. Move `target_compile_options()` into loop over test targets, in case the `"exo_planet_c_api"` target does not exist. 2. Add `-fno-exceptions` option also for `NVHPC`. 3. Also check for `__cpp_exceptions` in exo_planet_c_api.cpp.
* 1. Fix accident (forgot to undo temporary change). 2. Special-case __EMSCRIPTEN__ in exo_planet_c_api.cpp
* Give up on compiling exo_planet_c_api.cpp with MSVC `/EHs-c-`:
There was one trouble maker (all other jobs worked):
Visual Studio 15 2017:
```
cl : Command line warning D9025: overriding '/EHc' with '/EHc-' [C:\projects\pybind11\tests\exo_planet_c_api.vcxproj]
...
C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.16.27023\include\xlocale(319): error C2220: warning treated as error - no 'object' file generated [C:\projects\pybind11\tests\exo_planet_c_api.vcxproj]
C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.16.27023\include\xlocale(319): warning C4530: C++ exception handler used, but unwind semantics are not enabled. Specify /EHsc
```
* Move pybind11/compat to pybind11/conduit as suggested by @henryiii:
https://github.com/pybind/pybind11/pull/5375#pullrequestreview-2329006001
* Fix MSVC MT/MD incompatibility in PYBIND11_BUILD_ABI
* Update comment about which PR
* Use msvc major version
* Use _MSC_VER/100
* Fix figuring out MD vs MT
* Add some test runs
* Skip one test
* Fix preprocessor
* simplify code
* fix if
* support only msvc 19
* Fold in changes from experimental PR #5411. Polish error messages.
* Remove `&& defined(_DLL)` (TBD: is it needed? but what is correct?)
* Fix MT vs MD
* Add a couple comments, based on https://github.com/pybind/pybind11/pull/4953#issuecomment-2435138593 (posted by @isuruf).
* Replace misleading comment: NVHPC is NOT outdated.
* Update include/pybind11/detail/internals.h
Co-authored-by: Robert Maynard <robertjmaynard@gmail.com>
---------
Co-authored-by: Ralf W. Grosse-Kunstleve <rgrossekunst@nvidia.com>
Co-authored-by: Ralf W. Grosse-Kunstleve <rwgkio@gmail.com>
Co-authored-by: Robert Maynard <robertjmaynard@gmail.com>
* Fix data race all_type_info_populate in free-threading mode
Description:
- fixed data race all_type_info_populate in free-threading mode
- added test
For example, we have 2 threads entering `all_type_info`.
Both enter `all_type_info_get_cache`` function and
there is a first one which inserts a tuple (type, empty_vector) to the map
and second is waiting. Inserting thread gets the (iter_to_key, True) and non-inserting thread
after waiting gets (iter_to_key, False).
Inserting thread than will add a weakref and will then call into `all_type_info_populate`.
However, non-inserting thread is not entering `if (ins.second) {` clause and
returns `ins.first->second;`` which is just empty_vector.
Finally, non-inserting thread is failing the check in `allocate_layout`:
```c++
if (n_types == 0) {
pybind11_fail(
"instance allocation failed: new instance has no pybind11-registered base types");
}
```
* style: pre-commit fixes
* Addressed PR comments
---------
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
* Fix buffer protocol implementation
According to the buffer protocol, `ndim` is a _required_ field [1], and
should always be set correctly. Additionally, `shape` should be set if
flags includes `PyBUF_ND` or higher [2]. The current implementation only
set those fields if flags was `PyBUF_STRIDES`.
[1] https://docs.python.org/3/c-api/buffer.html#request-independent-fields
[2] https://docs.python.org/3/c-api/buffer.html#shape-strides-suboffsets
* Apply suggestions from review
* Obey contiguity requests for buffer protocol
If a contiguous buffer is requested, and the underlying buffer isn't,
then that should raise. This matches NumPy behaviour if you do something
like:
```
struct.unpack_from('5d', np.arange(20.0)[::4]) # Raises for contiguity
```
Also, if a buffer is contiguous, then it can masquerade as a
less-complex buffer, either by dropping strides, or even pretending to
be 1D. This matches NumPy behaviour if you do something like:
```
a = np.full((3, 5), 30.0)
struct.unpack_from('15d', a) # --> Produces 1D tuple from 2D buffer.
```
* Handle review comments
* Test buffer protocol against NumPy
* Also check PyBUF_FORMAT results
* Incomplete attempt to address regression introduced in #5381
* style: pre-commit fixes
* Revert "style: pre-commit fixes"
This reverts commit 9d107d2f75.
* Revert "Incomplete attempt to address regression introduced in #5381"
This reverts commit 8cf1cdbc96.
* Simpler fix for the regression introduced in #5381
* style: pre-commit fixes
* Added if constexpr workaround
This can probably be done better but at least this is a start.
* style: pre-commit fixes
* Replace if constexpr with template struct
if constexpr was not added until C++ 17.
I think this should do the same thing as before.
* style: pre-commit fixes
* Made comment clearer
* Added test cases
* style: pre-commit fixes
* Fixed is_same_or_base_of reference
* style: pre-commit fixes
* Added static assert messages
* style: pre-commit fixes
* Replaced typedef with using
* style: pre-commit fixes
* Back out `ForwardClassPtr` (to be discussed separately). Tested locally with clang-tidy.
* Shuffle new `static_assert()` and leave error messages blank (they are more distracting than helpful here).
---------
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: gentlegiantJGC <gentlegiantJGC@users.noreply.github.com>
Co-authored-by: Ralf W. Grosse-Kunstleve <rgrossekunst@nvidia.com>
* [docs/advanced] A document about deadlock potential with C++ statics
* [docs/advanced] Refer to deadlock.md from misc.rst
* [docs/advanced] Fix tables in deadlock.md
* Use :ref:`deadlock-reference-label`
* Revert "Use :ref:`deadlock-reference-label`"
This reverts commit e5734d275f.
* Add simple references to docs/advanced/deadlock.md filename. (Maybe someone can work on clickable links later.)
---------
Co-authored-by: Ralf W. Grosse-Kunstleve <rgrossekunst@nvidia.com>
* Print key in map.getitem/delitem KeyError
* Add tests
* Fix tests
* Make robust
* Make clang-tidy happy
* Return a Python str
* Show beginning and end of the message
* Avoid implicit conversion
* Split out `format_message_key_error_key_object()` to reduce amount of templated code.
* Use `"✄✄✄"` instead of `"..."`
Also rename variable to `cut_length`, to not get into even/odd issues with the meaning of "half".
---------
Co-authored-by: Ralf W. Grosse-Kunstleve <rgrossekunst@nvidia.com>
* Initial support for GraalPy
* Mark tests that currently fail on GraalPy with xfail
* Add graalpy to CI
* Limit test deps on graalpy to available binary wheels
* Skip cmake test installed_function on GraalPy
CMake won't find libpython on GraalPy, it either fails or silently picks
CPython's libpython.
* Factor out setting function docstrings into a macro
* Try to narrow down skipped tests
* Allow subclasses of py::args and py::kwargs
The current implementation does not allow subclasses of args or kwargs.
This change allows subclasses to be used.
* Added test case
* style: pre-commit fixes
* Added missing semi-colons
* style: pre-commit fixes
* Added handle_type_name
* Moved classes outside of function
* Added namespaces
* style: pre-commit fixes
* Refactored tests
Added more tests and moved tests to more appropriate locations.
* style: pre-commit fixes
---------
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Issue: #5224
The `internals.registered_types_py...` line in pybind11.h triggers a
false-positive bounds checking warning in GCC 12.
This is discussed in
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=115824.
The workaround implemented is taken from suggestions then refactored to
use the `PYBIND11_WARNING_PUSH` and `PYBIND11_WARNING_POP` MACROS.