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

Terry Reedy tjreedy at udel.edu
Wed Feb 4 07:17:22 CET 2009

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.

More information about the Python-ideas mailing list