Skip to content

Commit

Permalink
Fix getting methods as attributes.
Browse files Browse the repository at this point in the history
  • Loading branch information
jamadden committed May 7, 2015
1 parent a193275 commit 9e244f2
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/zope/proxy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,22 +117,26 @@ def __getattribute__(self, name):
# __class__ is special cased in the C implementation
return wrapped.__class__

if name in ('__reduce__', '__reduce_ex__'):
# These things we specifically override and no one
# can stop us, not even a subclass
return object.__getattribute__(self, name)

# First, look for descriptors in this object's type
type_self = type(self)
descriptor = _WrapperType_Lookup(type_self, name)
if descriptor is _MARKER:
# Nothing in the class, go straight to the wrapped object
return getattr(wrapped, name)


if hasattr(descriptor, '__get__'):
if not hasattr(descriptor, '__set__'):
# Non-data-descriptor: call through to the wrapped object
# to see if it's there
try:
return getattr(wrapped, name)
except AttributeError:
raise
pass
# Data-descriptor on this type. Call it
return descriptor.__get__(self, type_self)
return descriptor
Expand Down
12 changes: 12 additions & 0 deletions src/zope/proxy/tests/test_proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -642,6 +642,18 @@ class Proxy(self._getTargetClass()):
self.assertRaises(AttributeError, setattr, proxy, 'attr', 42)
self.assertEqual(proxy.attr, "constant value")

def test_method_in_proxy_subclass(self):
class Proxy(self._getTargetClass()):
def __getitem__(self, k):
return k

proxy = Proxy(object())
# Both when called by the interpreter, which bypasses
# __getattribute__
self.assertEquals(proxy[42], 42)
# And when asked for as an attribute
self.assertNotEqual(getattr(proxy, '__getitem__'), self)

def test_string_to_int(self):
# XXX Implementation difference: This works in the
# Pure-Python version, but fails in CPython.
Expand Down

0 comments on commit 9e244f2

Please sign in to comment.