Skip to content
This repository was archived by the owner on Aug 20, 2022. It is now read-only.
This repository was archived by the owner on Aug 20, 2022. It is now read-only.

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

@gjednaszewski

Description

@gjednaszewski

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')

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions