Issue 31966: [EASY C][Windows] print('hello\n', end='', flush=True) raises OSError when ran with py -u (original) (raw)

Created on 2017-11-07 09:40 by Guillaume Aldebert, last changed 2022-04-11 14:58 by admin. 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 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 "", line 1, in 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
2022-04-11 14:58:54 admin set github: 76147
2018-06-16 08:47:53 serhiy.storchaka set status: open -> closedresolution: fixedstage: patch review -> resolved
2018-02-24 17:43:07 miss-islington set messages: +
2018-02-24 17:39:20 miss-islington set nosy: + miss-islingtonmessages: +
2018-02-24 16:58:02 miss-islington set pull_requests: + <pull%5Frequest5628>
2018-02-24 16:57:03 miss-islington set pull_requests: + <pull%5Frequest5627>
2018-02-24 16:55:53 serhiy.storchaka set messages: +
2018-02-19 11:56:18 serhiy.storchaka set keywords: + patchstage: needs patch -> patch reviewpull_requests: + <pull%5Frequest5531>
2018-02-19 11:09:05 serhiy.storchaka set assignee: serhiy.storchaka
2017-11-07 14:43:02 vstinner set messages: + 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:07 eryksun set nosy: + eryksunmessages: +
2017-11-07 13:04:56 serhiy.storchaka set messages: +
2017-11-07 12:35:08 vstinner set messages: +
2017-11-07 10:17:15 serhiy.storchaka set keywords: + easy (C)stage: needs patch
2017-11-07 10:13:49 vstinner set nosy: + vstinnermessages: +
2017-11-07 10:12:35 serhiy.storchaka set messages: +
2017-11-07 09:59:03 serhiy.storchaka set nosy: + serhiy.storchakacomponents: + IO
2017-11-07 09:40:05 Guillaume Aldebert create