classification
Title: [EASY C][Windows] print('hello\n', end='', flush=True) raises OSError when ran with py -u
Type: behavior Stage: resolved
Components: IO, Windows Versions: Python 3.7, Python 3.6
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: serhiy.storchaka Nosy List: Guillaume Aldebert, eryksun, miss-islington, paul.moore, serhiy.storchaka, steve.dower, tim.golden, vstinner, zach.ware
Priority: normal Keywords: easy (C), patch

Created on 2017-11-07 09:40 by Guillaume Aldebert, last changed 2018-06-16 08:47 by serhiy.storchaka. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 5754 merged serhiy.storchaka, 2018-02-19 11:56
PR 5852 merged miss-islington, 2018-02-24 16:57
PR 5853 merged miss-islington, 2018-02-24 16:58
Messages (10)
msg305726 - (view) Author: Guillaume Aldebert (Guillaume Aldebert) Date: 2017-11-07 09:40
noticed on windows10, 3.6.3-64 and 3.7.0a2-64:

using this test.py file:

#!/usr/bin/env python3
print('hello\n', end='', flush=True)

and running it in unbuffered mode:

C:\Dev>py -u test.py
hello
Traceback (most recent call last):
  File "test.py", line 2, in <module>
    print('hello\n', end='', flush=True)
OSError: [WinError 87] The parameter is incorrect

Note that (still using py -u):

print('hello', end='', flush=True) # works fine
print('', end='', flush=True) # raises OSError as well
msg305733 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2017-11-07 10:12
The problem is with _WindowsConsoleIO.

C:\py\cpython>python.bat -u -c "import sys; sys.stdout.buffer.write(b'')"
Running Release|Win32 interpreter...
Traceback (most recent call last):
  File "<string>", line 1, in <module>
OSError: [WinError 87] The parameter is incorrect
msg305734 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2017-11-07 10:13
Once the bug will be fixed, it would be nice to test this simple case :-)
msg305744 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2017-11-07 12:35
serhiy.storchaka: "keywords: + easy (C)"

For easy issue, you should explain how do you want the issue to be fixed.
msg305750 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2017-11-07 13:04
I don't know which part of _WindowsConsoleIO.write() fails to handle empty bytes string, but the simplest and the most efficient way to fix this bug it to add an explicit check for zero length at the begin of this method and return Python integer 0 in this case.

The test should check that sys.stdout.buffer.write(b''), sys.stdout.buffer.write(b'') and sys.stdout.buffer.raw.write(b'') return 0 (the latter to checks should be performed only if the corresponding buffer and raw attributes exist). There are special tests for WindowsConsoleIO, it would be nice to add an explicit test here too.
msg305756 - (view) Author: Eryk Sun (eryksun) * (Python triager) Date: 2017-11-07 13:29
The error is in _io__WindowsConsoleIO_write_impl. If it's passed a length 0 buffer, it still tries to decode it via MultiByteToWideChar, which fails as documented. As Serhiy says, it can simply return Python int(0) in the zero-length case.
msg305762 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2017-11-07 14:43
The _io__WindowsConsoleIO_write_impl() function should be fixed to not call MultiByteToWideChar() but use 0 if the input string is zero. Ok, it makes sense.

In that case, I agree to call it a simple issue ;-)
msg312730 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2018-02-24 16:55
New changeset 42c35d9c0c8175332f50fbe034a001fe52f057b9 by Serhiy Storchaka in branch 'master':
bpo-31966: Fixed WindowsConsoleIO.write() for writing empty data. (GH-5754)
https://github.com/python/cpython/commit/42c35d9c0c8175332f50fbe034a001fe52f057b9
msg312735 - (view) Author: miss-islington (miss-islington) Date: 2018-02-24 17:39
New changeset e49bf0f353a968cddc4d8e6ea668b9d2d116e2ac by Miss Islington (bot) in branch '3.7':
bpo-31966: Fixed WindowsConsoleIO.write() for writing empty data. (GH-5754)
https://github.com/python/cpython/commit/e49bf0f353a968cddc4d8e6ea668b9d2d116e2ac
msg312736 - (view) Author: miss-islington (miss-islington) Date: 2018-02-24 17:43
New changeset 980790eee0c804061a49b8ad7373e4669b48f2ec by Miss Islington (bot) in branch '3.6':
bpo-31966: Fixed WindowsConsoleIO.write() for writing empty data. (GH-5754)
https://github.com/python/cpython/commit/980790eee0c804061a49b8ad7373e4669b48f2ec
History
Date User Action Args
2018-06-16 08:47:53serhiy.storchakasetstatus: open -> closed
resolution: fixed
stage: patch review -> resolved
2018-02-24 17:43:07miss-islingtonsetmessages: + msg312736
2018-02-24 17:39:20miss-islingtonsetnosy: + miss-islington
messages: + msg312735
2018-02-24 16:58:02miss-islingtonsetpull_requests: + pull_request5628
2018-02-24 16:57:03miss-islingtonsetpull_requests: + pull_request5627
2018-02-24 16:55:53serhiy.storchakasetmessages: + msg312730
2018-02-19 11:56:18serhiy.storchakasetkeywords: + patch
stage: needs patch -> patch review
pull_requests: + pull_request5531
2018-02-19 11:09:05serhiy.storchakasetassignee: serhiy.storchaka
2017-11-07 14:43:02vstinnersetmessages: + msg305762
title: print('hello\n', end='', flush=True) raises OSError when ran with py -u -> [EASY C][Windows] print('hello\n', end='', flush=True) raises OSError when ran with py -u
2017-11-07 13:29:07eryksunsetnosy: + eryksun
messages: + msg305756
2017-11-07 13:04:56serhiy.storchakasetmessages: + msg305750
2017-11-07 12:35:08vstinnersetmessages: + msg305744
2017-11-07 10:17:15serhiy.storchakasetkeywords: + easy (C)
stage: needs patch
2017-11-07 10:13:49vstinnersetnosy: + vstinner
messages: + msg305734
2017-11-07 10:12:35serhiy.storchakasetmessages: + msg305733
2017-11-07 09:59:03serhiy.storchakasetnosy: + serhiy.storchaka
components: + IO
2017-11-07 09:40:05Guillaume Aldebertcreate