[svn] fixed severe jinja memcaching bug
Before adoption: this commit came in with the history the project adopted, so no landing records it.
--HG-- branch : trunk
authored by Armin Ronacher · committed by Armin Ronacher · parent 02d81f7 · Browse files · 2 files·+88 added−78 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+88 added−78 removed
- other:+88 added−78 removed
Provenance
- Commit
ca80b45- Tree
ef9fef1- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent
02d81f7
2 files changed+88 added−78 removed
modified:jinja/translators/python.py+35 added-31 removed
30 unmodified lines31 from jinja.utils import translate_exception, capture_generator, \32 RUNTIME_EXCEPTION_OFFSET33 34 35+#: regular expression for the debug symbols36+_debug_re = re.compile(r'^\s*\# DEBUG\(filename=(?P<filename>.*?), '37+ r'lineno=(?P<lineno>\d+)\)$')38+39+40 try:41 GeneratorExit42 except NameError:43 class GeneratorExit(Exception):39- pass44+ """For python2.3/python2.4 compatibility"""45 46 42-#: regular expression for the debug symbols43-_debug_re = re.compile(r'^\s*\# DEBUG\(filename=(?P<filename>.*?), '44- r'lineno=(?P<lineno>\d+)\)$')45-46-47 def _to_tuple(args):48 """49 Return a tuple repr without nested repr.50 """39 unmodified lines90 load = staticmethod(load)91 92 def render(self, *args, **kwargs):93 """Render a template."""94+ ctx = self._prepare(*args, **kwargs)95+ try:94- return capture_generator(self._generate(*args, **kwargs))96+ return capture_generator(self.generate_func(ctx))97+ except:98+ self._debug(ctx, *sys.exc_info())99 100 def stream(self, *args, **kwargs):101 """Render a template as stream."""98- return TemplateStream(self._generate(*args, **kwargs))102+ def proxy(ctx):103+ try:104+ for item in self.generate_func(ctx):105+ yield item106+ except GeneratorExit:107+ return108+ except:109+ self._debug(ctx, *sys.exc_info())110+ return TemplateStream(proxy(self._prepare(*args, **kwargs)))111 100- def _generate(self, *args, **kwargs):101- """Template generation helper"""112+ def _prepare(self, *args, **kwargs):113+ """Prepare the template execution."""114 # if there is no generation function we execute the code115 # in a new namespace and save the generation function and116 # debug information.117+ env = self.environment118 if self.generate_func is None:106- ns = {'environment': self.environment}119+ ns = {'environment': env}120 exec self.code in ns121 self.generate_func = ns['generate']122 self._debug_info = ns['debug_info']110- ctx = self.environment.context_class(self.environment, *args, **kwargs)111- try:112- for item in self.generate_func(ctx):113- yield item114- except:115- if not self.environment.friendly_traceback:116- raise117- # debugging system:118- # on any exception we first get the current exception information119- # and skip the internal frames (currently either one (python2.5)120- # or two (python2.4 and lower)). After that we call a function121- # that creates a new traceback that is easier to debug.122- exc_type, exc_value, traceback = sys.exc_info()123+ return env.context_class(env, *args, **kwargs)124 124- # if an exception is a GeneratorExit we just reraise it. If we125- # run on top of python2.3 or python2.4 a fake GeneratorExit126- # class is added for this module so that we don't get a NameError127- if exc_type is GeneratorExit:128- raise129-125+ def _debug(self, ctx, exc_type, exc_value, traceback):126+ """Debugging Helper"""127+ # just modify traceback if we have that feature enabled128+ if self.environment.friendly_traceback:129+ # debugging system:130+ # on any exception we first skip the internal frames (currently131+ # either one (python2.5) or two (python2.4 and lower)). After that132+ # we call a function that creates a new traceback that is easier133+ # to debug.134 for _ in xrange(RUNTIME_EXCEPTION_OFFSET):135 traceback = traceback.tb_next136 traceback = translate_exception(self, exc_type, exc_value,137 traceback, ctx)134- raise exc_type, exc_value, traceback138+ raise exc_type, exc_value, traceback139 140 141 class PythonTranslator(Translator):142 """872 unmodified linesmodified:jinja/loaders.py+53 added-47 removed
153 unmodified lines154 self.__lock.acquire()155 try:156 # caching is only possible for the python translator. skip157 # all other translators158- if translator is PythonTranslator:159- tmpl = None158+ if translator is not PythonTranslator:159+ return super(CachedLoaderMixin, self).load(160+ environment, name, translator)161 161- # auto reload enabled? check for the last change of162- # the template163- if self.__auto_reload:164- last_change = self.check_source_changed(environment, name)165- else:166- last_change = None162+ tmpl = None163+ save_to_disk = False164+ push_to_memory = False165 166+ # auto reload enabled? check for the last change of167+ # the template168+ if self.__auto_reload:169+ last_change = self.check_source_changed(environment, name)170+ else:171+ last_change = None172+168- # check if we have something in the memory cache and the169- # memory cache is enabled.170- if self.__memcache is not None and name in self.__memcache:173+ # check if we have something in the memory cache and the174+ # memory cache is enabled.175+ if self.__memcache is not None:176+ if name in self.__memcache:177 tmpl = self.__memcache[name]178+ # if auto reload is enabled check if the template changed179 if last_change is not None and \180 last_change > self.__times[name]:181 tmpl = None182+ push_to_memory = True183+ else:184+ push_to_memory = True185 176- # if diskcache is enabled look for an already compiled177- # template.178- if self.__cache_folder is not None:179- cache_fn = get_cachename(self.__cache_folder, name)180-181- # there is an up to date compiled template182- if tmpl is not None and last_change is None:183- try:184- cache_time = path.getmtime(cache_fn)185- except OSError:186- cache_time = -1187- if cache_time == -1 or last_change >= cache_time:188- f = file(cache_fn, 'rb')189- try:190- tmpl = Template.load(environment, f)191- finally:192- f.close()193-194- # no template so far, parse, translate and compile it195- elif tmpl is None:196- tmpl = super(CachedLoaderMixin, self).load(197- environment, name, translator)198-199- # save the compiled template200- f = file(cache_fn, 'wb')186+ # mem cache disabled or not cached by now187+ # try to load if from the disk cache188+ if tmpl is None and self.__cache_folder is not None:189+ cache_fn = get_cachename(self.__cache_folder, name)190+ if last_change is not None:191 try:202- tmpl.dump(f)192+ cache_time = path.getmtime(cache_fn)193+ except OSError:194+ cache_time = -1195+ if last_change is None or last_change <= cache_time:196+ f = file(cache_fn, 'rb')197+ try:198+ tmpl = Template.load(environment, f)199 finally:200 f.close()201+ else:202+ save_to_disk = True203 206- # if memcaching is enabled push the template207- if tmpl is not None:208- if self.__memcache is not None:209- self.__times[name] = time.time()210- self.__memcache[name] = tmpl211- return tmpl204+ # if we still have no template we load, parse and translate it.205+ if tmpl is None:206+ tmpl = super(CachedLoaderMixin, self).load(207+ environment, name, translator)208 213- # if we reach this point we don't have caching enabled or translate214- # to something else than python215- return super(CachedLoaderMixin, self).load(216- environment, name, translator)209+ # save the compiled template on the disk if enabled210+ if save_to_disk:211+ f = file(cache_fn, 'wb')212+ try:213+ tmpl.dump(f)214+ finally:215+ f.close()216+217+ # if memcaching is enabled and the template not loaded218+ # we add that there.219+ if push_to_memory:220+ self.__times[name] = time.time()221+ self.__memcache[name] = tmpl222+ return tmpl223 finally:224 self.__lock.release()225 226 258 unmodified lines