File indexing completed on 2025-02-16 05:12:18
0001 /* 0002 pybind11/pybind11.h: Main header file of the C++11 python 0003 binding generator library 0004 0005 Copyright (c) 2016 Wenzel Jakob <wenzel.jakob@epfl.ch> 0006 0007 All rights reserved. Use of this source code is governed by a 0008 BSD-style license that can be found in the LICENSE file. 0009 */ 0010 0011 #pragma once 0012 0013 #include "attr.h" 0014 #include "gil.h" 0015 #include "options.h" 0016 #include "detail/class.h" 0017 #include "detail/init.h" 0018 0019 #include <cstdlib> 0020 #include <memory> 0021 #include <new> 0022 #include <vector> 0023 #include <string> 0024 #include <utility> 0025 0026 #include <cstring> 0027 0028 #if defined(__cpp_lib_launder) && !(defined(_MSC_VER) && (_MSC_VER < 1914)) 0029 # define PYBIND11_STD_LAUNDER std::launder 0030 # define PYBIND11_HAS_STD_LAUNDER 1 0031 #else 0032 # define PYBIND11_STD_LAUNDER 0033 # define PYBIND11_HAS_STD_LAUNDER 0 0034 #endif 0035 #if defined(__GNUG__) && !defined(__clang__) 0036 # include <cxxabi.h> 0037 #endif 0038 0039 /* https://stackoverflow.com/questions/46798456/handling-gccs-noexcept-type-warning 0040 This warning is about ABI compatibility, not code health. 0041 It is only actually needed in a couple places, but apparently GCC 7 "generates this warning if 0042 and only if the first template instantiation ... involves noexcept" [stackoverflow], therefore 0043 it could get triggered from seemingly random places, depending on user code. 0044 No other GCC version generates this warning. 0045 */ 0046 #if defined(__GNUC__) && __GNUC__ == 7 0047 # pragma GCC diagnostic push 0048 # pragma GCC diagnostic ignored "-Wnoexcept-type" 0049 #endif 0050 0051 PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE) 0052 0053 PYBIND11_NAMESPACE_BEGIN(detail) 0054 0055 // Apply all the extensions translators from a list 0056 // Return true if one of the translators completed without raising an exception 0057 // itself. Return of false indicates that if there are other translators 0058 // available, they should be tried. 0059 inline bool apply_exception_translators(std::forward_list<ExceptionTranslator>& translators) { 0060 auto last_exception = std::current_exception(); 0061 0062 for (auto &translator : translators) { 0063 try { 0064 translator(last_exception); 0065 return true; 0066 } catch (...) { 0067 last_exception = std::current_exception(); 0068 } 0069 } 0070 return false; 0071 } 0072 0073 #if defined(_MSC_VER) 0074 # define PYBIND11_COMPAT_STRDUP _strdup 0075 #else 0076 # define PYBIND11_COMPAT_STRDUP strdup 0077 #endif 0078 0079 PYBIND11_NAMESPACE_END(detail) 0080 0081 /// Wraps an arbitrary C++ function/method/lambda function/.. into a callable Python object 0082 class cpp_function : public function { 0083 public: 0084 cpp_function() = default; 0085 // NOLINTNEXTLINE(google-explicit-constructor) 0086 cpp_function(std::nullptr_t) { } 0087 0088 /// Construct a cpp_function from a vanilla function pointer 0089 template <typename Return, typename... Args, typename... Extra> 0090 // NOLINTNEXTLINE(google-explicit-constructor) 0091 cpp_function(Return (*f)(Args...), const Extra&... extra) { 0092 initialize(f, f, extra...); 0093 } 0094 0095 /// Construct a cpp_function from a lambda function (possibly with internal state) 0096 template <typename Func, typename... Extra, 0097 typename = detail::enable_if_t<detail::is_lambda<Func>::value>> 0098 // NOLINTNEXTLINE(google-explicit-constructor) 0099 cpp_function(Func &&f, const Extra&... extra) { 0100 initialize(std::forward<Func>(f), 0101 (detail::function_signature_t<Func> *) nullptr, extra...); 0102 } 0103 0104 /// Construct a cpp_function from a class method (non-const, no ref-qualifier) 0105 template <typename Return, typename Class, typename... Arg, typename... Extra> 0106 // NOLINTNEXTLINE(google-explicit-constructor) 0107 cpp_function(Return (Class::*f)(Arg...), const Extra&... extra) { 0108 initialize([f](Class *c, Arg... args) -> Return { return (c->*f)(std::forward<Arg>(args)...); }, 0109 (Return (*) (Class *, Arg...)) nullptr, extra...); 0110 } 0111 0112 /// Construct a cpp_function from a class method (non-const, lvalue ref-qualifier) 0113 /// A copy of the overload for non-const functions without explicit ref-qualifier 0114 /// but with an added `&`. 0115 template <typename Return, typename Class, typename... Arg, typename... Extra> 0116 // NOLINTNEXTLINE(google-explicit-constructor) 0117 cpp_function(Return (Class::*f)(Arg...)&, const Extra&... extra) { 0118 initialize([f](Class *c, Arg... args) -> Return { return (c->*f)(std::forward<Arg>(args)...); }, 0119 (Return (*) (Class *, Arg...)) nullptr, extra...); 0120 } 0121 0122 /// Construct a cpp_function from a class method (const, no ref-qualifier) 0123 template <typename Return, typename Class, typename... Arg, typename... Extra> 0124 // NOLINTNEXTLINE(google-explicit-constructor) 0125 cpp_function(Return (Class::*f)(Arg...) const, const Extra&... extra) { 0126 initialize([f](const Class *c, Arg... args) -> Return { return (c->*f)(std::forward<Arg>(args)...); }, 0127 (Return (*)(const Class *, Arg ...)) nullptr, extra...); 0128 } 0129 0130 /// Construct a cpp_function from a class method (const, lvalue ref-qualifier) 0131 /// A copy of the overload for const functions without explicit ref-qualifier 0132 /// but with an added `&`. 0133 template <typename Return, typename Class, typename... Arg, typename... Extra> 0134 // NOLINTNEXTLINE(google-explicit-constructor) 0135 cpp_function(Return (Class::*f)(Arg...) const&, const Extra&... extra) { 0136 initialize([f](const Class *c, Arg... args) -> Return { return (c->*f)(std::forward<Arg>(args)...); }, 0137 (Return (*)(const Class *, Arg ...)) nullptr, extra...); 0138 } 0139 0140 /// Return the function name 0141 object name() const { return attr("__name__"); } 0142 0143 protected: 0144 struct InitializingFunctionRecordDeleter { 0145 // `destruct(function_record, false)`: `initialize_generic` copies strings and 0146 // takes care of cleaning up in case of exceptions. So pass `false` to `free_strings`. 0147 void operator()(detail::function_record * rec) { destruct(rec, false); } 0148 }; 0149 using unique_function_record = std::unique_ptr<detail::function_record, InitializingFunctionRecordDeleter>; 0150 0151 /// Space optimization: don't inline this frequently instantiated fragment 0152 PYBIND11_NOINLINE unique_function_record make_function_record() { 0153 return unique_function_record(new detail::function_record()); 0154 } 0155 0156 /// Special internal constructor for functors, lambda functions, etc. 0157 template <typename Func, typename Return, typename... Args, typename... Extra> 0158 void initialize(Func &&f, Return (*)(Args...), const Extra&... extra) { 0159 using namespace detail; 0160 struct capture { remove_reference_t<Func> f; }; 0161 0162 /* Store the function including any extra state it might have (e.g. a lambda capture object) */ 0163 // The unique_ptr makes sure nothing is leaked in case of an exception. 0164 auto unique_rec = make_function_record(); 0165 auto rec = unique_rec.get(); 0166 0167 /* Store the capture object directly in the function record if there is enough space */ 0168 if (PYBIND11_SILENCE_MSVC_C4127(sizeof(capture) <= sizeof(rec->data))) { 0169 /* Without these pragmas, GCC warns that there might not be 0170 enough space to use the placement new operator. However, the 0171 'if' statement above ensures that this is the case. */ 0172 #if defined(__GNUG__) && __GNUC__ >= 6 && !defined(__clang__) && !defined(__INTEL_COMPILER) 0173 # pragma GCC diagnostic push 0174 # pragma GCC diagnostic ignored "-Wplacement-new" 0175 #endif 0176 new ((capture *) &rec->data) capture { std::forward<Func>(f) }; 0177 #if defined(__GNUG__) && __GNUC__ >= 6 && !defined(__clang__) && !defined(__INTEL_COMPILER) 0178 # pragma GCC diagnostic pop 0179 #endif 0180 #if defined(__GNUG__) && !PYBIND11_HAS_STD_LAUNDER && !defined(__INTEL_COMPILER) 0181 # pragma GCC diagnostic push 0182 # pragma GCC diagnostic ignored "-Wstrict-aliasing" 0183 #endif 0184 // UB without std::launder, but without breaking ABI and/or 0185 // a significant refactoring it's "impossible" to solve. 0186 if (!std::is_trivially_destructible<capture>::value) 0187 rec->free_data = [](function_record *r) { 0188 auto data = PYBIND11_STD_LAUNDER((capture *) &r->data); 0189 (void) data; 0190 data->~capture(); 0191 }; 0192 #if defined(__GNUG__) && !PYBIND11_HAS_STD_LAUNDER && !defined(__INTEL_COMPILER) 0193 # pragma GCC diagnostic pop 0194 #endif 0195 } else { 0196 rec->data[0] = new capture { std::forward<Func>(f) }; 0197 rec->free_data = [](function_record *r) { delete ((capture *) r->data[0]); }; 0198 } 0199 0200 /* Type casters for the function arguments and return value */ 0201 using cast_in = argument_loader<Args...>; 0202 using cast_out = make_caster< 0203 conditional_t<std::is_void<Return>::value, void_type, Return> 0204 >; 0205 0206 static_assert(expected_num_args<Extra...>(sizeof...(Args), cast_in::args_pos >= 0, cast_in::has_kwargs), 0207 "The number of argument annotations does not match the number of function arguments"); 0208 0209 /* Dispatch code which converts function arguments and performs the actual function call */ 0210 rec->impl = [](function_call &call) -> handle { 0211 cast_in args_converter; 0212 0213 /* Try to cast the function arguments into the C++ domain */ 0214 if (!args_converter.load_args(call)) 0215 return PYBIND11_TRY_NEXT_OVERLOAD; 0216 0217 /* Invoke call policy pre-call hook */ 0218 process_attributes<Extra...>::precall(call); 0219 0220 /* Get a pointer to the capture object */ 0221 auto data = (sizeof(capture) <= sizeof(call.func.data) 0222 ? &call.func.data : call.func.data[0]); 0223 auto *cap = const_cast<capture *>(reinterpret_cast<const capture *>(data)); 0224 0225 /* Override policy for rvalues -- usually to enforce rvp::move on an rvalue */ 0226 return_value_policy policy = return_value_policy_override<Return>::policy(call.func.policy); 0227 0228 /* Function scope guard -- defaults to the compile-to-nothing `void_type` */ 0229 using Guard = extract_guard_t<Extra...>; 0230 0231 /* Perform the function call */ 0232 handle result = cast_out::cast( 0233 std::move(args_converter).template call<Return, Guard>(cap->f), policy, call.parent); 0234 0235 /* Invoke call policy post-call hook */ 0236 process_attributes<Extra...>::postcall(call, result); 0237 0238 return result; 0239 }; 0240 0241 rec->nargs_pos = cast_in::args_pos >= 0 0242 ? static_cast<std::uint16_t>(cast_in::args_pos) 0243 : sizeof...(Args) - cast_in::has_kwargs; // Will get reduced more if we have a kw_only 0244 rec->has_args = cast_in::args_pos >= 0; 0245 rec->has_kwargs = cast_in::has_kwargs; 0246 0247 /* Process any user-provided function attributes */ 0248 process_attributes<Extra...>::init(extra..., rec); 0249 0250 { 0251 constexpr bool has_kw_only_args = any_of<std::is_same<kw_only, Extra>...>::value, 0252 has_pos_only_args = any_of<std::is_same<pos_only, Extra>...>::value, 0253 has_arg_annotations = any_of<is_keyword<Extra>...>::value; 0254 static_assert(has_arg_annotations || !has_kw_only_args, "py::kw_only requires the use of argument annotations"); 0255 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)"); 0256 0257 static_assert(constexpr_sum(is_kw_only<Extra>::value...) <= 1, "py::kw_only may be specified only once"); 0258 static_assert(constexpr_sum(is_pos_only<Extra>::value...) <= 1, "py::pos_only may be specified only once"); 0259 constexpr auto kw_only_pos = constexpr_first<is_kw_only, Extra...>(); 0260 constexpr auto pos_only_pos = constexpr_first<is_pos_only, Extra...>(); 0261 static_assert(!(has_kw_only_args && has_pos_only_args) || pos_only_pos < kw_only_pos, "py::pos_only must come before py::kw_only"); 0262 } 0263 0264 /* Generate a readable signature describing the function's arguments and return value types */ 0265 static constexpr auto signature = const_name("(") + cast_in::arg_names + const_name(") -> ") + cast_out::name; 0266 PYBIND11_DESCR_CONSTEXPR auto types = decltype(signature)::types(); 0267 0268 /* Register the function with Python from generic (non-templated) code */ 0269 // Pass on the ownership over the `unique_rec` to `initialize_generic`. `rec` stays valid. 0270 initialize_generic(std::move(unique_rec), signature.text, types.data(), sizeof...(Args)); 0271 0272 /* Stash some additional information used by an important optimization in 'functional.h' */ 0273 using FunctionType = Return (*)(Args...); 0274 constexpr bool is_function_ptr = 0275 std::is_convertible<Func, FunctionType>::value && 0276 sizeof(capture) == sizeof(void *); 0277 if (is_function_ptr) { 0278 rec->is_stateless = true; 0279 rec->data[1] = const_cast<void *>(reinterpret_cast<const void *>(&typeid(FunctionType))); 0280 } 0281 } 0282 0283 // Utility class that keeps track of all duplicated strings, and cleans them up in its destructor, 0284 // unless they are released. Basically a RAII-solution to deal with exceptions along the way. 0285 class strdup_guard { 0286 public: 0287 ~strdup_guard() { 0288 for (auto s : strings) 0289 std::free(s); 0290 } 0291 char *operator()(const char *s) { 0292 auto t = PYBIND11_COMPAT_STRDUP(s); 0293 strings.push_back(t); 0294 return t; 0295 } 0296 void release() { 0297 strings.clear(); 0298 } 0299 private: 0300 std::vector<char *> strings; 0301 }; 0302 0303 /// Register a function call with Python (generic non-templated code goes here) 0304 void initialize_generic(unique_function_record &&unique_rec, const char *text, 0305 const std::type_info *const *types, size_t args) { 0306 // Do NOT receive `unique_rec` by value. If this function fails to move out the unique_ptr, 0307 // we do not want this to destuct the pointer. `initialize` (the caller) still relies on the 0308 // pointee being alive after this call. Only move out if a `capsule` is going to keep it alive. 0309 auto rec = unique_rec.get(); 0310 0311 // Keep track of strdup'ed strings, and clean them up as long as the function's capsule 0312 // has not taken ownership yet (when `unique_rec.release()` is called). 0313 // Note: This cannot easily be fixed by a `unique_ptr` with custom deleter, because the strings 0314 // are only referenced before strdup'ing. So only *after* the following block could `destruct` 0315 // safely be called, but even then, `repr` could still throw in the middle of copying all strings. 0316 strdup_guard guarded_strdup; 0317 0318 /* Create copies of all referenced C-style strings */ 0319 rec->name = guarded_strdup(rec->name ? rec->name : ""); 0320 if (rec->doc) rec->doc = guarded_strdup(rec->doc); 0321 for (auto &a: rec->args) { 0322 if (a.name) 0323 a.name = guarded_strdup(a.name); 0324 if (a.descr) 0325 a.descr = guarded_strdup(a.descr); 0326 else if (a.value) 0327 a.descr = guarded_strdup(repr(a.value).cast<std::string>().c_str()); 0328 } 0329 0330 rec->is_constructor = (std::strcmp(rec->name, "__init__") == 0) 0331 || (std::strcmp(rec->name, "__setstate__") == 0); 0332 0333 #if !defined(NDEBUG) && !defined(PYBIND11_DISABLE_NEW_STYLE_INIT_WARNING) 0334 if (rec->is_constructor && !rec->is_new_style_constructor) { 0335 const auto class_name = detail::get_fully_qualified_tp_name((PyTypeObject *) rec->scope.ptr()); 0336 const auto func_name = std::string(rec->name); 0337 PyErr_WarnEx( 0338 PyExc_FutureWarning, 0339 ("pybind11-bound class '" + class_name + "' is using an old-style " 0340 "placement-new '" + func_name + "' which has been deprecated. See " 0341 "the upgrade guide in pybind11's docs. This message is only visible " 0342 "when compiled in debug mode.").c_str(), 0 0343 ); 0344 } 0345 #endif 0346 0347 /* Generate a proper function signature */ 0348 std::string signature; 0349 size_t type_index = 0, arg_index = 0; 0350 bool is_starred = false; 0351 for (auto *pc = text; *pc != '\0'; ++pc) { 0352 const auto c = *pc; 0353 0354 if (c == '{') { 0355 // Write arg name for everything except *args and **kwargs. 0356 is_starred = *(pc + 1) == '*'; 0357 if (is_starred) 0358 continue; 0359 // Separator for keyword-only arguments, placed before the kw 0360 // arguments start (unless we are already putting an *args) 0361 if (!rec->has_args && arg_index == rec->nargs_pos) 0362 signature += "*, "; 0363 if (arg_index < rec->args.size() && rec->args[arg_index].name) { 0364 signature += rec->args[arg_index].name; 0365 } else if (arg_index == 0 && rec->is_method) { 0366 signature += "self"; 0367 } else { 0368 signature += "arg" + std::to_string(arg_index - (rec->is_method ? 1 : 0)); 0369 } 0370 signature += ": "; 0371 } else if (c == '}') { 0372 // Write default value if available. 0373 if (!is_starred && arg_index < rec->args.size() && rec->args[arg_index].descr) { 0374 signature += " = "; 0375 signature += rec->args[arg_index].descr; 0376 } 0377 // Separator for positional-only arguments (placed after the 0378 // argument, rather than before like * 0379 if (rec->nargs_pos_only > 0 && (arg_index + 1) == rec->nargs_pos_only) 0380 signature += ", /"; 0381 if (!is_starred) 0382 arg_index++; 0383 } else if (c == '%') { 0384 const std::type_info *t = types[type_index++]; 0385 if (!t) 0386 pybind11_fail("Internal error while parsing type signature (1)"); 0387 if (auto tinfo = detail::get_type_info(*t)) { 0388 handle th((PyObject *) tinfo->type); 0389 signature += 0390 th.attr("__module__").cast<std::string>() + "." + 0391 th.attr("__qualname__").cast<std::string>(); // Python 3.3+, but we backport it to earlier versions 0392 } else if (rec->is_new_style_constructor && arg_index == 0) { 0393 // A new-style `__init__` takes `self` as `value_and_holder`. 0394 // Rewrite it to the proper class type. 0395 signature += 0396 rec->scope.attr("__module__").cast<std::string>() + "." + 0397 rec->scope.attr("__qualname__").cast<std::string>(); 0398 } else { 0399 std::string tname(t->name()); 0400 detail::clean_type_id(tname); 0401 signature += tname; 0402 } 0403 } else { 0404 signature += c; 0405 } 0406 } 0407 0408 if (arg_index != args - rec->has_args - rec->has_kwargs || types[type_index] != nullptr) 0409 pybind11_fail("Internal error while parsing type signature (2)"); 0410 0411 #if PY_MAJOR_VERSION < 3 0412 if (std::strcmp(rec->name, "__next__") == 0) { 0413 std::free(rec->name); 0414 rec->name = guarded_strdup("next"); 0415 } else if (std::strcmp(rec->name, "__bool__") == 0) { 0416 std::free(rec->name); 0417 rec->name = guarded_strdup("__nonzero__"); 0418 } 0419 #endif 0420 rec->signature = guarded_strdup(signature.c_str()); 0421 rec->args.shrink_to_fit(); 0422 rec->nargs = (std::uint16_t) args; 0423 0424 if (rec->sibling && PYBIND11_INSTANCE_METHOD_CHECK(rec->sibling.ptr())) 0425 rec->sibling = PYBIND11_INSTANCE_METHOD_GET_FUNCTION(rec->sibling.ptr()); 0426 0427 detail::function_record *chain = nullptr, *chain_start = rec; 0428 if (rec->sibling) { 0429 if (PyCFunction_Check(rec->sibling.ptr())) { 0430 auto *self = PyCFunction_GET_SELF(rec->sibling.ptr()); 0431 capsule rec_capsule = isinstance<capsule>(self) ? reinterpret_borrow<capsule>(self) : capsule(self); 0432 chain = (detail::function_record *) rec_capsule; 0433 /* Never append a method to an overload chain of a parent class; 0434 instead, hide the parent's overloads in this case */ 0435 if (!chain->scope.is(rec->scope)) 0436 chain = nullptr; 0437 } 0438 // Don't trigger for things like the default __init__, which are wrapper_descriptors that we are intentionally replacing 0439 else if (!rec->sibling.is_none() && rec->name[0] != '_') 0440 pybind11_fail("Cannot overload existing non-function object \"" + std::string(rec->name) + 0441 "\" with a function of the same name"); 0442 } 0443 0444 if (!chain) { 0445 /* No existing overload was found, create a new function object */ 0446 rec->def = new PyMethodDef(); 0447 std::memset(rec->def, 0, sizeof(PyMethodDef)); 0448 rec->def->ml_name = rec->name; 0449 rec->def->ml_meth 0450 = reinterpret_cast<PyCFunction>(reinterpret_cast<void (*)()>(dispatcher)); 0451 rec->def->ml_flags = METH_VARARGS | METH_KEYWORDS; 0452 0453 capsule rec_capsule(unique_rec.release(), [](void *ptr) { 0454 destruct((detail::function_record *) ptr); 0455 }); 0456 guarded_strdup.release(); 0457 0458 object scope_module; 0459 if (rec->scope) { 0460 if (hasattr(rec->scope, "__module__")) { 0461 scope_module = rec->scope.attr("__module__"); 0462 } else if (hasattr(rec->scope, "__name__")) { 0463 scope_module = rec->scope.attr("__name__"); 0464 } 0465 } 0466 0467 m_ptr = PyCFunction_NewEx(rec->def, rec_capsule.ptr(), scope_module.ptr()); 0468 if (!m_ptr) 0469 pybind11_fail("cpp_function::cpp_function(): Could not allocate function object"); 0470 } else { 0471 /* Append at the beginning or end of the overload chain */ 0472 m_ptr = rec->sibling.ptr(); 0473 inc_ref(); 0474 if (chain->is_method != rec->is_method) 0475 pybind11_fail("overloading a method with both static and instance methods is not supported; " 0476 #if defined(NDEBUG) 0477 "compile in debug mode for more details" 0478 #else 0479 "error while attempting to bind " + std::string(rec->is_method ? "instance" : "static") + " method " + 0480 std::string(pybind11::str(rec->scope.attr("__name__"))) + "." + std::string(rec->name) + signature 0481 #endif 0482 ); 0483 0484 if (rec->prepend) { 0485 // Beginning of chain; we need to replace the capsule's current head-of-the-chain 0486 // pointer with this one, then make this one point to the previous head of the 0487 // chain. 0488 chain_start = rec; 0489 rec->next = chain; 0490 auto rec_capsule = reinterpret_borrow<capsule>(((PyCFunctionObject *) m_ptr)->m_self); 0491 rec_capsule.set_pointer(unique_rec.release()); 0492 guarded_strdup.release(); 0493 } else { 0494 // Or end of chain (normal behavior) 0495 chain_start = chain; 0496 while (chain->next) 0497 chain = chain->next; 0498 chain->next = unique_rec.release(); 0499 guarded_strdup.release(); 0500 } 0501 } 0502 0503 std::string signatures; 0504 int index = 0; 0505 /* Create a nice pydoc rec including all signatures and 0506 docstrings of the functions in the overload chain */ 0507 if (chain && options::show_function_signatures()) { 0508 // First a generic signature 0509 signatures += rec->name; 0510 signatures += "(*args, **kwargs)\n"; 0511 signatures += "Overloaded function.\n\n"; 0512 } 0513 // Then specific overload signatures 0514 bool first_user_def = true; 0515 for (auto it = chain_start; it != nullptr; it = it->next) { 0516 if (options::show_function_signatures()) { 0517 if (index > 0) signatures += "\n"; 0518 if (chain) 0519 signatures += std::to_string(++index) + ". "; 0520 signatures += rec->name; 0521 signatures += it->signature; 0522 signatures += "\n"; 0523 } 0524 if (it->doc && it->doc[0] != '\0' && options::show_user_defined_docstrings()) { 0525 // If we're appending another docstring, and aren't printing function signatures, we 0526 // need to append a newline first: 0527 if (!options::show_function_signatures()) { 0528 if (first_user_def) first_user_def = false; 0529 else signatures += "\n"; 0530 } 0531 if (options::show_function_signatures()) signatures += "\n"; 0532 signatures += it->doc; 0533 if (options::show_function_signatures()) signatures += "\n"; 0534 } 0535 } 0536 0537 /* Install docstring */ 0538 auto *func = (PyCFunctionObject *) m_ptr; 0539 std::free(const_cast<char *>(func->m_ml->ml_doc)); 0540 // Install docstring if it's non-empty (when at least one option is enabled) 0541 func->m_ml->ml_doc 0542 = signatures.empty() ? nullptr : PYBIND11_COMPAT_STRDUP(signatures.c_str()); 0543 0544 if (rec->is_method) { 0545 m_ptr = PYBIND11_INSTANCE_METHOD_NEW(m_ptr, rec->scope.ptr()); 0546 if (!m_ptr) 0547 pybind11_fail("cpp_function::cpp_function(): Could not allocate instance method object"); 0548 Py_DECREF(func); 0549 } 0550 } 0551 0552 /// When a cpp_function is GCed, release any memory allocated by pybind11 0553 static void destruct(detail::function_record *rec, bool free_strings = true) { 0554 // If on Python 3.9, check the interpreter "MICRO" (patch) version. 0555 // If this is running on 3.9.0, we have to work around a bug. 0556 #if !defined(PYPY_VERSION) && PY_MAJOR_VERSION == 3 && PY_MINOR_VERSION == 9 0557 static bool is_zero = Py_GetVersion()[4] == '0'; 0558 #endif 0559 0560 while (rec) { 0561 detail::function_record *next = rec->next; 0562 if (rec->free_data) 0563 rec->free_data(rec); 0564 // During initialization, these strings might not have been copied yet, 0565 // so they cannot be freed. Once the function has been created, they can. 0566 // Check `make_function_record` for more details. 0567 if (free_strings) { 0568 std::free((char *) rec->name); 0569 std::free((char *) rec->doc); 0570 std::free((char *) rec->signature); 0571 for (auto &arg: rec->args) { 0572 std::free(const_cast<char *>(arg.name)); 0573 std::free(const_cast<char *>(arg.descr)); 0574 } 0575 } 0576 for (auto &arg: rec->args) 0577 arg.value.dec_ref(); 0578 if (rec->def) { 0579 std::free(const_cast<char *>(rec->def->ml_doc)); 0580 // Python 3.9.0 decref's these in the wrong order; rec->def 0581 // If loaded on 3.9.0, let these leak (use Python 3.9.1 at runtime to fix) 0582 // See https://github.com/python/cpython/pull/22670 0583 #if !defined(PYPY_VERSION) && PY_MAJOR_VERSION == 3 && PY_MINOR_VERSION == 9 0584 if (!is_zero) 0585 delete rec->def; 0586 #else 0587 delete rec->def; 0588 #endif 0589 } 0590 delete rec; 0591 rec = next; 0592 } 0593 } 0594 0595 0596 /// Main dispatch logic for calls to functions bound using pybind11 0597 static PyObject *dispatcher(PyObject *self, PyObject *args_in, PyObject *kwargs_in) { 0598 using namespace detail; 0599 0600 /* Iterator over the list of potentially admissible overloads */ 0601 const function_record *overloads = (function_record *) PyCapsule_GetPointer(self, nullptr), 0602 *it = overloads; 0603 0604 /* Need to know how many arguments + keyword arguments there are to pick the right overload */ 0605 const auto n_args_in = (size_t) PyTuple_GET_SIZE(args_in); 0606 0607 handle parent = n_args_in > 0 ? PyTuple_GET_ITEM(args_in, 0) : nullptr, 0608 result = PYBIND11_TRY_NEXT_OVERLOAD; 0609 0610 auto self_value_and_holder = value_and_holder(); 0611 if (overloads->is_constructor) { 0612 if (!parent || !PyObject_TypeCheck(parent.ptr(), (PyTypeObject *) overloads->scope.ptr())) { 0613 PyErr_SetString(PyExc_TypeError, "__init__(self, ...) called with invalid or missing `self` argument"); 0614 return nullptr; 0615 } 0616 0617 const auto tinfo = get_type_info((PyTypeObject *) overloads->scope.ptr()); 0618 const auto pi = reinterpret_cast<instance *>(parent.ptr()); 0619 self_value_and_holder = pi->get_value_and_holder(tinfo, true); 0620 0621 // If this value is already registered it must mean __init__ is invoked multiple times; 0622 // we really can't support that in C++, so just ignore the second __init__. 0623 if (self_value_and_holder.instance_registered()) 0624 return none().release().ptr(); 0625 } 0626 0627 try { 0628 // We do this in two passes: in the first pass, we load arguments with `convert=false`; 0629 // in the second, we allow conversion (except for arguments with an explicit 0630 // py::arg().noconvert()). This lets us prefer calls without conversion, with 0631 // conversion as a fallback. 0632 std::vector<function_call> second_pass; 0633 0634 // However, if there are no overloads, we can just skip the no-convert pass entirely 0635 const bool overloaded = it != nullptr && it->next != nullptr; 0636 0637 for (; it != nullptr; it = it->next) { 0638 0639 /* For each overload: 0640 1. Copy all positional arguments we were given, also checking to make sure that 0641 named positional arguments weren't *also* specified via kwarg. 0642 2. If we weren't given enough, try to make up the omitted ones by checking 0643 whether they were provided by a kwarg matching the `py::arg("name")` name. If 0644 so, use it (and remove it from kwargs); if not, see if the function binding 0645 provided a default that we can use. 0646 3. Ensure that either all keyword arguments were "consumed", or that the function 0647 takes a kwargs argument to accept unconsumed kwargs. 0648 4. Any positional arguments still left get put into a tuple (for args), and any 0649 leftover kwargs get put into a dict. 0650 5. Pack everything into a vector; if we have py::args or py::kwargs, they are an 0651 extra tuple or dict at the end of the positional arguments. 0652 6. Call the function call dispatcher (function_record::impl) 0653 0654 If one of these fail, move on to the next overload and keep trying until we get a 0655 result other than PYBIND11_TRY_NEXT_OVERLOAD. 0656 */ 0657 0658 const function_record &func = *it; 0659 size_t num_args = func.nargs; // Number of positional arguments that we need 0660 if (func.has_args) --num_args; // (but don't count py::args 0661 if (func.has_kwargs) --num_args; // or py::kwargs) 0662 size_t pos_args = func.nargs_pos; 0663 0664 if (!func.has_args && n_args_in > pos_args) 0665 continue; // Too many positional arguments for this overload 0666 0667 if (n_args_in < pos_args && func.args.size() < pos_args) 0668 continue; // Not enough positional arguments given, and not enough defaults to fill in the blanks 0669 0670 function_call call(func, parent); 0671 0672 size_t args_to_copy = (std::min)(pos_args, n_args_in); // Protect std::min with parentheses 0673 size_t args_copied = 0; 0674 0675 // 0. Inject new-style `self` argument 0676 if (func.is_new_style_constructor) { 0677 // The `value` may have been preallocated by an old-style `__init__` 0678 // if it was a preceding candidate for overload resolution. 0679 if (self_value_and_holder) 0680 self_value_and_holder.type->dealloc(self_value_and_holder); 0681 0682 call.init_self = PyTuple_GET_ITEM(args_in, 0); 0683 call.args.emplace_back(reinterpret_cast<PyObject *>(&self_value_and_holder)); 0684 call.args_convert.push_back(false); 0685 ++args_copied; 0686 } 0687 0688 // 1. Copy any position arguments given. 0689 bool bad_arg = false; 0690 for (; args_copied < args_to_copy; ++args_copied) { 0691 const argument_record *arg_rec = args_copied < func.args.size() ? &func.args[args_copied] : nullptr; 0692 if (kwargs_in && arg_rec && arg_rec->name && dict_getitemstring(kwargs_in, arg_rec->name)) { 0693 bad_arg = true; 0694 break; 0695 } 0696 0697 handle arg(PyTuple_GET_ITEM(args_in, args_copied)); 0698 if (arg_rec && !arg_rec->none && arg.is_none()) { 0699 bad_arg = true; 0700 break; 0701 } 0702 call.args.push_back(arg); 0703 call.args_convert.push_back(arg_rec ? arg_rec->convert : true); 0704 } 0705 if (bad_arg) 0706 continue; // Maybe it was meant for another overload (issue #688) 0707 0708 // Keep track of how many position args we copied out in case we need to come back 0709 // to copy the rest into a py::args argument. 0710 size_t positional_args_copied = args_copied; 0711 0712 // We'll need to copy this if we steal some kwargs for defaults 0713 dict kwargs = reinterpret_borrow<dict>(kwargs_in); 0714 0715 // 1.5. Fill in any missing pos_only args from defaults if they exist 0716 if (args_copied < func.nargs_pos_only) { 0717 for (; args_copied < func.nargs_pos_only; ++args_copied) { 0718 const auto &arg_rec = func.args[args_copied]; 0719 handle value; 0720 0721 if (arg_rec.value) { 0722 value = arg_rec.value; 0723 } 0724 if (value) { 0725 call.args.push_back(value); 0726 call.args_convert.push_back(arg_rec.convert); 0727 } else 0728 break; 0729 } 0730 0731 if (args_copied < func.nargs_pos_only) 0732 continue; // Not enough defaults to fill the positional arguments 0733 } 0734 0735 // 2. Check kwargs and, failing that, defaults that may help complete the list 0736 if (args_copied < num_args) { 0737 bool copied_kwargs = false; 0738 0739 for (; args_copied < num_args; ++args_copied) { 0740 const auto &arg_rec = func.args[args_copied]; 0741 0742 handle value; 0743 if (kwargs_in && arg_rec.name) 0744 value = dict_getitemstring(kwargs.ptr(), arg_rec.name); 0745 0746 if (value) { 0747 // Consume a kwargs value 0748 if (!copied_kwargs) { 0749 kwargs = reinterpret_steal<dict>(PyDict_Copy(kwargs.ptr())); 0750 copied_kwargs = true; 0751 } 0752 if (PyDict_DelItemString(kwargs.ptr(), arg_rec.name) == -1) { 0753 throw error_already_set(); 0754 } 0755 } else if (arg_rec.value) { 0756 value = arg_rec.value; 0757 } 0758 0759 if (!arg_rec.none && value.is_none()) { 0760 break; 0761 } 0762 0763 if (value) { 0764 // If we're at the py::args index then first insert a stub for it to be replaced later 0765 if (func.has_args && call.args.size() == func.nargs_pos) 0766 call.args.push_back(none()); 0767 0768 call.args.push_back(value); 0769 call.args_convert.push_back(arg_rec.convert); 0770 } 0771 else 0772 break; 0773 } 0774 0775 if (args_copied < num_args) 0776 continue; // Not enough arguments, defaults, or kwargs to fill the positional arguments 0777 } 0778 0779 // 3. Check everything was consumed (unless we have a kwargs arg) 0780 if (kwargs && !kwargs.empty() && !func.has_kwargs) 0781 continue; // Unconsumed kwargs, but no py::kwargs argument to accept them 0782 0783 // 4a. If we have a py::args argument, create a new tuple with leftovers 0784 if (func.has_args) { 0785 tuple extra_args; 0786 if (args_to_copy == 0) { 0787 // We didn't copy out any position arguments from the args_in tuple, so we 0788 // can reuse it directly without copying: 0789 extra_args = reinterpret_borrow<tuple>(args_in); 0790 } else if (positional_args_copied >= n_args_in) { 0791 extra_args = tuple(0); 0792 } else { 0793 size_t args_size = n_args_in - positional_args_copied; 0794 extra_args = tuple(args_size); 0795 for (size_t i = 0; i < args_size; ++i) { 0796 extra_args[i] = PyTuple_GET_ITEM(args_in, positional_args_copied + i); 0797 } 0798 } 0799 if (call.args.size() <= func.nargs_pos) 0800 call.args.push_back(extra_args); 0801 else 0802 call.args[func.nargs_pos] = extra_args; 0803 call.args_convert.push_back(false); 0804 call.args_ref = std::move(extra_args); 0805 } 0806 0807 // 4b. If we have a py::kwargs, pass on any remaining kwargs 0808 if (func.has_kwargs) { 0809 if (!kwargs.ptr()) 0810 kwargs = dict(); // If we didn't get one, send an empty one 0811 call.args.push_back(kwargs); 0812 call.args_convert.push_back(false); 0813 call.kwargs_ref = std::move(kwargs); 0814 } 0815 0816 // 5. Put everything in a vector. Not technically step 5, we've been building it 0817 // in `call.args` all along. 0818 #if !defined(NDEBUG) 0819 if (call.args.size() != func.nargs || call.args_convert.size() != func.nargs) 0820 pybind11_fail("Internal error: function call dispatcher inserted wrong number of arguments!"); 0821 #endif 0822 0823 std::vector<bool> second_pass_convert; 0824 if (overloaded) { 0825 // We're in the first no-convert pass, so swap out the conversion flags for a 0826 // set of all-false flags. If the call fails, we'll swap the flags back in for 0827 // the conversion-allowed call below. 0828 second_pass_convert.resize(func.nargs, false); 0829 call.args_convert.swap(second_pass_convert); 0830 } 0831 0832 // 6. Call the function. 0833 try { 0834 loader_life_support guard{}; 0835 result = func.impl(call); 0836 } catch (reference_cast_error &) { 0837 result = PYBIND11_TRY_NEXT_OVERLOAD; 0838 } 0839 0840 if (result.ptr() != PYBIND11_TRY_NEXT_OVERLOAD) 0841 break; 0842 0843 if (overloaded) { 0844 // The (overloaded) call failed; if the call has at least one argument that 0845 // permits conversion (i.e. it hasn't been explicitly specified `.noconvert()`) 0846 // then add this call to the list of second pass overloads to try. 0847 for (size_t i = func.is_method ? 1 : 0; i < pos_args; i++) { 0848 if (second_pass_convert[i]) { 0849 // Found one: swap the converting flags back in and store the call for 0850 // the second pass. 0851 call.args_convert.swap(second_pass_convert); 0852 second_pass.push_back(std::move(call)); 0853 break; 0854 } 0855 } 0856 } 0857 } 0858 0859 if (overloaded && !second_pass.empty() && result.ptr() == PYBIND11_TRY_NEXT_OVERLOAD) { 0860 // The no-conversion pass finished without success, try again with conversion allowed 0861 for (auto &call : second_pass) { 0862 try { 0863 loader_life_support guard{}; 0864 result = call.func.impl(call); 0865 } catch (reference_cast_error &) { 0866 result = PYBIND11_TRY_NEXT_OVERLOAD; 0867 } 0868 0869 if (result.ptr() != PYBIND11_TRY_NEXT_OVERLOAD) { 0870 // The error reporting logic below expects 'it' to be valid, as it would be 0871 // if we'd encountered this failure in the first-pass loop. 0872 if (!result) 0873 it = &call.func; 0874 break; 0875 } 0876 } 0877 } 0878 } catch (error_already_set &e) { 0879 e.restore(); 0880 return nullptr; 0881 #ifdef __GLIBCXX__ 0882 } catch ( abi::__forced_unwind& ) { 0883 throw; 0884 #endif 0885 } catch (...) { 0886 /* When an exception is caught, give each registered exception 0887 translator a chance to translate it to a Python exception. First 0888 all module-local translators will be tried in reverse order of 0889 registration. If none of the module-locale translators handle 0890 the exception (or there are no module-locale translators) then 0891 the global translators will be tried, also in reverse order of 0892 registration. 0893 0894 A translator may choose to do one of the following: 0895 0896 - catch the exception and call PyErr_SetString or PyErr_SetObject 0897 to set a standard (or custom) Python exception, or 0898 - do nothing and let the exception fall through to the next translator, or 0899 - delegate translation to the next translator by throwing a new type of exception. */ 0900 0901 auto &local_exception_translators = get_local_internals().registered_exception_translators; 0902 if (detail::apply_exception_translators(local_exception_translators)) { 0903 return nullptr; 0904 } 0905 auto &exception_translators = get_internals().registered_exception_translators; 0906 if (detail::apply_exception_translators(exception_translators)) { 0907 return nullptr; 0908 } 0909 0910 PyErr_SetString(PyExc_SystemError, "Exception escaped from default exception translator!"); 0911 return nullptr; 0912 } 0913 0914 auto append_note_if_missing_header_is_suspected = [](std::string &msg) { 0915 if (msg.find("std::") != std::string::npos) { 0916 msg += "\n\n" 0917 "Did you forget to `#include <pybind11/stl.h>`? Or <pybind11/complex.h>,\n" 0918 "<pybind11/functional.h>, <pybind11/chrono.h>, etc. Some automatic\n" 0919 "conversions are optional and require extra headers to be included\n" 0920 "when compiling your pybind11 module."; 0921 } 0922 }; 0923 0924 if (result.ptr() == PYBIND11_TRY_NEXT_OVERLOAD) { 0925 if (overloads->is_operator) 0926 return handle(Py_NotImplemented).inc_ref().ptr(); 0927 0928 std::string msg = std::string(overloads->name) + "(): incompatible " + 0929 std::string(overloads->is_constructor ? "constructor" : "function") + 0930 " arguments. The following argument types are supported:\n"; 0931 0932 int ctr = 0; 0933 for (const function_record *it2 = overloads; it2 != nullptr; it2 = it2->next) { 0934 msg += " "+ std::to_string(++ctr) + ". "; 0935 0936 bool wrote_sig = false; 0937 if (overloads->is_constructor) { 0938 // For a constructor, rewrite `(self: Object, arg0, ...) -> NoneType` as `Object(arg0, ...)` 0939 std::string sig = it2->signature; 0940 size_t start = sig.find('(') + 7; // skip "(self: " 0941 if (start < sig.size()) { 0942 // End at the , for the next argument 0943 size_t end = sig.find(", "), next = end + 2; 0944 size_t ret = sig.rfind(" -> "); 0945 // Or the ), if there is no comma: 0946 if (end >= sig.size()) next = end = sig.find(')'); 0947 if (start < end && next < sig.size()) { 0948 msg.append(sig, start, end - start); 0949 msg += '('; 0950 msg.append(sig, next, ret - next); 0951 wrote_sig = true; 0952 } 0953 } 0954 } 0955 if (!wrote_sig) msg += it2->signature; 0956 0957 msg += "\n"; 0958 } 0959 msg += "\nInvoked with: "; 0960 auto args_ = reinterpret_borrow<tuple>(args_in); 0961 bool some_args = false; 0962 for (size_t ti = overloads->is_constructor ? 1 : 0; ti < args_.size(); ++ti) { 0963 if (!some_args) some_args = true; 0964 else msg += ", "; 0965 try { 0966 msg += pybind11::repr(args_[ti]); 0967 } catch (const error_already_set&) { 0968 msg += "<repr raised Error>"; 0969 } 0970 } 0971 if (kwargs_in) { 0972 auto kwargs = reinterpret_borrow<dict>(kwargs_in); 0973 if (!kwargs.empty()) { 0974 if (some_args) msg += "; "; 0975 msg += "kwargs: "; 0976 bool first = true; 0977 for (auto kwarg : kwargs) { 0978 if (first) first = false; 0979 else msg += ", "; 0980 msg += pybind11::str("{}=").format(kwarg.first); 0981 try { 0982 msg += pybind11::repr(kwarg.second); 0983 } catch (const error_already_set&) { 0984 msg += "<repr raised Error>"; 0985 } 0986 } 0987 } 0988 } 0989 0990 append_note_if_missing_header_is_suspected(msg); 0991 #if PY_VERSION_HEX >= 0x03030000 0992 // Attach additional error info to the exception if supported 0993 if (PyErr_Occurred()) { 0994 // #HelpAppreciated: unit test coverage for this branch. 0995 raise_from(PyExc_TypeError, msg.c_str()); 0996 return nullptr; 0997 } 0998 #endif 0999 PyErr_SetString(PyExc_TypeError, msg.c_str()); 1000 return nullptr; 1001 } 1002 if (!result) { 1003 std::string msg = "Unable to convert function return value to a " 1004 "Python type! The signature was\n\t"; 1005 msg += it->signature; 1006 append_note_if_missing_header_is_suspected(msg); 1007 #if PY_VERSION_HEX >= 0x03030000 1008 // Attach additional error info to the exception if supported 1009 if (PyErr_Occurred()) { 1010 raise_from(PyExc_TypeError, msg.c_str()); 1011 return nullptr; 1012 } 1013 #endif 1014 PyErr_SetString(PyExc_TypeError, msg.c_str()); 1015 return nullptr; 1016 } 1017 if (overloads->is_constructor && !self_value_and_holder.holder_constructed()) { 1018 auto *pi = reinterpret_cast<instance *>(parent.ptr()); 1019 self_value_and_holder.type->init_instance(pi, nullptr); 1020 } 1021 return result.ptr(); 1022 } 1023 }; 1024 1025 1026 /// Wrapper for Python extension modules 1027 class module_ : public object { 1028 public: 1029 PYBIND11_OBJECT_DEFAULT(module_, object, PyModule_Check) 1030 1031 /// Create a new top-level Python module with the given name and docstring 1032 PYBIND11_DEPRECATED("Use PYBIND11_MODULE or module_::create_extension_module instead") 1033 explicit module_(const char *name, const char *doc = nullptr) { 1034 #if PY_MAJOR_VERSION >= 3 1035 *this = create_extension_module(name, doc, new PyModuleDef()); 1036 #else 1037 *this = create_extension_module(name, doc, nullptr); 1038 #endif 1039 } 1040 1041 /** \rst 1042 Create Python binding for a new function within the module scope. ``Func`` 1043 can be a plain C++ function, a function pointer, or a lambda function. For 1044 details on the ``Extra&& ... extra`` argument, see section :ref:`extras`. 1045 \endrst */ 1046 template <typename Func, typename... Extra> 1047 module_ &def(const char *name_, Func &&f, const Extra& ... extra) { 1048 cpp_function func(std::forward<Func>(f), name(name_), scope(*this), 1049 sibling(getattr(*this, name_, none())), extra...); 1050 // NB: allow overwriting here because cpp_function sets up a chain with the intention of 1051 // overwriting (and has already checked internally that it isn't overwriting non-functions). 1052 add_object(name_, func, true /* overwrite */); 1053 return *this; 1054 } 1055 1056 /** \rst 1057 Create and return a new Python submodule with the given name and docstring. 1058 This also works recursively, i.e. 1059 1060 .. code-block:: cpp 1061 1062 py::module_ m("example", "pybind11 example plugin"); 1063 py::module_ m2 = m.def_submodule("sub", "A submodule of 'example'"); 1064 py::module_ m3 = m2.def_submodule("subsub", "A submodule of 'example.sub'"); 1065 \endrst */ 1066 module_ def_submodule(const char *name, const char *doc = nullptr) { 1067 std::string full_name = std::string(PyModule_GetName(m_ptr)) 1068 + std::string(".") + std::string(name); 1069 auto result = reinterpret_borrow<module_>(PyImport_AddModule(full_name.c_str())); 1070 if (doc && options::show_user_defined_docstrings()) 1071 result.attr("__doc__") = pybind11::str(doc); 1072 attr(name) = result; 1073 return result; 1074 } 1075 1076 /// Import and return a module or throws `error_already_set`. 1077 static module_ import(const char *name) { 1078 PyObject *obj = PyImport_ImportModule(name); 1079 if (!obj) 1080 throw error_already_set(); 1081 return reinterpret_steal<module_>(obj); 1082 } 1083 1084 /// Reload the module or throws `error_already_set`. 1085 void reload() { 1086 PyObject *obj = PyImport_ReloadModule(ptr()); 1087 if (!obj) 1088 throw error_already_set(); 1089 *this = reinterpret_steal<module_>(obj); 1090 } 1091 1092 /** \rst 1093 Adds an object to the module using the given name. Throws if an object with the given name 1094 already exists. 1095 1096 ``overwrite`` should almost always be false: attempting to overwrite objects that pybind11 has 1097 established will, in most cases, break things. 1098 \endrst */ 1099 PYBIND11_NOINLINE void add_object(const char *name, handle obj, bool overwrite = false) { 1100 if (!overwrite && hasattr(*this, name)) 1101 pybind11_fail("Error during initialization: multiple incompatible definitions with name \"" + 1102 std::string(name) + "\""); 1103 1104 PyModule_AddObject(ptr(), name, obj.inc_ref().ptr() /* steals a reference */); 1105 } 1106 1107 #if PY_MAJOR_VERSION >= 3 1108 using module_def = PyModuleDef; 1109 #else 1110 struct module_def {}; 1111 #endif 1112 1113 /** \rst 1114 Create a new top-level module that can be used as the main module of a C extension. 1115 1116 For Python 3, ``def`` should point to a statically allocated module_def. 1117 For Python 2, ``def`` can be a nullptr and is completely ignored. 1118 \endrst */ 1119 static module_ create_extension_module(const char *name, const char *doc, module_def *def) { 1120 #if PY_MAJOR_VERSION >= 3 1121 // module_def is PyModuleDef 1122 def = new (def) PyModuleDef { // Placement new (not an allocation). 1123 /* m_base */ PyModuleDef_HEAD_INIT, 1124 /* m_name */ name, 1125 /* m_doc */ options::show_user_defined_docstrings() ? doc : nullptr, 1126 /* m_size */ -1, 1127 /* m_methods */ nullptr, 1128 /* m_slots */ nullptr, 1129 /* m_traverse */ nullptr, 1130 /* m_clear */ nullptr, 1131 /* m_free */ nullptr 1132 }; 1133 auto m = PyModule_Create(def); 1134 #else 1135 // Ignore module_def *def; only necessary for Python 3 1136 (void) def; 1137 auto m = Py_InitModule3(name, nullptr, options::show_user_defined_docstrings() ? doc : nullptr); 1138 #endif 1139 if (m == nullptr) { 1140 if (PyErr_Occurred()) 1141 throw error_already_set(); 1142 pybind11_fail("Internal error in module_::create_extension_module()"); 1143 } 1144 // TODO: Should be reinterpret_steal for Python 3, but Python also steals it again when returned from PyInit_... 1145 // For Python 2, reinterpret_borrow is correct. 1146 return reinterpret_borrow<module_>(m); 1147 } 1148 }; 1149 1150 // When inside a namespace (or anywhere as long as it's not the first item on a line), 1151 // C++20 allows "module" to be used. This is provided for backward compatibility, and for 1152 // simplicity, if someone wants to use py::module for example, that is perfectly safe. 1153 using module = module_; 1154 1155 /// \ingroup python_builtins 1156 /// Return a dictionary representing the global variables in the current execution frame, 1157 /// or ``__main__.__dict__`` if there is no frame (usually when the interpreter is embedded). 1158 inline dict globals() { 1159 PyObject *p = PyEval_GetGlobals(); 1160 return reinterpret_borrow<dict>(p ? p : module_::import("__main__").attr("__dict__").ptr()); 1161 } 1162 1163 #if PY_VERSION_HEX >= 0x03030000 1164 template <typename... Args, 1165 typename = detail::enable_if_t<args_are_all_keyword_or_ds<Args...>()>> 1166 PYBIND11_DEPRECATED("make_simple_namespace should be replaced with py::module_::import(\"types\").attr(\"SimpleNamespace\") ") 1167 object make_simple_namespace(Args&&... args_) { 1168 return module_::import("types").attr("SimpleNamespace")(std::forward<Args>(args_)...); 1169 } 1170 #endif 1171 1172 PYBIND11_NAMESPACE_BEGIN(detail) 1173 /// Generic support for creating new Python heap types 1174 class generic_type : public object { 1175 public: 1176 PYBIND11_OBJECT_DEFAULT(generic_type, object, PyType_Check) 1177 protected: 1178 void initialize(const type_record &rec) { 1179 if (rec.scope && hasattr(rec.scope, "__dict__") && rec.scope.attr("__dict__").contains(rec.name)) 1180 pybind11_fail("generic_type: cannot initialize type \"" + std::string(rec.name) + 1181 "\": an object with that name is already defined"); 1182 1183 if ((rec.module_local ? get_local_type_info(*rec.type) : get_global_type_info(*rec.type)) 1184 != nullptr) 1185 pybind11_fail("generic_type: type \"" + std::string(rec.name) + 1186 "\" is already registered!"); 1187 1188 m_ptr = make_new_python_type(rec); 1189 1190 /* Register supplemental type information in C++ dict */ 1191 auto *tinfo = new detail::type_info(); 1192 tinfo->type = (PyTypeObject *) m_ptr; 1193 tinfo->cpptype = rec.type; 1194 tinfo->type_size = rec.type_size; 1195 tinfo->type_align = rec.type_align; 1196 tinfo->operator_new = rec.operator_new; 1197 tinfo->holder_size_in_ptrs = size_in_ptrs(rec.holder_size); 1198 tinfo->init_instance = rec.init_instance; 1199 tinfo->dealloc = rec.dealloc; 1200 tinfo->simple_type = true; 1201 tinfo->simple_ancestors = true; 1202 tinfo->default_holder = rec.default_holder; 1203 tinfo->module_local = rec.module_local; 1204 1205 auto &internals = get_internals(); 1206 auto tindex = std::type_index(*rec.type); 1207 tinfo->direct_conversions = &internals.direct_conversions[tindex]; 1208 if (rec.module_local) 1209 get_local_internals().registered_types_cpp[tindex] = tinfo; 1210 else 1211 internals.registered_types_cpp[tindex] = tinfo; 1212 internals.registered_types_py[(PyTypeObject *) m_ptr] = { tinfo }; 1213 1214 if (rec.bases.size() > 1 || rec.multiple_inheritance) { 1215 mark_parents_nonsimple(tinfo->type); 1216 tinfo->simple_ancestors = false; 1217 } 1218 else if (rec.bases.size() == 1) { 1219 auto *parent_tinfo = get_type_info((PyTypeObject *) rec.bases[0].ptr()); 1220 assert(parent_tinfo != nullptr); 1221 bool parent_simple_ancestors = parent_tinfo->simple_ancestors; 1222 tinfo->simple_ancestors = parent_simple_ancestors; 1223 // The parent can no longer be a simple type if it has MI and has a child 1224 parent_tinfo->simple_type = parent_tinfo->simple_type && parent_simple_ancestors; 1225 } 1226 1227 if (rec.module_local) { 1228 // Stash the local typeinfo and loader so that external modules can access it. 1229 tinfo->module_local_load = &type_caster_generic::local_load; 1230 setattr(m_ptr, PYBIND11_MODULE_LOCAL_ID, capsule(tinfo)); 1231 } 1232 } 1233 1234 /// Helper function which tags all parents of a type using mult. inheritance 1235 void mark_parents_nonsimple(PyTypeObject *value) { 1236 auto t = reinterpret_borrow<tuple>(value->tp_bases); 1237 for (handle h : t) { 1238 auto tinfo2 = get_type_info((PyTypeObject *) h.ptr()); 1239 if (tinfo2) 1240 tinfo2->simple_type = false; 1241 mark_parents_nonsimple((PyTypeObject *) h.ptr()); 1242 } 1243 } 1244 1245 void install_buffer_funcs( 1246 buffer_info *(*get_buffer)(PyObject *, void *), 1247 void *get_buffer_data) { 1248 auto *type = (PyHeapTypeObject*) m_ptr; 1249 auto tinfo = detail::get_type_info(&type->ht_type); 1250 1251 if (!type->ht_type.tp_as_buffer) 1252 pybind11_fail( 1253 "To be able to register buffer protocol support for the type '" + 1254 get_fully_qualified_tp_name(tinfo->type) + 1255 "' the associated class<>(..) invocation must " 1256 "include the pybind11::buffer_protocol() annotation!"); 1257 1258 tinfo->get_buffer = get_buffer; 1259 tinfo->get_buffer_data = get_buffer_data; 1260 } 1261 1262 // rec_func must be set for either fget or fset. 1263 void def_property_static_impl(const char *name, 1264 handle fget, handle fset, 1265 detail::function_record *rec_func) { 1266 const auto is_static = (rec_func != nullptr) && !(rec_func->is_method && rec_func->scope); 1267 const auto has_doc = (rec_func != nullptr) && (rec_func->doc != nullptr) 1268 && pybind11::options::show_user_defined_docstrings(); 1269 auto property = handle((PyObject *) (is_static ? get_internals().static_property_type 1270 : &PyProperty_Type)); 1271 attr(name) = property(fget.ptr() ? fget : none(), 1272 fset.ptr() ? fset : none(), 1273 /*deleter*/none(), 1274 pybind11::str(has_doc ? rec_func->doc : "")); 1275 } 1276 }; 1277 1278 /// Set the pointer to operator new if it exists. The cast is needed because it can be overloaded. 1279 template <typename T, typename = void_t<decltype(static_cast<void *(*)(size_t)>(T::operator new))>> 1280 void set_operator_new(type_record *r) { r->operator_new = &T::operator new; } 1281 1282 template <typename> void set_operator_new(...) { } 1283 1284 template <typename T, typename SFINAE = void> struct has_operator_delete : std::false_type { }; 1285 template <typename T> struct has_operator_delete<T, void_t<decltype(static_cast<void (*)(void *)>(T::operator delete))>> 1286 : std::true_type { }; 1287 template <typename T, typename SFINAE = void> struct has_operator_delete_size : std::false_type { }; 1288 template <typename T> struct has_operator_delete_size<T, void_t<decltype(static_cast<void (*)(void *, size_t)>(T::operator delete))>> 1289 : std::true_type { }; 1290 /// Call class-specific delete if it exists or global otherwise. Can also be an overload set. 1291 template <typename T, enable_if_t<has_operator_delete<T>::value, int> = 0> 1292 void call_operator_delete(T *p, size_t, size_t) { T::operator delete(p); } 1293 template <typename T, enable_if_t<!has_operator_delete<T>::value && has_operator_delete_size<T>::value, int> = 0> 1294 void call_operator_delete(T *p, size_t s, size_t) { T::operator delete(p, s); } 1295 1296 inline void call_operator_delete(void *p, size_t s, size_t a) { 1297 (void)s; (void)a; 1298 #if defined(__cpp_aligned_new) && (!defined(_MSC_VER) || _MSC_VER >= 1912) 1299 if (a > __STDCPP_DEFAULT_NEW_ALIGNMENT__) { 1300 #ifdef __cpp_sized_deallocation 1301 ::operator delete(p, s, std::align_val_t(a)); 1302 #else 1303 ::operator delete(p, std::align_val_t(a)); 1304 #endif 1305 return; 1306 } 1307 #endif 1308 #ifdef __cpp_sized_deallocation 1309 ::operator delete(p, s); 1310 #else 1311 ::operator delete(p); 1312 #endif 1313 } 1314 1315 inline void add_class_method(object& cls, const char *name_, const cpp_function &cf) { 1316 cls.attr(cf.name()) = cf; 1317 if (std::strcmp(name_, "__eq__") == 0 && !cls.attr("__dict__").contains("__hash__")) { 1318 cls.attr("__hash__") = none(); 1319 } 1320 } 1321 1322 PYBIND11_NAMESPACE_END(detail) 1323 1324 /// Given a pointer to a member function, cast it to its `Derived` version. 1325 /// Forward everything else unchanged. 1326 template <typename /*Derived*/, typename F> 1327 auto method_adaptor(F &&f) -> decltype(std::forward<F>(f)) { return std::forward<F>(f); } 1328 1329 template <typename Derived, typename Return, typename Class, typename... Args> 1330 auto method_adaptor(Return (Class::*pmf)(Args...)) -> Return (Derived::*)(Args...) { 1331 static_assert(detail::is_accessible_base_of<Class, Derived>::value, 1332 "Cannot bind an inaccessible base class method; use a lambda definition instead"); 1333 return pmf; 1334 } 1335 1336 template <typename Derived, typename Return, typename Class, typename... Args> 1337 auto method_adaptor(Return (Class::*pmf)(Args...) const) -> Return (Derived::*)(Args...) const { 1338 static_assert(detail::is_accessible_base_of<Class, Derived>::value, 1339 "Cannot bind an inaccessible base class method; use a lambda definition instead"); 1340 return pmf; 1341 } 1342 1343 template <typename type_, typename... options> 1344 class class_ : public detail::generic_type { 1345 template <typename T> using is_holder = detail::is_holder_type<type_, T>; 1346 template <typename T> using is_subtype = detail::is_strict_base_of<type_, T>; 1347 template <typename T> using is_base = detail::is_strict_base_of<T, type_>; 1348 // struct instead of using here to help MSVC: 1349 template <typename T> struct is_valid_class_option : 1350 detail::any_of<is_holder<T>, is_subtype<T>, is_base<T>> {}; 1351 1352 public: 1353 using type = type_; 1354 using type_alias = detail::exactly_one_t<is_subtype, void, options...>; 1355 constexpr static bool has_alias = !std::is_void<type_alias>::value; 1356 using holder_type = detail::exactly_one_t<is_holder, std::unique_ptr<type>, options...>; 1357 1358 static_assert(detail::all_of<is_valid_class_option<options>...>::value, 1359 "Unknown/invalid class_ template parameters provided"); 1360 1361 static_assert(!has_alias || std::is_polymorphic<type>::value, 1362 "Cannot use an alias class with a non-polymorphic type"); 1363 1364 PYBIND11_OBJECT(class_, generic_type, PyType_Check) 1365 1366 template <typename... Extra> 1367 class_(handle scope, const char *name, const Extra &... extra) { 1368 using namespace detail; 1369 1370 // MI can only be specified via class_ template options, not constructor parameters 1371 static_assert( 1372 none_of<is_pyobject<Extra>...>::value || // no base class arguments, or: 1373 ( constexpr_sum(is_pyobject<Extra>::value...) == 1 && // Exactly one base 1374 constexpr_sum(is_base<options>::value...) == 0 && // no template option bases 1375 none_of<std::is_same<multiple_inheritance, Extra>...>::value), // no multiple_inheritance attr 1376 "Error: multiple inheritance bases must be specified via class_ template options"); 1377 1378 type_record record; 1379 record.scope = scope; 1380 record.name = name; 1381 record.type = &typeid(type); 1382 record.type_size = sizeof(conditional_t<has_alias, type_alias, type>); 1383 record.type_align = alignof(conditional_t<has_alias, type_alias, type>&); 1384 record.holder_size = sizeof(holder_type); 1385 record.init_instance = init_instance; 1386 record.dealloc = dealloc; 1387 record.default_holder = detail::is_instantiation<std::unique_ptr, holder_type>::value; 1388 1389 set_operator_new<type>(&record); 1390 1391 /* Register base classes specified via template arguments to class_, if any */ 1392 PYBIND11_EXPAND_SIDE_EFFECTS(add_base<options>(record)); 1393 1394 /* Process optional arguments, if any */ 1395 process_attributes<Extra...>::init(extra..., &record); 1396 1397 generic_type::initialize(record); 1398 1399 if (has_alias) { 1400 auto &instances = record.module_local ? get_local_internals().registered_types_cpp : get_internals().registered_types_cpp; 1401 instances[std::type_index(typeid(type_alias))] = instances[std::type_index(typeid(type))]; 1402 } 1403 } 1404 1405 template <typename Base, detail::enable_if_t<is_base<Base>::value, int> = 0> 1406 static void add_base(detail::type_record &rec) { 1407 rec.add_base(typeid(Base), [](void *src) -> void * { 1408 return static_cast<Base *>(reinterpret_cast<type *>(src)); 1409 }); 1410 } 1411 1412 template <typename Base, detail::enable_if_t<!is_base<Base>::value, int> = 0> 1413 static void add_base(detail::type_record &) { } 1414 1415 template <typename Func, typename... Extra> 1416 class_ &def(const char *name_, Func&& f, const Extra&... extra) { 1417 cpp_function cf(method_adaptor<type>(std::forward<Func>(f)), name(name_), is_method(*this), 1418 sibling(getattr(*this, name_, none())), extra...); 1419 add_class_method(*this, name_, cf); 1420 return *this; 1421 } 1422 1423 template <typename Func, typename... Extra> class_ & 1424 def_static(const char *name_, Func &&f, const Extra&... extra) { 1425 static_assert(!std::is_member_function_pointer<Func>::value, 1426 "def_static(...) called with a non-static member function pointer"); 1427 cpp_function cf(std::forward<Func>(f), name(name_), scope(*this), 1428 sibling(getattr(*this, name_, none())), extra...); 1429 attr(cf.name()) = staticmethod(cf); 1430 return *this; 1431 } 1432 1433 template <detail::op_id id, detail::op_type ot, typename L, typename R, typename... Extra> 1434 class_ &def(const detail::op_<id, ot, L, R> &op, const Extra&... extra) { 1435 op.execute(*this, extra...); 1436 return *this; 1437 } 1438 1439 template <detail::op_id id, detail::op_type ot, typename L, typename R, typename... Extra> 1440 class_ & def_cast(const detail::op_<id, ot, L, R> &op, const Extra&... extra) { 1441 op.execute_cast(*this, extra...); 1442 return *this; 1443 } 1444 1445 template <typename... Args, typename... Extra> 1446 class_ &def(const detail::initimpl::constructor<Args...> &init, const Extra&... extra) { 1447 PYBIND11_WORKAROUND_INCORRECT_MSVC_C4100(init); 1448 init.execute(*this, extra...); 1449 return *this; 1450 } 1451 1452 template <typename... Args, typename... Extra> 1453 class_ &def(const detail::initimpl::alias_constructor<Args...> &init, const Extra&... extra) { 1454 PYBIND11_WORKAROUND_INCORRECT_MSVC_C4100(init); 1455 init.execute(*this, extra...); 1456 return *this; 1457 } 1458 1459 template <typename... Args, typename... Extra> 1460 class_ &def(detail::initimpl::factory<Args...> &&init, const Extra&... extra) { 1461 std::move(init).execute(*this, extra...); 1462 return *this; 1463 } 1464 1465 template <typename... Args, typename... Extra> 1466 class_ &def(detail::initimpl::pickle_factory<Args...> &&pf, const Extra &...extra) { 1467 std::move(pf).execute(*this, extra...); 1468 return *this; 1469 } 1470 1471 template <typename Func> 1472 class_& def_buffer(Func &&func) { 1473 struct capture { Func func; }; 1474 auto *ptr = new capture { std::forward<Func>(func) }; 1475 install_buffer_funcs([](PyObject *obj, void *ptr) -> buffer_info* { 1476 detail::make_caster<type> caster; 1477 if (!caster.load(obj, false)) 1478 return nullptr; 1479 return new buffer_info(((capture *) ptr)->func(caster)); 1480 }, ptr); 1481 weakref(m_ptr, cpp_function([ptr](handle wr) { 1482 delete ptr; 1483 wr.dec_ref(); 1484 })).release(); 1485 return *this; 1486 } 1487 1488 template <typename Return, typename Class, typename... Args> 1489 class_ &def_buffer(Return (Class::*func)(Args...)) { 1490 return def_buffer([func] (type &obj) { return (obj.*func)(); }); 1491 } 1492 1493 template <typename Return, typename Class, typename... Args> 1494 class_ &def_buffer(Return (Class::*func)(Args...) const) { 1495 return def_buffer([func] (const type &obj) { return (obj.*func)(); }); 1496 } 1497 1498 template <typename C, typename D, typename... Extra> 1499 class_ &def_readwrite(const char *name, D C::*pm, const Extra&... extra) { 1500 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)"); 1501 cpp_function fget([pm](const type &c) -> const D &{ return c.*pm; }, is_method(*this)), 1502 fset([pm](type &c, const D &value) { c.*pm = value; }, is_method(*this)); 1503 def_property(name, fget, fset, return_value_policy::reference_internal, extra...); 1504 return *this; 1505 } 1506 1507 template <typename C, typename D, typename... Extra> 1508 class_ &def_readonly(const char *name, const D C::*pm, const Extra& ...extra) { 1509 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)"); 1510 cpp_function fget([pm](const type &c) -> const D &{ return c.*pm; }, is_method(*this)); 1511 def_property_readonly(name, fget, return_value_policy::reference_internal, extra...); 1512 return *this; 1513 } 1514 1515 template <typename D, typename... Extra> 1516 class_ &def_readwrite_static(const char *name, D *pm, const Extra& ...extra) { 1517 cpp_function fget([pm](const object &) -> const D & { return *pm; }, scope(*this)), 1518 fset([pm](const object &, const D &value) { *pm = value; }, scope(*this)); 1519 def_property_static(name, fget, fset, return_value_policy::reference, extra...); 1520 return *this; 1521 } 1522 1523 template <typename D, typename... Extra> 1524 class_ &def_readonly_static(const char *name, const D *pm, const Extra& ...extra) { 1525 cpp_function fget([pm](const object &) -> const D & { return *pm; }, scope(*this)); 1526 def_property_readonly_static(name, fget, return_value_policy::reference, extra...); 1527 return *this; 1528 } 1529 1530 /// Uses return_value_policy::reference_internal by default 1531 template <typename Getter, typename... Extra> 1532 class_ &def_property_readonly(const char *name, const Getter &fget, const Extra& ...extra) { 1533 return def_property_readonly(name, cpp_function(method_adaptor<type>(fget)), 1534 return_value_policy::reference_internal, extra...); 1535 } 1536 1537 /// Uses cpp_function's return_value_policy by default 1538 template <typename... Extra> 1539 class_ &def_property_readonly(const char *name, const cpp_function &fget, const Extra& ...extra) { 1540 return def_property(name, fget, nullptr, extra...); 1541 } 1542 1543 /// Uses return_value_policy::reference by default 1544 template <typename Getter, typename... Extra> 1545 class_ &def_property_readonly_static(const char *name, const Getter &fget, const Extra& ...extra) { 1546 return def_property_readonly_static(name, cpp_function(fget), return_value_policy::reference, extra...); 1547 } 1548 1549 /// Uses cpp_function's return_value_policy by default 1550 template <typename... Extra> 1551 class_ &def_property_readonly_static(const char *name, const cpp_function &fget, const Extra& ...extra) { 1552 return def_property_static(name, fget, nullptr, extra...); 1553 } 1554 1555 /// Uses return_value_policy::reference_internal by default 1556 template <typename Getter, typename Setter, typename... Extra> 1557 class_ &def_property(const char *name, const Getter &fget, const Setter &fset, const Extra& ...extra) { 1558 return def_property(name, fget, cpp_function(method_adaptor<type>(fset)), extra...); 1559 } 1560 template <typename Getter, typename... Extra> 1561 class_ &def_property(const char *name, const Getter &fget, const cpp_function &fset, const Extra& ...extra) { 1562 return def_property(name, cpp_function(method_adaptor<type>(fget)), fset, 1563 return_value_policy::reference_internal, extra...); 1564 } 1565 1566 /// Uses cpp_function's return_value_policy by default 1567 template <typename... Extra> 1568 class_ &def_property(const char *name, const cpp_function &fget, const cpp_function &fset, const Extra& ...extra) { 1569 return def_property_static(name, fget, fset, is_method(*this), extra...); 1570 } 1571 1572 /// Uses return_value_policy::reference by default 1573 template <typename Getter, typename... Extra> 1574 class_ &def_property_static(const char *name, const Getter &fget, const cpp_function &fset, const Extra& ...extra) { 1575 return def_property_static(name, cpp_function(fget), fset, return_value_policy::reference, extra...); 1576 } 1577 1578 /// Uses cpp_function's return_value_policy by default 1579 template <typename... Extra> 1580 class_ &def_property_static(const char *name, const cpp_function &fget, const cpp_function &fset, const Extra& ...extra) { 1581 static_assert( 0 == detail::constexpr_sum(std::is_base_of<arg, Extra>::value...), 1582 "Argument annotations are not allowed for properties"); 1583 auto rec_fget = get_function_record(fget), rec_fset = get_function_record(fset); 1584 auto *rec_active = rec_fget; 1585 if (rec_fget) { 1586 char *doc_prev = rec_fget->doc; /* 'extra' field may include a property-specific documentation string */ 1587 detail::process_attributes<Extra...>::init(extra..., rec_fget); 1588 if (rec_fget->doc && rec_fget->doc != doc_prev) { 1589 std::free(doc_prev); 1590 rec_fget->doc = PYBIND11_COMPAT_STRDUP(rec_fget->doc); 1591 } 1592 } 1593 if (rec_fset) { 1594 char *doc_prev = rec_fset->doc; 1595 detail::process_attributes<Extra...>::init(extra..., rec_fset); 1596 if (rec_fset->doc && rec_fset->doc != doc_prev) { 1597 std::free(doc_prev); 1598 rec_fset->doc = PYBIND11_COMPAT_STRDUP(rec_fset->doc); 1599 } 1600 if (! rec_active) rec_active = rec_fset; 1601 } 1602 def_property_static_impl(name, fget, fset, rec_active); 1603 return *this; 1604 } 1605 1606 private: 1607 /// Initialize holder object, variant 1: object derives from enable_shared_from_this 1608 template <typename T> 1609 static void init_holder(detail::instance *inst, detail::value_and_holder &v_h, 1610 const holder_type * /* unused */, const std::enable_shared_from_this<T> * /* dummy */) { 1611 1612 auto sh = std::dynamic_pointer_cast<typename holder_type::element_type>( 1613 detail::try_get_shared_from_this(v_h.value_ptr<type>())); 1614 if (sh) { 1615 new (std::addressof(v_h.holder<holder_type>())) holder_type(std::move(sh)); 1616 v_h.set_holder_constructed(); 1617 } 1618 1619 if (!v_h.holder_constructed() && inst->owned) { 1620 new (std::addressof(v_h.holder<holder_type>())) holder_type(v_h.value_ptr<type>()); 1621 v_h.set_holder_constructed(); 1622 } 1623 } 1624 1625 static void init_holder_from_existing(const detail::value_and_holder &v_h, 1626 const holder_type *holder_ptr, std::true_type /*is_copy_constructible*/) { 1627 new (std::addressof(v_h.holder<holder_type>())) holder_type(*reinterpret_cast<const holder_type *>(holder_ptr)); 1628 } 1629 1630 static void init_holder_from_existing(const detail::value_and_holder &v_h, 1631 const holder_type *holder_ptr, std::false_type /*is_copy_constructible*/) { 1632 new (std::addressof(v_h.holder<holder_type>())) holder_type(std::move(*const_cast<holder_type *>(holder_ptr))); 1633 } 1634 1635 /// Initialize holder object, variant 2: try to construct from existing holder object, if possible 1636 static void init_holder(detail::instance *inst, detail::value_and_holder &v_h, 1637 const holder_type *holder_ptr, const void * /* dummy -- not enable_shared_from_this<T>) */) { 1638 if (holder_ptr) { 1639 init_holder_from_existing(v_h, holder_ptr, std::is_copy_constructible<holder_type>()); 1640 v_h.set_holder_constructed(); 1641 } else if (inst->owned || detail::always_construct_holder<holder_type>::value) { 1642 new (std::addressof(v_h.holder<holder_type>())) holder_type(v_h.value_ptr<type>()); 1643 v_h.set_holder_constructed(); 1644 } 1645 } 1646 1647 /// Performs instance initialization including constructing a holder and registering the known 1648 /// instance. Should be called as soon as the `type` value_ptr is set for an instance. Takes an 1649 /// optional pointer to an existing holder to use; if not specified and the instance is 1650 /// `.owned`, a new holder will be constructed to manage the value pointer. 1651 static void init_instance(detail::instance *inst, const void *holder_ptr) { 1652 auto v_h = inst->get_value_and_holder(detail::get_type_info(typeid(type))); 1653 if (!v_h.instance_registered()) { 1654 register_instance(inst, v_h.value_ptr(), v_h.type); 1655 v_h.set_instance_registered(); 1656 } 1657 init_holder(inst, v_h, (const holder_type *) holder_ptr, v_h.value_ptr<type>()); 1658 } 1659 1660 /// Deallocates an instance; via holder, if constructed; otherwise via operator delete. 1661 static void dealloc(detail::value_and_holder &v_h) { 1662 // We could be deallocating because we are cleaning up after a Python exception. 1663 // If so, the Python error indicator will be set. We need to clear that before 1664 // running the destructor, in case the destructor code calls more Python. 1665 // If we don't, the Python API will exit with an exception, and pybind11 will 1666 // throw error_already_set from the C++ destructor which is forbidden and triggers 1667 // std::terminate(). 1668 error_scope scope; 1669 if (v_h.holder_constructed()) { 1670 v_h.holder<holder_type>().~holder_type(); 1671 v_h.set_holder_constructed(false); 1672 } 1673 else { 1674 detail::call_operator_delete(v_h.value_ptr<type>(), 1675 v_h.type->type_size, 1676 v_h.type->type_align 1677 ); 1678 } 1679 v_h.value_ptr() = nullptr; 1680 } 1681 1682 static detail::function_record *get_function_record(handle h) { 1683 h = detail::get_function(h); 1684 return h ? (detail::function_record *) reinterpret_borrow<capsule>(PyCFunction_GET_SELF(h.ptr())) 1685 : nullptr; 1686 } 1687 }; 1688 1689 /// Binds an existing constructor taking arguments Args... 1690 template <typename... Args> detail::initimpl::constructor<Args...> init() { return {}; } 1691 /// Like `init<Args...>()`, but the instance is always constructed through the alias class (even 1692 /// when not inheriting on the Python side). 1693 template <typename... Args> detail::initimpl::alias_constructor<Args...> init_alias() { return {}; } 1694 1695 /// Binds a factory function as a constructor 1696 template <typename Func, typename Ret = detail::initimpl::factory<Func>> 1697 Ret init(Func &&f) { return {std::forward<Func>(f)}; } 1698 1699 /// Dual-argument factory function: the first function is called when no alias is needed, the second 1700 /// when an alias is needed (i.e. due to python-side inheritance). Arguments must be identical. 1701 template <typename CFunc, typename AFunc, typename Ret = detail::initimpl::factory<CFunc, AFunc>> 1702 Ret init(CFunc &&c, AFunc &&a) { 1703 return {std::forward<CFunc>(c), std::forward<AFunc>(a)}; 1704 } 1705 1706 /// Binds pickling functions `__getstate__` and `__setstate__` and ensures that the type 1707 /// returned by `__getstate__` is the same as the argument accepted by `__setstate__`. 1708 template <typename GetState, typename SetState> 1709 detail::initimpl::pickle_factory<GetState, SetState> pickle(GetState &&g, SetState &&s) { 1710 return {std::forward<GetState>(g), std::forward<SetState>(s)}; 1711 } 1712 1713 PYBIND11_NAMESPACE_BEGIN(detail) 1714 1715 inline str enum_name(handle arg) { 1716 dict entries = arg.get_type().attr("__entries"); 1717 for (auto kv : entries) { 1718 if (handle(kv.second[int_(0)]).equal(arg)) 1719 return pybind11::str(kv.first); 1720 } 1721 return "???"; 1722 } 1723 1724 struct enum_base { 1725 enum_base(const handle &base, const handle &parent) : m_base(base), m_parent(parent) { } 1726 1727 PYBIND11_NOINLINE void init(bool is_arithmetic, bool is_convertible) { 1728 m_base.attr("__entries") = dict(); 1729 auto property = handle((PyObject *) &PyProperty_Type); 1730 auto static_property = handle((PyObject *) get_internals().static_property_type); 1731 1732 m_base.attr("__repr__") = cpp_function( 1733 [](const object &arg) -> str { 1734 handle type = type::handle_of(arg); 1735 object type_name = type.attr("__name__"); 1736 return pybind11::str("<{}.{}: {}>").format(type_name, enum_name(arg), int_(arg)); 1737 }, 1738 name("__repr__"), 1739 is_method(m_base)); 1740 1741 m_base.attr("name") = property(cpp_function(&enum_name, name("name"), is_method(m_base))); 1742 1743 m_base.attr("__str__") = cpp_function( 1744 [](handle arg) -> str { 1745 object type_name = type::handle_of(arg).attr("__name__"); 1746 return pybind11::str("{}.{}").format(type_name, enum_name(arg)); 1747 }, name("name"), is_method(m_base) 1748 ); 1749 1750 m_base.attr("__doc__") = static_property(cpp_function( 1751 [](handle arg) -> std::string { 1752 std::string docstring; 1753 dict entries = arg.attr("__entries"); 1754 if (((PyTypeObject *) arg.ptr())->tp_doc) 1755 docstring += std::string(((PyTypeObject *) arg.ptr())->tp_doc) + "\n\n"; 1756 docstring += "Members:"; 1757 for (auto kv : entries) { 1758 auto key = std::string(pybind11::str(kv.first)); 1759 auto comment = kv.second[int_(1)]; 1760 docstring += "\n\n " + key; 1761 if (!comment.is_none()) 1762 docstring += " : " + (std::string) pybind11::str(comment); 1763 } 1764 return docstring; 1765 }, name("__doc__") 1766 ), none(), none(), ""); 1767 1768 m_base.attr("__members__") = static_property(cpp_function( 1769 [](handle arg) -> dict { 1770 dict entries = arg.attr("__entries"), m; 1771 for (auto kv : entries) 1772 m[kv.first] = kv.second[int_(0)]; 1773 return m; 1774 }, name("__members__")), none(), none(), "" 1775 ); 1776 1777 #define PYBIND11_ENUM_OP_STRICT(op, expr, strict_behavior) \ 1778 m_base.attr(op) = cpp_function( \ 1779 [](const object &a, const object &b) { \ 1780 if (!type::handle_of(a).is(type::handle_of(b))) \ 1781 strict_behavior; /* NOLINT(bugprone-macro-parentheses) */ \ 1782 return expr; \ 1783 }, \ 1784 name(op), \ 1785 is_method(m_base), \ 1786 arg("other")) 1787 1788 #define PYBIND11_ENUM_OP_CONV(op, expr) \ 1789 m_base.attr(op) = cpp_function( \ 1790 [](const object &a_, const object &b_) { \ 1791 int_ a(a_), b(b_); \ 1792 return expr; \ 1793 }, \ 1794 name(op), \ 1795 is_method(m_base), \ 1796 arg("other")) 1797 1798 #define PYBIND11_ENUM_OP_CONV_LHS(op, expr) \ 1799 m_base.attr(op) = cpp_function( \ 1800 [](const object &a_, const object &b) { \ 1801 int_ a(a_); \ 1802 return expr; \ 1803 }, \ 1804 name(op), \ 1805 is_method(m_base), \ 1806 arg("other")) 1807 1808 if (is_convertible) { 1809 PYBIND11_ENUM_OP_CONV_LHS("__eq__", !b.is_none() && a.equal(b)); 1810 PYBIND11_ENUM_OP_CONV_LHS("__ne__", b.is_none() || !a.equal(b)); 1811 1812 if (is_arithmetic) { 1813 PYBIND11_ENUM_OP_CONV("__lt__", a < b); 1814 PYBIND11_ENUM_OP_CONV("__gt__", a > b); 1815 PYBIND11_ENUM_OP_CONV("__le__", a <= b); 1816 PYBIND11_ENUM_OP_CONV("__ge__", a >= b); 1817 PYBIND11_ENUM_OP_CONV("__and__", a & b); 1818 PYBIND11_ENUM_OP_CONV("__rand__", a & b); 1819 PYBIND11_ENUM_OP_CONV("__or__", a | b); 1820 PYBIND11_ENUM_OP_CONV("__ror__", a | b); 1821 PYBIND11_ENUM_OP_CONV("__xor__", a ^ b); 1822 PYBIND11_ENUM_OP_CONV("__rxor__", a ^ b); 1823 m_base.attr("__invert__") 1824 = cpp_function([](const object &arg) { return ~(int_(arg)); }, 1825 name("__invert__"), 1826 is_method(m_base)); 1827 } 1828 } else { 1829 PYBIND11_ENUM_OP_STRICT("__eq__", int_(a).equal(int_(b)), return false); 1830 PYBIND11_ENUM_OP_STRICT("__ne__", !int_(a).equal(int_(b)), return true); 1831 1832 if (is_arithmetic) { 1833 #define PYBIND11_THROW throw type_error("Expected an enumeration of matching type!"); 1834 PYBIND11_ENUM_OP_STRICT("__lt__", int_(a) < int_(b), PYBIND11_THROW); 1835 PYBIND11_ENUM_OP_STRICT("__gt__", int_(a) > int_(b), PYBIND11_THROW); 1836 PYBIND11_ENUM_OP_STRICT("__le__", int_(a) <= int_(b), PYBIND11_THROW); 1837 PYBIND11_ENUM_OP_STRICT("__ge__", int_(a) >= int_(b), PYBIND11_THROW); 1838 #undef PYBIND11_THROW 1839 } 1840 } 1841 1842 #undef PYBIND11_ENUM_OP_CONV_LHS 1843 #undef PYBIND11_ENUM_OP_CONV 1844 #undef PYBIND11_ENUM_OP_STRICT 1845 1846 m_base.attr("__getstate__") = cpp_function( 1847 [](const object &arg) { return int_(arg); }, name("__getstate__"), is_method(m_base)); 1848 1849 m_base.attr("__hash__") = cpp_function( 1850 [](const object &arg) { return int_(arg); }, name("__hash__"), is_method(m_base)); 1851 } 1852 1853 PYBIND11_NOINLINE void value(char const* name_, object value, const char *doc = nullptr) { 1854 dict entries = m_base.attr("__entries"); 1855 str name(name_); 1856 if (entries.contains(name)) { 1857 std::string type_name = (std::string) str(m_base.attr("__name__")); 1858 throw value_error(type_name + ": element \"" + std::string(name_) + "\" already exists!"); 1859 } 1860 1861 entries[name] = std::make_pair(value, doc); 1862 m_base.attr(name) = value; 1863 } 1864 1865 PYBIND11_NOINLINE void export_values() { 1866 dict entries = m_base.attr("__entries"); 1867 for (auto kv : entries) 1868 m_parent.attr(kv.first) = kv.second[int_(0)]; 1869 } 1870 1871 handle m_base; 1872 handle m_parent; 1873 }; 1874 1875 template <bool is_signed, size_t length> struct equivalent_integer {}; 1876 template <> struct equivalent_integer<true, 1> { using type = int8_t; }; 1877 template <> struct equivalent_integer<false, 1> { using type = uint8_t; }; 1878 template <> struct equivalent_integer<true, 2> { using type = int16_t; }; 1879 template <> struct equivalent_integer<false, 2> { using type = uint16_t; }; 1880 template <> struct equivalent_integer<true, 4> { using type = int32_t; }; 1881 template <> struct equivalent_integer<false, 4> { using type = uint32_t; }; 1882 template <> struct equivalent_integer<true, 8> { using type = int64_t; }; 1883 template <> struct equivalent_integer<false, 8> { using type = uint64_t; }; 1884 1885 template <typename IntLike> 1886 using equivalent_integer_t = typename equivalent_integer<std::is_signed<IntLike>::value, sizeof(IntLike)>::type; 1887 1888 PYBIND11_NAMESPACE_END(detail) 1889 1890 /// Binds C++ enumerations and enumeration classes to Python 1891 template <typename Type> class enum_ : public class_<Type> { 1892 public: 1893 using Base = class_<Type>; 1894 using Base::def; 1895 using Base::attr; 1896 using Base::def_property_readonly; 1897 using Base::def_property_readonly_static; 1898 using Underlying = typename std::underlying_type<Type>::type; 1899 // Scalar is the integer representation of underlying type 1900 using Scalar = detail::conditional_t<detail::any_of< 1901 detail::is_std_char_type<Underlying>, std::is_same<Underlying, bool> 1902 >::value, detail::equivalent_integer_t<Underlying>, Underlying>; 1903 1904 template <typename... Extra> 1905 enum_(const handle &scope, const char *name, const Extra&... extra) 1906 : class_<Type>(scope, name, extra...), m_base(*this, scope) { 1907 constexpr bool is_arithmetic = detail::any_of<std::is_same<arithmetic, Extra>...>::value; 1908 constexpr bool is_convertible = std::is_convertible<Type, Underlying>::value; 1909 m_base.init(is_arithmetic, is_convertible); 1910 1911 def(init([](Scalar i) { return static_cast<Type>(i); }), arg("value")); 1912 def_property_readonly("value", [](Type value) { return (Scalar) value; }); 1913 def("__int__", [](Type value) { return (Scalar) value; }); 1914 #if PY_MAJOR_VERSION < 3 1915 def("__long__", [](Type value) { return (Scalar) value; }); 1916 #endif 1917 #if PY_MAJOR_VERSION > 3 || (PY_MAJOR_VERSION == 3 && PY_MINOR_VERSION >= 8) 1918 def("__index__", [](Type value) { return (Scalar) value; }); 1919 #endif 1920 1921 attr("__setstate__") = cpp_function( 1922 [](detail::value_and_holder &v_h, Scalar arg) { 1923 detail::initimpl::setstate<Base>(v_h, static_cast<Type>(arg), 1924 Py_TYPE(v_h.inst) != v_h.type->type); }, 1925 detail::is_new_style_constructor(), 1926 pybind11::name("__setstate__"), is_method(*this), arg("state")); 1927 } 1928 1929 /// Export enumeration entries into the parent scope 1930 enum_& export_values() { 1931 m_base.export_values(); 1932 return *this; 1933 } 1934 1935 /// Add an enumeration entry 1936 enum_& value(char const* name, Type value, const char *doc = nullptr) { 1937 m_base.value(name, pybind11::cast(value, return_value_policy::copy), doc); 1938 return *this; 1939 } 1940 1941 private: 1942 detail::enum_base m_base; 1943 }; 1944 1945 PYBIND11_NAMESPACE_BEGIN(detail) 1946 1947 1948 PYBIND11_NOINLINE void keep_alive_impl(handle nurse, handle patient) { 1949 if (!nurse || !patient) 1950 pybind11_fail("Could not activate keep_alive!"); 1951 1952 if (patient.is_none() || nurse.is_none()) 1953 return; /* Nothing to keep alive or nothing to be kept alive by */ 1954 1955 auto tinfo = all_type_info(Py_TYPE(nurse.ptr())); 1956 if (!tinfo.empty()) { 1957 /* It's a pybind-registered type, so we can store the patient in the 1958 * internal list. */ 1959 add_patient(nurse.ptr(), patient.ptr()); 1960 } 1961 else { 1962 /* Fall back to clever approach based on weak references taken from 1963 * Boost.Python. This is not used for pybind-registered types because 1964 * the objects can be destroyed out-of-order in a GC pass. */ 1965 cpp_function disable_lifesupport( 1966 [patient](handle weakref) { patient.dec_ref(); weakref.dec_ref(); }); 1967 1968 weakref wr(nurse, disable_lifesupport); 1969 1970 patient.inc_ref(); /* reference patient and leak the weak reference */ 1971 (void) wr.release(); 1972 } 1973 } 1974 1975 PYBIND11_NOINLINE void keep_alive_impl(size_t Nurse, size_t Patient, function_call &call, handle ret) { 1976 auto get_arg = [&](size_t n) { 1977 if (n == 0) 1978 return ret; 1979 if (n == 1 && call.init_self) 1980 return call.init_self; 1981 if (n <= call.args.size()) 1982 return call.args[n - 1]; 1983 return handle(); 1984 }; 1985 1986 keep_alive_impl(get_arg(Nurse), get_arg(Patient)); 1987 } 1988 1989 inline std::pair<decltype(internals::registered_types_py)::iterator, bool> all_type_info_get_cache(PyTypeObject *type) { 1990 auto res = get_internals().registered_types_py 1991 #ifdef __cpp_lib_unordered_map_try_emplace 1992 .try_emplace(type); 1993 #else 1994 .emplace(type, std::vector<detail::type_info *>()); 1995 #endif 1996 if (res.second) { 1997 // New cache entry created; set up a weak reference to automatically remove it if the type 1998 // gets destroyed: 1999 weakref((PyObject *) type, cpp_function([type](handle wr) { 2000 get_internals().registered_types_py.erase(type); 2001 2002 // TODO consolidate the erasure code in pybind11_meta_dealloc() in class.h 2003 auto &cache = get_internals().inactive_override_cache; 2004 for (auto it = cache.begin(), last = cache.end(); it != last; ) { 2005 if (it->first == reinterpret_cast<PyObject *>(type)) 2006 it = cache.erase(it); 2007 else 2008 ++it; 2009 } 2010 2011 wr.dec_ref(); 2012 })).release(); 2013 } 2014 2015 return res; 2016 } 2017 2018 /* There are a large number of apparently unused template arguments because 2019 * each combination requires a separate py::class_ registration. 2020 */ 2021 template <typename Access, return_value_policy Policy, typename Iterator, typename Sentinel, typename ValueType, typename... Extra> 2022 struct iterator_state { 2023 Iterator it; 2024 Sentinel end; 2025 bool first_or_done; 2026 }; 2027 2028 // Note: these helpers take the iterator by non-const reference because some 2029 // iterators in the wild can't be dereferenced when const. The & after Iterator 2030 // is required for MSVC < 16.9. SFINAE cannot be reused for result_type due to 2031 // bugs in ICC, NVCC, and PGI compilers. See PR #3293. 2032 template <typename Iterator, typename SFINAE = decltype(*std::declval<Iterator &>())> 2033 struct iterator_access { 2034 using result_type = decltype(*std::declval<Iterator &>()); 2035 // NOLINTNEXTLINE(readability-const-return-type) // PR #3263 2036 result_type operator()(Iterator &it) const { 2037 return *it; 2038 } 2039 }; 2040 2041 template <typename Iterator, typename SFINAE = decltype((*std::declval<Iterator &>()).first) > 2042 class iterator_key_access { 2043 private: 2044 using pair_type = decltype(*std::declval<Iterator &>()); 2045 2046 public: 2047 /* If either the pair itself or the element of the pair is a reference, we 2048 * want to return a reference, otherwise a value. When the decltype 2049 * expression is parenthesized it is based on the value category of the 2050 * expression; otherwise it is the declared type of the pair member. 2051 * The use of declval<pair_type> in the second branch rather than directly 2052 * using *std::declval<Iterator &>() is a workaround for nvcc 2053 * (it's not used in the first branch because going via decltype and back 2054 * through declval does not perfectly preserve references). 2055 */ 2056 using result_type = conditional_t< 2057 std::is_reference<decltype(*std::declval<Iterator &>())>::value, 2058 decltype(((*std::declval<Iterator &>()).first)), 2059 decltype(std::declval<pair_type>().first) 2060 >; 2061 result_type operator()(Iterator &it) const { 2062 return (*it).first; 2063 } 2064 }; 2065 2066 template <typename Iterator, typename SFINAE = decltype((*std::declval<Iterator &>()).second)> 2067 class iterator_value_access { 2068 private: 2069 using pair_type = decltype(*std::declval<Iterator &>()); 2070 2071 public: 2072 using result_type = conditional_t< 2073 std::is_reference<decltype(*std::declval<Iterator &>())>::value, 2074 decltype(((*std::declval<Iterator &>()).second)), 2075 decltype(std::declval<pair_type>().second) 2076 >; 2077 result_type operator()(Iterator &it) const { 2078 return (*it).second; 2079 } 2080 }; 2081 2082 template <typename Access, 2083 return_value_policy Policy, 2084 typename Iterator, 2085 typename Sentinel, 2086 typename ValueType, 2087 typename... Extra> 2088 iterator make_iterator_impl(Iterator first, Sentinel last, Extra &&... extra) { 2089 using state = detail::iterator_state<Access, Policy, Iterator, Sentinel, ValueType, Extra...>; 2090 // TODO: state captures only the types of Extra, not the values 2091 2092 if (!detail::get_type_info(typeid(state), false)) { 2093 class_<state>(handle(), "iterator", pybind11::module_local()) 2094 .def("__iter__", [](state &s) -> state& { return s; }) 2095 .def("__next__", [](state &s) -> ValueType { 2096 if (!s.first_or_done) 2097 ++s.it; 2098 else 2099 s.first_or_done = false; 2100 if (s.it == s.end) { 2101 s.first_or_done = true; 2102 throw stop_iteration(); 2103 } 2104 return Access()(s.it); 2105 // NOLINTNEXTLINE(readability-const-return-type) // PR #3263 2106 }, std::forward<Extra>(extra)..., Policy); 2107 } 2108 2109 return cast(state{first, last, true}); 2110 } 2111 2112 PYBIND11_NAMESPACE_END(detail) 2113 2114 /// Makes a python iterator from a first and past-the-end C++ InputIterator. 2115 template <return_value_policy Policy = return_value_policy::reference_internal, 2116 typename Iterator, 2117 typename Sentinel, 2118 typename ValueType = typename detail::iterator_access<Iterator>::result_type, 2119 typename... Extra> 2120 iterator make_iterator(Iterator first, Sentinel last, Extra &&... extra) { 2121 return detail::make_iterator_impl< 2122 detail::iterator_access<Iterator>, 2123 Policy, 2124 Iterator, 2125 Sentinel, 2126 ValueType, 2127 Extra...>(first, last, std::forward<Extra>(extra)...); 2128 } 2129 2130 /// Makes a python iterator over the keys (`.first`) of a iterator over pairs from a 2131 /// first and past-the-end InputIterator. 2132 template <return_value_policy Policy = return_value_policy::reference_internal, 2133 typename Iterator, 2134 typename Sentinel, 2135 typename KeyType = typename detail::iterator_key_access<Iterator>::result_type, 2136 typename... Extra> 2137 iterator make_key_iterator(Iterator first, Sentinel last, Extra &&...extra) { 2138 return detail::make_iterator_impl< 2139 detail::iterator_key_access<Iterator>, 2140 Policy, 2141 Iterator, 2142 Sentinel, 2143 KeyType, 2144 Extra...>(first, last, std::forward<Extra>(extra)...); 2145 } 2146 2147 /// Makes a python iterator over the values (`.second`) of a iterator over pairs from a 2148 /// first and past-the-end InputIterator. 2149 template <return_value_policy Policy = return_value_policy::reference_internal, 2150 typename Iterator, 2151 typename Sentinel, 2152 typename ValueType = typename detail::iterator_value_access<Iterator>::result_type, 2153 typename... Extra> 2154 iterator make_value_iterator(Iterator first, Sentinel last, Extra &&...extra) { 2155 return detail::make_iterator_impl< 2156 detail::iterator_value_access<Iterator>, 2157 Policy, Iterator, 2158 Sentinel, 2159 ValueType, 2160 Extra...>(first, last, std::forward<Extra>(extra)...); 2161 } 2162 2163 /// Makes an iterator over values of an stl container or other container supporting 2164 /// `std::begin()`/`std::end()` 2165 template <return_value_policy Policy = return_value_policy::reference_internal, 2166 typename Type, typename... Extra> iterator make_iterator(Type &value, Extra&&... extra) { 2167 return make_iterator<Policy>(std::begin(value), std::end(value), extra...); 2168 } 2169 2170 /// Makes an iterator over the keys (`.first`) of a stl map-like container supporting 2171 /// `std::begin()`/`std::end()` 2172 template <return_value_policy Policy = return_value_policy::reference_internal, 2173 typename Type, typename... Extra> iterator make_key_iterator(Type &value, Extra&&... extra) { 2174 return make_key_iterator<Policy>(std::begin(value), std::end(value), extra...); 2175 } 2176 2177 /// Makes an iterator over the values (`.second`) of a stl map-like container supporting 2178 /// `std::begin()`/`std::end()` 2179 template <return_value_policy Policy = return_value_policy::reference_internal, 2180 typename Type, typename... Extra> iterator make_value_iterator(Type &value, Extra&&... extra) { 2181 return make_value_iterator<Policy>(std::begin(value), std::end(value), extra...); 2182 } 2183 2184 template <typename InputType, typename OutputType> void implicitly_convertible() { 2185 struct set_flag { 2186 bool &flag; 2187 explicit set_flag(bool &flag_) : flag(flag_) { flag_ = true; } 2188 ~set_flag() { flag = false; } 2189 }; 2190 auto implicit_caster = [](PyObject *obj, PyTypeObject *type) -> PyObject * { 2191 static bool currently_used = false; 2192 if (currently_used) // implicit conversions are non-reentrant 2193 return nullptr; 2194 set_flag flag_helper(currently_used); 2195 if (!detail::make_caster<InputType>().load(obj, false)) 2196 return nullptr; 2197 tuple args(1); 2198 args[0] = obj; 2199 PyObject *result = PyObject_Call((PyObject *) type, args.ptr(), nullptr); 2200 if (result == nullptr) 2201 PyErr_Clear(); 2202 return result; 2203 }; 2204 2205 if (auto tinfo = detail::get_type_info(typeid(OutputType))) 2206 tinfo->implicit_conversions.push_back(implicit_caster); 2207 else 2208 pybind11_fail("implicitly_convertible: Unable to find type " + type_id<OutputType>()); 2209 } 2210 2211 2212 inline void register_exception_translator(ExceptionTranslator &&translator) { 2213 detail::get_internals().registered_exception_translators.push_front( 2214 std::forward<ExceptionTranslator>(translator)); 2215 } 2216 2217 2218 /** 2219 * Add a new module-local exception translator. Locally registered functions 2220 * will be tried before any globally registered exception translators, which 2221 * will only be invoked if the module-local handlers do not deal with 2222 * the exception. 2223 */ 2224 inline void register_local_exception_translator(ExceptionTranslator &&translator) { 2225 detail::get_local_internals().registered_exception_translators.push_front( 2226 std::forward<ExceptionTranslator>(translator)); 2227 } 2228 2229 /** 2230 * Wrapper to generate a new Python exception type. 2231 * 2232 * This should only be used with PyErr_SetString for now. 2233 * It is not (yet) possible to use as a py::base. 2234 * Template type argument is reserved for future use. 2235 */ 2236 template <typename type> 2237 class exception : public object { 2238 public: 2239 exception() = default; 2240 exception(handle scope, const char *name, handle base = PyExc_Exception) { 2241 std::string full_name = scope.attr("__name__").cast<std::string>() + 2242 std::string(".") + name; 2243 m_ptr = PyErr_NewException(const_cast<char *>(full_name.c_str()), base.ptr(), NULL); 2244 if (hasattr(scope, "__dict__") && scope.attr("__dict__").contains(name)) 2245 pybind11_fail("Error during initialization: multiple incompatible " 2246 "definitions with name \"" + std::string(name) + "\""); 2247 scope.attr(name) = *this; 2248 } 2249 2250 // Sets the current python exception to this exception object with the given message 2251 void operator()(const char *message) { 2252 PyErr_SetString(m_ptr, message); 2253 } 2254 }; 2255 2256 PYBIND11_NAMESPACE_BEGIN(detail) 2257 // Returns a reference to a function-local static exception object used in the simple 2258 // register_exception approach below. (It would be simpler to have the static local variable 2259 // directly in register_exception, but that makes clang <3.5 segfault - issue #1349). 2260 template <typename CppException> 2261 exception<CppException> &get_exception_object() { static exception<CppException> ex; return ex; } 2262 2263 // Helper function for register_exception and register_local_exception 2264 template <typename CppException> 2265 exception<CppException> ®ister_exception_impl(handle scope, 2266 const char *name, 2267 handle base, 2268 bool isLocal) { 2269 auto &ex = detail::get_exception_object<CppException>(); 2270 if (!ex) ex = exception<CppException>(scope, name, base); 2271 2272 auto register_func = isLocal ? ®ister_local_exception_translator 2273 : ®ister_exception_translator; 2274 2275 register_func([](std::exception_ptr p) { 2276 if (!p) return; 2277 try { 2278 std::rethrow_exception(p); 2279 } catch (const CppException &e) { 2280 detail::get_exception_object<CppException>()(e.what()); 2281 } 2282 }); 2283 return ex; 2284 } 2285 2286 PYBIND11_NAMESPACE_END(detail) 2287 2288 /** 2289 * Registers a Python exception in `m` of the given `name` and installs a translator to 2290 * translate the C++ exception to the created Python exception using the what() method. 2291 * This is intended for simple exception translations; for more complex translation, register the 2292 * exception object and translator directly. 2293 */ 2294 template <typename CppException> 2295 exception<CppException> ®ister_exception(handle scope, 2296 const char *name, 2297 handle base = PyExc_Exception) { 2298 return detail::register_exception_impl<CppException>(scope, name, base, false /* isLocal */); 2299 } 2300 2301 /** 2302 * Registers a Python exception in `m` of the given `name` and installs a translator to 2303 * translate the C++ exception to the created Python exception using the what() method. 2304 * This translator will only be used for exceptions that are thrown in this module and will be 2305 * tried before global exception translators, including those registered with register_exception. 2306 * This is intended for simple exception translations; for more complex translation, register the 2307 * exception object and translator directly. 2308 */ 2309 template <typename CppException> 2310 exception<CppException> ®ister_local_exception(handle scope, 2311 const char *name, 2312 handle base = PyExc_Exception) { 2313 return detail::register_exception_impl<CppException>(scope, name, base, true /* isLocal */); 2314 } 2315 2316 PYBIND11_NAMESPACE_BEGIN(detail) 2317 PYBIND11_NOINLINE void print(const tuple &args, const dict &kwargs) { 2318 auto strings = tuple(args.size()); 2319 for (size_t i = 0; i < args.size(); ++i) { 2320 strings[i] = str(args[i]); 2321 } 2322 auto sep = kwargs.contains("sep") ? kwargs["sep"] : cast(" "); 2323 auto line = sep.attr("join")(strings); 2324 2325 object file; 2326 if (kwargs.contains("file")) { 2327 file = kwargs["file"].cast<object>(); 2328 } else { 2329 try { 2330 file = module_::import("sys").attr("stdout"); 2331 } catch (const error_already_set &) { 2332 /* If print() is called from code that is executed as 2333 part of garbage collection during interpreter shutdown, 2334 importing 'sys' can fail. Give up rather than crashing the 2335 interpreter in this case. */ 2336 return; 2337 } 2338 } 2339 2340 auto write = file.attr("write"); 2341 write(line); 2342 write(kwargs.contains("end") ? kwargs["end"] : cast("\n")); 2343 2344 if (kwargs.contains("flush") && kwargs["flush"].cast<bool>()) 2345 file.attr("flush")(); 2346 } 2347 PYBIND11_NAMESPACE_END(detail) 2348 2349 template <return_value_policy policy = return_value_policy::automatic_reference, typename... Args> 2350 void print(Args &&...args) { 2351 auto c = detail::collect_arguments<policy>(std::forward<Args>(args)...); 2352 detail::print(c.args(), c.kwargs()); 2353 } 2354 2355 error_already_set::~error_already_set() { 2356 if (m_type) { 2357 gil_scoped_acquire gil; 2358 error_scope scope; 2359 m_type.release().dec_ref(); 2360 m_value.release().dec_ref(); 2361 m_trace.release().dec_ref(); 2362 } 2363 } 2364 2365 PYBIND11_NAMESPACE_BEGIN(detail) 2366 inline function get_type_override(const void *this_ptr, const type_info *this_type, const char *name) { 2367 handle self = get_object_handle(this_ptr, this_type); 2368 if (!self) 2369 return function(); 2370 handle type = type::handle_of(self); 2371 auto key = std::make_pair(type.ptr(), name); 2372 2373 /* Cache functions that aren't overridden in Python to avoid 2374 many costly Python dictionary lookups below */ 2375 auto &cache = get_internals().inactive_override_cache; 2376 if (cache.find(key) != cache.end()) 2377 return function(); 2378 2379 function override = getattr(self, name, function()); 2380 if (override.is_cpp_function()) { 2381 cache.insert(key); 2382 return function(); 2383 } 2384 2385 /* Don't call dispatch code if invoked from overridden function. 2386 Unfortunately this doesn't work on PyPy. */ 2387 #if !defined(PYPY_VERSION) && PY_VERSION_HEX < 0x030B0000 2388 // TODO: Remove PyPy workaround for Python 3.11. 2389 // Current API fails on 3.11 since co_varnames can be null. 2390 #if PY_VERSION_HEX >= 0x03090000 2391 PyFrameObject *frame = PyThreadState_GetFrame(PyThreadState_Get()); 2392 if (frame != nullptr) { 2393 PyCodeObject *f_code = PyFrame_GetCode(frame); 2394 // f_code is guaranteed to not be NULL 2395 if ((std::string) str(f_code->co_name) == name && f_code->co_argcount > 0) { 2396 PyObject* locals = PyEval_GetLocals(); 2397 if (locals != nullptr && f_code->co_varnames != nullptr) { 2398 PyObject *self_caller = dict_getitem( 2399 locals, PyTuple_GET_ITEM(f_code->co_varnames, 0) 2400 ); 2401 if (self_caller == self.ptr()) { 2402 Py_DECREF(f_code); 2403 Py_DECREF(frame); 2404 return function(); 2405 } 2406 } 2407 } 2408 Py_DECREF(f_code); 2409 Py_DECREF(frame); 2410 } 2411 #else 2412 PyFrameObject *frame = PyThreadState_Get()->frame; 2413 if (frame != nullptr && (std::string) str(frame->f_code->co_name) == name 2414 && frame->f_code->co_argcount > 0) { 2415 PyFrame_FastToLocals(frame); 2416 PyObject *self_caller = dict_getitem( 2417 frame->f_locals, PyTuple_GET_ITEM(frame->f_code->co_varnames, 0)); 2418 if (self_caller == self.ptr()) 2419 return function(); 2420 } 2421 #endif 2422 2423 #else 2424 /* PyPy currently doesn't provide a detailed cpyext emulation of 2425 frame objects, so we have to emulate this using Python. This 2426 is going to be slow..*/ 2427 dict d; d["self"] = self; d["name"] = pybind11::str(name); 2428 PyObject *result = PyRun_String( 2429 "import inspect\n" 2430 "frame = inspect.currentframe()\n" 2431 "if frame is not None:\n" 2432 " frame = frame.f_back\n" 2433 " if frame is not None and str(frame.f_code.co_name) == name and " 2434 "frame.f_code.co_argcount > 0:\n" 2435 " self_caller = frame.f_locals[frame.f_code.co_varnames[0]]\n" 2436 " if self_caller == self:\n" 2437 " self = None\n", 2438 Py_file_input, d.ptr(), d.ptr()); 2439 if (result == nullptr) 2440 throw error_already_set(); 2441 if (d["self"].is_none()) 2442 return function(); 2443 Py_DECREF(result); 2444 #endif 2445 2446 return override; 2447 } 2448 PYBIND11_NAMESPACE_END(detail) 2449 2450 /** \rst 2451 Try to retrieve a python method by the provided name from the instance pointed to by the this_ptr. 2452 2453 :this_ptr: The pointer to the object the overridden method should be retrieved for. This should be 2454 the first non-trampoline class encountered in the inheritance chain. 2455 :name: The name of the overridden Python method to retrieve. 2456 :return: The Python method by this name from the object or an empty function wrapper. 2457 \endrst */ 2458 template <class T> function get_override(const T *this_ptr, const char *name) { 2459 auto tinfo = detail::get_type_info(typeid(T)); 2460 return tinfo ? detail::get_type_override(this_ptr, tinfo, name) : function(); 2461 } 2462 2463 #define PYBIND11_OVERRIDE_IMPL(ret_type, cname, name, ...) \ 2464 do { \ 2465 pybind11::gil_scoped_acquire gil; \ 2466 pybind11::function override \ 2467 = pybind11::get_override(static_cast<const cname *>(this), name); \ 2468 if (override) { \ 2469 auto o = override(__VA_ARGS__); \ 2470 if (pybind11::detail::cast_is_temporary_value_reference<ret_type>::value) { \ 2471 static pybind11::detail::override_caster_t<ret_type> caster; \ 2472 return pybind11::detail::cast_ref<ret_type>(std::move(o), caster); \ 2473 } \ 2474 return pybind11::detail::cast_safe<ret_type>(std::move(o)); \ 2475 } \ 2476 } while (false) 2477 2478 /** \rst 2479 Macro to populate the virtual method in the trampoline class. This macro tries to look up a method named 'fn' 2480 from the Python side, deals with the :ref:`gil` and necessary argument conversions to call this method and return 2481 the appropriate type. See :ref:`overriding_virtuals` for more information. This macro should be used when the method 2482 name in C is not the same as the method name in Python. For example with `__str__`. 2483 2484 .. code-block:: cpp 2485 2486 std::string toString() override { 2487 PYBIND11_OVERRIDE_NAME( 2488 std::string, // Return type (ret_type) 2489 Animal, // Parent class (cname) 2490 "__str__", // Name of method in Python (name) 2491 toString, // Name of function in C++ (fn) 2492 ); 2493 } 2494 \endrst */ 2495 #define PYBIND11_OVERRIDE_NAME(ret_type, cname, name, fn, ...) \ 2496 do { \ 2497 PYBIND11_OVERRIDE_IMPL(PYBIND11_TYPE(ret_type), PYBIND11_TYPE(cname), name, __VA_ARGS__); \ 2498 return cname::fn(__VA_ARGS__); \ 2499 } while (false) 2500 2501 /** \rst 2502 Macro for pure virtual functions, this function is identical to :c:macro:`PYBIND11_OVERRIDE_NAME`, except that it 2503 throws if no override can be found. 2504 \endrst */ 2505 #define PYBIND11_OVERRIDE_PURE_NAME(ret_type, cname, name, fn, ...) \ 2506 do { \ 2507 PYBIND11_OVERRIDE_IMPL(PYBIND11_TYPE(ret_type), PYBIND11_TYPE(cname), name, __VA_ARGS__); \ 2508 pybind11::pybind11_fail("Tried to call pure virtual function \"" PYBIND11_STRINGIFY(cname) "::" name "\""); \ 2509 } while (false) 2510 2511 /** \rst 2512 Macro to populate the virtual method in the trampoline class. This macro tries to look up the method 2513 from the Python side, deals with the :ref:`gil` and necessary argument conversions to call this method and return 2514 the appropriate type. This macro should be used if the method name in C and in Python are identical. 2515 See :ref:`overriding_virtuals` for more information. 2516 2517 .. code-block:: cpp 2518 2519 class PyAnimal : public Animal { 2520 public: 2521 // Inherit the constructors 2522 using Animal::Animal; 2523 2524 // Trampoline (need one for each virtual function) 2525 std::string go(int n_times) override { 2526 PYBIND11_OVERRIDE_PURE( 2527 std::string, // Return type (ret_type) 2528 Animal, // Parent class (cname) 2529 go, // Name of function in C++ (must match Python name) (fn) 2530 n_times // Argument(s) (...) 2531 ); 2532 } 2533 }; 2534 \endrst */ 2535 #define PYBIND11_OVERRIDE(ret_type, cname, fn, ...) \ 2536 PYBIND11_OVERRIDE_NAME(PYBIND11_TYPE(ret_type), PYBIND11_TYPE(cname), #fn, fn, __VA_ARGS__) 2537 2538 /** \rst 2539 Macro for pure virtual functions, this function is identical to :c:macro:`PYBIND11_OVERRIDE`, except that it throws 2540 if no override can be found. 2541 \endrst */ 2542 #define PYBIND11_OVERRIDE_PURE(ret_type, cname, fn, ...) \ 2543 PYBIND11_OVERRIDE_PURE_NAME(PYBIND11_TYPE(ret_type), PYBIND11_TYPE(cname), #fn, fn, __VA_ARGS__) 2544 2545 2546 // Deprecated versions 2547 2548 PYBIND11_DEPRECATED("get_type_overload has been deprecated") 2549 inline function get_type_overload(const void *this_ptr, const detail::type_info *this_type, const char *name) { 2550 return detail::get_type_override(this_ptr, this_type, name); 2551 } 2552 2553 template <class T> 2554 inline function get_overload(const T *this_ptr, const char *name) { 2555 return get_override(this_ptr, name); 2556 } 2557 2558 #define PYBIND11_OVERLOAD_INT(ret_type, cname, name, ...) \ 2559 PYBIND11_OVERRIDE_IMPL(PYBIND11_TYPE(ret_type), PYBIND11_TYPE(cname), name, __VA_ARGS__) 2560 #define PYBIND11_OVERLOAD_NAME(ret_type, cname, name, fn, ...) \ 2561 PYBIND11_OVERRIDE_NAME(PYBIND11_TYPE(ret_type), PYBIND11_TYPE(cname), name, fn, __VA_ARGS__) 2562 #define PYBIND11_OVERLOAD_PURE_NAME(ret_type, cname, name, fn, ...) \ 2563 PYBIND11_OVERRIDE_PURE_NAME(PYBIND11_TYPE(ret_type), PYBIND11_TYPE(cname), name, fn, __VA_ARGS__); 2564 #define PYBIND11_OVERLOAD(ret_type, cname, fn, ...) \ 2565 PYBIND11_OVERRIDE(PYBIND11_TYPE(ret_type), PYBIND11_TYPE(cname), fn, __VA_ARGS__) 2566 #define PYBIND11_OVERLOAD_PURE(ret_type, cname, fn, ...) \ 2567 PYBIND11_OVERRIDE_PURE(PYBIND11_TYPE(ret_type), PYBIND11_TYPE(cname), fn, __VA_ARGS__); 2568 2569 PYBIND11_NAMESPACE_END(PYBIND11_NAMESPACE) 2570 2571 #if defined(__GNUC__) && __GNUC__ == 7 2572 # pragma GCC diagnostic pop // -Wnoexcept-type 2573 #endif