1
"""This module includes tests of the code object representation.
19
consts: ('None', '<code object g>')
21
>>> dump(f(4).__code__)
45
varnames: ('x', 'y', 'a', 'b', 'c')
57
>>> dump(attrs.__code__)
61
names: ('print', 'attr1', 'attr2', 'attr3')
69
>>> def optimize_away():
75
>>> dump(optimize_away.__code__)
85
consts: ("'doc string'", 'None')
87
>>> def keywordonly_args(a,b,*,k1):
91
>>> dump(keywordonly_args.__code__)
92
name: keywordonly_args
96
varnames: ('a', 'b', 'k1')
106
"""Yield a doctest-safe sequence of object reprs."""
109
if r.startswith("<code object"):
110
yield "<code object %s>" % elt.co_name
115
"""Print out a text representation of a code object."""
116
for attr in ["name", "argcount", "kwonlyargcount", "names", "varnames",
117
"cellvars", "freevars", "nlocals", "flags"]:
118
print("%s: %s" % (attr, getattr(co, "co_" + attr)))
119
print("consts:", tuple(consts(co.co_consts)))
121
def test_main(verbose=None):
122
from test.support import run_doctest
123
from test import test_code
124
run_doctest(test_code, verbose)
127
if __name__ == '__main__':