use 3.x warning architecture

This commit is contained in:
John Evans 2014-09-16 20:05:28 -04:00
commit 6ae0b14ba8

View file

@ -15,6 +15,7 @@ Test suite specifically targeting JP2 box layout.
# pylint: disable=W0613
import os
import re
import struct
import sys
import tempfile
@ -166,7 +167,6 @@ class TestXML(unittest.TestCase):
@unittest.skipIf(os.name == "nt", "NamedTemporaryFile issue on windows")
class TestJp2kBadXmlFile(unittest.TestCase):
"""Test suite for bad XML box situations"""
@ -207,14 +207,11 @@ class TestJp2kBadXmlFile(unittest.TestCase):
def tearDown(self):
pass
@unittest.skipIf(sys.hexversion < 0x03020000, "assertWarns not until 3.2")
def test_invalid_xml_box(self):
"""Should be able to recover info from xml box with bad xml."""
with warnings.catch_warnings(record=True) as w:
warnings.simplefilter("always")
with self.assertWarns(UserWarning):
jp2k = Jp2k(self._bad_xml_file)
self.assertTrue(issubclass(w[0].category, UserWarning))
msg = 'No XML was retrieved'
self.assertTrue(msg in str(w[0].message))
self.assertEqual(jp2k.box[3].box_id, 'xml ')
self.assertEqual(jp2k.box[3].offset, 77)
@ -263,14 +260,12 @@ class TestBadButRecoverableXmlFile(unittest.TestCase):
def tearDownClass(cls):
os.unlink(cls._bad_xml_file)
@unittest.skipIf(sys.hexversion < 0x03020000, "assertWarns not until 3.2")
def test_bad_xml_box_warning(self):
"""Should warn in case of bad XML"""
with warnings.catch_warnings(record=True) as w:
warnings.simplefilter('always')
regex = 'A UnicodeDecodeError was encountered parsing an XML box'
with self.assertWarnsRegex(UserWarning, regex):
Jp2k(self._bad_xml_file)
self.assertTrue(issubclass(w[0].category, UserWarning))
msg = 'A UnicodeDecodeError was encountered parsing an XML box'
self.assertTrue(msg in str(w[0].message))
def test_recover_from_bad_xml(self):
"""Should be able to recover info from xml box with bad xml."""
@ -285,6 +280,7 @@ class TestBadButRecoverableXmlFile(unittest.TestCase):
b'<test>this is a test</test>')
@unittest.skipIf(sys.hexversion < 0x03020000, "assertWarns not until 3.2")
@unittest.skipIf(OPJ_DATA_ROOT is None,
"OPJ_DATA_ROOT environment variable not set")
class TestXML_OpjDataRoot(unittest.TestCase):
@ -295,12 +291,10 @@ class TestXML_OpjDataRoot(unittest.TestCase):
filename = opj_data_file(os.path.join('input',
'nonregression',
'issue171.jp2'))
with warnings.catch_warnings(record=True) as w:
warnings.simplefilter("always")
msg = 'An illegal BOM \(byte order marker\) was detected and removed '
msg += 'from the XML contents in the box starting at byte offset \d+'
with self.assertWarnsRegex(UserWarning, re.compile(msg)):
jp2 = Jp2k(filename)
self.assertTrue(issubclass(w[0].category, UserWarning))
msg = 'An illegal BOM (byte order marker) was detected and removed'
self.assertTrue(msg in str(w[0].message))
self.assertIsNotNone(jp2.box[3].xml)
@ -310,10 +304,8 @@ class TestXML_OpjDataRoot(unittest.TestCase):
filename = opj_data_file(os.path.join('input',
'nonregression',
'26ccf3651020967f7778238ef5af08af.SIGFPE.d25.527.jp2'))
with warnings.catch_warnings(record=True) as w:
warnings.simplefilter("always")
with self.assertWarns((UserWarning, UserWarning)):
jp2 = Jp2k(filename)
self.assertTrue(issubclass(w[0].category, UserWarning))
self.assertIsNone(jp2.box[3].box[1].box[1].xml)