pybind.h 36.1 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

#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
19
#elif defined(__GNUG__) and !defined(__clang__)
20
21
22
23
#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
24
25
#endif

26
#include <pybind/cast.h>
27
#include <iostream>
Wenzel Jakob's avatar
Wenzel Jakob committed
28
29
30

NAMESPACE_BEGIN(pybind)

31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
template <typename T> struct arg_t;

/// Annotation for keyword arguments
struct arg {
    arg(const char *name) : name(name) { }
    template <typename T> inline arg_t<T> operator=(const T &value);
    const char *name;
};

/// Annotation for keyword arguments with default values
template <typename T> struct arg_t : public arg {
    arg_t(const char *name, const T &value) : arg(name), value(value) { }
    T value;
};
template <typename T> inline arg_t<T> arg::operator=(const T &value) { return arg_t<T>(name, value); }

/// Annotation for methods
48
49
50
51
52
53
54
55
struct is_method {
#if PY_MAJOR_VERSION < 3
    PyObject *class_;
    is_method(object *o) : class_(o->ptr()) { }
#else
    is_method(object *) { }
#endif
};
56
57
58
59
60
61
62
63
64
65

/// Annotation for documentation
struct doc { const char *value; doc(const char *value) : value(value) { } };

/// Annotation for function names
struct name { const char *value; name(const char *value) : value(value) { } };

/// Annotation for function siblings
struct sibling { PyObject *value; sibling(handle value) : value(value.ptr()) { } };

Wenzel Jakob's avatar
Wenzel Jakob committed
66
/// Wraps an arbitrary C++ function/method/lambda function/.. into a callable Python object
67
class cpp_function : public function {
Wenzel Jakob's avatar
Wenzel Jakob committed
68
69
private:
    /// Chained list of function entries for overloading
Wenzel Jakob's avatar
Wenzel Jakob committed
70
    struct function_entry {
71
        const char *name = nullptr;
Wenzel Jakob's avatar
Wenzel Jakob committed
72
73
        PyObject * (*impl) (function_entry *, PyObject *, PyObject *, PyObject *) = nullptr;
        PyMethodDef *def = nullptr;
74
        void *data = nullptr;
75
76
77
78
        bool is_constructor = false, is_method = false;
        short keywords = 0;
        return_value_policy policy = return_value_policy::automatic;
        std::string signature;
79
80
81
#if PY_MAJOR_VERSION < 3
        PyObject *class_ = nullptr;
#endif
82
83
        PyObject *sibling = nullptr;
        const char *doc = nullptr;
Wenzel Jakob's avatar
Wenzel Jakob committed
84
85
86
        function_entry *next = nullptr;
    };

87
88
    function_entry *m_entry;

Wenzel Jakob's avatar
Wenzel Jakob committed
89
90
91
92
93
94
    /// Picks a suitable return value converter from cast.h
    template <typename T> using return_value_caster =
        detail::type_caster<typename std::conditional<
            std::is_void<T>::value, detail::void_type, typename detail::decay<T>::type>::type>;

    /// Picks a suitable argument value converter from cast.h
95
    template <typename... T> using arg_value_caster =
Wenzel Jakob's avatar
Wenzel Jakob committed
96
        detail::type_caster<typename std::tuple<T...>>;
97

98
99
100
    template <typename... T> static void process_extras(const std::tuple<T...> &args,
            function_entry *entry, const char **kw, const char **def) {
        process_extras(args, entry, kw, def, typename detail::make_index_sequence<sizeof...(T)>::type());
101
102
    }

103
104
105
    template <typename... T, size_t ... Index> static void process_extras(const std::tuple<T...> &args,
            function_entry *entry, const char **kw, const char **def, detail::index_sequence<Index...>) {
        int unused[] = { 0, (process_extra(std::get<Index>(args), entry, kw, def), 0)... };
106
107
108
        (void) unused;
    }

109
110
111
112
    template <typename... T> static void process_extras(const std::tuple<T...> &args,
            PyObject *pyArgs, PyObject *kwargs, bool is_method) {
        process_extras(args, pyArgs, kwargs, is_method, typename detail::make_index_sequence<sizeof...(T)>::type());
    }
113

114
115
116
117
    template <typename... T, size_t... Index> static void process_extras(const std::tuple<T...> &args,
            PyObject *pyArgs, PyObject *kwargs, bool is_method, detail::index_sequence<Index...>) {
        int index = is_method ? 1 : 0;
        int unused[] = { 0, (process_extra(std::get<Index>(args), index, pyArgs, kwargs), 0)... };
Wenzel Jakob's avatar
Wenzel Jakob committed
118
        (void) unused; (void) index;
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
    }

