summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorandrei kulakov <andrei.avk@gmail.com>2022-12-09 16:14:33 (GMT)
committerGitHub <noreply@github.com>2022-12-09 16:14:33 (GMT)
commitd0679c12398579fe9f10e78b6332dded119e4697 (patch)
tree840c814be55fe6579fe099c27bf7b8c2614f85ed
parenta29a7b9b786d6b928c4bb4e6e683a3788e3ab1c1 (diff)
downloadcpython-d0679c12398579fe9f10e78b6332dded119e4697.zip
cpython-d0679c12398579fe9f10e78b6332dded119e4697.tar.gz
cpython-d0679c12398579fe9f10e78b6332dded119e4697.tar.bz2
bpo-44512: Fix handling of extrasactions arg to csv.DictWriter with mixed or upper case (#26924)
-rw-r--r--Lib/csv.py3
-rw-r--r--Lib/test/test_csv.py8
-rw-r--r--Misc/NEWS.d/next/Library/2022-12-09-10-35-36.bpo-44592.z-P3oe.rst2
3 files changed, 12 insertions, 1 deletions
diff --git a/Lib/csv.py b/Lib/csv.py
index 309a8f3..4ef8be4 100644
--- a/Lib/csv.py
+++ b/Lib/csv.py
@@ -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
diff --git a/Lib/test/test_csv.py b/Lib/test/test_csv.py
index d64bff1..8289ddb 100644
--- a/Lib/test/test_csv.py
+++ b/Lib/test/test_csv.py
@@ -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
index 0000000..7f29060
--- /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`.