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