mirror of
https://github.com/python/cpython.git
synced 2025-08-22 01:35:16 +00:00
Issue #8402: Added the escape() function to the glob module.
This commit is contained in:
parent
e3010fd740
commit
fd32fffa5a
4 changed files with 49 additions and 2 deletions
16
Lib/glob.py
16
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
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue