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  using result_t = typename make_caster<T>::template cast_op_type<T>; // See PR #4893
46  return caster.operator result_t();
47 }
48 template <typename T>
51  using result_t = typename make_caster<T>::template cast_op_type<
52  typename std::add_rvalue_reference<T>::type>; // See PR #4893
53  return std::move(caster).operator result_t();
54 }
55 
56 template <typename type>
57 class type_caster<std::reference_wrapper<type>> {
58 private:
61  using reference_t = type &;
62  using subcaster_cast_op_type = typename caster_t::template cast_op_type<reference_t>;
63 
64  static_assert(
67  "std::reference_wrapper<T> caster requires T to have a caster with an "
68  "`operator T &()` or `operator const T &()`");
69 
70 public:
71  bool load(handle src, bool convert) { return subcaster.load(src, convert); }
72  static constexpr auto name = caster_t::name;
73  static handle
74  cast(const std::reference_wrapper<type> &src, return_value_policy policy, handle parent) {
75  // It is definitely wrong to take ownership of this pointer, so mask that rvp
77  || policy == return_value_policy::automatic) {
79  }
80  return caster_t::cast(&src.get(), policy, parent);
81  }
82  template <typename T>
83  using cast_op_type = std::reference_wrapper<type>;
84  explicit operator std::reference_wrapper<type>() { return cast_op<type &>(subcaster); }
85 };
86 
87 #define PYBIND11_TYPE_CASTER(type, py_name) \
88 protected: \
89  type value; \
90  \
91 public: \
92  static constexpr auto name = py_name; \
93  template <typename T_, \
94  ::pybind11::detail::enable_if_t< \
95  std::is_same<type, ::pybind11::detail::remove_cv_t<T_>>::value, \
96  int> \
97  = 0> \
98  static ::pybind11::handle cast( \
99  T_ *src, ::pybind11::return_value_policy policy, ::pybind11::handle parent) { \
100  if (!src) \
101  return ::pybind11::none().release(); \
102  if (policy == ::pybind11::return_value_policy::take_ownership) { \
103  auto h = cast(std::move(*src), policy, parent); \
104  delete src; \
105  return h; \
106  } \
107  return cast(*src, policy, parent); \
108  } \
109  operator type *() { return &value; } /* NOLINT(bugprone-macro-parentheses) */ \
110  operator type &() { return value; } /* NOLINT(bugprone-macro-parentheses) */ \
111  operator type &&() && { return std::move(value); } /* NOLINT(bugprone-macro-parentheses) */ \
112  template <typename T_> \
113  using cast_op_type = ::pybind11::detail::movable_cast_op_type<T_>
114 
115 template <typename CharT>
116 using is_std_char_type = any_of<std::is_same<CharT, char>, /* std::string */
117 #if defined(PYBIND11_HAS_U8STRING)
118  std::is_same<CharT, char8_t>, /* std::u8string */
119 #endif
120  std::is_same<CharT, char16_t>, /* std::u16string */
121  std::is_same<CharT, char32_t>, /* std::u32string */
122  std::is_same<CharT, wchar_t> /* std::wstring */
123  >;
124 
125 template <typename T>
126 struct type_caster<T, enable_if_t<std::is_arithmetic<T>::value && !is_std_char_type<T>::value>> {
127  using _py_type_0 = conditional_t<sizeof(T) <= sizeof(long), long, long long>;
129  _py_type_0,
132 
133 public:
134  bool load(handle src, bool convert) {
135  py_type py_value;
136 
137  if (!src) {
138  return false;
139  }
140 
141 #if !defined(PYPY_VERSION)
142  auto index_check = [](PyObject *o) { return PyIndex_Check(o); };
143 #else
144  // In PyPy 7.3.3, `PyIndex_Check` is implemented by calling `__index__`,
145  // while CPython only considers the existence of `nb_index`/`__index__`.
146  auto index_check = [](PyObject *o) { return hasattr(o, "__index__"); };
147 #endif
148 
150  if (convert || PyFloat_Check(src.ptr())) {
151  py_value = (py_type) PyFloat_AsDouble(src.ptr());
152  } else {
153  return false;
154  }
155  } else if (PyFloat_Check(src.ptr())
156  || (!convert && !PYBIND11_LONG_CHECK(src.ptr()) && !index_check(src.ptr()))) {
157  return false;
158  } else {
159  handle src_or_index = src;
160  // PyPy: 7.3.7's 3.8 does not implement PyLong_*'s __index__ calls.
161 #if PY_VERSION_HEX < 0x03080000 || defined(PYPY_VERSION)
162  object index;
163  if (!PYBIND11_LONG_CHECK(src.ptr())) { // So: index_check(src.ptr())
164  index = reinterpret_steal<object>(PyNumber_Index(src.ptr()));
165  if (!index) {
166  PyErr_Clear();
167  if (!convert)
168  return false;
169  } else {
170  src_or_index = index;
171  }
172  }
173 #endif
175  py_value = as_unsigned<py_type>(src_or_index.ptr());
176  } else { // signed integer:
177  py_value = sizeof(T) <= sizeof(long)
178  ? (py_type) PyLong_AsLong(src_or_index.ptr())
179  : (py_type) PYBIND11_LONG_AS_LONGLONG(src_or_index.ptr());
180  }
181  }
182 
183  // Python API reported an error
184  bool py_err = py_value == (py_type) -1 && PyErr_Occurred();
185 
186  // Check to see if the conversion is valid (integers should match exactly)
187  // Signed/unsigned checks happen elsewhere
188  if (py_err
189  || (std::is_integral<T>::value && sizeof(py_type) != sizeof(T)
190  && py_value != (py_type) (T) py_value)) {
191  PyErr_Clear();
192  if (py_err && convert && (PyNumber_Check(src.ptr()) != 0)) {
193  auto tmp = reinterpret_steal<object>(std::is_floating_point<T>::value
194  ? PyNumber_Float(src.ptr())
195  : PyNumber_Long(src.ptr()));
196  PyErr_Clear();
197  return load(tmp, false);
198  }
199  return false;
200  }
201 
202  value = (T) py_value;
203  return true;
204  }
205 
206  template <typename U = T>
208  cast(U src, return_value_policy /* policy */, handle /* parent */) {
209  return PyFloat_FromDouble((double) src);
210  }
211 
212  template <typename U = T>
214  && (sizeof(U) <= sizeof(long)),
215  handle>::type
216  cast(U src, return_value_policy /* policy */, handle /* parent */) {
217  return PYBIND11_LONG_FROM_SIGNED((long) src);
218  }
219 
220  template <typename U = T>
222  && (sizeof(U) <= sizeof(unsigned long)),
223  handle>::type
224  cast(U src, return_value_policy /* policy */, handle /* parent */) {
225  return PYBIND11_LONG_FROM_UNSIGNED((unsigned long) src);
226  }
227 
228  template <typename U = T>
230  && (sizeof(U) > sizeof(long)),
231  handle>::type
232  cast(U src, return_value_policy /* policy */, handle /* parent */) {
233  return PyLong_FromLongLong((long long) src);
234  }
235 
236  template <typename U = T>
238  && (sizeof(U) > sizeof(unsigned long)),
239  handle>::type
240  cast(U src, return_value_policy /* policy */, handle /* parent */) {
241  return PyLong_FromUnsignedLongLong((unsigned long long) src);
242  }
243 
245 };
246 
247 template <typename T>
248 struct void_caster {
249 public:
250  bool load(handle src, bool) {
251  if (src && src.is_none()) {
252  return true;
253  }
254  return false;
255  }
256  static handle cast(T, return_value_policy /* policy */, handle /* parent */) {
257  return none().release();
258  }
260 };
261 
262 template <>
263 class type_caster<void_type> : public void_caster<void_type> {};
264 
265 template <>
266 class type_caster<void> : public type_caster<void_type> {
267 public:
269 
270  bool load(handle h, bool) {
271  if (!h) {
272  return false;
273  }
274  if (h.is_none()) {
275  value = nullptr;
276  return true;
277  }
278 
279  /* Check if this is a capsule */
280  if (isinstance<capsule>(h)) {
281  value = reinterpret_borrow<capsule>(h);
282  return true;
283  }
284 
285  /* Check if this is a C++ type */
286  const auto &bases = all_type_info((PyTypeObject *) type::handle_of(h).ptr());
287  if (bases.size() == 1) { // Only allowing loading from a single-value type
288  value = values_and_holders(reinterpret_cast<instance *>(h.ptr())).begin()->value_ptr();
289  return true;
290  }
291 
292  /* Fail */
293  return false;
294  }
295 
296  static handle cast(const void *ptr, return_value_policy /* policy */, handle /* parent */) {
297  if (ptr) {
298  return capsule(ptr).release();
299  }
300  return none().release();
301  }
302 
303  template <typename T>
304  using cast_op_type = void *&;
305  explicit operator void *&() { return value; }
306  static constexpr auto name = const_name("capsule");
307 
308 private:
309  void *value = nullptr;
310 };
311 
312 template <>
313 class type_caster<std::nullptr_t> : public void_caster<std::nullptr_t> {};
314 
315 template <>
316 class type_caster<bool> {
317 public:
318  bool load(handle src, bool convert) {
319  if (!src) {
320  return false;
321  }
322  if (src.ptr() == Py_True) {
323  value = true;
324  return true;
325  }
326  if (src.ptr() == Py_False) {
327  value = false;
328  return true;
329  }
330  if (convert || is_numpy_bool(src)) {
331  // (allow non-implicit conversion for numpy booleans), use strncmp
332  // since NumPy 1.x had an additional trailing underscore.
333 
334  Py_ssize_t res = -1;
335  if (src.is_none()) {
336  res = 0; // None is implicitly converted to False
337  }
338 #if defined(PYPY_VERSION)
339  // On PyPy, check that "__bool__" attr exists
340  else if (hasattr(src, PYBIND11_BOOL_ATTR)) {
341  res = PyObject_IsTrue(src.ptr());
342  }
343 #else
344  // Alternate approach for CPython: this does the same as the above, but optimized
345  // using the CPython API so as to avoid an unneeded attribute lookup.
346  else if (auto *tp_as_number = src.ptr()->ob_type->tp_as_number) {
347  if (PYBIND11_NB_BOOL(tp_as_number)) {
348  res = (*PYBIND11_NB_BOOL(tp_as_number))(src.ptr());
349  }
350  }
351 #endif
352  if (res == 0 || res == 1) {
353  value = (res != 0);
354  return true;
355  }
356  PyErr_Clear();
357  }
358  return false;
359  }
360  static handle cast(bool src, return_value_policy /* policy */, handle /* parent */) {
361  return handle(src ? Py_True : Py_False).inc_ref();
362  }
363  PYBIND11_TYPE_CASTER(bool, const_name("bool"));
364 
365 private:
366  // Test if an object is a NumPy boolean (without fetching the type).
367  static inline bool is_numpy_bool(handle object) {
368  const char *type_name = Py_TYPE(object.ptr())->tp_name;
369  // Name changed to `numpy.bool` in NumPy 2, `numpy.bool_` is needed for 1.x support
370  return std::strcmp("numpy.bool", type_name) == 0
371  || std::strcmp("numpy.bool_", type_name) == 0;
372  }
373 };
374 
375 // Helper class for UTF-{8,16,32} C++ stl strings:
376 template <typename StringType, bool IsView = false>
378  using CharT = typename StringType::value_type;
379 
380  // Simplify life by being able to assume standard char sizes (the standard only guarantees
381  // minimums, but Python requires exact sizes)
382  static_assert(!std::is_same<CharT, char>::value || sizeof(CharT) == 1,
383  "Unsupported char size != 1");
384 #if defined(PYBIND11_HAS_U8STRING)
385  static_assert(!std::is_same<CharT, char8_t>::value || sizeof(CharT) == 1,
386  "Unsupported char8_t size != 1");
387 #endif
388  static_assert(!std::is_same<CharT, char16_t>::value || sizeof(CharT) == 2,
389  "Unsupported char16_t size != 2");
390  static_assert(!std::is_same<CharT, char32_t>::value || sizeof(CharT) == 4,
391  "Unsupported char32_t size != 4");
392  // wchar_t can be either 16 bits (Windows) or 32 (everywhere else)
393  static_assert(!std::is_same<CharT, wchar_t>::value || sizeof(CharT) == 2 || sizeof(CharT) == 4,
394  "Unsupported wchar_t size != 2/4");
395  static constexpr size_t UTF_N = 8 * sizeof(CharT);
396 
397  bool load(handle src, bool) {
398  handle load_src = src;
399  if (!src) {
400  return false;
401  }
402  if (!PyUnicode_Check(load_src.ptr())) {
403  return load_raw(load_src);
404  }
405 
406  // For UTF-8 we avoid the need for a temporary `bytes` object by using
407  // `PyUnicode_AsUTF8AndSize`.
408  if (UTF_N == 8) {
409  Py_ssize_t size = -1;
410  const auto *buffer
411  = reinterpret_cast<const CharT *>(PyUnicode_AsUTF8AndSize(load_src.ptr(), &size));
412  if (!buffer) {
413  PyErr_Clear();
414  return false;
415  }
416  value = StringType(buffer, static_cast<size_t>(size));
417  return true;
418  }
419 
420  auto utfNbytes
421  = reinterpret_steal<object>(PyUnicode_AsEncodedString(load_src.ptr(),
422  UTF_N == 8 ? "utf-8"
423  : UTF_N == 16 ? "utf-16"
424  : "utf-32",
425  nullptr));
426  if (!utfNbytes) {
427  PyErr_Clear();
428  return false;
429  }
430 
431  const auto *buffer
432  = reinterpret_cast<const CharT *>(PYBIND11_BYTES_AS_STRING(utfNbytes.ptr()));
433  size_t length = (size_t) PYBIND11_BYTES_SIZE(utfNbytes.ptr()) / sizeof(CharT);
434  // Skip BOM for UTF-16/32
435  if (UTF_N > 8) {
436  buffer++;
437  length--;
438  }
439  value = StringType(buffer, length);
440 
441  // If we're loading a string_view we need to keep the encoded Python object alive:
442  if (IsView) {
444  }
445 
446  return true;
447  }
448 
449  static handle
450  cast(const StringType &src, return_value_policy /* policy */, handle /* parent */) {
451  const char *buffer = reinterpret_cast<const char *>(src.data());
452  auto nbytes = ssize_t(src.size() * sizeof(CharT));
453  handle s = decode_utfN(buffer, nbytes);
454  if (!s) {
455  throw error_already_set();
456  }
457  return s;
458  }
459 
461 
462 private:
463  static handle decode_utfN(const char *buffer, ssize_t nbytes) {
464 #if !defined(PYPY_VERSION)
465  return UTF_N == 8 ? PyUnicode_DecodeUTF8(buffer, nbytes, nullptr)
466  : UTF_N == 16 ? PyUnicode_DecodeUTF16(buffer, nbytes, nullptr, nullptr)
467  : PyUnicode_DecodeUTF32(buffer, nbytes, nullptr, nullptr);
468 #else
469  // PyPy segfaults when on PyUnicode_DecodeUTF16 (and possibly on PyUnicode_DecodeUTF32 as
470  // well), so bypass the whole thing by just passing the encoding as a string value, which
471  // works properly:
472  return PyUnicode_Decode(buffer,
473  nbytes,
474  UTF_N == 8 ? "utf-8"
475  : UTF_N == 16 ? "utf-16"
476  : "utf-32",
477  nullptr);
478 #endif
479  }
480 
481  // When loading into a std::string or char*, accept a bytes/bytearray object as-is (i.e.
482  // without any encoding/decoding attempt). For other C++ char sizes this is a no-op.
483  // which supports loading a unicode from a str, doesn't take this path.
484  template <typename C = CharT>
486  if (PYBIND11_BYTES_CHECK(src.ptr())) {
487  // We were passed raw bytes; accept it into a std::string or char*
488  // without any encoding attempt.
489  const char *bytes = PYBIND11_BYTES_AS_STRING(src.ptr());
490  if (!bytes) {
491  pybind11_fail("Unexpected PYBIND11_BYTES_AS_STRING() failure.");
492  }
493  value = StringType(bytes, (size_t) PYBIND11_BYTES_SIZE(src.ptr()));
494  return true;
495  }
496  if (PyByteArray_Check(src.ptr())) {
497  // We were passed a bytearray; accept it into a std::string or char*
498  // without any encoding attempt.
499  const char *bytearray = PyByteArray_AsString(src.ptr());
500  if (!bytearray) {
501  pybind11_fail("Unexpected PyByteArray_AsString() failure.");
502  }
503  value = StringType(bytearray, (size_t) PyByteArray_Size(src.ptr()));
504  return true;
505  }
506 
507  return false;
508  }
509 
510  template <typename C = CharT>
512  return false;
513  }
514 };
515 
516 template <typename CharT, class Traits, class Allocator>
517 struct type_caster<std::basic_string<CharT, Traits, Allocator>,
518  enable_if_t<is_std_char_type<CharT>::value>>
519  : string_caster<std::basic_string<CharT, Traits, Allocator>> {};
520 
521 #ifdef PYBIND11_HAS_STRING_VIEW
522 template <typename CharT, class Traits>
523 struct type_caster<std::basic_string_view<CharT, Traits>,
524  enable_if_t<is_std_char_type<CharT>::value>>
525  : string_caster<std::basic_string_view<CharT, Traits>, true> {};
526 #endif
527 
528 // Type caster for C-style strings. We basically use a std::string type caster, but also add the
529 // ability to use None as a nullptr char* (which the string caster doesn't allow).
530 template <typename CharT>
532  using StringType = std::basic_string<CharT>;
535  bool none = false;
536  CharT one_char = 0;
537 
538 public:
539  bool load(handle src, bool convert) {
540  if (!src) {
541  return false;
542  }
543  if (src.is_none()) {
544  // Defer accepting None to other overloads (if we aren't in convert mode):
545  if (!convert) {
546  return false;
547  }
548  none = true;
549  return true;
550  }
551  return str_caster.load(src, convert);
552  }
553 
554  static handle cast(const CharT *src, return_value_policy policy, handle parent) {
555  if (src == nullptr) {
556  return pybind11::none().release();
557  }
558  return StringCaster::cast(StringType(src), policy, parent);
559  }
560 
561  static handle cast(CharT src, return_value_policy policy, handle parent) {
563  handle s = PyUnicode_DecodeLatin1((const char *) &src, 1, nullptr);
564  if (!s) {
565  throw error_already_set();
566  }
567  return s;
568  }
569  return StringCaster::cast(StringType(1, src), policy, parent);
570  }
571 
572  explicit operator CharT *() {
573  return none ? nullptr : const_cast<CharT *>(static_cast<StringType &>(str_caster).c_str());
574  }
575  explicit operator CharT &() {
576  if (none) {
577  throw value_error("Cannot convert None to a character");
578  }
579 
580  auto &value = static_cast<StringType &>(str_caster);
581  size_t str_len = value.size();
582  if (str_len == 0) {
583  throw value_error("Cannot convert empty string to a character");
584  }
585 
586  // If we're in UTF-8 mode, we have two possible failures: one for a unicode character that
587  // is too high, and one for multiple unicode characters (caught later), so we need to
588  // figure out how long the first encoded character is in bytes to distinguish between these
589  // two errors. We also allow want to allow unicode characters U+0080 through U+00FF, as
590  // those can fit into a single char value.
591  if (StringCaster::UTF_N == 8 && str_len > 1 && str_len <= 4) {
592  auto v0 = static_cast<unsigned char>(value[0]);
593  // low bits only: 0-127
594  // 0b110xxxxx - start of 2-byte sequence
595  // 0b1110xxxx - start of 3-byte sequence
596  // 0b11110xxx - start of 4-byte sequence
597  size_t char0_bytes = (v0 & 0x80) == 0 ? 1
598  : (v0 & 0xE0) == 0xC0 ? 2
599  : (v0 & 0xF0) == 0xE0 ? 3
600  : 4;
601 
602  if (char0_bytes == str_len) {
603  // If we have a 128-255 value, we can decode it into a single char:
604  if (char0_bytes == 2 && (v0 & 0xFC) == 0xC0) { // 0x110000xx 0x10xxxxxx
605  one_char = static_cast<CharT>(((v0 & 3) << 6)
606  + (static_cast<unsigned char>(value[1]) & 0x3F));
607  return one_char;
608  }
609  // Otherwise we have a single character, but it's > U+00FF
610  throw value_error("Character code point not in range(0x100)");
611  }
612  }
613 
614  // UTF-16 is much easier: we can only have a surrogate pair for values above U+FFFF, thus a
615  // surrogate pair with total length 2 instantly indicates a range error (but not a "your
616  // string was too long" error).
617  else if (StringCaster::UTF_N == 16 && str_len == 2) {
618  one_char = static_cast<CharT>(value[0]);
619  if (one_char >= 0xD800 && one_char < 0xE000) {
620  throw value_error("Character code point not in range(0x10000)");
621  }
622  }
623 
624  if (str_len != 1) {
625  throw value_error("Expected a character, but multi-character string found");
626  }
627 
628  one_char = value[0];
629  return one_char;
630  }
631 
632  static constexpr auto name = const_name(PYBIND11_STRING_NAME);
633  template <typename _T>
634  using cast_op_type = pybind11::detail::cast_op_type<_T>;
635 };
636 
637 // Base implementation for std::tuple and std::pair
638 template <template <typename...> class Tuple, typename... Ts>
640  using type = Tuple<Ts...>;
641  static constexpr auto size = sizeof...(Ts);
643 
644 public:
645  bool load(handle src, bool convert) {
646  if (!isinstance<sequence>(src)) {
647  return false;
648  }
649  const auto seq = reinterpret_borrow<sequence>(src);
650  if (seq.size() != size) {
651  return false;
652  }
653  return load_impl(seq, convert, indices{});
654  }
655 
656  template <typename T>
657  static handle cast(T &&src, return_value_policy policy, handle parent) {
658  return cast_impl(std::forward<T>(src), policy, parent, indices{});
659  }
660 
661  // copied from the PYBIND11_TYPE_CASTER macro
662  template <typename T>
663  static handle cast(T *src, return_value_policy policy, handle parent) {
664  if (!src) {
665  return none().release();
666  }
667  if (policy == return_value_policy::take_ownership) {
668  auto h = cast(std::move(*src), policy, parent);
669  delete src;
670  return h;
671  }
672  return cast(*src, policy, parent);
673  }
674 
675  static constexpr auto name = const_name("tuple[")
677  + const_name("]");
678 
679  template <typename T>
681 
682  explicit operator type() & { return implicit_cast(indices{}); }
683  explicit operator type() && { return std::move(*this).implicit_cast(indices{}); }
684 
685 protected:
686  template <size_t... Is>
688  return type(cast_op<Ts>(std::get<Is>(subcasters))...);
689  }
690  template <size_t... Is>
692  return type(cast_op<Ts>(std::move(std::get<Is>(subcasters)))...);
693  }
694 
695  static constexpr bool load_impl(const sequence &, bool, index_sequence<>) { return true; }
696 
697  template <size_t... Is>
699 #ifdef __cpp_fold_expressions
700  if ((... || !std::get<Is>(subcasters).load(seq[Is], convert))) {
701  return false;
702  }
703 #else
704  for (bool r : {std::get<Is>(subcasters).load(seq[Is], convert)...}) {
705  if (!r) {
706  return false;
707  }
708  }
709 #endif
710  return true;
711  }
712 
713  /* Implementation: Convert a C++ tuple into a Python tuple */
714  template <typename T, size_t... Is>
715  static handle
717  PYBIND11_WORKAROUND_INCORRECT_MSVC_C4100(src, policy, parent);
719  std::array<object, size> entries{{reinterpret_steal<object>(
720  make_caster<Ts>::cast(std::get<Is>(std::forward<T>(src)), policy, parent))...}};
721  for (const auto &entry : entries) {
722  if (!entry) {
723  return handle();
724  }
725  }
726  tuple result(size);
727  int counter = 0;
728  for (auto &entry : entries) {
729  PyTuple_SET_ITEM(result.ptr(), counter++, entry.release().ptr());
730  }
731  return result.release();
732  }
733 
735 };
736 
737 template <typename T1, typename T2>
738 class type_caster<std::pair<T1, T2>> : public tuple_caster<std::pair, T1, T2> {};
739 
740 template <typename... Ts>
741 class type_caster<std::tuple<Ts...>> : public tuple_caster<std::tuple, Ts...> {};
742 
743 template <>
744 class type_caster<std::tuple<>> : public tuple_caster<std::tuple> {
745 public:
746  // PEP 484 specifies this syntax for an empty tuple
747  static constexpr auto name = const_name("tuple[()]");
748 };
749 
752 template <typename T>
754  static auto get(const T &p) -> decltype(p.get()) { return p.get(); }
755 };
756 
762 template <typename type, typename holder_type, typename SFINAE = void>
764 public:
766  static_assert(std::is_base_of<base, type_caster<type>>::value,
767  "Holder classes are only supported for custom types");
768  using base::base;
769  using base::cast;
770  using base::typeinfo;
771  using base::value;
772 
773  bool load(handle src, bool convert) {
774  return base::template load_impl<copyable_holder_caster<type, holder_type>>(src, convert);
775  }
776 
777  explicit operator type *() { return this->value; }
778  // static_cast works around compiler error with MSVC 17 and CUDA 10.2
779  // see issue #2180
780  explicit operator type &() { return *(static_cast<type *>(this->value)); }
781  explicit operator holder_type *() { return std::addressof(holder); }
782  explicit operator holder_type &() { return holder; }
783 
784  static handle cast(const holder_type &src, return_value_policy, handle) {
785  const auto *ptr = holder_helper<holder_type>::get(src);
786  return type_caster_base<type>::cast_holder(ptr, &src);
787  }
788 
789 protected:
790  friend class type_caster_generic;
792  if (typeinfo->default_holder) {
793  throw cast_error("Unable to load a custom holder type from a default-holder instance");
794  }
795  }
796 
798  if (v_h.holder_constructed()) {
799  value = v_h.value_ptr();
800  holder = v_h.template holder<holder_type>();
801  return;
802  }
803  throw cast_error("Unable to cast from non-held to held instance (T& to Holder<T>) "
805  "(#define PYBIND11_DETAILED_ERROR_MESSAGES or compile in debug mode for "
806  "type information)");
807 #else
808  "of type '"
809  + type_id<holder_type>() + "''");
810 #endif
811  }
812 
813  template <typename T = holder_type,
816  return false;
817  }
818 
819  template <typename T = holder_type,
822  for (auto &cast : typeinfo->implicit_casts) {
823  copyable_holder_caster sub_caster(*cast.first);
824  if (sub_caster.load(src, convert)) {
825  value = cast.second(sub_caster.value);
826  holder = holder_type(sub_caster.holder, (type *) value);
827  return true;
828  }
829  }
830  return false;
831  }
832 
833  static bool try_direct_conversions(handle) { return false; }
834 
835  holder_type holder;
836 };
837 
839 template <typename T>
840 class type_caster<std::shared_ptr<T>> : public copyable_holder_caster<T, std::shared_ptr<T>> {};
841 
845 template <typename type, typename holder_type, typename SFINAE = void>
847  static_assert(std::is_base_of<type_caster_base<type>, type_caster<type>>::value,
848  "Holder classes are only supported for custom types");
849 
850  static handle cast(holder_type &&src, return_value_policy, handle) {
851  auto *ptr = holder_helper<holder_type>::get(src);
852  return type_caster_base<type>::cast_holder(ptr, std::addressof(src));
853  }
854  static constexpr auto name = type_caster_base<type>::name;
855 };
856 
857 template <typename type, typename deleter>
858 class type_caster<std::unique_ptr<type, deleter>>
859  : public move_only_holder_caster<type, std::unique_ptr<type, deleter>> {};
860 
861 template <typename type, typename holder_type>
865 
866 template <typename T, bool Value = false>
868  static constexpr bool value = Value;
869 };
870 
872 #define PYBIND11_DECLARE_HOLDER_TYPE(type, holder_type, ...) \
873  PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE) \
874  namespace detail { \
875  template <typename type> \
876  struct always_construct_holder<holder_type> : always_construct_holder<void, ##__VA_ARGS__> { \
877  }; \
878  template <typename type> \
879  class type_caster<holder_type, enable_if_t<!is_shared_ptr<holder_type>::value>> \
880  : public type_caster_holder<type, holder_type> {}; \
881  } \
882  PYBIND11_NAMESPACE_END(PYBIND11_NAMESPACE)
883 
884 // PYBIND11_DECLARE_HOLDER_TYPE holder types:
885 template <typename base, typename holder>
887  : std::is_base_of<detail::type_caster_holder<base, holder>, detail::type_caster<holder>> {};
888 // Specialization for always-supported unique_ptr holders:
889 template <typename base, typename deleter>
890 struct is_holder_type<base, std::unique_ptr<base, deleter>> : std::true_type {};
891 
892 #ifdef PYBIND11_DISABLE_HANDLE_TYPE_NAME_DEFAULT_IMPLEMENTATION // See PR #4888
893 
894 // This leads to compilation errors if a specialization is missing.
895 template <typename T>
896 struct handle_type_name;
897 
898 #else
899 
900 template <typename T>
902  static constexpr auto name = const_name<T>();
903 };
904 
905 #endif
906 
907 template <>
909  static constexpr auto name = const_name("object");
910 };
911 template <>
913  static constexpr auto name = const_name("list");
914 };
915 template <>
917  static constexpr auto name = const_name("dict");
918 };
919 template <>
921  static constexpr auto name = const_name("Union[set, frozenset]");
922 };
923 template <>
925  static constexpr auto name = const_name("set");
926 };
927 template <>
929  static constexpr auto name = const_name("frozenset");
930 };
931 template <>
933  static constexpr auto name = const_name("str");
934 };
935 template <>
937  static constexpr auto name = const_name("tuple");
938 };
939 template <>
941  static constexpr auto name = const_name("bool");
942 };
943 template <>
945  static constexpr auto name = const_name(PYBIND11_BYTES_NAME);
946 };
947 template <>
949  static constexpr auto name = const_name("Buffer");
950 };
951 template <>
953  static constexpr auto name = const_name("int");
954 };
955 template <>
957  static constexpr auto name = const_name("Iterable");
958 };
959 template <>
961  static constexpr auto name = const_name("Iterator");
962 };
963 template <>
965  static constexpr auto name = const_name("float");
966 };
967 template <>
969  static constexpr auto name = const_name("Callable");
970 };
971 template <>
973  static constexpr auto name = handle_type_name<object>::name;
974 };
975 template <>
977  static constexpr auto name = const_name("None");
978 };
979 template <>
981  static constexpr auto name = const_name("Sequence");
982 };
983 template <>
985  static constexpr auto name = const_name("bytearray");
986 };
987 template <>
989  static constexpr auto name = const_name("memoryview");
990 };
991 template <>
993  static constexpr auto name = const_name("slice");
994 };
995 template <>
997  static constexpr auto name = const_name("type");
998 };
999 template <>
1001  static constexpr auto name = const_name("capsule");
1002 };
1003 template <>
1005  static constexpr auto name = const_name("ellipsis");
1006 };
1007 template <>
1009  static constexpr auto name = const_name("weakref");
1010 };
1011 template <>
1013  static constexpr auto name = const_name("*args");
1014 };
1015 template <>
1017  static constexpr auto name = const_name("**kwargs");
1018 };
1019 template <>
1021  static constexpr auto name = const_name<obj_attr_accessor>();
1022 };
1023 template <>
1025  static constexpr auto name = const_name<str_attr_accessor>();
1026 };
1027 template <>
1029  static constexpr auto name = const_name<item_accessor>();
1030 };
1031 template <>
1033  static constexpr auto name = const_name<sequence_accessor>();
1034 };
1035 template <>
1037  static constexpr auto name = const_name<list_accessor>();
1038 };
1039 template <>
1041  static constexpr auto name = const_name<tuple_accessor>();
1042 };
1043 
1044 template <typename type>
1048 
1049  // `type` may not be default constructible (e.g. frozenset, anyset). Initializing `value`
1050  // to a nil handle is safe since it will only be accessed if `load` succeeds.
1053 
1055  bool load(handle src, bool /* convert */) {
1056  value = src;
1057  return static_cast<bool>(value);
1058  }
1059 
1061  bool load(handle src, bool /* convert */) {
1062  if (!isinstance<type>(src)) {
1063  return false;
1064  }
1065  value = reinterpret_borrow<type>(src);
1066  return true;
1067  }
1068 
1069  static handle cast(const handle &src, return_value_policy /* policy */, handle /* parent */) {
1070  return src.inc_ref();
1071  }
1073 };
1074 
1075 template <typename T>
1077 
1078 // Our conditions for enabling moving are quite restrictive:
1079 // At compile time:
1080 // - T needs to be a non-const, non-pointer, non-reference type
1081 // - type_caster<T>::operator T&() must exist
1082 // - the type must be move constructible (obviously)
1083 // At run-time:
1084 // - if the type is non-copy-constructible, the object must be the sole owner of the type (i.e. it
1085 // must have ref_count() == 1)h
1086 // If any of the above are not satisfied, we fall back to copying.
1087 template <typename T>
1088 using move_is_plain_type
1090 template <typename T, typename SFINAE = void>
1091 struct move_always : std::false_type {};
1092 template <typename T>
1094  T,
1095  enable_if_t<
1099  std::is_same<decltype(std::declval<make_caster<T>>().operator T &()), T &>>::value>>
1100  : std::true_type {};
1101 template <typename T, typename SFINAE = void>
1102 struct move_if_unreferenced : std::false_type {};
1103 template <typename T>
1105  T,
1106  enable_if_t<
1110  std::is_same<decltype(std::declval<make_caster<T>>().operator T &()), T &>>::value>>
1111  : std::true_type {};
1112 template <typename T>
1114 
1115 // Detect whether returning a `type` from a cast on type's type_caster is going to result in a
1116 // reference or pointer to a local variable of the type_caster. Basically, only
1117 // non-reference/pointer `type`s and reference/pointers from a type_caster_generic are safe;
1118 // everything else returns a reference/pointer to a local variable.
1119 template <typename type>
1122  && !std::is_base_of<type_caster_generic, make_caster<type>>::value
1123  && !std::is_same<intrinsic_t<type>, void>::value>;
1124 
1125 // When a value returned from a C++ function is being cast back to Python, we almost always want to
1126 // force `policy = move`, regardless of the return value policy the function/method was declared
1127 // with.
1128 template <typename Return, typename SFINAE = void>
1131 };
1132 
1133 template <typename Return>
1135  Return,
1136  detail::enable_if_t<std::is_base_of<type_caster_generic, make_caster<Return>>::value, void>> {
1140  : p;
1141  }
1142 };
1143 
1144 // Basic python -> C++ casting; throws if casting fails
1145 template <typename T, typename SFINAE>
1147  static_assert(!detail::is_pyobject<T>::value,
1148  "Internal error: type_caster should only be used for C++ types");
1149  if (!conv.load(handle, true)) {
1150 #if !defined(PYBIND11_DETAILED_ERROR_MESSAGES)
1151  throw cast_error(
1152  "Unable to cast Python instance of type "
1153  + str(type::handle_of(handle)).cast<std::string>()
1154  + " to C++ type '?' (#define "
1155  "PYBIND11_DETAILED_ERROR_MESSAGES or compile in debug mode for details)");
1156 #else
1157  throw cast_error("Unable to cast Python instance of type "
1158  + str(type::handle_of(handle)).cast<std::string>() + " to C++ type '"
1159  + type_id<T>() + "'");
1160 #endif
1161  }
1162  return conv;
1163 }
1164 // Wrapper around the above that also constructs and returns a type_caster
1165 template <typename T>
1168  load_type(conv, handle);
1169  return conv;
1170 }
1171 
1173 
1174 // pytype -> C++ type
1175 template <typename T,
1178  int>
1179  = 0>
1180 T cast(const handle &handle) {
1181  using namespace detail;
1183  "Unable to cast type to reference: value is local to type caster");
1184  return cast_op<T>(load_type<T>(handle));
1185 }
1186 
1187 // pytype -> pytype (calls converting constructor)
1189 T cast(const handle &handle) {
1190  return T(reinterpret_borrow<object>(handle));
1191 }
1192 
1193 // Note that `cast<PyObject *>(obj)` increments the reference count of `obj`.
1194 // This is necessary for the case that `obj` is a temporary, and could
1195 // not possibly be different, given
1196 // 1. the established convention that the passed `handle` is borrowed, and
1197 // 2. we don't want to force all generic code using `cast<T>()` to special-case
1198 // handling of `T` = `PyObject *` (to increment the reference count there).
1199 // It is the responsibility of the caller to ensure that the reference count
1200 // is decremented.
1201 template <typename T,
1202  typename Handle,
1205  int>
1206  = 0>
1207 T cast(Handle &&handle) {
1208  return handle.inc_ref().ptr();
1209 }
1210 // To optimize way an inc_ref/dec_ref cycle:
1211 template <typename T,
1212  typename Object,
1215  int>
1216  = 0>
1217 T cast(Object &&obj) {
1218  return obj.release().ptr();
1219 }
1220 
1221 // C++ type -> py::object
1223 object cast(T &&value,
1225  handle parent = handle()) {
1226  using no_ref_T = typename std::remove_reference<T>::type;
1227  if (policy == return_value_policy::automatic) {
1231  } else if (policy == return_value_policy::automatic_reference) {
1235  }
1236  return reinterpret_steal<object>(
1237  detail::make_caster<T>::cast(std::forward<T>(value), policy, parent));
1238 }
1239 
1240 template <typename T>
1241 T handle::cast() const {
1242  return pybind11::cast<T>(*this);
1243 }
1244 template <>
1245 inline void handle::cast() const {
1246  return;
1247 }
1248 
1249 template <typename T>
1251  if (obj.ref_count() > 1) {
1252 #if !defined(PYBIND11_DETAILED_ERROR_MESSAGES)
1253  throw cast_error(
1254  "Unable to cast Python " + str(type::handle_of(obj)).cast<std::string>()
1255  + " instance to C++ rvalue: instance has multiple references"
1256  " (#define PYBIND11_DETAILED_ERROR_MESSAGES or compile in debug mode for details)");
1257 #else
1258  throw cast_error("Unable to move from Python "
1259  + str(type::handle_of(obj)).cast<std::string>() + " instance to C++ "
1260  + type_id<T>() + " instance: instance has multiple references");
1261 #endif
1262  }
1263 
1264  // Move into a temporary and return that, because the reference may be a local value of `conv`
1265  T ret = std::move(detail::load_type<T>(obj).operator T &());
1266  return ret;
1267 }
1268 
1269 // Calling cast() on an rvalue calls pybind11::cast with the object rvalue, which does:
1270 // - If we have to move (because T has no copy constructor), do it. This will fail if the moved
1271 // object has multiple references, but trying to copy will fail to compile.
1272 // - If both movable and copyable, check ref count: if 1, move; otherwise copy
1273 // - Otherwise (not movable), copy.
1274 template <typename T>
1276 cast(object &&object) {
1277  return move<T>(std::move(object));
1278 }
1279 template <typename T>
1281 cast(object &&object) {
1282  if (object.ref_count() > 1) {
1283  return cast<T>(object);
1284  }
1285  return move<T>(std::move(object));
1286 }
1287 template <typename T>
1289 cast(object &&object) {
1290  return cast<T>(object);
1291 }
1292 
1293 // pytype rvalue -> pytype (calls converting constructor)
1294 template <typename T>
1296  return T(std::move(object));
1297 }
1298 
1299 template <typename T>
1300 T object::cast() const & {
1301  return pybind11::cast<T>(*this);
1302 }
1303 template <typename T>
1304 T object::cast() && {
1305  return pybind11::cast<T>(std::move(*this));
1306 }
1307 template <>
1308 inline void object::cast() const & {
1309  return;
1310 }
1311 template <>
1312 inline void object::cast() && {
1313  return;
1314 }
1315 
1317 
1318 // Declared in pytypes.h:
1320 object object_or_cast(T &&o) {
1321  return pybind11::cast(std::forward<T>(o));
1322 }
1323 
1324 // Placeholder type for the unneeded (and dead code) static variable in the
1325 // PYBIND11_OVERRIDE_OVERRIDE macro
1327 template <typename ret_type>
1331 
1332 // Trampoline use: for reference/pointer types to value-converted values, we do a value cast, then
1333 // store the result in the given variable. For other types, this is a no-op.
1334 template <typename T>
1336  make_caster<T> &caster) {
1337  return cast_op<T>(load_type(caster, o));
1338 }
1339 template <typename T>
1341  override_unused &) {
1342  pybind11_fail("Internal error: cast_ref fallback invoked");
1343 }
1344 
1345 // Trampoline use: Having a pybind11::cast with an invalid reference type is going to
1346 // static_assert, even though if it's in dead code, so we provide a "trampoline" to pybind11::cast
1347 // that only does anything in cases where pybind11::cast is valid.
1348 template <typename T>
1351  T>
1352 cast_safe(object &&) {
1353  pybind11_fail("Internal error: cast_safe fallback invoked");
1354 }
1355 template <typename T>
1357 template <typename T>
1359 cast_safe(object &&o) {
1360  return o.release().ptr();
1361 }
1362 template <typename T>
1364  detail::is_same_ignoring_cvref<T, PyObject *>,
1365  std::is_void<T>>::value,
1366  T>
1367 cast_safe(object &&o) {
1368  return pybind11::cast<T>(std::move(o));
1369 }
1370 
1372 
1373 // The overloads could coexist, i.e. the #if is not strictly speaking needed,
1374 // but it is an easy minor optimization.
1375 #if !defined(PYBIND11_DETAILED_ERROR_MESSAGES)
1376 inline cast_error cast_error_unable_to_convert_call_arg(const std::string &name) {
1377  return cast_error("Unable to convert call argument '" + name
1378  + "' to Python object (#define "
1379  "PYBIND11_DETAILED_ERROR_MESSAGES or compile in debug mode for details)");
1380 }
1381 #else
1382 inline cast_error cast_error_unable_to_convert_call_arg(const std::string &name,
1383  const std::string &type) {
1384  return cast_error("Unable to convert call argument '" + name + "' of type '" + type
1385  + "' to Python object");
1386 }
1387 #endif
1388 
1389 template <return_value_policy policy = return_value_policy::automatic_reference>
1391  return tuple(0);
1392 }
1393 
1394 template <return_value_policy policy = return_value_policy::automatic_reference, typename... Args>
1395 tuple make_tuple(Args &&...args_) {
1396  constexpr size_t size = sizeof...(Args);
1397  std::array<object, size> args{{reinterpret_steal<object>(
1398  detail::make_caster<Args>::cast(std::forward<Args>(args_), policy, nullptr))...}};
1399  for (size_t i = 0; i < args.size(); i++) {
1400  if (!args[i]) {
1401 #if !defined(PYBIND11_DETAILED_ERROR_MESSAGES)
1402  throw cast_error_unable_to_convert_call_arg(std::to_string(i));
1403 #else
1404  std::array<std::string, size> argtypes{{type_id<Args>()...}};
1405  throw cast_error_unable_to_convert_call_arg(std::to_string(i), argtypes[i]);
1406 #endif
1407  }
1408  }
1409  tuple result(size);
1410  int counter = 0;
1411  for (auto &arg_value : args) {
1412  PyTuple_SET_ITEM(result.ptr(), counter++, arg_value.release().ptr());
1413  }
1414  return result;
1415 }
1416 
1419 struct arg {
1422  constexpr explicit arg(const char *name = nullptr)
1423  : name(name), flag_noconvert(false), flag_none(true) {}
1425  template <typename T>
1426  arg_v operator=(T &&value) const;
1428  arg &noconvert(bool flag = true) {
1429  flag_noconvert = flag;
1430  return *this;
1431  }
1433  arg &none(bool flag = true) {
1434  flag_none = flag;
1435  return *this;
1436  }
1437 
1438  const char *name;
1439  bool flag_noconvert : 1;
1440  bool flag_none : 1;
1442 };
1443 
1446 struct arg_v : arg {
1447 private:
1448  template <typename T>
1449  arg_v(arg &&base, T &&x, const char *descr = nullptr)
1452  descr(descr)
1453 #if defined(PYBIND11_DETAILED_ERROR_MESSAGES)
1454  ,
1455  type(type_id<T>())
1456 #endif
1457  {
1458  // Workaround! See:
1459  // https://github.com/pybind/pybind11/issues/2336
1460  // https://github.com/pybind/pybind11/pull/2685#issuecomment-731286700
1461  if (PyErr_Occurred()) {
1462  PyErr_Clear();
1463  }
1464  }
1465 
1466 public:
1468  template <typename T>
1469  arg_v(const char *name, T &&x, const char *descr = nullptr)
1470  : arg_v(arg(name), std::forward<T>(x), descr) {}
1471 
1473  template <typename T>
1474  arg_v(const arg &base, T &&x, const char *descr = nullptr)
1475  : arg_v(arg(base), std::forward<T>(x), descr) {}
1476 
1478  arg_v &noconvert(bool flag = true) {
1479  arg::noconvert(flag);
1480  return *this;
1481  }
1482 
1484  arg_v &none(bool flag = true) {
1485  arg::none(flag);
1486  return *this;
1487  }
1488 
1490  object value;
1492  const char *descr;
1493 #if defined(PYBIND11_DETAILED_ERROR_MESSAGES)
1494  std::string type;
1496 #endif
1497 };
1498 
1502 struct kw_only {};
1503 
1507 struct pos_only {};
1508 
1509 template <typename T>
1511  return {*this, std::forward<T>(value)};
1512 }
1513 
1515 template <typename /*unused*/>
1516 using arg_t = arg_v;
1517 
1518 inline namespace literals {
1522 constexpr arg
1523 #if !defined(__clang__) && defined(__GNUC__) && __GNUC__ < 5
1524 operator"" _a // gcc 4.8.5 insists on having a space (hard error).
1525 #else
1526 operator""_a // clang 17 generates a deprecation warning if there is a space.
1527 #endif
1528  (const char *name, size_t) {
1529  return arg(name);
1530 }
1531 } // namespace literals
1532 
1534 
1535 template <typename T>
1536 using is_kw_only = std::is_same<intrinsic_t<T>, kw_only>;
1537 template <typename T>
1538 using is_pos_only = std::is_same<intrinsic_t<T>, pos_only>;
1539 
1540 // forward declaration (definition in attr.h)
1541 struct function_record;
1542 
1545  function_call(const function_record &f, handle p); // Implementation in attr.h
1546 
1549 
1551  std::vector<handle> args;
1552 
1554  std::vector<bool> args_convert;
1555 
1559 
1562 
1565 };
1566 
1568 template <typename... Args>
1570  using indices = make_index_sequence<sizeof...(Args)>;
1571 
1572  template <typename Arg>
1573  using argument_is_args = std::is_same<intrinsic_t<Arg>, args>;
1574  template <typename Arg>
1575  using argument_is_kwargs = std::is_same<intrinsic_t<Arg>, kwargs>;
1576  // Get kwargs argument position, or -1 if not present:
1577  static constexpr auto kwargs_pos = constexpr_last<argument_is_kwargs, Args...>();
1578 
1579  static_assert(kwargs_pos == -1 || kwargs_pos == (int) sizeof...(Args) - 1,
1580  "py::kwargs is only permitted as the last argument of a function");
1581 
1582 public:
1583  static constexpr bool has_kwargs = kwargs_pos != -1;
1584 
1585  // py::args argument position; -1 if not present.
1586  static constexpr int args_pos = constexpr_last<argument_is_args, Args...>();
1587 
1588  static_assert(args_pos == -1 || args_pos == constexpr_first<argument_is_args, Args...>(),
1589  "py::args cannot be specified more than once");
1590 
1591  static constexpr auto arg_names
1593 
1595 
1596  template <typename Return, typename Guard, typename Func>
1597  // NOLINTNEXTLINE(readability-const-return-type)
1599  return std::move(*this).template call_impl<remove_cv_t<Return>>(
1600  std::forward<Func>(f), indices{}, Guard{});
1601  }
1602 
1603  template <typename Return, typename Guard, typename Func>
1605  std::move(*this).template call_impl<remove_cv_t<Return>>(
1606  std::forward<Func>(f), indices{}, Guard{});
1607  return void_type();
1608  }
1609 
1610 private:
1611  static bool load_impl_sequence(function_call &, index_sequence<>) { return true; }
1612 
1613  template <size_t... Is>
1615 #ifdef __cpp_fold_expressions
1616  if ((... || !std::get<Is>(argcasters).load(call.args[Is], call.args_convert[Is]))) {
1617  return false;
1618  }
1619 #else
1620  for (bool r : {std::get<Is>(argcasters).load(call.args[Is], call.args_convert[Is])...}) {
1621  if (!r) {
1622  return false;
1623  }
1624  }
1625 #endif
1626  return true;
1627  }
1628 
1629  template <typename Return, typename Func, size_t... Is, typename Guard>
1630  Return call_impl(Func &&f, index_sequence<Is...>, Guard &&) && {
1631  return std::forward<Func>(f)(cast_op<Args>(std::move(std::get<Is>(argcasters)))...);
1632  }
1633 
1634  std::tuple<make_caster<Args>...> argcasters;
1635 };
1636 
1639 template <return_value_policy policy>
1641 public:
1642  template <typename... Ts>
1643  explicit simple_collector(Ts &&...values)
1644  : m_args(pybind11::make_tuple<policy>(std::forward<Ts>(values)...)) {}
1645 
1646  const tuple &args() const & { return m_args; }
1647  dict kwargs() const { return {}; }
1648 
1649  tuple args() && { return std::move(m_args); }
1650 
1652  object call(PyObject *ptr) const {
1653  PyObject *result = PyObject_CallObject(ptr, m_args.ptr());
1654  if (!result) {
1655  throw error_already_set();
1656  }
1657  return reinterpret_steal<object>(result);
1658  }
1659 
1660 private:
1662 };
1663 
1665 template <return_value_policy policy>
1667 public:
1668  template <typename... Ts>
1669  explicit unpacking_collector(Ts &&...values) {
1670  // Tuples aren't (easily) resizable so a list is needed for collection,
1671  // but the actual function call strictly requires a tuple.
1672  auto args_list = list();
1673  using expander = int[];
1674  (void) expander{0, (process(args_list, std::forward<Ts>(values)), 0)...};
1675 
1676  m_args = std::move(args_list);
1677  }
1678 
1679  const tuple &args() const & { return m_args; }
1680  const dict &kwargs() const & { return m_kwargs; }
1681 
1682  tuple args() && { return std::move(m_args); }
1683  dict kwargs() && { return std::move(m_kwargs); }
1684 
1686  object call(PyObject *ptr) const {
1687  PyObject *result = PyObject_Call(ptr, m_args.ptr(), m_kwargs.ptr());
1688  if (!result) {
1689  throw error_already_set();
1690  }
1691  return reinterpret_steal<object>(result);
1692  }
1693 
1694 private:
1695  template <typename T>
1696  void process(list &args_list, T &&x) {
1697  auto o = reinterpret_steal<object>(
1698  detail::make_caster<T>::cast(std::forward<T>(x), policy, {}));
1699  if (!o) {
1700 #if !defined(PYBIND11_DETAILED_ERROR_MESSAGES)
1701  throw cast_error_unable_to_convert_call_arg(std::to_string(args_list.size()));
1702 #else
1703  throw cast_error_unable_to_convert_call_arg(std::to_string(args_list.size()),
1704  type_id<T>());
1705 #endif
1706  }
1707  args_list.append(std::move(o));
1708  }
1709 
1710  void process(list &args_list, detail::args_proxy ap) {
1711  for (auto a : ap) {
1712  args_list.append(a);
1713  }
1714  }
1715 
1716  void process(list & /*args_list*/, arg_v a) {
1717  if (!a.name) {
1718 #if !defined(PYBIND11_DETAILED_ERROR_MESSAGES)
1720 #else
1721  nameless_argument_error(a.type);
1722 #endif
1723  }
1724  if (m_kwargs.contains(a.name)) {
1725 #if !defined(PYBIND11_DETAILED_ERROR_MESSAGES)
1727 #else
1728  multiple_values_error(a.name);
1729 #endif
1730  }
1731  if (!a.value) {
1732 #if !defined(PYBIND11_DETAILED_ERROR_MESSAGES)
1734 #else
1735  throw cast_error_unable_to_convert_call_arg(a.name, a.type);
1736 #endif
1737  }
1738  m_kwargs[a.name] = std::move(a.value);
1739  }
1740 
1741  void process(list & /*args_list*/, detail::kwargs_proxy kp) {
1742  if (!kp) {
1743  return;
1744  }
1745  for (auto k : reinterpret_borrow<dict>(kp)) {
1746  if (m_kwargs.contains(k.first)) {
1747 #if !defined(PYBIND11_DETAILED_ERROR_MESSAGES)
1749 #else
1750  multiple_values_error(str(k.first));
1751 #endif
1752  }
1753  m_kwargs[k.first] = k.second;
1754  }
1755  }
1756 
1757  [[noreturn]] static void nameless_argument_error() {
1758  throw type_error(
1759  "Got kwargs without a name; only named arguments "
1760  "may be passed via py::arg() to a python function call. "
1761  "(#define PYBIND11_DETAILED_ERROR_MESSAGES or compile in debug mode for details)");
1762  }
1763  [[noreturn]] static void nameless_argument_error(const std::string &type) {
1764  throw type_error("Got kwargs without a name of type '" + type
1765  + "'; only named "
1766  "arguments may be passed via py::arg() to a python function call. ");
1767  }
1768  [[noreturn]] static void multiple_values_error() {
1769  throw type_error(
1770  "Got multiple values for keyword argument "
1771  "(#define PYBIND11_DETAILED_ERROR_MESSAGES or compile in debug mode for details)");
1772  }
1773 
1774  [[noreturn]] static void multiple_values_error(const std::string &name) {
1775  throw type_error("Got multiple values for keyword argument '" + name + "'");
1776  }
1777 
1778 private:
1781 };
1782 
1783 // [workaround(intel)] Separate function required here
1784 // We need to put this into a separate function because the Intel compiler
1785 // fails to compile enable_if_t<!all_of<is_positional<Args>...>::value>
1786 // (tested with ICC 2021.1 Beta 20200827).
1787 template <typename... Args>
1788 constexpr bool args_are_all_positional() {
1790 }
1791 
1793 template <return_value_policy policy,
1794  typename... Args,
1795  typename = enable_if_t<args_are_all_positional<Args...>()>>
1797  return simple_collector<policy>(std::forward<Args>(args)...);
1798 }
1799 
1801 template <return_value_policy policy,
1802  typename... Args,
1803  typename = enable_if_t<!args_are_all_positional<Args...>()>>
1805  // Following argument order rules for generalized unpacking according to PEP 448
1806  static_assert(constexpr_last<is_positional, Args...>()
1807  < constexpr_first<is_keyword_or_ds, Args...>()
1808  && constexpr_last<is_s_unpacking, Args...>()
1809  < constexpr_first<is_ds_unpacking, Args...>(),
1810  "Invalid function call: positional args must precede keywords and ** unpacking; "
1811  "* unpacking must precede ** unpacking");
1812  return unpacking_collector<policy>(std::forward<Args>(args)...);
1813 }
1814 
1815 template <typename Derived>
1816 template <return_value_policy policy, typename... Args>
1817 object object_api<Derived>::operator()(Args &&...args) const {
1818 #ifndef NDEBUG
1819  if (!PyGILState_Check()) {
1820  pybind11_fail("pybind11::object_api<>::operator() PyGILState_Check() failure.");
1821  }
1822 #endif
1823  return detail::collect_arguments<policy>(std::forward<Args>(args)...).call(derived().ptr());
1824 }
1825 
1826 template <typename Derived>
1827 template <return_value_policy policy, typename... Args>
1828 object object_api<Derived>::call(Args &&...args) const {
1829  return operator()<policy>(std::forward<Args>(args)...);
1830 }
1831 
1833 
1834 template <typename T>
1836  static_assert(std::is_base_of<detail::type_caster_generic, detail::make_caster<T>>::value,
1837  "py::type::of<T> only supports the case where T is a registered C++ types.");
1838 
1839  return detail::get_type_handle(typeid(T), true);
1840 }
1841 
1842 #define PYBIND11_MAKE_OPAQUE(...) \
1843  PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE) \
1844  namespace detail { \
1845  template <> \
1846  class type_caster<__VA_ARGS__> : public type_caster_base<__VA_ARGS__> {}; \
1847  } \
1848  PYBIND11_NAMESPACE_END(PYBIND11_NAMESPACE)
1849 
1853 #define PYBIND11_TYPE(...) __VA_ARGS__
1854 
type_caster< CharT, enable_if_t< is_std_char_type< CharT >::value > >::str_caster
StringCaster str_caster
Definition: cast.h:534
tuple_caster::cast
static handle cast(T *src, return_value_policy policy, handle parent)
Definition: cast.h:663
int_
Definition: pytypes.h:1837
pyobject_caster::pyobject_caster
pyobject_caster()
Definition: cast.h:1047
load_type
type_caster< T, SFINAE > & load_type(type_caster< T, SFINAE > &conv, const handle &handle)
Definition: cast.h:1146
PYBIND11_BYTES_SIZE
#define PYBIND11_BYTES_SIZE
Definition: wrap/pybind11/include/pybind11/detail/common.h:365
unpacking_collector::args
tuple args() &&
Definition: cast.h:1682
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:759
unpacking_collector::m_args
tuple m_args
Definition: cast.h:1779
external::detail::conv
PyObject * conv(PyObject *o)
Definition: test_pytypes.cpp:28
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:224
unpacking_collector::process
void process(list &args_list, T &&x)
Definition: cast.h:1696
name
Annotation for function names.
Definition: attr.h:51
return_value_policy_override
Definition: cast.h:1129
function_record
Definition: attr.h:191
cast
T cast(const handle &handle)
Definition: cast.h:1180
string_caster::UTF_N
static constexpr size_t UTF_N
Definition: cast.h:395
base
Annotation indicating that a class derives from another given type.
Definition: attr.h:64
bytes
Definition: pytypes.h:1664
type_caster< void >::cast
static handle cast(const void *ptr, return_value_policy, handle)
Definition: cast.h:296
set
Definition: pytypes.h:2234
index_sequence
Index sequences.
Definition: wrap/pybind11/include/pybind11/detail/common.h:704
argument_loader::load_args
bool load_args(function_call &call)
Definition: cast.h:1594
arg::arg
constexpr arg(const char *name=nullptr)
Definition: cast.h:1422
gtsam.examples.DogLegOptimizerExample.type
type
Definition: DogLegOptimizerExample.py:111
ssize_t
Py_ssize_t ssize_t
Definition: wrap/pybind11/include/pybind11/detail/common.h:508
type_caster< bool >::cast
static handle cast(bool src, return_value_policy, handle)
Definition: cast.h:360
error_already_set
Definition: pytypes.h:739
kwargs
Definition: pytypes.h:2215
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:639
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:561
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:518
void_caster::load
bool load(handle src, bool)
Definition: cast.h:250
list
Definition: pytypes.h:2168
type_info::implicit_casts
std::vector< std::pair< const std::type_info *, void *(*)(void *)> > implicit_casts
Definition: internals.h:249
copyable_holder_caster::cast
static handle cast(const holder_type &src, return_value_policy, handle)
Definition: cast.h:784
type_caster_generic::value
void * value
Definition: type_caster_base.h:783
remove_cv_t
typename std::remove_cv< T >::type remove_cv_t
Definition: wrap/pybind11/include/pybind11/detail/common.h:677
literals
Definition: cast.h:1518
argument_loader::call
enable_if_t<!std::is_void< Return >::value, Return > call(Func &&f) &&
Definition: cast.h:1598
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:258
function_call::func
const function_record & func
The function data:
Definition: cast.h:1548
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:1137
ret
DenseIndex ret
Definition: level1_cplx_impl.h:44
type_info::default_holder
bool default_holder
Definition: internals.h:262
values_and_holders::begin
iterator begin()
Definition: type_caster_base.h:319
argument_loader::argument_is_args
std::is_same< intrinsic_t< Arg >, args > argument_is_args
Definition: cast.h:1573
type_caster< CharT, enable_if_t< is_std_char_type< CharT >::value > >::StringType
std::basic_string< CharT > StringType
Definition: cast.h:532
simple_collector::args
tuple args() &&
Definition: cast.h:1649
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:1774
hasattr
bool hasattr(handle obj, handle name)
Definition: pytypes.h:870
capsule
Definition: pytypes.h:1953
move_only_holder_caster::cast
static handle cast(holder_type &&src, return_value_policy, handle)
Definition: cast.h:850
arg_v::arg_v
arg_v(arg &&base, T &&x, const char *descr=nullptr)
Definition: cast.h:1449
tuple_caster::type
Tuple< Ts... > type
Definition: cast.h:640
copyable_holder_caster::load_value
void load_value(value_and_holder &&v_h)
Definition: cast.h:797
void_caster
Definition: cast.h:248
type
Definition: pytypes.h:1527
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:130
move_always
Definition: cast.h:1091
value_and_holder
Definition: value_and_holder.h:15
T
Eigen::Triplet< double > T
Definition: Tutorial_sparse_example.cpp:6
object_api::operator()
object operator()(Args &&...args) const
Definition: cast.h:1817
argument_loader::load_impl_sequence
bool load_impl_sequence(function_call &call, index_sequence< Is... >)
Definition: cast.h:1614
argument_loader::arg_names
static constexpr auto arg_names
Definition: cast.h:1592
different_sigmas::values
HybridValues values
Definition: testHybridBayesNet.cpp:247
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:554
cast_ref
enable_if_t< cast_is_temporary_value_reference< T >::value, T > cast_ref(object &&o, make_caster< T > &caster)
Definition: cast.h:1335
detail
Definition: testSerializationNonlinear.cpp:69
function_call::function_call
function_call(const function_record &f, handle p)
Definition: attr.h:366
is_holder_type
Definition: cast.h:886
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:216
buffer
Definition: pytypes.h:2272
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:1796
iterator
Definition: pytypes.h:1462
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:986
arg::operator=
arg_v operator=(T &&value) const
Assign a value to this argument.
Definition: cast.h:1510
always_construct_holder
Definition: cast.h:867
all_type_info
const std::vector< detail::type_info * > & all_type_info(PyTypeObject *type)
Definition: type_caster_base.h:178
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:716
descr
Definition: descr.h:25
type_caster< std::reference_wrapper< type > >::cast_op_type
std::reference_wrapper< type > cast_op_type
Definition: cast.h:83
string_caster::cast
static handle cast(const StringType &src, return_value_policy, handle)
Definition: cast.h:450
argument_loader::call
enable_if_t< std::is_void< Return >::value, void_type > call(Func &&f) &&
Definition: cast.h:1604
weakref
Definition: pytypes.h:1894
copyable_holder_caster::try_direct_conversions
static bool try_direct_conversions(handle)
Definition: cast.h:833
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:134
PYBIND11_LONG_AS_LONGLONG
#define PYBIND11_LONG_AS_LONGLONG(o)
Definition: wrap/pybind11/include/pybind11/detail/common.h:367
tuple_caster::implicit_cast
type implicit_cast(index_sequence< Is... >) &&
Definition: cast.h:691
function_call::parent
handle parent
The parent, if any.
Definition: cast.h:1561
conditional_t
typename std::conditional< B, T, F >::type conditional_t
Definition: wrap/pybind11/include/pybind11/detail/common.h:675
name
static char name[]
Definition: rgamma.c:72
tuple_caster< std::pair, T1, T2 >::cast_op_type
type cast_op_type
Definition: cast.h:680
size
Scalar Scalar int size
Definition: benchVecAdd.cpp:17
bool_
Definition: pytypes.h:1800
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:602
type_name
string type_name()
Definition: benchmark-blocking-sizes.cpp:252
function_call
Internal data associated with a single function call.
Definition: cast.h:1544
type_caster< std::reference_wrapper< type > >::load
bool load(handle src, bool convert)
Definition: cast.h:71
override_unused
Definition: cast.h:1326
simple_collector::simple_collector
simple_collector(Ts &&...values)
Definition: cast.h:1643
is_pos_only
std::is_same< intrinsic_t< T >, pos_only > is_pos_only
Definition: cast.h:1538
type_caster< void >::cast_op_type
void *& cast_op_type
Definition: cast.h:304
unpacking_collector::process
void process(list &, detail::kwargs_proxy kp)
Definition: cast.h:1741
arg_v::type
std::string type
The C++ type name of the default value (only available when compiled in debug mode)
Definition: cast.h:1495
slice
Definition: pytypes.h:1911
always_construct_holder::value
static constexpr bool value
Definition: cast.h:868
object
Definition: pytypes.h:364
type_caster_base::cast
static handle cast(const itype &src, return_value_policy policy, handle parent)
Definition: type_caster_base.h:1086
copyable_holder_caster::check_holder_compat
void check_holder_compat()
Definition: cast.h:791
simple_collector::call
object call(PyObject *ptr) const
Call a Python function and pass the collected arguments.
Definition: cast.h:1652
is_kw_only
std::is_same< intrinsic_t< T >, kw_only > is_kw_only
Definition: cast.h:1536
arg::name
const char * name
If non-null, this is a named kwargs argument.
Definition: cast.h:1438
Object
Reference counted object base class.
Definition: object.h:9
simple_collector::args
const tuple & args() const &
Definition: cast.h:1646
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:366
dict
Definition: pytypes.h:2109
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:1469
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:645
simple_collector::m_args
tuple m_args
Definition: cast.h:1661
return_value_policy::automatic
@ automatic
intrinsic_t
typename intrinsic_type< T >::type intrinsic_t
Definition: wrap/pybind11/include/pybind11/detail/common.h:831
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:1330
handle
Definition: pytypes.h:226
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:131
sequence
Definition: pytypes.h:2148
handle::cast
T cast() const
Definition: cast.h:1241
arg_v::value
object value
The default value.
Definition: cast.h:1490
type::handle_of
static handle handle_of()
Definition: cast.h:1835
make_tuple
tuple make_tuple()
Definition: cast.h:1390
function_call::init_self
handle init_self
If this is a call to an initializer, this argument contains self
Definition: cast.h:1564
arg_v
Definition: cast.h:1446
return_value_policy_override::policy
static return_value_policy policy(return_value_policy p)
Definition: cast.h:1130
return_value_policy::copy
@ copy
object::release
handle release()
Definition: pytypes.h:385
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:1123
void_caster::cast
static handle cast(T, return_value_policy, handle)
Definition: cast.h:256
arg::flag_noconvert
bool flag_noconvert
Definition: cast.h:1439
typeid.h
copyable_holder_caster::load
bool load(handle src, bool convert)
Definition: cast.h:773
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:240
arg
Definition: cast.h:1419
return_value_policy::reference
@ reference
PYBIND11_DETAILED_ERROR_MESSAGES
#define PYBIND11_DETAILED_ERROR_MESSAGES
Definition: wrap/pybind11/include/pybind11/detail/common.h:1283
string_caster
Definition: cast.h:377
tuple_caster::subcasters
Tuple< make_caster< Ts >... > subcasters
Definition: cast.h:734
unpacking_collector::kwargs
dict kwargs() &&
Definition: cast.h:1683
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:62
negation
Definition: wrap/pybind11/include/pybind11/detail/common.h:732
pyobject_caster::load
bool load(handle src, bool)
Definition: cast.h:1055
unpacking_collector::process
void process(list &args_list, detail::args_proxy ap)
Definition: cast.h:1710
arg::flag_none
bool flag_none
If set (the default), allow None to be passed to this argument.
Definition: cast.h:1441
unpacking_collector::unpacking_collector
unpacking_collector(Ts &&...values)
Definition: cast.h:1669
unpacking_collector::kwargs
const dict & kwargs() const &
Definition: cast.h:1680
bytearray
Definition: pytypes.h:1758
type_caster< CharT, enable_if_t< is_std_char_type< CharT >::value > >::load
bool load(handle src, bool convert)
Definition: cast.h:539
holder_helper
Definition: cast.h:753
unpacking_collector::m_kwargs
dict m_kwargs
Definition: cast.h:1780
is_copy_constructible
Definition: type_caster_base.h:1005
memoryview
Definition: pytypes.h:2290
move_if_unreferenced
Definition: cast.h:1102
argument_loader::call_impl
Return call_impl(Func &&f, index_sequence< Is... >, Guard &&) &&
Definition: cast.h:1630
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:1382
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:1045
pybind11_fail
PyExc_RuntimeError PYBIND11_NOINLINE void pybind11_fail(const char *reason)
Used internally.
Definition: wrap/pybind11/include/pybind11/detail/common.h:1045
simple_collector
Definition: cast.h:1640
kw_only
Definition: cast.h:1502
argument_loader::indices
make_index_sequence< sizeof...(Args)> indices
Definition: cast.h:1570
argument_loader::args_pos
static constexpr int args_pos
Definition: cast.h:1586
value_and_holder::value_ptr
V *& value_ptr() const
Definition: value_and_holder.h:34
type_caster< void >::load
bool load(handle h, bool)
Definition: cast.h:270
function_call::args_ref
object args_ref
Definition: cast.h:1558
size_t
std::size_t size_t
Definition: wrap/pybind11/include/pybind11/detail/common.h:509
unpacking_collector::nameless_argument_error
static void nameless_argument_error(const std::string &type)
Definition: cast.h:1763
arg_v::noconvert
arg_v & noconvert(bool flag=true)
Same as arg::noconvert(), but returns *this as arg_v&, not arg&.
Definition: cast.h:1478
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:822
type_descr
constexpr descr< N+2, Ts... > type_descr(const descr< N, Ts... > &descr)
Definition: descr.h:167
argument_loader::kwargs_pos
static constexpr auto kwargs_pos
Definition: cast.h:1577
pytypes.h
descr.h
type_caster_generic
Definition: type_caster_base.h:475
PYBIND11_BYTES_NAME
#define PYBIND11_BYTES_NAME
Definition: wrap/pybind11/include/pybind11/detail/common.h:370
str
Definition: pytypes.h:1560
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:375
tuple_caster< std::pair, T1, T2 >::indices
make_index_sequence< size > indices
Definition: cast.h:642
PYBIND11_TYPE_CASTER
#define PYBIND11_TYPE_CASTER(type, py_name)
Definition: cast.h:87
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:730
unpacking_collector::multiple_values_error
static void multiple_values_error()
Definition: cast.h:1768
gtsam::index_sequence<>
return_value_policy::take_ownership
@ take_ownership
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:74
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:1433
unpacking_collector
Helper class which collects positional, keyword, * and ** arguments for a Python function call.
Definition: cast.h:1666
arg_v::descr
const char * descr
The (optional) description of the default value.
Definition: cast.h:1492
PYBIND11_BYTES_AS_STRING
#define PYBIND11_BYTES_AS_STRING
Definition: wrap/pybind11/include/pybind11/detail/common.h:364
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:1250
PYBIND11_WORKAROUND_INCORRECT_MSVC_C4100
#define PYBIND11_WORKAROUND_INCORRECT_MSVC_C4100(...)
Definition: wrap/pybind11/include/pybind11/detail/common.h:1249
accessor
Definition: pytypes.h:53
string_caster::load_raw
bool load_raw(enable_if_t< std::is_same< C, char >::value, handle > src)
Definition: cast.h:485
copyable_holder_caster::holder
holder_type holder
Definition: cast.h:835
make_index_sequence
typename make_index_sequence_impl< N >::type make_index_sequence
Definition: wrap/pybind11/include/pybind11/detail/common.h:712
dict::contains
bool contains(T &&key) const
Definition: pytypes.h:2130
handle_type_name
Definition: cast.h:901
handle::ptr
PyObject * ptr() const
Return the underlying PyObject * pointer.
Definition: pytypes.h:250
tuple::size
size_t size() const
Definition: pytypes.h:2090
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:878
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:1575
cast_safe
enable_if_t< cast_is_temporary_value_reference< T >::value &&!detail::is_same_ignoring_cvref< T, PyObject * >::value, T > cast_safe(object &&)
Definition: cast.h:1352
tuple_caster::load_impl
static constexpr bool load_impl(const sequence &, bool, index_sequence<>)
Definition: cast.h:695
type_caster< std::reference_wrapper< type > >::subcaster
caster_t subcaster
Definition: cast.h:60
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:864
argument_loader::load_impl_sequence
static bool load_impl_sequence(function_call &, index_sequence<>)
Definition: cast.h:1611
argument_loader
Helper class which loads arguments for C++ functions called from Python.
Definition: cast.h:1569
iterable
Definition: pytypes.h:1553
copyable_holder_caster::try_implicit_casts
bool try_implicit_casts(handle, bool)
Definition: cast.h:815
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:634
get_type_handle
PYBIND11_NOINLINE handle get_type_handle(const std::type_info &tp, bool throw_if_missing)
Definition: type_caster_base.h:246
std
Definition: BFloat16.h:88
tuple_caster::size
static constexpr auto size
Definition: cast.h:641
void_type
Helper type to replace 'void' in some expressions.
Definition: wrap/pybind11/include/pybind11/detail/common.h:834
args
Definition: pytypes.h:2212
unpacking_collector::call
object call(PyObject *ptr) const
Call a Python function and pass the collected arguments.
Definition: cast.h:1686
move_only_holder_caster
Definition: cast.h:846
p
float * p
Definition: Tutorial_Map_using.cpp:9
unpacking_collector::nameless_argument_error
static void nameless_argument_error()
Definition: cast.h:1757
c_str
const char * c_str(Args &&...args)
Definition: internals.h:701
argument_loader::argcasters
std::tuple< make_caster< Args >... > argcasters
Definition: cast.h:1634
holder_helper::get
static auto get(const T &p) -> decltype(p.get())
Definition: cast.h:754
tuple
Definition: pytypes.h:2079
is_pyobject
std::is_base_of< pyobject_tag, remove_reference_t< T > > is_pyobject
Definition: pytypes.h:73
float_
Definition: pytypes.h:1872
tuple_caster::load_impl
bool load_impl(const sequence &seq, bool convert, index_sequence< Is... >)
Definition: cast.h:698
PYBIND11_NB_BOOL
#define PYBIND11_NB_BOOL(ptr)
Definition: wrap/pybind11/include/pybind11/detail/common.h:376
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:463
unpacking_collector::process
void process(list &, arg_v a)
Definition: cast.h:1716
pos_only
Definition: cast.h:1507
U
@ U
Definition: testDecisionTree.cpp:342
pybind11
Definition: wrap/pybind11/pybind11/__init__.py:1
function
Definition: pytypes.h:2254
type_caster< bool >::load
bool load(handle src, bool convert)
Definition: cast.h:318
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:208
type_caster_generic::typeinfo
const type_info * typeinfo
Definition: type_caster_base.h:781
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:1257
type_caster_generic::load
bool load(handle src, bool convert)
Definition: type_caster_base.h:483
function_call::args
std::vector< handle > args
Arguments passed to the function:
Definition: cast.h:1551
object_or_cast
object object_or_cast(T &&o)
Definition: cast.h:1320
type_caster_base::cast_holder
static handle cast_holder(const itype *src, const void *holder)
Definition: type_caster_base.h:1133
pyobject_caster::cast
static handle cast(const handle &src, return_value_policy, handle)
Definition: cast.h:1069
object::cast
T cast() const &
Definition: cast.h:1300
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:127
copyable_holder_caster
Definition: cast.h:763
simple_collector::kwargs
dict kwargs() const
Definition: cast.h:1647
Tuple
Definition: typing.h:31
none
Definition: pytypes.h:1788
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:232
arg::noconvert
arg & noconvert(bool flag=true)
Indicate that the type should not be converted in the type caster.
Definition: cast.h:1428
PYBIND11_LONG_FROM_SIGNED
#define PYBIND11_LONG_FROM_SIGNED(o)
Definition: wrap/pybind11/include/pybind11/detail/common.h:368
function_call::kwargs_ref
object kwargs_ref
Definition: cast.h:1558
copyable_holder_caster::try_implicit_casts
bool try_implicit_casts(handle src, bool convert)
Definition: cast.h:821
frozenset
Definition: pytypes.h:2249
unpacking_collector::args
const tuple & args() const &
Definition: cast.h:1679
return_value_policy::automatic_reference
@ automatic_reference
tuple_caster::implicit_cast
type implicit_cast(index_sequence< Is... >) &
Definition: cast.h:687
reinterpret_steal
T reinterpret_steal(handle h)
Definition: pytypes.h:480
gtsam::convert
static BinaryMeasurement< Rot3 > convert(const BetweenFactor< Pose3 >::shared_ptr &f)
Definition: ShonanAveraging.cpp:995
values_and_holders
Definition: type_caster_base.h:268
gtsam.examples.ShonanAveragingCLI.str
str
Definition: ShonanAveragingCLI.py:115
PYBIND11_STRING_NAME
#define PYBIND11_STRING_NAME
Definition: wrap/pybind11/include/pybind11/detail/common.h:371
argument_loader::has_kwargs
static constexpr bool has_kwargs
Definition: cast.h:1583
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:360
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:673
test_callbacks.value
value
Definition: test_callbacks.py:162
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:369
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:1474
loader_life_support::add_patient
static PYBIND11_NOINLINE void add_patient(handle h)
Definition: type_caster_base.h:76
function_call::args_convert
std::vector< bool > args_convert
The convert value the arguments should be loaded with.
Definition: cast.h:1554
string_caster< std::basic_string< CharT, Traits, Allocator > >::CharT
typename std::basic_string< CharT, Traits, Allocator > ::value_type CharT
Definition: cast.h:378
ellipsis
Definition: pytypes.h:1794
args_are_all_positional
constexpr bool args_are_all_positional()
Definition: cast.h:1788
tuple_caster::cast
static handle cast(T &&src, return_value_policy policy, handle parent)
Definition: cast.h:657
arg_v::none
arg_v & none(bool flag=true)
Same as arg::nonone(), but returns *this as arg_v&, not arg&.
Definition: cast.h:1484
string_caster::load
bool load(handle src, bool)
Definition: cast.h:397
string_caster::load_raw
bool load_raw(enable_if_t<!std::is_same< C, char >::value, handle >)
Definition: cast.h:511
type_caster_base
Generic type caster for objects stored on the heap.
Definition: type_caster_base.h:1077
type_caster_base.h
anyset
Definition: pytypes.h:2219
type_caster< bool >::is_numpy_bool
static bool is_numpy_bool(handle object)
Definition: cast.h:367


gtsam
Author(s):
autogenerated on Wed Mar 19 2025 03:01:22