diff options
| author | Chris McDonough <chrism@agendaless.com> | 2009-06-22 02:49:41 +0000 |
|---|---|---|
| committer | Chris McDonough <chrism@agendaless.com> | 2009-06-22 02:49:41 +0000 |
| commit | d70fd93e4b76369edea80ac1ded9da9ac0a93289 (patch) | |
| tree | 988fb7bc31ea1a19616f9ef82aab3e478170dc09 | |
| parent | 1cd5984995fefa64bdb7bdd47e6e09e8d424f6d2 (diff) | |
| download | pyramid-d70fd93e4b76369edea80ac1ded9da9ac0a93289.tar.gz pyramid-d70fd93e4b76369edea80ac1ded9da9ac0a93289.tar.bz2 pyramid-d70fd93e4b76369edea80ac1ded9da9ac0a93289.zip | |
More micro-optimizations to the default traverser.
Fix registerModels to use the right traversal info names.
| -rw-r--r-- | repoze/bfg/testing.py | 7 | ||||
| -rw-r--r-- | repoze/bfg/tests/test_testing.py | 16 | ||||
| -rw-r--r-- | repoze/bfg/traversal.py | 80 |
3 files changed, 55 insertions, 48 deletions
diff --git a/repoze/bfg/testing.py b/repoze/bfg/testing.py index 52c383b65..6f33172d6 100644 --- a/repoze/bfg/testing.py +++ b/repoze/bfg/testing.py @@ -44,9 +44,10 @@ def registerModels(models): path = environ['PATH_INFO'] ob = models[path] from repoze.bfg.traversal import traversal_path - traversed = list(traversal_path(path)) - return {'context':ob, 'view_name':'','subpath':[], - 'traversed':traversed, 'vroot':ob, 'vroot_path':[]} + traversed = traversal_path(path) + return {'context':ob, 'view_name':'','subpath':(), + 'traversed':traversed, 'virtual_root':ob, + 'virtual_root_path':(), 'root':ob} registerTraverserFactory(DummyTraverserFactory) return models diff --git a/repoze/bfg/tests/test_testing.py b/repoze/bfg/tests/test_testing.py index ca6f517be..4ad3c83f6 100644 --- a/repoze/bfg/tests/test_testing.py +++ b/repoze/bfg/tests/test_testing.py @@ -35,17 +35,17 @@ class TestTestingFunctions(unittest.TestCase): result = adapter({'PATH_INFO':'/ob1'}) self.assertEqual(result['context'], ob1) self.assertEqual(result['view_name'], '') - self.assertEqual(result['subpath'], []) - self.assertEqual(result['traversed'], [u'ob1']) - self.assertEqual(result['vroot'], ob1) - self.assertEqual(result['vroot_path'], []) + self.assertEqual(result['subpath'], ()) + self.assertEqual(result['traversed'], (u'ob1',)) + self.assertEqual(result['virtual_root'], ob1) + self.assertEqual(result['virtual_root_path'], ()) result = adapter({'PATH_INFO':'/ob2'}) self.assertEqual(result['context'], ob2) self.assertEqual(result['view_name'], '') - self.assertEqual(result['subpath'], []) - self.assertEqual(result['traversed'], [u'ob2']) - self.assertEqual(result['vroot'], ob2) - self.assertEqual(result['vroot_path'], []) + self.assertEqual(result['subpath'], ()) + self.assertEqual(result['traversed'], (u'ob2',)) + self.assertEqual(result['virtual_root'], ob2) + self.assertEqual(result['virtual_root_path'], ()) self.assertRaises(KeyError, adapter, {'PATH_INFO':'/ob3'}) from repoze.bfg.traversal import find_model self.assertEqual(find_model(None, '/ob1'), ob1) diff --git a/repoze/bfg/traversal.py b/repoze/bfg/traversal.py index 428553ec6..59ad9115d 100644 --- a/repoze/bfg/traversal.py +++ b/repoze/bfg/traversal.py @@ -516,45 +516,51 @@ class ModelGraphTraverser(object): vroot_idx = len(vroot_path) path = vroot_path_string + path - path = traversal_path(path) ob = vroot = self.root - # in case you're wondering, we do dead reckoning here instead - # of pushing and popping temporary lists for speed purposes - - i = 1 - j = vroot_idx - - for segment in path: - if segment[:2] =='@@': - return dict(context=ob, view_name=segment[2:], - subpath=path[i:], traversed=path[:j], - virtual_root=vroot, - virtual_root_path=vroot_path, - root=self.root) - try: - getitem = ob.__getitem__ - except AttributeError: - return dict(context=ob, view_name=segment, - subpath=path[i:], traversed=path[:j], - virtual_root=vroot, - virtual_root_path=vroot_path, - root=self.root) - - try: - next = getitem(segment) - except KeyError: - return dict(context=ob, view_name=segment, - subpath=path[i:], traversed=path[:j], - virtual_root=vroot, - virtual_root_path=vroot_path, - root=self.root) - - if vroot_idx == i-1: - vroot = ob - ob = next - i += 1 - j += 1 + if (not path) or path == '/': + # save a call to traversal_path if we know it's going to return + # the empty tuple + path = () + else: + path = traversal_path(path) + + # in case you're wondering, we do dead reckoning here instead + # of pushing and popping temporary lists for speed purposes + + i = 1 + j = vroot_idx + + for segment in path: + if segment[:2] =='@@': + return dict(context=ob, view_name=segment[2:], + subpath=path[i:], traversed=path[:j], + virtual_root=vroot, + virtual_root_path=vroot_path, + root=self.root) + try: + getitem = ob.__getitem__ + except AttributeError: + return dict(context=ob, view_name=segment, + subpath=path[i:], traversed=path[:j], + virtual_root=vroot, + virtual_root_path=vroot_path, + root=self.root) + + try: + next = getitem(segment) + except KeyError: + return dict(context=ob, view_name=segment, + subpath=path[i:], traversed=path[:j], + virtual_root=vroot, + virtual_root_path=vroot_path, + root=self.root) + + if vroot_idx == i-1: + vroot = ob + ob = next + i += 1 + j += 1 return dict(context=ob, view_name=u'', subpath=subpath, traversed=path, virtual_root=vroot, |
