Apparent magic number problem

Steven D'Aprano steve+comp.lang.python at pearwood.info
Sat Mar 9 06:22:59 CET 2013


On Fri, 08 Mar 2013 19:48:28 -0500, Colin J. Williams wrote:

> I have a program that I wish to run in both Python 2.7 and Python 3.2
> 
> The program runs correctly under each version, but it runs more slowly
> under 3.2.

Without knowing what your program does, it is impossible to comment on 
why it is slower under 3.2.


> This is probably due to the fact that the .pyc file is created for the
> Python 2.7 execution.

I doubt it.


> When Python 3.2 is run it fails to create a new .pyc file and if the 2.7
> .pyc is offered directly a magic number problem is reported.

What do you mean, "offered directly"? Can you show exactly how you are 
running the program?

 
> Is there a bug here?  it seems to me that the Magic Number exception
> should lead to a new compile of the program.

Certainly not. Consider what that would mean. Suppose I try to run a .pyc 
file directly:

python32 myprogram.pyc 


If myprogram.pyc is compiled for the correct version, it will run. If it 
is not, then an error occurs. You want Python to recompile that. But 
consider what that would mean:

- Python would have to *guess* which .py file it should compile. Just 
because it can find something called "myprogram.py", doesn't mean that it 
is the right file. You might have renamed the file after compiling it, or 
moved it into a different folder. Who knows?

- After guessing what file to compile, it would have to compile it, and 
*delete* the existing .pyc file, overwriting it with the newly compiled 
version. This potentially loses data.

- And finally it would run the brand new .pyc file, which could do 
something *completely different* from the .pyc file you thought you were 
running.


Python only compiles files when you import them, or if you use a tool 
like compileall. Merely running a file does not compile it.

Likewise, if you run:

python myprogram.py

any myprogram.pyc file is ignored. .pyc files are only used if you 
directly run them, or if you import them.


-- 
Steven



More information about the Python-list mailing list