diff options
author | Victor Stinner <victor.stinner@haypocalc.com> | 2011-09-23 19:37:03 +0200 |
---|---|---|
committer | Victor Stinner <victor.stinner@haypocalc.com> | 2011-09-23 19:37:03 +0200 |
commit | 63c22fac727a72f0a4da8f72b12cd5f9b480376b (patch) | |
tree | 8c3da37f17c59c36e9204e527c1ceea2a48695f7 /Objects/fileobject.c | |
parent | 51b719814e0c32fd0eea570d3769d323bea97cab (diff) | |
download | cpython-git-63c22fac727a72f0a4da8f72b12cd5f9b480376b.tar.gz |
Issue #7732: Fix a crash on importing a module if a directory has the same name
than a Python module (e.g. "__init__.py"): don't close the file twice.
PyFile_FromFile() does also close the file if PyString_FromString() failed. It
did already close the file on fill_file_fields() error (e.g. if the file is a
directory).
Diffstat (limited to 'Objects/fileobject.c')
-rw-r--r-- | Objects/fileobject.c | 32 |
1 files changed, 19 insertions, 13 deletions
diff --git a/Objects/fileobject.c b/Objects/fileobject.c index 974d6424e4..edd839e8b0 100644 --- a/Objects/fileobject.c +++ b/Objects/fileobject.c @@ -468,28 +468,34 @@ 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 && fp != 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 * 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); |