Skip to content

gh-88569: add ntpath.isreserved() #95486

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

Merged
merged 38 commits into from
Jan 26, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
6b89ecc
gh-88569: add `os.path.isreserved()`
barneygale Jul 31, 2022
3e08e4a
Fix tests
barneygale Jul 31, 2022
49ba439
Remove implementation in `genericpath`
barneygale Jul 31, 2022
238b3e3
Apply suggestions from code review
barneygale Aug 9, 2022
f647502
Apply suggestions from code review
barneygale Aug 10, 2022
f07c7ad
Remove tests for pathlib.PurePath.is_reserved().
barneygale Aug 10, 2022
dc857c9
Speed up isreserved('.') and '..'
barneygale Aug 12, 2022
f0fd2c8
Note change to algorithm in pathlib docs.
barneygale Aug 12, 2022
3b51db8
Update Lib/ntpath.py
barneygale Aug 12, 2022
06cb428
Update Doc/library/os.path.rst
barneygale Aug 13, 2022
79c0be4
Update Lib/posixpath.py
barneygale Aug 13, 2022
0a0db6a
Restore `os.fspath()` call in `posixpath.isreserved()`
barneygale Aug 16, 2022
7145b86
Apply suggestions from code review
barneygale Aug 23, 2022
9f74b64
posixpath.isreserved(): return True for paths with NUL characters
barneygale Aug 23, 2022
91b2bb3
ntpath.isreserved(): minor tweaks
barneygale Aug 23, 2022
e6aff58
ntpath.isreserved(): restore initial splitdrive() call
barneygale Aug 23, 2022
e6a2c0b
ntpath.isreserved(): avoid calling `splitdrive()` repeatedly.
barneygale Aug 23, 2022
14dde15
Update Lib/ntpath.py
barneygale Aug 23, 2022
fab274a
Add `isreservedname()` for discussion.
barneygale Aug 23, 2022
936dcc8
Apply suggestions from code review
barneygale Aug 23, 2022
3fb127f
Undo posixpath changes
barneygale Jan 8, 2024
002d951
Merge branch 'main' into os-path-isreserved
barneygale Jan 8, 2024
c8ed711
Update version numbers.
barneygale Jan 8, 2024
c772b25
Fix syntax, whitespace.
barneygale Jan 8, 2024
3fbef57
Make `isreservedname()` private
barneygale Jan 8, 2024
4b34274
Update Lib/ntpath.py
barneygale Jan 8, 2024
a877677
Tighten up `PurePath.is_reserved()` exception handling.
barneygale Jan 8, 2024
3721c8c
Use `str(self)` to support non-os.PathLike implementations.
barneygale Jan 8, 2024
b905d2f
Deprecate `pathlib.PurePath.is_reserved()`
barneygale Jan 8, 2024
2756ffb
Add note about approximate and changing Windows rules; remove doctest.
barneygale Jan 8, 2024
c03c672
Update Doc/library/os.path.rst
barneygale Jan 8, 2024
4085ff5
Update what's new.
barneygale Jan 8, 2024
b4b3d0b
Mention deprecation in NEWS
barneygale Jan 8, 2024
9e2d21f
Address review feedback
barneygale Jan 8, 2024
44c37cb
Point to `os.path.isreserved()` in whatsnew deprecation notices.
barneygale Jan 8, 2024
f9033b3
Merge branch 'main' into os-path-isreserved
barneygale Jan 8, 2024
efb7681
Merge branch 'main' into os-path-isreserved
barneygale Jan 14, 2024
e398c3f
Address review feedback
barneygale Jan 16, 2024
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
9 changes: 9 additions & 0 deletions Doc/library/os.path.rst
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,15 @@ the :mod:`glob` module.)
Accepts a :term:`path-like object`.


.. function:: isreserved(path)

Return ``True`` if *path* is a reserved pathname on the current system. On
Windows, reserved names include "NUL", "AUX" and "CON". On other platforms,
this function always returns ``False``.

.. versionadded:: 3.12


.. function:: join(path, *paths)

