summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--CHANGES.txt12
-rw-r--r--TODO.txt12
-rw-r--r--pyramid/request.py9
-rw-r--r--pyramid/tests/test_request.py28
-rw-r--r--pyramid/tests/test_view.py8
-rw-r--r--pyramid/view.py13
6 files changed, 64 insertions, 18 deletions
diff --git a/CHANGES.txt b/CHANGES.txt
index c7ca3794d..ea4bedc7e 100644
--- a/CHANGES.txt
+++ b/CHANGES.txt
@@ -140,6 +140,10 @@ Features
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 @@ Deprecations
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
----------------
diff --git a/TODO.txt b/TODO.txt
index 9d9bbf1eb..27ab9ffb5 100644
--- a/TODO.txt
+++ b/TODO.txt
@@ -6,18 +6,6 @@ Must-Have
- 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.
diff --git a/pyramid/request.py b/pyramid/request.py
index b69440ac6..06dbddd29 100644
--- a/pyramid/request.py
+++ b/pyramid/request.py
@@ -323,6 +323,15 @@ class Request(BaseRequest):
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
diff --git a/pyramid/tests/test_request.py b/pyramid/tests/test_request.py
index e35856ce0..90c55b0f0 100644
--- a/pyramid/tests/test_request.py
+++ b/pyramid/tests/test_request.py
@@ -203,7 +203,35 @@ class TestRequest(unittest.TestCase):
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):
diff --git a/pyramid/tests/test_view.py b/pyramid/tests/test_view.py
index 0ea9a11a0..b42224d4c 100644
--- a/pyramid/tests/test_view.py
+++ b/pyramid/tests/test_view.py
@@ -198,6 +198,14 @@ class RenderViewTests(BaseTest, unittest.TestCase):
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)
diff --git a/pyramid/view.py b/pyramid/view.py
index a89df8859..afa10fd0f 100644
--- a/pyramid/view.py
+++ b/pyramid/view.py
@@ -318,14 +318,15 @@ def is_response(ob):
""" 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.')