Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 1 | /* |
Wenzel Jakob | 8f4eb00 | 2015-10-15 18:13:33 +0200 | [diff] [blame] | 2 | pybind11/pybind11.h: Main header file of the C++11 python binding generator library |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 3 | |
| 4 | Copyright (c) 2015 Wenzel Jakob <wenzel@inf.ethz.ch> |
| 5 | |
| 6 | All rights reserved. Use of this source code is governed by a |
| 7 | BSD-style license that can be found in the LICENSE file. |
| 8 | */ |
| 9 | |
Wenzel Jakob | bd4a529 | 2015-07-11 17:41:48 +0200 | [diff] [blame] | 10 | #pragma once |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 11 | |
| 12 | #if defined(_MSC_VER) |
| 13 | #pragma warning(push) |
| 14 | #pragma warning(disable: 4127) // warning C4127: Conditional expression is constant |
| 15 | #pragma warning(disable: 4800) // warning C4800: 'int': forcing value to bool 'true' or 'false' (performance warning) |
| 16 | #pragma warning(disable: 4996) // warning C4996: The POSIX name for this item is deprecated. Instead, use the ISO C and C++ conformant name |
| 17 | #pragma warning(disable: 4100) // warning C4100: Unreferenced formal parameter |
| 18 | #pragma warning(disable: 4512) // warning C4512: Assignment operator was implicitly defined as deleted |
Wenzel Jakob | cd5cda7 | 2015-08-03 12:17:54 +0200 | [diff] [blame] | 19 | #elif defined(__GNUG__) and !defined(__clang__) |
Wenzel Jakob | 281aa0e | 2015-07-30 15:29:00 +0200 | [diff] [blame] | 20 | #pragma GCC diagnostic push |
| 21 | #pragma GCC diagnostic ignored "-Wunused-but-set-parameter" |
| 22 | #pragma GCC diagnostic ignored "-Wunused-but-set-variable" |
| 23 | #pragma GCC diagnostic ignored "-Wmissing-field-initializers" |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 24 | #endif |
| 25 | |
Wenzel Jakob | 8f4eb00 | 2015-10-15 18:13:33 +0200 | [diff] [blame] | 26 | #include "cast.h" |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 27 | |
Wenzel Jakob | 8f4eb00 | 2015-10-15 18:13:33 +0200 | [diff] [blame] | 28 | NAMESPACE_BEGIN(pybind11) |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 29 | |
Wenzel Jakob | 7186783 | 2015-07-29 17:43:52 +0200 | [diff] [blame] | 30 | template <typename T> struct arg_t; |
| 31 | |
| 32 | /// Annotation for keyword arguments |
| 33 | struct arg { |
| 34 | arg(const char *name) : name(name) { } |
Wenzel Jakob | 5428930 | 2015-10-26 20:10:24 +0100 | [diff] [blame] | 35 | template <typename T> arg_t<T> operator=(const T &value); |
Wenzel Jakob | 7186783 | 2015-07-29 17:43:52 +0200 | [diff] [blame] | 36 | const char *name; |
| 37 | }; |
| 38 | |
| 39 | /// Annotation for keyword arguments with default values |
| 40 | template <typename T> struct arg_t : public arg { |
Wenzel Jakob | 66c9a40 | 2016-01-17 22:36:36 +0100 | [diff] [blame] | 41 | arg_t(const char *name, const T &value, const char *descr = nullptr) |
| 42 | : arg(name), value(value), descr(descr) { } |
Wenzel Jakob | 7186783 | 2015-07-29 17:43:52 +0200 | [diff] [blame] | 43 | T value; |
Wenzel Jakob | 66c9a40 | 2016-01-17 22:36:36 +0100 | [diff] [blame] | 44 | const char *descr; |
Wenzel Jakob | 7186783 | 2015-07-29 17:43:52 +0200 | [diff] [blame] | 45 | }; |
Wenzel Jakob | 2ac5044 | 2016-01-17 22:36:35 +0100 | [diff] [blame] | 46 | |
Wenzel Jakob | 5428930 | 2015-10-26 20:10:24 +0100 | [diff] [blame] | 47 | template <typename T> arg_t<T> arg::operator=(const T &value) { return arg_t<T>(name, value); } |
Wenzel Jakob | 7186783 | 2015-07-29 17:43:52 +0200 | [diff] [blame] | 48 | |
| 49 | /// Annotation for methods |
Wenzel Jakob | 5428930 | 2015-10-26 20:10:24 +0100 | [diff] [blame] | 50 | struct is_method { PyObject *class_; is_method(object *o) : class_(o->ptr()) { } }; |
Wenzel Jakob | 7186783 | 2015-07-29 17:43:52 +0200 | [diff] [blame] | 51 | |
| 52 | /// Annotation for documentation |
| 53 | struct doc { const char *value; doc(const char *value) : value(value) { } }; |
| 54 | |
| 55 | /// Annotation for function names |
| 56 | struct name { const char *value; name(const char *value) : value(value) { } }; |
| 57 | |
| 58 | /// Annotation for function siblings |
| 59 | struct sibling { PyObject *value; sibling(handle value) : value(value.ptr()) { } }; |
| 60 | |
Wenzel Jakob | 5f218b3 | 2016-01-17 22:36:39 +0100 | [diff] [blame] | 61 | /// Keep patient alive while nurse lives |
| 62 | template <int Nurse, int Patient> struct keep_alive { }; |
| 63 | |
| 64 | NAMESPACE_BEGIN(detail) |
| 65 | template <typename... Args> struct process_dynamic; |
| 66 | template <typename T> struct process_dynamic<T> { |
| 67 | static void precall(PyObject *) { } |
| 68 | static void postcall(PyObject *, PyObject *) { } |
| 69 | }; |
| 70 | template <> struct process_dynamic<> : public process_dynamic<void> { }; |
| 71 | template <typename T, typename... Args> struct process_dynamic<T, Args...> { |
| 72 | static void precall(PyObject *arg) { |
| 73 | process_dynamic<T>::precall(arg); |
| 74 | process_dynamic<Args...>::precall(arg); |
| 75 | } |
| 76 | static void postcall(PyObject *arg, PyObject *ret) { |
| 77 | process_dynamic<T>::postcall(arg, ret); |
| 78 | process_dynamic<Args...>::postcall(arg, ret); |
| 79 | } |
| 80 | }; |
| 81 | NAMESPACE_END(detail) |
| 82 | |
Wenzel Jakob | d4258ba | 2015-07-26 16:33:49 +0200 | [diff] [blame] | 83 | /// Wraps an arbitrary C++ function/method/lambda function/.. into a callable Python object |
Wenzel Jakob | bd4a529 | 2015-07-11 17:41:48 +0200 | [diff] [blame] | 84 | class cpp_function : public function { |
Wenzel Jakob | d4258ba | 2015-07-26 16:33:49 +0200 | [diff] [blame] | 85 | private: |
Wenzel Jakob | 2ac5044 | 2016-01-17 22:36:35 +0100 | [diff] [blame] | 86 | /// Linked list of function overloads |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 87 | struct function_entry { |
Wenzel Jakob | 2ac5044 | 2016-01-17 22:36:35 +0100 | [diff] [blame] | 88 | /// Function name and user-specified documentation string |
Wenzel Jakob | 66c9a40 | 2016-01-17 22:36:36 +0100 | [diff] [blame] | 89 | char *name = nullptr, *doc = nullptr; /* why no C++ strings? They generate heavier code.. */ |
| 90 | /// Human-readable version of the function signature |
| 91 | char *signature = nullptr; |
Wenzel Jakob | 2ac5044 | 2016-01-17 22:36:35 +0100 | [diff] [blame] | 92 | /// List of registered keyword arguments |
Wenzel Jakob | 66c9a40 | 2016-01-17 22:36:36 +0100 | [diff] [blame] | 93 | std::vector<detail::argument_entry> args; |
| 94 | /// Pointer to lambda function which converts arguments and performs the actual call |
Wenzel Jakob | 2ac5044 | 2016-01-17 22:36:35 +0100 | [diff] [blame] | 95 | PyObject * (*impl) (function_entry *, PyObject *, PyObject *) = nullptr; |
| 96 | /// Storage for the wrapped function pointer and captured data, if any |
Wenzel Jakob | 281aa0e | 2015-07-30 15:29:00 +0200 | [diff] [blame] | 97 | void *data = nullptr; |
Wenzel Jakob | 2ac5044 | 2016-01-17 22:36:35 +0100 | [diff] [blame] | 98 | /// Pointer to custom destructor for 'data' (if needed) |
Wenzel Jakob | 66c9a40 | 2016-01-17 22:36:36 +0100 | [diff] [blame] | 99 | void (*free_data) (void *ptr) = nullptr; |
Wenzel Jakob | 2ac5044 | 2016-01-17 22:36:35 +0100 | [diff] [blame] | 100 | /// Return value policy associated with this function |
Wenzel Jakob | 7186783 | 2015-07-29 17:43:52 +0200 | [diff] [blame] | 101 | return_value_policy policy = return_value_policy::automatic; |
Wenzel Jakob | 2ac5044 | 2016-01-17 22:36:35 +0100 | [diff] [blame] | 102 | /// True if name == '__init__' |
| 103 | bool is_constructor = false; |
| 104 | /// Python method object |
| 105 | PyMethodDef *def = nullptr; |
| 106 | /// Pointer to class (if this is method) |
Wenzel Jakob | 5708221 | 2015-09-04 23:42:12 +0200 | [diff] [blame] | 107 | PyObject *class_ = nullptr; |
Wenzel Jakob | 2ac5044 | 2016-01-17 22:36:35 +0100 | [diff] [blame] | 108 | /// Pointer to first registered function in overload chain |
Wenzel Jakob | 7186783 | 2015-07-29 17:43:52 +0200 | [diff] [blame] | 109 | PyObject *sibling = nullptr; |
Wenzel Jakob | 2ac5044 | 2016-01-17 22:36:35 +0100 | [diff] [blame] | 110 | /// Pointer to next overload |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 111 | function_entry *next = nullptr; |
| 112 | }; |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 113 | |
Wenzel Jakob | 5708221 | 2015-09-04 23:42:12 +0200 | [diff] [blame] | 114 | function_entry *m_entry; |
| 115 | |
Wenzel Jakob | d4258ba | 2015-07-26 16:33:49 +0200 | [diff] [blame] | 116 | /// Picks a suitable return value converter from cast.h |
| 117 | template <typename T> using return_value_caster = |
| 118 | detail::type_caster<typename std::conditional< |
Wenzel Jakob | 4177ed4 | 2016-01-17 22:36:38 +0100 | [diff] [blame] | 119 | std::is_void<T>::value, detail::void_type, typename detail::intrinsic_type<T>::type>::type>; |
Wenzel Jakob | d4258ba | 2015-07-26 16:33:49 +0200 | [diff] [blame] | 120 | |
| 121 | /// Picks a suitable argument value converter from cast.h |
Wenzel Jakob | 7186783 | 2015-07-29 17:43:52 +0200 | [diff] [blame] | 122 | template <typename... T> using arg_value_caster = |
Wenzel Jakob | d4258ba | 2015-07-26 16:33:49 +0200 | [diff] [blame] | 123 | detail::type_caster<typename std::tuple<T...>>; |
Wenzel Jakob | 7186783 | 2015-07-29 17:43:52 +0200 | [diff] [blame] | 124 | |
Wenzel Jakob | b2c2c79 | 2016-01-17 22:36:40 +0100 | [diff] [blame] | 125 | template <typename... T> static void process_static(const std::tuple<T...> &args, function_entry *entry) { |
| 126 | process_static(args, entry, typename detail::make_index_sequence<sizeof...(T)>::type()); |
Wenzel Jakob | 7186783 | 2015-07-29 17:43:52 +0200 | [diff] [blame] | 127 | } |
| 128 | |
Wenzel Jakob | b2c2c79 | 2016-01-17 22:36:40 +0100 | [diff] [blame] | 129 | template <typename... T, size_t ... Index> static void process_static(const std::tuple<T...> &args, |
Wenzel Jakob | 2ac5044 | 2016-01-17 22:36:35 +0100 | [diff] [blame] | 130 | function_entry *entry, detail::index_sequence<Index...>) { |
Wenzel Jakob | b2c2c79 | 2016-01-17 22:36:40 +0100 | [diff] [blame] | 131 | int unused[] = { 0, (process_static(std::get<Index>(args), entry), 0)... }; |
Wenzel Jakob | 7186783 | 2015-07-29 17:43:52 +0200 | [diff] [blame] | 132 | (void) unused; |
| 133 | } |
| 134 | |
Wenzel Jakob | 5f218b3 | 2016-01-17 22:36:39 +0100 | [diff] [blame] | 135 | template <int Nurse, int Patient> |
Wenzel Jakob | b2c2c79 | 2016-01-17 22:36:40 +0100 | [diff] [blame] | 136 | static void process_static(const keep_alive<Nurse, Patient> &, function_entry *) { } |
| 137 | static void process_static(const char *doc, function_entry *entry) { entry->doc = (char *) doc; } |
| 138 | static void process_static(const pybind11::doc &d, function_entry *entry) { entry->doc = (char *) d.value; } |
| 139 | static void process_static(const pybind11::name &n, function_entry *entry) { entry->name = (char *) n.value; } |
| 140 | static void process_static(const pybind11::return_value_policy p, function_entry *entry) { entry->policy = p; } |
| 141 | static void process_static(const pybind11::sibling s, function_entry *entry) { entry->sibling = s.value; } |
| 142 | static void process_static(const pybind11::is_method &m, function_entry *entry) { entry->class_ = m.class_; } |
| 143 | static void process_static(const pybind11::arg &a, function_entry *entry) { |
Wenzel Jakob | 2ac5044 | 2016-01-17 22:36:35 +0100 | [diff] [blame] | 144 | if (entry->class_ && entry->args.empty()) |
Wenzel Jakob | 66c9a40 | 2016-01-17 22:36:36 +0100 | [diff] [blame] | 145 | entry->args.emplace_back("self", nullptr, nullptr); |
| 146 | entry->args.emplace_back(a.name, nullptr, nullptr); |
Wenzel Jakob | a576e6a | 2015-07-29 17:51:54 +0200 | [diff] [blame] | 147 | } |
Wenzel Jakob | a2f6fde | 2015-10-01 16:46:03 +0200 | [diff] [blame] | 148 | |
Wenzel Jakob | a576e6a | 2015-07-29 17:51:54 +0200 | [diff] [blame] | 149 | template <typename T> |
Wenzel Jakob | b2c2c79 | 2016-01-17 22:36:40 +0100 | [diff] [blame] | 150 | static void process_static(const pybind11::arg_t<T> &a, function_entry *entry) { |
Wenzel Jakob | 2ac5044 | 2016-01-17 22:36:35 +0100 | [diff] [blame] | 151 | if (entry->class_ && entry->args.empty()) |
Wenzel Jakob | 66c9a40 | 2016-01-17 22:36:36 +0100 | [diff] [blame] | 152 | entry->args.emplace_back("self", nullptr, nullptr); |
Wenzel Jakob | a576e6a | 2015-07-29 17:51:54 +0200 | [diff] [blame] | 153 | |
Wenzel Jakob | 4177ed4 | 2016-01-17 22:36:38 +0100 | [diff] [blame] | 154 | PyObject *obj = detail::type_caster<typename detail::intrinsic_type<T>::type>::cast( |
Wenzel Jakob | a576e6a | 2015-07-29 17:51:54 +0200 | [diff] [blame] | 155 | a.value, return_value_policy::automatic, nullptr); |
Wenzel Jakob | 2ac5044 | 2016-01-17 22:36:35 +0100 | [diff] [blame] | 156 | |
Wenzel Jakob | 66c9a40 | 2016-01-17 22:36:36 +0100 | [diff] [blame] | 157 | if (obj == nullptr) |
| 158 | throw std::runtime_error("arg(): could not convert default keyword " |
| 159 | "argument into a Python object (type not " |
| 160 | "registered yet?)"); |
| 161 | |
| 162 | entry->args.emplace_back(a.name, a.descr, obj); |
Wenzel Jakob | a576e6a | 2015-07-29 17:51:54 +0200 | [diff] [blame] | 163 | } |
Wenzel Jakob | d4258ba | 2015-07-26 16:33:49 +0200 | [diff] [blame] | 164 | public: |
Wenzel Jakob | bd4a529 | 2015-07-11 17:41:48 +0200 | [diff] [blame] | 165 | cpp_function() { } |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 166 | |
Wenzel Jakob | d4258ba | 2015-07-26 16:33:49 +0200 | [diff] [blame] | 167 | /// Vanilla function pointers |
Wenzel Jakob | 66c9a40 | 2016-01-17 22:36:36 +0100 | [diff] [blame] | 168 | template <typename Return, typename... Args, typename... Extra> |
| 169 | cpp_function(Return (*f)(Args...), Extra&&... extra) { |
| 170 | using detail::descr; |
Wenzel Jakob | 5708221 | 2015-09-04 23:42:12 +0200 | [diff] [blame] | 171 | m_entry = new function_entry(); |
Wenzel Jakob | 2ac5044 | 2016-01-17 22:36:35 +0100 | [diff] [blame] | 172 | m_entry->data = (void *) f; |
Wenzel Jakob | 7186783 | 2015-07-29 17:43:52 +0200 | [diff] [blame] | 173 | |
Wenzel Jakob | 66c9a40 | 2016-01-17 22:36:36 +0100 | [diff] [blame] | 174 | typedef arg_value_caster<Args...> cast_in; |
Wenzel Jakob | 7186783 | 2015-07-29 17:43:52 +0200 | [diff] [blame] | 175 | typedef return_value_caster<Return> cast_out; |
| 176 | |
Wenzel Jakob | 2ac5044 | 2016-01-17 22:36:35 +0100 | [diff] [blame] | 177 | m_entry->impl = [](function_entry *entry, PyObject *pyArgs, PyObject *parent) -> PyObject * { |
Wenzel Jakob | 7186783 | 2015-07-29 17:43:52 +0200 | [diff] [blame] | 178 | cast_in args; |
Wenzel Jakob | 2ac5044 | 2016-01-17 22:36:35 +0100 | [diff] [blame] | 179 | if (!args.load(pyArgs, true)) |
Wenzel Jakob | b3ee3ea | 2015-10-04 15:17:34 +0200 | [diff] [blame] | 180 | return (PyObject *) 1; /* Special return code: try next overload */ |
Wenzel Jakob | 5f218b3 | 2016-01-17 22:36:39 +0100 | [diff] [blame] | 181 | detail::process_dynamic<Extra...>::precall(pyArgs); |
| 182 | PyObject *result = cast_out::cast(args.template call<Return>((Return (*)(Args...)) entry->data), entry->policy, parent); |
| 183 | detail::process_dynamic<Extra...>::postcall(pyArgs, result); |
| 184 | return result; |
Wenzel Jakob | 7186783 | 2015-07-29 17:43:52 +0200 | [diff] [blame] | 185 | }; |
| 186 | |
Wenzel Jakob | b2c2c79 | 2016-01-17 22:36:40 +0100 | [diff] [blame] | 187 | process_static(std::make_tuple(std::forward<Extra>(extra)...), m_entry); |
Wenzel Jakob | 66c9a40 | 2016-01-17 22:36:36 +0100 | [diff] [blame] | 188 | PYBIND11_DESCR signature = cast_in::name() + detail::_(" -> ") + cast_out::name(); |
| 189 | initialize(signature.text(), signature.types(), sizeof...(Args)); |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 190 | } |
Wenzel Jakob | d4258ba | 2015-07-26 16:33:49 +0200 | [diff] [blame] | 191 | |
| 192 | /// Delegating helper constructor to deal with lambda functions |
Wenzel Jakob | 7186783 | 2015-07-29 17:43:52 +0200 | [diff] [blame] | 193 | template <typename Func, typename... Extra> cpp_function(Func &&f, Extra&&... extra) { |
| 194 | initialize(std::forward<Func>(f), |
Wenzel Jakob | d4258ba | 2015-07-26 16:33:49 +0200 | [diff] [blame] | 195 | (typename detail::remove_class<decltype( |
Wenzel Jakob | 7186783 | 2015-07-29 17:43:52 +0200 | [diff] [blame] | 196 | &std::remove_reference<Func>::type::operator())>::type *) nullptr, |
| 197 | std::forward<Extra>(extra)...); |
Wenzel Jakob | d4258ba | 2015-07-26 16:33:49 +0200 | [diff] [blame] | 198 | } |
| 199 | |
Wenzel Jakob | d4258ba | 2015-07-26 16:33:49 +0200 | [diff] [blame] | 200 | /// Class methods (non-const) |
Wenzel Jakob | 7186783 | 2015-07-29 17:43:52 +0200 | [diff] [blame] | 201 | template <typename Return, typename Class, typename... Arg, typename... Extra> cpp_function( |
| 202 | Return (Class::*f)(Arg...), Extra&&... extra) { |
| 203 | initialize([f](Class *c, Arg... args) -> Return { return (c->*f)(args...); }, |
| 204 | (Return (*) (Class *, Arg...)) nullptr, std::forward<Extra>(extra)...); |
Wenzel Jakob | d4258ba | 2015-07-26 16:33:49 +0200 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | /// Class methods (const) |
Wenzel Jakob | 7186783 | 2015-07-29 17:43:52 +0200 | [diff] [blame] | 208 | template <typename Return, typename Class, typename... Arg, typename... Extra> cpp_function( |
| 209 | Return (Class::*f)(Arg...) const, Extra&&... extra) { |
| 210 | initialize([f](const Class *c, Arg... args) -> Return { return (c->*f)(args...); }, |
| 211 | (Return (*)(const Class *, Arg ...)) nullptr, std::forward<Extra>(extra)...); |
Wenzel Jakob | d4258ba | 2015-07-26 16:33:49 +0200 | [diff] [blame] | 212 | } |
| 213 | |
Wenzel Jakob | 5708221 | 2015-09-04 23:42:12 +0200 | [diff] [blame] | 214 | /// Return the function name |
| 215 | const char *name() const { return m_entry->name; } |
| 216 | |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 217 | private: |
Wenzel Jakob | d4258ba | 2015-07-26 16:33:49 +0200 | [diff] [blame] | 218 | /// Functors, lambda functions, etc. |
Wenzel Jakob | 66c9a40 | 2016-01-17 22:36:36 +0100 | [diff] [blame] | 219 | template <typename Func, typename Return, typename... Args, typename... Extra> |
| 220 | void initialize(Func &&f, Return (*)(Args...), Extra&&... extra) { |
| 221 | using detail::descr; |
| 222 | |
| 223 | struct capture { typename std::remove_reference<Func>::type f; }; |
Wenzel Jakob | d4258ba | 2015-07-26 16:33:49 +0200 | [diff] [blame] | 224 | |
Wenzel Jakob | 5708221 | 2015-09-04 23:42:12 +0200 | [diff] [blame] | 225 | m_entry = new function_entry(); |
Wenzel Jakob | 2ac5044 | 2016-01-17 22:36:35 +0100 | [diff] [blame] | 226 | m_entry->data = new capture { std::forward<Func>(f) }; |
Wenzel Jakob | 7186783 | 2015-07-29 17:43:52 +0200 | [diff] [blame] | 227 | |
Wenzel Jakob | 19208fe | 2015-10-13 17:37:25 +0200 | [diff] [blame] | 228 | if (!std::is_trivially_destructible<Func>::value) |
Wenzel Jakob | 66c9a40 | 2016-01-17 22:36:36 +0100 | [diff] [blame] | 229 | m_entry->free_data = [](void *ptr) { delete (capture *) ptr; }; |
Wenzel Jakob | 2ac5044 | 2016-01-17 22:36:35 +0100 | [diff] [blame] | 230 | else |
Wenzel Jakob | 66c9a40 | 2016-01-17 22:36:36 +0100 | [diff] [blame] | 231 | m_entry->free_data = operator delete; |
Wenzel Jakob | 19208fe | 2015-10-13 17:37:25 +0200 | [diff] [blame] | 232 | |
Wenzel Jakob | 66c9a40 | 2016-01-17 22:36:36 +0100 | [diff] [blame] | 233 | typedef arg_value_caster<Args...> cast_in; |
Wenzel Jakob | 7186783 | 2015-07-29 17:43:52 +0200 | [diff] [blame] | 234 | typedef return_value_caster<Return> cast_out; |
| 235 | |
Wenzel Jakob | 2ac5044 | 2016-01-17 22:36:35 +0100 | [diff] [blame] | 236 | m_entry->impl = [](function_entry *entry, PyObject *pyArgs, PyObject *parent) -> PyObject *{ |
Wenzel Jakob | 7186783 | 2015-07-29 17:43:52 +0200 | [diff] [blame] | 237 | cast_in args; |
Wenzel Jakob | 2ac5044 | 2016-01-17 22:36:35 +0100 | [diff] [blame] | 238 | if (!args.load(pyArgs, true)) |
Wenzel Jakob | b3ee3ea | 2015-10-04 15:17:34 +0200 | [diff] [blame] | 239 | return (PyObject *) 1; /* Special return code: try next overload */ |
Wenzel Jakob | 5f218b3 | 2016-01-17 22:36:39 +0100 | [diff] [blame] | 240 | detail::process_dynamic<Extra...>::precall(pyArgs); |
| 241 | PyObject *result = cast_out::cast(args.template call<Return>(((capture *) entry->data)->f), entry->policy, parent); |
| 242 | detail::process_dynamic<Extra...>::postcall(pyArgs, result); |
| 243 | return result; |
Wenzel Jakob | 7186783 | 2015-07-29 17:43:52 +0200 | [diff] [blame] | 244 | }; |
| 245 | |
Wenzel Jakob | b2c2c79 | 2016-01-17 22:36:40 +0100 | [diff] [blame] | 246 | process_static(std::make_tuple(std::forward<Extra>(extra)...), m_entry); |
Wenzel Jakob | 66c9a40 | 2016-01-17 22:36:36 +0100 | [diff] [blame] | 247 | PYBIND11_DESCR signature = cast_in::name() + detail::_(" -> ") + cast_out::name(); |
| 248 | initialize(signature.text(), signature.types(), sizeof...(Args)); |
Wenzel Jakob | d4258ba | 2015-07-26 16:33:49 +0200 | [diff] [blame] | 249 | } |
| 250 | |
Wenzel Jakob | 5257330 | 2015-09-14 16:38:47 +0200 | [diff] [blame] | 251 | static PyObject *dispatcher(PyObject *self, PyObject *args, PyObject *kwargs) { |
Wenzel Jakob | 2ac5044 | 2016-01-17 22:36:35 +0100 | [diff] [blame] | 252 | function_entry *overloads = (function_entry *) PyCapsule_GetPointer(self, nullptr), |
| 253 | *it = overloads; |
| 254 | int nargs = (int) PyTuple_Size(args), |
| 255 | nkwargs = kwargs ? (int) PyDict_Size(kwargs) : 0; |
| 256 | PyObject *parent = nargs > 0 ? PyTuple_GetItem(args, 0) : nullptr, |
| 257 | *result = (PyObject *) 1; |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 258 | try { |
Wenzel Jakob | b3ee3ea | 2015-10-04 15:17:34 +0200 | [diff] [blame] | 259 | for (; it != nullptr; it = it->next) { |
Wenzel Jakob | f4671f6 | 2016-01-17 22:36:36 +0100 | [diff] [blame] | 260 | object args_(args, true); |
Wenzel Jakob | 2ac5044 | 2016-01-17 22:36:35 +0100 | [diff] [blame] | 261 | int kwargs_consumed = 0; |
Wenzel Jakob | a576e6a | 2015-07-29 17:51:54 +0200 | [diff] [blame] | 262 | |
Wenzel Jakob | 2ac5044 | 2016-01-17 22:36:35 +0100 | [diff] [blame] | 263 | if (nargs < (int) it->args.size()) { |
Wenzel Jakob | f4671f6 | 2016-01-17 22:36:36 +0100 | [diff] [blame] | 264 | args_ = object(PyTuple_New(it->args.size()), false); |
Wenzel Jakob | 2ac5044 | 2016-01-17 22:36:35 +0100 | [diff] [blame] | 265 | for (int i = 0; i < nargs; ++i) { |
Wenzel Jakob | a576e6a | 2015-07-29 17:51:54 +0200 | [diff] [blame] | 266 | PyObject *item = PyTuple_GET_ITEM(args, i); |
| 267 | Py_INCREF(item); |
Wenzel Jakob | f4671f6 | 2016-01-17 22:36:36 +0100 | [diff] [blame] | 268 | PyTuple_SET_ITEM(args_.ptr(), i, item); |
Wenzel Jakob | a576e6a | 2015-07-29 17:51:54 +0200 | [diff] [blame] | 269 | } |
Wenzel Jakob | 2ac5044 | 2016-01-17 22:36:35 +0100 | [diff] [blame] | 270 | int arg_ctr = 0; |
Wenzel Jakob | 66c9a40 | 2016-01-17 22:36:36 +0100 | [diff] [blame] | 271 | for (auto const &it2 : it->args) { |
Wenzel Jakob | 2ac5044 | 2016-01-17 22:36:35 +0100 | [diff] [blame] | 272 | int index = arg_ctr++; |
Wenzel Jakob | f4671f6 | 2016-01-17 22:36:36 +0100 | [diff] [blame] | 273 | if (PyTuple_GET_ITEM(args_.ptr(), index)) |
Wenzel Jakob | 2ac5044 | 2016-01-17 22:36:35 +0100 | [diff] [blame] | 274 | continue; |
| 275 | PyObject *value = nullptr; |
| 276 | if (kwargs) |
Wenzel Jakob | 66c9a40 | 2016-01-17 22:36:36 +0100 | [diff] [blame] | 277 | value = PyDict_GetItemString(kwargs, it2.name); |
Wenzel Jakob | 2ac5044 | 2016-01-17 22:36:35 +0100 | [diff] [blame] | 278 | if (value) |
| 279 | kwargs_consumed++; |
Wenzel Jakob | 66c9a40 | 2016-01-17 22:36:36 +0100 | [diff] [blame] | 280 | else if (it2.value) |
| 281 | value = it2.value; |
Wenzel Jakob | 2ac5044 | 2016-01-17 22:36:35 +0100 | [diff] [blame] | 282 | if (value) { |
| 283 | Py_INCREF(value); |
Wenzel Jakob | f4671f6 | 2016-01-17 22:36:36 +0100 | [diff] [blame] | 284 | PyTuple_SET_ITEM(args_.ptr(), index, value); |
Wenzel Jakob | 2ac5044 | 2016-01-17 22:36:35 +0100 | [diff] [blame] | 285 | } else { |
| 286 | kwargs_consumed = -1; /* definite failure */ |
| 287 | break; |
| 288 | } |
| 289 | } |
Wenzel Jakob | a576e6a | 2015-07-29 17:51:54 +0200 | [diff] [blame] | 290 | } |
| 291 | |
Wenzel Jakob | 2ac5044 | 2016-01-17 22:36:35 +0100 | [diff] [blame] | 292 | if (kwargs_consumed == nkwargs) |
Wenzel Jakob | f4671f6 | 2016-01-17 22:36:36 +0100 | [diff] [blame] | 293 | result = it->impl(it, args_.ptr(), parent); |
Wenzel Jakob | a576e6a | 2015-07-29 17:51:54 +0200 | [diff] [blame] | 294 | |
Wenzel Jakob | b3ee3ea | 2015-10-04 15:17:34 +0200 | [diff] [blame] | 295 | if (result != (PyObject *) 1) |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 296 | break; |
| 297 | } |
Jonas Adler | 2b9fdbe | 2015-12-15 11:27:19 +0100 | [diff] [blame] | 298 | } catch (const error_already_set &) { return nullptr; |
| 299 | } catch (const index_error &e) { PyErr_SetString(PyExc_IndexError, e.what()); return nullptr; |
| 300 | } catch (const stop_iteration &e) { PyErr_SetString(PyExc_StopIteration, e.what()); return nullptr; |
| 301 | } catch (const std::bad_alloc &e) { PyErr_SetString(PyExc_MemoryError, e.what()); return nullptr; |
| 302 | } catch (const std::domain_error &e) { PyErr_SetString(PyExc_ValueError, e.what()); return nullptr; |
| 303 | } catch (const std::invalid_argument &e) { PyErr_SetString(PyExc_ValueError, e.what()); return nullptr; |
| 304 | } catch (const std::length_error &e) { PyErr_SetString(PyExc_ValueError, e.what()); return nullptr; |
| 305 | } catch (const std::out_of_range &e) { PyErr_SetString(PyExc_IndexError, e.what()); return nullptr; |
| 306 | } catch (const std::range_error &e) { PyErr_SetString(PyExc_ValueError, e.what()); return nullptr; |
| 307 | } catch (const std::exception &e) { PyErr_SetString(PyExc_RuntimeError, e.what()); return nullptr; |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 308 | } catch (...) { |
| 309 | PyErr_SetString(PyExc_RuntimeError, "Caught an unknown exception!"); |
| 310 | return nullptr; |
| 311 | } |
Wenzel Jakob | b3ee3ea | 2015-10-04 15:17:34 +0200 | [diff] [blame] | 312 | if (result == (PyObject *) 1) { |
| 313 | std::string msg = "Incompatible function arguments. The " |
| 314 | "following argument types are supported:\n"; |
| 315 | int ctr = 0; |
Wenzel Jakob | d2a902b | 2015-10-04 20:24:20 +0200 | [diff] [blame] | 316 | for (function_entry *it2 = overloads; it2 != nullptr; it2 = it2->next) { |
Wenzel Jakob | b3ee3ea | 2015-10-04 15:17:34 +0200 | [diff] [blame] | 317 | msg += " "+ std::to_string(++ctr) + ". "; |
Wenzel Jakob | 66c9a40 | 2016-01-17 22:36:36 +0100 | [diff] [blame] | 318 | msg += it2->signature; |
Wenzel Jakob | b3ee3ea | 2015-10-04 15:17:34 +0200 | [diff] [blame] | 319 | msg += "\n"; |
| 320 | } |
| 321 | PyErr_SetString(PyExc_TypeError, msg.c_str()); |
| 322 | return nullptr; |
| 323 | } else if (result == nullptr) { |
| 324 | std::string msg = "Unable to convert function return value to a " |
| 325 | "Python type! The signature was\n\t"; |
Wenzel Jakob | 66c9a40 | 2016-01-17 22:36:36 +0100 | [diff] [blame] | 326 | msg += it->signature; |
Wenzel Jakob | b3ee3ea | 2015-10-04 15:17:34 +0200 | [diff] [blame] | 327 | PyErr_SetString(PyExc_TypeError, msg.c_str()); |
| 328 | return nullptr; |
| 329 | } else { |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 330 | if (overloads->is_constructor) { |
| 331 | PyObject *inst = PyTuple_GetItem(args, 0); |
| 332 | const detail::type_info *type_info = |
| 333 | capsule(PyObject_GetAttrString((PyObject *) Py_TYPE(inst), |
Wenzel Jakob | 8f4eb00 | 2015-10-15 18:13:33 +0200 | [diff] [blame] | 334 | const_cast<char *>("__pybind11__")), false); |
Wenzel Jakob | b2c2c79 | 2016-01-17 22:36:40 +0100 | [diff] [blame] | 335 | type_info->init_holder(inst, nullptr); |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 336 | } |
| 337 | return result; |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 338 | } |
| 339 | } |
| 340 | |
Wenzel Jakob | 281aa0e | 2015-07-30 15:29:00 +0200 | [diff] [blame] | 341 | static void destruct(function_entry *entry) { |
| 342 | while (entry) { |
Wenzel Jakob | 281aa0e | 2015-07-30 15:29:00 +0200 | [diff] [blame] | 343 | function_entry *next = entry->next; |
Wenzel Jakob | 66c9a40 | 2016-01-17 22:36:36 +0100 | [diff] [blame] | 344 | if (entry->free_data) |
| 345 | entry->free_data(entry->data); |
| 346 | std::free((char *) entry->name); |
| 347 | std::free((char *) entry->doc); |
| 348 | std::free((char *) entry->signature); |
| 349 | for (auto &arg: entry->args) { |
| 350 | std::free((char *) arg.name); |
| 351 | std::free((char *) arg.descr); |
| 352 | Py_XDECREF(arg.value); |
| 353 | } |
Wenzel Jakob | 95d1869 | 2016-01-17 22:36:40 +0100 | [diff] [blame^] | 354 | if (entry->def) { |
| 355 | free((char *) entry->def->ml_doc); |
| 356 | delete entry->def; |
| 357 | } |
Wenzel Jakob | 281aa0e | 2015-07-30 15:29:00 +0200 | [diff] [blame] | 358 | delete entry; |
| 359 | entry = next; |
| 360 | } |
| 361 | } |
| 362 | |
Wenzel Jakob | 66c9a40 | 2016-01-17 22:36:36 +0100 | [diff] [blame] | 363 | void initialize(const char *text, const std::type_info * const * types, int args) { |
| 364 | /* Create copies of all referenced C-style strings */ |
| 365 | m_entry->name = strdup(m_entry->name ? m_entry->name : ""); |
| 366 | if (m_entry->doc) m_entry->doc = strdup(m_entry->doc); |
| 367 | for (auto &a: m_entry->args) { |
| 368 | if (a.name) |
| 369 | a.name = strdup(a.name); |
| 370 | if (a.descr) |
| 371 | a.descr = strdup(a.descr); |
| 372 | else if (a.value) |
Wenzel Jakob | 27e8e10 | 2016-01-17 22:36:37 +0100 | [diff] [blame] | 373 | a.descr = strdup(((std::string) ((object) handle(a.value).attr("__repr__")).call().str()).c_str()); |
Wenzel Jakob | 66c9a40 | 2016-01-17 22:36:36 +0100 | [diff] [blame] | 374 | } |
| 375 | auto const ®istered_types = detail::get_internals().registered_types; |
| 376 | |
| 377 | /* Generate a proper function signature */ |
| 378 | std::string signature; |
| 379 | size_t type_depth = 0, char_index = 0, type_index = 0, arg_index = 0; |
| 380 | while (true) { |
| 381 | char c = text[char_index++]; |
| 382 | if (c == '\0') |
| 383 | break; |
| 384 | |
| 385 | if (c == '{') { |
| 386 | if (type_depth == 1 && arg_index < m_entry->args.size()) { |
| 387 | signature += m_entry->args[arg_index].name; |
| 388 | signature += " : "; |
| 389 | } |
| 390 | ++type_depth; |
| 391 | } else if (c == '}') { |
| 392 | --type_depth; |
| 393 | if (type_depth == 1 && arg_index < m_entry->args.size()) { |
| 394 | if (m_entry->args[arg_index].descr) { |
| 395 | signature += " = "; |
| 396 | signature += m_entry->args[arg_index].descr; |
| 397 | } |
| 398 | arg_index++; |
| 399 | } |
| 400 | } else if (c == '%') { |
| 401 | const std::type_info *t = types[type_index++]; |
Wenzel Jakob | b2c2c79 | 2016-01-17 22:36:40 +0100 | [diff] [blame] | 402 | if (!t) |
Wenzel Jakob | 27e8e10 | 2016-01-17 22:36:37 +0100 | [diff] [blame] | 403 | throw std::runtime_error("Internal error while parsing type signature (1)"); |
Wenzel Jakob | 66c9a40 | 2016-01-17 22:36:36 +0100 | [diff] [blame] | 404 | auto it = registered_types.find(t); |
| 405 | if (it != registered_types.end()) { |
| 406 | signature += it->second.type->tp_name; |
| 407 | } else { |
| 408 | std::string tname(t->name()); |
| 409 | detail::clean_type_id(tname); |
| 410 | signature += tname; |
| 411 | } |
| 412 | } else { |
| 413 | signature += c; |
| 414 | } |
| 415 | } |
Wenzel Jakob | 95d1869 | 2016-01-17 22:36:40 +0100 | [diff] [blame^] | 416 | if (type_depth != 0 || types[type_index] != nullptr) |
Wenzel Jakob | 27e8e10 | 2016-01-17 22:36:37 +0100 | [diff] [blame] | 417 | throw std::runtime_error("Internal error while parsing type signature (2)"); |
Wenzel Jakob | 66c9a40 | 2016-01-17 22:36:36 +0100 | [diff] [blame] | 418 | |
| 419 | #if !defined(PYBIND11_CPP14) |
| 420 | delete[] types; |
| 421 | delete[] text; |
| 422 | #endif |
Wenzel Jakob | f5fae92 | 2015-08-24 15:31:24 +0200 | [diff] [blame] | 423 | |
Wenzel Jakob | 5708221 | 2015-09-04 23:42:12 +0200 | [diff] [blame] | 424 | #if PY_MAJOR_VERSION < 3 |
Wenzel Jakob | 66c9a40 | 2016-01-17 22:36:36 +0100 | [diff] [blame] | 425 | if (strcmp(m_entry->name, "__next__") == 0) { |
| 426 | free(m_entry->name); |
| 427 | m_entry->name = strdup("next"); |
| 428 | } |
Wenzel Jakob | 5708221 | 2015-09-04 23:42:12 +0200 | [diff] [blame] | 429 | #endif |
| 430 | |
Wenzel Jakob | 2ac5044 | 2016-01-17 22:36:35 +0100 | [diff] [blame] | 431 | if (!m_entry->args.empty() && (int) m_entry->args.size() != args) |
Wenzel Jakob | a576e6a | 2015-07-29 17:51:54 +0200 | [diff] [blame] | 432 | throw std::runtime_error( |
Wenzel Jakob | 5708221 | 2015-09-04 23:42:12 +0200 | [diff] [blame] | 433 | "cpp_function(): function \"" + std::string(m_entry->name) + "\" takes " + |
Wenzel Jakob | 2ac5044 | 2016-01-17 22:36:35 +0100 | [diff] [blame] | 434 | std::to_string(args) + " arguments, but " + std::to_string(m_entry->args.size()) + |
Wenzel Jakob | 8f4eb00 | 2015-10-15 18:13:33 +0200 | [diff] [blame] | 435 | " pybind11::arg entries were specified!"); |
Wenzel Jakob | bd4a529 | 2015-07-11 17:41:48 +0200 | [diff] [blame] | 436 | |
Wenzel Jakob | 5708221 | 2015-09-04 23:42:12 +0200 | [diff] [blame] | 437 | m_entry->is_constructor = !strcmp(m_entry->name, "__init__"); |
Wenzel Jakob | 66c9a40 | 2016-01-17 22:36:36 +0100 | [diff] [blame] | 438 | m_entry->signature = strdup(signature.c_str()); |
| 439 | m_entry->args.shrink_to_fit(); |
Wenzel Jakob | bd4a529 | 2015-07-11 17:41:48 +0200 | [diff] [blame] | 440 | |
Wenzel Jakob | 5708221 | 2015-09-04 23:42:12 +0200 | [diff] [blame] | 441 | #if PY_MAJOR_VERSION < 3 |
| 442 | if (m_entry->sibling && PyMethod_Check(m_entry->sibling)) |
| 443 | m_entry->sibling = PyMethod_GET_FUNCTION(m_entry->sibling); |
| 444 | #endif |
| 445 | |
Wenzel Jakob | a2f6fde | 2015-10-01 16:46:03 +0200 | [diff] [blame] | 446 | function_entry *s_entry = nullptr, *entry = m_entry; |
| 447 | if (m_entry->sibling && PyCFunction_Check(m_entry->sibling)) { |
| 448 | capsule entry_capsule(PyCFunction_GetSelf(m_entry->sibling), true); |
| 449 | s_entry = (function_entry *) entry_capsule; |
| 450 | if (s_entry->class_ != m_entry->class_) |
Wenzel Jakob | 66c9a40 | 2016-01-17 22:36:36 +0100 | [diff] [blame] | 451 | s_entry = nullptr; /* Overridden method, don't append to parent class overloads */ |
Wenzel Jakob | a2f6fde | 2015-10-01 16:46:03 +0200 | [diff] [blame] | 452 | } |
| 453 | |
Wenzel Jakob | 66c9a40 | 2016-01-17 22:36:36 +0100 | [diff] [blame] | 454 | if (!s_entry) { /* No existing overload was found, create a function object */ |
Wenzel Jakob | a2f6fde | 2015-10-01 16:46:03 +0200 | [diff] [blame] | 455 | m_entry->def = new PyMethodDef(); |
| 456 | memset(m_entry->def, 0, sizeof(PyMethodDef)); |
| 457 | m_entry->def->ml_name = m_entry->name; |
| 458 | m_entry->def->ml_meth = reinterpret_cast<PyCFunction>(*dispatcher); |
| 459 | m_entry->def->ml_flags = METH_VARARGS | METH_KEYWORDS; |
| 460 | capsule entry_capsule(m_entry, [](PyObject *o) { destruct((function_entry *) PyCapsule_GetPointer(o, nullptr)); }); |
| 461 | m_ptr = PyCFunction_New(m_entry->def, entry_capsule.ptr()); |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 462 | if (!m_ptr) |
Wenzel Jakob | bd4a529 | 2015-07-11 17:41:48 +0200 | [diff] [blame] | 463 | throw std::runtime_error("cpp_function::cpp_function(): Could not allocate function object"); |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 464 | } else { |
Wenzel Jakob | a2f6fde | 2015-10-01 16:46:03 +0200 | [diff] [blame] | 465 | m_ptr = m_entry->sibling; |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 466 | inc_ref(); |
Wenzel Jakob | a2f6fde | 2015-10-01 16:46:03 +0200 | [diff] [blame] | 467 | entry = s_entry; |
| 468 | while (s_entry->next) |
| 469 | s_entry = s_entry->next; |
| 470 | s_entry->next = m_entry; |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 471 | } |
Wenzel Jakob | 281aa0e | 2015-07-30 15:29:00 +0200 | [diff] [blame] | 472 | |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 473 | std::string signatures; |
Wenzel Jakob | 7186783 | 2015-07-29 17:43:52 +0200 | [diff] [blame] | 474 | int index = 0; |
| 475 | function_entry *it = entry; |
Wenzel Jakob | 66c9a40 | 2016-01-17 22:36:36 +0100 | [diff] [blame] | 476 | while (it) { /* Create pydoc entry including all function signatures and docstrings of the overload chain */ |
Wenzel Jakob | a2f6fde | 2015-10-01 16:46:03 +0200 | [diff] [blame] | 477 | if (s_entry) |
Wenzel Jakob | 7186783 | 2015-07-29 17:43:52 +0200 | [diff] [blame] | 478 | signatures += std::to_string(++index) + ". "; |
Wenzel Jakob | 66c9a40 | 2016-01-17 22:36:36 +0100 | [diff] [blame] | 479 | signatures += "Signature : "; |
| 480 | signatures += it->signature; |
| 481 | signatures += "\n"; |
| 482 | if (it->doc && strlen(it->doc) > 0) { |
| 483 | signatures += "\n"; |
| 484 | signatures += it->doc; |
| 485 | signatures += "\n"; |
| 486 | } |
Wenzel Jakob | 7186783 | 2015-07-29 17:43:52 +0200 | [diff] [blame] | 487 | if (it->next) |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 488 | signatures += "\n"; |
Wenzel Jakob | 7186783 | 2015-07-29 17:43:52 +0200 | [diff] [blame] | 489 | it = it->next; |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 490 | } |
| 491 | PyCFunctionObject *func = (PyCFunctionObject *) m_ptr; |
| 492 | if (func->m_ml->ml_doc) |
Wenzel Jakob | 66c9a40 | 2016-01-17 22:36:36 +0100 | [diff] [blame] | 493 | free((char *) func->m_ml->ml_doc); |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 494 | func->m_ml->ml_doc = strdup(signatures.c_str()); |
Wenzel Jakob | 2ac5044 | 2016-01-17 22:36:35 +0100 | [diff] [blame] | 495 | if (entry->class_) { |
Wenzel Jakob | 5708221 | 2015-09-04 23:42:12 +0200 | [diff] [blame] | 496 | #if PY_MAJOR_VERSION >= 3 |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 497 | m_ptr = PyInstanceMethod_New(m_ptr); |
Wenzel Jakob | 5708221 | 2015-09-04 23:42:12 +0200 | [diff] [blame] | 498 | #else |
| 499 | m_ptr = PyMethod_New(m_ptr, nullptr, entry->class_); |
| 500 | #endif |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 501 | if (!m_ptr) |
Wenzel Jakob | bd4a529 | 2015-07-11 17:41:48 +0200 | [diff] [blame] | 502 | throw std::runtime_error("cpp_function::cpp_function(): Could not allocate instance method object"); |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 503 | Py_DECREF(func); |
| 504 | } |
| 505 | } |
| 506 | }; |
| 507 | |
| 508 | class module : public object { |
| 509 | public: |
Wenzel Jakob | b1b7140 | 2015-10-18 16:48:30 +0200 | [diff] [blame] | 510 | PYBIND11_OBJECT_DEFAULT(module, object, PyModule_Check) |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 511 | |
| 512 | module(const char *name, const char *doc = nullptr) { |
Wenzel Jakob | 5708221 | 2015-09-04 23:42:12 +0200 | [diff] [blame] | 513 | #if PY_MAJOR_VERSION >= 3 |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 514 | PyModuleDef *def = new PyModuleDef(); |
| 515 | memset(def, 0, sizeof(PyModuleDef)); |
| 516 | def->m_name = name; |
| 517 | def->m_doc = doc; |
| 518 | def->m_size = -1; |
| 519 | Py_INCREF(def); |
| 520 | m_ptr = PyModule_Create(def); |
Wenzel Jakob | 5708221 | 2015-09-04 23:42:12 +0200 | [diff] [blame] | 521 | #else |
| 522 | m_ptr = Py_InitModule3(name, nullptr, doc); |
| 523 | #endif |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 524 | if (m_ptr == nullptr) |
| 525 | throw std::runtime_error("Internal error in module::module()"); |
| 526 | inc_ref(); |
| 527 | } |
| 528 | |
Wenzel Jakob | 7186783 | 2015-07-29 17:43:52 +0200 | [diff] [blame] | 529 | template <typename Func, typename... Extra> |
| 530 | module &def(const char *name_, Func &&f, Extra&& ... extra) { |
| 531 | cpp_function func(std::forward<Func>(f), name(name_), |
| 532 | sibling((handle) attr(name_)), std::forward<Extra>(extra)...); |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 533 | func.inc_ref(); /* The following line steals a reference to 'func' */ |
Wenzel Jakob | 7186783 | 2015-07-29 17:43:52 +0200 | [diff] [blame] | 534 | PyModule_AddObject(ptr(), name_, func.ptr()); |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 535 | return *this; |
| 536 | } |
| 537 | |
Wenzel Jakob | bd4a529 | 2015-07-11 17:41:48 +0200 | [diff] [blame] | 538 | module def_submodule(const char *name, const char *doc = nullptr) { |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 539 | std::string full_name = std::string(PyModule_GetName(m_ptr)) |
| 540 | + std::string(".") + std::string(name); |
| 541 | module result(PyImport_AddModule(full_name.c_str()), true); |
Wenzel Jakob | bd4a529 | 2015-07-11 17:41:48 +0200 | [diff] [blame] | 542 | if (doc) |
Wenzel Jakob | 8f4eb00 | 2015-10-15 18:13:33 +0200 | [diff] [blame] | 543 | result.attr("__doc__") = pybind11::str(doc); |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 544 | attr(name) = result; |
| 545 | return result; |
| 546 | } |
Wenzel Jakob | db028d6 | 2015-10-13 23:44:25 +0200 | [diff] [blame] | 547 | |
| 548 | static module import(const char *name) { |
Wenzel Jakob | dd57a34 | 2015-12-26 14:04:52 +0100 | [diff] [blame] | 549 | PyObject *obj = PyImport_ImportModule(name); |
| 550 | if (!obj) |
| 551 | throw std::runtime_error("Module \"" + std::string(name) + "\" not found!"); |
| 552 | return module(obj, false); |
Wenzel Jakob | db028d6 | 2015-10-13 23:44:25 +0200 | [diff] [blame] | 553 | } |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 554 | }; |
| 555 | |
| 556 | NAMESPACE_BEGIN(detail) |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 557 | /// Basic support for creating new Python heap types |
| 558 | class custom_type : public object { |
| 559 | public: |
Wenzel Jakob | b1b7140 | 2015-10-18 16:48:30 +0200 | [diff] [blame] | 560 | PYBIND11_OBJECT_DEFAULT(custom_type, object, PyType_Check) |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 561 | |
Wenzel Jakob | f5fae92 | 2015-08-24 15:31:24 +0200 | [diff] [blame] | 562 | custom_type(object &scope, const char *name_, const std::type_info *tinfo, |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 563 | size_t type_size, size_t instance_size, |
Wenzel Jakob | b2c2c79 | 2016-01-17 22:36:40 +0100 | [diff] [blame] | 564 | void (*init_holder)(PyObject *, const void *), const destructor &dealloc, |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 565 | PyObject *parent, const char *doc) { |
| 566 | PyHeapTypeObject *type = (PyHeapTypeObject*) PyType_Type.tp_alloc(&PyType_Type, 0); |
Wenzel Jakob | 5708221 | 2015-09-04 23:42:12 +0200 | [diff] [blame] | 567 | #if PY_MAJOR_VERSION >= 3 |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 568 | PyObject *name = PyUnicode_FromString(name_); |
Wenzel Jakob | 5708221 | 2015-09-04 23:42:12 +0200 | [diff] [blame] | 569 | #else |
| 570 | PyObject *name = PyString_FromString(name_); |
| 571 | #endif |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 572 | if (type == nullptr || name == nullptr) |
| 573 | throw std::runtime_error("Internal error in custom_type::custom_type()"); |
| 574 | Py_INCREF(name); |
| 575 | std::string full_name(name_); |
| 576 | |
Wenzel Jakob | 8f4eb00 | 2015-10-15 18:13:33 +0200 | [diff] [blame] | 577 | pybind11::str scope_name = (object) scope.attr("__name__"), |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 578 | module_name = (object) scope.attr("__module__"); |
| 579 | |
| 580 | if (scope_name.check()) |
| 581 | full_name = std::string(scope_name) + "." + full_name; |
| 582 | if (module_name.check()) |
| 583 | full_name = std::string(module_name) + "." + full_name; |
| 584 | |
Wenzel Jakob | 5708221 | 2015-09-04 23:42:12 +0200 | [diff] [blame] | 585 | type->ht_name = name; |
| 586 | #if PY_MAJOR_VERSION >= 3 |
| 587 | type->ht_qualname = name; |
| 588 | #endif |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 589 | type->ht_type.tp_name = strdup(full_name.c_str()); |
| 590 | type->ht_type.tp_basicsize = instance_size; |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 591 | type->ht_type.tp_init = (initproc) init; |
| 592 | type->ht_type.tp_new = (newfunc) new_instance; |
| 593 | type->ht_type.tp_dealloc = dealloc; |
| 594 | type->ht_type.tp_flags |= |
| 595 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HEAPTYPE; |
| 596 | type->ht_type.tp_flags &= ~Py_TPFLAGS_HAVE_GC; |
Wenzel Jakob | 5708221 | 2015-09-04 23:42:12 +0200 | [diff] [blame] | 597 | #if PY_MAJOR_VERSION < 3 |
| 598 | type->ht_type.tp_flags |= Py_TPFLAGS_CHECKTYPES; |
| 599 | #endif |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 600 | type->ht_type.tp_as_number = &type->as_number; |
| 601 | type->ht_type.tp_as_sequence = &type->as_sequence; |
| 602 | type->ht_type.tp_as_mapping = &type->as_mapping; |
| 603 | type->ht_type.tp_base = (PyTypeObject *) parent; |
Wenzel Jakob | 5f218b3 | 2016-01-17 22:36:39 +0100 | [diff] [blame] | 604 | type->ht_type.tp_weaklistoffset = offsetof(instance<void>, weakrefs); |
| 605 | |
Wenzel Jakob | 5116b02 | 2015-09-05 02:09:17 +0200 | [diff] [blame] | 606 | if (doc) { |
Wenzel Jakob | 5f218b3 | 2016-01-17 22:36:39 +0100 | [diff] [blame] | 607 | size_t size = strlen(doc) + 1; |
Wenzel Jakob | 66c9a40 | 2016-01-17 22:36:36 +0100 | [diff] [blame] | 608 | type->ht_type.tp_doc = (char *) PyObject_MALLOC(size); |
Wenzel Jakob | 5116b02 | 2015-09-05 02:09:17 +0200 | [diff] [blame] | 609 | memcpy((void *) type->ht_type.tp_doc, doc, size); |
| 610 | } |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 611 | Py_XINCREF(parent); |
| 612 | |
| 613 | if (PyType_Ready(&type->ht_type) < 0) |
| 614 | throw std::runtime_error("Internal error in custom_type::custom_type()"); |
| 615 | m_ptr = (PyObject *) type; |
| 616 | |
| 617 | /* Needed by pydoc */ |
Wenzel Jakob | 04358b0 | 2015-10-01 16:45:28 +0200 | [diff] [blame] | 618 | attr("__module__") = scope_name; |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 619 | |
Wenzel Jakob | 66c9a40 | 2016-01-17 22:36:36 +0100 | [diff] [blame] | 620 | auto ®istered_types = get_internals().registered_types; |
| 621 | auto &type_info = registered_types[tinfo]; |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 622 | type_info.type = (PyTypeObject *) m_ptr; |
| 623 | type_info.type_size = type_size; |
| 624 | type_info.init_holder = init_holder; |
Wenzel Jakob | 8f4eb00 | 2015-10-15 18:13:33 +0200 | [diff] [blame] | 625 | attr("__pybind11__") = capsule(&type_info); |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 626 | |
| 627 | scope.attr(name) = *this; |
| 628 | } |
| 629 | |
| 630 | protected: |
| 631 | /* Allocate a metaclass on demand (for static properties) */ |
| 632 | handle metaclass() { |
| 633 | auto &ht_type = ((PyHeapTypeObject *) m_ptr)->ht_type; |
Wenzel Jakob | 5708221 | 2015-09-04 23:42:12 +0200 | [diff] [blame] | 634 | #if PY_MAJOR_VERSION >= 3 |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 635 | auto &ob_type = ht_type.ob_base.ob_base.ob_type; |
Wenzel Jakob | 5708221 | 2015-09-04 23:42:12 +0200 | [diff] [blame] | 636 | #else |
| 637 | auto &ob_type = ht_type.ob_type; |
| 638 | #endif |
| 639 | |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 640 | if (ob_type == &PyType_Type) { |
| 641 | std::string name_ = std::string(ht_type.tp_name) + "_meta"; |
| 642 | PyHeapTypeObject *type = (PyHeapTypeObject*) PyType_Type.tp_alloc(&PyType_Type, 0); |
| 643 | PyObject *name = PyUnicode_FromString(name_.c_str()); |
| 644 | if (type == nullptr || name == nullptr) |
| 645 | throw std::runtime_error("Internal error in custom_type::metaclass()"); |
| 646 | Py_INCREF(name); |
Wenzel Jakob | 5708221 | 2015-09-04 23:42:12 +0200 | [diff] [blame] | 647 | type->ht_name = name; |
| 648 | #if PY_MAJOR_VERSION >= 3 |
| 649 | type->ht_qualname = name; |
| 650 | #endif |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 651 | type->ht_type.tp_name = strdup(name_.c_str()); |
| 652 | type->ht_type.tp_base = &PyType_Type; |
| 653 | type->ht_type.tp_flags |= Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HEAPTYPE; |
| 654 | type->ht_type.tp_flags &= ~Py_TPFLAGS_HAVE_GC; |
| 655 | if (PyType_Ready(&type->ht_type) < 0) |
| 656 | throw std::runtime_error("Internal error in custom_type::metaclass()"); |
| 657 | ob_type = (PyTypeObject *) type; |
| 658 | Py_INCREF(type); |
| 659 | } |
| 660 | return handle((PyObject *) ob_type); |
| 661 | } |
| 662 | |
| 663 | static int init(void *self, PyObject *, PyObject *) { |
| 664 | std::string msg = std::string(Py_TYPE(self)->tp_name) + ": No constructor defined!"; |
| 665 | PyErr_SetString(PyExc_TypeError, msg.c_str()); |
| 666 | return -1; |
| 667 | } |
| 668 | |
| 669 | static PyObject *new_instance(PyTypeObject *type, PyObject *, PyObject *) { |
| 670 | const detail::type_info *type_info = capsule( |
Wenzel Jakob | 8f4eb00 | 2015-10-15 18:13:33 +0200 | [diff] [blame] | 671 | PyObject_GetAttrString((PyObject *) type, const_cast<char*>("__pybind11__")), false); |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 672 | instance<void> *self = (instance<void> *) PyType_GenericAlloc(type, 0); |
| 673 | self->value = ::operator new(type_info->type_size); |
| 674 | self->owned = true; |
| 675 | self->parent = nullptr; |
| 676 | self->constructed = false; |
| 677 | detail::get_internals().registered_instances[self->value] = (PyObject *) self; |
| 678 | return (PyObject *) self; |
| 679 | } |
| 680 | |
| 681 | static void dealloc(instance<void> *self) { |
| 682 | if (self->value) { |
| 683 | bool dont_cache = self->parent && ((instance<void> *) self->parent)->value == self->value; |
| 684 | if (!dont_cache) { // avoid an issue with internal references matching their parent's address |
| 685 | auto ®istered_instances = detail::get_internals().registered_instances; |
| 686 | auto it = registered_instances.find(self->value); |
| 687 | if (it == registered_instances.end()) |
| 688 | throw std::runtime_error("Deallocating unregistered instance!"); |
| 689 | registered_instances.erase(it); |
| 690 | } |
| 691 | Py_XDECREF(self->parent); |
Wenzel Jakob | 5f218b3 | 2016-01-17 22:36:39 +0100 | [diff] [blame] | 692 | if (self->weakrefs) |
| 693 | PyObject_ClearWeakRefs((PyObject *) self); |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 694 | } |
| 695 | Py_TYPE(self)->tp_free((PyObject*) self); |
| 696 | } |
| 697 | |
Wenzel Jakob | 43398a8 | 2015-07-28 16:12:20 +0200 | [diff] [blame] | 698 | void install_buffer_funcs( |
| 699 | buffer_info *(*get_buffer)(PyObject *, void *), |
| 700 | void *get_buffer_data) { |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 701 | PyHeapTypeObject *type = (PyHeapTypeObject*) m_ptr; |
| 702 | type->ht_type.tp_as_buffer = &type->as_buffer; |
Wenzel Jakob | 5708221 | 2015-09-04 23:42:12 +0200 | [diff] [blame] | 703 | #if PY_MAJOR_VERSION < 3 |
| 704 | type->ht_type.tp_flags |= Py_TPFLAGS_HAVE_NEWBUFFER; |
| 705 | #endif |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 706 | type->as_buffer.bf_getbuffer = getbuffer; |
| 707 | type->as_buffer.bf_releasebuffer = releasebuffer; |
Wenzel Jakob | 8f4eb00 | 2015-10-15 18:13:33 +0200 | [diff] [blame] | 708 | auto info = ((detail::type_info *) capsule(attr("__pybind11__"))); |
Wenzel Jakob | 43398a8 | 2015-07-28 16:12:20 +0200 | [diff] [blame] | 709 | info->get_buffer = get_buffer; |
| 710 | info->get_buffer_data = get_buffer_data; |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 711 | } |
| 712 | |
| 713 | static int getbuffer(PyObject *obj, Py_buffer *view, int flags) { |
Wenzel Jakob | 8f4eb00 | 2015-10-15 18:13:33 +0200 | [diff] [blame] | 714 | auto const &typeinfo = ((detail::type_info *) capsule(handle(obj).attr("__pybind11__"))); |
Wenzel Jakob | 43398a8 | 2015-07-28 16:12:20 +0200 | [diff] [blame] | 715 | |
| 716 | if (view == nullptr || obj == nullptr || !typeinfo || !typeinfo->get_buffer) { |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 717 | PyErr_SetString(PyExc_BufferError, "Internal error"); |
| 718 | return -1; |
| 719 | } |
| 720 | memset(view, 0, sizeof(Py_buffer)); |
Wenzel Jakob | 43398a8 | 2015-07-28 16:12:20 +0200 | [diff] [blame] | 721 | buffer_info *info = typeinfo->get_buffer(obj, typeinfo->get_buffer_data); |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 722 | view->obj = obj; |
| 723 | view->ndim = 1; |
| 724 | view->internal = info; |
| 725 | view->buf = info->ptr; |
| 726 | view->itemsize = info->itemsize; |
| 727 | view->len = view->itemsize; |
| 728 | for (auto s : info->shape) |
| 729 | view->len *= s; |
| 730 | if ((flags & PyBUF_FORMAT) == PyBUF_FORMAT) |
| 731 | view->format = const_cast<char *>(info->format.c_str()); |
| 732 | if ((flags & PyBUF_STRIDES) == PyBUF_STRIDES) { |
| 733 | view->ndim = info->ndim; |
Wenzel Jakob | 27e8e10 | 2016-01-17 22:36:37 +0100 | [diff] [blame] | 734 | view->strides = (ssize_t *) &info->strides[0]; |
| 735 | view->shape = (ssize_t *) &info->shape[0]; |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 736 | } |
| 737 | Py_INCREF(view->obj); |
| 738 | return 0; |
| 739 | } |
| 740 | |
| 741 | static void releasebuffer(PyObject *, Py_buffer *view) { delete (buffer_info *) view->internal; } |
| 742 | }; |
Wenzel Jakob | 7186783 | 2015-07-29 17:43:52 +0200 | [diff] [blame] | 743 | |
| 744 | /* Forward declarations */ |
| 745 | enum op_id : int; |
| 746 | enum op_type : int; |
| 747 | struct undefined_t; |
| 748 | template <op_id id, op_type ot, typename L = undefined_t, typename R = undefined_t> struct op_; |
| 749 | template <typename... Args> struct init; |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 750 | NAMESPACE_END(detail) |
| 751 | |
| 752 | template <typename type, typename holder_type = std::unique_ptr<type>> class class_ : public detail::custom_type { |
| 753 | public: |
| 754 | typedef detail::instance<type, holder_type> instance_type; |
| 755 | |
Wenzel Jakob | b1b7140 | 2015-10-18 16:48:30 +0200 | [diff] [blame] | 756 | PYBIND11_OBJECT(class_, detail::custom_type, PyType_Check) |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 757 | |
| 758 | class_(object &scope, const char *name, const char *doc = nullptr) |
Wenzel Jakob | f5fae92 | 2015-08-24 15:31:24 +0200 | [diff] [blame] | 759 | : detail::custom_type(scope, name, &typeid(type), sizeof(type), |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 760 | sizeof(instance_type), init_holder, dealloc, |
| 761 | nullptr, doc) { } |
| 762 | |
| 763 | class_(object &scope, const char *name, object &parent, |
| 764 | const char *doc = nullptr) |
Wenzel Jakob | f5fae92 | 2015-08-24 15:31:24 +0200 | [diff] [blame] | 765 | : detail::custom_type(scope, name, &typeid(type), sizeof(type), |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 766 | sizeof(instance_type), init_holder, dealloc, |
| 767 | parent.ptr(), doc) { } |
| 768 | |
Wenzel Jakob | 7186783 | 2015-07-29 17:43:52 +0200 | [diff] [blame] | 769 | template <typename Func, typename... Extra> |
| 770 | class_ &def(const char *name_, Func&& f, Extra&&... extra) { |
Wenzel Jakob | 5708221 | 2015-09-04 23:42:12 +0200 | [diff] [blame] | 771 | cpp_function cf(std::forward<Func>(f), name(name_), |
| 772 | sibling(attr(name_)), is_method(this), |
| 773 | std::forward<Extra>(extra)...); |
| 774 | attr(cf.name()) = cf; |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 775 | return *this; |
| 776 | } |
| 777 | |
Wenzel Jakob | 7186783 | 2015-07-29 17:43:52 +0200 | [diff] [blame] | 778 | template <typename Func, typename... Extra> class_ & |
| 779 | def_static(const char *name_, Func f, Extra&&... extra) { |
Wenzel Jakob | 5708221 | 2015-09-04 23:42:12 +0200 | [diff] [blame] | 780 | cpp_function cf(std::forward<Func>(f), name(name_), |
| 781 | sibling(attr(name_)), |
| 782 | std::forward<Extra>(extra)...); |
| 783 | attr(cf.name()) = cf; |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 784 | return *this; |
| 785 | } |
| 786 | |
Wenzel Jakob | 7186783 | 2015-07-29 17:43:52 +0200 | [diff] [blame] | 787 | template <detail::op_id id, detail::op_type ot, typename L, typename R, typename... Extra> |
| 788 | class_ &def(const detail::op_<id, ot, L, R> &op, Extra&&... extra) { |
| 789 | op.template execute<type>(*this, std::forward<Extra>(extra)...); |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 790 | return *this; |
| 791 | } |
| 792 | |
Wenzel Jakob | 7186783 | 2015-07-29 17:43:52 +0200 | [diff] [blame] | 793 | template <detail::op_id id, detail::op_type ot, typename L, typename R, typename... Extra> |
| 794 | class_ & def_cast(const detail::op_<id, ot, L, R> &op, Extra&&... extra) { |
| 795 | op.template execute_cast<type>(*this, std::forward<Extra>(extra)...); |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 796 | return *this; |
| 797 | } |
| 798 | |
Wenzel Jakob | 7186783 | 2015-07-29 17:43:52 +0200 | [diff] [blame] | 799 | template <typename... Args, typename... Extra> |
| 800 | class_ &def(const detail::init<Args...> &init, Extra&&... extra) { |
| 801 | init.template execute<type>(*this, std::forward<Extra>(extra)...); |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 802 | return *this; |
| 803 | } |
| 804 | |
Wenzel Jakob | 7186783 | 2015-07-29 17:43:52 +0200 | [diff] [blame] | 805 | template <typename Func> class_& def_buffer(Func &&func) { |
Wenzel Jakob | 43398a8 | 2015-07-28 16:12:20 +0200 | [diff] [blame] | 806 | struct capture { Func func; }; |
| 807 | capture *ptr = new capture { std::forward<Func>(func) }; |
| 808 | install_buffer_funcs([](PyObject *obj, void *ptr) -> buffer_info* { |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 809 | detail::type_caster<type> caster; |
| 810 | if (!caster.load(obj, false)) |
| 811 | return nullptr; |
Wenzel Jakob | 43398a8 | 2015-07-28 16:12:20 +0200 | [diff] [blame] | 812 | return new buffer_info(((capture *) ptr)->func(caster)); |
| 813 | }, ptr); |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 814 | return *this; |
| 815 | } |
| 816 | |
Wenzel Jakob | 7186783 | 2015-07-29 17:43:52 +0200 | [diff] [blame] | 817 | template <typename C, typename D, typename... Extra> |
| 818 | class_ &def_readwrite(const char *name, D C::*pm, Extra&&... extra) { |
| 819 | cpp_function fget([pm](const C &c) -> const D &{ return c.*pm; }, |
| 820 | return_value_policy::reference_internal, |
Wenzel Jakob | 5708221 | 2015-09-04 23:42:12 +0200 | [diff] [blame] | 821 | is_method(this), extra...), |
Wenzel Jakob | 7186783 | 2015-07-29 17:43:52 +0200 | [diff] [blame] | 822 | fset([pm](C &c, const D &value) { c.*pm = value; }, |
Wenzel Jakob | 5708221 | 2015-09-04 23:42:12 +0200 | [diff] [blame] | 823 | is_method(this), extra...); |
Wenzel Jakob | 7186783 | 2015-07-29 17:43:52 +0200 | [diff] [blame] | 824 | def_property(name, fget, fset); |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 825 | return *this; |
| 826 | } |
| 827 | |
Wenzel Jakob | 7186783 | 2015-07-29 17:43:52 +0200 | [diff] [blame] | 828 | template <typename C, typename D, typename... Extra> |
| 829 | class_ &def_readonly(const char *name, const D C::*pm, Extra&& ...extra) { |
| 830 | cpp_function fget([pm](const C &c) -> const D &{ return c.*pm; }, |
| 831 | return_value_policy::reference_internal, |
Wenzel Jakob | 5708221 | 2015-09-04 23:42:12 +0200 | [diff] [blame] | 832 | is_method(this), std::forward<Extra>(extra)...); |
Wenzel Jakob | 7186783 | 2015-07-29 17:43:52 +0200 | [diff] [blame] | 833 | def_property_readonly(name, fget); |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 834 | return *this; |
| 835 | } |
| 836 | |
Wenzel Jakob | 7186783 | 2015-07-29 17:43:52 +0200 | [diff] [blame] | 837 | template <typename D, typename... Extra> |
| 838 | class_ &def_readwrite_static(const char *name, D *pm, Extra&& ...extra) { |
Wenzel Jakob | bd4a529 | 2015-07-11 17:41:48 +0200 | [diff] [blame] | 839 | cpp_function fget([pm](object) -> const D &{ return *pm; }, nullptr, |
Wenzel Jakob | 7186783 | 2015-07-29 17:43:52 +0200 | [diff] [blame] | 840 | return_value_policy::reference_internal, extra...), |
| 841 | fset([pm](object, const D &value) { *pm = value; }, extra...); |
| 842 | def_property_static(name, fget, fset); |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 843 | return *this; |
| 844 | } |
| 845 | |
Wenzel Jakob | 7186783 | 2015-07-29 17:43:52 +0200 | [diff] [blame] | 846 | template <typename D, typename... Extra> |
| 847 | class_ &def_readonly_static(const char *name, const D *pm, Extra&& ...extra) { |
Wenzel Jakob | bd4a529 | 2015-07-11 17:41:48 +0200 | [diff] [blame] | 848 | cpp_function fget([pm](object) -> const D &{ return *pm; }, nullptr, |
Wenzel Jakob | 7186783 | 2015-07-29 17:43:52 +0200 | [diff] [blame] | 849 | return_value_policy::reference_internal, std::forward<Extra>(extra)...); |
| 850 | def_property_readonly_static(name, fget); |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 851 | return *this; |
| 852 | } |
| 853 | |
Wenzel Jakob | a576e6a | 2015-07-29 17:51:54 +0200 | [diff] [blame] | 854 | class_ &def_property_readonly(const char *name, const cpp_function &fget, const char *doc = nullptr) { |
| 855 | def_property(name, fget, cpp_function(), doc); |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 856 | return *this; |
| 857 | } |
| 858 | |
Wenzel Jakob | a576e6a | 2015-07-29 17:51:54 +0200 | [diff] [blame] | 859 | class_ &def_property_readonly_static(const char *name, const cpp_function &fget, const char *doc = nullptr) { |
| 860 | def_property_static(name, fget, cpp_function(), doc); |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 861 | return *this; |
| 862 | } |
| 863 | |
Wenzel Jakob | a576e6a | 2015-07-29 17:51:54 +0200 | [diff] [blame] | 864 | class_ &def_property(const char *name, const cpp_function &fget, const cpp_function &fset, const char *doc = nullptr) { |
Wenzel Jakob | 8f4eb00 | 2015-10-15 18:13:33 +0200 | [diff] [blame] | 865 | object doc_obj = doc ? pybind11::str(doc) : (object) const_cast<cpp_function&>(fget).attr("__doc__"); |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 866 | object property( |
| 867 | PyObject_CallFunction((PyObject *)&PyProperty_Type, |
Wenzel Jakob | 7186783 | 2015-07-29 17:43:52 +0200 | [diff] [blame] | 868 | const_cast<char *>("OOOO"), fget.ptr() ? fget.ptr() : Py_None, |
Wenzel Jakob | a576e6a | 2015-07-29 17:51:54 +0200 | [diff] [blame] | 869 | fset.ptr() ? fset.ptr() : Py_None, Py_None, doc_obj.ptr()), false); |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 870 | attr(name) = property; |
| 871 | return *this; |
| 872 | } |
| 873 | |
Wenzel Jakob | a576e6a | 2015-07-29 17:51:54 +0200 | [diff] [blame] | 874 | class_ &def_property_static(const char *name, const cpp_function &fget, const cpp_function &fset, const char *doc = nullptr) { |
Wenzel Jakob | 8f4eb00 | 2015-10-15 18:13:33 +0200 | [diff] [blame] | 875 | object doc_obj = doc ? pybind11::str(doc) : (object) const_cast<cpp_function&>(fget).attr("__doc__"); |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 876 | object property( |
| 877 | PyObject_CallFunction((PyObject *)&PyProperty_Type, |
Wenzel Jakob | a576e6a | 2015-07-29 17:51:54 +0200 | [diff] [blame] | 878 | const_cast<char *>("OOOs"), fget.ptr() ? fget.ptr() : Py_None, |
| 879 | fset.ptr() ? fset.ptr() : Py_None, Py_None, doc_obj.ptr()), false); |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 880 | metaclass().attr(name) = property; |
| 881 | return *this; |
| 882 | } |
Wenzel Jakob | a2f6fde | 2015-10-01 16:46:03 +0200 | [diff] [blame] | 883 | |
| 884 | template <typename target> class_ alias() { |
Wenzel Jakob | 8f4eb00 | 2015-10-15 18:13:33 +0200 | [diff] [blame] | 885 | auto &instances = pybind11::detail::get_internals().registered_types; |
Wenzel Jakob | a2f6fde | 2015-10-01 16:46:03 +0200 | [diff] [blame] | 886 | instances[&typeid(target)] = instances[&typeid(type)]; |
| 887 | return *this; |
| 888 | } |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 889 | private: |
Wenzel Jakob | b2c2c79 | 2016-01-17 22:36:40 +0100 | [diff] [blame] | 890 | /// Initialize holder object, variant 1: object derives from enable_shared_from_this |
| 891 | template <typename T> |
| 892 | static void init_holder_helper(instance_type *inst, const holder_type * /* unused */, const std::enable_shared_from_this<T> * /* dummy */) { |
Wenzel Jakob | 6e213c9 | 2015-11-24 23:05:58 +0100 | [diff] [blame] | 893 | try { |
Wenzel Jakob | b2c2c79 | 2016-01-17 22:36:40 +0100 | [diff] [blame] | 894 | new (&inst->holder) holder_type(inst->value->shared_from_this()); |
Wenzel Jakob | 6e213c9 | 2015-11-24 23:05:58 +0100 | [diff] [blame] | 895 | } catch (const std::bad_weak_ptr &) { |
| 896 | new (&inst->holder) holder_type(inst->value); |
| 897 | } |
Wenzel Jakob | b2c2c79 | 2016-01-17 22:36:40 +0100 | [diff] [blame] | 898 | } |
| 899 | |
| 900 | /// Initialize holder object, variant 2: try to construct from existing holder object, if possible |
| 901 | template <typename T = holder_type, |
| 902 | typename std::enable_if<std::is_copy_constructible<T>::value, int>::type = 0> |
| 903 | static void init_holder_helper(instance_type *inst, const holder_type *holder_ptr, const void * /* dummy */) { |
| 904 | if (holder_ptr) |
| 905 | new (&inst->holder) holder_type(*holder_ptr); |
| 906 | else |
| 907 | new (&inst->holder) holder_type(inst->value); |
| 908 | } |
| 909 | |
| 910 | /// Initialize holder object, variant 3: holder is not copy constructible (e.g. unique_ptr), always initialize from raw pointer |
| 911 | template <typename T = holder_type, |
| 912 | typename std::enable_if<!std::is_copy_constructible<T>::value, int>::type = 0> |
| 913 | static void init_holder_helper(instance_type *inst, const holder_type * /* unused */, const void * /* dummy */) { |
| 914 | new (&inst->holder) holder_type(inst->value); |
| 915 | } |
| 916 | |
| 917 | /// Initialize holder object of an instance, possibly given a pointer to an existing holder |
| 918 | static void init_holder(PyObject *inst_, const void *holder_ptr) { |
| 919 | auto inst = (instance_type *) inst_; |
| 920 | init_holder_helper(inst, (const holder_type *) holder_ptr, inst->value); |
Wenzel Jakob | 6e213c9 | 2015-11-24 23:05:58 +0100 | [diff] [blame] | 921 | inst->constructed = true; |
| 922 | } |
| 923 | |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 924 | static void dealloc(PyObject *inst_) { |
| 925 | instance_type *inst = (instance_type *) inst_; |
| 926 | if (inst->owned) { |
| 927 | if (inst->constructed) |
| 928 | inst->holder.~holder_type(); |
| 929 | else |
| 930 | ::operator delete(inst->value); |
| 931 | } |
| 932 | custom_type::dealloc((detail::instance<void> *) inst); |
| 933 | } |
| 934 | }; |
| 935 | |
| 936 | /// Binds C++ enumerations and enumeration classes to Python |
| 937 | template <typename Type> class enum_ : public class_<Type> { |
| 938 | public: |
| 939 | enum_(object &scope, const char *name, const char *doc = nullptr) |
| 940 | : class_<Type>(scope, name, doc), m_parent(scope) { |
| 941 | auto entries = new std::unordered_map<int, const char *>(); |
Wenzel Jakob | 8456a4d | 2015-10-13 02:42:20 +0200 | [diff] [blame] | 942 | this->def("__repr__", [name, entries](Type value) -> std::string { |
Wenzel Jakob | a750031 | 2015-08-29 02:08:32 +0200 | [diff] [blame] | 943 | auto it = entries->find((int) value); |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 944 | return std::string(name) + "." + |
| 945 | ((it == entries->end()) ? std::string("???") |
| 946 | : std::string(it->second)); |
| 947 | }); |
Wenzel Jakob | 6918922 | 2015-10-01 21:32:23 +0200 | [diff] [blame] | 948 | this->def("__int__", [](Type value) { return (int) value; }); |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 949 | m_entries = entries; |
| 950 | } |
| 951 | |
| 952 | /// Export enumeration entries into the parent scope |
| 953 | void export_values() { |
| 954 | PyObject *dict = ((PyTypeObject *) this->m_ptr)->tp_dict; |
| 955 | PyObject *key, *value; |
Wenzel Jakob | 27e8e10 | 2016-01-17 22:36:37 +0100 | [diff] [blame] | 956 | ssize_t pos = 0; |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 957 | while (PyDict_Next(dict, &pos, &key, &value)) |
| 958 | if (PyObject_IsInstance(value, this->m_ptr)) |
| 959 | m_parent.attr(key) = value; |
| 960 | } |
| 961 | |
| 962 | /// Add an enumeration entry |
| 963 | enum_& value(char const* name, Type value) { |
Wenzel Jakob | 8f4eb00 | 2015-10-15 18:13:33 +0200 | [diff] [blame] | 964 | this->attr(name) = pybind11::cast(value, return_value_policy::copy); |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 965 | (*m_entries)[(int) value] = name; |
| 966 | return *this; |
| 967 | } |
| 968 | private: |
| 969 | std::unordered_map<int, const char *> *m_entries; |
| 970 | object &m_parent; |
| 971 | }; |
| 972 | |
| 973 | NAMESPACE_BEGIN(detail) |
Wenzel Jakob | 7186783 | 2015-07-29 17:43:52 +0200 | [diff] [blame] | 974 | template <typename... Args> struct init { |
Wenzel Jakob | 8f4eb00 | 2015-10-15 18:13:33 +0200 | [diff] [blame] | 975 | template <typename Base, typename Holder, typename... Extra> void execute(pybind11::class_<Base, Holder> &class_, Extra&&... extra) const { |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 976 | /// Function which calls a specific C++ in-place constructor |
Wenzel Jakob | 7186783 | 2015-07-29 17:43:52 +0200 | [diff] [blame] | 977 | class_.def("__init__", [](Base *instance, Args... args) { new (instance) Base(args...); }, std::forward<Extra>(extra)...); |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 978 | } |
| 979 | }; |
Wenzel Jakob | 5f218b3 | 2016-01-17 22:36:39 +0100 | [diff] [blame] | 980 | |
| 981 | PYBIND11_NOINLINE inline void keep_alive_impl(int Nurse, int Patient, PyObject *arg, PyObject *ret) { |
| 982 | /* Clever approach based on weak references taken from Boost.Python */ |
Wenzel Jakob | b2c2c79 | 2016-01-17 22:36:40 +0100 | [diff] [blame] | 983 | handle nurse (Nurse > 0 ? PyTuple_GetItem(arg, Nurse - 1) : ret); |
| 984 | handle patient(Patient > 0 ? PyTuple_GetItem(arg, Patient - 1) : ret); |
Wenzel Jakob | 5f218b3 | 2016-01-17 22:36:39 +0100 | [diff] [blame] | 985 | |
Wenzel Jakob | b2c2c79 | 2016-01-17 22:36:40 +0100 | [diff] [blame] | 986 | if (!nurse || !patient) |
| 987 | throw std::runtime_error("Could not activate keep_alive!"); |
Wenzel Jakob | 5f218b3 | 2016-01-17 22:36:39 +0100 | [diff] [blame] | 988 | |
| 989 | cpp_function disable_lifesupport( |
Wenzel Jakob | b2c2c79 | 2016-01-17 22:36:40 +0100 | [diff] [blame] | 990 | [patient](handle weakref) { patient.dec_ref(); weakref.dec_ref(); }); |
Wenzel Jakob | 5f218b3 | 2016-01-17 22:36:39 +0100 | [diff] [blame] | 991 | |
Wenzel Jakob | b2c2c79 | 2016-01-17 22:36:40 +0100 | [diff] [blame] | 992 | weakref wr(nurse, disable_lifesupport); |
| 993 | if (!wr) |
Wenzel Jakob | 5f218b3 | 2016-01-17 22:36:39 +0100 | [diff] [blame] | 994 | throw std::runtime_error("Could not allocate weak reference!"); |
| 995 | |
Wenzel Jakob | b2c2c79 | 2016-01-17 22:36:40 +0100 | [diff] [blame] | 996 | patient.inc_ref(); /* reference patient and leak the weak reference */ |
| 997 | (void) wr.release(); |
Wenzel Jakob | 5f218b3 | 2016-01-17 22:36:39 +0100 | [diff] [blame] | 998 | } |
| 999 | |
| 1000 | template <int Nurse, int Patient> struct process_dynamic<keep_alive<Nurse, Patient>> : public process_dynamic<void> { |
| 1001 | template <int N = Nurse, int P = Patient, typename std::enable_if<N != 0 && P != 0, int>::type = 0> |
| 1002 | static void precall(PyObject *arg) { keep_alive_impl(Nurse, Patient, arg, nullptr); } |
| 1003 | template <int N = Nurse, int P = Patient, typename std::enable_if<N != 0 && P != 0, int>::type = 0> |
| 1004 | static void postcall(PyObject *, PyObject *) { } |
| 1005 | template <int N = Nurse, int P = Patient, typename std::enable_if<N == 0 || P == 0, int>::type = 0> |
| 1006 | static void precall(PyObject *) { } |
| 1007 | template <int N = Nurse, int P = Patient, typename std::enable_if<N == 0 || P == 0, int>::type = 0> |
| 1008 | static void postcall(PyObject *arg, PyObject *ret) { keep_alive_impl(Nurse, Patient, arg, ret); } |
| 1009 | }; |
| 1010 | |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 1011 | NAMESPACE_END(detail) |
| 1012 | |
| 1013 | template <typename... Args> detail::init<Args...> init() { return detail::init<Args...>(); }; |
| 1014 | |
| 1015 | template <typename InputType, typename OutputType> void implicitly_convertible() { |
Wenzel Jakob | 2ac5044 | 2016-01-17 22:36:35 +0100 | [diff] [blame] | 1016 | auto implicit_caster = [](PyObject *obj, PyTypeObject *type) -> PyObject * { |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 1017 | if (!detail::type_caster<InputType>().load(obj, false)) |
| 1018 | return nullptr; |
| 1019 | tuple args(1); |
| 1020 | args[0] = obj; |
| 1021 | PyObject *result = PyObject_Call((PyObject *) type, args.ptr(), nullptr); |
| 1022 | if (result == nullptr) |
| 1023 | PyErr_Clear(); |
| 1024 | return result; |
| 1025 | }; |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 1026 | auto & registered_types = detail::get_internals().registered_types; |
Wenzel Jakob | f5fae92 | 2015-08-24 15:31:24 +0200 | [diff] [blame] | 1027 | auto it = registered_types.find(&typeid(OutputType)); |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 1028 | if (it == registered_types.end()) |
Wenzel Jakob | f5fae92 | 2015-08-24 15:31:24 +0200 | [diff] [blame] | 1029 | throw std::runtime_error("implicitly_convertible: Unable to find type " + type_id<OutputType>()); |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 1030 | it->second.implicit_conversions.push_back(implicit_caster); |
| 1031 | } |
| 1032 | |
| 1033 | inline void init_threading() { PyEval_InitThreads(); } |
| 1034 | |
| 1035 | class gil_scoped_acquire { |
| 1036 | PyGILState_STATE state; |
| 1037 | public: |
| 1038 | inline gil_scoped_acquire() { state = PyGILState_Ensure(); } |
| 1039 | inline ~gil_scoped_acquire() { PyGILState_Release(state); } |
| 1040 | }; |
| 1041 | |
| 1042 | class gil_scoped_release { |
| 1043 | PyThreadState *state; |
| 1044 | public: |
| 1045 | inline gil_scoped_release() { state = PyEval_SaveThread(); } |
| 1046 | inline ~gil_scoped_release() { PyEval_RestoreThread(state); } |
| 1047 | }; |
| 1048 | |
Wenzel Jakob | a2f6fde | 2015-10-01 16:46:03 +0200 | [diff] [blame] | 1049 | inline function get_overload(const void *this_ptr, const char *name) { |
| 1050 | handle py_object = detail::get_object_handle(this_ptr); |
Wenzel Jakob | ac0fde9 | 2015-10-01 18:37:26 +0200 | [diff] [blame] | 1051 | if (!py_object) |
| 1052 | return function(); |
Wenzel Jakob | a2f6fde | 2015-10-01 16:46:03 +0200 | [diff] [blame] | 1053 | handle type = py_object.get_type(); |
| 1054 | auto key = std::make_pair(type.ptr(), name); |
| 1055 | |
| 1056 | /* Cache functions that aren't overloaded in python to avoid |
| 1057 | many costly dictionary lookups in Python */ |
| 1058 | auto &cache = detail::get_internals().inactive_overload_cache; |
| 1059 | if (cache.find(key) != cache.end()) |
| 1060 | return function(); |
| 1061 | |
| 1062 | function overload = (function) py_object.attr(name); |
| 1063 | if (overload.is_cpp_function()) { |
| 1064 | cache.insert(key); |
| 1065 | return function(); |
| 1066 | } |
Wenzel Jakob | 27e8e10 | 2016-01-17 22:36:37 +0100 | [diff] [blame] | 1067 | |
Wenzel Jakob | a2f6fde | 2015-10-01 16:46:03 +0200 | [diff] [blame] | 1068 | PyFrameObject *frame = PyThreadState_Get()->frame; |
Wenzel Jakob | 8f4eb00 | 2015-10-15 18:13:33 +0200 | [diff] [blame] | 1069 | pybind11::str caller = pybind11::handle(frame->f_code->co_name).str(); |
Wenzel Jakob | 27e8e10 | 2016-01-17 22:36:37 +0100 | [diff] [blame] | 1070 | if ((std::string) caller == name) |
Wenzel Jakob | a2f6fde | 2015-10-01 16:46:03 +0200 | [diff] [blame] | 1071 | return function(); |
| 1072 | return overload; |
| 1073 | } |
| 1074 | |
Wenzel Jakob | b1b7140 | 2015-10-18 16:48:30 +0200 | [diff] [blame] | 1075 | #define PYBIND11_OVERLOAD_INT(ret_type, class_name, name, ...) { \ |
Wenzel Jakob | 8f4eb00 | 2015-10-15 18:13:33 +0200 | [diff] [blame] | 1076 | pybind11::gil_scoped_acquire gil; \ |
| 1077 | pybind11::function overload = pybind11::get_overload(this, #name); \ |
Wenzel Jakob | a2f6fde | 2015-10-01 16:46:03 +0200 | [diff] [blame] | 1078 | if (overload) \ |
| 1079 | return overload.call(__VA_ARGS__).cast<ret_type>(); } |
| 1080 | |
Wenzel Jakob | b1b7140 | 2015-10-18 16:48:30 +0200 | [diff] [blame] | 1081 | #define PYBIND11_OVERLOAD(ret_type, class_name, name, ...) \ |
| 1082 | PYBIND11_OVERLOAD_INT(ret_type, class_name, name, __VA_ARGS__) \ |
Wenzel Jakob | a2f6fde | 2015-10-01 16:46:03 +0200 | [diff] [blame] | 1083 | return class_name::name(__VA_ARGS__) |
| 1084 | |
Wenzel Jakob | b1b7140 | 2015-10-18 16:48:30 +0200 | [diff] [blame] | 1085 | #define PYBIND11_OVERLOAD_PURE(ret_type, class_name, name, ...) \ |
| 1086 | PYBIND11_OVERLOAD_INT(ret_type, class_name, name, __VA_ARGS__) \ |
Wenzel Jakob | a2f6fde | 2015-10-01 16:46:03 +0200 | [diff] [blame] | 1087 | throw std::runtime_error("Tried to call pure virtual function \"" #name "\""); |
| 1088 | |
Wenzel Jakob | 8f4eb00 | 2015-10-15 18:13:33 +0200 | [diff] [blame] | 1089 | NAMESPACE_END(pybind11) |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 1090 | |
| 1091 | #if defined(_MSC_VER) |
| 1092 | #pragma warning(pop) |
Wenzel Jakob | cd5cda7 | 2015-08-03 12:17:54 +0200 | [diff] [blame] | 1093 | #elif defined(__GNUG__) and !defined(__clang__) |
Wenzel Jakob | 281aa0e | 2015-07-30 15:29:00 +0200 | [diff] [blame] | 1094 | #pragma GCC diagnostic pop |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 1095 | #endif |
Wenzel Jakob | 281aa0e | 2015-07-30 15:29:00 +0200 | [diff] [blame] | 1096 | |