Commit Graph

555 Commits

Author SHA1 Message Date
Jeremy Maitin-Shepard 62c4909cce
Add `custom_type_setup` attribute (#3287)
* Fix `pybind11::object::operator=` to be safe if `*this` is accessible from Python

* Add `custom_type_setup` attribute

This allows for custom modifications to the PyHeapTypeObject prior to
calling `PyType_Ready`.  This may be used, for example, to define
`tp_traverse` and `tp_clear` functions.
2021-09-24 12:08:22 -07:00
Henry Schreiner 21282e645a
feat: reapply fixed version of #3271 (#3293)
* Add make_value_iterator (#3271)

* Add make_value_iterator

This is the counterpart to make_key_iterator, and will allow
implementing a `value` method in `bind_map` (although doing so is left
for a subsequent PR).

I made a few design changes to reduce copy-and-paste boilerplate.
Previously detail::iterator_state had a boolean template parameter to
indicate whether it was being used for make_iterator or
make_key_iterator. I replaced the boolean with a class that determines
how to dereference the iterator. This allows for a generic
implementation of `__next__`.

I also added the ValueType and Extra... parameters to the iterator_state
template args, because I think it was a bug that they were missing: if
make_iterator is called twice with different values of these, only the
first set has effect (because the state class is only registered once).
There is still a potential issue in that the *values* of the extra
arguments are latched on the first call, but since most policies are
empty classes this should be even less common.

* Add some remove_cv_t to appease clang-tidy

* Make iterator_access and friends take reference

For some reason I'd accidentally made it take a const value, which
caused some issues with third-party packages.

* Another attempt to remove remove_cv_t from iterators

Some of the return types were const (non-reference) types because of the
pecularities of decltype: `decltype((*it).first)` is the *declared* type
of the member of the pair, rather than the type of the expression. So if
the reference type of the iterator is `pair<const int, int> &`, then the
decltype is `const int`. Wrapping an extra set of parentheses to form
`decltype(((*it).first))` would instead give `const int &`.

This means that the existing make_key_iterator actually returns by value
from `__next__`, rather than by reference. Since for mapping types, keys
are always const, this probably hasn't been noticed, but it will affect
make_value_iterator if the Python code tries to mutate the returned
objects. I've changed things to use double parentheses so that
make_iterator, make_key_iterator and make_value_iterator should now all
return the reference type of the iterator. I'll still need to add a test
for that; for now I'm just checking whether I can keep Clang-Tidy happy.

* Add back some NOLINTNEXTLINE to appease Clang-Tidy

This is favoured over using remove_cv_t because in some cases a const
value return type is deliberate (particularly for Eigen).

* Add a unit test for iterator referencing

Ensure that make_iterator, make_key_iterator and make_value_iterator
return references to the container elements, rather than copies. The
test for make_key_iterator fails to compile on master, which gives me
confidence that this branch has fixed it.

* Make the iterator_access etc operator() const

I'm actually a little surprised it compiled at all given that the
operator() is called on a temporary, but I don't claim to fully
understand all the different value types in C++11.

* Attempt to work around compiler bugs

https://godbolt.org/ shows an example where ICC gets the wrong result
for a decltype used as the default for a template argument, and CI also
showed problems with PGI. This is a shot in the dark to see if it fixes
things.

* Make a test constructor explicit (Clang-Tidy)

* Fix unit test on GCC 4.8.5

It seems to require the arguments to the std::pair constructor to be
implicitly convertible to the types in the pair, rather than just
requiring is_constructible.

* Remove DOXYGEN_SHOULD_SKIP_THIS guards

Now that a complex decltype expression has been replaced by a simpler
nested type, I'm hoping Doxygen will be able to build it without issues.

* Add comment to explain iterator_state template params

* fix: regression in #3271

Co-authored-by: Bruce Merry <1963944+bmerry@users.noreply.github.com>
2021-09-23 15:06:07 -04:00
Henry Schreiner 2fa3fcfda5 Revert "Add make_value_iterator (#3271)"
This reverts commit ee0c5ee405.
2021-09-22 23:10:03 -04:00
Aaron Gokaslan 0fb981b219
Add blacken-docs and pycln pre-commit hooks (#3292)
* Apply blacken-docs and fix language-hints

* Add blacken-docs pre-commit hook

* Add pycln pre-commit hook

* Enable a few builtin hooks

* Black no longer ignores pyi files
2021-09-22 15:38:50 -04:00
Bruce Merry ee0c5ee405
Add make_value_iterator (#3271)
* Add make_value_iterator

This is the counterpart to make_key_iterator, and will allow
implementing a `value` method in `bind_map` (although doing so is left
for a subsequent PR).

I made a few design changes to reduce copy-and-paste boilerplate.
Previously detail::iterator_state had a boolean template parameter to
indicate whether it was being used for make_iterator or
make_key_iterator. I replaced the boolean with a class that determines
how to dereference the iterator. This allows for a generic
implementation of `__next__`.

I also added the ValueType and Extra... parameters to the iterator_state
template args, because I think it was a bug that they were missing: if
make_iterator is called twice with different values of these, only the
first set has effect (because the state class is only registered once).
There is still a potential issue in that the *values* of the extra
arguments are latched on the first call, but since most policies are
empty classes this should be even less common.

* Add some remove_cv_t to appease clang-tidy

* Make iterator_access and friends take reference

For some reason I'd accidentally made it take a const value, which
caused some issues with third-party packages.

* Another attempt to remove remove_cv_t from iterators

Some of the return types were const (non-reference) types because of the
pecularities of decltype: `decltype((*it).first)` is the *declared* type
of the member of the pair, rather than the type of the expression. So if
the reference type of the iterator is `pair<const int, int> &`, then the
decltype is `const int`. Wrapping an extra set of parentheses to form
`decltype(((*it).first))` would instead give `const int &`.

This means that the existing make_key_iterator actually returns by value
from `__next__`, rather than by reference. Since for mapping types, keys
are always const, this probably hasn't been noticed, but it will affect
make_value_iterator if the Python code tries to mutate the returned
objects. I've changed things to use double parentheses so that
make_iterator, make_key_iterator and make_value_iterator should now all
return the reference type of the iterator. I'll still need to add a test
for that; for now I'm just checking whether I can keep Clang-Tidy happy.

* Add back some NOLINTNEXTLINE to appease Clang-Tidy

This is favoured over using remove_cv_t because in some cases a const
value return type is deliberate (particularly for Eigen).

* Add a unit test for iterator referencing

Ensure that make_iterator, make_key_iterator and make_value_iterator
return references to the container elements, rather than copies. The
test for make_key_iterator fails to compile on master, which gives me
confidence that this branch has fixed it.

* Make the iterator_access etc operator() const

I'm actually a little surprised it compiled at all given that the
operator() is called on a temporary, but I don't claim to fully
understand all the different value types in C++11.

* Attempt to work around compiler bugs

https://godbolt.org/ shows an example where ICC gets the wrong result
for a decltype used as the default for a template argument, and CI also
showed problems with PGI. This is a shot in the dark to see if it fixes
things.

* Make a test constructor explicit (Clang-Tidy)

* Fix unit test on GCC 4.8.5

It seems to require the arguments to the std::pair constructor to be
implicitly convertible to the types in the pair, rather than just
requiring is_constructible.

* Remove DOXYGEN_SHOULD_SKIP_THIS guards

Now that a complex decltype expression has been replaced by a simpler
nested type, I'm hoping Doxygen will be able to build it without issues.

* Add comment to explain iterator_state template params
2021-09-21 13:37:19 -04:00
Henry Schreiner 04dd3262f0
docs: update CHANGELOG (#3276) 2021-09-17 17:28:26 -04:00
Thomas Ballinger 39a0aac88e
docs fix to avoid nonexistent SmartCompile (#3241) 2021-09-08 14:00:00 -04:00
Jouke Witteveen 031a700dfd
Add make_simple_namespace function and tests (#2840)
Co-authored-by: Jouke Witteveen <j.witteveen@cosine.nl>
2021-08-26 08:04:22 -07:00
Ralf W. Grosse-Kunstleve c8ce4b8df8
Clone of @virtuald's PR #2112 with minor enhancements. (#3215)
* Add py::raise_from to enable chaining exceptions on Python 3.3+

* Use 'raise from' in initialization

* Documenting the exact base version of _PyErr_FormatVFromCause, adding back `assert`s.

Co-authored-by: Dustin Spicuzza <dustin@virtualroadside.com>
2021-08-23 17:30:01 -07:00
Aaron Gokaslan 9df2f1ff13
maint(precommit): Apply isort (#3195)
* Apply isort

* Tweak isort config

* Add env.py as a known_first_party

* Add one missing known first party

* Make config compat with older isort versions

* Add another comment

* Revert pyproject setting
2021-08-13 12:37:05 -04:00
Henry Schreiner a2b78a8c27
chore: changelog update (#3163)
* chore: changelog update

* Update docs/changelog.rst
2021-08-03 13:16:14 -04:00
jesse-sony d65edfb024
Feature/local exception translator (#2650)
* Create a module_internals struct

Since we now have two things that are going to be module local, it felt
correct to add a struct to manage them.

* Add local exception translators

These are added via the  register_local_exception_translator function
and are then applied before the global translators

* Add unit tests to show the local exception translator works

* Fix a bug in the unit test with the string value of KeyError

* Fix a formatting issue

* Rename registered_local_types_cpp()

Rename it to get_registered_local_types_cpp() to disambiguate from the
new member of module_internals

* Add additional comments to new local exception code path

* Add a register_local_exception function

* Add additional unit tests for register_local_exception

* Use get_local_internals like get_internals

* Update documentation for new local exception feature

* Add back a missing space

* Clean-up some issues in the docs

* Remove the code duplication when translating exceptions

Separated out the exception processing into a standalone function in the
details namespace.

Clean-up some comments as per PR notes as well

* Remove the code duplication in register_exception

* Cleanup some formatting things caught by clang-format

* Remove the templates from exception translators

But I added a using declaration to alias the type.

* Remove the extra local from local_internals variable names

* Add an extra explanatory comment to local_internals

* Fix a typo in the code
2021-07-21 05:22:18 -07:00
Henry Schreiner 74935f8d67
chore: post-release (#3128) 2021-07-17 11:50:42 -04:00
Henry Schreiner 65e95ea867
chore: bump to 2.7.0 (#3123) 2021-07-16 09:27:47 -04:00
Henry Schreiner 6642f389dc
docs: update changelog (#3122) 2021-07-15 20:00:07 -04:00
Axel Huebl 55f6f6e9bf
Fix: RTD Docutils Build (#3119)
The docutils 0.17+ release uses more semantic HTML5 tags, which
the RTD theme does not (yet) know.

This breaks side bar, lists and other elements.
2021-07-15 15:41:36 -04:00
Henry Schreiner 31843d455d
docs: reduce visibility of 3.9.0 warning (#3105) 2021-07-15 15:01:13 -04:00
Henry Schreiner cd061aeef1
style: pre-commit cleanup (#3111)
* style: disallow PyTest (should be pytest)

* style: cleanup spell checking a bit

* style: add a few items to the .gitignore
2021-07-14 16:49:13 -04:00
Antony Lee 1be0a0a610
Add helper to build in-tree extensions. (#2831)
For single-file extensions, a convenient pattern offered by cython
is to place the source files directly in the python source tree
(`foo/__init__.py`, `foo/ext.pyx`), deriving the package names from
their filesystem location.  Adapt this pattern for pybind11, using an
`intree_extensions` helper, which should be thought of as the moral
equivalent to `cythonize`.

Differences with cythonize: I chose not to include globbing support
(`intree_extensions(glob.glob("**/*.cpp"))` seems sufficient), nor to
provide extension-customization kwargs (directly setting the attributes
on the resulting Pybind11Extension objects seems sufficient).

We could choose to have `intree_extension` (singular instead) and make
users write `[*map(intree_extension, glob.glob("**/*.cpp"))]`; no strong
opinion here.

Co-authored-by: Aaron Gokaslan <skylion.aaron@gmail.com>
2021-07-13 17:21:55 -04:00
Henry Schreiner 6a644c8f04
docs: update changelog (#3099)
* docs: update changelog

* docs: add one more and merge tidy
2021-07-13 00:08:29 -04:00
Ralf W. Grosse-Kunstleve 75090647ce
More precise return_value_policy::automatic documentation. (#2920)
* Adding test_return_vector_bool_raw_ptr to test_stl.py.

* First attempt to make the documentation more accurate, but not trying to be comprehensive, to not bloat the reference table with too many details.

* Fixing minor oversights.

* Applying reviewer suggestion.
2021-07-12 16:56:10 -07:00
Henry Schreiner f0a65c899c
docs(fix): spelling mistake in recent commit 2021-07-12 16:57:28 -04:00
Ralf W. Grosse-Kunstleve 7472d37a93
Adding iostream.h thread-safety documentation. (#2995)
* Adding iostream.h thread-safety documentation.

* Restoring `TestThread` code with added `std::lock_guard<std::mutex>`.

* Updating new comments to reflect new information.

* Fixing up `git rebase -X theirs` accidents.
2021-07-12 13:39:06 -07:00
Jan Iwaszkiewicz cf006af2f0
Fix typos and docs style (#3088)
* py::pickle typo

* correct dots and parentheses
2021-07-10 11:16:07 -07:00
luzpaz 8bee61b645
docs: fix various typos (#3075)
Found via `codespell -q 3 -L nd,ot,thist`
2021-07-04 19:58:35 -04:00
Antony Lee 5bcaaa0423
Add a std::filesystem::path <-> os.PathLike caster. (#2730) 2021-07-02 07:00:50 -07:00
Aaron Gokaslan b4b67f026b
Fix typos (#3044) 2021-06-17 13:39:59 -07:00
Aaron Gokaslan cd4b49a2c8
Update py::kwargs examples to pass by reference (#3038) 2021-06-17 13:20:17 -07:00
Yichen 3ac690b88b
Explicitly export exception types. (#2999)
* Set visibility of exceptions to default.

Co-authored-by: XZiar <czktc2007@gmail.com>

* add test

* update docs

* Skip failed test.
2021-05-27 08:00:18 -07:00
Eric Cousineau b6ec0e950c
functions: Add doc on incorrect argument index (#2979)
test_call_policies: Explicitly check free-functions and static methods
2021-05-06 10:13:30 -04:00
Henry Schreiner 114be7f4ad
docs: remove recommonmark (#2955) 2021-04-15 18:27:16 -04:00
JYX 3df0ee6fe3
docs: typo in classes.rst (#2926) 2021-04-02 11:46:43 -04:00
Tom de Geus 9c0aa69937
Pointing out namespace in docs (#2874) 2021-02-25 07:25:50 -08:00
jakobjw 98f9a33c62
Correct typo in FAQ (#2868) 2021-02-20 23:28:27 +01:00
Ralf W. Grosse-Kunstleve 0432ae7c52
Changing pybind11::str to exclusively hold PyUnicodeObject (#2409)
* Changing pybind11::str to exclusively hold PyUnicodeObject
2021-01-29 09:41:42 -08:00
Yannick Jadoul 587d5f840a
Update breathe to 4.26.1, add make_tuple, make_iterator, and make_key_iterator (#2828) 2021-01-28 12:28:16 +01:00
Henry Fredrick Schreiner 4a5b81b1b7 chore: get back to work 2021-01-26 22:28:09 -05:00
Henry Schreiner 8de7772cc7
chore: prepare for the 2.6.2 release (#2821) 2021-01-26 21:26:45 -05:00
Henry Schreiner 8e5d3d234e
docs: prepare for 2.6.2 (#2820)
* docs: prepare for 2.6.2

* chore: pre-commit autoupdate
2021-01-25 16:09:36 -05:00
Henry Schreiner 08bca374fd
docs: update changelog, nicer output for script (#2811) 2021-01-21 11:34:39 -05:00
Henry Schreiner 0df11d857c
docs: update build description slightly (#2794)
* docs: update build description slightly

* docs: missing ticks
2021-01-16 20:12:19 -05:00
Yannick Jadoul 1faf4a8ae4
docs: the order of alternatives for variant types matters, and follows the same rules as overload resolution (#2784) 2021-01-13 23:17:27 -05:00
Andy Maloney 14b375123c
docs: fix example code in Exceptions section (match vs. matches) (#2781) 2021-01-13 23:14:45 -05:00
Henry Schreiner b7dfe5cc84
chore: changelog update (#2750) 2020-12-28 20:14:54 -05:00
Steve Siano 6f66e7603c
docs: add a note about compiling the example (#2737)
* docs: mention PYTHONPATH in installing.rst

When pybind11 is included as a submodule, the user needs to update their
Python module search path.  Otherwise, the first c++ compilation command
in docs/basics.rst will fail.

* docs: add a note about compiling the example

This note shows how to modify the compilation command for the example
when the pybind11 source has been included as a Git submodule.

* docs: add a note about compiling the example

Added an internal link to the docs

* docs: updated a note about compiling the example

Also updated the command substitution syntax for consistency
2020-12-24 09:51:36 -05:00
Henry Schreiner 79b0e2c052
docs: fix pdf build, simpler start page (#2736) 2020-12-22 08:50:45 -05:00
Henry Schreiner 5bd766bf6c
docs: update changelog and add script to help generate it (#2733) 2020-12-21 21:04:33 -05:00
Antony Lee d068ab286a
docs: pybind11/numpy.h does not require numpy at build time. (#2720)
This is nice enough to be mentioned explicitly in the docs.
2020-12-08 18:07:36 -05:00
Yannick Jadoul 028812ae7e
docs: add warning about FindPython's Development component when libraries don't exist (e.g. on manylinux) (#2689)
* Add waring about FindPython's Development component when libraries don't exist (e.g. on manylinux)

* Minor wording update (thanks, @henryiii!)
2020-11-24 13:37:51 -05:00
James Foster d57c1fab7f
docs: update installing.rst (#2691)
`git submodule add` needs the branch before the repository or else it is ignored. The previous code checked out the `master` branch, not the `stable` branch.
2020-11-24 12:08:33 -05:00