]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Patch #1635454: the csv.DictWriter class now includes the offending
authorGeorg Brandl <georg@python.org>
Tue, 13 Mar 2007 09:32:11 +0000 (09:32 +0000)
committerGeorg Brandl <georg@python.org>
Tue, 13 Mar 2007 09:32:11 +0000 (09:32 +0000)
field names in its exception message if you try to write a record with
a dictionary containing fields not in the CSV field names list.

Lib/csv.py
Misc/NEWS

index f213854783eb67f1483e3abeeb94c70afefa8193..8c6b7401271ea5231645ed673878791a3060d9ae 100644 (file)
@@ -115,9 +115,10 @@ class DictWriter:
 
     def _dict_to_list(self, rowdict):
         if self.extrasaction == "raise":
-            for k in rowdict.keys():
-                if k not in self.fieldnames:
-                    raise ValueError, "dict contains fields not in fieldnames"
+            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))
         return [rowdict.get(key, self.restval) for key in self.fieldnames]
 
     def writerow(self, rowdict):
index 79d242b03a7be9791e3393394b32c14aa05beb4c..33f6d9a09c366c5de04c3343ae3b59f6e68a57a9 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -168,6 +168,10 @@ Core and builtins
 Library
 -------
 
+- Patch #1635454: the csv.DictWriter class now includes the offending
+  field names in its exception message if you try to write a record with
+  a dictionary containing fields not in the CSV field names list.
+
 - Patch #1668100: urllib2 now correctly raises URLError instead of
   OSError if accessing a local file via the file:// protocol fails.