diff --git a/mock.py b/mock.py index 237a4a2747414e2dba0d61cf9e62fcd7d96c389f..9b9a51318553d1eb1b422600a5fd372e7ef0f8a3 100644 --- a/mock.py +++ b/mock.py @@ -2221,11 +2221,8 @@ FunctionAttributes = set([ 'func_name', ]) -if inPy3k: - import _io - file_spec = list(set(dir(_io.TextIOWrapper)).union(set(dir(_io.BytesIO)))) -else: - file_spec = file + +file_spec = None def mock_open(mock=None, read_data=''): @@ -2240,6 +2237,15 @@ def mock_open(mock=None, read_data=''): `read_data` is a string for the `read` method of the file handle to return. This is an empty string by default. """ + global file_spec + if file_spec is None: + # set on first use + if inPy3k: + import _io + file_spec = list(set(dir(_io.TextIOWrapper)).union(set(dir(_io.BytesIO)))) + else: + file_spec = file + if mock is None: mock = MagicMock(name='open', spec=open)