diff --git a/Python/pythonrun.c b/Python/pythonrun.c --- a/Python/pythonrun.c +++ b/Python/pythonrun.c @@ -1385,7 +1385,7 @@ { PyObject *m, *d, *v; const char *ext; - int set_file_name = 0, ret; + int set_file_name = 0, ret, closeown = 0; size_t len; m = PyImport_AddModule("__main__"); @@ -1419,6 +1419,7 @@ ret = -1; goto done; } + closeown = 1; /* Turn on optimization if a .pyo file is given */ if (strcmp(ext, ".pyo") == 0) Py_OptimizeFlag = 1; @@ -1449,6 +1450,8 @@ Py_DECREF(v); ret = 0; done: + if (closeown) + fclose(fp); if (set_file_name && PyDict_DelItemString(d, "__file__")) PyErr_Clear(); return ret;