-
-
Notifications
You must be signed in to change notification settings - Fork 30.9k
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
warnings.catch_warnings fails gracelessly when recording warnings but no warnings are emitted #48031
Comments
This example shows the behavior: from warnings import catch_warnings
def test():
with catch_warnings(True) as w:
assert str(w.message) == "foo", "%r != %r" % (w.message, "foo")
test() This fails with an IndexError from the |
On Thu, Sep 4, 2008 at 3:10 PM, Jean-Paul Calderone
The question is what exception to raise when no warning has been So unless someone comes up with a better suggestion I will change |
The specific exception type isn't that important to me, as long as I can |
I won't be able to get to this until tonight, but assuming no one |
Why wouldn't w.message simply be None? |
There is no specific reason why it would be, although that is an option |
The attached patch has WarningsRecorder raise AttributeError when there And just so you know, JP, make sure to use keyword arguments when |
I hate to make API suggestions this late in the process, but this is the with catch_warnings(True) as w:
assert len(w) > 0, 'No caught warnings'
assert str(w[-1].message) == 'foo', 'blah blah' |
The use of __getattr__ is a historical artifact. Originally there was no While having the attributes of the last warning directly accessible is OK, I'm sold. =) I will work on this today or tomorrow and get the patch |
And I forgot to mention that subclassing list is a new thing I tossed in |
Sounds good. This needs to go into 2.6 and 3.0. And while you're add |
On Sat, Sep 6, 2008 at 11:31 AM, Barry A. Warsaw <report@bugs.python.org> wrote:
Oh, definitely.
Yep. And I will also change the __init__() so that it properly forces |
The new patch ditches the WarningsRecorder class and just returns a list |
The patch mostly looks good. However, all the w[-1] logic looks rather |
On Mon, Sep 8, 2008 at 3:12 PM, Benjamin Peterson
Well, the real question is whether most users will use this for I honest don't fine the [-1] indexing that bad and I had to add all of I will wait to see if Barry has anything to say on the matter since he |
Covered by r66321 in the trunk. Now I just need to merge into 3.0. |
r66322 has the fix in 3.0. |
Reopening this because I disagree with the fix - I would prefer to see That version had perfectly clear semantics when no warning was issued: The attributes of the last warning are cached on the recorder because by *aside from the use of @contextmanager, obviously |
As far as the use cases go, it was moved out of test.test_support to |
With a name like catch_warnings() in the warnings module, it will You'd still have to fix the w.messages attribute to be None if there |
It turns out the warnings.catch_warnings version has re-entrancy issues Python 2.6b3+ (trunk:66143M, Sep 2 2008, 20:04:43)
[GCC 4.2.3 (Ubuntu 4.2.3-2ubuntu7)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import warnings
>>> orig_filters = warnings.filters
>>> cw = warnings.catch_warnings()
>>> with cw:
... warnings.filters = []
... with cw:
... pass
...
>>> warnings.filters is orig_filters
False
>>> warnings.filters
[]
>>> orig_filters
[('ignore', None, <type 'exceptions.PendingDeprecationWarning'>, None,
0), ('ignore', None, <type 'exceptions.ImportWarning'>, None, 0),
('ignore', None, <type 'exceptions.BytesWarning'>, None, 0)] I propose that we just revert to the test.test_support.catch_warnings So on trunk we would revert the following checkins: And on the py3k branch we would revert: *This commit actually appears to have missed the changes to |
Exposing a list seems like a great low-level API to me. There are no For Twisted, we actually would have very little difficulty coming up I understand the concern with introducing changes like this into CPython |
In working on the reversion patch, I just noticed that r66321 also When using a single catch_warning() context to test two or more |
test.test_support *is* a public API (it's even documented). |
There was a discussion on python-dev about using things from the http://mail.python.org/pipermail/python-dev/2008-August/081860.html |
I will put together two patches: one that reverts all the way back to |
Fully reverting the relocation also required reverting r66197 (a belated There was also a change to test_warnings that had to be reverted (it Running tests now for the full reversion patch. The major objection to So as much as I was pushing that option earlier, it looks like it isn't It's past 1 am here, so I'll be working on the other (cleaner) patch The intended end result of that other patch: A warnings.catch_warnings() implementation with the current interface A test_support.catch_warning() implementation which is tailored towards |
Reversion patch attached - it does indeed recreate some nasty (That's 18-20 hours from the time of this post, for anyone trying to |
I can understand the amount of time it might take to revert this; And it took me days to make the transition and related changes, so |
No worries - the only reason I suggested full reversion at all is J.P.'s suggestion (basic functionality in warnings, with each group of |
On Tue, Sep 9, 2008 at 3:13 PM, Nick Coghlan <report@bugs.python.org> wrote:
Adopt how? As in just provide a context manager that copies and |
Not quite that basic for the warnings.catch_warnings() part. I plan to The patch will make it clearer (working on that now). |
Cleanup patch now attached. Details of changes:
Just needs a review and then should be good to go. |
Code looks good. |
Applied to trunk for 2.6 in r66386. |
Blocked merge in the py3k branch since it requires some fiddling to |
On Thu, Sep 11, 2008 at 9:03 AM, Nick Coghlan <report@bugs.python.org> wrote:
The best way to do that is: (trunk) $ svn diff -c mergerevision Lib/test/test_support.py > diff.patch
|
3.0 version of the patch attached (it turned that it wasn't so much |
The 3.0 forward port of r66386 still needs a once over before being Once that is done, then this issue can be closed. |
Applied in r66945. |
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: