pybind.h 24.8 KB
Newer Older
Wenzel Jakob's avatar
Wenzel Jakob committed
1
2
3
4
5
6
7
8
9
/*
    pybind/pybind.h: Main header file of the C++11 python binding generator library

    Copyright (c) 2015 Wenzel Jakob <wenzel@inf.ethz.ch>

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

10
#pragma once
Wenzel Jakob's avatar
Wenzel Jakob committed
11
12
13
14
15
16
17
18
19
20

#if defined(_MSC_VER)
#pragma warning(push)
#pragma warning(disable: 4127) // warning C4127: Conditional expression is constant
#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
#pragma warning(disable: 4100) // warning C4100: Unreferenced formal parameter
#pragma warning(disable: 4512) // warning C4512: Assignment operator was implicitly defined as deleted
#endif

21
#include <pybind/cast.h>
Wenzel Jakob's avatar
Wenzel Jakob committed
22
23
24

NAMESPACE_BEGIN(pybind)

25
26
class cpp_function : public function {
public:
Wenzel Jakob's avatar
Wenzel Jakob committed
27
28
29
30
31
32
33
    struct function_entry {
        std::function<PyObject* (PyObject *)> impl;
        std::string signature, doc;
        bool is_constructor;
        function_entry *next = nullptr;
    };

34
35
36
37
38
    cpp_function() { }
    template <typename Func> cpp_function(
        Func &&_func, const char *name = nullptr, const char *doc = nullptr,
        return_value_policy policy = return_value_policy::automatic,
        function sibling = function(), bool is_method = false) {
Wenzel Jakob's avatar
Wenzel Jakob committed
39
40
41
42
43
44
        /* Function traits extracted from the template type 'Func' */
        typedef mpl::function_traits<Func> f_traits;

        /* Suitable input and output casters */
        typedef typename detail::type_caster<typename f_traits::args_type> cast_in;
        typedef typename detail::type_caster<typename mpl::normalize_type<typename f_traits::return_type>::type> cast_out;
45
        typename f_traits::f_type func = f_traits::cast(std::forward<Func>(_func));
Wenzel Jakob's avatar
Wenzel Jakob committed
46
47
48
49
50
51
52
53

        auto impl = [func, policy](PyObject *pyArgs) -> PyObject *{
            cast_in args;
            if (!args.load(pyArgs, true))
                return nullptr;
            PyObject *parent = policy != return_value_policy::reference_internal
                ? nullptr : PyTuple_GetItem(pyArgs, 0);
            return cast_out::cast(
54
                f_traits::dispatch(func, args.operator typename f_traits::args_type()),
Wenzel Jakob's avatar
Wenzel Jakob committed
55
56
57
                policy, parent);
        };

58
59
        initialize(name, doc, cast_in::name() + std::string(" -> ") + cast_out::name(),
                    sibling, is_method, std::move(impl));
Wenzel Jakob's avatar
Wenzel Jakob committed
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
    }
private:
    static PyObject *dispatcher(PyObject *self, PyObject *args, PyObject * /* kwargs */) {
        function_entry *overloads = (function_entry *) PyCapsule_GetPointer(self, nullptr);
        PyObject *result = nullptr;
        try {
            for (function_entry *it = overloads; it != nullptr; it = it->next) {
                if ((result = it->impl(args)) != nullptr)
                    break;
            }
        } catch (const error_already_set &) {                                               return nullptr;
        } catch (const index_error &e)    { PyErr_SetString(PyExc_IndexError,    e.what()); return nullptr;
        } catch (const stop_iteration &e) { PyErr_SetString(PyExc_StopIteration, e.what()); return nullptr;
        } catch (const std::exception &e) { PyErr_SetString(PyExc_RuntimeError,  e.what()); return nullptr;
        } catch (...) {
            PyErr_SetString(PyExc_RuntimeError, "Caught an unknown exception!");
            return nullptr;
        }
        if (result) {
            if (overloads->is_constructor) {
                PyObject *inst = PyTuple_GetItem(args, 0);
                const detail::type_info *type_info =
                    capsule(PyObject_GetAttrString((PyObject *) Py_TYPE(inst),
                                const_cast<char *>("__pybind__")), false);
                type_info->init_holder(inst);
            }
            return result;
        } else {
            std::string signatures = "Incompatible function arguments. The "
                                     "following argument types are supported:\n";
            int ctr = 0;
            for (function_entry *it = overloads; it != nullptr; it = it->next) {
                signatures += "    "+ std::to_string(++ctr) + ". ";
                signatures += it->signature;
                signatures += "\n";
            }
            PyErr_SetString(PyExc_TypeError, signatures.c_str());
            return nullptr;
        }
    }

