From: Alan K. <jyt...@xh...> - 2007-11-05 12:28:29
|
[Mehendran] > I need to access fileno(file descriptor number) of a file. But that function is > not available in jython. Yes it is available; it's just not returning what you (perhaps mistakenly) expect. Jython 2.2.1 on java1.4.2_15 Type "copyright", "credits" or "license" for more information. >>> import socket >>> s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) >>> s.connect( ('localhost', 80) ) >>> s.fileno() java.nio.channels.SocketChannel[connected local=/127.0.0.1:1622 remote=localhost/127.0.0.1:80] >>> s.getchannel() java.nio.channels.SocketChannel[connected local=/127.0.0.1:1622 remote=localhost/127.0.0.1:80] >>> Both the socket.fileno() method and the jython-specific socket.getchannel() method return a java specific java.nio.channels.SocketChannel object, which is the only thing that it is possible to return on the java platform. [Mehendran] > Is there any specific reason behind that that is omitted currently? 1. It is not omitted 2. Jython cannot return a numeric file-descriptor because java has no concept of numeric file-descriptors; numeric file descriptors are a unix-specific thing (but also a paradigm that has been made available in most C implementations on other OSes); hence cpython, because it's written in C, supports them on most platforms. Java instead has its own concept, called Channels, which are used for several purposes, with the most obvious being multiplexing. I could have implemented a file-descriptor table which stored these java.nio.channels.*Channels in an array, which would then be indexed by an integer. (Which is exactly what C-language file-descriptors are: an index into an array of struct FILE*). But my opinion of this approach/hack was swayed by the following conversation with Guido on python-dev. http://www.mail-archive.com/pyt...@py.../msg23202.html What do you need to use the result of fileno() for? If it is for multiplexing, then simply pass the socket object directly to the select call; the standard python select API supports passing the socket directly. In fact, this is the only portable way to do it in python; any other mechanism is not portable between python implementations. If you need to use socket.fileno() for some other purpose, then you'll have to explain that purpose, so we can recommend the proper way to do it. I thought I had documented this on the wiki documentation, but on checking I see that I did not. http://wiki.python.org/jython/NewSocketModule I will update the wiki docs when I get the time; meantime, if anyone else wishes to update the wiki page, please feel free. Regards, Alan. |