Raw byte conversion is something I would like to have in
werkzeug.routing. So a conversion such as <bytes:id> would produce a
`str` type instead of unicode for passing to libraries expecting the
former - and without having do explicit conversion in the handler.
Please review attached patch.
Thanks,
--
\\\\\/\"/\\\\\\\\\\\
\\\\/ // //\/\\\\\\\
\\\/ \\// /\ \/\\\\
\\/ /\/ / /\/ /\ \\\
\/ / /\/ /\ /\\\ \\
/ /\\\ /\\\ \\\\\/\
\/\\\\\/\\\\\/\\\\\\
d.p.s
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"pocoo-libs" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/pocoo-libs?hl=en
-~----------~----~----~----~------~----~------~--~---
diff -r 1cb0ad8b3f01 tests/test_routing.py
--- a/tests/test_routing.py Wed Aug 13 20:25:11 2008 +0200
+++ b/tests/test_routing.py Mon Aug 18 09:25:14 2008 -0400
@@ -132,6 +132,11 @@ def test_path():
assert adapter.match('/Files/downloads/werkzeug/0.2.zip') == ('files',
{'file':'downloads/werkzeug/0.2.zip'})
+def test_bytes_conversion():
+ map = Map([Rule('/<bytes:id>', endpoint='things')])
+ adapter = map.bind('example.org', '/')
+ assert type( adapter.match('/feedbeef')[1]['id'] ) is str
+
def test_dispatch():
env = create_environ('/')
map = Map([
diff -r 1cb0ad8b3f01 werkzeug/routing.py
--- a/werkzeug/routing.py Wed Aug 13 20:25:11 2008 +0200
+++ b/werkzeug/routing.py Mon Aug 18 09:25:14 2008 -0400
@@ -781,6 +781,16 @@ class UnicodeConverter(BaseConverter):
self.regex = '[^/]' + length
+class BytesConverter(UnicodeConverter):
+ """This converter is the same as UnicodeConverter except raw byes
+ are (or python C{str} type) are produced instead of unicode.
+ The arguments are the same for UnicodeConverter.
+ """
+
+ def to_python(self, value):
+ return value.encode('utf-8')
+
+
class AnyConverter(BaseConverter):
"""Matches one of the items provided. Items can either be Python
identifiers or unicode strings::
@@ -1276,6 +1286,7 @@ DEFAULT_CONVERTERS = {
DEFAULT_CONVERTERS = {
'default': UnicodeConverter,
'string': UnicodeConverter,
+ 'bytes': BytesConverter,
'any': AnyConverter,
'path': PathConverter,
'int': IntegerConverter,