diff Objects/floatobject.c @ 11383:aca7b5eaf5e8 legacy-trunk

Mainlining the string_methods branch. See branch revision log messages for specific changes.
author Barry Warsaw <barry@python.org>
date Tue, 12 Oct 1999 19:54:53 +0000
parents 135c1ec9636c
children cf7a0f1b7b48
line wrap: on
line diff
--- a/Objects/floatobject.c	Tue Oct 12 16:20:13 1999 +0000
+++ b/Objects/floatobject.c	Tue Oct 12 19:54:53 1999 +0000
@@ -149,6 +149,57 @@
 	return (PyObject *) op;
 }
 
+PyObject *
+PyFloat_FromString(v, pend)
+	PyObject *v;
+	char **pend;
+{
+	extern double strtod Py_PROTO((const char *, char **));
+	char *s, *last, *end;
+	double x;
+	char buffer[256]; /* For errors */
+
+	if (!PyString_Check(v))
+		return NULL;
+	s = PyString_AS_STRING(v);
+
+	last = s + PyString_GET_SIZE(v);
+	while (*s && isspace(Py_CHARMASK(*s)))
+		s++;
+	if (s[0] == '\0') {
+		PyErr_SetString(PyExc_ValueError, "empty string for float()");
+		return NULL;
+	}
+	errno = 0;
+	PyFPE_START_PROTECT("PyFloat_FromString", return 0)
+	x = strtod(s, &end);
+	PyFPE_END_PROTECT(x)
+	/* Believe it or not, Solaris 2.6 can move end *beyond* the null
+	   byte at the end of the string, when the input is inf(inity) */
+	if (end > last)
+		end = last;
+	while (*end && isspace(Py_CHARMASK(*end)))
+		end++;
+	if (*end != '\0') {
+		sprintf(buffer, "invalid literal for float(): %.200s", s);
+		PyErr_SetString(PyExc_ValueError, buffer);
+		return NULL;
+	}
+	else if (end != PyString_AS_STRING(v) + PyString_GET_SIZE(v)) {
+		PyErr_SetString(PyExc_ValueError,
+				"null byte in argument for float()");
+		return NULL;
+	}
+	else if (errno != 0) {
+		sprintf(buffer, "float() literal too large: %.200s", s);
+		PyErr_SetString(PyExc_ValueError, buffer);
+		return NULL;
+	}
+	if (pend)
+		*pend = end;
+	return PyFloat_FromDouble(x);
+}
+
 static void
 float_dealloc(op)
 	PyFloatObject *op;