This commit is contained in:
Miroslav Stampar
2016-10-29 00:13:04 +02:00
parent f4e36fc049
commit 10097dd124
4 changed files with 19 additions and 14 deletions

21
lib/core/convert.py Normal file → Executable file
View File

@@ -6,9 +6,11 @@ See the file 'doc/COPYING' for copying permission
"""
try:
import cPickle as pickle
import cPickle as pickle
except:
import pickle
import pickle
finally:
import pickle as picklePy
import base64
import json
@@ -45,7 +47,7 @@ def base64pickle(value):
Serializes (with pickle) and encodes to Base64 format supplied (binary) value
>>> base64pickle('foobar')
'gAJVBmZvb2JhcnEALg=='
'gAJVBmZvb2JhcnEBLg=='
"""
retVal = None
@@ -64,11 +66,11 @@ def base64pickle(value):
return retVal
def base64unpickle(value):
def base64unpickle(value, unsafe=False):
"""
Decodes value from Base64 to plain format and deserializes (with pickle) its content
>>> base64unpickle('gAJVBmZvb2JhcnEALg==')
>>> base64unpickle('gAJVBmZvb2JhcnEBLg==')
'foobar'
"""
@@ -82,9 +84,12 @@ def base64unpickle(value):
self.load_reduce()
def loads(str):
file = StringIO.StringIO(str)
unpickler = pickle.Unpickler(file)
unpickler.dispatch[pickle.REDUCE] = _
f = StringIO.StringIO(str)
if unsafe:
unpickler = picklePy.Unpickler(f)
unpickler.dispatch[pickle.REDUCE] = _
else:
unpickler = pickle.Unpickler(f)
return unpickler.load()
try: