pydicom.MultiValue not handled properly

Issue #803 resolved
Tim de Wit created an issue

When receiving c-find responses containing MultiValue objects, inside my python3 dev environment qrscu crashes immediately with below error message:

Traceback (most recent call last):
  File "qrscu.py", line 1512, in <module>
    qrscu_script()
  File "qrscu.py", line 1489, in qrscu_script
    get_empty_sr=processed_args['get_empty_sr']
  File "qrscu.py", line 912, in qrscu
    modalities_returned, modality_matching = _query_for_each_modality(all_mods, query, d, assoc)
  File "qrscu.py", line 736, in _query_for_each_modality
    _query_study(assoc, d, query, query_id)
  File "qrscu.py", line 666, in _query_study
    rsp.set_modalities_in_study(identifier.ModalitiesInStudy)
  File "/home/openrem/openremrepo/openrem/remapp/models.py", line 283, in set_modalities_in_study
    self.modalities_in_study = json.dumps(x)
  File "/usr/lib/python3.7/json/__init__.py", line 231, in dumps
    return _default_encoder.encode(obj)
  File "/usr/lib/python3.7/json/encoder.py", line 199, in encode
    chunks = self.iterencode(o, _one_shot=True)
  File "/usr/lib/python3.7/json/encoder.py", line 257, in iterencode
    return _iterencode(o, 0)
  File "/usr/lib/python3.7/json/encoder.py", line 179, in default
    raise TypeError(f'Object of type {o.__class__.__name__} '
TypeError: Object of type MultiValue is not JSON serializable

Easily reproduced with following code:

from pydicom.multival import MultiValue
import json

multi = MultiValue(str, ["DX", "SR"])

print(json.dumps(multi))

Replacing pydicom.multival with dicom.multival (pydicom 0.9.9) this code works, but with newer (python3) pydicom versions it fails… not sure if it’s because of python 2 vs 3 or modifications inside the pydicom library.

Comments (4)

  1. Tim de Wit reporter

    Error seemed to originate from pydicom’s MultiValue class subclassing list in older versions, but subclassing MutableSequence starting from the first v1.0 beta versions of pydicom. Quick(est) fix on my end was to change

    self.modalities_in_study = json.dumps(x)
    

    Into

    self.modalities_in_study = json.dumps(list(x))
    

    in models.py

  2. Log in to comment