[Python-bugs-list] [ python-Bugs-665336 ] win32 os.path.normpath not correct for leading slash cases

SourceForge.net noreply@sourceforge.net
Thu, 09 Jan 2003 13:42:03 -0800


Bugs item #665336, was opened at 2003-01-09 21:42
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=665336&group_id=5470

Category: Python Library
Group: Python 2.2
Status: Open
Resolution: None
Priority: 5
Submitted By: Stephan R.A. Deibel (sdeibel)
Assigned to: Nobody/Anonymous (nobody)
Summary: win32 os.path.normpath not correct for leading slash cases

Initial Comment:
os.path.normpath on win32 misses two cases in its
duplicate slash
removal code that I believe should be dealt with:

c:\x\y\z
\\x\y\z

Both of these are returned unchanged.

I've attached an implementation that fixes these to
return the following, respectively:

c:\x\y\z
\x\y\z

I did see the other normpath bugs that were reported
and rejected
and think that the above isn't also a case of "garbage
in garbage out" but of course there's room for
interpretation.

I am a bit unsure about the UNC case since Posix
collapses only 3+ leading slashes to a single slash and
otherwise leaves up to two slashes.  But in the context
of win32 the above seems to make more sense to me.

Thanks,

Stephan Deibel
Wing IDE Developer
Archaeopteryx Software

--------------------

def normpath(path):
    """Normalize path, eliminating double slashes, etc."""
    path = path.replace("/", "\")
    prefix, path = os.path.splitdrive(path)
    if prefix == '':
        max_leading = 2
    else:
        max_leading = 1
    i = 0
    while path[:1] == "\":
        if i < max_leading:
            prefix = prefix + "\"
            i += 1
        path = path[1:]
    comps = path.split("\")
    i = 0
    while i < len(comps):
        if comps[i] in ('.', ''):
            del comps[i]
        elif comps[i] == '..':
            if i > 0 and comps[i-1] != '..':
                del comps[i-1:i+1]
                i -= 1
            elif i == 0 and prefix.endswith("\"):
                del comps[i]
            else:
                i += 1
        else:
            i += 1
    # If the path is now empty, substitute '.'
    if not prefix and not comps:
        comps.append('.')
    return prefix + "\".join(comps)


----------------------------------------------------------------------

You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=665336&group_id=5470