python / python/cpython

warn_explicit() discards the source line obtained from module_globals

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

Ninguém assumiu esta issue ainda.

3.13 3.14 3.15 3.16 extension-modules type-bug
Linguagem predominante
Python
Estrelas
77.2k
Forks
36k
Métricas de merge de PRs
Métricas de PR pendentes

Descrição

The only purpose of the module_globals argument of warnings.warn_explicit() is to get the source line of the warning from the module loader when the file cannot be read. The C implementation computes it in get_source_line(), but then throws it away.

call_show_warning() passes None in the line slot of WarningMessage and ignores its own sourceline argument:

    msg = PyObject_CallFunctionObjArgs(warnmsg_cls, message, category,
            filename, lineno_obj, Py_None, Py_None,
            source ? source : Py_None, module,
            NULL);

sourceline is only used in the fallback show_warning() path, taken when warnings._showwarnmsg is not available.

As a result the source line is lost, while the pure Python implementation, which seeds linecache instead, displays it:

$ cat > spam.py <<EOF
import warnings
def f():
    warnings.warn_explicit('eggs', UserWarning, 'bar', 1, module_globals=globals())
EOF
$ ./python -c 'import spam; spam.f()'
bar:1: UserWarning: eggs
$ ./python -c 'import sys; sys.modules["_warnings"] = None; import spam; spam.f()'
bar:1: UserWarning: eggs
  import warnings

The None was added in 914cde89d4c together with the source argument, so the C implementation has never passed the source line.

Linked PRs
  • gh-155320
  • gh-155824
  • gh-155825
  • gh-155826

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 pelas funções C get_source_line() e call_show_warning(), que a issue identifica como responsáveis por calcular e depois descartar a linha de origem. Reproduza os exemplos de warn_explicit() da issue e verifique se a linha de origem é preservada tanto no caminho de WarningMessage quanto no caminho alternativo de show_warning().

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

Avaliação

Stack de tecnologia
c, python
Domínio
backend
Tipo de issue
Bug
Dificuldade
3/5
Tempo estimado
1-2 dias
Status de atividade
Estagnada
Clareza
Claramente especificada
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.