python / python/cpython

[Tool: c-analyzer] Print Helpful Error Messages for Invalid .tsv Files

未關閉
#116,826 0 則留言 1 個 reaction 已指派 0 人 在 GitHub 檢視

還沒有人認領這個 Issue。

type-feature
主要語言
Python
星號
77.2k
分支
36k
PR 合併指標
PR 指標待擷取

描述

Feature or enhancement

CI uses the c-analyzer tool (Tools/c-analyzer) to make sure we don't add any new unsupported C global variables to the code base. The tool makes use of .tsv files (tab-separated values) for some customization. The most import of these files are Tools/c-analyzer/cpython/ignored.tsv and .../globals-to-fix.tsv. If someone adds a line that has spaces instead of tabs (or spaces around the tabs), which is easy to do, then the tool unhelpfully fails as though the line weren't there.

(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...

[extraneous text snipped]

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).

[extraneous text snipped]

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.

There's a similar situation when someone adds a line but forgets one of the row values, likely the last one which is usually a single hyphen. Such a line gets ignored without any indication to the user what went wrong or what to do about it.

(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...

[extraneous text snipped]

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.

It would be helpful if the tool failed with an appropriate specific message for the following cases:

  • spaces instead of tabs
  • spaces next to tabs
  • incomplete lines

CC @iritkatriel

貢獻指南

開啟貢獻指南

從這裡開始

  1. 先讀完整個 Issue,再讀專案的貢獻指南。
  2. 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
  3. Fork 儲存庫,在一個分支上完成修改。
  4. 送出 Pull Request,並在描述裡引用這個 Issue 編號。

研究方向

從 Tools/c-analyzer 開始,檢查 check-c-globals.py 如何讀取 Tools/c-analyzer/cpython/ignored.tsv 和 globals-to-fix.tsv。針對格式錯誤的 TSV 範例執行 ./python Tools/c-analyzer/check-c-globals.py --format summary。當工具針對以空格代替定位字元、定位字元旁有空格,以及不完整的行回報具體錯誤時,即表示完成。

由索引模型根據 Issue 內容生成。

評估

技術堆疊
python
領域
tooling
Issue 類型
功能
難度
3/5
預估耗時
1-2 天
活躍度
停滯
描述清晰度
基本清楚
新手友好度
45/100

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。