Skip to content

Commit bcc0db8

Browse files
committed
Get rid of remnants of integer division
1 parent ed483ba commit bcc0db8

28 files changed

+47
-212
lines changed

Include/object.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,6 @@ typedef struct {
158158
binaryfunc nb_add;
159159
binaryfunc nb_subtract;
160160
binaryfunc nb_multiply;
161-
binaryfunc nb_divide;
162161
binaryfunc nb_remainder;
163162
binaryfunc nb_divmod;
164163
ternaryfunc nb_power;
@@ -182,7 +181,6 @@ typedef struct {
182181
binaryfunc nb_inplace_add;
183182
binaryfunc nb_inplace_subtract;
184183
binaryfunc nb_inplace_multiply;
185-
binaryfunc nb_inplace_divide;
186184
binaryfunc nb_inplace_remainder;
187185
ternaryfunc nb_inplace_power;
188186
binaryfunc nb_inplace_lshift;
@@ -192,7 +190,6 @@ typedef struct {
192190
binaryfunc nb_inplace_or;
193191

194192
/* Added in release 2.2 */
195-
/* The following require the Py_TPFLAGS_HAVE_CLASS flag */
196193
binaryfunc nb_floor_divide;
197194
binaryfunc nb_true_divide;
198195
binaryfunc nb_inplace_floor_divide;

Lib/decimal.py

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1135,10 +1135,9 @@ def __mul__(self, other, context=None):
11351135
return ans
11361136
__rmul__ = __mul__
11371137

1138-
def __div__(self, other, context=None):
1138+
def __truediv__(self, other, context=None):
11391139
"""Return self / other."""
11401140
return self._divide(other, context=context)
1141-
__truediv__ = __div__
11421141

11431142
def _divide(self, other, divmod = 0, context=None):
11441143
"""Return a / b, to context.prec precision.
@@ -1306,13 +1305,12 @@ def _divide(self, other, divmod = 0, context=None):
13061305
ans = ans._fix(context)
13071306
return ans
13081307

1309-
def __rdiv__(self, other, context=None):
1310-
"""Swaps self/other and returns __div__."""
1308+
def __rtruediv__(self, other, context=None):
1309+
"""Swaps self/other and returns __truediv__."""
13111310
other = _convert_other(other)
13121311
if other is NotImplemented:
13131312
return other
1314-
return other.__div__(self, context=context)
1315-
__rtruediv__ = __rdiv__
1313+
return other.__truediv__(self, context=context)
13161314

13171315
def __divmod__(self, other, context=None):
13181316
"""
@@ -1384,9 +1382,9 @@ def remainder_near(self, other, context=None):
13841382
rounding = context._set_rounding_decision(NEVER_ROUND)
13851383

13861384
if other._sign:
1387-
comparison = other.__div__(Decimal(-2), context=context)
1385+
comparison = other.__truediv__(Decimal(-2), context=context)
13881386
else:
1389-
comparison = other.__div__(Decimal(2), context=context)
1387+
comparison = other.__truediv__(Decimal(2), context=context)
13901388

13911389
context._set_rounding_decision(rounding)
13921390
context._regard_flags(*flags)
@@ -1751,7 +1749,7 @@ def __pow__(self, n, modulo = None, context=None):
17511749
if n < 0:
17521750
#n is a long now, not Decimal instance
17531751
n = -n
1754-
mul = Decimal(1).__div__(mul, context=context)
1752+
mul = Decimal(1).__truediv__(mul, context=context)
17551753

17561754
spot = 1
17571755
while spot <= n:
@@ -1972,7 +1970,7 @@ def sqrt(self, context=None):
19721970
rounding = context._set_rounding(ROUND_HALF_EVEN)
19731971
while 1:
19741972
context.prec = min(2*context.prec - 2, maxp)
1975-
ans = half.__mul__(ans.__add__(tmp.__div__(ans, context=context),
1973+
ans = half.__mul__(ans.__add__(tmp.__truediv__(ans, context=context),
19761974
context=context), context=context)
19771975
if context.prec == maxp:
19781976
break
@@ -2454,7 +2452,7 @@ def divide(self, a, b):
24542452
>>> ExtendedContext.divide(Decimal('2.40E+6'), Decimal('2'))
24552453
Decimal("1.20E+6")
24562454
"""
2457-
return a.__div__(b, context=self)
2455+
return a.__truediv__(b, context=self)
24582456

24592457
def divide_int(self, a, b):
24602458
"""Divides two numbers and returns the integer part of the result.

Lib/test/test_augassign.py

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
x *= 2
66
x **= 2
77
x -= 8
8-
x //= 2
8+
x /= 2
99
x //= 1
1010
x %= 12
1111
x &= 2
@@ -19,7 +19,7 @@
1919
x[0] *= 2
2020
x[0] **= 2
2121
x[0] -= 8
22-
x[0] //= 2
22+
x[0] /= 2
2323
x[0] //= 2
2424
x[0] %= 12
2525
x[0] &= 2
@@ -33,7 +33,7 @@
3333
x[0] *= 2
3434
x[0] **= 2
3535
x[0] -= 8
36-
x[0] //= 2
36+
x[0] /= 2
3737
x[0] //= 1
3838
x[0] %= 12
3939
x[0] &= 2
@@ -123,14 +123,6 @@ def __imul__(self, val):
123123
print "__imul__ called"
124124
return self
125125

126-
def __div__(self, val):
127-
print "__div__ called"
128-
def __rdiv__(self, val):
129-
print "__rdiv__ called"
130-
def __idiv__(self, val):
131-
print "__idiv__ called"
132-
return self
133-
134126
def __floordiv__(self, val):
135127
print "__floordiv__ called"
136128
return self
@@ -147,6 +139,9 @@ def __truediv__(self, val):
147139
def __itruediv__(self, val):
148140
print "__itruediv__ called"
149141
return self
142+
def __rtruediv__(self, val):
143+
print "__rtruediv__ called"
144+
return self
150145

151146
def __mod__(self, val):
152147
print "__mod__ called"
@@ -217,16 +212,9 @@ def __ilshift__(self, val):
217212
1 * x
218213
x *= 1
219214

220-
if 1/2 == 0:
221-
x / 1
222-
1 / x
223-
x /= 1
224-
else:
225-
# True division is in effect, so "/" doesn't map to __div__ etc;
226-
# but the canned expected-output file requires that those get called.
227-
x.__div__(1)
228-
x.__rdiv__(1)
229-
x.__idiv__(1)
215+
x / 1
216+
1 / x
217+
x /= 1
230218

231219
x // 1
232220
1 // x

Lib/test/test_binop.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -140,8 +140,6 @@ def __truediv__(self, other):
140140
return float(self) / other
141141
return NotImplemented
142142

143-
__div__ = __truediv__
144-
145143
def __rtruediv__(self, other):
146144
"""Divide two Rats, or a Rat and a number (reversed args)."""
147145
if isRat(other):
@@ -152,8 +150,6 @@ def __rtruediv__(self, other):
152150
return other / float(self)
153151
return NotImplemented
154152

155-
__rdiv__ = __rtruediv__
156-
157153
def __floordiv__(self, other):
158154
"""Divide two Rats, returning the floored result."""
159155
if isint(other):

Lib/test/test_class.py

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
"rsub",
1212
"mul",
1313
"rmul",
14-
"div",
15-
"rdiv",
14+
"truediv",
15+
"rtruediv",
1616
"mod",
1717
"rmod",
1818
"divmod",
@@ -134,16 +134,8 @@ def __%(method)s__(self, *args):
134134
testme * 1
135135
1 * testme
136136

137-
if 1/2 == 0:
138-
testme / 1
139-
1 / testme
140-
else:
141-
# True division is in effect, so "/" doesn't map to __div__ etc; but
142-
# the canned expected-output file requires that __div__ etc get called.
143-
testme.__coerce__(1)
144-
testme.__div__(1)
145-
testme.__coerce__(1)
146-
testme.__rdiv__(1)
137+
testme / 1
138+
1 / testme
147139

148140
testme % 1
149141
1 % testme

Lib/test/test_coercion.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,10 @@ def __mul__(self,other):
4444
def __rmul__(self,other):
4545
return other * self.arg
4646

47-
def __div__(self,other):
47+
def __truediv__(self,other):
4848
return self.arg / other
4949

50-
def __rdiv__(self,other):
50+
def __rtruediv__(self,other):
5151
return other / self.arg
5252

5353
def __pow__(self,other):

Lib/test/test_complex.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,19 +55,15 @@ def check_div(self, x, y):
5555
if x != 0:
5656
q = z / x
5757
self.assertClose(q, y)
58-
q = z.__div__(x)
59-
self.assertClose(q, y)
6058
q = z.__truediv__(x)
6159
self.assertClose(q, y)
6260
if y != 0:
6361
q = z / y
6462
self.assertClose(q, x)
65-
q = z.__div__(y)
66-
self.assertClose(q, x)
6763
q = z.__truediv__(y)
6864
self.assertClose(q, x)
6965

70-
def test_div(self):
66+
def test_truediv(self):
7167
simple_real = [float(i) for i in xrange(-5, 6)]
7268
simple_complex = [complex(x, y) for x in simple_real for y in simple_real]
7369
for x in simple_complex:
@@ -84,7 +80,7 @@ def test_div(self):
8480
self.check_div(complex(random(), random()),
8581
complex(random(), random()))
8682

87-
self.assertRaises(ZeroDivisionError, complex.__div__, 1+1j, 0+0j)
83+
self.assertRaises(ZeroDivisionError, complex.__truediv__, 1+1j, 0+0j)
8884
# FIXME: The following currently crashes on Alpha
8985
# self.assertRaises(OverflowError, pow, 1e200+1j, 1e200+1j)
9086

Lib/test/test_decimal.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -507,7 +507,7 @@ def __ne__(self, other):
507507
('+', '__add__', '__radd__'),
508508
('-', '__sub__', '__rsub__'),
509509
('*', '__mul__', '__rmul__'),
510-
('/', '__div__', '__rdiv__'),
510+
('/', '__truediv__', '__rtruediv__'),
511511
('%', '__mod__', '__rmod__'),
512512
('//', '__floordiv__', '__rfloordiv__'),
513513
('**', '__pow__', '__rpow__'),
@@ -975,7 +975,6 @@ def checkSameDec(operation, useOther=False):
975975

976976
checkSameDec("__abs__")
977977
checkSameDec("__add__", True)
978-
checkSameDec("__div__", True)
979978
checkSameDec("__divmod__", True)
980979
checkSameDec("__cmp__", True)
981980
checkSameDec("__float__")
@@ -990,7 +989,6 @@ def checkSameDec(operation, useOther=False):
990989
checkSameDec("__pos__")
991990
checkSameDec("__pow__", True)
992991
checkSameDec("__radd__", True)
993-
checkSameDec("__rdiv__", True)
994992
checkSameDec("__rdivmod__", True)
995993
checkSameDec("__repr__")
996994
checkSameDec("__rfloordiv__", True)

Lib/test/test_descr.py

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,6 @@ def testbinop(a, b, res, expr="a+b", meth="__add__"):
2929
if verbose: print "checking", expr
3030
dict = {'a': a, 'b': b}
3131

32-
# XXX Hack so this passes before 2.3 when -Qnew is specified.
33-
if meth == "__div__" and 1/2 == 0.5:
34-
meth = "__truediv__"
35-
3632
vereq(eval(expr, dict), res)
3733
t = type(a)
3834
m = getattr(t, meth)
@@ -4044,9 +4040,8 @@ def check(expr, x, y):
40444040
('__add__', 'x + y', 'x += y'),
40454041
('__sub__', 'x - y', 'x -= y'),
40464042
('__mul__', 'x * y', 'x *= y'),
4047-
('__truediv__', 'operator.truediv(x, y)', None),
4048-
('__floordiv__', 'operator.floordiv(x, y)', None),
4049-
('__div__', 'x / y', 'x /= y'),
4043+
('__truediv__', 'x / y', None),
4044+
('__floordiv__', 'x // y', None),
40504045
('__mod__', 'x % y', 'x %= y'),
40514046
('__divmod__', 'divmod(x, y)', None),
40524047
('__pow__', 'x ** y', 'x **= y'),

Lib/test/test_operator.py

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -144,11 +144,6 @@ def test_delslice(self):
144144
self.failUnless(operator.delslice(a, 2, 8) is None)
145145
self.assert_(a == [0, 1, 8, 9])
146146

147-
def test_div(self):
148-
self.failUnlessRaises(TypeError, operator.div, 5)
149-
self.failUnlessRaises(TypeError, operator.div, None, None)
150-
self.failUnless(operator.floordiv(5, 2) == 2)
151-
152147
def test_floordiv(self):
153148
self.failUnlessRaises(TypeError, operator.floordiv, 5)
154149
self.failUnlessRaises(TypeError, operator.floordiv, None, None)
@@ -416,7 +411,6 @@ def test_inplace(self):
416411
class C(object):
417412
def __iadd__ (self, other): return "iadd"
418413
def __iand__ (self, other): return "iand"
419-
def __idiv__ (self, other): return "idiv"
420414
def __ifloordiv__(self, other): return "ifloordiv"
421415
def __ilshift__ (self, other): return "ilshift"
422416
def __imod__ (self, other): return "imod"
@@ -431,7 +425,6 @@ def __getitem__(self, other): return 5 # so that C is a sequence
431425
c = C()
432426
self.assertEqual(operator.iadd (c, 5), "iadd")
433427
self.assertEqual(operator.iand (c, 5), "iand")
434-
self.assertEqual(operator.idiv (c, 5), "idiv")
435428
self.assertEqual(operator.ifloordiv(c, 5), "ifloordiv")
436429
self.assertEqual(operator.ilshift (c, 5), "ilshift")
437430
self.assertEqual(operator.imod (c, 5), "imod")
@@ -446,7 +439,6 @@ def __getitem__(self, other): return 5 # so that C is a sequence
446439
self.assertEqual(operator.irepeat (c, 5), "imul")
447440
self.assertEqual(operator.__iadd__ (c, 5), "iadd")
448441
self.assertEqual(operator.__iand__ (c, 5), "iand")
449-
self.assertEqual(operator.__idiv__ (c, 5), "idiv")
450442
self.assertEqual(operator.__ifloordiv__(c, 5), "ifloordiv")
451443
self.assertEqual(operator.__ilshift__ (c, 5), "ilshift")
452444
self.assertEqual(operator.__imod__ (c, 5), "imod")

Misc/NEWS

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,14 @@ Core and Builtins
4040
- Exceptions *must* derive from BaseException.
4141

4242
- Integer division always returns a float. The -Q option is no more.
43+
All the following are gone:
44+
* PyNumber_Divide and PyNumber_InPlaceDivide
45+
* __div__, __rdiv__, and __idiv__
46+
* nb_divide, nb_inplace_divide
47+
* operator.div, operator.idiv, operator.__div__, operator.__idiv__
48+
(Only __truediv__ and __floordiv__ remain, not sure how to handle them
49+
if we want to re-use __div__ and friends. If we do, it will make
50+
it harder to write code for both 2.x and 3.x.)
4351

4452
- 'as' and 'with' are keywords.
4553

Modules/_ctypes/_ctypes.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3812,7 +3812,6 @@ static PyNumberMethods Simple_as_number = {
38123812
0, /* nb_add */
38133813
0, /* nb_subtract */
38143814
0, /* nb_multiply */
3815-
0, /* nb_divide */
38163815
0, /* nb_remainder */
38173816
0, /* nb_divmod */
38183817
0, /* nb_power */
@@ -4165,7 +4164,6 @@ static PyNumberMethods Pointer_as_number = {
41654164
0, /* nb_add */
41664165
0, /* nb_subtract */
41674166
0, /* nb_multiply */
4168-
0, /* nb_divide */
41694167
0, /* nb_remainder */
41704168
0, /* nb_divmod */
41714169
0, /* nb_power */

0 commit comments

Comments
 (0)