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
Next Next commit
Remove flavour.is_reserved()
  • Loading branch information
barneygale committed May 14, 2021
commit 3655d5ef6ae3923929a9ba6d3185962ea55b1f35
43 changes: 21 additions & 22 deletions Lib/pathlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,12 +123,6 @@ class _WindowsFlavour(_Flavour):
drive_letters = set('abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ')
ext_namespace_prefix = '\\\\?\\'

reserved_names = (
{'CON', 'PRN', 'AUX', 'NUL'} |
{'COM%d' % i for i in range(1, 10)} |
{'LPT%d' % i for i in range(1, 10)}
)

# Interesting findings about extended paths:
# - '\\?\c:\a', '//?/c:\a' and '//?/c:/a' are all supported
# but '\\?\c:/a' is not
Expand Down Expand Up @@ -193,18 +187,6 @@ def _split_extended_path(self, s, ext_prefix=ext_namespace_prefix):
s = '\\' + s[3:]
return prefix, s

def is_reserved(self, parts):
# NOTE: the rules for reserved names seem somewhat complicated
# (e.g. r"..\NUL" is reserved but not r"foo\NUL").
# We err on the side of caution and return True for paths which are
# not considered reserved by Windows.
if not parts:
return False
if parts[0].startswith('\\\\'):
# UNC paths are never reserved
return False
return parts[-1].partition('.')[0].upper() in self.reserved_names

def make_uri(self, path):
# Under Windows, file URIs use the UTF-8 encoding.
drive = path.drive
Expand Down Expand Up @@ -250,9 +232,6 @@ def casefold_parts(self, parts):
def compile_pattern(self, pattern):
return re.compile(fnmatch.translate(pattern)).fullmatch

def is_reserved(self, parts):
return False

def make_uri(self, path):
# We represent the path using the local filesystem encoding,
# for portability to other applications.
Expand Down Expand Up @@ -875,7 +854,7 @@ def is_absolute(self):
def is_reserved(self):
"""Return True if the path contains one of the special names reserved
by the system, if any."""
return self._flavour.is_reserved(self._parts)
raise NotImplementedError

def match(self, path_pattern):
"""
Expand Down Expand Up @@ -916,6 +895,9 @@ class PurePosixPath(PurePath):
_flavour = _posix_flavour
__slots__ = ()

def is_reserved(self):
return False


class PureWindowsPath(PurePath):
"""PurePath subclass for Windows systems.
Expand All @@ -924,8 +906,25 @@ class PureWindowsPath(PurePath):
However, you can also instantiate it directly on any system.
"""
_flavour = _windows_flavour
_reserved_names = (
{'CON', 'PRN', 'AUX', 'NUL'} |
{'COM%d' % i for i in range(1, 10)} |
{'LPT%d' % i for i in range(1, 10)}
)
__slots__ = ()

def is_reserved(self):
# NOTE: the rules for reserved names seem somewhat complicated
# (e.g. r"..\NUL" is reserved but not r"foo\NUL").
# We err on the side of caution and return True for paths which are
# not considered reserved by Windows.
if not self._parts:
return False
if self._parts[0].startswith('\\\\'):
# UNC paths are never reserved
return False
return self._parts[-1].partition('.')[0].upper() in self._reserved_names


# Filesystem-accessing classes

Expand Down
0