I want to have a dict mapping from active frame (FrameType) objects to some data. Active meaning that it is in the current execution stack trace.
However, holding a reference to the frame object is not a good idea because that would also keep inactive frames and that will blow up the memory very soon because of all the references to all the local variables.
The natural solution would be to use a weakref to the frame objects, or more specifically WeakKeyDictionary.
However, currently it is not possible to create a weakref to a frame object:
import weakref
import sys
f = sys._getframe()
weakref.ref(f)
yields
TypeError: cannot create weak reference to 'frame' object
I assume this is a CPython implementation detail?
So, what are the options?
I could maybe anyway create references but try to clean them up as soon as possible when they are not active anymore. How do I actually check if a frame object is active?