From: Stefan Behnel Date: Sat, 4 Dec 2010 09:41:44 +0000 (+0100) Subject: provide Python complex type as cpython.complex.complex X-Git-Tag: 0.14.alpha0~23 X-Git-Url: http://git.tremily.us/?a=commitdiff_plain;h=0a2e5351617d49c333d5e4cc4f52deb1104b1a35;p=cython.git provide Python complex type as cpython.complex.complex --- diff --git a/Cython/Includes/cpython/complex.pxd b/Cython/Includes/cpython/complex.pxd index 116698d9..48091a4f 100644 --- a/Cython/Includes/cpython/complex.pxd +++ b/Cython/Includes/cpython/complex.pxd @@ -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 index 00000000..60361175 --- /dev/null +++ b/tests/run/builtincomplex.pyx @@ -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)