2005-10-20 16:59:25 -03:00
|
|
|
"""This module includes tests of the code object representation.
|
|
|
|
|
|
|
|
>>> def f(x):
|
|
|
|
... def g(y):
|
|
|
|
... return x + y
|
|
|
|
... return g
|
|
|
|
...
|
|
|
|
|
|
|
|
>>> dump(f.func_code)
|
|
|
|
name: f
|
|
|
|
argcount: 1
|
|
|
|
names: ()
|
|
|
|
varnames: ('x', 'g')
|
|
|
|
cellvars: ('x',)
|
|
|
|
freevars: ()
|
|
|
|
nlocals: 2
|
|
|
|
flags: 3
|
|
|
|
consts: ('None', '<code object g>')
|
|
|
|
|
|
|
|
>>> dump(f(4).func_code)
|
|
|
|
name: g
|
|
|
|
argcount: 1
|
|
|
|
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
|
2005-12-25 19:18:31 -04:00
|
|
|
...
|
2005-10-20 16:59:25 -03:00
|
|
|
>>> dump(h.func_code)
|
|
|
|
name: h
|
|
|
|
argcount: 2
|
|
|
|
names: ()
|
|
|
|
varnames: ('x', 'y', 'a', 'b', 'c')
|
|
|
|
cellvars: ()
|
|
|
|
freevars: ()
|
|
|
|
nlocals: 5
|
|
|
|
flags: 67
|
|
|
|
consts: ('None',)
|
|
|
|
|
|
|
|
>>> def attrs(obj):
|
|
|
|
... print obj.attr1
|
|
|
|
... print obj.attr2
|
|
|
|
... print obj.attr3
|
|
|
|
|
|
|
|
>>> dump(attrs.func_code)
|
|
|
|
name: attrs
|
|
|
|
argcount: 1
|
|
|
|
names: ('attr1', 'attr2', 'attr3')
|
|
|
|
varnames: ('obj',)
|
|
|
|
cellvars: ()
|
|
|
|
freevars: ()
|
|
|
|
nlocals: 1
|
|
|
|
flags: 67
|
|
|
|
consts: ('None',)
|
|
|
|
|
2006-08-04 02:09:28 -03:00
|
|
|
>>> def optimize_away():
|
|
|
|
... 'doc string'
|
|
|
|
... 'not a docstring'
|
|
|
|
... 53
|
|
|
|
... 53L
|
|
|
|
|
|
|
|
>>> dump(optimize_away.func_code)
|
|
|
|
name: optimize_away
|
|
|
|
argcount: 0
|
|
|
|
names: ()
|
|
|
|
varnames: ()
|
|
|
|
cellvars: ()
|
|
|
|
freevars: ()
|
|
|
|
nlocals: 0
|
|
|
|
flags: 67
|
|
|
|
consts: ("'doc string'", 'None')
|
|
|
|
|
2005-10-20 16:59:25 -03:00
|
|
|
"""
|
|
|
|
|
2009-05-08 18:51:06 -03:00
|
|
|
import unittest
|
2010-03-18 18:54:01 -03:00
|
|
|
import weakref
|
2014-02-07 04:06:05 -04:00
|
|
|
from test.test_support import run_doctest, run_unittest, cpython_only
|
2009-05-08 18:51:06 -03:00
|
|
|
|
2010-03-18 18:54:01 -03:00
|
|
|
|
2005-10-20 16:59:25 -03:00
|
|
|
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."""
|
|
|
|
for attr in ["name", "argcount", "names", "varnames", "cellvars",
|
|
|
|
"freevars", "nlocals", "flags"]:
|
|
|
|
print "%s: %s" % (attr, getattr(co, "co_" + attr))
|
|
|
|
print "consts:", tuple(consts(co.co_consts))
|
|
|
|
|
2009-05-08 18:51:06 -03:00
|
|
|
|
|
|
|
class CodeTest(unittest.TestCase):
|
|
|
|
|
2014-02-07 04:06:05 -04:00
|
|
|
@cpython_only
|
2009-05-08 18:51:06 -03:00
|
|
|
def test_newempty(self):
|
2014-02-07 04:06:05 -04:00
|
|
|
import _testcapi
|
2009-05-08 18:51:06 -03:00
|
|
|
co = _testcapi.code_newempty("filename", "funcname", 15)
|
2010-11-21 09:34:58 -04:00
|
|
|
self.assertEqual(co.co_filename, "filename")
|
|
|
|
self.assertEqual(co.co_name, "funcname")
|
|
|
|
self.assertEqual(co.co_firstlineno, 15)
|
2009-05-08 18:51:06 -03:00
|
|
|
|
|
|
|
|
2010-03-18 18:54:01 -03:00
|
|
|
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" in 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)
|
|
|
|
|
|
|
|
|
2005-10-20 16:59:25 -03:00
|
|
|
def test_main(verbose=None):
|
|
|
|
from test import test_code
|
|
|
|
run_doctest(test_code, verbose)
|
2010-03-18 18:54:01 -03:00
|
|
|
run_unittest(CodeTest, CodeWeakRefTest)
|
2008-04-01 09:46:02 -03:00
|
|
|
|
|
|
|
|
2010-03-18 18:54:01 -03:00
|
|
|
if __name__ == "__main__":
|
2008-04-01 09:46:02 -03:00
|
|
|
test_main()
|