Fix open() and pickle encoding for py3k

This commit is contained in:
Kai Huuhko 2014-09-05 08:14:29 +03:00
parent 8dae070215
commit 784ace189b
1 changed files with 16 additions and 4 deletions

View File

@ -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.")