@@ -3802,6 +3802,26 @@ def test_traceback_header(self):
38023802 exc = traceback .TracebackException (Exception , Exception ("haven" ), None )
38033803 self .assertEqual (list (exc .format ()), ["Exception: haven\n " ])
38043804
3805+ def test_exception_punctuation_handling_with_suggestions (self ):
3806+ def raise_mssage (message , name ):
3807+ try :
3808+ raise NameError (message , name = name )
3809+ except Exception as e :
3810+ return traceback .TracebackException .from_exception (e )._str
3811+
3812+ test_cases = [
3813+ ("Error." , "time" , "Error. Did you forget to import 'time'?" ),
3814+ ("Error?" , "time" , "Error? Did you forget to import 'time'?" ),
3815+ ("Error!" , "time" , "Error! Did you forget to import 'time'?" ),
3816+ ("Error" , "time" , "Error. Did you forget to import 'time'?" ),
3817+ ("Error" , "foo123" , "Error" ),
3818+ ]
3819+ for puctuation , name , expected in test_cases :
3820+ with self .subTest (puctuation = puctuation ):
3821+ messsage = raise_mssage (puctuation , name )
3822+ self .assertEqual (messsage , expected )
3823+
3824+
38053825 @requires_debug_ranges ()
38063826 def test_print (self ):
38073827 def f ():
@@ -4878,28 +4898,6 @@ class PurePythonSuggestionFormattingTests(
48784898 traceback printing in traceback.py.
48794899 """
48804900
4881- def test_exception_punctuation_handling_with_suggestions (self ):
4882- def raise_with_period (): raise NameError ("Error." , name = 'time' )
4883- def raise_with_exclamation (): raise NameError ("Error!" , name = 'time' )
4884- def raise_with_question (): raise NameError ("Error?" , name = 'time' )
4885- def raise_without_punctuation (): raise NameError ("Error" , name = 'time' )
4886-
4887- test_cases = [
4888- (raise_with_period , "." ),
4889- (raise_with_exclamation , "!" ),
4890- (raise_with_question , "?" ),
4891- (raise_without_punctuation , "." ),
4892- ]
4893-
4894- for raise_function , punctuation in test_cases :
4895- with self .subTest (raise_func = raise_function .__name__ ):
4896- result_lines = self .get_exception (
4897- raise_function , slice_start = - 1 , slice_end = None
4898- )
4899- expected = f"NameError: Error{ punctuation } Did you forget to import 'time'?"
4900- self .assertEqual (result_lines [0 ], expected )
4901-
4902-
49034901
49044902@cpython_only
49054903class CPythonSuggestionFormattingTests (
0 commit comments