mhe / pynrrd

Simple pure-python module for reading and writing nrrd files.
https://pynrrd.readthedocs.io/
MIT License
116 stars 51 forks source link

Fix string decode in read_header #32

Closed addisonElliott closed 6 years ago

addisonElliott commented 6 years ago

In read_header, each line of the nrrdfile was decoded to ASCII. The decode function is only defined for a byte string (A.K.A when you open with 'b' suffix). An error occurs if you input a string list, such as the example given in docstring of read_header:

    >>> read_header(("NRRD0005", "type: float", "dimension: 3"))
    {u'type': 'float', u'dimension': 3, u'keyvaluepairs': {}}
    >>> read_header(("NRRD0005", "my extra info:=my : colon-separated : values"))
    {u'keyvaluepairs': {u'my extra info': u'my : colon-separated : values'}}

The error is: AttributeError: 'str' object has no attribute 'decode'

This PR fixes by checking if the data given has a decode attribute and if so the object will be decoded. This is ideal because a bytes or bytesarray could be given, both of which can be decoded.

Resolves issue #27. In #27, the issue was that read_header was given a filename string instead of file handle. The error now becomes:

nrrd.NrrdError: Missing magic "NRRD" word. Is this an NRRD file?