]> granicus.if.org Git - python/commitdiff
Minor changes to the unittest.mock.mock_open helper
authorMichael Foord <michael@voidspace.org.uk>
Sun, 25 Mar 2012 18:11:50 +0000 (19:11 +0100)
committerMichael Foord <michael@voidspace.org.uk>
Sun, 25 Mar 2012 18:11:50 +0000 (19:11 +0100)
Lib/unittest/mock.py

index a0b7fb003bc04307a9ed3266e2ded3ca71cba5d8..1c7f33ad65b4da1f51b4ff911d9d140e1622aa7b 100644 (file)
@@ -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