Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Undo posixpath changes
  • Loading branch information
barneygale committed Jan 8, 2024
commit 3fb127fbcc4d4eaf26fe833877ef862c40113c38
3 changes: 1 addition & 2 deletions Doc/library/os.path.rst
Original file line number Diff line number Diff line change
Expand Up @@ -305,8 +305,7 @@ the :mod:`glob` module.)
as well as DOS device names such as "NUL", "CON", "CONIN$", "CONOUT$",
"AUX", "PRN", "COM1", and "LPT1".

On POSIX platforms, paths that contains NUL characters are reserved; all
other paths are unreserved.
.. availability:: Windows.

.. versionadded:: 3.12

Expand Down
16 changes: 6 additions & 10 deletions Doc/library/pathlib.rst
Original file line number Diff line number Diff line change
Expand Up @@ -505,26 +505,23 @@ Pure paths provide the following methods and properties:

.. method:: PurePath.is_reserved()

Return ``True`` if the path is considered reserved, ``False`` otherwise.
With :class:`PureWindowsPath`, return ``True`` if the path is considered
reserved under Windows, ``False`` otherwise. With :class:`PurePosixPath`,
``False`` is always returned.


>>> PureWindowsPath('nul').is_reserved()
True
>>> PurePosixPath('\x00').is_reserved()
True
>>> PurePosixPath('nul').is_reserved()
False

File system calls on reserved paths can fail mysteriously or have
unintended effects.

.. seealso::
:func:`os.path.isreserved`, which is used to implement this method.

.. versionchanged: 3.12
Windows path names that contain a colon, or end with a dot or a space,
are considered reserved. UNC paths may be reserved.

.. versionchanged: 3.12
POSIX path names that contain a NUL character are considered reserved.


.. method:: PurePath.joinpath(*other)

Expand Down Expand Up @@ -1400,7 +1397,6 @@ 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
16 changes: 2 additions & 14 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","isreserved","expanduser","expandvars","normpath",
"abspath","samefile","sameopenfile","samestat",
"ismount", "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,18 +216,6 @@ def ismount(path):
return False


def isreserved(path):
"""Return true if the pathname is reserved by the system."""
path = os.fsencode(path)
return b'\0' in path


def isreservedname(name):
"""Return true if the filename is reserved by the system."""
name = os.fsencode(name)
return b'\0' in name or b'/' in name


# 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
29 changes: 0 additions & 29 deletions Lib/test/test_posixpath.py
Original file line number Diff line number Diff line change
Expand Up @@ -242,35 +242,6 @@ def fake_lstat(path):
finally:
os.lstat = save_lstat

def test_isreserved(self):
# Regular ol' paths are unreserved.
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'))

# '.' and '..' are unreserved.
self.assertFalse(posixpath.isreserved('.'))
self.assertFalse(posixpath.isreserved(b'.'))
self.assertFalse(posixpath.isreserved('..'))
self.assertFalse(posixpath.isreserved(b'..'))

# Paths reserved on Windows aren't reserved here.
self.assertFalse(posixpath.isreserved('NUL'))
self.assertFalse(posixpath.isreserved(b'NUL'))

# Null bytes ARE reserved
self.assertTrue(posixpath.isreserved('\x00'))
self.assertTrue(posixpath.isreserved(b'\x00'))
self.assertTrue(posixpath.isreserved('ham/eggs\x00beans/tomato'))
self.assertTrue(posixpath.isreserved(b'ham/eggs\x00beans/tomato'))

# Other special characters are not reserved.
self.assertFalse(posixpath.isreserved('\x01'))
self.assertFalse(posixpath.isreserved(b'\x01'))

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
@@ -1,3 +1,2 @@
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``.
Add :func:`ntpath.isreserved`, which identifies reserved pathnames on
Windows; reserved names include "NUL", "AUX" and "CON".