diff options
author | Georg Brandl <georg@python.org> | 2010-04-24 09:08:10 (GMT) |
---|---|---|
committer | Georg Brandl <georg@python.org> | 2010-04-24 09:08:10 (GMT) |
commit | 4341e54de857aa0d5b169fd0277e45f6a52c039b (patch) | |
tree | 11349cb9d6cc6394fdfa1ffd38bcf89c214fa1fc /Lib | |
parent | f8bff488bd27110523b70c83a0ac8d8f0ebcf62e (diff) | |
download | cpython-4341e54de857aa0d5b169fd0277e45f6a52c039b.zip cpython-4341e54de857aa0d5b169fd0277e45f6a52c039b.tar.gz cpython-4341e54de857aa0d5b169fd0277e45f6a52c039b.tar.bz2 |
#7507: quote "!" in pipes.quote(); it is a special character for some shells.
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/pipes.py | 17 | ||||
-rw-r--r-- | Lib/test/test_pipes.py | 9 |
2 files changed, 10 insertions, 16 deletions
diff --git a/Lib/pipes.py b/Lib/pipes.py index 25e9915..02e1257 100644 --- a/Lib/pipes.py +++ b/Lib/pipes.py @@ -263,11 +263,11 @@ def makepipeline(infile, steps, outfile): # Reliably quote a string as a single argument for /bin/sh -_safechars = string.ascii_letters + string.digits + '!@%_-+=:,./' # Safe unquoted -_funnychars = '"`$\\' # Unsafe inside "double quotes" +# Safe unquoted +_safechars = frozenset(string.ascii_letters + string.digits + '@%_-+=:,./') def quote(file): - ''' return a shell-escaped version of the file string ''' + """Return a shell-escaped version of the file string.""" for c in file: if c not in _safechars: break @@ -275,11 +275,6 @@ def quote(file): if not file: return "''" return file - if '\'' not in file: - return '\'' + file + '\'' - res = '' - for c in file: - if c in _funnychars: - c = '\\' + c - res = res + c - return '"' + res + '"' + # use single quotes, and put single quotes into double quotes + # the string $'b is then quoted as '$'"'"'b' + return "'" + file.replace("'", "'\"'\"'") + "'" diff --git a/Lib/test/test_pipes.py b/Lib/test/test_pipes.py index a638598..476bd7c 100644 --- a/Lib/test/test_pipes.py +++ b/Lib/test/test_pipes.py @@ -64,9 +64,10 @@ class SimplePipeTests(unittest.TestCase): self.assertEqual(open(TESTFN).read(), d) def testQuoting(self): - safeunquoted = string.ascii_letters + string.digits + '!@%_-+=:,./' - unsafe = '"`$\\' + safeunquoted = string.ascii_letters + string.digits + '@%_-+=:,./' + unsafe = '"`$\\!' + self.assertEqual(pipes.quote(''), "''") self.assertEqual(pipes.quote(safeunquoted), safeunquoted) self.assertEqual(pipes.quote('test file name'), "'test file name'") for u in unsafe: @@ -74,9 +75,7 @@ class SimplePipeTests(unittest.TestCase): "'test%sname'" % u) for u in unsafe: self.assertEqual(pipes.quote("test%s'name'" % u), - '"test\\%s\'name\'"' % u) - - self.assertEqual(pipes.quote(''), "''") + "'test%s'\"'\"'name'\"'\"''" % u) def testRepr(self): t = pipes.Template() |