socket vs _socketobject

ts1 takeshi.sone at gmail.com
Mon Dec 3 11:12:45 EST 2007


[ Sorry if you see this message twice (or more), mail.python.org
seems  rejecting my posts.]

Hello,

I've found strangeness in socket.

Normal socket is wrapped by Python code in socket.py.

>>> socket.socket(socket.AF_INET, socket.SOCK_STREAM)
<socket._socketobject object at 0xb7d23a04>

However, socket created with fromfd() is not wrapped, raw socket
object
from socketmodule.c.

>>> socket.fromfd(0, socket.AF_INET, socket.SOCK_STREAM)
<socket object, fd=4, family=2, type=1, protocol=0>

Is this intended?
If so, why?

(0 is not really a socket, but it is irrelevant here, this is just an
example)

When makefile() of wrapped socket is called, it returns file-like
object written in Python. It supports timeout. This is good.
When makefile() of raw socket is called, it returns real file object,
which knows nothing about socket's timeout. If timeout was set on the
socket, calls to read() etc to the file can raise EAGAIN, since
O_NONBLOCK is inherited to the dup'ed fd and PyFile doesn't know about
it.
(Document states socket must be blocking mode when calling makefile
but nothing about timeout.)

I can workaround this problem by adding something like
    sock = socket.socket(_sock=sock)
but this is ugly.

Python versions:

>>> import sys
>>> sys.version
'2.5.1 (r251:54863, May  2 2007, 16:56:35) \n[GCC 4.1.2 (Ubuntu
4.1.2-0ubuntu4)]'
>>> sys.platform
'linux2'

Takeshi



More information about the Python-list mailing list