Report deeply nested regex as invalid instead of raising RecursionError - #1539
Closed
deepakganesh78 wants to merge 1 commit into
Closed
Conversation
re.compile raises RecursionError (not an re.error subclass) when a
pattern contains deeply nested groups, e.g. '(' * 500. The regex
format checker was registered with raises=re.error only, so the
RecursionError propagated uncaught from iter_errors instead of being
reported as an invalid format.
Add RecursionError alongside re.error in the raises declaration so
FormatChecker.check converts it to a FormatError.
Fixes python-jsonschema#1538
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Documentation build overview
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #1538.
Summary
The
regexformat checker crashes withRecursionErrorinstead of reporting the instance as invalid when the pattern contains deeply nested groups.Reproduction
`python
from jsonschema import Draft202012Validator, FormatChecker
v = Draft202012Validator({"format": "regex"}, format_checker=FormatChecker())
list(v.iter_errors("(" * 500))
RecursionError: maximum recursion depth exceeded
`
Root cause
re.compileraisesRecursionErrorwhen the regex pattern nesting exceeds the C stack limit.RecursionErroris not a subclass ofre.error, soFormatChecker.checkdoes not catch it and the exception propagates uncaught.This is the same class of issue as the
OverflowErrorcase in #1526, but a distinct exception type (RecursionErrorinherits fromRuntimeError, notArithmeticError).Fix
Add
RecursionErroralongsidere.errorin theraisesdeclaration foris_regex, soFormatChecker.checkconverts it to aFormatErrorand the instance is reported as invalid.Compatibility
No backward-incompatible changes. Previously crashing inputs now produce a validation error.
Validation
test_regex_format_rejects_deeply_nested_patternfails before the fix, passes after.ruff checkclean on touched files.