Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #21512

Re: How to know that two pyc files contain the same code

From Gelonida N <gelonida@gmail.com>
Subject Re: How to know that two pyc files contain the same code
Date 2012-03-12 00:56 +0100
References <mailman.544.1331390950.3037.python-list@python.org> <4f5bdb14$0$29891$c3e8da3$5496439d@news.astraweb.com> <mailman.554.1331428520.3037.python-list@python.org> <4f5c4f0d$0$29891$c3e8da3$5496439d@news.astraweb.com>
Newsgroups comp.lang.python
Message-ID <mailman.578.1331510204.3037.python-list@python.org> (permalink)

Show all headers | View raw


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()




Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

How to know that two pyc files contain the same code Gelonida N <gelonida@gmail.com> - 2012-03-10 15:48 +0100
  Re: How to know that two pyc files contain the same code Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-03-10 22:52 +0000
    Re: How to know that two pyc files contain the same code Chris Angelico <rosuav@gmail.com> - 2012-03-11 12:15 +1100
      Re: How to know that two pyc files contain the same code Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-03-11 07:06 +0000
        Re: How to know that two pyc files contain the same code Rick Johnson <rantingrickjohnson@gmail.com> - 2012-03-11 08:22 -0700
        Re: How to know that two pyc files contain the same code Gelonida N <gelonida@gmail.com> - 2012-03-12 00:56 +0100
    Re: How to know that two pyc files contain the same code Gelonida N <gelonida@gmail.com> - 2012-03-11 06:30 +0100

csiph-web