Title: py_compile.compile() should consistently create parent directory of target file
Type: behavior Stage:
Components: Library (Lib) Versions: Python 3.2
Status: closed Resolution: accepted
Dependencies: Superseder:
Assigned To: barry Nosy List: Arfrever, barry, benjamin.peterson
Priority: normal Keywords: patch

Created on 2010-05-08 19:36 by Arfrever, last changed 2010-05-08 19:53 by benjamin.peterson. This issue is now closed.

File name Uploaded Description Edit
python-issue8664.patch Arfrever, 2010-05-08 19:37 Patch for py_compile.compile() and compileall.compile_file()
Messages (2)
msg105328 - (view) Author: Arfrever Frehtes Taifersar Arahesis (Arfrever) * (Python triager) Date: 2010-05-08 19:36
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/
$ tree test

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

1 directory, 2 files
$ rm -fr test/__pycache__
$ python3.2 -c 'import py_compile; py_compile.compile("test/", "test/__pycache__/test.cpython-32.pyc")'
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/usr/lib64/python3.2/", 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/__pycache__/test.cpython-32.pyc")'
$ tree test
├── __pycache__
│   └── test.cpython-32.pyc

1 directory, 2 files
msg105332 - (view) Author: Benjamin Peterson (benjamin.peterson) * (Python committer) Date: 2010-05-08 19:53
Looks good. Applied in r81005.
Date User Action Args
2010-05-08 19:53:03benjamin.petersonsetstatus: open -> closed

nosy: + benjamin.peterson
messages: + msg105332

resolution: accepted
2010-05-08 19:43:32benjamin.petersonsetassignee: barry

nosy: + barry
2010-05-08 19:38:00Arfreversetfiles: + python-issue8664.patch
keywords: + patch
2010-05-08 19:36:31Arfrevercreate