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
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix a file descriptor leak in import.c
14 changes: 4 additions & 10 deletions Python/import.c
Original file line number Diff line number Diff line change
Expand Up @@ -4762,6 +4762,7 @@ static PyObject *
_imp_create_dynamic_impl(PyObject *module, PyObject *spec, PyObject *file)
/*[clinic end generated code: output=83249b827a4fde77 input=c31b954f4cf4e09d]*/
{
FILE *fp = NULL;
PyObject *mod = NULL;
PyThreadState *tstate = _PyThreadState_GET();

Expand Down Expand Up @@ -4804,16 +4805,12 @@ _imp_create_dynamic_impl(PyObject *module, PyObject *spec, PyObject *file)
/* We would move this (and the fclose() below) into
* _PyImport_GetModuleExportHooks(), but it isn't clear if the intervening
* code relies on fp still being open. */
FILE *fp;
if (file != NULL) {
fp = Py_fopen(info.filename, "r");
if (fp == NULL) {
goto finally;
}
}
else {
fp = NULL;
}

PyModInitFunction p0 = NULL;
PyModExportFunction ex0 = NULL;
Expand All @@ -4822,7 +4819,7 @@ _imp_create_dynamic_impl(PyObject *module, PyObject *spec, PyObject *file)
mod = import_run_modexport(tstate, ex0, &info, spec);
// Modules created from slots handle GIL enablement (Py_mod_gil slot)
// when they're created.
goto cleanup;
goto finally;
}
if (p0 == NULL) {
goto finally;
Expand All @@ -4845,13 +4842,10 @@ _imp_create_dynamic_impl(PyObject *module, PyObject *spec, PyObject *file)
}
#endif

cleanup:
// XXX Shouldn't this happen in the error cases too (i.e. in "finally")?
if (fp) {
finally:
if (fp != NULL) {
fclose(fp);
}

finally:
_Py_ext_module_loader_info_clear(&info);
return mod;
}
Expand Down
Loading