test_code.py 3.54 KB
Newer Older
Jeremy Hylton's avatar
Jeremy Hylton committed
1 2 3 4 5 6 7 8
"""This module includes tests of the code object representation.

>>> def f(x):
...     def g(y):
...         return x + y
...     return g
...

9
>>> dump(f.__code__)
Jeremy Hylton's avatar
Jeremy Hylton committed
10 11
name: f
argcount: 1
12
kwonlyargcount: 0
Jeremy Hylton's avatar
Jeremy Hylton committed
13 14 15 16 17 18
names: ()
varnames: ('x', 'g')
cellvars: ('x',)
freevars: ()
nlocals: 2
flags: 3
19
consts: ('None', '<code object g>', "'f.<locals>.g'")
Jeremy Hylton's avatar
Jeremy Hylton committed
20

21
>>> dump(f(4).__code__)
Jeremy Hylton's avatar
Jeremy Hylton committed
22 23
name: g
argcount: 1
24
kwonlyargcount: 0
Jeremy Hylton's avatar
Jeremy Hylton committed
25 26 27 28 29 30 31 32 33 34 35 36 37
names: ()
varnames: ('y',)
cellvars: ()
freevars: ('x',)
nlocals: 1
flags: 19
consts: ('None',)

>>> def h(x, y):
...     a = x + y
...     b = x - y
...     c = a * b
...     return c
Tim Peters's avatar
Tim Peters committed
38
...
39

40
>>> dump(h.__code__)
Jeremy Hylton's avatar
Jeremy Hylton committed
41 42
name: h
argcount: 2
43
kwonlyargcount: 0
Jeremy Hylton's avatar
Jeremy Hylton committed
44 45 46 47 48 49 50 51 52
names: ()
varnames: ('x', 'y', 'a', 'b', 'c')
cellvars: ()
freevars: ()
nlocals: 5
flags: 67
consts: ('None',)

>>> def attrs(obj):
53 54 55
...     print(obj.attr1)
...     print(obj.attr2)
...     print(obj.attr3)
Jeremy Hylton's avatar
Jeremy Hylton committed
56

57
>>> dump(attrs.__code__)
Jeremy Hylton's avatar
Jeremy Hylton committed
58 59
name: attrs
argcount: 1
60
kwonlyargcount: 0
61
names: ('print', 'attr1', 'attr2', 'attr3')
Jeremy Hylton's avatar
Jeremy Hylton committed
62 63 64 65 66 67 68
varnames: ('obj',)
cellvars: ()
freevars: ()
nlocals: 1
flags: 67
consts: ('None',)

69 70 71 72
>>> def optimize_away():
...     'doc string'
...     'not a docstring'
...     53
73
...     0x53
74

75
>>> dump(optimize_away.__code__)
76 77
name: optimize_away
argcount: 0
78
kwonlyargcount: 0
79 80 81 82 83 84 85 86
names: ()
varnames: ()
cellvars: ()
freevars: ()
nlocals: 0
flags: 67
consts: ("'doc string'", 'None')

87 88 89 90
>>> def keywordonly_args(a,b,*,k1):
...     return a,b,k1
...

91
>>> dump(keywordonly_args.__code__)
92 93 94 95 96 97 98 99 100 101 102
name: keywordonly_args
argcount: 2
kwonlyargcount: 1
names: ()
varnames: ('a', 'b', 'k1')
cellvars: ()
freevars: ()
nlocals: 3
flags: 67
consts: ('None',)

Jeremy Hylton's avatar
Jeremy Hylton committed
103 104
"""

105
import unittest
106
import weakref
107
from test.support import run_doctest, run_unittest, cpython_only
108

109

Jeremy Hylton's avatar
Jeremy Hylton committed
110 111 112 113 114 115 116 117 118 119 120
def consts(t):
    """Yield a doctest-safe sequence of object reprs."""
    for elt in t:
        r = repr(elt)
        if r.startswith("<code object"):
            yield "<code object %s>" % elt.co_name
        else:
            yield r

def dump(co):
    """Print out a text representation of a code object."""
121 122
    for attr in ["name", "argcount", "kwonlyargcount", "names", "varnames",
                 "cellvars", "freevars", "nlocals", "flags"]:
123 124
        print("%s: %s" % (attr, getattr(co, "co_" + attr)))
    print("consts:", tuple(consts(co.co_consts)))
Jeremy Hylton's avatar
Jeremy Hylton committed
125

126 127 128

class CodeTest(unittest.TestCase):

129
    @cpython_only
130
    def test_newempty(self):
131
        import _testcapi
132
        co = _testcapi.code_newempty("filename", "funcname", 15)
133 134 135
        self.assertEqual(co.co_filename, "filename")
        self.assertEqual(co.co_name, "funcname")
        self.assertEqual(co.co_firstlineno, 15)
136 137


138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
class CodeWeakRefTest(unittest.TestCase):

    def test_basic(self):
        # Create a code object in a clean environment so that we know we have
        # the only reference to it left.
        namespace = {}
        exec("def f(): pass", globals(), namespace)
        f = namespace["f"]
        del namespace

        self.called = False
        def callback(code):
            self.called = True

        # f is now the last reference to the function, and through it, the code
        # object.  While we hold it, check that we can create a weakref and
        # deref it.  Then delete it, and check that the callback gets called and
        # the reference dies.
        coderef = weakref.ref(f.__code__, callback)
        self.assertTrue(bool(coderef()))
        del f
        self.assertFalse(bool(coderef()))
        self.assertTrue(self.called)


Jeremy Hylton's avatar
Jeremy Hylton committed
163 164 165
def test_main(verbose=None):
    from test import test_code
    run_doctest(test_code, verbose)
166
    run_unittest(CodeTest, CodeWeakRefTest)
167 168


169
if __name__ == "__main__":
170
    test_main()