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

Popen with shell=True yield mangled repr output #87942

Closed
mpkocher mannequin opened this issue Apr 8, 2021 · 6 comments
Closed

Popen with shell=True yield mangled repr output #87942

mpkocher mannequin opened this issue Apr 8, 2021 · 6 comments
Assignees
Labels
3.9 only security fixes 3.10 only security fixes stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@mpkocher
Copy link
Mannequin

mpkocher mannequin commented Apr 8, 2021

BPO 43776
Nosy @rhettinger, @gpshead, @WildCard65, @miss-islington, @mpkocher
PRs
  • bpo-43776: Remove list call from args in Popen repr #25338
  • [3.9] bpo-43776: Remove list call from args in Popen repr (GH-25338) #26510
  • 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/gpshead'
    closed_at = <Date 2021-06-03.04:16:05.651>
    created_at = <Date 2021-04-08.18:42:23.832>
    labels = ['type-bug', 'library', '3.9', '3.10']
    title = 'Popen with shell=True yield mangled repr output'
    updated_at = <Date 2021-06-03.04:16:05.651>
    user = 'https://github.com/mpkocher'

    bugs.python.org fields:

    activity = <Date 2021-06-03.04:16:05.651>
    actor = 'gregory.p.smith'
    assignee = 'gregory.p.smith'
    closed = True
    closed_date = <Date 2021-06-03.04:16:05.651>
    closer = 'gregory.p.smith'
    components = ['Library (Lib)']
    creation = <Date 2021-04-08.18:42:23.832>
    creator = 'mkocher'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 43776
    keywords = ['patch', '3.9regression']
    message_count = 6.0
    messages = ['390542', '390579', '391751', '391757', '392171', '394983']
    nosy_count = 5.0
    nosy_names = ['rhettinger', 'gregory.p.smith', 'WildCard65', 'miss-islington', 'mkocher']
    pr_nums = ['25338', '26510']
    priority = 'normal'
    resolution = 'fixed'
    stage = 'commit review'
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue43776'
    versions = ['Python 3.9', 'Python 3.10']

    @mpkocher
    Copy link
    Mannequin Author

    mpkocher mannequin commented Apr 8, 2021

    When using Popen with shell=True, the output of the repr is not particularly user friendly.

    When using the form p = Popen('python --version'.split()), the output is reasonably output in a user friendly form of <Popen: returncode: None args: ['python', '--version']>.

    However, when running with shell=True, the output is mangled.

    For example, trying to run python --help via p = Popen('python --version', shell=True) yields the following output.

    <Popen: returncode: None args: ['p', 'y', 't', 'h', 'o', 'n', ' ', '-', '-',...>

    The original change appears to be motivated by https://bugs.python.org/issue38724

    and the PR here:

    https://github.com/python/cpython/pull/17151/files

    @mpkocher mpkocher mannequin added 3.9 only security fixes stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error labels Apr 8, 2021
    @WildCard65
    Copy link
    Mannequin

    WildCard65 mannequin commented Apr 9, 2021

    Actually, the problem is independent of the value of "shell", the __repr__ function from the initial PR that introduced it expects "args" to be a sequence and converts it to a list.

    @gpshead gpshead added the 3.10 only security fixes label Apr 11, 2021
    @gpshead gpshead self-assigned this Apr 11, 2021
    @gpshead gpshead added the 3.10 only security fixes label Apr 11, 2021
    @gpshead gpshead self-assigned this Apr 11, 2021
    @mpkocher
    Copy link
    Mannequin Author

    mpkocher mannequin commented Apr 24, 2021

    There's a PR up with removed list call as well as improved test coverage for the case where args is passed into Popen as a string.

    Hopefully this can get merged before things starting getting crazy with the 3.10 release crunch.

    @mpkocher
    Copy link
    Mannequin Author

    mpkocher mannequin commented Apr 24, 2021

    There also appears to be an issue when args are provided as a pathlib.Path instance.

    Python 3.9.2 | packaged by conda-forge | (default, Feb 21 2021, 05:02:20)
    Type 'copyright', 'credits' or 'license' for more information
    IPython 7.22.0 -- An enhanced Interactive Python. Type '?' for help.

    In [1]: from subprocess import Popen

    In [2]: from pathlib import Path

    In [3]: p = Path('/usr/local/bin/gtrue')

    In [4]: x = Popen(p)

    In [5]: repr(x)
    ---------------------------------------------------------------------------

    TypeError                                 Traceback (most recent call last)
    <ipython-input-5-5091dd558c19> in <module>
    ----> 1 repr(x)

    ~/opt/miniconda3/envs/core/lib/python3.9/subprocess.py in __repr__(self)
    990 obj_repr = (
    991 f"<{self.__class__.__name__}: "
    --> 992 f"returncode: {self.returncode} args: {list(self.args)!r}>"
    993 )
    994 if len(obj_repr) > 80:

    TypeError: 'PosixPath' object is not iterable

    @miss-islington
    Copy link
    Contributor

    New changeset db0c5b7 by M. Kocher in branch 'master':
    bpo-43776: Remove list call from args in Popen repr (GH-25338)
    db0c5b7

    @gpshead
    Copy link
    Member

    gpshead commented Jun 3, 2021

    New changeset 5a8ddcc by Gregory P. Smith in branch '3.9':
    [3.9] bpo-43776: Remove list call from args in Popen repr (GH-25338) (GH-26510)
    5a8ddcc

    @gpshead gpshead closed this as completed Jun 3, 2021
    @gpshead gpshead closed this as completed Jun 3, 2021
    @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.9 only security fixes 3.10 only security fixes stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    2 participants