This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author iszegedi
Recipients
Date 2007-05-08.16:01:45
SpamBayes Score
Marked as misclassified
Message-id
In-reply-to
Content
Robert,
I checked once again the POSIX definition of filenames and you are right; trailing whitespaces are allowed:

-- clip --
 3.169 Filename

A name consisting of 1 to {NAME_MAX} bytes used to name a file. The characters composing the name may be selected from the set of all character values excluding the slash character and the null byte. The filenames dot and dot-dot have special meaning. A filename is sometimes referred to as a "pathname component".
-- clip --


So I agree that this line

path = path.rstrip()

should be removed from the correction suggested by me.


-- clip --


def normpath(path):
    """Normalize path, eliminating double slashes, etc."""
    if path == '':
        return '.'
    initial_slashes = path.startswith('/')
    # The next line was added by iszegedi
    trailing_slash = path.endswith('/')
    # POSIX allows one or two initial slashes, but treats three or more
    # as single slash.
    if (initial_slashes and
        path.startswith('//') and not path.startswith('///')):
        initial_slashes = 2
    comps = path.split('/')
    new_comps = []
    for comp in comps:
        if comp in ('', '.'):
            continue
        if (comp != '..' or (not initial_slashes and not new_comps) or
             (new_comps and new_comps[-1] == '..')):
            new_comps.append(comp)
        elif new_comps:
            new_comps.pop()
    comps = new_comps
    path = '/'.join(comps)
    if initial_slashes:
        path = '/'*initial_slashes + path
    # The next two lines were added by iszegedi
    if trailing_slash:
        path = path + '/'
    return path or '.'


-- clip --

Nevertheless, the remaining question for me is still the same: what is the exact definition of normalized path in regards with trailing slash (in other words what is the correct solution: no slash at the end of the path OR there should be a slash at the end of the path OR or there should be a single . appended to the end of the path):

So what is the correct answer:
os.path.normpath('/etc/passwd/')  ---> '/etc/passwd'   OR
os.path.normpath('/etc/passwd/')  ---> '/etc/passwd/'  OR
os.path.normpath('/etc/passwd/')  ---> '/etc/passwd/.'

Once this is clarified, the solution is pretty easy.

Istvan
History
Date User Action Args
2007-08-23 14:53:26adminlinkissue1707768 messages
2007-08-23 14:53:26admincreate