101
102
103
104
105
106
107
108
109
110
111
112
113
114
    void initialize(const char *name, const char *doc,
                    const std::string &signature, function sibling,
                    bool is_method, std::function<PyObject *(PyObject *)> &&impl) {
        if (name == nullptr)
            name = "";

        /* Linked list of function call handlers (for overloading) */
        function_entry *entry = new function_entry();
        entry->impl = std::move(impl);
        entry->is_constructor = !strcmp(name, "__init__");
        entry->signature = signature;
        if (doc) entry->doc = doc;

        if (!sibling.ptr() || !PyCFunction_Check(sibling.ptr())) {
Wenzel Jakob's avatar
Wenzel Jakob committed
115
116
            PyMethodDef *def = new PyMethodDef();
            memset(def, 0, sizeof(PyMethodDef));
117
            def->ml_name = name != nullptr ? strdup(name) : name;
Wenzel Jakob's avatar
Wenzel Jakob committed
118
119
120
121
122
            def->ml_meth = reinterpret_cast<PyCFunction>(*dispatcher);
            def->ml_flags = METH_VARARGS | METH_KEYWORDS;
            capsule entry_capsule(entry);
            m_ptr = PyCFunction_New(def, entry_capsule.ptr());
            if (!m_ptr)
123
                throw std::runtime_error("cpp_function::cpp_function(): Could not allocate function object");
Wenzel Jakob's avatar
Wenzel Jakob committed
124
        } else {
125
            m_ptr = sibling.ptr();
Wenzel Jakob's avatar
Wenzel Jakob committed
126
127
128
129
130
131
132
133
134
            inc_ref();
            capsule entry_capsule(PyCFunction_GetSelf(m_ptr), true);
            function_entry *parent = (function_entry *) entry_capsule, *backup = parent;
            while (parent->next)
                parent = parent->next;
            parent->next = entry;
            entry = backup;
        }
        std::string signatures;
Wenzel Jakob's avatar
Wenzel Jakob committed
135
        int it = 0;
Wenzel Jakob's avatar
Wenzel Jakob committed
136
        while (entry) { /* Create pydoc entry */
Wenzel Jakob's avatar
Wenzel Jakob committed
137
138
            if (sibling.ptr())
                signatures += std::to_string(++it) + ". ";
Wenzel Jakob's avatar
Wenzel Jakob committed
139
140
141
142
143
144
145
146
147
148
149
150
151
152
            signatures += "Signature : " + std::string(entry->signature) + "\n";
            if (!entry->doc.empty())
                signatures += "\n" + std::string(entry->doc) + "\n";
            if (entry->next)
                signatures += "\n";
            entry = entry->next;
        }
        PyCFunctionObject *func = (PyCFunctionObject *) m_ptr;
        if (func->m_ml->ml_doc)
            std::free((char *) func->m_ml->ml_doc);
        func->m_ml->ml_doc = strdup(signatures.c_str());
        if (is_method) {
            m_ptr = PyInstanceMethod_New(m_ptr);
            if (!m_ptr)
153
                throw std::runtime_error("cpp_function::cpp_function(): Could not allocate instance method object");
Wenzel Jakob's avatar
Wenzel Jakob committed
154
155
156
157
158
            Py_DECREF(func);
        }
    }
};

159
160
161
162
163
164
165
166
167
168
class cpp_method : public cpp_function {
public:
    cpp_method () { }
    template <typename Func>
    cpp_method(Func &&_func, const char *name = nullptr, const char *doc = nullptr,
               return_value_policy policy = return_value_policy::automatic,
               function sibling = function())
        : cpp_function(std::forward<Func>(_func), name, doc, policy, sibling, true) { }
};

Wenzel Jakob's avatar
Wenzel Jakob committed
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
class module : public object {
public:
    PYTHON_OBJECT_DEFAULT(module, object, PyModule_Check)

    module(const char *name, const char *doc = nullptr) {
        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);
        if (m_ptr == nullptr)
            throw std::runtime_error("Internal error in module::module()");
        inc_ref();
    }

