python / python/cpython

Weird `inspect.getsource` behavior with generators

Aberta
#121,331 3 comentários 0 reações 0 responsáveis Ver no GitHub

Ninguém assumiu esta issue ainda.

stdlib type-bug
Linguagem predominante
Python
Estrelas
77.2k
Forks
35.9k
Métricas de merge de PRs
Métricas de PR pendentes

Descrição

Bug report

Bug description:

If you return two generators (one from a generator function and one a generator expression) from a function and use getsource on each, you get the code for the generator function for both.

def get_2_generators():
    def my_generator_1():
        for x in range(5):
            yield x*2 
    my_generator_2 = (x*3 for x in range(6))
    return my_generator_1(), my_generator_2

g1, g2 = get_2_generators()

getsource(g1.gi_code)
>>>'    def my_generator_1():\n        for x in range(5):\n            yield x*2 \n'

getsource(g2.gi_code)
>>>'    def my_generator_1():\n        for x in range(5):\n            yield x*2 \n'

Further, presumably not a bug but a current limitation, in general using getsource on generator-expression objects returns the code of the function they were created in, rather than the code of the generator expression itself, which can make things confusing when there are multiple such expressions, e.g.:

def get_2_generators():
    my_generator_1 = (x*2 for x in range(5)) 
    my_generator_2 = (x*3 for x in range(6))
    return my_generator_1, my_generator_2

g1, g2 = get_2_generators()
getsource(g1.gi_code)
>>>'def get_2_generators():\n    my_generator_1 = (x*2 for x in range(5)) \n    my_generator_2 = (x*3 for x in range(6))\n    return my_generator_1, my_generator_2\n'

getsource(g2.gi_code)
>>>'def get_2_generators():\n    my_generator_1 = (x*2 for x in range(5)) \n    my_generator_2 = (x*3 for x in range(6))\n    return my_generator_1, my_generator_2\n'

Can we fix both of the above by making getsource return the code of the generator expression itself?

CPython versions tested on:

3.12

Operating systems tested on:

Windows

Linked PRs
  • gh-121933

Guia de contribuição

Abrir o guia de contribuição

Primeiros passos

  1. Leia a issue inteira e depois o guia de contribuição do projeto.
  2. Comente na issue dizendo que vai assumir — evita que duas pessoas façam o mesmo trabalho.
  3. Faça um fork do repositório e trabalhe em uma branch.
  4. Abra um pull request que referencie o número da issue.

Direção de pesquisa

Comece com inspect.getsource e os exemplos de generator gi_code no relatório; em seguida, revise o PR vinculado gh-121933 para conhecer a direção atual. Compare os casos de funções geradoras e expressões geradoras e verifique se o trabalho concluído distingue vários geradores, em vez de retornar código-fonte envolvente não relacionado.

Escrita pelo modelo de indexação a partir do texto da issue.

Avaliação

Stack de tecnologia
python
Domínio
developer-experience
Tipo de issue
Bug
Dificuldade
5/5
Tempo estimado
Mais de uma semana
Status de atividade
Estagnada
Clareza
Razoavelmente clara
Facilidade para iniciantes
25/100

Receba novas issues na sua caixa de entrada

Um resumo curto de issues do GitHub para quem está começando.