blob: 6cc6d5e80b95c59f827258cd5f513c3bac9452bc [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)
Wenzel Jakob1ffce742016-08-25 01:43:33 +020015# pragma warning(disable: 4100) // warning C4100: Unreferenced formal parameter
Wenzel Jakob48548ea2016-01-17 22:36:44 +010016# pragma warning(disable: 4127) // warning C4127: Conditional expression is constant
Wenzel Jakob1ffce742016-08-25 01:43:33 +020017# pragma warning(disable: 4512) // warning C4512: Assignment operator was implicitly defined as deleted
Wenzel Jakob48548ea2016-01-17 22:36:44 +010018# pragma warning(disable: 4800) // warning C4800: 'int': forcing value to bool 'true' or 'false' (performance warning)
19# pragma warning(disable: 4996) // warning C4996: The POSIX name for this item is deprecated. Instead, use the ISO C and C++ conformant name
Wenzel Jakob8e5dceb2016-09-11 20:00:40 +090020# pragma warning(disable: 4702) // warning C4702: unreachable code
Dean Moldovan2bab5792016-09-23 00:27:59 +020021# pragma warning(disable: 4522) // warning C4522: multiple assignment operators specified
Wenzel Jakob1ffce742016-08-25 01:43:33 +020022#elif defined(__INTEL_COMPILER)
Ben Pritchard33f34302016-02-18 15:25:51 -050023# pragma warning(push)
Wenzel Jakob1ffce742016-08-25 01:43:33 +020024# pragma warning(disable: 186) // pointless comparison of unsigned integer with zero
25# pragma warning(disable: 1334) // the "template" keyword used for syntactic disambiguation may only be used within a template
26# pragma warning(disable: 2196) // warning #2196: routine is both "inline" and "noinline"
Wenzel Jakob6d252962016-05-01 20:47:49 +020027#elif defined(__GNUG__) && !defined(__clang__)
Wenzel Jakob48548ea2016-01-17 22:36:44 +010028# pragma GCC diagnostic push
29# pragma GCC diagnostic ignored "-Wunused-but-set-parameter"
30# pragma GCC diagnostic ignored "-Wunused-but-set-variable"
31# pragma GCC diagnostic ignored "-Wmissing-field-initializers"
Wenzel Jakobdeeab552016-05-15 23:54:34 +020032# pragma GCC diagnostic ignored "-Wstrict-aliasing"
33# pragma GCC diagnostic ignored "-Wattributes"
Wenzel Jakob38bd7112015-07-05 20:05:44 +020034#endif
35
Wenzel Jakob48548ea2016-01-17 22:36:44 +010036#include "attr.h"
Alexander Stukowski9a110e62016-11-15 12:38:05 +010037#include "options.h"
Dean Moldovanc91f8bd2017-02-13 18:11:24 +010038#include "class_support.h"
Wenzel Jakob38bd7112015-07-05 20:05:44 +020039
Wenzel Jakob8f4eb002015-10-15 18:13:33 +020040NAMESPACE_BEGIN(pybind11)
Wenzel Jakob38bd7112015-07-05 20:05:44 +020041
Wenzel Jakobd4258ba2015-07-26 16:33:49 +020042/// Wraps an arbitrary C++ function/method/lambda function/.. into a callable Python object
Wenzel Jakobbd4a5292015-07-11 17:41:48 +020043class cpp_function : public function {
Wenzel Jakobd4258ba2015-07-26 16:33:49 +020044public:
Wenzel Jakobbd4a5292015-07-11 17:41:48 +020045 cpp_function() { }
Wenzel Jakob38bd7112015-07-05 20:05:44 +020046
Wenzel Jakob5984baa2016-05-10 15:05:03 +010047 /// Construct a cpp_function from a vanilla function pointer
Jason Rhinelander1d7998e2017-02-17 06:56:41 -050048 template <typename Return, typename... Args, typename... Extra>
49 cpp_function(Return (*f)(Args...), const Extra&... extra) {
Wenzel Jakob5984baa2016-05-10 15:05:03 +010050 initialize(f, f, extra...);
Wenzel Jakob38bd7112015-07-05 20:05:44 +020051 }
Wenzel Jakobd4258ba2015-07-26 16:33:49 +020052
Wenzel Jakob5984baa2016-05-10 15:05:03 +010053 /// Construct a cpp_function from a lambda function (possibly with internal state)
Jason Rhinelander1d7998e2017-02-17 06:56:41 -050054 template <typename Func, typename... Extra,
55 typename FuncType = typename detail::remove_class<decltype(&std::remove_reference<Func>::type::operator())>::type>
56 cpp_function(Func &&f, const Extra&... extra) {
Wenzel Jakob71867832015-07-29 17:43:52 +020057 initialize(std::forward<Func>(f),
Jason Rhinelander1d7998e2017-02-17 06:56:41 -050058 (FuncType *) 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 (non-const)
Jason Rhinelander1d7998e2017-02-17 06:56:41 -050062 template <typename Return, typename Class, typename... Arg, typename... Extra>
63 cpp_function(Return (Class::*f)(Arg...), const Extra&... extra) {
Wenzel Jakob71867832015-07-29 17:43:52 +020064 initialize([f](Class *c, Arg... args) -> Return { return (c->*f)(args...); },
Jason Rhinelander1d7998e2017-02-17 06:56:41 -050065 (Return (*) (Class *, Arg...)) nullptr, extra...);
Wenzel Jakobd4258ba2015-07-26 16:33:49 +020066 }
67
Wenzel Jakob5984baa2016-05-10 15:05:03 +010068 /// Construct a cpp_function from a class method (const)
Jason Rhinelander1d7998e2017-02-17 06:56:41 -050069 template <typename Return, typename Class, typename... Arg, typename... Extra>
70 cpp_function(Return (Class::*f)(Arg...) const, const Extra&... extra) {
Wenzel Jakob71867832015-07-29 17:43:52 +020071 initialize([f](const Class *c, Arg... args) -> Return { return (c->*f)(args...); },
Jason Rhinelander1d7998e2017-02-17 06:56:41 -050072 (Return (*)(const Class *, Arg ...)) nullptr, extra...);
Wenzel Jakobd4258ba2015-07-26 16:33:49 +020073 }
74
Wenzel Jakob57082212015-09-04 23:42:12 +020075 /// Return the function name
Wenzel Jakob48548ea2016-01-17 22:36:44 +010076 object name() const { return attr("__name__"); }
Wenzel Jakob57082212015-09-04 23:42:12 +020077
Wenzel Jakobd561cb02016-01-17 22:36:41 +010078protected:
Wenzel Jakob382484a2016-09-10 15:28:37 +090079 /// Space optimization: don't inline this frequently instantiated fragment
80 PYBIND11_NOINLINE detail::function_record *make_function_record() {
81 return new detail::function_record();
82 }
83
Wenzel Jakobd561cb02016-01-17 22:36:41 +010084 /// Special internal constructor for functors, lambda functions, etc.
Jason Rhinelander1d7998e2017-02-17 06:56:41 -050085 template <typename Func, typename Return, typename... Args, typename... Extra>
86 void initialize(Func &&f, Return (*)(Args...), const Extra&... extra) {
Dean Moldovan9e625582016-06-04 00:27:32 +020087
Wenzel Jakob66c9a402016-01-17 22:36:36 +010088 struct capture { typename std::remove_reference<Func>::type f; };
Wenzel Jakobd4258ba2015-07-26 16:33:49 +020089
Wenzel Jakobd561cb02016-01-17 22:36:41 +010090 /* Store the function including any extra state it might have (e.g. a lambda capture object) */
Wenzel Jakob382484a2016-09-10 15:28:37 +090091 auto rec = make_function_record();
Wenzel Jakob71867832015-07-29 17:43:52 +020092
Wenzel Jakob5984baa2016-05-10 15:05:03 +010093 /* Store the capture object directly in the function record if there is enough space */
94 if (sizeof(capture) <= sizeof(rec->data)) {
Wenzel Jakob954b7932016-07-10 10:13:18 +020095 /* Without these pragmas, GCC warns that there might not be
96 enough space to use the placement new operator. However, the
97 'if' statement above ensures that this is the case. */
Jason Rhinelander0b12f912016-07-07 16:26:04 -040098#if defined(__GNUG__) && !defined(__clang__) && __GNUC__ >= 6
Jason Rhinelandercae0e002016-07-07 16:11:42 -040099# pragma GCC diagnostic push
100# pragma GCC diagnostic ignored "-Wplacement-new"
101#endif
Wenzel Jakob5984baa2016-05-10 15:05:03 +0100102 new ((capture *) &rec->data) capture { std::forward<Func>(f) };
Jason Rhinelander0b12f912016-07-07 16:26:04 -0400103#if defined(__GNUG__) && !defined(__clang__) && __GNUC__ >= 6
Jason Rhinelandercae0e002016-07-07 16:11:42 -0400104# pragma GCC diagnostic pop
105#endif
Wenzel Jakob5984baa2016-05-10 15:05:03 +0100106 if (!std::is_trivially_destructible<Func>::value)
107 rec->free_data = [](detail::function_record *r) { ((capture *) &r->data)->~capture(); };
108 } else {
109 rec->data[0] = new capture { std::forward<Func>(f) };
110 rec->free_data = [](detail::function_record *r) { delete ((capture *) r->data[0]); };
111 }
Wenzel Jakob19208fe2015-10-13 17:37:25 +0200112
Wenzel Jakob5984baa2016-05-10 15:05:03 +0100113 /* Type casters for the function arguments and return value */
Dean Moldovan719c1732016-11-27 18:19:34 +0100114 using cast_in = detail::argument_loader<Args...>;
115 using cast_out = detail::make_caster<
116 detail::conditional_t<std::is_void<Return>::value, detail::void_type, Return>
117 >;
Wenzel Jakob71867832015-07-29 17:43:52 +0200118
Jason Rhinelander2686da82017-01-21 23:42:14 -0500119 static_assert(detail::expected_num_args<Extra...>(sizeof...(Args), cast_in::has_args, cast_in::has_kwargs),
120 "The number of named arguments does not match the function signature");
121
Wenzel Jakobd561cb02016-01-17 22:36:41 +0100122 /* Dispatch code which converts function arguments and performs the actual function call */
Jason Rhinelanderbfcf9522017-01-30 13:34:38 -0500123 rec->impl = [](detail::function_call &call) -> handle {
Wenzel Jakob5984baa2016-05-10 15:05:03 +0100124 cast_in args_converter;
Wenzel Jakobd561cb02016-01-17 22:36:41 +0100125
126 /* Try to cast the function arguments into the C++ domain */
Jason Rhinelanderabc29ca2017-01-23 03:50:00 -0500127 if (!args_converter.load_args(call))
Wenzel Jakobd561cb02016-01-17 22:36:41 +0100128 return PYBIND11_TRY_NEXT_OVERLOAD;
129
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100130 /* Invoke call policy pre-call hook */
Jason Rhinelanderbfcf9522017-01-30 13:34:38 -0500131 detail::process_attributes<Extra...>::precall(call);
Wenzel Jakobd561cb02016-01-17 22:36:41 +0100132
Wenzel Jakob5984baa2016-05-10 15:05:03 +0100133 /* Get a pointer to the capture object */
Matthew Woehlkee15fa9f2017-02-08 17:43:08 -0500134 auto data = (sizeof(capture) <= sizeof(call.func.data)
135 ? &call.func.data : call.func.data[0]);
136 capture *cap = const_cast<capture *>(reinterpret_cast<const capture *>(data));
Wenzel Jakob5984baa2016-05-10 15:05:03 +0100137
Dean Moldovand079f412016-11-20 05:31:02 +0100138 /* Override policy for rvalues -- always move */
139 constexpr auto is_rvalue = !std::is_pointer<Return>::value
140 && !std::is_lvalue_reference<Return>::value;
Jason Rhinelanderbfcf9522017-01-30 13:34:38 -0500141 const auto policy = is_rvalue ? return_value_policy::move : call.func.policy;
Dean Moldovand079f412016-11-20 05:31:02 +0100142
Wenzel Jakob954b7932016-07-10 10:13:18 +0200143 /* Perform the function call */
Wenzel Jakob5984baa2016-05-10 15:05:03 +0100144 handle result = cast_out::cast(args_converter.template call<Return>(cap->f),
Jason Rhinelanderbfcf9522017-01-30 13:34:38 -0500145 policy, call.parent);
Wenzel Jakobd561cb02016-01-17 22:36:41 +0100146
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100147 /* Invoke call policy post-call hook */
Jason Rhinelanderbfcf9522017-01-30 13:34:38 -0500148 detail::process_attributes<Extra...>::postcall(call, result);
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100149
Wenzel Jakob5f218b32016-01-17 22:36:39 +0100150 return result;
Wenzel Jakob71867832015-07-29 17:43:52 +0200151 };
152
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100153 /* Process any user-provided function attributes */
154 detail::process_attributes<Extra...>::init(extra..., rec);
Wenzel Jakobd561cb02016-01-17 22:36:41 +0100155
156 /* Generate a readable signature describing the function's arguments and return value types */
Dean Moldovaned23dda2016-08-04 01:40:40 +0200157 using detail::descr; using detail::_;
Dean Moldovan719c1732016-11-27 18:19:34 +0100158 PYBIND11_DESCR signature = _("(") + cast_in::arg_names() + _(") -> ") + cast_out::name();
Wenzel Jakobd561cb02016-01-17 22:36:41 +0100159
160 /* Register the function with Python from generic (non-templated) code */
Wenzel Jakob5984baa2016-05-10 15:05:03 +0100161 initialize_generic(rec, signature.text(), signature.types(), sizeof...(Args));
Wenzel Jakob178c8a82016-05-10 15:59:01 +0100162
163 if (cast_in::has_args) rec->has_args = true;
164 if (cast_in::has_kwargs) rec->has_kwargs = true;
Wenzel Jakob954b7932016-07-10 10:13:18 +0200165
166 /* Stash some additional information used by an important optimization in 'functional.h' */
Jason Rhinelander1d7998e2017-02-17 06:56:41 -0500167 using FunctionType = Return (*)(Args...);
Wenzel Jakob954b7932016-07-10 10:13:18 +0200168 constexpr bool is_function_ptr =
169 std::is_convertible<Func, FunctionType>::value &&
170 sizeof(capture) == sizeof(void *);
171 if (is_function_ptr) {
172 rec->is_stateless = true;
Matthew Woehlkee15fa9f2017-02-08 17:43:08 -0500173 rec->data[1] = const_cast<void *>(reinterpret_cast<const void *>(&typeid(FunctionType)));
Wenzel Jakob954b7932016-07-10 10:13:18 +0200174 }
Wenzel Jakob281aa0e2015-07-30 15:29:00 +0200175 }
176
Wenzel Jakobd561cb02016-01-17 22:36:41 +0100177 /// Register a function call with Python (generic non-templated code goes here)
Wenzel Jakob5984baa2016-05-10 15:05:03 +0100178 void initialize_generic(detail::function_record *rec, const char *text,
Dean Moldovanecced6c2016-07-31 20:03:18 +0200179 const std::type_info *const *types, size_t args) {
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100180
Wenzel Jakob66c9a402016-01-17 22:36:36 +0100181 /* Create copies of all referenced C-style strings */
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100182 rec->name = strdup(rec->name ? rec->name : "");
183 if (rec->doc) rec->doc = strdup(rec->doc);
184 for (auto &a: rec->args) {
Wenzel Jakob66c9a402016-01-17 22:36:36 +0100185 if (a.name)
186 a.name = strdup(a.name);
187 if (a.descr)
188 a.descr = strdup(a.descr);
189 else if (a.value)
Dean Moldovan242b1462016-09-08 17:02:04 +0200190 a.descr = strdup(a.value.attr("__repr__")().cast<std::string>().c_str());
Wenzel Jakob66c9a402016-01-17 22:36:36 +0100191 }
Wenzel Jakob954b7932016-07-10 10:13:18 +0200192
Wenzel Jakob66c9a402016-01-17 22:36:36 +0100193 /* Generate a proper function signature */
194 std::string signature;
195 size_t type_depth = 0, char_index = 0, type_index = 0, arg_index = 0;
196 while (true) {
197 char c = text[char_index++];
198 if (c == '\0')
199 break;
200
201 if (c == '{') {
Dean Moldovanecced6c2016-07-31 20:03:18 +0200202 // Write arg name for everything except *args, **kwargs and return type.
Dean Moldovaned23dda2016-08-04 01:40:40 +0200203 if (type_depth == 0 && text[char_index] != '*' && arg_index < args) {
Jason Rhinelanderabc29ca2017-01-23 03:50:00 -0500204 if (!rec->args.empty() && rec->args[arg_index].name) {
Dean Moldovanecced6c2016-07-31 20:03:18 +0200205 signature += rec->args[arg_index].name;
Wenzel Jakob31fbf182016-11-20 05:27:05 +0100206 } else if (arg_index == 0 && rec->is_method) {
Dean Moldovanecced6c2016-07-31 20:03:18 +0200207 signature += "self";
208 } else {
Wenzel Jakob31fbf182016-11-20 05:27:05 +0100209 signature += "arg" + std::to_string(arg_index - (rec->is_method ? 1 : 0));
Dean Moldovanecced6c2016-07-31 20:03:18 +0200210 }
211 signature += ": ";
Wenzel Jakob66c9a402016-01-17 22:36:36 +0100212 }
213 ++type_depth;
214 } else if (c == '}') {
215 --type_depth;
Dean Moldovaned23dda2016-08-04 01:40:40 +0200216 if (type_depth == 0) {
Dean Moldovanecced6c2016-07-31 20:03:18 +0200217 if (arg_index < rec->args.size() && rec->args[arg_index].descr) {
218 signature += "=";
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100219 signature += rec->args[arg_index].descr;
Wenzel Jakob66c9a402016-01-17 22:36:36 +0100220 }
221 arg_index++;
222 }
223 } else if (c == '%') {
224 const std::type_info *t = types[type_index++];
Wenzel Jakobb2c2c792016-01-17 22:36:40 +0100225 if (!t)
Wenzel Jakob678d7872016-01-17 22:36:41 +0100226 pybind11_fail("Internal error while parsing type signature (1)");
Ivan Smirnov8f3e0452016-10-23 15:43:03 +0100227 if (auto tinfo = detail::get_type_info(*t)) {
Wenzel Jakob1d1f81b2016-12-16 15:00:46 +0100228#if defined(PYPY_VERSION)
229 signature += handle((PyObject *) tinfo->type)
230 .attr("__module__")
231 .cast<std::string>() + ".";
232#endif
Ivan Smirnov8f3e0452016-10-23 15:43:03 +0100233 signature += tinfo->type->tp_name;
Wenzel Jakob66c9a402016-01-17 22:36:36 +0100234 } else {
235 std::string tname(t->name());
236 detail::clean_type_id(tname);
237 signature += tname;
238 }
239 } else {
240 signature += c;
241 }
242 }
Wenzel Jakob95d18692016-01-17 22:36:40 +0100243 if (type_depth != 0 || types[type_index] != nullptr)
Wenzel Jakob678d7872016-01-17 22:36:41 +0100244 pybind11_fail("Internal error while parsing type signature (2)");
Wenzel Jakob66c9a402016-01-17 22:36:36 +0100245
246 #if !defined(PYBIND11_CPP14)
247 delete[] types;
248 delete[] text;
249 #endif
Wenzel Jakobf5fae922015-08-24 15:31:24 +0200250
Wenzel Jakob57082212015-09-04 23:42:12 +0200251#if PY_MAJOR_VERSION < 3
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100252 if (strcmp(rec->name, "__next__") == 0) {
253 std::free(rec->name);
254 rec->name = strdup("next");
Wenzel Jakobd1bfc4e2016-05-16 18:52:46 +0200255 } else if (strcmp(rec->name, "__bool__") == 0) {
256 std::free(rec->name);
257 rec->name = strdup("__nonzero__");
Wenzel Jakob66c9a402016-01-17 22:36:36 +0100258 }
Wenzel Jakob57082212015-09-04 23:42:12 +0200259#endif
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100260 rec->signature = strdup(signature.c_str());
261 rec->args.shrink_to_fit();
Wenzel Jakoba380ed92016-05-15 23:54:13 +0200262 rec->is_constructor = !strcmp(rec->name, "__init__") || !strcmp(rec->name, "__setstate__");
Jason Rhinelanderabc29ca2017-01-23 03:50:00 -0500263 rec->nargs = (std::uint16_t) args;
Wenzel Jakobbd4a5292015-07-11 17:41:48 +0200264
Wenzel Jakob57082212015-09-04 23:42:12 +0200265#if PY_MAJOR_VERSION < 3
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100266 if (rec->sibling && PyMethod_Check(rec->sibling.ptr()))
267 rec->sibling = PyMethod_GET_FUNCTION(rec->sibling.ptr());
Wenzel Jakob57082212015-09-04 23:42:12 +0200268#endif
269
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100270 detail::function_record *chain = nullptr, *chain_start = rec;
Jason Rhinelander6873c202016-10-24 21:58:22 -0400271 if (rec->sibling) {
272 if (PyCFunction_Check(rec->sibling.ptr())) {
Wenzel Jakob1d1f81b2016-12-16 15:00:46 +0100273 auto rec_capsule = reinterpret_borrow<capsule>(PyCFunction_GET_SELF(rec->sibling.ptr()));
Jason Rhinelander6873c202016-10-24 21:58:22 -0400274 chain = (detail::function_record *) rec_capsule;
275 /* Never append a method to an overload chain of a parent class;
276 instead, hide the parent's overloads in this case */
Wenzel Jakob7c2461e2016-11-20 05:26:02 +0100277 if (chain->scope != rec->scope)
Jason Rhinelander6873c202016-10-24 21:58:22 -0400278 chain = nullptr;
279 }
280 // Don't trigger for things like the default __init__, which are wrapper_descriptors that we are intentionally replacing
281 else if (!rec->sibling.is_none() && rec->name[0] != '_')
282 pybind11_fail("Cannot overload existing non-function object \"" + std::string(rec->name) +
283 "\" with a function of the same name");
Wenzel Jakoba2f6fde2015-10-01 16:46:03 +0200284 }
285
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100286 if (!chain) {
Wenzel Jakobd561cb02016-01-17 22:36:41 +0100287 /* No existing overload was found, create a new function object */
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100288 rec->def = new PyMethodDef();
289 memset(rec->def, 0, sizeof(PyMethodDef));
290 rec->def->ml_name = rec->name;
291 rec->def->ml_meth = reinterpret_cast<PyCFunction>(*dispatcher);
292 rec->def->ml_flags = METH_VARARGS | METH_KEYWORDS;
Wenzel Jakoba6501792016-02-04 23:02:07 +0100293
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100294 capsule rec_capsule(rec, [](PyObject *o) {
295 destruct((detail::function_record *) PyCapsule_GetPointer(o, nullptr));
Wenzel Jakobd561cb02016-01-17 22:36:41 +0100296 });
Wenzel Jakoba6501792016-02-04 23:02:07 +0100297
298 object scope_module;
299 if (rec->scope) {
Dean Moldovan865e4302016-09-21 01:06:32 +0200300 if (hasattr(rec->scope, "__module__")) {
301 scope_module = rec->scope.attr("__module__");
302 } else if (hasattr(rec->scope, "__name__")) {
303 scope_module = rec->scope.attr("__name__");
304 }
Wenzel Jakoba6501792016-02-04 23:02:07 +0100305 }
306
307 m_ptr = PyCFunction_NewEx(rec->def, rec_capsule.ptr(), scope_module.ptr());
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200308 if (!m_ptr)
Wenzel Jakob678d7872016-01-17 22:36:41 +0100309 pybind11_fail("cpp_function::cpp_function(): Could not allocate function object");
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200310 } else {
Wenzel Jakobd561cb02016-01-17 22:36:41 +0100311 /* Append at the end of the overload chain */
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100312 m_ptr = rec->sibling.ptr();
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200313 inc_ref();
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100314 chain_start = chain;
315 while (chain->next)
316 chain = chain->next;
317 chain->next = rec;
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200318 }
Wenzel Jakob281aa0e2015-07-30 15:29:00 +0200319
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200320 std::string signatures;
Wenzel Jakob71867832015-07-29 17:43:52 +0200321 int index = 0;
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100322 /* Create a nice pydoc rec including all signatures and
Wenzel Jakobd561cb02016-01-17 22:36:41 +0100323 docstrings of the functions in the overload chain */
Alexander Stukowski9a110e62016-11-15 12:38:05 +0100324 if (chain && options::show_function_signatures()) {
Sylvain Corlay0e04fdf2016-03-08 16:42:12 -0500325 // First a generic signature
326 signatures += rec->name;
327 signatures += "(*args, **kwargs)\n";
328 signatures += "Overloaded function.\n\n";
329 }
330 // Then specific overload signatures
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100331 for (auto it = chain_start; it != nullptr; it = it->next) {
Alexander Stukowski9a110e62016-11-15 12:38:05 +0100332 if (options::show_function_signatures()) {
333 if (chain)
334 signatures += std::to_string(++index) + ". ";
335 signatures += rec->name;
336 signatures += it->signature;
Wenzel Jakob66c9a402016-01-17 22:36:36 +0100337 signatures += "\n";
Alexander Stukowski9a110e62016-11-15 12:38:05 +0100338 }
339 if (it->doc && strlen(it->doc) > 0 && options::show_user_defined_docstrings()) {
340 if (options::show_function_signatures()) signatures += "\n";
Wenzel Jakob218b6ce2016-02-28 23:52:37 +0100341 signatures += it->doc;
Alexander Stukowski9a110e62016-11-15 12:38:05 +0100342 if (options::show_function_signatures()) signatures += "\n";
Wenzel Jakob66c9a402016-01-17 22:36:36 +0100343 }
Wenzel Jakob71867832015-07-29 17:43:52 +0200344 if (it->next)
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200345 signatures += "\n";
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200346 }
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100347
348 /* Install docstring */
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200349 PyCFunctionObject *func = (PyCFunctionObject *) m_ptr;
350 if (func->m_ml->ml_doc)
Matthew Woehlkee15fa9f2017-02-08 17:43:08 -0500351 std::free(const_cast<char *>(func->m_ml->ml_doc));
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200352 func->m_ml->ml_doc = strdup(signatures.c_str());
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100353
Wenzel Jakob31fbf182016-11-20 05:27:05 +0100354 if (rec->is_method) {
355 m_ptr = PYBIND11_INSTANCE_METHOD_NEW(m_ptr, rec->scope.ptr());
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200356 if (!m_ptr)
Wenzel Jakob678d7872016-01-17 22:36:41 +0100357 pybind11_fail("cpp_function::cpp_function(): Could not allocate instance method object");
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200358 Py_DECREF(func);
359 }
360 }
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100361
362 /// When a cpp_function is GCed, release any memory allocated by pybind11
363 static void destruct(detail::function_record *rec) {
364 while (rec) {
365 detail::function_record *next = rec->next;
366 if (rec->free_data)
Wenzel Jakob5984baa2016-05-10 15:05:03 +0100367 rec->free_data(rec);
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100368 std::free((char *) rec->name);
369 std::free((char *) rec->doc);
370 std::free((char *) rec->signature);
371 for (auto &arg: rec->args) {
Matthew Woehlkee15fa9f2017-02-08 17:43:08 -0500372 std::free(const_cast<char *>(arg.name));
373 std::free(const_cast<char *>(arg.descr));
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100374 arg.value.dec_ref();
375 }
376 if (rec->def) {
Matthew Woehlkee15fa9f2017-02-08 17:43:08 -0500377 std::free(const_cast<char *>(rec->def->ml_doc));
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100378 delete rec->def;
379 }
380 delete rec;
381 rec = next;
382 }
383 }
384
385 /// Main dispatch logic for calls to functions bound using pybind11
Jason Rhinelander2686da82017-01-21 23:42:14 -0500386 static PyObject *dispatcher(PyObject *self, PyObject *args_in, PyObject *kwargs_in) {
Jason Rhinelanderbfcf9522017-01-30 13:34:38 -0500387 using namespace detail;
388
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100389 /* Iterator over the list of potentially admissible overloads */
Jason Rhinelanderbfcf9522017-01-30 13:34:38 -0500390 function_record *overloads = (function_record *) PyCapsule_GetPointer(self, nullptr),
391 *it = overloads;
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100392
393 /* Need to know how many arguments + keyword arguments there are to pick the right overload */
Jason Rhinelander2686da82017-01-21 23:42:14 -0500394 const size_t n_args_in = (size_t) PyTuple_GET_SIZE(args_in);
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100395
Jason Rhinelander2686da82017-01-21 23:42:14 -0500396 handle parent = n_args_in > 0 ? PyTuple_GET_ITEM(args_in, 0) : nullptr,
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100397 result = PYBIND11_TRY_NEXT_OVERLOAD;
Jason Rhinelanderabc29ca2017-01-23 03:50:00 -0500398
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100399 try {
Jason Rhinelandere5505892017-02-03 18:25:34 -0500400 // We do this in two passes: in the first pass, we load arguments with `convert=false`;
401 // in the second, we allow conversion (except for arguments with an explicit
402 // py::arg().noconvert()). This lets us prefer calls without conversion, with
403 // conversion as a fallback.
404 std::vector<function_call> second_pass;
405
406 // However, if there are no overloads, we can just skip the no-convert pass entirely
407 const bool overloaded = it != nullptr && it->next != nullptr;
408
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100409 for (; it != nullptr; it = it->next) {
Jason Rhinelanderabc29ca2017-01-23 03:50:00 -0500410
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100411 /* For each overload:
Jason Rhinelander2686da82017-01-21 23:42:14 -0500412 1. Copy all positional arguments we were given, also checking to make sure that
413 named positional arguments weren't *also* specified via kwarg.
Wenzel Jakobab60bf12017-01-31 17:23:53 +0100414 2. If we weren't given enough, try to make up the omitted ones by checking
Jason Rhinelander2686da82017-01-21 23:42:14 -0500415 whether they were provided by a kwarg matching the `py::arg("name")` name. If
416 so, use it (and remove it from kwargs; if not, see if the function binding
417 provided a default that we can use.
418 3. Ensure that either all keyword arguments were "consumed", or that the function
419 takes a kwargs argument to accept unconsumed kwargs.
420 4. Any positional arguments still left get put into a tuple (for args), and any
421 leftover kwargs get put into a dict.
422 5. Pack everything into a vector; if we have py::args or py::kwargs, they are an
423 extra tuple or dict at the end of the positional arguments.
424 6. Call the function call dispatcher (function_record::impl)
425
426 If one of these fail, move on to the next overload and keep trying until we get a
427 result other than PYBIND11_TRY_NEXT_OVERLOAD.
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100428 */
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100429
Jason Rhinelanderbfcf9522017-01-30 13:34:38 -0500430 function_record &func = *it;
431 size_t pos_args = func.nargs; // Number of positional arguments that we need
432 if (func.has_args) --pos_args; // (but don't count py::args
433 if (func.has_kwargs) --pos_args; // or py::kwargs)
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100434
Jason Rhinelanderbfcf9522017-01-30 13:34:38 -0500435 if (!func.has_args && n_args_in > pos_args)
Jason Rhinelander2686da82017-01-21 23:42:14 -0500436 continue; // Too many arguments for this overload
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100437
Jason Rhinelanderbfcf9522017-01-30 13:34:38 -0500438 if (n_args_in < pos_args && func.args.size() < pos_args)
Jason Rhinelander2686da82017-01-21 23:42:14 -0500439 continue; // Not enough arguments given, and not enough defaults to fill in the blanks
440
Jason Rhinelanderbfcf9522017-01-30 13:34:38 -0500441 function_call call(func, parent);
Jason Rhinelander2686da82017-01-21 23:42:14 -0500442
443 size_t args_to_copy = std::min(pos_args, n_args_in);
444 size_t args_copied = 0;
445
446 // 1. Copy any position arguments given.
447 for (; args_copied < args_to_copy; ++args_copied) {
448 // If we find a given positional argument that also has a named kwargs argument,
449 // raise a TypeError like Python does. (We could also continue with the next
450 // overload, but this seems highly likely to be a caller mistake rather than a
451 // legitimate overload).
Jason Rhinelanderabc29ca2017-01-23 03:50:00 -0500452 if (kwargs_in && args_copied < func.args.size() && func.args[args_copied].name) {
453 handle value = PyDict_GetItemString(kwargs_in, func.args[args_copied].name);
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100454 if (value)
Jason Rhinelanderabc29ca2017-01-23 03:50:00 -0500455 throw type_error(std::string(func.name) + "(): got multiple values for argument '" +
456 std::string(func.args[args_copied].name) + "'");
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100457 }
Jason Rhinelander2686da82017-01-21 23:42:14 -0500458
Jason Rhinelanderbfcf9522017-01-30 13:34:38 -0500459 call.args.push_back(PyTuple_GET_ITEM(args_in, args_copied));
Jason Rhinelanderabc29ca2017-01-23 03:50:00 -0500460 call.args_convert.push_back(args_copied < func.args.size() ? func.args[args_copied].convert : true);
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100461 }
Wenzel Jakobbd57eb42016-05-01 14:42:20 +0200462
Jason Rhinelander2686da82017-01-21 23:42:14 -0500463 // We'll need to copy this if we steal some kwargs for defaults
464 dict kwargs = reinterpret_borrow<dict>(kwargs_in);
465
466 // 2. Check kwargs and, failing that, defaults that may help complete the list
467 if (args_copied < pos_args) {
468 bool copied_kwargs = false;
469
470 for (; args_copied < pos_args; ++args_copied) {
Jason Rhinelanderabc29ca2017-01-23 03:50:00 -0500471 const auto &arg = func.args[args_copied];
Jason Rhinelander2686da82017-01-21 23:42:14 -0500472
473 handle value;
Jason Rhinelanderabc29ca2017-01-23 03:50:00 -0500474 if (kwargs_in && arg.name)
Jason Rhinelander2686da82017-01-21 23:42:14 -0500475 value = PyDict_GetItemString(kwargs.ptr(), arg.name);
476
477 if (value) {
478 // Consume a kwargs value
479 if (!copied_kwargs) {
480 kwargs = reinterpret_steal<dict>(PyDict_Copy(kwargs.ptr()));
481 copied_kwargs = true;
482 }
483 PyDict_DelItemString(kwargs.ptr(), arg.name);
Wenzel Jakobab60bf12017-01-31 17:23:53 +0100484 } else if (arg.value) {
Jason Rhinelander2686da82017-01-21 23:42:14 -0500485 value = arg.value;
486 }
487
Jason Rhinelanderabc29ca2017-01-23 03:50:00 -0500488 if (value) {
Jason Rhinelanderbfcf9522017-01-30 13:34:38 -0500489 call.args.push_back(value);
Jason Rhinelanderabc29ca2017-01-23 03:50:00 -0500490 call.args_convert.push_back(arg.convert);
491 }
Jason Rhinelander2686da82017-01-21 23:42:14 -0500492 else
493 break;
494 }
495
496 if (args_copied < pos_args)
497 continue; // Not enough arguments, defaults, or kwargs to fill the positional arguments
498 }
499
500 // 3. Check everything was consumed (unless we have a kwargs arg)
Jason Rhinelanderabc29ca2017-01-23 03:50:00 -0500501 if (kwargs && kwargs.size() > 0 && !func.has_kwargs)
Jason Rhinelander2686da82017-01-21 23:42:14 -0500502 continue; // Unconsumed kwargs, but no py::kwargs argument to accept them
503
504 // 4a. If we have a py::args argument, create a new tuple with leftovers
505 tuple extra_args;
Jason Rhinelanderabc29ca2017-01-23 03:50:00 -0500506 if (func.has_args) {
Jason Rhinelander2686da82017-01-21 23:42:14 -0500507 if (args_to_copy == 0) {
508 // We didn't copy out any position arguments from the args_in tuple, so we
509 // can reuse it directly without copying:
510 extra_args = reinterpret_borrow<tuple>(args_in);
Wenzel Jakobab60bf12017-01-31 17:23:53 +0100511 } else if (args_copied >= n_args_in) {
Jason Rhinelander2686da82017-01-21 23:42:14 -0500512 extra_args = tuple(0);
Wenzel Jakobab60bf12017-01-31 17:23:53 +0100513 } else {
Jason Rhinelander2686da82017-01-21 23:42:14 -0500514 size_t args_size = n_args_in - args_copied;
515 extra_args = tuple(args_size);
516 for (size_t i = 0; i < args_size; ++i) {
517 handle item = PyTuple_GET_ITEM(args_in, args_copied + i);
518 extra_args[i] = item.inc_ref().ptr();
519 }
520 }
Jason Rhinelanderbfcf9522017-01-30 13:34:38 -0500521 call.args.push_back(extra_args);
Jason Rhinelanderabc29ca2017-01-23 03:50:00 -0500522 call.args_convert.push_back(false);
Jason Rhinelander2686da82017-01-21 23:42:14 -0500523 }
524
525 // 4b. If we have a py::kwargs, pass on any remaining kwargs
Jason Rhinelanderabc29ca2017-01-23 03:50:00 -0500526 if (func.has_kwargs) {
Jason Rhinelander2686da82017-01-21 23:42:14 -0500527 if (!kwargs.ptr())
528 kwargs = dict(); // If we didn't get one, send an empty one
Jason Rhinelanderbfcf9522017-01-30 13:34:38 -0500529 call.args.push_back(kwargs);
Jason Rhinelanderabc29ca2017-01-23 03:50:00 -0500530 call.args_convert.push_back(false);
Jason Rhinelander2686da82017-01-21 23:42:14 -0500531 }
532
Jason Rhinelanderbfcf9522017-01-30 13:34:38 -0500533 // 5. Put everything in a vector. Not technically step 5, we've been building it
534 // in `call.args` all along.
535 #if !defined(NDEBUG)
Jason Rhinelanderabc29ca2017-01-23 03:50:00 -0500536 if (call.args.size() != func.nargs || call.args_convert.size() != func.nargs)
Jason Rhinelanderbfcf9522017-01-30 13:34:38 -0500537 pybind11_fail("Internal error: function call dispatcher inserted wrong number of arguments!");
538 #endif
Jason Rhinelander2686da82017-01-21 23:42:14 -0500539
Jason Rhinelandere5505892017-02-03 18:25:34 -0500540 std::vector<bool> second_pass_convert;
541 if (overloaded) {
542 // We're in the first no-convert pass, so swap out the conversion flags for a
543 // set of all-false flags. If the call fails, we'll swap the flags back in for
544 // the conversion-allowed call below.
545 second_pass_convert.resize(func.nargs, false);
546 call.args_convert.swap(second_pass_convert);
547 }
548
Jason Rhinelander2686da82017-01-21 23:42:14 -0500549 // 6. Call the function.
Wenzel Jakobbd57eb42016-05-01 14:42:20 +0200550 try {
Jason Rhinelanderabc29ca2017-01-23 03:50:00 -0500551 result = func.impl(call);
Wenzel Jakob00062592016-07-01 16:07:35 +0200552 } catch (reference_cast_error &) {
Wenzel Jakobbd57eb42016-05-01 14:42:20 +0200553 result = PYBIND11_TRY_NEXT_OVERLOAD;
554 }
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100555
556 if (result.ptr() != PYBIND11_TRY_NEXT_OVERLOAD)
557 break;
Jason Rhinelanderabc29ca2017-01-23 03:50:00 -0500558
Jason Rhinelandere5505892017-02-03 18:25:34 -0500559 if (overloaded) {
560 // The (overloaded) call failed; if the call has at least one argument that
561 // permits conversion (i.e. it hasn't been explicitly specified `.noconvert()`)
562 // then add this call to the list of second pass overloads to try.
563 for (size_t i = func.is_method ? 1 : 0; i < pos_args; i++) {
564 if (second_pass_convert[i]) {
565 // Found one: swap the converting flags back in and store the call for
566 // the second pass.
567 call.args_convert.swap(second_pass_convert);
568 second_pass.push_back(std::move(call));
569 break;
570 }
571 }
572 }
573 }
574
575 if (overloaded && !second_pass.empty() && result.ptr() == PYBIND11_TRY_NEXT_OVERLOAD) {
576 // The no-conversion pass finished without success, try again with conversion allowed
577 for (auto &call : second_pass) {
578 try {
579 result = call.func.impl(call);
580 } catch (reference_cast_error &) {
581 result = PYBIND11_TRY_NEXT_OVERLOAD;
582 }
583
584 if (result.ptr() != PYBIND11_TRY_NEXT_OVERLOAD)
585 break;
586 }
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100587 }
Dean Moldovan135ba8d2016-09-10 11:58:02 +0200588 } catch (error_already_set &e) {
589 e.restore();
Pim Schellart5a7d17f2016-06-17 17:35:59 -0400590 return nullptr;
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100591 } catch (...) {
Pim Schellart5a7d17f2016-06-17 17:35:59 -0400592 /* When an exception is caught, give each registered exception
593 translator a chance to translate it to a Python exception
594 in reverse order of registration.
Wenzel Jakob1f66a582016-07-18 10:47:10 +0200595
Pim Schellart5a7d17f2016-06-17 17:35:59 -0400596 A translator may choose to do one of the following:
Wenzel Jakob1f66a582016-07-18 10:47:10 +0200597
Pim Schellart5a7d17f2016-06-17 17:35:59 -0400598 - catch the exception and call PyErr_SetString or PyErr_SetObject
599 to set a standard (or custom) Python exception, or
600 - do nothing and let the exception fall through to the next translator, or
601 - delegate translation to the next translator by throwing a new type of exception. */
602
Wenzel Jakob1f66a582016-07-18 10:47:10 +0200603 auto last_exception = std::current_exception();
Jason Rhinelanderbfcf9522017-01-30 13:34:38 -0500604 auto &registered_exception_translators = get_internals().registered_exception_translators;
Pim Schellart5a7d17f2016-06-17 17:35:59 -0400605 for (auto& translator : registered_exception_translators) {
606 try {
607 translator(last_exception);
608 } catch (...) {
609 last_exception = std::current_exception();
610 continue;
611 }
612 return nullptr;
613 }
614 PyErr_SetString(PyExc_SystemError, "Exception escaped from default exception translator!");
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100615 return nullptr;
616 }
617
618 if (result.ptr() == PYBIND11_TRY_NEXT_OVERLOAD) {
Wenzel Jakob382484a2016-09-10 15:28:37 +0900619 if (overloads->is_operator)
620 return handle(Py_NotImplemented).inc_ref().ptr();
621
Wenzel Jakobe99ebae2016-09-12 11:44:37 +0900622 std::string msg = std::string(overloads->name) + "(): incompatible " +
623 std::string(overloads->is_constructor ? "constructor" : "function") +
624 " arguments. The following argument types are supported:\n";
625
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100626 int ctr = 0;
Jason Rhinelanderbfcf9522017-01-30 13:34:38 -0500627 for (function_record *it2 = overloads; it2 != nullptr; it2 = it2->next) {
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100628 msg += " "+ std::to_string(++ctr) + ". ";
Jason Rhinelander4e45e182016-07-17 17:43:00 -0400629
630 bool wrote_sig = false;
631 if (overloads->is_constructor) {
Dean Moldovanecced6c2016-07-31 20:03:18 +0200632 // For a constructor, rewrite `(self: Object, arg0, ...) -> NoneType` as `Object(arg0, ...)`
Jason Rhinelander4e45e182016-07-17 17:43:00 -0400633 std::string sig = it2->signature;
Dean Moldovanecced6c2016-07-31 20:03:18 +0200634 size_t start = sig.find('(') + 7; // skip "(self: "
Jason Rhinelander4e45e182016-07-17 17:43:00 -0400635 if (start < sig.size()) {
636 // End at the , for the next argument
637 size_t end = sig.find(", "), next = end + 2;
638 size_t ret = sig.rfind(" -> ");
639 // Or the ), if there is no comma:
640 if (end >= sig.size()) next = end = sig.find(')');
641 if (start < end && next < sig.size()) {
642 msg.append(sig, start, end - start);
643 msg += '(';
644 msg.append(sig, next, ret - next);
645 wrote_sig = true;
646 }
647 }
648 }
649 if (!wrote_sig) msg += it2->signature;
650
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100651 msg += "\n";
652 }
Wenzel Jakobe99ebae2016-09-12 11:44:37 +0900653 msg += "\nInvoked with: ";
Jason Rhinelander2686da82017-01-21 23:42:14 -0500654 auto args_ = reinterpret_borrow<tuple>(args_in);
Wenzel Jakob1f66a582016-07-18 10:47:10 +0200655 for (size_t ti = overloads->is_constructor ? 1 : 0; ti < args_.size(); ++ti) {
Jason Rhinelander7146d622016-11-22 05:28:40 -0500656 msg += pybind11::repr(args_[ti]);
Andreas Bergmeier16d43942016-05-24 09:19:35 +0200657 if ((ti + 1) != args_.size() )
658 msg += ", ";
659 }
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100660 PyErr_SetString(PyExc_TypeError, msg.c_str());
661 return nullptr;
662 } else if (!result) {
663 std::string msg = "Unable to convert function return value to a "
664 "Python type! The signature was\n\t";
665 msg += it->signature;
666 PyErr_SetString(PyExc_TypeError, msg.c_str());
667 return nullptr;
668 } else {
669 if (overloads->is_constructor) {
Wenzel Jakob772c6d52016-04-30 19:56:10 +0200670 /* When a constructor ran successfully, the corresponding
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100671 holder type (e.g. std::unique_ptr) must still be initialized. */
Jason Rhinelanderbfcf9522017-01-30 13:34:38 -0500672 auto tinfo = get_type_info(Py_TYPE(parent.ptr()));
Jason Rhinelander2686da82017-01-21 23:42:14 -0500673 tinfo->init_holder(parent.ptr(), nullptr);
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100674 }
675 return result.ptr();
676 }
677 }
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200678};
679
Wenzel Jakobd561cb02016-01-17 22:36:41 +0100680/// Wrapper for Python extension modules
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200681class module : public object {
682public:
Wenzel Jakobb1b71402015-10-18 16:48:30 +0200683 PYBIND11_OBJECT_DEFAULT(module, object, PyModule_Check)
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200684
Dean Moldovan57a9bbc2017-01-31 16:54:08 +0100685 /// Create a new top-level Python module with the given name and docstring
Jason Rhinelander12d76602016-10-16 16:27:42 -0400686 explicit module(const char *name, const char *doc = nullptr) {
Alexander Stukowski9a110e62016-11-15 12:38:05 +0100687 if (!options::show_user_defined_docstrings()) doc = nullptr;
Wenzel Jakob57082212015-09-04 23:42:12 +0200688#if PY_MAJOR_VERSION >= 3
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200689 PyModuleDef *def = new PyModuleDef();
690 memset(def, 0, sizeof(PyModuleDef));
691 def->m_name = name;
692 def->m_doc = doc;
693 def->m_size = -1;
694 Py_INCREF(def);
695 m_ptr = PyModule_Create(def);
Wenzel Jakob57082212015-09-04 23:42:12 +0200696#else
697 m_ptr = Py_InitModule3(name, nullptr, doc);
698#endif
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200699 if (m_ptr == nullptr)
Wenzel Jakob678d7872016-01-17 22:36:41 +0100700 pybind11_fail("Internal error in module::module()");
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200701 inc_ref();
702 }
703
Dean Moldovan57a9bbc2017-01-31 16:54:08 +0100704 /** \rst
705 Create Python binding for a new function within the module scope. ``Func``
706 can be a plain C++ function, a function pointer, or a lambda function. For
707 details on the ``Extra&& ... extra`` argument, see section :ref:`extras`.
708 \endrst */
Wenzel Jakob71867832015-07-29 17:43:52 +0200709 template <typename Func, typename... Extra>
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100710 module &def(const char *name_, Func &&f, const Extra& ... extra) {
Dean Moldovan865e4302016-09-21 01:06:32 +0200711 cpp_function func(std::forward<Func>(f), name(name_), scope(*this),
712 sibling(getattr(*this, name_, none())), extra...);
Jason Rhinelander6873c202016-10-24 21:58:22 -0400713 // NB: allow overwriting here because cpp_function sets up a chain with the intention of
714 // overwriting (and has already checked internally that it isn't overwriting non-functions).
715 add_object(name_, func, true /* overwrite */);
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200716 return *this;
717 }
718
Dean Moldovan57a9bbc2017-01-31 16:54:08 +0100719 /** \rst
720 Create and return a new Python submodule with the given name and docstring.
721 This also works recursively, i.e.
722
723 .. code-block:: cpp
724
725 py::module m("example", "pybind11 example plugin");
726 py::module m2 = m.def_submodule("sub", "A submodule of 'example'");
727 py::module m3 = m2.def_submodule("subsub", "A submodule of 'example.sub'");
728 \endrst */
Wenzel Jakobbd4a5292015-07-11 17:41:48 +0200729 module def_submodule(const char *name, const char *doc = nullptr) {
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200730 std::string full_name = std::string(PyModule_GetName(m_ptr))
731 + std::string(".") + std::string(name);
Dean Moldovanc7ac16b2016-10-28 03:08:15 +0200732 auto result = reinterpret_borrow<module>(PyImport_AddModule(full_name.c_str()));
Alexander Stukowski9a110e62016-11-15 12:38:05 +0100733 if (doc && options::show_user_defined_docstrings())
Wenzel Jakob8f4eb002015-10-15 18:13:33 +0200734 result.attr("__doc__") = pybind11::str(doc);
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200735 attr(name) = result;
736 return result;
737 }
Wenzel Jakobdb028d62015-10-13 23:44:25 +0200738
Dean Moldovan57a9bbc2017-01-31 16:54:08 +0100739 /// Import and return a module or throws `error_already_set`.
Wenzel Jakobdb028d62015-10-13 23:44:25 +0200740 static module import(const char *name) {
Wenzel Jakobdd57a342015-12-26 14:04:52 +0100741 PyObject *obj = PyImport_ImportModule(name);
742 if (!obj)
esquires67a68f12016-12-01 05:35:34 -0500743 throw error_already_set();
Dean Moldovanc7ac16b2016-10-28 03:08:15 +0200744 return reinterpret_steal<module>(obj);
Wenzel Jakobdb028d62015-10-13 23:44:25 +0200745 }
Jason Rhinelander6873c202016-10-24 21:58:22 -0400746
747 // Adds an object to the module using the given name. Throws if an object with the given name
748 // already exists.
749 //
750 // overwrite should almost always be false: attempting to overwrite objects that pybind11 has
751 // established will, in most cases, break things.
752 PYBIND11_NOINLINE void add_object(const char *name, object &obj, bool overwrite = false) {
753 if (!overwrite && hasattr(*this, name))
754 pybind11_fail("Error during initialization: multiple incompatible definitions with name \"" +
755 std::string(name) + "\"");
756
757 obj.inc_ref(); // PyModule_AddObject() steals a reference
758 PyModule_AddObject(ptr(), name, obj.ptr());
759 }
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200760};
761
762NAMESPACE_BEGIN(detail)
Dean Moldovan6fccf692016-10-11 01:12:48 +0200763extern "C" inline PyObject *get_dict(PyObject *op, void *) {
Dean Moldovan22726c92016-10-12 23:20:32 +0200764 PyObject *&dict = *_PyObject_GetDictPtr(op);
Wenzel Jakob1d1f81b2016-12-16 15:00:46 +0100765 if (!dict)
Dean Moldovan22726c92016-10-12 23:20:32 +0200766 dict = PyDict_New();
Dean Moldovan22726c92016-10-12 23:20:32 +0200767 Py_XINCREF(dict);
768 return dict;
Dean Moldovan6fccf692016-10-11 01:12:48 +0200769}
770
Dean Moldovan22726c92016-10-12 23:20:32 +0200771extern "C" inline int set_dict(PyObject *op, PyObject *new_dict, void *) {
772 if (!PyDict_Check(new_dict)) {
Dean Moldovan6fccf692016-10-11 01:12:48 +0200773 PyErr_Format(PyExc_TypeError, "__dict__ must be set to a dictionary, not a '%.200s'",
Dean Moldovan22726c92016-10-12 23:20:32 +0200774 Py_TYPE(new_dict)->tp_name);
Dean Moldovan6fccf692016-10-11 01:12:48 +0200775 return -1;
776 }
Dean Moldovan22726c92016-10-12 23:20:32 +0200777 PyObject *&dict = *_PyObject_GetDictPtr(op);
778 Py_INCREF(new_dict);
779 Py_CLEAR(dict);
780 dict = new_dict;
Dean Moldovan6fccf692016-10-11 01:12:48 +0200781 return 0;
782}
783
784static PyGetSetDef generic_getset[] = {
785 {const_cast<char*>("__dict__"), get_dict, set_dict, nullptr, nullptr},
786 {nullptr, nullptr, nullptr, nullptr, nullptr}
787};
788
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100789/// Generic support for creating new Python heap types
Wenzel Jakobd561cb02016-01-17 22:36:41 +0100790class generic_type : public object {
Jason Rhinelander5fffe202016-09-06 12:17:06 -0400791 template <typename...> friend class class_;
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200792public:
Wenzel Jakobd561cb02016-01-17 22:36:41 +0100793 PYBIND11_OBJECT_DEFAULT(generic_type, object, PyType_Check)
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100794protected:
795 void initialize(type_record *rec) {
Wenzel Jakob38d8b8c2016-05-31 09:53:28 +0200796 auto &internals = get_internals();
797 auto tindex = std::type_index(*(rec->type));
798
Ivan Smirnov8f3e0452016-10-23 15:43:03 +0100799 if (get_type_info(*(rec->type)))
Wenzel Jakob38d8b8c2016-05-31 09:53:28 +0200800 pybind11_fail("generic_type: type \"" + std::string(rec->name) +
801 "\" is already registered!");
802
Dean Moldovanc7ac16b2016-10-28 03:08:15 +0200803 auto name = reinterpret_steal<object>(PYBIND11_FROM_STRING(rec->name));
Wenzel Jakobc47d4982016-07-11 23:40:28 +0200804 object scope_module;
805 if (rec->scope) {
Jason Rhinelander6873c202016-10-24 21:58:22 -0400806 if (hasattr(rec->scope, rec->name))
807 pybind11_fail("generic_type: cannot initialize type \"" + std::string(rec->name) +
808 "\": an object with that name is already defined");
809
Dean Moldovan865e4302016-09-21 01:06:32 +0200810 if (hasattr(rec->scope, "__module__")) {
811 scope_module = rec->scope.attr("__module__");
812 } else if (hasattr(rec->scope, "__name__")) {
813 scope_module = rec->scope.attr("__name__");
814 }
Wenzel Jakobc47d4982016-07-11 23:40:28 +0200815 }
816
817#if PY_MAJOR_VERSION >= 3 && PY_MINOR_VERSION >= 3
818 /* Qualified names for Python >= 3.3 */
819 object scope_qualname;
Dean Moldovan865e4302016-09-21 01:06:32 +0200820 if (rec->scope && hasattr(rec->scope, "__qualname__"))
821 scope_qualname = rec->scope.attr("__qualname__");
Dean Moldovanc91f8bd2017-02-13 18:11:24 +0100822 object ht_qualname;
Wenzel Jakob1d1f81b2016-12-16 15:00:46 +0100823 if (scope_qualname)
Dean Moldovanc7ac16b2016-10-28 03:08:15 +0200824 ht_qualname = reinterpret_steal<object>(PyUnicode_FromFormat(
825 "%U.%U", scope_qualname.ptr(), name.ptr()));
Wenzel Jakob1d1f81b2016-12-16 15:00:46 +0100826 else
Wenzel Jakobc47d4982016-07-11 23:40:28 +0200827 ht_qualname = name;
Wenzel Jakobc47d4982016-07-11 23:40:28 +0200828#endif
Wenzel Jakob8e5dceb2016-09-11 20:00:40 +0900829
Wenzel Jakob1d1f81b2016-12-16 15:00:46 +0100830#if !defined(PYPY_VERSION)
831 std::string full_name = (scope_module ? ((std::string) pybind11::str(scope_module) + "." + rec->name)
832 : std::string(rec->name));
833#else
834 std::string full_name = std::string(rec->name);
835#endif
836
Wenzel Jakob8e5dceb2016-09-11 20:00:40 +0900837 size_t num_bases = rec->bases.size();
Dean Moldovane18bc022016-10-25 22:12:39 +0200838 auto bases = tuple(rec->bases);
Wenzel Jakob8e5dceb2016-09-11 20:00:40 +0900839
Wenzel Jakobc47d4982016-07-11 23:40:28 +0200840 char *tp_doc = nullptr;
Alexander Stukowski9a110e62016-11-15 12:38:05 +0100841 if (rec->doc && options::show_user_defined_docstrings()) {
Wenzel Jakobc47d4982016-07-11 23:40:28 +0200842 /* Allocate memory for docstring (using PyObject_MALLOC, since
843 Python will free this later on) */
844 size_t size = strlen(rec->doc) + 1;
845 tp_doc = (char *) PyObject_MALLOC(size);
846 memcpy((void *) tp_doc, rec->doc, size);
847 }
848
Wenzel Jakob8e5dceb2016-09-11 20:00:40 +0900849 /* Danger zone: from now (and until PyType_Ready), make sure to
850 issue no Python C API calls which could potentially invoke the
851 garbage collector (the GC will call type_traverse(), which will in
852 turn find the newly constructed type in an invalid state) */
853
Dean Moldovanc7ac16b2016-10-28 03:08:15 +0200854 auto type_holder = reinterpret_steal<object>(PyType_Type.tp_alloc(&PyType_Type, 0));
Wenzel Jakobd561cb02016-01-17 22:36:41 +0100855 auto type = (PyHeapTypeObject*) type_holder.ptr();
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200856
Wenzel Jakobd561cb02016-01-17 22:36:41 +0100857 if (!type_holder || !name)
Wenzel Jakobe72a6762016-09-14 23:39:16 +0800858 pybind11_fail(std::string(rec->name) + ": Unable to create type object!");
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200859
Wenzel Jakobd561cb02016-01-17 22:36:41 +0100860 /* Register supplemental type information in C++ dict */
Wenzel Jakobd561cb02016-01-17 22:36:41 +0100861 detail::type_info *tinfo = new detail::type_info();
862 tinfo->type = (PyTypeObject *) type;
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100863 tinfo->type_size = rec->type_size;
864 tinfo->init_holder = rec->init_holder;
Ivan Smirnova6e6a8b2016-10-23 15:27:13 +0100865 tinfo->direct_conversions = &internals.direct_conversions[tindex];
Pim Schellartcc88aae2017-01-31 10:52:11 -0500866 tinfo->default_holder = rec->default_holder;
Wenzel Jakob38d8b8c2016-05-31 09:53:28 +0200867 internals.registered_types_cpp[tindex] = tinfo;
Wenzel Jakobd561cb02016-01-17 22:36:41 +0100868 internals.registered_types_py[type] = tinfo;
869
Wenzel Jakobd561cb02016-01-17 22:36:41 +0100870 /* Basic type attributes */
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200871 type->ht_type.tp_name = strdup(full_name.c_str());
Wenzel Jakob0a078052016-05-29 13:40:40 +0200872 type->ht_type.tp_basicsize = (ssize_t) rec->instance_size;
Wenzel Jakob8e5dceb2016-09-11 20:00:40 +0900873
874 if (num_bases > 0) {
875 type->ht_type.tp_base = (PyTypeObject *) ((object) bases[0]).inc_ref().ptr();
876 type->ht_type.tp_bases = bases.release().ptr();
877 rec->multiple_inheritance |= num_bases > 1;
878 }
Wenzel Jakobd561cb02016-01-17 22:36:41 +0100879
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100880 type->ht_name = name.release().ptr();
Wenzel Jakobd561cb02016-01-17 22:36:41 +0100881
Wenzel Jakobc47d4982016-07-11 23:40:28 +0200882#if PY_MAJOR_VERSION >= 3 && PY_MINOR_VERSION >= 3
883 type->ht_qualname = ht_qualname.release().ptr();
884#endif
885
Dean Moldovanc91f8bd2017-02-13 18:11:24 +0100886 /* Custom metaclass if requested (used for static properties) */
887 if (rec->metaclass)
888 PYBIND11_OB_TYPE(type->ht_type) = internals.default_metaclass;
Wenzel Jakob1d1f81b2016-12-16 15:00:46 +0100889
Wenzel Jakobd561cb02016-01-17 22:36:41 +0100890 /* Supported protocols */
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200891 type->ht_type.tp_as_number = &type->as_number;
892 type->ht_type.tp_as_sequence = &type->as_sequence;
893 type->ht_type.tp_as_mapping = &type->as_mapping;
Wenzel Jakobd561cb02016-01-17 22:36:41 +0100894
895 /* Supported elementary operations */
896 type->ht_type.tp_init = (initproc) init;
897 type->ht_type.tp_new = (newfunc) new_instance;
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100898 type->ht_type.tp_dealloc = rec->dealloc;
Wenzel Jakobd561cb02016-01-17 22:36:41 +0100899
900 /* Support weak references (needed for the keep_alive feature) */
Wenzel Jakob88d1d042016-01-20 01:26:42 +0100901 type->ht_type.tp_weaklistoffset = offsetof(instance_essentials<void>, weakrefs);
Wenzel Jakob5f218b32016-01-17 22:36:39 +0100902
Wenzel Jakobd561cb02016-01-17 22:36:41 +0100903 /* Flags */
904 type->ht_type.tp_flags |= Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HEAPTYPE;
905#if PY_MAJOR_VERSION < 3
906 type->ht_type.tp_flags |= Py_TPFLAGS_CHECKTYPES;
907#endif
908 type->ht_type.tp_flags &= ~Py_TPFLAGS_HAVE_GC;
909
Dean Moldovan6fccf692016-10-11 01:12:48 +0200910 /* Support dynamic attributes */
911 if (rec->dynamic_attr) {
Wenzel Jakob64cb6992016-12-26 13:12:10 +0100912 #if defined(PYPY_VERSION)
913 pybind11_fail(std::string(rec->name) + ": dynamic attributes are "
914 "currently not supported in "
915 "conunction with PyPy!");
916 #endif
Dean Moldovan6fccf692016-10-11 01:12:48 +0200917 type->ht_type.tp_flags |= Py_TPFLAGS_HAVE_GC;
Dean Moldovan22726c92016-10-12 23:20:32 +0200918 type->ht_type.tp_dictoffset = type->ht_type.tp_basicsize; // place the dict at the end
919 type->ht_type.tp_basicsize += sizeof(PyObject *); // and allocate enough space for it
Dean Moldovan6fccf692016-10-11 01:12:48 +0200920 type->ht_type.tp_getset = generic_getset;
921 type->ht_type.tp_traverse = traverse;
922 type->ht_type.tp_clear = clear;
923 }
924
Wenzel Jakob1d1f81b2016-12-16 15:00:46 +0100925 if (rec->buffer_protocol) {
926 type->ht_type.tp_as_buffer = &type->as_buffer;
927#if PY_MAJOR_VERSION < 3
928 type->ht_type.tp_flags |= Py_TPFLAGS_HAVE_NEWBUFFER;
929#endif
930 type->as_buffer.bf_getbuffer = getbuffer;
931 type->as_buffer.bf_releasebuffer = releasebuffer;
932 }
933
Wenzel Jakobc47d4982016-07-11 23:40:28 +0200934 type->ht_type.tp_doc = tp_doc;
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200935
Wenzel Jakob1d1f81b2016-12-16 15:00:46 +0100936 m_ptr = type_holder.ptr();
937
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200938 if (PyType_Ready(&type->ht_type) < 0)
Wenzel Jakobe72a6762016-09-14 23:39:16 +0800939 pybind11_fail(std::string(rec->name) + ": PyType_Ready failed (" +
940 detail::error_string() + ")!");
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200941
Wenzel Jakobd561cb02016-01-17 22:36:41 +0100942 if (scope_module) // Needed by pydoc
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100943 attr("__module__") = scope_module;
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200944
Wenzel Jakobd561cb02016-01-17 22:36:41 +0100945 /* Register type with the parent scope */
Wenzel Jakobb2825952016-04-13 23:33:00 +0200946 if (rec->scope)
947 rec->scope.attr(handle(type->ht_name)) = *this;
Wenzel Jakobd561cb02016-01-17 22:36:41 +0100948
Wenzel Jakob8e5dceb2016-09-11 20:00:40 +0900949 if (rec->multiple_inheritance)
950 mark_parents_nonsimple(&type->ht_type);
951
Wenzel Jakobd561cb02016-01-17 22:36:41 +0100952 type_holder.release();
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200953 }
954
Wenzel Jakob8e5dceb2016-09-11 20:00:40 +0900955 /// Helper function which tags all parents of a type using mult. inheritance
956 void mark_parents_nonsimple(PyTypeObject *value) {
Dean Moldovanc7ac16b2016-10-28 03:08:15 +0200957 auto t = reinterpret_borrow<tuple>(value->tp_bases);
Wenzel Jakob8e5dceb2016-09-11 20:00:40 +0900958 for (handle h : t) {
959 auto tinfo2 = get_type_info((PyTypeObject *) h.ptr());
960 if (tinfo2)
961 tinfo2->simple_type = false;
962 mark_parents_nonsimple((PyTypeObject *) h.ptr());
963 }
964 }
965
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200966 static int init(void *self, PyObject *, PyObject *) {
Wenzel Jakob1d1f81b2016-12-16 15:00:46 +0100967 PyTypeObject *type = Py_TYPE(self);
968 std::string msg;
969#if defined(PYPY_VERSION)
970 msg += handle((PyObject *) type).attr("__module__").cast<std::string>() + ".";
971#endif
972 msg += type->tp_name;
973 msg += ": No constructor defined!";
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200974 PyErr_SetString(PyExc_TypeError, msg.c_str());
975 return -1;
976 }
977
978 static PyObject *new_instance(PyTypeObject *type, PyObject *, PyObject *) {
Wenzel Jakobd561cb02016-01-17 22:36:41 +0100979 instance<void> *self = (instance<void> *) PyType_GenericAlloc((PyTypeObject *) type, 0);
980 auto tinfo = detail::get_type_info(type);
981 self->value = ::operator new(tinfo->type_size);
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200982 self->owned = true;
Jason Rhinelanderc07ec312016-11-06 13:12:48 -0500983 self->holder_constructed = false;
Jason Rhinelander1b05ce52016-08-09 17:57:59 -0400984 detail::get_internals().registered_instances.emplace(self->value, (PyObject *) self);
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200985 return (PyObject *) self;
986 }
987
988 static void dealloc(instance<void> *self) {
989 if (self->value) {
Jason Rhinelander1b05ce52016-08-09 17:57:59 -0400990 auto instance_type = Py_TYPE(self);
991 auto &registered_instances = detail::get_internals().registered_instances;
992 auto range = registered_instances.equal_range(self->value);
993 bool found = false;
994 for (auto it = range.first; it != range.second; ++it) {
995 if (instance_type == Py_TYPE(it->second)) {
996 registered_instances.erase(it);
997 found = true;
998 break;
999 }
Wenzel Jakob38bd7112015-07-05 20:05:44 +02001000 }
Jason Rhinelander1b05ce52016-08-09 17:57:59 -04001001 if (!found)
1002 pybind11_fail("generic_type::dealloc(): Tried to deallocate unregistered instance!");
1003
Wenzel Jakob5f218b32016-01-17 22:36:39 +01001004 if (self->weakrefs)
1005 PyObject_ClearWeakRefs((PyObject *) self);
Dean Moldovan6fccf692016-10-11 01:12:48 +02001006
Dean Moldovan22726c92016-10-12 23:20:32 +02001007 PyObject **dict_ptr = _PyObject_GetDictPtr((PyObject *) self);
Wenzel Jakob1d1f81b2016-12-16 15:00:46 +01001008 if (dict_ptr)
Dean Moldovan22726c92016-10-12 23:20:32 +02001009 Py_CLEAR(*dict_ptr);
Wenzel Jakob38bd7112015-07-05 20:05:44 +02001010 }
1011 Py_TYPE(self)->tp_free((PyObject*) self);
1012 }
1013
Dean Moldovan6fccf692016-10-11 01:12:48 +02001014 static int traverse(PyObject *op, visitproc visit, void *arg) {
Dean Moldovan22726c92016-10-12 23:20:32 +02001015 PyObject *&dict = *_PyObject_GetDictPtr(op);
1016 Py_VISIT(dict);
Dean Moldovan6fccf692016-10-11 01:12:48 +02001017 return 0;
1018 }
1019
1020 static int clear(PyObject *op) {
Dean Moldovan22726c92016-10-12 23:20:32 +02001021 PyObject *&dict = *_PyObject_GetDictPtr(op);
1022 Py_CLEAR(dict);
Dean Moldovan6fccf692016-10-11 01:12:48 +02001023 return 0;
1024 }
1025
Wenzel Jakob43398a82015-07-28 16:12:20 +02001026 void install_buffer_funcs(
1027 buffer_info *(*get_buffer)(PyObject *, void *),
1028 void *get_buffer_data) {
Wenzel Jakob38bd7112015-07-05 20:05:44 +02001029 PyHeapTypeObject *type = (PyHeapTypeObject*) m_ptr;
Wenzel Jakobd561cb02016-01-17 22:36:41 +01001030 auto tinfo = detail::get_type_info(&type->ht_type);
Wenzel Jakob1d1f81b2016-12-16 15:00:46 +01001031
1032 if (!type->ht_type.tp_as_buffer)
1033 pybind11_fail(
1034 "To be able to register buffer protocol support for the type '" +
1035 std::string(tinfo->type->tp_name) +
1036 "' the associated class<>(..) invocation must "
1037 "include the pybind11::buffer_protocol() annotation!");
1038
Wenzel Jakobd561cb02016-01-17 22:36:41 +01001039 tinfo->get_buffer = get_buffer;
1040 tinfo->get_buffer_data = get_buffer_data;
Wenzel Jakob38bd7112015-07-05 20:05:44 +02001041 }
1042
1043 static int getbuffer(PyObject *obj, Py_buffer *view, int flags) {
Wenzel Jakobd561cb02016-01-17 22:36:41 +01001044 auto tinfo = detail::get_type_info(Py_TYPE(obj));
1045 if (view == nullptr || obj == nullptr || !tinfo || !tinfo->get_buffer) {
Wenzel Jakob1d1f81b2016-12-16 15:00:46 +01001046 if (view)
1047 view->obj = nullptr;
Wenzel Jakobd561cb02016-01-17 22:36:41 +01001048 PyErr_SetString(PyExc_BufferError, "generic_type::getbuffer(): Internal error");
Wenzel Jakob38bd7112015-07-05 20:05:44 +02001049 return -1;
1050 }
1051 memset(view, 0, sizeof(Py_buffer));
Wenzel Jakobd561cb02016-01-17 22:36:41 +01001052 buffer_info *info = tinfo->get_buffer(obj, tinfo->get_buffer_data);
Wenzel Jakob38bd7112015-07-05 20:05:44 +02001053 view->obj = obj;
1054 view->ndim = 1;
1055 view->internal = info;
1056 view->buf = info->ptr;
Wenzel Jakob0a078052016-05-29 13:40:40 +02001057 view->itemsize = (ssize_t) info->itemsize;
Wenzel Jakob38bd7112015-07-05 20:05:44 +02001058 view->len = view->itemsize;
1059 for (auto s : info->shape)
1060 view->len *= s;
1061 if ((flags & PyBUF_FORMAT) == PyBUF_FORMAT)
1062 view->format = const_cast<char *>(info->format.c_str());
1063 if ((flags & PyBUF_STRIDES) == PyBUF_STRIDES) {
Wenzel Jakob0a078052016-05-29 13:40:40 +02001064 view->ndim = (int) info->ndim;
Wenzel Jakob27e8e102016-01-17 22:36:37 +01001065 view->strides = (ssize_t *) &info->strides[0];
1066 view->shape = (ssize_t *) &info->shape[0];
Wenzel Jakob38bd7112015-07-05 20:05:44 +02001067 }
1068 Py_INCREF(view->obj);
1069 return 0;
1070 }
1071
1072 static void releasebuffer(PyObject *, Py_buffer *view) { delete (buffer_info *) view->internal; }
Wenzel Jakob1d1f81b2016-12-16 15:00:46 +01001073
1074 void def_property_static_impl(const char *name,
1075 handle fget, handle fset,
1076 detail::function_record *rec_fget) {
Dean Moldovanc91f8bd2017-02-13 18:11:24 +01001077 const auto is_static = !(rec_fget->is_method && rec_fget->scope);
1078 const auto has_doc = rec_fget->doc && pybind11::options::show_user_defined_docstrings();
1079
1080 if (is_static) {
Wenzel Jakob1d1f81b2016-12-16 15:00:46 +01001081 auto mclass = handle((PyObject *) PYBIND11_OB_TYPE(*((PyTypeObject *) m_ptr)));
1082
1083 if ((PyTypeObject *) mclass.ptr() == &PyType_Type)
1084 pybind11_fail(
1085 "Adding static properties to the type '" +
1086 std::string(((PyTypeObject *) m_ptr)->tp_name) +
1087 "' requires the type to have a custom metaclass. Please "
1088 "ensure that one is created by supplying the pybind11::metaclass() "
1089 "annotation to the associated class_<>(..) invocation.");
Wenzel Jakob1d1f81b2016-12-16 15:00:46 +01001090 }
Dean Moldovanc91f8bd2017-02-13 18:11:24 +01001091
1092 auto property = handle((PyObject *) (is_static ? get_internals().static_property_type
1093 : &PyProperty_Type));
1094 attr(name) = property(fget.ptr() ? fget : none(),
1095 fset.ptr() ? fset : none(),
1096 /*deleter*/none(),
1097 pybind11::str(has_doc ? rec_fget->doc : ""));
Wenzel Jakob1d1f81b2016-12-16 15:00:46 +01001098 }
Wenzel Jakob38bd7112015-07-05 20:05:44 +02001099};
Jason Rhinelander6b52c832016-09-06 12:27:00 -04001100
Wenzel Jakob38bd7112015-07-05 20:05:44 +02001101NAMESPACE_END(detail)
1102
Jason Rhinelander5fffe202016-09-06 12:17:06 -04001103template <typename type_, typename... options>
Wenzel Jakob48548ea2016-01-17 22:36:44 +01001104class class_ : public detail::generic_type {
Jason Rhinelander5fffe202016-09-06 12:17:06 -04001105 template <typename T> using is_holder = detail::is_holder_type<type_, T>;
1106 template <typename T> using is_subtype = detail::bool_constant<std::is_base_of<type_, T>::value && !std::is_same<T, type_>::value>;
Wenzel Jakob8e5dceb2016-09-11 20:00:40 +09001107 template <typename T> using is_base = detail::bool_constant<std::is_base_of<T, type_>::value && !std::is_same<T, type_>::value>;
Jason Rhinelanderfa5d05e2016-12-12 18:11:49 -05001108 // struct instead of using here to help MSVC:
1109 template <typename T> struct is_valid_class_option :
1110 detail::any_of<is_holder<T>, is_subtype<T>, is_base<T>> {};
Jason Rhinelander5fffe202016-09-06 12:17:06 -04001111
Wenzel Jakob38bd7112015-07-05 20:05:44 +02001112public:
Jason Rhinelander5fffe202016-09-06 12:17:06 -04001113 using type = type_;
Jason Rhinelander6b52c832016-09-06 12:27:00 -04001114 using type_alias = detail::first_of_t<is_subtype, void, options...>;
Jason Rhinelander5fffe202016-09-06 12:17:06 -04001115 constexpr static bool has_alias = !std::is_void<type_alias>::value;
Jason Rhinelander6b52c832016-09-06 12:27:00 -04001116 using holder_type = detail::first_of_t<is_holder, std::unique_ptr<type>, options...>;
Jason Rhinelander5fffe202016-09-06 12:17:06 -04001117 using instance_type = detail::instance<type, holder_type>;
1118
Jason Rhinelanderfa5d05e2016-12-12 18:11:49 -05001119 static_assert(detail::all_of<is_valid_class_option<options>...>::value,
Jason Rhinelander5fffe202016-09-06 12:17:06 -04001120 "Unknown/invalid class_ template parameters provided");
Wenzel Jakob38bd7112015-07-05 20:05:44 +02001121
Dean Moldovanc7ac16b2016-10-28 03:08:15 +02001122 PYBIND11_OBJECT(class_, generic_type, PyType_Check)
Wenzel Jakob38bd7112015-07-05 20:05:44 +02001123
Wenzel Jakob48548ea2016-01-17 22:36:44 +01001124 template <typename... Extra>
1125 class_(handle scope, const char *name, const Extra &... extra) {
1126 detail::type_record record;
1127 record.scope = scope;
1128 record.name = name;
1129 record.type = &typeid(type);
Jason Rhinelander9c6859e2016-09-08 11:03:08 -04001130 record.type_size = sizeof(detail::conditional_t<has_alias, type_alias, type>);
Wenzel Jakob48548ea2016-01-17 22:36:44 +01001131 record.instance_size = sizeof(instance_type);
1132 record.init_holder = init_holder;
1133 record.dealloc = dealloc;
Pim Schellartcc88aae2017-01-31 10:52:11 -05001134 record.default_holder = std::is_same<holder_type, std::unique_ptr<type>>::value;
Wenzel Jakob38bd7112015-07-05 20:05:44 +02001135
Wenzel Jakob8e5dceb2016-09-11 20:00:40 +09001136 /* Register base classes specified via template arguments to class_, if any */
Wenzel Jakobc1fc27e2016-09-13 00:36:43 +09001137 bool unused[] = { (add_base<options>(record), false)..., false };
Wenzel Jakob8e5dceb2016-09-11 20:00:40 +09001138 (void) unused;
Jason Rhinelander6b52c832016-09-06 12:27:00 -04001139
Wenzel Jakob48548ea2016-01-17 22:36:44 +01001140 /* Process optional arguments, if any */
1141 detail::process_attributes<Extra...>::init(extra..., &record);
1142
1143 detail::generic_type::initialize(&record);
Wenzel Jakob86d825f2016-05-26 13:19:27 +02001144
Jason Rhinelander5fffe202016-09-06 12:17:06 -04001145 if (has_alias) {
Wenzel Jakob86d825f2016-05-26 13:19:27 +02001146 auto &instances = pybind11::detail::get_internals().registered_types_cpp;
1147 instances[std::type_index(typeid(type_alias))] = instances[std::type_index(typeid(type))];
1148 }
Wenzel Jakob48548ea2016-01-17 22:36:44 +01001149 }
Wenzel Jakob38bd7112015-07-05 20:05:44 +02001150
Wenzel Jakobc1fc27e2016-09-13 00:36:43 +09001151 template <typename Base, detail::enable_if_t<is_base<Base>::value, int> = 0>
Wenzel Jakob8e5dceb2016-09-11 20:00:40 +09001152 static void add_base(detail::type_record &rec) {
1153 rec.add_base(&typeid(Base), [](void *src) -> void * {
1154 return static_cast<Base *>(reinterpret_cast<type *>(src));
1155 });
1156 }
1157
Wenzel Jakobc1fc27e2016-09-13 00:36:43 +09001158 template <typename Base, detail::enable_if_t<!is_base<Base>::value, int> = 0>
Wenzel Jakob8e5dceb2016-09-11 20:00:40 +09001159 static void add_base(detail::type_record &) { }
1160
Wenzel Jakob71867832015-07-29 17:43:52 +02001161 template <typename Func, typename... Extra>
Wenzel Jakob48548ea2016-01-17 22:36:44 +01001162 class_ &def(const char *name_, Func&& f, const Extra&... extra) {
Dean Moldovan865e4302016-09-21 01:06:32 +02001163 cpp_function cf(std::forward<Func>(f), name(name_), is_method(*this),
1164 sibling(getattr(*this, name_, none())), extra...);
Wenzel Jakob57082212015-09-04 23:42:12 +02001165 attr(cf.name()) = cf;
Wenzel Jakob38bd7112015-07-05 20:05:44 +02001166 return *this;
1167 }
1168
Wenzel Jakob71867832015-07-29 17:43:52 +02001169 template <typename Func, typename... Extra> class_ &
Wenzel Jakob48548ea2016-01-17 22:36:44 +01001170 def_static(const char *name_, Func f, const Extra&... extra) {
Dean Moldovan865e4302016-09-21 01:06:32 +02001171 cpp_function cf(std::forward<Func>(f), name(name_), scope(*this),
1172 sibling(getattr(*this, name_, none())), extra...);
Wenzel Jakob57082212015-09-04 23:42:12 +02001173 attr(cf.name()) = cf;
Wenzel Jakob38bd7112015-07-05 20:05:44 +02001174 return *this;
1175 }
1176
Wenzel Jakob71867832015-07-29 17:43:52 +02001177 template <detail::op_id id, detail::op_type ot, typename L, typename R, typename... Extra>
Wenzel Jakob48548ea2016-01-17 22:36:44 +01001178 class_ &def(const detail::op_<id, ot, L, R> &op, const Extra&... extra) {
Jason Rhinelander5fffe202016-09-06 12:17:06 -04001179 op.execute(*this, extra...);
Wenzel Jakob38bd7112015-07-05 20:05:44 +02001180 return *this;
1181 }
1182
Wenzel Jakob71867832015-07-29 17:43:52 +02001183 template <detail::op_id id, detail::op_type ot, typename L, typename R, typename... Extra>
Wenzel Jakob48548ea2016-01-17 22:36:44 +01001184 class_ & def_cast(const detail::op_<id, ot, L, R> &op, const Extra&... extra) {
Jason Rhinelander5fffe202016-09-06 12:17:06 -04001185 op.execute_cast(*this, extra...);
Wenzel Jakob38bd7112015-07-05 20:05:44 +02001186 return *this;
1187 }
1188
Wenzel Jakob71867832015-07-29 17:43:52 +02001189 template <typename... Args, typename... Extra>
Wenzel Jakob48548ea2016-01-17 22:36:44 +01001190 class_ &def(const detail::init<Args...> &init, const Extra&... extra) {
Jason Rhinelander5fffe202016-09-06 12:17:06 -04001191 init.execute(*this, extra...);
Wenzel Jakob38bd7112015-07-05 20:05:44 +02001192 return *this;
1193 }
1194
Wenzel Jakob86d825f2016-05-26 13:19:27 +02001195 template <typename... Args, typename... Extra>
1196 class_ &def(const detail::init_alias<Args...> &init, const Extra&... extra) {
Jason Rhinelander5fffe202016-09-06 12:17:06 -04001197 init.execute(*this, extra...);
Wenzel Jakob86d825f2016-05-26 13:19:27 +02001198 return *this;
1199 }
1200
Wenzel Jakob71867832015-07-29 17:43:52 +02001201 template <typename Func> class_& def_buffer(Func &&func) {
Wenzel Jakob43398a82015-07-28 16:12:20 +02001202 struct capture { Func func; };
1203 capture *ptr = new capture { std::forward<Func>(func) };
1204 install_buffer_funcs([](PyObject *obj, void *ptr) -> buffer_info* {
Dean Moldovan5f07fac2017-01-03 11:52:05 +01001205 detail::make_caster<type> caster;
Wenzel Jakob38bd7112015-07-05 20:05:44 +02001206 if (!caster.load(obj, false))
1207 return nullptr;
Wenzel Jakob43398a82015-07-28 16:12:20 +02001208 return new buffer_info(((capture *) ptr)->func(caster));
1209 }, ptr);
Wenzel Jakob38bd7112015-07-05 20:05:44 +02001210 return *this;
1211 }
1212
Wenzel Jakob71867832015-07-29 17:43:52 +02001213 template <typename C, typename D, typename... Extra>
Wenzel Jakob48548ea2016-01-17 22:36:44 +01001214 class_ &def_readwrite(const char *name, D C::*pm, const Extra&... extra) {
Wenzel Jakob0b489582016-03-25 16:13:10 +01001215 cpp_function fget([pm](const C &c) -> const D &{ return c.*pm; }, is_method(*this)),
1216 fset([pm](C &c, const D &value) { c.*pm = value; }, is_method(*this));
1217 def_property(name, fget, fset, return_value_policy::reference_internal, extra...);
Wenzel Jakob38bd7112015-07-05 20:05:44 +02001218 return *this;
1219 }
1220
Wenzel Jakob71867832015-07-29 17:43:52 +02001221 template <typename C, typename D, typename... Extra>
Wenzel Jakob48548ea2016-01-17 22:36:44 +01001222 class_ &def_readonly(const char *name, const D C::*pm, const Extra& ...extra) {
Wenzel Jakob0b489582016-03-25 16:13:10 +01001223 cpp_function fget([pm](const C &c) -> const D &{ return c.*pm; }, is_method(*this));
1224 def_property_readonly(name, fget, return_value_policy::reference_internal, extra...);
Wenzel Jakob38bd7112015-07-05 20:05:44 +02001225 return *this;
1226 }
1227
Wenzel Jakob71867832015-07-29 17:43:52 +02001228 template <typename D, typename... Extra>
Wenzel Jakob48548ea2016-01-17 22:36:44 +01001229 class_ &def_readwrite_static(const char *name, D *pm, const Extra& ...extra) {
Wenzel Jakob0b489582016-03-25 16:13:10 +01001230 cpp_function fget([pm](object) -> const D &{ return *pm; }, scope(*this)),
1231 fset([pm](object, const D &value) { *pm = value; }, scope(*this));
1232 def_property_static(name, fget, fset, return_value_policy::reference, extra...);
Wenzel Jakob38bd7112015-07-05 20:05:44 +02001233 return *this;
1234 }
1235
Wenzel Jakob71867832015-07-29 17:43:52 +02001236 template <typename D, typename... Extra>
Wenzel Jakob48548ea2016-01-17 22:36:44 +01001237 class_ &def_readonly_static(const char *name, const D *pm, const Extra& ...extra) {
Wenzel Jakob0b489582016-03-25 16:13:10 +01001238 cpp_function fget([pm](object) -> const D &{ return *pm; }, scope(*this));
1239 def_property_readonly_static(name, fget, return_value_policy::reference, extra...);
Wenzel Jakob38bd7112015-07-05 20:05:44 +02001240 return *this;
1241 }
1242
Dean Moldovan03f627e2016-11-01 11:44:57 +01001243 /// Uses return_value_policy::reference_internal by default
1244 template <typename Getter, typename... Extra>
1245 class_ &def_property_readonly(const char *name, const Getter &fget, const Extra& ...extra) {
1246 return def_property_readonly(name, cpp_function(fget), return_value_policy::reference_internal, extra...);
1247 }
1248
1249 /// Uses cpp_function's return_value_policy by default
Wenzel Jakob84ec78f2016-03-21 17:54:24 +01001250 template <typename... Extra>
1251 class_ &def_property_readonly(const char *name, const cpp_function &fget, const Extra& ...extra) {
Dean Moldovan03f627e2016-11-01 11:44:57 +01001252 return def_property(name, fget, cpp_function(), extra...);
Wenzel Jakob38bd7112015-07-05 20:05:44 +02001253 }
1254
Dean Moldovan03f627e2016-11-01 11:44:57 +01001255 /// Uses return_value_policy::reference by default
1256 template <typename Getter, typename... Extra>
1257 class_ &def_property_readonly_static(const char *name, const Getter &fget, const Extra& ...extra) {
1258 return def_property_readonly_static(name, cpp_function(fget), return_value_policy::reference, extra...);
1259 }
1260
1261 /// Uses cpp_function's return_value_policy by default
Wenzel Jakob84ec78f2016-03-21 17:54:24 +01001262 template <typename... Extra>
1263 class_ &def_property_readonly_static(const char *name, const cpp_function &fget, const Extra& ...extra) {
Dean Moldovan03f627e2016-11-01 11:44:57 +01001264 return def_property_static(name, fget, cpp_function(), extra...);
Wenzel Jakob38bd7112015-07-05 20:05:44 +02001265 }
1266
Dean Moldovan03f627e2016-11-01 11:44:57 +01001267 /// Uses return_value_policy::reference_internal by default
1268 template <typename Getter, typename... Extra>
1269 class_ &def_property(const char *name, const Getter &fget, const cpp_function &fset, const Extra& ...extra) {
1270 return def_property(name, cpp_function(fget), fset, return_value_policy::reference_internal, extra...);
1271 }
1272
1273 /// Uses cpp_function's return_value_policy by default
Wenzel Jakob84ec78f2016-03-21 17:54:24 +01001274 template <typename... Extra>
1275 class_ &def_property(const char *name, const cpp_function &fget, const cpp_function &fset, const Extra& ...extra) {
Wenzel Jakob0b489582016-03-25 16:13:10 +01001276 return def_property_static(name, fget, fset, is_method(*this), extra...);
Wenzel Jakob84ec78f2016-03-21 17:54:24 +01001277 }
1278
Dean Moldovan03f627e2016-11-01 11:44:57 +01001279 /// Uses return_value_policy::reference by default
1280 template <typename Getter, typename... Extra>
1281 class_ &def_property_static(const char *name, const Getter &fget, const cpp_function &fset, const Extra& ...extra) {
1282 return def_property_static(name, cpp_function(fget), fset, return_value_policy::reference, extra...);
1283 }
1284
1285 /// Uses cpp_function's return_value_policy by default
Wenzel Jakob84ec78f2016-03-21 17:54:24 +01001286 template <typename... Extra>
1287 class_ &def_property_static(const char *name, const cpp_function &fget, const cpp_function &fset, const Extra& ...extra) {
1288 auto rec_fget = get_function_record(fget), rec_fset = get_function_record(fset);
Wenzel Jakob7c99ff22016-06-02 20:33:01 +02001289 char *doc_prev = rec_fget->doc; /* 'extra' field may include a property-specific documentation string */
Wenzel Jakob84ec78f2016-03-21 17:54:24 +01001290 detail::process_attributes<Extra...>::init(extra..., rec_fget);
Wenzel Jakob7c99ff22016-06-02 20:33:01 +02001291 if (rec_fget->doc && rec_fget->doc != doc_prev) {
1292 free(doc_prev);
1293 rec_fget->doc = strdup(rec_fget->doc);
1294 }
1295 if (rec_fset) {
1296 doc_prev = rec_fset->doc;
Wenzel Jakob84ec78f2016-03-21 17:54:24 +01001297 detail::process_attributes<Extra...>::init(extra..., rec_fset);
Wenzel Jakob7c99ff22016-06-02 20:33:01 +02001298 if (rec_fset->doc && rec_fset->doc != doc_prev) {
1299 free(doc_prev);
1300 rec_fset->doc = strdup(rec_fset->doc);
1301 }
1302 }
Wenzel Jakob1d1f81b2016-12-16 15:00:46 +01001303 def_property_static_impl(name, fget, fset, rec_fget);
Wenzel Jakob38bd7112015-07-05 20:05:44 +02001304 return *this;
1305 }
Wenzel Jakoba2f6fde2015-10-01 16:46:03 +02001306
Wenzel Jakob38bd7112015-07-05 20:05:44 +02001307private:
Wenzel Jakobb2c2c792016-01-17 22:36:40 +01001308 /// Initialize holder object, variant 1: object derives from enable_shared_from_this
1309 template <typename T>
1310 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 +01001311 try {
hbruintjes70d2e572016-07-01 12:39:55 +02001312 new (&inst->holder) holder_type(std::static_pointer_cast<typename holder_type::element_type>(inst->value->shared_from_this()));
Dean Moldovanab90ec62016-12-07 02:36:44 +01001313 inst->holder_constructed = true;
Wenzel Jakob6e213c92015-11-24 23:05:58 +01001314 } catch (const std::bad_weak_ptr &) {
Dean Moldovanab90ec62016-12-07 02:36:44 +01001315 if (inst->owned) {
1316 new (&inst->holder) holder_type(inst->value);
1317 inst->holder_constructed = true;
1318 }
Wenzel Jakob6e213c92015-11-24 23:05:58 +01001319 }
Wenzel Jakobb2c2c792016-01-17 22:36:40 +01001320 }
1321
Dean Moldovanec009a72017-01-31 17:05:44 +01001322 static void init_holder_from_existing(instance_type *inst, const holder_type *holder_ptr,
1323 std::true_type /*is_copy_constructible*/) {
1324 new (&inst->holder) holder_type(*holder_ptr);
Wenzel Jakobb2c2c792016-01-17 22:36:40 +01001325 }
1326
Dean Moldovanec009a72017-01-31 17:05:44 +01001327 static void init_holder_from_existing(instance_type *inst, const holder_type *holder_ptr,
1328 std::false_type /*is_copy_constructible*/) {
1329 new (&inst->holder) holder_type(std::move(*const_cast<holder_type *>(holder_ptr)));
1330 }
1331
1332 /// Initialize holder object, variant 2: try to construct from existing holder object, if possible
1333 static void init_holder_helper(instance_type *inst, const holder_type *holder_ptr, const void * /* dummy */) {
1334 if (holder_ptr) {
1335 init_holder_from_existing(inst, holder_ptr, std::is_copy_constructible<holder_type>());
1336 inst->holder_constructed = true;
1337 } else if (inst->owned || detail::always_construct_holder<holder_type>::value) {
Jason Rhinelanderc07ec312016-11-06 13:12:48 -05001338 new (&inst->holder) holder_type(inst->value);
1339 inst->holder_constructed = true;
1340 }
Wenzel Jakobb2c2c792016-01-17 22:36:40 +01001341 }
1342
1343 /// Initialize holder object of an instance, possibly given a pointer to an existing holder
1344 static void init_holder(PyObject *inst_, const void *holder_ptr) {
1345 auto inst = (instance_type *) inst_;
1346 init_holder_helper(inst, (const holder_type *) holder_ptr, inst->value);
Wenzel Jakob6e213c92015-11-24 23:05:58 +01001347 }
1348
Wenzel Jakob38bd7112015-07-05 20:05:44 +02001349 static void dealloc(PyObject *inst_) {
1350 instance_type *inst = (instance_type *) inst_;
Jason Rhinelanderc07ec312016-11-06 13:12:48 -05001351 if (inst->holder_constructed)
1352 inst->holder.~holder_type();
1353 else if (inst->owned)
1354 ::operator delete(inst->value);
1355
Wenzel Jakobd561cb02016-01-17 22:36:41 +01001356 generic_type::dealloc((detail::instance<void> *) inst);
Wenzel Jakob38bd7112015-07-05 20:05:44 +02001357 }
Wenzel Jakob84ec78f2016-03-21 17:54:24 +01001358
1359 static detail::function_record *get_function_record(handle h) {
1360 h = detail::get_function(h);
Wenzel Jakob1d1f81b2016-12-16 15:00:46 +01001361 return h ? (detail::function_record *) reinterpret_borrow<capsule>(PyCFunction_GET_SELF(h.ptr()))
Dean Moldovanc7ac16b2016-10-28 03:08:15 +02001362 : nullptr;
Wenzel Jakob84ec78f2016-03-21 17:54:24 +01001363 }
Wenzel Jakob38bd7112015-07-05 20:05:44 +02001364};
1365
1366/// Binds C++ enumerations and enumeration classes to Python
1367template <typename Type> class enum_ : public class_<Type> {
1368public:
Wenzel Jakob8ac97152016-09-05 17:20:50 +09001369 using class_<Type>::def;
Wenzel Jakob405f6d12016-11-17 23:24:47 +01001370 using Scalar = typename std::underlying_type<Type>::type;
1371 template <typename T> using arithmetic_tag = std::is_same<T, arithmetic>;
1372
Wenzel Jakob48548ea2016-01-17 22:36:44 +01001373 template <typename... Extra>
1374 enum_(const handle &scope, const char *name, const Extra&... extra)
1375 : class_<Type>(scope, name, extra...), m_parent(scope) {
Wenzel Jakob405f6d12016-11-17 23:24:47 +01001376
1377 constexpr bool is_arithmetic =
1378 !std::is_same<detail::first_of_t<arithmetic_tag, void, Extra...>,
1379 void>::value;
1380
1381 auto entries = new std::unordered_map<Scalar, const char *>();
Wenzel Jakob8ac97152016-09-05 17:20:50 +09001382 def("__repr__", [name, entries](Type value) -> std::string {
Wenzel Jakob405f6d12016-11-17 23:24:47 +01001383 auto it = entries->find((Scalar) value);
Wenzel Jakob38bd7112015-07-05 20:05:44 +02001384 return std::string(name) + "." +
1385 ((it == entries->end()) ? std::string("???")
1386 : std::string(it->second));
1387 });
Wenzel Jakob405f6d12016-11-17 23:24:47 +01001388 def("__init__", [](Type& value, Scalar i) { value = (Type)i; });
1389 def("__init__", [](Type& value, Scalar i) { new (&value) Type((Type) i); });
1390 def("__int__", [](Type value) { return (Scalar) value; });
Wenzel Jakob8ac97152016-09-05 17:20:50 +09001391 def("__eq__", [](const Type &value, Type *value2) { return value2 && value == *value2; });
1392 def("__ne__", [](const Type &value, Type *value2) { return !value2 || value != *value2; });
Wenzel Jakob405f6d12016-11-17 23:24:47 +01001393 if (is_arithmetic) {
1394 def("__lt__", [](const Type &value, Type *value2) { return value2 && value < *value2; });
1395 def("__gt__", [](const Type &value, Type *value2) { return value2 && value > *value2; });
1396 def("__le__", [](const Type &value, Type *value2) { return value2 && value <= *value2; });
1397 def("__ge__", [](const Type &value, Type *value2) { return value2 && value >= *value2; });
1398 }
1399 if (std::is_convertible<Type, Scalar>::value) {
Jason Rhinelanderd41a2732016-08-04 00:21:37 -04001400 // Don't provide comparison with the underlying type if the enum isn't convertible,
1401 // i.e. if Type is a scoped enum, mirroring the C++ behaviour. (NB: we explicitly
Wenzel Jakob405f6d12016-11-17 23:24:47 +01001402 // convert Type to Scalar below anyway because this needs to compile).
1403 def("__eq__", [](const Type &value, Scalar value2) { return (Scalar) value == value2; });
1404 def("__ne__", [](const Type &value, Scalar value2) { return (Scalar) value != value2; });
1405 if (is_arithmetic) {
1406 def("__lt__", [](const Type &value, Scalar value2) { return (Scalar) value < value2; });
1407 def("__gt__", [](const Type &value, Scalar value2) { return (Scalar) value > value2; });
1408 def("__le__", [](const Type &value, Scalar value2) { return (Scalar) value <= value2; });
1409 def("__ge__", [](const Type &value, Scalar value2) { return (Scalar) value >= value2; });
1410 def("__invert__", [](const Type &value) { return ~((Scalar) value); });
1411 def("__and__", [](const Type &value, Scalar value2) { return (Scalar) value & value2; });
1412 def("__or__", [](const Type &value, Scalar value2) { return (Scalar) value | value2; });
1413 def("__xor__", [](const Type &value, Scalar value2) { return (Scalar) value ^ value2; });
1414 def("__rand__", [](const Type &value, Scalar value2) { return (Scalar) value & value2; });
1415 def("__ror__", [](const Type &value, Scalar value2) { return (Scalar) value | value2; });
1416 def("__rxor__", [](const Type &value, Scalar value2) { return (Scalar) value ^ value2; });
1417 def("__and__", [](const Type &value, const Type &value2) { return (Scalar) value & (Scalar) value2; });
1418 def("__or__", [](const Type &value, const Type &value2) { return (Scalar) value | (Scalar) value2; });
1419 def("__xor__", [](const Type &value, const Type &value2) { return (Scalar) value ^ (Scalar) value2; });
1420 }
Jason Rhinelanderd41a2732016-08-04 00:21:37 -04001421 }
Wenzel Jakob405f6d12016-11-17 23:24:47 +01001422 def("__hash__", [](const Type &value) { return (Scalar) value; });
Wenzel Jakobfe342412016-09-06 13:02:29 +09001423 // Pickling and unpickling -- needed for use with the 'multiprocessing' module
Wenzel Jakob405f6d12016-11-17 23:24:47 +01001424 def("__getstate__", [](const Type &value) { return pybind11::make_tuple((Scalar) value); });
1425 def("__setstate__", [](Type &p, tuple t) { new (&p) Type((Type) t[0].cast<Scalar>()); });
Wenzel Jakob38bd7112015-07-05 20:05:44 +02001426 m_entries = entries;
1427 }
1428
1429 /// Export enumeration entries into the parent scope
Wenzel Jakobe916d842016-11-04 16:51:14 +01001430 enum_ &export_values() {
Wenzel Jakob1d1f81b2016-12-16 15:00:46 +01001431#if !defined(PYPY_VERSION)
Wenzel Jakob38bd7112015-07-05 20:05:44 +02001432 PyObject *dict = ((PyTypeObject *) this->m_ptr)->tp_dict;
1433 PyObject *key, *value;
Wenzel Jakob27e8e102016-01-17 22:36:37 +01001434 ssize_t pos = 0;
Wenzel Jakob1d1f81b2016-12-16 15:00:46 +01001435
1436 while (PyDict_Next(dict, &pos, &key, &value)) {
Wenzel Jakob38bd7112015-07-05 20:05:44 +02001437 if (PyObject_IsInstance(value, this->m_ptr))
1438 m_parent.attr(key) = value;
Wenzel Jakob1d1f81b2016-12-16 15:00:46 +01001439 }
1440#else
1441 /* PyPy's cpyext still has difficulties with the above
1442 CPython API calls; emulate using Python code. */
1443 dict d; d["t"] = *this; d["p"] = m_parent;
1444 PyObject *result = PyRun_String(
1445 "for k, v in t.__dict__.items():\n"
1446 " if isinstance(v, t):\n"
1447 " setattr(p, k, v)\n",
1448 Py_file_input, d.ptr(), d.ptr());
1449 if (result == nullptr)
1450 throw error_already_set();
1451 Py_DECREF(result);
1452#endif
1453
Wenzel Jakobe916d842016-11-04 16:51:14 +01001454 return *this;
Wenzel Jakob38bd7112015-07-05 20:05:44 +02001455 }
1456
1457 /// Add an enumeration entry
1458 enum_& value(char const* name, Type value) {
Wenzel Jakob8f4eb002015-10-15 18:13:33 +02001459 this->attr(name) = pybind11::cast(value, return_value_policy::copy);
Wenzel Jakob405f6d12016-11-17 23:24:47 +01001460 (*m_entries)[(Scalar) value] = name;
Wenzel Jakob38bd7112015-07-05 20:05:44 +02001461 return *this;
1462 }
1463private:
Wenzel Jakob405f6d12016-11-17 23:24:47 +01001464 std::unordered_map<Scalar, const char *> *m_entries;
Wenzel Jakob48548ea2016-01-17 22:36:44 +01001465 handle m_parent;
Wenzel Jakob38bd7112015-07-05 20:05:44 +02001466};
1467
1468NAMESPACE_BEGIN(detail)
Wenzel Jakob71867832015-07-29 17:43:52 +02001469template <typename... Args> struct init {
Jason Rhinelanderec62d972016-09-09 02:42:51 -04001470 template <typename Class, typename... Extra, enable_if_t<!Class::has_alias, int> = 0>
1471 static void execute(Class &cl, const Extra&... extra) {
Jason Rhinelander5fffe202016-09-06 12:17:06 -04001472 using Base = typename Class::type;
Wenzel Jakob38bd7112015-07-05 20:05:44 +02001473 /// Function which calls a specific C++ in-place constructor
Jason Rhinelander5fffe202016-09-06 12:17:06 -04001474 cl.def("__init__", [](Base *self_, Args... args) { new (self_) Base(args...); }, extra...);
Wenzel Jakob86d825f2016-05-26 13:19:27 +02001475 }
1476
Jason Rhinelander5fffe202016-09-06 12:17:06 -04001477 template <typename Class, typename... Extra,
Jason Rhinelanderec62d972016-09-09 02:42:51 -04001478 enable_if_t<Class::has_alias &&
1479 std::is_constructible<typename Class::type, Args...>::value, int> = 0>
1480 static void execute(Class &cl, const Extra&... extra) {
Jason Rhinelander5fffe202016-09-06 12:17:06 -04001481 using Base = typename Class::type;
1482 using Alias = typename Class::type_alias;
1483 handle cl_type = cl;
1484 cl.def("__init__", [cl_type](handle self_, Args... args) {
Wenzel Jakob69e1a5c2016-05-26 14:29:31 +02001485 if (self_.get_type() == cl_type)
1486 new (self_.cast<Base *>()) Base(args...);
Wenzel Jakob86d825f2016-05-26 13:19:27 +02001487 else
Wenzel Jakob69e1a5c2016-05-26 14:29:31 +02001488 new (self_.cast<Alias *>()) Alias(args...);
Wenzel Jakob86d825f2016-05-26 13:19:27 +02001489 }, extra...);
1490 }
1491
Jason Rhinelander5fffe202016-09-06 12:17:06 -04001492 template <typename Class, typename... Extra,
Jason Rhinelanderec62d972016-09-09 02:42:51 -04001493 enable_if_t<Class::has_alias &&
1494 !std::is_constructible<typename Class::type, Args...>::value, int> = 0>
1495 static void execute(Class &cl, const Extra&... extra) {
1496 init_alias<Args...>::execute(cl, extra...);
1497 }
1498};
1499template <typename... Args> struct init_alias {
1500 template <typename Class, typename... Extra,
1501 enable_if_t<Class::has_alias && std::is_constructible<typename Class::type_alias, Args...>::value, int> = 0>
1502 static void execute(Class &cl, const Extra&... extra) {
Jason Rhinelander5fffe202016-09-06 12:17:06 -04001503 using Alias = typename Class::type_alias;
1504 cl.def("__init__", [](Alias *self_, Args... args) { new (self_) Alias(args...); }, extra...);
Wenzel Jakob38bd7112015-07-05 20:05:44 +02001505 }
1506};
Wenzel Jakob5f218b32016-01-17 22:36:39 +01001507
Jason Rhinelanderec62d972016-09-09 02:42:51 -04001508
Jason Rhinelanderf2ecd892016-08-10 12:08:04 -04001509inline void keep_alive_impl(handle nurse, handle patient) {
Wenzel Jakob5f218b32016-01-17 22:36:39 +01001510 /* Clever approach based on weak references taken from Boost.Python */
Wenzel Jakobb2c2c792016-01-17 22:36:40 +01001511 if (!nurse || !patient)
Wenzel Jakob678d7872016-01-17 22:36:41 +01001512 pybind11_fail("Could not activate keep_alive!");
Wenzel Jakob5f218b32016-01-17 22:36:39 +01001513
Ivan Smirnov984c7622016-08-29 02:38:47 +01001514 if (patient.is_none() || nurse.is_none())
Glen Walkerf45bb582016-08-16 17:50:43 +12001515 return; /* Nothing to keep alive or nothing to be kept alive by */
Wenzel Jakob9b880ba2016-04-25 03:25:13 +02001516
Wenzel Jakob5f218b32016-01-17 22:36:39 +01001517 cpp_function disable_lifesupport(
Wenzel Jakobb2c2c792016-01-17 22:36:40 +01001518 [patient](handle weakref) { patient.dec_ref(); weakref.dec_ref(); });
Wenzel Jakob5f218b32016-01-17 22:36:39 +01001519
Wenzel Jakobb2c2c792016-01-17 22:36:40 +01001520 weakref wr(nurse, disable_lifesupport);
Wenzel Jakob5f218b32016-01-17 22:36:39 +01001521
Wenzel Jakobb2c2c792016-01-17 22:36:40 +01001522 patient.inc_ref(); /* reference patient and leak the weak reference */
1523 (void) wr.release();
Wenzel Jakob5f218b32016-01-17 22:36:39 +01001524}
1525
Jason Rhinelanderbfcf9522017-01-30 13:34:38 -05001526PYBIND11_NOINLINE inline void keep_alive_impl(size_t Nurse, size_t Patient, function_call &call, handle ret) {
Jason Rhinelander2686da82017-01-21 23:42:14 -05001527 keep_alive_impl(
Jason Rhinelanderbfcf9522017-01-30 13:34:38 -05001528 Nurse == 0 ? ret : Nurse <= call.args.size() ? call.args[Nurse - 1] : handle(),
1529 Patient == 0 ? ret : Patient <= call.args.size() ? call.args[Patient - 1] : handle()
Jason Rhinelander2686da82017-01-21 23:42:14 -05001530 );
Jason Rhinelanderf2ecd892016-08-10 12:08:04 -04001531}
1532
Wenzel Jakobb212f6c2016-09-10 16:00:50 +09001533template <typename Iterator, typename Sentinel, bool KeyIterator, return_value_policy Policy>
Ivan Smirnov2b308e02016-08-24 23:29:04 +01001534struct iterator_state {
1535 Iterator it;
1536 Sentinel end;
Ivan Smirnovdaed1ab2016-06-17 22:29:39 +01001537 bool first;
1538};
Wenzel Jakobb2825952016-04-13 23:33:00 +02001539
Wenzel Jakob38bd7112015-07-05 20:05:44 +02001540NAMESPACE_END(detail)
1541
Ben Pritchard2de6e1d2016-02-18 13:20:15 -05001542template <typename... Args> detail::init<Args...> init() { return detail::init<Args...>(); }
Jason Rhinelanderec62d972016-09-09 02:42:51 -04001543template <typename... Args> detail::init_alias<Args...> init_alias() { return detail::init_alias<Args...>(); }
Wenzel Jakob38bd7112015-07-05 20:05:44 +02001544
Wenzel Jakobb212f6c2016-09-10 16:00:50 +09001545template <return_value_policy Policy = return_value_policy::reference_internal,
1546 typename Iterator,
Ivan Smirnov2b308e02016-08-24 23:29:04 +01001547 typename Sentinel,
Wenzel Jakob5dd33d82016-05-30 11:28:21 +02001548 typename ValueType = decltype(*std::declval<Iterator>()),
1549 typename... Extra>
Ivan Smirnov2b308e02016-08-24 23:29:04 +01001550iterator make_iterator(Iterator first, Sentinel last, Extra &&... extra) {
Wenzel Jakobb212f6c2016-09-10 16:00:50 +09001551 typedef detail::iterator_state<Iterator, Sentinel, false, Policy> state;
Wenzel Jakobb2825952016-04-13 23:33:00 +02001552
Wenzel Jakob8e5dceb2016-09-11 20:00:40 +09001553 if (!detail::get_type_info(typeid(state), false)) {
Wenzel Jakob146397e2016-09-06 13:06:31 +09001554 class_<state>(handle(), "iterator")
Wenzel Jakobb2825952016-04-13 23:33:00 +02001555 .def("__iter__", [](state &s) -> state& { return s; })
Wenzel Jakob5dd33d82016-05-30 11:28:21 +02001556 .def("__next__", [](state &s) -> ValueType {
Ivan Smirnovdaed1ab2016-06-17 22:29:39 +01001557 if (!s.first)
1558 ++s.it;
1559 else
1560 s.first = false;
Wenzel Jakobb2825952016-04-13 23:33:00 +02001561 if (s.it == s.end)
1562 throw stop_iteration();
Ivan Smirnovdaed1ab2016-06-17 22:29:39 +01001563 return *s.it;
Wenzel Jakobb212f6c2016-09-10 16:00:50 +09001564 }, std::forward<Extra>(extra)..., Policy);
Wenzel Jakobb2825952016-04-13 23:33:00 +02001565 }
1566
Ivan Smirnovdaed1ab2016-06-17 22:29:39 +01001567 return (iterator) cast(state { first, last, true });
Wenzel Jakobb2825952016-04-13 23:33:00 +02001568}
Wenzel Jakob146397e2016-09-06 13:06:31 +09001569
Wenzel Jakobb212f6c2016-09-10 16:00:50 +09001570template <return_value_policy Policy = return_value_policy::reference_internal,
1571 typename Iterator,
Ivan Smirnov2b308e02016-08-24 23:29:04 +01001572 typename Sentinel,
Ivan Smirnovc5a1c8a2016-08-24 23:27:19 +01001573 typename KeyType = decltype((*std::declval<Iterator>()).first),
Jason Rhinelander5aa85be2016-08-11 21:22:05 -04001574 typename... Extra>
Ivan Smirnov2b308e02016-08-24 23:29:04 +01001575iterator make_key_iterator(Iterator first, Sentinel last, Extra &&... extra) {
Wenzel Jakobb212f6c2016-09-10 16:00:50 +09001576 typedef detail::iterator_state<Iterator, Sentinel, true, Policy> state;
Jason Rhinelander5aa85be2016-08-11 21:22:05 -04001577
Wenzel Jakob8e5dceb2016-09-11 20:00:40 +09001578 if (!detail::get_type_info(typeid(state), false)) {
Wenzel Jakob146397e2016-09-06 13:06:31 +09001579 class_<state>(handle(), "iterator")
Jason Rhinelander5aa85be2016-08-11 21:22:05 -04001580 .def("__iter__", [](state &s) -> state& { return s; })
1581 .def("__next__", [](state &s) -> KeyType {
1582 if (!s.first)
1583 ++s.it;
1584 else
1585 s.first = false;
1586 if (s.it == s.end)
1587 throw stop_iteration();
Ivan Smirnovc5a1c8a2016-08-24 23:27:19 +01001588 return (*s.it).first;
Wenzel Jakobb212f6c2016-09-10 16:00:50 +09001589 }, std::forward<Extra>(extra)..., Policy);
Jason Rhinelander5aa85be2016-08-11 21:22:05 -04001590 }
1591
1592 return (iterator) cast(state { first, last, true });
1593}
Wenzel Jakobb2825952016-04-13 23:33:00 +02001594
Wenzel Jakobb212f6c2016-09-10 16:00:50 +09001595template <return_value_policy Policy = return_value_policy::reference_internal,
1596 typename Type, typename... Extra> iterator make_iterator(Type &value, Extra&&... extra) {
1597 return make_iterator<Policy>(std::begin(value), std::end(value), extra...);
Wenzel Jakobbf0c7dc2016-04-18 10:52:12 +02001598}
1599
Wenzel Jakobb212f6c2016-09-10 16:00:50 +09001600template <return_value_policy Policy = return_value_policy::reference_internal,
1601 typename Type, typename... Extra> iterator make_key_iterator(Type &value, Extra&&... extra) {
1602 return make_key_iterator<Policy>(std::begin(value), std::end(value), extra...);
Jason Rhinelander5aa85be2016-08-11 21:22:05 -04001603}
1604
Wenzel Jakob38bd7112015-07-05 20:05:44 +02001605template <typename InputType, typename OutputType> void implicitly_convertible() {
Wenzel Jakob2ac50442016-01-17 22:36:35 +01001606 auto implicit_caster = [](PyObject *obj, PyTypeObject *type) -> PyObject * {
Dean Moldovan5f07fac2017-01-03 11:52:05 +01001607 if (!detail::make_caster<InputType>().load(obj, false))
Wenzel Jakob38bd7112015-07-05 20:05:44 +02001608 return nullptr;
1609 tuple args(1);
1610 args[0] = obj;
1611 PyObject *result = PyObject_Call((PyObject *) type, args.ptr(), nullptr);
1612 if (result == nullptr)
1613 PyErr_Clear();
1614 return result;
1615 };
Ivan Smirnov8f3e0452016-10-23 15:43:03 +01001616
1617 if (auto tinfo = detail::get_type_info(typeid(OutputType)))
1618 tinfo->implicit_conversions.push_back(implicit_caster);
1619 else
Wenzel Jakob678d7872016-01-17 22:36:41 +01001620 pybind11_fail("implicitly_convertible: Unable to find type " + type_id<OutputType>());
Wenzel Jakob38bd7112015-07-05 20:05:44 +02001621}
1622
Pim Schellart5a7d17f2016-06-17 17:35:59 -04001623template <typename ExceptionTranslator>
1624void register_exception_translator(ExceptionTranslator&& translator) {
1625 detail::get_internals().registered_exception_translators.push_front(
1626 std::forward<ExceptionTranslator>(translator));
1627}
1628
1629/* Wrapper to generate a new Python exception type.
1630 *
1631 * This should only be used with PyErr_SetString for now.
1632 * It is not (yet) possible to use as a py::base.
1633 * Template type argument is reserved for future use.
1634 */
1635template <typename type>
1636class exception : public object {
1637public:
Wenzel Jakob281ccc62016-11-16 17:59:56 +01001638 exception(handle scope, const char *name, PyObject *base = PyExc_Exception) {
1639 std::string full_name = scope.attr("__name__").cast<std::string>() +
1640 std::string(".") + name;
Matthew Woehlkee15fa9f2017-02-08 17:43:08 -05001641 m_ptr = PyErr_NewException(const_cast<char *>(full_name.c_str()), base, NULL);
Wenzel Jakob281ccc62016-11-16 17:59:56 +01001642 if (hasattr(scope, name))
1643 pybind11_fail("Error during initialization: multiple incompatible "
1644 "definitions with name \"" + std::string(name) + "\"");
1645 scope.attr(name) = *this;
Pim Schellart5a7d17f2016-06-17 17:35:59 -04001646 }
Jason Rhinelanderb3794f12016-09-16 02:04:15 -04001647
1648 // Sets the current python exception to this exception object with the given message
1649 void operator()(const char *message) {
1650 PyErr_SetString(m_ptr, message);
1651 }
Pim Schellart5a7d17f2016-06-17 17:35:59 -04001652};
1653
Jason Rhinelanderb3794f12016-09-16 02:04:15 -04001654/** Registers a Python exception in `m` of the given `name` and installs an exception translator to
1655 * translate the C++ exception to the created Python exception using the exceptions what() method.
1656 * This is intended for simple exception translations; for more complex translation, register the
1657 * exception object and translator directly.
1658 */
Wenzel Jakob281ccc62016-11-16 17:59:56 +01001659template <typename CppException>
1660exception<CppException> &register_exception(handle scope,
1661 const char *name,
1662 PyObject *base = PyExc_Exception) {
1663 static exception<CppException> ex(scope, name, base);
Jason Rhinelanderb3794f12016-09-16 02:04:15 -04001664 register_exception_translator([](std::exception_ptr p) {
1665 if (!p) return;
1666 try {
1667 std::rethrow_exception(p);
Wenzel Jakob281ccc62016-11-16 17:59:56 +01001668 } catch (const CppException &e) {
Jason Rhinelanderb3794f12016-09-16 02:04:15 -04001669 ex(e.what());
1670 }
1671 });
1672 return ex;
1673}
1674
Dean Moldovan67990d92016-08-29 18:03:34 +02001675NAMESPACE_BEGIN(detail)
1676PYBIND11_NOINLINE inline void print(tuple args, dict kwargs) {
1677 auto strings = tuple(args.size());
1678 for (size_t i = 0; i < args.size(); ++i) {
Dean Moldovane18bc022016-10-25 22:12:39 +02001679 strings[i] = str(args[i]);
Dean Moldovan67990d92016-08-29 18:03:34 +02001680 }
Dean Moldovan865e4302016-09-21 01:06:32 +02001681 auto sep = kwargs.contains("sep") ? kwargs["sep"] : cast(" ");
Dean Moldovan242b1462016-09-08 17:02:04 +02001682 auto line = sep.attr("join")(strings);
Dean Moldovan67990d92016-08-29 18:03:34 +02001683
Wenzel Jakobc49d6e52016-10-13 10:34:52 +02001684 object file;
1685 if (kwargs.contains("file")) {
1686 file = kwargs["file"].cast<object>();
1687 } else {
1688 try {
1689 file = module::import("sys").attr("stdout");
esquires67a68f12016-12-01 05:35:34 -05001690 } catch (const error_already_set &) {
Wenzel Jakobc49d6e52016-10-13 10:34:52 +02001691 /* If print() is called from code that is executed as
1692 part of garbage collection during interpreter shutdown,
1693 importing 'sys' can fail. Give up rather than crashing the
1694 interpreter in this case. */
1695 return;
1696 }
1697 }
1698
Dean Moldovan242b1462016-09-08 17:02:04 +02001699 auto write = file.attr("write");
Dean Moldovan67990d92016-08-29 18:03:34 +02001700 write(line);
Dean Moldovan865e4302016-09-21 01:06:32 +02001701 write(kwargs.contains("end") ? kwargs["end"] : cast("\n"));
Dean Moldovan67990d92016-08-29 18:03:34 +02001702
Wenzel Jakobc49d6e52016-10-13 10:34:52 +02001703 if (kwargs.contains("flush") && kwargs["flush"].cast<bool>())
Dean Moldovan242b1462016-09-08 17:02:04 +02001704 file.attr("flush")();
Dean Moldovan67990d92016-08-29 18:03:34 +02001705}
1706NAMESPACE_END(detail)
1707
1708template <return_value_policy policy = return_value_policy::automatic_reference, typename... Args>
1709void print(Args &&...args) {
1710 auto c = detail::collect_arguments<policy>(std::forward<Args>(args)...);
1711 detail::print(c.args(), c.kwargs());
1712}
1713
Wenzel Jakob1d1f81b2016-12-16 15:00:46 +01001714#if defined(WITH_THREAD) && !defined(PYPY_VERSION)
Wenzel Jakob39e97e62016-04-25 03:26:15 +02001715
1716/* The functions below essentially reproduce the PyGILState_* API using a RAII
1717 * pattern, but there are a few important differences:
1718 *
1719 * 1. When acquiring the GIL from an non-main thread during the finalization
1720 * phase, the GILState API blindly terminates the calling thread, which
1721 * is often not what is wanted. This API does not do this.
1722 *
1723 * 2. The gil_scoped_release function can optionally cut the relationship
1724 * of a PyThreadState and its associated thread, which allows moving it to
1725 * another thread (this is a fairly rare/advanced use case).
1726 *
1727 * 3. The reference count of an acquired thread state can be controlled. This
1728 * can be handy to prevent cases where callbacks issued from an external
Wenzel Jakobfbafdea2016-04-25 15:02:43 +02001729 * thread would otherwise constantly construct and destroy thread state data
1730 * structures.
Wenzel Jakob69e1a5c2016-05-26 14:29:31 +02001731 *
1732 * See the Python bindings of NanoGUI (http://github.com/wjakob/nanogui) for an
1733 * example which uses features 2 and 3 to migrate the Python thread of
1734 * execution to another thread (to run the event loop on the original thread,
1735 * in this case).
Wenzel Jakobfbafdea2016-04-25 15:02:43 +02001736 */
Wenzel Jakob38bd7112015-07-05 20:05:44 +02001737
1738class gil_scoped_acquire {
Wenzel Jakob38bd7112015-07-05 20:05:44 +02001739public:
Wenzel Jakobfbafdea2016-04-25 15:02:43 +02001740 PYBIND11_NOINLINE gil_scoped_acquire() {
Wenzel Jakob39e97e62016-04-25 03:26:15 +02001741 auto const &internals = detail::get_internals();
1742 tstate = (PyThreadState *) PyThread_get_key_value(internals.tstate);
1743
1744 if (!tstate) {
1745 tstate = PyThreadState_New(internals.istate);
1746 #if !defined(NDEBUG)
1747 if (!tstate)
1748 pybind11_fail("scoped_acquire: could not create thread state!");
1749 #endif
1750 tstate->gilstate_counter = 0;
Wenzel Jakobfbafdea2016-04-25 15:02:43 +02001751 #if PY_MAJOR_VERSION < 3
1752 PyThread_delete_key_value(internals.tstate);
1753 #endif
Wenzel Jakob39e97e62016-04-25 03:26:15 +02001754 PyThread_set_key_value(internals.tstate, tstate);
1755 } else {
Wenzel Jakobfbafdea2016-04-25 15:02:43 +02001756 release = detail::get_thread_state_unchecked() != tstate;
Wenzel Jakob39e97e62016-04-25 03:26:15 +02001757 }
1758
1759 if (release) {
Wenzel Jakob39e97e62016-04-25 03:26:15 +02001760 /* Work around an annoying assertion in PyThreadState_Swap */
Wenzel Jakobfbafdea2016-04-25 15:02:43 +02001761 #if defined(Py_DEBUG)
1762 PyInterpreterState *interp = tstate->interp;
1763 tstate->interp = nullptr;
1764 #endif
Wenzel Jakob39e97e62016-04-25 03:26:15 +02001765 PyEval_AcquireThread(tstate);
Wenzel Jakobfbafdea2016-04-25 15:02:43 +02001766 #if defined(Py_DEBUG)
1767 tstate->interp = interp;
1768 #endif
Wenzel Jakob39e97e62016-04-25 03:26:15 +02001769 }
1770
1771 inc_ref();
1772 }
1773
1774 void inc_ref() {
1775 ++tstate->gilstate_counter;
1776 }
1777
Wenzel Jakobfbafdea2016-04-25 15:02:43 +02001778 PYBIND11_NOINLINE void dec_ref() {
Wenzel Jakob39e97e62016-04-25 03:26:15 +02001779 --tstate->gilstate_counter;
1780 #if !defined(NDEBUG)
Wenzel Jakobfbafdea2016-04-25 15:02:43 +02001781 if (detail::get_thread_state_unchecked() != tstate)
Wenzel Jakob39e97e62016-04-25 03:26:15 +02001782 pybind11_fail("scoped_acquire::dec_ref(): thread state must be current!");
1783 if (tstate->gilstate_counter < 0)
1784 pybind11_fail("scoped_acquire::dec_ref(): reference count underflow!");
1785 #endif
1786 if (tstate->gilstate_counter == 0) {
1787 #if !defined(NDEBUG)
1788 if (!release)
1789 pybind11_fail("scoped_acquire::dec_ref(): internal error!");
1790 #endif
1791 PyThreadState_Clear(tstate);
1792 PyThreadState_DeleteCurrent();
Wenzel Jakobfbafdea2016-04-25 15:02:43 +02001793 PyThread_delete_key_value(detail::get_internals().tstate);
Wenzel Jakob39e97e62016-04-25 03:26:15 +02001794 release = false;
1795 }
1796 }
1797
Wenzel Jakobfbafdea2016-04-25 15:02:43 +02001798 PYBIND11_NOINLINE ~gil_scoped_acquire() {
Wenzel Jakob39e97e62016-04-25 03:26:15 +02001799 dec_ref();
1800 if (release)
1801 PyEval_SaveThread();
1802 }
1803private:
1804 PyThreadState *tstate = nullptr;
1805 bool release = true;
Wenzel Jakob38bd7112015-07-05 20:05:44 +02001806};
1807
1808class gil_scoped_release {
Wenzel Jakob38bd7112015-07-05 20:05:44 +02001809public:
Jason Rhinelander12d76602016-10-16 16:27:42 -04001810 explicit gil_scoped_release(bool disassoc = false) : disassoc(disassoc) {
Wenzel Jakob39e97e62016-04-25 03:26:15 +02001811 tstate = PyEval_SaveThread();
Wenzel Jakob2f6662e2016-04-25 09:16:41 +02001812 if (disassoc) {
Boris Schäling20ee9352016-05-28 12:26:18 +02001813 auto key = detail::get_internals().tstate;
Wenzel Jakob2f6662e2016-04-25 09:16:41 +02001814 #if PY_MAJOR_VERSION < 3
1815 PyThread_delete_key_value(key);
1816 #else
1817 PyThread_set_key_value(key, nullptr);
1818 #endif
1819 }
Wenzel Jakob39e97e62016-04-25 03:26:15 +02001820 }
1821 ~gil_scoped_release() {
1822 if (!tstate)
1823 return;
1824 PyEval_RestoreThread(tstate);
Wenzel Jakob2f6662e2016-04-25 09:16:41 +02001825 if (disassoc) {
Boris Schäling20ee9352016-05-28 12:26:18 +02001826 auto key = detail::get_internals().tstate;
Wenzel Jakob2f6662e2016-04-25 09:16:41 +02001827 #if PY_MAJOR_VERSION < 3
1828 PyThread_delete_key_value(key);
1829 #endif
1830 PyThread_set_key_value(key, tstate);
1831 }
Wenzel Jakob39e97e62016-04-25 03:26:15 +02001832 }
1833private:
1834 PyThreadState *tstate;
1835 bool disassoc;
Wenzel Jakob38bd7112015-07-05 20:05:44 +02001836};
Wenzel Jakob1d1f81b2016-12-16 15:00:46 +01001837#elif defined(PYPY_VERSION)
1838class gil_scoped_acquire {
1839 PyGILState_STATE state;
1840public:
1841 gil_scoped_acquire() { state = PyGILState_Ensure(); }
1842 ~gil_scoped_acquire() { PyGILState_Release(state); }
1843};
1844
1845class gil_scoped_release {
1846 PyThreadState *state;
1847public:
1848 gil_scoped_release() { state = PyEval_SaveThread(); }
1849 ~gil_scoped_release() { PyEval_RestoreThread(state); }
1850};
Wenzel Jakob39e97e62016-04-25 03:26:15 +02001851#else
1852class gil_scoped_acquire { };
1853class gil_scoped_release { };
Felipe Lema2547ca42016-01-25 16:22:44 -03001854#endif
Wenzel Jakob38bd7112015-07-05 20:05:44 +02001855
Wenzel Jakob8d396fc2016-11-24 23:11:02 +01001856error_already_set::~error_already_set() {
1857 if (value) {
1858 gil_scoped_acquire gil;
Dean Moldovand534bd62017-02-08 20:23:56 +01001859 clear();
Wenzel Jakob8d396fc2016-11-24 23:11:02 +01001860 }
1861}
1862
Jason Rhinelander1b05ce52016-08-09 17:57:59 -04001863inline function get_type_overload(const void *this_ptr, const detail::type_info *this_type, const char *name) {
Wenzel Jakob1d1f81b2016-12-16 15:00:46 +01001864 handle self = detail::get_object_handle(this_ptr, this_type);
1865 if (!self)
Wenzel Jakobac0fde92015-10-01 18:37:26 +02001866 return function();
Wenzel Jakob1d1f81b2016-12-16 15:00:46 +01001867 handle type = self.get_type();
Wenzel Jakoba2f6fde2015-10-01 16:46:03 +02001868 auto key = std::make_pair(type.ptr(), name);
1869
Wenzel Jakobf5c154a2016-04-11 18:13:08 +02001870 /* Cache functions that aren't overloaded in Python to avoid
1871 many costly Python dictionary lookups below */
Wenzel Jakoba2f6fde2015-10-01 16:46:03 +02001872 auto &cache = detail::get_internals().inactive_overload_cache;
1873 if (cache.find(key) != cache.end())
1874 return function();
1875
Wenzel Jakob1d1f81b2016-12-16 15:00:46 +01001876 function overload = getattr(self, name, function());
Wenzel Jakoba2f6fde2015-10-01 16:46:03 +02001877 if (overload.is_cpp_function()) {
1878 cache.insert(key);
1879 return function();
1880 }
Wenzel Jakob27e8e102016-01-17 22:36:37 +01001881
Wenzel Jakob1d1f81b2016-12-16 15:00:46 +01001882 /* Don't call dispatch code if invoked from overridden function.
1883 Unfortunately this doesn't work on PyPy. */
1884#if !defined(PYPY_VERSION)
Wenzel Jakoba2f6fde2015-10-01 16:46:03 +02001885 PyFrameObject *frame = PyThreadState_Get()->frame;
Dean Moldovane18bc022016-10-25 22:12:39 +02001886 if (frame && (std::string) str(frame->f_code->co_name) == name &&
Wenzel Jakobf5c154a2016-04-11 18:13:08 +02001887 frame->f_code->co_argcount > 0) {
1888 PyFrame_FastToLocals(frame);
1889 PyObject *self_caller = PyDict_GetItem(
1890 frame->f_locals, PyTuple_GET_ITEM(frame->f_code->co_varnames, 0));
Wenzel Jakob1d1f81b2016-12-16 15:00:46 +01001891 if (self_caller == self.ptr())
Wenzel Jakobf5c154a2016-04-11 18:13:08 +02001892 return function();
1893 }
Wenzel Jakob1d1f81b2016-12-16 15:00:46 +01001894#else
1895 /* PyPy currently doesn't provide a detailed cpyext emulation of
1896 frame objects, so we have to emulate this using Python. This
1897 is going to be slow..*/
1898 dict d; d["self"] = self; d["name"] = pybind11::str(name);
1899 PyObject *result = PyRun_String(
1900 "import inspect\n"
1901 "frame = inspect.currentframe()\n"
1902 "if frame is not None:\n"
1903 " frame = frame.f_back\n"
1904 " if frame is not None and str(frame.f_code.co_name) == name and "
1905 "frame.f_code.co_argcount > 0:\n"
1906 " self_caller = frame.f_locals[frame.f_code.co_varnames[0]]\n"
1907 " if self_caller == self:\n"
1908 " self = None\n",
1909 Py_file_input, d.ptr(), d.ptr());
1910 if (result == nullptr)
1911 throw error_already_set();
1912 if ((handle) d["self"] == Py_None)
1913 return function();
1914 Py_DECREF(result);
1915#endif
1916
Wenzel Jakoba2f6fde2015-10-01 16:46:03 +02001917 return overload;
1918}
1919
Jason Rhinelander1b05ce52016-08-09 17:57:59 -04001920template <class T> function get_overload(const T *this_ptr, const char *name) {
Ivan Smirnov8f3e0452016-10-23 15:43:03 +01001921 auto tinfo = detail::get_type_info(typeid(T));
1922 return tinfo ? get_type_overload(this_ptr, tinfo, name) : function();
Jason Rhinelander1b05ce52016-08-09 17:57:59 -04001923}
1924
Jason Rhinelander20978262016-08-29 18:16:46 -04001925#define PYBIND11_OVERLOAD_INT(ret_type, cname, name, ...) { \
Wenzel Jakob8f4eb002015-10-15 18:13:33 +02001926 pybind11::gil_scoped_acquire gil; \
Jason Rhinelander20978262016-08-29 18:16:46 -04001927 pybind11::function overload = pybind11::get_overload(static_cast<const cname *>(this), name); \
Jason Rhinelander7dfb9322016-09-08 14:49:43 -04001928 if (overload) { \
Jason Rhinelander3e4fe6c2016-09-11 12:17:41 -04001929 auto o = overload(__VA_ARGS__); \
Jason Rhinelander7dfb9322016-09-08 14:49:43 -04001930 if (pybind11::detail::cast_is_temporary_value_reference<ret_type>::value) { \
Jason Rhinelander3e4fe6c2016-09-11 12:17:41 -04001931 static pybind11::detail::overload_caster_t<ret_type> caster; \
1932 return pybind11::detail::cast_ref<ret_type>(std::move(o), caster); \
Jason Rhinelander7dfb9322016-09-08 14:49:43 -04001933 } \
1934 else return pybind11::detail::cast_safe<ret_type>(std::move(o)); \
1935 } \
1936 }
Wenzel Jakoba2f6fde2015-10-01 16:46:03 +02001937
Wenzel Jakob1e3be732016-05-24 23:42:05 +02001938#define PYBIND11_OVERLOAD_NAME(ret_type, cname, name, fn, ...) \
Jason Rhinelander20978262016-08-29 18:16:46 -04001939 PYBIND11_OVERLOAD_INT(ret_type, cname, name, __VA_ARGS__) \
Wenzel Jakob1e3be732016-05-24 23:42:05 +02001940 return cname::fn(__VA_ARGS__)
Wenzel Jakoba2f6fde2015-10-01 16:46:03 +02001941
Wenzel Jakob1e3be732016-05-24 23:42:05 +02001942#define PYBIND11_OVERLOAD_PURE_NAME(ret_type, cname, name, fn, ...) \
Jason Rhinelander20978262016-08-29 18:16:46 -04001943 PYBIND11_OVERLOAD_INT(ret_type, cname, name, __VA_ARGS__) \
Wenzel Jakob1e3be732016-05-24 23:42:05 +02001944 pybind11::pybind11_fail("Tried to call pure virtual function \"" #cname "::" name "\"");
1945
1946#define PYBIND11_OVERLOAD(ret_type, cname, fn, ...) \
1947 PYBIND11_OVERLOAD_NAME(ret_type, cname, #fn, fn, __VA_ARGS__)
1948
1949#define PYBIND11_OVERLOAD_PURE(ret_type, cname, fn, ...) \
1950 PYBIND11_OVERLOAD_PURE_NAME(ret_type, cname, #fn, fn, __VA_ARGS__)
Wenzel Jakoba2f6fde2015-10-01 16:46:03 +02001951
Wenzel Jakob8f4eb002015-10-15 18:13:33 +02001952NAMESPACE_END(pybind11)
Wenzel Jakob38bd7112015-07-05 20:05:44 +02001953
1954#if defined(_MSC_VER)
Wenzel Jakob48548ea2016-01-17 22:36:44 +01001955# pragma warning(pop)
Wenzel Jakob1ffce742016-08-25 01:43:33 +02001956#elif defined(__INTEL_COMPILER)
1957/* Leave ignored warnings on */
Wenzel Jakob6d252962016-05-01 20:47:49 +02001958#elif defined(__GNUG__) && !defined(__clang__)
Wenzel Jakob48548ea2016-01-17 22:36:44 +01001959# pragma GCC diagnostic pop
Wenzel Jakob38bd7112015-07-05 20:05:44 +02001960#endif