How to know that two pyc files contain the same code

Gelonida N gelonida at gmail.com
Mon Mar 12 00:56:26 CET 2012


On 03/11/2012 08:06 AM, Steven D'Aprano wrote:
> What if one merely changed the order of definition? Instead of:
> 
> def foo(): pass
> def bar(): pass
> 
> one had this?
> 
> def bar(): pass
> def foo(): pass
> 
> It depends on why the OP cares if they are "identical". I can imagine use-
> cases where the right solution is to forget ideas about identical code, 
> and just checksum the files (ignoring any timestamps).

I guess this is what I will do for my use case
Perform a checksum ignoring the time stamp.

What I did not know though is where the time stamp was located.
it seems it's in bytes 4-7 for all C-python versions so far.

What is regrettable though is, that the absolute path name is part of
the .pyc file, as I do not care


Following snippet calculates the hash of a .pyc file by just ignoring
the time stamp:

import hashlib

def md5_for_pyc(fname):
    hasher = hashlib.md5()
    with open(fname, 'rb') as fin:
        version = fin.read(4)
        hasher.update(version)
        _tstamp = fin.read(4)
        bytecode = fin.read()
        hasher.update(bytecode)
    return hasher.hexdigest()








More information about the Python-list mailing list