Skip to content

Commit ac0944b

Browse files
authored
Merge pull request #133 from bnavigator/fix-warnings
Fix warnings capture
2 parents 10d667c + af98e9f commit ac0944b

File tree

3 files changed

+76
-53
lines changed

3 files changed

+76
-53
lines changed

slycot/CMakeLists.txt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,8 +117,9 @@ configure_file(version.py.in version.py @ONLY)
117117

118118
set(PYSOURCE
119119

120-
__init__.py analysis.py examples.py math.py synthesis.py
121-
transform.py ${CMAKE_CURRENT_BINARY_DIR}/version.py)
120+
__init__.py examples.py exceptions.py
121+
analysis.py math.py synthesis.py transform.py
122+
${CMAKE_CURRENT_BINARY_DIR}/version.py)
122123

123124
set(SLYCOT_MODULE "_wrapper")
124125
find_package(PythonExtensions REQUIRED)

slycot/tests/test_examples.py

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,27 @@
1313
examplefunctions = [fun for (fname, fun) in getmembers(examples)
1414
if isfunction(fun) and "_example" in fname]
1515

16+
# Exempt certain functions to produce warnings with attributes (iwarn, info)
17+
#
18+
# Failed to compute beta(A) within the specified tolerance
19+
examples.ab13fd_example.allow_iwarninfo = [(None, 1)]
20+
21+
22+
def check_warn(recwarn, examplefun):
23+
"""Returns True if a warning occurs that is not exempt"""
24+
for w in recwarn:
25+
try:
26+
if (w.message.iwarn, w.message.info) in examplefun.allow_iwarninfo:
27+
continue
28+
except AttributeError:
29+
pass
30+
return True
31+
1632

1733
@pytest.mark.parametrize('examplefun', examplefunctions)
34+
#ignore numpy ABI change warnings https://github.com/numpy/numpy/pull/432
35+
@pytest.mark.filterwarnings("ignore:numpy.dtype size changed")
36+
@pytest.mark.filterwarnings("ignore:numpy.ufunc size changed")
1837
def test_example(examplefun, capsys, recwarn):
1938
"""
2039
Test the examples.
@@ -24,6 +43,18 @@ def test_example(examplefun, capsys, recwarn):
2443
"""
2544
examplefun()
2645
captured = capsys.readouterr()
27-
assert len(captured.out) > 0
28-
assert not captured.err
29-
assert not recwarn
46+
47+
# fail for first in order
48+
failconditions = [
49+
((not len(captured.out) > 0), "Example {} did not print any results\n"),
50+
(captured.err, "Example {} wrote to stderr\n"),
51+
(check_warn(recwarn, examplefun), "Example {} produced a warning.\n")]
52+
for failed, msgfmt in failconditions:
53+
if failed:
54+
pytest.fail(msgfmt.format(examplefun.__name__) +
55+
"Captured output:\n{}\n"
56+
"Captured stderr:\n{}\n"
57+
"Captured warnings:\n{}\n"
58+
"".format(captured.out,
59+
captured.err,
60+
[w.message for w in recwarn]))

slycot/tests/test_mc.py

Lines changed: 39 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -2,53 +2,44 @@
22
# test_mc.py - test suite for polynomial and rational function manipulation
33
# bnavigator <[email protected]>, Aug 2019
44

5-
import unittest
6-
import warnings
5+
import pytest
6+
import re
77

88
from slycot import mc01td
9-
10-
11-
class test_mc(unittest.TestCase):
12-
13-
def test_mc01td(self):
14-
""" test_mc01td: doc example
15-
data from http://slicot.org/objects/software/shared/doc/MC01TD.html
16-
"""
17-
(dp, stable, nz) = mc01td('C', 4, [2, 0, 1, -1, 1])
18-
self.assertEqual(dp, 4)
19-
self.assertEqual(stable, 0)
20-
self.assertEqual(nz, 2)
21-
22-
def test_mc01td_D(self):
23-
""" test_mc01td_D: test discrete option """
24-
(dp, stable, nz) = mc01td('D', 3, [1, 2, 3, 4])
25-
self.assertEqual(dp, 3)
26-
self.assertEqual(stable, 1)
27-
self.assertEqual(nz, 0)
28-
(dp, stable, nz) = mc01td('D', 3, [4, 3, 2, 1])
29-
self.assertEqual(dp, 3)
30-
self.assertEqual(stable, 0)
31-
self.assertEqual(nz, 3)
32-
33-
def test_mc01td_warnings(self):
34-
""" test_mc01td_warnings: Test warnings """
35-
T = [([0, 0], "\n"
36-
"Entry ``P(x)`` is the zero polynomial."),
37-
([0, 1], "\n"
38-
"The polynomial ``P(x)`` is most probably unstable,\n"
39-
"although it may be stable with one or more zeros\n"
40-
"very close to the imaginary axis.\n"
41-
"The number of unstable zeros (NZ) is not determined."),
42-
([1, 0], "\n"
43-
"The degree of the polynomial ``P(x)`` has been\n"
44-
"reduced to ``(DB - 1)`` because\n"
45-
"``P(DB+1-j) = 0.0`` on entry\n"
46-
"for ``j = 0, 1,..., k-1`` and ``P(DB+1-k) <> 0.0``.")]
47-
for P, m in T:
48-
with warnings.catch_warnings(record=True) as w:
49-
(dp, stable, nz) = mc01td('C', len(P)-1, P)
50-
self.assertEqual(str(w[0].message), m)
51-
52-
53-
if __name__ == "__main__":
54-
unittest.main()
9+
from slycot.exceptions import SlycotResultWarning
10+
11+
12+
def test_mc01td():
13+
""" test_mc01td: doc example
14+
data from http://slicot.org/objects/software/shared/doc/MC01TD.html
15+
"""
16+
(dp, stable, nz) = mc01td('C', 4, [2, 0, 1, -1, 1])
17+
assert dp == 4
18+
assert stable == 0
19+
assert nz == 2
20+
21+
def test_mc01td_D():
22+
""" test_mc01td_D: test discrete option """
23+
(dp, stable, nz) = mc01td('D', 3, [1, 2, 3, 4])
24+
assert dp == 3
25+
assert stable == 1
26+
assert nz == 0
27+
(dp, stable, nz) = mc01td('D', 3, [4, 3, 2, 1])
28+
assert dp == 3
29+
assert stable == 0
30+
assert nz == 3
31+
32+
def test_mc01td_warnings():
33+
""" test_mc01td_warnings: Test warnings """
34+
T = [([0, 0], "Entry ``P(x)`` is the zero polynomial."),
35+
([0, 1], "The polynomial ``P(x)`` is most probably unstable,\n"
36+
"although it may be stable with one or more zeros\n"
37+
"very close to the imaginary axis.\n"
38+
"The number of unstable zeros (NZ) is not determined."),
39+
([1, 0], "The degree of the polynomial ``P(x)`` has been\n"
40+
"reduced to ``(DB - 1)`` because\n"
41+
"``P(DB+1-j) = 0.0`` on entry\n"
42+
"for ``j = 0, 1,..., k-1`` and ``P(DB+1-k) <> 0.0``.")]
43+
for P, m in T:
44+
with pytest.warns(SlycotResultWarning, match=re.escape(m)):
45+
(dp, stable, nz) = mc01td('C', len(P)-1, P)

0 commit comments

Comments
 (0)