- Some tests that check that assignments are not allowed expect this
to raise TypeError. In practice, a disallowed attribute assignment can raise either TypeError or AttributeError (and it's unclear which is better). So allow either. (Yes, this is in anticipation of a code change that switches the exception raised. :-) - Add a utility function, cantset(), which verifies that setting a particular attribute to a given value is disallowed, and also that deleting that same attribute is disallowed. Use this in the test_func_*() tests. - Add a new set of tests that test conformance of various instance method attributes. (Also in anticipation of code that changes their implementation.)
This commit is contained in:
parent
427ce80df6
commit
bd13149711
|
@ -32,13 +32,13 @@ if 'publish' not in dir(b):
|
||||||
|
|
||||||
try:
|
try:
|
||||||
del b.__dict__
|
del b.__dict__
|
||||||
except TypeError: pass
|
except (AttributeError, TypeError): pass
|
||||||
else: raise TestFailed, 'del func.__dict__ expected TypeError'
|
else: raise TestFailed, 'del func.__dict__ expected TypeError'
|
||||||
|
|
||||||
b.publish = 1
|
b.publish = 1
|
||||||
try:
|
try:
|
||||||
b.__dict__ = None
|
b.__dict__ = None
|
||||||
except TypeError: pass
|
except (AttributeError, TypeError): pass
|
||||||
else: raise TestFailed, 'func.__dict__ = None expected TypeError'
|
else: raise TestFailed, 'func.__dict__ = None expected TypeError'
|
||||||
|
|
||||||
d = {'hello': 'world'}
|
d = {'hello': 'world'}
|
||||||
|
@ -65,8 +65,8 @@ else: raise TestFailed, 'expected AttributeError'
|
||||||
# (it was already disallowed on bound methods). See the PEP for details.
|
# (it was already disallowed on bound methods). See the PEP for details.
|
||||||
try:
|
try:
|
||||||
F.a.publish = 1
|
F.a.publish = 1
|
||||||
except TypeError: pass
|
except (AttributeError, TypeError): pass
|
||||||
else: raise TestFailed, 'expected TypeError'
|
else: raise TestFailed, 'expected AttributeError or TypeError'
|
||||||
|
|
||||||
# But setting it explicitly on the underlying function object is okay.
|
# But setting it explicitly on the underlying function object is okay.
|
||||||
F.a.im_func.publish = 1
|
F.a.im_func.publish = 1
|
||||||
|
@ -85,14 +85,14 @@ if 'publish' not in dir(F.a):
|
||||||
|
|
||||||
try:
|
try:
|
||||||
f1.a.publish = 0
|
f1.a.publish = 0
|
||||||
except TypeError: pass
|
except (AttributeError, TypeError): pass
|
||||||
else: raise TestFailed, 'expected TypeError'
|
else: raise TestFailed, 'expected AttributeError or TypeError'
|
||||||
|
|
||||||
# See the comment above about the change in semantics for Python 2.1b1
|
# See the comment above about the change in semantics for Python 2.1b1
|
||||||
try:
|
try:
|
||||||
F.a.myclass = F
|
F.a.myclass = F
|
||||||
except TypeError: pass
|
except (AttributeError, TypeError): pass
|
||||||
else: raise TestFailed, 'expected TypeError'
|
else: raise TestFailed, 'expected AttributeError or TypeError'
|
||||||
|
|
||||||
F.a.im_func.myclass = F
|
F.a.im_func.myclass = F
|
||||||
|
|
||||||
|
@ -108,7 +108,7 @@ if f1.a.myclass is not f2.a.myclass or \
|
||||||
# try setting __dict__
|
# try setting __dict__
|
||||||
try:
|
try:
|
||||||
F.a.__dict__ = (1, 2, 3)
|
F.a.__dict__ = (1, 2, 3)
|
||||||
except TypeError: pass
|
except (AttributeError, TypeError): pass
|
||||||
else: raise TestFailed, 'expected TypeError'
|
else: raise TestFailed, 'expected TypeError'
|
||||||
|
|
||||||
F.a.im_func.__dict__ = {'one': 11, 'two': 22, 'three': 33}
|
F.a.im_func.__dict__ = {'one': 11, 'two': 22, 'three': 33}
|
||||||
|
@ -121,7 +121,7 @@ d = UserDict({'four': 44, 'five': 55})
|
||||||
|
|
||||||
try:
|
try:
|
||||||
F.a.__dict__ = d
|
F.a.__dict__ = d
|
||||||
except TypeError: pass
|
except (AttributeError, TypeError): pass
|
||||||
else: raise TestFailed
|
else: raise TestFailed
|
||||||
|
|
||||||
if f2.a.one <> f1.a.one <> F.a.one <> 11:
|
if f2.a.one <> f1.a.one <> F.a.one <> 11:
|
||||||
|
@ -142,7 +142,7 @@ else: raise TestFailed
|
||||||
|
|
||||||
try:
|
try:
|
||||||
F.id.foo = 12
|
F.id.foo = 12
|
||||||
except TypeError: pass
|
except (AttributeError, TypeError): pass
|
||||||
else: raise TestFailed
|
else: raise TestFailed
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
@ -157,7 +157,7 @@ else: raise TestFailed
|
||||||
|
|
||||||
try:
|
try:
|
||||||
eff.id.foo = 12
|
eff.id.foo = 12
|
||||||
except TypeError: pass
|
except (AttributeError, TypeError): pass
|
||||||
else: raise TestFailed
|
else: raise TestFailed
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
@ -171,17 +171,17 @@ def another():
|
||||||
|
|
||||||
try:
|
try:
|
||||||
del another.__dict__
|
del another.__dict__
|
||||||
except TypeError: pass
|
except (AttributeError, TypeError): pass
|
||||||
else: raise TestFailed
|
else: raise TestFailed
|
||||||
|
|
||||||
try:
|
try:
|
||||||
del another.func_dict
|
del another.func_dict
|
||||||
except TypeError: pass
|
except (AttributeError, TypeError): pass
|
||||||
else: raise TestFailed
|
else: raise TestFailed
|
||||||
|
|
||||||
try:
|
try:
|
||||||
another.func_dict = None
|
another.func_dict = None
|
||||||
except TypeError: pass
|
except (AttributeError, TypeError): pass
|
||||||
else: raise TestFailed
|
else: raise TestFailed
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
@ -214,6 +214,21 @@ d[foo]
|
||||||
|
|
||||||
# Test all predefined function attributes systematically
|
# Test all predefined function attributes systematically
|
||||||
|
|
||||||
|
def cantset(obj, name, value):
|
||||||
|
verify(hasattr(obj, name)) # Otherwise it's probably a typo
|
||||||
|
try:
|
||||||
|
setattr(obj, name, value)
|
||||||
|
except (AttributeError, TypeError):
|
||||||
|
pass
|
||||||
|
else:
|
||||||
|
raise TestFailed, "shouldn't be able to set %s to %r" % (name, value)
|
||||||
|
try:
|
||||||
|
delattr(obj, name)
|
||||||
|
except (AttributeError, TypeError):
|
||||||
|
pass
|
||||||
|
else:
|
||||||
|
raise TestFailed, "shouldn't be able to del %s" % name
|
||||||
|
|
||||||
def test_func_closure():
|
def test_func_closure():
|
||||||
a = 12
|
a = 12
|
||||||
def f(): print a
|
def f(): print a
|
||||||
|
@ -221,18 +236,7 @@ def test_func_closure():
|
||||||
verify(isinstance(c, tuple))
|
verify(isinstance(c, tuple))
|
||||||
verify(len(c) == 1)
|
verify(len(c) == 1)
|
||||||
verify(c[0].__class__.__name__ == "cell") # don't have a type object handy
|
verify(c[0].__class__.__name__ == "cell") # don't have a type object handy
|
||||||
try:
|
cantset(f, "func_closure", c)
|
||||||
f.func_closure = c
|
|
||||||
except (AttributeError, TypeError):
|
|
||||||
pass
|
|
||||||
else:
|
|
||||||
raise TestFailed, "shouldn't be allowed to set func_closure"
|
|
||||||
try:
|
|
||||||
del a.func_closure
|
|
||||||
except (AttributeError, TypeError):
|
|
||||||
pass
|
|
||||||
else:
|
|
||||||
raise TestFailed, "shouldn't be allowed to del func_closure"
|
|
||||||
|
|
||||||
def test_func_doc():
|
def test_func_doc():
|
||||||
def f(): pass
|
def f(): pass
|
||||||
|
@ -254,59 +258,21 @@ def test_func_doc():
|
||||||
def test_func_globals():
|
def test_func_globals():
|
||||||
def f(): pass
|
def f(): pass
|
||||||
verify(f.func_globals is globals())
|
verify(f.func_globals is globals())
|
||||||
try:
|
cantset(f, "func_globals", globals())
|
||||||
f.func_globals = globals()
|
|
||||||
except (AttributeError, TypeError):
|
|
||||||
pass
|
|
||||||
else:
|
|
||||||
raise TestFailed, "shouldn't be allowed to set func_globals"
|
|
||||||
try:
|
|
||||||
del f.func_globals
|
|
||||||
except (AttributeError, TypeError):
|
|
||||||
pass
|
|
||||||
else:
|
|
||||||
raise TestFailed, "shouldn't be allowed to del func_globals"
|
|
||||||
|
|
||||||
def test_func_name():
|
def test_func_name():
|
||||||
def f(): pass
|
def f(): pass
|
||||||
verify(f.__name__ == "f")
|
verify(f.__name__ == "f")
|
||||||
verify(f.func_name == "f")
|
verify(f.func_name == "f")
|
||||||
try:
|
cantset(f, "func_name", "f")
|
||||||
f.func_name = "f"
|
cantset(f, "__name__", "f")
|
||||||
except (AttributeError, TypeError):
|
|
||||||
pass
|
|
||||||
else:
|
|
||||||
raise TestFailed, "shouldn't be allowed to set func_name"
|
|
||||||
try:
|
|
||||||
f.__name__ = "f"
|
|
||||||
except (AttributeError, TypeError):
|
|
||||||
pass
|
|
||||||
else:
|
|
||||||
raise TestFailed, "shouldn't be allowed to set __name__"
|
|
||||||
try:
|
|
||||||
del f.func_name
|
|
||||||
except (AttributeError, TypeError):
|
|
||||||
pass
|
|
||||||
else:
|
|
||||||
raise TestFailed, "shouldn't be allowed to del func_name"
|
|
||||||
try:
|
|
||||||
del f.__name__
|
|
||||||
except (AttributeError, TypeError):
|
|
||||||
pass
|
|
||||||
else:
|
|
||||||
raise TestFailed, "shouldn't be allowed to del __name__"
|
|
||||||
|
|
||||||
def test_func_code():
|
def test_func_code():
|
||||||
def f(): pass
|
def f(): pass
|
||||||
def g(): print 12
|
def g(): print 12
|
||||||
verify(type(f.func_code) is types.CodeType)
|
verify(type(f.func_code) is types.CodeType)
|
||||||
f.func_code = g.func_code
|
f.func_code = g.func_code
|
||||||
try:
|
cantset(f, "func_code", None)
|
||||||
del f.func_code
|
|
||||||
except (AttributeError, TypeError):
|
|
||||||
pass
|
|
||||||
else:
|
|
||||||
raise TestFailed, "shouldn't be allowed to del func_code"
|
|
||||||
|
|
||||||
def test_func_defaults():
|
def test_func_defaults():
|
||||||
def f(a, b): return (a, b)
|
def f(a, b): return (a, b)
|
||||||
|
@ -335,27 +301,64 @@ def test_func_dict():
|
||||||
verify(a == {'hello': 'world'})
|
verify(a == {'hello': 'world'})
|
||||||
verify(f.func_dict is a is f.__dict__)
|
verify(f.func_dict is a is f.__dict__)
|
||||||
f.func_dict = {}
|
f.func_dict = {}
|
||||||
try:
|
verify(not hasattr(f, "hello"))
|
||||||
f.hello
|
|
||||||
except (AttributeError, TypeError):
|
|
||||||
pass
|
|
||||||
else:
|
|
||||||
raise TestFailed, "hello attribute should have disappeared"
|
|
||||||
f.__dict__ = {'world': 'hello'}
|
f.__dict__ = {'world': 'hello'}
|
||||||
verify(f.world == "hello")
|
verify(f.world == "hello")
|
||||||
verify(f.__dict__ is f.func_dict == {'world': 'hello'})
|
verify(f.__dict__ is f.func_dict == {'world': 'hello'})
|
||||||
try:
|
cantset(f, "func_dict", None)
|
||||||
del f.func_dict
|
cantset(f, "__dict__", None)
|
||||||
except (AttributeError, TypeError):
|
|
||||||
|
def test_im_class():
|
||||||
|
class C:
|
||||||
|
def foo(self): pass
|
||||||
|
verify(C.foo.im_class is C)
|
||||||
|
verify(C().foo.im_class is C)
|
||||||
|
cantset(C.foo, "im_class", C)
|
||||||
|
cantset(C().foo, "im_class", C)
|
||||||
|
|
||||||
|
def test_im_func():
|
||||||
|
def foo(self): pass
|
||||||
|
class C:
|
||||||
pass
|
pass
|
||||||
else:
|
C.foo = foo
|
||||||
raise TestFailed, "shouldn't be allowed to delete func_dict"
|
verify(C.foo.im_func is foo)
|
||||||
try:
|
verify(C().foo.im_func is foo)
|
||||||
del f.__dict__
|
cantset(C.foo, "im_func", foo)
|
||||||
except (AttributeError, TypeError):
|
cantset(C().foo, "im_func", foo)
|
||||||
pass
|
|
||||||
else:
|
def test_im_self():
|
||||||
raise TestFailed, "shouldn't be allowed to delete __dict__"
|
class C:
|
||||||
|
def foo(self): pass
|
||||||
|
verify(C.foo.im_self is None)
|
||||||
|
c = C()
|
||||||
|
verify(c.foo.im_self is c)
|
||||||
|
cantset(C.foo, "im_self", None)
|
||||||
|
cantset(c.foo, "im_self", c)
|
||||||
|
|
||||||
|
def test_im_dict():
|
||||||
|
class C:
|
||||||
|
def foo(self): pass
|
||||||
|
foo.bar = 42
|
||||||
|
verify(C.foo.__dict__ == {'bar': 42})
|
||||||
|
verify(C().foo.__dict__ == {'bar': 42})
|
||||||
|
cantset(C.foo, "__dict__", C.foo.__dict__)
|
||||||
|
cantset(C().foo, "__dict__", C.foo.__dict__)
|
||||||
|
|
||||||
|
def test_im_doc():
|
||||||
|
class C:
|
||||||
|
def foo(self): "hello"
|
||||||
|
verify(C.foo.__doc__ == "hello")
|
||||||
|
verify(C().foo.__doc__ == "hello")
|
||||||
|
cantset(C.foo, "__doc__", "hello")
|
||||||
|
cantset(C().foo, "__doc__", "hello")
|
||||||
|
|
||||||
|
def test_im_name():
|
||||||
|
class C:
|
||||||
|
def foo(self): pass
|
||||||
|
verify(C.foo.__name__ == "foo")
|
||||||
|
verify(C().foo.__name__ == "foo")
|
||||||
|
cantset(C.foo, "__name__", "foo")
|
||||||
|
cantset(C().foo, "__name__", "foo")
|
||||||
|
|
||||||
def testmore():
|
def testmore():
|
||||||
test_func_closure()
|
test_func_closure()
|
||||||
|
@ -365,5 +368,12 @@ def testmore():
|
||||||
test_func_code()
|
test_func_code()
|
||||||
test_func_defaults()
|
test_func_defaults()
|
||||||
test_func_dict()
|
test_func_dict()
|
||||||
|
# Tests for instance method attributes
|
||||||
|
test_im_class()
|
||||||
|
test_im_func()
|
||||||
|
test_im_self()
|
||||||
|
test_im_dict()
|
||||||
|
test_im_doc()
|
||||||
|
test_im_name()
|
||||||
|
|
||||||
testmore()
|
testmore()
|
||||||
|
|
Loading…
Reference in New Issue