Skip to content

Keep a bytearray exported while it formats with % - #9147

Merged
youknowone merged 1 commit into
RustPython:mainfrom
luantaraschi:fix/bytearray-mod-export
Oct 15, 2026
Merged

youknowone merged 1 commit into
RustPython:mainfrom
luantaraschi:fix/bytearray-mod-export

Conversation

@luantaraschi

@luantaraschi luantaraschi commented Oct 15, 2026 •

Copy link
Copy Markdown
Contributor

Summary

test_mod_concurrent_mutation in test_bytes was still skipped as a segfault. #8524 fixed the crash by formatting a copy of the bytearray, but nothing stopped a conversion callback from resizing the format meanwhile, so the test's BufferError never came:

fmt = bytearray(b"%a end")
class S:
    def __repr__(self):
        fmt.clear()
        return "E"

fmt % S()

Here this returned bytearray(b'E end') and left fmt empty. CPython 3.15 raises BufferError: Existing exports of data: object cannot be re-sized and fmt keeps b"%a end".

__mod__ now holds an export for the whole call, which is what join already does for its separator. The format is still copied, so one thing still differs: an in-place change that keeps the length (e.g. fmt[4] = ord("r") from inside __bytes__) is not seen, where CPython formats the changed buffer. No CPython test covers that, and fixing it means not copying, so I left it.

With the skip removed, the test fails before the change (BufferError not raised by __mod__) and passes after. Nine variants of this case match CPython 3.15.0. test_bytes passes 3 runs out of 3, and test_memoryview, test_buffer and test_format pass too. Workspace cargo test with the AGENTS.md excludes, clippy -Dwarnings, cargo fmt --check and the prek hooks (including check_redundant_patches) are clean. Linux release build only.

Written with Claude Code (claude-opus-5-5) as a tool, and I went over the change and the results before opening this.

Summary by CodeRabbit

  • Bug Fixes
    • Bytearray formatting now prevents the bytearray from being resized while value-conversion callbacks are running.

bytearray.__mod__ copied the format and ran the arguments' __repr__,
__str__, __bytes__ and __index__ without holding an export, so a callback
could clear or extend the format and formatting went on with the old
copy:

    fmt = bytearray(b"%a end")
    class S:
        def __repr__(self):
            fmt.clear()
            return "E"
    fmt % S()   # bytearray(b'E end'), fmt left empty

CPython holds a buffer on the format for the whole call, so the resize
fails with BufferError and the format is left as it was. Taking an export
for the duration of __mod__ does the same here, the way bytearray.join
already does for its separator.

This lets test_mod_concurrent_mutation in test_bytes run again.

Assisted-by: Claude Code:claude-opus-5-5
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 15, 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 ⚠️ Failed 2026-10-15T10:21:27.792231Z 2feef00 PR opened
ℹ️ 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.

@coderabbitai

coderabbitai Bot commented Oct 15, 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: e1cb7734-cd23-4ff7-9328-a0cec533212e
📥 Commits

Reviewing files that changed from the base of the PR and between 16e333b and 2feef00.

