Kaydet (Commit) ed4ca821 authored tarafından Georg Brandl's avatar Georg Brandl

#5101: add back tests to test_funcattrs that were lost during unittest…

#5101: add back tests to test_funcattrs that were lost during unittest conversion, and make some PEP8 cleanups.
üst d22b9519
...@@ -13,15 +13,20 @@ class FuncAttrsTest(unittest.TestCase): ...@@ -13,15 +13,20 @@ class FuncAttrsTest(unittest.TestCase):
self.fi = F() self.fi = F()
self.b = b self.b = b
def cannot_set_attr(self,obj, name, value, exceptions): def cannot_set_attr(self, obj, name, value, exceptions):
# This method is not called as a test (name doesn't start with 'test'), # Helper method for other tests.
# but may be used by other tests. try:
try: setattr(obj, name, value) setattr(obj, name, value)
except exceptions: pass except exceptions:
else: self.fail("shouldn't be able to set %s to %r" % (name, value)) pass
try: delattr(obj, name) else:
except exceptions: pass self.fail("shouldn't be able to set %s to %r" % (name, value))
else: self.fail("shouldn't be able to del %s" % name) try:
delattr(obj, name)
except exceptions:
pass
else:
self.fail("shouldn't be able to del %s" % name)
class FunctionPropertiesTest(FuncAttrsTest): class FunctionPropertiesTest(FuncAttrsTest):
...@@ -32,15 +37,15 @@ class FunctionPropertiesTest(FuncAttrsTest): ...@@ -32,15 +37,15 @@ class FunctionPropertiesTest(FuncAttrsTest):
def test_dir_includes_correct_attrs(self): def test_dir_includes_correct_attrs(self):
self.b.known_attr = 7 self.b.known_attr = 7
self.assertTrue('known_attr' in dir(self.b), self.assertTrue('known_attr' in dir(self.b),
"set attributes not in dir listing of method") "set attributes not in dir listing of method")
# Test on underlying function object of method # Test on underlying function object of method
self.f.a.im_func.known_attr = 7 self.f.a.im_func.known_attr = 7
self.assertTrue('known_attr' in dir(self.f.a), self.assertTrue('known_attr' in dir(self.f.a),
"set attribute on unbound method implementation in class not in " "set attribute on unbound method implementation in "
"dir") "class not in dir")
self.assertTrue('known_attr' in dir(self.fi.a), self.assertTrue('known_attr' in dir(self.fi.a),
"set attribute on unbound method implementations, should show up" "set attribute on unbound method implementations, "
" in next dir") "should show up in next dir")
def test_duplicate_function_equality(self): def test_duplicate_function_equality(self):
# Body of `duplicate' is the exact same as self.b # Body of `duplicate' is the exact same as self.b
...@@ -56,9 +61,29 @@ class FunctionPropertiesTest(FuncAttrsTest): ...@@ -56,9 +61,29 @@ class FunctionPropertiesTest(FuncAttrsTest):
self.assertEqual(test(), 3) # self.b always returns 3, arbitrarily self.assertEqual(test(), 3) # self.b always returns 3, arbitrarily
def test_func_globals(self): def test_func_globals(self):
self.assertEqual(self.b.func_globals, globals()) self.assertIs(self.b.func_globals, globals())
self.cannot_set_attr(self.b, 'func_globals', 2, TypeError) self.cannot_set_attr(self.b, 'func_globals', 2, TypeError)
def test_func_closure(self):
a = 12
def f(): print a
c = f.func_closure
self.assertTrue(isinstance(c, tuple))
self.assertEqual(len(c), 1)
# don't have a type object handy
self.assertEqual(c[0].__class__.__name__, "cell")
self.cannot_set_attr(f, "func_closure", c, TypeError)
def test_empty_cell(self):
def f(): print a
try:
f.func_closure[0].cell_contents
except ValueError:
pass
else:
self.fail("shouldn't be able to read an empty cell")
a = 12
def test_func_name(self): def test_func_name(self):
self.assertEqual(self.b.__name__, 'b') self.assertEqual(self.b.__name__, 'b')
self.assertEqual(self.b.func_name, 'b') self.assertEqual(self.b.func_name, 'b')
...@@ -96,16 +121,20 @@ class FunctionPropertiesTest(FuncAttrsTest): ...@@ -96,16 +121,20 @@ class FunctionPropertiesTest(FuncAttrsTest):
self.assertEqual(c.func_code, d.func_code) self.assertEqual(c.func_code, d.func_code)
self.assertEqual(c(), 7) self.assertEqual(c(), 7)
# self.assertEqual(d(), 7) # self.assertEqual(d(), 7)
try: b.func_code = c.func_code try:
except ValueError: pass b.func_code = c.func_code
else: self.fail( except ValueError:
"func_code with different numbers of free vars should not be " pass
"possible") else:
try: e.func_code = d.func_code self.fail("func_code with different numbers of free vars should "
except ValueError: pass "not be possible")
else: self.fail( try:
"func_code with different numbers of free vars should not be " e.func_code = d.func_code
"possible") except ValueError:
pass
else:
self.fail("func_code with different numbers of free vars should "
"not be possible")
def test_blank_func_defaults(self): def test_blank_func_defaults(self):
self.assertEqual(self.b.func_defaults, None) self.assertEqual(self.b.func_defaults, None)
...@@ -126,13 +155,16 @@ class FunctionPropertiesTest(FuncAttrsTest): ...@@ -126,13 +155,16 @@ class FunctionPropertiesTest(FuncAttrsTest):
self.assertEqual(first_func(3, 5), 8) self.assertEqual(first_func(3, 5), 8)
del second_func.func_defaults del second_func.func_defaults
self.assertEqual(second_func.func_defaults, None) self.assertEqual(second_func.func_defaults, None)
try: second_func() try:
except TypeError: pass second_func()
else: self.fail( except TypeError:
"func_defaults does not update; deleting it does not remove " pass
"requirement") else:
self.fail("func_defaults does not update; deleting it does not "
"remove requirement")
class ImplicitReferencesTest(FuncAttrsTest): class InstancemethodAttrTest(FuncAttrsTest):
def test_im_class(self): def test_im_class(self):
self.assertEqual(self.f.a.im_class, self.f) self.assertEqual(self.f.a.im_class, self.f)
self.assertEqual(self.fi.a.im_class, self.f) self.assertEqual(self.fi.a.im_class, self.f)
...@@ -159,9 +191,12 @@ class ImplicitReferencesTest(FuncAttrsTest): ...@@ -159,9 +191,12 @@ class ImplicitReferencesTest(FuncAttrsTest):
self.assertEqual(self.fi.id(), id(self.fi)) self.assertEqual(self.fi.id(), id(self.fi))
self.assertNotEqual(self.fi.id(), id(self.f)) self.assertNotEqual(self.fi.id(), id(self.f))
# Test usage # Test usage
try: self.f.id.unknown_attr try:
except AttributeError: pass self.f.id.unknown_attr
else: self.fail("using unknown attributes should raise AttributeError") except AttributeError:
pass
else:
self.fail("using unknown attributes should raise AttributeError")
# Test assignment and deletion # Test assignment and deletion
self.cannot_set_attr(self.f.id, 'unknown_attr', 2, AttributeError) self.cannot_set_attr(self.f.id, 'unknown_attr', 2, AttributeError)
self.cannot_set_attr(self.fi.id, 'unknown_attr', 2, AttributeError) self.cannot_set_attr(self.fi.id, 'unknown_attr', 2, AttributeError)
...@@ -171,35 +206,50 @@ class ImplicitReferencesTest(FuncAttrsTest): ...@@ -171,35 +206,50 @@ class ImplicitReferencesTest(FuncAttrsTest):
self.assertEqual(self.f.a.known_attr, 7) self.assertEqual(self.f.a.known_attr, 7)
self.assertEqual(self.fi.a.known_attr, 7) self.assertEqual(self.fi.a.known_attr, 7)
class ArbitraryFunctionAttrTest(FuncAttrsTest): class ArbitraryFunctionAttrTest(FuncAttrsTest):
def test_set_attr(self): def test_set_attr(self):
# setting attributes only works on function objects
self.b.known_attr = 7 self.b.known_attr = 7
self.assertEqual(self.b.known_attr, 7) self.assertEqual(self.b.known_attr, 7)
for func in [self.f.a, self.fi.a]: for func in [self.f.a, self.fi.a]:
try: func.known_attr = 7 try:
except AttributeError: pass func.known_attr = 7
else: self.fail("setting attributes on methods should raise error") except AttributeError:
pass
else:
self.fail("setting attributes on methods should raise error")
def test_delete_unknown_attr(self): def test_delete_unknown_attr(self):
try: del self.b.unknown_attr try:
except AttributeError: pass del self.b.unknown_attr
else: self.fail("deleting unknown attribute should raise TypeError") except AttributeError:
pass
else:
self.fail("deleting unknown attribute should raise TypeError")
def test_setting_attrs_duplicates(self): def test_setting_attrs_duplicates(self):
try: self.f.a.klass = self.f try:
except AttributeError: pass self.f.a.klass = self.f
else: self.fail("setting arbitrary attribute in unbound function " except AttributeError:
" should raise AttributeError") pass
else:
self.fail("setting arbitrary attribute in unbound function "
" should raise AttributeError")
self.f.a.im_func.klass = self.f self.f.a.im_func.klass = self.f
for method in [self.f.a, self.fi.a, self.fi.a.im_func]: for method in [self.f.a, self.fi.a, self.fi.a.im_func]:
self.assertEqual(method.klass, self.f) self.assertEqual(method.klass, self.f)
def test_unset_attr(self): def test_unset_attr(self):
for func in [self.b, self.f.a, self.fi.a]: for func in [self.b, self.f.a, self.fi.a]:
try: func.non_existent_attr try:
except AttributeError: pass func.non_existent_attr
else: self.fail("using unknown attributes should raise " except AttributeError:
"AttributeError") pass
else:
self.fail("using unknown attributes should raise "
"AttributeError")
class FunctionDictsTest(FuncAttrsTest): class FunctionDictsTest(FuncAttrsTest):
def test_setting_dict_to_invalid(self): def test_setting_dict_to_invalid(self):
...@@ -216,13 +266,13 @@ class FunctionDictsTest(FuncAttrsTest): ...@@ -216,13 +266,13 @@ class FunctionDictsTest(FuncAttrsTest):
# Setting dict is only possible on the underlying function objects # Setting dict is only possible on the underlying function objects
self.f.a.im_func.__dict__ = d self.f.a.im_func.__dict__ = d
# Test assignment # Test assignment
self.assertEqual(d, self.b.__dict__) self.assertIs(d, self.b.__dict__)
self.assertEqual(d, self.b.func_dict) self.assertIs(d, self.b.func_dict)
# ... and on all the different ways of referencing the method's func # ... and on all the different ways of referencing the method's func
self.assertEqual(d, self.f.a.im_func.__dict__) self.assertIs(d, self.f.a.im_func.__dict__)
self.assertEqual(d, self.f.a.__dict__) self.assertIs(d, self.f.a.__dict__)
self.assertEqual(d, self.fi.a.im_func.__dict__) self.assertIs(d, self.fi.a.im_func.__dict__)
self.assertEqual(d, self.fi.a.__dict__) self.assertIs(d, self.fi.a.__dict__)
# Test value # Test value
self.assertEqual(self.b.known_attr, 7) self.assertEqual(self.b.known_attr, 7)
self.assertEqual(self.b.__dict__['known_attr'], 7) self.assertEqual(self.b.__dict__['known_attr'], 7)
...@@ -234,12 +284,18 @@ class FunctionDictsTest(FuncAttrsTest): ...@@ -234,12 +284,18 @@ class FunctionDictsTest(FuncAttrsTest):
self.assertEqual(self.fi.a.known_attr, 7) self.assertEqual(self.fi.a.known_attr, 7)
def test_delete_func_dict(self): def test_delete_func_dict(self):
try: del self.b.__dict__ try:
except TypeError: pass del self.b.__dict__
else: self.fail("deleting function dictionary should raise TypeError") except TypeError:
try: del self.b.func_dict pass
except TypeError: pass else:
else: self.fail("deleting function dictionary should raise TypeError") self.fail("deleting function dictionary should raise TypeError")
try:
del self.b.func_dict
except TypeError:
pass
else:
self.fail("deleting function dictionary should raise TypeError")
def test_unassigned_dict(self): def test_unassigned_dict(self):
self.assertEqual(self.b.__dict__, {}) self.assertEqual(self.b.__dict__, {})
...@@ -250,6 +306,7 @@ class FunctionDictsTest(FuncAttrsTest): ...@@ -250,6 +306,7 @@ class FunctionDictsTest(FuncAttrsTest):
d[self.b] = value d[self.b] = value
self.assertEqual(d[self.b], value) self.assertEqual(d[self.b], value)
class FunctionDocstringTest(FuncAttrsTest): class FunctionDocstringTest(FuncAttrsTest):
def test_set_docstring_attr(self): def test_set_docstring_attr(self):
self.assertEqual(self.b.__doc__, None) self.assertEqual(self.b.__doc__, None)
...@@ -273,6 +330,7 @@ class FunctionDocstringTest(FuncAttrsTest): ...@@ -273,6 +330,7 @@ class FunctionDocstringTest(FuncAttrsTest):
self.assertEqual(self.b.__doc__, None) self.assertEqual(self.b.__doc__, None)
self.assertEqual(self.b.func_doc, None) self.assertEqual(self.b.func_doc, None)
class StaticMethodAttrsTest(unittest.TestCase): class StaticMethodAttrsTest(unittest.TestCase):
def test_func_attribute(self): def test_func_attribute(self):
def f(): def f():
...@@ -286,7 +344,7 @@ class StaticMethodAttrsTest(unittest.TestCase): ...@@ -286,7 +344,7 @@ class StaticMethodAttrsTest(unittest.TestCase):
def test_main(): def test_main():
test_support.run_unittest(FunctionPropertiesTest, ImplicitReferencesTest, test_support.run_unittest(FunctionPropertiesTest, InstancemethodAttrTest,
ArbitraryFunctionAttrTest, FunctionDictsTest, ArbitraryFunctionAttrTest, FunctionDictsTest,
FunctionDocstringTest, FunctionDocstringTest,
StaticMethodAttrsTest) StaticMethodAttrsTest)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment