13#include "detail/common.h"
14#include "detail/descr.h"
15#include "detail/type_caster_base.h"
16#include "detail/typeid.h"
37template <typename
type, typename SFINAE =
void>
39template <
typename type>
54template <
typename type>
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 &()`");
70 static constexpr auto name = caster_t::name;
74 if (policy == return_value_policy::take_ownership
75 || policy == return_value_policy::automatic) {
76 policy = return_value_policy::automatic_reference;
78 return caster_t::cast(&src.get(), policy, parent);
82 explicit operator std::reference_wrapper<type>() {
return cast_op<type &>(subcaster); }
85#define PYBIND11_TYPE_CASTER(type, py_name) \
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, \
96 static ::pybind11::handle cast( \
97 T_ *src, ::pybind11::return_value_policy policy, ::pybind11::handle parent) { \
99 return ::pybind11::none().release(); \
100 if (policy == ::pybind11::return_value_policy::take_ownership) { \
101 auto h = cast(std::move(*src), policy, parent); \
105 return cast(*src, policy, parent); \
107 operator type *() { return &value; } \
108 operator type &() { return value; } \
109 operator type &&() && { return std::move(value); } \
110 template <typename T_> \
111 using cast_op_type = ::pybind11::detail::movable_cast_op_type<T_>
113template <
typename CharT>
115#if defined(PYBIND11_HAS_U8STRING)
116 std::is_same<CharT, char8_t>,
118 std::is_same<CharT, char16_t>,
119 std::is_same<CharT, char32_t>,
120 std::is_same<CharT, wchar_t>
128 typename std::make_unsigned<_py_type_0>::type>;
139#if !defined(PYPY_VERSION)
140 auto index_check = [](PyObject *o) {
return PyIndex_Check(o); };
144 auto index_check = [](PyObject *o) {
return hasattr(o,
"__index__"); };
147 if (std::is_floating_point<T>::value) {
148 if (convert || PyFloat_Check(src.
ptr())) {
149 py_value = (
py_type) PyFloat_AsDouble(src.
ptr());
153 }
else if (PyFloat_Check(src.
ptr())
157 handle src_or_index = src;
159#if PY_VERSION_HEX < 0x03080000 || defined(PYPY_VERSION)
162 index = reinterpret_steal<object>(PyNumber_Index(src.
ptr()));
168 src_or_index = index;
172 if (std::is_unsigned<py_type>::value) {
173 py_value = as_unsigned<py_type>(src_or_index.
ptr());
175 py_value =
sizeof(T) <=
sizeof(
long)
182 bool py_err = py_value == (
py_type) -1 && PyErr_Occurred();
187 || (std::is_integral<T>::value &&
sizeof(
py_type) !=
sizeof(T)
188 && py_value != (
py_type) (T) py_value)) {
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()));
195 return load(tmp,
false);
200 value = (T) py_value;
204 template <
typename U = T>
205 static typename std::enable_if<std::is_floating_point<U>::value,
handle>
::type
207 return PyFloat_FromDouble((
double) src);
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)),
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)),
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)),
231 return PyLong_FromLongLong((
long long) src);
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)),
239 return PyLong_FromUnsignedLongLong((
unsigned long long) src);
249 if (src && src.is_none()) {
278 if (isinstance<capsule>(h)) {
279 value = reinterpret_borrow<capsule>(h);
285 if (bases.size() == 1) {
301 template <
typename T>
303 explicit operator void *&() {
return value; }
307 void *
value =
nullptr;
320 if (src.
ptr() == Py_True) {
324 if (src.
ptr() == Py_False) {
328 if (convert || (std::strcmp(
"numpy.bool_", Py_TYPE(src.
ptr())->tp_name) == 0)) {
335#if defined(PYPY_VERSION)
338 res = PyObject_IsTrue(src.
ptr());
343 else if (
auto *tp_as_number = src.
ptr()->ob_type->tp_as_number) {
349 if (res == 0 || res == 1) {
364template <
typename StringType,
bool IsView = false>
366 using CharT =
typename StringType::value_type;
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");
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");
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);
390 if (!PyUnicode_Check(load_src.
ptr())) {
397 Py_ssize_t size = -1;
399 =
reinterpret_cast<const CharT *
>(PyUnicode_AsUTF8AndSize(load_src.
ptr(), &size));
404 value = StringType(
buffer,
static_cast<size_t>(size));
409 = reinterpret_steal<object>(PyUnicode_AsEncodedString(load_src.
ptr(),
411 :
UTF_N == 16 ?
"utf-16"
427 value = StringType(
buffer, length);
439 const char *
buffer =
reinterpret_cast<const char *
>(src.data());
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);
460 return PyUnicode_Decode(
buffer,
463 :
UTF_N == 16 ?
"utf-16"
472 template <
typename C = CharT>
479 pybind11_fail(
"Unexpected PYBIND11_BYTES_AS_STRING() failure.");
484 if (PyByteArray_Check(src.ptr())) {
487 const char *
bytearray = PyByteArray_AsString(src.ptr());
491 value = StringType(
bytearray, (
size_t) PyByteArray_Size(src.ptr()));
498 template <
typename C = CharT>
504template <
typename CharT,
class Traits,
class Allocator>
507 :
string_caster<std::basic_string<CharT, Traits, Allocator>> {};
509#ifdef PYBIND11_HAS_STRING_VIEW
510template <
typename CharT,
class Traits>
513 :
string_caster<std::basic_string_view<CharT, Traits>, true> {};
518template <
typename CharT>
539 return str_caster.
load(src, convert);
543 if (src ==
nullptr) {
544 return pybind11::none().release();
546 return StringCaster::cast(
StringType(src), policy, parent);
550 if (std::is_same<char, CharT>::value) {
551 handle s = PyUnicode_DecodeLatin1((
const char *) &src, 1,
nullptr);
557 return StringCaster::cast(
StringType(1, src), policy, parent);
560 explicit operator CharT *() {
561 return none ? nullptr :
const_cast<CharT *
>(
static_cast<StringType &
>(str_caster).
c_str());
563 explicit operator CharT &() {
565 throw value_error(
"Cannot convert None to a character");
569 size_t str_len =
value.size();
571 throw value_error(
"Cannot convert empty string to a character");
579 if (StringCaster::UTF_N == 8 && str_len > 1 && str_len <= 4) {
580 auto v0 =
static_cast<unsigned char>(
value[0]);
585 size_t char0_bytes = (v0 & 0x80) == 0 ? 1
586 : (v0 & 0xE0) == 0xC0 ? 2
587 : (v0 & 0xF0) == 0xE0 ? 3
590 if (char0_bytes == str_len) {
592 if (char0_bytes == 2 && (v0 & 0xFC) == 0xC0) {
593 one_char =
static_cast<CharT
>(((v0 & 3) << 6)
594 + (
static_cast<unsigned char>(
value[1]) & 0x3F));
598 throw value_error(
"Character code point not in range(0x100)");
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)");
613 throw value_error(
"Expected a character, but multi-character string found");
621 template <
typename _T>
626template <
template <
typename...>
class Tuple,
typename... Ts>
629 static constexpr auto size =
sizeof...(Ts);
634 if (!isinstance<sequence>(src)) {
637 const auto seq = reinterpret_borrow<sequence>(src);
638 if (seq.size() !=
size) {
644 template <
typename T>
650 template <
typename T>
655 if (policy == return_value_policy::take_ownership) {
656 auto h =
cast(std::move(*src), policy, parent);
660 return cast(*src, policy, parent);
663 static constexpr auto name
666 template <
typename T>
670 explicit operator type() && {
return std::move(*this).implicit_cast(
indices{}); }
673 template <
size_t... Is>
677 template <
size_t... Is>
684 template <
size_t... Is>
686#ifdef __cpp_fold_expressions
691 for (
bool r : {std::get<Is>(
subcasters).load(seq[Is], convert)...}) {
701 template <
typename T,
size_t... Is>
706 std::array<object, size> entries{{reinterpret_steal<object>(
708 for (
const auto &entry : entries) {
715 for (
auto &entry : entries) {
716 PyTuple_SET_ITEM(result.ptr(), counter++, entry.release().ptr());
718 return result.release();
724template <
typename T1,
typename T2>
727template <
typename... Ts>
734 static auto get(
const T &p) ->
decltype(p.get()) {
return p.get(); }
742template <
typename type,
typename holder_type,
typename SFINAE =
void>
746 static_assert(std::is_base_of<base, type_caster<type>>
::value,
747 "Holder classes are only supported for custom types");
754 return base::template load_impl<copyable_holder_caster<type, holder_type>>(src, convert);
761 explicit operator holder_type *() {
return std::addressof(
holder); }
762 explicit operator holder_type &() {
return holder; }
773 throw cast_error(
"Unable to load a custom holder type from a default-holder instance");
778 if (v_h.holder_constructed()) {
779 value = v_h.value_ptr();
780 holder = v_h.template holder<holder_type>();
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)");
789 + type_id<holder_type>() +
"''");
793 template <
typename T = holder_type,
794 detail::enable_if_t<!std::is_constructible<T, const T &, type *>::value,
int> = 0>
799 template <
typename T = holder_type,
800 detail::enable_if_t<std::is_constructible<T, const T &, type *>::value,
int> = 0>
804 if (sub_caster.
load(src, convert)) {
825template <
typename type,
typename holder_type,
typename SFINAE =
void>
828 "Holder classes are only supported for custom types");
837template <
typename type,
typename deleter>
841template <
typename type,
typename holder_type>
846template <
typename T,
bool Value = false>
848 static constexpr bool value = Value;
852#define PYBIND11_DECLARE_HOLDER_TYPE(type, holder_type, ...) \
853 PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE) \
855 template <typename type> \
856 struct always_construct_holder<holder_type> : always_construct_holder<void, ##__VA_ARGS__> { \
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> {}; \
862 PYBIND11_NAMESPACE_END(PYBIND11_NAMESPACE)
865template <
typename base,
typename holder>
867 : std::is_base_of<detail::type_caster_holder<base, holder>, detail::type_caster<holder>> {};
869template <
typename base,
typename deleter>
874 static constexpr auto name = const_name<T>();
913template <
typename type>
915 template <typename T = type, enable_if_t<std::is_same<T, handle>::value,
int> = 0>
920 template <typename T = type, enable_if_t<std::is_base_of<object, T>::value,
int> = 0>
923 template <typename T = type, enable_if_t<std::is_same<T, handle>::value,
int> = 0>
926 return static_cast<bool>(value);
929 template <typename T = type, enable_if_t<std::is_base_of<object, T>::value,
int> = 0>
931 if (!isinstance<type>(src)) {
934 value = reinterpret_borrow<type>(src);
959template <
typename T,
typename SFINAE =
void>
967 std::is_move_constructible<T>,
968 std::is_same<decltype(std::declval<make_caster<T>>().operator T &()), T &>>::value>>
970template <
typename T,
typename SFINAE =
void>
978 std::is_move_constructible<T>,
979 std::is_same<decltype(std::declval<make_caster<T>>().operator T &()), T &>>::value>>
988template <
typename type>
990 =
bool_constant<(std::is_reference<type>::value || std::is_pointer<type>::value)
997template <
typename Return,
typename SFINAE =
void>
1002template <
typename 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
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)");
1023 throw cast_error(
"Unable to cast Python instance of type "
1025 + type_id<T>() +
"'");
1031template <
typename T>
1041template <typename T, detail::enable_if_t<!detail::is_pyobject<T>::value,
int> = 0>
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));
1050template <typename T, detail::enable_if_t<detail::is_pyobject<T>::value,
int> = 0>
1052 return T(reinterpret_borrow<object>(
handle));
1056template <typename T, detail::enable_if_t<!detail::is_pyobject<T>::value,
int> = 0>
1060 using no_ref_T =
typename std::remove_reference<T>::type;
1070 return reinterpret_steal<object>(
1074template <
typename T>
1076 return pybind11::cast<T>(*
this);
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)
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)");
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");
1098 T ret = std::move(detail::load_type<T>(obj).
operator T &());
1107template <
typename T>
1108detail::enable_if_t<!detail::is_pyobject<T>::value && detail::move_always<T>::value, T>
1110 return move<T>(std::move(
object));
1112template <
typename T>
1113detail::enable_if_t<!detail::is_pyobject<T>::value && detail::move_if_unreferenced<T>::value, T>
1115 if (
object.ref_count() > 1) {
1116 return cast<T>(
object);
1118 return move<T>(std::move(
object));
1120template <
typename T>
1121detail::enable_if_t<!detail::is_pyobject<T>::value && detail::move_never<T>::value, T>
1123 return cast<T>(
object);
1127template <
typename T>
1128detail::enable_if_t<detail::is_pyobject<T>::value, T>
cast(
object &&
object) {
1129 return T(std::move(
object));
1132template <
typename T>
1134 return pybind11::cast<T>(*
this);
1136template <
typename T>
1138 return pybind11::cast<T>(std::move(*
this));
1152template <typename T, enable_if_t<!is_pyobject<T>::value,
int>>
1154 return pybind11::cast(std::forward<T>(o));
1160template <
typename ret_type>
1167template <
typename T>
1170 return cast_op<T>(
load_type(caster, o));
1172template <
typename T>
1181template <
typename T>
1183 pybind11_fail(
"Internal error: cast_safe fallback invoked");
1185template <
typename T>
1187template <
typename T>
1190 return pybind11::cast<T>(std::move(o));
1197#if !defined(PYBIND11_DETAILED_ERROR_MESSAGES)
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)");
1204 const std::string &
type) {
1205 return cast_error(
"Unable to convert call argument '" +
name +
"' of type '" +
type
1206 +
"' to Python object");
1210template <return_value_policy policy = return_value_policy::automatic_reference>
1217 constexpr size_t size =
sizeof...(Args);
1218 std::array<object, size>
args{{reinterpret_steal<object>(
1220 for (
size_t i = 0; i <
args.
size(); i++) {
1222#if !defined(PYBIND11_DETAILED_ERROR_MESSAGES)
1225 std::array<std::string, size> argtypes{{type_id<Args>()...}};
1232 for (
auto &arg_value :
args) {
1233 PyTuple_SET_ITEM(result.ptr(), counter++, arg_value.release().ptr());
1243 constexpr explicit arg(
const char *
name =
nullptr)
1246 template <
typename T>
1269 template <
typename T>
1274#if defined(PYBIND11_DETAILED_ERROR_MESSAGES)
1282 if (PyErr_Occurred()) {
1289 template <
typename T>
1294 template <
typename T>
1314#if defined(PYBIND11_DETAILED_ERROR_MESSAGES)
1330template <
typename T>
1332 return {*
this, std::forward<T>(value)};
1343constexpr arg operator"" _a(
const char *
name,
size_t) {
return arg(
name); }
1348template <
typename T>
1350template <
typename T>
1364 std::vector<handle>
args;
1381template <
typename... Args>
1385 template <
typename Arg>
1387 template <
typename Arg>
1393 "py::kwargs is only permitted as the last argument of a function");
1402 "py::args cannot be specified more than once");
1408 template <
typename Return,
typename Guard,
typename Func>
1412 std::forward<Func>(f),
indices{}, Guard{});
1415 template <
typename Return,
typename Guard,
typename Func>
1418 std::forward<Func>(f),
indices{}, Guard{});
1425 template <
size_t... Is>
1427#ifdef __cpp_fold_expressions
1432 for (
bool r : {std::get<Is>(
argcasters).load(
call.args[Is],
call.args_convert[Is])...}) {
1441 template <
typename Return,
typename Func,
size_t... Is,
typename Guard>
1443 return std::forward<Func>(f)(cast_op<Args>(std::move(std::get<Is>(
argcasters)))...);
1451template <return_value_policy policy>
1454 template <
typename... Ts>
1465 PyObject *result = PyObject_CallObject(ptr,
m_args.
ptr());
1469 return reinterpret_steal<object>(result);
1477template <return_value_policy policy>
1480 template <
typename... Ts>
1484 auto args_list =
list();
1485 using expander =
int[];
1486 (void) expander{0, (
process(args_list, std::forward<Ts>(values)), 0)...};
1488 m_args = std::move(args_list);
1503 return reinterpret_steal<object>(result);
1507 template <
typename T>
1509 auto o = reinterpret_steal<object>(
1512#if !defined(PYBIND11_DETAILED_ERROR_MESSAGES)
1519 args_list.
append(std::move(o));
1530#if !defined(PYBIND11_DETAILED_ERROR_MESSAGES)
1537#if !defined(PYBIND11_DETAILED_ERROR_MESSAGES)
1544#if !defined(PYBIND11_DETAILED_ERROR_MESSAGES)
1557 for (
auto k : reinterpret_borrow<dict>(kp)) {
1559#if !defined(PYBIND11_DETAILED_ERROR_MESSAGES)
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)");
1576 throw type_error(
"Got kwargs without a name of type '" +
type
1578 "arguments may be passed via py::arg() to a python function call. ");
1582 "Got multiple values for keyword argument "
1583 "(#define PYBIND11_DETAILED_ERROR_MESSAGES or compile in debug mode for details)");
1587 throw type_error(
"Got multiple values for keyword argument '" +
name +
"'");
1599template <
typename... Args>
1619 < constexpr_first<is_keyword_or_ds, Args...>()
1621 < constexpr_first<is_ds_unpacking, Args...>(),
1622 "Invalid function call: positional args must precede keywords and ** unpacking; "
1623 "* unpacking must precede ** unpacking");
1627template <
typename Derived>
1631 if (!PyGILState_Check()) {
1632 pybind11_fail(
"pybind11::object_api<>::operator() PyGILState_Check() failure.");
1635 return detail::collect_arguments<policy>(std::forward<Args>(
args)...).call(derived().ptr());
1638template <
typename Derived>
1641 return operator()<policy>(std::forward<Args>(
args)...);
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.");
1651 return detail::get_type_handle(
typeid(T),
true);
1654#define PYBIND11_MAKE_OPAQUE(...) \
1655 PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE) \
1656 namespace detail { \
1658 class type_caster<__VA_ARGS__> : public type_caster_base<__VA_ARGS__> {}; \
1660 PYBIND11_NAMESPACE_END(PYBIND11_NAMESPACE)
1665#define PYBIND11_TYPE(...) __VA_ARGS__
Helper class which loads arguments for C++ functions called from Python.
bool load_impl_sequence(function_call &call, index_sequence< Is... >)
Return call_impl(Func &&f, index_sequence< Is... >, Guard &&) &&
enable_if_t< std::is_void< Return >::value, void_type > call(Func &&f) &&
std::is_same< intrinsic_t< Arg >, args > argument_is_args
static bool load_impl_sequence(function_call &, index_sequence<>)
make_index_sequence< sizeof...(Args)> indices
static constexpr auto arg_names
std::tuple< make_caster< Args >... > argcasters
static constexpr int args_pos
std::is_same< intrinsic_t< Arg >, kwargs > argument_is_kwargs
bool load_args(function_call &call)
enable_if_t<!std::is_void< Return >::value, Return > call(Func &&f) &&
static constexpr auto kwargs_pos
static constexpr bool has_kwargs
bool contains(T &&key) const
Fetch and hold an error which was already set in Python.
\rst Holds a reference to a Python object (no reference counting)
const handle & inc_ref() const &
\rst Manually increase the reference count of the Python object.
T cast() const
\rst Attempt to cast the Python object into the given C++ type.
PyObject * ptr() const
Return the underlying PyObject * pointer.
\rst Wraps a Python iterator so that it can also be used as a C++ input iterator
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...
\rst A mixin class which adds common functions to handle, object and various accessors.
object operator()(Args &&...args) const
\rst Assuming the Python object is a function or implements the __call__ protocol,...
\rst Holds a reference to a Python object (with reference counting)
handle release()
\rst Resets the internal pointer to nullptr without decreasing the object's reference count.
Helper class which collects only positional arguments for a Python function call.
simple_collector(Ts &&...values)
object call(PyObject *ptr) const
Call a Python function and pass the collected arguments.
const tuple & args() const &
static handle cast(T *src, return_value_policy policy, handle parent)
static constexpr auto size
type implicit_cast(index_sequence< Is... >) &&
bool load(handle src, bool convert)
static constexpr bool load_impl(const sequence &, bool, index_sequence<>)
static handle cast_impl(T &&src, return_value_policy policy, handle parent, index_sequence< Is... >)
bool load_impl(const sequence &seq, bool convert, index_sequence< Is... >)
type implicit_cast(index_sequence< Is... >) &
Tuple< make_caster< Ts >... > subcasters
static handle cast(T &&src, return_value_policy policy, handle parent)
make_index_sequence< size > indices
PYBIND11_TYPE_CASTER(bool, const_name("bool"))
bool load(handle src, bool convert)
static handle cast(bool src, return_value_policy, handle)
static handle cast(const std::reference_wrapper< type > &src, return_value_policy policy, handle parent)
std::reference_wrapper< type > cast_op_type
typename caster_t::template cast_op_type< reference_t > subcaster_cast_op_type
bool load(handle src, bool convert)
bool load(handle h, bool)
static handle cast(const void *ptr, return_value_policy, handle)
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)
static handle handle_of()
Convert C++ type to handle if previously registered.
Helper class which collects positional, keyword, * and ** arguments for a Python function call.
static void multiple_values_error()
static void nameless_argument_error()
static void multiple_values_error(const std::string &name)
void process(list &, detail::kwargs_proxy kp)
object call(PyObject *ptr) const
Call a Python function and pass the collected arguments.
const tuple & args() const &
unpacking_collector(Ts &&...values)
const dict & kwargs() const &
void process(list &args_list, detail::args_proxy ap)
static void nameless_argument_error(const std::string &type)
void process(list &args_list, T &&x)
void process(list &, arg_v a)
bool hasattr(handle obj, handle name)
conditional_t< is_copy_constructible< holder_type >::value, copyable_holder_caster< type, holder_type >, move_only_holder_caster< type, holder_type > > type_caster_holder
std::is_same< intrinsic_t< T >, pos_only > is_pos_only
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
constexpr bool args_are_all_positional()
T cast(const handle &handle)
cast_error cast_error_unable_to_convert_call_arg(const std::string &name, const std::string &type)
object object_or_cast(T &&o)
conditional_t< cast_is_temporary_value_reference< ret_type >::value, make_caster< ret_type >, override_unused > override_caster_t
enable_if_t< cast_is_temporary_value_reference< T >::value, T > cast_ref(object &&o, make_caster< T > &caster)
enable_if_t<!cast_is_temporary_value_reference< T >::value, T > cast_safe(object &&o)
make_caster< T >::template cast_op_type< T > cast_op(make_caster< T > &caster)
simple_collector< policy > collect_arguments(Args &&...args)
Collect only positional arguments for a Python function call.
std::is_same< intrinsic_t< T >, kw_only > is_kw_only
type_caster< T, SFINAE > & load_type(type_caster< T, SFINAE > &conv, const handle &handle)
typename std::enable_if< B, T >::type enable_if_t
from cpp_future import (convenient aliases from C++14/17)
std::integral_constant< bool, B > bool_constant
Backports of std::bool_constant and std::negation to accommodate older compilers.
#define PYBIND11_LONG_AS_LONGLONG(o)
PYBIND11_NOINLINE void pybind11_fail(const char *reason)
Thrown when pybind11::cast or.
constexpr int constexpr_first()
Return the index of the first type in Ts which satisfies Predicate<T>.
typename intrinsic_type< T >::type intrinsic_t
#define PYBIND11_STRING_NAME
#define PYBIND11_BYTES_SIZE
#define PYBIND11_BYTES_CHECK
#define PYBIND11_LONG_CHECK(o)
typename std::remove_cv< T >::type remove_cv_t
#define PYBIND11_WORKAROUND_INCORRECT_MSVC_C4100(...)
#define PYBIND11_NAMESPACE_END(name)
#define PYBIND11_BYTES_NAME
#define PYBIND11_NAMESPACE_BEGIN(name)
#define PYBIND11_WORKAROUND_INCORRECT_GCC_UNUSED_BUT_SET_PARAMETER(...)
#define PYBIND11_BYTES_AS_STRING
typename make_index_sequence_impl< N >::type make_index_sequence
std::is_same< bools< Ts::value..., true >, bools< true, Ts::value... > > all_of
typename std::conditional< B, T, F >::type conditional_t
#define PYBIND11_BOOL_ATTR
constexpr int constexpr_last()
Return the index of the last type in Ts which satisfies Predicate<T>, or -1 if none match.
return_value_policy
Approach used to cast a previously unknown C++ instance into a Python object.
@ 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)
#define PYBIND11_NB_BOOL(ptr)
#define PYBIND11_LONG_FROM_SIGNED(o)
constexpr descr< N - 1 > const_name(char const (&text)[N])
constexpr descr< N+2, Ts... > type_descr(const descr< N, Ts... > &descr)
constexpr descr< 0 > concat()
const char * c_str(Args &&...args)
Constructs a std::string with the given arguments, stores it in internals, and returns its c_str().
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
T reinterpret_steal(handle h)
\rst Like reinterpret_borrow, but steals the reference.
std::is_same< args_proxy, T > is_s_unpacking
satisfies_none_of< T, is_keyword, is_s_unpacking, is_ds_unpacking > is_positional
T cast(const handle &handle)
cast_error cast_error_unable_to_convert_call_arg(const std::string &name, const std::string &type)
#define PYBIND11_DETAILED_ERROR_MESSAGES
#define PYBIND11_WARNING_DISABLE_MSVC(name)
static constexpr bool value
Annotation for arguments with values.
arg_v(const arg &base, T &&x, const char *descr=nullptr)
Called internally when invoking py::arg("a") = value
arg_v & none(bool flag=true)
Same as arg::nonone(), but returns *this as arg_v&, not arg&.
arg_v & noconvert(bool flag=true)
Same as arg::noconvert(), but returns *this as arg_v&, not arg&.
object value
The default value.
std::string type
The C++ type name of the default value (only available when compiled in debug mode)
arg_v(const char *name, T &&x, const char *descr=nullptr)
Direct construction with name, default, and description.
arg_v(arg &&base, T &&x, const char *descr=nullptr)
const char * descr
The (optional) description of the default value.
Annotation for arguments.
arg & noconvert(bool flag=true)
Indicate that the type should not be converted in the type caster.
const char * name
If non-null, this is a named kwargs argument.
arg & none(bool flag=true)
Indicates that the argument should/shouldn't allow None (e.g. for nullable pointer args)
bool flag_none
If set (the default), allow None to be passed to this argument.
constexpr arg(const char *name=nullptr)
Constructs an argument with the name of the argument; if null or omitted, this is a positional argume...
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!)
Annotation indicating that a class derives from another given type.
Type caster for holder types like std::shared_ptr, etc.
bool load_value(value_and_holder &&v_h)
bool load(handle src, bool convert)
void check_holder_compat()
static bool try_direct_conversions(handle)
bool try_implicit_casts(handle src, bool convert)
bool try_implicit_casts(handle, bool)
static handle cast(const holder_type &src, return_value_policy, handle)
Internal data associated with a single function call.
object args_ref
Extra references for the optional py::args and/or py::kwargs arguments (which, if present,...
handle parent
The parent, if any.
function_call(const function_record &f, handle p)
std::vector< bool > args_convert
The convert value the arguments should be loaded with.
handle init_self
If this is a call to an initializer, this argument contains self
const function_record & func
The function data:
std::vector< handle > args
Arguments passed to the function:
Internal data structure which holds metadata about a bound function (signature, overloads,...
Helper class which abstracts away certain actions.
static auto get(const T &p) -> decltype(p.get())
The 'instance' type which needs to be standard layout (need to be able to use 'offsetof')
Annotation indicating that all following arguments are keyword-only; the is the equivalent of an unna...
Type caster for holder types like std::unique_ptr.
static handle cast(holder_type &&src, return_value_policy, handle)
Annotation for function names.
Annotation indicating that all previous arguments are positional-only; the is the equivalent of an un...
PYBIND11_TYPE_CASTER(type, handle_type_name< type >::name)
bool load(handle src, bool)
static handle cast(const handle &src, return_value_policy, handle)
static return_value_policy policy(return_value_policy p)
static return_value_policy policy(return_value_policy p)
typename StringType::value_type CharT
static handle decode_utfN(const char *buffer, ssize_t nbytes)
static constexpr size_t UTF_N
bool load(handle src, bool)
static handle cast(const StringType &src, return_value_policy, handle)
bool load_raw(enable_if_t<!std::is_same< C, char >::value, handle >)
PYBIND11_TYPE_CASTER(StringType, const_name(PYBIND11_STRING_NAME))
bool load_raw(enable_if_t< std::is_same< C, char >::value, handle > src)
bool load(handle src, bool convert)
pybind11::detail::cast_op_type< _T > cast_op_type
static handle cast(const CharT *src, return_value_policy policy, handle parent)
std::basic_string< CharT > StringType
static handle cast(CharT src, return_value_policy policy, handle parent)
conditional_t< sizeof(T)<=sizeof(long), long, long long > _py_type_0
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)
conditional_t< std::is_floating_point< T >::value, double, _py_type_1 > py_type
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)
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)
bool load(handle src, bool convert)
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)
static std::enable_if< std::is_floating_point< U >::value, handle >::type cast(U src, return_value_policy, handle)
conditional_t< std::is_signed< T >::value, _py_type_0, typename std::make_unsigned< _py_type_0 >::type > _py_type_1
std::vector< std::pair< const std::type_info *, void *(*)(void *)> > implicit_casts
PYBIND11_TYPE_CASTER(T, const_name("None"))
static handle cast(T, return_value_policy, handle)
bool load(handle src, bool)
Helper type to replace 'void' in some expressions.