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

Skip to content

Commit d807862

Browse files
authored
bpo-34170: Fix pymain_run_file() (GH-8660)
bpo-34170, bpo-34326: Fix pymain_run_file(): use PyRun_AnyFileExFlags(closeit=1) instead of calling fclose(fp) explicitly to close the input file before running the code.
1 parent 46dc4e3 commit d807862

1 file changed

Lines changed: 6 additions & 6 deletions

File tree

Modules/main.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1129,14 +1129,16 @@ pymain_run_file(_PyMain *pymain, _PyCoreConfig *config, PyCompilerFlags *cf)
11291129
"%ls: '%ls' is a directory, cannot continue\n",
11301130
config->program, filename);
11311131
pymain->status = 1;
1132-
goto done;
1132+
fclose(fp);
1133+
return;
11331134
}
11341135

11351136
/* call pending calls like signal handlers (SIGINT) */
11361137
if (Py_MakePendingCalls() == -1) {
11371138
PyErr_Print();
11381139
pymain->status = 1;
1139-
goto done;
1140+
fclose(fp);
1141+
return;
11401142
}
11411143

11421144
PyObject *unicode, *bytes = NULL;
@@ -1155,12 +1157,10 @@ pymain_run_file(_PyMain *pymain, _PyCoreConfig *config, PyCompilerFlags *cf)
11551157
filename_str = "<filename encoding error>";
11561158
}
11571159

1158-
int run = PyRun_AnyFileExFlags(fp, filename_str, 0, cf);
1160+
/* PyRun_AnyFileExFlags(closeit=1) calls fclose(fp) before running code */
1161+
int run = PyRun_AnyFileExFlags(fp, filename_str, 1, cf);
11591162
Py_XDECREF(bytes);
11601163
pymain->status = (run != 0);
1161-
1162-
done:
1163-
fclose(fp);
11641164
}
11651165

11661166

0 commit comments

Comments
 (0)