#25389: SimpleLazyObject doesn't pickle correctly when wrapping a Model
--------------------------------+----------------------------
     Reporter:  benjaminjkraft  |      Owner:  benjaminjkraft
         Type:  Bug             |     Status:  new
    Component:  Uncategorized   |    Version:  1.8
     Severity:  Normal          |   Keywords:
 Triage Stage:  Unreviewed      |  Has patch:  0
Easy pickings:  0               |      UI/UX:  0
--------------------------------+----------------------------
 Given a `SimpleLazyObject` wrapping an instance of a Django model, if the
 `SimpleLazyObject` has not yet been initialized, pickling it will not
 correctly save the `_django_version` attribute added by
 
[https://github.com/django/django/commit/42736ac8e8c31137131714013951249a09e6e7d4
 42736ac8].  This happens because `SimpleLazyObject`'s overridden
 `__reduce_ex__` clobbers that of Model, and means that unpickling the
 pickled object will raise a warning.  An example is given
 [https://gist.github.com/benjaminjkraft/2d330e22a58681c5db5c#file-
 raises_warning-py here].  This is especially relevant given that
 `request.user` is a `SimpleLazyObject` wrapping a model instance, so this
 will happen when pickling an untouched `request.user`.

 Furthermore, pickling a `SimpleLazyObject` wrapping a class which does
 certain scary things to its `__dict__` and overrides `__reduce__` can
 cause an error while pickling.  An abstract example is given
 [https://gist.github.com/benjaminjkraft/2d330e22a58681c5db5c#file-
 raises_error-py here].  (I don't think that supporting this case is a
 necessity, but it was how I noticed the above issue, and the attached
 patch fixes both cases.)

 Currently the behavior is effectively to just initialize and then pickle
 the wrapped object.  I have and will attach a patch which does that in a
 more direct way that works in both of the above cases.  It also adds a
 regression test which covers both of the above cases.

--
Ticket URL: <https://code.djangoproject.com/ticket/25389>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/057.d7ed0a35dd4ffda1b79d4b11b07afbef%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to