summaryrefslogtreecommitdiffstats
path: root/Lib/test
diff options
context:
space:
mode:
authorR David Murray <rdmurray@bitdance.com>2013-11-19 18:16:20 (GMT)
committerR David Murray <rdmurray@bitdance.com>2013-11-19 18:16:20 (GMT)
commitfb099c9ef134166ecaf3905c0d8b2995245f3ded (patch)
tree2f41745c93f1daaf0dbe505dc1ebf24227f63651 /Lib/test
parent30c5ad2aa2a809d82490010850dc49b54de36635 (diff)
downloadcpython-fb099c9ef134166ecaf3905c0d8b2995245f3ded.zip
cpython-fb099c9ef134166ecaf3905c0d8b2995245f3ded.tar.gz
cpython-fb099c9ef134166ecaf3905c0d8b2995245f3ded.tar.bz2
#19449: Handle non-string keys when generating 'fieldnames' error.
csv was handling non-string keys fine except for the error message generated when a non-string key was not in 'fieldnames'. Fix by Tomas Grahn, full patch-with-test by Vajrasky Kok (tweaked slightly).
Diffstat (limited to 'Lib/test')
-rw-r--r--Lib/test/test_csv.py12
1 files changed, 12 insertions, 0 deletions
diff --git a/Lib/test/test_csv.py b/Lib/test/test_csv.py
index 976e620..479ebd9 100644
--- a/Lib/test/test_csv.py
+++ b/Lib/test/test_csv.py
@@ -570,6 +570,18 @@ class TestDictFields(unittest.TestCase):
fileobj = StringIO()
self.assertRaises(TypeError, csv.DictWriter, fileobj)
+ def test_write_fields_not_in_fieldnames(self):
+ with TemporaryFile("w+", newline='') as fileobj:
+ writer = csv.DictWriter(fileobj, fieldnames = ["f1", "f2", "f3"])
+ # Of special note is the non-string key (issue 19449)
+ with self.assertRaises(ValueError) as cx:
+ writer.writerow({"f4": 10, "f2": "spam", 1: "abc"})
+ exception = str(cx.exception)
+ self.assertIn("fieldnames", exception)
+ self.assertIn("'f4'", exception)
+ self.assertNotIn("'f2'", exception)
+ self.assertIn("1", exception)
+
def test_read_dict_fields(self):
with TemporaryFile("w+") as fileobj:
fileobj.write("1,2,abc\r\n")