bpo-34170: Fix pymain_run_file() (GH-8660) · python/cpython@d807862 (original) (raw)

`@@ -1129,14 +1129,16 @@ pymain_run_file(_PyMain *pymain, _PyCoreConfig *config, PyCompilerFlags *cf)

`

1129

1129

`"%ls: '%ls' is a directory, cannot continue\n",

`

1130

1130

`config->program, filename);

`

1131

1131

`pymain->status = 1;

`

1132

``

`-

goto done;

`

``

1132

`+

fclose(fp);

`

``

1133

`+

return;

`

1133

1134

` }

`

1134

1135

``

1135

1136

`/* call pending calls like signal handlers (SIGINT) */

`

1136

1137

`if (Py_MakePendingCalls() == -1) {

`

1137

1138

`PyErr_Print();

`

1138

1139

`pymain->status = 1;

`

1139

``

`-

goto done;

`

``

1140

`+

fclose(fp);

`

``

1141

`+

return;

`

1140

1142

` }

`

1141

1143

``

1142

1144

`PyObject *unicode, *bytes = NULL;

`

`@@ -1155,12 +1157,10 @@ pymain_run_file(_PyMain *pymain, _PyCoreConfig *config, PyCompilerFlags *cf)

`

1155

1157

`filename_str = "";

`

1156

1158

` }

`

1157

1159

``

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);

`

1159

1162

`Py_XDECREF(bytes);

`

1160

1163

`pymain->status = (run != 0);

`

1161

``

-

1162

``

`-

done:

`

1163

``

`-

fclose(fp);

`

1164

1164

`}

`

1165

1165

``

1166

1166

``