1 | n/a | """This module includes tests of the code object representation. |
---|
2 | n/a | |
---|
3 | n/a | >>> def f(x): |
---|
4 | n/a | ... def g(y): |
---|
5 | n/a | ... return x + y |
---|
6 | n/a | ... return g |
---|
7 | n/a | ... |
---|
8 | n/a | |
---|
9 | n/a | >>> dump(f.__code__) |
---|
10 | n/a | name: f |
---|
11 | n/a | argcount: 1 |
---|
12 | n/a | kwonlyargcount: 0 |
---|
13 | n/a | names: () |
---|
14 | n/a | varnames: ('x', 'g') |
---|
15 | n/a | cellvars: ('x',) |
---|
16 | n/a | freevars: () |
---|
17 | n/a | nlocals: 2 |
---|
18 | n/a | flags: 3 |
---|
19 | n/a | consts: ('None', '<code object g>', "'f.<locals>.g'") |
---|
20 | n/a | |
---|
21 | n/a | >>> dump(f(4).__code__) |
---|
22 | n/a | name: g |
---|
23 | n/a | argcount: 1 |
---|
24 | n/a | kwonlyargcount: 0 |
---|
25 | n/a | names: () |
---|
26 | n/a | varnames: ('y',) |
---|
27 | n/a | cellvars: () |
---|
28 | n/a | freevars: ('x',) |
---|
29 | n/a | nlocals: 1 |
---|
30 | n/a | flags: 19 |
---|
31 | n/a | consts: ('None',) |
---|
32 | n/a | |
---|
33 | n/a | >>> def h(x, y): |
---|
34 | n/a | ... a = x + y |
---|
35 | n/a | ... b = x - y |
---|
36 | n/a | ... c = a * b |
---|
37 | n/a | ... return c |
---|
38 | n/a | ... |
---|
39 | n/a | |
---|
40 | n/a | >>> dump(h.__code__) |
---|
41 | n/a | name: h |
---|
42 | n/a | argcount: 2 |
---|
43 | n/a | kwonlyargcount: 0 |
---|
44 | n/a | names: () |
---|
45 | n/a | varnames: ('x', 'y', 'a', 'b', 'c') |
---|
46 | n/a | cellvars: () |
---|
47 | n/a | freevars: () |
---|
48 | n/a | nlocals: 5 |
---|
49 | n/a | flags: 67 |
---|
50 | n/a | consts: ('None',) |
---|
51 | n/a | |
---|
52 | n/a | >>> def attrs(obj): |
---|
53 | n/a | ... print(obj.attr1) |
---|
54 | n/a | ... print(obj.attr2) |
---|
55 | n/a | ... print(obj.attr3) |
---|
56 | n/a | |
---|
57 | n/a | >>> dump(attrs.__code__) |
---|
58 | n/a | name: attrs |
---|
59 | n/a | argcount: 1 |
---|
60 | n/a | kwonlyargcount: 0 |
---|
61 | n/a | names: ('print', 'attr1', 'attr2', 'attr3') |
---|
62 | n/a | varnames: ('obj',) |
---|
63 | n/a | cellvars: () |
---|
64 | n/a | freevars: () |
---|
65 | n/a | nlocals: 1 |
---|
66 | n/a | flags: 67 |
---|
67 | n/a | consts: ('None',) |
---|
68 | n/a | |
---|
69 | n/a | >>> def optimize_away(): |
---|
70 | n/a | ... 'doc string' |
---|
71 | n/a | ... 'not a docstring' |
---|
72 | n/a | ... 53 |
---|
73 | n/a | ... 0x53 |
---|
74 | n/a | |
---|
75 | n/a | >>> dump(optimize_away.__code__) |
---|
76 | n/a | name: optimize_away |
---|
77 | n/a | argcount: 0 |
---|
78 | n/a | kwonlyargcount: 0 |
---|
79 | n/a | names: () |
---|
80 | n/a | varnames: () |
---|
81 | n/a | cellvars: () |
---|
82 | n/a | freevars: () |
---|
83 | n/a | nlocals: 0 |
---|
84 | n/a | flags: 67 |
---|
85 | n/a | consts: ("'doc string'", 'None') |
---|
86 | n/a | |
---|
87 | n/a | >>> def keywordonly_args(a,b,*,k1): |
---|
88 | n/a | ... return a,b,k1 |
---|
89 | n/a | ... |
---|
90 | n/a | |
---|
91 | n/a | >>> dump(keywordonly_args.__code__) |
---|
92 | n/a | name: keywordonly_args |
---|
93 | n/a | argcount: 2 |
---|
94 | n/a | kwonlyargcount: 1 |
---|
95 | n/a | names: () |
---|
96 | n/a | varnames: ('a', 'b', 'k1') |
---|
97 | n/a | cellvars: () |
---|
98 | n/a | freevars: () |
---|
99 | n/a | nlocals: 3 |
---|
100 | n/a | flags: 67 |
---|
101 | n/a | consts: ('None',) |
---|
102 | n/a | |
---|
103 | n/a | """ |
---|
104 | n/a | |
---|
105 | n/a | import sys |
---|
106 | n/a | import unittest |
---|
107 | n/a | import weakref |
---|
108 | n/a | from test.support import run_doctest, run_unittest, cpython_only |
---|
109 | n/a | |
---|
110 | n/a | |
---|
111 | n/a | def consts(t): |
---|
112 | n/a | """Yield a doctest-safe sequence of object reprs.""" |
---|
113 | n/a | for elt in t: |
---|
114 | n/a | r = repr(elt) |
---|
115 | n/a | if r.startswith("<code object"): |
---|
116 | n/a | yield "<code object %s>" % elt.co_name |
---|
117 | n/a | else: |
---|
118 | n/a | yield r |
---|
119 | n/a | |
---|
120 | n/a | def dump(co): |
---|
121 | n/a | """Print out a text representation of a code object.""" |
---|
122 | n/a | for attr in ["name", "argcount", "kwonlyargcount", "names", "varnames", |
---|
123 | n/a | "cellvars", "freevars", "nlocals", "flags"]: |
---|
124 | n/a | print("%s: %s" % (attr, getattr(co, "co_" + attr))) |
---|
125 | n/a | print("consts:", tuple(consts(co.co_consts))) |
---|
126 | n/a | |
---|
127 | n/a | |
---|
128 | n/a | class CodeTest(unittest.TestCase): |
---|
129 | n/a | |
---|
130 | n/a | @cpython_only |
---|
131 | n/a | def test_newempty(self): |
---|
132 | n/a | import _testcapi |
---|
133 | n/a | co = _testcapi.code_newempty("filename", "funcname", 15) |
---|
134 | n/a | self.assertEqual(co.co_filename, "filename") |
---|
135 | n/a | self.assertEqual(co.co_name, "funcname") |
---|
136 | n/a | self.assertEqual(co.co_firstlineno, 15) |
---|
137 | n/a | |
---|
138 | n/a | |
---|
139 | n/a | def isinterned(s): |
---|
140 | n/a | return s is sys.intern(('_' + s + '_')[1:-1]) |
---|
141 | n/a | |
---|
142 | n/a | class CodeConstsTest(unittest.TestCase): |
---|
143 | n/a | |
---|
144 | n/a | def find_const(self, consts, value): |
---|
145 | n/a | for v in consts: |
---|
146 | n/a | if v == value: |
---|
147 | n/a | return v |
---|
148 | n/a | self.assertIn(value, consts) # raises an exception |
---|
149 | n/a | self.fail('Should never be reached') |
---|
150 | n/a | |
---|
151 | n/a | def assertIsInterned(self, s): |
---|
152 | n/a | if not isinterned(s): |
---|
153 | n/a | self.fail('String %r is not interned' % (s,)) |
---|
154 | n/a | |
---|
155 | n/a | def assertIsNotInterned(self, s): |
---|
156 | n/a | if isinterned(s): |
---|
157 | n/a | self.fail('String %r is interned' % (s,)) |
---|
158 | n/a | |
---|
159 | n/a | @cpython_only |
---|
160 | n/a | def test_interned_string(self): |
---|
161 | n/a | co = compile('res = "str_value"', '?', 'exec') |
---|
162 | n/a | v = self.find_const(co.co_consts, 'str_value') |
---|
163 | n/a | self.assertIsInterned(v) |
---|
164 | n/a | |
---|
165 | n/a | @cpython_only |
---|
166 | n/a | def test_interned_string_in_tuple(self): |
---|
167 | n/a | co = compile('res = ("str_value",)', '?', 'exec') |
---|
168 | n/a | v = self.find_const(co.co_consts, ('str_value',)) |
---|
169 | n/a | self.assertIsInterned(v[0]) |
---|
170 | n/a | |
---|
171 | n/a | @cpython_only |
---|
172 | n/a | def test_interned_string_in_frozenset(self): |
---|
173 | n/a | co = compile('res = a in {"str_value"}', '?', 'exec') |
---|
174 | n/a | v = self.find_const(co.co_consts, frozenset(('str_value',))) |
---|
175 | n/a | self.assertIsInterned(tuple(v)[0]) |
---|
176 | n/a | |
---|
177 | n/a | @cpython_only |
---|
178 | n/a | def test_interned_string_default(self): |
---|
179 | n/a | def f(a='str_value'): |
---|
180 | n/a | return a |
---|
181 | n/a | self.assertIsInterned(f()) |
---|
182 | n/a | |
---|
183 | n/a | @cpython_only |
---|
184 | n/a | def test_interned_string_with_null(self): |
---|
185 | n/a | co = compile(r'res = "str\0value!"', '?', 'exec') |
---|
186 | n/a | v = self.find_const(co.co_consts, 'str\0value!') |
---|
187 | n/a | self.assertIsNotInterned(v) |
---|
188 | n/a | |
---|
189 | n/a | |
---|
190 | n/a | class CodeWeakRefTest(unittest.TestCase): |
---|
191 | n/a | |
---|
192 | n/a | def test_basic(self): |
---|
193 | n/a | # Create a code object in a clean environment so that we know we have |
---|
194 | n/a | # the only reference to it left. |
---|
195 | n/a | namespace = {} |
---|
196 | n/a | exec("def f(): pass", globals(), namespace) |
---|
197 | n/a | f = namespace["f"] |
---|
198 | n/a | del namespace |
---|
199 | n/a | |
---|
200 | n/a | self.called = False |
---|
201 | n/a | def callback(code): |
---|
202 | n/a | self.called = True |
---|
203 | n/a | |
---|
204 | n/a | # f is now the last reference to the function, and through it, the code |
---|
205 | n/a | # object. While we hold it, check that we can create a weakref and |
---|
206 | n/a | # deref it. Then delete it, and check that the callback gets called and |
---|
207 | n/a | # the reference dies. |
---|
208 | n/a | coderef = weakref.ref(f.__code__, callback) |
---|
209 | n/a | self.assertTrue(bool(coderef())) |
---|
210 | n/a | del f |
---|
211 | n/a | self.assertFalse(bool(coderef())) |
---|
212 | n/a | self.assertTrue(self.called) |
---|
213 | n/a | |
---|
214 | n/a | |
---|
215 | n/a | def test_main(verbose=None): |
---|
216 | n/a | from test import test_code |
---|
217 | n/a | run_doctest(test_code, verbose) |
---|
218 | n/a | run_unittest(CodeTest, CodeConstsTest, CodeWeakRefTest) |
---|
219 | n/a | |
---|
220 | n/a | |
---|
221 | n/a | if __name__ == "__main__": |
---|
222 | n/a | test_main() |
---|