Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[EASY C][Windows] print('hello\n', end='', flush=True) raises OSError when ran with py -u #76147

Closed
GuillaumeAldebert mannequin opened this issue Nov 7, 2017 · 10 comments
Assignees
Labels
3.7 (EOL) end of life easy OS-windows topic-IO type-bug An unexpected behavior, bug, or error

Comments

@GuillaumeAldebert
Copy link
Mannequin

GuillaumeAldebert mannequin commented Nov 7, 2017

BPO 31966
Nosy @pfmoore, @vstinner, @tjguk, @zware, @serhiy-storchaka, @eryksun, @zooba, @miss-islington
PRs
  • bpo-31966: Fixed WindowsConsoleIO.write() for writing empty data. #5754
  • [3.7] bpo-31966: Fixed WindowsConsoleIO.write() for writing empty data. (GH-5754) #5852
  • [3.6] bpo-31966: Fixed WindowsConsoleIO.write() for writing empty data. (GH-5754) #5853
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = 'https://github.com/serhiy-storchaka'
    closed_at = <Date 2018-06-16.08:47:53.851>
    created_at = <Date 2017-11-07.09:40:05.730>
    labels = ['easy', 'type-bug', '3.7', 'expert-IO', 'OS-windows']
    title = "[EASY C][Windows] print('hello\\n', end='', flush=True) raises OSError when ran with py -u"
    updated_at = <Date 2018-06-16.08:47:53.850>
    user = 'https://bugs.python.org/GuillaumeAldebert'

    bugs.python.org fields:

    activity = <Date 2018-06-16.08:47:53.850>
    actor = 'serhiy.storchaka'
    assignee = 'serhiy.storchaka'
    closed = True
    closed_date = <Date 2018-06-16.08:47:53.851>
    closer = 'serhiy.storchaka'
    components = ['Windows', 'IO']
    creation = <Date 2017-11-07.09:40:05.730>
    creator = 'Guillaume Aldebert'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 31966
    keywords = ['patch', 'easy (C)']
    message_count = 10.0
    messages = ['305726', '305733', '305734', '305744', '305750', '305756', '305762', '312730', '312735', '312736']
    nosy_count = 9.0
    nosy_names = ['paul.moore', 'vstinner', 'tim.golden', 'zach.ware', 'serhiy.storchaka', 'eryksun', 'steve.dower', 'Guillaume Aldebert', 'miss-islington']
    pr_nums = ['5754', '5852', '5853']
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue31966'
    versions = ['Python 3.6', 'Python 3.7']

    @GuillaumeAldebert
    Copy link
    Mannequin Author

    GuillaumeAldebert mannequin commented Nov 7, 2017

    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

    @GuillaumeAldebert GuillaumeAldebert mannequin added 3.7 (EOL) end of life OS-windows type-bug An unexpected behavior, bug, or error labels Nov 7, 2017
    @serhiy-storchaka
    Copy link
    Member

    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

    @vstinner
    Copy link
    Member

    vstinner commented Nov 7, 2017

    Once the bug will be fixed, it would be nice to test this simple case :-)

    @vstinner
    Copy link
    Member

    vstinner commented Nov 7, 2017

    serhiy.storchaka: "keywords: + easy (C)"

    For easy issue, you should explain how do you want the issue to be fixed.

    @serhiy-storchaka
    Copy link
    Member

    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.

    @eryksun
    Copy link
    Contributor

    eryksun commented Nov 7, 2017

    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.

    @vstinner
    Copy link
    Member

    vstinner commented Nov 7, 2017

    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 ;-)

    @vstinner vstinner changed the 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 Nov 7, 2017
    @serhiy-storchaka serhiy-storchaka self-assigned this Feb 19, 2018
    @serhiy-storchaka
    Copy link
    Member

    New changeset 42c35d9 by Serhiy Storchaka in branch 'master':
    bpo-31966: Fixed WindowsConsoleIO.write() for writing empty data. (GH-5754)
    42c35d9

    @miss-islington
    Copy link
    Contributor

    New changeset e49bf0f by Miss Islington (bot) in branch '3.7':
    bpo-31966: Fixed WindowsConsoleIO.write() for writing empty data. (GH-5754)
    e49bf0f

    @miss-islington
    Copy link
    Contributor

    New changeset 980790e by Miss Islington (bot) in branch '3.6':
    bpo-31966: Fixed WindowsConsoleIO.write() for writing empty data. (GH-5754)
    980790e

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    3.7 (EOL) end of life easy OS-windows topic-IO type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    4 participants