[Python-Dev] Return value from socket.fileno()
Alan Kennedy
python-dev at xhaus.com
Wed May 23 12:27:56 CEST 2007
Dear all,
I am writing to seek information about the socket.fileno() method, and
opinions on how best to implement it on jython.
On cpython, socket.fileno() returns a file descriptor, i.e. an integer
index into an array of file objects. This integer is then passed to
select.select and select.poll, to register interest in specified events
on the socket, i.e. readability, writability, connectedness, etc.
Importantly, it is possible to select and poll on a socket's file
descriptor immediately after the socket is created, e.g. before it is
connected and even before a non-blocking connect process has been started.
This is problematic on java, because java has different classes for
client and server sockets. Therefore, on jython, creation of the actual
socket implementation is deferred until the user has called a method
which commits the socket to being a client or server socket, e.g.
connect, listen, etc. This means that there is no meaningful
descriptor/channel that can be returned from fileno() until the nature
of the socket is determined.
Also, file descriptors have no meaning on java. Instead, java Selectors
select on java.nio.channels.SelectableChannel objects. But, ideally,
this should not matter: AFAICT, the return value from fileno should be
simply an opaque handle which has no purpose other than to be handed to
select and poll, to indicate interest in events on the associated socket.
I have been thinking that the simplest way to implement socket.fileno()
on jython is to return the actual socket object, i.e. return self. When
this object is passed to select/poll as a parameter, the select/poll
implementation would know to retrieve the underlying java
SelectableChannel, if it exists yet. If it does not yet exist, because
the socket is not yet commited to being a server or client socket, then
it is simply excluded from the select/poll operation.
The only problem with it is returning a non-integer from the fileno()
method; instead a socket object would be returned.
So the question I'm asking is: Does anyone know of existing cpython code
which relies on the return value from socket.fileno() being an integer?
Or code that would break if it were returned a socket instance instead
of an integer?
Thanks,
Alan.
P.S. If anyone is interested, a non-blocking sockets and select (and
soon asyncore) implementation is currently residing in the jython
sandbox. It is hoped that it will be included in jython 2.2rc1. More here
http://wiki.python.org/jython/NewSocketModule
More information about the Python-Dev
mailing list