Compare commits

...
3 Commits
Author SHA1 Message Date
Christopher Smith c2bfcc4881 Merge pull request #16744 from smichr/16738
reject nan, oo, -oo at Rational instantiation
2019-04-27 20:34:27 -05:00
Christopher Smith c213d1f0fe reject nan, oo, n at Rational instantiation
Integer('10.5') -> TypeError like all other bad input to Integer;
a ValueError, however, can be returned from as_int.
2019-04-27 14:09:42 -05:00
Christopher Smith 2ab32186c1 reject nan, oo, n at Rational instantiation 2019-04-27 09:37:29 -05:00
2 changed files with 16 additions and 5 deletions
+6 -4
View File
@@ -1477,6 +1477,7 @@ class Rational(Number):
@cacheit
def __new__(cls, p, q=None, gcd=None):
from sympy.polys.polyutils import _not_a_coeff
if q is None:
if isinstance(p, Rational):
return p
@@ -1484,10 +1485,11 @@ class Rational(Number):
if isinstance(p, SYMPY_INTS):
pass
else:
if isinstance(p, (float, Float)):
if _not_a_coeff(p): # nan, oo, -oo
pass # error will raise below
elif isinstance(p, (float, Float)):
return Rational(*_as_integer_ratio(p))
if not isinstance(p, string_types):
elif not isinstance(p, string_types):
try:
p = sympify(p)
except (SympifyError, SyntaxError):
@@ -1955,7 +1957,7 @@ class Integer(Rational):
# be made into an int or whether an error should be raised.
try:
ival = int(i)
except TypeError:
except (TypeError, ValueError, OverflowError):
raise TypeError(
"Argument of Integer should be of numeric type, got %s." % i)
# We only work with well-behaved integer types. This converts, for
+10 -1
View File
@@ -176,6 +176,7 @@ def test_divmod():
assert divmod(S(4), S(-2.1)) == divmod(4, -2.1)
assert divmod(S(-8), S(-2.5) ) == Tuple(3 , -0.5)
def test_igcd():
assert igcd(0, 0) == 0
assert igcd(0, 1) == 1
@@ -218,6 +219,7 @@ def test_igcd_lehmer():
# swapping argmument
assert igcd_lehmer(1, 2) == igcd_lehmer(2, 1)
def test_igcd2():
# short loop
assert igcd2(2**100 - 1, 2**99 - 1) == 1
@@ -225,6 +227,7 @@ def test_igcd2():
a, b = int(fibonacci(10001)), int(fibonacci(10000))
assert igcd2(a, b) == 1
def test_ilcm():
assert ilcm(0, 0) == 0
assert ilcm(1, 0) == 0
@@ -271,6 +274,12 @@ def _test_rational_new(cls):
assert _strictly_equal(i, cls(i))
raises(TypeError, lambda: cls(Symbol('x')))
raises(TypeError, lambda: cls(float('nan')))
raises(TypeError, lambda: cls(float('inf')))
raises(TypeError, lambda: cls(float('-inf')))
raises(TypeError, lambda: cls(S.NaN))
raises(TypeError, lambda: cls(oo))
raises(TypeError, lambda: cls(-oo))
def test_Integer_new():
@@ -281,7 +290,7 @@ def test_Integer_new():
assert _strictly_equal(Integer(0.9), S.Zero)
assert _strictly_equal(Integer(10.5), Integer(10))
raises(ValueError, lambda: Integer("10.5"))
raises(TypeError, lambda: Integer("10.5")) # as_int will raise ValueError
assert Integer(Rational('1.' + '9'*20)) == 1