diff --git a/glymur/test/test_callbacks.py b/glymur/test/test_callbacks.py index 5b6bd74..ef61bc4 100644 --- a/glymur/test/test_callbacks.py +++ b/glymur/test/test_callbacks.py @@ -11,7 +11,6 @@ import os import re import sys import tempfile -import warnings import unittest @@ -41,8 +40,7 @@ class TestCallbacks(unittest.TestCase): def test_info_callback_on_write(self): """Verify messages printed when writing an image in verbose mode.""" j = glymur.Jp2k(self.jp2file) - with warnings.catch_warnings(): - warnings.simplefilter("ignore") + with self.assertWarns(UserWarning): tiledata = j.read(tile=0) with tempfile.NamedTemporaryFile(suffix='.jp2') as tfile: j = glymur.Jp2k(tfile.name, 'wb') diff --git a/glymur/test/test_codestream.py b/glymur/test/test_codestream.py index abea7e2..3b22807 100644 --- a/glymur/test/test_codestream.py +++ b/glymur/test/test_codestream.py @@ -13,7 +13,6 @@ import struct import sys import tempfile import unittest -import warnings from glymur import Jp2k import glymur diff --git a/glymur/test/test_config.py b/glymur/test/test_config.py index b8c97ae..2e3d6d0 100644 --- a/glymur/test/test_config.py +++ b/glymur/test/test_config.py @@ -16,7 +16,6 @@ import os import sys import tempfile import unittest -import warnings if sys.hexversion <= 0x03030000: from mock import patch @@ -84,11 +83,9 @@ class TestSuite(unittest.TestCase): with patch.dict('os.environ', {'XDG_CONFIG_HOME': tdir}): # Misconfigured new configuration file should # be rejected. - with warnings.catch_warnings(record=True) as w: - warnings.simplefilter('always') + regex = 'could not be loaded' + with self.assertWarnsRegex(UserWarning, regex): imp.reload(glymur.lib.openjp2) - self.assertTrue(issubclass(w[0].category,UserWarning)) - self.assertTrue('could not be loaded' in str(w[0].message)) @unittest.skipIf(glymur.lib.openjp2.OPENJP2 is None and diff --git a/glymur/test/test_glymur_warnings.py b/glymur/test/test_glymur_warnings.py index 7641d73..d222081 100644 --- a/glymur/test/test_glymur_warnings.py +++ b/glymur/test/test_glymur_warnings.py @@ -12,7 +12,6 @@ import struct import sys import tempfile import unittest -import warnings import six diff --git a/glymur/test/test_icc.py b/glymur/test/test_icc.py index e41042b..71819f3 100644 --- a/glymur/test/test_icc.py +++ b/glymur/test/test_icc.py @@ -9,7 +9,6 @@ import datetime import os import sys import unittest -import warnings import numpy as np @@ -31,9 +30,8 @@ class TestICC(unittest.TestCase): def test_file5(self): """basic ICC profile""" filename = opj_data_file('input/conformance/file5.jp2') - with warnings.catch_warnings(): + with self.assertWarns(UserWarning): # The file has a bad compatibility list entry. Not important here. - warnings.simplefilter("ignore") j = Jp2k(filename) profile = j.box[3].box[1].icc_profile self.assertEqual(profile['Size'], 546) diff --git a/glymur/test/test_jp2box.py b/glymur/test/test_jp2box.py index e0f12e2..03a8171 100644 --- a/glymur/test/test_jp2box.py +++ b/glymur/test/test_jp2box.py @@ -23,7 +23,6 @@ import tempfile import uuid from uuid import UUID import unittest -import warnings import lxml.etree as ET import numpy as np @@ -365,12 +364,9 @@ class TestChannelDefinition(unittest.TestCase): channel_type = (COLOR, COLOR, 3) association = (RED, GREEN, BLUE) - with warnings.catch_warnings(record=True) as w: - warnings.simplefilter('always') + with self.assertWarns(UserWarning): glymur.jp2box.ChannelDefinitionBox(channel_type=channel_type, association=association) - self.assertEqual(len(w), 1) - self.assertTrue(issubclass(w[0].category, UserWarning)) def test_wrong_lengths(self): """Should reject if not all of index, channel_type, association the @@ -378,12 +374,9 @@ class TestChannelDefinition(unittest.TestCase): """ channel_type = (COLOR, COLOR) association = (RED, GREEN, BLUE) - with warnings.catch_warnings(record=True) as w: - warnings.simplefilter('always') + with self.assertWarns(UserWarning): glymur.jp2box.ChannelDefinitionBox(channel_type=channel_type, association=association) - self.assertEqual(len(w), 1) - self.assertTrue(issubclass(w[0].category, UserWarning)) class TestFileTypeBox(unittest.TestCase): @@ -397,8 +390,7 @@ class TestFileTypeBox(unittest.TestCase): def test_brand_unknown(self): """A ftyp box brand must be 'jp2 ' or 'jpx '.""" - with warnings.catch_warnings(): - warnings.simplefilter("ignore") + with self.assertWarns(UserWarning): ftyp = glymur.jp2box.FileTypeBox(brand='jp3') with self.assertRaises(IOError): with tempfile.TemporaryFile() as tfile: @@ -406,9 +398,8 @@ class TestFileTypeBox(unittest.TestCase): def test_cl_entry_unknown(self): """A ftyp box cl list can only contain 'jp2 ', 'jpx ', or 'jpxb'.""" - with warnings.catch_warnings(): + with self.assertWarns(UserWarning): # Bad compatibility list item. - warnings.simplefilter("ignore") ftyp = glymur.jp2box.FileTypeBox(compatibility_list=['jp3']) with self.assertRaises(IOError): with tempfile.TemporaryFile() as tfile: @@ -481,39 +472,29 @@ class TestColourSpecificationBox(unittest.TestCase): def test_colr_with_cspace_and_icc(self): """Colour specification boxes can't have both.""" - with warnings.catch_warnings(record=True) as w: - warnings.simplefilter('always') + regex = 'Colorspace and icc_profile cannot both be set' + with self.assertWarnsRegex(UserWarning, regex): colorspace = glymur.core.SRGB rawb = b'\x01\x02\x03\x04' glymur.jp2box.ColourSpecificationBox(colorspace=colorspace, icc_profile=rawb) - self.assertTrue(issubclass(w[0].category,UserWarning)) - msg = 'Colorspace and icc_profile cannot both be set' - self.assertTrue(msg in str(w[0].message)) def test_colr_with_bad_method(self): """colr must have a valid method field""" colorspace = glymur.core.SRGB method = -1 - with warnings.catch_warnings(record=True) as w: - warnings.simplefilter('always') + regex = 'Invalid method' + with self.assertWarnsRegex(UserWarning, regex): glymur.jp2box.ColourSpecificationBox(colorspace=colorspace, method=method) - self.assertTrue(issubclass(w[0].category,UserWarning)) - msg = 'Invalid method' - self.assertTrue(msg in str(w[0].message)) def test_colr_with_bad_approx(self): """colr should have a valid approximation field""" colorspace = glymur.core.SRGB approx = -1 - with warnings.catch_warnings(record=True) as w: - warnings.simplefilter('always') + with self.assertWarnsRegex(UserWarning, 'Invalid approximation'): glymur.jp2box.ColourSpecificationBox(colorspace=colorspace, approximation=approx) - self.assertTrue(issubclass(w[0].category,UserWarning)) - msg = 'Invalid approximation' - self.assertTrue(msg in str(w[0].message)) def test_colr_with_bad_color(self): """colr must have a valid color, strange as though that may sound.""" @@ -542,24 +523,18 @@ class TestPaletteBox(unittest.TestCase): palette = np.array([[255, 0, 255], [0, 255, 0]], dtype=np.uint8) bps = (8, 8, 8) signed = (False, False) - with warnings.catch_warnings(record=True) as w: - warnings.simplefilter('always') + with self.assertWarns(UserWarning): pclr = glymur.jp2box.PaletteBox(palette, bits_per_component=bps, signed=signed) - self.assertEqual(len(w), 1) - self.assertTrue(issubclass(w[0].category, UserWarning)) def test_mismatched_signed_palette(self): """bitdepth and signed arguments must have equal length""" palette = np.array([[255, 0, 255], [0, 255, 0]], dtype=np.uint8) bps = (8, 8, 8, 8) signed = (False, False, False, False) - with warnings.catch_warnings(record=True) as w: - warnings.simplefilter('always') + with self.assertWarns(UserWarning): pclr = glymur.jp2box.PaletteBox(palette, bits_per_component=bps, signed=signed) - self.assertEqual(len(w), 1) - self.assertTrue(issubclass(w[0].category, UserWarning)) def test_writing_with_different_bitdepths(self): """Bitdepths must be the same when writing.""" diff --git a/glymur/test/test_jp2box_jpx.py b/glymur/test/test_jp2box_jpx.py index 3a5d42e..421496f 100644 --- a/glymur/test/test_jp2box_jpx.py +++ b/glymur/test/test_jp2box_jpx.py @@ -9,7 +9,6 @@ import struct import sys import tempfile import unittest -import warnings import lxml.etree as ET @@ -311,11 +310,8 @@ class TestJPXWrap(unittest.TestCase): boxes = [jp2.box[idx] for idx in [0, 1, 2, 4]] ftyp = glymur.jp2box.FileTypeBox() - with warnings.catch_warnings(record=True) as w: - warnings.simplefilter('always') + with self.assertWarns(UserWarning): dref = glymur.jp2box.DataReferenceBox([ftyp]) - self.assertEqual(len(w), 1) - self.assertTrue(issubclass(w[0].category, UserWarning)) # Try to get around it by appending the ftyp box after creation. dref = glymur.jp2box.DataReferenceBox() @@ -438,8 +434,7 @@ class TestJPX(unittest.TestCase): offset = [89] length = [1132288] reference = [0, 0] - with warnings.catch_warnings(): - warnings.simplefilter("ignore") + with self.assertWarns(UserWarning): flst = glymur.jp2box.FragmentListBox(offset, length, reference) with self.assertRaises(IOError): with tempfile.TemporaryFile() as tfile: @@ -450,8 +445,7 @@ class TestJPX(unittest.TestCase): offset = [0] length = [1132288] reference = [0] - with warnings.catch_warnings(): - warnings.simplefilter("ignore") + with self.assertWarns(UserWarning): flst = glymur.jp2box.FragmentListBox(offset, length, reference) with self.assertRaises((IOError, OSError)): with tempfile.TemporaryFile() as tfile: @@ -462,8 +456,7 @@ class TestJPX(unittest.TestCase): offset = [89] length = [0] reference = [0] - with warnings.catch_warnings(): - warnings.simplefilter("ignore") + with self.assertWarns(UserWarning): flst = glymur.jp2box.FragmentListBox(offset, length, reference) with self.assertRaises(IOError): with tempfile.TemporaryFile() as tfile: @@ -471,9 +464,7 @@ class TestJPX(unittest.TestCase): def test_ftbl_boxes_empty(self): """A fragment table box must have at least one child box.""" - with warnings.catch_warnings(): - warnings.simplefilter("ignore") - ftbl = glymur.jp2box.FragmentTableBox() + ftbl = glymur.jp2box.FragmentTableBox() with self.assertRaises(IOError): with tempfile.TemporaryFile() as tfile: ftbl.write(tfile) diff --git a/glymur/test/test_jp2box_uuid.py b/glymur/test/test_jp2box_uuid.py index 44808bb..9ff138f 100644 --- a/glymur/test/test_jp2box_uuid.py +++ b/glymur/test/test_jp2box_uuid.py @@ -17,7 +17,6 @@ import struct import sys import tempfile import uuid -import warnings if sys.hexversion < 0x02070000: import unittest2 as unittest @@ -107,12 +106,8 @@ class TestUUIDExif(unittest.TestCase): tfile.write(struct.pack('