From d42ef55164cc59116a84f1e334ff637986579e1c Mon Sep 17 00:00:00 2001 From: pol2hi Date: Tue, 20 Jun 2017 13:31:48 +0200 Subject: [PATCH 1/2] fix open file handle if pickledb.load fails --- pickledb.py | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/pickledb.py b/pickledb.py index 3a15f0c..bc29525 100644 --- a/pickledb.py +++ b/pickledb.py @@ -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 = json.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''' From 8b9b3ac65c194ace5cc972748fc885c9564448c4 Mon Sep 17 00:00:00 2001 From: pol2hi Date: Tue, 20 Jun 2017 14:00:08 +0200 Subject: [PATCH 2/2] use correct simplejson instead of json library --- pickledb.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pickledb.py b/pickledb.py index bc29525..eda67f5 100644 --- a/pickledb.py +++ b/pickledb.py @@ -195,7 +195,7 @@ def _loaddb(self): '''Load or reload the json info from the file''' fh = open(self.loco, 'rb') try: - self.db = json.load(fh) + self.db = simplejson.load(fh) except Exception, reason: self.db={} #This assures that the file is not locked if json.load