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.

classification
Title: stack tracebacks should give the relevant class name
Type: enhancement Stage:
Components: Interpreter Core Versions: Python 3.10
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: Winterflower, amaury.forgeotdarc, pitrou, stickwithjosh, terry.reedy, zach.ware
Priority: normal Keywords:

Created on 2011-01-25 15:12 by stickwithjosh, last changed 2022-04-11 14:57 by admin.

Messages (10)
msg127015 - (view) Author: Joshua Blount (stickwithjosh) Date: 2011-01-25 15:12
When I get a stack traceback, it would be very handy if the traceback gave me the relevant class name (along with all the current information).
msg127035 - (view) Author: Skip Montanaro (skip.montanaro) * (Python triager) Date: 2011-01-25 18:30
Given this code:

#!/usr/bin/env python

class C:
    def bomb(self):
        1/0

c = C()
c.bomb()

when run it produces

Traceback (most recent call last):
  File "<stdin>", line 8, in <module>
  File "<stdin>", line 5, in bomb
ZeroDivisionError: integer division or modulo by zero

You would like "bomb" to be "C.bomb"?
msg127037 - (view) Author: Joshua Blount (stickwithjosh) Date: 2011-01-25 18:34
Yes!
msg127040 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2011-01-25 18:54
Well, given you have the line number at which the method is defined, it is easy to know which class it belongs to.
msg127042 - (view) Author: Skip Montanaro (skip.montanaro) * (Python triager) Date: 2011-01-25 18:55
I'm not sure you can get there from here, certainly not in a
straightforward way.  The traceback formatter gets a reference to
the code object (traceback -> frame -> code).  That object has a name
attribute (which is what's displayed) but it doesn't have a reference
back to the function object.
msg127044 - (view) Author: Skip Montanaro (skip.montanaro) * (Python triager) Date: 2011-01-25 19:31
I agree with Antoine, however, if you can come up with a
reasonable patch which implements the desired behavior, I
think it would be reasonable to add it to Python 3.3.  The
definition of "reasonable" is subject to interpretation.
As I indicated in my earlier comment, there is no straight
path from the traceback object to the surrounding class.
msg127122 - (view) Author: Amaury Forgeot d'Arc (amaury.forgeotdarc) * (Python committer) Date: 2011-01-26 16:56
trace.py has a hack for this: it uses gc.get_referrers() to crawl back from the code object to the encloding class.
I would not trust this gc.get_referrers() though; it is probably slow, and there are even some crashers related to this function; I would not like it to be called on every traceback.
msg130417 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2011-03-09 04:06
I am not sure that traceback changes should be made in bug-fix releases, though it is a moot point until a change is ready for 3.3.

That said, having a bit more info in tracebacks so they can more often be understood without looking through possibly many source files would be good.
msg283138 - (view) Author: Camilla Montonen (Winterflower) Date: 2016-12-13 20:06
Reproduced original issue in 3.5.1.
msg283140 - (view) Author: Zachary Ware (zach.ware) * (Python committer) Date: 2016-12-13 20:37
__qualname__ has become a thing since this issue was opened, it may make implementation of this feature significantly easier.
History
Date User Action Args
2022-04-11 14:57:11adminsetgithub: 55216
2020-12-07 19:02:11iritkatriellinkissue39625 superseder
2020-11-04 18:17:04iritkatrielsetversions: + Python 3.10, - Python 3.7
2016-12-13 20:37:36zach.waresetnosy: + zach.ware

messages: + msg283140
versions: + Python 3.7, - Python 3.5
2016-12-13 20:06:46Winterflowersetnosy: + Winterflower

messages: + msg283138
versions: + Python 3.5, - Python 3.3
2011-03-19 19:08:35skip.montanarosetnosy: - skip.montanaro
2011-03-09 04:06:46terry.reedysetnosy: + terry.reedy

messages: + msg130417
versions: - Python 2.6, Python 2.5, Python 3.1, Python 2.7, Python 3.2
2011-01-26 16:56:57amaury.forgeotdarcsetnosy: + amaury.forgeotdarc
messages: + msg127122
2011-01-25 19:31:27skip.montanarosetmessages: + msg127044
2011-01-25 18:55:51skip.montanarosetmessages: + msg127042
2011-01-25 18:54:04pitrousetnosy: + pitrou
messages: + msg127040
2011-01-25 18:34:05stickwithjoshsetmessages: + msg127037
2011-01-25 18:30:41skip.montanarosetnosy: + skip.montanaro
messages: + msg127035
2011-01-25 15:12:24stickwithjoshcreate