raise RuntimeError if writing with library version is too early, closes #273

We still raise a LibraryNotFoundError if there's no library at all
This commit is contained in:
jevans 2014-10-18 19:52:22 -04:00
commit f36c169f52
2 changed files with 30 additions and 12 deletions

View file

@ -415,10 +415,12 @@ class Jp2k(Jp2kBox):
glymur.jp2k.LibraryNotFoundError glymur.jp2k.LibraryNotFoundError
if glymur is unable to load an openjpeg library suitable for writing if glymur is unable to load an openjpeg library suitable for writing
""" """
if re.match("1.[0-4]", version.openjpeg_version) is not None:
raise RuntimeError("You must have at least version 1.5 of OpenJPEG "
"in order to write images.")
if opj2.OPENJP2 is None and opj.OPENJPEG is None: if opj2.OPENJP2 is None and opj.OPENJPEG is None:
raise LibraryNotFoundError("You must have at least version 1.5 of " raise LibraryNotFoundError("Cannot load the OpenJPEG library.")
"OpenJPEG before using this "
"functionality.")
self._determine_colorspace(img_array, **kwargs) self._determine_colorspace(img_array, **kwargs)
cparams = self._populate_cparams(img_array, **kwargs) cparams = self._populate_cparams(img_array, **kwargs)

View file

@ -22,6 +22,11 @@ import uuid
import warnings import warnings
from xml.etree import cElementTree as ET from xml.etree import cElementTree as ET
if sys.hexversion <= 0x03030000:
from mock import patch
else:
from unittest.mock import patch
import numpy as np import numpy as np
import pkg_resources import pkg_resources
@ -833,6 +838,17 @@ class TestJp2k_write(unittest.TestCase):
def tearDown(self): def tearDown(self):
pass pass
def test_write_with_version_too_early(self):
"""Should raise a runtime error if trying to write with version 1.3"""
data = np.zeros((128, 128), dtype=np.uint8)
versions = ["1.0.0", "1.1.0", "1.2.0", "1.3.0"]
for version in versions:
with patch('glymur.version.openjpeg_version', new=version):
with tempfile.NamedTemporaryFile(suffix='.j2k') as tfile:
with self.assertRaises(RuntimeError):
j = Jp2k(tfile.name, 'wb')
j.write(data)
def test_cblkh_different_than_width(self): def test_cblkh_different_than_width(self):
"""Verify that we can set a code block size where height does not equal """Verify that we can set a code block size where height does not equal
width. width.