diff options
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) |
commit | 9bce311ea4f58ec04cab356a748e173ecfea381c (patch) | |
tree | fd767a62dab42bb79ce2e86a2ac6fb970930c10e /Lib/test/test_shlex.py | |
parent | fcdaaa9011be28d4653dadc92df4a94b2f669711 (diff) | |
download | cpython-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/test/test_shlex.py')
-rw-r--r-- | Lib/test/test_shlex.py | 20 |
1 files changed, 18 insertions, 2 deletions
diff --git a/Lib/test/test_shlex.py b/Lib/test/test_shlex.py index 25e4b6d..ea3d777 100644 --- a/Lib/test/test_shlex.py +++ b/Lib/test/test_shlex.py @@ -1,6 +1,7 @@ -import unittest -import os, sys, io +import io import shlex +import string +import unittest from test import support @@ -173,6 +174,21 @@ 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 = '"`$\\!' + + 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): |