    static void process_extra(const char *doc, function_entry *entry, const char **, const char **) { entry->doc = doc; }
    static void process_extra(const pybind::doc &d, function_entry *entry, const char **, const char **) { entry->doc = d.value; }
    static void process_extra(const pybind::name &n, function_entry *entry, const char **, const char **) { entry->name = n.value; }
    static void process_extra(const pybind::arg &a, function_entry *entry, const char **kw, const char **) {
        if (entry->is_method && entry->keywords == 0)
            kw[entry->keywords++] = "self";
        kw[entry->keywords++] = a.name;
    }
    template <typename T>
    static void process_extra(const pybind::arg_t<T> &a, function_entry *entry, const char **kw, const char **def) {
        if (entry->is_method && entry->keywords == 0)
            kw[entry->keywords++] = "self";
        kw[entry->keywords] = a.name;
        def[entry->keywords++] = strdup(std::to_string(a.value).c_str());
    }

137
138
139
140
141
142
143
144
    static void process_extra(const pybind::is_method &m, function_entry *entry, const char **, const char **) {
        entry->is_method = true;
#if PY_MAJOR_VERSION < 3
        entry->class_ = m.class_;
#else
        (void) m;
#endif
    }
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
    static void process_extra(const pybind::return_value_policy p, function_entry *entry, const char **, const char **) { entry->policy = p; }
    static void process_extra(pybind::sibling s, function_entry *entry, const char **, const char **) { entry->sibling = s.value; }

    template <typename T> static void process_extra(T, int &, PyObject *, PyObject *) { }
    static void process_extra(const pybind::arg &a, int &index, PyObject *args, PyObject *kwargs) {
        if (kwargs) {
            if (PyTuple_GET_ITEM(args, index) != nullptr) {
                index++;
                return;
            }
            PyObject *value = PyDict_GetItemString(kwargs, a.name);
            if (value) {
                Py_INCREF(value);
                PyTuple_SetItem(args, index, value);
            }
        }
        index++;
    }
    template <typename T>
    static void process_extra(const pybind::arg_t<T> &a, int &index, PyObject *args, PyObject *kwargs) {
        if (PyTuple_GET_ITEM(args, index) != nullptr) {
            index++;
            return;
        }
        PyObject *value = nullptr;
        if (kwargs)
            value = PyDict_GetItemString(kwargs, a.name);
        if (value) {
            Py_INCREF(value);
        } else {
            value = detail::type_caster<typename detail::decay<T>::type>::cast(
                a.value, return_value_policy::automatic, nullptr);
        }
        PyTuple_SetItem(args, index, value);
        index++;
    }
Wenzel Jakob's avatar
Wenzel Jakob committed
181
public:
182
    cpp_function() { }
Wenzel Jakob's avatar
Wenzel Jakob committed
183
184

    /// Vanilla function pointers
185
186
187
188
    template <typename Return, typename... Arg, typename... Extra>
    cpp_function(Return (*f)(Arg...), Extra&&... extra) {
        struct capture {
            Return (*f)(Arg...);
189
            std::tuple<Extra...> extras;
190
        };
Wenzel Jakob's avatar
Wenzel Jakob committed
191

192
193
        m_entry = new function_entry();
        m_entry->data = new capture { f, std::tuple<Extra...>(std::forward<Extra>(extra)...) };
Wenzel Jakob's avatar
Wenzel Jakob committed
194

195
196
197
        typedef arg_value_caster<Arg...> cast_in;
        typedef return_value_caster<Return> cast_out;

198
        m_entry->impl = [](function_entry *entry, PyObject *pyArgs, PyObject *kwargs, PyObject *parent) -> PyObject * {
199
200
            capture *data = (capture *) entry->data;
            process_extras(data->extras, pyArgs, kwargs, entry->is_method);
Wenzel Jakob's avatar
Wenzel Jakob committed
201
            cast_in args;
202
203
204
            if (!args.load(pyArgs, true))
                return nullptr;
            return cast_out::cast(args.template call<Return>(data->f), entry->policy, parent);
Wenzel Jakob's avatar
Wenzel Jakob committed
205
206
        };

207
208
        const int N = sizeof...(Extra) > sizeof...(Arg) ? sizeof...(Extra) : sizeof...(Arg);
        std::array<const char *, N> kw{}, def{};
209
        process_extras(((capture *) m_entry->data)->extras, m_entry, kw.data(), def.data());
210

Wenzel Jakob's avatar
Wenzel Jakob committed
211
        detail::descr d = cast_in::name(kw.data(), def.data());
212
        d += " -> ";
Wenzel Jakob's avatar
Wenzel Jakob committed
213
        d += std::move(cast_out::name());
214

215
        initialize(d, sizeof...(Arg));
Wenzel Jakob's avatar
Wenzel Jakob committed
216
217
218
    }

