from test
.test_support
import verbose
, TestFailed
, verify
# __module__ is a special attribute
verify(b
.__module
__ == __name__
)
verify(verify
.__module
__ == "test.test_support")
# setting attributes on functions
except AttributeError: pass
else: raise TestFailed
, 'expected AttributeError'
raise TestFailed
, 'expected unassigned func.__dict__ to be {}'
raise TestFailed
, 'function attribute not set to expected value'
docstring
= 'its docstring'
if b
.__doc
__ <> docstring
:
raise TestFailed
, 'problem with setting __doc__ attribute'
if 'publish' not in dir(b
):
raise TestFailed
, 'attribute not in dir()'
else: raise TestFailed
, 'del func.__dict__ expected TypeError'
else: raise TestFailed
, 'func.__dict__ = None expected TypeError'
raise TestFailed
, 'func.__dict__ assignment to dictionary failed'
raise TestFailed
, 'attribute after func.__dict__ assignment failed'
except AttributeError: pass
else: raise TestFailed
, 'expected AttributeError'
except AttributeError: pass
else: raise TestFailed
, 'expected AttributeError'
# In Python 2.1 beta 1, we disallowed setting attributes on unbound methods
# (it was already disallowed on bound methods). See the PEP for details.
except (AttributeError, TypeError): pass
else: raise TestFailed
, 'expected AttributeError or TypeError'
# But setting it explicitly on the underlying function object is okay.
raise TestFailed
, 'unbound method attribute not set to expected value'
raise TestFailed
, 'bound method attribute access did not work'
raise TestFailed
, 'bound method attribute access did not work'
if 'publish' not in dir(F
.a
):
raise TestFailed
, 'attribute not in dir()'
except (AttributeError, TypeError): pass
else: raise TestFailed
, 'expected AttributeError or TypeError'
# See the comment above about the change in semantics for Python 2.1b1
except (AttributeError, TypeError): pass
else: raise TestFailed
, 'expected AttributeError or TypeError'
if f1
.a
.myclass
is not f2
.a
.myclass
or \
f1
.a
.myclass
is not F
.a
.myclass
:
raise TestFailed
, 'attributes were not the same'
except (AttributeError, TypeError): pass
else: raise TestFailed
, 'expected TypeError or AttributeError'
F
.a
.im_func
.__dict
__ = {'one': 11, 'two': 22, 'three': 33}
raise TestFailed
, 'setting __dict__'
from UserDict
import UserDict
d
= UserDict({'four': 44, 'five': 55})
except (AttributeError, TypeError): pass
if f2
.a
.one
<> f1
.a
.one
<> F
.a
.one
<> 11:
# im_func may not be a Python method!
F
.id = new
.instancemethod(id, None, F
)
except AttributeError: pass
except (AttributeError, TypeError): pass
except AttributeError: pass
except AttributeError: pass
except (AttributeError, TypeError): pass
except AttributeError: pass
# Regression test for a crash in pre-2.1a1
except AttributeError: pass
# This isn't specifically related to function attributes, but it does test a
# core dump regression in funcobject.c
del another
.func_defaults
foo
.func_code
= temp
.func_code
# Test all predefined function attributes systematically
def cantset(obj
, name
, value
, exception
=(AttributeError, TypeError)):
verify(hasattr(obj
, name
)) # Otherwise it's probably a typo
setattr(obj
, name
, value
)
raise TestFailed
, "shouldn't be able to set %s to %r" % (name
, value
)
except (AttributeError, TypeError):
raise TestFailed
, "shouldn't be able to del %s" % name
verify(isinstance(c
, tuple))
verify(c
[0].__class
__.__name
__ == "cell") # don't have a type object handy
cantset(f
, "func_closure", c
)
verify(f
.__doc
__ is None)
verify(f
.func_doc
is None)
verify(f
.__doc
__ == "hello")
verify(f
.func_doc
== "hello")
verify(f
.__doc
__ is None)
verify(f
.func_doc
is None)
verify(f
.__doc
__ == "world")
verify(f
.func_doc
== "world")
verify(f
.func_doc
is None)
verify(f
.__doc
__ is None)
verify(f
.func_globals
is globals())
cantset(f
, "func_globals", globals())
verify(f
.__name
__ == "f")
verify(f
.func_name
== "f")
verify(f
.__name
__ == "g")
verify(f
.func_name
== "g")
verify(f
.__name
__ == "h")
verify(f
.func_name
== "h")
cantset(f
, "func_globals", 1)
cantset(f
, "__name__", 1)
# test that you can access func.__name__ in restricted mode
s
= """def f(): pass\nf.__name__"""
exec s
in {'__builtins__':{}}
verify(type(f
.func_code
) is types
.CodeType
)
f
.func_code
= g
.func_code
cantset(f
, "func_code", None)
# can't change the number of free vars
cantset(f
, "func_code", f1
.func_code
, exception
=ValueError)
cantset(f1
, "func_code", f
.func_code
, exception
=ValueError)
cantset(f1
, "func_code", f2
.func_code
, exception
=ValueError)
f1
.func_code
= g1
.func_code
def test_func_defaults():
def f(a
, b
): return (a
, b
)
verify(f
.func_defaults
is None)
verify(f
.func_defaults
== (1, 2))
def g(a
=1, b
=2): return (a
, b
)
verify(g
.func_defaults
== (1, 2))
verify(g
.func_defaults
is None)
raise TestFailed
, "shouldn't be allowed to call g() w/o defaults"
verify(a
== {'hello': 'world'})
verify(f
.func_dict
is a
is f
.__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)
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
)
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
)
verify(C
.foo
.im_self
is None)
verify(c
.foo
.im_self
is c
)
cantset(C
.foo
, "im_self", None)
cantset(c
.foo
, "im_self", c
)
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
__)
verify(C
.foo
.__doc
__ == "hello")
verify(C().foo
.__doc
__ == "hello")
cantset(C
.foo
, "__doc__", "hello")
cantset(C().foo
, "__doc__", "hello")
verify(C
.foo
.__name
__ == "foo")
verify(C().foo
.__name
__ == "foo")
cantset(C
.foo
, "__name__", "foo")
cantset(C().foo
, "__name__", "foo")
# Tests for instance method attributes