186
187
188
189
    template <typename Func>
    module &def(const char *name, Func f, const char *doc = nullptr,
                return_value_policy policy = return_value_policy::automatic) {
        cpp_function func(f, name, doc, policy, (function) attr(name));
Wenzel Jakob's avatar
Wenzel Jakob committed
190
191
192
193
194
        func.inc_ref(); /* The following line steals a reference to 'func' */
        PyModule_AddObject(ptr(), name, func.ptr());
        return *this;
    }

195
    module def_submodule(const char *name, const char *doc = nullptr) {
Wenzel Jakob's avatar
Wenzel Jakob committed
196
197
198
        std::string full_name = std::string(PyModule_GetName(m_ptr))
            + std::string(".") + std::string(name);
        module result(PyImport_AddModule(full_name.c_str()), true);
199
200
        if (doc)
            result.attr("__doc__") = pybind::str(doc);
Wenzel Jakob's avatar
Wenzel Jakob committed
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
        attr(name) = result;
        return result;
    }
};

NAMESPACE_BEGIN(detail)
/* Forward declarations */
enum op_id : int;
enum op_type : int;
struct undefined_t;
template <op_id id, op_type ot, typename L = undefined_t, typename R = undefined_t> struct op_;
template <typename ... Args> struct init;

/// Basic support for creating new Python heap types
class custom_type : public object {
public:
    PYTHON_OBJECT_DEFAULT(custom_type, object, PyType_Check)

    custom_type(object &scope, const char *name_, const std::string &type_name,
                size_t type_size, size_t instance_size,
                void (*init_holder)(PyObject *), const destructor &dealloc,
                PyObject *parent, const char *doc) {
        PyHeapTypeObject *type = (PyHeapTypeObject*) PyType_Type.tp_alloc(&PyType_Type, 0);
        PyObject *name = PyUnicode_FromString(name_);
        if (type == nullptr || name == nullptr)
            throw std::runtime_error("Internal error in custom_type::custom_type()");
        Py_INCREF(name);
        std::string full_name(name_);

        pybind::str scope_name = (object) scope.attr("__name__"),
                    module_name = (object) scope.attr("__module__");

        if (scope_name.check())
            full_name =  std::string(scope_name) + "." + full_name;
        if (module_name.check())
            full_name =  std::string(module_name) + "." + full_name;

        type->ht_name = type->ht_qualname = name;
        type->ht_type.tp_name = strdup(full_name.c_str());
        type->ht_type.tp_basicsize = instance_size;
        type->ht_type.tp_init = (initproc) init;
        type->ht_type.tp_new = (newfunc) new_instance;
        type->ht_type.tp_dealloc = dealloc;
        type->ht_type.tp_flags |=
            Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HEAPTYPE;
        type->ht_type.tp_flags &= ~Py_TPFLAGS_HAVE_GC;
        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;
        type->ht_type.tp_base = (PyTypeObject *) parent;
        Py_XINCREF(parent);

        if (PyType_Ready(&type->ht_type) < 0)
            throw std::runtime_error("Internal error in custom_type::custom_type()");
        m_ptr = (PyObject *) type;

        /* Needed by pydoc */
        if (((module &) scope).check())
            attr("__module__") = scope_name;

        auto &type_info = detail::get_internals().registered_types[type_name];
        type_info.type = (PyTypeObject *) m_ptr;
        type_info.type_size = type_size;
        type_info.init_holder = init_holder;
        attr("__pybind__") = capsule(&type_info);
266
267
        if (doc)
            attr("__doc__") = pybind::str(doc);
Wenzel Jakob's avatar
Wenzel Jakob committed
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389

        scope.attr(name) = *this;
    }

protected:
    /* Allocate a metaclass on demand (for static properties) */
    handle metaclass() {
        auto &ht_type = ((PyHeapTypeObject *) m_ptr)->ht_type;
        auto &ob_type = ht_type.ob_base.ob_base.ob_type;
        if (ob_type == &PyType_Type) {
            std::string name_ = std::string(ht_type.tp_name) + "_meta";
            PyHeapTypeObject *type = (PyHeapTypeObject*) PyType_Type.tp_alloc(&PyType_Type, 0);
            PyObject *name = PyUnicode_FromString(name_.c_str());
            if (type == nullptr || name == nullptr)
                throw std::runtime_error("Internal error in custom_type::metaclass()");
            Py_INCREF(name);
            type->ht_name = type->ht_qualname = name;
            type->ht_type.tp_name = strdup(name_.c_str());
            type->ht_type.tp_base = &PyType_Type;
            type->ht_type.tp_flags |= Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HEAPTYPE;
            type->ht_type.tp_flags &= ~Py_TPFLAGS_HAVE_GC;
            if (PyType_Ready(&type->ht_type) < 0)
                throw std::runtime_error("Internal error in custom_type::metaclass()");
            ob_type = (PyTypeObject *) type;
            Py_INCREF(type);
        }
        return handle((PyObject *) ob_type);
    }

