cast.h
Go to the documentation of this file.
1 /*
2  pybind11/cast.h: Partial template specializations to cast between
3  C++ and Python types
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/common.h"
14 #include "detail/descr.h"
16 #include "detail/typeid.h"
17 #include "pytypes.h"
18 
19 #include <array>
20 #include <cstring>
21 #include <functional>
22 #include <iosfwd>
23 #include <iterator>
24 #include <memory>
25 #include <string>
26 #include <tuple>
27 #include <type_traits>
28 #include <utility>
29 #include <vector>
30 
32 
34 
36 
37 template <typename type, typename SFINAE = void>
38 class type_caster : public type_caster_base<type> {};
39 template <typename type>
41 
42 // Shortcut for calling a caster's `cast_op_type` cast operator for casting a type_caster to a T
43 template <typename T>
45  return caster.operator typename make_caster<T>::template cast_op_type<T>();
46 }
47 template <typename T>
50  return std::move(caster).operator typename make_caster<T>::
52 }
53 
54 template <typename type>
55 class type_caster<std::reference_wrapper<type>> {
56 private:
59  using reference_t = type &;
60  using subcaster_cast_op_type = typename caster_t::template cast_op_type<reference_t>;
61 
62  static_assert(
65  "std::reference_wrapper<T> caster requires T to have a caster with an "
66  "`operator T &()` or `operator const T &()`");
67 
68 public:
69  bool load(handle src, bool convert) { return subcaster.load(src, convert); }
70  static constexpr auto name = caster_t::name;
71  static handle
72  cast(const std::reference_wrapper<type> &src, return_value_policy policy, handle parent) {
73  // It is definitely wrong to take ownership of this pointer, so mask that rvp
75  || policy == return_value_policy::automatic) {
77  }
78  return caster_t::cast(&src.get(), policy, parent);
79  }
80  template <typename T>
81  using cast_op_type = std::reference_wrapper<type>;
82  explicit operator std::reference_wrapper<type>() { return cast_op<type &>(subcaster); }
83 };
84 
85 #define PYBIND11_TYPE_CASTER(type, py_name) \
86 protected: \
87  type value; \
88  \
89 public: \
90  static constexpr auto name = py_name; \
91  template <typename T_, \
92  ::pybind11::detail::enable_if_t< \
93  std::is_same<type, ::pybind11::detail::remove_cv_t<T_>>::value, \
94  int> \
95  = 0> \
96  static ::pybind11::handle cast( \
97  T_ *src, ::pybind11::return_value_policy policy, ::pybind11::handle parent) { \
98  if (!src) \
99  return ::pybind11::none().release(); \
100  if (policy == ::pybind11::return_value_policy::take_ownership) { \
101  auto h = cast(std::move(*src), policy, parent); \
102  delete src; \
103  return h; \
104  } \
105  return cast(*src, policy, parent); \
106  } \
107  operator type *() { return &value; } /* NOLINT(bugprone-macro-parentheses) */ \
108  operator type &() { return value; } /* NOLINT(bugprone-macro-parentheses) */ \
109  operator type &&() && { return std::move(value); } /* NOLINT(bugprone-macro-parentheses) */ \
110  template <typename T_> \
111  using cast_op_type = ::pybind11::detail::movable_cast_op_type<T_>
112 
113 template <typename CharT>
114 using is_std_char_type = any_of<std::is_same<CharT, char>, /* std::string */
115 #if defined(PYBIND11_HAS_U8STRING)
116  std::is_same<CharT, char8_t>, /* std::u8string */
117 #endif
118  std::is_same<CharT, char16_t>, /* std::u16string */
119  std::is_same<CharT, char32_t>, /* std::u32string */
120  std::is_same<CharT, wchar_t> /* std::wstring */
121  >;
122 
123 template <typename T>
124 struct type_caster<T, enable_if_t<std::is_arithmetic<T>::value && !is_std_char_type<T>::value>> {
125  using _py_type_0 = conditional_t<sizeof(T) <= sizeof(long), long, long long>;
127  _py_type_0,
130 
131 public:
132  bool load(handle src, bool convert) {
133  py_type py_value;
134 
135  if (!src) {
136  return false;
137  }
138 
139 #if !defined(PYPY_VERSION)
140  auto index_check = [](PyObject *o) { return PyIndex_Check(o); };
141 #else
142  // In PyPy 7.3.3, `PyIndex_Check` is implemented by calling `__index__`,
143  // while CPython only considers the existence of `nb_index`/`__index__`.
144  auto index_check = [](PyObject *o) { return hasattr(o, "__index__"); };
145 #endif
146 
148  if (convert || PyFloat_Check(src.ptr())) {
149  py_value = (py_type) PyFloat_AsDouble(src.ptr());
150  } else {
151  return false;
152  }
153  } else if (PyFloat_Check(src.ptr())
154  || (!convert && !PYBIND11_LONG_CHECK(src.ptr()) && !index_check(src.ptr()))) {
155  return false;
156  } else {
157  handle src_or_index = src;
158  // PyPy: 7.3.7's 3.8 does not implement PyLong_*'s __index__ calls.
159 #if PY_VERSION_HEX < 0x03080000 || defined(PYPY_VERSION)
160  object index;
161  if (!PYBIND11_LONG_CHECK(src.ptr())) { // So: index_check(src.ptr())
162  index = reinterpret_steal<object>(PyNumber_Index(src.ptr()));
163  if (!index) {
164  PyErr_Clear();
165  if (!convert)
166  return false;
167  } else {
168  src_or_index = index;
169  }
170  }
171 #endif
173  py_value = as_unsigned<py_type>(src_or_index.ptr());
174  } else { // signed integer:
175  py_value = sizeof(T) <= sizeof(long)
176  ? (py_type) PyLong_AsLong(src_or_index.ptr())
177  : (py_type) PYBIND11_LONG_AS_LONGLONG(src_or_index.ptr());
178  }
179  }
180 
181  // Python API reported an error
182  bool py_err = py_value == (py_type) -1 && PyErr_Occurred();
183 
184  // Check to see if the conversion is valid (integers should match exactly)
185  // Signed/unsigned checks happen elsewhere
186  if (py_err
187  || (std::is_integral<T>::value && sizeof(py_type) != sizeof(T)
188  && py_value != (py_type) (T) py_value)) {
189  PyErr_Clear();
190  if (py_err && convert && (PyNumber_Check(src.ptr()) != 0)) {
191  auto tmp = reinterpret_steal<object>(std::is_floating_point<T>::value
192  ? PyNumber_Float(src.ptr())
193  : PyNumber_Long(src.ptr()));
194  PyErr_Clear();
195  return load(tmp, false);
196  }
197  return false;
198  }
199 
200  value = (T) py_value;
201  return true;
202  }
203 
204  template <typename U = T>
206  cast(U src, return_value_policy /* policy */, handle /* parent */) {
207  return PyFloat_FromDouble((double) src);
208  }
209 
210  template <typename U = T>
212  && (sizeof(U) <= sizeof(long)),
213  handle>::type
214  cast(U src, return_value_policy /* policy */, handle /* parent */) {
215  return PYBIND11_LONG_FROM_SIGNED((long) src);
216  }
217 
218  template <typename U = T>
220  && (sizeof(U) <= sizeof(unsigned long)),
221  handle>::type
222  cast(U src, return_value_policy /* policy */, handle /* parent */) {
223  return PYBIND11_LONG_FROM_UNSIGNED((unsigned long) src);
224  }
225 
226  template <typename U = T>
228  && (sizeof(U) > sizeof(long)),
229  handle>::type
230  cast(U src, return_value_policy /* policy */, handle /* parent */) {
231  return PyLong_FromLongLong((long long) src);
232  }
233 
234  template <typename U = T>
236  && (sizeof(U) > sizeof(unsigned long)),
237  handle>::type
238  cast(U src, return_value_policy /* policy */, handle /* parent */) {
239  return PyLong_FromUnsignedLongLong((unsigned long long) src);
240  }
241 
243 };
244 
245 template <typename T>
246 struct void_caster {
247 public:
248  bool load(handle src, bool) {
249  if (src && src.is_none()) {
250  return true;
251  }
252  return false;
253  }
254  static handle cast(T, return_value_policy /* policy */, handle /* parent */) {
255  return none().release();
256  }
258 };
259 
260 template <>
261 class type_caster<void_type> : public void_caster<void_type> {};
262 
263 template <>
264 class type_caster<void> : public type_caster<void_type> {
265 public:
267 
268  bool load(handle h, bool) {
269  if (!h) {
270  return false;
271  }
272  if (h.is_none()) {
273  value = nullptr;
274  return true;
275  }
276 
277  /* Check if this is a capsule */
278  if (isinstance<capsule>(h)) {
279  value = reinterpret_borrow<capsule>(h);
280  return true;
281  }
282 
283  /* Check if this is a C++ type */
284  const auto &bases = all_type_info((PyTypeObject *) type::handle_of(h).ptr());
285  if (bases.size() == 1) { // Only allowing loading from a single-value type
286  value = values_and_holders(reinterpret_cast<instance *>(h.ptr())).begin()->value_ptr();
287  return true;
288  }
289 
290  /* Fail */
291  return false;
292  }
293 
294  static handle cast(const void *ptr, return_value_policy /* policy */, handle /* parent */) {
295  if (ptr) {
296  return capsule(ptr).release();
297  }
298  return none().release();
299  }
300 
301  template <typename T>
302  using cast_op_type = void *&;
303  explicit operator void *&() { return value; }
304  static constexpr auto name = const_name("capsule");
305 
306 private:
307  void *value = nullptr;
308 };
309 
310 template <>
311 class type_caster<std::nullptr_t> : public void_caster<std::nullptr_t> {};
312 
313 template <>
314 class type_caster<bool> {
315 public:
316  bool load(handle src, bool convert) {
317  if (!src) {
318  return false;
319  }
320  if (src.ptr() == Py_True) {
321  value = true;
322  return true;
323  }
324  if (src.ptr() == Py_False) {
325  value = false;
326  return true;
327  }
328  if (convert || (std::strcmp("numpy.bool_", Py_TYPE(src.ptr())->tp_name) == 0)) {
329  // (allow non-implicit conversion for numpy booleans)
330 
331  Py_ssize_t res = -1;
332  if (src.is_none()) {
333  res = 0; // None is implicitly converted to False
334  }
335 #if defined(PYPY_VERSION)
336  // On PyPy, check that "__bool__" attr exists
337  else if (hasattr(src, PYBIND11_BOOL_ATTR)) {
338  res = PyObject_IsTrue(src.ptr());
339  }
340 #else
341  // Alternate approach for CPython: this does the same as the above, but optimized
342  // using the CPython API so as to avoid an unneeded attribute lookup.
343  else if (auto *tp_as_number = src.ptr()->ob_type->tp_as_number) {
344  if (PYBIND11_NB_BOOL(tp_as_number)) {
345  res = (*PYBIND11_NB_BOOL(tp_as_number))(src.ptr());
346  }
347  }
348 #endif
349  if (res == 0 || res == 1) {
350  value = (res != 0);
351  return true;
352  }
353  PyErr_Clear();
354  }
355  return false;
356  }
357  static handle cast(bool src, return_value_policy /* policy */, handle /* parent */) {
358  return handle(src ? Py_True : Py_False).inc_ref();
359  }
360  PYBIND11_TYPE_CASTER(bool, const_name("bool"));
361 };
362 
363 // Helper class for UTF-{8,16,32} C++ stl strings:
364 template <typename StringType, bool IsView = false>
366  using CharT = typename StringType::value_type;
367 
368  // Simplify life by being able to assume standard char sizes (the standard only guarantees
369  // minimums, but Python requires exact sizes)
370  static_assert(!std::is_same<CharT, char>::value || sizeof(CharT) == 1,
371  "Unsupported char size != 1");
372 #if defined(PYBIND11_HAS_U8STRING)
373  static_assert(!std::is_same<CharT, char8_t>::value || sizeof(CharT) == 1,
374  "Unsupported char8_t size != 1");
375 #endif
376  static_assert(!std::is_same<CharT, char16_t>::value || sizeof(CharT) == 2,
377  "Unsupported char16_t size != 2");
378  static_assert(!std::is_same<CharT, char32_t>::value || sizeof(CharT) == 4,
379  "Unsupported char32_t size != 4");
380  // wchar_t can be either 16 bits (Windows) or 32 (everywhere else)
381  static_assert(!std::is_same<CharT, wchar_t>::value || sizeof(CharT) == 2 || sizeof(CharT) == 4,
382  "Unsupported wchar_t size != 2/4");
383  static constexpr size_t UTF_N = 8 * sizeof(CharT);
384 
385  bool load(handle src, bool) {
386  handle load_src = src;
387  if (!src) {
388  return false;
389  }
390  if (!PyUnicode_Check(load_src.ptr())) {
391  return load_raw(load_src);
392  }
393 
394  // For UTF-8 we avoid the need for a temporary `bytes` object by using
395  // `PyUnicode_AsUTF8AndSize`.
396  if (UTF_N == 8) {
397  Py_ssize_t size = -1;
398  const auto *buffer
399  = reinterpret_cast<const CharT *>(PyUnicode_AsUTF8AndSize(load_src.ptr(), &size));
400  if (!buffer) {
401  PyErr_Clear();
402  return false;
403  }
404  value = StringType(buffer, static_cast<size_t>(size));
405  return true;
406  }
407 
408  auto utfNbytes
409  = reinterpret_steal<object>(PyUnicode_AsEncodedString(load_src.ptr(),
410  UTF_N == 8 ? "utf-8"
411  : UTF_N == 16 ? "utf-16"
412  : "utf-32",
413  nullptr));
414  if (!utfNbytes) {
415  PyErr_Clear();
416  return false;
417  }
418 
419  const auto *buffer
420  = reinterpret_cast<const CharT *>(PYBIND11_BYTES_AS_STRING(utfNbytes.ptr()));
421  size_t length = (size_t) PYBIND11_BYTES_SIZE(utfNbytes.ptr()) / sizeof(CharT);
422  // Skip BOM for UTF-16/32
423  if (UTF_N > 8) {
424  buffer++;
425  length--;
426  }
427  value = StringType(buffer, length);
428 
429  // If we're loading a string_view we need to keep the encoded Python object alive:
430  if (IsView) {
432  }
433 
434  return true;
435  }
436 
437  static handle
438  cast(const StringType &src, return_value_policy /* policy */, handle /* parent */) {
439  const char *buffer = reinterpret_cast<const char *>(src.data());
440  auto nbytes = ssize_t(src.size() * sizeof(CharT));
441  handle s = decode_utfN(buffer, nbytes);
442  if (!s) {
443  throw error_already_set();
444  }
445  return s;
446  }
447 
449 
450 private:
451  static handle decode_utfN(const char *buffer, ssize_t nbytes) {
452 #if !defined(PYPY_VERSION)
453  return UTF_N == 8 ? PyUnicode_DecodeUTF8(buffer, nbytes, nullptr)
454  : UTF_N == 16 ? PyUnicode_DecodeUTF16(buffer, nbytes, nullptr, nullptr)
455  : PyUnicode_DecodeUTF32(buffer, nbytes, nullptr, nullptr);
456 #else
457  // PyPy segfaults when on PyUnicode_DecodeUTF16 (and possibly on PyUnicode_DecodeUTF32 as
458  // well), so bypass the whole thing by just passing the encoding as a string value, which
459  // works properly:
460  return PyUnicode_Decode(buffer,
461  nbytes,
462  UTF_N == 8 ? "utf-8"
463  : UTF_N == 16 ? "utf-16"
464  : "utf-32",
465  nullptr);
466 #endif
467  }
468 
469  // When loading into a std::string or char*, accept a bytes/bytearray object as-is (i.e.
470  // without any encoding/decoding attempt). For other C++ char sizes this is a no-op.
471  // which supports loading a unicode from a str, doesn't take this path.
472  template <typename C = CharT>
474  if (PYBIND11_BYTES_CHECK(src.ptr())) {
475  // We were passed raw bytes; accept it into a std::string or char*
476  // without any encoding attempt.
477  const char *bytes = PYBIND11_BYTES_AS_STRING(src.ptr());
478  if (!bytes) {
479  pybind11_fail("Unexpected PYBIND11_BYTES_AS_STRING() failure.");
480  }
481  value = StringType(bytes, (size_t) PYBIND11_BYTES_SIZE(src.ptr()));
482  return true;
483  }
484  if (PyByteArray_Check(src.ptr())) {
485  // We were passed a bytearray; accept it into a std::string or char*
486  // without any encoding attempt.
487  const char *bytearray = PyByteArray_AsString(src.ptr());
488  if (!bytearray) {
489  pybind11_fail("Unexpected PyByteArray_AsString() failure.");
490  }
491  value = StringType(bytearray, (size_t) PyByteArray_Size(src.ptr()));
492  return true;
493  }
494 
495  return false;
496  }
497 
498  template <typename C = CharT>
500  return false;
501  }
502 };
503 
504 template <typename CharT, class Traits, class Allocator>
505 struct type_caster<std::basic_string<CharT, Traits, Allocator>,
506  enable_if_t<is_std_char_type<CharT>::value>>
507  : string_caster<std::basic_string<CharT, Traits, Allocator>> {};
508 
509 #ifdef PYBIND11_HAS_STRING_VIEW
510 template <typename CharT, class Traits>
511 struct type_caster<std::basic_string_view<CharT, Traits>,
512  enable_if_t<is_std_char_type<CharT>::value>>
513  : string_caster<std::basic_string_view<CharT, Traits>, true> {};
514 #endif
515 
516 // Type caster for C-style strings. We basically use a std::string type caster, but also add the
517 // ability to use None as a nullptr char* (which the string caster doesn't allow).
518 template <typename CharT>
520  using StringType = std::basic_string<CharT>;
523  bool none = false;
524  CharT one_char = 0;
525 
526 public:
527  bool load(handle src, bool convert) {
528  if (!src) {
529  return false;
530  }
531  if (src.is_none()) {
532  // Defer accepting None to other overloads (if we aren't in convert mode):
533  if (!convert) {
534  return false;
535  }
536  none = true;
537  return true;
538  }
539  return str_caster.load(src, convert);
540  }
541 
542  static handle cast(const CharT *src, return_value_policy policy, handle parent) {
543  if (src == nullptr) {
544  return pybind11::none().release();
545  }
546  return StringCaster::cast(StringType(src), policy, parent);
547  }
548 
549  static handle cast(CharT src, return_value_policy policy, handle parent) {
551  handle s = PyUnicode_DecodeLatin1((const char *) &src, 1, nullptr);
552  if (!s) {
553  throw error_already_set();
554  }
555  return s;
556  }
557  return StringCaster::cast(StringType(1, src), policy, parent);
558  }
559 
560  explicit operator CharT *() {
561  return none ? nullptr : const_cast<CharT *>(static_cast<StringType &>(str_caster).c_str());
562  }
563  explicit operator CharT &() {
564  if (none) {
565  throw value_error("Cannot convert None to a character");
566  }
567 
568  auto &value = static_cast<StringType &>(str_caster);
569  size_t str_len = value.size();
570  if (str_len == 0) {
571  throw value_error("Cannot convert empty string to a character");
572  }
573 
574  // If we're in UTF-8 mode, we have two possible failures: one for a unicode character that
575  // is too high, and one for multiple unicode characters (caught later), so we need to
576  // figure out how long the first encoded character is in bytes to distinguish between these
577  // two errors. We also allow want to allow unicode characters U+0080 through U+00FF, as
578  // those can fit into a single char value.
579  if (StringCaster::UTF_N == 8 && str_len > 1 && str_len <= 4) {
580  auto v0 = static_cast<unsigned char>(value[0]);
581  // low bits only: 0-127
582  // 0b110xxxxx - start of 2-byte sequence
583  // 0b1110xxxx - start of 3-byte sequence
584  // 0b11110xxx - start of 4-byte sequence
585  size_t char0_bytes = (v0 & 0x80) == 0 ? 1
586  : (v0 & 0xE0) == 0xC0 ? 2
587  : (v0 & 0xF0) == 0xE0 ? 3
588  : 4;
589 
590  if (char0_bytes == str_len) {
591  // If we have a 128-255 value, we can decode it into a single char:
592  if (char0_bytes == 2 && (v0 & 0xFC) == 0xC0) { // 0x110000xx 0x10xxxxxx
593  one_char = static_cast<CharT>(((v0 & 3) << 6)
594  + (static_cast<unsigned char>(value[1]) & 0x3F));
595  return one_char;
596  }
597  // Otherwise we have a single character, but it's > U+00FF
598  throw value_error("Character code point not in range(0x100)");
599  }
600  }
601 
602  // UTF-16 is much easier: we can only have a surrogate pair for values above U+FFFF, thus a
603  // surrogate pair with total length 2 instantly indicates a range error (but not a "your
604  // string was too long" error).
605  else if (StringCaster::UTF_N == 16 && str_len == 2) {
606  one_char = static_cast<CharT>(value[0]);
607  if (one_char >= 0xD800 && one_char < 0xE000) {
608  throw value_error("Character code point not in range(0x10000)");
609  }
610  }
611 
612  if (str_len != 1) {
613  throw value_error("Expected a character, but multi-character string found");
614  }
615 
616  one_char = value[0];
617  return one_char;
618  }
619 
620  static constexpr auto name = const_name(PYBIND11_STRING_NAME);
621  template <typename _T>
622  using cast_op_type = pybind11::detail::cast_op_type<_T>;
623 };
624 
625 // Base implementation for std::tuple and std::pair
626 template <template <typename...> class Tuple, typename... Ts>
628  using type = Tuple<Ts...>;
629  static constexpr auto size = sizeof...(Ts);
631 
632 public:
633  bool load(handle src, bool convert) {
634  if (!isinstance<sequence>(src)) {
635  return false;
636  }
637  const auto seq = reinterpret_borrow<sequence>(src);
638  if (seq.size() != size) {
639  return false;
640  }
641  return load_impl(seq, convert, indices{});
642  }
643 
644  template <typename T>
645  static handle cast(T &&src, return_value_policy policy, handle parent) {
646  return cast_impl(std::forward<T>(src), policy, parent, indices{});
647  }
648 
649  // copied from the PYBIND11_TYPE_CASTER macro
650  template <typename T>
651  static handle cast(T *src, return_value_policy policy, handle parent) {
652  if (!src) {
653  return none().release();
654  }
655  if (policy == return_value_policy::take_ownership) {
656  auto h = cast(std::move(*src), policy, parent);
657  delete src;
658  return h;
659  }
660  return cast(*src, policy, parent);
661  }
662 
663  static constexpr auto name
665 
666  template <typename T>
668 
669  explicit operator type() & { return implicit_cast(indices{}); }
670  explicit operator type() && { return std::move(*this).implicit_cast(indices{}); }
671 
672 protected:
673  template <size_t... Is>
675  return type(cast_op<Ts>(std::get<Is>(subcasters))...);
676  }
677  template <size_t... Is>
679  return type(cast_op<Ts>(std::move(std::get<Is>(subcasters)))...);
680  }
681 
682  static constexpr bool load_impl(const sequence &, bool, index_sequence<>) { return true; }
683 
684  template <size_t... Is>
686 #ifdef __cpp_fold_expressions
687  if ((... || !std::get<Is>(subcasters).load(seq[Is], convert))) {
688  return false;
689  }
690 #else
691  for (bool r : {std::get<Is>(subcasters).load(seq[Is], convert)...}) {
692  if (!r) {
693  return false;
694  }
695  }
696 #endif
697  return true;
698  }
699 
700  /* Implementation: Convert a C++ tuple into a Python tuple */
701  template <typename T, size_t... Is>
702  static handle
704  PYBIND11_WORKAROUND_INCORRECT_MSVC_C4100(src, policy, parent);
706  std::array<object, size> entries{{reinterpret_steal<object>(
707  make_caster<Ts>::cast(std::get<Is>(std::forward<T>(src)), policy, parent))...}};
708  for (const auto &entry : entries) {
709  if (!entry) {
710  return handle();
711  }
712  }
713  tuple result(size);
714  int counter = 0;
715  for (auto &entry : entries) {
716  PyTuple_SET_ITEM(result.ptr(), counter++, entry.release().ptr());
717  }
718  return result.release();
719  }
720 
721  Tuple<make_caster<Ts>...> subcasters;
722 };
723 
724 template <typename T1, typename T2>
725 class type_caster<std::pair<T1, T2>> : public tuple_caster<std::pair, T1, T2> {};
726 
727 template <typename... Ts>
728 class type_caster<std::tuple<Ts...>> : public tuple_caster<std::tuple, Ts...> {};
729 
732 template <typename T>
734  static auto get(const T &p) -> decltype(p.get()) { return p.get(); }
735 };
736 
742 template <typename type, typename holder_type, typename SFINAE = void>
744 public:
746  static_assert(std::is_base_of<base, type_caster<type>>::value,
747  "Holder classes are only supported for custom types");
748  using base::base;
749  using base::cast;
750  using base::typeinfo;
751  using base::value;
752 
753  bool load(handle src, bool convert) {
754  return base::template load_impl<copyable_holder_caster<type, holder_type>>(src, convert);
755  }
756 
757  explicit operator type *() { return this->value; }
758  // static_cast works around compiler error with MSVC 17 and CUDA 10.2
759  // see issue #2180
760  explicit operator type &() { return *(static_cast<type *>(this->value)); }
761  explicit operator holder_type *() { return std::addressof(holder); }
762  explicit operator holder_type &() { return holder; }
763 
764  static handle cast(const holder_type &src, return_value_policy, handle) {
765  const auto *ptr = holder_helper<holder_type>::get(src);
766  return type_caster_base<type>::cast_holder(ptr, &src);
767  }
768 
769 protected:
770  friend class type_caster_generic;
772  if (typeinfo->default_holder) {
773  throw cast_error("Unable to load a custom holder type from a default-holder instance");
774  }
775  }
776 
778  if (v_h.holder_constructed()) {
779  value = v_h.value_ptr();
780  holder = v_h.template holder<holder_type>();
781  return true;
782  }
783  throw cast_error("Unable to cast from non-held to held instance (T& to Holder<T>) "
785  "(#define PYBIND11_DETAILED_ERROR_MESSAGES or compile in debug mode for "
786  "type information)");
787 #else
788  "of type '"
789  + type_id<holder_type>() + "''");
790 #endif
791  }
792 
793  template <typename T = holder_type,
796  return false;
797  }
798 
799  template <typename T = holder_type,
802  for (auto &cast : typeinfo->implicit_casts) {
803  copyable_holder_caster sub_caster(*cast.first);
804  if (sub_caster.load(src, convert)) {
805  value = cast.second(sub_caster.value);
806  holder = holder_type(sub_caster.holder, (type *) value);
807  return true;
808  }
809  }
810  return false;
811  }
812 
813  static bool try_direct_conversions(handle) { return false; }
814 
815  holder_type holder;
816 };
817 
819 template <typename T>
820 class type_caster<std::shared_ptr<T>> : public copyable_holder_caster<T, std::shared_ptr<T>> {};
821 
825 template <typename type, typename holder_type, typename SFINAE = void>
827  static_assert(std::is_base_of<type_caster_base<type>, type_caster<type>>::value,
828  "Holder classes are only supported for custom types");
829 
830  static handle cast(holder_type &&src, return_value_policy, handle) {
831  auto *ptr = holder_helper<holder_type>::get(src);
832  return type_caster_base<type>::cast_holder(ptr, std::addressof(src));
833  }
834  static constexpr auto name = type_caster_base<type>::name;
835 };
836 
837 template <typename type, typename deleter>
838 class type_caster<std::unique_ptr<type, deleter>>
839  : public move_only_holder_caster<type, std::unique_ptr<type, deleter>> {};
840 
841 template <typename type, typename holder_type>
845 
846 template <typename T, bool Value = false>
848  static constexpr bool value = Value;
849 };
850 
852 #define PYBIND11_DECLARE_HOLDER_TYPE(type, holder_type, ...) \
853  PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE) \
854  namespace detail { \
855  template <typename type> \
856  struct always_construct_holder<holder_type> : always_construct_holder<void, ##__VA_ARGS__> { \
857  }; \
858  template <typename type> \
859  class type_caster<holder_type, enable_if_t<!is_shared_ptr<holder_type>::value>> \
860  : public type_caster_holder<type, holder_type> {}; \
861  } \
862  PYBIND11_NAMESPACE_END(PYBIND11_NAMESPACE)
863 
864 // PYBIND11_DECLARE_HOLDER_TYPE holder types:
865 template <typename base, typename holder>
867  : std::is_base_of<detail::type_caster_holder<base, holder>, detail::type_caster<holder>> {};
868 // Specialization for always-supported unique_ptr holders:
869 template <typename base, typename deleter>
870 struct is_holder_type<base, std::unique_ptr<base, deleter>> : std::true_type {};
871 
872 template <typename T>
874  static constexpr auto name = const_name<T>();
875 };
876 template <>
878  static constexpr auto name = const_name("bool");
879 };
880 template <>
882  static constexpr auto name = const_name(PYBIND11_BYTES_NAME);
883 };
884 template <>
886  static constexpr auto name = const_name("int");
887 };
888 template <>
890  static constexpr auto name = const_name("Iterable");
891 };
892 template <>
894  static constexpr auto name = const_name("Iterator");
895 };
896 template <>
898  static constexpr auto name = const_name("float");
899 };
900 template <>
902  static constexpr auto name = const_name("None");
903 };
904 template <>
906  static constexpr auto name = const_name("*args");
907 };
908 template <>
910  static constexpr auto name = const_name("**kwargs");
911 };
912 
913 template <typename type>
917 
918  // `type` may not be default constructible (e.g. frozenset, anyset). Initializing `value`
919  // to a nil handle is safe since it will only be accessed if `load` succeeds.
922 
924  bool load(handle src, bool /* convert */) {
925  value = src;
926  return static_cast<bool>(value);
927  }
928 
930  bool load(handle src, bool /* convert */) {
931  if (!isinstance<type>(src)) {
932  return false;
933  }
934  value = reinterpret_borrow<type>(src);
935  return true;
936  }
937 
938  static handle cast(const handle &src, return_value_policy /* policy */, handle /* parent */) {
939  return src.inc_ref();
940  }
942 };
943 
944 template <typename T>
946 
947 // Our conditions for enabling moving are quite restrictive:
948 // At compile time:
949 // - T needs to be a non-const, non-pointer, non-reference type
950 // - type_caster<T>::operator T&() must exist
951 // - the type must be move constructible (obviously)
952 // At run-time:
953 // - if the type is non-copy-constructible, the object must be the sole owner of the type (i.e. it
954 // must have ref_count() == 1)h
955 // If any of the above are not satisfied, we fall back to copying.
956 template <typename T>
957 using move_is_plain_type
959 template <typename T, typename SFINAE = void>
960 struct move_always : std::false_type {};
961 template <typename T>
962 struct move_always<
963  T,
964  enable_if_t<
968  std::is_same<decltype(std::declval<make_caster<T>>().operator T &()), T &>>::value>>
969  : std::true_type {};
970 template <typename T, typename SFINAE = void>
971 struct move_if_unreferenced : std::false_type {};
972 template <typename T>
974  T,
975  enable_if_t<
979  std::is_same<decltype(std::declval<make_caster<T>>().operator T &()), T &>>::value>>
980  : std::true_type {};
981 template <typename T>
983 
984 // Detect whether returning a `type` from a cast on type's type_caster is going to result in a
985 // reference or pointer to a local variable of the type_caster. Basically, only
986 // non-reference/pointer `type`s and reference/pointers from a type_caster_generic are safe;
987 // everything else returns a reference/pointer to a local variable.
988 template <typename type>
991  && !std::is_base_of<type_caster_generic, make_caster<type>>::value
992  && !std::is_same<intrinsic_t<type>, void>::value>;
993 
994 // When a value returned from a C++ function is being cast back to Python, we almost always want to
995 // force `policy = move`, regardless of the return value policy the function/method was declared
996 // with.
997 template <typename Return, typename SFINAE = void>
1000 };
1001 
1002 template <typename Return>
1004  Return,
1005  detail::enable_if_t<std::is_base_of<type_caster_generic, make_caster<Return>>::value, void>> {
1009  : p;
1010  }
1011 };
1012 
1013 // Basic python -> C++ casting; throws if casting fails
1014 template <typename T, typename SFINAE>
1016  static_assert(!detail::is_pyobject<T>::value,
1017  "Internal error: type_caster should only be used for C++ types");
1018  if (!conv.load(handle, true)) {
1019 #if !defined(PYBIND11_DETAILED_ERROR_MESSAGES)
1020  throw cast_error(
1021  "Unable to cast Python instance of type "
1022  + str(type::handle_of(handle)).cast<std::string>()
1023  + " to C++ type '?' (#define "
1024  "PYBIND11_DETAILED_ERROR_MESSAGES or compile in debug mode for details)");
1025 #else
1026  throw cast_error("Unable to cast Python instance of type "
1027  + str(type::handle_of(handle)).cast<std::string>() + " to C++ type '"
1028  + type_id<T>() + "'");
1029 #endif
1030  }
1031  return conv;
1032 }
1033 // Wrapper around the above that also constructs and returns a type_caster
1034 template <typename T>
1037  load_type(conv, handle);
1038  return conv;
1039 }
1040 
1042 
1043 // pytype -> C++ type
1044 template <typename T,
1047  int>
1048  = 0>
1049 T cast(const handle &handle) {
1050  using namespace detail;
1052  "Unable to cast type to reference: value is local to type caster");
1053  return cast_op<T>(load_type<T>(handle));
1054 }
1055 
1056 // pytype -> pytype (calls converting constructor)
1058 T cast(const handle &handle) {
1059  return T(reinterpret_borrow<object>(handle));
1060 }
1061 
1062 // Note that `cast<PyObject *>(obj)` increments the reference count of `obj`.
1063 // This is necessary for the case that `obj` is a temporary, and could
1064 // not possibly be different, given
1065 // 1. the established convention that the passed `handle` is borrowed, and
1066 // 2. we don't want to force all generic code using `cast<T>()` to special-case
1067 // handling of `T` = `PyObject *` (to increment the reference count there).
1068 // It is the responsibility of the caller to ensure that the reference count
1069 // is decremented.
1070 template <typename T,
1071  typename Handle,
1074  int>
1075  = 0>
1076 T cast(Handle &&handle) {
1077  return handle.inc_ref().ptr();
1078 }
1079 // To optimize way an inc_ref/dec_ref cycle:
1080 template <typename T,
1081  typename Object,
1084  int>
1085  = 0>
1086 T cast(Object &&obj) {
1087  return obj.release().ptr();
1088 }
1089 
1090 // C++ type -> py::object
1092 object cast(T &&value,
1094  handle parent = handle()) {
1095  using no_ref_T = typename std::remove_reference<T>::type;
1096  if (policy == return_value_policy::automatic) {
1100  } else if (policy == return_value_policy::automatic_reference) {
1104  }
1105  return reinterpret_steal<object>(
1106  detail::make_caster<T>::cast(std::forward<T>(value), policy, parent));
1107 }
1108 
1109 template <typename T>
1110 T handle::cast() const {
1111  return pybind11::cast<T>(*this);
1112 }
1113 template <>
1114 inline void handle::cast() const {
1115  return;
1116 }
1117 
1118 template <typename T>
1120  if (obj.ref_count() > 1) {
1121 #if !defined(PYBIND11_DETAILED_ERROR_MESSAGES)
1122  throw cast_error(
1123  "Unable to cast Python " + str(type::handle_of(obj)).cast<std::string>()
1124  + " instance to C++ rvalue: instance has multiple references"
1125  " (#define PYBIND11_DETAILED_ERROR_MESSAGES or compile in debug mode for details)");
1126 #else
1127  throw cast_error("Unable to move from Python "
1128  + str(type::handle_of(obj)).cast<std::string>() + " instance to C++ "
1129  + type_id<T>() + " instance: instance has multiple references");
1130 #endif
1131  }
1132 
1133  // Move into a temporary and return that, because the reference may be a local value of `conv`
1134  T ret = std::move(detail::load_type<T>(obj).operator T &());
1135  return ret;
1136 }
1137 
1138 // Calling cast() on an rvalue calls pybind11::cast with the object rvalue, which does:
1139 // - If we have to move (because T has no copy constructor), do it. This will fail if the moved
1140 // object has multiple references, but trying to copy will fail to compile.
1141 // - If both movable and copyable, check ref count: if 1, move; otherwise copy
1142 // - Otherwise (not movable), copy.
1143 template <typename T>
1145 cast(object &&object) {
1146  return move<T>(std::move(object));
1147 }
1148 template <typename T>
1150 cast(object &&object) {
1151  if (object.ref_count() > 1) {
1152  return cast<T>(object);
1153  }
1154  return move<T>(std::move(object));
1155 }
1156 template <typename T>
1158 cast(object &&object) {
1159  return cast<T>(object);
1160 }
1161 
1162 // pytype rvalue -> pytype (calls converting constructor)
1163 template <typename T>
1165  return T(std::move(object));
1166 }
1167 
1168 template <typename T>
1169 T object::cast() const & {
1170  return pybind11::cast<T>(*this);
1171 }
1172 template <typename T>
1173 T object::cast() && {
1174  return pybind11::cast<T>(std::move(*this));
1175 }
1176 template <>
1177 inline void object::cast() const & {
1178  return;
1179 }
1180 template <>
1181 inline void object::cast() && {
1182  return;
1183 }
1184 
1186 
1187 // Declared in pytypes.h:
1189 object object_or_cast(T &&o) {
1190  return pybind11::cast(std::forward<T>(o));
1191 }
1192 
1193 // Placeholder type for the unneeded (and dead code) static variable in the
1194 // PYBIND11_OVERRIDE_OVERRIDE macro
1196 template <typename ret_type>
1200 
1201 // Trampoline use: for reference/pointer types to value-converted values, we do a value cast, then
1202 // store the result in the given variable. For other types, this is a no-op.
1203 template <typename T>
1205  make_caster<T> &caster) {
1206  return cast_op<T>(load_type(caster, o));
1207 }
1208 template <typename T>
1210  override_unused &) {
1211  pybind11_fail("Internal error: cast_ref fallback invoked");
1212 }
1213 
1214 // Trampoline use: Having a pybind11::cast with an invalid reference type is going to
1215 // static_assert, even though if it's in dead code, so we provide a "trampoline" to pybind11::cast
1216 // that only does anything in cases where pybind11::cast is valid.
1217 template <typename T>
1219  pybind11_fail("Internal error: cast_safe fallback invoked");
1220 }
1221 template <typename T>
1223 template <typename T>
1225 cast_safe(object &&o) {
1226  return pybind11::cast<T>(std::move(o));
1227 }
1228 
1230 
1231 // The overloads could coexist, i.e. the #if is not strictly speaking needed,
1232 // but it is an easy minor optimization.
1233 #if !defined(PYBIND11_DETAILED_ERROR_MESSAGES)
1234 inline cast_error cast_error_unable_to_convert_call_arg(const std::string &name) {
1235  return cast_error("Unable to convert call argument '" + name
1236  + "' to Python object (#define "
1237  "PYBIND11_DETAILED_ERROR_MESSAGES or compile in debug mode for details)");
1238 }
1239 #else
1240 inline cast_error cast_error_unable_to_convert_call_arg(const std::string &name,
1241  const std::string &type) {
1242  return cast_error("Unable to convert call argument '" + name + "' of type '" + type
1243  + "' to Python object");
1244 }
1245 #endif
1246 
1247 template <return_value_policy policy = return_value_policy::automatic_reference>
1249  return tuple(0);
1250 }
1251 
1252 template <return_value_policy policy = return_value_policy::automatic_reference, typename... Args>
1253 tuple make_tuple(Args &&...args_) {
1254  constexpr size_t size = sizeof...(Args);
1255  std::array<object, size> args{{reinterpret_steal<object>(
1256  detail::make_caster<Args>::cast(std::forward<Args>(args_), policy, nullptr))...}};
1257  for (size_t i = 0; i < args.size(); i++) {
1258  if (!args[i]) {
1259 #if !defined(PYBIND11_DETAILED_ERROR_MESSAGES)
1260  throw cast_error_unable_to_convert_call_arg(std::to_string(i));
1261 #else
1262  std::array<std::string, size> argtypes{{type_id<Args>()...}};
1263  throw cast_error_unable_to_convert_call_arg(std::to_string(i), argtypes[i]);
1264 #endif
1265  }
1266  }
1267  tuple result(size);
1268  int counter = 0;
1269  for (auto &arg_value : args) {
1270  PyTuple_SET_ITEM(result.ptr(), counter++, arg_value.release().ptr());
1271  }
1272  return result;
1273 }
1274 
1277 struct arg {
1280  constexpr explicit arg(const char *name = nullptr)
1281  : name(name), flag_noconvert(false), flag_none(true) {}
1283  template <typename T>
1284  arg_v operator=(T &&value) const;
1286  arg &noconvert(bool flag = true) {
1287  flag_noconvert = flag;
1288  return *this;
1289  }
1291  arg &none(bool flag = true) {
1292  flag_none = flag;
1293  return *this;
1294  }
1295 
1296  const char *name;
1297  bool flag_noconvert : 1;
1298  bool flag_none : 1;
1300 };
1301 
1304 struct arg_v : arg {
1305 private:
1306  template <typename T>
1307  arg_v(arg &&base, T &&x, const char *descr = nullptr)
1310  descr(descr)
1311 #if defined(PYBIND11_DETAILED_ERROR_MESSAGES)
1312  ,
1313  type(type_id<T>())
1314 #endif
1315  {
1316  // Workaround! See:
1317  // https://github.com/pybind/pybind11/issues/2336
1318  // https://github.com/pybind/pybind11/pull/2685#issuecomment-731286700
1319  if (PyErr_Occurred()) {
1320  PyErr_Clear();
1321  }
1322  }
1323 
1324 public:
1326  template <typename T>
1327  arg_v(const char *name, T &&x, const char *descr = nullptr)
1328  : arg_v(arg(name), std::forward<T>(x), descr) {}
1329 
1331  template <typename T>
1332  arg_v(const arg &base, T &&x, const char *descr = nullptr)
1333  : arg_v(arg(base), std::forward<T>(x), descr) {}
1334 
1336  arg_v &noconvert(bool flag = true) {
1337  arg::noconvert(flag);
1338  return *this;
1339  }
1340 
1342  arg_v &none(bool flag = true) {
1343  arg::none(flag);
1344  return *this;
1345  }
1346 
1348  object value;
1350  const char *descr;
1351 #if defined(PYBIND11_DETAILED_ERROR_MESSAGES)
1352  std::string type;
1354 #endif
1355 };
1356 
1360 struct kw_only {};
1361 
1365 struct pos_only {};
1366 
1367 template <typename T>
1369  return {*this, std::forward<T>(value)};
1370 }
1371 
1373 template <typename /*unused*/>
1374 using arg_t = arg_v;
1375 
1376 inline namespace literals {
1380 constexpr arg operator"" _a(const char *name, size_t) { return arg(name); }
1381 } // namespace literals
1382 
1384 
1385 template <typename T>
1386 using is_kw_only = std::is_same<intrinsic_t<T>, kw_only>;
1387 template <typename T>
1388 using is_pos_only = std::is_same<intrinsic_t<T>, pos_only>;
1389 
1390 // forward declaration (definition in attr.h)
1391 struct function_record;
1392 
1395  function_call(const function_record &f, handle p); // Implementation in attr.h
1396 
1399 
1401  std::vector<handle> args;
1402 
1404  std::vector<bool> args_convert;
1405 
1409 
1412 
1415 };
1416 
1418 template <typename... Args>
1420  using indices = make_index_sequence<sizeof...(Args)>;
1421 
1422  template <typename Arg>
1423  using argument_is_args = std::is_same<intrinsic_t<Arg>, args>;
1424  template <typename Arg>
1425  using argument_is_kwargs = std::is_same<intrinsic_t<Arg>, kwargs>;
1426  // Get kwargs argument position, or -1 if not present:
1427  static constexpr auto kwargs_pos = constexpr_last<argument_is_kwargs, Args...>();
1428 
1429  static_assert(kwargs_pos == -1 || kwargs_pos == (int) sizeof...(Args) - 1,
1430  "py::kwargs is only permitted as the last argument of a function");
1431 
1432 public:
1433  static constexpr bool has_kwargs = kwargs_pos != -1;
1434 
1435  // py::args argument position; -1 if not present.
1436  static constexpr int args_pos = constexpr_last<argument_is_args, Args...>();
1437 
1438  static_assert(args_pos == -1 || args_pos == constexpr_first<argument_is_args, Args...>(),
1439  "py::args cannot be specified more than once");
1440 
1441  static constexpr auto arg_names = concat(type_descr(make_caster<Args>::name)...);
1442 
1444 
1445  template <typename Return, typename Guard, typename Func>
1446  // NOLINTNEXTLINE(readability-const-return-type)
1448  return std::move(*this).template call_impl<remove_cv_t<Return>>(
1449  std::forward<Func>(f), indices{}, Guard{});
1450  }
1451 
1452  template <typename Return, typename Guard, typename Func>
1454  std::move(*this).template call_impl<remove_cv_t<Return>>(
1455  std::forward<Func>(f), indices{}, Guard{});
1456  return void_type();
1457  }
1458 
1459 private:
1460  static bool load_impl_sequence(function_call &, index_sequence<>) { return true; }
1461 
1462  template <size_t... Is>
1464 #ifdef __cpp_fold_expressions
1465  if ((... || !std::get<Is>(argcasters).load(call.args[Is], call.args_convert[Is]))) {
1466  return false;
1467  }
1468 #else
1469  for (bool r : {std::get<Is>(argcasters).load(call.args[Is], call.args_convert[Is])...}) {
1470  if (!r) {
1471  return false;
1472  }
1473  }
1474 #endif
1475  return true;
1476  }
1477 
1478  template <typename Return, typename Func, size_t... Is, typename Guard>
1479  Return call_impl(Func &&f, index_sequence<Is...>, Guard &&) && {
1480  return std::forward<Func>(f)(cast_op<Args>(std::move(std::get<Is>(argcasters)))...);
1481  }
1482 
1483  std::tuple<make_caster<Args>...> argcasters;
1484 };
1485 
1488 template <return_value_policy policy>
1490 public:
1491  template <typename... Ts>
1492  explicit simple_collector(Ts &&...values)
1493  : m_args(pybind11::make_tuple<policy>(std::forward<Ts>(values)...)) {}
1494 
1495  const tuple &args() const & { return m_args; }
1496  dict kwargs() const { return {}; }
1497 
1498  tuple args() && { return std::move(m_args); }
1499 
1501  object call(PyObject *ptr) const {
1502  PyObject *result = PyObject_CallObject(ptr, m_args.ptr());
1503  if (!result) {
1504  throw error_already_set();
1505  }
1506  return reinterpret_steal<object>(result);
1507  }
1508 
1509 private:
1511 };
1512 
1514 template <return_value_policy policy>
1516 public:
1517  template <typename... Ts>
1518  explicit unpacking_collector(Ts &&...values) {
1519  // Tuples aren't (easily) resizable so a list is needed for collection,
1520  // but the actual function call strictly requires a tuple.
1521  auto args_list = list();
1522  using expander = int[];
1523  (void) expander{0, (process(args_list, std::forward<Ts>(values)), 0)...};
1524 
1525  m_args = std::move(args_list);
1526  }
1527 
1528  const tuple &args() const & { return m_args; }
1529  const dict &kwargs() const & { return m_kwargs; }
1530 
1531  tuple args() && { return std::move(m_args); }
1532  dict kwargs() && { return std::move(m_kwargs); }
1533 
1535  object call(PyObject *ptr) const {
1536  PyObject *result = PyObject_Call(ptr, m_args.ptr(), m_kwargs.ptr());
1537  if (!result) {
1538  throw error_already_set();
1539  }
1540  return reinterpret_steal<object>(result);
1541  }
1542 
1543 private:
1544  template <typename T>
1545  void process(list &args_list, T &&x) {
1546  auto o = reinterpret_steal<object>(
1547  detail::make_caster<T>::cast(std::forward<T>(x), policy, {}));
1548  if (!o) {
1549 #if !defined(PYBIND11_DETAILED_ERROR_MESSAGES)
1550  throw cast_error_unable_to_convert_call_arg(std::to_string(args_list.size()));
1551 #else
1552  throw cast_error_unable_to_convert_call_arg(std::to_string(args_list.size()),
1553  type_id<T>());
1554 #endif
1555  }
1556  args_list.append(std::move(o));
1557  }
1558 
1559  void process(list &args_list, detail::args_proxy ap) {
1560  for (auto a : ap) {
1561  args_list.append(a);
1562  }
1563  }
1564 
1565  void process(list & /*args_list*/, arg_v a) {
1566  if (!a.name) {
1567 #if !defined(PYBIND11_DETAILED_ERROR_MESSAGES)
1569 #else
1570  nameless_argument_error(a.type);
1571 #endif
1572  }
1573  if (m_kwargs.contains(a.name)) {
1574 #if !defined(PYBIND11_DETAILED_ERROR_MESSAGES)
1576 #else
1577  multiple_values_error(a.name);
1578 #endif
1579  }
1580  if (!a.value) {
1581 #if !defined(PYBIND11_DETAILED_ERROR_MESSAGES)
1583 #else
1584  throw cast_error_unable_to_convert_call_arg(a.name, a.type);
1585 #endif
1586  }
1587  m_kwargs[a.name] = std::move(a.value);
1588  }
1589 
1590  void process(list & /*args_list*/, detail::kwargs_proxy kp) {
1591  if (!kp) {
1592  return;
1593  }
1594  for (auto k : reinterpret_borrow<dict>(kp)) {
1595  if (m_kwargs.contains(k.first)) {
1596 #if !defined(PYBIND11_DETAILED_ERROR_MESSAGES)
1598 #else
1599  multiple_values_error(str(k.first));
1600 #endif
1601  }
1602  m_kwargs[k.first] = k.second;
1603  }
1604  }
1605 
1606  [[noreturn]] static void nameless_argument_error() {
1607  throw type_error(
1608  "Got kwargs without a name; only named arguments "
1609  "may be passed via py::arg() to a python function call. "
1610  "(#define PYBIND11_DETAILED_ERROR_MESSAGES or compile in debug mode for details)");
1611  }
1612  [[noreturn]] static void nameless_argument_error(const std::string &type) {
1613  throw type_error("Got kwargs without a name of type '" + type
1614  + "'; only named "
1615  "arguments may be passed via py::arg() to a python function call. ");
1616  }
1617  [[noreturn]] static void multiple_values_error() {
1618  throw type_error(
1619  "Got multiple values for keyword argument "
1620  "(#define PYBIND11_DETAILED_ERROR_MESSAGES or compile in debug mode for details)");
1621  }
1622 
1623  [[noreturn]] static void multiple_values_error(const std::string &name) {
1624  throw type_error("Got multiple values for keyword argument '" + name + "'");
1625  }
1626 
1627 private:
1630 };
1631 
1632 // [workaround(intel)] Separate function required here
1633 // We need to put this into a separate function because the Intel compiler
1634 // fails to compile enable_if_t<!all_of<is_positional<Args>...>::value>
1635 // (tested with ICC 2021.1 Beta 20200827).
1636 template <typename... Args>
1637 constexpr bool args_are_all_positional() {
1639 }
1640 
1642 template <return_value_policy policy,
1643  typename... Args,
1644  typename = enable_if_t<args_are_all_positional<Args...>()>>
1646  return simple_collector<policy>(std::forward<Args>(args)...);
1647 }
1648 
1650 template <return_value_policy policy,
1651  typename... Args,
1652  typename = enable_if_t<!args_are_all_positional<Args...>()>>
1654  // Following argument order rules for generalized unpacking according to PEP 448
1655  static_assert(constexpr_last<is_positional, Args...>()
1656  < constexpr_first<is_keyword_or_ds, Args...>()
1657  && constexpr_last<is_s_unpacking, Args...>()
1658  < constexpr_first<is_ds_unpacking, Args...>(),
1659  "Invalid function call: positional args must precede keywords and ** unpacking; "
1660  "* unpacking must precede ** unpacking");
1661  return unpacking_collector<policy>(std::forward<Args>(args)...);
1662 }
1663 
1664 template <typename Derived>
1665 template <return_value_policy policy, typename... Args>
1666 object object_api<Derived>::operator()(Args &&...args) const {
1667 #ifndef NDEBUG
1668  if (!PyGILState_Check()) {
1669  pybind11_fail("pybind11::object_api<>::operator() PyGILState_Check() failure.");
1670  }
1671 #endif
1672  return detail::collect_arguments<policy>(std::forward<Args>(args)...).call(derived().ptr());
1673 }
1674 
1675 template <typename Derived>
1676 template <return_value_policy policy, typename... Args>
1677 object object_api<Derived>::call(Args &&...args) const {
1678  return operator()<policy>(std::forward<Args>(args)...);
1679 }
1680 
1682 
1683 template <typename T>
1685  static_assert(std::is_base_of<detail::type_caster_generic, detail::make_caster<T>>::value,
1686  "py::type::of<T> only supports the case where T is a registered C++ types.");
1687 
1688  return detail::get_type_handle(typeid(T), true);
1689 }
1690 
1691 #define PYBIND11_MAKE_OPAQUE(...) \
1692  PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE) \
1693  namespace detail { \
1694  template <> \
1695  class type_caster<__VA_ARGS__> : public type_caster_base<__VA_ARGS__> {}; \
1696  } \
1697  PYBIND11_NAMESPACE_END(PYBIND11_NAMESPACE)
1698 
1702 #define PYBIND11_TYPE(...) __VA_ARGS__
1703 
type_caster< CharT, enable_if_t< is_std_char_type< CharT >::value > >::str_caster
StringCaster str_caster
Definition: cast.h:522
tuple_caster::cast
static handle cast(T *src, return_value_policy policy, handle parent)
Definition: cast.h:651
int_
Definition: pytypes.h:1793
pyobject_caster::pyobject_caster
pyobject_caster()
Definition: cast.h:916
load_type
type_caster< T, SFINAE > & load_type(type_caster< T, SFINAE > &conv, const handle &handle)
Definition: cast.h:1015
PYBIND11_BYTES_SIZE
#define PYBIND11_BYTES_SIZE
Definition: wrap/pybind11/include/pybind11/detail/common.h:354
unpacking_collector::args
tuple args() &&
Definition: cast.h:1531
return_value_policy::move
@ move
all_of
std::is_same< bools< Ts::value..., true >, bools< true, Ts::value... > > all_of
Definition: wrap/pybind11/include/pybind11/detail/common.h:726
unpacking_collector::m_args
tuple m_args
Definition: cast.h:1628
external::detail::conv
PyObject * conv(PyObject *o)
Definition: test_pytypes.cpp:18
type_caster< T, enable_if_t< std::is_arithmetic< T >::value &&!is_std_char_type< T >::value > >::cast
static std::enable_if<!std::is_floating_point< U >::value &&std::is_unsigned< U >::value &&(sizeof(U)<=sizeof(unsigned long)), handle >::type cast(U src, return_value_policy, handle)
Definition: cast.h:222
unpacking_collector::process
void process(list &args_list, T &&x)
Definition: cast.h:1545
name
Annotation for function names.
Definition: attr.h:51
return_value_policy_override
Definition: cast.h:998
function_record
Definition: attr.h:191
cast
T cast(const handle &handle)
Definition: cast.h:1049
string_caster::UTF_N
static constexpr size_t UTF_N
Definition: cast.h:383
base
Annotation indicating that a class derives from another given type.
Definition: attr.h:64
bytes
Definition: pytypes.h:1620
type_caster< void >::cast
static handle cast(const void *ptr, return_value_policy, handle)
Definition: cast.h:294
index_sequence
Index sequences.
Definition: wrap/pybind11/include/pybind11/detail/common.h:671
argument_loader::load_args
bool load_args(function_call &call)
Definition: cast.h:1443
arg::arg
constexpr arg(const char *name=nullptr)
Definition: cast.h:1280
gtsam.examples.DogLegOptimizerExample.type
type
Definition: DogLegOptimizerExample.py:111
ssize_t
Py_ssize_t ssize_t
Definition: wrap/pybind11/include/pybind11/detail/common.h:475
type_caster< bool >::cast
static handle cast(bool src, return_value_policy, handle)
Definition: cast.h:357
error_already_set
Definition: pytypes.h:722
kwargs
Definition: pytypes.h:2166
s
RealScalar s
Definition: level1_cplx_impl.h:126
const_name
constexpr descr< N - 1 > const_name(char const (&text)[N])
Definition: descr.h:60
tuple_caster
Definition: cast.h:627
v0
static const double v0
Definition: testCal3DFisheye.cpp:31
type_caster< CharT, enable_if_t< is_std_char_type< CharT >::value > >::cast
static handle cast(CharT src, return_value_policy policy, handle parent)
Definition: cast.h:549
return_value_policy
return_value_policy
Approach used to cast a previously unknown C++ instance into a Python object.
Definition: wrap/pybind11/include/pybind11/detail/common.h:485
void_caster::load
bool load(handle src, bool)
Definition: cast.h:248
type_info::implicit_casts
std::vector< std::pair< const std::type_info *, void *(*)(void *)> > implicit_casts
Definition: internals.h:234
copyable_holder_caster::cast
static handle cast(const holder_type &src, return_value_policy, handle)
Definition: cast.h:764
type_caster_generic::value
void * value
Definition: type_caster_base.h:795
remove_cv_t
typename std::remove_cv< T >::type remove_cv_t
Definition: wrap/pybind11/include/pybind11/detail/common.h:644
literals
Definition: cast.h:1376
argument_loader::call
enable_if_t<!std::is_void< Return >::value, Return > call(Func &&f) &&
Definition: cast.h:1447
x
set noclip points set clip one set noclip two set bar set border lt lw set xdata set ydata set zdata set x2data set y2data set boxwidth set dummy x
Definition: gnuplot_common_settings.hh:12
handle::inc_ref
const handle & inc_ref() const &
Definition: pytypes.h:249
function_call::func
const function_record & func
The function data:
Definition: cast.h:1398
return_value_policy_override< Return, detail::enable_if_t< std::is_base_of< type_caster_generic, make_caster< Return > >::value, void > >::policy
static return_value_policy policy(return_value_policy p)
Definition: cast.h:1006
ret
DenseIndex ret
Definition: level1_cplx_impl.h:44
type_info::default_holder
bool default_holder
Definition: internals.h:247
values_and_holders::begin
iterator begin()
Definition: type_caster_base.h:355
argument_loader::argument_is_args
std::is_same< intrinsic_t< Arg >, args > argument_is_args
Definition: cast.h:1423
type_caster< CharT, enable_if_t< is_std_char_type< CharT >::value > >::StringType
std::basic_string< CharT > StringType
Definition: cast.h:520
simple_collector::args
tuple args() &&
Definition: cast.h:1498
PYBIND11_NAMESPACE_END
#define PYBIND11_NAMESPACE_END(name)
Definition: wrap/pybind11/include/pybind11/detail/common.h:80
unpacking_collector::multiple_values_error
static void multiple_values_error(const std::string &name)
Definition: cast.h:1623
hasattr
bool hasattr(handle obj, handle name)
Definition: pytypes.h:853
capsule
Definition: pytypes.h:1909
move_only_holder_caster::cast
static handle cast(holder_type &&src, return_value_policy, handle)
Definition: cast.h:830
arg_v::arg_v
arg_v(arg &&base, T &&x, const char *descr=nullptr)
Definition: cast.h:1307
tuple_caster::type
Tuple< Ts... > type
Definition: cast.h:628
void_caster
Definition: cast.h:246
type
Definition: pytypes.h:1491
type_caster< T, enable_if_t< std::is_arithmetic< T >::value &&!is_std_char_type< T >::value > >::_py_type_1
conditional_t< std::is_signed< T >::value, _py_type_0, typename std::make_unsigned< _py_type_0 >::type > _py_type_1
Definition: cast.h:128
move_always
Definition: cast.h:960
value_and_holder
Definition: type_caster_base.h:253
T
Eigen::Triplet< double > T
Definition: Tutorial_sparse_example.cpp:6
object_api::operator()
object operator()(Args &&...args) const
Definition: cast.h:1666
argument_loader::load_impl_sequence
bool load_impl_sequence(function_call &call, index_sequence< Is... >)
Definition: cast.h:1463
argument_loader::arg_names
static constexpr auto arg_names
Definition: cast.h:1441
type_caster< CharT, enable_if_t< is_std_char_type< CharT >::value > >::cast
static handle cast(const CharT *src, return_value_policy policy, handle parent)
Definition: cast.h:542
cast_ref
enable_if_t< cast_is_temporary_value_reference< T >::value, T > cast_ref(object &&o, make_caster< T > &caster)
Definition: cast.h:1204
detail
Definition: testSerializationNonlinear.cpp:70
function_call::function_call
function_call(const function_record &f, handle p)
Definition: attr.h:366
is_holder_type
Definition: cast.h:866
type_caster< T, enable_if_t< std::is_arithmetic< T >::value &&!is_std_char_type< T >::value > >::cast
static std::enable_if<!std::is_floating_point< U >::value &&std::is_signed< U >::value &&(sizeof(U)<=sizeof(long)), handle >::type cast(U src, return_value_policy, handle)
Definition: cast.h:214
buffer
Definition: pytypes.h:2223
PYBIND11_NAMESPACE_BEGIN
#define PYBIND11_NAMESPACE_BEGIN(name)
Definition: wrap/pybind11/include/pybind11/detail/common.h:76
collect_arguments
simple_collector< policy > collect_arguments(Args &&...args)
Collect only positional arguments for a Python function call.
Definition: cast.h:1645
iterator
Definition: pytypes.h:1426
res
cout<< "Here is the matrix m:"<< endl<< m<< endl;Matrix< ptrdiff_t, 3, 1 > res
Definition: PartialRedux_count.cpp:3
h
const double h
Definition: testSimpleHelicopter.cpp:19
is_move_constructible
Definition: type_caster_base.h:972
arg::operator=
arg_v operator=(T &&value) const
Assign a value to this argument.
Definition: cast.h:1368
always_construct_holder
Definition: cast.h:847
all_type_info
const std::vector< detail::type_info * > & all_type_info(PyTypeObject *type)
Definition: type_caster_base.h:169
result
Values result
Definition: OdometryOptimize.cpp:8
tuple_caster::cast_impl
static handle cast_impl(T &&src, return_value_policy policy, handle parent, index_sequence< Is... >)
Definition: cast.h:703
descr
Definition: descr.h:25
type_caster< std::reference_wrapper< type > >::cast_op_type
std::reference_wrapper< type > cast_op_type
Definition: cast.h:81
string_caster::cast
static handle cast(const StringType &src, return_value_policy, handle)
Definition: cast.h:438
argument_loader::call
enable_if_t< std::is_void< Return >::value, void_type > call(Func &&f) &&
Definition: cast.h:1453
copyable_holder_caster::try_direct_conversions
static bool try_direct_conversions(handle)
Definition: cast.h:813
type_caster< T, enable_if_t< std::is_arithmetic< T >::value &&!is_std_char_type< T >::value > >::load
bool load(handle src, bool convert)
Definition: cast.h:132
PYBIND11_LONG_AS_LONGLONG
#define PYBIND11_LONG_AS_LONGLONG(o)
Definition: wrap/pybind11/include/pybind11/detail/common.h:356
tuple_caster::implicit_cast
type implicit_cast(index_sequence< Is... >) &&
Definition: cast.h:678
function_call::parent
handle parent
The parent, if any.
Definition: cast.h:1411
conditional_t
typename std::conditional< B, T, F >::type conditional_t
Definition: wrap/pybind11/include/pybind11/detail/common.h:642
name
static char name[]
Definition: rgamma.c:72
tuple_caster< std::pair, T1, T2 >::cast_op_type
type cast_op_type
Definition: cast.h:667
size
Scalar Scalar int size
Definition: benchVecAdd.cpp:17
bool_
Definition: pytypes.h:1756
instance
The 'instance' type which needs to be standard layout (need to be able to use 'offsetof')
Definition: wrap/pybind11/include/pybind11/detail/common.h:569
function_call
Internal data associated with a single function call.
Definition: cast.h:1394
type_caster< std::reference_wrapper< type > >::load
bool load(handle src, bool convert)
Definition: cast.h:69
override_unused
Definition: cast.h:1195
simple_collector::simple_collector
simple_collector(Ts &&...values)
Definition: cast.h:1492
is_pos_only
std::is_same< intrinsic_t< T >, pos_only > is_pos_only
Definition: cast.h:1388
type_caster< void >::cast_op_type
void *& cast_op_type
Definition: cast.h:302
unpacking_collector::process
void process(list &, detail::kwargs_proxy kp)
Definition: cast.h:1590
arg_v::type
std::string type
The C++ type name of the default value (only available when compiled in debug mode)
Definition: cast.h:1353
always_construct_holder::value
static constexpr bool value
Definition: cast.h:848
type_caster_base::cast
static handle cast(const itype &src, return_value_policy policy, handle parent)
Definition: type_caster_base.h:1072
copyable_holder_caster::check_holder_compat
void check_holder_compat()
Definition: cast.h:771
simple_collector::call
object call(PyObject *ptr) const
Call a Python function and pass the collected arguments.
Definition: cast.h:1501
is_kw_only
std::is_same< intrinsic_t< T >, kw_only > is_kw_only
Definition: cast.h:1386
arg::name
const char * name
If non-null, this is a named kwargs argument.
Definition: cast.h:1296
Object
Reference counted object base class.
Definition: object.h:9
simple_collector::args
const tuple & args() const &
Definition: cast.h:1495
pyobject_caster::PYBIND11_TYPE_CASTER
PYBIND11_TYPE_CASTER(type, handle_type_name< type >::name)
PYBIND11_LONG_CHECK
#define PYBIND11_LONG_CHECK(o)
Definition: wrap/pybind11/include/pybind11/detail/common.h:355
dict
Definition: pytypes.h:2065
arg_v::arg_v
arg_v(const char *name, T &&x, const char *descr=nullptr)
Direct construction with name, default, and description.
Definition: cast.h:1327
string_caster::PYBIND11_TYPE_CASTER
PYBIND11_TYPE_CASTER(StringType, const_name(PYBIND11_STRING_NAME))
void_caster::PYBIND11_TYPE_CASTER
PYBIND11_TYPE_CASTER(T, const_name("None"))
tuple_caster::load
bool load(handle src, bool convert)
Definition: cast.h:633
simple_collector::m_args
tuple m_args
Definition: cast.h:1510
return_value_policy::automatic
@ automatic
intrinsic_t
typename intrinsic_type< T >::type intrinsic_t
Definition: wrap/pybind11/include/pybind11/detail/common.h:798
override_caster_t
conditional_t< cast_is_temporary_value_reference< ret_type >::value, make_caster< ret_type >, override_unused > override_caster_t
Definition: cast.h:1199
handle
Definition: pytypes.h:217
type_caster
Definition: cast.h:38
type_caster< T, enable_if_t< std::is_arithmetic< T >::value &&!is_std_char_type< T >::value > >::py_type
conditional_t< std::is_floating_point< T >::value, double, _py_type_1 > py_type
Definition: cast.h:129
sequence
Definition: pytypes.h:2104
handle::cast
T cast() const
Definition: cast.h:1110
arg_v::value
object value
The default value.
Definition: cast.h:1348
type::handle_of
static handle handle_of()
Definition: cast.h:1684
make_tuple
tuple make_tuple()
Definition: cast.h:1248
function_call::init_self
handle init_self
If this is a call to an initializer, this argument contains self
Definition: cast.h:1414
arg_v
Definition: cast.h:1304
return_value_policy_override::policy
static return_value_policy policy(return_value_policy p)
Definition: cast.h:999
return_value_policy::copy
@ copy
object::release
handle release()
Definition: pytypes.h:368
cast_is_temporary_value_reference
bool_constant<(std::is_reference< type >::value||std::is_pointer< type >::value) &&!std::is_base_of< type_caster_generic, make_caster< type > >::value &&!std::is_same< intrinsic_t< type >, void >::value > cast_is_temporary_value_reference
Definition: cast.h:992
void_caster::cast
static handle cast(T, return_value_policy, handle)
Definition: cast.h:254
arg::flag_noconvert
bool flag_noconvert
Definition: cast.h:1297
typeid.h
copyable_holder_caster::load
bool load(handle src, bool convert)
Definition: cast.h:753
type_caster< T, enable_if_t< std::is_arithmetic< T >::value &&!is_std_char_type< T >::value > >::cast
static std::enable_if<!std::is_floating_point< U >::value &&std::is_unsigned< U >::value &&(sizeof(U) > sizeof(unsigned long)), handle >::type cast(U src, return_value_policy, handle)
Definition: cast.h:238
arg
Definition: cast.h:1277
return_value_policy::reference
@ reference
PYBIND11_DETAILED_ERROR_MESSAGES
#define PYBIND11_DETAILED_ERROR_MESSAGES
Definition: wrap/pybind11/include/pybind11/detail/common.h:1251
string_caster
Definition: cast.h:365
tuple_caster::subcasters
Tuple< make_caster< Ts >... > subcasters
Definition: cast.h:721
unpacking_collector::kwargs
dict kwargs() &&
Definition: cast.h:1532
type_caster< std::reference_wrapper< type > >::subcaster_cast_op_type
typename caster_t::template cast_op_type< reference_t > subcaster_cast_op_type
Definition: cast.h:60
negation
Definition: wrap/pybind11/include/pybind11/detail/common.h:699
pyobject_caster::load
bool load(handle src, bool)
Definition: cast.h:924
unpacking_collector::process
void process(list &args_list, detail::args_proxy ap)
Definition: cast.h:1559
arg::flag_none
bool flag_none
If set (the default), allow None to be passed to this argument.
Definition: cast.h:1299
unpacking_collector::unpacking_collector
unpacking_collector(Ts &&...values)
Definition: cast.h:1518
unpacking_collector::kwargs
const dict & kwargs() const &
Definition: cast.h:1529
bytearray
Definition: pytypes.h:1714
type_caster< CharT, enable_if_t< is_std_char_type< CharT >::value > >::load
bool load(handle src, bool convert)
Definition: cast.h:527
holder_helper
Definition: cast.h:733
unpacking_collector::m_kwargs
dict m_kwargs
Definition: cast.h:1629
is_copy_constructible
Definition: type_caster_base.h:991
move_if_unreferenced
Definition: cast.h:971
argument_loader::call_impl
Return call_impl(Func &&f, index_sequence< Is... >, Guard &&) &&
Definition: cast.h:1479
PYBIND11_NAMESPACE
Definition: test_custom_type_casters.cpp:24
cast_error_unable_to_convert_call_arg
cast_error cast_error_unable_to_convert_call_arg(const std::string &name, const std::string &type)
Definition: cast.h:1240
Eigen::Triplet< double >
concat
constexpr descr< 0 > concat()
Definition: descr.h:139
arg
EIGEN_DEVICE_FUNC const EIGEN_STRONG_INLINE ArgReturnType arg() const
Definition: ArrayCwiseUnaryOps.h:66
common.h
pyobject_caster
Definition: cast.h:914
cast_safe
enable_if_t< cast_is_temporary_value_reference< T >::value, T > cast_safe(object &&)
Definition: cast.h:1218
pybind11_fail
PyExc_RuntimeError PYBIND11_NOINLINE void pybind11_fail(const char *reason)
Used internally.
Definition: wrap/pybind11/include/pybind11/detail/common.h:1013
simple_collector
Definition: cast.h:1489
kw_only
Definition: cast.h:1360
argument_loader::indices
make_index_sequence< sizeof...(Args)> indices
Definition: cast.h:1420
argument_loader::args_pos
static constexpr int args_pos
Definition: cast.h:1436
value_and_holder::value_ptr
V *& value_ptr() const
Definition: type_caster_base.h:272
type_caster< void >::load
bool load(handle h, bool)
Definition: cast.h:268
function_call::args_ref
object args_ref
Definition: cast.h:1408
size_t
std::size_t size_t
Definition: wrap/pybind11/include/pybind11/detail/common.h:476
unpacking_collector::nameless_argument_error
static void nameless_argument_error(const std::string &type)
Definition: cast.h:1612
arg_v::noconvert
arg_v & noconvert(bool flag=true)
Same as arg::noconvert(), but returns *this as arg_v&, not arg&.
Definition: cast.h:1336
cast_op_type
conditional_t< std::is_pointer< remove_reference_t< T > >::value, typename std::add_pointer< intrinsic_t< T > >::type, typename std::add_lvalue_reference< intrinsic_t< T > >::type > cast_op_type
Definition: type_caster_base.h:808
type_descr
constexpr descr< N+2, Ts... > type_descr(const descr< N, Ts... > &descr)
Definition: descr.h:166
argument_loader::kwargs_pos
static constexpr auto kwargs_pos
Definition: cast.h:1427
pytypes.h
descr.h
type_caster_generic
Definition: type_caster_base.h:498
PYBIND11_BYTES_NAME
#define PYBIND11_BYTES_NAME
Definition: wrap/pybind11/include/pybind11/detail/common.h:359
tree::f
Point2(* f)(const Point3 &, OptionalJacobian< 2, 3 >)
Definition: testExpression.cpp:218
PYBIND11_BOOL_ATTR
#define PYBIND11_BOOL_ATTR
Definition: wrap/pybind11/include/pybind11/detail/common.h:364
tuple_caster< std::pair, T1, T2 >::indices
make_index_sequence< size > indices
Definition: cast.h:630
PYBIND11_TYPE_CASTER
#define PYBIND11_TYPE_CASTER(type, py_name)
Definition: cast.h:85
bool_constant
std::integral_constant< bool, B > bool_constant
Backports of std::bool_constant and std::negation to accommodate older compilers.
Definition: wrap/pybind11/include/pybind11/detail/common.h:697
unpacking_collector::multiple_values_error
static void multiple_values_error()
Definition: cast.h:1617
gtsam::index_sequence<>
return_value_policy::take_ownership
@ take_ownership
copyable_holder_caster::load_value
bool load_value(value_and_holder &&v_h)
Definition: cast.h:777
type_caster< std::reference_wrapper< type > >::cast
static handle cast(const std::reference_wrapper< type > &src, return_value_policy policy, handle parent)
Definition: cast.h:72
arg::none
arg & none(bool flag=true)
Indicates that the argument should/shouldn't allow None (e.g. for nullable pointer args)
Definition: cast.h:1291
unpacking_collector
Helper class which collects positional, keyword, * and ** arguments for a Python function call.
Definition: cast.h:1515
arg_v::descr
const char * descr
The (optional) description of the default value.
Definition: cast.h:1350
PYBIND11_BYTES_AS_STRING
#define PYBIND11_BYTES_AS_STRING
Definition: wrap/pybind11/include/pybind11/detail/common.h:353
a
ArrayXXi a
Definition: Array_initializer_list_23_cxx11.cpp:1
move
detail::enable_if_t<!detail::move_never< T >::value, T > move(object &&obj)
Definition: cast.h:1119
PYBIND11_WORKAROUND_INCORRECT_MSVC_C4100
#define PYBIND11_WORKAROUND_INCORRECT_MSVC_C4100(...)
Definition: wrap/pybind11/include/pybind11/detail/common.h:1217
string_caster::load_raw
bool load_raw(enable_if_t< std::is_same< C, char >::value, handle > src)
Definition: cast.h:473
copyable_holder_caster::holder
holder_type holder
Definition: cast.h:815
make_index_sequence
typename make_index_sequence_impl< N >::type make_index_sequence
Definition: wrap/pybind11/include/pybind11/detail/common.h:679
dict::contains
bool contains(T &&key) const
Definition: pytypes.h:2086
handle_type_name
Definition: cast.h:873
handle::ptr
PyObject * ptr() const
Return the underlying PyObject * pointer.
Definition: pytypes.h:241
tuple::size
size_t size() const
Definition: pytypes.h:2046
constexpr_last
constexpr int constexpr_last()
Return the index of the last type in Ts which satisfies Predicate<T>, or -1 if none match.
Definition: wrap/pybind11/include/pybind11/detail/common.h:845
forward
Definition: testScenarioRunner.cpp:79
PYBIND11_WARNING_DISABLE_MSVC
PYBIND11_WARNING_PUSH PYBIND11_WARNING_DISABLE_MSVC(5054) PYBIND11_WARNING_POP static_assert(EIGEN_VERSION_AT_LEAST(3
argument_loader::argument_is_kwargs
std::is_same< intrinsic_t< Arg >, kwargs > argument_is_kwargs
Definition: cast.h:1425
leaf::values
leaf::MyValues values
tuple_caster::load_impl
static constexpr bool load_impl(const sequence &, bool, index_sequence<>)
Definition: cast.h:682
type_caster< std::reference_wrapper< type > >::subcaster
caster_t subcaster
Definition: cast.h:58
type_caster_holder
conditional_t< is_copy_constructible< holder_type >::value, copyable_holder_caster< type, holder_type >, move_only_holder_caster< type, holder_type > > type_caster_holder
Definition: cast.h:844
argument_loader::load_impl_sequence
static bool load_impl_sequence(function_call &, index_sequence<>)
Definition: cast.h:1460
argument_loader
Helper class which loads arguments for C++ functions called from Python.
Definition: cast.h:1419
iterable
Definition: pytypes.h:1517
copyable_holder_caster::try_implicit_casts
bool try_implicit_casts(handle, bool)
Definition: cast.h:795
type_caster< CharT, enable_if_t< is_std_char_type< CharT >::value > >::cast_op_type
pybind11::detail::cast_op_type< _T > cast_op_type
Definition: cast.h:622
get_type_handle
PYBIND11_NOINLINE handle get_type_handle(const std::type_info &tp, bool throw_if_missing)
Definition: type_caster_base.h:234
std
Definition: BFloat16.h:88
tuple_caster::size
static constexpr auto size
Definition: cast.h:629
void_type
Helper type to replace 'void' in some expressions.
Definition: wrap/pybind11/include/pybind11/detail/common.h:801
args
Definition: pytypes.h:2163
unpacking_collector::call
object call(PyObject *ptr) const
Call a Python function and pass the collected arguments.
Definition: cast.h:1535
move_only_holder_caster
Definition: cast.h:826
p
float * p
Definition: Tutorial_Map_using.cpp:9
unpacking_collector::nameless_argument_error
static void nameless_argument_error()
Definition: cast.h:1606
c_str
const char * c_str(Args &&...args)
Definition: internals.h:599
argument_loader::argcasters
std::tuple< make_caster< Args >... > argcasters
Definition: cast.h:1483
holder_helper::get
static auto get(const T &p) -> decltype(p.get())
Definition: cast.h:734
tuple
Definition: pytypes.h:2035
is_pyobject
std::is_base_of< pyobject_tag, remove_reference_t< T > > is_pyobject
Definition: pytypes.h:72
float_
Definition: pytypes.h:1828
tuple_caster::load_impl
bool load_impl(const sequence &seq, bool convert, index_sequence< Is... >)
Definition: cast.h:685
PYBIND11_NB_BOOL
#define PYBIND11_NB_BOOL(ptr)
Definition: wrap/pybind11/include/pybind11/detail/common.h:365
cast_op
make_caster< T >::template cast_op_type< T > cast_op(make_caster< T > &caster)
Definition: cast.h:44
string_caster::decode_utfN
static handle decode_utfN(const char *buffer, ssize_t nbytes)
Definition: cast.h:451
unpacking_collector::process
void process(list &, arg_v a)
Definition: cast.h:1565
pos_only
Definition: cast.h:1365
U
@ U
Definition: testDecisionTree.cpp:296
pybind11
Definition: wrap/pybind11/pybind11/__init__.py:1
type_caster< bool >::load
bool load(handle src, bool convert)
Definition: cast.h:316
type_caster< T, enable_if_t< std::is_arithmetic< T >::value &&!is_std_char_type< T >::value > >::cast
static std::enable_if< std::is_floating_point< U >::value, handle >::type cast(U src, return_value_policy, handle)
Definition: cast.h:206
type_caster_generic::typeinfo
const type_info * typeinfo
Definition: type_caster_base.h:793
PYBIND11_WORKAROUND_INCORRECT_GCC_UNUSED_BUT_SET_PARAMETER
#define PYBIND11_WORKAROUND_INCORRECT_GCC_UNUSED_BUT_SET_PARAMETER(...)
Definition: wrap/pybind11/include/pybind11/detail/common.h:1225
type_caster_generic::load
bool load(handle src, bool convert)
Definition: type_caster_base.h:506
function_call::args
std::vector< handle > args
Arguments passed to the function:
Definition: cast.h:1401
object_or_cast
object object_or_cast(T &&o)
Definition: cast.h:1189
type_caster_base::cast_holder
static handle cast_holder(const itype *src, const void *holder)
Definition: type_caster_base.h:1119
pyobject_caster::cast
static handle cast(const handle &src, return_value_policy, handle)
Definition: cast.h:938
object::cast
T cast() const &
Definition: cast.h:1169
type_caster< T, enable_if_t< std::is_arithmetic< T >::value &&!is_std_char_type< T >::value > >::_py_type_0
conditional_t< sizeof(T)<=sizeof(long), long, long long > _py_type_0
Definition: cast.h:125
copyable_holder_caster
Definition: cast.h:743
simple_collector::kwargs
dict kwargs() const
Definition: cast.h:1496
none
Definition: pytypes.h:1744
type_caster< T, enable_if_t< std::is_arithmetic< T >::value &&!is_std_char_type< T >::value > >::cast
static std::enable_if<!std::is_floating_point< U >::value &&std::is_signed< U >::value &&(sizeof(U) > sizeof(long)), handle >::type cast(U src, return_value_policy, handle)
Definition: cast.h:230
arg::noconvert
arg & noconvert(bool flag=true)
Indicate that the type should not be converted in the type caster.
Definition: cast.h:1286
PYBIND11_LONG_FROM_SIGNED
#define PYBIND11_LONG_FROM_SIGNED(o)
Definition: wrap/pybind11/include/pybind11/detail/common.h:357
function_call::kwargs_ref
object kwargs_ref
Definition: cast.h:1408
copyable_holder_caster::try_implicit_casts
bool try_implicit_casts(handle src, bool convert)
Definition: cast.h:801
unpacking_collector::args
const tuple & args() const &
Definition: cast.h:1528
return_value_policy::automatic_reference
@ automatic_reference
tuple_caster::implicit_cast
type implicit_cast(index_sequence< Is... >) &
Definition: cast.h:674
reinterpret_steal
T reinterpret_steal(handle h)
Definition: pytypes.h:463
gtsam::convert
static BinaryMeasurement< Rot3 > convert(const BetweenFactor< Pose3 >::shared_ptr &f)
Definition: ShonanAveraging.cpp:998
values_and_holders
Definition: type_caster_base.h:315
gtsam.examples.ShonanAveragingCLI.str
str
Definition: ShonanAveragingCLI.py:115
PYBIND11_STRING_NAME
#define PYBIND11_STRING_NAME
Definition: wrap/pybind11/include/pybind11/detail/common.h:360
argument_loader::has_kwargs
static constexpr bool has_kwargs
Definition: cast.h:1433
Eigen::seq
internal::enable_if<!(symbolic::is_symbolic< FirstType >::value||symbolic::is_symbolic< LastType >::value), ArithmeticSequence< typename internal::cleanup_index_type< FirstType >::type, Index > >::type seq(FirstType f, LastType l)
Definition: ArithmeticSequence.h:234
PYBIND11_BYTES_CHECK
#define PYBIND11_BYTES_CHECK
Definition: wrap/pybind11/include/pybind11/detail/common.h:349
enable_if_t
typename std::enable_if< B, T >::type enable_if_t
from cpp_future import (convenient aliases from C++14/17)
Definition: wrap/pybind11/include/pybind11/detail/common.h:640
test_callbacks.value
value
Definition: test_callbacks.py:158
i
int i
Definition: BiCGSTAB_step_by_step.cpp:9
PYBIND11_LONG_FROM_UNSIGNED
#define PYBIND11_LONG_FROM_UNSIGNED(o)
Definition: wrap/pybind11/include/pybind11/detail/common.h:358
arg_v::arg_v
arg_v(const arg &base, T &&x, const char *descr=nullptr)
Called internally when invoking py::arg("a") = value
Definition: cast.h:1332
loader_life_support::add_patient
static PYBIND11_NOINLINE void add_patient(handle h)
Definition: type_caster_base.h:81
function_call::args_convert
std::vector< bool > args_convert
The convert value the arguments should be loaded with.
Definition: cast.h:1404
string_caster< std::basic_string< CharT, Traits, Allocator > >::CharT
typename std::basic_string< CharT, Traits, Allocator > ::value_type CharT
Definition: cast.h:366
args_are_all_positional
constexpr bool args_are_all_positional()
Definition: cast.h:1637
tuple_caster::cast
static handle cast(T &&src, return_value_policy policy, handle parent)
Definition: cast.h:645
arg_v::none
arg_v & none(bool flag=true)
Same as arg::nonone(), but returns *this as arg_v&, not arg&.
Definition: cast.h:1342
string_caster::load
bool load(handle src, bool)
Definition: cast.h:385
string_caster::load_raw
bool load_raw(enable_if_t<!std::is_same< C, char >::value, handle >)
Definition: cast.h:499
type_caster_base
Generic type caster for objects stored on the heap.
Definition: type_caster_base.h:1063
type_caster_base.h


gtsam
Author(s):
autogenerated on Sat Jun 1 2024 03:01:17