python / python/cpython

Same arguments and more similar documentation for exec() and for eval().

Aberta
#100,068 0 comentários 0 reações 0 responsáveis Ver no GitHub

Ninguém assumiu esta issue ainda.

interpreter-core type-feature
Linguagem predominante
Python
Estrelas
77.2k
Forks
36k
Métricas de merge de PRs
Métricas de PR pendentes

Descrição

Feature or enhancement

This involves changes to the documentation and to the implementation.

I notice that the arguments for exec() and eval() differ in certain ways, which I believe are unnecessary.
When the source is a code object, both calls result in a call to PyEval_EvalCode or, if closure is given, to PyEval_EvalCodeEx.
When the source is a string, there is no closure argument allowed, and the source is compiled as a file input or as an eval input, and then there is a call to PyRun_String or PyRun_StringFlags.
Therefore, the differences between the two functions are solely in the builtin_exec_impl() and builtin_eval_impl() functions in bltinmodule.c., and builtin_exec() and builtin_eval() in bltinmodule.c.h.

  1. eval() doesn't take a closure argument. It would be useful if calling eval('x := 3'), for example, if x is a nonlocal variable.
  2. eval() and exec() both require that a globals argument be an actual dict object or subclass. However, the documentation for exec() is incorrect and should be changed, by removing the phrase ' (and not a subclass of dictionary)'.
  3. When the first argument is a string, eval() returns the result of compiling and running the string as an expresion, and exec() compiles and runs the string as a file input and returns None.

Pitch

I've had difficulty understanding how both of these functions work, due to confusing documentation of them. They largely say the same thing but in different ways, and this obscures the fact that the two functions are mostly the same.

I imagine that the closure argument was not added to eval() as an oversight. However, eval() could be used to access a nonlocal variable in the caller's namespace in the same way as exec() could. It could even modify the variable if the code object is compiled from an expression containing a walrus.

Previous discussion

discussion.
This discussion post has more details and suggestions for making the proposed changes.

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 por bltinmodule.c e bltinmodule.c.h, especialmente builtin_exec_impl(), builtin_eval_impl(), builtin_exec() e builtin_eval(). Leia a discussão vinculada sobre Python antes de decidir o comportamento pretendido dos argumentos e dos closures; em seguida, revise a documentação de exec() e eval(). Considera-se concluído quando a implementação e a documentação descrevem e dão suporte de forma consistente ao comportamento acordado.

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

Avaliação

Stack de tecnologia
python
Domínio
backend, documentation
Tipo de issue
Funcionalidade
Dificuldade
5/5
Tempo estimado
Mais de uma semana
Status de atividade
Estagnada
Clareza
Razoavelmente clara
Facilidade para iniciantes
35/100

Receba novas issues na sua caixa de entrada

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