[Python-Dev] Mysterious Python pyc file corruption problems

Terry Jan Reedy tjreedy at udel.edu
Fri May 17 00:00:49 CEST 2013

On 5/16/2013 5:30 PM, Brett Cannon wrote:
> On Thu, May 16, 2013 at 5:19 PM, Guido van Rossum <guido at python.org> wrote:
>> This reminds me of the following bug, which can happen when two
>> processes are both writing the .pyc file and a third is reading it.
>> First some background.
>> When writing a .pyc file, we use the following strategy:
>> - open the file for writing
>> - write a dummy header (four null bytes)
>> - write the .py file's mtime
>> - write the marshalled code object
>> - replace the dummy heaer with the correct magic word
> Just so people know, this is how we used to do it. In importlib we
> write the entire file to a temp file and then to an atomic rename.
>> Even py_compile.py (used by compileall.py) uses this strategy.
> py_compile as of Python 3.4 now just uses importlib directly, so it
> matches its semantics.

But in 3.3, it still is as Guido describes, even though importlib is 

More information about the Python-Dev mailing list