summaryrefslogtreecommitdiffstats
path: root/Lib/unittest
diff options
context:
space:
mode:
authorMichael Foord <michael@voidspace.org.uk>2012-03-25 18:11:50 (GMT)
committerMichael Foord <michael@voidspace.org.uk>2012-03-25 18:11:50 (GMT)
commit0dccf657b5112ad3fb11250ac8f9a70a89ea0c07 (patch)
tree2eb8382c182bd21f405416225084ce0fc543e40e /Lib/unittest
parent99254730b28100c26575c1bd32459fcb52f5bdb3 (diff)
downloadcpython-0dccf657b5112ad3fb11250ac8f9a70a89ea0c07.zip
cpython-0dccf657b5112ad3fb11250ac8f9a70a89ea0c07.tar.gz
cpython-0dccf657b5112ad3fb11250ac8f9a70a89ea0c07.tar.bz2
Minor changes to the unittest.mock.mock_open helper
Diffstat (limited to 'Lib/unittest')
-rw-r--r--Lib/unittest/mock.py9
1 files changed, 4 insertions, 5 deletions
diff --git a/Lib/unittest/mock.py b/Lib/unittest/mock.py
index a0b7fb0..1c7f33a 100644
--- a/Lib/unittest/mock.py
+++ b/Lib/unittest/mock.py
@@ -2141,7 +2141,8 @@ FunctionAttributes = set([
file_spec = None
-def mock_open(mock=None, read_data=None):
+
+def mock_open(mock=None, read_data=''):
"""
A helper function to create a mock to replace the use of `open`. It works
for `open` called directly or used as a context manager.
@@ -2159,14 +2160,12 @@ def mock_open(mock=None, read_data=None):
file_spec = list(set(dir(_io.TextIOWrapper)).union(set(dir(_io.BytesIO))))
if mock is None:
- mock = MagicMock(spec=file_spec)
+ mock = MagicMock(name='open', spec=open)
handle = MagicMock(spec=file_spec)
handle.write.return_value = None
handle.__enter__.return_value = handle
-
- if read_data is not None:
- handle.read.return_value = read_data
+ handle.read.return_value = read_data
mock.return_value = handle
return mock