attr.h
Go to the documentation of this file.
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 
16 
17 
21 struct is_method { handle class_; is_method(const handle &c) : class_(c) { } };
22 
24 struct is_operator { };
25 
27 struct is_final { };
28 
30 struct scope { handle value; scope(const handle &s) : value(s) { } };
31 
33 struct doc { const char *value; doc(const char *value) : value(value) { } };
34 
36 struct name { const char *value; name(const char *value) : value(value) { } };
37 
39 struct sibling { handle value; sibling(const handle &value) : value(value.ptr()) { } };
40 
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_")
45  base() { } // NOLINT(modernize-use-equals-default): breaks MSVC 2015 when adding an attribute
46 };
47 
49 template <size_t Nurse, size_t Patient> struct keep_alive { };
50 
53 
55 struct dynamic_attr { };
56 
58 struct buffer_protocol { };
59 
61 struct metaclass {
63 
64  PYBIND11_DEPRECATED("py::metaclass() is no longer required. It's turned on by default now.")
65  metaclass() { } // NOLINT(modernize-use-equals-default): breaks MSVC 2015 when adding an attribute
66 
68  explicit metaclass(handle value) : value(value) { }
69 };
70 
72 struct module_local { const bool value; constexpr module_local(bool v = true) : value(v) { } };
73 
75 struct arithmetic { };
76 
95 template <typename... Ts> struct call_guard;
96 
97 template <> struct call_guard<> { using type = detail::void_type; };
98 
99 template <typename T>
100 struct call_guard<T> {
102  "The guard type must be default constructible");
103 
104  using type = T;
105 };
106 
107 template <typename T, typename... Ts>
108 struct call_guard<T, Ts...> {
109  struct type {
110  T guard{}; // Compose multiple guard types with left-to-right default-constructor order
111  typename call_guard<Ts...>::type next{};
112  };
113 };
114 
116 
118 /* Forward declarations */
119 enum op_id : int;
120 enum op_type : int;
121 struct undefined_t;
122 template <op_id id, op_type ot, typename L = undefined_t, typename R = undefined_t> struct op_;
123 inline void keep_alive_impl(size_t Nurse, size_t Patient, function_call &call, handle ret);
124 
127  const char *name;
128  const char *descr;
130  bool convert : 1;
131  bool none : 1;
132 
133  argument_record(const char *name, const char *descr, handle value, bool convert, bool none)
134  : name(name), descr(descr), value(value), convert(convert), none(none) { }
135 };
136 
140  : is_constructor(false), is_new_style_constructor(false), is_stateless(false),
141  is_operator(false), is_method(false),
142  has_args(false), has_kwargs(false), has_kw_only_args(false) { }
143 
145  char *name = nullptr; /* why no C++ strings? They generate heavier code.. */
146 
147  // User-specified documentation string
148  char *doc = nullptr;
149 
151  char *signature = nullptr;
152 
154  std::vector<argument_record> args;
155 
157  handle (*impl) (function_call &) = nullptr;
158 
160  void *data[3] = { };
161 
163  void (*free_data) (function_record *ptr) = nullptr;
164 
167 
169  bool is_constructor : 1;
170 
173 
175  bool is_stateless : 1;
176 
178  bool is_operator : 1;
179 
181  bool is_method : 1;
182 
184  bool has_args : 1;
185 
187  bool has_kwargs : 1;
188 
190  bool has_kw_only_args : 1;
191 
194 
196  std::uint16_t nargs_kw_only = 0;
197 
199  std::uint16_t nargs_pos_only = 0;
200 
202  PyMethodDef *def = nullptr;
203 
206 
209 
211  function_record *next = nullptr;
212 };
213 
215 struct type_record {
217  : multiple_inheritance(false), dynamic_attr(false), buffer_protocol(false),
218  default_holder(true), module_local(false), is_final(false) { }
219 
222 
224  const char *name = nullptr;
225 
226  // Pointer to RTTI type_info data structure
227  const std::type_info *type = nullptr;
228 
230  size_t type_size = 0;
231 
233  size_t type_align = 0;
234 
236  size_t holder_size = 0;
237 
239  void *(*operator_new)(size_t) = nullptr;
240 
242  void (*init_instance)(instance *, const void *) = nullptr;
243 
245  void (*dealloc)(detail::value_and_holder &) = nullptr;
246 
249 
251  const char *doc = nullptr;
252 
255 
258 
260  bool dynamic_attr : 1;
261 
263  bool buffer_protocol : 1;
264 
266  bool default_holder : 1;
267 
269  bool module_local : 1;
270 
272  bool is_final : 1;
273 
274  PYBIND11_NOINLINE void add_base(const std::type_info &base, void *(*caster)(void *)) {
275  auto base_info = detail::get_type_info(base, false);
276  if (!base_info) {
277  std::string tname(base.name());
278  detail::clean_type_id(tname);
279  pybind11_fail("generic_type: type \"" + std::string(name) +
280  "\" referenced unknown base type \"" + tname + "\"");
281  }
282 
283  if (default_holder != base_info->default_holder) {
284  std::string tname(base.name());
285  detail::clean_type_id(tname);
286  pybind11_fail("generic_type: type \"" + std::string(name) + "\" " +
287  (default_holder ? "does not have" : "has") +
288  " a non-default holder type while its base \"" + tname + "\" " +
289  (base_info->default_holder ? "does not" : "does"));
290  }
291 
292  bases.append((PyObject *) base_info->type);
293 
294  if (base_info->type->tp_dictoffset != 0)
295  dynamic_attr = true;
296 
297  if (caster)
298  base_info->implicit_casts.emplace_back(type, caster);
299  }
300 };
301 
303  func(f), parent(p) {
304  args.reserve(f.nargs);
305  args_convert.reserve(f.nargs);
306 }
307 
310 
317 template <typename T, typename SFINAE = void> struct process_attribute;
318 
319 template <typename T> struct process_attribute_default {
321  static void init(const T &, function_record *) { }
322  static void init(const T &, type_record *) { }
323  static void precall(function_call &) { }
324  static void postcall(function_call &, handle) { }
325 };
326 
328 template <> struct process_attribute<name> : process_attribute_default<name> {
329  static void init(const name &n, function_record *r) { r->name = const_cast<char *>(n.value); }
330 };
331 
333 template <> struct process_attribute<doc> : process_attribute_default<doc> {
334  static void init(const doc &n, function_record *r) { r->doc = const_cast<char *>(n.value); }
335 };
336 
338 template <> struct process_attribute<const char *> : process_attribute_default<const char *> {
339  static void init(const char *d, function_record *r) { r->doc = const_cast<char *>(d); }
340  static void init(const char *d, type_record *r) { r->doc = const_cast<char *>(d); }
341 };
342 template <> struct process_attribute<char *> : process_attribute<const char *> { };
343 
345 template <> struct process_attribute<return_value_policy> : process_attribute_default<return_value_policy> {
346  static void init(const return_value_policy &p, function_record *r) { r->policy = p; }
347 };
348 
350 template <> struct process_attribute<sibling> : process_attribute_default<sibling> {
351  static void init(const sibling &s, function_record *r) { r->sibling = s.value; }
352 };
353 
355 template <> struct process_attribute<is_method> : process_attribute_default<is_method> {
356  static void init(const is_method &s, function_record *r) { r->is_method = true; r->scope = s.class_; }
357 };
358 
360 template <> struct process_attribute<scope> : process_attribute_default<scope> {
361  static void init(const scope &s, function_record *r) { r->scope = s.value; }
362 };
363 
365 template <> struct process_attribute<is_operator> : process_attribute_default<is_operator> {
366  static void init(const is_operator &, function_record *r) { r->is_operator = true; }
367 };
368 
369 template <> struct process_attribute<is_new_style_constructor> : process_attribute_default<is_new_style_constructor> {
371 };
372 
373 inline void process_kw_only_arg(const arg &a, function_record *r) {
374  if (!a.name || strlen(a.name) == 0)
375  pybind11_fail("arg(): cannot specify an unnamed argument after an kw_only() annotation");
376  ++r->nargs_kw_only;
377 }
378 
380 template <> struct process_attribute<arg> : process_attribute_default<arg> {
381  static void init(const arg &a, function_record *r) {
382  if (r->is_method && r->args.empty())
383  r->args.emplace_back("self", nullptr, handle(), true /*convert*/, false /*none not allowed*/);
384  r->args.emplace_back(a.name, nullptr, handle(), !a.flag_noconvert, a.flag_none);
385 
387  }
388 };
389 
391 template <> struct process_attribute<arg_v> : process_attribute_default<arg_v> {
392  static void init(const arg_v &a, function_record *r) {
393  if (r->is_method && r->args.empty())
394  r->args.emplace_back("self", nullptr /*descr*/, handle() /*parent*/, true /*convert*/, false /*none not allowed*/);
395 
396  if (!a.value) {
397 #if !defined(NDEBUG)
398  std::string descr("'");
399  if (a.name) descr += std::string(a.name) + ": ";
400  descr += a.type + "'";
401  if (r->is_method) {
402  if (r->name)
403  descr += " in method '" + (std::string) str(r->scope) + "." + (std::string) r->name + "'";
404  else
405  descr += " in method of '" + (std::string) str(r->scope) + "'";
406  } else if (r->name) {
407  descr += " in function '" + (std::string) r->name + "'";
408  }
409  pybind11_fail("arg(): could not convert default argument "
410  + descr + " into a Python object (type not registered yet?)");
411 #else
412  pybind11_fail("arg(): could not convert default argument "
413  "into a Python object (type not registered yet?). "
414  "Compile in debug mode for more information.");
415 #endif
416  }
417  r->args.emplace_back(a.name, a.descr, a.value.inc_ref(), !a.flag_noconvert, a.flag_none);
418 
420  }
421 };
422 
424 template <> struct process_attribute<kw_only> : process_attribute_default<kw_only> {
425  static void init(const kw_only &, function_record *r) {
426  r->has_kw_only_args = true;
427  }
428 };
429 
431 template <> struct process_attribute<pos_only> : process_attribute_default<pos_only> {
432  static void init(const pos_only &, function_record *r) {
433  r->nargs_pos_only = static_cast<std::uint16_t>(r->args.size());
434  }
435 };
436 
438 template <typename T>
440  static void init(const handle &h, type_record *r) { r->bases.append(h); }
441 };
442 
444 template <typename T>
446  static void init(const base<T> &, type_record *r) { r->add_base(typeid(T), nullptr); }
447 };
448 
450 template <>
452  static void init(const multiple_inheritance &, type_record *r) { r->multiple_inheritance = true; }
453 };
454 
455 template <>
457  static void init(const dynamic_attr &, type_record *r) { r->dynamic_attr = true; }
458 };
459 
460 template <>
462  static void init(const is_final &, type_record *r) { r->is_final = true; }
463 };
464 
465 template <>
467  static void init(const buffer_protocol &, type_record *r) { r->buffer_protocol = true; }
468 };
469 
470 template <>
471 struct process_attribute<metaclass> : process_attribute_default<metaclass> {
472  static void init(const metaclass &m, type_record *r) { r->metaclass = m.value; }
473 };
474 
475 template <>
477  static void init(const module_local &l, type_record *r) { r->module_local = l.value; }
478 };
479 
481 template <>
483 
484 template <typename... Ts>
485 struct process_attribute<call_guard<Ts...>> : process_attribute_default<call_guard<Ts...>> { };
486 
492 template <size_t Nurse, size_t Patient> struct process_attribute<keep_alive<Nurse, Patient>> : public process_attribute_default<keep_alive<Nurse, Patient>> {
493  template <size_t N = Nurse, size_t P = Patient, enable_if_t<N != 0 && P != 0, int> = 0>
494  static void precall(function_call &call) { keep_alive_impl(Nurse, Patient, call, handle()); }
495  template <size_t N = Nurse, size_t P = Patient, enable_if_t<N != 0 && P != 0, int> = 0>
496  static void postcall(function_call &, handle) { }
497  template <size_t N = Nurse, size_t P = Patient, enable_if_t<N == 0 || P == 0, int> = 0>
498  static void precall(function_call &) { }
499  template <size_t N = Nurse, size_t P = Patient, enable_if_t<N == 0 || P == 0, int> = 0>
500  static void postcall(function_call &call, handle ret) { keep_alive_impl(Nurse, Patient, call, ret); }
501 };
502 
504 template <typename... Args> struct process_attributes {
505  static void init(const Args&... args, function_record *r) {
506  int unused[] = { 0, (process_attribute<typename std::decay<Args>::type>::init(args, r), 0) ... };
507  ignore_unused(unused);
508  }
509  static void init(const Args&... args, type_record *r) {
510  int unused[] = { 0, (process_attribute<typename std::decay<Args>::type>::init(args, r), 0) ... };
511  ignore_unused(unused);
512  }
513  static void precall(function_call &call) {
514  int unused[] = { 0, (process_attribute<typename std::decay<Args>::type>::precall(call), 0) ... };
515  ignore_unused(unused);
516  }
517  static void postcall(function_call &call, handle fn_ret) {
518  int unused[] = { 0, (process_attribute<typename std::decay<Args>::type>::postcall(call, fn_ret), 0) ... };
519  ignore_unused(unused);
520  }
521 };
522 
523 template <typename T>
525 
527 template <typename... Extra>
529 
531 template <typename... Extra,
534 constexpr bool expected_num_args(size_t nargs, bool has_args, bool has_kwargs) {
535  return named == 0 || (self + named + has_args + has_kwargs) == nargs;
536 }
537 
handle scope
Python handle to the parent scope (a class or a module)
Definition: attr.h:205
static void precall(function_call &)
Definition: attr.h:323
Matrix3f m
void ignore_unused(const int *)
Ignore that a variable is unused in compiler warnings.
Annotation which enables dynamic attributes, i.e. adds __dict__ to a class.
Definition: attr.h:55
handle class_
Definition: attr.h:21
Annotation for parent scope.
Definition: attr.h:30
bool is_final
Is the class inheritable from python classes?
Definition: attr.h:272
std::uint16_t nargs
Number of arguments (including py::args and/or py::kwargs, if present)
Definition: attr.h:193
Operator implementation generator.
Definition: attr.h:122
is_method(const handle &c)
Definition: attr.h:21
PYBIND11_NOINLINE type_record()
Definition: attr.h:216
static void postcall(function_call &call, handle ret)
Definition: attr.h:500
return int(ret)+1
void append(T &&val) const
Definition: pytypes.h:1313
Keep patient alive while nurse lives.
Definition: attr.h:49
static void init(const scope &s, function_record *r)
Definition: attr.h:361
return_value_policy policy
Return value policy associated with this function.
Definition: attr.h:166
static void init(const buffer_protocol &, type_record *r)
Definition: attr.h:467
PyObject * ptr() const
Return the underlying PyObject * pointer.
Definition: pytypes.h:184
handle sibling
Python handle to the sibling function representing an overload chain.
Definition: attr.h:208
Annotation which requests that a special metaclass is created for a type.
Definition: attr.h:61
static void init(const doc &n, function_record *r)
Definition: attr.h:334
ArrayXcf v
Definition: Cwise_arg.cpp:1
Definition: pytypes.h:1322
Annotation for documentation.
Definition: attr.h:33
Definition: cast.h:1870
Tag for a new-style __init__ defined in detail/init.h
Definition: attr.h:309
Type for an unused type slot.
Definition: operators.h:44
int n
bool has_kw_only_args
True once a &#39;py::kw_only&#39; is encountered (any following args are keyword-only)
Definition: attr.h:190
static void init(const kw_only &, function_record *r)
Definition: attr.h:425
Scalar Scalar * c
Definition: benchVecAdd.cpp:17
handle value
Definition: attr.h:62
object value
The default value.
Definition: cast.h:1902
bool buffer_protocol
Does the class implement the buffer protocol?
Definition: attr.h:263
static void init(const arg_v &a, function_record *r)
Definition: attr.h:392
static void init(const dynamic_attr &, type_record *r)
Definition: attr.h:457
argument_record(const char *name, const char *descr, handle value, bool convert, bool none)
Definition: attr.h:133
const char * doc
Optional docstring.
Definition: attr.h:251
bool multiple_inheritance
Multiple inheritance marker.
Definition: attr.h:257
const char * value
Definition: attr.h:33
Annotation for operators.
Definition: attr.h:24
unsigned short uint16_t
Definition: ms_stdint.h:84
static void postcall(function_call &, handle)
Definition: attr.h:496
static void postcall(function_call &call, handle fn_ret)
Definition: attr.h:517
static void init(const char *d, type_record *r)
Definition: attr.h:340
typename exactly_one< Predicate, Default, Ts... >::type exactly_one_t
Definition: cast.h:1853
static void init(const arg &a, function_record *r)
Definition: attr.h:381
PYBIND11_NOINLINE void add_base(const std::type_info &base, void *(*caster)(void *))
Definition: attr.h:274
Internal data associated with a single function call.
Definition: cast.h:1940
handle value
Associated Python object.
Definition: attr.h:129
static void init(const base< T > &, type_record *r)
Definition: attr.h:446
The &#39;instance&#39; type which needs to be standard layout (need to be able to use &#39;offsetof&#39;) ...
Definition: pytypes.h:1057
op_id
Enumeration with all supported operator types.
Definition: operators.h:25
Process an attribute specifying the function&#39;s docstring (provided as a C-style string) ...
Definition: attr.h:338
PYBIND11_NOINLINE void clean_type_id(std::string &name)
Definition: typeid.h:32
constexpr size_t constexpr_sum()
Compile-time integer sum.
Array33i a
PyExc_RuntimeError[[noreturn]] PYBIND11_NOINLINE void pybind11_fail(const char *reason)
Used internally.
static void init(const sibling &s, function_record *r)
Definition: attr.h:351
const handle & inc_ref() const &
Definition: pytypes.h:192
const char * descr
Human-readable version of the argument value.
Definition: attr.h:128
handle metaclass
Custom metaclass (optional)
Definition: attr.h:254
static void init(const module_local &l, type_record *r)
Definition: attr.h:477
static const Line3 l(Rot3(), 1, 1)
Definition: descr.h:25
void keep_alive_impl(size_t Nurse, size_t Patient, function_call &call, handle ret)
Definition: pybind11.h:1716
handle value
Definition: attr.h:39
const char * name
Argument name.
Definition: attr.h:127
static void init(const Args &...args, type_record *r)
Definition: attr.h:509
handle scope
Handle to the parent scope.
Definition: attr.h:221
static void init(const is_final &, type_record *r)
Definition: attr.h:462
Definition: cast.h:1914
static void init(const pos_only &, function_record *r)
Definition: attr.h:432
Annotation that marks a class as local to the module:
Definition: attr.h:72
std::uint16_t nargs_pos_only
Number of leading arguments (counted in nargs) that are positional-only.
Definition: attr.h:199
list bases
List of base classes of the newly created type.
Definition: attr.h:248
float * ptr
function_call(const function_record &f, handle p)
Definition: attr.h:302
std::vector< bool > args_convert
The convert value the arguments should be loaded with.
Definition: cast.h:1950
bool convert(const int &y)
metaclass(handle value)
Override pybind11&#39;s default metaclass.
Definition: attr.h:68
std::uint16_t nargs_kw_only
Number of trailing arguments (counted in nargs) that are keyword-only.
Definition: attr.h:196
static void init(const T &, function_record *)
Default implementation: do nothing.
Definition: attr.h:321
static void precall(function_call &call)
Definition: attr.h:513
Eigen::Triplet< double > T
int data[]
constexpr bool expected_num_args(size_t nargs, bool has_args, bool has_kwargs)
Check the number of named arguments at compile time.
Definition: attr.h:534
Point2(* f)(const Point3 &, OptionalJacobian< 2, 3 >)
bool is_method
True if this is a method.
Definition: attr.h:181
Recursively iterate over variadic template arguments.
Definition: attr.h:504
RealScalar s
#define PYBIND11_DEPRECATED(reason)
bool module_local
Is the class definition local to the module shared object?
Definition: attr.h:269
static void precall(function_call &call)
Definition: attr.h:494
Annotation which enables the buffer protocol for a type.
Definition: attr.h:58
const char * value
Definition: attr.h:36
static void init(const name &n, function_record *r)
Definition: attr.h:329
static void init(const T &, type_record *)
Definition: attr.h:322
static void init(const handle &h, type_record *r)
Definition: attr.h:440
Definition: pytypes.h:1301
static void init(const Args &...args, function_record *r)
Definition: attr.h:505
function_record()
Definition: attr.h:139
Annotation indicating that a class is involved in a multiple inheritance relationship.
Definition: attr.h:52
const double h
Annotation to mark enums as an arithmetic type.
Definition: attr.h:75
static void init(const metaclass &m, type_record *r)
Definition: attr.h:472
const bool value
Definition: attr.h:72
def doc()
Definition: conftest.py:157
std::is_base_of< pyobject_tag, remove_reference_t< T >> is_pyobject
Definition: pytypes.h:48
static void init(const is_operator&, function_record *r)
Definition: attr.h:366
typename exactly_one_t< is_call_guard, call_guard<>, Extra... >::type extract_guard_t
Extract the type from the first call_guard in Extras... (or void_type if none found) ...
Definition: attr.h:528
Annotation for classes that cannot be subclassed.
Definition: attr.h:27
static void init(const is_method &s, function_record *r)
Definition: attr.h:356
Special data structure which (temporarily) holds metadata about a bound class.
Definition: attr.h:215
Annotation for methods.
Definition: attr.h:21
PYBIND11_NOINLINE detail::type_info * get_type_info(PyTypeObject *type)
Definition: cast.h:164
void process_kw_only_arg(const arg &a, function_record *r)
Definition: attr.h:373
static void init(const char *d, function_record *r)
Definition: attr.h:339
DenseIndex ret
Definition: level1_impl.h:59
static void postcall(function_call &, handle)
Definition: attr.h:324
detail::initimpl::constructor< Args... > init()
Binds an existing constructor taking arguments Args...
Definition: pybind11.h:1460
char * doc
Definition: attr.h:148
std::vector< argument_record > args
List of registered keyword arguments.
Definition: attr.h:154
static void init(const return_value_policy &p, function_record *r)
Definition: attr.h:346
float * p
char * name
Function name.
Definition: attr.h:145
bool is_operator
True if this is an operator (add), etc.
Definition: attr.h:178
typename std::enable_if< B, T >::type enable_if_t
from cpp_future import (convenient aliases from C++14/17)
const char * descr
The (optional) description of the default value.
Definition: cast.h:1904
static void init(const is_new_style_constructor &, function_record *r)
Definition: attr.h:370
bool is_new_style_constructor
True if this is a new-style __init__ defined in detail/init.h
Definition: attr.h:172
Annotation for function names.
Definition: attr.h:36
const char * name
If non-null, this is a named kwargs argument.
Definition: cast.h:1863
Annotation indicating that a class derives from another given type.
Definition: attr.h:42
static void init(const multiple_inheritance &, type_record *r)
Definition: attr.h:452
Annotation indicating that a function is an overload associated with a given "sibling".
Definition: attr.h:39
Internal data structure which holds metadata about a bound function (signature, overloads, etc.)
Definition: attr.h:138
return_value_policy
Approach used to cast a previously unknown C++ instance into a Python object.
bool flag_noconvert
If set, do not allow conversion (requires a supporting type caster!)
Definition: cast.h:1864
op_type
Definition: operators.h:34
Internal data structure which holds metadata about a keyword argument.
Definition: attr.h:126
#define PYBIND11_NAMESPACE_END(name)
std::string type
The C++ type name of the default value (only available when compiled in debug mode) ...
Definition: cast.h:1907
static void precall(function_call &)
Definition: attr.h:498
bool dynamic_attr
Does the class manage a dict?
Definition: attr.h:260
handle value
Definition: attr.h:30
Definition: pytypes.h:897
#define PYBIND11_NAMESPACE_BEGIN(name)
bool flag_none
If set (the default), allow None to be passed to this argument.
Definition: cast.h:1865


gtsam
Author(s):
autogenerated on Sat May 8 2021 02:41:39