• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2     pybind11/attr.h: Infrastructure for processing custom
3     type and function attributes
4 
5     Copyright (c) 2016 Wenzel Jakob <wenzel.jakob@epfl.ch>
6 
7     All rights reserved. Use of this source code is governed by a
8     BSD-style license that can be found in the LICENSE file.
9 */
10 
11 #pragma once
12 
13 #include "cast.h"
14 
15 PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE)
16 
17 /// \addtogroup annotations
18 /// @{
19 
20 /// Annotation for methods
is_methodis_method21 struct is_method { handle class_; is_method(const handle &c) : class_(c) { } };
22 
23 /// Annotation for operators
24 struct is_operator { };
25 
26 /// Annotation for classes that cannot be subclassed
27 struct is_final { };
28 
29 /// Annotation for parent scope
scopescope30 struct scope { handle value; scope(const handle &s) : value(s) { } };
31 
32 /// Annotation for documentation
docdoc33 struct doc { const char *value; doc(const char *value) : value(value) { } };
34 
35 /// Annotation for function names
namename36 struct name { const char *value; name(const char *value) : value(value) { } };
37 
38 /// Annotation indicating that a function is an overload associated with a given "sibling"
siblingsibling39 struct sibling { handle value; sibling(const handle &value) : value(value.ptr()) { } };
40 
41 /// Annotation indicating that a class derives from another given type
42 template <typename T> struct base {
43 
44     PYBIND11_DEPRECATED("base<T>() was deprecated in favor of specifying 'T' as a template argument to class_")
basebase45     base() { } // NOLINT(modernize-use-equals-default): breaks MSVC 2015 when adding an attribute
46 };
47 
48 /// Keep patient alive while nurse lives
49 template <size_t Nurse, size_t Patient> struct keep_alive { };
50 
51 /// Annotation indicating that a class is involved in a multiple inheritance relationship
52 struct multiple_inheritance { };
53 
54 /// Annotation which enables dynamic attributes, i.e. adds `__dict__` to a class
55 struct dynamic_attr { };
56 
57 /// Annotation which enables the buffer protocol for a type
58 struct buffer_protocol { };
59 
60 /// Annotation which requests that a special metaclass is created for a type
61 struct metaclass {
62     handle value;
63 
64     PYBIND11_DEPRECATED("py::metaclass() is no longer required. It's turned on by default now.")
metaclassmetaclass65     metaclass() { } // NOLINT(modernize-use-equals-default): breaks MSVC 2015 when adding an attribute
66 
67     /// Override pybind11's default metaclass
metaclassmetaclass68     explicit metaclass(handle value) : value(value) { }
69 };
70 
71 /// Annotation that marks a class as local to the module:
valuemodule_local72 struct module_local { const bool value; constexpr module_local(bool v = true) : value(v) { } };
73 
74 /// Annotation to mark enums as an arithmetic type
75 struct arithmetic { };
76 
77 /// Mark a function for addition at the beginning of the existing overload chain instead of the end
78 struct prepend { };
79 
80 /** \rst
81     A call policy which places one or more guard variables (``Ts...``) around the function call.
82 
83     For example, this definition:
84 
85     .. code-block:: cpp
86 
87         m.def("foo", foo, py::call_guard<T>());
88 
89     is equivalent to the following pseudocode:
90 
91     .. code-block:: cpp
92 
93         m.def("foo", [](args...) {
94             T scope_guard;
95             return foo(args...); // forwarded arguments
96         });
97  \endrst */
98 template <typename... Ts> struct call_guard;
99 
100 template <> struct call_guard<> { using type = detail::void_type; };
101 
102 template <typename T>
103 struct call_guard<T> {
104     static_assert(std::is_default_constructible<T>::value,
105                   "The guard type must be default constructible");
106 
107     using type = T;
108 };
109 
110 template <typename T, typename... Ts>
111 struct call_guard<T, Ts...> {
112     struct type {
113         T guard{}; // Compose multiple guard types with left-to-right default-constructor order
114         typename call_guard<Ts...>::type next{};
115     };
116 };
117 
118 /// @} annotations
119 
120 PYBIND11_NAMESPACE_BEGIN(detail)
121 /* Forward declarations */
122 enum op_id : int;
123 enum op_type : int;
124 struct undefined_t;
125 template <op_id id, op_type ot, typename L = undefined_t, typename R = undefined_t> struct op_;
126 inline void keep_alive_impl(size_t Nurse, size_t Patient, function_call &call, handle ret);
127 
128 /// Internal data structure which holds metadata about a keyword argument
129 struct argument_record {
130     const char *name;  ///< Argument name
131     const char *descr; ///< Human-readable version of the argument value
132     handle value;      ///< Associated Python object
133     bool convert : 1;  ///< True if the argument is allowed to convert when loading
134     bool none : 1;     ///< True if None is allowed when loading
135 
136     argument_record(const char *name, const char *descr, handle value, bool convert, bool none)
137         : name(name), descr(descr), value(value), convert(convert), none(none) { }
138 };
139 
140 /// Internal data structure which holds metadata about a bound function (signature, overloads, etc.)
141 struct function_record {
142     function_record()
143         : is_constructor(false), is_new_style_constructor(false), is_stateless(false),
144           is_operator(false), is_method(false), has_args(false),
145           has_kwargs(false), has_kw_only_args(false), prepend(false) { }
146 
147     /// Function name
148     char *name = nullptr; /* why no C++ strings? They generate heavier code.. */
149 
150     // User-specified documentation string
151     char *doc = nullptr;
152 
153     /// Human-readable version of the function signature
154     char *signature = nullptr;
155 
156     /// List of registered keyword arguments
157     std::vector<argument_record> args;
158 
159     /// Pointer to lambda function which converts arguments and performs the actual call
160     handle (*impl) (function_call &) = nullptr;
161 
162     /// Storage for the wrapped function pointer and captured data, if any
163     void *data[3] = { };
164 
165     /// Pointer to custom destructor for 'data' (if needed)
166     void (*free_data) (function_record *ptr) = nullptr;
167 
168     /// Return value policy associated with this function
169     return_value_policy policy = return_value_policy::automatic;
170 
171     /// True if name == '__init__'
172     bool is_constructor : 1;
173 
174     /// True if this is a new-style `__init__` defined in `detail/init.h`
175     bool is_new_style_constructor : 1;
176 
177     /// True if this is a stateless function pointer
178     bool is_stateless : 1;
179 
180     /// True if this is an operator (__add__), etc.
181     bool is_operator : 1;
182 
183     /// True if this is a method
184     bool is_method : 1;
185 
186     /// True if the function has a '*args' argument
187     bool has_args : 1;
188 
189     /// True if the function has a '**kwargs' argument
190     bool has_kwargs : 1;
191 
192     /// True once a 'py::kw_only' is encountered (any following args are keyword-only)
193     bool has_kw_only_args : 1;
194 
195     /// True if this function is to be inserted at the beginning of the overload resolution chain
196     bool prepend : 1;
197 
198     /// Number of arguments (including py::args and/or py::kwargs, if present)
199     std::uint16_t nargs;
200 
201     /// Number of trailing arguments (counted in `nargs`) that are keyword-only
202     std::uint16_t nargs_kw_only = 0;
203 
204     /// Number of leading arguments (counted in `nargs`) that are positional-only
205     std::uint16_t nargs_pos_only = 0;
206 
207     /// Python method object
208     PyMethodDef *def = nullptr;
209 
210     /// Python handle to the parent scope (a class or a module)
211     handle scope;
212 
213     /// Python handle to the sibling function representing an overload chain
214     handle sibling;
215 
216     /// Pointer to next overload
217     function_record *next = nullptr;
218 };
219 
220 /// Special data structure which (temporarily) holds metadata about a bound class
221 struct type_record {
222     PYBIND11_NOINLINE type_record()
223         : multiple_inheritance(false), dynamic_attr(false), buffer_protocol(false),
224           default_holder(true), module_local(false), is_final(false) { }
225 
226     /// Handle to the parent scope
227     handle scope;
228 
229     /// Name of the class
230     const char *name = nullptr;
231 
232     // Pointer to RTTI type_info data structure
233     const std::type_info *type = nullptr;
234 
235     /// How large is the underlying C++ type?
236     size_t type_size = 0;
237 
238     /// What is the alignment of the underlying C++ type?
239     size_t type_align = 0;
240 
241     /// How large is the type's holder?
242     size_t holder_size = 0;
243 
244     /// The global operator new can be overridden with a class-specific variant
245     void *(*operator_new)(size_t) = nullptr;
246 
247     /// Function pointer to class_<..>::init_instance
248     void (*init_instance)(instance *, const void *) = nullptr;
249 
250     /// Function pointer to class_<..>::dealloc
251     void (*dealloc)(detail::value_and_holder &) = nullptr;
252 
253     /// List of base classes of the newly created type
254     list bases;
255 
256     /// Optional docstring
257     const char *doc = nullptr;
258 
259     /// Custom metaclass (optional)
260     handle metaclass;
261 
262     /// Multiple inheritance marker
263     bool multiple_inheritance : 1;
264 
265     /// Does the class manage a __dict__?
266     bool dynamic_attr : 1;
267 
268     /// Does the class implement the buffer protocol?
269     bool buffer_protocol : 1;
270 
271     /// Is the default (unique_ptr) holder type used?
272     bool default_holder : 1;
273 
274     /// Is the class definition local to the module shared object?
275     bool module_local : 1;
276 
277     /// Is the class inheritable from python classes?
278     bool is_final : 1;
279 
280     PYBIND11_NOINLINE void add_base(const std::type_info &base, void *(*caster)(void *)) {
281         auto base_info = detail::get_type_info(base, false);
282         if (!base_info) {
283             std::string tname(base.name());
284             detail::clean_type_id(tname);
285             pybind11_fail("generic_type: type \"" + std::string(name) +
286                           "\" referenced unknown base type \"" + tname + "\"");
287         }
288 
289         if (default_holder != base_info->default_holder) {
290             std::string tname(base.name());
291             detail::clean_type_id(tname);
292             pybind11_fail("generic_type: type \"" + std::string(name) + "\" " +
293                     (default_holder ? "does not have" : "has") +
294                     " a non-default holder type while its base \"" + tname + "\" " +
295                     (base_info->default_holder ? "does not" : "does"));
296         }
297 
298         bases.append((PyObject *) base_info->type);
299 
300         if (base_info->type->tp_dictoffset != 0)
301             dynamic_attr = true;
302 
303         if (caster)
304             base_info->implicit_casts.emplace_back(type, caster);
305     }
306 };
307 
308 inline function_call::function_call(const function_record &f, handle p) :
309         func(f), parent(p) {
310     args.reserve(f.nargs);
311     args_convert.reserve(f.nargs);
312 }
313 
314 /// Tag for a new-style `__init__` defined in `detail/init.h`
315 struct is_new_style_constructor { };
316 
317 /**
318  * Partial template specializations to process custom attributes provided to
319  * cpp_function_ and class_. These are either used to initialize the respective
320  * fields in the type_record and function_record data structures or executed at
321  * runtime to deal with custom call policies (e.g. keep_alive).
322  */
323 template <typename T, typename SFINAE = void> struct process_attribute;
324 
325 template <typename T> struct process_attribute_default {
326     /// Default implementation: do nothing
327     static void init(const T &, function_record *) { }
328     static void init(const T &, type_record *) { }
329     static void precall(function_call &) { }
330     static void postcall(function_call &, handle) { }
331 };
332 
333 /// Process an attribute specifying the function's name
334 template <> struct process_attribute<name> : process_attribute_default<name> {
335     static void init(const name &n, function_record *r) { r->name = const_cast<char *>(n.value); }
336 };
337 
338 /// Process an attribute specifying the function's docstring
339 template <> struct process_attribute<doc> : process_attribute_default<doc> {
340     static void init(const doc &n, function_record *r) { r->doc = const_cast<char *>(n.value); }
341 };
342 
343 /// Process an attribute specifying the function's docstring (provided as a C-style string)
344 template <> struct process_attribute<const char *> : process_attribute_default<const char *> {
345     static void init(const char *d, function_record *r) { r->doc = const_cast<char *>(d); }
346     static void init(const char *d, type_record *r) { r->doc = const_cast<char *>(d); }
347 };
348 template <> struct process_attribute<char *> : process_attribute<const char *> { };
349 
350 /// Process an attribute indicating the function's return value policy
351 template <> struct process_attribute<return_value_policy> : process_attribute_default<return_value_policy> {
352     static void init(const return_value_policy &p, function_record *r) { r->policy = p; }
353 };
354 
355 /// Process an attribute which indicates that this is an overloaded function associated with a given sibling
356 template <> struct process_attribute<sibling> : process_attribute_default<sibling> {
357     static void init(const sibling &s, function_record *r) { r->sibling = s.value; }
358 };
359 
360 /// Process an attribute which indicates that this function is a method
361 template <> struct process_attribute<is_method> : process_attribute_default<is_method> {
362     static void init(const is_method &s, function_record *r) { r->is_method = true; r->scope = s.class_; }
363 };
364 
365 /// Process an attribute which indicates the parent scope of a method
366 template <> struct process_attribute<scope> : process_attribute_default<scope> {
367     static void init(const scope &s, function_record *r) { r->scope = s.value; }
368 };
369 
370 /// Process an attribute which indicates that this function is an operator
371 template <> struct process_attribute<is_operator> : process_attribute_default<is_operator> {
372     static void init(const is_operator &, function_record *r) { r->is_operator = true; }
373 };
374 
375 template <> struct process_attribute<is_new_style_constructor> : process_attribute_default<is_new_style_constructor> {
376     static void init(const is_new_style_constructor &, function_record *r) { r->is_new_style_constructor = true; }
377 };
378 
379 inline void process_kw_only_arg(const arg &a, function_record *r) {
380     if (!a.name || strlen(a.name) == 0)
381         pybind11_fail("arg(): cannot specify an unnamed argument after an kw_only() annotation");
382     ++r->nargs_kw_only;
383 }
384 
385 /// Process a keyword argument attribute (*without* a default value)
386 template <> struct process_attribute<arg> : process_attribute_default<arg> {
387     static void init(const arg &a, function_record *r) {
388         if (r->is_method && r->args.empty())
389             r->args.emplace_back("self", nullptr, handle(), true /*convert*/, false /*none not allowed*/);
390         r->args.emplace_back(a.name, nullptr, handle(), !a.flag_noconvert, a.flag_none);
391 
392         if (r->has_kw_only_args) process_kw_only_arg(a, r);
393     }
394 };
395 
396 /// Process a keyword argument attribute (*with* a default value)
397 template <> struct process_attribute<arg_v> : process_attribute_default<arg_v> {
398     static void init(const arg_v &a, function_record *r) {
399         if (r->is_method && r->args.empty())
400             r->args.emplace_back("self", nullptr /*descr*/, handle() /*parent*/, true /*convert*/, false /*none not allowed*/);
401 
402         if (!a.value) {
403 #if !defined(NDEBUG)
404             std::string descr("'");
405             if (a.name) descr += std::string(a.name) + ": ";
406             descr += a.type + "'";
407             if (r->is_method) {
408                 if (r->name)
409                     descr += " in method '" + (std::string) str(r->scope) + "." + (std::string) r->name + "'";
410                 else
411                     descr += " in method of '" + (std::string) str(r->scope) + "'";
412             } else if (r->name) {
413                 descr += " in function '" + (std::string) r->name + "'";
414             }
415             pybind11_fail("arg(): could not convert default argument "
416                           + descr + " into a Python object (type not registered yet?)");
417 #else
418             pybind11_fail("arg(): could not convert default argument "
419                           "into a Python object (type not registered yet?). "
420                           "Compile in debug mode for more information.");
421 #endif
422         }
423         r->args.emplace_back(a.name, a.descr, a.value.inc_ref(), !a.flag_noconvert, a.flag_none);
424 
425         if (r->has_kw_only_args) process_kw_only_arg(a, r);
426     }
427 };
428 
429 /// Process a keyword-only-arguments-follow pseudo argument
430 template <> struct process_attribute<kw_only> : process_attribute_default<kw_only> {
431     static void init(const kw_only &, function_record *r) {
432         r->has_kw_only_args = true;
433     }
434 };
435 
436 /// Process a positional-only-argument maker
437 template <> struct process_attribute<pos_only> : process_attribute_default<pos_only> {
438     static void init(const pos_only &, function_record *r) {
439         r->nargs_pos_only = static_cast<std::uint16_t>(r->args.size());
440     }
441 };
442 
443 /// Process a parent class attribute.  Single inheritance only (class_ itself already guarantees that)
444 template <typename T>
445 struct process_attribute<T, enable_if_t<is_pyobject<T>::value>> : process_attribute_default<handle> {
446     static void init(const handle &h, type_record *r) { r->bases.append(h); }
447 };
448 
449 /// Process a parent class attribute (deprecated, does not support multiple inheritance)
450 template <typename T>
451 struct process_attribute<base<T>> : process_attribute_default<base<T>> {
452     static void init(const base<T> &, type_record *r) { r->add_base(typeid(T), nullptr); }
453 };
454 
455 /// Process a multiple inheritance attribute
456 template <>
457 struct process_attribute<multiple_inheritance> : process_attribute_default<multiple_inheritance> {
458     static void init(const multiple_inheritance &, type_record *r) { r->multiple_inheritance = true; }
459 };
460 
461 template <>
462 struct process_attribute<dynamic_attr> : process_attribute_default<dynamic_attr> {
463     static void init(const dynamic_attr &, type_record *r) { r->dynamic_attr = true; }
464 };
465 
466 template <>
467 struct process_attribute<is_final> : process_attribute_default<is_final> {
468     static void init(const is_final &, type_record *r) { r->is_final = true; }
469 };
470 
471 template <>
472 struct process_attribute<buffer_protocol> : process_attribute_default<buffer_protocol> {
473     static void init(const buffer_protocol &, type_record *r) { r->buffer_protocol = true; }
474 };
475 
476 template <>
477 struct process_attribute<metaclass> : process_attribute_default<metaclass> {
478     static void init(const metaclass &m, type_record *r) { r->metaclass = m.value; }
479 };
480 
481 template <>
482 struct process_attribute<module_local> : process_attribute_default<module_local> {
483     static void init(const module_local &l, type_record *r) { r->module_local = l.value; }
484 };
485 
486 /// Process a 'prepend' attribute, putting this at the beginning of the overload chain
487 template <>
488 struct process_attribute<prepend> : process_attribute_default<prepend> {
489     static void init(const prepend &, function_record *r) { r->prepend = true; }
490 };
491 
492 /// Process an 'arithmetic' attribute for enums (does nothing here)
493 template <>
494 struct process_attribute<arithmetic> : process_attribute_default<arithmetic> {};
495 
496 template <typename... Ts>
497 struct process_attribute<call_guard<Ts...>> : process_attribute_default<call_guard<Ts...>> { };
498 
499 /**
500  * Process a keep_alive call policy -- invokes keep_alive_impl during the
501  * pre-call handler if both Nurse, Patient != 0 and use the post-call handler
502  * otherwise
503  */
504 template <size_t Nurse, size_t Patient> struct process_attribute<keep_alive<Nurse, Patient>> : public process_attribute_default<keep_alive<Nurse, Patient>> {
505     template <size_t N = Nurse, size_t P = Patient, enable_if_t<N != 0 && P != 0, int> = 0>
506     static void precall(function_call &call) { keep_alive_impl(Nurse, Patient, call, handle()); }
507     template <size_t N = Nurse, size_t P = Patient, enable_if_t<N != 0 && P != 0, int> = 0>
508     static void postcall(function_call &, handle) { }
509     template <size_t N = Nurse, size_t P = Patient, enable_if_t<N == 0 || P == 0, int> = 0>
510     static void precall(function_call &) { }
511     template <size_t N = Nurse, size_t P = Patient, enable_if_t<N == 0 || P == 0, int> = 0>
512     static void postcall(function_call &call, handle ret) { keep_alive_impl(Nurse, Patient, call, ret); }
513 };
514 
515 /// Recursively iterate over variadic template arguments
516 template <typename... Args> struct process_attributes {
517     static void init(const Args&... args, function_record *r) {
518         int unused[] = { 0, (process_attribute<typename std::decay<Args>::type>::init(args, r), 0) ... };
519         ignore_unused(unused);
520     }
521     static void init(const Args&... args, type_record *r) {
522         int unused[] = { 0, (process_attribute<typename std::decay<Args>::type>::init(args, r), 0) ... };
523         ignore_unused(unused);
524     }
525     static void precall(function_call &call) {
526         int unused[] = { 0, (process_attribute<typename std::decay<Args>::type>::precall(call), 0) ... };
527         ignore_unused(unused);
528     }
529     static void postcall(function_call &call, handle fn_ret) {
530         int unused[] = { 0, (process_attribute<typename std::decay<Args>::type>::postcall(call, fn_ret), 0) ... };
531         ignore_unused(unused);
532     }
533 };
534 
535 template <typename T>
536 using is_call_guard = is_instantiation<call_guard, T>;
537 
538 /// Extract the ``type`` from the first `call_guard` in `Extras...` (or `void_type` if none found)
539 template <typename... Extra>
540 using extract_guard_t = typename exactly_one_t<is_call_guard, call_guard<>, Extra...>::type;
541 
542 /// Check the number of named arguments at compile time
543 template <typename... Extra,
544           size_t named = constexpr_sum(std::is_base_of<arg, Extra>::value...),
545           size_t self  = constexpr_sum(std::is_same<is_method, Extra>::value...)>
546 constexpr bool expected_num_args(size_t nargs, bool has_args, bool has_kwargs) {
547     return named == 0 || (self + named + size_t(has_args) + size_t(has_kwargs)) == nargs;
548 }
549 
550 PYBIND11_NAMESPACE_END(detail)
551 PYBIND11_NAMESPACE_END(PYBIND11_NAMESPACE)
552