Skip to content

Commit

Permalink
Improve robustness of version check for decoding plugins (#2004)
Browse files Browse the repository at this point in the history
  • Loading branch information
scaramallion committed Feb 7, 2024
1 parent 2a24a10 commit 2877aa6
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 6 deletions.
7 changes: 4 additions & 3 deletions src/pydicom/pixels/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,11 @@ def _passes_version_check(package_name: str, minimum_version: tuple[int, ...]) -
"""
try:
module = importlib.import_module(package_name, "__version__")
except ModuleNotFoundError:
return False
return tuple(int(x) for x in module.__version__.split(".")) >= minimum_version
except Exception as exc:
LOGGER.exception(exc)

return tuple(int(x) for x in module.__version__.split(".")) >= minimum_version
return False


# TODO: Python 3.11 switch to StrEnum
Expand Down
4 changes: 2 additions & 2 deletions tests/pixels/test_decoder_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -1517,7 +1517,7 @@ def test_expb_ow_view_only_warns(self, caplog):
decoder.as_array(reference.ds, view_only=True)
assert msg in caplog.text

def test_expb_ow_index_invalid_raises(self, caplog):
def test_expb_ow_index_invalid_raises(self):
"""Test invalid index with BE swapped OW raises"""
decoder = get_decoder(ExplicitVRBigEndian)
reference = EXPB_8_1_1F
Expand Down Expand Up @@ -1873,7 +1873,7 @@ def test_encapsulated_excess_frames(self):

assert len(buffer) == 11 * 64 * 64 * 2

def test_expb_ow_index_invalid_raises(self, caplog):
def test_expb_ow_index_invalid_raises(self):
"""Test invalid index with BE swapped OW raises"""
decoder = get_decoder(ExplicitVRBigEndian)
reference = EXPB_8_1_1F
Expand Down
11 changes: 11 additions & 0 deletions tests/pixels/test_decoder_gdcm.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Test the GDCM decoder."""

import importlib
import logging

import pytest

Expand All @@ -12,6 +13,7 @@
HAVE_NP = False

from pydicom.pixels import get_decoder
from pydicom.pixels.utils import _passes_version_check
from pydicom.uid import (
JPEGBaseline8Bit,
JPEGExtended12Bit,
Expand Down Expand Up @@ -127,3 +129,12 @@ def test_j2k(self, reference):
assert arr.shape == reference.shape
assert arr.dtype == reference.dtype
assert arr.flags.writeable


@pytest.mark.skipif(SKIP_TEST, reason="Test is missing dependencies")
def test_version_check(caplog):
"""Test _passes_version_check() when the package has no __version__"""
# GDCM doesn't have a __version__ attribute
with caplog.at_level(logging.ERROR, logger="pydicom"):
assert _passes_version_check("gdcm", (3, 0)) is False
assert "module 'gdcm' has no attribute '__version__'" in caplog.text
10 changes: 9 additions & 1 deletion tests/pixels/test_utils.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import importlib
from io import BytesIO
import logging
import os

import pytest
Expand All @@ -14,7 +15,7 @@
from pydicom import dcmread, Dataset
from pydicom.pixel_data_handlers.util import convert_color_space
from pydicom.pixels import pixel_array, iter_pixels
from pydicom.pixels.utils import _as_options
from pydicom.pixels.utils import _as_options, _passes_version_check
from pydicom.uid import EnhancedMRImageStorage, ExplicitVRLittleEndian

from .pixels_reference import (
Expand Down Expand Up @@ -346,3 +347,10 @@ def test_decoding_plugin(self):
pylibjpeg_gen = iter_pixels(RLE_16_1_10F.path, decoding_plugin="pylibjpeg")
for frame1, frame2 in zip(pydicom_gen, pylibjpeg_gen):
assert np.array_equal(frame1, frame2)


def test_version_check(caplog):
"""Test _passes_version_check() when the package is absent"""
with caplog.at_level(logging.ERROR, logger="pydicom"):
assert _passes_version_check("foo", (3, 0)) is False
assert "No module named 'foo'" in caplog.text

0 comments on commit 2877aa6

Please sign in to comment.