@@ -2714,35 +2714,12 @@ private String formatInteger(long v, int radix, boolean unsigned) {
27142714 return s ;
27152715 }
27162716
2717- private String formatFloatDecimal (PyObject arg , boolean truncate ) {
2718- PyFloat argAsFloat ;
2719- if (arg instanceof PyFloat ) {
2720- // Fast path
2721- argAsFloat = (PyFloat )arg ;
2722- } else {
2723- // Use __float__
2724- if (arg instanceof PyInteger || arg instanceof PyLong ) {
2725- // Typical cases, safe to call __float__:
2726- argAsFloat = arg .__float__ ();
2727- } else {
2728- try {
2729- // We can't simply call arg.__float__() because PyString implements
2730- // it without exposing it to python (i.e, str instances has no
2731- // __float__ attribute). So, we would support strings as arguments
2732- // for %g format, which is forbidden by CPython tests (on
2733- // test_format.py).
2734- argAsFloat = (PyFloat )arg .__getattr__ ("__float__" ).__call__ ();
2735- } catch (PyException e ) {
2736- // XXX: Swallow customs AttributeError throws from __float__ methods
2737- // No better alternative for the moment
2738- if (e .match (Py .AttributeError )) {
2739- throw Py .TypeError ("float argument required" );
2740- }
2741- throw e ;
2742- }
2743- }
2717+ private double asDouble (PyObject obj ) {
2718+ try {
2719+ return obj .asDouble ();
2720+ } catch (PyException pye ) {
2721+ throw !pye .match (Py .TypeError ) ? pye : Py .TypeError ("float argument required" );
27442722 }
2745- return formatFloatDecimal (argAsFloat .getValue (), truncate );
27462723 }
27472724
27482725 private String formatFloatDecimal (double v , boolean truncate ) {
@@ -2772,7 +2749,7 @@ private String formatFloatExponential(PyObject arg, char e,
27722749 boolean truncate )
27732750 {
27742751 StringBuilder buf = new StringBuilder ();
2775- double v = arg . __float__ (). getValue ( );
2752+ double v = asDouble ( arg );
27762753 boolean isNegative = false ;
27772754 if (v < 0 ) {
27782755 v = -v ;
@@ -2971,10 +2948,8 @@ else if (arg instanceof PyInteger || arg instanceof PyFloat) {
29712948 string = formatFloatExponential (arg , c , false );
29722949 break ;
29732950 case 'f' :
2974- case 'F' :
2975- string = formatFloatDecimal (arg , false );
2976- // if (altFlag && string.indexOf('.') == -1)
2977- // string += '.';
2951+ case 'F' :
2952+ string = formatFloatDecimal (asDouble (arg ), false );
29782953 break ;
29792954 case 'g' :
29802955 case 'G' :
@@ -2983,15 +2958,15 @@ else if (arg instanceof PyInteger || arg instanceof PyFloat) {
29832958 precision = 6 ;
29842959 }
29852960
2986- double v = arg . __float__ (). getValue ( );
2961+ double v = asDouble ( arg );
29872962 int exponent = (int )ExtraMath .closeFloor (Math .log10 (Math .abs (v == 0 ? 1 : v )));
29882963 if (v == Double .POSITIVE_INFINITY ) {
29892964 string = "inf" ;
29902965 } else if (v == Double .NEGATIVE_INFINITY ) {
29912966 string = "-inf" ;
29922967 } else if (exponent >= -4 && exponent < precision ) {
29932968 precision -= exponent + 1 ;
2994- string = formatFloatDecimal (arg , !altFlag );
2969+ string = formatFloatDecimal (v , !altFlag );
29952970
29962971 // XXX: this block may be unnecessary now
29972972 if (altFlag && string .indexOf ('.' ) == -1 ) {
0 commit comments