posix_utils.py 3.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. from __future__ import annotations
  2. import os
  3. import select
  4. from codecs import getincrementaldecoder
  5. __all__ = [
  6. "PosixStdinReader",
  7. ]
  8. class PosixStdinReader:
  9. """
  10. Wrapper around stdin which reads (nonblocking) the next available 1024
  11. bytes and decodes it.
  12. Note that you can't be sure that the input file is closed if the ``read``
  13. function returns an empty string. When ``errors=ignore`` is passed,
  14. ``read`` can return an empty string if all malformed input was replaced by
  15. an empty string. (We can't block here and wait for more input.) So, because
  16. of that, check the ``closed`` attribute, to be sure that the file has been
  17. closed.
  18. :param stdin_fd: File descriptor from which we read.
  19. :param errors: Can be 'ignore', 'strict' or 'replace'.
  20. On Python3, this can be 'surrogateescape', which is the default.
  21. 'surrogateescape' is preferred, because this allows us to transfer
  22. unrecognized bytes to the key bindings. Some terminals, like lxterminal
  23. and Guake, use the 'Mxx' notation to send mouse events, where each 'x'
  24. can be any possible byte.
  25. """
  26. # By default, we want to 'ignore' errors here. The input stream can be full
  27. # of junk. One occurrence of this that I had was when using iTerm2 on OS X,
  28. # with "Option as Meta" checked (You should choose "Option as +Esc".)
  29. def __init__(
  30. self, stdin_fd: int, errors: str = "surrogateescape", encoding: str = "utf-8"
  31. ) -> None:
  32. self.stdin_fd = stdin_fd
  33. self.errors = errors
  34. # Create incremental decoder for decoding stdin.
  35. # We can not just do `os.read(stdin.fileno(), 1024).decode('utf-8')`, because
  36. # it could be that we are in the middle of a utf-8 byte sequence.
  37. self._stdin_decoder_cls = getincrementaldecoder(encoding)
  38. self._stdin_decoder = self._stdin_decoder_cls(errors=errors)
  39. #: True when there is nothing anymore to read.
  40. self.closed = False
  41. def read(self, count: int = 1024) -> str:
  42. # By default we choose a rather small chunk size, because reading
  43. # big amounts of input at once, causes the event loop to process
  44. # all these key bindings also at once without going back to the
  45. # loop. This will make the application feel unresponsive.
  46. """
  47. Read the input and return it as a string.
  48. Return the text. Note that this can return an empty string, even when
  49. the input stream was not yet closed. This means that something went
  50. wrong during the decoding.
  51. """
  52. if self.closed:
  53. return ""
  54. # Check whether there is some input to read. `os.read` would block
  55. # otherwise.
  56. # (Actually, the event loop is responsible to make sure that this
  57. # function is only called when there is something to read, but for some
  58. # reason this happens in certain situations.)
  59. try:
  60. if not select.select([self.stdin_fd], [], [], 0)[0]:
  61. return ""
  62. except OSError:
  63. # Happens for instance when the file descriptor was closed.
  64. # (We had this in ptterm, where the FD became ready, a callback was
  65. # scheduled, but in the meantime another callback closed it already.)
  66. self.closed = True
  67. # Note: the following works better than wrapping `self.stdin` like
  68. # `codecs.getreader('utf-8')(stdin)` and doing `read(1)`.
  69. # Somehow that causes some latency when the escape
  70. # character is pressed. (Especially on combination with the `select`.)
  71. try:
  72. data = os.read(self.stdin_fd, count)
  73. # Nothing more to read, stream is closed.
  74. if data == b"":
  75. self.closed = True
  76. return ""
  77. except OSError:
  78. # In case of SIGWINCH
  79. data = b""
  80. return self._stdin_decoder.decode(data)