Debian Codegraph @

pypy/module/_hpy_universal/test/_vendored/test_hpylong.py

raw symbols SCIP
1 from .support import HPyTest, DefaultExtensionTemplate
2
3 class HPyLongTemplate(DefaultExtensionTemplate):
4 def DEFINE_Long_From(self, type, api_suffix, val):
5 self.EXPORT("from_{type}_{val}".format(type=type, val=val))
6 return """
7 HPyDef_METH(from_{type}_{val}, "from_{type}_{val}", HPyFunc_NOARGS)
8 static HPy from_{type}_{val}_impl(HPyContext *ctx, HPy self)
9 {{
10 {type} a = {val};
11 return HPyLong_From{api_suffix}(ctx, a);
12 }}
13 """.format(type=type, val=val, api_suffix=api_suffix)
14
15 def DEFINE_Long_As(self, type, api_suffix, from_suffix=None):
16 self.EXPORT("as_{api_suffix}".format(api_suffix=api_suffix))
17 if not from_suffix:
18 from_suffix = api_suffix
19 return """
20 HPyDef_METH(as_{api_suffix}, "as_{api_suffix}", HPyFunc_O)
21 static HPy as_{api_suffix}_impl(HPyContext *ctx, HPy self, HPy arg)
22 {{
23 {type} a = HPyLong_As{api_suffix}(ctx, arg);
24 if (a == (({type})-1) && HPyErr_Occurred(ctx))
25 return HPy_NULL;
26 return HPyLong_From{from_suffix}(ctx, a * 2);
27 }}
28 """.format(type=type, api_suffix=api_suffix, from_suffix=from_suffix)
29
30 class TestLong(HPyTest):
31
32 ExtensionTemplate = HPyLongTemplate
33
34 def unsigned_long_bits(self):
35 """ Return the number of bits in an unsigned long. """
36 import struct
37 unsigned_long_bytes = len(struct.pack('l', 0))
38 return 8 * unsigned_long_bytes
39
40 def magic_int(self, v):
41 """ Return an instance of a class that implements __int__
42 and returns value v.
43 """
44 class MagicInt(object):
45 def __int__(self):
46 return v
47 return MagicInt()
48
49 def magic_index(self, v):
50 """ Return an instance of a class that implements __index__
51 and returns value v.
52 """
53 class MagicIndex(object):
54 def __index__(self):
55 return v
56 return MagicIndex()
57
58 def python_supports_magic_index(self):
59 """ Return True if the Python version is 3.8 or later and thus
60 should support calling __index__ in the various HPyLong_As...
61 methods.
62 """
63 import sys
64 vi = sys.version_info
65 return (vi.major > 3 or (vi.major == 3 and vi.minor >= 8))
66
67 def python_supports_magic_int(self):
68 """ Return True if the Python version is 3.9 or earlier and thus
69 should support calling __int__ on non-int based types in some
70 HPyLong_As... methods.
71 """
72 import sys
73 vi = sys.version_info
74 assert vi.major >= 3
75 return (vi.major == 3 and vi.minor <= 9)
76
77 def test_Long_FromFixedWidth(self):
78 mod = self.make_module("""
79 @DEFINE_Long_From(int32_t, Int32_t, INT32_MAX)
80 @DEFINE_Long_From(int32_t, Int32_t, INT32_MIN)
81 @DEFINE_Long_From(uint32_t, UInt32_t, UINT32_MAX)
82 @DEFINE_Long_From(int64_t, Int64_t, INT64_MAX)
83 @DEFINE_Long_From(int64_t, Int64_t, INT64_MIN)
84 @DEFINE_Long_From(uint64_t, UInt64_t, UINT64_MAX)
85 @INIT
86 """)
87 assert mod.from_int32_t_INT32_MAX() == 2147483647
88 assert mod.from_int32_t_INT32_MIN() == -2147483648
89 assert mod.from_uint32_t_UINT32_MAX() == 4294967295
90 assert mod.from_int64_t_INT64_MAX() == 9223372036854775807
91 assert mod.from_int64_t_INT64_MIN() == -9223372036854775808
92 assert mod.from_uint64_t_UINT64_MAX() == 18446744073709551615
93
94 def test_Long_FromLong(self):
95 mod = self.make_module("""
96 HPyDef_METH(f, "f", HPyFunc_NOARGS)
97 static HPy f_impl(HPyContext *ctx, HPy self)
98 {
99 long a = 500;
100 return HPyLong_FromLong(ctx, a);
101 }
102 @EXPORT(f)
103 @INIT
104 """)
105 assert mod.f() == 500
106
107 def test_Long_AsFixedWidth(self):
108 import pytest
109 mod = self.make_module("""
110 @DEFINE_Long_As(int32_t, Int32_t)
111 @DEFINE_Long_As(uint32_t, UInt32_t)
112 @DEFINE_Long_As(uint32_t, UInt32_tMask, UInt32_t)
113 @DEFINE_Long_As(int64_t, Int64_t)
114 @DEFINE_Long_As(uint64_t, UInt64_t)
115 @DEFINE_Long_As(uint64_t, UInt64_tMask, UInt64_t)
116 @INIT
117 """)
118
119 assert mod.as_Int32_t(45) == 90
120 assert mod.as_Int32_t(-45) == -90
121 # doubling INT32_MAX is like a left-shift and will result in a negative value
122 assert mod.as_Int32_t(2147483647) < 0
123 with pytest.raises(TypeError):
124 mod.as_Int32_t("this is not a number")
125 if self.python_supports_magic_int():
126 assert mod.as_Int32_t(self.magic_int(2)) == 4
127 if self.python_supports_magic_index():
128 assert mod.as_Int32_t(self.magic_index(2)) == 4
129
130 assert mod.as_UInt32_t(45) == 90
131 with pytest.raises(OverflowError):
132 mod.as_UInt32_t(-45)
133 assert mod.as_UInt32_t(2147483647) == 4294967294
134
135 assert mod.as_UInt32_tMask(0xffffffffffffffff) == 0xfffffffe
136
137 assert mod.as_Int64_t(45) == 90
138 assert mod.as_Int64_t(-45) == -90
139 # doubling INT32_MAX is like a left-shift and will result in a negative value
140 assert mod.as_Int64_t(2147483647) == 4294967294
141 assert mod.as_Int64_t(9223372036854775807) < 0
142 with pytest.raises(TypeError):
143 mod.as_Int64_t("this is not a number")
144
145 assert mod.as_UInt64_t(45) == 90
146 with pytest.raises(OverflowError):
147 mod.as_UInt64_t(-45)
148 assert mod.as_UInt64_t(9223372036854775807) == 18446744073709551614
149
150 assert mod.as_UInt64_tMask(0xffffffffffffffffff) == 0xfffffffffffffffe
151
152 def test_Long_AsLong(self):
153 import pytest
154 mod = self.make_module("""
155 HPyDef_METH(f, "f", HPyFunc_O)
156 static HPy f_impl(HPyContext *ctx, HPy self, HPy arg)
157 {
158 long a = HPyLong_AsLong(ctx, arg);
159 if (a == -1 && HPyErr_Occurred(ctx))
160 return HPy_NULL;
161 return HPyLong_FromLong(ctx, a * 2);
162 }
163 @EXPORT(f)
164 @INIT
165 """)
166 assert mod.f(45) == 90
167 with pytest.raises(TypeError):
168 mod.f("this is not a number")
169 if self.python_supports_magic_int():
170 assert mod.f(self.magic_int(2)) == 4
171 if self.python_supports_magic_index():
172 assert mod.f(self.magic_index(2)) == 4
173
174 def test_Long_FromUnsignedLong(self):
175 mod = self.make_module("""
176 HPyDef_METH(f, "f", HPyFunc_NOARGS)
177 static HPy f_impl(HPyContext *ctx, HPy self)
178 {
179 unsigned long a = 500;
180 return HPyLong_FromUnsignedLong(ctx, a);
181 }
182 @EXPORT(f)
183 @INIT
184 """)
185 assert mod.f() == 500
186
187 def test_Long_AsUnsignedLong(self):
188 import pytest
189 mod = self.make_module("""
190 HPyDef_METH(f, "f", HPyFunc_O)
191 static HPy f_impl(HPyContext *ctx, HPy self, HPy arg)
192 {
193 unsigned long a = HPyLong_AsUnsignedLong(ctx, arg);
194 if ((a == (unsigned long) -1) && HPyErr_Occurred(ctx))
195 return HPy_NULL;
196 return HPyLong_FromUnsignedLong(ctx, a);
197 }
198 @EXPORT(f)
199 @INIT
200 """)
201 assert mod.f(45) == 45
202 with pytest.raises(OverflowError):
203 mod.f(-91)
204 with pytest.raises(TypeError):
205 mod.f("this is not a number")
206 with pytest.raises(TypeError):
207 mod.f(self.magic_int(2))
208 with pytest.raises(TypeError):
209 mod.f(self.magic_index(2))
210
211 def test_Long_AsUnsignedLongMask(self):
212 import pytest
213 mod = self.make_module("""
214 HPyDef_METH(f, "f", HPyFunc_O)
215 static HPy f_impl(HPyContext *ctx, HPy self, HPy arg)
216 {
217 unsigned long a = HPyLong_AsUnsignedLongMask(ctx, arg);
218 if ((a == (unsigned long) -1) && HPyErr_Occurred(ctx))
219 return HPy_NULL;
220 return HPyLong_FromUnsignedLong(ctx, a);
221 }
222 @EXPORT(f)
223 @INIT
224 """)
225 assert mod.f(45) == 45
226 assert mod.f(-1) == 2**self.unsigned_long_bits() - 1
227 with pytest.raises(TypeError):
228 mod.f("this is not a number")
229 if self.python_supports_magic_int():
230 assert mod.f(self.magic_int(2)) == 2
231 if self.python_supports_magic_index():
232 assert mod.f(self.magic_index(2)) == 2
233
234 def test_Long_FromLongLong(self):
235 mod = self.make_module("""
236 HPyDef_METH(f, "f", HPyFunc_NOARGS)
237 static HPy f_impl(HPyContext *ctx, HPy self)
238 {
239 // take a value which doesn't fit in 32 bit
240 long long val = 2147483648;
241 return HPyLong_FromLongLong(ctx, val);
242 }
243 @EXPORT(f)
244 @INIT
245 """)
246 assert mod.f() == 2147483648
247
248 def test_Long_AsLongLong(self):
249 import pytest
250 mod = self.make_module("""
251 HPyDef_METH(f, "f", HPyFunc_O)
252 static HPy f_impl(HPyContext *ctx, HPy self, HPy arg)
253 {
254 long long a = HPyLong_AsLongLong(ctx, arg);
255 if ((a == (long long) -1) && HPyErr_Occurred(ctx))
256 return HPy_NULL;
257 return HPyLong_FromLongLong(ctx, a);
258 }
259 @EXPORT(f)
260 @INIT
261 """)
262 assert mod.f(2147483648) == 2147483648
263 assert mod.f(-2147483648) == -2147483648
264 with pytest.raises(TypeError):
265 mod.f("this is not a number")
266 if self.python_supports_magic_int():
267 assert mod.f(self.magic_int(2)) == 2
268 if self.python_supports_magic_index():
269 assert mod.f(self.magic_index(2)) == 2
270
271 def test_Long_FromUnsignedLongLong(self):
272 mod = self.make_module("""
273 HPyDef_METH(f, "f", HPyFunc_NOARGS)
274 static HPy f_impl(HPyContext *ctx, HPy self)
275 {
276 // take a value which doesn't fit in unsigned 32 bit
277 unsigned long long val = 4294967296;
278 return HPyLong_FromUnsignedLongLong(ctx, val);
279 }
280 @EXPORT(f)
281 @INIT
282 """)
283 assert mod.f() == 4294967296
284
285 def test_Long_AsUnsignedLongLong(self):
286 import pytest
287 mod = self.make_module("""
288 HPyDef_METH(f, "f", HPyFunc_O)
289 static HPy f_impl(HPyContext *ctx, HPy self, HPy arg)
290 {
291 unsigned long long a = HPyLong_AsUnsignedLongLong(ctx, arg);
292 if ((a == (unsigned long long) -1) && HPyErr_Occurred(ctx))
293 return HPy_NULL;
294 return HPyLong_FromUnsignedLongLong(ctx, a);
295 }
296 @EXPORT(f)
297 @INIT
298 """)
299 assert mod.f(4294967296) == 4294967296
300 with pytest.raises(OverflowError):
301 mod.f(-4294967296)
302 with pytest.raises(TypeError):
303 mod.f("this is not a number")
304 with pytest.raises(TypeError):
305 mod.f(self.magic_int(2))
306 with pytest.raises(TypeError):
307 mod.f(self.magic_index(2))
308
309 def test_Long_AsUnsignedLongLongMask(self):
310 import pytest
311 mod = self.make_module("""
312 HPyDef_METH(f, "f", HPyFunc_O)
313 static HPy f_impl(HPyContext *ctx, HPy self, HPy arg)
314 {
315 unsigned long long a = HPyLong_AsUnsignedLongLongMask(ctx, arg);
316 if ((a == (unsigned long long) -1) && HPyErr_Occurred(ctx))
317 return HPy_NULL;
318 return HPyLong_FromUnsignedLongLong(ctx, a);
319 }
320 @EXPORT(f)
321 @INIT
322 """)
323 assert mod.f(45) == 45
324 assert mod.f(-1) == 2**64 - 1
325 with pytest.raises(TypeError):
326 mod.f("this is not a number")
327 if self.python_supports_magic_int():
328 assert mod.f(self.magic_int(2)) == 2
329 if self.python_supports_magic_index():
330 assert mod.f(self.magic_index(2)) == 2
331
332 def test_Long_FromSize_t(self):
333 mod = self.make_module("""
334 HPyDef_METH(f, "f", HPyFunc_NOARGS)
335 static HPy f_impl(HPyContext *ctx, HPy self)
336 {
337 // take a value which doesn't fit in 32 bit
338 size_t a = 2147483648;
339 return HPyLong_FromSize_t(ctx, a);
340 }
341 @EXPORT(f)
342 @INIT
343 """)
344 assert mod.f() == 2147483648
345
346 def test_Long_AsSize_t(self):
347 import pytest
348 mod = self.make_module("""
349 HPyDef_METH(f, "f", HPyFunc_O)
350 static HPy f_impl(HPyContext *ctx, HPy self, HPy arg)
351 {
352 size_t a = HPyLong_AsSize_t(ctx, arg);
353 if ((a == (size_t) -1) && HPyErr_Occurred(ctx))
354 return HPy_NULL;
355 return HPyLong_FromSize_t(ctx, a);
356 }
357 @EXPORT(f)
358 @INIT
359 """)
360 assert mod.f(2147483648) == 2147483648
361 with pytest.raises(OverflowError):
362 mod.f(-2147483648)
363 with pytest.raises(TypeError):
364 mod.f("this is not a number")
365 with pytest.raises(TypeError):
366 mod.f(self.magic_int(2))
367 with pytest.raises(TypeError):
368 mod.f(self.magic_index(2))
369
370 def test_Long_FromSsize_t(self):
371 mod = self.make_module("""
372 HPyDef_METH(f, "f", HPyFunc_NOARGS)
373 static HPy f_impl(HPyContext *ctx, HPy self)
374 {
375 HPy_ssize_t a = -42;
376 return HPyLong_FromSsize_t(ctx, a);
377 }
378 @EXPORT(f)
379 @INIT
380 """)
381 assert mod.f() == -42
382
383 def test_Long_AsSsize_t(self):
384 import pytest
385 mod = self.make_module("""
386 HPyDef_METH(f, "f", HPyFunc_O)
387 static HPy f_impl(HPyContext *ctx, HPy self, HPy arg)
388 {
389 HPy_ssize_t a = HPyLong_AsSsize_t(ctx, arg);
390 if ((a == (HPy_ssize_t) -1) && HPyErr_Occurred(ctx))
391 return HPy_NULL;
392 return HPyLong_FromSsize_t(ctx, a);
393 }
394 @EXPORT(f)
395 @INIT
396 """)
397 assert mod.f(41) == 41
398 assert mod.f(-41) == -41
399 with pytest.raises(TypeError):
400 mod.f("this is not a number")
401 with pytest.raises(TypeError):
402 mod.f(self.magic_int(2))
403 with pytest.raises(TypeError):
404 mod.f(self.magic_index(2))
405
406 def test_Long_AsVoidPtr(self):
407 mod = self.make_module("""
408 HPyDef_METH(f, "is_null", HPyFunc_O)
409 static HPy f_impl(HPyContext *ctx, HPy self, HPy val)
410 {
411 void* ptr = HPyLong_AsVoidPtr(ctx, val);
412 if (!ptr) {
413 return HPy_Dup(ctx, ctx->h_True);
414 } else {
415 return HPy_Dup(ctx, ctx->h_False);
416 }
417 }
418 @EXPORT(f)
419 @INIT
420 """)
421 assert mod.is_null(0) == True
422 assert mod.is_null(10) == False
423
424 def test_Long_AsDouble(self):
425 import pytest
426 mod = self.make_module("""
427 HPyDef_METH(f, "f", HPyFunc_O)
428 static HPy f_impl(HPyContext *ctx, HPy self, HPy arg)
429 {
430 double a = HPyLong_AsDouble(ctx, arg);
431 if (a == -1.0 && HPyErr_Occurred(ctx))
432 return HPy_NULL;
433 return HPyFloat_FromDouble(ctx, a);
434 }
435 @EXPORT(f)
436 @INIT
437 """)
438 assert mod.f(45) == 45.0
439 with pytest.raises(TypeError):
440 mod.f("this is not a number")