def mock_open(mock=None, read_data='<STR_LIT>'):
global file_spec<EOL>if file_spec is None:<EOL><INDENT>if inPy3k:<EOL><INDENT>import _io<EOL>file_spec = list(set(dir(_io.TextIOWrapper)).union(set(dir(_io.BytesIO))))<EOL><DEDENT>else:<EOL><INDENT>file_spec = file<EOL><DEDENT><DEDENT>if mock is None:<EOL><INDENT>mock = MagicMock(name='<STR_LIT>', spec=open)<EOL><DEDENT>handle = MagicMock(spec=file_spec)<EOL>handle.write.return_value = None<EOL>handle.__enter__.return_value = handle<EOL>handle.read.return_value = read_data<EOL>mock.return_value = handle<EOL>return mock<EOL>
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.

The `mock` argument is the mock object to configure. If `None` (the
default) then a `MagicMock` will be created for you, with the API limited
to methods or attributes available on standard file handles.

`read_data` is a string for the `read` method of the file handle to return.
This is an empty string by default.