Fix re.findall reporting a group that did not match (#8563) · RustPython/RustPython@6079ad0 · GitHub
Skip to content

Commit 6079ad0

Browse files
authored
Fix re.findall reporting a group that did not match (#8563)
findall returns the matched text rather than a match object, so a group that took no part in the match is reported as an empty value. With one group it was reported as `None`: >>> re.findall(r"(a)?b", "b ab") [None, 'a'] # CPython: ['', 'a'] The branch for two or more groups was already right, since it passes `""` to `Match.groups` as the default, so the two halves of the same function disagreed with each other. That default is built as a `str` whatever the pattern is, so a `bytes` pattern came back with `str` mixed into it: >>> re.findall(rb"(a)|(b)", b"ab") [(b'a', ''), ('', b'b')] # CPython: [(b'a', b''), (b'', b'b')] The empty value is now built once from `isbytes` and both branches use it. `Match.groups` still reports `None`, which is what CPython does. Assisted-by: Claude Code:claude-opus-5
1 parent c077373 commit 6079ad0

2 files changed

Lines changed: 57 additions & 3 deletions

File tree

crates/vm/src/stdlib/_sre.rs

Lines changed: 13 additions & 3 deletions

extra_tests/snippets/stdlib_re.py

Lines changed: 44 additions & 0 deletions

0 commit comments

Comments
 (0)