If a weakref callback raises an exception, weakref writes out some text (to stderr, I think) and ignores it.
I think it would be more appropriate if weakref emitted that text using the Python warning module, to allow it to be better controlled.
Consider this code with two foolish mistakes in it:
---------
import warnings
import weakref
warnings.simplefilter('ignore') # Whoops, ignoring warnings is foolish.
def callback(condemned_object):
raise Exception("Failure") # Whoops, raising an exception in a callback is foolish.
class RandomObject(object):
pass
strong_ref = RandomObject()
wr = weakref.proxy(strong_ref, callback)
print "Removing the only strong reference"
strong_ref = None
# No guarantee that the garbage collector will trigger
# in practice, in CPython, it does.
print "Shutting down now."
---------
When I run this I get:
Removing the only strong reference
Exception Exception: Exception('Failure',) in <function callback at 0x0280A1B0> ignored
Shutting down now.
The exception text is output even though I don't want it to be. To help me debug, I want for the exception text to be manageable (not by ignoring it, like in the example above, but using the other warnings module features.)
|