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

Discussion in 'Python' started by gelonida, May 5, 2010.

  1. gelonida

    gelonida Guest

    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
    gelonida, May 5, 2010
    #1
    1. Advertising

  2. En Wed, 05 May 2010 10:34:34 -0300, gelonida <> escribió:

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

    --
    Gabriel Genellina
    Gabriel Genellina, May 6, 2010
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Roger Pack
    Replies:
    3
    Views:
    148
    Roger Pack
    Sep 28, 2010
  2. Gelonida N
    Replies:
    6
    Views:
    180
    Gelonida N
    Mar 11, 2012
  3. Gelonida N
    Replies:
    1
    Views:
    227
    Bl0ckeduser
    Mar 10, 2012
  4. Jason Carlton
    Replies:
    11
    Views:
    235
    Dr J R Stockton
    Dec 8, 2009
  5. Mircescu Andrei
    Replies:
    3
    Views:
    79
    Mircescu Andrei
    Feb 20, 2014
Loading...

Share This Page