Implement bytearray.__str__ && bytes.__str__#7477
Implement bytearray.__str__ && bytes.__str__#7477moreal wants to merge 3 commits intoRustPython:mainfrom
bytearray.__str__ && bytes.__str__#7477Conversation
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Path: .coderabbit.yml Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (2)
🚧 Files skipped from review as they are similar to previous changes (2)
📝 WalkthroughWalkthroughAdded Changes
Sequence DiagramsequenceDiagram
participant Caller
participant PyObj as PyBytes / PyByteArray
participant Inner as PyBytesInner
participant VM as VirtualMachine
participant Config as VM Config
Caller->>PyObj: str(obj)
PyObj->>Inner: warn_on_str(message, vm)
Inner->>Config: read bytes_warning
Config-->>Inner: bytes_warning level
alt bytes_warning > 0
Inner->>VM: emit bytes_warning(exception, message)
end
Inner-->>PyObj: Ok(())
PyObj->>PyObj: compute repr / repr_with_name
PyObj-->>Caller: PyStr (string)
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Possibly related PRs
Suggested reviewers
Poem
🚥 Pre-merge checks | ✅ 2 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
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. Comment |
There was a problem hiding this comment.
🧹 Nitpick comments (1)
crates/vm/src/builtins/bytes.rs (1)
227-231: Please add regression coverage for warning modes.Recommend adding tests that assert:
- default mode: no warning,
-b:BytesWarningemitted,-bb: warning escalates appropriately,
whilestr(bytes_obj)output remains unchanged in all cases.🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@crates/vm/src/builtins/bytes.rs` around lines 227 - 231, Add regression tests for the bytes __str__ path that exercise PyBytesInner::warn_on_str via the bytes.__str__ implementation: create a bytes object, call str(bytes_obj) and assert its output is unchanged, then run three scenarios asserting warnings/behavior: (1) default mode — no warning emitted; (2) warning mode "-b" — a BytesWarning is emitted when calling str(bytes_obj); (3) warning mode "-bb" — the warning is escalated (treated as an error) when calling str(bytes_obj). Use the same call into the code path that triggers PyBytesInner::warn_on_str and make explicit assertions about the presence/absence/type of warning while keeping the returned string identical in all cases.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Nitpick comments:
In `@crates/vm/src/builtins/bytes.rs`:
- Around line 227-231: Add regression tests for the bytes __str__ path that
exercise PyBytesInner::warn_on_str via the bytes.__str__ implementation: create
a bytes object, call str(bytes_obj) and assert its output is unchanged, then run
three scenarios asserting warnings/behavior: (1) default mode — no warning
emitted; (2) warning mode "-b" — a BytesWarning is emitted when calling
str(bytes_obj); (3) warning mode "-bb" — the warning is escalated (treated as an
error) when calling str(bytes_obj). Use the same call into the code path that
triggers PyBytesInner::warn_on_str and make explicit assertions about the
presence/absence/type of warning while keeping the returned string identical in
all cases.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yml
Review profile: CHILL
Plan: Pro
Run ID: 97d67d10-55d4-45a2-9406-15e1acf0abdc
📒 Files selected for processing (3)
crates/vm/src/builtins/bytearray.rscrates/vm/src/builtins/bytes.rscrates/vm/src/bytes_inner.rs
crates/vm/src/builtins/bytearray.rs
Outdated
| #[pymethod] | ||
| fn __str__(zelf: &Py<Self>, vm: &VirtualMachine) -> PyResult<PyStrRef> { |
There was a problem hiding this comment.
| #[pymethod] | |
| fn __str__(zelf: &Py<Self>, vm: &VirtualMachine) -> PyResult<PyStrRef> { | |
| #[pyslot] | |
| fn slot_str(zelf: &PyObject, vm: &VirtualMachine) -> PyResult<PyStrRef> { |
does this work?
There was a problem hiding this comment.
Yes. I applied it in 590e514
I think it would be nice to have a trait that implements slot_str (like Representable), but I can't think of a good name for it. 😅
There was a problem hiding this comment.
If you are interested in, could you also check this? #6485
That was a trial that removing single function traits. But this is a big change of interface design. I'd like to listen how other people think about it.
There was a problem hiding this comment.
I left my thought on the pull request #6485 (comment)
This pull request let
bytearray.__str__andbytes.__str__make warning. With-bor-bboption, it will show or raise warning.Behavior changes
CPython (3.14.3)
RustPython (before)
RustPython (after)
CPython reference
bytes.__str__https://github.com/python/cpython/blob/v3.14.3/Objects/bytesobject.c#L1423-L1433bytearray.__str__https://github.com/python/cpython/blob/v3.14.3/Objects/bytearrayobject.c#L1171-L1181Summary by CodeRabbit