From 42de599bd0be7562aff6433d50eb51d4314f50d3 Mon Sep 17 00:00:00 2001 From: John Ralls Date: Thu, 17 Jan 2013 19:48:32 +0000 Subject: [PATCH] GrampsLocale: Fix file encoding error in recentfiles Expat wants a binary file, not a string file. svn: r21152 --- gramps/gen/recentfiles.py | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/gramps/gen/recentfiles.py b/gramps/gen/recentfiles.py index e26752424..8903914c4 100644 --- a/gramps/gen/recentfiles.py +++ b/gramps/gen/recentfiles.py @@ -30,6 +30,8 @@ import os import time import io +import sys +import logging from xml.parsers.expat import ParserCreate try: @@ -182,7 +184,8 @@ class RecentFiles(object): """ Saves the current GRAMPS RecentFiles collection to the associated file. """ - xml_file = io.open(os.path.expanduser(GRAMPS_FILENAME),'w', encoding="utf-8") + xml_file = open(os.path.expanduser(GRAMPS_FILENAME),'w') + if use_lock: fcntl.lockf(xml_file,fcntl.LOCK_EX) xml_file.write("\n") @@ -215,9 +218,13 @@ class RecentParser(object): def __init__(self): self.recent_files = [] +#Python3's expat wants bytes, Python2's wants a string. try: - xml_file = io.open(os.path.expanduser(GRAMPS_FILENAME), "r", - encoding = 'utf-8') + if sys.version_info[0] < 3: + xml_file = open(os.path.expanduser(GRAMPS_FILENAME), "r") + else: + xml_file = open(os.path.expanduser(GRAMPS_FILENAME), "rb") + if use_lock: fcntl.lockf(xml_file,fcntl.LOCK_SH) @@ -230,7 +237,8 @@ class RecentParser(object): if use_lock: fcntl.lockf(xml_file,fcntl.LOCK_UN) xml_file.close() - except: + except Exception as err: + logging.error("Recent file parse error %s", str(err)) if xml_file: xml_file.close()