Skip to content

Commit

Permalink
Python 2.7.9+ compatibility
Browse files Browse the repository at this point in the history
Merge branch 'py279'
  • Loading branch information
Roguelazer committed Apr 2, 2015
2 parents a6150d6 + c7fb6d0 commit 89b932e
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 17 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ python:
- "2.7"
install:
- "pip install -r requirements.txt -r requirements-dev.txt --use-mirrors"
script: "py.test"
script: "py.test cassette"
54 changes: 40 additions & 14 deletions cassette/http_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"""
import logging
import socket
import ssl
from httplib import HTTPConnection, HTTPSConnection

log = logging.getLogger("cassette")
Expand All @@ -30,19 +31,6 @@ def request(self, method, url, body=None, headers=None):
if self._cassette_name in lib:
self._response = lib[self._cassette_name]

# urllib3 does some additional weirdness to the `sock` attribute
# when the request method is called. Since we skip that method here,
# this is a hack to make it ignore this and not break.
#
# TODO: If we're going to add more adaptors to this module, this
# class shouldn't know anything about its parent classes.
try:
if (isinstance(self, UL3CassetteHTTPConnection) and
hasattr(self, 'sock')):
del self.sock
except NameError:
pass

return

log.warning("Making external HTTP request: %s" % self._cassette_name)
Expand Down Expand Up @@ -76,7 +64,7 @@ def __init__(self, *args, **kwargs):
HTTPConnection.__init__(self, *args, **kwargs)


class CassetteHTTPSConnection(CassetteConnectionMixin, HTTPSConnection):
class CassetteHTTPSConnectionPre279(CassetteConnectionMixin, HTTPSConnection):

_baseclass = HTTPSConnection

Expand All @@ -89,6 +77,44 @@ def __init__(self, host, port=None, key_file=None, cert_file=None,
self.key_file = key_file
self.cert_file = cert_file


class CassetteHTTPSConnectionPost279(CassetteConnectionMixin, HTTPSConnection):

_baseclass = HTTPSConnection

def __init__(self, host, port=None, key_file=None, cert_file=None,
strict=None, timeout=socket._GLOBAL_DEFAULT_TIMEOUT,
source_address=None, context=None):
# Directly taken from httplib.
HTTPConnection.__init__(self, host, port, strict, timeout,
source_address)
self.key_file = key_file
self.cert_file = cert_file
if context is None:
context = ssl._create_default_https_context()
if key_file or cert_file:
context.load_cert_chain(cert_file, key_file)
self._context = context

def connect(self):
"Connect to a host on a given (SSL) port."

HTTPConnection.connect(self)

if self._tunnel_host:
server_hostname = self._tunnel_host
else:
server_hostname = self.host

self.sock = self._context.wrap_socket(self.sock,
server_hostname=server_hostname)


if hasattr(ssl, 'SSLContext'):
CassetteHTTPSConnection = CassetteHTTPSConnectionPost279
else:
CassetteHTTPSConnection = CassetteHTTPSConnectionPre279

try:
from requests.packages import urllib3 as requests_urllib3
except ImportError:
Expand Down
7 changes: 5 additions & 2 deletions cassette/patcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@

unpatched_HTTPConnection = httplib.HTTPConnection
unpatched_HTTPSConnection = httplib.HTTPSConnection
if requests:
unpatched_requests_HTTPConnection = requests.packages.urllib3.connection.HTTPConnection
unpatched_requests_HTTPSConnection = requests.packages.urllib3.connection.HTTPSConnection


def patch(cassette_library):
Expand Down Expand Up @@ -53,6 +56,6 @@ def unpatch():

if requests:
requests.packages.urllib3.connection.HTTPConnection = \
unpatched_HTTPConnection
unpatched_requests_HTTPConnection
requests.packages.urllib3.connection.HTTPSConnection = \
unpatched_HTTPSConnection
unpatched_requests_HTTPSConnection

0 comments on commit 89b932e

Please sign in to comment.