Index: Lib/decimal.py =================================================================== --- Lib/decimal.py (revisiĆ³n: 77317) +++ Lib/decimal.py (copia de trabajo) @@ -3863,6 +3863,8 @@ >>> ExtendedContext.add(Decimal('1E+2'), Decimal('1.01E+4')) Decimal('1.02E+4') """ + if not isinstance(a, Decimal): + a = Decimal(a, context=self) return a.__add__(b, context=self) def _apply(self, a): @@ -3906,6 +3908,8 @@ >>> ExtendedContext.compare(Decimal('-3'), Decimal('2.1')) Decimal('-1') """ + if not isinstance(a, Decimal): + a = Decimal(a, context=self) return a.compare(b, context=self) def compare_signal(self, a, b): @@ -3934,6 +3938,8 @@ >>> print c.flags[InvalidOperation] 1 """ + if not isinstance(a, Decimal): + a = Decimal(a, context=self) return a.compare_signal(b, context=self) def compare_total(self, a, b): @@ -3956,6 +3962,8 @@ >>> ExtendedContext.compare_total(Decimal('12.3'), Decimal('NaN')) Decimal('-1') """ + if not isinstance(a, Decimal): + a = Decimal(a, context=self) return a.compare_total(b) def compare_total_mag(self, a, b): @@ -3963,6 +3971,8 @@ Like compare_total, but with operand's sign ignored and assumed to be 0. """ + if not isinstance(a, Decimal): + a = Decimal(a, context=self) return a.compare_total_mag(b) def copy_abs(self, a): @@ -4010,6 +4020,8 @@ >>> ExtendedContext.copy_sign(Decimal('-1.50'), Decimal('-7.33')) Decimal('-1.50') """ + if not isinstance(a, Decimal): + a = Decimal(a, context=self) return a.copy_sign(b) def divide(self, a, b): @@ -4036,6 +4048,8 @@ >>> ExtendedContext.divide(Decimal('2.40E+6'), Decimal('2')) Decimal('1.20E+6') """ + if not isinstance(a, Decimal): + a = Decimal(a, context=self) return a.__div__(b, context=self) def divide_int(self, a, b): @@ -4048,9 +4062,20 @@ >>> ExtendedContext.divide_int(Decimal('1'), Decimal('0.3')) Decimal('3') """ + if not isinstance(a, Decimal): + a = Decimal(a, context=self) return a.__floordiv__(b, context=self) def divmod(self, a, b): + """Return (self // other, self % other) + + >>> ExtendedContext.divmod(Decimal(8), Decimal(3)) + (Decimal('2'), Decimal('2')) + >>> ExtendedContext.divmod(Decimal(8), Decimal(4)) + (Decimal('2'), Decimal('0')) + """ + if not isinstance(a, Decimal): + a = Decimal(a, context=self) return a.__divmod__(b, context=self) def exp(self, a): @@ -4088,6 +4113,8 @@ >>> ExtendedContext.fma(Decimal('888565290'), Decimal('1557.96930'), Decimal('-86087.7578')) Decimal('1.38435736E+12') """ + if not isinstance(a, Decimal): + a = Decimal(a, context=self) return a.fma(b, c, context=self) def is_canonical(self, a): @@ -4312,6 +4339,8 @@ >>> ExtendedContext.logical_and(Decimal('1111'), Decimal('10')) Decimal('10') """ + if not isinstance(a, Decimal): + a = Decimal(a, context=self) return a.logical_and(b, context=self) def logical_invert(self, a): @@ -4348,6 +4377,8 @@ >>> ExtendedContext.logical_or(Decimal('1110'), Decimal('10')) Decimal('1110') """ + if not isinstance(a, Decimal): + a = Decimal(a, context=self) return a.logical_or(b, context=self) def logical_xor(self, a, b): @@ -4368,6 +4399,8 @@ >>> ExtendedContext.logical_xor(Decimal('1111'), Decimal('10')) Decimal('1101') """ + if not isinstance(a, Decimal): + a = Decimal(a, context=self) return a.logical_xor(b, context=self) def max(self, a,b): @@ -4388,10 +4421,14 @@ >>> ExtendedContext.max(Decimal('7'), Decimal('NaN')) Decimal('7') """ + if not isinstance(a, Decimal): + a = Decimal(a, context=self) return a.max(b, context=self) def max_mag(self, a, b): """Compares the values numerically with their sign ignored.""" + if not isinstance(a, Decimal): + a = Decimal(a, context=self) return a.max_mag(b, context=self) def min(self, a,b): @@ -4412,10 +4449,14 @@ >>> ExtendedContext.min(Decimal('7'), Decimal('NaN')) Decimal('7') """ + if not isinstance(a, Decimal): + a = Decimal(a, context=self) return a.min(b, context=self) def min_mag(self, a, b): """Compares the values numerically with their sign ignored.""" + if not isinstance(a, Decimal): + a = Decimal(a, context=self) return a.min_mag(b, context=self) def minus(self, a): @@ -4451,6 +4492,8 @@ >>> ExtendedContext.multiply(Decimal('654321'), Decimal('654321')) Decimal('4.28135971E+11') """ + if not isinstance(a, Decimal): + a = Decimal(a, context=self) return a.__mul__(b, context=self) def next_minus(self, a): @@ -4513,6 +4556,8 @@ >>> c.next_toward(Decimal('0.00'), Decimal('-0.0000')) Decimal('-0.00') """ + if not isinstance(a, Decimal): + a = Decimal(a, context=self) return a.next_toward(b, context=self) def normalize(self, a): @@ -4664,6 +4709,8 @@ >>> c.power(Decimal('-23'), Decimal('0'), Decimal('65537')) Decimal('1') """ + if not isinstance(a, Decimal): + a = Decimal(a, context=self) return a.__pow__(b, modulo, context=self) def quantize(self, a, b): @@ -4715,6 +4762,8 @@ >>> ExtendedContext.quantize(Decimal('217'), Decimal('1e+2')) Decimal('2E+2') """ + if not isinstance(a, Decimal): + a = Decimal(a, context=self) return a.quantize(b, context=self) def radix(self): @@ -4750,6 +4799,8 @@ >>> ExtendedContext.remainder(Decimal('3.6'), Decimal('1.3')) Decimal('1.0') """ + if not isinstance(a, Decimal): + a = Decimal(a, context=self) return a.__mod__(b, context=self) def remainder_near(self, a, b): @@ -4777,6 +4828,8 @@ >>> ExtendedContext.remainder_near(Decimal('3.6'), Decimal('1.3')) Decimal('-0.3') """ + if not isinstance(a, Decimal): + a = Decimal(a, context=self) return a.remainder_near(b, context=self) def rotate(self, a, b): @@ -4799,6 +4852,8 @@ >>> ExtendedContext.rotate(Decimal('123456789'), Decimal('+2')) Decimal('345678912') """ + if not isinstance(a, Decimal): + a = Decimal(a, context=self) return a.rotate(b, context=self) def same_quantum(self, a, b): @@ -4816,6 +4871,8 @@ >>> ExtendedContext.same_quantum(Decimal('Inf'), Decimal('-Inf')) True """ + if not isinstance(a, Decimal): + a = Decimal(a, context=self) return a.same_quantum(b) def scaleb (self, a, b): @@ -4828,7 +4885,9 @@ >>> ExtendedContext.scaleb(Decimal('7.50'), Decimal('3')) Decimal('7.50E+3') """ - return a.scaleb (b, context=self) + if not isinstance(a, Decimal): + a = Decimal(a, context=self) + return a.scaleb(b, context=self) def shift(self, a, b): """Returns a shifted copy of a, b times. @@ -4851,6 +4910,8 @@ >>> ExtendedContext.shift(Decimal('123456789'), Decimal('+2')) Decimal('345678900') """ + if not isinstance(a, Decimal): + a = Decimal(a, context=self) return a.shift(b, context=self) def sqrt(self, a): @@ -4892,6 +4953,8 @@ >>> ExtendedContext.subtract(Decimal('1.3'), Decimal('2.07')) Decimal('-0.77') """ + if not isinstance(a, Decimal): + a = Decimal(a, context=self) return a.__sub__(b, context=self) def to_eng_string(self, a): Index: Lib/test/test_decimal.py =================================================================== --- Lib/test/test_decimal.py (revisiĆ³n: 77317) +++ Lib/test/test_decimal.py (copia de trabajo) @@ -1638,7 +1638,193 @@ self.assertNotEqual(id(c), id(d)) self.assertNotEqual(id(c.flags), id(d.flags)) self.assertNotEqual(id(c.traps), id(d.traps)) + + def test_add(self): + c = Context() + d = c.add(Decimal(1), Decimal(1)) + self.assertEqual(c.add(1, 1), d) + self.assertEqual(c.add(Decimal(1), 1), d) + self.assertEqual(c.add(1, Decimal(1)), d) + def test_compare(self): + c = Context() + d = c.compare(Decimal(1), Decimal(1)) + self.assertEqual(c.compare(1, 1), d) + self.assertEqual(c.compare(Decimal(1), 1), d) + self.assertEqual(c.compare(1, Decimal(1)), d) + + def test_compare_signal(self): + c = Context() + d = c.compare_signal(Decimal(1), Decimal(1)) + self.assertEqual(c.compare_signal(1, 1), d) + self.assertEqual(c.compare_signal(Decimal(1), 1), d) + self.assertEqual(c.compare_signal(1, Decimal(1)), d) + + def test_compare_total(self): + c = Context() + d = c.compare_total(Decimal(1), Decimal(1)) + self.assertEqual(c.compare_total(1, 1), d) + self.assertEqual(c.compare_total(Decimal(1), 1), d) + self.assertEqual(c.compare_total(1, Decimal(1)), d) + + def test_compare_total_mag(self): + c = Context() + d = c.compare_total_mag(Decimal(1), Decimal(1)) + self.assertEqual(c.compare_total_mag(1, 1), d) + self.assertEqual(c.compare_total_mag(Decimal(1), 1), d) + self.assertEqual(c.compare_total_mag(1, Decimal(1)), d) + + def test_divide(self): + c = Context() + d = c.divide(Decimal(1), Decimal(2)) + self.assertEqual(c.divide(1, 2), d) + self.assertEqual(c.divide(Decimal(1), 2), d) + self.assertEqual(c.divide(1, Decimal(2)), d) + + def test_divide_int(self): + c = Context() + d = c.divide_int(Decimal(1), Decimal(2)) + self.assertEqual(c.divide_int(1, 2), d) + self.assertEqual(c.divide_int(Decimal(1), 2), d) + self.assertEqual(c.divide_int(1, Decimal(2)), d) + + def test_fma(self): + c = Context() + d = c.fma(Decimal(2), Decimal(3), Decimal(4)) + self.assertEqual(c.fma(2, 3, 4), d) + self.assertEqual(c.fma(Decimal(2), 3, 4), d) + self.assertEqual(c.fma(2, Decimal(3), 4), d) + self.assertEqual(c.fma(2, 3, Decimal(4)), d) + self.assertEqual(c.fma(Decimal(2), Decimal(3), 4), d) + + def test_logical_and(self): + c = Context() + d = c.logical_and(Decimal(1), Decimal(1)) + self.assertEqual(c.logical_and(1, 1), d) + self.assertEqual(c.logical_and(Decimal(1), 1), d) + self.assertEqual(c.logical_and(1, Decimal(1)), d) + + def test_logical_or(self): + c = Context() + d = c.logical_or(Decimal(1), Decimal(1)) + self.assertEqual(c.logical_or(1, 1), d) + self.assertEqual(c.logical_or(Decimal(1), 1), d) + self.assertEqual(c.logical_or(1, Decimal(1)), d) + + def test_logical_xor(self): + c = Context() + d = c.logical_xor(Decimal(1), Decimal(1)) + self.assertEqual(c.logical_xor(1, 1), d) + self.assertEqual(c.logical_xor(Decimal(1), 1), d) + self.assertEqual(c.logical_xor(1, Decimal(1)), d) + + def test_max(self): + c = Context() + d = c.max(Decimal(1), Decimal(2)) + self.assertEqual(c.max(1, 2), d) + self.assertEqual(c.max(Decimal(1), 2), d) + self.assertEqual(c.max(1, Decimal(2)), d) + + def test_max_mag(self): + c = Context() + d = c.max_mag(Decimal(1), Decimal(2)) + self.assertEqual(c.max_mag(1, 2), d) + self.assertEqual(c.max_mag(Decimal(1), 2), d) + self.assertEqual(c.max_mag(1, Decimal(2)), d) + + def test_min(self): + c = Context() + d = c.min(Decimal(1), Decimal(2)) + self.assertEqual(c.min(1, 2), d) + self.assertEqual(c.min(Decimal(1), 2), d) + self.assertEqual(c.min(1, Decimal(2)), d) + + def test_min_mag(self): + c = Context() + d = c.min_mag(Decimal(1), Decimal(2)) + self.assertEqual(c.min_mag(1, 2), d) + self.assertEqual(c.min_mag(Decimal(1), 2), d) + self.assertEqual(c.min_mag(1, Decimal(2)), d) + + def test_multiply(self): + c = Context() + d = c.multiply(Decimal(1), Decimal(2)) + self.assertEqual(c.multiply(1, 2), d) + self.assertEqual(c.multiply(Decimal(1), 2), d) + self.assertEqual(c.multiply(1, Decimal(2)), d) + + def test_next_toward(self): + c = Context() + d = c.next_toward(Decimal(1), Decimal(2)) + self.assertEqual(c.next_toward(1, 2), d) + self.assertEqual(c.next_toward(Decimal(1), 2), d) + self.assertEqual(c.next_toward(1, Decimal(2)), d) + + def test_power(self): + c = Context() + d = c.power(Decimal(1), Decimal(4), Decimal(2)) + self.assertEqual(c.power(1, 4, 2), d) + self.assertEqual(c.power(Decimal(1), 4, 2), d) + self.assertEqual(c.power(1, Decimal(4), 2), d) + self.assertEqual(c.power(1, 4, Decimal(2)), d) + self.assertEqual(c.power(Decimal(1), Decimal(4), 2), d) + + def test_quantize(self): + c = Context() + d = c.quantize(Decimal(1), Decimal(2)) + self.assertEqual(c.quantize(1, 2), d) + self.assertEqual(c.quantize(Decimal(1), 2), d) + self.assertEqual(c.quantize(1, Decimal(2)), d) + + def test_remainder(self): + c = Context() + d = c.remainder(Decimal(1), Decimal(2)) + self.assertEqual(c.remainder(1, 2), d) + self.assertEqual(c.remainder(Decimal(1), 2), d) + self.assertEqual(c.remainder(1, Decimal(2)), d) + + def test_remainder_near(self): + c = Context() + d = c.remainder_near(Decimal(1), Decimal(2)) + self.assertEqual(c.remainder_near(1, 2), d) + self.assertEqual(c.remainder_near(Decimal(1), 2), d) + self.assertEqual(c.remainder_near(1, Decimal(2)), d) + + def test_rotate(self): + c = Context() + d = c.rotate(Decimal(1), Decimal(2)) + self.assertEqual(c.rotate(1, 2), d) + self.assertEqual(c.rotate(Decimal(1), 2), d) + self.assertEqual(c.rotate(1, Decimal(2)), d) + + def test_same_quantum(self): + c = Context() + d = c.same_quantum(Decimal(1), Decimal(2)) + self.assertEqual(c.same_quantum(1, 2), d) + self.assertEqual(c.same_quantum(Decimal(1), 2), d) + self.assertEqual(c.same_quantum(1, Decimal(2)), d) + + def test_scaleb(self): + c = Context() + d = c.scaleb(Decimal(1), Decimal(2)) + self.assertEqual(c.scaleb(1, 2), d) + self.assertEqual(c.scaleb(Decimal(1), 2), d) + self.assertEqual(c.scaleb(1, Decimal(2)), d) + + def test_shift(self): + c = Context() + d = c.shift(Decimal(1), Decimal(2)) + self.assertEqual(c.shift(1, 2), d) + self.assertEqual(c.shift(Decimal(1), 2), d) + self.assertEqual(c.shift(1, Decimal(2)), d) + + def test_subtract(self): + c = Context() + d = c.subtract(Decimal(1), Decimal(2)) + self.assertEqual(c.subtract(1, 2), d) + self.assertEqual(c.subtract(Decimal(1), 2), d) + self.assertEqual(c.subtract(1, Decimal(2)), d) + class WithStatementTest(unittest.TestCase): # Can't do these as docstrings until Python 2.6 # as doctest can't handle __future__ statements