diff options
author | Martin v. Löwis <martin@v.loewis.de> | 2007-12-04 22:10:37 (GMT) |
---|---|---|
committer | Martin v. Löwis <martin@v.loewis.de> | 2007-12-04 22:10:37 (GMT) |
commit | d1a1d1ed802187cd1a9a8a95ac5d758c7acffee6 (patch) | |
tree | 17489e6ea4df32ba3b3bbda6e4b31155a460f265 /Modules | |
parent | 0fbab7ff8d2efd92e222fcc13c0aff0998c3c158 (diff) | |
download | cpython-d1a1d1ed802187cd1a9a8a95ac5d758c7acffee6.zip cpython-d1a1d1ed802187cd1a9a8a95ac5d758c7acffee6.tar.gz cpython-d1a1d1ed802187cd1a9a8a95ac5d758c7acffee6.tar.bz2 |
Remove PyInt_CheckExact. Add PyLong_AsLongAndOverflow.
Diffstat (limited to 'Modules')
-rw-r--r-- | Modules/_csv.c | 21 | ||||
-rw-r--r-- | Modules/_cursesmodule.c | 9 | ||||
-rw-r--r-- | Modules/_tkinter.c | 11 | ||||
-rw-r--r-- | Modules/datetimemodule.c | 9 | ||||
-rw-r--r-- | Modules/socketmodule.c | 7 | ||||
-rw-r--r-- | Modules/timemodule.c | 4 |
6 files changed, 47 insertions, 14 deletions
diff --git a/Modules/_csv.c b/Modules/_csv.c index c30cea9..aee490c 100644 --- a/Modules/_csv.c +++ b/Modules/_csv.c @@ -181,12 +181,23 @@ _set_int(const char *name, int *target, PyObject *src, int dflt) if (src == NULL) *target = dflt; else { - if (!PyInt_CheckExact(src)) { + long value; + if (!PyLong_CheckExact(src)) { PyErr_Format(PyExc_TypeError, "\"%s\" must be an integer", name); return -1; } - *target = PyLong_AsLong(src); + value = PyLong_AsLong(src); + if (value == -1 && PyErr_Occurred()) + return -1; +#if SIZEOF_LONG > SIZEOF_INT + if (value > INT_MAX || value < INT_MIN) { + PyErr_Format(PyExc_ValueError, + "integer out of range for \"%s\"", name); + return -1; + } +#endif + *target = (int)value; } return 0; } @@ -1385,12 +1396,16 @@ csv_field_size_limit(PyObject *module, PyObject *args) if (!PyArg_UnpackTuple(args, "field_size_limit", 0, 1, &new_limit)) return NULL; if (new_limit != NULL) { - if (!PyInt_CheckExact(new_limit)) { + if (!PyLong_CheckExact(new_limit)) { PyErr_Format(PyExc_TypeError, "limit must be an integer"); return NULL; } field_limit = PyLong_AsLong(new_limit); + if (field_limit == -1 && PyErr_Occurred()) { + field_limit = old_limit; + return NULL; + } } return PyLong_FromLong(old_limit); } diff --git a/Modules/_cursesmodule.c b/Modules/_cursesmodule.c index 1fc7da7..d1cd1551 100644 --- a/Modules/_cursesmodule.c +++ b/Modules/_cursesmodule.c @@ -196,8 +196,13 @@ PyCursesCheckERR(int code, char *fname) static int PyCurses_ConvertToChtype(PyObject *obj, chtype *ch) { - if (PyInt_CheckExact(obj)) { - *ch = (chtype) PyLong_AsLong(obj); + if (PyLong_CheckExact(obj)) { + int overflow; + /* XXX should the truncation by the cast also be reported + as an error? */ + *ch = (chtype) PyLong_AsLongAndOverflow(obj, &overflow); + if (overflow) + return 0; } else if(PyString_Check(obj) && (PyString_Size(obj) == 1)) { *ch = (chtype) *PyString_AsString(obj); diff --git a/Modules/_tkinter.c b/Modules/_tkinter.c index c755f89..2a341ab 100644 --- a/Modules/_tkinter.c +++ b/Modules/_tkinter.c @@ -863,14 +863,21 @@ static Tcl_Obj* AsObj(PyObject *value) { Tcl_Obj *result; + long longVal; + int overflow; if (PyString_Check(value)) return Tcl_NewStringObj(PyString_AS_STRING(value), PyString_GET_SIZE(value)); else if (PyBool_Check(value)) return Tcl_NewBooleanObj(PyObject_IsTrue(value)); - else if (PyInt_CheckExact(value)) - return Tcl_NewLongObj(PyLong_AS_LONG(value)); + else if (PyLong_CheckExact(value) && + ((longVal = PyLong_AsLongAndOverflow(value, &overflow)), + !overflow)) { + /* If there is an overflow in the long conversion, + fall through to default object handling. */ + return Tcl_NewLongObj(longVal); + } else if (PyFloat_Check(value)) return Tcl_NewDoubleObj(PyFloat_AS_DOUBLE(value)); else if (PyTuple_Check(value)) { diff --git a/Modules/datetimemodule.c b/Modules/datetimemodule.c index 6955c78..8eb7e04 100644 --- a/Modules/datetimemodule.c +++ b/Modules/datetimemodule.c @@ -3827,7 +3827,7 @@ datetime_strptime(PyObject *cls, PyObject *args) Py_DECREF(module); if (obj != NULL) { - int i, good_timetuple = 1; + int i, good_timetuple = 1, overflow; long int ia[6]; if (PySequence_Check(obj) && PySequence_Size(obj) >= 6) for (i=0; i < 6; i++) { @@ -3836,8 +3836,11 @@ datetime_strptime(PyObject *cls, PyObject *args) Py_DECREF(obj); return NULL; } - if (PyInt_CheckExact(p)) - ia[i] = PyLong_AsLong(p); + if (PyLong_CheckExact(p)) { + ia[i] = PyLong_AsLongAndOverflow(p, &overflow); + if (overflow) + good_timetuple = 0; + } else good_timetuple = 0; Py_DECREF(p); diff --git a/Modules/socketmodule.c b/Modules/socketmodule.c index 926f059..c1fb5aa 100644 --- a/Modules/socketmodule.c +++ b/Modules/socketmodule.c @@ -3595,8 +3595,11 @@ socket_getaddrinfo(PyObject *self, PyObject *args) "getaddrinfo() argument 1 must be string or None"); return NULL; } - if (PyInt_CheckExact(pobj)) { - PyOS_snprintf(pbuf, sizeof(pbuf), "%ld", PyLong_AsLong(pobj)); + if (PyLong_CheckExact(pobj)) { + long value = PyLong_AsLong(pobj); + if (value == -1 && PyErr_Occurred()) + goto err; + PyOS_snprintf(pbuf, sizeof(pbuf), "%ld", value); pptr = pbuf; } else if (PyUnicode_Check(pobj)) { pptr = PyUnicode_AsString(pobj); diff --git a/Modules/timemodule.c b/Modules/timemodule.c index 4196381..c64a356 100644 --- a/Modules/timemodule.c +++ b/Modules/timemodule.c @@ -392,8 +392,8 @@ gettmarg(PyObject *args, struct tm *p) if (y < 1900) { PyObject *accept = PyDict_GetItemString(moddict, "accept2dyear"); - if (accept == NULL || !PyInt_CheckExact(accept) || - PyLong_AsLong(accept) == 0) { + if (accept == NULL || !PyLong_CheckExact(accept) || + !PyObject_IsTrue(accept)) { PyErr_SetString(PyExc_ValueError, "year >= 1900 required"); return 0; |