Skip to content

Commit c31547a

Browse files
Eclips4ambv
andauthored
pythongh-134097: Print number of refs & blocks after each statement in new REPL (pythongh-134136)
Co-authored-by: Łukasz Langa <[email protected]>
1 parent 44b73d3 commit c31547a

File tree

3 files changed

+24
-1
lines changed

3 files changed

+24
-1
lines changed

Lib/_pyrepl/simple_interact.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,10 @@ def run_multiline_interactive_console(
110110
more_lines = functools.partial(_more_lines, console)
111111
input_n = 0
112112

113+
_is_x_showrefcount_set = sys._xoptions.get("showrefcount")
114+
_is_pydebug_build = hasattr(sys, "gettotalrefcount")
115+
show_ref_count = _is_x_showrefcount_set and _is_pydebug_build
116+
113117
def maybe_run_command(statement: str) -> bool:
114118
statement = statement.strip()
115119
if statement in console.locals or statement not in REPL_COMMANDS:
@@ -167,3 +171,8 @@ def maybe_run_command(statement: str) -> bool:
167171
except:
168172
console.showtraceback()
169173
console.resetbuffer()
174+
if show_ref_count:
175+
console.write(
176+
f"[{sys.gettotalrefcount()} refs,"
177+
f" {sys.getallocatedblocks()} blocks]\n"
178+
)

Lib/test/test_pyrepl/test_pyrepl.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
import tempfile
1111
from unittest import TestCase, skipUnless, skipIf
1212
from unittest.mock import patch
13-
from test.support import force_not_colorized, make_clean_env
13+
from test.support import force_not_colorized, make_clean_env, Py_DEBUG
1414
from test.support import SHORT_TIMEOUT, STDLIB_DIR
1515
from test.support.import_helper import import_module
1616
from test.support.os_helper import EnvironmentVarGuard, unlink
@@ -1610,3 +1610,16 @@ def test_prompt_after_help(self):
16101610
# Extra stuff (newline and `exit` rewrites) are necessary
16111611
# because of how run_repl works.
16121612
self.assertNotIn(">>> \n>>> >>>", cleaned_output)
1613+
1614+
@skipUnless(Py_DEBUG, '-X showrefcount requires a Python debug build')
1615+
def test_showrefcount(self):
1616+
env = os.environ.copy()
1617+
env.pop("PYTHON_BASIC_REPL", "")
1618+
output, _ = self.run_repl("1\n1+2\nexit()\n", cmdline_args=['-Xshowrefcount'], env=env)
1619+
matches = re.findall(r'\[-?\d+ refs, \d+ blocks\]', output)
1620+
self.assertEqual(len(matches), 3)
1621+
1622+
env["PYTHON_BASIC_REPL"] = "1"
1623+
output, _ = self.run_repl("1\n1+2\nexit()\n", cmdline_args=['-Xshowrefcount'], env=env)
1624+
matches = re.findall(r'\[-?\d+ refs, \d+ blocks\]', output)
1625+
self.assertEqual(len(matches), 3)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix interaction of the new :term:`REPL` and :option:`-X showrefcount <-X>` command line option.

0 commit comments

Comments
 (0)