Skip to content

gh-119670: Add 'always' keyword argument to shlex.quote #119674

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
Open
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
5 changes: 5 additions & 0 deletions Doc/whatsnew/3.14.rst
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,11 @@ ___
Use :func:`pty.openpty` instead.
(Contributed by Nikita Sobolev in :gh:`118824`.)

shlex
-----

* :func:`shlex.quote` now always quotes its input instead of testing a regex

sqlite3
-------

Expand Down
8 changes: 0 additions & 8 deletions Lib/shlex.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
# changes to tokenize more like Posix shells by Vinay Sajip, July 2016.

import os
import re
import sys
from collections import deque

Expand Down Expand Up @@ -318,15 +317,8 @@ def join(split_command):
return ' '.join(quote(arg) for arg in split_command)


_find_unsafe = re.compile(r'[^\w@%+=:,./-]', re.ASCII).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("'", "'\"'\"'") + "'"
Expand Down
8 changes: 3 additions & 5 deletions Lib/test/test_shlex.py
Original file line number Diff line number Diff line change
Expand Up @@ -323,12 +323,10 @@ def testUnicodeHandling(self):
self.assertEqual(list(s), ref)

def testQuote(self):
safeunquoted = string.ascii_letters + string.digits + '@%_-+=:,./'
unicode_sample = '\xe9\xe0\xdf' # e + acute accent, a + grave, sharp s
unsafe = '"`$\\!' + unicode_sample

self.assertEqual(shlex.quote(''), "''")
self.assertEqual(shlex.quote(safeunquoted), safeunquoted)
self.assertEqual(shlex.quote('test file name'), "'test file name'")
for u in unsafe:
self.assertEqual(shlex.quote('test%sname' % u),
Expand All @@ -339,9 +337,9 @@ def testQuote(self):

def testJoin(self):
for split_command, command in [
(['a ', 'b'], "'a ' b"),
(['a', ' b'], "a ' b'"),
(['a', ' ', 'b'], "a ' ' b"),
(['a ', 'b'], "'a ' 'b'"),
(['a', ' b'], "'a' ' b'"),
(['a', ' ', 'b'], "'a' ' ' 'b'"),
(['"a', 'b"'], '\'"a\' \'b"\''),
]:
with self.subTest(command=command):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
:func:`shlex.quote` now always quotes its input instead of testing a regex
Loading