blob: f374c8d628790227bcd56fc834db97672c2019b7 [file] [log] [blame]
Georg Brandlc8088df2008-02-02 10:18:15 +00001from test import test_support
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +00002import types
Georg Brandlc8088df2008-02-02 10:18:15 +00003import unittest
Barry Warsaw4a420a02001-01-15 20:30:15 +00004
Georg Brandlc8088df2008-02-02 10:18:15 +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.f = F
13 self.fi = F()
14 self.b = b
Barry Warsaw4a420a02001-01-15 20:30:15 +000015
Georg Brandl8ada1772008-02-05 18:32:47 +000016 def cannot_set_attr(self,obj, name, value, exceptions):
17 # This method is not called as a test (name doesn't start with 'test'),
18 # but may be used by other tests.
19 try: setattr(obj, name, value)
20 except exceptions: pass
21 else: self.fail("shouldn't be able to set %s to %r" % (name, value))
22 try: delattr(obj, name)
23 except exceptions: pass
24 else: self.fail("shouldn't be able to del %s" % name)
25
26
Georg Brandlc8088df2008-02-02 10:18:15 +000027class FunctionPropertiesTest(FuncAttrsTest):
28 # Include the external setUp method that is common to all tests
29 def test_module(self):
30 self.assertEqual(self.b.__module__, __name__)
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +000031
Georg Brandlc8088df2008-02-02 10:18:15 +000032 def test_dir_includes_correct_attrs(self):
33 self.b.known_attr = 7
34 self.assert_('known_attr' in dir(self.b),
35 "set attributes not in dir listing of method")
36 # Test on underlying function object of method
37 self.f.a.im_func.known_attr = 7
38 self.assert_('known_attr' in dir(self.f.a),
39 "set attribute on unbound method implementation in class not in "
40 "dir")
41 self.assert_('known_attr' in dir(self.fi.a),
42 "set attribute on unbound method implementations, should show up"
43 " in next dir")
Barry Warsaw4a420a02001-01-15 20:30:15 +000044
Georg Brandlc8088df2008-02-02 10:18:15 +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 Warsawc1e100f2001-02-26 18:07:26 +000051
Georg Brandlc8088df2008-02-02 10:18:15 +000052 def test_copying_func_code(self):
53 def test(): pass
54 self.assertEqual(test(), None)
55 test.func_code = self.b.func_code
56 self.assertEqual(test(), 3) # self.b always returns 3, arbitrarily
Barry Warsaw4a420a02001-01-15 20:30:15 +000057
Georg Brandlc8088df2008-02-02 10:18:15 +000058 def test_func_globals(self):
59 self.assertEqual(self.b.func_globals, globals())
Georg Brandl8ada1772008-02-05 18:32:47 +000060 self.cannot_set_attr(self.b, 'func_globals', 2, TypeError)
Barry Warsaw4a420a02001-01-15 20:30:15 +000061
Georg Brandlc8088df2008-02-02 10:18:15 +000062 def test_func_name(self):
63 self.assertEqual(self.b.__name__, 'b')
64 self.assertEqual(self.b.func_name, 'b')
65 self.b.__name__ = 'c'
66 self.assertEqual(self.b.__name__, 'c')
67 self.assertEqual(self.b.func_name, 'c')
68 self.b.func_name = 'd'
69 self.assertEqual(self.b.__name__, 'd')
70 self.assertEqual(self.b.func_name, 'd')
71 # __name__ and func_name must be a string
Georg Brandl8ada1772008-02-05 18:32:47 +000072 self.cannot_set_attr(self.b, '__name__', 7, TypeError)
73 self.cannot_set_attr(self.b, 'func_name', 7, TypeError)
Georg Brandlc8088df2008-02-02 10:18:15 +000074 # __name__ must be available when in restricted mode. Exec will raise
75 # AttributeError if __name__ is not available on f.
76 s = """def f(): pass\nf.__name__"""
77 exec s in {'__builtins__': {}}
78 # Test on methods, too
79 self.assertEqual(self.f.a.__name__, 'a')
80 self.assertEqual(self.fi.a.__name__, 'a')
Georg Brandl8ada1772008-02-05 18:32:47 +000081 self.cannot_set_attr(self.f.a, "__name__", 'a', AttributeError)
82 self.cannot_set_attr(self.fi.a, "__name__", 'a', AttributeError)
Barry Warsaw4a420a02001-01-15 20:30:15 +000083
Georg Brandlc8088df2008-02-02 10:18:15 +000084 def test_func_code(self):
85 num_one, num_two = 7, 8
86 def a(): pass
87 def b(): return 12
88 def c(): return num_one
89 def d(): return num_two
90 def e(): return num_one, num_two
91 for func in [a, b, c, d, e]:
92 self.assertEqual(type(func.func_code), types.CodeType)
93 self.assertEqual(c(), 7)
94 self.assertEqual(d(), 8)
95 d.func_code = c.func_code
96 self.assertEqual(c.func_code, d.func_code)
97 self.assertEqual(c(), 7)
98 # self.assertEqual(d(), 7)
99 try: b.func_code = c.func_code
100 except ValueError: pass
101 else: self.fail(
102 "func_code with different numbers of free vars should not be "
103 "possible")
104 try: e.func_code = d.func_code
105 except ValueError: pass
106 else: self.fail(
107 "func_code with different numbers of free vars should not be "
108 "possible")
Barry Warsawc1e100f2001-02-26 18:07:26 +0000109
Georg Brandlc8088df2008-02-02 10:18:15 +0000110 def test_blank_func_defaults(self):
111 self.assertEqual(self.b.func_defaults, None)
112 del self.b.func_defaults
113 self.assertEqual(self.b.func_defaults, None)
Barry Warsawc1e100f2001-02-26 18:07:26 +0000114
Georg Brandlc8088df2008-02-02 10:18:15 +0000115 def test_func_default_args(self):
116 def first_func(a, b):
117 return a+b
118 def second_func(a=1, b=2):
119 return a+b
120 self.assertEqual(first_func.func_defaults, None)
121 self.assertEqual(second_func.func_defaults, (1, 2))
122 first_func.func_defaults = (1, 2)
123 self.assertEqual(first_func.func_defaults, (1, 2))
124 self.assertEqual(first_func(), 3)
125 self.assertEqual(first_func(3), 5)
126 self.assertEqual(first_func(3, 5), 8)
127 del second_func.func_defaults
128 self.assertEqual(second_func.func_defaults, None)
129 try: second_func()
130 except TypeError: pass
131 else: self.fail(
132 "func_defaults does not update; deleting it does not remove "
133 "requirement")
Barry Warsawc1e100f2001-02-26 18:07:26 +0000134
Georg Brandlc8088df2008-02-02 10:18:15 +0000135class ImplicitReferencesTest(FuncAttrsTest):
136 def test_im_class(self):
137 self.assertEqual(self.f.a.im_class, self.f)
138 self.assertEqual(self.fi.a.im_class, self.f)
Georg Brandl8ada1772008-02-05 18:32:47 +0000139 self.cannot_set_attr(self.f.a, "im_class", self.f, TypeError)
140 self.cannot_set_attr(self.fi.a, "im_class", self.f, TypeError)
Barry Warsaw4a420a02001-01-15 20:30:15 +0000141
Georg Brandlc8088df2008-02-02 10:18:15 +0000142 def test_im_func(self):
143 self.f.b = self.b
144 self.assertEqual(self.f.b.im_func, self.b)
145 self.assertEqual(self.fi.b.im_func, self.b)
Georg Brandl8ada1772008-02-05 18:32:47 +0000146 self.cannot_set_attr(self.f.b, "im_func", self.b, TypeError)
147 self.cannot_set_attr(self.fi.b, "im_func", self.b, TypeError)
Barry Warsaw4a420a02001-01-15 20:30:15 +0000148
Georg Brandlc8088df2008-02-02 10:18:15 +0000149 def test_im_self(self):
150 self.assertEqual(self.f.a.im_self, None)
151 self.assertEqual(self.fi.a.im_self, self.fi)
Georg Brandl8ada1772008-02-05 18:32:47 +0000152 self.cannot_set_attr(self.f.a, "im_self", None, TypeError)
153 self.cannot_set_attr(self.fi.a, "im_self", self.fi, TypeError)
Barry Warsaw4a420a02001-01-15 20:30:15 +0000154
Georg Brandlc8088df2008-02-02 10:18:15 +0000155 def test_im_func_non_method(self):
156 # Behavior should be the same when a method is added via an attr
157 # assignment
158 self.f.id = types.MethodType(id, None, self.f)
159 self.assertEqual(self.fi.id(), id(self.fi))
160 self.assertNotEqual(self.fi.id(), id(self.f))
161 # Test usage
162 try: self.f.id.unknown_attr
163 except AttributeError: pass
164 else: self.fail("using unknown attributes should raise AttributeError")
165 # Test assignment and deletion
Georg Brandl8ada1772008-02-05 18:32:47 +0000166 self.cannot_set_attr(self.f.id, 'unknown_attr', 2, AttributeError)
167 self.cannot_set_attr(self.fi.id, 'unknown_attr', 2, AttributeError)
Barry Warsaw4a420a02001-01-15 20:30:15 +0000168
Georg Brandlc8088df2008-02-02 10:18:15 +0000169 def test_implicit_method_properties(self):
170 self.f.a.im_func.known_attr = 7
171 self.assertEqual(self.f.a.known_attr, 7)
172 self.assertEqual(self.fi.a.known_attr, 7)
Barry Warsawc1e100f2001-02-26 18:07:26 +0000173
Georg Brandlc8088df2008-02-02 10:18:15 +0000174class ArbitraryFunctionAttrTest(FuncAttrsTest):
175 def test_set_attr(self):
176 self.b.known_attr = 7
177 self.assertEqual(self.b.known_attr, 7)
178 for func in [self.f.a, self.fi.a]:
179 try: func.known_attr = 7
180 except AttributeError: pass
181 else: self.fail("setting attributes on methods should raise error")
Barry Warsaw4a420a02001-01-15 20:30:15 +0000182
Georg Brandlc8088df2008-02-02 10:18:15 +0000183 def test_delete_unknown_attr(self):
184 try: del self.b.unknown_attr
185 except AttributeError: pass
186 else: self.fail("deleting unknown attribute should raise TypeError")
Barry Warsaw4a420a02001-01-15 20:30:15 +0000187
Georg Brandlc8088df2008-02-02 10:18:15 +0000188 def test_setting_attrs_duplicates(self):
189 try: self.f.a.klass = self.f
190 except AttributeError: pass
191 else: self.fail("setting arbitrary attribute in unbound function "
192 " should raise AttributeError")
193 self.f.a.im_func.klass = self.f
194 for method in [self.f.a, self.fi.a, self.fi.a.im_func]:
195 self.assertEqual(method.klass, self.f)
Barry Warsaw4a420a02001-01-15 20:30:15 +0000196
Georg Brandlc8088df2008-02-02 10:18:15 +0000197 def test_unset_attr(self):
198 for func in [self.b, self.f.a, self.fi.a]:
Mark Dickinson3e4caeb2009-02-21 20:27:01 +0000199 try: func.non_existent_attr
Georg Brandlc8088df2008-02-02 10:18:15 +0000200 except AttributeError: pass
201 else: self.fail("using unknown attributes should raise "
202 "AttributeError")
Barry Warsaw4a420a02001-01-15 20:30:15 +0000203
Georg Brandlc8088df2008-02-02 10:18:15 +0000204class FunctionDictsTest(FuncAttrsTest):
205 def test_setting_dict_to_invalid(self):
Georg Brandl8ada1772008-02-05 18:32:47 +0000206 self.cannot_set_attr(self.b, '__dict__', None, TypeError)
207 self.cannot_set_attr(self.b, 'func_dict', None, TypeError)
Georg Brandlc8088df2008-02-02 10:18:15 +0000208 from UserDict import UserDict
209 d = UserDict({'known_attr': 7})
Georg Brandl8ada1772008-02-05 18:32:47 +0000210 self.cannot_set_attr(self.f.a.im_func, '__dict__', d, TypeError)
211 self.cannot_set_attr(self.fi.a.im_func, '__dict__', d, TypeError)
Barry Warsaw4a420a02001-01-15 20:30:15 +0000212
Georg Brandlc8088df2008-02-02 10:18:15 +0000213 def test_setting_dict_to_valid(self):
214 d = {'known_attr': 7}
215 self.b.__dict__ = d
216 # Setting dict is only possible on the underlying function objects
217 self.f.a.im_func.__dict__ = d
218 # Test assignment
219 self.assertEqual(d, self.b.__dict__)
220 self.assertEqual(d, self.b.func_dict)
221 # ... and on all the different ways of referencing the method's func
222 self.assertEqual(d, self.f.a.im_func.__dict__)
223 self.assertEqual(d, self.f.a.__dict__)
224 self.assertEqual(d, self.fi.a.im_func.__dict__)
225 self.assertEqual(d, self.fi.a.__dict__)
226 # Test value
227 self.assertEqual(self.b.known_attr, 7)
228 self.assertEqual(self.b.__dict__['known_attr'], 7)
229 self.assertEqual(self.b.func_dict['known_attr'], 7)
230 # ... and again, on all the different method's names
231 self.assertEqual(self.f.a.im_func.known_attr, 7)
232 self.assertEqual(self.f.a.known_attr, 7)
233 self.assertEqual(self.fi.a.im_func.known_attr, 7)
234 self.assertEqual(self.fi.a.known_attr, 7)
Barry Warsawc1e100f2001-02-26 18:07:26 +0000235
Georg Brandlc8088df2008-02-02 10:18:15 +0000236 def test_delete_func_dict(self):
237 try: del self.b.__dict__
238 except TypeError: pass
239 else: self.fail("deleting function dictionary should raise TypeError")
240 try: del self.b.func_dict
241 except TypeError: pass
242 else: self.fail("deleting function dictionary should raise TypeError")
Barry Warsawc1e100f2001-02-26 18:07:26 +0000243
Georg Brandlc8088df2008-02-02 10:18:15 +0000244 def test_unassigned_dict(self):
245 self.assertEqual(self.b.__dict__, {})
Barry Warsaw4a420a02001-01-15 20:30:15 +0000246
Georg Brandlc8088df2008-02-02 10:18:15 +0000247 def test_func_as_dict_key(self):
248 value = "Some string"
249 d = {}
250 d[self.b] = value
251 self.assertEqual(d[self.b], value)
Barry Warsaw4a420a02001-01-15 20:30:15 +0000252
Georg Brandlc8088df2008-02-02 10:18:15 +0000253class FunctionDocstringTest(FuncAttrsTest):
254 def test_set_docstring_attr(self):
255 self.assertEqual(self.b.__doc__, None)
256 self.assertEqual(self.b.func_doc, None)
257 docstr = "A test method that does nothing"
258 self.b.__doc__ = self.f.a.im_func.__doc__ = docstr
259 self.assertEqual(self.b.__doc__, docstr)
260 self.assertEqual(self.b.func_doc, docstr)
261 self.assertEqual(self.f.a.__doc__, docstr)
262 self.assertEqual(self.fi.a.__doc__, docstr)
Georg Brandl8ada1772008-02-05 18:32:47 +0000263 self.cannot_set_attr(self.f.a, "__doc__", docstr, AttributeError)
264 self.cannot_set_attr(self.fi.a, "__doc__", docstr, AttributeError)
Guido van Rossumbd131492001-09-18 03:28:54 +0000265
Georg Brandlc8088df2008-02-02 10:18:15 +0000266 def test_delete_docstring(self):
267 self.b.__doc__ = "The docstring"
268 del self.b.__doc__
269 self.assertEqual(self.b.__doc__, None)
270 self.assertEqual(self.b.func_doc, None)
271 self.b.func_doc = "The docstring"
272 del self.b.func_doc
273 self.assertEqual(self.b.__doc__, None)
274 self.assertEqual(self.b.func_doc, None)
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000275
Georg Brandlc8088df2008-02-02 10:18:15 +0000276def test_main():
277 test_support.run_unittest(FunctionPropertiesTest, ImplicitReferencesTest,
278 ArbitraryFunctionAttrTest, FunctionDictsTest,
279 FunctionDocstringTest)
Guido van Rossumd9d1d4a2001-09-17 23:46:56 +0000280
Georg Brandlc8088df2008-02-02 10:18:15 +0000281if __name__ == "__main__":
282 test_main()