Commit Graph

2752 Commits

Author SHA1 Message Date
Kenji
f47ff3280e
Fix grammar in functions.rst (#4791)
The previous sentence had an extra "a" before "several", which isn't right.
2023-08-15 07:09:13 -07:00
Ralf W. Grosse-Kunstleve
80bcd21fb7
[ci skip] Adopt nanobind config. (#4792) 2023-08-15 07:02:54 -07:00
Ralf W. Grosse-Kunstleve
add281a2da
Migrate to readthedocs configuration file v2¶ (#4789)
* Copy of recommded readthedocs configuration file v2

* [ci skip] It is now requirements (not requirements_file)
2023-08-14 21:46:17 -07:00
Henry Schreiner
9039e6ac42
chore: use 2x faster black mirror (#4784)
Committed via https://github.com/asottile/all-repos
2023-08-10 15:17:56 -04:00
Pieter P
4bf60c609a
Disable strip when build type is unset (#4454) (#4780) 2023-08-07 20:58:30 -07:00
Ralf W. Grosse-Kunstleve
690a115d84
Add py::set_error(), use in updated py::exception<> documentation (#4772)
* Copy clang 17 compatibility fixes from PR #4762 to a separate PR.

* static py::exception<> -> static py::handle

* Add `py::set_error()` but also try the suggestion of @malfet (https://github.com/pytorch/pytorch/pull/106401#pullrequestreview-1559961407).

* clang 17 compatibility fixes (#4767)

* Copy clang 17 compatibility fixes from PR #4762 to a separate PR.

* Add gcc:13 C++20

* Add silkeh/clang:16-bullseye C++20

* chore(deps): update pre-commit hooks (#4770)

updates:
- [github.com/psf/black: 23.3.0 → 23.7.0](https://github.com/psf/black/compare/23.3.0...23.7.0)
- [github.com/astral-sh/ruff-pre-commit: v0.0.276 → v0.0.281](https://github.com/astral-sh/ruff-pre-commit/compare/v0.0.276...v0.0.281)
- [github.com/asottile/blacken-docs: 1.14.0 → 1.15.0](https://github.com/asottile/blacken-docs/compare/1.14.0...1.15.0)

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>

* docs: Remove upper bound on pybind11 in example pyproject.toml for setuptools (#4774)

* docs: Remove upper bound on pybind11 in example pyproject.toml for setuptools

* Update docs/compiling.rst

---------

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

* Provide better type hints for a variety of generic types (#4259)

* Provide better type hints for a variety of generic types

* Makes better documentation
* tuple, dict, list, set, function

* Move to py::typing

* style: pre-commit fixes

* Update copyright line with correct year and actual author. The author information was copy-pasted from the git log output.

---------

Co-authored-by: Ralf W. Grosse-Kunstleve <rwgk@google.com>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>

* Use `py::set_error()` everywhere possible (only one special case, in common.h).
Overload `py::set_error(py::handle, py::handle)`.
Change back to `static py::handle exc = ... .release();`
Deprecate `py::exception<>::operator()`

* Add `PYBIND11_WARNING_DISABLE` for INTEL and MSVC (and sort alphabetically).

* `PYBIND11_WARNING_DISABLE_INTEL(10441)` does not work.

For ICC only, falling back to the recommended `py::set_error()` to keep the testing simple.

It is troublesome to add `--diag-disable=10441` specifically for test_exceptions.cpp, even that is non-ideal because it covers the entire file, not just the one line we need it for, and the value of exercising the trivial deprecated `operator()` on this one extra platform is practically zero.

* Fix silly oversight.

* NVHPC 23.5.0 generates deprecation warnings. They are currently not treated as errors, but falling back to using `py::set_error()` to not have to deal with that distraction.

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Keto D. Zhang <keto.zhang@gmail.com>
Co-authored-by: Henry Schreiner <HenrySchreinerIII@gmail.com>
Co-authored-by: Dustin Spicuzza <dustin@virtualroadside.com>
2023-08-07 20:48:20 -07:00
Axel Huebl
824dc27a01
CI: Reenable an NVHPC Test (#4764)
Update from CentOS to Ubuntu and to a recent version
of NVHPC (former: PGI).

Co-authored-by: Ralf W. Grosse-Kunstleve <rwgk@google.com>
2023-08-04 11:17:33 -07:00
Dustin Spicuzza
f8703154ec
Provide better type hints for a variety of generic types (#4259)
* Provide better type hints for a variety of generic types

* Makes better documentation
* tuple, dict, list, set, function

* Move to py::typing

* style: pre-commit fixes

* Update copyright line with correct year and actual author. The author information was copy-pasted from the git log output.

---------

Co-authored-by: Ralf W. Grosse-Kunstleve <rwgk@google.com>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
2023-08-03 22:48:57 -07:00
Keto D. Zhang
9ad7e827a2
docs: Remove upper bound on pybind11 in example pyproject.toml for setuptools (#4774)
* docs: Remove upper bound on pybind11 in example pyproject.toml for setuptools

* Update docs/compiling.rst

---------

Co-authored-by: Henry Schreiner <HenrySchreinerIII@gmail.com>
2023-08-03 22:14:23 -07:00
pre-commit-ci[bot]
413e6328dc
chore(deps): update pre-commit hooks (#4770)
updates:
- [github.com/psf/black: 23.3.0 → 23.7.0](https://github.com/psf/black/compare/23.3.0...23.7.0)
- [github.com/astral-sh/ruff-pre-commit: v0.0.276 → v0.0.281](https://github.com/astral-sh/ruff-pre-commit/compare/v0.0.276...v0.0.281)
- [github.com/asottile/blacken-docs: 1.14.0 → 1.15.0](https://github.com/asottile/blacken-docs/compare/1.14.0...1.15.0)

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
2023-08-03 22:11:25 -07:00
Ralf W. Grosse-Kunstleve
17b614303f
clang 17 compatibility fixes (#4767)
* Copy clang 17 compatibility fixes from PR #4762 to a separate PR.

* Add gcc:13 C++20

* Add silkeh/clang:16-bullseye C++20
2023-08-03 13:36:29 -07:00
Ralf W. Grosse-Kunstleve
f3e0602802
Add command to check for vX.Y.Z tag vs pybind11/_version.py consistency. (#4757)
* Add command to check for vX.Y.Z tag vs pybind11/_version.py consistency. Piggy-backing hints for converting changelog to release message.

* Simpler way of double-checking version number in sources vs. git tag

* Even simpler.

* Fix rst rendering (hopefully) and remove stray dots.

* [ci skip] Trying more to make GitHub rst renderer show this nicely.

* [ci skip] Fix up RST rendering issues.

Lesson learned:

This is NOT GOOD:

```
- Bullet nesting level 1.
    - Bullet nesting level 2.
```

This is BETTER:

```
- Bullet nesting level 1.
  - Bullet nesting level 2.
```

Also consistently adding empty lines between bullet points, to make the .rst
file easier to read.

Also piggy-backing a few very minor enhancements.
2023-07-23 11:10:10 -07:00
Ralf W. Grosse-Kunstleve
8d08dc64ca
Copy v2.11.1 changelog section as released. (#4755) 2023-07-17 12:07:42 -07:00
Ethan Steinberg
99131a85b6
Provide PYBIND11_NO_ASSERT_GIL_HELD_INCREF_DECREF as an option (#4753)
* Remove GIL checks

* Update common.h

* Add flag

* style: pre-commit fixes

* Update pytypes.h

* style: pre-commit fixes

* Update common.h

* style: pre-commit fixes

* Update pytypes.h

* style: pre-commit fixes

* Update common.h

* style: pre-commit fixes

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
2023-07-17 00:05:52 -07:00
Ralf W. Grosse-Kunstleve
ec1b57c50c
Disable PYBIND11_ASSERT_GIL_HELD_INCREF_DECREF generally for PyPy (not just PyPy Windows). (#4751) 2023-07-15 12:55:00 -07:00
bzaar
0620d71638
Update README.rst - Add missing comma in the list of acknowlegements (#4750) 2023-07-15 09:58:54 -07:00
Ralf W. Grosse-Kunstleve
e85696e80e
Post release version bump (#4747)
* Boilerplate version number update after v2.11.0 release.

* Small updates to release.rst
2023-07-14 14:53:58 -07:00
Ralf W. Grosse-Kunstleve
1a917f1852
docs: preparation for v2.11.0 release (#4744)
* Remove .dev1 from version number.

* Update Changelog (starting from `nox -s make_changelog` output)

* Miscellaneous minor fixes from proofreading in GitHub web view.

* docs: minor changelog updates

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

---------

Signed-off-by: Henry Schreiner <henryschreineriii@gmail.com>
Co-authored-by: Henry Schreiner <henryschreineriii@gmail.com>
2023-07-14 10:20:38 -05:00
Aaron Gokaslan
b33d06f615
bugfix: fixes a test suite bug in the __new__ example (#4698)
* bugfix: fixes a test suite bug in the __new__ example

* See https://github.com/pybind/pybind11/pull/4698#discussion_r1227107682

---------

Co-authored-by: Ralf W. Grosse-Kunstleve <rwgk@google.com>
2023-07-12 13:20:08 -07:00
Henry Schreiner
2e5f5c4cf8
fix: support CMake 3.27, drop 3.4 (#4719)
* fix: support CMake 3.27, drop 3.4

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

* Update upgrade.rst

* Update upgrade.rst

* Update upgrade.rst

---------

Signed-off-by: Henry Schreiner <henryschreineriii@gmail.com>
2023-07-12 13:10:24 -05:00
Luc de Jonckheere
6d22dba82f
Warning on comparing wrapper enums with is (#4732)
* Warning on comparing wrapper enums with is

* backticks for quoting and link to related issue

---------

Co-authored-by: Ralf W. Grosse-Kunstleve <rwgk@google.com>
2023-07-12 08:56:40 -07:00
dependabot[bot]
b2732c6e1a
chore(deps): bump pypa/gh-action-pypi-publish from 1.8.6 to 1.8.7 (#4718)
* chore(deps): bump pypa/gh-action-pypi-publish from 1.8.6 to 1.8.7

Bumps [pypa/gh-action-pypi-publish](https://github.com/pypa/gh-action-pypi-publish) from 1.8.6 to 1.8.7.
- [Release notes](https://github.com/pypa/gh-action-pypi-publish/releases)
- [Commits](https://github.com/pypa/gh-action-pypi-publish/compare/v1.8.6...v1.8.7)

---
updated-dependencies:
- dependency-name: pypa/gh-action-pypi-publish
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

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

* Apply suggestions from code review

---------

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>
2023-07-12 08:55:18 -07:00
pre-commit-ci[bot]
47dc0c4bd1
chore(deps): update pre-commit hooks (#4727)
updates:
- [github.com/pre-commit/mirrors-clang-format: v16.0.4 → v16.0.6](https://github.com/pre-commit/mirrors-clang-format/compare/v16.0.4...v16.0.6)
- https://github.com/charliermarsh/ruff-pre-commithttps://github.com/astral-sh/ruff-pre-commit
- [github.com/astral-sh/ruff-pre-commit: v0.0.270 → v0.0.276](https://github.com/astral-sh/ruff-pre-commit/compare/v0.0.270...v0.0.276)
- [github.com/pre-commit/mirrors-mypy: v1.3.0 → v1.4.1](https://github.com/pre-commit/mirrors-mypy/compare/v1.3.0...v1.4.1)
- [github.com/asottile/blacken-docs: 1.13.0 → 1.14.0](https://github.com/asottile/blacken-docs/compare/1.13.0...1.14.0)
- [github.com/codespell-project/codespell: v2.2.4 → v2.2.5](https://github.com/codespell-project/codespell/compare/v2.2.4...v2.2.5)

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Henry Schreiner <HenrySchreinerIII@gmail.com>
2023-07-11 10:31:43 -07:00
dependabot[bot]
d462dd91a5
chore(deps): bump scipy from 1.8.0 to 1.10.0 in /tests (#4731)
Bumps [scipy](https://github.com/scipy/scipy) from 1.8.0 to 1.10.0.
- [Release notes](https://github.com/scipy/scipy/releases)
- [Commits](https://github.com/scipy/scipy/compare/v1.8.0...v1.10.0)

---
updated-dependencies:
- dependency-name: scipy
  dependency-type: direct:production
...

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 <rwgk@google.com>
2023-07-10 10:43:20 -07:00
Ralf W. Grosse-Kunstleve
782b6281a2
Drop PyPy 3.7 from GitHub Actions (ci.yml) (#4728)
* Remove `pypy-3.7` from ci.yml

* Update .github/workflows/ci.yml

---------

Co-authored-by: Henry Schreiner <HenrySchreinerIII@gmail.com>
2023-07-09 09:40:47 -07:00
Henry Schreiner
5ccb9e412d
chore: ruff moved to astral-sh (#4726)
* chore: ruff moved to astral-sh

Committed via https://github.com/asottile/all-repos

* ci: bump CUDA version
2023-07-04 08:38:14 -04:00
Ralf W. Grosse-Kunstleve
2fb3d7cbde
Trivial refactoring to make the capsule API more user friendly. (#4720)
* Trivial refactoring to make the capsule API more user friendly.

* Use new API in production code. Thanks @Lalaland for pointing this out.
2023-06-27 15:08:32 -07:00
Ralf W. Grosse-Kunstleve
e10da79b6e
Undo ci.yml gcc10 workaround after docker-library/gcc#95 was resolved. (#4717)
The gcc10 workaround was introduced with PR #4705.
2023-06-24 12:12:35 -07:00
Henry Schreiner
bc1bcf7c05
chore: 3.12 + cleanup (#4713)
Signed-off-by: Henry Schreiner <henryschreineriii@gmail.com>
2023-06-21 13:25:10 -04:00
Ralf W. Grosse-Kunstleve
849322806c
Systematically add PIP_BREAK_SYSTEM_PACKAGES to all .yml files from which pip is called. (#4705)
* Systematically add PIP_BREAK_SYSTEM_PACKAGES to all .yml files from which pip is called.

* Try gcc:10-bullseye (because gcc:10 is broken: https://github.com/docker-library/gcc/issues/95)

* bug fix (matrix did not work as hoped)
2023-06-17 07:02:23 -07:00
Ralf W. Grosse-Kunstleve
86f60a0c07
pre-commit markdown-it-py<3 (for Python 3.7 compatibility) (#4704)
* markdown-it-py dropped Python 3.7 support, but we are type checking for Python 3.7 from a newer version of Python. Keep using markdown-it-py<3 as long as we support Python 3.7.

* Shuffle order of pre-commit checks for better agility.
2023-06-16 14:09:40 -07:00
Ralf W. Grosse-Kunstleve
0e43fcc75e
Python 3.12b2 testing (#4695)
* Uncomment `Interface test` sections (test_embed)

* fix: setuptools has been removed from default installs in 3.12

---------

Co-authored-by: Henry Schreiner <HenrySchreinerIII@gmail.com>
2023-06-10 10:14:08 -07:00
dependabot[bot]
c679a92095
chore(deps): bump deadsnakes/action from 3.0.0 to 3.0.1 (#4687)
Bumps [deadsnakes/action](https://github.com/deadsnakes/action) from 3.0.0 to 3.0.1.
- [Release notes](https://github.com/deadsnakes/action/releases)
- [Commits](https://github.com/deadsnakes/action/compare/v3.0.0...v3.0.1)

---
updated-dependencies:
- dependency-name: deadsnakes/action
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

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 <rwgk@google.com>
2023-06-07 13:49:14 -07:00
pre-commit-ci[bot]
3617f3554a
chore(deps): update pre-commit hooks (#4689)
* chore(deps): update pre-commit hooks

updates:
- [github.com/charliermarsh/ruff-pre-commit: v0.0.263 → v0.0.270](https://github.com/charliermarsh/ruff-pre-commit/compare/v0.0.263...v0.0.270)
- [github.com/pre-commit/mirrors-mypy: v1.2.0 → v1.3.0](https://github.com/pre-commit/mirrors-mypy/compare/v1.2.0...v1.3.0)
- [github.com/shellcheck-py/shellcheck-py: v0.9.0.2 → v0.9.0.5](https://github.com/shellcheck-py/shellcheck-py/compare/v0.9.0.2...v0.9.0.5)
- [github.com/pre-commit/mirrors-clang-format: v16.0.2 → v16.0.4](https://github.com/pre-commit/mirrors-clang-format/compare/v16.0.2...v16.0.4)

* Resolve new ruff error:

```
tests/test_pytypes.py:478:20: PLW0130 Duplicate value `3` in set
```

The reduction from `{3, 3}` to `{3}` never happened in pybind11 code, therefore this change is essentially a no-op for the purpose of unit-testing pybind11.

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Ralf W. Grosse-Kunstleve <rwgk@google.com>
2023-06-07 12:57:38 -07:00
Ralf W. Grosse-Kunstleve
29487dee8f
Disable 🐍 3 • CentOS7 / PGI 22.9 • x64 (#4691) 2023-06-07 12:00:37 -07:00
T.Yamada
d0232b119f
Use annotated for array (#4679)
* use Annotated for std::array docstring

* add tests

* fix test

* style: pre-commit fixes

* fix valarray annotation

* style: pre-commit fixes

* refix test

* add FixedSize

* style: pre-commit fixes

* Update test_stl.py

* style: pre-commit fixes

---------

Co-authored-by: Taiju Yamada <taiju.yamada@mujin.co.jp>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
2023-05-24 21:39:36 -07:00
Ralf W. Grosse-Kunstleve
8e1f9d5c40
Add format_descriptor<> & npy_format_descriptor<> PyObject * specializations. (#4674)
* Add `npy_format_descriptor<PyObject *>` to enable `py::array_t<PyObject *>` to/from-python conversions.

* resolve clang-tidy warning

* Use existing constructor instead of adding a static method. Thanks @Skylion007 for pointing out.

* Add `format_descriptor<PyObject *>`

Trivial addition, but still in search for a meaningful test.

* Add test_format_descriptor_format

* Ensure the Eigen `type_caster`s do not segfault when loading arrays with dtype=object

* Use `static_assert()` `!std::is_pointer<>` to replace runtime guards.

* Add comments to explain how to check for ref-count bugs. (NO code changes.)

* Make the "Pointer types ... are not supported" message Eigen-specific, as suggested by @Lalaland. Move to new pybind11/eigen/common.h header.

* Change "format_descriptor_format" implementation as suggested by @Lalaland. Additional tests meant to ensure consistency between py::format_descriptor<>, np.array, np.format_parser turn out to be useful only to highlight long-standing inconsistencies.

* resolve clang-tidy warning

* Account for np.float128, np.complex256 not being available on Windows, in a future-proof way.

* Fully address i|q|l ambiguity (hopefully).

* Remove the new `np.format_parser()`-based test, it's much more distracting than useful.

* Use bi.itemsize to disambiguate "l" or "L"

* Use `py::detail::compare_buffer_info<T>::compare()` to validate the `format_descriptor<T>::format()` strings.

* Add `buffer_info::compare<T>` to make `detail::compare_buffer_info<T>::compare` more visible & accessible.

* silence clang-tidy warning

* pytest-compatible access to np.float128, np.complex256

* Revert "pytest-compatible access to np.float128, np.complex256"

This reverts commit e9a289c50f.

* Use `sizeof(long double) == sizeof(double)` instead of `std::is_same<>`

* Report skipped `long double` tests.

* Change the name of the new `buffer_info` member function to `item_type_is_equivalent_to`. Add comment defining "equivalent" by example.

* Change `item_type_is_equivalent_to<>()` from `static` function to member function, as suggested by @Lalaland
2023-05-23 10:49:32 -07:00
Joyce
6e6bcca5b2
Create s Security Policy (#4671)
* Create SECURITY.md

* Update test_files.py to include SECURITY.md file

* Update MANIFEST.in to include SECURITY.md file
2023-05-23 10:05:25 -07:00
Ralf W. Grosse-Kunstleve
ce9bbc0a21
Python 3.11+: Add __notes__ to error_already_set::what() output. (#4678)
* First version adding `__notes__` to `error_already_set::what()` output.

* Fix trivial oversight (missing adjustment in existing test).

* Minor enhancements of new code.

* Re-enable `cmake --target cpptest -j 2`

* Revert "Re-enable `cmake --target cpptest -j 2`"

This reverts commit 60816285e9.

* Add general comment explaining why the `error_fetch_and_normalize` code is so unusual.
2023-05-23 10:03:33 -07:00
Ralf W. Grosse-Kunstleve
19816f0db7
chore: update changelog, with target date for v2.11.0 release (#4677)
* Remove .dev1 from version number.

* [skip ci] Update changelog.rst

* [ci skip] Fix pre-commit: rst ``code`` is two backticks

* Apply fix suggested by @henryiii

* [ci skip] Set target date for the release to June 2, 2023

* [ci skip] Apply more fixes suggested by @henryiii (I missed those before).

* [ci skip] Revert "Remove .dev1 from version number."

This reverts commit afc80134cb.
2023-05-22 13:44:03 -07:00
Joyce
d72ffb448c
ci: set minimal permissions to github workflows (#4665)
* set ci.yml minimal permissions

Signed-off-by: Joyce <joycebrum@google.com>

* set configure.yml minimal permissions

Signed-off-by: Joyce <joycebrum@google.com>

* set format.yml minimal permissions

Signed-off-by: Joyce <joycebrum@google.com>

* set pip.yml minimal permissions

Signed-off-by: Joyce <joycebrum@google.com>

* set upstream.yml minimal permissions

Signed-off-by: Joyce <joycebrum@google.com>

* set labeler.yml minimal permissions

Signed-off-by: Joyce <joycebrum@google.com>

* Update ci.yml to read all

Signed-off-by: Joyce <joycebrum@google.com>

* test labeler.yml

Signed-off-by: Joyce <joycebrum@google.com>

* restore the if at labeler.yml

Signed-off-by: Joyce <joycebrum@google.com>

---------

Signed-off-by: Joyce <joycebrum@google.com>
2023-05-15 13:02:25 -04:00
Tim Stumbaugh
cca4c51ca4
Update errors in string "Explicit conversions" docs (#4658)
`PyUnicode_DecodeLatin1` requires you to pass in the `error`
parameter. The code as it is in the docs didn't compile.

There is a reference leak in the example
code. `PyUnicode_DecodeLatin1` returns a new reference. Calling
`py::str(PyObject*)` calls `PyObject_Str`, which also returns a new
reference. That reference is managed by the `py::str`
constructor (which correctly steals the reference, using the
`stolen_t` constructor), but the original reference returned by
`PyUnicode_DecodeLatin1` is never decref'd: it never makes it into an
`object`, and it's never manually decremented.

This fixes both of those issues. The code compiles, and I viewed the
sphinx docs locally.
2023-05-09 07:04:20 -07:00
Ralf W. Grosse-Kunstleve
e9b961d9b9
Elide to-python conversion of setter return values (#4621)
* Reproducer for property setter with return type that is not wrapped.

* Use `py::class_<OptionsBase>()` to work around the return value conversion issue.

* WIP drop_return_value

* Remove struct drop_return_value

* Introduce `return_value_policy::return_none` for use by setters.

* Add `is_setter` to attr.h and use from `.def_property()`

* Merge the new test into test_methods_and_attributes

* Remove return_none return_value_policy again.

* Fix oversight (NOLINTNEXTLINE placement).

* Simplification (for the better) found while searching for a way to resolve GCC build failures.

Example of failure resolved by this change:

g++ (Ubuntu 9.4.0-1ubuntu1~20.04.1) 9.4.0

```
cd /build/tests && /usr/bin/c++ -DPYBIND11_TEST_EIGEN -Dpybind11_tests_EXPORTS -I/mounted_pybind11/include -isystem /usr/include/python3.8 -isystem /build/_deps/eigen-src -g -std=c++17 -fPIC -fvisibility=hidden -Wall -Wextra -Wconversion -Wcast-qual -Wdeprecated -Wundef -Wnon-virtual-dtor -MD -MT tests/CMakeFiles/pybind11_tests.dir/test_buffers.cpp.o -MF CMakeFiles/pybind11_tests.dir/test_buffers.cpp.o.d -o CMakeFiles/pybind11_tests.dir/test_buffers.cpp.o -c /mounted_pybind11/tests/test_buffers.cpp
In file included from /mounted_pybind11/include/pybind11/stl.h:12,
                 from /mounted_pybind11/tests/test_buffers.cpp:10:
/mounted_pybind11/include/pybind11/pybind11.h: In instantiation of ‘pybind11::class_<type_, options>& pybind11::class_<type_, options>::def_property(const char*, const Getter&, const Setter&, const Extra& ...) [with Getter = pybind11::cpp_function; Setter = std::nullptr_t; Extra = {pybind11::return_value_policy}; type_ = pybind11::buffer_info; options = {}]’:
/mounted_pybind11/include/pybind11/pybind11.h:1716:58:   required from ‘pybind11::class_<type_, options>& pybind11::class_<type_, options>::def_property_readonly(const char*, const pybind11::cpp_function&, const Extra& ...) [with Extra = {pybind11::return_value_policy}; type_ = pybind11::buffer_info; options = {}]’
/mounted_pybind11/include/pybind11/pybind11.h:1684:9:   required from ‘pybind11::class_<type_, options>& pybind11::class_<type_, options>::def_readonly(const char*, const D C::*, const Extra& ...) [with C = pybind11::buffer_info; D = long int; Extra = {}; type_ = pybind11::buffer_info; options = {}]’
/mounted_pybind11/tests/test_buffers.cpp:209:61:   required from here
/mounted_pybind11/include/pybind11/pybind11.h:1740:25: error: call of overloaded ‘cpp_function(std::nullptr_t&, pybind11::is_setter)’ is ambiguous
 1740 |             name, fget, cpp_function(method_adaptor<type>(fset), is_setter()), extra...);
      |                         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/mounted_pybind11/include/pybind11/pybind11.h:101:5: note: candidate: ‘pybind11::cpp_function::cpp_function(Func&&, const Extra& ...) [with Func = std::nullptr_t&; Extra = {pybind11::is_setter}; <template-parameter-1-3> = void]’
  101 |     cpp_function(Func &&f, const Extra &...extra) {
      |     ^~~~~~~~~~~~
In file included from /mounted_pybind11/include/pybind11/stl.h:12,
                 from /mounted_pybind11/tests/test_buffers.cpp:10:
/mounted_pybind11/include/pybind11/pybind11.h:87:5: note: candidate: ‘pybind11::cpp_function::cpp_function(std::nullptr_t, const Extra& ...) [with Extra = {pybind11::is_setter}; std::nullptr_t = std::nullptr_t]’
   87 |     cpp_function(std::nullptr_t, const Extra &...) {}
      |     ^~~~~~~~~~~~
```

* Bug fix: obvious in hindsight. I thought the original version was incrementing the reference count for None, but no.

Discovered via many failing tests in the wild (10s of thousands).

It is very tricky to construct a meaningful unit test for this bug specifically. It's unlikely to come back, because 10s of thousands of tests will fail again.
2023-05-08 10:13:54 -07:00
Ralf W. Grosse-Kunstleve
90312a6ee8
Add type_caster<PyObject> (#4601)
* Add `type_caster<PyObject>` (tests are still incomplete).

* Fix oversight (`const PyObject *`).

* Ensure `type_caster<PyObject>` only works for `PyObject *`

* Move `is_same_ignoring_cvref` into `detail` namespace.

* Add test_cast_nullptr

* Change is_same_ignoring_cvref from variable template to using.

```
test_type_caster_pyobject_ptr.cpp:8:23: error: variable templates only available with ‘-std=c++14’ or ‘-std=gnu++14’ [-Werror]
    8 | static constexpr bool is_same_ignoring_cvref = std::is_same<detail::remove_cvref_t<T>, U>::value;
      |                       ^~~~~~~~~~~~~~~~~~~~~~
```

* Remove `return_value_policy::reference_internal` `keep_alive` feature (because of doubts about it actually being useful).

* Add missing test, fix bug (missing `throw error_already_set();`), various cosmetic changes.

* Move `type_caster<PyObject>` from test to new include (pybind11/type_caster_pyobject_ptr.h)

* Add new header file to CMakeLists.txt and tests/extra_python_package/test_files.py

* Backport changes from https://github.com/google/pywrapcc/pull/30021 to https://github.com/pybind/pybind11/pull/4601

* Fix oversight in test (to resolve a valgrind leak detection error) and add a related comment in cast.h.

No production code changes.

Make tests more sensitive by using `ValueHolder` instead of empty tuples and dicts.

Manual leak checks with `while True:` & top command repeated for all tests.

* Add tests for interop with stl.h `list_caster`

(No production code changes.)

* Bug fix in test. Minor comment enhancements.

* Change `type_caster<PyObject>::name` to `object`, as suggested by @Skylion007

* Expand comment for the new `T cast(const handle &handle)` [`T` = `PyObject *`]

* Add `T cast(object &&obj)` overload as suggested by @Skylion007

The original suggestion leads to `error: call to 'cast' is ambiguous` (full error message below), therefore SFINAE guarding is needed.

```
clang++ -o pybind11/tests/test_type_caster_pyobject_ptr.os -c -std=c++17 -fPIC -fvisibility=hidden -O0 -g -Wall -Wextra -Wconversion -Wcast-qual -Wdeprecated -Wundef -Wnon-virtual-dtor -Wunused-result -Werror -isystem /usr/include/python3.10 -isystem /usr/include/eigen3 -DPYBIND11_STRICT_ASSERTS_CLASS_HOLDER_VS_TYPE_CASTER_MIX -DPYBIND11_ENABLE_TYPE_CASTER_ODR_GUARD_IF_AVAILABLE -DPYBIND11_TEST_BOOST -Ipybind11/include -I/usr/local/google/home/rwgk/forked/pybind11/include -I/usr/local/google/home/rwgk/clone/pybind11/include /usr/local/google/home/rwgk/forked/pybind11/tests/test_type_caster_pyobject_ptr.cpp
In file included from /usr/local/google/home/rwgk/forked/pybind11/tests/test_type_caster_pyobject_ptr.cpp:1:
In file included from /usr/local/google/home/rwgk/forked/pybind11/include/pybind11/functional.h:12:
In file included from /usr/local/google/home/rwgk/forked/pybind11/include/pybind11/pybind11.h:13:
In file included from /usr/local/google/home/rwgk/forked/pybind11/include/pybind11/detail/class.h:12:
In file included from /usr/local/google/home/rwgk/forked/pybind11/include/pybind11/attr.h:14:
/usr/local/google/home/rwgk/forked/pybind11/include/pybind11/cast.h:1165:12: error: call to 'cast' is ambiguous
    return pybind11::cast<T>(std::move(*this));
           ^~~~~~~~~~~~~~~~~
/usr/local/google/home/rwgk/forked/pybind11/include/pybind11/functional.h:109:70: note: in instantiation of function template specialization 'pybind11::object::cast<_object *>' requested here
                return hfunc.f(std::forward<Args>(args)...).template cast<Return>();
                                                                     ^
/usr/local/google/home/rwgk/forked/pybind11/include/pybind11/functional.h:103:16: note: in instantiation of member function 'pybind11::detail::type_caster<std::function<_object *(int)>>::load(pybind11::handle, bool)::func_wrapper::operator()' requested here
        struct func_wrapper {
               ^
/usr/local/google/home/rwgk/forked/pybind11/include/pybind11/cast.h:1456:47: note: in instantiation of member function 'pybind11::detail::type_caster<std::function<_object *(int)>>::load' requested here
        if ((... || !std::get<Is>(argcasters).load(call.args[Is], call.args_convert[Is]))) {
                                              ^
/usr/local/google/home/rwgk/forked/pybind11/include/pybind11/cast.h:1434:50: note: in instantiation of function template specialization 'pybind11::detail::argument_loader<const std::function<_object *(int)> &, int>::load_impl_sequence<0UL, 1UL>' requested here
    bool load_args(function_call &call) { return load_impl_sequence(call, indices{}); }
                                                 ^
/usr/local/google/home/rwgk/forked/pybind11/include/pybind11/pybind11.h:227:33: note: in instantiation of member function 'pybind11::detail::argument_loader<const std::function<_object *(int)> &, int>::load_args' requested here
            if (!args_converter.load_args(call)) {
                                ^
/usr/local/google/home/rwgk/forked/pybind11/include/pybind11/pybind11.h:101:9: note: in instantiation of function template specialization 'pybind11::cpp_function::initialize<(lambda at /usr/local/google/home/rwgk/forked/pybind11/tests/test_type_caster_pyobject_ptr.cpp:50:9), _object *, const std::function<_object *(int)> &, int, pybind11::name, pybind11::scope, pybind11::sibling, pybind11::return_value_policy>' requested here
        initialize(
        ^
/usr/local/google/home/rwgk/forked/pybind11/include/pybind11/pybind11.h:1163:22: note: in instantiation of function template specialization 'pybind11::cpp_function::cpp_function<(lambda at /usr/local/google/home/rwgk/forked/pybind11/tests/test_type_caster_pyobject_ptr.cpp:50:9), pybind11::name, pybind11::scope, pybind11::sibling, pybind11::return_value_policy, void>' requested here
        cpp_function func(std::forward<Func>(f),
                     ^
/usr/local/google/home/rwgk/forked/pybind11/tests/test_type_caster_pyobject_ptr.cpp:48:7: note: in instantiation of function template specialization 'pybind11::module_::def<(lambda at /usr/local/google/home/rwgk/forked/pybind11/tests/test_type_caster_pyobject_ptr.cpp:50:9), pybind11::return_value_policy>' requested here
    m.def(
      ^
/usr/local/google/home/rwgk/forked/pybind11/include/pybind11/cast.h:1077:3: note: candidate function [with T = _object *, $1 = 0]
T cast(object &&obj) {
  ^
/usr/local/google/home/rwgk/forked/pybind11/include/pybind11/cast.h:1149:1: note: candidate function [with T = _object *]
cast(object &&object) {
^
1 error generated.
```
2023-05-07 10:15:53 -07:00
Franz Pöschel
f701654633
Introduce recursive_container_traits (#4623)
* Testing

* Similar fix for std::vector

* Fix infinite recursion check:

1) Apply to is_copy_assignable additionally
2) Check infinite recursion for map-like types

* style: pre-commit fixes

* Optional commit that demonstrates the limitations of this PR

* Fix positioning of container bindings

The bindings were previously in a block that was only activated if numpy
was available.

* Suggestions from code review: API side

* Suggestions from code review: Test side

* Suggestions from code review

1) Renaming: is_recursive_container and
   MutuallyRecursiveContainerPair(MV|VM)
2) Avoid ambiguous specializations of is_recursive_container

* Some little fixes

* Reordering of structs

* Add recursive checks for is_move_constructible

* Static testing for pybind11 type traits

* More precise checking of recursive types

Instead of a trait `is_recursive_container`, use a trait
`recursive_container_traits` with dependent type
`recursive_container_traits::type_to_check_recursively`.
So, instead of just checking if a type is recursive and then trying to
somehow deal with it, recursively-defined traits such as
is_move_constructible can now directly ask this trait where the
recursion should proceed.

* Review suggestions

1. Use std::conditional
2. Fix typo

* Remove leftover include from test

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
2023-05-04 22:39:05 -07:00
pre-commit-ci[bot]
b3e88ecf89
chore(deps): update pre-commit hooks (#4648)
updates:
- [github.com/pre-commit/mirrors-mypy: v1.1.1 → v1.2.0](https://github.com/pre-commit/mirrors-mypy/compare/v1.1.1...v1.2.0)
- [github.com/pre-commit/mirrors-clang-format: v16.0.0 → v16.0.2](https://github.com/pre-commit/mirrors-clang-format/compare/v16.0.0...v16.0.2)

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
2023-05-03 00:35:21 -04:00
dependabot[bot]
dff75a62bd
chore(deps): bump pypa/gh-action-pypi-publish from 1.8.5 to 1.8.6 (#4650)
Bumps [pypa/gh-action-pypi-publish](https://github.com/pypa/gh-action-pypi-publish) from 1.8.5 to 1.8.6.
- [Release notes](https://github.com/pypa/gh-action-pypi-publish/releases)
- [Commits](https://github.com/pypa/gh-action-pypi-publish/compare/v1.8.5...v1.8.6)

---
updated-dependencies:
- dependency-name: pypa/gh-action-pypi-publish
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2023-05-03 00:34:51 -04:00
biergaizi
da91926295
fix: remove -stdlib=libc++ from setup helpers, not needed on modern Pythons (#4639)
* Inject -stdlib=libc++ on macOS only when it's supported, close #4637.

On macOS, by default, pybind11 currently unconditionally set the compiler
flag "-stdlib=libc++" in Pybind11Extension.__init__(), regardless of which
compiler is used. This flag is required for clang, but is invalid for GCC.
If GCC is used, it causes compilation failures in all Python projects that
use pybind11, with the error message:

    arm64-apple-darwin22-gcc: error: unrecognized command-line option -stdlib=libc++.

This commit uses has_flag() to detect whether "-stdlib=libc++" on macOS,
and injects this flag from build_ext.build_extensions(), rather than
setting it unconditionally.

Signed-off-by: Yifeng Li <tomli@tomli.me>

* revert: just remove flags

---------

Signed-off-by: Yifeng Li <tomli@tomli.me>
Co-authored-by: Henry Schreiner <HenrySchreinerIII@gmail.com>
2023-05-01 10:14:52 -04:00
Henry Schreiner
956390a87f
fix(cmake): only define lto if CMAKE's IPO setting is unset (#4643)
Signed-off-by: Henry Schreiner <henryschreineriii@gmail.com>
2023-04-29 10:15:45 -04:00
pre-commit-ci[bot]
5e946c2fa5
chore(deps): update pre-commit hooks (#4605)
* chore(deps): update pre-commit hooks

updates:
- [github.com/psf/black: 23.1.0 → 23.3.0](https://github.com/psf/black/compare/23.1.0...23.3.0)
- [github.com/Lucas-C/pre-commit-hooks: v1.4.2 → v1.5.1](https://github.com/Lucas-C/pre-commit-hooks/compare/v1.4.2...v1.5.1)
- [github.com/charliermarsh/ruff-pre-commit: v0.0.254 → v0.0.260](https://github.com/charliermarsh/ruff-pre-commit/compare/v0.0.254...v0.0.260)
- [github.com/PyCQA/pylint: v2.16.4 → v3.0.0a6](https://github.com/PyCQA/pylint/compare/v2.16.4...v3.0.0a6)
- [github.com/pre-commit/mirrors-mypy: v1.0.1 → v1.1.1](https://github.com/pre-commit/mirrors-mypy/compare/v1.0.1...v1.1.1)
- [github.com/codespell-project/codespell: v2.2.2 → v2.2.4](https://github.com/codespell-project/codespell/compare/v2.2.2...v2.2.4)
- [github.com/pre-commit/mirrors-clang-format: v15.0.7 → v16.0.0](https://github.com/pre-commit/mirrors-clang-format/compare/v15.0.7...v16.0.0)

* style: pre-commit fixes

* style: fix issues

* Update tests/test_call_policies.py

* Update tests/test_call_policies.py

* fix: ignore code in file

* style: pre-commit fixes

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Henry Schreiner <HenrySchreinerIII@gmail.com>
2023-04-28 14:32:32 -04:00