Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions Doc/library/shlex.rst
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,12 @@ The :mod:`shlex` module defines the following functions:

.. function:: quote(s)

Return a shell-escaped version of the string *s*. The returned value is a
string that can safely be used as one token in a shell command line, for
cases where you cannot use a list.
Return a shell-escaped version of the string *s* or the bytes object. The
returned value is a string literal or bytes that can safely be used as one
token in a shell command line, for cases where you cannot use a list.

.. versionchanged:: 3.8
The *s* parameter can be a bytes object.

This idiom would be unsafe:
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should add ".. versionchanged:: 3.8" markup and describe the new feature.


Expand Down
12 changes: 9 additions & 3 deletions Lib/shlex.py
Original file line number Diff line number Diff line change
Expand Up @@ -306,17 +306,23 @@ def split(s, comments=False, posix=True):


_find_unsafe = re.compile(r'[^\w@%+=:,./-]', re.ASCII).search
_find_unsafe_bytes = re.compile(b'[^\w@%+=:,./-]').search
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
_find_unsafe_bytes = re.compile(b'[^\w@%+=:,./-]').search
_find_unsafe_bytes = re.compile(br'[^\w@%+=:,./-]').search

This line generates a SyntaxWarning that causes test failure in CI where assert_python_ok asserts for no output in stderr that has this SyntaxWarning. Perhaps use a raw string ?

/Users/karthikeyansingaravelan/stuff/python/cpython/Lib/shlex.py:309: SyntaxWarning: invalid escape sequence \w
  _find_unsafe_bytes = re.compile(b'[^\w@%+=:,./-]').search


def quote(s):
"""Return a shell-escaped version of the string *s*."""
if not s:
return "''"
if _find_unsafe(s) is None:
return s

# use single quotes, and put single quotes into double quotes
# the string $'b is then quoted as '$'"'"'b'
return "'" + s.replace("'", "'\"'\"'") + "'"
if isinstance(s, bytes):
if _find_unsafe_bytes(s) is None:
return s
return b"'" + s.replace(b"'", b"'\"'\"'") + b"'"
else:
if _find_unsafe(s) is None:
return s
return "'" + s.replace("'", "'\"'\"'") + "'"


def _print_tokens(lexer):
Expand Down
5 changes: 5 additions & 0 deletions Lib/test/test_shlex.py
Original file line number Diff line number Diff line change
Expand Up @@ -304,9 +304,14 @@ def testQuote(self):
for u in unsafe:
self.assertEqual(shlex.quote('test%sname' % u),
"'test%sname'" % u)
self.assertEqual(shlex.quote(b'test%sname' % u.encode('utf-8')),
b"'test%sname'" % u.encode('utf-8'))
for u in unsafe:
self.assertEqual(shlex.quote("test%s'name'" % u),
"'test%s'\"'\"'name'\"'\"''" % u)
self.assertEqual(shlex.quote(b"test%s'name'" % u.encode('utf-8')),
b"'test%s'\"'\"'name'\"'\"''" % u.encode('utf-8'))


# Allow this test to be used with old shlex.py
if not getattr(shlex, "split", None):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add the bytes support the the :func:`shlex.quote` function.