Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
mod = __import__(name)
except ImportError:
return False
# Handle submodules, e.g. django.utils.simplejson
try:
for attr in name.split('.')[1:]:
mod = getattr(mod, attr)
except AttributeError:
return False
if (not self._store(self._encoders, name, mod, dumps) or
not self._store(self._decoders, name, mod, loads)):
return False
if isinstance(loads_exc, (str, unicode)):
# This backend's decoder exception is part of the backend
if not self._store(self._decoder_exceptions, name, mod, loads_exc):
return False
else:
# simplejson uses ValueError
self._decoder_exceptions[name] = loads_exc
# Setup the default args and kwargs for this encoder/decoder
self._encoder_options[name] = ([], {})
self._decoder_options[name] = ([], {})
# Add this backend to the list of candidate backends
self._backend_names.append(name)
# Indicate that we successfully loaded a JSON backend
self._verified = True
# we ignore those
pass
if has_reduce and not reduce_val:
try:
reduce_val = obj.__reduce__()
except TypeError:
# A lot of builtin types have a reduce which just raises a TypeError
# we ignore those
pass
if reduce_val:
try:
# At this stage, we only handle the case where __reduce__ returns a string
# other reduce functionality is implemented further down
if isinstance(reduce_val, (str, unicode)):
varpath = iter(reduce_val.split('.'))
# curmod will be transformed by the loop into the value to pickle
curmod = sys.modules[next(varpath)]
for modname in varpath:
curmod = getattr(curmod, modname)
# replace obj with value retrieved
return self._flatten(curmod)
except KeyError:
# well, we can't do anything with that, so we ignore it
pass
if has_getnewargs_ex:
data[tags.NEWARGSEX] = list(map(self._flatten, obj.__getnewargs_ex__()))
if has_getnewargs and not has_getnewargs_ex:
data[tags.NEWARGS] = self._flatten(obj.__getnewargs__())
def _restore_from_dict(self, obj, instance, ignorereserved=True):
restore_key = self._restore_key_fn()
method = _obj_setattr
for k, v in sorted(obj.items(), key=util.itemgetter):
# ignore the reserved attribute
if ignorereserved and k in tags.RESERVED:
continue
if isinstance(k, numeric_types):
str_k = unicode(k)
else:
str_k = k
self._namestack.append(str_k)
k = restore_key(k)
# step into the namespace
value = self._restore(v)
if (util.is_noncomplex(instance) or
util.is_dictionary_subclass(instance)):
instance[k] = value
else:
setattr(instance, k, value)
# This instance has an instance variable named `k` that is
# currently a proxy and must be replaced
if isinstance(value, _Proxy):
self._proxies.append((instance, k, value, method))
if not util.is_picklable(k, v):
return data
if self.keys:
if not isinstance(k, (str, unicode)) or k.startswith(tags.JSON_KEY):
k = self._escape_key(k)
else:
if k is None:
k = 'null' # for compatibility with common json encoders
if self.numeric_keys and isinstance(k, numeric_types):
pass
elif not isinstance(k, (str, unicode)):
try:
k = repr(k)
except:
k = unicode(k)
data[k] = self._flatten(v)
return data
if not util.is_picklable(k, v):
return data
if self.keys:
if not isinstance(k, (str, unicode)) or k.startswith(tags.JSON_KEY):
k = self._escape_key(k)
else:
if k is None:
k = 'null' # for compatibility with common json encoders
if self.numeric_keys and isinstance(k, numeric_types):
pass
elif not isinstance(k, (str, unicode)):
try:
k = repr(k)
except:
k = unicode(k)
data[k] = self._flatten(v)
return data
try:
state = obj.__getstate__()
except TypeError:
# Has getstate but it cannot be called, e.g. file descriptors
# in Python3
self._pickle_warning(obj)
return None
else:
return self._getstate(state, data)
if util.is_module(obj):
if self.unpicklable:
data[tags.REPR] = '%s/%s' % (obj.__name__,
obj.__name__)
else:
data = unicode(obj)
return data
if util.is_dictionary_subclass(obj):
self._flatten_dict_obj(obj, data)
return data
if util.is_sequence_subclass(obj):
return self._flatten_sequence_obj(obj, data)
if util.is_noncomplex(obj):
return [self._flatten(v) for v in obj]
if util.is_iterator(obj):
# force list in python 3
data[tags.ITERATOR] = list(map(self._flatten, islice(obj, self._max_iter)))
return data
def _restore_from_dict(self, obj, instance, ignorereserved=True):
restore_key = self._restore_key_fn()
method = _obj_setattr
for k, v in sorted(obj.items(), key=util.itemgetter):
# ignore the reserved attribute
if ignorereserved and k in tags.RESERVED:
continue
if isinstance(k, numeric_types):
str_k = unicode(k)
else:
str_k = k
self._namestack.append(str_k)
k = restore_key(k)
# step into the namespace
value = self._restore(v)
if (util.is_noncomplex(instance) or
util.is_dictionary_subclass(instance)):
instance[k] = value
else:
setattr(instance, k, value)
# This instance has an instance variable named `k` that is
# currently a proxy and must be replaced
if isinstance(value, _Proxy):
self._proxies.append((instance, k, value, method))
def _restore_dict(self, obj):
data = {}
restore_key = self._restore_key_fn()
for k, v in sorted(obj.items(), key=util.itemgetter):
if isinstance(k, numeric_types):
str_k = unicode(k)
else:
str_k = k
self._namestack.append(str_k)
k = restore_key(k)
data[k] = self._restore(v)
self._namestack.pop()
return data
def itemgetter(obj, getter=operator.itemgetter(0)):
return unicode(getter(obj))