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