FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

gh-119670: Add 'always' keyword argument to `shlex.quote` by jb2170 · Pull Request #119674 · python/cpython · GitHub

Repository navigation

Closed
5 changes: 5 additions & 0 deletions Doc/whatsnew/3.14.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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

Back | FazBrowse Home | New Git URL