diff options
author | Victor Stinner <vstinner@python.org> | 2020-02-07 22:42:51 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-02-07 22:42:51 (GMT) |
commit | dc7a50d73a3d16918529669ff7b8783c08cff090 (patch) | |
tree | 03dd057164d756447338063473d5559a1fc9c957 /Lib/fractions.py | |
parent | 60ac6ed5579f6666130fc264d3b748ee9575e3aa (diff) | |
download | cpython-dc7a50d73a3d16918529669ff7b8783c08cff090.zip cpython-dc7a50d73a3d16918529669ff7b8783c08cff090.tar.gz cpython-dc7a50d73a3d16918529669ff7b8783c08cff090.tar.bz2 |
bpo-39350: Fix fractions for int subclasses (GH-18375)
Fix regression in fractions.Fraction if the numerator and/or the
denominator is an int subclass. The math.gcd() function is now
used to normalize the numerator and denominator. math.gcd() always
return a int type. Previously, the GCD type depended on numerator
and denominator.
Diffstat (limited to 'Lib/fractions.py')
-rw-r--r-- | Lib/fractions.py | 10 |
1 files changed, 3 insertions, 7 deletions
diff --git a/Lib/fractions.py b/Lib/fractions.py index f5a8544..de3e23b 100644 --- a/Lib/fractions.py +++ b/Lib/fractions.py @@ -155,13 +155,9 @@ class Fraction(numbers.Rational): if denominator == 0: raise ZeroDivisionError('Fraction(%s, 0)' % numerator) if _normalize: - if type(numerator) is int is type(denominator): - # *very* normal case - g = math.gcd(numerator, denominator) - if denominator < 0: - g = -g - else: - g = _gcd(numerator, denominator) + g = math.gcd(numerator, denominator) + if denominator < 0: + g = -g numerator //= g denominator //= g self._numerator = numerator |