Skip to content

Commit fdef371

Browse files
Apply suggestions from code review
Co-authored-by: Eryk Sun <eryksun@gmail.com>
1 parent 462cc40 commit fdef371

1 file changed

Lines changed: 18 additions & 63 deletions

File tree

Python/fileutils.c

Lines changed: 18 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1813,61 +1813,6 @@ _Py_write_impl(int fd, const void *buf, size_t count, int gil_held)
18131813
}
18141814
}
18151815

1816-
// In non-blocking mode, a write that exceeds the available size of a pipe
1817-
// fails without writing anything. Limiting writes to the pipe size allows
1818-
// a buffered write to succeed eventually, as the pipe is read.
1819-
HANDLE hfile = _Py_get_osfhandle(fd);
1820-
if (hfile == INVALID_HANDLE_VALUE) {
1821-
return -1;
1822-
}
1823-
DWORD pipe_mode, pipe_flags, pipe_size, pipe_insize, pipe_outsize;
1824-
if (gil_held) {
1825-
Py_BEGIN_ALLOW_THREADS
1826-
if (GetFileType(hfile) == FILE_TYPE_PIPE &&
1827-
GetNamedPipeHandleStateW(hfile, &pipe_mode, NULL, NULL, NULL,
1828-
NULL, 0) &&
1829-
pipe_mode & PIPE_NOWAIT)
1830-
{
1831-
// GetNamedPipeInfo() requires FILE_READ_ATTRIBUTES access.
1832-
// CreatePipe() includes this access for the write handle.
1833-
if (!GetNamedPipeInfo(hfile, &pipe_flags, &pipe_outsize,
1834-
&pipe_insize, NULL))
1835-
{
1836-
pipe_size = 4096;
1837-
}
1838-
else {
1839-
pipe_size = pipe_flags & PIPE_SERVER_END ? pipe_outsize :
1840-
pipe_insize;
1841-
}
1842-
if (count > pipe_size) {
1843-
count = pipe_size;
1844-
}
1845-
}
1846-
Py_END_ALLOW_THREADS
1847-
}
1848-
else {
1849-
if (GetFileType(hfile) == FILE_TYPE_PIPE &&
1850-
GetNamedPipeHandleStateW(hfile, &pipe_mode, NULL, NULL, NULL,
1851-
NULL, 0) &&
1852-
pipe_mode & PIPE_NOWAIT)
1853-
{
1854-
// GetNamedPipeInfo() requires FILE_READ_ATTRIBUTES access.
1855-
// CreatePipe() includes this access for the write handle.
1856-
if (!GetNamedPipeInfo(hfile, &pipe_flags, &pipe_outsize,
1857-
&pipe_insize, NULL))
1858-
{
1859-
pipe_size = 4096;
1860-
}
1861-
else {
1862-
pipe_size = pipe_flags & PIPE_SERVER_END ? pipe_outsize :
1863-
pipe_insize;
1864-
}
1865-
if (count > pipe_size) {
1866-
count = pipe_size;
1867-
}
1868-
}
1869-
}
1870-
18711816
#endif
18721817
if (count > _PY_WRITE_MAX) {
18731818
count = _PY_WRITE_MAX;
@@ -1878,13 +1823,18 @@ _Py_write_impl(int fd, const void *buf, size_t count, int gil_held)
18781823
Py_BEGIN_ALLOW_THREADS
18791824
errno = 0;
18801825
#ifdef MS_WINDOWS
1881-
_doserrno = 0;
1882-
n = write(fd, buf, (int)count);
18831826
// write() on a non-blocking pipe fails with ENOSPC on Windows if
18841827
// the pipe lacks available space for the entire buffer.
1885-
if (n < 0 && errno == ENOSPC && _doserrno == 0) {
1828+
int c = (int)count;
1829+
do {
1830+
_doserrno = 0;
1831+
n = write(fd, buf, c);
1832+
if (n >= 0 || errno != ENOSPC || _doserrno != 0) {
1833+
break;
1834+
}
18861835
errno = EAGAIN;
1887-
}
1836+
c /= 2;
1837+
} while (c > 0);
18881838
#else
18891839
n = write(fd, buf, count);
18901840
#endif
@@ -1899,13 +1849,18 @@ _Py_write_impl(int fd, const void *buf, size_t count, int gil_held)
18991849
do {
19001850
errno = 0;
19011851
#ifdef MS_WINDOWS
1902-
_doserrno = 0;
1903-
n = write(fd, buf, (int)count);
19041852
// write() on a non-blocking pipe fails with ENOSPC on Windows if
19051853
// the pipe lacks available space for the entire buffer.
1906-
if (n < 0 && errno == ENOSPC && _doserrno == 0) {
1854+
int c = (int)count;
1855+
do {
1856+
_doserrno = 0;
1857+
n = write(fd, buf, c);
1858+
if (n >= 0 || errno != ENOSPC || _doserrno != 0) {
1859+
break;
1860+
}
19071861
errno = EAGAIN;
1908-
}
1862+
c /= 2;
1863+
} while (c > 0);
19091864
#else
19101865
n = write(fd, buf, count);
19111866
#endif

0 commit comments

Comments
 (0)