diff --git a/Python/compile.c b/Python/compile.c index 6616c588824..8c259c12305 100644 --- a/Python/compile.c +++ b/Python/compile.c @@ -323,6 +323,99 @@ intern_strings(PyObject *tuple) return 0; } +#define GETARG(arr, i) ((int)((arr[i+2]<<8) + arr[i+1])) +#define UNCONDITIONAL_JUMP(op) (op==JUMP_ABSOLUTE || op==JUMP_FORWARD) +#define GETJUMPTGT(arr, i) (GETARG(arr,i) + (arr[i]==JUMP_ABSOLUTE ? 0 : i+3)) +#define SETARG(arr, i, val) arr[i+2] = val>>8; arr[i+1] = val & 255 + +static PyObject * +optimize_code(PyObject *code, PyObject* consts) +{ + int i, j, codelen; + int tgt, tgttgt, opcode; + unsigned char *codestr; + + /* Make a modifiable copy of the code string */ + if (!PyString_Check(code)) + goto exitUnchanged; + codelen = PyString_Size(code); + codestr = PyMem_Malloc(codelen); + if (codestr == NULL) + goto exitUnchanged; + codestr = memcpy(codestr, PyString_AS_STRING(code), codelen); + assert(PyTuple_Check(consts)); + + for (i=0 ; ico_nlocals = nlocals; co->co_stacksize = stacksize; co->co_flags = flags; - Py_INCREF(code); - co->co_code = code; + co->co_code = optimize_code(code, consts); Py_INCREF(consts); co->co_consts = consts; Py_INCREF(names);