Skip to content

Check percent-c character lengths before index conversion - #9052

Merged
youknowone merged 1 commit into
RustPython:mainfrom
youknowdot:fix-percent-character-subclass-dispatch
Oct 10, 2026
Merged

youknowone merged 1 commit into
RustPython:mainfrom
youknowdot:fix-percent-character-subclass-dispatch

Conversation

@youknowdot

@youknowdot youknowdot commented Oct 10, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Extract the small %c character-subclass dispatch fix from #8954 (43065e79ddc46eafeca58cb39398ac007b5d25f7) onto Python 3.14 main (3af5f8a15314446d2b28a90e429c938dc7b8beeb).

Empty or multi-character str subclasses, and empty or multi-byte bytes/bytearray subclasses, must raise the existing length-specific TypeError before considering __index__. Currently those objects fall through to numeric conversion. One-character payloads and non-character numeric fallback remain unchanged.

The existing 3.14 error constructors are reused without adopting the mega's 3.15 messages or broad streaming-parser rewrite. The bytearray read guard is released before constructing its error. References: CPython 3.14.7 formatchar and byte_converter.

Related merged #8876, #7769 and #8600 are already in the base. Their numeric conversion and existing diagnostics are preserved. Open #8950 retains this fallthrough behavior, so this is not a duplicate of its argument-context changes.

Validation

  • Exact CPython 3.14.7: full updated builtin_str snippet and bounded callback/length probes pass.
  • Unchanged native main fails the new regression by invoking the forbidden __index__; this head passes the full snippet and its pytest entry. Native full-snippet runs had a 1.5-GiB address-space limit after reviewing its existing oversized-allocation error paths.
  • All 4 selected canonical format tests pass: text, bytes/bytearray, NUL and non-ASCII formatting. Allocation-sized precision tests and unrelated bytes suites were not run locally.
  • Native bounded probes pass for all five argument/template combinations, lengths 0/1/2, and numeric fallback.
  • VM Clippy and separate C-API Clippy with warnings denied; normal repository commit hooks.
  • Independent source audit and 96 bounded CPython-only checks cover stored code points, binary bytes, small widths and suppressed subclass callbacks.

This is only character-family length precedence; pre-existing numeric callback-error normalization and qualified-name diagnostic differences are outside its scope. No canonical assertions, README text, implementation-name guards or Rust behavioral tests were added or changed. Local checks are bounded Linux checks, not a complete workspace/cross-platform claim. Full hosted CI is pending; this remains a draft for review.

AI-assisted implementation and validation with OpenAI Codex; model identifier unavailable. No human review is implied.

Summary by CodeRabbit

  • Bug Fixes
    • %c formatting now reports clear type errors for empty or multi-character strings, bytes, and bytearrays. Valid single-character values and integer-like inputs continue to work.
  • Tests
    • Added coverage for %c formatting with subclasses of strings, bytes, and bytearrays.

Assisted-by: OpenAI Codex:model identifier unavailable
@coderabbitai

coderabbitai Bot commented Oct 10, 2026 •

Copy link
Copy Markdown
Contributor

Review in Change Stack →

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration
  • Configuration used: Repository: RustPython/RustPython/.coderabbit.yml
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 5b35c11e-5de8-4a9a-b0d7-072c51e40cf7

📥 Commits

Reviewing files that changed from the base of the PR and between 3af5f8a and b8134a7.


📒 Files selected for processing (2)
  • crates/vm/src/cformat.rs
  • extra_tests/snippets/builtin_str.py

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 1 remain after this review.



📝 Walkthrough

Walkthrough

The %c formatter now rejects bytes, bytearrays, and strings whose lengths are not one. Tests cover subclass behavior and confirm that integer and __index__ formatting remains supported.

Changes

%c formatting

Layer / File(s) Summary
%c input validation
crates/vm/src/cformat.rs, extra_tests/snippets/builtin_str.py
Bytes-like and string values with invalid lengths now raise TypeError messages that include the type and length. Tests check subclass behavior and integer index formatting.

Priority: ⬇️ Low

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

Change: Bug fix

Suggested reviewers: youknowone


Merge Risk: ⚪ Minimal · up to b8134

The change makes %c formatting reject empty or multi-character string, bytes and bytearray subclasses with the existing TypeError instead of calling __index__. No concrete merge-blocking risk was found. Hosted CI results are still pending.

Pre-merge checks | Passed 4 | Failed 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 4 functions across 2 files. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title clearly and concisely describes the main change: validating %c character lengths before attempting index conversion.
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.

  • Fix all pre-merge checks with AI
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create a new PR

  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

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.

@youknowone
youknowone marked this pull request as ready for review October 10, 2026 11:07
@youknowone
youknowone enabled auto-merge (squash) October 10, 2026 11:07
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 10, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-10T11:11:41.714683Z b8134a7 Draft marked ready
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: b8134a701b

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

assert b"%o" % PercentInt(3) == b"7"


def test_percent_character_subclasses():

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge Remove the added test logic

This adds a new test function and assertions to an existing test file, but the repository policy limits test-file changes to adding expected-failure decorators or removing them together with their TODO comments; keep the %c fix in Rust without adding this test logic.

AGENTS.md reference: AGENTS.md:L273-L279

Useful? React with 👍 / 👎.

@youknowone
youknowone merged commit c0f0680 into RustPython:main Oct 10, 2026
21 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants