- Notifications
You must be signed in to change notification settings - Fork 31.7k
/
Copy pathtest_number.py
352 lines (287 loc) · 13.7 KB
/
test_number.py
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
importitertools
importoperator
importunittest
importwarnings
fromtest.supportimportimport_helper
_testcapi=import_helper.import_module('_testcapi')
from_testcapiimportPY_SSIZE_T_MAX, PY_SSIZE_T_MIN
try:
from_testbufferimportndarray
exceptImportError:
ndarray=None
NULL=None
classBadDescr:
def__get__(self, obj, objtype=None):
raiseRuntimeError
classWithDunder:
def_meth(self, *args):
ifself.val:
returnself.val
ifself.exc:
raiseself.exc
@classmethod
defwith_val(cls, val):
obj=super().__new__(cls)
obj.val=val
obj.exc=None
setattr(cls, cls.methname, cls._meth)
returnobj
@classmethod
defwith_exc(cls, exc):
obj=super().__new__(cls)
obj.val=None
obj.exc=exc
setattr(cls, cls.methname, cls._meth)
returnobj
classHasBadAttr:
def__new__(cls):
obj=super().__new__(cls)
setattr(cls, cls.methname, BadDescr())
returnobj
classIndexLike(WithDunder):
methname='__index__'
classIntLike(WithDunder):
methname='__int__'
classFloatLike(WithDunder):
methname='__float__'
defsubclassof(base):
returntype(base.__name__+'Subclass', (base,), {})
classSomeError(Exception):
pass
classOtherError(Exception):
pass
classCAPITest(unittest.TestCase):
deftest_check(self):
# Test PyNumber_Check()
check=_testcapi.number_check
self.assertTrue(check(1))
self.assertTrue(check(IndexLike.with_val(1)))
self.assertTrue(check(IntLike.with_val(99)))
self.assertTrue(check(0.5))
self.assertTrue(check(FloatLike.with_val(4.25)))
self.assertTrue(check(1+2j))
self.assertFalse(check([]))
self.assertFalse(check("abc"))
self.assertFalse(check(object()))
self.assertFalse(check(NULL))
deftest_unary_ops(self):
methmap= {'__neg__': _testcapi.number_negative, # PyNumber_Negative()
'__pos__': _testcapi.number_positive, # PyNumber_Positive()
'__abs__': _testcapi.number_absolute, # PyNumber_Absolute()
'__invert__': _testcapi.number_invert} # PyNumber_Invert()
forname, funcinmethmap.items():
# Generic object, has no tp_as_number structure
self.assertRaises(TypeError, func, object())
# C-API function accepts NULL
self.assertRaises(SystemError, func, NULL)
# Behave as corresponding unary operation
op=getattr(operator, name)
forxin [0, 42, -1, 3.14, 1+2j]:
try:
op(x)
exceptTypeError:
self.assertRaises(TypeError, func, x)
else:
self.assertEqual(func(x), op(x))
deftest_binary_ops(self):
methmap= {'__add__': _testcapi.number_add, # PyNumber_Add()
'__sub__': _testcapi.number_subtract, # PyNumber_Subtract()
'__mul__': _testcapi.number_multiply, # PyNumber_Multiply()
'__matmul__': _testcapi.number_matrixmultiply, # PyNumber_MatrixMultiply()
'__floordiv__': _testcapi.number_floordivide, # PyNumber_FloorDivide()
'__truediv__': _testcapi.number_truedivide, # PyNumber_TrueDivide()
'__mod__': _testcapi.number_remainder, # PyNumber_Remainder()
'__divmod__': _testcapi.number_divmod, # PyNumber_Divmod()
'__lshift__': _testcapi.number_lshift, # PyNumber_Lshift()
'__rshift__': _testcapi.number_rshift, # PyNumber_Rshift()
'__and__': _testcapi.number_and, # PyNumber_And()
'__xor__': _testcapi.number_xor, # PyNumber_Xor()
'__or__': _testcapi.number_or, # PyNumber_Or()
'__pow__': _testcapi.number_power, # PyNumber_Power()
'__iadd__': _testcapi.number_inplaceadd, # PyNumber_InPlaceAdd()
'__isub__': _testcapi.number_inplacesubtract, # PyNumber_InPlaceSubtract()
'__imul__': _testcapi.number_inplacemultiply, # PyNumber_InPlaceMultiply()
'__imatmul__': _testcapi.number_inplacematrixmultiply, # PyNumber_InPlaceMatrixMultiply()
'__ifloordiv__': _testcapi.number_inplacefloordivide, # PyNumber_InPlaceFloorDivide()
'__itruediv__': _testcapi.number_inplacetruedivide, # PyNumber_InPlaceTrueDivide()
'__imod__': _testcapi.number_inplaceremainder, # PyNumber_InPlaceRemainder()
'__ilshift__': _testcapi.number_inplacelshift, # PyNumber_InPlaceLshift()
'__irshift__': _testcapi.number_inplacershift, # PyNumber_InPlaceRshift()
'__iand__': _testcapi.number_inplaceand, # PyNumber_InPlaceAnd()
'__ixor__': _testcapi.number_inplacexor, # PyNumber_InPlaceXor()
'__ior__': _testcapi.number_inplaceor, # PyNumber_InPlaceOr()
'__ipow__': _testcapi.number_inplacepower, # PyNumber_InPlacePower()
}
forname, funcinmethmap.items():
cases= [0, 42, 3.14, -1, 123, 1+2j]
# Generic object, has no tp_as_number structure
forxincases:
self.assertRaises(TypeError, func, object(), x)
self.assertRaises(TypeError, func, x, object())
# Behave as corresponding binary operation
op=getattr(operator, name, divmod)
forx, yinitertools.combinations(cases, 2):
try:
op(x, y)
except (TypeError, ValueError, ZeroDivisionError) asexc:
self.assertRaises(exc.__class__, func, x, y)
else:
self.assertEqual(func(x, y), op(x, y))
# CRASHES func(NULL, object())
# CRASHES func(object(), NULL)
@unittest.skipIf(ndarrayisNone, "needs _testbuffer")
deftest_misc_add(self):
# PyNumber_Add(), PyNumber_InPlaceAdd()
add=_testcapi.number_add
inplaceadd=_testcapi.number_inplaceadd
# test sq_concat/sq_inplace_concat slots
a, b, r= [1, 2], [3, 4], [1, 2, 3, 4]
self.assertEqual(add(a, b), r)
self.assertEqual(a, [1, 2])
self.assertRaises(TypeError, add, ndarray([1], (1,)), 2)
a, b, r= [1, 2], [3, 4], [1, 2, 3, 4]
self.assertEqual(inplaceadd(a, b), r)
self.assertEqual(a, r)
self.assertRaises(TypeError, inplaceadd, ndarray([1], (1,)), 2)
@unittest.skipIf(ndarrayisNone, "needs _testbuffer")
deftest_misc_multiply(self):
# PyNumber_Multiply(), PyNumber_InPlaceMultiply()
multiply=_testcapi.number_multiply
inplacemultiply=_testcapi.number_inplacemultiply
# test sq_repeat/sq_inplace_repeat slots
a, b, r= [1], 2, [1, 1]
self.assertEqual(multiply(a, b), r)
self.assertEqual((a, b), ([1], 2))
self.assertEqual(multiply(b, a), r)
self.assertEqual((a, b), ([1], 2))
self.assertEqual(multiply([1], -1), [])
self.assertRaises(TypeError, multiply, ndarray([1], (1,)), 2)
self.assertRaises(TypeError, multiply, [1], 0.5)
self.assertRaises(OverflowError, multiply, [1], PY_SSIZE_T_MAX+1)
self.assertRaises(MemoryError, multiply, [1, 2], PY_SSIZE_T_MAX//2+1)
a, b, r= [1], 2, [1, 1]
self.assertEqual(inplacemultiply(a, b), r)
self.assertEqual((a, b), (r, 2))
a= [1]
self.assertEqual(inplacemultiply(b, a), r)
self.assertEqual((a, b), ([1], 2))
self.assertRaises(TypeError, inplacemultiply, ndarray([1], (1,)), 2)
self.assertRaises(OverflowError, inplacemultiply, [1], PY_SSIZE_T_MAX+1)
self.assertRaises(MemoryError, inplacemultiply, [1, 2], PY_SSIZE_T_MAX//2+1)
deftest_misc_power(self):
# PyNumber_Power(), PyNumber_InPlacePower()
power=_testcapi.number_power
inplacepower=_testcapi.number_inplacepower
classHasPow(WithDunder):
methname='__pow__'
# ternary op
self.assertEqual(power(4, 11, 5), pow(4, 11, 5))
self.assertRaises(TypeError, power, 4, 11, 1.25)
self.assertRaises(TypeError, power, 4, 11, HasPow.with_val(NotImplemented))
self.assertRaises(TypeError, power, 4, 11, object())
self.assertEqual(inplacepower(4, 11, 5), pow(4, 11, 5))
self.assertRaises(TypeError, inplacepower, 4, 11, 1.25)
self.assertRaises(TypeError, inplacepower, 4, 11, object())
classX:
def__pow__(*args):
returnargs
x=X()
self.assertEqual(power(x, 11), (x, 11))
self.assertEqual(inplacepower(x, 11), (x, 11))
self.assertEqual(power(x, 11, 5), (x, 11, 5))
self.assertEqual(inplacepower(x, 11, 5), (x, 11, 5))
classX:
def__rpow__(*args):
returnargs
x=X()
self.assertEqual(power(4, x), (x, 4))
self.assertEqual(inplacepower(4, x), (x, 4))
self.assertEqual(power(4, x, 5), (x, 4, 5))
self.assertEqual(inplacepower(4, x, 5), (x, 4, 5))
classX:
def__ipow__(*args):
returnargs
x=X()
self.assertEqual(inplacepower(x, 11), (x, 11))
# XXX: In-place power doesn't pass the third arg to __ipow__.
self.assertEqual(inplacepower(x, 11, 5), (x, 11))
deftest_long(self):
# Test PyNumber_Long()
long=_testcapi.number_long
self.assertEqual(long(42), 42)
self.assertEqual(long(1.25), 1)
self.assertEqual(long("42"), 42)
self.assertEqual(long(b"42"), 42)
self.assertEqual(long(bytearray(b"42")), 42)
self.assertEqual(long(memoryview(b"42")), 42)
self.assertEqual(long(IndexLike.with_val(99)), 99)
self.assertEqual(long(IntLike.with_val(99)), 99)
self.assertRaises(TypeError, long, IntLike.with_val(1.0))
withwarnings.catch_warnings():
warnings.simplefilter("error", DeprecationWarning)
self.assertRaises(DeprecationWarning, long, IntLike.with_val(True))
withself.assertWarns(DeprecationWarning):
self.assertEqual(long(IntLike.with_val(True)), 1)
self.assertRaises(RuntimeError, long, IntLike.with_exc(RuntimeError))
self.assertRaises(TypeError, long, 1j)
self.assertRaises(TypeError, long, object())
self.assertRaises(SystemError, long, NULL)
deftest_float(self):
# Test PyNumber_Float()
float_=_testcapi.number_float
self.assertEqual(float_(1.25), 1.25)
self.assertEqual(float_(123), 123.)
self.assertEqual(float_("1.25"), 1.25)
self.assertEqual(float_(FloatLike.with_val(4.25)), 4.25)
self.assertEqual(float_(IndexLike.with_val(99)), 99.0)
self.assertEqual(float_(IndexLike.with_val(-1)), -1.0)
self.assertRaises(TypeError, float_, FloatLike.with_val(687))
withwarnings.catch_warnings():
warnings.simplefilter("error", DeprecationWarning)
self.assertRaises(DeprecationWarning, float_, FloatLike.with_val(subclassof(float)(4.25)))
withself.assertWarns(DeprecationWarning):
self.assertEqual(float_(FloatLike.with_val(subclassof(float)(4.25))), 4.25)
self.assertRaises(RuntimeError, float_, FloatLike.with_exc(RuntimeError))
self.assertRaises(TypeError, float_, IndexLike.with_val(1.25))
self.assertRaises(OverflowError, float_, IndexLike.with_val(2**2000))
self.assertRaises(TypeError, float_, 1j)
self.assertRaises(TypeError, float_, object())
self.assertRaises(SystemError, float_, NULL)
deftest_index(self):
# Test PyNumber_Index()
index=_testcapi.number_index
self.assertEqual(index(11), 11)
withwarnings.catch_warnings():
warnings.simplefilter("error", DeprecationWarning)
self.assertRaises(DeprecationWarning, index, IndexLike.with_val(True))
withself.assertWarns(DeprecationWarning):
self.assertEqual(index(IndexLike.with_val(True)), 1)
self.assertRaises(TypeError, index, IndexLike.with_val(1.0))
self.assertRaises(RuntimeError, index, IndexLike.with_exc(RuntimeError))
self.assertRaises(TypeError, index, 1.25)
self.assertRaises(TypeError, index, "42")
self.assertRaises(TypeError, index, object())
self.assertRaises(SystemError, index, NULL)
deftest_tobase(self):
# Test PyNumber_ToBase()
tobase=_testcapi.number_tobase
self.assertEqual(tobase(10, 2), bin(10))
self.assertEqual(tobase(11, 8), oct(11))
self.assertEqual(tobase(16, 10), str(16))
self.assertEqual(tobase(13, 16), hex(13))
self.assertRaises(SystemError, tobase, NULL, 2)
self.assertRaises(SystemError, tobase, 2, 3)
self.assertRaises(TypeError, tobase, 1.25, 2)
self.assertRaises(TypeError, tobase, "42", 2)
deftest_asssizet(self):
# Test PyNumber_AsSsize_t()
asssizet=_testcapi.number_asssizet
fornin [*range(-6, 7), PY_SSIZE_T_MIN, PY_SSIZE_T_MAX]:
self.assertEqual(asssizet(n, OverflowError), n)
self.assertEqual(asssizet(PY_SSIZE_T_MAX+10, NULL), PY_SSIZE_T_MAX)
self.assertEqual(asssizet(PY_SSIZE_T_MIN-10, NULL), PY_SSIZE_T_MIN)
self.assertRaises(OverflowError, asssizet, PY_SSIZE_T_MAX+10, OverflowError)
self.assertRaises(RuntimeError, asssizet, PY_SSIZE_T_MAX+10, RuntimeError)
self.assertRaises(SystemError, asssizet, NULL, TypeError)
if__name__=="__main__":
unittest.main()