def _temporary_exponent_bounds(emin, emax): old_emin = mpfr.mpfr_get_emin() mpfr.mpfr_set_emin(emin) try: old_emax = mpfr.mpfr_get_emax() mpfr.mpfr_set_emax(emax) try: yield finally: mpfr.mpfr_set_emax(old_emax) finally: mpfr.mpfr_set_emin(old_emin)
def test__temporary_exponent_bounds(self): # Failed calls to _temporary_exponent_bounds shouldn't affect emin or # emax. original_emin = mpfr.mpfr_get_emin() original_emax = mpfr.mpfr_get_emax() # Call to _temporary_exponent_bounds should restore original values. with _temporary_exponent_bounds(-10, 10): self.assertEqual(mpfr.mpfr_get_emin(), -10) self.assertEqual(mpfr.mpfr_get_emax(), 10) self.assertEqual(mpfr.mpfr_get_emin(), original_emin) self.assertEqual(mpfr.mpfr_get_emax(), original_emax) # Even erroneous calls should restore originals. with self.assertRaises(OverflowError): with _temporary_exponent_bounds(-10, 10 ** 100): pass self.assertEqual(mpfr.mpfr_get_emin(), original_emin) self.assertEqual(mpfr.mpfr_get_emax(), original_emax) with self.assertRaises(OverflowError): with _temporary_exponent_bounds(-10 ** 100, 10): pass self.assertEqual(mpfr.mpfr_get_emin(), original_emin) self.assertEqual(mpfr.mpfr_get_emax(), original_emax) with self.assertRaises(OverflowError): with _temporary_exponent_bounds(-10 ** 100, 10 ** 100): pass self.assertEqual(mpfr.mpfr_get_emin(), original_emin) self.assertEqual(mpfr.mpfr_get_emax(), original_emax)
def test__temporary_exponent_bounds(self): # Failed calls to _temporary_exponent_bounds shouldn't affect emin or # emax. original_emin = mpfr.mpfr_get_emin() original_emax = mpfr.mpfr_get_emax() # Call to _temporary_exponent_bounds should restore original values. with _temporary_exponent_bounds(-10, 10): self.assertEqual(mpfr.mpfr_get_emin(), -10) self.assertEqual(mpfr.mpfr_get_emax(), 10) self.assertEqual(mpfr.mpfr_get_emin(), original_emin) self.assertEqual(mpfr.mpfr_get_emax(), original_emax) # Even erroneous calls should restore originals. with self.assertRaises(OverflowError): with _temporary_exponent_bounds(-10, 10**100): pass self.assertEqual(mpfr.mpfr_get_emin(), original_emin) self.assertEqual(mpfr.mpfr_get_emax(), original_emax) with self.assertRaises(OverflowError): with _temporary_exponent_bounds(-10**100, 10): pass self.assertEqual(mpfr.mpfr_get_emin(), original_emin) self.assertEqual(mpfr.mpfr_get_emax(), original_emax) with self.assertRaises(OverflowError): with _temporary_exponent_bounds(-10**100, 10**100): pass self.assertEqual(mpfr.mpfr_get_emin(), original_emin) self.assertEqual(mpfr.mpfr_get_emax(), original_emax)