Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add co_qualname attribute in code objects #57881

Closed
Arfrever mannequin opened this issue Dec 28, 2011 · 10 comments
Closed

Add co_qualname attribute in code objects #57881

Arfrever mannequin opened this issue Dec 28, 2011 · 10 comments
Labels
interpreter-core (Objects, Python, Grammar, and Parser dirs) type-feature A feature request or enhancement

Comments

@Arfrever
Copy link
Mannequin

Arfrever mannequin commented Dec 28, 2011

BPO 13672
Nosy @birkenfeld, @jcea, @pitrou, @meadori, @ericsnowcurrently, @alexmojaki
Superseder
  • bpo-44530: Propagate qualname from the compiler unit to code objects for finer grained profiling data
  • Files
  • issue13672-v0.patch
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = None
    closed_at = <Date 2021-12-30.00:20:44.766>
    created_at = <Date 2011-12-28.19:19:59.575>
    labels = ['interpreter-core', 'type-feature']
    title = 'Add co_qualname attribute in code objects'
    updated_at = <Date 2021-12-30.00:20:44.765>
    user = 'https://bugs.python.org/Arfrever'

    bugs.python.org fields:

    activity = <Date 2021-12-30.00:20:44.765>
    actor = 'iritkatriel'
    assignee = 'none'
    closed = True
    closed_date = <Date 2021-12-30.00:20:44.766>
    closer = 'iritkatriel'
    components = ['Interpreter Core']
    creation = <Date 2011-12-28.19:19:59.575>
    creator = 'Arfrever'
    dependencies = []
    files = ['24317']
    hgrepos = []
    issue_num = 13672
    keywords = ['patch']
    message_count = 10.0
    messages = ['150312', '150313', '150316', '150317', '151922', '151924', '164011', '164012', '186802', '362261']
    nosy_count = 8.0
    nosy_names = ['georg.brandl', 'jcea', 'pitrou', 'Arfrever', 'meador.inge', 'eric.snow', 'James.Pye', 'alexmojaki']
    pr_nums = []
    priority = 'normal'
    resolution = 'duplicate'
    stage = 'resolved'
    status = 'closed'
    superseder = '44530'
    type = 'enhancement'
    url = 'https://bugs.python.org/issue13672'
    versions = ['Python 3.5']

    @Arfrever
    Copy link
    Mannequin Author

    Arfrever mannequin commented Dec 28, 2011

    PEP-3155 added qualified name as __qualname__ attribute in classes and functions. It would be useful if qualified name was also available as co_qualname attribute of code objects.

    >>> import sys
    >>> class A:
    ...     def f1():
    ...         return B.f2()
    ... 
    >>> class B:
    ...     def f2():
    ...         return sys._getframe(1)
    ...
    >>> A.f1.__name__
    'f1'
    >>> A.f1.__qualname__
    'A.f1'
    >>> B.f2.__name__
    'f2'
    >>> B.f2.__qualname__
    'B.f2'
    >>> frame = A.f1()
    >>> frame
    <frame object at 0x7f9c1adca3a0>
    >>> frame.f_code
    <code object f1 at 0x7f9c1ae4be40, file "<stdin>", line 2>
    >>> frame.f_code.co_name
    'f1'
    >>> frame.f_code.co_qualname
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    AttributeError: 'code' object has no attribute 'co_qualname'
    
    
    Suggested behavior:
    >>> frame.f_code.co_qualname
    'A.f1'

    @Arfrever Arfrever mannequin added the interpreter-core (Objects, Python, Grammar, and Parser dirs) label Dec 28, 2011
    @ericsnowcurrently
    Copy link
    Member

    with f_func (see python/cpython#57066) you would get that for free:
    >>> frame.f_func.__qualname__
    'A.f1'

    @Arfrever
    Copy link
    Mannequin Author

    Arfrever mannequin commented Dec 28, 2011

    co_qualname could still be useful if somebody has code object without frame object.

    @ericsnowcurrently
    Copy link
    Member

    True. I wonder, though if perhaps a co_func (as a weak ref) or co_orig_func would be better, since co_qualname would be built from the original function anyway. Then you could call "code.co_func.func_qualname".

    One sticky point is that there isn't a guarantee of one-to-one between function object and code object. A code object could be bound to several different functions as happens with function definitions (particularly lambdas) inside comprehensions.

    Also, if a code object is not associated with a function, i.e. one generated by exec, what should the qualname for the code object be? How about, in CPython, the code objects created for classes and modules?

    @meadori meadori added the type-feature A feature request or enhancement label Dec 28, 2011
    @meadori
    Copy link
    Member

    meadori commented Jan 24, 2012

    On Wed, Dec 28, 2011 at 3:11 PM, Eric Snow <report@bugs.python.org> wrote:

    One sticky point is that there isn't a guarantee of one-to-one between function object and code object.  A code object could be bound to several
    different functions as happens with function definitions (particularly lambdas) inside comprehensions.

    Also, if a code object is not associated with a function, i.e. one generated by exec, what should the qualname for the code object be?  How
    about, in CPython, the code objects created for classes and modules?

    We already these issues with 'co_name', though. These cases can be
    treated the same as they are for 'co_name':

    '<listcomp>'
    >>> compile('[i for i in [1, 2]]', '<foo>', 'exec').co_consts[0].co_qualname
    '<listcomp>'
    >>> compile('class T: pass', '<foo>', 'exec').co_consts[0].co_qualname
    'T'
    >>> compile('class T: pass', '<foo>', 'exec').co_consts[0].co_name
    'T'
    >>> compile('a = 12', '<foo>', 'exec').co_name
    '<module>'
    >>> compile('a = 12', '<foo>', 'exec').co_qualname
    '<module>'
    >>> compile('lambda x: x', '<foo>', 'exec').co_consts[0].co_qualname
    '<lambda>'
    >>> compile('lambda x: x', '<foo>', 'exec').co_consts[0].co_name
    '<lambda>'

    @meadori
    Copy link
    Member

    meadori commented Jan 24, 2012

    This seems to be a useful feature to me. Another area where it can help
    is with fixing function types. Currently the qualname can be lost:

    >>> def f():
    ...    def g():
    ...       pass
    ...    return g
    ... 
    >>> g = f()
    >>> g
    <function f.<locals>.g at 0x7f1dac4d8ba0>
    >>> types.FunctionType(f.__code__, {})
    <function f at 0x7f1dac4dfae0>
    >>> types.FunctionType(g.__code__, {})

    There is no way to specify a qualname when constructing a
    'FunctionType'. The name is derived from the code object. The
    qualname could be too if we fix this issue. I will open another issue
    for the 'FunctionType' problem.

    I have attached a WIP patch. It passes the full test suite. I am
    somewhat unsure about the CAPI changes, though. They are a breaking API
    change. Do we need a *_QualName version instead like we did for
    'PyFunction_NewWithQualName'? I think probably so. I will add doc
    updates to the patch once the API stuff gets worked out.

    @Arfrever
    Copy link
    Mannequin Author

    Arfrever mannequin commented Jun 25, 2012

    Could this patch be included in Python 3.3?

    @pitrou
    Copy link
    Member

    pitrou commented Jun 25, 2012

    PyCode_New and PyCode_NewEmpty are documented public APIs, so you can't change their signatures like that.

    @JamesPye
    Copy link
    Mannequin

    JamesPye mannequin commented Apr 13, 2013

    Considering the API changes necessary for adding qualname, perhaps a better solution would be to just start using the qualname instead of the function's "basename"--co_name is the qualname. This would offer an automatic improvement to the readability of coverage/profiling tool reports. Admittedly, not sure what kind of breakage might ensue... =\

    @alexmojaki
    Copy link
    Mannequin

    alexmojaki mannequin commented Feb 19, 2020

    I think this would be very useful in tracebacks. If a traceback line had a more useful name like MyClass.__init__ instead of just __init__ or my_decorator.<locals>.wrapper instead of just wrapper it would provide useful context to the frame.

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    interpreter-core (Objects, Python, Grammar, and Parser dirs) type-feature A feature request or enhancement
    Projects
    None yet
    Development

    No branches or pull requests

    4 participants