Join one or more path components intelligently. The return value is the
Expand Down
1 change: 1 addition & 0 deletions Doc/library/pathlib.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1392,6 +1392,7 @@ Below is a table mapping various :mod:`os` functions to their corresponding
:meth:`Path.owner`,
:meth:`Path.group`
:func:`os.path.isabs` :meth:`PurePath.is_absolute`
:func:`os.path.isreserved` :meth:`PurePath.is_reserved`
:func:`os.path.join` :func:`PurePath.joinpath`
:func:`os.path.basename` :data:`PurePath.name`
:func:`os.path.dirname` :data:`PurePath.parent`
Expand Down
26 changes: 24 additions & 2 deletions Lib/ntpath.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@
__all__ = ["normcase","isabs","join","splitdrive","split","splitext",
"basename","dirname","commonprefix","getsize","getmtime",
"getatime","getctime", "islink","exists","lexists","isdir","isfile",
"ismount", "expanduser","expandvars","normpath","abspath",
"curdir","pardir","sep","pathsep","defpath","altsep",
"ismount","isreserved","expanduser","expandvars","normpath",
"abspath","curdir","pardir","sep","pathsep","defpath","altsep",
"extsep","devnull","realpath","supports_unicode_filenames","relpath",
"samefile", "sameopenfile", "samestat", "commonpath"]

Expand Down Expand Up @@ -311,6 +311,28 @@ def ismount(path):
return False


_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'}
)
def isreserved(path):
"""Return true if the pathname is reserved by the system."""
# Refer to "Naming Files, Paths, and Namespaces":
# https://docs.microsoft.com/en-us/windows/win32/fileio/naming-a-file
path = os.fspath(path)
name = os.fsdecode(basename(path))
# Trailing spaces and dots are reserved.
# File streams are reserved (e.g. "filename:stream[:type]").
if name.rstrip('. ') != name or ':' in name:
return True
# DOS device names are reserved (e.g. "nul" or "nul .txt"). The rules
# are complicated and vary across Windows versions (e.g. "../nul" is
# reserved but not "foo/nul" if "foo" does not exist). On the side of
# caution, return True for names that may not be reserved.
return name.partition('.')[0].rstrip(' ').upper() in _reserved_names


# Expand paths beginning with '~' or '~user'.
# '~' means $HOME; '~user' means that user's home directory.
# If the path doesn't begin with '~', or if the user or $HOME is unknown,
Expand Down
24 changes: 1 addition & 23 deletions Lib/pathlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,12 +127,6 @@ class _WindowsFlavour(_Flavour):

is_supported = (os.name == 'nt')

reserved_names = (
{'CON', 'PRN', 'AUX', 'NUL', 'CONIN$', 'CONOUT$'} |
{'COM%s' % c for c in '123456789\xb9\xb2\xb3'} |
{'LPT%s' % c for c in '123456789\xb9\xb2\xb3'}
)

def splitroot(self, part, sep=sep):
drv, rest = self.pathmod.splitdrive(part)
if drv[:1] == sep or rest[:1] == sep:
Expand All @@ -149,19 +143,6 @@ def casefold_parts(self, parts):
def compile_pattern(self, pattern):
return re.compile(fnmatch.translate(pattern), re.IGNORECASE).fullmatch

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" 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 not parts:
return False
if parts[0].startswith('\\\\'):
# UNC paths are never reserved
return False
name = parts[-1].partition('.')[0].partition(':')[0].rstrip(' ')
return name.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 @@ -207,9 +188,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 @@ -750,7 +728,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)
return self._flavour.pathmod.isreserved(self)

