Improve the leak fix so that PyTuple_New is only called when needed.

This commit is contained in:
Raymond Hettinger 2003-09-16 04:27:52 +00:00
parent deb2dc6658
commit cc1798e0c0

View file

@ -105,7 +105,7 @@ code_new(PyTypeObject *type, PyObject *args, PyObject *kw)
int stacksize; int stacksize;
int flags; int flags;
PyObject *co; PyObject *co;
PyObject *empty; PyObject *empty = NULL;
PyObject *code; PyObject *code;
PyObject *consts; PyObject *consts;
PyObject *names; PyObject *names;
@ -135,6 +135,7 @@ code_new(PyTypeObject *type, PyObject *args, PyObject *kw)
return NULL; return NULL;
} }
if (freevars == NULL || cellvars == NULL) {
empty = PyTuple_New(0); empty = PyTuple_New(0);
if (empty == NULL) if (empty == NULL)
return NULL; return NULL;
@ -142,12 +143,13 @@ code_new(PyTypeObject *type, PyObject *args, PyObject *kw)
freevars = empty; freevars = empty;
if (cellvars == NULL) if (cellvars == NULL)
cellvars = empty; cellvars = empty;
}
co = (PyObject *) PyCode_New(argcount, nlocals, stacksize, flags, co = (PyObject *) PyCode_New(argcount, nlocals, stacksize, flags,
code, consts, names, varnames, code, consts, names, varnames,
freevars, cellvars, filename, name, freevars, cellvars, filename, name,
firstlineno, lnotab); firstlineno, lnotab);
Py_DECREF(empty); Py_XDECREF(empty);
return co; return co;
} }