This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Title: Adding the opposite function of shlex.split()
Type: enhancement Stage: resolved
Components: Library (Lib) Versions: Python 3.8
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: Matthijs Kooijman, Sworddragon, bbayles, cheryl.sabella, dhimmel, p-ganssle, r.david.murray, roippi, vinay.sajip
Priority: normal Keywords: patch

Created on 2014-09-21 19:35 by Sworddragon, 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: (Sworddragon) 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: (Sworddragon) 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)
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?
Date User Action Args
2022-04-11 14:58:08adminsetgithub: 66644
2019-09-06 14:43:48dhimmelsetnosy: + dhimmel
messages: + msg351257
2019-05-29 17:09:06berker.peksagsetstatus: open -> closed
resolution: fixed
stage: patch review -> resolved
2019-05-29 08:06:21vinay.sajipsetmessages: + msg343872
2019-04-17 16:06:10terry.reedysetversions: + Python 3.8, - Python 3.5
2019-04-11 18:55:05p-gansslesetnosy: + p-ganssle
2018-06-10 23:00:55bbaylessetnosy: + bbayles
2018-06-10 22:59:07bbaylessetkeywords: + patch
stage: patch review
pull_requests: + pull_request7228
2018-06-07 08:14:46Matthijs Kooijmansetmessages: + msg318913
2018-06-07 08:05:22vinay.sajipsetmessages: + msg318911
2018-06-06 12:59:58r.david.murraysetnosy: + r.david.murray
messages: + msg318817
2018-06-06 12:37:02cheryl.sabellasetnosy: + vinay.sajip, cheryl.sabella
messages: + msg318816
2018-04-25 13:01:11Matthijs Kooijmansetnosy: + Matthijs Kooijman
2014-09-22 00:08:21roippisetmessages: + msg227244
versions: + Python 3.5, - Python 3.4
2014-09-21 21:26:34Sworddragonsetmessages: + msg227239
2014-09-21 20:23:19roippisetnosy: + roippi
messages: + msg227235
2014-09-21 19:35:49Sworddragoncreate