python / python/cpython

[Tool: c-analyzer] Unexpected Intermediate Error When the Tool Fails

未关闭
#116,828 2 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

type-bug
主要语言
Python
星标
77.2k
派生
36k
PR 合并指标
PR 指标待抓取

描述

Bug report

When the c-analyzer tool (Tools/c-analyzer) fails due to an unsupported C global variable, it is also printing a traceback that indicates a bug somewhere in the tool.

(expand for an example)

From the CI check in gh-116413:

python3.12 ./Tools/c-analyzer/check-c-globals.py \
	--format summary \
	--traceback
analyzing files...
..... ..... ..... ..... .....
..... ..... ..... ..... .....
..... ..... ..... ..... .....
..... ..... ..... ..... .....

..... ..... ..... ..... .....
..... ..... ..... ..... .....
..... ..... ..... ..... .....
..... ..... ..... ..... .....

..... ..... ..... ..... .....
..... ..... ..... ..... .....
..... ..... ..... ..... .....
..... ..... ..... ..... .....

..... ..... ..... ..... .....
..... ..... ..... ..... .....
..... ..... ..... ..... .....
..... ..... ..... ..... .....

..... ..... ..... ..... .....
..... ..... ..... ..... .....
..... ..... ..... ..... .....
..... ..... ..... ..... .....

..... ..... ..... ..... .....
..... ..... ..... ..... .....
..... ..... ..... ..... .....
..... ..... ..... ..... .....

..... ..... ..... ..... .....
..... ..... ..... ..... .....
..... ..... ..... ..... .....
..... ..... ..... ..... .....

..... ....
checking analysis results...
Traceback (most recent call last):

-------------------------
  File "/home/runner/work/cpython/cpython/./Tools/c-analyzer/check-c-globals.py", line 39, in <module>
    main(cmd, cmd_kwargs)
  File "/home/runner/work/cpython/cpython/Tools/c-analyzer/cpython/__main__.py", line 498, in main

Non-constant global variables are generally not supported
in the CPython repo.  We use a tool to analyze the C code
and report if any unsupported globals are found.  The tool
may be run manually with:

  ./python Tools/c-analyzer/check-c-globals.py --format summary [FILE]

Occasionally the tool is unable to parse updated code.
If this happens then add the file to the "EXCLUDED" list
in Tools/c-analyzer/cpython/_parser.py and create a new
issue for fixing the tool (and CC ericsnowcurrently
on the issue).

If the tool reports an unsupported global variable and
it is actually const (and thus supported) then first try
fixing the declaration appropriately in the code.  If that
doesn't work then add the variable to the "should be const"
section of Tools/c-analyzer/cpython/ignored.tsv.

