[Python-checkins] r71526 - in python/branches/py3k-short-float-repr: Include/pystrtod.h Modules/_pickle.c Objects/complexobject.c Objects/floatobject.c Objects/stringlib/formatter.h Objects/unicodeobject.c Python/marshal.c Python/pystrtod.c

eric.smith python-checkins at python.org
Sun Apr 12 18:39:19 CEST 2009


Author: eric.smith
Date: Sun Apr 12 18:39:18 2009
New Revision: 71526
Log:
Added output parameter 'type' to PyOS_double_to_string, so the caller can tell if the resulting string represents a normal number, a nan, or an inf. Added corresponding constants to pystrtod.h.
Modified:
 python/branches/py3k-short-float-repr/Include/pystrtod.h
 python/branches/py3k-short-float-repr/Modules/_pickle.c
 python/branches/py3k-short-float-repr/Objects/complexobject.c
 python/branches/py3k-short-float-repr/Objects/floatobject.c
 python/branches/py3k-short-float-repr/Objects/stringlib/formatter.h
 python/branches/py3k-short-float-repr/Objects/unicodeobject.c
 python/branches/py3k-short-float-repr/Python/marshal.c
 python/branches/py3k-short-float-repr/Python/pystrtod.c
Modified: python/branches/py3k-short-float-repr/Include/pystrtod.h
==============================================================================
--- python/branches/py3k-short-float-repr/Include/pystrtod.h	(original)
+++ python/branches/py3k-short-float-repr/Include/pystrtod.h	Sun Apr 12 18:39:18 2009
@@ -15,14 +15,21 @@
 PyAPI_FUNC(char *) PyOS_double_to_string(double val,
 char format_code,
 int precision,
- int flags);
+ int flags,
+ int *type);
 
 
+/* PyOS_double_to_string's "flags" parameter can be set to 0 or more of: */
 #define Py_DTSF_SIGN 0x01 /* always add the sign */
 #define Py_DTSF_ADD_DOT_0 0x02 /* if the result is an integer add ".0" */
 #define Py_DTSF_ALT 0x04 /* "alternate" formatting. it's format_code
 specific */
 
+/* PyOS_double_to_string's "type", if non-NULL, will be set to one of: */
+#define Py_DTST_FINITE 0
+#define Py_DTST_INFINITE 1
+#define Py_DTST_NAN 2
+
 #ifdef __cplusplus
 }
 #endif
Modified: python/branches/py3k-short-float-repr/Modules/_pickle.c
==============================================================================
--- python/branches/py3k-short-float-repr/Modules/_pickle.c	(original)
+++ python/branches/py3k-short-float-repr/Modules/_pickle.c	Sun Apr 12 18:39:18 2009
@@ -1025,7 +1025,7 @@
 if (pickler_write(self, &op, 1) < 0)
 goto done;
 
- buf = PyOS_double_to_string(x, 'r', 0, 0);
+ buf = PyOS_double_to_string(x, 'r', 0, 0, NULL);
 if (!buf) {
 PyErr_NoMemory();
 goto done;
Modified: python/branches/py3k-short-float-repr/Objects/complexobject.c
==============================================================================
--- python/branches/py3k-short-float-repr/Objects/complexobject.c	(original)
+++ python/branches/py3k-short-float-repr/Objects/complexobject.c	Sun Apr 12 18:39:18 2009
@@ -359,7 +359,8 @@
 im = "-inf*";
 }
 else {
- pim = PyOS_double_to_string(v->cval.imag, format_code, 0, 0);
+ pim = PyOS_double_to_string(v->cval.imag, format_code,
+ 0, 0, NULL);
 if (!pim) {
 PyErr_NoMemory();
 goto done;
@@ -378,7 +379,8 @@
 re = "-inf";
 }
 else {
- pre = PyOS_double_to_string(v->cval.real, format_code, 0, 0);
+ pre = PyOS_double_to_string(v->cval.real, format_code,
+ 0, 0, NULL);
 if (!pre) {
 PyErr_NoMemory();
 goto done;
@@ -397,7 +399,7 @@
 }
 else {
 pim = PyOS_double_to_string(v->cval.imag, format_code,
- 0, Py_DTSF_SIGN);
+ 0, Py_DTSF_SIGN, NULL);
 if (!pim) {
 PyErr_NoMemory();
 goto done;
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	Sun Apr 12 18:39:18 2009
@@ -350,7 +350,8 @@
 {
 PyObject *result;
 char *buf = PyOS_double_to_string(PyFloat_AS_DOUBLE(v),
- format_code, 0, Py_DTSF_ADD_DOT_0);
+ format_code, 0, Py_DTSF_ADD_DOT_0,
+ NULL);
 if (!buf)
 return PyErr_NoMemory();
 result = PyUnicode_FromString(buf);
@@ -1898,7 +1899,7 @@
 				 Py_REFCNT(p) != 0) {
 					char *buf = PyOS_double_to_string(
 						PyFloat_AS_DOUBLE(p), 'r',
-						0, 0);
+						0, 0, NULL);
 					if (buf) {
 						/* XXX(twouters) cast
 						 refcount to long
Modified: python/branches/py3k-short-float-repr/Objects/stringlib/formatter.h
==============================================================================
--- python/branches/py3k-short-float-repr/Objects/stringlib/formatter.h	(original)
+++ python/branches/py3k-short-float-repr/Objects/stringlib/formatter.h	Sun Apr 12 18:39:18 2009
@@ -856,6 +856,7 @@
 int flags = 0;
 PyObject *result = NULL;
 STRINGLIB_CHAR sign_char = '0円';
+ int float_type; /* Used to see if we have a nan, inf, or regular float. */
 
 #if STRINGLIB_IS_UNICODE
 Py_UNICODE *unicode_tmp = NULL;
@@ -904,7 +905,8 @@
 /* cast "type", because if we're in unicode we need to pass a
 8-bit char. this is safe, because we've restricted what "type"
 can be */
- buf = PyOS_double_to_string(val, (char)type, precision, flags);
+ buf = PyOS_double_to_string(val, (char)type, precision, flags,
+ &float_type);
 if (buf == NULL)
 goto done;
 n_digits = strlen(buf);
Modified: python/branches/py3k-short-float-repr/Objects/unicodeobject.c
==============================================================================
--- python/branches/py3k-short-float-repr/Objects/unicodeobject.c	(original)
+++ python/branches/py3k-short-float-repr/Objects/unicodeobject.c	Sun Apr 12 18:39:18 2009
@@ -8849,7 +8849,7 @@
 }
 
 p = PyOS_double_to_string(x, type, prec,
- (flags & F_ALT) ? Py_DTSF_ALT : 0);
+ (flags & F_ALT) ? Py_DTSF_ALT : 0, NULL);
 len = strlen(p);
 if (len+1 >= buflen) {
 /* Caller supplied buffer is not large enough. */
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	Sun Apr 12 18:39:18 2009
@@ -237,7 +237,7 @@
 		}
 		else {
 			char *buf = PyOS_double_to_string(PyFloat_AS_DOUBLE(v),
-				'r', 0, 0);
+ 'r', 0, 0, NULL);
 			if (!buf)
 return;
 			n = strlen(buf);
@@ -269,7 +269,7 @@
 			char *buf;
 			w_byte(TYPE_COMPLEX, p);
 			buf = PyOS_double_to_string(PyComplex_RealAsDouble(v),
-				'r', 0, 0);
+ 'r', 0, 0, NULL);
 			if (!buf)
 return;
 			n = strlen(buf);
