pybind11.h 77.2 KB
Newer Older
Wenzel Jakob's avatar
Wenzel Jakob committed
1
/*
Wenzel Jakob's avatar
Wenzel Jakob committed
2
3
    pybind11/pybind11.h: Main header file of the C++11 python
    binding generator library
Wenzel Jakob's avatar
Wenzel Jakob committed
4

5
    Copyright (c) 2016 Wenzel Jakob <wenzel.jakob@epfl.ch>
Wenzel Jakob's avatar
Wenzel Jakob committed
6
7
8
9
10

    All rights reserved. Use of this source code is governed by a
    BSD-style license that can be found in the LICENSE file.
*/

11
#pragma once
Wenzel Jakob's avatar
Wenzel Jakob committed
12
13

#if defined(_MSC_VER)
Wenzel Jakob's avatar
Wenzel Jakob committed
14
#  pragma warning(push)
15
#  pragma warning(disable: 4100) // warning C4100: Unreferenced formal parameter
Wenzel Jakob's avatar
Wenzel Jakob committed
16
#  pragma warning(disable: 4127) // warning C4127: Conditional expression is constant
17
#  pragma warning(disable: 4512) // warning C4512: Assignment operator was implicitly defined as deleted
Wenzel Jakob's avatar
Wenzel Jakob committed
18
19
#  pragma warning(disable: 4800) // warning C4800: 'int': forcing value to bool 'true' or 'false' (performance warning)
#  pragma warning(disable: 4996) // warning C4996: The POSIX name for this item is deprecated. Instead, use the ISO C and C++ conformant name
Wenzel Jakob's avatar
Wenzel Jakob committed
20
#  pragma warning(disable: 4702) // warning C4702: unreachable code
21
#  pragma warning(disable: 4522) // warning C4522: multiple assignment operators specified
22
#elif defined(__INTEL_COMPILER)
Ben Pritchard's avatar
Ben Pritchard committed
23
#  pragma warning(push)
24
25
26
#  pragma warning(disable: 186)   // pointless comparison of unsigned integer with zero
#  pragma warning(disable: 1334)  // the "template" keyword used for syntactic disambiguation may only be used within a template
#  pragma warning(disable: 2196)  // warning #2196: routine is both "inline" and "noinline"
27
#elif defined(__GNUG__) && !defined(__clang__)
Wenzel Jakob's avatar
Wenzel Jakob committed
28
29
30
31
#  pragma GCC diagnostic push
#  pragma GCC diagnostic ignored "-Wunused-but-set-parameter"
#  pragma GCC diagnostic ignored "-Wunused-but-set-variable"
#  pragma GCC diagnostic ignored "-Wmissing-field-initializers"
Wenzel Jakob's avatar
Wenzel Jakob committed
32
33
#  pragma GCC diagnostic ignored "-Wstrict-aliasing"
#  pragma GCC diagnostic ignored "-Wattributes"
Wenzel Jakob's avatar
Wenzel Jakob committed
34
35
#endif

Wenzel Jakob's avatar
Wenzel Jakob committed
36
#include "attr.h"
37
#include "options.h"
Wenzel Jakob's avatar
Wenzel Jakob committed
38

39
NAMESPACE_BEGIN(pybind11)
Wenzel Jakob's avatar
Wenzel Jakob committed
40

Wenzel Jakob's avatar
Wenzel Jakob committed
41
/// Wraps an arbitrary C++ function/method/lambda function/.. into a callable Python object
42
class cpp_function : public function {
Wenzel Jakob's avatar
Wenzel Jakob committed
43
public:
44
    cpp_function() { }
Wenzel Jakob's avatar
Wenzel Jakob committed
45

46
    /// Construct a cpp_function from a vanilla function pointer
47
48
    template <typename Return, typename... Args, typename... Extra /*,*/ PYBIND11_NOEXCEPT_TPL_ARG>
    cpp_function(Return (*f)(Args...) PYBIND11_NOEXCEPT_SPECIFIER, const Extra&... extra) {
49
        initialize(f, f, extra...);
Wenzel Jakob's avatar
Wenzel Jakob committed
50
51
    }

52
    /// Construct a cpp_function from a lambda function (possibly with internal state)
53
    template <typename Func, typename... Extra> cpp_function(Func &&f, const Extra&... extra) {
54
        initialize(std::forward<Func>(f),
Wenzel Jakob's avatar
Wenzel Jakob committed
55
                   (typename detail::remove_class<decltype(
Wenzel Jakob's avatar
Wenzel Jakob committed
56
                       &std::remove_reference<Func>::type::operator())>::type *) nullptr, extra...);
Wenzel Jakob's avatar
Wenzel Jakob committed
57
58
    }

59
    /// Construct a cpp_function from a class method (non-const)
60
61
    template <typename Return, typename Class, typename... Arg, typename... Extra /*,*/ PYBIND11_NOEXCEPT_TPL_ARG>
    cpp_function(Return (Class::*f)(Arg...) PYBIND11_NOEXCEPT_SPECIFIER, const Extra&... extra) {
62
        initialize([f](Class *c, Arg... args) -> Return { return (c->*f)(args...); },
63
                   (Return (*) (Class *, Arg...) PYBIND11_NOEXCEPT_SPECIFIER) nullptr, extra...);
Wenzel Jakob's avatar
Wenzel Jakob committed
64
    }
Wenzel Jakob's avatar
Wenzel Jakob committed
65

66
    /// Construct a cpp_function from a class method (const)
67
68
    template <typename Return, typename Class, typename... Arg, typename... Extra /*,*/ PYBIND11_NOEXCEPT_TPL_ARG>
    cpp_function(Return (Class::*f)(Arg...) const PYBIND11_NOEXCEPT_SPECIFIER, const Extra&... extra) {
69
        initialize([f](const Class *c, Arg... args) -> Return { return (c->*f)(args...); },
70
                   (Return (*)(const Class *, Arg ...) PYBIND11_NOEXCEPT_SPECIFIER) nullptr, extra...);
Wenzel Jakob's avatar
Wenzel Jakob committed
71
72
    }

73
    /// Return the function name
Wenzel Jakob's avatar
Wenzel Jakob committed
74
    object name() const { return attr("__name__"); }
75

76
protected:
77
78
79
80
81
    /// Space optimization: don't inline this frequently instantiated fragment
    PYBIND11_NOINLINE detail::function_record *make_function_record() {
        return new detail::function_record();
    }

82
    /// Special internal constructor for functors, lambda functions, etc.
83
84
    template <typename Func, typename Return, typename... Args, typename... Extra /*,*/ PYBIND11_NOEXCEPT_TPL_ARG>
    void initialize(Func &&f, Return (*)(Args...) PYBIND11_NOEXCEPT_SPECIFIER, const Extra&... extra) {
85
86
87
        static_assert(detail::expected_num_args<Extra...>(sizeof...(Args)),
                      "The number of named arguments does not match the function signature");

88
        struct capture { typename std::remove_reference<Func>::type f; };
Wenzel Jakob's avatar
Wenzel Jakob committed
89

90
        /* Store the function including any extra state it might have (e.g. a lambda capture object) */
91
        auto rec = make_function_record();
92

93
94
        /* Store the capture object directly in the function record if there is enough space */
        if (sizeof(capture) <= sizeof(rec->data)) {
95
96
97
            /* Without these pragmas, GCC warns that there might not be
               enough space to use the placement new operator. However, the
               'if' statement above ensures that this is the case. */
98
#if defined(__GNUG__) && !defined(__clang__) && __GNUC__ >= 6
99
100
101
#  pragma GCC diagnostic push
#  pragma GCC diagnostic ignored "-Wplacement-new"
#endif
102
            new ((capture *) &rec->data) capture { std::forward<Func>(f) };
103
#if defined(__GNUG__) && !defined(__clang__) && __GNUC__ >= 6
104
105
#  pragma GCC diagnostic pop
#endif
106
107
108
109
110
111
            if (!std::is_trivially_destructible<Func>::value)
                rec->free_data = [](detail::function_record *r) { ((capture *) &r->data)->~capture(); };
        } else {
            rec->data[0] = new capture { std::forward<Func>(f) };
            rec->free_data = [](detail::function_record *r) { delete ((capture *) r->data[0]); };
        }
112

113
        /* Type casters for the function arguments and return value */
114
115
116
117
        using cast_in = detail::argument_loader<Args...>;
        using cast_out = detail::make_caster<
            detail::conditional_t<std::is_void<Return>::value, detail::void_type, Return>
        >;
Wenzel Jakob's avatar
Wenzel Jakob committed
118

119
        /* Dispatch code which converts function arguments and performs the actual function call */
120
        rec->impl = [](detail::function_record *rec, handle args, handle kwargs, handle parent) -> handle {
121
            cast_in args_converter;
122
123

            /* Try to cast the function arguments into the C++ domain */
124
            if (!args_converter.load_args(args, kwargs))
125
126
                return PYBIND11_TRY_NEXT_OVERLOAD;

Wenzel Jakob's avatar
Wenzel Jakob committed
127
            /* Invoke call policy pre-call hook */
128
129
130
131
132
            detail::process_attributes<Extra...>::precall(args);

            /* Get a pointer to the capture object */
            capture *cap = (capture *) (sizeof(capture) <= sizeof(rec->data)
                                        ? &rec->data : rec->data[0]);
133

134
135
136
137
138
            /* Override policy for rvalues -- always move */
            constexpr auto is_rvalue = !std::is_pointer<Return>::value
                                       && !std::is_lvalue_reference<Return>::value;
            const auto policy = is_rvalue ? return_value_policy::move : rec->policy;

139
            /* Perform the function call */
140
            handle result = cast_out::cast(args_converter.template call<Return>(cap->f),
141
                                           policy, parent);
Wenzel Jakob's avatar
Wenzel Jakob committed
142
143

            /* Invoke call policy post-call hook */
144
            detail::process_attributes<Extra...>::postcall(args, result);
145

146
            return result;
Wenzel Jakob's avatar
Wenzel Jakob committed
147
148
        };

Wenzel Jakob's avatar
Wenzel Jakob committed
149
150
        /* Process any user-provided function attributes */
        detail::process_attributes<Extra...>::init(extra..., rec);
151
152

        /* Generate a readable signature describing the function's arguments and return value types */
153
        using detail::descr; using detail::_;
154
        PYBIND11_DESCR signature = _("(") + cast_in::arg_names() + _(") -> ") + cast_out::name();
155
156

        /* Register the function with Python from generic (non-templated) code */
157
        initialize_generic(rec, signature.text(), signature.types(), sizeof...(Args));
158
159
160

        if (cast_in::has_args) rec->has_args = true;
        if (cast_in::has_kwargs) rec->has_kwargs = true;
161
162

        /* Stash some additional information used by an important optimization in 'functional.h' */
163
        using FunctionType = Return (*)(Args...) PYBIND11_NOEXCEPT_SPECIFIER;
164
165
166
167
168
169
170
        constexpr bool is_function_ptr =
            std::is_convertible<Func, FunctionType>::value &&
            sizeof(capture) == sizeof(void *);
        if (is_function_ptr) {
            rec->is_stateless = true;
            rec->data[1] = (void *) &typeid(FunctionType);
        }
171
172
    }

173
    /// Register a function call with Python (generic non-templated code goes here)
174
    void initialize_generic(detail::function_record *rec, const char *text,
175
                            const std::type_info *const *types, size_t args) {
Wenzel Jakob's avatar
Wenzel Jakob committed
176

177
        /* Create copies of all referenced C-style strings */
Wenzel Jakob's avatar
Wenzel Jakob committed
178
179
180
        rec->name = strdup(rec->name ? rec->name : "");
        if (rec->doc) rec->doc = strdup(rec->doc);
        for (auto &a: rec->args) {
181
182
183
184
185
            if (a.name)
                a.name = strdup(a.name);
            if (a.descr)
                a.descr = strdup(a.descr);
            else if (a.value)
186
                a.descr = strdup(a.value.attr("__repr__")().cast<std::string>().c_str());
187
        }
188

189
190
191
192
193
194
195
196
197
        /* Generate a proper function signature */
        std::string signature;
        size_t type_depth = 0, char_index = 0, type_index = 0, arg_index = 0;
        while (true) {
            char c = text[char_index++];
            if (c == '\0')
                break;

            if (c == '{') {
198
                // Write arg name for everything except *args, **kwargs and return type.
199
                if (type_depth == 0 && text[char_index] != '*' && arg_index < args) {
200
201
                    if (!rec->args.empty()) {
                        signature += rec->args[arg_index].name;
202
                    } else if (arg_index == 0 && rec->is_method) {
203
204
                        signature += "self";
                    } else {
205
                        signature += "arg" + std::to_string(arg_index - (rec->is_method ? 1 : 0));
206
207
                    }
                    signature += ": ";
208
209
210
211
                }
                ++type_depth;
            } else if (c == '}') {
                --type_depth;
212
                if (type_depth == 0) {
213
214
                    if (arg_index < rec->args.size() && rec->args[arg_index].descr) {
                        signature += "=";
Wenzel Jakob's avatar
Wenzel Jakob committed
215
                        signature += rec->args[arg_index].descr;
216
217
218
219
220
                    }
                    arg_index++;
                }
            } else if (c == '%') {
                const std::type_info *t = types[type_index++];
221
                if (!t)
222
                    pybind11_fail("Internal error while parsing type signature (1)");
223
                if (auto tinfo = detail::get_type_info(*t)) {
Wenzel Jakob's avatar
Wenzel Jakob committed
224
225
226
227
228
#if defined(PYPY_VERSION)
                    signature += handle((PyObject *) tinfo->type)
                                     .attr("__module__")
                                     .cast<std::string>() + ".";
#endif
229
                    signature += tinfo->type->tp_name;
230
231
232
233
234
235
236
237
238
                } else {
                    std::string tname(t->name());
                    detail::clean_type_id(tname);
                    signature += tname;
                }
            } else {
                signature += c;
            }
        }
239
        if (type_depth != 0 || types[type_index] != nullptr)
240
            pybind11_fail("Internal error while parsing type signature (2)");
241
242
243
244
245

        #if !defined(PYBIND11_CPP14)
            delete[] types;
            delete[] text;
        #endif
246

247
#if PY_MAJOR_VERSION < 3
Wenzel Jakob's avatar
Wenzel Jakob committed
248
249
250
        if (strcmp(rec->name, "__next__") == 0) {
            std::free(rec->name);
            rec->name = strdup("next");
251
252
253
        } else if (strcmp(rec->name, "__bool__") == 0) {
            std::free(rec->name);
            rec->name = strdup("__nonzero__");
254
        }
255
#endif
Wenzel Jakob's avatar
Wenzel Jakob committed
256
257
        rec->signature = strdup(signature.c_str());
        rec->args.shrink_to_fit();
258
        rec->is_constructor = !strcmp(rec->name, "__init__") || !strcmp(rec->name, "__setstate__");
259
        rec->nargs = (uint16_t) args;
260
261

#if PY_MAJOR_VERSION < 3
Wenzel Jakob's avatar
Wenzel Jakob committed
262
263
        if (rec->sibling && PyMethod_Check(rec->sibling.ptr()))
            rec->sibling = PyMethod_GET_FUNCTION(rec->sibling.ptr());
264
#endif
265

Wenzel Jakob's avatar
Wenzel Jakob committed
266
        detail::function_record *chain = nullptr, *chain_start = rec;
267
268
        if (rec->sibling) {
            if (PyCFunction_Check(rec->sibling.ptr())) {
Wenzel Jakob's avatar
Wenzel Jakob committed
269
                auto rec_capsule = reinterpret_borrow<capsule>(PyCFunction_GET_SELF(rec->sibling.ptr()));
270
271
272
                chain = (detail::function_record *) rec_capsule;
                /* Never append a method to an overload chain of a parent class;
                   instead, hide the parent's overloads in this case */
273
                if (chain->scope != rec->scope)
274
275
276
277
278
279
                    chain = nullptr;
            }
            // Don't trigger for things like the default __init__, which are wrapper_descriptors that we are intentionally replacing
            else if (!rec->sibling.is_none() && rec->name[0] != '_')
                pybind11_fail("Cannot overload existing non-function object \"" + std::string(rec->name) +
                        "\" with a function of the same name");
280
281
        }

Wenzel Jakob's avatar
Wenzel Jakob committed
282
        if (!chain) {
283
            /* No existing overload was found, create a new function object */
Wenzel Jakob's avatar
Wenzel Jakob committed
284
285
286
287
288
            rec->def = new PyMethodDef();
            memset(rec->def, 0, sizeof(PyMethodDef));
            rec->def->ml_name = rec->name;
            rec->def->ml_meth = reinterpret_cast<PyCFunction>(*dispatcher);
            rec->def->ml_flags = METH_VARARGS | METH_KEYWORDS;
289

Wenzel Jakob's avatar
Wenzel Jakob committed
290
291
            capsule rec_capsule(rec, [](PyObject *o) {
                destruct((detail::function_record *) PyCapsule_GetPointer(o, nullptr));
292
            });
293
294
295

            object scope_module;
            if (rec->scope) {
296
297
298
299
300
                if (hasattr(rec->scope, "__module__")) {
                    scope_module = rec->scope.attr("__module__");
                } else if (hasattr(rec->scope, "__name__")) {
                    scope_module = rec->scope.attr("__name__");
                }
301
302
303
            }

            m_ptr = PyCFunction_NewEx(rec->def, rec_capsule.ptr(), scope_module.ptr());
Wenzel Jakob's avatar
Wenzel Jakob committed
304
            if (!m_ptr)
305
                pybind11_fail("cpp_function::cpp_function(): Could not allocate function object");
Wenzel Jakob's avatar
Wenzel Jakob committed
306
        } else {
307
            /* Append at the end of the overload chain */
Wenzel Jakob's avatar
Wenzel Jakob committed
308
            m_ptr = rec->sibling.ptr();
Wenzel Jakob's avatar
Wenzel Jakob committed
309
            inc_ref();
Wenzel Jakob's avatar
Wenzel Jakob committed
310
311
312
313
            chain_start = chain;
            while (chain->next)
                chain = chain->next;
            chain->next = rec;
Wenzel Jakob's avatar
Wenzel Jakob committed
314
        }
315

Wenzel Jakob's avatar
Wenzel Jakob committed
316
        std::string signatures;
317
        int index = 0;
Wenzel Jakob's avatar
Wenzel Jakob committed
318
        /* Create a nice pydoc rec including all signatures and
319
           docstrings of the functions in the overload chain */
320
        if (chain && options::show_function_signatures()) {
321
322
323
324
325
326
            // First a generic signature
            signatures += rec->name;
            signatures += "(*args, **kwargs)\n";
            signatures += "Overloaded function.\n\n";
        }
        // Then specific overload signatures
Wenzel Jakob's avatar
Wenzel Jakob committed
327
        for (auto it = chain_start; it != nullptr; it = it->next) {
328
329
330
331
332
            if (options::show_function_signatures()) {
                if (chain)
                    signatures += std::to_string(++index) + ". ";
                signatures += rec->name;
                signatures += it->signature;
333
                signatures += "\n";
334
335
336
            }
            if (it->doc && strlen(it->doc) > 0 && options::show_user_defined_docstrings()) {
                if (options::show_function_signatures()) signatures += "\n";
337
                signatures += it->doc;
338
                if (options::show_function_signatures()) signatures += "\n";
339
            }
340
            if (it->next)
Wenzel Jakob's avatar
Wenzel Jakob committed
341
342
                signatures += "\n";
        }
Wenzel Jakob's avatar
Wenzel Jakob committed
343
344

        /* Install docstring */
Wenzel Jakob's avatar
Wenzel Jakob committed
345
346
        PyCFunctionObject *func = (PyCFunctionObject *) m_ptr;
        if (func->m_ml->ml_doc)
347
            std::free((char *) func->m_ml->ml_doc);
Wenzel Jakob's avatar
Wenzel Jakob committed
348
        func->m_ml->ml_doc = strdup(signatures.c_str());
Wenzel Jakob's avatar
Wenzel Jakob committed
349

350
351
        if (rec->is_method) {
            m_ptr = PYBIND11_INSTANCE_METHOD_NEW(m_ptr, rec->scope.ptr());
Wenzel Jakob's avatar
Wenzel Jakob committed
352
            if (!m_ptr)
353
                pybind11_fail("cpp_function::cpp_function(): Could not allocate instance method object");
Wenzel Jakob's avatar
Wenzel Jakob committed
354
355
356
            Py_DECREF(func);
        }
    }
Wenzel Jakob's avatar
Wenzel Jakob committed
357
358
359
360
361
362