If the tool otherwise reports an unsupported global variable
then first try to make it non-global, possibly adding to
PyInterpreterState (for core code) or module state (for
extension modules).  In an emergency, you can add the
variable to Tools/c-analyzer/cpython/globals-to-fix.tsv
to get CI passing, but doing so should be avoided.  If
this course it taken, be sure to create an issue for
eliminating the global (and CC ericsnowcurrently).

    run_cmd(**cmd_kwargs)
  File "/home/runner/work/cpython/cpython/Tools/c-analyzer/cpython/__main__.py", line 1[59](https://github.com/python/cpython/actions/runs/8281492333/job/22660094475#step:16:60), in cmd_check
    c_analyzer.cmd_check(
  File "/home/runner/work/cpython/cpython/Tools/c-analyzer/c_analyzer/__main__.py", line 331, in cmd_check
    for data, failure in _check_all(decls, checks, failfast=failfast):
  File "/home/runner/work/cpython/cpython/Tools/c-analyzer/c_analyzer/__init__.py", line 94, in check_all
    for data, failure in check(analysis):
  File "/home/runner/work/cpython/cpython/Tools/c-analyzer/cpython/_analyzer.py", line 411, in check_globals
    ignored = read_ignored()
              ^^^^^^^^^^^^^^
  File "/home/runner/work/cpython/cpython/Tools/c-analyzer/cpython/_analyzer.py", line 124, in read_ignored
    _IGNORED.update(_datafiles.read_ignored(NEED_FIX_FILE, relroot=REPO_ROOT))
                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/runner/work/cpython/cpython/Tools/c-analyzer/c_analyzer/datafiles.py", line [93](https://github.com/python/cpython/actions/runs/8281492333/job/22660094475#step:16:94), in read_ignored
    return dict(_iter_ignored(infile, relroot))
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/runner/work/cpython/cpython/Tools/c-analyzer/c_analyzer/datafiles.py", line [107](https://github.com/python/cpython/actions/runs/8281492333/job/22660094475#step:16:108), in _iter_ignored
    varid = _info.DeclID.from_row(varidinfo)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/runner/work/cpython/cpython/Tools/c-analyzer/c_parser/info.py", line 207, in from_row
    return cls(*row)
           ^^^^^^^^^
TypeError: DeclID.__new__() missing 3 required positional arguments: 'filename', 'funcname', and 'name'
make: *** [Makefile:3[16](https://github.com/python/cpython/actions/runs/8281492333/job/22660094475#step:16:17)1: check-c-globals] Error 1
Error: Process completed with exit code 2.
(expand for another example)

From the CI check in gh-116413:

python3.12 ./Tools/c-analyzer/check-c-globals.py \
	--format summary \
	--traceback
analyzing files...
..... ..... ..... ..... .....
..... ..... ..... ..... .....
..... ..... ..... ..... .....
..... ..... ..... ..... .....

..... ..... ..... ..... .....
..... ..... ..... ..... .....
..... ..... ..... ..... .....
..... ..... ..... ..... .....

..... ..... ..... ..... .....
..... ..... ..... ..... .....
..... ..... ..... ..... .....
..... ..... ..... ..... .....

..... ..... ..... ..... .....
..... ..... ..... ..... .....
..... ..... ..... ..... .....
..... ..... ..... ..... .....

..... ..... ..... ..... .....
..... ..... ..... ..... .....
..... ..... ..... ..... .....
..... ..... ..... ..... .....

..... ..... ..... ..... .....
..... ..... ..... ..... .....
..... ..... ..... ..... .....
..... ..... ..... ..... .....

..... ..... ..... ..... .....
..... ..... ..... ..... .....
..... ..... ..... ..... .....
..... ..... ..... ..... .....

..... ....
checking analysis results...
Traceback (most recent call last):
  File "/home/runner/work/cpython/cpython/./Tools/c-analyzer/check-c-globals.py", line 39, in <module>

    main(cmd, cmd_kwargs)
  File "/home/runner/work/cpython/cpython/Tools/c-analyzer/cpython/__main__.py", line 498, in main
-------------------------
    run_cmd(**cmd_kwargs)
  File "/home/runner/work/cpython/cpython/Tools/c-analyzer/cpython/__main__.py", line 1[59](https://github.com/python/cpython/actions/runs/8280034808/job/22655683079#step:16:60), in cmd_check
    c_analyzer.cmd_check(
  File "/home/runner/work/cpython/cpython/Tools/c-analyzer/c_analyzer/__main__.py", line 331, in cmd_check
    for data, failure in _check_all(decls, checks, failfast=failfast):
  File "/home/runner/work/cpython/cpython/Tools/c-analyzer/c_analyzer/__init__.py", line 94, in check_all
    for data, failure in check(analysis):
  File "/home/runner/work/cpython/cpython/Tools/c-analyzer/cpython/_analyzer.py", line 411, in check_globals
    ignored = read_ignored()
              ^^^^^^^^^^^^^^
  File "/home/runner/work/cpython/cpython/Tools/c-analyzer/cpython/_analyzer.py", line 124, in read_ignored
    _IGNORED.update(_datafiles.read_ignored(NEED_FIX_FILE, relroot=REPO_ROOT))
                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/runner/work/cpython/cpython/Tools/c-analyzer/c_analyzer/datafiles.py", line 93, in read_ignored
    return dict(_iter_ignored(infile, relroot))
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/runner/work/cpython/cpython/Tools/c-analyzer/c_analyzer/datafiles.py", line 107, in _iter_ignored
    varid = _info.DeclID.from_row(varidinfo)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/runner/work/cpython/cpython/Tools/c-analyzer/c_parser/info.py", line 207, in from_row
    return cls(*row)
           ^^^^^^^^^
TypeError: DeclID.__new__() missing 3 required positional arguments: 'filename', 'funcname', and 'name'

Non-constant global variables are generally not supported
in the CPython repo.  We use a tool to analyze the C code
and report if any unsupported globals are found.  The tool
may be run manually with:

  ./python Tools/c-analyzer/check-c-globals.py --format summary [FILE]

Occasionally the tool is unable to parse updated code.
If this happens then add the file to the "EXCLUDED" list
in Tools/c-analyzer/cpython/_parser.py and create a new
issue for fixing the tool (and CC ericsnowcurrently
on the issue).

If the tool reports an unsupported global variable and
it is actually const (and thus supported) then first try
fixing the declaration appropriately in the code.  If that
doesn't work then add the variable to the "should be const"
section of Tools/c-analyzer/cpython/ignored.tsv.

If the tool otherwise reports an unsupported global variable
then first try to make it non-global, possibly adding to
PyInterpreterState (for core code) or module state (for
extension modules).  In an emergency, you can add the
variable to Tools/c-analyzer/cpython/globals-to-fix.tsv
to get CI passing, but doing so should be avoided.  If
this course it taken, be sure to create an issue for
eliminating the global (and CC ericsnowcurrently).

make: *** [Makefile:3[16](https://github.com/python/cpython/actions/runs/8280034808/job/22655683079#step:16:17)1: check-c-globals] Error 1
Error: Process completed with exit code 2.

The examples demonstrate that the traceback is coming from a different thread than the expected error message.

Note that, in the examples, the tool doesn't report the unsupported globals it found, like it normally would.

Related: gh-116826

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

调研方向

从 Tools/c-analyzer/c_analyzer/datafiles.py、c_parser/info.py 和 cpython/_analyzer.py 开始,跟踪 check-c-globals.py 命令和 DeclID.from_row() 的 traceback。使用所示命令重现失败;完成的标准是正常报告不受支持的 globals,且不出现无关的中间 traceback。

由索引模型根据 Issue 内容生成。

评估

技术栈
c, python
领域
ci-cd, tooling
Issue 类型
缺陷
难度
3/5
预计耗时
1-2 天
活跃度
停滞
描述清晰度
描述清楚
新手友好度
45/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。