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

py launcher stderr is not piped to subprocess.Popen.stderr #69975

Closed
wm75 mannequin opened this issue Dec 3, 2015 · 6 comments
Closed

py launcher stderr is not piped to subprocess.Popen.stderr #69975

wm75 mannequin opened this issue Dec 3, 2015 · 6 comments
Labels
OS-windows type-bug An unexpected behavior, bug, or error

Comments

@wm75
Copy link
Mannequin

wm75 mannequin commented Dec 3, 2015

BPO 25789
Nosy @pfmoore, @vsajip, @tjguk, @zware, @eryksun, @zooba, @wm75
Files
  • issue25789_1.patch
  • issue25789_2.patch
  • 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 = None
    closed_at = <Date 2015-12-26.12:36:12.092>
    created_at = <Date 2015-12-03.17:25:52.654>
    labels = ['type-bug', 'OS-windows']
    title = 'py launcher stderr is not piped to subprocess.Popen.stderr'
    updated_at = <Date 2015-12-26.12:36:12.090>
    user = 'https://github.com/wm75'

    bugs.python.org fields:

    activity = <Date 2015-12-26.12:36:12.090>
    actor = 'python-dev'
    assignee = 'none'
    closed = True
    closed_date = <Date 2015-12-26.12:36:12.092>
    closer = 'python-dev'
    components = ['Windows']
    creation = <Date 2015-12-03.17:25:52.654>
    creator = 'wolma'
    dependencies = []
    files = ['41228', '41239']
    hgrepos = []
    issue_num = 25789
    keywords = ['patch']
    message_count = 6.0
    messages = ['255822', '255833', '255854', '255865', '255866', '257029']
    nosy_count = 8.0
    nosy_names = ['paul.moore', 'vinay.sajip', 'tim.golden', 'python-dev', 'zach.ware', 'eryksun', 'steve.dower', 'wolma']
    pr_nums = []
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue25789'
    versions = ['Python 3.6']

    @wm75
    Copy link
    Mannequin Author

    wm75 mannequin commented Dec 3, 2015

    from the console:

    py -3.7

    or any other not installed Python version gives:
    Requested Python version (3.7) not installed

    However, when the launcher is executed from python via subprocess.Popen:

    >>import subprocess
    >>p=subprocess.Popen(['py', '-3.7'], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    >>p.communicate()
    (b'', b'')

    the error message is not accessible. (Error messages from any successfully launched Python interpreter are available through p.stderr though.)

    @wm75 wm75 mannequin added OS-windows type-bug An unexpected behavior, bug, or error labels Dec 3, 2015
    @eryksun
    Copy link
    Contributor

    eryksun commented Dec 3, 2015

    The error() function in PC/launcher.c should call exit(rc) instead of ExitProcess(rc). This allows the CRT to terminate properly and flush the stderr FILE stream. With this change it works as expected:

        >>> import subprocess
        >>> p = subprocess.Popen(r'amd64\py_d -3.7', stderr=subprocess.PIPE)
        >>> p.stderr.read()
        b'Requested Python version (3.7) not installed\r\n'

    @wm75
    Copy link
    Mannequin Author

    wm75 mannequin commented Dec 4, 2015

    The error() function in PC/launcher.c should call exit(rc) instead of ExitProcess(rc). This allows the CRT to terminate properly and flush the stderr FILE stream.

    Interesting. Why do you need to flush stderr? I would have expected it to be unbuffered in the first place. What's the usecase/advantage of calling ExitProcess then?
    Sorry, if that does not really belong here and just shows my limited knowledge of C.

    @zooba
    Copy link
    Member

    zooba commented Dec 4, 2015

    ExitProcess is a system API and exit is the C runtime API. The C runtime is doing the buffering, so the system doesn't know about it and can't flush if you terminate through that API. The exit function should, or we could explicitly flush the buffer after writing to it.

    @eryksun
    Copy link
    Contributor

    eryksun commented Dec 4, 2015

    Patch 2 additionally modifies run_child to call exit() instead of ExitProcess. For example:

        >>> import os, subprocess
        >>> os.environ['PYLAUNCH_DEBUG'] = '1'
        >>> p = subprocess.Popen(r'py -3 -c ""', stderr=subprocess.PIPE, stdout=subprocess.PIPE)
        >>> p.stderr.read()
        b''

    Patched:

        >>> p = subprocess.Popen(r'amd64\py_d -3 -c ""', stderr=subprocess.PIPE, stdout=subprocess.PIPE)
        >>> p.stderr.readlines()[-1]
        b'child process exit code: 0\r\n'

    For good measure I also added a call to setvbuf to disable buffering stderr.

    @python-dev
    Copy link
    Mannequin

    python-dev mannequin commented Dec 26, 2015

    New changeset d0a84d0c5ceb by Vinay Sajip in branch 'default':
    Closes bpo-25789: Improved buffering behaviour in launcher.
    https://hg.python.org/cpython/rev/d0a84d0c5ceb

    @python-dev python-dev mannequin closed this as completed Dec 26, 2015
    @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
    OS-windows type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    2 participants