summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--pyramid/router.py10
-rw-r--r--pyramid/tests/test_router.py4
-rw-r--r--pyramid/tests/test_traversal.py157
-rw-r--r--pyramid/traversal.py20
4 files changed, 80 insertions, 111 deletions
diff --git a/pyramid/router.py b/pyramid/router.py
index 0c115a1ac..3491884b8 100644
--- a/pyramid/router.py
+++ b/pyramid/router.py
@@ -84,13 +84,6 @@ class Router(object):
request.url)
logger and logger.debug(msg)
else:
- # TODO: kill off bfg.routes.* environ keys
- # when traverser requires request arg, and
- # cant cope with environ anymore (they are
- # docs-deprecated as of BFG 1.3)
- environ = request.environ
- environ['bfg.routes.route'] = route
- environ['bfg.routes.matchdict'] = match
attrs['matchdict'] = match
attrs['matched_route'] = route
@@ -105,7 +98,8 @@ class Router(object):
request.url,
route.name,
request.path_info,
- route.pattern, match,
+ route.pattern,
+ match,
', '.join([p.__text__ for p in route.predicates]))
)
logger and logger.debug(msg)
diff --git a/pyramid/tests/test_router.py b/pyramid/tests/test_router.py
index eb9b7285d..6c68d548c 100644
--- a/pyramid/tests/test_router.py
+++ b/pyramid/tests/test_router.py
@@ -646,8 +646,6 @@ class TestRouter(unittest.TestCase):
self.assertEqual(request.context, context)
self.assertEqual(request.root, root)
matchdict = {'action':'action1', 'article':'article1'}
- self.assertEqual(environ['bfg.routes.matchdict'], matchdict)
- self.assertEqual(environ['bfg.routes.route'].name, 'foo')
self.assertEqual(request.matchdict, matchdict)
self.assertEqual(request.matched_route.name, 'foo')
self.assertEqual(len(logger.messages), 1)
@@ -712,8 +710,6 @@ class TestRouter(unittest.TestCase):
self.assertEqual(request.context, context)
self.assertEqual(request.root, root)
matchdict = {'action':'action1', 'article':'article1'}
- self.assertEqual(environ['bfg.routes.matchdict'], matchdict)
- self.assertEqual(environ['bfg.routes.route'].name, 'foo')
self.assertEqual(request.matchdict, matchdict)
self.assertEqual(request.matched_route.name, 'foo')
self.assertTrue(IFoo.providedBy(request))
diff --git a/pyramid/tests/test_traversal.py b/pyramid/tests/test_traversal.py
index 8e0bb2494..40893bfcf 100644
--- a/pyramid/tests/test_traversal.py
+++ b/pyramid/tests/test_traversal.py
@@ -71,6 +71,8 @@ class TraversalPathInfoTests(unittest.TestCase):
self.assertEqual(self._callFUT('../../bar'), (text_('bar'),))
def test_segments_are_unicode(self):
+ # breaks because lru_cached holds on to strings? possibly from
+ # other tests. not good.
result = self._callFUT('/foo/bar')
self.assertEqual(type(result[0]), text_type)
self.assertEqual(type(result[1]), text_type)
@@ -142,31 +144,25 @@ class ResourceTreeTraverserTests(unittest.TestCase):
self.assertEqual(result['virtual_root_path'], ())
def test_call_with_pathinfo_highorder(self):
- foo = DummyContext(None, text_(b'Qu\xc3\xa9bec', 'utf-8'))
+ path = text_(b'/Qu\xc3\xa9bec', 'utf-8')
+ foo = DummyContext(None, path)
root = DummyContext(foo, 'root')
policy = self._makeOne(root)
- if PY3: # pragma: no cover
- path_info = b'/Qu\xc3\xa9bec'.decode('latin-1')
- else:
- path_info = b'/Qu\xc3\xa9bec'
- environ = self._getEnviron(PATH_INFO=path_info)
- request = DummyRequest(environ)
+ environ = self._getEnviron()
+ request = DummyRequest(environ, path_info=path)
result = policy(request)
self.assertEqual(result['context'], foo)
self.assertEqual(result['view_name'], '')
self.assertEqual(result['subpath'], ())
- self.assertEqual(
- result['traversed'],
- (text_(b'Qu\xc3\xa9bec', 'utf-8'),)
- )
+ self.assertEqual(result['traversed'], (path[1:],))
self.assertEqual(result['root'], policy.root)
self.assertEqual(result['virtual_root'], policy.root)
self.assertEqual(result['virtual_root_path'], ())
def test_call_pathel_with_no_getitem(self):
policy = self._makeOne(None)
- environ = self._getEnviron(PATH_INFO='/foo/bar')
- request = DummyRequest(environ)
+ environ = self._getEnviron()
+ request = DummyRequest(environ, path_info='/foo/bar')
result = policy(request)
self.assertEqual(result['context'], None)
self.assertEqual(result['view_name'], 'foo')
@@ -179,8 +175,8 @@ class ResourceTreeTraverserTests(unittest.TestCase):
def test_call_withconn_getitem_emptypath_nosubpath(self):
root = DummyContext()
policy = self._makeOne(root)
- environ = self._getEnviron(PATH_INFO='')
- request = DummyRequest(environ)
+ environ = self._getEnviron()
+ request = DummyRequest(environ, path_info='')
result = policy(request)
self.assertEqual(result['context'], root)
self.assertEqual(result['view_name'], '')
@@ -194,8 +190,8 @@ class ResourceTreeTraverserTests(unittest.TestCase):
foo = DummyContext()
root = DummyContext(foo)
policy = self._makeOne(root)
- environ = self._getEnviron(PATH_INFO='/foo/bar')
- request = DummyRequest(environ)
+ environ = self._getEnviron()
+ request = DummyRequest(environ, path_info='/foo/bar')
result = policy(request)
self.assertEqual(result['context'], foo)
self.assertEqual(result['view_name'], 'bar')
@@ -209,8 +205,8 @@ class ResourceTreeTraverserTests(unittest.TestCase):
foo = DummyContext()
root = DummyContext(foo)
policy = self._makeOne(root)
- environ = self._getEnviron(PATH_INFO='/foo/bar/baz/buz')
- request = DummyRequest(environ)
+ environ = self._getEnviron()
+ request = DummyRequest(environ, path_info='/foo/bar/baz/buz')
result = policy(request)
self.assertEqual(result['context'], foo)
self.assertEqual(result['view_name'], 'bar')
@@ -224,8 +220,8 @@ class ResourceTreeTraverserTests(unittest.TestCase):
foo = DummyContext()
root = DummyContext(foo)
policy = self._makeOne(root)
- environ = self._getEnviron(PATH_INFO='/@@foo')
- request = DummyRequest(environ)
+ environ = self._getEnviron()
+ request = DummyRequest(environ, path_info='/@@foo')
result = policy(request)
self.assertEqual(result['context'], root)
self.assertEqual(result['view_name'], 'foo')
@@ -236,14 +232,13 @@ class ResourceTreeTraverserTests(unittest.TestCase):
self.assertEqual(result['virtual_root_path'], ())
def test_call_with_vh_root(self):
- environ = self._getEnviron(PATH_INFO='/baz',
- HTTP_X_VHM_ROOT='/foo/bar')
+ environ = self._getEnviron(HTTP_X_VHM_ROOT='/foo/bar')
baz = DummyContext(None, 'baz')
bar = DummyContext(baz, 'bar')
foo = DummyContext(bar, 'foo')
root = DummyContext(foo, 'root')
policy = self._makeOne(root)
- request = DummyRequest(environ)
+ request = DummyRequest(environ, path_info='/baz')
result = policy(request)
self.assertEqual(result['context'], baz)
self.assertEqual(result['view_name'], '')
@@ -256,14 +251,13 @@ class ResourceTreeTraverserTests(unittest.TestCase):
(text_('foo'), text_('bar')))
def test_call_with_vh_root2(self):
- environ = self._getEnviron(PATH_INFO='/bar/baz',
- HTTP_X_VHM_ROOT='/foo')
+ environ = self._getEnviron(HTTP_X_VHM_ROOT='/foo')
baz = DummyContext(None, 'baz')
bar = DummyContext(baz, 'bar')
foo = DummyContext(bar, 'foo')
root = DummyContext(foo, 'root')
policy = self._makeOne(root)
- request = DummyRequest(environ)
+ request = DummyRequest(environ, path_info='/bar/baz')
result = policy(request)
self.assertEqual(result['context'], baz)
self.assertEqual(result['view_name'], '')
@@ -275,14 +269,13 @@ class ResourceTreeTraverserTests(unittest.TestCase):
self.assertEqual(result['virtual_root_path'], (text_('foo'),))
def test_call_with_vh_root3(self):
- environ = self._getEnviron(PATH_INFO='/foo/bar/baz',
- HTTP_X_VHM_ROOT='/')
+ environ = self._getEnviron(HTTP_X_VHM_ROOT='/')
baz = DummyContext()
bar = DummyContext(baz)
foo = DummyContext(bar)
root = DummyContext(foo)
policy = self._makeOne(root)
- request = DummyRequest(environ)
+ request = DummyRequest(environ, path_info='/foo/bar/baz')
result = policy(request)
self.assertEqual(result['context'], baz)
self.assertEqual(result['view_name'], '')
@@ -294,14 +287,13 @@ class ResourceTreeTraverserTests(unittest.TestCase):
self.assertEqual(result['virtual_root_path'], ())
def test_call_with_vh_root4(self):
- environ = self._getEnviron(PATH_INFO='/',
- HTTP_X_VHM_ROOT='/foo/bar/baz')
+ environ = self._getEnviron(HTTP_X_VHM_ROOT='/foo/bar/baz')
baz = DummyContext(None, 'baz')
bar = DummyContext(baz, 'bar')
foo = DummyContext(bar, 'foo')
root = DummyContext(foo, 'root')
policy = self._makeOne(root)
- request = DummyRequest(environ)
+ request = DummyRequest(environ, path_info='/')
result = policy(request)
self.assertEqual(result['context'], baz)
self.assertEqual(result['view_name'], '')
@@ -315,9 +307,8 @@ class ResourceTreeTraverserTests(unittest.TestCase):
def test_call_with_vh_root_path_root(self):
policy = self._makeOne(None)
- environ = self._getEnviron(HTTP_X_VHM_ROOT='/',
- PATH_INFO='/')
- request = DummyRequest(environ)
+ environ = self._getEnviron(HTTP_X_VHM_ROOT='/')
+ request = DummyRequest(environ, path_info='/')
result = policy(request)
self.assertEqual(result['context'], None)
self.assertEqual(result['view_name'], '')
@@ -328,57 +319,48 @@ class ResourceTreeTraverserTests(unittest.TestCase):
self.assertEqual(result['virtual_root_path'], ())
def test_call_with_vh_root_highorder(self):
+ path = text_(b'Qu\xc3\xa9bec', 'utf-8')
bar = DummyContext(None, 'bar')
- foo = DummyContext(bar, text_(b'Qu\xc3\xa9bec', 'utf-8'))
+ foo = DummyContext(bar, path)
root = DummyContext(foo, 'root')
policy = self._makeOne(root)
if PY3: # pragma: no cover
vhm_root = b'/Qu\xc3\xa9bec'.decode('latin-1')
else:
vhm_root = b'/Qu\xc3\xa9bec'
- environ = self._getEnviron(HTTP_X_VHM_ROOT=vhm_root,
- PATH_INFO='/bar')
- request = DummyRequest(environ)
+ environ = self._getEnviron(HTTP_X_VHM_ROOT=vhm_root)
+ request = DummyRequest(environ, path_info='/bar')
result = policy(request)
self.assertEqual(result['context'], bar)
self.assertEqual(result['view_name'], '')
self.assertEqual(result['subpath'], ())
self.assertEqual(
result['traversed'],
- (text_(b'Qu\xc3\xa9bec', 'utf-8'), text_('bar'))
+ (path, text_('bar'))
)
self.assertEqual(result['root'], policy.root)
self.assertEqual(result['virtual_root'], foo)
self.assertEqual(
result['virtual_root_path'],
- (text_(b'Qu\xc3\xa9bec', 'utf-8'),)
+ (path,)
)
- def test_non_utf8_path_segment_unicode_path_segments_fails(self):
+ def test_path_info_raises_unicodedecodeerror(self):
from pyramid.exceptions import URLDecodeError
foo = DummyContext()
root = DummyContext(foo)
policy = self._makeOne(root)
- segment = native_(text_(b'LaPe\xc3\xb1a', 'utf-8'), 'utf-16')
- environ = self._getEnviron(PATH_INFO='/%s' % segment)
- request = DummyRequest(environ)
- self.assertRaises(URLDecodeError, policy, request)
-
- def test_non_utf8_path_segment_settings_unicode_path_segments_fails(self):
- from pyramid.exceptions import URLDecodeError
- foo = DummyContext()
- root = DummyContext(foo)
- policy = self._makeOne(root)
- segment = native_(text_(b'LaPe\xc3\xb1a', 'utf-8'), 'utf-16')
- environ = self._getEnviron(PATH_INFO='/%s' % segment)
- request = DummyRequest(environ)
+ environ = self._getEnviron()
+ toraise = UnicodeDecodeError('ascii', 'a', 2, 3, '5')
+ request = DummyRequest(environ, toraise=toraise)
+ request.matchdict = None
self.assertRaises(URLDecodeError, policy, request)
def test_withroute_nothingfancy(self):
resource = DummyContext()
traverser = self._makeOne(resource)
- environ = {'bfg.routes.matchdict': {}}
- request = DummyRequest(environ)
+ request = DummyRequest({})
+ request.matchdict = {}
result = traverser(request)
self.assertEqual(result['context'], resource)
self.assertEqual(result['view_name'], '')
@@ -391,8 +373,9 @@ class ResourceTreeTraverserTests(unittest.TestCase):
def test_withroute_with_subpath_string(self):
resource = DummyContext()
traverser = self._makeOne(resource)
- environ = {'bfg.routes.matchdict': {'subpath':'/a/b/c'}}
- request = DummyRequest(environ)
+ matchdict = {'subpath':'/a/b/c'}
+ request = DummyRequest({})
+ request.matchdict = matchdict
result = traverser(request)
self.assertEqual(result['context'], resource)
self.assertEqual(result['view_name'], '')
@@ -405,8 +388,9 @@ class ResourceTreeTraverserTests(unittest.TestCase):
def test_withroute_with_subpath_tuple(self):
resource = DummyContext()
traverser = self._makeOne(resource)
- environ = {'bfg.routes.matchdict': {'subpath':('a', 'b', 'c')}}
- request = DummyRequest(environ)
+ matchdict = {'subpath':('a', 'b', 'c')}
+ request = DummyRequest({})
+ request.matchdict = matchdict
result = traverser(request)
self.assertEqual(result['context'], resource)
self.assertEqual(result['view_name'], '')
@@ -419,8 +403,9 @@ class ResourceTreeTraverserTests(unittest.TestCase):
def test_withroute_and_traverse_string(self):
resource = DummyContext()
traverser = self._makeOne(resource)
- environ = {'bfg.routes.matchdict': {'traverse':'foo/bar'}}
- request = DummyRequest(environ)
+ matchdict = {'traverse':'foo/bar'}
+ request = DummyRequest({})
+ request.matchdict = matchdict
result = traverser(request)
self.assertEqual(result['context'], resource)
self.assertEqual(result['view_name'], 'foo')
@@ -433,8 +418,9 @@ class ResourceTreeTraverserTests(unittest.TestCase):
def test_withroute_and_traverse_tuple(self):
resource = DummyContext()
traverser = self._makeOne(resource)
- environ = {'bfg.routes.matchdict': {'traverse':('foo', 'bar')}}
- request = DummyRequest(environ)
+ matchdict = {'traverse':('foo', 'bar')}
+ request = DummyRequest({})
+ request.matchdict = matchdict
result = traverser(request)
self.assertEqual(result['context'], resource)
self.assertEqual(result['view_name'], 'foo')
@@ -447,8 +433,9 @@ class ResourceTreeTraverserTests(unittest.TestCase):
def test_withroute_and_traverse_empty(self):
resource = DummyContext()
traverser = self._makeOne(resource)
- environ = {'bfg.routes.matchdict': {'traverse':''}}
- request = DummyRequest(environ)
+ matchdict = {'traverse':''}
+ request = DummyRequest({})
+ request.matchdict = matchdict
result = traverser(request)
self.assertEqual(result['context'], resource)
self.assertEqual(result['view_name'], '')
@@ -458,21 +445,6 @@ class ResourceTreeTraverserTests(unittest.TestCase):
self.assertEqual(result['virtual_root'], resource)
self.assertEqual(result['virtual_root_path'], ())
- def test_call_with_environ(self):
- with warnings.catch_warnings(record=True) as w:
- warnings.filterwarnings('always')
- policy = self._makeOne(None)
- environ = self._getEnviron()
- result = policy(environ)
- self.assertEqual(result['context'], None)
- self.assertEqual(result['view_name'], '')
- self.assertEqual(result['subpath'], ())
- self.assertEqual(result['traversed'], ())
- self.assertEqual(result['root'], policy.root)
- self.assertEqual(result['virtual_root'], policy.root)
- self.assertEqual(result['virtual_root_path'], ())
- self.assertEqual(len(w), 1)
-
class FindInterfaceTests(unittest.TestCase):
def _callFUT(self, context, iface):
from pyramid.traversal import find_interface
@@ -1310,11 +1282,28 @@ class DummyContext(object):
return '<DummyContext with name %s at id %s>'%(self.__name__, id(self))
class DummyRequest:
+
application_url = 'http://example.com:5432' # app_url never ends with slash
- def __init__(self, environ=None):
+ matchdict = None
+ matched_route = None
+
+ def __init__(self, environ=None, path_info='/', toraise=None):
if environ is None:
environ = {}
self.environ = environ
+ self._set_path_info(path_info)
+ self.toraise = toraise
+
+ def _get_path_info(self):
+ if self.toraise:
+ raise self.toraise
+ return self._path_info
+
+ def _set_path_info(self, v):
+ self._path_info = v
+
+ path_info = property(_get_path_info, _set_path_info)
+
class DummyContextURL:
def __init__(self, context, request):
diff --git a/pyramid/traversal.py b/pyramid/traversal.py
index b514d4c16..975a63787 100644
--- a/pyramid/traversal.py
+++ b/pyramid/traversal.py
@@ -629,20 +629,10 @@ class ResourceTreeTraverser(object):
self.root = root
def __call__(self, request):
- try:
- environ = request.environ
- except AttributeError:
- # In BFG 1.0 and before, this API expected an environ
- # rather than a request; some bit of code may still be
- # passing us an environ. If so, deal.
- environ = request
- depwarn = ('Passing an environ dictionary directly to a traverser '
- 'is deprecated in Pyramid 1.1. Pass a request object '
- 'instead.')
- warnings.warn(depwarn, DeprecationWarning, 2)
-
- if 'bfg.routes.matchdict' in environ:
- matchdict = environ['bfg.routes.matchdict']
+ matchdict = request.matchdict
+ environ = request.environ
+
+ if matchdict is not None:
path = matchdict.get('traverse', '/') or '/'
if is_nonstr_iter(path):
@@ -663,7 +653,7 @@ class ResourceTreeTraverser(object):
subpath = ()
try:
# empty if mounted under a path in mod_wsgi, for example
- path = decode_path_info(environ['PATH_INFO'] or '/')
+ path = request.path_info
except KeyError:
path = '/'
except UnicodeDecodeError as e: