summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGuido van Rossum <guido@python.org>1998-08-04 15:02:01 (GMT)
committerGuido van Rossum <guido@python.org>1998-08-04 15:02:01 (GMT)
commitdf3d8756b77979279784d2a5545574e2eda85eea (patch)
tree71a3f5d1fc8e18a531e79607e8b9398c3631b6d7
parent152d8173a3844d00d7511484d3c1bfd1b8725613 (diff)
downloadcpython-df3d8756b77979279784d2a5545574e2eda85eea.zip
cpython-df3d8756b77979279784d2a5545574e2eda85eea.tar.gz
cpython-df3d8756b77979279784d2a5545574e2eda85eea.tar.bz2
Better error messages when raising ValueError for int and long
literals. (The previous version of this code would not show the offending input, even though there was code that attempted this.)
-rw-r--r--Objects/abstract.c15
1 files changed, 9 insertions, 6 deletions
diff --git a/Objects/abstract.c b/Objects/abstract.c
index cfa4cc8..7458b94 100644
--- a/Objects/abstract.c
+++ b/Objects/abstract.c
@@ -69,13 +69,12 @@ int_from_string(v)
s++;
errno = 0;
x = PyOS_strtol(s, &end, 10);
- if (end == s || !isdigit(end[-1])) {
- PyErr_SetString(PyExc_ValueError, "no digits in int constant");
- return NULL;
- }
+ if (end == s || !isdigit(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;
@@ -105,14 +104,18 @@ long_from_string(v)
while (*s && isspace(Py_CHARMASK(*s)))
s++;
x = PyLong_FromString(s, &end, 10);
- if (x == NULL)
+ if (x == NULL) {
+ if (PyErr_ExceptionMatches(PyExc_ValueError))
+ goto bad;
return NULL;
+ }
while (*end && isspace(Py_CHARMASK(*end)))
end++;
if (*end != '\0') {
+ bad:
sprintf(buffer, "invalid literal for long(): %.200s", s);
PyErr_SetString(PyExc_ValueError, buffer);
- Py_DECREF(x);
+ Py_XDECREF(x);
return NULL;
}
else if (end != PyString_AS_STRING(v) + PyString_GET_SIZE(v)) {