python / python/cpython

Misbehavior of `f.__code__.co_firstlineno` for decorated functions

Aperta
#139,838 7 commenti 1 reazione 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

interpreter-core topic-parser type-bug
Lingua principale
Python
Stelle
77.2k
Fork
35.9k
Metriche di merge delle PR
Metriche PR in attesa

Descrizione

Bug report

Bug description:

See also: #139783

Compare:

dummy = lambda f: f                 # L1
@dummy                              # L2
def f():                            # L3
    return                          # L4
print(f.__code__.co_firstlineno)    # L5  # result = 2

with

dummy = lambda f: f                 # L1
pass                                # L2
def f():                            # L3
    return                          # L4
print(f.__code__.co_firstlineno)    # L5  # result = 3

and

dummy = lambda f: f                 # L1
@\
dummy                               # L3
def f():                            # L4
    return                          # L5
print(f.__code__.co_firstlineno)    # L6  # result = 3

In the last example, the line continuation after @ is not handled correctly and the first line for f.__code__ then becomes the line at dummy. Because of that, the values returned by inspect.findsource(f) will not be consistent and inspect.getsourcelines will not use the correct source (it will only start at "dummy" and will not contain the "@" part).

@serhiy-storchaka suggested that this is an issue in the AST parser. I however don't know how we should actually fix this.

Consequences to inspect.getsource and other introspection functions:
import inspect

def outer(*args, **kwargs):
    def inner(f):
        return f
    return deco

@(
    outer(
        ...
    )
)
def f():
    return "hello"

print(inspect.getsource(f))

On Python 3.12, the output is expected:

@(
    outer(
        ...
    )
)
def func():
    return "hello"

On Python 3.13.7, the output is missing the first line @(:

    outer(
        ...
    )
)
def func():
    return "hello"

On Python 3.13.9 and main, we are missing @ and outer parentheses:

    outer(
        ...
    )
CPython versions tested on:

CPython main branch

Operating systems tested on:

No response

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Riproduci i tre esempi di decorator e l’esempio di decorator annidati, quindi esamina il comportamento del parser AST insieme a inspect.findsource, inspect.getsourcelines e inspect.getsource. Il lavoro è completato quando le funzioni decorate riportano la prima riga del codice sorgente corretta, inclusi i decorator continuati e le parentesi aperte, e l’introspezione restituisce il codice sorgente completo del decorator.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
python
Ambito
compilers, devtools
Tipo di issue
Bug
Difficoltà
4/5
Tempo stimato
3-5 giorni
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
35/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.