diff options
author | Chris Jerdonek <chris.jerdonek@gmail.com> | 2012-05-03 20:32:23 -0700 |
---|---|---|
committer | Chris Jerdonek <chris.jerdonek@gmail.com> | 2012-05-03 20:32:23 -0700 |
commit | d9554ae27d2468b7f03acdcb59ee1bd020abde3e (patch) | |
tree | 632c9408de272af5db6b369f5ea0ad8119fbdf8b /pystache | |
parent | 3c839348727aab1da9ee48e532124083df3a364d (diff) | |
download | pystache-d9554ae27d2468b7f03acdcb59ee1bd020abde3e.tar.gz |
Fixed part of issue #110: accessing from context a property raising an exception.
Diffstat (limited to 'pystache')
-rw-r--r-- | pystache/context.py | 9 | ||||
-rw-r--r-- | pystache/tests/test_context.py | 20 |
2 files changed, 28 insertions, 1 deletions
diff --git a/pystache/context.py b/pystache/context.py index 8a95059..cf309d6 100644 --- a/pystache/context.py +++ b/pystache/context.py @@ -55,8 +55,15 @@ def _get_value(context, key): # types like integers and strings as objects (cf. issue #81). # Instances of user-defined classes on the other hand, for example, # are considered objects by the test above. - if hasattr(context, key): + try: attr = getattr(context, key) + except AttributeError: + # TODO: distinguish the case of the attribute not existing from + # an AttributeError being raised by the call to the attribute. + # See the following issue for implementation ideas: + # http://bugs.python.org/issue7559 + pass + else: # TODO: consider using EAFP here instead. # http://docs.python.org/glossary.html#term-eafp if callable(attr): diff --git a/pystache/tests/test_context.py b/pystache/tests/test_context.py index 0c5097b..fb69981 100644 --- a/pystache/tests/test_context.py +++ b/pystache/tests/test_context.py @@ -147,6 +147,26 @@ class GetValueTests(unittest.TestCase, AssertIsMixin): self.assertEqual(item["foo"], "bar") self.assertNotFound(item, "foo") + def test_object__property__raising_exception(self): + """ + Test getting a property that raises an exception. + + """ + class Foo(object): + + @property + def bar(self): + return 1 + + @property + def baz(self): + raise ValueError("test") + + foo = Foo() + self.assertEqual(_get_value(foo, 'bar'), 1) + self.assertNotFound(foo, 'missing') + self.assertRaises(ValueError, _get_value, foo, 'baz') + ### Case: the item is an instance of a built-in type. def test_built_in_type__integer(self): |