1 from test
.test_support
import verbose
, TestFailed
, verify
12 # __module__ is a special attribute
13 verify(b
.__module
__ == __name__
)
14 verify(verify
.__module
__ == "test.test_support")
16 # setting attributes on functions
19 except AttributeError: pass
20 else: raise TestFailed
, 'expected AttributeError'
23 raise TestFailed
, 'expected unassigned func.__dict__ to be {}'
27 raise TestFailed
, 'function attribute not set to expected value'
29 docstring
= 'its docstring'
31 if b
.__doc
__ <> docstring
:
32 raise TestFailed
, 'problem with setting __doc__ attribute'
34 if 'publish' not in dir(b
):
35 raise TestFailed
, 'attribute not in dir()'
39 except TypeError: pass
40 else: raise TestFailed
, 'del func.__dict__ expected TypeError'
45 except TypeError: pass
46 else: raise TestFailed
, 'func.__dict__ = None expected TypeError'
48 d
= {'hello': 'world'}
50 if b
.func_dict
is not d
:
51 raise TestFailed
, 'func.__dict__ assignment to dictionary failed'
52 if b
.hello
<> 'world':
53 raise TestFailed
, 'attribute after func.__dict__ assignment failed'
60 except AttributeError: pass
61 else: raise TestFailed
, 'expected AttributeError'
65 except AttributeError: pass
66 else: raise TestFailed
, 'expected AttributeError'
68 # In Python 2.1 beta 1, we disallowed setting attributes on unbound methods
69 # (it was already disallowed on bound methods). See the PEP for details.
72 except (AttributeError, TypeError): pass
73 else: raise TestFailed
, 'expected AttributeError or TypeError'
75 # But setting it explicitly on the underlying function object is okay.
76 F
.a
.im_func
.publish
= 1
79 raise TestFailed
, 'unbound method attribute not set to expected value'
82 raise TestFailed
, 'bound method attribute access did not work'
85 raise TestFailed
, 'bound method attribute access did not work'
87 if 'publish' not in dir(F
.a
):
88 raise TestFailed
, 'attribute not in dir()'
92 except (AttributeError, TypeError): pass
93 else: raise TestFailed
, 'expected AttributeError or TypeError'
95 # See the comment above about the change in semantics for Python 2.1b1
98 except (AttributeError, TypeError): pass
99 else: raise TestFailed
, 'expected AttributeError or TypeError'
101 F
.a
.im_func
.myclass
= F
108 if f1
.a
.myclass
is not f2
.a
.myclass
or \
109 f1
.a
.myclass
is not F
.a
.myclass
:
110 raise TestFailed
, 'attributes were not the same'
112 # try setting __dict__
114 F
.a
.__dict
__ = (1, 2, 3)
115 except (AttributeError, TypeError): pass
116 else: raise TestFailed
, 'expected TypeError or AttributeError'
118 F
.a
.im_func
.__dict
__ = {'one': 11, 'two': 22, 'three': 33}
121 raise TestFailed
, 'setting __dict__'
123 from UserDict
import UserDict
124 d
= UserDict({'four': 44, 'five': 55})
128 except (AttributeError, TypeError): pass
129 else: raise TestFailed
131 if f2
.a
.one
<> f1
.a
.one
<> F
.a
.one
<> 11:
134 # im_func may not be a Python method!
136 F
.id = new
.instancemethod(id, None, F
)
139 if eff
.id() <> id(eff
):
144 except AttributeError: pass
145 else: raise TestFailed
149 except (AttributeError, TypeError): pass
150 else: raise TestFailed
154 except AttributeError: pass
155 else: raise TestFailed
159 except AttributeError: pass
160 else: raise TestFailed
164 except (AttributeError, TypeError): pass
165 else: raise TestFailed
169 except AttributeError: pass
170 else: raise TestFailed
172 # Regression test for a crash in pre-2.1a1
178 except TypeError: pass
179 else: raise TestFailed
182 del another
.func_dict
183 except TypeError: pass
184 else: raise TestFailed
187 another
.func_dict
= None
188 except TypeError: pass
189 else: raise TestFailed
193 except AttributeError: pass
194 else: raise TestFailed
196 # This isn't specifically related to function attributes, but it does test a
197 # core dump regression in funcobject.c
198 del another
.func_defaults
215 foo
.func_code
= temp
.func_code
219 # Test all predefined function attributes systematically
221 def cantset(obj
, name
, value
, exception
=(AttributeError, TypeError)):
222 verify(hasattr(obj
, name
)) # Otherwise it's probably a typo
224 setattr(obj
, name
, value
)
228 raise TestFailed
, "shouldn't be able to set %s to %r" % (name
, value
)
231 except (AttributeError, TypeError):
234 raise TestFailed
, "shouldn't be able to del %s" % name
236 def test_func_closure():
240 verify(isinstance(c
, tuple))
242 verify(c
[0].__class
__.__name
__ == "cell") # don't have a type object handy
243 cantset(f
, "func_closure", c
)
247 verify(f
.__doc
__ is None)
248 verify(f
.func_doc
is None)
250 verify(f
.__doc
__ == "hello")
251 verify(f
.func_doc
== "hello")
253 verify(f
.__doc
__ is None)
254 verify(f
.func_doc
is None)
256 verify(f
.__doc
__ == "world")
257 verify(f
.func_doc
== "world")
259 verify(f
.func_doc
is None)
260 verify(f
.__doc
__ is None)
262 def test_func_globals():
264 verify(f
.func_globals
is globals())
265 cantset(f
, "func_globals", globals())
267 def test_func_name():
269 verify(f
.__name
__ == "f")
270 verify(f
.func_name
== "f")
272 verify(f
.__name
__ == "g")
273 verify(f
.func_name
== "g")
275 verify(f
.__name
__ == "h")
276 verify(f
.func_name
== "h")
277 cantset(f
, "func_globals", 1)
278 cantset(f
, "__name__", 1)
279 # test that you can access func.__name__ in restricted mode
280 s
= """def f(): pass\nf.__name__"""
281 exec s
in {'__builtins__':{}}
284 def test_func_code():
291 verify(type(f
.func_code
) is types
.CodeType
)
292 f
.func_code
= g
.func_code
293 cantset(f
, "func_code", None)
294 # can't change the number of free vars
295 cantset(f
, "func_code", f1
.func_code
, exception
=ValueError)
296 cantset(f1
, "func_code", f
.func_code
, exception
=ValueError)
297 cantset(f1
, "func_code", f2
.func_code
, exception
=ValueError)
298 f1
.func_code
= g1
.func_code
300 def test_func_defaults():
301 def f(a
, b
): return (a
, b
)
302 verify(f
.func_defaults
is None)
303 f
.func_defaults
= (1, 2)
304 verify(f
.func_defaults
== (1, 2))
305 verify(f(10) == (10, 2))
306 def g(a
=1, b
=2): return (a
, b
)
307 verify(g
.func_defaults
== (1, 2))
309 verify(g
.func_defaults
is None)
315 raise TestFailed
, "shouldn't be allowed to call g() w/o defaults"
317 def test_func_dict():
324 verify(a
== {'hello': 'world'})
325 verify(f
.func_dict
is a
is f
.__dict
__)
327 verify(not hasattr(f
, "hello"))
328 f
.__dict
__ = {'world': 'hello'}
329 verify(f
.world
== "hello")
330 verify(f
.__dict
__ is f
.func_dict
== {'world': 'hello'})
331 cantset(f
, "func_dict", None)
332 cantset(f
, "__dict__", None)
337 verify(C
.foo
.im_class
is C
)
338 verify(C().foo
.im_class
is C
)
339 cantset(C
.foo
, "im_class", C
)
340 cantset(C().foo
, "im_class", C
)
347 verify(C
.foo
.im_func
is foo
)
348 verify(C().foo
.im_func
is foo
)
349 cantset(C
.foo
, "im_func", foo
)
350 cantset(C().foo
, "im_func", foo
)
355 verify(C
.foo
.im_self
is None)
357 verify(c
.foo
.im_self
is c
)
358 cantset(C
.foo
, "im_self", None)
359 cantset(c
.foo
, "im_self", c
)
365 verify(C
.foo
.__dict
__ == {'bar': 42})
366 verify(C().foo
.__dict
__ == {'bar': 42})
367 cantset(C
.foo
, "__dict__", C
.foo
.__dict
__)
368 cantset(C().foo
, "__dict__", C
.foo
.__dict
__)
372 def foo(self
): "hello"
373 verify(C
.foo
.__doc
__ == "hello")
374 verify(C().foo
.__doc
__ == "hello")
375 cantset(C
.foo
, "__doc__", "hello")
376 cantset(C().foo
, "__doc__", "hello")
381 verify(C
.foo
.__name
__ == "foo")
382 verify(C().foo
.__name
__ == "foo")
383 cantset(C
.foo
, "__name__", "foo")
384 cantset(C().foo
, "__name__", "foo")
394 # Tests for instance method attributes