Mercurial > cpython-lru_cache-weakref
annotate Lib/test/test_code.py @ 80661:94de77bd0b4b 2.7
Issue #16194: document imp.load_dynamic problems
when read several extension modules from the same binary object file.
| author | Andrew Svetlov <andrew.svetlov@gmail.com> |
|---|---|
| date | Thu, 29 Nov 2012 19:51:28 +0200 |
| parents | 614c84670c63 |
| children |
| rev | line source |
|---|---|
| 35742 | 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 | |
| 9 >>> dump(f.func_code) | |
| 10 name: f | |
| 11 argcount: 1 | |
| 12 names: () | |
| 13 varnames: ('x', 'g') | |
| 14 cellvars: ('x',) | |
| 15 freevars: () | |
| 16 nlocals: 2 | |
| 17 flags: 3 | |
| 18 consts: ('None', '<code object g>') | |
| 19 | |
| 20 >>> dump(f(4).func_code) | |
| 21 name: g | |
| 22 argcount: 1 | |
| 23 names: () | |
| 24 varnames: ('y',) | |
| 25 cellvars: () | |
| 26 freevars: ('x',) | |
| 27 nlocals: 1 | |
| 28 flags: 19 | |
| 29 consts: ('None',) | |
| 30 | |
| 31 >>> def h(x, y): | |
| 32 ... a = x + y | |
| 33 ... b = x - y | |
| 34 ... c = a * b | |
| 35 ... return c | |
|
36121
686306a67e9b
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
35742
diff
changeset
|
36 ... |
| 35742 | 37 >>> dump(h.func_code) |
| 38 name: h | |
| 39 argcount: 2 | |
| 40 names: () | |
| 41 varnames: ('x', 'y', 'a', 'b', 'c') | |
| 42 cellvars: () | |
| 43 freevars: () | |
| 44 nlocals: 5 | |
| 45 flags: 67 | |
| 46 consts: ('None',) | |
| 47 | |
| 48 >>> def attrs(obj): | |
| 49 ... print obj.attr1 | |
| 50 ... print obj.attr2 | |
| 51 ... print obj.attr3 | |
| 52 | |
| 53 >>> dump(attrs.func_code) | |
| 54 name: attrs | |
| 55 argcount: 1 | |
| 56 names: ('attr1', 'attr2', 'attr3') | |
| 57 varnames: ('obj',) | |
| 58 cellvars: () | |
| 59 freevars: () | |
| 60 nlocals: 1 | |
| 61 flags: 67 | |
| 62 consts: ('None',) | |
| 63 | |
|
39364
8ef3f8cf90e1
Bug #1333982: string/number constants were inappropriately stored
Neal Norwitz <nnorwitz@gmail.com>
parents:
36121
diff
changeset
|
64 >>> def optimize_away(): |
|
8ef3f8cf90e1
Bug #1333982: string/number constants were inappropriately stored
Neal Norwitz <nnorwitz@gmail.com>
parents:
36121
diff
changeset
|
65 ... 'doc string' |
|
8ef3f8cf90e1
Bug #1333982: string/number constants were inappropriately stored
Neal Norwitz <nnorwitz@gmail.com>
parents:
36121
diff
changeset
|
66 ... 'not a docstring' |
|
8ef3f8cf90e1
Bug #1333982: string/number constants were inappropriately stored
Neal Norwitz <nnorwitz@gmail.com>
parents:
36121
diff
changeset
|
67 ... 53 |
|
8ef3f8cf90e1
Bug #1333982: string/number constants were inappropriately stored
Neal Norwitz <nnorwitz@gmail.com>
parents:
36121
diff
changeset
|
68 ... 53L |
|
8ef3f8cf90e1
Bug #1333982: string/number constants were inappropriately stored
Neal Norwitz <nnorwitz@gmail.com>
parents:
36121
diff
changeset
|
69 |
|
8ef3f8cf90e1
Bug #1333982: string/number constants were inappropriately stored
Neal Norwitz <nnorwitz@gmail.com>
parents:
36121
diff
changeset
|
70 >>> dump(optimize_away.func_code) |
|
8ef3f8cf90e1
Bug #1333982: string/number constants were inappropriately stored
Neal Norwitz <nnorwitz@gmail.com>
parents:
36121
diff
changeset
|
71 name: optimize_away |
|
8ef3f8cf90e1
Bug #1333982: string/number constants were inappropriately stored
Neal Norwitz <nnorwitz@gmail.com>
parents:
36121
diff
changeset
|
72 argcount: 0 |
|
8ef3f8cf90e1
Bug #1333982: string/number constants were inappropriately stored
Neal Norwitz <nnorwitz@gmail.com>
parents:
36121
diff
changeset
|
73 names: () |
|
8ef3f8cf90e1
Bug #1333982: string/number constants were inappropriately stored
Neal Norwitz <nnorwitz@gmail.com>
parents:
36121
diff
changeset
|
74 varnames: () |
|
8ef3f8cf90e1
Bug #1333982: string/number constants were inappropriately stored
Neal Norwitz <nnorwitz@gmail.com>
parents:
36121
diff
changeset
|
75 cellvars: () |
|
8ef3f8cf90e1
Bug #1333982: string/number constants were inappropriately stored
Neal Norwitz <nnorwitz@gmail.com>
parents:
36121
diff
changeset
|
76 freevars: () |
|
8ef3f8cf90e1
Bug #1333982: string/number constants were inappropriately stored
Neal Norwitz <nnorwitz@gmail.com>
parents:
36121
diff
changeset
|
77 nlocals: 0 |
|
8ef3f8cf90e1
Bug #1333982: string/number constants were inappropriately stored
Neal Norwitz <nnorwitz@gmail.com>
parents:
36121
diff
changeset
|
78 flags: 67 |
|
8ef3f8cf90e1
Bug #1333982: string/number constants were inappropriately stored
Neal Norwitz <nnorwitz@gmail.com>
parents:
36121
diff
changeset
|
79 consts: ("'doc string'", 'None') |
|
8ef3f8cf90e1
Bug #1333982: string/number constants were inappropriately stored
Neal Norwitz <nnorwitz@gmail.com>
parents:
36121
diff
changeset
|
80 |
| 35742 | 81 """ |
| 82 | |
| 53930 | 83 import unittest |
|
59694
22f589a8e3ab
Add support for weak references to code objects. This will be used by an optimization in the incoming Python 3 JIT.
Collin Winter <collinw@gmail.com>
parents:
53930
diff
changeset
|
84 import weakref |
| 53930 | 85 import _testcapi |
| 86 | |
|
59694
22f589a8e3ab
Add support for weak references to code objects. This will be used by an optimization in the incoming Python 3 JIT.
Collin Winter <collinw@gmail.com>
parents:
53930
diff
changeset
|
87 |
| 35742 | 88 def consts(t): |
| 89 """Yield a doctest-safe sequence of object reprs.""" | |
| 90 for elt in t: | |
| 91 r = repr(elt) | |
| 92 if r.startswith("<code object"): | |
| 93 yield "<code object %s>" % elt.co_name | |
| 94 else: | |
| 95 yield r | |
| 96 | |
| 97 def dump(co): | |
| 98 """Print out a text representation of a code object.""" | |
| 99 for attr in ["name", "argcount", "names", "varnames", "cellvars", | |
| 100 "freevars", "nlocals", "flags"]: | |
| 101 print "%s: %s" % (attr, getattr(co, "co_" + attr)) | |
| 102 print "consts:", tuple(consts(co.co_consts)) | |
| 103 | |
| 53930 | 104 |
| 105 class CodeTest(unittest.TestCase): | |
| 106 | |
| 107 def test_newempty(self): | |
| 108 co = _testcapi.code_newempty("filename", "funcname", 15) | |
|
66348
614c84670c63
Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
59694
diff
changeset
|
109 self.assertEqual(co.co_filename, "filename") |
|
614c84670c63
Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
59694
diff
changeset
|
110 self.assertEqual(co.co_name, "funcname") |
|
614c84670c63
Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
59694
diff
changeset
|
111 self.assertEqual(co.co_firstlineno, 15) |
| 53930 | 112 |
| 113 | |
|
59694
22f589a8e3ab
Add support for weak references to code objects. This will be used by an optimization in the incoming Python 3 JIT.
Collin Winter <collinw@gmail.com>
parents:
53930
diff
changeset
|
114 class CodeWeakRefTest(unittest.TestCase): |
|
22f589a8e3ab
Add support for weak references to code objects. This will be used by an optimization in the incoming Python 3 JIT.
Collin Winter <collinw@gmail.com>
parents:
53930
diff
changeset
|
115 |
|
22f589a8e3ab
Add support for weak references to code objects. This will be used by an optimization in the incoming Python 3 JIT.
Collin Winter <collinw@gmail.com>
parents:
53930
diff
changeset
|
116 def test_basic(self): |
|
22f589a8e3ab
Add support for weak references to code objects. This will be used by an optimization in the incoming Python 3 JIT.
Collin Winter <collinw@gmail.com>
parents:
53930
diff
changeset
|
117 # Create a code object in a clean environment so that we know we have |
|
22f589a8e3ab
Add support for weak references to code objects. This will be used by an optimization in the incoming Python 3 JIT.
Collin Winter <collinw@gmail.com>
parents:
53930
diff
changeset
|
118 # the only reference to it left. |
|
22f589a8e3ab
Add support for weak references to code objects. This will be used by an optimization in the incoming Python 3 JIT.
Collin Winter <collinw@gmail.com>
parents:
53930
diff
changeset
|
119 namespace = {} |
|
22f589a8e3ab
Add support for weak references to code objects. This will be used by an optimization in the incoming Python 3 JIT.
Collin Winter <collinw@gmail.com>
parents:
53930
diff
changeset
|
120 exec "def f(): pass" in globals(), namespace |
|
22f589a8e3ab
Add support for weak references to code objects. This will be used by an optimization in the incoming Python 3 JIT.
Collin Winter <collinw@gmail.com>
parents:
53930
diff
changeset
|
121 f = namespace["f"] |
|
22f589a8e3ab
Add support for weak references to code objects. This will be used by an optimization in the incoming Python 3 JIT.
Collin Winter <collinw@gmail.com>
parents:
53930
diff
changeset
|
122 del namespace |
|
22f589a8e3ab
Add support for weak references to code objects. This will be used by an optimization in the incoming Python 3 JIT.
Collin Winter <collinw@gmail.com>
parents:
53930
diff
changeset
|
123 |
|
22f589a8e3ab
Add support for weak references to code objects. This will be used by an optimization in the incoming Python 3 JIT.
Collin Winter <collinw@gmail.com>
parents:
53930
diff
changeset
|
124 self.called = False |
|
22f589a8e3ab
Add support for weak references to code objects. This will be used by an optimization in the incoming Python 3 JIT.
Collin Winter <collinw@gmail.com>
parents:
53930
diff
changeset
|
125 def callback(code): |
|
22f589a8e3ab
Add support for weak references to code objects. This will be used by an optimization in the incoming Python 3 JIT.
Collin Winter <collinw@gmail.com>
parents:
53930
diff
changeset
|
126 self.called = True |
|
22f589a8e3ab
Add support for weak references to code objects. This will be used by an optimization in the incoming Python 3 JIT.
Collin Winter <collinw@gmail.com>
parents:
53930
diff
changeset
|
127 |
|
22f589a8e3ab
Add support for weak references to code objects. This will be used by an optimization in the incoming Python 3 JIT.
Collin Winter <collinw@gmail.com>
parents:
53930
diff
changeset
|
128 # f is now the last reference to the function, and through it, the code |
|
22f589a8e3ab
Add support for weak references to code objects. This will be used by an optimization in the incoming Python 3 JIT.
Collin Winter <collinw@gmail.com>
parents:
53930
diff
changeset
|
129 # object. While we hold it, check that we can create a weakref and |
|
22f589a8e3ab
Add support for weak references to code objects. This will be used by an optimization in the incoming Python 3 JIT.
Collin Winter <collinw@gmail.com>
parents:
53930
diff
changeset
|
130 # deref it. Then delete it, and check that the callback gets called and |
|
22f589a8e3ab
Add support for weak references to code objects. This will be used by an optimization in the incoming Python 3 JIT.
Collin Winter <collinw@gmail.com>
parents:
53930
diff
changeset
|
131 # the reference dies. |
|
22f589a8e3ab
Add support for weak references to code objects. This will be used by an optimization in the incoming Python 3 JIT.
Collin Winter <collinw@gmail.com>
parents:
53930
diff
changeset
|
132 coderef = weakref.ref(f.__code__, callback) |
|
22f589a8e3ab
Add support for weak references to code objects. This will be used by an optimization in the incoming Python 3 JIT.
Collin Winter <collinw@gmail.com>
parents:
53930
diff
changeset
|
133 self.assertTrue(bool(coderef())) |
|
22f589a8e3ab
Add support for weak references to code objects. This will be used by an optimization in the incoming Python 3 JIT.
Collin Winter <collinw@gmail.com>
parents:
53930
diff
changeset
|
134 del f |
|
22f589a8e3ab
Add support for weak references to code objects. This will be used by an optimization in the incoming Python 3 JIT.
Collin Winter <collinw@gmail.com>
parents:
53930
diff
changeset
|
135 self.assertFalse(bool(coderef())) |
|
22f589a8e3ab
Add support for weak references to code objects. This will be used by an optimization in the incoming Python 3 JIT.
Collin Winter <collinw@gmail.com>
parents:
53930
diff
changeset
|
136 self.assertTrue(self.called) |
|
22f589a8e3ab
Add support for weak references to code objects. This will be used by an optimization in the incoming Python 3 JIT.
Collin Winter <collinw@gmail.com>
parents:
53930
diff
changeset
|
137 |
|
22f589a8e3ab
Add support for weak references to code objects. This will be used by an optimization in the incoming Python 3 JIT.
Collin Winter <collinw@gmail.com>
parents:
53930
diff
changeset
|
138 |
| 35742 | 139 def test_main(verbose=None): |
| 53930 | 140 from test.test_support import run_doctest, run_unittest |
| 35742 | 141 from test import test_code |
| 142 run_doctest(test_code, verbose) | |
|
59694
22f589a8e3ab
Add support for weak references to code objects. This will be used by an optimization in the incoming Python 3 JIT.
Collin Winter <collinw@gmail.com>
parents:
53930
diff
changeset
|
143 run_unittest(CodeTest, CodeWeakRefTest) |
|
46242
4c5065ddb9bd
Add ``if __name__ == '__main__'`` to some test files where it didn't take a lot
Brett Cannon <bcannon@gmail.com>
parents:
39364
diff
changeset
|
144 |
|
4c5065ddb9bd
Add ``if __name__ == '__main__'`` to some test files where it didn't take a lot
Brett Cannon <bcannon@gmail.com>
parents:
39364
diff
changeset
|
145 |
|
59694
22f589a8e3ab
Add support for weak references to code objects. This will be used by an optimization in the incoming Python 3 JIT.
Collin Winter <collinw@gmail.com>
parents:
53930
diff
changeset
|
146 if __name__ == "__main__": |
|
46242
4c5065ddb9bd
Add ``if __name__ == '__main__'`` to some test files where it didn't take a lot
Brett Cannon <bcannon@gmail.com>
parents:
39364
diff
changeset
|
147 test_main() |
