[issue10093] Warn when files are not explicitly closed

Antoine Pitrou report at bugs.python.org
Fri Oct 29 13:52:38 CEST 2010


Antoine Pitrou <pitrou at free.fr> added the comment:

> Whether you write an application with automatic closing of
> the file/socket at garbage collection time in mind, or you explicitly
> close the file/socket, the timing is the same.

No, because objects can be kept alive through tracebacks (or reference
cycles).

> I don't follow you. Where's the difference between writing:
> 
> s.close()
> or
> s = None
> 
> for an open socket s ?

The difference is when s is still referenced elsewhere.
Also, the intent of the former is clear while the latter is deliberately
obscure (while not saving any significant amount of typing).

> The for-loop file iterator support was explicitly added to make
> writing:
> 
> for line in open(filename):
>     print line
> 
> possible.

So what?

> At least for Linux, that's not hard and I doubt it is for other OSes.
> 
> 4
> 
> On other Unixes, you can simply use fcntl() to scan all possible FDs
> for open FDs.
> 
> On Windows you can use one of these functions for the same effect:
> http://msdn.microsoft.com/en-us/library/kdfaxaay(v=VS.90).aspx

Until you post some code I won't understand what you are talking about.

----------

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue10093>
_______________________________________


More information about the Python-bugs-list mailing list