blob: 5e9f7d3affe6a99dbfe1838e54bb6ff07f4c5db9 [file] [log] [blame]
Benjamin Petersonee8712c2008-05-20 21:35:26 +00001from test import support
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +00002import types
Georg Brandl60d14562008-02-05 18:31:41 +00003import unittest
Barry Warsaw4a420a02001-01-15 20:30:15 +00004
Georg Brandl60d14562008-02-05 18:31:41 +00005class FuncAttrsTest(unittest.TestCase):
6 def setUp(self):
7 class F:
8 def a(self):
9 pass
10 def b():
11 return 3
12 self.fi = F()
13 self.F = F
14 self.b = b
Barry Warsaw4a420a02001-01-15 20:30:15 +000015
Georg Brandl60d14562008-02-05 18:31:41 +000016 def cannot_set_attr(self, obj, name, value, exceptions):
17 try:
18 setattr(obj, name, value)
19 except exceptions:
20 pass
21 else:
22 self.fail("shouldn't be able to set %s to %r" % (name, value))
23 try:
24 delattr(obj, name)
25 except exceptions:
26 pass
27 else:
28 self.fail("shouldn't be able to del %s" % name)
Barry Warsaw4a420a02001-01-15 20:30:15 +000029
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +000030
Georg Brandl60d14562008-02-05 18:31:41 +000031class FunctionPropertiesTest(FuncAttrsTest):
32 # Include the external setUp method that is common to all tests
33 def test_module(self):
34 self.assertEqual(self.b.__module__, __name__)
Barry Warsaw4a420a02001-01-15 20:30:15 +000035
Georg Brandl60d14562008-02-05 18:31:41 +000036 def test_dir_includes_correct_attrs(self):
37 self.b.known_attr = 7
38 self.assert_('known_attr' in dir(self.b),
39 "set attributes not in dir listing of method")
40 # Test on underlying function object of method
41 self.F.a.known_attr = 7
42 self.assert_('known_attr' in dir(self.fi.a), "set attribute on function "
43 "implementations, should show up in next dir")
Barry Warsawc1e100f2001-02-26 18:07:26 +000044
Georg Brandl60d14562008-02-05 18:31:41 +000045 def test_duplicate_function_equality(self):
46 # Body of `duplicate' is the exact same as self.b
47 def duplicate():
48 'my docstring'
49 return 3
50 self.assertNotEqual(self.b, duplicate)
Barry Warsaw4a420a02001-01-15 20:30:15 +000051
Georg Brandl60d14562008-02-05 18:31:41 +000052 def test_copying___code__(self):
53 def test(): pass
54 self.assertEqual(test(), None)
55 test.__code__ = self.b.__code__
56 self.assertEqual(test(), 3) # self.b always returns 3, arbitrarily
Barry Warsaw4a420a02001-01-15 20:30:15 +000057
Georg Brandl60d14562008-02-05 18:31:41 +000058 def test___globals__(self):
59 self.assertEqual(self.b.__globals__, globals())
60 self.cannot_set_attr(self.b, '__globals__', 2, (AttributeError, TypeError))
Barry Warsaw4a420a02001-01-15 20:30:15 +000061
Georg Brandl60d14562008-02-05 18:31:41 +000062 def test___name__(self):
63 self.assertEqual(self.b.__name__, 'b')
64 self.b.__name__ = 'c'
65 self.assertEqual(self.b.__name__, 'c')
66 self.b.__name__ = 'd'
67 self.assertEqual(self.b.__name__, 'd')
68 # __name__ and __name__ must be a string
69 self.cannot_set_attr(self.b, '__name__', 7, TypeError)
70 # __name__ must be available when in restricted mode. Exec will raise
71 # AttributeError if __name__ is not available on f.
72 s = """def f(): pass\nf.__name__"""
73 exec(s, {'__builtins__': {}})
74 # Test on methods, too
75 self.assertEqual(self.fi.a.__name__, 'a')
76 self.cannot_set_attr(self.fi.a, "__name__", 'a', AttributeError)
Barry Warsawc1e100f2001-02-26 18:07:26 +000077
Georg Brandl60d14562008-02-05 18:31:41 +000078 def test___code__(self):
79 num_one, num_two = 7, 8
80 def a(): pass
81 def b(): return 12
82 def c(): return num_one
83 def d(): return num_two
84 def e(): return num_one, num_two
85 for func in [a, b, c, d, e]:
86 self.assertEqual(type(func.__code__), types.CodeType)
87 self.assertEqual(c(), 7)
88 self.assertEqual(d(), 8)
89 d.__code__ = c.__code__
90 self.assertEqual(c.__code__, d.__code__)
91 self.assertEqual(c(), 7)
92 # self.assertEqual(d(), 7)
93 try: b.__code__ = c.__code__
94 except ValueError: pass
95 else: self.fail(
96 "__code__ with different numbers of free vars should not be "
97 "possible")
98 try: e.__code__ = d.__code__
99 except ValueError: pass
100 else: self.fail(
101 "__code__ with different numbers of free vars should not be "
102 "possible")
Barry Warsawc1e100f2001-02-26 18:07:26 +0000103
Georg Brandl60d14562008-02-05 18:31:41 +0000104 def test_blank_func_defaults(self):
105 self.assertEqual(self.b.__defaults__, None)
106 del self.b.__defaults__
107 self.assertEqual(self.b.__defaults__, None)
Barry Warsawc1e100f2001-02-26 18:07:26 +0000108
Georg Brandl60d14562008-02-05 18:31:41 +0000109 def test_func_default_args(self):
110 def first_func(a, b):
111 return a+b
112 def second_func(a=1, b=2):
113 return a+b
114 self.assertEqual(first_func.__defaults__, None)
115 self.assertEqual(second_func.__defaults__, (1, 2))
116 first_func.__defaults__ = (1, 2)
117 self.assertEqual(first_func.__defaults__, (1, 2))
118 self.assertEqual(first_func(), 3)
119 self.assertEqual(first_func(3), 5)
120 self.assertEqual(first_func(3, 5), 8)
121 del second_func.__defaults__
122 self.assertEqual(second_func.__defaults__, None)
123 try: second_func()
124 except TypeError: pass
125 else: self.fail(
126 "func_defaults does not update; deleting it does not remove "
127 "requirement")
Barry Warsaw4a420a02001-01-15 20:30:15 +0000128
Georg Brandl60d14562008-02-05 18:31:41 +0000129class ImplicitReferencesTest(FuncAttrsTest):
Barry Warsaw4a420a02001-01-15 20:30:15 +0000130
Georg Brandl60d14562008-02-05 18:31:41 +0000131 def test___class__(self):
132 self.assertEqual(self.fi.a.__self__.__class__, self.F)
133 self.cannot_set_attr(self.fi.a, "__class__", self.F, TypeError)
Barry Warsaw4a420a02001-01-15 20:30:15 +0000134
Georg Brandl60d14562008-02-05 18:31:41 +0000135 def test___func__(self):
136 self.assertEqual(self.fi.a.__func__, self.F.a)
137 self.cannot_set_attr(self.fi.a, "__func__", self.F.a, AttributeError)
Barry Warsawc1e100f2001-02-26 18:07:26 +0000138
Georg Brandl60d14562008-02-05 18:31:41 +0000139 def test___self__(self):
140 self.assertEqual(self.fi.a.__self__, self.fi)
141 self.cannot_set_attr(self.fi.a, "__self__", self.fi, AttributeError)
Barry Warsaw4a420a02001-01-15 20:30:15 +0000142
Georg Brandl60d14562008-02-05 18:31:41 +0000143 def test___func___non_method(self):
144 # Behavior should be the same when a method is added via an attr
145 # assignment
146 self.fi.id = types.MethodType(id, self.fi)
147 self.assertEqual(self.fi.id(), id(self.fi))
148 # Test usage
149 try: self.fi.id.unknown_attr
150 except AttributeError: pass
151 else: self.fail("using unknown attributes should raise AttributeError")
152 # Test assignment and deletion
153 self.cannot_set_attr(self.fi.id, 'unknown_attr', 2, AttributeError)
Barry Warsaw4a420a02001-01-15 20:30:15 +0000154
Georg Brandl60d14562008-02-05 18:31:41 +0000155class ArbitraryFunctionAttrTest(FuncAttrsTest):
156 def test_set_attr(self):
157 self.b.known_attr = 7
158 self.assertEqual(self.b.known_attr, 7)
159 try: self.fi.a.known_attr = 7
160 except AttributeError: pass
161 else: self.fail("setting attributes on methods should raise error")
Barry Warsaw4a420a02001-01-15 20:30:15 +0000162
Georg Brandl60d14562008-02-05 18:31:41 +0000163 def test_delete_unknown_attr(self):
164 try: del self.b.unknown_attr
165 except AttributeError: pass
166 else: self.fail("deleting unknown attribute should raise TypeError")
Barry Warsaw4a420a02001-01-15 20:30:15 +0000167
Georg Brandl60d14562008-02-05 18:31:41 +0000168 def test_unset_attr(self):
169 for func in [self.b, self.fi.a]:
170 try: func.non_existant_attr
171 except AttributeError: pass
172 else: self.fail("using unknown attributes should raise "
173 "AttributeError")
Barry Warsaw4a420a02001-01-15 20:30:15 +0000174
Georg Brandl60d14562008-02-05 18:31:41 +0000175class FunctionDictsTest(FuncAttrsTest):
176 def test_setting_dict_to_invalid(self):
177 self.cannot_set_attr(self.b, '__dict__', None, TypeError)
Raymond Hettingerf80680d2008-02-06 00:07:11 +0000178 from collections import UserDict
Georg Brandl60d14562008-02-05 18:31:41 +0000179 d = UserDict({'known_attr': 7})
180 self.cannot_set_attr(self.fi.a.__func__, '__dict__', d, TypeError)
Barry Warsawc1e100f2001-02-26 18:07:26 +0000181
Georg Brandl60d14562008-02-05 18:31:41 +0000182 def test_setting_dict_to_valid(self):
183 d = {'known_attr': 7}
184 self.b.__dict__ = d
185 # Test assignment
186 self.assertEqual(d, self.b.__dict__)
187 # ... and on all the different ways of referencing the method's func
188 self.F.a.__dict__ = d
189 self.assertEqual(d, self.fi.a.__func__.__dict__)
190 self.assertEqual(d, self.fi.a.__dict__)
191 # Test value
192 self.assertEqual(self.b.known_attr, 7)
193 self.assertEqual(self.b.__dict__['known_attr'], 7)
194 # ... and again, on all the different method's names
195 self.assertEqual(self.fi.a.__func__.known_attr, 7)
196 self.assertEqual(self.fi.a.known_attr, 7)
Barry Warsaw4a420a02001-01-15 20:30:15 +0000197
Georg Brandl60d14562008-02-05 18:31:41 +0000198 def test_delete___dict__(self):
199 try: del self.b.__dict__
200 except TypeError: pass
201 else: self.fail("deleting function dictionary should raise TypeError")
Barry Warsaw4a420a02001-01-15 20:30:15 +0000202
Georg Brandl60d14562008-02-05 18:31:41 +0000203 def test_unassigned_dict(self):
204 self.assertEqual(self.b.__dict__, {})
Barry Warsaw4a420a02001-01-15 20:30:15 +0000205
Georg Brandl60d14562008-02-05 18:31:41 +0000206 def test_func_as_dict_key(self):
207 value = "Some string"
208 d = {}
209 d[self.b] = value
210 self.assertEqual(d[self.b], value)
Barry Warsaw534c60f2001-01-15 21:00:02 +0000211
Georg Brandl60d14562008-02-05 18:31:41 +0000212class FunctionDocstringTest(FuncAttrsTest):
213 def test_set_docstring_attr(self):
214 self.assertEqual(self.b.__doc__, None)
215 docstr = "A test method that does nothing"
216 self.b.__doc__ = docstr
217 self.F.a.__doc__ = docstr
218 self.assertEqual(self.b.__doc__, docstr)
219 self.assertEqual(self.fi.a.__doc__, docstr)
220 self.cannot_set_attr(self.fi.a, "__doc__", docstr, AttributeError)
Guido van Rossumbd131492001-09-18 03:28:54 +0000221
Georg Brandl60d14562008-02-05 18:31:41 +0000222 def test_delete_docstring(self):
223 self.b.__doc__ = "The docstring"
224 del self.b.__doc__
225 self.assertEqual(self.b.__doc__, None)
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000226
Georg Brandl60d14562008-02-05 18:31:41 +0000227def test_main():
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000228 support.run_unittest(FunctionPropertiesTest, ImplicitReferencesTest,
Georg Brandl60d14562008-02-05 18:31:41 +0000229 ArbitraryFunctionAttrTest, FunctionDictsTest,
230 FunctionDocstringTest)
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000231
Georg Brandl60d14562008-02-05 18:31:41 +0000232if __name__ == "__main__":
233 test_main()