8000 bpo-44136: remove `pathlib._Flavour` by barneygale · Pull Request #26141 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

bpo-44136: remove pathlib._Flavour #26141

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Remove flavour.compile_pattern()
  • Loading branch information
barneygale committed May 15, 2021
commit 66f5453f8eb80bad4406858c48dd5662936a0070
36 changes: 17 additions & 19 deletions Lib/pathlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,6 @@ def casefold(self, s):
def casefold_parts(self, parts):
return [p.lower() for p in parts]

def compile_pattern(self, pattern):
return re.compile(fnmatch.translate(pattern), re.IGNORECASE).fullmatch


class _PosixFlavour(_Flavour):
has_drv = False
Expand All @@ -80,9 +77,6 @@ def casefold(self, s):
def casefold_parts(self, parts):
return parts

def compile_pattern(self, pattern):
return re.compile(fnmatch.translate(pattern)).fullmatch


_windows_flavour = _WindowsFlavour()
_posix_flavour = _PosixFlavour()
Expand Down Expand Up @@ -179,7 +173,7 @@ def group(self, path):
# Globbing helpers
#

def _make_selector(pattern_parts, flavour):
def _make_selector(pattern_parts, case_insensitive):
pat = pattern_parts[0]
child_parts = pattern_parts[1:]
if pat == '**':
Expand All @@ -190,7 +184,7 @@ def _make_selector(pattern_parts, flavour):
cls = _WildcardSelector
else:
cls = _PreciseSelector
return cls(pat, child_parts, flavour)
return cls(pat, child_parts, case_insensitive)

if hasattr(functools, "lru_cache"):
_make_selector = functools.lru_cache()(_make_selector)
Expand All @@ -200,10 +194,10 @@ class _Selector:
"""A selector matches a specific glob pattern part against the children
of a given path."""

def __init__(self, child_parts, flavour):
def __init__(self, child_parts, case_insensitive):
self.child_parts = child_parts
if child_parts:
self.successor = _make_selector(child_parts, flavour)
self.successor = _make_selector(child_parts, case_insensitive)
self.dironly = True
else:
self.successor = _TerminatingSelector()
Expand All @@ -229,9 +223,9 @@ def _select_from(self, parent_path, is_dir, exists, scandir):

class _PreciseSelector(_Selector):

def __init__(self, name, child_parts, flavour):
def __init__(self, name, child_parts, case_insensitive):
self.name = name
_Selector.__init__(self, child_parts, flavour)
_Selector.__init__(self, child_parts, case_insensitive)

def _select_from(self, parent_path, is_dir, exists, scandir):
try:
Expand All @@ -245,9 +239,10 @@ def _select_from(self, parent_path, is_dir, exists, scandir):

class _WildcardSelector(_Selector):

def __init__(self, pat, child_parts, flavour):
self.match = flavour.compile_pattern(pat)
_Selector.__init__(self, child_parts, flavour)
def __init__(self, pat, child_parts, case_insensitive):
flags = re.IGNORECASE if case_insensitive else 0
self.match = re.compile(fnmatch.translate(pat), flags=flags).fullmatch
_Selector.__init__(self, child_parts, case_insensitive)

def _select_from(self, parent_path, is_dir, exists, scandir):
try:
Expand Down Expand Up @@ -276,8 +271,8 @@ def _select_from(self, parent_path, is_dir, exists, scandir):

class _RecursiveWildcardSelector(_Selector):

def __init__(self, pat, child_parts, flavour):
_Selector.__init__(self, child_parts, flavour)
def __init__(self, pat, child_parts, case_insensitive):
_Selector.__init__(self, child_parts, case_insensitive)

def _iterate_directories(self, parent_path, is_dir, scandir):
yield parent_path
Expand Down Expand Up @@ -793,6 +788,7 @@ class PurePosixPath(PurePath):
"""
_flavour = _posix_flavour
_pathmod = posixpath
_case_insensitive = False
__slots__ = ()

@classmethod
Expand Down Expand Up @@ -832,6 +828,7 @@ class PureWindowsPath(PurePath):
"""
_flavour = _windows_flavour
_pathmod = ntpath
_case_insensitive = True
_drive_letters = set('abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ')
_ext_namespace_prefix = '\\\\?\\'
_reserved_names = (
Expand Down Expand Up @@ -1016,7 +1013,7 @@ def glob(self, pattern):
drv, root, pattern_parts = self._parse_parts((pattern,))
if drv or root:
raise NotImplementedError("Non-relative patterns are unsupported")
selector = _make_selector(tuple(pattern_parts), self._flavour)
selector = _make_selector(tuple(pattern_parts), self._case_insensitive)
for p in selector.select_from(self):
yield p

Expand All @@ -1029,7 +1026,8 @@ def rglob(self, pattern):
drv, root, pattern_parts = self._parse_parts((pattern,))
if drv or root:
raise NotImplementedError("Non-relative patterns are unsupported")
selector = _make_selector(("**",) + tuple(pattern_parts), self._flavour)
selector = _make_selector(("**",) + tuple(pattern_parts),
self._case_insensitive)
for p in selector.select_from(self):
yield p

Expand Down
0