mirror of
https://github.com/kevin1024/vcrpy.git
synced 2025-12-10 09:35:34 +00:00
use conditional requirements for backport libraries Fixes #147
This commit is contained in:
@@ -2,14 +2,11 @@
|
||||
import functools
|
||||
import logging
|
||||
|
||||
import contextlib2
|
||||
|
||||
import wrapt
|
||||
try:
|
||||
from collections import Counter
|
||||
except ImportError:
|
||||
from backport_collections import Counter
|
||||
|
||||
# Internal imports
|
||||
from .compat import contextlib, collections
|
||||
from .errors import UnhandledHTTPRequestError
|
||||
from .matchers import requests_match, uri, method
|
||||
from .patch import CassettePatcherBuilder
|
||||
@@ -43,7 +40,7 @@ class CassetteContextDecorator(object):
|
||||
self.__finish = None
|
||||
|
||||
def _patch_generator(self, cassette):
|
||||
with contextlib2.ExitStack() as exit_stack:
|
||||
with contextlib.ExitStack() as exit_stack:
|
||||
for patcher in CassettePatcherBuilder(cassette).build():
|
||||
exit_stack.enter_context(patcher)
|
||||
log.debug('Entered context for cassette at {0}.'.format(cassette._path))
|
||||
@@ -148,7 +145,7 @@ class Cassette(object):
|
||||
|
||||
# self.data is the list of (req, resp) tuples
|
||||
self.data = []
|
||||
self.play_counts = Counter()
|
||||
self.play_counts = collections.Counter()
|
||||
self.dirty = False
|
||||
self.rewound = False
|
||||
|
||||
|
||||
18
vcr/compat.py
Normal file
18
vcr/compat.py
Normal file
@@ -0,0 +1,18 @@
|
||||
try:
|
||||
from unittest import mock
|
||||
except ImportError:
|
||||
import mock
|
||||
|
||||
try:
|
||||
import contextlib
|
||||
except ImportError:
|
||||
import contextlib2 as contextlib
|
||||
else:
|
||||
if not hasattr(contextlib, 'ExitStack'):
|
||||
import contextlib2 as contextlib
|
||||
|
||||
import collections
|
||||
if not hasattr(collections, 'Counter'):
|
||||
import backport_collections as collections
|
||||
|
||||
__all__ = ['mock', 'contextlib', 'collections']
|
||||
@@ -1,4 +1,3 @@
|
||||
import collections
|
||||
import copy
|
||||
import functools
|
||||
import inspect
|
||||
@@ -6,6 +5,7 @@ import os
|
||||
|
||||
import six
|
||||
|
||||
from .compat import collections
|
||||
from .cassette import Cassette
|
||||
from .serializers import yamlserializer, jsonserializer
|
||||
from .util import compose
|
||||
|
||||
@@ -1,12 +1,10 @@
|
||||
from six import BytesIO, text_type
|
||||
from six.moves.urllib.parse import urlparse, urlencode, urlunparse
|
||||
try:
|
||||
from collections import OrderedDict
|
||||
except ImportError:
|
||||
from backport_collections import OrderedDict
|
||||
import copy
|
||||
import json
|
||||
|
||||
from .compat import collections
|
||||
|
||||
|
||||
def remove_headers(request, headers_to_remove):
|
||||
headers = copy.copy(request.headers)
|
||||
@@ -40,7 +38,7 @@ def remove_post_data_parameters(request, post_data_parameters_to_remove):
|
||||
del json_data[k]
|
||||
request.body = json.dumps(json_data).encode('utf-8')
|
||||
else:
|
||||
post_data = OrderedDict()
|
||||
post_data = collections.OrderedDict()
|
||||
if isinstance(request.body, text_type):
|
||||
request.body = request.body.encode('utf-8')
|
||||
|
||||
|
||||
@@ -2,9 +2,7 @@
|
||||
import functools
|
||||
import itertools
|
||||
|
||||
import contextlib2
|
||||
import mock
|
||||
|
||||
from .compat import contextlib, mock
|
||||
from .stubs import VCRHTTPConnection, VCRHTTPSConnection
|
||||
from six.moves import http_client as httplib
|
||||
|
||||
@@ -323,9 +321,9 @@ def reset_patchers():
|
||||
_CertValidatingHTTPSConnection)
|
||||
|
||||
|
||||
@contextlib2.contextmanager
|
||||
@contextlib.contextmanager
|
||||
def force_reset():
|
||||
with contextlib2.ExitStack() as exit_stack:
|
||||
with contextlib.ExitStack() as exit_stack:
|
||||
for patcher in reset_patchers():
|
||||
exit_stack.enter_context(patcher)
|
||||
yield
|
||||
|
||||
Reference in New Issue
Block a user