Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 1 | from test import support |
Guido van Rossum | d9d1d4a | 2001-09-17 23:46:56 +0000 | [diff] [blame] | 2 | import types |
Georg Brandl | 60d1456 | 2008-02-05 18:31:41 +0000 | [diff] [blame] | 3 | import unittest |
Barry Warsaw | 4a420a0 | 2001-01-15 20:30:15 +0000 | [diff] [blame] | 4 | |
Georg Brandl | 60d1456 | 2008-02-05 18:31:41 +0000 | [diff] [blame] | 5 | class 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 Warsaw | 4a420a0 | 2001-01-15 20:30:15 +0000 | [diff] [blame] | 15 | |
Georg Brandl | 60d1456 | 2008-02-05 18:31:41 +0000 | [diff] [blame] | 16 | 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 Warsaw | 4a420a0 | 2001-01-15 20:30:15 +0000 | [diff] [blame] | 29 | |
Jeremy Hylton | 4f0dcc9 | 2003-01-31 18:33:18 +0000 | [diff] [blame] | 30 | |
Georg Brandl | 60d1456 | 2008-02-05 18:31:41 +0000 | [diff] [blame] | 31 | class 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 Warsaw | 4a420a0 | 2001-01-15 20:30:15 +0000 | [diff] [blame] | 35 | |
Georg Brandl | 60d1456 | 2008-02-05 18:31:41 +0000 | [diff] [blame] | 36 | 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 Warsaw | c1e100f | 2001-02-26 18:07:26 +0000 | [diff] [blame] | 44 | |
Georg Brandl | 60d1456 | 2008-02-05 18:31:41 +0000 | [diff] [blame] | 45 | 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 Warsaw | 4a420a0 | 2001-01-15 20:30:15 +0000 | [diff] [blame] | 51 | |
Georg Brandl | 60d1456 | 2008-02-05 18:31:41 +0000 | [diff] [blame] | 52 | 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 Warsaw | 4a420a0 | 2001-01-15 20:30:15 +0000 | [diff] [blame] | 57 | |
Georg Brandl | 60d1456 | 2008-02-05 18:31:41 +0000 | [diff] [blame] | 58 | def test___globals__(self): |
| 59 | self.assertEqual(self.b.__globals__, globals()) |
| 60 | self.cannot_set_attr(self.b, '__globals__', 2, (AttributeError, TypeError)) |
Barry Warsaw | 4a420a0 | 2001-01-15 20:30:15 +0000 | [diff] [blame] | 61 | |
Georg Brandl | 60d1456 | 2008-02-05 18:31:41 +0000 | [diff] [blame] | 62 | 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 Warsaw | c1e100f | 2001-02-26 18:07:26 +0000 | [diff] [blame] | 77 | |
Georg Brandl | 60d1456 | 2008-02-05 18:31:41 +0000 | [diff] [blame] | 78 | 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 Warsaw | c1e100f | 2001-02-26 18:07:26 +0000 | [diff] [blame] | 103 | |
Georg Brandl | 60d1456 | 2008-02-05 18:31:41 +0000 | [diff] [blame] | 104 | 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 Warsaw | c1e100f | 2001-02-26 18:07:26 +0000 | [diff] [blame] | 108 | |
Georg Brandl | 60d1456 | 2008-02-05 18:31:41 +0000 | [diff] [blame] | 109 | 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 Warsaw | 4a420a0 | 2001-01-15 20:30:15 +0000 | [diff] [blame] | 128 | |
Georg Brandl | 60d1456 | 2008-02-05 18:31:41 +0000 | [diff] [blame] | 129 | class ImplicitReferencesTest(FuncAttrsTest): |
Barry Warsaw | 4a420a0 | 2001-01-15 20:30:15 +0000 | [diff] [blame] | 130 | |
Georg Brandl | 60d1456 | 2008-02-05 18:31:41 +0000 | [diff] [blame] | 131 | 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 Warsaw | 4a420a0 | 2001-01-15 20:30:15 +0000 | [diff] [blame] | 134 | |
Georg Brandl | 60d1456 | 2008-02-05 18:31:41 +0000 | [diff] [blame] | 135 | 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 Warsaw | c1e100f | 2001-02-26 18:07:26 +0000 | [diff] [blame] | 138 | |
Georg Brandl | 60d1456 | 2008-02-05 18:31:41 +0000 | [diff] [blame] | 139 | 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 Warsaw | 4a420a0 | 2001-01-15 20:30:15 +0000 | [diff] [blame] | 142 | |
Georg Brandl | 60d1456 | 2008-02-05 18:31:41 +0000 | [diff] [blame] | 143 | 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 Warsaw | 4a420a0 | 2001-01-15 20:30:15 +0000 | [diff] [blame] | 154 | |
Georg Brandl | 60d1456 | 2008-02-05 18:31:41 +0000 | [diff] [blame] | 155 | class 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 Warsaw | 4a420a0 | 2001-01-15 20:30:15 +0000 | [diff] [blame] | 162 | |
Georg Brandl | 60d1456 | 2008-02-05 18:31:41 +0000 | [diff] [blame] | 163 | 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 Warsaw | 4a420a0 | 2001-01-15 20:30:15 +0000 | [diff] [blame] | 167 | |
Georg Brandl | 60d1456 | 2008-02-05 18:31:41 +0000 | [diff] [blame] | 168 | 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 Warsaw | 4a420a0 | 2001-01-15 20:30:15 +0000 | [diff] [blame] | 174 | |
Georg Brandl | 60d1456 | 2008-02-05 18:31:41 +0000 | [diff] [blame] | 175 | class FunctionDictsTest(FuncAttrsTest): |
| 176 | def test_setting_dict_to_invalid(self): |
| 177 | self.cannot_set_attr(self.b, '__dict__', None, TypeError) |
Raymond Hettinger | f80680d | 2008-02-06 00:07:11 +0000 | [diff] [blame] | 178 | from collections import UserDict |
Georg Brandl | 60d1456 | 2008-02-05 18:31:41 +0000 | [diff] [blame] | 179 | d = UserDict({'known_attr': 7}) |
| 180 | self.cannot_set_attr(self.fi.a.__func__, '__dict__', d, TypeError) |
Barry Warsaw | c1e100f | 2001-02-26 18:07:26 +0000 | [diff] [blame] | 181 | |
Georg Brandl | 60d1456 | 2008-02-05 18:31:41 +0000 | [diff] [blame] | 182 | 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 Warsaw | 4a420a0 | 2001-01-15 20:30:15 +0000 | [diff] [blame] | 197 | |
Georg Brandl | 60d1456 | 2008-02-05 18:31:41 +0000 | [diff] [blame] | 198 | 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 Warsaw | 4a420a0 | 2001-01-15 20:30:15 +0000 | [diff] [blame] | 202 | |
Georg Brandl | 60d1456 | 2008-02-05 18:31:41 +0000 | [diff] [blame] | 203 | def test_unassigned_dict(self): |
| 204 | self.assertEqual(self.b.__dict__, {}) |
Barry Warsaw | 4a420a0 | 2001-01-15 20:30:15 +0000 | [diff] [blame] | 205 | |
Georg Brandl | 60d1456 | 2008-02-05 18:31:41 +0000 | [diff] [blame] | 206 | 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 Warsaw | 534c60f | 2001-01-15 21:00:02 +0000 | [diff] [blame] | 211 | |
Georg Brandl | 60d1456 | 2008-02-05 18:31:41 +0000 | [diff] [blame] | 212 | class 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 Rossum | bd13149 | 2001-09-18 03:28:54 +0000 | [diff] [blame] | 221 | |
Georg Brandl | 60d1456 | 2008-02-05 18:31:41 +0000 | [diff] [blame] | 222 | 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 Rossum | d9d1d4a | 2001-09-17 23:46:56 +0000 | [diff] [blame] | 226 | |
Georg Brandl | 60d1456 | 2008-02-05 18:31:41 +0000 | [diff] [blame] | 227 | def test_main(): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 228 | support.run_unittest(FunctionPropertiesTest, ImplicitReferencesTest, |
Georg Brandl | 60d1456 | 2008-02-05 18:31:41 +0000 | [diff] [blame] | 229 | ArbitraryFunctionAttrTest, FunctionDictsTest, |
| 230 | FunctionDocstringTest) |
Guido van Rossum | d9d1d4a | 2001-09-17 23:46:56 +0000 | [diff] [blame] | 231 | |
Georg Brandl | 60d1456 | 2008-02-05 18:31:41 +0000 | [diff] [blame] | 232 | if __name__ == "__main__": |
| 233 | test_main() |