Skip to content

Commit c2ccce7

Browse files
Issue python#23641: Cleaned out legacy dunder names from tests and docs.
Fixed 2 to 3 porting bug in pynche.ColorDB. Added few tests for __truediv__, __floordiv__ and __matmul__.
2 parents da0870c + a60c2fe commit c2ccce7

File tree

19 files changed

+86
-124
lines changed

19 files changed

+86
-124
lines changed

Doc/library/multiprocessing.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1834,7 +1834,7 @@ itself. This means, for example, that one shared object can contain a second:
18341834
>>> l = manager.list(range(10))
18351835
>>> l._callmethod('__len__')
18361836
10
1837-
>>> l._callmethod('__getslice__', (2, 7)) # equiv to `l[2:7]`
1837+
>>> l._callmethod('__getitem__', (slice(2, 7),)) # equiv to `l[2:7]`
18381838
[2, 3, 4, 5, 6]
18391839
>>> l._callmethod('__getitem__', (20,)) # equiv to `l[20]`
18401840
Traceback (most recent call last):

Doc/library/unittest.mock.rst

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1621,19 +1621,19 @@ The full list of supported magic methods is:
16211621
* ``__hash__``, ``__sizeof__``, ``__repr__`` and ``__str__``
16221622
* ``__dir__``, ``__format__`` and ``__subclasses__``
16231623
* ``__floor__``, ``__trunc__`` and ``__ceil__``
1624-
* Comparisons: ``__cmp__``, ``__lt__``, ``__gt__``, ``__le__``, ``__ge__``,
1624+
* Comparisons: ``__lt__``, ``__gt__``, ``__le__``, ``__ge__``,
16251625
``__eq__`` and ``__ne__``
16261626
* Container methods: ``__getitem__``, ``__setitem__``, ``__delitem__``,
1627-
``__contains__``, ``__len__``, ``__iter__``, ``__getslice__``,
1628-
``__setslice__``, ``__reversed__`` and ``__missing__``
1627+
``__contains__``, ``__len__``, ``__iter__``, ``__reversed__``
1628+
and ``__missing__``
16291629
* Context manager: ``__enter__`` and ``__exit__``
16301630
* Unary numeric methods: ``__neg__``, ``__pos__`` and ``__invert__``
16311631
* The numeric methods (including right hand and in-place variants):
1632-
``__add__``, ``__sub__``, ``__mul__``, ``__div__``,
1632+
``__add__``, ``__sub__``, ``__mul__``, ``__matmul__``, ``__div__``, ``__truediv__``,
16331633
``__floordiv__``, ``__mod__``, ``__divmod__``, ``__lshift__``,
16341634
``__rshift__``, ``__and__``, ``__xor__``, ``__or__``, and ``__pow__``
1635-
* Numeric conversion methods: ``__complex__``, ``__int__``, ``__float__``,
1636-
``__index__`` and ``__coerce__``
1635+
* Numeric conversion methods: ``__complex__``, ``__int__``, ``__float__``
1636+
and ``__index__``
16371637
* Descriptor methods: ``__get__``, ``__set__`` and ``__delete__``
16381638
* Pickling: ``__reduce__``, ``__reduce_ex__``, ``__getinitargs__``,
16391639
``__getnewargs__``, ``__getstate__`` and ``__setstate__``

Lib/_pydecimal.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -960,13 +960,12 @@ def __ge__(self, other, context=None):
960960
return self._cmp(other) >= 0
961961

962962
def compare(self, other, context=None):
963-
"""Compares one to another.
963+
"""Compare self to other. Return a decimal value:
964964
965-
-1 => a < b
966-
0 => a = b
967-
1 => a > b
968-
NaN => one is NaN
969-
Like __cmp__, but returns Decimal instances.
965+
a or b is a NaN ==> Decimal('NaN')
966+
a < b ==> Decimal('-1')
967+
a == b ==> Decimal('0')
968+
a > b ==> Decimal('1')
970969
"""
971970
other = _convert_other(other, raiseit=True)
972971

Lib/datetime.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -651,7 +651,7 @@ class date:
651651
Operators:
652652
653653
__repr__, __str__
654-
__cmp__, __hash__
654+
__eq__, __le__, __lt__, __ge__, __gt__, __hash__
655655
__add__, __radd__, __sub__ (add/radd only with timedelta arg)
656656
657657
Methods:
@@ -786,7 +786,8 @@ def day(self):
786786
"""day (1-31)"""
787787
return self._day
788788

789-
# Standard conversions, __cmp__, __hash__ (and helpers)
789+
# Standard conversions, __eq__, __le__, __lt__, __ge__, __gt__,
790+
# __hash__ (and helpers)
790791

791792
def timetuple(self):
792793
"Return local time tuple compatible with time.localtime()."
@@ -1010,7 +1011,7 @@ class time:
10101011
Operators:
10111012
10121013
__repr__, __str__
1013-
__cmp__, __hash__
1014+
__eq__, __le__, __lt__, __ge__, __gt__, __hash__
10141015
10151016
Methods:
10161017

Lib/sqlite3/test/types.py

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -88,19 +88,10 @@ def __init__(self, _val):
8888
_val = _val.decode('utf-8')
8989
self.val = _val
9090

91-
def __cmp__(self, other):
92-
if not isinstance(other, DeclTypesTests.Foo):
93-
raise ValueError
94-
if self.val == other.val:
95-
return 0
96-
else:
97-
return 1
98-
9991
def __eq__(self, other):
100-
c = self.__cmp__(other)
101-
if c is NotImplemented:
102-
return c
103-
return c == 0
92+
if not isinstance(other, DeclTypesTests.Foo):
93+
return NotImplemented
94+
return self.val == other.val
10495

10596
def __conform__(self, protocol):
10697
if protocol is sqlite.PrepareProtocol:

Lib/test/mapping_tests.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ def test_read(self):
6464
self.assertEqual(d, d)
6565
self.assertNotEqual(p, d)
6666
self.assertNotEqual(d, p)
67-
#__non__zero__
67+
#bool
6868
if p: self.fail("Empty mapping must compare to False")
6969
if not d: self.fail("Full mapping must compare to True")
7070
# keys(), items(), iterkeys() ...

Lib/test/test_abc.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -194,9 +194,9 @@ def foo(self, val): pass
194194
# check that the property's __isabstractmethod__ descriptor does the
195195
# right thing when presented with a value that fails truth testing:
196196
class NotBool(object):
197-
def __nonzero__(self):
197+
def __bool__(self):
198198
raise ValueError()
199-
__len__ = __nonzero__
199+
__len__ = __bool__
200200
with self.assertRaises(ValueError):
201201
class F(C):
202202
def bar(self):

Lib/test/test_augassign.py

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -144,14 +144,6 @@ def __imatmul__(self, val):
144144
output.append("__imatmul__ called")
145145
return self
146146

147-
def __div__(self, val):
148-
output.append("__div__ called")
149-
def __rdiv__(self, val):
150-
output.append("__rdiv__ called")
151-
def __idiv__(self, val):
152-
output.append("__idiv__ called")
153-
return self
154-
155147
def __floordiv__(self, val):
156148
output.append("__floordiv__ called")
157149
return self

Lib/test/test_class.py

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,12 @@
1313
"rsub",
1414
"mul",
1515
"rmul",
16+
"matmul",
17+
"rmatmul",
1618
"truediv",
1719
"rtruediv",
20+
"floordiv",
21+
"rfloordiv",
1822
"mod",
1923
"rmod",
2024
"divmod",
@@ -174,15 +178,31 @@ def testBinaryOps(self):
174178
1 * testme
175179
self.assertCallStack([("__rmul__", (testme, 1))])
176180

177-
if 1/2 == 0:
178-
callLst[:] = []
179-
testme / 1
180-
self.assertCallStack([("__div__", (testme, 1))])
181+
callLst[:] = []
182+
testme @ 1
183+
self.assertCallStack([("__matmul__", (testme, 1))])
184+
185+
callLst[:] = []
186+
1 @ testme
187+
self.assertCallStack([("__rmatmul__", (testme, 1))])
188+
189+
callLst[:] = []
190+
testme / 1
191+
self.assertCallStack([("__truediv__", (testme, 1))])
181192

182193

183-
callLst[:] = []
184-
1 / testme
185-
self.assertCallStack([("__rdiv__", (testme, 1))])
194+
callLst[:] = []
195+
1 / testme
196+
self.assertCallStack([("__rtruediv__", (testme, 1))])
197+
198+
callLst[:] = []
199+
testme // 1
200+
self.assertCallStack([("__floordiv__", (testme, 1))])
201+
202+
203+
callLst[:] = []
204+
1 // testme
205+
self.assertCallStack([("__rfloordiv__", (testme, 1))])
186206

187207
callLst[:] = []
188208
testme % 1
@@ -444,12 +464,16 @@ class BadTypeClass:
444464
def __int__(self):
445465
return None
446466
__float__ = __int__
467+
__complex__ = __int__
447468
__str__ = __int__
448469
__repr__ = __int__
449-
__oct__ = __int__
450-
__hex__ = __int__
470+
__bytes__ = __int__
471+
__bool__ = __int__
472+
__index__ = __int__
473+
def index(x):
474+
return [][x]
451475

452-
for f in [int, float, str, repr, oct, hex]:
476+
for f in [float, complex, str, repr, bytes, bin, oct, hex, bool, index]:
453477
self.assertRaises(TypeError, f, BadTypeClass())
454478

455479
def testHashStuff(self):

Lib/test/test_descr.py

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@ def __init__(self, *args, **kwargs):
2121
'add': '+',
2222
'sub': '-',
2323
'mul': '*',
24-
'div': '/',
24+
'matmul': '@',
25+
'truediv': '/',
26+
'floordiv': '//',
2527
'divmod': 'divmod',
2628
'pow': '**',
2729
'lshift': '<<',
@@ -52,8 +54,6 @@ def __init__(self, *args, **kwargs):
5254
'invert': '~',
5355
'int': 'int',
5456
'float': 'float',
55-
'oct': 'oct',
56-
'hex': 'hex',
5757
}
5858

5959
for name, expr in list(self.unops.items()):
@@ -82,12 +82,6 @@ def unop_test(self, a, res, expr="len(a)", meth="__len__"):
8282
def binop_test(self, a, b, res, expr="a+b", meth="__add__"):
8383
d = {'a': a, 'b': b}
8484

85-
# XXX Hack so this passes before 2.3 when -Qnew is specified.
86-
if meth == "__div__" and 1/2 == 0.5:
87-
meth = "__truediv__"
88-
89-
if meth == '__divmod__': pass
90-
9185
self.assertEqual(eval(expr, d), res)
9286
t = type(a)
9387
m = getattr(t, meth)
@@ -221,7 +215,7 @@ def test_dicts(self):
221215
def number_operators(self, a, b, skip=[]):
222216
dict = {'a': a, 'b': b}
223217

224-
for name, expr in list(self.binops.items()):
218+
for name, expr in self.binops.items():
225219
if name not in skip:
226220
name = "__%s__" % name
227221
if hasattr(a, name):
@@ -261,7 +255,7 @@ def test_complexes(self):
261255
# Testing complex operations...
262256
self.number_operators(100.0j, 3.0j, skip=['lt', 'le', 'gt', 'ge',
263257
'int', 'float',
264-
'divmod', 'mod'])
258+
'floordiv', 'divmod', 'mod'])
265259

266260
class Number(complex):
267261
__slots__ = ['prec']
@@ -4177,9 +4171,8 @@ def check(expr, x, y):
41774171
('__sub__', 'x - y', 'x -= y'),
41784172
('__mul__', 'x * y', 'x *= y'),
41794173
('__matmul__', 'x @ y', 'x @= y'),
4180-
('__truediv__', 'operator.truediv(x, y)', None),
4181-
('__floordiv__', 'operator.floordiv(x, y)', None),
4182-
('__div__', 'x / y', 'x /= y'),
4174+
('__truediv__', 'x / y', 'x /= y'),
4175+
('__floordiv__', 'x // y', 'x //= y'),
41834176
('__mod__', 'x % y', 'x %= y'),
41844177
('__divmod__', 'divmod(x, y)', None),
41854178
('__pow__', 'x ** y', 'x **= y'),
@@ -4241,8 +4234,8 @@ class X(object):
42414234
# Also check type_getattro for correctness.
42424235
class Meta(type):
42434236
pass
4244-
class X(object):
4245-
__metaclass__ = Meta
4237+
class X(metaclass=Meta):
4238+
pass
42464239
X.a = 42
42474240
Meta.a = Descr("a")
42484241
self.assertEqual(X.a, 42)

0 commit comments

Comments
 (0)