From 3a8c99c1cb396d2429f4b9919c79adeacb5f769e Mon Sep 17 00:00:00 2001 From: PyBind11 Upstream Date: Thu, 29 Oct 2020 11:37:42 -0400 Subject: [PATCH] pybind11 2020-10-29 (06a54018) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Code extracted from: https://github.com/pybind/pybind11.git at commit 06a54018c8a9fd9a7be5f5b56414b5da9259f637 (master). Upstream Shortlog ----------------- Ahmed Sobhy (1): 8d8d48c5 fix: do not use abi::__forced_unwind with libc++, even with gcc instead of clang (#2569) Andrew J. Hesford (1): a3118130 pytypes.h: fix docs generation (#2220) Andrey Dorozhkin (1): 1817d211 Disable defining (v)snprintf as macro in modern Visual Studio Ashley Whetter (1): 8e85fadf Render `py::none` as `None` in docstrings Axel Huebl (2): a6355b00 CMake: Add Python 3.8 to pybind11Tools (#1974) 6ebfc4b2 Document CMAKE_CXX_STANDARD B Krishna Chaitanya (1): 71442438 Refactor: Extract Custom Type Casts related tests Baljak (2): 07e22593 Fix compilation with MinGW only (#2053) 4f72ef84 Fix the use of MSVC in an MSYS environment (#2087) Bjorn (1): d4d7ef5d Update pybind11Tools.cmake (#2419) Boris Staletic (14): fe2a06e3 Pin breathe to 4.13.1 dc9006db Use newer macOS image for python3 testing 1376eb0e Free tstate on python 3.7+ on finalize_interpreter (#2020) ae2ee2a4 Avoid using deprecated API in python 3.9 b2f52225 Rename embedded_module object's name (#2282) 8b9eb964 Check for NULL in raw_str on Python3 (#2290) aa982e13 Small fixes in numpy.h (#2293) 441e7770 Use new style __init__ in numpy docs (#2316) 2819ce64 Avoid attr("__repr__") in initialize_generic (#2317) cc982ac1 fix: allow assignment of time points of resolutions other than that of a system clock (#2481) 32bb9071 Avoid C-style casts for pointers in docs (#2487) d3c999c7 fix: rename `pybind11::module` to `pybind11::module_` (#2489) 5ebc7816 Allow raw unions without base classes in is_accessible_base_of (#2320) 4038542b Remove friend declaration of py::class_ in py::detail::generic_type (#2613) Charles Brossollet (1): 0f1d3bfe Add FAQ entry for dealing with long functions interruption (#2000) Chuck Atkins (1): d730fbc0 Utilize CMake's language standards abstraction when possible Ciro Santilli (1): b47efd35 Use defined for some preprocessor variables that might be undefined (#2476) Clemens Sielaff (1): 63df87fa Add lvalue ref-qualified cpp_function constructors (#2213) Daniel Saxton (1): fb0a3a0e Fix broken README link (#2449) Dariusz Suchojad (1): bed9080c docs: Updated the link to the PDF docs (#2603) (#2605) David Stone (1): 5088364b Declare `operator==` and `operator!=` member functions const. David Vo (1): 5e6ec496 Add enum value to enum repr (#2126) Dekken (1): 1abc4a9d fix: doc typo, drop second use of 'without' (#2439) Dustin Spicuzza (4): 0dfffcf2 Add is_final to disallow inheritance from Python 2c4cd841 Add AutoWIG to list of binding generators (#1990) 1b0bf352 Throw TypeError when subclasses forget to call __init__ (#2152) 6f3470f7 Add robotpy-build to list of tools (#2359) Eric Cousineau (10): baf69345 Minor modifications to interrupt handling FAQ (#2007) 2c30e0a1 cmake: Expose `PYBIND11_TEST_OVERRIDE` (#2218) 53095730 operators: Move hash check to before mutations, tweak whitespace 4e3d9fea operators: Explicitly expose `py::hash(py::self)` ebdd0d36 tests: Consolidate version (2 vs. 3) and platform (CPython vs. PyPy) checks (#2376) 5a3ff723 ci: Remove "Setup Boost (macOS)" step (#2395) 44fa79ca pytypes: Add Gotchas section about default-constructed wrapper types and py::none() (#2362) 4e7c08da test_numpy_dtypes: Add test for py::vectorize() (#2260) d74be321 tests: Fix test override execution (#2512) c513b5e3 tests(CMakeLists): robustify against add'l versions of Boost (#2608) Erick Matsen (1): b32b762c Fixing minor typo in basics.rst (#1984) Florian Apolloner (1): fe1392d0 Silence Clang 10 compiler warnings. Fixes #2225. (#2294) Francesco Biscani (2): deb3cb23 Add exception translation for std::overflow_error. (#1977) bd24155b Aligned allocation fix for clang-cl (#1988) Fritz Reese (1): e8ad33bb Fix buffer_info for ctypes buffers (pybind#2502) (#2503) Griffin Downs (1): a4cee36b Add vcpkg installation instructions (#1936) Guilherme Dantas (1): 8908552d typo Hans Dembinski (2): c27a6e13 make builds with python tests and cpp tests fail if either one fails (#1967) bdf6a5e8 Report type names in return value policy-related cast exceptions (#1965) Henry Fredrick Schreiner (2): 11f756f5 fix: type bug intruduced in #2492 e3774b76 tests: the condition was too look on xfail Henry Schreiner (151): a38e5331 Fix CI, prepare test on Python 3.9 beta (#2233) d8c7ee00 ci: GHA basic format & pre-commit (#2309) ef057f12 test: support pypy2 7.3 3d20b73e test: pass through C++ std for cmake test 49007b7f test: fix warning/error on Python 2 in 14/17 mode a59c6de8 test: allow MSVC/Xcode to run the embed test 73dc9786 style: avoid adding virtual environment directories b16347e5 ci: enable GHA testing 8b0ccf76 style: adding pre-commit check-style.sh 5c936d4b refactor: issue fixed upstream 6f3e5e38 ci: keep travis from running twice 6a80171c ci: Add clang builds & C++17 e428a7f6 ci: fix clang warnings 1f53c373 fix: C++17 mode on Clang may error 44706717 fix: too many braces on clang 3.6 5927c581 chore: update Catch aab70139 ci: cache, build wheels on macOS PyPy3 78069057 ci: trim down Appveyor 9521bc56 ci: drop parts from Travis 1b880dfd ci: download EIGEN 2ee1bcd9 ci: add GHA badge 2fbbd510 ci: add gcc e450ea05 ci: add docker build to GHA 249a13bd ci: move docs check to GHA ff39ad3f ci: include scipy for tests 88b3e5c6 ci: nicer naming scheme a6324868 ci: Windows 2016 test c51b3f43 ci: change caching 1491c94c ci: cmake Configure test (#2327) 6ec1775f feat: drop CMake 3.6 and below, modernize CMake 1b92cd17 fix: address review points from @YannickJadoul f64d5aa6 fix: typo for warning on C++17, support Win + 2.7 1651c324 update: address review points e2a96890 format: add cmake-format to pre-commit 94db5c5e format: apply cmake-format b8047245 fix: uninstall cleanup (#2346) 1caf1d06 fix: check list validity and add missing file (#2352) df115977 chore: cleanup da803eb0 fix: duplicate target names removed ed6de125 format: include .in files c664d557 ci: better output / more config 227170dc fix: better handling of PYBIND11_CPP_STANDARD 0af7fe6c fix: typo in pybind11_add_module (#2374) f7abac66 fix: boost's include dir was listed first (#2384) 1534e17e ci: include Python 3.9 RC1 (#2387) 2e2de8c8 fix: add missing signature (#2363) cba4a985 ci: include Boost (#2393) a876aac2 tests: loosen test, not valid on some systems (#2399) 4d9024ec tests: cleanup and ci hardening (#2397) 64040997 docs: contrib/issue templates (#2377) cf0a6459 fix: throwing repr caused a segfault (#2389) 1729aae9 feat: new FindPython support (#2370) 04fdc44f tests: avoid putting build products into source directory (#2353) 24dffe46 fix: PYBIND11_MASTER_PROJECT always ON (#2412) 110e6c12 ci: reduce flakiness a little (#2418) a6887b60 docs: update changelog and versionadded 2fa18431 docs: pin versions for readthedocs f31df738 docs: move CONTRIBUTING (#2402) 56df3c46 fix: a couple more places where pybind11 is missing 11 (#2421) c58f7b74 fix: reduce target collision in add_submodule mode (#2423) 5b59b7b2 ci: gha annotations (#2427) 9b8cb020 fix: respect PYTHON_VERSION if set in classic mode (#2414) 3a89bffa ci: harden chrono test, mark another macos 4.9 dev failure (#2448) 72b06b86 ci: Eigen moved 0dbda6e8 feat: py::pos_only (#2459) ce1a07ef fix: use classic extension handling unless otherwise requested (#2462) 37f845a1 ci: disallow some common capitalization mistakes (#2472) 621906b3 fix: support nvcc and test (#2461) f12ec00d feat: py::type::of() and py::type::of(h) (#2364) 8dc31c7b style: clang-tidy: llvm-namespace-comment 5dfbe6f9 style: clang-tidy: modernize-use-override 96e6a8d5 style: clang-tidy: readability-container-size-empty b342c373 style: clang-tidy: modernize-use-using b491b465 style: clang-tidy: modernize-use-equals-default ce88e940 style: clang-tidy: modernize-use-auto 4d786408 style: clang-tidy: modernize-use-emplace e7bafc8e style: clang-tidy: default checks and fix bug in iostream deconstruction dabbbf31 fix: use OVERRIDE instead of OVERLOAD (#2490) 41aa9260 refactor: replace .get_type with type::handle_of (#2492) fd61f503 feat: setup.py redesign and helpers (#2433) e37921d7 refactor: drop mkdoc and update changelog (#2491) dec33c29 docs: installing section (#2494) 99ef2b84 docs: read version from pybind11 file (#2496) 81555ce6 docs: Use README.rst in docs as home page (#2500) 54831a9a fix: use svg to pdf converter instead of original solution d753b769 docs: corrected link to documentation 8fa70e74 ci: fix broken documenation test (works on RtD) (#2506) 87828c7e fix: typo in Windows C++17 support (#2507) 4a288ab9 fix: Windows C++ latest (#2508) d0ed035c fix: AppleClang 12 warnings (#2510) e01f4924 fix: Add a nice CMake error when Interpreter component is missing. (#2519) 3c7ef56b fix: warning on latest AppleClang (#2522) 82dbc5b7 ci: releases (#2530) 21902462 chore: bump to 2.6.0b1, beta release 0fc5774c fix: beta does not have a dot in front (#2539) 1994691a ci: download artifact issue 319b9964 fix: metadata was adding quotes to distinfo 01ad8920 ci: publish in two steps 6d2d08db fix: conda-forge patch (#2540) b9d00273 feat: parallel compiler (#2521) 5f9b1667 ci: label PRs with changelog needed (#2546) 1bcd5f0a Update pr_merged.yml ca4127ce ci: fix labeler 3488494a refactor: import check as a common function (#2526) 560ed3e3 docs: fix odd spacing 6bcd220c refactor: module -> module_ with typedef (#2544) 2a2f5220 fix: find_import didn't work properly for classic tools (#2550) 9a0c96dd feat: py::prepend tag (#1131) 00edc300 fix: PYBIND11_OBJECT required pybind11 namespace (regression) (#2553) b6f37f67 docs: minor cleanup (#2555) 49c389b7 ci: test on Windows 3.8 and 3.9 (mostly) (#2560) deba040b test: hide segfault (#2559) e4e5c49a docs: known issues (#2565) fae45325 ci: fix labeler f385eccd chore: bump to 2.6.0rc1 c02f0ec6 fix: formatting issue fecef388 refactor: simpler followup to #2569 (#2572) d46f3322 fix: allow cmake varibles from Python calls to be cached (#2570) 7d7309b8 ci: monitor dependencies cd2c25a2 ci: adjust labeler match pattern 4f45052d ci: fix ci label 993495c9 fix: Intel 18+ required (#2577) ae2e5a36 ci: try to get labeler to add needs changelog 2f746eee ci: original labeler 7c71dd38 ci: inverted labeler logic edda89ba fix: install to DATAROOTDIR instead of hardcoding share (#2584) c99d7f1c docs: PR template (#2580) 2a263e08 docs: some final 2.6.0 updates (#2582) 526a7733 ci: merged labels still missing a8c2e3ee fix: ipo should be off for debug or relwithdebinfo (#2590) 645d8381 feat: typing support for helpers (#2588) c4a8b5bb chore: bump to 2.6.0rc2 63f2deea chore: update Eigen f2008325 style: ssize_t -> py::ssize_t b72c79f9 fix: std::is_pod -> pybind11::detail::is_pod 9ac604a3 fix: PyPy windows issue 6bb71c48 fix: more intp asserts, reinterpret_cast ad2fe0ee fix: pypy3 windows extension issue a4221153 fix: new line in PYPY_VERSION 8ae11fbf ci: add more runs 2e31e466 docs: update for PyPy win 32 issue c50f90ec style: use Black everywhere (#2594) 064362fb fix: allow the ABI string to be forced (#2602) c16da993 chore: bump to 2.6.0rc3 09056717 fix: much better pdf (#2604) 0b9acc40 fix: chapters in PDF again (#2606) 120e2e21 docs: tiny typo 4d037439 fix: missing identifier for Python 3.9 ace4deb4 ci: add more jobs (#2610) 139c05da ci: test on NumPy + Python 3.9 (#2628) 06a54018 fix: min macos (#2622) Holger Kohr (1): fbc75636 Add py::object casting example to embedding docs (#2466) Hyrum Wright (1): 961b2e62 fix: ensure the GIL is held when copying a function. (#2545) Isuru Fernando (3): 37352491 Install headers using both headers and package_data (#1995) e107fc2a Fix setuptools record of headers 0d70f0e1 PyPy3 support (#2146) JGamache-autodesk (1): 37d04abd Fixes #1295: Handle debug interpreter (#2025) James R. Barlow (1): 3618bea2 Add and document py::error_already_set::discard_as_unraisable() Jason Rhinelander (1): be0d8045 Support keyword-only arguments Jay Soffian (1): 01453b05 fix (setup_helpers): fix bug in _add_lflags (#2586) Jeremy Nimmer (1): 759221f5 Obey __cpp_sized_deallocation and __cpp_aligned_new Kota Yamaguchi (1): e2488698 Fix undefined memoryview format (#2223) Liam Keegan (1): c4fd1fd9 CMake improvement: support windows python on msys (#2312) Lin Hsu (1): d031efe7 Typo MRocholl (3): 00c462d1 find library path to libclang.so via glob command in /usr/lib/llvm-* and set it 9358e30d change set_path to set_file b14aeb7c fix typo in sorted function call argument reverse Mana Borwornpadungkitti (1): 6edd0e6d fix: Reject keyword argument `None` with `.none(false)` (#2611) Marcin Wojdyr (1): 8e40e389 cast pointer to std::tuple and std::pair (#2334) Matthew Dawkins (1): 6f11347a Prevent cmake error when prefix empty (#1986) Matthijs van der Burgh (1): b5240089 Deepcopy documentation (#2242) Michael Goulding (1): fb042d69 Fix warning C26817 on copying in `for (auto vh : value_and_holder(...))` (#2382) Mosalam Ebrahimi (1): 7dd2bdb0 docs: fix typo (#2405) Nicholas Musolino (1): 02c83dba Propagate exceptions in sequence::size() (#2076) Nils Berg (1): 819802da Fix a memory leak when creating Python3 modules. (#2019) Nils Leif Fischer (1): 141e8cc0 Fix uninitialized-variables cmake warnings Orell Garten (1): 9ed8b440 Change __init__(self) to __new__(cls) Paul Fultz II (1): 69821d9e Disable testing when using BUILD_TESTING (#1682) Ralf W. Grosse-Kunstleve (8): 4697149d Allows users to specialize polymorphic_type_hook with std::enable_if. f6e543b1 Adding a default virtual destructor to Animal type in test_tagbased_polymorphic.cpp. 96c67636 test_builtin_casters.py test_string_view: Python 2 c++17, c++2a compatibility. (#2314) 17320463 Adding tests specifically to exercise pybind11::str::raw_str. (#2366) cd856992 Using recently added `pytest.PY2` instead of `str is bytes`. (#2396) 3c061f21 Fixing `pybind11::bytes()` ambiguous conversion issue. d159a563 static allocation for PyModuleDef, to avoid leak check errors. (#2413) 8290a5a0 clang -Wnon-virtual-dtor compatibility (#2626) Riccardo Bertossa (1): 6c29cbf8 misleading comment corrected (strides in buffer_info is bytes and not number of entries) (#1958) Rickard Hallerbäck (1): 5a8ec8e5 Include what is used in pybind11.h (#2499) Riyaz Haque (1): 2b6b98e2 Bugfix/Check actual value when deregistering pybind11 instance (#2252) Robert Haschke (2): 370a2ae2 Declare call_impl() as && (#2057) f2226aef Allow perfect forwarding of method args (#2048) Sebastian Gsänger (1): a83d69e7 test pair-copyability on C++17 upwards (#1886) Sebastian Koslowski (2): dc65d661 support for readonly buffers (#863) (#1466) a86ac538 rename args_kw_only to kwonly Sergei Izmailov (7): 22b25040 Render full numpy numeric names (e.g. numpy.int32) 57070fb0 Render py::iterator/py::iterable as Iterator/Iterable in docstrings 90d99b56 Render pybind11::array as numpy.ndarray in docstrings 4f1531c4 Render `py::int_` as `int` in docstrings ab323e04 Test py::iterable/py::iterator representation in docstrings 7b067cc3 Set __hash__ to None for types that defines __eq__, but not __hash__ (#2291) 4c36fb7b [DOC] avoid C++ types in docstrings (#2441) Simeon Ehrig (1): c776e9ef Fix compiler error with MSVC 17 and CUDA 10.2 Takahiro Ishikawa (1): 3618807d Fix python.lib name Thomas Köppe (3): eeb10448 [common.h] Mark entry point as "unused". 1e14930d [common.h] Mark another entry point as "unused". f2e79986 [common.h] Mark another entry point as "unused". (#2625) Valentin Boettcher (1): 3be085d1 docs: typo prefix -> suffix (#2593) Vemund Handeland (1): 6e39b765 Add C++20 char8_t/u8string support (#2026) Wenzel Jakob (22): dfde1554 begin working on next version 61e4f118 numpy.h: minor preprocessor fix suggested by @chaekwan a6064822 Revert "numpy.h: minor preprocessor fix suggested by @chaekwan" fb910ae9 Revert "Fix a memory leak when creating Python3 modules. (#2019)" b4e5d582 undo #define copysign in pyconfig.h f9f3bd71 Use C++17 fold expressions when casting tuples and argument lists (#2043) 2fda9d5d Travis CI fix (MacOS, Py3) 4c206e8c bindings for import_error exception bf2b0314 Handle cases where binding code immediately throws py::error_already_set bb9c91cc pybind11Tools.cmake: search for Python 3.9 3b1dbeba v2.5.0 release 02348716 begin working on next version a54eab92 Revert "Change __init__(self) to __new__(cls)" 8c0cd944 ignore another type of visual studio project file fc3a4490 Minor clarification (@AntoinePrv, #2083) 36c666f0 pybind11_add_module(): OPT_SIZE target 064a03a4 main CMakeLists.txt file: be less noisy 3232e59b Check if module file extension could be queried correctly (#2538) 2bc62dc8 updated credits 7f9445a6 Thanks, Google! 59a2ac27 v2.6.0 release 86d3e9eb begin working towards a future v2.6.1 patch release Wink Saville (1): 7c26c718 Fix mispelling in basics.rst (#2614) Yannick Jadoul (30): 55ff4642 Fixing SystemError when nb_bool/nb_nonzero sets a Python exception in type_caster::load (#1976) 805c5862 Adding method names to cpp_function constructor calls in enum_base d54d6d8c Adding pybind11::cast overload for rvalue references (#1260) f980d76d Change NAMESPACE_* macros into PYBIND11_NAMESPACE_* (#2283) 964ab956 Fix DeprecationWarning about 'invalid escape sequence' in tools/docs Python scripts (#2281) 3e448c0b Enable py::ellipsis on Python 2 (#2360) 4493751a Fix new-style __init__ usage in numpy docs (#2426) b3d8fec0 Adapt code example in advanced/classes.rst to new handling of forgetting to call the superclass __init__ (#2429) 43f390ad Add note that VS2017 requires /permissive- to build in C++17 mode (#2431) a2bb297b Throw exception on returning a unique_ptr or shared_ptr nullptr (or any other holder type) from py::init, rather than crashing (#2430) 03b3d59d tests: fix CI by including to stop MSVC from complaining about std::count_if in tests/test_sequences_and_iterators.cpp (#2435) 6a192781 Fix bug roundtripping datetime.time objects after midnight in eastern hemisphere timezones (#2417) (#2438) fe9ee86b Add check if `str(handle)` correctly converted the object, and throw py::error_already_set if not (bis) (#2477) 9df13835 Stop py::array_t arguments from accepting arrays that do not match the C- or F-contiguity flags (#2484) d65e34d6 Resolve empty statement warning when using PYBIND11_OVERLOAD_PURE_NAME and PYBIND11_OVERLOAD_PURE (#2325) 16f199f8 Change base parameter type in register_exception and exception constructor from PyObject* to handle (#2467) c72708a7 Moving tp_class access, and consistent fully-qualified naming for PyPy, to detail::get_tp_name (#2520) 07b069a5 Unify Python 2 & 3 py::module constructor, and make contructor with pre-allocated PyModuleDef private (#2534) 56784c4f Add unchecked_reference::operator() and operator[] to overload resolution of unchecked_mutable_reference (#2514) 9796fe98 feat: vectorize functions with void return type (#1969) b70894df docs: add std::valarray to docs/advanced/cast/stl.rst (#2547) 14112077 chore: drop support for PyPy < 7.3.1 and clean up old PyPy workarounds (#2456) f537093a Fail on passing py::object with wrong Python type to py::object subclass using PYBIND11_OBJECT macro (#2349) 71aea49b Check scope's __dict__ instead of using hasattr when registering classes and exceptions (#2335) 0c5cc031 feat: deprecate public constructors of module_ class (#2552) 1914b7d3 Shorten PYBIND11_EMBEDDED_MODULE macro implementation by using PYBIND11_CATCH_INIT_EXCEPTIONS (#2579) 99773fc5 fix: throw error_already_set in py::len on failing PyObject_Length (#2575) 493649f9 fix: valgrind-detected after-freeing access of PyMethodDef (macOS Python 3.9.0 segfaults) (#2576) 6364b732 fix: test_factory_constructors.py failure triggered by test_register_duplicate_class (#2564) 3a37d338 Add __builtins__ to globals argument of `py::exec` and `py::eval` if not present (#2616) acxz (1): 6f6e9394 feat: add uninstall target for CMake (#2265) andriish (2): 38370a87 fix: support NVIDIA-PGI HPC SDK (#2475) 59323a97 ci: update PGI to 20.9 (#2566) fatvlady (4): a3daf87d Add failing optional test 556277d6 Fix optional dereference type deduction 8c06b954 Fix compile error on GCC4.8 f99ff736 Add additional check to be more compliant with other casters fwjavox (1): e97c735f stl_bind: add binding for std::vector::clear (#2074) jbarlow83 (3): 4d90f1a1 Add error_scope to py::class_::dealloc() to protect destructor calls (#2342) b8863698 Improve documentation of Python and C++ exceptions (#2408) 49cdb70a docs: use sorted(glob()) in example setup.py (#2561) marc-chiesa (1): 830adda8 Modified Vector STL bind initialization from a buffer type with optimization for simple arrays (#2298) methylDragon (1): d96c3451 Fix docs typo michalsustr (1): 3bd0d7a8 Add note about specifying custom base class for Exceptions. (#2465) nicolov (1): de5a29c0 Fix build with -Wmissing-prototypes (#1954) peter (1): 03f9e4a8 Fix compilation with clang-cl tuxmaster5000 (1): 7e0a4fb7 Fix for #2168 (#2299) xkszltl (1): 134a2ec3 Add `.git` suffix to URL because only GitHub itself works without it. (#2319) --- include/pybind11/attr.h | 78 +++- include/pybind11/buffer_info.h | 80 +++-- include/pybind11/cast.h | 233 ++++++++---- include/pybind11/chrono.h | 33 +- include/pybind11/complex.h | 8 +- include/pybind11/detail/class.h | 132 +++++-- include/pybind11/detail/common.h | 149 +++++--- include/pybind11/detail/descr.h | 8 +- include/pybind11/detail/init.h | 13 +- include/pybind11/detail/internals.h | 109 +++--- include/pybind11/detail/typeid.h | 8 +- include/pybind11/eigen.h | 14 +- include/pybind11/embed.h | 53 +-- include/pybind11/eval.h | 41 ++- include/pybind11/functional.h | 13 +- include/pybind11/iostream.h | 35 +- include/pybind11/numpy.h | 209 +++++++---- include/pybind11/operators.h | 13 +- include/pybind11/options.h | 4 +- include/pybind11/pybind11.h | 537 +++++++++++++++++++--------- include/pybind11/pytypes.h | 297 +++++++++++---- include/pybind11/stl.h | 18 +- include/pybind11/stl_bind.h | 40 ++- 23 files changed, 1477 insertions(+), 648 deletions(-) diff --git a/include/pybind11/attr.h b/include/pybind11/attr.h index 6962d6fc53..0c41670926 100644 --- a/include/pybind11/attr.h +++ b/include/pybind11/attr.h @@ -12,7 +12,7 @@ #include "cast.h" -NAMESPACE_BEGIN(PYBIND11_NAMESPACE) +PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE) /// \addtogroup annotations /// @{ @@ -23,6 +23,9 @@ struct is_method { handle class_; is_method(const handle &c) : class_(c) { } }; /// Annotation for operators struct is_operator { }; +/// Annotation for classes that cannot be subclassed +struct is_final { }; + /// Annotation for parent scope struct scope { handle value; scope(const handle &s) : value(s) { } }; @@ -37,8 +40,9 @@ struct sibling { handle value; sibling(const handle &value) : value(value.ptr()) /// Annotation indicating that a class derives from another given type template struct base { + PYBIND11_DEPRECATED("base() was deprecated in favor of specifying 'T' as a template argument to class_") - base() { } + base() { } // NOLINT(modernize-use-equals-default): breaks MSVC 2015 when adding an attribute }; /// Keep patient alive while nurse lives @@ -58,7 +62,7 @@ struct metaclass { handle value; PYBIND11_DEPRECATED("py::metaclass() is no longer required. It's turned on by default now.") - metaclass() {} + metaclass() { } // NOLINT(modernize-use-equals-default): breaks MSVC 2015 when adding an attribute /// Override pybind11's default metaclass explicit metaclass(handle value) : value(value) { } @@ -70,6 +74,9 @@ struct module_local { const bool value; constexpr module_local(bool v = true) : /// Annotation to mark enums as an arithmetic type struct arithmetic { }; +/// Mark a function for addition at the beginning of the existing overload chain instead of the end +struct prepend { }; + /** \rst A call policy which places one or more guard variables (``Ts...``) around the function call. @@ -110,7 +117,7 @@ struct call_guard { /// @} annotations -NAMESPACE_BEGIN(detail) +PYBIND11_NAMESPACE_BEGIN(detail) /* Forward declarations */ enum op_id : int; enum op_type : int; @@ -134,7 +141,8 @@ struct argument_record { struct function_record { function_record() : is_constructor(false), is_new_style_constructor(false), is_stateless(false), - is_operator(false), has_args(false), has_kwargs(false), is_method(false) { } + is_operator(false), is_method(false), has_args(false), + has_kwargs(false), has_kw_only_args(false), prepend(false) { } /// Function name char *name = nullptr; /* why no C++ strings? They generate heavier code.. */ @@ -172,18 +180,30 @@ struct function_record { /// True if this is an operator (__add__), etc. bool is_operator : 1; + /// True if this is a method + bool is_method : 1; + /// True if the function has a '*args' argument bool has_args : 1; /// True if the function has a '**kwargs' argument bool has_kwargs : 1; - /// True if this is a method - bool is_method : 1; + /// True once a 'py::kw_only' is encountered (any following args are keyword-only) + bool has_kw_only_args : 1; + + /// True if this function is to be inserted at the beginning of the overload resolution chain + bool prepend : 1; /// Number of arguments (including py::args and/or py::kwargs, if present) std::uint16_t nargs; + /// Number of trailing arguments (counted in `nargs`) that are keyword-only + std::uint16_t nargs_kw_only = 0; + + /// Number of leading arguments (counted in `nargs`) that are positional-only + std::uint16_t nargs_pos_only = 0; + /// Python method object PyMethodDef *def = nullptr; @@ -201,7 +221,7 @@ struct function_record { struct type_record { PYBIND11_NOINLINE type_record() : multiple_inheritance(false), dynamic_attr(false), buffer_protocol(false), - default_holder(true), module_local(false) { } + default_holder(true), module_local(false), is_final(false) { } /// Handle to the parent scope handle scope; @@ -254,6 +274,9 @@ struct type_record { /// Is the class definition local to the module shared object? bool module_local : 1; + /// Is the class inheritable from python classes? + bool is_final : 1; + PYBIND11_NOINLINE void add_base(const std::type_info &base, void *(*caster)(void *)) { auto base_info = detail::get_type_info(base, false); if (!base_info) { @@ -353,12 +376,20 @@ template <> struct process_attribute : process_attribu static void init(const is_new_style_constructor &, function_record *r) { r->is_new_style_constructor = true; } }; +inline void process_kw_only_arg(const arg &a, function_record *r) { + if (!a.name || strlen(a.name) == 0) + pybind11_fail("arg(): cannot specify an unnamed argument after an kw_only() annotation"); + ++r->nargs_kw_only; +} + /// Process a keyword argument attribute (*without* a default value) template <> struct process_attribute : process_attribute_default { static void init(const arg &a, function_record *r) { if (r->is_method && r->args.empty()) r->args.emplace_back("self", nullptr, handle(), true /*convert*/, false /*none not allowed*/); r->args.emplace_back(a.name, nullptr, handle(), !a.flag_noconvert, a.flag_none); + + if (r->has_kw_only_args) process_kw_only_arg(a, r); } }; @@ -390,6 +421,22 @@ template <> struct process_attribute : process_attribute_default { #endif } r->args.emplace_back(a.name, a.descr, a.value.inc_ref(), !a.flag_noconvert, a.flag_none); + + if (r->has_kw_only_args) process_kw_only_arg(a, r); + } +}; + +/// Process a keyword-only-arguments-follow pseudo argument +template <> struct process_attribute : process_attribute_default { + static void init(const kw_only &, function_record *r) { + r->has_kw_only_args = true; + } +}; + +/// Process a positional-only-argument maker +template <> struct process_attribute : process_attribute_default { + static void init(const pos_only &, function_record *r) { + r->nargs_pos_only = static_cast(r->args.size()); } }; @@ -416,6 +463,11 @@ struct process_attribute : process_attribute_default static void init(const dynamic_attr &, type_record *r) { r->dynamic_attr = true; } }; +template <> +struct process_attribute : process_attribute_default { + static void init(const is_final &, type_record *r) { r->is_final = true; } +}; + template <> struct process_attribute : process_attribute_default { static void init(const buffer_protocol &, type_record *r) { r->buffer_protocol = true; } @@ -431,6 +483,12 @@ struct process_attribute : process_attribute_default static void init(const module_local &l, type_record *r) { r->module_local = l.value; } }; +/// Process a 'prepend' attribute, putting this at the beginning of the overload chain +template <> +struct process_attribute : process_attribute_default { + static void init(const prepend &, function_record *r) { r->prepend = true; } +}; + /// Process an 'arithmetic' attribute for enums (does nothing here) template <> struct process_attribute : process_attribute_default {}; @@ -489,5 +547,5 @@ constexpr bool expected_num_args(size_t nargs, bool has_args, bool has_kwargs) { return named == 0 || (self + named + has_args + has_kwargs) == nargs; } -NAMESPACE_END(detail) -NAMESPACE_END(PYBIND11_NAMESPACE) +PYBIND11_NAMESPACE_END(detail) +PYBIND11_NAMESPACE_END(PYBIND11_NAMESPACE) diff --git a/include/pybind11/buffer_info.h b/include/pybind11/buffer_info.h index 9f072fa738..d803004a10 100644 --- a/include/pybind11/buffer_info.h +++ b/include/pybind11/buffer_info.h @@ -11,7 +11,30 @@ #include "detail/common.h" -NAMESPACE_BEGIN(PYBIND11_NAMESPACE) +PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE) + +PYBIND11_NAMESPACE_BEGIN(detail) + +// Default, C-style strides +inline std::vector c_strides(const std::vector &shape, ssize_t itemsize) { + auto ndim = shape.size(); + std::vector strides(ndim, itemsize); + if (ndim > 0) + for (size_t i = ndim - 1; i > 0; --i) + strides[i - 1] = strides[i] * shape[i]; + return strides; +} + +// F-style strides; default when constructing an array_t with `ExtraFlags & f_style` +inline std::vector f_strides(const std::vector &shape, ssize_t itemsize) { + auto ndim = shape.size(); + std::vector strides(ndim, itemsize); + for (size_t i = 1; i < ndim; ++i) + strides[i] = strides[i - 1] * shape[i - 1]; + return strides; +} + +PYBIND11_NAMESPACE_END(detail) /// Information record describing a Python buffer object struct buffer_info { @@ -21,14 +44,15 @@ struct buffer_info { std::string format; // For homogeneous buffers, this should be set to format_descriptor::format() ssize_t ndim = 0; // Number of dimensions std::vector shape; // Shape of the tensor (1 entry per dimension) - std::vector strides; // Number of entries between adjacent entries (for each per dimension) + std::vector strides; // Number of bytes between adjacent entries (for each per dimension) + bool readonly = false; // flag to indicate if the underlying storage may be written to - buffer_info() { } + buffer_info() = default; buffer_info(void *ptr, ssize_t itemsize, const std::string &format, ssize_t ndim, - detail::any_container shape_in, detail::any_container strides_in) + detail::any_container shape_in, detail::any_container strides_in, bool readonly=false) : ptr(ptr), itemsize(itemsize), size(1), format(format), ndim(ndim), - shape(std::move(shape_in)), strides(std::move(strides_in)) { + shape(std::move(shape_in)), strides(std::move(strides_in)), readonly(readonly) { if (ndim != (ssize_t) shape.size() || ndim != (ssize_t) strides.size()) pybind11_fail("buffer_info: ndim doesn't match shape and/or strides length"); for (size_t i = 0; i < (size_t) ndim; ++i) @@ -36,20 +60,31 @@ struct buffer_info { } template - buffer_info(T *ptr, detail::any_container shape_in, detail::any_container strides_in) - : buffer_info(private_ctr_tag(), ptr, sizeof(T), format_descriptor::format(), static_cast(shape_in->size()), std::move(shape_in), std::move(strides_in)) { } + buffer_info(T *ptr, detail::any_container shape_in, detail::any_container strides_in, bool readonly=false) + : buffer_info(private_ctr_tag(), ptr, sizeof(T), format_descriptor::format(), static_cast(shape_in->size()), std::move(shape_in), std::move(strides_in), readonly) { } - buffer_info(void *ptr, ssize_t itemsize, const std::string &format, ssize_t size) - : buffer_info(ptr, itemsize, format, 1, {size}, {itemsize}) { } + buffer_info(void *ptr, ssize_t itemsize, const std::string &format, ssize_t size, bool readonly=false) + : buffer_info(ptr, itemsize, format, 1, {size}, {itemsize}, readonly) { } template - buffer_info(T *ptr, ssize_t size) - : buffer_info(ptr, sizeof(T), format_descriptor::format(), size) { } + buffer_info(T *ptr, ssize_t size, bool readonly=false) + : buffer_info(ptr, sizeof(T), format_descriptor::format(), size, readonly) { } + + template + buffer_info(const T *ptr, ssize_t size, bool readonly=true) + : buffer_info(const_cast(ptr), sizeof(T), format_descriptor::format(), size, readonly) { } explicit buffer_info(Py_buffer *view, bool ownview = true) : buffer_info(view->buf, view->itemsize, view->format, view->ndim, - {view->shape, view->shape + view->ndim}, {view->strides, view->strides + view->ndim}) { - this->view = view; + {view->shape, view->shape + view->ndim}, + /* Though buffer::request() requests PyBUF_STRIDES, ctypes objects + * ignore this flag and return a view with NULL strides. + * When strides are NULL, build them manually. */ + view->strides + ? std::vector(view->strides, view->strides + view->ndim) + : detail::c_strides({view->shape, view->shape + view->ndim}, view->itemsize), + view->readonly) { + this->m_view = view; this->ownview = ownview; } @@ -68,27 +103,30 @@ struct buffer_info { ndim = rhs.ndim; shape = std::move(rhs.shape); strides = std::move(rhs.strides); - std::swap(view, rhs.view); + std::swap(m_view, rhs.m_view); std::swap(ownview, rhs.ownview); + readonly = rhs.readonly; return *this; } ~buffer_info() { - if (view && ownview) { PyBuffer_Release(view); delete view; } + if (m_view && ownview) { PyBuffer_Release(m_view); delete m_view; } } + Py_buffer *view() const { return m_view; } + Py_buffer *&view() { return m_view; } private: struct private_ctr_tag { }; buffer_info(private_ctr_tag, void *ptr, ssize_t itemsize, const std::string &format, ssize_t ndim, - detail::any_container &&shape_in, detail::any_container &&strides_in) - : buffer_info(ptr, itemsize, format, ndim, std::move(shape_in), std::move(strides_in)) { } + detail::any_container &&shape_in, detail::any_container &&strides_in, bool readonly) + : buffer_info(ptr, itemsize, format, ndim, std::move(shape_in), std::move(strides_in), readonly) { } - Py_buffer *view = nullptr; + Py_buffer *m_view = nullptr; bool ownview = false; }; -NAMESPACE_BEGIN(detail) +PYBIND11_NAMESPACE_BEGIN(detail) template struct compare_buffer_info { static bool compare(const buffer_info& b) { @@ -104,5 +142,5 @@ template struct compare_buffer_info #endif -NAMESPACE_BEGIN(PYBIND11_NAMESPACE) -NAMESPACE_BEGIN(detail) +#if defined(__cpp_lib_char8_t) && __cpp_lib_char8_t >= 201811L +# define PYBIND11_HAS_U8STRING +#endif + +PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE) +PYBIND11_NAMESPACE_BEGIN(detail) /// A life support system for temporary objects created by `type_caster::load()`. /// Adding a patient will keep it alive up until the enclosing function returns. @@ -55,7 +59,7 @@ class loader_life_support { Py_CLEAR(ptr); // A heuristic to reduce the stack's capacity (e.g. after long recursive calls) - if (stack.capacity() > 16 && stack.size() != 0 && stack.capacity() / stack.size() > 2) + if (stack.capacity() > 16 && !stack.empty() && stack.capacity() / stack.size() > 2) stack.shrink_to_fit(); } @@ -159,7 +163,7 @@ inline const std::vector &all_type_info(PyTypeObject *type) */ PYBIND11_NOINLINE inline detail::type_info* get_type_info(PyTypeObject *type) { auto &bases = all_type_info(type); - if (bases.size() == 0) + if (bases.empty()) return nullptr; if (bases.size() > 1) pybind11_fail("pybind11::detail::get_type_info: type has multiple pybind11-registered bases"); @@ -216,7 +220,7 @@ struct value_and_holder { {} // Default constructor (used to signal a value-and-holder not found by get_value_and_holder()) - value_and_holder() {} + value_and_holder() = default; // Used for past-the-end iterator value_and_holder(size_t index) : index{index} {} @@ -284,8 +288,8 @@ struct values_and_holders { // Past-the-end iterator: iterator(size_t end) : curr(end) {} public: - bool operator==(const iterator &other) { return curr.index == other.curr.index; } - bool operator!=(const iterator &other) { return curr.index != other.curr.index; } + bool operator==(const iterator &other) const { return curr.index == other.curr.index; } + bool operator!=(const iterator &other) const { return curr.index != other.curr.index; } iterator &operator++() { if (!inst->simple_layout) curr.vh += 1 + (*types)[curr.index]->holder_size_in_ptrs; @@ -338,8 +342,8 @@ PYBIND11_NOINLINE inline value_and_holder instance::get_value_and_holder(const t "(compile in debug mode for type details)"); #else pybind11_fail("pybind11::detail::instance::get_value_and_holder: `" + - std::string(find_type->type->tp_name) + "' is not a pybind11 base of the given `" + - std::string(Py_TYPE(this)->tp_name) + "' instance"); + get_fully_qualified_tp_name(find_type->type) + "' is not a pybind11 base of the given `" + + get_fully_qualified_tp_name(Py_TYPE(this)) + "' instance"); #endif } @@ -428,7 +432,7 @@ PYBIND11_NOINLINE inline std::string error_string() { #if !defined(PYPY_VERSION) if (scope.trace) { - PyTracebackObject *trace = (PyTracebackObject *) scope.trace; + auto *trace = (PyTracebackObject *) scope.trace; /* Get the deepest trace possible */ while (trace->tb_next) @@ -454,7 +458,7 @@ PYBIND11_NOINLINE inline handle get_object_handle(const void *ptr, const detail: auto &instances = get_internals().registered_instances; auto range = instances.equal_range(ptr); for (auto it = range.first; it != range.second; ++it) { - for (auto vh : values_and_holders(it->second)) { + for (const auto &vh : values_and_holders(it->second)) { if (vh.type == type) return handle((PyObject *) it->second); } @@ -533,9 +537,17 @@ class type_caster_generic { case return_value_policy::copy: if (copy_constructor) valueptr = copy_constructor(src); - else - throw cast_error("return_value_policy = copy, but the " - "object is non-copyable!"); + else { +#if defined(NDEBUG) + throw cast_error("return_value_policy = copy, but type is " + "non-copyable! (compile in debug mode for details)"); +#else + std::string type_name(tinfo->cpptype->name()); + detail::clean_type_id(type_name); + throw cast_error("return_value_policy = copy, but type " + + type_name + " is non-copyable!"); +#endif + } wrapper->owned = true; break; @@ -544,9 +556,18 @@ class type_caster_generic { valueptr = move_constructor(src); else if (copy_constructor) valueptr = copy_constructor(src); - else - throw cast_error("return_value_policy = move, but the " - "object is neither movable nor copyable!"); + else { +#if defined(NDEBUG) + throw cast_error("return_value_policy = move, but type is neither " + "movable nor copyable! " + "(compile in debug mode for details)"); +#else + std::string type_name(tinfo->cpptype->name()); + detail::clean_type_id(type_name); + throw cast_error("return_value_policy = move, but type " + + type_name + " is neither movable nor copyable!"); +#endif + } wrapper->owned = true; break; @@ -574,10 +595,10 @@ class type_caster_generic { if (type->operator_new) { vptr = type->operator_new(type->type_size); } else { - #if defined(PYBIND11_CPP17) + #if defined(__cpp_aligned_new) && (!defined(_MSC_VER) || _MSC_VER >= 1912) if (type->type_align > __STDCPP_DEFAULT_NEW_ALIGNMENT__) vptr = ::operator new(type->type_size, - (std::align_val_t) type->type_align); + std::align_val_t(type->type_align)); else #endif vptr = ::operator new(type->type_size); @@ -615,7 +636,7 @@ class type_caster_generic { /// native typeinfo, or when the native one wasn't able to produce a value. PYBIND11_NOINLINE bool try_load_foreign_module_local(handle src) { constexpr auto *local_key = PYBIND11_MODULE_LOCAL_ID; - const auto pytype = src.get_type(); + const auto pytype = type::handle_of(src); if (!hasattr(pytype, local_key)) return false; @@ -780,14 +801,22 @@ template struct is_copy_constructible> >::value>> : is_copy_constructible {}; -#if !defined(PYBIND11_CPP17) -// Likewise for std::pair before C++17 (which mandates that the copy constructor not exist when the -// two types aren't themselves copy constructible). +// Likewise for std::pair +// (after C++17 it is mandatory that the copy constructor not exist when the two types aren't themselves +// copy constructible, but this can not be relied upon when T1 or T2 are themselves containers). template struct is_copy_constructible> : all_of, is_copy_constructible> {}; -#endif -NAMESPACE_END(detail) +// The same problems arise with std::is_copy_assignable, so we use the same workaround. +template struct is_copy_assignable : std::is_copy_assignable {}; +template struct is_copy_assignable, + std::is_same + >::value>> : is_copy_assignable {}; +template struct is_copy_assignable> + : all_of, is_copy_assignable> {}; + +PYBIND11_NAMESPACE_END(detail) // polymorphic_type_hook::get(src, tinfo) determines whether the object pointed // to by `src` actually is an instance of some class derived from `itype`. @@ -806,21 +835,27 @@ NAMESPACE_END(detail) // You may specialize polymorphic_type_hook yourself for types that want to appear // polymorphic to Python but do not use C++ RTTI. (This is a not uncommon pattern // in performance-sensitive applications, used most notably in LLVM.) +// +// polymorphic_type_hook_base allows users to specialize polymorphic_type_hook with +// std::enable_if. User provided specializations will always have higher priority than +// the default implementation and specialization provided in polymorphic_type_hook_base. template -struct polymorphic_type_hook +struct polymorphic_type_hook_base { static const void *get(const itype *src, const std::type_info*&) { return src; } }; template -struct polymorphic_type_hook::value>> +struct polymorphic_type_hook_base::value>> { static const void *get(const itype *src, const std::type_info*& type) { type = src ? &typeid(*src) : nullptr; return dynamic_cast(src); } }; +template +struct polymorphic_type_hook : public polymorphic_type_hook_base {}; -NAMESPACE_BEGIN(detail) +PYBIND11_NAMESPACE_BEGIN(detail) /// Generic type caster for objects stored on the heap template class type_caster_base : public type_caster_generic { @@ -963,11 +998,15 @@ template class type_caster> { template using is_std_char_type = any_of< std::is_same, /* std::string */ +#if defined(PYBIND11_HAS_U8STRING) + std::is_same, /* std::u8string */ +#endif std::is_same, /* std::u16string */ std::is_same, /* std::u32string */ std::is_same /* std::wstring */ >; + template struct type_caster::value && !is_std_char_type::value>> { using _py_type_0 = conditional_t; @@ -996,12 +1035,12 @@ struct type_caster::value && !is_std_char_t : (py_type) PYBIND11_LONG_AS_LONGLONG(src.ptr()); } + // Python API reported an error bool py_err = py_value == (py_type) -1 && PyErr_Occurred(); - // Protect std::numeric_limits::min/max with parentheses - if (py_err || (std::is_integral::value && sizeof(py_type) != sizeof(T) && - (py_value < (py_type) (std::numeric_limits::min)() || - py_value > (py_type) (std::numeric_limits::max)()))) { + // Check to see if the conversion is valid (integers should match exactly) + // Signed/unsigned checks happen elsewhere + if (py_err || (std::is_integral::value && sizeof(py_type) != sizeof(T) && py_value != (py_type) (T) py_value)) { bool type_error = py_err && PyErr_ExceptionMatches( #if PY_VERSION_HEX < 0x03000000 && !defined(PYPY_VERSION) PyExc_SystemError @@ -1091,7 +1130,7 @@ template <> class type_caster : public type_caster { } /* Check if this is a C++ type */ - auto &bases = all_type_info((PyTypeObject *) h.get_type().ptr()); + auto &bases = all_type_info((PyTypeObject *) type::handle_of(h).ptr()); if (bases.size() == 1) { // Only allowing loading from a single-value type value = values_and_holders(reinterpret_cast(h.ptr())).begin()->value_ptr(); return true; @@ -1147,6 +1186,8 @@ template <> class type_caster { if (res == 0 || res == 1) { value = (bool) res; return true; + } else { + PyErr_Clear(); } } return false; @@ -1164,6 +1205,9 @@ template struct string_caster { // Simplify life by being able to assume standard char sizes (the standard only guarantees // minimums, but Python requires exact sizes) static_assert(!std::is_same::value || sizeof(CharT) == 1, "Unsupported char size != 1"); +#if defined(PYBIND11_HAS_U8STRING) + static_assert(!std::is_same::value || sizeof(CharT) == 1, "Unsupported char8_t size != 1"); +#endif static_assert(!std::is_same::value || sizeof(CharT) == 2, "Unsupported char16_t size != 2"); static_assert(!std::is_same::value || sizeof(CharT) == 4, "Unsupported char32_t size != 4"); // wchar_t can be either 16 bits (Windows) or 32 (everywhere else) @@ -1182,7 +1226,7 @@ template struct string_caster { #if PY_MAJOR_VERSION >= 3 return load_bytes(load_src); #else - if (sizeof(CharT) == 1) { + if (std::is_same::value) { return load_bytes(load_src); } @@ -1200,7 +1244,7 @@ template struct string_caster { load_src.ptr(), UTF_N == 8 ? "utf-8" : UTF_N == 16 ? "utf-16" : "utf-32", nullptr)); if (!utfNbytes) { PyErr_Clear(); return false; } - const CharT *buffer = reinterpret_cast(PYBIND11_BYTES_AS_STRING(utfNbytes.ptr())); + const auto *buffer = reinterpret_cast(PYBIND11_BYTES_AS_STRING(utfNbytes.ptr())); size_t length = (size_t) PYBIND11_BYTES_SIZE(utfNbytes.ptr()) / sizeof(CharT); if (UTF_N > 8) { buffer++; length--; } // Skip BOM for UTF-16/32 value = StringType(buffer, length); @@ -1214,7 +1258,7 @@ template struct string_caster { static handle cast(const StringType &src, return_value_policy /* policy */, handle /* parent */) { const char *buffer = reinterpret_cast(src.data()); - ssize_t nbytes = ssize_t(src.size() * sizeof(CharT)); + auto nbytes = ssize_t(src.size() * sizeof(CharT)); handle s = decode_utfN(buffer, nbytes); if (!s) throw error_already_set(); return s; @@ -1230,10 +1274,8 @@ template struct string_caster { UTF_N == 16 ? PyUnicode_DecodeUTF16(buffer, nbytes, nullptr, nullptr) : PyUnicode_DecodeUTF32(buffer, nbytes, nullptr, nullptr); #else - // PyPy seems to have multiple problems related to PyUnicode_UTF*: the UTF8 version - // sometimes segfaults for unknown reasons, while the UTF16 and 32 versions require a - // non-const char * arguments, which is also a nuisance, so bypass the whole thing by just - // passing the encoding as a string value, which works properly: + // PyPy segfaults when on PyUnicode_DecodeUTF16 (and possibly on PyUnicode_DecodeUTF32 as well), + // so bypass the whole thing by just passing the encoding as a string value, which works properly: return PyUnicode_Decode(buffer, nbytes, UTF_N == 8 ? "utf-8" : UTF_N == 16 ? "utf-16" : "utf-32", nullptr); #endif } @@ -1242,7 +1284,7 @@ template struct string_caster { // without any encoding/decoding attempt). For other C++ char sizes this is a no-op. // which supports loading a unicode from a str, doesn't take this path. template - bool load_bytes(enable_if_t src) { + bool load_bytes(enable_if_t::value, handle> src) { if (PYBIND11_BYTES_CHECK(src.ptr())) { // We were passed a Python 3 raw bytes; accept it into a std::string or char* // without any encoding attempt. @@ -1257,7 +1299,7 @@ template struct string_caster { } template - bool load_bytes(enable_if_t) { return false; } + bool load_bytes(enable_if_t::value, handle>) { return false; } }; template @@ -1320,7 +1362,7 @@ template struct type_caster 1 && str_len <= 4) { - unsigned char v0 = static_cast(value[0]); + auto v0 = static_cast(value[0]); size_t char0_bytes = !(v0 & 0x80) ? 1 : // low bits only: 0-127 (v0 & 0xE0) == 0xC0 ? 2 : // 0b110xxxxx - start of 2-byte sequence (v0 & 0xF0) == 0xE0 ? 3 : // 0b1110xxxx - start of 3-byte sequence @@ -1378,6 +1420,17 @@ template class Tuple, typename... Ts> class tuple_caster return cast_impl(std::forward(src), policy, parent, indices{}); } + // copied from the PYBIND11_TYPE_CASTER macro + template + static handle cast(T *src, return_value_policy policy, handle parent) { + if (!src) return none().release(); + if (policy == return_value_policy::take_ownership) { + auto h = cast(std::move(*src), policy, parent); delete src; return h; + } else { + return cast(*src, policy, parent); + } + } + static constexpr auto name = _("Tuple[") + concat(make_caster::name...) + _("]"); template using cast_op_type = type; @@ -1395,9 +1448,14 @@ template class Tuple, typename... Ts> class tuple_caster template bool load_impl(const sequence &seq, bool convert, index_sequence) { +#ifdef __cpp_fold_expressions + if ((... || !std::get(subcasters).load(seq[Is], convert))) + return false; +#else for (bool r : {std::get(subcasters).load(seq[Is], convert)...}) if (!r) return false; +#endif return true; } @@ -1450,16 +1508,11 @@ struct copyable_holder_caster : public type_caster_base { } explicit operator type*() { return this->value; } - explicit operator type&() { return *(this->value); } + // static_cast works around compiler error with MSVC 17 and CUDA 10.2 + // see issue #2180 + explicit operator type&() { return *(static_cast(this->value)); } explicit operator holder_type*() { return std::addressof(holder); } - - // Workaround for Intel compiler bug - // see pybind11 issue 94 - #if defined(__ICC) || defined(__INTEL_COMPILER) - operator holder_type&() { return holder; } - #else explicit operator holder_type&() { return holder; } - #endif static handle cast(const holder_type &src, return_value_policy, handle) { const auto *ptr = holder_helper::get(src); @@ -1556,6 +1609,10 @@ template struct is_holder_type struct handle_type_name { static constexpr auto name = _(); }; template <> struct handle_type_name { static constexpr auto name = _(PYBIND11_BYTES_NAME); }; +template <> struct handle_type_name { static constexpr auto name = _("int"); }; +template <> struct handle_type_name { static constexpr auto name = _("Iterable"); }; +template <> struct handle_type_name { static constexpr auto name = _("Iterator"); }; +template <> struct handle_type_name { static constexpr auto name = _("None"); }; template <> struct handle_type_name { static constexpr auto name = _("*args"); }; template <> struct handle_type_name { static constexpr auto name = _("**kwargs"); }; @@ -1642,7 +1699,7 @@ template type_caster &load_type(type_ca throw cast_error("Unable to cast Python instance to C++ type (compile in debug mode for details)"); #else throw cast_error("Unable to cast Python instance of type " + - (std::string) str(handle.get_type()) + " to C++ type '" + type_id() + "'"); + (std::string) str(type::handle_of(handle)) + " to C++ type '" + type_id() + "'"); #endif } return conv; @@ -1654,7 +1711,7 @@ template make_caster load_type(const handle &handle) { return conv; } -NAMESPACE_END(detail) +PYBIND11_NAMESPACE_END(detail) // pytype -> C++ type template ::value, int> = 0> @@ -1671,13 +1728,16 @@ T cast(const handle &handle) { return T(reinterpret_borrow(handle)); } // C++ type -> py::object template ::value, int> = 0> -object cast(const T &value, return_value_policy policy = return_value_policy::automatic_reference, +object cast(T &&value, return_value_policy policy = return_value_policy::automatic_reference, handle parent = handle()) { + using no_ref_T = typename std::remove_reference::type; if (policy == return_value_policy::automatic) - policy = std::is_pointer::value ? return_value_policy::take_ownership : return_value_policy::copy; + policy = std::is_pointer::value ? return_value_policy::take_ownership : + std::is_lvalue_reference::value ? return_value_policy::copy : return_value_policy::move; else if (policy == return_value_policy::automatic_reference) - policy = std::is_pointer::value ? return_value_policy::reference : return_value_policy::copy; - return reinterpret_steal(detail::make_caster::cast(value, policy, parent)); + policy = std::is_pointer::value ? return_value_policy::reference : + std::is_lvalue_reference::value ? return_value_policy::copy : return_value_policy::move; + return reinterpret_steal(detail::make_caster::cast(std::forward(value), policy, parent)); } template T handle::cast() const { return pybind11::cast(*this); } @@ -1690,7 +1750,7 @@ detail::enable_if_t::value, T> move(object &&obj) { throw cast_error("Unable to cast Python instance to C++ rvalue: instance has multiple references" " (compile in debug mode for details)"); #else - throw cast_error("Unable to move from Python " + (std::string) str(obj.get_type()) + + throw cast_error("Unable to move from Python " + (std::string) str(type::handle_of(obj)) + " instance to C++ " + type_id() + " instance: instance has multiple references"); #endif @@ -1699,7 +1759,7 @@ detail::enable_if_t::value, T> move(object &&obj) { return ret; } -// Calling cast() on an rvalue calls pybind::cast with the object rvalue, which does: +// Calling cast() on an rvalue calls pybind11::cast with the object rvalue, which does: // - If we have to move (because T has no copy constructor), do it. This will fail if the moved // object has multiple references, but trying to copy will fail to compile. // - If both movable and copyable, check ref count: if 1, move; otherwise copy @@ -1722,22 +1782,22 @@ template T object::cast() && { return pybind11::cast(std::move(* template <> inline void object::cast() const & { return; } template <> inline void object::cast() && { return; } -NAMESPACE_BEGIN(detail) +PYBIND11_NAMESPACE_BEGIN(detail) // Declared in pytypes.h: template ::value, int>> object object_or_cast(T &&o) { return pybind11::cast(std::forward(o)); } -struct overload_unused {}; // Placeholder type for the unneeded (and dead code) static variable in the OVERLOAD_INT macro -template using overload_caster_t = conditional_t< - cast_is_temporary_value_reference::value, make_caster, overload_unused>; +struct override_unused {}; // Placeholder type for the unneeded (and dead code) static variable in the PYBIND11_OVERRIDE_OVERRIDE macro +template using override_caster_t = conditional_t< + cast_is_temporary_value_reference::value, make_caster, override_unused>; // Trampoline use: for reference/pointer types to value-converted values, we do a value cast, then // store the result in the given variable. For other types, this is a no-op. template enable_if_t::value, T> cast_ref(object &&o, make_caster &caster) { return cast_op(load_type(caster, o)); } -template enable_if_t::value, T> cast_ref(object &&, overload_unused &) { +template enable_if_t::value, T> cast_ref(object &&, override_unused &) { pybind11_fail("Internal error: cast_ref fallback invoked"); } // Trampoline use: Having a pybind11::cast with an invalid reference type is going to static_assert, even @@ -1749,7 +1809,7 @@ template enable_if_t::value, T pybind11_fail("Internal error: cast_safe fallback invoked"); } template <> inline void cast_safe(object &&) {} -NAMESPACE_END(detail) +PYBIND11_NAMESPACE_END(detail) template tuple make_tuple() { return tuple(0); } @@ -1839,6 +1899,16 @@ struct arg_v : arg { #endif }; +/// \ingroup annotations +/// Annotation indicating that all following arguments are keyword-only; the is the equivalent of an +/// unnamed '*' argument (in Python 3) +struct kw_only {}; + +/// \ingroup annotations +/// Annotation indicating that all previous arguments are positional-only; the is the equivalent of an +/// unnamed '/' argument (in Python 3.8) +struct pos_only {}; + template arg_v arg::operator=(T &&value) const { return {std::move(*this), std::forward(value)}; } @@ -1850,9 +1920,9 @@ inline namespace literals { String literal version of `arg` \endrst */ constexpr arg operator"" _a(const char *name, size_t) { return arg(name); } -} +} // namespace literals -NAMESPACE_BEGIN(detail) +PYBIND11_NAMESPACE_BEGIN(detail) // forward declaration (definition in attr.h) struct function_record; @@ -1924,14 +1994,19 @@ class argument_loader { template bool load_impl_sequence(function_call &call, index_sequence) { +#ifdef __cpp_fold_expressions + if ((... || !std::get(argcasters).load(call.args[Is], call.args_convert[Is]))) + return false; +#else for (bool r : {std::get(argcasters).load(call.args[Is], call.args_convert[Is])...}) if (!r) return false; +#endif return true; } template - Return call_impl(Func &&f, index_sequence, Guard &&) { + Return call_impl(Func &&f, index_sequence, Guard &&) && { return std::forward(f)(cast_op(std::move(std::get(argcasters)))...); } @@ -2008,7 +2083,7 @@ class unpacking_collector { } void process(list &args_list, detail::args_proxy ap) { - for (const auto &a : ap) + for (auto a : ap) args_list.append(a); } @@ -2040,7 +2115,7 @@ class unpacking_collector { void process(list &/*args_list*/, detail::kwargs_proxy kp) { if (!kp) return; - for (const auto &k : reinterpret_borrow(kp)) { + for (auto k : reinterpret_borrow(kp)) { if (m_kwargs.contains(k.first)) { #if defined(NDEBUG) multiple_values_error(); @@ -2118,7 +2193,19 @@ object object_api::call(Args &&...args) const { return operator()(std::forward(args)...); } -NAMESPACE_END(detail) +PYBIND11_NAMESPACE_END(detail) + + +template +handle type::handle_of() { + static_assert( + std::is_base_of>::value, + "py::type::of only supports the case where T is a registered C++ types." + ); + + return detail::get_type_handle(typeid(T), true); +} + #define PYBIND11_MAKE_OPAQUE(...) \ namespace pybind11 { namespace detail { \ @@ -2126,7 +2213,7 @@ NAMESPACE_END(detail) }} /// Lets you pass a type containing a `,` through a macro parameter without needing a separate -/// typedef, e.g.: `PYBIND11_OVERLOAD(PYBIND11_TYPE(ReturnType), PYBIND11_TYPE(Parent), f, arg)` +/// typedef, e.g.: `PYBIND11_OVERRIDE(PYBIND11_TYPE(ReturnType), PYBIND11_TYPE(Parent), f, arg)` #define PYBIND11_TYPE(...) __VA_ARGS__ -NAMESPACE_END(PYBIND11_NAMESPACE) +PYBIND11_NAMESPACE_END(PYBIND11_NAMESPACE) diff --git a/include/pybind11/chrono.h b/include/pybind11/chrono.h index ea777e6965..cbe9acec35 100644 --- a/include/pybind11/chrono.h +++ b/include/pybind11/chrono.h @@ -27,15 +27,15 @@ #define PyDateTime_DELTA_GET_MICROSECONDS(o) (((PyDateTime_Delta*)o)->microseconds) #endif -NAMESPACE_BEGIN(PYBIND11_NAMESPACE) -NAMESPACE_BEGIN(detail) +PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE) +PYBIND11_NAMESPACE_BEGIN(detail) template class duration_caster { public: typedef typename type::rep rep; - typedef typename type::period period; + using period = typename type::period; - typedef std::chrono::duration> days; + using days = std::chrono::duration>; bool load(handle src, bool) { using namespace std::chrono; @@ -98,7 +98,7 @@ template class duration_caster { // This is for casting times on the system clock into datetime.datetime instances template class type_caster> { public: - typedef std::chrono::time_point type; + using type = std::chrono::time_point; bool load(handle src, bool) { using namespace std::chrono; @@ -140,7 +140,7 @@ template class type_caster(system_clock::from_time_t(std::mktime(&cal)) + msecs); return true; } @@ -150,21 +150,28 @@ template class type_caster(src)); + // Get out microseconds, and make sure they are positive, to avoid bug in eastern hemisphere time zones + // (cfr. https://github.com/pybind/pybind11/issues/2417) + using us_t = duration; + auto us = duration_cast(src.time_since_epoch() % seconds(1)); + if (us.count() < 0) + us += seconds(1); + + // Subtract microseconds BEFORE `system_clock::to_time_t`, because: + // > If std::time_t has lower precision, it is implementation-defined whether the value is rounded or truncated. + // (https://en.cppreference.com/w/cpp/chrono/system_clock/to_time_t) + std::time_t tt = system_clock::to_time_t(time_point_cast(src - us)); // this function uses static memory so it's best to copy it out asap just in case // otherwise other code that is using localtime may break this (not just python code) std::tm localtime = *std::localtime(&tt); - // Declare these special duration types so the conversions happen with the correct primitive types (int) - using us_t = duration; - return PyDateTime_FromDateAndTime(localtime.tm_year + 1900, localtime.tm_mon + 1, localtime.tm_mday, localtime.tm_hour, localtime.tm_min, localtime.tm_sec, - (duration_cast(src.time_since_epoch() % seconds(1))).count()); + us.count()); } PYBIND11_TYPE_CASTER(type, _("datetime.datetime")); }; @@ -180,5 +187,5 @@ template class type_caster> { }; -NAMESPACE_END(detail) -NAMESPACE_END(PYBIND11_NAMESPACE) +PYBIND11_NAMESPACE_END(detail) +PYBIND11_NAMESPACE_END(PYBIND11_NAMESPACE) diff --git a/include/pybind11/complex.h b/include/pybind11/complex.h index 3f89638571..f8327eb373 100644 --- a/include/pybind11/complex.h +++ b/include/pybind11/complex.h @@ -17,7 +17,7 @@ # undef I #endif -NAMESPACE_BEGIN(PYBIND11_NAMESPACE) +PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE) template struct format_descriptor, detail::enable_if_t::value>> { static constexpr const char c = format_descriptor::c; @@ -32,7 +32,7 @@ template constexpr const char format_descriptor< #endif -NAMESPACE_BEGIN(detail) +PYBIND11_NAMESPACE_BEGIN(detail) template struct is_fmt_numeric, detail::enable_if_t::value>> { static constexpr bool value = true; @@ -61,5 +61,5 @@ template class type_caster> { PYBIND11_TYPE_CASTER(std::complex, _("complex")); }; -NAMESPACE_END(detail) -NAMESPACE_END(PYBIND11_NAMESPACE) +PYBIND11_NAMESPACE_END(detail) +PYBIND11_NAMESPACE_END(PYBIND11_NAMESPACE) diff --git a/include/pybind11/detail/class.h b/include/pybind11/detail/class.h index 230ae81ae8..569c5415d6 100644 --- a/include/pybind11/detail/class.h +++ b/include/pybind11/detail/class.h @@ -12,10 +12,10 @@ #include "../attr.h" #include "../options.h" -NAMESPACE_BEGIN(PYBIND11_NAMESPACE) -NAMESPACE_BEGIN(detail) +PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE) +PYBIND11_NAMESPACE_BEGIN(detail) -#if PY_VERSION_HEX >= 0x03030000 +#if PY_VERSION_HEX >= 0x03030000 && !defined(PYPY_VERSION) # define PYBIND11_BUILTIN_QUALNAME # define PYBIND11_SET_OLDPY_QUALNAME(obj, nameobj) #else @@ -24,6 +24,18 @@ NAMESPACE_BEGIN(detail) # define PYBIND11_SET_OLDPY_QUALNAME(obj, nameobj) setattr((PyObject *) obj, "__qualname__", nameobj) #endif +inline std::string get_fully_qualified_tp_name(PyTypeObject *type) { +#if !defined(PYPY_VERSION) + return type->tp_name; +#else + auto module_name = handle((PyObject *) type).attr("__module__").cast(); + if (module_name == PYBIND11_BUILTINS_MODULE) + return type->tp_name; + else + return std::move(module_name) + "." + type->tp_name; +#endif +} + inline PyTypeObject *type_incref(PyTypeObject *type) { Py_INCREF(type); return type; @@ -156,6 +168,69 @@ extern "C" inline PyObject *pybind11_meta_getattro(PyObject *obj, PyObject *name } #endif +/// metaclass `__call__` function that is used to create all pybind11 objects. +extern "C" inline PyObject *pybind11_meta_call(PyObject *type, PyObject *args, PyObject *kwargs) { + + // use the default metaclass call to create/initialize the object + PyObject *self = PyType_Type.tp_call(type, args, kwargs); + if (self == nullptr) { + return nullptr; + } + + // This must be a pybind11 instance + auto instance = reinterpret_cast(self); + + // Ensure that the base __init__ function(s) were called + for (const auto &vh : values_and_holders(instance)) { + if (!vh.holder_constructed()) { + PyErr_Format(PyExc_TypeError, "%.200s.__init__() must be called when overriding __init__", + get_fully_qualified_tp_name(vh.type->type).c_str()); + Py_DECREF(self); + return nullptr; + } + } + + return self; +} + +/// Cleanup the type-info for a pybind11-registered type. +extern "C" inline void pybind11_meta_dealloc(PyObject *obj) { + auto *type = (PyTypeObject *) obj; + auto &internals = get_internals(); + + // A pybind11-registered type will: + // 1) be found in internals.registered_types_py + // 2) have exactly one associated `detail::type_info` + auto found_type = internals.registered_types_py.find(type); + if (found_type != internals.registered_types_py.end() && + found_type->second.size() == 1 && + found_type->second[0]->type == type) { + + auto *tinfo = found_type->second[0]; + auto tindex = std::type_index(*tinfo->cpptype); + internals.direct_conversions.erase(tindex); + + if (tinfo->module_local) + registered_local_types_cpp().erase(tindex); + else + internals.registered_types_cpp.erase(tindex); + internals.registered_types_py.erase(tinfo->type); + + // Actually just `std::erase_if`, but that's only available in C++20 + auto &cache = internals.inactive_override_cache; + for (auto it = cache.begin(), last = cache.end(); it != last; ) { + if (it->first == (PyObject *) tinfo->type) + it = cache.erase(it); + else + ++it; + } + + delete tinfo; + } + + PyType_Type.tp_dealloc(obj); +} + /** This metaclass is assigned by default to all pybind11 types and is required in order for static properties to function correctly. Users may override this using `py::metaclass`. Return value: New reference. */ @@ -181,11 +256,15 @@ inline PyTypeObject* make_default_metaclass() { type->tp_base = type_incref(&PyType_Type); type->tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HEAPTYPE; + type->tp_call = pybind11_meta_call; + type->tp_setattro = pybind11_meta_setattro; #if PY_MAJOR_VERSION >= 3 type->tp_getattro = pybind11_meta_getattro; #endif + type->tp_dealloc = pybind11_meta_dealloc; + if (PyType_Ready(type) < 0) pybind11_fail("make_default_metaclass(): failure in PyType_Ready()!"); @@ -223,7 +302,7 @@ inline bool deregister_instance_impl(void *ptr, instance *self) { auto ®istered_instances = get_internals().registered_instances; auto range = registered_instances.equal_range(ptr); for (auto it = range.first; it != range.second; ++it) { - if (Py_TYPE(self) == Py_TYPE(it->second)) { + if (self == it->second) { registered_instances.erase(it); return true; } @@ -277,12 +356,7 @@ extern "C" inline PyObject *pybind11_object_new(PyTypeObject *type, PyObject *, /// following default function will be used which simply throws an exception. extern "C" inline int pybind11_object_init(PyObject *self, PyObject *, PyObject *) { PyTypeObject *type = Py_TYPE(self); - std::string msg; -#if defined(PYPY_VERSION) - msg += handle((PyObject *) type).attr("__module__").cast() + "."; -#endif - msg += type->tp_name; - msg += ": No constructor defined!"; + std::string msg = get_fully_qualified_tp_name(type) + ": No constructor defined!"; PyErr_SetString(PyExc_TypeError, msg.c_str()); return -1; } @@ -421,7 +495,7 @@ extern "C" inline PyObject *pybind11_get_dict(PyObject *self, void *) { extern "C" inline int pybind11_set_dict(PyObject *self, PyObject *new_dict, void *) { if (!PyDict_Check(new_dict)) { PyErr_Format(PyExc_TypeError, "__dict__ must be set to a dictionary, not a '%.200s'", - Py_TYPE(new_dict)->tp_name); + get_fully_qualified_tp_name(Py_TYPE(new_dict)).c_str()); return -1; } PyObject *&dict = *_PyObject_GetDictPtr(self); @@ -448,11 +522,6 @@ extern "C" inline int pybind11_clear(PyObject *self) { /// Give instances of this type a `__dict__` and opt into garbage collection. inline void enable_dynamic_attributes(PyHeapTypeObject *heap_type) { auto type = &heap_type->ht_type; -#if defined(PYPY_VERSION) - pybind11_fail(std::string(type->tp_name) + ": dynamic attributes are " - "currently not supported in " - "conjunction with PyPy!"); -#endif type->tp_flags |= Py_TPFLAGS_HAVE_GC; type->tp_dictoffset = type->tp_basicsize; // place dict at the end type->tp_basicsize += (ssize_t)sizeof(PyObject *); // and allocate enough space for it @@ -491,6 +560,13 @@ extern "C" inline int pybind11_getbuffer(PyObject *obj, Py_buffer *view, int fla view->len = view->itemsize; for (auto s : info->shape) view->len *= s; + view->readonly = info->readonly; + if ((flags & PyBUF_WRITABLE) == PyBUF_WRITABLE && info->readonly) { + if (view) + view->obj = nullptr; + PyErr_SetString(PyExc_BufferError, "Writable buffer requested for readonly storage"); + return -1; + } if ((flags & PyBUF_FORMAT) == PyBUF_FORMAT) view->format = const_cast(info->format.c_str()); if ((flags & PyBUF_STRIDES) == PyBUF_STRIDES) { @@ -533,17 +609,17 @@ inline PyObject* make_new_python_type(const type_record &rec) { #endif } - object module; + object module_; if (rec.scope) { if (hasattr(rec.scope, "__module__")) - module = rec.scope.attr("__module__"); + module_ = rec.scope.attr("__module__"); else if (hasattr(rec.scope, "__name__")) - module = rec.scope.attr("__name__"); + module_ = rec.scope.attr("__name__"); } auto full_name = c_str( #if !defined(PYPY_VERSION) - module ? str(module).cast() + "." + rec.name : + module_ ? str(module_).cast() + "." + rec.name : #endif rec.name); @@ -558,7 +634,7 @@ inline PyObject* make_new_python_type(const type_record &rec) { auto &internals = get_internals(); auto bases = tuple(rec.bases); - auto base = (bases.size() == 0) ? internals.instance_base + auto base = (bases.empty()) ? internals.instance_base : bases[0].ptr(); /* Danger zone: from now (and until PyType_Ready), make sure to @@ -582,7 +658,7 @@ inline PyObject* make_new_python_type(const type_record &rec) { type->tp_doc = tp_doc; type->tp_base = type_incref((PyTypeObject *)base); type->tp_basicsize = static_cast(sizeof(instance)); - if (bases.size() > 0) + if (!bases.empty()) type->tp_bases = bases.release().ptr(); /* Don't inherit base __init__ */ @@ -597,10 +673,12 @@ inline PyObject* make_new_python_type(const type_record &rec) { #endif /* Flags */ - type->tp_flags |= Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HEAPTYPE; + type->tp_flags |= Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HEAPTYPE; #if PY_MAJOR_VERSION < 3 type->tp_flags |= Py_TPFLAGS_CHECKTYPES; #endif + if (!rec.is_final) + type->tp_flags |= Py_TPFLAGS_BASETYPE; if (rec.dynamic_attr) enable_dynamic_attributes(heap_type); @@ -620,13 +698,13 @@ inline PyObject* make_new_python_type(const type_record &rec) { else Py_INCREF(type); // Keep it alive forever (reference leak) - if (module) // Needed by pydoc - setattr((PyObject *) type, "__module__", module); + if (module_) // Needed by pydoc + setattr((PyObject *) type, "__module__", module_); PYBIND11_SET_OLDPY_QUALNAME(type, qualname); return (PyObject *) type; } -NAMESPACE_END(detail) -NAMESPACE_END(PYBIND11_NAMESPACE) +PYBIND11_NAMESPACE_END(detail) +PYBIND11_NAMESPACE_END(PYBIND11_NAMESPACE) diff --git a/include/pybind11/detail/common.h b/include/pybind11/detail/common.h index 6da5470608..9ee4dd2d75 100644 --- a/include/pybind11/detail/common.h +++ b/include/pybind11/detail/common.h @@ -9,12 +9,12 @@ #pragma once -#if !defined(NAMESPACE_BEGIN) -# define NAMESPACE_BEGIN(name) namespace name { -#endif -#if !defined(NAMESPACE_END) -# define NAMESPACE_END(name) } -#endif +#define PYBIND11_VERSION_MAJOR 2 +#define PYBIND11_VERSION_MINOR 6 +#define PYBIND11_VERSION_PATCH 1.dev1 + +#define PYBIND11_NAMESPACE_BEGIN(name) namespace name { +#define PYBIND11_NAMESPACE_END(name) } // Robust support for some features and loading modules compiled against different pybind versions // requires forcing hidden visibility on pybind code, so we enforce this by setting the attribute on @@ -47,8 +47,8 @@ // Compiler version assertions #if defined(__INTEL_COMPILER) -# if __INTEL_COMPILER < 1700 -# error pybind11 requires Intel C++ compiler v17 or newer +# if __INTEL_COMPILER < 1800 +# error pybind11 requires Intel C++ compiler v18 or newer # endif #elif defined(__clang__) && !defined(__apple_build_version__) # if __clang_major__ < 3 || (__clang_major__ == 3 && __clang_minor__ < 3) @@ -92,9 +92,19 @@ # define PYBIND11_DEPRECATED(reason) __attribute__((deprecated(reason))) #endif -#define PYBIND11_VERSION_MAJOR 2 -#define PYBIND11_VERSION_MINOR 4 -#define PYBIND11_VERSION_PATCH 3 +#if defined(PYBIND11_CPP17) +# define PYBIND11_MAYBE_UNUSED [[maybe_unused]] +#elif defined(_MSC_VER) && !defined(__clang__) +# define PYBIND11_MAYBE_UNUSED +#else +# define PYBIND11_MAYBE_UNUSED __attribute__ ((__unused__)) +#endif + +/* Don't let Python.h #define (v)snprintf as macro because they are implemented + properly in Visual Studio since 2015. */ +#if defined(_MSC_VER) && _MSC_VER >= 1900 +# define HAVE_SNPRINTF 1 +#endif /// Include Python header, disable linking to pythonX_d.lib on Windows in debug mode #if defined(_MSC_VER) @@ -103,7 +113,7 @@ # endif # pragma warning(push) # pragma warning(disable: 4510 4610 4512 4005) -# if defined(_DEBUG) +# if defined(_DEBUG) && !defined(Py_DEBUG) # define PYBIND11_DEBUG_MARKER # undef _DEBUG # endif @@ -113,6 +123,9 @@ #include #include +/* Python #defines overrides on all sorts of core functions, which + tends to weak havok in C++ codebases that expect these to work + like regular functions (potentially with several overloads) */ #if defined(isalnum) # undef isalnum # undef isalpha @@ -123,6 +136,10 @@ # undef toupper #endif +#if defined(copysign) +# undef copysign +#endif + #if defined(_MSC_VER) # if defined(PYBIND11_DEBUG_MARKER) # define _DEBUG @@ -137,6 +154,7 @@ #include #include #include +#include #include #include #include @@ -164,9 +182,11 @@ #define PYBIND11_STR_TYPE ::pybind11::str #define PYBIND11_BOOL_ATTR "__bool__" #define PYBIND11_NB_BOOL(ptr) ((ptr)->nb_bool) -// Providing a separate declaration to make Clang's -Wmissing-prototypes happy +#define PYBIND11_BUILTINS_MODULE "builtins" +// Providing a separate declaration to make Clang's -Wmissing-prototypes happy. +// See comment for PYBIND11_MODULE below for why this is marked "maybe unused". #define PYBIND11_PLUGIN_IMPL(name) \ - extern "C" PYBIND11_EXPORT PyObject *PyInit_##name(); \ + extern "C" PYBIND11_MAYBE_UNUSED PYBIND11_EXPORT PyObject *PyInit_##name(); \ extern "C" PYBIND11_EXPORT PyObject *PyInit_##name() #else @@ -190,13 +210,15 @@ #define PYBIND11_STR_TYPE ::pybind11::bytes #define PYBIND11_BOOL_ATTR "__nonzero__" #define PYBIND11_NB_BOOL(ptr) ((ptr)->nb_nonzero) -// Providing a separate PyInit decl to make Clang's -Wmissing-prototypes happy +#define PYBIND11_BUILTINS_MODULE "__builtin__" +// Providing a separate PyInit decl to make Clang's -Wmissing-prototypes happy. +// See comment for PYBIND11_MODULE below for why this is marked "maybe unused". #define PYBIND11_PLUGIN_IMPL(name) \ - static PyObject *pybind11_init_wrapper(); \ - extern "C" PYBIND11_EXPORT void init##name(); \ - extern "C" PYBIND11_EXPORT void init##name() { \ - (void)pybind11_init_wrapper(); \ - } \ + static PyObject *pybind11_init_wrapper(); \ + extern "C" PYBIND11_MAYBE_UNUSED PYBIND11_EXPORT void init##name(); \ + extern "C" PYBIND11_EXPORT void init##name() { \ + (void)pybind11_init_wrapper(); \ + } \ PyObject *pybind11_init_wrapper() #endif @@ -211,6 +233,8 @@ extern "C" { #define PYBIND11_STRINGIFY(x) #x #define PYBIND11_TOSTRING(x) PYBIND11_STRINGIFY(x) #define PYBIND11_CONCAT(first, second) first##second +#define PYBIND11_ENSURE_INTERNALS_READY \ + pybind11::detail::get_internals(); #define PYBIND11_CHECK_PYTHON_VERSION \ { \ @@ -241,13 +265,13 @@ extern "C" { ***Deprecated in favor of PYBIND11_MODULE*** This macro creates the entry point that will be invoked when the Python interpreter - imports a plugin library. Please create a `module` in the function body and return + imports a plugin library. Please create a `module_` in the function body and return the pointer to its underlying Python object at the end. .. code-block:: cpp PYBIND11_PLUGIN(example) { - pybind11::module m("example", "pybind11 example plugin"); + pybind11::module_ m("example", "pybind11 example plugin"); /// Set up bindings here return m.ptr(); } @@ -257,6 +281,7 @@ extern "C" { static PyObject *pybind11_init(); \ PYBIND11_PLUGIN_IMPL(name) { \ PYBIND11_CHECK_PYTHON_VERSION \ + PYBIND11_ENSURE_INTERNALS_READY \ try { \ return pybind11_init(); \ } PYBIND11_CATCH_INIT_EXCEPTIONS \ @@ -267,7 +292,11 @@ extern "C" { This macro creates the entry point that will be invoked when the Python interpreter imports an extension module. The module name is given as the fist argument and it should not be in quotes. The second macro argument defines a variable of type - `py::module` which can be used to initialize the module. + `py::module_` which can be used to initialize the module. + + The entry point is marked as "maybe unused" to aid dead-code detection analysis: + since the entry point is typically only looked up at runtime and not referenced + during translation, it would otherwise appear as unused ("dead") code. .. code-block:: cpp @@ -281,19 +310,25 @@ extern "C" { } \endrst */ #define PYBIND11_MODULE(name, variable) \ - static void PYBIND11_CONCAT(pybind11_init_, name)(pybind11::module &); \ + static ::pybind11::module_::module_def \ + PYBIND11_CONCAT(pybind11_module_def_, name) PYBIND11_MAYBE_UNUSED; \ + PYBIND11_MAYBE_UNUSED \ + static void PYBIND11_CONCAT(pybind11_init_, name)(::pybind11::module_ &); \ PYBIND11_PLUGIN_IMPL(name) { \ PYBIND11_CHECK_PYTHON_VERSION \ - auto m = pybind11::module(PYBIND11_TOSTRING(name)); \ + PYBIND11_ENSURE_INTERNALS_READY \ + auto m = ::pybind11::module_::create_extension_module( \ + PYBIND11_TOSTRING(name), nullptr, \ + &PYBIND11_CONCAT(pybind11_module_def_, name)); \ try { \ PYBIND11_CONCAT(pybind11_init_, name)(m); \ return m.ptr(); \ } PYBIND11_CATCH_INIT_EXCEPTIONS \ } \ - void PYBIND11_CONCAT(pybind11_init_, name)(pybind11::module &variable) + void PYBIND11_CONCAT(pybind11_init_, name)(::pybind11::module_ &variable) -NAMESPACE_BEGIN(PYBIND11_NAMESPACE) +PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE) using ssize_t = Py_ssize_t; using size_t = std::size_t; @@ -350,7 +385,7 @@ enum class return_value_policy : uint8_t { reference_internal }; -NAMESPACE_BEGIN(detail) +PYBIND11_NAMESPACE_BEGIN(detail) inline static constexpr int log2(size_t n, int k = 0) { return (n <= 1) ? k : log2(n >> 1, k + 1); } @@ -473,8 +508,16 @@ template using select_indices = typename select_indices_impl using bool_constant = std::integral_constant; template struct negation : bool_constant { }; +// PGI/Intel cannot detect operator delete with the "compatible" void_t impl, so +// using the new one (C++14 defect, so generally works on newer compilers, even +// if not in C++17 mode) +#if defined(__PGIC__) || defined(__INTEL_COMPILER) +template using void_t = void; +#else template struct void_t_impl { using type = void; }; template using void_t = typename void_t_impl::type; +#endif + /// Compile-time all/any/none of that check the boolean value of all template types #if defined(__cpp_fold_expressions) && !(defined(_MSC_VER) && (_MSC_VER < 1916)) @@ -500,17 +543,17 @@ template class... Predicates> using satisfies_none_of /// Strip the class from a method type template struct remove_class { }; -template struct remove_class { typedef R type(A...); }; -template struct remove_class { typedef R type(A...); }; +template struct remove_class { using type = R (A...); }; +template struct remove_class { using type = R (A...); }; /// Helper template to strip away type modifiers -template struct intrinsic_type { typedef T type; }; -template struct intrinsic_type { typedef typename intrinsic_type::type type; }; -template struct intrinsic_type { typedef typename intrinsic_type::type type; }; -template struct intrinsic_type { typedef typename intrinsic_type::type type; }; -template struct intrinsic_type { typedef typename intrinsic_type::type type; }; -template struct intrinsic_type { typedef typename intrinsic_type::type type; }; -template struct intrinsic_type { typedef typename intrinsic_type::type type; }; +template struct intrinsic_type { using type = T; }; +template struct intrinsic_type { using type = typename intrinsic_type::type; }; +template struct intrinsic_type { using type = typename intrinsic_type::type; }; +template struct intrinsic_type { using type = typename intrinsic_type::type; }; +template struct intrinsic_type { using type = typename intrinsic_type::type; }; +template struct intrinsic_type { using type = typename intrinsic_type::type; }; +template struct intrinsic_type { using type = typename intrinsic_type::type; }; template using intrinsic_t = typename intrinsic_type::type; /// Helper type to replace 'void' in some expressions @@ -528,7 +571,7 @@ template constexpr size_t constexpr_sum(T n, Ts... ns) { return size_t{n} + constexpr_sum(ns...); } #endif -NAMESPACE_BEGIN(constexpr_impl) +PYBIND11_NAMESPACE_BEGIN(constexpr_impl) /// Implementation details for constexpr functions constexpr int first(int i) { return i; } template @@ -537,7 +580,7 @@ constexpr int first(int i, T v, Ts... vs) { return v ? i : first(i + 1, vs...); constexpr int last(int /*i*/, int result) { return result; } template constexpr int last(int i, int result, T v, Ts... vs) { return last(i + 1, v ? i : result, vs...); } -NAMESPACE_END(constexpr_impl) +PYBIND11_NAMESPACE_END(constexpr_impl) /// Return the index of the first type in Ts which satisfies Predicate. Returns sizeof...(Ts) if /// none match. @@ -581,8 +624,9 @@ template using is_strict_base_of = bool_consta /// Like is_base_of, but also requires that the base type is accessible (i.e. that a Derived pointer /// can be converted to a Base pointer) +/// For unions, `is_base_of::value` is False, so we need to check `is_same` as well. template using is_accessible_base_of = bool_constant< - std::is_base_of::value && std::is_convertible::value>; + (std::is_same::value || std::is_base_of::value) && std::is_convertible::value>; template class Base> struct is_template_base_of_impl { @@ -648,10 +692,10 @@ inline void ignore_unused(const int *) { } #define PYBIND11_EXPAND_SIDE_EFFECTS(PATTERN) (((PATTERN), void()), ...) #else using expand_side_effects = bool[]; -#define PYBIND11_EXPAND_SIDE_EFFECTS(PATTERN) pybind11::detail::expand_side_effects{ ((PATTERN), void(), false)..., false } +#define PYBIND11_EXPAND_SIDE_EFFECTS(PATTERN) (void)pybind11::detail::expand_side_effects{ ((PATTERN), void(), false)..., false } #endif -NAMESPACE_END(detail) +PYBIND11_NAMESPACE_END(detail) /// C++ bindings of builtin Python exceptions class builtin_exception : public std::runtime_error { @@ -674,6 +718,7 @@ PYBIND11_RUNTIME_EXCEPTION(key_error, PyExc_KeyError) PYBIND11_RUNTIME_EXCEPTION(value_error, PyExc_ValueError) PYBIND11_RUNTIME_EXCEPTION(type_error, PyExc_TypeError) PYBIND11_RUNTIME_EXCEPTION(buffer_error, PyExc_BufferError) +PYBIND11_RUNTIME_EXCEPTION(import_error, PyExc_ImportError) PYBIND11_RUNTIME_EXCEPTION(cast_error, PyExc_RuntimeError) /// Thrown when pybind11::cast or handle::call fail due to a type casting error PYBIND11_RUNTIME_EXCEPTION(reference_cast_error, PyExc_RuntimeError) /// Used internally @@ -682,7 +727,7 @@ PYBIND11_RUNTIME_EXCEPTION(reference_cast_error, PyExc_RuntimeError) /// Used in template struct format_descriptor { }; -NAMESPACE_BEGIN(detail) +PYBIND11_NAMESPACE_BEGIN(detail) // Returns the index of the given type in the type char array below, and in the list in numpy.h // The order here is: bool; 8 ints ((signed,unsigned)x(8,16,32,64)bits); float,double,long double; // complex float,double,long double. Note that the long double types only participate when long @@ -695,7 +740,7 @@ template struct is_fmt_numeric std::is_integral::value ? detail::log2(sizeof(T))*2 + std::is_unsigned::value : 8 + ( std::is_same::value ? 1 : std::is_same::value ? 2 : 0)); }; -NAMESPACE_END(detail) +PYBIND11_NAMESPACE_END(detail) template struct format_descriptor::value>> { static constexpr const char c = "?bBhHiIqQfdg"[detail::is_fmt_numeric::index]; @@ -720,10 +765,10 @@ struct error_scope { /// Dummy destructor wrapper that can be used to expose classes with a private destructor struct nodelete { template void operator()(T*) { } }; -NAMESPACE_BEGIN(detail) +PYBIND11_NAMESPACE_BEGIN(detail) template struct overload_cast_impl { - constexpr overload_cast_impl() {} // MSVC 2015 needs this + constexpr overload_cast_impl() {}; // NOLINT(modernize-use-equals-default): MSVC 2015 needs this template constexpr auto operator()(Return (*pf)(Args...)) const noexcept @@ -737,7 +782,7 @@ struct overload_cast_impl { constexpr auto operator()(Return (Class::*pmf)(Args...) const, std::true_type) const noexcept -> decltype(pmf) { return pmf; } }; -NAMESPACE_END(detail) +PYBIND11_NAMESPACE_END(detail) // overload_cast requires variable templates: C++14 #if defined(PYBIND11_CPP14) @@ -762,7 +807,7 @@ template struct overload_cast { }; #endif // overload_cast -NAMESPACE_BEGIN(detail) +PYBIND11_NAMESPACE_BEGIN(detail) // Adaptor for converting arbitrary container arguments into a vector; implicitly convertible from // any standard container (or C-style array) supporting std::begin/std::end, any singleton @@ -801,8 +846,8 @@ class any_container { const std::vector *operator->() const { return &v; } }; -NAMESPACE_END(detail) - - +// Forward-declaration; see detail/class.h +std::string get_fully_qualified_tp_name(PyTypeObject*); -NAMESPACE_END(PYBIND11_NAMESPACE) +PYBIND11_NAMESPACE_END(detail) +PYBIND11_NAMESPACE_END(PYBIND11_NAMESPACE) diff --git a/include/pybind11/detail/descr.h b/include/pybind11/detail/descr.h index 8d404e5346..92720cd562 100644 --- a/include/pybind11/detail/descr.h +++ b/include/pybind11/detail/descr.h @@ -11,8 +11,8 @@ #include "common.h" -NAMESPACE_BEGIN(PYBIND11_NAMESPACE) -NAMESPACE_BEGIN(detail) +PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE) +PYBIND11_NAMESPACE_BEGIN(detail) #if !defined(_MSC_VER) # define PYBIND11_DESCR_CONSTEXPR static constexpr @@ -96,5 +96,5 @@ constexpr descr type_descr(const descr &descr) { return _("{") + descr + _("}"); } -NAMESPACE_END(detail) -NAMESPACE_END(PYBIND11_NAMESPACE) +PYBIND11_NAMESPACE_END(detail) +PYBIND11_NAMESPACE_END(PYBIND11_NAMESPACE) diff --git a/include/pybind11/detail/init.h b/include/pybind11/detail/init.h index acfe00bdb7..3ef78c1179 100644 --- a/include/pybind11/detail/init.h +++ b/include/pybind11/detail/init.h @@ -11,8 +11,8 @@ #include "class.h" -NAMESPACE_BEGIN(PYBIND11_NAMESPACE) -NAMESPACE_BEGIN(detail) +PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE) +PYBIND11_NAMESPACE_BEGIN(detail) template <> class type_caster { @@ -30,7 +30,7 @@ class type_caster { value_and_holder *value = nullptr; }; -NAMESPACE_BEGIN(initimpl) +PYBIND11_NAMESPACE_BEGIN(initimpl) inline void no_nullptr(void *ptr) { if (!ptr) throw type_error("pybind11::init(): factory function returned nullptr"); @@ -132,6 +132,7 @@ void construct(value_and_holder &v_h, Alias *alias_ptr, bool) { template void construct(value_and_holder &v_h, Holder holder, bool need_alias) { auto *ptr = holder_helper>::get(holder); + no_nullptr(ptr); // If we need an alias, check that the held pointer is actually an alias instance if (Class::has_alias && need_alias && !is_alias(ptr)) throw type_error("pybind11::init(): construction failed: returned holder-wrapped instance " @@ -330,6 +331,6 @@ struct pickle_factory { } }; -NAMESPACE_END(initimpl) -NAMESPACE_END(detail) -NAMESPACE_END(pybind11) +PYBIND11_NAMESPACE_END(initimpl) +PYBIND11_NAMESPACE_END(detail) +PYBIND11_NAMESPACE_END(pybind11) diff --git a/include/pybind11/detail/internals.h b/include/pybind11/detail/internals.h index 067780c260..a455715bfd 100644 --- a/include/pybind11/detail/internals.h +++ b/include/pybind11/detail/internals.h @@ -11,8 +11,8 @@ #include "../pytypes.h" -NAMESPACE_BEGIN(PYBIND11_NAMESPACE) -NAMESPACE_BEGIN(detail) +PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE) +PYBIND11_NAMESPACE_BEGIN(detail) // Forward declarations inline PyTypeObject *make_static_property_type(); inline PyTypeObject *make_default_metaclass(); @@ -25,6 +25,7 @@ inline PyObject *make_object_base_type(PyTypeObject *metaclass); # define PYBIND11_TLS_GET_VALUE(key) PyThread_tss_get((key)) # define PYBIND11_TLS_REPLACE_VALUE(key, value) PyThread_tss_set((key), (value)) # define PYBIND11_TLS_DELETE_VALUE(key) PyThread_tss_set((key), nullptr) +# define PYBIND11_TLS_FREE(key) PyThread_tss_free(key) #else // Usually an int but a long on Cygwin64 with Python 3.x # define PYBIND11_TLS_KEY_INIT(var) decltype(PyThread_create_key()) var = 0 @@ -43,6 +44,7 @@ inline PyObject *make_object_base_type(PyTypeObject *metaclass); # define PYBIND11_TLS_REPLACE_VALUE(key, value) \ PyThread_set_key_value((key), (value)) # endif +# define PYBIND11_TLS_FREE(key) (void)key #endif // Python loads modules by default with dlopen with the RTLD_LOCAL flag; under libc++ and possibly @@ -80,10 +82,10 @@ struct type_equal_to { template using type_map = std::unordered_map; -struct overload_hash { +struct override_hash { inline size_t operator()(const std::pair& v) const { size_t value = std::hash()(v.first); - value ^= std::hash()(v.second) + 0x9e3779b9 + (value<<6) + (value>>2); + value ^= std::hash()(v.second) + 0x9e3779b9 + (value<<6) + (value>>2); return value; } }; @@ -95,7 +97,7 @@ struct internals { type_map registered_types_cpp; // std::type_index -> pybind11's type information std::unordered_map> registered_types_py; // PyTypeObject* -> base type_info(s) std::unordered_multimap registered_instances; // void * -> instance* - std::unordered_set, overload_hash> inactive_overload_cache; + std::unordered_set, override_hash> inactive_override_cache; type_map> direct_conversions; std::unordered_map> patients; std::forward_list registered_exception_translators; @@ -108,6 +110,16 @@ struct internals { #if defined(WITH_THREAD) PYBIND11_TLS_KEY_INIT(tstate); PyInterpreterState *istate = nullptr; + ~internals() { + // This destructor is called *after* Py_Finalize() in finalize_interpreter(). + // That *SHOULD BE* fine. The following details what happens whe PyThread_tss_free is called. + // PYBIND11_TLS_FREE is PyThread_tss_free on python 3.7+. On older python, it does nothing. + // PyThread_tss_free calls PyThread_tss_delete and PyMem_RawFree. + // PyThread_tss_delete just calls TlsFree (on Windows) or pthread_key_delete (on *NIX). Neither + // of those have anything to do with CPython internals. + // PyMem_RawFree *requires* that the `tstate` be allocated with the CPython allocator. + PYBIND11_TLS_FREE(tstate); + } #endif }; @@ -138,53 +150,64 @@ struct type_info { }; /// Tracks the `internals` and `type_info` ABI version independent of the main library version -#define PYBIND11_INTERNALS_VERSION 3 +#define PYBIND11_INTERNALS_VERSION 4 /// On MSVC, debug and release builds are not ABI-compatible! #if defined(_MSC_VER) && defined(_DEBUG) -# define PYBIND11_BUILD_TYPE "_debug" +# define PYBIND11_BUILD_TYPE "_debug" #else -# define PYBIND11_BUILD_TYPE "" +# define PYBIND11_BUILD_TYPE "" #endif /// Let's assume that different compilers are ABI-incompatible. -#if defined(_MSC_VER) -# define PYBIND11_COMPILER_TYPE "_msvc" -#elif defined(__INTEL_COMPILER) -# define PYBIND11_COMPILER_TYPE "_icc" -#elif defined(__clang__) -# define PYBIND11_COMPILER_TYPE "_clang" -#elif defined(__PGI) -# define PYBIND11_COMPILER_TYPE "_pgi" -#elif defined(__MINGW32__) -# define PYBIND11_COMPILER_TYPE "_mingw" -#elif defined(__CYGWIN__) -# define PYBIND11_COMPILER_TYPE "_gcc_cygwin" -#elif defined(__GNUC__) -# define PYBIND11_COMPILER_TYPE "_gcc" -#else -# define PYBIND11_COMPILER_TYPE "_unknown" +/// A user can manually set this string if they know their +/// compiler is compatible. +#ifndef PYBIND11_COMPILER_TYPE +# if defined(_MSC_VER) +# define PYBIND11_COMPILER_TYPE "_msvc" +# elif defined(__INTEL_COMPILER) +# define PYBIND11_COMPILER_TYPE "_icc" +# elif defined(__clang__) +# define PYBIND11_COMPILER_TYPE "_clang" +# elif defined(__PGI) +# define PYBIND11_COMPILER_TYPE "_pgi" +# elif defined(__MINGW32__) +# define PYBIND11_COMPILER_TYPE "_mingw" +# elif defined(__CYGWIN__) +# define PYBIND11_COMPILER_TYPE "_gcc_cygwin" +# elif defined(__GNUC__) +# define PYBIND11_COMPILER_TYPE "_gcc" +# else +# define PYBIND11_COMPILER_TYPE "_unknown" +# endif #endif -#if defined(_LIBCPP_VERSION) -# define PYBIND11_STDLIB "_libcpp" -#elif defined(__GLIBCXX__) || defined(__GLIBCPP__) -# define PYBIND11_STDLIB "_libstdcpp" -#else -# define PYBIND11_STDLIB "" +/// Also standard libs +#ifndef PYBIND11_STDLIB +# if defined(_LIBCPP_VERSION) +# define PYBIND11_STDLIB "_libcpp" +# elif defined(__GLIBCXX__) || defined(__GLIBCPP__) +# define PYBIND11_STDLIB "_libstdcpp" +# else +# define PYBIND11_STDLIB "" +# endif #endif /// On Linux/OSX, changes in __GXX_ABI_VERSION__ indicate ABI incompatibility. -#if defined(__GXX_ABI_VERSION) -# define PYBIND11_BUILD_ABI "_cxxabi" PYBIND11_TOSTRING(__GXX_ABI_VERSION) -#else -# define PYBIND11_BUILD_ABI "" +#ifndef PYBIND11_BUILD_ABI +# if defined(__GXX_ABI_VERSION) +# define PYBIND11_BUILD_ABI "_cxxabi" PYBIND11_TOSTRING(__GXX_ABI_VERSION) +# else +# define PYBIND11_BUILD_ABI "" +# endif #endif -#if defined(WITH_THREAD) -# define PYBIND11_INTERNALS_KIND "" -#else -# define PYBIND11_INTERNALS_KIND "_without_thread" +#ifndef PYBIND11_INTERNALS_KIND +# if defined(WITH_THREAD) +# define PYBIND11_INTERNALS_KIND "" +# else +# define PYBIND11_INTERNALS_KIND "_without_thread" +# endif #endif #define PYBIND11_INTERNALS_ID "__pybind11_internals_v" \ @@ -211,6 +234,7 @@ inline void translate_exception(std::exception_ptr p) { } catch (const std::length_error &e) { PyErr_SetString(PyExc_ValueError, e.what()); return; } catch (const std::out_of_range &e) { PyErr_SetString(PyExc_IndexError, e.what()); return; } catch (const std::range_error &e) { PyErr_SetString(PyExc_ValueError, e.what()); return; + } catch (const std::overflow_error &e) { PyErr_SetString(PyExc_OverflowError, e.what()); return; } catch (const std::exception &e) { PyErr_SetString(PyExc_RuntimeError, e.what()); return; } catch (...) { PyErr_SetString(PyExc_RuntimeError, "Caught an unknown exception!"); @@ -260,7 +284,10 @@ PYBIND11_NOINLINE inline internals &get_internals() { auto *&internals_ptr = *internals_pp; internals_ptr = new internals(); #if defined(WITH_THREAD) - PyEval_InitThreads(); + + #if PY_VERSION_HEX < 0x03090000 + PyEval_InitThreads(); + #endif PyThreadState *tstate = PyThreadState_Get(); #if PY_VERSION_HEX >= 0x03070000 internals_ptr->tstate = PyThread_tss_alloc(); @@ -301,7 +328,7 @@ const char *c_str(Args &&...args) { return strings.front().c_str(); } -NAMESPACE_END(detail) +PYBIND11_NAMESPACE_END(detail) /// Returns a named pointer that is shared among all extension modules (using the same /// pybind11 version) running in the current interpreter. Names starting with underscores @@ -333,4 +360,4 @@ T &get_or_create_shared_data(const std::string &name) { return *ptr; } -NAMESPACE_END(PYBIND11_NAMESPACE) +PYBIND11_NAMESPACE_END(PYBIND11_NAMESPACE) diff --git a/include/pybind11/detail/typeid.h b/include/pybind11/detail/typeid.h index 9c8a4fc69a..148889ffef 100644 --- a/include/pybind11/detail/typeid.h +++ b/include/pybind11/detail/typeid.h @@ -18,8 +18,8 @@ #include "common.h" -NAMESPACE_BEGIN(PYBIND11_NAMESPACE) -NAMESPACE_BEGIN(detail) +PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE) +PYBIND11_NAMESPACE_BEGIN(detail) /// Erase all occurrences of a substring inline void erase_all(std::string &string, const std::string &search) { for (size_t pos = 0;;) { @@ -43,7 +43,7 @@ PYBIND11_NOINLINE inline void clean_type_id(std::string &name) { #endif detail::erase_all(name, "pybind11::"); } -NAMESPACE_END(detail) +PYBIND11_NAMESPACE_END(detail) /// Return a string representation of a C++ type template static std::string type_id() { @@ -52,4 +52,4 @@ template static std::string type_id() { return name; } -NAMESPACE_END(PYBIND11_NAMESPACE) +PYBIND11_NAMESPACE_END(PYBIND11_NAMESPACE) diff --git a/include/pybind11/eigen.h b/include/pybind11/eigen.h index d963d9650b..58582b2cea 100644 --- a/include/pybind11/eigen.h +++ b/include/pybind11/eigen.h @@ -41,14 +41,14 @@ // of matrices seems highly undesirable. static_assert(EIGEN_VERSION_AT_LEAST(3,2,7), "Eigen support in pybind11 requires Eigen >= 3.2.7"); -NAMESPACE_BEGIN(PYBIND11_NAMESPACE) +PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE) // Provide a convenience alias for easier pass-by-ref usage with fully dynamic strides: using EigenDStride = Eigen::Stride; template using EigenDRef = Eigen::Ref; template using EigenDMap = Eigen::Map; -NAMESPACE_BEGIN(detail) +PYBIND11_NAMESPACE_BEGIN(detail) #if EIGEN_VERSION_AT_LEAST(3,3,0) using EigenIndex = Eigen::Index; @@ -549,11 +549,11 @@ struct type_caster::value>> { return false; auto obj = reinterpret_borrow(src); - object sparse_module = module::import("scipy.sparse"); + object sparse_module = module_::import("scipy.sparse"); object matrix_type = sparse_module.attr( rowMajor ? "csr_matrix" : "csc_matrix"); - if (!obj.get_type().is(matrix_type)) { + if (!type::handle_of(obj).is(matrix_type)) { try { obj = matrix_type(obj); } catch (const error_already_set &) { @@ -580,7 +580,7 @@ struct type_caster::value>> { static handle cast(const Type &src, return_value_policy /* policy */, handle /* parent */) { const_cast(src).makeCompressed(); - object matrix_type = module::import("scipy.sparse").attr( + object matrix_type = module_::import("scipy.sparse").attr( rowMajor ? "csr_matrix" : "csc_matrix"); array data(src.nonZeros(), src.valuePtr()); @@ -597,8 +597,8 @@ struct type_caster::value>> { + npy_format_descriptor::name + _("]")); }; -NAMESPACE_END(detail) -NAMESPACE_END(PYBIND11_NAMESPACE) +PYBIND11_NAMESPACE_END(detail) +PYBIND11_NAMESPACE_END(PYBIND11_NAMESPACE) #if defined(__GNUG__) || defined(__clang__) # pragma GCC diagnostic pop diff --git a/include/pybind11/embed.h b/include/pybind11/embed.h index 72655885eb..204aaf989f 100644 --- a/include/pybind11/embed.h +++ b/include/pybind11/embed.h @@ -18,11 +18,13 @@ #if PY_MAJOR_VERSION >= 3 # define PYBIND11_EMBEDDED_MODULE_IMPL(name) \ + extern "C" PyObject *pybind11_init_impl_##name(); \ extern "C" PyObject *pybind11_init_impl_##name() { \ return pybind11_init_wrapper_##name(); \ } #else # define PYBIND11_EMBEDDED_MODULE_IMPL(name) \ + extern "C" void pybind11_init_impl_##name(); \ extern "C" void pybind11_init_impl_##name() { \ pybind11_init_wrapper_##name(); \ } @@ -43,29 +45,28 @@ }); } \endrst */ -#define PYBIND11_EMBEDDED_MODULE(name, variable) \ - static void PYBIND11_CONCAT(pybind11_init_, name)(pybind11::module &); \ - static PyObject PYBIND11_CONCAT(*pybind11_init_wrapper_, name)() { \ - auto m = pybind11::module(PYBIND11_TOSTRING(name)); \ - try { \ - PYBIND11_CONCAT(pybind11_init_, name)(m); \ - return m.ptr(); \ - } catch (pybind11::error_already_set &e) { \ - PyErr_SetString(PyExc_ImportError, e.what()); \ - return nullptr; \ - } catch (const std::exception &e) { \ - PyErr_SetString(PyExc_ImportError, e.what()); \ - return nullptr; \ - } \ - } \ - PYBIND11_EMBEDDED_MODULE_IMPL(name) \ - pybind11::detail::embedded_module name(PYBIND11_TOSTRING(name), \ - PYBIND11_CONCAT(pybind11_init_impl_, name)); \ - void PYBIND11_CONCAT(pybind11_init_, name)(pybind11::module &variable) - - -NAMESPACE_BEGIN(PYBIND11_NAMESPACE) -NAMESPACE_BEGIN(detail) +#define PYBIND11_EMBEDDED_MODULE(name, variable) \ + static ::pybind11::module_::module_def \ + PYBIND11_CONCAT(pybind11_module_def_, name); \ + static void PYBIND11_CONCAT(pybind11_init_, name)(::pybind11::module_ &); \ + static PyObject PYBIND11_CONCAT(*pybind11_init_wrapper_, name)() { \ + auto m = ::pybind11::module_::create_extension_module( \ + PYBIND11_TOSTRING(name), nullptr, \ + &PYBIND11_CONCAT(pybind11_module_def_, name)); \ + try { \ + PYBIND11_CONCAT(pybind11_init_, name)(m); \ + return m.ptr(); \ + } PYBIND11_CATCH_INIT_EXCEPTIONS \ + } \ + PYBIND11_EMBEDDED_MODULE_IMPL(name) \ + ::pybind11::detail::embedded_module PYBIND11_CONCAT(pybind11_module_, name) \ + (PYBIND11_TOSTRING(name), \ + PYBIND11_CONCAT(pybind11_init_impl_, name)); \ + void PYBIND11_CONCAT(pybind11_init_, name)(::pybind11::module_ &variable) + + +PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE) +PYBIND11_NAMESPACE_BEGIN(detail) /// Python 2.7/3.x compatible version of `PyImport_AppendInittab` and error checks. struct embedded_module { @@ -84,7 +85,7 @@ struct embedded_module { } }; -NAMESPACE_END(detail) +PYBIND11_NAMESPACE_END(detail) /** \rst Initialize the Python interpreter. No other pybind11 or CPython API functions can be @@ -106,7 +107,7 @@ inline void initialize_interpreter(bool init_signal_handlers = true) { Py_InitializeEx(init_signal_handlers ? 1 : 0); // Make .py files in the working directory available by default - module::import("sys").attr("path").cast().append("."); + module_::import("sys").attr("path").cast().append("."); } /** \rst @@ -197,4 +198,4 @@ class scoped_interpreter { bool is_valid = true; }; -NAMESPACE_END(PYBIND11_NAMESPACE) +PYBIND11_NAMESPACE_END(PYBIND11_NAMESPACE) diff --git a/include/pybind11/eval.h b/include/pybind11/eval.h index ea85ba1dbe..fa6b8af479 100644 --- a/include/pybind11/eval.h +++ b/include/pybind11/eval.h @@ -13,7 +13,23 @@ #include "pybind11.h" -NAMESPACE_BEGIN(PYBIND11_NAMESPACE) +PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE) +PYBIND11_NAMESPACE_BEGIN(detail) + +inline void ensure_builtins_in_globals(object &global) { + #if PY_VERSION_HEX < 0x03080000 + // Running exec and eval on Python 2 and 3 adds `builtins` module under + // `__builtins__` key to globals if not yet present. + // Python 3.8 made PyRun_String behave similarly. Let's also do that for + // older versions, for consistency. + if (!global.contains("__builtins__")) + global["__builtins__"] = module_::import(PYBIND11_BUILTINS_MODULE); + #else + (void) global; + #endif +} + +PYBIND11_NAMESPACE_END(detail) enum eval_mode { /// Evaluate a string containing an isolated expression @@ -31,6 +47,8 @@ object eval(str expr, object global = globals(), object local = object()) { if (!local) local = global; + detail::ensure_builtins_in_globals(global); + /* PyRun_String does not accept a PyObject / encoding specifier, this seems to be the only alternative */ std::string buffer = "# -*- coding: utf-8 -*-\n" + (std::string) expr; @@ -52,7 +70,7 @@ object eval(str expr, object global = globals(), object local = object()) { template object eval(const char (&s)[N], object global = globals(), object local = object()) { /* Support raw string literals by removing common leading whitespace */ - auto expr = (s[0] == '\n') ? str(module::import("textwrap").attr("dedent")(s)) + auto expr = (s[0] == '\n') ? str(module_::import("textwrap").attr("dedent")(s)) : str(s); return eval(expr, global, local); } @@ -66,11 +84,27 @@ void exec(const char (&s)[N], object global = globals(), object local = object() eval(s, global, local); } +#if defined(PYPY_VERSION) && PY_VERSION_HEX >= 0x03000000 +template +object eval_file(str, object, object) { + pybind11_fail("eval_file not supported in PyPy3. Use eval"); +} +template +object eval_file(str, object) { + pybind11_fail("eval_file not supported in PyPy3. Use eval"); +} +template +object eval_file(str) { + pybind11_fail("eval_file not supported in PyPy3. Use eval"); +} +#else template object eval_file(str fname, object global = globals(), object local = object()) { if (!local) local = global; + detail::ensure_builtins_in_globals(global); + int start; switch (mode) { case eval_expr: start = Py_eval_input; break; @@ -113,5 +147,6 @@ object eval_file(str fname, object global = globals(), object local = object()) throw error_already_set(); return reinterpret_steal(result); } +#endif -NAMESPACE_END(PYBIND11_NAMESPACE) +PYBIND11_NAMESPACE_END(PYBIND11_NAMESPACE) diff --git a/include/pybind11/functional.h b/include/pybind11/functional.h index f8bda64831..92c17dc225 100644 --- a/include/pybind11/functional.h +++ b/include/pybind11/functional.h @@ -12,8 +12,8 @@ #include "pybind11.h" #include -NAMESPACE_BEGIN(PYBIND11_NAMESPACE) -NAMESPACE_BEGIN(detail) +PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE) +PYBIND11_NAMESPACE_BEGIN(detail) template struct type_caster> { @@ -58,7 +58,10 @@ struct type_caster> { struct func_handle { function f; func_handle(function&& f_) : f(std::move(f_)) {} - func_handle(const func_handle&) = default; + func_handle(const func_handle& f_) { + gil_scoped_acquire acq; + f = f_.f; + } ~func_handle() { gil_scoped_acquire acq; function kill_f(std::move(f)); @@ -97,5 +100,5 @@ struct type_caster> { + make_caster::name + _("]")); }; -NAMESPACE_END(detail) -NAMESPACE_END(PYBIND11_NAMESPACE) +PYBIND11_NAMESPACE_END(detail) +PYBIND11_NAMESPACE_END(PYBIND11_NAMESPACE) diff --git a/include/pybind11/iostream.h b/include/pybind11/iostream.h index c43b7c93a6..5e9a8143d0 100644 --- a/include/pybind11/iostream.h +++ b/include/pybind11/iostream.h @@ -17,8 +17,8 @@ #include #include -NAMESPACE_BEGIN(PYBIND11_NAMESPACE) -NAMESPACE_BEGIN(detail) +PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE) +PYBIND11_NAMESPACE_BEGIN(detail) // Buffer that writes to Python instead of C++ class pythonbuf : public std::streambuf { @@ -30,7 +30,7 @@ class pythonbuf : public std::streambuf { object pywrite; object pyflush; - int overflow(int c) { + int overflow(int c) override { if (!traits_type::eq_int_type(c, traits_type::eof())) { *pptr() = traits_type::to_char_type(c); pbump(1); @@ -38,7 +38,10 @@ class pythonbuf : public std::streambuf { return sync() == 0 ? traits_type::not_eof(c) : traits_type::eof(); } - int sync() { + // This function must be non-virtual to be called in a destructor. If the + // rare MSVC test failure shows up with this version, then this should be + // simplified to a fully qualified call. + int _sync() { if (pbase() != pptr()) { // This subtraction cannot be negative, so dropping the sign str line(pbase(), static_cast(pptr() - pbase())); @@ -54,6 +57,10 @@ class pythonbuf : public std::streambuf { return 0; } + int sync() override { + return _sync(); + } + public: pythonbuf(object pyostream, size_t buffer_size = 1024) @@ -67,12 +74,12 @@ class pythonbuf : public std::streambuf { pythonbuf(pythonbuf&&) = default; /// Sync before destroy - ~pythonbuf() { - sync(); + ~pythonbuf() override { + _sync(); } }; -NAMESPACE_END(detail) +PYBIND11_NAMESPACE_END(detail) /** \rst @@ -95,7 +102,7 @@ NAMESPACE_END(detail) .. code-block:: cpp { - py::scoped_ostream_redirect output{std::cerr, py::module::import("sys").attr("stderr")}; + py::scoped_ostream_redirect output{std::cerr, py::module_::import("sys").attr("stderr")}; std::cerr << "Hello, World!"; } \endrst */ @@ -108,7 +115,7 @@ class scoped_ostream_redirect { public: scoped_ostream_redirect( std::ostream &costream = std::cout, - object pyostream = module::import("sys").attr("stdout")) + object pyostream = module_::import("sys").attr("stdout")) : costream(costream), buffer(pyostream) { old = costream.rdbuf(&buffer); } @@ -139,12 +146,12 @@ class scoped_estream_redirect : public scoped_ostream_redirect { public: scoped_estream_redirect( std::ostream &costream = std::cerr, - object pyostream = module::import("sys").attr("stderr")) + object pyostream = module_::import("sys").attr("stderr")) : scoped_ostream_redirect(costream,pyostream) {} }; -NAMESPACE_BEGIN(detail) +PYBIND11_NAMESPACE_BEGIN(detail) // Class to redirect output as a context manager. C++ backend. class OstreamRedirect { @@ -170,7 +177,7 @@ class OstreamRedirect { } }; -NAMESPACE_END(detail) +PYBIND11_NAMESPACE_END(detail) /** \rst This is a helper function to add a C++ redirect context manager to Python @@ -199,11 +206,11 @@ NAMESPACE_END(detail) m.noisy_function_with_error_printing() \endrst */ -inline class_ add_ostream_redirect(module m, std::string name = "ostream_redirect") { +inline class_ add_ostream_redirect(module_ m, std::string name = "ostream_redirect") { return class_(m, name.c_str(), module_local()) .def(init(), arg("stdout")=true, arg("stderr")=true) .def("__enter__", &detail::OstreamRedirect::enter) .def("__exit__", [](detail::OstreamRedirect &self_, args) { self_.exit(); }); } -NAMESPACE_END(PYBIND11_NAMESPACE) +PYBIND11_NAMESPACE_END(PYBIND11_NAMESPACE) diff --git a/include/pybind11/numpy.h b/include/pybind11/numpy.h index ba41a223d0..2c2ab7889b 100644 --- a/include/pybind11/numpy.h +++ b/include/pybind11/numpy.h @@ -20,6 +20,7 @@ #include #include #include +#include #include #include #include @@ -33,13 +34,18 @@ whole npy_intp / ssize_t / Py_intptr_t business down to just ssize_t for all size and dimension types (e.g. shape, strides, indexing), instead of inflicting this upon the library user. */ -static_assert(sizeof(ssize_t) == sizeof(Py_intptr_t), "ssize_t != Py_intptr_t"); +static_assert(sizeof(::pybind11::ssize_t) == sizeof(Py_intptr_t), "ssize_t != Py_intptr_t"); +static_assert(std::is_signed::value, "Py_intptr_t must be signed"); +// We now can reinterpret_cast between py::ssize_t and Py_intptr_t (MSVC + PyPy cares) -NAMESPACE_BEGIN(PYBIND11_NAMESPACE) +PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE) class array; // Forward declaration -NAMESPACE_BEGIN(detail) +PYBIND11_NAMESPACE_BEGIN(detail) + +template <> struct handle_type_name { static constexpr auto name = _("numpy.ndarray"); }; + template struct npy_format_descriptor; struct PyArrayDescr_Proxy { @@ -178,8 +184,9 @@ struct npy_api { unsigned int (*PyArray_GetNDArrayCFeatureVersion_)(); PyObject *(*PyArray_DescrFromType_)(int); PyObject *(*PyArray_NewFromDescr_) - (PyTypeObject *, PyObject *, int, Py_intptr_t *, - Py_intptr_t *, void *, int, PyObject *); + (PyTypeObject *, PyObject *, int, Py_intptr_t const *, + Py_intptr_t const *, void *, int, PyObject *); + // Unused. Not removed because that affects ABI of the class. PyObject *(*PyArray_DescrNewFromType_)(int); int (*PyArray_CopyInto_)(PyObject *, PyObject *); PyObject *(*PyArray_NewCopy_)(PyObject *, int); @@ -190,9 +197,10 @@ struct npy_api { PyObject *(*PyArray_FromAny_) (PyObject *, PyObject *, int, int, int, PyObject *); int (*PyArray_DescrConverter_) (PyObject *, PyObject **); bool (*PyArray_EquivTypes_) (PyObject *, PyObject *); - int (*PyArray_GetArrayParamsFromObject_)(PyObject *, PyObject *, char, PyObject **, int *, - Py_ssize_t *, PyObject **, PyObject *); + int (*PyArray_GetArrayParamsFromObject_)(PyObject *, PyObject *, unsigned char, PyObject **, int *, + Py_intptr_t *, PyObject **, PyObject *); PyObject *(*PyArray_Squeeze_)(PyObject *); + // Unused. Not removed because that affects ABI of the class. int (*PyArray_SetBaseObject_)(PyObject *, PyObject *); PyObject* (*PyArray_Resize_)(PyObject*, PyArray_Dims*, int, int); private: @@ -208,7 +216,7 @@ struct npy_api { API_PyArray_CopyInto = 82, API_PyArray_NewCopy = 85, API_PyArray_NewFromDescr = 94, - API_PyArray_DescrNewFromType = 9, + API_PyArray_DescrNewFromType = 96, API_PyArray_DescrConverter = 174, API_PyArray_EquivTypes = 182, API_PyArray_GetArrayParamsFromObject = 278, @@ -217,7 +225,7 @@ struct npy_api { }; static npy_api lookup() { - module m = module::import("numpy.core.multiarray"); + module_ m = module_::import("numpy.core.multiarray"); auto c = m.attr("_ARRAY_API"); #if PY_MAJOR_VERSION >= 3 void **api_ptr = (void **) PyCapsule_GetPointer(c.ptr(), NULL); @@ -276,7 +284,7 @@ template struct is_complex : std::false_type { }; template struct is_complex> : std::true_type { }; template struct array_info_scalar { - typedef T type; + using type = T; static constexpr bool is_array = false; static constexpr bool is_empty = false; static constexpr auto extents = _(""); @@ -323,6 +331,12 @@ template using is_pod_struct = all_of< satisfies_none_of >; +// Replacement for std::is_pod (deprecated in C++20) +template using is_pod = all_of< + std::is_standard_layout, + std::is_trivial +>; + template ssize_t byte_offset_unsafe(const Strides &) { return 0; } template ssize_t byte_offset_unsafe(const Strides &strides, ssize_t i, Ix... index) { @@ -414,6 +428,10 @@ class unchecked_mutable_reference : public unchecked_reference { using ConstBase::ConstBase; using ConstBase::Dynamic; public: + // Bring in const-qualified versions from base class + using ConstBase::operator(); + using ConstBase::operator[]; + /// Mutable, unchecked access to data at the given indices. template T& operator()(Ix... index) { static_assert(ssize_t{sizeof...(Ix)} == Dims || Dynamic, @@ -439,7 +457,7 @@ struct type_caster> { template struct type_caster> : type_caster> {}; -NAMESPACE_END(detail) +PYBIND11_NAMESPACE_END(detail) class dtype : public object { public: @@ -496,7 +514,7 @@ class dtype : public object { private: static object _dtype_from_pep3118() { - static PyObject *obj = module::import("numpy.core._internal") + static PyObject *obj = module_::import("numpy.core._internal") .attr("_dtype_from_pep3118").cast().release().ptr(); return reinterpret_borrow(obj); } @@ -545,7 +563,7 @@ class array : public buffer { forcecast = detail::npy_api::NPY_ARRAY_FORCECAST_ }; - array() : array({{0}}, static_cast(nullptr)) {} + array() : array(0, static_cast(nullptr)) {} using ShapeContainer = detail::any_container; using StridesContainer = detail::any_container; @@ -555,7 +573,7 @@ class array : public buffer { const void *ptr = nullptr, handle base = handle()) { if (strides->empty()) - *strides = c_strides(*shape, dt.itemsize()); + *strides = detail::c_strides(*shape, dt.itemsize()); auto ndim = shape->size(); if (ndim != strides->size()) @@ -574,7 +592,10 @@ class array : public buffer { auto &api = detail::npy_api::get(); auto tmp = reinterpret_steal(api.PyArray_NewFromDescr_( - api.PyArray_Type_, descr.release().ptr(), (int) ndim, shape->data(), strides->data(), + api.PyArray_Type_, descr.release().ptr(), (int) ndim, + // Use reinterpret_cast for PyPy on Windows (remove if fixed, checked on 7.3.1) + reinterpret_cast(shape->data()), + reinterpret_cast(strides->data()), const_cast(ptr), flags, nullptr)); if (!tmp) throw error_already_set(); @@ -606,8 +627,8 @@ class array : public buffer { template explicit array(ssize_t count, const T *ptr, handle base = handle()) : array({count}, {}, ptr, base) { } - explicit array(const buffer_info &info) - : array(pybind11::dtype(info), info.shape, info.strides, info.ptr) { } + explicit array(const buffer_info &info, handle base = handle()) + : array(pybind11::dtype(info), info.shape, info.strides, info.ptr, base) { } /// Array descriptor (dtype) pybind11::dtype dtype() const { @@ -746,7 +767,9 @@ class array : public buffer { /// then resize will succeed only if it makes a reshape, i.e. original size doesn't change void resize(ShapeContainer new_shape, bool refcheck = true) { detail::npy_api::PyArray_Dims d = { - new_shape->data(), int(new_shape->size()) + // Use reinterpret_cast for PyPy on Windows (remove if fixed, checked on 7.3.1) + reinterpret_cast(new_shape->data()), + int(new_shape->size()) }; // try to resize, set ordering param to -1 cause it's not used anyway object new_array = reinterpret_steal( @@ -783,25 +806,6 @@ class array : public buffer { throw std::domain_error("array is not writeable"); } - // Default, C-style strides - static std::vector c_strides(const std::vector &shape, ssize_t itemsize) { - auto ndim = shape.size(); - std::vector strides(ndim, itemsize); - if (ndim > 0) - for (size_t i = ndim - 1; i > 0; --i) - strides[i - 1] = strides[i] * shape[i]; - return strides; - } - - // F-style strides; default when constructing an array_t with `ExtraFlags & f_style` - static std::vector f_strides(const std::vector &shape, ssize_t itemsize) { - auto ndim = shape.size(); - std::vector strides(ndim, itemsize); - for (size_t i = 1; i < ndim; ++i) - strides[i] = strides[i - 1] * shape[i - 1]; - return strides; - } - template void check_dimensions(Ix... index) const { check_dimensions_impl(ssize_t(0), shape(), ssize_t(index)...); } @@ -853,17 +857,19 @@ template class array_t : public if (!m_ptr) throw error_already_set(); } - explicit array_t(const buffer_info& info) : array(info) { } + explicit array_t(const buffer_info& info, handle base = handle()) : array(info, base) { } array_t(ShapeContainer shape, StridesContainer strides, const T *ptr = nullptr, handle base = handle()) : array(std::move(shape), std::move(strides), ptr, base) { } explicit array_t(ShapeContainer shape, const T *ptr = nullptr, handle base = handle()) : array_t(private_ctor{}, std::move(shape), - ExtraFlags & f_style ? f_strides(*shape, itemsize()) : c_strides(*shape, itemsize()), + ExtraFlags & f_style + ? detail::f_strides(*shape, itemsize()) + : detail::c_strides(*shape, itemsize()), ptr, base) { } - explicit array_t(size_t count, const T *ptr = nullptr, handle base = handle()) + explicit array_t(ssize_t count, const T *ptr = nullptr, handle base = handle()) : array({count}, {}, ptr, base) { } constexpr ssize_t itemsize() const { @@ -929,7 +935,8 @@ template class array_t : public static bool check_(handle h) { const auto &api = detail::npy_api::get(); return api.PyArray_Check_(h.ptr()) - && api.PyArray_EquivTypes_(detail::array_proxy(h.ptr())->descr, dtype::of().ptr()); + && api.PyArray_EquivTypes_(detail::array_proxy(h.ptr())->descr, dtype::of().ptr()) + && detail::check_flags(h.ptr(), ExtraFlags & (array::c_style | array::f_style)); } protected: @@ -976,7 +983,7 @@ struct format_descriptor::is_array> } }; -NAMESPACE_BEGIN(detail) +PYBIND11_NAMESPACE_BEGIN(detail) template struct pyobject_caster> { using type = array_t; @@ -1007,14 +1014,14 @@ struct npy_format_descriptor_name; template struct npy_format_descriptor_name::value>> { static constexpr auto name = _::value>( - _("bool"), _::value>("int", "uint") + _() + _("bool"), _::value>("numpy.int", "numpy.uint") + _() ); }; template struct npy_format_descriptor_name::value>> { static constexpr auto name = _::value || std::is_same::value>( - _("float") + _(), _("longdouble") + _("numpy.float") + _(), _("numpy.longdouble") ); }; @@ -1022,7 +1029,7 @@ template struct npy_format_descriptor_name::value>> { static constexpr auto name = _::value || std::is_same::value>( - _("complex") + _(), _("longcomplex") + _("numpy.complex") + _(), _("numpy.longcomplex") ); }; @@ -1218,7 +1225,7 @@ template struct npy_format_descriptor { #define PYBIND11_MAP_NEXT0(test, next, ...) next PYBIND11_MAP_OUT #define PYBIND11_MAP_NEXT1(test, next) PYBIND11_MAP_NEXT0 (test, next, 0) #define PYBIND11_MAP_NEXT(test, next) PYBIND11_MAP_NEXT1 (PYBIND11_MAP_GET_END test, next) -#ifdef _MSC_VER // MSVC is not as eager to expand macros, hence this workaround +#if defined(_MSC_VER) && !defined(__clang__) // MSVC is not as eager to expand macros, hence this workaround #define PYBIND11_MAP_LIST_NEXT1(test, next) \ PYBIND11_EVAL0 (PYBIND11_MAP_NEXT0 (test, PYBIND11_MAP_COMMA next, 0)) #else @@ -1240,7 +1247,7 @@ template struct npy_format_descriptor { (::std::vector<::pybind11::detail::field_descriptor> \ {PYBIND11_MAP_LIST (PYBIND11_FIELD_DESCRIPTOR, Type, __VA_ARGS__)}) -#ifdef _MSC_VER +#if defined(_MSC_VER) && !defined(__clang__) #define PYBIND11_MAP2_LIST_NEXT1(test, next) \ PYBIND11_EVAL0 (PYBIND11_MAP_NEXT0 (test, PYBIND11_MAP_COMMA next, 0)) #else @@ -1264,19 +1271,6 @@ template struct npy_format_descriptor { #endif // __CLION_IDE__ -template -using array_iterator = typename std::add_pointer::type; - -template -array_iterator array_begin(const buffer_info& buffer) { - return array_iterator(reinterpret_cast(buffer.ptr)); -} - -template -array_iterator array_end(const buffer_info& buffer) { - return array_iterator(reinterpret_cast(buffer.ptr) + buffer.size); -} - class common_iterator { public: using container_type = std::vector; @@ -1290,7 +1284,7 @@ class common_iterator { m_strides.back() = static_cast(strides.back()); for (size_type i = m_strides.size() - 1; i != 0; --i) { size_type j = i - 1; - value_type s = static_cast(shape[i]); + auto s = static_cast(shape[i]); m_strides[j] = strides[j] + m_strides[i] - strides[i] * s; } } @@ -1468,7 +1462,7 @@ struct vectorize_arg { using call_type = remove_reference_t; // Is this a vectorized argument? static constexpr bool vectorize = - satisfies_any_of::value && + satisfies_any_of::value && satisfies_none_of::value && (!std::is_reference::value || (std::is_lvalue_reference::value && std::is_const::value)); @@ -1476,9 +1470,66 @@ struct vectorize_arg { using type = conditional_t, array::forcecast>, T>; }; + +// py::vectorize when a return type is present +template +struct vectorize_returned_array { + using Type = array_t; + + static Type create(broadcast_trivial trivial, const std::vector &shape) { + if (trivial == broadcast_trivial::f_trivial) + return array_t(shape); + else + return array_t(shape); + } + + static Return *mutable_data(Type &array) { + return array.mutable_data(); + } + + static Return call(Func &f, Args &... args) { + return f(args...); + } + + static void call(Return *out, size_t i, Func &f, Args &... args) { + out[i] = f(args...); + } +}; + +// py::vectorize when a return type is not present +template +struct vectorize_returned_array { + using Type = none; + + static Type create(broadcast_trivial, const std::vector &) { + return none(); + } + + static void *mutable_data(Type &) { + return nullptr; + } + + static detail::void_type call(Func &f, Args &... args) { + f(args...); + return {}; + } + + static void call(void *, size_t, Func &f, Args &... args) { + f(args...); + } +}; + + template struct vectorize_helper { + +// NVCC for some reason breaks if NVectorized is private +#ifdef __CUDACC__ +public: +#else private: +#endif + static constexpr size_t N = sizeof...(Args); static constexpr size_t NVectorized = constexpr_sum(vectorize_arg::vectorize...); static_assert(NVectorized >= 1, @@ -1503,6 +1554,8 @@ struct vectorize_helper { using arg_call_types = std::tuple::call_type...>; template using param_n_t = typename std::tuple_element::type; + using returned_array = vectorize_returned_array; + // Runs a vectorized function given arguments tuple and three index sequences: // - Index is the full set of 0 ... (N-1) argument indices; // - VIndex is the subset of argument indices with vectorized parameters, letting us access @@ -1526,7 +1579,7 @@ struct vectorize_helper { ssize_t nd = 0; std::vector shape(0); auto trivial = broadcast(buffers, nd, shape); - size_t ndim = (size_t) nd; + auto ndim = (size_t) nd; size_t size = std::accumulate(shape.begin(), shape.end(), (size_t) 1, std::multiplies()); @@ -1534,20 +1587,19 @@ struct vectorize_helper { // not wrapped in an array). if (size == 1 && ndim == 0) { PYBIND11_EXPAND_SIDE_EFFECTS(params[VIndex] = buffers[BIndex].ptr); - return cast(f(*reinterpret_cast *>(params[Index])...)); + return cast(returned_array::call(f, *reinterpret_cast *>(params[Index])...)); } - array_t result; - if (trivial == broadcast_trivial::f_trivial) result = array_t(shape); - else result = array_t(shape); + auto result = returned_array::create(trivial, shape); if (size == 0) return std::move(result); /* Call the function */ + auto mutable_data = returned_array::mutable_data(result); if (trivial == broadcast_trivial::non_trivial) - apply_broadcast(buffers, params, result, i_seq, vi_seq, bi_seq); + apply_broadcast(buffers, params, mutable_data, size, shape, i_seq, vi_seq, bi_seq); else - apply_trivial(buffers, params, result.mutable_data(), size, i_seq, vi_seq, bi_seq); + apply_trivial(buffers, params, mutable_data, size, i_seq, vi_seq, bi_seq); return std::move(result); } @@ -1570,7 +1622,7 @@ struct vectorize_helper { }}; for (size_t i = 0; i < size; ++i) { - out[i] = f(*reinterpret_cast *>(params[Index])...); + returned_array::call(out, i, f, *reinterpret_cast *>(params[Index])...); for (auto &x : vecparams) x.first += x.second; } } @@ -1578,19 +1630,18 @@ struct vectorize_helper { template void apply_broadcast(std::array &buffers, std::array ¶ms, - array_t &output_array, + Return *out, + size_t size, + const std::vector &output_shape, index_sequence, index_sequence, index_sequence) { - buffer_info output = output_array.request(); - multi_array_iterator input_iter(buffers, output.shape); + multi_array_iterator input_iter(buffers, output_shape); - for (array_iterator iter = array_begin(output), end = array_end(output); - iter != end; - ++iter, ++input_iter) { + for (size_t i = 0; i < size; ++i, ++input_iter) { PYBIND11_EXPAND_SIDE_EFFECTS(( params[VIndex] = input_iter.template data() )); - *iter = f(*reinterpret_cast *>(std::get(params))...); + returned_array::call(out, i, f, *reinterpret_cast *>(std::get(params))...); } } }; @@ -1605,7 +1656,7 @@ template struct handle_type_name> { static constexpr auto name = _("numpy.ndarray[") + npy_format_descriptor::name + _("]"); }; -NAMESPACE_END(detail) +PYBIND11_NAMESPACE_END(detail) // Vanilla pointer vectorizer: template @@ -1635,7 +1686,7 @@ Helper vectorize(Return (Class::*f)(Args...) const) { return Helper(std::mem_fn(f)); } -NAMESPACE_END(PYBIND11_NAMESPACE) +PYBIND11_NAMESPACE_END(PYBIND11_NAMESPACE) #if defined(_MSC_VER) #pragma warning(pop) diff --git a/include/pybind11/operators.h b/include/pybind11/operators.h index b3dd62c3b6..086cb4cfd8 100644 --- a/include/pybind11/operators.h +++ b/include/pybind11/operators.h @@ -18,8 +18,8 @@ # pragma warning(disable: 4127) // warning C4127: Conditional expression is constant #endif -NAMESPACE_BEGIN(PYBIND11_NAMESPACE) -NAMESPACE_BEGIN(detail) +PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE) +PYBIND11_NAMESPACE_BEGIN(detail) /// Enumeration with all supported operator types enum op_id : int { @@ -147,6 +147,9 @@ PYBIND11_INPLACE_OPERATOR(ixor, operator^=, l ^= r) PYBIND11_INPLACE_OPERATOR(ior, operator|=, l |= r) PYBIND11_UNARY_OPERATOR(neg, operator-, -l) PYBIND11_UNARY_OPERATOR(pos, operator+, +l) +// WARNING: This usage of `abs` should only be done for existing STL overloads. +// Adding overloads directly in to the `std::` namespace is advised against: +// https://en.cppreference.com/w/cpp/language/extending_std PYBIND11_UNARY_OPERATOR(abs, abs, std::abs(l)) PYBIND11_UNARY_OPERATOR(hash, hash, std::hash()(l)) PYBIND11_UNARY_OPERATOR(invert, operator~, (~l)) @@ -157,11 +160,13 @@ PYBIND11_UNARY_OPERATOR(float, float_, (double) l) #undef PYBIND11_BINARY_OPERATOR #undef PYBIND11_INPLACE_OPERATOR #undef PYBIND11_UNARY_OPERATOR -NAMESPACE_END(detail) +PYBIND11_NAMESPACE_END(detail) using detail::self; +// Add named operators so that they are accessible via `py::`. +using detail::hash; -NAMESPACE_END(PYBIND11_NAMESPACE) +PYBIND11_NAMESPACE_END(PYBIND11_NAMESPACE) #if defined(_MSC_VER) # pragma warning(pop) diff --git a/include/pybind11/options.h b/include/pybind11/options.h index cc1e1f6f0f..d74db1c68d 100644 --- a/include/pybind11/options.h +++ b/include/pybind11/options.h @@ -11,7 +11,7 @@ #include "detail/common.h" -NAMESPACE_BEGIN(PYBIND11_NAMESPACE) +PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE) class options { public: @@ -62,4 +62,4 @@ class options { state previous_state; }; -NAMESPACE_END(PYBIND11_NAMESPACE) +PYBIND11_NAMESPACE_END(PYBIND11_NAMESPACE) diff --git a/include/pybind11/pybind11.h b/include/pybind11/pybind11.h index c6237056ba..fa98f7c18b 100644 --- a/include/pybind11/pybind11.h +++ b/include/pybind11/pybind11.h @@ -29,6 +29,7 @@ # pragma warning(disable: 4996) // warning C4996: The POSIX name for this item is deprecated. Instead, use the ISO C and C++ conformant name # pragma warning(disable: 4702) // warning C4702: unreachable code # pragma warning(disable: 4522) // warning C4522: multiple assignment operators specified +# pragma warning(disable: 4505) // warning C4505: 'PySlice_GetIndicesEx': unreferenced local function has been removed (PyPy only) #elif defined(__GNUG__) && !defined(__clang__) # pragma GCC diagnostic push # pragma GCC diagnostic ignored "-Wunused-but-set-parameter" @@ -46,16 +47,21 @@ #include "detail/class.h" #include "detail/init.h" +#include +#include +#include +#include + #if defined(__GNUG__) && !defined(__clang__) # include #endif -NAMESPACE_BEGIN(PYBIND11_NAMESPACE) +PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE) /// Wraps an arbitrary C++ function/method/lambda function/.. into a callable Python object class cpp_function : public function { public: - cpp_function() { } + cpp_function() = default; cpp_function(std::nullptr_t) { } /// Construct a cpp_function from a vanilla function pointer @@ -72,16 +78,34 @@ class cpp_function : public function { (detail::function_signature_t *) nullptr, extra...); } - /// Construct a cpp_function from a class method (non-const) + /// Construct a cpp_function from a class method (non-const, no ref-qualifier) template cpp_function(Return (Class::*f)(Arg...), const Extra&... extra) { + initialize([f](Class *c, Arg... args) -> Return { return (c->*f)(std::forward(args)...); }, + (Return (*) (Class *, Arg...)) nullptr, extra...); + } + + /// Construct a cpp_function from a class method (non-const, lvalue ref-qualifier) + /// A copy of the overload for non-const functions without explicit ref-qualifier + /// but with an added `&`. + template + cpp_function(Return (Class::*f)(Arg...)&, const Extra&... extra) { initialize([f](Class *c, Arg... args) -> Return { return (c->*f)(args...); }, (Return (*) (Class *, Arg...)) nullptr, extra...); } - /// Construct a cpp_function from a class method (const) + /// Construct a cpp_function from a class method (const, no ref-qualifier) template cpp_function(Return (Class::*f)(Arg...) const, const Extra&... extra) { + initialize([f](const Class *c, Arg... args) -> Return { return (c->*f)(std::forward(args)...); }, + (Return (*)(const Class *, Arg ...)) nullptr, extra...); + } + + /// Construct a cpp_function from a class method (const, lvalue ref-qualifier) + /// A copy of the overload for const functions without explicit ref-qualifier + /// but with an added `&`. + template + cpp_function(Return (Class::*f)(Arg...) const&, const Extra&... extra) { initialize([f](const Class *c, Arg... args) -> Return { return (c->*f)(args...); }, (Return (*)(const Class *, Arg ...)) nullptr, extra...); } @@ -147,7 +171,7 @@ class cpp_function : public function { /* Get a pointer to the capture object */ auto data = (sizeof(capture) <= sizeof(call.func.data) ? &call.func.data : call.func.data[0]); - capture *cap = const_cast(reinterpret_cast(data)); + auto *cap = const_cast(reinterpret_cast(data)); /* Override policy for rvalues -- usually to enforce rvp::move on an rvalue */ return_value_policy policy = return_value_policy_override::policy(call.func.policy); @@ -168,6 +192,16 @@ class cpp_function : public function { /* Process any user-provided function attributes */ process_attributes::init(extra..., rec); + { + constexpr bool has_kw_only_args = any_of...>::value, + has_pos_only_args = any_of...>::value, + has_args = any_of...>::value, + has_arg_annotations = any_of...>::value; + static_assert(has_arg_annotations || !has_kw_only_args, "py::kw_only requires the use of argument annotations"); + static_assert(has_arg_annotations || !has_pos_only_args, "py::pos_only requires the use of argument annotations (for docstrings and aligning the annotations to the argument)"); + static_assert(!(has_args && has_kw_only_args), "py::kw_only cannot be combined with a py::args argument"); + } + /* Generate a readable signature describing the function's arguments and return value types */ static constexpr auto signature = _("(") + cast_in::arg_names + _(") -> ") + cast_out::name; PYBIND11_DESCR_CONSTEXPR auto types = decltype(signature)::types(); @@ -202,14 +236,14 @@ class cpp_function : public function { if (a.descr) a.descr = strdup(a.descr); else if (a.value) - a.descr = strdup(a.value.attr("__repr__")().cast().c_str()); + a.descr = strdup(repr(a.value).cast().c_str()); } rec->is_constructor = !strcmp(rec->name, "__init__") || !strcmp(rec->name, "__setstate__"); #if !defined(NDEBUG) && !defined(PYBIND11_DISABLE_NEW_STYLE_INIT_WARNING) if (rec->is_constructor && !rec->is_new_style_constructor) { - const auto class_name = std::string(((PyTypeObject *) rec->scope.ptr())->tp_name); + const auto class_name = detail::get_fully_qualified_tp_name((PyTypeObject *) rec->scope.ptr()); const auto func_name = std::string(rec->name); PyErr_WarnEx( PyExc_FutureWarning, @@ -231,7 +265,10 @@ class cpp_function : public function { // Write arg name for everything except *args and **kwargs. if (*(pc + 1) == '*') continue; - + // Separator for keyword-only arguments, placed before the kw + // arguments start + if (rec->nargs_kw_only > 0 && arg_index + rec->nargs_kw_only == args) + signature += "*, "; if (arg_index < rec->args.size() && rec->args[arg_index].name) { signature += rec->args[arg_index].name; } else if (arg_index == 0 && rec->is_method) { @@ -246,6 +283,10 @@ class cpp_function : public function { signature += " = "; signature += rec->args[arg_index].descr; } + // Separator for positional-only arguments (placed after the + // argument, rather than before like * + if (rec->nargs_pos_only > 0 && (arg_index + 1) == rec->nargs_pos_only) + signature += ", /"; arg_index++; } else if (c == '%') { const std::type_info *t = types[type_index++]; @@ -271,6 +312,7 @@ class cpp_function : public function { signature += c; } } + if (arg_index != args || types[type_index] != nullptr) pybind11_fail("Internal error while parsing type signature (2)"); @@ -331,10 +373,9 @@ class cpp_function : public function { if (!m_ptr) pybind11_fail("cpp_function::cpp_function(): Could not allocate function object"); } else { - /* Append at the end of the overload chain */ + /* Append at the beginning or end of the overload chain */ m_ptr = rec->sibling.ptr(); inc_ref(); - chain_start = chain; if (chain->is_method != rec->is_method) pybind11_fail("overloading a method with both static and instance methods is not supported; " #if defined(NDEBUG) @@ -344,9 +385,22 @@ class cpp_function : public function { std::string(pybind11::str(rec->scope.attr("__name__"))) + "." + std::string(rec->name) + signature #endif ); - while (chain->next) - chain = chain->next; - chain->next = rec; + + if (rec->prepend) { + // Beginning of chain; we need to replace the capsule's current head-of-the-chain + // pointer with this one, then make this one point to the previous head of the + // chain. + chain_start = rec; + rec->next = chain; + auto rec_capsule = reinterpret_borrow(((PyCFunctionObject *) m_ptr)->m_self); + rec_capsule.set_pointer(rec); + } else { + // Or end of chain (normal behavior) + chain_start = chain; + while (chain->next) + chain = chain->next; + chain->next = rec; + } } std::string signatures; @@ -384,7 +438,7 @@ class cpp_function : public function { } /* Install docstring */ - PyCFunctionObject *func = (PyCFunctionObject *) m_ptr; + auto *func = (PyCFunctionObject *) m_ptr; if (func->m_ml->ml_doc) std::free(const_cast(func->m_ml->ml_doc)); func->m_ml->ml_doc = strdup(signatures.c_str()); @@ -399,6 +453,12 @@ class cpp_function : public function { /// When a cpp_function is GCed, release any memory allocated by pybind11 static void destruct(detail::function_record *rec) { + // If on Python 3.9, check the interpreter "MICRO" (patch) version. + // If this is running on 3.9.0, we have to work around a bug. + #if !defined(PYPY_VERSION) && PY_MAJOR_VERSION == 3 && PY_MINOR_VERSION == 9 + static bool is_zero = Py_GetVersion()[4] == '0'; + #endif + while (rec) { detail::function_record *next = rec->next; if (rec->free_data) @@ -413,7 +473,15 @@ class cpp_function : public function { } if (rec->def) { std::free(const_cast(rec->def->ml_doc)); - delete rec->def; + // Python 3.9.0 decref's these in the wrong order; rec->def + // If loaded on 3.9.0, let these leak (use Python 3.9.1 at runtime to fix) + // See https://github.com/python/cpython/pull/22670 + #if !defined(PYPY_VERSION) && PY_MAJOR_VERSION == 3 && PY_MINOR_VERSION == 9 + if (!is_zero) + delete rec->def; + #else + delete rec->def; + #endif } delete rec; rec = next; @@ -429,7 +497,7 @@ class cpp_function : public function { *it = overloads; /* Need to know how many arguments + keyword arguments there are to pick the right overload */ - const size_t n_args_in = (size_t) PyTuple_GET_SIZE(args_in); + const auto n_args_in = (size_t) PyTuple_GET_SIZE(args_in); handle parent = n_args_in > 0 ? PyTuple_GET_ITEM(args_in, 0) : nullptr, result = PYBIND11_TRY_NEXT_OVERLOAD; @@ -483,15 +551,16 @@ class cpp_function : public function { */ const function_record &func = *it; - size_t pos_args = func.nargs; // Number of positional arguments that we need - if (func.has_args) --pos_args; // (but don't count py::args - if (func.has_kwargs) --pos_args; // or py::kwargs) + size_t num_args = func.nargs; // Number of positional arguments that we need + if (func.has_args) --num_args; // (but don't count py::args + if (func.has_kwargs) --num_args; // or py::kwargs) + size_t pos_args = num_args - func.nargs_kw_only; if (!func.has_args && n_args_in > pos_args) - continue; // Too many arguments for this overload + continue; // Too many positional arguments for this overload if (n_args_in < pos_args && func.args.size() < pos_args) - continue; // Not enough arguments given, and not enough defaults to fill in the blanks + continue; // Not enough positional arguments given, and not enough defaults to fill in the blanks function_call call(func, parent); @@ -506,7 +575,7 @@ class cpp_function : public function { self_value_and_holder.type->dealloc(self_value_and_holder); call.init_self = PyTuple_GET_ITEM(args_in, 0); - call.args.push_back(reinterpret_cast(&self_value_and_holder)); + call.args.emplace_back(reinterpret_cast(&self_value_and_holder)); call.args_convert.push_back(false); ++args_copied; } @@ -534,16 +603,36 @@ class cpp_function : public function { // We'll need to copy this if we steal some kwargs for defaults dict kwargs = reinterpret_borrow(kwargs_in); + // 1.5. Fill in any missing pos_only args from defaults if they exist + if (args_copied < func.nargs_pos_only) { + for (; args_copied < func.nargs_pos_only; ++args_copied) { + const auto &arg_rec = func.args[args_copied]; + handle value; + + if (arg_rec.value) { + value = arg_rec.value; + } + if (value) { + call.args.push_back(value); + call.args_convert.push_back(arg_rec.convert); + } else + break; + } + + if (args_copied < func.nargs_pos_only) + continue; // Not enough defaults to fill the positional arguments + } + // 2. Check kwargs and, failing that, defaults that may help complete the list - if (args_copied < pos_args) { + if (args_copied < num_args) { bool copied_kwargs = false; - for (; args_copied < pos_args; ++args_copied) { - const auto &arg = func.args[args_copied]; + for (; args_copied < num_args; ++args_copied) { + const auto &arg_rec = func.args[args_copied]; handle value; - if (kwargs_in && arg.name) - value = PyDict_GetItemString(kwargs.ptr(), arg.name); + if (kwargs_in && arg_rec.name) + value = PyDict_GetItemString(kwargs.ptr(), arg_rec.name); if (value) { // Consume a kwargs value @@ -551,25 +640,29 @@ class cpp_function : public function { kwargs = reinterpret_steal(PyDict_Copy(kwargs.ptr())); copied_kwargs = true; } - PyDict_DelItemString(kwargs.ptr(), arg.name); - } else if (arg.value) { - value = arg.value; + PyDict_DelItemString(kwargs.ptr(), arg_rec.name); + } else if (arg_rec.value) { + value = arg_rec.value; + } + + if (!arg_rec.none && value.is_none()) { + break; } if (value) { call.args.push_back(value); - call.args_convert.push_back(arg.convert); + call.args_convert.push_back(arg_rec.convert); } else break; } - if (args_copied < pos_args) + if (args_copied < num_args) continue; // Not enough arguments, defaults, or kwargs to fill the positional arguments } // 3. Check everything was consumed (unless we have a kwargs arg) - if (kwargs && kwargs.size() > 0 && !func.has_kwargs) + if (kwargs && !kwargs.empty() && !func.has_kwargs) continue; // Unconsumed kwargs, but no py::kwargs argument to accept them // 4a. If we have a py::args argument, create a new tuple with leftovers @@ -667,7 +760,7 @@ class cpp_function : public function { } catch (error_already_set &e) { e.restore(); return nullptr; -#if defined(__GNUG__) && !defined(__clang__) +#ifdef __GLIBCXX__ } catch ( abi::__forced_unwind& ) { throw; #endif @@ -749,18 +842,27 @@ class cpp_function : public function { for (size_t ti = overloads->is_constructor ? 1 : 0; ti < args_.size(); ++ti) { if (!some_args) some_args = true; else msg += ", "; - msg += pybind11::repr(args_[ti]); + try { + msg += pybind11::repr(args_[ti]); + } catch (const error_already_set&) { + msg += ""; + } } if (kwargs_in) { auto kwargs = reinterpret_borrow(kwargs_in); - if (kwargs.size() > 0) { + if (!kwargs.empty()) { if (some_args) msg += "; "; msg += "kwargs: "; bool first = true; for (auto kwarg : kwargs) { if (first) first = false; else msg += ", "; - msg += pybind11::str("{}={!r}").format(kwarg.first, kwarg.second); + msg += pybind11::str("{}=").format(kwarg.first); + try { + msg += pybind11::repr(kwarg.second); + } catch (const error_already_set&) { + msg += ""; + } } } } @@ -786,27 +888,18 @@ class cpp_function : public function { }; /// Wrapper for Python extension modules -class module : public object { +class module_ : public object { public: - PYBIND11_OBJECT_DEFAULT(module, object, PyModule_Check) + PYBIND11_OBJECT_DEFAULT(module_, object, PyModule_Check) /// Create a new top-level Python module with the given name and docstring - explicit module(const char *name, const char *doc = nullptr) { - if (!options::show_user_defined_docstrings()) doc = nullptr; + PYBIND11_DEPRECATED("Use PYBIND11_MODULE or module_::create_extension_module instead") + explicit module_(const char *name, const char *doc = nullptr) { #if PY_MAJOR_VERSION >= 3 - PyModuleDef *def = new PyModuleDef(); - std::memset(def, 0, sizeof(PyModuleDef)); - def->m_name = name; - def->m_doc = doc; - def->m_size = -1; - Py_INCREF(def); - m_ptr = PyModule_Create(def); + *this = create_extension_module(name, doc, new PyModuleDef()); #else - m_ptr = Py_InitModule3(name, nullptr, doc); + *this = create_extension_module(name, doc, nullptr); #endif - if (m_ptr == nullptr) - pybind11_fail("Internal error in module::module()"); - inc_ref(); } /** \rst @@ -815,7 +908,7 @@ class module : public object { details on the ``Extra&& ... extra`` argument, see section :ref:`extras`. \endrst */ template - module &def(const char *name_, Func &&f, const Extra& ... extra) { + module_ &def(const char *name_, Func &&f, const Extra& ... extra) { cpp_function func(std::forward(f), name(name_), scope(*this), sibling(getattr(*this, name_, none())), extra...); // NB: allow overwriting here because cpp_function sets up a chain with the intention of @@ -830,14 +923,14 @@ class module : public object { .. code-block:: cpp - py::module m("example", "pybind11 example plugin"); - py::module m2 = m.def_submodule("sub", "A submodule of 'example'"); - py::module m3 = m2.def_submodule("subsub", "A submodule of 'example.sub'"); + py::module_ m("example", "pybind11 example plugin"); + py::module_ m2 = m.def_submodule("sub", "A submodule of 'example'"); + py::module_ m3 = m2.def_submodule("subsub", "A submodule of 'example.sub'"); \endrst */ - module def_submodule(const char *name, const char *doc = nullptr) { + module_ def_submodule(const char *name, const char *doc = nullptr) { std::string full_name = std::string(PyModule_GetName(m_ptr)) + std::string(".") + std::string(name); - auto result = reinterpret_borrow(PyImport_AddModule(full_name.c_str())); + auto result = reinterpret_borrow(PyImport_AddModule(full_name.c_str())); if (doc && options::show_user_defined_docstrings()) result.attr("__doc__") = pybind11::str(doc); attr(name) = result; @@ -845,11 +938,11 @@ class module : public object { } /// Import and return a module or throws `error_already_set`. - static module import(const char *name) { + static module_ import(const char *name) { PyObject *obj = PyImport_ImportModule(name); if (!obj) throw error_already_set(); - return reinterpret_steal(obj); + return reinterpret_steal(obj); } /// Reload the module or throws `error_already_set`. @@ -857,14 +950,16 @@ class module : public object { PyObject *obj = PyImport_ReloadModule(ptr()); if (!obj) throw error_already_set(); - *this = reinterpret_steal(obj); + *this = reinterpret_steal(obj); } - // Adds an object to the module using the given name. Throws if an object with the given name - // already exists. - // - // overwrite should almost always be false: attempting to overwrite objects that pybind11 has - // established will, in most cases, break things. + /** \rst + Adds an object to the module using the given name. Throws if an object with the given name + already exists. + + ``overwrite`` should almost always be false: attempting to overwrite objects that pybind11 has + established will, in most cases, break things. + \endrst */ PYBIND11_NOINLINE void add_object(const char *name, handle obj, bool overwrite = false) { if (!overwrite && hasattr(*this, name)) pybind11_fail("Error during initialization: multiple incompatible definitions with name \"" + @@ -872,25 +967,71 @@ class module : public object { PyModule_AddObject(ptr(), name, obj.inc_ref().ptr() /* steals a reference */); } + +#if PY_MAJOR_VERSION >= 3 + using module_def = PyModuleDef; +#else + struct module_def {}; +#endif + + /** \rst + Create a new top-level module that can be used as the main module of a C extension. + + For Python 3, ``def`` should point to a staticly allocated module_def. + For Python 2, ``def`` can be a nullptr and is completely ignored. + \endrst */ + static module_ create_extension_module(const char *name, const char *doc, module_def *def) { +#if PY_MAJOR_VERSION >= 3 + // module_def is PyModuleDef + def = new (def) PyModuleDef { // Placement new (not an allocation). + /* m_base */ PyModuleDef_HEAD_INIT, + /* m_name */ name, + /* m_doc */ options::show_user_defined_docstrings() ? doc : nullptr, + /* m_size */ -1, + /* m_methods */ nullptr, + /* m_slots */ nullptr, + /* m_traverse */ nullptr, + /* m_clear */ nullptr, + /* m_free */ nullptr + }; + auto m = PyModule_Create(def); +#else + // Ignore module_def *def; only necessary for Python 3 + (void) def; + auto m = Py_InitModule3(name, nullptr, options::show_user_defined_docstrings() ? doc : nullptr); +#endif + if (m == nullptr) { + if (PyErr_Occurred()) + throw error_already_set(); + pybind11_fail("Internal error in module_::create_extension_module()"); + } + // TODO: Sould be reinterpret_steal for Python 3, but Python also steals it again when returned from PyInit_... + // For Python 2, reinterpret_borrow is correct. + return reinterpret_borrow(m); + } }; +// When inside a namespace (or anywhere as long as it's not the first item on a line), +// C++20 allows "module" to be used. This is provided for backward compatibility, and for +// simplicity, if someone wants to use py::module for example, that is perfectly safe. +using module = module_; + /// \ingroup python_builtins /// Return a dictionary representing the global variables in the current execution frame, /// or ``__main__.__dict__`` if there is no frame (usually when the interpreter is embedded). inline dict globals() { PyObject *p = PyEval_GetGlobals(); - return reinterpret_borrow(p ? p : module::import("__main__").attr("__dict__").ptr()); + return reinterpret_borrow(p ? p : module_::import("__main__").attr("__dict__").ptr()); } -NAMESPACE_BEGIN(detail) +PYBIND11_NAMESPACE_BEGIN(detail) /// Generic support for creating new Python heap types class generic_type : public object { - template friend class class_; public: PYBIND11_OBJECT_DEFAULT(generic_type, object, PyType_Check) protected: void initialize(const type_record &rec) { - if (rec.scope && hasattr(rec.scope, rec.name)) + if (rec.scope && hasattr(rec.scope, "__dict__") && rec.scope.attr("__dict__").contains(rec.name)) pybind11_fail("generic_type: cannot initialize type \"" + std::string(rec.name) + "\": an object with that name is already defined"); @@ -954,13 +1095,13 @@ class generic_type : public object { void install_buffer_funcs( buffer_info *(*get_buffer)(PyObject *, void *), void *get_buffer_data) { - PyHeapTypeObject *type = (PyHeapTypeObject*) m_ptr; + auto *type = (PyHeapTypeObject*) m_ptr; auto tinfo = detail::get_type_info(&type->ht_type); if (!type->ht_type.tp_as_buffer) pybind11_fail( "To be able to register buffer protocol support for the type '" + - std::string(tinfo->type->tp_name) + + get_fully_qualified_tp_name(tinfo->type) + "' the associated class<>(..) invocation must " "include the pybind11::buffer_protocol() annotation!"); @@ -1003,17 +1144,31 @@ void call_operator_delete(T *p, size_t s, size_t) { T::operator delete(p, s); } inline void call_operator_delete(void *p, size_t s, size_t a) { (void)s; (void)a; -#if defined(PYBIND11_CPP17) - if (a > __STDCPP_DEFAULT_NEW_ALIGNMENT__) - ::operator delete(p, s, std::align_val_t(a)); - else + #if defined(__cpp_aligned_new) && (!defined(_MSC_VER) || _MSC_VER >= 1912) + if (a > __STDCPP_DEFAULT_NEW_ALIGNMENT__) { + #ifdef __cpp_sized_deallocation + ::operator delete(p, s, std::align_val_t(a)); + #else + ::operator delete(p, std::align_val_t(a)); + #endif + return; + } + #endif + #ifdef __cpp_sized_deallocation ::operator delete(p, s); -#else - ::operator delete(p); -#endif + #else + ::operator delete(p); + #endif } -NAMESPACE_END(detail) +inline void add_class_method(object& cls, const char *name_, const cpp_function &cf) { + cls.attr(cf.name()) = cf; + if (strcmp(name_, "__eq__") == 0 && !cls.attr("__dict__").contains("__hash__")) { + cls.attr("__hash__") = none(); + } +} + +PYBIND11_NAMESPACE_END(detail) /// Given a pointer to a member function, cast it to its `Derived` version. /// Forward everything else unchanged. @@ -1110,7 +1265,7 @@ class class_ : public detail::generic_type { class_ &def(const char *name_, Func&& f, const Extra&... extra) { cpp_function cf(method_adaptor(std::forward(f)), name(name_), is_method(*this), sibling(getattr(*this, name_, none())), extra...); - attr(cf.name()) = cf; + add_class_method(*this, name_, cf); return *this; } @@ -1162,7 +1317,7 @@ class class_ : public detail::generic_type { template class_& def_buffer(Func &&func) { struct capture { Func func; }; - capture *ptr = new capture { std::forward(func) }; + auto *ptr = new capture { std::forward(func) }; install_buffer_funcs([](PyObject *obj, void *ptr) -> buffer_info* { detail::make_caster caster; if (!caster.load(obj, false)) @@ -1347,6 +1502,13 @@ class class_ : public detail::generic_type { /// Deallocates an instance; via holder, if constructed; otherwise via operator delete. static void dealloc(detail::value_and_holder &v_h) { + // We could be deallocating because we are cleaning up after a Python exception. + // If so, the Python error indicator will be set. We need to clear that before + // running the destructor, in case the destructor code calls more Python. + // If we don't, the Python API will exit with an exception, and pybind11 will + // throw error_already_set from the C++ destructor which is forbidden and triggers + // std::terminate(). + error_scope scope; if (v_h.holder_constructed()) { v_h.holder().~holder_type(); v_h.set_holder_constructed(false); @@ -1391,7 +1553,17 @@ detail::initimpl::pickle_factory pickle(GetState &&g, SetSta return {std::forward(g), std::forward(s)}; } -NAMESPACE_BEGIN(detail) +PYBIND11_NAMESPACE_BEGIN(detail) + +inline str enum_name(handle arg) { + dict entries = arg.get_type().attr("__entries"); + for (auto kv : entries) { + if (handle(kv.second[int_(0)]).equal(arg)) + return pybind11::str(kv.first); + } + return "???"; +} + struct enum_base { enum_base(handle base, handle parent) : m_base(base), m_parent(parent) { } @@ -1401,29 +1573,21 @@ struct enum_base { auto static_property = handle((PyObject *) get_internals().static_property_type); m_base.attr("__repr__") = cpp_function( - [](handle arg) -> str { - handle type = arg.get_type(); + [](object arg) -> str { + handle type = type::handle_of(arg); object type_name = type.attr("__name__"); - dict entries = type.attr("__entries"); - for (const auto &kv : entries) { - object other = kv.second[int_(0)]; - if (other.equal(arg)) - return pybind11::str("{}.{}").format(type_name, kv.first); - } - return pybind11::str("{}.???").format(type_name); - }, is_method(m_base) + return pybind11::str("<{}.{}: {}>").format(type_name, enum_name(arg), int_(arg)); + }, name("__repr__"), is_method(m_base) ); - m_base.attr("name") = property(cpp_function( + m_base.attr("name") = property(cpp_function(&enum_name, name("name"), is_method(m_base))); + + m_base.attr("__str__") = cpp_function( [](handle arg) -> str { - dict entries = arg.get_type().attr("__entries"); - for (const auto &kv : entries) { - if (handle(kv.second[int_(0)]).equal(arg)) - return pybind11::str(kv.first); - } - return "???"; - }, is_method(m_base) - )); + object type_name = type::handle_of(arg).attr("__name__"); + return pybind11::str("{}.{}").format(type_name, enum_name(arg)); + }, name("name"), is_method(m_base) + ); m_base.attr("__doc__") = static_property(cpp_function( [](handle arg) -> std::string { @@ -1432,7 +1596,7 @@ struct enum_base { if (((PyTypeObject *) arg.ptr())->tp_doc) docstring += std::string(((PyTypeObject *) arg.ptr())->tp_doc) + "\n\n"; docstring += "Members:"; - for (const auto &kv : entries) { + for (auto kv : entries) { auto key = std::string(pybind11::str(kv.first)); auto comment = kv.second[int_(1)]; docstring += "\n\n " + key; @@ -1440,26 +1604,26 @@ struct enum_base { docstring += " : " + (std::string) pybind11::str(comment); } return docstring; - } + }, name("__doc__") ), none(), none(), ""); m_base.attr("__members__") = static_property(cpp_function( [](handle arg) -> dict { dict entries = arg.attr("__entries"), m; - for (const auto &kv : entries) + for (auto kv : entries) m[kv.first] = kv.second[int_(0)]; return m; - }), none(), none(), "" + }, name("__members__")), none(), none(), "" ); #define PYBIND11_ENUM_OP_STRICT(op, expr, strict_behavior) \ m_base.attr(op) = cpp_function( \ [](object a, object b) { \ - if (!a.get_type().is(b.get_type())) \ + if (!type::handle_of(a).is(type::handle_of(b))) \ strict_behavior; \ return expr; \ }, \ - is_method(m_base)) + name(op), is_method(m_base)) #define PYBIND11_ENUM_OP_CONV(op, expr) \ m_base.attr(op) = cpp_function( \ @@ -1467,7 +1631,7 @@ struct enum_base { int_ a(a_), b(b_); \ return expr; \ }, \ - is_method(m_base)) + name(op), is_method(m_base)) #define PYBIND11_ENUM_OP_CONV_LHS(op, expr) \ m_base.attr(op) = cpp_function( \ @@ -1475,7 +1639,7 @@ struct enum_base { int_ a(a_); \ return expr; \ }, \ - is_method(m_base)) + name(op), is_method(m_base)) if (is_convertible) { PYBIND11_ENUM_OP_CONV_LHS("__eq__", !b.is_none() && a.equal(b)); @@ -1493,7 +1657,7 @@ struct enum_base { PYBIND11_ENUM_OP_CONV("__xor__", a ^ b); PYBIND11_ENUM_OP_CONV("__rxor__", a ^ b); m_base.attr("__invert__") = cpp_function( - [](object arg) { return ~(int_(arg)); }, is_method(m_base)); + [](object arg) { return ~(int_(arg)); }, name("__invert__"), is_method(m_base)); } } else { PYBIND11_ENUM_OP_STRICT("__eq__", int_(a).equal(int_(b)), return false); @@ -1513,11 +1677,11 @@ struct enum_base { #undef PYBIND11_ENUM_OP_CONV #undef PYBIND11_ENUM_OP_STRICT - object getstate = cpp_function( - [](object arg) { return int_(arg); }, is_method(m_base)); + m_base.attr("__getstate__") = cpp_function( + [](object arg) { return int_(arg); }, name("__getstate__"), is_method(m_base)); - m_base.attr("__getstate__") = getstate; - m_base.attr("__hash__") = getstate; + m_base.attr("__hash__") = cpp_function( + [](object arg) { return int_(arg); }, name("__hash__"), is_method(m_base)); } PYBIND11_NOINLINE void value(char const* name_, object value, const char *doc = nullptr) { @@ -1534,7 +1698,7 @@ struct enum_base { PYBIND11_NOINLINE void export_values() { dict entries = m_base.attr("__entries"); - for (const auto &kv : entries) + for (auto kv : entries) m_parent.attr(kv.first) = kv.second[int_(0)]; } @@ -1542,7 +1706,7 @@ struct enum_base { handle m_parent; }; -NAMESPACE_END(detail) +PYBIND11_NAMESPACE_END(detail) /// Binds C++ enumerations and enumeration classes to Python template class enum_ : public class_ { @@ -1570,10 +1734,12 @@ template class enum_ : public class_ { def("__index__", [](Type value) { return (Scalar) value; }); #endif - cpp_function setstate( - [](Type &value, Scalar arg) { value = static_cast(arg); }, - is_method(*this)); - attr("__setstate__") = setstate; + attr("__setstate__") = cpp_function( + [](detail::value_and_holder &v_h, Scalar arg) { + detail::initimpl::setstate(v_h, static_cast(arg), + Py_TYPE(v_h.inst) != v_h.type->type); }, + detail::is_new_style_constructor(), + pybind11::name("__setstate__"), is_method(*this)); } /// Export enumeration entries into the parent scope @@ -1592,7 +1758,7 @@ template class enum_ : public class_ { detail::enum_base m_base; }; -NAMESPACE_BEGIN(detail) +PYBIND11_NAMESPACE_BEGIN(detail) inline void keep_alive_impl(handle nurse, handle patient) { @@ -1662,7 +1828,7 @@ struct iterator_state { bool first_or_done; }; -NAMESPACE_END(detail) +PYBIND11_NAMESPACE_END(detail) /// Makes a python iterator from a first and past-the-end C++ InputIterator. template ()).first), typename... Extra> iterator make_key_iterator(Iterator first, Sentinel last, Extra &&... extra) { - typedef detail::iterator_state state; + using state = detail::iterator_state; if (!detail::get_type_info(typeid(state), false)) { class_(handle(), "iterator", pybind11::module_local()) @@ -1779,11 +1945,11 @@ template class exception : public object { public: exception() = default; - exception(handle scope, const char *name, PyObject *base = PyExc_Exception) { + exception(handle scope, const char *name, handle base = PyExc_Exception) { std::string full_name = scope.attr("__name__").cast() + std::string(".") + name; - m_ptr = PyErr_NewException(const_cast(full_name.c_str()), base, NULL); - if (hasattr(scope, name)) + m_ptr = PyErr_NewException(const_cast(full_name.c_str()), base.ptr(), NULL); + if (hasattr(scope, "__dict__") && scope.attr("__dict__").contains(name)) pybind11_fail("Error during initialization: multiple incompatible " "definitions with name \"" + std::string(name) + "\""); scope.attr(name) = *this; @@ -1795,13 +1961,13 @@ class exception : public object { } }; -NAMESPACE_BEGIN(detail) +PYBIND11_NAMESPACE_BEGIN(detail) // Returns a reference to a function-local static exception object used in the simple // register_exception approach below. (It would be simpler to have the static local variable // directly in register_exception, but that makes clang <3.5 segfault - issue #1349). template exception &get_exception_object() { static exception ex; return ex; } -NAMESPACE_END(detail) +PYBIND11_NAMESPACE_END(detail) /** * Registers a Python exception in `m` of the given `name` and installs an exception translator to @@ -1812,7 +1978,7 @@ NAMESPACE_END(detail) template exception ®ister_exception(handle scope, const char *name, - PyObject *base = PyExc_Exception) { + handle base = PyExc_Exception) { auto &ex = detail::get_exception_object(); if (!ex) ex = exception(scope, name, base); @@ -1827,7 +1993,7 @@ exception ®ister_exception(handle scope, return ex; } -NAMESPACE_BEGIN(detail) +PYBIND11_NAMESPACE_BEGIN(detail) PYBIND11_NOINLINE inline void print(tuple args, dict kwargs) { auto strings = tuple(args.size()); for (size_t i = 0; i < args.size(); ++i) { @@ -1841,7 +2007,7 @@ PYBIND11_NOINLINE inline void print(tuple args, dict kwargs) { file = kwargs["file"].cast(); } else { try { - file = module::import("sys").attr("stdout"); + file = module_::import("sys").attr("stdout"); } catch (const error_already_set &) { /* If print() is called from code that is executed as part of garbage collection during interpreter shutdown, @@ -1858,7 +2024,7 @@ PYBIND11_NOINLINE inline void print(tuple args, dict kwargs) { if (kwargs.contains("flush") && kwargs["flush"].cast()) file.attr("flush")(); } -NAMESPACE_END(detail) +PYBIND11_NAMESPACE_END(detail) template void print(Args &&...args) { @@ -2021,21 +2187,22 @@ error_already_set::~error_already_set() { } } -inline function get_type_overload(const void *this_ptr, const detail::type_info *this_type, const char *name) { - handle self = detail::get_object_handle(this_ptr, this_type); +PYBIND11_NAMESPACE_BEGIN(detail) +inline function get_type_override(const void *this_ptr, const type_info *this_type, const char *name) { + handle self = get_object_handle(this_ptr, this_type); if (!self) return function(); - handle type = self.get_type(); + handle type = type::handle_of(self); auto key = std::make_pair(type.ptr(), name); - /* Cache functions that aren't overloaded in Python to avoid + /* Cache functions that aren't overridden in Python to avoid many costly Python dictionary lookups below */ - auto &cache = detail::get_internals().inactive_overload_cache; + auto &cache = get_internals().inactive_override_cache; if (cache.find(key) != cache.end()) return function(); - function overload = getattr(self, name, function()); - if (overload.is_cpp_function()) { + function override = getattr(self, name, function()); + if (override.is_cpp_function()) { cache.insert(key); return function(); } @@ -2075,34 +2242,36 @@ inline function get_type_overload(const void *this_ptr, const detail::type_info Py_DECREF(result); #endif - return overload; + return override; } +PYBIND11_NAMESPACE_END(detail) /** \rst Try to retrieve a python method by the provided name from the instance pointed to by the this_ptr. - :this_ptr: The pointer to the object the overload should be retrieved for. This should be the first - non-trampoline class encountered in the inheritance chain. - :name: The name of the overloaded Python method to retrieve. + :this_ptr: The pointer to the object the overriden method should be retrieved for. This should be + the first non-trampoline class encountered in the inheritance chain. + :name: The name of the overridden Python method to retrieve. :return: The Python method by this name from the object or an empty function wrapper. \endrst */ -template function get_overload(const T *this_ptr, const char *name) { +template function get_override(const T *this_ptr, const char *name) { auto tinfo = detail::get_type_info(typeid(T)); - return tinfo ? get_type_overload(this_ptr, tinfo, name) : function(); + return tinfo ? detail::get_type_override(this_ptr, tinfo, name) : function(); } -#define PYBIND11_OVERLOAD_INT(ret_type, cname, name, ...) { \ +#define PYBIND11_OVERRIDE_IMPL(ret_type, cname, name, ...) \ + do { \ pybind11::gil_scoped_acquire gil; \ - pybind11::function overload = pybind11::get_overload(static_cast(this), name); \ - if (overload) { \ - auto o = overload(__VA_ARGS__); \ + pybind11::function override = pybind11::get_override(static_cast(this), name); \ + if (override) { \ + auto o = override(__VA_ARGS__); \ if (pybind11::detail::cast_is_temporary_value_reference::value) { \ - static pybind11::detail::overload_caster_t caster; \ + static pybind11::detail::override_caster_t caster; \ return pybind11::detail::cast_ref(std::move(o), caster); \ } \ else return pybind11::detail::cast_safe(std::move(o)); \ } \ - } + } while (false) /** \rst Macro to populate the virtual method in the trampoline class. This macro tries to look up a method named 'fn' @@ -2113,25 +2282,29 @@ template function get_overload(const T *this_ptr, const char *name) { .. code-block:: cpp std::string toString() override { - PYBIND11_OVERLOAD_NAME( + PYBIND11_OVERRIDE_NAME( std::string, // Return type (ret_type) Animal, // Parent class (cname) - toString, // Name of function in C++ (name) - "__str__", // Name of method in Python (fn) + "__str__", // Name of method in Python (name) + toString, // Name of function in C++ (fn) ); } \endrst */ -#define PYBIND11_OVERLOAD_NAME(ret_type, cname, name, fn, ...) \ - PYBIND11_OVERLOAD_INT(PYBIND11_TYPE(ret_type), PYBIND11_TYPE(cname), name, __VA_ARGS__) \ - return cname::fn(__VA_ARGS__) +#define PYBIND11_OVERRIDE_NAME(ret_type, cname, name, fn, ...) \ + do { \ + PYBIND11_OVERRIDE_IMPL(PYBIND11_TYPE(ret_type), PYBIND11_TYPE(cname), name, __VA_ARGS__); \ + return cname::fn(__VA_ARGS__); \ + } while (false) /** \rst - Macro for pure virtual functions, this function is identical to :c:macro:`PYBIND11_OVERLOAD_NAME`, except that it - throws if no overload can be found. + Macro for pure virtual functions, this function is identical to :c:macro:`PYBIND11_OVERRIDE_NAME`, except that it + throws if no override can be found. \endrst */ -#define PYBIND11_OVERLOAD_PURE_NAME(ret_type, cname, name, fn, ...) \ - PYBIND11_OVERLOAD_INT(PYBIND11_TYPE(ret_type), PYBIND11_TYPE(cname), name, __VA_ARGS__) \ - pybind11::pybind11_fail("Tried to call pure virtual function \"" PYBIND11_STRINGIFY(cname) "::" name "\""); +#define PYBIND11_OVERRIDE_PURE_NAME(ret_type, cname, name, fn, ...) \ + do { \ + PYBIND11_OVERRIDE_IMPL(PYBIND11_TYPE(ret_type), PYBIND11_TYPE(cname), name, __VA_ARGS__); \ + pybind11::pybind11_fail("Tried to call pure virtual function \"" PYBIND11_STRINGIFY(cname) "::" name "\""); \ + } while (false) /** \rst Macro to populate the virtual method in the trampoline class. This macro tries to look up the method @@ -2148,7 +2321,7 @@ template function get_overload(const T *this_ptr, const char *name) { // Trampoline (need one for each virtual function) std::string go(int n_times) override { - PYBIND11_OVERLOAD_PURE( + PYBIND11_OVERRIDE_PURE( std::string, // Return type (ret_type) Animal, // Parent class (cname) go, // Name of function in C++ (must match Python name) (fn) @@ -2157,17 +2330,41 @@ template function get_overload(const T *this_ptr, const char *name) { } }; \endrst */ -#define PYBIND11_OVERLOAD(ret_type, cname, fn, ...) \ - PYBIND11_OVERLOAD_NAME(PYBIND11_TYPE(ret_type), PYBIND11_TYPE(cname), #fn, fn, __VA_ARGS__) +#define PYBIND11_OVERRIDE(ret_type, cname, fn, ...) \ + PYBIND11_OVERRIDE_NAME(PYBIND11_TYPE(ret_type), PYBIND11_TYPE(cname), #fn, fn, __VA_ARGS__) /** \rst - Macro for pure virtual functions, this function is identical to :c:macro:`PYBIND11_OVERLOAD`, except that it throws - if no overload can be found. + Macro for pure virtual functions, this function is identical to :c:macro:`PYBIND11_OVERRIDE`, except that it throws + if no override can be found. \endrst */ +#define PYBIND11_OVERRIDE_PURE(ret_type, cname, fn, ...) \ + PYBIND11_OVERRIDE_PURE_NAME(PYBIND11_TYPE(ret_type), PYBIND11_TYPE(cname), #fn, fn, __VA_ARGS__) + + +// Deprecated versions + +PYBIND11_DEPRECATED("get_type_overload has been deprecated") +inline function get_type_overload(const void *this_ptr, const detail::type_info *this_type, const char *name) { + return detail::get_type_override(this_ptr, this_type, name); +} + +template +inline function get_overload(const T *this_ptr, const char *name) { + return get_override(this_ptr, name); +} + +#define PYBIND11_OVERLOAD_INT(ret_type, cname, name, ...) \ + PYBIND11_OVERRIDE_IMPL(PYBIND11_TYPE(ret_type), PYBIND11_TYPE(cname), name, __VA_ARGS__) +#define PYBIND11_OVERLOAD_NAME(ret_type, cname, name, fn, ...) \ + PYBIND11_OVERRIDE_NAME(PYBIND11_TYPE(ret_type), PYBIND11_TYPE(cname), name, fn, __VA_ARGS__) +#define PYBIND11_OVERLOAD_PURE_NAME(ret_type, cname, name, fn, ...) \ + PYBIND11_OVERRIDE_PURE_NAME(PYBIND11_TYPE(ret_type), PYBIND11_TYPE(cname), name, fn, __VA_ARGS__); +#define PYBIND11_OVERLOAD(ret_type, cname, fn, ...) \ + PYBIND11_OVERRIDE(PYBIND11_TYPE(ret_type), PYBIND11_TYPE(cname), fn, __VA_ARGS__) #define PYBIND11_OVERLOAD_PURE(ret_type, cname, fn, ...) \ - PYBIND11_OVERLOAD_PURE_NAME(PYBIND11_TYPE(ret_type), PYBIND11_TYPE(cname), #fn, fn, __VA_ARGS__) + PYBIND11_OVERRIDE_PURE(PYBIND11_TYPE(ret_type), PYBIND11_TYPE(cname), fn, __VA_ARGS__); -NAMESPACE_END(PYBIND11_NAMESPACE) +PYBIND11_NAMESPACE_END(PYBIND11_NAMESPACE) #if defined(_MSC_VER) && !defined(__INTEL_COMPILER) # pragma warning(pop) diff --git a/include/pybind11/pytypes.h b/include/pybind11/pytypes.h index 96eab9662c..1010ad713c 100644 --- a/include/pybind11/pytypes.h +++ b/include/pybind11/pytypes.h @@ -14,14 +14,15 @@ #include #include -NAMESPACE_BEGIN(PYBIND11_NAMESPACE) +PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE) /* A few forward declarations */ class handle; class object; class str; class iterator; +class type; struct arg; struct arg_v; -NAMESPACE_BEGIN(detail) +PYBIND11_NAMESPACE_BEGIN(detail) class args_proxy; inline bool isinstance_generic(handle obj, const std::type_info &tp); @@ -34,7 +35,7 @@ namespace accessor_policies { struct sequence_item; struct list_item; struct tuple_item; -} +} // namespace accessor_policies using obj_attr_accessor = accessor; using str_attr_accessor = accessor; using item_accessor = accessor; @@ -151,14 +152,15 @@ class object_api : public pyobject_tag { /// Return the object's current reference count int ref_count() const { return static_cast(Py_REFCNT(derived().ptr())); } - /// Return a handle to the Python type object underlying the instance + + // TODO PYBIND11_DEPRECATED("Call py::type::handle_of(h) or py::type::of(h) instead of h.get_type()") handle get_type() const; private: bool rich_compare(object_api const &other, int value) const; }; -NAMESPACE_END(detail) +PYBIND11_NAMESPACE_END(detail) /** \rst Holds a reference to a Python object (no reference counting) @@ -240,7 +242,7 @@ class object : public handle { ~object() { dec_ref(); } /** \rst - Resets the internal pointer to ``nullptr`` without without decreasing the + Resets the internal pointer to ``nullptr`` without decreasing the object's reference count. The function returns a raw handle to the original Python object. \endrst */ @@ -311,9 +313,9 @@ template T reinterpret_borrow(handle h) { return {h, object::borrow \endrst */ template T reinterpret_steal(handle h) { return {h, object::stolen_t{}}; } -NAMESPACE_BEGIN(detail) +PYBIND11_NAMESPACE_BEGIN(detail) inline std::string error_string(); -NAMESPACE_END(detail) +PYBIND11_NAMESPACE_END(detail) /// Fetch and hold an error which was already set in Python. An instance of this is typically /// thrown to propagate python-side errors back through C++ which can either be caught manually or @@ -330,13 +332,27 @@ class error_already_set : public std::runtime_error { error_already_set(const error_already_set &) = default; error_already_set(error_already_set &&) = default; - inline ~error_already_set(); + inline ~error_already_set() override; /// Give the currently-held error back to Python, if any. If there is currently a Python error /// already set it is cleared first. After this call, the current object no longer stores the /// error variables (but the `.what()` string is still available). void restore() { PyErr_Restore(m_type.release().ptr(), m_value.release().ptr(), m_trace.release().ptr()); } + /// If it is impossible to raise the currently-held error, such as in destructor, we can write + /// it out using Python's unraisable hook (sys.unraisablehook). The error context should be + /// some object whose repr() helps identify the location of the error. Python already knows the + /// type and value of the error, so there is no need to repeat that. For example, __func__ could + /// be helpful. After this call, the current object no longer stores the error variables, + /// and neither does Python. + void discard_as_unraisable(object err_context) { + restore(); + PyErr_WriteUnraisable(err_context.ptr()); + } + void discard_as_unraisable(const char *err_context) { + discard_as_unraisable(reinterpret_steal(PYBIND11_FROM_STRING(err_context))); + } + // Does nothing; provided for backwards compatibility. PYBIND11_DEPRECATED("Use of error_already_set.clear() is deprecated") void clear() {} @@ -370,7 +386,7 @@ bool isinstance(handle obj) { return T::check_(obj); } template ::value, int> = 0> bool isinstance(handle obj) { return detail::isinstance_generic(obj, typeid(T)); } -template <> inline bool isinstance(handle obj) = delete; +template <> inline bool isinstance(handle) = delete; template <> inline bool isinstance(handle obj) { return obj.ptr() != nullptr; } /// \ingroup python_builtins @@ -446,7 +462,7 @@ inline ssize_t hash(handle obj) { /// @} python_builtins -NAMESPACE_BEGIN(detail) +PYBIND11_NAMESPACE_BEGIN(detail) inline handle get_function(handle value) { if (value) { #if PY_MAJOR_VERSION >= 3 @@ -520,7 +536,7 @@ class accessor : public object_api> { mutable object cache; }; -NAMESPACE_BEGIN(accessor_policies) +PYBIND11_NAMESPACE_BEGIN(accessor_policies) struct obj_attr { using key_type = object; static object get(handle obj, handle key) { return getattr(obj, key); } @@ -597,7 +613,7 @@ struct tuple_item { } } }; -NAMESPACE_END(accessor_policies) +PYBIND11_NAMESPACE_END(accessor_policies) /// STL iterator template used for tuple, list, sequence and dict template @@ -638,7 +654,7 @@ class generic_iterator : public Policy { friend bool operator<=(const It &a, const It &b) { return !(a > b); } }; -NAMESPACE_BEGIN(iterator_policies) +PYBIND11_NAMESPACE_BEGIN(iterator_policies) /// Quick proxy class needed to implement ``operator->`` for iterators which can't return pointers template struct arrow_proxy { @@ -711,7 +727,7 @@ class dict_readonly { PyObject *key = nullptr, *value = nullptr; ssize_t pos = -1; }; -NAMESPACE_END(iterator_policies) +PYBIND11_NAMESPACE_END(iterator_policies) #if !defined(PYPY_VERSION) using tuple_iterator = generic_iterator; @@ -736,9 +752,7 @@ inline bool PyIterable_Check(PyObject *obj) { } inline bool PyNone_Check(PyObject *o) { return o == Py_None; } -#if PY_MAJOR_VERSION >= 3 inline bool PyEllipsis_Check(PyObject *o) { return o == Py_Ellipsis; } -#endif inline bool PyUnicode_Check_Permissive(PyObject *o) { return PyUnicode_Check(o) || PYBIND11_BYTES_CHECK(o); } @@ -770,7 +784,7 @@ class simple_collector; template class unpacking_collector; -NAMESPACE_END(detail) +PYBIND11_NAMESPACE_END(detail) // TODO: After the deprecated constructors are removed, this macro can be simplified by // inheriting ctors: `using Parent::Parent`. It's not an option right now because @@ -784,7 +798,9 @@ NAMESPACE_END(detail) Name(handle h, stolen_t) : Parent(h, stolen_t{}) { } \ PYBIND11_DEPRECATED("Use py::isinstance(obj) instead") \ bool check() const { return m_ptr != nullptr && (bool) CheckFun(m_ptr); } \ - static bool check_(handle h) { return h.ptr() != nullptr && CheckFun(h.ptr()); } + static bool check_(handle h) { return h.ptr() != nullptr && CheckFun(h.ptr()); } \ + template \ + Name(const ::pybind11::detail::accessor &a) : Name(object(a)) { } #define PYBIND11_OBJECT_CVT(Name, Parent, CheckFun, ConvertFun) \ PYBIND11_OBJECT_COMMON(Name, Parent, CheckFun) \ @@ -794,15 +810,20 @@ NAMESPACE_END(detail) { if (!m_ptr) throw error_already_set(); } \ Name(object &&o) \ : Parent(check_(o) ? o.release().ptr() : ConvertFun(o.ptr()), stolen_t{}) \ - { if (!m_ptr) throw error_already_set(); } \ - template \ - Name(const ::pybind11::detail::accessor &a) : Name(object(a)) { } + { if (!m_ptr) throw error_already_set(); } + +#define PYBIND11_OBJECT_CHECK_FAILED(Name, o) \ + ::pybind11::type_error("Object of type '" + \ + ::pybind11::detail::get_fully_qualified_tp_name(Py_TYPE(o.ptr())) + \ + "' is not an instance of '" #Name "'") #define PYBIND11_OBJECT(Name, Parent, CheckFun) \ PYBIND11_OBJECT_COMMON(Name, Parent, CheckFun) \ /* This is deliberately not 'explicit' to allow implicit conversion from object: */ \ - Name(const object &o) : Parent(o) { } \ - Name(object &&o) : Parent(std::move(o)) { } + Name(const object &o) : Parent(o) \ + { if (o && !check_(o)) throw PYBIND11_OBJECT_CHECK_FAILED(Name, o); } \ + Name(object &&o) : Parent(std::move(o)) \ + { if (o && !check_(o)) throw PYBIND11_OBJECT_CHECK_FAILED(Name, o); } #define PYBIND11_OBJECT_DEFAULT(Name, Parent, CheckFun) \ PYBIND11_OBJECT(Name, Parent, CheckFun) \ @@ -878,6 +899,32 @@ class iterator : public object { object value = {}; }; + + +class type : public object { +public: + PYBIND11_OBJECT(type, object, PyType_Check) + + /// Return a type handle from a handle or an object + static handle handle_of(handle h) { return handle((PyObject*) Py_TYPE(h.ptr())); } + + /// Return a type object from a handle or an object + static type of(handle h) { return type(type::handle_of(h), borrowed_t{}); } + + // Defined in pybind11/cast.h + /// Convert C++ type to handle if previously registered. Does not convert + /// standard types, like int, float. etc. yet. + /// See https://github.com/pybind/pybind11/issues/2486 + template + static handle handle_of(); + + /// Convert C++ type to type if previously registered. Does not convert + /// standard types, like int, float. etc. yet. + /// See https://github.com/pybind/pybind11/issues/2486 + template + static type of() {return type(type::handle_of(), borrowed_t{}); } +}; + class iterable : public object { public: PYBIND11_OBJECT_DEFAULT(iterable, object, detail::PyIterable_Check) @@ -908,7 +955,7 @@ class str : public object { Return a string representation of the object. This is analogous to the ``str()`` function in Python. \endrst */ - explicit str(handle h) : object(raw_str(h.ptr()), stolen_t{}) { } + explicit str(handle h) : object(raw_str(h.ptr()), stolen_t{}) { if (!m_ptr) throw error_already_set(); } operator std::string() const { object temp = *this; @@ -948,7 +995,7 @@ inline namespace literals { String literal version of `str` \endrst */ inline str operator"" _s(const char *s, size_t size) { return {s, size}; } -} +} // namespace literals /// \addtogroup pytypes /// @{ @@ -980,6 +1027,9 @@ class bytes : public object { return std::string(buffer, (size_t) length); } }; +// Note: breathe >= 4.17.0 will fail to build docs if the below two constructors +// are included in the doxygen group; close here and reopen after as a workaround +/// @} pytypes inline bytes::bytes(const pybind11::str &s) { object temp = s; @@ -1009,19 +1059,19 @@ inline str::str(const bytes& b) { m_ptr = obj.release().ptr(); } +/// \addtogroup pytypes +/// @{ class none : public object { public: PYBIND11_OBJECT(none, object, detail::PyNone_Check) none() : object(Py_None, borrowed_t{}) { } }; -#if PY_MAJOR_VERSION >= 3 class ellipsis : public object { public: PYBIND11_OBJECT(ellipsis, object, detail::PyEllipsis_Check) ellipsis() : object(Py_Ellipsis, borrowed_t{}) { } }; -#endif class bool_ : public object { public: @@ -1040,7 +1090,7 @@ class bool_ : public object { } }; -NAMESPACE_BEGIN(detail) +PYBIND11_NAMESPACE_BEGIN(detail) // Converts a value to the given unsigned type. If an error occurs, you get back (Unsigned) -1; // otherwise you get back the unsigned long or unsigned long long value cast to (Unsigned). // (The distinction is critically important when casting a returned -1 error value to some other @@ -1060,7 +1110,7 @@ Unsigned as_unsigned(PyObject *o) { return v == (unsigned long long) -1 && PyErr_Occurred() ? (Unsigned) -1 : (Unsigned) v; } } -NAMESPACE_END(detail) +PYBIND11_NAMESPACE_END(detail) class int_ : public object { public: @@ -1186,12 +1236,24 @@ class capsule : public object { } template operator T *() const { + return get_pointer(); + } + + /// Get the pointer the capsule holds. + template + T* get_pointer() const { auto name = this->name(); - T * result = static_cast(PyCapsule_GetPointer(m_ptr, name)); + T *result = static_cast(PyCapsule_GetPointer(m_ptr, name)); if (!result) pybind11_fail("Unable to extract capsule contents!"); return result; } + /// Replaces a capsule's pointer *without* calling the destructor on the existing one. + void set_pointer(const void *value) { + if (PyCapsule_SetPointer(m_ptr, const_cast(value)) != 0) + pybind11_fail("Could not set capsule pointer"); + } + const char *name() const { return PyCapsule_GetName(m_ptr); } }; @@ -1242,7 +1304,12 @@ class dict : public object { class sequence : public object { public: PYBIND11_OBJECT_DEFAULT(sequence, object, PySequence_Check) - size_t size() const { return (size_t) PySequence_Size(m_ptr); } + size_t size() const { + ssize_t result = PySequence_Size(m_ptr); + if (result == -1) + throw error_already_set(); + return (size_t) result; + } bool empty() const { return size() == 0; } detail::sequence_accessor operator[](size_t index) const { return {*this, index}; } detail::item_accessor operator[](handle h) const { return object::operator[](h); } @@ -1315,7 +1382,7 @@ class buffer : public object { buffer_info request(bool writable = false) const { int flags = PyBUF_STRIDES | PyBUF_FORMAT; if (writable) flags |= PyBUF_WRITABLE; - Py_buffer *view = new Py_buffer(); + auto *view = new Py_buffer(); if (PyObject_GetBuffer(m_ptr, view, flags) != 0) { delete view; throw error_already_set(); @@ -1326,46 +1393,154 @@ class buffer : public object { class memoryview : public object { public: - explicit memoryview(const buffer_info& info) { - static Py_buffer buf { }; - // Py_buffer uses signed sizes, strides and shape!.. - static std::vector py_strides { }; - static std::vector py_shape { }; - buf.buf = info.ptr; - buf.itemsize = info.itemsize; - buf.format = const_cast(info.format.c_str()); - buf.ndim = (int) info.ndim; - buf.len = info.size; - py_strides.clear(); - py_shape.clear(); - for (size_t i = 0; i < (size_t) info.ndim; ++i) { - py_strides.push_back(info.strides[i]); - py_shape.push_back(info.shape[i]); - } - buf.strides = py_strides.data(); - buf.shape = py_shape.data(); - buf.suboffsets = nullptr; - buf.readonly = false; - buf.internal = nullptr; + PYBIND11_OBJECT_CVT(memoryview, object, PyMemoryView_Check, PyMemoryView_FromObject) + + /** \rst + Creates ``memoryview`` from ``buffer_info``. - m_ptr = PyMemoryView_FromBuffer(&buf); + ``buffer_info`` must be created from ``buffer::request()``. Otherwise + throws an exception. + + For creating a ``memoryview`` from objects that support buffer protocol, + use ``memoryview(const object& obj)`` instead of this constructor. + \endrst */ + explicit memoryview(const buffer_info& info) { + if (!info.view()) + pybind11_fail("Prohibited to create memoryview without Py_buffer"); + // Note: PyMemoryView_FromBuffer never increments obj reference. + m_ptr = (info.view()->obj) ? + PyMemoryView_FromObject(info.view()->obj) : + PyMemoryView_FromBuffer(info.view()); if (!m_ptr) pybind11_fail("Unable to create memoryview from buffer descriptor"); } - PYBIND11_OBJECT_CVT(memoryview, object, PyMemoryView_Check, PyMemoryView_FromObject) + /** \rst + Creates ``memoryview`` from static buffer. + + This method is meant for providing a ``memoryview`` for C/C++ buffer not + managed by Python. The caller is responsible for managing the lifetime + of ``ptr`` and ``format``, which MUST outlive the memoryview constructed + here. + + See also: Python C API documentation for `PyMemoryView_FromBuffer`_. + + .. _PyMemoryView_FromBuffer: https://docs.python.org/c-api/memoryview.html#c.PyMemoryView_FromBuffer + + :param ptr: Pointer to the buffer. + :param itemsize: Byte size of an element. + :param format: Pointer to the null-terminated format string. For + homogeneous Buffers, this should be set to + ``format_descriptor::value``. + :param shape: Shape of the tensor (1 entry per dimension). + :param strides: Number of bytes between adjacent entries (for each + per dimension). + :param readonly: Flag to indicate if the underlying storage may be + written to. + \endrst */ + static memoryview from_buffer( + void *ptr, ssize_t itemsize, const char *format, + detail::any_container shape, + detail::any_container strides, bool readonly = false); + + static memoryview from_buffer( + const void *ptr, ssize_t itemsize, const char *format, + detail::any_container shape, + detail::any_container strides) { + return memoryview::from_buffer( + const_cast(ptr), itemsize, format, shape, strides, true); + } + + template + static memoryview from_buffer( + T *ptr, detail::any_container shape, + detail::any_container strides, bool readonly = false) { + return memoryview::from_buffer( + reinterpret_cast(ptr), sizeof(T), + format_descriptor::value, shape, strides, readonly); + } + + template + static memoryview from_buffer( + const T *ptr, detail::any_container shape, + detail::any_container strides) { + return memoryview::from_buffer( + const_cast(ptr), shape, strides, true); + } + +#if PY_MAJOR_VERSION >= 3 + /** \rst + Creates ``memoryview`` from static memory. + + This method is meant for providing a ``memoryview`` for C/C++ buffer not + managed by Python. The caller is responsible for managing the lifetime + of ``mem``, which MUST outlive the memoryview constructed here. + + This method is not available in Python 2. + + See also: Python C API documentation for `PyMemoryView_FromBuffer`_. + + .. _PyMemoryView_FromMemory: https://docs.python.org/c-api/memoryview.html#c.PyMemoryView_FromMemory + \endrst */ + static memoryview from_memory(void *mem, ssize_t size, bool readonly = false) { + PyObject* ptr = PyMemoryView_FromMemory( + reinterpret_cast(mem), size, + (readonly) ? PyBUF_READ : PyBUF_WRITE); + if (!ptr) + pybind11_fail("Could not allocate memoryview object!"); + return memoryview(object(ptr, stolen_t{})); + } + + static memoryview from_memory(const void *mem, ssize_t size) { + return memoryview::from_memory(const_cast(mem), size, true); + } +#endif }; + +#ifndef DOXYGEN_SHOULD_SKIP_THIS +inline memoryview memoryview::from_buffer( + void *ptr, ssize_t itemsize, const char* format, + detail::any_container shape, + detail::any_container strides, bool readonly) { + size_t ndim = shape->size(); + if (ndim != strides->size()) + pybind11_fail("memoryview: shape length doesn't match strides length"); + ssize_t size = ndim ? 1 : 0; + for (size_t i = 0; i < ndim; ++i) + size *= (*shape)[i]; + Py_buffer view; + view.buf = ptr; + view.obj = nullptr; + view.len = size * itemsize; + view.readonly = static_cast(readonly); + view.itemsize = itemsize; + view.format = const_cast(format); + view.ndim = static_cast(ndim); + view.shape = shape->data(); + view.strides = strides->data(); + view.suboffsets = nullptr; + view.internal = nullptr; + PyObject* obj = PyMemoryView_FromBuffer(&view); + if (!obj) + throw error_already_set(); + return memoryview(object(obj, stolen_t{})); +} +#endif // DOXYGEN_SHOULD_SKIP_THIS /// @} pytypes /// \addtogroup python_builtins /// @{ + +/// Get the length of a Python object. inline size_t len(handle h) { ssize_t result = PyObject_Length(h.ptr()); if (result < 0) - pybind11_fail("Unable to compute length of object"); + throw error_already_set(); return (size_t) result; } +/// Get the length hint of a Python object. +/// Returns 0 when this cannot be determined. inline size_t len_hint(handle h) { #if PY_VERSION_HEX >= 0x03040000 ssize_t result = PyObject_LengthHint(h.ptr(), 0); @@ -1399,7 +1574,7 @@ inline iterator iter(handle obj) { } /// @} python_builtins -NAMESPACE_BEGIN(detail) +PYBIND11_NAMESPACE_BEGIN(detail) template iterator object_api::begin() const { return iter(derived()); } template iterator object_api::end() const { return iterator::sentinel(); } template item_accessor object_api::operator[](handle key) const { @@ -1428,7 +1603,7 @@ template str_attr_accessor object_api::doc() const { return attr("__doc__"); } template -handle object_api::get_type() const { return (PyObject *) Py_TYPE(derived().ptr()); } +handle object_api::get_type() const { return type::handle_of(derived()); } template bool object_api::rich_compare(object_api const &other, int value) const { @@ -1480,5 +1655,5 @@ PYBIND11_MATH_OPERATOR_BINARY(operator>>=, PyNumber_InPlaceRshift) #undef PYBIND11_MATH_OPERATOR_UNARY #undef PYBIND11_MATH_OPERATOR_BINARY -NAMESPACE_END(detail) -NAMESPACE_END(PYBIND11_NAMESPACE) +PYBIND11_NAMESPACE_END(detail) +PYBIND11_NAMESPACE_END(PYBIND11_NAMESPACE) diff --git a/include/pybind11/stl.h b/include/pybind11/stl.h index 32f8d294ac..721bb669f0 100644 --- a/include/pybind11/stl.h +++ b/include/pybind11/stl.h @@ -48,8 +48,8 @@ # define PYBIND11_HAS_VARIANT 1 #endif -NAMESPACE_BEGIN(PYBIND11_NAMESPACE) -NAMESPACE_BEGIN(detail) +PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE) +PYBIND11_NAMESPACE_BEGIN(detail) /// Extracts an const lvalue reference or rvalue reference for U based on the type of T (e.g. for /// forwarding a container element). Typically used indirect via forwarded_type(), below. @@ -266,7 +266,9 @@ template struct optional_caster { static handle cast(T_ &&src, return_value_policy policy, handle parent) { if (!src) return none().inc_ref(); - policy = return_value_policy_override::policy(policy); + if (!std::is_lvalue_reference::value) { + policy = return_value_policy_override::policy(policy); + } return value_conv::cast(*std::forward(src), policy, parent); } @@ -287,7 +289,7 @@ template struct optional_caster { PYBIND11_TYPE_CASTER(T, _("Optional[") + value_conv::name + _("]")); }; -#if PYBIND11_HAS_OPTIONAL +#if defined(PYBIND11_HAS_OPTIONAL) template struct type_caster> : public optional_caster> {}; @@ -295,7 +297,7 @@ template<> struct type_caster : public void_caster {}; #endif -#if PYBIND11_HAS_EXP_OPTIONAL +#if defined(PYBIND11_HAS_EXP_OPTIONAL) template struct type_caster> : public optional_caster> {}; @@ -367,19 +369,19 @@ struct variant_caster> { PYBIND11_TYPE_CASTER(Type, _("Union[") + detail::concat(make_caster::name...) + _("]")); }; -#if PYBIND11_HAS_VARIANT +#if defined(PYBIND11_HAS_VARIANT) template struct type_caster> : variant_caster> { }; #endif -NAMESPACE_END(detail) +PYBIND11_NAMESPACE_END(detail) inline std::ostream &operator<<(std::ostream &os, const handle &obj) { os << (std::string) str(obj); return os; } -NAMESPACE_END(PYBIND11_NAMESPACE) +PYBIND11_NAMESPACE_END(PYBIND11_NAMESPACE) #if defined(_MSC_VER) #pragma warning(pop) diff --git a/include/pybind11/stl_bind.h b/include/pybind11/stl_bind.h index d3adaed3a2..9d8ed0c825 100644 --- a/include/pybind11/stl_bind.h +++ b/include/pybind11/stl_bind.h @@ -15,8 +15,8 @@ #include #include -NAMESPACE_BEGIN(PYBIND11_NAMESPACE) -NAMESPACE_BEGIN(detail) +PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE) +PYBIND11_NAMESPACE_BEGIN(detail) /* SFINAE helper class used by 'is_comparable */ template struct container_traits { @@ -136,6 +136,13 @@ void vector_modifiers(enable_if_treserve((size_t) slicelength); for (size_t i=0; i::compare(info) || (ssize_t) sizeof(T) != info.itemsize) throw type_error("Format mismatch (Python: " + info.format + " C++: " + format_descriptor::format() + ")"); - auto vec = std::unique_ptr(new Vector()); - vec->reserve((size_t) info.shape[0]); T *p = static_cast(info.ptr); ssize_t step = info.strides[0] / static_cast(sizeof(T)); T *end = p + info.shape[0] * step; - for (; p != end; p += step) - vec->push_back(*p); - return vec.release(); + if (step == 1) { + return Vector(p, end); + } + else { + Vector vec; + vec.reserve((size_t) info.shape[0]); + for (; p != end; p += step) + vec.push_back(*p); + return vec; + } })); return; @@ -406,7 +418,7 @@ vector_buffer(Class_& cl) { template enable_if_t...>::value> vector_buffer(Class_&) {} -NAMESPACE_END(detail) +PYBIND11_NAMESPACE_END(detail) // // std::vector @@ -504,7 +516,7 @@ class_ bind_vector(handle scope, std::string const &name, A // std::map, std::unordered_map // -NAMESPACE_BEGIN(detail) +PYBIND11_NAMESPACE_BEGIN(detail) /* Fallback functions */ template void map_if_insertion_operator(const Args &...) { } @@ -512,7 +524,7 @@ template void map_assignment(const Args & // Map assignment when copy-assignable: just copy the value template -void map_assignment(enable_if_t::value, Class_> &cl) { +void map_assignment(enable_if_t::value, Class_> &cl) { using KeyType = typename Map::key_type; using MappedType = typename Map::mapped_type; @@ -528,7 +540,7 @@ void map_assignment(enable_if_t void map_assignment(enable_if_t< - !std::is_copy_assignable::value && + !is_copy_assignable::value && is_copy_constructible::value, Class_> &cl) { using KeyType = typename Map::key_type; @@ -570,7 +582,7 @@ template auto map_if_insertion_operator(Class_ & } -NAMESPACE_END(detail) +PYBIND11_NAMESPACE_END(detail) template , typename... Args> class_ bind_map(handle scope, const std::string &name, Args&&... args) { @@ -646,4 +658,4 @@ class_ bind_map(handle scope, const std::string &name, Args&&. return cl; } -NAMESPACE_END(PYBIND11_NAMESPACE) +PYBIND11_NAMESPACE_END(PYBIND11_NAMESPACE)