Skip to content

Commit c115345

Browse files
committed
Catch compilation errors too
1 parent 442bbe3 commit c115345

File tree

2 files changed

+25
-21
lines changed

2 files changed

+25
-21
lines changed

Lib/asyncio/__main__.py

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -101,17 +101,16 @@ def run(self):
101101

102102
if not sys.flags.isolated and (startup_path := os.getenv("PYTHONSTARTUP")):
103103
sys.audit("cpython.run_startup", startup_path)
104-
105-
import tokenize
106-
with tokenize.open(startup_path) as f:
107-
startup_code = compile(f.read(), startup_path, "exec")
108-
try:
104+
try:
105+
import tokenize
106+
with tokenize.open(startup_path) as f:
107+
startup_code = compile(f.read(), startup_path, "exec")
109108
exec(startup_code, console.locals)
110-
# TODO: Revisit in GH-143023
111-
except SystemExit:
112-
raise
113-
except BaseException:
114-
console.showtraceback()
109+
# TODO: Revisit in GH-143023
110+
except SystemExit:
111+
raise
112+
except BaseException:
113+
console.showtraceback()
115114

116115
ps1 = getattr(sys, "ps1", ">>> ")
117116
if CAN_USE_PYREPL:

Lib/test/test_repl.py

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
cpython_only,
1414
has_subprocess_support,
1515
os_helper,
16+
subTests,
1617
SuppressCrashReport,
1718
SHORT_TIMEOUT,
1819
)
@@ -461,8 +462,14 @@ def test_quiet_mode(self):
461462
self.assertEqual(p.returncode, 0)
462463
self.assertEqual(output[:3], ">>>")
463464

464-
def test_pythonstartup_failure(self):
465-
startup_code = "1/0\n"
465+
@subTests(
466+
("startup_code", "expected_error"),
467+
[
468+
("some invalid syntax\n", "SyntaxError: invalid syntax"),
469+
("1/0\n", "ZeroDivisionError: division by zero"),
470+
],
471+
)
472+
def test_pythonstartup_failure(self, startup_code, expected_error):
466473
startup_env = self.enterContext(
467474
new_pythonstartup_env(code=startup_code, histfile=".asyncio_history"))
468475

@@ -471,16 +478,14 @@ def test_pythonstartup_failure(self):
471478
env=os.environ | startup_env,
472479
isolated=False,
473480
custom=True)
474-
p.stdin.write("print('executed user code anyway')")
481+
p.stdin.write("print('user code', 'executed')\n")
475482
output = kill_python(p)
476-
expected = dedent(f"""\
477-
File "{startup_env['PYTHONSTARTUP']}", line 1, in <module>
478-
1/0
479-
~^~
480-
ZeroDivisionError: division by zero
481-
""")
482-
self.assertIn(expected, output)
483-
self.assertIn("executed user code anyway", output)
483+
484+
tb_hint = f'File "{startup_env["PYTHONSTARTUP"]}", line 1'
485+
self.assertIn(tb_hint, output)
486+
self.assertIn(expected_error, output)
487+
488+
self.assertIn("user code executed", output)
484489

485490

486491
if __name__ == "__main__":

0 commit comments

Comments
 (0)