8000 Use the new PyFile_IncUseCount & PyFile_DecUseCount calls appropriatly · python/cpython@c20adf8 · GitHub
[go: up one dir, main page]

Skip to content
10000

Commit c20adf8

Browse files
committed
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*.
1 parent d918e4e commit c20adf8

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

Modules/bz2module.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1063,6 +1063,10 @@ BZ2File_seek(BZ2FileObject *self, PyObject *args)
10631063
} else {
10641064
/* we cannot move back, so rewind the stream */
10651065
BZ2_bzReadClose(&bzerror, self->fp);
1066+
if (self->fp) {
1067+
PyFile_DecUseCount(self->file);
1068+
self->fp = NULL;
1069+
}
10661070
if (bzerror != BZ_OK) {
10671071
Util_CatchBZ2Error(bzerror);
10681072
goto cleanup;
@@ -1075,6 +1079,8 @@ BZ2File_seek(BZ2FileObject *self, PyObject *args)
10751079
self->pos = 0;
10761080
self->fp = BZ2_bzReadOpen(&bzerror, PyFile_AsFile(self->file),
10771081
0, 0, NULL, 0);
1082+
if (self->fp)
1083+
PyFile_IncUseCount(self->file);
10781084
if (bzerror != BZ_OK) {
10791085
Util_CatchBZ2Error(bzerror);
10801086
goto cleanup;
@@ -1174,6 +1180,10 @@ BZ2File_close(BZ2FileObject *self)
11741180
0, NULL, NULL);
11751181
break;
11761182
}
1183+
if (self->fp) {
1184+
PyFile_DecUseCount(self->file);
1185+
self->fp = NULL;
1186+
}
11771187
self->mode = MODE_CLOSED;
11781188
ret = PyObject_CallMethod(self->file, "close", NULL);
11791189
if (bzerror != BZ_OK) {
@@ -1376,6 +1386,7 @@ BZ2File_init(BZ2FileObject *self, PyObject *args, PyObject *kwargs)
13761386
Util_CatchBZ2Error(bzerror);
13771387
goto error;
13781388
}
1389+
PyFile_IncUseCount(self->file);
13791390

13801391
self->mode = (mode_char == 'r') ? MODE_READ : MODE_WRITE;
13811392

@@ -1410,6 +1421,10 @@ BZ2File_dealloc(BZ2FileObject *self)
14101421
0, NULL, NULL);
14111422
break;
14121423
}
1424+
if (self->fp) {
1425+
PyFile_DecUseCount(self->file);
1426+
self->fp = NULL;
1427+
}
14131428
Util_DropReadAhead(self);
14141429
Py_XDECREF(self->file);
14151430
Py_TYPE(self)->tp_free((PyObject *)self);

Modules/cPickle.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -431,9 +431,11 @@ write_file(Picklerobject *self, const char *s, Py_ssize_t n)
431431
return -1;
432432
}
433433

434+
PyFile_IncUseCount((PyFileObject *)self->file);
434435
Py_BEGIN_ALLOW_THREADS
435436
nbyteswritten = fwrite(s, sizeof(char), n, self->fp);
436437
Py_END_ALLOW_THREADS
438+
PyFile_DecUseCount((PyFileObject *)self->file);
437439
if (nbyteswritten != (size_t)n) {
438440
PyErr_SetFromErrno(PyExc_IOError);
439441
return -1;
@@ -542,9 +544,11 @@ read_file(Unpicklerobject *self, char **s, Py_ssize_t n)
542544
self->buf_size = n;
543545
}
544546

547+
PyFile_IncUseCount((PyFileObject *)self->file);
545548
Py_BEGIN_ALLOW_THREADS
546549
nbytesread = fread(self->buf, sizeof(char), n, self->fp);
547550
Py_END_ALLOW_THREADS
551+
PyFile_DecUseCount((PyFileObject *)self->file);
548552
if (nbytesread != (size_t)n) {
549553
if (feof(self->fp)) {
550554
PyErr_SetNone(PyExc_EOFError);

0 commit comments

Comments
 (0)
0