1
1
import unittest
2
2
from collections import OrderedDict
3
- import _testcapi
3
+ from test . support import import_helper
4
4
5
+ _testcapi = import_helper .import_module ('_testcapi' )
6
+ from _testcapi import PY_SSIZE_T_MIN , PY_SSIZE_T_MAX
5
7
6
8
NULL = None
7
9
@@ -574,6 +576,8 @@ def test_sequence_getitem(self):
574
576
self .assertEqual (getitem (lst , 1 ), 'b' )
575
577
self .assertEqual (getitem (lst , - 1 ), 'c' )
576
578
self .assertRaises (IndexError , getitem , lst , 3 )
579
+ self .assertRaises (IndexError , getitem , lst , PY_SSIZE_T_MAX )
580
+ self .assertRaises (IndexError , getitem , lst , PY_SSIZE_T_MIN )
577
581
578
582
self .assertRaises (TypeError , getitem , 42 , 1 )
579
583
self .assertRaises (TypeError , getitem , {}, 1 )
@@ -598,6 +602,9 @@ def test_sequence_repeat(self):
598
602
self .assertEqual (repeat (('a' , 'b' ), 2 ), ('a' , 'b' , 'a' , 'b' ))
599
603
self .assertEqual (repeat (['a' , 'b' ], 0 ), [])
600
604
self .assertEqual (repeat (['a' , 'b' ], - 1 ), [])
605
+ self .assertEqual (repeat (['a' , 'b' ], PY_SSIZE_T_MIN ), [])
606
+ self .assertEqual (repeat ([], PY_SSIZE_T_MAX ), [])
607
+ self .assertRaises (MemoryError , repeat , ['a' , 'b' ], PY_SSIZE_T_MAX )
601
608
602
609
self .assertRaises (TypeError , repeat , set (), 2 )
603
610
self .assertRaises (TypeError , repeat , 42 , 2 )
@@ -631,6 +638,9 @@ def test_sequence_inplacerepeat(self):
631
638
self .assertEqual (inplacerepeat (('a' , 'b' ), 2 ), ('a' , 'b' , 'a' , 'b' ))
632
639
self .assertEqual (inplacerepeat (['a' , 'b' ], 0 ), [])
633
640
self .assertEqual (inplacerepeat (['a' , 'b' ], - 1 ), [])
641
+ self .assertEqual (inplacerepeat (['a' , 'b' ], PY_SSIZE_T_MIN ), [])
642
+ self .assertEqual (inplacerepeat ([], PY_SSIZE_T_MAX ), [])
643
+ self .assertRaises (MemoryError , inplacerepeat , ['a' , 'b' ], PY_SSIZE_T_MAX )
634
644
635
645
self .assertRaises (TypeError , inplacerepeat , set (), 2 )
636
646
self .assertRaises (TypeError , inplacerepeat , 42 , 2 )
@@ -647,6 +657,8 @@ def test_sequence_setitem(self):
647
657
setitem (lst , 0 , NULL )
648
658
self .assertEqual (lst , ['x' , 'y' ])
649
659
self .assertRaises (IndexError , setitem , lst , 3 , 'x' )
660
+ self .assertRaises (IndexError , setitem , lst , PY_SSIZE_T_MAX , 'x' )
661
+ self .assertRaises (IndexError , setitem , lst , PY_SSIZE_T_MIN , 'x' )
650
662
651
663
self .assertRaises (TypeError , setitem , 42 , 1 , 'x' )
652
664
self .assertRaises (TypeError , setitem , {}, 1 , 'x' )
@@ -660,6 +672,8 @@ def test_sequence_delitem(self):
660
672
delitem (lst , - 1 )
661
673
self .assertEqual (lst , ['a' ])
662
674
self .assertRaises (IndexError , delitem , lst , 3 )
675
+ self .assertRaises (IndexError , delitem , lst , PY_SSIZE_T_MAX )
676
+ self .assertRaises (IndexError , delitem , lst , PY_SSIZE_T_MIN )
663
677
664
678
self .assertRaises (TypeError , delitem , 42 , 1 )
665
679
self .assertRaises (TypeError , delitem , {}, 1 )
@@ -669,13 +683,19 @@ def test_sequence_setslice(self):
669
683
setslice = _testcapi .sequence_setslice
670
684
671
685
# Correct case:
672
- data = [1 , 2 , 3 , 4 , 5 ]
673
- data_copy = data .copy ()
674
-
675
- setslice (data , 1 , 3 , [8 , 9 ])
676
- data_copy [1 :3 ] = [8 , 9 ]
677
- self .assertEqual (data , data_copy )
678
- self .assertEqual (data , [1 , 8 , 9 , 4 , 5 ])
686
+ for start in [* range (- 6 , 7 ), PY_SSIZE_T_MIN , PY_SSIZE_T_MAX ]:
687
+ for stop in [* range (- 6 , 7 ), PY_SSIZE_T_MIN , PY_SSIZE_T_MAX ]:
688
+ data = [1 , 2 , 3 , 4 , 5 ]
689
+ data_copy = [1 , 2 , 3 , 4 , 5 ]
690
+ setslice (data , start , stop , [8 , 9 ])
691
+ data_copy [start :stop ] = [8 , 9 ]
692
+ self .assertEqual (data , data_copy )
693
+
694
+ data = [1 , 2 , 3 , 4 , 5 ]
695
+ data_copy = [1 , 2 , 3 , 4 , 5 ]
696
+ setslice (data , start , stop , NULL )
697
+ del data_copy [start :stop ]
698
+ self .assertEqual (data , data_copy )
679
699
680
700
# Custom class:
681
701
class Custom :
@@ -701,21 +721,17 @@ def __setitem__(self, index, value):
701
721
self .assertRaises (TypeError , setslice , object (), 1 , 3 , 'xy' )
702
722
self .assertRaises (SystemError , setslice , NULL , 1 , 3 , 'xy' )
703
723
704
- data_copy = data .copy ()
705
- setslice (data_copy , 1 , 3 , NULL )
706
- self .assertEqual (data_copy , [1 , 4 , 5 ])
707
-
708
724
def test_sequence_delslice (self ):
709
725
delslice = _testcapi .sequence_delslice
710
726
711
727
# Correct case:
712
- data = [ 1 , 2 , 3 , 4 , 5 ]
713
- data_copy = data . copy ()
714
-
715
- delslice ( data , 1 , 3 )
716
- del data_copy [ 1 : 3 ]
717
- self . assertEqual ( data , data_copy )
718
- self .assertEqual (data , [ 1 , 4 , 5 ] )
728
+ for start in [ * range ( - 6 , 7 ), PY_SSIZE_T_MIN , PY_SSIZE_T_MAX ]:
729
+ for stop in [ * range ( - 6 , 7 ), PY_SSIZE_T_MIN , PY_SSIZE_T_MAX ]:
730
+ data = [ 1 , 2 , 3 , 4 , 5 ]
731
+ data_copy = [ 1 , 2 , 3 , 4 , 5 ]
732
+ delslice ( data , start , stop )
733
+ del data_copy [ start : stop ]
734
+ self .assertEqual (data , data_copy )
719
735
720
736
# Custom class:
721
737
class Custom :
0 commit comments