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

Ka-Ping Yee ping@lfw.org
Sun, 4 Feb 2001 11:34:33 -0800 (PST)


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?

On Sun, 4 Feb 2001, Fredrik Lundh wrote:
> Python 1.5.2 (#0, May  9 2000, 14:04:03)
> >>> import imp
> >>> imp.get_magic()
> '\231N\015\012'

I don't understand, Eric.  Why won't the existing magic number work?

I tried the following and it works fine:

0	string		\x99N\x0d	Python 1.5.2 compiled bytecode data
0	string		\x87\xc6\x0d	Python 2.0 compiled bytecode data


However, when i add \x0a to the end of the bytecode patterns, this
stops working:

0	string		\x99N\x0d\x0a	Python 1.5.2 compiled bytecode data
0	string		\x87\xc6\x0d\x0a	Python 2.0 compiled bytecode data

Do you know what's going on?


These all work fine too, by the way:

0	string		#!/usr/bin/env\ python		Python program text
0	string		#!\ /usr/bin/env\ python	Python program text
0	string		#!/bin/env\ python		Python program text
0	string		#!\ /bin/env\ python		Python program text
0	string		#!/usr/bin/python		Python program text
0	string		#!\ /usr/bin/python		Python program text
0	string		#!/usr/local/bin/python		Python program text
0	string		#!\ /usr/local/bin/python	Python program text
0	string		"""		Python module text

Unfortunately, many Python modules are mis-recognized as
Java source text because they begin with the word "import".
Even more unfortunately, this too-general test for "import"
seems to be hard-coded into the file(1) command and cannot
be changed by editing /usr/share/magic.


-- ?!ng

"Old code doesn't die -- it just smells that way."
    -- Bill Frantz