Skip to content
This repository was archived by the owner on Oct 4, 2023. It is now read-only.
This repository was archived by the owner on Oct 4, 2023. It is now read-only.

upload_from_path does not close the file #45

@brenol

Description

@brenol

as can be seen from the following snippet:

def upload_from_path(self, path, config=None, anon=True):
if not config:
config = dict()
fd = open(path, 'rb')
contents = fd.read()
b64 = base64.b64encode(contents)
data = {
'image': b64,
'type': 'base64',
}
data.update({meta: config[meta] for meta in set(self.allowed_image_fields).intersection(config.keys())})
return self.make_request('POST', 'upload', data, anon)

The file needs to be closed after being opened. The code above can be easily changed into the following:

def upload_from_path(self, path, config=None, anon=True):
    if not config:
        config = dict()

    with open(path, 'rb') as fd:
        contents = fd.read()
    b64 = base64.b64encode(contents)

    data = {
        'image': b64,
        'type': 'base64',
    }

    data.update({meta: config[meta] for meta in set(self.allowed_image_fields).intersection(config.keys())})
    return self.make_request('POST', 'upload', data, anon)

And the issue will be fixed. The issue is that a BufferedReader is left open and never closed.

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