diff options
| -rw-r--r-- | docs/glossary.rst | 3 | ||||
| -rw-r--r-- | docs/narr/hooks.rst | 29 | ||||
| -rw-r--r-- | pyramid/renderers.py | 4 | ||||
| -rw-r--r-- | pyramid/request.py | 4 | ||||
| -rw-r--r-- | pyramid/testing.py | 10 | ||||
| -rw-r--r-- | pyramid/tests/test_config/test_factories.py | 2 | ||||
| -rw-r--r-- | pyramid/tests/test_config/test_init.py | 32 | ||||
| -rw-r--r-- | pyramid/tests/test_renderers.py | 9 | ||||
| -rw-r--r-- | pyramid/tests/test_testing.py | 4 | ||||
| -rw-r--r-- | pyramid/tests/test_util.py | 26 | ||||
| -rw-r--r-- | pyramid/util.py | 13 |
11 files changed, 39 insertions, 97 deletions
diff --git a/docs/glossary.rst b/docs/glossary.rst index 05ff7c114..38133f68f 100644 --- a/docs/glossary.rst +++ b/docs/glossary.rst @@ -17,7 +17,8 @@ Glossary positional argument, returns a Pyramid-compatible request. response factory - An object which returns a Pyramid-compatible response. + An object which, provied a :term:`request` as a single positional + argument, returns a Pyramid-compatible response. response An object returned by a :term:`view callable` that represents response diff --git a/docs/narr/hooks.rst b/docs/narr/hooks.rst index 689ce9dc2..e250c2d7e 100644 --- a/docs/narr/hooks.rst +++ b/docs/narr/hooks.rst @@ -369,10 +369,10 @@ Whenever :app:`Pyramid` returns a response from a view it creates a :class:`pyramid.response.Response` class is created to represent the response object. -The class (aka "factory") that :app:`Pyramid` uses to create a response object -instance can be changed by passing a ``response_factory`` argument to the -constructor of the :term:`configurator`. This argument can be either a -callable or a :term:`dotted Python name` representing a callable. +The factory that :app:`Pyramid` uses to create a response object instance can be +changed by passing a ``response_factory`` argument to the constructor of the +:term:`configurator`. This argument can be either a callable or a +:term:`dotted Python name` representing a callable. .. code-block:: python :linenos: @@ -382,7 +382,7 @@ callable or a :term:`dotted Python name` representing a callable. class MyResponse(Response): pass - config = Configurator(response_factory=MyResponse) + config = Configurator(response_factory=lambda r: MyResponse()) If you're doing imperative configuration, and you'd rather do it after you've already constructed a :term:`configurator` it can also be registered via the @@ -398,25 +398,8 @@ already constructed a :term:`configurator` it can also be registered via the pass config = Configurator() - config.set_response_factory(MyRequest) - -If you are already using a custom ```request_factory`` you can also set the -``ResponseClass`` on your :class:`pyramid.request.Request`: - -.. code-block:: python - :linenos: - - from pyramid.config import Configurator - from pyramid.response import Response - from pyramid.request import Request - - class MyResponse(Response): - pass - - class MyRequest(Request): - ResponseClass = MyResponse + config.set_response_factory(lambda r: MyResponse()) - config = Configurator() Using The Before Render Event ----------------------------- diff --git a/pyramid/renderers.py b/pyramid/renderers.py index 3d5390840..51dbd318b 100644 --- a/pyramid/renderers.py +++ b/pyramid/renderers.py @@ -449,8 +449,8 @@ class RendererHelper(object): if response is None: # request is None or request is not a pyramid.response.Response registry = self.registry - response_factory = _get_response_factory(registry, request) - response = response_factory() + response_factory = _get_response_factory(registry) + response = response_factory(request) if result is not None: if isinstance(result, text_type): diff --git a/pyramid/request.py b/pyramid/request.py index b66b8926c..fc957fae2 100644 --- a/pyramid/request.py +++ b/pyramid/request.py @@ -216,8 +216,8 @@ class Request( right" attributes (e.g. by calling ``request.response.set_cookie()``) within a view that uses a renderer. Mutations to this response object will be preserved in the response sent to the client.""" - response_factory = _get_response_factory(self.registry, self) - return response_factory() + response_factory = _get_response_factory(self.registry) + return response_factory(self) def is_response(self, ob): """ Return ``True`` if the object passed as ``ob`` is a valid diff --git a/pyramid/testing.py b/pyramid/testing.py index f77889e72..66c694b31 100644 --- a/pyramid/testing.py +++ b/pyramid/testing.py @@ -9,7 +9,6 @@ from zope.interface import ( from pyramid.interfaces import ( IRequest, - IResponseFactory, ISession, ) @@ -40,7 +39,10 @@ from pyramid.threadlocal import ( from pyramid.i18n import LocalizerRequestMixin from pyramid.request import CallbackMethodsMixin from pyramid.url import URLMethodsMixin -from pyramid.util import InstancePropertyMixin +from pyramid.util import ( + InstancePropertyMixin, + _get_response_factory + ) _marker = object() @@ -383,8 +385,8 @@ class DummyRequest( @reify def response(self): - f = self.registry.queryUtility(IResponseFactory, default=Response) - return f() + f = _get_response_factory(self.registry) + return f(self) have_zca = True diff --git a/pyramid/tests/test_config/test_factories.py b/pyramid/tests/test_config/test_factories.py index c6785d4a5..0bd5336ff 100644 --- a/pyramid/tests/test_config/test_factories.py +++ b/pyramid/tests/test_config/test_factories.py @@ -26,7 +26,7 @@ class TestFactoriesMixin(unittest.TestCase): def test_set_response_factory(self): from pyramid.interfaces import IResponseFactory config = self._makeOne(autocommit=True) - factory = object() + factory = lambda r: object() config.set_response_factory(factory) self.assertEqual(config.registry.getUtility(IResponseFactory), factory) diff --git a/pyramid/tests/test_config/test_init.py b/pyramid/tests/test_config/test_init.py index 8fa6be011..aeebe3c91 100644 --- a/pyramid/tests/test_config/test_init.py +++ b/pyramid/tests/test_config/test_init.py @@ -551,43 +551,13 @@ class ConfiguratorTests(unittest.TestCase): from pyramid.interfaces import IResponseFactory reg = Registry() config = self._makeOne(reg) - factory = object() + factory = lambda r: object() config.setup_registry(response_factory=factory) self.assertEqual(reg.queryUtility(IResponseFactory), None) config.commit() utility = reg.getUtility(IResponseFactory) self.assertEqual(utility, factory) - def test_setup_registry_request_factory_custom_response_class(self): - from pyramid.registry import Registry - from pyramid.interfaces import IRequestFactory - from pyramid.request import Request - - class MyResponse(object): - pass - - class MyRequest(Request): - ResponseClass = MyResponse - - reg = Registry() - config = self._makeOne(reg) - factory = MyRequest({ - 'PATH_INFO': '/', - 'wsgi.url_scheme': 'http', - 'HTTP_HOST': 'localhost', - 'SERVER_PROTOCOL': '1.0', - }) - factory.registry = reg - - config.setup_registry(request_factory=factory) - self.assertEqual(reg.queryUtility(IRequestFactory), None) - config.commit() - utility = reg.getUtility(IRequestFactory) - self.assertEqual(utility, factory) - - new_response = factory.response - self.assertTrue(isinstance(new_response, MyResponse)) - def test_setup_registry_request_factory_dottedname(self): from pyramid.registry import Registry from pyramid.interfaces import IRequestFactory diff --git a/pyramid/tests/test_renderers.py b/pyramid/tests/test_renderers.py index 2bddd2318..21878b41f 100644 --- a/pyramid/tests/test_renderers.py +++ b/pyramid/tests/test_renderers.py @@ -182,7 +182,10 @@ class TestRendererHelper(unittest.TestCase): from pyramid.interfaces import IResponseFactory class ResponseFactory(object): pass - self.config.registry.registerUtility(ResponseFactory, IResponseFactory) + + self.config.registry.registerUtility( + lambda r: ResponseFactory(), IResponseFactory + ) def test_render_to_response(self): self._registerRendererFactory() @@ -310,7 +313,9 @@ class TestRendererHelper(unittest.TestCase): class ResponseFactory(object): def __init__(self): pass - self.config.registry.registerUtility(ResponseFactory, IResponseFactory) + self.config.registry.registerUtility( + lambda r: ResponseFactory(), IResponseFactory + ) request = testing.DummyRequest() helper = self._makeOne('loo.foo') response = helper._make_response(b'abc', request) diff --git a/pyramid/tests/test_testing.py b/pyramid/tests/test_testing.py index dfcad2a0c..113f7e5f4 100644 --- a/pyramid/tests/test_testing.py +++ b/pyramid/tests/test_testing.py @@ -259,7 +259,9 @@ class TestDummyRequest(unittest.TestCase): registry = Registry('this_test') class ResponseFactory(object): pass - registry.registerUtility(ResponseFactory, IResponseFactory) + registry.registerUtility( + lambda r: ResponseFactory(), IResponseFactory + ) request = self._makeOne() request.registry = registry resp = request.response diff --git a/pyramid/tests/test_util.py b/pyramid/tests/test_util.py index 0ebbd80a2..ba128eede 100644 --- a/pyramid/tests/test_util.py +++ b/pyramid/tests/test_util.py @@ -324,7 +324,7 @@ class Test_object_description(unittest.TestCase): self.assertEqual( self._callFUT(inst), "object %s" % str(inst)) - + def test_shortened_repr(self): inst = ['1'] * 1000 self.assertEqual( @@ -592,7 +592,7 @@ class TestActionInfo(unittest.TestCase): def _getTargetClass(self): from pyramid.util import ActionInfo return ActionInfo - + def _makeOne(self, filename, lineno, function, linerepr): return self._getTargetClass()(filename, lineno, function, linerepr) @@ -620,30 +620,14 @@ class TestActionInfo(unittest.TestCase): class TestGetResponseFactory(unittest.TestCase): - def test_no_request(self): + def test_get_factory(self): from pyramid.util import _get_response_factory from pyramid.registry import Registry from pyramid.response import Response registry = Registry() - factory = _get_response_factory(registry) - self.assertEqual(factory, Response) - - def test_with_request(self): - from pyramid.util import _get_response_factory - from pyramid.registry import Registry - from pyramid.request import Request - - class MyResponse(object): - pass - - class MyRequest(Request): - ResponseClass = MyResponse - registry = Registry() - - request = MyRequest({}) - factory = _get_response_factory(request.registry, request) - self.assertEqual(factory, MyResponse) + response = _get_response_factory(registry)(None) + self.assertTrue(isinstance(response, Response)) def dummyfunc(): pass diff --git a/pyramid/util.py b/pyramid/util.py index ee642164a..7aecca19c 100644 --- a/pyramid/util.py +++ b/pyramid/util.py @@ -556,19 +556,14 @@ def action_method(wrapped): wrapper.__docobj__ = wrapped return wrapper -def _get_response_factory(registry, request=None): + +def _get_response_factory(registry): """ Obtain a :class: `pyramid.response.Response` using the - ``request.ResponseClass`` property if available. + `pyramid.interfaces.IResponseFactory`. """ - # Request is `None` or does not have a `ResponseClass` - if hasattr(request, 'ResponseClass'): - response_class = request.ResponseClass - else: - response_class = Response - response_factory = registry.queryUtility( IResponseFactory, - default=response_class + default=lambda r: Response() ) return response_factory |
