From 0c19acd74f2cfc8c4b351021a490bc32a33af8e1 Mon Sep 17 00:00:00 2001 From: Ivan Malison Date: Thu, 18 Sep 2014 15:25:42 -0700 Subject: [PATCH] Use contextdecorator from contextlib2. add logging for entering context. --- vcr/cassette.py | 17 ++++++++--------- vcr/patch.py | 7 ++++--- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/vcr/cassette.py b/vcr/cassette.py index f03c01b..ddbb0f1 100644 --- a/vcr/cassette.py +++ b/vcr/cassette.py @@ -1,6 +1,7 @@ '''The container for recorded requests and responses''' +import logging + import contextlib2 -import functools try: from collections import Counter except ImportError: @@ -15,7 +16,10 @@ from .matchers import requests_match, uri, method from .errors import UnhandledHTTPRequestError -class CassetteContextDecorator(object): +log = logging.getLogger(__name__) + + +class CassetteContextDecorator(contextlib2.ContextDecorator): """Context manager/decorator that handles installing the cassette and removing cassettes. @@ -38,7 +42,9 @@ class CassetteContextDecorator(object): with contextlib2.ExitStack() as exit_stack: for patcher in PatcherBuilder(cassette).build_patchers(): exit_stack.enter_context(patcher) + log.debug('Entered context for cassette at {0}.'.format(cassette._path)) yield cassette + log.debug('Exiting context for cassette at {0}.'.format(cassette._path)) # TODO(@IvanMalison): Hmmm. it kind of feels like this should be somewhere else. cassette._save() @@ -52,13 +58,6 @@ class CassetteContextDecorator(object): next(self.__finish, None) self.__finish = None - def __call__(self, function): - @functools.wraps(function) - def wrapped(*args, **kwargs): - with self: - return function(*args, **kwargs) - return wrapped - class Cassette(object): '''A container for recorded requests and responses''' diff --git a/vcr/patch.py b/vcr/patch.py index e38cea0..dc4e72e 100644 --- a/vcr/patch.py +++ b/vcr/patch.py @@ -79,11 +79,13 @@ class PatcherBuilder(object): return mock.patch.object(obj, patched_attribute, replacement_class) def _get_cassette_subclass(self, klass): + if klass.cassette is not None: + return klass if klass not in self._class_to_cassette_subclass: - self._class_to_cassette_subclass[klass] = self._cassette_subclass(klass) + self._class_to_cassette_subclass[klass] = self._build_cassette_subclass(klass) return self._class_to_cassette_subclass[klass] - def _cassette_subclass(self, base_class): + def _build_cassette_subclass(self, base_class): bases = (base_class,) if not issubclass(base_class, object): # Check for old style class bases += (object,) @@ -144,7 +146,6 @@ class PatcherBuilder(object): yield cpool, 'CertValidatingHTTPSConnection', VCRCertValidatingHTTPSConnection - def reset_patchers(): yield mock.patch.object(httplib, 'HTTPConnection', _HTTPConnection) yield mock.patch.object(httplib, 'HTTPSConnection', _HTTPSConnection)