Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 37a724d

Browse files
committed
Fix leak discovered in test_new by Michael Hudson.
Will backport to 2.3.1
1 parent 1e4cf67 commit 37a724d

1 file changed

Lines changed: 14 additions & 17 deletions

File tree

Python/compile.c

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,8 @@ code_new(PyTypeObject *type, PyObject *args, PyObject *kw)
104104
int nlocals;
105105
int stacksize;
106106
int flags;
107+
PyObject *co;
108+
PyObject *empty;
107109
PyObject *code;
108110
PyObject *consts;
109111
PyObject *names;
@@ -127,31 +129,26 @@ code_new(PyTypeObject *type, PyObject *args, PyObject *kw)
127129
&PyTuple_Type, &cellvars))
128130
return NULL;
129131

130-
if (freevars == NULL || cellvars == NULL) {
131-
PyObject *empty = PyTuple_New(0);
132-
if (empty == NULL)
133-
return NULL;
134-
if (freevars == NULL) {
135-
freevars = empty;
136-
Py_INCREF(freevars);
137-
}
138-
if (cellvars == NULL) {
139-
cellvars = empty;
140-
Py_INCREF(cellvars);
141-
}
142-
Py_DECREF(empty);
143-
}
144-
145132
if (!PyObject_CheckReadBuffer(code)) {
146133
PyErr_SetString(PyExc_TypeError,
147134
"bytecode object must be a single-segment read-only buffer");
148135
return NULL;
149136
}
150137

151-
return (PyObject *)PyCode_New(argcount, nlocals, stacksize, flags,
138+
empty = PyTuple_New(0);
139+
if (empty == NULL)
140+
return NULL;
141+
if (freevars == NULL)
142+
freevars = empty;
143+
if (cellvars == NULL)
144+
cellvars = empty;
145+
146+
co = (PyObject *) PyCode_New(argcount, nlocals, stacksize, flags,
152147
code, consts, names, varnames,
153148
freevars, cellvars, filename, name,
154-
firstlineno, lnotab);
149+
firstlineno, lnotab);
150+
Py_DECREF(empty);
151+
return co;
155152
}
156153

157154
static void

0 commit comments

Comments
 (0)