gh-135621: Remove dependency on curses from PyREPL (GH-136758)

This commit is contained in:
Łukasz Langa 2025-07-21 11:57:34 +02:00 committed by GitHub
parent d1d526afe7
commit 09dfb50f1b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
11 changed files with 1229 additions and 160 deletions

View file

@ -18,7 +18,7 @@
# CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
# CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
from . import curses
from .terminfo import TermInfo
from .trace import trace
from .base_eventqueue import BaseEventQueue
from termios import tcgetattr, VERASE
@ -54,22 +54,23 @@
b'\033Oc': 'ctrl right',
}
def get_terminal_keycodes() -> dict[bytes, str]:
def get_terminal_keycodes(ti: TermInfo) -> dict[bytes, str]:
"""
Generates a dictionary mapping terminal keycodes to human-readable names.
"""
keycodes = {}
for key, terminal_code in TERMINAL_KEYNAMES.items():
keycode = curses.tigetstr(terminal_code)
keycode = ti.get(terminal_code)
trace('key {key} tiname {terminal_code} keycode {keycode!r}', **locals())
if keycode:
keycodes[keycode] = key
keycodes.update(CTRL_ARROW_KEYCODES)
return keycodes
class EventQueue(BaseEventQueue):
def __init__(self, fd: int, encoding: str) -> None:
keycodes = get_terminal_keycodes()
def __init__(self, fd: int, encoding: str, ti: TermInfo) -> None:
keycodes = get_terminal_keycodes(ti)
if os.isatty(fd):
backspace = tcgetattr(fd)[6][VERASE]
keycodes[backspace] = "backspace"