Merge: #19449: Handle non-string keys when generating 'fieldnames' error.

This commit is contained in:
R David Murray 2013-11-19 13:17:26 -05:00
commit 5160da1afc
3 changed files with 16 additions and 1 deletions

View File

@ -146,7 +146,7 @@ class DictWriter:
wrong_fields = [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))
+ ", ".join([repr(x) for x in wrong_fields]))
return [rowdict.get(key, self.restval) for key in self.fieldnames]
def writerow(self, rowdict):

View File

@ -579,6 +579,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")

View File

@ -56,6 +56,9 @@ Core and Builtins
Library
-------
- Issue #19449: in csv's writerow, handle non-string keys when generating the
error message that certain keys are not in the 'fieldnames' list.
- Issue #8402: Added the escape() function to the glob module.
- Issue #17618: Add Base85 and Ascii85 encoding/decoding to the base64 module.