[issue32547] csv.DictWriter emits strange errors if fieldnames is an iterator

2022-04-01 Thread Sam Ezeh


Change by Sam Ezeh :


--
pull_requests: +30300
stage:  -> patch review
pull_request: https://github.com/python/cpython/pull/32225

___
Python tracker 

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



[issue32547] csv.DictWriter emits strange errors if fieldnames is an iterator

2022-04-01 Thread Sam Ezeh


Change by Sam Ezeh :


Added file: https://bugs.python.org/file50711/sam_ezeh.patch

___
Python tracker 

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



[issue32547] csv.DictWriter emits strange errors if fieldnames is an iterator

2022-04-01 Thread Sam Ezeh


Change by Sam Ezeh :


Removed file: https://bugs.python.org/file50710/sam_ezeh.patch

___
Python tracker 

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



[issue32547] csv.DictWriter emits strange errors if fieldnames is an iterator

2022-04-01 Thread Sam Ezeh


Change by Sam Ezeh :


Added file: https://bugs.python.org/file50710/sam_ezeh.patch

___
Python tracker 

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



[issue32547] csv.DictWriter emits strange errors if fieldnames is an iterator

2022-04-01 Thread Sam Ezeh


Change by Sam Ezeh :


Removed file: https://bugs.python.org/file50709/sam_ezeh.patch

___
Python tracker 

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



[issue32547] csv.DictWriter emits strange errors if fieldnames is an iterator

2022-04-01 Thread Sam Ezeh


Sam Ezeh  added the comment:

I've submitted a patch that introduces code that raises TypeError at 
construction if `fieldnames` is not a sequence

--
keywords: +patch
nosy: +sam_ezeh
Added file: https://bugs.python.org/file50709/sam_ezeh.patch

___
Python tracker 

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



[issue32547] csv.DictWriter emits strange errors if fieldnames is an iterator

2022-03-19 Thread Irit Katriel


Irit Katriel  added the comment:

Perhaps we could raise an exception with a clearer error message when 
fieldnames not a sequence?

--
nosy: +iritkatriel
versions: +Python 3.11 -Python 3.7

___
Python tracker 

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



[issue32547] csv.DictWriter emits strange errors if fieldnames is an iterator

2018-01-13 Thread Raymond Hettinger

Raymond Hettinger  added the comment:

This isn't a bug.  The docs specify that *fieldnames* is a sequence, "The 
fieldnames parameter is a sequence of keys that identify the order in which 
values in the dictionary passed to the writerow() method are written to the 
csvfile."

We could make this a feature request. but as far as I can tell there isn't a 
real use case, nor has one ever arisen in the decade long history of the 
module.  Adding the list(fieldnames) behavior could be done without a backwards 
compatibility issue, but it does incur an overhead that would be paid by all 
existing working code.

--
nosy: +rhettinger
type:  -> enhancement
versions:  -Python 3.4, Python 3.5, Python 3.6, Python 3.8

___
Python tracker 

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



[issue32547] csv.DictWriter emits strange errors if fieldnames is an iterator

2018-01-13 Thread Ben Cummings

New submission from Ben Cummings :

If I pass an iterator to csv.DictWriter as the fieldname field, then DictWriter 
consumes that iterator pretty quickly, emitting strange errors such as the 
following when trying to write the headers.

>>> import csv
>>> fields = iter(["a", "b", "c", "d"])
>>> f = open('test.csv', 'w')
>>> writer = csv.DictWriter(f, fieldnames=fields)
>>> writer.writeheader()
Traceback (most recent call last):
  File "", line 1, in 
  File "/usr/lib/python3.4/csv.py", line 142, in writeheader
self.writerow(header)
  File "/usr/lib/python3.4/csv.py", line 153, in writerow
return self.writer.writerow(self._dict_to_list(rowdict))
  File "/usr/lib/python3.4/csv.py", line 149, in _dict_to_list
+ ", ".join([repr(x) for x in wrong_fields]))
ValueError: dict contains fields not in fieldnames: 'c', 'a'

This is because writeheader and _dict_to_list repeatedly iterate over 
self.fieldnames. It seems like this could be solved by making a list of 
fieldnames in the constructor.

--
components: Library (Lib)
messages: 309904
nosy: bendotc
priority: normal
severity: normal
status: open
title: csv.DictWriter emits strange errors if fieldnames is an iterator
versions: Python 3.4, Python 3.5, Python 3.6, Python 3.7, Python 3.8

___
Python tracker 

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