From d8c55c0157b37594a5266ad92b8d203b5f6cb0ca Mon Sep 17 00:00:00 2001 From: Chris McDonough Date: Sun, 26 Jun 2011 02:59:19 -0400 Subject: - Accessing or setting deprecated response_* attrs on request (e.g. ``response_content_type``) now issues a deprecation warning at access time rather than at rendering time. --- CHANGES.txt | 3 ++ TODO.txt | 3 -- pyramid/renderers.py | 26 ++++---------- pyramid/request.py | 79 ++++++++++++++++++++++++++++++++++++++++- pyramid/tests/test_renderers.py | 27 +++----------- pyramid/tests/test_request.py | 53 +++++++++++++++++++++++---- 6 files changed, 140 insertions(+), 51 deletions(-) diff --git a/CHANGES.txt b/CHANGES.txt index 3c1d16fa5..b95211d09 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -8,6 +8,9 @@ Bug fixes to set a ``__text__`` attribute on a custom predicate that was actually a classmethod. See https://github.com/Pylons/pyramid/pull/217 . +- Accessing or setting deprecated response_* attrs on request + (e.g. ``response_content_type``) now issues a deprecation warning at access + time rather than at rendering time. 1.1a2 (2011-06-22) ================== diff --git a/TODO.txt b/TODO.txt index b52ad4eec..ba9a72d03 100644 --- a/TODO.txt +++ b/TODO.txt @@ -4,9 +4,6 @@ Pyramid TODOs Must-Have --------- -- Deprecate response_foo attrs on request at attribute set time rather than - lookup time. - - Investigate mod_wsgi tutorial to make sure it still works (2 reports say no; application package not found). diff --git a/pyramid/renderers.py b/pyramid/renderers.py index 6865067dd..b8d02ace3 100644 --- a/pyramid/renderers.py +++ b/pyramid/renderers.py @@ -365,36 +365,24 @@ class RendererHelper(object): response.body = result if request is not None: - # deprecated mechanism to set up request.response_* attrs + # deprecated mechanism to set up request.response_* attrs, see + # pyramid.request.Request attrs = request.__dict__ - content_type = attrs.get('response_content_type', None) + content_type = attrs.get('_response_content_type', None) if content_type is not None: response.content_type = content_type - deprecate_req_attr('Setting', 'content_type', - 'set', 'content_type') - headerlist = attrs.get('response_headerlist', None) + headerlist = attrs.get('_response_headerlist', None) if headerlist is not None: for k, v in headerlist: response.headers.add(k, v) - deprecate_req_attr('Setting or mutating', 'headerlist', - 'set or mutate', 'headerlist') - status = attrs.get('response_status', None) + status = attrs.get('_response_status', None) if status is not None: response.status = status - deprecate_req_attr('Setting', 'status', 'set', 'status') - charset = attrs.get('response_charset', None) + charset = attrs.get('_response_charset', None) if charset is not None: response.charset = charset - deprecate_req_attr('Setting', 'charset', 'set', 'charset') - cache_for = attrs.get('response_cache_for', None) + cache_for = attrs.get('_response_cache_for', None) if cache_for is not None: response.cache_expires = cache_for - deprecate_req_attr('Setting', 'cache_for', - 'set', 'cache_expires') - return response -def deprecate_req_attr(*args): - depwarn = ('%s "request.response_%s" is deprecated as of Pyramid 1.1; %s ' - '"request.response.%s" instead.') - warnings.warn(depwarn % args, DeprecationWarning, 3) diff --git a/pyramid/request.py b/pyramid/request.py index 06dbddd29..b9dd2dfe9 100644 --- a/pyramid/request.py +++ b/pyramid/request.py @@ -1,4 +1,5 @@ from zope.deprecation import deprecate +from zope.deprecation.deprecation import deprecated from zope.interface import implements from zope.interface.interface import InterfaceClass @@ -12,7 +13,6 @@ from pyramid.interfaces import IResponseFactory from pyramid.exceptions import ConfigurationError from pyramid.decorator import reify from pyramid.response import Response -from pyramid.threadlocal import get_current_registry from pyramid.url import resource_url from pyramid.url import route_url from pyramid.url import static_url @@ -409,6 +409,83 @@ class Request(BaseRequest): def values(self): return self.environ.values() + # 1.0 deprecated bw compat code for using response_* values + + rr_dep = ('Accessing and setting "request.response_%s" is ' + 'deprecated as of Pyramid 1.1; access or set ' + '"request.response.%s" instead.') + + # response_content_type + def _response_content_type_get(self): + return self._response_content_type + def _response_content_type_set(self, value): + self._response_content_type = value + def _response_content_type_del(self): + del self._response_content_type + response_content_type = property(_response_content_type_get, + _response_content_type_set, + _response_content_type_del) + response_content_type = deprecated( + response_content_type, + rr_dep % ('content_type', 'content_type')) + + # response_headerlist + def _response_headerlist_get(self): + return self._response_headerlist + def _response_headerlist_set(self, value): + self._response_headerlist = value + def _response_headerlist_del(self): + del self._response_headerlist + response_headerlist = property(_response_headerlist_get, + _response_headerlist_set, + _response_headerlist_del) + response_headerlist = deprecated( + response_headerlist, + rr_dep % ('headerlist', 'headerlist')) + + # response_status + def _response_status_get(self): + return self._response_status + def _response_status_set(self, value): + self._response_status = value + def _response_status_del(self): + del self._response_status + response_status = property(_response_status_get, + _response_status_set, + _response_status_del) + + response_status = deprecated( + response_status, + rr_dep % ('status', 'status')) + + # response_charset + def _response_charset_get(self): + return self._response_charset + def _response_charset_set(self, value): + self._response_charset = value + def _response_charset_del(self): + del self._response_charset + response_charset = property(_response_charset_get, + _response_charset_set, + _response_charset_del) + response_charset = deprecated( + response_charset, + rr_dep % ('charset', 'charset')) + + # response_cache_for + def _response_cache_for_get(self): + return self._response_cache_for + def _response_cache_for_set(self, value): + self._response_cache_for = value + def _response_cache_for_del(self): + del self._response_cache_for + response_cache_for = property(_response_cache_for_get, + _response_cache_for_set, + _response_cache_for_del) + response_cache_for = deprecated( + response_cache_for, + rr_dep % ('cache_for', 'cache_expires')) + def route_request_iface(name, bases=()): iface = InterfaceClass('%s_IRequest' % name, bases=bases) # for exception view lookups diff --git a/pyramid/tests/test_renderers.py b/pyramid/tests/test_renderers.py index 0dbb0d821..68369d570 100644 --- a/pyramid/tests/test_renderers.py +++ b/pyramid/tests/test_renderers.py @@ -3,18 +3,6 @@ import unittest from pyramid.testing import cleanUp from pyramid import testing -def hide_warnings(wrapped): - import warnings - def wrapper(*arg, **kw): - warnings.filterwarnings('ignore') - try: - wrapped(*arg, **kw) - finally: - warnings.resetwarnings() - wrapper.__name__ = wrapped.__name__ - wrapper.__doc__ = wrapped.__doc__ - return wrapper - class TestTemplateRendererFactory(unittest.TestCase): def setUp(self): self.config = cleanUp() @@ -619,24 +607,22 @@ class TestRendererHelper(unittest.TestCase): response = helper._make_response(la.encode('utf-8'), request) self.assertEqual(response.body, la.encode('utf-8')) - @hide_warnings def test__make_response_with_content_type(self): from pyramid.response import Response request = testing.DummyRequest() request.response = Response() - attrs = {'response_content_type':'text/nonsense'} + attrs = {'_response_content_type':'text/nonsense'} request.__dict__.update(attrs) helper = self._makeOne('loo.foo') response = helper._make_response('abc', request) self.assertEqual(response.content_type, 'text/nonsense') self.assertEqual(response.body, 'abc') - @hide_warnings def test__make_response_with_headerlist(self): from pyramid.response import Response request = testing.DummyRequest() request.response = Response() - attrs = {'response_headerlist':[('a', '1'), ('b', '2')]} + attrs = {'_response_headerlist':[('a', '1'), ('b', '2')]} request.__dict__.update(attrs) helper = self._makeOne('loo.foo') response = helper._make_response('abc', request) @@ -647,35 +633,32 @@ class TestRendererHelper(unittest.TestCase): ('b', '2')]) self.assertEqual(response.body, 'abc') - @hide_warnings def test__make_response_with_status(self): from pyramid.response import Response request = testing.DummyRequest() request.response = Response() - attrs = {'response_status':'406 You Lose'} + attrs = {'_response_status':'406 You Lose'} request.__dict__.update(attrs) helper = self._makeOne('loo.foo') response = helper._make_response('abc', request) self.assertEqual(response.status, '406 You Lose') self.assertEqual(response.body, 'abc') - @hide_warnings def test__make_response_with_charset(self): from pyramid.response import Response request = testing.DummyRequest() request.response = Response() - attrs = {'response_charset':'UTF-16'} + attrs = {'_response_charset':'UTF-16'} request.__dict__.update(attrs) helper = self._makeOne('loo.foo') response = helper._make_response('abc', request) self.assertEqual(response.charset, 'UTF-16') - @hide_warnings def test__make_response_with_cache_for(self): from pyramid.response import Response request = testing.DummyRequest() request.response = Response() - attrs = {'response_cache_for':100} + attrs = {'_response_cache_for':100} request.__dict__.update(attrs) helper = self._makeOne('loo.foo') response = helper._make_response('abc', request) diff --git a/pyramid/tests/test_request.py b/pyramid/tests/test_request.py index 90c55b0f0..76426b8a8 100644 --- a/pyramid/tests/test_request.py +++ b/pyramid/tests/test_request.py @@ -236,20 +236,21 @@ class TestRequest(unittest.TestCase): class TestRequestDeprecatedMethods(unittest.TestCase): def setUp(self): self.config = testing.setUp() - self.config.begin() - import warnings - warnings.filterwarnings('ignore') + from zope.deprecation import __show__ + __show__.off() def tearDown(self): testing.tearDown() - import warnings - warnings.resetwarnings() + from zope.deprecation import __show__ + __show__.on() def _getTargetClass(self): from pyramid.request import Request return Request - def _makeOne(self, environ): + def _makeOne(self, environ=None): + if environ is None: + environ = {} return self._getTargetClass()(environ) def test___contains__(self): @@ -349,6 +350,46 @@ class TestRequestDeprecatedMethods(unittest.TestCase): result = inst.values() self.assertEqual(result, environ.values()) + def test_response_content_type(self): + inst = self._makeOne() + self.assertFalse(hasattr(inst, 'response_content_type')) + inst.response_content_type = 'abc' + self.assertEqual(inst.response_content_type, 'abc') + del inst.response_content_type + self.assertFalse(hasattr(inst, 'response_content_type')) + + def test_response_headerlist(self): + inst = self._makeOne() + self.assertFalse(hasattr(inst, 'response_headerlist')) + inst.response_headerlist = 'abc' + self.assertEqual(inst.response_headerlist, 'abc') + del inst.response_headerlist + self.assertFalse(hasattr(inst, 'response_headerlist')) + + def test_response_status(self): + inst = self._makeOne() + self.assertFalse(hasattr(inst, 'response_status')) + inst.response_status = 'abc' + self.assertEqual(inst.response_status, 'abc') + del inst.response_status + self.assertFalse(hasattr(inst, 'response_status')) + + def test_response_charset(self): + inst = self._makeOne() + self.assertFalse(hasattr(inst, 'response_charset')) + inst.response_charset = 'abc' + self.assertEqual(inst.response_charset, 'abc') + del inst.response_charset + self.assertFalse(hasattr(inst, 'response_charset')) + + def test_response_cache_for(self): + inst = self._makeOne() + self.assertFalse(hasattr(inst, 'response_cache_for')) + inst.response_cache_for = 'abc' + self.assertEqual(inst.response_cache_for, 'abc') + del inst.response_cache_for + self.assertFalse(hasattr(inst, 'response_cache_for')) + class Test_route_request_iface(unittest.TestCase): def _callFUT(self, name): from pyramid.request import route_request_iface -- cgit v1.2.3