diff options
author | Michael Foord <michael@voidspace.org.uk> | 2012-03-13 16:38:50 -0700 |
---|---|---|
committer | Michael Foord <michael@voidspace.org.uk> | 2012-03-13 16:38:50 -0700 |
commit | f5237198bb48a21e1bec104f6575ad1d605d9844 (patch) | |
tree | de4f200c5d309b12fbc8129db7847d5f2b83c41c /mock.py | |
parent | 7b9de7e6e4271a013e0d7f1f8eba384ba87fd933 (diff) | |
download | mock-f5237198bb48a21e1bec104f6575ad1d605d9844.tar.gz |
Adding mock_open helper
Diffstat (limited to 'mock.py')
-rw-r--r-- | mock.py | 39 |
1 files changed, 37 insertions, 2 deletions
@@ -25,6 +25,7 @@ __all__ = ( 'FILTER_DIR', 'NonCallableMock', 'NonCallableMagicMock', + 'mock_open', ) @@ -335,6 +336,8 @@ class _Sentinel(object): sentinel = _Sentinel() DEFAULT = sentinel.DEFAULT +_missing = sentinel.MISSING +_deleted = sentinel.DELETED class OldStyleClass: @@ -630,7 +633,9 @@ class NonCallableMock(Base): raise AttributeError(name) result = self._mock_children.get(name) - if result is None: + if result is _deleted: + raise AttributeError(name) + elif result is None: wraps = None if self._mock_wraps is not None: # XXXX should we get the attribute without triggering code @@ -757,7 +762,16 @@ class NonCallableMock(Base): # not set on the instance itself return - return object.__delattr__(self, name) + if name in self.__dict__: + object.__delattr__(self, name) + + obj = self._mock_children.get(name, _missing) + if obj is _deleted: + raise AttributeError(name) + if obj is not _missing: + del self._mock_children[name] + self._mock_children[name] = _deleted + def _format_mock_call_signature(self, args, kwargs): @@ -2201,3 +2215,24 @@ FunctionAttributes = set([ 'func_globals', 'func_name', ]) + +if inPy3k: + import _io + file_spec = list(set(dir(_io.TextIOWrapper)).union(set(dir(_io.BytesIO)))) +else: + file_spec = file + + +def mock_open(mock=None, read_data=None): + if mock is None: + mock = MagicMock(spec=file_spec) + + handle = MagicMock(spec=file_spec) + handle.write.return_value = None + handle.__enter__.return_value = handle + + if read_data is not None: + handle.read.return_value = read_data + + mock.return_value = handle + return mock |