-
-
Notifications
You must be signed in to change notification settings - Fork 29.9k
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
Missing test for type of error when printing traceback for non-exception #89778
Comments
In C code, there is a check in print_exception that the value passed in is of type exception, and it raises TypeError otherwise. This check is not covered by tests, and indeed it is hard to reach it with tests because the _testcapi module has this check as well, which blocks it before the interpreter's check is reached. In traceback.py, there is no such check so this happens:
>>> traceback.print_exception(12)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Users/iritkatriel/src/cpython-654/Lib/traceback.py", line 121, in print_exception
value, tb = _parse_value_tb(exc, value, tb)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/iritkatriel/src/cpython-654/Lib/traceback.py", line 102, in _parse_value_tb
raise TypeError(f"Expected exception, got {exc}")
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: Expected exception, got 12 |
For me something different happens now:
|
You're right Nikita, that's what main currently does. My output was from a branch where I started fixing it. Sorry about the confusion. |
Nikita, if you want to work on this go head, but I would wait until PR 29207 is merged. I think in the C code what we need to do is remove the check in _testcapi - this is a test utility, it doesn't need to verify input types. |
Yes, I would love to! Thanks :) I will wait for #73393 to be merged first. чт, 4 нояб. 2021 г. в 14:02, Irit Katriel <report@bugs.python.org>:
|
It's been merged now, so go ahead. Note that there are two traceback printing functions - in the interpreter in traceback.c and in the library in traceback.py. They are expected to work the same as much as possible. If you add a test to BaseExceptionReportingTests, it will test both versions (see the PyExcReportingTests and PyExcReportingTests subclasses). |
Thanks! пт, 5 нояб. 2021 г. в 12:47, Irit Katriel <report@bugs.python.org>:
|
Couple of thoughts.
def test_non_exception_subtype(self):
class RegularObject:
__traceback__ = None
__suppress_context__ = None
__cause__ = None
__context__ = None
def __call__(self):
return self # we need it for `get_exception` to work
obj = RegularObject()
try:
1 / 0
except Exception as ex:
obj.__traceback__ = ex.__traceback__
err = self.get_report(obj, force=True)
self.assertIn('1 / 0', err) # passes Is it really worth it?
There's no correct way of calling
So, maybe instead we should change Or we can cahnge some levels above. Like What do you think? :) |
@cpython_only
def test_print_exception_bad_type_ct(self):
with self.assertRaises(TypeError):
from _testcapi import exception_print
exception_print(42)
def test_print_exception_bad_type_python(self):
with self.assertRaises(TypeError):
traceback.print_exception(42) It could be that they don't fit in BaseExceptionReportingTests because that is for tests that use get_report. It's fine of they are added separately. The python one can come after test_exception_is_None, and the C one perhaps after test_unhashable (and their names should be slightly different than above).
Assertion failed: (PyExceptionInstance_Check(exc)), function _PyBaseExceptionObject_cast, file exceptions.c, line 321. This issue was created because Erlend found that the type check in print_exception is not covered by tests. It's possible that this check is in the wrong place at the moment. |
Right now, this test won't pass:
Why? Because we don't type check the argument to be I have several opposing thoughts:
вс, 7 нояб. 2021 г. в 13:37, Irit Katriel <report@bugs.python.org>:
|
I see what you mean. I think it's ok in traceback.py to reject an exception clone which is not an instance of BaseException. I agree this should not be backported. You could make that explicit by adding a few words to this sentence in the doc, to make it about the exc value as well as the traceback: "The module uses traceback objects — this is the object type that is stored in the sys.last_traceback variable and returned as the third item from sys.exc_info()." (https://docs.python.org/3/library/traceback.html) The C code fix doesn't need to be backported either - this issue is not something a user had a problem with, we found it through test coverage. |
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: