Eryk Sun <eryk...@gmail.com> added the comment:

> The biggest risk here is that we have to emulate GNU readline for 
> compatibility, which severely limits the data that can be passed 
> through, and also forces multiple encoding/decoding passes. 

I'm not suggesting to disable the console's line-input and echo-input modes and 
implement our own line editor. If people don't like the built-in line editor 
that the console provides, they can use pyreadline, which directly uses the 
console's low-level API via ctypes.

Here's an example of what I would like to just work by default in Python:

    import sys
    import win32console

    def write_input(h, s):
         records = []
         for c in s:
             b = c.encode('utf-16le')
             for i in range(0, len(b), 2):
                 r = win32console.PyINPUT_RECORDType(win32console.KEY_EVENT)
                 r.KeyDown = True
                 r.RepeatCount = 1
                 r.Char = b[i:i+2].decode('utf-16le', 'surrogatepass')
                 records.append(r)
         h.WriteConsoleInput(records)

    def write_and_read_line(s):
        if '\r' not in s:
            s += '\r'
        h = win32console.GetStdHandle(win32console.STD_INPUT_HANDLE)
        mode = h.GetConsoleMode()
        h.SetConsoleMode(mode & ~win32console.ENABLE_ECHO_INPUT)
        try:
            write_input(h, s)
            line = sys.stdin.readline()
        finally:
            h.SetConsoleMode(mode)
        return line

    >>> src_line = 'a' * 32765 + '\r'
    >>> res_line = write_and_read_line(src_line)
    >>> assert res_line == src_line.replace('\r', '\n')
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    AssertionError
    >>> len(res_line)
    511
    >>> res_line[:5], res_line[-5:]
    ('aaaaa', 'aaaa\n')


Currently the ReadConsoleW buffer in read_console_w is capped at 512 (BUFSIZ) 
characters. With the console's processed-input mode enabled, it writes a 
trailing CRLF instead of the raw CR. So the user is limited to typing or 
pasting just 510 characters on a single line. 

I was only thinking to increase the default maximum size up to 32K in 
read_console_w -- removing the fixed BUFSIZ aspect of the implementation in 
favor of capping the buffer used at BUFMAX. In practice this also requires 
similar default increases for the BufferedReader size and TextIOWrapper chunk 
size.

----------

_______________________________________
Python tracker <rep...@bugs.python.org>
<https://bugs.python.org/issue41849>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to