diff options
author | Benjamin Peterson <benjamin@python.org> | 2019-11-12 22:51:34 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-11-12 22:51:34 (GMT) |
commit | 74fa9f723f700a342e582b5ad4b51a2c4801cd1c (patch) | |
tree | 0d34e5bbb923f72121243ba0c425a39c08d4d411 /Lib | |
parent | d593881505c1f4acfd17f41312b27cc898451816 (diff) | |
download | cpython-74fa9f723f700a342e582b5ad4b51a2c4801cd1c.zip cpython-74fa9f723f700a342e582b5ad4b51a2c4801cd1c.tar.gz cpython-74fa9f723f700a342e582b5ad4b51a2c4801cd1c.tar.bz2 |
closes bpo-27805: Ignore ESPIPE in initializing seek of append-mode files. (GH-17112)
This change, which follows the behavior of C stdio's fdopen and Python 2's file object, allows pipes to be opened in append mode.
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/_pyio.py | 6 | ||||
-rw-r--r-- | Lib/test/test_io.py | 11 |
2 files changed, 16 insertions, 1 deletions
diff --git a/Lib/_pyio.py b/Lib/_pyio.py index e819b0a..4c24146 100644 --- a/Lib/_pyio.py +++ b/Lib/_pyio.py @@ -1577,7 +1577,11 @@ class FileIO(RawIOBase): # For consistent behaviour, we explicitly seek to the # end of file (otherwise, it might be done only on the # first write()). - os.lseek(fd, 0, SEEK_END) + try: + os.lseek(fd, 0, SEEK_END) + except OSError as e: + if e.errno != errno.ESPIPE: + raise except: if owned_fd is not None: os.close(owned_fd) diff --git a/Lib/test/test_io.py b/Lib/test/test_io.py index ad22dfe..501e931 100644 --- a/Lib/test/test_io.py +++ b/Lib/test/test_io.py @@ -3906,6 +3906,17 @@ class MiscIOTest(unittest.TestCase): self.open(support.TESTFN, mode) self.assertIn('invalid mode', str(cm.exception)) + def test_open_pipe_with_append(self): + # bpo-27805: Ignore ESPIPE from lseek() in open(). + r, w = os.pipe() + self.addCleanup(os.close, r) + f = self.open(w, 'a') + self.addCleanup(f.close) + # Check that the file is marked non-seekable. On Windows, however, lseek + # somehow succeeds on pipes. + if sys.platform != 'win32': + self.assertFalse(f.seekable()) + def test_io_after_close(self): for kwargs in [ {"mode": "w"}, |