diff -r 05973411b31e Python/compile.c --- a/Python/compile.c Fri Nov 15 23:26:25 2013 +0100 +++ b/Python/compile.c Sat Nov 16 00:06:58 2013 +0100 @@ -170,7 +170,7 @@ static basicblock *compiler_new_block(st static int compiler_next_instr(struct compiler *, basicblock *); static int compiler_addop(struct compiler *, int); static int compiler_addop_o(struct compiler *, int, PyObject *, PyObject *); -static int compiler_addop_i(struct compiler *, int, int); +static int compiler_addop_i(struct compiler *, int, Py_ssize_t); static int compiler_addop_j(struct compiler *, int, basicblock *, int); static basicblock *compiler_use_new_block(struct compiler *); static int compiler_error(struct compiler *, const char *); @@ -195,7 +195,7 @@ static int inplace_binop(struct compiler static int expr_constant(struct compiler *, expr_ty); static int compiler_with(struct compiler *, stmt_ty, int); -static int compiler_call_helper(struct compiler *c, int n, +static int compiler_call_helper(struct compiler *c, Py_ssize_t n, asdl_seq *args, asdl_seq *keywords, expr_ty starargs, @@ -388,7 +388,7 @@ list2dict(PyObject *list) 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; @@ -416,7 +416,7 @@ each key. */ static PyObject * -dictbytype(PyObject *src, int scope_type, int flag, int offset) +dictbytype(PyObject *src, int scope_type, int flag, Py_ssize_t offset) { Py_ssize_t i = offset, scope, num_keys, key_i; PyObject *k, *v, *dest = PyDict_New(); @@ -450,7 +450,7 @@ dictbytype(PyObject *src, int scope_type 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); @@ -635,7 +635,7 @@ compiler_enter_scope(struct compiler *c, static void compiler_exit_scope(struct compiler *c) { - int n; + Py_ssize_t n; PyObject *capsule; c->c_nestlevel--; @@ -1128,7 +1128,7 @@ compiler_add_o(struct compiler *c, PyObj 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; @@ -1150,7 +1150,7 @@ static int compiler_addop_o(struct compiler *c, int opcode, PyObject *dict, PyObject *o) { - int arg = compiler_add_o(c, dict, o); + Py_ssize_t arg = compiler_add_o(c, dict, o); if (arg < 0) return 0; return compiler_addop_i(c, opcode, arg); @@ -1160,7 +1160,7 @@ static int compiler_addop_name(struct compiler *c, int opcode, PyObject *dict, PyObject *o) { - int arg; + Py_ssize_t arg; PyObject *mangled = _Py_Mangle(c->u->u_private, o); if (!mangled) return 0; @@ -1176,10 +1176,16 @@ compiler_addop_name(struct compiler *c, */ static int -compiler_addop_i(struct compiler *c, int opcode, int oparg) +compiler_addop_i(struct compiler *c, int opcode, Py_ssize_t oparg) { struct instr *i; int off; + + /* Integer arguments are limit to 16-bit. There is an extension for 32-bit + integer arguments. */ + assert(0 <= opcode); + assert(opcode <= INT_MAX); + off = compiler_next_instr(c, c->u->u_curblock); if (off < 0) return 0; @@ -1436,9 +1442,9 @@ compiler_lookup_arg(PyObject *dict, PyOb } static int -compiler_make_closure(struct compiler *c, PyCodeObject *co, int args, PyObject *qualname) +compiler_make_closure(struct compiler *c, PyCodeObject *co, Py_ssize_t args, PyObject *qualname) { - int i, free = PyCode_GetNumFree(co); + Py_ssize_t i, free = PyCode_GetNumFree(co); if (qualname == NULL) qualname = co->co_name; @@ -1565,7 +1571,7 @@ compiler_visit_annotations(struct compil */ static identifier return_str; PyObject *names; - int len; + Py_ssize_t len; names = PyList_New(0); if (!names) return -1; @@ -1602,7 +1608,7 @@ compiler_visit_annotations(struct compil if (len) { /* convert names to a tuple and place on stack */ PyObject *elt; - int i; + Py_ssize_t i; PyObject *s = PyTuple_New(len); if (!s) goto error; @@ -1616,7 +1622,9 @@ compiler_visit_annotations(struct compil len++; /* include the just-pushed tuple */ } Py_DECREF(names); - return len; + + /* We just checked that len <= 65535, see above */ + return (int)len; error: Py_DECREF(names); @@ -1632,7 +1640,8 @@ compiler_function(struct compiler *c, st expr_ty returns = s->v.FunctionDef.returns; asdl_seq* decos = s->v.FunctionDef.decorator_list; stmt_ty st; - int i, n, docstring, kw_default_count = 0, arglength; + Py_ssize_t i, n, arglength; + int docstring, kw_default_count = 0; int num_annotations; assert(s->kind == FunctionDef_kind); @@ -1851,7 +1860,8 @@ compiler_lambda(struct compiler *c, expr PyCodeObject *co; PyObject *qualname; static identifier name; - int kw_default_count = 0, arglength; + int kw_default_count = 0; + Py_ssize_t arglength; arguments_ty args = e->v.Lambda.args; assert(e->kind == Lambda_kind); @@ -2162,7 +2172,7 @@ static int compiler_try_except(struct compiler *c, stmt_ty s) { basicblock *body, *orelse, *except, *end; - int i, n; + Py_ssize_t i, n; body = compiler_new_block(c); except = compiler_new_block(c); @@ -2328,7 +2338,7 @@ compiler_import(struct compiler *c, stmt module names. XXX Perhaps change the representation to make this case simpler? */ - int i, n = asdl_seq_LEN(s->v.Import.names); + Py_ssize_t i, n = asdl_seq_LEN(s->v.Import.names); for (i = 0; i < n; i++) { alias_ty alias = (alias_ty)asdl_seq_GET(s->v.Import.names, i); @@ -2372,7 +2382,7 @@ compiler_import(struct compiler *c, stmt static int compiler_from_import(struct compiler *c, stmt_ty s) { - int i, n = asdl_seq_LEN(s->v.ImportFrom.names); + Py_ssize_t i, n = asdl_seq_LEN(s->v.ImportFrom.names); PyObject *names = PyTuple_New(n); PyObject *level; @@ -2489,7 +2499,7 @@ compiler_assert(struct compiler *c, stmt static int compiler_visit_stmt(struct compiler *c, stmt_ty s) { - int i, n; + Py_ssize_t i, n; /* Always assign a lineno to the next instruction for a stmt. */ c->u->u_lineno = s->lineno; @@ -2537,12 +2547,12 @@ compiler_visit_stmt(struct compiler *c, if (s->v.Raise.exc) { VISIT(c, expr, s->v.Raise.exc); n++; - if (s->v.Raise.cause) { - VISIT(c, expr, s->v.Raise.cause); - n++; + if (s->v.Raise.cause) { + VISIT(c, expr, s->v.Raise.cause); + n++; + } } - } - ADDOP_I(c, RAISE_VARARGS, n); + ADDOP_I(c, RAISE_VARARGS, (int)n); break; case Try_kind: return compiler_try(c, s); @@ -2702,7 +2712,8 @@ inplace_binop(struct compiler *c, operat static int compiler_nameop(struct compiler *c, identifier name, expr_context_ty ctx) { - int op, scope, arg; + int op, scope; + Py_ssize_t arg; enum { OP_FAST, OP_GLOBAL, OP_DEREF, OP_NAME } optype; PyObject *dict = c->u->u_names; @@ -2828,7 +2839,8 @@ static int compiler_boolop(struct compiler *c, expr_ty e) { basicblock *end; - int jumpi, i, n; + int jumpi; + Py_ssize_t i, n; asdl_seq *s; assert(e->kind == BoolOp_kind); @@ -2854,7 +2866,7 @@ compiler_boolop(struct compiler *c, expr static int compiler_list(struct compiler *c, expr_ty e) { - int n = asdl_seq_LEN(e->v.List.elts); + Py_ssize_t n = asdl_seq_LEN(e->v.List.elts); if (e->v.List.ctx == Store) { int i, seen_star = 0; for (i = 0; i < n; i++) { @@ -2887,7 +2899,7 @@ compiler_list(struct compiler *c, expr_t static int compiler_tuple(struct compiler *c, expr_ty e) { - int n = asdl_seq_LEN(e->v.Tuple.elts); + Py_ssize_t n = asdl_seq_LEN(e->v.Tuple.elts); if (e->v.Tuple.ctx == Store) { int i, seen_star = 0; for (i = 0; i < n; i++) { @@ -2920,7 +2932,7 @@ compiler_tuple(struct compiler *c, expr_ static int compiler_compare(struct compiler *c, expr_ty e) { - int i, n; + Py_ssize_t i, n; basicblock *cleanup = NULL; /* XXX the logic can be cleaned up for 1 or multiple comparisons */ @@ -2976,11 +2988,11 @@ compiler_call(struct compiler *c, expr_t /* shared code between compiler_call and compiler_class */ static int compiler_call_helper(struct compiler *c, - int n, /* Args already pushed */ - asdl_seq *args, - asdl_seq *keywords, - expr_ty starargs, - expr_ty kwargs) + Py_ssize_t n, /* Args already pushed */ + asdl_seq *args, + asdl_seq *keywords, + expr_ty starargs, + expr_ty kwargs) { int code = 0; @@ -3041,7 +3053,7 @@ compiler_comprehension_generator(struct comprehension_ty gen; basicblock *start, *anchor, *skip, *if_cleanup; - int i, n; + Py_ssize_t i, n; start = compiler_new_block(c); skip = compiler_new_block(c); @@ -3380,7 +3392,7 @@ compiler_with(struct compiler *c, stmt_t static int compiler_visit_expr(struct compiler *c, expr_ty e) { - int i, n; + Py_ssize_t i, n; /* If expr e has a different line number than the last expr/stmt, set a new line number for the next instruction. @@ -3409,6 +3421,8 @@ compiler_visit_expr(struct compiler *c, return compiler_ifexp(c, e); case Dict_kind: n = asdl_seq_LEN(e->v.Dict.values); + /* BUILD_MAP parameter is only used to preallocate the dictionary, + it doesn't need to be exact */ ADDOP_I(c, BUILD_MAP, (n>0xFFFF ? 0xFFFF : n)); for (i = 0; i < n; i++) { VISIT(c, expr, @@ -3756,7 +3770,7 @@ compiler_visit_slice(struct compiler *c, case ExtSlice_kind: kindname = "extended slice"; if (ctx != AugStore) { - int i, n = asdl_seq_LEN(s->v.ExtSlice.dims); + Py_ssize_t i, n = asdl_seq_LEN(s->v.ExtSlice.dims); for (i = 0; i < n; i++) { slice_ty sub = (slice_ty)asdl_seq_GET( s->v.ExtSlice.dims, i); @@ -3934,7 +3948,7 @@ static int 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; @@ -4125,7 +4139,7 @@ assemble_jump_offsets(struct assembler * } static PyObject * -dict_keys_inorder(PyObject *dict, int offset) +dict_keys_inorder(PyObject *dict, Py_ssize_t offset) { PyObject *tuple, *k, *v; Py_ssize_t i, pos = 0, size = PyDict_Size(dict); @@ -4150,7 +4164,8 @@ static int 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) @@ -4174,9 +4189,9 @@ compute_code_flags(struct compiler *c) if (n == 0) { n = PyDict_Size(c->u->u_cellvars); if (n < 0) - return -1; + return -1; if (n == 0) { - flags |= CO_NOFREE; + flags |= CO_NOFREE; } } @@ -4195,7 +4210,9 @@ makecode(struct compiler *c, struct asse PyObject *freevars = NULL; PyObject *cellvars = NULL; PyObject *bytecode = NULL; - int nlocals, flags; + Py_ssize_t nlocals; + int nlocals_int; + int flags; tmp = dict_keys_inorder(c->u->u_consts, 0); if (!tmp) @@ -4214,7 +4231,11 @@ makecode(struct compiler *c, struct asse freevars = dict_keys_inorder(c->u->u_freevars, PyTuple_Size(cellvars)); if (!freevars) goto error; + nlocals = PyDict_Size(c->u->u_varnames); + assert(nlocals < INT_MAX); + nlocals_int = Py_SAFE_DOWNCAST(nlocals, Py_ssize_t, int); + flags = compute_code_flags(c); if (flags < 0) goto error; @@ -4230,7 +4251,7 @@ makecode(struct compiler *c, struct asse consts = tmp; co = PyCode_New(c->u->u_argcount, c->u->u_kwonlyargcount, - nlocals, stackdepth(c), flags, + nlocals_int, stackdepth(c), flags, bytecode, consts, names, varnames, freevars, cellvars, c->c_filename, c->u->u_name, @@ -4349,4 +4370,3 @@ PyAST_Compile(mod_ty mod, const char *fi return PyAST_CompileEx(mod, filename, flags, -1, arena); } -