Skip to content

Commit 08c9ba5

Browse files
miss-islingtonEclips4ambv
authored
[3.13] gh-134097: Print number of refs & blocks after each statement in new REPL (gh-134136) (gh-134221)
(cherry picked from commit c31547a) Co-authored-by: Kirill Podoprigora <kirill.bast9@mail.ru> Co-authored-by: Łukasz Langa <lukasz@langa.pl>
1 parent 55a7cb1 commit 08c9ba5

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
@@ -114,6 +114,10 @@ def run_multiline_interactive_console(
114114
more_lines = functools.partial(_more_lines, console)
115115
input_n = 0
116116

117+
_is_x_showrefcount_set = sys._xoptions.get("showrefcount")
118+
_is_pydebug_build = hasattr(sys, "gettotalrefcount")
119+
show_ref_count = _is_x_showrefcount_set and _is_pydebug_build
120+
117121
def maybe_run_command(statement: str) -> bool:
118122
statement = statement.strip()
119123
if statement in console.locals or statement not in REPL_COMMANDS:
@@ -168,3 +172,8 @@ def maybe_run_command(statement: str) -> bool:
168172
except:
169173
console.showtraceback()
170174
console.resetbuffer()
175+
if show_ref_count:
176+
console.write(
177+
f"[{sys.gettotalrefcount()} refs,"
178+
f" {sys.getallocatedblocks()} blocks]\n"
179+
)

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
@@ -1380,3 +1380,16 @@ def test_prompt_after_help(self):
13801380
# Extra stuff (newline and `exit` rewrites) are necessary
13811381
# because of how run_repl works.
13821382
self.assertNotIn(">>> \n>>> >>>", cleaned_output)
1383+
1384+
@skipUnless(Py_DEBUG, '-X showrefcount requires a Python debug build')
1385+
def test_showrefcount(self):
1386+
env = os.environ.copy()
1387+
env.pop("PYTHON_BASIC_REPL", "")
1388+
output, _ = self.run_repl("1\n1+2\nexit()\n", cmdline_args=['-Xshowrefcount'], env=env)
1389+
matches = re.findall(r'\[-?\d+ refs, \d+ blocks\]', output)
1390+
self.assertEqual(len(matches), 3)
1391+
1392+
env["PYTHON_BASIC_REPL"] = "1"
1393+
output, _ = self.run_repl("1\n1+2\nexit()\n", cmdline_args=['-Xshowrefcount'], env=env)
1394+
matches = re.findall(r'\[-?\d+ refs, \d+ blocks\]', output)
1395+
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)