| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 30c5ad2 commit fb099c9
3 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -146,7 +146,7 @@ def _dict_to_list(self, rowdict): | |||
| 146 | 146 | wrong_fields = [k for k in rowdict if k not in self.fieldnames] | |
| 147 | 147 | if wrong_fields: | |
| 148 | 148 | raise ValueError("dict contains fields not in fieldnames: " | |
| 149 | - + ", ".join(wrong_fields)) | ||
| 149 | + + ", ".join([repr(x) for x in wrong_fields])) | ||
| 150 | 150 | return [rowdict.get(key, self.restval) for key in self.fieldnames] | |
| 151 | 151 | ||
| 152 | 152 | def writerow(self, rowdict): | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -570,6 +570,18 @@ def test_write_no_fields(self): | |||
| 570 | 570 | fileobj = StringIO() | |
| 571 | 571 | self.assertRaises(TypeError, csv.DictWriter, fileobj) | |
| 572 | 572 | ||
| 573 | + def test_write_fields_not_in_fieldnames(self): | ||
| 574 | + with TemporaryFile("w+", newline='') as fileobj: | ||
| 575 | + writer = csv.DictWriter(fileobj, fieldnames = ["f1", "f2", "f3"]) | ||
| 576 | + # Of special note is the non-string key (issue 19449) | ||
| 577 | + with self.assertRaises(ValueError) as cx: | ||
| 578 | + writer.writerow({"f4": 10, "f2": "spam", 1: "abc"}) | ||
| 579 | + exception = str(cx.exception) | ||
| 580 | + self.assertIn("fieldnames", exception) | ||
| 581 | + self.assertIn("'f4'", exception) | ||
| 582 | + self.assertNotIn("'f2'", exception) | ||
| 583 | + self.assertIn("1", exception) | ||
| 584 | + | ||
| 573 | 585 | def test_read_dict_fields(self): | |
| 574 | 586 | with TemporaryFile("w+") as fileobj: | |
| 575 | 587 | fileobj.write("1,2,abc\r\n") | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -13,6 +13,9 @@ Core and Builtins | |||
| 13 | 13 | Library | |
| 14 | 14 | ------- | |
| 15 | 15 | ||
| 16 | + - Issue #19449: in csv's writerow, handle non-string keys when generating the | ||
| 17 | + error message that certain keys are not in the 'fieldnames' list. | ||
| 18 | + | ||
| 16 | 19 | - Fix test.support.bind_port() to not cause an error when Python was compiled | |
| 17 | 20 | on a system with SO_REUSEPORT defined in the headers but run on a system | |
| 18 | 21 | with an OS kernel that does not support that reasonably new socket option. | |
| Back | FazBrowse Home | New Git URL |
0 commit comments