1
1
import unittest
2
2
import sys
3
3
from collections import OrderedDict
4
- from test import support
5
4
from test .support import import_helper
6
- import _testcapi
7
5
6
+ _testcapi = import_helper .import_module ('_testcapi' )
7
+ from _testcapi import PY_SSIZE_T_MIN , PY_SSIZE_T_MAX
8
8
9
9
NULL = None
10
10
@@ -446,6 +446,8 @@ def test_sequence_getitem(self):
446
446
self .assertEqual (getitem (lst , 1 ), 'b' )
447
447
self .assertEqual (getitem (lst , - 1 ), 'c' )
448
448
self .assertRaises (IndexError , getitem , lst , 3 )
449
+ self .assertRaises (IndexError , getitem , lst , PY_SSIZE_T_MAX )
450
+ self .assertRaises (IndexError , getitem , lst , PY_SSIZE_T_MIN )
449
451
450
452
self .assertRaises (TypeError , getitem , 42 , 1 )
451
453
self .assertRaises (TypeError , getitem , {}, 1 )
@@ -470,6 +472,9 @@ def test_sequence_repeat(self):
470
472
self .assertEqual (repeat (('a' , 'b' ), 2 ), ('a' , 'b' , 'a' , 'b' ))
471
473
self .assertEqual (repeat (['a' , 'b' ], 0 ), [])
472
474
self .assertEqual (repeat (['a' , 'b' ], - 1 ), [])
475
+ self .assertEqual (repeat (['a' , 'b' ], PY_SSIZE_T_MIN ), [])
476
+ self .assertEqual (repeat ([], PY_SSIZE_T_MAX ), [])
477
+ self .assertRaises (MemoryError , repeat , ['a' , 'b' ], PY_SSIZE_T_MAX )
473
478
474
479
self .assertRaises (TypeError , repeat , set (), 2 )
475
480
self .assertRaises (TypeError , repeat , 42 , 2 )
@@ -503,6 +508,9 @@ def test_sequence_inplacerepeat(self):
503
508
self .assertEqual (inplacerepeat (('a' , 'b' ), 2 ), ('a' , 'b' , 'a' , 'b' ))
504
509
self .assertEqual (inplacerepeat (['a' , 'b' ], 0 ), [])
505
510
self .assertEqual (inplacerepeat (['a' , 'b' ], - 1 ), [])
511
+ self .assertEqual (inplacerepeat (['a' , 'b' ], PY_SSIZE_T_MIN ), [])
512
+ self .assertEqual (inplacerepeat ([], PY_SSIZE_T_MAX ), [])
513
+ self .assertRaises (MemoryError , inplacerepeat , ['a' , 'b' ], PY_SSIZE_T_MAX )
506
514
507
515
self .assertRaises (TypeError , inplacerepeat , set (), 2 )
508
516
self .assertRaises (TypeError , inplacerepeat , 42 , 2 )
@@ -519,6 +527,8 @@ def test_sequence_setitem(self):
519
527
setitem (lst , 0 , NULL )
520
528
self .assertEqual (lst , ['x' , 'y' ])
521
529
self .assertRaises (IndexError , setitem , lst , 3 , 'x' )
530
+ self .assertRaises (IndexError , setitem , lst , PY_SSIZE_T_MAX , 'x' )
531
+ self .assertRaises (IndexError , setitem , lst , PY_SSIZE_T_MIN , 'x' )
522
532
523
533
self .assertRaises (TypeError , setitem , 42 , 1 , 'x' )
524
534
self .assertRaises (TypeError , setitem , {}, 1 , 'x' )
@@ -532,6 +542,8 @@ def test_sequence_delitem(self):
532
542
delitem (lst , - 1 )
533
543
self .assertEqual (lst , ['a' ])
534
544
self .assertRaises (IndexError , delitem , lst , 3 )
545
+ self .assertRaises (IndexError , delitem , lst , PY_SSIZE_T_MAX )
546
+ self .assertRaises (IndexError , delitem , lst , PY_SSIZE_T_MIN )
535
547
536
548
self .assertRaises (TypeError , delitem , 42 , 1 )
537
549
self .assertRaises (TypeError , delitem , {}, 1 )
@@ -541,13 +553,19 @@ def test_sequence_setslice(self):
541
553
setslice = _testcapi .sequence_setslice
542
554
543
555
# Correct case:
544
- data = [1 , 2 , 3 , 4 , 5 ]
545
- data_copy = data .copy ()
546
-
547
- setslice (data , 1 , 3 , [8 , 9 ])
548
- data_copy [1 :3 ] = [8 , 9 ]
549
- self .assertEqual (data , data_copy )
550
- self .assertEqual (data , [1 , 8 , 9 , 4 , 5 ])
556
+ for start in [* range (- 6 , 7 ), PY_SSIZE_T_MIN , PY_SSIZE_T_MAX ]:
557
+ for stop in [* range (- 6 , 7 ), PY_SSIZE_T_MIN , PY_SSIZE_T_MAX ]:
558
+ data = [1 , 2 , 3 , 4 , 5 ]
559
+ data_copy = [1 , 2 , 3 , 4 , 5 ]
560
+ setslice (data , start , stop , [8 , 9 ])
561
+ data_copy [start :stop ] = [8 , 9 ]
562
+ self .assertEqual (data , data_copy )
563
+
564
+ data = [1 , 2 , 3 , 4 , 5 ]
565
+ data_copy = [1 , 2 , 3 , 4 , 5 ]
566
+ setslice (data , start , stop , NULL )
567
+ del data_copy [start :stop ]
568
+ self .assertEqual (data , data_copy )
551
569
552
570
# Custom class:
553
571
class Custom :
@@ -573,21 +591,17 @@ def __setitem__(self, index, value):
573
591
self .assertRaises (TypeError , setslice , object (), 1 , 3 , 'xy' )
574
592
self .assertRaises (SystemError , setslice , NULL , 1 , 3 , 'xy' )
575
593
576
- data_copy = data .copy ()
577
- setslice (data_copy , 1 , 3 , NULL )
578
- self .assertEqual (data_copy , [1 , 4 , 5 ])
579
-
580
594
def test_sequence_delslice (self ):
581
595
delslice = _testcapi .sequence_delslice
582
596
583
597
# Correct case:
584
- data = [ 1 , 2 , 3 , 4 , 5 ]
585
- data_copy = data . copy ()
586
-
587
- delslice ( data , 1 , 3 )
588
- del data_copy [ 1 : 3 ]
589
- self . assertEqual ( data , data_copy )
590
- self .assertEqual (data , [ 1 , 4 , 5 ] )
598
+ for start in [ * range ( - 6 , 7 ), PY_SSIZE_T_MIN , PY_SSIZE_T_MAX ]:
599
+ for stop in [ * range ( - 6 , 7 ), PY_SSIZE_T_MIN , PY_SSIZE_T_MAX ]:
600
+ data = [ 1 , 2 , 3 , 4 , 5 ]
601
+ data_copy = [ 1 , 2 , 3 , 4 , 5 ]
602
+ delslice ( data , start , stop )
603
+ del data_copy [ start : stop ]
604
+ self .assertEqual (data , data_copy )
591
605
592
606
# Custom class:
593
607
class Custom :
0 commit comments