aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/Lib/_pyrepl/unix_console.py
diff options
context:
space:
mode:
Diffstat (limited to 'Lib/_pyrepl/unix_console.py')
-rw-r--r--Lib/_pyrepl/unix_console.py39
1 files changed, 17 insertions, 22 deletions
diff --git a/Lib/_pyrepl/unix_console.py b/Lib/_pyrepl/unix_console.py
index 96379bc20f3..d21cdd9b076 100644
--- a/Lib/_pyrepl/unix_console.py
+++ b/Lib/_pyrepl/unix_console.py
@@ -29,6 +29,7 @@ import signal
import struct
import termios
import time
+import types
import platform
from fcntl import ioctl
@@ -39,6 +40,12 @@ from .trace import trace
from .unix_eventqueue import EventQueue
from .utils import wlen
+# declare posix optional to allow None assignment on other platforms
+posix: types.ModuleType | None
+try:
+ import posix
+except ImportError:
+ posix = None
TYPE_CHECKING = False
@@ -150,8 +157,6 @@ class UnixConsole(Console):
self.pollob = poll()
self.pollob.register(self.input_fd, select.POLLIN)
- self.input_buffer = b""
- self.input_buffer_pos = 0
curses.setupterm(term or None, self.output_fd)
self.term = term
@@ -199,22 +204,14 @@ class UnixConsole(Console):
self.event_queue = EventQueue(self.input_fd, self.encoding)
self.cursor_visible = 1
- def more_in_buffer(self) -> bool:
- return bool(
- self.input_buffer
- and self.input_buffer_pos < len(self.input_buffer)
- )
+ signal.signal(signal.SIGCONT, self._sigcont_handler)
- def __read(self, n: int) -> bytes:
- if not self.more_in_buffer():
- self.input_buffer = os.read(self.input_fd, 10000)
+ def _sigcont_handler(self, signum, frame):
+ self.restore()
+ self.prepare()
- ret = self.input_buffer[self.input_buffer_pos : self.input_buffer_pos + n]
- self.input_buffer_pos += len(ret)
- if self.input_buffer_pos >= len(self.input_buffer):
- self.input_buffer = b""
- self.input_buffer_pos = 0
- return ret
+ def __read(self, n: int) -> bytes:
+ return os.read(self.input_fd, n)
def change_encoding(self, encoding: str) -> None:
@@ -422,7 +419,6 @@ class UnixConsole(Console):
"""
return (
not self.event_queue.empty()
- or self.more_in_buffer()
or bool(self.pollob.poll(timeout))
)
@@ -525,6 +521,7 @@ class UnixConsole(Console):
e.raw += e.raw
amount = struct.unpack("i", ioctl(self.input_fd, FIONREAD, b"\0\0\0\0"))[0]
+ trace("getpending({a})", a=amount)
raw = self.__read(amount)
data = str(raw, self.encoding, "replace")
e.data += data
@@ -566,11 +563,9 @@ class UnixConsole(Console):
@property
def input_hook(self):
- try:
- import posix
- except ImportError:
- return None
- if posix._is_inputhook_installed():
+ # avoid inline imports here so the repl doesn't get flooded
+ # with import logging from -X importtime=2
+ if posix is not None and posix._is_inputhook_installed():
return posix._inputhook
def __enable_bracketed_paste(self) -> None: