[Python-bugs-list] Bad default value to constructor in Lib/fileinput.py (PR#280)

guido@python.org guido@python.org
Fri, 7 Apr 2000 09:18:08 -0400 (EDT)


> Full_Name: Lawrence Kesteloot
> Version: 1.5.2
> OS: Linux
> Submission from: dynamic37.pm02.sf3d.best.com (209.24.234.101)
> 
> In the Lib/fileinput.py module, the default value for the list
> of files is (), an empty tuple.  This means that the function
> cannot distinguish between being passed nothing (meaning use
> sys.argv) and an empty list.  If the function is passed an empty
> list, it should use stdin, not revert to sys.argv!  This use
> of fileinput may have nothing to do with sys.argv, and it's
> extremely weird to pass an empty list and find that sys.argv
> is being used.
> 
> For example, I may want to have two parameters followed by
> a possibly-empty list of files.  (Empty would be stdin.)
> 
>     foo1 = sys.argv[1]
>     foo2 = sys.argv[2]
>     for line in fileinput.input(argv[3:])
>         ...
> 
> But in the above code, if no files are specified, then the
> first two arguments will be used for filenames.  This is
> really bad and never what the user wants.
> 
> Instead of using () as the default value, use None.  Replacing
> the top of the constructor with this works for me:
> 
>     if type(files) == type(''):
>         files = (files,)
>     else:
>         if files is None:
>             files = tuple(sys.argv[1:])
>         else:
>             files = tuple(files)
>         if not files:
>             files = ('-',)
>     print files
> 
> And change the default values of "files" in both the
> constructor and the global input() function to None.
> 
> I suspect that this is a safe change, because I can't
> imagine someone actively passing in an empty list when
> they want sys.argv used.

Very good!  This also fixes a bug in the current test harness (for
which a much less elegant fix was just submitted).

--Guido van Rossum (home page: http://www.python.org/~guido/)