From 14f6b74043ce7f5fec054107db9818f436ee1497 Mon Sep 17 00:00:00 2001 From: Andreas Kloeckner <inform@tiker.net> Date: Wed, 23 Jan 2013 01:43:10 -0500 Subject: [PATCH] Py 2.5 complex workaround: If no __complex__, still call AsCComplex. --- src/wrapper/_pvt_struct_v2.cpp | 44 +++++++++++++--------------------- 1 file changed, 16 insertions(+), 28 deletions(-) diff --git a/src/wrapper/_pvt_struct_v2.cpp b/src/wrapper/_pvt_struct_v2.cpp index f0762c62..23a40781 100644 --- a/src/wrapper/_pvt_struct_v2.cpp +++ b/src/wrapper/_pvt_struct_v2.cpp @@ -757,22 +757,16 @@ np_complex_float(char *p, PyObject *v, const formatdef *f) float im = 0.0f; Py_complex cplx; #if (PY_VERSION_HEX < 0x02060000) + if (PyComplex_Check(v)) + cplx = PyComplex_AsCComplex(v); + else if (PyObject_HasAttrString(v, "__complex__")) { - 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; - } + PyObject *v2 = PyObject_CallMethod(v, "__complex__", ""); + cplx = PyComplex_AsCComplex(v2); + Py_DECREF(v2); } + else + cplx = PyComplex_AsCComplex(v); #else cplx = PyComplex_AsCComplex(v); #endif @@ -807,22 +801,16 @@ np_complex_double(char *p, PyObject *v, const formatdef *f) double im = 0.0; Py_complex cplx; #if (PY_VERSION_HEX < 0x02060000) + if (PyComplex_Check(v)) + cplx = PyComplex_AsCComplex(v); + else if (PyObject_HasAttrString(v, "__complex__")) { - 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; - } + PyObject *v2 = PyObject_CallMethod(v, "__complex__", ""); + cplx = PyComplex_AsCComplex(v2); + Py_DECREF(v2); } + else + cplx = PyComplex_AsCComplex(v); #else cplx = PyComplex_AsCComplex(v); #endif -- GitLab