From 392d901aba3930243c516ee2a9ef00a090357ac6 Mon Sep 17 00:00:00 2001 From: Ned Batchelder Date: Wed, 10 Jan 2018 21:47:09 -0500 Subject: Python 3.7 tweaked the layout of .pyc files --- coverage/execfile.py | 19 ++++++++++++++----- lab/show_pyc.py | 25 ++++++++++++++++++------- 2 files changed, 32 insertions(+), 12 deletions(-) diff --git a/coverage/execfile.py b/coverage/execfile.py index 693f54f..ffe3ca1 100644 --- a/coverage/execfile.py +++ b/coverage/execfile.py @@ -5,6 +5,7 @@ import marshal import os +import struct import sys import types @@ -253,11 +254,19 @@ def make_code_from_pyc(filename): if magic != PYC_MAGIC_NUMBER: raise NoCode("Bad magic number in .pyc file") - # Skip the junk in the header that we don't need. - fpyc.read(4) # Skip the moddate. - if sys.version_info >= (3, 3): - # 3.3 added another long to the header (size), skip it. - fpyc.read(4) + date_based = True + if sys.version_info >= (3, 7): + flags = struct.unpack('= (3, 3): + # 3.3 added another long to the header (size), skip it. + fpyc.read(4) # The rest of the file is the code object we want. code = marshal.load(fpyc) diff --git a/lab/show_pyc.py b/lab/show_pyc.py index 0a28e4f..525797a 100644 --- a/lab/show_pyc.py +++ b/lab/show_pyc.py @@ -13,14 +13,25 @@ import types def show_pyc_file(fname): f = open(fname, "rb") magic = f.read(4) - moddate = f.read(4) - modtime = time.asctime(time.localtime(struct.unpack('= (3, 3): - # 3.3 added another long to the header (size). - size = f.read(4) - print("pysize %s (%d)" % (binascii.hexlify(size), struct.unpack('= (3, 7): + # 3.7 added a flags word + flags = struct.unpack('= (3, 3): + # 3.3 added another long to the header (size). + size = f.read(4) + print("pysize %s (%d)" % (binascii.hexlify(size), struct.unpack('