summaryrefslogtreecommitdiff
path: root/pecan/rest.py
diff options
context:
space:
mode:
authorRyan Petrello <lists@ryanpetrello.com>2015-05-07 10:45:39 -0700
committerRyan Petrello <lists@ryanpetrello.com>2015-05-07 11:15:45 -0700
commit9a6a893d5e2f090087be92d301705e35ccf158ba (patch)
tree081c06e89567db76a03e701ac595e4b5cdddf65a /pecan/rest.py
parentd907987e7022025f3ad067d7407532125e6b8e2e (diff)
downloadpecan-9a6a893d5e2f090087be92d301705e35ccf158ba.tar.gz
Properly handle Python3 Unicode path segments in pecan routing.
Change-Id: I3890d73a087f7635ddc51b71d3d6f68a41058c42 Closes-Bug: 1451842
Diffstat (limited to 'pecan/rest.py')
-rw-r--r--pecan/rest.py27
1 files changed, 19 insertions, 8 deletions
diff --git a/pecan/rest.py b/pecan/rest.py
index 9406ad4..ef5d753 100644
--- a/pecan/rest.py
+++ b/pecan/rest.py
@@ -59,6 +59,17 @@ class RestController(object):
# invalid path.
abort(404)
+ def _lookup_child(self, remainder):
+ """
+ Lookup a child controller with a named path (handling Unicode paths
+ properly for Python 2).
+ """
+ try:
+ controller = getattr(self, remainder, None)
+ except UnicodeEncodeError:
+ return None
+ return controller
+
@expose()
def _route(self, args, request=None):
'''
@@ -138,7 +149,7 @@ class RestController(object):
Returns the appropriate controller for routing a custom action.
'''
for name in args:
- obj = getattr(self, name, None)
+ obj = self._lookup_child(name)
if obj and iscontroller(obj):
return obj
return None
@@ -167,7 +178,7 @@ class RestController(object):
# attempt to locate a sub-controller
if var_args:
for i, item in enumerate(remainder):
- controller = getattr(self, item, None)
+ controller = self._lookup_child(item)
if controller and not ismethod(controller):
self._set_routing_args(request, remainder[:i])
return lookup_controller(controller, remainder[i + 1:],
@@ -175,7 +186,7 @@ class RestController(object):
elif fixed_args < len(remainder) and hasattr(
self, remainder[fixed_args]
):
- controller = getattr(self, remainder[fixed_args])
+ controller = self._lookup_child(remainder[fixed_args])
if not ismethod(controller):
self._set_routing_args(request, remainder[:fixed_args])
return lookup_controller(
@@ -201,7 +212,7 @@ class RestController(object):
if remainder:
if self._find_controller(remainder[0]):
abort(405)
- sub_controller = getattr(self, remainder[0], None)
+ sub_controller = self._lookup_child(remainder[0])
if sub_controller:
return lookup_controller(sub_controller, remainder[1:],
request)
@@ -237,7 +248,7 @@ class RestController(object):
if match:
return match
- controller = getattr(self, remainder[0], None)
+ controller = self._lookup_child(remainder[0])
if controller and not ismethod(controller):
return lookup_controller(controller, remainder[1:], request)
@@ -261,7 +272,7 @@ class RestController(object):
if match:
return match
- controller = getattr(self, remainder[0], None)
+ controller = self._lookup_child(remainder[0])
if controller and not ismethod(controller):
return lookup_controller(controller, remainder[1:], request)
@@ -275,7 +286,7 @@ class RestController(object):
if remainder:
if self._find_controller(remainder[0]):
abort(405)
- sub_controller = getattr(self, remainder[0], None)
+ sub_controller = self._lookup_child(remainder[0])
if sub_controller:
return lookup_controller(sub_controller, remainder[1:],
request)
@@ -295,7 +306,7 @@ class RestController(object):
if match:
return match
- controller = getattr(self, remainder[0], None)
+ controller = self._lookup_child(remainder[0])
if controller and not ismethod(controller):
return lookup_controller(controller, remainder[1:], request)