Skip to content

Commit 715e2b8

Browse files
iritkatrielgvanrossum
authored andcommitted
fix leak in _PyCode_Update
1 parent 91a7a5e commit 715e2b8

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

Objects/codeobject.c

+9-3
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,7 @@ _PyCode_Validate(struct _PyCodeConstructor *con)
296296
}
297297

298298
static void
299-
init_code(PyCodeObject *co, struct _PyCodeConstructor *con)
299+
init_code(PyCodeObject *co, struct _PyCodeConstructor *con, bool is_new)
300300
{
301301
if (con->localsplusnames) {
302302
int nlocalsplus = (int)PyTuple_GET_SIZE(con->localsplusnames);
@@ -318,6 +318,11 @@ init_code(PyCodeObject *co, struct _PyCodeConstructor *con)
318318
co->co_nfreevars = 0;
319319
}
320320

321+
if(!is_new) {
322+
Py_XDECREF(co->co_filename);
323+
Py_XDECREF(co->co_name);
324+
Py_XDECREF(co->co_qualname);
325+
}
321326
Py_INCREF(con->filename);
322327
co->co_filename = con->filename;
323328
Py_INCREF(con->name);
@@ -420,7 +425,8 @@ _PyCode_New(struct _PyCodeConstructor *con)
420425
PyErr_NoMemory();
421426
return NULL;
422427
}
423-
init_code(co, con);
428+
429+
init_code(co, con, true);
424430

425431
return co;
426432
}
@@ -442,7 +448,7 @@ _PyCode_Update(struct _PyCodeConstructor *con, PyCodeObject *code)
442448
con->columntable = Py_None;
443449
}
444450

445-
init_code(code, con); // TODO: This leaks!
451+
init_code(code, con, false);
446452

447453
return code;
448454
}

0 commit comments

Comments
 (0)