summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2013-11-18 11:06:43 (GMT)
committerSerhiy Storchaka <storchaka@gmail.com>2013-11-18 11:06:43 (GMT)
commitfd32fffa5ada8b8be8a65bd51b001d989f99a3d3 (patch)
tree2b0324a2068af8e9b1d887bb74b426ac9f12ca2b /Lib
parente3010fd740b63df306f878fc872e29d5f6be7487 (diff)
downloadcpython-fd32fffa5ada8b8be8a65bd51b001d989f99a3d3.zip
cpython-fd32fffa5ada8b8be8a65bd51b001d989f99a3d3.tar.gz
cpython-fd32fffa5ada8b8be8a65bd51b001d989f99a3d3.tar.bz2
Issue #8402: Added the escape() function to the glob module.
Diffstat (limited to 'Lib')
-rw-r--r--Lib/glob.py16
-rw-r--r--Lib/test/test_glob.py22
2 files changed, 36 insertions, 2 deletions
diff --git a/Lib/glob.py b/Lib/glob.py
index 1a268a3..e388b5f 100644
--- a/Lib/glob.py
+++ b/Lib/glob.py
@@ -79,8 +79,8 @@ def glob0(dirname, basename):
return []
-magic_check = re.compile('[*?[]')
-magic_check_bytes = re.compile(b'[*?[]')
+magic_check = re.compile('([*?[])')
+magic_check_bytes = re.compile(b'([*?[])')
def has_magic(s):
if isinstance(s, bytes):
@@ -91,3 +91,15 @@ def has_magic(s):
def _ishidden(path):
return path[0] in ('.', b'.'[0])
+
+def escape(pathname):
+ """Escape all special characters.
+ """
+ # Escaping is done by wrapping any of "*?[" between square brackets.
+ # Metacharacters do not work in the drive part and shouldn't be escaped.
+ drive, pathname = os.path.splitdrive(pathname)
+ if isinstance(pathname, bytes):
+ pathname = magic_check_bytes.sub(br'[\1]', pathname)
+ else:
+ pathname = magic_check.sub(r'[\1]', pathname)
+ return drive + pathname
diff --git a/Lib/test/test_glob.py b/Lib/test/test_glob.py
index eb9aeb5..a5ab8d6 100644
--- a/Lib/test/test_glob.py
+++ b/Lib/test/test_glob.py
@@ -169,6 +169,28 @@ class GlobTests(unittest.TestCase):
eq(glob.glob('\\\\*\\*\\'), [])
eq(glob.glob(b'\\\\*\\*\\'), [])
+ def check_escape(self, arg, expected):
+ self.assertEqual(glob.escape(arg), expected)
+ self.assertEqual(glob.escape(os.fsencode(arg)), os.fsencode(expected))
+
+ def test_escape(self):
+ check = self.check_escape
+ check('abc', 'abc')
+ check('[', '[[]')
+ check('?', '[?]')
+ check('*', '[*]')
+ check('[[_/*?*/_]]', '[[][[]_/[*][?][*]/_]]')
+ check('/[[_/*?*/_]]/', '/[[][[]_/[*][?][*]/_]]/')
+
+ @unittest.skipUnless(sys.platform == "win32", "Win32 specific test")
+ def test_escape_windows(self):
+ check = self.check_escape
+ check('?:?', '?:[?]')
+ check('*:*', '*:[*]')
+ check(r'\\?\c:\?', r'\\?\c:\[?]')
+ check(r'\\*\*\*', r'\\*\*\[*]')
+ check('//?/c:/?', '//?/c:/[?]')
+ check('//*/*/*', '//*/*/[*]')
def test_main():
run_unittest(GlobTests)