aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/Lib/test
diff options
context:
space:
mode:
authorY5 <124019959+y5c4l3@users.noreply.github.com>2025-02-24 03:30:33 +0800
committerGitHub <noreply@github.com>2025-02-23 20:30:33 +0100
commita65366ed879a3d9f27cbcc811ed2e05ad1a2af06 (patch)
tree24446b1acaf844ba65a606b449b7feed93a50424 /Lib/test
parent25a7ddf2efeaf77bcf94dbfca28ba3a6fe9ab57e (diff)
downloadcpython-a65366ed879a3d9f27cbcc811ed2e05ad1a2af06.tar.gz
cpython-a65366ed879a3d9f27cbcc811ed2e05ad1a2af06.zip
gh-124096: Enable REPL virtual terminal support on Windows (#124119)
To support virtual terminal mode in Windows PYREPL, we need a scanner to read over the supported escaped VT sequences. Windows REPL input was using virtual key mode, which does not support terminal escape sequences. This patch calls `SetConsoleMode` properly when initializing and send sequences to enable bracketed-paste modes to support verbatim copy-and-paste. Signed-off-by: y5c4l3 <y5c4l3@proton.me> Co-authored-by: Petr Viktorin <encukou@gmail.com> Co-authored-by: Pablo Galindo Salgado <Pablogsal@gmail.com> Co-authored-by: Dustin L. Howett <dustin@howett.net> Co-authored-by: wheeheee <104880306+wheeheee@users.noreply.github.com>
Diffstat (limited to 'Lib/test')
-rw-r--r--Lib/test/test_pyrepl/test_eventqueue.py (renamed from Lib/test/test_pyrepl/test_unix_eventqueue.py)70
1 files changed, 48 insertions, 22 deletions
diff --git a/Lib/test/test_pyrepl/test_unix_eventqueue.py b/Lib/test/test_pyrepl/test_eventqueue.py
index 301f79927a7..a1bac38fbd4 100644
--- a/Lib/test/test_pyrepl/test_unix_eventqueue.py
+++ b/Lib/test/test_pyrepl/test_eventqueue.py
@@ -2,70 +2,77 @@ import tempfile
import unittest
import sys
from unittest.mock import patch
+from test import support
try:
from _pyrepl.console import Event
- from _pyrepl.unix_eventqueue import EventQueue
+ from _pyrepl import base_eventqueue
except ImportError:
pass
-@unittest.skipIf(sys.platform == "win32", "No Unix event queue on Windows")
-@patch("_pyrepl.curses.tigetstr", lambda x: b"")
-class TestUnixEventQueue(unittest.TestCase):
- def setUp(self):
- self.file = tempfile.TemporaryFile()
+try:
+ from _pyrepl import unix_eventqueue
+except ImportError:
+ pass
- def tearDown(self) -> None:
- self.file.close()
+try:
+ from _pyrepl import windows_eventqueue
+except ImportError:
+ pass
+
+class EventQueueTestBase:
+ """OS-independent mixin"""
+ def make_eventqueue(self) -> base_eventqueue.BaseEventQueue:
+ raise NotImplementedError()
def test_get(self):
- eq = EventQueue(self.file.fileno(), "utf-8")
+ eq = self.make_eventqueue()
event = Event("key", "a", b"a")
eq.insert(event)
self.assertEqual(eq.get(), event)
def test_empty(self):
- eq = EventQueue(self.file.fileno(), "utf-8")
+ eq = self.make_eventqueue()
self.assertTrue(eq.empty())
eq.insert(Event("key", "a", b"a"))
self.assertFalse(eq.empty())
def test_flush_buf(self):
- eq = EventQueue(self.file.fileno(), "utf-8")
+ eq = self.make_eventqueue()
eq.buf.extend(b"test")
self.assertEqual(eq.flush_buf(), b"test")
self.assertEqual(eq.buf, bytearray())
def test_insert(self):
- eq = EventQueue(self.file.fileno(), "utf-8")
+ eq = self.make_eventqueue()
event = Event("key", "a", b"a")
eq.insert(event)
self.assertEqual(eq.events[0], event)
- @patch("_pyrepl.unix_eventqueue.keymap")
+ @patch("_pyrepl.base_eventqueue.keymap")
def test_push_with_key_in_keymap(self, mock_keymap):
mock_keymap.compile_keymap.return_value = {"a": "b"}
- eq = EventQueue(self.file.fileno(), "utf-8")
+ eq = self.make_eventqueue()
eq.keymap = {b"a": "b"}
eq.push("a")
mock_keymap.compile_keymap.assert_called()
self.assertEqual(eq.events[0].evt, "key")
self.assertEqual(eq.events[0].data, "b")
- @patch("_pyrepl.unix_eventqueue.keymap")
+ @patch("_pyrepl.base_eventqueue.keymap")
def test_push_without_key_in_keymap(self, mock_keymap):
mock_keymap.compile_keymap.return_value = {"a": "b"}
- eq = EventQueue(self.file.fileno(), "utf-8")
+ eq = self.make_eventqueue()
eq.keymap = {b"c": "d"}
eq.push("a")
mock_keymap.compile_keymap.assert_called()
self.assertEqual(eq.events[0].evt, "key")
self.assertEqual(eq.events[0].data, "a")
- @patch("_pyrepl.unix_eventqueue.keymap")
+ @patch("_pyrepl.base_eventqueue.keymap")
def test_push_with_keymap_in_keymap(self, mock_keymap):
mock_keymap.compile_keymap.return_value = {"a": "b"}
- eq = EventQueue(self.file.fileno(), "utf-8")
+ eq = self.make_eventqueue()
eq.keymap = {b"a": {b"b": "c"}}
eq.push("a")
mock_keymap.compile_keymap.assert_called()
@@ -77,10 +84,10 @@ class TestUnixEventQueue(unittest.TestCase):
self.assertEqual(eq.events[1].evt, "key")
self.assertEqual(eq.events[1].data, "d")
- @patch("_pyrepl.unix_eventqueue.keymap")
+ @patch("_pyrepl.base_eventqueue.keymap")
def test_push_with_keymap_in_keymap_and_escape(self, mock_keymap):
mock_keymap.compile_keymap.return_value = {"a": "b"}
- eq = EventQueue(self.file.fileno(), "utf-8")
+ eq = self.make_eventqueue()
eq.keymap = {b"a": {b"b": "c"}}
eq.push("a")
mock_keymap.compile_keymap.assert_called()
@@ -94,7 +101,7 @@ class TestUnixEventQueue(unittest.TestCase):
self.assertEqual(eq.events[1].data, "b")
def test_push_special_key(self):
- eq = EventQueue(self.file.fileno(), "utf-8")
+ eq = self.make_eventqueue()
eq.keymap = {}
eq.push("\x1b")
eq.push("[")
@@ -103,7 +110,7 @@ class TestUnixEventQueue(unittest.TestCase):
self.assertEqual(eq.events[0].data, "\x1b")
def test_push_unrecognized_escape_sequence(self):
- eq = EventQueue(self.file.fileno(), "utf-8")
+ eq = self.make_eventqueue()
eq.keymap = {}
eq.push("\x1b")
eq.push("[")
@@ -115,3 +122,22 @@ class TestUnixEventQueue(unittest.TestCase):
self.assertEqual(eq.events[1].data, "[")
self.assertEqual(eq.events[2].evt, "key")
self.assertEqual(eq.events[2].data, "Z")
+
+
+@unittest.skipIf(support.MS_WINDOWS, "No Unix event queue on Windows")
+class TestUnixEventQueue(EventQueueTestBase, unittest.TestCase):
+ def setUp(self):
+ self.enterContext(patch("_pyrepl.curses.tigetstr", lambda x: b""))
+ self.file = tempfile.TemporaryFile()
+
+ def tearDown(self) -> None:
+ self.file.close()
+
+ def make_eventqueue(self) -> base_eventqueue.BaseEventQueue:
+ return unix_eventqueue.EventQueue(self.file.fileno(), "utf-8")
+
+
+@unittest.skipUnless(support.MS_WINDOWS, "No Windows event queue on Unix")
+class TestWindowsEventQueue(EventQueueTestBase, unittest.TestCase):
+ def make_eventqueue(self) -> base_eventqueue.BaseEventQueue:
+ return windows_eventqueue.EventQueue("utf-8")