    static int init(void *self, PyObject *, PyObject *) {
        std::string msg = std::string(Py_TYPE(self)->tp_name) + ": No constructor defined!";
        PyErr_SetString(PyExc_TypeError, msg.c_str());
        return -1;
    }

    static PyObject *new_instance(PyTypeObject *type, PyObject *, PyObject *) {
        const detail::type_info *type_info = capsule(
            PyObject_GetAttrString((PyObject *) type, const_cast<char*>("__pybind__")), false);
        instance<void> *self = (instance<void> *) PyType_GenericAlloc(type, 0);
        self->value = ::operator new(type_info->type_size);
        self->owned = true;
        self->parent = nullptr;
        self->constructed = false;
        detail::get_internals().registered_instances[self->value] = (PyObject *) self;
        return (PyObject *) self;
    }

    static void dealloc(instance<void> *self) {
        if (self->value) {
            bool dont_cache = self->parent && ((instance<void> *) self->parent)->value == self->value;
            if (!dont_cache) { // avoid an issue with internal references matching their parent's address
                auto &registered_instances = detail::get_internals().registered_instances;
                auto it = registered_instances.find(self->value);
                if (it == registered_instances.end())
                    throw std::runtime_error("Deallocating unregistered instance!");
                registered_instances.erase(it);
            }
            Py_XDECREF(self->parent);
        }
        Py_TYPE(self)->tp_free((PyObject*) self);
    }

    void install_buffer_funcs(const std::function<buffer_info *(PyObject *)> &func) {
        PyHeapTypeObject *type = (PyHeapTypeObject*) m_ptr;
        type->ht_type.tp_as_buffer = &type->as_buffer;
        type->as_buffer.bf_getbuffer = getbuffer;
        type->as_buffer.bf_releasebuffer = releasebuffer;
        ((detail::type_info *) capsule(attr("__pybind__")))->get_buffer = func;
    }

    static int getbuffer(PyObject *obj, Py_buffer *view, int flags) {
        auto const &info_func = ((detail::type_info *) capsule(handle(obj).attr("__pybind__")))->get_buffer;
        if (view == nullptr || obj == nullptr || !info_func) {
            PyErr_SetString(PyExc_BufferError, "Internal error");
            return -1;
        }
        memset(view, 0, sizeof(Py_buffer));
        buffer_info *info = info_func(obj);
        view->obj = obj;
        view->ndim = 1;
        view->internal = info;
        view->buf = info->ptr;
        view->itemsize = info->itemsize;
        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) {
            view->ndim = info->ndim;
            view->strides = (Py_ssize_t *)&info->strides[0];
            view->shape = (Py_ssize_t *) &info->shape[0];
        }
        Py_INCREF(view->obj);
        return 0;
    }

    static void releasebuffer(PyObject *, Py_buffer *view) { delete (buffer_info *) view->internal; }
};

NAMESPACE_END(detail)

