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

Fredrik Lundh fredrik@effbot.org
Mon, 5 Feb 2001 07:35:02 +0100

tim wrote:
> Then you have /F's post, which purports to give a "fully backward
> compatible" scheme, despite changing what probably appears <wink> to be
> almost everyting.

unlike earlier proposals, it doesn't break py_compile:

    MAGIC = imp.get_magic()

    fc = open(cfile, 'wb')
    wr_long(fc, timestamp)
    marshal.dump(codeobject, fc)
    fc.seek(0, 0)

and it doesn't break imputil:

    f = open(file, 'rb')
    if f.read(4) == imp.get_magic():
        t = struct.unpack('<I', f.read(4))[0]
        if t == t_py:
            code = marshal.load(f)

and it doesn't break any user code that does similar things
(squeeze, pythonworks, and a dozen other tools I've written;
applications using local copies of imputils, etc)

Cheers /F