Commit Graph

826 Commits

Author SHA1 Message Date
Ralf W. Grosse-Kunstleve
4070a64f86 Merge branch 'master' into sh_merge_master 2022-02-03 15:07:04 -08:00
Michał Górny
1d3b04e805
test: Strip whitespace when comparing numpy dtypes for 1.22 compat (#3682)
* test: Strip whitespace when comparing numpy dtypes for 1.22 compat

Strip whitespace when comparing numpy dtype str() in order to preserve
test compatibility with both numpy 1.22 and older versions whose output
differ by whitespace.

Fixes #3680

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
2022-02-03 12:29:46 -05:00
Aaron Gokaslan
3a8d92308d
Fix caster optimization regression introduced in #3650 (#3659)
* Fix optimization bug introduced in #3650

* Add simple Python extension test for MVF

* Improve comments

* Clarify comment

* Clarify another comment

* Add test docstring

* Fix typo
2022-01-31 12:19:48 -05:00
Ralf W. Grosse-Kunstleve
aa4fe8e4ff Merge branch 'master' into sh_merge_master 2022-01-26 19:08:58 -08:00
Dustin Spicuzza
ec81e8e778
Propagate py::multiple_inheritance to all children (#3650)
* Add tests demonstrating smart_holder issues with multiple inheritance

* Propagate C++ multiple inheritance markers to all children

- Makes py::multiple_inheritance only needed in base classes hidden from pybind11
2022-01-26 17:03:52 -08:00
Xiaofei Wang
93a376a40a
[smart_holder] Add missing test case to CMakeLists.txt (#3645)
* Add missing test case to CMakeLists.txt

* Fix warnings

* Fix Clangtidy
2022-01-25 16:43:11 -08:00
Xiaofei Wang
da058a2904
[Smart holder] Support void pointer capsules (#3633)
* Make smart holder type casters support void pointer capsules.

* Fix warnings

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Fix checks

* Fix check failures under CentOS

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Remove unused regex module

* Resolve comments

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Resolve comments

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Fix clangtidy

* Resolve comments

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
2022-01-25 13:29:54 -08:00
Ralf W. Grosse-Kunstleve
152bb100f2 Merge branch 'master' into sh_merge_master 2022-01-16 07:07:12 -08:00
Sergei Izmailov
5194855900
Render py::bool_ and py::float_ without _ in docstrings (#3622)
* Render `py::bool_` as `bool` in docstrings

* Render `py::float_` as `float` in docstrings
2022-01-16 07:05:46 -08:00
Ralf W. Grosse-Kunstleve
c8d36b3c99 Merge branch 'master' into sh_merge_master 2022-01-14 23:17:11 -08:00
Aaron Gokaslan
d2ec836712
Add support for nested C++11 exceptions (#3608)
* Add support for nested C++11 exceptions

* Remove wrong include

* Fix if directive

* Fix missing skipif

* Simplify code and try to work around MSVC bug

* Clarify comment

* Further simplify code

* Remove the last extra throw statement

* Qualify auto

* Fix typo

* Add missing return for consistency

* Fix clang-tidy complaint

* Fix python2 stub

* Make clang-tidy happy

* Fix compile error

* Fix python2 function signature

* Extract C++20 utility and backport

* Cleanup code a bit more

* Improve test case

* Consolidate code and fix signature

* Fix typo
2022-01-14 14:22:47 -05:00
Ralf W. Grosse-Kunstleve
aa209cc90f Adding back test_class_sh, test_classh additions, building on PR #3590. 2022-01-11 17:48:59 -08:00
Ralf W. Grosse-Kunstleve
ea0c1e77c8 Merge branch 'master' into sh_merge_master 2022-01-11 17:48:37 -08:00
Ralf W. Grosse-Kunstleve
ea4cc147a2 Temporarily backing out all test_class_sh, test_classh additions. 2022-01-11 17:48:05 -08:00
Aaron Gokaslan
f8d4aa47b6
Add clang-tidy readability checks for sus args (#3611) 2022-01-11 17:34:16 -08:00
Ivor Wanders
21911e126f
A way to register additional test targets and support .py only tests. (#3590)
* A way to register additional test targets.

* Support specifying tests with extension.

* Ensure TEST_OVERRIDE is backwards compatible.

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Ensure regex is non greedy.

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
2022-01-11 17:31:19 -08:00
Ralf W. Grosse-Kunstleve
f588810871
Replacing ICC C++14 with C++17 (#3570)
* Replacing ICC C++14 with C++17

* ICPC: -diag-disable:conversion

Try to suppress the `-Werror-all` promotion in `#include <variant>`

* Trying `-DPYBIND11_WERROR=OFF`

* Trying `-Wno-conversion` (todo: make specific to C++17 if it works)

* Inserting -Wno-conversion only for C++17

Co-authored-by: Axel Huebl <axel.huebl@plasma.ninja>
2022-01-11 12:13:02 -08:00
Aaron Gokaslan
ef070f7750
Add additional info to TypeError when C++->Python casting fails (#3605)
* Add additional info to TypeInfo when C++->Python casting fails

* Fix typo

* Address reviewer comments
2022-01-10 21:18:00 -05:00
Dustin Spicuzza
9e43db614f
fix: names of types held by smart holder should be the actual type (#3588)
Required for pybind11-stubgen to work properly
2022-01-03 15:46:13 -08:00
Ralf W. Grosse-Kunstleve
388c03b18c Merge branch 'master' into sh_merge_master_clean 2021-12-29 13:24:20 -08:00
Ralf W. Grosse-Kunstleve
1bbaeb3462
Adding dedicated test_const_name. (#3578)
* Adding dedicated test_const_name.

Also exercises pybind11::detail::_ backward compatibility.

See also: PR #3423

* Backing out tests involving int_to_str (requires C++17 or higher).

* Suppressing clang-tidy errors.

* Disabling test_const_name for MSVC 2015 due to bizarre failures.

* Stacking @pytest.mark.parametrize (thanks to @skylion007 for pointing out).
2021-12-29 12:54:25 -08:00
Henry Schreiner
39fbc7992b
fix: avoiding usage of _ if already defined (#3423)
* fix: avoid usage of _

* ci: test _ defined

* docs: include change in docs

* fix: add a test and comment

* refactor: const_str -> const_name
2021-12-21 14:24:21 -05:00
Ralf W. Grosse-Kunstleve
d13e03af41 Merge branch 'master' into smart_holder 2021-12-03 10:07:57 -08:00
Jason Rhinelander
b4939fcbfb
Expand std::string_view support to str, bytes, memoryview (#3521)
* Expand string_view support to str, bytes, memoryview

1. Allows constructing a str or bytes implicitly from a string_view;
   this is essentially a small shortcut allowing a caller to write
   `py::bytes{sv}` rather than `py::bytes{sv.data(), sv.size()}`.

2. Allows implicit conversion *to* string_view from py::bytes -- this
   saves a fair bit more as currently there is no simple way to get such
   a view of the bytes without copying it (or resorting to Python API
   calls).

   (This is not done for `str` because when the str contains unicode we
   have to allocate to a temporary and so there might not be some string
   data we can properly view without owning.)

3. Allows `memoryview::from_memory` to accept a string_view.  As with
   the other from_memory calls, it's entirely your responsibility to
   keep it alive.

This also required moving the string_view availability detection into
detail/common.h because this PR needs it in pytypes.h, which is higher
up the include chain than cast.h where it was being detected currently.

* Move string_view include to pytypes.h

* CI-testing a fix for the "ambiguous conversion" issue.

This change is known to fix the `tensorflow::tstring` issue reported under https://github.com/pybind/pybind11/pull/3521#issuecomment-985100965

TODO: Minimal reproducer for the `tensorflow::tstring` issue.

* Make clang-tidy happy (hopefully).

* Adding minimal reproducer for the `tensorflow::tstring` issue.

Error without the enable_if trick:

```
/usr/local/google/home/rwgk/forked/pybind11/tests/test_builtin_casters.cpp:169:16: error: ambiguous conversion for functional-style cast from 'TypeWithBothOperatorStringAndStringView' to 'py::bytes'
        return py::bytes(TypeWithBothOperatorStringAndStringView());
               ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/local/google/home/rwgk/forked/pybind11/include/pybind11/detail/../pytypes.h:1174:5: note: candidate constructor
    bytes(const std::string &s) : bytes(s.data(), s.size()) { }
    ^
/usr/local/google/home/rwgk/forked/pybind11/include/pybind11/detail/../pytypes.h:1191:5: note: candidate constructor
    bytes(std::string_view s) : bytes(s.data(), s.size()) { }
    ^
```

* Adding missing NOLINTNEXTLINE

* Also apply ambiguous conversion workaround to str()

Co-authored-by: Ralf W. Grosse-Kunstleve <rwgk@google.com>
2021-12-03 13:20:32 -04:00
Ralf W. Grosse-Kunstleve
98bdbba999 Merge branch 'master' into smart_holder 2021-11-22 01:35:01 -08:00
Ralf W. Grosse-Kunstleve
9281faf429
Fixing stict vs strict typo. (#3493) 2021-11-22 01:33:03 -08:00
Ralf W. Grosse-Kunstleve
7bbc5e3cdd Merge branch 'master' into smart_holder 2021-11-20 16:03:36 -08:00
Jason Rhinelander
673b4be3d7
Fix py::kw_only when used before the first arg of a method (#3488)
* Fix py::kw_only when used before the first arg of a method

The implicit space for the `self` argument isn't added until we hit the
first argument, but this wasn't being done for kw_only or pos_only, and
so a kw_only before the first argument would break.

This fixes it by properly checking whether we need to add the self arg.

(The pos_only issue here was extremely mild -- you didn't get the `/` in
the docstring, but AFAICT it has no other effect since there are no
meaningful arguments before it anyway).

* Style changes

- rename check_have_self_arg -> append_self_arg_if_needed

- move the argument name inline comments before the args instead of
  after
2021-11-20 16:01:57 -08:00
Ralf W. Grosse-Kunstleve
e57afe23f7 Merge branch 'master' into smart_holder 2021-11-20 09:35:40 -08:00
Henry Schreiner
15f8d7c12e
fix(build): cleaner CMake printouts & IDE folders (#3479) 2021-11-18 10:01:24 -05:00
Henry Schreiner
72282f75a1
ci: support development releases of Python (#3419)
* ci: support development releases of Python

* fix: better PyPy support

* fix: patch over a few more pypy issues

* Try to patch

* Properly follow pep667

* Fix typo

* Whoops, 667 not in yet

* For testing

* More testing

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Try to backport

* Try to simplify fix

* Nail down the fix

* Try pypy workaround

* Typo

* one last typo

* Replacing 0x03110000 with 0x030B0000

* Add TODO. Drop PyPy

* Fix typo

* Revert catch upgrade

* fix: minor cleanup, try pypy again

Co-authored-by: Aaron Gokaslan <skylion.aaron@gmail.com>
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>
2021-11-17 09:44:19 -05:00
Ralf W. Grosse-Kunstleve
8a7643f471 Using PYBIND11_SH_DEF for test_override_cache_helper introduced by PR #3465. 2021-11-15 14:59:46 -08:00
Ralf W. Grosse-Kunstleve
f4fab85d87 Merge branch 'master' into sh_merge_master 2021-11-15 14:43:08 -08:00
Trigve
afdc09deda
[master] Wrong caching of overrides (#3465)
* override: Fix wrong caching of the overrides

There was a problem when the python type, which was stored in override
cache for C++ functions, was destroyed and  the record wasn't removed from the
override cache. Therefor, dangling pointer was stored there. Then when the
memory was reused and new type was allocated at the given address and the
method with the same name (as previously stored in the cache) was actually
overridden in python, it would wrongly find it in the override cache for C++
functions and therefor override from python wouldn't be called.
The fix is to erase the type from the override cache when the type is destroyed.

* test: Pass by const ref instead of by value (clang-tidy)

* test: Rename classes and move to different files

Rename the classes and files so they're no too generic. Also, better place to
test the stuff is in test_virtual_functions.cpp/.py as we're basically testing
the virtual functions/trampolines.

* Add TODO for erasure code

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
2021-11-15 13:36:41 -05:00
Antony Lee
b11ff912a6
fix(setup =_helpers): don't add -g0 CFLAGS sets -g (#3436)
On Unix, setuptools prepends $CFLAGS and $CPPFLAGS to the compiler flags
(they always come before extra_compile_args and anything else; see
distutils.sysconfig.customize_compiler).  In practice, the environment
variables are useful e.g. to quickly generate a debug build (e.g. by
setting CFLAGS=-g), but Pybind11Extension currently unconditionally
overwrites this with -g0.

Instead, check the environment variables and only insert -g0 if not
overridden by them.
2021-11-08 16:27:32 -05:00
Ralf W. Grosse-Kunstleve
58c7f076bc Merge branch 'master' into sh_merge_master 2021-11-07 16:26:55 -08:00
Boris Rasin
01f938e799
fix: add missing std::forward calls (#3443)
* fix: add missing std::forward calls

Two of the four cpp_function overloads are missing std::forward calls, which seems like a simple oversight.

* add test for https://github.com/pybind/pybind11/pull/3443

* add py tests

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* fix test

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
2021-11-07 18:35:25 -05:00
Jason Rhinelander
e7c9753f1d
feat: allow kw-only args after a py::args (#3402)
* Simply has_kw_only_args handling

This simplifies tracking the number of kw-only args by instead tracking
the number of positional arguments (which is really what we care about
everywhere this is used).

* Allow keyword-only arguments to follow py::args

This removes the constraint that py::args has to be last (or
second-last, with py::kwargs) and instead makes py::args imply
py::kw_only for any remaining arguments, allowing you to bind a function
that works the same way as a Python function such as:

    def f(a, *args, b):
        return a * b + sum(args)

    f(10, 1, 2, 3, b=20)  # == 206

With this change, you can bind such a function using:

    m.def("f", [](int a, py::args args, int b) { /* ... */ },
        "a"_a, "b"_a);

Or, to be more explicit about the keyword-only arguments:

    m.def("g", [](int a, py::args args, int b) { /* ... */ },
        "a"_a, py::kw_only{}, "b"_a);

(The only difference between the two is that the latter will fail at
binding time if the `kw_only{}` doesn't match the `py::args` position).

This doesn't affect backwards compatibility at all because, currently,
you can't have a py::args anywhere except the end/2nd-last.

* Take args/kwargs by const lvalue ref

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

Co-authored-by: Henry Schreiner <HenrySchreinerIII@gmail.com>
2021-10-28 23:16:55 -04:00
Ralf W. Grosse-Kunstleve
4958fd9aa1 Merge branch 'master' into sh_merge_master 2021-10-26 12:10:34 -07:00
Henry Schreiner
e7e2c79f3f
fix: improve support for Python 3.11-dev (#3368)
* ci: support Python 3.11-dev

Also update 3.10 to final, better PyPy usage

* fix: use PyFrame_GetCode on Python 3.9+

* ci: some bitiness of pypy not supported on win

* chore: update CMake support to 3.22rc1 to quiet warning

* fix: use dev version of py to fix Py 3.11

* tests: print proper Eigen version

* ci: include pypy2, not sure why

* ci: avoid running on Python 3.11 for now

* ci: fix runs

* ci: simpler PyPy usage, drop unmaintained scipy + pypy index

* ci: only binary numpy, wait on pypy 3.8

* refactor: address review
2021-10-26 14:50:34 -04:00
Ryan Cahoon
c2d3e220bd
fix: the types for return_value_policy_override in optional_caster (#3376)
* fix: the types for return_value_policy_override in optional_caster

`return_value_policy_override` was not being applied correctly in
`optional_caster` in two ways:
- The `is_lvalue_reference` condition referenced `T`, which was the
`optional<T>` type parameter from the class, when it should have used `T_`,
which was the parameter to the `cast` function. `T_` can potentially be a
reference type, but `T` will never be.
- The type parameter passed to `return_value_policy_override` should be
`T::value_type`, not `T`. This matches the way that the other STL container
type casters work.

The result of these issues was that a method/property definition which used a
`reference` or `reference_internal` return value policy would create a Python
value that's bound by reference to a temporary C++ object, resulting in
undefined behavior. For reasons that I was not able to figure out fully, it
seems like this causes problems when using old versions of `boost::optional`,
but not with recent versions of `boost::optional` or the `libstdc++`
implementation of `std::optional`. The issue (that the override to
`return_value_policy::move` is never being applied) is present for all
implementations, it just seems like that somehow doesn't result in problems for
the some implementation of `optional`. This change includes a regression type
with a custom optional-like type which was able to reproduce the issue.

Part of the issue with using the wrong types may have stemmed from the type
variables `T` and `T_` having very similar names. This also changes the type
variables in `optional_caster` to use slightly more descriptive names, which
also more closely follow the naming convention used by the other STL casters.

Fixes #3330

* Fix clang-tidy complaints

* Add missing NOLINT

* Apply a couple more fixes

* fix: support GCC 4.8

* tests: avoid warning about unknown compiler for compilers missing C++17

* Remove unneeded test module attribute

* Change test enum to have more unique int values

Co-authored-by: Aaron Gokaslan <skylion.aaron@gmail.com>
Co-authored-by: Henry Schreiner <HenrySchreinerIII@gmail.com>
2021-10-25 19:04:45 -07:00
Dmitry Yershov
076c89fc54
tests: test recursive dispatch using visitor pattern (#3365) 2021-10-22 17:09:15 -04:00
Henry Schreiner
f791dc8648
fix: deprecate make_simple_namespace, fix Python 3.11 (#3374)
* fix: deprecate make_simple_namespace, fix Python 3.11

* docs: update links
2021-10-19 14:39:29 -04:00
Ralf W. Grosse-Kunstleve
cce23af383 Fixes based on newly added pre-submit checks. 2021-10-11 13:22:39 -07:00
Ralf W. Grosse-Kunstleve
fd68dede10 Merge branch 'master' into sh_merge_master 2021-10-11 13:18:33 -07:00
Ralf W. Grosse-Kunstleve
7c580586f8
Correct options on Eigen::MappedSparseMatrix & adding MSVC C4127 suppression around Eigen includes. (#3352)
* Adding MSVC C4127 suppression around Eigen includes.

* For MSVC 2015 only: also adding the C4127 suppression to test_eigen.cpp

* Copying original change from PR #3343, with extra line breaks to not run past 99 columns (our desired but currently not enforced limit).
2021-10-11 13:13:01 -07:00
Bruce Merry
8a7c266d26
Fix make_key_iterator/make_value_iterator for prvalue iterators (#3348)
* Add a test showing a flaw in make_key_iterator/make_value_iterator

If the iterator dereference operator returns a value rather than a
reference (and that pair also does not *contain* references),
make_key_iterator and make_value_iterator will return a reference to a
temporary, causing a segfault.

* Fix make_key_iterator/make_value_iterator for prvalue iterators

If an iterator returns a pair<T1, T2> rather than a reference to a pair
or a pair of references, make_key_iterator and make_value_iterator would
return a reference to a temporary, typically leading to a segfault. This
is because the value category of member access to a prvalue is an
xvalue, not a prvalue, so decltype produces an rvalue reference type.
Fix the type calculation to handle this case.

I also removed some decltype parentheses that weren't needed, either
because the expression isn't one of the special cases for decltype or
because decltype was only used for SFINAE. Hopefully that makes the code
a bit more readable.

Closes #3347

* Attempt a workaround for nvcc
2021-10-11 08:35:39 -07:00
Aaron Gokaslan
f4c81e0877
maint: Add additional linter-related pre-commit hooks (#3337)
* Add additional pygrep pre-commit hooks

* Remove useless noqas with hook

* Fix all single rst backticks

* Simplify mypy pre-commit hook with upstream fixes

* Add back missing comment

* Add one last pygrep hook
2021-10-08 08:38:04 -04:00
Ralf W. Grosse-Kunstleve
5a90dae7fe Merge branch 'master' into smart_holder 2021-10-06 00:47:43 -07:00
Henry Schreiner
d7a7edc12b tests: support Eigen configuration 2021-10-04 10:06:12 -07:00