Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 3b2b347

Browse files
committed
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...
1 parent f57736e commit 3b2b347

1 file changed

Lines changed: 8 additions & 12 deletions

File tree

Objects/abstract.c

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -67,20 +67,20 @@ int_from_string(v)
6767
s = PyString_AS_STRING(v);
6868
while (*s && isspace(Py_CHARMASK(*s)))
6969
s++;
70-
if (s[0] == '\0') {
71-
PyErr_SetString(PyExc_ValueError, "empty string for int()");
72-
return NULL;
73-
}
7470
errno = 0;
7571
x = PyOS_strtol(s, &end, 10);
72+
if (end == s || !isdigit(end[-1])) {
73+
PyErr_SetString(PyExc_ValueError, "no digits in int constant");
74+
return NULL;
75+
}
7676
while (*end && isspace(Py_CHARMASK(*end)))
7777
end++;
7878
if (*end != '\0') {
7979
sprintf(buffer, "invalid literal for int(): %.200s", s);
8080
PyErr_SetString(PyExc_ValueError, buffer);
8181
return NULL;
8282
}
83-
else if (end-s != PyString_GET_SIZE(v)) {
83+
else if (end != PyString_AS_STRING(v) + PyString_GET_SIZE(v)) {
8484
PyErr_SetString(PyExc_ValueError,
8585
"null byte in argument for int()");
8686
return NULL;
@@ -104,10 +104,6 @@ long_from_string(v)
104104
s = PyString_AS_STRING(v);
105105
while (*s && isspace(Py_CHARMASK(*s)))
106106
s++;
107-
if (s[0] == '\0') {
108-
PyErr_SetString(PyExc_ValueError, "empty string for long()");
109-
return NULL;
110-
}
111107
x = PyLong_FromString(s, &end, 10);
112108
if (x == NULL)
113109
return NULL;
@@ -119,9 +115,9 @@ long_from_string(v)
119115
Py_DECREF(x);
120116
return NULL;
121117
}
122-
else if (end-s != PyString_GET_SIZE(v)) {
118+
else if (end != PyString_AS_STRING(v) + PyString_GET_SIZE(v)) {
123119
PyErr_SetString(PyExc_ValueError,
124-
"null byte in argument for float()");
120+
"null byte in argument for long()");
125121
return NULL;
126122
}
127123
return x;
@@ -154,7 +150,7 @@ float_from_string(v)
154150
PyErr_SetString(PyExc_ValueError, buffer);
155151
return NULL;
156152
}
157-
else if (end-s != PyString_GET_SIZE(v)) {
153+
else if (end != PyString_AS_STRING(v) + PyString_GET_SIZE(v)) {
158154
PyErr_SetString(PyExc_ValueError,
159155
"null byte in argument for float()");
160156
return NULL;

0 commit comments

Comments
 (0)