Blame tests/run/hasattr.pyx
|
Packit |
562c7a |
class Foo:
|
|
Packit |
562c7a |
@property
|
|
Packit |
562c7a |
def foo(self):
|
|
Packit |
562c7a |
return None
|
|
Packit |
562c7a |
@property
|
|
Packit |
562c7a |
def bar(self):
|
|
Packit |
562c7a |
raise AttributeError
|
|
Packit |
562c7a |
@property
|
|
Packit |
562c7a |
def baz(self):
|
|
Packit |
562c7a |
return int(1)/int(0)
|
|
Packit |
562c7a |
|
|
Packit |
562c7a |
|
|
Packit |
562c7a |
unicode_foo = u"foo"
|
|
Packit |
562c7a |
|
|
Packit |
562c7a |
|
|
Packit |
562c7a |
def wrap_hasattr(obj, name):
|
|
Packit |
562c7a |
"""
|
|
Packit |
562c7a |
>>> wrap_hasattr(None, "abc")
|
|
Packit |
562c7a |
False
|
|
Packit |
562c7a |
>>> wrap_hasattr(list, "append")
|
|
Packit |
562c7a |
True
|
|
Packit |
562c7a |
>>> wrap_hasattr(Foo(), "foo")
|
|
Packit |
562c7a |
True
|
|
Packit |
562c7a |
>>> wrap_hasattr(Foo(), unicode_foo)
|
|
Packit |
562c7a |
True
|
|
Packit |
562c7a |
>>> wrap_hasattr(Foo(), "spam")
|
|
Packit |
562c7a |
False
|
|
Packit |
562c7a |
>>> wrap_hasattr(Foo(), "bar")
|
|
Packit |
562c7a |
False
|
|
Packit |
562c7a |
>>> Foo().baz #doctest: +ELLIPSIS
|
|
Packit |
562c7a |
Traceback (most recent call last):
|
|
Packit |
562c7a |
...
|
|
Packit |
562c7a |
ZeroDivisionError: ...
|
|
Packit |
562c7a |
>>> wrap_hasattr(Foo(), "baz")
|
|
Packit |
562c7a |
False
|
|
Packit |
562c7a |
>>> hasattr(Foo(), None) #doctest: +ELLIPSIS
|
|
Packit |
562c7a |
Traceback (most recent call last):
|
|
Packit |
562c7a |
...
|
|
Packit |
562c7a |
TypeError: hasattr(): attribute name must be string
|
|
Packit |
562c7a |
"""
|
|
Packit |
562c7a |
return hasattr(obj, name)
|