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*.
diff --git a/Modules/bz2module.c b/Modules/bz2module.c
index fc14a06..6e5ed14 100644
--- a/Modules/bz2module.c
+++ b/Modules/bz2module.c
@@ -1063,6 +1063,10 @@
} 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 @@
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 @@
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 @@
Util_CatchBZ2Error(bzerror);
goto error;
}
+ PyFile_IncUseCount(self->file);
self->mode = (mode_char == 'r') ? MODE_READ : MODE_WRITE;
@@ -1410,6 +1421,10 @@
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);