    /// Delegating helper constructor to deal with lambda functions
219
220
    template <typename Func, typename... Extra> cpp_function(Func &&f, Extra&&... extra) {
        initialize(std::forward<Func>(f),
Wenzel Jakob's avatar
Wenzel Jakob committed
221
                   (typename detail::remove_class<decltype(
222
223
                       &std::remove_reference<Func>::type::operator())>::type *) nullptr,
                   std::forward<Extra>(extra)...);
Wenzel Jakob's avatar
Wenzel Jakob committed
224
225
226
    }

    /// Class methods (non-const)
227
228
229
230
    template <typename Return, typename Class, typename... Arg, typename... Extra> cpp_function(
            Return (Class::*f)(Arg...), Extra&&... extra) {
        initialize([f](Class *c, Arg... args) -> Return { return (c->*f)(args...); },
                   (Return (*) (Class *, Arg...)) nullptr, std::forward<Extra>(extra)...);
Wenzel Jakob's avatar
Wenzel Jakob committed
231
    }
Wenzel Jakob's avatar
Wenzel Jakob committed
232
233

    /// Class methods (const)
234
235
236
237
    template <typename Return, typename Class, typename... Arg, typename... Extra> cpp_function(
            Return (Class::*f)(Arg...) const, Extra&&... extra) {
        initialize([f](const Class *c, Arg... args) -> Return { return (c->*f)(args...); },
                   (Return (*)(const Class *, Arg ...)) nullptr, std::forward<Extra>(extra)...);
Wenzel Jakob's avatar
Wenzel Jakob committed
238
239
    }

240
241
242
    /// Return the function name
    const char *name() const { return m_entry->name; }

Wenzel Jakob's avatar
Wenzel Jakob committed
243
private:
Wenzel Jakob's avatar
Wenzel Jakob committed
244
    /// Functors, lambda functions, etc.
245
246
247
248
    template <typename Func, typename Return, typename... Arg, typename... Extra>
    void initialize(Func &&f, Return (*)(Arg...), Extra&&... extra) {
        struct capture {
            typename std::remove_reference<Func>::type f;
249
            std::tuple<Extra...> extras;
250
        };
Wenzel Jakob's avatar
Wenzel Jakob committed
251

252
253
        m_entry = new function_entry();
        m_entry->data = new capture { std::forward<Func>(f), std::tuple<Extra...>(std::forward<Extra>(extra)...) };
254
255
256

        typedef arg_value_caster<Arg...> cast_in;
        typedef return_value_caster<Return> cast_out;
Wenzel Jakob's avatar
Wenzel Jakob committed
257

258
        m_entry->impl = [](function_entry *entry, PyObject *pyArgs, PyObject *kwargs, PyObject *parent) -> PyObject *{
259
260
            capture *data = (capture *)entry->data;
            process_extras(data->extras, pyArgs, kwargs, entry->is_method);
Wenzel Jakob's avatar
Wenzel Jakob committed
261
            cast_in args;
262
263
264
            if (!args.load(pyArgs, true))
                return nullptr;
            return cast_out::cast(args.template call<Return>(data->f), entry->policy, parent);
Wenzel Jakob's avatar
Wenzel Jakob committed
265
266
        };

267
268
        const int N = sizeof...(Extra) > sizeof...(Arg) ? sizeof...(Extra) : sizeof...(Arg);
        std::array<const char *, N> kw{}, def{};
269
        process_extras(((capture *) m_entry->data)->extras, m_entry, kw.data(), def.data());
270

Wenzel Jakob's avatar
Wenzel Jakob committed
271
        detail::descr d = cast_in::name(kw.data(), def.data());
272
        d += " -> ";
Wenzel Jakob's avatar
Wenzel Jakob committed
273
        d += std::move(cast_out::name());
274

275
        initialize(d, sizeof...(Arg));
Wenzel Jakob's avatar
Wenzel Jakob committed
276
277
    }

Wenzel Jakob's avatar
Wenzel Jakob committed
278
    static PyObject *dispatcher(PyObject *self, PyObject *args, PyObject *kwargs) {
Wenzel Jakob's avatar
Wenzel Jakob committed
279
        function_entry *overloads = (function_entry *) PyCapsule_GetPointer(self, nullptr);
280
        int nargs = (int) PyTuple_Size(args);
Wenzel Jakob's avatar
Wenzel Jakob committed
281
        PyObject *result = nullptr;
282
        PyObject *parent = nargs > 0 ? PyTuple_GetItem(args, 0) : nullptr;
Wenzel Jakob's avatar
Wenzel Jakob committed
283
284
        try {
            for (function_entry *it = overloads; it != nullptr; it = it->next) {
285
286
                PyObject *args_ = args;

Wenzel Jakob's avatar
Wenzel Jakob committed
287
                if (it->keywords != 0 && nargs < it->keywords) {
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
                    args_ = PyTuple_New(it->keywords);
                    for (int i=0; i<nargs; ++i) {
                        PyObject *item = PyTuple_GET_ITEM(args, i);
                        Py_INCREF(item);
                        PyTuple_SET_ITEM(args_, i, item);
                    }
                }

                result = it->impl(it, args_, kwargs, parent);

                if (args_ != args) {
                    Py_DECREF(args_);
                }

                if (result != nullptr)
Wenzel Jakob's avatar
Wenzel Jakob committed
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
                    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;
        }
    }

336
337
338
339
340
341
342
343
344
345
    static void destruct(function_entry *entry) {
        while (entry) {
            delete entry->def;
            operator delete(entry->data);
            function_entry *next = entry->next;
            delete entry;
            entry = next;
        }
    }

346
347
348
    void initialize(const detail::descr &descr, int args) {
        if (m_entry->name == nullptr)
            m_entry->name = "";
349

350
351
352
353
354
355
#if PY_MAJOR_VERSION < 3
        if (strcmp(m_entry->name, "__next__") == 0)
            m_entry->name = "next";
#endif

        if (m_entry->keywords != 0 && m_entry->keywords != args)
356
            throw std::runtime_error(
357
358
                "cpp_function(): function \"" + std::string(m_entry->name) + "\" takes " +
                std::to_string(args) + " arguments, but " + std::to_string(m_entry->keywords) +
359
                " pybind::arg entries were specified!");
360

361
362
363
364
365
366
367
        m_entry->is_constructor = !strcmp(m_entry->name, "__init__");
        m_entry->signature = descr.str();

#if PY_MAJOR_VERSION < 3
        if (m_entry->sibling && PyMethod_Check(m_entry->sibling))
            m_entry->sibling = PyMethod_GET_FUNCTION(m_entry->sibling);
#endif
368

369
370
        function_entry *entry = m_entry;
        bool overloaded = false;
371
        if (!entry->sibling || !PyCFunction_Check(entry->sibling)) {
372
373
374
375
376
377
378
            entry->def = new PyMethodDef();
            memset(entry->def, 0, sizeof(PyMethodDef));
            entry->def->ml_name = entry->name;
            entry->def->ml_meth = reinterpret_cast<PyCFunction>(*dispatcher);
            entry->def->ml_flags = METH_VARARGS | METH_KEYWORDS;
            capsule entry_capsule(entry, [](PyObject *o) { destruct((function_entry *) PyCapsule_GetPointer(o, nullptr)); });
            m_ptr = PyCFunction_New(entry->def, entry_capsule.ptr());
Wenzel Jakob's avatar
Wenzel Jakob committed
379
            if (!m_ptr)
380
                throw std::runtime_error("cpp_function::cpp_function(): Could not allocate function object");
Wenzel Jakob's avatar
Wenzel Jakob committed
381
        } else {
382
            m_ptr = entry->sibling;
Wenzel Jakob's avatar
Wenzel Jakob committed
383
384
385
386
387
388
389
            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;
390
            overloaded = true;
Wenzel Jakob's avatar
Wenzel Jakob committed
391
        }
392

Wenzel Jakob's avatar
Wenzel Jakob committed
393
        std::string signatures;
394
395
396
        int index = 0;
        function_entry *it = entry;
        while (it) { /* Create pydoc it */
397
            if (overloaded)
398
399
400
401
402
                signatures += std::to_string(++index) + ". ";
            signatures += "Signature : " + std::string(it->signature) + "\n";
            if (it->doc && strlen(it->doc) > 0)
                signatures += "\n" + std::string(it->doc) + "\n";
            if (it->next)
Wenzel Jakob's avatar
Wenzel Jakob committed
403
                signatures += "\n";
404
            it = it->next;
Wenzel Jakob's avatar
Wenzel Jakob committed
405
406
407
408
409
        }
        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());
410
        if (entry->is_method) {
411
#if PY_MAJOR_VERSION >= 3
Wenzel Jakob's avatar
Wenzel Jakob committed
412
            m_ptr = PyInstanceMethod_New(m_ptr);
413
414
415
#else
            m_ptr = PyMethod_New(m_ptr, nullptr, entry->class_);
#endif
Wenzel Jakob's avatar
Wenzel Jakob committed
416
            if (!m_ptr)
417
                throw std::runtime_error("cpp_function::cpp_function(): Could not allocate instance method object");
Wenzel Jakob's avatar
Wenzel Jakob committed
418
419
420
421
422
423
424
            Py_DECREF(func);
        }
    }
};

