summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChris McDonough <chrism@plope.com>2011-08-10 18:21:47 -0400
committerChris McDonough <chrism@plope.com>2011-08-10 18:21:47 -0400
commit04ca37b938fc99c05b00e4cb817dc0fe1bebc21b (patch)
treeb08e6a6b99191efd8d0e2fec3850145f118928e1
parentbfbfd803d071529f07cbfb58216b8d32883ddb52 (diff)
downloadpyramid-04ca37b938fc99c05b00e4cb817dc0fe1bebc21b.tar.gz
pyramid-04ca37b938fc99c05b00e4cb817dc0fe1bebc21b.tar.bz2
pyramid-04ca37b938fc99c05b00e4cb817dc0fe1bebc21b.zip
remove bogus sortroots; add some more ordering tests; prevent people from attempting to add tweens over ingress or under main
-rw-r--r--pyramid/config.py8
-rw-r--r--pyramid/tests/test_config.py16
-rw-r--r--pyramid/tests/test_tweens.py41
-rw-r--r--pyramid/tweens.py31
4 files changed, 72 insertions, 24 deletions
diff --git a/pyramid/config.py b/pyramid/config.py
index d3789bca4..917b689b1 100644
--- a/pyramid/config.py
+++ b/pyramid/config.py
@@ -993,7 +993,13 @@ class Configurator(object):
name = tween_factory_name(tween_factory)
if alias in (MAIN, INGRESS):
raise ConfigurationError('%s is a reserved tween name' % alias)
-
+
+ if over is INGRESS:
+ raise ConfigurationError('%s cannot be over INGRESS')
+
+ if under is MAIN:
+ raise ConfigurationError('%s cannot be under MAIN')
+
registry = self.registry
tweens = registry.queryUtility(ITweens)
if tweens is None:
diff --git a/pyramid/tests/test_config.py b/pyramid/tests/test_config.py
index 3febe86bd..5d7caef03 100644
--- a/pyramid/tests/test_config.py
+++ b/pyramid/tests/test_config.py
@@ -743,6 +743,22 @@ pyramid.tests.test_config.dummy_include2""",
config.add_tween(atween2, alias='a')
self.assertRaises(ConfigurationConflictError, config.commit)
+ def test_add_tween_over_ingress(self):
+ from pyramid.exceptions import ConfigurationError
+ from pyramid.tweens import INGRESS
+ config = self._makeOne()
+ self.assertRaises(ConfigurationError,
+ config.add_tween, 'pyramid.tests.test_config.dummy_tween_factory',
+ over=INGRESS)
+
+ def test_add_tween_under_main(self):
+ from pyramid.exceptions import ConfigurationError
+ from pyramid.tweens import MAIN
+ config = self._makeOne()
+ self.assertRaises(ConfigurationError,
+ config.add_tween, 'pyramid.tests.test_config.dummy_tween_factory',
+ under=MAIN)
+
def test_add_subscriber_defaults(self):
from zope.interface import implements
from zope.interface import Interface
diff --git a/pyramid/tests/test_tweens.py b/pyramid/tests/test_tweens.py
index b18334f74..5fa999e8a 100644
--- a/pyramid/tests/test_tweens.py
+++ b/pyramid/tests/test_tweens.py
@@ -15,7 +15,6 @@ class TestTweens(unittest.TestCase):
def test_add_implicit_noaliases(self):
from pyramid.tweens import INGRESS
- from pyramid.tweens import MAIN
tweens = self._makeOne()
tweens.add_implicit('name', 'factory')
self.assertEqual(tweens.names, ['name'])
@@ -174,6 +173,26 @@ class TestTweens(unittest.TestCase):
('txnmgr', 'txnmgr_factory'),
])
+ def test_implicit_ordering_5(self):
+ from pyramid.tweens import MAIN, INGRESS
+ tweens = self._makeOne()
+ add = tweens.add_implicit
+ add('exceptionview', 'excview_factory', over=MAIN)
+ add('auth', 'auth_factory', under=INGRESS)
+ add('retry', 'retry_factory', over='txnmgr', under='exceptionview')
+ add('browserid', 'browserid_factory', under=INGRESS)
+ add('txnmgr', 'txnmgr_factory', under='exceptionview', over=MAIN)
+ add('dbt', 'dbt_factory')
+ self.assertEqual(tweens.implicit(),
+ [
+ ('dbt', 'dbt_factory'),
+ ('browserid', 'browserid_factory'),
+ ('auth', 'auth_factory'),
+ ('exceptionview', 'excview_factory'),
+ ('retry', 'retry_factory'),
+ ('txnmgr', 'txnmgr_factory'),
+ ])
+
def test_implicit_ordering_withaliases(self):
from pyramid.tweens import MAIN
tweens = self._makeOne()
@@ -194,6 +213,26 @@ class TestTweens(unittest.TestCase):
('txnmgr', 'txnmgr_factory'),
])
+ def test_implicit_ordering_withaliases2(self):
+ from pyramid.tweens import MAIN
+ tweens = self._makeOne()
+ add = tweens.add_implicit
+ add('exceptionview', 'excview_factory', alias='e', over=MAIN)
+ add('auth', 'auth_factory', alias='a', under='b')
+ add('retry', 'retry_factory', alias='r', over='t', under='e')
+ add('browserid', 'browserid_factory', alias='b')
+ add('txnmgr', 'txnmgr_factory', alias='t', under='e')
+ add('dbt', 'dbt_factory', alias='d')
+ self.assertEqual(tweens.implicit(),
+ [
+ ('dbt', 'dbt_factory'),
+ ('browserid', 'browserid_factory'),
+ ('auth', 'auth_factory'),
+ ('exceptionview', 'excview_factory'),
+ ('retry', 'retry_factory'),
+ ('txnmgr', 'txnmgr_factory'),
+ ])
+
def test_implicit_ordering_missing_partial(self):
from pyramid.tweens import MAIN
tweens = self._makeOne()
diff --git a/pyramid/tweens.py b/pyramid/tweens.py
index a9426c456..90ff67267 100644
--- a/pyramid/tweens.py
+++ b/pyramid/tweens.py
@@ -87,7 +87,7 @@ class Tweens(object):
self.order.append((alias, over))
def implicit(self):
- order = []
+ order = [(INGRESS, MAIN)]
roots = []
graph = {}
has_order = {}
@@ -103,12 +103,12 @@ class Tweens(object):
b = self.name_to_alias.get(b, b)
order.append((a, b))
- def add_node(graph, node):
+ def add_node(node):
if not graph.has_key(node):
roots.append(node)
graph[node] = [0] # 0 = number of arcs coming into this node
- def add_arc(graph, fromnode, tonode):
+ def add_arc(fromnode, tonode):
graph[fromnode].append(tonode)
graph[tonode][0] += 1
if tonode in roots:
@@ -123,31 +123,18 @@ class Tweens(object):
else:
has_order[first] = has_order[second] = True
- for v in aliases:
+ for alias in aliases:
# any alias that doesn't have an ordering after we detect all
# nodes with orders should get an ordering relative to INGRESS,
# as if it were added with no under or over in add_implicit
- if (not v in has_order) and (v not in (INGRESS, MAIN)):
- order.append((INGRESS, v))
- ingress_alias_names.append(v)
- add_node(graph, v)
+ if (not alias in has_order) and (alias not in (INGRESS, MAIN)):
+ order.append((INGRESS, alias))
+ ingress_alias_names.append(alias)
+ add_node(alias)
for a, b in order:
if a is not None and b is not None: # deal with removed orders
- add_arc(graph, a, b)
-
- def sortroots(alias):
- # sort roots so that roots (and their children) that depend only
- # on the ingress sort nearer the (nearer the ingress)
- if alias in ingress_alias_names:
- return -1
- children = graph[alias][1:]
- for child in children:
- if sortroots(child) == -1:
- return -1
- return 1
-
- roots.sort(key=sortroots)
+ add_arc(a, b)
sorted_aliases = []