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

Fredrik Lundh fredrik@effbot.org
Mon, 5 Feb 2001 01:12:09 +0100


tim wrote:
> [/F]
> > Breaking people's code just for fun seems to be a new
> > trend here.  That's bad.
> 
> The details of the current scheme stop working at the end of the year
> regardless.

might so be, but it's perfectly possible to change this in a
fully backwards compatible way:

-- stick to a 4-byte bytecode version magic, but change
   the algoritm to make it work after 2001.  if necessary,
   use 3 or 4 bytes to hold the "serial number".

   if the bytecode version is the same as imp.get_magic()
   and the file isn't damaged, it should be safe to pass it to
   marshal.load.  if marshal returns a code object, it should
   be safe (relatively speaking) to execute it.

-- define the 4-byte timestamp to be an unsigned int, so
   we can keep going for another 100 years or so.

-- introduce a new type code (e.g. 'P') for marshal.  this
   is followed by an extended magic field, followed by the
   code using today's format (same as for type code 'c').

   let the extended magic field contain:

        -- a python identifier (e.g. "YTHON")
        -- a newline/eof mangling detector (e.g. "\r\n")
        -- sys.hexversion (4 bytes)
        -- a flag field (4 bytes)
        -- maybe the size of the marshalled block (4 bytes)
        -- maybe etc

Cheers /F