1# ticket: 561
2# tag: py3
3# This file tests the behavior of special methods under Python 3
4# after #561.  (Only methods whose behavior differs between Python 2 and 3
5# are tested here; see special_methods_T561.pyx for the rest of the tests.)
6
7__doc__ = u"""
8    >>> vs0 = VerySpecial(0)
9    VS __init__ 0
10    >>> # Python 3 does not use __cmp__, so any provided __cmp__ method is
11    >>> # discarded under Python 3.
12    >>> vs0_cmp = vs0.__cmp__
13    Traceback (most recent call last):
14    ...
15    AttributeError: 'special_methods_T561_py3.VerySpecial' object has no attribute '__cmp__'
16    >>> # Python 3 does not use __div__ or __idiv__, so these methods are
17    >>> # discarded under Python 3.
18    >>> vs0_div = vs0.__div__
19    Traceback (most recent call last):
20    ...
21    AttributeError: 'special_methods_T561_py3.VerySpecial' object has no attribute '__div__'
22    >>> vs0_rdiv = vs0.__rdiv__
23    Traceback (most recent call last):
24    ...
25    AttributeError: 'special_methods_T561_py3.VerySpecial' object has no attribute '__rdiv__'
26    >>> vs0_idiv = vs0.__idiv__
27    Traceback (most recent call last):
28    ...
29    AttributeError: 'special_methods_T561_py3.VerySpecial' object has no attribute '__idiv__'
30    >>> # Python 3 does not use __oct__ or __hex__, so these methods are
31    >>> # discarded under Python 3.
32    >>> vs0_oct = vs0.__oct__
33    Traceback (most recent call last):
34    ...
35    AttributeError: 'special_methods_T561_py3.VerySpecial' object has no attribute '__oct__'
36    >>> vs0_hex = vs0.__hex__
37    Traceback (most recent call last):
38    ...
39    AttributeError: 'special_methods_T561_py3.VerySpecial' object has no attribute '__hex__'
40    >>> # Python 3 does not use __long__; if you define __long__ but not
41    >>> # __int__, the __long__ definition will be used for __int__.
42    >>> Ll = Long().__long__
43    Traceback (most recent call last):
44    ...
45    AttributeError: 'special_methods_T561_py3.Long' object has no attribute '__long__'
46    >>> Li = Long().__int__
47    >>> Li()
48    Long __long__
49    >>> # As of Python 3, defining __nonzero__ gives you a __bool__ method
50    >>> # instead.
51    >>> vs0_bool = vs0.__bool__
52    >>> vs0_bool()
53    VS __nonzero__ 0
54    False
55"""
56
57cdef class VerySpecial:
58    cdef readonly int value
59
60    def __init__(self, v):
61        self.value = v
62        print "VS __init__ %d" % self.value
63
64    def __nonzero__(self):
65        print "VS __nonzero__ %d" % self.value
66
67    def __oct__(self):
68        print "VS __oct__ %d" % self.value
69
70    def __hex__(self):
71        print "VS __hex__ %d" % self.value
72
73    def __cmp__(self, other):
74        print "VS __cmp__ %d %d" % (self.value, other.value)
75
76    def __div__(self, other):
77        print "VS __div__ %d %d" % (self.value, other.value)
78
79    def __idiv__(self, other):
80        print "VS __idiv__ %d /= %d" % (self.value, other.value)
81
82cdef class Long:
83    def __long__(self):
84        print "Long __long__"
85