Commit c20adf8e authored by Gregory P. Smith's avatar Gregory P. Smith

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*.
parent d918e4e0
......@@ -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);
......
......@@ -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);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment