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

subprocess: error redirecting i/o from non-console process #44285

Closed
orenti mannequin opened this issue Nov 27, 2006 · 6 comments
Closed

subprocess: error redirecting i/o from non-console process #44285

orenti mannequin opened this issue Nov 27, 2006 · 6 comments

Comments

@orenti
Copy link
Mannequin

orenti mannequin commented Nov 27, 2006

BPO 1603907
Files
  • subprocess-noconsole.patch: fix redirection in non-console processes
  • subprocess-noconsole2.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 2007-01-22.19:29:10.000>
    created_at = <Date 2006-11-27.17:20:38.000>
    labels = []
    title = 'subprocess: error redirecting i/o from non-console process '
    updated_at = <Date 2007-01-22.19:29:10.000>
    user = 'https://bugs.python.org/orenti'

    bugs.python.org fields:

    activity = <Date 2007-01-22.19:29:10.000>
    actor = 'astrand'
    assignee = 'astrand'
    closed = True
    closed_date = None
    closer = None
    components = ['None']
    creation = <Date 2006-11-27.17:20:38.000>
    creator = 'orenti'
    dependencies = []
    files = ['2243', '2244']
    hgrepos = []
    issue_num = 1603907
    keywords = []
    message_count = 6.0
    messages = ['30701', '30702', '30703', '30704', '30705', '30706']
    nosy_count = 2.0
    nosy_names = ['astrand', 'orenti']
    pr_nums = []
    priority = 'normal'
    resolution = 'duplicate'
    stage = None
    status = 'closed'
    superseder = None
    type = None
    url = 'https://bugs.python.org/issue1603907'
    versions = ['Python 2.5']

    @orenti
    Copy link
    Mannequin Author

    orenti mannequin commented Nov 27, 2006

    In IDLE, PythonWin or other non-console interactive Python under Windows:

    >> from subprocess import *
    >> Popen('cmd', stdout=PIPE)

    Traceback (most recent call last):
      File "<pyshell#11>", line 1, in -toplevel-
        Popen('', stdout=PIPE)
      File "C:\python24\lib\subprocess.py", line 533, in __init__
        (p2cread, p2cwrite,
      File "C:\python24\lib\subprocess.py", line 593, in _get_handles
        p2cread = self._make_inheritable(p2cread)
      File "C:\python24\lib\subprocess.py", line 634, in _make_inheritable
        DUPLICATE_SAME_ACCESS)
    TypeError: an integer is required

    The same command in a console windows is successful.

    Why it happens:
    subprocess assumes that GetStdHandle always succeeds but when there is no console it returns None. DuplicateHandle then complains about getting a non-integer. This problem does not happen when redirecting all three standard handles.

    Solution:
    Replace None with -1 (INVALID_HANDLE_VALUE) in _make_inheritable.

    Patch attached.

    @orenti orenti mannequin closed this as completed Nov 27, 2006
    @orenti orenti mannequin assigned astrand Nov 27, 2006
    @orenti orenti mannequin closed this as completed Nov 27, 2006
    @orenti orenti mannequin assigned astrand Nov 27, 2006
    @astrand
    Copy link
    Mannequin

    astrand mannequin commented Jan 7, 2007

    This patch looks very interesting. However, it feels a little bit strange to call DuplicateHandle with a handle of -1. Is this really allowed? What will DuplicateHandle return in this case? INVALID_HANDLE_VALUE? In that case, isn't it better to return INVALID_HANDLE_VALUE directly?

    @orenti
    Copy link
    Mannequin Author

    orenti mannequin commented Jan 7, 2007

    If you duplicate INVALID_HANDLE_VALUE you get a new valid handle to nothing :-) I guess the code really should not rely on this undocumented behavior. The reason I didn't return INVALID_HANDLE_VALUE directly is because DuplicateHandle returns a _subprocess_handle object, not an int. It's expected to have a .Close() method elsewhere in the code.

    Because of subtle difference between in the behavior of the _subprocess and win32api implementations of GetStdHandle in this case solving this issue this gets quite messy!
    File Added: subprocess-noconsole2.patch

    @orenti
    Copy link
    Mannequin Author

    orenti mannequin commented Jan 7, 2007

    Oops. The new patch does not solve it in all cases in the win32api version, either...

    @astrand
    Copy link
    Mannequin

    astrand mannequin commented Jan 21, 2007

    This the suggested patches are not ready for commit, I'm moving this issue to "bugs" instead.

    @astrand
    Copy link
    Mannequin

    astrand mannequin commented Jan 22, 2007

    Duplicate of 1124861.

    @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
    None yet
    Projects
    None yet
    Development

    No branches or pull requests

    0 participants