diff -r 0f4d4f4db724 Include/longobject.h --- a/Include/longobject.h Wed Oct 03 03:16:42 2012 +0200 +++ b/Include/longobject.h Wed Oct 03 10:12:33 2012 +0300 @@ -26,6 +26,9 @@ PyAPI_FUNC(size_t) PyLong_AsSize_t(PyObject *); PyAPI_FUNC(unsigned long) PyLong_AsUnsignedLong(PyObject *); PyAPI_FUNC(unsigned long) PyLong_AsUnsignedLongMask(PyObject *); +#ifndef Py_LIMITED_API +PyAPI_FUNC(int) _PyLong_AsInt(PyObject *); +#endif PyAPI_FUNC(PyObject *) PyLong_GetInfo(void); /* It may be useful in the future. I've added it in the PyInt -> PyLong diff -r 0f4d4f4db724 Modules/_ctypes/stgdict.c --- a/Modules/_ctypes/stgdict.c Wed Oct 03 03:16:42 2012 +0200 +++ b/Modules/_ctypes/stgdict.c Wed Oct 03 10:12:33 2012 +0300 @@ -335,7 +335,7 @@ isPacked = PyObject_GetAttrString(type, "_pack_"); if (isPacked) { - pack = PyLong_AsLong(isPacked); + pack = _PyLong_AsInt(isPacked); if (pack < 0 || PyErr_Occurred()) { Py_XDECREF(isPacked); PyErr_SetString(PyExc_ValueError, diff -r 0f4d4f4db724 Modules/_io/_iomodule.c --- a/Modules/_io/_iomodule.c Wed Oct 03 03:16:42 2012 +0200 +++ b/Modules/_io/_iomodule.c Wed Oct 03 10:12:33 2012 +0300 @@ -303,7 +303,8 @@ int text = 0, binary = 0, universal = 0; char rawmode[5], *m; - int line_buffering, isatty; + int line_buffering; + long isatty; PyObject *raw, *modeobj = NULL, *buffer = NULL, *wrapper = NULL; @@ -441,12 +442,12 @@ #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE { struct stat st; - long fileno; + int fileno; PyObject *res = PyObject_CallMethod(raw, "fileno", NULL); if (res == NULL) goto error; - fileno = PyLong_AsLong(res); + fileno = _PyLong_AsInt(res); Py_DECREF(res); if (fileno == -1 && PyErr_Occurred()) goto error; diff -r 0f4d4f4db724 Modules/_io/fileio.c --- a/Modules/_io/fileio.c Wed Oct 03 03:16:42 2012 +0200 +++ b/Modules/_io/fileio.c Wed Oct 03 10:12:33 2012 +0300 @@ -240,7 +240,7 @@ return -1; } - fd = PyLong_AsLong(nameobj); + fd = _PyLong_AsInt(nameobj); if (fd < 0) { if (!PyErr_Occurred()) { PyErr_SetString(PyExc_ValueError, diff -r 0f4d4f4db724 Modules/_sqlite/connection.c --- a/Modules/_sqlite/connection.c Wed Oct 03 03:16:42 2012 +0200 +++ b/Modules/_sqlite/connection.c Wed Oct 03 10:12:33 2012 +0300 @@ -861,7 +861,7 @@ rc = SQLITE_DENY; } else { if (PyLong_Check(ret)) { - rc = (int)PyLong_AsLong(ret); + rc = _PyLong_AsInt(ret); } else { rc = SQLITE_DENY; } @@ -1289,7 +1289,7 @@ goto finally; } - result = PyLong_AsLong(retval); + result = _PyLong_AsInt(retval); if (PyErr_Occurred()) { result = 0; } diff -r 0f4d4f4db724 Modules/grpmodule.c --- a/Modules/grpmodule.c Wed Oct 03 03:16:42 2012 +0200 +++ b/Modules/grpmodule.c Wed Oct 03 10:12:33 2012 +0300 @@ -85,7 +85,7 @@ grp_getgrgid(PyObject *self, PyObject *pyo_id) { PyObject *py_int_id; - unsigned int gid; + long gid; struct group *p; py_int_id = PyNumber_Long(pyo_id); @@ -93,8 +93,15 @@ return NULL; gid = PyLong_AS_LONG(py_int_id); Py_DECREF(py_int_id); + if (gid == -1 && PyErr_Occurred()) + return NULL; + if ((long)(gid_t)gid != gid) { + PyErr_SetString(PyExc_OverflowError, + "Python int too large to convert to gid_t"); + return NULL; + } - if ((p = getgrgid(gid)) == NULL) { + if ((p = getgrgid((gid_t)gid)) == NULL) { PyErr_Format(PyExc_KeyError, "getgrgid(): gid not found: %d", gid); return NULL; } diff -r 0f4d4f4db724 Modules/parsermodule.c --- a/Modules/parsermodule.c Wed Oct 03 03:16:42 2012 +0200 +++ b/Modules/parsermodule.c Wed Oct 03 10:12:33 2012 +0300 @@ -747,7 +747,7 @@ /* elem must always be a sequence, however simple */ PyObject* elem = PySequence_GetItem(tuple, i); int ok = elem != NULL; - long type = 0; + int type = 0; char *strn = 0; if (ok) @@ -758,8 +758,14 @@ ok = 0; else { ok = PyLong_Check(temp); - if (ok) - type = PyLong_AS_LONG(temp); + if (ok) { + type = _PyLong_AsInt(temp); + if (type == -1 && PyErr_Occurred()) { + Py_DECREF(temp); + Py_DECREF(elem); + return 0; + } + } Py_DECREF(temp); } } @@ -795,8 +801,16 @@ if (len == 3) { PyObject *o = PySequence_GetItem(elem, 2); if (o != NULL) { - if (PyLong_Check(o)) - *line_num = PyLong_AS_LONG(o); + if (PyLong_Check(o)) { + int num = _PyLong_AsInt(o); + if (num == -1 && PyErr_Occurred()) { + Py_DECREF(o); + Py_DECREF(temp); + Py_DECREF(elem); + return 0; + } + *line_num = num; + } else { PyErr_Format(parser_error, "third item in terminal node must be an" diff -r 0f4d4f4db724 Modules/posixmodule.c --- a/Modules/posixmodule.c Wed Oct 03 03:16:42 2012 +0200 +++ b/Modules/posixmodule.c Wed Oct 03 10:12:33 2012 +0300 @@ -6491,7 +6491,7 @@ */ struct constdef { char *name; - long value; + int value; }; static int @@ -6499,7 +6499,10 @@ size_t tablesize) { if (PyLong_Check(arg)) { - *valuep = PyLong_AS_LONG(arg); + int value = _PyLong_AsInt(arg); + if (value == -1 && PyErr_Occurred()) + return 0; + *valuep = value; return 1; } else { diff -r 0f4d4f4db724 Modules/readline.c --- a/Modules/readline.c Wed Oct 03 03:16:42 2012 +0200 +++ b/Modules/readline.c Wed Oct 03 10:12:33 2012 +0300 @@ -742,7 +742,7 @@ if (r == Py_None) result = 0; else { - result = PyLong_AsLong(r); + result = _PyLong_AsInt(r); if (result == -1 && PyErr_Occurred()) goto error; } diff -r 0f4d4f4db724 Modules/selectmodule.c --- a/Modules/selectmodule.c Wed Oct 03 03:16:42 2012 +0200 +++ b/Modules/selectmodule.c Wed Oct 03 10:12:33 2012 +0300 @@ -342,10 +342,13 @@ i = pos = 0; while (PyDict_Next(self->dict, &pos, &key, &value)) { - self->ufds[i].fd = PyLong_AsLong(key); + assert(i < self->ufd_len); + /* Never overflow */ + self->ufds[i].fd = (int)PyLong_AsLong(key); self->ufds[i].events = (short)PyLong_AsLong(value); i++; } + assert(i == self->ufd_len); self->ufd_uptodate = 1; return 1; } @@ -361,10 +364,11 @@ poll_register(pollObject *self, PyObject *args) { PyObject *o, *key, *value; - int fd, events = POLLIN | POLLPRI | POLLOUT; + int fd; + short events = POLLIN | POLLPRI | POLLOUT; int err; - if (!PyArg_ParseTuple(args, "O|i:register", &o, &events)) { + if (!PyArg_ParseTuple(args, "O|h:register", &o, &events)) { return NULL; } @@ -503,7 +507,7 @@ tout = PyNumber_Long(tout); if (!tout) return NULL; - timeout = PyLong_AsLong(tout); + timeout = _PyLong_AsInt(tout); Py_DECREF(tout); if (timeout == -1 && PyErr_Occurred()) return NULL; diff -r 0f4d4f4db724 Modules/socketmodule.c --- a/Modules/socketmodule.c Wed Oct 03 03:16:42 2012 +0200 +++ b/Modules/socketmodule.c Wed Oct 03 10:12:33 2012 +0300 @@ -1737,7 +1737,7 @@ static PyObject * sock_setblocking(PySocketSockObject *s, PyObject *arg) { - int block; + long block; block = PyLong_AsLong(arg); if (block == -1 && PyErr_Occurred()) @@ -2219,7 +2219,7 @@ int backlog; int res; - backlog = PyLong_AsLong(arg); + backlog = _PyLong_AsInt(arg); if (backlog == -1 && PyErr_Occurred()) return NULL; Py_BEGIN_ALLOW_THREADS @@ -2822,7 +2822,7 @@ int how; int res; - how = PyLong_AsLong(arg); + how = _PyLong_AsInt(arg); if (how == -1 && PyErr_Occurred()) return NULL; Py_BEGIN_ALLOW_THREADS diff -r 0f4d4f4db724 Objects/fileobject.c --- a/Objects/fileobject.c Wed Oct 03 03:16:42 2012 +0200 +++ b/Objects/fileobject.c Wed Oct 03 10:12:33 2012 +0300 @@ -200,7 +200,7 @@ PyObject *meth; if (PyLong_Check(o)) { - fd = PyLong_AsLong(o); + fd = _PyLong_AsInt(o); } else if ((meth = PyObject_GetAttrString(o, "fileno")) != NULL) { @@ -210,7 +210,7 @@ return -1; if (PyLong_Check(fno)) { - fd = PyLong_AsLong(fno); + fd = _PyLong_AsInt(fno); Py_DECREF(fno); } else { diff -r 0f4d4f4db724 Objects/longobject.c --- a/Objects/longobject.c Wed Oct 03 03:16:42 2012 +0200 +++ b/Objects/longobject.c Wed Oct 03 10:12:33 2012 +0300 @@ -424,6 +424,24 @@ return result; } +/* Get a C int from a long int object or any object that has an __int__ + method. Return -1 and set an error if overflow occurs. */ + +int +_PyLong_AsInt(PyObject *obj) +{ + int overflow; + long result = PyLong_AsLongAndOverflow(obj, &overflow); + if (overflow || result > INT_MAX || result < INT_MIN) { + /* XXX: could be cute and give a different + message for overflow == -1 */ + PyErr_SetString(PyExc_OverflowError, + "Python int too large to convert to C int"); + return -1; + } + return (int)result; +} + /* Get a Py_ssize_t from a long int object. Returns -1 and sets an error condition if overflow occurs. */ diff -r 0f4d4f4db724 Objects/structseq.c --- a/Objects/structseq.c Wed Oct 03 03:16:42 2012 +0200 +++ b/Objects/structseq.c Wed Oct 03 10:12:33 2012 +0300 @@ -13,14 +13,14 @@ char *PyStructSequence_UnnamedField = "unnamed field"; #define VISIBLE_SIZE(op) Py_SIZE(op) -#define VISIBLE_SIZE_TP(tp) PyLong_AsLong( \ +#define VISIBLE_SIZE_TP(tp) PyLong_AsSsize_t( \ PyDict_GetItemString((tp)->tp_dict, visible_length_key)) -#define REAL_SIZE_TP(tp) PyLong_AsLong( \ +#define REAL_SIZE_TP(tp) PyLong_AsSsize_t( \ PyDict_GetItemString((tp)->tp_dict, real_length_key)) #define REAL_SIZE(op) REAL_SIZE_TP(Py_TYPE(op)) -#define UNNAMED_FIELDS_TP(tp) PyLong_AsLong( \ +#define UNNAMED_FIELDS_TP(tp) PyLong_AsSsize_t( \ PyDict_GetItemString((tp)->tp_dict, unnamed_fields_key)) #define UNNAMED_FIELDS(op) UNNAMED_FIELDS_TP(Py_TYPE(op)) @@ -161,7 +161,8 @@ #define TYPE_MAXSIZE 100 PyTypeObject *typ = Py_TYPE(obj); - int i, removelast = 0; + Py_ssize_t i; + int removelast = 0; Py_ssize_t len; char buf[REPR_BUFFER_SIZE]; char *endofbuf, *pbuf = buf; @@ -233,8 +234,7 @@ PyObject* tup; PyObject* dict; PyObject* result; - Py_ssize_t n_fields, n_visible_fields, n_unnamed_fields; - int i; + Py_ssize_t n_fields, n_visible_fields, n_unnamed_fields, i; n_fields = REAL_SIZE(self); n_visible_fields = VISIBLE_SIZE(self); @@ -320,7 +320,7 @@ { PyObject *dict; PyMemberDef* members; - int n_members, n_unnamed_members, i, k; + Py_ssize_t n_members, n_unnamed_members, i, k; #ifdef Py_TRACE_REFS /* if the type object was chained, unchain it first @@ -365,18 +365,18 @@ Py_INCREF(type); dict = type->tp_dict; -#define SET_DICT_FROM_INT(key, value) \ +#define SET_DICT_FROM_SIZE(key, value) \ do { \ - PyObject *v = PyLong_FromLong((long) value); \ + PyObject *v = PyLong_FromSsize_t(value); \ if (v != NULL) { \ PyDict_SetItemString(dict, key, v); \ Py_DECREF(v); \ } \ } while (0) - SET_DICT_FROM_INT(visible_length_key, desc->n_in_sequence); - SET_DICT_FROM_INT(real_length_key, n_members); - SET_DICT_FROM_INT(unnamed_fields_key, n_unnamed_members); + SET_DICT_FROM_SIZE(visible_length_key, desc->n_in_sequence); + SET_DICT_FROM_SIZE(real_length_key, n_members); + SET_DICT_FROM_SIZE(unnamed_fields_key, n_unnamed_members); } PyTypeObject* diff -r 0f4d4f4db724 Objects/unicodeobject.c --- a/Objects/unicodeobject.c Wed Oct 03 03:16:42 2012 +0200 +++ b/Objects/unicodeobject.c Wed Oct 03 10:12:33 2012 +0300 @@ -9632,7 +9632,7 @@ "* wants int"); goto onError; } - width = PyLong_AsLong(v); + width = PyLong_AsSsize_t(v); if (width == -1 && PyErr_Occurred()) goto onError; if (width < 0) { @@ -9669,7 +9669,7 @@ "* wants int"); goto onError; } - prec = PyLong_AsLong(v); + prec = _PyLong_AsInt(v); if (prec == -1 && PyErr_Occurred()) goto onError; if (prec < 0) diff -r 0f4d4f4db724 Python/Python-ast.c --- a/Python/Python-ast.c Wed Oct 03 03:16:42 2012 +0200 +++ b/Python/Python-ast.c Wed Oct 03 10:12:33 2012 +0300 @@ -626,7 +626,7 @@ return 1; } - i = (int)PyLong_AsLong(obj); + i = _PyLong_AsInt(obj); if (i == -1 && PyErr_Occurred()) return 1; *out = i; diff -r 0f4d4f4db724 Python/compile.c --- a/Python/compile.c Wed Oct 03 03:16:42 2012 +0200 +++ b/Python/compile.c Wed Oct 03 10:12:33 2012 +0300 @@ -396,6 +396,7 @@ v = PyDict_GetItem(src, k); assert(PyLong_Check(v)); vi = PyLong_AS_LONG(v); + assert(!PyErr_Occurred()); scope = (vi >> SCOPE_OFFSET) & SCOPE_MASK; if (scope == scope_type || vi & flag) { @@ -971,6 +972,12 @@ Py_DECREF(t); return -1; } + arg = _PyLong_AsInt(v); + if (arg == -1 && PyErr_Occurred()) { + Py_DECREF(t); + Py_DECREF(v); + return -1; + } if (PyDict_SetItem(dict, t, v) < 0) { Py_DECREF(t); Py_DECREF(v); @@ -979,7 +986,7 @@ Py_DECREF(v); } else - arg = PyLong_AsLong(v); + arg = _PyLong_AsInt(v); Py_DECREF(t); return arg; } diff -r 0f4d4f4db724 Python/pythonrun.c --- a/Python/pythonrun.c Wed Oct 03 03:16:42 2012 +0200 +++ b/Python/pythonrun.c Wed Oct 03 10:12:33 2012 +0300 @@ -1334,7 +1334,7 @@ parse_syntax_error(PyObject *err, PyObject **message, const char **filename, int *lineno, int *offset, const char **text) { - long hold; + int hold; PyObject *v; /* old style errors */ @@ -1366,11 +1366,11 @@ v = PyObject_GetAttrString(err, "lineno"); if (!v) goto finally; - hold = PyLong_AsLong(v); + hold = _PyLong_AsInt(v); Py_DECREF(v); if (hold < 0 && PyErr_Occurred()) goto finally; - *lineno = (int)hold; + *lineno = hold; v = PyObject_GetAttrString(err, "offset"); if (!v) @@ -1379,11 +1379,11 @@ *offset = -1; Py_DECREF(v); } else { - hold = PyLong_AsLong(v); + hold = _PyLong_AsInt(v); Py_DECREF(v); if (hold < 0 && PyErr_Occurred()) goto finally; - *offset = (int)hold; + *offset = hold; } v = PyObject_GetAttrString(err, "text");