diff --git a/glymur/jp2box.py b/glymur/jp2box.py index 4a390a5..e29df28 100644 --- a/glymur/jp2box.py +++ b/glymur/jp2box.py @@ -35,11 +35,12 @@ from .core import (_COLORSPACE_MAP_DISPLAY, _COLOR_TYPE_MAP_DISPLAY, from . import _uuid_io -_METHOD_DISPLAY = { - ENUMERATED_COLORSPACE: 'enumerated colorspace', - RESTRICTED_ICC_PROFILE: 'restricted ICC profile', - ANY_ICC_PROFILE: 'any ICC profile', - VENDOR_COLOR_METHOD: 'vendor color method'} +_factory = lambda x: '{0} (invalid)'.format(x) +_keysvalues = {ENUMERATED_COLORSPACE: 'enumerated colorspace', + RESTRICTED_ICC_PROFILE: 'restricted ICC profile', + ANY_ICC_PROFILE: 'any ICC profile', + VENDOR_COLOR_METHOD: 'vendor color method'} +_METHOD_DISPLAY = _Keydefaultdict(_factory, _keysvalues) _factory = lambda x: '{0} (invalid)'.format(x) _keysvalues = {1: 'accurately represents correct colorspace definition', @@ -1317,7 +1318,13 @@ class FileTypeBox(Jp2kBox): for j in range(int(num_entries)): entry, = struct.unpack_from('>4s', read_buffer, 8 + j * 4) if sys.hexversion >= 0x03000000: - entry = entry.decode('utf-8') + try: + entry = entry.decode('utf-8') + except UnicodeDecodeError as err: + # The entry is invalid, but we've got code to catch this + # later on. + pass + compatibility_list.append(entry) return cls(brand=brand, minor_version=minor_version, diff --git a/glymur/test/test_jp2k.py b/glymur/test/test_jp2k.py index 4dc4df4..4bc928b 100644 --- a/glymur/test/test_jp2k.py +++ b/glymur/test/test_jp2k.py @@ -272,6 +272,15 @@ class TestJp2k(unittest.TestCase): jp2 = Jp2k(jfile) self.assertEqual(jp2.shape, (128, 128)) + @unittest.skipIf(OPJ_DATA_ROOT is None, + "OPJ_DATA_ROOT environment variable not set") + def test_invalid_compatibility_list_entry(self): + """should not error out with invalid compatibility list entry""" + filename = opj_data_file('input/nonregression/issue397.jp2') + with self.assertWarns(UserWarning): + Jp2k(filename) + self.assertTrue(True) + def test_shape_j2k(self): """verify shape attribute for J2K file """ diff --git a/glymur/test/test_opj_suite_dump.py b/glymur/test/test_opj_suite_dump.py index f13b128..844ce9c 100644 --- a/glymur/test/test_opj_suite_dump.py +++ b/glymur/test/test_opj_suite_dump.py @@ -3,7 +3,6 @@ The tests defined here roughly correspond to what is in the OpenJPEG test suite. """ import re -import sys import unittest import numpy as np diff --git a/glymur/test/test_printing.py b/glymur/test/test_printing.py index f9fcd73..49fec57 100644 --- a/glymur/test/test_printing.py +++ b/glymur/test/test_printing.py @@ -831,6 +831,17 @@ class TestPrintingOpjDataRootWarns(unittest.TestCase): def tearDown(self): pass + def test_invalid_colour_specification_method(self): + """should not error out with invalid colour specification method""" + # Don't care so much about what the output looks like, just that we + # do not error out. + filename = opj_data_file('input/nonregression/issue397.jp2') + with self.assertWarns(UserWarning): + jp2 = Jp2k(filename) + with patch('sys.stdout', new=StringIO()): + print(jp2) + self.assertTrue(True) + def test_invalid_colorspace(self): """An invalid colorspace shouldn't cause an error.""" filename = opj_data_file('input/nonregression/edf_c2_1103421.jp2')