class module : public object {
public:
Wenzel Jakob's avatar
Wenzel Jakob committed
425
    PYBIND_OBJECT_DEFAULT(module, object, PyModule_Check)
Wenzel Jakob's avatar
Wenzel Jakob committed
426
427

    module(const char *name, const char *doc = nullptr) {
428
#if PY_MAJOR_VERSION >= 3
Wenzel Jakob's avatar
Wenzel Jakob committed
429
430
431
432
433
434
435
        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);
436
437
438
#else
        m_ptr = Py_InitModule3(name, nullptr, doc);
#endif
Wenzel Jakob's avatar
Wenzel Jakob committed
439
440
441
442
443
        if (m_ptr == nullptr)
            throw std::runtime_error("Internal error in module::module()");
        inc_ref();
    }

444
445
446
447
    template <typename Func, typename... Extra>
    module &def(const char *name_, Func &&f, Extra&& ... extra) {
        cpp_function func(std::forward<Func>(f), name(name_),
                          sibling((handle) attr(name_)), std::forward<Extra>(extra)...);
Wenzel Jakob's avatar
Wenzel Jakob committed
448
        func.inc_ref(); /* The following line steals a reference to 'func' */
449
        PyModule_AddObject(ptr(), name_, func.ptr());
Wenzel Jakob's avatar
Wenzel Jakob committed
450
451
452
        return *this;
    }

453
    module def_submodule(const char *name, const char *doc = nullptr) {
Wenzel Jakob's avatar
Wenzel Jakob committed
454
455
456
        std::string full_name = std::string(PyModule_GetName(m_ptr))
            + std::string(".") + std::string(name);
        module result(PyImport_AddModule(full_name.c_str()), true);
457
458
        if (doc)
            result.attr("__doc__") = pybind::str(doc);
Wenzel Jakob's avatar
Wenzel Jakob committed
459
460
461
462
463
464
465
466
467
        attr(name) = result;
        return result;
    }
};

NAMESPACE_BEGIN(detail)
/// Basic support for creating new Python heap types
class custom_type : public object {
public:
Wenzel Jakob's avatar
Wenzel Jakob committed
468
    PYBIND_OBJECT_DEFAULT(custom_type, object, PyType_Check)
Wenzel Jakob's avatar
Wenzel Jakob committed
469

470
    custom_type(object &scope, const char *name_, const std::type_info *tinfo,
Wenzel Jakob's avatar
Wenzel Jakob committed
471
472
473
474
                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);
475
#if PY_MAJOR_VERSION >= 3
Wenzel Jakob's avatar
Wenzel Jakob committed
476
        PyObject *name = PyUnicode_FromString(name_);
477
478
479
#else
        PyObject *name = PyString_FromString(name_);
#endif
Wenzel Jakob's avatar
Wenzel Jakob committed
480
481
482
483
484
485
486
487
488
489
490
491
492
        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;

493
494
495
496
        type->ht_name = name;
#if PY_MAJOR_VERSION >= 3
        type->ht_qualname = name;
#endif
Wenzel Jakob's avatar
Wenzel Jakob committed
497
498
499
500
501
502
503
504
        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;
505
506
507
#if PY_MAJOR_VERSION < 3
        type->ht_type.tp_flags |= Py_TPFLAGS_CHECKTYPES;
#endif
Wenzel Jakob's avatar
Wenzel Jakob committed
508
509
510
511
        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;
Wenzel Jakob's avatar
Wenzel Jakob committed
512
513
514
515
516
        if (doc) {
            size_t size = strlen(doc)+1;
            type->ht_type.tp_doc = (char *)PyObject_MALLOC(size);
            memcpy((void *) type->ht_type.tp_doc, doc, size);
        }
Wenzel Jakob's avatar
Wenzel Jakob committed
517
518
519
520
521
522
523
        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 */
524
        attr("__module__") = scope_name;
Wenzel Jakob's avatar
Wenzel Jakob committed
525

526
        auto &type_info = detail::get_internals().registered_types[tinfo];
Wenzel Jakob's avatar
Wenzel Jakob committed
527
528
529
530
531
532
533
534
535
536
537
538
        type_info.type = (PyTypeObject *) m_ptr;
        type_info.type_size = type_size;
        type_info.init_holder = init_holder;
        attr("__pybind__") = capsule(&type_info);