@@ -277,7 +277,7 @@
 			w_string(buf, (int)n, p);
 			PyMem_Free(buf);
 			buf = PyOS_double_to_string(PyComplex_ImagAsDouble(v),
-				'r', 0, 0);
+ 'r', 0, 0, NULL);
 			if (!buf)
 return;
 			n = strlen(buf);
Modified: python/branches/py3k-short-float-repr/Python/pystrtod.c
==============================================================================
--- python/branches/py3k-short-float-repr/Python/pystrtod.c	(original)
+++ python/branches/py3k-short-float-repr/Python/pystrtod.c	Sun Apr 12 18:39:18 2009
@@ -539,6 +539,11 @@
 should have ".0" added. Only applies to format codes 'r', 's', and 'g'.
 use_alt_formatting is nonzero if alternative formatting should be
 used. Only applies to format codes 'e', 'f' and 'g'.
+ type, if non-NULL, will be set to one of these constants to identify
+ the type of the 'd' argument:
+ Py_DTST_FINITE
+ Py_DTST_INFINITE
+ Py_DTST_NAN
 
 Returns a PyMem_Malloc'd block of memory containing the resulting string,
 or NULL on error. If NULL is returned, the Python error has been set.
@@ -548,7 +553,7 @@
 format_float_short(double d, char format_code,
 		 int mode, Py_ssize_t precision,
 		 int always_add_sign, int add_dot_0_if_integer,
-		 int use_alt_formatting, char **float_strings)
+		 int use_alt_formatting, char **float_strings, int *type)
 {
 	char *buf = NULL;
 	char *p;
@@ -593,12 +598,18 @@
 			}
 			strncpy(p, float_strings[OFS_INF], 3);
 			p += 3;
+
+			if (type)
+				*type = Py_DTST_INFINITE;
 		}
 		else if (digits[0] == 'n' || digits[0] == 'N') {
 			/* note that we *never* add a sign for a nan,
 			 even if one has explicitly been requested */
 			strncpy(p, float_strings[OFS_NAN], 3);
 			p += 3;
+
+			if (type)
+				*type = Py_DTST_NAN;
 		}
 		else {
 			/* shouldn't get here: Gay's code should always return
@@ -610,6 +621,11 @@
 		goto exit;
 	}
 
+	/* The result must be finite (not inf or nan). */
+	if (type)
+		*type = Py_DTST_FINITE;
+
+
 	/* We got digits back, format them. We may need to pad 'digits'
 	 either on the left or right (or both) with extra zeros, so in
 	 general the resulting string has the form
@@ -785,7 +801,8 @@
 PyAPI_FUNC(char *) PyOS_double_to_string(double val,
 char format_code,
 int precision,
- int flags)
+ int flags,
+					 int *type)
 {
 	char lc_format_code = format_code;
 	char** float_strings = lc_float_strings;
@@ -856,5 +873,5 @@
 				 flags & Py_DTSF_SIGN,
 				 flags & Py_DTSF_ADD_DOT_0,
 				 flags & Py_DTSF_ALT,
-				 float_strings);
+				 float_strings, type);
 }


More information about the Python-checkins mailing list

AltStyle によって変換されたページ (->オリジナル) /