    /// When a cpp_function is GCed, release any memory allocated by pybind11
    static void destruct(detail::function_record *rec) {
        while (rec) {
            detail::function_record *next = rec->next;
            if (rec->free_data)
363
                rec->free_data(rec);
Wenzel Jakob's avatar
Wenzel Jakob committed
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
            std::free((char *) rec->name);
            std::free((char *) rec->doc);
            std::free((char *) rec->signature);
            for (auto &arg: rec->args) {
                std::free((char *) arg.name);
                std::free((char *) arg.descr);
                arg.value.dec_ref();
            }
            if (rec->def) {
                std::free((char *) rec->def->ml_doc);
                delete rec->def;
            }
            delete rec;
            rec = next;
        }
    }

    /// Main dispatch logic for calls to functions bound using pybind11
    static PyObject *dispatcher(PyObject *self, PyObject *args, PyObject *kwargs) {
        /* Iterator over the list of potentially admissible overloads */
        detail::function_record *overloads = (detail::function_record *) PyCapsule_GetPointer(self, nullptr),
                                *it = overloads;

        /* Need to know how many arguments + keyword arguments there are to pick the right overload */
388
389
        size_t nargs = (size_t) PyTuple_GET_SIZE(args),
               nkwargs = kwargs ? (size_t) PyDict_Size(kwargs) : 0;
Wenzel Jakob's avatar
Wenzel Jakob committed
390

391
        handle parent = nargs > 0 ? PyTuple_GET_ITEM(args, 0) : nullptr,
Wenzel Jakob's avatar
Wenzel Jakob committed
392
393
394
               result = PYBIND11_TRY_NEXT_OVERLOAD;
        try {
            for (; it != nullptr; it = it->next) {
395
                auto args_ = reinterpret_borrow<tuple>(args);
396
                size_t kwargs_consumed = 0;
Wenzel Jakob's avatar
Wenzel Jakob committed
397
398
399
400
401
402
403
404

                /* For each overload:
                   1. If the required list of arguments is longer than the
                      actually provided amount, create a copy of the argument
                      list and fill in any available keyword/default arguments.
                   2. Ensure that all keyword arguments were "consumed"
                   3. Call the function call dispatcher (function_record::impl)
                 */
405
406
407
408
409
                size_t nargs_ = nargs;
                if (nargs < it->args.size()) {
                    nargs_ = it->args.size();
                    args_ = tuple(nargs_);
                    for (size_t i = 0; i < nargs; ++i) {
Wenzel Jakob's avatar
Wenzel Jakob committed
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
                        handle item = PyTuple_GET_ITEM(args, i);
                        PyTuple_SET_ITEM(args_.ptr(), i, item.inc_ref().ptr());
                    }

                    int arg_ctr = 0;
                    for (auto const &it2 : it->args) {
                        int index = arg_ctr++;
                        if (PyTuple_GET_ITEM(args_.ptr(), index))
                            continue;

                        handle value;
                        if (kwargs)
                            value = PyDict_GetItemString(kwargs, it2.name);

                        if (value)
                            kwargs_consumed++;
                        else if (it2.value)
                            value = it2.value;

                        if (value) {
                            PyTuple_SET_ITEM(args_.ptr(), index, value.inc_ref().ptr());
                        } else {
432
                            kwargs_consumed = (size_t) -1; /* definite failure */
Wenzel Jakob's avatar
Wenzel Jakob committed
433
434
435
436
                            break;
                        }
                    }
                }
437
438

                try {
439
440
441
                    if ((kwargs_consumed == nkwargs || it->has_kwargs) &&
                        (nargs_ == it->nargs || it->has_args))
                        result = it->impl(it, args_, kwargs, parent);
442
                } catch (reference_cast_error &) {
443
444
                    result = PYBIND11_TRY_NEXT_OVERLOAD;
                }
Wenzel Jakob's avatar
Wenzel Jakob committed
445
446
447
448

                if (result.ptr() != PYBIND11_TRY_NEXT_OVERLOAD)
                    break;
            }
449
450
        } catch (error_already_set &e) {
            e.restore();
451
            return nullptr;
Wenzel Jakob's avatar
Wenzel Jakob committed
452
        } catch (...) {
453
454
455
            /* When an exception is caught, give each registered exception
               translator a chance to translate it to a Python exception
               in reverse order of registration.
456

457
               A translator may choose to do one of the following:
458

459
460
461
462
463
                - catch the exception and call PyErr_SetString or PyErr_SetObject
                  to set a standard (or custom) Python exception, or
                - do nothing and let the exception fall through to the next translator, or
                - delegate translation to the next translator by throwing a new type of exception. */

464
            auto last_exception = std::current_exception();
465
466
467
468
469
470
471
472
473
474
475
            auto &registered_exception_translators = pybind11::detail::get_internals().registered_exception_translators;
            for (auto& translator : registered_exception_translators) {
                try {
                    translator(last_exception);
                } catch (...) {
                    last_exception = std::current_exception();
                    continue;
                }
                return nullptr;
            }
            PyErr_SetString(PyExc_SystemError, "Exception escaped from default exception translator!");
Wenzel Jakob's avatar
Wenzel Jakob committed
476
477
478
479
            return nullptr;
        }

        if (result.ptr() == PYBIND11_TRY_NEXT_OVERLOAD) {
480
481
482
            if (overloads->is_operator)
                return handle(Py_NotImplemented).inc_ref().ptr();

483
484
485
486
            std::string msg = std::string(overloads->name) + "(): incompatible " +
                std::string(overloads->is_constructor ? "constructor" : "function") +
                " arguments. The following argument types are supported:\n";

Wenzel Jakob's avatar
Wenzel Jakob committed
487
488
489
            int ctr = 0;
            for (detail::function_record *it2 = overloads; it2 != nullptr; it2 = it2->next) {
                msg += "    "+ std::to_string(++ctr) + ". ";
490
491
492

                bool wrote_sig = false;
                if (overloads->is_constructor) {
493
                    // For a constructor, rewrite `(self: Object, arg0, ...) -> NoneType` as `Object(arg0, ...)`
494
                    std::string sig = it2->signature;
495
                    size_t start = sig.find('(') + 7; // skip "(self: "
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
                    if (start < sig.size()) {
                        // End at the , for the next argument
                        size_t end = sig.find(", "), next = end + 2;
                        size_t ret = sig.rfind(" -> ");
                        // Or the ), if there is no comma:
                        if (end >= sig.size()) next = end = sig.find(')');
                        if (start < end && next < sig.size()) {
                            msg.append(sig, start, end - start);
                            msg += '(';
                            msg.append(sig, next, ret - next);
                            wrote_sig = true;
                        }
                    }
                }
                if (!wrote_sig) msg += it2->signature;

Wenzel Jakob's avatar
Wenzel Jakob committed
512
513
                msg += "\n";
            }
514
            msg += "\nInvoked with: ";
515
            auto args_ = reinterpret_borrow<tuple>(args);
516
            for (size_t ti = overloads->is_constructor ? 1 : 0; ti < args_.size(); ++ti) {
517
                msg += pybind11::repr(args_[ti]);
518
519
520
                if ((ti + 1) != args_.size() )
                    msg += ", ";
            }
Wenzel Jakob's avatar
Wenzel Jakob committed
521
522
523
524
525
526
527
528
529
530
            PyErr_SetString(PyExc_TypeError, msg.c_str());
            return nullptr;
        } else if (!result) {
            std::string msg = "Unable to convert function return value to a "
                              "Python type! The signature was\n\t";
            msg += it->signature;
            PyErr_SetString(PyExc_TypeError, msg.c_str());
            return nullptr;
        } else {
            if (overloads->is_constructor) {
531
                /* When a constructor ran successfully, the corresponding
Wenzel Jakob's avatar
Wenzel Jakob committed
532
                   holder type (e.g. std::unique_ptr) must still be initialized. */
533
                PyObject *inst = PyTuple_GET_ITEM(args, 0);
Wenzel Jakob's avatar
Wenzel Jakob committed
534
535
536
537
538
539
                auto tinfo = detail::get_type_info(Py_TYPE(inst));
                tinfo->init_holder(inst, nullptr);
            }
            return result.ptr();
        }
    }
Wenzel Jakob's avatar
Wenzel Jakob committed
540
541
};

542
/// Wrapper for Python extension modules
Wenzel Jakob's avatar
Wenzel Jakob committed
543
544
class module : public object {
public:
545
    PYBIND11_OBJECT_DEFAULT(module, object, PyModule_Check)
Wenzel Jakob's avatar
Wenzel Jakob committed
546

547
    explicit module(const char *name, const char *doc = nullptr) {
548
        if (!options::show_user_defined_docstrings()) doc = nullptr;
549
#if PY_MAJOR_VERSION >= 3
Wenzel Jakob's avatar
Wenzel Jakob committed
550
551
552
553
554
555
556
        PyModuleDef *def = new PyModuleDef();
        memset(def, 0, sizeof(PyModuleDef));
        def->m_name = name;
        def->m_doc = doc;
        def->m_size = -1;
        Py_INCREF(def);
        m_ptr = PyModule_Create(def);
557
558
559
#else
        m_ptr = Py_InitModule3(name, nullptr, doc);
#endif
Wenzel Jakob's avatar
Wenzel Jakob committed
560
        if (m_ptr == nullptr)
561
            pybind11_fail("Internal error in module::module()");
Wenzel Jakob's avatar
Wenzel Jakob committed
562
563
564
        inc_ref();
    }

565
    template <typename Func, typename... Extra>
Wenzel Jakob's avatar
Wenzel Jakob committed
566
    module &def(const char *name_, Func &&f, const Extra& ... extra) {
567
568
        cpp_function func(std::forward<Func>(f), name(name_), scope(*this),
                          sibling(getattr(*this, name_, none())), extra...);
569
570
571
        // NB: allow overwriting here because cpp_function sets up a chain with the intention of
        // overwriting (and has already checked internally that it isn't overwriting non-functions).
        add_object(name_, func, true /* overwrite */);
Wenzel Jakob's avatar
Wenzel Jakob committed
572
573
574
        return *this;
    }

575
    module def_submodule(const char *name, const char *doc = nullptr) {
Wenzel Jakob's avatar
Wenzel Jakob committed
576
577
        std::string full_name = std::string(PyModule_GetName(m_ptr))
            + std::string(".") + std::string(name);
578
        auto result = reinterpret_borrow<module>(PyImport_AddModule(full_name.c_str()));
579
        if (doc && options::show_user_defined_docstrings())
580
            result.attr("__doc__") = pybind11::str(doc);
Wenzel Jakob's avatar
Wenzel Jakob committed
581
582
583
        attr(name) = result;
        return result;
    }
Wenzel Jakob's avatar
Wenzel Jakob committed
584
585

    static module import(const char *name) {
586
587
        PyObject *obj = PyImport_ImportModule(name);
        if (!obj)
588
            throw error_already_set();
589
        return reinterpret_steal<module>(obj);
Wenzel Jakob's avatar
Wenzel Jakob committed
590
    }
591
592
593
594
595
596
597
598
599
600
601
602
603
604

    // Adds an object to the module using the given name.  Throws if an object with the given name
    // already exists.
    //
    // overwrite should almost always be false: attempting to overwrite objects that pybind11 has
    // established will, in most cases, break things.
    PYBIND11_NOINLINE void add_object(const char *name, object &obj, bool overwrite = false) {
        if (!overwrite && hasattr(*this, name))
            pybind11_fail("Error during initialization: multiple incompatible definitions with name \"" +
                    std::string(name) + "\"");

        obj.inc_ref(); // PyModule_AddObject() steals a reference
        PyModule_AddObject(ptr(), name, obj.ptr());
    }
Wenzel Jakob's avatar
Wenzel Jakob committed
605
606
607
};

NAMESPACE_BEGIN(detail)
Dean Moldovan's avatar
Dean Moldovan committed
608
extern "C" inline PyObject *get_dict(PyObject *op, void *) {
609
    PyObject *&dict = *_PyObject_GetDictPtr(op);
Wenzel Jakob's avatar
Wenzel Jakob committed
610
    if (!dict)
611
612
613
        dict = PyDict_New();
    Py_XINCREF(dict);
    return dict;
Dean Moldovan's avatar
Dean Moldovan committed
614
615
}

616
617
extern "C" inline int set_dict(PyObject *op, PyObject *new_dict, void *) {
    if (!PyDict_Check(new_dict)) {
Dean Moldovan's avatar
Dean Moldovan committed
618
        PyErr_Format(PyExc_TypeError, "__dict__ must be set to a dictionary, not a '%.200s'",
619
                     Py_TYPE(new_dict)->tp_name);
Dean Moldovan's avatar
Dean Moldovan committed
620
621
        return -1;
    }
622
623
624
625
    PyObject *&dict = *_PyObject_GetDictPtr(op);
    Py_INCREF(new_dict);
    Py_CLEAR(dict);
    dict = new_dict;
Dean Moldovan's avatar
Dean Moldovan committed
626
627
628
629
630
631
632
633
    return 0;
}

static PyGetSetDef generic_getset[] = {
    {const_cast<char*>("__dict__"), get_dict, set_dict, nullptr, nullptr},
    {nullptr, nullptr, nullptr, nullptr, nullptr}
};

