diff options
author | Bartosz SÅ‚awecki <bartoszpiotrslawecki@gmail.com> | 2024-10-01 14:17:22 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-10-01 14:17:22 (GMT) |
commit | 67e01a430f4ecfcb540d6a29b347966ff4e53454 (patch) | |
tree | 043e73955ba1aac1a2d1a8ae791be39d796d496f /Lib/test/test_repl.py | |
parent | 3e3a4d231518f91ff2f3c5a085b3849e32f1d548 (diff) | |
download | cpython-67e01a430f4ecfcb540d6a29b347966ff4e53454.zip cpython-67e01a430f4ecfcb540d6a29b347966ff4e53454.tar.gz cpython-67e01a430f4ecfcb540d6a29b347966ff4e53454.tar.bz2 |
gh-124594: Create and reuse the same context for the entire asyncio REPL session (#124595)
Co-authored-by: Andrew Svetlov <andrew.svetlov@gmail.com>
Diffstat (limited to 'Lib/test/test_repl.py')
-rw-r--r-- | Lib/test/test_repl.py | 37 |
1 files changed, 37 insertions, 0 deletions
diff --git a/Lib/test/test_repl.py b/Lib/test/test_repl.py index 7a7285a..e764e60 100644 --- a/Lib/test/test_repl.py +++ b/Lib/test/test_repl.py @@ -291,5 +291,42 @@ class TestInteractiveModeSyntaxErrors(unittest.TestCase): self.assertEqual(traceback_lines, expected_lines) +class TestAsyncioREPLContextVars(unittest.TestCase): + def test_toplevel_contextvars_sync(self): + user_input = dedent("""\ + from contextvars import ContextVar + var = ContextVar("var", default="failed") + var.set("ok") + """) + p = spawn_repl("-m", "asyncio") + p.stdin.write(user_input) + user_input2 = dedent(""" + print(f"toplevel contextvar test: {var.get()}") + """) + p.stdin.write(user_input2) + output = kill_python(p) + self.assertEqual(p.returncode, 0) + expected = "toplevel contextvar test: ok" + self.assertIn(expected, output, expected) + + def test_toplevel_contextvars_async(self): + user_input = dedent("""\ + from contextvars import ContextVar + var = ContextVar('var', default='failed') + """) + p = spawn_repl("-m", "asyncio") + p.stdin.write(user_input) + user_input2 = "async def set_var(): var.set('ok')\n" + p.stdin.write(user_input2) + user_input3 = "await set_var()\n" + p.stdin.write(user_input3) + user_input4 = "print(f'toplevel contextvar test: {var.get()}')\n" + p.stdin.write(user_input4) + output = kill_python(p) + self.assertEqual(p.returncode, 0) + expected = "toplevel contextvar test: ok" + self.assertIn(expected, output, expected) + + if __name__ == "__main__": unittest.main() |