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

Adding the opposite function of shlex.split() #66644

Closed
Sworddragon mannequin opened this issue Sep 21, 2014 · 10 comments
Closed

Adding the opposite function of shlex.split() #66644

Sworddragon mannequin opened this issue Sep 21, 2014 · 10 comments
Labels
3.8 only security fixes stdlib Python modules in the Lib dir type-feature A feature request or enhancement

Comments

@Sworddragon
Copy link
Mannequin

Sworddragon mannequin commented Sep 21, 2014

BPO 22454
Nosy @vsajip, @bitdancer, @pganssle, @dhimmel, @csabella, @bbayles
PRs
  • bpo-22454: Add shlex.join() (the opposite of shlex.split()) #7605
  • 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 2019-05-29.17:09:06.862>
    created_at = <Date 2014-09-21.19:35:49.228>
    labels = ['3.8', 'type-feature', 'library']
    title = 'Adding the opposite function of shlex.split()'
    updated_at = <Date 2019-09-06.14:43:48.663>
    user = 'https://bugs.python.org/Sworddragon'

    bugs.python.org fields:

    activity = <Date 2019-09-06.14:43:48.663>
    actor = 'dhimmel'
    assignee = 'none'
    closed = True
    closed_date = <Date 2019-05-29.17:09:06.862>
    closer = 'berker.peksag'
    components = ['Library (Lib)']
    creation = <Date 2014-09-21.19:35:49.228>
    creator = 'Sworddragon'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 22454
    keywords = ['patch']
    message_count = 10.0
    messages = ['227228', '227235', '227239', '227244', '318816', '318817', '318911', '318913', '343872', '351257']
    nosy_count = 9.0
    nosy_names = ['vinay.sajip', 'r.david.murray', 'Sworddragon', 'roippi', 'p-ganssle', 'dhimmel', 'cheryl.sabella', 'bbayles', 'Matthijs Kooijman']
    pr_nums = ['7605']
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'enhancement'
    url = 'https://bugs.python.org/issue22454'
    versions = ['Python 3.8']

    @Sworddragon
    Copy link
    Mannequin Author

    Sworddragon mannequin commented Sep 21, 2014

    There is currently shlex.split() that is for example useful to split a command string and pass it to subprocess.Popen with shell=False. But I'm missing a function that does the opposite: Building the command string from a list that could for example then be used in subprocess.Popen with shell=True.

    @Sworddragon Sworddragon mannequin added stdlib Python modules in the Lib dir type-feature A feature request or enhancement labels Sep 21, 2014
    @roippi
    Copy link
    Mannequin

    roippi mannequin commented Sep 21, 2014

    ' '.join(shlex.quote(x) for x in split_command)

    @Sworddragon
    Copy link
    Mannequin Author

    Sworddragon mannequin commented Sep 21, 2014

    Yes, it is possible to do this with a few other commands. But I think it would be still a nice enhancement to have a direct function for it.

    @roippi
    Copy link
    Mannequin

    roippi mannequin commented Sep 22, 2014

    For the record I am on board with a shlex.join. Even though the implementation is simple:

    • It is not obvious to many users if there are any "gotchas" by doing a ' '.join yourself, /even if/ you know that strings with spaces in them need to pass through shlex.quote first.
    • The symmetry of shlex.split and shlex.join is obvious - I don't need to read any documentation to know that they are inverse operations.
    • "batteries included," "one obvious way," and all that.

    @csabella
    Copy link
    Contributor

    csabella commented Jun 6, 2018

    Hi Vinay,

    You made the most recent changes to shlex, so I was wondering what you thought of this suggestion. Thanks!

    @bitdancer
    Copy link
    Member

    I like it, myself, though there is some danger in promoting the idea that this is a "safe" operation. It theoretically should be, but it increases the attack surface slightly if you actually use it (that is, using shell=False is always safer, by at least a small margin). Maybe a mention of that in the docs would be enough, though.

    @vsajip
    Copy link
    Member

    vsajip commented Jun 7, 2018

    I'm +0 on the idea.

    @MatthijsKooijman
    Copy link
    Mannequin

    MatthijsKooijman mannequin commented Jun 7, 2018

    One usecase that such a function would be well-suited for is for *displaying* commands being executed. Then, the commands will be executed as a command+args array, but can be displayed unambiguously in log output.

    @terryjreedy terryjreedy added the 3.8 only security fixes label Apr 17, 2019
    @vsajip
    Copy link
    Member

    vsajip commented May 29, 2019

    New changeset ca80495 by Vinay Sajip (Bo Bayles) in branch 'master':
    bpo-22454: Add shlex.join() (the opposite of shlex.split()) (GH-7605)
    ca80495

    @dhimmel
    Copy link
    Mannequin

    dhimmel mannequin commented Sep 6, 2019

    I am interested in shlex.join as a way to log subprocess.CompletedProcess.args as a string that users could run in their terminals. I initially assumed that this was also the scope of shlex.join. However, it seems that shlex.join does not accept all types of command arguments supported by subprocess, such as pathlib.Path objects. Should shlex.join support an split_command list that includes pathlib.Path objects and any other types supported by subprocess?

    @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.8 only security fixes stdlib Python modules in the Lib dir type-feature A feature request or enhancement
    Projects
    None yet
    Development

    No branches or pull requests

    5 participants