Skip to content

Commit 526438d

Browse files
authored
Require FS.openbin to return a file-handle in binary mode (#435)
* Add test to ensure mode of files opened with `openbin` contain `b` flag * Fix `FTPFS.openbin` not implicitly converting mode to binary mode * Add missing `mode` attribute to `_MemoryFile` objects * Add missing type annotation to `_MemoryFile.mode` property
1 parent 9a8f94a commit 526438d

File tree

4 files changed

+16
-1
lines changed

4 files changed

+16
-1
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
77

88
## [2.4.12] - (Unreleased)
99

10+
### Added
11+
12+
- Missing `mode` attribute to `_MemoryFile` objects returned by `MemoryFS.openbin`.
13+
1014
### Changed
1115

1216
- Start testing on PyPy. Due to [#342](https://github.com/PyFilesystem/pyfilesystem2/issues/342)
@@ -23,6 +27,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
2327
- Fixed documentation of `Mode.to_platform_bin`. Closes [#382](https://github.com/PyFilesystem/pyfilesystem2/issues/382).
2428
- Fixed the code example in the "Testing Filesystems" section of the
2529
"Implementing Filesystems" guide. Closes [#407](https://github.com/PyFilesystem/pyfilesystem2/issues/407).
30+
- Fixed `FTPFS.openbin` not implicitly opening files in binary mode like expected
31+
from `openbin`. Closes [#406](https://github.com/PyFilesystem/pyfilesystem2/issues/406).
2632

2733
## [2.4.11] - 2019-09-07
2834

fs/ftpfs.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -682,7 +682,7 @@ def openbin(self, path, mode="r", buffering=-1, **options):
682682
raise errors.FileExpected(path)
683683
if _mode.exclusive:
684684
raise errors.FileExists(path)
685-
ftp_file = FTPFile(self, _path, mode)
685+
ftp_file = FTPFile(self, _path, _mode.to_platform_bin())
686686
return ftp_file # type: ignore
687687

688688
def remove(self, path):

fs/memoryfs.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,11 @@ def __str__(self):
7575
_template = "<memoryfile '{path}' '{mode}'>"
7676
return _template.format(path=self._path, mode=self._mode)
7777

78+
@property
79+
def mode(self):
80+
# type: () -> Text
81+
return self._mode.to_platform_bin()
82+
7883
@contextlib.contextmanager
7984
def _seek_lock(self):
8085
# type: () -> Iterator[None]

fs/test.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -774,6 +774,7 @@ def test_openbin_rw(self):
774774

775775
with self.fs.openbin("foo/hello", "w") as f:
776776
repr(f)
777+
self.assertIn("b", f.mode)
777778
self.assertIsInstance(f, io.IOBase)
778779
self.assertTrue(f.writable())
779780
self.assertFalse(f.readable())
@@ -787,6 +788,7 @@ def test_openbin_rw(self):
787788

788789
# Read it back
789790
with self.fs.openbin("foo/hello", "r") as f:
791+
self.assertIn("b", f.mode)
790792
self.assertIsInstance(f, io.IOBase)
791793
self.assertTrue(f.readable())
792794
self.assertFalse(f.writable())
@@ -927,6 +929,7 @@ def test_openbin(self):
927929
with self.fs.openbin("file.bin", "wb") as write_file:
928930
repr(write_file)
929931
text_type(write_file)
932+
self.assertIn("b", write_file.mode)
930933
self.assertIsInstance(write_file, io.IOBase)
931934
self.assertTrue(write_file.writable())
932935
self.assertFalse(write_file.readable())
@@ -938,6 +941,7 @@ def test_openbin(self):
938941
with self.fs.openbin("file.bin", "rb") as read_file:
939942
repr(write_file)
940943
text_type(write_file)
944+
self.assertIn("b", read_file.mode)
941945
self.assertIsInstance(read_file, io.IOBase)
942946
self.assertTrue(read_file.readable())
943947
self.assertFalse(read_file.writable())

0 commit comments

Comments
 (0)