summaryrefslogtreecommitdiffstats
path: root/Lib/pipes.py
diff options
context:
space:
mode:
authorÉric Araujo <merwok@netwok.org>2011-07-27 16:29:31 (GMT)
committerÉric Araujo <merwok@netwok.org>2011-07-27 16:29:31 (GMT)
commit9bce311ea4f58ec04cab356a748e173ecfea381c (patch)
treefd767a62dab42bb79ce2e86a2ac6fb970930c10e /Lib/pipes.py
parentfcdaaa9011be28d4653dadc92df4a94b2f669711 (diff)
downloadcpython-9bce311ea4f58ec04cab356a748e173ecfea381c.zip
cpython-9bce311ea4f58ec04cab356a748e173ecfea381c.tar.gz
cpython-9bce311ea4f58ec04cab356a748e173ecfea381c.tar.bz2
Add shlex.quote function, to escape filenames and command lines (#9723).
This function used to live as pipes.quote, where it was undocumented but used anyway. (An alias still exists for backward compatibility.) The tests have been moved as is, but the code of the function was changed to use a regex instead of a loop with string comparisons (at Ian Bicking’s suggestion). I’m terrible at regexes, so any feedback is welcome.
Diffstat (limited to 'Lib/pipes.py')
-rw-r--r--Lib/pipes.py23
1 files changed, 3 insertions, 20 deletions
diff --git a/Lib/pipes.py b/Lib/pipes.py
index 51666a8..693309f 100644
--- a/Lib/pipes.py
+++ b/Lib/pipes.py
@@ -62,7 +62,9 @@ For an example, see the function test() at the end of the file.
import re
import os
import tempfile
-import string
+# we import the quote function rather than the module for backward compat
+# (quote used to be an undocumented but used function in pipes)
+from shlex import quote
__all__ = ["Template"]
@@ -245,22 +247,3 @@ def makepipeline(infile, steps, outfile):
cmdlist = trapcmd + '\n' + cmdlist + '\n' + rmcmd
#
return cmdlist
-
-
-# Reliably quote a string as a single argument for /bin/sh
-
-# Safe unquoted
-_safechars = frozenset(string.ascii_letters + string.digits + '@%_-+=:,./')
-
-def quote(file):
- """Return a shell-escaped version of the file string."""
- for c in file:
- if c not in _safechars:
- break
- else:
- if not file:
- return "''"
- return file
- # use single quotes, and put single quotes into double quotes
- # the string $'b is then quoted as '$'"'"'b'
- return "'" + file.replace("'", "'\"'\"'") + "'"