Edgewall Software

Changeset 818


Ignore:
Timestamp:
Mar 31, 2008, 11:30:26 AM (16 years ago)
Author:
cmlenz
Message:

Improve error reporting when accessing an attribute in a Python expression raises an AttributeError. Closes #191. Thanks to Michele Cella for the patch!

Location:
trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/ChangeLog

    r816 r818  
    7070   The buffering can be disabled using the new `buffer` optimization hint on
    7171   the `<py:match>` directive.
     72 * Improve error reporting when accessing an attribute in a Python expression
     73   raises an `AttributeError` (ticket #191).
    7274
    7375
  • trunk/genshi/template/eval.py

    r807 r818  
    272272    def lookup_attr(cls, obj, key):
    273273        __traceback_hide__ = True
    274         val = getattr(obj, key, UNDEFINED)
    275         if val is UNDEFINED:
    276             try:
    277                 val = obj[key]
    278             except (KeyError, TypeError):
    279                 val = cls.undefined(key, owner=obj)
     274        try:
     275            val = getattr(obj, key)
     276        except AttributeError:
     277            if hasattr(obj.__class__, key):
     278                raise
     279            else:
     280                try:
     281                    val = obj[key]
     282                except (KeyError, TypeError):
     283                    val = cls.undefined(key, owner=obj)
    280284        return val
    281285    lookup_attr = classmethod(lookup_attr)
  • trunk/genshi/template/tests/eval.py

    r798 r818  
    343343    def test_getattr_exception(self):
    344344        class Something(object):
    345             def prop(self):
     345            def prop_a(self):
    346346                raise NotImplementedError
    347             prop = property(prop)
     347            prop_a = property(prop_a)
     348            def prop_b(self):
     349                raise AttributeError
     350            prop_b = property(prop_b)
    348351        self.assertRaises(NotImplementedError,
    349                           Expression('s.prop').evaluate, {'s': Something()})
     352                          Expression('s.prop_a').evaluate, {'s': Something()})
     353        self.assertRaises(AttributeError,
     354                          Expression('s.prop_b').evaluate, {'s': Something()})
    350355
    351356    def test_getitem_undefined_string(self):
Note: See TracChangeset for help on using the changeset viewer.