Skip to content

Commit 41cf743

Browse files
authored
Fix leak in _PyCode_Update (#48)
1 parent 2c31f5d commit 41cf743

File tree

3 files changed

+12
-3
lines changed

3 files changed

+12
-3
lines changed

Lib/importlib/_bootstrap_external.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -372,7 +372,7 @@ def _write_atomic(path, data, mode=0o666):
372372
# Whenever MAGIC_NUMBER is changed, the ranges in the magic_values array
373373
# in PC/launcher.c must also be updated.
374374

375-
MAGIC_NUMBER = (9994).to_bytes(2, 'little') + b'\r\n'
375+
MAGIC_NUMBER = (9996).to_bytes(2, 'little') + b'\r\n'
376376
_RAW_MAGIC_NUMBER = int.from_bytes(MAGIC_NUMBER, 'little') # For import.c
377377

378378
_PYCACHE = '__pycache__'

Objects/codeobject.c

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,7 @@ init_code(PyCodeObject *co, struct _PyCodeConstructor *con)
324324
co->co_name = con->name;
325325
Py_INCREF(con->qualname);
326326
co->co_qualname = con->qualname;
327+
327328
co->co_flags = con->flags;
328329

329330
Py_XINCREF(con->code);
@@ -420,6 +421,10 @@ _PyCode_New(struct _PyCodeConstructor *con)
420421
PyErr_NoMemory();
421422
return NULL;
422423
}
424+
co->co_filename = NULL;
425+
co->co_name = NULL;
426+
co->co_qualname = NULL;
427+
423428
init_code(co, con);
424429

425430
return co;
@@ -442,7 +447,11 @@ _PyCode_Update(struct _PyCodeConstructor *con, PyCodeObject *code)
442447
con->columntable = Py_None;
443448
}
444449

445-
init_code(code, con); // TODO: This leaks!
450+
Py_XDECREF(code->co_filename);
451+
Py_XDECREF(code->co_name);
452+
Py_XDECREF(code->co_qualname);
453+
454+
init_code(code, con);
446455

447456
return code;
448457
}

Python/importlib_external.h

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)