Imgur / imgurpython

Official Imgur python client library (deprecated)
MIT License
546 stars 116 forks source link

upload_from_path does not close the file #45

Open brenol opened 9 years ago

brenol commented 9 years ago

as can be seen from the following snippet: https://github.com/Imgur/imgurpython/blob/534c2c8d346e7067b78036f7ece9a1d36a6be18e/imgurpython/client.py#L582-L596

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.

ueg1990 commented 9 years ago

:+1: