# Temporary hack until https://github.com/gevent/gevent/issues/477 is
# fixed.

# Re-add sslwrap to Python 2.7.9
import inspect
import gevent.ssl as ssl2
__ssl__ = __import__('ssl')

try:
    _ssl = __ssl__._ssl
except AttributeError:
    _ssl = __ssl__._ssl2


orig_SSLSocket = ssl2.SSLSocket


class SSLSocketFixed(orig_SSLSocket):
    def __init__(self, *args, **kwargs):
        # https://github.com/gevent/gevent/issues/477
        kwargs.pop('server_hostname', None)
        kwargs.pop('_context', None)
        print kwargs
        orig_SSLSocket.__init__(self, *args, **kwargs)

ssl2.SSLSocket = SSLSocketFixed
__ssl__.SSLSocket = SSLSocketFixed


def new_sslwrap(sock, server_side=False, keyfile=None, certfile=None, cert_reqs=__ssl__.CERT_NONE, ssl_version=__ssl__.PROTOCOL_SSLv23, ca_certs=None, ciphers=None):
    context = __ssl__.SSLContext(ssl_version)
    context.verify_mode = cert_reqs or __ssl__.CERT_NONE
    if ca_certs:
        context.load_verify_locations(ca_certs)
    if certfile:
        context.load_cert_chain(certfile, keyfile)
    if ciphers:
        context.set_ciphers(ciphers)

    caller_self = inspect.currentframe().f_back.f_locals['self']
    return context._wrap_socket(sock, server_side=server_side, ssl_sock=caller_self)

if not hasattr(_ssl, 'sslwrap'):
    _ssl.sslwrap = new_sslwrap
