From 244488222481a3d5c5b35d0ff6502cce219f01b8 Mon Sep 17 00:00:00 2001 From: Sergey B Kirpichev Date: Mon, 17 Mar 2025 11:22:59 +0300 Subject: [PATCH 1/2] gh-131217: new repl display output from all statements, separated by ';' Was: ```pycon >>> 1;2 2 ``` Now: ```pycon >>> 1;2 1 2 ``` --- Lib/_pyrepl/console.py | 6 ++---- Lib/test/test_pyrepl/test_interact.py | 11 ++++++++++- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/Lib/_pyrepl/console.py b/Lib/_pyrepl/console.py index 7916638921bbf2..e6719491f1404d 100644 --- a/Lib/_pyrepl/console.py +++ b/Lib/_pyrepl/console.py @@ -201,11 +201,9 @@ def runsource(self, source, filename="", symbol="single"): if tree.body: *_, last_stmt = tree.body for stmt in tree.body: - wrapper = ast.Interactive if stmt is last_stmt else ast.Module - the_symbol = symbol if stmt is last_stmt else "exec" - item = wrapper([stmt]) + item = ast.Interactive([stmt]) try: - code = self.compile.compiler(item, filename, the_symbol) + code = self.compile.compiler(item, filename, symbol) linecache._register_code(code, source, filename) except SyntaxError as e: if e.args[0] == "'await' outside function": diff --git a/Lib/test/test_pyrepl/test_interact.py b/Lib/test/test_pyrepl/test_interact.py index c3204832a6a93a..a278152d5c4b09 100644 --- a/Lib/test/test_pyrepl/test_interact.py +++ b/Lib/test/test_pyrepl/test_interact.py @@ -51,7 +51,16 @@ def test_multiple_statements_output(self): with contextlib.redirect_stdout(f): more = console.push(code, filename="", _symbol="single") # type: ignore[call-arg] self.assertFalse(more) - self.assertEqual(f.getvalue(), "1\n") + self.assertEqual(f.getvalue(), "1\n1\n") + + namespace = {} + code = "'foo';'bar'" + console = InteractiveColoredConsole(namespace, filename="") + f = io.StringIO() + with contextlib.redirect_stdout(f): + more = console.push(code, filename="", _symbol="single") # type: ignore[call-arg] + self.assertFalse(more) + self.assertEqual(f.getvalue(), "'foo'\n'bar'\n") @force_not_colorized def test_multiple_statements_fail_early(self): From ee97ef7c5a3d1a9d51a6e8808360655de52b1b2e Mon Sep 17 00:00:00 2001 From: Sergey B Kirpichev Date: Mon, 17 Mar 2025 12:03:35 +0300 Subject: [PATCH 2/2] + news --- .../next/Library/2025-03-17-12-03-32.gh-issue-131217.ejHRzN.rst | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 Misc/NEWS.d/next/Library/2025-03-17-12-03-32.gh-issue-131217.ejHRzN.rst diff --git a/Misc/NEWS.d/next/Library/2025-03-17-12-03-32.gh-issue-131217.ejHRzN.rst b/Misc/NEWS.d/next/Library/2025-03-17-12-03-32.gh-issue-131217.ejHRzN.rst new file mode 100644 index 00000000000000..21f36c94f4c529 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2025-03-17-12-03-32.gh-issue-131217.ejHRzN.rst @@ -0,0 +1,2 @@ +Fix new repl to display output from multiple statements, separated by ';'. +Patch by Sergey B Kirpichev.