Skip to content

bpo-4643: Handles failure with getattr exception in cgitb#24038

Closed
karlcow wants to merge 1 commit into
python:masterfrom
karlcow:bpo-4643
Closed

bpo-4643: Handles failure with getattr exception in cgitb#24038
karlcow wants to merge 1 commit into
python:masterfrom
karlcow:bpo-4643

Conversation

@karlcow

@karlcow karlcow commented Jan 1, 2021

Copy link
Copy Markdown
Contributor

This convert a patch to a GitHub PR written by Arthur Petitpierre
and proposed by Allan Crooks
Co-authored-by: Arthur Petitpierre
Co-authored-by: Allan Crooks

https://bugs.python.org/issue4643

This convert a patch to a GitHub PR written by Arthur Petitpierre
and proposed by Allan Crooks
Co-authored-by: Arthur Petitpierre
Co-authored-by: Allan Crooks
Comment thread Lib/cgitb.py
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.

Comment thread Lib/cgitb.py
value = getattr(parent, token, __UNDEF__)
try:
value = getattr(parent, token, __UNDEF__)
except Exception:

@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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants