Last active
September 15, 2015 04:20
-
-
Save benjaminjkraft/2d330e22a58681c5db5c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pickle | |
from django.utils.functional import SimpleLazyObject | |
class FooBase(object): | |
def __reduce__(self): | |
self.__dict__['_version'] = 1 | |
return super(FooBase, self).__reduce__() | |
class Foo(FooBase): | |
def __init__(self, foobase): | |
self.__dict__ = foobase.__dict__ | |
self._foo = foobase | |
lazy_foo = SimpleLazyObject(lambda: Foo(FooBase())) | |
print pickle.dumps(lazy_foo) # raises an error |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# run in a ./manage.py shell | |
import pickle | |
from django.contrib.auth.models import User #any model will do | |
from django.utils.functional import SimpleLazyObject | |
user = User.objects.get(username='benkraft') | |
lazy_user = SimpleLazyObject(lambda: user) | |
pickle.loads(pickle.dumps(lazy_user)) # warns |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pickle | |
from django.utils.functional import SimpleLazyObject | |
class Foo(object): | |
def __reduce__(self): | |
self.__dict__['_version'] = 1 | |
return super(Foo, self).__reduce__() | |
def __init__(self): | |
self._foo = self | |
lazy_foo = SimpleLazyObject(lambda: Foo()) | |
print pickle.dumps(lazy_foo) # raises an error |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment