[Python-Dev] Identifying magic prefix on Python files?
Fredrik Lundh
fredrik@pythonware.com
Sun, 4 Feb 2001 20:00:48 +0100
eric wrote:
> Python's .pyc files don't have a magic prefix that the file(1) utility
> can recognize. Would anyone object if I fixed this? A trivial pair of
> hacks to the compiler and interpreter would do it. Backward compatibility
> would be easily arranged.
>
> Embedding the Python version number in the prefix might enable some
> useful behavior down the road.
Python 1.5.2 (#0, May 9 2000, 14:04:03)
>>> import imp
>>> imp.get_magic()
'\231N\015\012'
Python 2.0 (#8, Jan 29 2001, 22:28:01)
>>> import imp
>>> imp.get_magic()
'\207\306\015\012'
>>> open("some_module.pyc", "rb").read(4)
'\207\306\015\012'
Python 2.1a1 (#9, Jan 19 2001, 08:41:32)
>>> import imp
>>> imp.get_magic()
'\xdc\xea\r\n'
if you want to change the magic, there are a couple
things to consider:
1) the header must consist of imp.get_magic() plus
a 4-byte timestamp, followed by a marshalled code
object
2) the magic should be four bytes.
3) the magic must be different for different bytecode
versions
4) the magic shouldn't survive text/binary conversions
on platforms which treat text files and binary files diff-
erently.
Cheers /F