69: Made Proxy.base a private attribute; updated unit tests

This commit is contained in:
Jason Gerard DeRose 2008-08-07 00:21:50 +00:00
parent 7335af8a9e
commit 19dbd57141
2 changed files with 14 additions and 12 deletions

View File

@ -124,38 +124,41 @@ class ReadOnly(object):
class Proxy(ReadOnly): class Proxy(ReadOnly):
__slots__ = ( __slots__ = (
'base', '__base',
'name',
'__target', '__target',
'__name_attr', '__name_attr',
'__public',
'name',
) )
def __init__(self, base, target, name_attr='name'): def __init__(self, base, target, name_attr='name'):
if not inspect.isclass(base): if not inspect.isclass(base):
raise TypeError('arg1 must be a class, got %r' % base) raise TypeError('arg1 must be a class, got %r' % base)
if not isinstance(target, base): if not isinstance(target, base):
raise ValueError('arg2 must be instance of arg1, got %r' % target) raise ValueError('arg2 must be instance of arg1, got %r' % target)
object.__setattr__(self, 'base', base) object.__setattr__(self, '_Proxy__base', base)
object.__setattr__(self, '_Proxy__target', target) object.__setattr__(self, '_Proxy__target', target)
object.__setattr__(self, '_Proxy__name_attr', name_attr) object.__setattr__(self, '_Proxy__name_attr', name_attr)
object.__setattr__(self, '_Proxy__public', base.public)
object.__setattr__(self, 'name', getattr(target, name_attr))
# Check base.public # Check __public
assert type(self.base.public) is frozenset assert type(self.__public) is frozenset
# Check name # Check name
object.__setattr__(self, 'name', getattr(target, name_attr))
check_identifier(self.name) check_identifier(self.name)
def __iter__(self): def __iter__(self):
for name in sorted(self.base.public): for name in sorted(self.__public):
yield name yield name
def __getitem__(self, key): def __getitem__(self, key):
if key in self.base.public: if key in self.__public:
return getattr(self.__target, key) return getattr(self.__target, key)
raise KeyError('no proxy attribute %r' % key) raise KeyError('no proxy attribute %r' % key)
def __getattr__(self, name): def __getattr__(self, name):
if name in self.base.public: if name in self.__public:
return getattr(self.__target, name) return getattr(self.__target, name)
raise AttributeError('no proxy attribute %r' % name) raise AttributeError('no proxy attribute %r' % name)
@ -163,12 +166,12 @@ class Proxy(ReadOnly):
return self['__call__'](*args, **kw) return self['__call__'](*args, **kw)
def _clone(self, name_attr): def _clone(self, name_attr):
return self.__class__(self.base, self.__target, name_attr) return self.__class__(self.__base, self.__target, name_attr)
def __repr__(self): def __repr__(self):
return '%s(%s, %r, %r)' % ( return '%s(%s, %r, %r)' % (
self.__class__.__name__, self.__class__.__name__,
self.base.__name__, self.__base.__name__,
self.__target, self.__target,
self.__name_attr, self.__name_attr,
) )

View File

@ -144,7 +144,6 @@ def test_Proxy():
# Test with correct arguments: # Test with correct arguments:
i = plugin() i = plugin()
p = cls(base, i) p = cls(base, i)
assert read_only(p, 'base') is base
assert read_only(p, 'name') == 'user_add' assert read_only(p, 'name') == 'user_add'
assert list(p) == sorted(base.public) assert list(p) == sorted(base.public)