@@ -2726,6 +2726,34 @@ def test_tolerant_fstring_closer_at_expression_entry_depth(self):
27262726 ("f-string: unmatched ']'" , position ),
27272727 )
27282728
2729+ def test_tolerant_fstring_mismatch_keeps_delimiter_state (self ):
2730+ sources = [
2731+ ("f'{x:{)}!]" , (1 , 10 )),
2732+ ("f'{x:{)}!\n ]" , (2 , 1 )),
2733+ ]
2734+ for source , tolerant_position in sources :
2735+ cases = [
2736+ (False , ("f-string: unmatched ')'" , (1 , 7 ))),
2737+ (True , ("f-string: unmatched ']'" , tolerant_position )),
2738+ ]
2739+ for extra_tokens , expected in cases :
2740+ with self .subTest (
2741+ source = source , extra_tokens = extra_tokens
2742+ ):
2743+ with self .assertRaises (tokenize .TokenError ) as caught :
2744+ self ._get_tokens (source , extra_tokens = extra_tokens )
2745+ self .assertEqual (caught .exception .args , expected )
2746+
2747+ with self .subTest (source = source , public = True ):
2748+ with self .assertRaises (tokenize .TokenError ) as caught :
2749+ list (tokenize .tokenize (
2750+ BytesIO (source .encode ()).readline
2751+ ))
2752+ self .assertEqual (
2753+ caught .exception .args ,
2754+ ("f-string: unmatched ']'" , tolerant_position ),
2755+ )
2756+
27292757 def test_int (self ):
27302758
27312759 self .check_tokenize ('0xff <= 255' , """\
0 commit comments