template <typename type, typename holder_type = std::unique_ptr<type>> class class_ : public detail::custom_type {
public:
    typedef detail::instance<type, holder_type> instance_type;

    PYTHON_OBJECT(class_, detail::custom_type, PyType_Check)

    class_(object &scope, const char *name, const char *doc = nullptr)
        : detail::custom_type(scope, name, type_id<type>(), sizeof(type),
                              sizeof(instance_type), init_holder, dealloc,
                              nullptr, doc) { }

    class_(object &scope, const char *name, object &parent,
           const char *doc = nullptr)
        : detail::custom_type(scope, name, type_id<type>(), sizeof(type),
                              sizeof(instance_type), init_holder, dealloc,
                              parent.ptr(), doc) { }

    template <typename Func>
    class_ &def(const char *name, Func f, const char *doc = nullptr,
                return_value_policy policy = return_value_policy::automatic) {
390
        attr(name) = cpp_method(f, name, doc, policy, (function) attr(name));
Wenzel Jakob's avatar
Wenzel Jakob committed
391
392
393
394
395
396
        return *this;
    }

    template <typename Func> class_ &
    def_static(const char *name, Func f, const char *doc = nullptr,
               return_value_policy policy = return_value_policy::automatic) {
397
        attr(name) = cpp_function(f, name, doc, policy, (function) attr(name));
Wenzel Jakob's avatar
Wenzel Jakob committed
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
        return *this;
    }

    template <detail::op_id id, detail::op_type ot, typename L, typename R>
    class_ &def(const detail::op_<id, ot, L, R> &op, const char *doc = nullptr,
                return_value_policy policy = return_value_policy::automatic) {
        op.template execute<type>(*this, doc, policy);
        return *this;
    }

    template <detail::op_id id, detail::op_type ot, typename L, typename R> class_ &
    def_cast(const detail::op_<id, ot, L, R> &op, const char *doc = nullptr,
             return_value_policy policy = return_value_policy::automatic) {
        op.template execute_cast<type>(*this, doc, policy);
        return *this;
    }

    template <typename... Args>
    class_ &def(const detail::init<Args...> &init, const char *doc = nullptr) {
        init.template execute<type>(*this, doc);
        return *this;
    }

    class_& def_buffer(const std::function<buffer_info(type&)> &func) {
        install_buffer_funcs([func](PyObject *obj) -> buffer_info* {
            detail::type_caster<type> caster;
            if (!caster.load(obj, false))
                return nullptr;
            return new buffer_info(func(caster));
        });
        return *this;
    }

    template <typename C, typename D>
    class_ &def_readwrite(const char *name, D C::*pm,
433
434
435
436
                         const char *doc = nullptr) {
        cpp_method fget([pm](const C &c) -> const D &{ return c.*pm; }, nullptr,
                        nullptr, return_value_policy::reference_internal),
                   fset([pm](C &c, const D &value) { c.*pm = value; });
Wenzel Jakob's avatar
Wenzel Jakob committed
437
438
439
440
441
442
        def_property(name, fget, fset, doc);
        return *this;
    }

    template <typename C, typename D>
    class_ &def_readonly(const char *name, const D C::*pm,
443
444
445
                          const char *doc = nullptr) {
        cpp_method fget([pm](const C &c) -> const D &{ return c.*pm; }, nullptr,
                        nullptr, return_value_policy::reference_internal);
Wenzel Jakob's avatar
Wenzel Jakob committed
446
447
448
449
450
451
452
        def_property(name, fget, doc);
        return *this;
    }

    template <typename D>
    class_ &def_readwrite_static(const char *name, D *pm,
                                 const char *doc = nullptr) {
453
454
455
        cpp_function fget([pm](object) -> const D &{ return *pm; }, nullptr,
                        nullptr, return_value_policy::reference_internal),
                     fset([pm](object, const D &value) { *pm = value; });
Wenzel Jakob's avatar
Wenzel Jakob committed
456
457
458
459
460
461
462
        def_property_static(name, fget, fset, doc);
        return *this;
    }

    template <typename D>
    class_ &def_readonly_static(const char *name, const D *pm,
                                const char *doc = nullptr) {
463
464
        cpp_function fget([pm](object) -> const D &{ return *pm; }, nullptr,
                        nullptr, return_value_policy::reference_internal);
Wenzel Jakob's avatar
Wenzel Jakob committed
465
466
467
468
        def_property_static(name, fget, doc);
        return *this;
    }

469
    class_ &def_property(const char *name, const cpp_method &fget,
Wenzel Jakob's avatar
Wenzel Jakob committed
470
                         const char *doc = nullptr) {
471
        def_property(name, fget, cpp_method(), doc);
Wenzel Jakob's avatar
Wenzel Jakob committed
472
473
474
        return *this;
    }

475
    class_ &def_property_static(const char *name, const cpp_function &fget,
Wenzel Jakob's avatar
Wenzel Jakob committed
476
                                const char *doc = nullptr) {
477
        def_property_static(name, fget, cpp_function(), doc);
Wenzel Jakob's avatar
Wenzel Jakob committed
478
479
480
        return *this;
    }

481
482
    class_ &def_property(const char *name, const cpp_method &fget,
                         const cpp_method &fset, const char *doc = nullptr) {
Wenzel Jakob's avatar
Wenzel Jakob committed
483
484
485
486
487
488
489
490
        object property(
            PyObject_CallFunction((PyObject *)&PyProperty_Type,
                                  const_cast<char *>("OOOs"), fget.ptr() ? fget.ptr() : Py_None,
                                  fset.ptr() ? fset.ptr() : Py_None, Py_None, doc), false);
        attr(name) = property;
        return *this;
    }

491
492
    class_ &def_property_static(const char *name, const cpp_function &fget,
                                const cpp_function &fset,
Wenzel Jakob's avatar
Wenzel Jakob committed
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
                                const char *doc = nullptr) {
        object property(
            PyObject_CallFunction((PyObject *)&PyProperty_Type,
                                  const_cast<char *>("OOOs"), fget.ptr() ? fget.ptr() : Py_None,
                                  fset.ptr() ? fset.ptr() : Py_None, Py_None, doc), false);
        metaclass().attr(name) = property;
        return *this;
    }
private:
    static void init_holder(PyObject *inst_) {
        instance_type *inst = (instance_type *) inst_;
        new (&inst->holder) holder_type(inst->value);
        inst->constructed = true;
    }
    static void dealloc(PyObject *inst_) {
        instance_type *inst = (instance_type *) inst_;
        if (inst->owned) {
            if (inst->constructed)
                inst->holder.~holder_type();
            else
                ::operator delete(inst->value);
        }
        custom_type::dealloc((detail::instance<void> *) inst);
    }
};

