<s> """<STR_LIT>""" <EOL> import time <EOL> from gevent import _socketcommon <EOL> from gevent . _util import copy_globals <EOL> from gevent . _compat import PYPY <EOL> copy_globals ( _socketcommon , globals ( ) , <EOL> names_to_ignore = _socketcommon . __py3_imports__ + _socketcommon . __extensions__ , <EOL> dunder_names_to_keep = ( ) ) <EOL> __socket__ = _socketcommon . __socket__ <EOL> __implements__ = _socketcommon . _implements <EOL> __extensions__ = _socketcommon . __extensions__ <EOL> __imports__ = [ i for i in _socketcommon . __imports__ if i not in _socketcommon . __py3_imports__ ] <EOL> __dns__ = _socketcommon . __dns__ <EOL> try : <EOL> _fileobject = __socket__ . _fileobject <EOL> _socketmethods = __socket__ . _socketmethods <EOL> except AttributeError : <EOL> _fileobject = object <EOL> _socketmethods = ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' ) <EOL> else : <EOL> if not hasattr ( _fileobject , '<STR_LIT>' ) : <EOL> class _fileobject ( _fileobject ) : <EOL> def __enter__ ( self ) : <EOL> return self <EOL> def __exit__ ( self , * args ) : <EOL> if not self . closed : <EOL> self . close ( ) <EOL> def _get_memory ( data ) : <EOL> try : <EOL> mv = memoryview ( data ) <EOL> if mv . shape : <EOL> return mv <EOL> return mv . tobytes ( ) <EOL> except TypeError : <EOL> return buffer ( data ) <EOL> class _closedsocket ( object ) : <EOL> __slots__ = [ ] <EOL> def _dummy ( * args , ** kwargs ) : <EOL> raise error ( EBADF , '<STR_LIT>' ) <EOL> send = recv = recv_into = sendto = recvfrom = recvfrom_into = _dummy <EOL> if PYPY : <EOL> def _drop ( self ) : <EOL> pass <EOL> def _reuse ( self ) : <EOL> pass <EOL> __getattr__ = _dummy <EOL> timeout_default = object ( ) <EOL> class socket ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , family = AF_INET , type = SOCK_STREAM , proto = <NUM_LIT:0> , _sock = None ) : <EOL> if _sock is None : <EOL> self . _sock = _realsocket ( family , type , proto ) <EOL> self . timeout = _socket . getdefaulttimeout ( ) <EOL> else : <EOL> if hasattr ( _sock , '<STR_LIT>' ) : <EOL> self . _sock = _sock . _sock <EOL> self . timeout = getattr ( _sock , '<STR_LIT>' , False ) <EOL> if self . timeout is False : <EOL> self . timeout = _socket . getdefaulttimeout ( ) <EOL> else : <EOL> self . _sock = _sock <EOL> self . timeout = _socket . getdefaulttimeout ( ) <EOL> if PYPY : <EOL> self . _sock . _reuse ( ) <EOL> self . _sock . setblocking ( <NUM_LIT:0> ) <EOL> fileno = self . _sock . fileno ( ) <EOL> self . hub = get_hub ( ) <EOL> io = self . hub . loop . io <EOL> self . _read_event = io ( fileno , <NUM_LIT:1> ) <EOL> self . _write_event = io ( fileno , <NUM_LIT:2> ) <EOL> def __repr__ ( self ) : <EOL> return '<STR_LIT>' % ( type ( self ) . __name__ , hex ( id ( self ) ) , self . _formatinfo ( ) ) <EOL> def __str__ ( self ) : <EOL> return '<STR_LIT>' % ( type ( self ) . __name__ , self . _formatinfo ( ) ) <EOL> def _formatinfo ( self ) : <EOL> try : <EOL> fileno = self . fileno ( ) <EOL> except Exception as ex : <EOL> fileno = str ( ex ) <EOL> try : <EOL> sockname = self . getsockname ( ) <EOL> sockname = '<STR_LIT>' % sockname <EOL> except Exception : <EOL> sockname = None <EOL> try : <EOL> peername = self . getpeername ( ) <EOL> peername = '<STR_LIT>' % peername <EOL> except Exception : <EOL> peername = None <EOL> result = '<STR_LIT>' % fileno <EOL> if sockname is not None : <EOL> result += '<STR_LIT>' + str ( sockname ) <EOL> if peername is not None : <EOL> result += '<STR_LIT>' + str ( peername ) <EOL> if getattr ( self , '<STR_LIT>' , None ) is not None : <EOL> result += '<STR_LIT>' + str ( self . timeout ) <EOL> return result <EOL> def _get_ref ( self ) : <EOL> return self . _read_event . ref or self . _write_event . ref <EOL> def _set_ref ( self , value ) : <EOL> self . _read_event . ref = value <EOL> self . _write_event . ref = value <EOL> ref = property ( _get_ref , _set_ref ) <EOL> def _wait ( self , watcher , timeout_exc = timeout ( '<STR_LIT>' ) ) : <EOL> """<STR_LIT>""" <EOL> if watcher . callback is not None : <EOL> raise _socketcommon . ConcurrentObjectUseError ( '<STR_LIT>' % ( watcher . callback , ) ) <EOL> if self . timeout is not None : <EOL> timeout = Timeout . start_new ( self . timeout , timeout_exc , ref = False ) <EOL> else : <EOL> timeout = None <EOL> try : <EOL> self . hub . wait ( watcher ) <EOL> finally : <EOL> if timeout is not None : <EOL> timeout . cancel ( ) <EOL> def accept ( self ) : <EOL> sock = self . _sock <EOL> while True : <EOL> try : <EOL> client_socket , address = sock . accept ( ) <EOL> break <EOL> except error as ex : <EOL> if ex . args [ <NUM_LIT:0> ] != EWOULDBLOCK or self . timeout == <NUM_LIT:0.0> : <EOL> raise <EOL> sys . exc_clear ( ) <EOL> self . _wait ( self . _read_event ) <EOL> sockobj = socket ( _sock = client_socket ) <EOL> if PYPY : <EOL> client_socket . _drop ( ) <EOL> return sockobj , address <EOL> def close ( self , _closedsocket = _closedsocket , cancel_wait_ex = cancel_wait_ex ) : <EOL> self . hub . cancel_wait ( self . _read_event , cancel_wait_ex ) <EOL> self . hub . cancel_wait ( self . _write_event , cancel_wait_ex ) <EOL> s = self . _sock <EOL> self . _sock = _closedsocket ( ) <EOL> if PYPY : <EOL> s . _drop ( ) <EOL> @ property <EOL> def closed ( self ) : <EOL> return isinstance ( self . _sock , _closedsocket ) <EOL> def connect ( self , address ) : <EOL> if self . timeout == <NUM_LIT:0.0> : <EOL> return self . _sock . connect ( address ) <EOL> sock = self . _sock <EOL> if isinstance ( address , tuple ) : <EOL> r = getaddrinfo ( address [ <NUM_LIT:0> ] , address [ <NUM_LIT:1> ] , sock . family , sock . type , sock . proto ) <EOL> address = r [ <NUM_LIT:0> ] [ - <NUM_LIT:1> ] <EOL> if self . timeout is not None : <EOL> timer = Timeout . start_new ( self . timeout , timeout ( '<STR_LIT>' ) ) <EOL> else : <EOL> timer = None <EOL> try : <EOL> while True : <EOL> err = sock . getsockopt ( SOL_SOCKET , SO_ERROR ) <EOL> if err : <EOL> raise error ( err , strerror ( err ) ) <EOL> result = sock . connect_ex ( address ) <EOL> if not result or result == EISCONN : <EOL> break <EOL> elif ( result in ( EWOULDBLOCK , EINPROGRESS , EALREADY ) ) or ( result == EINVAL and is_windows ) : <EOL> self . _wait ( self . _write_event ) <EOL> else : <EOL> raise error ( result , strerror ( result ) ) <EOL> finally : <EOL> if timer is not None : <EOL> timer . cancel ( ) <EOL> def connect_ex ( self , address ) : <EOL> try : <EOL> return self . connect ( address ) or <NUM_LIT:0> <EOL> except timeout : <EOL> return EAGAIN <EOL> except error as ex : <EOL> if type ( ex ) is error : <EOL> return ex . args [ <NUM_LIT:0> ] <EOL> else : <EOL> raise <EOL> def dup ( self ) : <EOL> """<STR_LIT>""" <EOL> return socket ( _sock = self . _sock ) <EOL> def makefile ( self , mode = '<STR_LIT:r>' , bufsize = - <NUM_LIT:1> ) : <EOL> fobj = _fileobject ( type ( self ) ( _sock = self ) , mode , bufsize ) <EOL> if PYPY : <EOL> self . _sock . _drop ( ) <EOL> return fobj <EOL> def recv ( self , * args ) : <EOL> sock = self . _sock <EOL> while True : <EOL> try : <EOL> return sock . recv ( * args ) <EOL> except error as ex : <EOL> if ex . args [ <NUM_LIT:0> ] != EWOULDBLOCK or self . timeout == <NUM_LIT:0.0> : <EOL> raise <EOL> sys . exc_clear ( ) <EOL> self . _wait ( self . _read_event ) <EOL> def recvfrom ( self , * args ) : <EOL> sock = self . _sock <EOL> while True : <EOL> try : <EOL> return sock . recvfrom ( * args ) <EOL> except error as ex : <EOL> if ex . args [ <NUM_LIT:0> ] != EWOULDBLOCK or self . timeout == <NUM_LIT:0.0> : <EOL> raise <EOL> sys . exc_clear ( ) <EOL> self . _wait ( self . _read_event ) <EOL> def recvfrom_into ( self , * args ) : <EOL> sock = self . _sock <EOL> while True : <EOL> try : <EOL> return sock . recvfrom_into ( * args ) <EOL> except error as ex : <EOL> if ex . args [ <NUM_LIT:0> ] != EWOULDBLOCK or self . timeout == <NUM_LIT:0.0> : <EOL> raise <EOL> sys . exc_clear ( ) <EOL> self . _wait ( self . _read_event ) <EOL> def recv_into ( self , * args ) : <EOL> sock = self . _sock <EOL> while True : <EOL> try : <EOL> return sock . recv_into ( * args ) <EOL> except error as ex : <EOL> if ex . args [ <NUM_LIT:0> ] != EWOULDBLOCK or self . timeout == <NUM_LIT:0.0> : <EOL> raise <EOL> sys . exc_clear ( ) <EOL> self . _wait ( self . _read_event ) <EOL> def send ( self , data , flags = <NUM_LIT:0> , timeout = timeout_default ) : <EOL> sock = self . _sock <EOL> if timeout is timeout_default : <EOL> timeout = self . timeout <EOL> try : <EOL> return sock . send ( data , flags ) <EOL> except error as ex : <EOL> if ex . args [ <NUM_LIT:0> ] != EWOULDBLOCK or timeout == <NUM_LIT:0.0> : <EOL> raise <EOL> sys . exc_clear ( ) <EOL> self . _wait ( self . _write_event ) <EOL> try : <EOL> return sock . send ( data , flags ) <EOL> except error as ex2 : <EOL> if ex2 . args [ <NUM_LIT:0> ] == EWOULDBLOCK : <EOL> return <NUM_LIT:0> <EOL> raise <EOL> def __send_chunk ( self , data_memory , flags , timeleft , end ) : <EOL> """<STR_LIT>""" <EOL> data_sent = <NUM_LIT:0> <EOL> len_data_memory = len ( data_memory ) <EOL> while data_sent < len_data_memory : <EOL> chunk = data_memory [ data_sent : ] <EOL> if timeleft is None : <EOL> data_sent += self . send ( chunk , flags ) <EOL> elif timeleft <= <NUM_LIT:0> : <EOL> raise timeout ( '<STR_LIT>' ) <EOL> else : <EOL> data_sent += self . send ( chunk , flags , timeout = timeleft ) <EOL> timeleft = end - time . time ( ) <EOL> return timeleft <EOL> def sendall ( self , data , flags = <NUM_LIT:0> ) : <EOL> if isinstance ( data , unicode ) : <EOL> data = data . encode ( ) <EOL> data_memory = _get_memory ( data ) <EOL> len_data_memory = len ( data_memory ) <EOL> if not len_data_memory : <EOL> return <NUM_LIT:0> <EOL> chunk_size = max ( self . getsockopt ( SOL_SOCKET , SO_SNDBUF ) , <NUM_LIT> * <NUM_LIT> ) <EOL> data_sent = <NUM_LIT:0> <EOL> end = None <EOL> timeleft = None <EOL> if self . timeout is not None : <EOL> timeleft = self . timeout <EOL> end = time . time ( ) + timeleft <EOL> while data_sent < len_data_memory : <EOL> chunk_end = min ( data_sent + chunk_size , len_data_memory ) <EOL> chunk = data_memory [ data_sent : chunk_end ] <EOL> timeleft = self . __send_chunk ( chunk , flags , timeleft , end ) <EOL> data_sent += len ( chunk ) <EOL> def sendto ( self , * args ) : <EOL> sock = self . _sock <EOL> try : <EOL> return sock . sendto ( * args ) <EOL> except error as ex : <EOL> if ex . args [ <NUM_LIT:0> ] != EWOULDBLOCK or self . timeout == <NUM_LIT:0.0> : <EOL> raise <EOL> sys . exc_clear ( ) <EOL> self . _wait ( self . _write_event ) <EOL> try : <EOL> return sock . sendto ( * args ) <EOL> except error as ex2 : <EOL> if ex2 . args [ <NUM_LIT:0> ] == EWOULDBLOCK : <EOL> return <NUM_LIT:0> <EOL> raise <EOL> def setblocking ( self , flag ) : <EOL> if flag : <EOL> self . timeout = None <EOL> else : <EOL> self . timeout = <NUM_LIT:0.0> <EOL> def settimeout ( self , howlong ) : <EOL> if howlong is not None : <EOL> try : <EOL> f = howlong . __float__ <EOL> except AttributeError : <EOL> raise TypeError ( '<STR_LIT>' ) <EOL> howlong = f ( ) <EOL> if howlong < <NUM_LIT:0.0> : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> self . __dict__ [ '<STR_LIT>' ] = howlong <EOL> def gettimeout ( self ) : <EOL> return self . __dict__ [ '<STR_LIT>' ] <EOL> def shutdown ( self , how ) : <EOL> if how == <NUM_LIT:0> : <EOL> self . hub . cancel_wait ( self . _read_event , cancel_wait_ex ) <EOL> elif how == <NUM_LIT:1> : <EOL> self . hub . cancel_wait ( self . _write_event , cancel_wait_ex ) <EOL> else : <EOL> self . hub . cancel_wait ( self . _read_event , cancel_wait_ex ) <EOL> self . hub . cancel_wait ( self . _write_event , cancel_wait_ex ) <EOL> self . _sock . shutdown ( how ) <EOL> family = property ( lambda self : self . _sock . family ) <EOL> type = property ( lambda self : self . _sock . type ) <EOL> proto = property ( lambda self : self . _sock . proto ) <EOL> def fileno ( self ) : <EOL> return self . _sock . fileno ( ) <EOL> def getsockname ( self ) : <EOL> return self . _sock . getsockname ( ) <EOL> def getpeername ( self ) : <EOL> return self . _sock . getpeername ( ) <EOL> _s = "<STR_LIT>" <EOL> _m = None <EOL> for _m in set ( _socketmethods ) - set ( locals ( ) ) : <EOL> exec ( _s % ( _m , _m , ) ) <EOL> del _m , _s <EOL> if PYPY : <EOL> def _reuse ( self ) : <EOL> self . _sock . _reuse ( ) <EOL> def _drop ( self ) : <EOL> self . _sock . _drop ( ) <EOL> SocketType = socket <EOL> if hasattr ( _socket , '<STR_LIT>' ) : <EOL> def socketpair ( * args ) : <EOL> one , two = _socket . socketpair ( * args ) <EOL> result = socket ( _sock = one ) , socket ( _sock = two ) <EOL> if PYPY : <EOL> one . _drop ( ) <EOL> two . _drop ( ) <EOL> return result <EOL> elif '<STR_LIT>' in __implements__ : <EOL> __implements__ . remove ( '<STR_LIT>' ) <EOL> if hasattr ( _socket , '<STR_LIT>' ) : <EOL> def fromfd ( * args ) : <EOL> s = _socket . fromfd ( * args ) <EOL> result = socket ( _sock = s ) <EOL> if PYPY : <EOL> s . _drop ( ) <EOL> return result <EOL> elif '<STR_LIT>' in __implements__ : <EOL> __implements__ . remove ( '<STR_LIT>' ) <EOL> if hasattr ( __socket__ , '<STR_LIT>' ) : <EOL> def ssl ( sock , keyfile = None , certfile = None ) : <EOL> from gevent import ssl as _sslmod <EOL> wrap = getattr ( _sslmod , '<STR_LIT>' , None ) or getattr ( _sslmod , '<STR_LIT>' ) <EOL> return wrap ( sock , keyfile , certfile ) <EOL> __implements__ . append ( '<STR_LIT>' ) <EOL> __all__ = __implements__ + __extensions__ + __imports__ </s>
