Skip to content

Commit 1ffc727

Browse files
committed
Try to fix test__warnings
1 parent 5f8d213 commit 1ffc727

1 file changed

Lines changed: 41 additions & 53 deletions

File tree

tests/suite/modules/misc/test__warnings.py

Lines changed: 41 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -6,69 +6,57 @@
66
CPython's _warnings module. http://docs.python.org/library/warnings.html
77
'''
88

9-
import unittest
109
import _warnings
11-
from iptest import IronPythonTestCase, run_test, stderr_trapper as stderr_trapper
10+
from iptest import IronPythonTestCase, stderr_trapper, run_test
1211

1312
#--GLOBALS---------------------------------------------------------------------
14-
EXPECTED = [] # expected output (ignoring filename, lineno, and line)
1513
WARN_TYPES = [Warning, UserWarning, PendingDeprecationWarning, SyntaxWarning,
1614
RuntimeWarning, FutureWarning, ImportWarning, UnicodeWarning,
1715
BytesWarning]
1816

19-
def cleanup():
20-
'''Clean up after possible incomplete test runs.'''
21-
global EXPECTED
22-
EXPECTED = []
23-
24-
25-
def expect(warn_type, message):
26-
'''Helper for test output'''
27-
for filter in _warnings.filters:
28-
if filter[0] == "ignore" and issubclass(warn_type, filter[2]):
29-
return
30-
EXPECTED.append(": " + warn_type.__name__ + ": " + message + "\n")
31-
32-
3317
class _WarningsTest(IronPythonTestCase):
3418
#TODO: @skip("multiple_execute")
3519
def test_sanity(self):
36-
global EXPECTED
37-
try:
38-
with stderr_trapper() as output:
39-
# generate test output
40-
_warnings.warn("Warning Message!")
41-
expect(UserWarning, "Warning Message!")
42-
for warn_type in WARN_TYPES:
43-
_warnings.warn(warn_type("Type-overriding message!"), UnicodeWarning)
44-
expect(warn_type, "Type-overriding message!")
45-
_warnings.warn("Another Warning Message!", warn_type)
46-
expect(warn_type, "Another Warning Message!")
47-
_warnings.warn_explicit("Explicit Warning!", warn_type, "nonexistent_file.py", 12)
48-
expect(warn_type, "Explicit Warning!")
49-
_warnings.warn_explicit("Explicit Warning!", warn_type, "test_python26.py", 34)
50-
expect(warn_type, "Explicit Warning!")
51-
_warnings.warn_explicit("Explicit Warning!", warn_type, "nonexistent_file.py", 56, "module.py")
52-
expect(warn_type, "Explicit Warning!")
53-
_warnings.warn_explicit("Explicit Warning!", warn_type, "test_python26.py", 78, "module.py")
54-
expect(warn_type, "Explicit Warning!")
55-
56-
temp_messages = output.messages
57-
58-
#No point in going further if the number of lines is not what we expect
59-
nlines = len([x for x in temp_messages if not x.startswith(" ")])
60-
self.assertEqual(nlines, len(EXPECTED))
61-
62-
# match lines
63-
for line in temp_messages:
64-
if line.startswith(" "):
65-
continue
66-
temp = EXPECTED.pop(0).rstrip()
67-
self.assertTrue(line.endswith(temp), str(line) + " does not end with " + temp)
68-
69-
finally:
70-
# remove generated files
71-
cleanup()
20+
EXPECTED = [] # expected output (ignoring filename, lineno, and line)
21+
22+
def expect(warn_type, message):
23+
'''Helper for test output'''
24+
# unittest will reset the warning filters so the base filters do nothing
25+
#for filter in _warnings.filters:
26+
# if filter[0] == "ignore" and issubclass(warn_type, filter[2]):
27+
# return
28+
EXPECTED.append(": " + warn_type.__name__ + ": " + message + "\n")
29+
30+
with stderr_trapper() as output:
31+
# generate test output
32+
_warnings.warn("Warning Message!")
33+
expect(UserWarning, "Warning Message!")
34+
for warn_type in WARN_TYPES:
35+
_warnings.warn(warn_type("Type-overriding message!"), UnicodeWarning)
36+
expect(warn_type, "Type-overriding message!")
37+
_warnings.warn("Another Warning Message!", warn_type)
38+
expect(warn_type, "Another Warning Message!")
39+
_warnings.warn_explicit("Explicit Warning!", warn_type, "nonexistent_file.py", 12)
40+
expect(warn_type, "Explicit Warning!")
41+
_warnings.warn_explicit("Explicit Warning!", warn_type, "test_python26.py", 34)
42+
expect(warn_type, "Explicit Warning!")
43+
_warnings.warn_explicit("Explicit Warning!", warn_type, "nonexistent_file.py", 56, "module.py")
44+
expect(warn_type, "Explicit Warning!")
45+
_warnings.warn_explicit("Explicit Warning!", warn_type, "test_python26.py", 78, "module.py")
46+
expect(warn_type, "Explicit Warning!")
47+
48+
temp_messages = output.messages
49+
50+
#No point in going further if the number of lines is not what we expect
51+
nlines = len([x for x in temp_messages if not x.startswith(" ")])
52+
self.assertEqual(nlines, len(EXPECTED))
53+
54+
# match lines
55+
for line in temp_messages:
56+
if line.startswith(" "):
57+
continue
58+
temp = EXPECTED.pop(0).rstrip()
59+
self.assertTrue(line.endswith(temp), str(line) + " does not end with " + temp)
7260

7361
def test_ipy2_gh23(self):
7462
def test():

0 commit comments

Comments
 (0)