Chris McDonough
2011-06-14 92099080859976ce78882de477ddc2c01bc880b2
- New method named ``pyramid.request.Request.is_response``.  This method
should be used instead of the ``pyramid.view.is_response`` function, which
has been deprecated.

- Deprecated ``pyramid.view.is_response`` function in favor of (newly-added)
``pyramid.request.Request.is_response`` method. Determining if an object
is truly a valid response object now requires access to the registry, which
is only easily available as a request attribute. The
``pyramid.view.is_response`` function will still work until it is removed,
but now may return an incorrect answer under some (very uncommon)
circumstances.
6 files modified
82 ■■■■ changed files
CHANGES.txt 12 ●●●●● patch | view | raw | blame | history
TODO.txt 12 ●●●●● patch | view | raw | blame | history
pyramid/request.py 9 ●●●●● patch | view | raw | blame | history
pyramid/tests/test_request.py 28 ●●●●● patch | view | raw | blame | history
pyramid/tests/test_view.py 8 ●●●●● patch | view | raw | blame | history
pyramid/view.py 13 ●●●● patch | view | raw | blame | history
CHANGES.txt
@@ -140,6 +140,10 @@
  among other things, the ``conditional_response`` feature of WebOb response
  objects will now behave properly.
- New method named ``pyramid.request.Request.is_response``.  This method
  should be used instead of the ``pyramid.view.is_response`` function, which
  has been deprecated.
Bug Fixes
---------
@@ -270,6 +274,14 @@
  1.0 and before).  In a future version, these methods will be removed
  entirely.
- Deprecated ``pyramid.view.is_response`` function in favor of (newly-added)
  ``pyramid.request.Request.is_response`` method.  Determining if an object
  is truly a valid response object now requires access to the registry, which
  is only easily available as a request attribute.  The
  ``pyramid.view.is_response`` function will still work until it is removed,
  but now may return an incorrect answer under some (very uncommon)
  circumstances.
Behavior Changes
----------------
TODO.txt
@@ -6,18 +6,6 @@
- To subclass or not subclass http exceptions.
- Deprecate view.is_response?
- Move is_response to response.py?
- Create add_response_adapter Configurator API?
- Make sure registering IResponse adapter for webob.Response doesn't make it
  impossible to register an IResponse adapter for an interface that a
  webob.Response happens to implement.
- Run whatsitdoing tests.
- Docs mention ``exception.args[0]`` as a way to get messages; check that
  this works.
pyramid/request.py
@@ -323,6 +323,15 @@
                                                 default=Response)
        return response_factory()
    def is_response(self, ob):
        """ Return ``True`` if the object passed as ``ob`` is a valid
        response object, ``False`` otherwise."""
        registry = self.registry
        adapted = registry.queryAdapterOrSelf(ob, IResponse)
        if adapted is None:
            return False
        return adapted is ob
    # b/c dict interface for "root factory" code that expects a bare
    # environ.  Explicitly omitted dict methods: clear (unnecessary),
    # copy (implemented by WebOb), fromkeys (unnecessary); deprecated
pyramid/tests/test_request.py
@@ -203,7 +203,35 @@
        self.assertEqual(result, 'abc')
        self.assertEqual(info.args,
                         ('pyramid.tests:static/foo.css', request, {}) )
    def test_is_response_false(self):
        request = self._makeOne({})
        request.registry = self.config.registry
        self.assertEqual(request.is_response('abc'), False)
    def test_is_response_false_adapter_is_not_self(self):
        from pyramid.interfaces import IResponse
        request = self._makeOne({})
        request.registry = self.config.registry
        def adapter(ob):
            return object()
        class Foo(object):
            pass
        foo = Foo()
        request.registry.registerAdapter(adapter, (Foo,), IResponse)
        self.assertEqual(request.is_response(foo), False)
        
    def test_is_response_adapter_true(self):
        from pyramid.interfaces import IResponse
        request = self._makeOne({})
        request.registry = self.config.registry
        class Foo(object):
            pass
        foo = Foo()
        def adapter(ob):
            return ob
        request.registry.registerAdapter(adapter, (Foo,), IResponse)
        self.assertEqual(request.is_response(foo), True)
class TestRequestDeprecatedMethods(unittest.TestCase):
    def setUp(self):
pyramid/tests/test_view.py
@@ -198,6 +198,14 @@
        self.assertEqual(s, 'anotherview')
class TestIsResponse(unittest.TestCase):
    def setUp(self):
        from zope.deprecation import __show__
        __show__.off()
    def tearDown(self):
        from zope.deprecation import __show__
        __show__.on()
    def _callFUT(self, *arg, **kw):
        from pyramid.view import is_response
        return is_response(*arg, **kw)
pyramid/view.py
@@ -318,14 +318,15 @@
    """ Return ``True`` if ``ob`` implements the interface implied by
    :ref:`the_response`. ``False`` if not.
    .. note:: This isn't a true interface or subclass check.  Instead, it's a
        duck-typing check, as response objects are not obligated to be of a
        particular class or provide any particular Zope interface."""
    # response objects aren't obligated to implement a Zope interface,
    # so we do it the hard way
    .. warning:: This function is deprecated as of :app:`Pyramid` 1.1.  New
       code should not use it.  Instead, new code should use the
       :func:`pyramid.request.Request.is_response` method."""
    if ( hasattr(ob, 'app_iter') and hasattr(ob, 'headerlist') and
         hasattr(ob, 'status') ):
        return True
    return False
deprecated(
    'is_response',
    'pyramid.view.is_response is deprecated as of Pyramid 1.1.  Use '
    'pyramid.request.Request.is_response instead.')