diff -r cdc3837cb1fc Lib/asyncore.py --- a/Lib/asyncore.py Tue Dec 16 03:21:54 2014 -0500 +++ b/Lib/asyncore.py Tue Dec 16 17:30:26 2014 +0800 @@ -54,30 +54,33 @@ import os from errno import EALREADY, EINPROGRESS, EWOULDBLOCK, ECONNRESET, EINVAL, \ - ENOTCONN, ESHUTDOWN, EISCONN, EBADF, ECONNABORTED, EPIPE, EAGAIN, \ - errorcode + ENOTCONN, ESHUTDOWN, EISCONN, EBADF, ECONNABORTED, EPIPE, EAGAIN, \ + errorcode -_DISCONNECTED = frozenset({ECONNRESET, ENOTCONN, ESHUTDOWN, ECONNABORTED, EPIPE, - EBADF}) +_DISCONNECTED = frozenset({ECONNRESET, ENOTCONN, ESHUTDOWN, ECONNABORTED, + EPIPE, EBADF}) try: socket_map except NameError: socket_map = {} + def _strerror(err): try: return os.strerror(err) except (ValueError, OverflowError, NameError): if err in errorcode: return errorcode[err] - return "Unknown error %s" %err + return "Unknown error %s" % err + class ExitNow(Exception): pass _reraised_exceptions = (ExitNow, KeyboardInterrupt, SystemExit) + def read(obj): try: obj.handle_read_event() @@ -86,6 +89,7 @@ except: obj.handle_error() + def write(obj): try: obj.handle_write_event() @@ -94,6 +98,7 @@ except: obj.handle_error() + def _exception(obj): try: obj.handle_expt_event() @@ -102,6 +107,7 @@ except: obj.handle_error() + def readwrite(obj, flags): try: if flags & select.POLLIN: @@ -122,11 +128,12 @@ except: obj.handle_error() + def poll(timeout=0.0, map=None): if map is None: map = socket_map if map: - r = []; w = []; e = [] + r, w, e = [], [], [] for fd, obj in list(map.items()): is_r = obj.readable() is_w = obj.writable() @@ -164,6 +171,7 @@ continue _exception(obj) + def poll2(timeout=0.0, map=None): # Use the poll() support added to the select module in Python 2.0 if map is None: @@ -194,6 +202,7 @@ poll3 = poll2 # Alias for backward compatibility + def loop(timeout=30.0, use_poll=False, map=None, count=None): if map is None: map = socket_map @@ -212,6 +221,7 @@ poll_fun(timeout, map) count = count - 1 + class dispatcher: debug = False @@ -270,7 +280,7 @@ __str__ = __repr__ def add_channel(self, map=None): - #self.log_info('adding channel %s' % self) + # self.log_info('adding channel %s' % self) if map is None: map = self._map map[self._fileno] = self @@ -280,7 +290,7 @@ if map is None: map = self._map if fd in map: - #self.log_info('closing channel %d:%s' % (fd, self)) + # self.log_info('closing channel %d:%s' % (fd, self)) del map[fd] self._fileno = None @@ -292,7 +302,7 @@ def set_socket(self, sock, map=None): self.socket = sock -## self.__dict__['socket'] = sock + # self.__dict__['socket'] = sock self._fileno = sock.fileno() self.add_channel(map) @@ -337,8 +347,8 @@ self.connected = False self.connecting = True err = self.socket.connect_ex(address) - if err in (EINPROGRESS, EALREADY, EWOULDBLOCK) \ - or err == EINVAL and os.name in ('nt', 'ce'): + if err in (EINPROGRESS, EALREADY, EWOULDBLOCK) or\ + err == EINVAL and os.name in ('nt', 'ce'): self.addr = address return if err in (0, EISCONN): @@ -511,6 +521,7 @@ # [for more sophisticated usage use asynchat.async_chat] # --------------------------------------------------------------------------- + class dispatcher_with_send(dispatcher): def __init__(self, sock=None, map=None): @@ -538,10 +549,11 @@ # used for debugging. # --------------------------------------------------------------------------- + def compact_traceback(): t, v, tb = sys.exc_info() tbinfo = [] - if not tb: # Must have a traceback + if not tb: # Must have a traceback raise AssertionError("traceback does not exist") while tb: tbinfo.append(( @@ -558,6 +570,7 @@ info = ' '.join(['[%s|%s|%s]' % x for x in tbinfo]) return (file, function, line), t, v, info + def close_all(map=None, ignore_all=False): if map is None: map = socket_map @@ -610,9 +623,9 @@ return os.write(self.fd, *args) def getsockopt(self, level, optname, buflen=None): - if (level == socket.SOL_SOCKET and - optname == socket.SO_ERROR and - not buflen): + if level == socket.SOL_SOCKET and \ + optname == socket.SO_ERROR and \ + not buflen: return 0 raise NotImplementedError("Only asyncore specific behaviour " "implemented.")