        scope.attr(name) = *this;
    }

protected:
    /* Allocate a metaclass on demand (for static properties) */
    handle metaclass() {
        auto &ht_type = ((PyHeapTypeObject *) m_ptr)->ht_type;
539
#if PY_MAJOR_VERSION >= 3
Wenzel Jakob's avatar
Wenzel Jakob committed
540
        auto &ob_type = ht_type.ob_base.ob_base.ob_type;
541
542
543
544
#else
        auto &ob_type = ht_type.ob_type;
#endif

Wenzel Jakob's avatar
Wenzel Jakob committed
545
546
547
548
549
550
551
        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);
552
553
554
555
            type->ht_name = name;
#if PY_MAJOR_VERSION >= 3
            type->ht_qualname = name;
#endif
Wenzel Jakob's avatar
Wenzel Jakob committed
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
            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);
    }

Wenzel Jakob's avatar
Wenzel Jakob committed
601
602
603
    void install_buffer_funcs(
            buffer_info *(*get_buffer)(PyObject *, void *),
            void *get_buffer_data) {
Wenzel Jakob's avatar
Wenzel Jakob committed
604
605
        PyHeapTypeObject *type = (PyHeapTypeObject*) m_ptr;
        type->ht_type.tp_as_buffer = &type->as_buffer;
606
607
608
#if PY_MAJOR_VERSION < 3
        type->ht_type.tp_flags |= Py_TPFLAGS_HAVE_NEWBUFFER;
#endif
Wenzel Jakob's avatar
Wenzel Jakob committed
609
610
        type->as_buffer.bf_getbuffer = getbuffer;
        type->as_buffer.bf_releasebuffer = releasebuffer;
Wenzel Jakob's avatar
Wenzel Jakob committed
611
612
613
        auto info = ((detail::type_info *) capsule(attr("__pybind__")));
        info->get_buffer = get_buffer;
        info->get_buffer_data = get_buffer_data;
Wenzel Jakob's avatar
Wenzel Jakob committed
614
615
616
    }

    static int getbuffer(PyObject *obj, Py_buffer *view, int flags) {
Wenzel Jakob's avatar
Wenzel Jakob committed
617
618
619
        auto const &typeinfo = ((detail::type_info *) capsule(handle(obj).attr("__pybind__")));

        if (view == nullptr || obj == nullptr || !typeinfo || !typeinfo->get_buffer) {
Wenzel Jakob's avatar
Wenzel Jakob committed
620
621
622
623
            PyErr_SetString(PyExc_BufferError, "Internal error");
            return -1;
        }
        memset(view, 0, sizeof(Py_buffer));
Wenzel Jakob's avatar
Wenzel Jakob committed
624
        buffer_info *info = typeinfo->get_buffer(obj, typeinfo->get_buffer_data);
Wenzel Jakob's avatar
Wenzel Jakob committed
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
        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; }
};
646
647
648
649
650
651
652

