Back to index

python3.2  3.2.2
Classes | Functions
test.test_code Namespace Reference

Classes

class  CodeTest
class  CodeWeakRefTest

Functions

def consts
def dump
def test_main

Detailed Description

This module includes tests of the code object representation.

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

>>> dump(f.__code__)
name: f
argcount: 1
kwonlyargcount: 0
names: ()
varnames: ('x', 'g')
cellvars: ('x',)
freevars: ()
nlocals: 2
flags: 3
consts: ('None', '<code object g>')

>>> dump(f(4).__code__)
name: g
argcount: 1
kwonlyargcount: 0
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
...

>>> dump(h.__code__)
name: h
argcount: 2
kwonlyargcount: 0
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.__code__)
name: attrs
argcount: 1
kwonlyargcount: 0
names: ('print', 'attr1', 'attr2', 'attr3')
varnames: ('obj',)
cellvars: ()
freevars: ()
nlocals: 1
flags: 67
consts: ('None',)

>>> def optimize_away():
...     'doc string'
...     'not a docstring'
...     53
...     0x53

>>> dump(optimize_away.__code__)
name: optimize_away
argcount: 0
kwonlyargcount: 0
names: ()
varnames: ()
cellvars: ()
freevars: ()
nlocals: 0
flags: 67
consts: ("'doc string'", 'None')

>>> def keywordonly_args(a,b,*,k1):
...     return a,b,k1
...

>>> dump(keywordonly_args.__code__)
name: keywordonly_args
argcount: 2
kwonlyargcount: 1
names: ()
varnames: ('a', 'b', 'k1')
cellvars: ()
freevars: ()
nlocals: 3
flags: 67
consts: ('None',)

Function Documentation

def test.test_code.consts (   t)
Yield a doctest-safe sequence of object reprs.

Definition at line 110 of file test_code.py.

00110 
00111 def consts(t):
00112     """Yield a doctest-safe sequence of object reprs."""
00113     for elt in t:
00114         r = repr(elt)
00115         if r.startswith("<code object"):
00116             yield "<code object %s>" % elt.co_name
00117         else:
00118             yield r

Here is the caller graph for this function:

def test.test_code.dump (   co)
Print out a text representation of a code object.

Definition at line 119 of file test_code.py.

00119 
00120 def dump(co):
00121     """Print out a text representation of a code object."""
00122     for attr in ["name", "argcount", "kwonlyargcount", "names", "varnames",
00123                  "cellvars", "freevars", "nlocals", "flags"]:
00124         print("%s: %s" % (attr, getattr(co, "co_" + attr)))
00125     print("consts:", tuple(consts(co.co_consts)))
00126 

Here is the call graph for this function:

def test.test_code.test_main (   verbose = None)

Definition at line 161 of file test_code.py.

00161 
00162 def test_main(verbose=None):
00163     from test.support import run_doctest, run_unittest
00164     from test import test_code
00165     run_doctest(test_code, verbose)
00166     run_unittest(CodeTest, CodeWeakRefTest)
00167 

Here is the call graph for this function: