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