diff options
author | wbond <will@wbond.net> | 2018-04-05 12:02:29 -0400 |
---|---|---|
committer | wbond <will@wbond.net> | 2018-04-05 12:02:29 -0400 |
commit | a24d46683f58096f5a49fdc79cc7f8b1ade75a5a (patch) | |
tree | 84de3e23790af580c76aebddb2b32646a35129d2 | |
parent | 7bab7ae1d2b416aeda3ecac8e94d1636e972407f (diff) | |
download | urllib3-a24d46683f58096f5a49fdc79cc7f8b1ade75a5a.tar.gz |
Allow SecureTransport to work on Python 2.6
-rw-r--r-- | urllib3/contrib/securetransport.py | 24 |
1 files changed, 18 insertions, 6 deletions
diff --git a/urllib3/contrib/securetransport.py b/urllib3/contrib/securetransport.py index ea54c0c7..9a3ec653 100644 --- a/urllib3/contrib/securetransport.py +++ b/urllib3/contrib/securetransport.py @@ -53,8 +53,9 @@ except ImportError: # Platform-specific: Python 3 try: memoryview(b'') + has_memoryview = True except NameError: - raise ImportError("SecureTransport only works on Pythons with memoryview") + has_memoryview = False __all__ = ['inject_into_urllib3', 'extract_from_urllib3'] @@ -195,8 +196,11 @@ def _read_callback(connection_id, data_buffer, data_length_pointer): timeout = wrapped_socket.gettimeout() error = None read_count = 0 - buffer = (ctypes.c_char * requested_length).from_address(data_buffer) - buffer_view = memoryview(buffer) + if has_memoryview: + buffer = (ctypes.c_char * requested_length).from_address(data_buffer) + buffer_view = memoryview(buffer) + else: + buffer_view = None try: while read_count < requested_length: @@ -207,9 +211,17 @@ def _read_callback(connection_id, data_buffer, data_length_pointer): # We need to tell ctypes that we have a buffer that can be # written to. Upsettingly, we do that like this: - chunk_size = base_socket.recv_into( - buffer_view[read_count:requested_length] - ) + if buffer_view is not None: + chunk_size = base_socket.recv_into( + buffer_view[read_count:requested_length] + ) + else: + remaining = requested_length - read_count + # This is likely innefficient, but works on Python 2.6 + buffer = (ctypes.c_char * remaining).from_address( + data_buffer + read_count + ) + chunk_size = base_socket.recv_into(buffer, remaining) read_count += chunk_size if not chunk_size: if not read_count: |