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: Use code.co_qualname to provide richer information
Type: enhancement Stage: patch review
Components: Interpreter Core Versions: Python 3.11
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: Dennis Sweeney, Gabriele Tornetta, aroberge, pablogsal
Priority: normal Keywords: patch

Created on 2022-02-05 21:25 by Gabriele Tornetta, last changed 2022-04-11 14:59 by admin.

Pull Requests
URL Status Linked Edit
PR 31150 open Gabriele Tornetta, 2022-02-05 21:30
Messages (3)
msg412598 - (view) Author: Gabriele N Tornetta (Gabriele Tornetta) * Date: 2022-02-05 21:25
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.
msg412616 - (view) Author: Dennis Sweeney (Dennis Sweeney) * (Python committer) Date: 2022-02-06 03:58
Similar changes at bpo-40679 accidentally broke Cython when it was assumed that co_qualname was non-null, which was then fixed by defaulting to co_name in that case. I don't know if Cython still produces cases like that, but we should make sure not to needlessly break such cases if it does.
msg412622 - (view) Author: Gabriele N Tornetta (Gabriele Tornetta) * Date: 2022-02-06 10:04
code.co_qualname was introduced in 2021 with bpo-44530 and shuold give the same guarantees as code.co_name. The __qualname__ attribute is derived from code.co_qualname, so perhaps Cython can benefit from accessing code.co_qualname directly instead?
History
Date User Action Args
2022-04-11 14:59:55adminsetgithub: 90810
2022-02-06 10:04:22Gabriele Tornettasetmessages: + msg412622
2022-02-06 03:58:59Dennis Sweeneysetnosy: + Dennis Sweeney
messages: + msg412616
2022-02-06 01:14:13arobergesetnosy: + aroberge
2022-02-05 21:30:44Gabriele Tornettasetkeywords: + patch
stage: patch review
pull_requests: + pull_request29327
2022-02-05 21:25:55Gabriele Tornettacreate