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

M.-A. Lemburg mal@lemburg.com
Mon, 05 Feb 2001 12:15:03 +0100


Fredrik Lundh wrote:
> 
> 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.

Side note: the magic can also change due to command line options
being used, e.g. -U will bump the magic number by 1.

-- 
Marc-Andre Lemburg
______________________________________________________________________
Company:                                        http://www.egenix.com/
Consulting:                                    http://www.lemburg.com/
Python Pages:                           http://www.lemburg.com/python/