SeiictyUsui / pydicom

Automatically exported from code.google.com/p/pydicom
0 stars 0 forks source link

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

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
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 "<stdin>", line 1, in <module>
  File
"/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/site-packages/p
ydicom-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 "<stdin>", line 1, in <module>
  File
"/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/site-packages/p
ydicom-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/p
ydicom-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/p
ydicom-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

Original issue reported on code.google.com by NikitaTh...@gmail.com on 27 Jan 2010 at 10:44

Attachments:

GoogleCodeExporter commented 9 years ago
In the second sentence of my bug report, "latter" should say "former".

Original comment by NikitaTh...@gmail.com on 28 Jan 2010 at 5:36

GoogleCodeExporter commented 9 years ago
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

Original comment by darcymason@gmail.com on 30 Jan 2010 at 5:44

GoogleCodeExporter commented 9 years ago
This issue was closed by revision aec1410aad.

Original comment by darcymason@gmail.com on 30 Jan 2010 at 9:42