Back to index

python3.2  3.2.2
Public Member Functions | Public Attributes
test.test_funcattrs.FunctionPropertiesTest Class Reference
Inheritance diagram for test.test_funcattrs.FunctionPropertiesTest:
Inheritance graph
[legend]
Collaboration diagram for test.test_funcattrs.FunctionPropertiesTest:
Collaboration graph
[legend]

List of all members.

Public Member Functions

def test_module
def test_dir_includes_correct_attrs
def test_duplicate_function_equality
def test_copying___code__
def test___globals__
def test___closure__
def test_empty_cell
def test___name__
def test___code__
def test_blank_func_defaults
def test_func_default_args
def setUp
def cannot_set_attr

Public Attributes

 fi
 F
 b

Detailed Description

Definition at line 31 of file test_funcattrs.py.


Member Function Documentation

def test.test_funcattrs.FuncAttrsTest.cannot_set_attr (   self,
  obj,
  name,
  value,
  exceptions 
) [inherited]

Definition at line 16 of file test_funcattrs.py.

00016 
00017     def cannot_set_attr(self, obj, name, value, exceptions):
00018         try:
00019             setattr(obj, name, value)
00020         except exceptions:
00021             pass
00022         else:
00023             self.fail("shouldn't be able to set %s to %r" % (name, value))
00024         try:
00025             delattr(obj, name)
00026         except exceptions:
00027             pass
00028         else:
00029             self.fail("shouldn't be able to del %s" % name)
00030 

Here is the call graph for this function:

Here is the caller graph for this function:

def test.test_funcattrs.FuncAttrsTest.setUp (   self) [inherited]

Definition at line 6 of file test_funcattrs.py.

00006 
00007     def setUp(self):
00008         class F:
00009             def a(self):
00010                 pass
00011         def b():
00012             return 3
00013         self.fi = F()
00014         self.F = F
00015         self.b = b

Here is the caller graph for this function:

Definition at line 63 of file test_funcattrs.py.

00063 
00064     def test___closure__(self):
00065         a = 12
00066         def f(): print(a)
00067         c = f.__closure__
00068         self.assertIsInstance(c, tuple)
00069         self.assertEqual(len(c), 1)
00070         # don't have a type object handy
00071         self.assertEqual(c[0].__class__.__name__, "cell")
00072         self.cannot_set_attr(f, "__closure__", c, AttributeError)

Here is the call graph for this function:

Definition at line 99 of file test_funcattrs.py.

00099 
00100     def test___code__(self):
00101         num_one, num_two = 7, 8
00102         def a(): pass
00103         def b(): return 12
00104         def c(): return num_one
00105         def d(): return num_two
00106         def e(): return num_one, num_two
00107         for func in [a, b, c, d, e]:
00108             self.assertEqual(type(func.__code__), types.CodeType)
00109         self.assertEqual(c(), 7)
00110         self.assertEqual(d(), 8)
00111         d.__code__ = c.__code__
00112         self.assertEqual(c.__code__, d.__code__)
00113         self.assertEqual(c(), 7)
00114         # self.assertEqual(d(), 7)
00115         try:
00116             b.__code__ = c.__code__
00117         except ValueError:
00118             pass
00119         else:
00120             self.fail("__code__ with different numbers of free vars should "
00121                       "not be possible")
00122         try:
00123             e.__code__ = d.__code__
00124         except ValueError:
00125             pass
00126         else:
00127             self.fail("__code__ with different numbers of free vars should "
00128                       "not be possible")

Here is the call graph for this function:

Definition at line 58 of file test_funcattrs.py.

00058 
00059     def test___globals__(self):
00060         self.assertIs(self.b.__globals__, globals())
00061         self.cannot_set_attr(self.b, '__globals__', 2,
00062                              (AttributeError, TypeError))

Here is the call graph for this function:

Definition at line 83 of file test_funcattrs.py.

