1 """This module includes tests of the code object representation.
18 consts: ('None', '<code object g>')
20 >>> dump(f(4).func_code)
41 varnames: ('x', 'y', 'a', 'b', 'c')
53 >>> dump(attrs.func_code)
56 names: ('attr1', 'attr2', 'attr3')
64 >>> def optimize_away():
70 >>> dump(optimize_away.func_code)
79 consts: ("'doc string'", 'None')
87 """Yield a doctest-safe sequence of object reprs."""
90 if r
.startswith("<code object"):
91 yield "<code object %s>" % elt
.co_name
96 """Print out a text representation of a code object."""
97 for attr
in ["name", "argcount", "names", "varnames", "cellvars",
98 "freevars", "nlocals", "flags"]:
99 print "%s: %s" % (attr
, getattr(co
, "co_" + attr
))
100 print "consts:", tuple(consts(co
.co_consts
))
103 class CodeTest(unittest
.TestCase
):
105 def test_newempty(self
):
106 co
= _testcapi
.code_newempty("filename", "funcname", 15)
107 self
.assertEquals(co
.co_filename
, "filename")
108 self
.assertEquals(co
.co_name
, "funcname")
109 self
.assertEquals(co
.co_firstlineno
, 15)
112 def test_main(verbose
=None):
113 from test
.test_support
import run_doctest
, run_unittest
114 from test
import test_code
115 run_doctest(test_code
, verbose
)
116 run_unittest(CodeTest
)
119 if __name__
== '__main__':