https://github.com/python/cpython/commit/175ba3639fbc336912f429cf4de43b22e05fd6dc
commit: 175ba3639fbc336912f429cf4de43b22e05fd6dc
branch: main
author: Sergey B Kirpichev <[email protected]>
committer: serhiy-storchaka <[email protected]>
date: 2025-05-20T12:47:27+03:00
summary:
gh-72902: improve Fraction constructor speed for typical inputs (GH-134320)
This moves abc check for numbers.Rational - down.
files:
A Misc/NEWS.d/next/Library/2025-05-20-11-35-08.gh-issue-72902.jzEI-E.rst
M Lib/fractions.py
diff --git a/Lib/fractions.py b/Lib/fractions.py
index 8163e3bb594f6b..063f28478c7338 100644
--- a/Lib/fractions.py
+++ b/Lib/fractions.py
@@ -238,11 +238,6 @@ def __new__(cls, numerator=0, denominator=None):
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 @@ def __new__(cls, numerator=0, denominator=None):
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")
diff --git
a/Misc/NEWS.d/next/Library/2025-05-20-11-35-08.gh-issue-72902.jzEI-E.rst
b/Misc/NEWS.d/next/Library/2025-05-20-11-35-08.gh-issue-72902.jzEI-E.rst
new file mode 100644
index 00000000000000..932e751a32a043
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2025-05-20-11-35-08.gh-issue-72902.jzEI-E.rst
@@ -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).
_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-checkins.python.org/
Member address: [email protected]