From 43fd6ab113257130c0be3fa60eea726d330c2570 Mon Sep 17 00:00:00 2001 From: mrbean-bremen Date: Sat, 5 Oct 2024 15:51:04 +0200 Subject: [PATCH] Patch is_reserved for Windows/PosixPurePath - had been only patched for pathlib.Path --- pyfakefs/fake_pathlib.py | 76 +++++++++++++++++++++++----------------- 1 file changed, 44 insertions(+), 32 deletions(-) diff --git a/pyfakefs/fake_pathlib.py b/pyfakefs/fake_pathlib.py index b9a1e773..a7106c94 100644 --- a/pyfakefs/fake_pathlib.py +++ b/pyfakefs/fake_pathlib.py @@ -52,6 +52,13 @@ from pyfakefs.helpers import IS_PYPY, is_called_from_skipped_module, FSType +_WIN_RESERVED_NAMES = ( + {"CON", "PRN", "AUX", "NUL"} + | {"COM%d" % i for i in range(1, 10)} + | {"LPT%d" % i for i in range(1, 10)} +) + + def init_module(filesystem): """Initializes the fake module with the fake file system.""" # pylint: disable=protected-access @@ -433,11 +440,6 @@ class _FakeWindowsFlavour(_FakeFlavour): implementations independent of FakeFilesystem properties. """ - reserved_names = ( - {"CON", "PRN", "AUX", "NUL"} - | {"COM%d" % i for i in range(1, 10)} - | {"LPT%d" % i for i in range(1, 10)} - ) sep = "\\" altsep = "/" has_drv = True @@ -455,7 +457,7 @@ def is_reserved(self, parts): if self.filesystem.is_windows_fs and parts[0].startswith("\\\\"): # UNC paths are never reserved return False - return parts[-1].partition(".")[0].upper() in self.reserved_names + return parts[-1].partition(".")[0].upper() in self._WIN_RESERVED_NAMES def make_uri(self, path): """Return a file URI for the given path""" @@ -848,33 +850,15 @@ def touch(self, mode=0o666, exist_ok=True): fake_file.close() self.chmod(mode) - if sys.version_info >= (3, 12): - """These are reimplemented for now because the original implementation - checks the flavour against ntpath/posixpath. - """ - def is_absolute(self): - if self.filesystem.is_windows_fs: - return self.drive and self.root - return os.path.isabs(self._path()) - - def is_reserved(self): - if sys.version_info >= (3, 13): - warnings.warn( - "pathlib.PurePath.is_reserved() is deprecated and scheduled " - "for removal in Python 3.15. Use os.path.isreserved() to detect " - "reserved paths on Windows.", - DeprecationWarning, - ) - if not self.filesystem.is_windows_fs: - return False - if sys.version_info < (3, 13): - if not self._tail or self._tail[0].startswith("\\\\"): - # UNC paths are never reserved. - return False - name = self._tail[-1].partition(".")[0].partition(":")[0].rstrip(" ") - return name.upper() in pathlib._WIN_RESERVED_NAMES - return self.filesystem.isreserved(self._path()) +def _warn_is_reserved_deprecated(): + if sys.version_info >= (3, 13): + warnings.warn( + "pathlib.PurePath.is_reserved() is deprecated and scheduled " + "for removal in Python 3.15. Use os.path.isreserved() to detect " + "reserved paths on Windows.", + DeprecationWarning, + ) class FakePathlibModule: @@ -900,12 +884,40 @@ class PurePosixPath(PurePath): paths""" __slots__ = () + if sys.version_info >= (3, 12): + + def is_reserved(self): + _warn_is_reserved_deprecated() + return False + + def is_absolute(self): + return os.path.isabs(self) class PureWindowsPath(PurePath): """A subclass of PurePath, that represents Windows filesystem paths""" __slots__ = () + if sys.version_info >= (3, 12): + """These are reimplemented because the PurePath implementation + checks the flavour against ntpath/posixpath. + """ + + def is_reserved(self): + _warn_is_reserved_deprecated() + if sys.version_info < (3, 13) or os.name != "nt": + if not self._tail or self._tail[0].startswith("\\\\"): + # UNC paths are never reserved. + return False + name = ( + self._tail[-1].partition(".")[0].partition(":")[0].rstrip(" ") + ) + return name.upper() in _WIN_RESERVED_NAMES + return os.path.isreserved(self) + + def is_absolute(self): + return bool(self.drive and self.root) + class WindowsPath(FakePath, PureWindowsPath): """A subclass of Path and PureWindowsPath that represents concrete Windows filesystem paths.