From 0807cd495f86edb38845a68a845e46e4c15a8bea Mon Sep 17 00:00:00 2001 From: Martin Larralde Date: Tue, 13 Oct 2020 14:29:41 +0200 Subject: [PATCH 1/4] Add test to ensure mode of files opened with `openbin` contain `b` flag --- fs/test.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/fs/test.py b/fs/test.py index 53ed290e..37751731 100644 --- a/fs/test.py +++ b/fs/test.py @@ -774,6 +774,7 @@ def test_openbin_rw(self): with self.fs.openbin("foo/hello", "w") as f: repr(f) + self.assertIn("b", f.mode) self.assertIsInstance(f, io.IOBase) self.assertTrue(f.writable()) self.assertFalse(f.readable()) @@ -787,6 +788,7 @@ def test_openbin_rw(self): # Read it back with self.fs.openbin("foo/hello", "r") as f: + self.assertIn("b", f.mode) self.assertIsInstance(f, io.IOBase) self.assertTrue(f.readable()) self.assertFalse(f.writable()) @@ -927,6 +929,7 @@ def test_openbin(self): with self.fs.openbin("file.bin", "wb") as write_file: repr(write_file) text_type(write_file) + self.assertIn("b", write_file.mode) self.assertIsInstance(write_file, io.IOBase) self.assertTrue(write_file.writable()) self.assertFalse(write_file.readable()) @@ -938,6 +941,7 @@ def test_openbin(self): with self.fs.openbin("file.bin", "rb") as read_file: repr(write_file) text_type(write_file) + self.assertIn("b", read_file.mode) self.assertIsInstance(read_file, io.IOBase) self.assertTrue(read_file.readable()) self.assertFalse(read_file.writable()) From 9616208a64c6a6c7b600b79ec90785ebec5c7097 Mon Sep 17 00:00:00 2001 From: Martin Larralde Date: Tue, 13 Oct 2020 14:36:25 +0200 Subject: [PATCH 2/4] Fix `FTPFS.openbin` not implicitly converting mode to binary mode --- CHANGELOG.md | 2 ++ fs/ftpfs.py | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 562645b3..5292e5db 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -23,6 +23,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/). - Fixed documentation of `Mode.to_platform_bin`. Closes [#382](https://github.com/PyFilesystem/pyfilesystem2/issues/382). - Fixed the code example in the "Testing Filesystems" section of the "Implementing Filesystems" guide. Closes [#407](https://github.com/PyFilesystem/pyfilesystem2/issues/407). +- Fixed `FTPFS.openbin` not implicitly opening files in binary mode like expected + from `openbin`. Closes [#406](https://github.com/PyFilesystem/pyfilesystem2/issues/406). ## [2.4.11] - 2019-09-07 diff --git a/fs/ftpfs.py b/fs/ftpfs.py index 11d2c4cc..6442a1ba 100644 --- a/fs/ftpfs.py +++ b/fs/ftpfs.py @@ -682,7 +682,7 @@ def openbin(self, path, mode="r", buffering=-1, **options): raise errors.FileExpected(path) if _mode.exclusive: raise errors.FileExists(path) - ftp_file = FTPFile(self, _path, mode) + ftp_file = FTPFile(self, _path, _mode.to_platform_bin()) return ftp_file # type: ignore def remove(self, path): From cbcfec9cb1f62e6e99c1f01eebfb2f08a98a44ea Mon Sep 17 00:00:00 2001 From: Martin Larralde Date: Tue, 13 Oct 2020 14:41:47 +0200 Subject: [PATCH 3/4] Add missing `mode` attribute to `_MemoryFile` objects --- CHANGELOG.md | 4 ++++ fs/memoryfs.py | 4 ++++ 2 files changed, 8 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5292e5db..fa072e2e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/). ## [2.4.12] - (Unreleased) +### Added + +- Missing `mode` attribute to `_MemoryFile` objects returned by `MemoryFS.openbin`. + ### Changed - Start testing on PyPy. Due to [#342](https://github.com/PyFilesystem/pyfilesystem2/issues/342) diff --git a/fs/memoryfs.py b/fs/memoryfs.py index 0814b2e0..f328227c 100644 --- a/fs/memoryfs.py +++ b/fs/memoryfs.py @@ -75,6 +75,10 @@ def __str__(self): _template = "" return _template.format(path=self._path, mode=self._mode) + @property + def mode(self): + return self._mode.to_platform_bin() + @contextlib.contextmanager def _seek_lock(self): # type: () -> Iterator[None] From da515eb1eba404bd9d5b440869315414d1295546 Mon Sep 17 00:00:00 2001 From: Martin Larralde Date: Tue, 13 Oct 2020 14:53:50 +0200 Subject: [PATCH 4/4] Add missing type annotation to `_MemoryFile.mode` property --- fs/memoryfs.py | 1 + 1 file changed, 1 insertion(+) diff --git a/fs/memoryfs.py b/fs/memoryfs.py index f328227c..2a011b21 100644 --- a/fs/memoryfs.py +++ b/fs/memoryfs.py @@ -77,6 +77,7 @@ def __str__(self): @property def mode(self): + # type: () -> Text return self._mode.to_platform_bin() @contextlib.contextmanager