this fixes #277.
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 c5b509f · Browse files · 3 files·+73 added−8 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+73 added−8 removed
- other:+73 added−8 removed
Provenance
- Commit
c9fe16e- Tree
79e6495- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent
c5b509f
3 files changed+73 added−8 removed
modified:jinja/translators/python.py+17 added-7 removed
142 unmodified lines143 """144 145 def __init__(self, environment, node, source):146 self.environment = environment147+ self.loader = environment.loader.get_controlled_loader()148 self.node = node149 self.source = source150 self.closed = False151 126 unmodified lines278 """279 Clean up stuff.280 """281 self.closed = True281- self.handlers = self.node = self.environment = None282+ self.handlers = self.node = self.environment = self.loader = None283 284 def translate(self):285 """286 Translate the node defined in the constructor.38 unmodified lines325 requirements += [child for child in node.body.get_child_nodes()326 if child.__class__ not in (nodes.Text,327 nodes.Block)]328 328- # load the template we inherit from and add not known blocks329- parent = self.environment.loader.parse(node.extends,330- node.filename)329+ # load the template we inherit from and add not known blocks.330+ # this also marks the templates on the controlled loader but331+ # are never removed. that's no problem because we don't allow332+ # parents we extend from as includes and the controlled loader333+ # is only used for this templated334+ parent = self.loader.parse(node.extends,335+ node.filename)336+337 # look up all block nodes in the current template and338 # add them to the override dict.339 for n in get_nodes(nodes.Block, node):340 overwrites[n.name] = n156 unmodified lines497 for filename, file_id in file_mapping:498 result.append('%s = %r' % (file_id, filename))499 result.append('debug_info = %s' % self.to_tuple(debug_mapping))500 result.append('template_source = %r' % self.source)501+502 return '\n'.join(result)503 504 def handle_template_text(self, node):505 """334 unmodified lines840 def handle_include(self, node):841 """842 Include another template at the current position.843 """837- tmpl = self.environment.loader.parse(node.template,838- node.filename)839- return self.handle_node(tmpl.body)844+ tmpl = self.loader.parse(node.template,845+ node.filename)846+ try:847+ return self.handle_node(tmpl.body)848+ finally:849+ self.loader.mark_as_processed()850 851 def handle_trans(self, node):852 """853 Handle translations.324 unmodified linesmodified:jinja/exceptions.py+7 added
80 unmodified lines81 """82 Raised by the template engine if a tag encountered an error when83 rendering.84 """85+86+87+class TemplateIncludeError(TemplateError):88+ """89+ Raised by the `ControlledLoader` if recursive includes where90+ detected.91+ """modified:jinja/loaders.py+49 added-1 removed
17 unmodified lines18 from os import path19 from threading import Lock20 from jinja.parser import Parser21 from jinja.translators.python import PythonTranslator, Template22-from jinja.exceptions import TemplateNotFound, TemplateSyntaxError22+from jinja.exceptions import TemplateNotFound, TemplateSyntaxError, \23+ TemplateIncludeError24 from jinja.utils import CacheDict25 26 27 #: when updating this, update the listing in the jinja package too72 unmodified lines100 __traceback_hide__ = True101 from jinja.debugger import raise_syntax_error102 raise_syntax_error(e, self.environment)103 104+ def get_controlled_loader(self):105+ """106+ Return a loader that runs in a controlled environment. (Keeps107+ track of templates that it loads and is not thread safe).108+ """109+ return ControlledLoader(self.environment, self.loader)110+111 def _loader_missing(self, *args, **kwargs):112 """Helper method that overrides all other methods if no113 loader is defined."""114 raise RuntimeError('no loader defined')1 unmodified line116 def __nonzero__(self):117 return self.available118 119 120+class ControlledLoader(LoaderWrapper):121+ """122+ Used for template extending and including.123+ """124+125+ def __init__(self, environment, loader):126+ LoaderWrapper.__init__(self, environment, loader)127+ self._stack = []128+129+ def get_controlled_loader(self):130+ raise TypeError('Cannot get new controlled loader from an already '131+ 'controlled loader.')132+133+ def mark_as_processed(self):134+ """Mark the last parsed/sourced/included template as processed."""135+ if not self._stack:136+ raise RuntimeError('No template for marking found')137+ self._stack.pop()138+139+ def _controlled(method):140+ def new_method(self, name, *args, **kw):141+ if name in self._stack:142+ raise TemplateIncludeError('Circular imports/extends '143+ 'detected. %r appeared twice.' %144+ name)145+ self._stack.append(name)146+ return method(self, name, *args, **kw)147+ try:148+ new_method.__name__ = method.__name__149+ new_method.__doc__ = method.__doc__150+ except AttributeError:151+ pass152+ return new_method153+154+ get_source = _controlled(LoaderWrapper.get_source)155+ parse = _controlled(LoaderWrapper.parse)156+ load = _controlled(LoaderWrapper.load)157+ del _controlled158+159+160 class BaseLoader(object):161 """162 Use this class to implement loaders.163 659 unmodified lines