Commit 346c81fe authored by Stefan Behnel's avatar Stefan Behnel

Make sure that version dependent special methods are correctly and completely...

Make sure that version dependent special methods are correctly and completely excluded via preprocessor guards.
Previously, implementing "__div__" could fail in Py3 (if the code for adapting the Python wrapper was generated) or would at least generate C compiler warnings about unused "__div__" C functions.
parent 3748c3cd
......@@ -515,8 +515,8 @@ class BinopSlot(SyntheticSlot):
SyntheticSlot.__init__(
self, slot_name, [left_method, right_method], "0", is_binop=True, **kargs)
# MethodSlot causes special method registration.
self.left_slot = MethodSlot(signature, "", left_method)
self.right_slot = MethodSlot(signature, "", right_method)
self.left_slot = MethodSlot(signature, "", left_method, **kargs)
self.right_slot = MethodSlot(signature, "", right_method, **kargs)
class RichcmpSlot(MethodSlot):
......
cimport cython
import sys
IS_PYTHON2 = sys.version_info[0] == 2
__doc__ = ""
@cython.c_api_binop_methods(False)
@cython.cclass
class Base(object):
......@@ -57,6 +63,7 @@ class Base(object):
def __repr__(self):
return "%s()" % (self.__class__.__name__)
@cython.c_api_binop_methods(False)
@cython.cclass
class OverloadLeft(Base):
......@@ -128,6 +135,7 @@ class OverloadRight(Base):
else:
return NotImplemented
@cython.c_api_binop_methods(True)
@cython.cclass
class OverloadCApi(Base):
......@@ -167,4 +175,84 @@ class OverloadCApi(Base):
else:
return NotImplemented
if sys.version_info >= (3, 5):
__doc__ += """
>>> d = PyVersionDependent()
>>> d @ 2
9
>>> 2 @ d
99
>>> i = d
>>> i @= 2
>>> i
999
"""
@cython.c_api_binop_methods(False)
@cython.cclass
class PyVersionDependent:
"""
>>> d = PyVersionDependent()
>>> d / 2
5
>>> 2 / d
2
>>> d // 2
55
>>> 2 // d
22
>>> i = d
>>> i /= 2
>>> i
4
>>> i = d
>>> i //= 2
>>> i
44
"""
def __div__(self, other):
assert IS_PYTHON2
return 5
def __rdiv__(self, other):
assert IS_PYTHON2
return 2
def __idiv__(self, other):
assert IS_PYTHON2
return 4
def __truediv__(self, other):
assert not IS_PYTHON2
return 5
def __rtruediv__(self, other):
assert not IS_PYTHON2
return 2
def __itruediv__(self, other):
assert not IS_PYTHON2
return 4
def __floordiv__(self, other):
return 55
def __rfloordiv__(self, other):
return 22
def __ifloordiv__(self, other):
return 44
def __matmul__(self, other):
return 9
def __rmatmul__(self, other):
return 99
def __imatmul__(self, other):
return 999
# TODO: Test a class that only defines the `__r...__()` methods.
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment