]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.13] gh-119548: Add a 'clear' command to the REPL (GH-119549) (#119552)
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Fri, 31 May 2024 08:35:21 +0000 (10:35 +0200)
committerGitHub <noreply@github.com>
Fri, 31 May 2024 08:35:21 +0000 (08:35 +0000)
gh-119548: Add a 'clear' command to the REPL (GH-119549)
(cherry picked from commit e3bac04c37f6823cebc74d97feae0e0c25818b31)

Co-authored-by: Pablo Galindo Salgado <Pablogsal@gmail.com>
Lib/_pyrepl/reader.py
Lib/_pyrepl/simple_interact.py
Misc/NEWS.d/next/Core and Builtins/2024-05-25-16-45-27.gh-issue-119548.pqF9Y6.rst [new file with mode: 0644]

index 0f0ef15f9eb2eab6ef62c50e52dec6cc763d1643..1c816d5bda5fedfb41d21f494722507e853eb845 100644 (file)
@@ -238,6 +238,7 @@ class Reader:
     cxy: tuple[int, int] = field(init=False)
     lxy: tuple[int, int] = field(init=False)
     calc_screen: CalcScreen = field(init=False)
+    scheduled_commands: list[str] = field(default_factory=list)
 
     def __post_init__(self) -> None:
         # Enable the use of `insert` without a `prepare` call - necessary to
@@ -557,6 +558,10 @@ class Reader:
             self.restore()
             raise
 
+        while self.scheduled_commands:
+            cmd = self.scheduled_commands.pop()
+            self.do_cmd((cmd, []))
+
     def last_command_is(self, cls: type) -> bool:
         if not self.last_command:
             return False
index 1568a73c1b5ec0c3463d7fc8acb8ce0cd304880d..11e831c1d6c5d4a1e954fb3e442d541c2befff8a 100644 (file)
@@ -57,12 +57,17 @@ def _strip_final_indent(text: str) -> str:
     return text
 
 
+def _clear_screen():
+    reader = _get_reader()
+    reader.scheduled_commands.append("clear_screen")
+
+
 REPL_COMMANDS = {
     "exit": _sitebuiltins.Quitter('exit', ''),
     "quit": _sitebuiltins.Quitter('quit' ,''),
     "copyright": _sitebuiltins._Printer('copyright', sys.copyright),
     "help": "help",
-    "clear": "clear_screen",
+    "clear": _clear_screen,
 }
 
 class InteractiveColoredConsole(code.InteractiveConsole):
diff --git a/Misc/NEWS.d/next/Core and Builtins/2024-05-25-16-45-27.gh-issue-119548.pqF9Y6.rst b/Misc/NEWS.d/next/Core and Builtins/2024-05-25-16-45-27.gh-issue-119548.pqF9Y6.rst
new file mode 100644 (file)
index 0000000..0318790
--- /dev/null
@@ -0,0 +1 @@
+Add a ``clear`` command to the REPL. Patch by Pablo Galindo