[Django] #21043: resolve doesn't handle lazily evaluated reverses

3 views
Skip to first unread message

Django

unread,
Sep 5, 2013, 7:01:19 AM9/5/13
to django-...@googlegroups.com
#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.

Django

unread,
Sep 6, 2013, 10:52:41 AM9/6/13
to django-...@googlegroups.com
#21043: resolve doesn't handle lazily evaluated reverses
-------------------------------------+-------------------------------------
Reporter: Keryn Knight | Owner:
<django@…> | DanRJohnson
Type: | Status: assigned
Cleanup/optimization | Version: master
Component: Documentation | Resolution:
Severity: Normal | Triage Stage:
Keywords: urlresolvers | Unreviewed
resolve reverse | Needs documentation: 0
Has patch: 0 | Patch needs improvement: 0
Needs tests: 0 | UI/UX: 0
Easy pickings: 1 |
-------------------------------------+-------------------------------------
Changes (by DanRJohnson):

* status: new => assigned
* needs_docs: => 0
* owner: nobody => DanRJohnson
* needs_tests: => 0
* needs_better_patch: => 0


--
Ticket URL: <https://code.djangoproject.com/ticket/21043#comment:1>

Django

unread,
Sep 6, 2013, 11:41:11 AM9/6/13
to django-...@googlegroups.com
#21043: resolve doesn't handle lazily evaluated reverses
-------------------------------------+-------------------------------------
Reporter: Keryn Knight | Owner:
<django@…> | DanRJohnson
Type: | Status: assigned
Cleanup/optimization | Version: master
Component: Core (URLs) | Resolution:
Severity: Normal | Triage Stage: Accepted
Keywords: urlresolvers | Needs documentation: 0
resolve reverse | Patch needs improvement: 0
Has patch: 0 | UI/UX: 0
Needs tests: 1 |

Easy pickings: 1 |
-------------------------------------+-------------------------------------
Changes (by DanRJohnson):

* component: Documentation => Core (URLs)
* needs_tests: 0 => 1
* stage: Unreviewed => Accepted


Comment:

I can confirm this issue.

However, I think that resolve should be fixed to work correctly with the
proxy object returned by reverse_lazy. I think we should fix the issue
with resolve not evaluating the proxy object instead of documenting a work
around.

--
Ticket URL: <https://code.djangoproject.com/ticket/21043#comment:2>

Django

unread,
Sep 6, 2013, 3:13:17 PM9/6/13
to django-...@googlegroups.com
#21043: resolve doesn't handle lazily evaluated reverses
-------------------------------------+-------------------------------------
Reporter: Keryn Knight | Owner:
<django@…> | DanRJohnson
Type: Bug | Status: assigned
Component: Core (URLs) | Version: master
Severity: Normal | Resolution:
Keywords: urlresolvers | Triage Stage: Accepted

resolve reverse | Needs documentation: 0
Has patch: 1 | Patch needs improvement: 0
Needs tests: 1 | UI/UX: 0
Easy pickings: 1 |
-------------------------------------+-------------------------------------
Changes (by DanRJohnson):

* has_patch: 0 => 1
* type: Cleanup/optimization => Bug


Comment:

I've created a patch and created a pull request.

https://github.com/django/django/pull/1570

--
Ticket URL: <https://code.djangoproject.com/ticket/21043#comment:3>

Django

unread,
Sep 6, 2013, 3:13:45 PM9/6/13
to django-...@googlegroups.com
#21043: resolve doesn't handle lazily evaluated reverses
-------------------------------------+-------------------------------------
Reporter: Keryn Knight | Owner:
<django@…> | Status: new
Type: Bug | Version: master

Component: Core (URLs) | Resolution:
Severity: Normal | Triage Stage: Accepted
Keywords: urlresolvers | Needs documentation: 0
resolve reverse | Patch needs improvement: 0
Has patch: 1 | UI/UX: 0
Needs tests: 1 |

Easy pickings: 1 |
-------------------------------------+-------------------------------------
Changes (by DanRJohnson):

* owner: DanRJohnson =>
* status: assigned => new


--
Ticket URL: <https://code.djangoproject.com/ticket/21043#comment:4>

Django

unread,
Sep 6, 2013, 3:32:27 PM9/6/13
to django-...@googlegroups.com
#21043: resolve doesn't handle lazily evaluated reverses
-------------------------------------+-------------------------------------
Reporter: Keryn Knight | Owner:
<django@…> | Status: new
Type: Bug | Version: master
Component: Core (URLs) | Resolution:
Severity: Normal | Triage Stage: Accepted
Keywords: urlresolvers | Needs documentation: 0
resolve reverse | Patch needs improvement: 0
Has patch: 1 | UI/UX: 0
Needs tests: 0 |
Easy pickings: 1 |
-------------------------------------+-------------------------------------
Changes (by DanRJohnson):

* needs_tests: 1 => 0


--
Ticket URL: <https://code.djangoproject.com/ticket/21043#comment:5>

Django

unread,
Sep 7, 2013, 1:53:48 AM9/7/13
to django-...@googlegroups.com
#21043: resolve doesn't handle lazily evaluated reverses
-------------------------------------+-------------------------------------
Reporter: Keryn Knight | Owner:
<django@…> | Status: closed
Type: Bug | Version: master
Component: Core (URLs) | Resolution: fixed

Severity: Normal | Triage Stage: Accepted
Keywords: urlresolvers | Needs documentation: 0
resolve reverse | Patch needs improvement: 0
Has patch: 1 | UI/UX: 0
Needs tests: 0 |
Easy pickings: 1 |
-------------------------------------+-------------------------------------
Changes (by grue):

* status: new => closed
* resolution: => fixed


--
Ticket URL: <https://code.djangoproject.com/ticket/21043#comment:6>

Django

unread,
Sep 7, 2013, 1:54:22 AM9/7/13
to django-...@googlegroups.com
#21043: resolve doesn't handle lazily evaluated reverses
-------------------------------------+-------------------------------------
Reporter: Keryn Knight | Owner:
<django@…> | Status: closed
Type: Bug | Version: master
Component: Core (URLs) | Resolution: fixed
Severity: Normal | Triage Stage: Accepted
Keywords: urlresolvers | Needs documentation: 0
resolve reverse | Patch needs improvement: 0
Has patch: 1 | UI/UX: 0
Needs tests: 0 |
Easy pickings: 1 |
-------------------------------------+-------------------------------------

Comment (by grue):

This has been closed as per
https://github.com/django/django/commit/df462cf7604578c2afd43b988b7ea1fe5e727896

--
Ticket URL: <https://code.djangoproject.com/ticket/21043#comment:7>

Reply all
Reply to author
Forward
0 new messages