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 
745 template <typename T>
747  static auto get(const T &p) -> decltype(p.get()) { return p.get(); }
748 };
749 
755 template <typename type, typename holder_type, typename SFINAE = void>
757 public:
759  static_assert(std::is_base_of<base, type_caster<type>>::value,
760  "Holder classes are only supported for custom types");
761  using base::base;
762  using base::cast;
763  using base::typeinfo;
764  using base::value;
765 
766  bool load(handle src, bool convert) {
767  return base::template load_impl<copyable_holder_caster<type, holder_type>>(src, convert);
768  }
769 
770  explicit operator type *() { return this->value; }
771  // static_cast works around compiler error with MSVC 17 and CUDA 10.2
772  // see issue #2180
773  explicit operator type &() { return *(static_cast<type *>(this->value)); }
774  explicit operator holder_type *() { return std::addressof(holder); }
775  explicit operator holder_type &() { return holder; }
776 
777  static handle cast(const holder_type &src, return_value_policy, handle) {
778  const auto *ptr = holder_helper<holder_type>::get(src);
779  return type_caster_base<type>::cast_holder(ptr, &src);
780  }
781 
782 protected:
783  friend class type_caster_generic;
785  if (typeinfo->default_holder) {
786  throw cast_error("Unable to load a custom holder type from a default-holder instance");
787  }
788  }
789 
791  if (v_h.holder_constructed()) {
792  value = v_h.value_ptr();
793  holder = v_h.template holder<holder_type>();
794  return true;
795  }
796  throw cast_error("Unable to cast from non-held to held instance (T& to Holder<T>) "
798  "(#define PYBIND11_DETAILED_ERROR_MESSAGES or compile in debug mode for "
799  "type information)");
800 #else
801  "of type '"
802  + type_id<holder_type>() + "''");
803 #endif
804  }
805 
806  template <typename T = holder_type,
809  return false;
810  }
811 
812  template <typename T = holder_type,
815  for (auto &cast : typeinfo->implicit_casts) {
816  copyable_holder_caster sub_caster(*cast.first);
817  if (sub_caster.load(src, convert)) {
818  value = cast.second(sub_caster.value);
819  holder = holder_type(sub_caster.holder, (type *) value);
820  return true;
821  }
822  }
823  return false;
824  }
825 
826  static bool try_direct_conversions(handle) { return false; }
827 
828  holder_type holder;
829 };
830 
832 template <typename T>
833 class type_caster<std::shared_ptr<T>> : public copyable_holder_caster<T, std::shared_ptr<T>> {};
834 
838 template <typename type, typename holder_type, typename SFINAE = void>
840  static_assert(std::is_base_of<type_caster_base<type>, type_caster<type>>::value,
841  "Holder classes are only supported for custom types");
842 
843  static handle cast(holder_type &&src, return_value_policy, handle) {
844  auto *ptr = holder_helper<holder_type>::get(src);
845  return type_caster_base<type>::cast_holder(ptr, std::addressof(src));
846  }
847  static constexpr auto name = type_caster_base<type>::name;
848 };
849 
850 template <typename type, typename deleter>
851 class type_caster<std::unique_ptr<type, deleter>>
852  : public move_only_holder_caster<type, std::unique_ptr<type, deleter>> {};
853 
854 template <typename type, typename holder_type>
858 
859 template <typename T, bool Value = false>
861  static constexpr bool value = Value;
862 };
863 
865 #define PYBIND11_DECLARE_HOLDER_TYPE(type, holder_type, ...) \
866  PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE) \
867  namespace detail { \
868  template <typename type> \
869  struct always_construct_holder<holder_type> : always_construct_holder<void, ##__VA_ARGS__> { \
870  }; \
871  template <typename type> \
872  class type_caster<holder_type, enable_if_t<!is_shared_ptr<holder_type>::value>> \
873  : public type_caster_holder<type, holder_type> {}; \
874  } \
875  PYBIND11_NAMESPACE_END(PYBIND11_NAMESPACE)
876 
877 // PYBIND11_DECLARE_HOLDER_TYPE holder types:
878 template <typename base, typename holder>
880  : std::is_base_of<detail::type_caster_holder<base, holder>, detail::type_caster<holder>> {};
881 // Specialization for always-supported unique_ptr holders:
882 template <typename base, typename deleter>
883 struct is_holder_type<base, std::unique_ptr<base, deleter>> : std::true_type {};
884 
885 #ifdef PYBIND11_DISABLE_HANDLE_TYPE_NAME_DEFAULT_IMPLEMENTATION // See PR #4888
886 
887 // This leads to compilation errors if a specialization is missing.
888 template <typename T>
889 struct handle_type_name;
890 
891 #else
892 
893 template <typename T>
895  static constexpr auto name = const_name<T>();
896 };
897 
898 #endif
899 
900 template <>
902  static constexpr auto name = const_name("object");
903 };
904 template <>
906  static constexpr auto name = const_name("list");
907 };
908 template <>
910  static constexpr auto name = const_name("dict");
911 };
912 template <>
914  static constexpr auto name = const_name("Union[set, frozenset]");
915 };
916 template <>
918  static constexpr auto name = const_name("set");
919 };
920 template <>
922  static constexpr auto name = const_name("frozenset");
923 };
924 template <>
926  static constexpr auto name = const_name("str");
927 };
928 template <>
930  static constexpr auto name = const_name("tuple");
931 };
932 template <>
934  static constexpr auto name = const_name("bool");
935 };
936 template <>
938  static constexpr auto name = const_name(PYBIND11_BYTES_NAME);
939 };
940 template <>
942  static constexpr auto name = const_name("Buffer");
943 };
944 template <>
946  static constexpr auto name = const_name("int");
947 };
948 template <>
950  static constexpr auto name = const_name("Iterable");
951 };
952 template <>
954  static constexpr auto name = const_name("Iterator");
955 };
956 template <>
958  static constexpr auto name = const_name("float");
959 };
960 template <>
962  static constexpr auto name = const_name("Callable");
963 };
964 template <>
966  static constexpr auto name = handle_type_name<object>::name;
967 };
968 template <>
970  static constexpr auto name = const_name("None");
971 };
972 template <>
974  static constexpr auto name = const_name("Sequence");
975 };
976 template <>
978  static constexpr auto name = const_name("bytearray");
979 };
980 template <>
982  static constexpr auto name = const_name("memoryview");
983 };
984 template <>
986  static constexpr auto name = const_name("slice");
987 };
988 template <>
990  static constexpr auto name = const_name("type");
991 };
992 template <>
994  static constexpr auto name = const_name("capsule");
995 };
996 template <>
998  static constexpr auto name = const_name("ellipsis");
999 };
1000 template <>
1002  static constexpr auto name = const_name("weakref");
1003 };
1004 template <>
1006  static constexpr auto name = const_name("*args");
1007 };
1008 template <>
1010  static constexpr auto name = const_name("**kwargs");
1011 };
1012 template <>
1014  static constexpr auto name = const_name<obj_attr_accessor>();
1015 };
1016 template <>
1018  static constexpr auto name = const_name<str_attr_accessor>();
1019 };
1020 template <>
1022  static constexpr auto name = const_name<item_accessor>();
1023 };
1024 template <>
1026  static constexpr auto name = const_name<sequence_accessor>();
1027 };
1028 template <>
1030  static constexpr auto name = const_name<list_accessor>();
1031 };
1032 template <>
1034  static constexpr auto name = const_name<tuple_accessor>();
1035 };
1036 
1037 template <typename type>
1041 
1042  // `type` may not be default constructible (e.g. frozenset, anyset). Initializing `value`
1043  // to a nil handle is safe since it will only be accessed if `load` succeeds.
1046 
1048  bool load(handle src, bool /* convert */) {
1049  value = src;
1050  return static_cast<bool>(value);
1051  }
1052 
1054  bool load(handle src, bool /* convert */) {
1055  if (!isinstance<type>(src)) {
1056  return false;
1057  }
1058  value = reinterpret_borrow<type>(src);
1059  return true;
1060  }
1061 
1062  static handle cast(const handle &src, return_value_policy /* policy */, handle /* parent */) {
1063  return src.inc_ref();
1064  }
1066 };
1067 
1068 template <typename T>
1070 
1071 // Our conditions for enabling moving are quite restrictive:
1072 // At compile time:
1073 // - T needs to be a non-const, non-pointer, non-reference type
1074 // - type_caster<T>::operator T&() must exist
1075 // - the type must be move constructible (obviously)
1076 // At run-time:
1077 // - if the type is non-copy-constructible, the object must be the sole owner of the type (i.e. it
1078 // must have ref_count() == 1)h
1079 // If any of the above are not satisfied, we fall back to copying.
1080 template <typename T>
1081 using move_is_plain_type
1083 template <typename T, typename SFINAE = void>
1084 struct move_always : std::false_type {};
1085 template <typename T>
1087  T,
1088  enable_if_t<
1092  std::is_same<decltype(std::declval<make_caster<T>>().operator T &()), T &>>::value>>
1093  : std::true_type {};
1094 template <typename T, typename SFINAE = void>
1095 struct move_if_unreferenced : std::false_type {};
1096 template <typename T>
1098  T,
1099  enable_if_t<
1103  std::is_same<decltype(std::declval<make_caster<T>>().operator T &()), T &>>::value>>
1104  : std::true_type {};
1105 template <typename T>
1107 
1108 // Detect whether returning a `type` from a cast on type's type_caster is going to result in a
1109 // reference or pointer to a local variable of the type_caster. Basically, only
1110 // non-reference/pointer `type`s and reference/pointers from a type_caster_generic are safe;
1111 // everything else returns a reference/pointer to a local variable.
1112 template <typename type>
1115  && !std::is_base_of<type_caster_generic, make_caster<type>>::value
1116  && !std::is_same<intrinsic_t<type>, void>::value>;
1117 
1118 // When a value returned from a C++ function is being cast back to Python, we almost always want to
1119 // force `policy = move`, regardless of the return value policy the function/method was declared
1120 // with.
1121 template <typename Return, typename SFINAE = void>
1124 };
1125 
1126 template <typename Return>
1128  Return,
1129  detail::enable_if_t<std::is_base_of<type_caster_generic, make_caster<Return>>::value, void>> {
1133  : p;
1134  }
1135 };
1136 
1137 // Basic python -> C++ casting; throws if casting fails
1138 template <typename T, typename SFINAE>
1140  static_assert(!detail::is_pyobject<T>::value,
1141  "Internal error: type_caster should only be used for C++ types");
1142  if (!conv.load(handle, true)) {
1143 #if !defined(PYBIND11_DETAILED_ERROR_MESSAGES)
1144  throw cast_error(
1145  "Unable to cast Python instance of type "
1146  + str(type::handle_of(handle)).cast<std::string>()
1147  + " to C++ type '?' (#define "
1148  "PYBIND11_DETAILED_ERROR_MESSAGES or compile in debug mode for details)");
1149 #else
1150  throw cast_error("Unable to cast Python instance of type "
1151  + str(type::handle_of(handle)).cast<std::string>() + " to C++ type '"
1152  + type_id<T>() + "'");
1153 #endif
1154  }
1155  return conv;
1156 }
1157 // Wrapper around the above that also constructs and returns a type_caster
1158 template <typename T>
1161  load_type(conv, handle);
1162  return conv;
1163 }
1164 
1166 
1167 // pytype -> C++ type
1168 template <typename T,
1171  int>
1172  = 0>
1173 T cast(const handle &handle) {
1174  using namespace detail;
1176  "Unable to cast type to reference: value is local to type caster");
1177  return cast_op<T>(load_type<T>(handle));
1178 }
1179 
1180 // pytype -> pytype (calls converting constructor)
1182 T cast(const handle &handle) {
1183  return T(reinterpret_borrow<object>(handle));
1184 }
1185 
1186 // Note that `cast<PyObject *>(obj)` increments the reference count of `obj`.
1187 // This is necessary for the case that `obj` is a temporary, and could
1188 // not possibly be different, given
1189 // 1. the established convention that the passed `handle` is borrowed, and
1190 // 2. we don't want to force all generic code using `cast<T>()` to special-case
1191 // handling of `T` = `PyObject *` (to increment the reference count there).
1192 // It is the responsibility of the caller to ensure that the reference count
1193 // is decremented.
1194 template <typename T,
1195  typename Handle,
1198  int>
1199  = 0>
1200 T cast(Handle &&handle) {
1201  return handle.inc_ref().ptr();
1202 }
1203 // To optimize way an inc_ref/dec_ref cycle:
1204 template <typename T,
1205  typename Object,
1208  int>
1209  = 0>
1210 T cast(Object &&obj) {
1211  return obj.release().ptr();
1212 }
1213 
1214 // C++ type -> py::object
1216 object cast(T &&value,
1218  handle parent = handle()) {
1219  using no_ref_T = typename std::remove_reference<T>::type;
1220  if (policy == return_value_policy::automatic) {
1224  } else if (policy == return_value_policy::automatic_reference) {
1228  }
1229  return reinterpret_steal<object>(
1230  detail::make_caster<T>::cast(std::forward<T>(value), policy, parent));
1231 }
1232 
1233 template <typename T>
1234 T handle::cast() const {
1235  return pybind11::cast<T>(*this);
1236 }
1237 template <>
1238 inline void handle::cast() const {
1239  return;
1240 }
1241 
1242 template <typename T>
1244  if (obj.ref_count() > 1) {
1245 #if !defined(PYBIND11_DETAILED_ERROR_MESSAGES)
1246  throw cast_error(
1247  "Unable to cast Python " + str(type::handle_of(obj)).cast<std::string>()
1248  + " instance to C++ rvalue: instance has multiple references"
1249  " (#define PYBIND11_DETAILED_ERROR_MESSAGES or compile in debug mode for details)");
1250 #else
1251  throw cast_error("Unable to move from Python "
1252  + str(type::handle_of(obj)).cast<std::string>() + " instance to C++ "
1253  + type_id<T>() + " instance: instance has multiple references");
1254 #endif
1255  }
1256 
1257  // Move into a temporary and return that, because the reference may be a local value of `conv`
1258  T ret = std::move(detail::load_type<T>(obj).operator T &());
1259  return ret;
1260 }
1261 
1262 // Calling cast() on an rvalue calls pybind11::cast with the object rvalue, which does:
1263 // - If we have to move (because T has no copy constructor), do it. This will fail if the moved
1264 // object has multiple references, but trying to copy will fail to compile.
1265 // - If both movable and copyable, check ref count: if 1, move; otherwise copy
1266 // - Otherwise (not movable), copy.
1267 template <typename T>
1269 cast(object &&object) {
1270  return move<T>(std::move(object));
1271 }
1272 template <typename T>
1274 cast(object &&object) {
1275  if (object.ref_count() > 1) {
1276  return cast<T>(object);
1277  }
1278  return move<T>(std::move(object));
1279 }
1280 template <typename T>
1282 cast(object &&object) {
1283  return cast<T>(object);
1284 }
1285 
1286 // pytype rvalue -> pytype (calls converting constructor)
1287 template <typename T>
1289  return T(std::move(object));
1290 }
1291 
1292 template <typename T>
1293 T object::cast() const & {
1294  return pybind11::cast<T>(*this);
1295 }
1296 template <typename T>
1297 T object::cast() && {
1298  return pybind11::cast<T>(std::move(*this));
1299 }
1300 template <>
1301 inline void object::cast() const & {
1302  return;
1303 }
1304 template <>
1305 inline void object::cast() && {
1306  return;
1307 }
1308 
1310 
1311 // Declared in pytypes.h:
1313 object object_or_cast(T &&o) {
1314  return pybind11::cast(std::forward<T>(o));
1315 }
1316 
1317 // Placeholder type for the unneeded (and dead code) static variable in the
1318 // PYBIND11_OVERRIDE_OVERRIDE macro
1320 template <typename ret_type>
1324 
1325 // Trampoline use: for reference/pointer types to value-converted values, we do a value cast, then
1326 // store the result in the given variable. For other types, this is a no-op.
1327 template <typename T>
1329  make_caster<T> &caster) {
1330  return cast_op<T>(load_type(caster, o));
1331 }
1332 template <typename T>
1334  override_unused &) {
1335  pybind11_fail("Internal error: cast_ref fallback invoked");
1336 }
1337 
1338 // Trampoline use: Having a pybind11::cast with an invalid reference type is going to
1339 // static_assert, even though if it's in dead code, so we provide a "trampoline" to pybind11::cast
1340 // that only does anything in cases where pybind11::cast is valid.
1341 template <typename T>
1344  T>
1345 cast_safe(object &&) {
1346  pybind11_fail("Internal error: cast_safe fallback invoked");
1347 }
1348 template <typename T>
1350 template <typename T>
1352 cast_safe(object &&o) {
1353  return o.release().ptr();
1354 }
1355 template <typename T>
1357  detail::is_same_ignoring_cvref<T, PyObject *>,
1358  std::is_void<T>>::value,
1359  T>
1360 cast_safe(object &&o) {
1361  return pybind11::cast<T>(std::move(o));
1362 }
1363 
1365 
1366 // The overloads could coexist, i.e. the #if is not strictly speaking needed,
1367 // but it is an easy minor optimization.
1368 #if !defined(PYBIND11_DETAILED_ERROR_MESSAGES)
1369 inline cast_error cast_error_unable_to_convert_call_arg(const std::string &name) {
1370  return cast_error("Unable to convert call argument '" + name
1371  + "' to Python object (#define "
1372  "PYBIND11_DETAILED_ERROR_MESSAGES or compile in debug mode for details)");
1373 }
1374 #else
1375 inline cast_error cast_error_unable_to_convert_call_arg(const std::string &name,
1376  const std::string &type) {
1377  return cast_error("Unable to convert call argument '" + name + "' of type '" + type
1378  + "' to Python object");
1379 }
1380 #endif
1381 
1382 template <return_value_policy policy = return_value_policy::automatic_reference>
1384  return tuple(0);
1385 }
1386 
1387 template <return_value_policy policy = return_value_policy::automatic_reference, typename... Args>
1388 tuple make_tuple(Args &&...args_) {
1389  constexpr size_t size = sizeof...(Args);
1390  std::array<object, size> args{{reinterpret_steal<object>(
1391  detail::make_caster<Args>::cast(std::forward<Args>(args_), policy, nullptr))...}};
1392  for (size_t i = 0; i < args.size(); i++) {
1393  if (!args[i]) {
1394 #if !defined(PYBIND11_DETAILED_ERROR_MESSAGES)
1395  throw cast_error_unable_to_convert_call_arg(std::to_string(i));
1396 #else
1397  std::array<std::string, size> argtypes{{type_id<Args>()...}};
1398  throw cast_error_unable_to_convert_call_arg(std::to_string(i), argtypes[i]);
1399 #endif
1400  }
1401  }
1402  tuple result(size);
1403  int counter = 0;
1404  for (auto &arg_value : args) {
1405  PyTuple_SET_ITEM(result.ptr(), counter++, arg_value.release().ptr());
1406  }
1407  return result;
1408 }
1409 
1412 struct arg {
1415  constexpr explicit arg(const char *name = nullptr)
1416  : name(name), flag_noconvert(false), flag_none(true) {}
1418  template <typename T>
1419  arg_v operator=(T &&value) const;
1421  arg &noconvert(bool flag = true) {
1422  flag_noconvert = flag;
1423  return *this;
1424  }
1426  arg &none(bool flag = true) {
1427  flag_none = flag;
1428  return *this;
1429  }
1430 
1431  const char *name;
1432  bool flag_noconvert : 1;
1433  bool flag_none : 1;
1435 };
1436 
1439 struct arg_v : arg {
1440 private:
1441  template <typename T>
1442  arg_v(arg &&base, T &&x, const char *descr = nullptr)
1445  descr(descr)
1446 #if defined(PYBIND11_DETAILED_ERROR_MESSAGES)
1447  ,
1448  type(type_id<T>())
1449 #endif
1450  {
1451  // Workaround! See:
1452  // https://github.com/pybind/pybind11/issues/2336
1453  // https://github.com/pybind/pybind11/pull/2685#issuecomment-731286700
1454  if (PyErr_Occurred()) {
1455  PyErr_Clear();
1456  }
1457  }
1458 
1459 public:
1461  template <typename T>
1462  arg_v(const char *name, T &&x, const char *descr = nullptr)
1463  : arg_v(arg(name), std::forward<T>(x), descr) {}
1464 
1466  template <typename T>
1467  arg_v(const arg &base, T &&x, const char *descr = nullptr)
1468  : arg_v(arg(base), std::forward<T>(x), descr) {}
1469 
1471  arg_v &noconvert(bool flag = true) {
1472  arg::noconvert(flag);
1473  return *this;
1474  }
1475 
1477  arg_v &none(bool flag = true) {
1478  arg::none(flag);
1479  return *this;
1480  }
1481 
1483  object value;
1485  const char *descr;
1486 #if defined(PYBIND11_DETAILED_ERROR_MESSAGES)
1487  std::string type;
1489 #endif
1490 };
1491 
1495 struct kw_only {};
1496 
1500 struct pos_only {};
1501 
1502 template <typename T>
1504  return {*this, std::forward<T>(value)};
1505 }
1506 
1508 template <typename /*unused*/>
1509 using arg_t = arg_v;
1510 
1511 inline namespace literals {
1515 constexpr arg
1516 #if !defined(__clang__) && defined(__GNUC__) && __GNUC__ < 5
1517 operator"" _a // gcc 4.8.5 insists on having a space (hard error).
1518 #else
1519 operator""_a // clang 17 generates a deprecation warning if there is a space.
1520 #endif
1521  (const char *name, size_t) {
1522  return arg(name);
1523 }
1524 } // namespace literals
1525 
1527 
1528 template <typename T>
1529 using is_kw_only = std::is_same<intrinsic_t<T>, kw_only>;
1530 template <typename T>
1531 using is_pos_only = std::is_same<intrinsic_t<T>, pos_only>;
1532 
1533 // forward declaration (definition in attr.h)
1534 struct function_record;
1535 
1538  function_call(const function_record &f, handle p); // Implementation in attr.h
1539 
1542 
1544  std::vector<handle> args;
1545 
1547  std::vector<bool> args_convert;
1548 
1552 
1555 
1558 };
1559 
1561 template <typename... Args>
1563  using indices = make_index_sequence<sizeof...(Args)>;
1564 
1565  template <typename Arg>
1566  using argument_is_args = std::is_same<intrinsic_t<Arg>, args>;
1567  template <typename Arg>
1568  using argument_is_kwargs = std::is_same<intrinsic_t<Arg>, kwargs>;
1569  // Get kwargs argument position, or -1 if not present:
1570  static constexpr auto kwargs_pos = constexpr_last<argument_is_kwargs, Args...>();
1571 
1572  static_assert(kwargs_pos == -1 || kwargs_pos == (int) sizeof...(Args) - 1,
1573  "py::kwargs is only permitted as the last argument of a function");
1574 
1575 public:
1576  static constexpr bool has_kwargs = kwargs_pos != -1;
1577 
1578  // py::args argument position; -1 if not present.
1579  static constexpr int args_pos = constexpr_last<argument_is_args, Args...>();
1580 
1581  static_assert(args_pos == -1 || args_pos == constexpr_first<argument_is_args, Args...>(),
1582  "py::args cannot be specified more than once");
1583 
1584  static constexpr auto arg_names
1586 
1588 
1589  template <typename Return, typename Guard, typename Func>
1590  // NOLINTNEXTLINE(readability-const-return-type)
1592  return std::move(*this).template call_impl<remove_cv_t<Return>>(
1593  std::forward<Func>(f), indices{}, Guard{});
1594  }
1595 
1596  template <typename Return, typename Guard, typename Func>
1598  std::move(*this).template call_impl<remove_cv_t<Return>>(
1599  std::forward<Func>(f), indices{}, Guard{});
1600  return void_type();
1601  }
1602 
1603 private:
1604  static bool load_impl_sequence(function_call &, index_sequence<>) { return true; }
1605 
1606  template <size_t... Is>
1608 #ifdef __cpp_fold_expressions
1609  if ((... || !std::get<Is>(argcasters).load(call.args[Is], call.args_convert[Is]))) {
1610  return false;
1611  }
1612 #else
1613  for (bool r : {std::get<Is>(argcasters).load(call.args[Is], call.args_convert[Is])...}) {
1614  if (!r) {
1615  return false;
1616  }
1617  }
1618 #endif
1619  return true;
1620  }
1621 
1622  template <typename Return, typename Func, size_t... Is, typename Guard>
1623  Return call_impl(Func &&f, index_sequence<Is...>, Guard &&) && {
1624  return std::forward<Func>(f)(cast_op<Args>(std::move(std::get<Is>(argcasters)))...);
1625  }
1626 
1627  std::tuple<make_caster<Args>...> argcasters;
1628 };
1629 
1632 template <return_value_policy policy>
1634 public:
1635  template <typename... Ts>
1636  explicit simple_collector(Ts &&...values)
1637  : m_args(pybind11::make_tuple<policy>(std::forward<Ts>(values)...)) {}
1638 
1639  const tuple &args() const & { return m_args; }
1640  dict kwargs() const { return {}; }
1641 
1642  tuple args() && { return std::move(m_args); }
1643 
1645  object call(PyObject *ptr) const {
1646  PyObject *result = PyObject_CallObject(ptr, m_args.ptr());
1647  if (!result) {
1648  throw error_already_set();
1649  }
1650  return reinterpret_steal<object>(result);
1651  }
1652 
1653 private:
1655 };
1656 
1658 template <return_value_policy policy>
1660 public:
1661  template <typename... Ts>
1662  explicit unpacking_collector(Ts &&...values) {
1663  // Tuples aren't (easily) resizable so a list is needed for collection,
1664  // but the actual function call strictly requires a tuple.
1665  auto args_list = list();
1666  using expander = int[];
1667  (void) expander{0, (process(args_list, std::forward<Ts>(values)), 0)...};
1668 
1669  m_args = std::move(args_list);
1670  }
1671 
1672  const tuple &args() const & { return m_args; }
1673  const dict &kwargs() const & { return m_kwargs; }
1674 
1675  tuple args() && { return std::move(m_args); }
1676  dict kwargs() && { return std::move(m_kwargs); }
1677 
1679  object call(PyObject *ptr) const {
1680  PyObject *result = PyObject_Call(ptr, m_args.ptr(), m_kwargs.ptr());
1681  if (!result) {
1682  throw error_already_set();
1683  }
1684  return reinterpret_steal<object>(result);
1685  }
1686 
1687 private:
1688  template <typename T>
1689  void process(list &args_list, T &&x) {
1690  auto o = reinterpret_steal<object>(
1691  detail::make_caster<T>::cast(std::forward<T>(x), policy, {}));
1692  if (!o) {
1693 #if !defined(PYBIND11_DETAILED_ERROR_MESSAGES)
1694  throw cast_error_unable_to_convert_call_arg(std::to_string(args_list.size()));
1695 #else
1696  throw cast_error_unable_to_convert_call_arg(std::to_string(args_list.size()),
1697  type_id<T>());
1698 #endif
1699  }
1700  args_list.append(std::move(o));
1701  }
1702 
1703  void process(list &args_list, detail::args_proxy ap) {
1704  for (auto a : ap) {
1705  args_list.append(a);
1706  }
1707  }
1708 
1709  void process(list & /*args_list*/, arg_v a) {
1710  if (!a.name) {
1711 #if !defined(PYBIND11_DETAILED_ERROR_MESSAGES)
1713 #else
1714  nameless_argument_error(a.type);
1715 #endif
1716  }
1717  if (m_kwargs.contains(a.name)) {
1718 #if !defined(PYBIND11_DETAILED_ERROR_MESSAGES)
1720 #else
1721  multiple_values_error(a.name);
1722 #endif
1723  }
1724  if (!a.value) {
1725 #if !defined(PYBIND11_DETAILED_ERROR_MESSAGES)
1727 #else
1728  throw cast_error_unable_to_convert_call_arg(a.name, a.type);
1729 #endif
1730  }
1731  m_kwargs[a.name] = std::move(a.value);
1732  }
1733 
1734  void process(list & /*args_list*/, detail::kwargs_proxy kp) {
1735  if (!kp) {
1736  return;
1737  }
1738  for (auto k : reinterpret_borrow<dict>(kp)) {
1739  if (m_kwargs.contains(k.first)) {
1740 #if !defined(PYBIND11_DETAILED_ERROR_MESSAGES)
1742 #else
1743  multiple_values_error(str(k.first));
1744 #endif
1745  }
1746  m_kwargs[k.first] = k.second;
1747  }
1748  }
1749 
1750  [[noreturn]] static void nameless_argument_error() {
1751  throw type_error(
1752  "Got kwargs without a name; only named arguments "
1753  "may be passed via py::arg() to a python function call. "
1754  "(#define PYBIND11_DETAILED_ERROR_MESSAGES or compile in debug mode for details)");
1755  }
1756  [[noreturn]] static void nameless_argument_error(const std::string &type) {
1757  throw type_error("Got kwargs without a name of type '" + type
1758  + "'; only named "
1759  "arguments may be passed via py::arg() to a python function call. ");
1760  }
1761  [[noreturn]] static void multiple_values_error() {
1762  throw type_error(
1763  "Got multiple values for keyword argument "
1764  "(#define PYBIND11_DETAILED_ERROR_MESSAGES or compile in debug mode for details)");
1765  }
1766 
1767  [[noreturn]] static void multiple_values_error(const std::string &name) {
1768  throw type_error("Got multiple values for keyword argument '" + name + "'");
1769  }
1770 
1771 private:
1774 };
1775 
1776 // [workaround(intel)] Separate function required here
1777 // We need to put this into a separate function because the Intel compiler
1778 // fails to compile enable_if_t<!all_of<is_positional<Args>...>::value>
1779 // (tested with ICC 2021.1 Beta 20200827).
1780 template <typename... Args>
1781 constexpr bool args_are_all_positional() {
1783 }
1784 
1786 template <return_value_policy policy,
1787  typename... Args,
1788  typename = enable_if_t<args_are_all_positional<Args...>()>>
1790  return simple_collector<policy>(std::forward<Args>(args)...);
1791 }
1792 
1794 template <return_value_policy policy,
1795  typename... Args,
1796  typename = enable_if_t<!args_are_all_positional<Args...>()>>
1798  // Following argument order rules for generalized unpacking according to PEP 448
1799  static_assert(constexpr_last<is_positional, Args...>()
1800  < constexpr_first<is_keyword_or_ds, Args...>()
1801  && constexpr_last<is_s_unpacking, Args...>()
1802  < constexpr_first<is_ds_unpacking, Args...>(),
1803  "Invalid function call: positional args must precede keywords and ** unpacking; "
1804  "* unpacking must precede ** unpacking");
1805  return unpacking_collector<policy>(std::forward<Args>(args)...);
1806 }
1807 
1808 template <typename Derived>
1809 template <return_value_policy policy, typename... Args>
1810 object object_api<Derived>::operator()(Args &&...args) const {
1811 #ifndef NDEBUG
1812  if (!PyGILState_Check()) {
1813  pybind11_fail("pybind11::object_api<>::operator() PyGILState_Check() failure.");
1814  }
1815 #endif
1816  return detail::collect_arguments<policy>(std::forward<Args>(args)...).call(derived().ptr());
1817 }
1818 
1819 template <typename Derived>
1820 template <return_value_policy policy, typename... Args>
1821 object object_api<Derived>::call(Args &&...args) const {
1822  return operator()<policy>(std::forward<Args>(args)...);
1823 }
1824 
1826 
1827 template <typename T>
1829  static_assert(std::is_base_of<detail::type_caster_generic, detail::make_caster<T>>::value,
1830  "py::type::of<T> only supports the case where T is a registered C++ types.");
1831 
1832  return detail::get_type_handle(typeid(T), true);
1833 }
1834 
1835 #define PYBIND11_MAKE_OPAQUE(...) \
1836  PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE) \
1837  namespace detail { \
1838  template <> \
1839  class type_caster<__VA_ARGS__> : public type_caster_base<__VA_ARGS__> {}; \
1840  } \
1841  PYBIND11_NAMESPACE_END(PYBIND11_NAMESPACE)
1842 
1846 #define PYBIND11_TYPE(...) __VA_ARGS__
1847 
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:1835
pyobject_caster::pyobject_caster
pyobject_caster()
Definition: cast.h:1040
load_type
type_caster< T, SFINAE > & load_type(type_caster< T, SFINAE > &conv, const handle &handle)
Definition: cast.h:1139
PYBIND11_BYTES_SIZE
#define PYBIND11_BYTES_SIZE
Definition: wrap/pybind11/include/pybind11/detail/common.h:365
unpacking_collector::args
tuple args() &&
Definition: cast.h:1675
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:740
unpacking_collector::m_args
tuple m_args
Definition: cast.h:1772
external::detail::conv
PyObject * conv(PyObject *o)
Definition: test_pytypes.cpp:20
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:1689
name
Annotation for function names.
Definition: attr.h:51
return_value_policy_override
Definition: cast.h:1122
function_record
Definition: attr.h:191
cast
T cast(const handle &handle)
Definition: cast.h:1173
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:1662
type_caster< void >::cast
static handle cast(const void *ptr, return_value_policy, handle)
Definition: cast.h:296
set
Definition: pytypes.h:2232
index_sequence
Index sequences.
Definition: wrap/pybind11/include/pybind11/detail/common.h:685
argument_loader::load_args
bool load_args(function_call &call)
Definition: cast.h:1587
arg::arg
constexpr arg(const char *name=nullptr)
Definition: cast.h:1415
gtsam.examples.DogLegOptimizerExample.type
type
Definition: DogLegOptimizerExample.py:111
ssize_t
Py_ssize_t ssize_t
Definition: wrap/pybind11/include/pybind11/detail/common.h:489
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:2213
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:499
void_caster::load
bool load(handle src, bool)
Definition: cast.h:250
list
Definition: pytypes.h:2166
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:777
type_caster_generic::value
void * value
Definition: type_caster_base.h:828
remove_cv_t
typename std::remove_cv< T >::type remove_cv_t
Definition: wrap/pybind11/include/pybind11/detail/common.h:658
literals
Definition: cast.h:1511
argument_loader::call
enable_if_t<!std::is_void< Return >::value, Return > call(Func &&f) &&
Definition: cast.h:1591
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:1541
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:1130
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:375
argument_loader::argument_is_args
std::is_same< intrinsic_t< Arg >, args > argument_is_args
Definition: cast.h:1566
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:1642
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:1767
hasattr
bool hasattr(handle obj, handle name)
Definition: pytypes.h:870
capsule
Definition: pytypes.h:1951
move_only_holder_caster::cast
static handle cast(holder_type &&src, return_value_policy, handle)
Definition: cast.h:843
arg_v::arg_v
arg_v(arg &&base, T &&x, const char *descr=nullptr)
Definition: cast.h:1442
tuple_caster::type
Tuple< Ts... > type
Definition: cast.h:640
void_caster
Definition: cast.h:248
type
Definition: pytypes.h:1525
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:1084
value_and_holder
Definition: type_caster_base.h:262
T
Eigen::Triplet< double > T
Definition: Tutorial_sparse_example.cpp:6
object_api::operator()
object operator()(Args &&...args) const
Definition: cast.h:1810
argument_loader::load_impl_sequence
bool load_impl_sequence(function_call &call, index_sequence< Is... >)
Definition: cast.h:1607
argument_loader::arg_names
static constexpr auto arg_names
Definition: cast.h:1585
different_sigmas::values
HybridValues values
Definition: testHybridBayesNet.cpp:245
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:1328
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:879
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:2270
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:1789
iterator
Definition: pytypes.h:1460
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:1005
arg::operator=
arg_v operator=(T &&value) const
Assign a value to this argument.
Definition: cast.h:1503
always_construct_holder
Definition: cast.h:860
all_type_info
const std::vector< detail::type_info * > & all_type_info(PyTypeObject *type)
Definition: type_caster_base.h:173
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:1597
weakref
Definition: pytypes.h:1892
copyable_holder_caster::try_direct_conversions
static bool try_direct_conversions(handle)
Definition: cast.h:826
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:1554
conditional_t
typename std::conditional< B, T, F >::type conditional_t
Definition: wrap/pybind11/include/pybind11/detail/common.h:656
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:1798
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:583
type_name
string type_name()
Definition: benchmark-blocking-sizes.cpp:252
function_call
Internal data associated with a single function call.
Definition: cast.h:1537
type_caster< std::reference_wrapper< type > >::load
bool load(handle src, bool convert)
Definition: cast.h:71
override_unused
Definition: cast.h:1319
simple_collector::simple_collector
simple_collector(Ts &&...values)
Definition: cast.h:1636
is_pos_only
std::is_same< intrinsic_t< T >, pos_only > is_pos_only
Definition: cast.h:1531
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:1734
arg_v::type
std::string type
The C++ type name of the default value (only available when compiled in debug mode)
Definition: cast.h:1488
slice
Definition: pytypes.h:1909
always_construct_holder::value
static constexpr bool value
Definition: cast.h:861
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:1105
copyable_holder_caster::check_holder_compat
void check_holder_compat()
Definition: cast.h:784
simple_collector::call
object call(PyObject *ptr) const
Call a Python function and pass the collected arguments.
Definition: cast.h:1645
is_kw_only
std::is_same< intrinsic_t< T >, kw_only > is_kw_only
Definition: cast.h:1529
arg::name
const char * name
If non-null, this is a named kwargs argument.
Definition: cast.h:1431
Object
Reference counted object base class.
Definition: object.h:9
simple_collector::args
const tuple & args() const &
Definition: cast.h:1639
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:2107
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:1462
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:1654
return_value_policy::automatic
@ automatic
intrinsic_t
typename intrinsic_type< T >::type intrinsic_t
Definition: wrap/pybind11/include/pybind11/detail/common.h:812
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:1323
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:2146
handle::cast
T cast() const
Definition: cast.h:1234
arg_v::value
object value
The default value.
Definition: cast.h:1483
type::handle_of
static handle handle_of()
Definition: cast.h:1828
make_tuple
tuple make_tuple()
Definition: cast.h:1383
function_call::init_self
handle init_self
If this is a call to an initializer, this argument contains self
Definition: cast.h:1557
arg_v
Definition: cast.h:1439
return_value_policy_override::policy
static return_value_policy policy(return_value_policy p)
Definition: cast.h:1123
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:1116
void_caster::cast
static handle cast(T, return_value_policy, handle)
Definition: cast.h:256
arg::flag_noconvert
bool flag_noconvert
Definition: cast.h:1432
typeid.h
copyable_holder_caster::load
bool load(handle src, bool convert)
Definition: cast.h:766
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:1412
return_value_policy::reference
@ reference
PYBIND11_DETAILED_ERROR_MESSAGES
#define PYBIND11_DETAILED_ERROR_MESSAGES
Definition: wrap/pybind11/include/pybind11/detail/common.h:1264
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:1676
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:713
pyobject_caster::load
bool load(handle src, bool)
Definition: cast.h:1048
unpacking_collector::process
void process(list &args_list, detail::args_proxy ap)
Definition: cast.h:1703
arg::flag_none
bool flag_none
If set (the default), allow None to be passed to this argument.
Definition: cast.h:1434
unpacking_collector::unpacking_collector
unpacking_collector(Ts &&...values)
Definition: cast.h:1662
unpacking_collector::kwargs
const dict & kwargs() const &
Definition: cast.h:1673
bytearray
Definition: pytypes.h:1756
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:746
unpacking_collector::m_kwargs
dict m_kwargs
Definition: cast.h:1773
is_copy_constructible
Definition: type_caster_base.h:1024
memoryview
Definition: pytypes.h:2288
move_if_unreferenced
Definition: cast.h:1095
argument_loader::call_impl
Return call_impl(Func &&f, index_sequence< Is... >, Guard &&) &&
Definition: cast.h:1623
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:1375
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:1038
pybind11_fail
PyExc_RuntimeError PYBIND11_NOINLINE void pybind11_fail(const char *reason)
Used internally.
Definition: wrap/pybind11/include/pybind11/detail/common.h:1026
simple_collector
Definition: cast.h:1633
kw_only
Definition: cast.h:1495
argument_loader::indices
make_index_sequence< sizeof...(Args)> indices
Definition: cast.h:1563
argument_loader::args_pos
static constexpr int args_pos
Definition: cast.h:1579
value_and_holder::value_ptr
V *& value_ptr() const
Definition: type_caster_base.h:281
type_caster< void >::load
bool load(handle h, bool)
Definition: cast.h:270
function_call::args_ref
object args_ref
Definition: cast.h:1551
size_t
std::size_t size_t
Definition: wrap/pybind11/include/pybind11/detail/common.h:490
unpacking_collector::nameless_argument_error
static void nameless_argument_error(const std::string &type)
Definition: cast.h:1756
arg_v::noconvert
arg_v & noconvert(bool flag=true)
Same as arg::noconvert(), but returns *this as arg_v&, not arg&.
Definition: cast.h:1471
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:841
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:1570
pytypes.h
descr.h
type_caster_generic
Definition: type_caster_base.h:531
PYBIND11_BYTES_NAME
#define PYBIND11_BYTES_NAME
Definition: wrap/pybind11/include/pybind11/detail/common.h:370
str
Definition: pytypes.h:1558
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:711
unpacking_collector::multiple_values_error
static void multiple_values_error()
Definition: cast.h:1761
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:790
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:1426
unpacking_collector
Helper class which collects positional, keyword, * and ** arguments for a Python function call.
Definition: cast.h:1659
arg_v::descr
const char * descr
The (optional) description of the default value.
Definition: cast.h:1485
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:1243
PYBIND11_WORKAROUND_INCORRECT_MSVC_C4100
#define PYBIND11_WORKAROUND_INCORRECT_MSVC_C4100(...)
Definition: wrap/pybind11/include/pybind11/detail/common.h:1230
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:828
make_index_sequence
typename make_index_sequence_impl< N >::type make_index_sequence
Definition: wrap/pybind11/include/pybind11/detail/common.h:693
dict::contains
bool contains(T &&key) const
Definition: pytypes.h:2128
handle_type_name
Definition: cast.h:894
handle::ptr
PyObject * ptr() const
Return the underlying PyObject * pointer.
Definition: pytypes.h:250
tuple::size
size_t size() const
Definition: pytypes.h:2088
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:859
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:1568
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:1345
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:857
argument_loader::load_impl_sequence
static bool load_impl_sequence(function_call &, index_sequence<>)
Definition: cast.h:1604
argument_loader
Helper class which loads arguments for C++ functions called from Python.
Definition: cast.h:1562
iterable
Definition: pytypes.h:1551
copyable_holder_caster::try_implicit_casts
bool try_implicit_casts(handle, bool)
Definition: cast.h:808
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:241
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:815
args
Definition: pytypes.h:2210
unpacking_collector::call
object call(PyObject *ptr) const
Call a Python function and pass the collected arguments.
Definition: cast.h:1679
move_only_holder_caster
Definition: cast.h:839
p
float * p
Definition: Tutorial_Map_using.cpp:9
unpacking_collector::nameless_argument_error
static void nameless_argument_error()
Definition: cast.h:1750
c_str
const char * c_str(Args &&...args)
Definition: internals.h:684
argument_loader::argcasters
std::tuple< make_caster< Args >... > argcasters
Definition: cast.h:1627
holder_helper::get
static auto get(const T &p) -> decltype(p.get())
Definition: cast.h:747
tuple
Definition: pytypes.h:2077
is_pyobject
std::is_base_of< pyobject_tag, remove_reference_t< T > > is_pyobject
Definition: pytypes.h:73
float_
Definition: pytypes.h:1870
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:1709
pos_only
Definition: cast.h:1500
U
@ U
Definition: testDecisionTree.cpp:342
pybind11
Definition: wrap/pybind11/pybind11/__init__.py:1
function
Definition: pytypes.h:2252
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:826
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:1238
type_caster_generic::load
bool load(handle src, bool convert)
Definition: type_caster_base.h:539
function_call::args
std::vector< handle > args
Arguments passed to the function:
Definition: cast.h:1544
object_or_cast
object object_or_cast(T &&o)
Definition: cast.h:1313
type_caster_base::cast_holder
static handle cast_holder(const itype *src, const void *holder)
Definition: type_caster_base.h:1152
pyobject_caster::cast
static handle cast(const handle &src, return_value_policy, handle)
Definition: cast.h:1062
object::cast
T cast() const &
Definition: cast.h:1293
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:756
simple_collector::kwargs
dict kwargs() const
Definition: cast.h:1640
Tuple
Definition: typing.h:29
none
Definition: pytypes.h:1786
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:1421
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:1551
copyable_holder_caster::try_implicit_casts
bool try_implicit_casts(handle src, bool convert)
Definition: cast.h:814
frozenset
Definition: pytypes.h:2247
unpacking_collector::args
const tuple & args() const &
Definition: cast.h:1672
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:994
values_and_holders
Definition: type_caster_base.h:324
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:1576
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:654
test_callbacks.value
value
Definition: test_callbacks.py:160
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:1467
loader_life_support::add_patient
static PYBIND11_NOINLINE void add_patient(handle h)
Definition: type_caster_base.h:71
function_call::args_convert
std::vector< bool > args_convert
The convert value the arguments should be loaded with.
Definition: cast.h:1547
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:1792
args_are_all_positional
constexpr bool args_are_all_positional()
Definition: cast.h:1781
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:1477
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:1096
type_caster_base.h
anyset
Definition: pytypes.h:2217
type_caster< bool >::is_numpy_bool
static bool is_numpy_bool(handle object)
Definition: cast.h:367


gtsam
Author(s):
autogenerated on Sun Dec 22 2024 04:11:14