Skip to content

Commit 2d7e01a

Browse files
author
Ben Cipollini
committed
Fixing test failures.
1 parent 3d0e362 commit 2d7e01a

File tree

3 files changed

+43
-24
lines changed

3 files changed

+43
-24
lines changed

nibabel/testing/__init__.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
''' Utilities for testing '''
1010
from __future__ import division, print_function
1111

12+
import sys
1213
import warnings
1314
from os.path import dirname, abspath, join as pjoin
1415

@@ -56,6 +57,16 @@ def assert_allclose_safely(a, b, match_nans=True):
5657
assert_true(np.allclose(a, b))
5758

5859

60+
def get_fresh_mod(mod_name=__name__):
61+
# Get this module, with warning registry empty
62+
my_mod = sys.modules[mod_name]
63+
try:
64+
my_mod.__warningregistry__.clear()
65+
except AttributeError:
66+
pass
67+
return my_mod
68+
69+
5970
class clear_and_catch_warnings(warnings.catch_warnings):
6071
""" Context manager that resets warning registry for catching warnings
6172

nibabel/tests/test_checkwarns.py

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,19 @@
33
from __future__ import division, print_function, absolute_import
44

55
from nose.tools import assert_true, assert_equal, assert_raises
6-
from ..testing import (error_warnings, suppress_warnings,
7-
clear_and_catch_warnings)
6+
from ..testing import clear_and_catch_warnings, suppress_warnings
87

98

109
def test_ignore_and_error_warnings():
11-
with clear_and_catch_warnings() as w:
12-
from ..checkwarns import ErrorWarnings, IgnoreWarnings
13-
assert_equal(len(w), 1)
14-
assert_equal(w[0].category, FutureWarning)
10+
with suppress_warnings():
11+
from .. import checkwarns
1512

1613
with clear_and_catch_warnings() as w:
17-
with IgnoreWarnings():
18-
pass
14+
checkwarns.IgnoreWarnings()
1915
assert_equal(len(w), 1)
2016
assert_equal(w[0].category, FutureWarning)
2117

2218
with clear_and_catch_warnings() as w:
23-
with ErrorWarnings():
24-
pass
19+
checkwarns.ErrorWarnings()
2520
assert_equal(len(w), 1)
2621
assert_equal(w[0].category, FutureWarning)

nibabel/tests/test_testing.py

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,14 @@
33
from __future__ import division, print_function, absolute_import
44

55
import sys
6-
from warnings import warn, simplefilter, filters
6+
import warnings
77

88
import numpy as np
99

1010
from nose.tools import assert_true, assert_equal, assert_raises
1111
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)
1314

1415

1516
def 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+
6376
def 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

93106
def 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

102115
def 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

117130
def 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

Comments
 (0)