cast.h revision 11986:c12e4625ab56
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 "pytypes.h"
14#include "typeid.h"
15#include "descr.h"
16#include <array>
17#include <limits>
18
19NAMESPACE_BEGIN(pybind11)
20NAMESPACE_BEGIN(detail)
21
22/// Additional type information which does not fit into the PyTypeObject
23struct type_info {
24    PyTypeObject *type;
25    size_t type_size;
26    void (*init_holder)(PyObject *, const void *);
27    std::vector<PyObject *(*)(PyObject *, PyTypeObject *)> implicit_conversions;
28    std::vector<std::pair<const std::type_info *, void *(*)(void *)>> implicit_casts;
29    std::vector<bool (*)(PyObject *, void *&)> *direct_conversions;
30    buffer_info *(*get_buffer)(PyObject *, void *) = nullptr;
31    void *get_buffer_data = nullptr;
32    /** A simple type never occurs as a (direct or indirect) parent
33     * of a class that makes use of multiple inheritance */
34    bool simple_type = true;
35};
36
37PYBIND11_NOINLINE inline internals &get_internals() {
38    static internals *internals_ptr = nullptr;
39    if (internals_ptr)
40        return *internals_ptr;
41    handle builtins(PyEval_GetBuiltins());
42    const char *id = PYBIND11_INTERNALS_ID;
43    if (builtins.contains(id) && isinstance<capsule>(builtins[id])) {
44        internals_ptr = capsule(builtins[id]);
45    } else {
46        internals_ptr = new internals();
47        #if defined(WITH_THREAD)
48            PyEval_InitThreads();
49            PyThreadState *tstate = PyThreadState_Get();
50            internals_ptr->tstate = PyThread_create_key();
51            PyThread_set_key_value(internals_ptr->tstate, tstate);
52            internals_ptr->istate = tstate->interp;
53        #endif
54        builtins[id] = capsule(internals_ptr);
55        internals_ptr->registered_exception_translators.push_front(
56            [](std::exception_ptr p) -> void {
57                try {
58                    if (p) std::rethrow_exception(p);
59                } catch (error_already_set &e)           { e.restore();                                    return;
60                } catch (const builtin_exception &e)     { e.set_error();                                  return;
61                } catch (const std::bad_alloc &e)        { PyErr_SetString(PyExc_MemoryError,   e.what()); return;
62                } catch (const std::domain_error &e)     { PyErr_SetString(PyExc_ValueError,    e.what()); return;
63                } catch (const std::invalid_argument &e) { PyErr_SetString(PyExc_ValueError,    e.what()); return;
64                } catch (const std::length_error &e)     { PyErr_SetString(PyExc_ValueError,    e.what()); return;
65                } catch (const std::out_of_range &e)     { PyErr_SetString(PyExc_IndexError,    e.what()); return;
66                } catch (const std::range_error &e)      { PyErr_SetString(PyExc_ValueError,    e.what()); return;
67                } catch (const std::exception &e)        { PyErr_SetString(PyExc_RuntimeError,  e.what()); return;
68                } catch (...) {
69                    PyErr_SetString(PyExc_RuntimeError, "Caught an unknown exception!");
70                    return;
71                }
72            }
73        );
74    }
75    return *internals_ptr;
76}
77
78PYBIND11_NOINLINE inline detail::type_info* get_type_info(PyTypeObject *type) {
79    auto const &type_dict = get_internals().registered_types_py;
80    do {
81        auto it = type_dict.find(type);
82        if (it != type_dict.end())
83            return (detail::type_info *) it->second;
84        type = type->tp_base;
85        if (!type)
86            return nullptr;
87    } while (true);
88}
89
90PYBIND11_NOINLINE inline detail::type_info *get_type_info(const std::type_info &tp,
91                                                          bool throw_if_missing = false) {
92    auto &types = get_internals().registered_types_cpp;
93
94    auto it = types.find(std::type_index(tp));
95    if (it != types.end())
96        return (detail::type_info *) it->second;
97    if (throw_if_missing) {
98        std::string tname = tp.name();
99        detail::clean_type_id(tname);
100        pybind11_fail("pybind11::detail::get_type_info: unable to find type info for \"" + tname + "\"");
101    }
102    return nullptr;
103}
104
105PYBIND11_NOINLINE inline handle get_type_handle(const std::type_info &tp, bool throw_if_missing) {
106    detail::type_info *type_info = get_type_info(tp, throw_if_missing);
107    return handle(type_info ? ((PyObject *) type_info->type) : nullptr);
108}
109
110PYBIND11_NOINLINE inline bool isinstance_generic(handle obj, const std::type_info &tp) {
111    const auto type = detail::get_type_handle(tp, false);
112    if (!type)
113        return false;
114
115    const auto result = PyObject_IsInstance(obj.ptr(), type.ptr());
116    if (result == -1)
117        throw error_already_set();
118    return result != 0;
119}
120
121PYBIND11_NOINLINE inline std::string error_string() {
122    if (!PyErr_Occurred()) {
123        PyErr_SetString(PyExc_RuntimeError, "Unknown internal error occurred");
124        return "Unknown internal error occurred";
125    }
126
127    error_scope scope; // Preserve error state
128
129    std::string errorString;
130    if (scope.type) {
131        errorString += handle(scope.type).attr("__name__").cast<std::string>();
132        errorString += ": ";
133    }
134    if (scope.value)
135        errorString += (std::string) str(scope.value);
136
137    PyErr_NormalizeException(&scope.type, &scope.value, &scope.trace);
138
139#if PY_MAJOR_VERSION >= 3
140    if (scope.trace != nullptr)
141        PyException_SetTraceback(scope.value, scope.trace);
142#endif
143
144    if (scope.trace) {
145        PyTracebackObject *trace = (PyTracebackObject *) scope.trace;
146
147        /* Get the deepest trace possible */
148        while (trace->tb_next)
149            trace = trace->tb_next;
150
151        PyFrameObject *frame = trace->tb_frame;
152        errorString += "\n\nAt:\n";
153        while (frame) {
154            int lineno = PyFrame_GetLineNumber(frame);
155            errorString +=
156                "  " + handle(frame->f_code->co_filename).cast<std::string>() +
157                "(" + std::to_string(lineno) + "): " +
158                handle(frame->f_code->co_name).cast<std::string>() + "\n";
159            frame = frame->f_back;
160        }
161        trace = trace->tb_next;
162    }
163
164    return errorString;
165}
166
167PYBIND11_NOINLINE inline handle get_object_handle(const void *ptr, const detail::type_info *type ) {
168    auto &instances = get_internals().registered_instances;
169    auto range = instances.equal_range(ptr);
170    for (auto it = range.first; it != range.second; ++it) {
171        auto instance_type = detail::get_type_info(Py_TYPE(it->second));
172        if (instance_type && instance_type == type)
173            return handle((PyObject *) it->second);
174    }
175    return handle();
176}
177
178inline PyThreadState *get_thread_state_unchecked() {
179#if   PY_VERSION_HEX < 0x03000000
180    return _PyThreadState_Current;
181#elif PY_VERSION_HEX < 0x03050000
182    return (PyThreadState*) _Py_atomic_load_relaxed(&_PyThreadState_Current);
183#elif PY_VERSION_HEX < 0x03050200
184    return (PyThreadState*) _PyThreadState_Current.value;
185#else
186    return _PyThreadState_UncheckedGet();
187#endif
188}
189
190// Forward declaration
191inline void keep_alive_impl(handle nurse, handle patient);
192
193class type_caster_generic {
194public:
195    PYBIND11_NOINLINE type_caster_generic(const std::type_info &type_info)
196     : typeinfo(get_type_info(type_info)) { }
197
198    PYBIND11_NOINLINE bool load(handle src, bool convert) {
199        if (!src)
200            return false;
201        return load(src, convert, Py_TYPE(src.ptr()));
202    }
203
204    bool load(handle src, bool convert, PyTypeObject *tobj) {
205        if (!src || !typeinfo)
206            return false;
207        if (src.is_none()) {
208            value = nullptr;
209            return true;
210        }
211
212        if (typeinfo->simple_type) { /* Case 1: no multiple inheritance etc. involved */
213            /* Check if we can safely perform a reinterpret-style cast */
214            if (PyType_IsSubtype(tobj, typeinfo->type)) {
215                value = reinterpret_cast<instance<void> *>(src.ptr())->value;
216                return true;
217            }
218        } else { /* Case 2: multiple inheritance */
219            /* Check if we can safely perform a reinterpret-style cast */
220            if (tobj == typeinfo->type) {
221                value = reinterpret_cast<instance<void> *>(src.ptr())->value;
222                return true;
223            }
224
225            /* If this is a python class, also check the parents recursively */
226            auto const &type_dict = get_internals().registered_types_py;
227            bool new_style_class = PyType_Check(tobj);
228            if (type_dict.find(tobj) == type_dict.end() && new_style_class && tobj->tp_bases) {
229                auto parents = reinterpret_borrow<tuple>(tobj->tp_bases);
230                for (handle parent : parents) {
231                    bool result = load(src, convert, (PyTypeObject *) parent.ptr());
232                    if (result)
233                        return true;
234                }
235            }
236
237            /* Try implicit casts */
238            for (auto &cast : typeinfo->implicit_casts) {
239                type_caster_generic sub_caster(*cast.first);
240                if (sub_caster.load(src, convert)) {
241                    value = cast.second(sub_caster.value);
242                    return true;
243                }
244            }
245        }
246
247        /* Perform an implicit conversion */
248        if (convert) {
249            for (auto &converter : typeinfo->implicit_conversions) {
250                temp = reinterpret_steal<object>(converter(src.ptr(), typeinfo->type));
251                if (load(temp, false))
252                    return true;
253            }
254            for (auto &converter : *typeinfo->direct_conversions) {
255                if (converter(src.ptr(), value))
256                    return true;
257            }
258        }
259        return false;
260    }
261
262    PYBIND11_NOINLINE static handle cast(const void *_src, return_value_policy policy, handle parent,
263                                         const std::type_info *type_info,
264                                         const std::type_info *type_info_backup,
265                                         void *(*copy_constructor)(const void *),
266                                         void *(*move_constructor)(const void *),
267                                         const void *existing_holder = nullptr) {
268        void *src = const_cast<void *>(_src);
269        if (src == nullptr)
270            return none().inc_ref();
271
272        auto &internals = get_internals();
273
274        auto it = internals.registered_types_cpp.find(std::type_index(*type_info));
275        if (it == internals.registered_types_cpp.end()) {
276            type_info = type_info_backup;
277            it = internals.registered_types_cpp.find(std::type_index(*type_info));
278        }
279
280        if (it == internals.registered_types_cpp.end()) {
281            std::string tname = type_info->name();
282            detail::clean_type_id(tname);
283            std::string msg = "Unregistered type : " + tname;
284            PyErr_SetString(PyExc_TypeError, msg.c_str());
285            return handle();
286        }
287
288        auto tinfo = (const detail::type_info *) it->second;
289
290        auto it_instances = internals.registered_instances.equal_range(src);
291        for (auto it_i = it_instances.first; it_i != it_instances.second; ++it_i) {
292            auto instance_type = detail::get_type_info(Py_TYPE(it_i->second));
293            if (instance_type && instance_type == tinfo)
294                return handle((PyObject *) it_i->second).inc_ref();
295        }
296
297        auto inst = reinterpret_steal<object>(PyType_GenericAlloc(tinfo->type, 0));
298
299        auto wrapper = (instance<void> *) inst.ptr();
300
301        wrapper->value = nullptr;
302        wrapper->owned = false;
303
304        switch (policy) {
305            case return_value_policy::automatic:
306            case return_value_policy::take_ownership:
307                wrapper->value = src;
308                wrapper->owned = true;
309                break;
310
311            case return_value_policy::automatic_reference:
312            case return_value_policy::reference:
313                wrapper->value = src;
314                wrapper->owned = false;
315                break;
316
317            case return_value_policy::copy:
318                if (copy_constructor)
319                    wrapper->value = copy_constructor(src);
320                else
321                    throw cast_error("return_value_policy = copy, but the "
322                                     "object is non-copyable!");
323                wrapper->owned = true;
324                break;
325
326            case return_value_policy::move:
327                if (move_constructor)
328                    wrapper->value = move_constructor(src);
329                else if (copy_constructor)
330                    wrapper->value = copy_constructor(src);
331                else
332                    throw cast_error("return_value_policy = move, but the "
333                                     "object is neither movable nor copyable!");
334                wrapper->owned = true;
335                break;
336
337            case return_value_policy::reference_internal:
338                wrapper->value = src;
339                wrapper->owned = false;
340                detail::keep_alive_impl(inst, parent);
341                break;
342
343            default:
344                throw cast_error("unhandled return_value_policy: should not happen!");
345        }
346
347        tinfo->init_holder(inst.ptr(), existing_holder);
348
349        internals.registered_instances.emplace(wrapper->value, inst.ptr());
350
351        return inst.release();
352    }
353
354protected:
355    const type_info *typeinfo = nullptr;
356    void *value = nullptr;
357    object temp;
358};
359
360/* Determine suitable casting operator */
361template <typename T>
362using cast_op_type = typename std::conditional<std::is_pointer<typename std::remove_reference<T>::type>::value,
363    typename std::add_pointer<intrinsic_t<T>>::type,
364    typename std::add_lvalue_reference<intrinsic_t<T>>::type>::type;
365
366// std::is_copy_constructible isn't quite enough: it lets std::vector<T> (and similar) through when
367// T is non-copyable, but code containing such a copy constructor fails to actually compile.
368template <typename T, typename SFINAE = void> struct is_copy_constructible : std::is_copy_constructible<T> {};
369
370// Specialization for types that appear to be copy constructible but also look like stl containers
371// (we specifically check for: has `value_type` and `reference` with `reference = value_type&`): if
372// so, copy constructability depends on whether the value_type is copy constructible.
373template <typename Container> struct is_copy_constructible<Container, enable_if_t<
374        std::is_copy_constructible<Container>::value &&
375        std::is_same<typename Container::value_type &, typename Container::reference>::value
376    >> : std::is_copy_constructible<typename Container::value_type> {};
377
378/// Generic type caster for objects stored on the heap
379template <typename type> class type_caster_base : public type_caster_generic {
380    using itype = intrinsic_t<type>;
381public:
382    static PYBIND11_DESCR name() { return type_descr(_<type>()); }
383
384    type_caster_base() : type_caster_base(typeid(type)) { }
385    explicit type_caster_base(const std::type_info &info) : type_caster_generic(info) { }
386
387    static handle cast(const itype &src, return_value_policy policy, handle parent) {
388        if (policy == return_value_policy::automatic || policy == return_value_policy::automatic_reference)
389            policy = return_value_policy::copy;
390        return cast(&src, policy, parent);
391    }
392
393    static handle cast(itype &&src, return_value_policy, handle parent) {
394        return cast(&src, return_value_policy::move, parent);
395    }
396
397    static handle cast(const itype *src, return_value_policy policy, handle parent) {
398        return type_caster_generic::cast(
399            src, policy, parent, src ? &typeid(*src) : nullptr, &typeid(type),
400            make_copy_constructor(src), make_move_constructor(src));
401    }
402
403    template <typename T> using cast_op_type = pybind11::detail::cast_op_type<T>;
404
405    operator itype*() { return (type *) value; }
406    operator itype&() { if (!value) throw reference_cast_error(); return *((itype *) value); }
407
408protected:
409    typedef void *(*Constructor)(const void *stream);
410#if !defined(_MSC_VER)
411    /* Only enabled when the types are {copy,move}-constructible *and* when the type
412       does not have a private operator new implementaton. */
413    template <typename T = type, typename = enable_if_t<is_copy_constructible<T>::value>> static auto make_copy_constructor(const T *value) -> decltype(new T(*value), Constructor(nullptr)) {
414        return [](const void *arg) -> void * { return new T(*((const T *) arg)); }; }
415    template <typename T = type> static auto make_move_constructor(const T *value) -> decltype(new T(std::move(*((T *) value))), Constructor(nullptr)) {
416        return [](const void *arg) -> void * { return (void *) new T(std::move(*((T *) arg))); }; }
417#else
418    /* Visual Studio 2015's SFINAE implementation doesn't yet handle the above robustly in all situations.
419       Use a workaround that only tests for constructibility for now. */
420    template <typename T = type, typename = enable_if_t<is_copy_constructible<T>::value>>
421    static Constructor make_copy_constructor(const T *value) {
422        return [](const void *arg) -> void * { return new T(*((const T *)arg)); }; }
423    template <typename T = type, typename = enable_if_t<std::is_move_constructible<T>::value>>
424    static Constructor make_move_constructor(const T *value) {
425        return [](const void *arg) -> void * { return (void *) new T(std::move(*((T *)arg))); }; }
426#endif
427
428    static Constructor make_copy_constructor(...) { return nullptr; }
429    static Constructor make_move_constructor(...) { return nullptr; }
430};
431
432template <typename type, typename SFINAE = void> class type_caster : public type_caster_base<type> { };
433template <typename type> using make_caster = type_caster<intrinsic_t<type>>;
434
435// Shortcut for calling a caster's `cast_op_type` cast operator for casting a type_caster to a T
436template <typename T> typename make_caster<T>::template cast_op_type<T> cast_op(make_caster<T> &caster) {
437    return caster.operator typename make_caster<T>::template cast_op_type<T>();
438}
439template <typename T> typename make_caster<T>::template cast_op_type<T> cast_op(make_caster<T> &&caster) {
440    return cast_op<T>(caster);
441}
442
443template <typename type> class type_caster<std::reference_wrapper<type>> : public type_caster_base<type> {
444public:
445    static handle cast(const std::reference_wrapper<type> &src, return_value_policy policy, handle parent) {
446        return type_caster_base<type>::cast(&src.get(), policy, parent);
447    }
448    template <typename T> using cast_op_type = std::reference_wrapper<type>;
449    operator std::reference_wrapper<type>() { return std::ref(*((type *) this->value)); }
450};
451
452#define PYBIND11_TYPE_CASTER(type, py_name) \
453    protected: \
454        type value; \
455    public: \
456        static PYBIND11_DESCR name() { return type_descr(py_name); } \
457        static handle cast(const type *src, return_value_policy policy, handle parent) { \
458            return cast(*src, policy, parent); \
459        } \
460        operator type*() { return &value; } \
461        operator type&() { return value; } \
462        template <typename _T> using cast_op_type = pybind11::detail::cast_op_type<_T>
463
464
465template <typename T>
466struct type_caster<T, enable_if_t<std::is_arithmetic<T>::value>> {
467    typedef typename std::conditional<sizeof(T) <= sizeof(long), long, long long>::type _py_type_0;
468    typedef typename std::conditional<std::is_signed<T>::value, _py_type_0, typename std::make_unsigned<_py_type_0>::type>::type _py_type_1;
469    typedef typename std::conditional<std::is_floating_point<T>::value, double, _py_type_1>::type py_type;
470public:
471
472    bool load(handle src, bool) {
473        py_type py_value;
474
475        if (!src) {
476            return false;
477        } if (std::is_floating_point<T>::value) {
478            py_value = (py_type) PyFloat_AsDouble(src.ptr());
479        } else if (sizeof(T) <= sizeof(long)) {
480            if (PyFloat_Check(src.ptr()))
481                return false;
482            if (std::is_signed<T>::value)
483                py_value = (py_type) PyLong_AsLong(src.ptr());
484            else
485                py_value = (py_type) PyLong_AsUnsignedLong(src.ptr());
486        } else {
487            if (PyFloat_Check(src.ptr()))
488                return false;
489            if (std::is_signed<T>::value)
490                py_value = (py_type) PYBIND11_LONG_AS_LONGLONG(src.ptr());
491            else
492                py_value = (py_type) PYBIND11_LONG_AS_UNSIGNED_LONGLONG(src.ptr());
493        }
494
495        if ((py_value == (py_type) -1 && PyErr_Occurred()) ||
496            (std::is_integral<T>::value && sizeof(py_type) != sizeof(T) &&
497               (py_value < (py_type) std::numeric_limits<T>::min() ||
498                py_value > (py_type) std::numeric_limits<T>::max()))) {
499#if PY_VERSION_HEX < 0x03000000
500            bool type_error = PyErr_ExceptionMatches(PyExc_SystemError);
501#else
502            bool type_error = PyErr_ExceptionMatches(PyExc_TypeError);
503#endif
504            PyErr_Clear();
505            if (type_error && PyNumber_Check(src.ptr())) {
506                auto tmp = reinterpret_borrow<object>(std::is_floating_point<T>::value
507                                                      ? PyNumber_Float(src.ptr())
508                                                      : PyNumber_Long(src.ptr()));
509                PyErr_Clear();
510                return load(tmp, false);
511            }
512            return false;
513        }
514
515        value = (T) py_value;
516        return true;
517    }
518
519    static handle cast(T src, return_value_policy /* policy */, handle /* parent */) {
520        if (std::is_floating_point<T>::value) {
521            return PyFloat_FromDouble((double) src);
522        } else if (sizeof(T) <= sizeof(long)) {
523            if (std::is_signed<T>::value)
524                return PyLong_FromLong((long) src);
525            else
526                return PyLong_FromUnsignedLong((unsigned long) src);
527        } else {
528            if (std::is_signed<T>::value)
529                return PyLong_FromLongLong((long long) src);
530            else
531                return PyLong_FromUnsignedLongLong((unsigned long long) src);
532        }
533    }
534
535    PYBIND11_TYPE_CASTER(T, _<std::is_integral<T>::value>("int", "float"));
536};
537
538template<typename T> struct void_caster {
539public:
540    bool load(handle, bool) { return false; }
541    static handle cast(T, return_value_policy /* policy */, handle /* parent */) {
542        return none().inc_ref();
543    }
544    PYBIND11_TYPE_CASTER(T, _("None"));
545};
546
547template <> class type_caster<void_type> : public void_caster<void_type> {};
548
549template <> class type_caster<void> : public type_caster<void_type> {
550public:
551    using type_caster<void_type>::cast;
552
553    bool load(handle h, bool) {
554        if (!h) {
555            return false;
556        } else if (h.is_none()) {
557            value = nullptr;
558            return true;
559        }
560
561        /* Check if this is a capsule */
562        if (isinstance<capsule>(h)) {
563            value = reinterpret_borrow<capsule>(h);
564            return true;
565        }
566
567        /* Check if this is a C++ type */
568        if (get_type_info((PyTypeObject *) h.get_type().ptr())) {
569            value = ((instance<void> *) h.ptr())->value;
570            return true;
571        }
572
573        /* Fail */
574        return false;
575    }
576
577    static handle cast(const void *ptr, return_value_policy /* policy */, handle /* parent */) {
578        if (ptr)
579            return capsule(ptr).release();
580        else
581            return none().inc_ref();
582    }
583
584    template <typename T> using cast_op_type = void*&;
585    operator void *&() { return value; }
586    static PYBIND11_DESCR name() { return type_descr(_("capsule")); }
587private:
588    void *value = nullptr;
589};
590
591template <> class type_caster<std::nullptr_t> : public type_caster<void_type> { };
592
593template <> class type_caster<bool> {
594public:
595    bool load(handle src, bool) {
596        if (!src) return false;
597        else if (src.ptr() == Py_True) { value = true; return true; }
598        else if (src.ptr() == Py_False) { value = false; return true; }
599        else return false;
600    }
601    static handle cast(bool src, return_value_policy /* policy */, handle /* parent */) {
602        return handle(src ? Py_True : Py_False).inc_ref();
603    }
604    PYBIND11_TYPE_CASTER(bool, _("bool"));
605};
606
607template <> class type_caster<std::string> {
608public:
609    bool load(handle src, bool) {
610        object temp;
611        handle load_src = src;
612        if (!src) {
613            return false;
614        } else if (PyUnicode_Check(load_src.ptr())) {
615            temp = reinterpret_steal<object>(PyUnicode_AsUTF8String(load_src.ptr()));
616            if (!temp) { PyErr_Clear(); return false; }  // UnicodeEncodeError
617            load_src = temp;
618        }
619        char *buffer;
620        ssize_t length;
621        int err = PYBIND11_BYTES_AS_STRING_AND_SIZE(load_src.ptr(), &buffer, &length);
622        if (err == -1) { PyErr_Clear(); return false; }  // TypeError
623        value = std::string(buffer, (size_t) length);
624        success = true;
625        return true;
626    }
627
628    static handle cast(const std::string &src, return_value_policy /* policy */, handle /* parent */) {
629        return PyUnicode_FromStringAndSize(src.c_str(), (ssize_t) src.length());
630    }
631
632    PYBIND11_TYPE_CASTER(std::string, _(PYBIND11_STRING_NAME));
633protected:
634    bool success = false;
635};
636
637template <typename type, typename deleter> class type_caster<std::unique_ptr<type, deleter>> {
638public:
639    static handle cast(std::unique_ptr<type, deleter> &&src, return_value_policy policy, handle parent) {
640        handle result = type_caster_base<type>::cast(src.get(), policy, parent);
641        if (result)
642            src.release();
643        return result;
644    }
645    static PYBIND11_DESCR name() { return type_caster_base<type>::name(); }
646};
647
648template <> class type_caster<std::wstring> {
649public:
650    bool load(handle src, bool) {
651        object temp;
652        handle load_src = src;
653        if (!src) {
654            return false;
655        } else if (!PyUnicode_Check(load_src.ptr())) {
656            temp = reinterpret_steal<object>(PyUnicode_FromObject(load_src.ptr()));
657            if (!temp) { PyErr_Clear(); return false; }
658            load_src = temp;
659        }
660        wchar_t *buffer = nullptr;
661        ssize_t length = -1;
662#if PY_MAJOR_VERSION >= 3
663        buffer = PyUnicode_AsWideCharString(load_src.ptr(), &length);
664#else
665        temp = reinterpret_steal<object>(
666            sizeof(wchar_t) == sizeof(short)
667                ? PyUnicode_AsUTF16String(load_src.ptr())
668                : PyUnicode_AsUTF32String(load_src.ptr()));
669        if (temp) {
670            int err = PYBIND11_BYTES_AS_STRING_AND_SIZE(temp.ptr(), (char **) &buffer, &length);
671            if (err == -1) { buffer = nullptr; }  // TypeError
672            length = length / (ssize_t) sizeof(wchar_t) - 1; ++buffer; // Skip BOM
673        }
674#endif
675        if (!buffer) { PyErr_Clear(); return false; }
676        value = std::wstring(buffer, (size_t) length);
677        success = true;
678        return true;
679    }
680
681    static handle cast(const std::wstring &src, return_value_policy /* policy */, handle /* parent */) {
682        return PyUnicode_FromWideChar(src.c_str(), (ssize_t) src.length());
683    }
684
685    PYBIND11_TYPE_CASTER(std::wstring, _(PYBIND11_STRING_NAME));
686protected:
687    bool success = false;
688};
689
690template <> class type_caster<char> : public type_caster<std::string> {
691public:
692    bool load(handle src, bool convert) {
693        if (src.is_none()) return true;
694        return type_caster<std::string>::load(src, convert);
695    }
696
697    static handle cast(const char *src, return_value_policy /* policy */, handle /* parent */) {
698        if (src == nullptr) return none().inc_ref();
699        return PyUnicode_FromString(src);
700    }
701
702    static handle cast(char src, return_value_policy /* policy */, handle /* parent */) {
703        char str[2] = { src, '\0' };
704        return PyUnicode_DecodeLatin1(str, 1, nullptr);
705    }
706
707    operator char*() { return success ? (char *) value.c_str() : nullptr; }
708    operator char&() { return value[0]; }
709
710    static PYBIND11_DESCR name() { return type_descr(_(PYBIND11_STRING_NAME)); }
711};
712
713template <> class type_caster<wchar_t> : public type_caster<std::wstring> {
714public:
715    bool load(handle src, bool convert) {
716        if (src.is_none()) return true;
717        return type_caster<std::wstring>::load(src, convert);
718    }
719
720    static handle cast(const wchar_t *src, return_value_policy /* policy */, handle /* parent */) {
721        if (src == nullptr) return none().inc_ref();
722        return PyUnicode_FromWideChar(src, (ssize_t) wcslen(src));
723    }
724
725    static handle cast(wchar_t src, return_value_policy /* policy */, handle /* parent */) {
726        wchar_t wstr[2] = { src, L'\0' };
727        return PyUnicode_FromWideChar(wstr, 1);
728    }
729
730    operator wchar_t*() { return success ? (wchar_t *) value.c_str() : nullptr; }
731    operator wchar_t&() { return value[0]; }
732
733    static PYBIND11_DESCR name() { return type_descr(_(PYBIND11_STRING_NAME)); }
734};
735
736template <typename T1, typename T2> class type_caster<std::pair<T1, T2>> {
737    typedef std::pair<T1, T2> type;
738public:
739    bool load(handle src, bool convert) {
740        if (!isinstance<sequence>(src))
741            return false;
742        const auto seq = reinterpret_borrow<sequence>(src);
743        if (seq.size() != 2)
744            return false;
745        return first.load(seq[0], convert) && second.load(seq[1], convert);
746    }
747
748    static handle cast(const type &src, return_value_policy policy, handle parent) {
749        auto o1 = reinterpret_steal<object>(make_caster<T1>::cast(src.first, policy, parent));
750        auto o2 = reinterpret_steal<object>(make_caster<T2>::cast(src.second, policy, parent));
751        if (!o1 || !o2)
752            return handle();
753        tuple result(2);
754        PyTuple_SET_ITEM(result.ptr(), 0, o1.release().ptr());
755        PyTuple_SET_ITEM(result.ptr(), 1, o2.release().ptr());
756        return result.release();
757    }
758
759    static PYBIND11_DESCR name() {
760        return type_descr(
761            _("Tuple[") + make_caster<T1>::name() + _(", ") + make_caster<T2>::name() + _("]")
762        );
763    }
764
765    template <typename T> using cast_op_type = type;
766
767    operator type() {
768        return type(cast_op<T1>(first), cast_op<T2>(second));
769    }
770protected:
771    make_caster<T1> first;
772    make_caster<T2> second;
773};
774
775template <typename... Tuple> class type_caster<std::tuple<Tuple...>> {
776    using type = std::tuple<Tuple...>;
777    using indices = make_index_sequence<sizeof...(Tuple)>;
778    static constexpr auto size = sizeof...(Tuple);
779
780public:
781    bool load(handle src, bool convert) {
782        if (!isinstance<sequence>(src))
783            return false;
784        const auto seq = reinterpret_borrow<sequence>(src);
785        if (seq.size() != size)
786            return false;
787        return load_impl(seq, convert, indices{});
788    }
789
790    static handle cast(const type &src, return_value_policy policy, handle parent) {
791        return cast_impl(src, policy, parent, indices{});
792    }
793
794    static PYBIND11_DESCR name() {
795        return type_descr(_("Tuple[") + detail::concat(make_caster<Tuple>::name()...) + _("]"));
796    }
797
798    template <typename T> using cast_op_type = type;
799
800    operator type() { return implicit_cast(indices{}); }
801
802protected:
803    template <size_t... Is>
804    type implicit_cast(index_sequence<Is...>) { return type(cast_op<Tuple>(std::get<Is>(value))...); }
805
806    static constexpr bool load_impl(const sequence &, bool, index_sequence<>) { return true; }
807
808    template <size_t... Is>
809    bool load_impl(const sequence &seq, bool convert, index_sequence<Is...>) {
810        for (bool r : {std::get<Is>(value).load(seq[Is], convert)...})
811            if (!r)
812                return false;
813        return true;
814    }
815
816    static handle cast_impl(const type &, return_value_policy, handle,
817                            index_sequence<>) { return tuple().release(); }
818
819    /* Implementation: Convert a C++ tuple into a Python tuple */
820    template <size_t... Is>
821    static handle cast_impl(const type &src, return_value_policy policy, handle parent, index_sequence<Is...>) {
822        std::array<object, size> entries {{
823            reinterpret_steal<object>(make_caster<Tuple>::cast(std::get<Is>(src), policy, parent))...
824        }};
825        for (const auto &entry: entries)
826            if (!entry)
827                return handle();
828        tuple result(size);
829        int counter = 0;
830        for (auto & entry: entries)
831            PyTuple_SET_ITEM(result.ptr(), counter++, entry.release().ptr());
832        return result.release();
833    }
834
835protected:
836    std::tuple<make_caster<Tuple>...> value;
837};
838
839/// Type caster for holder types like std::shared_ptr, etc.
840template <typename type, typename holder_type> class type_caster_holder : public type_caster_base<type> {
841public:
842    using base = type_caster_base<type>;
843    using base::base;
844    using base::cast;
845    using base::typeinfo;
846    using base::value;
847    using base::temp;
848
849    PYBIND11_NOINLINE bool load(handle src, bool convert) {
850        return load(src, convert, Py_TYPE(src.ptr()));
851    }
852
853    bool load(handle src, bool convert, PyTypeObject *tobj) {
854        if (!src || !typeinfo)
855            return false;
856        if (src.is_none()) {
857            value = nullptr;
858            return true;
859        }
860
861        if (typeinfo->simple_type) { /* Case 1: no multiple inheritance etc. involved */
862            /* Check if we can safely perform a reinterpret-style cast */
863            if (PyType_IsSubtype(tobj, typeinfo->type))
864                return load_value_and_holder(src);
865        } else { /* Case 2: multiple inheritance */
866            /* Check if we can safely perform a reinterpret-style cast */
867            if (tobj == typeinfo->type)
868                return load_value_and_holder(src);
869
870            /* If this is a python class, also check the parents recursively */
871            auto const &type_dict = get_internals().registered_types_py;
872            bool new_style_class = PyType_Check(tobj);
873            if (type_dict.find(tobj) == type_dict.end() && new_style_class && tobj->tp_bases) {
874                auto parents = reinterpret_borrow<tuple>(tobj->tp_bases);
875                for (handle parent : parents) {
876                    bool result = load(src, convert, (PyTypeObject *) parent.ptr());
877                    if (result)
878                        return true;
879                }
880            }
881
882            if (try_implicit_casts(src, convert))
883                return true;
884        }
885
886        if (convert) {
887            for (auto &converter : typeinfo->implicit_conversions) {
888                temp = reinterpret_steal<object>(converter(src.ptr(), typeinfo->type));
889                if (load(temp, false))
890                    return true;
891            }
892        }
893
894        return false;
895    }
896
897    bool load_value_and_holder(handle src) {
898        auto inst = (instance<type, holder_type> *) src.ptr();
899        value = (void *) inst->value;
900        if (inst->holder_constructed) {
901            holder = inst->holder;
902            return true;
903        } else {
904            throw cast_error("Unable to cast from non-held to held instance (T& to Holder<T>) "
905#if defined(NDEBUG)
906                             "(compile in debug mode for type information)");
907#else
908                             "of type '" + type_id<holder_type>() + "''");
909#endif
910        }
911    }
912
913    template <typename T = holder_type, detail::enable_if_t<!std::is_constructible<T, const T &, type*>::value, int> = 0>
914    bool try_implicit_casts(handle, bool) { return false; }
915
916    template <typename T = holder_type, detail::enable_if_t<std::is_constructible<T, const T &, type*>::value, int> = 0>
917    bool try_implicit_casts(handle src, bool convert) {
918        for (auto &cast : typeinfo->implicit_casts) {
919            type_caster_holder sub_caster(*cast.first);
920            if (sub_caster.load(src, convert)) {
921                value = cast.second(sub_caster.value);
922                holder = holder_type(sub_caster.holder, (type *) value);
923                return true;
924            }
925        }
926        return false;
927    }
928
929    explicit operator type*() { return this->value; }
930    explicit operator type&() { return *(this->value); }
931    explicit operator holder_type*() { return &holder; }
932
933    // Workaround for Intel compiler bug
934    // see pybind11 issue 94
935    #if defined(__ICC) || defined(__INTEL_COMPILER)
936    operator holder_type&() { return holder; }
937    #else
938    explicit operator holder_type&() { return holder; }
939    #endif
940
941    static handle cast(const holder_type &src, return_value_policy, handle) {
942        return type_caster_generic::cast(
943            src.get(), return_value_policy::take_ownership, handle(),
944            src.get() ? &typeid(*src.get()) : nullptr, &typeid(type),
945            nullptr, nullptr, &src);
946    }
947
948protected:
949    holder_type holder;
950};
951
952/// Specialize for the common std::shared_ptr, so users don't need to
953template <typename T>
954class type_caster<std::shared_ptr<T>> : public type_caster_holder<T, std::shared_ptr<T>> { };
955
956/// Create a specialization for custom holder types (silently ignores std::shared_ptr)
957#define PYBIND11_DECLARE_HOLDER_TYPE(type, holder_type) \
958    namespace pybind11 { namespace detail { \
959    template <typename type> \
960    class type_caster<holder_type, enable_if_t<!is_shared_ptr<holder_type>::value>> \
961        : public type_caster_holder<type, holder_type> { }; \
962    }}
963
964// PYBIND11_DECLARE_HOLDER_TYPE holder types:
965template <typename base, typename holder> struct is_holder_type :
966    std::is_base_of<detail::type_caster_holder<base, holder>, detail::type_caster<holder>> {};
967// Specialization for always-supported unique_ptr holders:
968template <typename base, typename deleter> struct is_holder_type<base, std::unique_ptr<base, deleter>> :
969    std::true_type {};
970
971template <typename T> struct handle_type_name { static PYBIND11_DESCR name() { return _<T>(); } };
972template <> struct handle_type_name<bytes> { static PYBIND11_DESCR name() { return _(PYBIND11_BYTES_NAME); } };
973template <> struct handle_type_name<args> { static PYBIND11_DESCR name() { return _("*args"); } };
974template <> struct handle_type_name<kwargs> { static PYBIND11_DESCR name() { return _("**kwargs"); } };
975
976template <typename type>
977struct pyobject_caster {
978    template <typename T = type, enable_if_t<std::is_same<T, handle>::value, int> = 0>
979    bool load(handle src, bool /* convert */) { value = src; return static_cast<bool>(value); }
980
981    template <typename T = type, enable_if_t<std::is_base_of<object, T>::value, int> = 0>
982    bool load(handle src, bool /* convert */) {
983        if (!isinstance<type>(src))
984            return false;
985        value = reinterpret_borrow<type>(src);
986        return true;
987    }
988
989    static handle cast(const handle &src, return_value_policy /* policy */, handle /* parent */) {
990        return src.inc_ref();
991    }
992    PYBIND11_TYPE_CASTER(type, handle_type_name<type>::name());
993};
994
995template <typename T>
996class type_caster<T, enable_if_t<is_pyobject<T>::value>> : public pyobject_caster<T> { };
997
998// Our conditions for enabling moving are quite restrictive:
999// At compile time:
1000// - T needs to be a non-const, non-pointer, non-reference type
1001// - type_caster<T>::operator T&() must exist
1002// - the type must be move constructible (obviously)
1003// At run-time:
1004// - if the type is non-copy-constructible, the object must be the sole owner of the type (i.e. it
1005//   must have ref_count() == 1)h
1006// If any of the above are not satisfied, we fall back to copying.
1007template <typename T, typename SFINAE = void> struct move_is_plain_type : std::false_type {};
1008template <typename T> struct move_is_plain_type<T, enable_if_t<
1009        !std::is_void<T>::value && !std::is_pointer<T>::value && !std::is_reference<T>::value && !std::is_const<T>::value
1010    >> : std::true_type { };
1011template <typename T, typename SFINAE = void> struct move_always : std::false_type {};
1012template <typename T> struct move_always<T, enable_if_t<
1013        move_is_plain_type<T>::value &&
1014        !std::is_copy_constructible<T>::value && std::is_move_constructible<T>::value &&
1015        std::is_same<decltype(std::declval<type_caster<T>>().operator T&()), T&>::value
1016    >> : std::true_type { };
1017template <typename T, typename SFINAE = void> struct move_if_unreferenced : std::false_type {};
1018template <typename T> struct move_if_unreferenced<T, enable_if_t<
1019        move_is_plain_type<T>::value &&
1020        !move_always<T>::value && std::is_move_constructible<T>::value &&
1021        std::is_same<decltype(std::declval<type_caster<T>>().operator T&()), T&>::value
1022    >> : std::true_type { };
1023template <typename T> using move_never = std::integral_constant<bool, !move_always<T>::value && !move_if_unreferenced<T>::value>;
1024
1025// Detect whether returning a `type` from a cast on type's type_caster is going to result in a
1026// reference or pointer to a local variable of the type_caster.  Basically, only
1027// non-reference/pointer `type`s and reference/pointers from a type_caster_generic are safe;
1028// everything else returns a reference/pointer to a local variable.
1029template <typename type> using cast_is_temporary_value_reference = bool_constant<
1030    (std::is_reference<type>::value || std::is_pointer<type>::value) &&
1031    !std::is_base_of<type_caster_generic, make_caster<type>>::value
1032>;
1033
1034// Basic python -> C++ casting; throws if casting fails
1035template <typename T, typename SFINAE> type_caster<T, SFINAE> &load_type(type_caster<T, SFINAE> &conv, const handle &handle) {
1036    if (!conv.load(handle, true)) {
1037#if defined(NDEBUG)
1038        throw cast_error("Unable to cast Python instance to C++ type (compile in debug mode for details)");
1039#else
1040        throw cast_error("Unable to cast Python instance of type " +
1041            (std::string) str(handle.get_type()) + " to C++ type '" + type_id<T>() + "''");
1042#endif
1043    }
1044    return conv;
1045}
1046// Wrapper around the above that also constructs and returns a type_caster
1047template <typename T> make_caster<T> load_type(const handle &handle) {
1048    make_caster<T> conv;
1049    load_type(conv, handle);
1050    return conv;
1051}
1052
1053NAMESPACE_END(detail)
1054
1055// pytype -> C++ type
1056template <typename T, detail::enable_if_t<!detail::is_pyobject<T>::value, int> = 0>
1057T cast(const handle &handle) {
1058    using namespace detail;
1059    static_assert(!cast_is_temporary_value_reference<T>::value,
1060            "Unable to cast type to reference: value is local to type caster");
1061    return cast_op<T>(load_type<T>(handle));
1062}
1063
1064// pytype -> pytype (calls converting constructor)
1065template <typename T, detail::enable_if_t<detail::is_pyobject<T>::value, int> = 0>
1066T cast(const handle &handle) { return T(reinterpret_borrow<object>(handle)); }
1067
1068// C++ type -> py::object
1069template <typename T, detail::enable_if_t<!detail::is_pyobject<T>::value, int> = 0>
1070object cast(const T &value, return_value_policy policy = return_value_policy::automatic_reference,
1071            handle parent = handle()) {
1072    if (policy == return_value_policy::automatic)
1073        policy = std::is_pointer<T>::value ? return_value_policy::take_ownership : return_value_policy::copy;
1074    else if (policy == return_value_policy::automatic_reference)
1075        policy = std::is_pointer<T>::value ? return_value_policy::reference : return_value_policy::copy;
1076    return reinterpret_steal<object>(detail::make_caster<T>::cast(value, policy, parent));
1077}
1078
1079template <typename T> T handle::cast() const { return pybind11::cast<T>(*this); }
1080template <> inline void handle::cast() const { return; }
1081
1082template <typename T>
1083detail::enable_if_t<detail::move_always<T>::value || detail::move_if_unreferenced<T>::value, T> move(object &&obj) {
1084    if (obj.ref_count() > 1)
1085#if defined(NDEBUG)
1086        throw cast_error("Unable to cast Python instance to C++ rvalue: instance has multiple references"
1087            " (compile in debug mode for details)");
1088#else
1089        throw cast_error("Unable to move from Python " + (std::string) str(obj.get_type()) +
1090                " instance to C++ " + type_id<T>() + " instance: instance has multiple references");
1091#endif
1092
1093    // Move into a temporary and return that, because the reference may be a local value of `conv`
1094    T ret = std::move(detail::load_type<T>(obj).operator T&());
1095    return ret;
1096}
1097
1098// Calling cast() on an rvalue calls pybind::cast with the object rvalue, which does:
1099// - If we have to move (because T has no copy constructor), do it.  This will fail if the moved
1100//   object has multiple references, but trying to copy will fail to compile.
1101// - If both movable and copyable, check ref count: if 1, move; otherwise copy
1102// - Otherwise (not movable), copy.
1103template <typename T> detail::enable_if_t<detail::move_always<T>::value, T> cast(object &&object) {
1104    return move<T>(std::move(object));
1105}
1106template <typename T> detail::enable_if_t<detail::move_if_unreferenced<T>::value, T> cast(object &&object) {
1107    if (object.ref_count() > 1)
1108        return cast<T>(object);
1109    else
1110        return move<T>(std::move(object));
1111}
1112template <typename T> detail::enable_if_t<detail::move_never<T>::value, T> cast(object &&object) {
1113    return cast<T>(object);
1114}
1115
1116template <typename T> T object::cast() const & { return pybind11::cast<T>(*this); }
1117template <typename T> T object::cast() && { return pybind11::cast<T>(std::move(*this)); }
1118template <> inline void object::cast() const & { return; }
1119template <> inline void object::cast() && { return; }
1120
1121NAMESPACE_BEGIN(detail)
1122
1123// Declared in pytypes.h:
1124template <typename T, enable_if_t<!is_pyobject<T>::value, int>>
1125object object_or_cast(T &&o) { return pybind11::cast(std::forward<T>(o)); }
1126
1127struct overload_unused {}; // Placeholder type for the unneeded (and dead code) static variable in the OVERLOAD_INT macro
1128template <typename ret_type> using overload_caster_t = conditional_t<
1129    cast_is_temporary_value_reference<ret_type>::value, make_caster<ret_type>, overload_unused>;
1130
1131// Trampoline use: for reference/pointer types to value-converted values, we do a value cast, then
1132// store the result in the given variable.  For other types, this is a no-op.
1133template <typename T> enable_if_t<cast_is_temporary_value_reference<T>::value, T> cast_ref(object &&o, make_caster<T> &caster) {
1134    return cast_op<T>(load_type(caster, o));
1135}
1136template <typename T> enable_if_t<!cast_is_temporary_value_reference<T>::value, T> cast_ref(object &&, overload_unused &) {
1137    pybind11_fail("Internal error: cast_ref fallback invoked"); }
1138
1139// Trampoline use: Having a pybind11::cast with an invalid reference type is going to static_assert, even
1140// though if it's in dead code, so we provide a "trampoline" to pybind11::cast that only does anything in
1141// cases where pybind11::cast is valid.
1142template <typename T> enable_if_t<!cast_is_temporary_value_reference<T>::value, T> cast_safe(object &&o) {
1143    return pybind11::cast<T>(std::move(o)); }
1144template <typename T> enable_if_t<cast_is_temporary_value_reference<T>::value, T> cast_safe(object &&) {
1145    pybind11_fail("Internal error: cast_safe fallback invoked"); }
1146template <> inline void cast_safe<void>(object &&) {}
1147
1148NAMESPACE_END(detail)
1149
1150template <return_value_policy policy = return_value_policy::automatic_reference,
1151          typename... Args> tuple make_tuple(Args&&... args_) {
1152    const size_t size = sizeof...(Args);
1153    std::array<object, size> args {
1154        { reinterpret_steal<object>(detail::make_caster<Args>::cast(
1155            std::forward<Args>(args_), policy, nullptr))... }
1156    };
1157    for (auto &arg_value : args) {
1158        if (!arg_value) {
1159#if defined(NDEBUG)
1160            throw cast_error("make_tuple(): unable to convert arguments to Python object (compile in debug mode for details)");
1161#else
1162            throw cast_error("make_tuple(): unable to convert arguments of types '" +
1163                (std::string) type_id<std::tuple<Args...>>() + "' to Python object");
1164#endif
1165        }
1166    }
1167    tuple result(size);
1168    int counter = 0;
1169    for (auto &arg_value : args)
1170        PyTuple_SET_ITEM(result.ptr(), counter++, arg_value.release().ptr());
1171    return result;
1172}
1173
1174/// Annotation for keyword arguments
1175struct arg {
1176    constexpr explicit arg(const char *name) : name(name) { }
1177    template <typename T> arg_v operator=(T &&value) const;
1178
1179    const char *name;
1180};
1181
1182/// Annotation for keyword arguments with values
1183struct arg_v : arg {
1184    template <typename T>
1185    arg_v(const char *name, T &&x, const char *descr = nullptr)
1186        : arg(name),
1187          value(reinterpret_steal<object>(
1188              detail::make_caster<T>::cast(x, return_value_policy::automatic, {})
1189          )),
1190          descr(descr)
1191#if !defined(NDEBUG)
1192        , type(type_id<T>())
1193#endif
1194    { }
1195
1196    object value;
1197    const char *descr;
1198#if !defined(NDEBUG)
1199    std::string type;
1200#endif
1201};
1202
1203template <typename T>
1204arg_v arg::operator=(T &&value) const { return {name, std::forward<T>(value)}; }
1205
1206/// Alias for backward compatibility -- to be removed in version 2.0
1207template <typename /*unused*/> using arg_t = arg_v;
1208
1209inline namespace literals {
1210/// String literal version of arg
1211constexpr arg operator"" _a(const char *name, size_t) { return arg(name); }
1212}
1213
1214NAMESPACE_BEGIN(detail)
1215
1216/// Helper class which loads arguments for C++ functions called from Python
1217template <typename... Args>
1218class argument_loader {
1219    using itypes = type_list<intrinsic_t<Args>...>;
1220    using indices = make_index_sequence<sizeof...(Args)>;
1221
1222public:
1223    static constexpr auto has_kwargs = std::is_same<itypes, type_list<args, kwargs>>::value;
1224    static constexpr auto has_args = has_kwargs || std::is_same<itypes, type_list<args>>::value;
1225
1226    static PYBIND11_DESCR arg_names() { return detail::concat(make_caster<Args>::name()...); }
1227
1228    bool load_args(handle args, handle kwargs, bool convert) {
1229        return load_impl(args, kwargs, convert, itypes{});
1230    }
1231
1232    template <typename Return, typename Func>
1233    enable_if_t<!std::is_void<Return>::value, Return> call(Func &&f) {
1234        return call_impl<Return>(std::forward<Func>(f), indices{});
1235    }
1236
1237    template <typename Return, typename Func>
1238    enable_if_t<std::is_void<Return>::value, void_type> call(Func &&f) {
1239        call_impl<Return>(std::forward<Func>(f), indices{});
1240        return void_type();
1241    }
1242
1243private:
1244    bool load_impl(handle args_, handle, bool convert, type_list<args>) {
1245        std::get<0>(value).load(args_, convert);
1246        return true;
1247    }
1248
1249    bool load_impl(handle args_, handle kwargs_, bool convert, type_list<args, kwargs>) {
1250        std::get<0>(value).load(args_, convert);
1251        std::get<1>(value).load(kwargs_, convert);
1252        return true;
1253    }
1254
1255    bool load_impl(handle args, handle, bool convert, ... /* anything else */) {
1256        return load_impl_sequence(args, convert, indices{});
1257    }
1258
1259    static constexpr bool load_impl_sequence(handle, bool, index_sequence<>) { return true; }
1260
1261    template <size_t... Is>
1262    bool load_impl_sequence(handle src, bool convert, index_sequence<Is...>) {
1263        for (bool r : {std::get<Is>(value).load(PyTuple_GET_ITEM(src.ptr(), Is), convert)...})
1264            if (!r)
1265                return false;
1266        return true;
1267    }
1268
1269    template <typename Return, typename Func, size_t... Is>
1270    Return call_impl(Func &&f, index_sequence<Is...>) {
1271        return std::forward<Func>(f)(cast_op<Args>(std::get<Is>(value))...);
1272    }
1273
1274private:
1275    std::tuple<make_caster<Args>...> value;
1276};
1277
1278NAMESPACE_BEGIN(constexpr_impl)
1279/// Implementation details for constexpr functions
1280constexpr int first(int i) { return i; }
1281template <typename T, typename... Ts>
1282constexpr int first(int i, T v, Ts... vs) { return v ? i : first(i + 1, vs...); }
1283
1284constexpr int last(int /*i*/, int result) { return result; }
1285template <typename T, typename... Ts>
1286constexpr int last(int i, int result, T v, Ts... vs) { return last(i + 1, v ? i : result, vs...); }
1287NAMESPACE_END(constexpr_impl)
1288
1289/// Return the index of the first type in Ts which satisfies Predicate<T>
1290template <template<typename> class Predicate, typename... Ts>
1291constexpr int constexpr_first() { return constexpr_impl::first(0, Predicate<Ts>::value...); }
1292
1293/// Return the index of the last type in Ts which satisfies Predicate<T>
1294template <template<typename> class Predicate, typename... Ts>
1295constexpr int constexpr_last() { return constexpr_impl::last(0, -1, Predicate<Ts>::value...); }
1296
1297/// Helper class which collects only positional arguments for a Python function call.
1298/// A fancier version below can collect any argument, but this one is optimal for simple calls.
1299template <return_value_policy policy>
1300class simple_collector {
1301public:
1302    template <typename... Ts>
1303    explicit simple_collector(Ts &&...values)
1304        : m_args(pybind11::make_tuple<policy>(std::forward<Ts>(values)...)) { }
1305
1306    const tuple &args() const & { return m_args; }
1307    dict kwargs() const { return {}; }
1308
1309    tuple args() && { return std::move(m_args); }
1310
1311    /// Call a Python function and pass the collected arguments
1312    object call(PyObject *ptr) const {
1313        PyObject *result = PyObject_CallObject(ptr, m_args.ptr());
1314        if (!result)
1315            throw error_already_set();
1316        return reinterpret_steal<object>(result);
1317    }
1318
1319private:
1320    tuple m_args;
1321};
1322
1323/// Helper class which collects positional, keyword, * and ** arguments for a Python function call
1324template <return_value_policy policy>
1325class unpacking_collector {
1326public:
1327    template <typename... Ts>
1328    explicit unpacking_collector(Ts &&...values) {
1329        // Tuples aren't (easily) resizable so a list is needed for collection,
1330        // but the actual function call strictly requires a tuple.
1331        auto args_list = list();
1332        int _[] = { 0, (process(args_list, std::forward<Ts>(values)), 0)... };
1333        ignore_unused(_);
1334
1335        m_args = std::move(args_list);
1336    }
1337
1338    const tuple &args() const & { return m_args; }
1339    const dict &kwargs() const & { return m_kwargs; }
1340
1341    tuple args() && { return std::move(m_args); }
1342    dict kwargs() && { return std::move(m_kwargs); }
1343
1344    /// Call a Python function and pass the collected arguments
1345    object call(PyObject *ptr) const {
1346        PyObject *result = PyObject_Call(ptr, m_args.ptr(), m_kwargs.ptr());
1347        if (!result)
1348            throw error_already_set();
1349        return reinterpret_steal<object>(result);
1350    }
1351
1352private:
1353    template <typename T>
1354    void process(list &args_list, T &&x) {
1355        auto o = reinterpret_steal<object>(detail::make_caster<T>::cast(std::forward<T>(x), policy, {}));
1356        if (!o) {
1357#if defined(NDEBUG)
1358            argument_cast_error();
1359#else
1360            argument_cast_error(std::to_string(args_list.size()), type_id<T>());
1361#endif
1362        }
1363        args_list.append(o);
1364    }
1365
1366    void process(list &args_list, detail::args_proxy ap) {
1367        for (const auto &a : ap)
1368            args_list.append(a);
1369    }
1370
1371    void process(list &/*args_list*/, arg_v a) {
1372        if (m_kwargs.contains(a.name)) {
1373#if defined(NDEBUG)
1374            multiple_values_error();
1375#else
1376            multiple_values_error(a.name);
1377#endif
1378        }
1379        if (!a.value) {
1380#if defined(NDEBUG)
1381            argument_cast_error();
1382#else
1383            argument_cast_error(a.name, a.type);
1384#endif
1385        }
1386        m_kwargs[a.name] = a.value;
1387    }
1388
1389    void process(list &/*args_list*/, detail::kwargs_proxy kp) {
1390        if (!kp)
1391            return;
1392        for (const auto &k : reinterpret_borrow<dict>(kp)) {
1393            if (m_kwargs.contains(k.first)) {
1394#if defined(NDEBUG)
1395                multiple_values_error();
1396#else
1397                multiple_values_error(str(k.first));
1398#endif
1399            }
1400            m_kwargs[k.first] = k.second;
1401        }
1402    }
1403
1404    [[noreturn]] static void multiple_values_error() {
1405        throw type_error("Got multiple values for keyword argument "
1406                         "(compile in debug mode for details)");
1407    }
1408
1409    [[noreturn]] static void multiple_values_error(std::string name) {
1410        throw type_error("Got multiple values for keyword argument '" + name + "'");
1411    }
1412
1413    [[noreturn]] static void argument_cast_error() {
1414        throw cast_error("Unable to convert call argument to Python object "
1415                         "(compile in debug mode for details)");
1416    }
1417
1418    [[noreturn]] static void argument_cast_error(std::string name, std::string type) {
1419        throw cast_error("Unable to convert call argument '" + name
1420                         + "' of type '" + type + "' to Python object");
1421    }
1422
1423private:
1424    tuple m_args;
1425    dict m_kwargs;
1426};
1427
1428/// Collect only positional arguments for a Python function call
1429template <return_value_policy policy, typename... Args,
1430          typename = enable_if_t<all_of_t<is_positional, Args...>::value>>
1431simple_collector<policy> collect_arguments(Args &&...args) {
1432    return simple_collector<policy>(std::forward<Args>(args)...);
1433}
1434
1435/// Collect all arguments, including keywords and unpacking (only instantiated when needed)
1436template <return_value_policy policy, typename... Args,
1437          typename = enable_if_t<!all_of_t<is_positional, Args...>::value>>
1438unpacking_collector<policy> collect_arguments(Args &&...args) {
1439    // Following argument order rules for generalized unpacking according to PEP 448
1440    static_assert(
1441        constexpr_last<is_positional, Args...>() < constexpr_first<is_keyword_or_ds, Args...>()
1442        && constexpr_last<is_s_unpacking, Args...>() < constexpr_first<is_ds_unpacking, Args...>(),
1443        "Invalid function call: positional args must precede keywords and ** unpacking; "
1444        "* unpacking must precede ** unpacking"
1445    );
1446    return unpacking_collector<policy>(std::forward<Args>(args)...);
1447}
1448
1449template <typename Derived>
1450template <return_value_policy policy, typename... Args>
1451object object_api<Derived>::operator()(Args &&...args) const {
1452    return detail::collect_arguments<policy>(std::forward<Args>(args)...).call(derived().ptr());
1453}
1454
1455template <typename Derived>
1456template <return_value_policy policy, typename... Args>
1457object object_api<Derived>::call(Args &&...args) const {
1458    return operator()<policy>(std::forward<Args>(args)...);
1459}
1460
1461NAMESPACE_END(detail)
1462
1463#define PYBIND11_MAKE_OPAQUE(Type) \
1464    namespace pybind11 { namespace detail { \
1465        template<> class type_caster<Type> : public type_caster_base<Type> { }; \
1466    }}
1467
1468NAMESPACE_END(pybind11)
1469