mirror of
https://github.com/pybind/pybind11.git
synced 2025-01-20 01:42:37 +00:00
1bafd5db5f
* Adding test_unique_ptr_member (for desired PyCLIF behavior). See also: https://github.com/pybind/pybind11/issues/2583 Does not build with upstream master or https://github.com/pybind/pybind11/pull/2047, but builds with https://github.com/RobotLocomotion/pybind11 and almost runs: ``` Running tests in directory "/usr/local/google/home/rwgk/forked/EricCousineau-TRI/pybind11/tests": ================================================================================= test session starts ================================================================================= platform linux -- Python 3.8.5, pytest-5.4.3, py-1.9.0, pluggy-0.13.1 rootdir: /usr/local/google/home/rwgk/forked/EricCousineau-TRI/pybind11/tests, inifile: pytest.ini collected 2 items test_unique_ptr_member.py .F [100%] ====================================================================================== FAILURES ======================================================================================= _____________________________________________________________________________ test_pointee_and_ptr_owner ______________________________________________________________________________ def test_pointee_and_ptr_owner(): obj = m.pointee() assert obj.get_int() == 213 m.ptr_owner(obj) with pytest.raises(ValueError) as exc_info: > obj.get_int() E Failed: DID NOT RAISE <class 'ValueError'> test_unique_ptr_member.py:17: Failed ============================================================================= 1 failed, 1 passed in 0.06s ============================================================================= ``` * unique_ptr or shared_ptr return * new test_variant_unique_shared with vptr_holder prototype * moving prototype code to pybind11/vptr_holder.h, adding type_caster specialization to make the bindings involving unique_ptr passing compile, but load and cast implementations are missing * disabling GitHub Actions on pull_request (for this PR) * disabling AppVeyor (for this PR) * TRIGGER_SEGSEV macro, annotations for GET_STACK (vptr::get), GET_INT_STACK (pointee) * adding test_promotion_of_disowned_to_shared * Copying tests as-is from xxx_value_ptr_xxx_holder branch. https://github.com/rwgk/pybind11/tree/xxx_value_ptr_xxx_holder Systematically exercising returning and passing unique_ptr<T>, shared_ptr<T> with unique_ptr, shared_ptr holder. Observations: test_holder_unique_ptr: make_unique_pointee OK pass_unique_pointee BUILD_FAIL (as documented) make_shared_pointee Abort free(): double free detected pass_shared_pointee RuntimeError: Unable to load a custom holder type from a default-holder instance test_holder_shared_ptr: make_unique_pointee Segmentation fault (#1138) pass_unique_pointee BUILD_FAIL (as documented) make_shared_pointee OK pass_shared_pointee OK * Copying tests as-is from xxx_value_ptr_xxx_holder branch. https://github.com/rwgk/pybind11/tree/xxx_value_ptr_xxx_holder Systematically exercising casting between shared_ptr<base>, shared_ptr<derived>. * Demonstration of Undefined Behavior in handling of shared_ptr holder. Based on https://godbolt.org/z/4fdjaW by jorgbrown@ (thanks Jorg!). * Additional demonstration of Undefined Behavior in handling of shared_ptr holder. * fixing up-down mixup in comment * Demonstration of Undefined Behavior in handling of polymorphic pointers. (This demo does NOT involve smart pointers at all, unlike the otherwise similar test_smart_ptr_private_first_base.) * minor test_private_first_base.cpp simplification (after discovering that this can be wrapped with Boost.Python, using boost::noncopyable) * pybind11 equivalent of Boost.Python test similar to reproducer under #1333 * Snapshot of WIP, TODO: shared_ptr deleter with on/off switch * Adding vptr_deleter. * Adding from/as unique_ptr<T> and unique_ptr<T, D>. * Adding from_shared_ptr. Some polishing. * New tests/core/smart_holder_poc_test.cpp, using Catch2. * Adding in vptr_deleter_guard_flag. * Improved labeling of TEST_CASEs. * Shuffling existing TEST_CASEs into systematic matrix. * Implementing all [S]uccess tests. * Implementing all [E]xception tests. * Testing of exceptions not covered by the from-as matrix. * Adding top-level comment. * Converting from methods to factory functions (no functional change). * Removing obsolete and very incomplete test (replaced by Catch2-based test). * Removing stray file. * Adding type_caster_bare_interface_demo. * Adding shared_ptr<mpty>, shared_ptr<mpty const> casters. * Adding unique_ptr<mpty>, unique_ptr<mpty const> casters. * Pure copy of `class class_` implementation in pybind11.h (master commit98f1bbb800
). * classh.h: renaming of class_ to classh + namespace; forking test_classh_wip from test_type_caster_bare_interface_demo. * Hard-coding smart_holder into classh. * Adding mpty::mtxt string member. * Adding isinstance<mpty> in type_caster::load functions. * Adding rvalue_ref, renaming const_value_ref to lvalue_ref & removing const. * Retrieving smart_holder pointer in type_caster<mpty>::load, and using it cast_op operators. * Factoring out smart_holder_type_caster_load. * Retrieving smart_holder pointer in type_caster<std::shared_ptr<mpty[ const]>>::load, and using it cast_op operators. * Improved error messaging: Cannot disown nullptr (as_unique_ptr). * Retrieving smart_holder pointer in type_caster<std::unique_ptr<mpty[ const]>>::load, and using it cast_op operators. * Pure `clang-format --style=file -i` change. * Pure `clang-format --style=file -i` change, with two `clang-format off` directives. * Fixing oversight (discovered by flake8). * flake8 cleanup * Systematically setting mtxt for all rtrn_mpty_* functions (preparation, the values are not actually used yet). * static cast handle for rtrn_cptr works by simply dropping in code from type_caster_base (marked with comments). * static cast handle for rtrn_cref works by simply dropping in code from type_caster_base (marked with comments). rtrn_mref and rtrn_mptr work via const_cast (to add const). * static cast handle for rtrn_valu works by simply dropping in code from type_caster_base (marked with comments). rtrn_rref raises a RuntimeError, to be investigated. * Copying type_caster_generic::cast into type_caster<mpty> as-is (preparation for handling smart pointers). * Pure clang-format change (applied to original type_caster_generic::cast). * Adding comment re potential use_count data race. * static handle cast implementations for rtrn_shmp, rtrn_shcp. * Adding MISSING comments in operator std::unique_ptr<mpty[ const]>. * static handle cast implementations for rtrn_uqmp, rtrn_uqcp. * Bug fix: vptr_deleter_armed_flag_ptr has to live on the heap. See new bullet point in comment section near the top. The variable was also renamed to reflect its function more accurately. * Fixing bugs discovered by ASAN. The code is now ASAN, MSAN, UBSAN clean. * Making test_type_caster_bare_interface_demo.cpp slightly more realistic, ASAN, MSAN, UBSAN clean. * Calling deregister_instance after disowning via unique_ptr. * Removing enable_shared_from_this stub, simplifying existing code, clang-format. Open question, with respect to the original code:76a160070b/include/pybind11/pybind11.h (L1510)
To me it looks like the exact situation marked as `std::shared_ptr<Good> gp1 = not_so_good.getptr();` here: https://en.cppreference.com/w/cpp/memory/enable_shared_from_this The comment there is: `// undefined behavior (until C++17) and std::bad_weak_ptr thrown (since C++17)` Does the existing code have UB pre C++17? I'll leave handling of enable_shared_from_this for later, as the need arises. * Cosmetical change around helper functions. * Using type_caster_base<mpty>::src_and_type directly, removing copy. Also renaming one cast to cast_const_raw_ptr, for clarity. * Fixing clang-format oversight. * Using factored-out make_constructor (PR #2798), removing duplicate code. * Inserting additional assert to ensure a returned unique_ptr is always a new Python instance. * Adding minor comment (change to internals needed to distinguish uninitialized/disowned in error message). * Factoring out find_existing_python_instance(). * Moving factored-out make_constructor to test_classh_wip.cpp, restoring previous version of cast.h. This is currently the most practical approach. See PR #2798 for background. * Copying classh type_casters from test_classh_wip.cpp UNMODIFIED, as a baseline for generalizing the code. * Using pybind11/detail/classh_type_casters.h from test_classh_wip.cpp. * Adding & using PYBIND11_CLASSH_TYPE_CASTERS define. * Adding test_classh_inheritance, currently failing (passes with class_). * Removing .clang-format before git rebase master (where the file was added). * Bringing back .clang-format, the previous rm was a bad idea. * Folding in modified_type_caster_generic_load_impl, just enough to pass test_class_wip. test_classh_inheritance is still failing, but with a different error: [RuntimeError: Incompatible type (as_raw_ptr_unowned).] * Minimal changes needed to pass test_classh_inheritance. * First pass adjusting try_implicit_casts and try_load_foreign_module_local to capture loaded_v_h, but untested and guarded with pybind11_failure("Untested"). This was done mainly to determine general feasibility. Note the TODO in pybind11.h, where type_caster_generic::local_load is currently hard-coded. test_classh_wip and test_classh_inheritance still pass, as before. * Decoupling generic_type from type_caster_generic. * Changes and tests covering classh_type_casters try_implicit_casts. * Minimal test covering classh_type_casters load_impl Case 2b. * Removing stray isinstance<T>(src): it interferes with the py::module_local feature. Adding missing #includes. * Tests for classh py::module_local() feature. * Pure renaming of function names in test_classh_inheritance, similar to the systematic approach used in test_class_wip. NO functional changes. * Pure renaming of function and variable names, for better generalization when convoluting with inheritance. NO functional changes. * Adopting systematic naming scheme from test_classh_wip. NO functional changes. * Moving const after type name, for functions that cover a systematic scheme. NO functional changes. * Adding smart_holder_type_caster_load::loaded_as_shared_ptr, currently bypassing smart_holder shared_ptr tracking completely, but the tests pass and are sanitizer clean. * Removing rtti_held from smart_holder. See updated comment. * Cleaning up loaded_as_raw_ptr_unowned, loaded_as_shared_ptr. * Factoring out convert_type and folding into loaded_as_unique_ptr. * Folding convert_type into lvalue_ref and rvalue_ref paths. Some smart_holder_type_caster_load cleanup. * Using unique_ptr in local_load to replace static variable. Also adding local_load_safety_guard. * Converting test_unique_ptr_member to using classh: fully working, ASAN, MSAN, UBSAN clean. * Removing debugging comments (GET_STACK, GET_INT_STACK). cast.h is identical to current master again, pybind11.h only has the generic_type::initialize(..., &type_caster_generic::local_load) change. * Purging obsolete pybind11/vptr_holder.h and associated test. * Moving several tests to github.com/rwgk/rwgk_tbx/tree/main/pybind11_testsa2c2f88174
These tests are from experimenting, and for demonstrating UB in pybind11 multiple inheritance handling ("first_base"), to be fixed later. * Adding py::smart_holder support to py::class_, purging py::classh completely. * Renaming files in include directory, creating pybind11/smart_holder.h. * Renaming all "classh" to "smart_holder" in pybind11/detail/smart_holder_type_casters.h. The user-facing macro is now PYBIND11_SMART_HOLDER_TYPE_CASTERS. * Systematically renaming tests to use "class_sh" in the name. * Renaming test_type_caster_bare_interface_demo to test_type_caster_bare_interface. * Renaming new tests/core subdirectory to tests/pure_cpp. * Adding new tests to CMake config, resetting CI config. * Changing CMake file so that test_class_sh_module_local.py actually runs. * clang-tidy fixes. * 32-bit compatibility. * Reusing type_caster_base make_copy_constructor, make_move_constructor with a trick. * CMake COMPARE NATURAL is not available with older versions. * Adding copyright notices to new header files. * Explicitly define copy/move constructors/assignments. * Adding new header files to tests/extra_python_package/test_files.py. * Adding tests/pure_cpp/CMakeLists.txt. * Making use of the new find_existing_python_instance() function factored out with PR #2822. * Moving define PYBIND11_SMART_HOLDER_TYPE_CASTERS(T) down in the file. NO functional changes. Preparation for follow-up work (to keep that diff smaller). * Reintroducing py::classh, this time as a simple alias for py::class_<U, py::smart_holder>. * Replacing detail::is_smart_holder<H> in cast.h with detail::is_smart_holder_type_caster<T>. Moving get_local_load_function_ptr, init_instance_for_type to smart_holder_type_caster_class_hooks. Expanding static_assert in py::type::handle_of<> to accommodate smart_holder_type_casters. * Fixing oversight. * Adding classu alias for class_<U, std::unique_ptr<U>>. * Giving up on idea to use legacy init_instance only if is_base_of<type_caster_generic, type_caster<T>. There are use cases in the wild that define both a custom type_caster and class_. * Removing test_type_caster_bare_interface, which was moved to the separate PR #2834. * Moving up is_smart_holder_type_caster, to also use in cast_is_temporary_value_reference. * Adding smart_holder_type_casters for unique_ptr with custom deleter. SEVERE CODE DUPLICATION. This commit is to establish a baseline for consolidating the unique_ptr code. * Unification of unique_ptr, unique_ptr_with_deleter code in smart_holder_poc.h. Leads to more fitting error messages. Enables use of unique_ptr<T, D> smart_holder_type_casters also for unique_ptr<T>. * Copying files as-is from branch test_unique_ptr_member (PR #2672). * Adding comment, simplifying naming, cmake addition. * Introducing PYBIND11_USE_SMART_HOLDER_AS_DEFAULT macro (tested only undefined; there are many errors with the macro defined). * Removing test_type_caster_bare_interface, which was moved to the separate PR #2834. * Fixing oversight introduced with commit95425f13d6
. * Setting record.default_holder correctly for PYBIND11_USE_SMART_HOLDER_AS_DEFAULT. With this test_class.cpp builds and even mostly runs, except `test_multiple_instances_with_same_pointer`, which segfaults because it is using a `unique_ptr` holder but `smart_holder` `type_caster`. Also adding `static_assert`s to generate build errors for such situations, but guarding with `#if 0` to first pivot to test_factory_constructors.cpp. * Fixing up cast.h and smart_holder.h after rebase. * Removing detail/smart_holder_type_casters.h in separate commit. * Commenting out const in def_buffer(... const). With this, test_buffers builds and runs with PYBIND11_USE_SMART_HOLDER_AS_DEFAULT. Explanation why the const needs to be removed, or fix elsewhere, is still needed, but left for later. * Adding test_class_sh_factory_constructors, reproducing test_factory_constructors failure. Using py::class_ in this commit, to be changed to py::classh for debugging. * Removing include/pybind11/detail/smart_holder_type_casters.h from CMakeLists.txt, test_files.py (since it does not exist in this branch). * Adding // DANGER ZONE reminders. * Converting as many py::class_ to py::classh as possible, not breaking tests. * Adding initimpl::construct() overloads, resulting in test_class_sh_factory_constructors feature parity for py::class_ and py::classh. * Adding enable_if !is_smart_holder_type_caster to existing initimpl::construct(). With this test_factory_constructors.cpp builds with PYBIND11_USE_SMART_HOLDER_AS_DEFAULT. * Disabling shared_ptr&, shared_ptr* tests when building with PYBIND11_USE_SMART_HOLDER_AS_DEFAULT for now, pending work on smart_holder_type_caster<shared_ptr>. * Factoring out struct and class definitions into anonymous namespace. Preparation for building with PYBIND11_USE_SMART_HOLDER_AS_DEFAULT. * Simplifying from_unique_ptr(): typename D = std::default_delete<T> is not needed. Factoring out is_std_default_delete<T>() for consistentcy between ensure_compatible_rtti_uqp_del() and from_unique_ptr(). * Introducing PYBIND11_SMART_POINTER_HOLDER_TYPE_CASTERS. Using it in test_smart_ptr.cpp. With this test_smart_ptr builds with PYBIND11_USE_SMART_HOLDER_AS_DEFAULT and all but one test run successfully. * Introducing 1. type_caster_for_class_, used in PYBIND11_MAKE_OPAQUE, and 2. default_holder_type, used in stl_bind.h. * Using __VA_ARGS__ in PYBIND11_SMART_POINTER_HOLDER_TYPE_CASTERS. * Replacing condense_for_macro with much simpler approach. * Softening static_assert, to only check specifically that smart_holder is not mixed with type_caster_base, and unique_ptr/shared_ptr holders are not mixed with smart_holder_type_casters. * Adding PYBIND11_SMART_POINTER_HOLDER_TYPE_CASTERS in test_class.cpp (with this all but one test succeed with PYBIND11_USE_SMART_HOLDER_AS_DEFAULT). * Adding remaining PYBIND11_SMART_POINTER_HOLDER_TYPE_CASTERS. static_assert for "necessary conditions" for both types of default holder, static_assert for "strict conditions" guarded by new PYBIND11_STRICT_ASSERTS_CLASS_HOLDER_VS_TYPE_CASTER_MIX. All tests build & run as before with unique_ptr as the default holder, all tests build for smart_holder as the default holder, even with the strict static_assert. * Introducing check_is_smart_holder_type_caster() function for runtime check, and reinterpreting record.default_holder as "uses_unique_ptr_holder". With this test_smart_ptr succeeds. (All 42 tests build, 35 tests succeed, 5 run but have some failures, 2 segfault.) * Bug fix: Adding have_value() to smart_holder_type_caster_load. With this test_builtin_casters succeeds. (All 42 tests build, 36 tests succeed, 5 run but have some failures, 1 segfault.) * Adding unowned_void_ptr_from_direct_conversion to modified_type_caster_generic_load_impl. This fixes the last remaining segfault (test_numpy_dtypes). New stats for all tests combined: 12 failed, 458 passed. * Adding "Lazy allocation for unallocated values" (for old-style __init__) into load_value_and_holder. Deferring destruction of disowned holder until clear_instance, to remain inspectable for "uninitialized" or "disowned" detection. New stats for all tests combined: 5 failed, 465 passed. * Changing std::shared_ptr pointer/reference to const pointer/reference. New stats for all tests combined: 4 failed, 466 passed. * Adding return_value_policy::move to permissible policies for unique_ptr returns. New stats for all tests combined: 3 failed, 467 passed. * Overlooked flake8 fixes. * Manipulating failing ConstructorStats test to pass, to be able to run all tests with ASAN. This version of the code is ASAN clean with unique_ptr or smart_holder as the default. This change needs to be reverted after adopting the existing move-only-if-refcount-is-1 logic used by type_caster_base. * Adding copy constructor and move constructor tracking to atyp. Preparation for a follow-up change in smart_holder_type_caster, to make this test sensitive to the changing behavior. [skip ci] * Removing `operator T&&() &&` from smart_holder_type_caster, for compatibility with the behavior of type_caster_base. Enables reverting 2 of 3 test manipulations applied under commit249df7cbdb
. The manipulation in test_factory_constructors.py is NOT reverted in this commit. [skip ci] * Fixing unfortunate editing mishap. This reverts the last remaining test manipulation in commit249df7cbdb
and makes all existing unit tests pass with smart_holder as default holder. * GitHub CI clang-tidy fixes. * Adding messages to terse `static_assert`s, for pre-C++17 compatibility. * Using @pytest.mark.parametrize to run each assert separately (to see all errors, not just the first). * Systematically removing _atyp from function names, to make the test code simpler. * Using re.match to accommodate variable number of intermediate MvCtor. * Also removing `operator T()` from smart_holder_type_caster, to fix gcc compilation errors. The only loss is pass_rref in test_class_sh_basic. * Systematically replacing `detail::enable_if_t<...smart_holder...>` with `typename std::enable_if<...smart_holder...>::type`. Attempt to work around MSVC 2015 issues, to be tested via GitHub CI. The idea for this change originates from this comment: https://github.com/pybind/pybind11/issues/1616#issuecomment-444536813 * Importing re before pytest after observing a PyPy CI flake when importing pytest first. * Copying MSVC 2015 compatibility change from branch pr2672_use_smart_holder_as_default. * Introducing is_smart_holder_type_caster_base_tag, to keep smart_holder code more disconnected. * Working around MSVC 2015 bug. * Expanding comment for MSVC 2015 workaround. * Systematically changing std::enable_if back to detail::enable_if_t, effectively reverting commit5d4b6890a3
. * Removing unused smart_holder_type_caster_load::loaded_as_rvalue_ref (it was an oversight that it was not removed with commit23036a45eb
). * Removing py::classu, because it does not seem useful enough. * Reverting commit6349531306
by un-commenting `const` in `def_buffer(...)`. To make this possible, `operator T const&` and `operator T const*` in `smart_holder_type_caster` need to be marked as `const` member functions. * Adding construct() overloads for constructing smart_holder from alias unique_ptr, shared_ptr returns. * Adding test_class_sh_factory_constructors.cpp to tests/CMakeLists.txt (fixes oversight, this should have been added long before). * Compatibility with old clang versions (clang 3.6, 3.7 C++11). * Cleaning up changes to existing unit tests. * Systematically adding SMART_HOLDER_WIP tag. Removing minor UNTESTED tags (only the throw are not actually exercised, investing time there has a high cost but very little benefit). * Splitting out smart_holder_type_casters again, into new detail/smart_holder_type_casters_inline_include.h. * Splitting out smart_holder_init_inline_include.h. * Adding additional new include files to CMakeLists.txt, tests/extra_python_package/test_files.py. * clang-format cleanup of most smart_holder code. * Adding source code comments in response to review. * Simple micro-benchmark ("ubench") comparing runtime performance for several holders. Tested using github.com/rwgk/pybind11_scons and Google-internal build system. Sorry, no cmake support at the moment. First results: https://docs.google.com/spreadsheets/d/1InapCYws2Gt-stmFf_Bwl33eOMo3aLE_gc9adveY7RU/edit#gid=0 * Breaking out number_bucket.h, adding hook for also collecting performance data for PyCLIF. * Accounting for ubench in MANIFEST.in (simply prune, for now). * Smarter determination of call_repetitions. [skip ci] * Also scaling performance data to PyCLIF. [skip ci] * Adding ubench/python/number_bucket.clif here for general visibility. * Fix after rebase * Merging detail/smart_holder_init_inline_include.h into detail/init.h. * Renaming detail/is_smart_holder_type_caster.h -> detail/smart_holder_sfinae_hooks_only.h. * Renaming is_smart_holder_type_caster -> type_uses_smart_holder_type_caster for clarity. * Renaming type_caster_type_is_smart_holder_type_caster -> wrapped_type_uses_smart_holder_type_caster for clarity. * Renaming is_smart_holder_type_caster_base_tag -> smart_holder_type_caster_base_tag for simplicity. * Adding copyright notices and minor colateral cleanup. * iwyu cleanup (comprehensive only for cast.h and smart_holder*.h files). * Fixing `git rebase master` accident. * Moving large `pragma warning` block from pybind11.h to detail/common.h. * Fixing another `git rebase master` accident.
2354 lines
106 KiB
C++
2354 lines
106 KiB
C++
// clang-format off
|
|
/*
|
|
pybind11/pybind11.h: Main header file of the C++11 python
|
|
binding generator library
|
|
|
|
Copyright (c) 2016 Wenzel Jakob <wenzel.jakob@epfl.ch>
|
|
|
|
All rights reserved. Use of this source code is governed by a
|
|
BSD-style license that can be found in the LICENSE file.
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
#include "attr.h"
|
|
#include "gil.h"
|
|
#include "options.h"
|
|
#include "detail/class.h"
|
|
#include "detail/init.h"
|
|
#include "detail/smart_holder_sfinae_hooks_only.h"
|
|
|
|
#include <memory>
|
|
#include <vector>
|
|
#include <string>
|
|
#include <utility>
|
|
|
|
#if defined(__GNUG__) && !defined(__clang__)
|
|
# include <cxxabi.h>
|
|
#endif
|
|
|
|
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() = default;
|
|
cpp_function(std::nullptr_t) { }
|
|
|
|
/// Construct a cpp_function from a vanilla function pointer
|
|
template <typename Return, typename... Args, typename... Extra>
|
|
cpp_function(Return (*f)(Args...), const Extra&... extra) {
|
|
initialize(f, f, extra...);
|
|
}
|
|
|
|
/// Construct a cpp_function from a lambda function (possibly with internal state)
|
|
template <typename Func, typename... Extra,
|
|
typename = detail::enable_if_t<detail::is_lambda<Func>::value>>
|
|
cpp_function(Func &&f, const Extra&... extra) {
|
|
initialize(std::forward<Func>(f),
|
|
(detail::function_signature_t<Func> *) nullptr, extra...);
|
|
}
|
|
|
|
/// Construct a cpp_function from a class method (non-const, no ref-qualifier)
|
|
template <typename Return, typename Class, typename... Arg, typename... Extra>
|
|
cpp_function(Return (Class::*f)(Arg...), const Extra&... extra) {
|
|
initialize([f](Class *c, Arg... args) -> Return { return (c->*f)(std::forward<Arg>(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 <typename Return, typename Class, typename... Arg, typename... Extra>
|
|
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, no ref-qualifier)
|
|
template <typename Return, typename Class, typename... Arg, typename... Extra>
|
|
cpp_function(Return (Class::*f)(Arg...) const, const Extra&... extra) {
|
|
initialize([f](const Class *c, Arg... args) -> Return { return (c->*f)(std::forward<Arg>(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 <typename Return, typename Class, typename... Arg, typename... Extra>
|
|
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...);
|
|
}
|
|
|
|
/// Return the function name
|
|
object name() const { return attr("__name__"); }
|
|
|
|
protected:
|
|
struct InitializingFunctionRecordDeleter {
|
|
// `destruct(function_record, false)`: `initialize_generic` copies strings and
|
|
// takes care of cleaning up in case of exceptions. So pass `false` to `free_strings`.
|
|
void operator()(detail::function_record * rec) { destruct(rec, false); }
|
|
};
|
|
using unique_function_record = std::unique_ptr<detail::function_record, InitializingFunctionRecordDeleter>;
|
|
|
|
/// Space optimization: don't inline this frequently instantiated fragment
|
|
PYBIND11_NOINLINE unique_function_record make_function_record() {
|
|
return unique_function_record(new detail::function_record());
|
|
}
|
|
|
|
/// Special internal constructor for functors, lambda functions, etc.
|
|
template <typename Func, typename Return, typename... Args, typename... Extra>
|
|
void initialize(Func &&f, Return (*)(Args...), const Extra&... extra) {
|
|
using namespace detail;
|
|
struct capture { remove_reference_t<Func> f; };
|
|
|
|
/* Store the function including any extra state it might have (e.g. a lambda capture object) */
|
|
// The unique_ptr makes sure nothing is leaked in case of an exception.
|
|
auto unique_rec = make_function_record();
|
|
auto rec = unique_rec.get();
|
|
|
|
/* Store the capture object directly in the function record if there is enough space */
|
|
if (sizeof(capture) <= sizeof(rec->data)) {
|
|
/* Without these pragmas, GCC warns that there might not be
|
|
enough space to use the placement new operator. However, the
|
|
'if' statement above ensures that this is the case. */
|
|
#if defined(__GNUG__) && !defined(__clang__) && __GNUC__ >= 6
|
|
# pragma GCC diagnostic push
|
|
# pragma GCC diagnostic ignored "-Wplacement-new"
|
|
#endif
|
|
new ((capture *) &rec->data) capture { std::forward<Func>(f) };
|
|
#if defined(__GNUG__) && !defined(__clang__) && __GNUC__ >= 6
|
|
# pragma GCC diagnostic pop
|
|
#endif
|
|
if (!std::is_trivially_destructible<Func>::value)
|
|
rec->free_data = [](function_record *r) { ((capture *) &r->data)->~capture(); };
|
|
} else {
|
|
rec->data[0] = new capture { std::forward<Func>(f) };
|
|
rec->free_data = [](function_record *r) { delete ((capture *) r->data[0]); };
|
|
}
|
|
|
|
/* Type casters for the function arguments and return value */
|
|
using cast_in = argument_loader<Args...>;
|
|
using cast_out = make_caster<
|
|
conditional_t<std::is_void<Return>::value, void_type, Return>
|
|
>;
|
|
|
|
static_assert(expected_num_args<Extra...>(sizeof...(Args), cast_in::has_args, cast_in::has_kwargs),
|
|
"The number of argument annotations does not match the number of function arguments");
|
|
|
|
/* Dispatch code which converts function arguments and performs the actual function call */
|
|
rec->impl = [](function_call &call) -> handle {
|
|
cast_in args_converter;
|
|
|
|
/* Try to cast the function arguments into the C++ domain */
|
|
if (!args_converter.load_args(call))
|
|
return PYBIND11_TRY_NEXT_OVERLOAD;
|
|
|
|
/* Invoke call policy pre-call hook */
|
|
process_attributes<Extra...>::precall(call);
|
|
|
|
/* Get a pointer to the capture object */
|
|
auto data = (sizeof(capture) <= sizeof(call.func.data)
|
|
? &call.func.data : call.func.data[0]);
|
|
auto *cap = const_cast<capture *>(reinterpret_cast<const capture *>(data));
|
|
|
|
/* Override policy for rvalues -- usually to enforce rvp::move on an rvalue */
|
|
return_value_policy policy = return_value_policy_override<Return>::policy(call.func.policy);
|
|
|
|
/* Function scope guard -- defaults to the compile-to-nothing `void_type` */
|
|
using Guard = extract_guard_t<Extra...>;
|
|
|
|
/* Perform the function call */
|
|
handle result = cast_out::cast(
|
|
std::move(args_converter).template call<Return, Guard>(cap->f), policy, call.parent);
|
|
|
|
/* Invoke call policy post-call hook */
|
|
process_attributes<Extra...>::postcall(call, result);
|
|
|
|
return result;
|
|
};
|
|
|
|
/* Process any user-provided function attributes */
|
|
process_attributes<Extra...>::init(extra..., rec);
|
|
|
|
{
|
|
constexpr bool has_kw_only_args = any_of<std::is_same<kw_only, Extra>...>::value,
|
|
has_pos_only_args = any_of<std::is_same<pos_only, Extra>...>::value,
|
|
has_args = any_of<std::is_same<args, Args>...>::value,
|
|
has_arg_annotations = any_of<is_keyword<Extra>...>::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();
|
|
|
|
/* Register the function with Python from generic (non-templated) code */
|
|
// Pass on the ownership over the `unique_rec` to `initialize_generic`. `rec` stays valid.
|
|
initialize_generic(std::move(unique_rec), signature.text, types.data(), sizeof...(Args));
|
|
|
|
if (cast_in::has_args) rec->has_args = true;
|
|
if (cast_in::has_kwargs) rec->has_kwargs = true;
|
|
|
|
/* Stash some additional information used by an important optimization in 'functional.h' */
|
|
using FunctionType = Return (*)(Args...);
|
|
constexpr bool is_function_ptr =
|
|
std::is_convertible<Func, FunctionType>::value &&
|
|
sizeof(capture) == sizeof(void *);
|
|
if (is_function_ptr) {
|
|
rec->is_stateless = true;
|
|
rec->data[1] = const_cast<void *>(reinterpret_cast<const void *>(&typeid(FunctionType)));
|
|
}
|
|
}
|
|
|
|
// Utility class that keeps track of all duplicated strings, and cleans them up in its destructor,
|
|
// unless they are released. Basically a RAII-solution to deal with exceptions along the way.
|
|
class strdup_guard {
|
|
public:
|
|
~strdup_guard() {
|
|
for (auto s : strings)
|
|
std::free(s);
|
|
}
|
|
char *operator()(const char *s) {
|
|
auto t = strdup(s);
|
|
strings.push_back(t);
|
|
return t;
|
|
}
|
|
void release() {
|
|
strings.clear();
|
|
}
|
|
private:
|
|
std::vector<char *> strings;
|
|
};
|
|
|
|
/// Register a function call with Python (generic non-templated code goes here)
|
|
void initialize_generic(unique_function_record &&unique_rec, const char *text,
|
|
const std::type_info *const *types, size_t args) {
|
|
// Do NOT receive `unique_rec` by value. If this function fails to move out the unique_ptr,
|
|
// we do not want this to destuct the pointer. `initialize` (the caller) still relies on the
|
|
// pointee being alive after this call. Only move out if a `capsule` is going to keep it alive.
|
|
auto rec = unique_rec.get();
|
|
|
|
// Keep track of strdup'ed strings, and clean them up as long as the function's capsule
|
|
// has not taken ownership yet (when `unique_rec.release()` is called).
|
|
// Note: This cannot easily be fixed by a `unique_ptr` with custom deleter, because the strings
|
|
// are only referenced before strdup'ing. So only *after* the following block could `destruct`
|
|
// safely be called, but even then, `repr` could still throw in the middle of copying all strings.
|
|
strdup_guard guarded_strdup;
|
|
|
|
/* Create copies of all referenced C-style strings */
|
|
rec->name = guarded_strdup(rec->name ? rec->name : "");
|
|
if (rec->doc) rec->doc = guarded_strdup(rec->doc);
|
|
for (auto &a: rec->args) {
|
|
if (a.name)
|
|
a.name = guarded_strdup(a.name);
|
|
if (a.descr)
|
|
a.descr = guarded_strdup(a.descr);
|
|
else if (a.value)
|
|
a.descr = guarded_strdup(repr(a.value).cast<std::string>().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 = detail::get_fully_qualified_tp_name((PyTypeObject *) rec->scope.ptr());
|
|
const auto func_name = std::string(rec->name);
|
|
PyErr_WarnEx(
|
|
PyExc_FutureWarning,
|
|
("pybind11-bound class '" + class_name + "' is using an old-style "
|
|
"placement-new '" + func_name + "' which has been deprecated. See "
|
|
"the upgrade guide in pybind11's docs. This message is only visible "
|
|
"when compiled in debug mode.").c_str(), 0
|
|
);
|
|
}
|
|
#endif
|
|
|
|
/* Generate a proper function signature */
|
|
std::string signature;
|
|
size_t type_index = 0, arg_index = 0;
|
|
for (auto *pc = text; *pc != '\0'; ++pc) {
|
|
const auto c = *pc;
|
|
|
|
if (c == '{') {
|
|
// 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) {
|
|
signature += "self";
|
|
} else {
|
|
signature += "arg" + std::to_string(arg_index - (rec->is_method ? 1 : 0));
|
|
}
|
|
signature += ": ";
|
|
} else if (c == '}') {
|
|
// Write default value if available.
|
|
if (arg_index < rec->args.size() && rec->args[arg_index].descr) {
|
|
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++];
|
|
if (!t)
|
|
pybind11_fail("Internal error while parsing type signature (1)");
|
|
if (auto tinfo = detail::get_type_info(*t)) {
|
|
handle th((PyObject *) tinfo->type);
|
|
signature +=
|
|
th.attr("__module__").cast<std::string>() + "." +
|
|
th.attr("__qualname__").cast<std::string>(); // Python 3.3+, but we backport it to earlier versions
|
|
} else if (rec->is_new_style_constructor && arg_index == 0) {
|
|
// A new-style `__init__` takes `self` as `value_and_holder`.
|
|
// Rewrite it to the proper class type.
|
|
signature +=
|
|
rec->scope.attr("__module__").cast<std::string>() + "." +
|
|
rec->scope.attr("__qualname__").cast<std::string>();
|
|
} else {
|
|
std::string tname(t->name());
|
|
detail::clean_type_id(tname);
|
|
signature += tname;
|
|
}
|
|
} else {
|
|
signature += c;
|
|
}
|
|
}
|
|
|
|
if (arg_index != args || types[type_index] != nullptr)
|
|
pybind11_fail("Internal error while parsing type signature (2)");
|
|
|
|
#if PY_MAJOR_VERSION < 3
|
|
if (strcmp(rec->name, "__next__") == 0) {
|
|
std::free(rec->name);
|
|
rec->name = guarded_strdup("next");
|
|
} else if (strcmp(rec->name, "__bool__") == 0) {
|
|
std::free(rec->name);
|
|
rec->name = guarded_strdup("__nonzero__");
|
|
}
|
|
#endif
|
|
rec->signature = guarded_strdup(signature.c_str());
|
|
rec->args.shrink_to_fit();
|
|
rec->nargs = (std::uint16_t) args;
|
|
|
|
if (rec->sibling && PYBIND11_INSTANCE_METHOD_CHECK(rec->sibling.ptr()))
|
|
rec->sibling = PYBIND11_INSTANCE_METHOD_GET_FUNCTION(rec->sibling.ptr());
|
|
|
|
detail::function_record *chain = nullptr, *chain_start = rec;
|
|
if (rec->sibling) {
|
|
if (PyCFunction_Check(rec->sibling.ptr())) {
|
|
auto rec_capsule = reinterpret_borrow<capsule>(PyCFunction_GET_SELF(rec->sibling.ptr()));
|
|
chain = (detail::function_record *) rec_capsule;
|
|
/* Never append a method to an overload chain of a parent class;
|
|
instead, hide the parent's overloads in this case */
|
|
if (!chain->scope.is(rec->scope))
|
|
chain = nullptr;
|
|
}
|
|
// Don't trigger for things like the default __init__, which are wrapper_descriptors that we are intentionally replacing
|
|
else if (!rec->sibling.is_none() && rec->name[0] != '_')
|
|
pybind11_fail("Cannot overload existing non-function object \"" + std::string(rec->name) +
|
|
"\" with a function of the same name");
|
|
}
|
|
|
|
if (!chain) {
|
|
/* No existing overload was found, create a new function object */
|
|
rec->def = new PyMethodDef();
|
|
std::memset(rec->def, 0, sizeof(PyMethodDef));
|
|
rec->def->ml_name = rec->name;
|
|
rec->def->ml_meth = reinterpret_cast<PyCFunction>(reinterpret_cast<void (*) (void)>(*dispatcher));
|
|
rec->def->ml_flags = METH_VARARGS | METH_KEYWORDS;
|
|
|
|
capsule rec_capsule(unique_rec.release(), [](void *ptr) {
|
|
destruct((detail::function_record *) ptr);
|
|
});
|
|
guarded_strdup.release();
|
|
|
|
object scope_module;
|
|
if (rec->scope) {
|
|
if (hasattr(rec->scope, "__module__")) {
|
|
scope_module = rec->scope.attr("__module__");
|
|
} else if (hasattr(rec->scope, "__name__")) {
|
|
scope_module = rec->scope.attr("__name__");
|
|
}
|
|
}
|
|
|
|
m_ptr = PyCFunction_NewEx(rec->def, rec_capsule.ptr(), scope_module.ptr());
|
|
if (!m_ptr)
|
|
pybind11_fail("cpp_function::cpp_function(): Could not allocate function object");
|
|
} else {
|
|
/* Append at the beginning or end of the overload chain */
|
|
m_ptr = rec->sibling.ptr();
|
|
inc_ref();
|
|
if (chain->is_method != rec->is_method)
|
|
pybind11_fail("overloading a method with both static and instance methods is not supported; "
|
|
#if defined(NDEBUG)
|
|
"compile in debug mode for more details"
|
|
#else
|
|
"error while attempting to bind " + std::string(rec->is_method ? "instance" : "static") + " method " +
|
|
std::string(pybind11::str(rec->scope.attr("__name__"))) + "." + std::string(rec->name) + signature
|
|
#endif
|
|
);
|
|
|
|
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<capsule>(((PyCFunctionObject *) m_ptr)->m_self);
|
|
rec_capsule.set_pointer(unique_rec.release());
|
|
guarded_strdup.release();
|
|
} else {
|
|
// Or end of chain (normal behavior)
|
|
chain_start = chain;
|
|
while (chain->next)
|
|
chain = chain->next;
|
|
chain->next = unique_rec.release();
|
|
guarded_strdup.release();
|
|
}
|
|
}
|
|
|
|
std::string signatures;
|
|
int index = 0;
|
|
/* Create a nice pydoc rec including all signatures and
|
|
docstrings of the functions in the overload chain */
|
|
if (chain && options::show_function_signatures()) {
|
|
// First a generic signature
|
|
signatures += rec->name;
|
|
signatures += "(*args, **kwargs)\n";
|
|
signatures += "Overloaded function.\n\n";
|
|
}
|
|
// Then specific overload signatures
|
|
bool first_user_def = true;
|
|
for (auto it = chain_start; it != nullptr; it = it->next) {
|
|
if (options::show_function_signatures()) {
|
|
if (index > 0) signatures += "\n";
|
|
if (chain)
|
|
signatures += std::to_string(++index) + ". ";
|
|
signatures += rec->name;
|
|
signatures += it->signature;
|
|
signatures += "\n";
|
|
}
|
|
if (it->doc && strlen(it->doc) > 0 && options::show_user_defined_docstrings()) {
|
|
// If we're appending another docstring, and aren't printing function signatures, we
|
|
// need to append a newline first:
|
|
if (!options::show_function_signatures()) {
|
|
if (first_user_def) first_user_def = false;
|
|
else signatures += "\n";
|
|
}
|
|
if (options::show_function_signatures()) signatures += "\n";
|
|
signatures += it->doc;
|
|
if (options::show_function_signatures()) signatures += "\n";
|
|
}
|
|
}
|
|
|
|
/* Install docstring */
|
|
auto *func = (PyCFunctionObject *) m_ptr;
|
|
std::free(const_cast<char *>(func->m_ml->ml_doc));
|
|
// Install docstring if it's non-empty (when at least one option is enabled)
|
|
func->m_ml->ml_doc = signatures.empty() ? nullptr : strdup(signatures.c_str());
|
|
|
|
if (rec->is_method) {
|
|
m_ptr = PYBIND11_INSTANCE_METHOD_NEW(m_ptr, rec->scope.ptr());
|
|
if (!m_ptr)
|
|
pybind11_fail("cpp_function::cpp_function(): Could not allocate instance method object");
|
|
Py_DECREF(func);
|
|
}
|
|
}
|
|
|
|
/// When a cpp_function is GCed, release any memory allocated by pybind11
|
|
static void destruct(detail::function_record *rec, bool free_strings = true) {
|
|
// 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)
|
|
rec->free_data(rec);
|
|
// During initialization, these strings might not have been copied yet,
|
|
// so they cannot be freed. Once the function has been created, they can.
|
|
// Check `make_function_record` for more details.
|
|
if (free_strings) {
|
|
std::free((char *) rec->name);
|
|
std::free((char *) rec->doc);
|
|
std::free((char *) rec->signature);
|
|
for (auto &arg: rec->args) {
|
|
std::free(const_cast<char *>(arg.name));
|
|
std::free(const_cast<char *>(arg.descr));
|
|
}
|
|
}
|
|
for (auto &arg: rec->args)
|
|
arg.value.dec_ref();
|
|
if (rec->def) {
|
|
std::free(const_cast<char *>(rec->def->ml_doc));
|
|
// 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;
|
|
}
|
|
}
|
|
|
|
/// Main dispatch logic for calls to functions bound using pybind11
|
|
static PyObject *dispatcher(PyObject *self, PyObject *args_in, PyObject *kwargs_in) {
|
|
using namespace detail;
|
|
|
|
/* Iterator over the list of potentially admissible overloads */
|
|
const function_record *overloads = (function_record *) PyCapsule_GetPointer(self, nullptr),
|
|
*it = overloads;
|
|
|
|
/* Need to know how many arguments + keyword arguments there are to pick the right overload */
|
|
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;
|
|
|
|
auto self_value_and_holder = value_and_holder();
|
|
if (overloads->is_constructor) {
|
|
if (!PyObject_TypeCheck(parent.ptr(), (PyTypeObject *) overloads->scope.ptr())) {
|
|
PyErr_SetString(PyExc_TypeError, "__init__(self, ...) called with invalid `self` argument");
|
|
return nullptr;
|
|
}
|
|
|
|
const auto tinfo = get_type_info((PyTypeObject *) overloads->scope.ptr());
|
|
const auto pi = reinterpret_cast<instance *>(parent.ptr());
|
|
self_value_and_holder = pi->get_value_and_holder(tinfo, true);
|
|
|
|
// If this value is already registered it must mean __init__ is invoked multiple times;
|
|
// we really can't support that in C++, so just ignore the second __init__.
|
|
if (self_value_and_holder.instance_registered())
|
|
return none().release().ptr();
|
|
}
|
|
|
|
try {
|
|
// We do this in two passes: in the first pass, we load arguments with `convert=false`;
|
|
// in the second, we allow conversion (except for arguments with an explicit
|
|
// py::arg().noconvert()). This lets us prefer calls without conversion, with
|
|
// conversion as a fallback.
|
|
std::vector<function_call> second_pass;
|
|
|
|
// However, if there are no overloads, we can just skip the no-convert pass entirely
|
|
const bool overloaded = it != nullptr && it->next != nullptr;
|
|
|
|
for (; it != nullptr; it = it->next) {
|
|
|
|
/* For each overload:
|
|
1. Copy all positional arguments we were given, also checking to make sure that
|
|
named positional arguments weren't *also* specified via kwarg.
|
|
2. If we weren't given enough, try to make up the omitted ones by checking
|
|
whether they were provided by a kwarg matching the `py::arg("name")` name. If
|
|
so, use it (and remove it from kwargs; if not, see if the function binding
|
|
provided a default that we can use.
|
|
3. Ensure that either all keyword arguments were "consumed", or that the function
|
|
takes a kwargs argument to accept unconsumed kwargs.
|
|
4. Any positional arguments still left get put into a tuple (for args), and any
|
|
leftover kwargs get put into a dict.
|
|
5. Pack everything into a vector; if we have py::args or py::kwargs, they are an
|
|
extra tuple or dict at the end of the positional arguments.
|
|
6. Call the function call dispatcher (function_record::impl)
|
|
|
|
If one of these fail, move on to the next overload and keep trying until we get a
|
|
result other than PYBIND11_TRY_NEXT_OVERLOAD.
|
|
*/
|
|
|
|
const function_record &func = *it;
|
|
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 positional arguments for this overload
|
|
|
|
if (n_args_in < pos_args && func.args.size() < pos_args)
|
|
continue; // Not enough positional arguments given, and not enough defaults to fill in the blanks
|
|
|
|
function_call call(func, parent);
|
|
|
|
size_t args_to_copy = (std::min)(pos_args, n_args_in); // Protect std::min with parentheses
|
|
size_t args_copied = 0;
|
|
|
|
// 0. Inject new-style `self` argument
|
|
if (func.is_new_style_constructor) {
|
|
// The `value` may have been preallocated by an old-style `__init__`
|
|
// if it was a preceding candidate for overload resolution.
|
|
if (self_value_and_holder)
|
|
self_value_and_holder.type->dealloc(self_value_and_holder);
|
|
|
|
call.init_self = PyTuple_GET_ITEM(args_in, 0);
|
|
call.args.emplace_back(reinterpret_cast<PyObject *>(&self_value_and_holder));
|
|
call.args_convert.push_back(false);
|
|
++args_copied;
|
|
}
|
|
|
|
// 1. Copy any position arguments given.
|
|
bool bad_arg = false;
|
|
for (; args_copied < args_to_copy; ++args_copied) {
|
|
const argument_record *arg_rec = args_copied < func.args.size() ? &func.args[args_copied] : nullptr;
|
|
if (kwargs_in && arg_rec && arg_rec->name && PyDict_GetItemString(kwargs_in, arg_rec->name)) {
|
|
bad_arg = true;
|
|
break;
|
|
}
|
|
|
|
handle arg(PyTuple_GET_ITEM(args_in, args_copied));
|
|
if (arg_rec && !arg_rec->none && arg.is_none()) {
|
|
bad_arg = true;
|
|
break;
|
|
}
|
|
call.args.push_back(arg);
|
|
call.args_convert.push_back(arg_rec ? arg_rec->convert : true);
|
|
}
|
|
if (bad_arg)
|
|
continue; // Maybe it was meant for another overload (issue #688)
|
|
|
|
// We'll need to copy this if we steal some kwargs for defaults
|
|
dict kwargs = reinterpret_borrow<dict>(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 < num_args) {
|
|
bool copied_kwargs = false;
|
|
|
|
for (; args_copied < num_args; ++args_copied) {
|
|
const auto &arg_rec = func.args[args_copied];
|
|
|
|
handle value;
|
|
if (kwargs_in && arg_rec.name)
|
|
value = PyDict_GetItemString(kwargs.ptr(), arg_rec.name);
|
|
|
|
if (value) {
|
|
// Consume a kwargs value
|
|
if (!copied_kwargs) {
|
|
kwargs = reinterpret_steal<dict>(PyDict_Copy(kwargs.ptr()));
|
|
copied_kwargs = true;
|
|
}
|
|
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_rec.convert);
|
|
}
|
|
else
|
|
break;
|
|
}
|
|
|
|
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.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
|
|
if (func.has_args) {
|
|
tuple extra_args;
|
|
if (args_to_copy == 0) {
|
|
// We didn't copy out any position arguments from the args_in tuple, so we
|
|
// can reuse it directly without copying:
|
|
extra_args = reinterpret_borrow<tuple>(args_in);
|
|
} else if (args_copied >= n_args_in) {
|
|
extra_args = tuple(0);
|
|
} else {
|
|
size_t args_size = n_args_in - args_copied;
|
|
extra_args = tuple(args_size);
|
|
for (size_t i = 0; i < args_size; ++i) {
|
|
extra_args[i] = PyTuple_GET_ITEM(args_in, args_copied + i);
|
|
}
|
|
}
|
|
call.args.push_back(extra_args);
|
|
call.args_convert.push_back(false);
|
|
call.args_ref = std::move(extra_args);
|
|
}
|
|
|
|
// 4b. If we have a py::kwargs, pass on any remaining kwargs
|
|
if (func.has_kwargs) {
|
|
if (!kwargs.ptr())
|
|
kwargs = dict(); // If we didn't get one, send an empty one
|
|
call.args.push_back(kwargs);
|
|
call.args_convert.push_back(false);
|
|
call.kwargs_ref = std::move(kwargs);
|
|
}
|
|
|
|
// 5. Put everything in a vector. Not technically step 5, we've been building it
|
|
// in `call.args` all along.
|
|
#if !defined(NDEBUG)
|
|
if (call.args.size() != func.nargs || call.args_convert.size() != func.nargs)
|
|
pybind11_fail("Internal error: function call dispatcher inserted wrong number of arguments!");
|
|
#endif
|
|
|
|
std::vector<bool> second_pass_convert;
|
|
if (overloaded) {
|
|
// We're in the first no-convert pass, so swap out the conversion flags for a
|
|
// set of all-false flags. If the call fails, we'll swap the flags back in for
|
|
// the conversion-allowed call below.
|
|
second_pass_convert.resize(func.nargs, false);
|
|
call.args_convert.swap(second_pass_convert);
|
|
}
|
|
|
|
// 6. Call the function.
|
|
try {
|
|
loader_life_support guard{};
|
|
result = func.impl(call);
|
|
} catch (reference_cast_error &) {
|
|
result = PYBIND11_TRY_NEXT_OVERLOAD;
|
|
}
|
|
|
|
if (result.ptr() != PYBIND11_TRY_NEXT_OVERLOAD)
|
|
break;
|
|
|
|
if (overloaded) {
|
|
// The (overloaded) call failed; if the call has at least one argument that
|
|
// permits conversion (i.e. it hasn't been explicitly specified `.noconvert()`)
|
|
// then add this call to the list of second pass overloads to try.
|
|
for (size_t i = func.is_method ? 1 : 0; i < pos_args; i++) {
|
|
if (second_pass_convert[i]) {
|
|
// Found one: swap the converting flags back in and store the call for
|
|
// the second pass.
|
|
call.args_convert.swap(second_pass_convert);
|
|
second_pass.push_back(std::move(call));
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
if (overloaded && !second_pass.empty() && result.ptr() == PYBIND11_TRY_NEXT_OVERLOAD) {
|
|
// The no-conversion pass finished without success, try again with conversion allowed
|
|
for (auto &call : second_pass) {
|
|
try {
|
|
loader_life_support guard{};
|
|
result = call.func.impl(call);
|
|
} catch (reference_cast_error &) {
|
|
result = PYBIND11_TRY_NEXT_OVERLOAD;
|
|
}
|
|
|
|
if (result.ptr() != PYBIND11_TRY_NEXT_OVERLOAD) {
|
|
// The error reporting logic below expects 'it' to be valid, as it would be
|
|
// if we'd encountered this failure in the first-pass loop.
|
|
if (!result)
|
|
it = &call.func;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
} catch (error_already_set &e) {
|
|
e.restore();
|
|
return nullptr;
|
|
#ifdef __GLIBCXX__
|
|
} catch ( abi::__forced_unwind& ) {
|
|
throw;
|
|
#endif
|
|
} catch (...) {
|
|
/* When an exception is caught, give each registered exception
|
|
translator a chance to translate it to a Python exception
|
|
in reverse order of registration.
|
|
|
|
A translator may choose to do one of the following:
|
|
|
|
- catch the exception and call PyErr_SetString or PyErr_SetObject
|
|
to set a standard (or custom) Python exception, or
|
|
- do nothing and let the exception fall through to the next translator, or
|
|
- delegate translation to the next translator by throwing a new type of exception. */
|
|
|
|
auto last_exception = std::current_exception();
|
|
auto ®istered_exception_translators = get_internals().registered_exception_translators;
|
|
for (auto& translator : registered_exception_translators) {
|
|
try {
|
|
translator(last_exception);
|
|
} catch (...) {
|
|
last_exception = std::current_exception();
|
|
continue;
|
|
}
|
|
return nullptr;
|
|
}
|
|
PyErr_SetString(PyExc_SystemError, "Exception escaped from default exception translator!");
|
|
return nullptr;
|
|
}
|
|
|
|
auto append_note_if_missing_header_is_suspected = [](std::string &msg) {
|
|
if (msg.find("std::") != std::string::npos) {
|
|
msg += "\n\n"
|
|
"Did you forget to `#include <pybind11/stl.h>`? Or <pybind11/complex.h>,\n"
|
|
"<pybind11/functional.h>, <pybind11/chrono.h>, etc. Some automatic\n"
|
|
"conversions are optional and require extra headers to be included\n"
|
|
"when compiling your pybind11 module.";
|
|
}
|
|
};
|
|
|
|
if (result.ptr() == PYBIND11_TRY_NEXT_OVERLOAD) {
|
|
if (overloads->is_operator)
|
|
return handle(Py_NotImplemented).inc_ref().ptr();
|
|
|
|
std::string msg = std::string(overloads->name) + "(): incompatible " +
|
|
std::string(overloads->is_constructor ? "constructor" : "function") +
|
|
" arguments. The following argument types are supported:\n";
|
|
|
|
int ctr = 0;
|
|
for (const function_record *it2 = overloads; it2 != nullptr; it2 = it2->next) {
|
|
msg += " "+ std::to_string(++ctr) + ". ";
|
|
|
|
bool wrote_sig = false;
|
|
if (overloads->is_constructor) {
|
|
// For a constructor, rewrite `(self: Object, arg0, ...) -> NoneType` as `Object(arg0, ...)`
|
|
std::string sig = it2->signature;
|
|
size_t start = sig.find('(') + 7; // skip "(self: "
|
|
if (start < sig.size()) {
|
|
// End at the , for the next argument
|
|
size_t end = sig.find(", "), next = end + 2;
|
|
size_t ret = sig.rfind(" -> ");
|
|
// Or the ), if there is no comma:
|
|
if (end >= sig.size()) next = end = sig.find(')');
|
|
if (start < end && next < sig.size()) {
|
|
msg.append(sig, start, end - start);
|
|
msg += '(';
|
|
msg.append(sig, next, ret - next);
|
|
wrote_sig = true;
|
|
}
|
|
}
|
|
}
|
|
if (!wrote_sig) msg += it2->signature;
|
|
|
|
msg += "\n";
|
|
}
|
|
msg += "\nInvoked with: ";
|
|
auto args_ = reinterpret_borrow<tuple>(args_in);
|
|
bool some_args = false;
|
|
for (size_t ti = overloads->is_constructor ? 1 : 0; ti < args_.size(); ++ti) {
|
|
if (!some_args) some_args = true;
|
|
else msg += ", ";
|
|
try {
|
|
msg += pybind11::repr(args_[ti]);
|
|
} catch (const error_already_set&) {
|
|
msg += "<repr raised Error>";
|
|
}
|
|
}
|
|
if (kwargs_in) {
|
|
auto kwargs = reinterpret_borrow<dict>(kwargs_in);
|
|
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("{}=").format(kwarg.first);
|
|
try {
|
|
msg += pybind11::repr(kwarg.second);
|
|
} catch (const error_already_set&) {
|
|
msg += "<repr raised Error>";
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
append_note_if_missing_header_is_suspected(msg);
|
|
PyErr_SetString(PyExc_TypeError, msg.c_str());
|
|
return nullptr;
|
|
} else if (!result) {
|
|
std::string msg = "Unable to convert function return value to a "
|
|
"Python type! The signature was\n\t";
|
|
msg += it->signature;
|
|
append_note_if_missing_header_is_suspected(msg);
|
|
PyErr_SetString(PyExc_TypeError, msg.c_str());
|
|
return nullptr;
|
|
} else {
|
|
if (overloads->is_constructor && !self_value_and_holder.holder_constructed()) {
|
|
auto *pi = reinterpret_cast<instance *>(parent.ptr());
|
|
self_value_and_holder.type->init_instance(pi, nullptr);
|
|
}
|
|
return result.ptr();
|
|
}
|
|
}
|
|
};
|
|
|
|
/// Wrapper for Python extension modules
|
|
class module_ : public object {
|
|
public:
|
|
PYBIND11_OBJECT_DEFAULT(module_, object, PyModule_Check)
|
|
|
|
/// Create a new top-level Python module with the given name and docstring
|
|
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
|
|
*this = create_extension_module(name, doc, new PyModuleDef());
|
|
#else
|
|
*this = create_extension_module(name, doc, nullptr);
|
|
#endif
|
|
}
|
|
|
|
/** \rst
|
|
Create Python binding for a new function within the module scope. ``Func``
|
|
can be a plain C++ function, a function pointer, or a lambda function. For
|
|
details on the ``Extra&& ... extra`` argument, see section :ref:`extras`.
|
|
\endrst */
|
|
template <typename Func, typename... Extra>
|
|
module_ &def(const char *name_, Func &&f, const Extra& ... extra) {
|
|
cpp_function func(std::forward<Func>(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
|
|
// overwriting (and has already checked internally that it isn't overwriting non-functions).
|
|
add_object(name_, func, true /* overwrite */);
|
|
return *this;
|
|
}
|
|
|
|
/** \rst
|
|
Create and return a new Python submodule with the given name and docstring.
|
|
This also works recursively, i.e.
|
|
|
|
.. 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'");
|
|
\endrst */
|
|
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<module_>(PyImport_AddModule(full_name.c_str()));
|
|
if (doc && options::show_user_defined_docstrings())
|
|
result.attr("__doc__") = pybind11::str(doc);
|
|
attr(name) = result;
|
|
return result;
|
|
}
|
|
|
|
/// Import and return a module or throws `error_already_set`.
|
|
static module_ import(const char *name) {
|
|
PyObject *obj = PyImport_ImportModule(name);
|
|
if (!obj)
|
|
throw error_already_set();
|
|
return reinterpret_steal<module_>(obj);
|
|
}
|
|
|
|
/// Reload the module or throws `error_already_set`.
|
|
void reload() {
|
|
PyObject *obj = PyImport_ReloadModule(ptr());
|
|
if (!obj)
|
|
throw error_already_set();
|
|
*this = reinterpret_steal<module_>(obj);
|
|
}
|
|
|
|
/** \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 \"" +
|
|
std::string(name) + "\"");
|
|
|
|
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 statically 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: Should 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<module_>(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<dict>(p ? p : module_::import("__main__").attr("__dict__").ptr());
|
|
}
|
|
|
|
PYBIND11_NAMESPACE_BEGIN(detail)
|
|
/// Generic support for creating new Python heap types
|
|
class generic_type : public object {
|
|
public:
|
|
PYBIND11_OBJECT_DEFAULT(generic_type, object, PyType_Check)
|
|
protected:
|
|
void initialize(const type_record &rec,
|
|
void *(*type_caster_module_local_load)(PyObject *, const type_info *)) {
|
|
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");
|
|
|
|
if (rec.module_local ? get_local_type_info(*rec.type) : get_global_type_info(*rec.type))
|
|
pybind11_fail("generic_type: type \"" + std::string(rec.name) +
|
|
"\" is already registered!");
|
|
|
|
m_ptr = make_new_python_type(rec);
|
|
|
|
/* Register supplemental type information in C++ dict */
|
|
auto *tinfo = new detail::type_info();
|
|
tinfo->type = (PyTypeObject *) m_ptr;
|
|
tinfo->cpptype = rec.type;
|
|
tinfo->type_size = rec.type_size;
|
|
tinfo->type_align = rec.type_align;
|
|
tinfo->operator_new = rec.operator_new;
|
|
tinfo->holder_size_in_ptrs = size_in_ptrs(rec.holder_size);
|
|
tinfo->init_instance = rec.init_instance;
|
|
tinfo->dealloc = rec.dealloc;
|
|
tinfo->simple_type = true;
|
|
tinfo->simple_ancestors = true;
|
|
tinfo->default_holder = rec.default_holder;
|
|
tinfo->module_local = rec.module_local;
|
|
|
|
auto &internals = get_internals();
|
|
auto tindex = std::type_index(*rec.type);
|
|
tinfo->direct_conversions = &internals.direct_conversions[tindex];
|
|
if (rec.module_local)
|
|
registered_local_types_cpp()[tindex] = tinfo;
|
|
else
|
|
internals.registered_types_cpp[tindex] = tinfo;
|
|
internals.registered_types_py[(PyTypeObject *) m_ptr] = { tinfo };
|
|
|
|
if (rec.bases.size() > 1 || rec.multiple_inheritance) {
|
|
mark_parents_nonsimple(tinfo->type);
|
|
tinfo->simple_ancestors = false;
|
|
}
|
|
else if (rec.bases.size() == 1) {
|
|
auto parent_tinfo = get_type_info((PyTypeObject *) rec.bases[0].ptr());
|
|
tinfo->simple_ancestors = parent_tinfo->simple_ancestors;
|
|
}
|
|
|
|
if (rec.module_local) {
|
|
// Stash the local typeinfo and loader so that external modules can access it.
|
|
tinfo->module_local_load = type_caster_module_local_load;
|
|
setattr(m_ptr, PYBIND11_MODULE_LOCAL_ID, capsule(tinfo));
|
|
}
|
|
}
|
|
|
|
/// Helper function which tags all parents of a type using mult. inheritance
|
|
void mark_parents_nonsimple(PyTypeObject *value) {
|
|
auto t = reinterpret_borrow<tuple>(value->tp_bases);
|
|
for (handle h : t) {
|
|
auto tinfo2 = get_type_info((PyTypeObject *) h.ptr());
|
|
if (tinfo2)
|
|
tinfo2->simple_type = false;
|
|
mark_parents_nonsimple((PyTypeObject *) h.ptr());
|
|
}
|
|
}
|
|
|
|
void install_buffer_funcs(
|
|
buffer_info *(*get_buffer)(PyObject *, void *),
|
|
void *get_buffer_data) {
|
|
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 '" +
|
|
get_fully_qualified_tp_name(tinfo->type) +
|
|
"' the associated class<>(..) invocation must "
|
|
"include the pybind11::buffer_protocol() annotation!");
|
|
|
|
tinfo->get_buffer = get_buffer;
|
|
tinfo->get_buffer_data = get_buffer_data;
|
|
}
|
|
|
|
// rec_func must be set for either fget or fset.
|
|
void def_property_static_impl(const char *name,
|
|
handle fget, handle fset,
|
|
detail::function_record *rec_func) {
|
|
const auto is_static = rec_func && !(rec_func->is_method && rec_func->scope);
|
|
const auto has_doc = rec_func && rec_func->doc && pybind11::options::show_user_defined_docstrings();
|
|
auto property = handle((PyObject *) (is_static ? get_internals().static_property_type
|
|
: &PyProperty_Type));
|
|
attr(name) = property(fget.ptr() ? fget : none(),
|
|
fset.ptr() ? fset : none(),
|
|
/*deleter*/none(),
|
|
pybind11::str(has_doc ? rec_func->doc : ""));
|
|
}
|
|
};
|
|
|
|
/// Set the pointer to operator new if it exists. The cast is needed because it can be overloaded.
|
|
template <typename T, typename = void_t<decltype(static_cast<void *(*)(size_t)>(T::operator new))>>
|
|
void set_operator_new(type_record *r) { r->operator_new = &T::operator new; }
|
|
|
|
template <typename> void set_operator_new(...) { }
|
|
|
|
template <typename T, typename SFINAE = void> struct has_operator_delete : std::false_type { };
|
|
template <typename T> struct has_operator_delete<T, void_t<decltype(static_cast<void (*)(void *)>(T::operator delete))>>
|
|
: std::true_type { };
|
|
template <typename T, typename SFINAE = void> struct has_operator_delete_size : std::false_type { };
|
|
template <typename T> struct has_operator_delete_size<T, void_t<decltype(static_cast<void (*)(void *, size_t)>(T::operator delete))>>
|
|
: std::true_type { };
|
|
/// Call class-specific delete if it exists or global otherwise. Can also be an overload set.
|
|
template <typename T, enable_if_t<has_operator_delete<T>::value, int> = 0>
|
|
void call_operator_delete(T *p, size_t, size_t) { T::operator delete(p); }
|
|
template <typename T, enable_if_t<!has_operator_delete<T>::value && has_operator_delete_size<T>::value, int> = 0>
|
|
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(__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
|
|
}
|
|
|
|
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.
|
|
template <typename /*Derived*/, typename F>
|
|
auto method_adaptor(F &&f) -> decltype(std::forward<F>(f)) { return std::forward<F>(f); }
|
|
|
|
template <typename Derived, typename Return, typename Class, typename... Args>
|
|
auto method_adaptor(Return (Class::*pmf)(Args...)) -> Return (Derived::*)(Args...) {
|
|
static_assert(detail::is_accessible_base_of<Class, Derived>::value,
|
|
"Cannot bind an inaccessible base class method; use a lambda definition instead");
|
|
return pmf;
|
|
}
|
|
|
|
template <typename Derived, typename Return, typename Class, typename... Args>
|
|
auto method_adaptor(Return (Class::*pmf)(Args...) const) -> Return (Derived::*)(Args...) const {
|
|
static_assert(detail::is_accessible_base_of<Class, Derived>::value,
|
|
"Cannot bind an inaccessible base class method; use a lambda definition instead");
|
|
return pmf;
|
|
}
|
|
|
|
// clang-format on
|
|
template <typename T>
|
|
#ifndef PYBIND11_USE_SMART_HOLDER_AS_DEFAULT
|
|
|
|
using default_holder_type = std::unique_ptr<T>;
|
|
|
|
# define PYBIND11_SMART_POINTER_HOLDER_TYPE_CASTERS(T, ...)
|
|
|
|
#else
|
|
|
|
using default_holder_type = smart_holder;
|
|
|
|
// This define could be hidden away inside detail/smart_holder_type_casters.h, but is kept here
|
|
// for clarity.
|
|
# define PYBIND11_SMART_POINTER_HOLDER_TYPE_CASTERS(T, ...) \
|
|
namespace pybind11 { \
|
|
namespace detail { \
|
|
template <> \
|
|
class type_caster<T> : public type_caster_base<T> {}; \
|
|
template <> \
|
|
class type_caster<__VA_ARGS__> : public type_caster_holder<T, __VA_ARGS__> {}; \
|
|
} \
|
|
}
|
|
|
|
#endif
|
|
// clang-format off
|
|
|
|
template <typename type_, typename... options>
|
|
class class_ : public detail::generic_type {
|
|
template <typename T> using is_subtype = detail::is_strict_base_of<type_, T>;
|
|
template <typename T> using is_base = detail::is_strict_base_of<T, type_>;
|
|
template <typename T>
|
|
// clang-format on
|
|
using is_holder
|
|
= detail::any_of<detail::is_holder_type<type_, T>,
|
|
detail::all_of<detail::negation<is_base<T>>,
|
|
detail::negation<is_subtype<T>>,
|
|
detail::type_uses_smart_holder_type_caster<type_>>>;
|
|
// clang-format off
|
|
// struct instead of using here to help MSVC:
|
|
template <typename T> struct is_valid_class_option :
|
|
detail::any_of<is_holder<T>, is_subtype<T>, is_base<T>> {};
|
|
|
|
public:
|
|
using type = type_;
|
|
using type_alias = detail::exactly_one_t<is_subtype, void, options...>;
|
|
constexpr static bool has_alias = !std::is_void<type_alias>::value;
|
|
using holder_type = detail::exactly_one_t<is_holder, default_holder_type<type>, options...>;
|
|
|
|
static_assert(detail::all_of<is_valid_class_option<options>...>::value,
|
|
"Unknown/invalid class_ template parameters provided");
|
|
|
|
static_assert(!has_alias || std::is_polymorphic<type>::value,
|
|
"Cannot use an alias class with a non-polymorphic type");
|
|
|
|
PYBIND11_OBJECT(class_, generic_type, PyType_Check)
|
|
|
|
template <typename... Extra>
|
|
class_(handle scope, const char *name, const Extra &... extra) {
|
|
using namespace detail;
|
|
|
|
// MI can only be specified via class_ template options, not constructor parameters
|
|
static_assert(
|
|
none_of<is_pyobject<Extra>...>::value || // no base class arguments, or:
|
|
( constexpr_sum(is_pyobject<Extra>::value...) == 1 && // Exactly one base
|
|
constexpr_sum(is_base<options>::value...) == 0 && // no template option bases
|
|
none_of<std::is_same<multiple_inheritance, Extra>...>::value), // no multiple_inheritance attr
|
|
"Error: multiple inheritance bases must be specified via class_ template options");
|
|
|
|
// clang-format on
|
|
static constexpr bool holder_is_smart_holder
|
|
= detail::is_smart_holder_type<holder_type>::value;
|
|
static constexpr bool wrapped_type_uses_smart_holder_type_caster
|
|
= detail::type_uses_smart_holder_type_caster<type>::value;
|
|
static constexpr bool type_caster_type_is_type_caster_base_subtype
|
|
= std::is_base_of<detail::type_caster_base<type>, detail::type_caster<type>>::value;
|
|
// Necessary conditions, but not strict.
|
|
static_assert(
|
|
!(detail::is_instantiation<std::unique_ptr, holder_type>::value
|
|
&& wrapped_type_uses_smart_holder_type_caster),
|
|
"py::class_ holder vs type_caster mismatch:"
|
|
" missing PYBIND11_SMART_POINTER_HOLDER_TYPE_CASTERS(T, std::unique_ptr<T>)?");
|
|
static_assert(
|
|
!(detail::is_instantiation<std::shared_ptr, holder_type>::value
|
|
&& wrapped_type_uses_smart_holder_type_caster),
|
|
"py::class_ holder vs type_caster mismatch:"
|
|
" missing PYBIND11_SMART_POINTER_HOLDER_TYPE_CASTERS(T, std::shared_ptr<T>)?");
|
|
static_assert(!(holder_is_smart_holder && type_caster_type_is_type_caster_base_subtype),
|
|
"py::class_ holder vs type_caster mismatch:"
|
|
" missing PYBIND11_SMART_HOLDER_TYPE_CASTERS(T)?");
|
|
#ifdef PYBIND11_STRICT_ASSERTS_CLASS_HOLDER_VS_TYPE_CASTER_MIX
|
|
// Strict conditions cannot be enforced universally at the moment (PR #2836).
|
|
static_assert(holder_is_smart_holder == wrapped_type_uses_smart_holder_type_caster,
|
|
"py::class_ holder vs type_caster mismatch:"
|
|
" missing PYBIND11_SMART_HOLDER_TYPE_CASTERS(T)"
|
|
" or collision with custom py::detail::type_caster<T>?");
|
|
static_assert(!holder_is_smart_holder == type_caster_type_is_type_caster_base_subtype,
|
|
"py::class_ holder vs type_caster mismatch:"
|
|
" missing PYBIND11_SMART_POINTER_HOLDER_TYPE_CASTERS(T, ...)"
|
|
" or collision with custom py::detail::type_caster<T>?");
|
|
#endif
|
|
// clang-format off
|
|
type_record record;
|
|
record.scope = scope;
|
|
record.name = name;
|
|
record.type = &typeid(type);
|
|
record.type_size = sizeof(conditional_t<has_alias, type_alias, type>);
|
|
record.type_align = alignof(conditional_t<has_alias, type_alias, type>&);
|
|
record.holder_size = sizeof(holder_type);
|
|
record.init_instance = init_instance;
|
|
record.dealloc = dealloc;
|
|
|
|
// A more fitting name would be uses_unique_ptr_holder.
|
|
record.default_holder = detail::is_instantiation<std::unique_ptr, holder_type>::value;
|
|
|
|
set_operator_new<type>(&record);
|
|
|
|
/* Register base classes specified via template arguments to class_, if any */
|
|
PYBIND11_EXPAND_SIDE_EFFECTS(add_base<options>(record));
|
|
|
|
/* Process optional arguments, if any */
|
|
process_attributes<Extra...>::init(extra..., &record);
|
|
|
|
generic_type_initialize(record);
|
|
|
|
if (has_alias) {
|
|
auto &instances = record.module_local ? registered_local_types_cpp() : get_internals().registered_types_cpp;
|
|
instances[std::type_index(typeid(type_alias))] = instances[std::type_index(typeid(type))];
|
|
}
|
|
}
|
|
|
|
template <typename Base, detail::enable_if_t<is_base<Base>::value, int> = 0>
|
|
static void add_base(detail::type_record &rec) {
|
|
rec.add_base(typeid(Base), [](void *src) -> void * {
|
|
return static_cast<Base *>(reinterpret_cast<type *>(src));
|
|
});
|
|
}
|
|
|
|
template <typename Base, detail::enable_if_t<!is_base<Base>::value, int> = 0>
|
|
static void add_base(detail::type_record &) { }
|
|
|
|
template <typename Func, typename... Extra>
|
|
class_ &def(const char *name_, Func&& f, const Extra&... extra) {
|
|
cpp_function cf(method_adaptor<type>(std::forward<Func>(f)), name(name_), is_method(*this),
|
|
sibling(getattr(*this, name_, none())), extra...);
|
|
add_class_method(*this, name_, cf);
|
|
return *this;
|
|
}
|
|
|
|
template <typename Func, typename... Extra> class_ &
|
|
def_static(const char *name_, Func &&f, const Extra&... extra) {
|
|
static_assert(!std::is_member_function_pointer<Func>::value,
|
|
"def_static(...) called with a non-static member function pointer");
|
|
cpp_function cf(std::forward<Func>(f), name(name_), scope(*this),
|
|
sibling(getattr(*this, name_, none())), extra...);
|
|
attr(cf.name()) = staticmethod(cf);
|
|
return *this;
|
|
}
|
|
|
|
template <detail::op_id id, detail::op_type ot, typename L, typename R, typename... Extra>
|
|
class_ &def(const detail::op_<id, ot, L, R> &op, const Extra&... extra) {
|
|
op.execute(*this, extra...);
|
|
return *this;
|
|
}
|
|
|
|
template <detail::op_id id, detail::op_type ot, typename L, typename R, typename... Extra>
|
|
class_ & def_cast(const detail::op_<id, ot, L, R> &op, const Extra&... extra) {
|
|
op.execute_cast(*this, extra...);
|
|
return *this;
|
|
}
|
|
|
|
template <typename... Args, typename... Extra>
|
|
class_ &def(const detail::initimpl::constructor<Args...> &init, const Extra&... extra) {
|
|
init.execute(*this, extra...);
|
|
return *this;
|
|
}
|
|
|
|
template <typename... Args, typename... Extra>
|
|
class_ &def(const detail::initimpl::alias_constructor<Args...> &init, const Extra&... extra) {
|
|
init.execute(*this, extra...);
|
|
return *this;
|
|
}
|
|
|
|
template <typename... Args, typename... Extra>
|
|
class_ &def(detail::initimpl::factory<Args...> &&init, const Extra&... extra) {
|
|
std::move(init).execute(*this, extra...);
|
|
return *this;
|
|
}
|
|
|
|
template <typename... Args, typename... Extra>
|
|
class_ &def(detail::initimpl::pickle_factory<Args...> &&pf, const Extra &...extra) {
|
|
std::move(pf).execute(*this, extra...);
|
|
return *this;
|
|
}
|
|
|
|
template <typename Func>
|
|
class_& def_buffer(Func &&func) {
|
|
struct capture { Func func; };
|
|
auto *ptr = new capture { std::forward<Func>(func) };
|
|
install_buffer_funcs([](PyObject *obj, void *ptr) -> buffer_info* {
|
|
detail::make_caster<type> caster;
|
|
if (!caster.load(obj, false))
|
|
return nullptr;
|
|
return new buffer_info(((capture *) ptr)->func(caster));
|
|
}, ptr);
|
|
weakref(m_ptr, cpp_function([ptr](handle wr) {
|
|
delete ptr;
|
|
wr.dec_ref();
|
|
})).release();
|
|
return *this;
|
|
}
|
|
|
|
template <typename Return, typename Class, typename... Args>
|
|
class_ &def_buffer(Return (Class::*func)(Args...)) {
|
|
return def_buffer([func] (type &obj) { return (obj.*func)(); });
|
|
}
|
|
|
|
template <typename Return, typename Class, typename... Args>
|
|
class_ &def_buffer(Return (Class::*func)(Args...) const) {
|
|
return def_buffer([func] (const type &obj) { return (obj.*func)(); });
|
|
}
|
|
|
|
template <typename C, typename D, typename... Extra>
|
|
class_ &def_readwrite(const char *name, D C::*pm, const Extra&... extra) {
|
|
static_assert(std::is_same<C, type>::value || std::is_base_of<C, type>::value, "def_readwrite() requires a class member (or base class member)");
|
|
cpp_function fget([pm](const type &c) -> const D &{ return c.*pm; }, is_method(*this)),
|
|
fset([pm](type &c, const D &value) { c.*pm = value; }, is_method(*this));
|
|
def_property(name, fget, fset, return_value_policy::reference_internal, extra...);
|
|
return *this;
|
|
}
|
|
|
|
template <typename C, typename D, typename... Extra>
|
|
class_ &def_readonly(const char *name, const D C::*pm, const Extra& ...extra) {
|
|
static_assert(std::is_same<C, type>::value || std::is_base_of<C, type>::value, "def_readonly() requires a class member (or base class member)");
|
|
cpp_function fget([pm](const type &c) -> const D &{ return c.*pm; }, is_method(*this));
|
|
def_property_readonly(name, fget, return_value_policy::reference_internal, extra...);
|
|
return *this;
|
|
}
|
|
|
|
template <typename D, typename... Extra>
|
|
class_ &def_readwrite_static(const char *name, D *pm, const Extra& ...extra) {
|
|
cpp_function fget([pm](object) -> const D &{ return *pm; }, scope(*this)),
|
|
fset([pm](object, const D &value) { *pm = value; }, scope(*this));
|
|
def_property_static(name, fget, fset, return_value_policy::reference, extra...);
|
|
return *this;
|
|
}
|
|
|
|
template <typename D, typename... Extra>
|
|
class_ &def_readonly_static(const char *name, const D *pm, const Extra& ...extra) {
|
|
cpp_function fget([pm](object) -> const D &{ return *pm; }, scope(*this));
|
|
def_property_readonly_static(name, fget, return_value_policy::reference, extra...);
|
|
return *this;
|
|
}
|
|
|
|
/// Uses return_value_policy::reference_internal by default
|
|
template <typename Getter, typename... Extra>
|
|
class_ &def_property_readonly(const char *name, const Getter &fget, const Extra& ...extra) {
|
|
return def_property_readonly(name, cpp_function(method_adaptor<type>(fget)),
|
|
return_value_policy::reference_internal, extra...);
|
|
}
|
|
|
|
/// Uses cpp_function's return_value_policy by default
|
|
template <typename... Extra>
|
|
class_ &def_property_readonly(const char *name, const cpp_function &fget, const Extra& ...extra) {
|
|
return def_property(name, fget, nullptr, extra...);
|
|
}
|
|
|
|
/// Uses return_value_policy::reference by default
|
|
template <typename Getter, typename... Extra>
|
|
class_ &def_property_readonly_static(const char *name, const Getter &fget, const Extra& ...extra) {
|
|
return def_property_readonly_static(name, cpp_function(fget), return_value_policy::reference, extra...);
|
|
}
|
|
|
|
/// Uses cpp_function's return_value_policy by default
|
|
template <typename... Extra>
|
|
class_ &def_property_readonly_static(const char *name, const cpp_function &fget, const Extra& ...extra) {
|
|
return def_property_static(name, fget, nullptr, extra...);
|
|
}
|
|
|
|
/// Uses return_value_policy::reference_internal by default
|
|
template <typename Getter, typename Setter, typename... Extra>
|
|
class_ &def_property(const char *name, const Getter &fget, const Setter &fset, const Extra& ...extra) {
|
|
return def_property(name, fget, cpp_function(method_adaptor<type>(fset)), extra...);
|
|
}
|
|
template <typename Getter, typename... Extra>
|
|
class_ &def_property(const char *name, const Getter &fget, const cpp_function &fset, const Extra& ...extra) {
|
|
return def_property(name, cpp_function(method_adaptor<type>(fget)), fset,
|
|
return_value_policy::reference_internal, extra...);
|
|
}
|
|
|
|
/// Uses cpp_function's return_value_policy by default
|
|
template <typename... Extra>
|
|
class_ &def_property(const char *name, const cpp_function &fget, const cpp_function &fset, const Extra& ...extra) {
|
|
return def_property_static(name, fget, fset, is_method(*this), extra...);
|
|
}
|
|
|
|
/// Uses return_value_policy::reference by default
|
|
template <typename Getter, typename... Extra>
|
|
class_ &def_property_static(const char *name, const Getter &fget, const cpp_function &fset, const Extra& ...extra) {
|
|
return def_property_static(name, cpp_function(fget), fset, return_value_policy::reference, extra...);
|
|
}
|
|
|
|
/// Uses cpp_function's return_value_policy by default
|
|
template <typename... Extra>
|
|
class_ &def_property_static(const char *name, const cpp_function &fget, const cpp_function &fset, const Extra& ...extra) {
|
|
static_assert( 0 == detail::constexpr_sum(std::is_base_of<arg, Extra>::value...),
|
|
"Argument annotations are not allowed for properties");
|
|
auto rec_fget = get_function_record(fget), rec_fset = get_function_record(fset);
|
|
auto *rec_active = rec_fget;
|
|
if (rec_fget) {
|
|
char *doc_prev = rec_fget->doc; /* 'extra' field may include a property-specific documentation string */
|
|
detail::process_attributes<Extra...>::init(extra..., rec_fget);
|
|
if (rec_fget->doc && rec_fget->doc != doc_prev) {
|
|
free(doc_prev);
|
|
rec_fget->doc = strdup(rec_fget->doc);
|
|
}
|
|
}
|
|
if (rec_fset) {
|
|
char *doc_prev = rec_fset->doc;
|
|
detail::process_attributes<Extra...>::init(extra..., rec_fset);
|
|
if (rec_fset->doc && rec_fset->doc != doc_prev) {
|
|
free(doc_prev);
|
|
rec_fset->doc = strdup(rec_fset->doc);
|
|
}
|
|
if (! rec_active) rec_active = rec_fset;
|
|
}
|
|
def_property_static_impl(name, fget, fset, rec_active);
|
|
return *this;
|
|
}
|
|
|
|
private:
|
|
// clang-format on
|
|
template <typename T = type,
|
|
detail::enable_if_t<!detail::type_uses_smart_holder_type_caster<T>::value, int> = 0>
|
|
void generic_type_initialize(const detail::type_record &record) {
|
|
generic_type::initialize(record, &detail::type_caster_generic::local_load);
|
|
}
|
|
|
|
template <typename T = type,
|
|
detail::enable_if_t<detail::type_uses_smart_holder_type_caster<T>::value, int> = 0>
|
|
void generic_type_initialize(const detail::type_record &record) {
|
|
generic_type::initialize(record, detail::type_caster<T>::get_local_load_function_ptr());
|
|
}
|
|
// clang-format off
|
|
|
|
/// Initialize holder object, variant 1: object derives from enable_shared_from_this
|
|
template <typename T>
|
|
static void init_holder(detail::instance *inst, detail::value_and_holder &v_h,
|
|
const holder_type * /* unused */, const std::enable_shared_from_this<T> * /* dummy */) {
|
|
|
|
auto sh = std::dynamic_pointer_cast<typename holder_type::element_type>(
|
|
detail::try_get_shared_from_this(v_h.value_ptr<type>()));
|
|
if (sh) {
|
|
new (std::addressof(v_h.holder<holder_type>())) holder_type(std::move(sh));
|
|
v_h.set_holder_constructed();
|
|
}
|
|
|
|
if (!v_h.holder_constructed() && inst->owned) {
|
|
new (std::addressof(v_h.holder<holder_type>())) holder_type(v_h.value_ptr<type>());
|
|
v_h.set_holder_constructed();
|
|
}
|
|
}
|
|
|
|
static void init_holder_from_existing(const detail::value_and_holder &v_h,
|
|
const holder_type *holder_ptr, std::true_type /*is_copy_constructible*/) {
|
|
new (std::addressof(v_h.holder<holder_type>())) holder_type(*reinterpret_cast<const holder_type *>(holder_ptr));
|
|
}
|
|
|
|
static void init_holder_from_existing(const detail::value_and_holder &v_h,
|
|
const holder_type *holder_ptr, std::false_type /*is_copy_constructible*/) {
|
|
new (std::addressof(v_h.holder<holder_type>())) holder_type(std::move(*const_cast<holder_type *>(holder_ptr)));
|
|
}
|
|
|
|
/// Initialize holder object, variant 2: try to construct from existing holder object, if possible
|
|
static void init_holder(detail::instance *inst, detail::value_and_holder &v_h,
|
|
const holder_type *holder_ptr, const void * /* dummy -- not enable_shared_from_this<T>) */) {
|
|
if (holder_ptr) {
|
|
init_holder_from_existing(v_h, holder_ptr, std::is_copy_constructible<holder_type>());
|
|
v_h.set_holder_constructed();
|
|
} else if (inst->owned || detail::always_construct_holder<holder_type>::value) {
|
|
new (std::addressof(v_h.holder<holder_type>())) holder_type(v_h.value_ptr<type>());
|
|
v_h.set_holder_constructed();
|
|
}
|
|
}
|
|
|
|
/// Performs instance initialization including constructing a holder and registering the known
|
|
/// instance. Should be called as soon as the `type` value_ptr is set for an instance. Takes an
|
|
/// optional pointer to an existing holder to use; if not specified and the instance is
|
|
/// `.owned`, a new holder will be constructed to manage the value pointer.
|
|
template <
|
|
typename T = type,
|
|
detail::enable_if_t<!detail::type_uses_smart_holder_type_caster<T>::value, int> = 0>
|
|
static void init_instance(detail::instance *inst, const void *holder_ptr) {
|
|
auto v_h = inst->get_value_and_holder(detail::get_type_info(typeid(type)));
|
|
if (!v_h.instance_registered()) {
|
|
register_instance(inst, v_h.value_ptr(), v_h.type);
|
|
v_h.set_instance_registered();
|
|
}
|
|
init_holder(inst, v_h, (const holder_type *) holder_ptr, v_h.value_ptr<type>());
|
|
}
|
|
|
|
// clang-format on
|
|
template <typename T = type,
|
|
detail::enable_if_t<detail::type_uses_smart_holder_type_caster<T>::value, int> = 0>
|
|
static void init_instance(detail::instance *inst, const void *holder_ptr) {
|
|
detail::type_caster<T>::template init_instance_for_type<type>(inst, holder_ptr);
|
|
}
|
|
// clang-format off
|
|
|
|
/// 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>().~holder_type();
|
|
v_h.set_holder_constructed(false);
|
|
}
|
|
else {
|
|
detail::call_operator_delete(v_h.value_ptr<type>(),
|
|
v_h.type->type_size,
|
|
v_h.type->type_align
|
|
);
|
|
}
|
|
v_h.value_ptr() = nullptr;
|
|
}
|
|
|
|
static detail::function_record *get_function_record(handle h) {
|
|
h = detail::get_function(h);
|
|
return h ? (detail::function_record *) reinterpret_borrow<capsule>(PyCFunction_GET_SELF(h.ptr()))
|
|
: nullptr;
|
|
}
|
|
};
|
|
|
|
/// Binds an existing constructor taking arguments Args...
|
|
template <typename... Args> detail::initimpl::constructor<Args...> init() { return {}; }
|
|
/// Like `init<Args...>()`, but the instance is always constructed through the alias class (even
|
|
/// when not inheriting on the Python side).
|
|
template <typename... Args> detail::initimpl::alias_constructor<Args...> init_alias() { return {}; }
|
|
|
|
/// Binds a factory function as a constructor
|
|
template <typename Func, typename Ret = detail::initimpl::factory<Func>>
|
|
Ret init(Func &&f) { return {std::forward<Func>(f)}; }
|
|
|
|
/// Dual-argument factory function: the first function is called when no alias is needed, the second
|
|
/// when an alias is needed (i.e. due to python-side inheritance). Arguments must be identical.
|
|
template <typename CFunc, typename AFunc, typename Ret = detail::initimpl::factory<CFunc, AFunc>>
|
|
Ret init(CFunc &&c, AFunc &&a) {
|
|
return {std::forward<CFunc>(c), std::forward<AFunc>(a)};
|
|
}
|
|
|
|
/// Binds pickling functions `__getstate__` and `__setstate__` and ensures that the type
|
|
/// returned by `__getstate__` is the same as the argument accepted by `__setstate__`.
|
|
template <typename GetState, typename SetState>
|
|
detail::initimpl::pickle_factory<GetState, SetState> pickle(GetState &&g, SetState &&s) {
|
|
return {std::forward<GetState>(g), std::forward<SetState>(s)};
|
|
}
|
|
|
|
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) { }
|
|
|
|
PYBIND11_NOINLINE void init(bool is_arithmetic, bool is_convertible) {
|
|
m_base.attr("__entries") = dict();
|
|
auto property = handle((PyObject *) &PyProperty_Type);
|
|
auto static_property = handle((PyObject *) get_internals().static_property_type);
|
|
|
|
m_base.attr("__repr__") = cpp_function(
|
|
[](object arg) -> str {
|
|
handle type = type::handle_of(arg);
|
|
object type_name = type.attr("__name__");
|
|
return pybind11::str("<{}.{}: {}>").format(type_name, enum_name(arg), int_(arg));
|
|
}, name("__repr__"), is_method(m_base)
|
|
);
|
|
|
|
m_base.attr("name") = property(cpp_function(&enum_name, name("name"), is_method(m_base)));
|
|
|
|
m_base.attr("__str__") = cpp_function(
|
|
[](handle arg) -> str {
|
|
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 {
|
|
std::string docstring;
|
|
dict entries = arg.attr("__entries");
|
|
if (((PyTypeObject *) arg.ptr())->tp_doc)
|
|
docstring += std::string(((PyTypeObject *) arg.ptr())->tp_doc) + "\n\n";
|
|
docstring += "Members:";
|
|
for (auto kv : entries) {
|
|
auto key = std::string(pybind11::str(kv.first));
|
|
auto comment = kv.second[int_(1)];
|
|
docstring += "\n\n " + key;
|
|
if (!comment.is_none())
|
|
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 (auto kv : entries)
|
|
m[kv.first] = kv.second[int_(0)];
|
|
return m;
|
|
}, name("__members__")), none(), none(), ""
|
|
);
|
|
|
|
#define PYBIND11_ENUM_OP_STRICT(op, expr, strict_behavior) \
|
|
m_base.attr(op) = cpp_function( \
|
|
[](object a, object b) { \
|
|
if (!type::handle_of(a).is(type::handle_of(b))) \
|
|
strict_behavior; \
|
|
return expr; \
|
|
}, \
|
|
name(op), is_method(m_base), arg("other"))
|
|
|
|
#define PYBIND11_ENUM_OP_CONV(op, expr) \
|
|
m_base.attr(op) = cpp_function( \
|
|
[](object a_, object b_) { \
|
|
int_ a(a_), b(b_); \
|
|
return expr; \
|
|
}, \
|
|
name(op), is_method(m_base), arg("other"))
|
|
|
|
#define PYBIND11_ENUM_OP_CONV_LHS(op, expr) \
|
|
m_base.attr(op) = cpp_function( \
|
|
[](object a_, object b) { \
|
|
int_ a(a_); \
|
|
return expr; \
|
|
}, \
|
|
name(op), is_method(m_base), arg("other"))
|
|
|
|
if (is_convertible) {
|
|
PYBIND11_ENUM_OP_CONV_LHS("__eq__", !b.is_none() && a.equal(b));
|
|
PYBIND11_ENUM_OP_CONV_LHS("__ne__", b.is_none() || !a.equal(b));
|
|
|
|
if (is_arithmetic) {
|
|
PYBIND11_ENUM_OP_CONV("__lt__", a < b);
|
|
PYBIND11_ENUM_OP_CONV("__gt__", a > b);
|
|
PYBIND11_ENUM_OP_CONV("__le__", a <= b);
|
|
PYBIND11_ENUM_OP_CONV("__ge__", a >= b);
|
|
PYBIND11_ENUM_OP_CONV("__and__", a & b);
|
|
PYBIND11_ENUM_OP_CONV("__rand__", a & b);
|
|
PYBIND11_ENUM_OP_CONV("__or__", a | b);
|
|
PYBIND11_ENUM_OP_CONV("__ror__", a | b);
|
|
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)); }, name("__invert__"), is_method(m_base));
|
|
}
|
|
} else {
|
|
PYBIND11_ENUM_OP_STRICT("__eq__", int_(a).equal(int_(b)), return false);
|
|
PYBIND11_ENUM_OP_STRICT("__ne__", !int_(a).equal(int_(b)), return true);
|
|
|
|
if (is_arithmetic) {
|
|
#define PYBIND11_THROW throw type_error("Expected an enumeration of matching type!");
|
|
PYBIND11_ENUM_OP_STRICT("__lt__", int_(a) < int_(b), PYBIND11_THROW);
|
|
PYBIND11_ENUM_OP_STRICT("__gt__", int_(a) > int_(b), PYBIND11_THROW);
|
|
PYBIND11_ENUM_OP_STRICT("__le__", int_(a) <= int_(b), PYBIND11_THROW);
|
|
PYBIND11_ENUM_OP_STRICT("__ge__", int_(a) >= int_(b), PYBIND11_THROW);
|
|
#undef PYBIND11_THROW
|
|
}
|
|
}
|
|
|
|
#undef PYBIND11_ENUM_OP_CONV_LHS
|
|
#undef PYBIND11_ENUM_OP_CONV
|
|
#undef PYBIND11_ENUM_OP_STRICT
|
|
|
|
m_base.attr("__getstate__") = cpp_function(
|
|
[](object arg) { return int_(arg); }, name("__getstate__"), is_method(m_base));
|
|
|
|
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) {
|
|
dict entries = m_base.attr("__entries");
|
|
str name(name_);
|
|
if (entries.contains(name)) {
|
|
std::string type_name = (std::string) str(m_base.attr("__name__"));
|
|
throw value_error(type_name + ": element \"" + std::string(name_) + "\" already exists!");
|
|
}
|
|
|
|
entries[name] = std::make_pair(value, doc);
|
|
m_base.attr(name) = value;
|
|
}
|
|
|
|
PYBIND11_NOINLINE void export_values() {
|
|
dict entries = m_base.attr("__entries");
|
|
for (auto kv : entries)
|
|
m_parent.attr(kv.first) = kv.second[int_(0)];
|
|
}
|
|
|
|
handle m_base;
|
|
handle m_parent;
|
|
};
|
|
|
|
PYBIND11_NAMESPACE_END(detail)
|
|
|
|
/// Binds C++ enumerations and enumeration classes to Python
|
|
template <typename Type> class enum_ : public class_<Type> {
|
|
public:
|
|
using Base = class_<Type>;
|
|
using Base::def;
|
|
using Base::attr;
|
|
using Base::def_property_readonly;
|
|
using Base::def_property_readonly_static;
|
|
using Scalar = typename std::underlying_type<Type>::type;
|
|
|
|
template <typename... Extra>
|
|
enum_(const handle &scope, const char *name, const Extra&... extra)
|
|
: class_<Type>(scope, name, extra...), m_base(*this, scope) {
|
|
constexpr bool is_arithmetic = detail::any_of<std::is_same<arithmetic, Extra>...>::value;
|
|
constexpr bool is_convertible = std::is_convertible<Type, Scalar>::value;
|
|
m_base.init(is_arithmetic, is_convertible);
|
|
|
|
def(init([](Scalar i) { return static_cast<Type>(i); }), arg("value"));
|
|
def_property_readonly("value", [](Type value) { return (Scalar) value; });
|
|
def("__int__", [](Type value) { return (Scalar) value; });
|
|
#if PY_MAJOR_VERSION < 3
|
|
def("__long__", [](Type value) { return (Scalar) value; });
|
|
#endif
|
|
#if PY_MAJOR_VERSION > 3 || (PY_MAJOR_VERSION == 3 && PY_MINOR_VERSION >= 8)
|
|
def("__index__", [](Type value) { return (Scalar) value; });
|
|
#endif
|
|
|
|
attr("__setstate__") = cpp_function(
|
|
[](detail::value_and_holder &v_h, Scalar arg) {
|
|
detail::initimpl::setstate<Base>(v_h, static_cast<Type>(arg),
|
|
Py_TYPE(v_h.inst) != v_h.type->type); },
|
|
detail::is_new_style_constructor(),
|
|
pybind11::name("__setstate__"), is_method(*this), arg("state"));
|
|
}
|
|
|
|
/// Export enumeration entries into the parent scope
|
|
enum_& export_values() {
|
|
m_base.export_values();
|
|
return *this;
|
|
}
|
|
|
|
/// Add an enumeration entry
|
|
enum_& value(char const* name, Type value, const char *doc = nullptr) {
|
|
m_base.value(name, pybind11::cast(value, return_value_policy::copy), doc);
|
|
return *this;
|
|
}
|
|
|
|
private:
|
|
detail::enum_base m_base;
|
|
};
|
|
|
|
PYBIND11_NAMESPACE_BEGIN(detail)
|
|
|
|
|
|
inline void keep_alive_impl(handle nurse, handle patient) {
|
|
if (!nurse || !patient)
|
|
pybind11_fail("Could not activate keep_alive!");
|
|
|
|
if (patient.is_none() || nurse.is_none())
|
|
return; /* Nothing to keep alive or nothing to be kept alive by */
|
|
|
|
auto tinfo = all_type_info(Py_TYPE(nurse.ptr()));
|
|
if (!tinfo.empty()) {
|
|
/* It's a pybind-registered type, so we can store the patient in the
|
|
* internal list. */
|
|
add_patient(nurse.ptr(), patient.ptr());
|
|
}
|
|
else {
|
|
/* Fall back to clever approach based on weak references taken from
|
|
* Boost.Python. This is not used for pybind-registered types because
|
|
* the objects can be destroyed out-of-order in a GC pass. */
|
|
cpp_function disable_lifesupport(
|
|
[patient](handle weakref) { patient.dec_ref(); weakref.dec_ref(); });
|
|
|
|
weakref wr(nurse, disable_lifesupport);
|
|
|
|
patient.inc_ref(); /* reference patient and leak the weak reference */
|
|
(void) wr.release();
|
|
}
|
|
}
|
|
|
|
PYBIND11_NOINLINE inline void keep_alive_impl(size_t Nurse, size_t Patient, function_call &call, handle ret) {
|
|
auto get_arg = [&](size_t n) {
|
|
if (n == 0)
|
|
return ret;
|
|
else if (n == 1 && call.init_self)
|
|
return call.init_self;
|
|
else if (n <= call.args.size())
|
|
return call.args[n - 1];
|
|
return handle();
|
|
};
|
|
|
|
keep_alive_impl(get_arg(Nurse), get_arg(Patient));
|
|
}
|
|
|
|
inline std::pair<decltype(internals::registered_types_py)::iterator, bool> all_type_info_get_cache(PyTypeObject *type) {
|
|
auto res = get_internals().registered_types_py
|
|
#ifdef __cpp_lib_unordered_map_try_emplace
|
|
.try_emplace(type);
|
|
#else
|
|
.emplace(type, std::vector<detail::type_info *>());
|
|
#endif
|
|
if (res.second) {
|
|
// New cache entry created; set up a weak reference to automatically remove it if the type
|
|
// gets destroyed:
|
|
weakref((PyObject *) type, cpp_function([type](handle wr) {
|
|
get_internals().registered_types_py.erase(type);
|
|
wr.dec_ref();
|
|
})).release();
|
|
}
|
|
|
|
return res;
|
|
}
|
|
|
|
template <typename Iterator, typename Sentinel, bool KeyIterator, return_value_policy Policy>
|
|
struct iterator_state {
|
|
Iterator it;
|
|
Sentinel end;
|
|
bool first_or_done;
|
|
};
|
|
|
|
PYBIND11_NAMESPACE_END(detail)
|
|
|
|
/// Makes a python iterator from a first and past-the-end C++ InputIterator.
|
|
template <return_value_policy Policy = return_value_policy::reference_internal,
|
|
typename Iterator,
|
|
typename Sentinel,
|
|
#ifndef DOXYGEN_SHOULD_SKIP_THIS // Issue in breathe 4.26.1
|
|
typename ValueType = decltype(*std::declval<Iterator>()),
|
|
#endif
|
|
typename... Extra>
|
|
iterator make_iterator(Iterator first, Sentinel last, Extra &&... extra) {
|
|
using state = detail::iterator_state<Iterator, Sentinel, false, Policy>;
|
|
|
|
if (!detail::get_type_info(typeid(state), false)) {
|
|
class_<state>(handle(), "iterator", pybind11::module_local())
|
|
.def("__iter__", [](state &s) -> state& { return s; })
|
|
.def("__next__", [](state &s) -> ValueType {
|
|
if (!s.first_or_done)
|
|
++s.it;
|
|
else
|
|
s.first_or_done = false;
|
|
if (s.it == s.end) {
|
|
s.first_or_done = true;
|
|
throw stop_iteration();
|
|
}
|
|
return *s.it;
|
|
}, std::forward<Extra>(extra)..., Policy);
|
|
}
|
|
|
|
return cast(state{first, last, true});
|
|
}
|
|
|
|
/// Makes an python iterator over the keys (`.first`) of a iterator over pairs from a
|
|
/// first and past-the-end InputIterator.
|
|
template <return_value_policy Policy = return_value_policy::reference_internal,
|
|
typename Iterator,
|
|
typename Sentinel,
|
|
#ifndef DOXYGEN_SHOULD_SKIP_THIS // Issue in breathe 4.26.1
|
|
typename KeyType = decltype((*std::declval<Iterator>()).first),
|
|
#endif
|
|
typename... Extra>
|
|
iterator make_key_iterator(Iterator first, Sentinel last, Extra &&... extra) {
|
|
using state = detail::iterator_state<Iterator, Sentinel, true, Policy>;
|
|
|
|
if (!detail::get_type_info(typeid(state), false)) {
|
|
class_<state>(handle(), "iterator", pybind11::module_local())
|
|
.def("__iter__", [](state &s) -> state& { return s; })
|
|
.def("__next__", [](state &s) -> KeyType {
|
|
if (!s.first_or_done)
|
|
++s.it;
|
|
else
|
|
s.first_or_done = false;
|
|
if (s.it == s.end) {
|
|
s.first_or_done = true;
|
|
throw stop_iteration();
|
|
}
|
|
return (*s.it).first;
|
|
}, std::forward<Extra>(extra)..., Policy);
|
|
}
|
|
|
|
return cast(state{first, last, true});
|
|
}
|
|
|
|
/// Makes an iterator over values of an stl container or other container supporting
|
|
/// `std::begin()`/`std::end()`
|
|
template <return_value_policy Policy = return_value_policy::reference_internal,
|
|
typename Type, typename... Extra> iterator make_iterator(Type &value, Extra&&... extra) {
|
|
return make_iterator<Policy>(std::begin(value), std::end(value), extra...);
|
|
}
|
|
|
|
/// Makes an iterator over the keys (`.first`) of a stl map-like container supporting
|
|
/// `std::begin()`/`std::end()`
|
|
template <return_value_policy Policy = return_value_policy::reference_internal,
|
|
typename Type, typename... Extra> iterator make_key_iterator(Type &value, Extra&&... extra) {
|
|
return make_key_iterator<Policy>(std::begin(value), std::end(value), extra...);
|
|
}
|
|
|
|
template <typename InputType, typename OutputType> void implicitly_convertible() {
|
|
struct set_flag {
|
|
bool &flag;
|
|
set_flag(bool &flag_) : flag(flag_) { flag_ = true; }
|
|
~set_flag() { flag = false; }
|
|
};
|
|
auto implicit_caster = [](PyObject *obj, PyTypeObject *type) -> PyObject * {
|
|
static bool currently_used = false;
|
|
if (currently_used) // implicit conversions are non-reentrant
|
|
return nullptr;
|
|
set_flag flag_helper(currently_used);
|
|
if (!detail::make_caster<InputType>().load(obj, false))
|
|
return nullptr;
|
|
tuple args(1);
|
|
args[0] = obj;
|
|
PyObject *result = PyObject_Call((PyObject *) type, args.ptr(), nullptr);
|
|
if (result == nullptr)
|
|
PyErr_Clear();
|
|
return result;
|
|
};
|
|
|
|
if (auto tinfo = detail::get_type_info(typeid(OutputType)))
|
|
tinfo->implicit_conversions.push_back(implicit_caster);
|
|
else
|
|
pybind11_fail("implicitly_convertible: Unable to find type " + type_id<OutputType>());
|
|
}
|
|
|
|
template <typename ExceptionTranslator>
|
|
void register_exception_translator(ExceptionTranslator&& translator) {
|
|
detail::get_internals().registered_exception_translators.push_front(
|
|
std::forward<ExceptionTranslator>(translator));
|
|
}
|
|
|
|
/**
|
|
* Wrapper to generate a new Python exception type.
|
|
*
|
|
* This should only be used with PyErr_SetString for now.
|
|
* It is not (yet) possible to use as a py::base.
|
|
* Template type argument is reserved for future use.
|
|
*/
|
|
template <typename type>
|
|
class exception : public object {
|
|
public:
|
|
exception() = default;
|
|
exception(handle scope, const char *name, handle base = PyExc_Exception) {
|
|
std::string full_name = scope.attr("__name__").cast<std::string>() +
|
|
std::string(".") + name;
|
|
m_ptr = PyErr_NewException(const_cast<char *>(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;
|
|
}
|
|
|
|
// Sets the current python exception to this exception object with the given message
|
|
void operator()(const char *message) {
|
|
PyErr_SetString(m_ptr, message);
|
|
}
|
|
};
|
|
|
|
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 <typename CppException>
|
|
exception<CppException> &get_exception_object() { static exception<CppException> ex; return ex; }
|
|
PYBIND11_NAMESPACE_END(detail)
|
|
|
|
/**
|
|
* Registers a Python exception in `m` of the given `name` and installs an exception translator to
|
|
* translate the C++ exception to the created Python exception using the exceptions what() method.
|
|
* This is intended for simple exception translations; for more complex translation, register the
|
|
* exception object and translator directly.
|
|
*/
|
|
template <typename CppException>
|
|
exception<CppException> ®ister_exception(handle scope,
|
|
const char *name,
|
|
handle base = PyExc_Exception) {
|
|
auto &ex = detail::get_exception_object<CppException>();
|
|
if (!ex) ex = exception<CppException>(scope, name, base);
|
|
|
|
register_exception_translator([](std::exception_ptr p) {
|
|
if (!p) return;
|
|
try {
|
|
std::rethrow_exception(p);
|
|
} catch (const CppException &e) {
|
|
detail::get_exception_object<CppException>()(e.what());
|
|
}
|
|
});
|
|
return ex;
|
|
}
|
|
|
|
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) {
|
|
strings[i] = str(args[i]);
|
|
}
|
|
auto sep = kwargs.contains("sep") ? kwargs["sep"] : cast(" ");
|
|
auto line = sep.attr("join")(strings);
|
|
|
|
object file;
|
|
if (kwargs.contains("file")) {
|
|
file = kwargs["file"].cast<object>();
|
|
} else {
|
|
try {
|
|
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,
|
|
importing 'sys' can fail. Give up rather than crashing the
|
|
interpreter in this case. */
|
|
return;
|
|
}
|
|
}
|
|
|
|
auto write = file.attr("write");
|
|
write(line);
|
|
write(kwargs.contains("end") ? kwargs["end"] : cast("\n"));
|
|
|
|
if (kwargs.contains("flush") && kwargs["flush"].cast<bool>())
|
|
file.attr("flush")();
|
|
}
|
|
PYBIND11_NAMESPACE_END(detail)
|
|
|
|
template <return_value_policy policy = return_value_policy::automatic_reference, typename... Args>
|
|
void print(Args &&...args) {
|
|
auto c = detail::collect_arguments<policy>(std::forward<Args>(args)...);
|
|
detail::print(c.args(), c.kwargs());
|
|
}
|
|
|
|
error_already_set::~error_already_set() {
|
|
if (m_type) {
|
|
gil_scoped_acquire gil;
|
|
error_scope scope;
|
|
m_type.release().dec_ref();
|
|
m_value.release().dec_ref();
|
|
m_trace.release().dec_ref();
|
|
}
|
|
}
|
|
|
|
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 = type::handle_of(self);
|
|
auto key = std::make_pair(type.ptr(), name);
|
|
|
|
/* Cache functions that aren't overridden in Python to avoid
|
|
many costly Python dictionary lookups below */
|
|
auto &cache = get_internals().inactive_override_cache;
|
|
if (cache.find(key) != cache.end())
|
|
return function();
|
|
|
|
function override = getattr(self, name, function());
|
|
if (override.is_cpp_function()) {
|
|
cache.insert(key);
|
|
return function();
|
|
}
|
|
|
|
/* Don't call dispatch code if invoked from overridden function.
|
|
Unfortunately this doesn't work on PyPy. */
|
|
#if !defined(PYPY_VERSION)
|
|
PyFrameObject *frame = PyThreadState_Get()->frame;
|
|
if (frame && (std::string) str(frame->f_code->co_name) == name &&
|
|
frame->f_code->co_argcount > 0) {
|
|
PyFrame_FastToLocals(frame);
|
|
PyObject *self_caller = PyDict_GetItem(
|
|
frame->f_locals, PyTuple_GET_ITEM(frame->f_code->co_varnames, 0));
|
|
if (self_caller == self.ptr())
|
|
return function();
|
|
}
|
|
#else
|
|
/* PyPy currently doesn't provide a detailed cpyext emulation of
|
|
frame objects, so we have to emulate this using Python. This
|
|
is going to be slow..*/
|
|
dict d; d["self"] = self; d["name"] = pybind11::str(name);
|
|
PyObject *result = PyRun_String(
|
|
"import inspect\n"
|
|
"frame = inspect.currentframe()\n"
|
|
"if frame is not None:\n"
|
|
" frame = frame.f_back\n"
|
|
" if frame is not None and str(frame.f_code.co_name) == name and "
|
|
"frame.f_code.co_argcount > 0:\n"
|
|
" self_caller = frame.f_locals[frame.f_code.co_varnames[0]]\n"
|
|
" if self_caller == self:\n"
|
|
" self = None\n",
|
|
Py_file_input, d.ptr(), d.ptr());
|
|
if (result == nullptr)
|
|
throw error_already_set();
|
|
if (d["self"].is_none())
|
|
return function();
|
|
Py_DECREF(result);
|
|
#endif
|
|
|
|
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 overridden 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 <class T> function get_override(const T *this_ptr, const char *name) {
|
|
auto tinfo = detail::get_type_info(typeid(T));
|
|
return tinfo ? detail::get_type_override(this_ptr, tinfo, name) : function();
|
|
}
|
|
|
|
#define PYBIND11_OVERRIDE_IMPL(ret_type, cname, name, ...) \
|
|
do { \
|
|
pybind11::gil_scoped_acquire gil; \
|
|
pybind11::function override = pybind11::get_override(static_cast<const cname *>(this), name); \
|
|
if (override) { \
|
|
auto o = override(__VA_ARGS__); \
|
|
if (pybind11::detail::cast_is_temporary_value_reference<ret_type>::value) { \
|
|
static pybind11::detail::override_caster_t<ret_type> caster; \
|
|
return pybind11::detail::cast_ref<ret_type>(std::move(o), caster); \
|
|
} \
|
|
else return pybind11::detail::cast_safe<ret_type>(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'
|
|
from the Python side, deals with the :ref:`gil` and necessary argument conversions to call this method and return
|
|
the appropriate type. See :ref:`overriding_virtuals` for more information. This macro should be used when the method
|
|
name in C is not the same as the method name in Python. For example with `__str__`.
|
|
|
|
.. code-block:: cpp
|
|
|
|
std::string toString() override {
|
|
PYBIND11_OVERRIDE_NAME(
|
|
std::string, // Return type (ret_type)
|
|
Animal, // Parent class (cname)
|
|
"__str__", // Name of method in Python (name)
|
|
toString, // Name of function in C++ (fn)
|
|
);
|
|
}
|
|
\endrst */
|
|
#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_OVERRIDE_NAME`, except that it
|
|
throws if no override can be found.
|
|
\endrst */
|
|
#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
|
|
from the Python side, deals with the :ref:`gil` and necessary argument conversions to call this method and return
|
|
the appropriate type. This macro should be used if the method name in C and in Python are identical.
|
|
See :ref:`overriding_virtuals` for more information.
|
|
|
|
.. code-block:: cpp
|
|
|
|
class PyAnimal : public Animal {
|
|
public:
|
|
// Inherit the constructors
|
|
using Animal::Animal;
|
|
|
|
// Trampoline (need one for each virtual function)
|
|
std::string go(int n_times) override {
|
|
PYBIND11_OVERRIDE_PURE(
|
|
std::string, // Return type (ret_type)
|
|
Animal, // Parent class (cname)
|
|
go, // Name of function in C++ (must match Python name) (fn)
|
|
n_times // Argument(s) (...)
|
|
);
|
|
}
|
|
};
|
|
\endrst */
|
|
#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_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 <class T>
|
|
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_OVERRIDE_PURE(PYBIND11_TYPE(ret_type), PYBIND11_TYPE(cname), fn, __VA_ARGS__);
|
|
|
|
PYBIND11_NAMESPACE_END(PYBIND11_NAMESPACE)
|
|
|
|
#if defined(_MSC_VER) && !defined(__INTEL_COMPILER)
|
|
# pragma warning(pop)
|
|
#elif defined(__GNUG__) && !defined(__clang__)
|
|
# pragma GCC diagnostic pop
|
|
#endif
|