diff options
| author | Chris McDonough <chrism@plope.com> | 2010-12-07 01:42:35 -0500 |
|---|---|---|
| committer | Chris McDonough <chrism@plope.com> | 2010-12-07 01:42:35 -0500 |
| commit | 89a6aba6aa0247ba5e537969586702994d6681f6 (patch) | |
| tree | b659855f92b330f148cc30d03c890499dd3641bc | |
| parent | 3c58b3041b2312c6f4429f54458a49b0eaee57ea (diff) | |
| download | pyramid-89a6aba6aa0247ba5e537969586702994d6681f6.tar.gz pyramid-89a6aba6aa0247ba5e537969586702994d6681f6.tar.bz2 pyramid-89a6aba6aa0247ba5e537969586702994d6681f6.zip | |
ranging in on some sort of conflict detection solution
| -rw-r--r-- | pyramid/configuration.py | 143 | ||||
| -rw-r--r-- | pyramid/registry.py | 15 | ||||
| -rw-r--r-- | pyramid/tests/test_configuration.py | 51 | ||||
| -rw-r--r-- | pyramid/tests/test_zcml.py | 110 | ||||
| -rw-r--r-- | pyramid/zcml.py | 33 |
5 files changed, 181 insertions, 171 deletions
diff --git a/pyramid/configuration.py b/pyramid/configuration.py index a8f343dfd..269d7604d 100644 --- a/pyramid/configuration.py +++ b/pyramid/configuration.py @@ -3,6 +3,7 @@ import os import re import sys import threading +import traceback import venusian @@ -10,7 +11,8 @@ from translationstring import ChameleonTranslate from zope.configuration import xmlconfig from zope.configuration.config import GroupingContextDecorator -from zope.configuration.config import GroupingStackItem +from zope.configuration.config import ConfigurationMachine +from zope.configuration.xmlconfig import registerCommonDirectives from zope.interface import Interface from zope.interface import implementedBy @@ -236,6 +238,7 @@ class Configurator(object): renderer_globals_factory=None, default_permission=None, session_factory=None, + _ctx=None, ): if package is None: package = caller_package() @@ -244,9 +247,14 @@ class Configurator(object): self.package_name = name_resolver.package_name self.package = name_resolver.package self.registry = registry + if _ctx is None: + _ctx = self._make_context() + _ctx.registry = registry + self._ctx = _ctx if registry is None: registry = Registry(self.package_name) self.registry = registry + _ctx.registry = registry self.setup_registry( settings=settings, root_factory=root_factory, @@ -261,6 +269,12 @@ class Configurator(object): session_factory=session_factory, ) + def _action(self, discriminator, callable=None, args=(), kw=None, order=0): + """ Register an action which will be executed during a commit. """ + if kw is None: + kw = {} + self._ctx.action(discriminator, callable, args, kw, order) + def _set_settings(self, mapping): settings = Settings(mapping or {}) self.registry.settings = settings @@ -277,17 +291,15 @@ class Configurator(object): def register(): self.registry.registerUtility(factory, IRootFactory) self.registry.registerUtility(factory, IDefaultRootFactory) # b/c - self.action(IRootFactory, register) + self._action(IRootFactory, register) #@config_method def _set_authentication_policy(self, policy): """ Add a :app:`Pyramid` :term:`authentication policy` to the current configuration.""" policy = self.maybe_dotted(policy) - _info = self.ctx_info() - self.registry.registerUtility(policy, IAuthenticationPolicy, - info=_info) - self.action(IAuthenticationPolicy, None) + self.registry.registerUtility(policy, IAuthenticationPolicy) + self._action(IAuthenticationPolicy) #@config_method def _set_authorization_policy(self, policy): @@ -295,10 +307,8 @@ class Configurator(object): the current configuration state (also accepts a :term:`dotted Python name`.""" policy = self.maybe_dotted(policy) - _info = self.ctx_info() - self.registry.registerUtility(policy, IAuthorizationPolicy, - info=_info) - self.action(IAuthorizationPolicy, None) + self.registry.registerUtility(policy, IAuthorizationPolicy) + self._action(IAuthorizationPolicy, None) def _make_spec(self, path_or_spec): package, filename = resolve_resource_spec(path_or_spec, @@ -341,11 +351,10 @@ class Configurator(object): override_pkg_name = override_package.__name__ override = self.registry.queryUtility( IPackageOverrides, name=pkg_name) - _info = self.ctx_info() if override is None: override = PackageOverrides(package) self.registry.registerUtility(override, IPackageOverrides, - name=pkg_name, info=_info) + name=pkg_name) override.insert(path, override_pkg_name, override_prefix) def _set_security_policies(self, authentication, authorization=None): @@ -374,20 +383,29 @@ class Configurator(object): if not hasattr(_registry, 'has_listeners'): _registry.has_listeners = True + def _make_context(self): + context = PyramidConfigurationMachine() + registerCommonDirectives(context) + return context + # API def commit(self): """ Commit pending configuration actions. """ - self.registry.ctx.execute_actions() - self.registry.reset_context() + self._ctx.execute_actions() + self._ctx = self._make_context() + self._ctx.registry = self.registry - def with_package(self, package): + def with_package(self, package, _ctx=None): """ Return a new Configurator instance with the same registry as this configurator using the package supplied as the ``package`` argument to the new configurator. ``package`` may be an actual Python package object or a Python dotted name representing a package.""" - return self.__class__(registry=self.registry, package=package) + if _ctx is None: + _ctx = self._ctx + return self.__class__(registry=self.registry, package=package, + _ctx=_ctx) def maybe_dotted(self, dotted): """ Resolve the :term:`dotted Python name` ``dotted`` to a @@ -466,7 +484,7 @@ class Configurator(object): if session_factory is not None: self.set_session_factory(session_factory) self.commit() - + # getSiteManager is a unit testing dep injection def hook_zca(self, getSiteManager=None): """ Call :func:`zope.component.getSiteManager.sethook` with @@ -588,15 +606,6 @@ class Configurator(object): renderer = {'name':renderer, 'package':self.package} return self._derive_view(view, attr=attr, renderer=renderer) - def action(self, discriminator, callable, args=(), kw=None, order=0): - """ Register an action which will be executed during commit. """ - if kw is None: - kw = {} - self.registry.ctx.action(discriminator, callable, args, kw, order) - - def ctx_info(self): - return getattr(self.registry.ctx, 'info', '') - #@config_method def add_subscriber(self, subscriber, iface=None): """Add an event :term:`subscriber` for the event stream @@ -617,10 +626,9 @@ class Configurator(object): iface = (Interface,) if not isinstance(iface, (tuple, list)): iface = (iface,) - info = self.ctx_info() def register(): - self.registry.registerHandler(subscriber, iface, info=info) - self.action(None, register) + self.registry.registerHandler(subscriber, iface) + self._action(None, register) return subscriber def add_settings(self, settings=None, **kw): @@ -701,7 +709,7 @@ class Configurator(object): lock.acquire() registry = self.registry self.manager.push({'registry':registry, 'request':None}) - context = registry.ctx + context = self._ctx try: context.package = package xmlconfig.file(filename, package, context=context, execute=False) @@ -734,17 +742,13 @@ class Configurator(object): sourcefiles.sort() - _context = self.registry.ctx + _context = self._ctx for filename, func, module in sourcefiles: context = GroupingContextDecorator(_context) context.basepath = os.path.dirname(filename) context.includepath = _context.includepath + (filename,) - self.registry.ctx = context - try: - func(self.with_package(module)) - finally: - self.registry.ctx = _context + func(self.with_package(module, _ctx=context)) def add_handler(self, route_name, pattern, handler, action=None, **kw): @@ -1111,7 +1115,7 @@ class Configurator(object): ``True``, the associated view callable will be considered viable for a given request. - """ + """ view = self.maybe_dotted(view) context = self.maybe_dotted(context) for_ = self.maybe_dotted(for_) @@ -1172,8 +1176,6 @@ class Configurator(object): if not IInterface.providedBy(r_context): r_context = implementedBy(r_context) - _info = self.ctx_info() - def register(permission=permission): if permission is None: @@ -1222,13 +1224,13 @@ class Configurator(object): view_iface = IView self.registry.registerAdapter( derived_view, - (IViewClassifier, request_iface, context), - view_iface, name, info=_info) + (IViewClassifier, request_iface, context), view_iface, name + ) if isexc: self.registry.registerAdapter( derived_view, (IExceptionViewClassifier, request_iface, context), - view_iface, name, info=_info) + view_iface, name) is_multiview = IMultiView.providedBy(old_view) old_phash = getattr(old_view, '__phash__', DEFAULT_PHASH) @@ -1274,12 +1276,12 @@ class Configurator(object): self.registry.registerAdapter( multiview, (IViewClassifier, request_iface, context), - IMultiView, name=name, info=_info) + IMultiView, name=name) if isexc: self.registry.registerAdapter( multiview, (IExceptionViewClassifier, request_iface, context), - IMultiView, name=name, info=_info) + IMultiView, name=name) discriminator = [ 'view', context, name, request_type, IView, containment, @@ -1287,7 +1289,7 @@ class Configurator(object): xhr, accept, header, path_info] discriminator.extend(sorted(custom_predicates)) discriminator = tuple(discriminator) - self.action(discriminator, register) + self._action(discriminator, register) #@config_method def add_route(self, @@ -1313,7 +1315,8 @@ class Configurator(object): view_attr=None, use_global_views=False, path=None, - pregenerator=None): + pregenerator=None, + ): """ Add a :term:`route configuration` to the current configuration state, as well as possibly a :term:`view configuration` to be used to specify a :term:`view callable` @@ -1636,7 +1639,7 @@ class Configurator(object): discriminator.extend(sorted(custom_predicates)) discriminator = tuple(discriminator) - self.action(discriminator, None) + self._action(discriminator, None) return mapper.connect(name, pattern, factory, predicates=predicates, pregenerator=pregenerator) @@ -1714,9 +1717,8 @@ class Configurator(object): name = '' # we need to register renderers eagerly because they are used during # view configuration - self.registry.registerUtility( - factory, IRendererFactory, name=name, info=self.ctx_info()) - self.action((IRendererFactory, name), None) + self.registry.registerUtility(factory, IRendererFactory, name=name) + self._action((IRendererFactory, name), None) #@config_method def override_resource(self, to_override, override_with, _override=None): @@ -1764,7 +1766,7 @@ class Configurator(object): from_package = sys.modules[package] to_package = sys.modules[override_package] override(from_package, path, to_package, override_prefix) - self.action(None, register) + self._action(None, register) def set_forbidden_view(self, view=None, attr=None, renderer=None, wrapper=None): @@ -1857,7 +1859,7 @@ class Configurator(object): factory = self.maybe_dotted(factory) def register(): self.registry.registerUtility(factory, IRequestFactory) - self.action(IRequestFactory, register) + self._action(IRequestFactory, register) #@config_method def set_renderer_globals_factory(self, factory): @@ -1881,7 +1883,7 @@ class Configurator(object): factory = self.maybe_dotted(factory) def register(): self.registry.registerUtility(factory, IRendererGlobalsFactory) - self.action(IRendererGlobalsFactory, register) + self._action(IRendererGlobalsFactory, register) #@config_method def set_locale_negotiator(self, negotiator): @@ -1905,7 +1907,7 @@ class Configurator(object): negotiator = self.maybe_dotted(negotiator) def register(): self.registry.registerUtility(negotiator, ILocaleNegotiator) - self.action(ILocaleNegotiator, register) + self._action(ILocaleNegotiator, register) #@config_method def set_default_permission(self, permission): @@ -1936,7 +1938,7 @@ class Configurator(object): """ # default permission used during view registration self.registry.registerUtility(permission, IDefaultPermission) - self.action(IDefaultPermission, None) + self._action(IDefaultPermission, None) #@config_method def set_session_factory(self, session_factory): @@ -1947,7 +1949,7 @@ class Configurator(object): """ def register(): self.registry.registerUtility(session_factory, ISessionFactory) - self.action(ISessionFactory, register) + self._action(ISessionFactory, register) #@config_method def add_translation_dirs(self, *specs): @@ -2861,3 +2863,32 @@ class ActionPredicate(object): # others that share the same action name return hash(self.action) +class PyramidConfigurationMachine(ConfigurationMachine): + def action(self, discriminator, callable, args=(), kw=None, order=0): + if kw is None: + kw = {} + + includepath = getattr(self, 'includepath', ()) + info = getattr(self, 'info', None) + if not info: + info = self._extract_stack() + + action = (discriminator, callable, args, kw, + includepath, + info, + order, + ) + + # remove trailing false items + while (len(action) > 2) and not action[-1]: + action = action[:-1] + + self.actions.append(action) + + def _extract_stack(self): + try: + f = traceback.extract_stack(limit=5) + return f[0] + except: + return '' + diff --git a/pyramid/registry.py b/pyramid/registry.py index 062befc2e..0063abfef 100644 --- a/pyramid/registry.py +++ b/pyramid/registry.py @@ -1,9 +1,6 @@ from zope.component.registry import Components -from zope.configuration.config import ConfigurationMachine -from zope.configuration.xmlconfig import registerCommonDirectives from pyramid.interfaces import ISettings -from pyramid.decorator import reify class Registry(Components, dict): """ A registry object is an :term:`application registry`. The existence @@ -25,7 +22,6 @@ class Registry(Components, dict): # to notify them has_listeners = False _settings = None - _ctx = None def registerSubscriptionAdapter(self, *arg, **kw): result = Components.registerSubscriptionAdapter(self, *arg, **kw) @@ -53,15 +49,4 @@ class Registry(Components, dict): settings = property(_get_settings, _set_settings) - def reset_context(self): - context = ConfigurationMachine() - registerCommonDirectives(context) - context.registry = self # circdep - self.ctx = context - return context - - @reify - def ctx(self): - return self.reset_context() - global_registry = Registry('global') diff --git a/pyramid/tests/test_configuration.py b/pyramid/tests/test_configuration.py index d8895e0fa..488be73eb 100644 --- a/pyramid/tests/test_configuration.py +++ b/pyramid/tests/test_configuration.py @@ -245,43 +245,37 @@ class ConfiguratorTests(unittest.TestCase): self.assertEqual(result, 'pyramid.tests:templates') def test_setup_registry_fixed(self): - from zope.configuration.config import ConfigurationMachine class DummyRegistry(object): - ctx = ConfigurationMachine() def subscribers(self, events, name): self.events = events return events - def reset_context(self): - self.context_reset = True def registerUtility(self, *arg, **kw): pass reg = DummyRegistry() config = self._makeOne(reg) + old_ctx = config._ctx config.add_view = lambda *arg, **kw: False config.setup_registry() self.assertEqual(reg.has_listeners, True) self.assertEqual(reg.notify(1), None) self.assertEqual(reg.events, (1,)) - self.assertEqual(reg.context_reset, True) + self.failIf(old_ctx is config._ctx) def test_setup_registry_registers_default_exceptionresponse_view(self): from pyramid.interfaces import IExceptionResponse from pyramid.view import default_exceptionresponse_view - from zope.configuration.config import ConfigurationMachine class DummyRegistry(object): - ctx = ConfigurationMachine() def registerUtility(self, *arg, **kw): pass - def reset_context(self): - self.context_reset = True reg = DummyRegistry() config = self._makeOne(reg) + old_ctx = config._ctx views = [] config.add_view = lambda *arg, **kw: views.append((arg, kw)) config.setup_registry() self.assertEqual(views[0], ((default_exceptionresponse_view,), {'context':IExceptionResponse})) - self.assertEqual(reg.context_reset, True) + self.failIf(old_ctx is config._ctx) def test_setup_registry_explicit_notfound_trumps_iexceptionresponse(self): from zope.interface import implementedBy @@ -653,16 +647,15 @@ class ConfiguratorTests(unittest.TestCase): def test_include_with_dotted_name(self): from pyramid import tests - import os - here = os.path.dirname(__file__) - fname = os.path.join(here, 'test_configuration.py') config = self._makeOne() - context_before = config.registry.ctx + context_before = config._ctx config.include('pyramid.tests.test_configuration.dummy_include') - context_after = config.registry.ctx + context_after = config._ctx + actions = context_after.actions + self.assertEqual(len(actions), 1) self.assertEqual( - context_after.actions, - [('discrim', None, tests, {},(fname,))] + context_after.actions[0][:3], + ('discrim', None, tests), ) self.assertEqual(context_after.basepath, None) self.assertEqual(context_after.includepath, ()) @@ -670,16 +663,15 @@ class ConfiguratorTests(unittest.TestCase): def test_include_with_python_callable(self): from pyramid import tests - import os - here = os.path.dirname(__file__) - fname = os.path.join(here, 'test_configuration.py') config = self._makeOne() - context_before = config.registry.ctx + context_before = config._ctx config.include(dummy_include) - context_after = config.registry.ctx + context_after = config._ctx + actions = context_after.actions + self.assertEqual(len(actions), 1) self.assertEqual( - context_after.actions, - [('discrim', None, tests, {},(fname,))] + actions[0][:3], + ('discrim', None, tests), ) self.assertEqual(context_after.basepath, None) self.assertEqual(context_after.includepath, ()) @@ -3441,6 +3433,15 @@ class ConfiguratorTests(unittest.TestCase): renderer.assert_(bar=2) renderer.assert_(request=request) + def test_commit_conflict_simple(self): + from zope.configuration.config import ConfigurationConflictError + config = self._makeOne() + def view1(request): pass + def view2(request): pass + config.add_view(view1) + config.add_view(view2) + self.assertRaises(ConfigurationConflictError, config.commit) + class Test__map_view(unittest.TestCase): def setUp(self): from pyramid.registry import Registry @@ -4735,5 +4736,5 @@ class DummyHandler(object): # pragma: no cover return 'response 2' def dummy_include(config): - config.action('discrim', None, config.package) + config._action('discrim', None, config.package) diff --git a/pyramid/tests/test_zcml.py b/pyramid/tests/test_zcml.py index 95dd958b7..73426f166 100644 --- a/pyramid/tests/test_zcml.py +++ b/pyramid/tests/test_zcml.py @@ -26,7 +26,7 @@ class TestViewDirective(unittest.TestCase): from pyramid.interfaces import IRendererFactory from pyramid.interfaces import IRequest reg = self.config.registry - context = reg.ctx + context = self.config._ctx def factory(path): def foo(*arg): return 'OK' @@ -51,7 +51,7 @@ class TestViewDirective(unittest.TestCase): from pyramid.interfaces import IViewClassifier from pyramid.interfaces import IRequest reg = self.config.registry - context = reg.ctx + context = self.config._ctx view = lambda *arg: 'OK' def pred1(context, request): return True @@ -77,7 +77,7 @@ class TestViewDirective(unittest.TestCase): from pyramid.interfaces import IViewClassifier from pyramid.interfaces import IRequest reg = self.config.registry - context = reg.ctx + context = self.config._ctx view = lambda *arg: 'OK' class Foo: pass @@ -99,7 +99,7 @@ class TestViewDirective(unittest.TestCase): from pyramid.interfaces import IViewClassifier from pyramid.interfaces import IRequest reg = self.config.registry - context = reg.ctx + context = self.config._ctx view = lambda *arg: 'OK' class Foo: pass @@ -134,7 +134,7 @@ class TestNotFoundDirective(unittest.TestCase): from pyramid.exceptions import NotFound reg = self.config.registry - context = reg.ctx + context = self.config._ctx def view(request): return 'OK' self._callFUT(context, view) @@ -170,7 +170,7 @@ class TestNotFoundDirective(unittest.TestCase): config.commit() def view(request): return {} - context = reg.ctx + context = self.config._ctx self._callFUT(context, view, renderer='fake.pt') actions = extract_actions(context.actions) regadapt = actions[0] @@ -201,7 +201,7 @@ class TestForbiddenDirective(unittest.TestCase): from pyramid.interfaces import IViewClassifier from pyramid.exceptions import Forbidden reg = self.config.registry - context = reg.ctx + context = self.config._ctx def view(request): return 'OK' self._callFUT(context, view) @@ -238,7 +238,7 @@ class TestForbiddenDirective(unittest.TestCase): config.commit() def view(request): return {} - context = reg.ctx + context = self.config._ctx self._callFUT(context, view, renderer='fake.pt') actions = extract_actions(context.actions) regadapt = actions[0] @@ -265,7 +265,7 @@ class TestRepozeWho1AuthenticationPolicyDirective(unittest.TestCase): def test_it_defaults(self): reg = self.config.registry from pyramid.interfaces import IAuthenticationPolicy - context = self.config.registry.ctx + context = self.config._ctx self._callFUT(context) actions = extract_actions(context.actions) self.assertEqual(len(actions), 1) @@ -280,7 +280,7 @@ class TestRepozeWho1AuthenticationPolicyDirective(unittest.TestCase): def test_it(self): reg = self.config.registry from pyramid.interfaces import IAuthenticationPolicy - context = self.config.registry.ctx + context = self.config._ctx def callback(identity, request): """ """ self._callFUT(context, identifier_name='something', callback=callback) @@ -308,7 +308,7 @@ class TestRemoteUserAuthenticationPolicyDirective(unittest.TestCase): def test_defaults(self): from pyramid.interfaces import IAuthenticationPolicy reg = self.config.registry - context = reg.ctx + context = self.config._ctx def callback(identity, request): """ """ self._callFUT(context) @@ -325,7 +325,7 @@ class TestRemoteUserAuthenticationPolicyDirective(unittest.TestCase): def test_it(self): from pyramid.interfaces import IAuthenticationPolicy reg = self.config.registry - context = reg.ctx + context = self.config._ctx def callback(identity, request): """ """ self._callFUT(context, environ_key='BLAH', callback=callback) @@ -353,7 +353,7 @@ class TestAuthTktAuthenticationPolicyDirective(unittest.TestCase): def test_it_defaults(self): from pyramid.interfaces import IAuthenticationPolicy reg = self.config.registry - context = reg.ctx + context = self.config._ctx self._callFUT(context, 'sosecret') actions = extract_actions(context.actions) self.assertEqual(len(actions), 1) @@ -368,7 +368,7 @@ class TestAuthTktAuthenticationPolicyDirective(unittest.TestCase): def test_it_noconfigerror(self): from pyramid.interfaces import IAuthenticationPolicy reg = self.config.registry - context = reg.ctx + context = self.config._ctx def callback(identity, request): """ """ self._callFUT(context, 'sosecret', callback=callback, @@ -389,7 +389,7 @@ class TestAuthTktAuthenticationPolicyDirective(unittest.TestCase): def test_it_configerror(self): from pyramid.exceptions import ConfigurationError - context = self.config.registry.ctx + context = self.config._ctx def callback(identity, request): """ """ self.assertRaises(ConfigurationError, @@ -415,7 +415,7 @@ class TestACLAuthorizationPolicyDirective(unittest.TestCase): from pyramid.authorization import ACLAuthorizationPolicy from pyramid.interfaces import IAuthorizationPolicy reg = self.config.registry - context = reg.ctx + context = self.config._ctx def callback(identity, request): """ """ self._callFUT(context) @@ -457,7 +457,7 @@ class TestRouteDirective(unittest.TestCase): from pyramid.interfaces import IViewClassifier from pyramid.interfaces import IRouteRequest reg = self.config.registry - context = reg.ctx + context = self.config._ctx view = lambda *arg: 'OK' self._callFUT(context, 'name', 'pattern', view=view) actions = extract_actions(context.actions) @@ -486,7 +486,7 @@ class TestRouteDirective(unittest.TestCase): from pyramid.interfaces import IViewClassifier from pyramid.interfaces import IRouteRequest reg = self.config.registry - context = reg.ctx + context = self.config._ctx view = lambda *arg: 'OK' self._callFUT(context, 'name', 'pattern', view=view, view_context=IDummy) @@ -516,7 +516,7 @@ class TestRouteDirective(unittest.TestCase): from pyramid.interfaces import IViewClassifier from pyramid.interfaces import IRouteRequest reg = self.config.registry - context = reg.ctx + context = self.config._ctx view = lambda *arg: 'OK' class Foo: pass @@ -553,7 +553,7 @@ class TestRouteDirective(unittest.TestCase): def renderer(path): return lambda *arg: 'OK' reg.registerUtility(renderer, IRendererFactory, name='.pt') - context = reg.ctx + context = self.config._ctx view = lambda *arg: 'OK' self._callFUT(context, 'name', 'pattern', view=view, @@ -587,7 +587,7 @@ class TestRouteDirective(unittest.TestCase): def pred2(context, request): pass preds = tuple(sorted([pred1, pred2])) - context = self.config.registry.ctx + context = self.config._ctx self._callFUT(context, 'name', 'pattern', custom_predicates=(pred1, pred2)) @@ -602,7 +602,7 @@ class TestRouteDirective(unittest.TestCase): self._assertRoute('name', 'pattern', 2) def test_with_path_argument_no_pattern(self): - context = self.config.registry.ctx + context = self.config._ctx self._callFUT(context, 'name', path='pattern') actions = extract_actions(context.actions) self.assertEqual(len(actions), 1) @@ -614,7 +614,7 @@ class TestRouteDirective(unittest.TestCase): self._assertRoute('name', 'pattern') def test_with_path_argument_and_pattern(self): - context = self.config.registry.ctx + context = self.config._ctx self._callFUT(context, 'name', pattern='pattern', path='path') actions = extract_actions(context.actions) self.assertEqual(len(actions), 1) @@ -628,7 +628,7 @@ class TestRouteDirective(unittest.TestCase): def test_with_neither_path_nor_pattern(self): from pyramid.exceptions import ConfigurationError - context = self.config.registry.ctx + context = self.config._ctx self.assertRaises(ConfigurationError, self._callFUT, context, 'name') class TestStaticDirective(unittest.TestCase): @@ -653,7 +653,7 @@ class TestStaticDirective(unittest.TestCase): from pyramid.interfaces import IRouteRequest from pyramid.interfaces import IRoutesMapper reg = self.config.registry - context = reg.ctx + context = self.config._ctx self._callFUT(context, 'name', 'fixtures/static') actions = extract_actions(context.actions) @@ -696,7 +696,7 @@ class TestStaticDirective(unittest.TestCase): from pyramid.interfaces import IRouteRequest from pyramid.interfaces import IRoutesMapper reg = self.config.registry - context = reg.ctx + context = self.config._ctx self._callFUT(context, 'name', 'fixtures/static', permission='aperm') actions = extract_actions(context.actions) self.assertEqual(len(actions), 2) @@ -739,7 +739,7 @@ class TestResourceDirective(unittest.TestCase): def test_it(self): import pyramid.tests - context = self.config.registry.ctx + context = self.config._ctx L = [] def dummy_override(*arg): L.append(arg) @@ -769,7 +769,7 @@ class TestRendererDirective(unittest.TestCase): def test_it(self): from pyramid.interfaces import IRendererFactory reg = self.config.registry - context = reg.ctx + context = self.config._ctx renderer = lambda *arg, **kw: None self._callFUT(context, renderer, 'r') actions = extract_actions(context.actions) @@ -847,7 +847,7 @@ class TestZCMLScanDirective(unittest.TestCase): foo.__venusian_callbacks__ = {'pyramid':[bar]} dummy_module.foo = foo - context = self.config.registry.ctx + context = self.config._ctx self._callFUT(context, dummy_module) self.assertEqual(dummy_module.scanned, True) @@ -954,39 +954,39 @@ class TestSubscriberDirective(unittest.TestCase): return subscriber(*arg, **kw) def test_no_factory_no_handler(self): - context = self.config.registry.ctx + context = self.config._ctx self.assertRaises(TypeError, self._callFUT, context, for_=None, factory=None, handler=None, provides=None) def test_handler_with_provides(self): - context = self.config.registry.ctx + context = self.config._ctx self.assertRaises(TypeError, self._callFUT, context, for_=None, factory=None, handler=1, provides=1) def test_handler_and_factory(self): - context = self.config.registry.ctx + context = self.config._ctx self.assertRaises(TypeError, self._callFUT, context, for_=None, factory=1, handler=1, provides=None) def test_no_provides_with_factory(self): - context = self.config.registry.ctx + context = self.config._ctx self.assertRaises(TypeError, self._callFUT, context, for_=None, factory=1, handler=None, provides=None) def test_adapted_by_as_for_is_None(self): - context = self.config.registry.ctx + context = self.config._ctx factory = DummyFactory() factory.__component_adapts__ = None self.assertRaises(TypeError, self._callFUT, context, for_=None, factory=factory, handler=None, provides=IFactory) def test_register_with_factory(self): - context = self.config.registry.ctx + context = self.config._ctx factory = DummyFactory() self._callFUT(context, for_=(IDummy,), factory=factory, handler=None, provides=IFactory) @@ -1001,7 +1001,7 @@ class TestSubscriberDirective(unittest.TestCase): ) def test_register_with_handler(self): - context = self.config.registry.ctx + context = self.config._ctx factory = DummyFactory() self._callFUT(context, for_=(IDummy,), factory=None, handler=factory) @@ -1010,9 +1010,11 @@ class TestSubscriberDirective(unittest.TestCase): subadapt = actions[0] self.assertEqual(subadapt['discriminator'], None) subadapt['callable'](*subadapt['args'], **subadapt['kw']) + registrations = self.config.registry._handler_registrations + self.assertEqual(len(registrations), 1) self.assertEqual( - self.config.registry._handler_registrations, - [((IDummy,), u'', factory, '')] + registrations[0][:3], + ((IDummy,), u'', factory) ) class TestUtilityDirective(unittest.TestCase): @@ -1027,17 +1029,17 @@ class TestUtilityDirective(unittest.TestCase): return utility(*arg, **kw) def test_factory_and_component(self): - context = self.config.registry.ctx + context = self.config._ctx self.assertRaises(TypeError, self._callFUT, context, factory=1, component=1) def test_missing_provides(self): - context = self.config.registry.ctx + context = self.config._ctx self.assertRaises(TypeError, self._callFUT, context, provides=None) def test_provides_from_factory_implements(self): from pyramid.registry import Registry - context = self.config.registry.ctx + context = self.config._ctx self._callFUT(context, factory=DummyFactory) actions = extract_actions(context.actions) self.assertEqual(len(actions), 1) @@ -1050,7 +1052,7 @@ class TestUtilityDirective(unittest.TestCase): def test_provides_from_component_provides(self): from pyramid.registry import Registry - context = self.config.registry.ctx + context = self.config._ctx component = DummyFactory() self._callFUT(context, component=component) actions = extract_actions(context.actions) @@ -1078,7 +1080,7 @@ class TestTranslationDirDirective(unittest.TestCase): here = os.path.dirname(__file__) expected = os.path.join(here, 'localeapp', 'locale') from pyramid.interfaces import ITranslationDirectories - context = self.config.registry.ctx + context = self.config._ctx tdir = 'pyramid.tests.localeapp:locale' self._callFUT(context, tdir) util = self.config.registry.getUtility(ITranslationDirectories) @@ -1097,7 +1099,7 @@ class TestLocaleNegotiatorDirective(unittest.TestCase): def test_it(self): from pyramid.interfaces import ILocaleNegotiator - context = self.config.registry.ctx + context = self.config._ctx dummy_negotiator = object() self._callFUT(context, dummy_negotiator) actions = extract_actions(context.actions) @@ -1123,7 +1125,7 @@ class TestDefaultPermissionDirective(unittest.TestCase): def test_it(self): from pyramid.interfaces import IDefaultPermission reg = self.config.registry - context = reg.ctx + context = self.config._ctx self._callFUT(context, 'view') actions = extract_actions(context.actions) self.assertEqual(len(actions), 1) @@ -1257,26 +1259,16 @@ class DummyPackage(object): def __init__(self, name): self.__name__ = name self.__file__ = '/__init__.py' - + def extract_actions(native): L = [] for action in native: - alen = len(action) d = {} d['discriminator'] = action[0] d['callable'] = action[1] - if alen >= 3: - d['args'] = action[2] - else: - d['args'] = () - if alen >= 4: - d['kw'] = action[3] - else: - d['kw'] = {} - if alen >= 5: - d['order'] = action[4] - else: - d['order'] = 99 + d['args'] = action[2] + d['kw'] = action[3] + d['order'] = action[4] L.append(d) return L diff --git a/pyramid/zcml.py b/pyramid/zcml.py index 0a0573396..81c230250 100644 --- a/pyramid/zcml.py +++ b/pyramid/zcml.py @@ -174,7 +174,7 @@ def view( context = context or for_ - config = Configurator(reg, package=_context.package) + config = Configurator(reg, package=_context.package, _ctx=_context) config.add_view( permission=permission, context=context, view=view, name=name, request_type=request_type, route_name=route_name, @@ -278,7 +278,7 @@ def route(_context, if pattern is None: raise ConfigurationError('route directive must include a "pattern"') - config = Configurator(reg, package=_context.package) + config = Configurator(reg, package=_context.package, _ctx=_context) config.add_route( name, pattern, @@ -331,7 +331,7 @@ def notfound(_context, reg = _context.registry except AttributeError: # pragma: no cover (b/c) reg = get_current_registry() - config = Configurator(reg, package=_context.package) + config = Configurator(reg, package=_context.package, _ctx=_context) config.set_notfound_view(view=view, attr=attr, renderer=renderer, wrapper=wrapper) @@ -346,7 +346,7 @@ def forbidden(_context, reg = _context.registry except AttributeError: # pragma: no cover (b/c) reg = get_current_registry() - config = Configurator(reg, package=_context.package) + config = Configurator(reg, package=_context.package, _ctx=_context) config.set_forbidden_view(view=view, attr=attr, renderer=renderer, wrapper=wrapper) @@ -371,7 +371,7 @@ def resource(_context, to_override, override_with, _override=None): except AttributeError: # pragma: no cover (b/c) reg = get_current_registry() - config = Configurator(reg, package=_context.package) + config = Configurator(reg, package=_context.package, _ctx=_context) config.override_resource(to_override, override_with, _override=_override) class IRepozeWho1AuthenticationPolicyDirective(Interface): @@ -389,7 +389,7 @@ def repozewho1authenticationpolicy(_context, identifier_name='auth_tkt', reg = _context.registry except AttributeError: # pragma: no cover (b/c) reg = get_current_registry() - config = Configurator(reg, package=_context.package) + config = Configurator(reg, package=_context.package, _ctx=_context) config._set_authentication_policy(policy) class IRemoteUserAuthenticationPolicyDirective(Interface): @@ -407,7 +407,7 @@ def remoteuserauthenticationpolicy(_context, environ_key='REMOTE_USER', reg = _context.registry except AttributeError: # pragma: no cover (b/c) reg = get_current_registry() - config = Configurator(reg, package=_context.package) + config = Configurator(reg, package=_context.package, _ctx=_context) config._set_authentication_policy(policy) class IAuthTktAuthenticationPolicyDirective(Interface): @@ -453,7 +453,7 @@ def authtktauthenticationpolicy(_context, reg = _context.registry except AttributeError: # pragma: no cover (b/c) reg = get_current_registry() - config = Configurator(reg, package=_context.package) + config = Configurator(reg, package=_context.package, _ctx=_context) config._set_authentication_policy(policy) class IACLAuthorizationPolicyDirective(Interface): @@ -467,7 +467,7 @@ def aclauthorizationpolicy(_context): reg = _context.registry except AttributeError: # pragma: no cover (b/c) reg = get_current_registry() - config = Configurator(reg, package=_context.package) + config = Configurator(reg, package=_context.package, _ctx=_context) config._set_authorization_policy(policy) class IRendererDirective(Interface): @@ -486,7 +486,7 @@ def renderer(_context, factory, name=''): reg = _context.registry except AttributeError: # pragma: no cover (b/c) reg = get_current_registry() - config = Configurator(reg, package=_context.package) + config = Configurator(reg, package=_context.package, _ctx=_context) config.add_renderer(name, factory) class IStaticDirective(Interface): @@ -521,7 +521,7 @@ def static(_context, name, path, cache_max_age=3600, reg = _context.registry except AttributeError: # pragma: no cover (b/c) reg = get_current_registry() - config = Configurator(reg, package=_context.package) + config = Configurator(reg, package=_context.package, _ctx=_context) config.add_static_view(name, path, cache_max_age=cache_max_age, permission=permission) @@ -536,7 +536,7 @@ def scan(_context, package): reg = _context.registry except AttributeError: # pragma: no cover (b/c) reg = get_current_registry() - config = Configurator(reg, package=_context.package) + config = Configurator(reg, package=_context.package, _ctx=_context) config.scan(package) class ITranslationDirDirective(Interface): @@ -553,7 +553,7 @@ def translationdir(_context, dir): except AttributeError: # pragma: no cover (b/c) reg = get_current_registry() - config = Configurator(reg, package=_context.package) + config = Configurator(reg, package=_context.package, _ctx=_context) config.add_translation_dirs(path) class ILocaleNegotiatorDirective(Interface): @@ -568,7 +568,7 @@ def localenegotiator(_context, negotiator): reg = _context.registry except AttributeError: # pragma: no cover (b/c) reg = get_current_registry() - config = Configurator(reg, package=_context.package) + config = Configurator(reg, package=_context.package, _ctx=_context) config.set_locale_negotiator(negotiator) class IAdapterDirective(Interface): @@ -712,7 +712,8 @@ def subscriber(_context, for_=None, factory=None, handler=None, provides=None): except AttributeError: # pragma: no cover (b/c) registry = get_current_registry() - config = Configurator(registry=registry, package=_context.package) + config = Configurator(registry=registry, package=_context.package, + _ctx=_context) if handler is not None: config.add_subscriber(handler, for_) @@ -802,7 +803,7 @@ def default_permission(_context, name): reg = _context.registry except AttributeError: # pragma: no cover (b/c) reg = get_current_registry() - config = Configurator(reg, package=_context.package) + config = Configurator(reg, package=_context.package, _ctx=_context) config.set_default_permission(name) def path_spec(context, path): |
