Mercurial > cpython-lru_cache-weakref
annotate Lib/test/test_code.py @ 64166:d0e9febfa00a 3.1
Merged revisions 84172 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/branches/py3k
........
r84172 | martin.v.loewis | 2010-08-18 18:12:23 +0200 (Mi, 18 Aug 2010) | 2 lines
Restore GIL in nis_cat in case of error.
........
| author | Martin v. Löwis <martin@v.loewis.de> |
|---|---|
| date | Thu, 19 Aug 2010 09:03:03 +0000 |
| parents | 9c930a32cebb |
| children | 79718ee03986 |
| 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 | |
|
40910
eaaee785eb86
Change all the function attributes from func_* -> __*__. This gets rid
Neal Norwitz <nnorwitz@gmail.com>
parents:
40788
diff
changeset
|
9 >>> dump(f.__code__) |
| 35742 | 10 name: f |
| 11 argcount: 1 | |
|
40215
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39442
diff
changeset
|
12 kwonlyargcount: 0 |
| 35742 | 13 names: () |
| 14 varnames: ('x', 'g') | |
| 15 cellvars: ('x',) | |
| 16 freevars: () | |
| 17 nlocals: 2 | |
| 18 flags: 3 | |
| 19 consts: ('None', '<code object g>') | |
| 20 | |
|
40910
eaaee785eb86
Change all the function attributes from func_* -> __*__. This gets rid
Neal Norwitz <nnorwitz@gmail.com>
parents:
40788
diff
changeset
|
21 >>> dump(f(4).__code__) |
| 35742 | 22 name: g |
| 23 argcount: 1 | |
|
40215
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39442
diff
changeset
|
24 kwonlyargcount: 0 |
| 35742 | 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 | |
|
36121
686306a67e9b
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
35742
diff
changeset
|
38 ... |
|
40215
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39442
diff
changeset
|
39 |
|
40910
eaaee785eb86
Change all the function attributes from func_* -> __*__. This gets rid
Neal Norwitz <nnorwitz@gmail.com>
parents:
40788
diff
changeset
|
40 >>> dump(h.__code__) |
| 35742 | 41 name: h |
| 42 argcount: 2 | |
|
40215
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39442
diff
changeset
|
43 kwonlyargcount: 0 |
| 35742 | 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): | |
|
40785
22760d53d4da
Fix a bunch of doctests with the -d option of refactor.py.
Guido van Rossum <guido@python.org>
parents:
40775
diff
changeset
|
53 ... print(obj.attr1) |
|
22760d53d4da
Fix a bunch of doctests with the -d option of refactor.py.
Guido van Rossum <guido@python.org>
parents:
40775
diff
changeset
|
54 ... print(obj.attr2) |
|
22760d53d4da
Fix a bunch of doctests with the -d option of refactor.py.
Guido van Rossum <guido@python.org>
parents:
40775
diff
changeset
|
55 ... print(obj.attr3) |
| 35742 | 56 |
|
40910
eaaee785eb86
Change all the function attributes from func_* -> __*__. This gets rid
Neal Norwitz <nnorwitz@gmail.com>
parents:
40788
diff
changeset
|
57 >>> dump(attrs.__code__) |
| 35742 | 58 name: attrs |
| 59 argcount: 1 | |
|
40215
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39442
diff
changeset
|
60 kwonlyargcount: 0 |
|
40788
e9a6234b26af
* Remove PRINT_ITEM(_TO), PRINT_NEWLINE(_TO) opcodes.
Georg Brandl <georg@python.org>
parents:
40785
diff
changeset
|
61 names: ('print', 'attr1', 'attr2', 'attr3') |
| 35742 | 62 varnames: ('obj',) |
| 63 cellvars: () | |
| 64 freevars: () | |
| 65 nlocals: 1 | |
| 66 flags: 67 | |
| 67 consts: ('None',) | |
| 68 | |
|
39442
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
36121
diff
changeset
|
69 >>> def optimize_away(): |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
36121
diff
changeset
|
70 ... 'doc string' |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
36121
diff
changeset
|
71 ... 'not a docstring' |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
36121
diff
changeset
|
72 ... 53 |
|
40644
1c26505c1de6
Rip out 'long' and 'L'-suffixed integer literals.
Guido van Rossum <guido@python.org>
parents:
40215
diff
changeset
|
73 ... 0x53 |
|
39442
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
36121
diff
changeset
|
74 |
|
40910
eaaee785eb86
Change all the function attributes from func_* -> __*__. This gets rid
Neal Norwitz <nnorwitz@gmail.com>
parents:
40788
diff
changeset
|
75 >>> dump(optimize_away.__code__) |
|
39442
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
36121
diff
changeset
|
76 name: optimize_away |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
36121
diff
changeset
|
77 argcount: 0 |
|
40215
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39442
diff
changeset
|
78 kwonlyargcount: 0 |
|
39442
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
36121
diff
changeset
|
79 names: () |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
36121
diff
changeset
|
80 varnames: () |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
36121
diff
changeset
|
81 cellvars: () |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
36121
diff
changeset
|
82 freevars: () |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
36121
diff
changeset
|
83 nlocals: 0 |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
36121
diff
changeset
|
84 flags: 67 |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
36121
diff
changeset
|
85 consts: ("'doc string'", 'None') |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
36121
diff
changeset
|
86 |
|
40215
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39442
diff
changeset
|
87 >>> def keywordonly_args(a,b,*,k1): |
|
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39442
diff
changeset
|
88 ... return a,b,k1 |
|
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39442
diff
changeset
|
89 ... |
|
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39442
diff
changeset
|
90 |
|
40910
eaaee785eb86
Change all the function attributes from func_* -> __*__. This gets rid
Neal Norwitz <nnorwitz@gmail.com>
parents:
40788
diff
changeset
|
91 >>> dump(keywordonly_args.__code__) |
|
40215
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39442
diff
changeset
|
92 name: keywordonly_args |
|
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39442
diff
changeset
|
93 argcount: 2 |
|
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39442
diff
changeset
|
94 kwonlyargcount: 1 |
|
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39442
diff
changeset
|
95 names: () |
|
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39442
diff
changeset
|
96 varnames: ('a', 'b', 'k1') |
|
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39442
diff
changeset
|
97 cellvars: () |
|
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39442
diff
changeset
|
98 freevars: () |
|
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39442
diff
changeset
|
99 nlocals: 3 |
|
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39442
diff
changeset
|
100 flags: 67 |
|
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39442
diff
changeset
|
101 consts: ('None',) |
|
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39442
diff
changeset
|
102 |
| 35742 | 103 """ |
| 104 | |
| 105 def consts(t): | |
| 106 """Yield a doctest-safe sequence of object reprs.""" | |
| 107 for elt in t: | |
| 108 r = repr(elt) | |
| 109 if r.startswith("<code object"): | |
| 110 yield "<code object %s>" % elt.co_name | |
| 111 else: | |
| 112 yield r | |
| 113 | |
| 114 def dump(co): | |
| 115 """Print out a text representation of a code object.""" | |
|
40215
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39442
diff
changeset
|
116 for attr in ["name", "argcount", "kwonlyargcount", "names", "varnames", |
|
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39442
diff
changeset
|
117 "cellvars", "freevars", "nlocals", "flags"]: |
|
40775
4cdf21a0dbbc
Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents:
40644
diff
changeset
|
118 print("%s: %s" % (attr, getattr(co, "co_" + attr))) |
|
4cdf21a0dbbc
Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents:
40644
diff
changeset
|
119 print("consts:", tuple(consts(co.co_consts))) |
| 35742 | 120 |
| 121 def test_main(verbose=None): | |
|
47201
9c930a32cebb
#2621 rename test.test_support to test.support
Benjamin Peterson <benjamin@python.org>
parents:
46262
diff
changeset
|
122 from test.support import run_doctest |
| 35742 | 123 from test import test_code |
| 124 run_doctest(test_code, verbose) | |
|
46262
f547ebf02843
Merged revisions 62090-62091,62096,62100,62102,62110-62114 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
40910
diff
changeset
|
125 |
|
f547ebf02843
Merged revisions 62090-62091,62096,62100,62102,62110-62114 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
40910
diff
changeset
|
126 |
|
f547ebf02843
Merged revisions 62090-62091,62096,62100,62102,62110-62114 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
40910
diff
changeset
|
127 if __name__ == '__main__': |
|
f547ebf02843
Merged revisions 62090-62091,62096,62100,62102,62110-62114 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
40910
diff
changeset
|
128 test_main() |
