diff options
author | Barney Gale <barney.gale@gmail.com> | 2024-01-26 18:14:24 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-01-26 18:14:24 (GMT) |
commit | 7e31d6dea276ac91402aefb023c58d239dfd9246 (patch) | |
tree | 3a32adf17e7fa06baa399363f57a079d4f631f24 /Lib/pathlib | |
parent | 6c2b419fb91c8d7daa769d39f73768114b5eb45a (diff) | |
download | cpython-7e31d6dea276ac91402aefb023c58d239dfd9246.zip cpython-7e31d6dea276ac91402aefb023c58d239dfd9246.tar.gz cpython-7e31d6dea276ac91402aefb023c58d239dfd9246.tar.bz2 |
gh-88569: add `ntpath.isreserved()` (#95486)
Add `ntpath.isreserved()`, which identifies reserved pathnames such as "NUL", "AUX" and "CON".
Deprecate `pathlib.PurePath.is_reserved()`.
---------
Co-authored-by: Eryk Sun <eryksun@gmail.com>
Co-authored-by: Brett Cannon <brett@python.org>
Co-authored-by: Steve Dower <steve.dower@microsoft.com>
Diffstat (limited to 'Lib/pathlib')
-rw-r--r-- | Lib/pathlib/__init__.py | 28 |
1 files changed, 7 insertions, 21 deletions
diff --git a/Lib/pathlib/__init__.py b/Lib/pathlib/__init__.py index eee82ef..cc159ed 100644 --- a/Lib/pathlib/__init__.py +++ b/Lib/pathlib/__init__.py @@ -33,15 +33,6 @@ __all__ = [ ] -# Reference for Windows paths can be found at -# https://learn.microsoft.com/en-gb/windows/win32/fileio/naming-a-file . -_WIN_RESERVED_NAMES = frozenset( - {'CON', 'PRN', 'AUX', 'NUL', 'CONIN$', 'CONOUT$'} | - {f'COM{c}' for c in '123456789\xb9\xb2\xb3'} | - {f'LPT{c}' for c in '123456789\xb9\xb2\xb3'} -) - - class _PathParents(Sequence): """This object provides sequence-like access to the logical ancestors of a path. Don't try to construct it yourself.""" @@ -433,18 +424,13 @@ class PurePath(_abc.PurePathBase): def is_reserved(self): """Return True if the path contains one of the special names reserved by the system, if any.""" - if self.pathmod is not ntpath or not self.name: - return False - - # NOTE: the rules for reserved names seem somewhat complicated - # (e.g. r"..\NUL" is reserved but not r"foo\NUL" if "foo" does not - # exist). We err on the side of caution and return True for paths - # which are not considered reserved by Windows. - if self.drive.startswith('\\\\'): - # UNC paths are never reserved. - return False - name = self.name.partition('.')[0].partition(':')[0].rstrip(' ') - return name.upper() in _WIN_RESERVED_NAMES + msg = ("pathlib.PurePath.is_reserved() is deprecated and scheduled " + "for removal in Python 3.15. Use os.path.isreserved() to " + "detect reserved paths on Windows.") + warnings.warn(msg, DeprecationWarning, stacklevel=2) + if self.pathmod is ntpath: + return self.pathmod.isreserved(self) + return False def as_uri(self): """Return the path as a URI.""" |