[issue13824] argparse.FileType opens a file without excepting resposibility for closing it

2012-01-19 Thread David Layton

New submission from David Layton :

argparse.FileType.__call__ opens the specified file and returns it. This is 
well documented as an anit-idiom in 
http://docs.python.org/howto/doanddont.html#exceptions. 

"...a serious problem — due to implementation details in CPython, the file 
would not be closed when an exception is raised until the exception handler 
finishes; and, worse, in other implementations (e.g., Jython) it might not be 
closed at all regardless of whether or not an exception is raised."

Disregarding the above, handling a file which may or may not have been opened 
depending the users input requires a bit of boilerplate code compared to the 
usual with-open idiom.  

Additionally, there is no way to prevent FileType from clobbering an existing 
file when used with write mode. 

Given these issues and others, it seems to me that the usefulness of FileType 
is outweighed by propensity to encourage bad coding. Perhaps, it would be best 
if FileType (or some replacement) simply checked that the file exists (when 
such a check is appropriate), it can be opened in the specified mode, and, 
curry the call to open (i.e. return lambda: open(string, self._mode, 
self._bufsize))

--
components: Library (Lib)
messages: 151615
nosy: David.Layton, Paolo.Elvati, Stefan.Pfeiffer, bethard, manveru
priority: normal
severity: normal
status: open
title: argparse.FileType opens a file without excepting resposibility for 
closing it
type: behavior
versions: Python 2.7

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



[issue13824] argparse.FileType opens a file and never closes it

2012-02-03 Thread David Layton

David Layton  added the comment:

Eric,

checked that the file exists
But then you’d run into race conditions.  The only sure was to say if a
file can be opened is to open it.

I think you misunderstand me. I am NOT suggesting that you open and
close the file. I am saying that you should not open it in the first place.
If I cannot open the file at the point in my program where I actually want
to open it, then fine, I can decide, in my own code, what to do.

propensity to encourage bad
> coding.
I think the problem is not as bad as you paint it.  A great number of
unclosed file handles may cause problem to some OSes, but that’s it.  On
the plus side, the fact that argparse accepts for its type argument any
callable that can check and convert a string input is simple, clean and
works.  FileType is needed.

  Causing a problem on some OSes and not others is worse than causing a
problem on all OSes as it increases the likelihood of buggy code passing
tests and moving to production.
 I think argparse is wonderful; I just think that by having FileType not
open the file, the number of it's use cases is increased. As it stands now,
I would prefer the just pass the argument as a string argument and handling
the opening myself unless:

   1. I wanted my program to open the file at the very beginning of the
   program (where one traditionally handles arg parsing)
   2. I wanted to exit on the first, and only, attempt to open the file
   3. I really did not care if the file closed properly--which, granted, is
   often the case with tiny scripts

The moment any of these is not true due to a change in requirements, I will
have to refactor my code to use a filename arg. Where as if I start out
with a bog-standard filename and open it myself, I can easily add the
behaviour I want. I just don't see FileType as a big convenience. However,
I do see that changing this would break backwards compatibility and would
not want to see that happen. Perhaps a new FileNameType that does some
basic, perhaps, optional checks would have wider use-cases.

I hope this helps.

        David Layton

On Fri, Feb 3, 2012 at 2:37 PM, Éric Araujo  wrote:

>
> Éric Araujo  added the comment:
>
> s/sure was/sure way/
>
> --
>
> ___
> Python tracker 
> <http://bugs.python.org/issue13824>
> ___
>

--

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