[Python-checkins] cpython: Minor changes to the unittest.mock.mock_open helper

michael.foord python-checkins at python.org
Sun Mar 25 20:12:00 CEST 2012


http://hg.python.org/cpython/rev/b0002db98a54
changeset:   75929:b0002db98a54
user:        Michael Foord <michael at voidspace.org.uk>
date:        Sun Mar 25 19:11:50 2012 +0100
summary:
  Minor changes to the unittest.mock.mock_open helper

files:
  Lib/unittest/mock.py |  9 ++++-----
  1 files changed, 4 insertions(+), 5 deletions(-)


diff --git a/Lib/unittest/mock.py b/Lib/unittest/mock.py
--- a/Lib/unittest/mock.py
+++ b/Lib/unittest/mock.py
@@ -2141,7 +2141,8 @@
 
 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 @@
         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

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


More information about the Python-checkins mailing list