diff --git a/Lib/fractions.py b/Lib/fractions.py index 501f4b74a0b7ac..1f68ea833693db 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() 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).