File indexing completed on 2025-01-18 10:17:46
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011 #pragma once
0012
0013 #include "detail/common.h"
0014 #include "cast.h"
0015
0016 #include <functional>
0017
0018 PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE)
0019
0020
0021
0022
0023
0024 struct is_method {
0025 handle class_;
0026 explicit is_method(const handle &c) : class_(c) {}
0027 };
0028
0029
0030 struct is_operator {};
0031
0032
0033 struct is_final {};
0034
0035
0036 struct scope {
0037 handle value;
0038 explicit scope(const handle &s) : value(s) {}
0039 };
0040
0041
0042 struct doc {
0043 const char *value;
0044 explicit doc(const char *value) : value(value) {}
0045 };
0046
0047
0048 struct name {
0049 const char *value;
0050 explicit name(const char *value) : value(value) {}
0051 };
0052
0053
0054 struct sibling {
0055 handle value;
0056 explicit sibling(const handle &value) : value(value.ptr()) {}
0057 };
0058
0059
0060 template <typename T>
0061 struct base {
0062
0063 PYBIND11_DEPRECATED(
0064 "base<T>() was deprecated in favor of specifying 'T' as a template argument to class_")
0065 base() = default;
0066 };
0067
0068
0069 template <size_t Nurse, size_t Patient>
0070 struct keep_alive {};
0071
0072
0073 struct multiple_inheritance {};
0074
0075
0076 struct dynamic_attr {};
0077
0078
0079 struct buffer_protocol {};
0080
0081
0082 struct metaclass {
0083 handle value;
0084
0085 PYBIND11_DEPRECATED("py::metaclass() is no longer required. It's turned on by default now.")
0086 metaclass() = default;
0087
0088
0089 explicit metaclass(handle value) : value(value) {}
0090 };
0091
0092
0093
0094
0095
0096
0097
0098
0099
0100
0101 struct custom_type_setup {
0102 using callback = std::function<void(PyHeapTypeObject *heap_type)>;
0103
0104 explicit custom_type_setup(callback value) : value(std::move(value)) {}
0105
0106 callback value;
0107 };
0108
0109
0110 struct module_local {
0111 const bool value;
0112 constexpr explicit module_local(bool v = true) : value(v) {}
0113 };
0114
0115
0116 struct arithmetic {};
0117
0118
0119 struct prepend {};
0120
0121
0122
0123
0124
0125
0126
0127
0128
0129
0130
0131
0132
0133
0134
0135
0136
0137
0138
0139 template <typename... Ts>
0140 struct call_guard;
0141
0142 template <>
0143 struct call_guard<> {
0144 using type = detail::void_type;
0145 };
0146
0147 template <typename T>
0148 struct call_guard<T> {
0149 static_assert(std::is_default_constructible<T>::value,
0150 "The guard type must be default constructible");
0151
0152 using type = T;
0153 };
0154
0155 template <typename T, typename... Ts>
0156 struct call_guard<T, Ts...> {
0157 struct type {
0158 T guard{};
0159 typename call_guard<Ts...>::type next{};
0160 };
0161 };
0162
0163
0164
0165 PYBIND11_NAMESPACE_BEGIN(detail)
0166
0167 enum op_id : int;
0168 enum op_type : int;
0169 struct undefined_t;
0170 template <op_id id, op_type ot, typename L = undefined_t, typename R = undefined_t>
0171 struct op_;
0172 void keep_alive_impl(size_t Nurse, size_t Patient, function_call &call, handle ret);
0173
0174
0175 struct argument_record {
0176 const char *name;
0177 const char *descr;
0178 handle value;
0179 bool convert : 1;
0180 bool none : 1;
0181
0182 argument_record(const char *name, const char *descr, handle value, bool convert, bool none)
0183 : name(name), descr(descr), value(value), convert(convert), none(none) {}
0184 };
0185
0186
0187
0188 struct function_record {
0189 function_record()
0190 : is_constructor(false), is_new_style_constructor(false), is_stateless(false),
0191 is_operator(false), is_method(false), has_args(false), has_kwargs(false),
0192 prepend(false) {}
0193
0194
0195 char *name = nullptr;
0196
0197
0198 char *doc = nullptr;
0199
0200
0201 char *signature = nullptr;
0202
0203
0204 std::vector<argument_record> args;
0205
0206
0207 handle (*impl)(function_call &) = nullptr;
0208
0209
0210 void *data[3] = {};
0211
0212
0213 void (*free_data)(function_record *ptr) = nullptr;
0214
0215
0216 return_value_policy policy = return_value_policy::automatic;
0217
0218
0219 bool is_constructor : 1;
0220
0221
0222 bool is_new_style_constructor : 1;
0223
0224
0225 bool is_stateless : 1;
0226
0227
0228 bool is_operator : 1;
0229
0230
0231 bool is_method : 1;
0232
0233
0234 bool has_args : 1;
0235
0236
0237 bool has_kwargs : 1;
0238
0239
0240 bool prepend : 1;
0241
0242
0243 std::uint16_t nargs;
0244
0245
0246
0247 std::uint16_t nargs_pos = 0;
0248
0249
0250 std::uint16_t nargs_pos_only = 0;
0251
0252
0253 PyMethodDef *def = nullptr;
0254
0255
0256 handle scope;
0257
0258
0259 handle sibling;
0260
0261
0262 function_record *next = nullptr;
0263 };
0264
0265
0266 struct type_record {
0267 PYBIND11_NOINLINE type_record()
0268 : multiple_inheritance(false), dynamic_attr(false), buffer_protocol(false),
0269 default_holder(true), module_local(false), is_final(false) {}
0270
0271
0272 handle scope;
0273
0274
0275 const char *name = nullptr;
0276
0277
0278 const std::type_info *type = nullptr;
0279
0280
0281 size_t type_size = 0;
0282
0283
0284 size_t type_align = 0;
0285
0286
0287 size_t holder_size = 0;
0288
0289
0290 void *(*operator_new)(size_t) = nullptr;
0291
0292
0293 void (*init_instance)(instance *, const void *) = nullptr;
0294
0295
0296 void (*dealloc)(detail::value_and_holder &) = nullptr;
0297
0298
0299 list bases;
0300
0301
0302 const char *doc = nullptr;
0303
0304
0305 handle metaclass;
0306
0307
0308 custom_type_setup::callback custom_type_setup_callback;
0309
0310
0311 bool multiple_inheritance : 1;
0312
0313
0314 bool dynamic_attr : 1;
0315
0316
0317 bool buffer_protocol : 1;
0318
0319
0320 bool default_holder : 1;
0321
0322
0323 bool module_local : 1;
0324
0325
0326 bool is_final : 1;
0327
0328 PYBIND11_NOINLINE void add_base(const std::type_info &base, void *(*caster)(void *) ) {
0329 auto *base_info = detail::get_type_info(base, false);
0330 if (!base_info) {
0331 std::string tname(base.name());
0332 detail::clean_type_id(tname);
0333 pybind11_fail("generic_type: type \"" + std::string(name)
0334 + "\" referenced unknown base type \"" + tname + "\"");
0335 }
0336
0337 if (default_holder != base_info->default_holder) {
0338 std::string tname(base.name());
0339 detail::clean_type_id(tname);
0340 pybind11_fail("generic_type: type \"" + std::string(name) + "\" "
0341 + (default_holder ? "does not have" : "has")
0342 + " a non-default holder type while its base \"" + tname + "\" "
0343 + (base_info->default_holder ? "does not" : "does"));
0344 }
0345
0346 bases.append((PyObject *) base_info->type);
0347
0348 #if PY_VERSION_HEX < 0x030B0000
0349 dynamic_attr |= base_info->type->tp_dictoffset != 0;
0350 #else
0351 dynamic_attr |= (base_info->type->tp_flags & Py_TPFLAGS_MANAGED_DICT) != 0;
0352 #endif
0353
0354 if (caster) {
0355 base_info->implicit_casts.emplace_back(type, caster);
0356 }
0357 }
0358 };
0359
0360 inline function_call::function_call(const function_record &f, handle p) : func(f), parent(p) {
0361 args.reserve(f.nargs);
0362 args_convert.reserve(f.nargs);
0363 }
0364
0365
0366 struct is_new_style_constructor {};
0367
0368
0369
0370
0371
0372
0373
0374 template <typename T, typename SFINAE = void>
0375 struct process_attribute;
0376
0377 template <typename T>
0378 struct process_attribute_default {
0379
0380 static void init(const T &, function_record *) {}
0381 static void init(const T &, type_record *) {}
0382 static void precall(function_call &) {}
0383 static void postcall(function_call &, handle) {}
0384 };
0385
0386
0387 template <>
0388 struct process_attribute<name> : process_attribute_default<name> {
0389 static void init(const name &n, function_record *r) { r->name = const_cast<char *>(n.value); }
0390 };
0391
0392
0393 template <>
0394 struct process_attribute<doc> : process_attribute_default<doc> {
0395 static void init(const doc &n, function_record *r) { r->doc = const_cast<char *>(n.value); }
0396 };
0397
0398
0399 template <>
0400 struct process_attribute<const char *> : process_attribute_default<const char *> {
0401 static void init(const char *d, function_record *r) { r->doc = const_cast<char *>(d); }
0402 static void init(const char *d, type_record *r) { r->doc = d; }
0403 };
0404 template <>
0405 struct process_attribute<char *> : process_attribute<const char *> {};
0406
0407
0408 template <>
0409 struct process_attribute<return_value_policy> : process_attribute_default<return_value_policy> {
0410 static void init(const return_value_policy &p, function_record *r) { r->policy = p; }
0411 };
0412
0413
0414
0415 template <>
0416 struct process_attribute<sibling> : process_attribute_default<sibling> {
0417 static void init(const sibling &s, function_record *r) { r->sibling = s.value; }
0418 };
0419
0420
0421 template <>
0422 struct process_attribute<is_method> : process_attribute_default<is_method> {
0423 static void init(const is_method &s, function_record *r) {
0424 r->is_method = true;
0425 r->scope = s.class_;
0426 }
0427 };
0428
0429
0430 template <>
0431 struct process_attribute<scope> : process_attribute_default<scope> {
0432 static void init(const scope &s, function_record *r) { r->scope = s.value; }
0433 };
0434
0435
0436 template <>
0437 struct process_attribute<is_operator> : process_attribute_default<is_operator> {
0438 static void init(const is_operator &, function_record *r) { r->is_operator = true; }
0439 };
0440
0441 template <>
0442 struct process_attribute<is_new_style_constructor>
0443 : process_attribute_default<is_new_style_constructor> {
0444 static void init(const is_new_style_constructor &, function_record *r) {
0445 r->is_new_style_constructor = true;
0446 }
0447 };
0448
0449 inline void check_kw_only_arg(const arg &a, function_record *r) {
0450 if (r->args.size() > r->nargs_pos && (!a.name || a.name[0] == '\0')) {
0451 pybind11_fail("arg(): cannot specify an unnamed argument after a kw_only() annotation or "
0452 "args() argument");
0453 }
0454 }
0455
0456 inline void append_self_arg_if_needed(function_record *r) {
0457 if (r->is_method && r->args.empty()) {
0458 r->args.emplace_back("self", nullptr, handle(), true, false);
0459 }
0460 }
0461
0462
0463 template <>
0464 struct process_attribute<arg> : process_attribute_default<arg> {
0465 static void init(const arg &a, function_record *r) {
0466 append_self_arg_if_needed(r);
0467 r->args.emplace_back(a.name, nullptr, handle(), !a.flag_noconvert, a.flag_none);
0468
0469 check_kw_only_arg(a, r);
0470 }
0471 };
0472
0473
0474 template <>
0475 struct process_attribute<arg_v> : process_attribute_default<arg_v> {
0476 static void init(const arg_v &a, function_record *r) {
0477 if (r->is_method && r->args.empty()) {
0478 r->args.emplace_back(
0479 "self", nullptr, handle(), true, false);
0480 }
0481
0482 if (!a.value) {
0483 #if defined(PYBIND11_DETAILED_ERROR_MESSAGES)
0484 std::string descr("'");
0485 if (a.name) {
0486 descr += std::string(a.name) + ": ";
0487 }
0488 descr += a.type + "'";
0489 if (r->is_method) {
0490 if (r->name) {
0491 descr += " in method '" + (std::string) str(r->scope) + "."
0492 + (std::string) r->name + "'";
0493 } else {
0494 descr += " in method of '" + (std::string) str(r->scope) + "'";
0495 }
0496 } else if (r->name) {
0497 descr += " in function '" + (std::string) r->name + "'";
0498 }
0499 pybind11_fail("arg(): could not convert default argument " + descr
0500 + " into a Python object (type not registered yet?)");
0501 #else
0502 pybind11_fail("arg(): could not convert default argument "
0503 "into a Python object (type not registered yet?). "
0504 "#define PYBIND11_DETAILED_ERROR_MESSAGES or compile in debug mode for "
0505 "more information.");
0506 #endif
0507 }
0508 r->args.emplace_back(a.name, a.descr, a.value.inc_ref(), !a.flag_noconvert, a.flag_none);
0509
0510 check_kw_only_arg(a, r);
0511 }
0512 };
0513
0514
0515 template <>
0516 struct process_attribute<kw_only> : process_attribute_default<kw_only> {
0517 static void init(const kw_only &, function_record *r) {
0518 append_self_arg_if_needed(r);
0519 if (r->has_args && r->nargs_pos != static_cast<std::uint16_t>(r->args.size())) {
0520 pybind11_fail("Mismatched args() and kw_only(): they must occur at the same relative "
0521 "argument location (or omit kw_only() entirely)");
0522 }
0523 r->nargs_pos = static_cast<std::uint16_t>(r->args.size());
0524 }
0525 };
0526
0527
0528 template <>
0529 struct process_attribute<pos_only> : process_attribute_default<pos_only> {
0530 static void init(const pos_only &, function_record *r) {
0531 append_self_arg_if_needed(r);
0532 r->nargs_pos_only = static_cast<std::uint16_t>(r->args.size());
0533 if (r->nargs_pos_only > r->nargs_pos) {
0534 pybind11_fail("pos_only(): cannot follow a py::args() argument");
0535 }
0536
0537 }
0538 };
0539
0540
0541
0542 template <typename T>
0543 struct process_attribute<T, enable_if_t<is_pyobject<T>::value>>
0544 : process_attribute_default<handle> {
0545 static void init(const handle &h, type_record *r) { r->bases.append(h); }
0546 };
0547
0548
0549 template <typename T>
0550 struct process_attribute<base<T>> : process_attribute_default<base<T>> {
0551 static void init(const base<T> &, type_record *r) { r->add_base(typeid(T), nullptr); }
0552 };
0553
0554
0555 template <>
0556 struct process_attribute<multiple_inheritance> : process_attribute_default<multiple_inheritance> {
0557 static void init(const multiple_inheritance &, type_record *r) {
0558 r->multiple_inheritance = true;
0559 }
0560 };
0561
0562 template <>
0563 struct process_attribute<dynamic_attr> : process_attribute_default<dynamic_attr> {
0564 static void init(const dynamic_attr &, type_record *r) { r->dynamic_attr = true; }
0565 };
0566
0567 template <>
0568 struct process_attribute<custom_type_setup> {
0569 static void init(const custom_type_setup &value, type_record *r) {
0570 r->custom_type_setup_callback = value.value;
0571 }
0572 };
0573
0574 template <>
0575 struct process_attribute<is_final> : process_attribute_default<is_final> {
0576 static void init(const is_final &, type_record *r) { r->is_final = true; }
0577 };
0578
0579 template <>
0580 struct process_attribute<buffer_protocol> : process_attribute_default<buffer_protocol> {
0581 static void init(const buffer_protocol &, type_record *r) { r->buffer_protocol = true; }
0582 };
0583
0584 template <>
0585 struct process_attribute<metaclass> : process_attribute_default<metaclass> {
0586 static void init(const metaclass &m, type_record *r) { r->metaclass = m.value; }
0587 };
0588
0589 template <>
0590 struct process_attribute<module_local> : process_attribute_default<module_local> {
0591 static void init(const module_local &l, type_record *r) { r->module_local = l.value; }
0592 };
0593
0594
0595 template <>
0596 struct process_attribute<prepend> : process_attribute_default<prepend> {
0597 static void init(const prepend &, function_record *r) { r->prepend = true; }
0598 };
0599
0600
0601 template <>
0602 struct process_attribute<arithmetic> : process_attribute_default<arithmetic> {};
0603
0604 template <typename... Ts>
0605 struct process_attribute<call_guard<Ts...>> : process_attribute_default<call_guard<Ts...>> {};
0606
0607
0608
0609
0610
0611
0612 template <size_t Nurse, size_t Patient>
0613 struct process_attribute<keep_alive<Nurse, Patient>>
0614 : public process_attribute_default<keep_alive<Nurse, Patient>> {
0615 template <size_t N = Nurse, size_t P = Patient, enable_if_t<N != 0 && P != 0, int> = 0>
0616 static void precall(function_call &call) {
0617 keep_alive_impl(Nurse, Patient, call, handle());
0618 }
0619 template <size_t N = Nurse, size_t P = Patient, enable_if_t<N != 0 && P != 0, int> = 0>
0620 static void postcall(function_call &, handle) {}
0621 template <size_t N = Nurse, size_t P = Patient, enable_if_t<N == 0 || P == 0, int> = 0>
0622 static void precall(function_call &) {}
0623 template <size_t N = Nurse, size_t P = Patient, enable_if_t<N == 0 || P == 0, int> = 0>
0624 static void postcall(function_call &call, handle ret) {
0625 keep_alive_impl(Nurse, Patient, call, ret);
0626 }
0627 };
0628
0629
0630 template <typename... Args>
0631 struct process_attributes {
0632 static void init(const Args &...args, function_record *r) {
0633 PYBIND11_WORKAROUND_INCORRECT_MSVC_C4100(r);
0634 PYBIND11_WORKAROUND_INCORRECT_GCC_UNUSED_BUT_SET_PARAMETER(r);
0635 using expander = int[];
0636 (void) expander{
0637 0, ((void) process_attribute<typename std::decay<Args>::type>::init(args, r), 0)...};
0638 }
0639 static void init(const Args &...args, type_record *r) {
0640 PYBIND11_WORKAROUND_INCORRECT_MSVC_C4100(r);
0641 PYBIND11_WORKAROUND_INCORRECT_GCC_UNUSED_BUT_SET_PARAMETER(r);
0642 using expander = int[];
0643 (void) expander{0,
0644 (process_attribute<typename std::decay<Args>::type>::init(args, r), 0)...};
0645 }
0646 static void precall(function_call &call) {
0647 PYBIND11_WORKAROUND_INCORRECT_MSVC_C4100(call);
0648 using expander = int[];
0649 (void) expander{0,
0650 (process_attribute<typename std::decay<Args>::type>::precall(call), 0)...};
0651 }
0652 static void postcall(function_call &call, handle fn_ret) {
0653 PYBIND11_WORKAROUND_INCORRECT_MSVC_C4100(call, fn_ret);
0654 PYBIND11_WORKAROUND_INCORRECT_GCC_UNUSED_BUT_SET_PARAMETER(fn_ret);
0655 using expander = int[];
0656 (void) expander{
0657 0, (process_attribute<typename std::decay<Args>::type>::postcall(call, fn_ret), 0)...};
0658 }
0659 };
0660
0661 template <typename T>
0662 using is_call_guard = is_instantiation<call_guard, T>;
0663
0664
0665 template <typename... Extra>
0666 using extract_guard_t = typename exactly_one_t<is_call_guard, call_guard<>, Extra...>::type;
0667
0668
0669 template <typename... Extra,
0670 size_t named = constexpr_sum(std::is_base_of<arg, Extra>::value...),
0671 size_t self = constexpr_sum(std::is_same<is_method, Extra>::value...)>
0672 constexpr bool expected_num_args(size_t nargs, bool has_args, bool has_kwargs) {
0673 PYBIND11_WORKAROUND_INCORRECT_MSVC_C4100(nargs, has_args, has_kwargs);
0674 return named == 0 || (self + named + size_t(has_args) + size_t(has_kwargs)) == nargs;
0675 }
0676
0677 PYBIND11_NAMESPACE_END(detail)
0678 PYBIND11_NAMESPACE_END(PYBIND11_NAMESPACE)