bpo-32255: Always quote a single empty field when write into a CSV file. (#4769)
This allows to distinguish an empty row from a row consisting of a single empty field.
This commit is contained in:
parent
e0720cd9e4
commit
2001900b0c
|
@ -211,6 +211,25 @@ class Test_Csv(unittest.TestCase):
|
||||||
fileobj.seek(0)
|
fileobj.seek(0)
|
||||||
self.assertEqual(fileobj.read(), "a,b\r\nc,d\r\n")
|
self.assertEqual(fileobj.read(), "a,b\r\nc,d\r\n")
|
||||||
|
|
||||||
|
def test_writerows_with_none(self):
|
||||||
|
with TemporaryFile("w+", newline='') as fileobj:
|
||||||
|
writer = csv.writer(fileobj)
|
||||||
|
writer.writerows([['a', None], [None, 'd']])
|
||||||
|
fileobj.seek(0)
|
||||||
|
self.assertEqual(fileobj.read(), "a,\r\n,d\r\n")
|
||||||
|
|
||||||
|
with TemporaryFile("w+", newline='') as fileobj:
|
||||||
|
writer = csv.writer(fileobj)
|
||||||
|
writer.writerows([[None], ['a']])
|
||||||
|
fileobj.seek(0)
|
||||||
|
self.assertEqual(fileobj.read(), '""\r\na\r\n')
|
||||||
|
|
||||||
|
with TemporaryFile("w+", newline='') as fileobj:
|
||||||
|
writer = csv.writer(fileobj)
|
||||||
|
writer.writerows([['a'], [None]])
|
||||||
|
fileobj.seek(0)
|
||||||
|
self.assertEqual(fileobj.read(), 'a\r\n""\r\n')
|
||||||
|
|
||||||
@support.cpython_only
|
@support.cpython_only
|
||||||
def test_writerows_legacy_strings(self):
|
def test_writerows_legacy_strings(self):
|
||||||
import _testcapi
|
import _testcapi
|
||||||
|
|
|
@ -1554,6 +1554,7 @@ Joel Taddei
|
||||||
Arfrever Frehtes Taifersar Arahesis
|
Arfrever Frehtes Taifersar Arahesis
|
||||||
Hideaki Takahashi
|
Hideaki Takahashi
|
||||||
Takase Arihiro
|
Takase Arihiro
|
||||||
|
Licht Takeuchi
|
||||||
Indra Talip
|
Indra Talip
|
||||||
Neil Tallim
|
Neil Tallim
|
||||||
Geoff Talvola
|
Geoff Talvola
|
||||||
|
|
|
@ -0,0 +1,3 @@
|
||||||
|
A single empty field is now always quoted when written into a CSV file.
|
||||||
|
This allows to distinguish an empty row from a row consisting of a single empty field.
|
||||||
|
Patch by Licht Takeuchi.
|
|
@ -1238,7 +1238,7 @@ csv_writerow(WriterObj *self, PyObject *seq)
|
||||||
if (PyErr_Occurred())
|
if (PyErr_Occurred())
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
if (self->num_fields > 0 && self->rec_size == 0) {
|
if (self->num_fields > 0 && self->rec_len == 0) {
|
||||||
if (dialect->quoting == QUOTE_NONE) {
|
if (dialect->quoting == QUOTE_NONE) {
|
||||||
PyErr_Format(_csvstate_global->error_obj,
|
PyErr_Format(_csvstate_global->error_obj,
|
||||||
"single empty field record must be quoted");
|
"single empty field record must be quoted");
|
||||||
|
|
Loading…
Reference in New Issue