Skip to content

Commit

Permalink
Remove raw_escape from fnmatch (#163)
Browse files Browse the repository at this point in the history
As we are deprecating raw_escape in glob, there is no reason to release
fnmatch with raw_escape.
  • Loading branch information
facelessuser committed Feb 8, 2021
1 parent 3aa7448 commit 2737567
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 73 deletions.
60 changes: 9 additions & 51 deletions tests/test_fnmatch.py
Original file line number Diff line number Diff line change
Expand Up @@ -617,7 +617,7 @@ def test_all_bytes(self):
class TestFnMatchEscapes(unittest.TestCase):
"""Test escaping."""

def check_escape(self, arg, expected, raw=False, unix=None, raw_chars=True):
def check_escape(self, arg, expected, unix=None, raw_chars=True):
"""Verify escapes."""

flags = 0
Expand All @@ -626,27 +626,15 @@ def check_escape(self, arg, expected, raw=False, unix=None, raw_chars=True):
elif unix is True:
flags = fnmatch.FORCEUNIX

if raw:
self.assertEqual(fnmatch.raw_escape(arg, raw_chars=raw_chars), expected)
self.assertEqual(fnmatch.raw_escape(os.fsencode(arg), raw_chars=raw_chars), os.fsencode(expected))
file = (util.norm_pattern(arg, False, True) if raw_chars else arg).replace('\\\\', '\\')
self.assertTrue(
fnmatch.fnmatch(
file,
fnmatch.raw_escape(arg, raw_chars=raw_chars),
flags=flags
)
)
else:
self.assertEqual(fnmatch.escape(arg), expected)
self.assertEqual(fnmatch.escape(os.fsencode(arg)), os.fsencode(expected))
self.assertTrue(
fnmatch.fnmatch(
arg,
fnmatch.escape(arg),
flags=flags
)
self.assertEqual(fnmatch.escape(arg), expected)
self.assertEqual(fnmatch.escape(os.fsencode(arg)), os.fsencode(expected))
self.assertTrue(
fnmatch.fnmatch(
arg,
fnmatch.escape(arg),
flags=flags
)
)

def test_escape(self):
"""Test path escapes."""
Expand All @@ -659,36 +647,6 @@ def test_escape(self):
check('[[_/*?*/_]]', r'\[\[_/\*\?\*/_\]\]')
check('/[[_/*?*/_]]/', r'/\[\[_/\*\?\*/_\]\]/')

def test_raw_escape(self):
"""Test path escapes."""

check = self.check_escape
check(r'abc', 'abc', raw=True)
check(r'[', r'\[', raw=True)
check(r'?', r'\?', raw=True)
check(r'*', r'\*', raw=True)
check(r'[[_/*?*/_]]', r'\[\[_/\*\?\*/_\]\]', raw=True)
check(r'/[[_/*?*/_]]/', r'/\[\[_/\*\?\*/_\]\]/', raw=True)
check(r'\x3f', r'\?', raw=True)
# `fnmatch` doesn't care about drives
check(r'\\\\[^what]\\name\\temp', r'\\\\\[^what\]\\name\\temp', raw=True, unix=False)
check('//[^what]/name/temp', r'//\[^what\]/name/temp', raw=True, unix=False)

def test_raw_escape_no_raw_chars(self):
"""Test path escapes with no raw character translations."""

check = self.check_escape
check(r'abc', 'abc', raw=True, raw_chars=False)
check(r'[', r'\[', raw=True, raw_chars=False)
check(r'?', r'\?', raw=True, raw_chars=False)
check(r'*', r'\*', raw=True, raw_chars=False)
check(r'[[_/*?*/_]]', r'\[\[_/\*\?\*/_\]\]', raw=True, raw_chars=False)
check(r'/[[_/*?*/_]]/', r'/\[\[_/\*\?\*/_\]\]/', raw=True, raw_chars=False)
check(r'\x3f', r'\\x3f', raw=True, raw_chars=False)
# `fnmatch` doesn't care about drives
check(r'\\\\[^what]\\name\\temp', r'\\\\\[^what\]\\name\\temp', raw=True, raw_chars=False, unix=False)
check('//[^what]/name/temp', r'//\[^what\]/name/temp', raw=True, raw_chars=False, unix=False)

@unittest.skipUnless(sys.platform.startswith('win'), "Windows specific test")
def test_escape_windows(self):
"""Test windows escapes."""
Expand Down
19 changes: 7 additions & 12 deletions wcmatch/_wcparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -314,20 +314,15 @@ class PatternLimitException(Exception):
"""Pattern limit exception."""


def raw_escape(pattern, unix=None, raw_chars=True, pathname=False):
"""Apply raw character transform before applying escape."""

return _escape(util.norm_pattern(pattern, False, raw_chars, True), unix, True, pathname=pathname)


def escape(pattern, unix=None, pathname=False):
"""Normal escape."""

return _escape(pattern, unix, pathname=pathname)
def escape(pattern, unix=None, pathname=True, raw=False):
"""
Escape.
`unix`: use Unix style path logic.
`pathname`: Use path logic.
`raw`: Handle raw strings (deprecated)
def _escape(pattern, unix=None, raw=False, pathname=False):
"""Escape."""
"""

if isinstance(pattern, bytes):
drive_pat = RE_WIN_DRIVE[BYTES]
Expand Down
10 changes: 2 additions & 8 deletions wcmatch/fnmatch.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
"NEGATE", "MINUSNEGATE", "DOTMATCH", "BRACE", "SPLIT",
"NEGATEALL", "FORCEWIN", "FORCEUNIX",
"C", "I", "R", "N", "M", "D", "E", "S", "B", "A", "W", "U",
"translate", "fnmatch", "filter", "escape", "raw_escape", "is_magic"
"translate", "fnmatch", "filter", "escape", "is_magic"
)

C = CASE = _wcparse.CASE
Expand Down Expand Up @@ -105,16 +105,10 @@ def filter(filenames, patterns, *, flags=0, limit=_wcparse.PATTERN_LIMIT): # no
return matches


def raw_escape(pattern, raw_chars=True):
"""Apply raw character transform before applying escape."""

return _wcparse.raw_escape(pattern, raw_chars=raw_chars)


def escape(pattern):
"""Escape."""

return _wcparse.escape(pattern)
return _wcparse.escape(pattern, pathname=False)


def is_magic(pattern, *, flags=0):
Expand Down
4 changes: 2 additions & 2 deletions wcmatch/glob.py
Original file line number Diff line number Diff line change
Expand Up @@ -620,13 +620,13 @@ def globfilter(filenames, patterns, *, flags=0, root_dir=None, limit=_wcparse.PA
def raw_escape(pattern, unix=None, raw_chars=True):
"""Apply raw character transform before applying escape."""

return _wcparse.raw_escape(pattern, unix, raw_chars, pathname=True)
return _wcparse.escape(util.norm_pattern(pattern, False, raw_chars, True), unix=unix, pathname=True, raw=True)


def escape(pattern, unix=None):
"""Escape."""

return _wcparse.escape(pattern, unix, pathname=True)
return _wcparse.escape(pattern, unix=unix)


def is_magic(pattern, *, flags=0):
Expand Down

0 comments on commit 2737567

Please sign in to comment.