Wenzel Jakob's avatar
Wenzel Jakob committed
634
/// Generic support for creating new Python heap types
635
class generic_type : public object {
636
    template <typename...> friend class class_;
Wenzel Jakob's avatar
Wenzel Jakob committed
637
public:
638
    PYBIND11_OBJECT_DEFAULT(generic_type, object, PyType_Check)
Wenzel Jakob's avatar
Wenzel Jakob committed
639
640
protected:
    void initialize(type_record *rec) {
641
642
643
        auto &internals = get_internals();
        auto tindex = std::type_index(*(rec->type));

644
        if (get_type_info(*(rec->type)))
645
646
647
            pybind11_fail("generic_type: type \"" + std::string(rec->name) +
                          "\" is already registered!");

648
        auto name = reinterpret_steal<object>(PYBIND11_FROM_STRING(rec->name));
649
650
        object scope_module;
        if (rec->scope) {
651
652
653
654
            if (hasattr(rec->scope, rec->name))
                pybind11_fail("generic_type: cannot initialize type \"" + std::string(rec->name) +
                        "\": an object with that name is already defined");

655
656
657
658
659
            if (hasattr(rec->scope, "__module__")) {
                scope_module = rec->scope.attr("__module__");
            } else if (hasattr(rec->scope, "__name__")) {
                scope_module = rec->scope.attr("__name__");
            }
660
661
662
663
664
        }

#if PY_MAJOR_VERSION >= 3 && PY_MINOR_VERSION >= 3
        /* Qualified names for Python >= 3.3 */
        object scope_qualname;
665
666
        if (rec->scope && hasattr(rec->scope, "__qualname__"))
            scope_qualname = rec->scope.attr("__qualname__");
Wenzel Jakob's avatar
Wenzel Jakob committed
667
668
        object ht_qualname, ht_qualname_meta;
        if (scope_qualname)
669
670
            ht_qualname = reinterpret_steal<object>(PyUnicode_FromFormat(
                "%U.%U", scope_qualname.ptr(), name.ptr()));
Wenzel Jakob's avatar
Wenzel Jakob committed
671
        else
672
            ht_qualname = name;
Wenzel Jakob's avatar
Wenzel Jakob committed
673
674
675
        if (rec->metaclass)
            ht_qualname_meta = reinterpret_steal<object>(
                PyUnicode_FromFormat("%U__Meta", ht_qualname.ptr()));
676
#endif
Wenzel Jakob's avatar
Wenzel Jakob committed
677

Wenzel Jakob's avatar
Wenzel Jakob committed
678
#if !defined(PYPY_VERSION)
679
        std::string full_name = (scope_module ? ((std::string) pybind11::str(scope_module) + "." + rec->name)
680
                                              : std::string(rec->name));
Wenzel Jakob's avatar
Wenzel Jakob committed
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
#else
        std::string full_name = std::string(rec->name);
#endif

        /* Create a custom metaclass if requested (used for static properties) */
        object metaclass;
        if (rec->metaclass) {
            std::string meta_name_ = full_name + "__Meta";
            object meta_name = reinterpret_steal<object>(PYBIND11_FROM_STRING(meta_name_.c_str()));
            metaclass = reinterpret_steal<object>(PyType_Type.tp_alloc(&PyType_Type, 0));
            if (!metaclass || !name)
                pybind11_fail("generic_type::generic_type(): unable to create metaclass!");

            /* Danger zone: from now (and until PyType_Ready), make sure to
               issue no Python C API calls which could potentially invoke the
               garbage collector (the GC will call type_traverse(), which will in
               turn find the newly constructed type in an invalid state) */

            auto type = (PyHeapTypeObject*) metaclass.ptr();
            type->ht_name = meta_name.release().ptr();

#if PY_MAJOR_VERSION >= 3 && PY_MINOR_VERSION >= 3
            /* Qualified names for Python >= 3.3 */
704
            type->ht_qualname = ht_qualname_meta.release().ptr();
Wenzel Jakob's avatar
Wenzel Jakob committed
705
706
707
708
709
710
711
712
713
714
715
716
#endif
            type->ht_type.tp_name = strdup(meta_name_.c_str());
            type->ht_type.tp_base = &PyType_Type;
            type->ht_type.tp_flags |= (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HEAPTYPE) &
                                      ~Py_TPFLAGS_HAVE_GC;

            if (PyType_Ready(&type->ht_type) < 0)
                pybind11_fail("generic_type::generic_type(): failure in PyType_Ready() for metaclass!");
        }

        size_t num_bases = rec->bases.size();
        auto bases = tuple(rec->bases);
717
718

        char *tp_doc = nullptr;
719
        if (rec->doc && options::show_user_defined_docstrings()) {
720
721
722
723
724
725
726
            /* Allocate memory for docstring (using PyObject_MALLOC, since
               Python will free this later on) */
            size_t size = strlen(rec->doc) + 1;
            tp_doc = (char *) PyObject_MALLOC(size);
            memcpy((void *) tp_doc, rec->doc, size);
        }

Wenzel Jakob's avatar
Wenzel Jakob committed
727
728
729
730
731
        /* Danger zone: from now (and until PyType_Ready), make sure to
           issue no Python C API calls which could potentially invoke the
           garbage collector (the GC will call type_traverse(), which will in
           turn find the newly constructed type in an invalid state) */

732
        auto type_holder = reinterpret_steal<object>(PyType_Type.tp_alloc(&PyType_Type, 0));
733
734
735
        auto type = (PyHeapTypeObject*) type_holder.ptr();

        if (!type_holder || !name)
736
            pybind11_fail(std::string(rec->name) + ": Unable to create type object!");
737
738
739
740

        /* Register supplemental type information in C++ dict */
        detail::type_info *tinfo = new detail::type_info();
        tinfo->type = (PyTypeObject *) type;
Wenzel Jakob's avatar
Wenzel Jakob committed
741
742
        tinfo->type_size = rec->type_size;
        tinfo->init_holder = rec->init_holder;
743
        tinfo->direct_conversions = &internals.direct_conversions[tindex];
744
        tinfo->default_holder = rec->default_holder;
745
        internals.registered_types_cpp[tindex] = tinfo;
746
747
748
749
        internals.registered_types_py[type] = tinfo;

        /* Basic type attributes */
        type->ht_type.tp_name = strdup(full_name.c_str());
750
        type->ht_type.tp_basicsize = (ssize_t) rec->instance_size;
Wenzel Jakob's avatar
Wenzel Jakob committed
751
752
753
754
755
756

        if (num_bases > 0) {
            type->ht_type.tp_base = (PyTypeObject *) ((object) bases[0]).inc_ref().ptr();
            type->ht_type.tp_bases = bases.release().ptr();
            rec->multiple_inheritance |= num_bases > 1;
        }
757

758
759
        type->ht_name = name.release().ptr();

760
#if PY_MAJOR_VERSION >= 3 && PY_MINOR_VERSION >= 3
761
        type->ht_qualname = ht_qualname.release().ptr();
762
#endif
Wenzel Jakob's avatar
Wenzel Jakob committed
763

Wenzel Jakob's avatar
Wenzel Jakob committed
764
765
766
        /* Metaclass */
        PYBIND11_OB_TYPE(type->ht_type) = (PyTypeObject *) metaclass.release().ptr();

767
768
769
770
        /* Supported protocols */
        type->ht_type.tp_as_number = &type->as_number;
        type->ht_type.tp_as_sequence = &type->as_sequence;
        type->ht_type.tp_as_mapping = &type->as_mapping;
Wenzel Jakob's avatar
Wenzel Jakob committed
771

772
        /* Supported elementary operations */
Wenzel Jakob's avatar
Wenzel Jakob committed
773
774
        type->ht_type.tp_init = (initproc) init;
        type->ht_type.tp_new = (newfunc) new_instance;
Wenzel Jakob's avatar
Wenzel Jakob committed
775
        type->ht_type.tp_dealloc = rec->dealloc;
776
777

        /* Support weak references (needed for the keep_alive feature) */
778
        type->ht_type.tp_weaklistoffset = offsetof(instance_essentials<void>, weakrefs);
779
780
781

        /* Flags */
        type->ht_type.tp_flags |= Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HEAPTYPE;
782
783
784
#if PY_MAJOR_VERSION < 3
        type->ht_type.tp_flags |= Py_TPFLAGS_CHECKTYPES;
#endif
785
        type->ht_type.tp_flags &= ~Py_TPFLAGS_HAVE_GC;
786

Dean Moldovan's avatar
Dean Moldovan committed
787
788
        /* Support dynamic attributes */
        if (rec->dynamic_attr) {
789
790
791
792
793
            #if defined(PYPY_VERSION)
                pybind11_fail(std::string(rec->name) + ": dynamic attributes are "
                                                       "currently not supported in "
                                                       "conunction with PyPy!");
            #endif
Dean Moldovan's avatar
Dean Moldovan committed
794
            type->ht_type.tp_flags |= Py_TPFLAGS_HAVE_GC;
795
796
            type->ht_type.tp_dictoffset = type->ht_type.tp_basicsize; // place the dict at the end
            type->ht_type.tp_basicsize += sizeof(PyObject *); // and allocate enough space for it
Dean Moldovan's avatar
Dean Moldovan committed
797
798
799
800
801
            type->ht_type.tp_getset = generic_getset;
            type->ht_type.tp_traverse = traverse;
            type->ht_type.tp_clear = clear;
        }

Wenzel Jakob's avatar
Wenzel Jakob committed
802
803
804
805
806
807
808
809
810
        if (rec->buffer_protocol) {
            type->ht_type.tp_as_buffer = &type->as_buffer;
#if PY_MAJOR_VERSION < 3
            type->ht_type.tp_flags |= Py_TPFLAGS_HAVE_NEWBUFFER;
#endif
            type->as_buffer.bf_getbuffer = getbuffer;
            type->as_buffer.bf_releasebuffer = releasebuffer;
        }

811
        type->ht_type.tp_doc = tp_doc;
Wenzel Jakob's avatar
Wenzel Jakob committed
812

Wenzel Jakob's avatar
Wenzel Jakob committed
813
814
        m_ptr = type_holder.ptr();

Wenzel Jakob's avatar
Wenzel Jakob committed
815
        if (PyType_Ready(&type->ht_type) < 0)
816
817
            pybind11_fail(std::string(rec->name) + ": PyType_Ready failed (" +
                          detail::error_string() + ")!");
818
819

        if (scope_module) // Needed by pydoc
Wenzel Jakob's avatar
Wenzel Jakob committed
820
            attr("__module__") = scope_module;
Wenzel Jakob's avatar
Wenzel Jakob committed
821

822
        /* Register type with the parent scope */
823
824
        if (rec->scope)
            rec->scope.attr(handle(type->ht_name)) = *this;
Wenzel Jakob's avatar
Wenzel Jakob committed
825

Wenzel Jakob's avatar
Wenzel Jakob committed
826
827
828
        if (rec->multiple_inheritance)
            mark_parents_nonsimple(&type->ht_type);

829
        type_holder.release();
Wenzel Jakob's avatar
Wenzel Jakob committed
830
831
    }

Wenzel Jakob's avatar
Wenzel Jakob committed
832
833
    /// Helper function which tags all parents of a type using mult. inheritance
    void mark_parents_nonsimple(PyTypeObject *value) {
834
        auto t = reinterpret_borrow<tuple>(value->tp_bases);
Wenzel Jakob's avatar
Wenzel Jakob committed
835
836
837
838
839
840
841
842
        for (handle h : t) {
            auto tinfo2 = get_type_info((PyTypeObject *) h.ptr());
            if (tinfo2)
                tinfo2->simple_type = false;
            mark_parents_nonsimple((PyTypeObject *) h.ptr());
        }
    }

Wenzel Jakob's avatar
Wenzel Jakob committed
843
    static int init(void *self, PyObject *, PyObject *) {
Wenzel Jakob's avatar
Wenzel Jakob committed
844
845
846
847
848
849
850
        PyTypeObject *type = Py_TYPE(self);
        std::string msg;
#if defined(PYPY_VERSION)
        msg += handle((PyObject *) type).attr("__module__").cast<std::string>() + ".";
#endif
        msg += type->tp_name;
        msg += ": No constructor defined!";
Wenzel Jakob's avatar
Wenzel Jakob committed
851
852
853
854
855
        PyErr_SetString(PyExc_TypeError, msg.c_str());
        return -1;
    }

    static PyObject *new_instance(PyTypeObject *type, PyObject *, PyObject *) {
856
857
858
        instance<void> *self = (instance<void> *) PyType_GenericAlloc((PyTypeObject *) type, 0);
        auto tinfo = detail::get_type_info(type);
        self->value = ::operator new(tinfo->type_size);
Wenzel Jakob's avatar
Wenzel Jakob committed
859
        self->owned = true;
860
        self->holder_constructed = false;
861
        detail::get_internals().registered_instances.emplace(self->value, (PyObject *) self);
Wenzel Jakob's avatar
Wenzel Jakob committed
862
863
864
865
866
        return (PyObject *) self;
    }

    static void dealloc(instance<void> *self) {
        if (self->value) {
867
868
869
870
871
872
873
874
875
876
            auto instance_type = Py_TYPE(self);
            auto &registered_instances = detail::get_internals().registered_instances;
            auto range = registered_instances.equal_range(self->value);
            bool found = false;
            for (auto it = range.first; it != range.second; ++it) {
                if (instance_type == Py_TYPE(it->second)) {
                    registered_instances.erase(it);
                    found = true;
                    break;
                }
Wenzel Jakob's avatar
Wenzel Jakob committed
877
            }
878
879
880
            if (!found)
                pybind11_fail("generic_type::dealloc(): Tried to deallocate unregistered instance!");

881
882
            if (self->weakrefs)
                PyObject_ClearWeakRefs((PyObject *) self);
Dean Moldovan's avatar
Dean Moldovan committed
883

884
            PyObject **dict_ptr = _PyObject_GetDictPtr((PyObject *) self);
Wenzel Jakob's avatar
Wenzel Jakob committed
885
            if (dict_ptr)
886
                Py_CLEAR(*dict_ptr);
Wenzel Jakob's avatar
Wenzel Jakob committed
887
888
889
890
        }
        Py_TYPE(self)->tp_free((PyObject*) self);
    }

Dean Moldovan's avatar
Dean Moldovan committed
891
    static int traverse(PyObject *op, visitproc visit, void *arg) {
892
893
        PyObject *&dict = *_PyObject_GetDictPtr(op);
        Py_VISIT(dict);
Dean Moldovan's avatar
Dean Moldovan committed
894
895
896
897
        return 0;
    }

    static int clear(PyObject *op) {
898
899
        PyObject *&dict = *_PyObject_GetDictPtr(op);
        Py_CLEAR(dict);
Dean Moldovan's avatar
Dean Moldovan committed
900
901
902
        return 0;
    }

Wenzel Jakob's avatar
Wenzel Jakob committed
903
904
905
    void install_buffer_funcs(
            buffer_info *(*get_buffer)(PyObject *, void *),
            void *get_buffer_data) {
Wenzel Jakob's avatar
Wenzel Jakob committed
906
        PyHeapTypeObject *type = (PyHeapTypeObject*) m_ptr;
907
        auto tinfo = detail::get_type_info(&type->ht_type);
Wenzel Jakob's avatar
Wenzel Jakob committed
908
909
910
911
912
913
914
915

        if (!type->ht_type.tp_as_buffer)
            pybind11_fail(
                "To be able to register buffer protocol support for the type '" +
                std::string(tinfo->type->tp_name) +
                "' the associated class<>(..) invocation must "
                "include the pybind11::buffer_protocol() annotation!");

916
917
        tinfo->get_buffer = get_buffer;
        tinfo->get_buffer_data = get_buffer_data;
Wenzel Jakob's avatar
Wenzel Jakob committed
918
919
920
    }

