provide Python complex type as cpython.complex.complex
authorStefan Behnel <scoder@users.berlios.de>
Sat, 4 Dec 2010 09:41:44 +0000 (10:41 +0100)
committerStefan Behnel <scoder@users.berlios.de>
Sat, 4 Dec 2010 09:41:44 +0000 (10:41 +0100)
Cython/Includes/cpython/complex.pxd
tests/run/builtincomplex.pyx [new file with mode: 0644]

index 116698d91b5818d7801f6fd33e6373bd18705fc8..48091a4f9e72c300c2440e645b87b00683efad3d 100644 (file)
@@ -1,6 +1,9 @@
 
 cdef extern from "Python.h":
-    ctypedef struct Py_complex
+
+    ctypedef struct Py_complex:
+        double imag
+        double real
 
     ############################################################################
     # 7.2.5.2 Complex Numbers as Python Objects
@@ -9,6 +12,12 @@ cdef extern from "Python.h":
     # PyComplexObject
     # This subtype of PyObject represents a Python complex number object. 
 
+    ctypedef class __builtin__.complex [object PyComplexObject]:
+        cdef Py_complex cval
+        # not making these available to keep them read-only:
+        #cdef double imag "cval.imag"
+        #cdef double real "cval.real"
+
     # PyTypeObject PyComplex_Type
     # This instance of PyTypeObject represents the Python complex
     # number type. It is the same object as complex and
diff --git a/tests/run/builtincomplex.pyx b/tests/run/builtincomplex.pyx
new file mode 100644 (file)
index 0000000..6036117
--- /dev/null
@@ -0,0 +1,29 @@
+
+from cpython.complex cimport complex
+
+def complex_attributes():
+    """
+    >>> complex_attributes()
+    (1.0, 2.0)
+    """
+    cdef complex c = 1+2j
+    return (c.real, c.imag)
+
+def complex_attributes_assign():
+    """
+    >>> complex_attributes_assign()
+    (10.0, 20.0)
+    """
+    cdef complex c = 1+2j
+    c.cval.real, c.cval.imag = 10, 20
+    return (c.real, c.imag)
+
+def complex_cstruct_assign():
+    """
+    >>> complex_cstruct_assign()
+    (10.0, 20.0)
+    """
+    cdef complex c = 1+2j
+    cval = &c.cval
+    cval.real, cval.imag = 10, 20
+    return (c.real, c.imag)