From: Michael Foord Date: Sun, 25 Mar 2012 18:03:13 +0000 (+0100) Subject: unittest.mock: set file_spec on first use X-Git-Tag: v3.3.0a2~61 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=a74561a56d58187ebfdad25104486bbcd08e1de6;p=python unittest.mock: set file_spec on first use --- diff --git a/Lib/unittest/mock.py b/Lib/unittest/mock.py index 9a0bbf0fe3..6ecc4c71f6 100644 --- a/Lib/unittest/mock.py +++ b/Lib/unittest/mock.py @@ -2138,11 +2138,15 @@ FunctionAttributes = set([ 'func_name', ]) -import _io -file_spec = list(set(dir(_io.TextIOWrapper)).union(set(dir(_io.BytesIO)))) +file_spec = None def mock_open(mock=None, read_data=None): + global file_spec + if file_spec is None: + import _io + file_spec = list(set(dir(_io.TextIOWrapper)).union(set(dir(_io.BytesIO)))) + if mock is None: mock = MagicMock(spec=file_spec)