Skip to content

Commit

Permalink
Use co_lines to find executable lines if available
Browse files Browse the repository at this point in the history
  • Loading branch information
inducer committed Dec 5, 2023
1 parent 8c8f9d4 commit e45c1c5
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 12 deletions.
26 changes: 16 additions & 10 deletions pudb/lowlevel.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"""


import sys
import logging
from datetime import datetime

Expand Down Expand Up @@ -94,17 +95,22 @@ def _init_loggers():
# {{{ breakpoint validity

def generate_executable_lines_for_code(code):
lineno = code.co_firstlineno
yield lineno
# See https://github.com/python/cpython/blob/master/Objects/lnotab_notes.txt

for line_incr in code.co_lnotab[1::2]:
# NB: This code is specific to Python 3.6 and higher
# https://github.com/python/cpython/blob/v3.6.0/Objects/lnotab_notes.txt
if line_incr >= 0x80:
line_incr -= 0x100
lineno += line_incr
if sys.version_info >= (3, 10):
for _start, _end, lineno in code.co_lines():
if lineno is not None:
yield lineno
else:
lineno = code.co_firstlineno
yield lineno
# See https://github.com/python/cpython/blob/master/Objects/lnotab_notes.txt

for line_incr in code.co_lnotab[1::2]:
# NB: This code is specific to Python 3.6 and higher
# https://github.com/python/cpython/blob/v3.6.0/Objects/lnotab_notes.txt
if line_incr >= 0x80:
line_incr -= 0x100
lineno += line_incr
yield lineno


def get_executable_lines_for_codes_recursive(codes):
Expand Down
7 changes: 5 additions & 2 deletions pudb/test/test_lowlevel.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,13 @@ def main():
test_code = "a = 3*5\n" + 333 * "\n" + "b = 15"
expected = {
1,
128, # bogus,
255, # bogus,
335
}
if sys.version_info < (3, 12):
expected.update([
128, # bogus,
255, # bogus,
])
if sys.version_info >= (3, 11):
# See https://github.com/python/cpython/pull/94562 and
# https://peps.python.org/pep-0626/
Expand Down

0 comments on commit e45c1c5

Please sign in to comment.