00083 
00084     def test___name__(self):
00085         self.assertEqual(self.b.__name__, 'b')
00086         self.b.__name__ = 'c'
00087         self.assertEqual(self.b.__name__, 'c')
00088         self.b.__name__ = 'd'
00089         self.assertEqual(self.b.__name__, 'd')
00090         # __name__ and __name__ must be a string
00091         self.cannot_set_attr(self.b, '__name__', 7, TypeError)
00092         # __name__ must be available when in restricted mode. Exec will raise
00093         # AttributeError if __name__ is not available on f.
00094         s = """def f(): pass\nf.__name__"""
00095         exec(s, {'__builtins__': {}})
00096         # Test on methods, too
00097         self.assertEqual(self.fi.a.__name__, 'a')
00098         self.cannot_set_attr(self.fi.a, "__name__", 'a', AttributeError)

Here is the call graph for this function:

Definition at line 129 of file test_funcattrs.py.

00129 
00130     def test_blank_func_defaults(self):
00131         self.assertEqual(self.b.__defaults__, None)
00132         del self.b.__defaults__
00133         self.assertEqual(self.b.__defaults__, None)

Here is the call graph for this function:

Definition at line 52 of file test_funcattrs.py.

00052 
00053     def test_copying___code__(self):
00054         def test(): pass
00055         self.assertEqual(test(), None)
00056         test.__code__ = self.b.__code__
00057         self.assertEqual(test(), 3) # self.b always returns 3, arbitrarily

Here is the call graph for this function:

Definition at line 36 of file test_funcattrs.py.

00036 
00037     def test_dir_includes_correct_attrs(self):
00038         self.b.known_attr = 7
00039         self.assertIn('known_attr', dir(self.b),
00040             "set attributes not in dir listing of method")
00041         # Test on underlying function object of method
00042         self.F.a.known_attr = 7
00043         self.assertIn('known_attr', dir(self.fi.a), "set attribute on function "
00044                      "implementations, should show up in next dir")

Here is the call graph for this function:

Definition at line 45 of file test_funcattrs.py.

00045 
00046     def test_duplicate_function_equality(self):
00047         # Body of `duplicate' is the exact same as self.b
00048         def duplicate():
00049             'my docstring'
00050             return 3
00051         self.assertNotEqual(self.b, duplicate)

Here is the call graph for this function:

Definition at line 73 of file test_funcattrs.py.

00073 
00074     def test_empty_cell(self):
00075         def f(): print(a)
00076         try:
00077             f.__closure__[0].cell_contents
00078         except ValueError:
00079             pass
00080         else:
00081             self.fail("shouldn't be able to read an empty cell")
00082         a = 12

Here is the call graph for this function:

Definition at line 134 of file test_funcattrs.py.

00134 
00135     def test_func_default_args(self):
00136         def first_func(a, b):
00137             return a+b
00138         def second_func(a=1, b=2):
00139             return a+b
00140         self.assertEqual(first_func.__defaults__, None)
00141         self.assertEqual(second_func.__defaults__, (1, 2))
00142         first_func.__defaults__ = (1, 2)
00143         self.assertEqual(first_func.__defaults__, (1, 2))
00144         self.assertEqual(first_func(), 3)
00145         self.assertEqual(first_func(3), 5)
00146         self.assertEqual(first_func(3, 5), 8)
00147         del second_func.__defaults__
00148         self.assertEqual(second_func.__defaults__, None)
00149         try:
00150             second_func()
00151         except TypeError:
00152             pass
00153         else:
00154             self.fail("__defaults__ does not update; deleting it does not "
00155                       "remove requirement")
00156 

Here is the call graph for this function:

Definition at line 33 of file test_funcattrs.py.

00033 
00034     def test_module(self):
00035         self.assertEqual(self.b.__module__, __name__)

Here is the call graph for this function:


Member Data Documentation

Definition at line 14 of file test_funcattrs.py.

Definition at line 13 of file test_funcattrs.py.

Definition at line 12 of file test_funcattrs.py.


The documentation for this class was generated from the following file: