diff --git a/Doc/whatsnew/3.14.rst b/Doc/whatsnew/3.14.rst index 15216479cc6e5c..3088714c72e566 100644 --- a/Doc/whatsnew/3.14.rst +++ b/Doc/whatsnew/3.14.rst @@ -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 ------- diff --git a/Lib/shlex.py b/Lib/shlex.py index f4821616b62a0f..280ed75854f92b 100644 --- a/Lib/shlex.py +++ b/Lib/shlex.py @@ -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 @@ -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("'", "'\"'\"'") + "'" diff --git a/Lib/test/test_shlex.py b/Lib/test/test_shlex.py index 797c91ee7effdf..da0fd0e2c70a3b 100644 --- a/Lib/test/test_shlex.py +++ b/Lib/test/test_shlex.py @@ -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), @@ -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): diff --git a/Misc/NEWS.d/next/Library/2024-05-28-17-24-19.gh-issue-119670.P4J3vX.rst b/Misc/NEWS.d/next/Library/2024-05-28-17-24-19.gh-issue-119670.P4J3vX.rst new file mode 100644 index 00000000000000..db033b15f1e06d --- /dev/null +++ b/Misc/NEWS.d/next/Library/2024-05-28-17-24-19.gh-issue-119670.P4J3vX.rst @@ -0,0 +1 @@ +:func:`shlex.quote` now always quotes its input instead of testing a regex