feat(re): Enhance CFFI re module and add tests#7
Closed
Conversation
This commit includes several enhancements to the CFFI-based `re` module:
- Introduced `re.error` for regex-specific exceptions.
- Implemented `MatchObject` for `search()` results, providing access to captured groups via `group()` and `groups()` methods.
- Enhanced `findall()` to correctly return:
- `list[str]` of full matches if no capture groups.
- `list[str]` of captured strings if one capture group.
- `list[tuple[str,...]]` if multiple capture groups.
- Ensured `sub()` supports standard ECMAScript group references (e.g., `$1`, `$&`).
- Added comprehensive tests to `tests/test_re.py` to cover:
- Basic matching, searching, character classes, quantifiers, anchors.
- Detailed behavior of `search()` with `MatchObject` and groups.
- Detailed behavior of `findall()` with 0, 1, and multiple groups.
- `sub()` with various group backreferences.
- Error handling for invalid regex patterns.
The C++ backend in `native/src/regex_wrapper.cpp` was updated to support these features, including functions to get mark counts and return structured group information for `search`. The CFFI interface in `src/stdlib/re.py` and the stub file `native/src/regex_wrapper.pyi` were updated accordingly.
This commit adds an `__all__` list to `src/stdlib/re.py` to explicitly define the public API of the module. This ensures that `re.error` and other key components like `match`, `compile`, `search`, `findall`, `sub`, `CompiledRegex`, and `MatchObject` are correctly exported and accessible via `from stdlib.re import ...`. This addresses the issue where `re.error` was not directly importable.
Adds an `__all__` list to `src/stdlib/re.py` to explicitly define the public API of the module. This ensures that `re.error` and other key components like `match`, `compile`, `search`, `findall`, `sub`, `CompiledRegex`, and `MatchObject` are correctly exported. This addresses the issue where `from stdlib.re import error` would fail.
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.
This commit includes several enhancements to the CFFI-based
remodule:re.errorfor regex-specific exceptions.MatchObjectforsearch()results, providing access to captured groups viagroup()andgroups()methods.findall()to correctly return:list[str]of full matches if no capture groups.list[str]of captured strings if one capture group.list[tuple[str,...]]if multiple capture groups.sub()supports standard ECMAScript group references (e.g.,$1,$&).tests/test_re.pyto cover:search()withMatchObjectand groups.findall()with 0, 1, and multiple groups.sub()with various group backreferences.The C++ backend in
native/src/regex_wrapper.cppwas updated to support these features, including functions to get mark counts and return structured group information forsearch. The CFFI interface insrc/stdlib/re.pyand the stub filenative/src/regex_wrapper.pyiwere updated accordingly.