[docs] socket.fileno() documentation

Vlado Potisk vlado at poti.sk
Thu Oct 10 19:27:35 CEST 2013


I might be wrong but in my opinion the socket library documentation is not 
clear enough regarding the behaviour of the fileno() method in a case of a 
failure.

In the Python 3.2 socket library documentation there is:
----
exception socket.error

    A subclass of IOError, this exception is raised for socket-related errors. 
-----
 socket.close()

    Close the socket. All future operations on the socket object will fail.
----
 socket.fileno()

    Return the socket’s file descriptor (a small integer).
----

Based on the information quoted above, I wrote a test if a socket is active or 
if it has been closed already:

try:
	sock.fileno()
except socket.error:
	return False
return True

But is doesn't work. I have found out that fileno() returns -1 on a closed 
socket. Replacing fileno() with e.g. getsockname() fixes the code.

It looks like a fileno's failure - unlike to getsockname's failure - is not 
expressed by raising an exception, but by returning -1. However this seems to 
be not documented and that's why I'm unsure if I may rely on this.



More information about the docs mailing list