def _normalizePath(path):
    """Normalize a path by resolving '.' and '..' path elements."""

    # Special case for the root path.
    if path == u'/':
        return path

    new_segments = []
    prefix = u''
    if path.startswith('/'):
        prefix = u'/'
        path = path[1:]

    for segment in path.split(u'/'):
        if segment == u'.':
            continue
        if segment == u'..':
            new_segments.pop()  # raises IndexError if there is nothing to pop
            continue
        if not segment:
            raise ValueError('path must not contain empty segments: %s'
                             % path)
        new_segments.append(segment)

    return prefix + u'/'.join(new_segments)