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

@@ -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

}

1134113511351136

/* 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

}

1141114311421144

PyObject *unicode, *bytes = NULL;

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

11551157

filename_str = "<filename encoding error>";

11561158

}

115711591158-

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

}

1165116511661166