Skip to content

Commit

Permalink
Do not fail on unhandled ImportErrors
Browse files Browse the repository at this point in the history
In gh-121 we realized that some people are ignoring documentation and
attempting to use adapters that only work with more recent versions of
Requests and urllib3. As such, we need to handle ImportErrors from
our compatibility module better and issue warnings closer to where a
user might be encountering the problem.

Closes gh-121
  • Loading branch information
sigmavirus24 committed Jul 22, 2016
1 parent 67e3478 commit c446b55
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 7 deletions.
14 changes: 12 additions & 2 deletions requests_toolbelt/_compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,26 @@
import requests

try:
from requests.packages.urllib3 import connection
from requests.packages.urllib3 import fields
from requests.packages.urllib3 import filepost
from requests.packages.urllib3 import poolmanager
except ImportError:
from urllib3 import connection
from urllib3 import fields
from urllib3 import filepost
from urllib3 import poolmanager

try:
from requests.packages.urllib3.connection import HTTPConnection
from requests.packages.urllib3 import connection
except ImportError:
try:
from urllib3.connection import HTTPConnection
from urllib3 import connection
except ImportError:
HTTPConnection = None
connection = None


if requests.__build__ < 0x020300:
timeout = None
else:
Expand Down
20 changes: 15 additions & 5 deletions requests_toolbelt/adapters/socket_options.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
# -*- coding: utf-8 -*-
"""The implementation of the SocketOptionsAdapter."""
import socket
import warnings

import requests
from requests import adapters

from .._compat import connection
from .._compat import poolmanager
from .. import exceptions as exc


class SocketOptionsAdapter(adapters.HTTPAdapter):
Expand All @@ -33,11 +35,19 @@ class SocketOptionsAdapter(adapters.HTTPAdapter):
"""

default_options = getattr(
connection.HTTPConnection,
'default_socket_options',
[(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)]
)
if connection is not None:
default_options = getattr(
connection.HTTPConnection,
'default_socket_options',
[(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)]
)
else:
default_options = []
warnings.warn(exc.RequestsVersionTooOld,
"This version of Requests is only compatible with a "
"version of urllib3 which is too old to support "
"setting options on a socket. This adapter is "
"functionally useless.")

def __init__(self, **kwargs):
self.socket_options = kwargs.pop('socket_options',
Expand Down
9 changes: 9 additions & 0 deletions requests_toolbelt/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,12 @@ class VersionMismatchError(Exception):
appropriately but the version installed is not sufficient.
"""
pass


class RequestsVersionTooOld(Warning):
"""Used to indiciate that the Requests version is too old.
If the version of Requests is too old to support a feature, we will issue
this warning to the user.
"""
pass

0 comments on commit c446b55

Please sign in to comment.