@@ -131,7 +131,7 @@ def find_module(self, name, path=None):
131
131
pyc = os .path .join (cache_dir , cache_name )
132
132
# Notice that even if we're in a read-only directory, I'm going
133
133
# to check for a cached pyc. This may not be optimal...
134
- co = _read_pyc (fn_pypath , pyc )
134
+ co = _read_pyc (fn_pypath , pyc , state . trace )
135
135
if co is None :
136
136
state .trace ("rewriting %r" % (fn ,))
137
137
co = _rewrite_test (state , fn_pypath )
@@ -289,7 +289,7 @@ def _make_rewritten_pyc(state, fn, pyc, co):
289
289
if _write_pyc (state , co , fn , proc_pyc ):
290
290
os .rename (proc_pyc , pyc )
291
291
292
- def _read_pyc (source , pyc ):
292
+ def _read_pyc (source , pyc , trace = lambda x : None ):
293
293
"""Possibly read a pytest pyc containing rewritten code.
294
294
295
295
Return rewritten code if successful or None if not.
@@ -298,23 +298,27 @@ def _read_pyc(source, pyc):
298
298
fp = open (pyc , "rb" )
299
299
except IOError :
300
300
return None
301
- try :
301
+ with fp :
302
302
try :
303
303
mtime = int (source .mtime ())
304
304
data = fp .read (8 )
305
- except EnvironmentError :
305
+ except EnvironmentError as e :
306
+ trace ('_read_pyc(%s): EnvironmentError %s' % (source , e ))
306
307
return None
307
308
# Check for invalid or out of date pyc file.
308
309
if (len (data ) != 8 or data [:4 ] != imp .get_magic () or
309
310
struct .unpack ("<l" , data [4 :])[0 ] != mtime ):
311
+ trace ('_read_pyc(%s): invalid or out of date pyc' % source )
312
+ return None
313
+ try :
314
+ co = marshal .load (fp )
315
+ except Exception as e :
316
+ trace ('_read_pyc(%s): marshal.load error %s' % (source , e ))
310
317
return None
311
- co = marshal .load (fp )
312
318
if not isinstance (co , types .CodeType ):
313
- # That's interesting....
319
+ trace ( '_read_pyc(%s): not a code object' % source )
314
320
return None
315
321
return co
316
- finally :
317
- fp .close ()
318
322
319
323
320
324
def rewrite_asserts (mod ):
0 commit comments