mirror of
https://github.com/python/cpython.git
synced 2025-12-10 19:10:59 +00:00
Fix the tests for various anomalies in the string-to-numbers
conversions. Formerly, for example, int('-') would return 0 instead
of raising ValueError, and int(' 0') would raise ValueError
(complaining about a null byte!) instead of 0...
This commit is contained in:
parent
f57736e77a
commit
3b2b34790f
1 changed files with 8 additions and 12 deletions
|
|
@ -67,12 +67,12 @@ int_from_string(v)
|
||||||
s = PyString_AS_STRING(v);
|
s = PyString_AS_STRING(v);
|
||||||
while (*s && isspace(Py_CHARMASK(*s)))
|
while (*s && isspace(Py_CHARMASK(*s)))
|
||||||
s++;
|
s++;
|
||||||
if (s[0] == '\0') {
|
|
||||||
PyErr_SetString(PyExc_ValueError, "empty string for int()");
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
errno = 0;
|
errno = 0;
|
||||||
x = PyOS_strtol(s, &end, 10);
|
x = PyOS_strtol(s, &end, 10);
|
||||||
|
if (end == s || !isdigit(end[-1])) {
|
||||||
|
PyErr_SetString(PyExc_ValueError, "no digits in int constant");
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
while (*end && isspace(Py_CHARMASK(*end)))
|
while (*end && isspace(Py_CHARMASK(*end)))
|
||||||
end++;
|
end++;
|
||||||
if (*end != '\0') {
|
if (*end != '\0') {
|
||||||
|
|
@ -80,7 +80,7 @@ int_from_string(v)
|
||||||
PyErr_SetString(PyExc_ValueError, buffer);
|
PyErr_SetString(PyExc_ValueError, buffer);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
else if (end-s != PyString_GET_SIZE(v)) {
|
else if (end != PyString_AS_STRING(v) + PyString_GET_SIZE(v)) {
|
||||||
PyErr_SetString(PyExc_ValueError,
|
PyErr_SetString(PyExc_ValueError,
|
||||||
"null byte in argument for int()");
|
"null byte in argument for int()");
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
@ -104,10 +104,6 @@ long_from_string(v)
|
||||||
s = PyString_AS_STRING(v);
|
s = PyString_AS_STRING(v);
|
||||||
while (*s && isspace(Py_CHARMASK(*s)))
|
while (*s && isspace(Py_CHARMASK(*s)))
|
||||||
s++;
|
s++;
|
||||||
if (s[0] == '\0') {
|
|
||||||
PyErr_SetString(PyExc_ValueError, "empty string for long()");
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
x = PyLong_FromString(s, &end, 10);
|
x = PyLong_FromString(s, &end, 10);
|
||||||
if (x == NULL)
|
if (x == NULL)
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
@ -119,9 +115,9 @@ long_from_string(v)
|
||||||
Py_DECREF(x);
|
Py_DECREF(x);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
else if (end-s != PyString_GET_SIZE(v)) {
|
else if (end != PyString_AS_STRING(v) + PyString_GET_SIZE(v)) {
|
||||||
PyErr_SetString(PyExc_ValueError,
|
PyErr_SetString(PyExc_ValueError,
|
||||||
"null byte in argument for float()");
|
"null byte in argument for long()");
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
return x;
|
return x;
|
||||||
|
|
@ -154,7 +150,7 @@ float_from_string(v)
|
||||||
PyErr_SetString(PyExc_ValueError, buffer);
|
PyErr_SetString(PyExc_ValueError, buffer);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
else if (end-s != PyString_GET_SIZE(v)) {
|
else if (end != PyString_AS_STRING(v) + PyString_GET_SIZE(v)) {
|
||||||
PyErr_SetString(PyExc_ValueError,
|
PyErr_SetString(PyExc_ValueError,
|
||||||
"null byte in argument for float()");
|
"null byte in argument for float()");
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue