[Python-checkins] bpo-37008: make mock_open handle able to honor next() (GH-13492)

Miss Islington (bot) webhook-mailer at python.org
Thu May 23 06:21:15 EDT 2019


https://github.com/python/cpython/commit/7cc47e9c19b7d67c8f08df15a413d14cf69f45da
commit: 7cc47e9c19b7d67c8f08df15a413d14cf69f45da
branch: 3.7
author: Miss Islington (bot) <31488909+miss-islington at users.noreply.github.com>
committer: GitHub <noreply at github.com>
date: 2019-05-23T03:21:11-07:00
summary:

bpo-37008: make mock_open handle able to honor next() (GH-13492)


I've reported the issue on https://bugs.python.org/issue37008 and now I'm trying to bring a solution to this minor issue.

I think it could be trivially backported to 3.7 branch.

https://bugs.python.org/issue37008
(cherry picked from commit 394119afc6611f17bac96f5ec6fefa00000ae795)

Co-authored-by: Damien Nadé <Anvil at users.noreply.github.com>

files:
A Misc/NEWS.d/next/Library/2019-05-22-15-26-08.bpo-37008.WPbv31.rst
M Lib/unittest/mock.py
M Lib/unittest/test/testmock/testmock.py
M Lib/unittest/test/testmock/testwith.py

diff --git a/Lib/unittest/mock.py b/Lib/unittest/mock.py
index f71f1a6fbed4..1e577dff2f41 100644
--- a/Lib/unittest/mock.py
+++ b/Lib/unittest/mock.py
@@ -2377,6 +2377,11 @@ def _iter_side_effect():
         for line in _state[0]:
             yield line
 
+    def _next_side_effect():
+        if handle.readline.return_value is not None:
+            return handle.readline.return_value
+        return next(_state[0])
+
     global file_spec
     if file_spec is None:
         import _io
@@ -2398,6 +2403,7 @@ def _iter_side_effect():
     handle.readline.side_effect = _state[1]
     handle.readlines.side_effect = _readlines_side_effect
     handle.__iter__.side_effect = _iter_side_effect
+    handle.__next__.side_effect = _next_side_effect
 
     def reset_data(*args, **kwargs):
         _state[0] = _to_stream(read_data)
diff --git a/Lib/unittest/test/testmock/testmock.py b/Lib/unittest/test/testmock/testmock.py
index dab17651e057..76a648e57f12 100644
--- a/Lib/unittest/test/testmock/testmock.py
+++ b/Lib/unittest/test/testmock/testmock.py
@@ -1650,6 +1650,19 @@ def test_mock_open_dunder_iter_issue(self):
         self.assertEqual(lines[1], 'Norwegian Blue')
         self.assertEqual(list(f1), [])
 
+    def test_mock_open_using_next(self):
+        mocked_open = mock.mock_open(read_data='1st line\n2nd line\n3rd line')
+        f1 = mocked_open('a-name')
+        line1 = next(f1)
+        line2 = f1.__next__()
+        lines = [line for line in f1]
+        self.assertEqual(line1, '1st line\n')
+        self.assertEqual(line2, '2nd line\n')
+        self.assertEqual(lines[0], '3rd line')
+        self.assertEqual(list(f1), [])
+        with self.assertRaises(StopIteration):
+            next(f1)
+
     def test_mock_open_write(self):
         # Test exception in file writing write()
         mock_namedtemp = mock.mock_open(mock.MagicMock(name='JLV'))
diff --git a/Lib/unittest/test/testmock/testwith.py b/Lib/unittest/test/testmock/testwith.py
index 0fa42e18eca6..0dda459e8d0d 100644
--- a/Lib/unittest/test/testmock/testwith.py
+++ b/Lib/unittest/test/testmock/testwith.py
@@ -233,7 +233,22 @@ def test_dunder_iter_data(self):
         self.assertEqual(lines[1], 'bar\n')
         self.assertEqual(lines[2], 'baz\n')
         self.assertEqual(h.readline(), '')
+        with self.assertRaises(StopIteration):
+            next(h)
 
+    def test_next_data(self):
+        # Check that next will correctly return the next available
+        # line and plays well with the dunder_iter part.
+        mock = mock_open(read_data='foo\nbar\nbaz\n')
+        with patch('%s.open' % __name__, mock, create=True):
+            h = open('bar')
+            line1 = next(h)
+            line2 = next(h)
+            lines = [l for l in h]
+        self.assertEqual(line1, 'foo\n')
+        self.assertEqual(line2, 'bar\n')
+        self.assertEqual(lines[0], 'baz\n')
+        self.assertEqual(h.readline(), '')
 
     def test_readlines_data(self):
         # Test that emulating a file that ends in a newline character works
diff --git a/Misc/NEWS.d/next/Library/2019-05-22-15-26-08.bpo-37008.WPbv31.rst b/Misc/NEWS.d/next/Library/2019-05-22-15-26-08.bpo-37008.WPbv31.rst
new file mode 100644
index 000000000000..42747aead49a
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2019-05-22-15-26-08.bpo-37008.WPbv31.rst
@@ -0,0 +1,2 @@
+Add support for calling :func:`next` with the mock resulting from
+:func:`unittest.mock.mock_open`



More information about the Python-checkins mailing list