This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author eryksun
Recipients David.Sankel, Drekin, Jonitis, akira, amaury.forgeotdarc, berker.peksag, christoph, davidsarah, dead1ne, eryksun, escapewindow, ezio.melotti, flox, giampaolo.rodola, gurnec, hippietrail, lemburg, lilydjwg, mark, martin.panter, mhammond, ncoghlan, ned.deily, paul.moore, piotr.dobrogost, pitrou, python-dev, santoso.wijaya, smerlin, steve.dower, stijn, terry.reedy, tim.golden, tzot, v+python, wiz21
Date 2016-09-09.18:00:27
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1473444028.03.0.0848176343777.issue1602@psf.upfronthosting.co.za>
In-reply-to
Content
Martin, the console should be in line-input mode, in which case ReadConsole will block if there isn't at least one line in the input buffer. It reads up to the lesser of a complete line or the number of UTF-16 codes requested. If the previous call read the entire request size but didn't stop on '\n', then we know the next call shouldn't block because the input buffer has at least one '\n' in it.

> I can validate that we can open the console IO object from 
> 0, 1, 2, "CON", "CONIN$" and "CONOUT$", get fileno(), check
> readable()/writable() and close (multiple times without 
> crashing).

I like the idea to have fileno() lazily get a file descriptor on demand, but _open_osfhandle is a low I/O function that uses _open flags -- not 'rb' (int 0x7262) or 'wb' (int 0x7762). ;-)

You can use _O_RDONLY | _O_BINARY or _O_WRONLY | _O_BINARY. But really those values would be ignored anyway. It's not actually opening the file, so it only cares about a few flags. Specifically, in lowio\osfinfo.cpp I see that it looks for _O_APPEND, _O_TEXT, and _O_NOINHERIT. 

On line 329, the following assignment

        if (self->writable)
            access |= GENERIC_WRITE;

should be `access = GENERIC_WRITE`. Requesting both read and write access is an invalid parameter when opening "CON", as can be seen here:

    >>> f = open('CON', 'wb', buffering=0)
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    OSError: [WinError 87] The parameter is incorrect: 'CON'

CONOUT$ works, of course:

    >>> f = open('CONOUT$', 'wb', buffering=0)
    >>> f
    <_io._WindowsConsoleIO mode='wb' closefd=True>

Lastly, for a readall that starts with ^Z, you're still breaking out of the loop before incrementing len, which is thus 0 when subsequently checked. It ends up calling WideCharToMultiByte with len == 0, which fails.

    >>> sys.stdin.buffer.raw.read()
    ^Z
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    OSError: [WinError 87] The parameter is incorrect

> I can't actually come up with many useful tests for this... 

ctypes can be used to write to the input buffer and read from a screen buffer. For the latter it helps to first create and activate a scratch screen buffer, initialized to NULs to make it easy to read back everything that was written up to the current cursor position. I have existing ctypes code for this, written to solve the problem of a subprocess that stubbornly writes directly to the console instead of writing to stdout/stderr pipes.
History
Date User Action Args
2016-09-09 18:00:28eryksunsetrecipients: + eryksun, lemburg, mhammond, terry.reedy, paul.moore, tzot, amaury.forgeotdarc, ncoghlan, pitrou, giampaolo.rodola, tim.golden, mark, ned.deily, christoph, ezio.melotti, v+python, hippietrail, flox, davidsarah, santoso.wijaya, akira, David.Sankel, python-dev, smerlin, lilydjwg, berker.peksag, martin.panter, piotr.dobrogost, Drekin, steve.dower, wiz21, stijn, Jonitis, gurnec, escapewindow, dead1ne
2016-09-09 18:00:28eryksunsetmessageid: <1473444028.03.0.0848176343777.issue1602@psf.upfronthosting.co.za>
2016-09-09 18:00:28eryksunlinkissue1602 messages
2016-09-09 18:00:27eryksuncreate