@@ -389,34 +389,35 @@ def child_parsers(self):
389
389
"""
390
390
return (ByteParser (self .text , code = c ) for c in code_objects (self .code ))
391
391
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.
400
394
395
+ Uses co_lnotab described in Python/compile.c to find the
396
+ line numbers. Produces a sequence: l0, l1, ...
401
397
"""
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
420
421
421
422
def _find_statements (self ):
422
423
"""Find the statements in `self.code`.
@@ -427,7 +428,7 @@ def _find_statements(self):
427
428
"""
428
429
for bp in self .child_parsers ():
429
430
# Get all of the lineno information from this code.
430
- for _ , l in bp ._bytes_lines ():
431
+ for l in bp ._line_numbers ():
431
432
yield l
432
433
433
434
0 commit comments