Skip to content

Panic in csv.writer with QUOTE_STRINGS or QUOTE_NOTNULL #8108

Description

@doma17

Summary

csv.writer panics when quoting is set to csv.QUOTE_STRINGS or
csv.QUOTE_NOTNULL.

Both constants are exposed by RustPython, but the writer path still reaches a
todo!() implementation for these quote styles.

Reproducer

import csv
import io

buf = io.StringIO()
csv.writer(buf, quoting=csv.QUOTE_STRINGS).writerow(["x", 1, None, ""])
print(repr(buf.getvalue()))

buf = io.StringIO()
csv.writer(buf, quoting=csv.QUOTE_NOTNULL).writerow(["x", 1, None, ""])
print(repr(buf.getvalue()))

Expected

CPython exits normally and produces:

'"x",1,,""\r\n'
'"x","1",,""\r\n'

QUOTE_STRINGS quotes fields whose original value is a string, while None
is written as an empty unquoted field.

QUOTE_NOTNULL quotes every field except None, which is written as an empty
unquoted field.

A single empty field produced from None should raise csv.Error:

csv.writer(io.StringIO(), quoting=csv.QUOTE_STRINGS).writerow([None])
csv.writer(io.StringIO(), quoting=csv.QUOTE_NOTNULL).writerow([None])

Actual

RustPython panics with:

thread 'main' panicked at crates/stdlib/src/csv.rs:...:
not yet implemented

The panic comes from the QuoteStyle::Strings / QuoteStyle::Notnull mapping
inside the native _csv implementation.

Python Documentation

csv.QUOTE_STRINGS and csv.QUOTE_NOTNULL are documented quoting constants:

https://docs.python.org/3/library/csv.html#csv.QUOTE_STRINGS
https://docs.python.org/3/library/csv.html#csv.QUOTE_NOTNULL

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions