Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions Lib/_pydecimal.py
Original file line number Diff line number Diff line change
Expand Up @@ -1503,8 +1503,9 @@ def __divmod__(self, other, context=None):
context._raise_error(InvalidOperation, 'x % 0'))

quotient, remainder = self._divide(other, context)
remainder = remainder._fix(context)
return quotient, remainder
if not other._isinfinity():
quotient = quotient._fix(context)
return quotient, remainder._fix(context)
Comment on lines +1506 to +1508

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if not other._isinfinity():
quotient = quotient._fix(context)
return quotient, remainder._fix(context)
return quotient._fix(context), remainder._fix(context)

The helper already has a special case for Infinity, right? It's not altered.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

_fix() special-cases an infinite self, but when the divisor is infinite the quotient is a finite zero, and _fix() would clamp it where _decimal does not:

c = Context(prec=4, Emax=2, Emin=-4, clamp=1, traps=[])
c.divide_int(Decimal("-8.91"), Decimal("Infinity"))
# _decimal: Decimal("-0"), no flags
# _fix() on the quotient: Decimal("-0.0"), Clamped

That case was most of the mismatches when I compared against _decimal with the unconditional _fix() (483 of 20k random contexts, all clamp=1 with an infinite divisor), so I kept the check.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suggest you add test cases for this (and for __floordiv__).


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

return self._divide(other, context)[0]
quotient = self._divide(other, context)[0]
if not other._isinfinity():
quotient = quotient._fix(context)
return quotient
Comment thread
skirpichev marked this conversation as resolved.
Resolved

def __rfloordiv__(self, other, context=None):
"""Swaps self/other and returns __floordiv__."""
Expand Down
25 changes: 25 additions & 0 deletions Lib/test/test_decimal.py
Original file line number Diff line number Diff line change
Expand Up @@ -3293,6 +3293,31 @@ def test_divmod(self):
self.assertRaises(TypeError, c.divmod, '1', 2)
self.assertRaises(TypeError, c.divmod, 1, '2')

def test_divide_int_overflow(self):
Decimal = self.decimal.Decimal
Context = self.decimal.Context
Overflow = self.decimal.Overflow
Inexact = self.decimal.Inexact
Rounded = self.decimal.Rounded

# The quotient 300 fits in prec digits but not in Emax. The
# operators read the active context, so make it the one under test.
with self.decimal.localcontext(Context(prec=3, Emin=-1, Emax=1)) as c:
a, b = Decimal(75), Decimal('0.25')
for op in (c.divide_int, lambda x, y: c.divmod(x, y)[0],
operator.floordiv, lambda x, y: divmod(x, y)[0]):
c.traps[Overflow] = True
self.assertRaises(Overflow, op, a, b)

c.traps[Overflow] = False
c.clear_flags()
self.assertEqual(op(a, b), Decimal('Infinity'))
for flag in (Overflow, Inexact, Rounded):
self.assertTrue(c.flags[flag])
c.clear_flags()
self.assertEqual(c.remainder(a, b), Decimal('0.00'))
self.assertFalse(any(c.flags.values()))

def test_exp(self):
Decimal = self.decimal.Decimal
Context = self.decimal.Context
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Fix the pure-Python :mod:`decimal` implementation returning an out-of-range
quotient from ``//``, :func:`divmod`, :meth:`Context.divide_int
<decimal.Context.divide_int>` and :meth:`Context.divmod <decimal.Context.divmod>`
instead of signaling :exc:`~decimal.Overflow`.
Loading