From 358d84e334c5afdcf4264bb372b9b88af23f7056 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tor=20Arne=20Vestb=C3=B8?= Date: Mon, 11 Apr 2016 16:37:34 +0200 Subject: [PATCH] Gracefully handle malformed cookiejars LoadError would only be raised if the cookiejar didn't contain the expected magic header. But a pickeled jar would potentially contain data that raised a UnicodeDecodeError, so we need account for both. --- pyicloud/base.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/pyicloud/base.py b/pyicloud/base.py index 3b9711b..fcf2982 100644 --- a/pyicloud/base.py +++ b/pyicloud/base.py @@ -151,9 +151,12 @@ class PyiCloudService(object): if os.path.exists(cookiejar_path): try: self.session.cookies.load() - except cookielib.LoadError: - # Most likely a pickled cookiejar from earlier versions - pass + logger.debug("Read cookies from %s", cookiejar_path) + except: + # Most likely a pickled cookiejar from earlier versions. + # The cookiejar will get replaced with a valid one after + # successful authentication. + logger.warning("Failed to read cookiejar %s", cookiejar_path) self.params = { 'clientBuildNumber': '14E45', @@ -191,6 +194,7 @@ class PyiCloudService(object): if not os.path.exists(self._cookie_directory): os.mkdir(self._cookie_directory) self.session.cookies.save() + logger.debug("Cookies saved to %s", self._get_cookiejar_path()) self.data = resp self.webservices = self.data['webservices']