Debian Codegraph @

pypy/module/cpyext/complexobject.py

raw symbols SCIP
1 from rpython.rtyper.lltypesystem import lltype, rffi
2 from pypy.module.cpyext.api import (PyObjectFields, bootstrap_function,
3 cpython_api, cpython_struct, PyObject, build_type_checkers)
4 from pypy.module.cpyext.pyobject import (
5 make_typedescr, track_reference, from_ref)
6 from pypy.objspace.std.complexobject import W_ComplexObject, unpackcomplex
7 from pypy.interpreter.error import oefmt
8
9 PyComplex_Check, PyComplex_CheckExact = build_type_checkers("Complex")
10
11 Py_complex_t = rffi.CStruct('Py_complex_t',
12 ('real', rffi.DOUBLE),
13 ('imag', rffi.DOUBLE),
14 hints={'size': 2 * rffi.sizeof(rffi.DOUBLE)})
15 Py_complex_ptr = lltype.Ptr(Py_complex_t)
16
17 PyComplexObjectStruct = lltype.ForwardReference()
18 PyComplexObject = lltype.Ptr(PyComplexObjectStruct)
19 PyComplexObjectFields = PyObjectFields + \
20 (("cval", Py_complex_t),)
21 cpython_struct("PyComplexObject", PyComplexObjectFields, PyComplexObjectStruct)
22
23 @bootstrap_function
24 def init_complexobject(space):
25 "Type description of PyComplexObject"
26 make_typedescr(space.w_complex.layout.typedef,
27 basestruct=PyComplexObject.TO,
28 attach=complex_attach,
29 realize=complex_realize)
30
31 def complex_attach(space, py_obj, w_obj, w_userdata=None):
32 """
33 Fills a newly allocated PyComplexObject with the given complex object. The
34 value must not be modified.
35 """
36 assert isinstance(w_obj, W_ComplexObject)
37 py_obj = rffi.cast(PyComplexObject, py_obj)
38 py_obj.c_cval.c_real = w_obj.realval
39 py_obj.c_cval.c_imag = w_obj.imagval
40
41 def complex_realize(space, obj):
42 py_obj = rffi.cast(PyComplexObject, obj)
43 w_type = from_ref(space, rffi.cast(PyObject, obj.c_ob_type))
44 w_obj = space.allocate_instance(W_ComplexObject, w_type)
45 w_obj.__init__(py_obj.c_cval.c_real, py_obj.c_cval.c_imag)
46 track_reference(space, obj, w_obj)
47 return w_obj
48
49
50 @cpython_api([lltype.Float, lltype.Float], PyObject)
51 def PyComplex_FromDoubles(space, real, imag):
52 return space.newcomplex(real, imag)
53
54
55 @cpython_api([PyObject], lltype.Float, error=-1)
56 def PyComplex_RealAsDouble(space, w_obj):
57 if space.isinstance_w(w_obj, space.w_complex):
58 assert isinstance(w_obj, W_ComplexObject)
59 return w_obj.realval
60 else:
61 return space.float_w(w_obj)
62
63
64 @cpython_api([PyObject], lltype.Float, error=-1)
65 def PyComplex_ImagAsDouble(space, w_obj):
66 if space.isinstance_w(w_obj, space.w_complex):
67 assert isinstance(w_obj, W_ComplexObject)
68 return w_obj.imagval
69 else:
70 # CPython also accepts anything
71 return 0.0
72
73 @cpython_api([Py_complex_ptr], PyObject)
74 def _PyComplex_FromCComplex(space, v):
75 """Create a new Python complex number object from a C Py_complex value."""
76 return space.newcomplex(v.c_real, v.c_imag)
77
78 # lltype does not handle functions returning a structure. This implements a
79 # helper function, which takes as argument a reference to the return value.
80 # The real function is in src/complexobject.c
81 @cpython_api([PyObject, Py_complex_ptr], rffi.INT_real, error=-1)
82 def _PyComplex_AsCComplex(space, w_obj, result):
83 """Return the Py_complex value of the complex number op.
84
85 If op is not a Python complex number object but has a __complex__()
86 method, this method will first be called to convert op to a Python complex
87 number object."""
88 # return -1 on failure
89 realvalue, imagvalue = unpackcomplex(space, w_obj, allow_subclass=True)
90 result.c_real = realvalue
91 result.c_imag = imagvalue
92 return 0