[issue19449] csv.DictWriter can't handle extra non-string fields

2013-10-30 Thread Tomas Grahn

New submission from Tomas Grahn:

When csv.DictWriter.writerow is fed a dict with extra fieldnames (and 
extrasaction='raise') and any of those extra fieldnames aren't strings, a 
TypeError-exception is thrown. To fix the issue; in csv.py, edit the line:
raise ValueError("dict contains fields not in fieldnames: " + ", 
".join(wrong_fields))

to:
raise ValueError("dict contains fields not in fieldnames: " + ", 
".join(repr(wrong_field) for wrong_field in wrong_fields))

Attached is a patch that fixes the problem (works in both 2.6 and 2.7, I 
haven't tried anything else).

Here is a simple test to demonstrate the problem:

import cStringIO, csv

sio=cStringIO.StringIO()
writer=csv.DictWriter(sio, ["foo", "bar"])
writer.writerow({1:"hello", 2:"world"})

--
files: csv-patch.diff
keywords: patch
messages: 201727
nosy: tomas_grahn
priority: normal
severity: normal
status: open
title: csv.DictWriter can't handle extra non-string fields
type: behavior
versions: Python 2.6, Python 2.7
Added file: http://bugs.python.org/file32425/csv-patch.diff

___
Python tracker 
<http://bugs.python.org/issue19449>
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue19449] csv.DictWriter can't handle extra non-string fields

2013-10-30 Thread Tomas Grahn

Tomas Grahn added the comment:

If non-string field names aren't allowed then shouldn't they be caught at an 
earlier stage, rather then when the user feeds writerow a dict with an 
unexpected key?

But why should the field names have to be strings in the first place? 
Everything else is passed through str before being written anyway...

--

___
Python tracker 
<http://bugs.python.org/issue19449>
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com