Issue15267
Created on 2012-07-07 00:24 by tzs, last changed 2013-05-15 15:12 by lemanyk1.
| Files | ||||
|---|---|---|---|---|
| File name | Uploaded | Description | Edit | |
| issue15267.patch | ishimoto, 2012-07-30 08:29 | review | ||
| Messages (7) | |||
|---|---|---|---|
| msg164764 - (view) | Author: Tim Smith (tzs) | Date: 2012-07-07 00:24 | |
In httplib.py, there is this code to try to get the body length:
def _set_content_length(self, body):
# Set the content-length based on the body.
thelen = None
try:
thelen = str(len(body))
except TypeError, te:
# If this is a file-like object, try to
# fstat its file descriptor
try:
thelen = str(os.fstat(body.fileno()).st_size)
except (AttributeError, OSError):
# Don't send a length if this failed
if self.debuglevel > 0: print "Cannot stat!!"
However, if the body is a temporary file created by tempfile.TemporaryFile(), the len(body) in the first try throws an AttributeError, not a TypeError, on Windows and so it is not caught and unhappiness ensues. It is fine on Macintosh, and I would presume also on Linux.
Windows behaves different because on the other systems, TemporaryFile() returns an actual file object, and len() on a file throws TypeError. On Windows, TemporaryFile() returns an object that wraps the underlying file, and calling len() on that objects invokes this from tempfile.py (around line 371):
def __getattr__(self, name):
# Attribute lookups are delegated to the underlying file
# and cached for non-numeric results
# (i.e. methods are cached, closed and friends are not)
file = self.__dict__['file']
a = getattr(file, name)
if not issubclass(type(a), type(0)):
setattr(self, name, a)
return a
Since the underlying file does not have a __len__ method, the getattr fails and throws AttributeError.
I'm sorry I'm not submitting a patch, but I do not know enough about the design of these two libraries to know whether the correct fix is for httplib to be more broad in the exceptions that cause it to check for a file when len() fails, or if the object returned by TemporaryFile() should be more closely mimicking a true file object and so should be made to throw TypeError when someone tries to use len() on it.
|
|||
| msg165074 - (view) | Author: Tim Golden (tim.golden) ![]() |
Date: 2012-07-09 10:31 | |
Could you create a failing test, please, Tim S? |
|||
| msg165076 - (view) | Author: Ramchandra Apte (Ramchandra Apte) * | Date: 2012-07-09 11:36 | |
BTW, type(0) should be replaced with int in the code. |
|||
| msg165093 - (view) | Author: Tim Smith (tzs) | Date: 2012-07-09 16:19 | |
Here is a program that demonstrates the problem:
import httplib
import tempfile
f = tempfile.TemporaryFile()
f.write("Hello, Temporary File!")
f.seek(0)
c = httplib.HTTPConnection('bugs.python.org')
c.request('POST', '/', f, {'Content-type' : 'application/octet-stream'})
r = c.getresponse()
print r.status, r.reason
The expected output is "200 OK", and that's what happens on Mac and almost certainly on Linux. On Windows, this is the result:
Traceback (most recent call last):
File "bad.py", line 9, in <module>
c.request('POST', '/', f, {'Content-type' : 'application/octet
File "C:\Python27\lib\httplib.py", line 958, in request
self._send_request(method, url, body, headers)
File "C:\Python27\lib\httplib.py", line 989, in _send_request
self._set_content_length(body)
File "C:\Python27\lib\httplib.py", line 964, in _set_content_len
thelen = str(len(body))
File "C:\Python27\lib\tempfile.py", line 383, in __getattr__
a = getattr(file, name)
AttributeError: 'file' object has no attribute '__len__'
Changing the lines:
f = tempfile.TemporaryFile()
f.write("Hello, Temporary File!")
f.seek(0)
to:
f = open("temp.file", "w+")
f.write("Hello, Less Temporary File!")
f.seek(0)
makes it work on Windows.
|
|||
| msg165183 - (view) | Author: Tim Golden (tim.golden) ![]() |
Date: 2012-07-10 10:14 | |
I can confirm that this isn't a problem for 3.x. No-one's listed in the experts list for the httplib module but Senthil appears to be the de factor maintainer so I've added him to the call for his opinion. My feeling would be to do the simplest thing possible and add AttributeError to the list of trapped exceptions. |
|||
| msg166867 - (view) | Author: Atsuo Ishimoto (ishimoto) * | Date: 2012-07-30 08:29 | |
patch contains fix and test for 2.7. With this patch, AttibuteError is captured as Tim sujested. |
|||
| msg189285 - (view) | Author: (lemanyk1) | Date: 2013-05-15 15:12 | |
i think it is not good, len(fd) must raise AttributeError, not TypeError |
|||
| History | |||
|---|---|---|---|
| Date | User | Action | Args |
| 2013-05-15 15:12:24 | lemanyk1 | set | nosy:
+ lemanyk1 messages: + msg189285 |
| 2012-07-30 08:29:14 | ishimoto | set | files:
+ issue15267.patch nosy: + ishimoto messages: + msg166867 keywords: + patch |
| 2012-07-10 10:14:22 | tim.golden | set | nosy:
+ orsenthil messages: + msg165183 |
| 2012-07-09 16:19:53 | tzs | set | messages: + msg165093 |
| 2012-07-09 11:36:19 | Ramchandra Apte | set | nosy:
+ Ramchandra Apte messages: + msg165076 |
| 2012-07-09 10:31:52 | tim.golden | set | nosy:
+ tim.golden messages: + msg165074 |
| 2012-07-07 00:24:34 | tzs | create | |
