From: Harmon Date: Wed, 19 May 2021 15:41:59 +0000 (-0500) Subject: Properly close file in API.update_with_media X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=6c81a7fff8d3e9b43e0d6d85688390767a14f4bc;p=tweepy.git Properly close file in API.update_with_media --- diff --git a/tweepy/api.py b/tweepy/api.py index d82df20..856e0ab 100644 --- a/tweepy/api.py +++ b/tweepy/api.py @@ -2,6 +2,7 @@ # Copyright 2009-2021 Joshua Roesslein # See LICENSE for details. +import contextlib import functools import imghdr import logging @@ -940,16 +941,17 @@ class API: :reference: https://developer.twitter.com/en/docs/twitter-api/v1/tweets/post-and-engage/api-reference/post-statuses-update_with_media """ - if file is not None: - files = {'media[]': (filename, file)} - else: - files = {'media[]': open(filename, 'rb')} - return self.request( - 'POST', 'statuses/update_with_media', endpoint_parameters=( - 'status', 'possibly_sensitive', 'in_reply_to_status_id', - 'lat', 'long', 'place_id', 'display_coordinates' - ), status=status, files=files, **kwargs - ) + with contextlib.ExitStack() as stack: + if file is not None: + files = {'media[]': (filename, file)} + else: + files = {'media[]': stack.enter_context(open(filename, 'rb'))} + return self.request( + 'POST', 'statuses/update_with_media', endpoint_parameters=( + 'status', 'possibly_sensitive', 'in_reply_to_status_id', + 'lat', 'long', 'place_id', 'display_coordinates' + ), status=status, files=files, **kwargs + ) # Search Tweets