From 9c394047413c60f1bb4f21ca000418053e976734 Mon Sep 17 00:00:00 2001 From: Jo Bovy Date: Sat, 15 Sep 2018 17:05:39 -0400 Subject: [PATCH] Due to python 2.7 bug and odd re-scoping of filterwarnings in pytest 3.8.0, need to explicitly clean the warningregistry before switching to always showing warnings for some warnings tests, otherwise warnings already shown are still ignored --- tests/test_actionAngle.py | 19 +++++++++++++++++++ tests/test_actionAngleTorus.py | 5 +++++ tests/test_orbit.py | 12 +++++++++++- 3 files changed, 35 insertions(+), 1 deletion(-) diff --git a/tests/test_actionAngle.py b/tests/test_actionAngle.py index d49e23fd3..f90548633 100644 --- a/tests/test_actionAngle.py +++ b/tests/test_actionAngle.py @@ -1,10 +1,12 @@ from __future__ import print_function, division import os +import sys import pytest import warnings import numpy from galpy.util import galpyWarning _TRAVIS= bool(os.getenv('TRAVIS')) +PY2= sys.version < '3' # Print all galpyWarnings always for tests of warnings warnings.simplefilter("always",galpyWarning) @@ -2611,6 +2613,7 @@ def test_MWPotential_warning_adiabatic(): actionAngleAdiabaticGrid from galpy.potential import MWPotential with warnings.catch_warnings(record=True) as w: + if PY2: reset_warning_registry('galpy') warnings.simplefilter("always",galpyWarning) aAA= actionAngleAdiabatic(pot=MWPotential,gamma=1.) # Should raise warning bc of MWPotential, might raise others @@ -2638,6 +2641,7 @@ def test_MWPotential_warning_staeckel(): actionAngleStaeckelGrid from galpy.potential import MWPotential with warnings.catch_warnings(record=True) as w: + if PY2: reset_warning_registry('galpy') warnings.simplefilter("always",galpyWarning) aAA= actionAngleStaeckel(pot=MWPotential,delta=0.5) # Should raise warning bc of MWPotential, might raise others @@ -2664,6 +2668,7 @@ def test_MWPotential_warning_isochroneapprox(): from galpy.actionAngle import actionAngleIsochroneApprox from galpy.potential import MWPotential with warnings.catch_warnings(record=True) as w: + if PY2: reset_warning_registry('galpy') warnings.simplefilter("always",galpyWarning) aAA= actionAngleIsochroneApprox(pot=MWPotential,b=1.) # Should raise warning bc of MWPotential, might raise others @@ -2788,3 +2793,17 @@ def check_actionAngle_conserved_EccZmaxRperiRap(aA,obs,pot,tole,tolzmax, assert numpy.amax(numpy.fabs(raps/numpy.mean(raps)-1)) < 10.**tolrap, 'Rap conservation fails at %g%%' % (100.*numpy.amax(numpy.fabs(raps/numpy.mean(raps)-1))) return None +# Python 2 bug: setting simplefilter to 'always' still does not display +# warnings that were already displayed using 'once' or 'default', so some +# warnings tests fail; need to reset the registry +# Has become an issue at pytest 3.8.0, which seems to have changed the scope of +# filterwarnings (global one at the start is ignored) +def reset_warning_registry(pattern=".*"): + "clear warning registry for all match modules" + import re + import sys + key = "__warningregistry__" + for mod in sys.modules.values(): + if hasattr(mod, key) and re.match(pattern, mod.__name__): + getattr(mod, key).clear() + diff --git a/tests/test_actionAngleTorus.py b/tests/test_actionAngleTorus.py index 51492bd65..b87537053 100644 --- a/tests/test_actionAngleTorus.py +++ b/tests/test_actionAngleTorus.py @@ -1,10 +1,13 @@ from __future__ import print_function, division import os +import sys import pytest import warnings import numpy from galpy.util import galpyWarning +from test_actionAngle import reset_warning_registry _TRAVIS= bool(os.getenv('TRAVIS')) +PY2= sys.version < '3' # Print all galpyWarnings always for tests of warnings warnings.simplefilter("always",galpyWarning) @@ -513,6 +516,7 @@ def test_actionAngleTorus_AutoFitWarning(): #Turn warnings into errors to test for them import warnings with warnings.catch_warnings(record=True) as w: + if PY2: reset_warning_registry('galpy') warnings.simplefilter("always",galpyWarning) aAT(jr,jp,jz,ar,ap,az) # Should raise warning bc of Autofit, might raise others @@ -563,6 +567,7 @@ def test_MWPotential_warning_torus(): # Test that using MWPotential throws a warning, see #229 from galpy.actionAngle import actionAngleTorus from galpy.potential import MWPotential + if PY2: reset_warning_registry('galpy') warnings.simplefilter("error",galpyWarning) try: aAA= actionAngleTorus(pot=MWPotential) diff --git a/tests/test_orbit.py b/tests/test_orbit.py index 8acdc4bc1..064b40bdd 100644 --- a/tests/test_orbit.py +++ b/tests/test_orbit.py @@ -11,10 +11,12 @@ import pytest import numpy import astropy +PY2= sys.version < '3' _APY3= astropy.__version__ > '3' from galpy import potential from galpy.potential.Potential import _check_c from galpy.util import galpyWarning +from test_actionAngle import reset_warning_registry from test_potential import testplanarMWPotential, testMWPotential, \ testlinearMWPotential, \ mockFlatEllipticalDiskPotential, \ @@ -68,7 +70,7 @@ _QUICKTEST= True #Run a more limited set of tests else: _QUICKTEST= True #Also do this for Travis, bc otherwise it takes too long -_NOLONGINTEGRATIONS= False +_NOLONGINTEGRATIONS= True # Don't show all warnings, to reduce log output warnings.simplefilter("always",galpyWarning) @@ -3025,6 +3027,8 @@ def test_MWPotential_warning(): ts= numpy.linspace(0.,100.,1001) o= setup_orbit_energy(potential.MWPotential,axi=False) with pytest.warns(None) as record: + if PY2: reset_warning_registry('galpy') + warnings.simplefilter("always",galpyWarning) o.integrate(ts,potential.MWPotential) # Should raise warning bc of MWPotential, might raise others raisedWarning= False @@ -3808,6 +3812,8 @@ def test_orbitint_pythonfallback(): for orb in [Orbit([1.,0.1,1.1,0.1,0.,1.]),Orbit([1.,0.1,1.1,0.1,0.]), Orbit([1.,0.1,1.1,1.]),Orbit([1.,0.1,1.1])]: with pytest.warns(None) as record: + if PY2: reset_warning_registry('galpy') + warnings.simplefilter("always",galpyWarning) #Test w/ dopr54_c orb.integrate(ts,bp, method='dopr54_c') raisedWarning= False @@ -4694,6 +4700,8 @@ def check_radecetc_roWarning(o,funcName): # Convenience function to check whether the ro-needs-to-be-specified # warning is sounded with pytest.warns(None) as record: + if PY2: reset_warning_registry('galpy') + warnings.simplefilter("always",galpyWarning) getattr(o,funcName)() raisedWarning= False for rec in record: @@ -4706,6 +4714,8 @@ def check_radecetc_voWarning(o,funcName): # Convenience function to check whether the vo-needs-to-be-specified # warning is sounded with pytest.warns(None) as record: + if PY2: reset_warning_registry('galpy') + warnings.simplefilter("always",galpyWarning) getattr(o,funcName)() raisedWarning= False for rec in record: