How to know that two pyc files contain the same code

Peter Otten __peter__ at web.de
Sat Mar 10 17:21:00 CET 2012


Gelonida N wrote:

> I want to know whether two .pyc files are identical.
> 
> With identical I mean whether they contain the same byte code.
> 
> Unfortunately it seems, that .pyc files contain also something like the
> time stamp of the related source file.
> 
> So though two pyc files contain the same byte code, they will not be
> byte identical.
> 
> One option, that I found is to use
> python -m unpyclib.application -d filename.pyc and check whether the
> results are identical.

Or you could just strip off the first 8 (may be version-dependent) bytes 
before you compare the file contents.
 
> However even this will fail if the files were not compiled under the
> same absolute path name as the source filename is contained twice  (at
> least for my trivial example) in the disassemblers output.

That's another problem. If you are OK with likelihood you can replace the 
filename of the old code with that of the new one before you compare.




More information about the Python-list mailing list