def match(self, path_pattern):
"""
Expand Down
10 changes: 8 additions & 2 deletions Lib/posixpath.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@
__all__ = ["normcase","isabs","join","splitdrive","split","splitext",
"basename","dirname","commonprefix","getsize","getmtime",
"getatime","getctime","islink","exists","lexists","isdir","isfile",
"ismount", "expanduser","expandvars","normpath","abspath",
"samefile","sameopenfile","samestat",
"ismount","isreserved","expanduser","expandvars","normpath",
"abspath","samefile","sameopenfile","samestat",
"curdir","pardir","sep","pathsep","defpath","altsep","extsep",
"devnull","realpath","supports_unicode_filenames","relpath",
"commonpath"]
Expand Down Expand Up @@ -216,6 +216,12 @@ def ismount(path):
return False


def isreserved(path):
"""Return true if the pathname is reserved by the system."""
os.fspath(path)
return False


# Expand paths beginning with '~' or '~user'.
# '~' means $HOME; '~user' means that user's home directory.
# If the path doesn't begin with '~', or if the user or $HOME is unknown,
Expand Down
43 changes: 43 additions & 0 deletions Lib/test/test_ntpath.py
Original file line number Diff line number Diff line change
Expand Up @@ -822,6 +822,49 @@ def test_ismount(self):
self.assertTrue(ntpath.ismount(b"\\\\localhost\\c$"))
self.assertTrue(ntpath.ismount(b"\\\\localhost\\c$\\"))

def test_isreserved(self):
self.assertFalse(ntpath.isreserved(''))
self.assertFalse(ntpath.isreserved('/'))
self.assertFalse(ntpath.isreserved('/foo/bar'))
# A name that ends with a space or dot is reserved.
self.assertTrue(ntpath.isreserved('foo.'))
self.assertTrue(ntpath.isreserved('foo '))
# A name with a file stream is reserved.
self.assertTrue(ntpath.isreserved('foo:bar'))
# Case-insensitive DOS-device names are reserved.
self.assertTrue(ntpath.isreserved('nul'))
self.assertTrue(ntpath.isreserved('aux'))
self.assertTrue(ntpath.isreserved('prn'))
self.assertTrue(ntpath.isreserved('con'))
self.assertTrue(ntpath.isreserved('conin$'))
self.assertTrue(ntpath.isreserved('conout$'))
# COM/LPT + 1-9 or + superscript 1-3 are reserved.
self.assertTrue(ntpath.isreserved('COM1'))
self.assertTrue(ntpath.isreserved('LPT9'))
self.assertTrue(ntpath.isreserved('com\xb9'))
self.assertTrue(ntpath.isreserved('com\xb2'))
self.assertTrue(ntpath.isreserved('lpt\xb3'))
# DOS-device name matching ignores characters after a dot or
# a colon and also ignores trailing spaces.
self.assertTrue(ntpath.isreserved('NUL.txt'))
self.assertTrue(ntpath.isreserved('PRN '))
self.assertTrue(ntpath.isreserved('AUX .txt'))
self.assertTrue(ntpath.isreserved('COM1:bar'))
self.assertTrue(ntpath.isreserved('LPT9 :bar'))
# DOS-device names are only matched at the beginning
# of a path component.
self.assertFalse(ntpath.isreserved('bar.com9'))
self.assertFalse(ntpath.isreserved('bar.lpt9'))
# Only the last path component matters.
self.assertTrue(ntpath.isreserved('c:/baz/con/NUL'))
self.assertFalse(ntpath.isreserved('c:/NUL/con/baz'))
# Bytes are supported.
self.assertFalse(ntpath.isreserved(b''))
self.assertFalse(ntpath.isreserved(b'/'))
self.assertFalse(ntpath.isreserved(b'/foo/bar'))
self.assertTrue(ntpath.isreserved(b'foo.'))
self.assertTrue(ntpath.isreserved(b'nul'))

def assertEqualCI(self, s1, s2):
"""Assert that two strings are equal ignoring case differences."""
self.assertEqual(s1.lower(), s2.lower())
Expand Down
10 changes: 10 additions & 0 deletions Lib/test/test_posixpath.py
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,16 @@ def fake_lstat(path):
finally:
os.lstat = save_lstat

def test_isreserved(self):
self.assertFalse(posixpath.isreserved(''))
self.assertFalse(posixpath.isreserved(b''))
self.assertFalse(posixpath.isreserved('/'))
self.assertFalse(posixpath.isreserved(b'/'))
self.assertFalse(posixpath.isreserved('hi'))
self.assertFalse(posixpath.isreserved(b'hi'))
self.assertFalse(posixpath.isreserved('NUL'))
self.assertFalse(posixpath.isreserved(b'NUL'))

def test_expanduser(self):
self.assertEqual(posixpath.expanduser("foo"), "foo")
self.assertEqual(posixpath.expanduser(b"foo"), b"foo")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Add :func:`os.path.isreserved`, which identifies reserved pathnames. On
Windows, reserved names include "NUL", "AUX" and "CON". On other platforms,
this function always returns ``False``.