Chris McDonough
2011-06-26 d8c55c0157b37594a5266ad92b8d203b5f6cb0ca
- 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.
6 files modified
191 ■■■■ changed files
CHANGES.txt 3 ●●●●● patch | view | raw | blame | history
TODO.txt 3 ●●●●● patch | view | raw | blame | history
pyramid/renderers.py 26 ●●●● patch | view | raw | blame | history
pyramid/request.py 79 ●●●●● patch | view | raw | blame | history
pyramid/tests/test_renderers.py 27 ●●●● patch | view | raw | blame | history
pyramid/tests/test_request.py 53 ●●●● patch | view | raw | blame | history
CHANGES.txt
@@ -8,6 +8,9 @@
  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)
==================
TODO.txt
@@ -4,9 +4,6 @@
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).
pyramid/renderers.py
@@ -365,36 +365,24 @@
            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)
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.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 @@
    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
pyramid/tests/test_renderers.py
@@ -3,18 +3,6 @@
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 @@
        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 @@
                          ('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)
pyramid/tests/test_request.py
@@ -236,20 +236,21 @@
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 @@
        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