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:
parent
e4df103e39
commit
dd2a5c4a35
@ -162,7 +162,7 @@ class RecentFiles(object):
|
||||
"""
|
||||
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:
|
||||
fcntl.lockf(xml_file,fcntl.LOCK_EX)
|
||||
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(' </RecentItem>\n')
|
||||
xml_file.write('</RecentFiles>\n')
|
||||
if use_lock:
|
||||
fcntl.lockf(xml_file,fcntl.LOCK_UN)
|
||||
xml_file.close()
|
||||
# all advisory locks on a file are released on close
|
||||
|
||||
#-------------------------------------------------------------------------
|
||||
#
|
||||
@ -196,7 +194,7 @@ class RecentParser(object):
|
||||
self.recent_files = []
|
||||
|
||||
try:
|
||||
xml_file = open(os.path.expanduser(GRAMPS_FILENAME))
|
||||
with open(os.path.expanduser(GRAMPS_FILENAME)) as xml_file:
|
||||
if use_lock:
|
||||
fcntl.lockf(xml_file,fcntl.LOCK_SH)
|
||||
|
||||
@ -205,10 +203,7 @@ class RecentParser(object):
|
||||
p.EndElementHandler = self.endElement
|
||||
p.CharacterDataHandler = self.characters
|
||||
p.ParseFile(xml_file)
|
||||
|
||||
if use_lock:
|
||||
fcntl.lockf(xml_file,fcntl.LOCK_UN)
|
||||
xml_file.close()
|
||||
# all advisory locks on a file are released on close
|
||||
except:
|
||||
pass
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user