diff -r 267ad2ed4138 Lib/csv.py --- a/Lib/csv.py Mon Nov 11 14:50:13 2013 -0500 +++ b/Lib/csv.py Tue Nov 12 16:57:37 2013 +0800 @@ -143,7 +143,8 @@ 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 = [repr(k) for k in rowdict if k not in + self.fieldnames] if wrong_fields: raise ValueError("dict contains fields not in fieldnames: " + ", ".join(wrong_fields)) diff -r 267ad2ed4138 Lib/test/test_csv.py --- a/Lib/test/test_csv.py Mon Nov 11 14:50:13 2013 -0500 +++ b/Lib/test/test_csv.py Tue Nov 12 16:57:37 2013 +0800 @@ -579,6 +579,17 @@ 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"]) + with self.assertRaises(ValueError) as cx: + writer.writerow({"f4": 10, "f2": "spam", 1: "abc"}) + exception = str(cx.exception) + self.assertIn("dict contains fields not in 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")