Changeset 391 for python/trunk/Lib/posixpath.py
- Timestamp:
- Mar 19, 2014, 11:31:01 PM (11 years ago)
- Location:
- python/trunk
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
python/trunk
-
Property svn:mergeinfo
set to
/python/vendor/Python-2.7.6 merged eligible /python/vendor/current merged eligible
-
Property svn:mergeinfo
set to
-
python/trunk/Lib/posixpath.py
r2 r391 12 12 13 13 import os 14 import sys 14 15 import stat 15 16 import genericpath 16 17 import warnings 17 18 from genericpath import * 19 20 try: 21 _unicode = unicode 22 except NameError: 23 # If Python is built without Unicode support, the unicode type 24 # will not exist. Fake one. 25 class _unicode(object): 26 pass 18 27 19 28 __all__ = ["normcase","isabs","join","splitdrive","split","splitext", … … 60 69 """Join two or more pathname components, inserting '/' as needed. 61 70 If any component is an absolute path, all previous path components 62 will be discarded.""" 71 will be discarded. An empty last part will result in a path that 72 ends with a separator.""" 63 73 path = a 64 74 for b in p: … … 140 150 """Test whether a path exists. Returns True for broken symbolic links""" 141 151 try: 142 st =os.lstat(path)152 os.lstat(path) 143 153 except os.error: 144 154 return False … … 179 189 def ismount(path): 180 190 """Test whether a path is a mount point""" 191 if islink(path): 192 # A symlink can never be a mount point 193 return False 181 194 try: 182 195 s1 = os.lstat(path) … … 264 277 return path 265 278 userhome = pwent.pw_dir 266 userhome = userhome.rstrip('/') or userhome267 return userhome + path[i:]279 userhome = userhome.rstrip('/') 280 return (userhome + path[i:]) or '/' 268 281 269 282 … … 309 322 """Normalize path, eliminating double slashes, etc.""" 310 323 # Preserve unicode (if path is unicode) 311 slash, dot = (u'/', u'.') if isinstance(path, unicode) else ('/', '.')324 slash, dot = (u'/', u'.') if isinstance(path, _unicode) else ('/', '.') 312 325 if path == '': 313 326 return dot … … 338 351 """Return an absolute path.""" 339 352 if not isabs(path): 340 if isinstance(path, unicode):353 if isinstance(path, _unicode): 341 354 cwd = os.getcwdu() 342 355 else: … … 352 365 """Return the canonical path of the specified filename, eliminating any 353 366 symbolic links encountered in the path.""" 354 if isabs(filename): 355 bits = ['/'] + filename.split('/')[1:] 356 else: 357 bits = [''] + filename.split('/') 358 359 for i in range(2, len(bits)+1): 360 component = join(*bits[0:i]) 361 # Resolve symbolic links. 362 if islink(component): 363 resolved = _resolve_link(component) 364 if resolved is None: 365 # Infinite loop -- return original component + rest of the path 366 return abspath(join(*([component] + bits[i:]))) 367 path, ok = _joinrealpath('', filename, {}) 368 return abspath(path) 369 370 # Join two paths, normalizing ang eliminating any symbolic links 371 # encountered in the second path. 372 def _joinrealpath(path, rest, seen): 373 if isabs(rest): 374 rest = rest[1:] 375 path = sep 376 377 while rest: 378 name, _, rest = rest.partition(sep) 379 if not name or name == curdir: 380 # current dir 381 continue 382 if name == pardir: 383 # parent dir 384 if path: 385 path, name = split(path) 386 if name == pardir: 387 path = join(path, pardir, pardir) 367 388 else: 368 newpath = join(*([resolved] + bits[i:])) 369 return realpath(newpath) 370 371 return abspath(filename) 372 373 374 def _resolve_link(path): 375 """Internal helper function. Takes a path and follows symlinks 376 until we either arrive at something that isn't a symlink, or 377 encounter a path we've seen before (meaning that there's a loop). 378 """ 379 paths_seen = [] 380 while islink(path): 381 if path in paths_seen: 382 # Already seen this path, so we must have a symlink loop 383 return None 384 paths_seen.append(path) 385 # Resolve where the link points to 386 resolved = os.readlink(path) 387 if not isabs(resolved): 388 dir = dirname(path) 389 path = normpath(join(dir, resolved)) 390 else: 391 path = normpath(resolved) 392 return path 393 394 supports_unicode_filenames = False 389 path = pardir 390 continue 391 newpath = join(path, name) 392 if not islink(newpath): 393 path = newpath 394 continue 395 # Resolve the symbolic link 396 if newpath in seen: 397 # Already seen this path 398 path = seen[newpath] 399 if path is not None: 400 # use cached value 401 continue 402 # The symlink is not resolved, so we must have a symlink loop. 403 # Return already resolved part + rest of the path unchanged. 404 return join(newpath, rest), False 405 seen[newpath] = None # not resolved symlink 406 path, ok = _joinrealpath(path, os.readlink(newpath), seen) 407 if not ok: 408 return join(path, rest), False 409 seen[newpath] = path # resolved symlink 410 411 return path, True 412 413 414 supports_unicode_filenames = (sys.platform == 'darwin') 395 415 396 416 def relpath(path, start=curdir): … … 400 420 raise ValueError("no path specified") 401 421 402 start_list = abspath(start).split(sep)403 path_list = abspath(path).split(sep)422 start_list = [x for x in abspath(start).split(sep) if x] 423 path_list = [x for x in abspath(path).split(sep) if x] 404 424 405 425 # Work out how much of the filepath is shared by start and path.
Note:
See TracChangeset
for help on using the changeset viewer.