[Python-checkins] bpo-39350: Remove deprecated fractions.gcd() (GH-18021)

Victor Stinner webhook-mailer at python.org
Thu Jan 16 05:02:59 EST 2020


https://github.com/python/cpython/commit/4691a2f2a2b8174a6c958ce6976ed5f3354c9504
commit: 4691a2f2a2b8174a6c958ce6976ed5f3354c9504
branch: master
author: Victor Stinner <vstinner at python.org>
committer: GitHub <noreply at github.com>
date: 2020-01-16T11:02:51+01:00
summary:

bpo-39350: Remove deprecated fractions.gcd() (GH-18021)

Remove fractions.gcd() function, deprecated since Python 3.5
(bpo-22486): use math.gcd() instead.

files:
A Misc/NEWS.d/next/Library/2020-01-16-09-15-40.bpo-39350.ZQx0uY.rst
M Doc/library/fractions.rst
M Doc/whatsnew/3.9.rst
M Lib/fractions.py
M Lib/test/test_fractions.py

diff --git a/Doc/library/fractions.rst b/Doc/library/fractions.rst
index 58e7126b0bf21..d3a42762e3ff8 100644
--- a/Doc/library/fractions.rst
+++ b/Doc/library/fractions.rst
@@ -172,18 +172,6 @@ another rational number, or from a string.
       method can also be accessed through the :func:`round` function.
 
 
-.. function:: gcd(a, b)
-
-   Return the greatest common divisor of the integers *a* and *b*.  If either
-   *a* or *b* is nonzero, then the absolute value of ``gcd(a, b)`` is the
-   largest integer that divides both *a* and *b*.  ``gcd(a,b)`` has the same
-   sign as *b* if *b* is nonzero; otherwise it takes the sign of *a*.  ``gcd(0,
-   0)`` returns ``0``.
-
-   .. deprecated:: 3.5
-      Use :func:`math.gcd` instead.
-
-
 .. seealso::
 
    Module :mod:`numbers`
diff --git a/Doc/whatsnew/3.9.rst b/Doc/whatsnew/3.9.rst
index 47e8a37e56c32..8ca755645d66f 100644
--- a/Doc/whatsnew/3.9.rst
+++ b/Doc/whatsnew/3.9.rst
@@ -416,6 +416,10 @@ Removed
   :func:`base64.decodebytes` instead.
   (Contributed by Victor Stinner in :issue:`39351`.)
 
+* ``fractions.gcd()`` function has been removed, it was deprecated since Python
+  3.5 (:issue:`22486`): use :func:`math.gcd` instead.
+  (Contributed by Victor Stinner in :issue:`39350`.)
+
 
 Porting to Python 3.9
 =====================
diff --git a/Lib/fractions.py b/Lib/fractions.py
index 2e7047a81844d..501f4b74a0b7a 100644
--- a/Lib/fractions.py
+++ b/Lib/fractions.py
@@ -10,31 +10,9 @@
 import re
 import sys
 
-__all__ = ['Fraction', 'gcd']
+__all__ = ['Fraction']
 
 
-
-def gcd(a, b):
-    """Calculate the Greatest Common Divisor of a and b.
-
-    Unless b==0, the result will have the same sign as b (so that when
-    b is divided by it, the result comes out positive).
-    """
-    import warnings
-    warnings.warn('fractions.gcd() is deprecated. Use math.gcd() instead.',
-                  DeprecationWarning, 2)
-    if type(a) is int is type(b):
-        if (b or a) < 0:
-            return -math.gcd(a, b)
-        return math.gcd(a, b)
-    return _gcd(a, b)
-
-def _gcd(a, b):
-    # Supports non-integers for backward compatibility.
-    while b:
-        a, b = b, a%b
-    return a
-
 # Constants related to the hash implementation;  hash(x) is based
 # on the reduction of x modulo the prime _PyHASH_MODULUS.
 _PyHASH_MODULUS = sys.hash_info.modulus
diff --git a/Lib/test/test_fractions.py b/Lib/test/test_fractions.py
index 18ab28cfebe0c..7cf7899932b34 100644
--- a/Lib/test/test_fractions.py
+++ b/Lib/test/test_fractions.py
@@ -12,7 +12,7 @@
 from copy import copy, deepcopy
 from pickle import dumps, loads
 F = fractions.Fraction
-gcd = fractions.gcd
+
 
 class DummyFloat(object):
     """Dummy float class for testing comparisons with Fractions"""
@@ -81,30 +81,6 @@ def __float__(self):
 class DummyFraction(fractions.Fraction):
     """Dummy Fraction subclass for copy and deepcopy testing."""
 
-class GcdTest(unittest.TestCase):
-
-    def testMisc(self):
-        # fractions.gcd() is deprecated
-        with self.assertWarnsRegex(DeprecationWarning, r'fractions\.gcd'):
-            gcd(1, 1)
-        with warnings.catch_warnings():
-            warnings.filterwarnings('ignore', r'fractions\.gcd',
-                                    DeprecationWarning)
-            self.assertEqual(0, gcd(0, 0))
-            self.assertEqual(1, gcd(1, 0))
-            self.assertEqual(-1, gcd(-1, 0))
-            self.assertEqual(1, gcd(0, 1))
-            self.assertEqual(-1, gcd(0, -1))
-            self.assertEqual(1, gcd(7, 1))
-            self.assertEqual(-1, gcd(7, -1))
-            self.assertEqual(1, gcd(-23, 15))
-            self.assertEqual(12, gcd(120, 84))
-            self.assertEqual(-12, gcd(84, -120))
-            self.assertEqual(gcd(120.0, 84), 12.0)
-            self.assertEqual(gcd(120, 84.0), 12.0)
-            self.assertEqual(gcd(F(120), F(84)), F(12))
-            self.assertEqual(gcd(F(120, 77), F(84, 55)), F(12, 385))
-
 
 def _components(r):
     return (r.numerator, r.denominator)
@@ -690,5 +666,6 @@ def test_slots(self):
         r = F(13, 7)
         self.assertRaises(AttributeError, setattr, r, 'a', 10)
 
+
 if __name__ == '__main__':
     unittest.main()
diff --git a/Misc/NEWS.d/next/Library/2020-01-16-09-15-40.bpo-39350.ZQx0uY.rst b/Misc/NEWS.d/next/Library/2020-01-16-09-15-40.bpo-39350.ZQx0uY.rst
new file mode 100644
index 0000000000000..264e52fdc518b
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2020-01-16-09-15-40.bpo-39350.ZQx0uY.rst
@@ -0,0 +1,2 @@
+Remove ``fractions.gcd()`` function, deprecated since Python 3.5
+(:issue:`22486`): use :func:`math.gcd` instead.



More information about the Python-checkins mailing list