Support csv writer QUOTE_STRINGS and QUOTE_NOTNULL#8109
Conversation
📝 WalkthroughWalkthroughFixes a panic in ChangesCSV QUOTE_STRINGS and QUOTE_NOTNULL writer implementation
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Possibly related PRs
Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 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.
Actionable comments posted: 1
🧹 Nitpick comments (1)
crates/stdlib/src/csv.rs (1)
1172-1173: 💤 Low valueUnused lock acquisition.
The
_statelock guard is acquired but never used in this method since it builds output manually rather than using thecsv_core::WriterinWriteState. While harmless, this unnecessary synchronization could be removed.Proposed fix
fn writerow_quoted_strings(&self, row: PyObjectRef, vm: &VirtualMachine) -> PyResult { - let _state = self.state.lock(); let row: ArgIterable = ArgIterable::try_from_object(vm, row.clone()).map_err(|_e| {🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@crates/stdlib/src/csv.rs` around lines 1172 - 1173, In the writerow_quoted_strings method, remove the unnecessary lock acquisition line `let _state = self.state.lock();` since the lock guard is never actually used in the method. The method builds output manually without relying on the csv_core::Writer in WriteState, so this synchronization is not needed and can be safely deleted.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@crates/stdlib/src/csv.rs`:
- Around line 1138-1141: The code is unconditionally doubling the escape
character inside quoted fields when it encounters dialect.escapechar, but when
doublequote=True (the default), only quotes should be doubled, not escape
characters. Modify the logic that checks if dialect.escapechar == Some(byte) to
only apply the escape character doubling when doublequote=False. If doublequote
is True, skip the conditional block that pushes the byte an extra time for
escape characters, allowing only the single push outside the condition to
execute.
---
Nitpick comments:
In `@crates/stdlib/src/csv.rs`:
- Around line 1172-1173: In the writerow_quoted_strings method, remove the
unnecessary lock acquisition line `let _state = self.state.lock();` since the
lock guard is never actually used in the method. The method builds output
manually without relying on the csv_core::Writer in WriteState, so this
synchronization is not needed and can be safely deleted.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yml
Review profile: CHILL
Plan: Pro
Run ID: 4b5c06f1-7810-4acc-a372-e3408986c961
📒 Files selected for processing (2)
crates/stdlib/src/csv.rsextra_tests/snippets/stdlib_csv.py
I was surprised this wasn't already covered by a CPython test too :) Thanks for review! |
|
Probably adding a test to cpython will be also worth to do if you are interested in |
Implement writer handling for QUOTE_STRINGS and QUOTE_NOTNULL so the exported constants no longer panic and follow CPython behavior for string, non-null, None, and quotechar validation cases. Constraint: RustPython contribution policy requires CPython-compatible behavior through Rust-side changes and regression coverage outside Lib/. Rejected: Marking CPython Lib tests or hiding the constants | would preserve a runtime panic for supported public constants. Confidence: high Scope-risk: narrow Directive: Keep QUOTE_STRINGS and QUOTE_NOTNULL writer behavior aligned with CPython's csv module when extending dialect validation. Tested: cargo check -p rustpython-stdlib; cargo run --release -- extra_tests/snippets/stdlib_csv.py; cargo run --release -- -m test test_csv; cargo clippy; pre-commit run --all-files via temporary venv/PATH shim; cargo test --workspace --exclude rustpython_wasm --exclude rustpython-venvlauncher -- --test-threads=1 Not-tested: Remote CI and PR creation were not run because remote git operations require explicit approval. Assisted-by: Codex:gpt-5.5
Co-authored-by: Shahar Naveh <50263213+ShaharNaveh@users.noreply.github.com> Assisted-by: Codex:gpt-5.5
Co-authored-by: Shahar Naveh <50263213+ShaharNaveh@users.noreply.github.com> Assisted-by: Codex:gpt-5.5
Apply maintainer-requested expression cleanups and rustfmt wrapping so the PR lint job can pass without changing behavior. Constraint: GitHub Actions Lint failed on rustfmt output after the review suggestion. Rejected: Changing csv writer semantics | the failing check was formatting-only and all behavior tests already passed. Confidence: high Scope-risk: narrow Directive: Keep future review suggestions rustfmt-clean before pushing. Tested: cargo fmt --check; cargo check -p rustpython-stdlib; cargo run --release -- extra_tests/snippets/stdlib_csv.py; cargo run --release -- -m test test_csv; cargo clippy; pre-commit run --all-files; cargo test --workspace --exclude rustpython_wasm --exclude rustpython-venvlauncher -- --test-threads=1 Not-tested: Remote CI has not rerun yet; this commit will trigger it after push. Assisted-by: Codex:gpt-5.5
e14d586 to
eb9f111
Compare
Summary
Implements writer support for
csv.QUOTE_STRINGSandcsv.QUOTE_NOTNULL.These constants were already exposed, but using either quote style with
csv.writerreached an internaltodo!()and panicked.Problem
csv.writer(..., quoting=csv.QUOTE_STRINGS)andcsv.writer(..., quoting=csv.QUOTE_NOTNULL)should behave like CPython.Instead, RustPython converted these quote styles through an unimplemented path,
so normal Python code could terminate the interpreter with a Rust panic.
Fixes
QUOTE_STRINGSandQUOTE_NOTNULLaway from thetodo!()writer path.QUOTE_STRINGSquotes original string fields and still quotes fields thatrequire quoting because of delimiters, quote characters, or line endings.
QUOTE_NOTNULLquotes every field exceptNone.Noneis written as an empty unquoted field.None.quotechar=Nonewhen quoting is enabled.extra_tests/snippets/stdlib_csv.py.Testing
stdlib_csvregression with RustPython.test_csvpasses.AI use
Assisted by codex gpt-5.5 for implementation and test drafting. I reviewed, edited, and verified the changes locally.
Summary by CodeRabbit
Bug Fixes
QUOTE_STRINGSandQUOTE_NOTNULLso fields are escaped/quoted according to the active dialect.quotechar=Nonewhen quoting is enabled.New Features
Tests