@@ -35,24 +35,17 @@ cdef extern from *:
3535 cdef long long PY_LLONG_MIN, PY_LLONG_MAX
3636 cdef long long MAX_SMALL_NUMBER " (PY_LLONG_MAX / 100)"
3737
38- cdef object Rational, Integral, Real, Complex, Decimal, math, operator, re, sys
38+ cdef object Rational, Integral, Real, Complex, Decimal, math, operator, re
3939cdef object PY_MAX_LONG_LONG = PY_LLONG_MAX
4040
4141from numbers import Rational, Integral, Real, Complex
4242from decimal import Decimal
4343import math
4444import operator
4545import re
46- import sys
4746
48- cdef bint _decimal_supports_integer_ratio = hasattr (Decimal, " as_integer_ratio" ) # Py3.6+
4947cdef object _operator_index = operator.index
50- cdef object math_gcd
51- try :
52- math_gcd = math.gcd
53- except AttributeError :
54- pass
55-
48+ cdef object math_gcd = math.gcd
5649
5750# Cache widely used 10**x int objects.
5851# Py3.12/Ubuntu64: sys.getsizeof(tuple[58]) == 512 bytes, tuple[91] == 768, tuple[123] == 1024
@@ -264,21 +257,15 @@ cdef _gcd_fallback(a, b):
264257# Constants related to the hash implementation; hash(x) is based
265258# on the reduction of x modulo the prime _PyHASH_MODULUS.
266259
267- cdef Py_hash_t _PyHASH_MODULUS
268- try :
269- _PyHASH_MODULUS = sys.hash_info.modulus
270- except AttributeError : # pre Py3.2
271- # adapted from pyhash.h in Py3.4
272- _PyHASH_MODULUS = (< Py_hash_t> 1 ) << (61 if sizeof(Py_hash_t) >= 8 else 31 ) - 1
260+ from sys import hash_info
273261
262+ cdef Py_hash_t _PyHASH_MODULUS = hash_info.modulus
274263
275264# Value to be used for rationals that reduce to infinity modulo
276265# _PyHASH_MODULUS.
277- cdef Py_hash_t _PyHASH_INF
278- try :
279- _PyHASH_INF = sys.hash_info.inf
280- except AttributeError : # pre Py3.2
281- _PyHASH_INF = hash (float (' +inf' ))
266+ cdef Py_hash_t _PyHASH_INF = hash_info.inf
267+
268+ del hash_info
282269
283270
284271# Helpers for formatting
@@ -471,13 +458,7 @@ cdef class Fraction:
471458 return
472459
473460 elif isinstance (numerator, Decimal):
474- if _decimal_supports_integer_ratio:
475- # Exact conversion
476- self ._numerator, self ._denominator = numerator.as_integer_ratio()
477- else :
478- value = Fraction.from_decimal(numerator)
479- self ._numerator = (< Fraction> value)._numerator
480- self ._denominator = (< Fraction> value)._denominator
461+ self ._numerator, self ._denominator = numerator.as_integer_ratio()
481462 return
482463
483464 else :
@@ -590,18 +571,8 @@ cdef class Fraction:
590571 if dec.is_nan():
591572 raise ValueError (f" Cannot convert {dec} to {cls.__name__}." )
592573
593- if _decimal_supports_integer_ratio:
594- num, denom = dec.as_integer_ratio()
595- return _fraction_from_coprime_ints(num, denom, cls )
596-
597- sign, digits, exp = dec.as_tuple()
598- digits = int (' ' .join(map (str , digits)))
599- if sign:
600- digits = - digits
601- if exp >= 0 :
602- return _fraction_from_coprime_ints(digits * pow10(exp), 1 , cls )
603- else :
604- return cls (digits, pow10(- exp))
574+ num, denom = dec.as_integer_ratio()
575+ return _fraction_from_coprime_ints(num, denom, cls )
605576
606577 def is_integer (self ):
607578 """ Return True if the Fraction is an integer."""
0 commit comments