◐ Shell
clean mode source ↗

Message 138264 - Python tracker

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 ?