Paul Watson <[EMAIL PROTECTED]> wrote: > "Alex Martelli" <[EMAIL PROTECTED]> wrote in message > news:[EMAIL PROTECTED] > > > In today's implementations of Classic Python, yes. In other equally > > valid implementations of the language, such as Jython, IronPython, or, > > for all we know, some future implementation of Classic, that may well > > not be the case. Many, quite reasonably, dislike relying on a specific > > implementation's peculiarities, and prefer to write code that relies > > only on what the _language_ specs guarantee. > > How could I identify when Python code does not close files and depends on > the runtime to take care of this? I want to know that the code will work > well under other Python implementations and future implementations which may > not have this provided.
Then you should use try/finally (to have your code run correctly in all of today's implementations; Python 2.5 will have a 'with' statement to offer nicer syntax sugar for that, but it will be a while before all the implementations get around to adding it). If you're trying to test your code to ensure it explicitly closes all files, you could (from within your tests) rebind built-ins 'file' and 'open' to be a class wrapping the real thing, and adding a flag to remember if the file is open; at __del__ time it would warn if the file had not been explicitly closed. E.g. (untested code): import __builtin__ import warnings _f = __builtin__.file class testing_file(_f): def __init__(self, *a, **k): _f.__init__(self, *a, **k) self._opened = True def close(self): _f.close(self) self._opened = False def __del__(self): if self._opened: warnings.warn(...) self.close() __builtin__.file = __builtin__.open = testing_file Alex -- http://mail.python.org/mailman/listinfo/python-list