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 |
| 19 | consts: ('None', '<code object g>') |
| 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 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 69 | >>> def optimize_away(): |
| 70 | ... 'doc string' |
| 71 | ... 'not a docstring' |
| 72 | ... 53 |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 73 | ... 0x53 |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 74 | |
Neal Norwitz | 221085d | 2007-02-25 20:55:47 +0000 | [diff] [blame] | 75 | >>> dump(optimize_away.__code__) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 76 | name: optimize_away |
| 77 | argcount: 0 |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 78 | kwonlyargcount: 0 |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 79 | names: () |
| 80 | varnames: () |
| 81 | cellvars: () |
| 82 | freevars: () |
| 83 | nlocals: 0 |
| 84 | flags: 67 |
| 85 | consts: ("'doc string'", 'None') |
| 86 | |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 87 | >>> def keywordonly_args(a,b,*,k1): |
| 88 | ... return a,b,k1 |
| 89 | ... |
| 90 | |
Neal Norwitz | 221085d | 2007-02-25 20:55:47 +0000 | [diff] [blame] | 91 | >>> dump(keywordonly_args.__code__) |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 92 | name: keywordonly_args |
| 93 | argcount: 2 |
| 94 | kwonlyargcount: 1 |
| 95 | names: () |
| 96 | varnames: ('a', 'b', 'k1') |
| 97 | cellvars: () |
| 98 | freevars: () |
| 99 | nlocals: 3 |
| 100 | flags: 67 |
| 101 | consts: ('None',) |
| 102 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 103 | """ |
| 104 | |
Alexandre Vassalotti | 7b82b40 | 2009-07-21 04:30:03 +0000 | [diff] [blame] | 105 | import unittest |
Collin Winter | 4222e9c | 2010-03-18 22:46:40 +0000 | [diff] [blame] | 106 | import weakref |
Alexandre Vassalotti | 7b82b40 | 2009-07-21 04:30:03 +0000 | [diff] [blame] | 107 | import _testcapi |
| 108 | |
Collin Winter | 4222e9c | 2010-03-18 22:46:40 +0000 | [diff] [blame] | 109 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 110 | def consts(t): |
| 111 | """Yield a doctest-safe sequence of object reprs.""" |
| 112 | for elt in t: |
| 113 | r = repr(elt) |
| 114 | if r.startswith("<code object"): |
| 115 | yield "<code object %s>" % elt.co_name |
| 116 | else: |
| 117 | yield r |
| 118 | |
| 119 | def dump(co): |
| 120 | """Print out a text representation of a code object.""" |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 121 | for attr in ["name", "argcount", "kwonlyargcount", "names", "varnames", |
| 122 | "cellvars", "freevars", "nlocals", "flags"]: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 123 | print("%s: %s" % (attr, getattr(co, "co_" + attr))) |
| 124 | print("consts:", tuple(consts(co.co_consts))) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 125 | |
Alexandre Vassalotti | 7b82b40 | 2009-07-21 04:30:03 +0000 | [diff] [blame] | 126 | |
| 127 | class CodeTest(unittest.TestCase): |
| 128 | |
| 129 | def test_newempty(self): |
| 130 | co = _testcapi.code_newempty("filename", "funcname", 15) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 131 | self.assertEqual(co.co_filename, "filename") |
| 132 | self.assertEqual(co.co_name, "funcname") |
| 133 | self.assertEqual(co.co_firstlineno, 15) |
Alexandre Vassalotti | 7b82b40 | 2009-07-21 04:30:03 +0000 | [diff] [blame] | 134 | |
| 135 | |
Collin Winter | 4222e9c | 2010-03-18 22:46:40 +0000 | [diff] [blame] | 136 | class CodeWeakRefTest(unittest.TestCase): |
| 137 | |
| 138 | def test_basic(self): |
| 139 | # Create a code object in a clean environment so that we know we have |
| 140 | # the only reference to it left. |
| 141 | namespace = {} |
| 142 | exec("def f(): pass", globals(), namespace) |
| 143 | f = namespace["f"] |
| 144 | del namespace |
| 145 | |
| 146 | self.called = False |
| 147 | def callback(code): |
| 148 | self.called = True |
| 149 | |
| 150 | # f is now the last reference to the function, and through it, the code |
| 151 | # object. While we hold it, check that we can create a weakref and |
| 152 | # deref it. Then delete it, and check that the callback gets called and |
| 153 | # the reference dies. |
| 154 | coderef = weakref.ref(f.__code__, callback) |
| 155 | self.assertTrue(bool(coderef())) |
| 156 | del f |
| 157 | self.assertFalse(bool(coderef())) |
| 158 | self.assertTrue(self.called) |
| 159 | |
| 160 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 161 | def test_main(verbose=None): |
Alexandre Vassalotti | 7b82b40 | 2009-07-21 04:30:03 +0000 | [diff] [blame] | 162 | from test.support import run_doctest, run_unittest |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 163 | from test import test_code |
| 164 | run_doctest(test_code, verbose) |
Collin Winter | 4222e9c | 2010-03-18 22:46:40 +0000 | [diff] [blame] | 165 | run_unittest(CodeTest, CodeWeakRefTest) |
Benjamin Peterson | ad9d48d | 2008-04-02 21:49:44 +0000 | [diff] [blame] | 166 | |
| 167 | |
Collin Winter | 4222e9c | 2010-03-18 22:46:40 +0000 | [diff] [blame] | 168 | if __name__ == "__main__": |
Benjamin Peterson | ad9d48d | 2008-04-02 21:49:44 +0000 | [diff] [blame] | 169 | test_main() |