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 #include "detail/class.h"
14 #include "detail/init.h"
15 #include "attr.h"
16 #include "gil.h"
17 #include "options.h"
18 
19 #include <cstdlib>
20 #include <cstring>
21 #include <memory>
22 #include <new>
23 #include <string>
24 #include <utility>
25 #include <vector>
26 
27 #if defined(__cpp_lib_launder) && !(defined(_MSC_VER) && (_MSC_VER < 1914))
28 # define PYBIND11_STD_LAUNDER std::launder
29 # define PYBIND11_HAS_STD_LAUNDER 1
30 #else
31 # define PYBIND11_STD_LAUNDER
32 # define PYBIND11_HAS_STD_LAUNDER 0
33 #endif
34 #if defined(__GNUG__) && !defined(__clang__)
35 # include <cxxabi.h>
36 #endif
37 
38 /* https://stackoverflow.com/questions/46798456/handling-gccs-noexcept-type-warning
39  This warning is about ABI compatibility, not code health.
40  It is only actually needed in a couple places, but apparently GCC 7 "generates this warning if
41  and only if the first template instantiation ... involves noexcept" [stackoverflow], therefore
42  it could get triggered from seemingly random places, depending on user code.
43  No other GCC version generates this warning.
44  */
45 #if defined(__GNUC__) && __GNUC__ == 7
46 # pragma GCC diagnostic push
47 # pragma GCC diagnostic ignored "-Wnoexcept-type"
48 #endif
49 
51 
53 
54 // Apply all the extensions translators from a list
55 // Return true if one of the translators completed without raising an exception
56 // itself. Return of false indicates that if there are other translators
57 // available, they should be tried.
58 inline bool apply_exception_translators(std::forward_list<ExceptionTranslator> &translators) {
59  auto last_exception = std::current_exception();
60 
61  for (auto &translator : translators) {
62  try {
63  translator(last_exception);
64  return true;
65  } catch (...) {
66  last_exception = std::current_exception();
67  }
68  }
69  return false;
70 }
71 
72 #if defined(_MSC_VER)
73 # define PYBIND11_COMPAT_STRDUP _strdup
74 #else
75 # define PYBIND11_COMPAT_STRDUP strdup
76 #endif
77 
79 
80 class cpp_function : public function {
82 public:
83  cpp_function() = default;
84  // NOLINTNEXTLINE(google-explicit-constructor)
85  cpp_function(std::nullptr_t) {}
86 
88  template <typename Return, typename... Args, typename... Extra>
89  // NOLINTNEXTLINE(google-explicit-constructor)
90  cpp_function(Return (*f)(Args...), const Extra &...extra) {
91  initialize(f, f, extra...);
92  }
93 
95  template <typename Func,
96  typename... Extra,
98  // NOLINTNEXTLINE(google-explicit-constructor)
99  cpp_function(Func &&f, const Extra &...extra) {
100  initialize(
101  std::forward<Func>(f), (detail::function_signature_t<Func> *) nullptr, extra...);
102  }
103 
105  template <typename Return, typename Class, typename... Arg, typename... Extra>
106  // NOLINTNEXTLINE(google-explicit-constructor)
107  cpp_function(Return (Class::*f)(Arg...), const Extra &...extra) {
108  initialize(
109  [f](Class *c, Arg... args) -> Return { return (c->*f)(std::forward<Arg>(args)...); },
110  (Return(*)(Class *, Arg...)) nullptr,
111  extra...);
112  }
113 
117  template <typename Return, typename Class, typename... Arg, typename... Extra>
118  // NOLINTNEXTLINE(google-explicit-constructor)
119  cpp_function(Return (Class::*f)(Arg...) &, const Extra &...extra) {
120  initialize(
121  [f](Class *c, Arg... args) -> Return { return (c->*f)(std::forward<Arg>(args)...); },
122  (Return(*)(Class *, Arg...)) nullptr,
123  extra...);
124  }
125 
127  template <typename Return, typename Class, typename... Arg, typename... Extra>
128  // NOLINTNEXTLINE(google-explicit-constructor)
129  cpp_function(Return (Class::*f)(Arg...) const, const Extra &...extra) {
130  initialize([f](const Class *c,
131  Arg... args) -> Return { return (c->*f)(std::forward<Arg>(args)...); },
132  (Return(*)(const Class *, Arg...)) nullptr,
133  extra...);
134  }
135 
139  template <typename Return, typename Class, typename... Arg, typename... Extra>
140  // NOLINTNEXTLINE(google-explicit-constructor)
141  cpp_function(Return (Class::*f)(Arg...) const &, const Extra &...extra) {
142  initialize([f](const Class *c,
143  Arg... args) -> Return { return (c->*f)(std::forward<Arg>(args)...); },
144  (Return(*)(const Class *, Arg...)) nullptr,
145  extra...);
146  }
147 
149  object name() const { return attr("__name__"); }
150 
151 protected:
153  // `destruct(function_record, false)`: `initialize_generic` copies strings and
154  // takes care of cleaning up in case of exceptions. So pass `false` to `free_strings`.
155  void operator()(detail::function_record *rec) { destruct(rec, false); }
156  };
158  = std::unique_ptr<detail::function_record, InitializingFunctionRecordDeleter>;
159 
162  return unique_function_record(new detail::function_record());
163  }
164 
166  template <typename Func, typename Return, typename... Args, typename... Extra>
167  void initialize(Func &&f, Return (*)(Args...), const Extra &...extra) {
168  using namespace detail;
169  struct capture {
171  };
172 
173  /* Store the function including any extra state it might have (e.g. a lambda capture
174  * object) */
175  // The unique_ptr makes sure nothing is leaked in case of an exception.
176  auto unique_rec = make_function_record();
177  auto *rec = unique_rec.get();
178 
179  /* Store the capture object directly in the function record if there is enough space */
180  if (PYBIND11_SILENCE_MSVC_C4127(sizeof(capture) <= sizeof(rec->data))) {
181  /* Without these pragmas, GCC warns that there might not be
182  enough space to use the placement new operator. However, the
183  'if' statement above ensures that this is the case. */
184 #if defined(__GNUG__) && __GNUC__ >= 6 && !defined(__clang__) && !defined(__INTEL_COMPILER)
185 # pragma GCC diagnostic push
186 # pragma GCC diagnostic ignored "-Wplacement-new"
187 #endif
188  new ((capture *) &rec->data) capture{std::forward<Func>(f)};
189 #if defined(__GNUG__) && __GNUC__ >= 6 && !defined(__clang__) && !defined(__INTEL_COMPILER)
190 # pragma GCC diagnostic pop
191 #endif
192 #if defined(__GNUG__) && !PYBIND11_HAS_STD_LAUNDER && !defined(__INTEL_COMPILER)
193 # pragma GCC diagnostic push
194 # pragma GCC diagnostic ignored "-Wstrict-aliasing"
195 #endif
196  // UB without std::launder, but without breaking ABI and/or
197  // a significant refactoring it's "impossible" to solve.
199  rec->free_data = [](function_record *r) {
200  auto data = PYBIND11_STD_LAUNDER((capture *) &r->data);
201  (void) data;
202  data->~capture();
203  };
204  }
205 #if defined(__GNUG__) && !PYBIND11_HAS_STD_LAUNDER && !defined(__INTEL_COMPILER)
206 # pragma GCC diagnostic pop
207 #endif
208  } else {
209  rec->data[0] = new capture{std::forward<Func>(f)};
210  rec->free_data = [](function_record *r) { delete ((capture *) r->data[0]); };
211  }
212 
213  /* Type casters for the function arguments and return value */
214  using cast_in = argument_loader<Args...>;
215  using cast_out
217 
218  static_assert(
219  expected_num_args<Extra...>(
220  sizeof...(Args), cast_in::args_pos >= 0, cast_in::has_kwargs),
221  "The number of argument annotations does not match the number of function arguments");
222 
223  /* Dispatch code which converts function arguments and performs the actual function call */
224  rec->impl = [](function_call &call) -> handle {
225  cast_in args_converter;
226 
227  /* Try to cast the function arguments into the C++ domain */
228  if (!args_converter.load_args(call)) {
230  }
231 
232  /* Invoke call policy pre-call hook */
234 
235  /* Get a pointer to the capture object */
236  const auto *data = (sizeof(capture) <= sizeof(call.func.data) ? &call.func.data
237  : call.func.data[0]);
238  auto *cap = const_cast<capture *>(reinterpret_cast<const capture *>(data));
239 
240  /* Override policy for rvalues -- usually to enforce rvp::move on an rvalue */
241  return_value_policy policy
243 
244  /* Function scope guard -- defaults to the compile-to-nothing `void_type` */
245  using Guard = extract_guard_t<Extra...>;
246 
247  /* Perform the function call */
248  handle result
249  = cast_out::cast(std::move(args_converter).template call<Return, Guard>(cap->f),
250  policy,
251  call.parent);
252 
253  /* Invoke call policy post-call hook */
255 
256  return result;
257  };
258 
259  rec->nargs_pos = cast_in::args_pos >= 0
260  ? static_cast<std::uint16_t>(cast_in::args_pos)
261  : sizeof...(Args) - cast_in::has_kwargs; // Will get reduced more if
262  // we have a kw_only
263  rec->has_args = cast_in::args_pos >= 0;
264  rec->has_kwargs = cast_in::has_kwargs;
265 
266  /* Process any user-provided function attributes */
267  process_attributes<Extra...>::init(extra..., rec);
268 
269  {
270  constexpr bool has_kw_only_args = any_of<std::is_same<kw_only, Extra>...>::value,
271  has_pos_only_args = any_of<std::is_same<pos_only, Extra>...>::value,
272  has_arg_annotations = any_of<is_keyword<Extra>...>::value;
273  static_assert(has_arg_annotations || !has_kw_only_args,
274  "py::kw_only requires the use of argument annotations");
275  static_assert(has_arg_annotations || !has_pos_only_args,
276  "py::pos_only requires the use of argument annotations (for docstrings "
277  "and aligning the annotations to the argument)");
278 
279  static_assert(constexpr_sum(is_kw_only<Extra>::value...) <= 1,
280  "py::kw_only may be specified only once");
281  static_assert(constexpr_sum(is_pos_only<Extra>::value...) <= 1,
282  "py::pos_only may be specified only once");
283  constexpr auto kw_only_pos = constexpr_first<is_kw_only, Extra...>();
284  constexpr auto pos_only_pos = constexpr_first<is_pos_only, Extra...>();
285  static_assert(!(has_kw_only_args && has_pos_only_args) || pos_only_pos < kw_only_pos,
286  "py::pos_only must come before py::kw_only");
287  }
288 
289  /* Generate a readable signature describing the function's arguments and return
290  value types */
291  static constexpr auto signature
292  = const_name("(") + cast_in::arg_names + const_name(") -> ") + cast_out::name;
293  PYBIND11_DESCR_CONSTEXPR auto types = decltype(signature)::types();
294 
295  /* Register the function with Python from generic (non-templated) code */
296  // Pass on the ownership over the `unique_rec` to `initialize_generic`. `rec` stays valid.
297  initialize_generic(std::move(unique_rec), signature.text, types.data(), sizeof...(Args));
298 
299  /* Stash some additional information used by an important optimization in 'functional.h' */
300  using FunctionType = Return (*)(Args...);
301  constexpr bool is_function_ptr
302  = std::is_convertible<Func, FunctionType>::value && sizeof(capture) == sizeof(void *);
303  if (is_function_ptr) {
304  rec->is_stateless = true;
305  rec->data[1]
306  = const_cast<void *>(reinterpret_cast<const void *>(&typeid(FunctionType)));
307  }
308  }
309 
310  // Utility class that keeps track of all duplicated strings, and cleans them up in its
311  // destructor, unless they are released. Basically a RAII-solution to deal with exceptions
312  // along the way.
313  class strdup_guard {
314  public:
315  strdup_guard() = default;
316  strdup_guard(const strdup_guard &) = delete;
317  strdup_guard &operator=(const strdup_guard &) = delete;
318 
320  for (auto *s : strings) {
321  std::free(s);
322  }
323  }
324  char *operator()(const char *s) {
325  auto *t = PYBIND11_COMPAT_STRDUP(s);
326  strings.push_back(t);
327  return t;
328  }
329  void release() { strings.clear(); }
330 
331  private:
332  std::vector<char *> strings;
333  };
334 
337  const char *text,
338  const std::type_info *const *types,
339  size_t args) {
340  // Do NOT receive `unique_rec` by value. If this function fails to move out the unique_ptr,
341  // we do not want this to destruct the pointer. `initialize` (the caller) still relies on
342  // the pointee being alive after this call. Only move out if a `capsule` is going to keep
343  // it alive.
344  auto *rec = unique_rec.get();
345 
346  // Keep track of strdup'ed strings, and clean them up as long as the function's capsule
347  // has not taken ownership yet (when `unique_rec.release()` is called).
348  // Note: This cannot easily be fixed by a `unique_ptr` with custom deleter, because the
349  // strings are only referenced before strdup'ing. So only *after* the following block could
350  // `destruct` safely be called, but even then, `repr` could still throw in the middle of
351  // copying all strings.
352  strdup_guard guarded_strdup;
353 
354  /* Create copies of all referenced C-style strings */
355  rec->name = guarded_strdup(rec->name ? rec->name : "");
356  if (rec->doc) {
357  rec->doc = guarded_strdup(rec->doc);
358  }
359  for (auto &a : rec->args) {
360  if (a.name) {
361  a.name = guarded_strdup(a.name);
362  }
363  if (a.descr) {
364  a.descr = guarded_strdup(a.descr);
365  } else if (a.value) {
366  a.descr = guarded_strdup(repr(a.value).cast<std::string>().c_str());
367  }
368  }
369 
370  rec->is_constructor = (std::strcmp(rec->name, "__init__") == 0)
371  || (std::strcmp(rec->name, "__setstate__") == 0);
372 
373 #if defined(PYBIND11_DETAILED_ERROR_MESSAGES) && !defined(PYBIND11_DISABLE_NEW_STYLE_INIT_WARNING)
374  if (rec->is_constructor && !rec->is_new_style_constructor) {
375  const auto class_name
376  = detail::get_fully_qualified_tp_name((PyTypeObject *) rec->scope.ptr());
377  const auto func_name = std::string(rec->name);
378  PyErr_WarnEx(PyExc_FutureWarning,
379  ("pybind11-bound class '" + class_name
380  + "' is using an old-style "
381  "placement-new '"
382  + func_name
383  + "' which has been deprecated. See "
384  "the upgrade guide in pybind11's docs. This message is only visible "
385  "when compiled in debug mode.")
386  .c_str(),
387  0);
388  }
389 #endif
390 
391  /* Generate a proper function signature */
392  std::string signature;
393  size_t type_index = 0, arg_index = 0;
394  bool is_starred = false;
395  for (const auto *pc = text; *pc != '\0'; ++pc) {
396  const auto c = *pc;
397 
398  if (c == '{') {
399  // Write arg name for everything except *args and **kwargs.
400  is_starred = *(pc + 1) == '*';
401  if (is_starred) {
402  continue;
403  }
404  // Separator for keyword-only arguments, placed before the kw
405  // arguments start (unless we are already putting an *args)
406  if (!rec->has_args && arg_index == rec->nargs_pos) {
407  signature += "*, ";
408  }
409  if (arg_index < rec->args.size() && rec->args[arg_index].name) {
410  signature += rec->args[arg_index].name;
411  } else if (arg_index == 0 && rec->is_method) {
412  signature += "self";
413  } else {
414  signature += "arg" + std::to_string(arg_index - (rec->is_method ? 1 : 0));
415  }
416  signature += ": ";
417  } else if (c == '}') {
418  // Write default value if available.
419  if (!is_starred && arg_index < rec->args.size() && rec->args[arg_index].descr) {
420  signature += " = ";
421  signature += rec->args[arg_index].descr;
422  }
423  // Separator for positional-only arguments (placed after the
424  // argument, rather than before like *
425  if (rec->nargs_pos_only > 0 && (arg_index + 1) == rec->nargs_pos_only) {
426  signature += ", /";
427  }
428  if (!is_starred) {
429  arg_index++;
430  }
431  } else if (c == '%') {
432  const std::type_info *t = types[type_index++];
433  if (!t) {
434  pybind11_fail("Internal error while parsing type signature (1)");
435  }
436  if (auto *tinfo = detail::get_type_info(*t)) {
437  handle th((PyObject *) tinfo->type);
438  signature += th.attr("__module__").cast<std::string>() + "."
439  + th.attr("__qualname__").cast<std::string>();
440  } else if (rec->is_new_style_constructor && arg_index == 0) {
441  // A new-style `__init__` takes `self` as `value_and_holder`.
442  // Rewrite it to the proper class type.
443  signature += rec->scope.attr("__module__").cast<std::string>() + "."
444  + rec->scope.attr("__qualname__").cast<std::string>();
445  } else {
446  std::string tname(t->name());
447  detail::clean_type_id(tname);
448  signature += tname;
449  }
450  } else {
451  signature += c;
452  }
453  }
454 
455  if (arg_index != args - rec->has_args - rec->has_kwargs || types[type_index] != nullptr) {
456  pybind11_fail("Internal error while parsing type signature (2)");
457  }
458 
459  rec->signature = guarded_strdup(signature.c_str());
460  rec->args.shrink_to_fit();
461  rec->nargs = (std::uint16_t) args;
462 
463  if (rec->sibling && PYBIND11_INSTANCE_METHOD_CHECK(rec->sibling.ptr())) {
464  rec->sibling = PYBIND11_INSTANCE_METHOD_GET_FUNCTION(rec->sibling.ptr());
465  }
466 
467  detail::function_record *chain = nullptr, *chain_start = rec;
468  if (rec->sibling) {
469  if (PyCFunction_Check(rec->sibling.ptr())) {
470  auto *self = PyCFunction_GET_SELF(rec->sibling.ptr());
471  capsule rec_capsule = isinstance<capsule>(self) ? reinterpret_borrow<capsule>(self)
472  : capsule(self);
473  chain = (detail::function_record *) rec_capsule;
474  /* Never append a method to an overload chain of a parent class;
475  instead, hide the parent's overloads in this case */
476  if (!chain->scope.is(rec->scope)) {
477  chain = nullptr;
478  }
479  }
480  // Don't trigger for things like the default __init__, which are wrapper_descriptors
481  // that we are intentionally replacing
482  else if (!rec->sibling.is_none() && rec->name[0] != '_') {
483  pybind11_fail("Cannot overload existing non-function object \""
484  + std::string(rec->name) + "\" with a function of the same name");
485  }
486  }
487 
488  if (!chain) {
489  /* No existing overload was found, create a new function object */
490  rec->def = new PyMethodDef();
491  std::memset(rec->def, 0, sizeof(PyMethodDef));
492  rec->def->ml_name = rec->name;
493  rec->def->ml_meth
494  = reinterpret_cast<PyCFunction>(reinterpret_cast<void (*)()>(dispatcher));
495  rec->def->ml_flags = METH_VARARGS | METH_KEYWORDS;
496 
497  capsule rec_capsule(unique_rec.release(),
498  [](void *ptr) { destruct((detail::function_record *) ptr); });
499  guarded_strdup.release();
500 
501  object scope_module;
502  if (rec->scope) {
503  if (hasattr(rec->scope, "__module__")) {
504  scope_module = rec->scope.attr("__module__");
505  } else if (hasattr(rec->scope, "__name__")) {
506  scope_module = rec->scope.attr("__name__");
507  }
508  }
509 
510  m_ptr = PyCFunction_NewEx(rec->def, rec_capsule.ptr(), scope_module.ptr());
511  if (!m_ptr) {
512  pybind11_fail("cpp_function::cpp_function(): Could not allocate function object");
513  }
514  } else {
515  /* Append at the beginning or end of the overload chain */
516  m_ptr = rec->sibling.ptr();
517  inc_ref();
518  if (chain->is_method != rec->is_method) {
520  "overloading a method with both static and instance methods is not supported; "
522  "#define PYBIND11_DETAILED_ERROR_MESSAGES or compile in debug mode for more "
523  "details"
524 #else
525  "error while attempting to bind "
526  + std::string(rec->is_method ? "instance" : "static") + " method "
527  + std::string(pybind11::str(rec->scope.attr("__name__"))) + "."
528  + std::string(rec->name) + signature
529 #endif
530  );
531  }
532 
533  if (rec->prepend) {
534  // Beginning of chain; we need to replace the capsule's current head-of-the-chain
535  // pointer with this one, then make this one point to the previous head of the
536  // chain.
537  chain_start = rec;
538  rec->next = chain;
539  auto rec_capsule
540  = reinterpret_borrow<capsule>(((PyCFunctionObject *) m_ptr)->m_self);
541  rec_capsule.set_pointer(unique_rec.release());
542  guarded_strdup.release();
543  } else {
544  // Or end of chain (normal behavior)
545  chain_start = chain;
546  while (chain->next) {
547  chain = chain->next;
548  }
549  chain->next = unique_rec.release();
550  guarded_strdup.release();
551  }
552  }
553 
554  std::string signatures;
555  int index = 0;
556  /* Create a nice pydoc rec including all signatures and
557  docstrings of the functions in the overload chain */
558  if (chain && options::show_function_signatures()) {
559  // First a generic signature
560  signatures += rec->name;
561  signatures += "(*args, **kwargs)\n";
562  signatures += "Overloaded function.\n\n";
563  }
564  // Then specific overload signatures
565  bool first_user_def = true;
566  for (auto *it = chain_start; it != nullptr; it = it->next) {
568  if (index > 0) {
569  signatures += '\n';
570  }
571  if (chain) {
572  signatures += std::to_string(++index) + ". ";
573  }
574  signatures += rec->name;
575  signatures += it->signature;
576  signatures += '\n';
577  }
578  if (it->doc && it->doc[0] != '\0' && options::show_user_defined_docstrings()) {
579  // If we're appending another docstring, and aren't printing function signatures,
580  // we need to append a newline first:
582  if (first_user_def) {
583  first_user_def = false;
584  } else {
585  signatures += '\n';
586  }
587  }
589  signatures += '\n';
590  }
591  signatures += it->doc;
593  signatures += '\n';
594  }
595  }
596  }
597 
598  /* Install docstring */
599  auto *func = (PyCFunctionObject *) m_ptr;
600  std::free(const_cast<char *>(func->m_ml->ml_doc));
601  // Install docstring if it's non-empty (when at least one option is enabled)
602  func->m_ml->ml_doc
603  = signatures.empty() ? nullptr : PYBIND11_COMPAT_STRDUP(signatures.c_str());
604 
605  if (rec->is_method) {
606  m_ptr = PYBIND11_INSTANCE_METHOD_NEW(m_ptr, rec->scope.ptr());
607  if (!m_ptr) {
609  "cpp_function::cpp_function(): Could not allocate instance method object");
610  }
611  Py_DECREF(func);
612  }
613  }
614 
616  static void destruct(detail::function_record *rec, bool free_strings = true) {
617 // If on Python 3.9, check the interpreter "MICRO" (patch) version.
618 // If this is running on 3.9.0, we have to work around a bug.
619 #if !defined(PYPY_VERSION) && PY_MAJOR_VERSION == 3 && PY_MINOR_VERSION == 9
620  static bool is_zero = Py_GetVersion()[4] == '0';
621 #endif
622 
623  while (rec) {
624  detail::function_record *next = rec->next;
625  if (rec->free_data) {
626  rec->free_data(rec);
627  }
628  // During initialization, these strings might not have been copied yet,
629  // so they cannot be freed. Once the function has been created, they can.
630  // Check `make_function_record` for more details.
631  if (free_strings) {
632  std::free((char *) rec->name);
633  std::free((char *) rec->doc);
634  std::free((char *) rec->signature);
635  for (auto &arg : rec->args) {
636  std::free(const_cast<char *>(arg.name));
637  std::free(const_cast<char *>(arg.descr));
638  }
639  }
640  for (auto &arg : rec->args) {
641  arg.value.dec_ref();
642  }
643  if (rec->def) {
644  std::free(const_cast<char *>(rec->def->ml_doc));
645 // Python 3.9.0 decref's these in the wrong order; rec->def
646 // If loaded on 3.9.0, let these leak (use Python 3.9.1 at runtime to fix)
647 // See https://github.com/python/cpython/pull/22670
648 #if !defined(PYPY_VERSION) && PY_MAJOR_VERSION == 3 && PY_MINOR_VERSION == 9
649  if (!is_zero) {
650  delete rec->def;
651  }
652 #else
653  delete rec->def;
654 #endif
655  }
656  delete rec;
657  rec = next;
658  }
659  }
660 
662  static PyObject *dispatcher(PyObject *self, PyObject *args_in, PyObject *kwargs_in) {
663  using namespace detail;
664 
665  /* Iterator over the list of potentially admissible overloads */
666  const function_record *overloads = (function_record *) PyCapsule_GetPointer(self, nullptr),
667  *it = overloads;
668 
669  /* Need to know how many arguments + keyword arguments there are to pick the right
670  overload */
671  const auto n_args_in = (size_t) PyTuple_GET_SIZE(args_in);
672 
673  handle parent = n_args_in > 0 ? PyTuple_GET_ITEM(args_in, 0) : nullptr,
675 
676  auto self_value_and_holder = value_and_holder();
677  if (overloads->is_constructor) {
678  if (!parent
679  || !PyObject_TypeCheck(parent.ptr(), (PyTypeObject *) overloads->scope.ptr())) {
680  PyErr_SetString(
681  PyExc_TypeError,
682  "__init__(self, ...) called with invalid or missing `self` argument");
683  return nullptr;
684  }
685 
686  auto *const tinfo = get_type_info((PyTypeObject *) overloads->scope.ptr());
687  auto *const pi = reinterpret_cast<instance *>(parent.ptr());
688  self_value_and_holder = pi->get_value_and_holder(tinfo, true);
689 
690  // If this value is already registered it must mean __init__ is invoked multiple times;
691  // we really can't support that in C++, so just ignore the second __init__.
692  if (self_value_and_holder.instance_registered()) {
693  return none().release().ptr();
694  }
695  }
696 
697  try {
698  // We do this in two passes: in the first pass, we load arguments with `convert=false`;
699  // in the second, we allow conversion (except for arguments with an explicit
700  // py::arg().noconvert()). This lets us prefer calls without conversion, with
701  // conversion as a fallback.
702  std::vector<function_call> second_pass;
703 
704  // However, if there are no overloads, we can just skip the no-convert pass entirely
705  const bool overloaded = it != nullptr && it->next != nullptr;
706 
707  for (; it != nullptr; it = it->next) {
708 
709  /* For each overload:
710  1. Copy all positional arguments we were given, also checking to make sure that
711  named positional arguments weren't *also* specified via kwarg.
712  2. If we weren't given enough, try to make up the omitted ones by checking
713  whether they were provided by a kwarg matching the `py::arg("name")` name. If
714  so, use it (and remove it from kwargs); if not, see if the function binding
715  provided a default that we can use.
716  3. Ensure that either all keyword arguments were "consumed", or that the
717  function takes a kwargs argument to accept unconsumed kwargs.
718  4. Any positional arguments still left get put into a tuple (for args), and any
719  leftover kwargs get put into a dict.
720  5. Pack everything into a vector; if we have py::args or py::kwargs, they are an
721  extra tuple or dict at the end of the positional arguments.
722  6. Call the function call dispatcher (function_record::impl)
723 
724  If one of these fail, move on to the next overload and keep trying until we get
725  a result other than PYBIND11_TRY_NEXT_OVERLOAD.
726  */
727 
728  const function_record &func = *it;
729  size_t num_args = func.nargs; // Number of positional arguments that we need
730  if (func.has_args) {
731  --num_args; // (but don't count py::args
732  }
733  if (func.has_kwargs) {
734  --num_args; // or py::kwargs)
735  }
736  size_t pos_args = func.nargs_pos;
737 
738  if (!func.has_args && n_args_in > pos_args) {
739  continue; // Too many positional arguments for this overload
740  }
741 
742  if (n_args_in < pos_args && func.args.size() < pos_args) {
743  continue; // Not enough positional arguments given, and not enough defaults to
744  // fill in the blanks
745  }
746 
747  function_call call(func, parent);
748 
749  // Protect std::min with parentheses
750  size_t args_to_copy = (std::min)(pos_args, n_args_in);
751  size_t args_copied = 0;
752 
753  // 0. Inject new-style `self` argument
754  if (func.is_new_style_constructor) {
755  // The `value` may have been preallocated by an old-style `__init__`
756  // if it was a preceding candidate for overload resolution.
757  if (self_value_and_holder) {
758  self_value_and_holder.type->dealloc(self_value_and_holder);
759  }
760 
761  call.init_self = PyTuple_GET_ITEM(args_in, 0);
762  call.args.emplace_back(reinterpret_cast<PyObject *>(&self_value_and_holder));
763  call.args_convert.push_back(false);
764  ++args_copied;
765  }
766 
767  // 1. Copy any position arguments given.
768  bool bad_arg = false;
769  for (; args_copied < args_to_copy; ++args_copied) {
770  const argument_record *arg_rec
771  = args_copied < func.args.size() ? &func.args[args_copied] : nullptr;
772  if (kwargs_in && arg_rec && arg_rec->name
773  && dict_getitemstring(kwargs_in, arg_rec->name)) {
774  bad_arg = true;
775  break;
776  }
777 
778  handle arg(PyTuple_GET_ITEM(args_in, args_copied));
779  if (arg_rec && !arg_rec->none && arg.is_none()) {
780  bad_arg = true;
781  break;
782  }
783  call.args.push_back(arg);
784  call.args_convert.push_back(arg_rec ? arg_rec->convert : true);
785  }
786  if (bad_arg) {
787  continue; // Maybe it was meant for another overload (issue #688)
788  }
789 
790  // Keep track of how many position args we copied out in case we need to come back
791  // to copy the rest into a py::args argument.
792  size_t positional_args_copied = args_copied;
793 
794  // We'll need to copy this if we steal some kwargs for defaults
795  dict kwargs = reinterpret_borrow<dict>(kwargs_in);
796 
797  // 1.5. Fill in any missing pos_only args from defaults if they exist
798  if (args_copied < func.nargs_pos_only) {
799  for (; args_copied < func.nargs_pos_only; ++args_copied) {
800  const auto &arg_rec = func.args[args_copied];
801  handle value;
802 
803  if (arg_rec.value) {
804  value = arg_rec.value;
805  }
806  if (value) {
807  call.args.push_back(value);
808  call.args_convert.push_back(arg_rec.convert);
809  } else {
810  break;
811  }
812  }
813 
814  if (args_copied < func.nargs_pos_only) {
815  continue; // Not enough defaults to fill the positional arguments
816  }
817  }
818 
819  // 2. Check kwargs and, failing that, defaults that may help complete the list
820  if (args_copied < num_args) {
821  bool copied_kwargs = false;
822 
823  for (; args_copied < num_args; ++args_copied) {
824  const auto &arg_rec = func.args[args_copied];
825 
826  handle value;
827  if (kwargs_in && arg_rec.name) {
828  value = dict_getitemstring(kwargs.ptr(), arg_rec.name);
829  }
830 
831  if (value) {
832  // Consume a kwargs value
833  if (!copied_kwargs) {
834  kwargs = reinterpret_steal<dict>(PyDict_Copy(kwargs.ptr()));
835  copied_kwargs = true;
836  }
837  if (PyDict_DelItemString(kwargs.ptr(), arg_rec.name) == -1) {
838  throw error_already_set();
839  }
840  } else if (arg_rec.value) {
841  value = arg_rec.value;
842  }
843 
844  if (!arg_rec.none && value.is_none()) {
845  break;
846  }
847 
848  if (value) {
849  // If we're at the py::args index then first insert a stub for it to be
850  // replaced later
851  if (func.has_args && call.args.size() == func.nargs_pos) {
852  call.args.push_back(none());
853  }
854 
855  call.args.push_back(value);
856  call.args_convert.push_back(arg_rec.convert);
857  } else {
858  break;
859  }
860  }
861 
862  if (args_copied < num_args) {
863  continue; // Not enough arguments, defaults, or kwargs to fill the
864  // positional arguments
865  }
866  }
867 
868  // 3. Check everything was consumed (unless we have a kwargs arg)
869  if (kwargs && !kwargs.empty() && !func.has_kwargs) {
870  continue; // Unconsumed kwargs, but no py::kwargs argument to accept them
871  }
872 
873  // 4a. If we have a py::args argument, create a new tuple with leftovers
874  if (func.has_args) {
875  tuple extra_args;
876  if (args_to_copy == 0) {
877  // We didn't copy out any position arguments from the args_in tuple, so we
878  // can reuse it directly without copying:
879  extra_args = reinterpret_borrow<tuple>(args_in);
880  } else if (positional_args_copied >= n_args_in) {
881  extra_args = tuple(0);
882  } else {
883  size_t args_size = n_args_in - positional_args_copied;
884  extra_args = tuple(args_size);
885  for (size_t i = 0; i < args_size; ++i) {
886  extra_args[i] = PyTuple_GET_ITEM(args_in, positional_args_copied + i);
887  }
888  }
889  if (call.args.size() <= func.nargs_pos) {
890  call.args.push_back(extra_args);
891  } else {
892  call.args[func.nargs_pos] = extra_args;
893  }
894  call.args_convert.push_back(false);
895  call.args_ref = std::move(extra_args);
896  }
897 
898  // 4b. If we have a py::kwargs, pass on any remaining kwargs
899  if (func.has_kwargs) {
900  if (!kwargs.ptr()) {
901  kwargs = dict(); // If we didn't get one, send an empty one
902  }
903  call.args.push_back(kwargs);
904  call.args_convert.push_back(false);
905  call.kwargs_ref = std::move(kwargs);
906  }
907 
908 // 5. Put everything in a vector. Not technically step 5, we've been building it
909 // in `call.args` all along.
910 #if defined(PYBIND11_DETAILED_ERROR_MESSAGES)
911  if (call.args.size() != func.nargs || call.args_convert.size() != func.nargs) {
912  pybind11_fail("Internal error: function call dispatcher inserted wrong number "
913  "of arguments!");
914  }
915 #endif
916 
917  std::vector<bool> second_pass_convert;
918  if (overloaded) {
919  // We're in the first no-convert pass, so swap out the conversion flags for a
920  // set of all-false flags. If the call fails, we'll swap the flags back in for
921  // the conversion-allowed call below.
922  second_pass_convert.resize(func.nargs, false);
923  call.args_convert.swap(second_pass_convert);
924  }
925 
926  // 6. Call the function.
927  try {
928  loader_life_support guard{};
929  result = func.impl(call);
930  } catch (reference_cast_error &) {
932  }
933 
934  if (result.ptr() != PYBIND11_TRY_NEXT_OVERLOAD) {
935  break;
936  }
937 
938  if (overloaded) {
939  // The (overloaded) call failed; if the call has at least one argument that
940  // permits conversion (i.e. it hasn't been explicitly specified `.noconvert()`)
941  // then add this call to the list of second pass overloads to try.
942  for (size_t i = func.is_method ? 1 : 0; i < pos_args; i++) {
943  if (second_pass_convert[i]) {
944  // Found one: swap the converting flags back in and store the call for
945  // the second pass.
946  call.args_convert.swap(second_pass_convert);
947  second_pass.push_back(std::move(call));
948  break;
949  }
950  }
951  }
952  }
953 
954  if (overloaded && !second_pass.empty() && result.ptr() == PYBIND11_TRY_NEXT_OVERLOAD) {
955  // The no-conversion pass finished without success, try again with conversion
956  // allowed
957  for (auto &call : second_pass) {
958  try {
959  loader_life_support guard{};
960  result = call.func.impl(call);
961  } catch (reference_cast_error &) {
963  }
964 
965  if (result.ptr() != PYBIND11_TRY_NEXT_OVERLOAD) {
966  // The error reporting logic below expects 'it' to be valid, as it would be
967  // if we'd encountered this failure in the first-pass loop.
968  if (!result) {
969  it = &call.func;
970  }
971  break;
972  }
973  }
974  }
975  } catch (error_already_set &e) {
976  e.restore();
977  return nullptr;
978 #ifdef __GLIBCXX__
979  } catch (abi::__forced_unwind &) {
980  throw;
981 #endif
982  } catch (...) {
983  /* When an exception is caught, give each registered exception
984  translator a chance to translate it to a Python exception. First
985  all module-local translators will be tried in reverse order of
986  registration. If none of the module-locale translators handle
987  the exception (or there are no module-locale translators) then
988  the global translators will be tried, also in reverse order of
989  registration.
990 
991  A translator may choose to do one of the following:
992 
993  - catch the exception and call PyErr_SetString or PyErr_SetObject
994  to set a standard (or custom) Python exception, or
995  - do nothing and let the exception fall through to the next translator, or
996  - delegate translation to the next translator by throwing a new type of exception.
997  */
998 
999  auto &local_exception_translators
1001  if (detail::apply_exception_translators(local_exception_translators)) {
1002  return nullptr;
1003  }
1004  auto &exception_translators = get_internals().registered_exception_translators;
1005  if (detail::apply_exception_translators(exception_translators)) {
1006  return nullptr;
1007  }
1008 
1009  PyErr_SetString(PyExc_SystemError,
1010  "Exception escaped from default exception translator!");
1011  return nullptr;
1012  }
1013 
1014  auto append_note_if_missing_header_is_suspected = [](std::string &msg) {
1015  if (msg.find("std::") != std::string::npos) {
1016  msg += "\n\n"
1017  "Did you forget to `#include <pybind11/stl.h>`? Or <pybind11/complex.h>,\n"
1018  "<pybind11/functional.h>, <pybind11/chrono.h>, etc. Some automatic\n"
1019  "conversions are optional and require extra headers to be included\n"
1020  "when compiling your pybind11 module.";
1021  }
1022  };
1023 
1024  if (result.ptr() == PYBIND11_TRY_NEXT_OVERLOAD) {
1025  if (overloads->is_operator) {
1026  return handle(Py_NotImplemented).inc_ref().ptr();
1027  }
1028 
1029  std::string msg = std::string(overloads->name) + "(): incompatible "
1030  + std::string(overloads->is_constructor ? "constructor" : "function")
1031  + " arguments. The following argument types are supported:\n";
1032 
1033  int ctr = 0;
1034  for (const function_record *it2 = overloads; it2 != nullptr; it2 = it2->next) {
1035  msg += " " + std::to_string(++ctr) + ". ";
1036 
1037  bool wrote_sig = false;
1038  if (overloads->is_constructor) {
1039  // For a constructor, rewrite `(self: Object, arg0, ...) -> NoneType` as
1040  // `Object(arg0, ...)`
1041  std::string sig = it2->signature;
1042  size_t start = sig.find('(') + 7; // skip "(self: "
1043  if (start < sig.size()) {
1044  // End at the , for the next argument
1045  size_t end = sig.find(", "), next = end + 2;
1046  size_t ret = sig.rfind(" -> ");
1047  // Or the ), if there is no comma:
1048  if (end >= sig.size()) {
1049  next = end = sig.find(')');
1050  }
1051  if (start < end && next < sig.size()) {
1052  msg.append(sig, start, end - start);
1053  msg += '(';
1054  msg.append(sig, next, ret - next);
1055  wrote_sig = true;
1056  }
1057  }
1058  }
1059  if (!wrote_sig) {
1060  msg += it2->signature;
1061  }
1062 
1063  msg += '\n';
1064  }
1065  msg += "\nInvoked with: ";
1066  auto args_ = reinterpret_borrow<tuple>(args_in);
1067  bool some_args = false;
1068  for (size_t ti = overloads->is_constructor ? 1 : 0; ti < args_.size(); ++ti) {
1069  if (!some_args) {
1070  some_args = true;
1071  } else {
1072  msg += ", ";
1073  }
1074  try {
1075  msg += pybind11::repr(args_[ti]);
1076  } catch (const error_already_set &) {
1077  msg += "<repr raised Error>";
1078  }
1079  }
1080  if (kwargs_in) {
1081  auto kwargs = reinterpret_borrow<dict>(kwargs_in);
1082  if (!kwargs.empty()) {
1083  if (some_args) {
1084  msg += "; ";
1085  }
1086  msg += "kwargs: ";
1087  bool first = true;
1088  for (auto kwarg : kwargs) {
1089  if (first) {
1090  first = false;
1091  } else {
1092  msg += ", ";
1093  }
1094  msg += pybind11::str("{}=").format(kwarg.first);
1095  try {
1096  msg += pybind11::repr(kwarg.second);
1097  } catch (const error_already_set &) {
1098  msg += "<repr raised Error>";
1099  }
1100  }
1101  }
1102  }
1103 
1104  append_note_if_missing_header_is_suspected(msg);
1105  // Attach additional error info to the exception if supported
1106  if (PyErr_Occurred()) {
1107  // #HelpAppreciated: unit test coverage for this branch.
1108  raise_from(PyExc_TypeError, msg.c_str());
1109  return nullptr;
1110  }
1111  PyErr_SetString(PyExc_TypeError, msg.c_str());
1112  return nullptr;
1113  }
1114  if (!result) {
1115  std::string msg = "Unable to convert function return value to a "
1116  "Python type! The signature was\n\t";
1117  msg += it->signature;
1118  append_note_if_missing_header_is_suspected(msg);
1119  // Attach additional error info to the exception if supported
1120  if (PyErr_Occurred()) {
1121  raise_from(PyExc_TypeError, msg.c_str());
1122  return nullptr;
1123  }
1124  PyErr_SetString(PyExc_TypeError, msg.c_str());
1125  return nullptr;
1126  }
1127  if (overloads->is_constructor && !self_value_and_holder.holder_constructed()) {
1128  auto *pi = reinterpret_cast<instance *>(parent.ptr());
1129  self_value_and_holder.type->init_instance(pi, nullptr);
1130  }
1131  return result.ptr();
1132  }
1133 };
1134 
1136 class module_ : public object {
1137 public:
1138  PYBIND11_OBJECT_DEFAULT(module_, object, PyModule_Check)
1139 
1140 
1141  PYBIND11_DEPRECATED("Use PYBIND11_MODULE or module_::create_extension_module instead")
1142  explicit module_(const char *name, const char *doc = nullptr) {
1143  *this = create_extension_module(name, doc, new PyModuleDef());
1144  }
1145 
1151  template <typename Func, typename... Extra>
1152  module_ &def(const char *name_, Func &&f, const Extra &...extra) {
1153  cpp_function func(std::forward<Func>(f),
1154  name(name_),
1155  scope(*this),
1156  sibling(getattr(*this, name_, none())),
1157  extra...);
1158  // NB: allow overwriting here because cpp_function sets up a chain with the intention of
1159  // overwriting (and has already checked internally that it isn't overwriting
1160  // non-functions).
1161  add_object(name_, func, true /* overwrite */);
1162  return *this;
1163  }
1164 
1175  module_ def_submodule(const char *name, const char *doc = nullptr) {
1176  const char *this_name = PyModule_GetName(m_ptr);
1177  if (this_name == nullptr) {
1178  throw error_already_set();
1179  }
1180  std::string full_name = std::string(this_name) + '.' + name;
1181  handle submodule = PyImport_AddModule(full_name.c_str());
1182  if (!submodule) {
1183  throw error_already_set();
1184  }
1185  auto result = reinterpret_borrow<module_>(submodule);
1187  result.attr("__doc__") = pybind11::str(doc);
1188  }
1189  attr(name) = result;
1190  return result;
1191  }
1192 
1194  static module_ import(const char *name) {
1195  PyObject *obj = PyImport_ImportModule(name);
1196  if (!obj) {
1197  throw error_already_set();
1198  }
1199  return reinterpret_steal<module_>(obj);
1200  }
1201 
1203  void reload() {
1204  PyObject *obj = PyImport_ReloadModule(ptr());
1205  if (!obj) {
1206  throw error_already_set();
1207  }
1208  *this = reinterpret_steal<module_>(obj);
1209  }
1210 
1218  PYBIND11_NOINLINE void add_object(const char *name, handle obj, bool overwrite = false) {
1219  if (!overwrite && hasattr(*this, name)) {
1220  pybind11_fail(
1221  "Error during initialization: multiple incompatible definitions with name \""
1222  + std::string(name) + "\"");
1223  }
1224 
1225  PyModule_AddObject(ptr(), name, obj.inc_ref().ptr() /* steals a reference */);
1226  }
1227 
1228  using module_def = PyModuleDef; // TODO: Can this be removed (it was needed only for Python 2)?
1229 
1235  static module_ create_extension_module(const char *name, const char *doc, module_def *def) {
1236  // module_def is PyModuleDef
1237  // Placement new (not an allocation).
1238  def = new (def)
1239  PyModuleDef{/* m_base */ PyModuleDef_HEAD_INIT,
1240  /* m_name */ name,
1241  /* m_doc */ options::show_user_defined_docstrings() ? doc : nullptr,
1242  /* m_size */ -1,
1243  /* m_methods */ nullptr,
1244  /* m_slots */ nullptr,
1245  /* m_traverse */ nullptr,
1246  /* m_clear */ nullptr,
1247  /* m_free */ nullptr};
1248  auto *m = PyModule_Create(def);
1249  if (m == nullptr) {
1250  if (PyErr_Occurred()) {
1251  throw error_already_set();
1252  }
1253  pybind11_fail("Internal error in module_::create_extension_module()");
1254  }
1255  // TODO: Should be reinterpret_steal for Python 3, but Python also steals it again when
1256  // returned from PyInit_...
1257  // For Python 2, reinterpret_borrow was correct.
1258  return reinterpret_borrow<module_>(m);
1259  }
1260 };
1261 
1262 // When inside a namespace (or anywhere as long as it's not the first item on a line),
1263 // C++20 allows "module" to be used. This is provided for backward compatibility, and for
1264 // simplicity, if someone wants to use py::module for example, that is perfectly safe.
1265 using module = module_;
1266 
1270 inline dict globals() {
1271  PyObject *p = PyEval_GetGlobals();
1272  return reinterpret_borrow<dict>(p ? p : module_::import("__main__").attr("__dict__").ptr());
1273 }
1274 
1275 template <typename... Args, typename = detail::enable_if_t<args_are_all_keyword_or_ds<Args...>()>>
1276 PYBIND11_DEPRECATED("make_simple_namespace should be replaced with "
1277  "py::module_::import(\"types\").attr(\"SimpleNamespace\") ")
1278 object make_simple_namespace(Args &&...args_) {
1279  return module_::import("types").attr("SimpleNamespace")(std::forward<Args>(args_)...);
1280 }
1281 
1284 class generic_type : public object {
1285 public:
1286  PYBIND11_OBJECT_DEFAULT(generic_type, object, PyType_Check)
1287 protected:
1288  void initialize(const type_record &rec) {
1289  if (rec.scope && hasattr(rec.scope, "__dict__")
1290  && rec.scope.attr("__dict__").contains(rec.name)) {
1291  pybind11_fail("generic_type: cannot initialize type \"" + std::string(rec.name)
1292  + "\": an object with that name is already defined");
1293  }
1294 
1296  != nullptr) {
1297  pybind11_fail("generic_type: type \"" + std::string(rec.name)
1298  + "\" is already registered!");
1299  }
1300 
1301  m_ptr = make_new_python_type(rec);
1302 
1303  /* Register supplemental type information in C++ dict */
1304  auto *tinfo = new detail::type_info();
1305  tinfo->type = (PyTypeObject *) m_ptr;
1306  tinfo->cpptype = rec.type;
1307  tinfo->type_size = rec.type_size;
1308  tinfo->type_align = rec.type_align;
1309  tinfo->operator_new = rec.operator_new;
1310  tinfo->holder_size_in_ptrs = size_in_ptrs(rec.holder_size);
1311  tinfo->init_instance = rec.init_instance;
1312  tinfo->dealloc = rec.dealloc;
1313  tinfo->simple_type = true;
1314  tinfo->simple_ancestors = true;
1315  tinfo->default_holder = rec.default_holder;
1316  tinfo->module_local = rec.module_local;
1317 
1318  auto &internals = get_internals();
1319  auto tindex = std::type_index(*rec.type);
1320  tinfo->direct_conversions = &internals.direct_conversions[tindex];
1321  if (rec.module_local) {
1322  get_local_internals().registered_types_cpp[tindex] = tinfo;
1323  } else {
1324  internals.registered_types_cpp[tindex] = tinfo;
1325  }
1326  internals.registered_types_py[(PyTypeObject *) m_ptr] = {tinfo};
1327 
1328  if (rec.bases.size() > 1 || rec.multiple_inheritance) {
1329  mark_parents_nonsimple(tinfo->type);
1330  tinfo->simple_ancestors = false;
1331  } else if (rec.bases.size() == 1) {
1332  auto *parent_tinfo = get_type_info((PyTypeObject *) rec.bases[0].ptr());
1333  assert(parent_tinfo != nullptr);
1334  bool parent_simple_ancestors = parent_tinfo->simple_ancestors;
1335  tinfo->simple_ancestors = parent_simple_ancestors;
1336  // The parent can no longer be a simple type if it has MI and has a child
1337  parent_tinfo->simple_type = parent_tinfo->simple_type && parent_simple_ancestors;
1338  }
1339 
1340  if (rec.module_local) {
1341  // Stash the local typeinfo and loader so that external modules can access it.
1342  tinfo->module_local_load = &type_caster_generic::local_load;
1344  }
1345  }
1346 
1348  void mark_parents_nonsimple(PyTypeObject *value) {
1349  auto t = reinterpret_borrow<tuple>(value->tp_bases);
1350  for (handle h : t) {
1351  auto *tinfo2 = get_type_info((PyTypeObject *) h.ptr());
1352  if (tinfo2) {
1353  tinfo2->simple_type = false;
1354  }
1355  mark_parents_nonsimple((PyTypeObject *) h.ptr());
1356  }
1357  }
1358 
1359  void install_buffer_funcs(buffer_info *(*get_buffer)(PyObject *, void *),
1360  void *get_buffer_data) {
1361  auto *type = (PyHeapTypeObject *) m_ptr;
1362  auto *tinfo = detail::get_type_info(&type->ht_type);
1363 
1364  if (!type->ht_type.tp_as_buffer) {
1365  pybind11_fail("To be able to register buffer protocol support for the type '"
1366  + get_fully_qualified_tp_name(tinfo->type)
1367  + "' the associated class<>(..) invocation must "
1368  "include the pybind11::buffer_protocol() annotation!");
1369  }
1370 
1371  tinfo->get_buffer = get_buffer;
1372  tinfo->get_buffer_data = get_buffer_data;
1373  }
1374 
1375  // rec_func must be set for either fget or fset.
1376  void def_property_static_impl(const char *name,
1377  handle fget,
1378  handle fset,
1379  detail::function_record *rec_func) {
1380  const auto is_static = (rec_func != nullptr) && !(rec_func->is_method && rec_func->scope);
1381  const auto has_doc = (rec_func != nullptr) && (rec_func->doc != nullptr)
1382  && pybind11::options::show_user_defined_docstrings();
1383  auto property = handle(
1384  (PyObject *) (is_static ? get_internals().static_property_type : &PyProperty_Type));
1385  attr(name) = property(fget.ptr() ? fget : none(),
1386  fset.ptr() ? fset : none(),
1387  /*deleter*/ none(),
1388  pybind11::str(has_doc ? rec_func->doc : ""));
1389  }
1390 };
1391 
1393 template <typename T,
1394  typename = void_t<decltype(static_cast<void *(*) (size_t)>(T::operator new))>>
1396  r->operator_new = &T::operator new;
1397 }
1398 
1399 template <typename>
1400 void set_operator_new(...) {}
1401 
1402 template <typename T, typename SFINAE = void>
1403 struct has_operator_delete : std::false_type {};
1404 template <typename T>
1405 struct has_operator_delete<T, void_t<decltype(static_cast<void (*)(void *)>(T::operator delete))>>
1406  : std::true_type {};
1407 template <typename T, typename SFINAE = void>
1408 struct has_operator_delete_size : std::false_type {};
1409 template <typename T>
1411  T,
1412  void_t<decltype(static_cast<void (*)(void *, size_t)>(T::operator delete))>> : std::true_type {
1413 };
1416 void call_operator_delete(T *p, size_t, size_t) {
1417  T::operator delete(p);
1418 }
1419 template <
1420  typename T,
1422 void call_operator_delete(T *p, size_t s, size_t) {
1423  T::operator delete(p, s);
1424 }
1425 
1426 inline void call_operator_delete(void *p, size_t s, size_t a) {
1427  (void) s;
1428  (void) a;
1429 #if defined(__cpp_aligned_new) && (!defined(_MSC_VER) || _MSC_VER >= 1912)
1430  if (a > __STDCPP_DEFAULT_NEW_ALIGNMENT__) {
1431 # ifdef __cpp_sized_deallocation
1432  ::operator delete(p, s, std::align_val_t(a));
1433 # else
1434  ::operator delete(p, std::align_val_t(a));
1435 # endif
1436  return;
1437  }
1438 #endif
1439 #ifdef __cpp_sized_deallocation
1440  ::operator delete(p, s);
1441 #else
1442  ::operator delete(p);
1443 #endif
1444 }
1445 
1446 inline void add_class_method(object &cls, const char *name_, const cpp_function &cf) {
1447  cls.attr(cf.name()) = cf;
1448  if (std::strcmp(name_, "__eq__") == 0 && !cls.attr("__dict__").contains("__hash__")) {
1449  cls.attr("__hash__") = none();
1450  }
1451 }
1452 
1454 
1455 template <typename /*Derived*/, typename F>
1458 auto method_adaptor(F &&f) -> decltype(std::forward<F>(f)) {
1459  return std::forward<F>(f);
1460 }
1461 
1462 template <typename Derived, typename Return, typename Class, typename... Args>
1463 auto method_adaptor(Return (Class::*pmf)(Args...)) -> Return (Derived::*)(Args...) {
1464  static_assert(
1466  "Cannot bind an inaccessible base class method; use a lambda definition instead");
1467  return pmf;
1468 }
1469 
1470 template <typename Derived, typename Return, typename Class, typename... Args>
1471 auto method_adaptor(Return (Class::*pmf)(Args...) const) -> Return (Derived::*)(Args...) const {
1472  static_assert(
1474  "Cannot bind an inaccessible base class method; use a lambda definition instead");
1475  return pmf;
1476 }
1477 
1478 template <typename type_, typename... options>
1479 class class_ : public detail::generic_type {
1480  template <typename T>
1481  using is_holder = detail::is_holder_type<type_, T>;
1482  template <typename T>
1483  using is_subtype = detail::is_strict_base_of<type_, T>;
1484  template <typename T>
1485  using is_base = detail::is_strict_base_of<T, type_>;
1486  // struct instead of using here to help MSVC:
1487  template <typename T>
1488  struct is_valid_class_option : detail::any_of<is_holder<T>, is_subtype<T>, is_base<T>> {};
1489 
1490 public:
1491  using type = type_;
1493  constexpr static bool has_alias = !std::is_void<type_alias>::value;
1494  using holder_type = detail::exactly_one_t<is_holder, std::unique_ptr<type>, options...>;
1495 
1496  static_assert(detail::all_of<is_valid_class_option<options>...>::value,
1497  "Unknown/invalid class_ template parameters provided");
1498 
1499  static_assert(!has_alias || std::is_polymorphic<type>::value,
1500  "Cannot use an alias class with a non-polymorphic type");
1501 
1502  PYBIND11_OBJECT(class_, generic_type, PyType_Check)
1503 
1504  template <typename... Extra>
1505  class_(handle scope, const char *name, const Extra &...extra) {
1506  using namespace detail;
1507 
1508  // MI can only be specified via class_ template options, not constructor parameters
1509  static_assert(
1510  none_of<is_pyobject<Extra>...>::value || // no base class arguments, or:
1511  (constexpr_sum(is_pyobject<Extra>::value...) == 1 && // Exactly one base
1512  constexpr_sum(is_base<options>::value...) == 0 && // no template option bases
1513  // no multiple_inheritance attr
1514  none_of<std::is_same<multiple_inheritance, Extra>...>::value),
1515  "Error: multiple inheritance bases must be specified via class_ template options");
1516 
1517  type_record record;
1518  record.scope = scope;
1519  record.name = name;
1520  record.type = &typeid(type);
1523  record.holder_size = sizeof(holder_type);
1524  record.init_instance = init_instance;
1525  record.dealloc = dealloc;
1527 
1528  set_operator_new<type>(&record);
1529 
1530  /* Register base classes specified via template arguments to class_, if any */
1531  PYBIND11_EXPAND_SIDE_EFFECTS(add_base<options>(record));
1532 
1533  /* Process optional arguments, if any */
1534  process_attributes<Extra...>::init(extra..., &record);
1535 
1536  generic_type::initialize(record);
1537 
1538  if (has_alias) {
1539  auto &instances = record.module_local ? get_local_internals().registered_types_cpp
1541  instances[std::type_index(typeid(type_alias))]
1542  = instances[std::type_index(typeid(type))];
1543  }
1544  }
1545 
1547  static void add_base(detail::type_record &rec) {
1548  rec.add_base(typeid(Base), [](void *src) -> void * {
1549  return static_cast<Base *>(reinterpret_cast<type *>(src));
1550  });
1551  }
1552 
1554  static void add_base(detail::type_record &) {}
1555 
1556  template <typename Func, typename... Extra>
1557  class_ &def(const char *name_, Func &&f, const Extra &...extra) {
1558  cpp_function cf(method_adaptor<type>(std::forward<Func>(f)),
1559  name(name_),
1560  is_method(*this),
1561  sibling(getattr(*this, name_, none())),
1562  extra...);
1563  add_class_method(*this, name_, cf);
1564  return *this;
1565  }
1566 
1567  template <typename Func, typename... Extra>
1568  class_ &def_static(const char *name_, Func &&f, const Extra &...extra) {
1570  "def_static(...) called with a non-static member function pointer");
1571  cpp_function cf(std::forward<Func>(f),
1572  name(name_),
1573  scope(*this),
1574  sibling(getattr(*this, name_, none())),
1575  extra...);
1576  auto cf_name = cf.name();
1577  attr(std::move(cf_name)) = staticmethod(std::move(cf));
1578  return *this;
1579  }
1580 
1581  template <detail::op_id id, detail::op_type ot, typename L, typename R, typename... Extra>
1582  class_ &def(const detail::op_<id, ot, L, R> &op, const Extra &...extra) {
1583  op.execute(*this, extra...);
1584  return *this;
1585  }
1586 
1587  template <detail::op_id id, detail::op_type ot, typename L, typename R, typename... Extra>
1588  class_ &def_cast(const detail::op_<id, ot, L, R> &op, const Extra &...extra) {
1589  op.execute_cast(*this, extra...);
1590  return *this;
1591  }
1592 
1593  template <typename... Args, typename... Extra>
1594  class_ &def(const detail::initimpl::constructor<Args...> &init, const Extra &...extra) {
1596  init.execute(*this, extra...);
1597  return *this;
1598  }
1599 
1600  template <typename... Args, typename... Extra>
1601  class_ &def(const detail::initimpl::alias_constructor<Args...> &init, const Extra &...extra) {
1603  init.execute(*this, extra...);
1604  return *this;
1605  }
1606 
1607  template <typename... Args, typename... Extra>
1608  class_ &def(detail::initimpl::factory<Args...> &&init, const Extra &...extra) {
1609  std::move(init).execute(*this, extra...);
1610  return *this;
1611  }
1612 
1613  template <typename... Args, typename... Extra>
1614  class_ &def(detail::initimpl::pickle_factory<Args...> &&pf, const Extra &...extra) {
1615  std::move(pf).execute(*this, extra...);
1616  return *this;
1617  }
1618 
1619  template <typename Func>
1620  class_ &def_buffer(Func &&func) {
1621  struct capture {
1622  Func func;
1623  };
1624  auto *ptr = new capture{std::forward<Func>(func)};
1625  install_buffer_funcs(
1626  [](PyObject *obj, void *ptr) -> buffer_info * {
1627  detail::make_caster<type> caster;
1628  if (!caster.load(obj, false)) {
1629  return nullptr;
1630  }
1631  return new buffer_info(((capture *) ptr)->func(std::move(caster)));
1632  },
1633  ptr);
1634  weakref(m_ptr, cpp_function([ptr](handle wr) {
1635  delete ptr;
1636  wr.dec_ref();
1637  }))
1638  .release();
1639  return *this;
1640  }
1641 
1642  template <typename Return, typename Class, typename... Args>
1643  class_ &def_buffer(Return (Class::*func)(Args...)) {
1644  return def_buffer([func](type &obj) { return (obj.*func)(); });
1645  }
1646 
1647  template <typename Return, typename Class, typename... Args>
1648  class_ &def_buffer(Return (Class::*func)(Args...) const) {
1649  return def_buffer([func](const type &obj) { return (obj.*func)(); });
1650  }
1651 
1652  template <typename C, typename D, typename... Extra>
1653  class_ &def_readwrite(const char *name, D C::*pm, const Extra &...extra) {
1655  "def_readwrite() requires a class member (or base class member)");
1656  cpp_function fget([pm](const type &c) -> const D & { return c.*pm; }, is_method(*this)),
1657  fset([pm](type &c, const D &value) { c.*pm = value; }, is_method(*this));
1658  def_property(name, fget, fset, return_value_policy::reference_internal, extra...);
1659  return *this;
1660  }
1661 
1662  template <typename C, typename D, typename... Extra>
1663  class_ &def_readonly(const char *name, const D C::*pm, const Extra &...extra) {
1665  "def_readonly() requires a class member (or base class member)");
1666  cpp_function fget([pm](const type &c) -> const D & { return c.*pm; }, is_method(*this));
1667  def_property_readonly(name, fget, return_value_policy::reference_internal, extra...);
1668  return *this;
1669  }
1670 
1671  template <typename D, typename... Extra>
1672  class_ &def_readwrite_static(const char *name, D *pm, const Extra &...extra) {
1673  cpp_function fget([pm](const object &) -> const D & { return *pm; }, scope(*this)),
1674  fset([pm](const object &, const D &value) { *pm = value; }, scope(*this));
1675  def_property_static(name, fget, fset, return_value_policy::reference, extra...);
1676  return *this;
1677  }
1678 
1679  template <typename D, typename... Extra>
1680  class_ &def_readonly_static(const char *name, const D *pm, const Extra &...extra) {
1681  cpp_function fget([pm](const object &) -> const D & { return *pm; }, scope(*this));
1682  def_property_readonly_static(name, fget, return_value_policy::reference, extra...);
1683  return *this;
1684  }
1685 
1687  template <typename Getter, typename... Extra>
1688  class_ &def_property_readonly(const char *name, const Getter &fget, const Extra &...extra) {
1689  return def_property_readonly(name,
1690  cpp_function(method_adaptor<type>(fget)),
1692  extra...);
1693  }
1694 
1696  template <typename... Extra>
1697  class_ &
1698  def_property_readonly(const char *name, const cpp_function &fget, const Extra &...extra) {
1699  return def_property(name, fget, nullptr, extra...);
1700  }
1701 
1703  template <typename Getter, typename... Extra>
1704  class_ &
1705  def_property_readonly_static(const char *name, const Getter &fget, const Extra &...extra) {
1706  return def_property_readonly_static(
1707  name, cpp_function(fget), return_value_policy::reference, extra...);
1708  }
1709 
1711  template <typename... Extra>
1712  class_ &def_property_readonly_static(const char *name,
1713  const cpp_function &fget,
1714  const Extra &...extra) {
1715  return def_property_static(name, fget, nullptr, extra...);
1716  }
1717 
1719  template <typename Getter, typename Setter, typename... Extra>
1720  class_ &
1721  def_property(const char *name, const Getter &fget, const Setter &fset, const Extra &...extra) {
1722  return def_property(name, fget, cpp_function(method_adaptor<type>(fset)), extra...);
1723  }
1724  template <typename Getter, typename... Extra>
1725  class_ &def_property(const char *name,
1726  const Getter &fget,
1727  const cpp_function &fset,
1728  const Extra &...extra) {
1729  return def_property(name,
1730  cpp_function(method_adaptor<type>(fget)),
1731  fset,
1733  extra...);
1734  }
1735 
1737  template <typename... Extra>
1738  class_ &def_property(const char *name,
1739  const cpp_function &fget,
1740  const cpp_function &fset,
1741  const Extra &...extra) {
1742  return def_property_static(name, fget, fset, is_method(*this), extra...);
1743  }
1744 
1746  template <typename Getter, typename... Extra>
1747  class_ &def_property_static(const char *name,
1748  const Getter &fget,
1749  const cpp_function &fset,
1750  const Extra &...extra) {
1751  return def_property_static(
1752  name, cpp_function(fget), fset, return_value_policy::reference, extra...);
1753  }
1754 
1756  template <typename... Extra>
1757  class_ &def_property_static(const char *name,
1758  const cpp_function &fget,
1759  const cpp_function &fset,
1760  const Extra &...extra) {
1762  "Argument annotations are not allowed for properties");
1763  auto rec_fget = get_function_record(fget), rec_fset = get_function_record(fset);
1764  auto *rec_active = rec_fget;
1765  if (rec_fget) {
1766  char *doc_prev = rec_fget->doc; /* 'extra' field may include a property-specific
1767  documentation string */
1768  detail::process_attributes<Extra...>::init(extra..., rec_fget);
1769  if (rec_fget->doc && rec_fget->doc != doc_prev) {
1770  std::free(doc_prev);
1771  rec_fget->doc = PYBIND11_COMPAT_STRDUP(rec_fget->doc);
1772  }
1773  }
1774  if (rec_fset) {
1775  char *doc_prev = rec_fset->doc;
1776  detail::process_attributes<Extra...>::init(extra..., rec_fset);
1777  if (rec_fset->doc && rec_fset->doc != doc_prev) {
1778  std::free(doc_prev);
1779  rec_fset->doc = PYBIND11_COMPAT_STRDUP(rec_fset->doc);
1780  }
1781  if (!rec_active) {
1782  rec_active = rec_fset;
1783  }
1784  }
1785  def_property_static_impl(name, fget, fset, rec_active);
1786  return *this;
1787  }
1788 
1789 private:
1791  template <typename T>
1792  static void init_holder(detail::instance *inst,
1793  detail::value_and_holder &v_h,
1794  const holder_type * /* unused */,
1795  const std::enable_shared_from_this<T> * /* dummy */) {
1796 
1797  auto sh = std::dynamic_pointer_cast<typename holder_type::element_type>(
1798  detail::try_get_shared_from_this(v_h.value_ptr<type>()));
1799  if (sh) {
1800  new (std::addressof(v_h.holder<holder_type>())) holder_type(std::move(sh));
1801  v_h.set_holder_constructed();
1802  }
1803 
1804  if (!v_h.holder_constructed() && inst->owned) {
1805  new (std::addressof(v_h.holder<holder_type>())) holder_type(v_h.value_ptr<type>());
1806  v_h.set_holder_constructed();
1807  }
1808  }
1809 
1810  static void init_holder_from_existing(const detail::value_and_holder &v_h,
1811  const holder_type *holder_ptr,
1812  std::true_type /*is_copy_constructible*/) {
1813  new (std::addressof(v_h.holder<holder_type>()))
1814  holder_type(*reinterpret_cast<const holder_type *>(holder_ptr));
1815  }
1816 
1817  static void init_holder_from_existing(const detail::value_and_holder &v_h,
1818  const holder_type *holder_ptr,
1819  std::false_type /*is_copy_constructible*/) {
1820  new (std::addressof(v_h.holder<holder_type>()))
1821  holder_type(std::move(*const_cast<holder_type *>(holder_ptr)));
1822  }
1823 
1826  static void init_holder(detail::instance *inst,
1827  detail::value_and_holder &v_h,
1828  const holder_type *holder_ptr,
1829  const void * /* dummy -- not enable_shared_from_this<T>) */) {
1830  if (holder_ptr) {
1831  init_holder_from_existing(v_h, holder_ptr, std::is_copy_constructible<holder_type>());
1832  v_h.set_holder_constructed();
1834  || inst->owned) {
1835  new (std::addressof(v_h.holder<holder_type>())) holder_type(v_h.value_ptr<type>());
1836  v_h.set_holder_constructed();
1837  }
1838  }
1839 
1844  static void init_instance(detail::instance *inst, const void *holder_ptr) {
1845  auto v_h = inst->get_value_and_holder(detail::get_type_info(typeid(type)));
1846  if (!v_h.instance_registered()) {
1847  register_instance(inst, v_h.value_ptr(), v_h.type);
1848  v_h.set_instance_registered();
1849  }
1850  init_holder(inst, v_h, (const holder_type *) holder_ptr, v_h.value_ptr<type>());
1851  }
1852 
1854  static void dealloc(detail::value_and_holder &v_h) {
1855  // We could be deallocating because we are cleaning up after a Python exception.
1856  // If so, the Python error indicator will be set. We need to clear that before
1857  // running the destructor, in case the destructor code calls more Python.
1858  // If we don't, the Python API will exit with an exception, and pybind11 will
1859  // throw error_already_set from the C++ destructor which is forbidden and triggers
1860  // std::terminate().
1862  if (v_h.holder_constructed()) {
1863  v_h.holder<holder_type>().~holder_type();
1864  v_h.set_holder_constructed(false);
1865  } else {
1867  v_h.value_ptr<type>(), v_h.type->type_size, v_h.type->type_align);
1868  }
1869  v_h.value_ptr() = nullptr;
1870  }
1871 
1872  static detail::function_record *get_function_record(handle h) {
1873  h = detail::get_function(h);
1874  return h ? (detail::function_record *) reinterpret_borrow<capsule>(
1875  PyCFunction_GET_SELF(h.ptr()))
1876  : nullptr;
1877  }
1878 };
1879 
1881 template <typename... Args>
1882 detail::initimpl::constructor<Args...> init() {
1883  return {};
1884 }
1887 template <typename... Args>
1888 detail::initimpl::alias_constructor<Args...> init_alias() {
1889  return {};
1890 }
1891 
1893 template <typename Func, typename Ret = detail::initimpl::factory<Func>>
1894 Ret init(Func &&f) {
1895  return {std::forward<Func>(f)};
1896 }
1897 
1901 template <typename CFunc, typename AFunc, typename Ret = detail::initimpl::factory<CFunc, AFunc>>
1902 Ret init(CFunc &&c, AFunc &&a) {
1903  return {std::forward<CFunc>(c), std::forward<AFunc>(a)};
1904 }
1905 
1908 template <typename GetState, typename SetState>
1909 detail::initimpl::pickle_factory<GetState, SetState> pickle(GetState &&g, SetState &&s) {
1910  return {std::forward<GetState>(g), std::forward<SetState>(s)};
1911 }
1912 
1914 
1916  dict entries = arg.get_type().attr("__entries");
1917  for (auto kv : entries) {
1918  if (handle(kv.second[int_(0)]).equal(arg)) {
1919  return pybind11::str(kv.first);
1920  }
1921  }
1922  return "???";
1923 }
1924 
1925 struct enum_base {
1926  enum_base(const handle &base, const handle &parent) : m_base(base), m_parent(parent) {}
1927 
1928  PYBIND11_NOINLINE void init(bool is_arithmetic, bool is_convertible) {
1929  m_base.attr("__entries") = dict();
1930  auto property = handle((PyObject *) &PyProperty_Type);
1931  auto static_property = handle((PyObject *) get_internals().static_property_type);
1932 
1933  m_base.attr("__repr__") = cpp_function(
1934  [](const object &arg) -> str {
1935  handle type = type::handle_of(arg);
1936  object type_name = type.attr("__name__");
1937  return pybind11::str("<{}.{}: {}>")
1938  .format(std::move(type_name), enum_name(arg), int_(arg));
1939  },
1940  name("__repr__"),
1941  is_method(m_base));
1942 
1943  m_base.attr("name") = property(cpp_function(&enum_name, name("name"), is_method(m_base)));
1944 
1945  m_base.attr("__str__") = cpp_function(
1946  [](handle arg) -> str {
1947  object type_name = type::handle_of(arg).attr("__name__");
1948  return pybind11::str("{}.{}").format(std::move(type_name), enum_name(arg));
1949  },
1950  name("name"),
1951  is_method(m_base));
1952 
1953  m_base.attr("__doc__") = static_property(
1954  cpp_function(
1955  [](handle arg) -> std::string {
1956  std::string docstring;
1957  dict entries = arg.attr("__entries");
1958  if (((PyTypeObject *) arg.ptr())->tp_doc) {
1959  docstring += std::string(((PyTypeObject *) arg.ptr())->tp_doc) + "\n\n";
1960  }
1961  docstring += "Members:";
1962  for (auto kv : entries) {
1963  auto key = std::string(pybind11::str(kv.first));
1964  auto comment = kv.second[int_(1)];
1965  docstring += "\n\n " + key;
1966  if (!comment.is_none()) {
1967  docstring += " : " + (std::string) pybind11::str(comment);
1968  }
1969  }
1970  return docstring;
1971  },
1972  name("__doc__")),
1973  none(),
1974  none(),
1975  "");
1976 
1977  m_base.attr("__members__") = static_property(cpp_function(
1978  [](handle arg) -> dict {
1979  dict entries = arg.attr("__entries"),
1980  m;
1981  for (auto kv : entries) {
1982  m[kv.first] = kv.second[int_(0)];
1983  }
1984  return m;
1985  },
1986  name("__members__")),
1987  none(),
1988  none(),
1989  "");
1990 
1991 #define PYBIND11_ENUM_OP_STRICT(op, expr, strict_behavior) \
1992  m_base.attr(op) = cpp_function( \
1993  [](const object &a, const object &b) { \
1994  if (!type::handle_of(a).is(type::handle_of(b))) \
1995  strict_behavior; /* NOLINT(bugprone-macro-parentheses) */ \
1996  return expr; \
1997  }, \
1998  name(op), \
1999  is_method(m_base), \
2000  arg("other"))
2001 
2002 #define PYBIND11_ENUM_OP_CONV(op, expr) \
2003  m_base.attr(op) = cpp_function( \
2004  [](const object &a_, const object &b_) { \
2005  int_ a(a_), b(b_); \
2006  return expr; \
2007  }, \
2008  name(op), \
2009  is_method(m_base), \
2010  arg("other"))
2011 
2012 #define PYBIND11_ENUM_OP_CONV_LHS(op, expr) \
2013  m_base.attr(op) = cpp_function( \
2014  [](const object &a_, const object &b) { \
2015  int_ a(a_); \
2016  return expr; \
2017  }, \
2018  name(op), \
2019  is_method(m_base), \
2020  arg("other"))
2021 
2022  if (is_convertible) {
2023  PYBIND11_ENUM_OP_CONV_LHS("__eq__", !b.is_none() && a.equal(b));
2024  PYBIND11_ENUM_OP_CONV_LHS("__ne__", b.is_none() || !a.equal(b));
2025 
2026  if (is_arithmetic) {
2027  PYBIND11_ENUM_OP_CONV("__lt__", a < b);
2028  PYBIND11_ENUM_OP_CONV("__gt__", a > b);
2029  PYBIND11_ENUM_OP_CONV("__le__", a <= b);
2030  PYBIND11_ENUM_OP_CONV("__ge__", a >= b);
2031  PYBIND11_ENUM_OP_CONV("__and__", a & b);
2032  PYBIND11_ENUM_OP_CONV("__rand__", a & b);
2033  PYBIND11_ENUM_OP_CONV("__or__", a | b);
2034  PYBIND11_ENUM_OP_CONV("__ror__", a | b);
2035  PYBIND11_ENUM_OP_CONV("__xor__", a ^ b);
2036  PYBIND11_ENUM_OP_CONV("__rxor__", a ^ b);
2037  m_base.attr("__invert__")
2038  = cpp_function([](const object &arg) { return ~(int_(arg)); },
2039  name("__invert__"),
2040  is_method(m_base));
2041  }
2042  } else {
2043  PYBIND11_ENUM_OP_STRICT("__eq__", int_(a).equal(int_(b)), return false);
2044  PYBIND11_ENUM_OP_STRICT("__ne__", !int_(a).equal(int_(b)), return true);
2045 
2046  if (is_arithmetic) {
2047 #define PYBIND11_THROW throw type_error("Expected an enumeration of matching type!");
2052 #undef PYBIND11_THROW
2053  }
2054  }
2055 
2056 #undef PYBIND11_ENUM_OP_CONV_LHS
2057 #undef PYBIND11_ENUM_OP_CONV
2058 #undef PYBIND11_ENUM_OP_STRICT
2059 
2060  m_base.attr("__getstate__") = cpp_function(
2061  [](const object &arg) { return int_(arg); }, name("__getstate__"), is_method(m_base));
2062 
2063  m_base.attr("__hash__") = cpp_function(
2064  [](const object &arg) { return int_(arg); }, name("__hash__"), is_method(m_base));
2065  }
2066 
2067  PYBIND11_NOINLINE void value(char const *name_, object value, const char *doc = nullptr) {
2068  dict entries = m_base.attr("__entries");
2069  str name(name_);
2070  if (entries.contains(name)) {
2071  std::string type_name = (std::string) str(m_base.attr("__name__"));
2072  throw value_error(std::move(type_name) + ": element \"" + std::string(name_)
2073  + "\" already exists!");
2074  }
2075 
2076  entries[name] = std::make_pair(value, doc);
2077  m_base.attr(std::move(name)) = std::move(value);
2078  }
2079 
2081  dict entries = m_base.attr("__entries");
2082  for (auto kv : entries) {
2083  m_parent.attr(kv.first) = kv.second[int_(0)];
2084  }
2085  }
2086 
2089 };
2090 
2091 template <bool is_signed, size_t length>
2093 template <>
2094 struct equivalent_integer<true, 1> {
2095  using type = int8_t;
2096 };
2097 template <>
2098 struct equivalent_integer<false, 1> {
2099  using type = uint8_t;
2100 };
2101 template <>
2102 struct equivalent_integer<true, 2> {
2103  using type = int16_t;
2104 };
2105 template <>
2106 struct equivalent_integer<false, 2> {
2107  using type = uint16_t;
2108 };
2109 template <>
2110 struct equivalent_integer<true, 4> {
2111  using type = int32_t;
2112 };
2113 template <>
2114 struct equivalent_integer<false, 4> {
2115  using type = uint32_t;
2116 };
2117 template <>
2118 struct equivalent_integer<true, 8> {
2119  using type = int64_t;
2120 };
2121 template <>
2122 struct equivalent_integer<false, 8> {
2123  using type = uint64_t;
2124 };
2125 
2126 template <typename IntLike>
2127 using equivalent_integer_t =
2129 
2131 
2132 template <typename Type>
2134 class enum_ : public class_<Type> {
2135 public:
2137  using Base::attr;
2138  using Base::def;
2139  using Base::def_property_readonly;
2140  using Base::def_property_readonly_static;
2142  // Scalar is the integer representation of underlying type
2143  using Scalar = detail::conditional_t<detail::any_of<detail::is_std_char_type<Underlying>,
2144  std::is_same<Underlying, bool>>::value,
2145  detail::equivalent_integer_t<Underlying>,
2147 
2148  template <typename... Extra>
2149  enum_(const handle &scope, const char *name, const Extra &...extra)
2150  : class_<Type>(scope, name, extra...), m_base(*this, scope) {
2151  constexpr bool is_arithmetic = detail::any_of<std::is_same<arithmetic, Extra>...>::value;
2152  constexpr bool is_convertible = std::is_convertible<Type, Underlying>::value;
2153  m_base.init(is_arithmetic, is_convertible);
2154 
2155  def(init([](Scalar i) { return static_cast<Type>(i); }), arg("value"));
2156  def_property_readonly("value", [](Type value) { return (Scalar) value; });
2157  def("__int__", [](Type value) { return (Scalar) value; });
2158  def("__index__", [](Type value) { return (Scalar) value; });
2159  attr("__setstate__") = cpp_function(
2160  [](detail::value_and_holder &v_h, Scalar arg) {
2161  detail::initimpl::setstate<Base>(
2162  v_h, static_cast<Type>(arg), Py_TYPE(v_h.inst) != v_h.type->type);
2163  },
2164  detail::is_new_style_constructor(),
2165  pybind11::name("__setstate__"),
2166  is_method(*this),
2167  arg("state"));
2168  }
2169 
2172  m_base.export_values();
2173  return *this;
2174  }
2175 
2177  enum_ &value(char const *name, Type value, const char *doc = nullptr) {
2178  m_base.value(name, pybind11::cast(value, return_value_policy::copy), doc);
2179  return *this;
2180  }
2181 
2182 private:
2183  detail::enum_base m_base;
2184 };
2185 
2187 
2189  if (!nurse || !patient) {
2190  pybind11_fail("Could not activate keep_alive!");
2191  }
2192 
2193  if (patient.is_none() || nurse.is_none()) {
2194  return; /* Nothing to keep alive or nothing to be kept alive by */
2195  }
2196 
2197  auto tinfo = all_type_info(Py_TYPE(nurse.ptr()));
2198  if (!tinfo.empty()) {
2199  /* It's a pybind-registered type, so we can store the patient in the
2200  * internal list. */
2201  add_patient(nurse.ptr(), patient.ptr());
2202  } else {
2203  /* Fall back to clever approach based on weak references taken from
2204  * Boost.Python. This is not used for pybind-registered types because
2205  * the objects can be destroyed out-of-order in a GC pass. */
2206  cpp_function disable_lifesupport([patient](handle weakref) {
2207  patient.dec_ref();
2208  weakref.dec_ref();
2209  });
2210 
2211  weakref wr(nurse, disable_lifesupport);
2212 
2213  patient.inc_ref(); /* reference patient and leak the weak reference */
2214  (void) wr.release();
2215  }
2216 }
2217 
2218 PYBIND11_NOINLINE void
2219 keep_alive_impl(size_t Nurse, size_t Patient, function_call &call, handle ret) {
2220  auto get_arg = [&](size_t n) {
2221  if (n == 0) {
2222  return ret;
2223  }
2224  if (n == 1 && call.init_self) {
2225  return call.init_self;
2226  }
2227  if (n <= call.args.size()) {
2228  return call.args[n - 1];
2229  }
2230  return handle();
2231  };
2232 
2233  keep_alive_impl(get_arg(Nurse), get_arg(Patient));
2234 }
2235 
2236 inline std::pair<decltype(internals::registered_types_py)::iterator, bool>
2238  auto res = get_internals()
2240 #ifdef __cpp_lib_unordered_map_try_emplace
2241  .try_emplace(type);
2242 #else
2243  .emplace(type, std::vector<detail::type_info *>());
2244 #endif
2245  if (res.second) {
2246  // New cache entry created; set up a weak reference to automatically remove it if the type
2247  // gets destroyed:
2248  weakref((PyObject *) type, cpp_function([type](handle wr) {
2249  get_internals().registered_types_py.erase(type);
2250 
2251  // TODO consolidate the erasure code in pybind11_meta_dealloc() in class.h
2252  auto &cache = get_internals().inactive_override_cache;
2253  for (auto it = cache.begin(), last = cache.end(); it != last;) {
2254  if (it->first == reinterpret_cast<PyObject *>(type)) {
2255  it = cache.erase(it);
2256  } else {
2257  ++it;
2258  }
2259  }
2260 
2261  wr.dec_ref();
2262  }))
2263  .release();
2264  }
2265 
2266  return res;
2267 }
2268 
2269 /* There are a large number of apparently unused template arguments because
2270  * each combination requires a separate py::class_ registration.
2271  */
2272 template <typename Access,
2273  return_value_policy Policy,
2274  typename Iterator,
2275  typename Sentinel,
2276  typename ValueType,
2277  typename... Extra>
2279  Iterator it;
2280  Sentinel end;
2282 };
2283 
2284 // Note: these helpers take the iterator by non-const reference because some
2285 // iterators in the wild can't be dereferenced when const. The & after Iterator
2286 // is required for MSVC < 16.9. SFINAE cannot be reused for result_type due to
2287 // bugs in ICC, NVCC, and PGI compilers. See PR #3293.
2288 template <typename Iterator, typename SFINAE = decltype(*std::declval<Iterator &>())>
2290  using result_type = decltype(*std::declval<Iterator &>());
2291  // NOLINTNEXTLINE(readability-const-return-type) // PR #3263
2292  result_type operator()(Iterator &it) const { return *it; }
2293 };
2294 
2295 template <typename Iterator, typename SFINAE = decltype((*std::declval<Iterator &>()).first)>
2297 private:
2298  using pair_type = decltype(*std::declval<Iterator &>());
2299 
2300 public:
2301  /* If either the pair itself or the element of the pair is a reference, we
2302  * want to return a reference, otherwise a value. When the decltype
2303  * expression is parenthesized it is based on the value category of the
2304  * expression; otherwise it is the declared type of the pair member.
2305  * The use of declval<pair_type> in the second branch rather than directly
2306  * using *std::declval<Iterator &>() is a workaround for nvcc
2307  * (it's not used in the first branch because going via decltype and back
2308  * through declval does not perfectly preserve references).
2309  */
2310  using result_type
2312  decltype(((*std::declval<Iterator &>()).first)),
2313  decltype(std::declval<pair_type>().first)>;
2314  result_type operator()(Iterator &it) const { return (*it).first; }
2315 };
2316 
2317 template <typename Iterator, typename SFINAE = decltype((*std::declval<Iterator &>()).second)>
2319 private:
2320  using pair_type = decltype(*std::declval<Iterator &>());
2321 
2322 public:
2323  using result_type
2325  decltype(((*std::declval<Iterator &>()).second)),
2326  decltype(std::declval<pair_type>().second)>;
2327  result_type operator()(Iterator &it) const { return (*it).second; }
2328 };
2329 
2330 template <typename Access,
2331  return_value_policy Policy,
2332  typename Iterator,
2333  typename Sentinel,
2334  typename ValueType,
2335  typename... Extra>
2336 iterator make_iterator_impl(Iterator &&first, Sentinel &&last, Extra &&...extra) {
2337  using state = detail::iterator_state<Access, Policy, Iterator, Sentinel, ValueType, Extra...>;
2338  // TODO: state captures only the types of Extra, not the values
2339 
2340  if (!detail::get_type_info(typeid(state), false)) {
2341  class_<state>(handle(), "iterator", pybind11::module_local())
2342  .def("__iter__", [](state &s) -> state & { return s; })
2343  .def(
2344  "__next__",
2345  [](state &s) -> ValueType {
2346  if (!s.first_or_done) {
2347  ++s.it;
2348  } else {
2349  s.first_or_done = false;
2350  }
2351  if (s.it == s.end) {
2352  s.first_or_done = true;
2353  throw stop_iteration();
2354  }
2355  return Access()(s.it);
2356  // NOLINTNEXTLINE(readability-const-return-type) // PR #3263
2357  },
2358  std::forward<Extra>(extra)...,
2359  Policy);
2360  }
2361 
2362  return cast(state{std::forward<Iterator>(first), std::forward<Sentinel>(last), true});
2363 }
2364 
2366 
2369  typename Iterator,
2370  typename Sentinel,
2371  typename ValueType = typename detail::iterator_access<Iterator>::result_type,
2372  typename... Extra>
2373 iterator make_iterator(Iterator &&first, Sentinel &&last, Extra &&...extra) {
2374  return detail::make_iterator_impl<detail::iterator_access<Iterator>,
2375  Policy,
2376  Iterator,
2377  Sentinel,
2378  ValueType,
2379  Extra...>(std::forward<Iterator>(first),
2380  std::forward<Sentinel>(last),
2381  std::forward<Extra>(extra)...);
2382 }
2383 
2387  typename Iterator,
2388  typename Sentinel,
2389  typename KeyType = typename detail::iterator_key_access<Iterator>::result_type,
2390  typename... Extra>
2391 iterator make_key_iterator(Iterator &&first, Sentinel &&last, Extra &&...extra) {
2392  return detail::make_iterator_impl<detail::iterator_key_access<Iterator>,
2393  Policy,
2394  Iterator,
2395  Sentinel,
2396  KeyType,
2397  Extra...>(std::forward<Iterator>(first),
2398  std::forward<Sentinel>(last),
2399  std::forward<Extra>(extra)...);
2400 }
2401 
2405  typename Iterator,
2406  typename Sentinel,
2407  typename ValueType = typename detail::iterator_value_access<Iterator>::result_type,
2408  typename... Extra>
2409 iterator make_value_iterator(Iterator &&first, Sentinel &&last, Extra &&...extra) {
2410  return detail::make_iterator_impl<detail::iterator_value_access<Iterator>,
2411  Policy,
2412  Iterator,
2413  Sentinel,
2414  ValueType,
2415  Extra...>(std::forward<Iterator>(first),
2416  std::forward<Sentinel>(last),
2417  std::forward<Extra>(extra)...);
2418 }
2419 
2423  typename Type,
2424  typename... Extra>
2425 iterator make_iterator(Type &value, Extra &&...extra) {
2426  return make_iterator<Policy>(
2427  std::begin(value), std::end(value), std::forward<Extra>(extra)...);
2428 }
2429 
2433  typename Type,
2434  typename... Extra>
2435 iterator make_key_iterator(Type &value, Extra &&...extra) {
2436  return make_key_iterator<Policy>(
2437  std::begin(value), std::end(value), std::forward<Extra>(extra)...);
2438 }
2439 
2443  typename Type,
2444  typename... Extra>
2445 iterator make_value_iterator(Type &value, Extra &&...extra) {
2446  return make_value_iterator<Policy>(
2447  std::begin(value), std::end(value), std::forward<Extra>(extra)...);
2448 }
2449 
2450 template <typename InputType, typename OutputType>
2452  struct set_flag {
2453  bool &flag;
2454  explicit set_flag(bool &flag_) : flag(flag_) { flag_ = true; }
2455  ~set_flag() { flag = false; }
2456  };
2457  auto implicit_caster = [](PyObject *obj, PyTypeObject *type) -> PyObject * {
2458  static bool currently_used = false;
2459  if (currently_used) { // implicit conversions are non-reentrant
2460  return nullptr;
2461  }
2462  set_flag flag_helper(currently_used);
2463  if (!detail::make_caster<InputType>().load(obj, false)) {
2464  return nullptr;
2465  }
2466  tuple args(1);
2467  args[0] = obj;
2468  PyObject *result = PyObject_Call((PyObject *) type, args.ptr(), nullptr);
2469  if (result == nullptr) {
2470  PyErr_Clear();
2471  }
2472  return result;
2473  };
2474 
2475  if (auto *tinfo = detail::get_type_info(typeid(OutputType))) {
2476  tinfo->implicit_conversions.emplace_back(std::move(implicit_caster));
2477  } else {
2478  pybind11_fail("implicitly_convertible: Unable to find type " + type_id<OutputType>());
2479  }
2480 }
2481 
2484  std::forward<ExceptionTranslator>(translator));
2485 }
2486 
2495  std::forward<ExceptionTranslator>(translator));
2496 }
2497 
2505 template <typename type>
2506 class exception : public object {
2507 public:
2508  exception() = default;
2509  exception(handle scope, const char *name, handle base = PyExc_Exception) {
2510  std::string full_name
2511  = scope.attr("__name__").cast<std::string>() + std::string(".") + name;
2512  m_ptr = PyErr_NewException(const_cast<char *>(full_name.c_str()), base.ptr(), nullptr);
2513  if (hasattr(scope, "__dict__") && scope.attr("__dict__").contains(name)) {
2514  pybind11_fail("Error during initialization: multiple incompatible "
2515  "definitions with name \""
2516  + std::string(name) + "\"");
2517  }
2518  scope.attr(name) = *this;
2519  }
2520 
2521  // Sets the current python exception to this exception object with the given message
2522  void operator()(const char *message) { PyErr_SetString(m_ptr, message); }
2523 };
2524 
2526 // Returns a reference to a function-local static exception object used in the simple
2527 // register_exception approach below. (It would be simpler to have the static local variable
2528 // directly in register_exception, but that makes clang <3.5 segfault - issue #1349).
2529 template <typename CppException>
2530 exception<CppException> &get_exception_object() {
2531  static exception<CppException> ex;
2532  return ex;
2533 }
2534 
2535 // Helper function for register_exception and register_local_exception
2536 template <typename CppException>
2537 exception<CppException> &
2538 register_exception_impl(handle scope, const char *name, handle base, bool isLocal) {
2539  auto &ex = detail::get_exception_object<CppException>();
2540  if (!ex) {
2541  ex = exception<CppException>(scope, name, base);
2542  }
2543 
2544  auto register_func
2546 
2547  register_func([](std::exception_ptr p) {
2548  if (!p) {
2549  return;
2550  }
2551  try {
2552  std::rethrow_exception(p);
2553  } catch (const CppException &e) {
2554  detail::get_exception_object<CppException>()(e.what());
2555  }
2556  });
2557  return ex;
2558 }
2559 
2561 
2562 
2568 template <typename CppException>
2569 exception<CppException> &
2570 register_exception(handle scope, const char *name, handle base = PyExc_Exception) {
2571  return detail::register_exception_impl<CppException>(scope, name, base, false /* isLocal */);
2572 }
2573 
2582 template <typename CppException>
2583 exception<CppException> &
2584 register_local_exception(handle scope, const char *name, handle base = PyExc_Exception) {
2585  return detail::register_exception_impl<CppException>(scope, name, base, true /* isLocal */);
2586 }
2587 
2590  auto strings = tuple(args.size());
2591  for (size_t i = 0; i < args.size(); ++i) {
2592  strings[i] = str(args[i]);
2593  }
2594  auto sep = kwargs.contains("sep") ? kwargs["sep"] : str(" ");
2595  auto line = sep.attr("join")(std::move(strings));
2596 
2597  object file;
2598  if (kwargs.contains("file")) {
2599  file = kwargs["file"].cast<object>();
2600  } else {
2601  try {
2602  file = module_::import("sys").attr("stdout");
2603  } catch (const error_already_set &) {
2604  /* If print() is called from code that is executed as
2605  part of garbage collection during interpreter shutdown,
2606  importing 'sys' can fail. Give up rather than crashing the
2607  interpreter in this case. */
2608  return;
2609  }
2610  }
2611 
2612  auto write = file.attr("write");
2613  write(std::move(line));
2614  write(kwargs.contains("end") ? kwargs["end"] : str("\n"));
2615 
2616  if (kwargs.contains("flush") && kwargs["flush"].cast<bool>()) {
2617  file.attr("flush")();
2618  }
2619 }
2621 
2622 template <return_value_policy policy = return_value_policy::automatic_reference, typename... Args>
2623 void print(Args &&...args) {
2624  auto c = detail::collect_arguments<policy>(std::forward<Args>(args)...);
2625  detail::print(c.args(), c.kwargs());
2626 }
2627 
2628 inline void
2629 error_already_set::m_fetched_error_deleter(detail::error_fetch_and_normalize *raw_ptr) {
2630  gil_scoped_acquire gil;
2632  delete raw_ptr;
2633 }
2634 
2635 inline const char *error_already_set::what() const noexcept {
2636  gil_scoped_acquire gil;
2638  return m_fetched_error->error_string().c_str();
2639 }
2640 
2642 
2643 inline function
2644 get_type_override(const void *this_ptr, const type_info *this_type, const char *name) {
2645  handle self = get_object_handle(this_ptr, this_type);
2646  if (!self) {
2647  return function();
2648  }
2649  handle type = type::handle_of(self);
2650  auto key = std::make_pair(type.ptr(), name);
2651 
2652  /* Cache functions that aren't overridden in Python to avoid
2653  many costly Python dictionary lookups below */
2654  auto &cache = get_internals().inactive_override_cache;
2655  if (cache.find(key) != cache.end()) {
2656  return function();
2657  }
2658 
2659  function override = getattr(self, name, function());
2660  if (override.is_cpp_function()) {
2661  cache.insert(std::move(key));
2662  return function();
2663  }
2664 
2665  /* Don't call dispatch code if invoked from overridden function.
2666  Unfortunately this doesn't work on PyPy. */
2667 #if !defined(PYPY_VERSION)
2668 # if PY_VERSION_HEX >= 0x03090000
2669  PyFrameObject *frame = PyThreadState_GetFrame(PyThreadState_Get());
2670  if (frame != nullptr) {
2671  PyCodeObject *f_code = PyFrame_GetCode(frame);
2672  // f_code is guaranteed to not be NULL
2673  if ((std::string) str(f_code->co_name) == name && f_code->co_argcount > 0) {
2674  PyObject *locals = PyEval_GetLocals();
2675  if (locals != nullptr) {
2676  PyObject *co_varnames = PyObject_GetAttrString((PyObject *) f_code, "co_varnames");
2677  PyObject *self_arg = PyTuple_GET_ITEM(co_varnames, 0);
2678  Py_DECREF(co_varnames);
2679  PyObject *self_caller = dict_getitem(locals, self_arg);
2680  if (self_caller == self.ptr()) {
2681  Py_DECREF(f_code);
2682  Py_DECREF(frame);
2683  return function();
2684  }
2685  }
2686  }
2687  Py_DECREF(f_code);
2688  Py_DECREF(frame);
2689  }
2690 # else
2691  PyFrameObject *frame = PyThreadState_Get()->frame;
2692  if (frame != nullptr && (std::string) str(frame->f_code->co_name) == name
2693  && frame->f_code->co_argcount > 0) {
2694  PyFrame_FastToLocals(frame);
2695  PyObject *self_caller
2696  = dict_getitem(frame->f_locals, PyTuple_GET_ITEM(frame->f_code->co_varnames, 0));
2697  if (self_caller == self.ptr()) {
2698  return function();
2699  }
2700  }
2701 # endif
2702 
2703 #else
2704  /* PyPy currently doesn't provide a detailed cpyext emulation of
2705  frame objects, so we have to emulate this using Python. This
2706  is going to be slow..*/
2707  dict d;
2708  d["self"] = self;
2709  d["name"] = pybind11::str(name);
2710  PyObject *result
2711  = PyRun_String("import inspect\n"
2712  "frame = inspect.currentframe()\n"
2713  "if frame is not None:\n"
2714  " frame = frame.f_back\n"
2715  " if frame is not None and str(frame.f_code.co_name) == name and "
2716  "frame.f_code.co_argcount > 0:\n"
2717  " self_caller = frame.f_locals[frame.f_code.co_varnames[0]]\n"
2718  " if self_caller == self:\n"
2719  " self = None\n",
2720  Py_file_input,
2721  d.ptr(),
2722  d.ptr());
2723  if (result == nullptr)
2724  throw error_already_set();
2725  Py_DECREF(result);
2726  if (d["self"].is_none())
2727  return function();
2728 #endif
2729 
2730  return override;
2731 }
2733 
2734 
2743 template <class T>
2744 function get_override(const T *this_ptr, const char *name) {
2745  auto *tinfo = detail::get_type_info(typeid(T));
2746  return tinfo ? detail::get_type_override(this_ptr, tinfo, name) : function();
2747 }
2748 
2749 #define PYBIND11_OVERRIDE_IMPL(ret_type, cname, name, ...) \
2750  do { \
2751  pybind11::gil_scoped_acquire gil; \
2752  pybind11::function override \
2753  = pybind11::get_override(static_cast<const cname *>(this), name); \
2754  if (override) { \
2755  auto o = override(__VA_ARGS__); \
2756  if (pybind11::detail::cast_is_temporary_value_reference<ret_type>::value) { \
2757  static pybind11::detail::override_caster_t<ret_type> caster; \
2758  return pybind11::detail::cast_ref<ret_type>(std::move(o), caster); \
2759  } \
2760  return pybind11::detail::cast_safe<ret_type>(std::move(o)); \
2761  } \
2762  } while (false)
2763 
2782 #define PYBIND11_OVERRIDE_NAME(ret_type, cname, name, fn, ...) \
2783  do { \
2784  PYBIND11_OVERRIDE_IMPL(PYBIND11_TYPE(ret_type), PYBIND11_TYPE(cname), name, __VA_ARGS__); \
2785  return cname::fn(__VA_ARGS__); \
2786  } while (false)
2787 
2792 #define PYBIND11_OVERRIDE_PURE_NAME(ret_type, cname, name, fn, ...) \
2793  do { \
2794  PYBIND11_OVERRIDE_IMPL(PYBIND11_TYPE(ret_type), PYBIND11_TYPE(cname), name, __VA_ARGS__); \
2795  pybind11::pybind11_fail( \
2796  "Tried to call pure virtual function \"" PYBIND11_STRINGIFY(cname) "::" name "\""); \
2797  } while (false)
2798 
2824 #define PYBIND11_OVERRIDE(ret_type, cname, fn, ...) \
2825  PYBIND11_OVERRIDE_NAME(PYBIND11_TYPE(ret_type), PYBIND11_TYPE(cname), #fn, fn, __VA_ARGS__)
2826 
2831 #define PYBIND11_OVERRIDE_PURE(ret_type, cname, fn, ...) \
2832  PYBIND11_OVERRIDE_PURE_NAME( \
2833  PYBIND11_TYPE(ret_type), PYBIND11_TYPE(cname), #fn, fn, __VA_ARGS__)
2834 
2835 // Deprecated versions
2836 
2837 PYBIND11_DEPRECATED("get_type_overload has been deprecated")
2838 inline function
2839 get_type_overload(const void *this_ptr, const detail::type_info *this_type, const char *name) {
2840  return detail::get_type_override(this_ptr, this_type, name);
2841 }
2842 
2843 template <class T>
2844 inline function get_overload(const T *this_ptr, const char *name) {
2845  return get_override(this_ptr, name);
2846 }
2847 
2848 #define PYBIND11_OVERLOAD_INT(ret_type, cname, name, ...) \
2849  PYBIND11_OVERRIDE_IMPL(PYBIND11_TYPE(ret_type), PYBIND11_TYPE(cname), name, __VA_ARGS__)
2850 #define PYBIND11_OVERLOAD_NAME(ret_type, cname, name, fn, ...) \
2851  PYBIND11_OVERRIDE_NAME(PYBIND11_TYPE(ret_type), PYBIND11_TYPE(cname), name, fn, __VA_ARGS__)
2852 #define PYBIND11_OVERLOAD_PURE_NAME(ret_type, cname, name, fn, ...) \
2853  PYBIND11_OVERRIDE_PURE_NAME( \
2854  PYBIND11_TYPE(ret_type), PYBIND11_TYPE(cname), name, fn, __VA_ARGS__);
2855 #define PYBIND11_OVERLOAD(ret_type, cname, fn, ...) \
2856  PYBIND11_OVERRIDE(PYBIND11_TYPE(ret_type), PYBIND11_TYPE(cname), fn, __VA_ARGS__)
2857 #define PYBIND11_OVERLOAD_PURE(ret_type, cname, fn, ...) \
2858  PYBIND11_OVERRIDE_PURE(PYBIND11_TYPE(ret_type), PYBIND11_TYPE(cname), fn, __VA_ARGS__);
2859 
2861 
2862 #if defined(__GNUC__) && __GNUC__ == 7
2863 # pragma GCC diagnostic pop // -Wnoexcept-type
2864 #endif
class_ & def_buffer(Func &&func)
Definition: pybind11.h:1620
handle scope
Python handle to the parent scope (a class or a module)
Definition: attr.h:256
typename std::conditional< B, T, F >::type conditional_t
void call_operator_delete(T *p, size_t, size_t)
Call class-specific delete if it exists or global otherwise. Can also be an overload set...
Definition: pybind11.h:1416
PYBIND11_NOINLINE detail::type_info * get_type_info(PyTypeObject *type)
const gtsam::Symbol key('X', 0)
detail::exactly_one_t< is_holder, std::unique_ptr< type >, options... > holder_type
Definition: pybind11.h:1494
Matrix3f m
detail::exactly_one_t< is_subtype, void, options... > type_alias
Definition: pybind11.h:1492
unsigned char uint8_t
Definition: ms_stdint.h:83
#define PYBIND11_ENUM_OP_STRICT(op, expr, strict_behavior)
class_ & def_buffer(Return(Class::*func)(Args...))
Definition: pybind11.h:1643
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:1738
void(* init_instance)(instance *, const void *)
Function pointer to class_<..>::init_instance.
Definition: attr.h:293
size_t type_align
What is the alignment of the underlying C++ type?
Definition: attr.h:284
PYBIND11_DEPRECATED("Use reinterpret_borrow<object>() or reinterpret_steal<object>()") object(handle h
Annotation for parent scope.
Definition: attr.h:36
#define PYBIND11_WORKAROUND_INCORRECT_MSVC_C4100(...)
void(*)(std::exception_ptr) ExceptionTranslator
Definition: internals.h:36
std::uint16_t nargs
Number of arguments (including py::args and/or py::kwargs, if present)
Definition: attr.h:243
bool first_or_done
Definition: pybind11.h:2281
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:1698
static void init_holder_from_existing(const detail::value_and_holder &v_h, const holder_type *holder_ptr, std::true_type)
Definition: pybind11.h:1810
exception< CppException > & get_exception_object()
Definition: pybind11.h:2530
static handle handle_of()
Definition: cast.h:1645
string type_name()
bool hasattr(handle obj, handle name)
Definition: pytypes.h:728
Key F(std::uint64_t j)
PYBIND11_NOINLINE void keep_alive_impl(handle nurse, handle patient)
Definition: pybind11.h:2188
iterator make_iterator_impl(Iterator &&first, Sentinel &&last, Extra &&...extra)
Definition: pybind11.h:2336
Scalar * b
Definition: benchVecAdd.cpp:17
T cast() const &
exception()=default
auto method_adaptor(F &&f) -> decltype(std::forward< F >(f))
Definition: pybind11.h:1458
void initialize(const type_record &rec)
Definition: pybind11.h:1288
PyObject * dict_getitemstring(PyObject *v, const char *key)
Definition: pytypes.h:818
#define PYBIND11_COMPAT_STRDUP
Definition: pybind11.h:75
std::forward_list< ExceptionTranslator > registered_exception_translators
Definition: internals.h:160
module_ def_submodule(const char *name, const char *doc=nullptr)
Definition: pybind11.h:1175
PyModuleDef module_def
Definition: pybind11.h:1228
PYBIND11_NOINLINE handle get_object_handle(const void *ptr, const detail::type_info *type)
std::vector< handle > args
Arguments passed to the function:
Definition: cast.h:1362
class_ & def_property_readonly_static(const char *name, const Getter &fget, const Extra &...extra)
Uses return_value_policy::reference by default.
Definition: pybind11.h:1705
#define min(a, b)
Definition: datatypes.h:19
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:1792
int RealScalar int RealScalar int RealScalar * pc
static PyObject * dispatcher(PyObject *self, PyObject *args_in, PyObject *kwargs_in)
Main dispatch logic for calls to functions bound using pybind11.
Definition: pybind11.h:662
void restore()
Definition: pytypes.h:612
cpp_function(Return(Class::*f)(Arg...) const, const Extra &...extra)
Construct a cpp_function from a class method (const, no ref-qualifier)
Definition: pybind11.h:129
PYBIND11_NOINLINE unique_function_record make_function_record()
Space optimization: don&#39;t inline this frequently instantiated fragment.
Definition: pybind11.h:161
Definition: pytypes.h:2012
Annotation for documentation.
Definition: attr.h:42
#define PYBIND11_EXPAND_SIDE_EFFECTS(PATTERN)
cpp_function(Return(Class::*f)(Arg...), const Extra &...extra)
Construct a cpp_function from a class method (non-const, no ref-qualifier)
Definition: pybind11.h:107
decltype(*std::declval< Iterator & >()) result_type
Definition: pybind11.h:2290
#define PYBIND11_SILENCE_MSVC_C4127(...)
int n
PYBIND11_NOINLINE internals & get_internals()
Return a reference to the current internals data.
Definition: internals.h:405
cpp_function(Return(Class::*f)(Arg...) const &, const Extra &...extra)
Definition: pybind11.h:141
detail::is_strict_base_of< Type, T > is_subtype
Definition: pybind11.h:1483
Scalar Scalar * c
Definition: benchVecAdd.cpp:17
std::pair< decltype(internals::registered_types_py)::iterator, bool > all_type_info_get_cache(PyTypeObject *type)
Definition: pybind11.h:2237
void *(* operator_new)(size_t)
The global operator new can be overridden with a class-specific variant.
Definition: attr.h:290
type_map< type_info * > registered_types_cpp
Definition: internals.h:152
Rot2 R(Rot2::fromAngle(0.1))
class_ & def_property(const char *name, const Getter &fget, const cpp_function &fset, const Extra &...extra)
Definition: pybind11.h:1725
enum_ & value(char const *name, Type value, const char *doc=nullptr)
Add an enumeration entry.
Definition: pybind11.h:2177
class_ & def(const char *name_, Func &&f, const Extra &...extra)
Definition: pybind11.h:1557
exception< CppException > & register_local_exception(handle scope, const char *name, handle base=PyExc_Exception)
Definition: pybind11.h:2584
cpp_function()=default
Wrapper for Python extension modules.
Definition: pybind11.h:1136
bool multiple_inheritance
Multiple inheritance marker.
Definition: attr.h:311
MatrixXd L
Definition: LLT_example.cpp:6
void raise_from(PyObject *type, const char *message)
Definition: pytypes.h:655
std::forward_list< ExceptionTranslator > registered_exception_translators
Definition: internals.h:477
Definition: BFloat16.h:88
size_t type_size
How large is the underlying C++ type?
Definition: attr.h:281
unsigned short uint16_t
Definition: ms_stdint.h:84
enum_(const handle &scope, const char *name, const Extra &...extra)
Definition: pybind11.h:2149
PYBIND11_NOINLINE void print(const tuple &args, const dict &kwargs)
Definition: pybind11.h:2589
static constexpr size_t size_in_ptrs(size_t s)
Helper class which loads arguments for C++ functions called from Python.
Definition: cast.h:1380
class_ & def_buffer(Return(Class::*func)(Args...) const)
Definition: pybind11.h:1648
Sentinel end
Definition: pybind11.h:2280
static void add_base(detail::type_record &)
Definition: pybind11.h:1554
detail::conditional_t< detail::any_of< detail::is_std_char_type< Underlying >, std::is_same< Underlying, bool > >::value, detail::equivalent_integer_t< Underlying >, Underlying > Scalar
Definition: pybind11.h:2146
T cast() const
Definition: cast.h:1071
class_ & def_readwrite(const char *name, D C::*pm, const Extra &...extra)
Definition: pybind11.h:1653
static void postcall(function_call &call, handle fn_ret)
Definition: attr.h:652
Binds C++ enumerations and enumeration classes to Python.
Definition: pybind11.h:2134
void register_exception_translator(ExceptionTranslator &&translator)
Definition: pybind11.h:2482
typename exactly_one< Predicate, Default, Ts... >::type exactly_one_t
signed char int8_t
Definition: ms_stdint.h:80
Definition: cast.h:1238
EIGEN_CONSTEXPR Index first(const T &x) EIGEN_NOEXCEPT
signed short int16_t
Definition: ms_stdint.h:81
module_ & def(const char *name_, Func &&f, const Extra &...extra)
Definition: pybind11.h:1152
Internal data associated with a single function call.
Definition: cast.h:1355
The &#39;instance&#39; type which needs to be standard layout (need to be able to use &#39;offsetof&#39;) ...
Definition: pytypes.h:1614
op_id
Enumeration with all supported operator types.
Definition: operators.h:18
function get_override(const T *this_ptr, const char *name)
Definition: pybind11.h:2744
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:1747
static const symbolic::SymbolExpr< internal::symbolic_last_tag > last
static const Similarity3 id
PYBIND11_NOINLINE void clean_type_id(std::string &name)
Definition: typeid.h:35
str enum_name(handle arg)
Definition: pybind11.h:1915
void g(const string &key, int i)
Definition: testBTree.cpp:41
constexpr size_t constexpr_sum()
Compile-time integer sum.
cpp_function(Func &&f, const Extra &...extra)
Construct a cpp_function from a lambda function (possibly with internal state)
Definition: pybind11.h:99
result_type operator()(Iterator &it) const
Definition: pybind11.h:2314
PYBIND11_NOINLINE void value(char const *name_, object value, const char *doc=nullptr)
Definition: pybind11.h:2067
const char * c_str(Args &&...args)
Definition: internals.h:524
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:1721
decltype(*std::declval< Iterator & >()) pair_type
Definition: pybind11.h:2298
PyExc_RuntimeError [[noreturn]] PYBIND11_NOINLINE void pybind11_fail(const char *reason)
Used internally.
class_ & def_readwrite_static(const char *name, D *pm, const Extra &...extra)
Definition: pybind11.h:1672
PyObject * make_new_python_type(const type_record &rec)
Definition: class.h:629
cout<< "Here is the matrix m:"<< endl<< m<< endl;Matrix< ptrdiff_t, 3, 1 > res
const handle & inc_ref() const &
Definition: pytypes.h:246
decltype(*std::declval< Iterator & >()) pair_type
Definition: pybind11.h:2320
static void init_holder_from_existing(const detail::value_and_holder &v_h, const holder_type *holder_ptr, std::false_type)
Definition: pybind11.h:1817
void add_patient(PyObject *nurse, PyObject *patient)
Definition: class.h:372
bool has_kwargs
True if the function has a &#39;**kwargs&#39; argument.
Definition: attr.h:237
bool convert
True if the argument is allowed to convert when loading.
Definition: attr.h:179
cpp_function(Return(*f)(Args...), const Extra &...extra)
Construct a cpp_function from a vanilla function pointer.
Definition: pybind11.h:90
const char * name
Argument name.
Definition: attr.h:176
conditional_t< std::is_reference< decltype(*std::declval< Iterator & >())>::value, decltype(((*std::declval< Iterator & >()).first)), decltype(std::declval< pair_type >().first)> result_type
Definition: pybind11.h:2313
handle scope
Handle to the parent scope.
Definition: attr.h:272
value_and_holder get_value_and_holder(const type_info *find_type=nullptr, bool throw_if_missing=true)
#define PYBIND11_STD_LAUNDER
Definition: pybind11.h:31
PYBIND11_NOINLINE void init(bool is_arithmetic, bool is_convertible)
Definition: pybind11.h:1928
static void add_base(detail::type_record &rec)
Definition: pybind11.h:1547
PyObject * m_ptr
Definition: pytypes.h:284
class_ & def_readonly(const char *name, const D C::*pm, const Extra &...extra)
Definition: pybind11.h:1663
PYBIND11_NOINLINE void add_object(const char *name, handle obj, bool overwrite=false)
Definition: pybind11.h:1218
Values result
void initialize(Func &&f, Return(*)(Args...), const Extra &...extra)
Special internal constructor for functors, lambda functions, etc.
Definition: pybind11.h:167
handle(* impl)(function_call &)
Pointer to lambda function which converts arguments and performs the actual call. ...
Definition: attr.h:207
detail::initimpl::pickle_factory< GetState, SetState > pickle(GetState &&g, SetState &&s)
Definition: pybind11.h:1909
exception< CppException > & register_exception(handle scope, const char *name, handle base=PyExc_Exception)
Definition: pybind11.h:2570
std::uint16_t nargs_pos_only
Number of leading arguments (counted in nargs) that are positional-only.
Definition: attr.h:250
const char * name
Name of the class.
Definition: attr.h:275
list bases
List of base classes of the newly created type.
Definition: attr.h:299
object & operator=(const object &other)
Definition: pytypes.h:336
#define PYBIND11_DESCR_CONSTEXPR
Definition: descr.h:18
Definition: pytypes.h:1403
cpp_function(Return(Class::*f)(Arg...) &, const Extra &...extra)
Definition: pybind11.h:119
void def_property_static_impl(const char *name, handle fget, handle fset, detail::function_record *rec_func)
Definition: pybind11.h:1376
static void init_instance(detail::instance *inst, const void *holder_ptr)
Definition: pybind11.h:1844
std::vector< bool > args_convert
The convert value the arguments should be loaded with.
Definition: cast.h:1365
object name() const
Return the function name.
Definition: pybind11.h:149
std::unordered_set< std::pair< const PyObject *, const char * >, override_hash > inactive_override_cache
Definition: internals.h:157
std::string get_fully_qualified_tp_name(PyTypeObject *type)
Definition: class.h:28
signed __int64 int64_t
Definition: ms_stdint.h:94
PYBIND11_NOINLINE void export_values()
Definition: pybind11.h:2080
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const ArgReturnType arg() const
std::uint16_t nargs_pos
Definition: attr.h:247
type_map< std::vector< bool(*)(PyObject *, void *&)> > direct_conversions
Definition: internals.h:158
local_internals & get_local_internals()
Works like get_internals, but for things which are locally registered.
Definition: internals.h:514
type_map< type_info * > registered_types_cpp
Definition: internals.h:476
class_ & def(detail::initimpl::factory< Args... > &&init, const Extra &...extra)
Definition: pybind11.h:1608
unsigned __int64 uint64_t
Definition: ms_stdint.h:95
unsigned int uint32_t
Definition: ms_stdint.h:85
static void precall(function_call &call)
Definition: attr.h:646
EIGEN_DEVICE_FUNC NewType cast(const OldType &x)
Eigen::Triplet< double > T
signed int int32_t
Definition: ms_stdint.h:82
#define PYBIND11_OBJECT_DEFAULT(Name, Parent, CheckFun)
Definition: pytypes.h:1290
int data[]
static detail::function_record * get_function_record(handle h)
Definition: pybind11.h:1872
detail::is_holder_type< Type, T > is_holder
Definition: pybind11.h:1481
bool is_cpp_function() const
Definition: pytypes.h:2060
Point2(* f)(const Point3 &, OptionalJacobian< 2, 3 >)
bool is_method
True if this is a method.
Definition: attr.h:231
static return_value_policy policy(return_value_policy p)
Definition: cast.h:995
bool is_constructor
True if name == &#39;init&#39;.
Definition: attr.h:219
class_(handle scope, const char *name, const Extra &...extra)
Definition: pybind11.h:1505
enum_base(const handle &base, const handle &parent)
Definition: pybind11.h:1926
Array< double, 1, 3 > e(1./3., 0.5, 2.)
RealScalar s
typename std::remove_reference< T >::type remove_reference_t
bool module_local
Is the class definition local to the module shared object?
Definition: attr.h:323
function get_type_override(const void *this_ptr, const type_info *this_type, const char *name)
Definition: pybind11.h:2644
static bool show_function_signatures()
Definition: options.h:56
object()=default
typename void_t_impl< Ts... >::type void_t
const handle & dec_ref() const &
Definition: pytypes.h:259
#define PYBIND11_THROW
void reload()
Reload the module or throws error_already_set.
Definition: pybind11.h:1203
void mark_parents_nonsimple(PyTypeObject *value)
Helper function which tags all parents of a type using mult. inheritance.
Definition: pybind11.h:1348
Helper type to replace &#39;void&#39; in some expressions.
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:1712
function get_type_overload(const void *this_ptr, const detail::type_info *this_type, const char *name)
Definition: pybind11.h:2839
handle get_function(handle value)
Definition: pytypes.h:803
PyObject * dict_getitem(PyObject *v, PyObject *key)
Definition: pytypes.h:833
void operator()(detail::function_record *rec)
Definition: pybind11.h:155
conditional_t< std::is_reference< decltype(*std::declval< Iterator & >())>::value, decltype(((*std::declval< Iterator & >()).second)), decltype(std::declval< pair_type >().second)> result_type
Definition: pybind11.h:2326
#define PYBIND11_ENUM_OP_CONV(op, expr)
typename equivalent_integer< std::is_signed< IntLike >::value, sizeof(IntLike)>::type equivalent_integer_t
Definition: pybind11.h:2128
DenseIndex ret
Matrix< Scalar, Dynamic, Dynamic > C
Definition: bench_gemm.cpp:50
void add_class_method(object &cls, const char *name_, const cpp_function &cf)
Definition: pybind11.h:1446
static void destruct(detail::function_record *rec, bool free_strings=true)
When a cpp_function is GCed, release any memory allocated by pybind11.
Definition: pybind11.h:616
#define PYBIND11_MODULE_LOCAL_ID
Definition: internals.h:285
std::is_same< bools< Ts::value..., true >, bools< true, Ts::value... > > all_of
bool none
True if None is allowed when loading.
Definition: attr.h:180
class_ & def_cast(const detail::op_< id, ot, L, R > &op, const Extra &...extra)
Definition: pybind11.h:1588
static void init(const Args &...args, function_record *r)
Definition: attr.h:632
bool apply_exception_translators(std::forward_list< ExceptionTranslator > &translators)
Definition: pybind11.h:58
handle m_base
Definition: pybind11.h:2087
const double h
function_record * next
Pointer to next overload.
Definition: attr.h:262
void(* dealloc)(detail::value_and_holder &)
Function pointer to class_<..>::dealloc.
Definition: attr.h:296
detail::type_info * get_global_type_info(const std::type_index &tp)
int func(const int &a)
Definition: testDSF.cpp:221
enum_ & export_values()
Export enumeration entries into the parent scope.
Definition: pybind11.h:2171
handle release()
Definition: pytypes.h:330
def doc()
Definition: conftest.py:157
Definition: pytypes.h:1663
typename std::underlying_type< Type >::type Underlying
Definition: pybind11.h:2141
static void init_holder(detail::instance *inst, detail::value_and_holder &v_h, const holder_type *holder_ptr, const void *)
Definition: pybind11.h:1826
void set_operator_new(type_record *r)
Set the pointer to operator new if it exists. The cast is needed because it can be overloaded...
Definition: pybind11.h:1395
bool empty() const
Definition: pytypes.h:1940
std::vector< char * > strings
Definition: pybind11.h:332
size_t holder_size
How large is the type&#39;s holder?
Definition: attr.h:287
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:666
constexpr bool args_are_all_keyword_or_ds()
Definition: pytypes.h:1920
Special data structure which (temporarily) holds metadata about a bound class.
Definition: attr.h:266
static module_ import(const char *name)
Import and return a module or throws error_already_set.
Definition: pybind11.h:1194
Annotation for methods.
Definition: attr.h:24
const std::vector< detail::type_info * > & all_type_info(PyTypeObject *type)
bool default_holder
Is the default (unique_ptr) holder type used?
Definition: attr.h:320
object getattr(handle obj, handle name)
Definition: pytypes.h:748
detail::type_info * get_local_type_info(const std::type_index &tp)
handle m_parent
Definition: pybind11.h:2088
RAII wrapper that temporarily clears any Python error state.
std::is_same< intrinsic_t< T >, kw_only > is_kw_only
Definition: cast.h:1347
detail::is_strict_base_of< T, Type > is_base
Definition: pybind11.h:1485
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:1757
detail::initimpl::constructor< Args... > init()
Binds an existing constructor taking arguments Args...
Definition: pybind11.h:1882
Iterator it
Definition: pybind11.h:2279
static EIGEN_DEPRECATED const end_t end
static void dealloc(detail::value_and_holder &v_h)
Deallocates an instance; via holder, if constructed; otherwise via operator delete.
Definition: pybind11.h:1854
std::vector< argument_record > args
List of registered keyword arguments.
Definition: attr.h:204
const std::type_info * type
Definition: attr.h:278
class_ & def(const detail::initimpl::constructor< Args... > &init, const Extra &...extra)
Definition: pybind11.h:1594
Wraps an arbitrary C++ function/method/lambda function/.. into a callable Python object.
Definition: pybind11.h:81
class_ & def_static(const char *name_, Func &&f, const Extra &...extra)
Definition: pybind11.h:1568
float * p
object args_ref
Definition: cast.h:1369
exception< CppException > & register_exception_impl(handle scope, const char *name, handle base, bool isLocal)
Definition: pybind11.h:2538
void implicitly_convertible()
Definition: pybind11.h:2451
char * name
Function name.
Definition: attr.h:195
bool has_args
True if the function has a &#39;*args&#39; argument.
Definition: attr.h:234
object kwargs_ref
Definition: cast.h:1369
bool is_operator
True if this is an operator (add), etc.
Definition: attr.h:228
class_ & def(detail::initimpl::pickle_factory< Args... > &&pf, const Extra &...extra)
Definition: pybind11.h:1614
result_type operator()(Iterator &it) const
Definition: pybind11.h:2292
static std::shared_ptr< T > try_get_shared_from_this(std::enable_shared_from_this< T > *holder_value_ptr)
static bool show_user_defined_docstrings()
Definition: options.h:52
static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE typename ::Eigen::internal::enable_if< dt !=data_source::global_mem, void >::type write(PacketType &packet_data, DataScalar ptr)
write, a template function used for storing the data to local memory. This function is used to guaran...
static PYBIND11_NOINLINE void * local_load(PyObject *src, const type_info *ti)
iterator make_key_iterator(Iterator &&first, Sentinel &&last, Extra &&...extra)
Definition: pybind11.h:2391
typename std::enable_if< B, T >::type enable_if_t
from cpp_future import (convenient aliases from C++14/17)
result_type operator()(Iterator &it) const
Definition: pybind11.h:2327
iterator make_iterator(Iterator &&first, Sentinel &&last, Extra &&...extra)
Makes a python iterator from a first and past-the-end C++ InputIterator.
Definition: pybind11.h:2373
#define PYBIND11_ENUM_OP_CONV_LHS(op, expr)
static module_ create_extension_module(const char *name, const char *doc, module_def *def)
Definition: pybind11.h:1235
Definition: pytypes.h:1924
#define PYBIND11_INSTANCE_METHOD_NEW(ptr, class_)
Include Python header, disable linking to pythonX_d.lib on Windows in debug mode. ...
iterator make_value_iterator(Iterator &&first, Sentinel &&last, Extra &&...extra)
Definition: pybind11.h:2409
bool is_new_style_constructor
True if this is a new-style __init__ defined in detail/init.h
Definition: attr.h:222
Annotation for function names.
Definition: attr.h:48
const char * name
If non-null, this is a named kwargs argument.
Definition: cast.h:1257
Annotation indicating that a class derives from another given type.
Definition: attr.h:61
str repr(handle h)
Definition: pytypes.h:2265
std::unique_ptr< detail::function_record, InitializingFunctionRecordDeleter > unique_function_record
Definition: pybind11.h:158
Information record describing a Python buffer object.
Definition: buffer_info.h:43
PyObject * ptr() const
Return the underlying PyObject * pointer.
Definition: pytypes.h:238
function get_overload(const T *this_ptr, const char *name)
Definition: pybind11.h:2844
def capture(capsys)
Definition: conftest.py:118
detail::enum_base m_base
Definition: pybind11.h:2183
handle init_self
If this is a call to an initializer, this argument contains self
Definition: cast.h:1375
std::unordered_map< PyTypeObject *, std::vector< type_info * > > registered_types_py
Definition: internals.h:154
void initialize_generic(unique_function_record &&unique_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:336
bool equal(const T &obj1, const T &obj2, double tol)
Definition: Testable.h:85
constexpr descr< N - 1 > const_name(char const (&text)[N])
Definition: descr.h:60
void register_instance(instance *self, void *valptr, const type_info *tinfo)
Definition: class.h:321
class_ & def_property_readonly(const char *name, const Getter &fget, const Extra &...extra)
Uses return_value_policy::reference_internal by default.
Definition: pybind11.h:1688
void setattr(handle obj, handle name, handle value)
Definition: pytypes.h:780
static void m_fetched_error_deleter(detail::error_fetch_and_normalize *raw_ptr)
Definition: pybind11.h:2629
Annotation indicating that a function is an overload associated with a given "sibling".
Definition: attr.h:54
detail::initimpl::alias_constructor< Args... > init_alias()
Definition: pybind11.h:1888
dict globals()
Definition: pybind11.h:1270
exception(handle scope, const char *name, handle base=PyExc_Exception)
Definition: pybind11.h:2509
class_ & def(const detail::initimpl::alias_constructor< Args... > &init, const Extra &...extra)
Definition: pybind11.h:1601
class_ & def(const detail::op_< id, ot, L, R > &op, const Extra &...extra)
Definition: pybind11.h:1582
void register_local_exception_translator(ExceptionTranslator &&translator)
Definition: pybind11.h:2493
bool contains(T &&key) const
Definition: pytypes.h:1945
return_value_policy
Approach used to cast a previously unknown C++ instance into a Python object.
class_ & def_readonly_static(const char *name, const D *pm, const Extra &...extra)
Definition: pybind11.h:1680
void operator()(const char *message)
Definition: pybind11.h:2522
op_type
Definition: operators.h:65
handle()=default
The default constructor creates a handle with a nullptr-valued pointer.
negation< all_of< negation< Ts >... > > any_of
Internal data structure which holds metadata about a keyword argument.
Definition: attr.h:175
#define PYBIND11_NAMESPACE_END(name)
#define PYBIND11_OBJECT(Name, Parent, CheckFun)
Definition: pytypes.h:1276
std::is_base_of< pyobject_tag, remove_reference_t< T > > is_pyobject
Definition: pytypes.h:70
cpp_function(std::nullptr_t)
Definition: pybind11.h:85
Point2 t(10, 10)
Definition: pytypes.h:1370
#define PYBIND11_NAMESPACE_BEGIN(name)
std::is_same< intrinsic_t< T >, pos_only > is_pos_only
Definition: cast.h:1349
const char * what() const noexcept override
Definition: pybind11.h:2635
char * operator()(const char *s)
Definition: pybind11.h:324
std::string sep
Definition: IOFormat.cpp:1
void install_buffer_funcs(buffer_info *(*get_buffer)(PyObject *, void *), void *get_buffer_data)
Definition: pybind11.h:1359


gtsam
Author(s):
autogenerated on Tue Jul 4 2023 02:35:26