Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix open file handle if pickledb.load fails #29

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions pickledb.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,8 +192,19 @@ def deldb(self):
return True

def _loaddb(self):
'''Load or reload the json info from the file'''
self.db = simplejson.load(open(self.loco, 'rb'))
'''Load or reload the json info from the file'''
fh = open(self.loco, 'rb')
try:
self.db = simplejson.load(fh)
except Exception, reason:
self.db={}
#This assures that the file is not locked if json.load
#fails e.g. due to corrupt file..
#Clients of pickledb can then restore or delete the file.
fh.close()
raise Exception, reason
#no reason to keep the file handle open.
fh.close()

def _dumpdb(self, forced):
'''Write/save the json dump into the file'''
Expand Down