PyXR

c:\python24\lib \ test \ test_funcattrs.py



0001 from test.test_support import verbose, TestFailed, verify
0002 import types
0003 
0004 class F:
0005     def a(self):
0006         pass
0007 
0008 def b():
0009     'my docstring'
0010     pass
0011 
0012 # __module__ is a special attribute
0013 verify(b.__module__ == __name__)
0014 verify(verify.__module__ == "test.test_support")
0015 
0016 # setting attributes on functions
0017 try:
0018     b.publish
0019 except AttributeError: pass
0020 else: raise TestFailed, 'expected AttributeError'
0021 
0022 if b.__dict__ <> {}:
0023     raise TestFailed, 'expected unassigned func.__dict__ to be {}'
0024 
0025 b.publish = 1
0026 if b.publish <> 1:
0027     raise TestFailed, 'function attribute not set to expected value'
0028 
0029 docstring = 'its docstring'
0030 b.__doc__ = docstring
0031 if b.__doc__ <> docstring:
0032     raise TestFailed, 'problem with setting __doc__ attribute'
0033 
0034 if 'publish' not in dir(b):
0035     raise TestFailed, 'attribute not in dir()'
0036 
0037 try:
0038     del b.__dict__
0039 except TypeError: pass
0040 else: raise TestFailed, 'del func.__dict__ expected TypeError'
0041 
0042 b.publish = 1
0043 try:
0044     b.__dict__ = None
0045 except TypeError: pass
0046 else: raise TestFailed, 'func.__dict__ = None expected TypeError'
0047 
0048 d = {'hello': 'world'}
0049 b.__dict__ = d
0050 if b.func_dict is not d:
0051     raise TestFailed, 'func.__dict__ assignment to dictionary failed'
0052 if b.hello <> 'world':
0053     raise TestFailed, 'attribute after func.__dict__ assignment failed'
0054 
0055 f1 = F()
0056 f2 = F()
0057 
0058 try:
0059     F.a.publish
0060 except AttributeError: pass
0061 else: raise TestFailed, 'expected AttributeError'
0062 
0063 try:
0064     f1.a.publish
0065 except AttributeError: pass
0066 else: raise TestFailed, 'expected AttributeError'
0067 
0068 # In Python 2.1 beta 1, we disallowed setting attributes on unbound methods
0069 # (it was already disallowed on bound methods).  See the PEP for details.
0070 try:
0071     F.a.publish = 1
0072 except (AttributeError, TypeError): pass
0073 else: raise TestFailed, 'expected AttributeError or TypeError'
0074 
0075 # But setting it explicitly on the underlying function object is okay.
0076 F.a.im_func.publish = 1
0077 
0078 if F.a.publish <> 1:
0079     raise TestFailed, 'unbound method attribute not set to expected value'
0080 
0081 if f1.a.publish <> 1:
0082     raise TestFailed, 'bound method attribute access did not work'
0083 
0084 if f2.a.publish <> 1:
0085     raise TestFailed, 'bound method attribute access did not work'
0086 
0087 if 'publish' not in dir(F.a):
0088     raise TestFailed, 'attribute not in dir()'
0089 
0090 try:
0091     f1.a.publish = 0
0092 except (AttributeError, TypeError): pass
0093 else: raise TestFailed, 'expected AttributeError or TypeError'
0094 
0095 # See the comment above about the change in semantics for Python 2.1b1
0096 try:
0097     F.a.myclass = F
0098 except (AttributeError, TypeError): pass
0099 else: raise TestFailed, 'expected AttributeError or TypeError'
0100 
0101 F.a.im_func.myclass = F
0102 
0103 f1.a.myclass
0104 f2.a.myclass
0105 f1.a.myclass
0106 F.a.myclass
0107 
0108 if f1.a.myclass is not f2.a.myclass or \
0109        f1.a.myclass is not F.a.myclass:
0110     raise TestFailed, 'attributes were not the same'
0111 
0112 # try setting __dict__
0113 try:
0114     F.a.__dict__ = (1, 2, 3)
0115 except (AttributeError, TypeError): pass
0116 else: raise TestFailed, 'expected TypeError or AttributeError'
0117 
0118 F.a.im_func.__dict__ = {'one': 11, 'two': 22, 'three': 33}
0119 
0120 if f1.a.two <> 22:
0121     raise TestFailed, 'setting __dict__'
0122 
0123 from UserDict import UserDict
0124 d = UserDict({'four': 44, 'five': 55})
0125 
0126 try:
0127     F.a.__dict__ = d
0128 except (AttributeError, TypeError): pass
0129 else: raise TestFailed
0130 
0131 if f2.a.one <> f1.a.one <> F.a.one <> 11:
0132     raise TestFailed
0133 
0134 # im_func may not be a Python method!
0135 import new
0136 F.id = new.instancemethod(id, None, F)
0137 
0138 eff = F()
0139 if eff.id() <> id(eff):
0140     raise TestFailed
0141 
0142 try:
0143     F.id.foo
0144 except AttributeError: pass
0145 else: raise TestFailed
0146 
0147 try:
0148     F.id.foo = 12
0149 except (AttributeError, TypeError): pass
0150 else: raise TestFailed
0151 
0152 try:
0153     F.id.foo
0154 except AttributeError: pass
0155 else: raise TestFailed
0156 
0157 try:
0158     eff.id.foo
0159 except AttributeError: pass
0160 else: raise TestFailed
0161 
0162 try:
0163     eff.id.foo = 12
0164 except (AttributeError, TypeError): pass
0165 else: raise TestFailed
0166 
0167 try:
0168     eff.id.foo
0169 except AttributeError: pass
0170 else: raise TestFailed
0171 
0172 # Regression test for a crash in pre-2.1a1
0173 def another():
0174     pass
0175 
0176 try:
0177     del another.__dict__
0178 except TypeError: pass
0179 else: raise TestFailed
0180 
0181 try:
0182     del another.func_dict
0183 except TypeError: pass
0184 else: raise TestFailed
0185 
0186 try:
0187     another.func_dict = None
0188 except TypeError: pass
0189 else: raise TestFailed
0190 
0191 try:
0192     del another.bar
0193 except AttributeError: pass
0194 else: raise TestFailed
0195 
0196 # This isn't specifically related to function attributes, but it does test a
0197 # core dump regression in funcobject.c
0198 del another.func_defaults
0199 
0200 def foo():
0201     pass
0202 
0203 def bar():
0204     pass
0205 
0206 def temp():
0207     print 1
0208 
0209 if foo==bar:
0210     raise TestFailed
0211 
0212 d={}
0213 d[foo] = 1
0214 
0215 foo.func_code = temp.func_code
0216 
0217 d[foo]
0218 
0219 # Test all predefined function attributes systematically
0220 
0221 def cantset(obj, name, value, exception=(AttributeError, TypeError)):
0222     verify(hasattr(obj, name)) # Otherwise it's probably a typo
0223     try:
0224         setattr(obj, name, value)
0225     except exception:
0226         pass
0227     else:
0228         raise TestFailed, "shouldn't be able to set %s to %r" % (name, value)
0229     try:
0230         delattr(obj, name)
0231     except (AttributeError, TypeError):
0232         pass
0233     else:
0234         raise TestFailed, "shouldn't be able to del %s" % name
0235 
0236 def test_func_closure():
0237     a = 12
0238     def f(): print a
0239     c = f.func_closure
0240     verify(isinstance(c, tuple))
0241     verify(len(c) == 1)
0242     verify(c[0].__class__.__name__ == "cell") # don't have a type object handy
0243     cantset(f, "func_closure", c)
0244 
0245 def test_func_doc():
0246     def f(): pass
0247     verify(f.__doc__ is None)
0248     verify(f.func_doc is None)
0249     f.__doc__ = "hello"
0250     verify(f.__doc__ == "hello")
0251     verify(f.func_doc == "hello")
0252     del f.__doc__
0253     verify(f.__doc__ is None)
0254     verify(f.func_doc is None)
0255     f.func_doc = "world"
0256     verify(f.__doc__ == "world")
0257     verify(f.func_doc == "world")
0258     del f.func_doc
0259     verify(f.func_doc is None)
0260     verify(f.__doc__ is None)
0261 
0262 def test_func_globals():
0263     def f(): pass
0264     verify(f.func_globals is globals())
0265     cantset(f, "func_globals", globals())
0266 
0267 def test_func_name():
0268     def f(): pass
0269     verify(f.__name__ == "f")
0270     verify(f.func_name == "f")
0271     f.__name__ = "g"
0272     verify(f.__name__ == "g")
0273     verify(f.func_name == "g")
0274     f.func_name = "h"
0275     verify(f.__name__ == "h")
0276     verify(f.func_name == "h")
0277     cantset(f, "func_globals", 1)
0278     cantset(f, "__name__", 1)
0279 
0280 
0281 def test_func_code():
0282     a = b = 24
0283     def f(): pass
0284     def g(): print 12
0285     def f1(): print a
0286     def g1(): print b
0287     def f2(): print a, b
0288     verify(type(f.func_code) is types.CodeType)
0289     f.func_code = g.func_code
0290     cantset(f, "func_code", None)
0291     # can't change the number of free vars
0292     cantset(f,  "func_code", f1.func_code, exception=ValueError)
0293     cantset(f1, "func_code",  f.func_code, exception=ValueError)
0294     cantset(f1, "func_code", f2.func_code, exception=ValueError)
0295     f1.func_code = g1.func_code
0296 
0297 def test_func_defaults():
0298     def f(a, b): return (a, b)
0299     verify(f.func_defaults is None)
0300     f.func_defaults = (1, 2)
0301     verify(f.func_defaults == (1, 2))
0302     verify(f(10) == (10, 2))
0303     def g(a=1, b=2): return (a, b)
0304     verify(g.func_defaults == (1, 2))
0305     del g.func_defaults
0306     verify(g.func_defaults is None)
0307     try:
0308         g()
0309     except TypeError:
0310         pass
0311     else:
0312         raise TestFailed, "shouldn't be allowed to call g() w/o defaults"
0313 
0314 def test_func_dict():
0315     def f(): pass
0316     a = f.__dict__
0317     b = f.func_dict
0318     verify(a == {})
0319     verify(a is b)
0320     f.hello = 'world'
0321     verify(a == {'hello': 'world'})
0322     verify(f.func_dict is a is f.__dict__)
0323     f.func_dict = {}
0324     verify(not hasattr(f, "hello"))
0325     f.__dict__ = {'world': 'hello'}
0326     verify(f.world == "hello")
0327     verify(f.__dict__ is f.func_dict == {'world': 'hello'})
0328     cantset(f, "func_dict", None)
0329     cantset(f, "__dict__", None)
0330 
0331 def test_im_class():
0332     class C:
0333         def foo(self): pass
0334     verify(C.foo.im_class is C)
0335     verify(C().foo.im_class is C)
0336     cantset(C.foo, "im_class", C)
0337     cantset(C().foo, "im_class", C)
0338 
0339 def test_im_func():
0340     def foo(self): pass
0341     class C:
0342         pass
0343     C.foo = foo
0344     verify(C.foo.im_func is foo)
0345     verify(C().foo.im_func is foo)
0346     cantset(C.foo, "im_func", foo)
0347     cantset(C().foo, "im_func", foo)
0348 
0349 def test_im_self():
0350     class C:
0351         def foo(self): pass
0352     verify(C.foo.im_self is None)
0353     c = C()
0354     verify(c.foo.im_self is c)
0355     cantset(C.foo, "im_self", None)
0356     cantset(c.foo, "im_self", c)
0357 
0358 def test_im_dict():
0359     class C:
0360         def foo(self): pass
0361         foo.bar = 42
0362     verify(C.foo.__dict__ == {'bar': 42})
0363     verify(C().foo.__dict__ == {'bar': 42})
0364     cantset(C.foo, "__dict__", C.foo.__dict__)
0365     cantset(C().foo, "__dict__", C.foo.__dict__)
0366 
0367 def test_im_doc():
0368     class C:
0369         def foo(self): "hello"
0370     verify(C.foo.__doc__ == "hello")
0371     verify(C().foo.__doc__ == "hello")
0372     cantset(C.foo, "__doc__", "hello")
0373     cantset(C().foo, "__doc__", "hello")
0374 
0375 def test_im_name():
0376     class C:
0377         def foo(self): pass
0378     verify(C.foo.__name__ == "foo")
0379     verify(C().foo.__name__ == "foo")
0380     cantset(C.foo, "__name__", "foo")
0381     cantset(C().foo, "__name__", "foo")
0382 
0383 def testmore():
0384     test_func_closure()
0385     test_func_doc()
0386     test_func_globals()
0387     test_func_name()
0388     test_func_code()
0389     test_func_defaults()
0390     test_func_dict()
0391     # Tests for instance method attributes
0392     test_im_class()
0393     test_im_func()
0394     test_im_self()
0395     test_im_dict()
0396     test_im_doc()
0397     test_im_name()
0398 
0399 testmore()
0400 

Generated by PyXR 0.9.4
SourceForge.net Logo