From 0e1ba84ef86df39fdce1b5fefc088a206662e7ae Mon Sep 17 00:00:00 2001
From: Andreas Kloeckner <inform@tiker.net>
Date: Wed, 23 Jan 2013 01:10:43 -0500
Subject: [PATCH] Work around behavior of PyComplex_AsCComplex in Py 2.5.

---
 src/wrapper/_pvt_struct_v2.cpp | 44 ++++++++++++++++++++++++++++++++--
 1 file changed, 42 insertions(+), 2 deletions(-)

diff --git a/src/wrapper/_pvt_struct_v2.cpp b/src/wrapper/_pvt_struct_v2.cpp
index e7a1c05a..d8e9d91b 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");
-- 
GitLab