mirror of
https://github.com/python/cpython.git
synced 2025-07-07 19:35:27 +00:00
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
2 changed files with 7 additions and 5 deletions
|
@ -238,11 +238,6 @@ class Fraction(numbers.Rational):
|
|||
self._denominator = 1
|
||||
return self
|
||||
|
||||
elif isinstance(numerator, numbers.Rational):
|
||||
self._numerator = numerator.numerator
|
||||
self._denominator = numerator.denominator
|
||||
return self
|
||||
|
||||
elif (isinstance(numerator, float) or
|
||||
(not isinstance(numerator, type) and
|
||||
hasattr(numerator, 'as_integer_ratio'))):
|
||||
|
@ -278,6 +273,11 @@ class Fraction(numbers.Rational):
|
|||
if m.group('sign') == '-':
|
||||
numerator = -numerator
|
||||
|
||||
elif isinstance(numerator, numbers.Rational):
|
||||
self._numerator = numerator.numerator
|
||||
self._denominator = numerator.denominator
|
||||
return self
|
||||
|
||||
else:
|
||||
raise TypeError("argument should be a string or a Rational "
|
||||
"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…
Add table
Add a link
Reference in a new issue