classification
Title: traceback.TracebackException.format shouldn't format_exc_only() when __traceback__ is None
Type: Stage: resolved
Components: Library (Lib) Versions:
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: Aaron.Meurer, asmeurer, iritkatriel
Priority: normal Keywords:

Created on 2017-05-17 04:03 by Aaron.Meurer, last changed 2020-11-05 00:38 by iritkatriel. This issue is now closed.

Files
File name Uploaded Description Edit
test.py Aaron.Meurer, 2017-05-17 04:03
Messages (11)
msg293837 - (view) Author: Aaron Meurer (Aaron.Meurer) Date: 2017-05-17 04:03
I'm trying to completely hide an exception from the traceback module. From reading the source, it looks like the only way to do this is to set __traceback__ to None (I can also set __suppress_context__ to True, but that only works if I have another exception higher up in the context chain). 

However, this still prints the traceback itself, and the line for SyntaxErrors. Consider the attached test.py. It outputs

ValueError

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "test.py", line 8, in <module>
    raise TypeError
TypeError

  File "<string>", line 1
    a b
      ^
SyntaxError: unexpected EOF while parsing

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "test.py", line 19, in <module>
    raise TypeError
TypeError

I suppose it should also not print the "During handling of the above exception, another exception occurred:" part.
msg380336 - (view) Author: Irit Katriel (iritkatriel) * (Python committer) Date: 2020-11-04 16:16
Setting __traceback__ to None is not going to make the exception disappear.  Note that TracebackException does not represent a traceback, it represent an exception (for rendering in tracebacks). So all information of the exception is rendered by TracebackException.format(), not only the exception's traceback.

It's not entirely clear to me what you are trying to do (what is the output you are hoping to get?) but this looks more like a question than a bug report, so I am closing this issue. If this is still relevant, I'd suggest you ask on the python users list or StackOverflow - you are more likely to receive a prompt response there.
msg380359 - (view) Author: Aaron Meurer (asmeurer) Date: 2020-11-04 20:41
> It's not entirely clear to me what you are trying to do (what is the output you are hoping to get?) but this looks more like a question than a bug report, so I am closing this issue. If this is still relevant, I'd suggest you ask on the python users list or StackOverflow - you are more likely to receive a prompt response there.

If you don't understand an issue, the correct response isn't to close it because you don't understand it. If an issue is unclear, you should ask for clarification, not insult the person who opened it. 

What you described *is* the bug report. If you read even the title you would see that the report is that setting __traceback__ to None doesn't affect the printing of the exception.
msg380360 - (view) Author: Irit Katriel (iritkatriel) * (Python committer) Date: 2020-11-04 20:52
I had no intention to insult you and apologize if I did. I assumed that you forgot about this issue since you didn't chase it for 3 years, but you are right that I should have asked. 

I am reopening it so that you can explain the question. Since you didn't get a response so far, I'm probably not the only one who needs that. 

What do you mean by "completely hide an exception", and why do you think that setting __traceback__ to None should achieve that?
msg380361 - (view) Author: Aaron Meurer (asmeurer) Date: 2020-11-04 21:02
I think I found another way to achieve what I was trying to do, which is why I never pursued this. But I still think it's a bug.

__traceback__ = None isn't documented anywhere that I could find, so I was only able to deduce how it should work from reading the source code. If it is documented somewhere let me know.

I admit my initial report is a bit unclear. If you play with the test.py you can see what is going on

    import traceback

    try:
        raise ValueError
    except Exception as e:
        e.__traceback__ = None
        try:
            raise TypeError
        except:
            traceback.print_exc()

produces this output:


    ValueError

    During handling of the above exception, another exception occurred:

    Traceback (most recent call last):
      File "test.py", line 8, in <module>
        raise TypeError
    TypeError

My goal is to completely hide the caught exception in the traceback printed from the traceback module. It seems odd that it hides everything except for the actual ValueError.
msg380364 - (view) Author: Irit Katriel (iritkatriel) * (Python committer) Date: 2020-11-04 21:27
The traceback is only one part of an exception. It is simply a list of frames that show where the exception travelled between being raised and being caught.

An exception contains information about an error, including the type of the exception, sometimes some parameters like an exception message, and sometimes a traceback.

When exception is just created and before it is ever raised, its traceback is None. But you can still print the rest of the information in it (its type and args). 

I guess you're saying that the __context__ exception of the TypeError in your example has an empty traceback, which means it was never raised, which doesn't make sense because then how did it end up being the __context__ of another exception?

Yeah, __context__ is in a funny state, and the traceback module doesn't try to inspect it and interpret what you may have meant by doing that, it just prints the exception like it prints any other exception and you get a funny output.

I still don't a bug.
msg380367 - (view) Author: Aaron Meurer (asmeurer) Date: 2020-11-04 21:43
I don't think it's helpful to make such a literalistic interpretation. Just because the variable is called "traceback" doesn't mean it should apply only to the things that are *technically* a traceback (and I don't agree anyway that the line containing the exception isn't part of the "traceback").

> I guess you're saying that the __context__ exception of the TypeError in your example has an empty traceback, which means it was never raised

Does it mean that? Again, __traceback__ isn't documented anywhere, so I don't know what it being None really means.

All I know is that it apparently disables the printing of tracebacks in the traceback module, but fails to omit the exception line itself, leading to an unreadable traceback in my example.
msg380369 - (view) Author: Irit Katriel (iritkatriel) * (Python committer) Date: 2020-11-04 21:47
__traceback__  doesn't disable printing tracebacks.  It *is* the traceback. By setting it to None you erased the exception's traceback.
msg380372 - (view) Author: Irit Katriel (iritkatriel) * (Python committer) Date: 2020-11-04 22:24
I'm going to close this issue because it's not a bug.

I understand your question now, as well as the source of your confusion: you thought that traceback is "the thing that is printed to the screen when an exception is raised" and that __traceback__ is something that controls how it's printed.  Those are two misunderstandings and I can see why you thought, on that basis, that there is a bug.

The traceback is only part of what is printed to the screen when an exception is raised, and the __traceback__ field of an exception is a pointer to the actual traceback data.
msg380383 - (view) Author: Aaron Meurer (asmeurer) Date: 2020-11-05 00:25
Neither of those things preclude the possibility of the traceback module doing a better job of printing tracebacks for exceptions where __traceback__ = None.
msg380385 - (view) Author: Irit Katriel (iritkatriel) * (Python committer) Date: 2020-11-05 00:38
The python-ideas list is where ideas for enhancements and improvements are discussed. I suggest you bring this up there. 

https://mail.python.org/mailman3/lists/python-ideas.python.org/
History
Date User Action Args
2020-11-05 00:38:20iritkatrielsetmessages: + msg380385
2020-11-05 00:25:03asmeurersetmessages: + msg380383
2020-11-04 22:24:47iritkatrielsetstatus: open -> closed
resolution: not a bug
messages: + msg380372
2020-11-04 21:47:01iritkatrielsetmessages: + msg380369
2020-11-04 21:43:51asmeurersetmessages: + msg380367
2020-11-04 21:27:46iritkatrielsetmessages: + msg380364
2020-11-04 21:02:50asmeurersetstatus: pending -> open

messages: + msg380361
2020-11-04 20:58:50iritkatrielsetstatus: open -> pending
2020-11-04 20:52:08iritkatrielsetstatus: closed -> open
resolution: not a bug -> (no value)
messages: + msg380360
2020-11-04 20:41:17asmeurersetnosy: + asmeurer
messages: + msg380359
2020-11-04 16:16:10iritkatrielsetstatus: open -> closed

nosy: + iritkatriel
messages: + msg380336

resolution: not a bug
stage: resolved
2017-05-17 04:03:35Aaron.Meurercreate