[Python-checkins] CVS: python/dist/src/Lib/test test_funcattrs.py,1.8,1.9
Guido van Rossum
gvanrossum@users.sourceforge.net
Mon, 17 Sep 2001 20:28:57 -0700
Update of /cvsroot/python/python/dist/src/Lib/test
In directory usw-pr-cvs1:/tmp/cvs-serv18492
Modified Files:
test_funcattrs.py
Log Message:
- 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.)
Index: test_funcattrs.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Lib/test/test_funcattrs.py,v
retrieving revision 1.8
retrieving revision 1.9
diff -C2 -d -r1.8 -r1.9
*** test_funcattrs.py 2001/09/17 23:46:56 1.8
--- test_funcattrs.py 2001/09/18 03:28:54 1.9
***************
*** 33,37 ****
try:
del b.__dict__
! except TypeError: pass
else: raise TestFailed, 'del func.__dict__ expected TypeError'
--- 33,37 ----
try:
del b.__dict__
! except (AttributeError, TypeError): pass
else: raise TestFailed, 'del func.__dict__ expected TypeError'
***************
*** 39,43 ****
try:
b.__dict__ = None
! except TypeError: pass
else: raise TestFailed, 'func.__dict__ = None expected TypeError'
--- 39,43 ----
try:
b.__dict__ = None
! except (AttributeError, TypeError): pass
else: raise TestFailed, 'func.__dict__ = None expected TypeError'
***************
*** 66,71 ****
try:
F.a.publish = 1
! except TypeError: pass
! else: raise TestFailed, 'expected TypeError'
# But setting it explicitly on the underlying function object is okay.
--- 66,71 ----
try:
F.a.publish = 1
! except (AttributeError, TypeError): pass
! else: raise TestFailed, 'expected AttributeError or TypeError'
# But setting it explicitly on the underlying function object is okay.
***************
*** 86,97 ****
try:
f1.a.publish = 0
! except TypeError: pass
! else: raise TestFailed, 'expected TypeError'
# See the comment above about the change in semantics for Python 2.1b1
try:
F.a.myclass = F
! except TypeError: pass
! else: raise TestFailed, 'expected TypeError'
F.a.im_func.myclass = F
--- 86,97 ----
try:
f1.a.publish = 0
! except (AttributeError, TypeError): pass
! else: raise TestFailed, 'expected AttributeError or TypeError'
# See the comment above about the change in semantics for Python 2.1b1
try:
F.a.myclass = F
! except (AttributeError, TypeError): pass
! else: raise TestFailed, 'expected AttributeError or TypeError'
F.a.im_func.myclass = F
***************
*** 109,113 ****
try:
F.a.__dict__ = (1, 2, 3)
! except TypeError: pass
else: raise TestFailed, 'expected TypeError'
--- 109,113 ----
try:
F.a.__dict__ = (1, 2, 3)
! except (AttributeError, TypeError): pass
else: raise TestFailed, 'expected TypeError'
***************
*** 122,126 ****
try:
F.a.__dict__ = d
! except TypeError: pass
else: raise TestFailed
--- 122,126 ----
try:
F.a.__dict__ = d
! except (AttributeError, TypeError): pass
else: raise TestFailed
***************
*** 143,147 ****
try:
F.id.foo = 12
! except TypeError: pass
else: raise TestFailed
--- 143,147 ----
try:
F.id.foo = 12
! except (AttributeError, TypeError): pass
else: raise TestFailed
***************
*** 158,162 ****
try:
eff.id.foo = 12
! except TypeError: pass
else: raise TestFailed
--- 158,162 ----
try:
eff.id.foo = 12
! except (AttributeError, TypeError): pass
else: raise TestFailed
***************
*** 172,186 ****
try:
del another.__dict__
! except TypeError: pass
else: raise TestFailed
try:
del another.func_dict
! except TypeError: pass
else: raise TestFailed
try:
another.func_dict = None
! except TypeError: pass
else: raise TestFailed
--- 172,186 ----
try:
del another.__dict__
! except (AttributeError, TypeError): pass
else: raise TestFailed
try:
del another.func_dict
! except (AttributeError, TypeError): pass
else: raise TestFailed
try:
another.func_dict = None
! except (AttributeError, TypeError): pass
else: raise TestFailed
***************
*** 215,237 ****
# Test all predefined function attributes systematically
! def test_func_closure():
! a = 12
! def f(): print a
! c = f.func_closure
! verify(isinstance(c, tuple))
! verify(len(c) == 1)
! verify(c[0].__class__.__name__ == "cell") # don't have a type object handy
try:
! 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():
--- 215,241 ----
# 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():
! a = 12
! def f(): print a
! c = f.func_closure
! verify(isinstance(c, tuple))
! verify(len(c) == 1)
! verify(c[0].__class__.__name__ == "cell") # don't have a type object handy
! cantset(f, "func_closure", c)
def test_func_doc():
***************
*** 255,270 ****
def f(): pass
verify(f.func_globals is globals())
! try:
! 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():
--- 259,263 ----
def f(): pass
verify(f.func_globals is globals())
! cantset(f, "func_globals", globals())
def test_func_name():
***************
*** 272,299 ****
verify(f.__name__ == "f")
verify(f.func_name == "f")
! try:
! f.func_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():
--- 265,270 ----
verify(f.__name__ == "f")
verify(f.func_name == "f")
! cantset(f, "func_name", "f")
! cantset(f, "__name__", "f")
def test_func_code():
***************
*** 302,311 ****
verify(type(f.func_code) is types.CodeType)
f.func_code = g.func_code
! try:
! del f.func_code
! except (AttributeError, TypeError):
! pass
! else:
! raise TestFailed, "shouldn't be allowed to del func_code"
def test_func_defaults():
--- 273,277 ----
verify(type(f.func_code) is types.CodeType)
f.func_code = g.func_code
! cantset(f, "func_code", None)
def test_func_defaults():
***************
*** 336,361 ****
verify(f.func_dict is a is f.__dict__)
f.func_dict = {}
! try:
! f.hello
! except (AttributeError, TypeError):
! pass
! else:
! raise TestFailed, "hello attribute should have disappeared"
f.__dict__ = {'world': 'hello'}
verify(f.world == "hello")
verify(f.__dict__ is f.func_dict == {'world': 'hello'})
! try:
! del f.func_dict
! except (AttributeError, TypeError):
! pass
! else:
! raise TestFailed, "shouldn't be allowed to delete func_dict"
! try:
! del f.__dict__
! except (AttributeError, TypeError):
pass
! else:
! raise TestFailed, "shouldn't be allowed to delete __dict__"
def testmore():
test_func_closure()
--- 302,364 ----
verify(f.func_dict is a is f.__dict__)
f.func_dict = {}
! verify(not hasattr(f, "hello"))
f.__dict__ = {'world': 'hello'}
verify(f.world == "hello")
verify(f.__dict__ is f.func_dict == {'world': 'hello'})
! cantset(f, "func_dict", None)
! cantset(f, "__dict__", None)
!
! 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
! C.foo = foo
! verify(C.foo.im_func is foo)
! verify(C().foo.im_func is foo)
! cantset(C.foo, "im_func", foo)
! cantset(C().foo, "im_func", foo)
+ def test_im_self():
+ 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():
test_func_closure()
***************
*** 366,369 ****
--- 369,379 ----
test_func_defaults()
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()