Author: Carl Friedrich Bolz-Tereick <[email protected]>
Branch: py3.6
Changeset: r96965:287ec611c0dc
Date: 2019-07-11 11:25 +0200
http://bitbucket.org/pypy/pypy/changeset/287ec611c0dc/

Log:    (cfbolz, Luanna): fix subtle bug I introduced: abs(-sys.maxint-1)
        overflows, the the bigint version of the code needs to be used for
        it. Thanks Luanna for pointing that out.

diff --git a/pypy/module/math/interp_math.py b/pypy/module/math/interp_math.py
--- a/pypy/module/math/interp_math.py
+++ b/pypy/module/math/interp_math.py
@@ -489,13 +489,11 @@
 def gcd(space, w_a, w_b):
     """greatest common divisor of a and b"""
     from rpython.rlib import rbigint
-    w_a = space.index(w_a)
-    w_b = space.index(w_b)
+    w_a = space.abs(space.index(w_a))
+    w_b = space.abs(space.index(w_b))
     try:
         a = space.int_w(w_a)
         b = space.int_w(w_b)
-        g = rbigint.gcd_binary(a, b)
-        return space.newint(g)
     except OperationError as e:
         if not e.match(space, space.w_OverflowError):
             raise
@@ -504,3 +502,6 @@
         b = space.bigint_w(w_b)
         g = a.gcd(b)
         return space.newlong_from_rbigint(g)
+    else:
+        g = rbigint.gcd_binary(a, b)
+        return space.newint(g)
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
@@ -1,6 +1,5 @@
-from __future__ import with_statement
-
 import py
+import sys
 from pypy.interpreter.function import Function
 from pypy.interpreter.gateway import BuiltinCode
 from pypy.module.math.test import test_direct
@@ -18,6 +17,7 @@
             filename = filename[:-1]
         space = cls.space
         cls.w_math_cases = space.wrap(filename)
+        cls.w_maxint = space.wrap(sys.maxint)
 
     @classmethod
     def make_callable_wrapper(cls, func):
@@ -374,6 +374,9 @@
         assert math.gcd(-3**10*5**20*11**8, 2**5*3**5*7**20) == 3**5
         assert math.gcd(64, 200) == 8
 
+        assert math.gcd(-self.maxint-1, 3) == 1
+        assert math.gcd(-self.maxint-1, -self.maxint-1) == self.maxint+1
+
     def test_inf_nan(self):
         import math
         assert math.isinf(math.inf)
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to