Skip to content

Commit 6af16be

Browse files
committed
gh-162709: Signal Overflow for an out-of-range integer quotient in _pydecimal
1 parent 5dbe2ac commit 6af16be

3 files changed

Lines changed: 34 additions & 3 deletions

File tree

‎Lib/_pydecimal.py‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1503,8 +1503,9 @@ def __divmod__(self, other, context=None):
15031503
context._raise_error(InvalidOperation, 'x % 0'))
15041504

15051505
quotient, remainder = self._divide(other, context)
1506-
remainder = remainder._fix(context)
1507-
return quotient, remainder
1506+
if not other._isinfinity():
1507+
quotient = quotient._fix(context)
1508+
return quotient, remainder._fix(context)
15081509

15091510
def __rdivmod__(self, other, context=None):
15101511
"""Swaps self/other and returns __divmod__."""
@@ -1650,7 +1651,10 @@ def __floordiv__(self, other, context=None):
16501651
else:
16511652
return context._raise_error(DivisionUndefined, '0 // 0')
16521653

1653-
return self._divide(other, context)[0]
1654+
quotient = self._divide(other, context)[0]
1655+
if not other._isinfinity():
1656+
quotient = quotient._fix(context)
1657+
return quotient
16541658

16551659
def __rfloordiv__(self, other, context=None):
16561660
"""Swaps self/other and returns __floordiv__."""

‎Lib/test/test_decimal.py‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3293,6 +3293,29 @@ def test_divmod(self):
32933293
self.assertRaises(TypeError, c.divmod, '1', 2)
32943294
self.assertRaises(TypeError, c.divmod, 1, '2')
32953295

3296+
def test_divide_int_overflow(self):
3297+
Decimal = self.decimal.Decimal
3298+
Context = self.decimal.Context
3299+
Overflow = self.decimal.Overflow
3300+
Inexact = self.decimal.Inexact
3301+
Rounded = self.decimal.Rounded
3302+
3303+
# The quotient 300 fits in prec digits but not in Emax.
3304+
c = Context(prec=3, Emin=-1, Emax=1)
3305+
a, b = Decimal(75), Decimal('0.25')
3306+
for op in (c.divide_int, lambda x, y: c.divmod(x, y)[0]):
3307+
c.traps[Overflow] = True
3308+
self.assertRaises(Overflow, op, a, b)
3309+
3310+
c.traps[Overflow] = False
3311+
c.clear_flags()
3312+
self.assertEqual(op(a, b), Decimal('Infinity'))
3313+
for flag in (Overflow, Inexact, Rounded):
3314+
self.assertTrue(c.flags[flag])
3315+
c.clear_flags()
3316+
self.assertEqual(c.remainder(a, b), Decimal('0.00'))
3317+
self.assertFalse(any(c.flags.values()))
3318+
32963319
def test_exp(self):
32973320
Decimal = self.decimal.Decimal
32983321
Context = self.decimal.Context
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Fix the pure-Python :mod:`decimal` implementation returning an out-of-range
2+
quotient from ``//``, :func:`divmod`, :meth:`Context.divide_int
3+
<decimal.Context.divide_int>` and :meth:`Context.divmod <decimal.Context.divmod>`
4+
instead of signaling :exc:`~decimal.Overflow`.

0 commit comments

Comments
 (0)