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.)
6 >>> vs0 = VerySpecial(0)
8 >>> vs1 = VerySpecial(1)
10 >>> # Python 3 does not use __cmp__.
11 >>> vs0_cmp = vs0.__cmp__
15 >>> # Python 3 does not use __div__ or __idiv__.
16 >>> vs0_div = vs0.__div__
19 >>> vs0_idiv = vs0.__idiv__
22 >>> vs0_rdiv = vs0.__rdiv__
25 >>> # Python 3 does not use __oct__ or __hex__.
26 >>> vs0_oct = vs0.__oct__
29 >>> vs0_hex = vs0.__hex__
32 >>> # Python 3 does not use __nonzero__; if you define a __nonzero__
33 >>> # method, Cython for Python 3 would give you a __bool__ method
35 >>> vs0_nonzero = vs0.__nonzero__
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__
44 >>> vs0_next2 = vs0.next
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):
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):
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__
82 cdef class VerySpecial:
83 cdef readonly int value
85 def __init__(self, v):
87 print "VS __init__ %d" % self.value
89 def __getslice__(self, a, b):
90 print "VS __getslice__ %d %d %d" % (self.value, a, b)
93 print "VS next/__next__ %d" % self.value
95 def __nonzero__(self):
96 print "VS __nonzero__ %d" % self.value
99 print "VS __oct__ %d" % self.value
102 print "VS __hex__ %d" % self.value
104 def __cmp__(self, other):
105 print "VS __cmp__ %d %d" % (self.value, other.value)
107 def __div__(self, other):
108 print "VS __div__ %d %d" % (self.value, other.value)
110 def __idiv__(self, other):
111 print "VS __idiv__ %d /= %d" % (self.value, other.value)
114 def __setslice__(self, a, b, value):
115 print "SetSlice setslice %d %d %r" % (a, b, value)
118 def __delslice__(self, a, b):
119 print "DelSlice delslice %d %d" % (a, b)
121 cdef class SetDelSlice:
122 def __setslice__(self, a, b, value):
123 print "SetDelSlice setslice %d %d %r" % (a, b, value)
125 def __delslice__(self, a, b):
126 print "SetDelSlice delslice %d %d" % (a, b)
130 print "Long __long__"