/* 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;
Wenzel Jakob's avatar
Wenzel Jakob committed
653
654
655
656
657
658
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;

Wenzel Jakob's avatar
Wenzel Jakob committed
659
    PYBIND_OBJECT(class_, detail::custom_type, PyType_Check)
Wenzel Jakob's avatar
Wenzel Jakob committed
660
661

    class_(object &scope, const char *name, const char *doc = nullptr)
662
        : detail::custom_type(scope, name, &typeid(type), sizeof(type),
Wenzel Jakob's avatar
Wenzel Jakob committed
663
664
665
666
667
                              sizeof(instance_type), init_holder, dealloc,
                              nullptr, doc) { }

    class_(object &scope, const char *name, object &parent,
           const char *doc = nullptr)
668
        : detail::custom_type(scope, name, &typeid(type), sizeof(type),
Wenzel Jakob's avatar
Wenzel Jakob committed
669
670
671
                              sizeof(instance_type), init_holder, dealloc,
                              parent.ptr(), doc) { }

672
673
    template <typename Func, typename... Extra>
    class_ &def(const char *name_, Func&& f, Extra&&... extra) {
674
675
676
677
        cpp_function cf(std::forward<Func>(f), name(name_),
                        sibling(attr(name_)), is_method(this),
                        std::forward<Extra>(extra)...);
        attr(cf.name()) = cf;
Wenzel Jakob's avatar
Wenzel Jakob committed
678
679
680
        return *this;
    }

681
682
    template <typename Func, typename... Extra> class_ &
    def_static(const char *name_, Func f, Extra&&... extra) {
683
684
685
686
        cpp_function cf(std::forward<Func>(f), name(name_),
                        sibling(attr(name_)),
                        std::forward<Extra>(extra)...);
        attr(cf.name()) = cf;
Wenzel Jakob's avatar
Wenzel Jakob committed
687
688
689
        return *this;
    }

690
691
692
    template <detail::op_id id, detail::op_type ot, typename L, typename R, typename... Extra>
    class_ &def(const detail::op_<id, ot, L, R> &op, Extra&&... extra) {
        op.template execute<type>(*this, std::forward<Extra>(extra)...);
Wenzel Jakob's avatar
Wenzel Jakob committed
693
694
695
        return *this;
    }

696
697
698
    template <detail::op_id id, detail::op_type ot, typename L, typename R, typename... Extra>
    class_ & def_cast(const detail::op_<id, ot, L, R> &op, Extra&&... extra) {
        op.template execute_cast<type>(*this, std::forward<Extra>(extra)...);
Wenzel Jakob's avatar
Wenzel Jakob committed
699
700
701
        return *this;
    }

702
703
704
    template <typename... Args, typename... Extra>
    class_ &def(const detail::init<Args...> &init, Extra&&... extra) {
        init.template execute<type>(*this, std::forward<Extra>(extra)...);
Wenzel Jakob's avatar
Wenzel Jakob committed
705
706
707
        return *this;
    }

708
    template <typename Func> class_& def_buffer(Func &&func) {
Wenzel Jakob's avatar
Wenzel Jakob committed
709
710
711
        struct capture { Func func; };
        capture *ptr = new capture { std::forward<Func>(func) };
        install_buffer_funcs([](PyObject *obj, void *ptr) -> buffer_info* {
Wenzel Jakob's avatar
Wenzel Jakob committed
712
713
714
            detail::type_caster<type> caster;
            if (!caster.load(obj, false))
                return nullptr;
Wenzel Jakob's avatar
Wenzel Jakob committed
715
716
            return new buffer_info(((capture *) ptr)->func(caster));
        }, ptr);
Wenzel Jakob's avatar
Wenzel Jakob committed
717
718
719
        return *this;
    }

720
721
722
723
    template <typename C, typename D, typename... Extra>
    class_ &def_readwrite(const char *name, D C::*pm, Extra&&... extra) {
        cpp_function fget([pm](const C &c) -> const D &{ return c.*pm; },
                          return_value_policy::reference_internal,
724
                          is_method(this), extra...),
725
                     fset([pm](C &c, const D &value) { c.*pm = value; },
726
                          is_method(this), extra...);
727
        def_property(name, fget, fset);
Wenzel Jakob's avatar
Wenzel Jakob committed
728
729
730
        return *this;
    }

731
732
733
734
    template <typename C, typename D, typename... Extra>
    class_ &def_readonly(const char *name, const D C::*pm, Extra&& ...extra) {
        cpp_function fget([pm](const C &c) -> const D &{ return c.*pm; },
                          return_value_policy::reference_internal,
735
                          is_method(this), std::forward<Extra>(extra)...);
736
        def_property_readonly(name, fget);
Wenzel Jakob's avatar
Wenzel Jakob committed
737
738
739
        return *this;
    }

740
741
    template <typename D, typename... Extra>
    class_ &def_readwrite_static(const char *name, D *pm, Extra&& ...extra) {
742
        cpp_function fget([pm](object) -> const D &{ return *pm; }, nullptr,
743
744
745
                          return_value_policy::reference_internal, extra...),
                     fset([pm](object, const D &value) { *pm = value; }, extra...);
        def_property_static(name, fget, fset);
Wenzel Jakob's avatar
Wenzel Jakob committed
746
747
748
        return *this;
    }

749
750
    template <typename D, typename... Extra>
    class_ &def_readonly_static(const char *name, const D *pm, Extra&& ...extra) {
751
        cpp_function fget([pm](object) -> const D &{ return *pm; }, nullptr,
752
753
                          return_value_policy::reference_internal, std::forward<Extra>(extra)...);
        def_property_readonly_static(name, fget);
Wenzel Jakob's avatar
Wenzel Jakob committed
754
755
756
        return *this;
    }

757
758
    class_ &def_property_readonly(const char *name, const cpp_function &fget, const char *doc = nullptr) {
        def_property(name, fget, cpp_function(), doc);
Wenzel Jakob's avatar
Wenzel Jakob committed
759
760
761
        return *this;
    }

762
763
    class_ &def_property_readonly_static(const char *name, const cpp_function &fget, const char *doc = nullptr) {
        def_property_static(name, fget, cpp_function(), doc);
Wenzel Jakob's avatar
Wenzel Jakob committed
764
765
766
        return *this;
    }

767
768
    class_ &def_property(const char *name, const cpp_function &fget, const cpp_function &fset, const char *doc = nullptr) {
        object doc_obj = doc ? pybind::str(doc) : (object) const_cast<cpp_function&>(fget).attr("__doc__");
Wenzel Jakob's avatar
Wenzel Jakob committed
769
770
        object property(
            PyObject_CallFunction((PyObject *)&PyProperty_Type,
771
                                  const_cast<char *>("OOOO"), fget.ptr() ? fget.ptr() : Py_None,
772
                                  fset.ptr() ? fset.ptr() : Py_None, Py_None, doc_obj.ptr()), false);
Wenzel Jakob's avatar
Wenzel Jakob committed
773
774
775
776
        attr(name) = property;
        return *this;
    }

777
778
    class_ &def_property_static(const char *name, const cpp_function &fget, const cpp_function &fset, const char *doc = nullptr) {
        object doc_obj = doc ? pybind::str(doc) : (object) const_cast<cpp_function&>(fget).attr("__doc__");
Wenzel Jakob's avatar
Wenzel Jakob committed
779
780
        object property(
            PyObject_CallFunction((PyObject *)&PyProperty_Type,
781
782
                                  const_cast<char *>("OOOs"), fget.ptr() ? fget.ptr() : Py_None,
                                  fset.ptr() ? fset.ptr() : Py_None, Py_None, doc_obj.ptr()), false);
Wenzel Jakob's avatar
Wenzel Jakob committed
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
        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 {
Wenzel Jakob's avatar
Wenzel Jakob committed
811
            auto it = entries->find((int) value);
Wenzel Jakob's avatar
Wenzel Jakob committed
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
            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)
841
842
template <typename... Args> struct init {
    template <typename Base, typename Holder, typename... Extra> void execute(pybind::class_<Base, Holder> &class_, Extra&&... extra) const {
Wenzel Jakob's avatar
Wenzel Jakob committed
843
        /// Function which calls a specific C++ in-place constructor
844
        class_.def("__init__", [](Base *instance, Args... args) { new (instance) Base(args...); }, std::forward<Extra>(extra)...);
Wenzel Jakob's avatar
Wenzel Jakob committed
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
    }
};
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;
    };
    auto & registered_types = detail::get_internals().registered_types;
863
    auto it = registered_types.find(&typeid(OutputType));
Wenzel Jakob's avatar
Wenzel Jakob committed
864
    if (it == registered_types.end())
865
        throw std::runtime_error("implicitly_convertible: Unable to find type " + type_id<OutputType>());
Wenzel Jakob's avatar
Wenzel Jakob committed
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
    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)
889
#elif defined(__GNUG__) and !defined(__clang__)
890
#pragma GCC diagnostic pop
Wenzel Jakob's avatar
Wenzel Jakob committed
891
#endif
892