mirror of
https://github.com/python/cpython.git
synced 2025-09-26 18:29:57 +00:00
[3.14] gh-134097: Print number of refs & blocks after each statement in new REPL (gh-134136) (gh-134220)
(cherry picked from commit c31547a591
)
Co-authored-by: Kirill Podoprigora <kirill.bast9@mail.ru>
Co-authored-by: Łukasz Langa <lukasz@langa.pl>
This commit is contained in:
parent
e3cc4008c4
commit
182464eb9e
3 changed files with 24 additions and 1 deletions
|
@ -110,6 +110,10 @@ def run_multiline_interactive_console(
|
||||||
more_lines = functools.partial(_more_lines, console)
|
more_lines = functools.partial(_more_lines, console)
|
||||||
input_n = 0
|
input_n = 0
|
||||||
|
|
||||||
|
_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:
|
def maybe_run_command(statement: str) -> bool:
|
||||||
statement = statement.strip()
|
statement = statement.strip()
|
||||||
if statement in console.locals or statement not in REPL_COMMANDS:
|
if statement in console.locals or statement not in REPL_COMMANDS:
|
||||||
|
@ -167,3 +171,8 @@ def run_multiline_interactive_console(
|
||||||
except:
|
except:
|
||||||
console.showtraceback()
|
console.showtraceback()
|
||||||
console.resetbuffer()
|
console.resetbuffer()
|
||||||
|
if show_ref_count:
|
||||||
|
console.write(
|
||||||
|
f"[{sys.gettotalrefcount()} refs,"
|
||||||
|
f" {sys.getallocatedblocks()} blocks]\n"
|
||||||
|
)
|
||||||
|
|
|
@ -10,7 +10,7 @@ import sys
|
||||||
import tempfile
|
import tempfile
|
||||||
from unittest import TestCase, skipUnless, skipIf
|
from unittest import TestCase, skipUnless, skipIf
|
||||||
from unittest.mock import patch
|
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 SHORT_TIMEOUT, STDLIB_DIR
|
||||||
from test.support.import_helper import import_module
|
from test.support.import_helper import import_module
|
||||||
from test.support.os_helper import EnvironmentVarGuard, unlink
|
from test.support.os_helper import EnvironmentVarGuard, unlink
|
||||||
|
@ -1610,3 +1610,16 @@ class TestMain(ReplTestCase):
|
||||||
# Extra stuff (newline and `exit` rewrites) are necessary
|
# Extra stuff (newline and `exit` rewrites) are necessary
|
||||||
# because of how run_repl works.
|
# because of how run_repl works.
|
||||||
self.assertNotIn(">>> \n>>> >>>", cleaned_output)
|
self.assertNotIn(">>> \n>>> >>>", cleaned_output)
|
||||||
|
|
||||||
|
@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)
|
||||||
|
|
||||||
|
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)
|
||||||
|
|
|
@ -0,0 +1 @@
|
||||||
|
Fix interaction of the new :term:`REPL` and :option:`-X showrefcount <-X>` command line option.
|
Loading…
Add table
Add a link
Reference in a new issue