0
0
mirror of https://github.com/django/django.git synced 2024-12-01 15:42:04 +01:00

Refs #28575 -- Removed unnecessary code for model exception pickling.

Setting __qualname__ is sufficient for pickling of DoesNotExist and
and MultipleObjectsReturned to work correctly.
This commit is contained in:
Simon Charette 2017-10-13 10:29:34 -04:00 committed by Tim Graham
parent f2868f9739
commit 216eda103b

View File

@ -44,30 +44,18 @@ class Deferred:
DEFERRED = Deferred()
def subclass_exception(name, parents, module, attached_to=None):
def subclass_exception(name, bases, module, attached_to):
"""
Create exception subclass. Used by ModelBase below.
If 'attached_to' is supplied, the exception will be created in a way that
allows it to be pickled, assuming the returned exception class will be added
as an attribute to the 'attached_to' class.
The exception is created in a way that allows it to be pickled, assuming
that the returned exception class will be added as an attribute to the
'attached_to' class.
"""
class_dict = {'__module__': module}
if attached_to is not None:
def __reduce__(self):
# Exceptions are special - they've got state that isn't
# in self.__dict__. We assume it is all in self.args.
return (unpickle_inner_exception, (attached_to, name), self.args)
def __setstate__(self, args):
self.args = args
class_dict['__reduce__'] = __reduce__
class_dict['__setstate__'] = __setstate__
if attached_to:
class_dict['__qualname__'] = '%s.%s' % (attached_to.__qualname__, name)
return type(name, parents, class_dict)
return type(name, bases, {
'__module__': module,
'__qualname__': '%s.%s' % (attached_to.__qualname__, name),
})
class ModelBase(type):
@ -1726,9 +1714,3 @@ def model_unpickle(model_id):
model_unpickle.__safe_for_unpickle__ = True
def unpickle_inner_exception(klass, exception_name):
# Get the exception class from the class it is attached to:
exception = getattr(klass, exception_name)
return exception.__new__(exception)