# HG changeset patch # User cmlenz # Date 1155826408 0 # Node ID 718cba809cea4a985bd32878c565129b5e4fb57d # Parent 4ed68a904235fabe49517801371c0364095f9728 Better error reporting for errors in directive expressions, and when `py:otherwise`/`py:when` are used outside a `py:choose` directive. Thanks to Christian Boos for the initial patch. diff --git a/markup/template.py b/markup/template.py --- a/markup/template.py +++ b/markup/template.py @@ -152,6 +152,8 @@ try: self.expr = value and Expression(value, filename, lineno) or None except SyntaxError, err: + err.msg += ' in expression "%s" of "%s" directive' % (value, + self.name) raise TemplateSyntaxError(err, filename, lineno, offset + (err.offset or 0)) @@ -164,6 +166,11 @@ expr = ' "%s"' % self.expr.source return '<%s%s>' % (self.__class__.__name__, expr) + def name(self): + """Return the local name of the directive as it is used in templates.""" + return self.__class__.__name__.lower().replace('directive', '') + name = property(name) + def _apply_directives(stream, ctxt, directives): """Apply the given directives to the stream.""" @@ -593,6 +600,9 @@ def __call__(self, stream, ctxt, directives): choose = ctxt['_choose'] + if not choose: + raise TemplateSyntaxError('when directives can only be used inside ' + 'a choose directive', *stream.next()[2]) if choose.matched: return [] value = self.expr.evaluate(ctxt) @@ -615,6 +625,10 @@ """ def __call__(self, stream, ctxt, directives): choose = ctxt['_choose'] + if not choose: + raise TemplateSyntaxError('an otherwise directive can only be used ' + 'inside a choose directive', + *stream.next()[2]) if choose.matched: return [] choose.matched = True diff --git a/markup/tests/template.py b/markup/tests/template.py --- a/markup/tests/template.py +++ b/markup/tests/template.py @@ -124,6 +124,26 @@ foo """, str(tmpl.generate())) + def test_when_outside_choose(self): + """ + Verify that a `when` directive outside of a `choose` directive is + reported as an error. + """ + tmpl = Template(""" +
+ """) + self.assertRaises(TemplateSyntaxError, str, tmpl.generate()) + + def test_when_outside_choose(self): + """ + Verify that an `otherwise` directive outside of a `choose` directive is + reported as an error. + """ + tmpl = Template(""" +
+ """) + self.assertRaises(TemplateSyntaxError, str, tmpl.generate()) + def test_as_element(self): """ Verify that the directive can also be used as an element.