1/*
2 pybind11/detail/type_caster_base.h (originally first part of pybind11/cast.h)
3
4 Copyright (c) 2016 Wenzel Jakob <[email protected]>
5
6 All rights reserved. Use of this source code is governed by a
7 BSD-style license that can be found in the LICENSE file.
8*/
9
10#pragma once
11
12#include "../pytypes.h"
13#include "common.h"
14#include "descr.h"
15#include "internals.h"
16#include "typeid.h"
17
18#include <cstdint>
19#include <iterator>
20#include <new>
21#include <string>
22#include <type_traits>
23#include <typeindex>
24#include <typeinfo>
25#include <unordered_map>
26#include <utility>
27#include <vector>
28
29PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE)
30PYBIND11_NAMESPACE_BEGIN(detail)
31
32/// A life support system for temporary objects created by `type_caster::load()`.
33/// Adding a patient will keep it alive up until the enclosing function returns.
34class loader_life_support {
35private:
36 loader_life_support *parent = nullptr;
37 std::unordered_set<PyObject *> keep_alive;
38
39#if defined(WITH_THREAD)
40 // Store stack pointer in thread-local storage.
41 static PYBIND11_TLS_KEY_REF get_stack_tls_key() {
42# if PYBIND11_INTERNALS_VERSION == 4
43 return get_local_internals().loader_life_support_tls_key;
44# else
45 return get_internals().loader_life_support_tls_key;
46# endif
47 }
48 static loader_life_support *get_stack_top() {
49 return static_cast<loader_life_support *>(PYBIND11_TLS_GET_VALUE(get_stack_tls_key()));
50 }
51 static void set_stack_top(loader_life_support *value) {
52 PYBIND11_TLS_REPLACE_VALUE(get_stack_tls_key(), value);
53 }
54#else
55 // Use single global variable for stack.
56 static loader_life_support **get_stack_pp() {
57 static loader_life_support *global_stack = nullptr;
58 return global_stack;
59 }
60 static loader_life_support *get_stack_top() { return *get_stack_pp(); }
61 static void set_stack_top(loader_life_support *value) { *get_stack_pp() = value; }
62#endif
63
64public:
65 /// A new patient frame is created when a function is entered
66 loader_life_support() : parent{get_stack_top()} { set_stack_top(this); }
67
68 /// ... and destroyed after it returns
69 ~loader_life_support() {
70 if (get_stack_top() != this) {
71 pybind11_fail("loader_life_support: internal error");
72 }
73 set_stack_top(parent);
74 for (auto *item : keep_alive) {
75 Py_DECREF(item);
76 }
77 }
78
79 /// This can only be used inside a pybind11-bound function, either by `argument_loader`
80 /// at argument preparation time or by `py::cast()` at execution time.
81 PYBIND11_NOINLINE static void add_patient(handle h) {
82 loader_life_support *frame = get_stack_top();
83 if (!frame) {
84 // NOTE: It would be nice to include the stack frames here, as this indicates
85 // use of pybind11::cast<> outside the normal call framework, finding such
86 // a location is challenging. Developers could consider printing out
87 // stack frame addresses here using something like __builtin_frame_address(0)
88 throw cast_error("When called outside a bound function, py::cast() cannot "
89 "do Python -> C++ conversions which require the creation "
90 "of temporary values");
91 }
92
93 if (frame->keep_alive.insert(h.ptr()).second) {
94 Py_INCREF(h.ptr());
95 }
96 }
97};
98
99// Gets the cache entry for the given type, creating it if necessary. The return value is the pair
100// returned by emplace, i.e. an iterator for the entry and a bool set to `true` if the entry was
101// just created.
102inline std::pair<decltype(internals::registered_types_py)::iterator, bool>
103all_type_info_get_cache(PyTypeObject *type);
104
105// Populates a just-created cache entry.
106PYBIND11_NOINLINE void all_type_info_populate(PyTypeObject *t, std::vector<type_info *> &bases) {
107 std::vector<PyTypeObject *> check;
108 for (handle parent : reinterpret_borrow<tuple>(t->tp_bases)) {
109 check.push_back((PyTypeObject *) parent.ptr());
110 }
111
112 auto const &type_dict = get_internals().registered_types_py;
113 for (size_t i = 0; i < check.size(); i++) {
114 auto *type = check[i];
115 // Ignore Python2 old-style class super type:
116 if (!PyType_Check((PyObject *) type)) {
117 continue;
118 }
119
120 // Check `type` in the current set of registered python types:
121 auto it = type_dict.find(type);
122 if (it != type_dict.end()) {
123 // We found a cache entry for it, so it's either pybind-registered or has pre-computed
124 // pybind bases, but we have to make sure we haven't already seen the type(s) before:
125 // we want to follow Python/virtual C++ rules that there should only be one instance of
126 // a common base.
127 for (auto *tinfo : it->second) {
128 // NB: Could use a second set here, rather than doing a linear search, but since
129 // having a large number of immediate pybind11-registered types seems fairly
130 // unlikely, that probably isn't worthwhile.
131 bool found = false;
132 for (auto *known : bases) {
133 if (known == tinfo) {
134 found = true;
135 break;
136 }
137 }
138 if (!found) {
139 bases.push_back(tinfo);
140 }
141 }
142 } else if (type->tp_bases) {
143 // It's some python type, so keep follow its bases classes to look for one or more
144 // registered types
145 if (i + 1 == check.size()) {
146 // When we're at the end, we can pop off the current element to avoid growing
147 // `check` when adding just one base (which is typical--i.e. when there is no
148 // multiple inheritance)
149 check.pop_back();
150 i--;
151 }
152 for (handle parent : reinterpret_borrow<tuple>(type->tp_bases)) {
153 check.push_back((PyTypeObject *) parent.ptr());
154 }
155 }
156 }
157}
158
159/**
160 * Extracts vector of type_info pointers of pybind-registered roots of the given Python type. Will
161 * be just 1 pybind type for the Python type of a pybind-registered class, or for any Python-side
162 * derived class that uses single inheritance. Will contain as many types as required for a Python
163 * class that uses multiple inheritance to inherit (directly or indirectly) from multiple
164 * pybind-registered classes. Will be empty if neither the type nor any base classes are
165 * pybind-registered.
166 *
167 * The value is cached for the lifetime of the Python type.
168 */
169inline const std::vector<detail::type_info *> &all_type_info(PyTypeObject *type) {
170 auto ins = all_type_info_get_cache(type);
171 if (ins.second) {
172 // New cache entry: populate it
173 all_type_info_populate(type, ins.first->second);
174 }
175
176 return ins.first->second;
177}
178
179/**
180 * Gets a single pybind11 type info for a python type. Returns nullptr if neither the type nor any
181 * ancestors are pybind11-registered. Throws an exception if there are multiple bases--use
182 * `all_type_info` instead if you want to support multiple bases.
183 */
184PYBIND11_NOINLINE detail::type_info *get_type_info(PyTypeObject *type) {
185 const auto &bases = all_type_info(type);
186 if (bases.empty()) {
187 return nullptr;
188 }
189 if (bases.size() > 1) {
190 pybind11_fail(
191 "pybind11::detail::get_type_info: type has multiple pybind11-registered bases");
192 }
193 return bases.front();
194}
195
196inline detail::type_info *get_local_type_info(const std::type_index &tp) {
197 auto &locals = get_local_internals().registered_types_cpp;
198 auto it = locals.find(tp);
199 if (it != locals.end()) {
200 return it->second;
201 }
202 return nullptr;
203}
204
205inline detail::type_info *get_global_type_info(const std::type_index &tp) {
206 auto &types = get_internals().registered_types_cpp;
207 auto it = types.find(tp);
208 if (it != types.end()) {
209 return it->second;
210 }
211 return nullptr;
212}
213
214/// Return the type info for a given C++ type; on lookup failure can either throw or return
215/// nullptr.
216PYBIND11_NOINLINE detail::type_info *get_type_info(const std::type_index &tp,
217 bool throw_if_missing = false) {
218 if (auto *ltype = get_local_type_info(tp)) {
219 return ltype;
220 }
221 if (auto *gtype = get_global_type_info(tp)) {
222 return gtype;
223 }
224
225 if (throw_if_missing) {
226 std::string tname = tp.name();
227 detail::clean_type_id(tname);
228 pybind11_fail("pybind11::detail::get_type_info: unable to find type info for \""
229 + std::move(tname) + '"');
230 }
231 return nullptr;
232}
233
234PYBIND11_NOINLINE handle get_type_handle(const std::type_info &tp, bool throw_if_missing) {
235 detail::type_info *type_info = get_type_info(tp, throw_if_missing);
236 return handle(type_info ? ((PyObject *) type_info->type) : nullptr);
237}
238
239// Searches the inheritance graph for a registered Python instance, using all_type_info().
240PYBIND11_NOINLINE handle find_registered_python_instance(void *src,
241 const detail::type_info *tinfo) {
242 auto it_instances = get_internals().registered_instances.equal_range(src);
243 for (auto it_i = it_instances.first; it_i != it_instances.second; ++it_i) {
244 for (auto *instance_type : detail::all_type_info(Py_TYPE(it_i->second))) {
245 if (instance_type && same_type(*instance_type->cpptype, *tinfo->cpptype)) {
246 return handle((PyObject *) it_i->second).inc_ref();
247 }
248 }
249 }
250 return handle();
251}
252
253struct value_and_holder {
254 instance *inst = nullptr;
255 size_t index = 0u;
256 const detail::type_info *type = nullptr;
257 void **vh = nullptr;
258
259 // Main constructor for a found value/holder:
260 value_and_holder(instance *i, const detail::type_info *type, size_t vpos, size_t index)
261 : inst{i}, index{index}, type{type}, vh{inst->simple_layout
262 ? inst->simple_value_holder
263 : &inst->nonsimple.values_and_holders[vpos]} {}
264
265 // Default constructor (used to signal a value-and-holder not found by get_value_and_holder())
266 value_and_holder() = default;
267
268 // Used for past-the-end iterator
269 explicit value_and_holder(size_t index) : index{index} {}
270
271 template <typename V = void>
272 V *&value_ptr() const {
273 return reinterpret_cast<V *&>(vh[0]);
274 }
275 // True if this `value_and_holder` has a non-null value pointer
276 explicit operator bool() const { return value_ptr() != nullptr; }
277
278 template <typename H>
279 H &holder() const {
280 return reinterpret_cast<H &>(vh[1]);
281 }
282 bool holder_constructed() const {
283 return inst->simple_layout
284 ? inst->simple_holder_constructed
285 : (inst->nonsimple.status[index] & instance::status_holder_constructed) != 0u;
286 }
287 // NOLINTNEXTLINE(readability-make-member-function-const)
288 void set_holder_constructed(bool v = true) {
289 if (inst->simple_layout) {
290 inst->simple_holder_constructed = v;
291 } else if (v) {
292 inst->nonsimple.status[index] |= instance::status_holder_constructed;
293 } else {
294 inst->nonsimple.status[index] &= (std::uint8_t) ~instance::status_holder_constructed;
295 }
296 }
297 bool instance_registered() const {
298 return inst->simple_layout
299 ? inst->simple_instance_registered
300 : ((inst->nonsimple.status[index] & instance::status_instance_registered) != 0);
301 }
302 // NOLINTNEXTLINE(readability-make-member-function-const)
303 void set_instance_registered(bool v = true) {
304 if (inst->simple_layout) {
305 inst->simple_instance_registered = v;
306 } else if (v) {
307 inst->nonsimple.status[index] |= instance::status_instance_registered;
308 } else {
309 inst->nonsimple.status[index] &= (std::uint8_t) ~instance::status_instance_registered;
310 }
311 }
312};
313
314// Container for accessing and iterating over an instance's values/holders
315struct values_and_holders {
316private:
317 instance *inst;
318 using type_vec = std::vector<detail::type_info *>;
319 const type_vec &tinfo;
320
321public:
322 explicit values_and_holders(instance *inst)
323 : inst{inst}, tinfo(all_type_info(Py_TYPE(inst))) {}
324
325 struct iterator {
326 private:
327 instance *inst = nullptr;
328 const type_vec *types = nullptr;
329 value_and_holder curr;
330 friend struct values_and_holders;
331 iterator(instance *inst, const type_vec *tinfo)
332 : inst{inst}, types{tinfo},
333 curr(inst /* instance */,
334 types->empty() ? nullptr : (*types)[0] /* type info */,
335 0, /* vpos: (non-simple types only): the first vptr comes first */
336 0 /* index */) {}
337 // Past-the-end iterator:
338 explicit iterator(size_t end) : curr(end) {}
339
340 public:
341 bool operator==(const iterator &other) const { return curr.index == other.curr.index; }
342 bool operator!=(const iterator &other) const { return curr.index != other.curr.index; }
343 iterator &operator++() {
344 if (!inst->simple_layout) {
345 curr.vh += 1 + (*types)[curr.index]->holder_size_in_ptrs;
346 }
347 ++curr.index;
348 curr.type = curr.index < types->size() ? (*types)[curr.index] : nullptr;
349 return *this;
350 }
351 value_and_holder &operator*() { return curr; }
352 value_and_holder *operator->() { return &curr; }
353 };
354
355 iterator begin() { return iterator(inst, &tinfo); }
356 iterator end() { return iterator(tinfo.size()); }
357
358 iterator find(const type_info *find_type) {
359 auto it = begin(), endit = end();
360 while (it != endit && it->type != find_type) {
361 ++it;
362 }
363 return it;
364 }
365
366 size_t size() { return tinfo.size(); }
367};
368
369/**
370 * Extracts C++ value and holder pointer references from an instance (which may contain multiple
371 * values/holders for python-side multiple inheritance) that match the given type. Throws an error
372 * if the given type (or ValueType, if omitted) is not a pybind11 base of the given instance. If
373 * `find_type` is omitted (or explicitly specified as nullptr) the first value/holder are returned,
374 * regardless of type (and the resulting .type will be nullptr).
375 *
376 * The returned object should be short-lived: in particular, it must not outlive the called-upon
377 * instance.
378 */
379PYBIND11_NOINLINE value_and_holder
380instance::get_value_and_holder(const type_info *find_type /*= nullptr default in common.h*/,
381 bool throw_if_missing /*= true in common.h*/) {
382 // Optimize common case:
383 if (!find_type || Py_TYPE(this) == find_type->type) {
384 return value_and_holder(this, find_type, 0, 0);
385 }
386
387 detail::values_and_holders vhs(this);
388 auto it = vhs.find(find_type);
389 if (it != vhs.end()) {
390 return *it;
391 }
392
393 if (!throw_if_missing) {
394 return value_and_holder();
395 }
396
397#if defined(PYBIND11_DETAILED_ERROR_MESSAGES)
398 pybind11_fail("pybind11::detail::instance::get_value_and_holder: `"
399 + get_fully_qualified_tp_name(find_type->type)
400 + "' is not a pybind11 base of the given `"
401 + get_fully_qualified_tp_name(Py_TYPE(this)) + "' instance");
402#else
403 pybind11_fail(
404 "pybind11::detail::instance::get_value_and_holder: "
405 "type is not a pybind11 base of the given instance "
406 "(#define PYBIND11_DETAILED_ERROR_MESSAGES or compile in debug mode for type details)");
407#endif
408}
409
410PYBIND11_NOINLINE void instance::allocate_layout() {
411 const auto &tinfo = all_type_info(Py_TYPE(this));
412
413 const size_t n_types = tinfo.size();
414
415 if (n_types == 0) {
416 pybind11_fail(
417 "instance allocation failed: new instance has no pybind11-registered base types");
418 }
419
420 simple_layout
421 = n_types == 1 && tinfo.front()->holder_size_in_ptrs <= instance_simple_holder_in_ptrs();
422
423 // Simple path: no python-side multiple inheritance, and a small-enough holder
424 if (simple_layout) {
425 simple_value_holder[0] = nullptr;
426 simple_holder_constructed = false;
427 simple_instance_registered = false;
428 } else { // multiple base types or a too-large holder
429 // Allocate space to hold: [v1*][h1][v2*][h2]...[bb...] where [vN*] is a value pointer,
430 // [hN] is the (uninitialized) holder instance for value N, and [bb...] is a set of bool
431 // values that tracks whether each associated holder has been initialized. Each [block] is
432 // padded, if necessary, to an integer multiple of sizeof(void *).
433 size_t space = 0;
434 for (auto *t : tinfo) {
435 space += 1; // value pointer
436 space += t->holder_size_in_ptrs; // holder instance
437 }
438 size_t flags_at = space;
439 space += size_in_ptrs(n_types); // status bytes (holder_constructed and
440 // instance_registered)
441
442 // Allocate space for flags, values, and holders, and initialize it to 0 (flags and values,
443 // in particular, need to be 0). Use Python's memory allocation
444 // functions: Python is using pymalloc, which is designed to be
445 // efficient for small allocations like the one we're doing here;
446 // for larger allocations they are just wrappers around malloc.
447 // TODO: is this still true for pure Python 3.6?
448 nonsimple.values_and_holders = (void **) PyMem_Calloc(space, sizeof(void *));
449 if (!nonsimple.values_and_holders) {
450 throw std::bad_alloc();
451 }
452 nonsimple.status
453 = reinterpret_cast<std::uint8_t *>(&nonsimple.values_and_holders[flags_at]);
454 }
455 owned = true;
456}
457
458// NOLINTNEXTLINE(readability-make-member-function-const)
459PYBIND11_NOINLINE void instance::deallocate_layout() {
460 if (!simple_layout) {
461 PyMem_Free(nonsimple.values_and_holders);
462 }
463}
464
465PYBIND11_NOINLINE bool isinstance_generic(handle obj, const std::type_info &tp) {
466 handle type = detail::get_type_handle(tp, false);
467 if (!type) {
468 return false;
469 }
470 return isinstance(obj, type);
471}
472
473PYBIND11_NOINLINE handle get_object_handle(const void *ptr, const detail::type_info *type) {
474 auto &instances = get_internals().registered_instances;
475 auto range = instances.equal_range(ptr);
476 for (auto it = range.first; it != range.second; ++it) {
477 for (const auto &vh : values_and_holders(it->second)) {
478 if (vh.type == type) {
479 return handle((PyObject *) it->second);
480 }
481 }
482 }
483 return handle();
484}
485
486inline PyThreadState *get_thread_state_unchecked() {
487#if defined(PYPY_VERSION)
488 return PyThreadState_GET();
489#else
490 return _PyThreadState_UncheckedGet();
491#endif
492}
493
494// Forward declarations
495void keep_alive_impl(handle nurse, handle patient);
496inline PyObject *make_new_instance(PyTypeObject *type);
497
498class type_caster_generic {
499public:
500 PYBIND11_NOINLINE explicit type_caster_generic(const std::type_info &type_info)
501 : typeinfo(get_type_info(type_info)), cpptype(&type_info) {}
502
503 explicit type_caster_generic(const type_info *typeinfo)
504 : typeinfo(typeinfo), cpptype(typeinfo ? typeinfo->cpptype : nullptr) {}
505
506 bool load(handle src, bool convert) { return load_impl<type_caster_generic>(src, convert); }
507
508 PYBIND11_NOINLINE static handle cast(const void *_src,
509 return_value_policy policy,
510 handle parent,
511 const detail::type_info *tinfo,
512 void *(*copy_constructor)(const void *),
513 void *(*move_constructor)(const void *),
514 const void *existing_holder = nullptr) {
515 if (!tinfo) { // no type info: error will be set already
516 return handle();
517 }
518
519 void *src = const_cast<void *>(_src);
520 if (src == nullptr) {
521 return none().release();
522 }
523
524 if (handle registered_inst = find_registered_python_instance(src, tinfo)) {
525 return registered_inst;
526 }
527
528 auto inst = reinterpret_steal<object>(make_new_instance(tinfo->type));
529 auto *wrapper = reinterpret_cast<instance *>(inst.ptr());
530 wrapper->owned = false;
531 void *&valueptr = values_and_holders(wrapper).begin()->value_ptr();
532
533 switch (policy) {
534 case return_value_policy::automatic:
535 case return_value_policy::take_ownership:
536 valueptr = src;
537 wrapper->owned = true;
538 break;
539
540 case return_value_policy::automatic_reference:
541 case return_value_policy::reference:
542 valueptr = src;
543 wrapper->owned = false;
544 break;
545
546 case return_value_policy::copy:
547 if (copy_constructor) {
548 valueptr = copy_constructor(src);
549 } else {
550#if defined(PYBIND11_DETAILED_ERROR_MESSAGES)
551 std::string type_name(tinfo->cpptype->name());
552 detail::clean_type_id(type_name);
553 throw cast_error("return_value_policy = copy, but type " + type_name
554 + " is non-copyable!");
555#else
556 throw cast_error("return_value_policy = copy, but type is "
557 "non-copyable! (#define PYBIND11_DETAILED_ERROR_MESSAGES or "
558 "compile in debug mode for details)");
559#endif
560 }
561 wrapper->owned = true;
562 break;
563
564 case return_value_policy::move:
565 if (move_constructor) {
566 valueptr = move_constructor(src);
567 } else if (copy_constructor) {
568 valueptr = copy_constructor(src);
569 } else {
570#if defined(PYBIND11_DETAILED_ERROR_MESSAGES)
571 std::string type_name(tinfo->cpptype->name());
572 detail::clean_type_id(type_name);
573 throw cast_error("return_value_policy = move, but type " + type_name
574 + " is neither movable nor copyable!");
575#else
576 throw cast_error("return_value_policy = move, but type is neither "
577 "movable nor copyable! "
578 "(#define PYBIND11_DETAILED_ERROR_MESSAGES or compile in "
579 "debug mode for details)");
580#endif
581 }
582 wrapper->owned = true;
583 break;
584
585 case return_value_policy::reference_internal:
586 valueptr = src;
587 wrapper->owned = false;
588 keep_alive_impl(inst, parent);
589 break;
590
591 default:
592 throw cast_error("unhandled return_value_policy: should not happen!");
593 }
594
595 tinfo->init_instance(wrapper, existing_holder);
596
597 return inst.release();
598 }
599
600 // Base methods for generic caster; there are overridden in copyable_holder_caster
601 void load_value(value_and_holder &&v_h) {
602 auto *&vptr = v_h.value_ptr();
603 // Lazy allocation for unallocated values:
604 if (vptr == nullptr) {
605 const auto *type = v_h.type ? v_h.type : typeinfo;
606 if (type->operator_new) {
607 vptr = type->operator_new(type->type_size);
608 } else {
609#if defined(__cpp_aligned_new) && (!defined(_MSC_VER) || _MSC_VER >= 1912)
610 if (type->type_align > __STDCPP_DEFAULT_NEW_ALIGNMENT__) {
611 vptr = ::operator new(type->type_size, std::align_val_t(type->type_align));
612 } else {
613 vptr = ::operator new(type->type_size);
614 }
615#else
616 vptr = ::operator new(type->type_size);
617#endif
618 }
619 }
620 value = vptr;
621 }
622 bool try_implicit_casts(handle src, bool convert) {
623 for (const auto &cast : typeinfo->implicit_casts) {
624 type_caster_generic sub_caster(*cast.first);
625 if (sub_caster.load(src, convert)) {
626 value = cast.second(sub_caster.value);
627 return true;
628 }
629 }
630 return false;
631 }
632 bool try_direct_conversions(handle src) {
633 for (auto &converter : *typeinfo->direct_conversions) {
634 if (converter(src.ptr(), value)) {
635 return true;
636 }
637 }
638 return false;
639 }
640 void check_holder_compat() {}
641
642 PYBIND11_NOINLINE static void *local_load(PyObject *src, const type_info *ti) {
643 auto caster = type_caster_generic(ti);
644 if (caster.load(src, false)) {
645 return caster.value;
646 }
647 return nullptr;
648 }
649
650 /// Try to load with foreign typeinfo, if available. Used when there is no
651 /// native typeinfo, or when the native one wasn't able to produce a value.
652 PYBIND11_NOINLINE bool try_load_foreign_module_local(handle src) {
653 constexpr auto *local_key = PYBIND11_MODULE_LOCAL_ID;
654 const auto pytype = type::handle_of(src);
655 if (!hasattr(pytype, local_key)) {
656 return false;
657 }
658
659 type_info *foreign_typeinfo = reinterpret_borrow<capsule>(getattr(pytype, local_key));
660 // Only consider this foreign loader if actually foreign and is a loader of the correct cpp
661 // type
662 if (foreign_typeinfo->module_local_load == &local_load
663 || (cpptype && !same_type(*cpptype, *foreign_typeinfo->cpptype))) {
664 return false;
665 }
666
667 if (auto *result = foreign_typeinfo->module_local_load(src.ptr(), foreign_typeinfo)) {
668 value = result;
669 return true;
670 }
671 return false;
672 }
673
674 // Implementation of `load`; this takes the type of `this` so that it can dispatch the relevant
675 // bits of code between here and copyable_holder_caster where the two classes need different
676 // logic (without having to resort to virtual inheritance).
677 template <typename ThisT>
678 PYBIND11_NOINLINE bool load_impl(handle src, bool convert) {
679 if (!src) {
680 return false;
681 }
682 if (!typeinfo) {
683 return try_load_foreign_module_local(src);
684 }
685
686 auto &this_ = static_cast<ThisT &>(*this);
687 this_.check_holder_compat();
688
689 PyTypeObject *srctype = Py_TYPE(src.ptr());
690
691 // Case 1: If src is an exact type match for the target type then we can reinterpret_cast
692 // the instance's value pointer to the target type:
693 if (srctype == typeinfo->type) {
694 this_.load_value(reinterpret_cast<instance *>(src.ptr())->get_value_and_holder());
695 return true;
696 }
697 // Case 2: We have a derived class
698 if (PyType_IsSubtype(srctype, typeinfo->type)) {
699 const auto &bases = all_type_info(srctype);
700 bool no_cpp_mi = typeinfo->simple_type;
701
702 // Case 2a: the python type is a Python-inherited derived class that inherits from just
703 // one simple (no MI) pybind11 class, or is an exact match, so the C++ instance is of
704 // the right type and we can use reinterpret_cast.
705 // (This is essentially the same as case 2b, but because not using multiple inheritance
706 // is extremely common, we handle it specially to avoid the loop iterator and type
707 // pointer lookup overhead)
708 if (bases.size() == 1 && (no_cpp_mi || bases.front()->type == typeinfo->type)) {
709 this_.load_value(reinterpret_cast<instance *>(src.ptr())->get_value_and_holder());
710 return true;
711 }
712 // Case 2b: the python type inherits from multiple C++ bases. Check the bases to see
713 // if we can find an exact match (or, for a simple C++ type, an inherited match); if
714 // so, we can safely reinterpret_cast to the relevant pointer.
715 if (bases.size() > 1) {
716 for (auto *base : bases) {
717 if (no_cpp_mi ? PyType_IsSubtype(base->type, typeinfo->type)
718 : base->type == typeinfo->type) {
719 this_.load_value(
720 reinterpret_cast<instance *>(src.ptr())->get_value_and_holder(base));
721 return true;
722 }
723 }
724 }
725
726 // Case 2c: C++ multiple inheritance is involved and we couldn't find an exact type
727 // match in the registered bases, above, so try implicit casting (needed for proper C++
728 // casting when MI is involved).
729 if (this_.try_implicit_casts(src, convert)) {
730 return true;
731 }
732 }
733
734 // Perform an implicit conversion
735 if (convert) {
736 for (const auto &converter : typeinfo->implicit_conversions) {
737 auto temp = reinterpret_steal<object>(converter(src.ptr(), typeinfo->type));
738 if (load_impl<ThisT>(temp, false)) {
739 loader_life_support::add_patient(temp);
740 return true;
741 }
742 }
743 if (this_.try_direct_conversions(src)) {
744 return true;
745 }
746 }
747
748 // Failed to match local typeinfo. Try again with global.
749 if (typeinfo->module_local) {
750 if (auto *gtype = get_global_type_info(*typeinfo->cpptype)) {
751 typeinfo = gtype;
752 return load(src, false);
753 }
754 }
755
756 // Global typeinfo has precedence over foreign module_local
757 if (try_load_foreign_module_local(src)) {
758 return true;
759 }
760
761 // Custom converters didn't take None, now we convert None to nullptr.
762 if (src.is_none()) {
763 // Defer accepting None to other overloads (if we aren't in convert mode):
764 if (!convert) {
765 return false;
766 }
767 value = nullptr;
768 return true;
769 }
770
771 return false;
772 }
773
774 // Called to do type lookup and wrap the pointer and type in a pair when a dynamic_cast
775 // isn't needed or can't be used. If the type is unknown, sets the error and returns a pair
776 // with .second = nullptr. (p.first = nullptr is not an error: it becomes None).
777 PYBIND11_NOINLINE static std::pair<const void *, const type_info *>
778 src_and_type(const void *src,
779 const std::type_info &cast_type,
780 const std::type_info *rtti_type = nullptr) {
781 if (auto *tpi = get_type_info(cast_type)) {
782 return {src, const_cast<const type_info *>(tpi)};
783 }
784
785 // Not found, set error:
786 std::string tname = rtti_type ? rtti_type->name() : cast_type.name();
787 detail::clean_type_id(tname);
788 std::string msg = "Unregistered type : " + tname;
789 PyErr_SetString(PyExc_TypeError, msg.c_str());
790 return {nullptr, nullptr};
791 }
792
793 const type_info *typeinfo = nullptr;
794 const std::type_info *cpptype = nullptr;
795 void *value = nullptr;
796};
797
798/**
799 * Determine suitable casting operator for pointer-or-lvalue-casting type casters. The type caster
800 * needs to provide `operator T*()` and `operator T&()` operators.
801 *
802 * If the type supports moving the value away via an `operator T&&() &&` method, it should use
803 * `movable_cast_op_type` instead.
804 */
805template <typename T>
806using cast_op_type = conditional_t<std::is_pointer<remove_reference_t<T>>::value,
807 typename std::add_pointer<intrinsic_t<T>>::type,
808 typename std::add_lvalue_reference<intrinsic_t<T>>::type>;
809
810/**
811 * Determine suitable casting operator for a type caster with a movable value. Such a type caster
812 * needs to provide `operator T*()`, `operator T&()`, and `operator T&&() &&`. The latter will be
813 * called in appropriate contexts where the value can be moved rather than copied.
814 *
815 * These operator are automatically provided when using the PYBIND11_TYPE_CASTER macro.
816 */
817template <typename T>
818using movable_cast_op_type
819 = conditional_t<std::is_pointer<typename std::remove_reference<T>::type>::value,
820 typename std::add_pointer<intrinsic_t<T>>::type,
821 conditional_t<std::is_rvalue_reference<T>::value,
822 typename std::add_rvalue_reference<intrinsic_t<T>>::type,
823 typename std::add_lvalue_reference<intrinsic_t<T>>::type>>;
824
825// std::is_copy_constructible isn't quite enough: it lets std::vector<T> (and similar) through when
826// T is non-copyable, but code containing such a copy constructor fails to actually compile.
827template <typename T, typename SFINAE = void>
828struct is_copy_constructible : std::is_copy_constructible<T> {};
829
830// Specialization for types that appear to be copy constructible but also look like stl containers
831// (we specifically check for: has `value_type` and `reference` with `reference = value_type&`): if
832// so, copy constructability depends on whether the value_type is copy constructible.
833template <typename Container>
834struct is_copy_constructible<
835 Container,
836 enable_if_t<
837 all_of<std::is_copy_constructible<Container>,
838 std::is_same<typename Container::value_type &, typename Container::reference>,
839 // Avoid infinite recursion
840 negation<std::is_same<Container, typename Container::value_type>>>::value>>
841 : is_copy_constructible<typename Container::value_type> {};
842
843// Likewise for std::pair
844// (after C++17 it is mandatory that the copy constructor not exist when the two types aren't
845// themselves copy constructible, but this can not be relied upon when T1 or T2 are themselves
846// containers).
847template <typename T1, typename T2>
848struct is_copy_constructible<std::pair<T1, T2>>
849 : all_of<is_copy_constructible<T1>, is_copy_constructible<T2>> {};
850
851// The same problems arise with std::is_copy_assignable, so we use the same workaround.
852template <typename T, typename SFINAE = void>
853struct is_copy_assignable : std::is_copy_assignable<T> {};
854template <typename Container>
855struct is_copy_assignable<Container,
856 enable_if_t<all_of<std::is_copy_assignable<Container>,
857 std::is_same<typename Container::value_type &,
858 typename Container::reference>>::value>>
859 : is_copy_assignable<typename Container::value_type> {};
860template <typename T1, typename T2>
861struct is_copy_assignable<std::pair<T1, T2>>
862 : all_of<is_copy_assignable<T1>, is_copy_assignable<T2>> {};
863
864PYBIND11_NAMESPACE_END(detail)
865
866// polymorphic_type_hook<itype>::get(src, tinfo) determines whether the object pointed
867// to by `src` actually is an instance of some class derived from `itype`.
868// If so, it sets `tinfo` to point to the std::type_info representing that derived
869// type, and returns a pointer to the start of the most-derived object of that type
870// (in which `src` is a subobject; this will be the same address as `src` in most
871// single inheritance cases). If not, or if `src` is nullptr, it simply returns `src`
872// and leaves `tinfo` at its default value of nullptr.
873//
874// The default polymorphic_type_hook just returns src. A specialization for polymorphic
875// types determines the runtime type of the passed object and adjusts the this-pointer
876// appropriately via dynamic_cast<void*>. This is what enables a C++ Animal* to appear
877// to Python as a Dog (if Dog inherits from Animal, Animal is polymorphic, Dog is
878// registered with pybind11, and this Animal is in fact a Dog).
879//
880// You may specialize polymorphic_type_hook yourself for types that want to appear
881// polymorphic to Python but do not use C++ RTTI. (This is a not uncommon pattern
882// in performance-sensitive applications, used most notably in LLVM.)
883//
884// polymorphic_type_hook_base allows users to specialize polymorphic_type_hook with
885// std::enable_if. User provided specializations will always have higher priority than
886// the default implementation and specialization provided in polymorphic_type_hook_base.
887template <typename itype, typename SFINAE = void>
888struct polymorphic_type_hook_base {
889 static const void *get(const itype *src, const std::type_info *&) { return src; }
890};
891template <typename itype>
892struct polymorphic_type_hook_base<itype, detail::enable_if_t<std::is_polymorphic<itype>::value>> {
893 static const void *get(const itype *src, const std::type_info *&type) {
894 type = src ? &typeid(*src) : nullptr;
895 return dynamic_cast<const void *>(src);
896 }
897};
898template <typename itype, typename SFINAE = void>
899struct polymorphic_type_hook : public polymorphic_type_hook_base<itype> {};
900
901PYBIND11_NAMESPACE_BEGIN(detail)
902
903/// Generic type caster for objects stored on the heap
904template <typename type>
905class type_caster_base : public type_caster_generic {
906 using itype = intrinsic_t<type>;
907
908public:
909 static constexpr auto name = const_name<type>();
910
911 type_caster_base() : type_caster_base(typeid(type)) {}
912 explicit type_caster_base(const std::type_info &info) : type_caster_generic(info) {}
913
914 static handle cast(const itype &src, return_value_policy policy, handle parent) {
915 if (policy == return_value_policy::automatic
916 || policy == return_value_policy::automatic_reference) {
917 policy = return_value_policy::copy;
918 }
919 return cast(&src, policy, parent);
920 }
921
922 static handle cast(itype &&src, return_value_policy, handle parent) {
923 return cast(&src, return_value_policy::move, parent);
924 }
925
926 // Returns a (pointer, type_info) pair taking care of necessary type lookup for a
927 // polymorphic type (using RTTI by default, but can be overridden by specializing
928 // polymorphic_type_hook). If the instance isn't derived, returns the base version.
929 static std::pair<const void *, const type_info *> src_and_type(const itype *src) {
930 const auto &cast_type = typeid(itype);
931 const std::type_info *instance_type = nullptr;
932 const void *vsrc = polymorphic_type_hook<itype>::get(src, instance_type);
933 if (instance_type && !same_type(cast_type, *instance_type)) {
934 // This is a base pointer to a derived type. If the derived type is registered
935 // with pybind11, we want to make the full derived object available.
936 // In the typical case where itype is polymorphic, we get the correct
937 // derived pointer (which may be != base pointer) by a dynamic_cast to
938 // most derived type. If itype is not polymorphic, we won't get here
939 // except via a user-provided specialization of polymorphic_type_hook,
940 // and the user has promised that no this-pointer adjustment is
941 // required in that case, so it's OK to use static_cast.
942 if (const auto *tpi = get_type_info(*instance_type)) {
943 return {vsrc, tpi};
944 }
945 }
946 // Otherwise we have either a nullptr, an `itype` pointer, or an unknown derived pointer,
947 // so don't do a cast
948 return type_caster_generic::src_and_type(src, cast_type, instance_type);
949 }
950
951 static handle cast(const itype *src, return_value_policy policy, handle parent) {
952 auto st = src_and_type(src);
953 return type_caster_generic::cast(st.first,
954 policy,
955 parent,
956 st.second,
957 make_copy_constructor(src),
958 make_move_constructor(src));
959 }
960
961 static handle cast_holder(const itype *src, const void *holder) {
962 auto st = src_and_type(src);
963 return type_caster_generic::cast(st.first,
964 return_value_policy::take_ownership,
965 {},
966 st.second,
967 nullptr,
968 nullptr,
969 holder);
970 }
971
972 template <typename T>
973 using cast_op_type = detail::cast_op_type<T>;
974
975 // NOLINTNEXTLINE(google-explicit-constructor)
976 operator itype *() { return (type *) value; }
977 // NOLINTNEXTLINE(google-explicit-constructor)
978 operator itype &() {
979 if (!value) {
980 throw reference_cast_error();
981 }
982 return *((itype *) value);
983 }
984
985protected:
986 using Constructor = void *(*) (const void *);
987
988 /* Only enabled when the types are {copy,move}-constructible *and* when the type
989 does not have a private operator new implementation. A comma operator is used in the
990 decltype argument to apply SFINAE to the public copy/move constructors.*/
991 template <typename T, typename = enable_if_t<is_copy_constructible<T>::value>>
992 static auto make_copy_constructor(const T *)
993 -> decltype(new T(std::declval<const T>()), Constructor{}) {
994 return [](const void *arg) -> void * { return new T(*reinterpret_cast<const T *>(arg)); };
995 }
996
997 template <typename T, typename = enable_if_t<std::is_move_constructible<T>::value>>
998 static auto make_move_constructor(const T *)
999 -> decltype(new T(std::declval<T &&>()), Constructor{}) {
1000 return [](const void *arg) -> void * {
1001 return new T(std::move(*const_cast<T *>(reinterpret_cast<const T *>(arg))));
1002 };
1003 }
1004
1005 static Constructor make_copy_constructor(...) { return nullptr; }
1006 static Constructor make_move_constructor(...) { return nullptr; }
1007};
1008
1009PYBIND11_NAMESPACE_END(detail)
1010PYBIND11_NAMESPACE_END(PYBIND11_NAMESPACE)
1011