Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 6d29352

Browse files
committed
Issue #15895: my analysis was slightly off. The FILE pointer is only leaked when set_main_loader() fails for a pyc file with closeit=0. In the success case run_pyc_file() does its own cleanup of the fp. I've changed the code to use another FILE ptr for pyc files and moved the fclose() to PyRun_SimpleFileExFlags() to make it more obvious what's happening.
1 parent 6a77af6 commit 6d29352

2 files changed

Lines changed: 8 additions & 9 deletions

File tree

Misc/NEWS

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ What's New in Python 3.3.1
1010
Core and Builtins
1111
-----------------
1212

13-
- Issue #15895: Fix FILE pointer leak in PyRun_SimpleFileExFlags() when
13+
- Issue #15895: Fix FILE pointer leak in one error branch of
14+
PyRun_SimpleFileExFlags() when
1415
filename points to a pyc/pyo file and closeit is false.
1516

1617
- Issue #15900: Fix reference leak in PyUnicode_TranslateCharmap().

Python/pythonrun.c

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1385,7 +1385,7 @@ PyRun_SimpleFileExFlags(FILE *fp, const char *filename, int closeit,
13851385
{
13861386
PyObject *m, *d, *v;
13871387
const char *ext;
1388-
int set_file_name = 0, close_own_fp = 0, ret;
1388+
int set_file_name = 0, ret;
13891389
size_t len;
13901390

13911391
m = PyImport_AddModule("__main__");
@@ -1411,25 +1411,27 @@ PyRun_SimpleFileExFlags(FILE *fp, const char *filename, int closeit,
14111411
len = strlen(filename);
14121412
ext = filename + len - (len > 4 ? 4 : 0);
14131413
if (maybe_pyc_file(fp, filename, ext, closeit)) {
1414+
FILE *pyc_fp;
14141415
/* Try to run a pyc file. First, re-open in binary */
14151416
if (closeit)
14161417
fclose(fp);
1417-
if ((fp = fopen(filename, "rb")) == NULL) {
1418+
if ((pyc_fp = fopen(filename, "rb")) == NULL) {
14181419
fprintf(stderr, "python: Can't reopen .pyc file\n");
14191420
ret = -1;
14201421
goto done;
14211422
}
1422-
close_own_fp = 1;
14231423
/* Turn on optimization if a .pyo file is given */
14241424
if (strcmp(ext, ".pyo") == 0)
14251425
Py_OptimizeFlag = 1;
14261426

14271427
if (set_main_loader(d, filename, "SourcelessFileLoader") < 0) {
14281428
fprintf(stderr, "python: failed to set __main__.__loader__\n");
14291429
ret = -1;
1430+
fclose(pyc_fp);
14301431
goto done;
14311432
}
1432-
v = run_pyc_file(fp, filename, d, d, flags);
1433+
v = run_pyc_file(pyc_fp, filename, d, d, flags);
1434+
fclose(pyc_fp);
14331435
} else {
14341436
/* When running from stdin, leave __main__.__loader__ alone */
14351437
if (strcmp(filename, "<stdin>") != 0 &&
@@ -1450,9 +1452,6 @@ PyRun_SimpleFileExFlags(FILE *fp, const char *filename, int closeit,
14501452
Py_DECREF(v);
14511453
ret = 0;
14521454
done:
1453-
if (close_own_fp) {
1454-
fclose(fp);
1455-
}
14561455
if (set_file_name && PyDict_DelItemString(d, "__file__"))
14571456
PyErr_Clear();
14581457
return ret;
@@ -1999,7 +1998,6 @@ run_pyc_file(FILE *fp, const char *filename, PyObject *globals,
19991998
(void) PyMarshal_ReadLongFromFile(fp);
20001999
(void) PyMarshal_ReadLongFromFile(fp);
20012000
v = PyMarshal_ReadLastObjectFromFile(fp);
2002-
fclose(fp);
20032001
if (v == NULL || !PyCode_Check(v)) {
20042002
Py_XDECREF(v);
20052003
PyErr_SetString(PyExc_RuntimeError,

0 commit comments

Comments
 (0)