regex format checker crashes with ValueError on conflicting inline flags
Una pull request collegata è già stata integrata.
Valutazione
- Difficoltà
- 3/5
- Tempo stimato
- 1-2 giorni
- Idoneità per principianti
- 68/100
- Tipo di issue
- Bug
- Chiarezza
- Abbastanza chiara
- Stato di attività
- Attiva
- Stack tecnologico
- python
- Ambito
- testing-qa
Direzione di ricerca
Inizia dal verificatore del formato regex e da FormatChecker.check, quindi esamina la gestione esistente di re.error e le issue correlate #1526 e #1538. Aggiungi una copertura di regressione per i flag inline in conflitto e per gli altri casi di escaping segnalati, quindi verifica che i pattern non validi restituiscano False o un FormatError senza generare un’eccezione non intercettata in tutte le versioni di Python supportate.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Descrizione
The regex format checker is registered with raises=re.error:
@_checks_drafts(name="regex", raises=re.error)
def is_regex(instance: object) -> bool:
if not isinstance(instance, str):
return True
return bool(re.compile(instance))
FormatChecker.check only converts the declared exception into a FormatError. But re.compile raises ValueError (not an re.error subclass) when a pattern sets two incompatible inline flags, so the exception escapes uncaught and the format check crashes instead of reporting the string as an invalid regex.
Reproduction
from jsonschema import FormatChecker
fc = FormatChecker()
fc.conforms("[unterminated", "regex") # False - re.error, caught correctly
fc.conforms("(?u)(?a)", "regex") # ValueError: ASCII and UNICODE flags are incompatible
Also reproduces with the flags in the other order ("(?a)(?u)"). Note that "(?ua)" - both flags in a single group - returns False correctly, so this is specific to the two-group form.
Expected
"(?u)(?a)" is not a valid regular expression, so the instance should be reported as invalid, exactly like "[unterminated" is.
Cause
ValueError is not a subclass of re.error:
>>> import re
>>> issubclass(ValueError, re.error)
False
>>> re.error.__mro__
(<class 're.PatternError'>, <class 'Exception'>, <class 'BaseException'>, <class 'object'>)
so raises=re.error does not cover it.
Related
Same root cause as #1526 (OverflowError on an oversized repeat count) and #1538 (RecursionError on deep nesting) - three exception types, one declaration that only catches re.error.
There is a fourth case worth mentioning because it shows the set is not stable across CPython versions: "(?(1)a|b)" (a conditional) raised an uncaught RuntimeError on Python 3.9.6, but on 3.13 re.compile raises re.error for it and it is now handled correctly. So which exception types escape depends on the CPython release, not just on jsonschema.
Given three open issues with one cause, it may be worth widening what is_regex catches rather than adding types one at a time - re.compile does not document re.error as the only exception it can raise.
Versions
Reproduced on:
- jsonschema 4.26.0, Python 3.13.11
- jsonschema 4.25.1, Python 3.9.6
- Lingua principale
- Python
- Stelle
- 5k
- Fork
- 694
- Merge medio
- 1g 20h
- PR unite (30g)
- 6
Preparare l'ambiente
- Nessun Dockerfile né file Docker Compose
- Nessun 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-jsonschema/jsonschema
-
Iterator error contexts lose parent links and absolute pathsForse già presa @DawnofGenX l’ha presa 5 giorni fa. Aperta
Difficoltà 2/5 1-3 ore Idoneità per principianti 76/100
python-jsonschema/jsonschema#1584 · 3 commenti ·
-
regex format checker crashes with RecursionError on deeply nested patternsForse già presa @DawnofGenX l’ha presa 5 giorni fa. Aperta
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
python-jsonschema/jsonschema#1538 · 1 commento ·
-
is_duration raises an uncaught decimal.Overflow on a duration with a large exponentForse di nuovo libera Una pull request per questa issue è stata chiusa senza essere unita. ApertaNeeds Test Upstream
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
python-jsonschema/jsonschema#1511 · 1 commento ·
-
Difficoltà 4/5 3-5 giorni Idoneità per principianti 45/100
python-jsonschema/jsonschema#1547 ·
-
Difficoltà 4/5 3-5 giorni Idoneità per principianti 45/100
python-jsonschema/jsonschema#1536 · 1 commento ·
Tutte le issue di python-jsonschema/jsonschema
Issue simili
-
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 60/100
521xueweihan/HelloGitHub#3924 ·
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 67/100
wilbowes/EchoMuse#869 · 1 commento ·
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 85/100
-
Claiming namespace `jft63`Apertanamespace operations
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 72/100
EclipseFdn/open-vsx.org#14043 ·
I maintainer di solito rispondono entro 1 giorno
-
test: TestServeUntilStale races the server's close against the client's sendall (BrokenPipeError under load)Forse già presa @evoludigit l’ha presa oggi. Aperta
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 89/100
I maintainer di solito rispondono entro 1 giorno