1edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep"""This module includes tests of the code object representation.
2edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
3edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep>>> def f(x):
4edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep...     def g(y):
5edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep...         return x + y
6edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep...     return g
7edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep...
8edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
9edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep>>> dump(f.func_code)
10edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepname: f
11edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepargcount: 1
12edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepnames: ()
13edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepvarnames: ('x', 'g')
14edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepcellvars: ('x',)
15edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepfreevars: ()
16edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepnlocals: 2
17edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepflags: 3
18edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepconsts: ('None', '<code object g>')
19edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
20edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep>>> dump(f(4).func_code)
21edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepname: g
22edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepargcount: 1
23edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepnames: ()
24edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepvarnames: ('y',)
25edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepcellvars: ()
26edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepfreevars: ('x',)
27edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepnlocals: 1
28edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepflags: 19
29edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepconsts: ('None',)
30edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
31edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep>>> def h(x, y):
32edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep...     a = x + y
33edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep...     b = x - y
34edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep...     c = a * b
35edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep...     return c
36edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep...
37edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep>>> dump(h.func_code)
38edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepname: h
39edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepargcount: 2
40edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepnames: ()
41edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepvarnames: ('x', 'y', 'a', 'b', 'c')
42edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepcellvars: ()
43edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepfreevars: ()
44edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepnlocals: 5
45edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepflags: 67
46edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepconsts: ('None',)
47edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
48edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep>>> def attrs(obj):
49edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep...     print obj.attr1
50edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep...     print obj.attr2
51edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep...     print obj.attr3
52edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
53edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep>>> dump(attrs.func_code)
54edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepname: attrs
55edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepargcount: 1
56edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepnames: ('attr1', 'attr2', 'attr3')
57edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepvarnames: ('obj',)
58edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepcellvars: ()
59edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepfreevars: ()
60edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepnlocals: 1
61edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepflags: 67
62edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepconsts: ('None',)
63edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
64edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep>>> def optimize_away():
65edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep...     'doc string'
66edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep...     'not a docstring'
67edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep...     53
68edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep...     53L
69edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
70edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep>>> dump(optimize_away.func_code)
71edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepname: optimize_away
72edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepargcount: 0
73edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepnames: ()
74edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepvarnames: ()
75edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepcellvars: ()
76edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepfreevars: ()
77edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepnlocals: 0
78edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepflags: 67
79edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepconsts: ("'doc string'", 'None')
80edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
81edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep"""
82edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
83edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepimport unittest
84edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepimport weakref
85edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepimport _testcapi
86edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
87edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
88edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepdef consts(t):
89edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    """Yield a doctest-safe sequence of object reprs."""
90edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    for elt in t:
91edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        r = repr(elt)
92edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        if r.startswith("<code object"):
93edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            yield "<code object %s>" % elt.co_name
94edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        else:
95edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            yield r
96edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
97edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepdef dump(co):
98edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    """Print out a text representation of a code object."""
99edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    for attr in ["name", "argcount", "names", "varnames", "cellvars",
100edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                 "freevars", "nlocals", "flags"]:
101edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        print "%s: %s" % (attr, getattr(co, "co_" + attr))
102edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    print "consts:", tuple(consts(co.co_consts))
103edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
104edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
105edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepclass CodeTest(unittest.TestCase):
106edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
107edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def test_newempty(self):
108edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        co = _testcapi.code_newempty("filename", "funcname", 15)
109edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(co.co_filename, "filename")
110edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(co.co_name, "funcname")
111edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(co.co_firstlineno, 15)
112edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
113edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
114edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepclass CodeWeakRefTest(unittest.TestCase):
115edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
116edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def test_basic(self):
117edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # Create a code object in a clean environment so that we know we have
118edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # the only reference to it left.
119edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        namespace = {}
120edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        exec "def f(): pass" in globals(), namespace
121edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        f = namespace["f"]
122edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        del namespace
123edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
124edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.called = False
125edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        def callback(code):
126edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.called = True
127edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
128edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # f is now the last reference to the function, and through it, the code
129edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # object.  While we hold it, check that we can create a weakref and
130edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # deref it.  Then delete it, and check that the callback gets called and
131edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # the reference dies.
132edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        coderef = weakref.ref(f.__code__, callback)
133edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertTrue(bool(coderef()))
134edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        del f
135edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertFalse(bool(coderef()))
136edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertTrue(self.called)
137edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
138edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
139edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepdef test_main(verbose=None):
140edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    from test.test_support import run_doctest, run_unittest
141edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    from test import test_code
142edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    run_doctest(test_code, verbose)
143edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    run_unittest(CodeTest, CodeWeakRefTest)
144edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
145edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
146edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepif __name__ == "__main__":
147edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    test_main()
148