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