Fix error handling in PyCode_Optimize, by Alexander Schremmer at EuroPython sprint.

This commit is contained in:
Georg Brandl 2009-06-29 14:44:49 +00:00
parent 99ebc63f49
commit e323e0e91a
1 changed files with 9 additions and 6 deletions

View File

@ -305,7 +305,7 @@ PyCode_Optimize(PyObject *code, PyObject* consts, PyObject *names,
/* Bail out if an exception is set */ /* Bail out if an exception is set */
if (PyErr_Occurred()) if (PyErr_Occurred())
goto exitUnchanged; goto exitError;
/* Bypass optimization when the lineno table is too complex */ /* Bypass optimization when the lineno table is too complex */
assert(PyString_Check(lineno_obj)); assert(PyString_Check(lineno_obj));
@ -323,7 +323,7 @@ PyCode_Optimize(PyObject *code, PyObject* consts, PyObject *names,
/* Make a modifiable copy of the code string */ /* Make a modifiable copy of the code string */
codestr = (unsigned char *)PyMem_Malloc(codelen); codestr = (unsigned char *)PyMem_Malloc(codelen);
if (codestr == NULL) if (codestr == NULL)
goto exitUnchanged; goto exitError;
codestr = (unsigned char *)memcpy(codestr, codestr = (unsigned char *)memcpy(codestr,
PyString_AS_STRING(code), codelen); PyString_AS_STRING(code), codelen);
@ -338,11 +338,11 @@ PyCode_Optimize(PyObject *code, PyObject* consts, PyObject *names,
/* Mapping to new jump targets after NOPs are removed */ /* Mapping to new jump targets after NOPs are removed */
addrmap = (int *)PyMem_Malloc(codelen * sizeof(int)); addrmap = (int *)PyMem_Malloc(codelen * sizeof(int));
if (addrmap == NULL) if (addrmap == NULL)
goto exitUnchanged; goto exitError;
blocks = markblocks(codestr, codelen); blocks = markblocks(codestr, codelen);
if (blocks == NULL) if (blocks == NULL)
goto exitUnchanged; goto exitError;
assert(PyList_Check(consts)); assert(PyList_Check(consts));
for (i=0 ; i<codelen ; i += CODESIZE(codestr[i])) { for (i=0 ; i<codelen ; i += CODESIZE(codestr[i])) {
@ -394,7 +394,7 @@ PyCode_Optimize(PyObject *code, PyObject* consts, PyObject *names,
} }
if (j == PyList_GET_SIZE(consts)) { if (j == PyList_GET_SIZE(consts)) {
if (PyList_Append(consts, Py_None) == -1) if (PyList_Append(consts, Py_None) == -1)
goto exitUnchanged; goto exitError;
} }
assert(PyList_GET_ITEM(consts, j) == Py_None); assert(PyList_GET_ITEM(consts, j) == Py_None);
codestr[i] = LOAD_CONST; codestr[i] = LOAD_CONST;
@ -647,6 +647,9 @@ PyCode_Optimize(PyObject *code, PyObject* consts, PyObject *names,
PyMem_Free(blocks); PyMem_Free(blocks);
return code; return code;
exitError:
code = NULL;
exitUnchanged: exitUnchanged:
if (blocks != NULL) if (blocks != NULL)
PyMem_Free(blocks); PyMem_Free(blocks);
@ -654,6 +657,6 @@ PyCode_Optimize(PyObject *code, PyObject* consts, PyObject *names,
PyMem_Free(addrmap); PyMem_Free(addrmap);
if (codestr != NULL) if (codestr != NULL)
PyMem_Free(codestr); PyMem_Free(codestr);
Py_INCREF(code); Py_XINCREF(code);
return code; return code;
} }