[New-bugs-announce] [issue12327] in HTTPConnection the are len(body) and TypeError catch exception to detect if body is file like object, this hack do work with StringIO object

harobed report at bugs.python.org
Mon Jun 13 19:50:09 CEST 2011


New submission from harobed <stephane at harobed.org>:

Hi,

in httplib.HTTPConnection._send_request (Python 2.6)
in httplib.HTTPConnection._set_content_length (Python 2.7)
and http.client.HTTPConnection._set_content_length (Python 3.3)

there are something like that :

        try:
            thelen = str(len(body))
        except TypeError as 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!!")


If I put StringIO object in body and I do :

>>> len(body)
AttributeError: StringIO instance has no attribute '__len__'

I haven't TypeError.

I think we need to replace :

        try:
            thelen = str(len(body))
        except TypeError as te:

by :

        if hasattr(body, "read"):
           ... # it's file-like object
        else:
           ... # it's string object

What do you think about ?

----------
components: Library (Lib)
messages: 138264
nosy: harobed
priority: normal
severity: normal
status: open
title: in HTTPConnection the are len(body) and TypeError catch exception to detect if body is file like object, this hack do work with StringIO object
versions: Python 2.6, Python 2.7, Python 3.3

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue12327>
_______________________________________


More information about the New-bugs-announce mailing list