blob: 975cf1440d88e513567a0307b50d6e8e98bdfce7 [file] [log] [blame]
Wenzel Jakob38bd7112015-07-05 20:05:44 +02001/*
Wenzel Jakob48548ea2016-01-17 22:36:44 +01002 pybind11/pybind11.h: Main header file of the C++11 python
3 binding generator library
Wenzel Jakob38bd7112015-07-05 20:05:44 +02004
Wenzel Jakob8cb6cb32016-04-17 20:21:41 +02005 Copyright (c) 2016 Wenzel Jakob <wenzel.jakob@epfl.ch>
Wenzel Jakob38bd7112015-07-05 20:05:44 +02006
7 All rights reserved. Use of this source code is governed by a
8 BSD-style license that can be found in the LICENSE file.
9*/
10
Wenzel Jakobbd4a5292015-07-11 17:41:48 +020011#pragma once
Wenzel Jakob38bd7112015-07-05 20:05:44 +020012
13#if defined(_MSC_VER)
Wenzel Jakob48548ea2016-01-17 22:36:44 +010014# pragma warning(push)
15# pragma warning(disable: 4127) // warning C4127: Conditional expression is constant
16# pragma warning(disable: 4800) // warning C4800: 'int': forcing value to bool 'true' or 'false' (performance warning)
17# pragma warning(disable: 4996) // warning C4996: The POSIX name for this item is deprecated. Instead, use the ISO C and C++ conformant name
18# pragma warning(disable: 4100) // warning C4100: Unreferenced formal parameter
19# pragma warning(disable: 4512) // warning C4512: Assignment operator was implicitly defined as deleted
Ben Pritchard70ee47d2016-02-18 13:06:43 -050020#elif defined(__ICC) || defined(__INTEL_COMPILER)
Ben Pritchard33f34302016-02-18 15:25:51 -050021# pragma warning(push)
Ben Pritchard70ee47d2016-02-18 13:06:43 -050022# pragma warning(disable:2196) // warning #2196: routine is both "inline" and "noinline"
Wenzel Jakob6d252962016-05-01 20:47:49 +020023#elif defined(__GNUG__) && !defined(__clang__)
Wenzel Jakob48548ea2016-01-17 22:36:44 +010024# pragma GCC diagnostic push
25# pragma GCC diagnostic ignored "-Wunused-but-set-parameter"
26# pragma GCC diagnostic ignored "-Wunused-but-set-variable"
27# pragma GCC diagnostic ignored "-Wmissing-field-initializers"
Wenzel Jakobdeeab552016-05-15 23:54:34 +020028# pragma GCC diagnostic ignored "-Wstrict-aliasing"
29# pragma GCC diagnostic ignored "-Wattributes"
Wenzel Jakob38bd7112015-07-05 20:05:44 +020030#endif
31
Wenzel Jakob48548ea2016-01-17 22:36:44 +010032#include "attr.h"
Wenzel Jakob38bd7112015-07-05 20:05:44 +020033
Wenzel Jakob8f4eb002015-10-15 18:13:33 +020034NAMESPACE_BEGIN(pybind11)
Wenzel Jakob38bd7112015-07-05 20:05:44 +020035
Wenzel Jakobd4258ba2015-07-26 16:33:49 +020036/// Wraps an arbitrary C++ function/method/lambda function/.. into a callable Python object
Wenzel Jakobbd4a5292015-07-11 17:41:48 +020037class cpp_function : public function {
Wenzel Jakobd4258ba2015-07-26 16:33:49 +020038public:
Wenzel Jakobbd4a5292015-07-11 17:41:48 +020039 cpp_function() { }
Wenzel Jakob38bd7112015-07-05 20:05:44 +020040
Wenzel Jakob5984baa2016-05-10 15:05:03 +010041 /// Construct a cpp_function from a vanilla function pointer
Wenzel Jakob66c9a402016-01-17 22:36:36 +010042 template <typename Return, typename... Args, typename... Extra>
Wenzel Jakob48548ea2016-01-17 22:36:44 +010043 cpp_function(Return (*f)(Args...), const Extra&... extra) {
Wenzel Jakob5984baa2016-05-10 15:05:03 +010044 initialize(f, f, extra...);
Wenzel Jakob38bd7112015-07-05 20:05:44 +020045 }
Wenzel Jakobd4258ba2015-07-26 16:33:49 +020046
Wenzel Jakob5984baa2016-05-10 15:05:03 +010047 /// Construct a cpp_function from a lambda function (possibly with internal state)
Wenzel Jakob48548ea2016-01-17 22:36:44 +010048 template <typename Func, typename... Extra> cpp_function(Func &&f, const Extra&... extra) {
Wenzel Jakob71867832015-07-29 17:43:52 +020049 initialize(std::forward<Func>(f),
Wenzel Jakobd4258ba2015-07-26 16:33:49 +020050 (typename detail::remove_class<decltype(
Wenzel Jakob48548ea2016-01-17 22:36:44 +010051 &std::remove_reference<Func>::type::operator())>::type *) nullptr, extra...);
Wenzel Jakobd4258ba2015-07-26 16:33:49 +020052 }
53
Wenzel Jakob5984baa2016-05-10 15:05:03 +010054 /// Construct a cpp_function from a class method (non-const)
55 template <typename Return, typename Class, typename... Arg, typename... Extra>
56 cpp_function(Return (Class::*f)(Arg...), const Extra&... extra) {
Wenzel Jakob71867832015-07-29 17:43:52 +020057 initialize([f](Class *c, Arg... args) -> Return { return (c->*f)(args...); },
Wenzel Jakob48548ea2016-01-17 22:36:44 +010058 (Return (*) (Class *, Arg...)) nullptr, extra...);
Wenzel Jakobd4258ba2015-07-26 16:33:49 +020059 }
60
Wenzel Jakob5984baa2016-05-10 15:05:03 +010061 /// Construct a cpp_function from a class method (const)
62 template <typename Return, typename Class, typename... Arg, typename... Extra>
63 cpp_function(Return (Class::*f)(Arg...) const, const Extra&... extra) {
Wenzel Jakob71867832015-07-29 17:43:52 +020064 initialize([f](const Class *c, Arg... args) -> Return { return (c->*f)(args...); },
Wenzel Jakob48548ea2016-01-17 22:36:44 +010065 (Return (*)(const Class *, Arg ...)) nullptr, extra...);
Wenzel Jakobd4258ba2015-07-26 16:33:49 +020066 }
67
Wenzel Jakob57082212015-09-04 23:42:12 +020068 /// Return the function name
Wenzel Jakob48548ea2016-01-17 22:36:44 +010069 object name() const { return attr("__name__"); }
Wenzel Jakob57082212015-09-04 23:42:12 +020070
Wenzel Jakobd561cb02016-01-17 22:36:41 +010071protected:
72 /// Special internal constructor for functors, lambda functions, etc.
Wenzel Jakob66c9a402016-01-17 22:36:36 +010073 template <typename Func, typename Return, typename... Args, typename... Extra>
Wenzel Jakob48548ea2016-01-17 22:36:44 +010074 void initialize(Func &&f, Return (*)(Args...), const Extra&... extra) {
Dean Moldovan9e625582016-06-04 00:27:32 +020075 static_assert(detail::expected_num_args<Extra...>(sizeof...(Args)),
76 "The number of named arguments does not match the function signature");
77
Wenzel Jakob66c9a402016-01-17 22:36:36 +010078 struct capture { typename std::remove_reference<Func>::type f; };
Wenzel Jakobd4258ba2015-07-26 16:33:49 +020079
Wenzel Jakobd561cb02016-01-17 22:36:41 +010080 /* Store the function including any extra state it might have (e.g. a lambda capture object) */
Wenzel Jakob48548ea2016-01-17 22:36:44 +010081 auto rec = new detail::function_record();
Wenzel Jakob71867832015-07-29 17:43:52 +020082
Wenzel Jakob5984baa2016-05-10 15:05:03 +010083 /* Store the capture object directly in the function record if there is enough space */
84 if (sizeof(capture) <= sizeof(rec->data)) {
Wenzel Jakob954b7932016-07-10 10:13:18 +020085 /* Without these pragmas, GCC warns that there might not be
86 enough space to use the placement new operator. However, the
87 'if' statement above ensures that this is the case. */
Jason Rhinelander0b12f912016-07-07 16:26:04 -040088#if defined(__GNUG__) && !defined(__clang__) && __GNUC__ >= 6
Jason Rhinelandercae0e002016-07-07 16:11:42 -040089# pragma GCC diagnostic push
90# pragma GCC diagnostic ignored "-Wplacement-new"
91#endif
Wenzel Jakob5984baa2016-05-10 15:05:03 +010092 new ((capture *) &rec->data) capture { std::forward<Func>(f) };
Jason Rhinelander0b12f912016-07-07 16:26:04 -040093#if defined(__GNUG__) && !defined(__clang__) && __GNUC__ >= 6
Jason Rhinelandercae0e002016-07-07 16:11:42 -040094# pragma GCC diagnostic pop
95#endif
Wenzel Jakob5984baa2016-05-10 15:05:03 +010096 if (!std::is_trivially_destructible<Func>::value)
97 rec->free_data = [](detail::function_record *r) { ((capture *) &r->data)->~capture(); };
98 } else {
99 rec->data[0] = new capture { std::forward<Func>(f) };
100 rec->free_data = [](detail::function_record *r) { delete ((capture *) r->data[0]); };
101 }
Wenzel Jakob19208fe2015-10-13 17:37:25 +0200102
Wenzel Jakob5984baa2016-05-10 15:05:03 +0100103 /* Type casters for the function arguments and return value */
104 typedef detail::type_caster<typename std::tuple<Args...>> cast_in;
105 typedef detail::type_caster<typename std::conditional<
106 std::is_void<Return>::value, detail::void_type,
107 typename detail::intrinsic_type<Return>::type>::type> cast_out;
Wenzel Jakob71867832015-07-29 17:43:52 +0200108
Wenzel Jakobd561cb02016-01-17 22:36:41 +0100109 /* Dispatch code which converts function arguments and performs the actual function call */
Wenzel Jakob178c8a82016-05-10 15:59:01 +0100110 rec->impl = [](detail::function_record *rec, handle args, handle kwargs, handle parent) -> handle {
Wenzel Jakob5984baa2016-05-10 15:05:03 +0100111 cast_in args_converter;
Wenzel Jakobd561cb02016-01-17 22:36:41 +0100112
113 /* Try to cast the function arguments into the C++ domain */
Wenzel Jakob178c8a82016-05-10 15:59:01 +0100114 if (!args_converter.load_args(args, kwargs, true))
Wenzel Jakobd561cb02016-01-17 22:36:41 +0100115 return PYBIND11_TRY_NEXT_OVERLOAD;
116
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100117 /* Invoke call policy pre-call hook */
Wenzel Jakob5984baa2016-05-10 15:05:03 +0100118 detail::process_attributes<Extra...>::precall(args);
Wenzel Jakobd561cb02016-01-17 22:36:41 +0100119
Wenzel Jakob5984baa2016-05-10 15:05:03 +0100120 /* Get a pointer to the capture object */
121 capture *cap = (capture *) (sizeof(capture) <= sizeof(rec->data)
122 ? &rec->data : rec->data[0]);
123
Wenzel Jakob954b7932016-07-10 10:13:18 +0200124 /* Perform the function call */
Wenzel Jakob5984baa2016-05-10 15:05:03 +0100125 handle result = cast_out::cast(args_converter.template call<Return>(cap->f),
126 rec->policy, parent);
Wenzel Jakobd561cb02016-01-17 22:36:41 +0100127
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100128 /* Invoke call policy post-call hook */
Wenzel Jakob5984baa2016-05-10 15:05:03 +0100129 detail::process_attributes<Extra...>::postcall(args, result);
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100130
Wenzel Jakob5f218b32016-01-17 22:36:39 +0100131 return result;
Wenzel Jakob71867832015-07-29 17:43:52 +0200132 };
133
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100134 /* Process any user-provided function attributes */
135 detail::process_attributes<Extra...>::init(extra..., rec);
Wenzel Jakobd561cb02016-01-17 22:36:41 +0100136
137 /* Generate a readable signature describing the function's arguments and return value types */
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100138 using detail::descr;
Sylvain Corlay4c7bf9b2016-03-08 18:44:04 -0500139 PYBIND11_DESCR signature = cast_in::name() + detail::_(" -> ") + cast_out::name();
Wenzel Jakobd561cb02016-01-17 22:36:41 +0100140
141 /* Register the function with Python from generic (non-templated) code */
Wenzel Jakob5984baa2016-05-10 15:05:03 +0100142 initialize_generic(rec, signature.text(), signature.types(), sizeof...(Args));
Wenzel Jakob178c8a82016-05-10 15:59:01 +0100143
144 if (cast_in::has_args) rec->has_args = true;
145 if (cast_in::has_kwargs) rec->has_kwargs = true;
Wenzel Jakob954b7932016-07-10 10:13:18 +0200146
147 /* Stash some additional information used by an important optimization in 'functional.h' */
148 using FunctionType = Return (*)(Args...);
149 constexpr bool is_function_ptr =
150 std::is_convertible<Func, FunctionType>::value &&
151 sizeof(capture) == sizeof(void *);
152 if (is_function_ptr) {
153 rec->is_stateless = true;
154 rec->data[1] = (void *) &typeid(FunctionType);
155 }
Wenzel Jakob281aa0e2015-07-30 15:29:00 +0200156 }
157
Wenzel Jakobd561cb02016-01-17 22:36:41 +0100158 /// Register a function call with Python (generic non-templated code goes here)
Wenzel Jakob5984baa2016-05-10 15:05:03 +0100159 void initialize_generic(detail::function_record *rec, const char *text,
160 const std::type_info *const *types, int args) {
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100161
Wenzel Jakob66c9a402016-01-17 22:36:36 +0100162 /* Create copies of all referenced C-style strings */
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100163 rec->name = strdup(rec->name ? rec->name : "");
164 if (rec->doc) rec->doc = strdup(rec->doc);
165 for (auto &a: rec->args) {
Wenzel Jakob66c9a402016-01-17 22:36:36 +0100166 if (a.name)
167 a.name = strdup(a.name);
168 if (a.descr)
169 a.descr = strdup(a.descr);
170 else if (a.value)
Wenzel Jakob6c03beb2016-05-08 14:34:09 +0200171 a.descr = strdup(((std::string) ((object) handle(a.value).attr("__repr__"))().str()).c_str());
Wenzel Jakob66c9a402016-01-17 22:36:36 +0100172 }
Wenzel Jakob954b7932016-07-10 10:13:18 +0200173
Wenzel Jakobd561cb02016-01-17 22:36:41 +0100174 auto const &registered_types = detail::get_internals().registered_types_cpp;
Wenzel Jakob66c9a402016-01-17 22:36:36 +0100175
176 /* Generate a proper function signature */
177 std::string signature;
178 size_t type_depth = 0, char_index = 0, type_index = 0, arg_index = 0;
179 while (true) {
180 char c = text[char_index++];
181 if (c == '\0')
182 break;
183
184 if (c == '{') {
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100185 if (type_depth == 1 && arg_index < rec->args.size()) {
186 signature += rec->args[arg_index].name;
Wenzel Jakob66c9a402016-01-17 22:36:36 +0100187 signature += " : ";
188 }
189 ++type_depth;
190 } else if (c == '}') {
191 --type_depth;
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100192 if (type_depth == 1 && arg_index < rec->args.size()) {
193 if (rec->args[arg_index].descr) {
Wenzel Jakob66c9a402016-01-17 22:36:36 +0100194 signature += " = ";
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100195 signature += rec->args[arg_index].descr;
Wenzel Jakob66c9a402016-01-17 22:36:36 +0100196 }
197 arg_index++;
198 }
199 } else if (c == '%') {
200 const std::type_info *t = types[type_index++];
Wenzel Jakobb2c2c792016-01-17 22:36:40 +0100201 if (!t)
Wenzel Jakob678d7872016-01-17 22:36:41 +0100202 pybind11_fail("Internal error while parsing type signature (1)");
Wenzel Jakobb6cf75d2016-01-29 11:39:32 +0100203 auto it = registered_types.find(std::type_index(*t));
Wenzel Jakob66c9a402016-01-17 22:36:36 +0100204 if (it != registered_types.end()) {
Wenzel Jakobd561cb02016-01-17 22:36:41 +0100205 signature += ((const detail::type_info *) it->second)->type->tp_name;
Wenzel Jakob66c9a402016-01-17 22:36:36 +0100206 } else {
207 std::string tname(t->name());
208 detail::clean_type_id(tname);
209 signature += tname;
210 }
211 } else {
212 signature += c;
213 }
214 }
Wenzel Jakob95d18692016-01-17 22:36:40 +0100215 if (type_depth != 0 || types[type_index] != nullptr)
Wenzel Jakob678d7872016-01-17 22:36:41 +0100216 pybind11_fail("Internal error while parsing type signature (2)");
Wenzel Jakob66c9a402016-01-17 22:36:36 +0100217
218 #if !defined(PYBIND11_CPP14)
219 delete[] types;
220 delete[] text;
221 #endif
Wenzel Jakobf5fae922015-08-24 15:31:24 +0200222
Wenzel Jakob57082212015-09-04 23:42:12 +0200223#if PY_MAJOR_VERSION < 3
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100224 if (strcmp(rec->name, "__next__") == 0) {
225 std::free(rec->name);
226 rec->name = strdup("next");
Wenzel Jakobd1bfc4e2016-05-16 18:52:46 +0200227 } else if (strcmp(rec->name, "__bool__") == 0) {
228 std::free(rec->name);
229 rec->name = strdup("__nonzero__");
Wenzel Jakob66c9a402016-01-17 22:36:36 +0100230 }
Wenzel Jakob57082212015-09-04 23:42:12 +0200231#endif
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100232 rec->signature = strdup(signature.c_str());
233 rec->args.shrink_to_fit();
Wenzel Jakoba380ed92016-05-15 23:54:13 +0200234 rec->is_constructor = !strcmp(rec->name, "__init__") || !strcmp(rec->name, "__setstate__");
Wenzel Jakob954b7932016-07-10 10:13:18 +0200235 rec->is_stateless = false;
Wenzel Jakob178c8a82016-05-10 15:59:01 +0100236 rec->has_args = false;
237 rec->has_kwargs = false;
Wenzel Jakobaffb9f42016-05-15 23:55:06 +0200238 rec->nargs = (uint16_t) args;
Wenzel Jakobbd4a5292015-07-11 17:41:48 +0200239
Wenzel Jakob57082212015-09-04 23:42:12 +0200240#if PY_MAJOR_VERSION < 3
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100241 if (rec->sibling && PyMethod_Check(rec->sibling.ptr()))
242 rec->sibling = PyMethod_GET_FUNCTION(rec->sibling.ptr());
Wenzel Jakob57082212015-09-04 23:42:12 +0200243#endif
244
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100245 detail::function_record *chain = nullptr, *chain_start = rec;
246 if (rec->sibling && PyCFunction_Check(rec->sibling.ptr())) {
247 capsule rec_capsule(PyCFunction_GetSelf(rec->sibling.ptr()), true);
248 chain = (detail::function_record *) rec_capsule;
Wenzel Jakobd561cb02016-01-17 22:36:41 +0100249 /* Never append a method to an overload chain of a parent class;
250 instead, hide the parent's overloads in this case */
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100251 if (chain->class_ != rec->class_)
252 chain = nullptr;
Wenzel Jakoba2f6fde2015-10-01 16:46:03 +0200253 }
254
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100255 if (!chain) {
Wenzel Jakobd561cb02016-01-17 22:36:41 +0100256 /* No existing overload was found, create a new function object */
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100257 rec->def = new PyMethodDef();
258 memset(rec->def, 0, sizeof(PyMethodDef));
259 rec->def->ml_name = rec->name;
260 rec->def->ml_meth = reinterpret_cast<PyCFunction>(*dispatcher);
261 rec->def->ml_flags = METH_VARARGS | METH_KEYWORDS;
Wenzel Jakoba6501792016-02-04 23:02:07 +0100262
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100263 capsule rec_capsule(rec, [](PyObject *o) {
264 destruct((detail::function_record *) PyCapsule_GetPointer(o, nullptr));
Wenzel Jakobd561cb02016-01-17 22:36:41 +0100265 });
Wenzel Jakoba6501792016-02-04 23:02:07 +0100266
267 object scope_module;
268 if (rec->scope) {
269 scope_module = (object) rec->scope.attr("__module__");
270 if (!scope_module)
271 scope_module = (object) rec->scope.attr("__name__");
272 }
273
274 m_ptr = PyCFunction_NewEx(rec->def, rec_capsule.ptr(), scope_module.ptr());
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200275 if (!m_ptr)
Wenzel Jakob678d7872016-01-17 22:36:41 +0100276 pybind11_fail("cpp_function::cpp_function(): Could not allocate function object");
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200277 } else {
Wenzel Jakobd561cb02016-01-17 22:36:41 +0100278 /* Append at the end of the overload chain */
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100279 m_ptr = rec->sibling.ptr();
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200280 inc_ref();
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100281 chain_start = chain;
282 while (chain->next)
283 chain = chain->next;
284 chain->next = rec;
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200285 }
Wenzel Jakob281aa0e2015-07-30 15:29:00 +0200286
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200287 std::string signatures;
Wenzel Jakob71867832015-07-29 17:43:52 +0200288 int index = 0;
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100289 /* Create a nice pydoc rec including all signatures and
Wenzel Jakobd561cb02016-01-17 22:36:41 +0100290 docstrings of the functions in the overload chain */
Sylvain Corlay0e04fdf2016-03-08 16:42:12 -0500291 if (chain) {
292 // First a generic signature
293 signatures += rec->name;
294 signatures += "(*args, **kwargs)\n";
295 signatures += "Overloaded function.\n\n";
296 }
297 // Then specific overload signatures
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100298 for (auto it = chain_start; it != nullptr; it = it->next) {
299 if (chain)
Wenzel Jakob71867832015-07-29 17:43:52 +0200300 signatures += std::to_string(++index) + ". ";
Sylvain Corlay13b22bf2016-02-28 21:23:39 -0500301 signatures += rec->name;
Wenzel Jakob66c9a402016-01-17 22:36:36 +0100302 signatures += it->signature;
303 signatures += "\n";
304 if (it->doc && strlen(it->doc) > 0) {
305 signatures += "\n";
Wenzel Jakob218b6ce2016-02-28 23:52:37 +0100306 signatures += it->doc;
Wenzel Jakob66c9a402016-01-17 22:36:36 +0100307 signatures += "\n";
308 }
Wenzel Jakob71867832015-07-29 17:43:52 +0200309 if (it->next)
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200310 signatures += "\n";
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200311 }
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100312
313 /* Install docstring */
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200314 PyCFunctionObject *func = (PyCFunctionObject *) m_ptr;
315 if (func->m_ml->ml_doc)
Wenzel Jakobd561cb02016-01-17 22:36:41 +0100316 std::free((char *) func->m_ml->ml_doc);
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200317 func->m_ml->ml_doc = strdup(signatures.c_str());
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100318
319 if (rec->class_) {
320 m_ptr = PYBIND11_INSTANCE_METHOD_NEW(m_ptr, rec->class_.ptr());
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200321 if (!m_ptr)
Wenzel Jakob678d7872016-01-17 22:36:41 +0100322 pybind11_fail("cpp_function::cpp_function(): Could not allocate instance method object");
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200323 Py_DECREF(func);
324 }
325 }
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100326
327 /// When a cpp_function is GCed, release any memory allocated by pybind11
328 static void destruct(detail::function_record *rec) {
329 while (rec) {
330 detail::function_record *next = rec->next;
331 if (rec->free_data)
Wenzel Jakob5984baa2016-05-10 15:05:03 +0100332 rec->free_data(rec);
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100333 std::free((char *) rec->name);
334 std::free((char *) rec->doc);
335 std::free((char *) rec->signature);
336 for (auto &arg: rec->args) {
337 std::free((char *) arg.name);
338 std::free((char *) arg.descr);
339 arg.value.dec_ref();
340 }
341 if (rec->def) {
342 std::free((char *) rec->def->ml_doc);
343 delete rec->def;
344 }
345 delete rec;
346 rec = next;
347 }
348 }
349
350 /// Main dispatch logic for calls to functions bound using pybind11
351 static PyObject *dispatcher(PyObject *self, PyObject *args, PyObject *kwargs) {
352 /* Iterator over the list of potentially admissible overloads */
353 detail::function_record *overloads = (detail::function_record *) PyCapsule_GetPointer(self, nullptr),
354 *it = overloads;
355
356 /* Need to know how many arguments + keyword arguments there are to pick the right overload */
Wenzel Jakob0a078052016-05-29 13:40:40 +0200357 size_t nargs = (size_t) PyTuple_GET_SIZE(args),
358 nkwargs = kwargs ? (size_t) PyDict_Size(kwargs) : 0;
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100359
Wenzel Jakob178c8a82016-05-10 15:59:01 +0100360 handle parent = nargs > 0 ? PyTuple_GET_ITEM(args, 0) : nullptr,
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100361 result = PYBIND11_TRY_NEXT_OVERLOAD;
362 try {
363 for (; it != nullptr; it = it->next) {
364 tuple args_(args, true);
Wenzel Jakob178c8a82016-05-10 15:59:01 +0100365 size_t kwargs_consumed = 0;
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100366
367 /* For each overload:
368 1. If the required list of arguments is longer than the
369 actually provided amount, create a copy of the argument
370 list and fill in any available keyword/default arguments.
371 2. Ensure that all keyword arguments were "consumed"
372 3. Call the function call dispatcher (function_record::impl)
373 */
Wenzel Jakob178c8a82016-05-10 15:59:01 +0100374 size_t nargs_ = nargs;
375 if (nargs < it->args.size()) {
376 nargs_ = it->args.size();
377 args_ = tuple(nargs_);
378 for (size_t i = 0; i < nargs; ++i) {
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100379 handle item = PyTuple_GET_ITEM(args, i);
380 PyTuple_SET_ITEM(args_.ptr(), i, item.inc_ref().ptr());
381 }
382
383 int arg_ctr = 0;
384 for (auto const &it2 : it->args) {
385 int index = arg_ctr++;
386 if (PyTuple_GET_ITEM(args_.ptr(), index))
387 continue;
388
389 handle value;
390 if (kwargs)
391 value = PyDict_GetItemString(kwargs, it2.name);
392
393 if (value)
394 kwargs_consumed++;
395 else if (it2.value)
396 value = it2.value;
397
398 if (value) {
399 PyTuple_SET_ITEM(args_.ptr(), index, value.inc_ref().ptr());
400 } else {
Wenzel Jakob178c8a82016-05-10 15:59:01 +0100401 kwargs_consumed = (size_t) -1; /* definite failure */
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100402 break;
403 }
404 }
405 }
Wenzel Jakobbd57eb42016-05-01 14:42:20 +0200406
407 try {
Wenzel Jakob178c8a82016-05-10 15:59:01 +0100408 if ((kwargs_consumed == nkwargs || it->has_kwargs) &&
409 (nargs_ == it->nargs || it->has_args))
410 result = it->impl(it, args_, kwargs, parent);
Wenzel Jakob00062592016-07-01 16:07:35 +0200411 } catch (reference_cast_error &) {
Wenzel Jakobbd57eb42016-05-01 14:42:20 +0200412 result = PYBIND11_TRY_NEXT_OVERLOAD;
413 }
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100414
415 if (result.ptr() != PYBIND11_TRY_NEXT_OVERLOAD)
416 break;
417 }
Pim Schellart5a7d17f2016-06-17 17:35:59 -0400418 } catch (const error_already_set &) {
419 return nullptr;
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100420 } catch (...) {
Pim Schellart5a7d17f2016-06-17 17:35:59 -0400421 /* When an exception is caught, give each registered exception
422 translator a chance to translate it to a Python exception
423 in reverse order of registration.
Wenzel Jakob1f66a582016-07-18 10:47:10 +0200424
Pim Schellart5a7d17f2016-06-17 17:35:59 -0400425 A translator may choose to do one of the following:
Wenzel Jakob1f66a582016-07-18 10:47:10 +0200426
Pim Schellart5a7d17f2016-06-17 17:35:59 -0400427 - catch the exception and call PyErr_SetString or PyErr_SetObject
428 to set a standard (or custom) Python exception, or
429 - do nothing and let the exception fall through to the next translator, or
430 - delegate translation to the next translator by throwing a new type of exception. */
431
Wenzel Jakob1f66a582016-07-18 10:47:10 +0200432 auto last_exception = std::current_exception();
Pim Schellart5a7d17f2016-06-17 17:35:59 -0400433 auto &registered_exception_translators = pybind11::detail::get_internals().registered_exception_translators;
434 for (auto& translator : registered_exception_translators) {
435 try {
436 translator(last_exception);
437 } catch (...) {
438 last_exception = std::current_exception();
439 continue;
440 }
441 return nullptr;
442 }
443 PyErr_SetString(PyExc_SystemError, "Exception escaped from default exception translator!");
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100444 return nullptr;
445 }
446
447 if (result.ptr() == PYBIND11_TRY_NEXT_OVERLOAD) {
Jason Rhinelander4e45e182016-07-17 17:43:00 -0400448 std::string msg = "Incompatible " + std::string(overloads->is_constructor ? "constructor" : "function") +
449 " arguments. The following argument types are supported:\n";
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100450 int ctr = 0;
451 for (detail::function_record *it2 = overloads; it2 != nullptr; it2 = it2->next) {
452 msg += " "+ std::to_string(++ctr) + ". ";
Jason Rhinelander4e45e182016-07-17 17:43:00 -0400453
454 bool wrote_sig = false;
455 if (overloads->is_constructor) {
456 // For a constructor, rewrite `(Object, arg0, ...) -> NoneType` as `Object(arg0, ...)`
457 std::string sig = it2->signature;
458 size_t start = sig.find('(') + 1;
459 if (start < sig.size()) {
460 // End at the , for the next argument
461 size_t end = sig.find(", "), next = end + 2;
462 size_t ret = sig.rfind(" -> ");
463 // Or the ), if there is no comma:
464 if (end >= sig.size()) next = end = sig.find(')');
465 if (start < end && next < sig.size()) {
466 msg.append(sig, start, end - start);
467 msg += '(';
468 msg.append(sig, next, ret - next);
469 wrote_sig = true;
470 }
471 }
472 }
473 if (!wrote_sig) msg += it2->signature;
474
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100475 msg += "\n";
476 }
Andreas Bergmeier16d43942016-05-24 09:19:35 +0200477 msg += " Invoked with: ";
478 tuple args_(args, true);
Wenzel Jakob1f66a582016-07-18 10:47:10 +0200479 for (size_t ti = overloads->is_constructor ? 1 : 0; ti < args_.size(); ++ti) {
Andreas Bergmeier16d43942016-05-24 09:19:35 +0200480 msg += static_cast<std::string>(static_cast<object>(args_[ti]).str());
481 if ((ti + 1) != args_.size() )
482 msg += ", ";
483 }
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100484 PyErr_SetString(PyExc_TypeError, msg.c_str());
485 return nullptr;
486 } else if (!result) {
487 std::string msg = "Unable to convert function return value to a "
488 "Python type! The signature was\n\t";
489 msg += it->signature;
490 PyErr_SetString(PyExc_TypeError, msg.c_str());
491 return nullptr;
492 } else {
493 if (overloads->is_constructor) {
Wenzel Jakob772c6d52016-04-30 19:56:10 +0200494 /* When a constructor ran successfully, the corresponding
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100495 holder type (e.g. std::unique_ptr) must still be initialized. */
Wenzel Jakob178c8a82016-05-10 15:59:01 +0100496 PyObject *inst = PyTuple_GET_ITEM(args, 0);
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100497 auto tinfo = detail::get_type_info(Py_TYPE(inst));
498 tinfo->init_holder(inst, nullptr);
499 }
500 return result.ptr();
501 }
502 }
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200503};
504
Wenzel Jakobd561cb02016-01-17 22:36:41 +0100505/// Wrapper for Python extension modules
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200506class module : public object {
507public:
Wenzel Jakobb1b71402015-10-18 16:48:30 +0200508 PYBIND11_OBJECT_DEFAULT(module, object, PyModule_Check)
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200509
510 module(const char *name, const char *doc = nullptr) {
Wenzel Jakob57082212015-09-04 23:42:12 +0200511#if PY_MAJOR_VERSION >= 3
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200512 PyModuleDef *def = new PyModuleDef();
513 memset(def, 0, sizeof(PyModuleDef));
514 def->m_name = name;
515 def->m_doc = doc;
516 def->m_size = -1;
517 Py_INCREF(def);
518 m_ptr = PyModule_Create(def);
Wenzel Jakob57082212015-09-04 23:42:12 +0200519#else
520 m_ptr = Py_InitModule3(name, nullptr, doc);
521#endif
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200522 if (m_ptr == nullptr)
Wenzel Jakob678d7872016-01-17 22:36:41 +0100523 pybind11_fail("Internal error in module::module()");
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200524 inc_ref();
525 }
526
Wenzel Jakob71867832015-07-29 17:43:52 +0200527 template <typename Func, typename... Extra>
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100528 module &def(const char *name_, Func &&f, const Extra& ... extra) {
Wenzel Jakob71867832015-07-29 17:43:52 +0200529 cpp_function func(std::forward<Func>(f), name(name_),
Wenzel Jakoba6501792016-02-04 23:02:07 +0100530 sibling((handle) attr(name_)), scope(*this), extra...);
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100531 /* PyModule_AddObject steals a reference to 'func' */
532 PyModule_AddObject(ptr(), name_, func.inc_ref().ptr());
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200533 return *this;
534 }
535
Wenzel Jakobbd4a5292015-07-11 17:41:48 +0200536 module def_submodule(const char *name, const char *doc = nullptr) {
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200537 std::string full_name = std::string(PyModule_GetName(m_ptr))
538 + std::string(".") + std::string(name);
539 module result(PyImport_AddModule(full_name.c_str()), true);
Wenzel Jakobbd4a5292015-07-11 17:41:48 +0200540 if (doc)
Wenzel Jakob8f4eb002015-10-15 18:13:33 +0200541 result.attr("__doc__") = pybind11::str(doc);
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200542 attr(name) = result;
543 return result;
544 }
Wenzel Jakobdb028d62015-10-13 23:44:25 +0200545
546 static module import(const char *name) {
Wenzel Jakobdd57a342015-12-26 14:04:52 +0100547 PyObject *obj = PyImport_ImportModule(name);
548 if (!obj)
Wenzel Jakob678d7872016-01-17 22:36:41 +0100549 pybind11_fail("Module \"" + std::string(name) + "\" not found!");
Wenzel Jakobdd57a342015-12-26 14:04:52 +0100550 return module(obj, false);
Wenzel Jakobdb028d62015-10-13 23:44:25 +0200551 }
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200552};
553
554NAMESPACE_BEGIN(detail)
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100555/// Generic support for creating new Python heap types
Wenzel Jakobd561cb02016-01-17 22:36:41 +0100556class generic_type : public object {
Wenzel Jakob86d825f2016-05-26 13:19:27 +0200557 template <typename type, typename holder_type, typename type_alias> friend class class_;
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200558public:
Wenzel Jakobd561cb02016-01-17 22:36:41 +0100559 PYBIND11_OBJECT_DEFAULT(generic_type, object, PyType_Check)
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100560protected:
561 void initialize(type_record *rec) {
562 if (rec->base_type) {
563 if (rec->base_handle)
564 pybind11_fail("generic_type: specified base type multiple times!");
565 rec->base_handle = detail::get_type_handle(*(rec->base_type));
566 if (!rec->base_handle) {
567 std::string tname(rec->base_type->name());
568 detail::clean_type_id(tname);
569 pybind11_fail("generic_type: type \"" + std::string(rec->name) +
570 "\" referenced unknown base type \"" + tname + "\"");
571 }
572 }
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200573
Wenzel Jakob38d8b8c2016-05-31 09:53:28 +0200574 auto &internals = get_internals();
575 auto tindex = std::type_index(*(rec->type));
576
577 if (internals.registered_types_cpp.find(tindex) !=
578 internals.registered_types_cpp.end())
579 pybind11_fail("generic_type: type \"" + std::string(rec->name) +
580 "\" is already registered!");
581
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100582 object name(PYBIND11_FROM_STRING(rec->name), false);
Wenzel Jakobc47d4982016-07-11 23:40:28 +0200583 object scope_module;
584 if (rec->scope) {
585 scope_module = (object) rec->scope.attr("__module__");
586 if (!scope_module)
587 scope_module = (object) rec->scope.attr("__name__");
588 }
589
590#if PY_MAJOR_VERSION >= 3 && PY_MINOR_VERSION >= 3
591 /* Qualified names for Python >= 3.3 */
592 object scope_qualname;
593 if (rec->scope)
594 scope_qualname = (object) rec->scope.attr("__qualname__");
595 object ht_qualname;
596 if (scope_qualname) {
597 ht_qualname = object(PyUnicode_FromFormat(
598 "%U.%U", scope_qualname.ptr(), name.ptr()), false);
599 } else {
600 ht_qualname = name;
601 }
602#endif
603 std::string full_name = (scope_module ? ((std::string) scope_module.str() + "." + rec->name)
604 : std::string(rec->name));
605
606 char *tp_doc = nullptr;
607 if (rec->doc) {
608 /* Allocate memory for docstring (using PyObject_MALLOC, since
609 Python will free this later on) */
610 size_t size = strlen(rec->doc) + 1;
611 tp_doc = (char *) PyObject_MALLOC(size);
612 memcpy((void *) tp_doc, rec->doc, size);
613 }
614
615 object type_holder(PyType_Type.tp_alloc(&PyType_Type, 0), false);
Wenzel Jakobd561cb02016-01-17 22:36:41 +0100616 auto type = (PyHeapTypeObject*) type_holder.ptr();
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200617
Wenzel Jakobd561cb02016-01-17 22:36:41 +0100618 if (!type_holder || !name)
Wenzel Jakob678d7872016-01-17 22:36:41 +0100619 pybind11_fail("generic_type: unable to create type object!");
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200620
Wenzel Jakobd561cb02016-01-17 22:36:41 +0100621 /* Register supplemental type information in C++ dict */
Wenzel Jakobd561cb02016-01-17 22:36:41 +0100622 detail::type_info *tinfo = new detail::type_info();
623 tinfo->type = (PyTypeObject *) type;
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100624 tinfo->type_size = rec->type_size;
625 tinfo->init_holder = rec->init_holder;
Wenzel Jakob38d8b8c2016-05-31 09:53:28 +0200626 internals.registered_types_cpp[tindex] = tinfo;
Wenzel Jakobd561cb02016-01-17 22:36:41 +0100627 internals.registered_types_py[type] = tinfo;
628
Wenzel Jakobd561cb02016-01-17 22:36:41 +0100629 /* Basic type attributes */
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200630 type->ht_type.tp_name = strdup(full_name.c_str());
Wenzel Jakob0a078052016-05-29 13:40:40 +0200631 type->ht_type.tp_basicsize = (ssize_t) rec->instance_size;
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100632 type->ht_type.tp_base = (PyTypeObject *) rec->base_handle.ptr();
633 rec->base_handle.inc_ref();
Wenzel Jakobd561cb02016-01-17 22:36:41 +0100634
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100635 type->ht_name = name.release().ptr();
Wenzel Jakobd561cb02016-01-17 22:36:41 +0100636
Wenzel Jakobc47d4982016-07-11 23:40:28 +0200637#if PY_MAJOR_VERSION >= 3 && PY_MINOR_VERSION >= 3
638 type->ht_qualname = ht_qualname.release().ptr();
639#endif
640
Wenzel Jakobd561cb02016-01-17 22:36:41 +0100641 /* Supported protocols */
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200642 type->ht_type.tp_as_number = &type->as_number;
643 type->ht_type.tp_as_sequence = &type->as_sequence;
644 type->ht_type.tp_as_mapping = &type->as_mapping;
Wenzel Jakobd561cb02016-01-17 22:36:41 +0100645
646 /* Supported elementary operations */
647 type->ht_type.tp_init = (initproc) init;
648 type->ht_type.tp_new = (newfunc) new_instance;
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100649 type->ht_type.tp_dealloc = rec->dealloc;
Wenzel Jakobd561cb02016-01-17 22:36:41 +0100650
651 /* Support weak references (needed for the keep_alive feature) */
Wenzel Jakob88d1d042016-01-20 01:26:42 +0100652 type->ht_type.tp_weaklistoffset = offsetof(instance_essentials<void>, weakrefs);
Wenzel Jakob5f218b32016-01-17 22:36:39 +0100653
Wenzel Jakobd561cb02016-01-17 22:36:41 +0100654 /* Flags */
655 type->ht_type.tp_flags |= Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HEAPTYPE;
656#if PY_MAJOR_VERSION < 3
657 type->ht_type.tp_flags |= Py_TPFLAGS_CHECKTYPES;
658#endif
659 type->ht_type.tp_flags &= ~Py_TPFLAGS_HAVE_GC;
660
Wenzel Jakobc47d4982016-07-11 23:40:28 +0200661 type->ht_type.tp_doc = tp_doc;
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200662
663 if (PyType_Ready(&type->ht_type) < 0)
Wenzel Jakob678d7872016-01-17 22:36:41 +0100664 pybind11_fail("generic_type: PyType_Ready failed!");
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200665
Wenzel Jakobd561cb02016-01-17 22:36:41 +0100666 m_ptr = type_holder.ptr();
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200667
Wenzel Jakobd561cb02016-01-17 22:36:41 +0100668 if (scope_module) // Needed by pydoc
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100669 attr("__module__") = scope_module;
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200670
Wenzel Jakobd561cb02016-01-17 22:36:41 +0100671 /* Register type with the parent scope */
Wenzel Jakobb2825952016-04-13 23:33:00 +0200672 if (rec->scope)
673 rec->scope.attr(handle(type->ht_name)) = *this;
Wenzel Jakobd561cb02016-01-17 22:36:41 +0100674
675 type_holder.release();
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200676 }
677
Wenzel Jakobd561cb02016-01-17 22:36:41 +0100678 /// Allocate a metaclass on demand (for static properties)
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200679 handle metaclass() {
680 auto &ht_type = ((PyHeapTypeObject *) m_ptr)->ht_type;
Wenzel Jakobd561cb02016-01-17 22:36:41 +0100681 auto &ob_type = PYBIND11_OB_TYPE(ht_type);
Wenzel Jakob57082212015-09-04 23:42:12 +0200682
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200683 if (ob_type == &PyType_Type) {
Wenzel Jakobd561cb02016-01-17 22:36:41 +0100684 std::string name_ = std::string(ht_type.tp_name) + "__Meta";
Wenzel Jakobc47d4982016-07-11 23:40:28 +0200685#if PY_MAJOR_VERSION >= 3 && PY_MINOR_VERSION >= 3
686 object ht_qualname(PyUnicode_FromFormat(
687 "%U__Meta", ((object) attr("__qualname__")).ptr()), false);
688#endif
Wenzel Jakobd561cb02016-01-17 22:36:41 +0100689 object name(PYBIND11_FROM_STRING(name_.c_str()), false);
Wenzel Jakobc47d4982016-07-11 23:40:28 +0200690 object type_holder(PyType_Type.tp_alloc(&PyType_Type, 0), false);
Wenzel Jakobd561cb02016-01-17 22:36:41 +0100691 if (!type_holder || !name)
Wenzel Jakob678d7872016-01-17 22:36:41 +0100692 pybind11_fail("generic_type::metaclass(): unable to create type object!");
Wenzel Jakobd561cb02016-01-17 22:36:41 +0100693
694 auto type = (PyHeapTypeObject*) type_holder.ptr();
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100695 type->ht_name = name.release().ptr();
Wenzel Jakobc47d4982016-07-11 23:40:28 +0200696
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100697#if PY_MAJOR_VERSION >= 3 && PY_MINOR_VERSION >= 3
698 /* Qualified names for Python >= 3.3 */
Wenzel Jakobc47d4982016-07-11 23:40:28 +0200699 type->ht_qualname = ht_qualname.release().ptr();
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100700#endif
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200701 type->ht_type.tp_name = strdup(name_.c_str());
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100702 type->ht_type.tp_base = ob_type;
Wenzel Jakobd561cb02016-01-17 22:36:41 +0100703 type->ht_type.tp_flags |= (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HEAPTYPE) &
704 ~Py_TPFLAGS_HAVE_GC;
705
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200706 if (PyType_Ready(&type->ht_type) < 0)
Wenzel Jakob678d7872016-01-17 22:36:41 +0100707 pybind11_fail("generic_type::metaclass(): PyType_Ready failed!");
Wenzel Jakobd561cb02016-01-17 22:36:41 +0100708
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100709 ob_type = (PyTypeObject *) type_holder.release().ptr();
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200710 }
711 return handle((PyObject *) ob_type);
712 }
713
714 static int init(void *self, PyObject *, PyObject *) {
715 std::string msg = std::string(Py_TYPE(self)->tp_name) + ": No constructor defined!";
716 PyErr_SetString(PyExc_TypeError, msg.c_str());
717 return -1;
718 }
719
720 static PyObject *new_instance(PyTypeObject *type, PyObject *, PyObject *) {
Wenzel Jakobd561cb02016-01-17 22:36:41 +0100721 instance<void> *self = (instance<void> *) PyType_GenericAlloc((PyTypeObject *) type, 0);
722 auto tinfo = detail::get_type_info(type);
723 self->value = ::operator new(tinfo->type_size);
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200724 self->owned = true;
725 self->parent = nullptr;
726 self->constructed = false;
727 detail::get_internals().registered_instances[self->value] = (PyObject *) self;
728 return (PyObject *) self;
729 }
730
731 static void dealloc(instance<void> *self) {
732 if (self->value) {
733 bool dont_cache = self->parent && ((instance<void> *) self->parent)->value == self->value;
734 if (!dont_cache) { // avoid an issue with internal references matching their parent's address
735 auto &registered_instances = detail::get_internals().registered_instances;
736 auto it = registered_instances.find(self->value);
737 if (it == registered_instances.end())
Wenzel Jakob678d7872016-01-17 22:36:41 +0100738 pybind11_fail("generic_type::dealloc(): Tried to deallocate unregistered instance!");
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200739 registered_instances.erase(it);
740 }
741 Py_XDECREF(self->parent);
Wenzel Jakob5f218b32016-01-17 22:36:39 +0100742 if (self->weakrefs)
743 PyObject_ClearWeakRefs((PyObject *) self);
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200744 }
745 Py_TYPE(self)->tp_free((PyObject*) self);
746 }
747
Wenzel Jakob43398a82015-07-28 16:12:20 +0200748 void install_buffer_funcs(
749 buffer_info *(*get_buffer)(PyObject *, void *),
750 void *get_buffer_data) {
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200751 PyHeapTypeObject *type = (PyHeapTypeObject*) m_ptr;
752 type->ht_type.tp_as_buffer = &type->as_buffer;
Wenzel Jakob57082212015-09-04 23:42:12 +0200753#if PY_MAJOR_VERSION < 3
754 type->ht_type.tp_flags |= Py_TPFLAGS_HAVE_NEWBUFFER;
755#endif
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200756 type->as_buffer.bf_getbuffer = getbuffer;
757 type->as_buffer.bf_releasebuffer = releasebuffer;
Wenzel Jakobd561cb02016-01-17 22:36:41 +0100758 auto tinfo = detail::get_type_info(&type->ht_type);
759 tinfo->get_buffer = get_buffer;
760 tinfo->get_buffer_data = get_buffer_data;
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200761 }
762
763 static int getbuffer(PyObject *obj, Py_buffer *view, int flags) {
Wenzel Jakobd561cb02016-01-17 22:36:41 +0100764 auto tinfo = detail::get_type_info(Py_TYPE(obj));
765 if (view == nullptr || obj == nullptr || !tinfo || !tinfo->get_buffer) {
766 PyErr_SetString(PyExc_BufferError, "generic_type::getbuffer(): Internal error");
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200767 return -1;
768 }
769 memset(view, 0, sizeof(Py_buffer));
Wenzel Jakobd561cb02016-01-17 22:36:41 +0100770 buffer_info *info = tinfo->get_buffer(obj, tinfo->get_buffer_data);
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200771 view->obj = obj;
772 view->ndim = 1;
773 view->internal = info;
774 view->buf = info->ptr;
Wenzel Jakob0a078052016-05-29 13:40:40 +0200775 view->itemsize = (ssize_t) info->itemsize;
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200776 view->len = view->itemsize;
777 for (auto s : info->shape)
778 view->len *= s;
779 if ((flags & PyBUF_FORMAT) == PyBUF_FORMAT)
780 view->format = const_cast<char *>(info->format.c_str());
781 if ((flags & PyBUF_STRIDES) == PyBUF_STRIDES) {
Wenzel Jakob0a078052016-05-29 13:40:40 +0200782 view->ndim = (int) info->ndim;
Wenzel Jakob27e8e102016-01-17 22:36:37 +0100783 view->strides = (ssize_t *) &info->strides[0];
784 view->shape = (ssize_t *) &info->shape[0];
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200785 }
786 Py_INCREF(view->obj);
787 return 0;
788 }
789
790 static void releasebuffer(PyObject *, Py_buffer *view) { delete (buffer_info *) view->internal; }
791};
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200792NAMESPACE_END(detail)
793
Wenzel Jakob86d825f2016-05-26 13:19:27 +0200794template <typename type, typename holder_type = std::unique_ptr<type>, typename type_alias = type>
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100795class class_ : public detail::generic_type {
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200796public:
797 typedef detail::instance<type, holder_type> instance_type;
798
Wenzel Jakobd561cb02016-01-17 22:36:41 +0100799 PYBIND11_OBJECT(class_, detail::generic_type, PyType_Check)
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200800
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100801 template <typename... Extra>
802 class_(handle scope, const char *name, const Extra &... extra) {
803 detail::type_record record;
804 record.scope = scope;
805 record.name = name;
806 record.type = &typeid(type);
807 record.type_size = sizeof(type);
808 record.instance_size = sizeof(instance_type);
809 record.init_holder = init_holder;
810 record.dealloc = dealloc;
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200811
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100812 /* Process optional arguments, if any */
813 detail::process_attributes<Extra...>::init(extra..., &record);
814
815 detail::generic_type::initialize(&record);
Wenzel Jakob86d825f2016-05-26 13:19:27 +0200816
817 if (!std::is_same<type, type_alias>::value) {
818 auto &instances = pybind11::detail::get_internals().registered_types_cpp;
819 instances[std::type_index(typeid(type_alias))] = instances[std::type_index(typeid(type))];
820 }
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100821 }
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200822
Wenzel Jakob71867832015-07-29 17:43:52 +0200823 template <typename Func, typename... Extra>
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100824 class_ &def(const char *name_, Func&& f, const Extra&... extra) {
Wenzel Jakob57082212015-09-04 23:42:12 +0200825 cpp_function cf(std::forward<Func>(f), name(name_),
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100826 sibling(attr(name_)), is_method(*this),
827 extra...);
Wenzel Jakob57082212015-09-04 23:42:12 +0200828 attr(cf.name()) = cf;
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200829 return *this;
830 }
831
Wenzel Jakob71867832015-07-29 17:43:52 +0200832 template <typename Func, typename... Extra> class_ &
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100833 def_static(const char *name_, Func f, const Extra&... extra) {
Wenzel Jakob57082212015-09-04 23:42:12 +0200834 cpp_function cf(std::forward<Func>(f), name(name_),
Wenzel Jakoba6501792016-02-04 23:02:07 +0100835 sibling(attr(name_)), scope(*this), extra...);
Wenzel Jakob57082212015-09-04 23:42:12 +0200836 attr(cf.name()) = cf;
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200837 return *this;
838 }
839
Wenzel Jakob71867832015-07-29 17:43:52 +0200840 template <detail::op_id id, detail::op_type ot, typename L, typename R, typename... Extra>
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100841 class_ &def(const detail::op_<id, ot, L, R> &op, const Extra&... extra) {
Wenzel Jakobe2065642016-02-04 23:29:29 +0100842 op.template execute<type>(*this, extra...);
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200843 return *this;
844 }
845
Wenzel Jakob71867832015-07-29 17:43:52 +0200846 template <detail::op_id id, detail::op_type ot, typename L, typename R, typename... Extra>
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100847 class_ & def_cast(const detail::op_<id, ot, L, R> &op, const Extra&... extra) {
Wenzel Jakobe2065642016-02-04 23:29:29 +0100848 op.template execute_cast<type>(*this, extra...);
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200849 return *this;
850 }
851
Wenzel Jakob71867832015-07-29 17:43:52 +0200852 template <typename... Args, typename... Extra>
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100853 class_ &def(const detail::init<Args...> &init, const Extra&... extra) {
Wenzel Jakobe2065642016-02-04 23:29:29 +0100854 init.template execute<type>(*this, extra...);
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200855 return *this;
856 }
857
Wenzel Jakob86d825f2016-05-26 13:19:27 +0200858 template <typename... Args, typename... Extra>
859 class_ &def(const detail::init_alias<Args...> &init, const Extra&... extra) {
860 init.template execute<type>(*this, extra...);
861 return *this;
862 }
863
Wenzel Jakob71867832015-07-29 17:43:52 +0200864 template <typename Func> class_& def_buffer(Func &&func) {
Wenzel Jakob43398a82015-07-28 16:12:20 +0200865 struct capture { Func func; };
866 capture *ptr = new capture { std::forward<Func>(func) };
867 install_buffer_funcs([](PyObject *obj, void *ptr) -> buffer_info* {
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200868 detail::type_caster<type> caster;
869 if (!caster.load(obj, false))
870 return nullptr;
Wenzel Jakob43398a82015-07-28 16:12:20 +0200871 return new buffer_info(((capture *) ptr)->func(caster));
872 }, ptr);
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200873 return *this;
874 }
875
Wenzel Jakob71867832015-07-29 17:43:52 +0200876 template <typename C, typename D, typename... Extra>
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100877 class_ &def_readwrite(const char *name, D C::*pm, const Extra&... extra) {
Wenzel Jakob0b489582016-03-25 16:13:10 +0100878 cpp_function fget([pm](const C &c) -> const D &{ return c.*pm; }, is_method(*this)),
879 fset([pm](C &c, const D &value) { c.*pm = value; }, is_method(*this));
880 def_property(name, fget, fset, return_value_policy::reference_internal, extra...);
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200881 return *this;
882 }
883
Wenzel Jakob71867832015-07-29 17:43:52 +0200884 template <typename C, typename D, typename... Extra>
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100885 class_ &def_readonly(const char *name, const D C::*pm, const Extra& ...extra) {
Wenzel Jakob0b489582016-03-25 16:13:10 +0100886 cpp_function fget([pm](const C &c) -> const D &{ return c.*pm; }, is_method(*this));
887 def_property_readonly(name, fget, return_value_policy::reference_internal, extra...);
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200888 return *this;
889 }
890
Wenzel Jakob71867832015-07-29 17:43:52 +0200891 template <typename D, typename... Extra>
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100892 class_ &def_readwrite_static(const char *name, D *pm, const Extra& ...extra) {
Wenzel Jakob0b489582016-03-25 16:13:10 +0100893 cpp_function fget([pm](object) -> const D &{ return *pm; }, scope(*this)),
894 fset([pm](object, const D &value) { *pm = value; }, scope(*this));
895 def_property_static(name, fget, fset, return_value_policy::reference, extra...);
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200896 return *this;
897 }
898
Wenzel Jakob71867832015-07-29 17:43:52 +0200899 template <typename D, typename... Extra>
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100900 class_ &def_readonly_static(const char *name, const D *pm, const Extra& ...extra) {
Wenzel Jakob0b489582016-03-25 16:13:10 +0100901 cpp_function fget([pm](object) -> const D &{ return *pm; }, scope(*this));
902 def_property_readonly_static(name, fget, return_value_policy::reference, extra...);
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200903 return *this;
904 }
905
Wenzel Jakob84ec78f2016-03-21 17:54:24 +0100906 template <typename... Extra>
907 class_ &def_property_readonly(const char *name, const cpp_function &fget, const Extra& ...extra) {
908 def_property(name, fget, cpp_function(), extra...);
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200909 return *this;
910 }
911
Wenzel Jakob84ec78f2016-03-21 17:54:24 +0100912 template <typename... Extra>
913 class_ &def_property_readonly_static(const char *name, const cpp_function &fget, const Extra& ...extra) {
914 def_property_static(name, fget, cpp_function(), extra...);
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200915 return *this;
916 }
917
Wenzel Jakob84ec78f2016-03-21 17:54:24 +0100918 template <typename... Extra>
919 class_ &def_property(const char *name, const cpp_function &fget, const cpp_function &fset, const Extra& ...extra) {
Wenzel Jakob0b489582016-03-25 16:13:10 +0100920 return def_property_static(name, fget, fset, is_method(*this), extra...);
Wenzel Jakob84ec78f2016-03-21 17:54:24 +0100921 }
922
923 template <typename... Extra>
924 class_ &def_property_static(const char *name, const cpp_function &fget, const cpp_function &fset, const Extra& ...extra) {
925 auto rec_fget = get_function_record(fget), rec_fset = get_function_record(fset);
Wenzel Jakob7c99ff22016-06-02 20:33:01 +0200926 char *doc_prev = rec_fget->doc; /* 'extra' field may include a property-specific documentation string */
Wenzel Jakob84ec78f2016-03-21 17:54:24 +0100927 detail::process_attributes<Extra...>::init(extra..., rec_fget);
Wenzel Jakob7c99ff22016-06-02 20:33:01 +0200928 if (rec_fget->doc && rec_fget->doc != doc_prev) {
929 free(doc_prev);
930 rec_fget->doc = strdup(rec_fget->doc);
931 }
932 if (rec_fset) {
933 doc_prev = rec_fset->doc;
Wenzel Jakob84ec78f2016-03-21 17:54:24 +0100934 detail::process_attributes<Extra...>::init(extra..., rec_fset);
Wenzel Jakob7c99ff22016-06-02 20:33:01 +0200935 if (rec_fset->doc && rec_fset->doc != doc_prev) {
936 free(doc_prev);
937 rec_fset->doc = strdup(rec_fset->doc);
938 }
939 }
Wenzel Jakob84ec78f2016-03-21 17:54:24 +0100940 pybind11::str doc_obj = pybind11::str(rec_fget->doc ? rec_fget->doc : "");
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200941 object property(
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100942 PyObject_CallFunctionObjArgs((PyObject *) &PyProperty_Type, fget.ptr() ? fget.ptr() : Py_None,
943 fset.ptr() ? fset.ptr() : Py_None, Py_None, doc_obj.ptr(), nullptr), false);
Wenzel Jakob84ec78f2016-03-21 17:54:24 +0100944 if (rec_fget->class_)
945 attr(name) = property;
946 else
947 metaclass().attr(name) = property;
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200948 return *this;
949 }
Wenzel Jakoba2f6fde2015-10-01 16:46:03 +0200950
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200951private:
Wenzel Jakobb2c2c792016-01-17 22:36:40 +0100952 /// Initialize holder object, variant 1: object derives from enable_shared_from_this
953 template <typename T>
954 static void init_holder_helper(instance_type *inst, const holder_type * /* unused */, const std::enable_shared_from_this<T> * /* dummy */) {
Wenzel Jakob6e213c92015-11-24 23:05:58 +0100955 try {
hbruintjes70d2e572016-07-01 12:39:55 +0200956 new (&inst->holder) holder_type(std::static_pointer_cast<typename holder_type::element_type>(inst->value->shared_from_this()));
Wenzel Jakob6e213c92015-11-24 23:05:58 +0100957 } catch (const std::bad_weak_ptr &) {
958 new (&inst->holder) holder_type(inst->value);
959 }
Wenzel Jakobb2c2c792016-01-17 22:36:40 +0100960 }
961
962 /// Initialize holder object, variant 2: try to construct from existing holder object, if possible
963 template <typename T = holder_type,
964 typename std::enable_if<std::is_copy_constructible<T>::value, int>::type = 0>
965 static void init_holder_helper(instance_type *inst, const holder_type *holder_ptr, const void * /* dummy */) {
966 if (holder_ptr)
967 new (&inst->holder) holder_type(*holder_ptr);
968 else
969 new (&inst->holder) holder_type(inst->value);
970 }
971
972 /// Initialize holder object, variant 3: holder is not copy constructible (e.g. unique_ptr), always initialize from raw pointer
973 template <typename T = holder_type,
974 typename std::enable_if<!std::is_copy_constructible<T>::value, int>::type = 0>
975 static void init_holder_helper(instance_type *inst, const holder_type * /* unused */, const void * /* dummy */) {
976 new (&inst->holder) holder_type(inst->value);
977 }
978
979 /// Initialize holder object of an instance, possibly given a pointer to an existing holder
980 static void init_holder(PyObject *inst_, const void *holder_ptr) {
981 auto inst = (instance_type *) inst_;
982 init_holder_helper(inst, (const holder_type *) holder_ptr, inst->value);
Wenzel Jakob6e213c92015-11-24 23:05:58 +0100983 inst->constructed = true;
984 }
985
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200986 static void dealloc(PyObject *inst_) {
987 instance_type *inst = (instance_type *) inst_;
988 if (inst->owned) {
989 if (inst->constructed)
990 inst->holder.~holder_type();
991 else
992 ::operator delete(inst->value);
993 }
Wenzel Jakobd561cb02016-01-17 22:36:41 +0100994 generic_type::dealloc((detail::instance<void> *) inst);
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200995 }
Wenzel Jakob84ec78f2016-03-21 17:54:24 +0100996
997 static detail::function_record *get_function_record(handle h) {
998 h = detail::get_function(h);
999 return h ? (detail::function_record *) capsule(
1000 PyCFunction_GetSelf(h.ptr()), true) : nullptr;
1001 }
Wenzel Jakob38bd7112015-07-05 20:05:44 +02001002};
1003
1004/// Binds C++ enumerations and enumeration classes to Python
1005template <typename Type> class enum_ : public class_<Type> {
1006public:
Pim Schellarte5b42ef2016-08-02 10:58:32 -04001007 using UnderlyingType = typename std::underlying_type<Type>::type;
Wenzel Jakob48548ea2016-01-17 22:36:44 +01001008 template <typename... Extra>
1009 enum_(const handle &scope, const char *name, const Extra&... extra)
1010 : class_<Type>(scope, name, extra...), m_parent(scope) {
Pim Schellarte5b42ef2016-08-02 10:58:32 -04001011 auto entries = new std::unordered_map<UnderlyingType, const char *>();
Wenzel Jakob8456a4d2015-10-13 02:42:20 +02001012 this->def("__repr__", [name, entries](Type value) -> std::string {
Pim Schellarte5b42ef2016-08-02 10:58:32 -04001013 auto it = entries->find((UnderlyingType) value);
Wenzel Jakob38bd7112015-07-05 20:05:44 +02001014 return std::string(name) + "." +
1015 ((it == entries->end()) ? std::string("???")
1016 : std::string(it->second));
1017 });
Pim Schellarte5b42ef2016-08-02 10:58:32 -04001018 this->def("__init__", [](Type& value, UnderlyingType i) { value = (Type)i; });
1019 this->def("__init__", [](Type& value, UnderlyingType i) { new (&value) Type((Type) i); });
1020 this->def("__int__", [](Type value) { return (UnderlyingType) value; });
Wenzel Jakob6fb48492016-05-01 12:45:38 +02001021 this->def("__eq__", [](const Type &value, Type *value2) { return value2 && value == *value2; });
Pim Schellarte5b42ef2016-08-02 10:58:32 -04001022 this->def("__eq__", [](const Type &value, UnderlyingType value2) { return value2 && value == value2; });
Wenzel Jakob6fb48492016-05-01 12:45:38 +02001023 this->def("__ne__", [](const Type &value, Type *value2) { return !value2 || value != *value2; });
Pim Schellarte5b42ef2016-08-02 10:58:32 -04001024 this->def("__ne__", [](const Type &value, UnderlyingType value2) { return value != value2; });
1025 this->def("__hash__", [](const Type &value) { return (UnderlyingType) value; });
Wenzel Jakob38bd7112015-07-05 20:05:44 +02001026 m_entries = entries;
1027 }
1028
1029 /// Export enumeration entries into the parent scope
1030 void export_values() {
1031 PyObject *dict = ((PyTypeObject *) this->m_ptr)->tp_dict;
1032 PyObject *key, *value;
Wenzel Jakob27e8e102016-01-17 22:36:37 +01001033 ssize_t pos = 0;
Wenzel Jakob38bd7112015-07-05 20:05:44 +02001034 while (PyDict_Next(dict, &pos, &key, &value))
1035 if (PyObject_IsInstance(value, this->m_ptr))
1036 m_parent.attr(key) = value;
1037 }
1038
1039 /// Add an enumeration entry
1040 enum_& value(char const* name, Type value) {
Wenzel Jakob8f4eb002015-10-15 18:13:33 +02001041 this->attr(name) = pybind11::cast(value, return_value_policy::copy);
Pim Schellarte5b42ef2016-08-02 10:58:32 -04001042 (*m_entries)[(UnderlyingType) value] = name;
Wenzel Jakob38bd7112015-07-05 20:05:44 +02001043 return *this;
1044 }
1045private:
Pim Schellarte5b42ef2016-08-02 10:58:32 -04001046 std::unordered_map<UnderlyingType, const char *> *m_entries;
Wenzel Jakob48548ea2016-01-17 22:36:44 +01001047 handle m_parent;
Wenzel Jakob38bd7112015-07-05 20:05:44 +02001048};
1049
1050NAMESPACE_BEGIN(detail)
Wenzel Jakob71867832015-07-29 17:43:52 +02001051template <typename... Args> struct init {
Wenzel Jakob86d825f2016-05-26 13:19:27 +02001052 template <typename Base, typename Holder, typename Alias, typename... Extra,
1053 typename std::enable_if<std::is_same<Base, Alias>::value, int>::type = 0>
1054 void execute(pybind11::class_<Base, Holder, Alias> &class_, const Extra&... extra) const {
Wenzel Jakob38bd7112015-07-05 20:05:44 +02001055 /// Function which calls a specific C++ in-place constructor
Wenzel Jakob69e1a5c2016-05-26 14:29:31 +02001056 class_.def("__init__", [](Base *self_, Args... args) { new (self_) Base(args...); }, extra...);
Wenzel Jakob86d825f2016-05-26 13:19:27 +02001057 }
1058
1059 template <typename Base, typename Holder, typename Alias, typename... Extra,
1060 typename std::enable_if<!std::is_same<Base, Alias>::value &&
1061 std::is_constructible<Base, Args...>::value, int>::type = 0>
1062 void execute(pybind11::class_<Base, Holder, Alias> &class_, const Extra&... extra) const {
1063 handle cl_type = class_;
Wenzel Jakob69e1a5c2016-05-26 14:29:31 +02001064 class_.def("__init__", [cl_type](handle self_, Args... args) {
1065 if (self_.get_type() == cl_type)
1066 new (self_.cast<Base *>()) Base(args...);
Wenzel Jakob86d825f2016-05-26 13:19:27 +02001067 else
Wenzel Jakob69e1a5c2016-05-26 14:29:31 +02001068 new (self_.cast<Alias *>()) Alias(args...);
Wenzel Jakob86d825f2016-05-26 13:19:27 +02001069 }, extra...);
1070 }
1071
1072 template <typename Base, typename Holder, typename Alias, typename... Extra,
1073 typename std::enable_if<!std::is_same<Base, Alias>::value &&
1074 !std::is_constructible<Base, Args...>::value, int>::type = 0>
1075 void execute(pybind11::class_<Base, Holder, Alias> &class_, const Extra&... extra) const {
Wenzel Jakob2353b9b2016-06-27 16:05:46 +02001076 class_.def("__init__", [](Alias *self_, Args... args) { new (self_) Alias(args...); }, extra...);
Wenzel Jakob38bd7112015-07-05 20:05:44 +02001077 }
1078};
Wenzel Jakob5f218b32016-01-17 22:36:39 +01001079
Wenzel Jakob48548ea2016-01-17 22:36:44 +01001080PYBIND11_NOINLINE inline void keep_alive_impl(int Nurse, int Patient, handle args, handle ret) {
Wenzel Jakob5f218b32016-01-17 22:36:39 +01001081 /* Clever approach based on weak references taken from Boost.Python */
Wenzel Jakob48548ea2016-01-17 22:36:44 +01001082 handle nurse (Nurse > 0 ? PyTuple_GetItem(args.ptr(), Nurse - 1) : ret.ptr());
1083 handle patient(Patient > 0 ? PyTuple_GetItem(args.ptr(), Patient - 1) : ret.ptr());
Wenzel Jakob5f218b32016-01-17 22:36:39 +01001084
Wenzel Jakobb2c2c792016-01-17 22:36:40 +01001085 if (!nurse || !patient)
Wenzel Jakob678d7872016-01-17 22:36:41 +01001086 pybind11_fail("Could not activate keep_alive!");
Wenzel Jakob5f218b32016-01-17 22:36:39 +01001087
Wenzel Jakob9b880ba2016-04-25 03:25:13 +02001088 if (patient.ptr() == Py_None)
1089 return; /* Nothing to keep alive */
1090
Wenzel Jakob5f218b32016-01-17 22:36:39 +01001091 cpp_function disable_lifesupport(
Wenzel Jakobb2c2c792016-01-17 22:36:40 +01001092 [patient](handle weakref) { patient.dec_ref(); weakref.dec_ref(); });
Wenzel Jakob5f218b32016-01-17 22:36:39 +01001093
Wenzel Jakobb2c2c792016-01-17 22:36:40 +01001094 weakref wr(nurse, disable_lifesupport);
Wenzel Jakob5f218b32016-01-17 22:36:39 +01001095
Wenzel Jakobb2c2c792016-01-17 22:36:40 +01001096 patient.inc_ref(); /* reference patient and leak the weak reference */
1097 (void) wr.release();
Wenzel Jakob5f218b32016-01-17 22:36:39 +01001098}
1099
Ivan Smirnovdaed1ab2016-06-17 22:29:39 +01001100template <typename Iterator> struct iterator_state {
1101 Iterator it, end;
1102 bool first;
1103};
Wenzel Jakobb2825952016-04-13 23:33:00 +02001104
Wenzel Jakob38bd7112015-07-05 20:05:44 +02001105NAMESPACE_END(detail)
1106
Ben Pritchard2de6e1d2016-02-18 13:20:15 -05001107template <typename... Args> detail::init<Args...> init() { return detail::init<Args...>(); }
Wenzel Jakob38bd7112015-07-05 20:05:44 +02001108
Wenzel Jakob5dd33d82016-05-30 11:28:21 +02001109template <typename Iterator,
1110 typename ValueType = decltype(*std::declval<Iterator>()),
1111 typename... Extra>
1112iterator make_iterator(Iterator first, Iterator last, Extra &&... extra) {
Wenzel Jakobb2825952016-04-13 23:33:00 +02001113 typedef detail::iterator_state<Iterator> state;
1114
1115 if (!detail::get_type_info(typeid(state))) {
1116 class_<state>(handle(), "")
1117 .def("__iter__", [](state &s) -> state& { return s; })
Wenzel Jakob5dd33d82016-05-30 11:28:21 +02001118 .def("__next__", [](state &s) -> ValueType {
Ivan Smirnovdaed1ab2016-06-17 22:29:39 +01001119 if (!s.first)
1120 ++s.it;
1121 else
1122 s.first = false;
Wenzel Jakobb2825952016-04-13 23:33:00 +02001123 if (s.it == s.end)
1124 throw stop_iteration();
Ivan Smirnovdaed1ab2016-06-17 22:29:39 +01001125 return *s.it;
Wenzel Jakobb2825952016-04-13 23:33:00 +02001126 }, return_value_policy::reference_internal, std::forward<Extra>(extra)...);
1127 }
1128
Ivan Smirnovdaed1ab2016-06-17 22:29:39 +01001129 return (iterator) cast(state { first, last, true });
Wenzel Jakobb2825952016-04-13 23:33:00 +02001130}
1131
Wenzel Jakobbf0c7dc2016-04-18 10:52:12 +02001132template <typename Type, typename... Extra> iterator make_iterator(Type &value, Extra&&... extra) {
1133 return make_iterator(std::begin(value), std::end(value), extra...);
1134}
1135
Wenzel Jakob38bd7112015-07-05 20:05:44 +02001136template <typename InputType, typename OutputType> void implicitly_convertible() {
Wenzel Jakob2ac50442016-01-17 22:36:35 +01001137 auto implicit_caster = [](PyObject *obj, PyTypeObject *type) -> PyObject * {
Wenzel Jakob38bd7112015-07-05 20:05:44 +02001138 if (!detail::type_caster<InputType>().load(obj, false))
1139 return nullptr;
1140 tuple args(1);
1141 args[0] = obj;
1142 PyObject *result = PyObject_Call((PyObject *) type, args.ptr(), nullptr);
1143 if (result == nullptr)
1144 PyErr_Clear();
1145 return result;
1146 };
Wenzel Jakobb6cf75d2016-01-29 11:39:32 +01001147 auto &registered_types = detail::get_internals().registered_types_cpp;
1148 auto it = registered_types.find(std::type_index(typeid(OutputType)));
Wenzel Jakob38bd7112015-07-05 20:05:44 +02001149 if (it == registered_types.end())
Wenzel Jakob678d7872016-01-17 22:36:41 +01001150 pybind11_fail("implicitly_convertible: Unable to find type " + type_id<OutputType>());
Wenzel Jakobd561cb02016-01-17 22:36:41 +01001151 ((detail::type_info *) it->second)->implicit_conversions.push_back(implicit_caster);
Wenzel Jakob38bd7112015-07-05 20:05:44 +02001152}
1153
Pim Schellart5a7d17f2016-06-17 17:35:59 -04001154template <typename ExceptionTranslator>
1155void register_exception_translator(ExceptionTranslator&& translator) {
1156 detail::get_internals().registered_exception_translators.push_front(
1157 std::forward<ExceptionTranslator>(translator));
1158}
1159
1160/* Wrapper to generate a new Python exception type.
1161 *
1162 * This should only be used with PyErr_SetString for now.
1163 * It is not (yet) possible to use as a py::base.
1164 * Template type argument is reserved for future use.
1165 */
1166template <typename type>
1167class exception : public object {
1168public:
1169 exception(module &m, const std::string name, PyObject* base=PyExc_Exception) {
1170 std::string full_name = std::string(PyModule_GetName(m.ptr()))
1171 + std::string(".") + name;
1172 char* exception_name = const_cast<char*>(full_name.c_str());
1173 m_ptr = PyErr_NewException(exception_name, base, NULL);
1174 inc_ref(); // PyModule_AddObject() steals a reference
1175 PyModule_AddObject(m.ptr(), name.c_str(), m_ptr);
1176 }
1177};
1178
Felipe Lema2547ca42016-01-25 16:22:44 -03001179#if defined(WITH_THREAD)
Wenzel Jakob39e97e62016-04-25 03:26:15 +02001180
1181/* The functions below essentially reproduce the PyGILState_* API using a RAII
1182 * pattern, but there are a few important differences:
1183 *
1184 * 1. When acquiring the GIL from an non-main thread during the finalization
1185 * phase, the GILState API blindly terminates the calling thread, which
1186 * is often not what is wanted. This API does not do this.
1187 *
1188 * 2. The gil_scoped_release function can optionally cut the relationship
1189 * of a PyThreadState and its associated thread, which allows moving it to
1190 * another thread (this is a fairly rare/advanced use case).
1191 *
1192 * 3. The reference count of an acquired thread state can be controlled. This
1193 * can be handy to prevent cases where callbacks issued from an external
Wenzel Jakobfbafdea2016-04-25 15:02:43 +02001194 * thread would otherwise constantly construct and destroy thread state data
1195 * structures.
Wenzel Jakob69e1a5c2016-05-26 14:29:31 +02001196 *
1197 * See the Python bindings of NanoGUI (http://github.com/wjakob/nanogui) for an
1198 * example which uses features 2 and 3 to migrate the Python thread of
1199 * execution to another thread (to run the event loop on the original thread,
1200 * in this case).
Wenzel Jakobfbafdea2016-04-25 15:02:43 +02001201 */
Wenzel Jakob38bd7112015-07-05 20:05:44 +02001202
1203class gil_scoped_acquire {
Wenzel Jakob38bd7112015-07-05 20:05:44 +02001204public:
Wenzel Jakobfbafdea2016-04-25 15:02:43 +02001205 PYBIND11_NOINLINE gil_scoped_acquire() {
Wenzel Jakob39e97e62016-04-25 03:26:15 +02001206 auto const &internals = detail::get_internals();
1207 tstate = (PyThreadState *) PyThread_get_key_value(internals.tstate);
1208
1209 if (!tstate) {
1210 tstate = PyThreadState_New(internals.istate);
1211 #if !defined(NDEBUG)
1212 if (!tstate)
1213 pybind11_fail("scoped_acquire: could not create thread state!");
1214 #endif
1215 tstate->gilstate_counter = 0;
Wenzel Jakobfbafdea2016-04-25 15:02:43 +02001216 #if PY_MAJOR_VERSION < 3
1217 PyThread_delete_key_value(internals.tstate);
1218 #endif
Wenzel Jakob39e97e62016-04-25 03:26:15 +02001219 PyThread_set_key_value(internals.tstate, tstate);
1220 } else {
Wenzel Jakobfbafdea2016-04-25 15:02:43 +02001221 release = detail::get_thread_state_unchecked() != tstate;
Wenzel Jakob39e97e62016-04-25 03:26:15 +02001222 }
1223
1224 if (release) {
Wenzel Jakob39e97e62016-04-25 03:26:15 +02001225 /* Work around an annoying assertion in PyThreadState_Swap */
Wenzel Jakobfbafdea2016-04-25 15:02:43 +02001226 #if defined(Py_DEBUG)
1227 PyInterpreterState *interp = tstate->interp;
1228 tstate->interp = nullptr;
1229 #endif
Wenzel Jakob39e97e62016-04-25 03:26:15 +02001230 PyEval_AcquireThread(tstate);
Wenzel Jakobfbafdea2016-04-25 15:02:43 +02001231 #if defined(Py_DEBUG)
1232 tstate->interp = interp;
1233 #endif
Wenzel Jakob39e97e62016-04-25 03:26:15 +02001234 }
1235
1236 inc_ref();
1237 }
1238
1239 void inc_ref() {
1240 ++tstate->gilstate_counter;
1241 }
1242
Wenzel Jakobfbafdea2016-04-25 15:02:43 +02001243 PYBIND11_NOINLINE void dec_ref() {
Wenzel Jakob39e97e62016-04-25 03:26:15 +02001244 --tstate->gilstate_counter;
1245 #if !defined(NDEBUG)
Wenzel Jakobfbafdea2016-04-25 15:02:43 +02001246 if (detail::get_thread_state_unchecked() != tstate)
Wenzel Jakob39e97e62016-04-25 03:26:15 +02001247 pybind11_fail("scoped_acquire::dec_ref(): thread state must be current!");
1248 if (tstate->gilstate_counter < 0)
1249 pybind11_fail("scoped_acquire::dec_ref(): reference count underflow!");
1250 #endif
1251 if (tstate->gilstate_counter == 0) {
1252 #if !defined(NDEBUG)
1253 if (!release)
1254 pybind11_fail("scoped_acquire::dec_ref(): internal error!");
1255 #endif
1256 PyThreadState_Clear(tstate);
1257 PyThreadState_DeleteCurrent();
Wenzel Jakobfbafdea2016-04-25 15:02:43 +02001258 PyThread_delete_key_value(detail::get_internals().tstate);
Wenzel Jakob39e97e62016-04-25 03:26:15 +02001259 release = false;
1260 }
1261 }
1262
Wenzel Jakobfbafdea2016-04-25 15:02:43 +02001263 PYBIND11_NOINLINE ~gil_scoped_acquire() {
Wenzel Jakob39e97e62016-04-25 03:26:15 +02001264 dec_ref();
1265 if (release)
1266 PyEval_SaveThread();
1267 }
1268private:
1269 PyThreadState *tstate = nullptr;
1270 bool release = true;
Wenzel Jakob38bd7112015-07-05 20:05:44 +02001271};
1272
1273class gil_scoped_release {
Wenzel Jakob38bd7112015-07-05 20:05:44 +02001274public:
Wenzel Jakob39e97e62016-04-25 03:26:15 +02001275 gil_scoped_release(bool disassoc = false) : disassoc(disassoc) {
1276 tstate = PyEval_SaveThread();
Wenzel Jakob2f6662e2016-04-25 09:16:41 +02001277 if (disassoc) {
Boris Schäling20ee9352016-05-28 12:26:18 +02001278 auto key = detail::get_internals().tstate;
Wenzel Jakob2f6662e2016-04-25 09:16:41 +02001279 #if PY_MAJOR_VERSION < 3
1280 PyThread_delete_key_value(key);
1281 #else
1282 PyThread_set_key_value(key, nullptr);
1283 #endif
1284 }
Wenzel Jakob39e97e62016-04-25 03:26:15 +02001285 }
1286 ~gil_scoped_release() {
1287 if (!tstate)
1288 return;
1289 PyEval_RestoreThread(tstate);
Wenzel Jakob2f6662e2016-04-25 09:16:41 +02001290 if (disassoc) {
Boris Schäling20ee9352016-05-28 12:26:18 +02001291 auto key = detail::get_internals().tstate;
Wenzel Jakob2f6662e2016-04-25 09:16:41 +02001292 #if PY_MAJOR_VERSION < 3
1293 PyThread_delete_key_value(key);
1294 #endif
1295 PyThread_set_key_value(key, tstate);
1296 }
Wenzel Jakob39e97e62016-04-25 03:26:15 +02001297 }
1298private:
1299 PyThreadState *tstate;
1300 bool disassoc;
Wenzel Jakob38bd7112015-07-05 20:05:44 +02001301};
Wenzel Jakob39e97e62016-04-25 03:26:15 +02001302#else
1303class gil_scoped_acquire { };
1304class gil_scoped_release { };
Felipe Lema2547ca42016-01-25 16:22:44 -03001305#endif
Wenzel Jakob38bd7112015-07-05 20:05:44 +02001306
Wenzel Jakoba2f6fde2015-10-01 16:46:03 +02001307inline function get_overload(const void *this_ptr, const char *name) {
1308 handle py_object = detail::get_object_handle(this_ptr);
Wenzel Jakobac0fde92015-10-01 18:37:26 +02001309 if (!py_object)
1310 return function();
Wenzel Jakoba2f6fde2015-10-01 16:46:03 +02001311 handle type = py_object.get_type();
1312 auto key = std::make_pair(type.ptr(), name);
1313
Wenzel Jakobf5c154a2016-04-11 18:13:08 +02001314 /* Cache functions that aren't overloaded in Python to avoid
1315 many costly Python dictionary lookups below */
Wenzel Jakoba2f6fde2015-10-01 16:46:03 +02001316 auto &cache = detail::get_internals().inactive_overload_cache;
1317 if (cache.find(key) != cache.end())
1318 return function();
1319
1320 function overload = (function) py_object.attr(name);
1321 if (overload.is_cpp_function()) {
1322 cache.insert(key);
1323 return function();
1324 }
Wenzel Jakob27e8e102016-01-17 22:36:37 +01001325
Wenzel Jakobf5c154a2016-04-11 18:13:08 +02001326 /* Don't call dispatch code if invoked from overridden function */
Wenzel Jakoba2f6fde2015-10-01 16:46:03 +02001327 PyFrameObject *frame = PyThreadState_Get()->frame;
Wenzel Jakobb2b44a92016-04-15 17:50:40 +02001328 if (frame && (std::string) pybind11::handle(frame->f_code->co_name).str() == name &&
Wenzel Jakobf5c154a2016-04-11 18:13:08 +02001329 frame->f_code->co_argcount > 0) {
1330 PyFrame_FastToLocals(frame);
1331 PyObject *self_caller = PyDict_GetItem(
1332 frame->f_locals, PyTuple_GET_ITEM(frame->f_code->co_varnames, 0));
1333 if (self_caller == py_object.ptr())
1334 return function();
1335 }
Wenzel Jakoba2f6fde2015-10-01 16:46:03 +02001336 return overload;
1337}
1338
Wenzel Jakob1e3be732016-05-24 23:42:05 +02001339#define PYBIND11_OVERLOAD_INT(ret_type, name, ...) { \
Wenzel Jakob8f4eb002015-10-15 18:13:33 +02001340 pybind11::gil_scoped_acquire gil; \
Wenzel Jakob1e3be732016-05-24 23:42:05 +02001341 pybind11::function overload = pybind11::get_overload(this, name); \
Wenzel Jakoba2f6fde2015-10-01 16:46:03 +02001342 if (overload) \
Wenzel Jakob6c03beb2016-05-08 14:34:09 +02001343 return overload(__VA_ARGS__).template cast<ret_type>(); }
Wenzel Jakoba2f6fde2015-10-01 16:46:03 +02001344
Wenzel Jakob1e3be732016-05-24 23:42:05 +02001345#define PYBIND11_OVERLOAD_NAME(ret_type, cname, name, fn, ...) \
1346 PYBIND11_OVERLOAD_INT(ret_type, name, __VA_ARGS__) \
1347 return cname::fn(__VA_ARGS__)
Wenzel Jakoba2f6fde2015-10-01 16:46:03 +02001348
Wenzel Jakob1e3be732016-05-24 23:42:05 +02001349#define PYBIND11_OVERLOAD_PURE_NAME(ret_type, cname, name, fn, ...) \
1350 PYBIND11_OVERLOAD_INT(ret_type, name, __VA_ARGS__) \
1351 pybind11::pybind11_fail("Tried to call pure virtual function \"" #cname "::" name "\"");
1352
1353#define PYBIND11_OVERLOAD(ret_type, cname, fn, ...) \
1354 PYBIND11_OVERLOAD_NAME(ret_type, cname, #fn, fn, __VA_ARGS__)
1355
1356#define PYBIND11_OVERLOAD_PURE(ret_type, cname, fn, ...) \
1357 PYBIND11_OVERLOAD_PURE_NAME(ret_type, cname, #fn, fn, __VA_ARGS__)
Wenzel Jakoba2f6fde2015-10-01 16:46:03 +02001358
Wenzel Jakob8f4eb002015-10-15 18:13:33 +02001359NAMESPACE_END(pybind11)
Wenzel Jakob38bd7112015-07-05 20:05:44 +02001360
1361#if defined(_MSC_VER)
Wenzel Jakob48548ea2016-01-17 22:36:44 +01001362# pragma warning(pop)
Ben Pritchard33f34302016-02-18 15:25:51 -05001363#elif defined(__ICC) || defined(__INTEL_COMPILER)
1364# pragma warning(pop)
Wenzel Jakob6d252962016-05-01 20:47:49 +02001365#elif defined(__GNUG__) && !defined(__clang__)
Wenzel Jakob48548ea2016-01-17 22:36:44 +01001366# pragma GCC diagnostic pop
Wenzel Jakob38bd7112015-07-05 20:05:44 +02001367#endif