softlayer-object-storage-python icon indicating copy to clipboard operation
softlayer-object-storage-python copied to clipboard

storage_object.send() doesn't check for None in name attribute

Open gjednaszewski opened this issue 10 years ago • 0 comments

Often we use a SpooledTemporaryFile to write data to SL object storage. Previously we were using Python 2.7.2 and this worked fine. After updating to Python 2.7.9 we were running into an issue with this scenario where we would get an exception:

TypeError: expected string or buffer
File "python2.7/site-packages/object_storage/storage_object.py", line 349, in send
File "python2.7/mimetypes.py", line 291, in guess_type
File "python2.7/mimetypes.py", line 114, in guess_type
File "python2.7/urllib.py", line 1080, in splittype

It turns out in Python 2.7.4 a name attribute was added to SpooledTemporaryFile which has a value of None. (See http://bugs.python.org/issue10355) That breaks this code in send():

    if not content_type:
        _type = None
        if hasattr(data, 'name'):
            _type = mimetypes.guess_type(data.name)[0]
        content_type = (_type or
                        mimetypes.guess_type(self.name)[0] or
                        'application/octet-stream')

The issue can be fixed by changing the hasattr line:

    if not content_type:
        _type = None
        if hasattr(data, 'name') and data.name:
            _type = mimetypes.guess_type(data.name)[0]
        content_type = (_type or
                        mimetypes.guess_type(self.name)[0] or
                        'application/octet-stream')

gjednaszewski avatar Feb 18 '15 16:02 gjednaszewski