sre: the UCS2 and UCS4 variants of charset_loc_ignore are unreachable
維護者通常 1 天內回覆
還沒有人認領這個 Issue。
評估
- 難度
- 4/5
- 預估耗時
- 1-2 天
- 新手友好度
- 42/100
- Issue 類型
- 重構
- 描述清晰度
- 基本清楚
- 活躍度
- 活躍
- 領域
- backend, testing-qa
研究方向
Start by reading Modules/_sre/sre_lib.h alongside the three character-width inclusions and the IN_LOC_IGNORE emission in Lib/re/_compiler.py; the issue provides coverage evidence and a reproduction to understand. Confirm whether the UCS2 and UCS4 functions are emitted despite being unreachable, then establish maintainer intent before making a change. Done means the project clarifies whether the duplicates are intentional or identifies a scoped change to generate only the bytes variant.
由索引模型根據 Issue 內容生成。
描述
Summary
Modules/_sre/sre_lib.h is included three times, once per character width, so
charset_loc_ignore is compiled as three functions. Only the bytes one can ever
run: the IN_LOC_IGNORE opcode that calls it is emitted only for LOCALE
patterns, and LOCALE is rejected for str patterns.
Evidence
Coverage from a --coverage build of main running the full test suite (552 test
files, 52,070 tests, ./python -m test -j4):
Function 'sre_ucs4_charset_loc_ignore' Lines executed:0.00% of 39
Function 'sre_ucs2_charset_loc_ignore' Lines executed:0.00% of 39
Function 'sre_ucs1_charset_loc_ignore' Lines executed:87.18% of 39
78 lines that no input reaches.
Why they cannot be reached
IN_LOC_IGNORE is emitted in one place, Lib/re/_compiler.py:89:
if flags & SRE_FLAG_IGNORECASE and flags & SRE_FLAG_LOCALE:
emit(IN_LOC_IGNORE)
and the parser refuses LOCALE for str patterns, Lib/re/_parser.py:1106:
raise ValueError("cannot use LOCALE flag with a str pattern")
The opcode is fixed when the pattern is compiled, so adding the flag later does
not help. Constructing a pattern directly through _sre.compile() with the
LOCALE flag set keeps whatever opcode the code array already carries, which for
a str pattern is IN_UNI_IGNORE, not IN_LOC_IGNORE:
import re, _sre
from re import _parser, _compiler
p = _parser.parse('[a-z]', re.IGNORECASE)
code = _compiler._code(p, re.IGNORECASE)
groupindex = p.state.groupdict
indexgroup = [None] * p.state.groups
for k, i in groupindex.items():
indexgroup[i] = k
pat = _sre.compile('[a-z]', re.IGNORECASE | re.LOCALE, code,
p.state.groups - 1, groupindex, tuple(indexgroup))
pat.search('\U0001d11eA') # matches, and still does not call the UCS4 variant
Matching a str against a bytes pattern is rejected, so there is no route from
any public or private API to the UCS2/UCS4 copies.
Question
Is the triplication intentional (the template is included and every function is
emitted regardless of reachability, and the duplicates are simply dropped by the
linker or the optimiser), or would it be worth guarding the emission so only the
bytes variant exists?
I have not prepared a patch: this is a question about intent first. If the answer
is that they should not exist, the fix is to wrap the function definition in
sre_lib.h so it is only generated for the bytes build, and the coverage build
then confirms it.
CPython versions tested on
CPython main branch (3.16.0a1+)
Operating systems tested on
Linux
- 主要語言
- Python
- 星號
- 77.7k
- 分支
- 38.3k
- 平均合併
- 13 小時 43 分鐘
- 30 天內合併 PR
- 704
環境準備
在瀏覽器裡用你自己的 GitHub 帳號啟動這個專案的開發容器。
- 沒有 Dockerfile 或 Docker Compose 檔案
- 有 Pull Request 範本
- 閱讀貢獻指南
從這裡開始
- 先讀完整個 Issue,再讀專案的貢獻指南。
- 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
- Fork 儲存庫,在一個分支上完成修改。
- 送出 Pull Request,並在描述裡引用這個 Issue 編號。
python/cpython 的其他 Issue
-
topic-socket type-bug
難度 2/5 1-3 小時 新手友好度 72/100
python/cpython#160561 · 7 則留言 ·
維護者通常 1 天內回覆
-
tests topic-profiling type-bug
難度 2/5 1-3 小時 新手友好度 78/100
python/cpython#160334 · 1 則留言 ·
維護者通常 1 天內回覆
-
`iso2022_jp_3` encodes U+9B1D but decodes the result as U+9B1C可能已有人在做 @kinqsradio 於 1 天前認領。 未關閉extension-modules type-bug
難度 2/5 1-3 小時 新手友好度 84/100
維護者通常 1 天內回覆
-
interpreter-core type-feature
難度 1/5 1 小時以內 新手友好度 78/100
python/cpython#160109 · 1 則留言 ·
維護者通常 1 天內回覆
-
extension-modules type-bug
難度 2/5 1-3 小時 新手友好度 72/100
python/cpython#160001 · 3 則留言 ·
維護者通常 1 天內回覆
相似的 Issue
-
bug P1
難度 2/5 1-3 小時 新手友好度 76/100
Toloka/tolokaforge#1947 ·
維護者通常 1 天內回覆
-
SageMaker provider: streaming fails with NoStreamDataError on DJL Serving (LMI) endpoints可能已有人在做 @balajiaru 今天認領。 未關閉
難度 2/5 1-3 小時 新手友好度 88/100
UKGovernmentBEIS/inspect_ai#6127 ·
維護者通常 2 天內回覆
-
難度 2/5 1-3 小時 新手友好度 67/100
維護者通常 2 天內回覆
-
難度 2/5 1-3 小時 新手友好度 68/100
letsencrypt/cp-cps#361 ·
-
難度 2/5 1-3 小時 新手友好度 65/100