@@ -3010,32 +3010,72 @@ def __del__(self):
30103010 elem = b .close ()
30113011 self .assertEqual (elem [0 ].tail , 'ABCDEFGHIJKL' )
30123012
3013- def test_subscr (self ):
3014- # Issue #27863
3013+ def test_subscr_with_clear (self ):
3014+ # See https://github.com/python/cpython/issues/143200.
3015+ self .do_test_subscr_with_mutating_slice (use_clear_method = True )
3016+
3017+ def test_subscr_with_delete (self ):
3018+ # See https://github.com/python/cpython/issues/72050.
3019+ self .do_test_subscr_with_mutating_slice (use_clear_method = False )
3020+
3021+ def do_test_subscr_with_mutating_slice (self , * , use_clear_method ):
30153022 class X :
3023+ def __init__ (self , i = 0 ):
3024+ self .i = i
30163025 def __index__ (self ):
3017- del e [:]
3018- return 1
3026+ if use_clear_method :
3027+ e .clear ()
3028+ else :
3029+ del e [:]
3030+ return self .i
30193031
3020- e = ET .Element ('elem' )
3021- e .append (ET .Element ('child' ))
3022- e [:X ()] # shouldn't crash
3032+ for s in self .get_mutating_slices (X , 10 ):
3033+ with self .subTest (s ):
3034+ e = ET .Element ('elem' )
3035+ e .extend ([ET .Element (f'c{ i } ' ) for i in range (10 )])
3036+ e [s ] # shouldn't crash
30233037
3024- e .append (ET .Element ('child' ))
3025- e [0 :10 :X ()] # shouldn't crash
3038+ def test_ass_subscr_with_mutating_slice (self ):
3039+ # See https://github.com/python/cpython/issues/72050
3040+ # and https://github.com/python/cpython/issues/143200.
30263041
3027- def test_ass_subscr (self ):
3028- # Issue #27863
30293042 class X :
3043+ def __init__ (self , i = 0 ):
3044+ self .i = i
30303045 def __index__ (self ):
30313046 e [:] = []
3032- return 1
3047+ return self .i
3048+
3049+ for s in self .get_mutating_slices (X , 10 ):
3050+ with self .subTest (s ):
3051+ e = ET .Element ('elem' )
3052+ e .extend ([ET .Element (f'c{ i } ' ) for i in range (10 )])
3053+ e [s ] = [] # shouldn't crash
3054+
3055+ def get_mutating_slices (self , index_class , n_children ):
3056+ self .assertGreaterEqual (n_children , 10 )
3057+ return [
3058+ slice (index_class (), None , None ),
3059+ slice (index_class (2 ), None , None ),
3060+ slice (None , index_class (), None ),
3061+ slice (None , index_class (2 ), None ),
3062+ slice (0 , 2 , index_class (1 )),
3063+ slice (0 , 2 , index_class (2 )),
3064+ slice (0 , n_children , index_class (1 )),
3065+ slice (0 , n_children , index_class (2 )),
3066+ slice (0 , 2 * n_children , index_class (1 )),
3067+ slice (0 , 2 * n_children , index_class (2 )),
3068+ ]
30333069
3034- e = ET .Element ('elem' )
3035- for _ in range (10 ):
3036- e .insert (0 , ET .Element ('child' ))
3070+ def test_ass_subscr_with_mutating_iterable_value (self ):
3071+ class V :
3072+ def __iter__ (self ):
3073+ e .clear ()
3074+ return iter ([ET .Element ('a' ), ET .Element ('b' )])
30373075
3038- e [0 :10 :X ()] = [] # shouldn't crash
3076+ e = ET .Element ('elem' )
3077+ e .extend ([ET .Element (f'c{ i } ' ) for i in range (10 )])
3078+ e [:] = V ()
30393079
30403080 def test_treebuilder_start (self ):
30413081 # Issue #27863
0 commit comments