diff -r 65f2c92ed079 Include/asdl.h --- a/Include/asdl.h Sun Jul 07 23:30:24 2013 +0200 +++ b/Include/asdl.h Wed Jul 10 07:32:26 2013 -0600 @@ -29,10 +29,11 @@ asdl_int_seq *asdl_int_seq_new(Py_ssize_t size, PyArena *arena); #define asdl_seq_GET(S, I) (S)->elements[(I)] -#define asdl_seq_LEN(S) ((S) == NULL ? 0 : (S)->size) +#define asdl_seq_LEN(S) \ + ((S) == NULL ? 0 : Py_SAFE_DOWNCAST((S)->size, Py_ssize_t, int)) #ifdef Py_DEBUG #define asdl_seq_SET(S, I, V) { \ - int _asdl_i = (I); \ + Py_ssize_t _asdl_i = (I); \ assert((S) && _asdl_i < (S)->size); \ (S)->elements[_asdl_i] = (V); \ } diff -r 65f2c92ed079 Objects/codeobject.c --- a/Objects/codeobject.c Sun Jul 07 23:30:24 2013 +0200 +++ b/Objects/codeobject.c Wed Jul 10 07:32:26 2013 -0600 @@ -57,7 +57,7 @@ { PyCodeObject *co; unsigned char *cell2arg = NULL; - Py_ssize_t i, n_cellvars; + Py_ssize_t i, n_cellvars, total_args; /* Check argument types */ if (argcount < 0 || kwonlyargcount < 0 || nlocals < 0 || @@ -74,6 +74,12 @@ PyErr_BadInternalCall(); return NULL; } + total_args = argcount + kwonlyargcount + + ((flags & CO_VARARGS) != 0) + ((flags & CO_VARKEYWORDS) != 0); + if (total_args > 255) { + PyErr_SetString(PyExc_ValueError, "more than 255 arguments"); + return NULL; + } n_cellvars = PyTuple_GET_SIZE(cellvars); intern_strings(names); intern_strings(varnames); @@ -88,8 +94,6 @@ } /* Create mapping between cells and arguments if needed. */ if (n_cellvars) { - Py_ssize_t total_args = argcount + kwonlyargcount + - ((flags & CO_VARARGS) != 0) + ((flags & CO_VARKEYWORDS) != 0); Py_ssize_t alloc_size = sizeof(unsigned char) * n_cellvars; int used_cell2arg = 0; cell2arg = PyMem_MALLOC(alloc_size); @@ -98,7 +102,7 @@ memset(cell2arg, CO_CELL_NOT_AN_ARG, alloc_size); /* Find cells which are also arguments. */ for (i = 0; i < n_cellvars; i++) { - Py_ssize_t j; + unsigned char j; PyObject *cell = PyTuple_GET_ITEM(cellvars, i); for (j = 0; j < total_args; j++) { PyObject *arg = PyTuple_GET_ITEM(varnames, j); diff -r 65f2c92ed079 Objects/funcobject.c --- a/Objects/funcobject.c Sun Jul 07 23:30:24 2013 +0200 +++ b/Objects/funcobject.c Wed Jul 10 07:32:26 2013 -0600 @@ -633,8 +633,10 @@ result = PyEval_EvalCodeEx( PyFunction_GET_CODE(func), PyFunction_GET_GLOBALS(func), (PyObject *)NULL, - &PyTuple_GET_ITEM(arg, 0), PyTuple_GET_SIZE(arg), - k, nk, d, nd, + &PyTuple_GET_ITEM(arg, 0), + Py_SAFE_DOWNCAST(PyTuple_GET_SIZE(arg), Py_ssize_t, int), + k, Py_SAFE_DOWNCAST(nk, Py_ssize_t, int), + d, Py_SAFE_DOWNCAST(nd, Py_ssize_t, int), PyFunction_GET_KW_DEFAULTS(func), PyFunction_GET_CLOSURE(func)); diff -r 65f2c92ed079 Objects/obmalloc.c --- a/Objects/obmalloc.c Sun Jul 07 23:30:24 2013 +0200 +++ b/Objects/obmalloc.c Wed Jul 10 07:32:26 2013 -0600 @@ -1261,7 +1261,8 @@ pool = (poolp)usable_arenas->pool_address; assert((block*)pool <= (block*)usable_arenas->address + ARENA_SIZE - POOL_SIZE); - pool->arenaindex = usable_arenas - arenas; + pool->arenaindex = Py_SAFE_DOWNCAST(usable_arenas - arenas, + Py_intptr_t, int); assert(&arenas[pool->arenaindex] == usable_arenas); pool->szidx = DUMMY_SIZE_IDX; usable_arenas->pool_address += POOL_SIZE; diff -r 65f2c92ed079 Parser/grammar.c --- a/Parser/grammar.c Sun Jul 07 23:30:24 2013 +0200 +++ b/Parser/grammar.c Wed Jul 10 07:32:26 2013 -0600 @@ -63,7 +63,7 @@ s->s_upper = 0; s->s_accel = NULL; s->s_accept = 0; - return s - d->d_state; + return Py_SAFE_DOWNCAST(s - d->d_state, Py_intptr_t, int); } void @@ -105,7 +105,7 @@ if (Py_DebugFlag) printf("Label @ %8p, %d: %s\n", ll, ll->ll_nlabels, PyGrammar_LabelRepr(lb)); - return lb - ll->ll_label; + return Py_SAFE_DOWNCAST(lb - ll->ll_label, Py_intptr_t, int); } /* Same, but rather dies than adds */ diff -r 65f2c92ed079 Parser/parsetok.c --- a/Parser/parsetok.c Sun Jul 07 23:30:24 2013 +0200 +++ b/Parser/parsetok.c Wed Jul 10 07:32:26 2013 -0600 @@ -205,7 +205,8 @@ } #endif if (a >= tok->line_start) - col_offset = a - tok->line_start; + col_offset = Py_SAFE_DOWNCAST(a - tok->line_start, + Py_intptr_t, int); else col_offset = -1; diff -r 65f2c92ed079 Parser/tokenizer.c --- a/Parser/tokenizer.c Sun Jul 07 23:30:24 2013 +0200 +++ b/Parser/tokenizer.c Wed Jul 10 07:32:26 2013 -0600 @@ -692,7 +692,7 @@ current++; } *current = '\0'; - final_length = current - buf + 1; + final_length = Py_SAFE_DOWNCAST(current - buf + 1, Py_intptr_t, int); if (final_length < needed_length && final_length) /* should never fail */ buf = PyMem_REALLOC(buf, final_length); diff -r 65f2c92ed079 Python/ceval.c --- a/Python/ceval.c Sun Jul 07 23:30:24 2013 +0200 +++ b/Python/ceval.c Wed Jul 10 07:32:26 2013 -0600 @@ -4268,7 +4268,7 @@ } if (argdefs != NULL) { d = &PyTuple_GET_ITEM(argdefs, 0); - nd = Py_SIZE(argdefs); + nd = Py_SAFE_DOWNCAST(Py_SIZE(argdefs), Py_ssize_t, int); } return PyEval_EvalCodeEx((PyObject*)co, globals, (PyObject *)NULL, (*pp_stack)-n, na, @@ -4456,7 +4456,7 @@ Py_DECREF(stararg); stararg = t; } - nstar = PyTuple_GET_SIZE(stararg); + nstar = Py_SAFE_DOWNCAST(PyTuple_GET_SIZE(stararg), Py_ssize_t, int); } if (nk > 0) { kwdict = update_keyword_args(kwdict, nk, pp_stack, func); diff -r 65f2c92ed079 Python/compile.c --- a/Python/compile.c Sun Jul 07 23:30:24 2013 +0200 +++ b/Python/compile.c Wed Jul 10 07:32:26 2013 -0600 @@ -375,7 +375,7 @@ n = PyList_Size(list); for (i = 0; i < n; i++) { - v = PyLong_FromLong(i); + v = PyLong_FromSsize_t(i); if (!v) { Py_DECREF(dict); return NULL; @@ -437,7 +437,7 @@ scope = (vi >> SCOPE_OFFSET) & SCOPE_MASK; if (scope == scope_type || vi & flag) { - PyObject *tuple, *item = PyLong_FromLong(i); + PyObject *tuple, *item = PyLong_FromSsize_t(i); if (item == NULL) { Py_DECREF(sorted_keys); Py_DECREF(dest); @@ -568,7 +568,8 @@ } u->u_freevars = dictbytype(u->u_ste->ste_symbols, FREE, DEF_FREE_CLASS, - PyDict_Size(u->u_cellvars)); + Py_SAFE_DOWNCAST(PyDict_Size(u->u_cellvars), + Py_ssize_t, int)); if (!u->u_freevars) { compiler_unit_free(u); return 0; @@ -617,7 +618,7 @@ static void compiler_exit_scope(struct compiler *c) { - int n; + Py_ssize_t n; PyObject *capsule; c->c_nestlevel--; @@ -1090,7 +1091,7 @@ if (PyErr_Occurred()) return -1; arg = PyDict_Size(dict); - v = PyLong_FromLong(arg); + v = PyLong_FromSsize_t(arg); if (!v) { Py_DECREF(t); return -1; @@ -1103,9 +1104,9 @@ Py_DECREF(v); } else - arg = PyLong_AsLong(v); + arg = PyLong_AsSsize_t(v); Py_DECREF(t); - return arg; + return Py_SAFE_DOWNCAST(arg, Py_ssize_t, int); } static int @@ -1401,7 +1402,7 @@ static int compiler_make_closure(struct compiler *c, PyCodeObject *co, int args, PyObject *qualname) { - int i, free = PyCode_GetNumFree(co); + int i, free = Py_SAFE_DOWNCAST(PyCode_GetNumFree(co), Py_ssize_t, int); if (qualname == NULL) qualname = co->co_name; @@ -1528,7 +1529,7 @@ */ static identifier return_str; PyObject *names; - int len; + Py_ssize_t len; names = PyList_New(0); if (!names) return -1; @@ -1579,7 +1580,8 @@ len++; /* include the just-pushed tuple */ } Py_DECREF(names); - return len; + /* len is <= 65535 per check above */ + return (int)len; error: Py_DECREF(names); @@ -3886,7 +3888,7 @@ assemble_lnotab(struct assembler *a, struct instr *i) { int d_bytecode, d_lineno; - int len; + Py_ssize_t len; unsigned char *lnotab; d_bytecode = a->a_offset - a->a_lineno_off; @@ -4102,7 +4104,8 @@ compute_code_flags(struct compiler *c) { PySTEntryObject *ste = c->u->u_ste; - int flags = 0, n; + int flags = 0; + Py_ssize_t n; if (ste->ste_type == FunctionBlock) { flags |= CO_NEWLOCALS; if (!ste->ste_unoptimized) @@ -4147,7 +4150,7 @@ PyObject *freevars = NULL; PyObject *cellvars = NULL; PyObject *bytecode = NULL; - int nlocals, flags; + int nvars, flags; tmp = dict_keys_inorder(c->u->u_consts, 0); if (!tmp) @@ -4163,10 +4166,11 @@ cellvars = dict_keys_inorder(c->u->u_cellvars, 0); if (!cellvars) goto error; - freevars = dict_keys_inorder(c->u->u_freevars, PyTuple_Size(cellvars)); + nvars = Py_SAFE_DOWNCAST(PyTuple_GET_SIZE(cellvars), Py_ssize_t, int); + freevars = dict_keys_inorder(c->u->u_freevars, nvars); if (!freevars) goto error; - nlocals = PyDict_Size(c->u->u_varnames); + nvars = Py_SAFE_DOWNCAST(PyDict_Size(c->u->u_varnames), Py_ssize_t, int); flags = compute_code_flags(c); if (flags < 0) goto error; @@ -4182,7 +4186,7 @@ consts = tmp; co = PyCode_New(c->u->u_argcount, c->u->u_kwonlyargcount, - nlocals, stackdepth(c), flags, + nvars, stackdepth(c), flags, bytecode, consts, names, varnames, freevars, cellvars, c->c_filename_obj, c->u->u_name, diff -r 65f2c92ed079 Python/dtoa.c --- a/Python/dtoa.c Sun Jul 07 23:30:24 2013 +0200 +++ b/Python/dtoa.c Wed Jul 10 07:32:26 2013 -0600 @@ -1566,7 +1566,7 @@ s0 = s1 = s; while ('0' <= c && c <= '9') c = *++s; - nd0 = nd = s - s1; + nd0 = nd = Py_SAFE_DOWNCAST(s - s1, Py_intptr_t, int); /* Parse decimal point and following digits. */ if (c == '.') { @@ -1576,13 +1576,13 @@ while (c == '0') c = *++s; lz = lz || s != s1; - nd0 -= s - s1; + nd0 -= Py_SAFE_DOWNCAST(s - s1, Py_intptr_t, int); s0 = s; } s1 = s; while ('0' <= c && c <= '9') c = *++s; - nd += s - s1; + nd += Py_SAFE_DOWNCAST(s - s1, Py_intptr_t, int); } /* Now lz is true if and only if there were leading zero digits, and nd diff -r 65f2c92ed079 Python/getargs.c --- a/Python/getargs.c Sun Jul 07 23:30:24 2013 +0200 +++ b/Python/getargs.c Wed Jul 10 07:32:26 2013 -0600 @@ -55,11 +55,11 @@ /* Forward */ static int vgetargs1(PyObject *, const char *, va_list *, int); -static void seterror(Py_ssize_t, const char *, int *, const char *, const char *); -static char *convertitem(PyObject *, const char **, va_list *, int, int *, +static void seterror(Py_ssize_t, const char *, Py_ssize_t *, const char *, const char *); +static char *convertitem(PyObject *, const char **, va_list *, int, Py_ssize_t *, char *, size_t, freelist_t *); static char *converttuple(PyObject *, const char **, va_list *, int, - int *, char *, size_t, int, freelist_t *); + Py_ssize_t *, char *, size_t, int, freelist_t *); static char *convertsimple(PyObject *, const char **, va_list *, int, char *, size_t, freelist_t *); static Py_ssize_t convertbuffer(PyObject *, void **p, char **); @@ -199,14 +199,14 @@ vgetargs1(PyObject *args, const char *format, va_list *p_va, int flags) { char msgbuf[256]; - int levels[32]; + Py_ssize_t levels[32]; freelistentry_t static_entries[STATIC_FREELIST_ENTRIES]; freelist_t freelist = {static_entries, 0, 0}; const char *fname = NULL; const char *message = NULL; - int min = -1; - int max = 0; - int level = 0; + Py_ssize_t min = -1; + Py_ssize_t max = 0; + Py_ssize_t level = 0; int endfmt = 0; const char *formatsave = format; Py_ssize_t i, len; @@ -357,7 +357,7 @@ static void -seterror(Py_ssize_t iarg, const char *msg, int *levels, const char *fname, +seterror(Py_ssize_t iarg, const char *msg, Py_ssize_t *levels, const char *fname, const char *message) { char buf[512]; @@ -378,7 +378,7 @@ p += strlen(p); while (levels[i] > 0 && i < 32 && (int)(p-buf) < 220) { PyOS_snprintf(p, sizeof(buf) - (p - buf), - ", item %d", levels[i]-1); + ", item %" PY_FORMAT_SIZE_T "d", levels[i]-1); p += strlen(p); i++; } @@ -414,13 +414,13 @@ static char * converttuple(PyObject *arg, const char **p_format, va_list *p_va, int flags, - int *levels, char *msgbuf, size_t bufsize, int toplevel, + Py_ssize_t *levels, char *msgbuf, size_t bufsize, int toplevel, freelist_t *freelist) { int level = 0; int n = 0; const char *format = *p_format; - int i; + Py_ssize_t i; for (;;) { int c = *format++; @@ -490,7 +490,7 @@ static char * convertitem(PyObject *arg, const char **p_format, va_list *p_va, int flags, - int *levels, char *msgbuf, size_t bufsize, freelist_t *freelist) + Py_ssize_t *levels, char *msgbuf, size_t bufsize, freelist_t *freelist) { char *msg; const char *format = *p_format; @@ -1422,11 +1422,12 @@ char **kwlist, va_list *p_va, int flags) { char msgbuf[512]; - int levels[32]; + Py_ssize_t levels[32]; const char *fname, *msg, *custom_msg, *keyword; int min = INT_MAX; int max = INT_MAX; - int i, len, nargs, nkeywords; + int i; + Py_ssize_t len, nargs, nkeywords; PyObject *current_arg; freelistentry_t static_entries[STATIC_FREELIST_ENTRIES]; freelist_t freelist = {static_entries, 0, 0}; diff -r 65f2c92ed079 Python/marshal.c --- a/Python/marshal.c Sun Jul 07 23:30:24 2013 +0200 +++ b/Python/marshal.c Wed Jul 10 07:32:26 2013 -0600 @@ -569,7 +569,7 @@ data->ob_type->tp_name); } else { - read = (int)PyBytes_GET_SIZE(data); + read = PyBytes_GET_SIZE(data); if (read > 0) { ptr = PyBytes_AS_STRING(data); memcpy(s, ptr, read); diff -r 65f2c92ed079 Python/peephole.c --- a/Python/peephole.c Sun Jul 07 23:30:24 2013 +0200 +++ b/Python/peephole.c Wed Jul 10 07:32:26 2013 -0600 @@ -18,7 +18,9 @@ || op==JUMP_IF_FALSE_OR_POP || op==JUMP_IF_TRUE_OR_POP) #define JUMPS_ON_TRUE(op) (op==POP_JUMP_IF_TRUE || op==JUMP_IF_TRUE_OR_POP) #define GETJUMPTGT(arr, i) (GETARG(arr,i) + (ABSOLUTE_JUMP(arr[i]) ? 0 : i+3)) -#define SETARG(arr, i, val) arr[i+2] = val>>8; arr[i+1] = val & 255 +#define SETARG(arr, i, val) \ + arr[i+2] = Py_SAFE_DOWNCAST(val>>8, Py_ssize_t, unsigned char); \ + arr[i+1] = (unsigned char)(val & 255) #define CODESIZE(op) (HAS_ARG(op) ? 3 : 1) #define ISBASICBLOCK(blocks, start, bytes) \ (blocks[start]==blocks[start+bytes-1]) @@ -348,13 +350,14 @@ PyCode_Optimize(PyObject *code, PyObject* consts, PyObject *names, PyObject *lineno_obj) { - Py_ssize_t i, j, codelen; - int nops, h, adj; - int tgt, tgttgt, opcode; + Py_ssize_t h, i, j, codelen, tgt, tgttgt; + int nops, adj; + int opcode; unsigned char *codestr = NULL; unsigned char *lineno; - int *addrmap = NULL; - int new_line, cum_orig_line, last_line, tabsiz; + Py_ssize_t *addrmap = NULL; + Py_ssize_t new_line, cum_orig_line, last_line; + int tabsiz; PyObject **const_stack = NULL; Py_ssize_t *load_const_stack = NULL; Py_ssize_t const_stack_top = -1; @@ -369,7 +372,7 @@ /* Bypass optimization when the lineno table is too complex */ assert(PyBytes_Check(lineno_obj)); lineno = (unsigned char*)PyBytes_AS_STRING(lineno_obj); - tabsiz = PyBytes_GET_SIZE(lineno_obj); + tabsiz = Py_SAFE_DOWNCAST(PyBytes_GET_SIZE(lineno_obj), Py_ssize_t, int); if (memchr(lineno, 255, tabsiz) != NULL) goto exitUnchanged; @@ -395,7 +398,7 @@ goto exitUnchanged; /* Mapping to new jump targets after NOPs are removed */ - addrmap = (int *)PyMem_Malloc(codelen * sizeof(int)); + addrmap = (Py_ssize_t *)PyMem_Malloc(codelen * sizeof(Py_ssize_t)); if (addrmap == NULL) goto exitError; @@ -578,7 +581,7 @@ tgttgt = GETJUMPTGT(codestr, tgt); /* The current opcode inherits its target's stack behaviour */ - codestr[i] = j; + codestr[i] = (unsigned char)j; SETARG(codestr, i, tgttgt); goto reoptimize_current; } else { diff -r 65f2c92ed079 Python/random.c --- a/Python/random.c Sun Jul 07 23:30:24 2013 +0200 +++ b/Python/random.c Wed Jul 10 07:32:26 2013 -0600 @@ -49,8 +49,8 @@ while (size > 0) { - chunk = size > INT_MAX ? INT_MAX : size; - if (!CryptGenRandom(hCryptProv, chunk, buffer)) + chunk = size > MAXDWORD ? MAXDWORD : size; + if (!CryptGenRandom(hCryptProv, (DWORD)chunk, buffer)) { /* CryptGenRandom() failed */ if (raise)