[New-bugs-announce] [issue46652] Use code.co_qualname to provide richer information

Gabriele N Tornetta report at bugs.python.org
Sat Feb 5 16:25:55 EST 2022


New submission from Gabriele N Tornetta <phoenix1987 at gmail.com>:

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.

----------
components: Interpreter Core
messages: 412598
nosy: Gabriele Tornetta, pablogsal
priority: normal
severity: normal
status: open
title: Use code.co_qualname to provide richer information
type: enhancement
versions: Python 3.11

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue46652>
_______________________________________


More information about the New-bugs-announce mailing list