test_simplelazyobject.py 905 B

12345678910111213141516171819202122232425262728293031
  1. from __future__ import unicode_literals
  2. import pickle
  3. from django.contrib.auth.models import User
  4. from django.test import TestCase
  5. from django.utils import six
  6. from django.utils.functional import SimpleLazyObject
  7. class TestUtilsSimpleLazyObjectDjangoTestCase(TestCase):
  8. def test_pickle_py2_regression(self):
  9. # See ticket #20212
  10. user = User.objects.create_user('johndoe', 'john@example.com', 'pass')
  11. x = SimpleLazyObject(lambda: user)
  12. # This would fail with "TypeError: can't pickle instancemethod objects",
  13. # only on Python 2.X.
  14. pickle.dumps(x)
  15. # Try the variant protocol levels.
  16. pickle.dumps(x, 0)
  17. pickle.dumps(x, 1)
  18. pickle.dumps(x, 2)
  19. if six.PY2:
  20. import cPickle
  21. # This would fail with "TypeError: expected string or Unicode object, NoneType found".
  22. cPickle.dumps(x)