summaryrefslogtreecommitdiffstats
path: root/Objects/intobject.c
diff options
context:
space:
mode:
authorBarry Warsaw <barry@python.org>1999-10-12 19:54:53 (GMT)
committerBarry Warsaw <barry@python.org>1999-10-12 19:54:53 (GMT)
commit226ae6ca122f814dabdc40178c7b9656caf729c2 (patch)
treeabaa15aae569a2334c7516b50ea486ec40bfce66 /Objects/intobject.c
parent75260275fe3bcc5d177a1b3ff30fd60681809585 (diff)
downloadcpython-226ae6ca122f814dabdc40178c7b9656caf729c2.zip
cpython-226ae6ca122f814dabdc40178c7b9656caf729c2.tar.gz
cpython-226ae6ca122f814dabdc40178c7b9656caf729c2.tar.bz2
Mainlining the string_methods branch. See branch revision log
messages for specific changes.
Diffstat (limited to 'Objects/intobject.c')
-rw-r--r--Objects/intobject.c43
1 files changed, 43 insertions, 0 deletions
diff --git a/Objects/intobject.c b/Objects/intobject.c
index f2d77e1..45c2186 100644
--- a/Objects/intobject.c
+++ b/Objects/intobject.c
@@ -32,6 +32,7 @@ PERFORMANCE OF THIS SOFTWARE.
/* Integer object implementation */
#include "Python.h"
+#include <ctype.h>
#ifdef HAVE_LIMITS_H
#include <limits.h>
@@ -218,6 +219,48 @@ PyInt_AsLong(op)
return val;
}
+PyObject *
+PyInt_FromString(s, pend, base)
+ char *s;
+ char **pend;
+ int base;
+{
+ char *end;
+ long x;
+ char buffer[256]; /* For errors */
+
+ if ((base != 0 && base < 2) || base > 36) {
+ PyErr_SetString(PyExc_ValueError, "invalid base for int()");
+ return NULL;
+ }
+
+ while (*s && isspace(Py_CHARMASK(*s)))
+ s++;
+ errno = 0;
+ if (base == 0 && s[0] == '0')
+ x = (long) PyOS_strtoul(s, &end, base);
+ else
+ x = PyOS_strtol(s, &end, base);
+ if (end == s || !isalnum(end[-1]))
+ goto bad;
+ while (*end && isspace(Py_CHARMASK(*end)))
+ end++;
+ if (*end != '\0') {
+ bad:
+ sprintf(buffer, "invalid literal for int(): %.200s", s);
+ PyErr_SetString(PyExc_ValueError, buffer);
+ return NULL;
+ }
+ else if (errno != 0) {
+ sprintf(buffer, "int() literal too large: %.200s", s);
+ PyErr_SetString(PyExc_ValueError, buffer);
+ return NULL;
+ }
+ if (pend)
+ *pend = end;
+ return PyInt_FromLong(x);
+}
+
/* Methods */
/* ARGSUSED */