Django

Code

Changeset 5632

Show
Ignore:
Timestamp:
07/07/07 19:39:32 (1 year ago)
Author:
mtredinnick
Message:

Fixed reverse URL lookup using functions when the original URL pattern was a
string. This is now just as fragile as it was prior to [5609], but works in a
few cases that people were relying on, apparently.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/trunk/django/core/urlresolvers.py

    r5630 r5632  
    3939    during the import fail and the string is returned. 
    4040    """ 
    41     try: 
    42         # Bail out early if lookup_view is not ASCII. This can't be a function. 
    43         lookup_view = lookup_view.encode('ascii') 
    44  
    45         if not callable(lookup_view): 
     41    if not callable(lookup_view): 
     42        try: 
     43            # Bail early for non-ASCII strings (they can't be functions). 
     44            lookup_view = lookup_view.encode('ascii') 
    4645            mod_name, func_name = get_mod_func(lookup_view) 
    47             try: 
    48                 if func_name != '': 
    49                     lookup_view = getattr(__import__(mod_name, {}, {}, ['']), func_name) 
    50             except (ImportError, AttributeError): 
    51                 if not can_fail: 
    52                     raise 
    53     except UnicodeEncodeError: 
    54         pass 
     46            if func_name != '': 
     47                lookup_view = getattr(__import__(mod_name, {}, {}, ['']), func_name) 
     48        except (ImportError, AttributeError): 
     49            if not can_fail: 
     50                raise 
     51        except UnicodeEncodeError: 
     52            pass 
    5553    return lookup_view 
    56 get_callable = memoize(get_callable, _callable_cache
     54get_callable = memoize(get_callable, _callable_cache, 1
    5755 
    5856def get_resolver(urlconf): 
     
    6159        urlconf = settings.ROOT_URLCONF 
    6260    return RegexURLResolver(r'^/', urlconf) 
    63 get_resolver = memoize(get_resolver, _resolver_cache
     61get_resolver = memoize(get_resolver, _resolver_cache, 1
    6462 
    6563def get_mod_func(callback): 
  • django/trunk/django/utils/functional.py

    r5609 r5632  
    44    return _curried 
    55 
    6 def memoize(func, cache): 
     6def memoize(func, cache, num_args): 
    77    """ 
    88    Wrap a function so that results for any argument tuple are stored in 
    99    'cache'. Note that the args to the function must be usable as dictionary 
    1010    keys. 
     11 
     12    Only the first num_args are considered when creating the key. 
    1113    """ 
    1214    def wrapper(*args): 
    13         if args in cache: 
    14             return cache[args] 
    15  
     15        mem_args = args[:num_args] 
     16        if mem_args in cache: 
     17            return cache[mem_args] 
    1618        result = func(*args) 
    17         cache[args] = result 
     19        cache[mem_args] = result 
    1820        return result 
    1921    return wrapper