52
52
from pyfakefs .helpers import IS_PYPY , is_called_from_skipped_module , FSType
53
53
54
54
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
+
55
62
def init_module (filesystem ):
56
63
"""Initializes the fake module with the fake file system."""
57
64
# pylint: disable=protected-access
@@ -433,11 +440,6 @@ class _FakeWindowsFlavour(_FakeFlavour):
433
440
implementations independent of FakeFilesystem properties.
434
441
"""
435
442
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
- )
441
443
sep = "\\ "
442
444
altsep = "/"
443
445
has_drv = True
@@ -455,7 +457,7 @@ def is_reserved(self, parts):
455
457
if self .filesystem .is_windows_fs and parts [0 ].startswith ("\\ \\ " ):
456
458
# UNC paths are never reserved
457
459
return False
458
- return parts [- 1 ].partition ("." )[0 ].upper () in self . reserved_names
460
+ return parts [- 1 ].partition ("." )[0 ].upper () in _WIN_RESERVED_NAMES
459
461
460
462
def make_uri (self , path ):
461
463
"""Return a file URI for the given path"""
@@ -848,33 +850,15 @@ def touch(self, mode=0o666, exist_ok=True):
848
850
fake_file .close ()
849
851
self .chmod (mode )
850
852
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
- """
855
853
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
+ )
878
862
879
863
880
864
class FakePathlibModule :
@@ -900,12 +884,47 @@ class PurePosixPath(PurePath):
900
884
paths"""
901
885
902
886
__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 )
903
900
904
901
class PureWindowsPath (PurePath ):
905
902
"""A subclass of PurePath, that represents Windows filesystem paths"""
906
903
907
904
__slots__ = ()
908
905
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
+
909
928
class WindowsPath (FakePath , PureWindowsPath ):
910
929
"""A subclass of Path and PureWindowsPath that represents
911
930
concrete Windows filesystem paths.
0 commit comments