New submission from Tim Smith <t...@tzs.net>:

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.

----------
components: Windows
messages: 164764
nosy: tzs
priority: normal
severity: normal
status: open
title: tempfile.TemporaryFile and httplib incompatibility
type: behavior
versions: Python 2.7

_______________________________________
Python tracker <rep...@bugs.python.org>
<http://bugs.python.org/issue15267>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to