[Python-checkins] bpo-44512: Fix handling of extrasactions arg to csv.DictWriter with mixed or upper case (#26924)

iritkatriel webhook-mailer at python.org
Fri Dec 9 11:14:39 EST 2022


https://github.com/python/cpython/commit/d0679c12398579fe9f10e78b6332dded119e4697
commit: d0679c12398579fe9f10e78b6332dded119e4697
branch: main
author: andrei kulakov <andrei.avk at gmail.com>
committer: iritkatriel <1055913+iritkatriel at users.noreply.github.com>
date: 2022-12-09T16:14:33Z
summary:

bpo-44512: Fix handling of extrasactions arg to csv.DictWriter with mixed or upper case (#26924)

files:
A Misc/NEWS.d/next/Library/2022-12-09-10-35-36.bpo-44592.z-P3oe.rst
M Lib/csv.py
M Lib/test/test_csv.py

diff --git a/Lib/csv.py b/Lib/csv.py
index 309a8f3f4863..4ef8be45ca9e 100644
--- a/Lib/csv.py
+++ b/Lib/csv.py
@@ -139,7 +139,8 @@ def __init__(self, f, fieldnames, restval="", extrasaction="raise",
             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
diff --git a/Lib/test/test_csv.py b/Lib/test/test_csv.py
index d64bff13a44e..8289ddb1c3a5 100644
--- a/Lib/test/test_csv.py
+++ b/Lib/test/test_csv.py
@@ -762,6 +762,10 @@ def test_write_field_not_in_field_names_raise(self):
         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 @@ def test_write_field_not_in_field_names_ignore(self):
         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
index 000000000000..7f290605934d
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2022-12-09-10-35-36.bpo-44592.z-P3oe.rst
@@ -0,0 +1,2 @@
+Fixes inconsistent handling of case sensitivity of *extrasaction* arg in
+:class:`csv.DictWriter`.



More information about the Python-checkins mailing list