]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-44512: Fix handling of extrasactions arg to csv.DictWriter with mixed or upper...
authorandrei kulakov <andrei.avk@gmail.com>
Fri, 9 Dec 2022 16:14:33 +0000 (11:14 -0500)
committerGitHub <noreply@github.com>
Fri, 9 Dec 2022 16:14:33 +0000 (16:14 +0000)
Lib/csv.py
Lib/test/test_csv.py
Misc/NEWS.d/next/Library/2022-12-09-10-35-36.bpo-44592.z-P3oe.rst [new file with mode: 0644]

index 309a8f3f486365b2bcf4aecd871b798390334080..4ef8be45ca9e0ac1ac18481a3acaeef72ae5a95f 100644 (file)
@@ -139,7 +139,8 @@ class DictWriter:
             fieldnames = list(fieldnames)
         self.fieldnames = fieldnames    # list of keys for the dict
         self.restval = restval          # for writing short dicts
-        if extrasaction.lower() not in ("raise", "ignore"):
+        extrasaction = extrasaction.lower()
+        if extrasaction not in ("raise", "ignore"):
             raise ValueError("extrasaction (%s) must be 'raise' or 'ignore'"
                              % extrasaction)
         self.extrasaction = extrasaction
index d64bff13a44e877a6f195a3d7edf6417817ecc1b..8289ddb1c3a54f5c5ef47bb8b0213704144ec903 100644 (file)
@@ -762,6 +762,10 @@ class TestDictFields(unittest.TestCase):
         dictrow = {'f0': 0, 'f1': 1, 'f2': 2, 'f3': 3}
         self.assertRaises(ValueError, csv.DictWriter.writerow, writer, dictrow)
 
+        # see bpo-44512 (differently cased 'raise' should not result in 'ignore')
+        writer = csv.DictWriter(fileobj, ['f1', 'f2'], extrasaction="RAISE")
+        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")
@@ -769,6 +773,10 @@ class TestDictFields(unittest.TestCase):
         csv.DictWriter.writerow(writer, dictrow)
         self.assertEqual(fileobj.getvalue(), "1,2\r\n")
 
+        # bpo-44512
+        writer = csv.DictWriter(fileobj, ['f1', 'f2'], extrasaction="IGNORE")
+        csv.DictWriter.writerow(writer, dictrow)
+
     def test_dict_reader_fieldnames_accepts_iter(self):
         fieldnames = ["a", "b", "c"]
         f = StringIO()
diff --git a/Misc/NEWS.d/next/Library/2022-12-09-10-35-36.bpo-44592.z-P3oe.rst b/Misc/NEWS.d/next/Library/2022-12-09-10-35-36.bpo-44592.z-P3oe.rst
new file mode 100644 (file)
index 0000000..7f29060
--- /dev/null
@@ -0,0 +1,2 @@
+Fixes inconsistent handling of case sensitivity of *extrasaction* arg in
+:class:`csv.DictWriter`.