Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gh-94816: Improve coverage of decode_linetable #94853

Merged
merged 4 commits into from
Jul 14, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions 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: [4, 1, 10, 2]
consts: ('None', '<code object g>')

>>> dump(f(4).__code__)
Expand All @@ -30,6 +31,7 @@
freevars: ('x',)
nlocals: 1
flags: 19
lnotab: [4, 1]
consts: ('None',)

>>> def h(x, y):
Expand All @@ -50,6 +52,7 @@
freevars: ()
nlocals: 5
flags: 3
lnotab: [2, 1, 10, 1, 10, 1, 10, 1]
consts: ('None',)

>>> def attrs(obj):
Expand All @@ -68,6 +71,7 @@
freevars: ()
nlocals: 1
flags: 3
lnotab: [2, 1, 46, 1, 46, 1]
consts: ('None',)

>>> def optimize_away():
Expand All @@ -87,6 +91,7 @@
freevars: ()
nlocals: 0
flags: 3
lnotab: [2, 2, 2, 1, 2, 1]
consts: ("'doc string'", 'None')

>>> def keywordonly_args(a,b,*,k1):
Expand All @@ -104,6 +109,7 @@
freevars: ()
nlocals: 3
flags: 3
lnotab: [2, 1]
consts: ('None',)

>>> def posonly_args(a,b,/,c):
Expand All @@ -121,6 +127,7 @@
freevars: ()
nlocals: 3
flags: 3
lnotab: [2, 1]
consts: ('None',)

"""
Expand Down Expand Up @@ -161,6 +168,7 @@ def dump(co):
"kwonlyargcount", "names", "varnames",
"cellvars", "freevars", "nlocals", "flags"]:
print("%s: %s" % (attr, getattr(co, "co_" + attr)))
print("lnotab:", list(co.co_lnotab))
print("consts:", tuple(consts(co.co_consts)))

# Needed for test_closure_injection below
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) +
[" pass"] * (1 << 17)
)
source = "\n".join(lines)
exec(source, d)
code = d["f"].__code__

expected = 1032 * [0, 127] + [0, 9] + ((1 << 17) - 1) * [2, 1]
expected[0] = 2
self.assertEqual(list(code.co_lnotab), expected)


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