csv: apply dialect quoting and lineterminator in writer#8254
Conversation
|
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 ignored due to path filters (1)
📒 Files selected for processing (1)
🚧 Files skipped from review as they are similar to previous changes (1)
📝 WalkthroughWalkthroughCSV writer configuration now respects dialect-provided quoting and line terminators, while explicit ChangesCSV formatting behavior
Estimated code review effort: 2 (Simple) | ~10 minutes Possibly related PRs
Suggested reviewers: 🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ 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 |
📦 Library DependenciesThe following Lib/ modules were modified. Here are their dependencies: [x] lib: cpython/Lib/csv.py dependencies:
dependent tests: (4 tests)
Legend:
|
|
Hello! With this PR, RustPython writes \r,\n!, while CPython writes "\r","\n"!. csv.writer(sio, lineterminator="!").writerow(["\r", "\n"])It looks like embedded CR/LF may no longer be quoted when the line terminator is not CR or LF. |
QuoteStyle::Minimal was mapped to csv_core's Always, which quoted every field even under QUOTE_MINIMAL. Map it to Necessary so only fields that require quoting are quoted. Unmarks Test_Csv.test_write_quoting. Assisted-by: Claude Code:claude-opus-4-8
csv.writer ignored the quoting and lineterminator from a dialect (whether passed by name or object), always using the defaults. Resolve both from the dialect via get_quoting()/get_lineterminator(), mirroring get_delimiter(), so an explicit keyword argument still overrides the dialect. Unmarks TestDialectUnix.test_simple_writer. Assisted-by: Claude Code:claude-opus-4-8
7145002 to
13ef019
Compare
|
@doma17 thanks. From what I found, the cause seems to be in csv-core: for QUOTE_MINIMAL it decides quoting by the terminator, so with a custom lineterminator it doesn't quote \r/\n. CPython always quotes them. To fix it, I think we'd move QUOTE_MINIMAL to our native write path (like QUOTE_NONE) and unmark test_write_lineterminator. But that's a different test than this PR targeted, so the scope gets bigger than the original issue. Fix it here, or as a follow-up? |
Summary
csv.writerignored thequotingandlineterminatordefined by a dialect (passed by name likedialect='unix'or as a dialect object), so writing with a built-in dialect diverged from CPython. Only explicit keyword arguments took effect.to_writernow resolves both from the dialect (viaget_quoting()/get_lineterminator(), mirroring the existingget_delimiter()), while an explicit keyword argument still overrides it.While fixing this I also found the
QuoteStyle::Minimal→ csv_core mapping wasAlways(quoting every field underQUOTE_MINIMAL); it is nowNecessary. This was dormant because the quote style was only applied for an explicitquoting=argument, so it had to be fixed before applying the dialect's quoting.Test plan
TestDialectUnix.test_simple_writerandTest_Csv.test_write_quoting(both@unittest.expectedFailurebefore).cargo run --release -- -m test test_csv: 128 run, 7 skipped, SUCCESS.extra_tests/snippets/stdlib_csv.py: passes.cargo fmt --check/cargo clippy: clean (no new warnings).dialect='unix'andQUOTE_MINIMALcases match.Assisted-by: Claude Code:claude-opus-4-8
Summary by CodeRabbit