find out whther byte two .pyc files contain the same byte code.

G

gelonida

Hi,

I'd like to know whether the byte code of two .pyc files is identical.

I thought, I could just compare the md5sums of thw .pyc files.
However this does not work.

It seems, that .pyc ontains the time stamp of the source file.
the .pyc file changes its contents when I don't change the file's
contents but just
save it again.

Is there any way to compare two .pyc files or to just strip the
timestiam or set it to a fixed value?


Example:
##### I create a python file
$ echo 'print 1' > tst.py
##### I compile it and display the md5sum of the .pyc file
$ rm -f tst.pyc ; python -c 'import tst' ; md5sum tst.pyc
1
0d67af4c2c233dbbe89bd2dab6b12088 *tst.pyc

##### now I recompile and will get the same md5sum
$ rm -f tst.pyc ; python -c 'import tst' ; md5sum tst.pyc
1
0d67af4c2c233dbbe89bd2dab6b12088 *tst.pyc

#### now I change the time stamp of the python source
touch tst.py

#### now the md5sum will be different
$ rm -f tst.pyc ; python -c 'import tst' ; md5sum tst.pyc
1
f6faf2048b6f967c73e68951558f954c *tst.pyc



thanks for any suggestions / ideas
 
G

Gabriel Genellina

I'd like to know whether the byte code of two .pyc files is identical.

I thought, I could just compare the md5sums of thw .pyc files.
However this does not work.

It seems, that .pyc ontains the time stamp of the source file.
the .pyc file changes its contents when I don't change the file's
contents but just
save it again.

Yes, that's what happens.
First 4 bytes in a .pyc file contain a "magic" number identifying the
Python version used to create it. Next 4 bytes contain the timestamp of
the source file.

Just ignore the first 8 bytes when computing the checksum:

tail -c +8 tst.pyc | md5sum

(untested)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top