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