    static int getbuffer(PyObject *obj, Py_buffer *view, int flags) {
921
922
        auto tinfo = detail::get_type_info(Py_TYPE(obj));
        if (view == nullptr || obj == nullptr || !tinfo || !tinfo->get_buffer) {
Wenzel Jakob's avatar
Wenzel Jakob committed
923
924
            if (view)
                view->obj = nullptr;
925
            PyErr_SetString(PyExc_BufferError, "generic_type::getbuffer(): Internal error");
Wenzel Jakob's avatar
Wenzel Jakob committed
926
927
928
            return -1;
        }
        memset(view, 0, sizeof(Py_buffer));
929
        buffer_info *info = tinfo->get_buffer(obj, tinfo->get_buffer_data);
Wenzel Jakob's avatar
Wenzel Jakob committed
930
931
932
933
        view->obj = obj;
        view->ndim = 1;
        view->internal = info;
        view->buf = info->ptr;
934
        view->itemsize = (ssize_t) info->itemsize;
Wenzel Jakob's avatar
Wenzel Jakob committed
935
936
937
938
939
940
        view->len = view->itemsize;
        for (auto s : info->shape)
            view->len *= s;
        if ((flags & PyBUF_FORMAT) == PyBUF_FORMAT)
            view->format = const_cast<char *>(info->format.c_str());
        if ((flags & PyBUF_STRIDES) == PyBUF_STRIDES) {
941
            view->ndim = (int) info->ndim;
942
943
            view->strides = (ssize_t *) &info->strides[0];
            view->shape = (ssize_t *) &info->shape[0];
Wenzel Jakob's avatar
Wenzel Jakob committed
944
945
946
947
948
949
        }
        Py_INCREF(view->obj);
        return 0;
    }

    static void releasebuffer(PyObject *, Py_buffer *view) { delete (buffer_info *) view->internal; }
Wenzel Jakob's avatar
Wenzel Jakob committed
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974

    void def_property_static_impl(const char *name,
                                  handle fget, handle fset,
                                  detail::function_record *rec_fget) {
        pybind11::str doc_obj = pybind11::str(
            (rec_fget->doc && pybind11::options::show_user_defined_docstrings())
                ? rec_fget->doc : "");
        const auto property = reinterpret_steal<object>(
            PyObject_CallFunctionObjArgs((PyObject *) &PyProperty_Type, fget.ptr() ? fget.ptr() : Py_None,
                                         fset.ptr() ? fset.ptr() : Py_None, Py_None, doc_obj.ptr(), nullptr));
        if (rec_fget->is_method && rec_fget->scope) {
            attr(name) = property;
        } else {
            auto mclass = handle((PyObject *) PYBIND11_OB_TYPE(*((PyTypeObject *) m_ptr)));

            if ((PyTypeObject *) mclass.ptr() == &PyType_Type)
                pybind11_fail(
                    "Adding static properties to the type '" +
                    std::string(((PyTypeObject *) m_ptr)->tp_name) +
                    "' requires the type to have a custom metaclass. Please "
                    "ensure that one is created by supplying the pybind11::metaclass() "
                    "annotation to the associated class_<>(..) invocation.");
            mclass.attr(name) = property;
        }
    }
Wenzel Jakob's avatar
Wenzel Jakob committed
975
};
976

Wenzel Jakob's avatar
Wenzel Jakob committed
977
978
NAMESPACE_END(detail)

979
template <typename type_, typename... options>
Wenzel Jakob's avatar
Wenzel Jakob committed
980
class class_ : public detail::generic_type {
981
982
    template <typename T> using is_holder = detail::is_holder_type<type_, T>;
    template <typename T> using is_subtype = detail::bool_constant<std::is_base_of<type_, T>::value && !std::is_same<T, type_>::value>;
Wenzel Jakob's avatar
Wenzel Jakob committed
983
    template <typename T> using is_base = detail::bool_constant<std::is_base_of<T, type_>::value && !std::is_same<T, type_>::value>;
984
985
986
    // struct instead of using here to help MSVC:
    template <typename T> struct is_valid_class_option :
        detail::any_of<is_holder<T>, is_subtype<T>, is_base<T>> {};
987

Wenzel Jakob's avatar
Wenzel Jakob committed
988
public:
989
    using type = type_;
990
    using type_alias = detail::first_of_t<is_subtype, void, options...>;
991
    constexpr static bool has_alias = !std::is_void<type_alias>::value;
992
    using holder_type = detail::first_of_t<is_holder, std::unique_ptr<type>, options...>;
993
994
    using instance_type = detail::instance<type, holder_type>;

995
    static_assert(detail::all_of<is_valid_class_option<options>...>::value,
996
            "Unknown/invalid class_ template parameters provided");
Wenzel Jakob's avatar
Wenzel Jakob committed
997

998
    PYBIND11_OBJECT(class_, generic_type, PyType_Check)
Wenzel Jakob's avatar
Wenzel Jakob committed
999

Wenzel Jakob's avatar
Wenzel Jakob committed
1000
1001
1002
1003
1004
1005
    template <typename... Extra>
    class_(handle scope, const char *name, const Extra &... extra) {
        detail::type_record record;
        record.scope = scope;
        record.name = name;
        record.type = &typeid(type);
1006
        record.type_size = sizeof(detail::conditional_t<has_alias, type_alias, type>);
Wenzel Jakob's avatar
Wenzel Jakob committed
1007
1008
1009
        record.instance_size = sizeof(instance_type);
        record.init_holder = init_holder;
        record.dealloc = dealloc;
1010
        record.default_holder = std::is_same<holder_type, std::unique_ptr<type>>::value;
Wenzel Jakob's avatar
Wenzel Jakob committed
1011

Wenzel Jakob's avatar
Wenzel Jakob committed
1012
        /* Register base classes specified via template arguments to class_, if any */
1013
        bool unused[] = { (add_base<options>(record), false)..., false };
Wenzel Jakob's avatar
Wenzel Jakob committed
1014
        (void) unused;
1015

Wenzel Jakob's avatar
Wenzel Jakob committed
1016
1017
1018
1019
        /* Process optional arguments, if any */
        detail::process_attributes<Extra...>::init(extra..., &record);

        detail::generic_type::initialize(&record);
1020

1021
        if (has_alias) {
1022
1023
1024
            auto &instances = pybind11::detail::get_internals().registered_types_cpp;
            instances[std::type_index(typeid(type_alias))] = instances[std::type_index(typeid(type))];
        }
Wenzel Jakob's avatar
Wenzel Jakob committed
1025
    }
Wenzel Jakob's avatar
Wenzel Jakob committed
1026

1027
    template <typename Base, detail::enable_if_t<is_base<Base>::value, int> = 0>
Wenzel Jakob's avatar
Wenzel Jakob committed
1028
1029
1030
1031
1032
1033
    static void add_base(detail::type_record &rec) {
        rec.add_base(&typeid(Base), [](void *src) -> void * {
            return static_cast<Base *>(reinterpret_cast<type *>(src));
        });
    }

1034
    template <typename Base, detail::enable_if_t<!is_base<Base>::value, int> = 0>
Wenzel Jakob's avatar
Wenzel Jakob committed
1035
1036
    static void add_base(detail::type_record &) { }

1037
    template <typename Func, typename... Extra>
Wenzel Jakob's avatar
Wenzel Jakob committed
1038
    class_ &def(const char *name_, Func&& f, const Extra&... extra) {
1039
1040
        cpp_function cf(std::forward<Func>(f), name(name_), is_method(*this),
                        sibling(getattr(*this, name_, none())), extra...);
1041
        attr(cf.name()) = cf;
Wenzel Jakob's avatar
Wenzel Jakob committed
1042
1043
1044
        return *this;
    }

1045
    template <typename Func, typename... Extra> class_ &
Wenzel Jakob's avatar
Wenzel Jakob committed
1046
    def_static(const char *name_, Func f, const Extra&... extra) {
1047
1048
        cpp_function cf(std::forward<Func>(f), name(name_), scope(*this),
                        sibling(getattr(*this, name_, none())), extra...);
1049
        attr(cf.name()) = cf;
Wenzel Jakob's avatar
Wenzel Jakob committed
1050
1051
1052
        return *this;
    }

1053
    template <detail::op_id id, detail::op_type ot, typename L, typename R, typename... Extra>
Wenzel Jakob's avatar
Wenzel Jakob committed
1054
    class_ &def(const detail::op_<id, ot, L, R> &op, const Extra&... extra) {
1055
        op.execute(*this, extra...);
Wenzel Jakob's avatar
Wenzel Jakob committed
1056
1057
1058
        return *this;
    }

1059
    template <detail::op_id id, detail::op_type ot, typename L, typename R, typename... Extra>
Wenzel Jakob's avatar
Wenzel Jakob committed
1060
    class_ & def_cast(const detail::op_<id, ot, L, R> &op, const Extra&... extra) {
1061
        op.execute_cast(*this, extra...);
Wenzel Jakob's avatar
Wenzel Jakob committed
1062
1063
1064
        return *this;
    }

1065
    template <typename... Args, typename... Extra>
Wenzel Jakob's avatar
Wenzel Jakob committed
1066
    class_ &def(const detail::init<Args...> &init, const Extra&... extra) {
1067
        init.execute(*this, extra...);
Wenzel Jakob's avatar
Wenzel Jakob committed
1068
1069
1070
        return *this;
    }

1071
1072
    template <typename... Args, typename... Extra>
    class_ &def(const detail::init_alias<Args...> &init, const Extra&... extra) {
1073
        init.execute(*this, extra...);
1074
1075
1076
        return *this;
    }

1077
    template <typename Func> class_& def_buffer(Func &&func) {
Wenzel Jakob's avatar
Wenzel Jakob committed
1078
1079
1080
        struct capture { Func func; };
        capture *ptr = new capture { std::forward<Func>(func) };
        install_buffer_funcs([](PyObject *obj, void *ptr) -> buffer_info* {
1081
            detail::make_caster<type> caster;
Wenzel Jakob's avatar
Wenzel Jakob committed
1082
1083
            if (!caster.load(obj, false))
                return nullptr;
Wenzel Jakob's avatar
Wenzel Jakob committed
1084
1085
            return new buffer_info(((capture *) ptr)->func(caster));
        }, ptr);
Wenzel Jakob's avatar
Wenzel Jakob committed
1086
1087
1088
        return *this;
    }

1089
    template <typename C, typename D, typename... Extra>
Wenzel Jakob's avatar
Wenzel Jakob committed
1090
    class_ &def_readwrite(const char *name, D C::*pm, const Extra&... extra) {
1091
1092
1093
        cpp_function fget([pm](const C &c) -> const D &{ return c.*pm; }, is_method(*this)),
                     fset([pm](C &c, const D &value) { c.*pm = value; }, is_method(*this));
        def_property(name, fget, fset, return_value_policy::reference_internal, extra...);
Wenzel Jakob's avatar
Wenzel Jakob committed
1094
1095
1096
        return *this;
    }

1097
    template <typename C, typename D, typename... Extra>
Wenzel Jakob's avatar
Wenzel Jakob committed
1098
    class_ &def_readonly(const char *name, const D C::*pm, const Extra& ...extra) {
1099
1100
        cpp_function fget([pm](const C &c) -> const D &{ return c.*pm; }, is_method(*this));
        def_property_readonly(name, fget, return_value_policy::reference_internal, extra...);
Wenzel Jakob's avatar
Wenzel Jakob committed
1101
1102
1103
        return *this;
    }

1104
    template <typename D, typename... Extra>
Wenzel Jakob's avatar
Wenzel Jakob committed
1105
    class_ &def_readwrite_static(const char *name, D *pm, const Extra& ...extra) {
1106
1107
1108
        cpp_function fget([pm](object) -> const D &{ return *pm; }, scope(*this)),
                     fset([pm](object, const D &value) { *pm = value; }, scope(*this));
        def_property_static(name, fget, fset, return_value_policy::reference, extra...);
Wenzel Jakob's avatar
Wenzel Jakob committed
1109
1110
1111
        return *this;
    }

1112
    template <typename D, typename... Extra>
Wenzel Jakob's avatar
Wenzel Jakob committed
1113
    class_ &def_readonly_static(const char *name, const D *pm, const Extra& ...extra) {
1114
1115
        cpp_function fget([pm](object) -> const D &{ return *pm; }, scope(*this));
        def_property_readonly_static(name, fget, return_value_policy::reference, extra...);
Wenzel Jakob's avatar
Wenzel Jakob committed
1116
1117
1118
        return *this;
    }

1119
1120
1121
1122
1123
1124
1125
    /// Uses return_value_policy::reference_internal by default
    template <typename Getter, typename... Extra>
    class_ &def_property_readonly(const char *name, const Getter &fget, const Extra& ...extra) {
        return def_property_readonly(name, cpp_function(fget), return_value_policy::reference_internal, extra...);
    }

