diff -r ece75a3b942c Lib/cgitb.py --- a/Lib/cgitb.py Wed Dec 05 17:59:29 2012 +0200 +++ b/Lib/cgitb.py Thu Dec 06 22:14:38 2012 +0100 @@ -85,7 +85,10 @@ if ttype == tokenize.NAME and token not in keyword.kwlist: if lasttoken == '.': if parent is not __UNDEF__: - value = getattr(parent, token, __UNDEF__) + try: + value = getattr(parent, token, __UNDEF__) + except Exception: + value = __UNDEF__ vars.append((prefix + token, prefix, value)) else: where, value = lookup(token, frame, locals) diff -r ece75a3b942c Lib/test/test_cgitb.py --- a/Lib/test/test_cgitb.py Wed Dec 05 17:59:29 2012 +0200 +++ b/Lib/test/test_cgitb.py Thu Dec 06 22:14:38 2012 +0100 @@ -62,6 +62,19 @@ self.assertNotIn('

', out) self.assertNotIn('

', out) + def test_exception_in_gettattr_call(self): + # Issue 4643: cgitb.html fails if getattr call raises exception + class WeirdObject(object): + def __getattr__(self, attr): + if attr == 'a': + return str(slf) # Intentional NameError + raise AttributeError(attr) + try: + weird = WeirdObject() + weird.a + except Exception: + html_bt = cgitb.html(sys.exc_info()) + def test_main(): run_unittest(TestCgitb)