Debian Codegraph @

Lib/test/test_capi/test_number.py

raw
1 import itertools
2 import operator
3 import sys
4 import unittest
5 import warnings
6
7 from test.support import cpython_only, import_helper
8
9 _testcapi = import_helper.import_module('_testcapi')
10 from _testcapi import PY_SSIZE_T_MAX, PY_SSIZE_T_MIN
11
12 try:
13 from _testbuffer import ndarray
14 except ImportError:
15 ndarray = None
16
17 NULL = None
18
19 class BadDescr:
20 def __get__(self, obj, objtype=None):
21 raise RuntimeError
22
23 class WithDunder:
24 def _meth(self, *args):
25 if self.val:
26 return self.val
27 if self.exc:
28 raise self.exc
29 @classmethod
30 def with_val(cls, val):
31 obj = super().__new__(cls)
32 obj.val = val
33 obj.exc = None
34 setattr(cls, cls.methname, cls._meth)
35 return obj
36
37 @classmethod
38 def with_exc(cls, exc):
39 obj = super().__new__(cls)
40 obj.val = None
41 obj.exc = exc
42 setattr(cls, cls.methname, cls._meth)
43 return obj
44
45 class HasBadAttr:
46 def __new__(cls):
47 obj = super().__new__(cls)
48 setattr(cls, cls.methname, BadDescr())
49 return obj
50
51
52 class IndexLike(WithDunder):
53 methname = '__index__'
54
55 class IntLike(WithDunder):
56 methname = '__int__'
57
58 class FloatLike(WithDunder):
59 methname = '__float__'
60
61
62 def subclassof(base):
63 return type(base.__name__ + 'Subclass', (base,), {})
64
65
66 class SomeError(Exception):
67 pass
68
69 class OtherError(Exception):
70 pass
71
72
73 class CAPITest(unittest.TestCase):
74 def test_check(self):
75 # Test PyNumber_Check()
76 check = _testcapi.number_check
77
78 self.assertTrue(check(1))
79 self.assertTrue(check(IndexLike.with_val(1)))
80 self.assertTrue(check(IntLike.with_val(99)))
81 self.assertTrue(check(0.5))
82 self.assertTrue(check(FloatLike.with_val(4.25)))
83 self.assertTrue(check(1+2j))
84
85 self.assertFalse(check([]))
86 self.assertFalse(check("abc"))
87 self.assertFalse(check(object()))
88 self.assertFalse(check(NULL))
89
90 def test_unary_ops(self):
91 methmap = {'__neg__': _testcapi.number_negative, # PyNumber_Negative()
92 '__pos__': _testcapi.number_positive, # PyNumber_Positive()
93 '__abs__': _testcapi.number_absolute, # PyNumber_Absolute()
94 '__invert__': _testcapi.number_invert} # PyNumber_Invert()
95
96 for name, func in methmap.items():
97 # Generic object, has no tp_as_number structure
98 self.assertRaises(TypeError, func, object())
99
100 # C-API function accepts NULL
101 self.assertRaises(SystemError, func, NULL)
102
103 # Behave as corresponding unary operation
104 op = getattr(operator, name)
105 for x in [0, 42, -1, 3.14, 1+2j]:
106 try:
107 op(x)
108 except TypeError:
109 self.assertRaises(TypeError, func, x)
110 else:
111 self.assertEqual(func(x), op(x))
112
113 def test_binary_ops(self):
114 methmap = {'__add__': _testcapi.number_add, # PyNumber_Add()
115 '__sub__': _testcapi.number_subtract, # PyNumber_Subtract()
116 '__mul__': _testcapi.number_multiply, # PyNumber_Multiply()
117 '__matmul__': _testcapi.number_matrixmultiply, # PyNumber_MatrixMultiply()
118 '__floordiv__': _testcapi.number_floordivide, # PyNumber_FloorDivide()
119 '__truediv__': _testcapi.number_truedivide, # PyNumber_TrueDivide()
120 '__mod__': _testcapi.number_remainder, # PyNumber_Remainder()
121 '__divmod__': _testcapi.number_divmod, # PyNumber_Divmod()
122 '__lshift__': _testcapi.number_lshift, # PyNumber_Lshift()
123 '__rshift__': _testcapi.number_rshift, # PyNumber_Rshift()
124 '__and__': _testcapi.number_and, # PyNumber_And()
125 '__xor__': _testcapi.number_xor, # PyNumber_Xor()
126 '__or__': _testcapi.number_or, # PyNumber_Or()
127 '__pow__': _testcapi.number_power, # PyNumber_Power()
128 '__iadd__': _testcapi.number_inplaceadd, # PyNumber_InPlaceAdd()
129 '__isub__': _testcapi.number_inplacesubtract, # PyNumber_InPlaceSubtract()
130 '__imul__': _testcapi.number_inplacemultiply, # PyNumber_InPlaceMultiply()
131 '__imatmul__': _testcapi.number_inplacematrixmultiply, # PyNumber_InPlaceMatrixMultiply()
132 '__ifloordiv__': _testcapi.number_inplacefloordivide, # PyNumber_InPlaceFloorDivide()
133 '__itruediv__': _testcapi.number_inplacetruedivide, # PyNumber_InPlaceTrueDivide()
134 '__imod__': _testcapi.number_inplaceremainder, # PyNumber_InPlaceRemainder()
135 '__ilshift__': _testcapi.number_inplacelshift, # PyNumber_InPlaceLshift()
136 '__irshift__': _testcapi.number_inplacershift, # PyNumber_InPlaceRshift()
137 '__iand__': _testcapi.number_inplaceand, # PyNumber_InPlaceAnd()
138 '__ixor__': _testcapi.number_inplacexor, # PyNumber_InPlaceXor()
139 '__ior__': _testcapi.number_inplaceor, # PyNumber_InPlaceOr()
140 '__ipow__': _testcapi.number_inplacepower, # PyNumber_InPlacePower()
141 }
142
143 for name, func in methmap.items():
144 cases = [0, 42, 3.14, -1, 123, 1+2j]
145
146 # Generic object, has no tp_as_number structure
147 for x in cases:
148 self.assertRaises(TypeError, func, object(), x)
149 self.assertRaises(TypeError, func, x, object())
150
151 # Behave as corresponding binary operation
152 op = getattr(operator, name, divmod)
153 for x, y in itertools.combinations(cases, 2):
154 try:
155 op(x, y)
156 except (TypeError, ValueError, ZeroDivisionError) as exc:
157 self.assertRaises(exc.__class__, func, x, y)
158 else:
159 self.assertEqual(func(x, y), op(x, y))
160
161 # CRASHES func(NULL, object())
162 # CRASHES func(object(), NULL)
163
164 @unittest.skipIf(ndarray is None, "needs _testbuffer")
165 def test_misc_add(self):
166 # PyNumber_Add(), PyNumber_InPlaceAdd()
167 add = _testcapi.number_add
168 inplaceadd = _testcapi.number_inplaceadd
169
170 # test sq_concat/sq_inplace_concat slots
171 a, b, r = [1, 2], [3, 4], [1, 2, 3, 4]
172 self.assertEqual(add(a, b), r)
173 self.assertEqual(a, [1, 2])
174 self.assertRaises(TypeError, add, ndarray([1], (1,)), 2)
175 a, b, r = [1, 2], [3, 4], [1, 2, 3, 4]
176 self.assertEqual(inplaceadd(a, b), r)
177 self.assertEqual(a, r)
178 self.assertRaises(TypeError, inplaceadd, ndarray([1], (1,)), 2)
179
180 @unittest.skipIf(ndarray is None, "needs _testbuffer")
181 def test_misc_multiply(self):
182 # PyNumber_Multiply(), PyNumber_InPlaceMultiply()
183 multiply = _testcapi.number_multiply
184 inplacemultiply = _testcapi.number_inplacemultiply
185
186 # test sq_repeat/sq_inplace_repeat slots
187 a, b, r = [1], 2, [1, 1]
188 self.assertEqual(multiply(a, b), r)
189 self.assertEqual((a, b), ([1], 2))
190 self.assertEqual(multiply(b, a), r)
191 self.assertEqual((a, b), ([1], 2))
192 self.assertEqual(multiply([1], -1), [])
193 self.assertRaises(TypeError, multiply, ndarray([1], (1,)), 2)
194 self.assertRaises(TypeError, multiply, [1], 0.5)
195 self.assertRaises(OverflowError, multiply, [1], PY_SSIZE_T_MAX + 1)
196 self.assertRaises(MemoryError, multiply, [1, 2], PY_SSIZE_T_MAX//2 + 1)
197 a, b, r = [1], 2, [1, 1]
198 self.assertEqual(inplacemultiply(a, b), r)
199 self.assertEqual((a, b), (r, 2))
200 a = [1]
201 self.assertEqual(inplacemultiply(b, a), r)
202 self.assertEqual((a, b), ([1], 2))
203 self.assertRaises(TypeError, inplacemultiply, ndarray([1], (1,)), 2)
204 self.assertRaises(OverflowError, inplacemultiply, [1], PY_SSIZE_T_MAX + 1)
205 self.assertRaises(MemoryError, inplacemultiply, [1, 2], PY_SSIZE_T_MAX//2 + 1)
206
207 def test_misc_power(self):
208 # PyNumber_Power(), PyNumber_InPlacePower()
209 power = _testcapi.number_power
210 inplacepower = _testcapi.number_inplacepower
211
212 class HasPow(WithDunder):
213 methname = '__pow__'
214
215 # ternary op
216 self.assertEqual(power(4, 11, 5), pow(4, 11, 5))
217 self.assertRaises(TypeError, power, 4, 11, 1.25)
218 self.assertRaises(TypeError, power, 4, 11, HasPow.with_val(NotImplemented))
219 self.assertRaises(TypeError, power, 4, 11, object())
220 self.assertEqual(inplacepower(4, 11, 5), pow(4, 11, 5))
221 self.assertRaises(TypeError, inplacepower, 4, 11, 1.25)
222 self.assertRaises(TypeError, inplacepower, 4, 11, object())
223
224 class X:
225 def __pow__(*args):
226 return args
227
228 x = X()
229 self.assertEqual(power(x, 11), (x, 11))
230 self.assertEqual(inplacepower(x, 11), (x, 11))
231 self.assertEqual(power(x, 11, 5), (x, 11, 5))
232 self.assertEqual(inplacepower(x, 11, 5), (x, 11, 5))
233
234 class X:
235 def __rpow__(*args):
236 return args
237
238 x = X()
239 self.assertEqual(power(4, x), (x, 4))
240 self.assertEqual(inplacepower(4, x), (x, 4))
241 # XXX: Three-arg power doesn't use __rpow__.
242 self.assertRaises(TypeError, power, 4, x, 5)
243 self.assertRaises(TypeError, inplacepower, 4, x, 5)
244
245 class X:
246 def __ipow__(*args):
247 return args
248
249 x = X()
250 self.assertEqual(inplacepower(x, 11), (x, 11))
251 # XXX: In-place power doesn't pass the third arg to __ipow__.
252 self.assertEqual(inplacepower(x, 11, 5), (x, 11))
253
254 @cpython_only
255 def test_rshift_print(self):
256 # This tests correct syntax hint for py2 redirection (>>).
257 rshift = _testcapi.number_rshift
258
259 with self.assertRaises(TypeError) as context:
260 rshift(print, 42)
261 self.assertIn('Did you mean "print(<message>, '
262 'file=<output_stream>)"?', str(context.exception))
263 with self.assertRaises(TypeError) as context:
264 rshift(max, sys.stderr)
265 self.assertNotIn('Did you mean ', str(context.exception))
266 with self.assertRaises(TypeError) as context:
267 rshift(1, "spam")
268
269 def test_long(self):
270 # Test PyNumber_Long()
271 long = _testcapi.number_long
272
273 self.assertEqual(long(42), 42)
274 self.assertEqual(long(1.25), 1)
275 self.assertEqual(long("42"), 42)
276 self.assertEqual(long(b"42"), 42)
277 self.assertEqual(long(bytearray(b"42")), 42)
278 self.assertEqual(long(memoryview(b"42")), 42)
279 self.assertEqual(long(IndexLike.with_val(99)), 99)
280 self.assertEqual(long(IntLike.with_val(99)), 99)
281
282 self.assertRaises(TypeError, long, IntLike.with_val(1.0))
283 with warnings.catch_warnings():
284 warnings.simplefilter("error", DeprecationWarning)
285 self.assertRaises(DeprecationWarning, long, IntLike.with_val(True))
286 with self.assertWarns(DeprecationWarning):
287 self.assertEqual(long(IntLike.with_val(True)), 1)
288 self.assertRaises(RuntimeError, long, IntLike.with_exc(RuntimeError))
289
290 self.assertRaises(TypeError, long, 1j)
291 self.assertRaises(TypeError, long, object())
292 self.assertRaises(SystemError, long, NULL)
293
294 def test_float(self):
295 # Test PyNumber_Float()
296 float_ = _testcapi.number_float
297
298 self.assertEqual(float_(1.25), 1.25)
299 self.assertEqual(float_(123), 123.)
300 self.assertEqual(float_("1.25"), 1.25)
301
302 self.assertEqual(float_(FloatLike.with_val(4.25)), 4.25)
303 self.assertEqual(float_(IndexLike.with_val(99)), 99.0)
304 self.assertEqual(float_(IndexLike.with_val(-1)), -1.0)
305
306 self.assertRaises(TypeError, float_, FloatLike.with_val(687))
307 with warnings.catch_warnings():
308 warnings.simplefilter("error", DeprecationWarning)
309 self.assertRaises(DeprecationWarning, float_, FloatLike.with_val(subclassof(float)(4.25)))
310 with self.assertWarns(DeprecationWarning):
311 self.assertEqual(float_(FloatLike.with_val(subclassof(float)(4.25))), 4.25)
312 self.assertRaises(RuntimeError, float_, FloatLike.with_exc(RuntimeError))
313
314 self.assertRaises(TypeError, float_, IndexLike.with_val(1.25))
315 self.assertRaises(OverflowError, float_, IndexLike.with_val(2**2000))
316
317 self.assertRaises(TypeError, float_, 1j)
318 self.assertRaises(TypeError, float_, object())
319 self.assertRaises(SystemError, float_, NULL)
320
321 def test_index(self):
322 # Test PyNumber_Index()
323 index = _testcapi.number_index
324
325 self.assertEqual(index(11), 11)
326
327 with warnings.catch_warnings():
328 warnings.simplefilter("error", DeprecationWarning)
329 self.assertRaises(DeprecationWarning, index, IndexLike.with_val(True))
330 with self.assertWarns(DeprecationWarning):
331 self.assertEqual(index(IndexLike.with_val(True)), 1)
332 self.assertRaises(TypeError, index, IndexLike.with_val(1.0))
333 self.assertRaises(RuntimeError, index, IndexLike.with_exc(RuntimeError))
334
335 self.assertRaises(TypeError, index, 1.25)
336 self.assertRaises(TypeError, index, "42")
337 self.assertRaises(TypeError, index, object())
338 self.assertRaises(SystemError, index, NULL)
339
340 def test_tobase(self):
341 # Test PyNumber_ToBase()
342 tobase = _testcapi.number_tobase
343
344 self.assertEqual(tobase(10, 2), bin(10))
345 self.assertEqual(tobase(11, 8), oct(11))
346 self.assertEqual(tobase(16, 10), str(16))
347 self.assertEqual(tobase(13, 16), hex(13))
348
349 self.assertRaises(SystemError, tobase, NULL, 2)
350 self.assertRaises(SystemError, tobase, 2, 3)
351 self.assertRaises(TypeError, tobase, 1.25, 2)
352 self.assertRaises(TypeError, tobase, "42", 2)
353
354 def test_asssizet(self):
355 # Test PyNumber_AsSsize_t()
356 asssizet = _testcapi.number_asssizet
357
358 for n in [*range(-6, 7), PY_SSIZE_T_MIN, PY_SSIZE_T_MAX]:
359 self.assertEqual(asssizet(n, OverflowError), n)
360 self.assertEqual(asssizet(PY_SSIZE_T_MAX+10, NULL), PY_SSIZE_T_MAX)
361 self.assertEqual(asssizet(PY_SSIZE_T_MIN-10, NULL), PY_SSIZE_T_MIN)
362
363 self.assertRaises(OverflowError, asssizet, PY_SSIZE_T_MAX + 10, OverflowError)
364 self.assertRaises(RuntimeError, asssizet, PY_SSIZE_T_MAX + 10, RuntimeError)
365 self.assertRaises(SystemError, asssizet, NULL, TypeError)
366
367
368 if __name__ == "__main__":
369 unittest.main()