    /// Uses cpp_function's return_value_policy by default
1126
1127
    template <typename... Extra>
    class_ &def_property_readonly(const char *name, const cpp_function &fget, const Extra& ...extra) {
1128
1129
1130
1131
1132
1133
1134
        return def_property(name, fget, cpp_function(), extra...);
    }

    /// Uses return_value_policy::reference by default
    template <typename Getter, typename... Extra>
    class_ &def_property_readonly_static(const char *name, const Getter &fget, const Extra& ...extra) {
        return def_property_readonly_static(name, cpp_function(fget), return_value_policy::reference, extra...);
Wenzel Jakob's avatar
Wenzel Jakob committed
1135
1136
    }

1137
    /// Uses cpp_function's return_value_policy by default
1138
1139
    template <typename... Extra>
    class_ &def_property_readonly_static(const char *name, const cpp_function &fget, const Extra& ...extra) {
1140
1141
1142
1143
1144
1145
1146
        return def_property_static(name, fget, cpp_function(), extra...);
    }

    /// Uses return_value_policy::reference_internal by default
    template <typename Getter, typename... Extra>
    class_ &def_property(const char *name, const Getter &fget, const cpp_function &fset, const Extra& ...extra) {
        return def_property(name, cpp_function(fget), fset, return_value_policy::reference_internal, extra...);
Wenzel Jakob's avatar
Wenzel Jakob committed
1147
1148
    }

1149
    /// Uses cpp_function's return_value_policy by default
1150
1151
    template <typename... Extra>
    class_ &def_property(const char *name, const cpp_function &fget, const cpp_function &fset, const Extra& ...extra) {
1152
        return def_property_static(name, fget, fset, is_method(*this), extra...);
Wenzel Jakob's avatar
Wenzel Jakob committed
1153
1154
    }

1155
1156
1157
1158
1159
1160
1161
    /// Uses return_value_policy::reference by default
    template <typename Getter, typename... Extra>
    class_ &def_property_static(const char *name, const Getter &fget, const cpp_function &fset, const Extra& ...extra) {
        return def_property_static(name, cpp_function(fget), fset, return_value_policy::reference, extra...);
    }

    /// Uses cpp_function's return_value_policy by default
1162
1163
1164
    template <typename... Extra>
    class_ &def_property_static(const char *name, const cpp_function &fget, const cpp_function &fset, const Extra& ...extra) {
        auto rec_fget = get_function_record(fget), rec_fset = get_function_record(fset);
1165
        char *doc_prev = rec_fget->doc; /* 'extra' field may include a property-specific documentation string */
1166
        detail::process_attributes<Extra...>::init(extra..., rec_fget);
1167
1168
1169
1170
1171
1172
        if (rec_fget->doc && rec_fget->doc != doc_prev) {
            free(doc_prev);
            rec_fget->doc = strdup(rec_fget->doc);
        }
        if (rec_fset) {
            doc_prev = rec_fset->doc;
1173
            detail::process_attributes<Extra...>::init(extra..., rec_fset);
1174
1175
1176
1177
1178
            if (rec_fset->doc && rec_fset->doc != doc_prev) {
                free(doc_prev);
                rec_fset->doc = strdup(rec_fset->doc);
            }
        }
Wenzel Jakob's avatar
Wenzel Jakob committed
1179
        def_property_static_impl(name, fget, fset, rec_fget);
Wenzel Jakob's avatar
Wenzel Jakob committed
1180
1181
        return *this;
    }
1182

Wenzel Jakob's avatar
Wenzel Jakob committed
1183
private:
1184
1185
1186
    /// Initialize holder object, variant 1: object derives from enable_shared_from_this
    template <typename T>
    static void init_holder_helper(instance_type *inst, const holder_type * /* unused */, const std::enable_shared_from_this<T> * /* dummy */) {
1187
        try {
1188
            new (&inst->holder) holder_type(std::static_pointer_cast<typename holder_type::element_type>(inst->value->shared_from_this()));
1189
            inst->holder_constructed = true;
1190
        } catch (const std::bad_weak_ptr &) {
1191
1192
1193
1194
            if (inst->owned) {
                new (&inst->holder) holder_type(inst->value);
                inst->holder_constructed = true;
            }
1195
        }
1196
1197
1198
1199
    }

    /// Initialize holder object, variant 2: try to construct from existing holder object, if possible
    template <typename T = holder_type,
1200
              detail::enable_if_t<std::is_copy_constructible<T>::value, int> = 0>
1201
    static void init_holder_helper(instance_type *inst, const holder_type *holder_ptr, const void * /* dummy */) {
1202
        if (holder_ptr) {
1203
            new (&inst->holder) holder_type(*holder_ptr);
1204
            inst->holder_constructed = true;
1205
        } else if (inst->owned || detail::always_construct_holder<holder_type>::value) {
1206
            new (&inst->holder) holder_type(inst->value);
1207
1208
            inst->holder_constructed = true;
        }
1209
1210
1211
1212
    }

    /// Initialize holder object, variant 3: holder is not copy constructible (e.g. unique_ptr), always initialize from raw pointer
    template <typename T = holder_type,
1213
              detail::enable_if_t<!std::is_copy_constructible<T>::value, int> = 0>
1214
    static void init_holder_helper(instance_type *inst, const holder_type * /* unused */, const void * /* dummy */) {
1215
        if (inst->owned || detail::always_construct_holder<holder_type>::value) {
1216
1217
1218
            new (&inst->holder) holder_type(inst->value);
            inst->holder_constructed = true;
        }
1219
1220
1221
1222
1223
1224
    }

    /// Initialize holder object of an instance, possibly given a pointer to an existing holder
    static void init_holder(PyObject *inst_, const void *holder_ptr) {
        auto inst = (instance_type *) inst_;
        init_holder_helper(inst, (const holder_type *) holder_ptr, inst->value);
1225
1226
    }

Wenzel Jakob's avatar
Wenzel Jakob committed
1227
1228
    static void dealloc(PyObject *inst_) {
        instance_type *inst = (instance_type *) inst_;
1229
1230
1231
1232
1233
        if (inst->holder_constructed)
            inst->holder.~holder_type();
        else if (inst->owned)
            ::operator delete(inst->value);

1234
        generic_type::dealloc((detail::instance<void> *) inst);
Wenzel Jakob's avatar
Wenzel Jakob committed
1235
    }
1236
1237
1238

    static detail::function_record *get_function_record(handle h) {
        h = detail::get_function(h);
Wenzel Jakob's avatar
Wenzel Jakob committed
1239
        return h ? (detail::function_record *) reinterpret_borrow<capsule>(PyCFunction_GET_SELF(h.ptr()))
1240
                 : nullptr;
1241
    }
Wenzel Jakob's avatar
Wenzel Jakob committed
1242
1243
1244
1245
1246
};

/// Binds C++ enumerations and enumeration classes to Python
template <typename Type> class enum_ : public class_<Type> {
public:
1247
    using class_<Type>::def;
1248
1249
1250
    using Scalar = typename std::underlying_type<Type>::type;
    template <typename T> using arithmetic_tag = std::is_same<T, arithmetic>;

Wenzel Jakob's avatar
Wenzel Jakob committed
1251
1252
1253
    template <typename... Extra>
    enum_(const handle &scope, const char *name, const Extra&... extra)
      : class_<Type>(scope, name, extra...), m_parent(scope) {
1254
1255
1256
1257
1258
1259

        constexpr bool is_arithmetic =
            !std::is_same<detail::first_of_t<arithmetic_tag, void, Extra...>,
                          void>::value;

        auto entries = new std::unordered_map<Scalar, const char *>();
1260
        def("__repr__", [name, entries](Type value) -> std::string {
1261
            auto it = entries->find((Scalar) value);
Wenzel Jakob's avatar
Wenzel Jakob committed
1262
1263
1264
1265
            return std::string(name) + "." +
                ((it == entries->end()) ? std::string("???")
                                        : std::string(it->second));
        });
1266
1267
1268
        def("__init__", [](Type& value, Scalar i) { value = (Type)i; });
        def("__init__", [](Type& value, Scalar i) { new (&value) Type((Type) i); });
        def("__int__", [](Type value) { return (Scalar) value; });
1269
1270
        def("__eq__", [](const Type &value, Type *value2) { return value2 && value == *value2; });
        def("__ne__", [](const Type &value, Type *value2) { return !value2 || value != *value2; });
1271
1272
1273
1274
1275
1276
1277
        if (is_arithmetic) {
            def("__lt__", [](const Type &value, Type *value2) { return value2 && value < *value2; });
            def("__gt__", [](const Type &value, Type *value2) { return value2 && value > *value2; });
            def("__le__", [](const Type &value, Type *value2) { return value2 && value <= *value2; });
            def("__ge__", [](const Type &value, Type *value2) { return value2 && value >= *value2; });
        }
        if (std::is_convertible<Type, Scalar>::value) {
1278
1279
            // Don't provide comparison with the underlying type if the enum isn't convertible,
            // i.e. if Type is a scoped enum, mirroring the C++ behaviour.  (NB: we explicitly
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
            // convert Type to Scalar below anyway because this needs to compile).
            def("__eq__", [](const Type &value, Scalar value2) { return (Scalar) value == value2; });
            def("__ne__", [](const Type &value, Scalar value2) { return (Scalar) value != value2; });
            if (is_arithmetic) {
                def("__lt__", [](const Type &value, Scalar value2) { return (Scalar) value < value2; });
                def("__gt__", [](const Type &value, Scalar value2) { return (Scalar) value > value2; });
                def("__le__", [](const Type &value, Scalar value2) { return (Scalar) value <= value2; });
                def("__ge__", [](const Type &value, Scalar value2) { return (Scalar) value >= value2; });
                def("__invert__", [](const Type &value) { return ~((Scalar) value); });
                def("__and__", [](const Type &value, Scalar value2) { return (Scalar) value & value2; });
                def("__or__", [](const Type &value, Scalar value2) { return (Scalar) value | value2; });
                def("__xor__", [](const Type &value, Scalar value2) { return (Scalar) value ^ value2; });
                def("__rand__", [](const Type &value, Scalar value2) { return (Scalar) value & value2; });
                def("__ror__", [](const Type &value, Scalar value2) { return (Scalar) value | value2; });
                def("__rxor__", [](const Type &value, Scalar value2) { return (Scalar) value ^ value2; });
                def("__and__", [](const Type &value, const Type &value2) { return (Scalar) value & (Scalar) value2; });
                def("__or__", [](const Type &value, const Type &value2) { return (Scalar) value | (Scalar) value2; });
                def("__xor__", [](const Type &value, const Type &value2) { return (Scalar) value ^ (Scalar) value2; });
            }
1299
        }
1300
        def("__hash__", [](const Type &value) { return (Scalar) value; });
Wenzel Jakob's avatar
Wenzel Jakob committed
1301
        // Pickling and unpickling -- needed for use with the 'multiprocessing' module
1302
1303
        def("__getstate__", [](const Type &value) { return pybind11::make_tuple((Scalar) value); });
        def("__setstate__", [](Type &p, tuple t) { new (&p) Type((Type) t[0].cast<Scalar>()); });
Wenzel Jakob's avatar
Wenzel Jakob committed
1304
1305
1306
1307
        m_entries = entries;
    }

    /// Export enumeration entries into the parent scope
1308
    enum_ &export_values() {
Wenzel Jakob's avatar
Wenzel Jakob committed
1309
#if !defined(PYPY_VERSION)
Wenzel Jakob's avatar
Wenzel Jakob committed
1310
1311
        PyObject *dict = ((PyTypeObject *) this->m_ptr)->tp_dict;
        PyObject *key, *value;
1312
        ssize_t pos = 0;
Wenzel Jakob's avatar
Wenzel Jakob committed
1313
1314

        while (PyDict_Next(dict, &pos, &key, &value)) {
Wenzel Jakob's avatar
Wenzel Jakob committed
1315
1316
            if (PyObject_IsInstance(value, this->m_ptr))
                m_parent.attr(key) = value;
Wenzel Jakob's avatar
Wenzel Jakob committed
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
        }
#else
        /* PyPy's cpyext still has difficulties with the above
           CPython API calls; emulate using Python code. */
        dict d; d["t"] = *this; d["p"] = m_parent;
        PyObject *result = PyRun_String(
            "for k, v in t.__dict__.items():\n"
            "    if isinstance(v, t):\n"
            "        setattr(p, k, v)\n",
            Py_file_input, d.ptr(), d.ptr());
        if (result == nullptr)
            throw error_already_set();
        Py_DECREF(result);
#endif

1332
        return *this;
Wenzel Jakob's avatar
Wenzel Jakob committed
1333
1334
1335
1336
    }

    /// Add an enumeration entry
    enum_& value(char const* name, Type value) {
1337
        this->attr(name) = pybind11::cast(value, return_value_policy::copy);
1338
        (*m_entries)[(Scalar) value] = name;
Wenzel Jakob's avatar
Wenzel Jakob committed
1339
1340
1341
        return *this;
    }
private:
1342
    std::unordered_map<Scalar, const char *> *m_entries;
Wenzel Jakob's avatar
Wenzel Jakob committed
1343
    handle m_parent;
Wenzel Jakob's avatar
Wenzel Jakob committed
1344
1345
1346
};

