Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1 | """This module includes tests of the code object representation. |
| 2 | |
| 3 | >>> def f(x): |
| 4 | ... def g(y): |
| 5 | ... return x + y |
| 6 | ... return g |
| 7 | ... |
| 8 | |
Neal Norwitz | 221085d | 2007-02-25 20:55:47 +0000 | [diff] [blame] | 9 | >>> dump(f.__code__) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 10 | name: f |
| 11 | argcount: 1 |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 12 | kwonlyargcount: 0 |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 13 | names: () |
| 14 | varnames: ('x', 'g') |
| 15 | cellvars: ('x',) |
| 16 | freevars: () |
| 17 | nlocals: 2 |
| 18 | flags: 3 |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 19 | consts: ('None', '<code object g>', "'f.<locals>.g'") |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 20 | |
Neal Norwitz | 221085d | 2007-02-25 20:55:47 +0000 | [diff] [blame] | 21 | >>> dump(f(4).__code__) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 22 | name: g |
| 23 | argcount: 1 |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 24 | kwonlyargcount: 0 |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 25 | names: () |
| 26 | varnames: ('y',) |
| 27 | cellvars: () |
| 28 | freevars: ('x',) |
| 29 | nlocals: 1 |
| 30 | flags: 19 |
| 31 | consts: ('None',) |
| 32 | |
| 33 | >>> def h(x, y): |
| 34 | ... a = x + y |
| 35 | ... b = x - y |
| 36 | ... c = a * b |
| 37 | ... return c |
Tim Peters | 536cf99 | 2005-12-25 23:18:31 +0000 | [diff] [blame] | 38 | ... |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 39 | |
Neal Norwitz | 221085d | 2007-02-25 20:55:47 +0000 | [diff] [blame] | 40 | >>> dump(h.__code__) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 41 | name: h |
| 42 | argcount: 2 |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 43 | kwonlyargcount: 0 |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 44 | names: () |
| 45 | varnames: ('x', 'y', 'a', 'b', 'c') |
| 46 | cellvars: () |
| 47 | freevars: () |
| 48 | nlocals: 5 |
| 49 | flags: 67 |
| 50 | consts: ('None',) |
| 51 | |
| 52 | >>> def attrs(obj): |
Guido van Rossum | 7131f84 | 2007-02-09 20:13:25 +0000 | [diff] [blame] | 53 | ... print(obj.attr1) |
| 54 | ... print(obj.attr2) |
| 55 | ... print(obj.attr3) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 56 | |
Neal Norwitz | 221085d | 2007-02-25 20:55:47 +0000 | [diff] [blame] | 57 | >>> dump(attrs.__code__) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 58 | name: attrs |
| 59 | argcount: 1 |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 60 | kwonlyargcount: 0 |
Georg Brandl | 88fc664 | 2007-02-09 21:28:07 +0000 | [diff] [blame] | 61 | names: ('print', 'attr1', 'attr2', 'attr3') |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 62 | varnames: ('obj',) |
| 63 | cellvars: () |
| 64 | freevars: () |
| 65 | nlocals: 1 |
| 66 | flags: 67 |
| 67 | consts: ('None',) |
| 68 | |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 69 | >>> def keywordonly_args(a,b,*,k1): |
| 70 | ... return a,b,k1 |
| 71 | ... |
| 72 | |
Neal Norwitz | 221085d | 2007-02-25 20:55:47 +0000 | [diff] [blame] | 73 | >>> dump(keywordonly_args.__code__) |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 74 | name: keywordonly_args |
| 75 | argcount: 2 |
| 76 | kwonlyargcount: 1 |
| 77 | names: () |
| 78 | varnames: ('a', 'b', 'k1') |
| 79 | cellvars: () |
| 80 | freevars: () |
| 81 | nlocals: 3 |
| 82 | flags: 67 |
| 83 | consts: ('None',) |
| 84 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 85 | """ |
| 86 | |
Victor Stinner | a272409 | 2016-02-08 18:17:58 +0100 | [diff] [blame^] | 87 | import textwrap |
Alexandre Vassalotti | 7b82b40 | 2009-07-21 04:30:03 +0000 | [diff] [blame] | 88 | import unittest |
Collin Winter | 4222e9c | 2010-03-18 22:46:40 +0000 | [diff] [blame] | 89 | import weakref |
Victor Stinner | a272409 | 2016-02-08 18:17:58 +0100 | [diff] [blame^] | 90 | import warnings |
Serhiy Storchaka | 5cfc79d | 2014-02-07 10:06:39 +0200 | [diff] [blame] | 91 | from test.support import run_doctest, run_unittest, cpython_only |
Alexandre Vassalotti | 7b82b40 | 2009-07-21 04:30:03 +0000 | [diff] [blame] | 92 | |
Collin Winter | 4222e9c | 2010-03-18 22:46:40 +0000 | [diff] [blame] | 93 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 94 | def consts(t): |
| 95 | """Yield a doctest-safe sequence of object reprs.""" |
| 96 | for elt in t: |
| 97 | r = repr(elt) |
| 98 | if r.startswith("<code object"): |
| 99 | yield "<code object %s>" % elt.co_name |
| 100 | else: |
| 101 | yield r |
| 102 | |
| 103 | def dump(co): |
| 104 | """Print out a text representation of a code object.""" |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 105 | for attr in ["name", "argcount", "kwonlyargcount", "names", "varnames", |
| 106 | "cellvars", "freevars", "nlocals", "flags"]: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 107 | print("%s: %s" % (attr, getattr(co, "co_" + attr))) |
| 108 | print("consts:", tuple(consts(co.co_consts))) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 109 | |
Alexandre Vassalotti | 7b82b40 | 2009-07-21 04:30:03 +0000 | [diff] [blame] | 110 | |
| 111 | class CodeTest(unittest.TestCase): |
| 112 | |
Serhiy Storchaka | 5cfc79d | 2014-02-07 10:06:39 +0200 | [diff] [blame] | 113 | @cpython_only |
Alexandre Vassalotti | 7b82b40 | 2009-07-21 04:30:03 +0000 | [diff] [blame] | 114 | def test_newempty(self): |
Serhiy Storchaka | 5cfc79d | 2014-02-07 10:06:39 +0200 | [diff] [blame] | 115 | import _testcapi |
Alexandre Vassalotti | 7b82b40 | 2009-07-21 04:30:03 +0000 | [diff] [blame] | 116 | co = _testcapi.code_newempty("filename", "funcname", 15) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 117 | self.assertEqual(co.co_filename, "filename") |
| 118 | self.assertEqual(co.co_name, "funcname") |
| 119 | self.assertEqual(co.co_firstlineno, 15) |
Alexandre Vassalotti | 7b82b40 | 2009-07-21 04:30:03 +0000 | [diff] [blame] | 120 | |
Victor Stinner | a272409 | 2016-02-08 18:17:58 +0100 | [diff] [blame^] | 121 | def dump(self, co): |
| 122 | dump = {} |
| 123 | for attr in ["name", "argcount", "kwonlyargcount", "names", "varnames", |
| 124 | "cellvars", "freevars", "nlocals", "flags"]: |
| 125 | dump[attr] = getattr(co, "co_" + attr) |
| 126 | dump['consts'] = tuple(consts(co.co_consts)) |
| 127 | return dump |
| 128 | |
| 129 | def test_optimize_away(self): |
| 130 | ns = {} |
| 131 | with warnings.catch_warnings(): |
| 132 | warnings.filterwarnings('ignore', category=SyntaxWarning) |
| 133 | exec(textwrap.dedent(''' |
| 134 | def optimize_away(): |
| 135 | 'doc string' |
| 136 | 'not a docstring' |
| 137 | 53 |
| 138 | 0x53 |
| 139 | b'bytes' |
| 140 | 1.0 |
| 141 | True |
| 142 | False |
| 143 | None |
| 144 | ... |
| 145 | '''), ns) |
| 146 | |
| 147 | self.assertEqual(self.dump(ns['optimize_away'].__code__), |
| 148 | {'name': 'optimize_away', |
| 149 | 'argcount': 0, |
| 150 | 'kwonlyargcount': 0, |
| 151 | 'names': (), |
| 152 | 'varnames': (), |
| 153 | 'cellvars': (), |
| 154 | 'freevars': (), |
| 155 | 'nlocals': 0, |
| 156 | 'flags': 67, |
| 157 | 'consts': ("'doc string'", 'None')}) |
| 158 | |
Alexandre Vassalotti | 7b82b40 | 2009-07-21 04:30:03 +0000 | [diff] [blame] | 159 | |
Collin Winter | 4222e9c | 2010-03-18 22:46:40 +0000 | [diff] [blame] | 160 | class CodeWeakRefTest(unittest.TestCase): |
| 161 | |
| 162 | def test_basic(self): |
| 163 | # Create a code object in a clean environment so that we know we have |
| 164 | # the only reference to it left. |
| 165 | namespace = {} |
| 166 | exec("def f(): pass", globals(), namespace) |
| 167 | f = namespace["f"] |
| 168 | del namespace |
| 169 | |
| 170 | self.called = False |
| 171 | def callback(code): |
| 172 | self.called = True |
| 173 | |
| 174 | # f is now the last reference to the function, and through it, the code |
| 175 | # object. While we hold it, check that we can create a weakref and |
| 176 | # deref it. Then delete it, and check that the callback gets called and |
| 177 | # the reference dies. |
| 178 | coderef = weakref.ref(f.__code__, callback) |
| 179 | self.assertTrue(bool(coderef())) |
| 180 | del f |
| 181 | self.assertFalse(bool(coderef())) |
| 182 | self.assertTrue(self.called) |
| 183 | |
| 184 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 185 | def test_main(verbose=None): |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 186 | from test import test_code |
| 187 | run_doctest(test_code, verbose) |
Collin Winter | 4222e9c | 2010-03-18 22:46:40 +0000 | [diff] [blame] | 188 | run_unittest(CodeTest, CodeWeakRefTest) |
Benjamin Peterson | ad9d48d | 2008-04-02 21:49:44 +0000 | [diff] [blame] | 189 | |
| 190 | |
Collin Winter | 4222e9c | 2010-03-18 22:46:40 +0000 | [diff] [blame] | 191 | if __name__ == "__main__": |
Benjamin Peterson | ad9d48d | 2008-04-02 21:49:44 +0000 | [diff] [blame] | 192 | test_main() |