[Python-checkins] r80433 - in python/trunk: Lib/pipes.py Lib/test/test_pipes.py Misc/NEWS

georg.brandl python-checkins at python.org
Sat Apr 24 11:08:10 CEST 2010


Author: georg.brandl
Date: Sat Apr 24 11:08:10 2010
New Revision: 80433

Log:
#7507: quote "!" in pipes.quote(); it is a special character for some shells.

Modified:
   python/trunk/Lib/pipes.py
   python/trunk/Lib/test/test_pipes.py
   python/trunk/Misc/NEWS

Modified: python/trunk/Lib/pipes.py
==============================================================================
--- python/trunk/Lib/pipes.py	(original)
+++ python/trunk/Lib/pipes.py	Sat Apr 24 11:08:10 2010
@@ -263,11 +263,11 @@
 
 # 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 @@
         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("'", "'\"'\"'") + "'"

Modified: python/trunk/Lib/test/test_pipes.py
==============================================================================
--- python/trunk/Lib/test/test_pipes.py	(original)
+++ python/trunk/Lib/test/test_pipes.py	Sat Apr 24 11:08:10 2010
@@ -64,9 +64,10 @@
         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 @@
                               "'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()

Modified: python/trunk/Misc/NEWS
==============================================================================
--- python/trunk/Misc/NEWS	(original)
+++ python/trunk/Misc/NEWS	Sat Apr 24 11:08:10 2010
@@ -25,6 +25,8 @@
 Library
 -------
 
+- Issue #7507: Quote "!" in pipes.quote(); it is special to some shells.
+
 - Issue #5238: Calling makefile() on an SSL object would prevent the
   underlying socket from being closed until all objects get truely destroyed.
 


More information about the Python-checkins mailing list