From b3edd2fa5fcfe2716da7cf203f1bb8d61d037583 Mon Sep 17 00:00:00 2001 From: John Ralls Date: Mon, 21 Apr 2014 14:07:21 -0700 Subject: [PATCH] Make recentfiles.do_save exception safe. --- gramps/gen/recentfiles.py | 40 +++++++++++++++++++-------------------- 1 file changed, 20 insertions(+), 20 deletions(-) diff --git a/gramps/gen/recentfiles.py b/gramps/gen/recentfiles.py index ddaeec57b..645fef1bc 100644 --- a/gramps/gen/recentfiles.py +++ b/gramps/gen/recentfiles.py @@ -184,26 +184,26 @@ class RecentFiles(object): """ Saves the current Gramps RecentFiles collection to the associated file. """ - 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() + with (open(os.path.expanduser(GRAMPS_FILENAME), 'w') + if sys.version_info[0] < 3 else + 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 #-------------------------------------------------------------------------