Changeset 391 for python/trunk/Lib/test/test_funcattrs.py
- Timestamp:
- Mar 19, 2014, 11:31:01 PM (11 years ago)
- Location:
- python/trunk
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
python/trunk
-
Property svn:mergeinfo
set to
/python/vendor/Python-2.7.6 merged eligible /python/vendor/current merged eligible
-
Property svn:mergeinfo
set to
-
python/trunk/Lib/test/test_funcattrs.py
r2 r391 14 14 self.b = b 15 15 16 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) 16 def cannot_set_attr(self, obj, name, value, exceptions): 17 # Helper method for other tests. 18 try: 19 setattr(obj, name, value) 20 except exceptions: 21 pass 22 else: 23 self.fail("shouldn't be able to set %s to %r" % (name, value)) 24 try: 25 delattr(obj, name) 26 except exceptions: 27 pass 28 else: 29 self.fail("shouldn't be able to del %s" % name) 25 30 26 31 … … 32 37 def test_dir_includes_correct_attrs(self): 33 38 self.b.known_attr = 7 34 self.assert _('known_attr' indir(self.b),35 "set attributes not in dir listing of method")39 self.assertIn('known_attr', dir(self.b), 40 "set attributes not in dir listing of method") 36 41 # Test on underlying function object of method 37 42 self.f.a.im_func.known_attr = 7 38 self.assert _('known_attr' indir(self.f.a),39 "set attribute on unbound method implementation in class notin "40 "dir")41 self.assert _('known_attr' indir(self.fi.a),42 "set attribute on unbound method implementations, should show up"43 "in next dir")43 self.assertIn('known_attr', dir(self.f.a), 44 "set attribute on unbound method implementation in " 45 "class not in dir") 46 self.assertIn('known_attr', dir(self.fi.a), 47 "set attribute on unbound method implementations, " 48 "should show up in next dir") 44 49 45 50 def test_duplicate_function_equality(self): … … 57 62 58 63 def test_func_globals(self): 59 self.assert Equal(self.b.func_globals, globals())64 self.assertIs(self.b.func_globals, globals()) 60 65 self.cannot_set_attr(self.b, 'func_globals', 2, TypeError) 66 67 def test_func_closure(self): 68 a = 12 69 def f(): print a 70 c = f.func_closure 71 self.assertIsInstance(c, tuple) 72 self.assertEqual(len(c), 1) 73 # don't have a type object handy 74 self.assertEqual(c[0].__class__.__name__, "cell") 75 self.cannot_set_attr(f, "func_closure", c, TypeError) 76 77 def test_empty_cell(self): 78 def f(): print a 79 try: 80 f.func_closure[0].cell_contents 81 except ValueError: 82 pass 83 else: 84 self.fail("shouldn't be able to read an empty cell") 85 a = 12 61 86 62 87 def test_func_name(self): … … 97 122 self.assertEqual(c(), 7) 98 123 # 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") 124 try: 125 b.func_code = c.func_code 126 except ValueError: 127 pass 128 else: 129 self.fail("func_code with different numbers of free vars should " 130 "not be possible") 131 try: 132 e.func_code = d.func_code 133 except ValueError: 134 pass 135 else: 136 self.fail("func_code with different numbers of free vars should " 137 "not be possible") 109 138 110 139 def test_blank_func_defaults(self): … … 127 156 del second_func.func_defaults 128 157 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") 134 135 class ImplicitReferencesTest(FuncAttrsTest): 158 try: 159 second_func() 160 except TypeError: 161 pass 162 else: 163 self.fail("func_defaults does not update; deleting it does not " 164 "remove requirement") 165 166 167 class InstancemethodAttrTest(FuncAttrsTest): 136 168 def test_im_class(self): 137 169 self.assertEqual(self.f.a.im_class, self.f) … … 160 192 self.assertNotEqual(self.fi.id(), id(self.f)) 161 193 # Test usage 162 try: self.f.id.unknown_attr 163 except AttributeError: pass 164 else: self.fail("using unknown attributes should raise AttributeError") 194 try: 195 self.f.id.unknown_attr 196 except AttributeError: 197 pass 198 else: 199 self.fail("using unknown attributes should raise AttributeError") 165 200 # Test assignment and deletion 166 201 self.cannot_set_attr(self.f.id, 'unknown_attr', 2, AttributeError) … … 172 207 self.assertEqual(self.fi.a.known_attr, 7) 173 208 209 174 210 class ArbitraryFunctionAttrTest(FuncAttrsTest): 175 211 def test_set_attr(self): 212 # setting attributes only works on function objects 176 213 self.b.known_attr = 7 177 214 self.assertEqual(self.b.known_attr, 7) 178 215 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") 216 try: 217 func.known_attr = 7 218 except AttributeError: 219 pass 220 else: 221 self.fail("setting attributes on methods should raise error") 182 222 183 223 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") 224 try: 225 del self.b.unknown_attr 226 except AttributeError: 227 pass 228 else: 229 self.fail("deleting unknown attribute should raise TypeError") 187 230 188 231 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") 232 try: 233 self.f.a.klass = self.f 234 except AttributeError: 235 pass 236 else: 237 self.fail("setting arbitrary attribute in unbound function " 238 " should raise AttributeError") 193 239 self.f.a.im_func.klass = self.f 194 240 for method in [self.f.a, self.fi.a, self.fi.a.im_func]: … … 197 243 def test_unset_attr(self): 198 244 for func in [self.b, self.f.a, self.fi.a]: 199 try: func.non_existent_attr 200 except AttributeError: pass 201 else: self.fail("using unknown attributes should raise " 202 "AttributeError") 245 try: 246 func.non_existent_attr 247 except AttributeError: 248 pass 249 else: 250 self.fail("using unknown attributes should raise " 251 "AttributeError") 252 203 253 204 254 class FunctionDictsTest(FuncAttrsTest): … … 217 267 self.f.a.im_func.__dict__ = d 218 268 # Test assignment 219 self.assert Equal(d, self.b.__dict__)220 self.assert Equal(d, self.b.func_dict)269 self.assertIs(d, self.b.__dict__) 270 self.assertIs(d, self.b.func_dict) 221 271 # ... and on all the different ways of referencing the method's func 222 self.assert Equal(d, self.f.a.im_func.__dict__)223 self.assert Equal(d, self.f.a.__dict__)224 self.assert Equal(d, self.fi.a.im_func.__dict__)225 self.assert Equal(d, self.fi.a.__dict__)272 self.assertIs(d, self.f.a.im_func.__dict__) 273 self.assertIs(d, self.f.a.__dict__) 274 self.assertIs(d, self.fi.a.im_func.__dict__) 275 self.assertIs(d, self.fi.a.__dict__) 226 276 # Test value 227 277 self.assertEqual(self.b.known_attr, 7) … … 235 285 236 286 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") 287 try: 288 del self.b.__dict__ 289 except TypeError: 290 pass 291 else: 292 self.fail("deleting function dictionary should raise TypeError") 293 try: 294 del self.b.func_dict 295 except TypeError: 296 pass 297 else: 298 self.fail("deleting function dictionary should raise TypeError") 243 299 244 300 def test_unassigned_dict(self): … … 250 306 d[self.b] = value 251 307 self.assertEqual(d[self.b], value) 308 252 309 253 310 class FunctionDocstringTest(FuncAttrsTest): … … 274 331 self.assertEqual(self.b.func_doc, None) 275 332 333 334 class StaticMethodAttrsTest(unittest.TestCase): 335 def test_func_attribute(self): 336 def f(): 337 pass 338 339 c = classmethod(f) 340 self.assertTrue(c.__func__ is f) 341 342 s = staticmethod(f) 343 self.assertTrue(s.__func__ is f) 344 345 276 346 def test_main(): 277 test_support.run_unittest(FunctionPropertiesTest, I mplicitReferencesTest,347 test_support.run_unittest(FunctionPropertiesTest, InstancemethodAttrTest, 278 348 ArbitraryFunctionAttrTest, FunctionDictsTest, 279 FunctionDocstringTest) 349 FunctionDocstringTest, 350 StaticMethodAttrsTest) 280 351 281 352 if __name__ == "__main__":
Note:
See TracChangeset
for help on using the changeset viewer.