]> granicus.if.org Git - python/commitdiff
Use the new PyFile_IncUseCount & PyFile_DecUseCount calls appropriatly
authorGregory P. Smith <greg@mad-scientist.com>
Mon, 7 Apr 2008 06:33:21 +0000 (06:33 +0000)
committerGregory P. Smith <greg@mad-scientist.com>
Mon, 7 Apr 2008 06:33:21 +0000 (06:33 +0000)
within the standard library.  These modules use PyFile_AsFile and later
release the GIL while operating on the previously returned FILE*.

Modules/bz2module.c
Modules/cPickle.c

index fc14a06cb8c4290c35eb8949f7aafa890ba63357..6e5ed1448e8c2615e74ccf99cf278d9f7fdd4b4a 100644 (file)
@@ -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);
index 2a05c0645e4e57057b382c36ad5ec274ce2906f9..b9b95c8421b9777553dac13f16de6c0831d3219f 100644 (file)
@@ -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);