7327: clean up file open/lock/close code

In preparation for fixing the bug, discovered minor glitches
in the open/lock/close logic on error paths. Using the RAII syntax
for xml_file and removing redundant unlock-before-close.
This commit is contained in:
Vassilii Khachaturov 2013-12-26 18:45:18 +02:00
parent e4df103e39
commit dd2a5c4a35

View File

@ -162,7 +162,7 @@ class RecentFiles(object):
""" """
Saves the current GRAMPS RecentFiles collection to the associated file. Saves the current GRAMPS RecentFiles collection to the associated file.
""" """
xml_file = file(os.path.expanduser(GRAMPS_FILENAME),'w') with open(os.path.expanduser(GRAMPS_FILENAME), 'w') as xml_file:
if use_lock: if use_lock:
fcntl.lockf(xml_file,fcntl.LOCK_EX) fcntl.lockf(xml_file,fcntl.LOCK_EX)
xml_file.write("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n") xml_file.write("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n")
@ -178,9 +178,7 @@ class RecentFiles(object):
xml_file.write(' <Timestamp>%d</Timestamp>\n' % item.get_time()) xml_file.write(' <Timestamp>%d</Timestamp>\n' % item.get_time())
xml_file.write(' </RecentItem>\n') xml_file.write(' </RecentItem>\n')
xml_file.write('</RecentFiles>\n') xml_file.write('</RecentFiles>\n')
if use_lock: # all advisory locks on a file are released on close
fcntl.lockf(xml_file,fcntl.LOCK_UN)
xml_file.close()
#------------------------------------------------------------------------- #-------------------------------------------------------------------------
# #
@ -196,7 +194,7 @@ class RecentParser(object):
self.recent_files = [] self.recent_files = []
try: try:
xml_file = open(os.path.expanduser(GRAMPS_FILENAME)) with open(os.path.expanduser(GRAMPS_FILENAME)) as xml_file:
if use_lock: if use_lock:
fcntl.lockf(xml_file,fcntl.LOCK_SH) fcntl.lockf(xml_file,fcntl.LOCK_SH)
@ -205,10 +203,7 @@ class RecentParser(object):
p.EndElementHandler = self.endElement p.EndElementHandler = self.endElement
p.CharacterDataHandler = self.characters p.CharacterDataHandler = self.characters
p.ParseFile(xml_file) p.ParseFile(xml_file)
# all advisory locks on a file are released on close
if use_lock:
fcntl.lockf(xml_file,fcntl.LOCK_UN)
xml_file.close()
except: except:
pass pass