Socket exceptions aren't in the standard exception hierarchy

John Nagle nagle at animats.com
Mon Apr 23 02:20:25 EDT 2007


    Here are three network-related exceptions.  These
were caught by "except" with no exception type, because
none of the more specific exceptions matched.   This
is what a traceback produced:


1. File "D:\Python24\lib\socket.py", line 295, in read
data = self._sock.recv(recv_size)
timeout: timed out	

2. File "D:\Python24\lib\socket.py", line 295, in read
data = self._sock.recv(recv_size)
error: (10054, 'Connection reset by peer')

3. File "D:\Python24\lib\socket.py", line 317, in readline
data = recv(1)
IOError: [Errno socket error] timed out

For 1 and 2, those are errors that aren't in the
exception hierarchy.  Looking at the C code for "socketmodule.c",
it's clear that "socket.error" doesn't inherit from any standard
exception class.  See, in "init_socket()":

     socket_error = PyErr_NewException("socket.error", NULL, NULL);

That first NULL should be some parent exception, maybe "IOError".
As it is, "socket.error" is outside the standard exception hierarchy.
That's not too good.

Case #3, IOError, should have been caught by this:

    except IOError, message:	# I/O error

But it wasn't.  The "IOError" fell through, was caught by the
next outer exception block, and was logged as a generic
error.

    I can't find where in the Python socket module an "IOError"
could be raised.  I would have expected "socket.timeout".

    Anyway, I need to know the full set of exceptions that can
be raised by sockets.  Thanks.

				John Nagle



More information about the Python-list mailing list