NAMESPACE_BEGIN(detail)
1347
template <typename... Args> struct init {
1348
1349
    template <typename Class, typename... Extra, enable_if_t<!Class::has_alias, int> = 0>
    static void execute(Class &cl, const Extra&... extra) {
1350
        using Base = typename Class::type;
Wenzel Jakob's avatar
Wenzel Jakob committed
1351
        /// Function which calls a specific C++ in-place constructor
1352
        cl.def("__init__", [](Base *self_, Args... args) { new (self_) Base(args...); }, extra...);
1353
1354
    }

1355
    template <typename Class, typename... Extra,
1356
1357
1358
              enable_if_t<Class::has_alias &&
                          std::is_constructible<typename Class::type, Args...>::value, int> = 0>
    static void execute(Class &cl, const Extra&... extra) {
1359
1360
1361
1362
        using Base = typename Class::type;
        using Alias = typename Class::type_alias;
        handle cl_type = cl;
        cl.def("__init__", [cl_type](handle self_, Args... args) {
1363
1364
                if (self_.get_type() == cl_type)
                    new (self_.cast<Base *>()) Base(args...);
1365
                else
1366
                    new (self_.cast<Alias *>()) Alias(args...);
1367
1368
1369
            }, extra...);
    }

1370
    template <typename Class, typename... Extra,
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
              enable_if_t<Class::has_alias &&
                          !std::is_constructible<typename Class::type, Args...>::value, int> = 0>
    static void execute(Class &cl, const Extra&... extra) {
        init_alias<Args...>::execute(cl, extra...);
    }
};
template <typename... Args> struct init_alias {
    template <typename Class, typename... Extra,
              enable_if_t<Class::has_alias && std::is_constructible<typename Class::type_alias, Args...>::value, int> = 0>
    static void execute(Class &cl, const Extra&... extra) {
1381
1382
        using Alias = typename Class::type_alias;
        cl.def("__init__", [](Alias *self_, Args... args) { new (self_) Alias(args...); }, extra...);
Wenzel Jakob's avatar
Wenzel Jakob committed
1383
1384
    }
};
1385

1386

1387
inline void keep_alive_impl(handle nurse, handle patient) {
1388
    /* Clever approach based on weak references taken from Boost.Python */
1389
    if (!nurse || !patient)
1390
        pybind11_fail("Could not activate keep_alive!");
1391

1392
    if (patient.is_none() || nurse.is_none())
1393
        return; /* Nothing to keep alive or nothing to be kept alive by */
1394

1395
    cpp_function disable_lifesupport(
1396
        [patient](handle weakref) { patient.dec_ref(); weakref.dec_ref(); });
1397

1398
    weakref wr(nurse, disable_lifesupport);
1399

1400
1401
    patient.inc_ref(); /* reference patient and leak the weak reference */
    (void) wr.release();
1402
1403
}

1404
1405
1406
1407
1408
1409
1410
PYBIND11_NOINLINE inline void keep_alive_impl(int Nurse, int Patient, handle args, handle ret) {
    handle nurse  (Nurse   > 0 ? PyTuple_GetItem(args.ptr(), Nurse   - 1) : ret.ptr());
    handle patient(Patient > 0 ? PyTuple_GetItem(args.ptr(), Patient - 1) : ret.ptr());

    keep_alive_impl(nurse, patient);
}

1411
template <typename Iterator, typename Sentinel, bool KeyIterator, return_value_policy Policy>
1412
1413
1414
struct iterator_state {
    Iterator it;
    Sentinel end;
1415
1416
    bool first;
};
1417

Wenzel Jakob's avatar
Wenzel Jakob committed
1418
1419
NAMESPACE_END(detail)

1420
template <typename... Args> detail::init<Args...> init() { return detail::init<Args...>(); }
1421
template <typename... Args> detail::init_alias<Args...> init_alias() { return detail::init_alias<Args...>(); }
Wenzel Jakob's avatar
Wenzel Jakob committed
1422

1423
1424
template <return_value_policy Policy = return_value_policy::reference_internal,
          typename Iterator,
1425
          typename Sentinel,
1426
1427
          typename ValueType = decltype(*std::declval<Iterator>()),
          typename... Extra>
1428
iterator make_iterator(Iterator first, Sentinel last, Extra &&... extra) {
1429
    typedef detail::iterator_state<Iterator, Sentinel, false, Policy> state;
1430

Wenzel Jakob's avatar
Wenzel Jakob committed
1431
    if (!detail::get_type_info(typeid(state), false)) {
1432
        class_<state>(handle(), "iterator")
1433
            .def("__iter__", [](state &s) -> state& { return s; })
1434
            .def("__next__", [](state &s) -> ValueType {
1435
1436
1437
1438
                if (!s.first)
                    ++s.it;
                else
                    s.first = false;
1439
1440
                if (s.it == s.end)
                    throw stop_iteration();
1441
                return *s.it;
1442
            }, std::forward<Extra>(extra)..., Policy);
1443
1444
    }

1445
    return (iterator) cast(state { first, last, true });
1446
}
1447

1448
1449
template <return_value_policy Policy = return_value_policy::reference_internal,
          typename Iterator,
1450
          typename Sentinel,
1451
          typename KeyType = decltype((*std::declval<Iterator>()).first),
1452
          typename... Extra>
1453
iterator make_key_iterator(Iterator first, Sentinel last, Extra &&... extra) {
1454
    typedef detail::iterator_state<Iterator, Sentinel, true, Policy> state;
1455

Wenzel Jakob's avatar
Wenzel Jakob committed
1456
    if (!detail::get_type_info(typeid(state), false)) {
1457
        class_<state>(handle(), "iterator")
1458
1459
1460
1461
1462
1463
1464
1465
            .def("__iter__", [](state &s) -> state& { return s; })
            .def("__next__", [](state &s) -> KeyType {
                if (!s.first)
                    ++s.it;
                else
                    s.first = false;
                if (s.it == s.end)
                    throw stop_iteration();
1466
                return (*s.it).first;
1467
            }, std::forward<Extra>(extra)..., Policy);
1468
1469
1470
1471
    }

    return (iterator) cast(state { first, last, true });
}
1472

1473
1474
1475
template <return_value_policy Policy = return_value_policy::reference_internal,
          typename Type, typename... Extra> iterator make_iterator(Type &value, Extra&&... extra) {
    return make_iterator<Policy>(std::begin(value), std::end(value), extra...);
1476
1477
}

1478
1479
1480
template <return_value_policy Policy = return_value_policy::reference_internal,
          typename Type, typename... Extra> iterator make_key_iterator(Type &value, Extra&&... extra) {
    return make_key_iterator<Policy>(std::begin(value), std::end(value), extra...);
1481
1482
}

Wenzel Jakob's avatar
Wenzel Jakob committed
1483
template <typename InputType, typename OutputType> void implicitly_convertible() {
1484
    auto implicit_caster = [](PyObject *obj, PyTypeObject *type) -> PyObject * {
1485
        if (!detail::make_caster<InputType>().load(obj, false))
Wenzel Jakob's avatar
Wenzel Jakob committed
1486
1487
1488
1489
1490
1491
1492
1493
            return nullptr;
        tuple args(1);
        args[0] = obj;
        PyObject *result = PyObject_Call((PyObject *) type, args.ptr(), nullptr);
        if (result == nullptr)
            PyErr_Clear();
        return result;
    };
1494
1495
1496
1497

    if (auto tinfo = detail::get_type_info(typeid(OutputType)))
        tinfo->implicit_conversions.push_back(implicit_caster);
    else
1498
        pybind11_fail("implicitly_convertible: Unable to find type " + type_id<OutputType>());
Wenzel Jakob's avatar
Wenzel Jakob committed
1499
1500
}

1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
template <typename ExceptionTranslator>
void register_exception_translator(ExceptionTranslator&& translator) {
    detail::get_internals().registered_exception_translators.push_front(
        std::forward<ExceptionTranslator>(translator));
}

/* Wrapper to generate a new Python exception type.
 *
 * This should only be used with PyErr_SetString for now.
 * It is not (yet) possible to use as a py::base.
 * Template type argument is reserved for future use.
 */
template <typename type>
class exception : public object {
public:
1516
1517
1518
1519
1520
1521
1522
1523
    exception(handle scope, const char *name, PyObject *base = PyExc_Exception) {
        std::string full_name = scope.attr("__name__").cast<std::string>() +
                                std::string(".") + name;
        m_ptr = PyErr_NewException((char *) full_name.c_str(), base, NULL);
        if (hasattr(scope, name))
            pybind11_fail("Error during initialization: multiple incompatible "
                          "definitions with name \"" + std::string(name) + "\"");
        scope.attr(name) = *this;
1524
    }
1525
1526
1527
1528
1529

    // Sets the current python exception to this exception object with the given message
    void operator()(const char *message) {
        PyErr_SetString(m_ptr, message);
    }
1530
1531
};

1532
1533
1534
1535
1536
/** Registers a Python exception in `m` of the given `name` and installs an exception translator to
 * translate the C++ exception to the created Python exception using the exceptions what() method.
 * This is intended for simple exception translations; for more complex translation, register the
 * exception object and translator directly.
 */
1537
1538
1539
1540
1541
template <typename CppException>
exception<CppException> &register_exception(handle scope,
                                            const char *name,
                                            PyObject *base = PyExc_Exception) {
    static exception<CppException> ex(scope, name, base);
1542
1543
1544
1545
    register_exception_translator([](std::exception_ptr p) {
        if (!p) return;
        try {
            std::rethrow_exception(p);
1546
        } catch (const CppException &e) {
1547
1548
1549
1550
1551
1552
            ex(e.what());
        }
    });
    return ex;
}

Dean Moldovan's avatar
Dean Moldovan committed
1553
1554
1555
1556
NAMESPACE_BEGIN(detail)
PYBIND11_NOINLINE inline void print(tuple args, dict kwargs) {
    auto strings = tuple(args.size());
    for (size_t i = 0; i < args.size(); ++i) {
1557
        strings[i] = str(args[i]);
Dean Moldovan's avatar
Dean Moldovan committed
1558
    }
1559
    auto sep = kwargs.contains("sep") ? kwargs["sep"] : cast(" ");
1560
    auto line = sep.attr("join")(strings);
Dean Moldovan's avatar
Dean Moldovan committed
1561

1562
1563
1564
1565
1566
1567
    object file;
    if (kwargs.contains("file")) {
        file = kwargs["file"].cast<object>();
    } else {
        try {
            file = module::import("sys").attr("stdout");
1568
        } catch (const error_already_set &) {
1569
1570
1571
1572
1573
1574
1575
1576
            /* If print() is called from code that is executed as
               part of garbage collection during interpreter shutdown,
               importing 'sys' can fail. Give up rather than crashing the
               interpreter in this case. */
            return;
        }
    }

1577
    auto write = file.attr("write");
Dean Moldovan's avatar
Dean Moldovan committed
1578
    write(line);
1579
    write(kwargs.contains("end") ? kwargs["end"] : cast("\n"));
Dean Moldovan's avatar
Dean Moldovan committed
1580

1581
    if (kwargs.contains("flush") && kwargs["flush"].cast<bool>())
1582
        file.attr("flush")();
Dean Moldovan's avatar
Dean Moldovan committed
1583
1584
1585
1586
1587
1588
1589
1590
1591
}
NAMESPACE_END(detail)

