gh-72902: improve Fraction constructor speed for typical inputs (GH-134320)
This moves abc check for numbers.Rational - down.
This commit is contained in:
parent
e007e62ba7
commit
175ba3639f
@ -238,11 +238,6 @@ class Fraction(numbers.Rational):
|
|||||||
self._denominator = 1
|
self._denominator = 1
|
||||||
return self
|
return self
|
||||||
|
|
||||||
elif isinstance(numerator, numbers.Rational):
|
|
||||||
self._numerator = numerator.numerator
|
|
||||||
self._denominator = numerator.denominator
|
|
||||||
return self
|
|
||||||
|
|
||||||
elif (isinstance(numerator, float) or
|
elif (isinstance(numerator, float) or
|
||||||
(not isinstance(numerator, type) and
|
(not isinstance(numerator, type) and
|
||||||
hasattr(numerator, 'as_integer_ratio'))):
|
hasattr(numerator, 'as_integer_ratio'))):
|
||||||
@ -278,6 +273,11 @@ class Fraction(numbers.Rational):
|
|||||||
if m.group('sign') == '-':
|
if m.group('sign') == '-':
|
||||||
numerator = -numerator
|
numerator = -numerator
|
||||||
|
|
||||||
|
elif isinstance(numerator, numbers.Rational):
|
||||||
|
self._numerator = numerator.numerator
|
||||||
|
self._denominator = numerator.denominator
|
||||||
|
return self
|
||||||
|
|
||||||
else:
|
else:
|
||||||
raise TypeError("argument should be a string or a Rational "
|
raise TypeError("argument should be a string or a Rational "
|
||||||
"instance or have the as_integer_ratio() method")
|
"instance or have the as_integer_ratio() method")
|
||||||
|
@ -0,0 +1,2 @@
|
|||||||
|
Improve speed (x1.1-1.8) of the :class:`~fractions.Fraction` constructor for
|
||||||
|
typical inputs (:class:`float`'s, :class:`~decimal.Decimal`'s or strings).
|
Loading…
x
Reference in New Issue
Block a user