@@ -1750,7 +1750,15 @@ _Py_read(int fd, void *buf, size_t count)
1750
1750
Py_BEGIN_ALLOW_THREADS
1751
1751
errno = 0 ;
1752
1752
#ifdef MS_WINDOWS
1753
+ _doserrno = 0 ;
1753
1754
n = read (fd , buf , (int )count );
1755
+ // read() on a non-blocking empty pipe fails with EINVAL, which is
1756
+ // mapped from the Windows error code ERROR_NO_DATA.
1757
+ if (n < 0 && errno == EINVAL ) {
1758
+ if (_doserrno == ERROR_NO_DATA ) {
1759
+ errno = EAGAIN ;
1760
+ }
1761
+ }
1754
1762
#else
1755
1763
n = read (fd , buf , count );
1756
1764
#endif
@@ -1814,7 +1822,13 @@ _Py_write_impl(int fd, const void *buf, size_t count, int gil_held)
1814
1822
Py_BEGIN_ALLOW_THREADS
1815
1823
errno = 0 ;
1816
1824
#ifdef MS_WINDOWS
1825
+ _doserrno = 0 ;
1817
1826
n = write (fd , buf , (int )count );
1827
+ // write() on a non-blocking pipe fails with ENOSPC on Windows if
1828
+ // the pipe lacks available space for the entire buffer.
1829
+ if (n < 0 && errno == ENOSPC && _doserrno == 0 ) {
1830
+ errno = EAGAIN ;
1831
+ }
1818
1832
#else
1819
1833
n = write (fd , buf , count );
1820
1834
#endif
@@ -1829,7 +1843,13 @@ _Py_write_impl(int fd, const void *buf, size_t count, int gil_held)
1829
1843
do {
1830
1844
errno = 0 ;
1831
1845
#ifdef MS_WINDOWS
1846
+ _doserrno = 0 ;
1832
1847
n = write (fd , buf , (int )count );
1848
+ // write() on a non-blocking pipe fails with ENOSPC on Windows if
1849
+ // the pipe lacks available space for the entire buffer.
1850
+ if (n < 0 && errno == ENOSPC && _doserrno == 0 ) {
1851
+ errno = EAGAIN ;
1852
+ }
1833
1853
#else
1834
1854
n = write (fd , buf , count );
1835
1855
#endif
0 commit comments