[Python-Dev] Identifying magic prefix on Python files?

Eric S. Raymond esr@thyrsus.com
Sun, 4 Feb 2001 17:24:05 -0500


Tim Peters <tim.one@home.com>:
> [Eric S. Raymond]
> > Python's .pyc files don't have a magic prefix that the file(1)
> > utility can recognize.
> 
> Well, they *do* (#define MAGIC in import.c), but it changes from time to
> time.  Here's a NEWS item from 2.1a1:
> 
>     - The interpreter accepts now bytecode files on the command
>       line even if they do not have a .pyc or .pyo extension. On
>       Linux, after executing
> 
>       echo ':pyc:M::\x87\xc6\x0d\x0a::/usr/local/bin/python:' >
>          /proc/sys/fs/binfmt_misc/register
> 
>       any byte code file can be used as an executable (i.e. as an
>       argument to execve(2)).
> 
> However, the magic number has changed twice since then (in import.c rev
> 2.157 and again in rev 2.160), so the NEWS item is two changes obsolete.
> The current magic number can be obtained (as a 4-bytes string) via
> 
> import imp
> MAGIC = imp.get_magic()

Interesting.  I presume this has to be repeated at every boot?
 
> Note too that the method used for encoding the date runs out of bits at the
> end of 2001, so the current scheme is on its last legs regardless.

So this has to be fixed anyway.  I'm sure we can come up with a better 
scheme, perhaps one modeled after the PNG header.
-- 
		<a href="http://www.tuxedo.org/~esr/">Eric S. Raymond</a>

Are we at last brought to such a humiliating and debasing degradation,
that we cannot be trusted with arms for our own defence?  Where is the
difference between having our arms in our own possession and under our
own direction, and having them under the management of Congress?  If
our defence be the *real* object of having those arms, in whose hands
can they be trusted with more propriety, or equal safety to us, as in
our own hands?
        -- Patrick Henry, speech of June 9 1788