[Python-checkins] r70798 - in python/branches/py3k-short-float-repr: Objects/floatobject.c Python/marshal.c

eric.smith python-checkins at python.org
Tue Mar 31 11:58:20 CEST 2009


Author: eric.smith
Date: Tue Mar 31 11:58:18 2009
New Revision: 70798

Log:
Removed _PyFloat_Repr, which existed only for marshal. Have marshal format its strings directly. I need to verify this chchange with Mark.

This change is also problematic (and currently incomplete) because marshal assumes that float->string conversions can never fail, but the new code calls malloc, so it is possible that it will fail. That surgery on marshal is pretty severe, though, so it will have to wait for another day.

Modified:
   python/branches/py3k-short-float-repr/Objects/floatobject.c
   python/branches/py3k-short-float-repr/Python/marshal.c

Modified: python/branches/py3k-short-float-repr/Objects/floatobject.c
==============================================================================
--- python/branches/py3k-short-float-repr/Objects/floatobject.c	(original)
+++ python/branches/py3k-short-float-repr/Objects/floatobject.c	Tue Mar 31 11:58:18 2009
@@ -320,65 +320,6 @@
 	return val;
 }
 
-/* Methods */
-
-static void
-format_double(char *buf, size_t buflen, double ob_fval, int precision)
-{
-	register char *cp;
-	char format[32];
-	int i;
-
-	/* Subroutine for float_repr, float_str and float_print.
-	   We want float numbers to be recognizable as such,
-	   i.e., they should contain a decimal point or an exponent.
-	   However, %g may print the number as an integer;
-	   in such cases, we append ".0" to the string. */
-
-	PyOS_snprintf(format, 32, "%%.%ig", precision);
-	PyOS_ascii_formatd(buf, buflen, format, ob_fval);
-	cp = buf;
-	if (*cp == '-')
-		cp++;
-	for (; *cp != '\0'; cp++) {
-		/* Any non-digit means it's not an integer;
-		   this takes care of NAN and INF as well. */
-		if (!isdigit(Py_CHARMASK(*cp)))
-			break;
-	}
-	if (*cp == '\0') {
-		*cp++ = '.';
-		*cp++ = '0';
-		*cp++ = '\0';
-		return;
-	}
-	/* Checking the next three chars should be more than enough to
-	 * detect inf or nan, even on Windows. We check for inf or nan
-	 * at last because they are rare cases.
-	 */
-	for (i=0; *cp != '\0' && i<3; cp++, i++) {
-		if (isdigit(Py_CHARMASK(*cp)) || *cp == '.')
-			continue;
-		/* found something that is neither a digit nor point
-		 * it might be a NaN or INF
-		 */
-#ifdef Py_NAN
-		if (Py_IS_NAN(ob_fval)) {
-			strcpy(buf, "nan");
-		}
-                else
-#endif
-		if (Py_IS_INFINITY(ob_fval)) {
-			cp = buf;
-			if (*cp == '-')
-				cp++;
-			strcpy(cp, "inf");
-		}
-		break;
-	}
-
-}
-
 /* Macro and helper that convert PyObject obj to a C double and store
    the value in dbl.  If conversion to double raises an exception, obj is
    set to NULL, and the function invoking this macro returns NULL.  If
@@ -391,6 +332,8 @@
 	else if (convert_to_double(&(obj), &(dbl)) < 0)	\
 		return obj;
 
+/* Methods */
+
 static int
 convert_to_double(PyObject **v, double *dbl)
 {
@@ -411,13 +354,7 @@
 	return 0;
 }
 
-/* Precisions used by repr() and str(), respectively.
-
-   The repr() precision (17 significant decimal digits) is the minimal number
-   that is guaranteed to have enough precision so that if the number is read
-   back in the exact same binary value is recreated.  This is true for IEEE
-   floating point by design, and also happens to work for all other modern
-   hardware.
+/* Precision used by str().
 
    The str() precision is chosen so that in most cases, the rounding noise
    created by various operations is suppressed, while giving plenty of
@@ -425,7 +362,6 @@
 
 */
 
-#define PREC_REPR	17
 #define PREC_STR	12
 
 static PyObject *
@@ -2238,14 +2174,6 @@
 	}
 }
 
-/* Should only be used by marshal. */
-int
-_PyFloat_Repr(double x, char *p, size_t len)
-{
-	format_double(p, len, x, PREC_REPR);
-	return (int)strlen(p);
-}
-
 double
 _PyFloat_Unpack4(const unsigned char *p, int le)
 {

Modified: python/branches/py3k-short-float-repr/Python/marshal.c
==============================================================================
--- python/branches/py3k-short-float-repr/Python/marshal.c	(original)
+++ python/branches/py3k-short-float-repr/Python/marshal.c	Tue Mar 31 11:58:18 2009
@@ -236,12 +236,15 @@
 			w_string((char*)buf, 8, p);
 		}
 		else {
-			char buf[256]; /* Plenty to format any double */
-			n = _PyFloat_Repr(PyFloat_AS_DOUBLE(v),
-					  buf, sizeof(buf));
+			char *buf = PyOS_double_to_string(PyFloat_AS_DOUBLE(v),
+				0, 'g', 0, 0, 1);
+			if (!buf)
+                            return;
+			n = strlen(buf);
 			w_byte(TYPE_FLOAT, p);
 			w_byte((int)n, p);
 			w_string(buf, (int)n, p);
+			PyMem_Free(buf);
 		}
 	}
 #ifndef WITHOUT_COMPLEX
@@ -263,17 +266,24 @@
 			w_string((char*)buf, 8, p);
 		}
 		else {
-			char buf[256]; /* Plenty to format any double */
+			char *buf;
 			w_byte(TYPE_COMPLEX, p);
-			n = _PyFloat_Repr(PyComplex_RealAsDouble(v),
-					  buf, sizeof(buf));
+			buf = PyOS_double_to_string(PyComplex_RealAsDouble(v),
+				0, 'g', 0, 0, 1);
+			if (!buf)
+                            return;
 			n = strlen(buf);
 			w_byte((int)n, p);
 			w_string(buf, (int)n, p);
-			n = _PyFloat_Repr(PyComplex_ImagAsDouble(v),
-					  buf, sizeof(buf));
+			PyMem_Free(buf);
+			buf = PyOS_double_to_string(PyComplex_ImagAsDouble(v),
+				0, 'g', 0, 0, 1);
+			if (!buf)
+                            return;
+			n = strlen(buf);
 			w_byte((int)n, p);
 			w_string(buf, (int)n, p);
+			PyMem_Free(buf);
 		}
 	}
 #endif


More information about the Python-checkins mailing list