@@ -188,6 +188,68 @@ def foo(x):
188188 ]
189189 self .assertEqual (traceback_lines , expected_lines )
190190
191+ def test_pythonstartup_error_reporting (self ):
192+ # errors based on https://github.com/python/cpython/issues/137576
193+
194+ def make_repl (env ):
195+ return subprocess .Popen (
196+ [os .path .join (os .path .dirname (sys .executable ), '<stdin>' ), "-i" ],
197+ executable = sys .executable ,
198+ text = True ,
199+ stdin = subprocess .PIPE ,
200+ stdout = subprocess .PIPE ,
201+ stderr = subprocess .STDOUT ,
202+ env = env ,
203+ )
204+
205+ # case 1: error in user input, but PYTHONSTARTUP is fine
206+ with os_helper .temp_dir () as tmpdir :
207+ script = os .path .join (tmpdir , "pythonstartup.py" )
208+ with open (script , "w" ) as f :
209+ f .write ("print('from pythonstartup')" + os .linesep )
210+
211+ env = os .environ .copy ()
212+ env ['PYTHONSTARTUP' ] = script
213+ env ["PYTHON_HISTORY" ] = os .path .join (tmpdir , ".pythonhist" )
214+ p = make_repl (env )
215+ p .stdin .write ("1/0" )
216+ output = kill_python (p )
217+ expected = dedent ("""
218+ Traceback (most recent call last):
219+ File "<stdin>", line 1, in <module>
220+ 1/0
221+ ~^~
222+ ZeroDivisionError: division by zero
223+ """ )
224+ self .assertIn ("from pythonstartup" , output )
225+ self .assertIn (expected , output )
226+
227+ # case 2: error in PYTHONSTARTUP triggered by user input
228+ with os_helper .temp_dir () as tmpdir :
229+ script = os .path .join (tmpdir , "pythonstartup.py" )
230+ with open (script , "w" ) as f :
231+ f .write ("def foo():\n 1/0\n " )
232+
233+ env = os .environ .copy ()
234+ env ['PYTHONSTARTUP' ] = script
235+ env ["PYTHON_HISTORY" ] = os .path .join (tmpdir , ".pythonhist" )
236+ p = make_repl (env )
237+ p .stdin .write ('foo()' )
238+ output = kill_python (p )
239+ expected = dedent ("""
240+ Traceback (most recent call last):
241+ File "<stdin>", line 1, in <module>
242+ foo()
243+ ~~~^^
244+ File "%s", line 2, in foo
245+ 1/0
246+ ~^~
247+ ZeroDivisionError: division by zero
248+ """ ) % script
249+ self .assertIn (expected , output )
250+
251+
252+
191253 def test_runsource_show_syntax_error_location (self ):
192254 user_input = dedent ("""def f(x, x): ...
193255 """ )
0 commit comments