Skip to content

Commit

Permalink
Improve unittest.
Browse files Browse the repository at this point in the history
Before:
It was two steps
1) Acquire the method (for example __iadd__).
   On a C-Level this triggers Base_getattro
2) Then call the return object.

However this is not exactly what CPython does on a statement like this:
a += 1

CPython looks first at the number protocol of that type.
If the number protocol has the slot defined it calls it, otherwise it
raises a TypeError.
https://docs.python.org/3.6/c-api/typeobj.html?highlight=pynumbermethods#c.PyNumberMethods
=> CPython omits Base_getattro at all.

Now: By using the operator module, the unittests behave exactly as
cpython on a statement like this:
a += 1
  • Loading branch information
stephan-hof committed Feb 4, 2017
1 parent 42f2749 commit 8b5d8a8
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions src/Acquisition/tests.py
Expand Up @@ -3382,7 +3382,7 @@ def _check_special_methods(self, base_class=Acquisition.Implicit):
# Check that special methods are proxied
# when called implicitly by the interpreter

def binary_acquired_func(self, other):
def binary_acquired_func(self, other, modulo=None):
return self.value

def unary_acquired_func(self):
Expand Down Expand Up @@ -3426,9 +3426,11 @@ class B(Acquisition.Implicit):
_found_at_least_one_div = False

for meth in self.__binary_numeric_methods__:
# called on the instance
self.assertEqual(base.value,
getattr(base.derived, meth)(-1))
op = getattr(operator, meth, None)
if op is not None:
# called on the instance
self.assertEqual(base.value, op(base.derived, -1))

# called on the type, as the interpreter does
# Note that the C version can only implement either __truediv__
# or __div__, not both
Expand Down

0 comments on commit 8b5d8a8

Please sign in to comment.