mirror of
https://github.com/django/django.git
synced 2025-10-24 06:06:09 +00:00
Fixed #16326 -- Fixed re-pickling of unpickled TemplateResponse instances. Thanks, natrius and lrekucki.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@16568 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
from __future__ import with_statement
|
||||
from datetime import datetime
|
||||
import os
|
||||
import pickle
|
||||
@@ -8,7 +9,8 @@ from django.conf import settings
|
||||
import django.template.context
|
||||
from django.template import Template, Context
|
||||
from django.template.response import (TemplateResponse, SimpleTemplateResponse,
|
||||
ContentNotRenderedError)
|
||||
ContentNotRenderedError,
|
||||
DiscardedAttributeError)
|
||||
|
||||
def test_processor(request):
|
||||
return {'processors': 'yes'}
|
||||
@@ -190,9 +192,27 @@ class SimpleTemplateResponseTest(BaseTemplateResponseTest):
|
||||
|
||||
# ...and the unpickled reponse doesn't have the
|
||||
# template-related attributes, so it can't be re-rendered
|
||||
self.assertFalse(hasattr(unpickled_response, 'template_name'))
|
||||
self.assertFalse(hasattr(unpickled_response, 'context_data'))
|
||||
self.assertFalse(hasattr(unpickled_response, '_post_render_callbacks'))
|
||||
template_attrs = ('template_name', 'context_data', '_post_render_callbacks')
|
||||
for attr in template_attrs:
|
||||
self.assertFalse(hasattr(unpickled_response, attr))
|
||||
|
||||
# ...and requesting any of those attributes raises an exception
|
||||
for attr in template_attrs:
|
||||
with self.assertRaises(DiscardedAttributeError) as cm:
|
||||
getattr(unpickled_response, attr)
|
||||
|
||||
def test_repickling(self):
|
||||
response = SimpleTemplateResponse('first/test.html', {
|
||||
'value': 123,
|
||||
'fn': datetime.now,
|
||||
})
|
||||
self.assertRaises(ContentNotRenderedError,
|
||||
pickle.dumps, response)
|
||||
|
||||
response.render()
|
||||
pickled_response = pickle.dumps(response)
|
||||
unpickled_response = pickle.loads(pickled_response)
|
||||
repickled_response = pickle.dumps(unpickled_response)
|
||||
|
||||
class TemplateResponseTest(BaseTemplateResponseTest):
|
||||
|
||||
@@ -255,10 +275,28 @@ class TemplateResponseTest(BaseTemplateResponseTest):
|
||||
|
||||
# ...and the unpickled reponse doesn't have the
|
||||
# template-related attributes, so it can't be re-rendered
|
||||
self.assertFalse(hasattr(unpickled_response, '_request'))
|
||||
self.assertFalse(hasattr(unpickled_response, 'template_name'))
|
||||
self.assertFalse(hasattr(unpickled_response, 'context_data'))
|
||||
self.assertFalse(hasattr(unpickled_response, '_post_render_callbacks'))
|
||||
template_attrs = ('template_name', 'context_data',
|
||||
'_post_render_callbacks', '_request', '_current_app')
|
||||
for attr in template_attrs:
|
||||
self.assertFalse(hasattr(unpickled_response, attr))
|
||||
|
||||
# ...and requesting any of those attributes raises an exception
|
||||
for attr in template_attrs:
|
||||
with self.assertRaises(DiscardedAttributeError) as cm:
|
||||
getattr(unpickled_response, attr)
|
||||
|
||||
def test_repickling(self):
|
||||
response = SimpleTemplateResponse('first/test.html', {
|
||||
'value': 123,
|
||||
'fn': datetime.now,
|
||||
})
|
||||
self.assertRaises(ContentNotRenderedError,
|
||||
pickle.dumps, response)
|
||||
|
||||
response.render()
|
||||
pickled_response = pickle.dumps(response)
|
||||
unpickled_response = pickle.loads(pickled_response)
|
||||
repickled_response = pickle.dumps(unpickled_response)
|
||||
|
||||
|
||||
class CustomURLConfTest(TestCase):
|
||||
|
||||
Reference in New Issue
Block a user