| 48 | class type_caster<std::complex<T>> { |
| 49 | public: |
| 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 */) { |