suever / pydicom-experimental

pydicom test
0 stars 1 forks source link

dataset.get() fails when passed type other than string or Tag #72

Closed suever closed 9 years ago

suever commented 9 years ago

From NikitaTh...@gmail.com on January 22, 2010 16:10:43

I'm using pydicom 0.9.3.

The get() method in dataset.py doesn't work correctly when passed a key that's not a string or a Tag object (e.g. if I pass the key as a tuple). Specifically, get() returns the default even if the tag is present.

Here's an example using the "rtplan.dcm" file that's part of the pydicom distribution and using a tuple-style tag of (0x0010, 0x0010).

$ python Python 2.5.1 ( r251 :54863, Nov 17 2007, 21:19:53) [GCC 4.0.1 (Apple Computer, Inc. build 5367)] on darwin Type "help", "copyright", "credits" or "license" for more information.

import dicom ds=dicom.read_file("rtplan.dcm") t = (0x0010, 0x0010) t in ds True ds[t](0010, 0010) Patient's Name PN: 'Last^First^mid^pre' ds.get(t) ds.get(t, "foo") 'foo'

I patched dataset.py (patch attached) and that seems to fix the problem. In the interactive session below you can see that the patched get() behaves correctly when fed (0x0010, 0x0010) as a tuple, string, long, or Tag object. It also correctly returns the default when fed a non-existent tuple.

$ python Python 2.5.1 ( r251 :54863, Nov 17 2007, 21:19:53) [GCC 4.0.1 (Apple Computer, Inc. build 5367)] on darwin Type "help", "copyright", "credits" or "license" for more information.

import dicom ds=dicom.read_file("rtplan.dcm") t = (0x0010, 0x0010) t in ds True ds[t](0010, 0010) Patient's Name PN: 'Last^First^mid^pre' ds.get(t) (0010, 0010) Patient's Name PN: 'Last^First^mid^pre' ds.get(t, "foo") (0010, 0010) Patient's Name PN: 'Last^First^mid^pre' t = 0x0010<<16 | 0x0010 ds.get(t) (0010, 0010) Patient's Name PN: 'Last^First^mid^pre' ds.get("PatientsName") 'Last^First^mid^pre' tag = ds[(0x0010, 0x0010)].tag type(tag) <class 'dicom.tag.Tag'> ds.get(tag) (0010, 0010) Patient's Name PN: 'Last^First^mid^pre' t = (0x9999, 0x9999) ds.get(t, "foo") 'foo'

Attachment: dataset_get.patch

Original issue: http://code.google.com/p/pydicom/issues/detail?id=72

suever commented 9 years ago

From darcymason@gmail.com on January 24, 2010 16:46:59

NikitaTheSpider, thanks for finding this bug and for contributing the patch. I made one small change -- raising a TypeError if the key was neither string nor some kind of tag, because no other kind of keys should be allowed in a Dataset anyway.

Fixed in revision bb7b95651c .

Status: Fixed