Issue 2701: csv.reader accepts string instead of file object (duck typing gone bad) (original) (raw)

Issue2701

Created on 2008-04-27 02:59 by roysmith, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Messages (3)
msg65871 - (view) Author: Roy Smith (roysmith) Date: 2008-04-27 02:59
If you pass csv.reader() a filename as its first argument: csv.reader('filename') instead of a file object like you're supposed to, you don't get an error. You instead get a reader object which returns the characters which make up the filename. Technically, this is not a bug, since the documentation says, "csvfile can be any object which supports the iterator protocol and returns a string each time its next method is called", and a string meets that definition. Still, this is unexpected behavior, and is almost certainly not what the user intended. It would be useful if a way could be devised to catch this kind of mistake.
msg65884 - (view) Author: Skip Montanaro (skip.montanaro) * (Python triager) Date: 2008-04-27 11:41
It's not a mistake. In fact, the csv unit tests make use of being able to iterate over strings. I don't think this feature is going away.
msg68502 - (view) Author: Facundo Batista (facundobatista) * (Python committer) Date: 2008-06-21 15:48
Skip is right, this is working ok.
History
Date User Action Args
2022-04-11 14:56:33 admin set github: 46953
2008-06-21 15:48:58 facundobatista set status: open -> closednosy: + facundobatistaresolution: not a bugmessages: +
2008-04-27 11:41:51 skip.montanaro set nosy: + skip.montanaromessages: +
2008-04-27 02:59:50 roysmith create