-
-
Notifications
You must be signed in to change notification settings - Fork 34.4k
gh-140287: Handle PYTHONSTARTUP script exceptions in the asyncio REPL
#140288
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
faa22f4
5b3ad2c
00edac4
e396622
4079074
0440d0e
af6f657
848638d
d158dbf
9355da7
3dc4cac
c786584
b76db67
cad1748
d114ed5
e6e10ad
ac6cd83
149740a
bffac1c
a4c307e
a774da5
b3ed3d4
df6dfd8
b004839
ce03cce
9e92510
0a50a50
f8b8d53
5701fef
393aaad
875fd2a
4377d82
c4e488e
b9ffea7
ab799e6
2f8f99b
6020996
125efa6
cc624d1
812d22f
6749b83
e76426d
376c7a4
8888083
a5e4ff0
0d246a5
e19437b
57af54d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,6 +5,7 @@ | |
| import subprocess | ||
| import sys | ||
| import unittest | ||
| from contextlib import contextmanager | ||
| from functools import partial | ||
| from textwrap import dedent | ||
| from test import support | ||
|
|
@@ -67,6 +68,19 @@ def spawn_repl(*args, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, custom=F | |
| spawn_asyncio_repl = partial(spawn_repl, "-m", "asyncio", custom=True) | ||
|
|
||
|
|
||
| @contextmanager | ||
| def new_pythonstartup_env(*, code: str, histfile: str = ".pythonhist"): | ||
| """Create environment variables for a PYTHONSTARTUP script in a temporary directory.""" | ||
| with os_helper.temp_dir() as tmpdir: | ||
| filename = os.path.join(tmpdir, "pythonstartup.py") | ||
| with open(filename, "w") as f: | ||
| f.write(code) | ||
| yield { | ||
| "PYTHONSTARTUP": filename, | ||
| "PYTHON_HISTORY": os.path.join(tmpdir, histfile) | ||
| } | ||
|
Comment on lines
+71
to
+81
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This will be useful in #143023. I can inline it in |
||
|
|
||
|
|
||
| def run_on_interactive_mode(source): | ||
| """Spawn a new Python interpreter, pass the given | ||
| input source code from the stdin and return the | ||
|
|
@@ -276,8 +290,6 @@ def make_repl(env): | |
| """) % script | ||
| self.assertIn(expected, output) | ||
|
|
||
|
|
||
|
|
||
| def test_runsource_show_syntax_error_location(self): | ||
| user_input = dedent("""def f(x, x): ... | ||
| """) | ||
|
|
@@ -392,6 +404,7 @@ def f(): | |
| self.assertEqual(traceback_lines, expected_lines) | ||
|
|
||
|
|
||
| @support.force_not_colorized_test_class | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure this is needed, but I think it's OK to keep it as is. |
||
| class TestAsyncioREPL(unittest.TestCase): | ||
| def test_multiple_statements_fail_early(self): | ||
| user_input = "1 / 0; print(f'afterwards: {1+1}')" | ||
|
|
@@ -449,6 +462,27 @@ def test_quiet_mode(self): | |
| self.assertEqual(p.returncode, 0) | ||
| self.assertEqual(output[:3], ">>>") | ||
|
|
||
| def test_pythonstartup_failure(self): | ||
| startup_code = "1/0\n" | ||
| startup_env = self.enterContext( | ||
| new_pythonstartup_env(code=startup_code, histfile=".asyncio_history")) | ||
|
|
||
| p = spawn_repl( | ||
| "-qm", "asyncio", | ||
| env=os.environ | startup_env, | ||
| isolated=False, | ||
| custom=True) | ||
| p.stdin.write("print('executed user code anyway')") | ||
| output = kill_python(p) | ||
| expected = dedent(f"""\ | ||
| File "{startup_env['PYTHONSTARTUP']}", line 1, in <module> | ||
| 1/0 | ||
| ~^~ | ||
| ZeroDivisionError: division by zero | ||
| """) | ||
| self.assertIn(expected, output) | ||
| self.assertIn("executed user code anyway", output) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| unittest.main() | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| The :mod:`asyncio` REPL now handles exceptions when executing :envvar:`PYTHONSTARTUP` scripts. | ||
| Patch by Bartosz Sławecki. |
Uh oh!
There was an error while loading. Please reload this page.