Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_is_dictionary_subclass_subclass(self):
self.assertTrue(util.is_dictionary_subclass(DictSubclass()))
def test_is_dictionary_subclass_dict(self):
self.assertFalse(util.is_dictionary_subclass({}))
deferred = {}
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 = k.__str__()
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)):
try:
if k == '__dict__':
setattr(instance, k, value)
else:
instance[k] = value
except TypeError:
# Immutable object, must be constructed in one shot
if k != '__dict__':
deferred[k] = value
self._namestack.pop()
continue
else:
setattr(instance, k, value)
# This instance has an instance variable named `k` that is
# currently a proxy and must be replaced
has_dict = hasattr(obj, '__dict__')
if self._mkref(obj):
if has_class and not util.is_repr(obj):
module, name = _getclassdetail(obj)
if self.unpicklable is True:
data[tags.OBJECT] = '%s.%s' % (module, name)
if util.is_repr(obj):
if self.unpicklable is True:
data[tags.REPR] = '%s/%s' % (obj.__class__.__module__,
repr(obj))
else:
data = unicode(obj)
return self._pop(data)
if util.is_dictionary_subclass(obj):
return self._pop(self._flatten_dict_obj(obj, data))
if util.is_noncomplex(obj):
return self._pop([self.flatten(v) for v in obj])
if has_dict:
return self._pop(self._flatten_dict_obj(obj.__dict__, data))
else:
# We've seen this object before so place an object
# reference tag in the data. This avoids infinite recursion
# when processing cyclical objects.
return self._pop(self._getref(obj))
return self._pop(data)
# else, what else? (methods, functions, old style classes...)
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] = '{name}/{name}'.format(name=obj.__name__)
else:
data = compat.ustr(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_iterator(obj):
# force list in python 3
data[tags.ITERATOR] = list(
map(self._flatten, islice(obj, self._max_iter)))
return data
if has_dict:
# Support objects that subclasses list and set
if util.is_sequence_subclass(obj):
return self._flatten_sequence_obj(obj, data)
if self.unpicklable is True:
data[tags.REPR] = '%s/%s' % (obj.__name__,
obj.__name__)
else:
data = unicode(obj)
return data
if util.is_repr(obj):
if self.unpicklable is True:
data[tags.REPR] = '%s/%s' % (obj.__class__.__module__,
repr(obj))
else:
data = unicode(obj)
return data
if util.is_dictionary_subclass(obj):
return self._flatten_dict_obj(obj, data)
if util.is_noncomplex(obj):
return [self.flatten(v) for v in obj]
if has_dict:
# Support objects that subclasses list and set
if util.is_collection_subclass(obj):
return self._flatten_collection_obj(obj, data)
# Support objects with __getstate__(); this ensures that
# both __setstate__() and __getstate__() are implemented
if has_getstate_support:
data[tags.STATE] = self.flatten(obj.__getstate__())
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
self._namestack.append(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 type(value) is _Proxy:
self._proxies.append((instance, k, value, method))
# step out
self._namestack.pop()
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))
# step out
self._namestack.pop()
# 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
if reduce_val and not isinstance(reduce_val, (str, unicode)):
# at this point, reduce_val should be some kind of iterable
# 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
if reduce_val and not isinstance(reduce_val, (str, unicode)):
# at this point, reduce_val should be some kind of iterable