Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #7. Make the Python Acquirer cooperatively call super, and test this. #8

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 3 additions & 1 deletion CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ Changelog
4.2.2 (unreleased)
------------------

- TBD
- Make the pure-Python Acquirer objects cooperatively use the
superclass ``__getattribute__`` method, like the C implementation.
See https://github.com/zopefoundation/Acquisition/issues/7.

4.2.1 (2015-04-23)
------------------
Expand Down
16 changes: 13 additions & 3 deletions src/Acquisition/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,21 @@ def _rebound_method(method, wrapper):
if isinstance(method, types.MethodType):
method = types.MethodType(method.im_func, wrapper, method.im_class)
return method
exec("""def _reraise(tp, value, tb=None):
raise tp, value, tb
""")
else: # pragma: no cover (python 2 is currently our reference)
def _rebound_method(method, wrapper):
"""Returns a version of the method with self bound to `wrapper`"""
if isinstance(method, types.MethodType):
method = types.MethodType(method.__func__, wrapper)
return method
def _reraise(tp, value, tb=None):
if value is None:
value = tp()
if value.__traceback__ is not tb:
raise value.with_traceback(tb)
raise value

###
# Wrapper object protocol, mostly ported from C directly
Expand Down Expand Up @@ -684,11 +693,12 @@ class _Acquirer(ExtensionClass.Base):

def __getattribute__(self, name):
try:
return ExtensionClass.Base.__getattribute__(self, name)
return super(_Acquirer,self).__getattribute__(name)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: space after comma.

except AttributeError:
# the doctests have very specific error message
# requirements
raise AttributeError(name)
# requirements (but at least we can preserve the traceback)
t, v, tb = sys.exc_info()
_reraise(AttributeError, AttributeError(name), tb)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Binding the traceback to a local variable has historically been a source of reference leaks. Maybe wrap it in a try:...finally; del tb for sanity / safety?


def __of__(self, context):
return type(self)._Wrapper(self, context)
Expand Down
25 changes: 25 additions & 0 deletions src/Acquisition/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -3124,6 +3124,30 @@ def test_w_unicode_attr_name(self):
found = self.acquire(self.a.b.c, AQ_PARENT)
self.assertTrue(found.aq_self is self.a.b.aq_self)

class TestCooperativeBase(unittest.TestCase):

def _make_acquirer(self, kind):
from ExtensionClass import Base

class ExtendsBase(Base):
def __getattribute__(self, name):
if name == 'magic':
return 42
return super(ExtendsBase,self).__getattribute__(name)

class Acquirer(kind, ExtendsBase):
pass

return Acquirer()

def _check___getattribute___is_cooperative(self, acquirer):
self.assertEqual(getattr(acquirer, 'magic'), 42)

def test_implicit___getattribute__is_cooperative(self):
self._check___getattribute___is_cooperative(self._make_acquirer(Acquisition.Implicit))

def test_explicit___getattribute__is_cooperative(self):
self._check___getattribute___is_cooperative(self._make_acquirer(Acquisition.Explicit))

class TestUnicode(unittest.TestCase):

Expand Down Expand Up @@ -3537,6 +3561,7 @@ def test_suite():
unittest.makeSuite(TestAcquire),
unittest.makeSuite(TestUnicode),
unittest.makeSuite(TestProxying),
unittest.makeSuite(TestCooperativeBase),
]

# This file is only available in a source checkout, skip it
Expand Down