Commit eb08a929 authored by Benjamin Peterson's avatar Benjamin Peterson Committed by GitHub

coalesce GILless sections in new_buffersize (#5059)

830daae1 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.
parent dbf52e02
...@@ -1015,10 +1015,10 @@ new_buffersize(PyFileObject *f, size_t currentsize) ...@@ -1015,10 +1015,10 @@ new_buffersize(PyFileObject *f, size_t currentsize)
off_t pos, end; off_t pos, end;
struct stat st; struct stat st;
int res; int res;
size_t bufsize = 0;
Py_BEGIN_ALLOW_THREADS FILE_BEGIN_ALLOW_THREADS(f)
res = fstat(fileno(f->f_fp), &st); res = fstat(fileno(f->f_fp), &st);
Py_END_ALLOW_THREADS
if (res == 0) { if (res == 0) {
end = st.st_size; end = st.st_size;
...@@ -1032,9 +1032,7 @@ new_buffersize(PyFileObject *f, size_t currentsize) ...@@ -1032,9 +1032,7 @@ new_buffersize(PyFileObject *f, size_t currentsize)
need to take the amount of buffered data into account. need to take the amount of buffered data into account.
(Yet another reason why stdio stinks. :-) */ (Yet another reason why stdio stinks. :-) */
Py_BEGIN_ALLOW_THREADS
pos = lseek(fileno(f->f_fp), 0L, SEEK_CUR); pos = lseek(fileno(f->f_fp), 0L, SEEK_CUR);
Py_END_ALLOW_THREADS
if (pos >= 0) { if (pos >= 0) {
pos = ftell(f->f_fp); pos = ftell(f->f_fp);
...@@ -1042,9 +1040,12 @@ new_buffersize(PyFileObject *f, size_t currentsize) ...@@ -1042,9 +1040,12 @@ new_buffersize(PyFileObject *f, size_t currentsize)
if (pos < 0) if (pos < 0)
clearerr(f->f_fp); clearerr(f->f_fp);
if (end > pos && pos >= 0) if (end > pos && pos >= 0)
return currentsize + end - pos + 1; bufsize = currentsize + end - pos + 1;
/* Add 1 so if the file were to grow we'd notice. */ /* Add 1 so if the file were to grow we'd notice. */
} }
FILE_END_ALLOW_THREADS(f)
if (bufsize != 0)
return bufsize;
#endif #endif
/* Expand the buffer by an amount proportional to the current size, /* Expand the buffer by an amount proportional to the current size,
giving us amortized linear-time behavior. Use a less-than-double giving us amortized linear-time behavior. Use a less-than-double
......
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