[Python-Dev] Resource leaks warnings

Brett Cannon brett at python.org
Wed Sep 29 18:31:44 CEST 2010

On Wed, Sep 29, 2010 at 05:42, Antoine Pitrou <solipsis at pitrou.net> wrote:
> Le mercredi 29 septembre 2010 à 07:27 -0500, Benjamin Peterson a écrit :
>> >
>> > I would like to piggy-back on this discussion to suggest further
>> > warnings (either by default, or switchable).
>> >
>> > One feature I've often considered would be to add a warning in FileIO
>> > and socket dealloc if these objects haven't been closed explicitly. In
>> > most situations, relying on garbage collection to shutdown OS resources
>> > (here, file descriptors) is something we like to discourage.
>> > Furthermore, it can produce real bugs, especially under Windows when
>> > coupled with refererence cycles created by traceback objects (the
>> > random test_tarfile failures on the Windows buildbots were a symptom of
>> > that; their cause would have been obvious with such warnings).
>> >
>> > What do you think?
>> It seems like a slippery slope. Sometimes you really don't care like
>> when you're just hacking together a quick script.
> Isn't the "with" statement appropriate in these cases?

Yes, which is why I suspect people saying they don't bother have been
programming Python for a while and are not in the habit yet of using a
'with' statement. The amount of extra typing compared to inlining a
call is minimal.

> My assumption is/was that the benefit of warning against leaks in real
> applications (or even - sigh - the standard library) would outweigh the
> inconvenience when hacking together a quick script.

Does everyone here run all their code under py-debug? If not then I
say switch it on when py-debug is on so that we at least detect the
leaks in the stdlib without having to think about it.

> But if it doesn't, what about enabling it with a command-line switch?

Sure, but I say always turn it on under py-debug.

More information about the Python-Dev mailing list