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


gtsam
Author(s):
autogenerated on Tue Jul 4 2023 02:34:01