From c20adf8ef2d9ec938069d08fdcf66cff1d4a6905 Mon Sep 17 00:00:00 2001 From: "Gregory P. Smith" Date: Mon, 7 Apr 2008 06:33:21 +0000 Subject: [PATCH] Use the new PyFile_IncUseCount & PyFile_DecUseCount calls appropriatly within the standard library. These modules use PyFile_AsFile and later release the GIL while operating on the previously returned FILE*. --- Modules/bz2module.c | 15 +++++++++++++++ Modules/cPickle.c | 4 ++++ 2 files changed, 19 insertions(+) diff --git a/Modules/bz2module.c b/Modules/bz2module.c index fc14a06cb8..6e5ed1448e 100644 --- a/Modules/bz2module.c +++ b/Modules/bz2module.c @@ -1063,6 +1063,10 @@ BZ2File_seek(BZ2FileObject *self, PyObject *args) } else { /* we cannot move back, so rewind the stream */ BZ2_bzReadClose(&bzerror, self->fp); + if (self->fp) { + PyFile_DecUseCount(self->file); + self->fp = NULL; + } if (bzerror != BZ_OK) { Util_CatchBZ2Error(bzerror); goto cleanup; @@ -1075,6 +1079,8 @@ BZ2File_seek(BZ2FileObject *self, PyObject *args) self->pos = 0; self->fp = BZ2_bzReadOpen(&bzerror, PyFile_AsFile(self->file), 0, 0, NULL, 0); + if (self->fp) + PyFile_IncUseCount(self->file); if (bzerror != BZ_OK) { Util_CatchBZ2Error(bzerror); goto cleanup; @@ -1174,6 +1180,10 @@ BZ2File_close(BZ2FileObject *self) 0, NULL, NULL); break; } + if (self->fp) { + PyFile_DecUseCount(self->file); + self->fp = NULL; + } self->mode = MODE_CLOSED; ret = PyObject_CallMethod(self->file, "close", NULL); if (bzerror != BZ_OK) { @@ -1376,6 +1386,7 @@ BZ2File_init(BZ2FileObject *self, PyObject *args, PyObject *kwargs) Util_CatchBZ2Error(bzerror); goto error; } + PyFile_IncUseCount(self->file); self->mode = (mode_char == 'r') ? MODE_READ : MODE_WRITE; @@ -1410,6 +1421,10 @@ BZ2File_dealloc(BZ2FileObject *self) 0, NULL, NULL); break; } + if (self->fp) { + PyFile_DecUseCount(self->file); + self->fp = NULL; + } Util_DropReadAhead(self); Py_XDECREF(self->file); Py_TYPE(self)->tp_free((PyObject *)self); diff --git a/Modules/cPickle.c b/Modules/cPickle.c index 2a05c0645e..b9b95c8421 100644 --- a/Modules/cPickle.c +++ b/Modules/cPickle.c @@ -431,9 +431,11 @@ write_file(Picklerobject *self, const char *s, Py_ssize_t n) return -1; } + PyFile_IncUseCount((PyFileObject *)self->file); Py_BEGIN_ALLOW_THREADS nbyteswritten = fwrite(s, sizeof(char), n, self->fp); Py_END_ALLOW_THREADS + PyFile_DecUseCount((PyFileObject *)self->file); if (nbyteswritten != (size_t)n) { PyErr_SetFromErrno(PyExc_IOError); return -1; @@ -542,9 +544,11 @@ read_file(Unpicklerobject *self, char **s, Py_ssize_t n) self->buf_size = n; } + PyFile_IncUseCount((PyFileObject *)self->file); Py_BEGIN_ALLOW_THREADS nbytesread = fread(self->buf, sizeof(char), n, self->fp); Py_END_ALLOW_THREADS + PyFile_DecUseCount((PyFileObject *)self->file); if (nbytesread != (size_t)n) { if (feof(self->fp)) { PyErr_SetNone(PyExc_EOFError); -- 2.40.0