Skip to content

Commit eb08a92

Browse files
authored
coalesce GILless sections in new_buffersize (#5059)
830daae added some new GIL-releasing to new_buffersize. This is fine, but it's better to avoid reacquiring the GIL for as long as possible. Also, it should use FILE_(BEGIN|END)_ALLOW_THREADS to avoid having the file closed from under it.
1 parent dbf52e0 commit eb08a92

File tree

1 file changed

+6
-5
lines changed

1 file changed

+6
-5
lines changed

Objects/fileobject.c

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1015,10 +1015,10 @@ new_buffersize(PyFileObject *f, size_t currentsize)
10151015
off_t pos, end;
10161016
struct stat st;
10171017
int res;
1018+
size_t bufsize = 0;
10181019

1019-
Py_BEGIN_ALLOW_THREADS
1020+
FILE_BEGIN_ALLOW_THREADS(f)
10201021
res = fstat(fileno(f->f_fp), &st);
1021-
Py_END_ALLOW_THREADS
10221022

10231023
if (res == 0) {
10241024
end = st.st_size;
@@ -1032,19 +1032,20 @@ new_buffersize(PyFileObject *f, size_t currentsize)
10321032
need to take the amount of buffered data into account.
10331033
(Yet another reason why stdio stinks. :-) */
10341034

1035-
Py_BEGIN_ALLOW_THREADS
10361035
pos = lseek(fileno(f->f_fp), 0L, SEEK_CUR);
1037-
Py_END_ALLOW_THREADS
10381036

10391037
if (pos >= 0) {
10401038
pos = ftell(f->f_fp);
10411039
}
10421040
if (pos < 0)
10431041
clearerr(f->f_fp);
10441042
if (end > pos && pos >= 0)
1045-
return currentsize + end - pos + 1;
1043+
bufsize = currentsize + end - pos + 1;
10461044
/* Add 1 so if the file were to grow we'd notice. */
10471045
}
1046+
FILE_END_ALLOW_THREADS(f)
1047+
if (bufsize != 0)
1048+
return bufsize;
10481049
#endif
10491050
/* Expand the buffer by an amount proportional to the current size,
10501051
giving us amortized linear-time behavior. Use a less-than-double

0 commit comments

Comments
 (0)