`state_reset()` does not clear `save_marks` after an aborted match
Chưa có ai nhận issue này.
- Ngôn ngữ chính
- Python
- Star
- 77.2k
- Fork
- 35.9k
- Chỉ số merge pull request
- Chỉ số pull request đang chờ
Mô tả
Bug description:
state_reset() in Modules/_sre/sre.c clears the per-match state at the start of each match in a scanner loop, including state->repeat, but not state->save_marks, which is zeroed only in state_init(). A match aborted by a signal returns straight out of SRE(match), so that counter stays above zero for the rest of the iterator and every later match saves and restores marks on backtracking that it would otherwise skip. Only finditer() and pattern.scanner() can observe it, because the findall/sub/split loops abandon the state on error. Results are unchanged, only the cost.
$ cat repro.py
import re, signal, time
class Alarm(Exception): pass
def boom(*args): raise Alarm
signal.signal(signal.SIGALRM, boom)
PAT = r"(z)*" + "(a)" * 200 + r"(?:xy|x)" * 300
SUBJECT = "z" * 400000 + ("a" * 200 + "x" * 300) * 400
def run(interrupt):
it = re.compile(PAT).finditer(SUBJECT)
if interrupt:
signal.setitimer(signal.ITIMER_REAL, 0.02)
try:
next(it)
except Alarm:
pass
signal.setitimer(signal.ITIMER_REAL, 0)
next(it)
t = time.perf_counter()
spans = [m.span() for m in it]
return time.perf_counter() - t, spans
clean, clean_spans = run(False)
dirty, dirty_spans = run(True)
print("clean iterator : %.4fs, %d matches" % (clean, len(clean_spans)))
print("resumed after one abort : %.4fs, %d matches" % (dirty, len(dirty_spans)))
print("same results : %s" % (clean_spans == dirty_spans,))
$ ./python repro.py
clean iterator : 0.0087s, 399 matches
resumed after one abort : 0.0238s, 399 matches
same results : True
Expected: an iterator resumed after an aborted match costs the same as a clean one.
CPython versions tested on:
CPython main branch
Operating systems tested on:
Linux
Linked PRs
- gh-157707
Hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- 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.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Hướng nghiên cứu
Bắt đầu tại Modules/_sre/sre.c, ở state_reset và các đường dẫn scanner-loop được finditer() và pattern.scanner() sử dụng. Trước tiên, hãy chạy phép so sánh repro.py, sau đó xác minh rằng một match bị hủy không làm chậm việc lặp về sau và các span được tiếp tục vẫn không thay đổi.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Đánh giá
- Công nghệ
- python
- Lĩnh vực
- backend
- Loại issue
- Lỗi
- Độ khó
- 3/5
- Thời gian dự kiến
- 1-2 ngày
- Mức độ hoạt động
- Đình trệ
- Độ rõ ràng
- Đặc tả rõ ràng
- Mức phù hợp với người mới
- 30/100