Scanning a file

John J. Lee jjl at pobox.com
Sun Oct 30 17:01:02 EST 2005


aleaxit at yahoo.com (Alex Martelli) writes:
[...]
> 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):
[...]

In general __del__ methods interfere with garbage collection, don't
they?  I guess in the case of file objects this is unlikely to be
problematic (because unlikely to be any reference cycles), but I
thought it might be worth warning people that in general this
debugging strategy might get rather confusing since the __del__ could
actually change the operation of the program by preventing garbage
collection of the objects whose lifetime you're trying to
investigate...


John




More information about the Python-list mailing list