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 /Modules | |
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 'Modules')
-rw-r--r-- | Modules/_cursesmodule.c | 17 | ||||
-rw-r--r-- | Modules/bz2module.c | 12 | ||||
-rw-r--r-- | Modules/cPickle.c | 134 | ||||
-rw-r--r-- | Modules/pyexpat.c | 5 |
4 files changed, 19 insertions, 149 deletions
diff --git a/Modules/_cursesmodule.c b/Modules/_cursesmodule.c index 918e22d..2680320 100644 --- a/Modules/_cursesmodule.c +++ b/Modules/_cursesmodule.c @@ -1287,12 +1287,13 @@ PyCursesWindow_PutWin(PyCursesWindowObject *self, PyObject *args) if (!PyArg_ParseTuple(args, "O;fileobj", &temp)) return NULL; - if (!PyFile_Check(temp)) { - PyErr_SetString(PyExc_TypeError, "argument must be a file object"); - return NULL; - } + PyErr_SetString(PyExc_TypeError, "argument must be a file object"); + return NULL; + +#if 0 return PyCursesCheckERR(putwin(self->win, PyFile_AsFile(temp)), "putwin"); +#endif } static PyObject * @@ -1748,11 +1749,10 @@ PyCurses_GetWin(PyCursesWindowObject *self, PyObject *temp) PyCursesInitialised - if (!PyFile_Check(temp)) { - PyErr_SetString(PyExc_TypeError, "argument must be a file object"); - return NULL; - } + PyErr_SetString(PyExc_TypeError, "argument must be a file object"); + return NULL; +#if 0 win = getwin(PyFile_AsFile(temp)); if (win == NULL) { @@ -1761,6 +1761,7 @@ PyCurses_GetWin(PyCursesWindowObject *self, PyObject *temp) } return PyCursesWindow_New(win); +#endif } static PyObject * diff --git a/Modules/bz2module.c b/Modules/bz2module.c index d4f2743..029232c 100644 --- a/Modules/bz2module.c +++ b/Modules/bz2module.c @@ -1075,6 +1075,7 @@ BZ2File_seek(BZ2FileObject *self, PyObject *args) offset -= self->pos; } else { /* we cannot move back, so rewind the stream */ + FILE *fp = NULL; /* XXX temporary!!! */ BZ2_bzReadClose(&bzerror, self->fp); if (bzerror != BZ_OK) { Util_CatchBZ2Error(bzerror); @@ -1086,7 +1087,7 @@ BZ2File_seek(BZ2FileObject *self, PyObject *args) Py_DECREF(ret); ret = NULL; self->pos = 0; - self->fp = BZ2_bzReadOpen(&bzerror, PyFile_AsFile(self->file), + self->fp = BZ2_bzReadOpen(&bzerror, fp, 0, 0, NULL, 0); if (bzerror != BZ_OK) { Util_CatchBZ2Error(bzerror); @@ -1286,6 +1287,7 @@ BZ2File_init(BZ2FileObject *self, PyObject *args, PyObject *kwargs) { static char *kwlist[] = {"filename", "mode", "buffering", "compresslevel", 0}; + FILE *fp = NULL; /* XXX temporary!!! */ PyObject *name; char *mode = "r"; int buffering = -1; @@ -1347,8 +1349,8 @@ BZ2File_init(BZ2FileObject *self, PyObject *args, PyObject *kwargs) mode = (mode_char == 'r') ? "rb" : "wb"; - self->file = PyObject_CallFunction((PyObject*)&PyFile_Type, "(Osi)", - name, mode, buffering); + self->file = NULL; /* XXX io.open(name, mode, buffering); */ + PyErr_SetString(PyExc_RuntimeError, "can't open bz2 files yet"); if (self->file == NULL) return -1; @@ -1365,11 +1367,11 @@ BZ2File_init(BZ2FileObject *self, PyObject *args, PyObject *kwargs) if (mode_char == 'r') self->fp = BZ2_bzReadOpen(&bzerror, - PyFile_AsFile(self->file), + fp, 0, 0, NULL, 0); else self->fp = BZ2_bzWriteOpen(&bzerror, - PyFile_AsFile(self->file), + fp, compresslevel, 0, 0); if (bzerror != BZ_OK) { diff --git a/Modules/cPickle.c b/Modules/cPickle.c index a4dff7b..f0b3c8a 100644 --- a/Modules/cPickle.c +++ b/Modules/cPickle.c @@ -418,31 +418,6 @@ cPickle_ErrFormat(PyObject *ErrType, char *stringformat, char *format, ...) } static int -write_file(Picklerobject *self, const char *s, Py_ssize_t n) -{ - size_t nbyteswritten; - - if (s == NULL) { - return 0; - } - - if (n > INT_MAX) { - /* String too large */ - return -1; - } - - Py_BEGIN_ALLOW_THREADS - nbyteswritten = fwrite(s, sizeof(char), n, self->fp); - Py_END_ALLOW_THREADS - if (nbyteswritten != (size_t)n) { - PyErr_SetFromErrno(PyExc_IOError); - return -1; - } - - return (int)n; -} - -static int write_cStringIO(Picklerobject *self, const char *s, Py_ssize_t n) { if (s == NULL) { @@ -517,92 +492,6 @@ write_other(Picklerobject *self, const char *s, Py_ssize_t _n) static Py_ssize_t -read_file(Unpicklerobject *self, char **s, Py_ssize_t n) -{ - size_t nbytesread; - - if (self->buf_size == 0) { - int size; - - size = ((n < 32) ? 32 : n); - if (!( self->buf = (char *)malloc(size))) { - PyErr_NoMemory(); - return -1; - } - - self->buf_size = size; - } - else if (n > self->buf_size) { - char *newbuf = (char *)realloc(self->buf, n); - if (!newbuf) { - PyErr_NoMemory(); - return -1; - } - self->buf = newbuf; - self->buf_size = n; - } - - Py_BEGIN_ALLOW_THREADS - nbytesread = fread(self->buf, sizeof(char), n, self->fp); - Py_END_ALLOW_THREADS - if (nbytesread != (size_t)n) { - if (feof(self->fp)) { - PyErr_SetNone(PyExc_EOFError); - return -1; - } - - PyErr_SetFromErrno(PyExc_IOError); - return -1; - } - - *s = self->buf; - - return n; -} - - -static Py_ssize_t -readline_file(Unpicklerobject *self, char **s) -{ - int i; - - if (self->buf_size == 0) { - if (!( self->buf = (char *)malloc(40))) { - PyErr_NoMemory(); - return -1; - } - self->buf_size = 40; - } - - i = 0; - while (1) { - int bigger; - char *newbuf; - for (; i < (self->buf_size - 1); i++) { - if (feof(self->fp) || - (self->buf[i] = getc(self->fp)) == '\n') { - self->buf[i + 1] = '\0'; - *s = self->buf; - return i + 1; - } - } - bigger = self->buf_size << 1; - if (bigger <= 0) { /* overflow */ - PyErr_NoMemory(); - return -1; - } - newbuf = (char *)realloc(self->buf, bigger); - if (!newbuf) { - PyErr_NoMemory(); - return -1; - } - self->buf = newbuf; - self->buf_size = bigger; - } -} - - -static Py_ssize_t read_cStringIO(Unpicklerobject *self, char **s, Py_ssize_t n) { char *ptr; @@ -2665,16 +2554,7 @@ newPicklerobject(PyObject *file, int proto) if (!( self->memo = PyDict_New())) goto err; - if (PyFile_Check(file)) { - self->fp = PyFile_AsFile(file); - if (self->fp == NULL) { - PyErr_SetString(PyExc_ValueError, - "I/O operation on closed file"); - goto err; - } - self->write_func = write_file; - } - else if (PycStringIO_OutputCheck(file)) { + if (PycStringIO_OutputCheck(file)) { self->write_func = write_cStringIO; } else if (file == Py_None) { @@ -4988,17 +4868,7 @@ newUnpicklerobject(PyObject *f) self->file = f; /* Set read, readline based on type of f */ - if (PyFile_Check(f)) { - self->fp = PyFile_AsFile(f); - if (self->fp == NULL) { - PyErr_SetString(PyExc_ValueError, - "I/O operation on closed file"); - goto err; - } - self->read_func = read_file; - self->readline_func = readline_file; - } - else if (PycStringIO_InputCheck(f)) { + if (PycStringIO_InputCheck(f)) { self->fp = NULL; self->read_func = read_cStringIO; self->readline_func = readline_cStringIO; diff --git a/Modules/pyexpat.c b/Modules/pyexpat.c index aebae1c..c583edf 100644 --- a/Modules/pyexpat.c +++ b/Modules/pyexpat.c @@ -956,10 +956,7 @@ xmlparse_ParseFile(xmlparseobject *self, PyObject *f) FILE *fp; PyObject *readmethod = NULL; - if (PyFile_Check(f)) { - fp = PyFile_AsFile(f); - } - else { + { fp = NULL; readmethod = PyObject_GetAttrString(f, "read"); if (readmethod == NULL) { |