From a3fe1d1dc645aeb12ee99baf957e6b1a6d154d75 Mon Sep 17 00:00:00 2001 From: df Date: Mon, 11 Oct 2021 13:10:50 +0100 Subject: [PATCH] [utils] Avoid scrubbing supplied cookie file on failed update --- youtube_dl/utils.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/youtube_dl/utils.py b/youtube_dl/utils.py index e059f4a72..653fe9fcd 100644 --- a/youtube_dl/utils.py +++ b/youtube_dl/utils.py @@ -2953,7 +2953,8 @@ class YoutubeDLCookieJar(compat_cookiejar.MozillaCookieJar): if cookie.expires is None: cookie.expires = 0 - with io.open(filename, 'w', encoding='utf-8') as f: + with io.open(filename, 'r+', encoding='utf-8') as f: + assert 0 == f.seek(0) f.write(self._HEADER) now = time.time() for cookie in self: @@ -2985,6 +2986,7 @@ class YoutubeDLCookieJar(compat_cookiejar.MozillaCookieJar): f.write( '\t'.join([cookie.domain, initial_dot, cookie.path, secure, expires, name, value]) + '\n') + assert f.tell() == f.truncate() def load(self, filename=None, ignore_discard=False, ignore_expires=False): """Load cookies from a file."""