@@ -101,7 +101,7 @@ def dir() -> List[str]:
101
101
"samefile" ,
102
102
]
103
103
if sys .version_info >= (3 , 12 ):
104
- dir_list . append ( "isjunction" )
104
+ dir_list += [ "isjunction" , "splitroot" ]
105
105
return dir_list
106
106
107
107
def __init__ (self , filesystem : "FakeFilesystem" , os_module : "FakeOsModule" ):
@@ -198,6 +198,62 @@ def isjunction(self, path: AnyStr) -> bool:
198
198
"""Returns False. Junctions are never faked."""
199
199
return self .filesystem .isjunction (path )
200
200
201
+ def splitroot (self , path : AnyStr ):
202
+ """Split a pathname into drive, root and tail.
203
+ Implementation taken from ntpath and posixpath.
204
+ """
205
+ p = os .fspath (path )
206
+ if isinstance (p , bytes ):
207
+ sep = self .filesystem .path_separator .encode ()
208
+ altsep = None
209
+ if self .filesystem .alternative_path_separator :
210
+ altsep = self .filesystem .alternative_path_separator .encode ()
211
+ colon = b":"
212
+ unc_prefix = b"\\ \\ ?\\ UNC\\ "
213
+ empty = b""
214
+ else :
215
+ sep = self .filesystem .path_separator
216
+ altsep = self .filesystem .alternative_path_separator
217
+ colon = ":"
218
+ unc_prefix = "\\ \\ ?\\ UNC\\ "
219
+ empty = ""
220
+ if self .filesystem .is_windows_fs :
221
+ normp = p .replace (altsep , sep ) if altsep else p
222
+ if normp [:1 ] == sep :
223
+ if normp [1 :2 ] == sep :
224
+ # UNC drives, e.g. \\server\share or \\?\UNC\server\share
225
+ # Device drives, e.g. \\.\device or \\?\device
226
+ start = 8 if normp [:8 ].upper () == unc_prefix else 2
227
+ index = normp .find (sep , start )
228
+ if index == - 1 :
229
+ return p , empty , empty
230
+ index2 = normp .find (sep , index + 1 )
231
+ if index2 == - 1 :
232
+ return p , empty , empty
233
+ return p [:index2 ], p [index2 : index2 + 1 ], p [index2 + 1 :]
234
+ else :
235
+ # Relative path with root, e.g. \Windows
236
+ return empty , p [:1 ], p [1 :]
237
+ elif normp [1 :2 ] == colon :
238
+ if normp [2 :3 ] == sep :
239
+ # Absolute drive-letter path, e.g. X:\Windows
240
+ return p [:2 ], p [2 :3 ], p [3 :]
241
+ else :
242
+ # Relative path with drive, e.g. X:Windows
243
+ return p [:2 ], empty , p [2 :]
244
+ else :
245
+ # Relative path, e.g. Windows
246
+ return empty , empty , p
247
+ else :
248
+ if p [:1 ] != sep :
249
+ # Relative path, e.g.: 'foo'
250
+ return empty , empty , p
251
+ elif p [1 :2 ] != sep or p [2 :3 ] == sep :
252
+ # Absolute path, e.g.: '/foo', '///foo', '////foo', etc.
253
+ return empty , sep , p [1 :]
254
+ else :
255
+ return empty , p [:2 ], p [2 :]
256
+
201
257
def getmtime (self , path : AnyStr ) -> float :
202
258
"""Returns the modification time of the fake file.
203
259
@@ -473,3 +529,52 @@ def ismount(self, path: AnyStr) -> bool:
473
529
def __getattr__ (self , name : str ) -> Any :
474
530
"""Forwards any non-faked calls to the real os.path."""
475
531
return getattr (self ._os_path , name )
532
+
533
+
534
+ if sys .platform == "win32" :
535
+
536
+ class FakeNtModule :
537
+ """Under windows, a few function of `os.path` are taken from the `nt` module
538
+ for performance reasons. These are patched here.
539
+ """
540
+
541
+ @staticmethod
542
+ def dir ():
543
+ if sys .version_info >= (3 , 12 ):
544
+ return ["_path_exists" , "_path_isfile" , "_path_isdir" , "_path_islink" ]
545
+ else :
546
+ return ["_isdir" ]
547
+
548
+ def __init__ (self , filesystem : "FakeFilesystem" ):
549
+ """Init.
550
+
551
+ Args:
552
+ filesystem: FakeFilesystem used to provide file system information
553
+ """
554
+ import nt
555
+
556
+ self .filesystem = filesystem
557
+ self .nt_module : Any = nt
558
+
559
+ if sys .version_info >= (3 , 12 ):
560
+
561
+ def _path_isdir (self , path : AnyStr ) -> bool :
562
+ return self .filesystem .isdir (path )
563
+
564
+ def _path_isfile (self , path : AnyStr ) -> bool :
565
+ return self .filesystem .isfile (path )
566
+
567
+ def _path_islink (self , path : AnyStr ) -> bool :
568
+ return self .filesystem .islink (path )
569
+
570
+ def _path_exists (self , path : AnyStr ) -> bool :
571
+ return self .filesystem .exists (path )
572
+
573
+ else :
574
+
575
+ def _isdir (self , path : AnyStr ) -> bool :
576
+ return self .filesystem .isdir (path )
577
+
578
+ def __getattr__ (self , name : str ) -> Any :
579
+ """Forwards any non-faked calls to the real nt module."""
580
+ return getattr (self .nt_module , name )
0 commit comments