Skip to content

Commit

Permalink
Fix Implementation to use correct C function and add test.
Browse files Browse the repository at this point in the history
`Wrapper_str` is needed for Python 2 as it does not have a `PyBytes_FromObject`.
  • Loading branch information
Michael Howitz committed Apr 17, 2019
1 parent 90ff0f0 commit d522d16
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 3 deletions.
4 changes: 4 additions & 0 deletions src/Acquisition/_Acquisition.c
Original file line number Diff line number Diff line change
Expand Up @@ -982,7 +982,11 @@ Wrapper_bytes(Wrapper *self)
return r;
} else {
PyErr_Clear();
#ifdef PY3K
return PyBytes_FromObject(self->obj);
#else
return Wrapper_str(self);
#endif
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/Acquisition/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def _apply_filter(predicate, inst, name, result, extra, orig):
return predicate(orig, inst, name, result, extra)


if sys.version_info < (3,):
if sys.version_info < (3,): # pragma: PY2
import copy_reg

def _rebound_method(method, wrapper):
Expand Down Expand Up @@ -570,7 +570,7 @@ def __bytes__(self):
aq_self = self._obj
try:
return _rebound_method(aq_self.__bytes__, self)()
except (AttributeError, TypeError): # pragma: no cover (Only Py3)
except (AttributeError, TypeError): # pragma: PY3
return bytes(aq_self)

__binary_special_methods__ = [
Expand Down
13 changes: 12 additions & 1 deletion src/Acquisition/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -2450,7 +2450,7 @@ class Impl(Implicit):
self.assertIs(type(child.__dict__['child2']), Impl)

@unittest.skipIf(PY2, 'Python 2 has no explicit bytes support')
def test__bytes__is_correcty_wrapped(self):
def test__bytes__is_correcty_wrapped(self): # pragma: PY3
class A(Implicit):

def __bytes__(self):
Expand All @@ -2461,6 +2461,17 @@ def __bytes__(self):
wrapper = Acquisition.ImplicitAcquisitionWrapper(a.b, a)
self.assertEqual(b'my bytes', bytes(wrapper))

@unittest.skipIf(PY2, 'Python 2 has no explicit bytes support')
def test_AttributeError_if_object_has_no__bytes__(self): # pragma: PY3
class A(Implicit):
pass

a = A()
a.b = A()
wrapper = Acquisition.ImplicitAcquisitionWrapper(a.b, a)
with self.assertRaises(TypeError):
bytes(wrapper)


class TestOf(unittest.TestCase):

Expand Down

0 comments on commit d522d16

Please sign in to comment.