@@ -79,9 +79,6 @@ def mpf_convert_arg(cls, x, prec, rounding):
7979 if isinstance (x , int_types ): return from_int (x )
8080 if isinstance (x , float ): return from_float (x )
8181 if isinstance (x , cls .context .constant ): return x .func (prec , rounding )
82- if isinstance (x , numbers .Rational ): return from_rational (x .numerator ,
83- x .denominator ,
84- prec , rounding )
8582 if hasattr (x , '_mpf_' ): return x ._mpf_
8683 if hasattr (x , '_mpmath_' ):
8784 t = cls .context .convert (x ._mpmath_ (prec , rounding ))
@@ -92,6 +89,9 @@ def mpf_convert_arg(cls, x, prec, rounding):
9289 if a == b :
9390 return a
9491 raise ValueError ("can only create mpf from zero-width interval" )
92+ if isinstance (x , numbers .Rational ): return from_rational (x .numerator ,
93+ x .denominator ,
94+ prec , rounding )
9595 if type (x ).__module__ == 'decimal' :
9696 return from_Decimal (x , prec , rounding )
9797 raise TypeError ("cannot create mpf from " + repr (x ))
@@ -680,6 +680,10 @@ def convert(ctx, x, strings=True):
680680 return ctx .make_mpc ((from_float (x .real ), from_float (x .imag )))
681681 if type (x ).__module__ == 'numpy' : return ctx .npconvert (x )
682682 prec , rounding = ctx ._prec_rounding
683+ if hasattr (x , '_mpf_' ): return ctx .make_mpf (x ._mpf_ )
684+ if hasattr (x , '_mpc_' ): return ctx .make_mpc (x ._mpc_ )
685+ if hasattr (x , '_mpmath_' ):
686+ return ctx .convert (x ._mpmath_ (prec , rounding ))
683687 if isinstance (x , numbers .Rational ):
684688 p , q = x .numerator , x .denominator
685689 return ctx .make_mpf (from_rational (p , q , prec , rounding ))
@@ -689,10 +693,6 @@ def convert(ctx, x, strings=True):
689693 return ctx .make_mpf (_mpf_ )
690694 except ValueError :
691695 pass
692- if hasattr (x , '_mpf_' ): return ctx .make_mpf (x ._mpf_ )
693- if hasattr (x , '_mpc_' ): return ctx .make_mpc (x ._mpc_ )
694- if hasattr (x , '_mpmath_' ):
695- return ctx .convert (x ._mpmath_ (prec , rounding ))
696696 if type (x ).__module__ == 'decimal' :
697697 return ctx .make_mpf (from_Decimal (x , prec , rounding ))
698698 return ctx ._convert_fallback (x , strings )
0 commit comments