diff --git a/Doc/c-api/file.rst b/Doc/c-api/file.rst --- a/Doc/c-api/file.rst +++ b/Doc/c-api/file.rst @@ -55,7 +55,7 @@ change in future releases of Python. Create a new :ctype:`PyFileObject` from the already-open standard C file pointer, *fp*. The function *close* will be called when the file should be - closed. Return *NULL* on failure. + closed. Return *NULL* and close the file using :c:func:`close` on failure. .. cfunction:: FILE* PyFile_AsFile(PyObject \*p) diff --git a/Objects/fileobject.c b/Objects/fileobject.c --- a/Objects/fileobject.c +++ b/Objects/fileobject.c @@ -468,19 +468,26 @@ close_the_file(PyFileObject *f) PyObject * PyFile_FromFile(FILE *fp, char *name, char *mode, int (*close)(FILE *)) { - PyFileObject *f = (PyFileObject *)PyFile_Type.tp_new(&PyFile_Type, - NULL, NULL); - if (f != NULL) { - PyObject *o_name = PyString_FromString(name); - if (o_name == NULL) - return NULL; - if (fill_file_fields(f, fp, o_name, mode, close) == NULL) { - Py_DECREF(f); - f = NULL; - } + PyFileObject *f; + PyObject *o_name; + + f = (PyFileObject *)PyFile_Type.tp_new(&PyFile_Type, NULL, NULL); + if (f == NULL) + return NULL; + o_name = PyString_FromString(name); + if (o_name == NULL) { + if (close != NULL) + close(fp); + Py_DECREF(f); + return NULL; + } + if (fill_file_fields(f, fp, o_name, mode, close) == NULL) { + Py_DECREF(f); Py_DECREF(o_name); + return NULL; } - return (PyObject *) f; + Py_DECREF(o_name); + return (PyObject *)f; } PyObject * @@ -489,7 +496,7 @@ PyFile_FromString(char *name, char *mode extern int fclose(FILE *); PyFileObject *f; - f = (PyFileObject *)PyFile_FromFile((FILE *)NULL, name, mode, fclose); + f = (PyFileObject *)PyFile_FromFile((FILE *)NULL, name, mode, NULL); if (f != NULL) { if (open_the_file(f, name, mode) == NULL) { Py_DECREF(f); diff --git a/Python/import.c b/Python/import.c --- a/Python/import.c +++ b/Python/import.c @@ -2843,10 +2843,8 @@ call_find_module(char *name, PyObject *p return NULL; if (fp != NULL) { fob = PyFile_FromFile(fp, pathname, fdp->mode, fclose); - if (fob == NULL) { - fclose(fp); + if (fob == NULL) return NULL; - } } else { fob = Py_None;