5252from pyfakefs .helpers import IS_PYPY , is_called_from_skipped_module , FSType
5353
5454
55+ _WIN_RESERVED_NAMES = (
56+ {"CON" , "PRN" , "AUX" , "NUL" }
57+ | {"COM%d" % i for i in range (1 , 10 )}
58+ | {"LPT%d" % i for i in range (1 , 10 )}
59+ )
60+
61+
5562def init_module (filesystem ):
5663 """Initializes the fake module with the fake file system."""
5764 # pylint: disable=protected-access
@@ -433,11 +440,6 @@ class _FakeWindowsFlavour(_FakeFlavour):
433440 implementations independent of FakeFilesystem properties.
434441 """
435442
436- reserved_names = (
437- {"CON" , "PRN" , "AUX" , "NUL" }
438- | {"COM%d" % i for i in range (1 , 10 )}
439- | {"LPT%d" % i for i in range (1 , 10 )}
440- )
441443 sep = "\\ "
442444 altsep = "/"
443445 has_drv = True
@@ -455,7 +457,7 @@ def is_reserved(self, parts):
455457 if self .filesystem .is_windows_fs and parts [0 ].startswith ("\\ \\ " ):
456458 # UNC paths are never reserved
457459 return False
458- return parts [- 1 ].partition ("." )[0 ].upper () in self . reserved_names
460+ return parts [- 1 ].partition ("." )[0 ].upper () in _WIN_RESERVED_NAMES
459461
460462 def make_uri (self , path ):
461463 """Return a file URI for the given path"""
@@ -848,33 +850,15 @@ def touch(self, mode=0o666, exist_ok=True):
848850 fake_file .close ()
849851 self .chmod (mode )
850852
851- if sys .version_info >= (3 , 12 ):
852- """These are reimplemented for now because the original implementation
853- checks the flavour against ntpath/posixpath.
854- """
855853
856- def is_absolute (self ):
857- if self .filesystem .is_windows_fs :
858- return self .drive and self .root
859- return os .path .isabs (self ._path ())
860-
861- def is_reserved (self ):
862- if sys .version_info >= (3 , 13 ):
863- warnings .warn (
864- "pathlib.PurePath.is_reserved() is deprecated and scheduled "
865- "for removal in Python 3.15. Use os.path.isreserved() to detect "
866- "reserved paths on Windows." ,
867- DeprecationWarning ,
868- )
869- if not self .filesystem .is_windows_fs :
870- return False
871- if sys .version_info < (3 , 13 ):
872- if not self ._tail or self ._tail [0 ].startswith ("\\ \\ " ):
873- # UNC paths are never reserved.
874- return False
875- name = self ._tail [- 1 ].partition ("." )[0 ].partition (":" )[0 ].rstrip (" " )
876- return name .upper () in pathlib ._WIN_RESERVED_NAMES
877- return self .filesystem .isreserved (self ._path ())
854+ def _warn_is_reserved_deprecated ():
855+ if sys .version_info >= (3 , 13 ):
856+ warnings .warn (
857+ "pathlib.PurePath.is_reserved() is deprecated and scheduled "
858+ "for removal in Python 3.15. Use os.path.isreserved() to detect "
859+ "reserved paths on Windows." ,
860+ DeprecationWarning ,
861+ )
878862
879863
880864class FakePathlibModule :
@@ -900,12 +884,47 @@ class PurePosixPath(PurePath):
900884 paths"""
901885
902886 __slots__ = ()
887+ if sys .version_info >= (3 , 12 ):
888+
889+ def is_reserved (self ):
890+ _warn_is_reserved_deprecated ()
891+ return False
892+
893+ def is_absolute (self ):
894+ with os .path .filesystem .use_fs_type (FSType .POSIX ): # type: ignore[module-attr]
895+ return os .path .isabs (self )
896+
897+ def joinpath (self , * pathsegments ):
898+ with os .path .filesystem .use_fs_type (FSType .POSIX ): # type: ignore[module-attr]
899+ return super ().joinpath (* pathsegments )
903900
904901 class PureWindowsPath (PurePath ):
905902 """A subclass of PurePath, that represents Windows filesystem paths"""
906903
907904 __slots__ = ()
908905
906+ if sys .version_info >= (3 , 12 ):
907+ """These are reimplemented because the PurePath implementation
908+ checks the flavour against ntpath/posixpath.
909+ """
910+
911+ def is_reserved (self ):
912+ _warn_is_reserved_deprecated ()
913+ if sys .version_info < (3 , 13 ):
914+ if not self ._tail or self ._tail [0 ].startswith ("\\ \\ " ):
915+ # UNC paths are never reserved.
916+ return False
917+ name = (
918+ self ._tail [- 1 ].partition ("." )[0 ].partition (":" )[0 ].rstrip (" " )
919+ )
920+ return name .upper () in _WIN_RESERVED_NAMES
921+ with os .path .filesystem .use_fs_type (FSType .WINDOWS ): # type: ignore[module-attr]
922+ return os .path .isreserved (self )
923+
924+ def is_absolute (self ):
925+ with os .path .filesystem .use_fs_type (FSType .WINDOWS ):
926+ return bool (self .drive and self .root )
927+
909928 class WindowsPath (FakePath , PureWindowsPath ):
910929 """A subclass of Path and PureWindowsPath that represents
911930 concrete Windows filesystem paths.
0 commit comments