# Buggy code
def unpack_source_tree(tree_file, dir=None):
    if dir is None:
        dir = tempfile.mkdtemp()
    header = []
    cur_file = None
    f = open(tree_file)
    try:
        lines = f.readlines()
    finally:
        f.close()
    del f
    try:
        for line in lines:
            if line[:5] == "#####":
                filename = line.strip().strip("#").strip().replace("/", os.path.sep)
                path = os.path.join(dir, filename)
                if not os.path.exists(os.path.dirname(path)):
                    os.makedirs(os.path.dirname(path))
                if cur_file is not None:
                    f, cur_file = cur_file, None
                    f.close()
                cur_file = open(path, "w")
            elif cur_file is not None:
                cur_file.write(line)
            elif line.strip() and not line.lstrip().startswith("#"):
                if line.strip() not in ('"""', "'''"):
                    header.append(line)
    finally:
        if cur_file is not None:
            cur_file.close()
    return dir, "".join(header)


# Traceback (AssertionError)
runTest (__main__.EndToEndTest)
End-to-end fused_cmethods ... /home/user/Data/Projects/Code/Cython 20_2_18/Python2Venv/bin/python setup.py build_ext -i

/bin/sh: /home/user/Data/Projects/Code/Cython: No such file or directory


FAIL

======================================================================
FAIL: runTest (__main__.EndToEndTest)
End-to-end fused_cmethods
----------------------------------------------------------------------
Traceback (most recent call last):
File "runtests.py", line 1799, in runTest
self.assertEqual(0, res, "non-zero exit status")
AssertionError: non-zero exit status

----------------------------------------------------------------------
Ran 25 tests in 15.594s

# Fixed code
def unpack_source_tree(tree_file, workdir, cython_root):
    programs = {
        "PYTHON": [sys.executable],
        "CYTHON": [sys.executable, os.path.join(cython_root, "cython.py")],
        "CYTHONIZE": [sys.executable, os.path.join(cython_root, "cythonize.py")],
    }

    if workdir is None:
        workdir = tempfile.mkdtemp()
    header, cur_file = [], None
    with open(tree_file) as f:
        try:
            for line in f:
                if line.startswith("#####"):
                    filename = line.strip().strip("#").strip().replace("/", os.path.sep)
                    path = os.path.join(workdir, filename)
                    if not os.path.exists(os.path.dirname(path)):
                        os.makedirs(os.path.dirname(path))
                    if cur_file is not None:
                        to_close, cur_file = cur_file, None
                        to_close.close()
                    cur_file = open(path, "w")
                elif cur_file is not None:
                    cur_file.write(line)
                elif line.strip() and not line.lstrip().startswith("#"):
                    if line.strip() not in ('"""', "'''"):
                        command = shlex.split(line)
                        if not command:
                            continue
                        # In Python 3: prog, *args = command
                        prog, args = command[0], command[1:]
                        try:
                            header.append(programs[prog] + args)
                        except KeyError:
                            header.append(command)
        finally:
            if cur_file is not None:
                cur_file.close()
    return workdir, header