2014-12-17 16:25:28 +00:00
|
|
|
import datetime
|
2014-12-19 19:11:17 +00:00
|
|
|
import os
|
2014-06-06 11:10:20 +00:00
|
|
|
import pickle
|
2014-12-17 16:25:28 +00:00
|
|
|
import subprocess
|
2014-12-17 22:49:50 +00:00
|
|
|
import sys
|
2014-06-06 11:10:20 +00:00
|
|
|
import warnings
|
|
|
|
|
2014-12-19 19:11:17 +00:00
|
|
|
from django.core.files.temp import NamedTemporaryFile
|
2015-01-28 12:35:27 +00:00
|
|
|
from django.db import DJANGO_VERSION_PICKLE_KEY, models
|
2014-06-06 11:10:20 +00:00
|
|
|
from django.test import TestCase
|
|
|
|
from django.utils.encoding import force_text
|
|
|
|
from django.utils.version import get_major_version, get_version
|
|
|
|
|
2014-12-17 16:25:28 +00:00
|
|
|
from .models import Article
|
|
|
|
|
2014-06-06 11:10:20 +00:00
|
|
|
|
|
|
|
class ModelPickleTestCase(TestCase):
|
|
|
|
def test_missing_django_version_unpickling(self):
|
|
|
|
"""
|
|
|
|
#21430 -- Verifies a warning is raised for models that are
|
|
|
|
unpickled without a Django version
|
|
|
|
"""
|
|
|
|
class MissingDjangoVersion(models.Model):
|
|
|
|
title = models.CharField(max_length=10)
|
|
|
|
|
|
|
|
def __reduce__(self):
|
|
|
|
reduce_list = super(MissingDjangoVersion, self).__reduce__()
|
|
|
|
data = reduce_list[-1]
|
|
|
|
del data[DJANGO_VERSION_PICKLE_KEY]
|
|
|
|
return reduce_list
|
|
|
|
|
|
|
|
p = MissingDjangoVersion(title="FooBar")
|
|
|
|
with warnings.catch_warnings(record=True) as recorded:
|
|
|
|
pickle.loads(pickle.dumps(p))
|
|
|
|
msg = force_text(recorded.pop().message)
|
|
|
|
self.assertEqual(msg,
|
|
|
|
"Pickled model instance's Django version is not specified.")
|
|
|
|
|
|
|
|
def test_unsupported_unpickle(self):
|
|
|
|
"""
|
|
|
|
#21430 -- Verifies a warning is raised for models that are
|
|
|
|
unpickled with a different Django version than the current
|
|
|
|
"""
|
|
|
|
class DifferentDjangoVersion(models.Model):
|
|
|
|
title = models.CharField(max_length=10)
|
|
|
|
|
|
|
|
def __reduce__(self):
|
|
|
|
reduce_list = super(DifferentDjangoVersion, self).__reduce__()
|
|
|
|
data = reduce_list[-1]
|
|
|
|
data[DJANGO_VERSION_PICKLE_KEY] = str(float(get_major_version()) - 0.1)
|
|
|
|
return reduce_list
|
|
|
|
|
|
|
|
p = DifferentDjangoVersion(title="FooBar")
|
|
|
|
with warnings.catch_warnings(record=True) as recorded:
|
|
|
|
pickle.loads(pickle.dumps(p))
|
|
|
|
msg = force_text(recorded.pop().message)
|
|
|
|
self.assertEqual(msg,
|
|
|
|
"Pickled model instance's Django version %s does not "
|
|
|
|
"match the current version %s."
|
|
|
|
% (str(float(get_major_version()) - 0.1), get_version()))
|
2014-12-17 16:25:28 +00:00
|
|
|
|
|
|
|
def test_unpickling_when_appregistrynotready(self):
|
|
|
|
"""
|
|
|
|
#24007 -- Verifies that a pickled model can be unpickled without having
|
|
|
|
to manually setup the apps registry beforehand.
|
|
|
|
"""
|
|
|
|
script_template = """#!/usr/bin/env python
|
|
|
|
import pickle
|
|
|
|
|
|
|
|
from django.conf import settings
|
|
|
|
|
|
|
|
data = %r
|
|
|
|
|
2015-01-21 16:55:57 +00:00
|
|
|
settings.configure(DEBUG=False, INSTALLED_APPS=['model_regress'], SECRET_KEY = "blah")
|
2014-12-17 16:25:28 +00:00
|
|
|
article = pickle.loads(data)
|
|
|
|
print(article.headline)"""
|
|
|
|
a = Article.objects.create(
|
|
|
|
headline="Some object",
|
|
|
|
pub_date=datetime.datetime.now(),
|
|
|
|
article_text="This is an article",
|
|
|
|
)
|
|
|
|
|
2014-12-19 19:11:17 +00:00
|
|
|
with NamedTemporaryFile(mode='w+', suffix=".py", dir='.') as script:
|
2014-12-17 16:25:28 +00:00
|
|
|
script.write(script_template % pickle.dumps(a))
|
|
|
|
script.flush()
|
2015-01-16 16:41:14 +00:00
|
|
|
env = {
|
|
|
|
# Needed to run test outside of tests directory
|
|
|
|
str('PYTHONPATH'): os.pathsep.join(sys.path),
|
|
|
|
# Needed on Windows because http://bugs.python.org/issue8557
|
|
|
|
str('PATH'): os.environ['PATH'],
|
2015-02-15 23:59:54 +00:00
|
|
|
str('LANG'): os.environ.get('LANG'),
|
2015-01-16 16:41:14 +00:00
|
|
|
}
|
|
|
|
if 'SYSTEMROOT' in os.environ: # Windows http://bugs.python.org/issue20614
|
|
|
|
env[str('SYSTEMROOT')] = os.environ['SYSTEMROOT']
|
2014-12-17 16:25:28 +00:00
|
|
|
try:
|
2015-01-16 16:41:14 +00:00
|
|
|
result = subprocess.check_output([sys.executable, script.name], env=env)
|
2014-12-17 16:25:28 +00:00
|
|
|
except subprocess.CalledProcessError:
|
|
|
|
self.fail("Unable to reload model pickled data")
|
|
|
|
self.assertEqual(result.strip().decode(), "Some object")
|