From 1f6041c9aec0c3fe4f066f6d3245c71afafeeac2 Mon Sep 17 00:00:00 2001 From: Kirill Podoprigora Date: Sat, 17 May 2025 14:01:20 +0300 Subject: [PATCH 1/4] Print refs & blocks info after each statement --- Lib/_pyrepl/simple_interact.py | 11 +++++++++++ Lib/test/test_pyrepl/test_pyrepl.py | 14 +++++++++++++- .../2025-05-17-13-46-20.gh-issue-134097.fgkjE1.rst | 1 + 3 files changed, 25 insertions(+), 1 deletion(-) create mode 100644 Misc/NEWS.d/next/Library/2025-05-17-13-46-20.gh-issue-134097.fgkjE1.rst diff --git a/Lib/_pyrepl/simple_interact.py b/Lib/_pyrepl/simple_interact.py index b3848833e14208..1d03b50ba00239 100644 --- a/Lib/_pyrepl/simple_interact.py +++ b/Lib/_pyrepl/simple_interact.py @@ -110,6 +110,15 @@ def run_multiline_interactive_console( more_lines = functools.partial(_more_lines, console) input_n = 0 + if sys._xoptions.get("showrefcount"): + # Were we compiled --with-pydebug? + if hasattr(sys, "gettotalrefcount"): + showrefcount = sys._is_showrefcount_enabled() # type: ignore[attr-defined] + else: + showrefcount = False + else: + showrefcount = False + def maybe_run_command(statement: str) -> bool: statement = statement.strip() if statement in console.locals or statement not in REPL_COMMANDS: @@ -167,3 +176,5 @@ def maybe_run_command(statement: str) -> bool: except: console.showtraceback() console.resetbuffer() + if showrefcount: + console.write(f"\n[{sys.gettotalrefcount()} refs, {sys.getallocatedblocks()} blocks]\n") diff --git a/Lib/test/test_pyrepl/test_pyrepl.py b/Lib/test/test_pyrepl/test_pyrepl.py index fc8114891d12dd..76d99db3b37429 100644 --- a/Lib/test/test_pyrepl/test_pyrepl.py +++ b/Lib/test/test_pyrepl/test_pyrepl.py @@ -10,7 +10,7 @@ import tempfile from unittest import TestCase, skipUnless, skipIf from unittest.mock import patch -from test.support import force_not_colorized, make_clean_env +from test.support import force_not_colorized, make_clean_env, Py_DEBUG from test.support import SHORT_TIMEOUT, STDLIB_DIR from test.support.import_helper import import_module from test.support.os_helper import EnvironmentVarGuard, unlink @@ -1610,3 +1610,15 @@ def test_prompt_after_help(self): # Extra stuff (newline and `exit` rewrites) are necessary # because of how run_repl works. self.assertNotIn(">>> \n>>> >>>", cleaned_output) + + @skipUnless(Py_DEBUG, '-X showrefcount requires a Python debug build') + def test_showrefcount(self): + env = os.environ.copy() + output, _ = self.run_repl("1\n1+2\nexit()\n", cmdline_args=['-Xshowrefcount'], env=env) + matches = re.findall(r'\[-?\d+ refs, \d+ blocks\]', output) + self.assertEqual(len(matches), 3) + + env["PYTHON_BASIC_REPL"] = "1" + output, _ = self.run_repl("1\n1+2\nexit()\n", cmdline_args=['-Xshowrefcount'], env=env) + matches = re.findall(r'\[-?\d+ refs, \d+ blocks\]', output) + self.assertEqual(len(matches), 3) diff --git a/Misc/NEWS.d/next/Library/2025-05-17-13-46-20.gh-issue-134097.fgkjE1.rst b/Misc/NEWS.d/next/Library/2025-05-17-13-46-20.gh-issue-134097.fgkjE1.rst new file mode 100644 index 00000000000000..0b388d9db380f5 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2025-05-17-13-46-20.gh-issue-134097.fgkjE1.rst @@ -0,0 +1 @@ +Fix interaction of the new :term:`REPL` and :option:`-X showrefcount <-X>` command line option. From a2d21e455eda28680a463ab331d23533e97f9798 Mon Sep 17 00:00:00 2001 From: Kirill Podoprigora Date: Sat, 17 May 2025 14:03:35 +0300 Subject: [PATCH 2/4] Remove unnecessary newline --- Lib/_pyrepl/simple_interact.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/_pyrepl/simple_interact.py b/Lib/_pyrepl/simple_interact.py index 1d03b50ba00239..1edc9c95e070b2 100644 --- a/Lib/_pyrepl/simple_interact.py +++ b/Lib/_pyrepl/simple_interact.py @@ -177,4 +177,4 @@ def maybe_run_command(statement: str) -> bool: console.showtraceback() console.resetbuffer() if showrefcount: - console.write(f"\n[{sys.gettotalrefcount()} refs, {sys.getallocatedblocks()} blocks]\n") + console.write(f"[{sys.gettotalrefcount()} refs, {sys.getallocatedblocks()} blocks]\n") From d39e368d9c7361894a9cdcdf30b685dc81739cb4 Mon Sep 17 00:00:00 2001 From: Kirill Podoprigora Date: Sat, 17 May 2025 14:05:56 +0300 Subject: [PATCH 3/4] Remove old stuff --- Lib/_pyrepl/simple_interact.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/_pyrepl/simple_interact.py b/Lib/_pyrepl/simple_interact.py index 1edc9c95e070b2..5158cc503a3853 100644 --- a/Lib/_pyrepl/simple_interact.py +++ b/Lib/_pyrepl/simple_interact.py @@ -113,7 +113,7 @@ def run_multiline_interactive_console( if sys._xoptions.get("showrefcount"): # Were we compiled --with-pydebug? if hasattr(sys, "gettotalrefcount"): - showrefcount = sys._is_showrefcount_enabled() # type: ignore[attr-defined] + showrefcount = True else: showrefcount = False else: From cf621d7b6b9dd2c2ff47b3915bf6a4f1ed913f2f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Langa?= Date: Mon, 19 May 2025 14:51:40 +0200 Subject: [PATCH 4/4] Address review comments --- Lib/_pyrepl/simple_interact.py | 18 ++++++++---------- Lib/test/test_pyrepl/test_pyrepl.py | 1 + 2 files changed, 9 insertions(+), 10 deletions(-) diff --git a/Lib/_pyrepl/simple_interact.py b/Lib/_pyrepl/simple_interact.py index 5158cc503a3853..c23894a34b8b53 100644 --- a/Lib/_pyrepl/simple_interact.py +++ b/Lib/_pyrepl/simple_interact.py @@ -110,14 +110,9 @@ def run_multiline_interactive_console( more_lines = functools.partial(_more_lines, console) input_n = 0 - if sys._xoptions.get("showrefcount"): - # Were we compiled --with-pydebug? - if hasattr(sys, "gettotalrefcount"): - showrefcount = True - else: - showrefcount = False - else: - showrefcount = False + _is_x_showrefcount_set = sys._xoptions.get("showrefcount") + _is_pydebug_build = hasattr(sys, "gettotalrefcount") + show_ref_count = _is_x_showrefcount_set and _is_pydebug_build def maybe_run_command(statement: str) -> bool: statement = statement.strip() @@ -176,5 +171,8 @@ def maybe_run_command(statement: str) -> bool: except: console.showtraceback() console.resetbuffer() - if showrefcount: - console.write(f"[{sys.gettotalrefcount()} refs, {sys.getallocatedblocks()} blocks]\n") + if show_ref_count: + console.write( + f"[{sys.gettotalrefcount()} refs," + f" {sys.getallocatedblocks()} blocks]\n" + ) diff --git a/Lib/test/test_pyrepl/test_pyrepl.py b/Lib/test/test_pyrepl/test_pyrepl.py index 76d99db3b37429..f80755b3368f8f 100644 --- a/Lib/test/test_pyrepl/test_pyrepl.py +++ b/Lib/test/test_pyrepl/test_pyrepl.py @@ -1614,6 +1614,7 @@ def test_prompt_after_help(self): @skipUnless(Py_DEBUG, '-X showrefcount requires a Python debug build') def test_showrefcount(self): env = os.environ.copy() + env.pop("PYTHON_BASIC_REPL", "") output, _ = self.run_repl("1\n1+2\nexit()\n", cmdline_args=['-Xshowrefcount'], env=env) matches = re.findall(r'\[-?\d+ refs, \d+ blocks\]', output) self.assertEqual(len(matches), 3)