Remove trailing whitespace.
[cython.git] / tests / run / special_methods_T561_py2.pyx
1 # This file tests the behavior of special methods under Python 2
2 # after #561.  (Only methods whose behavior differs between Python 2 and 3
3 # are tested here; see special_methods_T561.pyx for the rest of the tests.)
4
5 __doc__ = u"""
6     >>> vs0 = VerySpecial(0)
7     VS __init__ 0
8     >>> vs1 = VerySpecial(1)
9     VS __init__ 1
10     >>> # Python 3 does not use __cmp__.
11     >>> vs0_cmp = vs0.__cmp__
12     >>> vs0_cmp(vs1)
13     VS __cmp__ 0 1
14     0
15     >>> # Python 3 does not use __div__ or __idiv__.
16     >>> vs0_div = vs0.__div__
17     >>> vs0_div(vs1)
18     VS __div__ 0 1
19     >>> vs0_idiv = vs0.__idiv__
20     >>> vs0_idiv(vs1)
21     VS __idiv__ 0 /= 1
22     >>> vs0_rdiv = vs0.__rdiv__
23     >>> vs0_rdiv(vs1)
24     VS __div__ 1 0
25     >>> # Python 3 does not use __oct__ or __hex__.
26     >>> vs0_oct = vs0.__oct__
27     >>> vs0_oct()
28     VS __oct__ 0
29     >>> vs0_hex = vs0.__hex__
30     >>> vs0_hex()
31     VS __hex__ 0
32     >>> # Python 3 does not use __nonzero__; if you define a __nonzero__
33     >>> # method, Cython for Python 3 would give you a __bool__ method
34     >>> # instead.
35     >>> vs0_nonzero = vs0.__nonzero__
36     >>> vs0_nonzero()
37     VS __nonzero__ 0
38     False
39     >>> # If you define __next__, you get both __next__ and next (this behavior
40     >>> # is unchanged by T561, but only happens in Python 2)
41     >>> vs0_next = vs0.__next__
42     >>> vs0_next()
43     VS next/__next__ 0
44     >>> vs0_next2 = vs0.next
45     >>> vs0_next2()
46     VS next/__next__ 0
47     >>> # Cython supports getslice only for Python 2.
48     >>> vs0_getslice = vs0.__getslice__
49     >>> vs0_getslice(13, 42)
50     VS __getslice__ 0 13 42
51     >>> # Cython supports setslice and delslice only for Python 2.
52     >>> # If you define either setslice or delslice, you get wrapper objects
53     >>> # for both methods.  (This behavior is unchanged by #561.)
54     >>> ss_setslice = SetSlice().__setslice__
55     >>> ss_setslice(13, 42, 'foo')
56     SetSlice setslice 13 42 'foo'
57     >>> ss_delslice = SetSlice().__delslice__
58     >>> ss_delslice(13, 42)
59     Traceback (most recent call last):
60     ...
61     NotImplementedError: 2-element slice deletion not supported by special_methods_T561_py2.SetSlice
62     >>> ds_setslice = DelSlice().__setslice__
63     >>> ds_setslice(13, 42, 'foo')
64     Traceback (most recent call last):
65     ...
66     NotImplementedError: 2-element slice assignment not supported by special_methods_T561_py2.DelSlice
67     >>> ds_delslice = DelSlice().__delslice__
68     >>> ds_delslice(13, 42)
69     DelSlice delslice 13 42
70     >>> sds_setslice = SetDelSlice().__setslice__
71     >>> sds_setslice(13, 42, 'foo')
72     SetDelSlice setslice 13 42 'foo'
73     >>> sds_delslice = SetDelSlice().__delslice__
74     >>> sds_delslice(13, 42)
75     SetDelSlice delslice 13 42
76     >>> # Python 3 does not use __long__.
77     >>> Ll = Long().__long__
78     >>> Ll()
79     Long __long__
80 """
81
82 cdef class VerySpecial:
83     cdef readonly int value
84
85     def __init__(self, v):
86         self.value = v
87         print "VS __init__ %d" % self.value
88
89     def __getslice__(self, a, b):
90         print "VS __getslice__ %d %d %d" % (self.value, a, b)
91
92     def __next__(self):
93         print "VS next/__next__ %d" % self.value
94
95     def __nonzero__(self):
96         print "VS __nonzero__ %d" % self.value
97
98     def __oct__(self):
99         print "VS __oct__ %d" % self.value
100
101     def __hex__(self):
102         print "VS __hex__ %d" % self.value
103
104     def __cmp__(self, other):
105         print "VS __cmp__ %d %d" % (self.value, other.value)
106
107     def __div__(self, other):
108         print "VS __div__ %d %d" % (self.value, other.value)
109
110     def __idiv__(self, other):
111         print "VS __idiv__ %d /= %d" % (self.value, other.value)
112
113 cdef class SetSlice:
114     def __setslice__(self, a, b, value):
115         print "SetSlice setslice %d %d %r" % (a, b, value)
116
117 cdef class DelSlice:
118     def __delslice__(self, a, b):
119         print "DelSlice delslice %d %d" % (a, b)
120
121 cdef class SetDelSlice:
122     def __setslice__(self, a, b, value):
123         print "SetDelSlice setslice %d %d %r" % (a, b, value)
124
125     def __delslice__(self, a, b):
126         print "SetDelSlice delslice %d %d" % (a, b)
127
128 cdef class Long:
129     def __long__(self):
130         print "Long __long__"