csv.Sniffer.sniff() takes quadratic time on long runs of spaces
I maintainer di solito rispondono entro 1 giorno
@serhiy-storchaka ci sta già lavorando.
Dal 26/10/2026.
Valutazione
- Difficoltà
- 2/5
- Tempo stimato
- 1-3 ore
- Idoneità per principianti
- 35/100
- Tipo di issue
- Bug
- Chiarezza
- Specificata chiaramente
- Stato di attività
- Attiva
- Stack tecnologico
- python
- Ambito
- performance
Direzione di ricerca
Read Lib/csv.py and inspect the regex search in Sniffer._split_dormant() described in the issue. Reproduce the slowdown with the supplied timing sample, then check that the proposed lookbehind preserves matching results while avoiding repeated attempts within a space run. Done means the long-run timings no longer grow quadratically and the existing csv tests pass.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Descrizione
Bug report
Bug description:
On main, csv.Sniffer().sniff() takes quadratic time when the sample contains a quote character and a long run of spaces that is not followed by one:
import csv, time
for n in (8000, 16000, 32000, 64000):
sample = 'a' + ' ' * n + 'x"\n'
t = time.perf_counter()
try:
csv.Sniffer().sniff(sample)
except csv.Error:
pass
print(n, round(time.perf_counter() - t, 4))
Seconds, on macOS 27.0.1 (Apple M3):
| n | main (debug build) | main + fix (debug build) | 3.15.0 (release, for comparison) |
|---|---|---|---|
| 8000 | 0.050 | 0.011 | 0.010 |
| 16000 | 0.153 | 0.002 | 0.004 |
| 32000 | 0.598 | 0.004 | 0.007 |
| 64000 | 2.373 | 0.008 | 0.015 |
The sample ('a,b' + ' ' * 100_000 + 'c"\n') * 4 takes 23.2 s on main and 0.06 s with the fix.
The cause is the regex in Sniffer._split_dormant() (Lib/csv.py):
m = re.compile(fr'(?:^|([\r\n{cls}]))'
fr' *{quotechar}').search(part, pos)
The space is a delimiter candidate whenever the sample contains one, so it is in cls. The search then tries a match at every space of the run: [...] matches that space, * consumes the rest of the run, and the match fails on the next character. A run of n spaces costs O(n**2).
A match that starts at a space preceded by a space is never the leftmost one: a match with the same group and the same end also starts at the beginning of the run. Adding the lookbehind (?<! ) after the group skips those positions without changing any result.
This was introduced in gh-83273 (25fea6d64e1) and is in 3.16.0a1. 3.15 and older are not affected.
CPython versions tested on:
3.15, CPython main branch
Operating systems tested on:
macOS
Linked PRs
- gh-160557
- Lingua principale
- Python
- Stelle
- 77.7k
- Fork
- 38.3k
- Merge medio
- 13h 43m
- PR unite (30g)
- 704
Preparare l'ambiente
Avvia il container di sviluppo del progetto nel browser, con il tuo account GitHub.
- Nessun Dockerfile né file Docker Compose
- Ha un modello di pull request
- Leggi la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Altre issue di python/cpython
-
topic-socket type-bug
Difficoltà 2/5 1-3 ore Idoneità per principianti 72/100
python/cpython#160561 · 7 commenti ·
I maintainer di solito rispondono entro 1 giorno
-
tests topic-profiling type-bug
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
python/cpython#160334 · 1 commento ·
I maintainer di solito rispondono entro 1 giorno
-
`iso2022_jp_3` encodes U+9B1D but decodes the result as U+9B1CForse già presa @kinqsradio l’ha presa oggi. Apertaextension-modules type-bug
Difficoltà 2/5 1-3 ore Idoneità per principianti 84/100
I maintainer di solito rispondono entro 1 giorno
-
Remove unused FVS_* macrosApertainterpreter-core type-feature
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 78/100
python/cpython#160109 · 1 commento ·
I maintainer di solito rispondono entro 1 giorno
-
extension-modules type-bug
Difficoltà 2/5 1-3 ore Idoneità per principianti 72/100
python/cpython#160001 · 3 commenti ·
I maintainer di solito rispondono entro 1 giorno
Tutte le issue di python/cpython
Issue simili
-
area/config comp/tools P2 platform/telegram type/bug
Difficoltà 2/5 1-3 ore Idoneità per principianti 74/100
NousResearch/hermes-agent#145412 ·
I maintainer di solito rispondono entro 1 giorno
-
[oblt-aw][autodoc] Update docs for security org scaffolding and Docs AI menu permission changesForse già presa @github-actions l’ha presa oggi. Aperta
Difficoltà 2/5 1-3 ore Idoneità per principianti 75/100
I maintainer di solito rispondono entro 1 giorno
-
Community project: hosted MOSS-Image studio with a visual JSON layout builder (no ComfyUI needed)Aperta
Difficoltà 1/5 1-3 ore Idoneità per principianti 65/100
OpenMOSS/MOSS-Image#27 · 1 commento ·
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 76/100
deepspeedai/DeepSpeed#8925 · 1 commento ·
I maintainer di solito rispondono entro 3 giorni
-
external integration integration-submission
Difficoltà 2/5 1-3 ore Idoneità per principianti 68/100
langchain-ai/docs#7077 ·
I maintainer di solito rispondono entro 1 giorno