Hacktoberfest 2026: le issue che i maintainer hanno segnato per ottobre, aperte e adatte ai principianti. Sfoglia le issue Hacktoberfest

regex format checker crashes with ValueError on conflicting inline flags

Aperta
#1,558 2 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Una pull request collegata è già stata integrata.

  • #1 di @Anusha0501 — integrata
  • #1559 di @agu2347 — chiusa senza merge
  • #1560 di @official-burak — chiusa senza merge
  • #1563 di @yzxcj797 — chiusa senza merge

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

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.

Altre issue di python-jsonschema/jsonschema

Tutte le issue di python-jsonschema/jsonschema

Issue simili

Altre issue su Python

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.