attr.h 21 KB

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