Exemple #1
0
def _resolve_placeholder_with_locals(name, template, local_vars, global_vars):
    if local_vars is not None:
        try:
            return local_vars[name]
        except KeyError:
            pass
        except TypeError:
            raise runtime.PlaceholderError(
                'unexpected type for local_vars: %s' % type(local_vars))

    return _resolve_placeholder(name, template, global_vars)
Exemple #2
0
def _resolve_placeholder(name, template, global_vars):
    placeholder_cache = template.placeholder_cache
    if placeholder_cache and name in placeholder_cache:
        ph = placeholder_cache[name]
        if isinstance(ph, weakref.ReferenceType):
            v = ph()
            if v is not None:
                return v
        else:
            return ph

    # Note: getattr with 3 args is somewhat slower if the attribute
    # is found, but much faster if the attribute is not found.
    udn_ph = UndefinedPlaceholder
    result = getattr(template, name, udn_ph)
    if result is not udn_ph:
        if placeholder_cache is not None:
            # Use a weakref for methods to prevent memory cycles.
            placeholder_cache[name] = weakref.ref(result) if inspect.ismethod(
                result) else result
        return result

    search_list = template.search_list
    if search_list:
        ph = resolve_from_search_list(search_list, name)
        if ph is not UnresolvedPlaceholder:
            if placeholder_cache is not None:
                # Use a weakref for methods to prevent memory cycles.
                placeholder_cache[name] = weakref.ref(ph) if inspect.ismethod(
                    ph) else ph
            return ph

    # TODO: Cache negative results in placedholder_cache?
    # This probably isn't worthwhile as it likely won't happen often enough
    # to make the extra code/cpu/memory worthwhile.

    if global_vars is not None:
        try:
            return global_vars[name]
        except KeyError:
            pass
        except TypeError:
            raise runtime.PlaceholderError(
                'unexpected type for global_vars: %s' % type(global_vars))

    # fixme: finally try to resolve builtins - this should be configurable
    # if you compile optimized modes, this isn't necessary
    try:
        return getattr(__builtin__, name)
    except AttributeError:
        return UndefinedPlaceholder(name, search_list)
Exemple #3
0
 def __nonzero__(self):
     raise runtime.PlaceholderError(self.name,
                                    'function placeholder was not called')
Exemple #4
0
 def __cmp__(self, unused_other):
     raise runtime.PlaceholderError(self.name,
                                    'function placeholder was not called')