This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Title: csv.reader accepts string instead of file object (duck typing gone bad)
Type: behavior Stage:
Components: Library (Lib) Versions: Python 2.5
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: facundobatista, roysmith, skip.montanaro
Priority: normal Keywords:

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:


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.
Date User Action Args
2022-04-11 14:56:33adminsetgithub: 46953
2008-06-21 15:48:58facundobatistasetstatus: open -> closed
nosy: + facundobatista
resolution: not a bug
messages: + msg68502
2008-04-27 11:41:51skip.montanarosetnosy: + skip.montanaro
messages: + msg65884
2008-04-27 02:59:50roysmithcreate