Skip to content
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

Closed
oddthinking mannequin opened this issue Nov 4, 2010 · 3 comments
Closed

Weakref callback exceptions should be turned into warnings. #54515

oddthinking mannequin opened this issue Nov 4, 2010 · 3 comments
Labels
stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@oddthinking
Copy link
Mannequin

oddthinking mannequin commented Nov 4, 2010

BPO 10306
Nosy @pitrou

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:

assignee = None
closed_at = <Date 2010-11-04.13:26:53.412>
created_at = <Date 2010-11-04.03:49:54.953>
labels = ['type-bug', 'library']
title = 'Weakref callback exceptions should be turned into warnings.'
updated_at = <Date 2010-11-04.13:26:53.410>
user = 'https://bugs.python.org/oddthinking'

bugs.python.org fields:

activity = <Date 2010-11-04.13:26:53.410>
actor = 'oddthinking'
assignee = 'none'
closed = True
closed_date = <Date 2010-11-04.13:26:53.412>
closer = 'oddthinking'
components = ['Library (Lib)']
creation = <Date 2010-11-04.03:49:54.953>
creator = 'oddthinking'
dependencies = []
files = []
hgrepos = []
issue_num = 10306
keywords = []
message_count = 3.0
messages = ['120375', '120401', '120402']
nosy_count = 2.0
nosy_names = ['pitrou', 'oddthinking']
pr_nums = []
priority = 'normal'
resolution = 'rejected'
stage = None
status = 'closed'
superseder = None
type = 'behavior'
url = 'https://bugs.python.org/issue10306'
versions = ['Python 2.7']

@oddthinking
Copy link
Mannequin Author

oddthinking mannequin commented Nov 4, 2010

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

@oddthinking oddthinking mannequin added stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error labels Nov 4, 2010
@pitrou
Copy link
Member

pitrou commented Nov 4, 2010

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.

@oddthinking
Copy link
Mannequin Author

oddthinking mannequin commented Nov 4, 2010

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.

@oddthinking oddthinking mannequin closed this as completed Nov 4, 2010
@ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

1 participant