From dce7f20e95e6bd3fc07517c0b2daf3942a34ddf7 Mon Sep 17 00:00:00 2001
From: Charles Harris <charlesr.harris@gmail.com>
Date: Wed, 14 Mar 2018 12:52:26 -0600
Subject: [PATCH] MAINT: Fix test_utils.py for Python 3.7.

The contents of the module warnings registries was made more module
specific in Python 3.7 and consequently the tests of the context
managers clear_and_catch_warnings and suppress_warnings need updating.
---
 numpy/testing/tests/test_utils.py | 43 +++++++++++++++++++++----------
 1 file changed, 29 insertions(+), 14 deletions(-)

diff --git a/numpy/testing/tests/test_utils.py b/numpy/testing/tests/test_utils.py
index a97b627f9..33b3555b0 100644
--- a/numpy/testing/tests/test_utils.py
+++ b/numpy/testing/tests/test_utils.py
@@ -1114,18 +1114,28 @@ class TestStringEqual(unittest.TestCase):
                           lambda: assert_string_equal("foo", "hello"))
 
 
-def assert_warn_len_equal(mod, n_in_context, py3_n_in_context=None):
+def assert_warn_len_equal(mod, n_in_context, py34=None, py37=None):
     mod_warns = mod.__warningregistry__
+    num_warns = len(mod_warns)
     # Python 3.4 appears to clear any pre-existing warnings of the same type,
     # when raising warnings inside a catch_warnings block. So, there is a
     # warning generated by the tests within the context manager, but no
     # previous warnings.
     if 'version' in mod_warns:
-        if py3_n_in_context is None:
-            py3_n_in_context = n_in_context
-        assert_equal(len(mod_warns) - 1, py3_n_in_context)
-    else:
-        assert_equal(len(mod_warns), n_in_context)
+        # Python 3 adds a 'version' entry to the registry,
+        # do not count it.
+        num_warns -= 1
+
+        # Behavior of warnings is Python version dependent. Adjust the
+        # expected result to compensate. In particular, Python 3.7 does
+        # not make an entry for ignored warnings.
+        if sys.version_info[:2] >= (3, 7):
+            if py37 is not None:
+                n_in_context = py37
+        elif sys.version_info[:2] >= (3, 4):
+            if py34 is not None:
+                n_in_context = py34
+    assert_equal(num_warns, n_in_context)
 
 
 def _get_fresh_mod():
@@ -1134,6 +1144,8 @@ def _get_fresh_mod():
     try:
         my_mod.__warningregistry__.clear()
     except AttributeError:
+        # will not have a __warningregistry__ unless warning has been
+        # raised in the module at some point
         pass
     return my_mod
 
@@ -1147,21 +1159,23 @@ def test_clear_and_catch_warnings():
         warnings.warn('Some warning')
     assert_equal(my_mod.__warningregistry__, {})
     # Without specified modules, don't clear warnings during context
+    # Python 3.7 catch_warnings doesn't make an entry for 'ignore'.
     with clear_and_catch_warnings():
         warnings.simplefilter('ignore')
         warnings.warn('Some warning')
-    assert_warn_len_equal(my_mod, 1)
+    assert_warn_len_equal(my_mod, 1, py37=0)
     # Confirm that specifying module keeps old warning, does not add new
     with clear_and_catch_warnings(modules=[my_mod]):
         warnings.simplefilter('ignore')
         warnings.warn('Another warning')
-    assert_warn_len_equal(my_mod, 1)
+    assert_warn_len_equal(my_mod, 1, py37=0)
     # Another warning, no module spec does add to warnings dict, except on
     # Python 3.4 (see comments in `assert_warn_len_equal`)
+    # Python 3.7 catch_warnings doesn't make an entry for 'ignore'.
     with clear_and_catch_warnings():
         warnings.simplefilter('ignore')
         warnings.warn('Another warning')
-    assert_warn_len_equal(my_mod, 2, 1)
+    assert_warn_len_equal(my_mod, 2, py34=1, py37=0)
 
 
 def test_suppress_warnings_module():
@@ -1178,6 +1192,7 @@ def test_suppress_warnings_module():
         np.apply_along_axis(warn, 0, [0])
 
     # Test module based warning suppression:
+    assert_warn_len_equal(my_mod, 0)
     with suppress_warnings() as sup:
         sup.record(UserWarning)
         # suppress warning from other module (may have .pyc ending),
@@ -1189,8 +1204,7 @@ def test_suppress_warnings_module():
     # got filtered)
     assert_(len(sup.log) == 1)
     assert_(sup.log[0].message.args[0] == "Some warning")
-
-    assert_warn_len_equal(my_mod, 0)
+    assert_warn_len_equal(my_mod, 0, py37=0)
     sup = suppress_warnings()
     # Will have to be changed if apply_along_axis is moved:
     sup.filter(module=my_mod)
@@ -1204,11 +1218,11 @@ def test_suppress_warnings_module():
     assert_warn_len_equal(my_mod, 0)
 
     # Without specified modules, don't clear warnings during context
+    # Python 3.7 does not add ignored warnings.
     with suppress_warnings():
         warnings.simplefilter('ignore')
         warnings.warn('Some warning')
-    assert_warn_len_equal(my_mod, 1)
-
+    assert_warn_len_equal(my_mod, 1, py37=0)
 
 def test_suppress_warnings_type():
     # Initial state of module, no warnings
@@ -1232,10 +1246,11 @@ def test_suppress_warnings_type():
     assert_warn_len_equal(my_mod, 0)
 
     # Without specified modules, don't clear warnings during context
+    # Python 3.7 does not add ignored warnings.
     with suppress_warnings():
         warnings.simplefilter('ignore')
         warnings.warn('Some warning')
-    assert_warn_len_equal(my_mod, 1)
+    assert_warn_len_equal(my_mod, 1, py37=0)
 
 
 def test_suppress_warnings_decorate_no_record():
-- 
2.18.0

