summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
authorBo Bayles <bbayles@gmail.com>2019-05-29 08:06:12 (GMT)
committerVinay Sajip <vinay_sajip@yahoo.co.uk>2019-05-29 08:06:11 (GMT)
commitca804955927dddb6ae5a846dbc0248a932be9a4e (patch)
treed9cb8069d21b991842d1210e1641a95ebddde912 /Lib
parentf83d1dbd3bfbde940117c85f5c70de00e47b7e6e (diff)
downloadcpython-ca804955927dddb6ae5a846dbc0248a932be9a4e.zip
cpython-ca804955927dddb6ae5a846dbc0248a932be9a4e.tar.gz
cpython-ca804955927dddb6ae5a846dbc0248a932be9a4e.tar.bz2
bpo-22454: Add shlex.join() (the opposite of shlex.split()) (GH-7605)
Diffstat (limited to 'Lib')
-rw-r--r--Lib/shlex.py7
-rw-r--r--Lib/test/test_shlex.py20
2 files changed, 26 insertions, 1 deletions
diff --git a/Lib/shlex.py b/Lib/shlex.py
index 2c9786c..fb1130d 100644
--- a/Lib/shlex.py
+++ b/Lib/shlex.py
@@ -14,7 +14,7 @@ from collections import deque
from io import StringIO
-__all__ = ["shlex", "split", "quote"]
+__all__ = ["shlex", "split", "quote", "join"]
class shlex:
"A lexical analyzer class for simple shell-like syntaxes."
@@ -305,6 +305,11 @@ def split(s, comments=False, posix=True):
return list(lex)
+def join(split_command):
+ """Return a shell-escaped string from *split_command*."""
+ return ' '.join(quote(arg) for arg in split_command)
+
+
_find_unsafe = re.compile(r'[^\w@%+=:,./-]', re.ASCII).search
def quote(s):
diff --git a/Lib/test/test_shlex.py b/Lib/test/test_shlex.py
index fd35788..a432610 100644
--- a/Lib/test/test_shlex.py
+++ b/Lib/test/test_shlex.py
@@ -308,6 +308,26 @@ class ShlexTest(unittest.TestCase):
self.assertEqual(shlex.quote("test%s'name'" % u),
"'test%s'\"'\"'name'\"'\"''" % u)
+ def testJoin(self):
+ for split_command, command in [
+ (['a ', 'b'], "'a ' b"),
+ (['a', ' b'], "a ' b'"),
+ (['a', ' ', 'b'], "a ' ' b"),
+ (['"a', 'b"'], '\'"a\' \'b"\''),
+ ]:
+ with self.subTest(command=command):
+ joined = shlex.join(split_command)
+ self.assertEqual(joined, command)
+
+ def testJoinRoundtrip(self):
+ all_data = self.data + self.posix_data
+ for command, *split_command in all_data:
+ with self.subTest(command=command):
+ joined = shlex.join(split_command)
+ resplit = shlex.split(joined)
+ self.assertEqual(split_command, resplit)
+
+
# Allow this test to be used with old shlex.py
if not getattr(shlex, "split", None):
for methname in dir(ShlexTest):