Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No tests were added. I think Lib/test/test_interpreters/test_lifecycle.py would be right place.

NEWS entry should reference gh-149630, not gh-146102 (that's the umbrella).

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Avoid possible memory leak in ``pylifecycle.c``. Patch by Ivy Xu.
30 changes: 25 additions & 5 deletions Python/pylifecycle.c

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pycore_init_builtins still leaks bimod on the _PyBuiltin_InitPythonFunctions early return (inside config->_install_importlib). Also the two fixed sites should be Py_DECREF not Py_XDECREF, bimod is non-NULL there.

new_interpreter: _PyObject_InitState and _PyStats_InterpInit failure checks still return status instead of goto error, skipping _PyThreadState_Attach.

Original file line number Diff line number Diff line change
Expand Up @@ -916,10 +916,12 @@ pycore_init_builtins(PyThreadState *tstate)
interp->callable_cache.object__getattribute__ = object__getattribute__;

if (_PyType_InitSlotDefs(interp) < 0) {
Py_XDECREF(bimod);
return _PyStatus_ERR("failed to init slotdefs");
}

if (_PyBuiltins_AddExceptions(bimod) < 0) {
Py_XDECREF(bimod);
return _PyStatus_ERR("failed to add exceptions to builtins");
}

Expand Down Expand Up @@ -1826,9 +1828,13 @@ finalize_clear_modules_dict(PyObject *modules)
PyDict_Clear(modules);
}
else {
if (PyObject_CallMethodNoArgs(modules, &_Py_ID(clear)) == NULL) {
PyObject *res = PyObject_CallMethodNoArgs(modules, &_Py_ID(clear));
if (res == NULL) {
PyErr_FormatUnraisable("Exception ignored while clearing sys.modules");
}
else {
Py_DECREF(res);
}
}
}

Expand Down Expand Up @@ -2932,6 +2938,7 @@ add_main_module(PyInterpreterState *interp)
return _PyStatus_ERR("Failed to retrieve builtins module");
}
if (PyDict_SetItemString(d, "__builtins__", bimod) < 0) {
Py_DECREF(bimod);
return _PyStatus_ERR("Failed to initialize __main__.__builtins__");
}
Py_DECREF(bimod);
Expand All @@ -2955,6 +2962,7 @@ add_main_module(PyInterpreterState *interp)
return _PyStatus_ERR("Failed to retrieve BuiltinImporter");
}
if (PyDict_SetItemString(d, "__loader__", loader) < 0) {
Py_DECREF(loader);
return _PyStatus_ERR("Failed to initialize __main__.__loader__");
}
Py_DECREF(loader);
Expand Down Expand Up @@ -3189,8 +3197,14 @@ init_sys_streams(PyThreadState *tstate)
config->stdio_errors);
if (std == NULL)
goto error;
PySys_SetObject("__stdin__", std);
_PySys_SetAttr(&_Py_ID(stdin), std);
if (PySys_SetObject("__stdin__", std) < 0) {
Py_DECREF(std);
goto error;
}
if (_PySys_SetAttr(&_Py_ID(stdin), std) < 0) {
Py_DECREF(std);
goto error;
}
Py_DECREF(std);

/* Set sys.stdout */
Expand All @@ -3200,8 +3214,14 @@ init_sys_streams(PyThreadState *tstate)
config->stdio_errors);
if (std == NULL)
goto error;
PySys_SetObject("__stdout__", std);
_PySys_SetAttr(&_Py_ID(stdout), std);
if (PySys_SetObject("__stdout__", std) < 0) {
Py_DECREF(std);
goto error;
}
if (_PySys_SetAttr(&_Py_ID(stdout), std) < 0) {
Py_DECREF(std);
goto error;
}
Py_DECREF(std);

#if 1 /* Disable this if you have trouble debugging bootstrap stuff */
Expand Down
Loading