CreateFromDocument crashes using binary types with zero-length data #51

Closed
evanunderscore opened this Issue Feb 29, 2016 · 2 comments

Projects

None yet

2 participants

@evanunderscore

Sample schema (this also happens with base64Binary):

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="test">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="bin" type="xs:hexBinary"/>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
</xs:schema>

Compile bindings:

pyxbgen test.xsd -m test_binding

Read document:

import test_binding
test_binding.CreateFromDocument(open('test.xml').read()).bin

The following XML samples work:

<test><bin>aa</bin></test>
<test><bin> </bin></test>

The following don't:

<test><bin></bin></test>
<test><bin/></test>

Traceback:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "test_binding.py", line 62, in CreateFromDocument
    saxer.parse(io.BytesIO(xmld))
  File "C:\Python27\lib\xml\sax\expatreader.py", line 110, in parse
    xmlreader.IncrementalParser.parse(self, source)
  File "C:\Python27\lib\xml\sax\xmlreader.py", line 123, in parse
    self.feed(buffer)
  File "C:\Python27\lib\xml\sax\expatreader.py", line 213, in feed
    self._parser.Parse(data, isFinal)
  File "C:\Python27\lib\xml\sax\expatreader.py", line 365, in end_element_ns
    self._cont_handler.endElementNS(pair, None)
  File "pyxb\binding\saxer.py", line 388, in endElementNS
    binding_object = this_state.endBindingElement()
  File "pyxb\binding\saxer.py", line 226, in endBindingElement
    self.__constructElement(self.__delayedConstructor, self.__attributes, args)
  File "pyxb\binding\saxer.py", line 116, in __constructElement
    self.__bindingInstance = new_object_factory(*content, **kw)
  File "pyxb\binding\basis.py", line 1600, in __call__
    rv = self.typeDefinition().Factory(*args, **kw)
  File "pyxb\binding\basis.py", line 305, in Factory
    rv = cls._DynamicCreate(*args, **kw)
  File "pyxb\binding\basis.py", line 677, in _DynamicCreate
    return ctor(*args, **kw)
  File "pyxb\binding\basis.py", line 873, in __new__
    args = cls._ConvertArguments(args, kw)
  File "pyxb\binding\basis.py", line 851, in _ConvertArguments
    return cls._ConvertArguments_vx(args, kw)
  File "pyxb\binding\datatypes.py", line 810, in _ConvertArguments_vx
    xmlt = args[0]
IndexError: tuple index out of range

It looks like the problem is that the SAX handler's ContentHandler.characters doesn't get called with the empty string for an empty element so args is ending up empty. The simplest fix is to handle that in hexBinary and base64Binary but I really have no idea how much more of the code is affected by this.

@pabigot pabigot added a commit that closed this issue Feb 29, 2016
@pabigot fix #51: binary type factory error with empty content
All other types appear to check for content before attempting to convert
it from lexical to value space.
9fced97
@pabigot pabigot closed this in 9fced97 Feb 29, 2016
@pabigot
Owner
pabigot commented Feb 29, 2016

Thanks for the input. From what I can tell the issue was limited to these types.

@evanunderscore

Thanks!

@pabigot pabigot added this to the PyXB 1.2.5 milestone Sep 18, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment