https://bugs.python.org/issue44530 introduced the co_qualname field to code objects. This could be used to, e.g. enrich the information provided by tracebacks. Consider this simple example
~~~ python
import traceback
class Bogus:
def __init__(self):
traceback.print_stack()
raise RuntimeError("Oh no!")
class Foo:
def __init__(self):
Bogus()
Foo()
~~~
The current output is
~~~
❯ python3.10 test_tb_format.py
File "/home/gabriele/Projects/cpython/test_tb_format.py", line 15, in <module>
Foo()
File "/home/gabriele/Projects/cpython/test_tb_format.py", line 12, in __init__
Bogus()
File "/home/gabriele/Projects/cpython/test_tb_format.py", line 6, in __init__
traceback.print_stack()
Traceback (most recent call last):
File "/home/gabriele/Projects/cpython/test_tb_format.py", line 15, in <module>
Foo()
File "/home/gabriele/Projects/cpython/test_tb_format.py", line 12, in __init__
Bogus()
File "/home/gabriele/Projects/cpython/test_tb_format.py", line 7, in __init__
raise RuntimeError("Oh no!")
RuntimeError: Oh no!
~~~
The proposed change is to use the co_qualname field instead of co_name to provide more immediate information about the distinct functions __init__, viz.
~~~
❯ ./python test_tb_format.py
File "/home/gabriele/Projects/cpython/test_tb_format.py", line 15, in <module>
Foo()
File "/home/gabriele/Projects/cpython/test_tb_format.py", line 12, in Foo.__init__
Bogus()
File "/home/gabriele/Projects/cpython/test_tb_format.py", line 6, in Bogus.__init__
traceback.print_stack()
Traceback (most recent call last):
File "/home/gabriele/Projects/cpython/test_tb_format.py", line 15, in <module>
Foo()
^^^^^
File "/home/gabriele/Projects/cpython/test_tb_format.py", line 12, in Foo.__init__
Bogus()
^^^^^^^
File "/home/gabriele/Projects/cpython/test_tb_format.py", line 7, in Bogus.__init__
raise RuntimeError("Oh no!")
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
RuntimeError: Oh no!
~~~
This makes it clear that two distinct __init__ functions are involved, without having to look at sources.
|