2222altsep = None
2323devnull = '/dev/null'
2424
25- try :
26- import os
27- except ImportError :
28- import _dummy_os as os
25+ import os
2926import sys
3027import stat
3128import genericpath
3229from genericpath import *
3330
34- __all__ = ["normcase" ,"isabs" ,"join" ,"splitdrive" ,"split" ,"splitext" ,
31+ __all__ = ["normcase" ,"isabs" ,"join" ,"splitdrive" ,"splitroot" , " split" ,"splitext" ,
3532 "basename" ,"dirname" ,"commonprefix" ,"getsize" ,"getmtime" ,
3633 "getatime" ,"getctime" ,"islink" ,"exists" ,"lexists" ,"isdir" ,"isfile" ,
3734 "ismount" , "expanduser" ,"expandvars" ,"normpath" ,"abspath" ,
3835 "samefile" ,"sameopenfile" ,"samestat" ,
3936 "curdir" ,"pardir" ,"sep" ,"pathsep" ,"defpath" ,"altsep" ,"extsep" ,
4037 "devnull" ,"realpath" ,"supports_unicode_filenames" ,"relpath" ,
41- "commonpath" ]
38+ "commonpath" , "isjunction" ]
4239
4340
4441def _get_sep (path ):
@@ -138,6 +135,35 @@ def splitdrive(p):
138135 return p [:0 ], p
139136
140137
138+ def splitroot (p ):
139+ """Split a pathname into drive, root and tail. On Posix, drive is always
140+ empty; the root may be empty, a single slash, or two slashes. The tail
141+ contains anything after the root. For example:
142+
143+ splitroot('foo/bar') == ('', '', 'foo/bar')
144+ splitroot('/foo/bar') == ('', '/', 'foo/bar')
145+ splitroot('//foo/bar') == ('', '//', 'foo/bar')
146+ splitroot('///foo/bar') == ('', '/', '//foo/bar')
147+ """
148+ p = os .fspath (p )
149+ if isinstance (p , bytes ):
150+ sep = b'/'
151+ empty = b''
152+ else :
153+ sep = '/'
154+ empty = ''
155+ if p [:1 ] != sep :
156+ # Relative path, e.g.: 'foo'
157+ return empty , empty , p
158+ elif p [1 :2 ] != sep or p [2 :3 ] == sep :
159+ # Absolute path, e.g.: '/foo', '///foo', '////foo', etc.
160+ return empty , sep , p [1 :]
161+ else :
162+ # Precisely two leading slashes, e.g.: '//foo'. Implementation defined per POSIX, see
163+ # https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap04.html#tag_04_13
164+ return empty , p [:2 ], p [2 :]
165+
166+
141167# Return the tail (basename) part of a path, same as split(path)[1].
142168
143169def basename (p ):
@@ -161,16 +187,14 @@ def dirname(p):
161187 return head
162188
163189
164- # Is a path a symbolic link?
165- # This will always return false on systems where os.lstat doesn't exist.
190+ # Is a path a junction?
191+
192+ def isjunction (path ):
193+ """Test whether a path is a junction
194+ Junctions are not a part of posix semantics"""
195+ os .fspath (path )
196+ return False
166197
167- def islink (path ):
168- """Test whether a path is a symbolic link"""
169- try :
170- st = os .lstat (path )
171- except (OSError , ValueError , AttributeError ):
172- return False
173- return stat .S_ISLNK (st .st_mode )
174198
175199# Being true for dangling symbolic links is also useful.
176200
@@ -198,6 +222,7 @@ def ismount(path):
198222 if stat .S_ISLNK (s1 .st_mode ):
199223 return False
200224
225+ path = os .fspath (path )
201226 if isinstance (path , bytes ):
202227 parent = join (path , b'..' )
203228 else :
@@ -244,7 +269,11 @@ def expanduser(path):
244269 i = len (path )
245270 if i == 1 :
246271 if 'HOME' not in os .environ :
247- import pwd
272+ try :
273+ import pwd
274+ except ImportError :
275+ # pwd module unavailable, return path unchanged
276+ return path
248277 try :
249278 userhome = pwd .getpwuid (os .getuid ()).pw_dir
250279 except KeyError :
@@ -254,7 +283,11 @@ def expanduser(path):
254283 else :
255284 userhome = os .environ ['HOME' ]
256285 else :
257- import pwd
286+ try :
287+ import pwd
288+ except ImportError :
289+ # pwd module unavailable, return path unchanged
290+ return path
258291 name = path [1 :i ]
259292 if isinstance (name , bytes ):
260293 name = str (name , 'ASCII' )
@@ -337,43 +370,47 @@ def expandvars(path):
337370# It should be understood that this may change the meaning of the path
338371# if it contains symbolic links!
339372
340- def normpath (path ):
341- """Normalize path, eliminating double slashes, etc."""
342- path = os .fspath (path )
343- if isinstance (path , bytes ):
344- sep = b'/'
345- empty = b''
346- dot = b'.'
347- dotdot = b'..'
348- else :
349- sep = '/'
350- empty = ''
351- dot = '.'
352- dotdot = '..'
353- if path == empty :
354- return dot
355- initial_slashes = path .startswith (sep )
356- # POSIX allows one or two initial slashes, but treats three or more
357- # as single slash.
358- # (see http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap04.html#tag_04_13)
359- if (initial_slashes and
360- path .startswith (sep * 2 ) and not path .startswith (sep * 3 )):
361- initial_slashes = 2
362- comps = path .split (sep )
363- new_comps = []
364- for comp in comps :
365- if comp in (empty , dot ):
366- continue
367- if (comp != dotdot or (not initial_slashes and not new_comps ) or
368- (new_comps and new_comps [- 1 ] == dotdot )):
369- new_comps .append (comp )
370- elif new_comps :
371- new_comps .pop ()
372- comps = new_comps
373- path = sep .join (comps )
374- if initial_slashes :
375- path = sep * initial_slashes + path
376- return path or dot
373+ try :
374+ from posix import _path_normpath
375+
376+ except ImportError :
377+ def normpath (path ):
378+ """Normalize path, eliminating double slashes, etc."""
379+ path = os .fspath (path )
380+ if isinstance (path , bytes ):
381+ sep = b'/'
382+ empty = b''
383+ dot = b'.'
384+ dotdot = b'..'
385+ else :
386+ sep = '/'
387+ empty = ''
388+ dot = '.'
389+ dotdot = '..'
390+ if path == empty :
391+ return dot
392+ _ , initial_slashes , path = splitroot (path )
393+ comps = path .split (sep )
394+ new_comps = []
395+ for comp in comps :
396+ if comp in (empty , dot ):
397+ continue
398+ if (comp != dotdot or (not initial_slashes and not new_comps ) or
399+ (new_comps and new_comps [- 1 ] == dotdot )):
400+ new_comps .append (comp )
401+ elif new_comps :
402+ new_comps .pop ()
403+ comps = new_comps
404+ path = initial_slashes + sep .join (comps )
405+ return path or dot
406+
407+ else :
408+ def normpath (path ):
409+ """Normalize path, eliminating double slashes, etc."""
410+ path = os .fspath (path )
411+ if isinstance (path , bytes ):
412+ return os .fsencode (_path_normpath (os .fsdecode (path ))) or b"."
413+ return _path_normpath (path ) or "."
377414
378415
379416def abspath (path ):
0 commit comments