Issue 22454: Adding the opposite function of shlex.split() (original) (raw)

Created on 2014-09-21 19:35 by deleted250130, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 7605 merged bbayles,2018-06-10 22:59
Messages (10)
msg227228 - (view) Author: (deleted250130) Date: 2014-09-21 19:35
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.
msg227235 - (view) Author: Ben Roberts (roippi) * Date: 2014-09-21 20:23
' '.join(shlex.quote(x) for x in split_command)
msg227239 - (view) Author: (deleted250130) Date: 2014-09-21 21:26
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.
msg227244 - (view) Author: Ben Roberts (roippi) * Date: 2014-09-22 00:08
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.
msg318816 - (view) Author: Cheryl Sabella (cheryl.sabella) * (Python committer) Date: 2018-06-06 12:37
Hi Vinay, You made the most recent changes to shlex, so I was wondering what you thought of this suggestion. Thanks!
msg318817 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2018-06-06 12:59
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.
msg318911 - (view) Author: Vinay Sajip (vinay.sajip) * (Python committer) Date: 2018-06-07 08:05
I'm +0 on the idea.
msg318913 - (view) Author: Matthijs Kooijman (Matthijs Kooijman) Date: 2018-06-07 08:14
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.
msg343872 - (view) Author: Vinay Sajip (vinay.sajip) * (Python committer) Date: 2019-05-29 08:06
New changeset ca804955927dddb6ae5a846dbc0248a932be9a4e by Vinay Sajip (Bo Bayles) in branch 'master': bpo-22454: Add shlex.join() (the opposite of shlex.split()) (GH-7605) https://github.com/python/cpython/commit/ca804955927dddb6ae5a846dbc0248a932be9a4e
msg351257 - (view) Author: Daniel Himmelstein (dhimmel) * Date: 2019-09-06 14:43
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?
History
Date User Action Args
2022-04-11 14:58:08 admin set github: 66644
2019-09-06 14:43:48 dhimmel set nosy: + dhimmelmessages: +
2019-05-29 17:09:06 berker.peksag set status: open -> closedresolution: fixedstage: patch review -> resolved
2019-05-29 08:06:21 vinay.sajip set messages: +
2019-04-17 16:06:10 terry.reedy set versions: + Python 3.8, - Python 3.5
2019-04-11 18:55:05 p-ganssle set nosy: + p-ganssle
2018-06-10 23:00:55 bbayles set nosy: + bbayles
2018-06-10 22:59:07 bbayles set keywords: + patchstage: patch reviewpull_requests: + <pull%5Frequest7228>
2018-06-07 08:14:46 Matthijs Kooijman set messages: +
2018-06-07 08:05:22 vinay.sajip set messages: +
2018-06-06 12:59:58 r.david.murray set nosy: + r.david.murraymessages: +
2018-06-06 12:37:02 cheryl.sabella set nosy: + vinay.sajip, cheryl.sabellamessages: +
2018-04-25 13:01:11 Matthijs Kooijman set nosy: + Matthijs Kooijman
2014-09-22 00:08:21 roippi set messages: + versions: + Python 3.5, - Python 3.4
2014-09-21 21:26:34 deleted250130 set messages: +
2014-09-21 20:23:19 roippi set nosy: + roippimessages: +
2014-09-21 19:35:49 deleted250130 create