Skip to content

Commit 9377ab6

Browse files
committed
_pydecimal: add tests for unbounded contexts
1 parent efbdc0a commit 9377ab6

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

Lib/test/test_decimal.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
with the corresponding argument.
2525
"""
2626

27+
import contextlib
2728
import logging
2829
import math
2930
import os, sys
@@ -4493,6 +4494,15 @@ def test_decimal_attributes(self):
44934494

44944495
class Coverage:
44954496

4497+
@contextlib.contextmanager
4498+
def unbound_context(self, prec=None, Emax=None, Emin=None):
4499+
with self.decimal.localcontext() as c:
4500+
c.prec = self.decimal.MAX_PREC if prec is None else prec
4501+
c.Emax = self.decimal.MAX_EMAX if Emax is None else Emax
4502+
c.Emin = self.decimal.MIN_EMIN if Emin is None else Emin
4503+
c.traps[self.decimal.Inexact] = 1
4504+
yield c
4505+
44964506
def test_adjusted(self):
44974507
Decimal = self.decimal.Decimal
44984508

@@ -4660,6 +4670,22 @@ def test_divmod(self):
46604670
self.assertTrue(c.flags[InvalidOperation] and
46614671
c.flags[DivisionByZero])
46624672

4673+
def test_divide_unbound_context(self):
4674+
with self.unbound_context() as c:
4675+
x = self.decimal.Decimal('1')
4676+
y = x // 1 # should be fast
4677+
4678+
def test_remainder_near(self):
4679+
L = 1000
4680+
limit = sys.get_int_max_str_digits()
4681+
sys.set_int_max_str_digits(L)
4682+
self.addCleanup(sys.set_int_max_str_digits, limit)
4683+
4684+
with self.unbound_context(prec=2 * L) as c:
4685+
self.assertEqual(c.prec, 2 * L)
4686+
x = self.decimal.Decimal(f'1e{L}')
4687+
y = x.remainder_near(1) # must not raise a ValueError
4688+
46634689
def test_power(self):
46644690
Decimal = self.decimal.Decimal
46654691
localcontext = self.decimal.localcontext

0 commit comments

Comments
 (0)