You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Pure-Python decimal returns an out-of-range integer quotient with the Overflow trap enabled #162709
The pure-Python decimal implementation can return a finite result outside the active context’s exponent range without raising Overflow or setting any flags. This affects //, divmod(), and the corresponding Context methods.
Expected: both operations raise decimal.Overflow. Both inputs fit the context. The exact quotient 300 fits the three-digit precision, but its adjusted exponent is 2 and Emax is 1.
Changing only the import to import _decimal as decimal makes the first operation raise Overflow. With the trap disabled, the C implementation returns Infinity for the quotient and sets Overflow, Inexact, and Rounded. The Python implementation still returns 300 with no flags. I also reproduced this through the public decimal module on a main build using the Python fallback.
Decimal._divide() checks whether the quotient fits the precision and constructs it with _dec_from_triple. __floordiv__ returns that quotient directly. __divmod__ applies _fix(context) only to the remainder. One distinction worth preserving in a fix: ctx.remainder(a, b) correctly returns 0.00 in both implementations.
The C-versus-Python differential tester skips contexts where prec > context.Emax, which includes this example. Related work on this helper and its tests: #140044 and #160482. The behavior also reproduces on Python 3.11, so it predates those changes.
CPython versions tested on:
3.11, 3.12, 3.13, 3.14, 3.15, 3.16, CPython main branch
Activity