Mercurial > cpython-lru_cache-weakref
diff Python/import.c @ 42071:825041fc8e2c
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.
| author | Guido van Rossum <guido@python.org> |
|---|---|
| date | Tue, 12 Jun 2007 23:30:11 +0000 |
| parents | 3473a685e5e7 |
| children | e70cdce0e8b2 |
line wrap: on
line diff
--- a/Python/import.c Tue Jun 12 18:07:38 2007 +0000 +++ b/Python/import.c Tue Jun 12 23:30:11 2007 +0000 @@ -2764,19 +2764,21 @@ 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 @@ 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 @@ 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 @@ 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 @@ 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; }
