https://github.com/python/cpython/commit/4b8c9f5a7b89fbdbee9caa07f5cdf3ae9bbec9ba
commit: 4b8c9f5a7b89fbdbee9caa07f5cdf3ae9bbec9ba
branch: 3.13
author: stratakis <[email protected]>
committer: kumaraditya303 <[email protected]>
date: 2025-12-20T12:48:42+05:30
summary:
[3.13] gh-142776: Ensure fp file descriptor is closed on all code paths in
import.c (GH-142777) (#142989)
gh-142776: Ensure fp file descriptor is closed on all code paths in import.c
(GH-142777)
(cherry picked from commit 6a4f10325d58deb1906b39d68dc8e84f4c2bf5a4)
files:
A Misc/NEWS.d/next/Core and
Builtins/2025-12-18-01-00-14.gh-issue-142776.ACaoeP.rst
M Python/import.c
diff --git a/Misc/NEWS.d/next/Core and
Builtins/2025-12-18-01-00-14.gh-issue-142776.ACaoeP.rst b/Misc/NEWS.d/next/Core
and Builtins/2025-12-18-01-00-14.gh-issue-142776.ACaoeP.rst
new file mode 100644
index 00000000000000..3039b04d89cb88
--- /dev/null
+++ b/Misc/NEWS.d/next/Core and
Builtins/2025-12-18-01-00-14.gh-issue-142776.ACaoeP.rst
@@ -0,0 +1 @@
+Fix a file descriptor leak in import.c
diff --git a/Python/import.c b/Python/import.c
index 64048a4ef91ec3..a002fa736ca4c9 100644
--- a/Python/import.c
+++ b/Python/import.c
@@ -4669,6 +4669,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();
@@ -4711,16 +4712,12 @@ _imp_create_dynamic_impl(PyObject *module, PyObject
*spec, PyObject *file)
/* We would move this (and the fclose() below) into
* _PyImport_GetModInitFunc(), but it isn't clear if the intervening
* code relies on fp still being open. */
- FILE *fp;
if (file != NULL) {
fp = _Py_fopen_obj(info.filename, "r");
if (fp == NULL) {
goto finally;
}
}
- else {
- fp = NULL;
- }
PyModInitFunction p0 = _PyImport_GetModInitFunc(&info, fp);
if (p0 == NULL) {
@@ -4744,12 +4741,10 @@ _imp_create_dynamic_impl(PyObject *module, PyObject
*spec, PyObject *file)
}
#endif
- // 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;
}
_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3//lists/python-checkins.python.org
Member address: [email protected]