Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gifti to_xml TypeError: cannot serialize 0 (type int) #469

Closed
yarikoptic opened this issue Aug 1, 2016 · 1 comment
Closed

gifti to_xml TypeError: cannot serialize 0 (type int) #469

yarikoptic opened this issue Aug 1, 2016 · 1 comment
Labels

Comments

@yarikoptic
Copy link
Member

happens with 2.0.1-634-g82a261c
After the fixup #468 ran into another failure which I am not sure at which stage should be addressed, so decided just to report with details:

======================================================================
ERROR: test_surf_gifti (mvpa2.tests.test_surfing.SurfTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/yoh/proj/pymvpa/pymvpa/mvpa2/testing/tools.py", line 278, in newfunc
    func(*(arg + (filename,)), **kw)
  File "/home/yoh/proj/pymvpa/pymvpa/mvpa2/tests/test_surfing.py", line 1104, in test_surf_gifti
    surf.write(fn, s)
  File "/home/yoh/proj/pymvpa/pymvpa/mvpa2/support/nibabel/surf.py", line 2229, in write
    surf_gifti.write(fn, s, overwrite=overwrite)
  File "/home/yoh/proj/pymvpa/pymvpa/mvpa2/support/nibabel/surf_gifti.py", line 256, in write
    xml = to_xml(s, fn)
  File "/home/yoh/proj/pymvpa/pymvpa/mvpa2/support/nibabel/surf_gifti.py", line 219, in to_xml
    xml = img.to_xml().encode('utf-8')
  File "/home/yoh/proj/nipy/nipy-suite/nibabel/nibabel/gifti/gifti.py", line 680, in to_xml
    """ + xml.XmlSerializable.to_xml(self, enc)
  File "/home/yoh/proj/nipy/nipy-suite/nibabel/nibabel/xmlutils.py", line 30, in to_xml
    return tostring(self._to_xml_element(), enc)
  File "/usr/lib/python2.7/xml/etree/ElementTree.py", line 1126, in tostring
    ElementTree(element).write(file, encoding, method=method)
  File "/usr/lib/python2.7/xml/etree/ElementTree.py", line 820, in write
    serialize(write, self._root, encoding, qnames, namespaces)
  File "/usr/lib/python2.7/xml/etree/ElementTree.py", line 939, in _serialize_xml
    _serialize_xml(write, e, encoding, qnames, None)
  File "/usr/lib/python2.7/xml/etree/ElementTree.py", line 932, in _serialize_xml
    v = _escape_attrib(v, encoding)
  File "/usr/lib/python2.7/xml/etree/ElementTree.py", line 1092, in _escape_attrib
    _raise_serialization_error(text)
  File "/usr/lib/python2.7/xml/etree/ElementTree.py", line 1052, in _raise_serialization_error
    "cannot serialize %r (type %s)" % (text, type(text).__name__)
TypeError: cannot serialize 0 (type int)

which happens due to "rogue" ExternalFileOffset being an int, not serialized to str

899     def _serialize_xml(write, elem, encoding, qnames, namespaces):
900         tag = elem.tag
901         text = elem.text
902         if tag is Comment:
903             write("<!--%s-->" % _encode(text, encoding))
904         elif tag is ProcessingInstruction:
905             write("<?%s?>" % _encode(text, encoding))
(Pdb) 
906         else:
907             tag = qnames[tag]
908             if tag is None:
909                 if text:
910                     write(_escape_cdata(text, encoding))
911                 for e in elem:
912                     _serialize_xml(write, e, encoding, qnames, None)
913             else:
914                 write("<" + tag)
915                 items = elem.items()
916                 if items or namespaces:
(Pdb) 
917                     if namespaces:
918                         for v, k in sorted(namespaces.items(),
919                                            key=lambda x: x[1]):  # sort on prefix
920                             if k:
921                                 k = ":" + k
922                             write(" xmlns%s=\"%s\"" % (
923                                 k.encode(encoding),
924                                 _escape_attrib(v, encoding)
925                                 ))
926                     for k, v in sorted(items):  # lexical order
927                         if isinstance(k, QName):
(Pdb) 
928                             k = k.text
929                         if isinstance(v, QName):
930                             v = qnames[v.text]
931                         else:
932  ->                         v = _escape_attrib(v, encoding)
933                         write(" %s=\"%s\"" % (qnames[k], v))
934                 if text or len(elem):
935                     write(">")
936                     if text:
937                         write(_escape_cdata(text, encoding))
938                     for e in elem:
(Pdb) p elem
<Element 'DataArray' at 0x7fd24f50de90>
*(Pdb) p elem.tag
'DataArray'
*(Pdb) p elem.text
None
(Pdb) p namespaces
None
(Pdb) p items
[('Dimensionality', '2'), ('ExternalFileName', ''), ('Encoding', 'GZipBase64Binary'), ('DataType', 'NIFTI_TYPE_FLOAT32'), ('ArrayIndexingOrder', 'RowMajorOrder'), ('ExternalFileOffset', 0), ('Dim0', '4'), ('Dim1', '3'), ('Intent', 'NIFTI_INTENT_POINTSET'), ('Endian', 'LittleEndian')]
@yarikoptic yarikoptic added the bug label Aug 1, 2016
matthew-brett added a commit to matthew-brett/nibabel that referenced this issue Aug 3, 2016
GiftiDataArrays created in memory (rather than loaded from disk) had
ExternalFileOffset attribute value as an integer, but for valid XML, the
values have to be strings.

Closes nipygh-469.
@matthew-brett
Copy link
Member

Check out fix in #470?

matthew-brett added a commit to matthew-brett/nibabel that referenced this issue Aug 3, 2016
GiftiDataArrays created in memory (rather than loaded from disk) had
ExternalFileOffset attribute value as an integer, but for valid XML, the
values have to be strings.

Closes nipygh-469.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants