@@ -2964,32 +2964,72 @@ def __del__(self):
29642964 elem = b .close ()
29652965 self .assertEqual (elem [0 ].tail , 'ABCDEFGHIJKL' )
29662966
2967- def test_subscr (self ):
2968- # Issue #27863
2967+ def test_subscr_with_clear (self ):
2968+ # See https://github.com/python/cpython/issues/143200.
2969+ self .do_test_subscr_with_mutating_slice (use_clear_method = True )
2970+
2971+ def test_subscr_with_delete (self ):
2972+ # See https://github.com/python/cpython/issues/72050.
2973+ self .do_test_subscr_with_mutating_slice (use_clear_method = False )
2974+
2975+ def do_test_subscr_with_mutating_slice (self , * , use_clear_method ):
29692976 class X :
2977+ def __init__ (self , i = 0 ):
2978+ self .i = i
29702979 def __index__ (self ):
2971- del e [:]
2972- return 1
2980+ if use_clear_method :
2981+ e .clear ()
2982+ else :
2983+ del e [:]
2984+ return self .i
29732985
2974- e = ET .Element ('elem' )
2975- e .append (ET .Element ('child' ))
2976- e [:X ()] # shouldn't crash
2986+ for s in self .get_mutating_slices (X , 10 ):
2987+ with self .subTest (s ):
2988+ e = ET .Element ('elem' )
2989+ e .extend ([ET .Element (f'c{ i } ' ) for i in range (10 )])
2990+ e [s ] # shouldn't crash
29772991
2978- e .append (ET .Element ('child' ))
2979- e [0 :10 :X ()] # shouldn't crash
2992+ def test_ass_subscr_with_mutating_slice (self ):
2993+ # See https://github.com/python/cpython/issues/72050
2994+ # and https://github.com/python/cpython/issues/143200.
29802995
2981- def test_ass_subscr (self ):
2982- # Issue #27863
29832996 class X :
2997+ def __init__ (self , i = 0 ):
2998+ self .i = i
29842999 def __index__ (self ):
29853000 e [:] = []
2986- return 1
3001+ return self .i
3002+
3003+ for s in self .get_mutating_slices (X , 10 ):
3004+ with self .subTest (s ):
3005+ e = ET .Element ('elem' )
3006+ e .extend ([ET .Element (f'c{ i } ' ) for i in range (10 )])
3007+ e [s ] = [] # shouldn't crash
3008+
3009+ def get_mutating_slices (self , index_class , n_children ):
3010+ self .assertGreaterEqual (n_children , 10 )
3011+ return [
3012+ slice (index_class (), None , None ),
3013+ slice (index_class (2 ), None , None ),
3014+ slice (None , index_class (), None ),
3015+ slice (None , index_class (2 ), None ),
3016+ slice (0 , 2 , index_class (1 )),
3017+ slice (0 , 2 , index_class (2 )),
3018+ slice (0 , n_children , index_class (1 )),
3019+ slice (0 , n_children , index_class (2 )),
3020+ slice (0 , 2 * n_children , index_class (1 )),
3021+ slice (0 , 2 * n_children , index_class (2 )),
3022+ ]
29873023
2988- e = ET .Element ('elem' )
2989- for _ in range (10 ):
2990- e .insert (0 , ET .Element ('child' ))
3024+ def test_ass_subscr_with_mutating_iterable_value (self ):
3025+ class V :
3026+ def __iter__ (self ):
3027+ e .clear ()
3028+ return iter ([ET .Element ('a' ), ET .Element ('b' )])
29913029
2992- e [0 :10 :X ()] = [] # shouldn't crash
3030+ e = ET .Element ('elem' )
3031+ e .extend ([ET .Element (f'c{ i } ' ) for i in range (10 )])
3032+ e [:] = V ()
29933033
29943034 def test_treebuilder_start (self ):
29953035 # Issue #27863
0 commit comments