⛔ Files ignored due to path filters: 1
  • Lib/test/test_bytes.py is excluded by !Lib/**
📒 Files selected for processing: 1
  • crates/vm/src/builtins/bytearray.rs

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

📝 Walkthrough

PyByteArray.__mod__ now holds a buffer export while it clones the format and runs formatting. This keeps the bytearray exported while value-conversion callbacks run.

Changes: Bytearray formatting

Layer / File(s) Summary
Hold export during formatting
crates/vm/src/builtins/bytearray.rs
__mod__ begins a buffer export before cloning the format and invoking formatting. This keeps the export active during conversion callbacks.

Priority: ⬇️ Low

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

Change: Bug fix

Suggested reviewers: youknowone

Merge Risk: ⚪ Minimal · up to 2feef

Formatting now rejects callback-triggered resizing with BufferError; no merge-blocking issue is established.

Pre-merge checks: Passed 4 · Failed 1

Failed 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 1 functions across 1 files. Write docstrings for the functions missing them to satisfy the coverage threshold.
Passed 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: keeping the bytearray exported during % formatting.
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.

@github-actions

Copy link
Copy Markdown
Contributor

📦 Library Dependencies

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

:4: SyntaxWarning: invalid decimal literal
:7: SyntaxWarning: "\z" is an invalid escape sequence. Such sequences will not work in the future. Did you mean "\z"? A raw string is also an option.
:10: SyntaxWarning: "\400" is an invalid octal escape sequence. Such sequences will not work in the future. Did you mean "\400"? A raw string is also an option.
:4: SyntaxWarning: invalid decimal literal
:7: SyntaxWarning: "\z" is an invalid escape sequence. Such sequences will not work in the future. Did you mean "\z"? A raw string is also an option.
:10: SyntaxWarning: "\400" is an invalid octal escape sequence. Such sequences will not work in the future. Did you mean "\400"? A raw string is also an option.
:4: SyntaxWarning: invalid decimal literal
:7: SyntaxWarning: "\z" is an invalid escape sequence. Such sequences will not work in the future. Did you mean "\z"? A raw string is also an option.
:10: SyntaxWarning: "\400" is an invalid octal escape sequence. Such sequences will not work in the future. Did you mean "\400"? A raw string is also an option.
[ ] lib: cpython/Lib/concurrent
[ ] test: cpython/Lib/test/test_concurrent_futures (TODO: 1)
[ ] test: cpython/Lib/test/test_interpreters
[ ] test: cpython/Lib/test/test__interpreters.py
[ ] test: cpython/Lib/test/test__interpchannels.py
[ ] test: cpython/Lib/test/test_crossinterp.py

dependencies:

  • concurrent (native: _crossinterp, _interpqueues, _interpreters, _queues, concurrent.futures, concurrent.futures._base, interpreter, itertools, multiprocessing.connection, multiprocessing.queues, multiprocessing.synchronize, process, sys, thread, time)
    • collections (native: _collections, _weakref, itertools, sys)
    • functools (native: _functools, _thread)
    • multiprocessing (native: _multiprocessing, _posixshmem, _posixsubprocess, _winapi, array, atexit, collections.abc, connection, context, dummy, errno, forkserver, heap, itertools, managers, mmap, msvcrt, multiprocessing.connection, pool, popen_fork, popen_forkserver, popen_spawn_posix, popen_spawn_win32, queues, resource_sharer, resource_tracker, sharedctypes, spawn, synchronize, sys, time, util, xmlrpc.client)
    • os (native: nt, os.path, posix, sys)
    • threading (native: _collections, _contextvars, _thread, itertools, sys, time)
    • traceback (native: _missing_stdlib_info, _suggestions, collections.abc, importlib.machinery, importlib.resources.readers, importlib.util, itertools, sys, unicodedata)
    • types (native: _types, sys)
    • weakref (native: _weakref, atexit, gc, itertools, sys)
    • logging, queue

dependent tests: (17 tests)

  • concurrent: test_asyncio test_compileall test_concurrent_futures test_context test_genericalias test_inspect test_struct test_sys test_threading test_types test_wmi
    • asyncio: test_asyncio test_external_inspection test_logging test_os test_pdb test_unittest

[ ] lib: cpython/Lib/asyncio
[ ] test: cpython/Lib/test/test_asyncio (TODO: 3)

dependencies:

  • asyncio (native: _asyncio, _overlapped, _pyrepl.console, _pyrepl.main, _pyrepl.simple_interact, _remote_debugging, _winapi, asyncio.tools, base_events, collections.abc, concurrent.futures, coroutines, errno, events, exceptions, futures, graph, itertools, locks, log, math, msvcrt, protocols, queues, readline, runners, streams, sys, taskgroups, tasks, threads, time, timeouts, transports, unix_events, windows_events)
    • _colorize (native: builtins, collections.abc, nt, sys)
    • argparse (native: sys)
    • ast (native: _ast, sys)
    • code (native: builtins, readline, sys)
    • contextlib (native: sys)
    • dataclasses (native: itertools, sys)
    • inspect (native: _weakref, builtins, collections.abc, importlib.machinery, itertools, sys)
    • io (native: _io, _thread, errno, msvcrt, sys)
    • linecache (native: sys)
    • rlcompleter (native: main, atexit, builtins, readline)
    • site (native: _io, _pyrepl.main, _pyrepl.pager, _pyrepl.readline, _pyrepl.unix_console, _pyrepl.windows_console, atexit, builtins, errno, readline, sitecustomize, sys, usercustomize)
    • socket (native: _socket, array, errno, sys)
    • ssl (native: _ssl, errno, sys, time)
    • stat (native: _stat)
    • subprocess (native: _posixsubprocess, _winapi, builtins, errno, fcntl, grp, msvcrt, pwd, select, sys, time)
    • tokenize (native: _tokenize, builtins, itertools, sys)
    • collections, functools, os, threading, traceback, types, weakref
    • contextvars, enum, heapq, logging, reprlib, selectors, signal, struct, warnings

dependent tests: (8 tests)

  • asyncio: test_asyncio test_concurrent_futures test_external_inspection test_inspect test_logging test_os test_pdb test_unittest

[ ] test: cpython/Lib/test/test_bytes.py (TODO: 14)

dependencies:

dependent tests: (no tests depend on bytes)

Legend:

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

@youknowone youknowone left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

👍

@youknowone
youknowone merged commit bf404e0 into RustPython:main Oct 15, 2026
22 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