Author: Amaury Forgeot d'Arc <[email protected]>
Branch: py3k
Changeset: r58392:53ce3af8e70b
Date: 2012-10-24 00:49 +0200
http://bitbucket.org/pypy/pypy/changeset/53ce3af8e70b/

Log:    Like CPython, avoid too large factorials.

diff --git a/pypy/module/math/app_math.py b/pypy/module/math/app_math.py
--- a/pypy/module/math/app_math.py
+++ b/pypy/module/math/app_math.py
@@ -1,3 +1,5 @@
+import sys
+
 def factorial(x):
     """Find x!."""
     if isinstance(x, float):
@@ -5,6 +7,8 @@
         if fl != x:
             raise ValueError("float arguments must be integral")
         x = fl
+    if x > sys.maxsize:
+        raise OverflowError("Too large for a factorial")
     if x < 0:
         raise ValueError("x must be >= 0")
     res = 1
diff --git a/pypy/module/math/test/test_math.py 
b/pypy/module/math/test/test_math.py
--- a/pypy/module/math/test/test_math.py
+++ b/pypy/module/math/test/test_math.py
@@ -89,7 +89,7 @@
             assert actual == expected
 
     def test_factorial(self):
-        import math
+        import math, sys
         assert math.factorial(0) == 1
         assert math.factorial(1) == 1
         assert math.factorial(2) == 2
@@ -98,6 +98,8 @@
         raises(ValueError, math.factorial, -1)
         raises(ValueError, math.factorial, -1.)
         raises(ValueError, math.factorial, 1.1)
+        raises(OverflowError, math.factorial, sys.maxsize+1)
+        raises(OverflowError, math.factorial, 10e100)
 
     def test_log1p(self):
         import math
_______________________________________________
pypy-commit mailing list
[email protected]
http://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to