@@ -565,7 +565,23 @@ def test_fstring_nested_too_deeply(self):
565565 self .assertAllRaise (SyntaxError ,
566566 "f-string: expressions nested too deeply" ,
567567 ['f"{1+2:{1+2:{1+1:{1}}}}"' ])
568+
569+ def create_nested_fstring (n ):
570+ if n == 0 :
571+ return "1+1"
572+ prev = create_nested_fstring (n - 1 )
573+ return f'f"{{{ prev } }}"'
568574
575+ self .assertAllRaise (SyntaxError ,
576+ "too many nested f-strings" ,
577+ [create_nested_fstring (160 )])
578+
579+ def test_syntax_error_in_nested_fstring (self ):
580+ # See gh-104016 for more information on this crash
581+ self .assertAllRaise (SyntaxError ,
582+ "invalid syntax" ,
583+ ['f"{1 1:' + ('{f"1:' * 199 )])
584+
569585 def test_double_braces (self ):
570586 self .assertEqual (f'{{' , '{' )
571587 self .assertEqual (f'a{{' , 'a{' )
@@ -1355,7 +1371,6 @@ def test_filename_in_syntaxerror(self):
13551371 # see issue 38964
13561372 with temp_cwd () as cwd :
13571373 file_path = os .path .join (cwd , 't.py' )
1358- with open (file_path , 'w' , encoding = "utf-8" ) as f :
13591374 f .write ('f"{a b}"' ) # This generates a SyntaxError
13601375 _ , _ , stderr = assert_python_failure (file_path ,
13611376 PYTHONIOENCODING = 'ascii' )
@@ -1549,9 +1564,5 @@ def test_syntax_error_after_debug(self):
15491564 "f'{1=}{1;}'" ,
15501565 ])
15511566
1552- def test_nested_fstring_max_stack_level (self ):
1553- with self .assertRaises (SyntaxError ):
1554- compile ('f"{1 1:' + ('{f"1:' * 199 ), "?" , "exec" )
1555-
15561567if __name__ == '__main__' :
15571568 unittest .main ()
0 commit comments