29
29
to be able to use e.g. pytest.raises (which on PyPy will be implemented by a
30
30
"fake pytest module")
31
31
"""
32
+ import math
33
+
32
34
from .support import HPyTest
33
35
34
36
@@ -339,6 +341,12 @@ def foo(self, value):
339
341
mod .f (b )
340
342
assert b .foo is True
341
343
344
+ def check_subscript_type_error (self , fun ):
345
+ import pytest
346
+ for obj in [42 , 3.14 , None ]:
347
+ with pytest .raises (TypeError ):
348
+ fun (obj )
349
+
342
350
def test_getitem (self ):
343
351
import pytest
344
352
mod = self .make_module ("""
@@ -359,6 +367,9 @@ def test_getitem(self):
359
367
@INIT
360
368
""" )
361
369
assert mod .f ({3 : "hello" }) == "hello"
370
+ assert mod .f ({3 : 42 }) == 42
371
+ assert mod .f ({3 : 0.5 }) == 0.5
372
+ assert math .isnan (mod .f ({3 : math .nan }))
362
373
with pytest .raises (KeyError ) as exc :
363
374
mod .f ({1 : "bad" })
364
375
assert exc .value .args == (3 ,)
@@ -367,6 +378,8 @@ def test_getitem(self):
367
378
with pytest .raises (IndexError ):
368
379
mod .f ([])
369
380
381
+ self .check_subscript_type_error (mod .f )
382
+
370
383
def test_getitem_i (self ):
371
384
import pytest
372
385
mod = self .make_module ("""
@@ -383,6 +396,9 @@ def test_getitem_i(self):
383
396
@INIT
384
397
""" )
385
398
assert mod .f ({3 : "hello" }) == "hello"
399
+ assert mod .f ({3 : 42 }) == 42
400
+ assert mod .f ({3 : 0.5 }) == 0.5
401
+ assert math .isnan (mod .f ({3 : math .nan }))
386
402
with pytest .raises (KeyError ) as exc :
387
403
mod .f ({1 : "bad" })
388
404
assert exc .value .args == (3 ,)
@@ -391,6 +407,8 @@ def test_getitem_i(self):
391
407
with pytest .raises (IndexError ):
392
408
mod .f ([])
393
409
410
+ self .check_subscript_type_error (mod .f )
411
+
394
412
def test_getitem_s (self ):
395
413
import pytest
396
414
mod = self .make_module ("""
@@ -407,13 +425,18 @@ def test_getitem_s(self):
407
425
@INIT
408
426
""" )
409
427
assert mod .f ({"limes" : "hello" }) == "hello"
428
+ assert mod .f ({"limes" : 42 }) == 42
429
+ assert mod .f ({"limes" : 0.5 }) == 0.5
430
+ assert math .isnan (mod .f ({"limes" : math .nan }))
410
431
with pytest .raises (KeyError ) as exc :
411
432
mod .f ({"oranges" : "bad" })
412
433
assert exc .value .args == ("limes" ,)
413
434
414
435
with pytest .raises (TypeError ):
415
436
mod .f ([])
416
437
438
+ self .check_subscript_type_error (mod .f )
439
+
417
440
def test_setitem (self ):
418
441
import pytest
419
442
mod = self .make_module ("""
@@ -442,6 +465,9 @@ def test_setitem(self):
442
465
with pytest .raises (IndexError ):
443
466
mod .f ([])
444
467
468
+ self .check_subscript_type_error (mod .f )
469
+
470
+
445
471
def test_setitem_i (self ):
446
472
import pytest
447
473
mod = self .make_module ("""
@@ -465,6 +491,8 @@ def test_setitem_i(self):
465
491
with pytest .raises (IndexError ):
466
492
mod .f ([])
467
493
494
+ self .check_subscript_type_error (mod .f )
495
+
468
496
def test_setitem_s (self ):
469
497
import pytest
470
498
mod = self .make_module ("""
@@ -487,6 +515,8 @@ def test_setitem_s(self):
487
515
with pytest .raises (TypeError ):
488
516
mod .f ([])
489
517
518
+ self .check_subscript_type_error (mod .f )
519
+
490
520
def test_length (self ):
491
521
mod = self .make_module ("""
492
522
HPyDef_METH(f, "f", f_impl, HPyFunc_O)
0 commit comments