Skip to content
Open
Show file tree
Hide file tree
Changes from 10 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
14 changes: 13 additions & 1 deletion Doc/library/shlex.rst
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,21 @@ The :mod:`shlex` module defines the following functions:
.. versionadded:: 3.8


.. function:: quote(s)
.. function:: quote(s, *, always=False)

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.

If the *always* keyword argument is set to ``True`` then the string *s*
will always be escaped, even in the absence of special characters. This is
nice for uniformity, for example when escaping a list of strings.
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
If the *always* keyword argument is set to ``True`` then the string *s*
will always be escaped, even in the absence of special characters. This is
nice for uniformity, for example when escaping a list of strings.
If *always* is ``True``, then the string *s* is
unconditionally escaped, even in the absence of
special characters. This is typically useful for
uniformly escaping a list of strings:


>>> from shlex import quote
>>> strs = ['escape', 'all of', 'us']
>>> [quote(s, always=True) for s in strs]
["'escape'", "'all of'", "'us'"]

.. _shlex-quote-warning:

.. warning::
Expand Down Expand Up @@ -98,6 +107,9 @@ The :mod:`shlex` module defines the following functions:

.. versionadded:: 3.3

.. versionchanged:: 3.14
Added the *always* parameter.

The :mod:`shlex` module defines the following class:


Expand Down
6 changes: 6 additions & 0 deletions Doc/whatsnew/3.14.rst
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,12 @@ ___
Use :func:`pty.openpty` instead.
(Contributed by Nikita Sobolev in :gh:`118824`.)

shlex
-----

* Add keyword-only argument of *always* to :func:`shlex.quote` for forcing
the escaping of the string passed to it.
Copy link
Member

Choose a reason for hiding this comment

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

* Allow :func:`shlex.quote` to unconditionally escape its input
  via ``shlex.quote(string, always=True)``.

  (Contributed by YOUR_NAME_OR_GITHUB_NICKNAME in :gh:`119670`.)


sqlite3
-------

Expand Down
12 changes: 6 additions & 6 deletions Lib/shlex.py
Original file line number Diff line number Diff line change
Expand Up @@ -320,17 +320,17 @@ def join(split_command):

_find_unsafe = re.compile(r'[^\w@%+=:,./-]', re.ASCII).search

def quote(s):
def quote(s, *, always=False):
"""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 always or _find_unsafe(s) is not None:
# use single quotes, and put single quotes into double quotes
# the string $'b is then quoted as '$'"'"'b'
return "'" + s.replace("'", "'\"'\"'") + "'"

return s

def _print_tokens(lexer):
while tt := lexer.get_token():
Expand Down
15 changes: 15 additions & 0 deletions Lib/test/test_shlex.py
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,21 @@ def testQuote(self):
self.assertEqual(shlex.quote("test%s'name'" % u),
"'test%s'\"'\"'name'\"'\"''" % u)

def testQuoteAlways(self):
strs = ['hello', 'to the', 'world', 'escape me', 'no-escape-needed']

# guarantee escaping all strings
expected = ["'hello'", "'to the'", "'world'", "'escape me'",
"'no-escape-needed'"]
result = [shlex.quote(s, always=True) for s in strs]
self.assertEqual(expected, result)

# just escape when necessary
expected = ["hello", "'to the'", "world", "'escape me'",
"no-escape-needed"]
result = [shlex.quote(s, always=False) for s in strs]
self.assertEqual(expected, result)

def testJoin(self):
for split_command, command in [
(['a ', 'b'], "'a ' b"),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
:func:`shlex.quote` now has an *always* keyword argument for forcing
the escaping of the string passed to it
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
:func:`shlex.quote` now has an *always* keyword argument for forcing
the escaping of the string passed to it
Allow :func:`shlex.quote` to unconditionally escape its input
via the keyword-only argument *always*.