~ubuntu-branches/ubuntu/maverick/python3.1/maverick

« back to all changes in this revision

Viewing changes to Lib/test/test_code.py

  • Committer: Bazaar Package Importer
  • Author(s): Matthias Klose
  • Date: 2009-03-23 00:01:27 UTC
  • Revision ID: james.westby@ubuntu.com-20090323000127-5fstfxju4ufrhthq
Tags: upstream-3.1~a1+20090322
ImportĀ upstreamĀ versionĀ 3.1~a1+20090322

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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.__code__)
 
10
name: f
 
11
argcount: 1
 
12
kwonlyargcount: 0
 
13
names: ()
 
14
varnames: ('x', 'g')
 
15
cellvars: ('x',)
 
16
freevars: ()
 
17
nlocals: 2
 
18
flags: 3
 
19
consts: ('None', '<code object g>')
 
20
 
 
21
>>> dump(f(4).__code__)
 
22
name: g
 
23
argcount: 1
 
24
kwonlyargcount: 0
 
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
 
38
...
 
39
 
 
40
>>> dump(h.__code__)
 
41
name: h
 
42
argcount: 2
 
43
kwonlyargcount: 0
 
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):
 
53
...     print(obj.attr1)
 
54
...     print(obj.attr2)
 
55
...     print(obj.attr3)
 
56
 
 
57
>>> dump(attrs.__code__)
 
58
name: attrs
 
59
argcount: 1
 
60
kwonlyargcount: 0
 
61
names: ('print', 'attr1', 'attr2', 'attr3')
 
62
varnames: ('obj',)
 
63
cellvars: ()
 
64
freevars: ()
 
65
nlocals: 1
 
66
flags: 67
 
67
consts: ('None',)
 
68
 
 
69
>>> def optimize_away():
 
70
...     'doc string'
 
71
...     'not a docstring'
 
72
...     53
 
73
...     0x53
 
74
 
 
75
>>> dump(optimize_away.__code__)
 
76
name: optimize_away
 
77
argcount: 0
 
78
kwonlyargcount: 0
 
79
names: ()
 
80
varnames: ()
 
81
cellvars: ()
 
82
freevars: ()
 
83
nlocals: 0
 
84
flags: 67
 
85
consts: ("'doc string'", 'None')
 
86
 
 
87
>>> def keywordonly_args(a,b,*,k1):
 
88
...     return a,b,k1
 
89
...
 
90
 
 
91
>>> dump(keywordonly_args.__code__)
 
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
 
 
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."""
 
116
    for attr in ["name", "argcount", "kwonlyargcount", "names", "varnames",
 
117
                 "cellvars", "freevars", "nlocals", "flags"]:
 
118
        print("%s: %s" % (attr, getattr(co, "co_" + attr)))
 
119
    print("consts:", tuple(consts(co.co_consts)))
 
120
 
 
121
def test_main(verbose=None):
 
122
    from test.support import run_doctest
 
123
    from test import test_code
 
124
    run_doctest(test_code, verbose)
 
125
 
 
126
 
 
127
if __name__ == '__main__':
 
128
    test_main()