Commit cc52bac9 authored by Stefan Behnel's avatar Stefan Behnel

clean up and extend test to assert call dependencies between __getattr__() and __getattribute__()

parent df19f619
__doc__ = u""" # mode: run
__getattribute__ and __getattr__ special methods for a single class.
""" # __getattribute__ and __getattr__ special methods for a single class.
cdef class just_getattribute: cdef class just_getattribute:
""" """
>>> a = just_getattribute() >>> a = just_getattribute()
>>> a.called
1
>>> a.called
2
>>> a.bar >>> a.bar
'bar' 'bar'
>>> a.called
4
>>> a.invalid >>> a.invalid
Traceback (most recent call last): Traceback (most recent call last):
AttributeError AttributeError
>>> a.called
6
""" """
cdef readonly int called
def __getattribute__(self,n): def __getattribute__(self,n):
self.called += 1
if n == 'bar': if n == 'bar':
return n return n
elif n == 'called':
return self.called
else: else:
raise AttributeError raise AttributeError
cdef class just_getattr: cdef class just_getattr:
""" """
>>> a = just_getattr() >>> a = just_getattr()
>>> a.called
0
>>> a.called
0
>>> a.foo >>> a.foo
10 10
>>> a.called
0
>>> a.bar >>> a.bar
'bar' 'bar'
>>> a.called
1
>>> a.invalid >>> a.invalid
Traceback (most recent call last): Traceback (most recent call last):
AttributeError AttributeError
>>> a.called
2
""" """
cdef readonly int called
cdef readonly int foo cdef readonly int foo
def __init__(self): def __init__(self):
self.foo = 10 self.foo = 10
def __getattr__(self,n): def __getattr__(self,n):
self.called += 1
if n == 'bar': if n == 'bar':
return n return n
else: else:
raise AttributeError raise AttributeError
cdef class both: cdef class both:
""" """
>>> a = both() >>> a = both()
>>> (a.called_getattr, a.called_getattribute)
(0, 2)
>>> a.foo >>> a.foo
10 10
>>> (a.called_getattr, a.called_getattribute)
(0, 5)
>>> a.bar >>> a.bar
'bar' 'bar'
>>> (a.called_getattr, a.called_getattribute)
(1, 8)
>>> a.invalid >>> a.invalid
Traceback (most recent call last): Traceback (most recent call last):
AttributeError AttributeError
>>> (a.called_getattr, a.called_getattribute)
(2, 11)
""" """
cdef readonly int called_getattribute
cdef readonly int called_getattr
cdef readonly int foo cdef readonly int foo
def __init__(self): def __init__(self):
self.foo = 10 self.foo = 10
def __getattribute__(self,n): def __getattribute__(self,n):
self.called_getattribute += 1
if n == 'foo': if n == 'foo':
return self.foo return self.foo
elif n == 'called_getattribute':
return self.called_getattribute
elif n == 'called_getattr':
return self.called_getattr
else: else:
raise AttributeError raise AttributeError
def __getattr__(self,n): def __getattr__(self,n):
self.called_getattr += 1
if n == 'bar': if n == 'bar':
return n return n
else: else:
......
This diff is collapsed.
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment