8000 gh-134097: Print number of refs & blocks after each statement in new REPL by Eclips4 · Pull Request #134136 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-134097: Print number of refs & blocks after each statement in new REPL #134136

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

Merged
merged 4 commits into from
May 19, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Print refs & blocks info after each statement
  • Loading branch information
Eclips4 committed May 17, 2025
commit 1f6041c9aec0c3fe4f066f6d3245c71afafeeac2
11 changes: 11 additions & 0 deletions Lib/_pyrepl/simple_interact.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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")
14 changes: 13 additions & 1 deletion Lib/test/test_pyrepl/test_pyrepl.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix interaction of the new :term:`REPL` and :option:`-X showrefcount <-X>` command line option.
0