Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #8726 -- When doing reverse URL resolving, make sure we're cons…

…istently

dealing with unicode strings throughout by promoting up from UTF-8 as necessary.


git-svn-id: http://code.djangoproject.com/svn/django/trunk@8777 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 7838493b9efa4ef17111599ed6bfaa05ed70645c 1 parent ab13303
Malcolm Tredinnick authored August 31, 2008
8  django/core/urlresolvers.py
@@ -228,12 +228,14 @@ def reverse(self, lookup_view, *args, **kwargs):
228 228
             if args:
229 229
                 if len(args) != len(params):
230 230
                     continue
231  
-                candidate =  result % dict(zip(params, args))
  231
+                unicode_args = [force_unicode(val) for val in args]
  232
+                candidate =  result % dict(zip(params, unicode_args))
232 233
             else:
233 234
                 if set(kwargs.keys()) != set(params):
234 235
                     continue
235  
-                candidate = result % kwargs
236  
-            if re.search('^%s' % pattern, candidate, re.UNICODE):
  236
+                unicode_kwargs = dict([(k, force_unicode(v)) for (k, v) in kwargs.items()])
  237
+                candidate = result % unicode_kwargs
  238
+            if re.search(u'^%s' % pattern, candidate, re.UNICODE):
237 239
                 return candidate
238 240
         raise NoReverseMatch("Reverse for '%s' with arguments '%s' and keyword "
239 241
                 "arguments '%s' not found." % (lookup_view, args, kwargs))
30  django/utils/regex_helper.py
@@ -13,12 +13,12 @@
13 13
     "A": None,
14 14
     "b": None,
15 15
     "B": None,
16  
-    "d": '0',
17  
-    "D": "x",
18  
-    "s": " ",
19  
-    "S": "x",
20  
-    "w": "x",
21  
-    "W": "!",
  16
+    "d": u"0",
  17
+    "D": u"x",
  18
+    "s": u" ",
  19
+    "S": u"x",
  20
+    "w": u"x",
  21
+    "W": u"!",
22 22
     "Z": None,
23 23
 }
24 24
 
@@ -77,7 +77,7 @@ def normalize(pattern):
77 77
     try:
78 78
         ch, escaped = pattern_iter.next()
79 79
     except StopIteration:
80  
-        return zip([''],  [[]])
  80
+        return zip([u''],  [[]])
81 81
 
82 82
     try:
83 83
         while True:
@@ -85,7 +85,7 @@ def normalize(pattern):
85 85
                 result.append(ch)
86 86
             elif ch == '.':
87 87
                 # Replace "any character" with an arbitrary representative.
88  
-                result.append("x")
  88
+                result.append(u"x")
89 89
             elif ch == '|':
90 90
                 # FIXME: One day we'll should do this, but not in 1.0.
91 91
                 raise NotImplementedError
@@ -117,7 +117,7 @@ def normalize(pattern):
117 117
                     # A positional group
118 118
                     name = "_%d" % num_args
119 119
                     num_args += 1
120  
-                    result.append(Group((("%%(%s)s" % name), name)))
  120
+                    result.append(Group(((u"%%(%s)s" % name), name)))
121 121
                     walk_to_end(ch, pattern_iter)
122 122
                 else:
123 123
                     ch, escaped = pattern_iter.next()
@@ -144,7 +144,7 @@ def normalize(pattern):
144 144
                             name.append(ch)
145 145
                             ch, escaped = pattern_iter.next()
146 146
                         param = ''.join(name)
147  
-                        result.append(Group((("%%(%s)s" % param), param)))
  147
+                        result.append(Group(((u"%%(%s)s" % param), param)))
148 148
                         walk_to_end(ch, pattern_iter)
149 149
             elif ch in "*?+{":
150 150
                 # Quanitifers affect the previous item in the result list.
@@ -180,7 +180,7 @@ def normalize(pattern):
180 180
         pass
181 181
     except NotImplementedError:
182 182
         # A case of using the disjunctive form. No results for you!
183  
-        return zip([''],  [[]])
  183
+        return zip([u''],  [[]])
184 184
 
185 185
     return zip(*flatten_result(result))
186 186
 
@@ -279,20 +279,20 @@ def flatten_result(source):
279 279
     Each of the two lists will be of the same length.
280 280
     """
281 281
     if source is None:
282  
-        return [''], [[]]
  282
+        return [u''], [[]]
283 283
     if isinstance(source, Group):
284 284
         if source[1] is None:
285 285
             params = []
286 286
         else:
287 287
             params = [source[1]]
288 288
         return [source[0]], [params]
289  
-    result = ['']
  289
+    result = [u'']
290 290
     result_args = [[]]
291 291
     pos = last = 0
292 292
     for pos, elt in enumerate(source):
293 293
         if isinstance(elt, basestring):
294 294
             continue
295  
-        piece = ''.join(source[last:pos])
  295
+        piece = u''.join(source[last:pos])
296 296
         if isinstance(elt, Group):
297 297
             piece += elt[0]
298 298
             param = elt[1]
@@ -320,7 +320,7 @@ def flatten_result(source):
320 320
             result = new_result
321 321
             result_args = new_args
322 322
     if pos >= last:
323  
-        piece = ''.join(source[last:])
  323
+        piece = u''.join(source[last:])
324 324
         for i in range(len(result)):
325 325
             result[i] += piece
326 326
     return result, result_args
3  tests/regressiontests/templates/tests.py
@@ -194,6 +194,7 @@ def test_template_loader(template_name, template_dirs=None):
194 194
                     output = self.render(test_template, vals)
195 195
                 except Exception, e:
196 196
                     if e.__class__ != result:
  197
+                        raise
197 198
                         failures.append("Template test (TEMPLATE_STRING_IF_INVALID='%s'): %s -- FAILED. Got %s, exception: %s" % (invalid_str, name, e.__class__, e))
198 199
                     continue
199 200
                 if output != result:
@@ -899,6 +900,8 @@ def get_template_tests(self):
899 900
             'url05': (u'{% url метка_оператора v %}', {'v': u'Ω'}, '/url_tag/%D0%AE%D0%BD%D0%B8%D0%BA%D0%BE%D0%B4/%CE%A9/'),
900 901
             'url06': (u'{% url метка_оператора_2 tag=v %}', {'v': u'Ω'}, '/url_tag/%D0%AE%D0%BD%D0%B8%D0%BA%D0%BE%D0%B4/%CE%A9/'),
901 902
             'url07': (u'{% url regressiontests.templates.views.client2 tag=v %}', {'v': u'Ω'}, '/url_tag/%D0%AE%D0%BD%D0%B8%D0%BA%D0%BE%D0%B4/%CE%A9/'),
  903
+            'url08': (u'{% url метка_оператора v %}', {'v': 'Ω'}, '/url_tag/%D0%AE%D0%BD%D0%B8%D0%BA%D0%BE%D0%B4/%CE%A9/'),
  904
+            'url09': (u'{% url метка_оператора_2 tag=v %}', {'v': 'Ω'}, '/url_tag/%D0%AE%D0%BD%D0%B8%D0%BA%D0%BE%D0%B4/%CE%A9/'),
902 905
 
903 906
             # Failures
904 907
             'url-fail01': ('{% url %}', {}, template.TemplateSyntaxError),

0 notes on commit 7838493

Please sign in to comment.
Something went wrong with that request. Please try again.