New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
cgitb.html fails if getattr call raises exception #48893
Comments
If cgitb.html tries to get the value of an attribute from an object, and If you run the attached file in Python 2.5.2 or 2.6, you get the ---- A: the letter a
B:
Something went wrong - attempting to generate HTML stack trace.
Error generating HTML stack trace!
Traceback (most recent call last):
File "attrtest.py", line 21, in <module>
html_text = cgitb.html(sys.exc_info())
File "C:\python26\lib\cgitb.py", line 133, in html
vars = scanvars(reader, frame, locals)
File "C:\python26\lib\cgitb.py", line 84, in scanvars
value = getattr(parent, token, __UNDEF__)
File "attrtest.py", line 8, in __getattr__
return str(slf) # Intentional NameError
NameError: global name 'slf' is not defined The problem is in the scanvars function - it offers no protection I think scanvars should catch any exceptions that come from the getattr If the line in the attached file is commented out, then the next line is |
In terms of patching scanvars, I came up with the following solution: ORIGINAL: SOLUTION: I think this makes the most sense - it requires a small change, and it My initial patch changed value to be a string to be "could not determine Then for something which shouldn't work, it would determine that the So I would recommend my original patch (as described above) as the best |
In the interests of getting this fixed (and not letting it die), should |
I believe a patch against the trunk would be enough, but should include |
I attached a patch containing both the fix suggested by Allan and a test case. |
The issue still occurs in 3.10. Python 3 version of the script: import cgitb
class WeirdObject(object):
def __getattr__(self, attr):
if attr == 'a':
return 'the letter a'
elif attr == 'b':
return str(slf) # Intentional NameError
raise AttributeError(attr) try: |
Arthur, are you interested in converting your patch to a github pull request? |
Converted into GitHub PR #24038 |
On closer scrutiny I'm not sure this patch should be merged. The getattr call here has a default value, so it should not raise AttributeError. It should also not raise any other exception because a valid implementation of __getattr__ should raise only AttributeError: https://docs.python.org/3/reference/datamodel.html#object.\_\_getattr__ |
but probably the intent of the patch here is to surface a meaningful error with regards to the script, more than an error on the cgitb python lib itself as this is a traceback tool. A bit like an HTML validator which continue to process things to give some kind of meaning. Diving into previous issues about scanvars Similar There is a patch which is lot better for this bug and which addresses the issues here. It has not been merged yet. Not sure why or if there is anything missing. Probably this bug could be closed as a duplicate of https://bugs.python.org/issue966992 And someone needs to push to push the latest bits for #15094 What do you think @iritkatriel ? I will close my PR. |
I agree that this can be closed as duplicate. Thanks, Karl, for the research. |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: