Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions Lib/test/test_csv.py
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,6 @@ def test_write_bigfield(self):
self._write_test([bigstring,bigstring], '%s,%s' % \
(bigstring, bigstring))

@unittest.expectedFailure # TODO: RUSTPYTHON
def test_write_quoting(self):
self._write_test(['a',1,'p,q'], 'a,1,"p,q"')
self._write_error_test(csv.Error, ['a',1,'p,q'],
Expand Down Expand Up @@ -864,7 +863,6 @@ def test_read_escape_fieldsep(self):
class TestDialectUnix(TestCsvBase):
dialect = 'unix'

@unittest.expectedFailure # TODO: RUSTPYTHON
def test_simple_writer(self):
self.writerAssertEqual([[1, 'abc def', 'abc']], '"1","abc def","abc"\n')

Expand Down
50 changes: 45 additions & 5 deletions crates/stdlib/src/csv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -462,7 +462,7 @@ mod _csv {
impl From<QuoteStyle> for csv_core::QuoteStyle {
fn from(val: QuoteStyle) -> Self {
match val {
QuoteStyle::Minimal => Self::Always,
QuoteStyle::Minimal => Self::Necessary,
QuoteStyle::All => Self::Always,
QuoteStyle::Nonnumeric => Self::NonNumeric,
QuoteStyle::None => Self::Never,
Expand Down Expand Up @@ -796,6 +796,48 @@ mod _csv {
delimiter
}

fn get_lineterminator(&self) -> csv_core::Terminator {
let mut lineterminator = match &self.dialect {
DialectItem::Str(name) => {
let g = GLOBAL_HASHMAP.lock();
if let Some(dialect) = g.get(name) {
dialect.lineterminator
} else {
Terminator::CRLF
}
}
DialectItem::Obj(obj) => obj.lineterminator,
_ => Terminator::CRLF,
};

if let Some(attr) = self.lineterminator {
lineterminator = attr
}

lineterminator
}

fn get_quoting(&self) -> QuoteStyle {
let mut quoting = match &self.dialect {
DialectItem::Str(name) => {
let g = GLOBAL_HASHMAP.lock();
if let Some(dialect) = g.get(name) {
dialect.quoting
} else {
QuoteStyle::Minimal
}
}
DialectItem::Obj(obj) => obj.quoting,
_ => QuoteStyle::Minimal,
};

if let Some(attr) = self.quoting {
quoting = attr
}

quoting
}

fn to_reader(&self) -> csv_core::Reader {
let mut builder = csv_core::ReaderBuilder::new();
let mut reader = match &self.dialect {
Expand Down Expand Up @@ -918,15 +960,13 @@ mod _csv {
writer = writer.double_quote(t);
}

writer = writer.terminator(self.lineterminator.unwrap_or(Terminator::CRLF));
writer = writer.terminator(self.get_lineterminator());

if let Some(e) = self.escapechar {
writer = writer.escape(e);
}

if let Some(e) = self.quoting {
writer = writer.quote_style(e.into());
}
writer = writer.quote_style(self.get_quoting().into());

writer.build()
}
Expand Down
Loading