Skip to content
This repository has been archived by the owner on Aug 13, 2021. It is now read-only.

Internal problem in Friendly-traceback. Please report this issue. #181

Closed
tomerv opened this issue Feb 1, 2021 · 5 comments
Closed

Internal problem in Friendly-traceback. Please report this issue. #181

tomerv opened this issue Feb 1, 2021 · 5 comments

Comments

@tomerv
Copy link

tomerv commented Feb 1, 2021

class C():
    def __str__(self):
        assert False

assert False, C()    

Google search finds this: https://stackoverflow.com/questions/62520310/exception-str-failed-py3

Enjoy 😉

@aroberge
Copy link
Owner

aroberge commented Feb 1, 2021

Thanks ... this is nasty! ;-)

@aroberge
Copy link
Owner

aroberge commented Feb 1, 2021

Tentative fix; first version.

> python -m friendly_traceback ignore.py

Warning: improperly formed exception.
I suspect that a custom exception has been raised
with a non-string value used as a message.
This can occur if a '__repr__' or a '__str__' method
is incorrectly defined.

╭──────────────────── Python exception ────────────────────╮
│ Traceback (most recent call last):                       │
│   File "ignore.py", line 5, in <module>                  │
│     assert False, C()                                    │
│ AssertionError:                                          │
│                                                          │
│ No information is available about this exception.        │
│                                                          │
│ Exception raised on line 5 of file 'ignore.py'.          │
│                                                          │
│        3:         assert False                           │
│        4:                                                │
│     -->5: assert False, C()                              │
│                                                          │
│     C: <class C> from __main__                           │
╰──────────────────────────────────────────────────────────╯

And, using the example code from the StackOverflow question.

> python -m friendly_traceback ignore.py --format=repl

Warning: improperly formed exception.
I suspect that a custom exception has been raised
with a non-string value used as a message.
This can occur if a '__repr__' or a '__str__' method
is incorrectly defined.

Traceback (most recent call last):
  File "ignore.py", line 26, in <module>
    run_me()
  File "ignore.py", line 24, in run_me
    raise MyTypeError("run_me")
MyTypeError:

    No information is available about this exception.

    Execution stopped on line 26 of file ignore.py.

       24:     raise MyTypeError("run_me")
       25:
    -->26: run_me()

            run_me: <function run_me>

    Exception raised on line 24 of file ignore.py.

       22:
       23: def run_me():
    -->24:     raise MyTypeError("run_me")

            global MyTypeError: <class MyTypeError> from __main__

@tomerv
Copy link
Author

tomerv commented Feb 1, 2021

I wouldn't necessarily say that __str__ is incorrectly defined.

For example:

class C():
    def __init__(self):
        self.open = False
    def __enter__(self):
        self.open = True
        return self
    def __exit__(self, a, b, c):
        self.open = False
    def __str__(self):
        assert self.open
        return "C()"  # could be something more interesting

with C() as c:
    assert False, c

@aroberge
Copy link
Owner

aroberge commented Feb 1, 2021

Perhaps I should add and raises an exception.

@aroberge
Copy link
Owner

aroberge commented Feb 1, 2021

The following is not a case of raising an exception and it still fails.

class C:
    def __repr__(self):
        return None

assert False, C()

So, I will likely use the following:

Warning: improperly formed exception.
I suspect that a custom exception has been raised
with a non-string value used as a message.
This can occur if a '__repr__' or a '__str__' method
is incorrectly defined and/or raises an exception.

aroberge added a commit that referenced this issue Feb 3, 2021
aroberge added a commit that referenced this issue Feb 3, 2021
@aroberge aroberge closed this as completed Feb 3, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants