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

patch to subprocess docs to better explain Popen's 'args' argument #51009

Closed
cvrebert mannequin opened this issue Aug 22, 2009 · 33 comments
Closed

patch to subprocess docs to better explain Popen's 'args' argument #51009

cvrebert mannequin opened this issue Aug 22, 2009 · 33 comments
Assignees
Labels
docs Documentation in the Doc dir type-feature A feature request or enhancement

Comments

@cvrebert
Copy link
Mannequin

cvrebert mannequin commented Aug 22, 2009

BPO 6760
Nosy @birkenfeld, @terryjreedy, @ncoghlan, @pitrou, @ericvsmith, @benjaminp, @bitdancer, @briancurtin
Files
  • subprocess-doc.patch
  • subprocess-doc.patch
  • subprocess.rst.patch: tweak shell transcript
  • 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/birkenfeld'
    closed_at = <Date 2010-02-04.16:49:42.301>
    created_at = <Date 2009-08-22.10:40:31.894>
    labels = ['type-feature', 'docs']
    title = "patch to subprocess docs to better explain Popen's 'args' argument"
    updated_at = <Date 2010-02-18.14:28:48.871>
    user = 'https://bugs.python.org/cvrebert'

    bugs.python.org fields:

    activity = <Date 2010-02-18.14:28:48.871>
    actor = 'christoph.neuroth'
    assignee = 'georg.brandl'
    closed = True
    closed_date = <Date 2010-02-04.16:49:42.301>
    closer = 'r.david.murray'
    components = ['Documentation']
    creation = <Date 2009-08-22.10:40:31.894>
    creator = 'cvrebert'
    dependencies = []
    files = ['16102', '16107', '16128']
    hgrepos = []
    issue_num = 6760
    keywords = ['patch', 'needs review']
    message_count = 33.0
    messages = ['91859', '92688', '93519', '98658', '98683', '98684', '98707', '98711', '98713', '98719', '98729', '98731', '98732', '98733', '98734', '98735', '98736', '98738', '98739', '98742', '98745', '98746', '98747', '98748', '98826', '98827', '98832', '98836', '98840', '98841', '98880', '98881', '99508']
    nosy_count = 10.0
    nosy_names = ['georg.brandl', 'terry.reedy', 'ncoghlan', 'pitrou', 'eric.smith', 'benjamin.peterson', 'r.david.murray', 'brian.curtin', 'cvrebert', 'christoph.neuroth']
    pr_nums = []
    priority = 'normal'
    resolution = 'accepted'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'enhancement'
    url = 'https://bugs.python.org/issue6760'
    versions = ['Python 2.6', 'Python 3.1', 'Python 2.7', 'Python 3.2']

    @cvrebert
    Copy link
    Mannequin Author

    cvrebert mannequin commented Aug 22, 2009

    From what I've seen on several c.l.p threads, some people have a tough
    time figuring the correct 'args' argument to subprocess.Popen's
    constructor. In an effort to cut down on such discussions in the future,
    I've written the attached docs patch to better explain the subject.

    I'm not an rst/Sphinx guru and thus was unable to actually test the
    patch, so there might be some (hopefully minor) formatting errors.

    @cvrebert cvrebert mannequin assigned birkenfeld Aug 22, 2009
    @cvrebert cvrebert mannequin added docs Documentation in the Doc dir type-feature A feature request or enhancement labels Aug 22, 2009
    @benjaminp
    Copy link
    Contributor

    We are trying to cut down on the number of "warning" directives in the
    docs; I think a "note" directive would be appropriate for yours.

    @cvrebert
    Copy link
    Mannequin Author

    cvrebert mannequin commented Oct 4, 2009

    Ok, changed to "note" directives instead of "warning"s. Anything else
    that keeps this from being applied?

    @pitrou
    Copy link
    Member

    pitrou commented Feb 1, 2010

    This is a particularly verbose patch for the information it is trying to convey.

    @terryjreedy
    Copy link
    Member

    The note starts out "Do not (do x) unless you're not (doing y)". This is confusing. I believe this means "If you are (doing y), you may (do x)". If so, please write it so. If not, please write what you do mean.

    After thinking about it, "Only (do x) if you are (doing y)" might be a better rewrite. Or "You may (do x) only if you are (doing y)"

    Change "be mindful of escaping" to the more direct "escape". Perhaps give a list of possible special characters.

    I agree that this seems a bit verbose for reference documentation. Perhaps there should be a separate PyWiki entry or HowTo doc. That could, for instance, list the special characters for various shells if they are not all the same.

    @terryjreedy
    Copy link
    Member

    PS. I only looked at the style of the patch. Once that is clearer, someone who knows more than me needs to review it for content correctness.

    @cvrebert
    Copy link
    Mannequin Author

    cvrebert mannequin commented Feb 2, 2010

    Working on a more concise new draft...

    @cvrebert
    Copy link
    Mannequin Author

    cvrebert mannequin commented Feb 2, 2010

    Okay; new, hopefully better, draft. Feedback?

    @briancurtin
    Copy link
    Member

    The raw_input() doesn't provide anything. I'd just drop that and pass the string directly to shlex.split.

    "Do not put an argument-taking option together with its argument as a single item in the *args* list"
    -- Something like "Argument-taking options should not be grouped with their arguments in the *args* list." may be a cleaner read, and avoids the big scary "do not".

    "Because that is incorrect"
    -- Maybe something like "shlex.split would suggest the following" instead?

    @cvrebert
    Copy link
    Mannequin Author

    cvrebert mannequin commented Feb 2, 2010

    Gonna have to disagree about the raw_input(), because the escaping involved would complicate the example and could be distracting/confusing.
    Rest of Brian's suggestions taken into account.

    @pitrou
    Copy link
    Member

    pitrou commented Feb 2, 2010

    I still think this is much too verbose. The second note looks redundant with the first one and the third one.
    Remember, the notes you are adding may be useful, but they'll also make reading the whole page more tedious.

    @cvrebert
    Copy link
    Mannequin Author

    cvrebert mannequin commented Feb 2, 2010

    Well, the same basic example is used for cohesiveness, but the issue/pitfall being highlighted in each note is distinct. But you have a point about shlex being pointed out twice, so here's a version with that redundancy excised.

    @bitdancer
    Copy link
    Member

    I like the idea of pointing out that shlex can be used to determine exactly what to pass to subprocess, but I agree that the proposed patch is too wordy (and still too much in a negative voice).

    Here is an alternate simpler patch.

    Note that while I have also clarified the last sentence in the shell=True case, I think that this is in fact a bug in the Popen API. As far as I can tell the ability to pass arguments to the shell after the -c is useless, and I think Popen should instead raise a ValueError if passed a sequence when shell is True. But that is a different bug report....

    @bitdancer
    Copy link
    Member

    Hmm. Somehow the patch got lost. Let's try again.

    @bitdancer
    Copy link
    Member

    Woops, spotted a word I left out. Fixed.

    @ericvsmith
    Copy link
    Member

    From David's patch:

    Note in particular that options and their arguments go in separate list
    elements, while arguments that need quoting when used in the shell
    (such as filenames containing spaces or the python command shown
    above) are single list elements.

    It's not always true that options and their arguments should be in separate elements. For example:
    ['/bin/ls', '-l', '--block-size=1024']
    I understand the sentiment, but if we're trying to give guidance that people can follow without understanding the underlying issue, then they should be correct all the time.

    Also, the code snippet includes subprocess without using it.

    @cvrebert
    Copy link
    Mannequin Author

    cvrebert mannequin commented Feb 2, 2010

    Counterpatch incorporating R. David Murray's succinctness improvements while retaining correct positioning of the first note, managing to incorporate the 3rd note not present in Mr. Murray's, and including more precise wording to address the problem Eric Smith pointed out.

    @bitdancer
    Copy link
    Member

    My placement of the note was carefully considered. It is discussing the shell=False case and IMO belongs after that paragraph. I understand now your concern about the note I omitted...and again I think this is a bug in the Popen API. If shell=False, I think Popen should generate an error if args is a string. Absent that fix, I've reworded the existing sentence along the lines you suggest, but using positive language rather than negative. I've incorporated your fix for Eric's bug report, but I haven't added in the explicit references to the strings in the example. IMO those are obvious and don't need repeating, but if others think it is clearer to add them, I'm fine with that.

    As for subprocess, IMO there's no need to show the subprocess call. The shlex result shows the list that would go into the subprocess call, and I think that's sufficient. What do others think?

    I've updated my version of the patch.

    @bitdancer
    Copy link
    Member

    By the way, I've been wanting the Popen docs improved for a long time but never got around to it, so thanks for pushing for this.

    @pitrou
    Copy link
    Member

    pitrou commented Feb 2, 2010

    The following sentences look like a distraction to me:

    « Additional quoting may be required because the entire string is a Python string. It may be useful to use a Python raw string in complex cases. »

    It is true of every string literal and is not specific to the subprocess module.

    Also, I don't understand what the reference to "-c" is about. subprocess isn't only for executing Python interpreters. Or perhaps you are talking about "sh -c", but it is quite cryptic.

    @bitdancer
    Copy link
    Member

    I'm happy to delete the two sentences about quoting.

    As for -c, you are so right that it is cryptic. In the new version of the patch I've changed the sentence to be as precise as possible, but I'm not at all convinced that it is less confusing. This is why I think the API should be changed.

    @cvrebert
    Copy link
    Mannequin Author

    cvrebert mannequin commented Feb 2, 2010

    This version takes Murray's most recent draft, applies some minor tweaks from my prior patch, and has the "python -c" etc. changed to "echo '$MONEY'" so the sh -c comment is completely unambiguous (and it's a simpler example generally).

    Whether the subprocess.Popen() call should be demonstrated in the code snippet remains an open issue.

    @bitdancer
    Copy link
    Member

    The change to echo is an excellent improvement. You forgot to change 'python' to 'echo' in the following paragraph, though. You are also correct about /bin/sh vs sh, my bad. And I was even looking at the source code when I wrote that...

    @ericvsmith
    Copy link
    Member

    Now you need to put the import of subprocess back in!

    Otherwise it looks good to me.

    @cvrebert
    Copy link
    Mannequin Author

    cvrebert mannequin commented Feb 4, 2010

    Okay, now if this could just get dev review...

    @ericvsmith
    Copy link
    Member

    I think this is an improvement to the existing docs, and should be committed.

    @ncoghlan
    Copy link
    Contributor

    ncoghlan commented Feb 4, 2010

    Committed for 2.7 as r77959.

    Still needs to be merged to the other branches.

    @ericvsmith
    Copy link
    Member

    When merging to py3k, don't forget to modify the print statement to be a function.

    @bitdancer
    Copy link
    Member

    Merged as part of r77961 (2.6), r77962 (py3k), and r77963 (3.1). Print fixed for py3.

    @cvrebert
    Copy link
    Mannequin Author

    cvrebert mannequin commented Feb 4, 2010

    Thanks to all for the copious feedback & suggestions, and R. David Murray for his superior docs writing skills!

    @cvrebert
    Copy link
    Mannequin Author

    cvrebert mannequin commented Feb 5, 2010

    One problem with the 3.x versions: the raw_input() should be input().

    @bitdancer
    Copy link
    Member

    Thanks for catching that. Fixed r77987 r77988.

    @christophneuroth
    Copy link
    Mannequin

    christophneuroth mannequin commented Feb 18, 2010

    As recommended by eric.smith on bpo-7950, I'd like to suggest further extending the documentation to include a security warning about (quite easily) possible code injection bugs when using the shell=True parameter (similar to other places in the documentation).

    @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
    docs Documentation in the Doc dir type-feature A feature request or enhancement
    Projects
    None yet
    Development

    No branches or pull requests

    8 participants