Scarab  2.8.1
Project 8 C++ Utility Library
pybind11.h
Go to the documentation of this file.
1 /*
2  pybind11/pybind11.h: Main header file of the C++11 python
3  binding generator library
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 #if defined(__INTEL_COMPILER)
14 # pragma warning push
15 # pragma warning disable 68 // integer conversion resulted in a change of sign
16 # pragma warning disable 186 // pointless comparison of unsigned integer with zero
17 # pragma warning disable 878 // incompatible exception specifications
18 # pragma warning disable 1334 // the "template" keyword used for syntactic disambiguation may only be used within a template
19 # pragma warning disable 1682 // implicit conversion of a 64-bit integral type to a smaller integral type (potential portability problem)
20 # pragma warning disable 1786 // function "strdup" was declared deprecated
21 # pragma warning disable 1875 // offsetof applied to non-POD (Plain Old Data) types is nonstandard
22 # pragma warning disable 2196 // warning #2196: routine is both "inline" and "noinline"
23 #elif defined(_MSC_VER)
24 # pragma warning(push)
25 # pragma warning(disable: 4100) // warning C4100: Unreferenced formal parameter
26 # pragma warning(disable: 4127) // warning C4127: Conditional expression is constant
27 # pragma warning(disable: 4512) // warning C4512: Assignment operator was implicitly defined as deleted
28 # pragma warning(disable: 4800) // warning C4800: 'int': forcing value to bool 'true' or 'false' (performance warning)
29 # pragma warning(disable: 4996) // warning C4996: The POSIX name for this item is deprecated. Instead, use the ISO C and C++ conformant name
30 # pragma warning(disable: 4702) // warning C4702: unreachable code
31 # pragma warning(disable: 4522) // warning C4522: multiple assignment operators specified
32 #elif defined(__GNUG__) && !defined(__clang__)
33 # pragma GCC diagnostic push
34 # pragma GCC diagnostic ignored "-Wunused-but-set-parameter"
35 # pragma GCC diagnostic ignored "-Wunused-but-set-variable"
36 # pragma GCC diagnostic ignored "-Wmissing-field-initializers"
37 # pragma GCC diagnostic ignored "-Wstrict-aliasing"
38 # pragma GCC diagnostic ignored "-Wattributes"
39 # if __GNUC__ >= 7
40 # pragma GCC diagnostic ignored "-Wnoexcept-type"
41 # endif
42 #endif
43 
44 #include "attr.h"
45 #include "options.h"
46 #include "detail/class.h"
47 #include "detail/init.h"
48 
49 #if defined(__GNUG__) && !defined(__clang__)
50 # include <cxxabi.h>
51 #endif
52 
54 
55 class cpp_function : public function {
57 public:
59  cpp_function(std::nullptr_t) { }
60 
62  template <typename Return, typename... Args, typename... Extra>
63  cpp_function(Return (*f)(Args...), const Extra&... extra) {
64  initialize(f, f, extra...);
65  }
66 
68  template <typename Func, typename... Extra,
70  cpp_function(Func &&f, const Extra&... extra) {
71  initialize(std::forward<Func>(f),
72  (detail::function_signature_t<Func> *) nullptr, extra...);
73  }
74 
76  template <typename Return, typename Class, typename... Arg, typename... Extra>
77  cpp_function(Return (Class::*f)(Arg...), const Extra&... extra) {
78  initialize([f](Class *c, Arg... args) -> Return { return (c->*f)(args...); },
79  (Return (*) (Class *, Arg...)) nullptr, extra...);
80  }
81 
83  template <typename Return, typename Class, typename... Arg, typename... Extra>
84  cpp_function(Return (Class::*f)(Arg...) const, const Extra&... extra) {
85  initialize([f](const Class *c, Arg... args) -> Return { return (c->*f)(args...); },
86  (Return (*)(const Class *, Arg ...)) nullptr, extra...);
87  }
88 
90  object name() const { return attr("__name__"); }
91 
92 protected:
94  PYBIND11_NOINLINE detail::function_record *make_function_record() {
95  return new detail::function_record();
96  }
97 
99  template <typename Func, typename Return, typename... Args, typename... Extra>
100  void initialize(Func &&f, Return (*)(Args...), const Extra&... extra) {
101  using namespace detail;
102  struct capture { remove_reference_t<Func> f; };
103 
104  /* Store the function including any extra state it might have (e.g. a lambda capture object) */
105  auto rec = make_function_record();
106 
107  /* Store the capture object directly in the function record if there is enough space */
108  if (sizeof(capture) <= sizeof(rec->data)) {
109  /* Without these pragmas, GCC warns that there might not be
110  enough space to use the placement new operator. However, the
111  'if' statement above ensures that this is the case. */
112 #if defined(__GNUG__) && !defined(__clang__) && __GNUC__ >= 6
113 # pragma GCC diagnostic push
114 # pragma GCC diagnostic ignored "-Wplacement-new"
115 #endif
116  new ((capture *) &rec->data) capture { std::forward<Func>(f) };
117 #if defined(__GNUG__) && !defined(__clang__) && __GNUC__ >= 6
118 # pragma GCC diagnostic pop
119 #endif
121  rec->free_data = [](function_record *r) { ((capture *) &r->data)->~capture(); };
122  } else {
123  rec->data[0] = new capture { std::forward<Func>(f) };
124  rec->free_data = [](function_record *r) { delete ((capture *) r->data[0]); };
125  }
126 
127  /* Type casters for the function arguments and return value */
128  using cast_in = argument_loader<Args...>;
129  using cast_out = make_caster<
131  >;
132 
133  static_assert(expected_num_args<Extra...>(sizeof...(Args), cast_in::has_args, cast_in::has_kwargs),
134  "The number of argument annotations does not match the number of function arguments");
135 
136  /* Dispatch code which converts function arguments and performs the actual function call */
137  rec->impl = [](function_call &call) -> handle {
138  cast_in args_converter;
139 
140  /* Try to cast the function arguments into the C++ domain */
141  if (!args_converter.load_args(call))
143 
144  /* Invoke call policy pre-call hook */
145  process_attributes<Extra...>::precall(call);
146 
147  /* Get a pointer to the capture object */
148  auto data = (sizeof(capture) <= sizeof(call.func.data)
149  ? &call.func.data : call.func.data[0]);
150  capture *cap = const_cast<capture *>(reinterpret_cast<const capture *>(data));
151 
152  /* Override policy for rvalues -- usually to enforce rvp::move on an rvalue */
153  return_value_policy policy = return_value_policy_override<Return>::policy(call.func.policy);
154 
155  /* Function scope guard -- defaults to the compile-to-nothing `void_type` */
156  using Guard = extract_guard_t<Extra...>;
157 
158  /* Perform the function call */
159  handle result = cast_out::cast(
160  std::move(args_converter).template call<Return, Guard>(cap->f), policy, call.parent);
161 
162  /* Invoke call policy post-call hook */
163  process_attributes<Extra...>::postcall(call, result);
164 
165  return result;
166  };
167 
168  /* Process any user-provided function attributes */
169  process_attributes<Extra...>::init(extra..., rec);
170 
171  /* Generate a readable signature describing the function's arguments and return value types */
172  static constexpr auto signature = _("(") + cast_in::arg_names + _(") -> ") + cast_out::name;
173  PYBIND11_DESCR_CONSTEXPR auto types = decltype(signature)::types();
174 
175  /* Register the function with Python from generic (non-templated) code */
176  initialize_generic(rec, signature.text, types.data(), sizeof...(Args));
177 
178  if (cast_in::has_args) rec->has_args = true;
179  if (cast_in::has_kwargs) rec->has_kwargs = true;
180 
181  /* Stash some additional information used by an important optimization in 'functional.h' */
182  using FunctionType = Return (*)(Args...);
183  constexpr bool is_function_ptr =
185  sizeof(capture) == sizeof(void *);
186  if (is_function_ptr) {
187  rec->is_stateless = true;
188  rec->data[1] = const_cast<void *>(reinterpret_cast<const void *>(&typeid(FunctionType)));
189  }
190  }
191 
193  void initialize_generic(detail::function_record *rec, const char *text,
194  const std::type_info *const *types, size_t args) {
195 
196  /* Create copies of all referenced C-style strings */
197  rec->name = strdup(rec->name ? rec->name : "");
198  if (rec->doc) rec->doc = strdup(rec->doc);
199  for (auto &a: rec->args) {
200  if (a.name)
201  a.name = strdup(a.name);
202  if (a.descr)
203  a.descr = strdup(a.descr);
204  else if (a.value)
205  a.descr = strdup(a.value.attr("__repr__")().cast<std::string>().c_str());
206  }
207 
208  rec->is_constructor = !strcmp(rec->name, "__init__") || !strcmp(rec->name, "__setstate__");
209 
210 #if !defined(NDEBUG) && !defined(PYBIND11_DISABLE_NEW_STYLE_INIT_WARNING)
211  if (rec->is_constructor && !rec->is_new_style_constructor) {
212  const auto class_name = std::string(((PyTypeObject *) rec->scope.ptr())->tp_name);
213  const auto func_name = std::string(rec->name);
214  PyErr_WarnEx(
215  PyExc_FutureWarning,
216  ("pybind11-bound class '" + class_name + "' is using an old-style "
217  "placement-new '" + func_name + "' which has been deprecated. See "
218  "the upgrade guide in pybind11's docs. This message is only visible "
219  "when compiled in debug mode.").c_str(), 0
220  );
221  }
222 #endif
223 
224  /* Generate a proper function signature */
225  std::string signature;
226  size_t type_index = 0, arg_index = 0;
227  for (auto *pc = text; *pc != '\0'; ++pc) {
228  const auto c = *pc;
229 
230  if (c == '{') {
231  // Write arg name for everything except *args and **kwargs.
232  if (*(pc + 1) == '*')
233  continue;
234 
235  if (arg_index < rec->args.size() && rec->args[arg_index].name) {
236  signature += rec->args[arg_index].name;
237  } else if (arg_index == 0 && rec->is_method) {
238  signature += "self";
239  } else {
240  signature += "arg" + std::to_string(arg_index - (rec->is_method ? 1 : 0));
241  }
242  signature += ": ";
243  } else if (c == '}') {
244  // Write default value if available.
245  if (arg_index < rec->args.size() && rec->args[arg_index].descr) {
246  signature += " = ";
247  signature += rec->args[arg_index].descr;
248  }
249  arg_index++;
250  } else if (c == '%') {
251  const std::type_info *t = types[type_index++];
252  if (!t)
253  pybind11_fail("Internal error while parsing type signature (1)");
254  if (auto tinfo = detail::get_type_info(*t)) {
255  handle th((PyObject *) tinfo->type);
256  signature +=
257  th.attr("__module__").cast<std::string>() + "." +
258  th.attr("__qualname__").cast<std::string>(); // Python 3.3+, but we backport it to earlier versions
259  } else if (rec->is_new_style_constructor && arg_index == 0) {
260  // A new-style `__init__` takes `self` as `value_and_holder`.
261  // Rewrite it to the proper class type.
262  signature +=
263  rec->scope.attr("__module__").cast<std::string>() + "." +
264  rec->scope.attr("__qualname__").cast<std::string>();
265  } else {
266  std::string tname(t->name());
267  detail::clean_type_id(tname);
268  signature += tname;
269  }
270  } else {
271  signature += c;
272  }
273  }
274  if (arg_index != args || types[type_index] != nullptr)
275  pybind11_fail("Internal error while parsing type signature (2)");
276 
277 #if PY_MAJOR_VERSION < 3
278  if (strcmp(rec->name, "__next__") == 0) {
279  std::free(rec->name);
280  rec->name = strdup("next");
281  } else if (strcmp(rec->name, "__bool__") == 0) {
282  std::free(rec->name);
283  rec->name = strdup("__nonzero__");
284  }
285 #endif
286  rec->signature = strdup(signature.c_str());
287  rec->args.shrink_to_fit();
288  rec->nargs = (std::uint16_t) args;
289 
292 
293  detail::function_record *chain = nullptr, *chain_start = rec;
294  if (rec->sibling) {
295  if (PyCFunction_Check(rec->sibling.ptr())) {
296  auto rec_capsule = reinterpret_borrow<capsule>(PyCFunction_GET_SELF(rec->sibling.ptr()));
297  chain = (detail::function_record *) rec_capsule;
298  /* Never append a method to an overload chain of a parent class;
299  instead, hide the parent's overloads in this case */
300  if (!chain->scope.is(rec->scope))
301  chain = nullptr;
302  }
303  // Don't trigger for things like the default __init__, which are wrapper_descriptors that we are intentionally replacing
304  else if (!rec->sibling.is_none() && rec->name[0] != '_')
305  pybind11_fail("Cannot overload existing non-function object \"" + std::string(rec->name) +
306  "\" with a function of the same name");
307  }
308 
309  if (!chain) {
310  /* No existing overload was found, create a new function object */
311  rec->def = new PyMethodDef();
312  std::memset(rec->def, 0, sizeof(PyMethodDef));
313  rec->def->ml_name = rec->name;
314  rec->def->ml_meth = reinterpret_cast<PyCFunction>(reinterpret_cast<void (*) (void)>(*dispatcher));
315  rec->def->ml_flags = METH_VARARGS | METH_KEYWORDS;
316 
317  capsule rec_capsule(rec, [](void *ptr) {
318  destruct((detail::function_record *) ptr);
319  });
320 
321  object scope_module;
322  if (rec->scope) {
323  if (hasattr(rec->scope, "__module__")) {
324  scope_module = rec->scope.attr("__module__");
325  } else if (hasattr(rec->scope, "__name__")) {
326  scope_module = rec->scope.attr("__name__");
327  }
328  }
329 
330  m_ptr = PyCFunction_NewEx(rec->def, rec_capsule.ptr(), scope_module.ptr());
331  if (!m_ptr)
332  pybind11_fail("cpp_function::cpp_function(): Could not allocate function object");
333  } else {
334  /* Append at the end of the overload chain */
335  m_ptr = rec->sibling.ptr();
336  inc_ref();
337  chain_start = chain;
338  if (chain->is_method != rec->is_method)
339  pybind11_fail("overloading a method with both static and instance methods is not supported; "
340  #if defined(NDEBUG)
341  "compile in debug mode for more details"
342  #else
343  "error while attempting to bind " + std::string(rec->is_method ? "instance" : "static") + " method " +
344  std::string(pybind11::str(rec->scope.attr("__name__"))) + "." + std::string(rec->name) + signature
345  #endif
346  );
347  while (chain->next)
348  chain = chain->next;
349  chain->next = rec;
350  }
351 
352  std::string signatures;
353  int index = 0;
354  /* Create a nice pydoc rec including all signatures and
355  docstrings of the functions in the overload chain */
356  if (chain && options::show_function_signatures()) {
357  // First a generic signature
358  signatures += rec->name;
359  signatures += "(*args, **kwargs)\n";
360  signatures += "Overloaded function.\n\n";
361  }
362  // Then specific overload signatures
363  bool first_user_def = true;
364  for (auto it = chain_start; it != nullptr; it = it->next) {
365  if (options::show_function_signatures()) {
366  if (index > 0) signatures += "\n";
367  if (chain)
368  signatures += std::to_string(++index) + ". ";
369  signatures += rec->name;
370  signatures += it->signature;
371  signatures += "\n";
372  }
373  if (it->doc && strlen(it->doc) > 0 && options::show_user_defined_docstrings()) {
374  // If we're appending another docstring, and aren't printing function signatures, we
375  // need to append a newline first:
376  if (!options::show_function_signatures()) {
377  if (first_user_def) first_user_def = false;
378  else signatures += "\n";
379  }
380  if (options::show_function_signatures()) signatures += "\n";
381  signatures += it->doc;
382  if (options::show_function_signatures()) signatures += "\n";
383  }
384  }
385 
386  /* Install docstring */
387  PyCFunctionObject *func = (PyCFunctionObject *) m_ptr;
388  if (func->m_ml->ml_doc)
389  std::free(const_cast<char *>(func->m_ml->ml_doc));
390  func->m_ml->ml_doc = strdup(signatures.c_str());
391 
392  if (rec->is_method) {
393  m_ptr = PYBIND11_INSTANCE_METHOD_NEW(m_ptr, rec->scope.ptr());
394  if (!m_ptr)
395  pybind11_fail("cpp_function::cpp_function(): Could not allocate instance method object");
396  Py_DECREF(func);
397  }
398  }
399 
401  static void destruct(detail::function_record *rec) {
402  while (rec) {
403  detail::function_record *next = rec->next;
404  if (rec->free_data)
405  rec->free_data(rec);
406  std::free((char *) rec->name);
407  std::free((char *) rec->doc);
408  std::free((char *) rec->signature);
409  for (auto &arg: rec->args) {
410  std::free(const_cast<char *>(arg.name));
411  std::free(const_cast<char *>(arg.descr));
412  arg.value.dec_ref();
413  }
414  if (rec->def) {
415  std::free(const_cast<char *>(rec->def->ml_doc));
416  delete rec->def;
417  }
418  delete rec;
419  rec = next;
420  }
421  }
422 
424  static PyObject *dispatcher(PyObject *self, PyObject *args_in, PyObject *kwargs_in) {
425  using namespace detail;
426 
427  /* Iterator over the list of potentially admissible overloads */
428  const function_record *overloads = (function_record *) PyCapsule_GetPointer(self, nullptr),
429  *it = overloads;
430 
431  /* Need to know how many arguments + keyword arguments there are to pick the right overload */
432  const size_t n_args_in = (size_t) PyTuple_GET_SIZE(args_in);
433 
434  handle parent = n_args_in > 0 ? PyTuple_GET_ITEM(args_in, 0) : nullptr,
436 
437  auto self_value_and_holder = value_and_holder();
438  if (overloads->is_constructor) {
439  const auto tinfo = get_type_info((PyTypeObject *) overloads->scope.ptr());
440  const auto pi = reinterpret_cast<instance *>(parent.ptr());
441  self_value_and_holder = pi->get_value_and_holder(tinfo, false);
442 
443  if (!self_value_and_holder.type || !self_value_and_holder.inst) {
444  PyErr_SetString(PyExc_TypeError, "__init__(self, ...) called with invalid `self` argument");
445  return nullptr;
446  }
447 
448  // If this value is already registered it must mean __init__ is invoked multiple times;
449  // we really can't support that in C++, so just ignore the second __init__.
450  if (self_value_and_holder.instance_registered())
451  return none().release().ptr();
452  }
453 
454  try {
455  // We do this in two passes: in the first pass, we load arguments with `convert=false`;
456  // in the second, we allow conversion (except for arguments with an explicit
457  // py::arg().noconvert()). This lets us prefer calls without conversion, with
458  // conversion as a fallback.
459  std::vector<function_call> second_pass;
460 
461  // However, if there are no overloads, we can just skip the no-convert pass entirely
462  const bool overloaded = it != nullptr && it->next != nullptr;
463 
464  for (; it != nullptr; it = it->next) {
465 
466  /* For each overload:
467  1. Copy all positional arguments we were given, also checking to make sure that
468  named positional arguments weren't *also* specified via kwarg.
469  2. If we weren't given enough, try to make up the omitted ones by checking
470  whether they were provided by a kwarg matching the `py::arg("name")` name. If
471  so, use it (and remove it from kwargs; if not, see if the function binding
472  provided a default that we can use.
473  3. Ensure that either all keyword arguments were "consumed", or that the function
474  takes a kwargs argument to accept unconsumed kwargs.
475  4. Any positional arguments still left get put into a tuple (for args), and any
476  leftover kwargs get put into a dict.
477  5. Pack everything into a vector; if we have py::args or py::kwargs, they are an
478  extra tuple or dict at the end of the positional arguments.
479  6. Call the function call dispatcher (function_record::impl)
480 
481  If one of these fail, move on to the next overload and keep trying until we get a
482  result other than PYBIND11_TRY_NEXT_OVERLOAD.
483  */
484 
485  const function_record &func = *it;
486  size_t pos_args = func.nargs; // Number of positional arguments that we need
487  if (func.has_args) --pos_args; // (but don't count py::args
488  if (func.has_kwargs) --pos_args; // or py::kwargs)
489 
490  if (!func.has_args && n_args_in > pos_args)
491  continue; // Too many arguments for this overload
492 
493  if (n_args_in < pos_args && func.args.size() < pos_args)
494  continue; // Not enough arguments given, and not enough defaults to fill in the blanks
495 
496  function_call call(func, parent);
497 
498  size_t args_to_copy = (std::min)(pos_args, n_args_in); // Protect std::min with parentheses
499  size_t args_copied = 0;
500 
501  // 0. Inject new-style `self` argument
502  if (func.is_new_style_constructor) {
503  // The `value` may have been preallocated by an old-style `__init__`
504  // if it was a preceding candidate for overload resolution.
505  if (self_value_and_holder)
506  self_value_and_holder.type->dealloc(self_value_and_holder);
507 
508  call.init_self = PyTuple_GET_ITEM(args_in, 0);
509  call.args.push_back(reinterpret_cast<PyObject *>(&self_value_and_holder));
510  call.args_convert.push_back(false);
511  ++args_copied;
512  }
513 
514  // 1. Copy any position arguments given.
515  bool bad_arg = false;
516  for (; args_copied < args_to_copy; ++args_copied) {
517  const argument_record *arg_rec = args_copied < func.args.size() ? &func.args[args_copied] : nullptr;
518  if (kwargs_in && arg_rec && arg_rec->name && PyDict_GetItemString(kwargs_in, arg_rec->name)) {
519  bad_arg = true;
520  break;
521  }
522 
523  handle arg(PyTuple_GET_ITEM(args_in, args_copied));
524  if (arg_rec && !arg_rec->none && arg.is_none()) {
525  bad_arg = true;
526  break;
527  }
528  call.args.push_back(arg);
529  call.args_convert.push_back(arg_rec ? arg_rec->convert : true);
530  }
531  if (bad_arg)
532  continue; // Maybe it was meant for another overload (issue #688)
533 
534  // We'll need to copy this if we steal some kwargs for defaults
535  dict kwargs = reinterpret_borrow<dict>(kwargs_in);
536 
537  // 2. Check kwargs and, failing that, defaults that may help complete the list
538  if (args_copied < pos_args) {
539  bool copied_kwargs = false;
540 
541  for (; args_copied < pos_args; ++args_copied) {
542  const auto &arg = func.args[args_copied];
543 
544  handle value;
545  if (kwargs_in && arg.name)
546  value = PyDict_GetItemString(kwargs.ptr(), arg.name);
547 
548  if (value) {
549  // Consume a kwargs value
550  if (!copied_kwargs) {
551  kwargs = reinterpret_steal<dict>(PyDict_Copy(kwargs.ptr()));
552  copied_kwargs = true;
553  }
554  PyDict_DelItemString(kwargs.ptr(), arg.name);
555  } else if (arg.value) {
556  value = arg.value;
557  }
558 
559  if (value) {
560  call.args.push_back(value);
561  call.args_convert.push_back(arg.convert);
562  }
563  else
564  break;
565  }
566 
567  if (args_copied < pos_args)
568  continue; // Not enough arguments, defaults, or kwargs to fill the positional arguments
569  }
570 
571  // 3. Check everything was consumed (unless we have a kwargs arg)
572  if (kwargs && kwargs.size() > 0 && !func.has_kwargs)
573  continue; // Unconsumed kwargs, but no py::kwargs argument to accept them
574 
575  // 4a. If we have a py::args argument, create a new tuple with leftovers
576  if (func.has_args) {
577  tuple extra_args;
578  if (args_to_copy == 0) {
579  // We didn't copy out any position arguments from the args_in tuple, so we
580  // can reuse it directly without copying:
581  extra_args = reinterpret_borrow<tuple>(args_in);
582  } else if (args_copied >= n_args_in) {
583  extra_args = tuple(0);
584  } else {
585  size_t args_size = n_args_in - args_copied;
586  extra_args = tuple(args_size);
587  for (size_t i = 0; i < args_size; ++i) {
588  extra_args[i] = PyTuple_GET_ITEM(args_in, args_copied + i);
589  }
590  }
591  call.args.push_back(extra_args);
592  call.args_convert.push_back(false);
593  call.args_ref = std::move(extra_args);
594  }
595 
596  // 4b. If we have a py::kwargs, pass on any remaining kwargs
597  if (func.has_kwargs) {
598  if (!kwargs.ptr())
599  kwargs = dict(); // If we didn't get one, send an empty one
600  call.args.push_back(kwargs);
601  call.args_convert.push_back(false);
602  call.kwargs_ref = std::move(kwargs);
603  }
604 
605  // 5. Put everything in a vector. Not technically step 5, we've been building it
606  // in `call.args` all along.
607  #if !defined(NDEBUG)
608  if (call.args.size() != func.nargs || call.args_convert.size() != func.nargs)
609  pybind11_fail("Internal error: function call dispatcher inserted wrong number of arguments!");
610  #endif
611 
612  std::vector<bool> second_pass_convert;
613  if (overloaded) {
614  // We're in the first no-convert pass, so swap out the conversion flags for a
615  // set of all-false flags. If the call fails, we'll swap the flags back in for
616  // the conversion-allowed call below.
617  second_pass_convert.resize(func.nargs, false);
618  call.args_convert.swap(second_pass_convert);
619  }
620 
621  // 6. Call the function.
622  try {
623  loader_life_support guard{};
624  result = func.impl(call);
625  } catch (reference_cast_error &) {
627  }
628 
629  if (result.ptr() != PYBIND11_TRY_NEXT_OVERLOAD)
630  break;
631 
632  if (overloaded) {
633  // The (overloaded) call failed; if the call has at least one argument that
634  // permits conversion (i.e. it hasn't been explicitly specified `.noconvert()`)
635  // then add this call to the list of second pass overloads to try.
636  for (size_t i = func.is_method ? 1 : 0; i < pos_args; i++) {
637  if (second_pass_convert[i]) {
638  // Found one: swap the converting flags back in and store the call for
639  // the second pass.
640  call.args_convert.swap(second_pass_convert);
641  second_pass.push_back(std::move(call));
642  break;
643  }
644  }
645  }
646  }
647 
648  if (overloaded && !second_pass.empty() && result.ptr() == PYBIND11_TRY_NEXT_OVERLOAD) {
649  // The no-conversion pass finished without success, try again with conversion allowed
650  for (auto &call : second_pass) {
651  try {
652  loader_life_support guard{};
653  result = call.func.impl(call);
654  } catch (reference_cast_error &) {
656  }
657 
658  if (result.ptr() != PYBIND11_TRY_NEXT_OVERLOAD) {
659  // The error reporting logic below expects 'it' to be valid, as it would be
660  // if we'd encountered this failure in the first-pass loop.
661  if (!result)
662  it = &call.func;
663  break;
664  }
665  }
666  }
667  } catch (error_already_set &e) {
668  e.restore();
669  return nullptr;
670 #if defined(__GNUG__) && !defined(__clang__)
671  } catch ( abi::__forced_unwind& ) {
672  throw;
673 #endif
674  } catch (...) {
675  /* When an exception is caught, give each registered exception
676  translator a chance to translate it to a Python exception
677  in reverse order of registration.
678 
679  A translator may choose to do one of the following:
680 
681  - catch the exception and call PyErr_SetString or PyErr_SetObject
682  to set a standard (or custom) Python exception, or
683  - do nothing and let the exception fall through to the next translator, or
684  - delegate translation to the next translator by throwing a new type of exception. */
685 
686  auto last_exception = std::current_exception();
687  auto &registered_exception_translators = get_internals().registered_exception_translators;
688  for (auto& translator : registered_exception_translators) {
689  try {
690  translator(last_exception);
691  } catch (...) {
692  last_exception = std::current_exception();
693  continue;
694  }
695  return nullptr;
696  }
697  PyErr_SetString(PyExc_SystemError, "Exception escaped from default exception translator!");
698  return nullptr;
699  }
700 
701  auto append_note_if_missing_header_is_suspected = [](std::string &msg) {
702  if (msg.find("std::") != std::string::npos) {
703  msg += "\n\n"
704  "Did you forget to `#include <pybind11/stl.h>`? Or <pybind11/complex.h>,\n"
705  "<pybind11/functional.h>, <pybind11/chrono.h>, etc. Some automatic\n"
706  "conversions are optional and require extra headers to be included\n"
707  "when compiling your pybind11 module.";
708  }
709  };
710 
711  if (result.ptr() == PYBIND11_TRY_NEXT_OVERLOAD) {
712  if (overloads->is_operator)
713  return handle(Py_NotImplemented).inc_ref().ptr();
714 
715  std::string msg = std::string(overloads->name) + "(): incompatible " +
716  std::string(overloads->is_constructor ? "constructor" : "function") +
717  " arguments. The following argument types are supported:\n";
718 
719  int ctr = 0;
720  for (const function_record *it2 = overloads; it2 != nullptr; it2 = it2->next) {
721  msg += " "+ std::to_string(++ctr) + ". ";
722 
723  bool wrote_sig = false;
724  if (overloads->is_constructor) {
725  // For a constructor, rewrite `(self: Object, arg0, ...) -> NoneType` as `Object(arg0, ...)`
726  std::string sig = it2->signature;
727  size_t start = sig.find('(') + 7; // skip "(self: "
728  if (start < sig.size()) {
729  // End at the , for the next argument
730  size_t end = sig.find(", "), next = end + 2;
731  size_t ret = sig.rfind(" -> ");
732  // Or the ), if there is no comma:
733  if (end >= sig.size()) next = end = sig.find(')');
734  if (start < end && next < sig.size()) {
735  msg.append(sig, start, end - start);
736  msg += '(';
737  msg.append(sig, next, ret - next);
738  wrote_sig = true;
739  }
740  }
741  }
742  if (!wrote_sig) msg += it2->signature;
743 
744  msg += "\n";
745  }
746  msg += "\nInvoked with: ";
747  auto args_ = reinterpret_borrow<tuple>(args_in);
748  bool some_args = false;
749  for (size_t ti = overloads->is_constructor ? 1 : 0; ti < args_.size(); ++ti) {
750  if (!some_args) some_args = true;
751  else msg += ", ";
752  msg += pybind11::repr(args_[ti]);
753  }
754  if (kwargs_in) {
755  auto kwargs = reinterpret_borrow<dict>(kwargs_in);
756  if (kwargs.size() > 0) {
757  if (some_args) msg += "; ";
758  msg += "kwargs: ";
759  bool first = true;
760  for (auto kwarg : kwargs) {
761  if (first) first = false;
762  else msg += ", ";
763  msg += pybind11::str("{}={!r}").format(kwarg.first, kwarg.second);
764  }
765  }
766  }
767 
768  append_note_if_missing_header_is_suspected(msg);
769  PyErr_SetString(PyExc_TypeError, msg.c_str());
770  return nullptr;
771  } else if (!result) {
772  std::string msg = "Unable to convert function return value to a "
773  "Python type! The signature was\n\t";
774  msg += it->signature;
775  append_note_if_missing_header_is_suspected(msg);
776  PyErr_SetString(PyExc_TypeError, msg.c_str());
777  return nullptr;
778  } else {
779  if (overloads->is_constructor && !self_value_and_holder.holder_constructed()) {
780  auto *pi = reinterpret_cast<instance *>(parent.ptr());
781  self_value_and_holder.type->init_instance(pi, nullptr);
782  }
783  return result.ptr();
784  }
785  }
786 };
787 
789 class module : public object {
790 public:
791  PYBIND11_OBJECT_DEFAULT(module, object, PyModule_Check)
792 
793 
794  explicit module(const char *name, const char *doc = nullptr) {
795  if (!options::show_user_defined_docstrings()) doc = nullptr;
796 #if PY_MAJOR_VERSION >= 3
797  PyModuleDef *def = new PyModuleDef();
798  std::memset(def, 0, sizeof(PyModuleDef));
799  def->m_name = name;
800  def->m_doc = doc;
801  def->m_size = -1;
802  Py_INCREF(def);
803  m_ptr = PyModule_Create(def);
804 #else
805  m_ptr = Py_InitModule3(name, nullptr, doc);
806 #endif
807  if (m_ptr == nullptr)
808  pybind11_fail("Internal error in module::module()");
809  inc_ref();
810  }
811 
817  template <typename Func, typename... Extra>
818  module &def(const char *name_, Func &&f, const Extra& ... extra) {
819  cpp_function func(std::forward<Func>(f), name(name_), scope(*this),
820  sibling(getattr(*this, name_, none())), extra...);
821  // NB: allow overwriting here because cpp_function sets up a chain with the intention of
822  // overwriting (and has already checked internally that it isn't overwriting non-functions).
823  add_object(name_, func, true /* overwrite */);
824  return *this;
825  }
826 
837  module def_submodule(const char *name, const char *doc = nullptr) {
838  std::string full_name = std::string(PyModule_GetName(m_ptr))
839  + std::string(".") + std::string(name);
840  auto result = reinterpret_borrow<module>(PyImport_AddModule(full_name.c_str()));
841  if (doc && options::show_user_defined_docstrings())
842  result.attr("__doc__") = pybind11::str(doc);
843  attr(name) = result;
844  return result;
845  }
846 
848  static module import(const char *name) {
849  PyObject *obj = PyImport_ImportModule(name);
850  if (!obj)
851  throw error_already_set();
852  return reinterpret_steal<module>(obj);
853  }
854 
856  void reload() {
857  PyObject *obj = PyImport_ReloadModule(ptr());
858  if (!obj)
859  throw error_already_set();
860  *this = reinterpret_steal<module>(obj);
861  }
862 
863  // Adds an object to the module using the given name. Throws if an object with the given name
864  // already exists.
865  //
866  // overwrite should almost always be false: attempting to overwrite objects that pybind11 has
867  // established will, in most cases, break things.
868  PYBIND11_NOINLINE void add_object(const char *name, handle obj, bool overwrite = false) {
869  if (!overwrite && hasattr(*this, name))
870  pybind11_fail("Error during initialization: multiple incompatible definitions with name \"" +
871  std::string(name) + "\"");
872 
873  PyModule_AddObject(ptr(), name, obj.inc_ref().ptr() /* steals a reference */);
874  }
875 };
876 
880 inline dict globals() {
881  PyObject *p = PyEval_GetGlobals();
882  return reinterpret_borrow<dict>(p ? p : module::import("__main__").attr("__dict__").ptr());
883 }
884 
885 NAMESPACE_BEGIN(detail)
887 class generic_type : public object {
888  template <typename...> friend class class_;
889 public:
890  PYBIND11_OBJECT_DEFAULT(generic_type, object, PyType_Check)
891 protected:
892  void initialize(const type_record &rec) {
893  if (rec.scope && hasattr(rec.scope, rec.name))
894  pybind11_fail("generic_type: cannot initialize type \"" + std::string(rec.name) +
895  "\": an object with that name is already defined");
896 
898  pybind11_fail("generic_type: type \"" + std::string(rec.name) +
899  "\" is already registered!");
900 
901  m_ptr = make_new_python_type(rec);
902 
903  /* Register supplemental type information in C++ dict */
904  auto *tinfo = new detail::type_info();
905  tinfo->type = (PyTypeObject *) m_ptr;
906  tinfo->cpptype = rec.type;
907  tinfo->type_size = rec.type_size;
908  tinfo->type_align = rec.type_align;
909  tinfo->operator_new = rec.operator_new;
910  tinfo->holder_size_in_ptrs = size_in_ptrs(rec.holder_size);
911  tinfo->init_instance = rec.init_instance;
912  tinfo->dealloc = rec.dealloc;
913  tinfo->simple_type = true;
914  tinfo->simple_ancestors = true;
915  tinfo->default_holder = rec.default_holder;
916  tinfo->module_local = rec.module_local;
917 
918  auto &internals = get_internals();
919  auto tindex = std::type_index(*rec.type);
920  tinfo->direct_conversions = &internals.direct_conversions[tindex];
921  if (rec.module_local)
922  registered_local_types_cpp()[tindex] = tinfo;
923  else
925  internals.registered_types_py[(PyTypeObject *) m_ptr] = { tinfo };
926 
927  if (rec.bases.size() > 1 || rec.multiple_inheritance) {
928  mark_parents_nonsimple(tinfo->type);
929  tinfo->simple_ancestors = false;
930  }
931  else if (rec.bases.size() == 1) {
932  auto parent_tinfo = get_type_info((PyTypeObject *) rec.bases[0].ptr());
933  tinfo->simple_ancestors = parent_tinfo->simple_ancestors;
934  }
935 
936  if (rec.module_local) {
937  // Stash the local typeinfo and loader so that external modules can access it.
938  tinfo->module_local_load = &type_caster_generic::local_load;
940  }
941  }
942 
944  void mark_parents_nonsimple(PyTypeObject *value) {
945  auto t = reinterpret_borrow<tuple>(value->tp_bases);
946  for (handle h : t) {
947  auto tinfo2 = get_type_info((PyTypeObject *) h.ptr());
948  if (tinfo2)
949  tinfo2->simple_type = false;
950  mark_parents_nonsimple((PyTypeObject *) h.ptr());
951  }
952  }
953 
954  void install_buffer_funcs(
955  buffer_info *(*get_buffer)(PyObject *, void *),
956  void *get_buffer_data) {
957  PyHeapTypeObject *type = (PyHeapTypeObject*) m_ptr;
958  auto tinfo = detail::get_type_info(&type->ht_type);
959 
960  if (!type->ht_type.tp_as_buffer)
961  pybind11_fail(
962  "To be able to register buffer protocol support for the type '" +
963  std::string(tinfo->type->tp_name) +
964  "' the associated class<>(..) invocation must "
965  "include the pybind11::buffer_protocol() annotation!");
966 
967  tinfo->get_buffer = get_buffer;
968  tinfo->get_buffer_data = get_buffer_data;
969  }
970 
971  // rec_func must be set for either fget or fset.
972  void def_property_static_impl(const char *name,
973  handle fget, handle fset,
974  detail::function_record *rec_func) {
975  const auto is_static = rec_func && !(rec_func->is_method && rec_func->scope);
976  const auto has_doc = rec_func && rec_func->doc && pybind11::options::show_user_defined_docstrings();
977  auto property = handle((PyObject *) (is_static ? get_internals().static_property_type
978  : &PyProperty_Type));
979  attr(name) = property(fget.ptr() ? fget : none(),
980  fset.ptr() ? fset : none(),
981  /*deleter*/none(),
982  pybind11::str(has_doc ? rec_func->doc : ""));
983  }
984 };
985 
987 template <typename T, typename = void_t<decltype(static_cast<void *(*)(size_t)>(T::operator new))>>
988 void set_operator_new(type_record *r) { r->operator_new = &T::operator new; }
989 
990 template <typename> void set_operator_new(...) { }
991 
992 template <typename T, typename SFINAE = void> struct has_operator_delete : std::false_type { };
993 template <typename T> struct has_operator_delete<T, void_t<decltype(static_cast<void (*)(void *)>(T::operator delete))>>
994  : std::true_type { };
995 template <typename T, typename SFINAE = void> struct has_operator_delete_size : std::false_type { };
996 template <typename T> struct has_operator_delete_size<T, void_t<decltype(static_cast<void (*)(void *, size_t)>(T::operator delete))>>
997  : std::true_type { };
1000 void call_operator_delete(T *p, size_t, size_t) { T::operator delete(p); }
1002 void call_operator_delete(T *p, size_t s, size_t) { T::operator delete(p, s); }
1003 
1004 inline void call_operator_delete(void *p, size_t s, size_t a) {
1005  (void)s; (void)a;
1006 #if defined(PYBIND11_CPP17)
1007  if (a > __STDCPP_DEFAULT_NEW_ALIGNMENT__)
1008  ::operator delete(p, s, std::align_val_t(a));
1009  else
1010  ::operator delete(p, s);
1011 #else
1012  ::operator delete(p);
1013 #endif
1014 }
1015 
1016 NAMESPACE_END(detail)
1017 
1018 template <typename /*Derived*/, typename F>
1021 auto method_adaptor(F &&f) -> decltype(std::forward<F>(f)) { return std::forward<F>(f); }
1022 
1023 template <typename Derived, typename Return, typename Class, typename... Args>
1024 auto method_adaptor(Return (Class::*pmf)(Args...)) -> Return (Derived::*)(Args...) {
1026  "Cannot bind an inaccessible base class method; use a lambda definition instead");
1027  return pmf;
1028 }
1029 
1030 template <typename Derived, typename Return, typename Class, typename... Args>
1031 auto method_adaptor(Return (Class::*pmf)(Args...) const) -> Return (Derived::*)(Args...) const {
1033  "Cannot bind an inaccessible base class method; use a lambda definition instead");
1034  return pmf;
1035 }
1036 
1037 template <typename type_, typename... options>
1039  template <typename T> using is_holder = detail::is_holder_type<type_, T>;
1040  template <typename T> using is_subtype = detail::is_strict_base_of<type_, T>;
1041  template <typename T> using is_base = detail::is_strict_base_of<T, type_>;
1042  // struct instead of using here to help MSVC:
1043  template <typename T> struct is_valid_class_option :
1044  detail::any_of<is_holder<T>, is_subtype<T>, is_base<T>> {};
1045 
1046 public:
1047  using type = type_;
1049  constexpr static bool has_alias = !std::is_void<type_alias>::value;
1051 
1052  static_assert(detail::all_of<is_valid_class_option<options>...>::value,
1053  "Unknown/invalid class_ template parameters provided");
1054 
1055  static_assert(!has_alias || std::is_polymorphic<type>::value,
1056  "Cannot use an alias class with a non-polymorphic type");
1057 
1058  PYBIND11_OBJECT(class_, generic_type, PyType_Check)
1059 
1060  template <typename... Extra>
1061  class_(handle scope, const char *name, const Extra &... extra) {
1062  using namespace detail;
1063 
1064  // MI can only be specified via class_ template options, not constructor parameters
1065  static_assert(
1066  none_of<is_pyobject<Extra>...>::value || // no base class arguments, or:
1067  ( constexpr_sum(is_pyobject<Extra>::value...) == 1 && // Exactly one base
1068  constexpr_sum(is_base<options>::value...) == 0 && // no template option bases
1069  none_of<std::is_same<multiple_inheritance, Extra>...>::value), // no multiple_inheritance attr
1070  "Error: multiple inheritance bases must be specified via class_ template options");
1071 
1072  type_record record;
1073  record.scope = scope;
1074  record.name = name;
1075  record.type = &typeid(type);
1076  record.type_size = sizeof(conditional_t<has_alias, type_alias, type>);
1077  record.type_align = alignof(conditional_t<has_alias, type_alias, type>&);
1078  record.holder_size = sizeof(holder_type);
1079  record.init_instance = init_instance;
1080  record.dealloc = dealloc;
1082 
1083  set_operator_new<type>(&record);
1084 
1085  /* Register base classes specified via template arguments to class_, if any */
1086  PYBIND11_EXPAND_SIDE_EFFECTS(add_base<options>(record));
1087 
1088  /* Process optional arguments, if any */
1089  process_attributes<Extra...>::init(extra..., &record);
1090 
1091  generic_type::initialize(record);
1092 
1093  if (has_alias) {
1094  auto &instances = record.module_local ? registered_local_types_cpp() : get_internals().registered_types_cpp;
1095  instances[std::type_index(typeid(type_alias))] = instances[std::type_index(typeid(type))];
1096  }
1097  }
1098 
1100  static void add_base(detail::type_record &rec) {
1101  rec.add_base(typeid(Base), [](void *src) -> void * {
1102  return static_cast<Base *>(reinterpret_cast<type *>(src));
1103  });
1104  }
1105 
1107  static void add_base(detail::type_record &) { }
1108 
1109  template <typename Func, typename... Extra>
1110  class_ &def(const char *name_, Func&& f, const Extra&... extra) {
1111  cpp_function cf(method_adaptor<type>(std::forward<Func>(f)), name(name_), is_method(*this),
1112  sibling(getattr(*this, name_, none())), extra...);
1113  attr(cf.name()) = cf;
1114  return *this;
1115  }
1116 
1117  template <typename Func, typename... Extra> class_ &
1118  def_static(const char *name_, Func &&f, const Extra&... extra) {
1120  "def_static(...) called with a non-static member function pointer");
1121  cpp_function cf(std::forward<Func>(f), name(name_), scope(*this),
1122  sibling(getattr(*this, name_, none())), extra...);
1123  attr(cf.name()) = staticmethod(cf);
1124  return *this;
1125  }
1126 
1127  template <detail::op_id id, detail::op_type ot, typename L, typename R, typename... Extra>
1128  class_ &def(const detail::op_<id, ot, L, R> &op, const Extra&... extra) {
1129  op.execute(*this, extra...);
1130  return *this;
1131  }
1132 
1133  template <detail::op_id id, detail::op_type ot, typename L, typename R, typename... Extra>
1134  class_ & def_cast(const detail::op_<id, ot, L, R> &op, const Extra&... extra) {
1135  op.execute_cast(*this, extra...);
1136  return *this;
1137  }
1138 
1139  template <typename... Args, typename... Extra>
1140  class_ &def(const detail::initimpl::constructor<Args...> &init, const Extra&... extra) {
1141  init.execute(*this, extra...);
1142  return *this;
1143  }
1144 
1145  template <typename... Args, typename... Extra>
1147  init.execute(*this, extra...);
1148  return *this;
1149  }
1150 
1151  template <typename... Args, typename... Extra>
1152  class_ &def(detail::initimpl::factory<Args...> &&init, const Extra&... extra) {
1153  std::move(init).execute(*this, extra...);
1154  return *this;
1155  }
1156 
1157  template <typename... Args, typename... Extra>
1158  class_ &def(detail::initimpl::pickle_factory<Args...> &&pf, const Extra &...extra) {
1159  std::move(pf).execute(*this, extra...);
1160  return *this;
1161  }
1162 
1163  template <typename Func> class_& def_buffer(Func &&func) {
1164  struct capture { Func func; };
1165  capture *ptr = new capture { std::forward<Func>(func) };
1166  install_buffer_funcs([](PyObject *obj, void *ptr) -> buffer_info* {
1168  if (!caster.load(obj, false))
1169  return nullptr;
1170  return new buffer_info(((capture *) ptr)->func(caster));
1171  }, ptr);
1172  return *this;
1173  }
1174 
1175  template <typename Return, typename Class, typename... Args>
1176  class_ &def_buffer(Return (Class::*func)(Args...)) {
1177  return def_buffer([func] (type &obj) { return (obj.*func)(); });
1178  }
1179 
1180  template <typename Return, typename Class, typename... Args>
1181  class_ &def_buffer(Return (Class::*func)(Args...) const) {
1182  return def_buffer([func] (const type &obj) { return (obj.*func)(); });
1183  }
1184 
1185  template <typename C, typename D, typename... Extra>
1186  class_ &def_readwrite(const char *name, D C::*pm, const Extra&... extra) {
1187  static_assert(std::is_same<C, type>::value || std::is_base_of<C, type>::value, "def_readwrite() requires a class member (or base class member)");
1188  cpp_function fget([pm](const type &c) -> const D &{ return c.*pm; }, is_method(*this)),
1189  fset([pm](type &c, const D &value) { c.*pm = value; }, is_method(*this));
1190  def_property(name, fget, fset, return_value_policy::reference_internal, extra...);
1191  return *this;
1192  }
1193 
1194  template <typename C, typename D, typename... Extra>
1195  class_ &def_readonly(const char *name, const D C::*pm, const Extra& ...extra) {
1196  static_assert(std::is_same<C, type>::value || std::is_base_of<C, type>::value, "def_readonly() requires a class member (or base class member)");
1197  cpp_function fget([pm](const type &c) -> const D &{ return c.*pm; }, is_method(*this));
1198  def_property_readonly(name, fget, return_value_policy::reference_internal, extra...);
1199  return *this;
1200  }
1201 
1202  template <typename D, typename... Extra>
1203  class_ &def_readwrite_static(const char *name, D *pm, const Extra& ...extra) {
1204  cpp_function fget([pm](object) -> const D &{ return *pm; }, scope(*this)),
1205  fset([pm](object, const D &value) { *pm = value; }, scope(*this));
1206  def_property_static(name, fget, fset, return_value_policy::reference, extra...);
1207  return *this;
1208  }
1209 
1210  template <typename D, typename... Extra>
1211  class_ &def_readonly_static(const char *name, const D *pm, const Extra& ...extra) {
1212  cpp_function fget([pm](object) -> const D &{ return *pm; }, scope(*this));
1213  def_property_readonly_static(name, fget, return_value_policy::reference, extra...);
1214  return *this;
1215  }
1216 
1218  template <typename Getter, typename... Extra>
1219  class_ &def_property_readonly(const char *name, const Getter &fget, const Extra& ...extra) {
1220  return def_property_readonly(name, cpp_function(method_adaptor<type>(fget)),
1221  return_value_policy::reference_internal, extra...);
1222  }
1223 
1225  template <typename... Extra>
1226  class_ &def_property_readonly(const char *name, const cpp_function &fget, const Extra& ...extra) {
1227  return def_property(name, fget, nullptr, extra...);
1228  }
1229 
1231  template <typename Getter, typename... Extra>
1232  class_ &def_property_readonly_static(const char *name, const Getter &fget, const Extra& ...extra) {
1233  return def_property_readonly_static(name, cpp_function(fget), return_value_policy::reference, extra...);
1234  }
1235 
1237  template <typename... Extra>
1238  class_ &def_property_readonly_static(const char *name, const cpp_function &fget, const Extra& ...extra) {
1239  return def_property_static(name, fget, nullptr, extra...);
1240  }
1241 
1243  template <typename Getter, typename Setter, typename... Extra>
1244  class_ &def_property(const char *name, const Getter &fget, const Setter &fset, const Extra& ...extra) {
1245  return def_property(name, fget, cpp_function(method_adaptor<type>(fset)), extra...);
1246  }
1247  template <typename Getter, typename... Extra>
1248  class_ &def_property(const char *name, const Getter &fget, const cpp_function &fset, const Extra& ...extra) {
1249  return def_property(name, cpp_function(method_adaptor<type>(fget)), fset,
1250  return_value_policy::reference_internal, extra...);
1251  }
1252 
1254  template <typename... Extra>
1255  class_ &def_property(const char *name, const cpp_function &fget, const cpp_function &fset, const Extra& ...extra) {
1256  return def_property_static(name, fget, fset, is_method(*this), extra...);
1257  }
1258 
1260  template <typename Getter, typename... Extra>
1261  class_ &def_property_static(const char *name, const Getter &fget, const cpp_function &fset, const Extra& ...extra) {
1262  return def_property_static(name, cpp_function(fget), fset, return_value_policy::reference, extra...);
1263  }
1264 
1266  template <typename... Extra>
1267  class_ &def_property_static(const char *name, const cpp_function &fget, const cpp_function &fset, const Extra& ...extra) {
1269  "Argument annotations are not allowed for properties");
1270  auto rec_fget = get_function_record(fget), rec_fset = get_function_record(fset);
1271  auto *rec_active = rec_fget;
1272  if (rec_fget) {
1273  char *doc_prev = rec_fget->doc; /* 'extra' field may include a property-specific documentation string */
1274  detail::process_attributes<Extra...>::init(extra..., rec_fget);
1275  if (rec_fget->doc && rec_fget->doc != doc_prev) {
1276  free(doc_prev);
1277  rec_fget->doc = strdup(rec_fget->doc);
1278  }
1279  }
1280  if (rec_fset) {
1281  char *doc_prev = rec_fset->doc;
1282  detail::process_attributes<Extra...>::init(extra..., rec_fset);
1283  if (rec_fset->doc && rec_fset->doc != doc_prev) {
1284  free(doc_prev);
1285  rec_fset->doc = strdup(rec_fset->doc);
1286  }
1287  if (! rec_active) rec_active = rec_fset;
1288  }
1289  def_property_static_impl(name, fget, fset, rec_active);
1290  return *this;
1291  }
1292 
1293 private:
1295  template <typename T>
1297  const holder_type * /* unused */, const std::enable_shared_from_this<T> * /* dummy */) {
1298  try {
1299  auto sh = std::dynamic_pointer_cast<typename holder_type::element_type>(
1300  v_h.value_ptr<type>()->shared_from_this());
1301  if (sh) {
1302  new (std::addressof(v_h.holder<holder_type>())) holder_type(std::move(sh));
1303  v_h.set_holder_constructed();
1304  }
1305  } catch (const std::bad_weak_ptr &) {}
1306 
1307  if (!v_h.holder_constructed() && inst->owned) {
1308  new (std::addressof(v_h.holder<holder_type>())) holder_type(v_h.value_ptr<type>());
1309  v_h.set_holder_constructed();
1310  }
1311  }
1312 
1314  const holder_type *holder_ptr, std::true_type /*is_copy_constructible*/) {
1315  new (std::addressof(v_h.holder<holder_type>())) holder_type(*reinterpret_cast<const holder_type *>(holder_ptr));
1316  }
1317 
1319  const holder_type *holder_ptr, std::false_type /*is_copy_constructible*/) {
1320  new (std::addressof(v_h.holder<holder_type>())) holder_type(std::move(*const_cast<holder_type *>(holder_ptr)));
1321  }
1322 
1325  const holder_type *holder_ptr, const void * /* dummy -- not enable_shared_from_this<T>) */) {
1326  if (holder_ptr) {
1327  init_holder_from_existing(v_h, holder_ptr, std::is_copy_constructible<holder_type>());
1328  v_h.set_holder_constructed();
1330  new (std::addressof(v_h.holder<holder_type>())) holder_type(v_h.value_ptr<type>());
1331  v_h.set_holder_constructed();
1332  }
1333  }
1334 
1339  static void init_instance(detail::instance *inst, const void *holder_ptr) {
1340  auto v_h = inst->get_value_and_holder(detail::get_type_info(typeid(type)));
1341  if (!v_h.instance_registered()) {
1342  register_instance(inst, v_h.value_ptr(), v_h.type);
1343  v_h.set_instance_registered();
1344  }
1345  init_holder(inst, v_h, (const holder_type *) holder_ptr, v_h.value_ptr<type>());
1346  }
1347 
1349  static void dealloc(detail::value_and_holder &v_h) {
1350  if (v_h.holder_constructed()) {
1351  v_h.holder<holder_type>().~holder_type();
1352  v_h.set_holder_constructed(false);
1353  }
1354  else {
1356  v_h.type->type_size,
1357  v_h.type->type_align
1358  );
1359  }
1360  v_h.value_ptr() = nullptr;
1361  }
1362 
1364  h = detail::get_function(h);
1365  return h ? (detail::function_record *) reinterpret_borrow<capsule>(PyCFunction_GET_SELF(h.ptr()))
1366  : nullptr;
1367  }
1368 };
1369 
1371 template <typename... Args> detail::initimpl::constructor<Args...> init() { return {}; }
1374 template <typename... Args> detail::initimpl::alias_constructor<Args...> init_alias() { return {}; }
1375 
1377 template <typename Func, typename Ret = detail::initimpl::factory<Func>>
1378 Ret init(Func &&f) { return {std::forward<Func>(f)}; }
1379 
1382 template <typename CFunc, typename AFunc, typename Ret = detail::initimpl::factory<CFunc, AFunc>>
1383 Ret init(CFunc &&c, AFunc &&a) {
1384  return {std::forward<CFunc>(c), std::forward<AFunc>(a)};
1385 }
1386 
1389 template <typename GetState, typename SetState>
1391  return {std::forward<GetState>(g), std::forward<SetState>(s)};
1392 }
1393 
1394 NAMESPACE_BEGIN(detail)
1395 struct enum_base {
1396  enum_base(handle base, handle parent) : m_base(base), m_parent(parent) { }
1397 
1398  PYBIND11_NOINLINE void init(bool is_arithmetic, bool is_convertible) {
1399  m_base.attr("__entries") = dict();
1400  auto property = handle((PyObject *) &PyProperty_Type);
1401  auto static_property = handle((PyObject *) get_internals().static_property_type);
1402 
1403  m_base.attr("__repr__") = cpp_function(
1404  [](handle arg) -> str {
1405  handle type = arg.get_type();
1406  object type_name = type.attr("__name__");
1407  dict entries = type.attr("__entries");
1408  for (const auto &kv : entries) {
1409  object other = kv.second[int_(0)];
1410  if (other.equal(arg))
1411  return pybind11::str("{}.{}").format(type_name, kv.first);
1412  }
1413  return pybind11::str("{}.???").format(type_name);
1414  }, is_method(m_base)
1415  );
1416 
1417  m_base.attr("name") = property(cpp_function(
1418  [](handle arg) -> str {
1419  dict entries = arg.get_type().attr("__entries");
1420  for (const auto &kv : entries) {
1421  if (handle(kv.second[int_(0)]).equal(arg))
1422  return pybind11::str(kv.first);
1423  }
1424  return "???";
1425  }, is_method(m_base)
1426  ));
1427 
1428  m_base.attr("__doc__") = static_property(cpp_function(
1429  [](handle arg) -> std::string {
1430  std::string docstring;
1431  dict entries = arg.attr("__entries");
1432  if (((PyTypeObject *) arg.ptr())->tp_doc)
1433  docstring += std::string(((PyTypeObject *) arg.ptr())->tp_doc) + "\n\n";
1434  docstring += "Members:";
1435  for (const auto &kv : entries) {
1436  auto key = std::string(pybind11::str(kv.first));
1437  auto comment = kv.second[int_(1)];
1438  docstring += "\n\n " + key;
1439  if (!comment.is_none())
1440  docstring += " : " + (std::string) pybind11::str(comment);
1441  }
1442  return docstring;
1443  }
1444  ), none(), none(), "");
1445 
1446  m_base.attr("__members__") = static_property(cpp_function(
1447  [](handle arg) -> dict {
1448  dict entries = arg.attr("__entries"), m;
1449  for (const auto &kv : entries)
1450  m[kv.first] = kv.second[int_(0)];
1451  return m;
1452  }), none(), none(), ""
1453  );
1454 
1455  #define PYBIND11_ENUM_OP_STRICT(op, expr, strict_behavior) \
1456  m_base.attr(op) = cpp_function( \
1457  [](object a, object b) { \
1458  if (!a.get_type().is(b.get_type())) \
1459  strict_behavior; \
1460  return expr; \
1461  }, \
1462  is_method(m_base))
1463 
1464  #define PYBIND11_ENUM_OP_CONV(op, expr) \
1465  m_base.attr(op) = cpp_function( \
1466  [](object a_, object b_) { \
1467  int_ a(a_), b(b_); \
1468  return expr; \
1469  }, \
1470  is_method(m_base))
1471 
1472  if (is_convertible) {
1473  PYBIND11_ENUM_OP_CONV("__eq__", !b.is_none() && a.equal(b));
1474  PYBIND11_ENUM_OP_CONV("__ne__", b.is_none() || !a.equal(b));
1475 
1476  if (is_arithmetic) {
1477  PYBIND11_ENUM_OP_CONV("__lt__", a < b);
1478  PYBIND11_ENUM_OP_CONV("__gt__", a > b);
1479  PYBIND11_ENUM_OP_CONV("__le__", a <= b);
1480  PYBIND11_ENUM_OP_CONV("__ge__", a >= b);
1481  PYBIND11_ENUM_OP_CONV("__and__", a & b);
1482  PYBIND11_ENUM_OP_CONV("__rand__", a & b);
1483  PYBIND11_ENUM_OP_CONV("__or__", a | b);
1484  PYBIND11_ENUM_OP_CONV("__ror__", a | b);
1485  PYBIND11_ENUM_OP_CONV("__xor__", a ^ b);
1486  PYBIND11_ENUM_OP_CONV("__rxor__", a ^ b);
1487  }
1488  } else {
1489  PYBIND11_ENUM_OP_STRICT("__eq__", int_(a).equal(int_(b)), return false);
1490  PYBIND11_ENUM_OP_STRICT("__ne__", !int_(a).equal(int_(b)), return true);
1491 
1492  if (is_arithmetic) {
1493  #define PYBIND11_THROW throw type_error("Expected an enumeration of matching type!");
1494  PYBIND11_ENUM_OP_STRICT("__lt__", int_(a) < int_(b), PYBIND11_THROW);
1495  PYBIND11_ENUM_OP_STRICT("__gt__", int_(a) > int_(b), PYBIND11_THROW);
1496  PYBIND11_ENUM_OP_STRICT("__le__", int_(a) <= int_(b), PYBIND11_THROW);
1497  PYBIND11_ENUM_OP_STRICT("__ge__", int_(a) >= int_(b), PYBIND11_THROW);
1498  #undef PYBIND11_THROW
1499  }
1500  }
1501 
1502  #undef PYBIND11_ENUM_OP_CONV
1503  #undef PYBIND11_ENUM_OP_STRICT
1504 
1505  object getstate = cpp_function(
1506  [](object arg) { return int_(arg); }, is_method(m_base));
1507 
1508  m_base.attr("__getstate__") = getstate;
1509  m_base.attr("__hash__") = getstate;
1510  }
1511 
1512  PYBIND11_NOINLINE void value(char const* name_, object value, const char *doc = nullptr) {
1513  dict entries = m_base.attr("__entries");
1514  str name(name_);
1515  if (entries.contains(name)) {
1516  std::string type_name = (std::string) str(m_base.attr("__name__"));
1517  throw value_error(type_name + ": element \"" + std::string(name_) + "\" already exists!");
1518  }
1519 
1520  entries[name] = std::make_pair(value, doc);
1521  m_base.attr(name) = value;
1522  }
1523 
1524  PYBIND11_NOINLINE void export_values() {
1525  dict entries = m_base.attr("__entries");
1526  for (const auto &kv : entries)
1527  m_parent.attr(kv.first) = kv.second[int_(0)];
1528  }
1529 
1530  handle m_base;
1532 };
1533 
1534 NAMESPACE_END(detail)
1535 
1536 template <typename Type> class enum_ : public class_<Type> {
1538 public:
1540  using Base::def;
1541  using Base::attr;
1542  using Base::def_property_readonly;
1543  using Base::def_property_readonly_static;
1545 
1546  template <typename... Extra>
1547  enum_(const handle &scope, const char *name, const Extra&... extra)
1548  : class_<Type>(scope, name, extra...), m_base(*this, scope) {
1549  constexpr bool is_arithmetic = detail::any_of<std::is_same<arithmetic, Extra>...>::value;
1550  constexpr bool is_convertible = std::is_convertible<Type, Scalar>::value;
1551  m_base.init(is_arithmetic, is_convertible);
1552 
1553  def(init([](Scalar i) { return static_cast<Type>(i); }));
1554  def("__int__", [](Type value) { return (Scalar) value; });
1555  #if PY_MAJOR_VERSION < 3
1556  def("__long__", [](Type value) { return (Scalar) value; });
1557  #endif
1559  [](Type &value, Scalar arg) { value = static_cast<Type>(arg); },
1560  is_method(*this));
1561  attr("__setstate__") = setstate;
1562  }
1563 
1566  m_base.export_values();
1567  return *this;
1568  }
1569 
1571  enum_& value(char const* name, Type value, const char *doc = nullptr) {
1572  m_base.value(name, pybind11::cast(value, return_value_policy::copy), doc);
1573  return *this;
1574  }
1575 
1576 private:
1578 };
1579 
1580 NAMESPACE_BEGIN(detail)
1581 
1582 
1583 inline void keep_alive_impl(handle nurse, handle patient) {
1584  if (!nurse || !patient)
1585  pybind11_fail("Could not activate keep_alive!");
1586 
1587  if (patient.is_none() || nurse.is_none())
1588  return; /* Nothing to keep alive or nothing to be kept alive by */
1589 
1590  auto tinfo = all_type_info(Py_TYPE(nurse.ptr()));
1591  if (!tinfo.empty()) {
1592  /* It's a pybind-registered type, so we can store the patient in the
1593  * internal list. */
1594  add_patient(nurse.ptr(), patient.ptr());
1595  }
1596  else {
1597  /* Fall back to clever approach based on weak references taken from
1598  * Boost.Python. This is not used for pybind-registered types because
1599  * the objects can be destroyed out-of-order in a GC pass. */
1600  cpp_function disable_lifesupport(
1601  [patient](handle weakref) { patient.dec_ref(); weakref.dec_ref(); });
1602 
1603  weakref wr(nurse, disable_lifesupport);
1604 
1605  patient.inc_ref(); /* reference patient and leak the weak reference */
1606  (void) wr.release();
1607  }
1608 }
1609 
1610 PYBIND11_NOINLINE inline void keep_alive_impl(size_t Nurse, size_t Patient, function_call &call, handle ret) {
1611  auto get_arg = [&](size_t n) {
1612  if (n == 0)
1613  return ret;
1614  else if (n == 1 && call.init_self)
1615  return call.init_self;
1616  else if (n <= call.args.size())
1617  return call.args[n - 1];
1618  return handle();
1619  };
1620 
1621  keep_alive_impl(get_arg(Nurse), get_arg(Patient));
1622 }
1623 
1624 inline std::pair<decltype(internals::registered_types_py)::iterator, bool> all_type_info_get_cache(PyTypeObject *type) {
1625  auto res = get_internals().registered_types_py
1626 #ifdef __cpp_lib_unordered_map_try_emplace
1627  .try_emplace(type);
1628 #else
1629  .emplace(type, std::vector<detail::type_info *>());
1630 #endif
1631  if (res.second) {
1632  // New cache entry created; set up a weak reference to automatically remove it if the type
1633  // gets destroyed:
1634  weakref((PyObject *) type, cpp_function([type](handle wr) {
1635  get_internals().registered_types_py.erase(type);
1636  wr.dec_ref();
1637  })).release();
1638  }
1639 
1640  return res;
1641 }
1642 
1643 template <typename Iterator, typename Sentinel, bool KeyIterator, return_value_policy Policy>
1645  Iterator it;
1646  Sentinel end;
1648 };
1649 
1650 NAMESPACE_END(detail)
1651 
1652 template <return_value_policy Policy = return_value_policy::reference_internal,
1654  typename Iterator,
1655  typename Sentinel,
1656  typename ValueType = decltype(*std::declval<Iterator>()),
1657  typename... Extra>
1658 iterator make_iterator(Iterator first, Sentinel last, Extra &&... extra) {
1660 
1661  if (!detail::get_type_info(typeid(state), false)) {
1662  class_<state>(handle(), "iterator", pybind11::module_local())
1663  .def("__iter__", [](state &s) -> state& { return s; })
1664  .def("__next__", [](state &s) -> ValueType {
1665  if (!s.first_or_done)
1666  ++s.it;
1667  else
1668  s.first_or_done = false;
1669  if (s.it == s.end) {
1670  s.first_or_done = true;
1671  throw stop_iteration();
1672  }
1673  return *s.it;
1674  }, std::forward<Extra>(extra)..., Policy);
1675  }
1676 
1677  return cast(state{first, last, true});
1678 }
1679 
1682 template <return_value_policy Policy = return_value_policy::reference_internal,
1683  typename Iterator,
1684  typename Sentinel,
1685  typename KeyType = decltype((*std::declval<Iterator>()).first),
1686  typename... Extra>
1687 iterator make_key_iterator(Iterator first, Sentinel last, Extra &&... extra) {
1689 
1690  if (!detail::get_type_info(typeid(state), false)) {
1691  class_<state>(handle(), "iterator", pybind11::module_local())
1692  .def("__iter__", [](state &s) -> state& { return s; })
1693  .def("__next__", [](state &s) -> KeyType {
1694  if (!s.first_or_done)
1695  ++s.it;
1696  else
1697  s.first_or_done = false;
1698  if (s.it == s.end) {
1699  s.first_or_done = true;
1700  throw stop_iteration();
1701  }
1702  return (*s.it).first;
1703  }, std::forward<Extra>(extra)..., Policy);
1704  }
1705 
1706  return cast(state{first, last, true});
1707 }
1708 
1711 template <return_value_policy Policy = return_value_policy::reference_internal,
1712  typename Type, typename... Extra> iterator make_iterator(Type &value, Extra&&... extra) {
1713  return make_iterator<Policy>(std::begin(value), std::end(value), extra...);
1714 }
1715 
1718 template <return_value_policy Policy = return_value_policy::reference_internal,
1719  typename Type, typename... Extra> iterator make_key_iterator(Type &value, Extra&&... extra) {
1720  return make_key_iterator<Policy>(std::begin(value), std::end(value), extra...);
1721 }
1722 
1723 template <typename InputType, typename OutputType> void implicitly_convertible() {
1724  struct set_flag {
1725  bool &flag;
1726  set_flag(bool &flag) : flag(flag) { flag = true; }
1727  ~set_flag() { flag = false; }
1728  };
1729  auto implicit_caster = [](PyObject *obj, PyTypeObject *type) -> PyObject * {
1730  static bool currently_used = false;
1731  if (currently_used) // implicit conversions are non-reentrant
1732  return nullptr;
1733  set_flag flag_helper(currently_used);
1734  if (!detail::make_caster<InputType>().load(obj, false))
1735  return nullptr;
1736  tuple args(1);
1737  args[0] = obj;
1738  PyObject *result = PyObject_Call((PyObject *) type, args.ptr(), nullptr);
1739  if (result == nullptr)
1740  PyErr_Clear();
1741  return result;
1742  };
1743 
1744  if (auto tinfo = detail::get_type_info(typeid(OutputType)))
1745  tinfo->implicit_conversions.push_back(implicit_caster);
1746  else
1747  pybind11_fail("implicitly_convertible: Unable to find type " + type_id<OutputType>());
1748 }
1749 
1750 template <typename ExceptionTranslator>
1751 void register_exception_translator(ExceptionTranslator&& translator) {
1752  detail::get_internals().registered_exception_translators.push_front(
1753  std::forward<ExceptionTranslator>(translator));
1754 }
1755 
1763 template <typename type>
1764 class exception : public object {
1765 public:
1766  exception() = default;
1767  exception(handle scope, const char *name, PyObject *base = PyExc_Exception) {
1768  std::string full_name = scope.attr("__name__").cast<std::string>() +
1769  std::string(".") + name;
1770  m_ptr = PyErr_NewException(const_cast<char *>(full_name.c_str()), base, NULL);
1771  if (hasattr(scope, name))
1772  pybind11_fail("Error during initialization: multiple incompatible "
1773  "definitions with name \"" + std::string(name) + "\"");
1774  scope.attr(name) = *this;
1775  }
1776 
1777  // Sets the current python exception to this exception object with the given message
1778  void operator()(const char *message) {
1779  PyErr_SetString(m_ptr, message);
1780  }
1781 };
1782 
1783 NAMESPACE_BEGIN(detail)
1784 // Returns a reference to a function-local static exception object used in the simple
1785 // register_exception approach below. (It would be simpler to have the static local variable
1786 // directly in register_exception, but that makes clang <3.5 segfault - issue #1349).
1787 template <typename CppException>
1789 NAMESPACE_END(detail)
1790 
1791 
1797 template <typename CppException>
1799  const char *name,
1800  PyObject *base = PyExc_Exception) {
1801  auto &ex = detail::get_exception_object<CppException>();
1802  if (!ex) ex = exception<CppException>(scope, name, base);
1803 
1804  register_exception_translator([](std::exception_ptr p) {
1805  if (!p) return;
1806  try {
1807  std::rethrow_exception(p);
1808  } catch (const CppException &e) {
1809  detail::get_exception_object<CppException>()(e.what());
1810  }
1811  });
1812  return ex;
1813 }
1814 
1815 NAMESPACE_BEGIN(detail)
1817  auto strings = tuple(args.size());
1818  for (size_t i = 0; i < args.size(); ++i) {
1819  strings[i] = str(args[i]);
1820  }
1821  auto sep = kwargs.contains("sep") ? kwargs["sep"] : cast(" ");
1822  auto line = sep.attr("join")(strings);
1823 
1824  object file;
1825  if (kwargs.contains("file")) {
1826  file = kwargs["file"].cast<object>();
1827  } else {
1828  try {
1829  file = module::import("sys").attr("stdout");
1830  } catch (const error_already_set &) {
1831  /* If print() is called from code that is executed as
1832  part of garbage collection during interpreter shutdown,
1833  importing 'sys' can fail. Give up rather than crashing the
1834  interpreter in this case. */
1835  return;
1836  }
1837  }
1838 
1839  auto write = file.attr("write");
1840  write(line);
1841  write(kwargs.contains("end") ? kwargs["end"] : cast("\n"));
1842 
1843  if (kwargs.contains("flush") && kwargs["flush"].cast<bool>())
1844  file.attr("flush")();
1845 }
1846 NAMESPACE_END(detail)
1847 
1848 template <return_value_policy policy = return_value_policy::automatic_reference, typename... Args>
1849 void print(Args &&...args) {
1850  auto c = detail::collect_arguments<policy>(std::forward<Args>(args)...);
1851  detail::print(c.args(), c.kwargs());
1852 }
1853 
1854 #if defined(WITH_THREAD) && !defined(PYPY_VERSION)
1855 
1856 /* The functions below essentially reproduce the PyGILState_* API using a RAII
1857  * pattern, but there are a few important differences:
1858  *
1859  * 1. When acquiring the GIL from an non-main thread during the finalization
1860  * phase, the GILState API blindly terminates the calling thread, which
1861  * is often not what is wanted. This API does not do this.
1862  *
1863  * 2. The gil_scoped_release function can optionally cut the relationship
1864  * of a PyThreadState and its associated thread, which allows moving it to
1865  * another thread (this is a fairly rare/advanced use case).
1866  *
1867  * 3. The reference count of an acquired thread state can be controlled. This
1868  * can be handy to prevent cases where callbacks issued from an external
1869  * thread would otherwise constantly construct and destroy thread state data
1870  * structures.
1871  *
1872  * See the Python bindings of NanoGUI (http://github.com/wjakob/nanogui) for an
1873  * example which uses features 2 and 3 to migrate the Python thread of
1874  * execution to another thread (to run the event loop on the original thread,
1875  * in this case).
1876  */
1877 
1878 class gil_scoped_acquire {
1879 public:
1881  auto const &internals = detail::get_internals();
1882  tstate = (PyThreadState *) PYBIND11_TLS_GET_VALUE(internals.tstate);
1883 
1884  if (!tstate) {
1885  /* Check if the GIL was acquired using the PyGILState_* API instead (e.g. if
1886  calling from a Python thread). Since we use a different key, this ensures
1887  we don't create a new thread state and deadlock in PyEval_AcquireThread
1888  below. Note we don't save this state with internals.tstate, since we don't
1889  create it we would fail to clear it (its reference count should be > 0). */
1890  tstate = PyGILState_GetThisThreadState();
1891  }
1892 
1893  if (!tstate) {
1894  tstate = PyThreadState_New(internals.istate);
1895  #if !defined(NDEBUG)
1896  if (!tstate)
1897  pybind11_fail("scoped_acquire: could not create thread state!");
1898  #endif
1899  tstate->gilstate_counter = 0;
1900  PYBIND11_TLS_REPLACE_VALUE(internals.tstate, tstate);
1901  } else {
1903  }
1904 
1905  if (release) {
1906  /* Work around an annoying assertion in PyThreadState_Swap */
1907  #if defined(Py_DEBUG)
1908  PyInterpreterState *interp = tstate->interp;
1909  tstate->interp = nullptr;
1910  #endif
1911  PyEval_AcquireThread(tstate);
1912  #if defined(Py_DEBUG)
1913  tstate->interp = interp;
1914  #endif
1915  }
1916 
1917  inc_ref();
1918  }
1919 
1920  void inc_ref() {
1921  ++tstate->gilstate_counter;
1922  }
1923 
1924  PYBIND11_NOINLINE void dec_ref() {
1925  --tstate->gilstate_counter;
1926  #if !defined(NDEBUG)
1927  if (detail::get_thread_state_unchecked() != tstate)
1928  pybind11_fail("scoped_acquire::dec_ref(): thread state must be current!");
1929  if (tstate->gilstate_counter < 0)
1930  pybind11_fail("scoped_acquire::dec_ref(): reference count underflow!");
1931  #endif
1932  if (tstate->gilstate_counter == 0) {
1933  #if !defined(NDEBUG)
1934  if (!release)
1935  pybind11_fail("scoped_acquire::dec_ref(): internal error!");
1936  #endif
1937  PyThreadState_Clear(tstate);
1938  PyThreadState_DeleteCurrent();
1939  PYBIND11_TLS_DELETE_VALUE(detail::get_internals().tstate);
1940  release = false;
1941  }
1942  }
1943 
1945  dec_ref();
1946  if (release)
1947  PyEval_SaveThread();
1948  }
1949 private:
1950  PyThreadState *tstate = nullptr;
1951  bool release = true;
1952 };
1953 
1954 class gil_scoped_release {
1955 public:
1956  explicit gil_scoped_release(bool disassoc = false) : disassoc(disassoc) {
1957  // `get_internals()` must be called here unconditionally in order to initialize
1958  // `internals.tstate` for subsequent `gil_scoped_acquire` calls. Otherwise, an
1959  // initialization race could occur as multiple threads try `gil_scoped_acquire`.
1960  const auto &internals = detail::get_internals();
1961  tstate = PyEval_SaveThread();
1962  if (disassoc) {
1963  auto key = internals.tstate;
1965  }
1966  }
1967  ~gil_scoped_release() {
1968  if (!tstate)
1969  return;
1970  PyEval_RestoreThread(tstate);
1971  if (disassoc) {
1972  auto key = detail::get_internals().tstate;
1973  PYBIND11_TLS_REPLACE_VALUE(key, tstate);
1974  }
1975  }
1976 private:
1977  PyThreadState *tstate;
1978  bool disassoc;
1979 };
1980 #elif defined(PYPY_VERSION)
1981 class gil_scoped_acquire {
1982  PyGILState_STATE state;
1983 public:
1984  gil_scoped_acquire() { state = PyGILState_Ensure(); }
1985  ~gil_scoped_acquire() { PyGILState_Release(state); }
1986 };
1987 
1988 class gil_scoped_release {
1989  PyThreadState *state;
1990 public:
1991  gil_scoped_release() { state = PyEval_SaveThread(); }
1992  ~gil_scoped_release() { PyEval_RestoreThread(state); }
1993 };
1994 #else
1997 #endif
1998 
1999 error_already_set::~error_already_set() {
2000  if (m_type) {
2001  gil_scoped_acquire gil;
2003  m_type.release().dec_ref();
2004  m_value.release().dec_ref();
2005  m_trace.release().dec_ref();
2006  }
2007 }
2008 
2009 inline function get_type_overload(const void *this_ptr, const detail::type_info *this_type, const char *name) {
2010  handle self = detail::get_object_handle(this_ptr, this_type);
2011  if (!self)
2012  return function();
2013  handle type = self.get_type();
2014  auto key = std::make_pair(type.ptr(), name);
2015 
2016  /* Cache functions that aren't overloaded in Python to avoid
2017  many costly Python dictionary lookups below */
2018  auto &cache = detail::get_internals().inactive_overload_cache;
2019  if (cache.find(key) != cache.end())
2020  return function();
2021 
2022  function overload = getattr(self, name, function());
2023  if (overload.is_cpp_function()) {
2024  cache.insert(key);
2025  return function();
2026  }
2027 
2028  /* Don't call dispatch code if invoked from overridden function.
2029  Unfortunately this doesn't work on PyPy. */
2030 #if !defined(PYPY_VERSION)
2031  PyFrameObject *frame = PyThreadState_Get()->frame;
2032  if (frame && (std::string) str(frame->f_code->co_name) == name &&
2033  frame->f_code->co_argcount > 0) {
2034  PyFrame_FastToLocals(frame);
2035  PyObject *self_caller = PyDict_GetItem(
2036  frame->f_locals, PyTuple_GET_ITEM(frame->f_code->co_varnames, 0));
2037  if (self_caller == self.ptr())
2038  return function();
2039  }
2040 #else
2041  /* PyPy currently doesn't provide a detailed cpyext emulation of
2042  frame objects, so we have to emulate this using Python. This
2043  is going to be slow..*/
2044  dict d; d["self"] = self; d["name"] = pybind11::str(name);
2045  PyObject *result = PyRun_String(
2046  "import inspect\n"
2047  "frame = inspect.currentframe()\n"
2048  "if frame is not None:\n"
2049  " frame = frame.f_back\n"
2050  " if frame is not None and str(frame.f_code.co_name) == name and "
2051  "frame.f_code.co_argcount > 0:\n"
2052  " self_caller = frame.f_locals[frame.f_code.co_varnames[0]]\n"
2053  " if self_caller == self:\n"
2054  " self = None\n",
2055  Py_file_input, d.ptr(), d.ptr());
2056  if (result == nullptr)
2057  throw error_already_set();
2058  if (d["self"].is_none())
2059  return function();
2060  Py_DECREF(result);
2061 #endif
2062 
2063  return overload;
2064 }
2065 
2074 template <class T> function get_overload(const T *this_ptr, const char *name) {
2075  auto tinfo = detail::get_type_info(typeid(T));
2076  return tinfo ? get_type_overload(this_ptr, tinfo, name) : function();
2077 }
2078 
2079 #define PYBIND11_OVERLOAD_INT(ret_type, cname, name, ...) { \
2080  pybind11::gil_scoped_acquire gil; \
2081  pybind11::function overload = pybind11::get_overload(static_cast<const cname *>(this), name); \
2082  if (overload) { \
2083  auto o = overload(__VA_ARGS__); \
2084  if (pybind11::detail::cast_is_temporary_value_reference<ret_type>::value) { \
2085  static pybind11::detail::overload_caster_t<ret_type> caster; \
2086  return pybind11::detail::cast_ref<ret_type>(std::move(o), caster); \
2087  } \
2088  else return pybind11::detail::cast_safe<ret_type>(std::move(o)); \
2089  } \
2090  }
2091 
2109 #define PYBIND11_OVERLOAD_NAME(ret_type, cname, name, fn, ...) \
2110  PYBIND11_OVERLOAD_INT(PYBIND11_TYPE(ret_type), PYBIND11_TYPE(cname), name, __VA_ARGS__) \
2111  return cname::fn(__VA_ARGS__)
2112 
2117 #define PYBIND11_OVERLOAD_PURE_NAME(ret_type, cname, name, fn, ...) \
2118  PYBIND11_OVERLOAD_INT(PYBIND11_TYPE(ret_type), PYBIND11_TYPE(cname), name, __VA_ARGS__) \
2119  pybind11::pybind11_fail("Tried to call pure virtual function \"" PYBIND11_STRINGIFY(cname) "::" name "\"");
2120 
2145 #define PYBIND11_OVERLOAD(ret_type, cname, fn, ...) \
2146  PYBIND11_OVERLOAD_NAME(PYBIND11_TYPE(ret_type), PYBIND11_TYPE(cname), #fn, fn, __VA_ARGS__)
2147 
2152 #define PYBIND11_OVERLOAD_PURE(ret_type, cname, fn, ...) \
2153  PYBIND11_OVERLOAD_PURE_NAME(PYBIND11_TYPE(ret_type), PYBIND11_TYPE(cname), #fn, fn, __VA_ARGS__)
2154 
2156 
2157 #if defined(_MSC_VER) && !defined(__INTEL_COMPILER)
2158 # pragma warning(pop)
2159 #elif defined(__GNUG__) && !defined(__clang__)
2160 # pragma GCC diagnostic pop
2161 #endif
dict globals()
Definition: pybind11.h:880
class_ & def_readwrite_static(const char *name, D *pm, const Extra &...extra)
Definition: pybind11.h:1203
class_ & def_property_static(const char *name, const Getter &fget, const cpp_function &fset, const Extra &...extra)
Uses return_value_policy::reference by default.
Definition: pybind11.h:1261
class_ & def(const detail::initimpl::constructor< Args... > &init, const Extra &... extra)
Definition: pybind11.h:1140
constexpr int first(int i)
Implementation details for constexpr functions.
Annotation for methods.
Definition: attr.h:21
#define PYBIND11_ENUM_OP_STRICT(op, expr, strict_behavior)
Definition: pybind11.h:1455
std::uint16_t nargs
Number of arguments (including py::args and/or py::kwargs, if present)
Definition: attr.h:185
Implementation for py::pickle(GetState, SetState)
Definition: init.h:301
detail::exactly_one_t< is_holder, std::unique_ptr< type >, options... > holder_type
Definition: pybind11.h:1050
handle get_type() const
Return a handle to the Python type object underlying the instance.
Definition: pytypes.h:1430
void operator()(const char *message)
Definition: pybind11.h:1778
std::vector< handle > args
Arguments passed to the function:
Definition: cast.h:1866
PyMethodDef * def
Python method object.
Definition: attr.h:188
static detail::function_record * get_function_record(handle h)
Definition: pybind11.h:1363
Wraps an arbitrary C++ function/method/lambda function/.. into a callable Python object.
Definition: pybind11.h:56
value_and_holder get_value_and_holder(const type_info *find_type=nullptr, bool throw_if_missing=true)
Annotation for parent scope.
Definition: attr.h:27
constexpr const date::last_spec last
Definition: date.h:1834
Operator implementation generator.
Definition: attr.h:118
std::unordered_map< PyTypeObject *, std::vector< type_info * > > registered_types_py
Definition: internals.h:96
#define PYBIND11_NAMESPACE
Definition: detail/common.h:26
constexpr const char * type_name()
This one should not be used, since vector types print the internal type.
Definition: CLI11.hpp:1058
PyObject * ptr() const
Return the underlying PyObject * pointer.
Definition: pytypes.h:182
std::size_t size_t
object getattr(handle obj, handle name)
Definition: pytypes.h:403
static void init_instance(detail::instance *inst, const void *holder_ptr)
Definition: pybind11.h:1339
return_value_policy policy
Return value policy associated with this function.
Definition: attr.h:161
bool is_constructor
True if name == &#39;init&#39;.
Definition: attr.h:164
class_ & def_property(const char *name, const cpp_function &fget, const cpp_function &fset, const Extra &...extra)
Uses cpp_function&#39;s return_value_policy by default.
Definition: pybind11.h:1255
handle sibling
Python handle to the sibling function representing an overload chain.
Definition: attr.h:194
string release
Definition: conf.py:66
typename exactly_one< Predicate, Default, Ts... >::type exactly_one_t
void call_operator_delete(void *p, size_t s, size_t a)
Definition: pybind11.h:1004
void register_instance(instance *self, void *valptr, const type_info *tinfo)
Definition: class.h:234
RAII wrapper that temporarily clears any Python error state.
void setstate(value_and_holder &v_h, T &&result, bool need_alias)
Set just the C++ state. Same as __init__.
Definition: init.h:286
const char * name
If non-null, this is a named kwargs argument.
Definition: cast.h:1792
cpp_function(Return(*f)(Args...), const Extra &... extra)
Construct a cpp_function from a vanilla function pointer.
Definition: pybind11.h:63
detail::exactly_one_t< is_subtype, void, options... > type_alias
Definition: pybind11.h:1048
const char * name
Name of the class.
Definition: attr.h:210
detail::type_info * get_local_type_info(const std::type_index &tp)
Definition: cast.h:169
void add_patient(PyObject *nurse, PyObject *patient)
Definition: class.h:290
typename void_t_impl< Ts... >::type void_t
class_ & def_buffer(Func &&func)
Definition: pybind11.h:1163
void(* free_data)(function_record *ptr)
Pointer to custom destructor for &#39;data&#39; (if needed)
Definition: attr.h:158
static constexpr size_t size_in_ptrs(size_t s)
static void execute(Class &cl, const Extra &... extra)
Definition: init.h:204
enum_base(handle base, handle parent)
Definition: pybind11.h:1396
void set_holder_constructed(bool v=true)
Definition: cast.h:238
void(* dealloc)(detail::value_and_holder &)
Function pointer to class_<..>::dealloc.
Definition: attr.h:231
std::is_same< bools< Ts::value..., true >, bools< true, Ts::value... > > all_of
class_ & def_property_readonly(const char *name, const Getter &fget, const Extra &...extra)
Uses return_value_policy::reference_internal by default.
Definition: pybind11.h:1219
class_ & def_buffer(Return(Class::*func)(Args...))
Definition: pybind11.h:1176
PyThreadState * get_thread_state_unchecked()
Definition: cast.h:465
bool holder_constructed() const
Definition: cast.h:233
bool equal(object_api const &other) const
Equivalent to obj == other in Python.
Definition: pytypes.h:118
bool contains(T &&key) const
Definition: pytypes.h:1228
#define PYBIND11_INSTANCE_METHOD_GET_FUNCTION
static void init_holder_from_existing(const detail::value_and_holder &v_h, const holder_type *holder_ptr, std::false_type)
Definition: pybind11.h:1318
enum_ & value(char const *name, Type value, const char *doc=nullptr)
Add an enumeration entry.
Definition: pybind11.h:1571
type_map< type_info * > & registered_local_types_cpp()
Works like internals.registered_types_cpp, but for module-local registered types: ...
Definition: internals.h:261
void initialize_generic(detail::function_record *rec, const char *text, const std::type_info *const *types, size_t args)
Register a function call with Python (generic non-templated code goes here)
Definition: pybind11.h:193
handle parent
The parent, if any.
Definition: cast.h:1876
bool_constant< std::is_base_of< Base, Derived >::value &&!std::is_same< Base, Derived >::value > is_strict_base_of
Internal data structure which holds metadata about a bound function (signature, overloads, etc.)
Definition: attr.h:134
detail::type_info * get_global_type_info(const std::type_index &tp)
Definition: cast.h:177
const std::vector< detail::type_info * > & all_type_info(PyTypeObject *type)
Definition: cast.h:146
Internal data associated with a single function call.
Definition: cast.h:1859
constexpr descr< N - 1 > _(char const(&text)[N])
Definition: descr.h:54
Ret init(CFunc &&c, AFunc &&a)
Definition: pybind11.h:1383
test_initializer class_("class_", test_submodule_class_)
Definition: pybind11.h:888
arr data(const arr &a, Ix... index)
Binds C++ enumerations and enumeration classes to Python.
Definition: pybind11.h:1537
def msg()
Definition: conftest.py:165
exception(handle scope, const char *name, PyObject *base=PyExc_Exception)
Definition: pybind11.h:1767
handle init_self
If this is a call to an initializer, this argument contains self
Definition: cast.h:1879
static void init_holder_from_existing(const detail::value_and_holder &v_h, const holder_type *holder_ptr, std::true_type)
Definition: pybind11.h:1313
class_ & def_readonly_static(const char *name, const D *pm, const Extra &...extra)
Definition: pybind11.h:1211
Information record describing a Python buffer object.
Definition: buffer_info.h:17
Wrapper for Python extension modules.
Definition: pybind11.h:789
const handle & dec_ref() const &
Definition: pytypes.h:197
#define PYBIND11_NOINLINE
Definition: detail/common.h:86
class_ & def_cast(const detail::op_< id, ot, L, R > &op, const Extra &... extra)
Definition: pybind11.h:1134
The &#39;instance&#39; type which needs to be standard layout (need to be able to use &#39;offsetof&#39;) ...
iterator make_iterator(Type &value, Extra &&... extra)
Definition: pybind11.h:1712
obj_attr_accessor attr(handle key) const
Definition: pytypes.h:1410
static void destruct(detail::function_record *rec)
When a cpp_function is GCed, release any memory allocated by pybind11.
Definition: pybind11.h:401
typename std::underlying_type< Type >::type Scalar
Definition: pybind11.h:1544
bool is_none() const
Equivalent to obj is None in Python.
Definition: pytypes.h:116
size_t function_call handle ret
Definition: pybind11.h:1610
op_< op_int, op_u, self_t, undefined_t > int_(const self_t &)
Definition: operators.h:154
detail::is_strict_base_of< Type, T > is_subtype
Definition: pybind11.h:1040
auto method_adaptor(Return(Class::*pmf)(Args...)) -> Return(Derived::*)(Args...)
Definition: pybind11.h:1024
detail::initimpl::pickle_factory< GetState, SetState > pickle(GetState &&g, SetState &&s)
Definition: pybind11.h:1390
handle scope
Python handle to the parent scope (a class or a module)
Definition: attr.h:191
handle scope
Handle to the parent scope.
Definition: attr.h:207
iterator make_key_iterator(Type &value, Extra &&... extra)
Definition: pybind11.h:1719
void(* init_instance)(instance *, const void *)
Function pointer to class_<..>::init_instance.
Definition: attr.h:228
#define NAMESPACE_END(name)
Definition: detail/common.h:16
exception< CppException > & register_exception(handle scope, const char *name, PyObject *base=PyExc_Exception)
Definition: pybind11.h:1798
#define PYBIND11_DESCR_CONSTEXPR
Definition: descr.h:18
cpp_function(Return(Class::*f)(Arg...), const Extra &... extra)
Construct a cpp_function from a class method (non-const)
Definition: pybind11.h:77
const detail::type_info * type
Definition: cast.h:209
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:482
size_t size() const
Definition: pytypes.h:1223
bool module_local
Is the class definition local to the module shared object?
Definition: attr.h:255
class_ & def_property_readonly_static(const char *name, const cpp_function &fget, const Extra &...extra)
Uses cpp_function&#39;s return_value_policy by default.
Definition: pybind11.h:1238
return_value_policy
Approach used to cast a previously unknown C++ instance into a Python object.
enum_ & export_values()
Export enumeration entries into the parent scope.
Definition: pybind11.h:1565
#define PYBIND11_OBJECT_DEFAULT(Name, Parent, CheckFun)
Definition: pytypes.h:807
void execute_cast(Class &cl, const Extra &... extra) const
Definition: operators.h:66
static void dealloc(detail::value_and_holder &v_h)
Deallocates an instance; via holder, if constructed; otherwise via operator delete.
Definition: pybind11.h:1349
Annotation indicating that a function is an overload associated with a given "sibling".
Definition: attr.h:36
bool multiple_inheritance
Multiple inheritance marker.
Definition: attr.h:243
bool is_method
True if this is a method.
Definition: attr.h:182
bool owned
If true, the pointer is owned which means we&#39;re free to manage it with a holder.
detail::initimpl::alias_constructor< Args... > init_alias()
Definition: pybind11.h:1374
class_ & def(const detail::op_< id, ot, L, R > &op, const Extra &... extra)
Definition: pybind11.h:1128
size_t function_call & call
Definition: pybind11.h:1610
enum_(const handle &scope, const char *name, const Extra &... extra)
Definition: pybind11.h:1547
static bool show_user_defined_docstrings()
Definition: options.h:43
char * name
Function name.
Definition: attr.h:140
negation< any_of< Ts... > > none_of
#define PYBIND11_THROW
#define PYBIND11_TLS_DELETE_VALUE(key)
Definition: internals.h:33
#define PYBIND11_EXPAND_SIDE_EFFECTS(PATTERN)
void initialize(Func &&f, Return(*)(Args...), const Extra &... extra)
Special internal constructor for functors, lambda functions, etc.
Definition: pybind11.h:100
void set_operator_new(...)
Definition: pybind11.h:990
constexpr size_t constexpr_sum()
Compile-time integer sum.
const std::type_info * type
Definition: attr.h:213
bool default_holder
Is the default (unique_ptr) holder type used?
Definition: attr.h:252
def d(s)
Definition: mkdoc.py:69
write(kwargs.contains("end") ? kwargs["end"] :cast("\))
class_ & def(detail::initimpl::factory< Args... > &&init, const Extra &... extra)
Definition: pybind11.h:1152
#define PYBIND11_ENUM_OP_CONV(op, expr)
Definition: pybind11.h:1464
std::pair< decltype(internals::registered_types_py)::iterator, bool > all_type_info_get_cache(PyTypeObject *type)
Definition: pybind11.h:1624
class_ & def_static(const char *name_, Func &&f, const Extra &... extra)
Definition: pybind11.h:1118
#define PYBIND11_MODULE_LOCAL_ID
Definition: internals.h:166
str repr(handle h)
Definition: pytypes.h:1383
class_ & def_buffer(Return(Class::*func)(Args...) const)
Definition: pybind11.h:1181
void *(* operator_new)(size_t)
The global operator new can be overridden with a class-specific variant.
Definition: attr.h:225
void register_exception_translator(ExceptionTranslator &&translator)
Definition: pybind11.h:1751
str format(Args &&...args) const
Definition: pytypes.h:928
Annotation for documentation.
Definition: attr.h:30
op_id
Enumeration with all supported operator types.
Definition: operators.h:25
def doc()
Definition: conftest.py:152
#define PYBIND11_TRY_NEXT_OVERLOAD
static PyObject * dispatcher(PyObject *self, PyObject *args_in, PyObject *kwargs_in)
Main dispatch logic for calls to functions bound using pybind11.
Definition: pybind11.h:424
const function_record & func
The function data:
Definition: cast.h:1863
detail::enum_base m_base
Definition: pybind11.h:1577
static void execute(Class &cl, const Extra &... extra)
Definition: init.h:172
static void init_holder(detail::instance *inst, detail::value_and_holder &v_h, const holder_type *, const std::enable_shared_from_this< T > *)
Initialize holder object, variant 1: object derives from enable_shared_from_this. ...
Definition: pybind11.h:1296
void execute(Class &cl, const Extra &... extra) const
Definition: operators.h:54
Annotation indicating that a class derives from another given type.
Definition: attr.h:39
const detail::type_info * type
Definition: cast.h:453
Annotation for function names.
Definition: attr.h:33
#define PYBIND11_TLS_REPLACE_VALUE(key, value)
Definition: internals.h:35
static void init_holder(detail::instance *inst, detail::value_and_holder &v_h, const holder_type *holder_ptr, const void *)
Initialize holder object, variant 2: try to construct from existing holder object, if possible.
Definition: pybind11.h:1324
type_map< type_info * > registered_types_cpp
Definition: internals.h:95
const char * c_str(Args &&...args)
Definition: internals.h:271
class_ & def_property_readonly_static(const char *name, const Getter &fget, const Extra &...extra)
Uses return_value_policy::reference by default.
Definition: pybind11.h:1232
void implicitly_convertible()
Definition: pybind11.h:1723
exception< CppException > & get_exception_object()
Definition: pybind11.h:1788
detail::enable_if_t<!detail::move_never< T >::value, T > move(object &&obj)
Definition: cast.h:1685
handle release()
Definition: pytypes.h:247
function_record * next
Pointer to next overload.
Definition: attr.h:197
handle get_function(handle value)
Definition: pytypes.h:450
class_ & def(const detail::initimpl::alias_constructor< Args... > &init, const Extra &... extra)
Definition: pybind11.h:1146
#define PYBIND11_INSTANCE_METHOD_NEW(ptr, class_)
Include Python header, disable linking to pythonX_d.lib on Windows in debug mode. ...
return os str()
detail::is_strict_base_of< T, Type > is_base
Definition: pybind11.h:1041
class_ & def_property(const char *name, const Getter &fget, const cpp_function &fset, const Extra &...extra)
Definition: pybind11.h:1248
char * signature
Human-readable version of the function signature.
Definition: attr.h:146
cpp_function(std::nullptr_t)
Definition: pybind11.h:59
void * data[3]
Storage for the wrapped function pointer and captured data, if any.
Definition: attr.h:155
class_ & def_property_static(const char *name, const cpp_function &fget, const cpp_function &fset, const Extra &...extra)
Uses cpp_function&#39;s return_value_policy by default.
Definition: pybind11.h:1267
cpp_function(Func &&f, const Extra &... extra)
Construct a cpp_function from a lambda function (possibly with internal state)
Definition: pybind11.h:70
PyObject * make_new_python_type(const type_record &rec)
Definition: class.h:517
function get_overload(const T *this_ptr, const char *name)
Definition: pybind11.h:2074
#define NAMESPACE_BEGIN(name)
Definition: detail/common.h:13
size_t holder_size
How large is the type&#39;s holder?
Definition: attr.h:222
#define PYBIND11_INSTANCE_METHOD_CHECK
T cast() const &
handle(* impl)(function_call &)
Pointer to lambda function which converts arguments and performs the actual call. ...
Definition: attr.h:152
Thrown when pybind11::cast or handle::call fail due to a type casting error.
Generic support for creating new Python heap types.
Definition: pybind11.h:887
#define PYBIND11_TLS_GET_VALUE(key)
Definition: internals.h:31
T cast(const handle &handle)
Definition: cast.h:1659
def capture(capsys)
Definition: conftest.py:111
bool is_new_style_constructor
True if this is a new-style __init__ defined in detail/init.h
Definition: attr.h:167
std::vector< argument_record > args
List of registered keyword arguments.
Definition: attr.h:149
class_ & def_readonly(const char *name, const D C::*pm, const Extra &...extra)
Definition: pybind11.h:1195
class_ & def_property(const char *name, const Getter &fget, const Setter &fset, const Extra &...extra)
Uses return_value_policy::reference_internal by default.
Definition: pybind11.h:1244
void print(Args &&...args)
Definition: pybind11.h:1849
bool load(handle src, bool convert)
Definition: cast.h:491
bool_constant< std::is_base_of< Base, Derived >::value &&std::is_convertible< Derived *, Base * >::value > is_accessible_base_of
conditional_t< std::is_function< F >::value, F, typename conditional_t< std::is_pointer< F >::value||std::is_member_pointer< F >::value, std::remove_pointer< F >, strip_function_object< F > >::type > function_signature_t
function get_type_overload(const void *this_ptr, const detail::type_info *this_type, const char *name)
Definition: pybind11.h:2009
size_t type_align
What is the alignment of the underlying C++ type?
Definition: attr.h:219
Special data structure which (temporarily) holds metadata about a bound class.
Definition: attr.h:201
list bases
List of base classes of the newly created type.
Definition: attr.h:234
type_caster< intrinsic_t< type > > make_caster
Definition: cast.h:910
void setattr(handle obj, handle name, handle value)
Definition: pytypes.h:433
bool typename Extra class_ & def(const char *name_, Func &&f, const Extra &... extra)
Definition: pybind11.h:1110
const std::type_info & tinfo
Definition: numpy.h:1091
type_map< std::vector< bool(*)(PyObject *, void *&)> > direct_conversions
Definition: internals.h:99
class_ & def(detail::initimpl::pickle_factory< Args... > &&pf, const Extra &...extra)
Definition: pybind11.h:1158
size_t type_size
How large is the underlying C++ type?
Definition: attr.h:216
#define PYBIND11_OBJECT(Name, Parent, CheckFun)
Definition: pytypes.h:801
cpp_function(Return(Class::*f)(Arg...) const, const Extra &... extra)
Construct a cpp_function from a class method (const)
Definition: pybind11.h:84
typename std::enable_if< B, T >::type enable_if_t
from cpp_future import (convenient aliases from C++14/17)
const handle & inc_ref() const &
Definition: pytypes.h:190
auto to_string(T &&value) -> decltype(std::forward< T >(value))
Convert an object to a string (directly forward if this can become a string)
Definition: CLI11.hpp:1028
class_ & def_property_readonly(const char *name, const cpp_function &fget, const Extra &...extra)
Uses cpp_function&#39;s return_value_policy by default.
Definition: pybind11.h:1226
bool hasattr(handle obj, handle name)
Definition: pytypes.h:387
object name() const
Return the function name.
Definition: pybind11.h:90
Annotation that marks a class as local to the module:
Definition: attr.h:68
class_ & def_readwrite(const char *name, D C::*pm, const Extra &... extra)
Definition: pybind11.h:1186
keep_alive_impl(get_arg(Nurse), get_arg(Patient))