Skip to content

Commit 6f267e2

Browse files
committed
fix: MediaFileUpload error if file does not exist
1 parent b2ea122 commit 6f267e2

File tree

1 file changed

+5
-3
lines changed

1 file changed

+5
-3
lines changed

googleapiclient/http.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -573,20 +573,22 @@ def __init__(
573573
resumable: bool, True if this is a resumable upload. False means upload
574574
in a single request.
575575
"""
576+
self._fd = None
576577
self._filename = filename
577-
fd = open(self._filename, "rb")
578+
self._fd = open(self._filename, "rb")
578579
if mimetype is None:
579580
# No mimetype provided, make a guess.
580581
mimetype, _ = mimetypes.guess_type(filename)
581582
if mimetype is None:
582583
# Guess failed, use octet-stream.
583584
mimetype = "application/octet-stream"
584585
super(MediaFileUpload, self).__init__(
585-
fd, mimetype, chunksize=chunksize, resumable=resumable
586+
self._fd, mimetype, chunksize=chunksize, resumable=resumable
586587
)
587588

588589
def __del__(self):
589-
self._fd.close()
590+
if self._fd:
591+
self._fd.close()
590592

591593
def to_json(self):
592594
"""Creating a JSON representation of an instance of MediaFileUpload.

0 commit comments

Comments
 (0)