Skip to content

Commit

Permalink
Make CassetteContextDecorator decorator produce reentrant functions.
Browse files Browse the repository at this point in the history
Closes #150.
  • Loading branch information
colonelpanic8 committed May 10, 2015
1 parent d293020 commit 0bbbc69
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
16 changes: 16 additions & 0 deletions tests/unit/test_cassettes.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,3 +201,19 @@ class Test(object):
assert issubclass(Test.attribute, VCRHTTPSConnection)
assert VCRHTTPSConnection is not Test.attribute
assert Test.attribute is old_attribute


def test_use_cassette_decorated_functions_are_reentrant():
info = {"second": False}
original_conn = httplib.HTTPConnection
@Cassette.use('whatever', inject=True)
def test_function(cassette):
if info['second']:
assert httplib.HTTPConnection is not info['first_conn']
else:
info['first_conn'] = httplib.HTTPConnection
info['second'] = True
test_function()
assert httplib.HTTPConnection is info['first_conn']
test_function()
assert httplib.HTTPConnection is original_conn
14 changes: 13 additions & 1 deletion vcr/cassette.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,14 @@ def _patch_generator(self, cassette):
cassette._save()

def __enter__(self):
# This assertion is here to prevent the dangerous behavior
# that would result from forgetting about a __finish before
# completing it.
# How might this condition be met? Here is an example:
# context_decorator = Cassette.use('whatever')
# with context_decorator:
# with context_decorator:
# pass
assert self.__finish is None, "Cassette already open."
path, kwargs = self._args_getter()
self.__finish = self._patch_generator(self.cls.load(path, **kwargs))
Expand All @@ -61,7 +69,11 @@ def __exit__(self, *args):

@wrapt.decorator
def __call__(self, function, instance, args, kwargs):
with self as cassette:
# This awkward cloning thing is done to ensure that decorated
# functions are reentrant. Reentrancy is required for thread
# safety and the correct operation of recursive functions.
clone = type(self)(self.cls, self._args_getter)
with clone as cassette:
if cassette.inject:
return function(cassette, *args, **kwargs)
else:
Expand Down

2 comments on commit 0bbbc69

@kevin1024
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wow, nice!

@colonelpanic8
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

its a little bit of an awkward fix, but I can't believe it didn't occur to me earlier. when I first noticed this I couldn't really think of an easy way to fix it. I wish there were an easier way to define context managers in terms of each other.

Please sign in to comment.