From: Éric Araujo Date: Tue, 9 Aug 2011 21:18:06 +0000 (+0200) Subject: Avoid unwanted behavior change in shlex.quote (see #9723). X-Git-Tag: v3.3.0a1~1712^2~5 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=7fc0394a12d4bb5632424bfdfd4bb35590dec2e5;p=thirdparty%2FPython%2Fcpython.git Avoid unwanted behavior change in shlex.quote (see #9723). I simplified the quote code to use a regex instead of a loop+test when I moved pipes.quote to shlex in 5966eeb0457d; Ezio Melotti pointed out that my regex contained redundant parts (now removed) and allowed non-ASCII characters (now disallowed). I think common UNIX shells don’t quote non-ASCII characters, but there’s no harm in doing so. We’ll see if users request a change. --- 7fc0394a12d4bb5632424bfdfd4bb35590dec2e5 diff --cc Lib/shlex.py index 279ab4840595,3edd3db1ed9e..92c49c360842 --- a/Lib/shlex.py +++ b/Lib/shlex.py @@@ -275,21 -274,6 +275,21 @@@ def split(s, comments=False, posix=True lex.commenters = '' return list(lex) + - _find_unsafe = re.compile(r'[^\w\d@%_\-\+=:,\./]').search ++_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("'", "'\"'\"'") + "'" + + if __name__ == '__main__': if len(sys.argv) == 1: lexer = shlex() diff --cc Lib/test/test_shlex.py index ea3d777ceba3,25e4b6df6c5d..d4463f300481 --- a/Lib/test/test_shlex.py +++ b/Lib/test/test_shlex.py @@@ -174,21 -173,6 +174,22 @@@ class ShlexTest(unittest.TestCase) "%s: %s != %s" % (self.data[i][0], l, self.data[i][1:])) + def testQuote(self): + safeunquoted = string.ascii_letters + string.digits + '@%_-+=:,./' - unsafe = '"`$\\!' ++ 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), + "'test%sname'" % u) + for u in unsafe: + self.assertEqual(shlex.quote("test%s'name'" % u), + "'test%s'\"'\"'name'\"'\"''" % u) + + # Allow this test to be used with old shlex.py if not getattr(shlex, "split", None): for methname in dir(ShlexTest):