#21043: resolve doesn't handle lazily evaluated reverses
-------------------------------------+-------------------------------------
     Reporter:  Keryn Knight         |      Owner:  nobody
  <django@…>                         |     Status:  new
         Type:                       |    Version:  master
  Cleanup/optimization               |   Keywords:  urlresolvers resolve
    Component:  Documentation        |  reverse
     Severity:  Normal               |  Has patch:  0
 Triage Stage:  Unreviewed           |      UI/UX:  0
Easy pickings:  1                    |
-------------------------------------+-------------------------------------
 the following code generates an exception, which isn't documented as a
 caveat of using `reverse_lazy` (or being given a lazy proxy when you
 might've expected to receive a string, because the code is external and
 outside your control):

 {{{
 >>> from django.core.urlresolvers import reverse_lazy, resolve
 >>> proxy = reverse_lazy('admin:index')
 >>> resolve(proxy)
 Traceback (most recent call last):
   File "<console>", line 1, in <module>
   File "/django/core/urlresolvers.py", line 453, in resolve
     return get_resolver(urlconf).resolve(path)
   File "/django/core/urlresolvers.py", line 315, in resolve
     match = self.regex.search(path)
 TypeError: expected string or buffer
 }}}

 The fix is dirt simple in userland, because you can just call
 str/unicode/force_unicode on the proxy to evaluate it into a path. The
 problem lies in the path given to `resolve` being directly handed off to
 the `re` module, without being checked for validity.

 {{{
 >>> from django.core.urlresolvers import reverse_lazy, resolve
 >>> proxy = reverse_lazy('admin:index')
 >>> from django.utils.encoding import force_unicode
 >>> resolve(force_unicode(proxy))
 ResolverMatch(func=<function index at 0x10ae0b230>, args=(), kwargs={},
 url_name='index', app_name='admin', namespace='admin')
 }}}

 Both the above assume a project with `django.contrib.admin` in
 INSTALLED_APPS, because it's the easiest way of ensuring there's views to
 resolve :)

 I think that the issue should be documented; it's not something that's
 common, and if you encounter it, one would hope you've got a reasonable
 idea about where the problem lies, and it's probably safer to document it
 than promote forced evaluation into the `resolve` method of
 `RegexURLResolver` Marking as easy pickings on that basis.

-- 
Ticket URL: <https://code.djangoproject.com/ticket/21043>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-updates+unsubscr...@googlegroups.com.
To post to this group, send email to django-updates@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/080.2157528069a508ec9588bc270dba6d40%40djangoproject.com.
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to