diff options
Diffstat (limited to 'Lib/asyncore.py')
-rw-r--r-- | Lib/asyncore.py | 96 |
1 files changed, 48 insertions, 48 deletions
diff --git a/Lib/asyncore.py b/Lib/asyncore.py index c9b39a3..69becac 100644 --- a/Lib/asyncore.py +++ b/Lib/asyncore.py @@ -37,38 +37,33 @@ if os.name == 'nt': EALREADY = 10037 ECONNRESET = 10054 ENOTCONN = 10057 + ESHUTDOWN = 10058 else: - from errno import EALREADY, EINPROGRESS, EWOULDBLOCK, ECONNRESET, ENOTCONN + from errno import EALREADY, EINPROGRESS, EWOULDBLOCK, ECONNRESET, ENOTCONN, ESHUTDOWN socket_map = {} -def poll (timeout=0.0, ignore_expt=1): +def poll (timeout=0.0): if socket_map: - sockets = socket_map.keys() - r = filter (lambda x: x.readable(), sockets) - w = filter (lambda x: x.writable(), sockets) - if ignore_expt: - e = [] - else: - e = sockets[:] + r = []; w = []; e = [] + for s in socket_map.keys(): + if s.readable(): + r.append (s) + if s.writable(): + w.append (s) (r,w,e) = select.select (r,w,e, timeout) - for x in e: - try: - x.handle_expt_event() - except: - x.handle_error (sys.exc_type, sys.exc_value, sys.exc_traceback) for x in r: try: x.handle_read_event() except: - x.handle_error (sys.exc_type, sys.exc_value, sys.exc_traceback) + x.handle_error() for x in w: try: x.handle_write_event() except: - x.handle_error (sys.exc_type, sys.exc_value, sys.exc_traceback) + x.handle_error() def poll2 (timeout=0.0): import poll @@ -88,18 +83,17 @@ def poll2 (timeout=0.0): if flags: l.append (fd, flags) r = poll.poll (l, timeout) - print r for fd, flags in r: s = fd_map[fd] try: if (flags & poll.POLLIN): - s.handle_read_event() + s.handle_read_event() if (flags & poll.POLLOUT): - s.handle_write_event() + s.handle_write_event() if (flags & poll.POLLERR): - s.handle_expt_event() + s.handle_expt_event() except: - apply (s.handle_error, sys.exc_info()) + s.handle_error() def loop (timeout=30.0, use_poll=0): @@ -149,12 +143,14 @@ class dispatcher: return '<__repr__ (self) failed for object at %x (addr=%s)>' % (id(self),ar) def add_channel (self): - self.log ('adding channel %s' % self) + if __debug__: + self.log ('adding channel %s' % self) socket_map [self] = 1 def del_channel (self): if socket_map.has_key (self): - self.log ('closing channel %d:%s' % (self.fileno(), self)) + if __debug__: + self.log ('closing channel %d:%s' % (self.fileno(), self)) del socket_map [self] def create_socket (self, family, type): @@ -164,7 +160,8 @@ class dispatcher: self.add_channel() def set_socket (self, socket): - self.socket = socket + # This is done so we can be called safely from __init__ + self.__dict__['socket'] = socket self.add_channel() def set_reuse_addr (self): @@ -210,6 +207,7 @@ class dispatcher: return self.socket.bind (addr) def connect (self, address): + self.connected = 0 try: self.socket.connect (address) except socket.error, why: @@ -253,7 +251,7 @@ class dispatcher: return data except socket.error, why: # winsock sometimes throws ENOTCONN - if why[0] in [ECONNRESET, ENOTCONN]: + if why[0] in [ECONNRESET, ENOTCONN, ESHUTDOWN]: self.handle_close() return '' else: @@ -262,15 +260,12 @@ class dispatcher: def close (self): self.del_channel() self.socket.close() - self.connected = 0 # cheap inheritance, used to pass all other attribute # references to the underlying socket object. + # NOTE: this may be removed soon for performance reasons. def __getattr__ (self, attr): - if attr != 'socket': - return getattr (self.socket, attr) - else: - raise AttributeError, attr + return getattr (self.socket, attr) def log (self, message): print 'log:', message @@ -299,9 +294,8 @@ class dispatcher: def handle_expt_event (self): self.handle_expt() - def handle_error (self, *info): - (t,v,tb) = info - (file,fun,line), tbinfo = compact_traceback (t,v,tb) + def handle_error (self): + (file,fun,line), t, v, tbinfo = compact_traceback() # sometimes a user repr method will crash. try: @@ -312,34 +306,36 @@ class dispatcher: print ( 'uncaptured python exception, closing channel %s (%s:%s %s)' % ( self_repr, - str(t), - str(v), + t, + v, tbinfo ) ) - del t,v,tb self.close() def handle_expt (self): - self.log ('unhandled exception') + if __debug__: + self.log ('unhandled exception') def handle_read (self): - self.log ('unhandled read event') + if __debug__: + self.log ('unhandled read event') def handle_write (self): - self.log ('unhandled write event') + if __debug__: + self.log ('unhandled write event') def handle_connect (self): - self.log ('unhandled connect event') - - def handle_oob (self): - self.log ('unhandled out-of-band event') + if __debug__: + self.log ('unhandled connect event') def handle_accept (self): - self.log ('unhandled accept event') + if __debug__: + self.log ('unhandled accept event') def handle_close (self): - self.log ('unhandled close event') + if __debug__: + self.log ('unhandled close event') self.close() # --------------------------------------------------------------------------- @@ -373,7 +369,8 @@ class dispatcher_with_send (dispatcher): # used for debugging. # --------------------------------------------------------------------------- -def compact_traceback (t,v,tb): +def compact_traceback (): + t,v,tb = sys.exc_info() tbinfo = [] while 1: tbinfo.append ( @@ -385,6 +382,9 @@ def compact_traceback (t,v,tb): if not tb: break + # just to be safe + del tb + file, function, line = tbinfo[-1] info = '[' + string.join ( map ( @@ -393,7 +393,7 @@ def compact_traceback (t,v,tb): ), '] [' ) + ']' - return (file, function, line), info + return (file, function, line), t, v, info def close_all (): global socket_map @@ -450,4 +450,4 @@ if os.name == 'posix': def set_file (self, fd): self.socket = file_wrapper (fd) self.add_channel() -#not really + |