From b8ae9b99bf7bd59309b04fe8692bd156c91b8da4 Mon Sep 17 00:00:00 2001 From: Mark Dickinson Date: Sun, 2 Feb 2020 10:01:13 +0000 Subject: [PATCH 1/3] Fix regression introduced when fractions.gcd was removed --- Lib/fractions.py | 6 ++++++ Lib/test/test_fractions.py | 18 ++++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/Lib/fractions.py b/Lib/fractions.py index 501f4b74a0b7ac..59833ab3755276 100644 --- a/Lib/fractions.py +++ b/Lib/fractions.py @@ -13,6 +13,12 @@ __all__ = ['Fraction'] +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 7cf7899932b34d..a5e0bfd875a589 100644 --- a/Lib/test/test_fractions.py +++ b/Lib/test/test_fractions.py @@ -666,6 +666,24 @@ def test_slots(self): r = F(13, 7) self.assertRaises(AttributeError, setattr, r, 'a', 10) + def test_gcd_compatibility_branch(self): + # Regression test for bugs.python.org/issue39350 + + class myint(int): + def __mul__(self, other): + return myint(int(self)*int(other)) + + @property + def numerator(self): + return self + + @property + def denominator(self): + return myint(1) + + myhalf = F(myint(1), myint(2)) + self.assertEqual(myhalf, F(1, 2)) + if __name__ == '__main__': unittest.main() From f634278480f13d34260a8870efa73c92aa417c82 Mon Sep 17 00:00:00 2001 From: Mark Dickinson Date: Sun, 2 Feb 2020 10:39:12 +0000 Subject: [PATCH 2/3] Add news entry. --- .../next/Library/2020-02-02-10-38-59.bpo-39350.8V69hi.rst | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 Misc/NEWS.d/next/Library/2020-02-02-10-38-59.bpo-39350.8V69hi.rst diff --git a/Misc/NEWS.d/next/Library/2020-02-02-10-38-59.bpo-39350.8V69hi.rst b/Misc/NEWS.d/next/Library/2020-02-02-10-38-59.bpo-39350.8V69hi.rst new file mode 100644 index 00000000000000..a0793ae10543d8 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2020-02-02-10-38-59.bpo-39350.8V69hi.rst @@ -0,0 +1,3 @@ +Revert removal of ``fractions._gcd``. That removal caused breakage when +using ``fractions.Fraction`` with integer-like values (for example NumPy +integers). From 2f0db20e2626b2b006223dabab17b0c5ef6d1ed5 Mon Sep 17 00:00:00 2001 From: Mark Dickinson Date: Sun, 2 Feb 2020 11:44:44 +0000 Subject: [PATCH 3/3] Fix indentation --- Lib/fractions.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Lib/fractions.py b/Lib/fractions.py index 59833ab3755276..1f68ea833693db 100644 --- a/Lib/fractions.py +++ b/Lib/fractions.py @@ -14,10 +14,10 @@ def _gcd(a, b): - # Supports non-integers for backward compatibility. - while b: - a, b = b, a%b - return a + # 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.