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>
This commit is contained in:
Barney Gale 2024-01-26 18:14:24 +00:00 • committed by GitHub
parent 6c2b419fb9
commit 7e31d6dea2
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
8 changed files with 154 additions and 72 deletions

View file

@ -326,6 +326,28 @@ the :mod:`glob` module.)
.. versionadded:: 3.12
.. function:: isreserved(path)
Return ``True`` if *path* is a reserved pathname on the current system.
On Windows, reserved filenames include those that end with a space or dot;
those that contain colons (i.e. file streams such as "name:stream"),
wildcard characters (i.e. ``'*?"<>'``), pipe, or ASCII control characters;
as well as DOS device names such as "NUL", "CON", "CONIN$", "CONOUT$",
"AUX", "PRN", "COM1", and "LPT1".
.. note::
This function approximates rules for reserved paths on most Windows
systems. These rules change over time in various Windows releases.
This function may be updated in future Python releases as changes to
the rules become broadly available.
.. availability:: Windows.
.. versionadded:: 3.13
.. function:: join(path, *paths)
Join one or more path segments intelligently. The return value is the

View file

@ -535,14 +535,13 @@ Pure paths provide the following methods and properties:
reserved under Windows, ``False`` otherwise. With :class:`PurePosixPath`,
``False`` is always returned.
>>> PureWindowsPath('nul').is_reserved()
True
>>> PurePosixPath('nul').is_reserved()
False
File system calls on reserved paths can fail mysteriously or have
unintended effects.
.. versionchanged:: 3.13
Windows path names that contain a colon, or end with a dot or a space,
are considered reserved. UNC paths may be reserved.
.. deprecated-removed:: 3.13 3.15
This method is deprecated; use :func:`os.path.isreserved` to detect
reserved paths on Windows.
.. method:: PurePath.joinpath(*pathsegments)