MCPcopy Index your code
hub / github.com/python/cpython / mock_open

Function mock_open

Lib/unittest/mock.py:2971–3054  ·  view source on GitHub ↗

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 me

(mock=None, read_data='')

Source from the content-addressed store, hash-verified

2969
2970
2971def mock_open(mock=None, read_data=''):
2972 """
2973 A helper function to create a mock to replace the use of `open`. It works
2974 for `open` called directly or used as a context manager.
2975
2976 The `mock` argument is the mock object to configure. If `None` (the
2977 default) then a `MagicMock` will be created for you, with the API limited
2978 to methods or attributes available on standard file handles.
2979
2980 `read_data` is a string for the `read`, `readline` and `readlines` of the
2981 file handle to return. This is an empty string by default.
2982 """
2983 _read_data = _to_stream(read_data)
2984 _state = [_read_data, None]
2985
2986 def _readlines_side_effect(*args, **kwargs):
2987 if handle.readlines.return_value is not None:
2988 return handle.readlines.return_value
2989 return _state[0].readlines(*args, **kwargs)
2990
2991 def _read_side_effect(*args, **kwargs):
2992 if handle.read.return_value is not None:
2993 return handle.read.return_value
2994 return _state[0].read(*args, **kwargs)
2995
2996 def _readline_side_effect(*args, **kwargs):
2997 yield from _iter_side_effect()
2998 while True:
2999 yield _state[0].readline(*args, **kwargs)
3000
3001 def _iter_side_effect():
3002 if handle.readline.return_value is not None:
3003 while True:
3004 yield handle.readline.return_value
3005 for line in _state[0]:
3006 yield line
3007
3008 def _next_side_effect():
3009 if handle.readline.return_value is not None:
3010 return handle.readline.return_value
3011 return next(_state[0])
3012
3013 def _exit_side_effect(exctype, excinst, exctb):
3014 handle.close()
3015
3016 global file_spec
3017 if file_spec is None:
3018 import _io
3019 file_spec = list(set(dir(_io.TextIOWrapper)).union(set(dir(_io.BytesIO))))
3020
3021 global open_spec
3022 if open_spec is None:
3023 import _io
3024 open_spec = list(set(dir(_io.open)))
3025 if mock is None:
3026 mock = MagicMock(name='open', spec=open_spec)
3027
3028 handle = MagicMock(spec=file_spec)

Callers 15

test_mock_openMethod · 0.90
test_explicit_mockMethod · 0.90
test_read_dataMethod · 0.90
test_readline_dataMethod · 0.90
test_dunder_iter_dataMethod · 0.90
test_next_dataMethod · 0.90
test_readlines_dataMethod · 0.90
test_read_bytesMethod · 0.90
test_readline_bytesMethod · 0.90
test_readlines_bytesMethod · 0.90

Calls 6

_to_streamFunction · 0.85
listClass · 0.85
setFunction · 0.85
MagicMockClass · 0.85
_readline_side_effectFunction · 0.85
unionMethod · 0.80

Tested by 15

test_mock_openMethod · 0.72
test_explicit_mockMethod · 0.72
test_read_dataMethod · 0.72
test_readline_dataMethod · 0.72
test_dunder_iter_dataMethod · 0.72
test_next_dataMethod · 0.72
test_readlines_dataMethod · 0.72
test_read_bytesMethod · 0.72
test_readline_bytesMethod · 0.72
test_readlines_bytesMethod · 0.72

Used in the wild real call sites across dependent graphs

searching dependent graphs…