Commit 0a2e5351 authored by Stefan Behnel's avatar Stefan Behnel

provide Python complex type as cpython.complex.complex

parent d9ce56c7
cdef extern from "Python.h": 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 # 7.2.5.2 Complex Numbers as Python Objects
...@@ -9,6 +12,12 @@ cdef extern from "Python.h": ...@@ -9,6 +12,12 @@ cdef extern from "Python.h":
# PyComplexObject # PyComplexObject
# This subtype of PyObject represents a Python complex number object. # 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 # PyTypeObject PyComplex_Type
# This instance of PyTypeObject represents the Python complex # This instance of PyTypeObject represents the Python complex
# number type. It is the same object as complex and # number type. It is the same object as complex and
......
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)
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment