Ignore:
Timestamp:
Mar 19, 2014, 11:31:01 PM (11 years ago)
Author:
dmik
Message:

python: Merge vendor 2.7.6 to trunk.

Location:
python/trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • python/trunk

  • python/trunk/Lib/posixpath.py

    r2 r391  
    1212
    1313import os
     14import sys
    1415import stat
    1516import genericpath
    1617import warnings
    1718from genericpath import *
     19
     20try:
     21    _unicode = unicode
     22except NameError:
     23    # If Python is built without Unicode support, the unicode type
     24    # will not exist. Fake one.
     25    class _unicode(object):
     26        pass
    1827
    1928__all__ = ["normcase","isabs","join","splitdrive","split","splitext",
     
    6069    """Join two or more pathname components, inserting '/' as needed.
    6170    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."""
    6373    path = a
    6474    for b in p:
     
    140150    """Test whether a path exists.  Returns True for broken symbolic links"""
    141151    try:
    142         st = os.lstat(path)
     152        os.lstat(path)
    143153    except os.error:
    144154        return False
     
    179189def ismount(path):
    180190    """Test whether a path is a mount point"""
     191    if islink(path):
     192        # A symlink can never be a mount point
     193        return False
    181194    try:
    182195        s1 = os.lstat(path)
     
    264277            return path
    265278        userhome = pwent.pw_dir
    266     userhome = userhome.rstrip('/') or userhome
    267     return userhome + path[i:]
     279    userhome = userhome.rstrip('/')
     280    return (userhome + path[i:]) or '/'
    268281
    269282
     
    309322    """Normalize path, eliminating double slashes, etc."""
    310323    # 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 ('/', '.')
    312325    if path == '':
    313326        return dot
     
    338351    """Return an absolute path."""
    339352    if not isabs(path):
    340         if isinstance(path, unicode):
     353        if isinstance(path, _unicode):
    341354            cwd = os.getcwdu()
    342355        else:
     
    352365    """Return the canonical path of the specified filename, eliminating any
    353366symbolic 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.
     372def _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)
    367388            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
     414supports_unicode_filenames = (sys.platform == 'darwin')
    395415
    396416def relpath(path, start=curdir):
     
    400420        raise ValueError("no path specified")
    401421
    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]
    404424
    405425    # Work out how much of the filepath is shared by start and path.
Note: See TracChangeset for help on using the changeset viewer.