Skip to content

Commit

Permalink
Due to python 2.7 bug and odd re-scoping of filterwarnings in pytest …
Browse files Browse the repository at this point in the history
…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
  • Loading branch information
jobovy committed Sep 15, 2018
1 parent 6db7b1e commit 9c39404
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 1 deletion.
19 changes: 19 additions & 0 deletions 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)

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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()

5 changes: 5 additions & 0 deletions 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)

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
12 changes: 11 additions & 1 deletion tests/test_orbit.py
Expand Up @@ -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, \
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand All @@ -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:
Expand Down

0 comments on commit 9c39404

Please sign in to comment.