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


Groups > comp.lang.python > #10379

Re: Only Bytecode, No .py Files

From Christian Heimes <lists@cheimes.de>
Subject Re: Only Bytecode, No .py Files
Date 2011-07-27 11:27 +0200
References <1311693548.3796.212.camel@ewzdev.atlantic> <CAGGBd_rmMyNdqaokHF5aOtC-YS3xjjW83q1-5dQz=wF31NbNbg@mail.gmail.com> <1311715229.3796.224.camel@ewzdev.atlantic> <mailman.1512.1311717864.1164.python-list@python.org> <TUJXp.106882$8G4.66955@newsfe17.iad>
Newsgroups comp.lang.python
Message-ID <mailman.1530.1311758872.1164.python-list@python.org> (permalink)

Show all headers | View raw


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

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


Thread

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

csiph-web