New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Weakref callback exceptions should be turned into warnings. #54515
Comments
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: 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.) |
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. |
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. |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: