This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Title: [doc] traceback set with BaseException.with_traceback() pushed back on raise
Type: behavior Stage: resolved
Components: Documentation Versions: Python 3.10, Python 3.9, Python 3.8
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: docs@python Nosy List: abathur, docs@python, emptysquare, iritkatriel, martin.panter, mdk, r.david.murray
Priority: normal Keywords: patch

Created on 2015-04-11 16:29 by abathur, last changed 2022-04-11 14:58 by admin. This issue is now closed.

File name Uploaded Description Edit abathur, 2015-04-11 16:29 issue demonstration martin.panter, 2015-04-11 23:03
issue23915.patch emptysquare, 2015-04-13 14:35 Add example of traceback chaining to with_traceback doc review
Pull Requests
URL Status Linked Edit
PR 23680 merged iritkatriel, 2020-12-07 16:44
Messages (9)
msg240483 - (view) Author: Travis A. Everett (abathur) Date: 2015-04-11 16:29
When BaseException.with_traceback(tb) is used, the __traceback__ property is properly set, but the property gets overwritten when the exception is raised. 

The attached file demonstrates the issue by raising exception a, which doesn't use with_traceback, and exception b, which uses with_traceback(a.__traceback__). It also demonstrates that the exception object can't observe this change. Executing the attached file produces output like:

a.__traceback__ before raise: [None]
a.__traceback__ after raise : [<traceback object at 0x7f95c5a21708>]
b.__traceback__ before raise: [<traceback object at 0x7f95c5a21708>]
b.__traceback__ after raise : [<traceback object at 0x7f95c5a21748>]
msg240487 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2015-04-11 16:50
With_traceback's original oupose is now served by chained tracebacks and 'from'.  However, it is documented to work so it ought to :)
msg240509 - (view) Author: Martin Panter (martin.panter) * (Python committer) Date: 2015-04-11 23:03
My understanding is that the traceback is a linked list. Every time the exception is raised into a calling function or exception handler, a new traceback object is inserted at the front of the list. Your original traceback is not overwritten, it is just pushed back in the list. See my version of the demonstration script. The output is now:

a.__traceback__ before raise : [None]
a.__traceback__ after raise : [<traceback object at 0xb6fb3b1c>, None]
b.__traceback__ before raise : [<traceback object at 0xb6fb3b1c>, None]
b.__traceback__ after raise : [<traceback object at 0xb6fb3b44>, <traceback object at 0xb6fb3b1c>, None]
msg240554 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2015-04-12 13:32
Oh,of course.  This issue was bugging the back of my brain after I commented on it, but I hadn't circled back to figure it out.  Thanks, Martin, I'll close this as not a bug.
msg240559 - (view) Author: Travis A. Everett (abathur) Date: 2015-04-12 15:51
Thanks, Martin--I should've thought to check to see if it'd just been pushed back in the list. I was just focusing on a workaround for another problem and did a double-take when the traceback value didn't match what was set.

This resolution is fine by me, but a note in the doc for with_traceback ( that it'll be immediately bumped back in the list when raised (as in the documentation's usage example) might help clarify for others.
msg240577 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2015-04-13 00:56
Yes, a doc note would be a good idea, I think.
msg240615 - (view) Author: A. Jesse Jiryu Davis (emptysquare) * Date: 2015-04-13 14:35
I've had a very hard time adding to the doc in a way that elucidates rather than further obfuscating; see if you like this patch.
msg382656 - (view) Author: Irit Katriel (iritkatriel) * (Python committer) Date: 2020-12-07 16:48
The new PR has an alternative suggestion for a change, where I tried to incorporate both Travis's and David's points: (1) there are better alternatives now for exception chaining (2) the current frame gets pushed on the traceback when the exception is raised.
msg383181 - (view) Author: Julien Palard (mdk) * (Python committer) Date: 2020-12-16 16:03
New changeset c590c2338e5a36cf3ce5b55e6d366a0675ed1db5 by Irit Katriel in branch 'master':
bpo-23915: update and elucidate documentation of with_traceback (GH-23680)
Date User Action Args
2022-04-11 14:58:15adminsetgithub: 68103
2020-12-16 16:04:24mdksetstatus: open -> closed
resolution: not a bug
2020-12-16 16:03:41mdksetnosy: + mdk
messages: + msg383181
2020-12-07 16:48:10iritkatrielsetnosy: r.david.murray, docs@python, martin.panter, emptysquare, abathur, iritkatriel
messages: + msg382656
stage: patch review -> resolved
2020-12-07 16:44:42iritkatrielsetnosy: + iritkatriel

pull_requests: + pull_request22544
stage: resolved -> patch review
2020-11-04 18:00:34iritkatrielsettitle: traceback set with BaseException.with_traceback() overwritten on raise -> [doc] traceback set with BaseException.with_traceback() pushed back on raise
versions: + Python 3.8, Python 3.9, Python 3.10, - Python 3.4, Python 3.5
2015-04-13 14:35:19emptysquaresetfiles: + issue23915.patch

nosy: + emptysquare
messages: + msg240615

keywords: + patch
2015-04-13 00:56:36r.david.murraysetstatus: closed -> open

assignee: docs@python
components: + Documentation
versions: - Python 3.2, Python 3.3, Python 3.6
nosy: + docs@python

messages: + msg240577
resolution: not a bug -> (no value)
2015-04-12 15:51:44abathursetmessages: + msg240559
2015-04-12 13:32:09r.david.murraysetstatus: open -> closed
resolution: not a bug
messages: + msg240554

stage: resolved
2015-04-11 23:03:49martin.pantersetfiles: +
nosy: + martin.panter
messages: + msg240509

2015-04-11 16:50:36r.david.murraysetnosy: + r.david.murray
messages: + msg240487
2015-04-11 16:29:20abathurcreate