suever / pydicom-experimental

pydicom test
0 stars 1 forks source link

read_file() fails when passed file-like objects #73

Closed suever closed 9 years ago

suever commented 9 years ago

From NikitaTh...@gmail.com on January 27, 2010 17:44:32

According to the docstring, read_file() accepts "either a file-like object, or a string containing the file name". However, only the latter works in practice. Both file and StringIO objects cause read_file() to fail. I attached a patch for the problem.

Here's an interpreter session that demonstrates the problem using the test file rtplan.dcm: $ 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 import StringIO filename = "rtplan.dcm"

When the filename is a string, all is well

... ds = dicom.read_file(filename)

When the param is a file object, the call fails

... f = open(filename, "rb") ds = dicom.read_file(f) Traceback (most recent call last): File "", line 1, in File "/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/site-packages/pydicom-0.9.3-py2.5.egg/dicom/filereader.py", line 416, in read_file fp.defer_size = defer_size AttributeError: 'file' object has no attribute 'defer_size'

When the param is a StringIO object, the call fails

... f.seek(0) fake_file = StringIO.StringIO(f.read()) ds = dicom.read_file(fake_file) Traceback (most recent call last): File "", line 1, in File "/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/site-packages/pydicom-0.9.3-py2.5.egg/dicom/filereader.py", line 429, in read_file FileMetaInfo = _read_file_meta_info(fp) File "/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/site-packages/pydicom-0.9.3-py2.5.egg/dicom/filereader.py", line 376, in _read_file_meta_info GroupLength = read_data_element(fp) File "/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/site-packages/pydicom-0.9.3-py2.5.egg/dicom/filereader.py", line 107, in read_data_element tag = fp.read_tag() AttributeError: StringIO instance has no attribute 'read_tag'

Applying the attached patch gets things working smoothly:

$ 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 import StringIO filename = "rtplan.dcm" ds = dicom.read_file(filename) f = open(filename, "rb") ds = dicom.read_file(f) f.seek(0) fake_file = StringIO.StringIO(f.read()) ds = dicom.read_file(fake_file)

Hope this helps, Philip

Attachment: filereader.patch

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

suever commented 9 years ago

From NikitaTh...@gmail.com on January 28, 2010 09:36:20

In the second sentence of my bug report, "latter" should say "former".

suever commented 9 years ago

From darcymason@gmail.com on January 30, 2010 09:44:15

At some point pydicom transitioned to using a DicomFileLike object, and passing a true file or StringIO to read_file() no longer worked (a bug, as pointed out here). However, the very recent repository pushes have fundamentally reworked that process again, going back to simple files (for reading, not writing yet), and include code very similar to this patch. So I believe this bug has been fixed. I've written some unit tests to be sure, and expect to push them to the repository soon. -Darcy

Status: Started

suever commented 9 years ago

From darcymason@gmail.com on January 30, 2010 13:42:23

This issue was closed by revision aec1410aad .

Status: Fixed
Mergedinto: -