Path: csiph.com!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder2.enfer-du-nord.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'subject:two': 0.04; 'snippet': 0.07; 'subject:code': 0.07; 'calculates': 0.09; 'cares': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'subject:files': 0.09; 'am,': 0.12; 'def': 0.13; "'rb')": 0.16; 'bytecode': 0.16; 'fin:': 0.16; 'hashlib': 0.16; 'subject:contain': 0.16; 'wrote:': 0.18; 'bytes': 0.18; 'this?': 0.19; 'seems': 0.20; 'file,': 0.21; 'header:In-Reply-To:1': 0.22; 'versions': 0.23; 'changed': 0.23; 'subject:same': 0.23; 'guess': 0.26; 'import': 0.27; 'code,': 0.28; 'depends': 0.28; 'pass': 0.29; 'far.': 0.29; 'hash': 0.30; 'version': 0.32; 'cases': 0.32; 'header:User-Agent:1': 0.33; 'instead': 0.33; 'file': 0.34; 'header:X-Complaints-To:1': 0.34; 'steven': 0.34; 'identical': 0.34; 'to:addr:python-list': 0.35; 'subject:How': 0.35; 'received:org': 0.36; 'files': 0.39; 'received:de': 0.39; 'absolute': 0.39; 'to:addr:python.org': 0.40; 'imagine': 0.66; 'care': 0.71; '08:06': 0.84; 'located.': 0.84; 'stamp': 0.91; 'subject:know': 0.93 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Gelonida N Subject: Re: How to know that two pyc files contain the same code Date: Mon, 12 Mar 2012 00:56:26 +0100 References: <4f5bdb14$0$29891$c3e8da3$5496439d@news.astraweb.com> <4f5c4f0d$0$29891$c3e8da3$5496439d@news.astraweb.com> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: unicorn.dungeon.de User-Agent: Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2.27) Gecko/20120216 Lightning/1.0b2 "" In-Reply-To: <4f5c4f0d$0$29891$c3e8da3$5496439d@news.astraweb.com> X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 44 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1331510204 news.xs4all.nl 6964 [2001:888:2000:d::a6]:41764 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:21512 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()