
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

Alan Kennedy wrote:
I am writing to seek information about the socket.fileno() method, and opinions on how best to implement it on jython.
I would hope that the new i/o system will make it unnecessary to use fileno() in portable code. It's really a unix-specific thing.
If you only pass it to other things supported on that platform that use filenos, probably not. BTW, you can pass socket objects directly to select() anyway. I'd regard this as the current portable way to use sockets and select. The man page says that this works via the fileno() method, but it doesn't have to be implemented that way -- select() could be taught to recognise socket objects natively. -- Greg Ewing, Computer Science Dept, +--------------------------------------+ University of Canterbury, | Carpe post meridiem! | Christchurch, New Zealand | (I'm not a morning person.) | greg.ewing@canterbury.ac.nz +--------------------------------------+

On 5/23/07, Greg Ewing <greg.ewing@canterbury.ac.nz> wrote:
I want to emphasize this option. Passing the socket to select should be more portable than using fileno(). -- --Guido van Rossum (home page: http://www.python.org/~guido/)

Alan Kennedy wrote:
I am writing to seek information about the socket.fileno() method, and opinions on how best to implement it on jython.
I would hope that the new i/o system will make it unnecessary to use fileno() in portable code. It's really a unix-specific thing.
If you only pass it to other things supported on that platform that use filenos, probably not. BTW, you can pass socket objects directly to select() anyway. I'd regard this as the current portable way to use sockets and select. The man page says that this works via the fileno() method, but it doesn't have to be implemented that way -- select() could be taught to recognise socket objects natively. -- Greg Ewing, Computer Science Dept, +--------------------------------------+ University of Canterbury, | Carpe post meridiem! | Christchurch, New Zealand | (I'm not a morning person.) | greg.ewing@canterbury.ac.nz +--------------------------------------+

On 5/23/07, Greg Ewing <greg.ewing@canterbury.ac.nz> wrote:
I want to emphasize this option. Passing the socket to select should be more portable than using fileno(). -- --Guido van Rossum (home page: http://www.python.org/~guido/)
participants (3)
-
Alan Kennedy
-
Greg Ewing
-
Guido van Rossum