Fixed #17892 -- Do not include whole RegexURLPattern lists in RegexURLResolver repr

Thanks milosu for the report and the initial patch.
This commit is contained in:
Claude Paroz 2012-09-01 22:07:18 +02:00
parent c6088bc91d
commit 28fd876bae
2 changed files with 17 additions and 2 deletions

View File

@ -245,8 +245,13 @@ class RegexURLResolver(LocaleRegexProvider):
self._app_dict = {} self._app_dict = {}
def __repr__(self): def __repr__(self):
if isinstance(self.urlconf_name, list) and len(self.urlconf_name):
# Don't bother to output the whole list, it can be huge
urlconf_repr = '<%s list>' % self.urlconf_name[0].__class__.__name__
else:
urlconf_repr = repr(self.urlconf_name)
return force_str('<%s %s (%s:%s) %s>' % ( return force_str('<%s %s (%s:%s) %s>' % (
self.__class__.__name__, self.urlconf_name, self.app_name, self.__class__.__name__, urlconf_repr, self.app_name,
self.namespace, self.regex.pattern)) self.namespace, self.regex.pattern))
def _populate(self): def _populate(self):

View File

@ -6,7 +6,7 @@ from __future__ import absolute_import, unicode_literals
from django.conf import settings from django.conf import settings
from django.core.exceptions import ImproperlyConfigured, ViewDoesNotExist from django.core.exceptions import ImproperlyConfigured, ViewDoesNotExist
from django.core.urlresolvers import (reverse, resolve, get_callable, from django.core.urlresolvers import (reverse, resolve, get_callable,
NoReverseMatch, Resolver404, ResolverMatch, RegexURLResolver, get_resolver, NoReverseMatch, Resolver404, ResolverMatch, RegexURLResolver,
RegexURLPattern) RegexURLPattern)
from django.http import HttpResponseRedirect, HttpResponsePermanentRedirect from django.http import HttpResponseRedirect, HttpResponsePermanentRedirect
from django.shortcuts import redirect from django.shortcuts import redirect
@ -172,6 +172,16 @@ class URLPatternReverse(TestCase):
self.assertRaises(NoReverseMatch, reverse, None) self.assertRaises(NoReverseMatch, reverse, None)
class ResolverTests(unittest.TestCase): class ResolverTests(unittest.TestCase):
def test_resolver_repr(self):
"""
Test repr of RegexURLResolver, especially when urlconf_name is a list
(#17892).
"""
# Pick a resolver from a namespaced urlconf
resolver = get_resolver('regressiontests.urlpatterns_reverse.namespace_urls')
sub_resolver = resolver.namespace_dict['test-ns1'][1]
self.assertIn('<RegexURLPattern list>', repr(sub_resolver))
def test_non_regex(self): def test_non_regex(self):
""" """
Verifies that we raise a Resolver404 if what we are resolving doesn't Verifies that we raise a Resolver404 if what we are resolving doesn't