File indexing completed on 2025-02-16 05:12:14
0001 /* 0002 pybind11/attr.h: Infrastructure for processing custom 0003 type and function attributes 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 "cast.h" 0014 0015 #include <functional> 0016 0017 PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE) 0018 0019 /// \addtogroup annotations 0020 /// @{ 0021 0022 /// Annotation for methods 0023 struct is_method { handle class_; 0024 explicit is_method(const handle &c) : class_(c) {} 0025 }; 0026 0027 /// Annotation for operators 0028 struct is_operator { }; 0029 0030 /// Annotation for classes that cannot be subclassed 0031 struct is_final { }; 0032 0033 /// Annotation for parent scope 0034 struct scope { handle value; 0035 explicit scope(const handle &s) : value(s) {} 0036 }; 0037 0038 /// Annotation for documentation 0039 struct doc { const char *value; 0040 explicit doc(const char *value) : value(value) {} 0041 }; 0042 0043 /// Annotation for function names 0044 struct name { const char *value; 0045 explicit name(const char *value) : value(value) {} 0046 }; 0047 0048 /// Annotation indicating that a function is an overload associated with a given "sibling" 0049 struct sibling { handle value; 0050 explicit sibling(const handle &value) : value(value.ptr()) {} 0051 }; 0052 0053 /// Annotation indicating that a class derives from another given type 0054 template <typename T> struct base { 0055 0056 PYBIND11_DEPRECATED("base<T>() was deprecated in favor of specifying 'T' as a template argument to class_") 0057 base() { } // NOLINT(modernize-use-equals-default): breaks MSVC 2015 when adding an attribute 0058 }; 0059 0060 /// Keep patient alive while nurse lives 0061 template <size_t Nurse, size_t Patient> struct keep_alive { }; 0062 0063 /// Annotation indicating that a class is involved in a multiple inheritance relationship 0064 struct multiple_inheritance { }; 0065 0066 /// Annotation which enables dynamic attributes, i.e. adds `__dict__` to a class 0067 struct dynamic_attr { }; 0068 0069 /// Annotation which enables the buffer protocol for a type 0070 struct buffer_protocol { }; 0071 0072 /// Annotation which requests that a special metaclass is created for a type 0073 struct metaclass { 0074 handle value; 0075 0076 PYBIND11_DEPRECATED("py::metaclass() is no longer required. It's turned on by default now.") 0077 // NOLINTNEXTLINE(modernize-use-equals-default): breaks MSVC 2015 when adding an attribute 0078 metaclass() {} 0079 0080 /// Override pybind11's default metaclass 0081 explicit metaclass(handle value) : value(value) { } 0082 }; 0083 0084 /// Specifies a custom callback with signature `void (PyHeapTypeObject*)` that 0085 /// may be used to customize the Python type. 0086 /// 0087 /// The callback is invoked immediately before `PyType_Ready`. 0088 /// 0089 /// Note: This is an advanced interface, and uses of it may require changes to 0090 /// work with later versions of pybind11. You may wish to consult the 0091 /// implementation of `make_new_python_type` in `detail/classes.h` to understand 0092 /// the context in which the callback will be run. 0093 struct custom_type_setup { 0094 using callback = std::function<void(PyHeapTypeObject *heap_type)>; 0095 0096 explicit custom_type_setup(callback value) : value(std::move(value)) {} 0097 0098 callback value; 0099 }; 0100 0101 /// Annotation that marks a class as local to the module: 0102 struct module_local { const bool value; 0103 constexpr explicit module_local(bool v = true) : value(v) {} 0104 }; 0105 0106 /// Annotation to mark enums as an arithmetic type 0107 struct arithmetic { }; 0108 0109 /// Mark a function for addition at the beginning of the existing overload chain instead of the end 0110 struct prepend { }; 0111 0112 /** \rst 0113 A call policy which places one or more guard variables (``Ts...``) around the function call. 0114 0115 For example, this definition: 0116 0117 .. code-block:: cpp 0118 0119 m.def("foo", foo, py::call_guard<T>()); 0120 0121 is equivalent to the following pseudocode: 0122 0123 .. code-block:: cpp 0124 0125 m.def("foo", [](args...) { 0126 T scope_guard; 0127 return foo(args...); // forwarded arguments 0128 }); 0129 \endrst */ 0130 template <typename... Ts> struct call_guard; 0131 0132 template <> struct call_guard<> { using type = detail::void_type; }; 0133 0134 template <typename T> 0135 struct call_guard<T> { 0136 static_assert(std::is_default_constructible<T>::value, 0137 "The guard type must be default constructible"); 0138 0139 using type = T; 0140 }; 0141 0142 template <typename T, typename... Ts> 0143 struct call_guard<T, Ts...> { 0144 struct type { 0145 T guard{}; // Compose multiple guard types with left-to-right default-constructor order 0146 typename call_guard<Ts...>::type next{}; 0147 }; 0148 }; 0149 0150 /// @} annotations 0151 0152 PYBIND11_NAMESPACE_BEGIN(detail) 0153 /* Forward declarations */ 0154 enum op_id : int; 0155 enum op_type : int; 0156 struct undefined_t; 0157 template <op_id id, op_type ot, typename L = undefined_t, typename R = undefined_t> struct op_; 0158 void keep_alive_impl(size_t Nurse, size_t Patient, function_call &call, handle ret); 0159 0160 /// Internal data structure which holds metadata about a keyword argument 0161 struct argument_record { 0162 const char *name; ///< Argument name 0163 const char *descr; ///< Human-readable version of the argument value 0164 handle value; ///< Associated Python object 0165 bool convert : 1; ///< True if the argument is allowed to convert when loading 0166 bool none : 1; ///< True if None is allowed when loading 0167 0168 argument_record(const char *name, const char *descr, handle value, bool convert, bool none) 0169 : name(name), descr(descr), value(value), convert(convert), none(none) { } 0170 }; 0171 0172 /// Internal data structure which holds metadata about a bound function (signature, overloads, etc.) 0173 struct function_record { 0174 function_record() 0175 : is_constructor(false), is_new_style_constructor(false), is_stateless(false), 0176 is_operator(false), is_method(false), has_args(false), 0177 has_kwargs(false), prepend(false) { } 0178 0179 /// Function name 0180 char *name = nullptr; /* why no C++ strings? They generate heavier code.. */ 0181 0182 // User-specified documentation string 0183 char *doc = nullptr; 0184 0185 /// Human-readable version of the function signature 0186 char *signature = nullptr; 0187 0188 /// List of registered keyword arguments 0189 std::vector<argument_record> args; 0190 0191 /// Pointer to lambda function which converts arguments and performs the actual call 0192 handle (*impl) (function_call &) = nullptr; 0193 0194 /// Storage for the wrapped function pointer and captured data, if any 0195 void *data[3] = { }; 0196 0197 /// Pointer to custom destructor for 'data' (if needed) 0198 void (*free_data) (function_record *ptr) = nullptr; 0199 0200 /// Return value policy associated with this function 0201 return_value_policy policy = return_value_policy::automatic; 0202 0203 /// True if name == '__init__' 0204 bool is_constructor : 1; 0205 0206 /// True if this is a new-style `__init__` defined in `detail/init.h` 0207 bool is_new_style_constructor : 1; 0208 0209 /// True if this is a stateless function pointer 0210 bool is_stateless : 1; 0211 0212 /// True if this is an operator (__add__), etc. 0213 bool is_operator : 1; 0214 0215 /// True if this is a method 0216 bool is_method : 1; 0217 0218 /// True if the function has a '*args' argument 0219 bool has_args : 1; 0220 0221 /// True if the function has a '**kwargs' argument 0222 bool has_kwargs : 1; 0223 0224 /// True if this function is to be inserted at the beginning of the overload resolution chain 0225 bool prepend : 1; 0226 0227 /// Number of arguments (including py::args and/or py::kwargs, if present) 0228 std::uint16_t nargs; 0229 0230 /// Number of leading positional arguments, which are terminated by a py::args or py::kwargs 0231 /// argument or by a py::kw_only annotation. 0232 std::uint16_t nargs_pos = 0; 0233 0234 /// Number of leading arguments (counted in `nargs`) that are positional-only 0235 std::uint16_t nargs_pos_only = 0; 0236 0237 /// Python method object 0238 PyMethodDef *def = nullptr; 0239 0240 /// Python handle to the parent scope (a class or a module) 0241 handle scope; 0242 0243 /// Python handle to the sibling function representing an overload chain 0244 handle sibling; 0245 0246 /// Pointer to next overload 0247 function_record *next = nullptr; 0248 }; 0249 0250 /// Special data structure which (temporarily) holds metadata about a bound class 0251 struct type_record { 0252 PYBIND11_NOINLINE type_record() 0253 : multiple_inheritance(false), dynamic_attr(false), buffer_protocol(false), 0254 default_holder(true), module_local(false), is_final(false) { } 0255 0256 /// Handle to the parent scope 0257 handle scope; 0258 0259 /// Name of the class 0260 const char *name = nullptr; 0261 0262 // Pointer to RTTI type_info data structure 0263 const std::type_info *type = nullptr; 0264 0265 /// How large is the underlying C++ type? 0266 size_t type_size = 0; 0267 0268 /// What is the alignment of the underlying C++ type? 0269 size_t type_align = 0; 0270 0271 /// How large is the type's holder? 0272 size_t holder_size = 0; 0273 0274 /// The global operator new can be overridden with a class-specific variant 0275 void *(*operator_new)(size_t) = nullptr; 0276 0277 /// Function pointer to class_<..>::init_instance 0278 void (*init_instance)(instance *, const void *) = nullptr; 0279 0280 /// Function pointer to class_<..>::dealloc 0281 void (*dealloc)(detail::value_and_holder &) = nullptr; 0282 0283 /// List of base classes of the newly created type 0284 list bases; 0285 0286 /// Optional docstring 0287 const char *doc = nullptr; 0288 0289 /// Custom metaclass (optional) 0290 handle metaclass; 0291 0292 /// Custom type setup. 0293 custom_type_setup::callback custom_type_setup_callback; 0294 0295 /// Multiple inheritance marker 0296 bool multiple_inheritance : 1; 0297 0298 /// Does the class manage a __dict__? 0299 bool dynamic_attr : 1; 0300 0301 /// Does the class implement the buffer protocol? 0302 bool buffer_protocol : 1; 0303 0304 /// Is the default (unique_ptr) holder type used? 0305 bool default_holder : 1; 0306 0307 /// Is the class definition local to the module shared object? 0308 bool module_local : 1; 0309 0310 /// Is the class inheritable from python classes? 0311 bool is_final : 1; 0312 0313 PYBIND11_NOINLINE void add_base(const std::type_info &base, void *(*caster)(void *)) { 0314 auto base_info = detail::get_type_info(base, false); 0315 if (!base_info) { 0316 std::string tname(base.name()); 0317 detail::clean_type_id(tname); 0318 pybind11_fail("generic_type: type \"" + std::string(name) + 0319 "\" referenced unknown base type \"" + tname + "\""); 0320 } 0321 0322 if (default_holder != base_info->default_holder) { 0323 std::string tname(base.name()); 0324 detail::clean_type_id(tname); 0325 pybind11_fail("generic_type: type \"" + std::string(name) + "\" " + 0326 (default_holder ? "does not have" : "has") + 0327 " a non-default holder type while its base \"" + tname + "\" " + 0328 (base_info->default_holder ? "does not" : "does")); 0329 } 0330 0331 bases.append((PyObject *) base_info->type); 0332 0333 if (base_info->type->tp_dictoffset != 0) 0334 dynamic_attr = true; 0335 0336 if (caster) 0337 base_info->implicit_casts.emplace_back(type, caster); 0338 } 0339 }; 0340 0341 inline function_call::function_call(const function_record &f, handle p) : 0342 func(f), parent(p) { 0343 args.reserve(f.nargs); 0344 args_convert.reserve(f.nargs); 0345 } 0346 0347 /// Tag for a new-style `__init__` defined in `detail/init.h` 0348 struct is_new_style_constructor { }; 0349 0350 /** 0351 * Partial template specializations to process custom attributes provided to 0352 * cpp_function_ and class_. These are either used to initialize the respective 0353 * fields in the type_record and function_record data structures or executed at 0354 * runtime to deal with custom call policies (e.g. keep_alive). 0355 */ 0356 template <typename T, typename SFINAE = void> struct process_attribute; 0357 0358 template <typename T> struct process_attribute_default { 0359 /// Default implementation: do nothing 0360 static void init(const T &, function_record *) { } 0361 static void init(const T &, type_record *) { } 0362 static void precall(function_call &) { } 0363 static void postcall(function_call &, handle) { } 0364 }; 0365 0366 /// Process an attribute specifying the function's name 0367 template <> struct process_attribute<name> : process_attribute_default<name> { 0368 static void init(const name &n, function_record *r) { r->name = const_cast<char *>(n.value); } 0369 }; 0370 0371 /// Process an attribute specifying the function's docstring 0372 template <> struct process_attribute<doc> : process_attribute_default<doc> { 0373 static void init(const doc &n, function_record *r) { r->doc = const_cast<char *>(n.value); } 0374 }; 0375 0376 /// Process an attribute specifying the function's docstring (provided as a C-style string) 0377 template <> struct process_attribute<const char *> : process_attribute_default<const char *> { 0378 static void init(const char *d, function_record *r) { r->doc = const_cast<char *>(d); } 0379 static void init(const char *d, type_record *r) { r->doc = const_cast<char *>(d); } 0380 }; 0381 template <> struct process_attribute<char *> : process_attribute<const char *> { }; 0382 0383 /// Process an attribute indicating the function's return value policy 0384 template <> struct process_attribute<return_value_policy> : process_attribute_default<return_value_policy> { 0385 static void init(const return_value_policy &p, function_record *r) { r->policy = p; } 0386 }; 0387 0388 /// Process an attribute which indicates that this is an overloaded function associated with a given sibling 0389 template <> struct process_attribute<sibling> : process_attribute_default<sibling> { 0390 static void init(const sibling &s, function_record *r) { r->sibling = s.value; } 0391 }; 0392 0393 /// Process an attribute which indicates that this function is a method 0394 template <> struct process_attribute<is_method> : process_attribute_default<is_method> { 0395 static void init(const is_method &s, function_record *r) { r->is_method = true; r->scope = s.class_; } 0396 }; 0397 0398 /// Process an attribute which indicates the parent scope of a method 0399 template <> struct process_attribute<scope> : process_attribute_default<scope> { 0400 static void init(const scope &s, function_record *r) { r->scope = s.value; } 0401 }; 0402 0403 /// Process an attribute which indicates that this function is an operator 0404 template <> struct process_attribute<is_operator> : process_attribute_default<is_operator> { 0405 static void init(const is_operator &, function_record *r) { r->is_operator = true; } 0406 }; 0407 0408 template <> struct process_attribute<is_new_style_constructor> : process_attribute_default<is_new_style_constructor> { 0409 static void init(const is_new_style_constructor &, function_record *r) { r->is_new_style_constructor = true; } 0410 }; 0411 0412 inline void check_kw_only_arg(const arg &a, function_record *r) { 0413 if (r->args.size() > r->nargs_pos && (!a.name || a.name[0] == '\0')) 0414 pybind11_fail("arg(): cannot specify an unnamed argument after a kw_only() annotation or args() argument"); 0415 } 0416 0417 inline void append_self_arg_if_needed(function_record *r) { 0418 if (r->is_method && r->args.empty()) 0419 r->args.emplace_back("self", nullptr, handle(), /*convert=*/ true, /*none=*/ false); 0420 } 0421 0422 /// Process a keyword argument attribute (*without* a default value) 0423 template <> struct process_attribute<arg> : process_attribute_default<arg> { 0424 static void init(const arg &a, function_record *r) { 0425 append_self_arg_if_needed(r); 0426 r->args.emplace_back(a.name, nullptr, handle(), !a.flag_noconvert, a.flag_none); 0427 0428 check_kw_only_arg(a, r); 0429 } 0430 }; 0431 0432 /// Process a keyword argument attribute (*with* a default value) 0433 template <> struct process_attribute<arg_v> : process_attribute_default<arg_v> { 0434 static void init(const arg_v &a, function_record *r) { 0435 if (r->is_method && r->args.empty()) 0436 r->args.emplace_back("self", /*descr=*/ nullptr, /*parent=*/ handle(), /*convert=*/ true, /*none=*/ false); 0437 0438 if (!a.value) { 0439 #if !defined(NDEBUG) 0440 std::string descr("'"); 0441 if (a.name) descr += std::string(a.name) + ": "; 0442 descr += a.type + "'"; 0443 if (r->is_method) { 0444 if (r->name) 0445 descr += " in method '" + (std::string) str(r->scope) + "." + (std::string) r->name + "'"; 0446 else 0447 descr += " in method of '" + (std::string) str(r->scope) + "'"; 0448 } else if (r->name) { 0449 descr += " in function '" + (std::string) r->name + "'"; 0450 } 0451 pybind11_fail("arg(): could not convert default argument " 0452 + descr + " into a Python object (type not registered yet?)"); 0453 #else 0454 pybind11_fail("arg(): could not convert default argument " 0455 "into a Python object (type not registered yet?). " 0456 "Compile in debug mode for more information."); 0457 #endif 0458 } 0459 r->args.emplace_back(a.name, a.descr, a.value.inc_ref(), !a.flag_noconvert, a.flag_none); 0460 0461 check_kw_only_arg(a, r); 0462 } 0463 }; 0464 0465 /// Process a keyword-only-arguments-follow pseudo argument 0466 template <> struct process_attribute<kw_only> : process_attribute_default<kw_only> { 0467 static void init(const kw_only &, function_record *r) { 0468 append_self_arg_if_needed(r); 0469 if (r->has_args && r->nargs_pos != static_cast<std::uint16_t>(r->args.size())) 0470 pybind11_fail("Mismatched args() and kw_only(): they must occur at the same relative argument location (or omit kw_only() entirely)"); 0471 r->nargs_pos = static_cast<std::uint16_t>(r->args.size()); 0472 } 0473 }; 0474 0475 /// Process a positional-only-argument maker 0476 template <> struct process_attribute<pos_only> : process_attribute_default<pos_only> { 0477 static void init(const pos_only &, function_record *r) { 0478 append_self_arg_if_needed(r); 0479 r->nargs_pos_only = static_cast<std::uint16_t>(r->args.size()); 0480 if (r->nargs_pos_only > r->nargs_pos) 0481 pybind11_fail("pos_only(): cannot follow a py::args() argument"); 0482 // It also can't follow a kw_only, but a static_assert in pybind11.h checks that 0483 } 0484 }; 0485 0486 /// Process a parent class attribute. Single inheritance only (class_ itself already guarantees that) 0487 template <typename T> 0488 struct process_attribute<T, enable_if_t<is_pyobject<T>::value>> : process_attribute_default<handle> { 0489 static void init(const handle &h, type_record *r) { r->bases.append(h); } 0490 }; 0491 0492 /// Process a parent class attribute (deprecated, does not support multiple inheritance) 0493 template <typename T> 0494 struct process_attribute<base<T>> : process_attribute_default<base<T>> { 0495 static void init(const base<T> &, type_record *r) { r->add_base(typeid(T), nullptr); } 0496 }; 0497 0498 /// Process a multiple inheritance attribute 0499 template <> 0500 struct process_attribute<multiple_inheritance> : process_attribute_default<multiple_inheritance> { 0501 static void init(const multiple_inheritance &, type_record *r) { r->multiple_inheritance = true; } 0502 }; 0503 0504 template <> 0505 struct process_attribute<dynamic_attr> : process_attribute_default<dynamic_attr> { 0506 static void init(const dynamic_attr &, type_record *r) { r->dynamic_attr = true; } 0507 }; 0508 0509 template <> 0510 struct process_attribute<custom_type_setup> { 0511 static void init(const custom_type_setup &value, type_record *r) { 0512 r->custom_type_setup_callback = value.value; 0513 } 0514 }; 0515 0516 template <> 0517 struct process_attribute<is_final> : process_attribute_default<is_final> { 0518 static void init(const is_final &, type_record *r) { r->is_final = true; } 0519 }; 0520 0521 template <> 0522 struct process_attribute<buffer_protocol> : process_attribute_default<buffer_protocol> { 0523 static void init(const buffer_protocol &, type_record *r) { r->buffer_protocol = true; } 0524 }; 0525 0526 template <> 0527 struct process_attribute<metaclass> : process_attribute_default<metaclass> { 0528 static void init(const metaclass &m, type_record *r) { r->metaclass = m.value; } 0529 }; 0530 0531 template <> 0532 struct process_attribute<module_local> : process_attribute_default<module_local> { 0533 static void init(const module_local &l, type_record *r) { r->module_local = l.value; } 0534 }; 0535 0536 /// Process a 'prepend' attribute, putting this at the beginning of the overload chain 0537 template <> 0538 struct process_attribute<prepend> : process_attribute_default<prepend> { 0539 static void init(const prepend &, function_record *r) { r->prepend = true; } 0540 }; 0541 0542 /// Process an 'arithmetic' attribute for enums (does nothing here) 0543 template <> 0544 struct process_attribute<arithmetic> : process_attribute_default<arithmetic> {}; 0545 0546 template <typename... Ts> 0547 struct process_attribute<call_guard<Ts...>> : process_attribute_default<call_guard<Ts...>> { }; 0548 0549 /** 0550 * Process a keep_alive call policy -- invokes keep_alive_impl during the 0551 * pre-call handler if both Nurse, Patient != 0 and use the post-call handler 0552 * otherwise 0553 */ 0554 template <size_t Nurse, size_t Patient> struct process_attribute<keep_alive<Nurse, Patient>> : public process_attribute_default<keep_alive<Nurse, Patient>> { 0555 template <size_t N = Nurse, size_t P = Patient, enable_if_t<N != 0 && P != 0, int> = 0> 0556 static void precall(function_call &call) { keep_alive_impl(Nurse, Patient, call, handle()); } 0557 template <size_t N = Nurse, size_t P = Patient, enable_if_t<N != 0 && P != 0, int> = 0> 0558 static void postcall(function_call &, handle) { } 0559 template <size_t N = Nurse, size_t P = Patient, enable_if_t<N == 0 || P == 0, int> = 0> 0560 static void precall(function_call &) { } 0561 template <size_t N = Nurse, size_t P = Patient, enable_if_t<N == 0 || P == 0, int> = 0> 0562 static void postcall(function_call &call, handle ret) { keep_alive_impl(Nurse, Patient, call, ret); } 0563 }; 0564 0565 /// Recursively iterate over variadic template arguments 0566 template <typename... Args> struct process_attributes { 0567 static void init(const Args&... args, function_record *r) { 0568 PYBIND11_WORKAROUND_INCORRECT_MSVC_C4100(r); 0569 PYBIND11_WORKAROUND_INCORRECT_GCC_UNUSED_BUT_SET_PARAMETER(r); 0570 using expander = int[]; 0571 (void) expander{ 0572 0, ((void) process_attribute<typename std::decay<Args>::type>::init(args, r), 0)...}; 0573 } 0574 static void init(const Args&... args, type_record *r) { 0575 PYBIND11_WORKAROUND_INCORRECT_MSVC_C4100(r); 0576 PYBIND11_WORKAROUND_INCORRECT_GCC_UNUSED_BUT_SET_PARAMETER(r); 0577 using expander = int[]; 0578 (void) expander{0, 0579 (process_attribute<typename std::decay<Args>::type>::init(args, r), 0)...}; 0580 } 0581 static void precall(function_call &call) { 0582 PYBIND11_WORKAROUND_INCORRECT_MSVC_C4100(call); 0583 using expander = int[]; 0584 (void) expander{0, 0585 (process_attribute<typename std::decay<Args>::type>::precall(call), 0)...}; 0586 } 0587 static void postcall(function_call &call, handle fn_ret) { 0588 PYBIND11_WORKAROUND_INCORRECT_MSVC_C4100(call, fn_ret); 0589 PYBIND11_WORKAROUND_INCORRECT_GCC_UNUSED_BUT_SET_PARAMETER(fn_ret); 0590 using expander = int[]; 0591 (void) expander{ 0592 0, (process_attribute<typename std::decay<Args>::type>::postcall(call, fn_ret), 0)...}; 0593 } 0594 }; 0595 0596 template <typename T> 0597 using is_call_guard = is_instantiation<call_guard, T>; 0598 0599 /// Extract the ``type`` from the first `call_guard` in `Extras...` (or `void_type` if none found) 0600 template <typename... Extra> 0601 using extract_guard_t = typename exactly_one_t<is_call_guard, call_guard<>, Extra...>::type; 0602 0603 /// Check the number of named arguments at compile time 0604 template <typename... Extra, 0605 size_t named = constexpr_sum(std::is_base_of<arg, Extra>::value...), 0606 size_t self = constexpr_sum(std::is_same<is_method, Extra>::value...)> 0607 constexpr bool expected_num_args(size_t nargs, bool has_args, bool has_kwargs) { 0608 PYBIND11_WORKAROUND_INCORRECT_MSVC_C4100(nargs, has_args, has_kwargs); 0609 return named == 0 || (self + named + size_t(has_args) + size_t(has_kwargs)) == nargs; 0610 } 0611 0612 PYBIND11_NAMESPACE_END(detail) 0613 PYBIND11_NAMESPACE_END(PYBIND11_NAMESPACE)