[Python-Dev] tracemalloc.get_object_traceback() and "unclosed" ResourceWarning

Victor Stinner victor.stinner at gmail.com
Tue Oct 29 15:05:32 CET 2013


2013/10/29 Nick Coghlan <ncoghlan at gmail.com>:
> I was thinking you could turn on warnings->errors and then catch the
> exception, and use that to get hold of the original object that
> triggered the resource warning, and then fed *that* into
> tracemalloc.get_object_traceback(). But, alas, even if we tweaked
> ResourceWarning to make sure that was possible, exceptions from
> __del__ methods are squashed by the interpreter, so you're only left
> with the text output.
>
> Didn't an earlier version of the PEP have the ability to find a
> traceback based on an object's ID rather than the object itself? If
> that capability existed (including for snapshots), then an object id
> printed in an error message could be matched up with the tracemalloc
> output. (ResourceWarning currently only prints the id if the object
> repr includes the id, but we could change that to ensure the object id
> is always shown).

An address is only valid while the object is alive. When an object is
destroyed, its trace is immediatly removed. So it's safer to get the
traceback when the warning is emitted, not before, not after.

> Anyway, I don't think this feature needs to be included in the PEP,
> but it's something we may decide we want to add later.

I don't want to add the ResourceWarning hack to the PEP :-) I'm just
asking for help to find the best solution.

Victor


More information about the Python-Dev mailing list