Permalink
Browse files

Identify errors more precisely, using a tuple

  • Loading branch information...
1 parent 828924d commit 626839731767ffbcb12d054ff4aa1bb2be19ef79 @mnot committed Jun 18, 2012
Showing with 17 additions and 12 deletions.
  1. +11 −9 thor/tcp.py
  2. +6 −3 thor/tls.py
View
@@ -106,14 +106,14 @@ class TcpConnection(EventSource):
write_bufsize = 16
read_bufsize = 1024 * 16
- _block_errs = set([
+ _block_errs = set([(socket.error, e) for e in [
errno.EAGAIN, errno.EWOULDBLOCK
- ])
- _close_errs = set([
+ ]])
+ _close_errs = set([(socket.error, e) for e in [
errno.EBADF, errno.ECONNRESET, errno.ESHUTDOWN,
errno.ECONNABORTED, errno.ECONNREFUSED,
errno.ENOTCONN, errno.EPIPE
- ])
+ ]])
def __init__(self, sock, host, port, loop=None):
EventSource.__init__(self, loop)
@@ -151,9 +151,10 @@ def handle_read(self):
# TODO: look into recv_into (but see python issue7827)
data = self.socket.recv(self.read_bufsize)
except Exception, why:
- if why[0] in self._block_errs:
+ err = (type(why), why[0])
+ if err in self._block_errs:
return
- elif why[0] in self._close_errs:
+ elif err in self._close_errs:
self.emit('close')
return
else:
@@ -171,10 +172,11 @@ def handle_write(self):
data = "".join(self._write_buffer)
try:
sent = self.socket.send(data)
- except socket.error, why:
- if why[0] in self._block_errs:
+ except Exception, why:
+ err = (type(why), why[0])
+ if err in self._block_errs:
return
- elif why[0] in self._close_errs:
+ elif err in self._close_errs:
self.emit('close')
return
else:
View
@@ -37,9 +37,12 @@
from thor.tcp import TcpServer, TcpClient, TcpConnection, server_listen
-TcpConnection._block_errs.add(sys_ssl.SSL_ERROR_WANT_READ)
-TcpConnection._block_errs.add(sys_ssl.SSL_ERROR_WANT_WRITE)
-TcpConnection._close_errs.add(sys_ssl.SSL_ERROR_EOF)
+TcpConnection._block_errs.add((sys_ssl.SSLError, sys_ssl.SSL_ERROR_WANT_READ))
+TcpConnection._block_errs.add(
+ (sys_ssl.SSLError, sys_ssl.SSL_ERROR_WANT_WRITE)
+)
+TcpConnection._close_errs.add((sys_ssl.SSLError, sys_ssl.SSL_ERROR_EOF))
+TcpConnection._close_errs.add((sys_ssl.SSLError, sys_ssl.SSL_ERROR_SSL))
# TODO: TlsServer
# TODO: expose cipher info, peer info

0 comments on commit 6268397

Please sign in to comment.