Permalink
Browse files

First naive fix for 2.7 context managing bug.

Passes the doctests from the comparison
garybernhardt/python-mock-comparison@08daf71
  • Loading branch information...
1 parent 4ae2449 commit a9ee067a369bc4b180ef32646393716512235626 @CharString CharString committed Sep 9, 2011
Showing with 44 additions and 2 deletions.
  1. +6 −1 dingus.py
  2. +38 −1 tests/test_dingus.py
View
@@ -186,7 +186,7 @@ def returner(return_value):
return Dingus(return_value=return_value)
-class Dingus(object):
+class BaseDingus(object):
def __init__(self, dingus_name=None, full_name=None, **kwargs):
self._parent = None
self.reset()
@@ -361,6 +361,11 @@ def __iter__(self):
__deepcopy__ = None
+class Dingus(BaseDingus):
+ __enter__ = BaseDingus()
+ __exit__ = BaseDingus()
+
+
def exception_raiser(exception):
def raise_exception(*args, **kwargs):
raise exception
View
@@ -4,7 +4,7 @@
from nose.tools import assert_raises
-from dingus import Dingus
+from dingus import Dingus, patch
class WhenCreatingNewDingus:
@@ -429,3 +429,40 @@ def should_be_independent_of_original_dingus(self):
assert copied_dingus.calls('frob').once()
assert not dingus.calls('frob')
+class WhenUsedAsAContextManager:
+ def setup(self):
+ self.dingus = Dingus()
+
+ def should_not_raise_an_exception(self):
+ with self.dingus:
+ pass
+
+ def should_be_able_to_return_something(self):
+ with patch('__builtin__.open', self.dingus):
+ file_ = open.return_value.__enter__.return_value
+ file_.read.return_value = 'some data'
+ with open('foo') as h:
+ assert 'some data' == h.read()
+
+ assert self.dingus.calls('()', 'foo').once()
+
+ #def _raiser(self, exc, dingus=None):
+ # dingus = self.dingus if dingus is None else dingus
+ # def callable():
+ # with dingus:
+ # raise exc
+ # return callable
+
+ #def should_not_consume_exceptions_from_context(self):
+ # assert_raises(KeyError, self._raiser(KeyError))
+
+ #def should_be_able_to_consume_an_arbitrary_exception(self):
+ # dingus = Dingus(__consumes=EOFError)
+ # self._raiser(EOFError, dingus)()
+ # assert_raises(KeyError, self._raiser(KeyError, dingus))
+
+ #def should_be_able_to_consume_multiple_exceptions(self):
+ # dingus = Dingus(__consumes=(NameError, NotImplementedError))
+ # self._raiser(NameError, dingus)()
+ # self._raiser(NotImplementedError, dingus)()
+ # assert_raises(KeyError, self._raiser(KeyError, dingus))

0 comments on commit a9ee067

Please sign in to comment.