Hacktoberfest 2026: những issue maintainer đã đánh dấu cho tháng Mười, đang mở và phù hợp người mới. Xem issue Hacktoberfest

sre: the UCS2 and UCS4 variants of charset_loc_ignore are unreachable

Đang mở
#160,498 0 bình luận 0 reaction 0 người được giao Xem trên GitHub

Maintainer thường phản hồi trong vòng 1 ngày

Chưa có ai nhận issue này.

Đánh giá

Độ khó
4/5
Thời gian dự kiến
1-2 ngày
Mức phù hợp với người mới
42/100
Loại issue
Tái cấu trúc
Độ rõ ràng
Khá rõ ràng
Mức độ hoạt động
Sôi nổi
Công nghệ
c, python
Lĩnh vực
backend, testing-qa

Hướng nghiên cứu

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.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

extension-modules topic-regex type-refactor

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

Ngôn ngữ chính
Python
Star
77.7k
Fork
38.3k
Merge trung bình
12 giờ 27 phút
Pull request đã merge (30 ngày)
710

Chuẩn bị môi trường

Mở trong Codespaces

Khởi chạy dev container của dự án ngay trên trình duyệt, bằng tài khoản GitHub của bạn.

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của python/cpython

Tất cả issue của python/cpython

Issue tương tự

Thêm issue về Python

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.