Skip to content Skip to sidebar Skip to footer

Python: Pickle Misbehaving In Django Shell As Opposed To Python Shell?

As an additional question stemming from my previous question it turns out that pickle behaves differently in django shell compared to python shell... this script: import pickle cl

Solution 1:

pickle will make sure it can re-import a class, as only the data on the instance itself is pickled, plus the import location of the class. As such, pickle looks for the __module__ attribute on the class to determine where it came from.

It appears the Django interactive environment does not set this __module__ attribute; as a result TestObj.__module__ is inherited from the object base class instead, and that's __builtin__. Perhaps no __name__ global is set. As a result, the pickle module ends up looking in the wrong place for your class. There is no __builtin__.TestObj after all.

From the comments, I gather that you are trying to store mocked objects in the Django cache. That won't work, as mock objects are not pickleable. That makes sense, as on unpickling (which could be in an entirely new Python process), how would pickle know what original class was being mocked?


Solution 2:

Could you not just use a serializer that doesn't pickle classes by reference… and then pre-serialize what you pass into the django cache? This would store not only the instance, but also the class definition itself -- which should allow you to reconstruct the instance anywhere.

See my answer on your original question: Pickle can't store an object in django locmem cache during tests?

I don't know anything about django Mock objects, or if there's something especially unpicklable about them… But as long as they are built from python code (as opposed to built in C, and have a thin python wrapper layer), then the above should likely work.


Post a Comment for "Python: Pickle Misbehaving In Django Shell As Opposed To Python Shell?"