3
3
from __future__ import division , print_function , absolute_import
4
4
5
5
import sys
6
- from warnings import warn , simplefilter , filters
6
+ import warnings
7
7
8
8
import numpy as np
9
9
10
10
from nose .tools import assert_true , assert_equal , assert_raises
11
11
from ..testing import (error_warnings , suppress_warnings ,
12
- clear_and_catch_warnings , assert_allclose_safely )
12
+ clear_and_catch_warnings , assert_allclose_safely ,
13
+ get_fresh_mod )
13
14
14
15
15
16
def assert_warn_len_equal (mod , n_in_context ):
@@ -60,9 +61,21 @@ def test_assert_allclose_safely():
60
61
assert_allclose_safely ([], [])
61
62
62
63
64
+ def assert_warn_len_equal (mod , n_in_context ):
65
+ mod_warns = mod .__warningregistry__
66
+ # Python 3.4 appears to clear any pre-existing warnings of the same type,
67
+ # when raising warnings inside a catch_warnings block. So, there is a
68
+ # warning generated by the tests within the context manager, but no
69
+ # previous warnings.
70
+ if 'version' in mod_warns :
71
+ assert_equal (len (mod_warns ), 2 ) # including 'version'
72
+ else :
73
+ assert_equal (len (mod_warns ), n_in_context )
74
+
75
+
63
76
def test_clear_and_catch_warnings ():
64
77
# Initial state of module, no warnings
65
- my_mod = _get_fresh_mod ( )
78
+ my_mod = get_fresh_mod ( __name__ )
66
79
assert_equal (getattr (my_mod , '__warningregistry__' , {}), {})
67
80
with clear_and_catch_warnings (modules = [my_mod ]):
68
81
warnings .simplefilter ('ignore' )
@@ -92,7 +105,7 @@ class my_cacw(clear_and_catch_warnings):
92
105
93
106
def test_clear_and_catch_warnings_inherit ():
94
107
# Test can subclass and add default modules
95
- my_mod = _get_fresh_mod ( )
108
+ my_mod = get_fresh_mod ( __name__ )
96
109
with my_cacw ():
97
110
warnings .simplefilter ('ignore' )
98
111
warnings .warn ('Some warning' )
@@ -101,12 +114,12 @@ def test_clear_and_catch_warnings_inherit():
101
114
102
115
def test_warn_error ():
103
116
# Check warning error context manager
104
- n_warns = len (filters )
117
+ n_warns = len (warnings . filters )
105
118
with error_warnings ():
106
- assert_raises (UserWarning , warn , 'A test' )
119
+ assert_raises (UserWarning , warnings . warn , 'A test' )
107
120
with error_warnings () as w : # w not used for anything
108
- assert_raises (UserWarning , warn , 'A test' )
109
- assert_equal (n_warns , len (filters ))
121
+ assert_raises (UserWarning , warnings . warn , 'A test' )
122
+ assert_equal (n_warns , len (warnings . filters ))
110
123
# Check other errors are propagated
111
124
def f ():
112
125
with error_warnings ():
@@ -116,14 +129,14 @@ def f():
116
129
117
130
def test_warn_ignore ():
118
131
# Check warning ignore context manager
119
- n_warns = len (filters )
132
+ n_warns = len (warnings . filters )
120
133
with suppress_warnings ():
121
- warn ('Here is a warning, you will not see it' )
122
- warn ('Nor this one' , DeprecationWarning )
134
+ warnings . warn ('Here is a warning, you will not see it' )
135
+ warnings . warn ('Nor this one' , DeprecationWarning )
123
136
with suppress_warnings () as w : # w not used
124
- warn ('Here is a warning, you will not see it' )
125
- warn ('Nor this one' , DeprecationWarning )
126
- assert_equal (n_warns , len (filters ))
137
+ warnings . warn ('Here is a warning, you will not see it' )
138
+ warnings . warn ('Nor this one' , DeprecationWarning )
139
+ assert_equal (n_warns , len (warnings . filters ))
127
140
# Check other errors are propagated
128
141
def f ():
129
142
with suppress_warnings ():
0 commit comments