Issue 8664: py_compile.compile() should consistently create parent directory of target file (original) (raw)

Currently when cfile argument of py_compile.compile() is None, then path to target file is automatically calculated and parent directory of target file is created. If the same path of target file is explicitly passed as cfile argument of py_compile.compile(), then parent directory of target file is not created automatically. This inconsistency in behavior can be easily fixed. Fixing it also allows to simplify compileall.compile_file() function, which calls py_compile.compile() and currently needs to earlier try to create parent directory of target file.

The following example shows inconsistent behavior:

$ mkdir test $ touch test/test.py $ tree test test └── test.py

0 directories, 1 file $ python3.2 -c 'import py_compile; py_compile.compile("test/test.py")' $ tree test test ├── pycache │ └── test.cpython-32.pyc └── test.py

1 directory, 2 files $ rm -fr test/pycache $ python3.2 -c 'import py_compile; py_compile.compile("test/test.py", "test/pycache/test.cpython-32.pyc")' Traceback (most recent call last): File "", line 1, in File "/usr/lib64/python3.2/py_compile.py", line 131, in compile with open(cfile, 'wb') as fc: IOError: [Errno 2] No such file or directory: 'test/pycache/test.cpython-32.pyc' $ mkdir test/pycache $ python3.2 -c 'import py_compile; py_compile.compile("test/test.py", "test/pycache/test.cpython-32.pyc")' $ tree test test ├── pycache │ └── test.cpython-32.pyc └── test.py

1 directory, 2 files