From 86e4ace490de0d5fe9d24b9719ae402b59e41941 Mon Sep 17 00:00:00 2001 From: Michael Foord <michael@voidspace.org.uk> Date: Sun, 18 Mar 2012 18:56:36 +0000 Subject: [PATCH] Set file_spec on first use --- mock.py | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/mock.py b/mock.py index 237a4a2..9b9a513 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) -- GitLab