classification
Title: _io._WindowsConsoleIO.write raises the wrong error when WriteConsoleW fails
Type: behavior Stage: resolved
Components: IO, Windows Versions: Python 3.7, Python 3.6
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: Segev Finer, eryksun, paul.moore, steve.dower, tim.golden, zach.ware
Priority: normal Keywords: 3.2regression

Created on 2017-06-01 22:57 by Segev Finer, last changed 2017-06-02 21:40 by steve.dower. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 1912 merged Segev Finer, 2017-06-01 23:02
PR 1925 merged steve.dower, 2017-06-02 21:14
Messages (6)
msg294975 - (view) Author: Segev Finer (Segev Finer) * Date: 2017-06-01 22:57
Found due to this: https://github.com/pytest-dev/py/issues/103

I'm going to submit a PR.
msg294988 - (view) Author: Eryk Sun (eryksun) * (Python triager) Date: 2017-06-02 08:11
The following is an example of the problem, right?

    >>> fd = os.open('stdout.txt', os.O_CREAT | os.O_WRONLY)
    >>> os.dup2(fd, 1)
    >>> print('spam')
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    OSError: [WinError 87] The parameter is incorrect

Code like this used to work, so possibly this needs to be addressed more generally. I have a couple ideas, but they're major changes. It's simpler to require code that pulls the rug out from under _WindowsConsoleIO to rebind sys.std*. 

For a counterexample, PyOS_StdioReadline always checks for a console and falls back on the old my_fgets function for a non-console. For example:

    >>> open('stdin.txt', 'w').write('x=42; os.dup2(oldfd, 0)\n')
    24
    >>> fd = os.open('stdin.txt', os.O_RDONLY)
    >>> oldfd = os.dup(0); os.dup2(fd, 0)
    >>> Breakpoint 0 hit
    python36!my_fgets:
    00000000`66e98318 488bc4          mov     rax,rsp
    0:000> g
    >>> x
    42
msg294998 - (view) Author: Segev Finer (Segev Finer) * Date: 2017-06-02 09:14
We might want to open a separate issue for the FD redirection issue since this one was really about the _io._WindowsConsoleIO.write GetLastError() issue.

Also see https://github.com/pytest-dev/pytest/pull/2462 where I discuss this more.

Still not sure why it's random in Pytest. I can only guess it's due to another console handle getting opened with the same number or something.
msg295033 - (view) Author: Steve Dower (steve.dower) * (Python committer) Date: 2017-06-02 16:26
New changeset 523776c3419f6795e78173d53c10e35ec4eed48d by Steve Dower (Segev Finer) in branch 'master':
bpo-30544: _io._WindowsConsoleIO.write raises the wrong error when WriteConsoleW fails (#1912)
https://github.com/python/cpython/commit/523776c3419f6795e78173d53c10e35ec4eed48d
msg295034 - (view) Author: Steve Dower (steve.dower) * (Python committer) Date: 2017-06-02 16:28
Yeah, make it a separate issue.

This PR fixes an obvious error that we should just fix, so I've merged it. (I might get to the backport today, but if someone else submits it then I'll merge.)
msg295056 - (view) Author: Steve Dower (steve.dower) * (Python committer) Date: 2017-06-02 21:39
New changeset c63ae1122f84d4188ffadfd1454902093eb10be1 by Steve Dower in branch '3.6':
bpo-30544: _io._WindowsConsoleIO.write raises the wrong error when WriteConsoleW fails (#1912) (#1925)
https://github.com/python/cpython/commit/c63ae1122f84d4188ffadfd1454902093eb10be1
History
Date User Action Args
2017-06-02 21:40:51steve.dowersetstatus: open -> closed
resolution: fixed
stage: resolved
2017-06-02 21:39:07steve.dowersetmessages: + msg295056
2017-06-02 21:14:44steve.dowersetpull_requests: + pull_request2005
2017-06-02 17:00:34Segev Finersettype: behavior
2017-06-02 16:28:36steve.dowersetmessages: + msg295034
2017-06-02 16:26:03steve.dowersetmessages: + msg295033
2017-06-02 09:14:16Segev Finersetmessages: + msg294998
2017-06-02 08:11:17eryksunsetkeywords: + 3.2regression
nosy: + eryksun
messages: + msg294988

2017-06-01 23:02:06Segev Finersetpull_requests: + pull_request1992
2017-06-01 22:57:43Segev Finercreate