+++ /dev/null
-"""This module includes tests of the code object representation.\r
-\r
->>> def f(x):\r
-... def g(y):\r
-... return x + y\r
-... return g\r
-...\r
-\r
->>> dump(f.func_code)\r
-name: f\r
-argcount: 1\r
-names: ()\r
-varnames: ('x', 'g')\r
-cellvars: ('x',)\r
-freevars: ()\r
-nlocals: 2\r
-flags: 3\r
-consts: ('None', '<code object g>')\r
-\r
->>> dump(f(4).func_code)\r
-name: g\r
-argcount: 1\r
-names: ()\r
-varnames: ('y',)\r
-cellvars: ()\r
-freevars: ('x',)\r
-nlocals: 1\r
-flags: 19\r
-consts: ('None',)\r
-\r
->>> def h(x, y):\r
-... a = x + y\r
-... b = x - y\r
-... c = a * b\r
-... return c\r
-...\r
->>> dump(h.func_code)\r
-name: h\r
-argcount: 2\r
-names: ()\r
-varnames: ('x', 'y', 'a', 'b', 'c')\r
-cellvars: ()\r
-freevars: ()\r
-nlocals: 5\r
-flags: 67\r
-consts: ('None',)\r
-\r
->>> def attrs(obj):\r
-... print obj.attr1\r
-... print obj.attr2\r
-... print obj.attr3\r
-\r
->>> dump(attrs.func_code)\r
-name: attrs\r
-argcount: 1\r
-names: ('attr1', 'attr2', 'attr3')\r
-varnames: ('obj',)\r
-cellvars: ()\r
-freevars: ()\r
-nlocals: 1\r
-flags: 67\r
-consts: ('None',)\r
-\r
->>> def optimize_away():\r
-... 'doc string'\r
-... 'not a docstring'\r
-... 53\r
-... 53L\r
-\r
->>> dump(optimize_away.func_code)\r
-name: optimize_away\r
-argcount: 0\r
-names: ()\r
-varnames: ()\r
-cellvars: ()\r
-freevars: ()\r
-nlocals: 0\r
-flags: 67\r
-consts: ("'doc string'", 'None')\r
-\r
-"""\r
-\r
-import unittest\r
-import weakref\r
-import _testcapi\r
-\r
-\r
-def consts(t):\r
- """Yield a doctest-safe sequence of object reprs."""\r
- for elt in t:\r
- r = repr(elt)\r
- if r.startswith("<code object"):\r
- yield "<code object %s>" % elt.co_name\r
- else:\r
- yield r\r
-\r
-def dump(co):\r
- """Print out a text representation of a code object."""\r
- for attr in ["name", "argcount", "names", "varnames", "cellvars",\r
- "freevars", "nlocals", "flags"]:\r
- print "%s: %s" % (attr, getattr(co, "co_" + attr))\r
- print "consts:", tuple(consts(co.co_consts))\r
-\r
-\r
-class CodeTest(unittest.TestCase):\r
-\r
- def test_newempty(self):\r
- co = _testcapi.code_newempty("filename", "funcname", 15)\r
- self.assertEqual(co.co_filename, "filename")\r
- self.assertEqual(co.co_name, "funcname")\r
- self.assertEqual(co.co_firstlineno, 15)\r
-\r
-\r
-class CodeWeakRefTest(unittest.TestCase):\r
-\r
- def test_basic(self):\r
- # Create a code object in a clean environment so that we know we have\r
- # the only reference to it left.\r
- namespace = {}\r
- exec "def f(): pass" in globals(), namespace\r
- f = namespace["f"]\r
- del namespace\r
-\r
- self.called = False\r
- def callback(code):\r
- self.called = True\r
-\r
- # f is now the last reference to the function, and through it, the code\r
- # object. While we hold it, check that we can create a weakref and\r
- # deref it. Then delete it, and check that the callback gets called and\r
- # the reference dies.\r
- coderef = weakref.ref(f.__code__, callback)\r
- self.assertTrue(bool(coderef()))\r
- del f\r
- self.assertFalse(bool(coderef()))\r
- self.assertTrue(self.called)\r
-\r
-\r
-def test_main(verbose=None):\r
- from test.test_support import run_doctest, run_unittest\r
- from test import test_code\r
- run_doctest(test_code, verbose)\r
- run_unittest(CodeTest, CodeWeakRefTest)\r
-\r
-\r
-if __name__ == "__main__":\r
- test_main()\r