Make int("...") return a long if an int would overflow.
Also remove the 512 character limitation for int(u"...") and long(u"..."). This closes SF bug #629989.
This commit is contained in:
parent
aca49b065b
commit
07e147667c
@ -435,10 +435,8 @@ if int(s)+1 != -sys.maxint:
|
|||||||
raise TestFailed, "int(%s)" % `s`
|
raise TestFailed, "int(%s)" % `s`
|
||||||
try:
|
try:
|
||||||
int(s[1:])
|
int(s[1:])
|
||||||
except ValueError:
|
except:
|
||||||
pass
|
raise TestFailed, "int(%s)" % `s[1:]` + " should return long"
|
||||||
else:
|
|
||||||
raise TestFailed, "int(%s)" % `s[1:]` + " should raise ValueError"
|
|
||||||
try:
|
try:
|
||||||
int(1e100)
|
int(1e100)
|
||||||
except OverflowError:
|
except OverflowError:
|
||||||
@ -468,9 +466,12 @@ try: int('53', 40)
|
|||||||
except ValueError: pass
|
except ValueError: pass
|
||||||
else: raise TestFailed("int('53', 40) didn't raise ValueError")
|
else: raise TestFailed("int('53', 40) didn't raise ValueError")
|
||||||
|
|
||||||
try: int('1' * 512)
|
try: int('1' * 600)
|
||||||
except ValueError: pass
|
except: raise TestFailed("int('1' * 600) didn't return long")
|
||||||
else: raise TestFailed("int('1' * 512) didn't raise ValueError")
|
|
||||||
|
if have_unicode:
|
||||||
|
try: int(unichr(0x661) * 600)
|
||||||
|
except: raise TestFailed("int('\\u0661' * 600) didn't return long")
|
||||||
|
|
||||||
try: int(1, 12)
|
try: int(1, 12)
|
||||||
except TypeError: pass
|
except TypeError: pass
|
||||||
|
@ -208,10 +208,9 @@ PyInt_FromString(char *s, char **pend, int base)
|
|||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
else if (errno != 0) {
|
else if (errno != 0) {
|
||||||
PyOS_snprintf(buffer, sizeof(buffer),
|
if (err_ovf("string/unicode conversion"))
|
||||||
"int() literal too large: %.200s", s);
|
return NULL;
|
||||||
PyErr_SetString(PyExc_ValueError, buffer);
|
return PyLong_FromString(s, pend, base);
|
||||||
return NULL;
|
|
||||||
}
|
}
|
||||||
if (pend)
|
if (pend)
|
||||||
*pend = end;
|
*pend = end;
|
||||||
@ -222,16 +221,19 @@ PyInt_FromString(char *s, char **pend, int base)
|
|||||||
PyObject *
|
PyObject *
|
||||||
PyInt_FromUnicode(Py_UNICODE *s, int length, int base)
|
PyInt_FromUnicode(Py_UNICODE *s, int length, int base)
|
||||||
{
|
{
|
||||||
char buffer[256];
|
PyObject *result;
|
||||||
|
char *buffer = PyMem_MALLOC(length+1);
|
||||||
|
|
||||||
if (length >= sizeof(buffer)) {
|
if (buffer == NULL)
|
||||||
PyErr_SetString(PyExc_ValueError,
|
return NULL;
|
||||||
"int() literal too large to convert");
|
|
||||||
|
if (PyUnicode_EncodeDecimal(s, length, buffer, NULL)) {
|
||||||
|
PyMem_FREE(buffer);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
if (PyUnicode_EncodeDecimal(s, length, buffer, NULL))
|
result = PyInt_FromString(buffer, NULL, base);
|
||||||
return NULL;
|
PyMem_FREE(buffer);
|
||||||
return PyInt_FromString(buffer, NULL, base);
|
return result;
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
@ -1123,17 +1123,19 @@ PyLong_FromString(char *str, char **pend, int base)
|
|||||||
PyObject *
|
PyObject *
|
||||||
PyLong_FromUnicode(Py_UNICODE *u, int length, int base)
|
PyLong_FromUnicode(Py_UNICODE *u, int length, int base)
|
||||||
{
|
{
|
||||||
char buffer[256];
|
PyObject *result;
|
||||||
|
char *buffer = PyMem_MALLOC(length+1);
|
||||||
|
|
||||||
if (length >= sizeof(buffer)) {
|
if (buffer == NULL)
|
||||||
PyErr_SetString(PyExc_ValueError,
|
return NULL;
|
||||||
"long() literal too large to convert");
|
|
||||||
|
if (PyUnicode_EncodeDecimal(u, length, buffer, NULL)) {
|
||||||
|
PyMem_FREE(buffer);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
if (PyUnicode_EncodeDecimal(u, length, buffer, NULL))
|
result = PyLong_FromString(buffer, NULL, base);
|
||||||
return NULL;
|
PyMem_FREE(buffer);
|
||||||
|
return result;
|
||||||
return PyLong_FromString(buffer, NULL, base);
|
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user