db325e0c5354ea056d47912cd9523f58d287a5c6
[gem5.git] / ext / pybind11 / include / pybind11 / pybind11.h
1 /*
2 pybind11/pybind11.h: Main header file of the C++11 python
3 binding generator library
4
5 Copyright (c) 2016 Wenzel Jakob <wenzel.jakob@epfl.ch>
6
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
11 #pragma once
12
13 #if defined(_MSC_VER)
14 # pragma warning(push)
15 # pragma warning(disable: 4100) // warning C4100: Unreferenced formal parameter
16 # pragma warning(disable: 4127) // warning C4127: Conditional expression is constant
17 # pragma warning(disable: 4512) // warning C4512: Assignment operator was implicitly defined as deleted
18 # 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
20 # pragma warning(disable: 4702) // warning C4702: unreachable code
21 # pragma warning(disable: 4522) // warning C4522: multiple assignment operators specified
22 #elif defined(__INTEL_COMPILER)
23 # pragma warning(push)
24 # pragma warning(disable: 68) // integer conversion resulted in a change of sign
25 # pragma warning(disable: 186) // pointless comparison of unsigned integer with zero
26 # pragma warning(disable: 878) // incompatible exception specifications
27 # pragma warning(disable: 1334) // the "template" keyword used for syntactic disambiguation may only be used within a template
28 # 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
30 # pragma warning(disable: 2196) // warning #2196: routine is both "inline" and "noinline"
31 #elif defined(__GNUG__) && !defined(__clang__)
32 # 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"
36 # pragma GCC diagnostic ignored "-Wstrict-aliasing"
37 # pragma GCC diagnostic ignored "-Wattributes"
38 # if __GNUC__ >= 7
39 # pragma GCC diagnostic ignored "-Wnoexcept-type"
40 # endif
41 #endif
42
43 #include "attr.h"
44 #include "options.h"
45 #include "detail/class.h"
46 #include "detail/init.h"
47
48 NAMESPACE_BEGIN(PYBIND11_NAMESPACE)
49
50 /// Wraps an arbitrary C++ function/method/lambda function/.. into a callable Python object
51 class cpp_function : public function {
52 public:
53 cpp_function() { }
54
55 /// Construct a cpp_function from a vanilla function pointer
56 template <typename Return, typename... Args, typename... Extra>
57 cpp_function(Return (*f)(Args...), const Extra&... extra) {
58 initialize(f, f, extra...);
59 }
60
61 /// Construct a cpp_function from a lambda function (possibly with internal state)
62 template <typename Func, typename... Extra,
63 typename = detail::enable_if_t<detail::is_lambda<Func>::value>>
64 cpp_function(Func &&f, const Extra&... extra) {
65 initialize(std::forward<Func>(f),
66 (detail::function_signature_t<Func> *) nullptr, extra...);
67 }
68
69 /// Construct a cpp_function from a class method (non-const)
70 template <typename Return, typename Class, typename... Arg, typename... Extra>
71 cpp_function(Return (Class::*f)(Arg...), const Extra&... extra) {
72 initialize([f](Class *c, Arg... args) -> Return { return (c->*f)(args...); },
73 (Return (*) (Class *, Arg...)) nullptr, extra...);
74 }
75
76 /// Construct a cpp_function from a class method (const)
77 template <typename Return, typename Class, typename... Arg, typename... Extra>
78 cpp_function(Return (Class::*f)(Arg...) const, const Extra&... extra) {
79 initialize([f](const Class *c, Arg... args) -> Return { return (c->*f)(args...); },
80 (Return (*)(const Class *, Arg ...)) nullptr, extra...);
81 }
82
83 /// Return the function name
84 object name() const { return attr("__name__"); }
85
86 protected:
87 /// 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
92 /// Special internal constructor for functors, lambda functions, etc.
93 template <typename Func, typename Return, typename... Args, typename... Extra>
94 void initialize(Func &&f, Return (*)(Args...), const Extra&... extra) {
95
96 struct capture { detail::remove_reference_t<Func> f; };
97
98 /* Store the function including any extra state it might have (e.g. a lambda capture object) */
99 auto rec = make_function_record();
100
101 /* Store the capture object directly in the function record if there is enough space */
102 if (sizeof(capture) <= sizeof(rec->data)) {
103 /* 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. */
106 #if defined(__GNUG__) && !defined(__clang__) && __GNUC__ >= 6
107 # pragma GCC diagnostic push
108 # pragma GCC diagnostic ignored "-Wplacement-new"
109 #endif
110 new ((capture *) &rec->data) capture { std::forward<Func>(f) };
111 #if defined(__GNUG__) && !defined(__clang__) && __GNUC__ >= 6
112 # pragma GCC diagnostic pop
113 #endif
114 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 }
120
121 /* Type casters for the function arguments and return value */
122 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 >;
126
127 static_assert(detail::expected_num_args<Extra...>(sizeof...(Args), cast_in::has_args, cast_in::has_kwargs),
128 "The number of argument annotations does not match the number of function arguments");
129
130 /* Dispatch code which converts function arguments and performs the actual function call */
131 rec->impl = [](detail::function_call &call) -> handle {
132 cast_in args_converter;
133
134 /* Try to cast the function arguments into the C++ domain */
135 if (!args_converter.load_args(call))
136 return PYBIND11_TRY_NEXT_OVERLOAD;
137
138 /* Invoke call policy pre-call hook */
139 detail::process_attributes<Extra...>::precall(call);
140
141 /* Get a pointer to the capture object */
142 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));
145
146 /* 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);
148
149 /* Function scope guard -- defaults to the compile-to-nothing `void_type` */
150 using Guard = detail::extract_guard_t<Extra...>;
151
152 /* Perform the function call */
153 handle result = cast_out::cast(
154 std::move(args_converter).template call<Return, Guard>(cap->f), policy, call.parent);
155
156 /* Invoke call policy post-call hook */
157 detail::process_attributes<Extra...>::postcall(call, result);
158
159 return result;
160 };
161
162 /* Process any user-provided function attributes */
163 detail::process_attributes<Extra...>::init(extra..., rec);
164
165 /* Generate a readable signature describing the function's arguments and return value types */
166 using detail::descr; using detail::_;
167 PYBIND11_DESCR signature = _("(") + cast_in::arg_names() + _(") -> ") + cast_out::name();
168
169 /* Register the function with Python from generic (non-templated) code */
170 initialize_generic(rec, signature.text(), signature.types(), sizeof...(Args));
171
172 if (cast_in::has_args) rec->has_args = true;
173 if (cast_in::has_kwargs) rec->has_kwargs = true;
174
175 /* Stash some additional information used by an important optimization in 'functional.h' */
176 using FunctionType = Return (*)(Args...);
177 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;
182 rec->data[1] = const_cast<void *>(reinterpret_cast<const void *>(&typeid(FunctionType)));
183 }
184 }
185
186 /// Register a function call with Python (generic non-templated code goes here)
187 void initialize_generic(detail::function_record *rec, const char *text,
188 const std::type_info *const *types, size_t args) {
189
190 /* Create copies of all referenced C-style strings */
191 rec->name = strdup(rec->name ? rec->name : "");
192 if (rec->doc) rec->doc = strdup(rec->doc);
193 for (auto &a: rec->args) {
194 if (a.name)
195 a.name = strdup(a.name);
196 if (a.descr)
197 a.descr = strdup(a.descr);
198 else if (a.value)
199 a.descr = strdup(a.value.attr("__repr__")().cast<std::string>().c_str());
200 }
201
202 rec->is_constructor = !strcmp(rec->name, "__init__") || !strcmp(rec->name, "__setstate__");
203
204 #if !defined(NDEBUG) && !defined(PYBIND11_DISABLE_NEW_STYLE_INIT_WARNING)
205 if (rec->is_constructor && !rec->is_new_style_constructor) {
206 const auto class_name = std::string(((PyTypeObject *) rec->scope.ptr())->tp_name);
207 const auto func_name = std::string(rec->name);
208 PyErr_WarnEx(
209 PyExc_FutureWarning,
210 ("pybind11-bound class '" + class_name + "' is using an old-style "
211 "placement-new '" + func_name + "' which has been deprecated. See "
212 "the upgrade guide in pybind11's docs. This message is only visible "
213 "when compiled in debug mode.").c_str(), 0
214 );
215 }
216 #endif
217
218 /* Generate a proper function signature */
219 std::string signature;
220 size_t type_depth = 0, char_index = 0, type_index = 0, arg_index = 0;
221 while (true) {
222 char c = text[char_index++];
223 if (c == '\0')
224 break;
225
226 if (c == '{') {
227 // Write arg name for everything except *args, **kwargs and return type.
228 if (type_depth == 0 && text[char_index] != '*' && arg_index < args) {
229 if (!rec->args.empty() && rec->args[arg_index].name) {
230 signature += rec->args[arg_index].name;
231 } else if (arg_index == 0 && rec->is_method) {
232 signature += "self";
233 } else {
234 signature += "arg" + std::to_string(arg_index - (rec->is_method ? 1 : 0));
235 }
236 signature += ": ";
237 }
238 ++type_depth;
239 } else if (c == '}') {
240 --type_depth;
241 if (type_depth == 0) {
242 if (arg_index < rec->args.size() && rec->args[arg_index].descr) {
243 signature += "=";
244 signature += rec->args[arg_index].descr;
245 }
246 arg_index++;
247 }
248 } else if (c == '%') {
249 const std::type_info *t = types[type_index++];
250 if (!t)
251 pybind11_fail("Internal error while parsing type signature (1)");
252 if (auto tinfo = detail::get_type_info(*t)) {
253 #if defined(PYPY_VERSION)
254 signature += handle((PyObject *) tinfo->type)
255 .attr("__module__")
256 .cast<std::string>() + ".";
257 #endif
258 signature += tinfo->type->tp_name;
259 } else if (rec->is_new_style_constructor && arg_index == 0) {
260 // A new-style `__init__` takes `self` as `value_and_holder`.
261 // Rewrite it to the proper class type.
262 #if defined(PYPY_VERSION)
263 signature += rec->scope.attr("__module__").cast<std::string>() + ".";
264 #endif
265 signature += ((PyTypeObject *) rec->scope.ptr())->tp_name;
266 } else {
267 std::string tname(t->name());
268 detail::clean_type_id(tname);
269 signature += tname;
270 }
271 } else {
272 signature += c;
273 }
274 }
275 if (type_depth != 0 || types[type_index] != nullptr)
276 pybind11_fail("Internal error while parsing type signature (2)");
277
278 #if !defined(PYBIND11_CONSTEXPR_DESCR)
279 delete[] types;
280 delete[] text;
281 #endif
282
283 #if PY_MAJOR_VERSION < 3
284 if (strcmp(rec->name, "__next__") == 0) {
285 std::free(rec->name);
286 rec->name = strdup("next");
287 } else if (strcmp(rec->name, "__bool__") == 0) {
288 std::free(rec->name);
289 rec->name = strdup("__nonzero__");
290 }
291 #endif
292 rec->signature = strdup(signature.c_str());
293 rec->args.shrink_to_fit();
294 rec->nargs = (std::uint16_t) args;
295
296 if (rec->sibling && PYBIND11_INSTANCE_METHOD_CHECK(rec->sibling.ptr()))
297 rec->sibling = PYBIND11_INSTANCE_METHOD_GET_FUNCTION(rec->sibling.ptr());
298
299 detail::function_record *chain = nullptr, *chain_start = rec;
300 if (rec->sibling) {
301 if (PyCFunction_Check(rec->sibling.ptr())) {
302 auto rec_capsule = reinterpret_borrow<capsule>(PyCFunction_GET_SELF(rec->sibling.ptr()));
303 chain = (detail::function_record *) rec_capsule;
304 /* Never append a method to an overload chain of a parent class;
305 instead, hide the parent's overloads in this case */
306 if (!chain->scope.is(rec->scope))
307 chain = nullptr;
308 }
309 // Don't trigger for things like the default __init__, which are wrapper_descriptors that we are intentionally replacing
310 else if (!rec->sibling.is_none() && rec->name[0] != '_')
311 pybind11_fail("Cannot overload existing non-function object \"" + std::string(rec->name) +
312 "\" with a function of the same name");
313 }
314
315 if (!chain) {
316 /* No existing overload was found, create a new function object */
317 rec->def = new PyMethodDef();
318 std::memset(rec->def, 0, sizeof(PyMethodDef));
319 rec->def->ml_name = rec->name;
320 rec->def->ml_meth = reinterpret_cast<PyCFunction>(reinterpret_cast<void (*) (void)>(*dispatcher));
321 rec->def->ml_flags = METH_VARARGS | METH_KEYWORDS;
322
323 capsule rec_capsule(rec, [](void *ptr) {
324 destruct((detail::function_record *) ptr);
325 });
326
327 object scope_module;
328 if (rec->scope) {
329 if (hasattr(rec->scope, "__module__")) {
330 scope_module = rec->scope.attr("__module__");
331 } else if (hasattr(rec->scope, "__name__")) {
332 scope_module = rec->scope.attr("__name__");
333 }
334 }
335
336 m_ptr = PyCFunction_NewEx(rec->def, rec_capsule.ptr(), scope_module.ptr());
337 if (!m_ptr)
338 pybind11_fail("cpp_function::cpp_function(): Could not allocate function object");
339 } else {
340 /* Append at the end of the overload chain */
341 m_ptr = rec->sibling.ptr();
342 inc_ref();
343 chain_start = chain;
344 if (chain->is_method != rec->is_method)
345 pybind11_fail("overloading a method with both static and instance methods is not supported; "
346 #if defined(NDEBUG)
347 "compile in debug mode for more details"
348 #else
349 "error while attempting to bind " + std::string(rec->is_method ? "instance" : "static") + " method " +
350 std::string(pybind11::str(rec->scope.attr("__name__"))) + "." + std::string(rec->name) + signature
351 #endif
352 );
353 while (chain->next)
354 chain = chain->next;
355 chain->next = rec;
356 }
357
358 std::string signatures;
359 int index = 0;
360 /* Create a nice pydoc rec including all signatures and
361 docstrings of the functions in the overload chain */
362 if (chain && options::show_function_signatures()) {
363 // First a generic signature
364 signatures += rec->name;
365 signatures += "(*args, **kwargs)\n";
366 signatures += "Overloaded function.\n\n";
367 }
368 // Then specific overload signatures
369 bool first_user_def = true;
370 for (auto it = chain_start; it != nullptr; it = it->next) {
371 if (options::show_function_signatures()) {
372 if (index > 0) signatures += "\n";
373 if (chain)
374 signatures += std::to_string(++index) + ". ";
375 signatures += rec->name;
376 signatures += it->signature;
377 signatures += "\n";
378 }
379 if (it->doc && strlen(it->doc) > 0 && options::show_user_defined_docstrings()) {
380 // If we're appending another docstring, and aren't printing function signatures, we
381 // need to append a newline first:
382 if (!options::show_function_signatures()) {
383 if (first_user_def) first_user_def = false;
384 else signatures += "\n";
385 }
386 if (options::show_function_signatures()) signatures += "\n";
387 signatures += it->doc;
388 if (options::show_function_signatures()) signatures += "\n";
389 }
390 }
391
392 /* Install docstring */
393 PyCFunctionObject *func = (PyCFunctionObject *) m_ptr;
394 if (func->m_ml->ml_doc)
395 std::free(const_cast<char *>(func->m_ml->ml_doc));
396 func->m_ml->ml_doc = strdup(signatures.c_str());
397
398 if (rec->is_method) {
399 m_ptr = PYBIND11_INSTANCE_METHOD_NEW(m_ptr, rec->scope.ptr());
400 if (!m_ptr)
401 pybind11_fail("cpp_function::cpp_function(): Could not allocate instance method object");
402 Py_DECREF(func);
403 }
404 }
405
406 /// When a cpp_function is GCed, release any memory allocated by pybind11
407 static void destruct(detail::function_record *rec) {
408 while (rec) {
409 detail::function_record *next = rec->next;
410 if (rec->free_data)
411 rec->free_data(rec);
412 std::free((char *) rec->name);
413 std::free((char *) rec->doc);
414 std::free((char *) rec->signature);
415 for (auto &arg: rec->args) {
416 std::free(const_cast<char *>(arg.name));
417 std::free(const_cast<char *>(arg.descr));
418 arg.value.dec_ref();
419 }
420 if (rec->def) {
421 std::free(const_cast<char *>(rec->def->ml_doc));
422 delete rec->def;
423 }
424 delete rec;
425 rec = next;
426 }
427 }
428
429 /// Main dispatch logic for calls to functions bound using pybind11
430 static PyObject *dispatcher(PyObject *self, PyObject *args_in, PyObject *kwargs_in) {
431 using namespace detail;
432
433 /* Iterator over the list of potentially admissible overloads */
434 function_record *overloads = (function_record *) PyCapsule_GetPointer(self, nullptr),
435 *it = overloads;
436
437 /* Need to know how many arguments + keyword arguments there are to pick the right overload */
438 const size_t n_args_in = (size_t) PyTuple_GET_SIZE(args_in);
439
440 handle parent = n_args_in > 0 ? PyTuple_GET_ITEM(args_in, 0) : nullptr,
441 result = PYBIND11_TRY_NEXT_OVERLOAD;
442
443 auto self_value_and_holder = value_and_holder();
444 if (overloads->is_constructor) {
445 const auto tinfo = get_type_info((PyTypeObject *) overloads->scope.ptr());
446 const auto pi = reinterpret_cast<instance *>(parent.ptr());
447 self_value_and_holder = pi->get_value_and_holder(tinfo, false);
448
449 if (!self_value_and_holder.type || !self_value_and_holder.inst) {
450 PyErr_SetString(PyExc_TypeError, "__init__(self, ...) called with invalid `self` argument");
451 return nullptr;
452 }
453
454 // If this value is already registered it must mean __init__ is invoked multiple times;
455 // we really can't support that in C++, so just ignore the second __init__.
456 if (self_value_and_holder.instance_registered())
457 return none().release().ptr();
458 }
459
460 try {
461 // We do this in two passes: in the first pass, we load arguments with `convert=false`;
462 // in the second, we allow conversion (except for arguments with an explicit
463 // py::arg().noconvert()). This lets us prefer calls without conversion, with
464 // conversion as a fallback.
465 std::vector<function_call> second_pass;
466
467 // However, if there are no overloads, we can just skip the no-convert pass entirely
468 const bool overloaded = it != nullptr && it->next != nullptr;
469
470 for (; it != nullptr; it = it->next) {
471
472 /* For each overload:
473 1. Copy all positional arguments we were given, also checking to make sure that
474 named positional arguments weren't *also* specified via kwarg.
475 2. If we weren't given enough, try to make up the omitted ones by checking
476 whether they were provided by a kwarg matching the `py::arg("name")` name. If
477 so, use it (and remove it from kwargs; if not, see if the function binding
478 provided a default that we can use.
479 3. Ensure that either all keyword arguments were "consumed", or that the function
480 takes a kwargs argument to accept unconsumed kwargs.
481 4. Any positional arguments still left get put into a tuple (for args), and any
482 leftover kwargs get put into a dict.
483 5. Pack everything into a vector; if we have py::args or py::kwargs, they are an
484 extra tuple or dict at the end of the positional arguments.
485 6. Call the function call dispatcher (function_record::impl)
486
487 If one of these fail, move on to the next overload and keep trying until we get a
488 result other than PYBIND11_TRY_NEXT_OVERLOAD.
489 */
490
491 function_record &func = *it;
492 size_t pos_args = func.nargs; // Number of positional arguments that we need
493 if (func.has_args) --pos_args; // (but don't count py::args
494 if (func.has_kwargs) --pos_args; // or py::kwargs)
495
496 if (!func.has_args && n_args_in > pos_args)
497 continue; // Too many arguments for this overload
498
499 if (n_args_in < pos_args && func.args.size() < pos_args)
500 continue; // Not enough arguments given, and not enough defaults to fill in the blanks
501
502 function_call call(func, parent);
503
504 size_t args_to_copy = std::min(pos_args, n_args_in);
505 size_t args_copied = 0;
506
507 // 0. Inject new-style `self` argument
508 if (func.is_new_style_constructor) {
509 // The `value` may have been preallocated by an old-style `__init__`
510 // if it was a preceding candidate for overload resolution.
511 if (self_value_and_holder)
512 self_value_and_holder.type->dealloc(self_value_and_holder);
513
514 call.init_self = PyTuple_GET_ITEM(args_in, 0);
515 call.args.push_back(reinterpret_cast<PyObject *>(&self_value_and_holder));
516 call.args_convert.push_back(false);
517 ++args_copied;
518 }
519
520 // 1. Copy any position arguments given.
521 bool bad_arg = false;
522 for (; args_copied < args_to_copy; ++args_copied) {
523 argument_record *arg_rec = args_copied < func.args.size() ? &func.args[args_copied] : nullptr;
524 if (kwargs_in && arg_rec && arg_rec->name && PyDict_GetItemString(kwargs_in, arg_rec->name)) {
525 bad_arg = true;
526 break;
527 }
528
529 handle arg(PyTuple_GET_ITEM(args_in, args_copied));
530 if (arg_rec && !arg_rec->none && arg.is_none()) {
531 bad_arg = true;
532 break;
533 }
534 call.args.push_back(arg);
535 call.args_convert.push_back(arg_rec ? arg_rec->convert : true);
536 }
537 if (bad_arg)
538 continue; // Maybe it was meant for another overload (issue #688)
539
540 // We'll need to copy this if we steal some kwargs for defaults
541 dict kwargs = reinterpret_borrow<dict>(kwargs_in);
542
543 // 2. Check kwargs and, failing that, defaults that may help complete the list
544 if (args_copied < pos_args) {
545 bool copied_kwargs = false;
546
547 for (; args_copied < pos_args; ++args_copied) {
548 const auto &arg = func.args[args_copied];
549
550 handle value;
551 if (kwargs_in && arg.name)
552 value = PyDict_GetItemString(kwargs.ptr(), arg.name);
553
554 if (value) {
555 // Consume a kwargs value
556 if (!copied_kwargs) {
557 kwargs = reinterpret_steal<dict>(PyDict_Copy(kwargs.ptr()));
558 copied_kwargs = true;
559 }
560 PyDict_DelItemString(kwargs.ptr(), arg.name);
561 } else if (arg.value) {
562 value = arg.value;
563 }
564
565 if (value) {
566 call.args.push_back(value);
567 call.args_convert.push_back(arg.convert);
568 }
569 else
570 break;
571 }
572
573 if (args_copied < pos_args)
574 continue; // Not enough arguments, defaults, or kwargs to fill the positional arguments
575 }
576
577 // 3. Check everything was consumed (unless we have a kwargs arg)
578 if (kwargs && kwargs.size() > 0 && !func.has_kwargs)
579 continue; // Unconsumed kwargs, but no py::kwargs argument to accept them
580
581 // 4a. If we have a py::args argument, create a new tuple with leftovers
582 tuple extra_args;
583 if (func.has_args) {
584 if (args_to_copy == 0) {
585 // We didn't copy out any position arguments from the args_in tuple, so we
586 // can reuse it directly without copying:
587 extra_args = reinterpret_borrow<tuple>(args_in);
588 } else if (args_copied >= n_args_in) {
589 extra_args = tuple(0);
590 } else {
591 size_t args_size = n_args_in - args_copied;
592 extra_args = tuple(args_size);
593 for (size_t i = 0; i < args_size; ++i) {
594 handle item = PyTuple_GET_ITEM(args_in, args_copied + i);
595 extra_args[i] = item.inc_ref().ptr();
596 }
597 }
598 call.args.push_back(extra_args);
599 call.args_convert.push_back(false);
600 }
601
602 // 4b. If we have a py::kwargs, pass on any remaining kwargs
603 if (func.has_kwargs) {
604 if (!kwargs.ptr())
605 kwargs = dict(); // If we didn't get one, send an empty one
606 call.args.push_back(kwargs);
607 call.args_convert.push_back(false);
608 }
609
610 // 5. Put everything in a vector. Not technically step 5, we've been building it
611 // in `call.args` all along.
612 #if !defined(NDEBUG)
613 if (call.args.size() != func.nargs || call.args_convert.size() != func.nargs)
614 pybind11_fail("Internal error: function call dispatcher inserted wrong number of arguments!");
615 #endif
616
617 std::vector<bool> second_pass_convert;
618 if (overloaded) {
619 // We're in the first no-convert pass, so swap out the conversion flags for a
620 // set of all-false flags. If the call fails, we'll swap the flags back in for
621 // the conversion-allowed call below.
622 second_pass_convert.resize(func.nargs, false);
623 call.args_convert.swap(second_pass_convert);
624 }
625
626 // 6. Call the function.
627 try {
628 loader_life_support guard{};
629 result = func.impl(call);
630 } catch (reference_cast_error &) {
631 result = PYBIND11_TRY_NEXT_OVERLOAD;
632 }
633
634 if (result.ptr() != PYBIND11_TRY_NEXT_OVERLOAD)
635 break;
636
637 if (overloaded) {
638 // The (overloaded) call failed; if the call has at least one argument that
639 // permits conversion (i.e. it hasn't been explicitly specified `.noconvert()`)
640 // then add this call to the list of second pass overloads to try.
641 for (size_t i = func.is_method ? 1 : 0; i < pos_args; i++) {
642 if (second_pass_convert[i]) {
643 // Found one: swap the converting flags back in and store the call for
644 // the second pass.
645 call.args_convert.swap(second_pass_convert);
646 second_pass.push_back(std::move(call));
647 break;
648 }
649 }
650 }
651 }
652
653 if (overloaded && !second_pass.empty() && result.ptr() == PYBIND11_TRY_NEXT_OVERLOAD) {
654 // The no-conversion pass finished without success, try again with conversion allowed
655 for (auto &call : second_pass) {
656 try {
657 loader_life_support guard{};
658 result = call.func.impl(call);
659 } catch (reference_cast_error &) {
660 result = PYBIND11_TRY_NEXT_OVERLOAD;
661 }
662
663 if (result.ptr() != PYBIND11_TRY_NEXT_OVERLOAD)
664 break;
665 }
666 }
667 } catch (error_already_set &e) {
668 e.restore();
669 return nullptr;
670 } catch (...) {
671 /* When an exception is caught, give each registered exception
672 translator a chance to translate it to a Python exception
673 in reverse order of registration.
674
675 A translator may choose to do one of the following:
676
677 - catch the exception and call PyErr_SetString or PyErr_SetObject
678 to set a standard (or custom) Python exception, or
679 - do nothing and let the exception fall through to the next translator, or
680 - delegate translation to the next translator by throwing a new type of exception. */
681
682 auto last_exception = std::current_exception();
683 auto &registered_exception_translators = get_internals().registered_exception_translators;
684 for (auto& translator : registered_exception_translators) {
685 try {
686 translator(last_exception);
687 } catch (...) {
688 last_exception = std::current_exception();
689 continue;
690 }
691 return nullptr;
692 }
693 PyErr_SetString(PyExc_SystemError, "Exception escaped from default exception translator!");
694 return nullptr;
695 }
696
697 auto append_note_if_missing_header_is_suspected = [](std::string &msg) {
698 if (msg.find("std::") != std::string::npos) {
699 msg += "\n\n"
700 "Did you forget to `#include <pybind11/stl.h>`? Or <pybind11/complex.h>,\n"
701 "<pybind11/functional.h>, <pybind11/chrono.h>, etc. Some automatic\n"
702 "conversions are optional and require extra headers to be included\n"
703 "when compiling your pybind11 module.";
704 }
705 };
706
707 if (result.ptr() == PYBIND11_TRY_NEXT_OVERLOAD) {
708 if (overloads->is_operator)
709 return handle(Py_NotImplemented).inc_ref().ptr();
710
711 std::string msg = std::string(overloads->name) + "(): incompatible " +
712 std::string(overloads->is_constructor ? "constructor" : "function") +
713 " arguments. The following argument types are supported:\n";
714
715 int ctr = 0;
716 for (function_record *it2 = overloads; it2 != nullptr; it2 = it2->next) {
717 msg += " "+ std::to_string(++ctr) + ". ";
718
719 bool wrote_sig = false;
720 if (overloads->is_constructor) {
721 // For a constructor, rewrite `(self: Object, arg0, ...) -> NoneType` as `Object(arg0, ...)`
722 std::string sig = it2->signature;
723 size_t start = sig.find('(') + 7; // skip "(self: "
724 if (start < sig.size()) {
725 // End at the , for the next argument
726 size_t end = sig.find(", "), next = end + 2;
727 size_t ret = sig.rfind(" -> ");
728 // Or the ), if there is no comma:
729 if (end >= sig.size()) next = end = sig.find(')');
730 if (start < end && next < sig.size()) {
731 msg.append(sig, start, end - start);
732 msg += '(';
733 msg.append(sig, next, ret - next);
734 wrote_sig = true;
735 }
736 }
737 }
738 if (!wrote_sig) msg += it2->signature;
739
740 msg += "\n";
741 }
742 msg += "\nInvoked with: ";
743 auto args_ = reinterpret_borrow<tuple>(args_in);
744 bool some_args = false;
745 for (size_t ti = overloads->is_constructor ? 1 : 0; ti < args_.size(); ++ti) {
746 if (!some_args) some_args = true;
747 else msg += ", ";
748 msg += pybind11::repr(args_[ti]);
749 }
750 if (kwargs_in) {
751 auto kwargs = reinterpret_borrow<dict>(kwargs_in);
752 if (kwargs.size() > 0) {
753 if (some_args) msg += "; ";
754 msg += "kwargs: ";
755 bool first = true;
756 for (auto kwarg : kwargs) {
757 if (first) first = false;
758 else msg += ", ";
759 msg += pybind11::str("{}={!r}").format(kwarg.first, kwarg.second);
760 }
761 }
762 }
763
764 append_note_if_missing_header_is_suspected(msg);
765 PyErr_SetString(PyExc_TypeError, msg.c_str());
766 return nullptr;
767 } else if (!result) {
768 std::string msg = "Unable to convert function return value to a "
769 "Python type! The signature was\n\t";
770 msg += it->signature;
771 append_note_if_missing_header_is_suspected(msg);
772 PyErr_SetString(PyExc_TypeError, msg.c_str());
773 return nullptr;
774 } else {
775 if (overloads->is_constructor && !self_value_and_holder.holder_constructed()) {
776 auto *pi = reinterpret_cast<instance *>(parent.ptr());
777 self_value_and_holder.type->init_instance(pi, nullptr);
778 }
779 return result.ptr();
780 }
781 }
782 };
783
784 /// Wrapper for Python extension modules
785 class module : public object {
786 public:
787 PYBIND11_OBJECT_DEFAULT(module, object, PyModule_Check)
788
789 /// Create a new top-level Python module with the given name and docstring
790 explicit module(const char *name, const char *doc = nullptr) {
791 if (!options::show_user_defined_docstrings()) doc = nullptr;
792 #if PY_MAJOR_VERSION >= 3
793 PyModuleDef *def = new PyModuleDef();
794 std::memset(def, 0, sizeof(PyModuleDef));
795 def->m_name = name;
796 def->m_doc = doc;
797 def->m_size = -1;
798 Py_INCREF(def);
799 m_ptr = PyModule_Create(def);
800 #else
801 m_ptr = Py_InitModule3(name, nullptr, doc);
802 #endif
803 if (m_ptr == nullptr)
804 pybind11_fail("Internal error in module::module()");
805 inc_ref();
806 }
807
808 /** \rst
809 Create Python binding for a new function within the module scope. ``Func``
810 can be a plain C++ function, a function pointer, or a lambda function. For
811 details on the ``Extra&& ... extra`` argument, see section :ref:`extras`.
812 \endrst */
813 template <typename Func, typename... Extra>
814 module &def(const char *name_, Func &&f, const Extra& ... extra) {
815 cpp_function func(std::forward<Func>(f), name(name_), scope(*this),
816 sibling(getattr(*this, name_, none())), extra...);
817 // NB: allow overwriting here because cpp_function sets up a chain with the intention of
818 // overwriting (and has already checked internally that it isn't overwriting non-functions).
819 add_object(name_, func, true /* overwrite */);
820 return *this;
821 }
822
823 /** \rst
824 Create and return a new Python submodule with the given name and docstring.
825 This also works recursively, i.e.
826
827 .. code-block:: cpp
828
829 py::module m("example", "pybind11 example plugin");
830 py::module m2 = m.def_submodule("sub", "A submodule of 'example'");
831 py::module m3 = m2.def_submodule("subsub", "A submodule of 'example.sub'");
832 \endrst */
833 module def_submodule(const char *name, const char *doc = nullptr) {
834 std::string full_name = std::string(PyModule_GetName(m_ptr))
835 + std::string(".") + std::string(name);
836 auto result = reinterpret_borrow<module>(PyImport_AddModule(full_name.c_str()));
837 if (doc && options::show_user_defined_docstrings())
838 result.attr("__doc__") = pybind11::str(doc);
839 attr(name) = result;
840 return result;
841 }
842
843 /// Import and return a module or throws `error_already_set`.
844 static module import(const char *name) {
845 PyObject *obj = PyImport_ImportModule(name);
846 if (!obj)
847 throw error_already_set();
848 return reinterpret_steal<module>(obj);
849 }
850
851 /// Reload the module or throws `error_already_set`.
852 void reload() {
853 PyObject *obj = PyImport_ReloadModule(ptr());
854 if (!obj)
855 throw error_already_set();
856 *this = reinterpret_steal<module>(obj);
857 }
858
859 // Adds an object to the module using the given name. Throws if an object with the given name
860 // already exists.
861 //
862 // overwrite should almost always be false: attempting to overwrite objects that pybind11 has
863 // established will, in most cases, break things.
864 PYBIND11_NOINLINE void add_object(const char *name, handle obj, bool overwrite = false) {
865 if (!overwrite && hasattr(*this, name))
866 pybind11_fail("Error during initialization: multiple incompatible definitions with name \"" +
867 std::string(name) + "\"");
868
869 PyModule_AddObject(ptr(), name, obj.inc_ref().ptr() /* steals a reference */);
870 }
871 };
872
873 /// \ingroup python_builtins
874 /// Return a dictionary representing the global variables in the current execution frame,
875 /// or ``__main__.__dict__`` if there is no frame (usually when the interpreter is embedded).
876 inline dict globals() {
877 PyObject *p = PyEval_GetGlobals();
878 return reinterpret_borrow<dict>(p ? p : module::import("__main__").attr("__dict__").ptr());
879 }
880
881 NAMESPACE_BEGIN(detail)
882 /// Generic support for creating new Python heap types
883 class generic_type : public object {
884 template <typename...> friend class class_;
885 public:
886 PYBIND11_OBJECT_DEFAULT(generic_type, object, PyType_Check)
887 protected:
888 void initialize(const type_record &rec) {
889 if (rec.scope && hasattr(rec.scope, rec.name))
890 pybind11_fail("generic_type: cannot initialize type \"" + std::string(rec.name) +
891 "\": an object with that name is already defined");
892
893 if (rec.module_local ? get_local_type_info(*rec.type) : get_global_type_info(*rec.type))
894 pybind11_fail("generic_type: type \"" + std::string(rec.name) +
895 "\" is already registered!");
896
897 m_ptr = make_new_python_type(rec);
898
899 /* Register supplemental type information in C++ dict */
900 auto *tinfo = new detail::type_info();
901 tinfo->type = (PyTypeObject *) m_ptr;
902 tinfo->cpptype = rec.type;
903 tinfo->type_size = rec.type_size;
904 tinfo->operator_new = rec.operator_new;
905 tinfo->holder_size_in_ptrs = size_in_ptrs(rec.holder_size);
906 tinfo->init_instance = rec.init_instance;
907 tinfo->dealloc = rec.dealloc;
908 tinfo->simple_type = true;
909 tinfo->simple_ancestors = true;
910 tinfo->default_holder = rec.default_holder;
911 tinfo->module_local = rec.module_local;
912
913 auto &internals = get_internals();
914 auto tindex = std::type_index(*rec.type);
915 tinfo->direct_conversions = &internals.direct_conversions[tindex];
916 if (rec.module_local)
917 registered_local_types_cpp()[tindex] = tinfo;
918 else
919 internals.registered_types_cpp[tindex] = tinfo;
920 internals.registered_types_py[(PyTypeObject *) m_ptr] = { tinfo };
921
922 if (rec.bases.size() > 1 || rec.multiple_inheritance) {
923 mark_parents_nonsimple(tinfo->type);
924 tinfo->simple_ancestors = false;
925 }
926 else if (rec.bases.size() == 1) {
927 auto parent_tinfo = get_type_info((PyTypeObject *) rec.bases[0].ptr());
928 tinfo->simple_ancestors = parent_tinfo->simple_ancestors;
929 }
930
931 if (rec.module_local) {
932 // Stash the local typeinfo and loader so that external modules can access it.
933 tinfo->module_local_load = &type_caster_generic::local_load;
934 setattr(m_ptr, PYBIND11_MODULE_LOCAL_ID, capsule(tinfo));
935 }
936 }
937
938 /// Helper function which tags all parents of a type using mult. inheritance
939 void mark_parents_nonsimple(PyTypeObject *value) {
940 auto t = reinterpret_borrow<tuple>(value->tp_bases);
941 for (handle h : t) {
942 auto tinfo2 = get_type_info((PyTypeObject *) h.ptr());
943 if (tinfo2)
944 tinfo2->simple_type = false;
945 mark_parents_nonsimple((PyTypeObject *) h.ptr());
946 }
947 }
948
949 void install_buffer_funcs(
950 buffer_info *(*get_buffer)(PyObject *, void *),
951 void *get_buffer_data) {
952 PyHeapTypeObject *type = (PyHeapTypeObject*) m_ptr;
953 auto tinfo = detail::get_type_info(&type->ht_type);
954
955 if (!type->ht_type.tp_as_buffer)
956 pybind11_fail(
957 "To be able to register buffer protocol support for the type '" +
958 std::string(tinfo->type->tp_name) +
959 "' the associated class<>(..) invocation must "
960 "include the pybind11::buffer_protocol() annotation!");
961
962 tinfo->get_buffer = get_buffer;
963 tinfo->get_buffer_data = get_buffer_data;
964 }
965
966 void def_property_static_impl(const char *name,
967 handle fget, handle fset,
968 detail::function_record *rec_fget) {
969 const auto is_static = !(rec_fget->is_method && rec_fget->scope);
970 const auto has_doc = rec_fget->doc && pybind11::options::show_user_defined_docstrings();
971
972 auto property = handle((PyObject *) (is_static ? get_internals().static_property_type
973 : &PyProperty_Type));
974 attr(name) = property(fget.ptr() ? fget : none(),
975 fset.ptr() ? fset : none(),
976 /*deleter*/none(),
977 pybind11::str(has_doc ? rec_fget->doc : ""));
978 }
979 };
980
981 /// Set the pointer to operator new if it exists. The cast is needed because it can be overloaded.
982 template <typename T, typename = void_t<decltype(static_cast<void *(*)(size_t)>(T::operator new))>>
983 void set_operator_new(type_record *r) { r->operator_new = &T::operator new; }
984
985 template <typename> void set_operator_new(...) { }
986
987 template <typename T, typename SFINAE = void> struct has_operator_delete : std::false_type { };
988 template <typename T> struct has_operator_delete<T, void_t<decltype(static_cast<void (*)(void *)>(T::operator delete))>>
989 : std::true_type { };
990 template <typename T, typename SFINAE = void> struct has_operator_delete_size : std::false_type { };
991 template <typename T> struct has_operator_delete_size<T, void_t<decltype(static_cast<void (*)(void *, size_t)>(T::operator delete))>>
992 : std::true_type { };
993 /// Call class-specific delete if it exists or global otherwise. Can also be an overload set.
994 template <typename T, enable_if_t<has_operator_delete<T>::value, int> = 0>
995 void call_operator_delete(T *p, size_t) { T::operator delete(p); }
996 template <typename T, enable_if_t<!has_operator_delete<T>::value && has_operator_delete_size<T>::value, int> = 0>
997 void call_operator_delete(T *p, size_t s) { T::operator delete(p, s); }
998
999 inline void call_operator_delete(void *p, size_t) { ::operator delete(p); }
1000
1001 NAMESPACE_END(detail)
1002
1003 /// Given a pointer to a member function, cast it to its `Derived` version.
1004 /// Forward everything else unchanged.
1005 template <typename /*Derived*/, typename F>
1006 auto method_adaptor(F &&f) -> decltype(std::forward<F>(f)) { return std::forward<F>(f); }
1007
1008 template <typename Derived, typename Return, typename Class, typename... Args>
1009 auto method_adaptor(Return (Class::*pmf)(Args...)) -> Return (Derived::*)(Args...) { return pmf; }
1010
1011 template <typename Derived, typename Return, typename Class, typename... Args>
1012 auto method_adaptor(Return (Class::*pmf)(Args...) const) -> Return (Derived::*)(Args...) const { return pmf; }
1013
1014 template <typename type_, typename... options>
1015 class class_ : public detail::generic_type {
1016 template <typename T> using is_holder = detail::is_holder_type<type_, T>;
1017 template <typename T> using is_subtype = detail::is_strict_base_of<type_, T>;
1018 template <typename T> using is_base = detail::is_strict_base_of<T, type_>;
1019 // struct instead of using here to help MSVC:
1020 template <typename T> struct is_valid_class_option :
1021 detail::any_of<is_holder<T>, is_subtype<T>, is_base<T>> {};
1022
1023 public:
1024 using type = type_;
1025 using type_alias = detail::exactly_one_t<is_subtype, void, options...>;
1026 constexpr static bool has_alias = !std::is_void<type_alias>::value;
1027 using holder_type = detail::exactly_one_t<is_holder, std::unique_ptr<type>, options...>;
1028
1029 static_assert(detail::all_of<is_valid_class_option<options>...>::value,
1030 "Unknown/invalid class_ template parameters provided");
1031
1032 static_assert(!has_alias || std::is_polymorphic<type>::value,
1033 "Cannot use an alias class with a non-polymorphic type");
1034
1035 PYBIND11_OBJECT(class_, generic_type, PyType_Check)
1036
1037 template <typename... Extra>
1038 class_(handle scope, const char *name, const Extra &... extra) {
1039 using namespace detail;
1040
1041 // MI can only be specified via class_ template options, not constructor parameters
1042 static_assert(
1043 none_of<is_pyobject<Extra>...>::value || // no base class arguments, or:
1044 ( constexpr_sum(is_pyobject<Extra>::value...) == 1 && // Exactly one base
1045 constexpr_sum(is_base<options>::value...) == 0 && // no template option bases
1046 none_of<std::is_same<multiple_inheritance, Extra>...>::value), // no multiple_inheritance attr
1047 "Error: multiple inheritance bases must be specified via class_ template options");
1048
1049 type_record record;
1050 record.scope = scope;
1051 record.name = name;
1052 record.type = &typeid(type);
1053 record.type_size = sizeof(conditional_t<has_alias, type_alias, type>);
1054 record.holder_size = sizeof(holder_type);
1055 record.init_instance = init_instance;
1056 record.dealloc = dealloc;
1057 record.default_holder = std::is_same<holder_type, std::unique_ptr<type>>::value;
1058
1059 set_operator_new<type>(&record);
1060
1061 /* Register base classes specified via template arguments to class_, if any */
1062 PYBIND11_EXPAND_SIDE_EFFECTS(add_base<options>(record));
1063
1064 /* Process optional arguments, if any */
1065 process_attributes<Extra...>::init(extra..., &record);
1066
1067 generic_type::initialize(record);
1068
1069 if (has_alias) {
1070 auto &instances = record.module_local ? registered_local_types_cpp() : get_internals().registered_types_cpp;
1071 instances[std::type_index(typeid(type_alias))] = instances[std::type_index(typeid(type))];
1072 }
1073 }
1074
1075 template <typename Base, detail::enable_if_t<is_base<Base>::value, int> = 0>
1076 static void add_base(detail::type_record &rec) {
1077 rec.add_base(typeid(Base), [](void *src) -> void * {
1078 return static_cast<Base *>(reinterpret_cast<type *>(src));
1079 });
1080 }
1081
1082 template <typename Base, detail::enable_if_t<!is_base<Base>::value, int> = 0>
1083 static void add_base(detail::type_record &) { }
1084
1085 template <typename Func, typename... Extra>
1086 class_ &def(const char *name_, Func&& f, const Extra&... extra) {
1087 cpp_function cf(method_adaptor<type>(std::forward<Func>(f)), name(name_), is_method(*this),
1088 sibling(getattr(*this, name_, none())), extra...);
1089 attr(cf.name()) = cf;
1090 return *this;
1091 }
1092
1093 template <typename Func, typename... Extra> class_ &
1094 def_static(const char *name_, Func &&f, const Extra&... extra) {
1095 static_assert(!std::is_member_function_pointer<Func>::value,
1096 "def_static(...) called with a non-static member function pointer");
1097 cpp_function cf(std::forward<Func>(f), name(name_), scope(*this),
1098 sibling(getattr(*this, name_, none())), extra...);
1099 attr(cf.name()) = cf;
1100 return *this;
1101 }
1102
1103 template <detail::op_id id, detail::op_type ot, typename L, typename R, typename... Extra>
1104 class_ &def(const detail::op_<id, ot, L, R> &op, const Extra&... extra) {
1105 op.execute(*this, extra...);
1106 return *this;
1107 }
1108
1109 template <detail::op_id id, detail::op_type ot, typename L, typename R, typename... Extra>
1110 class_ & def_cast(const detail::op_<id, ot, L, R> &op, const Extra&... extra) {
1111 op.execute_cast(*this, extra...);
1112 return *this;
1113 }
1114
1115 template <typename... Args, typename... Extra>
1116 class_ &def(const detail::initimpl::constructor<Args...> &init, const Extra&... extra) {
1117 init.execute(*this, extra...);
1118 return *this;
1119 }
1120
1121 template <typename... Args, typename... Extra>
1122 class_ &def(const detail::initimpl::alias_constructor<Args...> &init, const Extra&... extra) {
1123 init.execute(*this, extra...);
1124 return *this;
1125 }
1126
1127 template <typename... Args, typename... Extra>
1128 class_ &def(detail::initimpl::factory<Args...> &&init, const Extra&... extra) {
1129 std::move(init).execute(*this, extra...);
1130 return *this;
1131 }
1132
1133 template <typename... Args, typename... Extra>
1134 class_ &def(detail::initimpl::pickle_factory<Args...> &&pf, const Extra &...extra) {
1135 std::move(pf).execute(*this, extra...);
1136 return *this;
1137 }
1138
1139 template <typename Func> class_& def_buffer(Func &&func) {
1140 struct capture { Func func; };
1141 capture *ptr = new capture { std::forward<Func>(func) };
1142 install_buffer_funcs([](PyObject *obj, void *ptr) -> buffer_info* {
1143 detail::make_caster<type> caster;
1144 if (!caster.load(obj, false))
1145 return nullptr;
1146 return new buffer_info(((capture *) ptr)->func(caster));
1147 }, ptr);
1148 return *this;
1149 }
1150
1151 template <typename Return, typename Class, typename... Args>
1152 class_ &def_buffer(Return (Class::*func)(Args...)) {
1153 return def_buffer([func] (type &obj) { return (obj.*func)(); });
1154 }
1155
1156 template <typename Return, typename Class, typename... Args>
1157 class_ &def_buffer(Return (Class::*func)(Args...) const) {
1158 return def_buffer([func] (const type &obj) { return (obj.*func)(); });
1159 }
1160
1161 template <typename C, typename D, typename... Extra>
1162 class_ &def_readwrite(const char *name, D C::*pm, const Extra&... extra) {
1163 static_assert(std::is_base_of<C, type>::value, "def_readwrite() requires a class member (or base class member)");
1164 cpp_function fget([pm](const type &c) -> const D &{ return c.*pm; }, is_method(*this)),
1165 fset([pm](type &c, const D &value) { c.*pm = value; }, is_method(*this));
1166 def_property(name, fget, fset, return_value_policy::reference_internal, extra...);
1167 return *this;
1168 }
1169
1170 template <typename C, typename D, typename... Extra>
1171 class_ &def_readonly(const char *name, const D C::*pm, const Extra& ...extra) {
1172 static_assert(std::is_base_of<C, type>::value, "def_readonly() requires a class member (or base class member)");
1173 cpp_function fget([pm](const type &c) -> const D &{ return c.*pm; }, is_method(*this));
1174 def_property_readonly(name, fget, return_value_policy::reference_internal, extra...);
1175 return *this;
1176 }
1177
1178 template <typename D, typename... Extra>
1179 class_ &def_readwrite_static(const char *name, D *pm, const Extra& ...extra) {
1180 cpp_function fget([pm](object) -> const D &{ return *pm; }, scope(*this)),
1181 fset([pm](object, const D &value) { *pm = value; }, scope(*this));
1182 def_property_static(name, fget, fset, return_value_policy::reference, extra...);
1183 return *this;
1184 }
1185
1186 template <typename D, typename... Extra>
1187 class_ &def_readonly_static(const char *name, const D *pm, const Extra& ...extra) {
1188 cpp_function fget([pm](object) -> const D &{ return *pm; }, scope(*this));
1189 def_property_readonly_static(name, fget, return_value_policy::reference, extra...);
1190 return *this;
1191 }
1192
1193 /// Uses return_value_policy::reference_internal by default
1194 template <typename Getter, typename... Extra>
1195 class_ &def_property_readonly(const char *name, const Getter &fget, const Extra& ...extra) {
1196 return def_property_readonly(name, cpp_function(method_adaptor<type>(fget)),
1197 return_value_policy::reference_internal, extra...);
1198 }
1199
1200 /// Uses cpp_function's return_value_policy by default
1201 template <typename... Extra>
1202 class_ &def_property_readonly(const char *name, const cpp_function &fget, const Extra& ...extra) {
1203 return def_property(name, fget, cpp_function(), extra...);
1204 }
1205
1206 /// Uses return_value_policy::reference by default
1207 template <typename Getter, typename... Extra>
1208 class_ &def_property_readonly_static(const char *name, const Getter &fget, const Extra& ...extra) {
1209 return def_property_readonly_static(name, cpp_function(fget), return_value_policy::reference, extra...);
1210 }
1211
1212 /// Uses cpp_function's return_value_policy by default
1213 template <typename... Extra>
1214 class_ &def_property_readonly_static(const char *name, const cpp_function &fget, const Extra& ...extra) {
1215 return def_property_static(name, fget, cpp_function(), extra...);
1216 }
1217
1218 /// Uses return_value_policy::reference_internal by default
1219 template <typename Getter, typename Setter, typename... Extra>
1220 class_ &def_property(const char *name, const Getter &fget, const Setter &fset, const Extra& ...extra) {
1221 return def_property(name, fget, cpp_function(method_adaptor<type>(fset)), extra...);
1222 }
1223 template <typename Getter, typename... Extra>
1224 class_ &def_property(const char *name, const Getter &fget, const cpp_function &fset, const Extra& ...extra) {
1225 return def_property(name, cpp_function(method_adaptor<type>(fget)), fset,
1226 return_value_policy::reference_internal, extra...);
1227 }
1228
1229 /// Uses cpp_function's return_value_policy by default
1230 template <typename... Extra>
1231 class_ &def_property(const char *name, const cpp_function &fget, const cpp_function &fset, const Extra& ...extra) {
1232 return def_property_static(name, fget, fset, is_method(*this), extra...);
1233 }
1234
1235 /// Uses return_value_policy::reference by default
1236 template <typename Getter, typename... Extra>
1237 class_ &def_property_static(const char *name, const Getter &fget, const cpp_function &fset, const Extra& ...extra) {
1238 return def_property_static(name, cpp_function(fget), fset, return_value_policy::reference, extra...);
1239 }
1240
1241 /// Uses cpp_function's return_value_policy by default
1242 template <typename... Extra>
1243 class_ &def_property_static(const char *name, const cpp_function &fget, const cpp_function &fset, const Extra& ...extra) {
1244 auto rec_fget = get_function_record(fget), rec_fset = get_function_record(fset);
1245 char *doc_prev = rec_fget->doc; /* 'extra' field may include a property-specific documentation string */
1246 detail::process_attributes<Extra...>::init(extra..., rec_fget);
1247 if (rec_fget->doc && rec_fget->doc != doc_prev) {
1248 free(doc_prev);
1249 rec_fget->doc = strdup(rec_fget->doc);
1250 }
1251 if (rec_fset) {
1252 doc_prev = rec_fset->doc;
1253 detail::process_attributes<Extra...>::init(extra..., rec_fset);
1254 if (rec_fset->doc && rec_fset->doc != doc_prev) {
1255 free(doc_prev);
1256 rec_fset->doc = strdup(rec_fset->doc);
1257 }
1258 }
1259 def_property_static_impl(name, fget, fset, rec_fget);
1260 return *this;
1261 }
1262
1263 private:
1264 /// Initialize holder object, variant 1: object derives from enable_shared_from_this
1265 template <typename T>
1266 static void init_holder(detail::instance *inst, detail::value_and_holder &v_h,
1267 const holder_type * /* unused */, const std::enable_shared_from_this<T> * /* dummy */) {
1268 try {
1269 auto sh = std::dynamic_pointer_cast<typename holder_type::element_type>(
1270 v_h.value_ptr<type>()->shared_from_this());
1271 if (sh) {
1272 new (&v_h.holder<holder_type>()) holder_type(std::move(sh));
1273 v_h.set_holder_constructed();
1274 }
1275 } catch (const std::bad_weak_ptr &) {}
1276
1277 if (!v_h.holder_constructed() && inst->owned) {
1278 new (&v_h.holder<holder_type>()) holder_type(v_h.value_ptr<type>());
1279 v_h.set_holder_constructed();
1280 }
1281 }
1282
1283 static void init_holder_from_existing(const detail::value_and_holder &v_h,
1284 const holder_type *holder_ptr, std::true_type /*is_copy_constructible*/) {
1285 new (&v_h.holder<holder_type>()) holder_type(*reinterpret_cast<const holder_type *>(holder_ptr));
1286 }
1287
1288 static void init_holder_from_existing(const detail::value_and_holder &v_h,
1289 const holder_type *holder_ptr, std::false_type /*is_copy_constructible*/) {
1290 new (&v_h.holder<holder_type>()) holder_type(std::move(*const_cast<holder_type *>(holder_ptr)));
1291 }
1292
1293 /// Initialize holder object, variant 2: try to construct from existing holder object, if possible
1294 static void init_holder(detail::instance *inst, detail::value_and_holder &v_h,
1295 const holder_type *holder_ptr, const void * /* dummy -- not enable_shared_from_this<T>) */) {
1296 if (holder_ptr) {
1297 init_holder_from_existing(v_h, holder_ptr, std::is_copy_constructible<holder_type>());
1298 v_h.set_holder_constructed();
1299 } else if (inst->owned || detail::always_construct_holder<holder_type>::value) {
1300 new (&v_h.holder<holder_type>()) holder_type(v_h.value_ptr<type>());
1301 v_h.set_holder_constructed();
1302 }
1303 }
1304
1305 /// Performs instance initialization including constructing a holder and registering the known
1306 /// instance. Should be called as soon as the `type` value_ptr is set for an instance. Takes an
1307 /// optional pointer to an existing holder to use; if not specified and the instance is
1308 /// `.owned`, a new holder will be constructed to manage the value pointer.
1309 static void init_instance(detail::instance *inst, const void *holder_ptr) {
1310 auto v_h = inst->get_value_and_holder(detail::get_type_info(typeid(type)));
1311 if (!v_h.instance_registered()) {
1312 register_instance(inst, v_h.value_ptr(), v_h.type);
1313 v_h.set_instance_registered();
1314 }
1315 init_holder(inst, v_h, (const holder_type *) holder_ptr, v_h.value_ptr<type>());
1316 }
1317
1318 /// Deallocates an instance; via holder, if constructed; otherwise via operator delete.
1319 static void dealloc(detail::value_and_holder &v_h) {
1320 if (v_h.holder_constructed()) {
1321 v_h.holder<holder_type>().~holder_type();
1322 v_h.set_holder_constructed(false);
1323 }
1324 else {
1325 detail::call_operator_delete(v_h.value_ptr<type>(), v_h.type->type_size);
1326 }
1327 v_h.value_ptr() = nullptr;
1328 }
1329
1330 static detail::function_record *get_function_record(handle h) {
1331 h = detail::get_function(h);
1332 return h ? (detail::function_record *) reinterpret_borrow<capsule>(PyCFunction_GET_SELF(h.ptr()))
1333 : nullptr;
1334 }
1335 };
1336
1337 /// Binds an existing constructor taking arguments Args...
1338 template <typename... Args> detail::initimpl::constructor<Args...> init() { return {}; }
1339 /// Like `init<Args...>()`, but the instance is always constructed through the alias class (even
1340 /// when not inheriting on the Python side).
1341 template <typename... Args> detail::initimpl::alias_constructor<Args...> init_alias() { return {}; }
1342
1343 /// Binds a factory function as a constructor
1344 template <typename Func, typename Ret = detail::initimpl::factory<Func>>
1345 Ret init(Func &&f) { return {std::forward<Func>(f)}; }
1346
1347 /// Dual-argument factory function: the first function is called when no alias is needed, the second
1348 /// when an alias is needed (i.e. due to python-side inheritance). Arguments must be identical.
1349 template <typename CFunc, typename AFunc, typename Ret = detail::initimpl::factory<CFunc, AFunc>>
1350 Ret init(CFunc &&c, AFunc &&a) {
1351 return {std::forward<CFunc>(c), std::forward<AFunc>(a)};
1352 }
1353
1354 /// Binds pickling functions `__getstate__` and `__setstate__` and ensures that the type
1355 /// returned by `__getstate__` is the same as the argument accepted by `__setstate__`.
1356 template <typename GetState, typename SetState>
1357 detail::initimpl::pickle_factory<GetState, SetState> pickle(GetState &&g, SetState &&s) {
1358 return {std::forward<GetState>(g), std::forward<SetState>(s)};
1359 }
1360
1361 /// Binds C++ enumerations and enumeration classes to Python
1362 template <typename Type> class enum_ : public class_<Type> {
1363 public:
1364 using class_<Type>::def;
1365 using class_<Type>::def_property_readonly_static;
1366 using Scalar = typename std::underlying_type<Type>::type;
1367
1368 template <typename... Extra>
1369 enum_(const handle &scope, const char *name, const Extra&... extra)
1370 : class_<Type>(scope, name, extra...), m_entries(), m_parent(scope) {
1371
1372 constexpr bool is_arithmetic = detail::any_of<std::is_same<arithmetic, Extra>...>::value;
1373
1374 auto m_entries_ptr = m_entries.inc_ref().ptr();
1375 def("__repr__", [name, m_entries_ptr](Type value) -> pybind11::str {
1376 for (const auto &kv : reinterpret_borrow<dict>(m_entries_ptr)) {
1377 if (pybind11::cast<Type>(kv.second) == value)
1378 return pybind11::str("{}.{}").format(name, kv.first);
1379 }
1380 return pybind11::str("{}.???").format(name);
1381 });
1382 def_property_readonly_static("__members__", [m_entries_ptr](object /* self */) {
1383 dict m;
1384 for (const auto &kv : reinterpret_borrow<dict>(m_entries_ptr))
1385 m[kv.first] = kv.second;
1386 return m;
1387 }, return_value_policy::copy);
1388 def(init([](Scalar i) { return static_cast<Type>(i); }));
1389 def("__int__", [](Type value) { return (Scalar) value; });
1390 #if PY_MAJOR_VERSION < 3
1391 def("__long__", [](Type value) { return (Scalar) value; });
1392 #endif
1393 def("__eq__", [](const Type &value, Type *value2) { return value2 && value == *value2; });
1394 def("__ne__", [](const Type &value, Type *value2) { return !value2 || value != *value2; });
1395 if (is_arithmetic) {
1396 def("__lt__", [](const Type &value, Type *value2) { return value2 && value < *value2; });
1397 def("__gt__", [](const Type &value, Type *value2) { return value2 && value > *value2; });
1398 def("__le__", [](const Type &value, Type *value2) { return value2 && value <= *value2; });
1399 def("__ge__", [](const Type &value, Type *value2) { return value2 && value >= *value2; });
1400 }
1401 if (std::is_convertible<Type, Scalar>::value) {
1402 // Don't provide comparison with the underlying type if the enum isn't convertible,
1403 // i.e. if Type is a scoped enum, mirroring the C++ behaviour. (NB: we explicitly
1404 // convert Type to Scalar below anyway because this needs to compile).
1405 def("__eq__", [](const Type &value, Scalar value2) { return (Scalar) value == value2; });
1406 def("__ne__", [](const Type &value, Scalar value2) { return (Scalar) value != value2; });
1407 if (is_arithmetic) {
1408 def("__lt__", [](const Type &value, Scalar value2) { return (Scalar) value < value2; });
1409 def("__gt__", [](const Type &value, Scalar value2) { return (Scalar) value > value2; });
1410 def("__le__", [](const Type &value, Scalar value2) { return (Scalar) value <= value2; });
1411 def("__ge__", [](const Type &value, Scalar value2) { return (Scalar) value >= value2; });
1412 def("__invert__", [](const Type &value) { return ~((Scalar) value); });
1413 def("__and__", [](const Type &value, Scalar value2) { return (Scalar) value & value2; });
1414 def("__or__", [](const Type &value, Scalar value2) { return (Scalar) value | value2; });
1415 def("__xor__", [](const Type &value, Scalar value2) { return (Scalar) value ^ value2; });
1416 def("__rand__", [](const Type &value, Scalar value2) { return (Scalar) value & value2; });
1417 def("__ror__", [](const Type &value, Scalar value2) { return (Scalar) value | value2; });
1418 def("__rxor__", [](const Type &value, Scalar value2) { return (Scalar) value ^ value2; });
1419 def("__and__", [](const Type &value, const Type &value2) { return (Scalar) value & (Scalar) value2; });
1420 def("__or__", [](const Type &value, const Type &value2) { return (Scalar) value | (Scalar) value2; });
1421 def("__xor__", [](const Type &value, const Type &value2) { return (Scalar) value ^ (Scalar) value2; });
1422 }
1423 }
1424 def("__hash__", [](const Type &value) { return (Scalar) value; });
1425 // Pickling and unpickling -- needed for use with the 'multiprocessing' module
1426 def(pickle([](const Type &value) { return pybind11::make_tuple((Scalar) value); },
1427 [](tuple t) { return static_cast<Type>(t[0].cast<Scalar>()); }));
1428 }
1429
1430 /// Export enumeration entries into the parent scope
1431 enum_& export_values() {
1432 for (const auto &kv : m_entries)
1433 m_parent.attr(kv.first) = kv.second;
1434 return *this;
1435 }
1436
1437 /// Add an enumeration entry
1438 enum_& value(char const* name, Type value) {
1439 auto v = pybind11::cast(value, return_value_policy::copy);
1440 this->attr(name) = v;
1441 m_entries[pybind11::str(name)] = v;
1442 return *this;
1443 }
1444
1445 private:
1446 dict m_entries;
1447 handle m_parent;
1448 };
1449
1450 NAMESPACE_BEGIN(detail)
1451
1452
1453 inline void keep_alive_impl(handle nurse, handle patient) {
1454 if (!nurse || !patient)
1455 pybind11_fail("Could not activate keep_alive!");
1456
1457 if (patient.is_none() || nurse.is_none())
1458 return; /* Nothing to keep alive or nothing to be kept alive by */
1459
1460 auto tinfo = all_type_info(Py_TYPE(nurse.ptr()));
1461 if (!tinfo.empty()) {
1462 /* It's a pybind-registered type, so we can store the patient in the
1463 * internal list. */
1464 add_patient(nurse.ptr(), patient.ptr());
1465 }
1466 else {
1467 /* Fall back to clever approach based on weak references taken from
1468 * Boost.Python. This is not used for pybind-registered types because
1469 * the objects can be destroyed out-of-order in a GC pass. */
1470 cpp_function disable_lifesupport(
1471 [patient](handle weakref) { patient.dec_ref(); weakref.dec_ref(); });
1472
1473 weakref wr(nurse, disable_lifesupport);
1474
1475 patient.inc_ref(); /* reference patient and leak the weak reference */
1476 (void) wr.release();
1477 }
1478 }
1479
1480 PYBIND11_NOINLINE inline void keep_alive_impl(size_t Nurse, size_t Patient, function_call &call, handle ret) {
1481 auto get_arg = [&](size_t n) {
1482 if (n == 0)
1483 return ret;
1484 else if (n == 1 && call.init_self)
1485 return call.init_self;
1486 else if (n <= call.args.size())
1487 return call.args[n - 1];
1488 return handle();
1489 };
1490
1491 keep_alive_impl(get_arg(Nurse), get_arg(Patient));
1492 }
1493
1494 inline std::pair<decltype(internals::registered_types_py)::iterator, bool> all_type_info_get_cache(PyTypeObject *type) {
1495 auto res = get_internals().registered_types_py
1496 #ifdef __cpp_lib_unordered_map_try_emplace
1497 .try_emplace(type);
1498 #else
1499 .emplace(type, std::vector<detail::type_info *>());
1500 #endif
1501 if (res.second) {
1502 // New cache entry created; set up a weak reference to automatically remove it if the type
1503 // gets destroyed:
1504 weakref((PyObject *) type, cpp_function([type](handle wr) {
1505 get_internals().registered_types_py.erase(type);
1506 wr.dec_ref();
1507 })).release();
1508 }
1509
1510 return res;
1511 }
1512
1513 template <typename Iterator, typename Sentinel, bool KeyIterator, return_value_policy Policy>
1514 struct iterator_state {
1515 Iterator it;
1516 Sentinel end;
1517 bool first_or_done;
1518 };
1519
1520 NAMESPACE_END(detail)
1521
1522 /// Makes a python iterator from a first and past-the-end C++ InputIterator.
1523 template <return_value_policy Policy = return_value_policy::reference_internal,
1524 typename Iterator,
1525 typename Sentinel,
1526 typename ValueType = decltype(*std::declval<Iterator>()),
1527 typename... Extra>
1528 iterator make_iterator(Iterator first, Sentinel last, Extra &&... extra) {
1529 typedef detail::iterator_state<Iterator, Sentinel, false, Policy> state;
1530
1531 if (!detail::get_type_info(typeid(state), false)) {
1532 class_<state>(handle(), "iterator", pybind11::module_local())
1533 .def("__iter__", [](state &s) -> state& { return s; })
1534 .def("__next__", [](state &s) -> ValueType {
1535 if (!s.first_or_done)
1536 ++s.it;
1537 else
1538 s.first_or_done = false;
1539 if (s.it == s.end) {
1540 s.first_or_done = true;
1541 throw stop_iteration();
1542 }
1543 return *s.it;
1544 }, std::forward<Extra>(extra)..., Policy);
1545 }
1546
1547 return cast(state{first, last, true});
1548 }
1549
1550 /// Makes an python iterator over the keys (`.first`) of a iterator over pairs from a
1551 /// first and past-the-end InputIterator.
1552 template <return_value_policy Policy = return_value_policy::reference_internal,
1553 typename Iterator,
1554 typename Sentinel,
1555 typename KeyType = decltype((*std::declval<Iterator>()).first),
1556 typename... Extra>
1557 iterator make_key_iterator(Iterator first, Sentinel last, Extra &&... extra) {
1558 typedef detail::iterator_state<Iterator, Sentinel, true, Policy> state;
1559
1560 if (!detail::get_type_info(typeid(state), false)) {
1561 class_<state>(handle(), "iterator", pybind11::module_local())
1562 .def("__iter__", [](state &s) -> state& { return s; })
1563 .def("__next__", [](state &s) -> KeyType {
1564 if (!s.first_or_done)
1565 ++s.it;
1566 else
1567 s.first_or_done = false;
1568 if (s.it == s.end) {
1569 s.first_or_done = true;
1570 throw stop_iteration();
1571 }
1572 return (*s.it).first;
1573 }, std::forward<Extra>(extra)..., Policy);
1574 }
1575
1576 return cast(state{first, last, true});
1577 }
1578
1579 /// Makes an iterator over values of an stl container or other container supporting
1580 /// `std::begin()`/`std::end()`
1581 template <return_value_policy Policy = return_value_policy::reference_internal,
1582 typename Type, typename... Extra> iterator make_iterator(Type &value, Extra&&... extra) {
1583 return make_iterator<Policy>(std::begin(value), std::end(value), extra...);
1584 }
1585
1586 /// Makes an iterator over the keys (`.first`) of a stl map-like container supporting
1587 /// `std::begin()`/`std::end()`
1588 template <return_value_policy Policy = return_value_policy::reference_internal,
1589 typename Type, typename... Extra> iterator make_key_iterator(Type &value, Extra&&... extra) {
1590 return make_key_iterator<Policy>(std::begin(value), std::end(value), extra...);
1591 }
1592
1593 template <typename InputType, typename OutputType> void implicitly_convertible() {
1594 struct set_flag {
1595 bool &flag;
1596 set_flag(bool &flag) : flag(flag) { flag = true; }
1597 ~set_flag() { flag = false; }
1598 };
1599 auto implicit_caster = [](PyObject *obj, PyTypeObject *type) -> PyObject * {
1600 static bool currently_used = false;
1601 if (currently_used) // implicit conversions are non-reentrant
1602 return nullptr;
1603 set_flag flag_helper(currently_used);
1604 if (!detail::make_caster<InputType>().load(obj, false))
1605 return nullptr;
1606 tuple args(1);
1607 args[0] = obj;
1608 PyObject *result = PyObject_Call((PyObject *) type, args.ptr(), nullptr);
1609 if (result == nullptr)
1610 PyErr_Clear();
1611 return result;
1612 };
1613
1614 if (auto tinfo = detail::get_type_info(typeid(OutputType)))
1615 tinfo->implicit_conversions.push_back(implicit_caster);
1616 else
1617 pybind11_fail("implicitly_convertible: Unable to find type " + type_id<OutputType>());
1618 }
1619
1620 template <typename ExceptionTranslator>
1621 void register_exception_translator(ExceptionTranslator&& translator) {
1622 detail::get_internals().registered_exception_translators.push_front(
1623 std::forward<ExceptionTranslator>(translator));
1624 }
1625
1626 /**
1627 * Wrapper to generate a new Python exception type.
1628 *
1629 * This should only be used with PyErr_SetString for now.
1630 * It is not (yet) possible to use as a py::base.
1631 * Template type argument is reserved for future use.
1632 */
1633 template <typename type>
1634 class exception : public object {
1635 public:
1636 exception(handle scope, const char *name, PyObject *base = PyExc_Exception) {
1637 std::string full_name = scope.attr("__name__").cast<std::string>() +
1638 std::string(".") + name;
1639 m_ptr = PyErr_NewException(const_cast<char *>(full_name.c_str()), base, NULL);
1640 if (hasattr(scope, name))
1641 pybind11_fail("Error during initialization: multiple incompatible "
1642 "definitions with name \"" + std::string(name) + "\"");
1643 scope.attr(name) = *this;
1644 }
1645
1646 // Sets the current python exception to this exception object with the given message
1647 void operator()(const char *message) {
1648 PyErr_SetString(m_ptr, message);
1649 }
1650 };
1651
1652 /**
1653 * Registers a Python exception in `m` of the given `name` and installs an exception translator to
1654 * translate the C++ exception to the created Python exception using the exceptions what() method.
1655 * This is intended for simple exception translations; for more complex translation, register the
1656 * exception object and translator directly.
1657 */
1658 template <typename CppException>
1659 exception<CppException> &register_exception(handle scope,
1660 const char *name,
1661 PyObject *base = PyExc_Exception) {
1662 static exception<CppException> ex(scope, name, base);
1663 register_exception_translator([](std::exception_ptr p) {
1664 if (!p) return;
1665 try {
1666 std::rethrow_exception(p);
1667 } catch (const CppException &e) {
1668 ex(e.what());
1669 }
1670 });
1671 return ex;
1672 }
1673
1674 NAMESPACE_BEGIN(detail)
1675 PYBIND11_NOINLINE inline void print(tuple args, dict kwargs) {
1676 auto strings = tuple(args.size());
1677 for (size_t i = 0; i < args.size(); ++i) {
1678 strings[i] = str(args[i]);
1679 }
1680 auto sep = kwargs.contains("sep") ? kwargs["sep"] : cast(" ");
1681 auto line = sep.attr("join")(strings);
1682
1683 object file;
1684 if (kwargs.contains("file")) {
1685 file = kwargs["file"].cast<object>();
1686 } else {
1687 try {
1688 file = module::import("sys").attr("stdout");
1689 } catch (const error_already_set &) {
1690 /* If print() is called from code that is executed as
1691 part of garbage collection during interpreter shutdown,
1692 importing 'sys' can fail. Give up rather than crashing the
1693 interpreter in this case. */
1694 return;
1695 }
1696 }
1697
1698 auto write = file.attr("write");
1699 write(line);
1700 write(kwargs.contains("end") ? kwargs["end"] : cast("\n"));
1701
1702 if (kwargs.contains("flush") && kwargs["flush"].cast<bool>())
1703 file.attr("flush")();
1704 }
1705 NAMESPACE_END(detail)
1706
1707 template <return_value_policy policy = return_value_policy::automatic_reference, typename... Args>
1708 void print(Args &&...args) {
1709 auto c = detail::collect_arguments<policy>(std::forward<Args>(args)...);
1710 detail::print(c.args(), c.kwargs());
1711 }
1712
1713 #if defined(WITH_THREAD) && !defined(PYPY_VERSION)
1714
1715 /* The functions below essentially reproduce the PyGILState_* API using a RAII
1716 * pattern, but there are a few important differences:
1717 *
1718 * 1. When acquiring the GIL from an non-main thread during the finalization
1719 * phase, the GILState API blindly terminates the calling thread, which
1720 * is often not what is wanted. This API does not do this.
1721 *
1722 * 2. The gil_scoped_release function can optionally cut the relationship
1723 * of a PyThreadState and its associated thread, which allows moving it to
1724 * another thread (this is a fairly rare/advanced use case).
1725 *
1726 * 3. The reference count of an acquired thread state can be controlled. This
1727 * can be handy to prevent cases where callbacks issued from an external
1728 * thread would otherwise constantly construct and destroy thread state data
1729 * structures.
1730 *
1731 * See the Python bindings of NanoGUI (http://github.com/wjakob/nanogui) for an
1732 * example which uses features 2 and 3 to migrate the Python thread of
1733 * execution to another thread (to run the event loop on the original thread,
1734 * in this case).
1735 */
1736
1737 class gil_scoped_acquire {
1738 public:
1739 PYBIND11_NOINLINE gil_scoped_acquire() {
1740 auto const &internals = detail::get_internals();
1741 tstate = (PyThreadState *) PyThread_get_key_value(internals.tstate);
1742
1743 if (!tstate) {
1744 tstate = PyThreadState_New(internals.istate);
1745 #if !defined(NDEBUG)
1746 if (!tstate)
1747 pybind11_fail("scoped_acquire: could not create thread state!");
1748 #endif
1749 tstate->gilstate_counter = 0;
1750 #if PY_MAJOR_VERSION < 3
1751 PyThread_delete_key_value(internals.tstate);
1752 #endif
1753 PyThread_set_key_value(internals.tstate, tstate);
1754 } else {
1755 release = detail::get_thread_state_unchecked() != tstate;
1756 }
1757
1758 if (release) {
1759 /* Work around an annoying assertion in PyThreadState_Swap */
1760 #if defined(Py_DEBUG)
1761 PyInterpreterState *interp = tstate->interp;
1762 tstate->interp = nullptr;
1763 #endif
1764 PyEval_AcquireThread(tstate);
1765 #if defined(Py_DEBUG)
1766 tstate->interp = interp;
1767 #endif
1768 }
1769
1770 inc_ref();
1771 }
1772
1773 void inc_ref() {
1774 ++tstate->gilstate_counter;
1775 }
1776
1777 PYBIND11_NOINLINE void dec_ref() {
1778 --tstate->gilstate_counter;
1779 #if !defined(NDEBUG)
1780 if (detail::get_thread_state_unchecked() != tstate)
1781 pybind11_fail("scoped_acquire::dec_ref(): thread state must be current!");
1782 if (tstate->gilstate_counter < 0)
1783 pybind11_fail("scoped_acquire::dec_ref(): reference count underflow!");
1784 #endif
1785 if (tstate->gilstate_counter == 0) {
1786 #if !defined(NDEBUG)
1787 if (!release)
1788 pybind11_fail("scoped_acquire::dec_ref(): internal error!");
1789 #endif
1790 PyThreadState_Clear(tstate);
1791 PyThreadState_DeleteCurrent();
1792 PyThread_delete_key_value(detail::get_internals().tstate);
1793 release = false;
1794 }
1795 }
1796
1797 PYBIND11_NOINLINE ~gil_scoped_acquire() {
1798 dec_ref();
1799 if (release)
1800 PyEval_SaveThread();
1801 }
1802 private:
1803 PyThreadState *tstate = nullptr;
1804 bool release = true;
1805 };
1806
1807 class gil_scoped_release {
1808 public:
1809 explicit gil_scoped_release(bool disassoc = false) : disassoc(disassoc) {
1810 // `get_internals()` must be called here unconditionally in order to initialize
1811 // `internals.tstate` for subsequent `gil_scoped_acquire` calls. Otherwise, an
1812 // initialization race could occur as multiple threads try `gil_scoped_acquire`.
1813 const auto &internals = detail::get_internals();
1814 tstate = PyEval_SaveThread();
1815 if (disassoc) {
1816 auto key = internals.tstate;
1817 #if PY_MAJOR_VERSION < 3
1818 PyThread_delete_key_value(key);
1819 #else
1820 PyThread_set_key_value(key, nullptr);
1821 #endif
1822 }
1823 }
1824 ~gil_scoped_release() {
1825 if (!tstate)
1826 return;
1827 PyEval_RestoreThread(tstate);
1828 if (disassoc) {
1829 auto key = detail::get_internals().tstate;
1830 #if PY_MAJOR_VERSION < 3
1831 PyThread_delete_key_value(key);
1832 #endif
1833 PyThread_set_key_value(key, tstate);
1834 }
1835 }
1836 private:
1837 PyThreadState *tstate;
1838 bool disassoc;
1839 };
1840 #elif defined(PYPY_VERSION)
1841 class gil_scoped_acquire {
1842 PyGILState_STATE state;
1843 public:
1844 gil_scoped_acquire() { state = PyGILState_Ensure(); }
1845 ~gil_scoped_acquire() { PyGILState_Release(state); }
1846 };
1847
1848 class gil_scoped_release {
1849 PyThreadState *state;
1850 public:
1851 gil_scoped_release() { state = PyEval_SaveThread(); }
1852 ~gil_scoped_release() { PyEval_RestoreThread(state); }
1853 };
1854 #else
1855 class gil_scoped_acquire { };
1856 class gil_scoped_release { };
1857 #endif
1858
1859 error_already_set::~error_already_set() {
1860 if (type) {
1861 gil_scoped_acquire gil;
1862 type.release().dec_ref();
1863 value.release().dec_ref();
1864 trace.release().dec_ref();
1865 }
1866 }
1867
1868 inline function get_type_overload(const void *this_ptr, const detail::type_info *this_type, const char *name) {
1869 handle self = detail::get_object_handle(this_ptr, this_type);
1870 if (!self)
1871 return function();
1872 handle type = self.get_type();
1873 auto key = std::make_pair(type.ptr(), name);
1874
1875 /* Cache functions that aren't overloaded in Python to avoid
1876 many costly Python dictionary lookups below */
1877 auto &cache = detail::get_internals().inactive_overload_cache;
1878 if (cache.find(key) != cache.end())
1879 return function();
1880
1881 function overload = getattr(self, name, function());
1882 if (overload.is_cpp_function()) {
1883 cache.insert(key);
1884 return function();
1885 }
1886
1887 /* Don't call dispatch code if invoked from overridden function.
1888 Unfortunately this doesn't work on PyPy. */
1889 #if !defined(PYPY_VERSION)
1890 PyFrameObject *frame = PyThreadState_Get()->frame;
1891 if (frame && (std::string) str(frame->f_code->co_name) == name &&
1892 frame->f_code->co_argcount > 0) {
1893 PyFrame_FastToLocals(frame);
1894 PyObject *self_caller = PyDict_GetItem(
1895 frame->f_locals, PyTuple_GET_ITEM(frame->f_code->co_varnames, 0));
1896 if (self_caller == self.ptr())
1897 return function();
1898 }
1899 #else
1900 /* PyPy currently doesn't provide a detailed cpyext emulation of
1901 frame objects, so we have to emulate this using Python. This
1902 is going to be slow..*/
1903 dict d; d["self"] = self; d["name"] = pybind11::str(name);
1904 PyObject *result = PyRun_String(
1905 "import inspect\n"
1906 "frame = inspect.currentframe()\n"
1907 "if frame is not None:\n"
1908 " frame = frame.f_back\n"
1909 " if frame is not None and str(frame.f_code.co_name) == name and "
1910 "frame.f_code.co_argcount > 0:\n"
1911 " self_caller = frame.f_locals[frame.f_code.co_varnames[0]]\n"
1912 " if self_caller == self:\n"
1913 " self = None\n",
1914 Py_file_input, d.ptr(), d.ptr());
1915 if (result == nullptr)
1916 throw error_already_set();
1917 if (d["self"].is_none())
1918 return function();
1919 Py_DECREF(result);
1920 #endif
1921
1922 return overload;
1923 }
1924
1925 template <class T> function get_overload(const T *this_ptr, const char *name) {
1926 auto tinfo = detail::get_type_info(typeid(T));
1927 return tinfo ? get_type_overload(this_ptr, tinfo, name) : function();
1928 }
1929
1930 #define PYBIND11_OVERLOAD_INT(ret_type, cname, name, ...) { \
1931 pybind11::gil_scoped_acquire gil; \
1932 pybind11::function overload = pybind11::get_overload(static_cast<const cname *>(this), name); \
1933 if (overload) { \
1934 auto o = overload(__VA_ARGS__); \
1935 if (pybind11::detail::cast_is_temporary_value_reference<ret_type>::value) { \
1936 static pybind11::detail::overload_caster_t<ret_type> caster; \
1937 return pybind11::detail::cast_ref<ret_type>(std::move(o), caster); \
1938 } \
1939 else return pybind11::detail::cast_safe<ret_type>(std::move(o)); \
1940 } \
1941 }
1942
1943 #define PYBIND11_OVERLOAD_NAME(ret_type, cname, name, fn, ...) \
1944 PYBIND11_OVERLOAD_INT(ret_type, cname, name, __VA_ARGS__) \
1945 return cname::fn(__VA_ARGS__)
1946
1947 #define PYBIND11_OVERLOAD_PURE_NAME(ret_type, cname, name, fn, ...) \
1948 PYBIND11_OVERLOAD_INT(ret_type, cname, name, __VA_ARGS__) \
1949 pybind11::pybind11_fail("Tried to call pure virtual function \"" #cname "::" name "\"");
1950
1951 #define PYBIND11_OVERLOAD(ret_type, cname, fn, ...) \
1952 PYBIND11_OVERLOAD_NAME(ret_type, cname, #fn, fn, __VA_ARGS__)
1953
1954 #define PYBIND11_OVERLOAD_PURE(ret_type, cname, fn, ...) \
1955 PYBIND11_OVERLOAD_PURE_NAME(ret_type, cname, #fn, fn, __VA_ARGS__)
1956
1957 NAMESPACE_END(PYBIND11_NAMESPACE)
1958
1959 #if defined(_MSC_VER)
1960 # pragma warning(pop)
1961 #elif defined(__INTEL_COMPILER)
1962 /* Leave ignored warnings on */
1963 #elif defined(__GNUG__) && !defined(__clang__)
1964 # pragma GCC diagnostic pop
1965 #endif