From 784ace189b8b881bbbeefad8936c105732e413d8 Mon Sep 17 00:00:00 2001 From: Kai Huuhko Date: Fri, 5 Sep 2014 08:14:29 +0300 Subject: [PATCH] Fix open() and pickle encoding for py3k --- epour/session.py | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/epour/session.py b/epour/session.py index 98d047f..982177d 100644 --- a/epour/session.py +++ b/epour/session.py @@ -19,6 +19,7 @@ # MA 02110-1301, USA. # +import sys import os import mimetypes import urllib @@ -169,12 +170,19 @@ class Session(lt.session): return try: - pkl_file = open(torrents_path, 'rb') + if sys.hexversion >= 0x030000F0: + pkl_file = open(torrents_path, 'rb', encoding="utf-8") + else: + pkl_file = open(torrents_path, 'rb') except IOError: self.log.warning("Could not open the list of torrents.") else: try: - torrents = cPickle.load(pkl_file) + if sys.hexversion >= 0x030000F0: + torrents = cPickle.load( + pkl_file, fix_imports=True, encoding="utf-8") + else: + torrents = cPickle.load(pkl_file) except EOFError: self.log.exception("Opening the list of torrents failed.") else: @@ -245,8 +253,12 @@ class Session(lt.session): self.log.debug("Handle is invalid, skipping") path = os.path.join(save_data_path("epour"), "torrents") - with open(path, 'wb') as f: - cPickle.dump(self.torrents, f, protocol=2) + if sys.hexversion >= 0x030000F0: + with open(path, 'wb', encoding="utf-8") as f: + cPickle.dump(self.torrents, f, protocol=2) + else: + with open(path, 'wb') as f: + cPickle.dump(self.torrents, f, protocol=2) self.log.debug("List of torrents saved.")