33from __future__ import division , print_function , absolute_import
44
55import sys
6- from warnings import warn , simplefilter , filters
6+ import warnings
77
88import numpy as np
99
1010from nose .tools import assert_true , assert_equal , assert_raises
1111from ..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 )
1314
1415
1516def assert_warn_len_equal (mod , n_in_context ):
@@ -60,9 +61,21 @@ def test_assert_allclose_safely():
6061 assert_allclose_safely ([], [])
6162
6263
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+
6376def test_clear_and_catch_warnings ():
6477 # Initial state of module, no warnings
65- my_mod = _get_fresh_mod ( )
78+ my_mod = get_fresh_mod ( __name__ )
6679 assert_equal (getattr (my_mod , '__warningregistry__' , {}), {})
6780 with clear_and_catch_warnings (modules = [my_mod ]):
6881 warnings .simplefilter ('ignore' )
@@ -92,7 +105,7 @@ class my_cacw(clear_and_catch_warnings):
92105
93106def test_clear_and_catch_warnings_inherit ():
94107 # Test can subclass and add default modules
95- my_mod = _get_fresh_mod ( )
108+ my_mod = get_fresh_mod ( __name__ )
96109 with my_cacw ():
97110 warnings .simplefilter ('ignore' )
98111 warnings .warn ('Some warning' )
@@ -101,12 +114,12 @@ def test_clear_and_catch_warnings_inherit():
101114
102115def test_warn_error ():
103116 # Check warning error context manager
104- n_warns = len (filters )
117+ n_warns = len (warnings . filters )
105118 with error_warnings ():
106- assert_raises (UserWarning , warn , 'A test' )
119+ assert_raises (UserWarning , warnings . warn , 'A test' )
107120 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 ))
110123 # Check other errors are propagated
111124 def f ():
112125 with error_warnings ():
@@ -116,14 +129,14 @@ def f():
116129
117130def test_warn_ignore ():
118131 # Check warning ignore context manager
119- n_warns = len (filters )
132+ n_warns = len (warnings . filters )
120133 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 )
123136 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 ))
127140 # Check other errors are propagated
128141 def f ():
129142 with suppress_warnings ():
0 commit comments