<commit_before># urllib3/connectionpool.py
# Copyright 2008-2013 <NAME> and contributors (see CONTRIBUTORS.txt)
#
# This module is part of urllib3 and is released under
# the MIT License: http://www.opensource.org/licenses/mit-license.php

import logging
import socket
import time

from socket import error as SocketError, timeout as SocketTimeout
from .util import (resolve_cert_reqs, resolve_ssl_version, assert_fingerprint,
                   DEFAULT_TIMEOUT, Timeout)

try: # Python 3
    from http.client import HTTPConnection, HTTPException
    from http.client import HTTP_PORT, HTTPS_PORT
except ImportError:
    from httplib import HTTPConnection, HTTPException
    from httplib import HTTP_PORT, HTTPS_PORT

try: # Python 3
    from queue import LifoQueue, Empty, Full
except ImportError:
    from Queue import LifoQueue, Empty, Full
    import Queue as _  # Platform-specific: Windows


try: # Compiled with SSL?
    HTTPSConnection = object

    class BaseSSLError(BaseException):
        pass

    ssl = None

    try: # Python 3
        from http.client import HTTPSConnection
    except ImportError:
        from httplib import HTTPSConnection

    import ssl
    BaseSSLError = ssl.SSLError

except (ImportError, AttributeError): # Platform-specific: No SSL.
    pass


from .request import RequestMethods
from .response import HTTPResponse
from .util import get_host, is_connection_dropped, ssl_wrap_socket
from .exceptions import (
    ClosedPoolError,
    ConnectTimeoutError,
    EmptyPoolError,
    HostChangedError,
    MaxRetryError,
    SSLError,
    RequestTimeoutError,
    ProxyError,
)

from .packages.ssl_match_hostname import match_hostname, CertificateError
from .packages import six


xrange = six.moves.xrange

log = logging.getLogger(__name__)

_Default = object()
# The default timeout to use for socket connections. This is the attribute used
# by httplib to define the default timeout

port_by_scheme = {
    'http': HTTP_PORT,
    'https': HTTPS_PORT,
}

class EnhancedHTTPConnection(HTTPConnection):
    """ A :class:`httplib.HTTPConnection` that supports connection timeouts

    It would be nice not to have to override this class, however the default
    httplib.py does not allow setting separate connection and request timeouts,
    see http://hg.python.org/cpython/file/2.7/Lib/httplib.py#l769

    The behavior of this class and httplib.py should differ mainly in the
    connection timeout setting.
    """

    def __init__(self, host, port=None, strict=None, timeout=DEFAULT_TIMEOUT,
                 source_address=None):
        """ Create a new EnhancedHTTPConnection.

        This function is necessary to set our connect timeout value, otherwise
        the interface should mirror the HTTPConnection.__init__ interface in
        httplib.py
        """
        if isinstance(timeout, Timeout):
            # Call our timeout value enhanced_timeout to avoid type/assignment
            # errors with the parent class
            self.enhanced_timeout = timeout.clone()
            HTTPConnection.__init__(self, host, port=port, strict=strict,
                                    source_address=source_address,
                                    timeout=timeout.request)
        else:
            # This branch is for backwards compatibility, can be removed later
            self.enhanced_timeout = Timeout(request=timeout)
            HTTPConnection.__init__(self, host, port=port, strict=strict,
                                    source_address=source_address,
                                    timeout=timeout)


    def connect(self):
        """Connect to the host and port specified in __init__.

        This should mirror the implementation in httplib except to insert our
        connect timeout, instead of the global timeout attribute specified by
        :class:`httplib.HTTPConnection`, and then set the timeout on the socket
        to the new request timeout.
        """
        try:
            self.enhanced_timeout.start()
            self.sock = socket.create_connection(
                (self.host, self.port),
                self.enhanced_timeout.get_connect_timeout(),
                self.source_address)
            self.enhanced_timeout.stop()
        except SocketTimeout:
            raise ConnectTimeoutError(
                self, "Connection to %s timed out. (connect timeout=%s)" %
                (self.host, self.enhanced_timeout.connect))

        try:
            # We've connected, so set the timeout on the socket to the request<commit_msg>source_address not supported in python2.6<commit_after># urllib3/connectionpool.py
# Copyright 2008-2013 <NAME> and contributors (see CONTRIBUTORS.txt)
#
# This module is part of urllib3 and is released under
# the MIT License: http://www.opensource.org/licenses/mit-license.php

import logging
import socket
import time

from socket import error as SocketError, timeout as SocketTimeout
from .util import (resolve_cert_reqs, resolve_ssl_version, assert_fingerprint,
                   DEFAULT_TIMEOUT, Timeout)

try: # Python 3
    from http.client import HTTPConnection, HTTPException
    from http.client import HTTP_PORT, HTTPS_PORT
except ImportError:
    from httplib import HTTPConnection, HTTPException
    from httplib import HTTP_PORT, HTTPS_PORT

try: # Python 3
    from queue import LifoQueue, Empty, Full
except ImportError:
    from Queue import LifoQueue, Empty, Full
    import Queue as _  # Platform-specific: Windows


try: # Compiled with SSL?
    HTTPSConnection = object

    class BaseSSLError(BaseException):
        pass

    ssl = None

    try: # Python 3
        from http.client import HTTPSConnection
    except ImportError:
        from httplib import HTTPSConnection

    import ssl
    BaseSSLError = ssl.SSLError

except (ImportError, AttributeError): # Platform-specific: No SSL.
    pass


from .request import RequestMethods
from .response import HTTPResponse
from .util import get_host, is_connection_dropped, ssl_wrap_socket
from .exceptions import (
    ClosedPoolError,
    ConnectTimeoutError,
    EmptyPoolError,
    HostChangedError,
    MaxRetryError,
    SSLError,
    RequestTimeoutError,
    ProxyError,
)

from .packages.ssl_match_hostname import match_hostname, CertificateError
from .packages import six


xrange = six.moves.xrange

log = logging.getLogger(__name__)

_Default = object()
# The default timeout to use for socket connections. This is the attribute used
# by httplib to define the default timeout

port_by_scheme = {
    'http': HTTP_PORT,
    'https': HTTPS_PORT,
}

class EnhancedHTTPConnection(HTTPConnection):
    """ A :class:`httplib.HTTPConnection` that supports connection timeouts

    It would be nice not to have to override this class, however the default
    httplib.py does not allow setting separate connection and request timeouts,
    see http://hg.python.org/cpython/file/2.7/Lib/httplib.py#l769

    The behavior of this class and httplib.py should differ mainly in the
    connection timeout setting.
    """

    def __init__(self, host, port=None, strict=None, timeout=DEFAULT_TIMEOUT,
                 source_address=None):
        """ Create a new EnhancedHTTPConnection.

        This function is necessary to set our connect timeout value, otherwise
        the interface should mirror the HTTPConnection.__init__ interface in
        httplib.py
        """

        # source_address only added to httplib in python 2.7 - move this into
        # the __init__ call below if python2.6 support is dropped.
        self.source_address = source_address

        if isinstance(timeout, Timeout):
            # Call our timeout value enhanced_timeout to avoid type/assignment
            # errors with the parent class
            self.enhanced_timeout = timeout.clone()
            HTTPConnection.__init__(self, host, port=port, strict=strict,
                                    timeout=timeout.request)
        else:
            # This branch is for backwards compatibility, can be removed later
            self.enhanced_timeout = Timeout(request=timeout)
            HTTPConnection.__init__(self, host, port=port, strict=strict,
                                    timeout=timeout)


    def connect(self):
        """Connect to the host and port specified in __init__.

        This should mirror the implementation in httplib except to insert our
        connect timeout, instead of the global timeout attribute specified by
        :class:`httplib.HTTPConnection`, and then set the timeout on the socket
        to the new request timeout.
        """
        try:
            self.enhanced_timeout.start()
            self.sock = socket.create_connection(
                (self.host, self.port),
                self.enhanced_timeout.get_connect_timeout(),
                self.source_address)
            self.enhanced_timeout.stop()
        except SocketTimeout:
            raise ConnectTimeoutError(
                self, "Connection to %s timed out. (connect timeout=%s)" %
                (self.host, self.enhanced_timeout.connect))

        try:
            # We've connected, so set the timeout on the socket to the request
{'fraction_non_alphanumeric': 0.06274552597119162, 'fraction_numerical': 0.004583151462243562, 'mean_word_length': 2.7454025337147527, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4522026', 'n_tokens_mistral': 2443, 'n_tokens_neox': 2325, 'n_words': 819}