attr.h 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  1. /*
  2. pybind11/attr.h: Infrastructure for processing custom
  3. type and function attributes
  4. Copyright (c) 2016 Wenzel Jakob <wenzel.jakob@epfl.ch>
  5. All rights reserved. Use of this source code is governed by a
  6. BSD-style license that can be found in the LICENSE file.
  7. */
  8. #pragma once
  9. #include "cast.h"
  10. NAMESPACE_BEGIN(PYBIND11_NAMESPACE)
  11. /// \addtogroup annotations
  12. /// @{
  13. /// Annotation for methods
  14. struct is_method { handle class_; is_method(const handle &c) : class_(c) { } };
  15. /// Annotation for operators
  16. struct is_operator { };
  17. /// Annotation for parent scope
  18. struct scope { handle value; scope(const handle &s) : value(s) { } };
  19. /// Annotation for documentation
  20. struct doc { const char *value; doc(const char *value) : value(value) { } };
  21. /// Annotation for function names
  22. struct name { const char *value; name(const char *value) : value(value) { } };
  23. /// Annotation indicating that a function is an overload associated with a given "sibling"
  24. struct sibling { handle value; sibling(const handle &value) : value(value.ptr()) { } };
  25. /// Annotation indicating that a class derives from another given type
  26. template <typename T> struct base {
  27. PYBIND11_DEPRECATED("base<T>() was deprecated in favor of specifying 'T' as a template argument to class_")
  28. base() { }
  29. };
  30. /// Keep patient alive while nurse lives
  31. template <size_t Nurse, size_t Patient> struct keep_alive { };
  32. /// Annotation indicating that a class is involved in a multiple inheritance relationship
  33. struct multiple_inheritance { };
  34. /// Annotation which enables dynamic attributes, i.e. adds `__dict__` to a class
  35. struct dynamic_attr { };
  36. /// Annotation which enables the buffer protocol for a type
  37. struct buffer_protocol { };
  38. /// Annotation which requests that a special metaclass is created for a type
  39. struct metaclass {
  40. handle value;
  41. PYBIND11_DEPRECATED("py::metaclass() is no longer required. It's turned on by default now.")
  42. metaclass() {}
  43. /// Override pybind11's default metaclass
  44. explicit metaclass(handle value) : value(value) { }
  45. };
  46. /// Annotation that marks a class as local to the module:
  47. struct module_local { const bool value; constexpr module_local(bool v = true) : value(v) { } };
  48. /// Annotation to mark enums as an arithmetic type
  49. struct arithmetic { };
  50. /** \rst
  51. A call policy which places one or more guard variables (``Ts...``) around the function call.
  52. For example, this definition:
  53. .. code-block:: cpp
  54. m.def("foo", foo, py::call_guard<T>());
  55. is equivalent to the following pseudocode:
  56. .. code-block:: cpp
  57. m.def("foo", [](args...) {
  58. T scope_guard;
  59. return foo(args...); // forwarded arguments
  60. });
  61. \endrst */
  62. template <typename... Ts> struct call_guard;
  63. template <> struct call_guard<> { using type = detail::void_type; };
  64. template <typename T>
  65. struct call_guard<T> {
  66. static_assert(std::is_default_constructible<T>::value,
  67. "The guard type must be default constructible");
  68. using type = T;
  69. };
  70. template <typename T, typename... Ts>
  71. struct call_guard<T, Ts...> {
  72. struct type {
  73. T guard{}; // Compose multiple guard types with left-to-right default-constructor order
  74. typename call_guard<Ts...>::type next{};
  75. };
  76. };
  77. /// @} annotations
  78. NAMESPACE_BEGIN(detail)
  79. /* Forward declarations */
  80. enum op_id : int;
  81. enum op_type : int;
  82. struct undefined_t;
  83. template <op_id id, op_type ot, typename L = undefined_t, typename R = undefined_t> struct op_;
  84. inline void keep_alive_impl(size_t Nurse, size_t Patient, function_call &call, handle ret);
  85. /// Internal data structure which holds metadata about a keyword argument
  86. struct argument_record {
  87. const char *name; ///< Argument name
  88. const char *descr; ///< Human-readable version of the argument value
  89. handle value; ///< Associated Python object
  90. bool convert : 1; ///< True if the argument is allowed to convert when loading
  91. bool none : 1; ///< True if None is allowed when loading
  92. argument_record(const char *name, const char *descr, handle value, bool convert, bool none)
  93. : name(name), descr(descr), value(value), convert(convert), none(none) { }
  94. };
  95. /// Internal data structure which holds metadata about a bound function (signature, overloads, etc.)
  96. struct function_record {
  97. function_record()
  98. : is_constructor(false), is_new_style_constructor(false), is_stateless(false),
  99. is_operator(false), has_args(false), has_kwargs(false), is_method(false) { }
  100. /// Function name
  101. char *name = nullptr; /* why no C++ strings? They generate heavier code.. */
  102. // User-specified documentation string
  103. char *doc = nullptr;
  104. /// Human-readable version of the function signature
  105. char *signature = nullptr;
  106. /// List of registered keyword arguments
  107. std::vector<argument_record> args;
  108. /// Pointer to lambda function which converts arguments and performs the actual call
  109. handle (*impl) (function_call &) = nullptr;
  110. /// Storage for the wrapped function pointer and captured data, if any
  111. void *data[3] = { };
  112. /// Pointer to custom destructor for 'data' (if needed)
  113. void (*free_data) (function_record *ptr) = nullptr;
  114. /// Return value policy associated with this function
  115. return_value_policy policy = return_value_policy::automatic;
  116. /// True if name == '__init__'
  117. bool is_constructor : 1;
  118. /// True if this is a new-style `__init__` defined in `detail/init.h`
  119. bool is_new_style_constructor : 1;
  120. /// True if this is a stateless function pointer
  121. bool is_stateless : 1;
  122. /// True if this is an operator (__add__), etc.
  123. bool is_operator : 1;
  124. /// True if the function has a '*args' argument
  125. bool has_args : 1;
  126. /// True if the function has a '**kwargs' argument
  127. bool has_kwargs : 1;
  128. /// True if this is a method
  129. bool is_method : 1;
  130. /// Number of arguments (including py::args and/or py::kwargs, if present)
  131. std::uint16_t nargs;
  132. /// Python method object
  133. PyMethodDef *def = nullptr;
  134. /// Python handle to the parent scope (a class or a module)
  135. handle scope;
  136. /// Python handle to the sibling function representing an overload chain
  137. handle sibling;
  138. /// Pointer to next overload
  139. function_record *next = nullptr;
  140. };
  141. /// Special data structure which (temporarily) holds metadata about a bound class
  142. struct type_record {
  143. PYBIND11_NOINLINE type_record()
  144. : multiple_inheritance(false), dynamic_attr(false), buffer_protocol(false), module_local(false) { }
  145. /// Handle to the parent scope
  146. handle scope;
  147. /// Name of the class
  148. const char *name = nullptr;
  149. // Pointer to RTTI type_info data structure
  150. const std::type_info *type = nullptr;
  151. /// How large is the underlying C++ type?
  152. size_t type_size = 0;
  153. /// What is the alignment of the underlying C++ type?
  154. size_t type_align = 0;
  155. /// How large is the type's holder?
  156. size_t holder_size = 0;
  157. /// The global operator new can be overridden with a class-specific variant
  158. void *(*operator_new)(size_t) = nullptr;
  159. /// Function pointer to class_<..>::init_instance
  160. void (*init_instance)(instance *, const void *) = nullptr;
  161. /// Function pointer to class_<..>::dealloc
  162. void (*dealloc)(detail::value_and_holder &) = nullptr;
  163. /// List of base classes of the newly created type
  164. list bases;
  165. /// Optional docstring
  166. const char *doc = nullptr;
  167. /// Custom metaclass (optional)
  168. handle metaclass;
  169. /// Multiple inheritance marker
  170. bool multiple_inheritance : 1;
  171. /// Does the class manage a __dict__?
  172. bool dynamic_attr : 1;
  173. /// Does the class implement the buffer protocol?
  174. bool buffer_protocol : 1;
  175. /// Is the default (unique_ptr) holder type used?
  176. bool default_holder : 1;
  177. /// Is the class definition local to the module shared object?
  178. bool module_local : 1;
  179. PYBIND11_NOINLINE void add_base(const std::type_info &base, void *(*caster)(void *)) {
  180. auto base_info = detail::get_type_info(base, false);
  181. if (!base_info) {
  182. std::string tname(base.name());
  183. detail::clean_type_id(tname);
  184. pybind11_fail("generic_type: type \"" + std::string(name) +
  185. "\" referenced unknown base type \"" + tname + "\"");
  186. }
  187. if (default_holder != base_info->default_holder) {
  188. std::string tname(base.name());
  189. detail::clean_type_id(tname);
  190. pybind11_fail("generic_type: type \"" + std::string(name) + "\" " +
  191. (default_holder ? "does not have" : "has") +
  192. " a non-default holder type while its base \"" + tname + "\" " +
  193. (base_info->default_holder ? "does not" : "does"));
  194. }
  195. bases.append((PyObject *) base_info->type);
  196. if (base_info->type->tp_dictoffset != 0)
  197. dynamic_attr = true;
  198. if (caster)
  199. base_info->implicit_casts.emplace_back(type, caster);
  200. }
  201. };
  202. inline function_call::function_call(const function_record &f, handle p) :
  203. func(f), parent(p) {
  204. args.reserve(f.nargs);
  205. args_convert.reserve(f.nargs);
  206. }
  207. /// Tag for a new-style `__init__` defined in `detail/init.h`
  208. struct is_new_style_constructor { };
  209. /**
  210. * Partial template specializations to process custom attributes provided to
  211. * cpp_function_ and class_. These are either used to initialize the respective
  212. * fields in the type_record and function_record data structures or executed at
  213. * runtime to deal with custom call policies (e.g. keep_alive).
  214. */
  215. template <typename T, typename SFINAE = void> struct process_attribute;
  216. template <typename T> struct process_attribute_default {
  217. /// Default implementation: do nothing
  218. static void init(const T &, function_record *) { }
  219. static void init(const T &, type_record *) { }
  220. static void precall(function_call &) { }
  221. static void postcall(function_call &, handle) { }
  222. };
  223. /// Process an attribute specifying the function's name
  224. template <> struct process_attribute<name> : process_attribute_default<name> {
  225. static void init(const name &n, function_record *r) { r->name = const_cast<char *>(n.value); }
  226. };
  227. /// Process an attribute specifying the function's docstring
  228. template <> struct process_attribute<doc> : process_attribute_default<doc> {
  229. static void init(const doc &n, function_record *r) { r->doc = const_cast<char *>(n.value); }
  230. };
  231. /// Process an attribute specifying the function's docstring (provided as a C-style string)
  232. template <> struct process_attribute<const char *> : process_attribute_default<const char *> {
  233. static void init(const char *d, function_record *r) { r->doc = const_cast<char *>(d); }
  234. static void init(const char *d, type_record *r) { r->doc = const_cast<char *>(d); }
  235. };
  236. template <> struct process_attribute<char *> : process_attribute<const char *> { };
  237. /// Process an attribute indicating the function's return value policy
  238. template <> struct process_attribute<return_value_policy> : process_attribute_default<return_value_policy> {
  239. static void init(const return_value_policy &p, function_record *r) { r->policy = p; }
  240. };
  241. /// Process an attribute which indicates that this is an overloaded function associated with a given sibling
  242. template <> struct process_attribute<sibling> : process_attribute_default<sibling> {
  243. static void init(const sibling &s, function_record *r) { r->sibling = s.value; }
  244. };
  245. /// Process an attribute which indicates that this function is a method
  246. template <> struct process_attribute<is_method> : process_attribute_default<is_method> {
  247. static void init(const is_method &s, function_record *r) { r->is_method = true; r->scope = s.class_; }
  248. };
  249. /// Process an attribute which indicates the parent scope of a method
  250. template <> struct process_attribute<scope> : process_attribute_default<scope> {
  251. static void init(const scope &s, function_record *r) { r->scope = s.value; }
  252. };
  253. /// Process an attribute which indicates that this function is an operator
  254. template <> struct process_attribute<is_operator> : process_attribute_default<is_operator> {
  255. static void init(const is_operator &, function_record *r) { r->is_operator = true; }
  256. };
  257. template <> struct process_attribute<is_new_style_constructor> : process_attribute_default<is_new_style_constructor> {
  258. static void init(const is_new_style_constructor &, function_record *r) { r->is_new_style_constructor = true; }
  259. };
  260. /// Process a keyword argument attribute (*without* a default value)
  261. template <> struct process_attribute<arg> : process_attribute_default<arg> {
  262. static void init(const arg &a, function_record *r) {
  263. if (r->is_method && r->args.empty())
  264. r->args.emplace_back("self", nullptr, handle(), true /*convert*/, false /*none not allowed*/);
  265. r->args.emplace_back(a.name, nullptr, handle(), !a.flag_noconvert, a.flag_none);
  266. }
  267. };
  268. /// Process a keyword argument attribute (*with* a default value)
  269. template <> struct process_attribute<arg_v> : process_attribute_default<arg_v> {
  270. static void init(const arg_v &a, function_record *r) {
  271. if (r->is_method && r->args.empty())
  272. r->args.emplace_back("self", nullptr /*descr*/, handle() /*parent*/, true /*convert*/, false /*none not allowed*/);
  273. if (!a.value) {
  274. #if !defined(NDEBUG)
  275. std::string descr("'");
  276. if (a.name) descr += std::string(a.name) + ": ";
  277. descr += a.type + "'";
  278. if (r->is_method) {
  279. if (r->name)
  280. descr += " in method '" + (std::string) str(r->scope) + "." + (std::string) r->name + "'";
  281. else
  282. descr += " in method of '" + (std::string) str(r->scope) + "'";
  283. } else if (r->name) {
  284. descr += " in function '" + (std::string) r->name + "'";
  285. }
  286. pybind11_fail("arg(): could not convert default argument "
  287. + descr + " into a Python object (type not registered yet?)");
  288. #else
  289. pybind11_fail("arg(): could not convert default argument "
  290. "into a Python object (type not registered yet?). "
  291. "Compile in debug mode for more information.");
  292. #endif
  293. }
  294. r->args.emplace_back(a.name, a.descr, a.value.inc_ref(), !a.flag_noconvert, a.flag_none);
  295. }
  296. };
  297. /// Process a parent class attribute. Single inheritance only (class_ itself already guarantees that)
  298. template <typename T>
  299. struct process_attribute<T, enable_if_t<is_pyobject<T>::value>> : process_attribute_default<handle> {
  300. static void init(const handle &h, type_record *r) { r->bases.append(h); }
  301. };
  302. /// Process a parent class attribute (deprecated, does not support multiple inheritance)
  303. template <typename T>
  304. struct process_attribute<base<T>> : process_attribute_default<base<T>> {
  305. static void init(const base<T> &, type_record *r) { r->add_base(typeid(T), nullptr); }
  306. };
  307. /// Process a multiple inheritance attribute
  308. template <>
  309. struct process_attribute<multiple_inheritance> : process_attribute_default<multiple_inheritance> {
  310. static void init(const multiple_inheritance &, type_record *r) { r->multiple_inheritance = true; }
  311. };
  312. template <>
  313. struct process_attribute<dynamic_attr> : process_attribute_default<dynamic_attr> {
  314. static void init(const dynamic_attr &, type_record *r) { r->dynamic_attr = true; }
  315. };
  316. template <>
  317. struct process_attribute<buffer_protocol> : process_attribute_default<buffer_protocol> {
  318. static void init(const buffer_protocol &, type_record *r) { r->buffer_protocol = true; }
  319. };
  320. template <>
  321. struct process_attribute<metaclass> : process_attribute_default<metaclass> {
  322. static void init(const metaclass &m, type_record *r) { r->metaclass = m.value; }
  323. };
  324. template <>
  325. struct process_attribute<module_local> : process_attribute_default<module_local> {
  326. static void init(const module_local &l, type_record *r) { r->module_local = l.value; }
  327. };
  328. /// Process an 'arithmetic' attribute for enums (does nothing here)
  329. template <>
  330. struct process_attribute<arithmetic> : process_attribute_default<arithmetic> {};
  331. template <typename... Ts>
  332. struct process_attribute<call_guard<Ts...>> : process_attribute_default<call_guard<Ts...>> { };
  333. /**
  334. * Process a keep_alive call policy -- invokes keep_alive_impl during the
  335. * pre-call handler if both Nurse, Patient != 0 and use the post-call handler
  336. * otherwise
  337. */
  338. template <size_t Nurse, size_t Patient> struct process_attribute<keep_alive<Nurse, Patient>> : public process_attribute_default<keep_alive<Nurse, Patient>> {
  339. template <size_t N = Nurse, size_t P = Patient, enable_if_t<N != 0 && P != 0, int> = 0>
  340. static void precall(function_call &call) { keep_alive_impl(Nurse, Patient, call, handle()); }
  341. template <size_t N = Nurse, size_t P = Patient, enable_if_t<N != 0 && P != 0, int> = 0>
  342. static void postcall(function_call &, handle) { }
  343. template <size_t N = Nurse, size_t P = Patient, enable_if_t<N == 0 || P == 0, int> = 0>
  344. static void precall(function_call &) { }
  345. template <size_t N = Nurse, size_t P = Patient, enable_if_t<N == 0 || P == 0, int> = 0>
  346. static void postcall(function_call &call, handle ret) { keep_alive_impl(Nurse, Patient, call, ret); }
  347. };
  348. /// Recursively iterate over variadic template arguments
  349. template <typename... Args> struct process_attributes {
  350. static void init(const Args&... args, function_record *r) {
  351. int unused[] = { 0, (process_attribute<typename std::decay<Args>::type>::init(args, r), 0) ... };
  352. ignore_unused(unused);
  353. }
  354. static void init(const Args&... args, type_record *r) {
  355. int unused[] = { 0, (process_attribute<typename std::decay<Args>::type>::init(args, r), 0) ... };
  356. ignore_unused(unused);
  357. }
  358. static void precall(function_call &call) {
  359. int unused[] = { 0, (process_attribute<typename std::decay<Args>::type>::precall(call), 0) ... };
  360. ignore_unused(unused);
  361. }
  362. static void postcall(function_call &call, handle fn_ret) {
  363. int unused[] = { 0, (process_attribute<typename std::decay<Args>::type>::postcall(call, fn_ret), 0) ... };
  364. ignore_unused(unused);
  365. }
  366. };
  367. template <typename T>
  368. using is_call_guard = is_instantiation<call_guard, T>;
  369. /// Extract the ``type`` from the first `call_guard` in `Extras...` (or `void_type` if none found)
  370. template <typename... Extra>
  371. using extract_guard_t = typename exactly_one_t<is_call_guard, call_guard<>, Extra...>::type;
  372. /// Check the number of named arguments at compile time
  373. template <typename... Extra,
  374. size_t named = constexpr_sum(std::is_base_of<arg, Extra>::value...),
  375. size_t self = constexpr_sum(std::is_same<is_method, Extra>::value...)>
  376. constexpr bool expected_num_args(size_t nargs, bool has_args, bool has_kwargs) {
  377. return named == 0 || (self + named + has_args + has_kwargs) == nargs;
  378. }
  379. NAMESPACE_END(detail)
  380. NAMESPACE_END(PYBIND11_NAMESPACE)