Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!feeder.news-service.com!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!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.003 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'python': 0.08; 'correct.': 0.09; 'fix.': 0.09; 'mtime': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:80.91.229.12': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'received:lo.gmane.org': 0.09; 'wrote:': 0.15; '.py': 0.16; 'header)': 0.16; '>>>': 0.16; 'bytes': 0.19; 'file,': 0.22; "doesn't": 0.22; 'header:In-Reply-To:1': 0.22; 'import': 0.29; '27,': 0.29; 'ignored.': 0.29; 'subject: .': 0.29; 'match': 0.30; 'os,': 0.30; "won't": 0.32; 'to:addr:python-list': 0.34; 'header:X -Complaints-To:1': 0.34; 'header:User-Agent:1': 0.34; '...': 0.34; 'version.': 0.35; 'file': 0.36; 'received:org': 0.38; 'subject:: ': 0.38; 'header:Mime-Version:1': 0.39; 'returned': 0.39; 'to:addr:python.org': 0.39; 'header': 0.40; 'skip:d 20': 0.40; 'datetime': 0.84; 'recompile': 0.84; 'schrieb': 0.84; 'subject:Only': 0.84 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Christian Heimes Subject: Re: Only Bytecode, No .py Files Date: Wed, 27 Jul 2011 11:27:33 +0200 References: <1311693548.3796.212.camel@ewzdev.atlantic> <1311715229.3796.224.camel@ewzdev.atlantic> Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: f049079102.adsl.alicedsl.de User-Agent: Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2.18) Gecko/20110617 Lightning/1.0b2.102ipre2 Thunderbird/3.1.11 In-Reply-To: X-Enigmail-Version: 1.1.2 OpenPGP: id=AD16AB1B; url=http://cheimes.de/heimes.asc 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: 42 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1311758872 news.xs4all.nl 23926 [2001:888:2000:d::a6]:35389 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:10379 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