diff options
author | wuming0 <wumingdemail@163.com> | 2022-06-27 10:00:14 +0800 |
---|---|---|
committer | Sergey Shepelev <temotor@gmail.com> | 2022-06-27 05:00:55 +0300 |
commit | f0a887b94a86f9567e33037646712b89f02ae441 (patch) | |
tree | 85f2d08a81ef0181dccfb722cfa6fdfe9d630888 | |
parent | 88ec603404b2ed25c610dead75d4693c7b3e8072 (diff) | |
download | eventlet-f0a887b94a86f9567e33037646712b89f02ae441.tar.gz |
greenio: GreenPipe/fdopen() with 'a' in mode raised io.UnsupportedOperation: File or stream is not writable
https://github.com/eventlet/eventlet/pull/758
https://github.com/eventlet/eventlet/issues/757
Co-authored-by: Sergey Shepelev <temotor@gmail.com>
-rw-r--r-- | eventlet/greenio/py3.py | 2 | ||||
-rw-r--r-- | tests/__init__.py | 6 | ||||
-rw-r--r-- | tests/greenio_test.py | 39 |
3 files changed, 46 insertions, 1 deletions
diff --git a/eventlet/greenio/py3.py b/eventlet/greenio/py3.py index bc3dc94..fc6c67f 100644 --- a/eventlet/greenio/py3.py +++ b/eventlet/greenio/py3.py @@ -71,7 +71,7 @@ class GreenFileIO(_OriginalIOBase): return 'r' in self._mode or '+' in self._mode def writable(self): - return 'w' in self._mode or '+' in self._mode + return 'w' in self._mode or '+' in self._mode or 'a' in self._mode def fileno(self): return self._fileno diff --git a/tests/__init__.py b/tests/__init__.py index 1883667..4a5204c 100644 --- a/tests/__init__.py +++ b/tests/__init__.py @@ -488,3 +488,9 @@ def dns_tcp_server(ip_to_give, request_count=1): client.close() thread.join() server_socket.close() + + +def read_file(path, mode="rb"): + with open(path, mode) as f: + result = f.read() + return result diff --git a/tests/greenio_test.py b/tests/greenio_test.py index 3d9cf73..b8c5485 100644 --- a/tests/greenio_test.py +++ b/tests/greenio_test.py @@ -1022,3 +1022,42 @@ def test_pipe_context(): with w as f: assert f == w assert r.closed and w.closed + + +def test_greenpipe_write(): + expected = b"initial" + with tempfile.NamedTemporaryFile() as f: + with greenio.GreenPipe(f.name, "wb") as writer: + writer.write(expected) + + actual = tests.read_file(f.name) + assert actual == expected + + +def test_greenpipe_append(): + old_data = b"existing data..." + new_data = b"append with mode=a" + expected = old_data + new_data + with tempfile.NamedTemporaryFile() as f: + with open(f.name, "wb") as fw: + fw.write(old_data) + + with greenio.GreenPipe(f.name, "ab") as writer: + writer.write(new_data) + + actual = tests.read_file(f.name) + assert actual == expected + + +def test_greenpipe_read_overwrite(): + old_data = b"existing data..." + new_data = b"overwrite with mode=r+" + with tempfile.NamedTemporaryFile() as f: + with greenio.GreenPipe(f.name, "wb") as writer: + writer.write(old_data) + + with greenio.GreenPipe(f.name, "r+b") as writer: + writer.write(new_data) + + actual = tests.read_file(f.name) + assert actual == new_data |