This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Weakref callback exceptions should be turned into warnings.
Type: behavior Stage:
Components: Library (Lib) Versions: Python 2.7
process
Status: closed Resolution: rejected
Dependencies: Superseder:
Assigned To: Nosy List: oddthinking, pitrou
Priority: normal Keywords:

Created on 2010-11-04 03:49 by oddthinking, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Messages (3)
msg120375 - (view) Author: Julian (oddthinking) Date: 2010-11-04 03:49
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.)
msg120401 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2010-11-04 13:02
The behaviour of weakrefs here mirrors what is already done for __del__ exceptions. Using warnings would mean that exceptions can get silenced implicitly (for example, if an exception happens twice at the same location) which is not very Pythonic: generally, if you want to silence an exception, you have to do it explicitly through try...except.
msg120402 - (view) Author: Julian (oddthinking) Date: 2010-11-04 13:26
Thank you, Antoine, you make a good point.

In my example, I am suppressing the warning, which, I agree isn't a good idea.

In my real life usage, I was getting an unexpected exception in a callback in code written by another person. I wanted not to suppress the warning, but to turn it into an exception to help trace the cause.

Thinking about this further, (if it was a warning, as I suggest,) I now believe that would raise that exception into the garbage collector which might have some unpredictable consequences. I have rejected my own Issue for this reason.

I maintain that the current solution isn't terribly helpful. Getting this output to stderr is neither helpful to me, in development (i.e. nothing in the error indicated it was related to the weakref functionality) nor my users in production (fortunately the problem was caught before then!)

However, I concede my proposal isn't the right solution.

Thank you again for your comment.
History
Date User Action Args
2022-04-11 14:57:08adminsetgithub: 54515
2010-11-04 13:26:53oddthinkingsetstatus: open -> closed
resolution: rejected
messages: + msg120402
2010-11-04 13:02:31pitrousetnosy: + pitrou
messages: + msg120401
2010-11-04 03:49:55oddthinkingcreate