Skip to content
Myr

[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

7 files+55 added−19 removed

modified:docs/src/designerdoc.txt+8 added
686 unmodified lines
687688 {{ undefined.attribute().attribute_too[42] }}689 still returns `undefined`.690691Starting with Jinja 1.1 it's possible to control the `undefined` behavior692in the Jinja environment, thus on the application side. If you receive693a `TemplateRuntimeError` when operating on an undefined variable the694environment was created with `silent` disabled.695696In a non silent environment the error you get tells you the line number697and the name of the variable / attribute which could not be found.698699Escaping700========701702Sometimes you might want to add Jinja syntax elements into the template
149 unmodified lines
modified:docs/src/devintro.txt+10 added
72 unmodified lines
73 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========================= ==================================================8889All of these variables except those marked with a star (*) are modifiable after90environment initialization.
90 unmodified lines
modified:jinja/translators/python.py+2 added
82 unmodified lines
83 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 function
843 unmodified lines
modified:jinja/datastructure.py+16 added-15 removed
174 unmodified lines
175 result[key] = value176 return result177178 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)196197 def __setitem__(self, name, value):198 self.current[name] = value199
209 unmodified lines
modified:jinja/environment.py+13 added-4 removed
13 unmodified lines
14from jinja.loaders import LoaderWrapper15from jinja.datastructure import Undefined, Markup, Context, FakeTranslator16from jinja.utils import escape, collect_translations, get_attribute17from jinja.exceptions import FilterNotFound, TestNotFound, \18 SecurityException, TemplateSyntaxError18 SecurityException, TemplateSyntaxError, TemplateRuntimeError19from jinja.defaults import DEFAULT_FILTERS, DEFAULT_TESTS, DEFAULT_NAMESPACE202122class Environment(object):
16 unmodified lines
39 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):4647 # lexer / parser information48 self.block_start_string = block_start_string49 self.block_end_string = block_end_string
10 unmodified lines
60 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_traceback6667 # global namespace68 self.globals = namespace is None and DEFAULT_NAMESPACE.copy() \69 or namespace
24 unmodified lines
94 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 lines
185 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))193194 def get_attributes(self, obj, attributes):195 """196 Get some attributes from an object. If attributes is an
47 unmodified lines
modified:jinja/loaders.py+2 added
82 unmodified lines
83 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)9192 def _loader_missing(self, *args, **kwargs):
386 unmodified lines
modified:CHANGES+4 added
19 unmodified lines
20- strings without unicode characters are processed as binary strings now21 to workaround problems with `datetime.strftime` which only accepts22 binary strings.2324- developer friendly traceback is now toggleable2526- silent variable name failure is now toggleable272829Version 1.030-----------31(released Mar 23, 2007)32
1 unmodified line