int fd = PyObject_AsFileDescriptor(fob);
if (fd == -1)
return NULL;
- /* XXX This will leak a FILE struct. Fix this!!!!
- (But it doesn't leak a file descrioptor!) */
+ if (!_PyVerify_fd(fd))
+ goto error;
+ /* the FILE struct gets a new fd, so that it can be closed
+ * independently of the file descriptor given
+ */
+ fd = dup(fd);
+ if (fd == -1)
+ goto error;
fp = fdopen(fd, mode);
}
- if (fp == NULL)
- PyErr_SetFromErrno(PyExc_IOError);
- return fp;
+ if (fp)
+ return fp;
+error:
+ PyErr_SetFromErrno(PyExc_IOError);
+ return NULL;
}
static PyObject *
return NULL;
}
m = load_compiled_module(name, pathname, fp);
- if (fob == NULL)
- fclose(fp);
+ fclose(fp);
PyMem_Free(pathname);
return m;
}
}
m = _PyImport_LoadDynamicModule(name, pathname, fp);
PyMem_Free(pathname);
+ if (fp)
+ fclose(fp);
return m;
}
return NULL;
}
m = load_source_module(name, pathname, fp);
- if (fob == NULL)
- fclose(fp);
+ fclose(fp);
return m;
}
}
ret = load_module(name, fp, pathname, type, NULL);
PyMem_Free(pathname);
+ if (fp)
+ fclose(fp);
return ret;
}