msg60827 - (view) |
Author: Kevin Dwyer (kevindication) |
Date: 2005-10-16 14:41 |
In python2.3, the following code works. In python2.4 it fails with an AttributeError: >>> import urllib2 >>> request = urllib2.Request("http://pheared.net") >>> opener = urllib2.build_opener() >>> r = opener.open(request) >>> r.fileno() Traceback (most recent call last): File "", line 1, in ? File "/usr/lib/python2.4/socket.py", line 246, in fileno return self._sock.fileno() AttributeError: HTTPResponse instance has no attribute 'fileno' Without a fileno it's hard to do things like select. |
|
|
msg60828 - (view) |
Author: Kevin Dwyer (kevindication) |
Date: 2005-10-16 15:08 |
Logged In: YES user_id=285914 I think the problem is at line 1010 in urllib2.py. --- urllib2.py-orig 2005-10-16 11:04:30.000000000 -0400 +++ urllib2.py 2005-10-16 11:05:02.000000000 -0400 @@ -1007,7 +1007,7 @@ # out of socket._fileobject() and into a base class. r.recv = r.read - fp = socket._fileobject(r) + fp = socket._fileobject(r.fp) resp = addinfourl(fp, r.msg, req.get_full_url()) resp.code = r.status |
|
|
msg60829 - (view) |
Author: Kevin Dwyer (kevindication) |
Date: 2005-10-16 15:23 |
Logged In: YES user_id=285914 Actually that's not entirely correct. The fix is probably more like the other hack in there: --- urllib2.py-orig 2005-10-16 11:19:34.000000000 -0400 +++ urllib2.py 2005-10-16 11:22:30.000000000 -0400 @@ -1007,6 +1007,7 @@ # out of socket._fileobject() and into a base class. r.recv = r.read + r.fileno = r.fp.fileno fp = socket._fileobject(r) resp = addinfourl(fp, r.msg, req.get_full_url()) |
|
|
msg81437 - (view) |
Author: Daniel Diniz (ajaksu2) *  |
Date: 2009-02-09 04:57 |
Besides being outdated, the OP shows it's trivial to access the fileno: "r.fileno = r.fp.fileno". I suggest closing. |
|
|
msg81579 - (view) |
Author: Georg Brandl (georg.brandl) *  |
Date: 2009-02-10 18:44 |
Well, easy access is not the point. If the object is supposed to have the file-like interface, and can provide a fileno(), it should. |
|
|
msg81585 - (view) |
Author: Brett Cannon (brett.cannon) *  |
Date: 2009-02-10 19:34 |
Georg is right and this has been fixed apparently in 3.0, leaving 2.7 and older broken. There are two possible solutions to this. One is to change socket._fileobject.fileno() to simply try self._sock.fp.fileno() if self._sock.fileno() does not exist. The other option is to add a __getattr__ to httplib.HTTPResponse to redirect to self.fp. Anyone have an opinion? |
|
|
msg81588 - (view) |
Author: Brett Cannon (brett.cannon) *  |
Date: 2009-02-10 19:44 |
Another option is to change urllib.addinfourl to look for fileno() on an HTTPResponse object. |
|
|
msg81644 - (view) |
Author: Daniel Diniz (ajaksu2) *  |
Date: 2009-02-11 14:18 |
OK, I'll work on a test + patch. |
|
|
msg81651 - (view) |
Author: Daniel Diniz (ajaksu2) *  |
Date: 2009-02-11 16:56 |
IMHO, using a fileno property looks better than __getattr__. Setting an attribute in init works too, unless fp changes during the object life (then 3.x is broken IIUC). It works OK as a property of either urllib.addinfourl or of httplib.HTTPResponse (socket would work, but is this broken there or a higher layer). Tests and fixes for both solutions attached. As soon as one is chosen, some docs on fileno would be nice, even for 3.x. This (current) test in test_urllibnet.py passes, but I don't see how to improve it, assuming it should fail/detect this bug (maybe self.urlopen isn't testing urllib.urlopen correctly?): def test_fileno(self): if (sys.platform in ('win32',) or not hasattr(os, 'fdopen')): # On Windows, socket handles are not file descriptors; this # test can't pass on Windows. return # Make sure fd returned by fileno is valid. open_url = self.urlopen("http://www.python.org/") fd = open_url.fileno() FILE = os.fdopen(fd) |
|
|
msg81653 - (view) |
Author: Daniel Diniz (ajaksu2) *  |
Date: 2009-02-11 17:16 |
Hmm, always run all the tests :) fileno is set in test_urllib2. New patches. |
|
|
msg81663 - (view) |
Author: Daniel Diniz (ajaksu2) *  |
Date: 2009-02-11 20:18 |
The urllib patch breaks test_pyclbr because test_pyclbr can't handle properties. I think the test is wrong here, hence the hack attached... otherwise, let's go with httplib. |
|
|
msg110437 - (view) |
Author: Mark Lawrence (BreamoreBoy) * |
Date: 2010-07-16 13:28 |
These patches are small and marked as easy, can they be accepted? |
|
|
msg117013 - (view) |
Author: Senthil Kumaran (orsenthil) *  |
Date: 2010-09-21 01:42 |
Fix committed in revision 84932. A property based fileno was not required, because it is hardly set, it is always often a read-only attribute. Also, py3k behavior is same as the current fix. |
|
|
msg117018 - (view) |
Author: Senthil Kumaran (orsenthil) *  |
Date: 2010-09-21 02:11 |
Documented it in r84933, r84934 and r84935 |
|
|