/// Binds C++ enumerations and enumeration classes to Python
template <typename Type> class enum_ : public class_<Type> {
public:
    enum_(object &scope, const char *name, const char *doc = nullptr)
      : class_<Type>(scope, name, doc), m_parent(scope) {
        auto entries = new std::unordered_map<int, const char *>();
        this->def("__str__", [name, entries](Type value) -> std::string {
            auto it = entries->find(value);
            return std::string(name) + "." +
                ((it == entries->end()) ? std::string("???")
                                        : std::string(it->second));
        });
        m_entries = entries;
    }

    /// Export enumeration entries into the parent scope
    void export_values() {
        PyObject *dict = ((PyTypeObject *) this->m_ptr)->tp_dict;
        PyObject *key, *value;
        Py_ssize_t pos = 0;
        while (PyDict_Next(dict, &pos, &key, &value))
            if (PyObject_IsInstance(value, this->m_ptr))
                m_parent.attr(key) = value;
    }

    /// Add an enumeration entry
    enum_& value(char const* name, Type value) {
        this->attr(name) = pybind::cast(value, return_value_policy::copy);
        (*m_entries)[(int) value] = name;
        return *this;
    }
private:
    std::unordered_map<int, const char *> *m_entries;
    object &m_parent;
};

NAMESPACE_BEGIN(detail)
template <typename ... Args> struct init {
    template <typename Base, typename Holder> void execute(pybind::class_<Base, Holder> &class_, const char *doc) const {
        /// Function which calls a specific C++ in-place constructor
        class_.def("__init__", [](Base *instance, Args... args) { new (instance) Base(args...); }, doc);
    }
};
NAMESPACE_END(detail)

template <typename... Args> detail::init<Args...> init() { return detail::init<Args...>(); };

template <typename InputType, typename OutputType> void implicitly_convertible() {
    auto implicit_caster = [](PyObject *obj, PyTypeObject *type) -> PyObject *{
        if (!detail::type_caster<InputType>().load(obj, false))
            return nullptr;
        tuple args(1);
        args[0] = obj;
        PyObject *result = PyObject_Call((PyObject *) type, args.ptr(), nullptr);
        if (result == nullptr)
            PyErr_Clear();
        return result;
    };
    std::string output_type_name = type_id<OutputType>();
    auto & registered_types = detail::get_internals().registered_types;
    auto it = registered_types.find(output_type_name);
    if (it == registered_types.end())
        throw std::runtime_error("implicitly_convertible: Unable to find type " + output_type_name);
    it->second.implicit_conversions.push_back(implicit_caster);
}

inline void init_threading() { PyEval_InitThreads(); }

class gil_scoped_acquire {
    PyGILState_STATE state;
public:
    inline gil_scoped_acquire() { state = PyGILState_Ensure(); }
    inline ~gil_scoped_acquire() { PyGILState_Release(state); }
};

class gil_scoped_release {
    PyThreadState *state;
public:
    inline gil_scoped_release() { state = PyEval_SaveThread(); }
    inline ~gil_scoped_release() { PyEval_RestoreThread(state); }
};

NAMESPACE_END(pybind)

#if defined(_MSC_VER)
#pragma warning(pop)
#endif

#undef PYTHON_OBJECT
#undef PYTHON_OBJECT_DEFAULT