python / python/cpython

`state_reset()` does not clear `save_marks` after an aborted match

Aperta
#157,705 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

extension-modules type-bug
Lingua principale
Python
Stelle
77.2k
Fork
35.9k
Metriche di merge delle PR
Metriche PR in attesa

Descrizione

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

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Inizia in Modules/_sre/sre.c, in state_reset e nei percorsi di scanner-loop usati da finditer() e pattern.scanner(). Esegui prima il confronto con repro.py, quindi verifica che un match interrotto non rallenti l’iterazione successiva e che gli span ripresi rimangano invariati.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
python
Ambito
backend
Tipo di issue
Bug
Difficoltà
3/5
Tempo stimato
1-2 giorni
Stato di attività
Ferma
Chiarezza
Specificata chiaramente
Idoneità per principianti
30/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.