Skip to content

_sre: fix Match.expand to use _compile_template (#8229)#8256

Open
zzarbttoo wants to merge 1 commit into
RustPython:mainfrom
zzarbttoo:fix-8229-match-expand
Open

_sre: fix Match.expand to use _compile_template (#8229)#8256
zzarbttoo wants to merge 1 commit into
RustPython:mainfrom
zzarbttoo:fix-8229-match-expand

Conversation

@zzarbttoo

@zzarbttoo zzarbttoo commented Jul 11, 2026

Copy link
Copy Markdown

Summary

Match.expand delegated to re._expand, which CPython removed in 3.12 during the template-expansion refactor. With the 3.14 stdlib that helper no longer exists, so expand raised AttributeError.

Route expand through the compiled-template path
(Template::compile -> re._compile_template) and share the template-filling logic with Pattern.sub via a Match::expand_template helper. Each caller still compiles the template once, so sub keeps its no-recompile-per-match fast path.

Remove the expectedFailure marker on test_re.test_expand.

Assisted-by: Claude Code:claude-opus-4-8

Summary by CodeRabbit

  • Bug Fixes
    • Improved template-based replacements for regular expression matches.
    • Updated match expansion to handle replacement templates more consistently.
    • Preserved the correct output type—text or bytes—when expanding templates.
    • Enhanced support for passing template objects to match expansion methods.

Match.expand delegated to re._expand, which CPython removed in 3.12
during the template-expansion refactor. With the 3.14 stdlib that
helper no longer exists, so expand raised AttributeError.

Route expand through the compiled-template path
(Template::compile -> re._compile_template) and share the
template-filling logic with Pattern.sub via a Match::expand_template
helper. Each caller still compiles the template once, so sub keeps
its no-recompile-per-match fast path.

Remove the expectedFailure marker on test_re.test_expand.

Assisted-by: Claude Code:claude-opus-4-8
@coderabbitai

coderabbitai Bot commented Jul 11, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

Template expansion is centralized in _sre.Match, with Match.expand compiling and applying templates directly. Pattern.sub now reuses the shared expansion helper, including captured groups and literal segments.

Changes

Template expansion

Layer / File(s) Summary
Match template compilation and expansion
crates/vm/src/stdlib/_sre.rs
Match.expand accepts a Python object template, compiles it, expands captured slices and literal segments, and joins the result using the pattern’s string or bytes type.
Pattern.sub template reuse
crates/vm/src/stdlib/_sre.rs
The template replacement path delegates segment expansion to Match.expand_template instead of assembling literals and captures inline.

Estimated code review effort: 2 (Simple) | ~10 minutes

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 inconclusive)

Check name Status Explanation Resolution
Linked Issues check ❓ Inconclusive The code addresses Match.expand and compiled-template expansion, but the expected-failure test change can't be verified because Lib/test/test_re.py was excluded by !Lib/**. Include Lib/test/test_re.py in review or confirm the expected-failure marker was removed there.
✅ Passed checks (4 passed)
Check name Status Explanation
Out of Scope Changes check ✅ Passed The diff stays focused on Match.expand and template expansion with no unrelated code changes.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly matches the main change: fixing Match.expand to use the compiled-template path.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@github-actions

Copy link
Copy Markdown
Contributor

📦 Library Dependencies

The following Lib/ modules were modified. Here are their dependencies:

[x] lib: cpython/Lib/re
[x] lib: cpython/Lib/sre_compile.py
[x] lib: cpython/Lib/sre_constants.py
[x] lib: cpython/Lib/sre_parse.py
[ ] test: cpython/Lib/test/test_re.py (TODO: 12)
[x] test: cpython/Lib/test/re_tests.py

dependencies:

  • re

dependent tests: (81 tests)

  • re: test_android test_ast test_asyncio test_binascii test_builtin test_bytes test_clinic test_cmd_line test_ctypes test_dict test_dis test_docxmlrpc test_dtrace test_email test_embed test_faulthandler test_filecmp test_fileinput test_fnmatch test_format test_frame test_fstring test_functools test_future_stmt test_generated_cases test_genericalias test_glob test_hashlib test_http_cookiejar test_httplib test_httpservers test_imaplib test_importlib test_inspect test_ipaddress test_launcher test_logging test_mailbox test_mmap test_optparse test_ordered_dict test_platform test_pprint test_pydoc test_pyexpat test_pyrepl test_re test_regrtest test_runpy test_set test_site test_smtplib test_socket test_ssl test_strftime test_strptime test_strtod test_structseq test_symtable test_syntax test_sys test_sysconfig test_tarfile test_tempfile test_tokenize test_tools test_traceback test_turtle test_typing test_unittest test_unparse test_venv test_webbrowser test_winapi test_with test_wsgiref test_xmlrpc test_zipfile test_zipimport test_zoneinfo test_zstd

Legend:

  • [+] path exists in CPython
  • [x] up-to-date, [ ] outdated

@ShaharNaveh ShaharNaveh left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm, tysm:)

welcome to the project<3

@youknowone youknowone added the z-ca-2026 Tag to track Contribution Academy 2026 label Jul 12, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

z-ca-2026 Tag to track Contribution Academy 2026

Projects

None yet

Development

Successfully merging this pull request may close these issues.

re.Match.expand() broken: AttributeError: module 're' has no attribute '_expand' (removed in CPython 3.12 Lib)

3 participants