From ec82227213ac419d294f9f5019e9b94e81a71972 Mon Sep 17 00:00:00 2001 From: Ned Batchelder Date: Tue, 17 Feb 2015 07:35:01 -0500 Subject: Properly handle crazy-long code objects. #359 --- CHANGES.txt | 9 +++++++++ coverage/bytecode.py | 16 +++++++++++++--- lab/show_pyc.py | 2 +- tests/coveragetest.py | 12 +++++++----- tests/test_arcs.py | 21 +++++++++++++++++++++ 5 files changed, 51 insertions(+), 9 deletions(-) diff --git a/CHANGES.txt b/CHANGES.txt index f96db6e5..cab00a94 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -3,6 +3,15 @@ Change history for Coverage.py ------------------------------ +Latest +------ + +- Branch coverage couldn't properly handle certain extremely long files. This + is now fixed (`issue 359`_). + +.. _issue 359: https://bitbucket.org/ned/coveragepy/issue/359/xml-report-chunk-error + + Version 4.0a5 --- 16 February 2015 ---------------------------------- diff --git a/coverage/bytecode.py b/coverage/bytecode.py index 3f62dfaf..d7304936 100644 --- a/coverage/bytecode.py +++ b/coverage/bytecode.py @@ -1,9 +1,11 @@ """Bytecode manipulation for coverage.py""" -import opcode, types +import opcode +import types from coverage.backward import byte_to_int + class ByteCode(object): """A single bytecode.""" def __init__(self): @@ -26,6 +28,9 @@ class ByteCode(object): class ByteCodes(object): """Iterator over byte codes in `code`. + This handles the logic of EXTENDED_ARG byte codes internally. Those byte + codes are not returned by this iterator. + Returns `ByteCode` objects. """ @@ -37,6 +42,7 @@ class ByteCodes(object): def __iter__(self): offset = 0 + ext_arg = 0 while offset < len(self.code): bc = ByteCode() bc.op = self[offset] @@ -44,7 +50,7 @@ class ByteCodes(object): next_offset = offset+1 if bc.op >= opcode.HAVE_ARGUMENT: - bc.arg = self[offset+1] + 256*self[offset+2] + bc.arg = ext_arg + self[offset+1] + 256*self[offset+2] next_offset += 2 label = -1 @@ -55,7 +61,11 @@ class ByteCodes(object): bc.jump_to = label bc.next_offset = offset = next_offset - yield bc + if bc.op == opcode.EXTENDED_ARG: + ext_arg = bc.arg * 256*256 + else: + ext_arg = 0 + yield bc class CodeObjects(object): diff --git a/lab/show_pyc.py b/lab/show_pyc.py index b2cbb342..d6bbd921 100644 --- a/lab/show_pyc.py +++ b/lab/show_pyc.py @@ -4,7 +4,7 @@ def show_pyc_file(fname): f = open(fname, "rb") magic = f.read(4) moddate = f.read(4) - modtime = time.asctime(time.localtime(struct.unpack('L', moddate)[0])) + modtime = time.asctime(time.localtime(struct.unpack('