[issue45615] Missing test for type of error when printing traceback for non-exception

Nikita Sobolev report at bugs.python.org
Sun Nov 7 05:54:09 EST 2021


Nikita Sobolev <mail at sobolevn.me> added the comment:

1. Thanks! Yes, this is exactly the case I am talking about.

Right now, this test won't pass:

```
    def test_print_exception_bad_type_python(self):
        with self.assertRaises(TypeError):
            traceback.print_exception(42)
```

Why? Because we don't type check the argument to be `Exception` subtype.
It fails with `AttributeError`. And my question is more like: should we?

I have several opposing thoughts:
- Most likely it is always used with `Exception`. I am pretty sure that
re-implementing exceptions is not something you would do. And it will be in
sync with C code.
- But, on the other hand, it is a breaking change.

вс, 7 нояб. 2021 г. в 13:37, Irit Katriel <report at bugs.python.org>:

>
> Irit Katriel <iritkatriel at gmail.com> added the comment:
>
> 1. I don't think we need such a clone of exception. We just need something
> like these two tests:
>
>     @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).
>
>
> 2. _testcapi is how you call into print_exception directly (for testing).
> If I remove the type check in _testcapi then the test above segfaults with
>
> 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.
>
> ----------
>
> _______________________________________
> Python tracker <report at bugs.python.org>
> <https://bugs.python.org/issue45615>
> _______________________________________
>

----------

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue45615>
_______________________________________


More information about the Python-bugs-list mailing list