On 11Dec2022 22:22, Barry Scott <ba...@barrys-emacs.org> wrote:
# Get a single character, setcbreak rather than setraw meands CTRL/C
   etc. still work
   #
   def getch():
       sys.stdout.flush()
       tty.setcbreak(fdInput)
       ch = sys.stdin.buffer.raw.read(1).decode(sys.stdin.encoding)

Will not work for uncode code points above 255.

Aye. But one could write a little loop to collect bytes until a complete character was received. A little experiment:

    >>> try: bytes((0xf0,)).decode('utf8')
    ... except UnicodeDecodeError as e:
    ...   e2=e
    ...
    >>> e2
    UnicodeDecodeError('utf-8', b'\xf0', 0, 1, 'unexpected end of data')
    >>> e2.reason
    'unexpected end of data'

Keep collecting while you get `UnicodeDecodeError`s with a `.reason` of 'unexpected end of data'. Can be encoding agnostic (obviously you need to _choose_ an encoding, it is needn't be utf-8).

(For the OP: `UnicodeDecodeError` doesn't necessarily mean you're decoding Unicode data, you're decoding _into_ a Python string which is a Unicode string.)

Cheers,
Cameron Simpson <c...@cskk.id.au>
--
https://mail.python.org/mailman/listinfo/python-list

Reply via email to