[New-bugs-announce] [issue10306] Weakref callback exceptions should be turned into warnings.

Julian report at bugs.python.org
Thu Nov 4 04:49:56 CET 2010

New submission from Julian <python_org at somethinkodd.com>:

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):

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.)

components: Library (Lib)
messages: 120375
nosy: oddthinking
priority: normal
severity: normal
status: open
title: Weakref callback exceptions should be turned into warnings.
type: behavior
versions: Python 2.7

Python tracker <report at bugs.python.org>

More information about the New-bugs-announce mailing list