template <return_value_policy policy = return_value_policy::automatic_reference, typename... Args>
void print(Args &&...args) {
    auto c = detail::collect_arguments<policy>(std::forward<Args>(args)...);
    detail::print(c.args(), c.kwargs());
}

Wenzel Jakob's avatar
Wenzel Jakob committed
1592
#if defined(WITH_THREAD) && !defined(PYPY_VERSION)
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606

/* The functions below essentially reproduce the PyGILState_* API using a RAII
 * pattern, but there are a few important differences:
 *
 * 1. When acquiring the GIL from an non-main thread during the finalization
 *    phase, the GILState API blindly terminates the calling thread, which
 *    is often not what is wanted. This API does not do this.
 *
 * 2. The gil_scoped_release function can optionally cut the relationship
 *    of a PyThreadState and its associated thread, which allows moving it to
 *    another thread (this is a fairly rare/advanced use case).
 *
 * 3. The reference count of an acquired thread state can be controlled. This
 *    can be handy to prevent cases where callbacks issued from an external
1607
1608
 *    thread would otherwise constantly construct and destroy thread state data
 *    structures.
1609
1610
1611
1612
1613
 *
 * See the Python bindings of NanoGUI (http://github.com/wjakob/nanogui) for an
 * example which uses features 2 and 3 to migrate the Python thread of
 * execution to another thread (to run the event loop on the original thread,
 * in this case).
1614
 */
Wenzel Jakob's avatar
Wenzel Jakob committed
1615
1616
1617

class gil_scoped_acquire {
public:
1618
    PYBIND11_NOINLINE gil_scoped_acquire() {
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
        auto const &internals = detail::get_internals();
        tstate = (PyThreadState *) PyThread_get_key_value(internals.tstate);

        if (!tstate) {
            tstate = PyThreadState_New(internals.istate);
            #if !defined(NDEBUG)
                if (!tstate)
                    pybind11_fail("scoped_acquire: could not create thread state!");
            #endif
            tstate->gilstate_counter = 0;
1629
1630
1631
            #if PY_MAJOR_VERSION < 3
                PyThread_delete_key_value(internals.tstate);
            #endif
1632
1633
            PyThread_set_key_value(internals.tstate, tstate);
        } else {
1634
            release = detail::get_thread_state_unchecked() != tstate;
1635
1636
1637
1638
        }

        if (release) {
            /* Work around an annoying assertion in PyThreadState_Swap */
1639
1640
1641
1642
            #if defined(Py_DEBUG)
                PyInterpreterState *interp = tstate->interp;
                tstate->interp = nullptr;
            #endif
1643
            PyEval_AcquireThread(tstate);
1644
1645
1646
            #if defined(Py_DEBUG)
                tstate->interp = interp;
            #endif
1647
1648
1649
1650
1651
1652
1653
1654
1655
        }

        inc_ref();
    }

    void inc_ref() {
        ++tstate->gilstate_counter;
    }

1656
    PYBIND11_NOINLINE void dec_ref() {
1657
1658
        --tstate->gilstate_counter;
        #if !defined(NDEBUG)
1659
            if (detail::get_thread_state_unchecked() != tstate)
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
                pybind11_fail("scoped_acquire::dec_ref(): thread state must be current!");
            if (tstate->gilstate_counter < 0)
                pybind11_fail("scoped_acquire::dec_ref(): reference count underflow!");
        #endif
        if (tstate->gilstate_counter == 0) {
            #if !defined(NDEBUG)
                if (!release)
                    pybind11_fail("scoped_acquire::dec_ref(): internal error!");
            #endif
            PyThreadState_Clear(tstate);
            PyThreadState_DeleteCurrent();
1671
            PyThread_delete_key_value(detail::get_internals().tstate);
1672
1673
1674
1675
            release = false;
        }
    }

1676
    PYBIND11_NOINLINE ~gil_scoped_acquire() {
1677
1678
1679
1680
1681
1682
1683
        dec_ref();
        if (release)
           PyEval_SaveThread();
    }
private:
    PyThreadState *tstate = nullptr;
    bool release = true;
Wenzel Jakob's avatar
Wenzel Jakob committed
1684
1685
1686
1687
};

class gil_scoped_release {
public:
1688
    explicit gil_scoped_release(bool disassoc = false) : disassoc(disassoc) {
1689
        tstate = PyEval_SaveThread();
1690
        if (disassoc) {
1691
            auto key = detail::get_internals().tstate;
1692
1693
1694
1695
1696
1697
            #if PY_MAJOR_VERSION < 3
                PyThread_delete_key_value(key);
            #else
                PyThread_set_key_value(key, nullptr);
            #endif
        }
1698
1699
1700
1701
1702
    }
    ~gil_scoped_release() {
        if (!tstate)
            return;
        PyEval_RestoreThread(tstate);
1703
        if (disassoc) {
1704
            auto key = detail::get_internals().tstate;
1705
1706
1707
1708
1709
            #if PY_MAJOR_VERSION < 3
                PyThread_delete_key_value(key);
            #endif
            PyThread_set_key_value(key, tstate);
        }
1710
1711
1712
1713
    }
private:
    PyThreadState *tstate;
    bool disassoc;
Wenzel Jakob's avatar
Wenzel Jakob committed
1714
};
Wenzel Jakob's avatar
Wenzel Jakob committed
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
#elif defined(PYPY_VERSION)
class gil_scoped_acquire {
    PyGILState_STATE state;
public:
    gil_scoped_acquire() { state = PyGILState_Ensure(); }
    ~gil_scoped_acquire() { PyGILState_Release(state); }
};

class gil_scoped_release {
    PyThreadState *state;
public:
    gil_scoped_release() { state = PyEval_SaveThread(); }
    ~gil_scoped_release() { PyEval_RestoreThread(state); }
};
1729
1730
1731
#else
class gil_scoped_acquire { };
class gil_scoped_release { };
1732
#endif
Wenzel Jakob's avatar
Wenzel Jakob committed
1733

1734
1735
1736
1737
1738
1739
1740
1741
error_already_set::~error_already_set() {
    if (value) {
        gil_scoped_acquire gil;
        PyErr_Restore(type, value, trace);
        PyErr_Clear();
    }
}

1742
inline function get_type_overload(const void *this_ptr, const detail::type_info *this_type, const char *name)  {
Wenzel Jakob's avatar
Wenzel Jakob committed
1743
1744
    handle self = detail::get_object_handle(this_ptr, this_type);
    if (!self)
1745
        return function();
Wenzel Jakob's avatar
Wenzel Jakob committed
1746
    handle type = self.get_type();
1747
1748
    auto key = std::make_pair(type.ptr(), name);

1749
1750
    /* Cache functions that aren't overloaded in Python to avoid
       many costly Python dictionary lookups below */
1751
1752
1753
1754
    auto &cache = detail::get_internals().inactive_overload_cache;
    if (cache.find(key) != cache.end())
        return function();

Wenzel Jakob's avatar
Wenzel Jakob committed
1755
    function overload = getattr(self, name, function());
1756
1757
1758
1759
    if (overload.is_cpp_function()) {
        cache.insert(key);
        return function();
    }
1760

Wenzel Jakob's avatar
Wenzel Jakob committed
1761
1762
1763
    /* Don't call dispatch code if invoked from overridden function.
       Unfortunately this doesn't work on PyPy. */
#if !defined(PYPY_VERSION)
1764
    PyFrameObject *frame = PyThreadState_Get()->frame;
1765
    if (frame && (std::string) str(frame->f_code->co_name) == name &&
1766
1767
1768
1769
        frame->f_code->co_argcount > 0) {
        PyFrame_FastToLocals(frame);
        PyObject *self_caller = PyDict_GetItem(
            frame->f_locals, PyTuple_GET_ITEM(frame->f_code->co_varnames, 0));
Wenzel Jakob's avatar
Wenzel Jakob committed
1770
        if (self_caller == self.ptr())
1771
1772
            return function();
    }
Wenzel Jakob's avatar
Wenzel Jakob committed
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
#else
    /* PyPy currently doesn't provide a detailed cpyext emulation of
       frame objects, so we have to emulate this using Python. This
       is going to be slow..*/
    dict d; d["self"] = self; d["name"] = pybind11::str(name);
    PyObject *result = PyRun_String(
        "import inspect\n"
        "frame = inspect.currentframe()\n"
        "if frame is not None:\n"
        "    frame = frame.f_back\n"
        "    if frame is not None and str(frame.f_code.co_name) == name and "
        "frame.f_code.co_argcount > 0:\n"
        "        self_caller = frame.f_locals[frame.f_code.co_varnames[0]]\n"
        "        if self_caller == self:\n"
        "            self = None\n",
        Py_file_input, d.ptr(), d.ptr());
    if (result == nullptr)
        throw error_already_set();
    if ((handle) d["self"] == Py_None)
        return function();
    Py_DECREF(result);
#endif

1796
1797
1798
    return overload;
}

1799
template <class T> function get_overload(const T *this_ptr, const char *name) {
1800
1801
    auto tinfo = detail::get_type_info(typeid(T));
    return tinfo ? get_type_overload(this_ptr, tinfo, name) : function();
1802
1803
}

1804
#define PYBIND11_OVERLOAD_INT(ret_type, cname, name, ...) { \
1805
        pybind11::gil_scoped_acquire gil; \
1806
        pybind11::function overload = pybind11::get_overload(static_cast<const cname *>(this), name); \
1807
        if (overload) { \
1808
            auto o = overload(__VA_ARGS__); \
1809
            if (pybind11::detail::cast_is_temporary_value_reference<ret_type>::value) { \
1810
1811
                static pybind11::detail::overload_caster_t<ret_type> caster; \
                return pybind11::detail::cast_ref<ret_type>(std::move(o), caster); \
1812
1813
1814
1815
            } \
            else return pybind11::detail::cast_safe<ret_type>(std::move(o)); \
        } \
    }
1816

1817
#define PYBIND11_OVERLOAD_NAME(ret_type, cname, name, fn, ...) \
1818
    PYBIND11_OVERLOAD_INT(ret_type, cname, name, __VA_ARGS__) \
1819
    return cname::fn(__VA_ARGS__)
1820

1821
#define PYBIND11_OVERLOAD_PURE_NAME(ret_type, cname, name, fn, ...) \
1822
    PYBIND11_OVERLOAD_INT(ret_type, cname, name, __VA_ARGS__) \
1823
1824
1825
1826
1827
1828
1829
    pybind11::pybind11_fail("Tried to call pure virtual function \"" #cname "::" name "\"");

#define PYBIND11_OVERLOAD(ret_type, cname, fn, ...) \
    PYBIND11_OVERLOAD_NAME(ret_type, cname, #fn, fn, __VA_ARGS__)

#define PYBIND11_OVERLOAD_PURE(ret_type, cname, fn, ...) \
    PYBIND11_OVERLOAD_PURE_NAME(ret_type, cname, #fn, fn, __VA_ARGS__)
1830

1831
NAMESPACE_END(pybind11)
Wenzel Jakob's avatar
Wenzel Jakob committed
1832
1833

#if defined(_MSC_VER)
Wenzel Jakob's avatar
Wenzel Jakob committed
1834
#  pragma warning(pop)
1835
1836
#elif defined(__INTEL_COMPILER)
/* Leave ignored warnings on */
1837
#elif defined(__GNUG__) && !defined(__clang__)
Wenzel Jakob's avatar
Wenzel Jakob committed
1838
#  pragma GCC diagnostic pop
Wenzel Jakob's avatar
Wenzel Jakob committed
1839
#endif