@classdef.singleton_method("expand_path", path="path")
    def method_expand_path(self, space, path, w_dir=None):
        if path and path[0] == "~":
            try:
                home = os.environ["HOME"]
            except __HOLE__:
                raise space.error(space.w_ArgumentError, "couldn't find HOME environment -- expanding")
            if not home or (not IS_WINDOWS and home[0] != "/"):
                raise space.error(space.w_ArgumentError, "non-absolute home")
            if len(path) >= 2 and path[1] == "/":
                path = home + path[1:]
            elif len(path) < 2:
                return space.newstr_fromstr(home)
            else:
                raise space.error(space.w_NotImplementedError, "~user for File.expand_path")
        elif not path or path[0] != "/":
            if w_dir is not None and w_dir is not space.w_nil:
                dir = space.str_w(space.send(self, "expand_path", [w_dir]))
            else:
                dir = os.getcwd()

            path = dir + "/" + path

        items = []
        if IS_WINDOWS:
            path = path.replace("\\", "/")
        parts = path.split("/")
        was_letter = False
        first_slash = True
        for part in parts:
            if not part and not was_letter:
                if not first_slash:
                    items.append(part)
                first_slash = False
            elif part == "..":
                if len(items) > 0:
                    items.pop()
            elif part and part != ".":
                was_letter = True
                items.append(part)
        if not IS_WINDOWS:
            root = "/"
        else:
            root = ""
        return space.newstr_fromstr(root + "/".join(items))
dataset/ETHPy150Open topazproject/topaz/topaz/objects/fileobject.py/W_FileObject.method_expand_path