def __init__(self, filename, source=None): global linecache import linecache self.filename = filename if source is not None: cache_source(filename, source, self)
def apply_template(self, template, *args): try: return self.compiled_cache[template, args] except (KeyError, TypeError, AttributeError): pass try: closure = self.closures[template] except KeyError: if template.func_closure: raise TypeError("Templates cannot use outer-scope variables") import linecache; from peak.util.decorators import cache_source tmp = apply_template(template, self.function, *args) body = ''.join(linecache.getlines(tmp.func_code.co_filename)) filename = "<%s at 0x%08X wrapping %s at 0x%08X>" % ( template.__name__, id(template), self.function.__name__, id(self) ) d ={} exec compile(body, filename, "exec") in template.func_globals, d tmp, closure = d.popitem() closure.func_defaults = template.func_defaults cache_source(filename, body, closure) self.closures[template] = closure f = closure(self.function, *args) f.func_defaults = self.function.func_defaults try: hash(args) except TypeError: pass else: if self.compiled_cache is None: from weakref import WeakValueDictionary self.compiled_cache = WeakValueDictionary() self.compiled_cache[template, args] = f return f
def apply_template(self, template, *args): try: return self.compiled_cache[template, args] except (KeyError, TypeError, AttributeError): pass try: closure = self.closures[template] except KeyError: if getattr(template, CLOSURE): raise TypeError("Templates cannot use outer-scope variables") import linecache; from peak.util.decorators import cache_source tmp = apply_template(template, self.function, *args) body = ''.join(linecache.getlines(getattr(tmp, CODE).co_filename)) filename = "<%s at 0x%08X wrapping %s at 0x%08X>" % ( template.__name__, id(template), self.function.__name__, id(self) ) d ={} exec(compile(body, filename, "exec"), getattr(template, GLOBALS), d) tmp, closure = d.popitem() setattr(closure, DEFAULTS, getattr(template, DEFAULTS)) cache_source(filename, body, closure) self.closures[template] = closure f = closure(self.function, *args) setattr(f, DEFAULTS, getattr(self.function, DEFAULTS)) try: hash(args) except TypeError: pass else: if self.compiled_cache is None: from weakref import WeakValueDictionary self.compiled_cache = WeakValueDictionary() self.compiled_cache[template, args] = f return f
def apply_template(self, template, *args): try: return self.compiled_cache[template, args] except (KeyError, TypeError, AttributeError): pass try: closure = self.closures[template] except KeyError: if template.func_closure: raise TypeError("Templates cannot use outer-scope variables") import linecache from peak.util.decorators import cache_source tmp = apply_template(template, self.function, *args) body = ''.join(linecache.getlines(tmp.func_code.co_filename)) filename = "<%s at 0x%08X wrapping %s at 0x%08X>" % ( template.__name__, id(template), self.function.__name__, id(self)) d = {} exec compile(body, filename, "exec") in template.func_globals, d tmp, closure = d.popitem() closure.func_defaults = template.func_defaults cache_source(filename, body, closure) self.closures[template] = closure f = closure(self.function, *args) f.func_defaults = self.function.func_defaults try: hash(args) except TypeError: pass else: if self.compiled_cache is None: from weakref import WeakValueDictionary self.compiled_cache = WeakValueDictionary() self.compiled_cache[template, args] = f return f