Skip to content

Commit

Permalink
pythongh-94816: Improve coverage of decode_linetable
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
mdboom committed Jul 14, 2022
1 parent 6cbb57f commit 751bacf
Showing 1 changed file with 24 additions and 1 deletion.
25 changes: 24 additions & 1 deletion Lib/test/test_code.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
freevars: ()
nlocals: 2
flags: 3
lnotab: b'\\x04\\x01\\n\\x02'
consts: ('None', '<code object g>')
>>> dump(f(4).__code__)
Expand All @@ -30,6 +31,7 @@
freevars: ('x',)
nlocals: 1
flags: 19
lnotab: b'\\x04\\x01'
consts: ('None',)
>>> def h(x, y):
Expand All @@ -50,6 +52,7 @@
freevars: ()
nlocals: 5
flags: 3
lnotab: b'\\x02\\x01\\n\\x01\\n\\x01\\n\\x01'
consts: ('None',)
>>> def attrs(obj):
Expand All @@ -68,6 +71,7 @@
freevars: ()
nlocals: 1
flags: 3
lnotab: b'\\x02\\x01.\\x01.\\x01'
consts: ('None',)
>>> def optimize_away():
Expand All @@ -87,6 +91,7 @@
freevars: ()
nlocals: 0
flags: 3
lnotab: b'\\x02\\x02\\x02\\x01\\x02\\x01'
consts: ("'doc string'", 'None')
>>> def keywordonly_args(a,b,*,k1):
Expand All @@ -104,6 +109,7 @@
freevars: ()
nlocals: 3
flags: 3
lnotab: b'\\x02\\x01'
consts: ('None',)
>>> def posonly_args(a,b,/,c):
Expand All @@ -121,6 +127,7 @@
freevars: ()
nlocals: 3
flags: 3
lnotab: b'\\x02\\x01'
consts: ('None',)
"""
Expand Down Expand Up @@ -159,7 +166,8 @@ def dump(co):
"""Print out a text representation of a code object."""
for attr in ["name", "argcount", "posonlyargcount",
"kwonlyargcount", "names", "varnames",
"cellvars", "freevars", "nlocals", "flags"]:
"cellvars", "freevars", "nlocals", "flags",
"lnotab"]:
print("%s: %s" % (attr, getattr(co, "co_" + attr)))
print("consts:", tuple(consts(co.co_consts)))

Expand Down Expand Up @@ -428,6 +436,21 @@ def func():
self.assertIsNone(line)
self.assertEqual(end_line, new_code.co_firstlineno + 1)

def test_large_lnotab(self):
d = {}
lines = (
["def f():"] +
[' """'] +
[' .'] * (1 << 17) +
[' """'] +
[" x = 1"] * (1 << 17)
)
source = "\n".join(lines)
exec(source, d)
code = d["f"].__code__

self.assertEqual(len(code.co_lnotab), 264208)


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

0 comments on commit 751bacf

Please sign in to comment.