@@ -131,6 +131,68 @@ def test_close_stdin(self):
131131 self .assertEqual (process .returncode , 0 )
132132 self .assertIn ('before close' , output )
133133
134+ def test_interactive_traceback_reporting (self ):
135+ user_input = "1 / 0 / 3 / 4"
136+ p = spawn_repl ()
137+ p .stdin .write (user_input )
138+ output = kill_python (p )
139+ self .assertEqual (p .returncode , 0 )
140+
141+ traceback_lines = output .splitlines ()[- 6 :- 1 ]
142+ expected_lines = [
143+ "Traceback (most recent call last):" ,
144+ " File \" <stdin>\" , line 1, in <module>" ,
145+ " 1 / 0 / 3 / 4" ,
146+ " ~~^~~" ,
147+ "ZeroDivisionError: division by zero" ,
148+ ]
149+ self .assertEqual (traceback_lines , expected_lines )
150+
151+ def test_interactive_traceback_reporting_multiple_input (self ):
152+ user_input1 = dedent ("""
153+ def foo(x):
154+ 1 / x
155+
156+ """ )
157+ p = spawn_repl ()
158+ p .stdin .write (user_input1 )
159+ user_input2 = "foo(0)"
160+ p .stdin .write (user_input2 )
161+ output = kill_python (p )
162+ self .assertEqual (p .returncode , 0 )
163+
164+ traceback_lines = output .splitlines ()[- 7 :- 1 ]
165+ expected_lines = [
166+ ' File "<stdin>", line 1, in <module>' ,
167+ ' foo(0)' ,
168+ ' File "<stdin>", line 2, in foo' ,
169+ ' 1 / x' ,
170+ ' ~~^~~' ,
171+ 'ZeroDivisionError: division by zero'
172+ ]
173+ self .assertEqual (traceback_lines , expected_lines )
174+
175+ def test_interactive_source_is_in_linecache (self ):
176+ user_input = dedent ("""
177+ def foo(x):
178+ return x + 1
179+
180+ def bar(x):
181+ return foo(x) + 2
182+ """ )
183+ p = spawn_repl ()
184+ p .stdin .write (user_input )
185+ user_input2 = dedent ("""
186+ import linecache
187+ print(linecache.cache['<python-input-1>'])
188+ """ )
189+ p .stdin .write (user_input2 )
190+ output = kill_python (p )
191+ self .assertEqual (p .returncode , 0 )
192+ expected = "(30, None, [\' def foo(x):\\ n\' , \' return x + 1\\ n\' , \' \\ n\' ], \' <stdin>\' )"
193+ self .assertIn (expected , output , expected )
194+
195+
134196
135197class TestInteractiveModeSyntaxErrors (unittest .TestCase ):
136198
0 commit comments