blob: afa91accd02b8c31bf22e3a2035d888eed568c3d [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)
Dean Moldovan706a7d92017-07-04 01:27:18 +020024# pragma warning(disable: 68) // integer conversion resulted in a change of sign
Wenzel Jakob1ffce742016-08-25 01:43:33 +020025# pragma warning(disable: 186) // pointless comparison of unsigned integer with zero
Dean Moldovan706a7d92017-07-04 01:27:18 +020026# pragma warning(disable: 878) // incompatible exception specifications
Wenzel Jakob1ffce742016-08-25 01:43:33 +020027# pragma warning(disable: 1334) // the "template" keyword used for syntactic disambiguation may only be used within a template
Dean Moldovan706a7d92017-07-04 01:27:18 +020028# pragma warning(disable: 1682) // implicit conversion of a 64-bit integral type to a smaller integral type (potential portability problem)
29# pragma warning(disable: 1875) // offsetof applied to non-POD (Plain Old Data) types is nonstandard
Wenzel Jakob1ffce742016-08-25 01:43:33 +020030# pragma warning(disable: 2196) // warning #2196: routine is both "inline" and "noinline"
Wenzel Jakob6d252962016-05-01 20:47:49 +020031#elif defined(__GNUG__) && !defined(__clang__)
Wenzel Jakob48548ea2016-01-17 22:36:44 +010032# pragma GCC diagnostic push
33# pragma GCC diagnostic ignored "-Wunused-but-set-parameter"
34# pragma GCC diagnostic ignored "-Wunused-but-set-variable"
35# pragma GCC diagnostic ignored "-Wmissing-field-initializers"
Wenzel Jakobdeeab552016-05-15 23:54:34 +020036# pragma GCC diagnostic ignored "-Wstrict-aliasing"
37# pragma GCC diagnostic ignored "-Wattributes"
Jason Rhinelanderd15b2172017-05-09 14:46:15 -040038# if __GNUC__ >= 7
39# pragma GCC diagnostic ignored "-Wnoexcept-type"
40# endif
Wenzel Jakob38bd7112015-07-05 20:05:44 +020041#endif
42
Wenzel Jakob48548ea2016-01-17 22:36:44 +010043#include "attr.h"
Alexander Stukowski9a110e62016-11-15 12:38:05 +010044#include "options.h"
Dean Moldovanf5806492017-08-14 00:35:53 +020045#include "detail/class.h"
Jason Rhinelander464d9892017-06-12 21:52:48 -040046#include "detail/init.h"
Wenzel Jakob38bd7112015-07-05 20:05:44 +020047
Jason Rhinelandera859dd62017-08-10 12:03:29 -040048NAMESPACE_BEGIN(PYBIND11_NAMESPACE)
Wenzel Jakob38bd7112015-07-05 20:05:44 +020049
Wenzel Jakobd4258ba2015-07-26 16:33:49 +020050/// Wraps an arbitrary C++ function/method/lambda function/.. into a callable Python object
Wenzel Jakobbd4a5292015-07-11 17:41:48 +020051class cpp_function : public function {
Wenzel Jakobd4258ba2015-07-26 16:33:49 +020052public:
Wenzel Jakobbd4a5292015-07-11 17:41:48 +020053 cpp_function() { }
Wenzel Jakob38bd7112015-07-05 20:05:44 +020054
Wenzel Jakob5984baa2016-05-10 15:05:03 +010055 /// Construct a cpp_function from a vanilla function pointer
Jason Rhinelander1d7998e2017-02-17 06:56:41 -050056 template <typename Return, typename... Args, typename... Extra>
57 cpp_function(Return (*f)(Args...), const Extra&... extra) {
Wenzel Jakob5984baa2016-05-10 15:05:03 +010058 initialize(f, f, extra...);
Wenzel Jakob38bd7112015-07-05 20:05:44 +020059 }
Wenzel Jakobd4258ba2015-07-26 16:33:49 +020060
Wenzel Jakob5984baa2016-05-10 15:05:03 +010061 /// Construct a cpp_function from a lambda function (possibly with internal state)
Jason Rhinelanderb4bf5ed2017-05-10 01:51:08 -040062 template <typename Func, typename... Extra,
63 typename = detail::enable_if_t<detail::is_lambda<Func>::value>>
Jason Rhinelander1d7998e2017-02-17 06:56:41 -050064 cpp_function(Func &&f, const Extra&... extra) {
Wenzel Jakob71867832015-07-29 17:43:52 +020065 initialize(std::forward<Func>(f),
Jason Rhinelanderb4bf5ed2017-05-10 01:51:08 -040066 (detail::function_signature_t<Func> *) nullptr, extra...);
Wenzel Jakobd4258ba2015-07-26 16:33:49 +020067 }
68
Wenzel Jakob5984baa2016-05-10 15:05:03 +010069 /// Construct a cpp_function from a class method (non-const)
Jason Rhinelander1d7998e2017-02-17 06:56:41 -050070 template <typename Return, typename Class, typename... Arg, typename... Extra>
71 cpp_function(Return (Class::*f)(Arg...), const Extra&... extra) {
Wenzel Jakob71867832015-07-29 17:43:52 +020072 initialize([f](Class *c, Arg... args) -> Return { return (c->*f)(args...); },
Jason Rhinelander1d7998e2017-02-17 06:56:41 -050073 (Return (*) (Class *, Arg...)) nullptr, extra...);
Wenzel Jakobd4258ba2015-07-26 16:33:49 +020074 }
75
Wenzel Jakob5984baa2016-05-10 15:05:03 +010076 /// Construct a cpp_function from a class method (const)
Jason Rhinelander1d7998e2017-02-17 06:56:41 -050077 template <typename Return, typename Class, typename... Arg, typename... Extra>
78 cpp_function(Return (Class::*f)(Arg...) const, const Extra&... extra) {
Wenzel Jakob71867832015-07-29 17:43:52 +020079 initialize([f](const Class *c, Arg... args) -> Return { return (c->*f)(args...); },
Jason Rhinelander1d7998e2017-02-17 06:56:41 -050080 (Return (*)(const Class *, Arg ...)) nullptr, extra...);
Wenzel Jakobd4258ba2015-07-26 16:33:49 +020081 }
82
Wenzel Jakob57082212015-09-04 23:42:12 +020083 /// Return the function name
Wenzel Jakob48548ea2016-01-17 22:36:44 +010084 object name() const { return attr("__name__"); }
Wenzel Jakob57082212015-09-04 23:42:12 +020085
Wenzel Jakobd561cb02016-01-17 22:36:41 +010086protected:
Wenzel Jakob382484a2016-09-10 15:28:37 +090087 /// Space optimization: don't inline this frequently instantiated fragment
88 PYBIND11_NOINLINE detail::function_record *make_function_record() {
89 return new detail::function_record();
90 }
91
Wenzel Jakobd561cb02016-01-17 22:36:41 +010092 /// Special internal constructor for functors, lambda functions, etc.
Jason Rhinelander1d7998e2017-02-17 06:56:41 -050093 template <typename Func, typename Return, typename... Args, typename... Extra>
94 void initialize(Func &&f, Return (*)(Args...), const Extra&... extra) {
Dean Moldovan9e625582016-06-04 00:27:32 +020095
Jason Rhinelander129a7252017-03-26 00:01:52 -030096 struct capture { detail::remove_reference_t<Func> f; };
Wenzel Jakobd4258ba2015-07-26 16:33:49 +020097
Wenzel Jakobd561cb02016-01-17 22:36:41 +010098 /* Store the function including any extra state it might have (e.g. a lambda capture object) */
Wenzel Jakob382484a2016-09-10 15:28:37 +090099 auto rec = make_function_record();
Wenzel Jakob71867832015-07-29 17:43:52 +0200100
Wenzel Jakob5984baa2016-05-10 15:05:03 +0100101 /* Store the capture object directly in the function record if there is enough space */
102 if (sizeof(capture) <= sizeof(rec->data)) {
Wenzel Jakob954b7932016-07-10 10:13:18 +0200103 /* Without these pragmas, GCC warns that there might not be
104 enough space to use the placement new operator. However, the
105 'if' statement above ensures that this is the case. */
Jason Rhinelander0b12f912016-07-07 16:26:04 -0400106#if defined(__GNUG__) && !defined(__clang__) && __GNUC__ >= 6
Jason Rhinelandercae0e002016-07-07 16:11:42 -0400107# pragma GCC diagnostic push
108# pragma GCC diagnostic ignored "-Wplacement-new"
109#endif
Wenzel Jakob5984baa2016-05-10 15:05:03 +0100110 new ((capture *) &rec->data) capture { std::forward<Func>(f) };
Jason Rhinelander0b12f912016-07-07 16:26:04 -0400111#if defined(__GNUG__) && !defined(__clang__) && __GNUC__ >= 6
Jason Rhinelandercae0e002016-07-07 16:11:42 -0400112# pragma GCC diagnostic pop
113#endif
Wenzel Jakob5984baa2016-05-10 15:05:03 +0100114 if (!std::is_trivially_destructible<Func>::value)
115 rec->free_data = [](detail::function_record *r) { ((capture *) &r->data)->~capture(); };
116 } else {
117 rec->data[0] = new capture { std::forward<Func>(f) };
118 rec->free_data = [](detail::function_record *r) { delete ((capture *) r->data[0]); };
119 }
Wenzel Jakob19208fe2015-10-13 17:37:25 +0200120
Wenzel Jakob5984baa2016-05-10 15:05:03 +0100121 /* Type casters for the function arguments and return value */
Dean Moldovan719c1732016-11-27 18:19:34 +0100122 using cast_in = detail::argument_loader<Args...>;
123 using cast_out = detail::make_caster<
124 detail::conditional_t<std::is_void<Return>::value, detail::void_type, Return>
125 >;
Wenzel Jakob71867832015-07-29 17:43:52 +0200126
Jason Rhinelander2686da82017-01-21 23:42:14 -0500127 static_assert(detail::expected_num_args<Extra...>(sizeof...(Args), cast_in::has_args, cast_in::has_kwargs),
Jason Rhinelander0f5ec0a2017-03-19 12:36:18 -0300128 "The number of argument annotations does not match the number of function arguments");
Jason Rhinelander2686da82017-01-21 23:42:14 -0500129
Wenzel Jakobd561cb02016-01-17 22:36:41 +0100130 /* Dispatch code which converts function arguments and performs the actual function call */
Jason Rhinelanderbfcf9522017-01-30 13:34:38 -0500131 rec->impl = [](detail::function_call &call) -> handle {
Wenzel Jakob5984baa2016-05-10 15:05:03 +0100132 cast_in args_converter;
Wenzel Jakobd561cb02016-01-17 22:36:41 +0100133
134 /* Try to cast the function arguments into the C++ domain */
Jason Rhinelanderabc29ca2017-01-23 03:50:00 -0500135 if (!args_converter.load_args(call))
Wenzel Jakobd561cb02016-01-17 22:36:41 +0100136 return PYBIND11_TRY_NEXT_OVERLOAD;
137
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100138 /* Invoke call policy pre-call hook */
Jason Rhinelanderbfcf9522017-01-30 13:34:38 -0500139 detail::process_attributes<Extra...>::precall(call);
Wenzel Jakobd561cb02016-01-17 22:36:41 +0100140
Wenzel Jakob5984baa2016-05-10 15:05:03 +0100141 /* Get a pointer to the capture object */
Matthew Woehlkee15fa9f2017-02-08 17:43:08 -0500142 auto data = (sizeof(capture) <= sizeof(call.func.data)
143 ? &call.func.data : call.func.data[0]);
144 capture *cap = const_cast<capture *>(reinterpret_cast<const capture *>(data));
Wenzel Jakob5984baa2016-05-10 15:05:03 +0100145
Jason Rhinelander546f6fc2017-01-20 00:59:26 -0500146 /* Override policy for rvalues -- usually to enforce rvp::move on an rvalue */
147 const auto policy = detail::return_value_policy_override<Return>::policy(call.func.policy);
Dean Moldovand079f412016-11-20 05:31:02 +0100148
Dean Moldovan1ac19032017-03-16 11:22:26 +0100149 /* Function scope guard -- defaults to the compile-to-nothing `void_type` */
150 using Guard = detail::extract_guard_t<Extra...>;
151
Wenzel Jakob954b7932016-07-10 10:13:18 +0200152 /* Perform the function call */
Jason Rhinelander813d7e82017-05-14 15:57:26 -0400153 handle result = cast_out::cast(
154 std::move(args_converter).template call<Return, Guard>(cap->f), policy, call.parent);
Wenzel Jakobd561cb02016-01-17 22:36:41 +0100155
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100156 /* Invoke call policy post-call hook */
Jason Rhinelanderbfcf9522017-01-30 13:34:38 -0500157 detail::process_attributes<Extra...>::postcall(call, result);
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100158
Wenzel Jakob5f218b32016-01-17 22:36:39 +0100159 return result;
Wenzel Jakob71867832015-07-29 17:43:52 +0200160 };
161
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100162 /* Process any user-provided function attributes */
163 detail::process_attributes<Extra...>::init(extra..., rec);
Wenzel Jakobd561cb02016-01-17 22:36:41 +0100164
165 /* Generate a readable signature describing the function's arguments and return value types */
Dean Moldovaned23dda2016-08-04 01:40:40 +0200166 using detail::descr; using detail::_;
Dean Moldovan719c1732016-11-27 18:19:34 +0100167 PYBIND11_DESCR signature = _("(") + cast_in::arg_names() + _(") -> ") + cast_out::name();
Wenzel Jakobd561cb02016-01-17 22:36:41 +0100168
169 /* Register the function with Python from generic (non-templated) code */
Wenzel Jakob5984baa2016-05-10 15:05:03 +0100170 initialize_generic(rec, signature.text(), signature.types(), sizeof...(Args));
Wenzel Jakob178c8a82016-05-10 15:59:01 +0100171
172 if (cast_in::has_args) rec->has_args = true;
173 if (cast_in::has_kwargs) rec->has_kwargs = true;
Wenzel Jakob954b7932016-07-10 10:13:18 +0200174
175 /* Stash some additional information used by an important optimization in 'functional.h' */
Jason Rhinelander1d7998e2017-02-17 06:56:41 -0500176 using FunctionType = Return (*)(Args...);
Wenzel Jakob954b7932016-07-10 10:13:18 +0200177 constexpr bool is_function_ptr =
178 std::is_convertible<Func, FunctionType>::value &&
179 sizeof(capture) == sizeof(void *);
180 if (is_function_ptr) {
181 rec->is_stateless = true;
Matthew Woehlkee15fa9f2017-02-08 17:43:08 -0500182 rec->data[1] = const_cast<void *>(reinterpret_cast<const void *>(&typeid(FunctionType)));
Wenzel Jakob954b7932016-07-10 10:13:18 +0200183 }
Wenzel Jakob281aa0e2015-07-30 15:29:00 +0200184 }
185
Wenzel Jakobd561cb02016-01-17 22:36:41 +0100186 /// Register a function call with Python (generic non-templated code goes here)
Wenzel Jakob5984baa2016-05-10 15:05:03 +0100187 void initialize_generic(detail::function_record *rec, const char *text,
Dean Moldovanecced6c2016-07-31 20:03:18 +0200188 const std::type_info *const *types, size_t args) {
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100189
Wenzel Jakob66c9a402016-01-17 22:36:36 +0100190 /* Create copies of all referenced C-style strings */
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100191 rec->name = strdup(rec->name ? rec->name : "");
192 if (rec->doc) rec->doc = strdup(rec->doc);
193 for (auto &a: rec->args) {
Wenzel Jakob66c9a402016-01-17 22:36:36 +0100194 if (a.name)
195 a.name = strdup(a.name);
196 if (a.descr)
197 a.descr = strdup(a.descr);
198 else if (a.value)
Dean Moldovan242b1462016-09-08 17:02:04 +0200199 a.descr = strdup(a.value.attr("__repr__")().cast<std::string>().c_str());
Wenzel Jakob66c9a402016-01-17 22:36:36 +0100200 }
Wenzel Jakob954b7932016-07-10 10:13:18 +0200201
Jason Rhinelander464d9892017-06-12 21:52:48 -0400202 rec->is_constructor = !strcmp(rec->name, "__init__") || !strcmp(rec->name, "__setstate__");
203
Wenzel Jakob66c9a402016-01-17 22:36:36 +0100204 /* Generate a proper function signature */
205 std::string signature;
206 size_t type_depth = 0, char_index = 0, type_index = 0, arg_index = 0;
207 while (true) {
208 char c = text[char_index++];
209 if (c == '\0')
210 break;
211
212 if (c == '{') {
Dean Moldovanecced6c2016-07-31 20:03:18 +0200213 // Write arg name for everything except *args, **kwargs and return type.
Dean Moldovaned23dda2016-08-04 01:40:40 +0200214 if (type_depth == 0 && text[char_index] != '*' && arg_index < args) {
Jason Rhinelanderabc29ca2017-01-23 03:50:00 -0500215 if (!rec->args.empty() && rec->args[arg_index].name) {
Dean Moldovanecced6c2016-07-31 20:03:18 +0200216 signature += rec->args[arg_index].name;
Wenzel Jakob31fbf182016-11-20 05:27:05 +0100217 } else if (arg_index == 0 && rec->is_method) {
Dean Moldovanecced6c2016-07-31 20:03:18 +0200218 signature += "self";
219 } else {
Wenzel Jakob31fbf182016-11-20 05:27:05 +0100220 signature += "arg" + std::to_string(arg_index - (rec->is_method ? 1 : 0));
Dean Moldovanecced6c2016-07-31 20:03:18 +0200221 }
222 signature += ": ";
Wenzel Jakob66c9a402016-01-17 22:36:36 +0100223 }
224 ++type_depth;
225 } else if (c == '}') {
226 --type_depth;
Dean Moldovaned23dda2016-08-04 01:40:40 +0200227 if (type_depth == 0) {
Dean Moldovanecced6c2016-07-31 20:03:18 +0200228 if (arg_index < rec->args.size() && rec->args[arg_index].descr) {
229 signature += "=";
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100230 signature += rec->args[arg_index].descr;
Wenzel Jakob66c9a402016-01-17 22:36:36 +0100231 }
232 arg_index++;
233 }
234 } else if (c == '%') {
235 const std::type_info *t = types[type_index++];
Wenzel Jakobb2c2c792016-01-17 22:36:40 +0100236 if (!t)
Wenzel Jakob678d7872016-01-17 22:36:41 +0100237 pybind11_fail("Internal error while parsing type signature (1)");
Ivan Smirnov8f3e0452016-10-23 15:43:03 +0100238 if (auto tinfo = detail::get_type_info(*t)) {
Wenzel Jakob1d1f81b2016-12-16 15:00:46 +0100239#if defined(PYPY_VERSION)
240 signature += handle((PyObject *) tinfo->type)
241 .attr("__module__")
242 .cast<std::string>() + ".";
243#endif
Ivan Smirnov8f3e0452016-10-23 15:43:03 +0100244 signature += tinfo->type->tp_name;
Jason Rhinelander464d9892017-06-12 21:52:48 -0400245 } else if (rec->is_constructor && arg_index == 0 && detail::same_type(typeid(handle), *t) && rec->scope) {
246 // A py::init(...) constructor takes `self` as a `handle`; rewrite it to the type
247#if defined(PYPY_VERSION)
248 signature += rec->scope.attr("__module__").cast<std::string>() + ".";
249#endif
250 signature += ((PyTypeObject *) rec->scope.ptr())->tp_name;
Wenzel Jakob66c9a402016-01-17 22:36:36 +0100251 } else {
252 std::string tname(t->name());
253 detail::clean_type_id(tname);
254 signature += tname;
255 }
256 } else {
257 signature += c;
258 }
259 }
Wenzel Jakob95d18692016-01-17 22:36:40 +0100260 if (type_depth != 0 || types[type_index] != nullptr)
Wenzel Jakob678d7872016-01-17 22:36:41 +0100261 pybind11_fail("Internal error while parsing type signature (2)");
Wenzel Jakob66c9a402016-01-17 22:36:36 +0100262
Jason Rhinelanderca0e82b2017-05-09 14:34:45 -0400263 #if !defined(PYBIND11_CONSTEXPR_DESCR)
Wenzel Jakob66c9a402016-01-17 22:36:36 +0100264 delete[] types;
265 delete[] text;
266 #endif
Wenzel Jakobf5fae922015-08-24 15:31:24 +0200267
Wenzel Jakob57082212015-09-04 23:42:12 +0200268#if PY_MAJOR_VERSION < 3
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100269 if (strcmp(rec->name, "__next__") == 0) {
270 std::free(rec->name);
271 rec->name = strdup("next");
Wenzel Jakobd1bfc4e2016-05-16 18:52:46 +0200272 } else if (strcmp(rec->name, "__bool__") == 0) {
273 std::free(rec->name);
274 rec->name = strdup("__nonzero__");
Wenzel Jakob66c9a402016-01-17 22:36:36 +0100275 }
Wenzel Jakob57082212015-09-04 23:42:12 +0200276#endif
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100277 rec->signature = strdup(signature.c_str());
278 rec->args.shrink_to_fit();
Jason Rhinelanderabc29ca2017-01-23 03:50:00 -0500279 rec->nargs = (std::uint16_t) args;
Wenzel Jakobbd4a5292015-07-11 17:41:48 +0200280
Jason Rhinelander0a90b2d2017-04-16 20:30:52 -0400281 if (rec->sibling && PYBIND11_INSTANCE_METHOD_CHECK(rec->sibling.ptr()))
282 rec->sibling = PYBIND11_INSTANCE_METHOD_GET_FUNCTION(rec->sibling.ptr());
Wenzel Jakob57082212015-09-04 23:42:12 +0200283
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100284 detail::function_record *chain = nullptr, *chain_start = rec;
Jason Rhinelander6873c202016-10-24 21:58:22 -0400285 if (rec->sibling) {
286 if (PyCFunction_Check(rec->sibling.ptr())) {
Wenzel Jakob1d1f81b2016-12-16 15:00:46 +0100287 auto rec_capsule = reinterpret_borrow<capsule>(PyCFunction_GET_SELF(rec->sibling.ptr()));
Jason Rhinelander6873c202016-10-24 21:58:22 -0400288 chain = (detail::function_record *) rec_capsule;
289 /* Never append a method to an overload chain of a parent class;
290 instead, hide the parent's overloads in this case */
Dean Moldovan36f0a152017-02-08 01:01:56 +0100291 if (!chain->scope.is(rec->scope))
Jason Rhinelander6873c202016-10-24 21:58:22 -0400292 chain = nullptr;
293 }
294 // Don't trigger for things like the default __init__, which are wrapper_descriptors that we are intentionally replacing
295 else if (!rec->sibling.is_none() && rec->name[0] != '_')
296 pybind11_fail("Cannot overload existing non-function object \"" + std::string(rec->name) +
297 "\" with a function of the same name");
Wenzel Jakoba2f6fde2015-10-01 16:46:03 +0200298 }
299
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100300 if (!chain) {
Wenzel Jakobd561cb02016-01-17 22:36:41 +0100301 /* No existing overload was found, create a new function object */
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100302 rec->def = new PyMethodDef();
Jason Rhinelandere45c2112017-02-22 21:36:09 -0500303 std::memset(rec->def, 0, sizeof(PyMethodDef));
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100304 rec->def->ml_name = rec->name;
305 rec->def->ml_meth = reinterpret_cast<PyCFunction>(*dispatcher);
306 rec->def->ml_flags = METH_VARARGS | METH_KEYWORDS;
Wenzel Jakoba6501792016-02-04 23:02:07 +0100307
Wenzel Jakobb16421e2017-03-22 22:04:00 +0100308 capsule rec_capsule(rec, [](void *ptr) {
309 destruct((detail::function_record *) ptr);
Wenzel Jakobd561cb02016-01-17 22:36:41 +0100310 });
Wenzel Jakoba6501792016-02-04 23:02:07 +0100311
312 object scope_module;
313 if (rec->scope) {
Dean Moldovan865e4302016-09-21 01:06:32 +0200314 if (hasattr(rec->scope, "__module__")) {
315 scope_module = rec->scope.attr("__module__");
316 } else if (hasattr(rec->scope, "__name__")) {
317 scope_module = rec->scope.attr("__name__");
318 }
Wenzel Jakoba6501792016-02-04 23:02:07 +0100319 }
320
321 m_ptr = PyCFunction_NewEx(rec->def, rec_capsule.ptr(), scope_module.ptr());
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200322 if (!m_ptr)
Wenzel Jakob678d7872016-01-17 22:36:41 +0100323 pybind11_fail("cpp_function::cpp_function(): Could not allocate function object");
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200324 } else {
Wenzel Jakobd561cb02016-01-17 22:36:41 +0100325 /* Append at the end of the overload chain */
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100326 m_ptr = rec->sibling.ptr();
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200327 inc_ref();
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100328 chain_start = chain;
Jason Rhinelanderd355f2f2017-04-16 22:31:13 -0400329 if (chain->is_method != rec->is_method)
330 pybind11_fail("overloading a method with both static and instance methods is not supported; "
331 #if defined(NDEBUG)
332 "compile in debug mode for more details"
333 #else
334 "error while attempting to bind " + std::string(rec->is_method ? "instance" : "static") + " method " +
335 std::string(pybind11::str(rec->scope.attr("__name__"))) + "." + std::string(rec->name) + signature
336 #endif
337 );
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100338 while (chain->next)
339 chain = chain->next;
340 chain->next = rec;
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200341 }
Wenzel Jakob281aa0e2015-07-30 15:29:00 +0200342
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200343 std::string signatures;
Wenzel Jakob71867832015-07-29 17:43:52 +0200344 int index = 0;
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100345 /* Create a nice pydoc rec including all signatures and
Wenzel Jakobd561cb02016-01-17 22:36:41 +0100346 docstrings of the functions in the overload chain */
Alexander Stukowski9a110e62016-11-15 12:38:05 +0100347 if (chain && options::show_function_signatures()) {
Sylvain Corlay0e04fdf2016-03-08 16:42:12 -0500348 // First a generic signature
349 signatures += rec->name;
350 signatures += "(*args, **kwargs)\n";
351 signatures += "Overloaded function.\n\n";
352 }
353 // Then specific overload signatures
Jason Rhinelander10d13042017-03-08 12:32:42 -0500354 bool first_user_def = true;
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100355 for (auto it = chain_start; it != nullptr; it = it->next) {
Alexander Stukowski9a110e62016-11-15 12:38:05 +0100356 if (options::show_function_signatures()) {
Jason Rhinelander10d13042017-03-08 12:32:42 -0500357 if (index > 0) signatures += "\n";
Alexander Stukowski9a110e62016-11-15 12:38:05 +0100358 if (chain)
359 signatures += std::to_string(++index) + ". ";
360 signatures += rec->name;
361 signatures += it->signature;
Wenzel Jakob66c9a402016-01-17 22:36:36 +0100362 signatures += "\n";
Alexander Stukowski9a110e62016-11-15 12:38:05 +0100363 }
364 if (it->doc && strlen(it->doc) > 0 && options::show_user_defined_docstrings()) {
Jason Rhinelander10d13042017-03-08 12:32:42 -0500365 // If we're appending another docstring, and aren't printing function signatures, we
366 // need to append a newline first:
367 if (!options::show_function_signatures()) {
368 if (first_user_def) first_user_def = false;
369 else signatures += "\n";
370 }
Alexander Stukowski9a110e62016-11-15 12:38:05 +0100371 if (options::show_function_signatures()) signatures += "\n";
Wenzel Jakob218b6ce2016-02-28 23:52:37 +0100372 signatures += it->doc;
Alexander Stukowski9a110e62016-11-15 12:38:05 +0100373 if (options::show_function_signatures()) signatures += "\n";
Wenzel Jakob66c9a402016-01-17 22:36:36 +0100374 }
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200375 }
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100376
377 /* Install docstring */
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200378 PyCFunctionObject *func = (PyCFunctionObject *) m_ptr;
379 if (func->m_ml->ml_doc)
Matthew Woehlkee15fa9f2017-02-08 17:43:08 -0500380 std::free(const_cast<char *>(func->m_ml->ml_doc));
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200381 func->m_ml->ml_doc = strdup(signatures.c_str());
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100382
Wenzel Jakob31fbf182016-11-20 05:27:05 +0100383 if (rec->is_method) {
384 m_ptr = PYBIND11_INSTANCE_METHOD_NEW(m_ptr, rec->scope.ptr());
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200385 if (!m_ptr)
Wenzel Jakob678d7872016-01-17 22:36:41 +0100386 pybind11_fail("cpp_function::cpp_function(): Could not allocate instance method object");
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200387 Py_DECREF(func);
388 }
389 }
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100390
391 /// When a cpp_function is GCed, release any memory allocated by pybind11
392 static void destruct(detail::function_record *rec) {
393 while (rec) {
394 detail::function_record *next = rec->next;
395 if (rec->free_data)
Wenzel Jakob5984baa2016-05-10 15:05:03 +0100396 rec->free_data(rec);
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100397 std::free((char *) rec->name);
398 std::free((char *) rec->doc);
399 std::free((char *) rec->signature);
400 for (auto &arg: rec->args) {
Matthew Woehlkee15fa9f2017-02-08 17:43:08 -0500401 std::free(const_cast<char *>(arg.name));
402 std::free(const_cast<char *>(arg.descr));
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100403 arg.value.dec_ref();
404 }
405 if (rec->def) {
Matthew Woehlkee15fa9f2017-02-08 17:43:08 -0500406 std::free(const_cast<char *>(rec->def->ml_doc));
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100407 delete rec->def;
408 }
409 delete rec;
410 rec = next;
411 }
412 }
413
414 /// Main dispatch logic for calls to functions bound using pybind11
Jason Rhinelander2686da82017-01-21 23:42:14 -0500415 static PyObject *dispatcher(PyObject *self, PyObject *args_in, PyObject *kwargs_in) {
Jason Rhinelanderbfcf9522017-01-30 13:34:38 -0500416 using namespace detail;
417
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100418 /* Iterator over the list of potentially admissible overloads */
Jason Rhinelanderbfcf9522017-01-30 13:34:38 -0500419 function_record *overloads = (function_record *) PyCapsule_GetPointer(self, nullptr),
420 *it = overloads;
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100421
422 /* Need to know how many arguments + keyword arguments there are to pick the right overload */
Jason Rhinelander2686da82017-01-21 23:42:14 -0500423 const size_t n_args_in = (size_t) PyTuple_GET_SIZE(args_in);
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100424
Jason Rhinelander2686da82017-01-21 23:42:14 -0500425 handle parent = n_args_in > 0 ? PyTuple_GET_ITEM(args_in, 0) : nullptr,
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100426 result = PYBIND11_TRY_NEXT_OVERLOAD;
Jason Rhinelanderabc29ca2017-01-23 03:50:00 -0500427
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100428 try {
Jason Rhinelandere5505892017-02-03 18:25:34 -0500429 // We do this in two passes: in the first pass, we load arguments with `convert=false`;
430 // in the second, we allow conversion (except for arguments with an explicit
431 // py::arg().noconvert()). This lets us prefer calls without conversion, with
432 // conversion as a fallback.
433 std::vector<function_call> second_pass;
434
435 // However, if there are no overloads, we can just skip the no-convert pass entirely
436 const bool overloaded = it != nullptr && it->next != nullptr;
437
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100438 for (; it != nullptr; it = it->next) {
Jason Rhinelanderabc29ca2017-01-23 03:50:00 -0500439
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100440 /* For each overload:
Jason Rhinelander2686da82017-01-21 23:42:14 -0500441 1. Copy all positional arguments we were given, also checking to make sure that
442 named positional arguments weren't *also* specified via kwarg.
Wenzel Jakobab60bf12017-01-31 17:23:53 +0100443 2. If we weren't given enough, try to make up the omitted ones by checking
Jason Rhinelander2686da82017-01-21 23:42:14 -0500444 whether they were provided by a kwarg matching the `py::arg("name")` name. If
445 so, use it (and remove it from kwargs; if not, see if the function binding
446 provided a default that we can use.
447 3. Ensure that either all keyword arguments were "consumed", or that the function
448 takes a kwargs argument to accept unconsumed kwargs.
449 4. Any positional arguments still left get put into a tuple (for args), and any
450 leftover kwargs get put into a dict.
451 5. Pack everything into a vector; if we have py::args or py::kwargs, they are an
452 extra tuple or dict at the end of the positional arguments.
453 6. Call the function call dispatcher (function_record::impl)
454
455 If one of these fail, move on to the next overload and keep trying until we get a
456 result other than PYBIND11_TRY_NEXT_OVERLOAD.
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100457 */
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100458
Jason Rhinelanderbfcf9522017-01-30 13:34:38 -0500459 function_record &func = *it;
460 size_t pos_args = func.nargs; // Number of positional arguments that we need
461 if (func.has_args) --pos_args; // (but don't count py::args
462 if (func.has_kwargs) --pos_args; // or py::kwargs)
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100463
Jason Rhinelanderbfcf9522017-01-30 13:34:38 -0500464 if (!func.has_args && n_args_in > pos_args)
Jason Rhinelander2686da82017-01-21 23:42:14 -0500465 continue; // Too many arguments for this overload
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100466
Jason Rhinelanderbfcf9522017-01-30 13:34:38 -0500467 if (n_args_in < pos_args && func.args.size() < pos_args)
Jason Rhinelander2686da82017-01-21 23:42:14 -0500468 continue; // Not enough arguments given, and not enough defaults to fill in the blanks
469
Jason Rhinelanderbfcf9522017-01-30 13:34:38 -0500470 function_call call(func, parent);
Jason Rhinelander2686da82017-01-21 23:42:14 -0500471
472 size_t args_to_copy = std::min(pos_args, n_args_in);
473 size_t args_copied = 0;
474
475 // 1. Copy any position arguments given.
Jason Rhinelander4e1e4a52017-05-17 11:55:43 -0400476 bool bad_arg = false;
Jason Rhinelander2686da82017-01-21 23:42:14 -0500477 for (; args_copied < args_to_copy; ++args_copied) {
Jason Rhinelander4e1e4a52017-05-17 11:55:43 -0400478 argument_record *arg_rec = args_copied < func.args.size() ? &func.args[args_copied] : nullptr;
479 if (kwargs_in && arg_rec && arg_rec->name && PyDict_GetItemString(kwargs_in, arg_rec->name)) {
480 bad_arg = true;
Jason Rhinelandercaa13792017-02-23 19:54:53 -0500481 break;
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100482 }
Jason Rhinelander2686da82017-01-21 23:42:14 -0500483
Jason Rhinelander4e1e4a52017-05-17 11:55:43 -0400484 handle arg(PyTuple_GET_ITEM(args_in, args_copied));
485 if (arg_rec && !arg_rec->none && arg.is_none()) {
486 bad_arg = true;
487 break;
488 }
489 call.args.push_back(arg);
490 call.args_convert.push_back(arg_rec ? arg_rec->convert : true);
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100491 }
Jason Rhinelander4e1e4a52017-05-17 11:55:43 -0400492 if (bad_arg)
Jason Rhinelandercaa13792017-02-23 19:54:53 -0500493 continue; // Maybe it was meant for another overload (issue #688)
Wenzel Jakobbd57eb42016-05-01 14:42:20 +0200494
Jason Rhinelander2686da82017-01-21 23:42:14 -0500495 // We'll need to copy this if we steal some kwargs for defaults
496 dict kwargs = reinterpret_borrow<dict>(kwargs_in);
497
498 // 2. Check kwargs and, failing that, defaults that may help complete the list
499 if (args_copied < pos_args) {
500 bool copied_kwargs = false;
501
502 for (; args_copied < pos_args; ++args_copied) {
Jason Rhinelanderabc29ca2017-01-23 03:50:00 -0500503 const auto &arg = func.args[args_copied];
Jason Rhinelander2686da82017-01-21 23:42:14 -0500504
505 handle value;
Jason Rhinelanderabc29ca2017-01-23 03:50:00 -0500506 if (kwargs_in && arg.name)
Jason Rhinelander2686da82017-01-21 23:42:14 -0500507 value = PyDict_GetItemString(kwargs.ptr(), arg.name);
508
509 if (value) {
510 // Consume a kwargs value
511 if (!copied_kwargs) {
512 kwargs = reinterpret_steal<dict>(PyDict_Copy(kwargs.ptr()));
513 copied_kwargs = true;
514 }
515 PyDict_DelItemString(kwargs.ptr(), arg.name);
Wenzel Jakobab60bf12017-01-31 17:23:53 +0100516 } else if (arg.value) {
Jason Rhinelander2686da82017-01-21 23:42:14 -0500517 value = arg.value;
518 }
519
Jason Rhinelanderabc29ca2017-01-23 03:50:00 -0500520 if (value) {
Jason Rhinelanderbfcf9522017-01-30 13:34:38 -0500521 call.args.push_back(value);
Jason Rhinelanderabc29ca2017-01-23 03:50:00 -0500522 call.args_convert.push_back(arg.convert);
523 }
Jason Rhinelander2686da82017-01-21 23:42:14 -0500524 else
525 break;
526 }
527
528 if (args_copied < pos_args)
529 continue; // Not enough arguments, defaults, or kwargs to fill the positional arguments
530 }
531
532 // 3. Check everything was consumed (unless we have a kwargs arg)
Jason Rhinelanderabc29ca2017-01-23 03:50:00 -0500533 if (kwargs && kwargs.size() > 0 && !func.has_kwargs)
Jason Rhinelander2686da82017-01-21 23:42:14 -0500534 continue; // Unconsumed kwargs, but no py::kwargs argument to accept them
535
536 // 4a. If we have a py::args argument, create a new tuple with leftovers
537 tuple extra_args;
Jason Rhinelanderabc29ca2017-01-23 03:50:00 -0500538 if (func.has_args) {
Jason Rhinelander2686da82017-01-21 23:42:14 -0500539 if (args_to_copy == 0) {
540 // We didn't copy out any position arguments from the args_in tuple, so we
541 // can reuse it directly without copying:
542 extra_args = reinterpret_borrow<tuple>(args_in);
Wenzel Jakobab60bf12017-01-31 17:23:53 +0100543 } else if (args_copied >= n_args_in) {
Jason Rhinelander2686da82017-01-21 23:42:14 -0500544 extra_args = tuple(0);
Wenzel Jakobab60bf12017-01-31 17:23:53 +0100545 } else {
Jason Rhinelander2686da82017-01-21 23:42:14 -0500546 size_t args_size = n_args_in - args_copied;
547 extra_args = tuple(args_size);
548 for (size_t i = 0; i < args_size; ++i) {
549 handle item = PyTuple_GET_ITEM(args_in, args_copied + i);
550 extra_args[i] = item.inc_ref().ptr();
551 }
552 }
Jason Rhinelanderbfcf9522017-01-30 13:34:38 -0500553 call.args.push_back(extra_args);
Jason Rhinelanderabc29ca2017-01-23 03:50:00 -0500554 call.args_convert.push_back(false);
Jason Rhinelander2686da82017-01-21 23:42:14 -0500555 }
556
557 // 4b. If we have a py::kwargs, pass on any remaining kwargs
Jason Rhinelanderabc29ca2017-01-23 03:50:00 -0500558 if (func.has_kwargs) {
Jason Rhinelander2686da82017-01-21 23:42:14 -0500559 if (!kwargs.ptr())
560 kwargs = dict(); // If we didn't get one, send an empty one
Jason Rhinelanderbfcf9522017-01-30 13:34:38 -0500561 call.args.push_back(kwargs);
Jason Rhinelanderabc29ca2017-01-23 03:50:00 -0500562 call.args_convert.push_back(false);
Jason Rhinelander2686da82017-01-21 23:42:14 -0500563 }
564
Jason Rhinelanderbfcf9522017-01-30 13:34:38 -0500565 // 5. Put everything in a vector. Not technically step 5, we've been building it
566 // in `call.args` all along.
567 #if !defined(NDEBUG)
Jason Rhinelanderabc29ca2017-01-23 03:50:00 -0500568 if (call.args.size() != func.nargs || call.args_convert.size() != func.nargs)
Jason Rhinelanderbfcf9522017-01-30 13:34:38 -0500569 pybind11_fail("Internal error: function call dispatcher inserted wrong number of arguments!");
570 #endif
Jason Rhinelander2686da82017-01-21 23:42:14 -0500571
Jason Rhinelandere5505892017-02-03 18:25:34 -0500572 std::vector<bool> second_pass_convert;
573 if (overloaded) {
574 // We're in the first no-convert pass, so swap out the conversion flags for a
575 // set of all-false flags. If the call fails, we'll swap the flags back in for
576 // the conversion-allowed call below.
577 second_pass_convert.resize(func.nargs, false);
578 call.args_convert.swap(second_pass_convert);
579 }
580
Jason Rhinelander2686da82017-01-21 23:42:14 -0500581 // 6. Call the function.
Wenzel Jakobbd57eb42016-05-01 14:42:20 +0200582 try {
Dean Moldovanaf2dda32017-06-26 20:34:06 +0200583 loader_life_support guard{};
Jason Rhinelanderabc29ca2017-01-23 03:50:00 -0500584 result = func.impl(call);
Wenzel Jakob00062592016-07-01 16:07:35 +0200585 } catch (reference_cast_error &) {
Wenzel Jakobbd57eb42016-05-01 14:42:20 +0200586 result = PYBIND11_TRY_NEXT_OVERLOAD;
587 }
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100588
589 if (result.ptr() != PYBIND11_TRY_NEXT_OVERLOAD)
590 break;
Jason Rhinelanderabc29ca2017-01-23 03:50:00 -0500591
Jason Rhinelandere5505892017-02-03 18:25:34 -0500592 if (overloaded) {
593 // The (overloaded) call failed; if the call has at least one argument that
594 // permits conversion (i.e. it hasn't been explicitly specified `.noconvert()`)
595 // then add this call to the list of second pass overloads to try.
596 for (size_t i = func.is_method ? 1 : 0; i < pos_args; i++) {
597 if (second_pass_convert[i]) {
598 // Found one: swap the converting flags back in and store the call for
599 // the second pass.
600 call.args_convert.swap(second_pass_convert);
601 second_pass.push_back(std::move(call));
602 break;
603 }
604 }
605 }
606 }
607
608 if (overloaded && !second_pass.empty() && result.ptr() == PYBIND11_TRY_NEXT_OVERLOAD) {
609 // The no-conversion pass finished without success, try again with conversion allowed
610 for (auto &call : second_pass) {
611 try {
Dean Moldovanaf2dda32017-06-26 20:34:06 +0200612 loader_life_support guard{};
Jason Rhinelandere5505892017-02-03 18:25:34 -0500613 result = call.func.impl(call);
614 } catch (reference_cast_error &) {
615 result = PYBIND11_TRY_NEXT_OVERLOAD;
616 }
617
618 if (result.ptr() != PYBIND11_TRY_NEXT_OVERLOAD)
619 break;
620 }
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100621 }
Dean Moldovan135ba8d2016-09-10 11:58:02 +0200622 } catch (error_already_set &e) {
623 e.restore();
Pim Schellart5a7d17f2016-06-17 17:35:59 -0400624 return nullptr;
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100625 } catch (...) {
Pim Schellart5a7d17f2016-06-17 17:35:59 -0400626 /* When an exception is caught, give each registered exception
627 translator a chance to translate it to a Python exception
628 in reverse order of registration.
Wenzel Jakob1f66a582016-07-18 10:47:10 +0200629
Pim Schellart5a7d17f2016-06-17 17:35:59 -0400630 A translator may choose to do one of the following:
Wenzel Jakob1f66a582016-07-18 10:47:10 +0200631
Pim Schellart5a7d17f2016-06-17 17:35:59 -0400632 - catch the exception and call PyErr_SetString or PyErr_SetObject
633 to set a standard (or custom) Python exception, or
634 - do nothing and let the exception fall through to the next translator, or
635 - delegate translation to the next translator by throwing a new type of exception. */
636
Wenzel Jakob1f66a582016-07-18 10:47:10 +0200637 auto last_exception = std::current_exception();
Jason Rhinelanderbfcf9522017-01-30 13:34:38 -0500638 auto &registered_exception_translators = get_internals().registered_exception_translators;
Pim Schellart5a7d17f2016-06-17 17:35:59 -0400639 for (auto& translator : registered_exception_translators) {
640 try {
641 translator(last_exception);
642 } catch (...) {
643 last_exception = std::current_exception();
644 continue;
645 }
646 return nullptr;
647 }
648 PyErr_SetString(PyExc_SystemError, "Exception escaped from default exception translator!");
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100649 return nullptr;
650 }
651
652 if (result.ptr() == PYBIND11_TRY_NEXT_OVERLOAD) {
Wenzel Jakob382484a2016-09-10 15:28:37 +0900653 if (overloads->is_operator)
654 return handle(Py_NotImplemented).inc_ref().ptr();
655
Wenzel Jakobe99ebae2016-09-12 11:44:37 +0900656 std::string msg = std::string(overloads->name) + "(): incompatible " +
657 std::string(overloads->is_constructor ? "constructor" : "function") +
658 " arguments. The following argument types are supported:\n";
659
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100660 int ctr = 0;
Jason Rhinelanderbfcf9522017-01-30 13:34:38 -0500661 for (function_record *it2 = overloads; it2 != nullptr; it2 = it2->next) {
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100662 msg += " "+ std::to_string(++ctr) + ". ";
Jason Rhinelander4e45e182016-07-17 17:43:00 -0400663
664 bool wrote_sig = false;
665 if (overloads->is_constructor) {
Dean Moldovanecced6c2016-07-31 20:03:18 +0200666 // For a constructor, rewrite `(self: Object, arg0, ...) -> NoneType` as `Object(arg0, ...)`
Jason Rhinelander4e45e182016-07-17 17:43:00 -0400667 std::string sig = it2->signature;
Dean Moldovanecced6c2016-07-31 20:03:18 +0200668 size_t start = sig.find('(') + 7; // skip "(self: "
Jason Rhinelander4e45e182016-07-17 17:43:00 -0400669 if (start < sig.size()) {
670 // End at the , for the next argument
671 size_t end = sig.find(", "), next = end + 2;
672 size_t ret = sig.rfind(" -> ");
673 // Or the ), if there is no comma:
674 if (end >= sig.size()) next = end = sig.find(')');
675 if (start < end && next < sig.size()) {
676 msg.append(sig, start, end - start);
677 msg += '(';
678 msg.append(sig, next, ret - next);
679 wrote_sig = true;
680 }
681 }
682 }
683 if (!wrote_sig) msg += it2->signature;
684
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100685 msg += "\n";
686 }
Wenzel Jakobe99ebae2016-09-12 11:44:37 +0900687 msg += "\nInvoked with: ";
Jason Rhinelander2686da82017-01-21 23:42:14 -0500688 auto args_ = reinterpret_borrow<tuple>(args_in);
Jason Rhinelander231e1672017-02-23 21:04:46 -0500689 bool some_args = false;
Wenzel Jakob1f66a582016-07-18 10:47:10 +0200690 for (size_t ti = overloads->is_constructor ? 1 : 0; ti < args_.size(); ++ti) {
Jason Rhinelander231e1672017-02-23 21:04:46 -0500691 if (!some_args) some_args = true;
692 else msg += ", ";
Jason Rhinelander7146d622016-11-22 05:28:40 -0500693 msg += pybind11::repr(args_[ti]);
Andreas Bergmeier16d43942016-05-24 09:19:35 +0200694 }
Jason Rhinelander231e1672017-02-23 21:04:46 -0500695 if (kwargs_in) {
696 auto kwargs = reinterpret_borrow<dict>(kwargs_in);
697 if (kwargs.size() > 0) {
698 if (some_args) msg += "; ";
699 msg += "kwargs: ";
700 bool first = true;
701 for (auto kwarg : kwargs) {
702 if (first) first = false;
703 else msg += ", ";
704 msg += pybind11::str("{}={!r}").format(kwarg.first, kwarg.second);
705 }
706 }
707 }
708
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100709 PyErr_SetString(PyExc_TypeError, msg.c_str());
710 return nullptr;
711 } else if (!result) {
712 std::string msg = "Unable to convert function return value to a "
713 "Python type! The signature was\n\t";
714 msg += it->signature;
715 PyErr_SetString(PyExc_TypeError, msg.c_str());
716 return nullptr;
717 } else {
718 if (overloads->is_constructor) {
Jason Rhinelandere45c2112017-02-22 21:36:09 -0500719 auto tinfo = get_type_info((PyTypeObject *) overloads->scope.ptr());
Jason Rhinelander464d9892017-06-12 21:52:48 -0400720 auto *pi = reinterpret_cast<instance *>(parent.ptr());
721 auto v_h = pi->get_value_and_holder(tinfo);
722 if (!v_h.holder_constructed()) {
723 tinfo->init_instance(pi, nullptr);
724 }
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100725 }
726 return result.ptr();
727 }
728 }
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200729};
730
Wenzel Jakobd561cb02016-01-17 22:36:41 +0100731/// Wrapper for Python extension modules
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200732class module : public object {
733public:
Wenzel Jakobb1b71402015-10-18 16:48:30 +0200734 PYBIND11_OBJECT_DEFAULT(module, object, PyModule_Check)
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200735
Dean Moldovan57a9bbc2017-01-31 16:54:08 +0100736 /// Create a new top-level Python module with the given name and docstring
Jason Rhinelander12d76602016-10-16 16:27:42 -0400737 explicit module(const char *name, const char *doc = nullptr) {
Alexander Stukowski9a110e62016-11-15 12:38:05 +0100738 if (!options::show_user_defined_docstrings()) doc = nullptr;
Wenzel Jakob57082212015-09-04 23:42:12 +0200739#if PY_MAJOR_VERSION >= 3
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200740 PyModuleDef *def = new PyModuleDef();
Jason Rhinelandere45c2112017-02-22 21:36:09 -0500741 std::memset(def, 0, sizeof(PyModuleDef));
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200742 def->m_name = name;
743 def->m_doc = doc;
744 def->m_size = -1;
745 Py_INCREF(def);
746 m_ptr = PyModule_Create(def);
Wenzel Jakob57082212015-09-04 23:42:12 +0200747#else
748 m_ptr = Py_InitModule3(name, nullptr, doc);
749#endif
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200750 if (m_ptr == nullptr)
Wenzel Jakob678d7872016-01-17 22:36:41 +0100751 pybind11_fail("Internal error in module::module()");
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200752 inc_ref();
753 }
754
Dean Moldovan57a9bbc2017-01-31 16:54:08 +0100755 /** \rst
756 Create Python binding for a new function within the module scope. ``Func``
757 can be a plain C++ function, a function pointer, or a lambda function. For
758 details on the ``Extra&& ... extra`` argument, see section :ref:`extras`.
759 \endrst */
Wenzel Jakob71867832015-07-29 17:43:52 +0200760 template <typename Func, typename... Extra>
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100761 module &def(const char *name_, Func &&f, const Extra& ... extra) {
Dean Moldovan865e4302016-09-21 01:06:32 +0200762 cpp_function func(std::forward<Func>(f), name(name_), scope(*this),
763 sibling(getattr(*this, name_, none())), extra...);
Jason Rhinelander6873c202016-10-24 21:58:22 -0400764 // NB: allow overwriting here because cpp_function sets up a chain with the intention of
765 // overwriting (and has already checked internally that it isn't overwriting non-functions).
766 add_object(name_, func, true /* overwrite */);
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200767 return *this;
768 }
769
Dean Moldovan57a9bbc2017-01-31 16:54:08 +0100770 /** \rst
771 Create and return a new Python submodule with the given name and docstring.
772 This also works recursively, i.e.
773
774 .. code-block:: cpp
775
776 py::module m("example", "pybind11 example plugin");
777 py::module m2 = m.def_submodule("sub", "A submodule of 'example'");
778 py::module m3 = m2.def_submodule("subsub", "A submodule of 'example.sub'");
779 \endrst */
Wenzel Jakobbd4a5292015-07-11 17:41:48 +0200780 module def_submodule(const char *name, const char *doc = nullptr) {
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200781 std::string full_name = std::string(PyModule_GetName(m_ptr))
782 + std::string(".") + std::string(name);
Dean Moldovanc7ac16b2016-10-28 03:08:15 +0200783 auto result = reinterpret_borrow<module>(PyImport_AddModule(full_name.c_str()));
Alexander Stukowski9a110e62016-11-15 12:38:05 +0100784 if (doc && options::show_user_defined_docstrings())
Wenzel Jakob8f4eb002015-10-15 18:13:33 +0200785 result.attr("__doc__") = pybind11::str(doc);
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200786 attr(name) = result;
787 return result;
788 }
Wenzel Jakobdb028d62015-10-13 23:44:25 +0200789
Dean Moldovan57a9bbc2017-01-31 16:54:08 +0100790 /// Import and return a module or throws `error_already_set`.
Wenzel Jakobdb028d62015-10-13 23:44:25 +0200791 static module import(const char *name) {
Wenzel Jakobdd57a342015-12-26 14:04:52 +0100792 PyObject *obj = PyImport_ImportModule(name);
793 if (!obj)
esquires67a68f12016-12-01 05:35:34 -0500794 throw error_already_set();
Dean Moldovanc7ac16b2016-10-28 03:08:15 +0200795 return reinterpret_steal<module>(obj);
Wenzel Jakobdb028d62015-10-13 23:44:25 +0200796 }
Jason Rhinelander6873c202016-10-24 21:58:22 -0400797
798 // Adds an object to the module using the given name. Throws if an object with the given name
799 // already exists.
800 //
801 // overwrite should almost always be false: attempting to overwrite objects that pybind11 has
802 // established will, in most cases, break things.
Wenzel Jakobaa1b3162017-03-30 11:59:32 +0200803 PYBIND11_NOINLINE void add_object(const char *name, handle obj, bool overwrite = false) {
Jason Rhinelander6873c202016-10-24 21:58:22 -0400804 if (!overwrite && hasattr(*this, name))
805 pybind11_fail("Error during initialization: multiple incompatible definitions with name \"" +
806 std::string(name) + "\"");
807
Wenzel Jakobaa1b3162017-03-30 11:59:32 +0200808 PyModule_AddObject(ptr(), name, obj.inc_ref().ptr() /* steals a reference */);
Jason Rhinelander6873c202016-10-24 21:58:22 -0400809 }
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200810};
811
Dean Moldovan22c413b2017-03-30 00:20:42 +0200812/// \ingroup python_builtins
Dean Moldovan1d3c4bc2017-06-06 17:05:19 +0200813/// Return a dictionary representing the global variables in the current execution frame,
814/// or ``__main__.__dict__`` if there is no frame (usually when the interpreter is embedded).
815inline dict globals() {
816 PyObject *p = PyEval_GetGlobals();
817 return reinterpret_borrow<dict>(p ? p : module::import("__main__").attr("__dict__").ptr());
818}
Dean Moldovan22c413b2017-03-30 00:20:42 +0200819
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200820NAMESPACE_BEGIN(detail)
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100821/// Generic support for creating new Python heap types
Wenzel Jakobd561cb02016-01-17 22:36:41 +0100822class generic_type : public object {
Jason Rhinelander5fffe202016-09-06 12:17:06 -0400823 template <typename...> friend class class_;
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200824public:
Wenzel Jakobd561cb02016-01-17 22:36:41 +0100825 PYBIND11_OBJECT_DEFAULT(generic_type, object, PyType_Check)
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100826protected:
Dean Moldovan08cbe8d2017-02-15 21:10:25 +0100827 void initialize(const type_record &rec) {
828 if (rec.scope && hasattr(rec.scope, rec.name))
829 pybind11_fail("generic_type: cannot initialize type \"" + std::string(rec.name) +
830 "\": an object with that name is already defined");
Wenzel Jakob38d8b8c2016-05-31 09:53:28 +0200831
Jason Rhinelander4b159232017-08-04 13:05:12 -0400832 if (get_type_info(*rec.type, false /* don't throw */, !rec.module_local))
Dean Moldovan08cbe8d2017-02-15 21:10:25 +0100833 pybind11_fail("generic_type: type \"" + std::string(rec.name) +
Wenzel Jakob38d8b8c2016-05-31 09:53:28 +0200834 "\" is already registered!");
835
Dean Moldovan08cbe8d2017-02-15 21:10:25 +0100836 m_ptr = make_new_python_type(rec);
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200837
Wenzel Jakobd561cb02016-01-17 22:36:41 +0100838 /* Register supplemental type information in C++ dict */
Dean Moldovan08cbe8d2017-02-15 21:10:25 +0100839 auto *tinfo = new detail::type_info();
840 tinfo->type = (PyTypeObject *) m_ptr;
Jason Rhinelander1f8a1002017-04-21 19:01:30 -0400841 tinfo->cpptype = rec.type;
Dean Moldovan08cbe8d2017-02-15 21:10:25 +0100842 tinfo->type_size = rec.type_size;
Dean Moldovan0d765f42017-03-21 01:15:20 +0100843 tinfo->operator_new = rec.operator_new;
Jason Rhinelandere45c2112017-02-22 21:36:09 -0500844 tinfo->holder_size_in_ptrs = size_in_ptrs(rec.holder_size);
Jason Rhinelander353615f2017-07-25 00:53:23 -0400845 tinfo->init_instance = rec.init_instance;
Dean Moldovan08cbe8d2017-02-15 21:10:25 +0100846 tinfo->dealloc = rec.dealloc;
Jason Rhinelander1f8a1002017-04-21 19:01:30 -0400847 tinfo->simple_type = true;
848 tinfo->simple_ancestors = true;
Jason Rhinelander7437c692017-07-28 22:03:44 -0400849 tinfo->default_holder = rec.default_holder;
850 tinfo->module_local = rec.module_local;
Dean Moldovan08cbe8d2017-02-15 21:10:25 +0100851
852 auto &internals = get_internals();
853 auto tindex = std::type_index(*rec.type);
Ivan Smirnova6e6a8b2016-10-23 15:27:13 +0100854 tinfo->direct_conversions = &internals.direct_conversions[tindex];
Jason Rhinelander7437c692017-07-28 22:03:44 -0400855 if (rec.module_local)
856 registered_local_types_cpp()[tindex] = tinfo;
857 else
858 internals.registered_types_cpp[tindex] = tinfo;
Jason Rhinelandere45c2112017-02-22 21:36:09 -0500859 internals.registered_types_py[(PyTypeObject *) m_ptr] = { tinfo };
Wenzel Jakobd561cb02016-01-17 22:36:41 +0100860
Jason Rhinelander1f8a1002017-04-21 19:01:30 -0400861 if (rec.bases.size() > 1 || rec.multiple_inheritance) {
Dean Moldovan08cbe8d2017-02-15 21:10:25 +0100862 mark_parents_nonsimple(tinfo->type);
Jason Rhinelander1f8a1002017-04-21 19:01:30 -0400863 tinfo->simple_ancestors = false;
864 }
865 else if (rec.bases.size() == 1) {
866 auto parent_tinfo = get_type_info((PyTypeObject *) rec.bases[0].ptr());
867 tinfo->simple_ancestors = parent_tinfo->simple_ancestors;
868 }
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200869 }
870
Wenzel Jakob8e5dceb2016-09-11 20:00:40 +0900871 /// Helper function which tags all parents of a type using mult. inheritance
872 void mark_parents_nonsimple(PyTypeObject *value) {
Dean Moldovanc7ac16b2016-10-28 03:08:15 +0200873 auto t = reinterpret_borrow<tuple>(value->tp_bases);
Wenzel Jakob8e5dceb2016-09-11 20:00:40 +0900874 for (handle h : t) {
875 auto tinfo2 = get_type_info((PyTypeObject *) h.ptr());
876 if (tinfo2)
877 tinfo2->simple_type = false;
878 mark_parents_nonsimple((PyTypeObject *) h.ptr());
879 }
880 }
881
Wenzel Jakob43398a82015-07-28 16:12:20 +0200882 void install_buffer_funcs(
883 buffer_info *(*get_buffer)(PyObject *, void *),
884 void *get_buffer_data) {
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200885 PyHeapTypeObject *type = (PyHeapTypeObject*) m_ptr;
Wenzel Jakobd561cb02016-01-17 22:36:41 +0100886 auto tinfo = detail::get_type_info(&type->ht_type);
Wenzel Jakob1d1f81b2016-12-16 15:00:46 +0100887
888 if (!type->ht_type.tp_as_buffer)
889 pybind11_fail(
890 "To be able to register buffer protocol support for the type '" +
891 std::string(tinfo->type->tp_name) +
892 "' the associated class<>(..) invocation must "
893 "include the pybind11::buffer_protocol() annotation!");
894
Wenzel Jakobd561cb02016-01-17 22:36:41 +0100895 tinfo->get_buffer = get_buffer;
896 tinfo->get_buffer_data = get_buffer_data;
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200897 }
898
Wenzel Jakob1d1f81b2016-12-16 15:00:46 +0100899 void def_property_static_impl(const char *name,
900 handle fget, handle fset,
901 detail::function_record *rec_fget) {
Dean Moldovanc91f8bd2017-02-13 18:11:24 +0100902 const auto is_static = !(rec_fget->is_method && rec_fget->scope);
903 const auto has_doc = rec_fget->doc && pybind11::options::show_user_defined_docstrings();
904
Dean Moldovanc91f8bd2017-02-13 18:11:24 +0100905 auto property = handle((PyObject *) (is_static ? get_internals().static_property_type
906 : &PyProperty_Type));
907 attr(name) = property(fget.ptr() ? fget : none(),
908 fset.ptr() ? fset : none(),
909 /*deleter*/none(),
910 pybind11::str(has_doc ? rec_fget->doc : ""));
Wenzel Jakob1d1f81b2016-12-16 15:00:46 +0100911 }
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200912};
Jason Rhinelander6b52c832016-09-06 12:27:00 -0400913
Dean Moldovan0d765f42017-03-21 01:15:20 +0100914/// Set the pointer to operator new if it exists. The cast is needed because it can be overloaded.
915template <typename T, typename = void_t<decltype(static_cast<void *(*)(size_t)>(T::operator new))>>
916void set_operator_new(type_record *r) { r->operator_new = &T::operator new; }
917
918template <typename> void set_operator_new(...) { }
919
Jason Rhinelandera03408c2017-07-23 00:32:58 -0400920template <typename T, typename SFINAE = void> struct has_operator_delete : std::false_type { };
921template <typename T> struct has_operator_delete<T, void_t<decltype(static_cast<void (*)(void *)>(T::operator delete))>>
922 : std::true_type { };
923template <typename T, typename SFINAE = void> struct has_operator_delete_size : std::false_type { };
924template <typename T> struct has_operator_delete_size<T, void_t<decltype(static_cast<void (*)(void *, size_t)>(T::operator delete))>>
925 : std::true_type { };
Dean Moldovan0d765f42017-03-21 01:15:20 +0100926/// Call class-specific delete if it exists or global otherwise. Can also be an overload set.
Jason Rhinelandera03408c2017-07-23 00:32:58 -0400927template <typename T, enable_if_t<has_operator_delete<T>::value, int> = 0>
928void call_operator_delete(T *p, size_t) { T::operator delete(p); }
929template <typename T, enable_if_t<!has_operator_delete<T>::value && has_operator_delete_size<T>::value, int> = 0>
930void call_operator_delete(T *p, size_t s) { T::operator delete(p, s); }
Dean Moldovan0d765f42017-03-21 01:15:20 +0100931
Jason Rhinelandera03408c2017-07-23 00:32:58 -0400932inline void call_operator_delete(void *p, size_t) { ::operator delete(p); }
Dean Moldovan0d765f42017-03-21 01:15:20 +0100933
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200934NAMESPACE_END(detail)
935
Jason Rhinelander23bf8942017-05-16 11:07:28 -0400936/// Given a pointer to a member function, cast it to its `Derived` version.
937/// Forward everything else unchanged.
938template <typename /*Derived*/, typename F>
939auto method_adaptor(F &&f) -> decltype(std::forward<F>(f)) { return std::forward<F>(f); }
940
941template <typename Derived, typename Return, typename Class, typename... Args>
942auto method_adaptor(Return (Class::*pmf)(Args...)) -> Return (Derived::*)(Args...) { return pmf; }
943
944template <typename Derived, typename Return, typename Class, typename... Args>
945auto method_adaptor(Return (Class::*pmf)(Args...) const) -> Return (Derived::*)(Args...) const { return pmf; }
946
Jason Rhinelander5fffe202016-09-06 12:17:06 -0400947template <typename type_, typename... options>
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100948class class_ : public detail::generic_type {
Jason Rhinelander5fffe202016-09-06 12:17:06 -0400949 template <typename T> using is_holder = detail::is_holder_type<type_, T>;
Jason Rhinelander23bf8942017-05-16 11:07:28 -0400950 template <typename T> using is_subtype = detail::is_strict_base_of<type_, T>;
951 template <typename T> using is_base = detail::is_strict_base_of<T, type_>;
Jason Rhinelanderfa5d05e2016-12-12 18:11:49 -0500952 // struct instead of using here to help MSVC:
953 template <typename T> struct is_valid_class_option :
954 detail::any_of<is_holder<T>, is_subtype<T>, is_base<T>> {};
Jason Rhinelander5fffe202016-09-06 12:17:06 -0400955
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200956public:
Jason Rhinelander5fffe202016-09-06 12:17:06 -0400957 using type = type_;
Dean Moldovan82ece942017-03-29 11:55:18 +0200958 using type_alias = detail::exactly_one_t<is_subtype, void, options...>;
Jason Rhinelander5fffe202016-09-06 12:17:06 -0400959 constexpr static bool has_alias = !std::is_void<type_alias>::value;
Dean Moldovan82ece942017-03-29 11:55:18 +0200960 using holder_type = detail::exactly_one_t<is_holder, std::unique_ptr<type>, options...>;
Jason Rhinelander5fffe202016-09-06 12:17:06 -0400961
Jason Rhinelanderfa5d05e2016-12-12 18:11:49 -0500962 static_assert(detail::all_of<is_valid_class_option<options>...>::value,
Jason Rhinelander5fffe202016-09-06 12:17:06 -0400963 "Unknown/invalid class_ template parameters provided");
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200964
Jason Rhinelander42e5ddc2017-06-12 21:48:36 -0400965 static_assert(!has_alias || std::is_polymorphic<type>::value,
966 "Cannot use an alias class with a non-polymorphic type");
967
Dean Moldovanc7ac16b2016-10-28 03:08:15 +0200968 PYBIND11_OBJECT(class_, generic_type, PyType_Check)
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200969
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100970 template <typename... Extra>
971 class_(handle scope, const char *name, const Extra &... extra) {
Jason Rhinelanderb9616262017-03-16 20:10:48 -0300972 using namespace detail;
973
974 // MI can only be specified via class_ template options, not constructor parameters
975 static_assert(
976 none_of<is_pyobject<Extra>...>::value || // no base class arguments, or:
977 ( constexpr_sum(is_pyobject<Extra>::value...) == 1 && // Exactly one base
978 constexpr_sum(is_base<options>::value...) == 0 && // no template option bases
979 none_of<std::is_same<multiple_inheritance, Extra>...>::value), // no multiple_inheritance attr
980 "Error: multiple inheritance bases must be specified via class_ template options");
981
982 type_record record;
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100983 record.scope = scope;
984 record.name = name;
985 record.type = &typeid(type);
Jason Rhinelanderb9616262017-03-16 20:10:48 -0300986 record.type_size = sizeof(conditional_t<has_alias, type_alias, type>);
Jason Rhinelandere45c2112017-02-22 21:36:09 -0500987 record.holder_size = sizeof(holder_type);
Jason Rhinelander353615f2017-07-25 00:53:23 -0400988 record.init_instance = init_instance;
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100989 record.dealloc = dealloc;
Pim Schellartcc88aae2017-01-31 10:52:11 -0500990 record.default_holder = std::is_same<holder_type, std::unique_ptr<type>>::value;
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200991
Dean Moldovan0d765f42017-03-21 01:15:20 +0100992 set_operator_new<type>(&record);
993
Wenzel Jakob8e5dceb2016-09-11 20:00:40 +0900994 /* Register base classes specified via template arguments to class_, if any */
Jason Rhinelander926e2cf2017-03-25 22:41:06 -0300995 PYBIND11_EXPAND_SIDE_EFFECTS(add_base<options>(record));
Jason Rhinelander6b52c832016-09-06 12:27:00 -0400996
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100997 /* Process optional arguments, if any */
Jason Rhinelanderb9616262017-03-16 20:10:48 -0300998 process_attributes<Extra...>::init(extra..., &record);
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100999
Jason Rhinelanderb9616262017-03-16 20:10:48 -03001000 generic_type::initialize(record);
Wenzel Jakob86d825f2016-05-26 13:19:27 +02001001
Jason Rhinelander5fffe202016-09-06 12:17:06 -04001002 if (has_alias) {
Jason Rhinelander7437c692017-07-28 22:03:44 -04001003 auto &instances = record.module_local ? registered_local_types_cpp() : get_internals().registered_types_cpp;
Wenzel Jakob86d825f2016-05-26 13:19:27 +02001004 instances[std::type_index(typeid(type_alias))] = instances[std::type_index(typeid(type))];
1005 }
Wenzel Jakob48548ea2016-01-17 22:36:44 +01001006 }
Wenzel Jakob38bd7112015-07-05 20:05:44 +02001007
Wenzel Jakobc1fc27e2016-09-13 00:36:43 +09001008 template <typename Base, detail::enable_if_t<is_base<Base>::value, int> = 0>
Wenzel Jakob8e5dceb2016-09-11 20:00:40 +09001009 static void add_base(detail::type_record &rec) {
Jason Rhinelander21966962017-06-21 13:38:10 -04001010 rec.add_base(typeid(Base), [](void *src) -> void * {
Wenzel Jakob8e5dceb2016-09-11 20:00:40 +09001011 return static_cast<Base *>(reinterpret_cast<type *>(src));
1012 });
1013 }
1014
Wenzel Jakobc1fc27e2016-09-13 00:36:43 +09001015 template <typename Base, detail::enable_if_t<!is_base<Base>::value, int> = 0>
Wenzel Jakob8e5dceb2016-09-11 20:00:40 +09001016 static void add_base(detail::type_record &) { }
1017
Wenzel Jakob71867832015-07-29 17:43:52 +02001018 template <typename Func, typename... Extra>
Wenzel Jakob48548ea2016-01-17 22:36:44 +01001019 class_ &def(const char *name_, Func&& f, const Extra&... extra) {
Jason Rhinelander23bf8942017-05-16 11:07:28 -04001020 cpp_function cf(method_adaptor<type>(std::forward<Func>(f)), name(name_), is_method(*this),
Dean Moldovan865e4302016-09-21 01:06:32 +02001021 sibling(getattr(*this, name_, none())), extra...);
Wenzel Jakob57082212015-09-04 23:42:12 +02001022 attr(cf.name()) = cf;
Wenzel Jakob38bd7112015-07-05 20:05:44 +02001023 return *this;
1024 }
1025
Wenzel Jakob71867832015-07-29 17:43:52 +02001026 template <typename Func, typename... Extra> class_ &
Jason Rhinelander139a0822017-03-12 17:59:07 -03001027 def_static(const char *name_, Func &&f, const Extra&... extra) {
1028 static_assert(!std::is_member_function_pointer<Func>::value,
1029 "def_static(...) called with a non-static member function pointer");
Dean Moldovan865e4302016-09-21 01:06:32 +02001030 cpp_function cf(std::forward<Func>(f), name(name_), scope(*this),
1031 sibling(getattr(*this, name_, none())), extra...);
Wenzel Jakob57082212015-09-04 23:42:12 +02001032 attr(cf.name()) = cf;
Wenzel Jakob38bd7112015-07-05 20:05:44 +02001033 return *this;
1034 }
1035
Wenzel Jakob71867832015-07-29 17:43:52 +02001036 template <detail::op_id id, detail::op_type ot, typename L, typename R, typename... Extra>
Wenzel Jakob48548ea2016-01-17 22:36:44 +01001037 class_ &def(const detail::op_<id, ot, L, R> &op, const Extra&... extra) {
Jason Rhinelander5fffe202016-09-06 12:17:06 -04001038 op.execute(*this, extra...);
Wenzel Jakob38bd7112015-07-05 20:05:44 +02001039 return *this;
1040 }
1041
Wenzel Jakob71867832015-07-29 17:43:52 +02001042 template <detail::op_id id, detail::op_type ot, typename L, typename R, typename... Extra>
Wenzel Jakob48548ea2016-01-17 22:36:44 +01001043 class_ & def_cast(const detail::op_<id, ot, L, R> &op, const Extra&... extra) {
Jason Rhinelander5fffe202016-09-06 12:17:06 -04001044 op.execute_cast(*this, extra...);
Wenzel Jakob38bd7112015-07-05 20:05:44 +02001045 return *this;
1046 }
1047
Wenzel Jakob71867832015-07-29 17:43:52 +02001048 template <typename... Args, typename... Extra>
Jason Rhinelanderc4e18002017-08-17 00:01:42 -04001049 class_ &def(const detail::initimpl::constructor<Args...> &init, const Extra&... extra) {
Jason Rhinelander5fffe202016-09-06 12:17:06 -04001050 init.execute(*this, extra...);
Wenzel Jakob38bd7112015-07-05 20:05:44 +02001051 return *this;
1052 }
1053
Wenzel Jakob86d825f2016-05-26 13:19:27 +02001054 template <typename... Args, typename... Extra>
Jason Rhinelanderc4e18002017-08-17 00:01:42 -04001055 class_ &def(const detail::initimpl::alias_constructor<Args...> &init, const Extra&... extra) {
Jason Rhinelander5fffe202016-09-06 12:17:06 -04001056 init.execute(*this, extra...);
Wenzel Jakob86d825f2016-05-26 13:19:27 +02001057 return *this;
1058 }
1059
Jason Rhinelander464d9892017-06-12 21:52:48 -04001060 template <typename... Args, typename... Extra>
1061 class_ &def(detail::initimpl::factory<Args...> &&init, const Extra&... extra) {
1062 std::move(init).execute(*this, extra...);
1063 return *this;
1064 }
1065
Wenzel Jakob71867832015-07-29 17:43:52 +02001066 template <typename Func> class_& def_buffer(Func &&func) {
Wenzel Jakob43398a82015-07-28 16:12:20 +02001067 struct capture { Func func; };
1068 capture *ptr = new capture { std::forward<Func>(func) };
1069 install_buffer_funcs([](PyObject *obj, void *ptr) -> buffer_info* {
Dean Moldovan5f07fac2017-01-03 11:52:05 +01001070 detail::make_caster<type> caster;
Wenzel Jakob38bd7112015-07-05 20:05:44 +02001071 if (!caster.load(obj, false))
1072 return nullptr;
Wenzel Jakob43398a82015-07-28 16:12:20 +02001073 return new buffer_info(((capture *) ptr)->func(caster));
1074 }, ptr);
Wenzel Jakob38bd7112015-07-05 20:05:44 +02001075 return *this;
1076 }
1077
Bruce Merryfe0cf8b2017-05-17 10:52:33 +02001078 template <typename Return, typename Class, typename... Args>
1079 class_ &def_buffer(Return (Class::*func)(Args...)) {
1080 return def_buffer([func] (type &obj) { return (obj.*func)(); });
1081 }
1082
1083 template <typename Return, typename Class, typename... Args>
1084 class_ &def_buffer(Return (Class::*func)(Args...) const) {
1085 return def_buffer([func] (const type &obj) { return (obj.*func)(); });
1086 }
1087
Wenzel Jakob71867832015-07-29 17:43:52 +02001088 template <typename C, typename D, typename... Extra>
Wenzel Jakob48548ea2016-01-17 22:36:44 +01001089 class_ &def_readwrite(const char *name, D C::*pm, const Extra&... extra) {
Jason Rhinelander23bf8942017-05-16 11:07:28 -04001090 static_assert(std::is_base_of<C, type>::value, "def_readwrite() requires a class member (or base class member)");
1091 cpp_function fget([pm](const type &c) -> const D &{ return c.*pm; }, is_method(*this)),
1092 fset([pm](type &c, const D &value) { c.*pm = value; }, is_method(*this));
Wenzel Jakob0b489582016-03-25 16:13:10 +01001093 def_property(name, fget, fset, return_value_policy::reference_internal, extra...);
Wenzel Jakob38bd7112015-07-05 20:05:44 +02001094 return *this;
1095 }
1096
Wenzel Jakob71867832015-07-29 17:43:52 +02001097 template <typename C, typename D, typename... Extra>
Wenzel Jakob48548ea2016-01-17 22:36:44 +01001098 class_ &def_readonly(const char *name, const D C::*pm, const Extra& ...extra) {
Jason Rhinelander23bf8942017-05-16 11:07:28 -04001099 static_assert(std::is_base_of<C, type>::value, "def_readonly() requires a class member (or base class member)");
1100 cpp_function fget([pm](const type &c) -> const D &{ return c.*pm; }, is_method(*this));
Wenzel Jakob0b489582016-03-25 16:13:10 +01001101 def_property_readonly(name, fget, return_value_policy::reference_internal, extra...);
Wenzel Jakob38bd7112015-07-05 20:05:44 +02001102 return *this;
1103 }
1104
Wenzel Jakob71867832015-07-29 17:43:52 +02001105 template <typename D, typename... Extra>
Wenzel Jakob48548ea2016-01-17 22:36:44 +01001106 class_ &def_readwrite_static(const char *name, D *pm, const Extra& ...extra) {
Wenzel Jakob0b489582016-03-25 16:13:10 +01001107 cpp_function fget([pm](object) -> const D &{ return *pm; }, scope(*this)),
1108 fset([pm](object, const D &value) { *pm = value; }, scope(*this));
1109 def_property_static(name, fget, fset, return_value_policy::reference, extra...);
Wenzel Jakob38bd7112015-07-05 20:05:44 +02001110 return *this;
1111 }
1112
Wenzel Jakob71867832015-07-29 17:43:52 +02001113 template <typename D, typename... Extra>
Wenzel Jakob48548ea2016-01-17 22:36:44 +01001114 class_ &def_readonly_static(const char *name, const D *pm, const Extra& ...extra) {
Wenzel Jakob0b489582016-03-25 16:13:10 +01001115 cpp_function fget([pm](object) -> const D &{ return *pm; }, scope(*this));
1116 def_property_readonly_static(name, fget, return_value_policy::reference, extra...);
Wenzel Jakob38bd7112015-07-05 20:05:44 +02001117 return *this;
1118 }
1119
Dean Moldovan03f627e2016-11-01 11:44:57 +01001120 /// Uses return_value_policy::reference_internal by default
1121 template <typename Getter, typename... Extra>
1122 class_ &def_property_readonly(const char *name, const Getter &fget, const Extra& ...extra) {
Jason Rhinelander23bf8942017-05-16 11:07:28 -04001123 return def_property_readonly(name, cpp_function(method_adaptor<type>(fget)),
1124 return_value_policy::reference_internal, extra...);
Dean Moldovan03f627e2016-11-01 11:44:57 +01001125 }
1126
1127 /// Uses cpp_function's return_value_policy by default
Wenzel Jakob84ec78f2016-03-21 17:54:24 +01001128 template <typename... Extra>
1129 class_ &def_property_readonly(const char *name, const cpp_function &fget, const Extra& ...extra) {
Dean Moldovan03f627e2016-11-01 11:44:57 +01001130 return def_property(name, fget, cpp_function(), extra...);
Wenzel Jakob38bd7112015-07-05 20:05:44 +02001131 }
1132
Dean Moldovan03f627e2016-11-01 11:44:57 +01001133 /// Uses return_value_policy::reference by default
1134 template <typename Getter, typename... Extra>
1135 class_ &def_property_readonly_static(const char *name, const Getter &fget, const Extra& ...extra) {
1136 return def_property_readonly_static(name, cpp_function(fget), return_value_policy::reference, extra...);
1137 }
1138
1139 /// Uses cpp_function's return_value_policy by default
Wenzel Jakob84ec78f2016-03-21 17:54:24 +01001140 template <typename... Extra>
1141 class_ &def_property_readonly_static(const char *name, const cpp_function &fget, const Extra& ...extra) {
Dean Moldovan03f627e2016-11-01 11:44:57 +01001142 return def_property_static(name, fget, cpp_function(), extra...);
Wenzel Jakob38bd7112015-07-05 20:05:44 +02001143 }
1144
Dean Moldovan03f627e2016-11-01 11:44:57 +01001145 /// Uses return_value_policy::reference_internal by default
Jason Rhinelander23bf8942017-05-16 11:07:28 -04001146 template <typename Getter, typename Setter, typename... Extra>
1147 class_ &def_property(const char *name, const Getter &fget, const Setter &fset, const Extra& ...extra) {
1148 return def_property(name, fget, cpp_function(method_adaptor<type>(fset)), extra...);
1149 }
Dean Moldovan03f627e2016-11-01 11:44:57 +01001150 template <typename Getter, typename... Extra>
1151 class_ &def_property(const char *name, const Getter &fget, const cpp_function &fset, const Extra& ...extra) {
Jason Rhinelander23bf8942017-05-16 11:07:28 -04001152 return def_property(name, cpp_function(method_adaptor<type>(fget)), fset,
1153 return_value_policy::reference_internal, extra...);
Dean Moldovan03f627e2016-11-01 11:44:57 +01001154 }
1155
1156 /// Uses cpp_function's return_value_policy by default
Wenzel Jakob84ec78f2016-03-21 17:54:24 +01001157 template <typename... Extra>
1158 class_ &def_property(const char *name, const cpp_function &fget, const cpp_function &fset, const Extra& ...extra) {
Wenzel Jakob0b489582016-03-25 16:13:10 +01001159 return def_property_static(name, fget, fset, is_method(*this), extra...);
Wenzel Jakob84ec78f2016-03-21 17:54:24 +01001160 }
1161
Dean Moldovan03f627e2016-11-01 11:44:57 +01001162 /// Uses return_value_policy::reference by default
1163 template <typename Getter, typename... Extra>
1164 class_ &def_property_static(const char *name, const Getter &fget, const cpp_function &fset, const Extra& ...extra) {
1165 return def_property_static(name, cpp_function(fget), fset, return_value_policy::reference, extra...);
1166 }
1167
1168 /// Uses cpp_function's return_value_policy by default
Wenzel Jakob84ec78f2016-03-21 17:54:24 +01001169 template <typename... Extra>
1170 class_ &def_property_static(const char *name, const cpp_function &fget, const cpp_function &fset, const Extra& ...extra) {
1171 auto rec_fget = get_function_record(fget), rec_fset = get_function_record(fset);
Wenzel Jakob7c99ff22016-06-02 20:33:01 +02001172 char *doc_prev = rec_fget->doc; /* 'extra' field may include a property-specific documentation string */
Wenzel Jakob84ec78f2016-03-21 17:54:24 +01001173 detail::process_attributes<Extra...>::init(extra..., rec_fget);
Wenzel Jakob7c99ff22016-06-02 20:33:01 +02001174 if (rec_fget->doc && rec_fget->doc != doc_prev) {
1175 free(doc_prev);
1176 rec_fget->doc = strdup(rec_fget->doc);
1177 }
1178 if (rec_fset) {
1179 doc_prev = rec_fset->doc;
Wenzel Jakob84ec78f2016-03-21 17:54:24 +01001180 detail::process_attributes<Extra...>::init(extra..., rec_fset);
Wenzel Jakob7c99ff22016-06-02 20:33:01 +02001181 if (rec_fset->doc && rec_fset->doc != doc_prev) {
1182 free(doc_prev);
1183 rec_fset->doc = strdup(rec_fset->doc);
1184 }
1185 }
Wenzel Jakob1d1f81b2016-12-16 15:00:46 +01001186 def_property_static_impl(name, fget, fset, rec_fget);
Wenzel Jakob38bd7112015-07-05 20:05:44 +02001187 return *this;
1188 }
Wenzel Jakoba2f6fde2015-10-01 16:46:03 +02001189
Wenzel Jakob38bd7112015-07-05 20:05:44 +02001190private:
Wenzel Jakobb2c2c792016-01-17 22:36:40 +01001191 /// Initialize holder object, variant 1: object derives from enable_shared_from_this
1192 template <typename T>
Jason Rhinelander353615f2017-07-25 00:53:23 -04001193 static void init_holder(detail::instance *inst, detail::value_and_holder &v_h,
Jason Rhinelandere45c2112017-02-22 21:36:09 -05001194 const holder_type * /* unused */, const std::enable_shared_from_this<T> * /* dummy */) {
Wenzel Jakob6e213c92015-11-24 23:05:58 +01001195 try {
Jason Rhinelandere45c2112017-02-22 21:36:09 -05001196 auto sh = std::dynamic_pointer_cast<typename holder_type::element_type>(
1197 v_h.value_ptr<type>()->shared_from_this());
Jason Rhinelanderb8ac4382017-05-19 13:34:55 -04001198 if (sh) {
Jason Rhinelandere45c2112017-02-22 21:36:09 -05001199 new (&v_h.holder<holder_type>()) holder_type(std::move(sh));
1200 v_h.set_holder_constructed();
Dean Moldovanab90ec62016-12-07 02:36:44 +01001201 }
Jason Rhinelanderb8ac4382017-05-19 13:34:55 -04001202 } catch (const std::bad_weak_ptr &) {}
Jason Rhinelandere45c2112017-02-22 21:36:09 -05001203
1204 if (!v_h.holder_constructed() && inst->owned) {
1205 new (&v_h.holder<holder_type>()) holder_type(v_h.value_ptr<type>());
1206 v_h.set_holder_constructed();
Wenzel Jakob6e213c92015-11-24 23:05:58 +01001207 }
Wenzel Jakobb2c2c792016-01-17 22:36:40 +01001208 }
1209
Jason Rhinelandere45c2112017-02-22 21:36:09 -05001210 static void init_holder_from_existing(const detail::value_and_holder &v_h,
1211 const holder_type *holder_ptr, std::true_type /*is_copy_constructible*/) {
1212 new (&v_h.holder<holder_type>()) holder_type(*reinterpret_cast<const holder_type *>(holder_ptr));
Wenzel Jakobb2c2c792016-01-17 22:36:40 +01001213 }
1214
Jason Rhinelandere45c2112017-02-22 21:36:09 -05001215 static void init_holder_from_existing(const detail::value_and_holder &v_h,
1216 const holder_type *holder_ptr, std::false_type /*is_copy_constructible*/) {
1217 new (&v_h.holder<holder_type>()) holder_type(std::move(*const_cast<holder_type *>(holder_ptr)));
Dean Moldovanec009a72017-01-31 17:05:44 +01001218 }
1219
1220 /// Initialize holder object, variant 2: try to construct from existing holder object, if possible
Jason Rhinelander353615f2017-07-25 00:53:23 -04001221 static void init_holder(detail::instance *inst, detail::value_and_holder &v_h,
Jason Rhinelandere45c2112017-02-22 21:36:09 -05001222 const holder_type *holder_ptr, const void * /* dummy -- not enable_shared_from_this<T>) */) {
Dean Moldovanec009a72017-01-31 17:05:44 +01001223 if (holder_ptr) {
Jason Rhinelandere45c2112017-02-22 21:36:09 -05001224 init_holder_from_existing(v_h, holder_ptr, std::is_copy_constructible<holder_type>());
1225 v_h.set_holder_constructed();
Dean Moldovanec009a72017-01-31 17:05:44 +01001226 } else if (inst->owned || detail::always_construct_holder<holder_type>::value) {
Jason Rhinelandere45c2112017-02-22 21:36:09 -05001227 new (&v_h.holder<holder_type>()) holder_type(v_h.value_ptr<type>());
1228 v_h.set_holder_constructed();
Jason Rhinelanderc07ec312016-11-06 13:12:48 -05001229 }
Wenzel Jakobb2c2c792016-01-17 22:36:40 +01001230 }
1231
Jason Rhinelander353615f2017-07-25 00:53:23 -04001232 /// Performs instance initialization including constructing a holder and registering the known
1233 /// instance. Should be called as soon as the `type` value_ptr is set for an instance. Takes an
1234 /// optional pointer to an existing holder to use; if not specified and the instance is
1235 /// `.owned`, a new holder will be constructed to manage the value pointer.
1236 static void init_instance(detail::instance *inst, const void *holder_ptr) {
Jason Rhinelandere45c2112017-02-22 21:36:09 -05001237 auto v_h = inst->get_value_and_holder(detail::get_type_info(typeid(type)));
Jason Rhinelandercca20a72017-07-29 03:56:01 -04001238 if (!v_h.instance_registered()) {
1239 register_instance(inst, v_h.value_ptr(), v_h.type);
1240 v_h.set_instance_registered();
1241 }
Jason Rhinelander353615f2017-07-25 00:53:23 -04001242 init_holder(inst, v_h, (const holder_type *) holder_ptr, v_h.value_ptr<type>());
Wenzel Jakob6e213c92015-11-24 23:05:58 +01001243 }
1244
Jason Rhinelandere45c2112017-02-22 21:36:09 -05001245 /// Deallocates an instance; via holder, if constructed; otherwise via operator delete.
Jason Rhinelander464d9892017-06-12 21:52:48 -04001246 static void dealloc(detail::value_and_holder &v_h) {
1247 if (v_h.holder_constructed()) {
Jason Rhinelandere45c2112017-02-22 21:36:09 -05001248 v_h.holder<holder_type>().~holder_type();
Jason Rhinelander464d9892017-06-12 21:52:48 -04001249 v_h.set_holder_constructed(false);
1250 }
1251 else {
Jason Rhinelandera03408c2017-07-23 00:32:58 -04001252 detail::call_operator_delete(v_h.value_ptr<type>(), v_h.type->type_size);
Jason Rhinelander464d9892017-06-12 21:52:48 -04001253 }
1254 v_h.value_ptr() = nullptr;
Wenzel Jakob38bd7112015-07-05 20:05:44 +02001255 }
Wenzel Jakob84ec78f2016-03-21 17:54:24 +01001256
1257 static detail::function_record *get_function_record(handle h) {
1258 h = detail::get_function(h);
Wenzel Jakob1d1f81b2016-12-16 15:00:46 +01001259 return h ? (detail::function_record *) reinterpret_borrow<capsule>(PyCFunction_GET_SELF(h.ptr()))
Dean Moldovanc7ac16b2016-10-28 03:08:15 +02001260 : nullptr;
Wenzel Jakob84ec78f2016-03-21 17:54:24 +01001261 }
Wenzel Jakob38bd7112015-07-05 20:05:44 +02001262};
1263
1264/// Binds C++ enumerations and enumeration classes to Python
1265template <typename Type> class enum_ : public class_<Type> {
1266public:
Wenzel Jakob8ac97152016-09-05 17:20:50 +09001267 using class_<Type>::def;
Matthieu Becaf936e12017-03-03 08:45:50 -08001268 using class_<Type>::def_property_readonly_static;
Wenzel Jakob405f6d12016-11-17 23:24:47 +01001269 using Scalar = typename std::underlying_type<Type>::type;
Wenzel Jakob405f6d12016-11-17 23:24:47 +01001270
Wenzel Jakob48548ea2016-01-17 22:36:44 +01001271 template <typename... Extra>
1272 enum_(const handle &scope, const char *name, const Extra&... extra)
Matthieu Becaf936e12017-03-03 08:45:50 -08001273 : class_<Type>(scope, name, extra...), m_entries(), m_parent(scope) {
Wenzel Jakob405f6d12016-11-17 23:24:47 +01001274
Dean Moldovan82ece942017-03-29 11:55:18 +02001275 constexpr bool is_arithmetic = detail::any_of<std::is_same<arithmetic, Extra>...>::value;
Wenzel Jakob405f6d12016-11-17 23:24:47 +01001276
Matthieu Becaf936e12017-03-03 08:45:50 -08001277 auto m_entries_ptr = m_entries.inc_ref().ptr();
1278 def("__repr__", [name, m_entries_ptr](Type value) -> pybind11::str {
1279 for (const auto &kv : reinterpret_borrow<dict>(m_entries_ptr)) {
1280 if (pybind11::cast<Type>(kv.second) == value)
1281 return pybind11::str("{}.{}").format(name, kv.first);
1282 }
1283 return pybind11::str("{}.???").format(name);
Wenzel Jakob38bd7112015-07-05 20:05:44 +02001284 });
Matthieu Becaf936e12017-03-03 08:45:50 -08001285 def_property_readonly_static("__members__", [m_entries_ptr](object /* self */) {
1286 dict m;
1287 for (const auto &kv : reinterpret_borrow<dict>(m_entries_ptr))
1288 m[kv.first] = kv.second;
1289 return m;
1290 }, return_value_policy::copy);
Wenzel Jakob405f6d12016-11-17 23:24:47 +01001291 def("__init__", [](Type& value, Scalar i) { value = (Type)i; });
Wenzel Jakob405f6d12016-11-17 23:24:47 +01001292 def("__int__", [](Type value) { return (Scalar) value; });
Wenzel Jakobe6fd2cd2017-04-28 14:46:52 +02001293 #if PY_MAJOR_VERSION < 3
1294 def("__long__", [](Type value) { return (Scalar) value; });
1295 #endif
Wenzel Jakob8ac97152016-09-05 17:20:50 +09001296 def("__eq__", [](const Type &value, Type *value2) { return value2 && value == *value2; });
1297 def("__ne__", [](const Type &value, Type *value2) { return !value2 || value != *value2; });
Wenzel Jakob405f6d12016-11-17 23:24:47 +01001298 if (is_arithmetic) {
1299 def("__lt__", [](const Type &value, Type *value2) { return value2 && value < *value2; });
1300 def("__gt__", [](const Type &value, Type *value2) { return value2 && value > *value2; });
1301 def("__le__", [](const Type &value, Type *value2) { return value2 && value <= *value2; });
1302 def("__ge__", [](const Type &value, Type *value2) { return value2 && value >= *value2; });
1303 }
1304 if (std::is_convertible<Type, Scalar>::value) {
Jason Rhinelanderd41a2732016-08-04 00:21:37 -04001305 // Don't provide comparison with the underlying type if the enum isn't convertible,
1306 // i.e. if Type is a scoped enum, mirroring the C++ behaviour. (NB: we explicitly
Wenzel Jakob405f6d12016-11-17 23:24:47 +01001307 // convert Type to Scalar below anyway because this needs to compile).
1308 def("__eq__", [](const Type &value, Scalar value2) { return (Scalar) value == value2; });
1309 def("__ne__", [](const Type &value, Scalar value2) { return (Scalar) value != value2; });
1310 if (is_arithmetic) {
1311 def("__lt__", [](const Type &value, Scalar value2) { return (Scalar) value < value2; });
1312 def("__gt__", [](const Type &value, Scalar value2) { return (Scalar) value > value2; });
1313 def("__le__", [](const Type &value, Scalar value2) { return (Scalar) value <= value2; });
1314 def("__ge__", [](const Type &value, Scalar value2) { return (Scalar) value >= value2; });
1315 def("__invert__", [](const Type &value) { return ~((Scalar) value); });
1316 def("__and__", [](const Type &value, Scalar value2) { return (Scalar) value & value2; });
1317 def("__or__", [](const Type &value, Scalar value2) { return (Scalar) value | value2; });
1318 def("__xor__", [](const Type &value, Scalar value2) { return (Scalar) value ^ value2; });
1319 def("__rand__", [](const Type &value, Scalar value2) { return (Scalar) value & value2; });
1320 def("__ror__", [](const Type &value, Scalar value2) { return (Scalar) value | value2; });
1321 def("__rxor__", [](const Type &value, Scalar value2) { return (Scalar) value ^ value2; });
1322 def("__and__", [](const Type &value, const Type &value2) { return (Scalar) value & (Scalar) value2; });
1323 def("__or__", [](const Type &value, const Type &value2) { return (Scalar) value | (Scalar) value2; });
1324 def("__xor__", [](const Type &value, const Type &value2) { return (Scalar) value ^ (Scalar) value2; });
1325 }
Jason Rhinelanderd41a2732016-08-04 00:21:37 -04001326 }
Wenzel Jakob405f6d12016-11-17 23:24:47 +01001327 def("__hash__", [](const Type &value) { return (Scalar) value; });
Wenzel Jakobfe342412016-09-06 13:02:29 +09001328 // Pickling and unpickling -- needed for use with the 'multiprocessing' module
Wenzel Jakob405f6d12016-11-17 23:24:47 +01001329 def("__getstate__", [](const Type &value) { return pybind11::make_tuple((Scalar) value); });
1330 def("__setstate__", [](Type &p, tuple t) { new (&p) Type((Type) t[0].cast<Scalar>()); });
Wenzel Jakob38bd7112015-07-05 20:05:44 +02001331 }
1332
1333 /// Export enumeration entries into the parent scope
Matthieu Becaf936e12017-03-03 08:45:50 -08001334 enum_& export_values() {
1335 for (const auto &kv : m_entries)
1336 m_parent.attr(kv.first) = kv.second;
Wenzel Jakobe916d842016-11-04 16:51:14 +01001337 return *this;
Wenzel Jakob38bd7112015-07-05 20:05:44 +02001338 }
1339
1340 /// Add an enumeration entry
1341 enum_& value(char const* name, Type value) {
Matthieu Becaf936e12017-03-03 08:45:50 -08001342 auto v = pybind11::cast(value, return_value_policy::copy);
1343 this->attr(name) = v;
1344 m_entries[pybind11::str(name)] = v;
Wenzel Jakob38bd7112015-07-05 20:05:44 +02001345 return *this;
1346 }
Matthieu Becaf936e12017-03-03 08:45:50 -08001347
Wenzel Jakob38bd7112015-07-05 20:05:44 +02001348private:
Matthieu Becaf936e12017-03-03 08:45:50 -08001349 dict m_entries;
Wenzel Jakob48548ea2016-01-17 22:36:44 +01001350 handle m_parent;
Wenzel Jakob38bd7112015-07-05 20:05:44 +02001351};
1352
Jason Rhinelander464d9892017-06-12 21:52:48 -04001353/// Binds an existing constructor taking arguments Args...
Jason Rhinelanderc4e18002017-08-17 00:01:42 -04001354template <typename... Args> detail::initimpl::constructor<Args...> init() { return {}; }
Jason Rhinelander464d9892017-06-12 21:52:48 -04001355/// Like `init<Args...>()`, but the instance is always constructed through the alias class (even
1356/// when not inheriting on the Python side).
Jason Rhinelanderc4e18002017-08-17 00:01:42 -04001357template <typename... Args> detail::initimpl::alias_constructor<Args...> init_alias() { return {}; }
Jason Rhinelander464d9892017-06-12 21:52:48 -04001358
1359/// Binds a factory function as a constructor
1360template <typename Func, typename Ret = detail::initimpl::factory_t<Func>>
1361Ret init(Func &&f) { return {std::forward<Func>(f)}; }
1362
1363/// Dual-argument factory function: the first function is called when no alias is needed, the second
1364/// when an alias is needed (i.e. due to python-side inheritance). Arguments must be identical.
1365template <typename CFunc, typename AFunc, typename Ret = detail::initimpl::factory_t<CFunc, AFunc>>
1366Ret init(CFunc &&c, AFunc &&a) {
1367 return {std::forward<CFunc>(c), std::forward<AFunc>(a)};
1368}
1369
Wenzel Jakob38bd7112015-07-05 20:05:44 +02001370NAMESPACE_BEGIN(detail)
Wenzel Jakob5f218b32016-01-17 22:36:39 +01001371
Jason Rhinelanderec62d972016-09-09 02:42:51 -04001372
Jason Rhinelanderf2ecd892016-08-10 12:08:04 -04001373inline void keep_alive_impl(handle nurse, handle patient) {
Wenzel Jakobb2c2c792016-01-17 22:36:40 +01001374 if (!nurse || !patient)
Wenzel Jakob678d7872016-01-17 22:36:41 +01001375 pybind11_fail("Could not activate keep_alive!");
Wenzel Jakob5f218b32016-01-17 22:36:39 +01001376
Ivan Smirnov984c7622016-08-29 02:38:47 +01001377 if (patient.is_none() || nurse.is_none())
Glen Walkerf45bb582016-08-16 17:50:43 +12001378 return; /* Nothing to keep alive or nothing to be kept alive by */
Wenzel Jakob9b880ba2016-04-25 03:25:13 +02001379
Bruce Merry9d698f72017-06-24 14:58:42 +02001380 auto tinfo = all_type_info(Py_TYPE(nurse.ptr()));
1381 if (!tinfo.empty()) {
1382 /* It's a pybind-registered type, so we can store the patient in the
1383 * internal list. */
1384 add_patient(nurse.ptr(), patient.ptr());
1385 }
1386 else {
1387 /* Fall back to clever approach based on weak references taken from
1388 * Boost.Python. This is not used for pybind-registered types because
1389 * the objects can be destroyed out-of-order in a GC pass. */
1390 cpp_function disable_lifesupport(
1391 [patient](handle weakref) { patient.dec_ref(); weakref.dec_ref(); });
Wenzel Jakob5f218b32016-01-17 22:36:39 +01001392
Bruce Merry9d698f72017-06-24 14:58:42 +02001393 weakref wr(nurse, disable_lifesupport);
Wenzel Jakob5f218b32016-01-17 22:36:39 +01001394
Bruce Merry9d698f72017-06-24 14:58:42 +02001395 patient.inc_ref(); /* reference patient and leak the weak reference */
1396 (void) wr.release();
1397 }
Wenzel Jakob5f218b32016-01-17 22:36:39 +01001398}
1399
Jason Rhinelanderbfcf9522017-01-30 13:34:38 -05001400PYBIND11_NOINLINE inline void keep_alive_impl(size_t Nurse, size_t Patient, function_call &call, handle ret) {
Jason Rhinelander2686da82017-01-21 23:42:14 -05001401 keep_alive_impl(
Jason Rhinelanderbfcf9522017-01-30 13:34:38 -05001402 Nurse == 0 ? ret : Nurse <= call.args.size() ? call.args[Nurse - 1] : handle(),
1403 Patient == 0 ? ret : Patient <= call.args.size() ? call.args[Patient - 1] : handle()
Jason Rhinelander2686da82017-01-21 23:42:14 -05001404 );
Jason Rhinelanderf2ecd892016-08-10 12:08:04 -04001405}
1406
Jason Rhinelandere45c2112017-02-22 21:36:09 -05001407inline std::pair<decltype(internals::registered_types_py)::iterator, bool> all_type_info_get_cache(PyTypeObject *type) {
1408 auto res = get_internals().registered_types_py
Jason Rhinelander12be4cd2017-07-29 03:53:45 -04001409#ifdef __cpp_lib_unordered_map_try_emplace
Jason Rhinelandere45c2112017-02-22 21:36:09 -05001410 .try_emplace(type);
1411#else
1412 .emplace(type, std::vector<detail::type_info *>());
1413#endif
1414 if (res.second) {
1415 // New cache entry created; set up a weak reference to automatically remove it if the type
1416 // gets destroyed:
1417 weakref((PyObject *) type, cpp_function([type](handle wr) {
1418 get_internals().registered_types_py.erase(type);
1419 wr.dec_ref();
1420 })).release();
1421 }
1422
1423 return res;
1424}
1425
Wenzel Jakobb212f6c2016-09-10 16:00:50 +09001426template <typename Iterator, typename Sentinel, bool KeyIterator, return_value_policy Policy>
Ivan Smirnov2b308e02016-08-24 23:29:04 +01001427struct iterator_state {
1428 Iterator it;
1429 Sentinel end;
Dean Moldovancaedf742017-06-09 16:49:04 +02001430 bool first_or_done;
Ivan Smirnovdaed1ab2016-06-17 22:29:39 +01001431};
Wenzel Jakobb2825952016-04-13 23:33:00 +02001432
Wenzel Jakob38bd7112015-07-05 20:05:44 +02001433NAMESPACE_END(detail)
1434
Jason Rhinelander3d591e82017-03-18 13:34:21 -03001435/// Makes a python iterator from a first and past-the-end C++ InputIterator.
Wenzel Jakobb212f6c2016-09-10 16:00:50 +09001436template <return_value_policy Policy = return_value_policy::reference_internal,
1437 typename Iterator,
Ivan Smirnov2b308e02016-08-24 23:29:04 +01001438 typename Sentinel,
Wenzel Jakob5dd33d82016-05-30 11:28:21 +02001439 typename ValueType = decltype(*std::declval<Iterator>()),
1440 typename... Extra>
Ivan Smirnov2b308e02016-08-24 23:29:04 +01001441iterator make_iterator(Iterator first, Sentinel last, Extra &&... extra) {
Wenzel Jakobb212f6c2016-09-10 16:00:50 +09001442 typedef detail::iterator_state<Iterator, Sentinel, false, Policy> state;
Wenzel Jakobb2825952016-04-13 23:33:00 +02001443
Wenzel Jakob8e5dceb2016-09-11 20:00:40 +09001444 if (!detail::get_type_info(typeid(state), false)) {
Jason Rhinelander7437c692017-07-28 22:03:44 -04001445 class_<state>(handle(), "iterator", pybind11::module_local())
Wenzel Jakobb2825952016-04-13 23:33:00 +02001446 .def("__iter__", [](state &s) -> state& { return s; })
Wenzel Jakob5dd33d82016-05-30 11:28:21 +02001447 .def("__next__", [](state &s) -> ValueType {
Dean Moldovancaedf742017-06-09 16:49:04 +02001448 if (!s.first_or_done)
Ivan Smirnovdaed1ab2016-06-17 22:29:39 +01001449 ++s.it;
1450 else
Dean Moldovancaedf742017-06-09 16:49:04 +02001451 s.first_or_done = false;
1452 if (s.it == s.end) {
1453 s.first_or_done = true;
Wenzel Jakobb2825952016-04-13 23:33:00 +02001454 throw stop_iteration();
Dean Moldovancaedf742017-06-09 16:49:04 +02001455 }
Ivan Smirnovdaed1ab2016-06-17 22:29:39 +01001456 return *s.it;
Wenzel Jakobb212f6c2016-09-10 16:00:50 +09001457 }, std::forward<Extra>(extra)..., Policy);
Wenzel Jakobb2825952016-04-13 23:33:00 +02001458 }
1459
Dean Moldovancaedf742017-06-09 16:49:04 +02001460 return cast(state{first, last, true});
Wenzel Jakobb2825952016-04-13 23:33:00 +02001461}
Wenzel Jakob146397e2016-09-06 13:06:31 +09001462
Jason Rhinelander3d591e82017-03-18 13:34:21 -03001463/// Makes an python iterator over the keys (`.first`) of a iterator over pairs from a
1464/// first and past-the-end InputIterator.
Wenzel Jakobb212f6c2016-09-10 16:00:50 +09001465template <return_value_policy Policy = return_value_policy::reference_internal,
1466 typename Iterator,
Ivan Smirnov2b308e02016-08-24 23:29:04 +01001467 typename Sentinel,
Ivan Smirnovc5a1c8a2016-08-24 23:27:19 +01001468 typename KeyType = decltype((*std::declval<Iterator>()).first),
Jason Rhinelander5aa85be2016-08-11 21:22:05 -04001469 typename... Extra>
Ivan Smirnov2b308e02016-08-24 23:29:04 +01001470iterator make_key_iterator(Iterator first, Sentinel last, Extra &&... extra) {
Wenzel Jakobb212f6c2016-09-10 16:00:50 +09001471 typedef detail::iterator_state<Iterator, Sentinel, true, Policy> state;
Jason Rhinelander5aa85be2016-08-11 21:22:05 -04001472
Wenzel Jakob8e5dceb2016-09-11 20:00:40 +09001473 if (!detail::get_type_info(typeid(state), false)) {
Jason Rhinelander7437c692017-07-28 22:03:44 -04001474 class_<state>(handle(), "iterator", pybind11::module_local())
Jason Rhinelander5aa85be2016-08-11 21:22:05 -04001475 .def("__iter__", [](state &s) -> state& { return s; })
1476 .def("__next__", [](state &s) -> KeyType {
Dean Moldovancaedf742017-06-09 16:49:04 +02001477 if (!s.first_or_done)
Jason Rhinelander5aa85be2016-08-11 21:22:05 -04001478 ++s.it;
1479 else
Dean Moldovancaedf742017-06-09 16:49:04 +02001480 s.first_or_done = false;
1481 if (s.it == s.end) {
1482 s.first_or_done = true;
Jason Rhinelander5aa85be2016-08-11 21:22:05 -04001483 throw stop_iteration();
Dean Moldovancaedf742017-06-09 16:49:04 +02001484 }
Ivan Smirnovc5a1c8a2016-08-24 23:27:19 +01001485 return (*s.it).first;
Wenzel Jakobb212f6c2016-09-10 16:00:50 +09001486 }, std::forward<Extra>(extra)..., Policy);
Jason Rhinelander5aa85be2016-08-11 21:22:05 -04001487 }
1488
Dean Moldovancaedf742017-06-09 16:49:04 +02001489 return cast(state{first, last, true});
Jason Rhinelander5aa85be2016-08-11 21:22:05 -04001490}
Wenzel Jakobb2825952016-04-13 23:33:00 +02001491
Jason Rhinelander3d591e82017-03-18 13:34:21 -03001492/// Makes an iterator over values of an stl container or other container supporting
1493/// `std::begin()`/`std::end()`
Wenzel Jakobb212f6c2016-09-10 16:00:50 +09001494template <return_value_policy Policy = return_value_policy::reference_internal,
1495 typename Type, typename... Extra> iterator make_iterator(Type &value, Extra&&... extra) {
1496 return make_iterator<Policy>(std::begin(value), std::end(value), extra...);
Wenzel Jakobbf0c7dc2016-04-18 10:52:12 +02001497}
1498
Jason Rhinelander3d591e82017-03-18 13:34:21 -03001499/// Makes an iterator over the keys (`.first`) of a stl map-like container supporting
1500/// `std::begin()`/`std::end()`
Wenzel Jakobb212f6c2016-09-10 16:00:50 +09001501template <return_value_policy Policy = return_value_policy::reference_internal,
1502 typename Type, typename... Extra> iterator make_key_iterator(Type &value, Extra&&... extra) {
1503 return make_key_iterator<Policy>(std::begin(value), std::end(value), extra...);
Jason Rhinelander5aa85be2016-08-11 21:22:05 -04001504}
1505
Wenzel Jakob38bd7112015-07-05 20:05:44 +02001506template <typename InputType, typename OutputType> void implicitly_convertible() {
Wenzel Jakob2ac50442016-01-17 22:36:35 +01001507 auto implicit_caster = [](PyObject *obj, PyTypeObject *type) -> PyObject * {
Dean Moldovan5f07fac2017-01-03 11:52:05 +01001508 if (!detail::make_caster<InputType>().load(obj, false))
Wenzel Jakob38bd7112015-07-05 20:05:44 +02001509 return nullptr;
1510 tuple args(1);
1511 args[0] = obj;
1512 PyObject *result = PyObject_Call((PyObject *) type, args.ptr(), nullptr);
1513 if (result == nullptr)
1514 PyErr_Clear();
1515 return result;
1516 };
Ivan Smirnov8f3e0452016-10-23 15:43:03 +01001517
1518 if (auto tinfo = detail::get_type_info(typeid(OutputType)))
1519 tinfo->implicit_conversions.push_back(implicit_caster);
1520 else
Wenzel Jakob678d7872016-01-17 22:36:41 +01001521 pybind11_fail("implicitly_convertible: Unable to find type " + type_id<OutputType>());
Wenzel Jakob38bd7112015-07-05 20:05:44 +02001522}
1523
Pim Schellart5a7d17f2016-06-17 17:35:59 -04001524template <typename ExceptionTranslator>
1525void register_exception_translator(ExceptionTranslator&& translator) {
1526 detail::get_internals().registered_exception_translators.push_front(
1527 std::forward<ExceptionTranslator>(translator));
1528}
1529
Jason Rhinelander37b23832017-05-22 12:06:16 -04001530/**
1531 * Wrapper to generate a new Python exception type.
Pim Schellart5a7d17f2016-06-17 17:35:59 -04001532 *
1533 * This should only be used with PyErr_SetString for now.
1534 * It is not (yet) possible to use as a py::base.
1535 * Template type argument is reserved for future use.
1536 */
1537template <typename type>
1538class exception : public object {
1539public:
Wenzel Jakob281ccc62016-11-16 17:59:56 +01001540 exception(handle scope, const char *name, PyObject *base = PyExc_Exception) {
1541 std::string full_name = scope.attr("__name__").cast<std::string>() +
1542 std::string(".") + name;
Matthew Woehlkee15fa9f2017-02-08 17:43:08 -05001543 m_ptr = PyErr_NewException(const_cast<char *>(full_name.c_str()), base, NULL);
Wenzel Jakob281ccc62016-11-16 17:59:56 +01001544 if (hasattr(scope, name))
1545 pybind11_fail("Error during initialization: multiple incompatible "
1546 "definitions with name \"" + std::string(name) + "\"");
1547 scope.attr(name) = *this;
Pim Schellart5a7d17f2016-06-17 17:35:59 -04001548 }
Jason Rhinelanderb3794f12016-09-16 02:04:15 -04001549
1550 // Sets the current python exception to this exception object with the given message
1551 void operator()(const char *message) {
1552 PyErr_SetString(m_ptr, message);
1553 }
Pim Schellart5a7d17f2016-06-17 17:35:59 -04001554};
1555
Jason Rhinelander37b23832017-05-22 12:06:16 -04001556/**
1557 * Registers a Python exception in `m` of the given `name` and installs an exception translator to
Jason Rhinelanderb3794f12016-09-16 02:04:15 -04001558 * translate the C++ exception to the created Python exception using the exceptions what() method.
1559 * This is intended for simple exception translations; for more complex translation, register the
1560 * exception object and translator directly.
1561 */
Wenzel Jakob281ccc62016-11-16 17:59:56 +01001562template <typename CppException>
1563exception<CppException> &register_exception(handle scope,
1564 const char *name,
1565 PyObject *base = PyExc_Exception) {
1566 static exception<CppException> ex(scope, name, base);
Jason Rhinelanderb3794f12016-09-16 02:04:15 -04001567 register_exception_translator([](std::exception_ptr p) {
1568 if (!p) return;
1569 try {
1570 std::rethrow_exception(p);
Wenzel Jakob281ccc62016-11-16 17:59:56 +01001571 } catch (const CppException &e) {
Jason Rhinelanderb3794f12016-09-16 02:04:15 -04001572 ex(e.what());
1573 }
1574 });
1575 return ex;
1576}
1577
Dean Moldovan67990d92016-08-29 18:03:34 +02001578NAMESPACE_BEGIN(detail)
1579PYBIND11_NOINLINE inline void print(tuple args, dict kwargs) {
1580 auto strings = tuple(args.size());
1581 for (size_t i = 0; i < args.size(); ++i) {
Dean Moldovane18bc022016-10-25 22:12:39 +02001582 strings[i] = str(args[i]);
Dean Moldovan67990d92016-08-29 18:03:34 +02001583 }
Dean Moldovan865e4302016-09-21 01:06:32 +02001584 auto sep = kwargs.contains("sep") ? kwargs["sep"] : cast(" ");
Dean Moldovan242b1462016-09-08 17:02:04 +02001585 auto line = sep.attr("join")(strings);
Dean Moldovan67990d92016-08-29 18:03:34 +02001586
Wenzel Jakobc49d6e52016-10-13 10:34:52 +02001587 object file;
1588 if (kwargs.contains("file")) {
1589 file = kwargs["file"].cast<object>();
1590 } else {
1591 try {
1592 file = module::import("sys").attr("stdout");
esquires67a68f12016-12-01 05:35:34 -05001593 } catch (const error_already_set &) {
Wenzel Jakobc49d6e52016-10-13 10:34:52 +02001594 /* If print() is called from code that is executed as
1595 part of garbage collection during interpreter shutdown,
1596 importing 'sys' can fail. Give up rather than crashing the
1597 interpreter in this case. */
1598 return;
1599 }
1600 }
1601
Dean Moldovan242b1462016-09-08 17:02:04 +02001602 auto write = file.attr("write");
Dean Moldovan67990d92016-08-29 18:03:34 +02001603 write(line);
Dean Moldovan865e4302016-09-21 01:06:32 +02001604 write(kwargs.contains("end") ? kwargs["end"] : cast("\n"));
Dean Moldovan67990d92016-08-29 18:03:34 +02001605
Wenzel Jakobc49d6e52016-10-13 10:34:52 +02001606 if (kwargs.contains("flush") && kwargs["flush"].cast<bool>())
Dean Moldovan242b1462016-09-08 17:02:04 +02001607 file.attr("flush")();
Dean Moldovan67990d92016-08-29 18:03:34 +02001608}
1609NAMESPACE_END(detail)
1610
1611template <return_value_policy policy = return_value_policy::automatic_reference, typename... Args>
1612void print(Args &&...args) {
1613 auto c = detail::collect_arguments<policy>(std::forward<Args>(args)...);
1614 detail::print(c.args(), c.kwargs());
1615}
1616
Wenzel Jakob1d1f81b2016-12-16 15:00:46 +01001617#if defined(WITH_THREAD) && !defined(PYPY_VERSION)
Wenzel Jakob39e97e62016-04-25 03:26:15 +02001618
1619/* The functions below essentially reproduce the PyGILState_* API using a RAII
1620 * pattern, but there are a few important differences:
1621 *
1622 * 1. When acquiring the GIL from an non-main thread during the finalization
1623 * phase, the GILState API blindly terminates the calling thread, which
1624 * is often not what is wanted. This API does not do this.
1625 *
1626 * 2. The gil_scoped_release function can optionally cut the relationship
1627 * of a PyThreadState and its associated thread, which allows moving it to
1628 * another thread (this is a fairly rare/advanced use case).
1629 *
1630 * 3. The reference count of an acquired thread state can be controlled. This
1631 * can be handy to prevent cases where callbacks issued from an external
Wenzel Jakobfbafdea2016-04-25 15:02:43 +02001632 * thread would otherwise constantly construct and destroy thread state data
1633 * structures.
Wenzel Jakob69e1a5c2016-05-26 14:29:31 +02001634 *
1635 * See the Python bindings of NanoGUI (http://github.com/wjakob/nanogui) for an
1636 * example which uses features 2 and 3 to migrate the Python thread of
1637 * execution to another thread (to run the event loop on the original thread,
1638 * in this case).
Wenzel Jakobfbafdea2016-04-25 15:02:43 +02001639 */
Wenzel Jakob38bd7112015-07-05 20:05:44 +02001640
1641class gil_scoped_acquire {
Wenzel Jakob38bd7112015-07-05 20:05:44 +02001642public:
Wenzel Jakobfbafdea2016-04-25 15:02:43 +02001643 PYBIND11_NOINLINE gil_scoped_acquire() {
Wenzel Jakob39e97e62016-04-25 03:26:15 +02001644 auto const &internals = detail::get_internals();
1645 tstate = (PyThreadState *) PyThread_get_key_value(internals.tstate);
1646
1647 if (!tstate) {
1648 tstate = PyThreadState_New(internals.istate);
1649 #if !defined(NDEBUG)
1650 if (!tstate)
1651 pybind11_fail("scoped_acquire: could not create thread state!");
1652 #endif
1653 tstate->gilstate_counter = 0;
Wenzel Jakobfbafdea2016-04-25 15:02:43 +02001654 #if PY_MAJOR_VERSION < 3
1655 PyThread_delete_key_value(internals.tstate);
1656 #endif
Wenzel Jakob39e97e62016-04-25 03:26:15 +02001657 PyThread_set_key_value(internals.tstate, tstate);
1658 } else {
Wenzel Jakobfbafdea2016-04-25 15:02:43 +02001659 release = detail::get_thread_state_unchecked() != tstate;
Wenzel Jakob39e97e62016-04-25 03:26:15 +02001660 }
1661
1662 if (release) {
Wenzel Jakob39e97e62016-04-25 03:26:15 +02001663 /* Work around an annoying assertion in PyThreadState_Swap */
Wenzel Jakobfbafdea2016-04-25 15:02:43 +02001664 #if defined(Py_DEBUG)
1665 PyInterpreterState *interp = tstate->interp;
1666 tstate->interp = nullptr;
1667 #endif
Wenzel Jakob39e97e62016-04-25 03:26:15 +02001668 PyEval_AcquireThread(tstate);
Wenzel Jakobfbafdea2016-04-25 15:02:43 +02001669 #if defined(Py_DEBUG)
1670 tstate->interp = interp;
1671 #endif
Wenzel Jakob39e97e62016-04-25 03:26:15 +02001672 }
1673
1674 inc_ref();
1675 }
1676
1677 void inc_ref() {
1678 ++tstate->gilstate_counter;
1679 }
1680
Wenzel Jakobfbafdea2016-04-25 15:02:43 +02001681 PYBIND11_NOINLINE void dec_ref() {
Wenzel Jakob39e97e62016-04-25 03:26:15 +02001682 --tstate->gilstate_counter;
1683 #if !defined(NDEBUG)
Wenzel Jakobfbafdea2016-04-25 15:02:43 +02001684 if (detail::get_thread_state_unchecked() != tstate)
Wenzel Jakob39e97e62016-04-25 03:26:15 +02001685 pybind11_fail("scoped_acquire::dec_ref(): thread state must be current!");
1686 if (tstate->gilstate_counter < 0)
1687 pybind11_fail("scoped_acquire::dec_ref(): reference count underflow!");
1688 #endif
1689 if (tstate->gilstate_counter == 0) {
1690 #if !defined(NDEBUG)
1691 if (!release)
1692 pybind11_fail("scoped_acquire::dec_ref(): internal error!");
1693 #endif
1694 PyThreadState_Clear(tstate);
1695 PyThreadState_DeleteCurrent();
Wenzel Jakobfbafdea2016-04-25 15:02:43 +02001696 PyThread_delete_key_value(detail::get_internals().tstate);
Wenzel Jakob39e97e62016-04-25 03:26:15 +02001697 release = false;
1698 }
1699 }
1700
Wenzel Jakobfbafdea2016-04-25 15:02:43 +02001701 PYBIND11_NOINLINE ~gil_scoped_acquire() {
Wenzel Jakob39e97e62016-04-25 03:26:15 +02001702 dec_ref();
1703 if (release)
1704 PyEval_SaveThread();
1705 }
1706private:
1707 PyThreadState *tstate = nullptr;
1708 bool release = true;
Wenzel Jakob38bd7112015-07-05 20:05:44 +02001709};
1710
1711class gil_scoped_release {
Wenzel Jakob38bd7112015-07-05 20:05:44 +02001712public:
Jason Rhinelander12d76602016-10-16 16:27:42 -04001713 explicit gil_scoped_release(bool disassoc = false) : disassoc(disassoc) {
Dean Moldovan2d6116b2017-06-20 23:50:51 +02001714 // `get_internals()` must be called here unconditionally in order to initialize
1715 // `internals.tstate` for subsequent `gil_scoped_acquire` calls. Otherwise, an
1716 // initialization race could occur as multiple threads try `gil_scoped_acquire`.
1717 const auto &internals = detail::get_internals();
Wenzel Jakob39e97e62016-04-25 03:26:15 +02001718 tstate = PyEval_SaveThread();
Wenzel Jakob2f6662e2016-04-25 09:16:41 +02001719 if (disassoc) {
Dean Moldovan2d6116b2017-06-20 23:50:51 +02001720 auto key = internals.tstate;
Wenzel Jakob2f6662e2016-04-25 09:16:41 +02001721 #if PY_MAJOR_VERSION < 3
1722 PyThread_delete_key_value(key);
1723 #else
1724 PyThread_set_key_value(key, nullptr);
1725 #endif
1726 }
Wenzel Jakob39e97e62016-04-25 03:26:15 +02001727 }
1728 ~gil_scoped_release() {
1729 if (!tstate)
1730 return;
1731 PyEval_RestoreThread(tstate);
Wenzel Jakob2f6662e2016-04-25 09:16:41 +02001732 if (disassoc) {
Boris Schäling20ee9352016-05-28 12:26:18 +02001733 auto key = detail::get_internals().tstate;
Wenzel Jakob2f6662e2016-04-25 09:16:41 +02001734 #if PY_MAJOR_VERSION < 3
1735 PyThread_delete_key_value(key);
1736 #endif
1737 PyThread_set_key_value(key, tstate);
1738 }
Wenzel Jakob39e97e62016-04-25 03:26:15 +02001739 }
1740private:
1741 PyThreadState *tstate;
1742 bool disassoc;
Wenzel Jakob38bd7112015-07-05 20:05:44 +02001743};
Wenzel Jakob1d1f81b2016-12-16 15:00:46 +01001744#elif defined(PYPY_VERSION)
1745class gil_scoped_acquire {
1746 PyGILState_STATE state;
1747public:
1748 gil_scoped_acquire() { state = PyGILState_Ensure(); }
1749 ~gil_scoped_acquire() { PyGILState_Release(state); }
1750};
1751
1752class gil_scoped_release {
1753 PyThreadState *state;
1754public:
1755 gil_scoped_release() { state = PyEval_SaveThread(); }
1756 ~gil_scoped_release() { PyEval_RestoreThread(state); }
1757};
Wenzel Jakob39e97e62016-04-25 03:26:15 +02001758#else
1759class gil_scoped_acquire { };
1760class gil_scoped_release { };
Felipe Lema2547ca42016-01-25 16:22:44 -03001761#endif
Wenzel Jakob38bd7112015-07-05 20:05:44 +02001762
Wenzel Jakob8d396fc2016-11-24 23:11:02 +01001763error_already_set::~error_already_set() {
Jason Rhinelander1682b672017-07-20 23:14:33 -04001764 if (type) {
Wenzel Jakob8d396fc2016-11-24 23:11:02 +01001765 gil_scoped_acquire gil;
Jason Rhinelander1682b672017-07-20 23:14:33 -04001766 type.release().dec_ref();
1767 value.release().dec_ref();
1768 trace.release().dec_ref();
Wenzel Jakob8d396fc2016-11-24 23:11:02 +01001769 }
1770}
1771
Jason Rhinelander1b05ce52016-08-09 17:57:59 -04001772inline function get_type_overload(const void *this_ptr, const detail::type_info *this_type, const char *name) {
Wenzel Jakob1d1f81b2016-12-16 15:00:46 +01001773 handle self = detail::get_object_handle(this_ptr, this_type);
1774 if (!self)
Wenzel Jakobac0fde92015-10-01 18:37:26 +02001775 return function();
Wenzel Jakob1d1f81b2016-12-16 15:00:46 +01001776 handle type = self.get_type();
Wenzel Jakoba2f6fde2015-10-01 16:46:03 +02001777 auto key = std::make_pair(type.ptr(), name);
1778
Wenzel Jakobf5c154a2016-04-11 18:13:08 +02001779 /* Cache functions that aren't overloaded in Python to avoid
1780 many costly Python dictionary lookups below */
Wenzel Jakoba2f6fde2015-10-01 16:46:03 +02001781 auto &cache = detail::get_internals().inactive_overload_cache;
1782 if (cache.find(key) != cache.end())
1783 return function();
1784
Wenzel Jakob1d1f81b2016-12-16 15:00:46 +01001785 function overload = getattr(self, name, function());
Wenzel Jakoba2f6fde2015-10-01 16:46:03 +02001786 if (overload.is_cpp_function()) {
1787 cache.insert(key);
1788 return function();
1789 }
Wenzel Jakob27e8e102016-01-17 22:36:37 +01001790
Wenzel Jakob1d1f81b2016-12-16 15:00:46 +01001791 /* Don't call dispatch code if invoked from overridden function.
1792 Unfortunately this doesn't work on PyPy. */
1793#if !defined(PYPY_VERSION)
Wenzel Jakoba2f6fde2015-10-01 16:46:03 +02001794 PyFrameObject *frame = PyThreadState_Get()->frame;
Dean Moldovane18bc022016-10-25 22:12:39 +02001795 if (frame && (std::string) str(frame->f_code->co_name) == name &&
Wenzel Jakobf5c154a2016-04-11 18:13:08 +02001796 frame->f_code->co_argcount > 0) {
1797 PyFrame_FastToLocals(frame);
1798 PyObject *self_caller = PyDict_GetItem(
1799 frame->f_locals, PyTuple_GET_ITEM(frame->f_code->co_varnames, 0));
Wenzel Jakob1d1f81b2016-12-16 15:00:46 +01001800 if (self_caller == self.ptr())
Wenzel Jakobf5c154a2016-04-11 18:13:08 +02001801 return function();
1802 }
Wenzel Jakob1d1f81b2016-12-16 15:00:46 +01001803#else
1804 /* PyPy currently doesn't provide a detailed cpyext emulation of
1805 frame objects, so we have to emulate this using Python. This
1806 is going to be slow..*/
1807 dict d; d["self"] = self; d["name"] = pybind11::str(name);
1808 PyObject *result = PyRun_String(
1809 "import inspect\n"
1810 "frame = inspect.currentframe()\n"
1811 "if frame is not None:\n"
1812 " frame = frame.f_back\n"
1813 " if frame is not None and str(frame.f_code.co_name) == name and "
1814 "frame.f_code.co_argcount > 0:\n"
1815 " self_caller = frame.f_locals[frame.f_code.co_varnames[0]]\n"
1816 " if self_caller == self:\n"
1817 " self = None\n",
1818 Py_file_input, d.ptr(), d.ptr());
1819 if (result == nullptr)
1820 throw error_already_set();
Dean Moldovan36f0a152017-02-08 01:01:56 +01001821 if (d["self"].is_none())
Wenzel Jakob1d1f81b2016-12-16 15:00:46 +01001822 return function();
1823 Py_DECREF(result);
1824#endif
1825
Wenzel Jakoba2f6fde2015-10-01 16:46:03 +02001826 return overload;
1827}
1828
Jason Rhinelander1b05ce52016-08-09 17:57:59 -04001829template <class T> function get_overload(const T *this_ptr, const char *name) {
Ivan Smirnov8f3e0452016-10-23 15:43:03 +01001830 auto tinfo = detail::get_type_info(typeid(T));
1831 return tinfo ? get_type_overload(this_ptr, tinfo, name) : function();
Jason Rhinelander1b05ce52016-08-09 17:57:59 -04001832}
1833
Jason Rhinelander20978262016-08-29 18:16:46 -04001834#define PYBIND11_OVERLOAD_INT(ret_type, cname, name, ...) { \
Wenzel Jakob8f4eb002015-10-15 18:13:33 +02001835 pybind11::gil_scoped_acquire gil; \
Jason Rhinelander20978262016-08-29 18:16:46 -04001836 pybind11::function overload = pybind11::get_overload(static_cast<const cname *>(this), name); \
Jason Rhinelander7dfb9322016-09-08 14:49:43 -04001837 if (overload) { \
Jason Rhinelander3e4fe6c2016-09-11 12:17:41 -04001838 auto o = overload(__VA_ARGS__); \
Jason Rhinelander7dfb9322016-09-08 14:49:43 -04001839 if (pybind11::detail::cast_is_temporary_value_reference<ret_type>::value) { \
Jason Rhinelander3e4fe6c2016-09-11 12:17:41 -04001840 static pybind11::detail::overload_caster_t<ret_type> caster; \
1841 return pybind11::detail::cast_ref<ret_type>(std::move(o), caster); \
Jason Rhinelander7dfb9322016-09-08 14:49:43 -04001842 } \
1843 else return pybind11::detail::cast_safe<ret_type>(std::move(o)); \
1844 } \
1845 }
Wenzel Jakoba2f6fde2015-10-01 16:46:03 +02001846
Wenzel Jakob1e3be732016-05-24 23:42:05 +02001847#define PYBIND11_OVERLOAD_NAME(ret_type, cname, name, fn, ...) \
Jason Rhinelander20978262016-08-29 18:16:46 -04001848 PYBIND11_OVERLOAD_INT(ret_type, cname, name, __VA_ARGS__) \
Wenzel Jakob1e3be732016-05-24 23:42:05 +02001849 return cname::fn(__VA_ARGS__)
Wenzel Jakoba2f6fde2015-10-01 16:46:03 +02001850
Wenzel Jakob1e3be732016-05-24 23:42:05 +02001851#define PYBIND11_OVERLOAD_PURE_NAME(ret_type, cname, name, fn, ...) \
Jason Rhinelander20978262016-08-29 18:16:46 -04001852 PYBIND11_OVERLOAD_INT(ret_type, cname, name, __VA_ARGS__) \
Wenzel Jakob1e3be732016-05-24 23:42:05 +02001853 pybind11::pybind11_fail("Tried to call pure virtual function \"" #cname "::" name "\"");
1854
1855#define PYBIND11_OVERLOAD(ret_type, cname, fn, ...) \
1856 PYBIND11_OVERLOAD_NAME(ret_type, cname, #fn, fn, __VA_ARGS__)
1857
1858#define PYBIND11_OVERLOAD_PURE(ret_type, cname, fn, ...) \
1859 PYBIND11_OVERLOAD_PURE_NAME(ret_type, cname, #fn, fn, __VA_ARGS__)
Wenzel Jakoba2f6fde2015-10-01 16:46:03 +02001860
Jason Rhinelandera859dd62017-08-10 12:03:29 -04001861NAMESPACE_END(PYBIND11_NAMESPACE)
Wenzel Jakob38bd7112015-07-05 20:05:44 +02001862
1863#if defined(_MSC_VER)
Wenzel Jakob48548ea2016-01-17 22:36:44 +01001864# pragma warning(pop)
Wenzel Jakob1ffce742016-08-25 01:43:33 +02001865#elif defined(__INTEL_COMPILER)
1866/* Leave ignored warnings on */
Wenzel Jakob6d252962016-05-01 20:47:49 +02001867#elif defined(__GNUG__) && !defined(__clang__)
Wenzel Jakob48548ea2016-01-17 22:36:44 +01001868# pragma GCC diagnostic pop
Wenzel Jakob38bd7112015-07-05 20:05:44 +02001869#endif