diff options
author | Ned Batchelder <ned@nedbatchelder.com> | 2009-11-21 08:31:47 -0500 |
---|---|---|
committer | Ned Batchelder <ned@nedbatchelder.com> | 2009-11-21 08:31:47 -0500 |
commit | 002c59c122f5a72868c9a3835f692968eb689b0a (patch) | |
tree | 6e071722d8bf388dd18d419fc5d7dadb564cb7de | |
parent | 4108517952e9b96658d30dcb87ca098abbe14a14 (diff) | |
download | python-coveragepy-git-002c59c122f5a72868c9a3835f692968eb689b0a.tar.gz |
Add nicer exception reporting inside Templite so when a template goes wrong we have some hope of figuring out why.
-rw-r--r-- | coverage/templite.py | 10 | ||||
-rw-r--r-- | test/test_templite.py | 9 |
2 files changed, 16 insertions, 3 deletions
diff --git a/coverage/templite.py b/coverage/templite.py index f61fbdca..22c93a9e 100644 --- a/coverage/templite.py +++ b/coverage/templite.py @@ -2,7 +2,7 @@ # Coincidentally named the same as http://code.activestate.com/recipes/496702/ -import re +import re, sys class Templite(object): """A simple template renderer, for a nano-subset of Django syntax. @@ -120,7 +120,13 @@ class _TempliteEngine(object): if op == 'lit': self.result += args elif op == 'exp': - self.result += str(self.evaluate(args)) + try: + self.result += str(self.evaluate(args)) + except: + exc_class, exc, tb = sys.exc_info() + new_exc = exc_class("Couldn't evaluate {{ %s }}: %s" + % (args, exc)) + raise exc_class, new_exc, tb elif op == 'if': expr, body = args if self.evaluate(expr): diff --git a/test/test_templite.py b/test/test_templite.py index 8cb25865..6b445aa2 100644 --- a/test/test_templite.py +++ b/test/test_templite.py @@ -191,6 +191,13 @@ class TempliteTest(unittest.TestCase): "@a0b0c0a1b1c1a2b2c2!" ) - + def test_exception(self): + # TypeError: Couldn't evaluate {{ foo.bar.baz }}: + # 'NoneType' object is unsubscriptable + self.assertRaises(TypeError, self.try_render, + "Hey {{foo.bar.baz}} there", {'foo': None}, "Hey XXX there" + ) + + if __name__ == '__main__': unittest.main() |