Skip to content

Commit 20b9d2a

Browse files
authored
gh-94816: Improve coverage of decode_linetable (GH-94853)
This makes calls to co_lnotab to exercise this code, as well as generating synthetically large code to exercise the corner cases where line numbers need multiple bytes. Automerge-Triggered-By: GH:brandtbucher
1 parent 9b3f779 commit 20b9d2a

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

Lib/test/test_code.py

+23
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
freevars: ()
1818
nlocals: 2
1919
flags: 3
20+
lnotab: [4, 1, 10, 2]
2021
consts: ('None', '<code object g>')
2122
2223
>>> dump(f(4).__code__)
@@ -30,6 +31,7 @@
3031
freevars: ('x',)
3132
nlocals: 1
3233
flags: 19
34+
lnotab: [4, 1]
3335
consts: ('None',)
3436
3537
>>> def h(x, y):
@@ -50,6 +52,7 @@
5052
freevars: ()
5153
nlocals: 5
5254
flags: 3
55+
lnotab: [2, 1, 10, 1, 10, 1, 10, 1]
5356
consts: ('None',)
5457
5558
>>> def attrs(obj):
@@ -68,6 +71,7 @@
6871
freevars: ()
6972
nlocals: 1
7073
flags: 3
74+
lnotab: [2, 1, 46, 1, 46, 1]
7175
consts: ('None',)
7276
7377
>>> def optimize_away():
@@ -87,6 +91,7 @@
8791
freevars: ()
8892
nlocals: 0
8993
flags: 3
94+
lnotab: [2, 2, 2, 1, 2, 1]
9095
consts: ("'doc string'", 'None')
9196
9297
>>> def keywordonly_args(a,b,*,k1):
@@ -104,6 +109,7 @@
104109
freevars: ()
105110
nlocals: 3
106111
flags: 3
112+
lnotab: [2, 1]
107113
consts: ('None',)
108114
109115
>>> def posonly_args(a,b,/,c):
@@ -121,6 +127,7 @@
121127
freevars: ()
122128
nlocals: 3
123129
flags: 3
130+
lnotab: [2, 1]
124131
consts: ('None',)
125132
126133
"""
@@ -161,6 +168,7 @@ def dump(co):
161168
"kwonlyargcount", "names", "varnames",
162169
"cellvars", "freevars", "nlocals", "flags"]:
163170
print("%s: %s" % (attr, getattr(co, "co_" + attr)))
171+
print("lnotab:", list(co.co_lnotab))
164172
print("consts:", tuple(consts(co.co_consts)))
165173

166174
# Needed for test_closure_injection below
@@ -428,6 +436,21 @@ def func():
428436
self.assertIsNone(line)
429437
self.assertEqual(end_line, new_code.co_firstlineno + 1)
430438

439+
def test_large_lnotab(self):
440+
d = {}
441+
lines = (
442+
["def f():"] +
443+
[""] * (1 << 17) +
444+
[" pass"] * (1 << 17)
445+
)
446+
source = "\n".join(lines)
447+
exec(source, d)
448+
code = d["f"].__code__
449+
450+
expected = 1032 * [0, 127] + [0, 9] + ((1 << 17) - 1) * [2, 1]
451+
expected[0] = 2
452+
self.assertEqual(list(code.co_lnotab), expected)
453+
431454

432455
def isinterned(s):
433456
return s is sys.intern(('_' + s + '_')[1:-1])

0 commit comments

Comments
 (0)