diff options
author | Guido van Rossum <guido@python.org> | 2007-06-12 23:30:11 (GMT) |
---|---|---|
committer | Guido van Rossum <guido@python.org> | 2007-06-12 23:30:11 (GMT) |
commit | da5b8f2d28f2f7ce47be5d88244eaefc66f7de3e (patch) | |
tree | f3b0ab1f90be8ba18b1cefdb660cebd95c0f70d9 /Python/import.c | |
parent | 2d5c219fe09eacf81c139e5af9114fbbdd093d85 (diff) | |
download | cpython-da5b8f2d28f2f7ce47be5d88244eaefc66f7de3e.zip cpython-da5b8f2d28f2f7ce47be5d88244eaefc66f7de3e.tar.gz cpython-da5b8f2d28f2f7ce47be5d88244eaefc66f7de3e.tar.bz2 |
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.
Diffstat (limited to 'Python/import.c')
-rw-r--r-- | Python/import.c | 37 |
1 files changed, 17 insertions, 20 deletions
diff --git a/Python/import.c b/Python/import.c index 4786f1b..5680abc 100644 --- a/Python/import.c +++ b/Python/import.c @@ -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; } |