1. edgewall
  2. genshi

Commits

cmlenz  committed d43f504

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.

  • Participants
  • Parent commits 54a4be7
  • Branches trunk

Comments (0)

Files changed (2)

File markup/template.py

View file
         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))
 
             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."""
 
     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)
     """
     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

File markup/tests/template.py

View file
             <span>foo</span>
         </doc>""", 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("""<doc xmlns:py="http://markup.edgewall.org/">
+          <div py:when="xy" />
+        </doc>""")
+        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("""<doc xmlns:py="http://markup.edgewall.org/">
+          <div py:otherwise="" />
+        </doc>""")
+        self.assertRaises(TemplateSyntaxError, str, tmpl.generate())
+
     def test_as_element(self):
         """
         Verify that the directive can also be used as an element.