Skip to content

Commit 92dae9e

Browse files
committed
Use co_lines() if it's available
1 parent 70e3345 commit 92dae9e

File tree

1 file changed

+28
-27
lines changed

1 file changed

+28
-27
lines changed

coverage/parser.py

Lines changed: 28 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -389,34 +389,35 @@ def child_parsers(self):
389389
"""
390390
return (ByteParser(self.text, code=c) for c in code_objects(self.code))
391391

392-
def _bytes_lines(self):
393-
"""Map byte offsets to line numbers in `code`.
394-
395-
Uses co_lnotab described in Python/compile.c to map byte offsets to
396-
line numbers. Produces a sequence: (b0, l0), (b1, l1), ...
397-
398-
Only byte offsets that correspond to line numbers are included in the
399-
results.
392+
def _line_numbers(self):
393+
"""Yield the line numbers possible in this code object.
400394
395+
Uses co_lnotab described in Python/compile.c to find the
396+
line numbers. Produces a sequence: l0, l1, ...
401397
"""
402-
# Adapted from dis.py in the standard library.
403-
byte_increments = bytes_to_ints(self.code.co_lnotab[0::2])
404-
line_increments = bytes_to_ints(self.code.co_lnotab[1::2])
405-
406-
last_line_num = None
407-
line_num = self.code.co_firstlineno
408-
byte_num = 0
409-
for byte_incr, line_incr in zip(byte_increments, line_increments):
410-
if byte_incr:
411-
if line_num != last_line_num:
412-
yield (byte_num, line_num)
413-
last_line_num = line_num
414-
byte_num += byte_incr
415-
if env.PYBEHAVIOR.negative_lnotab and line_incr >= 0x80:
416-
line_incr -= 0x100
417-
line_num += line_incr
418-
if line_num != last_line_num:
419-
yield (byte_num, line_num)
398+
if hasattr(self.code, "co_lines"):
399+
for _, _, line in self.code.co_lines():
400+
if line is not None:
401+
yield line
402+
else:
403+
# Adapted from dis.py in the standard library.
404+
byte_increments = bytes_to_ints(self.code.co_lnotab[0::2])
405+
line_increments = bytes_to_ints(self.code.co_lnotab[1::2])
406+
407+
last_line_num = None
408+
line_num = self.code.co_firstlineno
409+
byte_num = 0
410+
for byte_incr, line_incr in zip(byte_increments, line_increments):
411+
if byte_incr:
412+
if line_num != last_line_num:
413+
yield line_num
414+
last_line_num = line_num
415+
byte_num += byte_incr
416+
if env.PYBEHAVIOR.negative_lnotab and line_incr >= 0x80:
417+
line_incr -= 0x100
418+
line_num += line_incr
419+
if line_num != last_line_num:
420+
yield line_num
420421

421422
def _find_statements(self):
422423
"""Find the statements in `self.code`.
@@ -427,7 +428,7 @@ def _find_statements(self):
427428
"""
428429
for bp in self.child_parsers():
429430
# Get all of the lineno information from this code.
430-
for _, l in bp._bytes_lines():
431+
for l in bp._line_numbers():
431432
yield l
432433

433434

0 commit comments

Comments
 (0)