Skip to content

Commit f24b810

Browse files
authored
bpo-42562: Fix issue when dis failed to parse function that has no line numbers (GH-23632)
Fix issue when dis failed to parse function that has only annotations
1 parent db68544 commit f24b810

File tree

3 files changed

+20
-1
lines changed

3 files changed

+20
-1
lines changed

Lib/dis.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -384,7 +384,7 @@ def _disassemble_bytes(code, lasti=-1, varnames=None, names=None,
384384
constants=None, cells=None, linestarts=None,
385385
*, file=None, line_offset=0):
386386
# Omit the line number column entirely if we have no line number info
387-
show_lineno = linestarts is not None
387+
show_lineno = bool(linestarts)
388388
if show_lineno:
389389
maxlineno = max(linestarts.values()) + line_offset
390390
if maxlineno >= 1000:

Lib/test/test_dis.py

+17
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,20 @@ def bug1333982(x=[]):
166166
bug1333982.__code__.co_firstlineno + 2,
167167
bug1333982.__code__.co_firstlineno + 1)
168168

169+
170+
def bug42562():
171+
pass
172+
173+
174+
# Set line number for 'pass' to None
175+
bug42562.__code__ = bug42562.__code__.replace(co_linetable=b'\x04\x80\xff\x80')
176+
177+
178+
dis_bug42562 = """\
179+
0 LOAD_CONST 0 (None)
180+
2 RETURN_VALUE
181+
"""
182+
169183
_BIG_LINENO_FORMAT = """\
170184
%3d 0 LOAD_GLOBAL 0 (spam)
171185
2 POP_TOP
@@ -520,6 +534,9 @@ def test_bug_1333982(self):
520534

521535
self.do_disassembly_test(bug1333982, dis_bug1333982)
522536

537+
def test_bug_42562(self):
538+
self.do_disassembly_test(bug42562, dis_bug42562)
539+
523540
def test_big_linenos(self):
524541
def func(count):
525542
namespace = {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix issue when dis failed to parse function that has no line numbers. Patch
2+
provided by Yurii Karabas.

0 commit comments

Comments
 (0)