File: test_complexobject.py

package info (click to toggle)
pypy3 7.3.19%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 212,236 kB
  • sloc: python: 2,098,316; ansic: 540,565; sh: 21,462; asm: 14,419; cpp: 4,451; makefile: 4,209; objc: 761; xml: 530; exp: 499; javascript: 314; pascal: 244; lisp: 45; csh: 12; awk: 4
file content (84 lines) | stat: -rw-r--r-- 3,373 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
from pypy.module.cpyext.test.test_cpyext import AppTestCpythonExtensionBase
from pypy.module.cpyext.test.test_api import BaseApiTest, raises_w
from pypy.module.cpyext.complexobject import (
    PyComplex_FromDoubles, PyComplex_RealAsDouble, PyComplex_ImagAsDouble)

class TestComplexObject(BaseApiTest):
    def test_complexobject(self, space):
        w_value = PyComplex_FromDoubles(space, 1.2, 3.4)
        assert space.unwrap(w_value) == 1.2+3.4j
        assert PyComplex_RealAsDouble(space, w_value) == 1.2
        assert PyComplex_ImagAsDouble(space, w_value) == 3.4

        assert PyComplex_RealAsDouble(space, space.wrap(42)) == 42
        assert PyComplex_RealAsDouble(space, space.wrap(1.5)) == 1.5
        assert PyComplex_ImagAsDouble(space, space.wrap(1.5)) == 0.0

        # cpython accepts anything for PyComplex_ImagAsDouble
        assert PyComplex_ImagAsDouble(space, space.w_None) == 0.0
        with raises_w(space, TypeError):
            PyComplex_RealAsDouble(space, space.w_None)

class AppTestCComplex(AppTestCpythonExtensionBase):
    def test_AsCComplex(self):
        module = self.import_extension('foo', [
            ("as_tuple", "METH_O",
             """
                 Py_complex c = PyComplex_AsCComplex(args);
                 if (PyErr_Occurred()) return NULL;
                 return Py_BuildValue("dd", c.real, c.imag);
             """)])
        assert module.as_tuple(12-34j) == (12, -34)
        assert module.as_tuple(-3.14) == (-3.14, 0.0)
        raises(TypeError, module.as_tuple, "12")

    def test_FromCComplex(self):
        module = self.import_extension('foo', [
            ("test", "METH_NOARGS",
             """
                 Py_complex c = {1.2, 3.4};
                 return PyComplex_FromCComplex(c);
             """)])
        assert module.test() == 1.2 + 3.4j

    def test_PyComplex_to_WComplex(self):
        module = self.import_extension('foo', [
            ("test", "METH_NOARGS",
             """
                 Py_complex c = {1.2, 3.4};
                 PyObject *obj = PyObject_Malloc(sizeof(PyComplexObject));
                 obj = PyObject_Init(obj, &PyComplex_Type);
                 assert(obj != NULL);
                 ((PyComplexObject *)obj)->cval = c;
                 return obj;
             """)])
        assert module.test() == 1.2 + 3.4j

    def test_WComplex_to_PyComplex(self):
        module = self.import_extension('foo', [
            ("test", "METH_O",
             """
                 Py_complex c = ((PyComplexObject *)args)->cval;
                 return Py_BuildValue("dd", c.real, c.imag);
             """)])
        assert module.test(1.2 + 3.4j) == (1.2, 3.4)

    def test_PyComplex_AsCComplex(self):
        module = self.import_extension('foo', [
            ("test", "METH_O",
             """
                Py_complex c = PyComplex_AsCComplex(args);
                if (c.real == -1. && PyErr_Occurred()) {
                    return NULL;
                }
                return Py_BuildValue("dd", c.real, c.imag);
             """)])

        class C(complex):
            def __complex__(self):
                return -10-10j

        assert module.test(complex(1 + 1j)) == (1, 1)
        assert complex(C(1 + 1j)) == -10-10j, C(1 + 1j)
        # since it is a subclass, the __complex__ is ignored.
        assert module.test(C(1 + 1j)) == (1, 1)