[Python-checkins] cpython (3.4): Issue #23004: mock_open() now reads binary data correctly when the type of

berker.peksag python-checkins at python.org
Thu Aug 6 12:17:39 CEST 2015


https://hg.python.org/cpython/rev/3d7adf5b3fb3
changeset:   97305:3d7adf5b3fb3
branch:      3.4
parent:      97302:ab75b7ff4dfe
user:        Berker Peksag <berker.peksag at gmail.com>
date:        Thu Aug 06 13:15:51 2015 +0300
summary:
  Issue #23004: mock_open() now reads binary data correctly when the type of read_data is bytes.

Initial patch by Aaron Hill.

files:
  Lib/unittest/mock.py                   |   9 ++--
  Lib/unittest/test/testmock/testwith.py |  28 ++++++++++++++
  Misc/NEWS                              |   3 +
  3 files changed, 36 insertions(+), 4 deletions(-)


diff --git a/Lib/unittest/mock.py b/Lib/unittest/mock.py
--- a/Lib/unittest/mock.py
+++ b/Lib/unittest/mock.py
@@ -2250,9 +2250,10 @@
     # Helper for mock_open:
     # Retrieve lines from read_data via a generator so that separate calls to
     # readline, read, and readlines are properly interleaved
-    data_as_list = ['{}\n'.format(l) for l in read_data.split('\n')]
-
-    if data_as_list[-1] == '\n':
+    sep = b'\n' if isinstance(read_data, bytes) else '\n'
+    data_as_list = [l + sep for l in read_data.split(sep)]
+
+    if data_as_list[-1] == sep:
         # If the last line ended in a newline, the list comprehension will have an
         # extra entry that's just a newline.  Remove this.
         data_as_list = data_as_list[:-1]
@@ -2286,7 +2287,7 @@
     def _read_side_effect(*args, **kwargs):
         if handle.read.return_value is not None:
             return handle.read.return_value
-        return ''.join(_state[0])
+        return type(read_data)().join(_state[0])
 
     def _readline_side_effect():
         if handle.readline.return_value is not None:
diff --git a/Lib/unittest/test/testmock/testwith.py b/Lib/unittest/test/testmock/testwith.py
--- a/Lib/unittest/test/testmock/testwith.py
+++ b/Lib/unittest/test/testmock/testwith.py
@@ -224,6 +224,34 @@
         self.assertEqual(result, ['foo\n', 'bar\n', 'baz'])
 
 
+    def test_read_bytes(self):
+        mock = mock_open(read_data=b'\xc6')
+        with patch('%s.open' % __name__, mock, create=True):
+            with open('abc', 'rb') as f:
+                result = f.read()
+        self.assertEqual(result, b'\xc6')
+
+
+    def test_readline_bytes(self):
+        m = mock_open(read_data=b'abc\ndef\nghi\n')
+        with patch('%s.open' % __name__, m, create=True):
+            with open('abc', 'rb') as f:
+                line1 = f.readline()
+                line2 = f.readline()
+                line3 = f.readline()
+        self.assertEqual(line1, b'abc\n')
+        self.assertEqual(line2, b'def\n')
+        self.assertEqual(line3, b'ghi\n')
+
+
+    def test_readlines_bytes(self):
+        m = mock_open(read_data=b'abc\ndef\nghi\n')
+        with patch('%s.open' % __name__, m, create=True):
+            with open('abc', 'rb') as f:
+                result = f.readlines()
+        self.assertEqual(result, [b'abc\n', b'def\n', b'ghi\n'])
+
+
     def test_mock_open_read_with_argument(self):
         # At one point calling read with an argument was broken
         # for mocks returned by mock_open
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -71,6 +71,9 @@
 
 - Issue #23888: Handle fractional time in cookie expiry. Patch by ssh.
 
+- Issue #23004: mock_open() now reads binary data correctly when the type of
+  read_data is bytes.  Initial patch by Aaron Hill.
+
 - Issue #23652: Make it possible to compile the select module against the
   libc headers from the Linux Standard Base, which do not include some
   EPOLL macros.  Patch by Matt Frank.

-- 
Repository URL: https://hg.python.org/cpython


More information about the Python-checkins mailing list