Annotation of pkgsrc/lang/python314/patches/patch-Lib___pyrepl_terminfo.py, revision 1.1
1.1 ! wiz 1: $NetBSD$
! 2:
! 3: Use C interface for getting information out of libterminfo on NetBSD.
! 4: Fall back to ansi for tmux-direct.
! 5: https://github.com/python/cpython/issues/140296
! 6:
! 7: --- Lib/_pyrepl/terminfo.py.orig 2026-05-10 10:21:34.000000000 +0000
! 8: +++ Lib/_pyrepl/terminfo.py
! 9: @@ -1,6 +1,8 @@ from dataclasses import dataclass, field
! 10: """Pure Python curses-like terminal capability queries."""
! 11:
! 12: from dataclasses import dataclass, field
! 13: +import ctypes
! 14: +import ctypes.util
! 15: import errno
! 16: import os
! 17: from pathlib import Path
! 18: @@ -130,6 +132,47 @@ def _validate_terminal_name_or_raise(terminal_name: st
! 19: raise ValueError("`terminal_name` cannot contain path separators")
! 20:
! 21:
! 22: +def _load_from_system_terminfo(terminal_name: str) -> dict[str, bytes] | None:
! 23: + """Try to load terminal capabilities via the system terminfo(3) library.
! 24: +
! 25: + Returns None if the library is unavailable or the terminal is unknown.
! 26: + """
! 27: + for libname in ("terminfo", "tinfo", "curses", "ncurses"):
! 28: + soname = ctypes.util.find_library(libname)
! 29: + if soname is None:
! 30: + continue
! 31: + try:
! 32: + lib = ctypes.CDLL(soname)
! 33: + except OSError:
! 34: + continue
! 35: + try:
! 36: + lib.setupterm.restype = ctypes.c_int
! 37: + lib.setupterm.argtypes = [
! 38: + ctypes.c_char_p, ctypes.c_int, ctypes.POINTER(ctypes.c_int)
! 39: + ]
! 40: + # Use c_void_p to avoid auto-dereferencing (char*)-1
! 41: + # that tigetstr returns for invalid capabilities.
! 42: + lib.tigetstr.restype = ctypes.c_void_p
! 43: + lib.tigetstr.argtypes = [ctypes.c_char_p]
! 44: + except AttributeError:
! 45: + continue
! 46: + errret = ctypes.c_int(0)
! 47: + ret = lib.setupterm(
! 48: + terminal_name.encode(), 1, ctypes.byref(errret)
! 49: + )
! 50: + if ret != 0 or errret.value != 1:
! 51: + return None
! 52: + capabilities: dict[str, bytes] = {}
! 53: + invalid = ctypes.c_void_p(-1).value
! 54: + for cap in _STRING_NAMES:
! 55: + addr = lib.tigetstr(cap.encode())
! 56: + if addr is None or addr == invalid:
! 57: + continue
! 58: + capabilities[cap] = ctypes.string_at(addr)
! 59: + return capabilities
! 60: + return None
! 61: +
! 62: +
! 63: def _read_terminfo_file(terminal_name: str) -> bytes:
! 64: """Find and read terminfo file for given terminal name.
! 65:
! 66: @@ -307,6 +350,7 @@ _TERM_ALIASES = {
! 67: "screen-256color": "ansi",
! 68: "tmux": "ansi",
! 69: "tmux-256color": "ansi",
! 70: + "tmux-direct": "ansi",
! 71: "vt100": "ansi",
! 72: "vt220": "ansi",
! 73: "rxvt": "ansi",
! 74: @@ -331,8 +375,9 @@ class TermInfo:
! 75: - ncurses/tinfo/lib_setup.c:TINFO_SETUP_TERM()
! 76:
! 77: This version first attempts to read terminfo database files like ncurses,
! 78: - then, if `fallback` is True, falls back to hardcoded capabilities for
! 79: - common terminal types.
! 80: + then tries finding information from terminfo via ctypes, and finally, if
! 81: + `fallback` is True, falls back to hardcoded capabilities for common terminal
! 82: + types.
! 83: """
! 84: # If termstr is None or empty, try to get from environment
! 85: if not self.terminal_name:
! 86: @@ -344,6 +389,11 @@ class TermInfo:
! 87: try:
! 88: self._parse_terminfo_file(self.terminal_name)
! 89: except (OSError, ValueError):
! 90: + caps = _load_from_system_terminfo(self.terminal_name)
! 91: + if caps is not None:
! 92: + self._capabilities = caps
! 93: + return
! 94: +
! 95: if not self.fallback:
! 96: raise
! 97:
CVSweb <webmaster@jp.NetBSD.org>