From d2066537c789ff3e4ffd4df5f096625cda0f6fd2 Mon Sep 17 00:00:00 2001 From: Josip Date: Sat, 5 Apr 2014 01:45:36 +0200 Subject: [PATCH] fix recentfiles.py to work for both py2/py3 --- gramps/gen/recentfiles.py | 40 +++++++++++++++++++++------------------ 1 file changed, 22 insertions(+), 18 deletions(-) diff --git a/gramps/gen/recentfiles.py b/gramps/gen/recentfiles.py index afc2e880f..ddaeec57b 100644 --- a/gramps/gen/recentfiles.py +++ b/gramps/gen/recentfiles.py @@ -184,23 +184,27 @@ class RecentFiles(object): """ Saves the current Gramps RecentFiles collection to the associated file. """ - with io.open(os.path.expanduser(GRAMPS_FILENAME), 'w', encoding='utf8') as xml_file: - if use_lock: - fcntl.lockf(xml_file,fcntl.LOCK_EX) - xml_file.write("\n") - xml_file.write('\n') - index = 0 - for item in self.gramps_recent_files: - index += 1 - if index > MAX_GRAMPS_ITEMS: - break - xml_file.write(' \n') - xml_file.write(' \n' % item.get_path()) - xml_file.write(' \n' % item.get_name()) - xml_file.write(' %d\n' % item.get_time()) - xml_file.write(' \n') - xml_file.write('\n') - # all advisory locks on a file are released on close + if sys.version_info[0] < 3: + xml_file = open(os.path.expanduser(GRAMPS_FILENAME), 'w') + else: + xml_file = open(os.path.expanduser(GRAMPS_FILENAME), 'w', encoding='utf8') + if use_lock: + fcntl.lockf(xml_file,fcntl.LOCK_EX) + xml_file.write("\n") + xml_file.write('\n') + index = 0 + for item in self.gramps_recent_files: + index += 1 + if index > MAX_GRAMPS_ITEMS: + break + xml_file.write(' \n') + xml_file.write(' \n' % item.get_path()) + xml_file.write(' \n' % item.get_name()) + xml_file.write(' %d\n' % item.get_time()) + xml_file.write(' \n') + xml_file.write('\n') + xml_file.close() + # all advisory locks on a file are released on close #------------------------------------------------------------------------- # @@ -222,7 +226,7 @@ class RecentParser(object): try: # Python3's expat wants bytes, Python2's wants a string. fmode = "r" if sys.version_info[0] < 3 else "rb" - with io.open(fname, fmode) as xml_file: + with open(fname, fmode) as xml_file: if use_lock: fcntl.lockf(xml_file,fcntl.LOCK_SH)