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


Groups > comp.lang.python > #10349 > unrolled thread

Re: Only Bytecode, No .py Files

Started byChristian Heimes <lists@cheimes.de>
First post2011-07-27 00:04 +0200
Last post2011-07-27 13:45 -0500
Articles 4 — 2 participants

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: Only Bytecode, No .py Files Christian Heimes <lists@cheimes.de> - 2011-07-27 00:04 +0200
    Re: Only Bytecode, No .py Files harrismh777 <harrismh777@charter.net> - 2011-07-26 20:32 -0500
      Re: Only Bytecode, No .py Files Christian Heimes <lists@cheimes.de> - 2011-07-27 11:27 +0200
        Re: Only Bytecode, No .py Files harrismh777 <harrismh777@charter.net> - 2011-07-27 13:45 -0500

#10349 — Re: Only Bytecode, No .py Files

FromChristian Heimes <lists@cheimes.de>
Date2011-07-27 00:04 +0200
SubjectRe: Only Bytecode, No .py Files
Message-ID<mailman.1512.1311717864.1164.python-list@python.org>
Am 26.07.2011 23:20, schrieb Eldon Ziegler:
> That seemed like a good idea but the .py file was complied even though
> it was dated 2001-01-01 00:00:00. Python must be checking something
> beside the date.

The first four bytes of a pyc file contain the magic header. It must
match the magic of the current Python version. The next four bytes
contain the pyc_mtime. It must match the mtime of the corresponding .py
files as returned by fstat().st_mtime. If the magic doesn't match or the
mtime header doesn't match the mtime of the .py file, the pyc is ignored.

Hint: If you run python with the -v option, you can get information why
a pyc file is ignored.

$ touch test.py
$ pycompile test.py
$ python -v -c "import test"
...
# test.pyc matches test.py
import test # precompiled from test.pyc
...
$ touch test.py
$ python -v -c "import test"
...
# test.pyc has bad mtime
import test # from test.py
...

Christian

[toc] | [next] | [standalone]


#10361

Fromharrismh777 <harrismh777@charter.net>
Date2011-07-26 20:32 -0500
Message-ID<TUJXp.106882$8G4.66955@newsfe17.iad>
In reply to#10349
Christian Heimes wrote:
> The first four bytes of a pyc file contain the magic header. It must
> match the magic of the current Python version. The next four bytes
> contain the pyc_mtime. It must match the mtime of the corresponding .py
> files as returned by fstat().st_mtime. If the magic doesn't match or the
> mtime header doesn't match the mtime of the .py file, the pyc is ignored.

    ... so recompile is required to fix.


[toc] | [prev] | [next] | [standalone]


#10379

FromChristian Heimes <lists@cheimes.de>
Date2011-07-27 11:27 +0200
Message-ID<mailman.1530.1311758872.1164.python-list@python.org>
In reply to#10361
Am 27.07.2011 03:32, schrieb harrismh777:
> Christian Heimes wrote:
>> The first four bytes of a pyc file contain the magic header. It must
>> match the magic of the current Python version. The next four bytes
>> contain the pyc_mtime. It must match the mtime of the corresponding .py
>> files as returned by fstat().st_mtime. If the magic doesn't match or the
>> mtime header doesn't match the mtime of the .py file, the pyc is ignored.
> 
>     ... so recompile is required to fix.

It's not required, you can fake a .py file with a trick:

>>> import os, struct, datetime

First get the magic and mtime from the pyc file

>>> with open("test.pyc", "rb") as f:
...     header = f.read(8)
...
>>> magic, mtime = struct.unpack("ii", header)
>>> magic, mtime
(168686339, 1311717735)

Verify it's a good date

>>> datetime.datetime.fromtimestamp(mtime)
datetime.datetime(2011, 7, 27, 0, 2, 15)

Now create an empty test.py

>>> open("test.py", "w").close()

Set its mtime

>>> os.utime("test.py", (mtime, mtime))

Now the test.py has the same mtime as test.pyc and Python won't
recompile the .pyc file from the .py file as long as the magic header
(168686339) is correct.

Christian

[toc] | [prev] | [next] | [standalone]


#10395

Fromharrismh777 <harrismh777@charter.net>
Date2011-07-27 13:45 -0500
Message-ID<b1ZXp.22377$%f4.20@newsfe16.iad>
In reply to#10379
Christian Heimes wrote:
> Now the test.py has the same mtime as test.pyc and Python won't
> recompile the .pyc file from the .py file as long as the magic header
> (168686339) is correct.
>

~very cool.


[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web