Fix Regex(flags=re.I) crashing on CPython <= 3.10 and PyPy (#246) - #357
Open
vineethsaivs wants to merge 1 commit into
Open
Fix Regex(flags=re.I) crashing on CPython <= 3.10 and PyPy (#246)#357vineethsaivs wants to merge 1 commit into
vineethsaivs wants to merge 1 commit into
Conversation
re.IGNORECASE and friends are re.RegexFlag (IntFlag) members, not plain
ints. Building the human-readable flag list formatted flags with the "b"
format code (f"{flags:09b}"); on CPython <= 3.10 and PyPy, IntFlag routes
that through Enum.__format__, which formats the member as its string form
and rejects the "b" code, raising ValueError before re.compile ran. So
Regex(pattern, flags=<any re flag>), the documented usage, was unusable
on those versions; only a plain int worked.
Coerce to int before the binary formatting. re.compile already accepts the
enum, so only the repr computation needed the coercion; output is identical
for the existing int-flag usage.
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.
What
Constructing a
Regexwith an actualreflag crashes on CPython <= 3.10 and PyPy:re.IGNORECASEand the otherre.*flags arere.RegexFlag(anIntFlag) members, not plain ints.Regex.__init__builds the human-readable flag list withOn CPython <= 3.10 and PyPy, applying the
bformat code to anIntFlagroutes throughEnum.__format__, which formats the member via its string form ("re.IGNORECASE") and then rejects thebcode, raisingValueError. This happens beforere.compile(...), soRegex(pattern, flags=<any re flag>), the natural documented usage, is completely unusable on those versions; passing a plain int works, which is why it slipped past the tests.Fix
Coerce to
intbefore the binary formatting (f"{int(flags):09b}").re.compilealready accepts the enum unchanged, so only the repr computation needed the coercion. The rendered output is byte-identical for the existing int-flag usage.Test
Added
test_regex_flags: it constructsRegexwithre.IGNORECASEand withre.IGNORECASE | re.MULTILINE, and asserts both validate correctly and the flag name appears in the repr. It fails before the change (ValueError) on affected versions and passes after; harmless on newer versions where the crash never fired.Fixes #246.