Commit Graph

595 Commits

Author SHA1 Message Date
David Vo 5e6ec49652
Add enum value to enum repr (#2126)
This changes enum reprs to look like `<Enum.name: value>` similarly to
the Python enum module.

This keeps the str of enums as `Enum.name`, like the Python enum module.
2020-09-20 01:12:19 +02:00
Henry Schreiner d0ed035cc5
fix: AppleClang 12 warnings (#2510)
* fix: AppleClang 12 new warning

* Fix: AppleClang X.X.0 will not trigger this warning
2020-09-19 20:23:47 +02:00
Henry Schreiner 81555ce61f
docs: Use README.rst in docs as home page (#2500) 2020-09-17 15:40:09 -04:00
Eric Cousineau 4e7c08daee
test_numpy_dtypes: Add test for py::vectorize() (#2260) 2020-09-17 07:19:33 -04:00
Henry Fredrick Schreiner e3774b76ed tests: the condition was too look on xfail 2020-09-16 23:21:47 -04:00
Henry Fredrick Schreiner 11f756f5a9 fix: type bug intruduced in #2492
This now tests the old form too, and fixes the bug introduced.
2020-09-16 23:21:47 -04:00
Henry Schreiner fd61f5038e
feat: setup.py redesign and helpers (#2433)
* feat: setup.py redesign and helpers

* refactor: simpler design with two outputs

* refactor: helper file update and Windows support

* fix: review points from @YannickJadoul

* refactor: fixes to naming and more docs

* feat: more customization points

* feat: add entry point pybind11-config

* refactor: Try Extension-focused method

* refactor: rename alt/inplace to global

* fix: allow usage with git modules, better docs

* feat: global as an extra (@YannickJadoul's suggestion)

* feat: single version location

* fix: remove the requirement that setuptools must be imported first

* fix: some review points from @wjacob

* fix: use .in, add procedure to docs

* refactor: avoid monkeypatch copy

* docs: minor typos corrected

* fix: minor points from @YannickJadoul

* fix: typo on Windows C++ mode

* fix: MSVC 15 update 3+ have c++14 flag

See <https://docs.microsoft.com/en-us/cpp/build/reference/std-specify-language-standard-version?view=vs-2019>

* docs: discuss making SDists by hand

* ci: use pep517.build instead of manual setup.py

* refactor: more comments from @YannickJadoul

* docs: updates from @ktbarrett

* fix: change to newly recommended tool instead of pep517.build

This was intended as a proof of concept; build seems to be the correct replacement.

See https://github.com/pypa/pep517/pull/83

* docs: updates from @wjakob

* refactor: dual version locations

* docs: typo spotted by @wjakob
2020-09-16 17:13:41 -04:00
Henry Schreiner dabbbf315d
fix: use OVERRIDE instead of OVERLOAD (#2490)
* fix: use OVERRIDE instead of OVERLOAD

* docs: more accurate statement
2020-09-15 12:10:31 -04:00
Henry Schreiner 4d78640830 style: clang-tidy: modernize-use-emplace 2020-09-15 09:56:59 -04:00
Henry Schreiner ce88e940ce style: clang-tidy: modernize-use-auto 2020-09-15 09:56:59 -04:00
Henry Schreiner b491b465c7 style: clang-tidy: modernize-use-equals-default 2020-09-15 09:56:59 -04:00
Henry Schreiner b342c37388 style: clang-tidy: modernize-use-using 2020-09-15 09:56:59 -04:00
Henry Schreiner 5dfbe6f903 style: clang-tidy: modernize-use-override 2020-09-15 09:56:59 -04:00
Henry Schreiner 8dc31c7b29 style: clang-tidy: llvm-namespace-comment 2020-09-15 09:56:59 -04:00
Yannick Jadoul d65e34d61d
Resolve empty statement warning when using PYBIND11_OVERLOAD_PURE_NAME and PYBIND11_OVERLOAD_PURE (#2325)
* Wrap PYBIND11_OVERLOAD_NAME and PYBIND11_OVERLOAD_PURE_NAME in do { ... } while (false), and resolve trailing semicolon

* Deprecate PYBIND11_OVERLOAD_* and get_overload in favor of PYBIND11_OVERRIDE_* and get_override

* Correct erroneous usage of 'overload' instead of 'override' in the implementation and internals

* Fix tests to use non-deprecated PYBIND11_OVERRIDE_* macros

* Update docs to use override instead of overload where appropriate, and add warning about deprecated aliases

* Add semicolons to deprecated PYBIND11_OVERLOAD macros to match original behavior

* Remove deprecation of PYBIND11_OVERLOAD_* macros and get_overload

* Add note to changelog and upgrade guide
2020-09-15 14:56:20 +02:00
Yannick Jadoul 9df13835c8
Stop py::array_t arguments from accepting arrays that do not match the C- or F-contiguity flags (#2484)
* Stop py::array_t arguments from accepting arrays that do not match the C- or F-contiguity flags

* Add trivially-contiguous arrays to the tests
2020-09-15 14:50:51 +02:00
Henry Schreiner f12ec00d70
feat: py::type::of<T>() and py::type::of(h) (#2364)
* feat: type<T>()

* refactor: using py::type as class

* refactor: py::object as base

* wip: tigher api

* refactor: fix conversion and limit API further

* docs: some added notes from @EricCousineau-TRI

* refactor: use py::type::of
2020-09-14 18:06:26 -04:00
Boris Staletic cc982ac1cd
fix: allow assignment of time points of resolutions other than that of a system clock (#2481) 2020-09-13 10:24:00 -04:00
andriish 38370a87f4
fix: support NVIDIA-PGI HPC SDK (#2475)
* Added guards to the includes

Added new CI config

Added new trigger

Changed CI workflow name

Debug CI

Debug CI

Debug CI

Debug CI

Added flags fro PGI

Disable Eigen

Removed tests that fail

Uncomment lines

* fix: missing include

fix: minor style cleanup

tests: support skipping

ci: remove and tighten a bit

fix: try msvc workaround for pgic

* tests: split up prealoc tests

* fix: PGI compiler fix

* fix: PGI void_t only

* fix: try to appease nvcc

* ci: better ordering for slow tests

* ci: minor improvements to testing

* ci: Add NumPy to testing

* ci: Eigen generates CUDA warnings / PGI errors

* Added CentOS7 back for a moment

* Fix YAML

* ci: runs-on missing

* centos7 is missing pytest

* ci: use C++11 on CentOS 7

* ci: test something else

* Try just adding flags on CentOS 7

* fix: CentOS 7

* refactor: move include to shared location

* Added verbose flag

* Try to use system cmake3 on CI

* Try to use system cmake3 on CI, attempt2

* Try to use system cmake3 on CI, attempt3

* tests: not finding pytest should be a warning, not a fatal error

* tests: cleanup

* Weird issue?

* fix: final polish

Co-authored-by: Andrii Verbytskyi <andrii.verbytskyi@mpp.mpg.de>
Co-authored-by: Henry Schreiner <henryschreineriii@gmail.com>
Co-authored-by: Andrii Verbytskyi <averbyts@cern.ch>
2020-09-11 22:06:52 -04:00
Yannick Jadoul fe9ee86ba8
Add check if `str(handle)` correctly converted the object, and throw py::error_already_set if not (bis) (#2477)
* Add check if `str(handle)` correctly converted the object, and throw py::error_already_set if not

* Fix tests on Python 3

* Apply @rwgk's fixes to cherry-picked commits from #2392
2020-09-11 19:53:04 +02:00
Ciro Santilli b47efd35fb
Use defined for some preprocessor variables that might be undefined (#2476)
The variables PYBIND11_HAS_OPTIONAL, PYBIND11_HAS_EXP_OPTIONAL, PYBIND11_HAS_VARIANT,
__clang__, __APPLE__ were not checked for defined in a minortity of instances.

If the project using pybind11 sets -Wundef, the warnings will show.

The test build is also modified to catch the problem.
2020-09-10 13:58:26 -04:00
Henry Schreiner 621906b3e7
fix: support nvcc and test (#2461)
* fix: support nvcc and test

* fixup! fix: support nvcc and test

* docs: mention what compilers fail

* fix: much simpler logic

* refactor: slightly faster / clearer
2020-09-10 11:49:26 -04:00
Henry Schreiner 37f845a1dc
ci: disallow some common capitalization mistakes (#2472)
* ci: only annotate linux for now

* style: block some common mistakes
2020-09-08 15:26:50 +02:00
Henry Schreiner 0dbda6e80b
feat: py::pos_only (#2459)
* feat: py::pos_only

* fix: review points from @YannickJadoul

* fix: review points from @bstaletic

* refactor: kwonly -> kw_only
2020-09-04 20:02:05 -04:00
Eric Cousineau 44fa79ca80
pytypes: Add Gotchas section about default-constructed wrapper types and py::none() (#2362) 2020-09-04 19:26:57 -04:00
Henry Schreiner 3a89bffac0
ci: harden chrono test, mark another macos 4.9 dev failure (#2448)
* ci: harden chrono test, mark another macos 4.9 dev failure

This should help with a little of the flakiness seen with the timing test

* Update tests/test_chrono.py

* Can also fail
2020-08-31 14:28:07 -04:00
Ralf W. Grosse-Kunstleve 3c061f2168 Fixing `pybind11::bytes()` ambiguous conversion issue.
Adding missing `bytes` type to `test_constructors()`, to exercise the code change.

The changes in the PR were cherry-picked from PR #2409 (with a very minor
modification in test_pytypes.py related to flake8). Via PR #2409, these
changes were extensively tested in the Google environment, as summarized here:
https://docs.google.com/document/d/1TPL-J__mph_yHa1quDvsO12E_F5OZnvBaZlW9IIrz8M/
The changes in this PR did not cause an issues at all.

Note that `test_constructors()` before this PR passes for Python 2 only
because `pybind11::str` can hold `PyUnicodeObject` or `PyBytesObject`. As a
side-effect of this PR, `test_constructors()` no longer relies on this
permissive `pybind11::str` behavior. However, the permissive behavior is still
exercised/exposed via the existing `test_pybind11_str_raw_str()`.

The test code change is designed to enable easy removal later, when Python 2
support is dropped.

For completeness: confusingly, the non-test code changes travelled through PR

Example `ambiguous conversion` error fixed by this PR:
```
pybind11/tests/test_pytypes.cpp:214:23: error: ambiguous conversion for functional-style cast from 'pybind11::detail::item_accessor' (aka 'accessor<accessor_policies::generic_item>') to 'py::bytes'
            "bytes"_a=py::bytes(d["bytes"]),
                      ^~~~~~~~~~~~~~~~~~~~
pybind11/include/pybind11/detail/../pytypes.h:957:21: note: candidate constructor
    PYBIND11_OBJECT(bytes, object, PYBIND11_BYTES_CHECK)
                    ^
pybind11/include/pybind11/detail/../pytypes.h:957:21: note: candidate constructor
pybind11/include/pybind11/detail/../pytypes.h:987:15: note: candidate constructor
inline bytes::bytes(const pybind11::str &s) {
              ^
1 error generated.
```
2020-08-28 11:52:51 -07:00
Yannick Jadoul 6a192781fc
Fix bug roundtripping datetime.time objects after midnight in eastern hemisphere timezones (#2417) (#2438)
* Fix bug roundtripping datetime.time objects after midnight in eastern hemisphere timezones (#2417)

* tests: check more timezones

* Fix review remarks: remove useless comment and skip setting TZ environment variable on Windows
2020-08-28 15:21:43 +02:00
Yannick Jadoul 03b3d59d10
tests: fix CI by including <algorithm> to stop MSVC from complaining about std::count_if in tests/test_sequences_and_iterators.cpp (#2435) 2020-08-25 23:51:07 -04:00
Yannick Jadoul a2bb297b32
Throw exception on returning a unique_ptr or shared_ptr nullptr (or any other holder type) from py::init, rather than crashing (#2430) 2020-08-25 18:51:06 +02:00
Henry Schreiner 110e6c12ce
ci: reduce flakiness a little (#2418) 2020-08-20 11:58:34 -04:00
Henry Schreiner 04fdc44f50
tests: avoid putting build products into source directory (#2353)
* tests: keep source dir clean

* ci: make first build inplace

* ci: drop dev setting (wasn't doing anything)

* tests: warn if source directory is dirty
2020-08-19 13:11:57 -04:00
Henry Schreiner 1729aae96f
feat: new FindPython support (#2370)
* feat: FindPython support

* refactor: rename to PYBIND11_FINDPYTHON

* docs: Caps fixes

* feat: NOPYTHON mode

* test: check simple call

* docs: add changelog/upgrade guide

* feat: Support Python3 and Python2

* refactor: Use targets in tests

* fix: support CMake 3.4+

* feat: classic search also finds virtual environments

* docs: some updates from @wjakob's review

* fix: wrong name for QUIET mode variable, reported by @skoslowski

* refactor: cleaner output messaging

* fix: support debug Python's in FindPython mode too

* fixup! refactor: cleaner output messaging

* fix: missing pybind11_FOUND and pybind11_INCLUDE_DIR restored to subdir mode

* fix: nicer reporting of Python / PyPy

* fix: out-of-order variable fix

* docs: minor last-minute cleanup
2020-08-19 12:26:26 -04:00
Henry Schreiner cf0a64596e
fix: throwing repr caused a segfault (#2389)
* fix: throwing repr caused a segfault

* fixup! ci: include Python 3.9 RC1 (#2387)
2020-08-18 07:14:34 -04:00
Henry Schreiner 6404099712
docs: contrib/issue templates (#2377)
* docs: move helpers to .github where allowed

* docs: more guidelines in CONTRIBUTING

* chore: update issue templates

* fix: review from @bstaletic

* refactor: a few points from @rwgk

* docs: more touchup, review changes
2020-08-17 10:14:23 -04:00
Henry Schreiner 4d9024ec71
tests: cleanup and ci hardening (#2397)
* tests: refactor and cleanup

* refactor: more consistent

* tests: vendor six

* tests: more xfails, nicer system

* tests: simplify to info

* tests: suggestions from @YannickJadoul and @bstaletic

* tests: restore some pypy tests that now pass

* tests: rename info to env

* tests: strict False/True

* tests: drop explicit strict=True again

* tests: reduce minimum PyTest to 3.1
2020-08-16 16:02:12 -04:00
James R. Barlow 3618bea2aa Add and document py::error_already_set::discard_as_unraisable()
To deal with exceptions that hit destructors or other noexcept functions.

Includes fixes to support Python 2.7 and extends documentation on
error handling.

@virtuald and @YannickJadoul both contributed to this PR.
2020-08-16 10:05:03 -07:00
Henry Schreiner a876aac2cf
tests: loosen test, not valid on some systems (#2399) 2020-08-16 11:18:47 -04:00
Ralf W. Grosse-Kunstleve cd85699212
Using recently added `pytest.PY2` instead of `str is bytes`. (#2396)
Important gain: uniformity & therefore easier cleanup when we drop PY2 support.
Very slight loss: it was nice to have `str is bytes` as a reminder in this specific context.
2020-08-14 16:53:41 -04:00
Eric Cousineau ebdd0d368c
tests: Consolidate version (2 vs. 3) and platform (CPython vs. PyPy) checks (#2376)
Fix logic in test_bytes_to_string

Co-authored-by: Henry Schreiner <henryschreineriii@gmail.com>
2020-08-14 14:03:43 -04:00
marc-chiesa 830adda850
Modified Vector STL bind initialization from a buffer type with optimization for simple arrays (#2298)
* Modified Vector STL bind initialization from a buffer type with optimization for simple arrays

* Add subtests to demonstrate processing Python buffer protocol objects with step > 1

* Fixed memoryview step test to only run on Python 3+

* Modified Vector constructor from buffer to return by value for readability
2020-08-13 22:47:23 +02:00
Henry Schreiner 1534e17e44
ci: include Python 3.9 RC1 (#2387) 2020-08-12 14:09:42 -04:00
Henry Schreiner f7abac6689
fix: boost's include dir was listed first (#2384) 2020-08-12 09:33:08 -04:00
Ralf W. Grosse-Kunstleve 173204639e
Adding tests specifically to exercise pybind11::str::raw_str. (#2366)
These tests will also alert us to any behavior changes across Python and PyPy versions.

Hardening tests in preparation for changing `pybind11::str` to only hold `PyUnicodeObject` (NOT also `bytes`). Note that this test exposes that `pybind11::str` can also hold `bytes`.
2020-08-10 17:49:14 -07:00
Henry Schreiner da803eb0a5 fix: duplicate target names removed 2020-08-06 11:54:41 -04:00
Henry Schreiner df115977df chore: cleanup 2020-08-06 11:54:41 -04:00
Yannick Jadoul 3e448c0b5e
Enable py::ellipsis on Python 2 (#2360)
* Enable py::ellipsis on Python 2

* Enable py::ellipsis tests on Python 2 and mention `Ellipsis` in the docs
2020-08-04 14:45:55 +02:00
jbarlow83 4d90f1a199
Add error_scope to py::class_::dealloc() to protect destructor calls (#2342)
Fixes issue #1878
2020-07-31 17:46:12 -07:00
Henry Schreiner 94db5c5ed1 format: apply cmake-format 2020-07-30 20:27:55 -04:00
Henry Schreiner e2a96890e1 format: add cmake-format to pre-commit 2020-07-30 20:27:55 -04:00
Henry Schreiner 6ec1775fff feat: drop CMake 3.6 and below, modernize CMake
fix: include PYTHON_IS_DEBUG
2020-07-30 20:27:55 -04:00
Marcin Wojdyr 8e40e389fd
cast pointer to std::tuple and std::pair (#2334) 2020-07-28 21:44:19 +02:00
Henry Schreiner c51b3f436d ci: change caching 2020-07-27 22:12:19 -04:00
Henry Schreiner ff39ad3f31 ci: include scipy for tests 2020-07-27 22:12:19 -04:00
Henry Schreiner 1b880dfd5a ci: download EIGEN 2020-07-27 22:12:19 -04:00
Sergei Izmailov 7b067cc387
Set __hash__ to None for types that defines __eq__, but not __hash__ (#2291)
fixes #2191
2020-07-27 01:44:25 +02:00
Henry Schreiner aab701399a ci: cache, build wheels on macOS PyPy3 2020-07-26 09:25:27 -04:00
Henry Schreiner 5927c5819f chore: update Catch 2020-07-26 09:25:27 -04:00
Henry Schreiner 1f53c373e4 fix: C++17 mode on Clang may error 2020-07-26 09:25:27 -04:00
Henry Schreiner e428a7f6b8 ci: fix clang warnings 2020-07-26 09:25:27 -04:00
Boris Staletic 2819ce64a4
Avoid attr("__repr__") in initialize_generic (#2317)
If the default argument value is a class, and not an instance of a
class, `a.value.attr("__repr__")` raises a `ValueError`. Switching to
`repr(a.value)` makes this use case work.

Fixes #2028
2020-07-24 18:43:59 +02:00
Henry Schreiner b16347e51d ci: enable GHA testing
Adds requirements file too.
2020-07-23 17:42:53 -04:00
Henry Schreiner a59c6de821 test: allow MSVC/Xcode to run the embed test 2020-07-23 17:42:53 -04:00
Henry Schreiner 49007b7ff3 test: fix warning/error on Python 2 in 14/17 mode 2020-07-23 17:42:53 -04:00
Henry Schreiner 3d20b73e87 test: pass through C++ std for cmake test 2020-07-23 17:42:53 -04:00
Henry Schreiner ef057f12b6 test: support pypy2 7.3 2020-07-23 17:42:53 -04:00
Ralf W. Grosse-Kunstleve 96c6763613
test_builtin_casters.py test_string_view: Python 2 c++17, c++2a compatibility. (#2314)
Tested with 2.7.18rc1, built with Py_UNICODE_SIZE 4.
Change also tested with Python 3.8.
2020-07-22 12:05:16 -07:00
Henry Schreiner d8c7ee00a6
ci: GHA basic format & pre-commit (#2309) 2020-07-20 13:35:21 -04:00
Kota Yamaguchi e248869893
Fix undefined memoryview format (#2223)
* Fix undefined memoryview format

* Add missing <algorithm> header

* Add workaround for py27 array compatibility

* Workaround py27 memoryview behavior

* Fix memoryview constructor from buffer_info

* Workaround PyMemoryView_FromMemory availability in py27

* Fix up memoryview tests

* Update memoryview test from buffer to check signedness

* Use static factory method to create memoryview

* Remove ndim arg from memoryview::frombuffer and add tests

* Allow ndim=0 memoryview and documentation fixup

* Use void* to align to frombuffer method signature

* Add const variants of frombuffer and frommemory

* Add memory view section in doc

* Fix docs

* Add test for null buffer

* Workaround py27 nullptr behavior in test

* Rename frombuffer to from_buffer
2020-07-15 08:50:43 -07:00
Yannick Jadoul f980d76d38
Change NAMESPACE_* macros into PYBIND11_NAMESPACE_* (#2283)
* Change NAMESPACE_BEGIN and NAMESPACE_END macros into PYBIND11_NAMESPACE_BEGIN and PYBIND11_NAMESPACE_END

* Fix sudden HomeBrew 'python not installed' error

* Sweep difference in 'Class.__init__() must be called when overriding __init__' error message between CPython and PyPy under the rug

* Homebrew updated to 3.8 yesterday.

Co-authored-by: Henry Schreiner <HenrySchreinerIII@gmail.com>
2020-07-08 18:14:41 -04:00
Isuru Fernando 0d70f0e18e
PyPy3 support (#2146)
* Error out eval_file

* Enable dynamic attribute support for Pypy >= 6

* Add a test for dynamic attribute support

* Skip test for eval_file on pypy

* Workaround for __qualname__ on PyPy3

* Add a PyPy3.6 7.3.0 build

* Only disable in PyPy3

* Fix travis testing

* No numpy and scipy for pypy

* Enable test on pypy2

* Fix logic in eval_file

* Skip a few tests due to bugs in PyPy

* scipy wheels are broken. make pypy2 a failrue

Co-authored-by: Andreas Kloeckner <inform@tiker.net>
2020-07-07 15:58:16 +02:00
Robert Haschke f2226aefe0
Allow perfect forwarding of method args (#2048) 2020-07-07 15:56:07 +02:00
Dustin Spicuzza 1b0bf352fa
Throw TypeError when subclasses forget to call __init__ (#2152)
- Fixes #2103
2020-07-07 12:04:06 +02:00
Yannick Jadoul d54d6d8c61
Adding pybind11::cast overload for rvalue references (#1260)
* Adding pybind11::cast overload for rvalue references
2020-07-01 01:53:09 +02:00
B Krishna Chaitanya 714424387f Refactor: Extract Custom Type Casts related tests 2020-07-01 00:07:49 +02:00
fatvlady 8c06b95492 Fix compile error on GCC4.8 2020-06-29 21:01:20 +02:00
fatvlady a3daf87d45 Add failing optional test 2020-06-29 21:01:20 +02:00
Ashley Whetter 8e85fadff2 Render `py::none` as `None` in docstrings
Closes #2270
2020-06-29 11:48:44 +02:00
Sergei Izmailov ab323e04f3 Test py::iterable/py::iterator representation in docstrings 2020-06-10 13:36:31 +02:00
Sergei Izmailov 4f1531c454 Render `py::int_` as `int` in docstrings 2020-06-10 13:36:31 +02:00
Sergei Izmailov 22b2504080 Render full numpy numeric names (e.g. numpy.int32) 2020-06-10 13:36:31 +02:00
Clemens Sielaff 63df87fa49
Add lvalue ref-qualified cpp_function constructors (#2213)
* added overload for l-value ref-qualified methods

* Added test.
Before, the code would have failed to build.
2020-06-10 13:35:10 +02:00
Eric Cousineau 4e3d9fea74 operators: Explicitly expose `py::hash(py::self)`
Add warnings about extending STL
2020-05-31 07:03:01 +02:00
Eric Cousineau 5309573005 operators: Move hash check to before mutations, tweak whitespace 2020-05-31 07:03:01 +02:00
Eric Cousineau 2c30e0a118
cmake: Expose `PYBIND11_TEST_OVERRIDE` (#2218)
Primarily for the ccmake curses GUI
2020-05-31 07:00:55 +02:00
Henry Schreiner a38e5331d7
Fix CI, prepare test on Python 3.9 beta (#2233)
* Test on Python 3.9 beta
* Pin Sphinx
* Newer version of PyPy
2020-05-31 06:29:30 +02:00
Nicholas Musolino 02c83dba0f Propagate exceptions in sequence::size() (#2076) 2020-04-26 18:40:52 +02:00
Sebastian Koslowski a86ac538f5 rename args_kw_only to kwonly 2020-04-26 18:07:51 +02:00
Jason Rhinelander be0d804523 Support keyword-only arguments
This adds support for a `py::args_kw_only()` annotation that can be
specified between `py::arg` annotations to indicate that any following
arguments are keyword-only.  This allows you to write:

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

and have it work like Python 3's:

    def f(a, *, b):
        # ...

with respect to how `a` and `b` arguments are accepted (that is, `a` can
be positional or by keyword; `b` can only be specified by keyword).
2020-04-26 18:07:51 +02:00
Dustin Spicuzza 0dfffcf257 Add is_final to disallow inheritance from Python
- Not currently supported on PyPy
2020-04-26 09:46:44 +02:00
Ralf W. Grosse-Kunstleve f6e543b15e Adding a default virtual destructor to Animal type in test_tagbased_polymorphic.cpp.
With this change, and cast.h as-is in master, test_tagbased_polymorphic.cpp fails to compile with the error message below.
With the cast.h change in pull/2016, building and testing succeeds.

cd pybind11/build/tests && /usr/bin/c++  -DPYBIND11_TEST_BOOST -DPYBIND11_TEST_EIGEN -Dpybind11_tests_EXPORTS -Ipybind11/include -I/usr/include/python3.7m -isystem /usr/include/eigen3  -Os -DNDEBUG -fPIC -fvisibility=hidden   -std=c++2a -flto -fno-fat-lto-objects -Wall -Wextra -Wconversion -Wcast-qual -Wdeprecated -o CMakeFiles/pybind11_tests.dir/test_tagbased_polymorphic.cpp.o -c pybind11/tests/test_tagbased_polymorphic.cpp
In file included from pybind11/include/pybind11/attr.h:13,
                 from pybind11/include/pybind11/pybind11.h:44,
                 from pybind11/tests/pybind11_tests.h:2,
                 from pybind11/tests/test_tagbased_polymorphic.cpp:10:
pybind11/include/pybind11/cast.h: In instantiation of ‘static std::pair<const void*, const pybind11::detail::type_info*> pybind11::detail::type_caster_base<type>::src_and_type(const itype*) [with type = Animal; pybind11::detail::type_caster_base<type>::itype = Animal]’:
pybind11/include/pybind11/cast.h:906:31:   required from ‘static pybind11::handle pybind11::detail::type_caster_base<type>::cast_holder(const itype*, const void*) [with type = Animal; pybind11::detail::type_caster_base<type>::itype = Animal]’
pybind11/include/pybind11/cast.h:1566:51:   required from ‘static pybind11::handle pybind11::detail::move_only_holder_caster<type, holder_type>::cast(holder_type&&, pybind11::return_value_policy, pybind11::handle) [with type = Animal; holder_type = std::unique_ptr<Animal>]’
pybind11/include/pybind11/stl.h:175:69:   required from ‘static pybind11::handle pybind11::detail::list_caster<Type, Value>::cast(T&&, pybind11::return_value_policy, pybind11::handle) [with T = std::vector<std::unique_ptr<Animal> >; Type = std::vector<std::unique_ptr<Animal> >; Value = std::unique_ptr<Animal>]’
pybind11/include/pybind11/pybind11.h:159:43:   required from ‘void pybind11::cpp_function::initialize(Func&&, Return (*)(Args ...), const Extra& ...) [with Func = std::vector<std::unique_ptr<Animal> > (*&)(); Return = std::vector<std::unique_ptr<Animal> >; Args = {}; Extra = {pybind11::name, pybind11::scope, pybind11::sibling}]’
pybind11/include/pybind11/pybind11.h:64:9:   required from ‘pybind11::cpp_function::cpp_function(Return (*)(Args ...), const Extra& ...) [with Return = std::vector<std::unique_ptr<Animal> >; Args = {}; Extra = {pybind11::name, pybind11::scope, pybind11::sibling}]’
pybind11/include/pybind11/pybind11.h:819:22:   required from ‘pybind11::module& pybind11::module::def(const char*, Func&&, const Extra& ...) [with Func = std::vector<std::unique_ptr<Animal> > (*)(); Extra = {}]’
pybind11/tests/test_tagbased_polymorphic.cpp:141:36:   required from here
pybind11/include/pybind11/cast.h:880:61: error: ambiguous template instantiation for ‘struct pybind11::polymorphic_type_hook<Animal, void>’
         const void *vsrc = polymorphic_type_hook<itype>::get(src, instance_type);
                            ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~
pybind11/include/pybind11/cast.h:844:8: note: candidates are: ‘template<class itype> struct pybind11::polymorphic_type_hook<itype, typename std::enable_if<std::is_polymorphic<_Tp>::value, void>::type> [with itype = Animal]’
 struct polymorphic_type_hook<itype, detail::enable_if_t<std::is_polymorphic<itype>::value>>
        ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
pybind11/tests/test_tagbased_polymorphic.cpp:115:12: note:                 ‘template<class itype> struct pybind11::polymorphic_type_hook<itype, typename std::enable_if<std::is_base_of<Animal, itype>::value, void>::type> [with itype = Animal]’
     struct polymorphic_type_hook<itype, detail::enable_if_t<std::is_base_of<Animal, itype>::value>>
            ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from pybind11/include/pybind11/attr.h:13,
                 from pybind11/include/pybind11/pybind11.h:44,
                 from pybind11/tests/pybind11_tests.h:2,
                 from pybind11/tests/test_tagbased_polymorphic.cpp:10:
pybind11/include/pybind11/cast.h:880:61: error: incomplete type ‘pybind11::polymorphic_type_hook<Animal, void>’ used in nested name specifier
         const void *vsrc = polymorphic_type_hook<itype>::get(src, instance_type);
                            ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~
2020-04-14 17:48:43 +02:00
fwjavox e97c735fc4 stl_bind: add binding for std::vector::clear (#2074) 2020-01-17 01:16:56 +01:00
Vemund Handeland 6e39b765b2 Add C++20 char8_t/u8string support (#2026)
* Fix test build in C++20

* Add C++20 char8_t/u8string support
2019-12-19 12:16:24 +01:00
Sebastian Koslowski dc65d66171 support for readonly buffers (#863) (#1466) 2019-11-24 08:33:05 +01:00
Francesco Biscani deb3cb238a Add exception translation for std::overflow_error. (#1977) 2019-11-14 08:56:58 +01:00
Yannick Jadoul 55ff464233 Fixing SystemError when nb_bool/nb_nonzero sets a Python exception in type_caster<bool>::load (#1976) 2019-11-14 08:56:22 +01:00
Sebastian Gsänger a83d69e78f test pair-copyability on C++17 upwards (#1886)
* test pair-copyability on C++17 upwards

The stdlib falsely detects containers like M=std::map<T, U>
as copyable, even when one of T and U is not copyable.
Therefore we cannot rely on the stdlib dismissing std::pair<T, M>
by itself, even on C++17.

* fix is_copy_assignable

bind_map used std::is_copy_assignable which suffers from the same problems
as std::is_copy_constructible, therefore the same fix has been applied.

* created tests for copyability
2019-10-31 12:38:24 +01:00
Hans Dembinski bdf6a5e870 Report type names in return value policy-related cast exceptions (#1965) 2019-10-23 13:19:58 +02:00
Wenzel Jakob 31680e6f9c Implicit conversion from enum to int for Python 3.8 (fix by @sizmailov) 2019-09-20 11:06:10 +02:00
Wenzel Jakob 21d0eb460f Fix Python 3.8 test regression 2019-09-20 09:38:30 +02:00
Sergei Izmailov 09f0829401 Avoid conversion to `int_` rhs argument of enum eq/ne (#1912)
* fix: Avoid conversion to `int_` rhs argument of enum eq/ne

* test: compare unscoped enum with strings

* suppress comparison to None warning

* test unscoped enum arithmetic and comparision with unsupported type
2019-09-19 18:23:27 +02:00
Lori A. Burns f6c4c1047a restores __invert__ to arithmetic-enabled enum, fixes #1907 (#1909) 2019-09-04 22:16:21 +02:00
Stephen Larew 5b4751af26 Add const to buffer:request() (#1890) 2019-08-27 17:05:47 +02:00
kingofpayne 12e8774bc9 Added support for list insertion. (#1888) 2019-08-19 23:00:36 +02:00
Andre Schmeißer 19189b4c2c Make `overload_cast_impl` available in C++11 mode. (#1581)
* Make `overload_cast_impl` available in C++11 mode.

Narrow the scope of the `#if defined(PYBIND11_CPP14)` block around overload_cast to only
cover the parts where C++14 is stricly required. Thus, the implementation in
`pybind11::details::overload_cast_impl` is still available in C++11 mode.

* PR #1581: Modify test to use overload_cast_impl, update docs and change log
2019-08-19 12:54:33 +02:00
Sergei Lebedev 08b0bda4bc Added set::contains and generalized dict::contains (#1884)
Dynamically resolving __contains__ on each call is wasteful since set
has a public PySet_Contains function.
2019-08-16 21:32:27 +02:00
ali-beep 5ef13eb680 Add negative indexing support to stl_bind. (#1882) 2019-08-15 19:41:11 +02:00
Pauli Virtanen c9d32a81f4 numpy: fix refcount leak to dtype singleton (#1860)
PyArray_DescrFromType returns a new reference, not borrowed one
2019-07-27 11:35:32 +02:00
Eric Cousineau e9ca89f453 numpy: Add test for explicit dtype checks. At present, int64 + uint64 do not exactly match dtype(...).num 2019-07-23 13:17:20 +02:00
phil-zxx c6b699d9c2 Added ability to convert from datetime.date to system_clock::time_point (#1848)
* Added ability to convert from Python datetime.date and datetime.time to C++ system_clock::time_point
2019-07-19 11:28:48 +02:00
Jeremy Maitin-Shepard a3f4a0e8ab Add support for __await__, __aiter__, and __anext__ protocols (#1842) 2019-07-18 09:02:35 +02:00
Saran Tunyasuvunakool b60fd233fa Make sure `detail::get_internals` acquires the GIL before making Python calls. (#1836)
This is only necessary if `get_internals` is called for the first time in a given module when the running thread is in a GIL-released state.

Fixes #1364
2019-07-15 16:47:02 +02:00
Igor Socec a301c5add8 Dtype field ordering for NumPy 1.14 (#1837)
* Test dtype field order in numpy dtype tests

When running tests with NumPy 1.14 or later this test exposes the
"invalid buffer descriptor" error reported in #1274.

* Create dtype_ptr with ordered fields
2019-07-15 13:31:03 +02:00
Wenzel Jakob 9fd4712121 fix test suite (pytest changes in ExceptionInfo class) 2019-07-06 17:35:31 +02:00
Ian Bell 502ffe50a9 Add docs and tests for unary op on class (#1814) 2019-06-22 12:07:41 +02:00
Alexander Gagarin b3bf248eec Fix casting of time points with non-system-clock duration with VS (#1748)
* Fix casting of time points with non-system-clock duration on Windows

Add explicit `time_point_cast` to time point with duration of system
clock. Fixes Visual Studio compile error.

* Add test case for custom time points casting
2019-06-13 09:17:10 +02:00
Wenzel Jakob e11e71d85c Make compiler flags for -Werror specific to GNU, Clang, or Intel 2019-06-11 23:28:58 +02:00
sizmailov 21c3911bd3 add signed overload for `py::slice::compute` 2019-06-11 23:28:58 +02:00
Chris Rusby 22859bb8fc Support more natural syntax for vector extend 2019-06-11 23:28:58 +02:00
Roland Dreier 1aa8dd1745 Fix assertion failure for unions (#1685) (#1709)
In def_readonly and def_readwrite, there is an assertion that the member comes
from the class or a base class:

    static_assert(std::is_base_of<C, type>::value, "...");

However, if C and type are the same type, is_base_of will still only be true
if they are the same _non-union_ type.  This means we can't define accessors
for the members of a union type because of this assertion.

Update the assertion to test

    std::is_same<C, type>::value || std::is_base_of<C, type>::value

which will allow union types, or members of base classes.

Also add a basic unit test for accessing unions.
2019-06-11 23:28:58 +02:00
Alexander Gagarin 0071a3feb0 Fix async Python functors invoking from multiple C++ threads (#1587) (#1595)
* Fix async Python functors invoking from multiple C++ threads (#1587)

Ensure GIL is held during functor destruction.

* Add async Python callbacks test that runs in separate Python thread
2019-06-11 23:28:58 +02:00
Axel Huebl 000aabb2a7 Test: Numpy Scalar Creation (#1530)
I found that the numpy array tests already contained an empty-shaped
array test, but none with data in it.

Following PEP 3118, scalars have an empty shape and ndim 0. This
works already and is now also documented/covered by a test.
2019-06-11 14:00:05 +02:00
luzpaz 21bf16f5b8 misc. comment typo (#1629) 2019-06-10 21:56:38 +02:00
Blake Thompson 30c0352348 Added __contains__ to stl bindings for maps (#1767)
* Added __contains__ to stl bindings for maps
2019-06-10 21:01:11 +02:00
Yannick Jadoul 97784dad3e [BUGFIX] Fixing pybind11::error_already_set.matches to also work with exception subclasses (#1715)
* Fixing order of arguments in call to PyErr_GivenExceptionMatches in pybind11::error_already_set.matches

* Added tests on error_already_set::matches fix for exception base classes
2019-05-12 23:35:49 +02:00
Henry Schreiner ae951ca085 CI fixes (#1744)
* Fix warning that not including a cmake source or build dir will be a fatal error (it is now on newest CMakes)
    * Fixes appveyor
* Travis uses CMake 3.9 for more than a year now
* Travis dropped sudo: false in December
* Dropping Sphinx 2
- clang7: Suppress self-assign warnings; fix missing virtual dtors
- pypy:
  - Keep old version (newer stuff breaks)
  - Pin packages to extra index for speed
- travis:
  - Make docker explicit; remove docker if not needed
  - Make commands more verbose (for debugging / repro)
  - Make Ubuntu dist explicit per job
- Fix Windows
- Add names to travis
2019-04-06 19:09:39 +02:00
Wenzel Jakob 25abf7efba flake8 fixes 2019-02-04 17:09:47 +01:00
Guilhem Saurel 43a39bc7d8 ignore numpy.ufunc size warnings 2019-02-04 16:09:21 +01:00
Guilhem Saurel e7ef34f23f compatibility with pytest 4.0, fix #1670
Cf. https://docs.pytest.org/en/latest/deprecations.html#pytest-namespace
2019-02-04 16:09:21 +01:00
Yannick Jadoul 085a29436a Increasing timeout in test_gil_scoped.py to get AppVeyor to succeed 2019-01-03 22:43:52 +01:00
Borja Zarco e2b884c33b Use `PyGILState_GetThisThreadState` when using gil_scoped_acquire. (#1211)
This avoids GIL deadlocking when pybind11 tries to acquire the GIL in a thread that already acquired it using standard Python API (e.g. when running from a Python thread).
2018-12-01 22:47:40 +09:00
voxmea 17983e7425 Adds type_caster support for std::deque. (#1609)
* Adds std::deque to the types supported by list_caster in stl.h.
* Adds a new test_deque test in test_stl.{py,cpp}.
* Updates the documentation to include std::deque as a default
  supported type.
2018-11-16 06:45:19 +01:00
Trevor Laughlin 63c2a972fe Enable unique_ptr holder with mixed Deleters between base and derived types (#1353)
* Check default holder

-Recognize "std::unique_ptr<T, D>" as a default holder even if "D" doesn't match between base and derived holders

* Add test for unique_ptr<T, D> change
2018-11-11 19:36:55 +01:00
Wenzel Jakob cea42467b0
fix py::cast<void *> (#1605)
Pybind11 provides a cast operator between opaque void* pointers on the
C++ side and capsules on the Python side. The py::cast<void *>
expression was not aware of this possibility and incorrectly triggered a
compile-time assertion ("Unable to cast type to reference: value is
local to type caster") that is now fixed.
2018-11-11 19:32:09 +01:00
Wenzel Jakob e2eca4f8f8
Support C++17 aligned new statement (#1582)
* Support C++17 aligned new statement

This patch makes pybind11 aware of nonstandard alignment requirements in
bound types and passes on this information to C++17 aligned 'new'
operator. Pre-C++17, the behavior is unchanged.
2018-11-09 20:14:53 +01:00
Wenzel Jakob adc2cdd5c4
fixed regression in STL type caster RVPs (fixes #1561) (#1603) 2018-11-09 20:12:46 +01:00
Wenzel Jakob 9f73060cc7
std::array<> caster: support arbitrary sequences (#1602)
This PR brings the std::array<> caster in sync with the other STL type
casters: to accept an arbitrary sequence as input (rather than a list,
which is too restrictive).
2018-11-09 12:32:48 +01:00
Tarcísio Fischer 54eb8193e5 Fix scoped enums comparison for equal/not equal cases (#1339) (#1571) 2018-10-24 11:18:58 +02:00
Allan Leal e76dff7751 Fix for Issue #1258 (#1298)
* Fix for Issue #1258

list_caster::load method will now check for a Python string and prevent its automatic conversion to a list.
This should fix the issue "pybind11/stl.h converts string to vector<string> #1258" (https://github.com/pybind/pybind11/issues/1258)

* Added tests for fix of issue #1258

* Changelog: stl string auto-conversion
2018-10-11 10:28:12 +02:00
Jason Rhinelander 177713fa4e Fix gcc-8 compilation warning 2018-10-02 12:11:37 -03:00
oremanj e7761e3383 Fix potential crash when calling an overloaded function (#1327)
* Fix potential crash when calling an overloaded function

The crash would occur if:
- dispatcher() uses two-pass logic (because the target is overloaded and some arguments support conversions)
- the first pass (with conversions disabled) doesn't find any matching overload
- the second pass does find a matching overload, but its return value can't be converted to Python

The code for formatting the error message assumed `it` still pointed to the selected overload,
but during the second-pass loop `it` was nullptr. Fix by setting `it` correctly if a second-pass
call returns a nullptr `handle`. Add a new test that segfaults without this fix.

* Make overload iteration const-correct so we don't have to iterate again on second-pass error

* Change test_error_after_conversions dependencies to local classes/variables
2018-09-25 23:55:18 +02:00
Wenzel Jakob f4245181ae enum_: move most functionality to a non-template implementation
This commit addresses an inefficiency in how enums are created in
pybind11. Most of the enum_<> implementation is completely generic --
however, being a template class, it ended up instantiating vast amounts
of essentially identical code in larger projects with many enums.

This commit introduces a generic non-templated helper class that is
compatible with any kind of enumeration. enum_ then becomes a thin
wrapper around this new class.

The new enum_<> API is designed to be 100% compatible with the old one.
2018-09-11 22:08:26 +02:00
Wenzel Jakob b4b2292488 relax operator[] for tuples, lists, and sequences
object_api::operator[] has a powerful overload for py::handle that can
accept slices, tuples (for NumPy), etc.

Lists, sequences, and tuples provide their own specialized operator[],
which unfortunately disables this functionality. This is accidental, and
the purpose of this commit is to re-enable the more general behavior.

This commit is tangentially related to the previous one in that it makes
py::handle/py::object et al. behave more like their Python counterparts.
2018-09-11 22:08:26 +02:00
Wenzel Jakob 067100201f object_api: support the number protocol
This commit revamps the object_api class so that it maps most C++
operators to their Python analogs. This makes it possible to, e.g.
perform arithmetic using a py::int_ or py::array.
2018-09-11 22:08:26 +02:00
Krzysztof Fornalczyk 5c8746ff13 check for already existing enum value added; added test (#1453)
* check for already existing enum value added; added test

* added enum value name to exception message

* test for defining enum with multiple identical names moved to test_enum.cpp/py
2018-09-11 10:59:56 +02:00
Wenzel Jakob 44e39e0de7
fix regression reported by @cyfdecyf in #1454 (#1517) 2018-09-11 09:32:45 +02:00
Wenzel Jakob e0f3a766e9
Fixed flake8 error in test_iostream.py 2018-08-29 12:10:48 +02:00
Justin Bassett 2cbafb057f fix detail::pythonbuf::overflow()'s return value to return not_eof(c) (#1479) 2018-08-29 11:48:30 +02:00
Wenzel Jakob 885b5b905a Eigen test suite: don't create a np.matrix 2018-08-28 23:22:55 +02:00
Wenzel Jakob d4b37a284a added py::ellipsis() method for slicing of multidimensional NumPy arrays
This PR adds a new py::ellipsis() method which can be used in
conjunction with NumPy's generalized slicing support. For instance,
the following is now valid (where "a" is a NumPy array):

py::array b = a[py::make_tuple(0, py::ellipsis(), 0)];
2018-08-28 23:22:55 +02:00