Mercurial > cpython-lru_cache-weakref
annotate Lib/test/test_code.py @ 39042:c02fa187e0f4 legacy-trunk
Part of SF patch #1484695. This removes dead code. The chksum was
already verified in .frombuf() on the lines above. If there was
a problem an exception is raised, so there was no way this condition
could have been true.
| author | Neal Norwitz <nnorwitz@gmail.com> |
|---|---|
| date | Mon, 10 Jul 2006 00:23:17 +0000 |
| parents | 686306a67e9b |
| children | 8ef3f8cf90e1 |
| 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 | |
| 64 """ | |
| 65 | |
| 66 def consts(t): | |
| 67 """Yield a doctest-safe sequence of object reprs.""" | |
| 68 for elt in t: | |
| 69 r = repr(elt) | |
| 70 if r.startswith("<code object"): | |
| 71 yield "<code object %s>" % elt.co_name | |
| 72 else: | |
| 73 yield r | |
| 74 | |
| 75 def dump(co): | |
| 76 """Print out a text representation of a code object.""" | |
| 77 for attr in ["name", "argcount", "names", "varnames", "cellvars", | |
| 78 "freevars", "nlocals", "flags"]: | |
| 79 print "%s: %s" % (attr, getattr(co, "co_" + attr)) | |
| 80 print "consts:", tuple(consts(co.co_consts)) | |
| 81 | |
| 82 def test_main(verbose=None): | |
| 83 from test.test_support import run_doctest | |
| 84 from test import test_code | |
| 85 run_doctest(test_code, verbose) |
