OLD | NEW |
1 """This module includes tests of the code object representation. | 1 """This module includes tests of the code object representation. |
2 | 2 |
3 >>> def f(x): | 3 >>> def f(x): |
4 ... def g(y): | 4 ... def g(y): |
5 ... return x + y | 5 ... return x + y |
6 ... return g | 6 ... return g |
7 ... | 7 ... |
8 | 8 |
9 >>> dump(f.__code__) | 9 >>> dump(f.__code__) |
10 name: f | 10 name: f |
(...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
121 for attr in ["name", "argcount", "kwonlyargcount", "names", "varnames", | 121 for attr in ["name", "argcount", "kwonlyargcount", "names", "varnames", |
122 "cellvars", "freevars", "nlocals", "flags"]: | 122 "cellvars", "freevars", "nlocals", "flags"]: |
123 print("%s: %s" % (attr, getattr(co, "co_" + attr))) | 123 print("%s: %s" % (attr, getattr(co, "co_" + attr))) |
124 print("consts:", tuple(consts(co.co_consts))) | 124 print("consts:", tuple(consts(co.co_consts))) |
125 | 125 |
126 | 126 |
127 class CodeTest(unittest.TestCase): | 127 class CodeTest(unittest.TestCase): |
128 | 128 |
129 def test_newempty(self): | 129 def test_newempty(self): |
130 co = _testcapi.code_newempty("filename", "funcname", 15) | 130 co = _testcapi.code_newempty("filename", "funcname", 15) |
131 self.assertEquals(co.co_filename, "filename") | 131 self.assertEqual(co.co_filename, "filename") |
132 self.assertEquals(co.co_name, "funcname") | 132 self.assertEqual(co.co_name, "funcname") |
133 self.assertEquals(co.co_firstlineno, 15) | 133 self.assertEqual(co.co_firstlineno, 15) |
134 | 134 |
135 | 135 |
136 class CodeWeakRefTest(unittest.TestCase): | 136 class CodeWeakRefTest(unittest.TestCase): |
137 | 137 |
138 def test_basic(self): | 138 def test_basic(self): |
139 # Create a code object in a clean environment so that we know we have | 139 # Create a code object in a clean environment so that we know we have |
140 # the only reference to it left. | 140 # the only reference to it left. |
141 namespace = {} | 141 namespace = {} |
142 exec("def f(): pass", globals(), namespace) | 142 exec("def f(): pass", globals(), namespace) |
143 f = namespace["f"] | 143 f = namespace["f"] |
(...skipping 16 matching lines...) Expand all Loading... |
160 | 160 |
161 def test_main(verbose=None): | 161 def test_main(verbose=None): |
162 from test.support import run_doctest, run_unittest | 162 from test.support import run_doctest, run_unittest |
163 from test import test_code | 163 from test import test_code |
164 run_doctest(test_code, verbose) | 164 run_doctest(test_code, verbose) |
165 run_unittest(CodeTest, CodeWeakRefTest) | 165 run_unittest(CodeTest, CodeWeakRefTest) |
166 | 166 |
167 | 167 |
168 if __name__ == "__main__": | 168 if __name__ == "__main__": |
169 test_main() | 169 test_main() |
OLD | NEW |