2012-06-08 00:08:47 +08:00
|
|
|
from __future__ import unicode_literals
|
|
|
|
|
2011-12-15 10:33:14 +08:00
|
|
|
import pickle
|
2010-09-27 23:15:04 +08:00
|
|
|
|
2013-09-18 21:35:51 +08:00
|
|
|
from django.contrib.auth.models import User
|
2014-01-29 12:53:30 +08:00
|
|
|
from django.test import TestCase
|
2012-07-20 20:48:51 +08:00
|
|
|
from django.utils import six
|
2014-01-29 12:53:30 +08:00
|
|
|
from django.utils.functional import SimpleLazyObject
|
2011-06-01 23:30:06 +08:00
|
|
|
|
2010-09-27 23:15:04 +08:00
|
|
|
|
2014-01-29 12:53:30 +08:00
|
|
|
class TestUtilsSimpleLazyObjectDjangoTestCase(TestCase):
|
2013-05-21 13:17:56 +08:00
|
|
|
|
2013-09-18 21:35:51 +08:00
|
|
|
def test_pickle_py2_regression(self):
|
2013-05-21 13:17:56 +08:00
|
|
|
# See ticket #20212
|
|
|
|
user = User.objects.create_user('johndoe', 'john@example.com', 'pass')
|
|
|
|
x = SimpleLazyObject(lambda: user)
|
|
|
|
|
|
|
|
# This would fail with "TypeError: can't pickle instancemethod objects",
|
|
|
|
# only on Python 2.X.
|
2013-10-19 20:31:38 +08:00
|
|
|
pickle.dumps(x)
|
2013-05-21 13:17:56 +08:00
|
|
|
|
|
|
|
# Try the variant protocol levels.
|
2013-10-19 20:31:38 +08:00
|
|
|
pickle.dumps(x, 0)
|
|
|
|
pickle.dumps(x, 1)
|
|
|
|
pickle.dumps(x, 2)
|
2013-05-21 13:17:56 +08:00
|
|
|
|
2013-09-02 18:06:32 +08:00
|
|
|
if six.PY2:
|
2013-05-21 13:17:56 +08:00
|
|
|
import cPickle
|
|
|
|
|
|
|
|
# This would fail with "TypeError: expected string or Unicode object, NoneType found".
|
2013-10-19 20:31:38 +08:00
|
|
|
cPickle.dumps(x)
|