diff --git a/src/wrapper/_pvt_struct_v2.cpp b/src/wrapper/_pvt_struct_v2.cpp index e7a1c05ad26e965c8f901308ca74d9d07d8d15ef..d8e9d91be25a9e6cb2dfe31f881ed7b6f5ddfbcc 100644 --- a/src/wrapper/_pvt_struct_v2.cpp +++ b/src/wrapper/_pvt_struct_v2.cpp @@ -755,7 +755,27 @@ np_complex_float(char *p, PyObject *v, const formatdef *f) else { float re = 0.0f; float im = 0.0f; - Py_complex cplx = PyComplex_AsCComplex(v); + Py_complex cplx; +#if (PY_VERSION_HEX < 0x02050000) + { + if (PyComplex_Check(v)) + cplx = PyComplex_AsCComplex(v); + else if (PyObject_HasAttrString(v, "__complex__")) + { + PyObject *v2 = PyObject_CallMethod(v, "__complex__", ""); + cplx = PyComplex_AsCComplex(v2); + Py_DECREF(v2); + } + else + { + PyErr_SetString(StructError, + "required argument is not a complex"); + return -1; + } + } +#else + cplx = PyComplex_AsCComplex(v); +#endif if (PyErr_Occurred()) { PyErr_SetString(StructError, "required argument is not a complex"); @@ -785,7 +805,27 @@ np_complex_double(char *p, PyObject *v, const formatdef *f) else { double re = 0.0; double im = 0.0; - Py_complex cplx = PyComplex_AsCComplex(v); + Py_complex cplx; +#if (PY_VERSION_HEX < 0x02050000) + { + if (PyComplex_Check(v)) + cplx = PyComplex_AsCComplex(v); + else if (PyObject_HasAttrString(v, "__complex__")) + { + PyObject *v2 = PyObject_CallMethod(v, "__complex__", ""); + cplx = PyComplex_AsCComplex(v2); + Py_DECREF(v2); + } + else + { + PyErr_SetString(StructError, + "required argument is not a complex"); + return -1; + } + } +#else + cplx = PyComplex_AsCComplex(v); +#endif if (PyErr_Occurred()) { PyErr_SetString(StructError, "required argument is not a complex");