Skip to content

Commit 4691a2f

Browse files
authored
bpo-39350: Remove deprecated fractions.gcd() (GH-18021)
Remove fractions.gcd() function, deprecated since Python 3.5 (bpo-22486): use math.gcd() instead.
1 parent 210c19e commit 4691a2f

File tree

5 files changed

+9
-60
lines changed

5 files changed

+9
-60
lines changed

Doc/library/fractions.rst

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -172,18 +172,6 @@ another rational number, or from a string.
172172
method can also be accessed through the :func:`round` function.
173173

174174

175-
.. function:: gcd(a, b)
176-
177-
Return the greatest common divisor of the integers *a* and *b*. If either
178-
*a* or *b* is nonzero, then the absolute value of ``gcd(a, b)`` is the
179-
largest integer that divides both *a* and *b*. ``gcd(a,b)`` has the same
180-
sign as *b* if *b* is nonzero; otherwise it takes the sign of *a*. ``gcd(0,
181-
0)`` returns ``0``.
182-
183-
.. deprecated:: 3.5
184-
Use :func:`math.gcd` instead.
185-
186-
187175
.. seealso::
188176

189177
Module :mod:`numbers`

Doc/whatsnew/3.9.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -416,6 +416,10 @@ Removed
416416
:func:`base64.decodebytes` instead.
417417
(Contributed by Victor Stinner in :issue:`39351`.)
418418

419+
* ``fractions.gcd()`` function has been removed, it was deprecated since Python
420+
3.5 (:issue:`22486`): use :func:`math.gcd` instead.
421+
(Contributed by Victor Stinner in :issue:`39350`.)
422+
419423

420424
Porting to Python 3.9
421425
=====================

Lib/fractions.py

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -10,31 +10,9 @@
1010
import re
1111
import sys
1212

13-
__all__ = ['Fraction', 'gcd']
13+
__all__ = ['Fraction']
1414

1515

16-
17-
def gcd(a, b):
18-
"""Calculate the Greatest Common Divisor of a and b.
19-
20-
Unless b==0, the result will have the same sign as b (so that when
21-
b is divided by it, the result comes out positive).
22-
"""
23-
import warnings
24-
warnings.warn('fractions.gcd() is deprecated. Use math.gcd() instead.',
25-
DeprecationWarning, 2)
26-
if type(a) is int is type(b):
27-
if (b or a) < 0:
28-
return -math.gcd(a, b)
29-
return math.gcd(a, b)
30-
return _gcd(a, b)
31-
32-
def _gcd(a, b):
33-
# Supports non-integers for backward compatibility.
34-
while b:
35-
a, b = b, a%b
36-
return a
37-
3816
# Constants related to the hash implementation; hash(x) is based
3917
# on the reduction of x modulo the prime _PyHASH_MODULUS.
4018
_PyHASH_MODULUS = sys.hash_info.modulus

Lib/test/test_fractions.py

Lines changed: 2 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
from copy import copy, deepcopy
1313
from pickle import dumps, loads
1414
F = fractions.Fraction
15-
gcd = fractions.gcd
15+
1616

1717
class DummyFloat(object):
1818
"""Dummy float class for testing comparisons with Fractions"""
@@ -81,30 +81,6 @@ def __float__(self):
8181
class DummyFraction(fractions.Fraction):
8282
"""Dummy Fraction subclass for copy and deepcopy testing."""
8383

84-
class GcdTest(unittest.TestCase):
85-
86-
def testMisc(self):
87-
# fractions.gcd() is deprecated
88-
with self.assertWarnsRegex(DeprecationWarning, r'fractions\.gcd'):
89-
gcd(1, 1)
90-
with warnings.catch_warnings():
91-
warnings.filterwarnings('ignore', r'fractions\.gcd',
92-
DeprecationWarning)
93-
self.assertEqual(0, gcd(0, 0))
94-
self.assertEqual(1, gcd(1, 0))
95-
self.assertEqual(-1, gcd(-1, 0))
96-
self.assertEqual(1, gcd(0, 1))
97-
self.assertEqual(-1, gcd(0, -1))
98-
self.assertEqual(1, gcd(7, 1))
99-
self.assertEqual(-1, gcd(7, -1))
100-
self.assertEqual(1, gcd(-23, 15))
101-
self.assertEqual(12, gcd(120, 84))
102-
self.assertEqual(-12, gcd(84, -120))
103-
self.assertEqual(gcd(120.0, 84), 12.0)
104-
self.assertEqual(gcd(120, 84.0), 12.0)
105-
self.assertEqual(gcd(F(120), F(84)), F(12))
106-
self.assertEqual(gcd(F(120, 77), F(84, 55)), F(12, 385))
107-
10884

10985
def _components(r):
11086
return (r.numerator, r.denominator)
@@ -690,5 +666,6 @@ def test_slots(self):
690666
r = F(13, 7)
691667
self.assertRaises(AttributeError, setattr, r, 'a', 10)
692668

669+
693670
if __name__ == '__main__':
694671
unittest.main()
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Remove ``fractions.gcd()`` function, deprecated since Python 3.5
2+
(:issue:`22486`): use :func:`math.gcd` instead.

0 commit comments

Comments
 (0)