Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion Lib/cgitb.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,10 @@ def scanvars(reader, frame, locals):
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:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't catch all subclasses of Exception. Make it more specific.

@iritkatriel iritkatriel Jan 1, 2021 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually this getattr should not raise exceptions at all because it is given a default value.

The docs for __getattr__ say: "This method should either return the (computed) attribute value or raise an AttributeError exception." It is an error for it to raise a different exception, so I think this patch should not be merged.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good catch.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, the issue is saying this is error handling and the code should be more robust in the presence of such malformed objects, so maybe this patch can be merged, but in any case you should not swallow SystemError/MemoryError/OverflowErrors - they should propagate to the user.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So I found an existing better PR for this: #15094 related to https://bugs.python.org/issue966992

I will close this PR here. Thanks @iritkatriel for the pushback, it helped me dig into the issue a bit more.

value = __UNDEF__
vars.append((prefix + token, prefix, value))
else:
where, value = lookup(token, frame, locals)
Expand Down
13 changes: 13 additions & 0 deletions Lib/test/test_cgitb.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,19 @@ def test_syshook_no_logdir_text_format(self):
self.assertNotIn('<p>', out)
self.assertNotIn('</p>', 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())


if __name__ == "__main__":
unittest.main()