]> granicus.if.org Git - python/commitdiff
Issue #18219: Optimize csv.DictWriter for large number of columns.
authorINADA Naoki <songofacandy@gmail.com>
Fri, 21 Oct 2016 10:47:57 +0000 (19:47 +0900)
committerINADA Naoki <songofacandy@gmail.com>
Fri, 21 Oct 2016 10:47:57 +0000 (19:47 +0900)
Patch by Mariatta Wijaya.

Doc/library/csv.rst
Lib/csv.py
Lib/test/test_csv.py
Misc/NEWS

index 6341bc31262b7d85fa07a202ab9530e2bae46521..f916572fd5d454d59bef5f03fcaa25ebe0e91ff6 100644 (file)
@@ -195,10 +195,12 @@ The :mod:`csv` module defines the following classes:
    written if the dictionary is missing a key in *fieldnames*.  If the
    dictionary passed to the :meth:`writerow` method contains a key not found in
    *fieldnames*, the optional *extrasaction* parameter indicates what action to
-   take.  If it is set to ``'raise'`` a :exc:`ValueError` is raised.  If it is
-   set to ``'ignore'``, extra values in the dictionary are ignored.  Any other
-   optional or keyword arguments are passed to the underlying :class:`writer`
-   instance.
+   take.
+   If it is set to ``'raise'``, the default value, a :exc:`ValueError`
+   is raised.
+   If it is set to ``'ignore'``, extra values in the dictionary are ignored.
+   Any other optional or keyword arguments are passed to the underlying
+   :class:`writer` instance.
 
    Note that unlike the :class:`DictReader` class, the *fieldnames* parameter
    of the :class:`DictWriter` is not optional.  Since Python's :class:`dict`
index 0481ea5586fb1e68db2ea9af6968a9d55f8434d9..0349e0bd1162c4bd6bc7ea3a387e7fbee2e7d873 100644 (file)
@@ -145,7 +145,7 @@ class DictWriter:
 
     def _dict_to_list(self, rowdict):
         if self.extrasaction == "raise":
-            wrong_fields = [k for k in rowdict if k not in self.fieldnames]
+            wrong_fields = rowdict.keys() - self.fieldnames
             if wrong_fields:
                 raise ValueError("dict contains fields not in fieldnames: "
                                  + ", ".join([repr(x) for x in wrong_fields]))
index 7dcea9ccb3b83c3b9add4cfb310cc04904ee25ce..03ab1840dd0df922d0cf58ba6c65e9bcbca94035 100644 (file)
@@ -626,6 +626,24 @@ class TestDictFields(unittest.TestCase):
             self.assertNotIn("'f2'", exception)
             self.assertIn("1", exception)
 
+    def test_typo_in_extrasaction_raises_error(self):
+        fileobj = StringIO()
+        self.assertRaises(ValueError, csv.DictWriter, fileobj, ['f1', 'f2'],
+                          extrasaction="raised")
+
+    def test_write_field_not_in_field_names_raise(self):
+        fileobj = StringIO()
+        writer = csv.DictWriter(fileobj, ['f1', 'f2'], extrasaction="raise")
+        dictrow = {'f0': 0, 'f1': 1, 'f2': 2, 'f3': 3}
+        self.assertRaises(ValueError, csv.DictWriter.writerow, writer, dictrow)
+
+    def test_write_field_not_in_field_names_ignore(self):
+        fileobj = StringIO()
+        writer = csv.DictWriter(fileobj, ['f1', 'f2'], extrasaction="ignore")
+        dictrow = {'f0': 0, 'f1': 1, 'f2': 2, 'f3': 3}
+        csv.DictWriter.writerow(writer, dictrow)
+        self.assertEqual(fileobj.getvalue(), "1,2\r\n")
+
     def test_read_dict_fields(self):
         with TemporaryFile("w+") as fileobj:
             fileobj.write("1,2,abc\r\n")
index 0ce27de9e6a2af21fa2f46eee0d8a14686f00e7b..237c9288e6c93f01411f595c7a5396edf7adfab0 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -20,6 +20,9 @@ Core and Builtins
 Library
 -------
 
+- Issue #18219: Optimize csv.DictWriter for large number of columns.
+  Patch by Mariatta Wijaya.
+
 - Issue #28448: Fix C implemented asyncio.Future didn't work on Windows.
 
 - Issue #28480: Fix error building socket module when multithreading is