[Python-ideas] Add a cryptographic hash (e.g SHA1) of source toPython Compiled objects?

Brett Cannon brett at python.org
Wed Feb 4 19:36:26 CET 2009


On Wed, Feb 4, 2009 at 01:57,  <rocky at gnu.org> wrote:
> Terry Reedy writes:
>  > rocky at gnu.org wrote:
>  >
>  > > Without a doubt you all are much more familiar at this stuff that I
>  > > am. (In fact I'm a rank novice.) So I'd be grateful if someone would
>  > > post code for a function say:
>  > >
>  > >   compare_src_obj(python_src, python_obj)
>  > >
>  > > that takes two strings -- a Python source filename and a Python object
>  > > -- does what's outlined above, and returns a status which indicates
>  > > the same or if not and if not whether the difference is because of the
>  > > wrong version of Python was used.
>  >
>  > Interesting question.  For equaility, I would start with, just guessing
>  > a bit:
>  >
>  > marshal(compile(open(file.py).read())) == open(file.pyc).read()
>  >
>  > Specifically for version clash, I believe the first 4 bytes are a magic
>  > version number.  If that is not part of the marshal string, it would
>  > need to be skipped for the equality comparison.
>
> There's also the mtime that needs to be ignored mentioned in prior
> posts. And is there a table which converts a magic number version back
> into a string with the Python version number? Thanks.

marshal.dumps(compile(open('file.py').read(), 'file.py', 'exec')) ==
open('file.pyc').read()[8:]

-Brett



More information about the Python-ideas mailing list