Thanks to visit codestin.com Credit goes to github.com
We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent f83d1db commit ca80495Copy full SHA for ca80495
5 files changed
Doc/library/shlex.rst
@@ -37,6 +37,21 @@ The :mod:`shlex` module defines the following functions:
37
standard input.
38
39
40
+.. function:: join(split_command)
41
+
42
+ Concatenate the tokens of the list *split_command* and return a string.
43
+ This function is the inverse of :func:`split`.
44
45
+ >>> from shlex import join
46
+ >>> print(join(['echo', '-n', 'Multiple words']))
47
+ echo -n 'Multiple words'
48
49
+ The returned value is shell-escaped to protect against injection
50
+ vulnerabilities (see :func:`quote`).
51
52
+ .. versionadded:: 3.8
53
54
55
.. function:: quote(s)
56
57
Return a shell-escaped version of the string *s*. The returned value is a
Doc/whatsnew/3.8.rst
@@ -552,6 +552,11 @@ convenience functions to automate the necessary tasks usually involved when
552
creating a server socket, including accepting both IPv4 and IPv6 connections
553
on the same socket. (Contributed by Giampaolo Rodola in :issue:`17561`.)
554
555
+shlex
556
+----------
557
558
+The new :func:`shlex.join` function acts as the inverse of :func:`shlex.split`.
559
+(Contributed by Bo Bayles in :issue:`32102`.)
560
561
shutil
562
------
Lib/shlex.py
@@ -14,7 +14,7 @@
14
15
from io import StringIO
16
17
-__all__ = ["shlex", "split", "quote"]
+__all__ = ["shlex", "split", "quote", "join"]
18
19
class shlex:
20
"A lexical analyzer class for simple shell-like syntaxes."
@@ -305,6 +305,11 @@ def split(s, comments=False, posix=True):
305
return list(lex)
306
307
308
+def join(split_command):
309
+ """Return a shell-escaped string from *split_command*."""
310
+ return ' '.join(quote(arg) for arg in split_command)
311
312
313
_find_unsafe = re.compile(r'[^\w@%+=:,./-]', re.ASCII).search
314
315
def quote(s):
Lib/test/test_shlex.py
@@ -308,6 +308,26 @@ def testQuote(self):
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"),
316
+ (['"a', 'b"'], '\'"a\' \'b"\''),
317
+ ]:
318
+ with self.subTest(command=command):
319
+ joined = shlex.join(split_command)
320
+ self.assertEqual(joined, command)
321
322
+ def testJoinRoundtrip(self):
323
+ all_data = self.data + self.posix_data
324
+ for command, *split_command in all_data:
325
326
327
+ resplit = shlex.split(joined)
328
+ self.assertEqual(split_command, resplit)
329
330
331
# Allow this test to be used with old shlex.py
332
if not getattr(shlex, "split", None):
333
for methname in dir(ShlexTest):
Misc/NEWS.d/next/Library/2018-06-10-17-48-07.bpo-22454.qeiy_X.rst
@@ -0,0 +1,2 @@
1
+The :mod:`shlex` module now exposes :func:`shlex.join`, the inverse of
2
+:func:`shlex.split`. Patch by Bo Bayles.
0 commit comments