Rip out the file object's implementation.

Fixed test_import.py while I was at it.

However, there's still a problem in import.c -- get_file() can leak a
FILE struct (not a file descriptor though).  I'm not sure how to fix
this; closing the FILE* closes the file descriptor, and that's the
wrong thing to do when there's still a Python file object keeping the
file descriptor open.  I also would rather not mess with dup(), as it
won't port to Windows.
This commit is contained in:
Guido van Rossum 2007-06-12 23:30:11 +00:00
parent 2d5c219fe0
commit da5b8f2d28
14 changed files with 106 additions and 2537 deletions

View file

@ -2764,19 +2764,21 @@ static FILE *
get_file(char *pathname, PyObject *fob, char *mode)
{
FILE *fp;
if (mode[0] == 'U')
mode = "r" PY_STDIOTEXTMODE;
if (fob == NULL) {
if (mode[0] == 'U')
mode = "r" PY_STDIOTEXTMODE;
fp = fopen(pathname, mode);
if (fp == NULL)
PyErr_SetFromErrno(PyExc_IOError);
}
else {
fp = PyFile_AsFile(fob);
if (fp == NULL)
PyErr_SetString(PyExc_ValueError,
"bad/closed file object");
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!) */
fp = fdopen(fd, mode);
}
if (fp == NULL)
PyErr_SetFromErrno(PyExc_IOError);
return fp;
}
@ -2788,8 +2790,8 @@ imp_load_compiled(PyObject *self, PyObject *args)
PyObject *fob = NULL;
PyObject *m;
FILE *fp;
if (!PyArg_ParseTuple(args, "ss|O!:load_compiled", &name, &pathname,
&PyFile_Type, &fob))
if (!PyArg_ParseTuple(args, "ss|O:load_compiled",
&name, &pathname, &fob))
return NULL;
fp = get_file(pathname, fob, "rb");
if (fp == NULL)
@ -2810,8 +2812,8 @@ imp_load_dynamic(PyObject *self, PyObject *args)
PyObject *fob = NULL;
PyObject *m;
FILE *fp = NULL;
if (!PyArg_ParseTuple(args, "ss|O!:load_dynamic", &name, &pathname,
&PyFile_Type, &fob))
if (!PyArg_ParseTuple(args, "ss|O:load_dynamic",
&name, &pathname, &fob))
return NULL;
if (fob) {
fp = get_file(pathname, fob, "r");
@ -2832,8 +2834,8 @@ imp_load_source(PyObject *self, PyObject *args)
PyObject *fob = NULL;
PyObject *m;
FILE *fp;
if (!PyArg_ParseTuple(args, "ss|O!:load_source", &name, &pathname,
&PyFile_Type, &fob))
if (!PyArg_ParseTuple(args, "ss|O:load_source",
&name, &pathname, &fob))
return NULL;
fp = get_file(pathname, fob, "r");
if (fp == NULL)
@ -2873,12 +2875,7 @@ imp_load_module(PyObject *self, PyObject *args)
if (fob == Py_None)
fp = NULL;
else {
if (!PyFile_Check(fob)) {
PyErr_SetString(PyExc_ValueError,
"load_module arg#2 should be a file or None");
return NULL;
}
fp = get_file(pathname, fob, mode);
fp = get_file(NULL, fob, mode);
if (fp == NULL)
return NULL;
}