def abspath(newpath, curpath):
    """Return the absolute path to the given 'newpath'.

    The current directory string must be given by 'curpath' as an absolute
    path.
    """
    assert newpath
    assert curpath
    assert curpath.startswith('/')

    subdirs = newpath.split('/')
    if not subdirs[0] or curpath == '/':
        # Absolute path (curpath is ignored)
        # or we're in the root directory
        dirs = [""]
    else:
        # Relative path; extract directory components from curpath
        dirs = curpath.split('/')

    for s in subdirs:
        if not s or s == ".":
            # Empty or 'current directory'
            pass
        elif s == "..":
            dirs.pop()
            if not dirs:
                raise ValueError("Too many '..' in path '{}'".format(newpath))
        else:
            dirs.append(s)

    if len(dirs) == 1:
        # Special case for root: joining [] or [""] return "", but you can't
        # set the first component to "/" since joining ["/","foo"] would
        # return "//foo"
        return '/'

    return '/'.join(dirs)