Debian Codegraph @

pypy/module/cpyext/test/test_number.py

raw symbols SCIP
1 import pytest
2 from rpython.rtyper.lltypesystem import lltype
3 from pypy.interpreter.error import OperationError
4 from pypy.module.cpyext.test.test_api import BaseApiTest
5 from pypy.module.cpyext.test.test_cpyext import AppTestCpythonExtensionBase
6 from pypy.module.cpyext.number import (
7 PyNumber_Long, PyNumber_Index, PyNumber_Add,
8 PyNumber_Multiply, PyNumber_InPlaceMultiply, PyNumber_Absolute,
9 PyNumber_Power, PyNumber_InPlacePower)
10 from pypy.module.cpyext.longobject import PyLong_CheckExact
11 from pypy.module.cpyext.object import PyObject_Size
12
13 class TestIterator(BaseApiTest):
14 def test_number_long(self, space):
15 w_l = PyNumber_Long(space, space.wrap(123))
16 assert PyLong_CheckExact(space, w_l)
17 w_l = PyNumber_Long(space, space.wrap("123"))
18 assert PyLong_CheckExact(space, w_l)
19
20 def test_number_long2(self, space):
21 w_l = PyNumber_Long(space, space.wraplong(123L))
22 assert PyLong_CheckExact(space, w_l)
23 w_l = PyNumber_Long(space, space.wrap(2 << 65))
24 assert PyLong_CheckExact(space, w_l)
25 w_l = PyNumber_Long(space, space.wrap(42.3))
26 assert PyLong_CheckExact(space, w_l)
27 w_l = PyNumber_Long(space, space.wrap("42"))
28 assert PyLong_CheckExact(space, w_l)
29
30 def test_number_index(self, space):
31 w_l = PyNumber_Index(space, space.wraplong(123L))
32 assert PyLong_CheckExact(space, w_l)
33 with pytest.raises(OperationError):
34 PyNumber_Index(space, space.wrap(42.3))
35
36 def test_numbermethods(self, space, api):
37 assert "ab" == space.unwrap(
38 PyNumber_Add(space, space.wrap("a"), space.wrap("b")))
39 assert "aaa" == space.unwrap(
40 PyNumber_Multiply(space, space.wrap("a"), space.wrap(3)))
41
42 w_l = space.newlist([1, 2, 3])
43 w_l2 = PyNumber_Multiply(space, w_l, space.wrap(3))
44 assert PyObject_Size(space, w_l2) == 9
45 assert PyObject_Size(space, w_l) == 3
46
47 w_l3 = PyNumber_InPlaceMultiply(space, w_l, space.wrap(3))
48 assert PyObject_Size(space, w_l) == 9
49 assert w_l3 is w_l
50
51 # unary function
52 assert 9 == space.unwrap(PyNumber_Absolute(space, space.wrap(-9)))
53
54 # power
55 assert 9 == space.unwrap(
56 PyNumber_Power(space, space.wrap(3), space.wrap(2), space.w_None))
57 assert 4 == space.unwrap(
58 PyNumber_Power(space, space.wrap(3), space.wrap(2), space.wrap(5)))
59 assert 9 == space.unwrap(
60 PyNumber_InPlacePower(space, space.wrap(3), space.wrap(2), space.w_None))
61
62
63 class AppTestCNumber(AppTestCpythonExtensionBase):
64 def test_Check(self):
65 import sys
66 mod = self.import_extension('foo', [
67 ("PyNumber_Check", "METH_O",
68 '''
69 int val = PyNumber_Check(args);
70 return PyLong_FromLong(val);
71 '''),
72 ("PyIndex_Check", "METH_O",
73 '''
74 int val = PyIndex_Check(args);
75 return PyLong_FromLong(val);
76 '''),
77 ])
78 if 0:
79 val = mod.PyNumber_Check(10)
80 assert val == 1
81 assert mod.PyIndex_Check(12)
82 assert mod.PyIndex_Check(-12)
83 assert not mod.PyIndex_Check(12.1)
84 assert not mod.PyIndex_Check('12')
85 assert not mod.PyIndex_Check(1 + 3j)
86
87 assert mod.PyNumber_Check(12)
88 assert mod.PyNumber_Check(-12)
89 assert mod.PyNumber_Check(12.1)
90 assert not mod.PyNumber_Check('12')
91 assert mod.PyNumber_Check(1 + 3j)
92 #
93 class MyIndex:
94 def __index__(self):
95 return 42
96
97 val = mod.PyNumber_Check(MyIndex())
98 assert val == 1
99
100 # issue 3383: CPython only checks for the presence of __index__,
101 # not that it is valid
102
103 class Raises:
104 def __index__(self):
105 raise ValueError(42)
106
107 class Missing:
108 pass
109
110 m = Raises()
111 assert mod.PyIndex_Check(m)
112 m = Missing()
113 assert not mod.PyIndex_Check(m)
114
115 def test_number_tobase(self):
116 import sys
117 mod = self.import_extension('foo', [
118 ("pynumber_tobase", "METH_VARARGS",
119 """
120 PyObject *obj;
121 int base;
122 if (!PyArg_ParseTuple(args, "Oi:pynumber_tobase",
123 &obj, &base)) {
124 return NULL;
125 }
126 return PyNumber_ToBase(obj, base);
127 """)])
128 assert mod.pynumber_tobase(123, 2) == '0b1111011'
129 assert mod.pynumber_tobase(123, 8) == '0o173'
130 assert mod.pynumber_tobase(123, 10) == '123'
131 assert mod.pynumber_tobase(123, 16) == '0x7b'
132 assert mod.pynumber_tobase(-123, 2) == '-0b1111011'
133 assert mod.pynumber_tobase(-123, 8) == '-0o173'
134 assert mod.pynumber_tobase(-123, 10) == '-123'
135 assert mod.pynumber_tobase(-123, 16) == '-0x7b'
136 try:
137 mod.pynumber_tobase(123.0, 10)
138 except TypeError:
139 pass
140 else:
141 assert False, 'expected TypeError'
142 try:
143 mod.pynumber_tobase('123', 10)
144 except TypeError:
145 pass
146 else:
147 assert False, 'expected TypeError'
148 if 'PyPy' in sys.version or sys.version_info >= (3,7):
149 # bpo 38643
150 try:
151 mod.pynumber_tobase(123, 0)
152 except SystemError:
153 pass
154 else:
155 assert False, 'expected TypeError'
156 # large number:
157 num = 2**66
158 assert mod.pynumber_tobase(num, 2) == '0b1' + '0' * 66
159 assert mod.pynumber_tobase(num, 8) == '0o10000000000000000000000'
160 assert mod.pynumber_tobase(num, 10) == str(num)
161 assert mod.pynumber_tobase(num, 16) == '0x40000000000000000'
162
163 def test_number_to_ssize_t(self):
164 import sys
165 mod = self.import_extension('foo', [
166 ("to_ssize_t", "METH_VARARGS",
167 """
168 PyObject *obj;
169 PyObject *exc = NULL;
170 long long value;
171 if (!PyArg_ParseTuple(args, "O|O:to_ssize_t",
172 &obj, &exc)) {
173 return NULL;
174 }
175 if (exc == NULL) {
176 // printf("got no exc\\n");
177 } else {
178 // printf("got exc\\n");
179 }
180 value = PyNumber_AsSsize_t(obj, exc);
181 if (PyErr_Occurred()) {
182 return NULL;
183 }
184 return PyLong_FromLongLong(value);
185 """)])
186 assert mod.to_ssize_t(2 ** 68) == sys.maxsize
187 assert mod.to_ssize_t(12) == 12
188 raises(TypeError, mod.to_ssize_t, 2 ** 68, TypeError)