MCPcopy Create free account
hub / github.com/pybind/pybind11 / load

Function load

include/pybind11/complex.h:50–80  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

48class type_caster<std::complex<T>> {
49public:
50 bool load(handle src, bool convert) {
51 if (!src) {
52 return false;
53 }
54 if (!convert && !PyComplex_Check(src.ptr())) {
55 return false;
56 }
57 handle src_or_index = src;
58 // PyPy: 7.3.7's 3.8 does not implement PyLong_*'s __index__ calls.
59 // The same logic is used in numeric_caster for ints and floats
60#if defined(PYPY_VERSION)
61 object index;
62 if (PYBIND11_INDEX_CHECK(src.ptr())) {
63 index = reinterpret_steal<object>(PyNumber_Index(src.ptr()));
64 if (!index) {
65 PyErr_Clear();
66 if (!convert)
67 return false;
68 } else {
69 src_or_index = index;
70 }
71 }
72#endif
73 Py_complex result = PyComplex_AsCComplex(src_or_index.ptr());
74 if (result.real == -1.0 && PyErr_Occurred()) {
75 PyErr_Clear();
76 return false;
77 }
78 value = std::complex<T>((T) result.real, (T) result.imag);
79 return true;
80 }
81
82 static handle
83 cast(const std::complex<T> &src, return_value_policy /* policy */, handle /* parent */) {

Callers

nothing calls this directly

Calls 1

ptrMethod · 0.80

Tested by

no test coverage detected