storage: deepcopy and sanitize json
This commit is contained in:
parent
592a403fa7
commit
65d0560475
@ -25,6 +25,7 @@ import random
|
|||||||
import time
|
import time
|
||||||
import math
|
import math
|
||||||
import json
|
import json
|
||||||
|
import copy
|
||||||
|
|
||||||
from util import print_msg, print_error
|
from util import print_msg, print_error
|
||||||
|
|
||||||
@ -92,13 +93,20 @@ class WalletStorage(object):
|
|||||||
except IOError:
|
except IOError:
|
||||||
return
|
return
|
||||||
try:
|
try:
|
||||||
d = json.loads(data)
|
self.data = json.loads(data)
|
||||||
except:
|
except:
|
||||||
try:
|
try:
|
||||||
d = ast.literal_eval(data) #parse raw data from reading wallet file
|
d = ast.literal_eval(data) #parse raw data from reading wallet file
|
||||||
except Exception:
|
except Exception:
|
||||||
raise IOError("Cannot read wallet file.")
|
raise IOError("Cannot read wallet file.")
|
||||||
self.data = d
|
self.data = {}
|
||||||
|
for k, v in d.items():
|
||||||
|
try:
|
||||||
|
json.dumps(key)
|
||||||
|
json.dumps(value)
|
||||||
|
except:
|
||||||
|
continue
|
||||||
|
self.data[key] = value
|
||||||
self.file_exists = True
|
self.file_exists = True
|
||||||
|
|
||||||
def get(self, key, default=None):
|
def get(self, key, default=None):
|
||||||
@ -106,13 +114,20 @@ class WalletStorage(object):
|
|||||||
v = self.data.get(key)
|
v = self.data.get(key)
|
||||||
if v is None:
|
if v is None:
|
||||||
v = default
|
v = default
|
||||||
|
else:
|
||||||
|
v = copy.deepcopy(v)
|
||||||
return v
|
return v
|
||||||
|
|
||||||
def put(self, key, value, save = True):
|
def put(self, key, value, save = True):
|
||||||
|
try:
|
||||||
|
json.dumps(key)
|
||||||
|
json.dumps(value)
|
||||||
|
except:
|
||||||
|
print_error("json error: cannot save", key)
|
||||||
|
return
|
||||||
with self.lock:
|
with self.lock:
|
||||||
if value is not None:
|
if value is not None:
|
||||||
self.data[key] = value
|
self.data[key] = copy.deepcopy(value)
|
||||||
elif key in self.data:
|
elif key in self.data:
|
||||||
self.data.pop(key)
|
self.data.pop(key)
|
||||||
if save:
|
if save:
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user