[svn] some changes in jinja regarding silent and non silent failure, the exception fun and documented block.super on the webpage
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 2da479f · Browse files · 7 files·+55 added−19 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
7 files+55 added−19 removed
- other:+55 added−19 removed
Provenance
- Commit
40cf47c- Tree
66be057- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent
2da479f
7 files changed+55 added−19 removed
modified:docs/src/designerdoc.txt+8 added
686 unmodified lines687 688 {{ undefined.attribute().attribute_too[42] }}689 still returns `undefined`.690 691+Starting with Jinja 1.1 it's possible to control the `undefined` behavior692+in the Jinja environment, thus on the application side. If you receive693+a `TemplateRuntimeError` when operating on an undefined variable the694+environment was created with `silent` disabled.695+696+In a non silent environment the error you get tells you the line number697+and the name of the variable / attribute which could not be found.698+699 Escaping700 ========701 702 Sometimes you might want to add Jinja syntax elements into the template149 unmodified linesmodified:docs/src/devintro.txt+10 added
72 unmodified lines73 defined.74 `tests` dict of tests of the default tests if not defined.75 `context_class` the context class this template should use. See76 the `context documentation`_ for more details.77+`silent` set this to `False` if you want to receive errors78+ for missing template variables or attributes.79+ Defaults to `False`. *new in Jinja 1.1*80+`friendly_traceback` Set this to `False` to disable the developer81+ friendly traceback rewriting. Whenever an82+ runtime or syntax error occours jinja will try83+ to make a developer friendly traceback that shows84+ the error in the template line. This however can85+ be annoying when debugging broken functions which86+ are called from the template. *new in Jinja 1.1*87 ========================= ==================================================88 89 All of these variables except those marked with a star (*) are modifiable after90 environment initialization.90 unmodified linesmodified:jinja/translators/python.py+2 added
82 unmodified lines83 ctx = self.environment.context_class(self.environment, *args, **kwargs)84 try:85 return capture_generator(self.generate_func(ctx))86 except:87+ if not self.environment.friendly_traceback:88+ raise89 # debugging system:90 # on any exception we first get the current exception information91 # and skip the internal frames (currently either one (python2.5)92 # or two (python2.4 and lower)). After that we call a function843 unmodified linesmodified:jinja/datastructure.py+16 added-15 removed
174 unmodified lines175 result[key] = value176 return result177 178 def __getitem__(self, name):179- if name.startswith('::'):179+ if not name.startswith('::'):180+ # because the stack is usually quite small we better use [::-1]181+ # which is faster than reversed() somehow.182+ for d in self._stack[::-1]:183+ if name in d:184+ rv = d[name]185+ if rv.__class__ is Deferred:186+ rv = rv(self, name)187+ # never touch the globals!188+ if d is self.globals:189+ self.initial[name] = rv190+ else:191+ d[name] = rv192+ return rv193+ if self.environment.silent:194 return Undefined181- # because the stack is usually quite small we better use [::-1]182- # which is faster than reversed() somehow.183- for d in self._stack[::-1]:184- if name in d:185- rv = d[name]186- if rv.__class__ is Deferred:187- rv = rv(self, name)188- # never touch the globals!189- if d is self.globals:190- self.initial[name] = rv191- else:192- d[name] = rv193- return rv194- return Undefined195+ raise TemplateRuntimeError('%r is not defined' % name)196 197 def __setitem__(self, name, value):198 self.current[name] = value199 209 unmodified linesmodified:jinja/environment.py+13 added-4 removed
13 unmodified lines14 from jinja.loaders import LoaderWrapper15 from jinja.datastructure import Undefined, Markup, Context, FakeTranslator16 from jinja.utils import escape, collect_translations, get_attribute17 from jinja.exceptions import FilterNotFound, TestNotFound, \18- SecurityException, TemplateSyntaxError18+ SecurityException, TemplateSyntaxError, TemplateRuntimeError19 from jinja.defaults import DEFAULT_FILTERS, DEFAULT_TESTS, DEFAULT_NAMESPACE20 21 22 class Environment(object):16 unmodified lines39 namespace=None,40 loader=None,41 filters=None,42 tests=None,43- context_class=Context):43+ context_class=Context,44+ silent=True,45+ friendly_traceback=True):46 47 # lexer / parser information48 self.block_start_string = block_start_string49 self.block_end_string = block_end_string10 unmodified lines60 self.filters = filters is None and DEFAULT_FILTERS.copy() or filters61 self.tests = tests is None and DEFAULT_TESTS.copy() or tests62 self.default_filters = default_filters or []63 self.context_class = context_class64+ self.silent = silent65+ self.friendly_traceback = friendly_traceback66 67 # global namespace68 self.globals = namespace is None and DEFAULT_NAMESPACE.copy() \69 or namespace24 unmodified lines94 from jinja.translators.python import PythonTranslator95 try:96 rv = PythonTranslator.process(self, Parser(self, source).parse())97 except TemplateSyntaxError, e:94- # if errors occour raise a better traceback98+ # on syntax errors rewrite the traceback if wanted99+ if not self.friendly_traceback:100+ raise101 from jinja.utils import raise_syntax_error102 __traceback_hide__ = True103 raise_syntax_error(e, self, source)104 else:80 unmodified lines185 try:186 return get_attribute(obj, name)187 except (AttributeError, SecurityException):188 pass189+ if self.silent:183- return Undefined190+ return Undefined191+ raise TemplateRuntimeError('attribute %r or object %r not defined' % (192+ name, obj))193 194 def get_attributes(self, obj, attributes):195 """196 Get some attributes from an object. If attributes is an47 unmodified linesmodified:jinja/loaders.py+2 added
82 unmodified lines83 name = str(name)84 try:85 return self.loader.load(self.environment, name, translator)86 except TemplateSyntaxError, e:87+ if not self.environment.friendly_traceback:88+ raise89 __traceback_hide__ = True90 raise_syntax_error(e, self.environment)91 92 def _loader_missing(self, *args, **kwargs):386 unmodified linesmodified:CHANGES+4 added
19 unmodified lines20 - strings without unicode characters are processed as binary strings now21 to workaround problems with `datetime.strftime` which only accepts22 binary strings.23 24+- developer friendly traceback is now toggleable25 26+- silent variable name failure is now toggleable27+28+29 Version 1.030 -----------31 (released Mar 23, 2007)32 1 unmodified line