Karthikeyan Singaravelan <[email protected]> added the comment:
Internally mock_open implementation uses line based iteration [0] to keep track
of the state change between read calls. So readline too ignores the argument.
There is issue25690 for an alternate mock_open implementation but the code
change is large and adds a lot of features. A simpler approach would be to use
StringIO or BytesIO to keep track of state changes and they provide read,
readline and readlines API. mock_open docs mentions about using a customized
mock for complex cases but I don't know if worthy enough to make this
enhancement given the internal implementation change to support the API or to
document this behavior.
Looking further there also seems to be a test case for it [1] which will fail
if this is fixed since this returns all characters instead of first 10 like
using open().read(10).
def test_mock_open_read_with_argument(self):
# At one point calling read with an argument was broken
# for mocks returned by mock_open
some_data = 'foo\nbar\nbaz'
mock = mock_open(read_data=some_data)
self.assertEqual(mock().read(10), some_data)
$ echo -n 'foo\nbar\nbaz' > /tmp/a.txt
$ ./python.exe
Python 3.8.0a0 (heads/master:f5107dfd42, Dec 16 2018, 13:41:57)
[Clang 7.0.2 (clang-700.1.81)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> with open('/tmp/a.txt') as f:
... actual = f.read(10)
... actual, len(actual)
...
('foo\nbar\nba', 10)
>>> with open('/tmp/a.txt') as f:
... from unittest.mock import mock_open
... mock = mock_open(read_data=f.read())
... mock_data = mock().read(10)
... mock_data, len(mock_data)
...
('foo\nbar\nbaz', 11)
[0]
https://github.com/python/cpython/blob/f5107dfd42121ef40b13eb678705802f0ff02cf9/Lib/unittest/mock.py#L2349
[1]
https://github.com/python/cpython/blob/f5107dfd42121ef40b13eb678705802f0ff02cf9/Lib/unittest/test/testmock/testwith.py#L284
----------
nosy: +cjw296, mariocj89, xtreak
versions: +Python 3.7, Python 3.8 -Python 2.7, Python 3.6
_______________________________________
Python tracker <[email protected]>
<https://bugs.python.org/issue31855>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe:
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com