Menu

#132 Crash in PyXB when attempting to retrieve Unicode value from exception

PyXB 1.1.4
closed
None
fixed
Binding model
major
PyXB 1.1.3
defect
2013-02-10
2012-04-25
Roger Dahl
No

pyxb/exceptions_.py contains the following function::

  def __str__ (self):
    """Override to use the system-provided message, if available."""
    if self.__message is not None:
      return '%s: %s' % (type(self).__name__, self.__message)
    return exceptions.Exception.__str__(self)

After calling CreateFromDocument() with an invalid XML document that contains Unicode, PyXB may raise an exception that contains Unicode, such as:

BadTypeValueError(u'pattern violation for <unicode characters here> in MyType',)

When attempting to retrieve the value of the exception, "exceptions.Exception.str(self)" throws an exception:

File "/usr/local/lib/python2.6/dist-packages/pyxb/exceptions_.py" in __str__

48. return exceptions.Exception.__str__(self)
Exception Type: UnicodeEncodeError
Exception Value: 'ascii' codec can't encode character u'\xe9' in position 81: ordinal not in range(128)

I have not been able to find a workaround that would enable me to get even an ASCII version of the exception value.

(I removed the actual Unicode characters because Trac can't handle them.)

1 Attachments

Discussion

  • Peter A. Bigot

    Peter A. Bigot - 2012-04-25
    • status changed from new to closed
    • resolution set to fixed

    AFAICT nobody uses message, so I've removed the feature allowing the underlying Python exception message infrastructure to provide access to the message. Depending on your Python version you may still have issues (see http://bugs.python.org/issue2517), but at least PyXB shouldn't be getting in the way anymore.

     
  • Peter A. Bigot

    Peter A. Bigot - 2012-04-25

    The fix is in the next branch of the git repository, or try the patch attached to this ticket.

     
  • Enno Gröper

    Enno Gröper - 2013-02-10
    • cc groeper added
    • status changed from closed to reopened
    • version changed from PyXB 1.1.3 to PyXB 1.2.1
    • resolution fixed deleted
    • milestone changed from PyXB 1.1.4 to PyXB 1.2.2

    One of my documents contains a field, which is checked against a pattern.
    When I put a UTF-8 character into the field, I should have gotten a ValidationError, but I got, when calling pyxb.ValidationError.details():

    Traceback (most recent call last):
     ...
      File "/usr/local/lib/python2.7/dist-packages/pyxb/exceptions_.py", line 183, in details
        return str(self)
    UnicodeEncodeError: 'ascii' codec can't encode character u'\xf6' in position 116: ordinal not in range(128)
    

    My calling code:

        try:
            xml = file(args.exp_file).read()
            exp = expxml.CreateFromDocument(xml)
        except pyxb.ValidationError as e:
            log.error("Error validating %s: %s"
                      % (args.exp_file, e.details()))
            sys.exit(-1)
    

    I'm using Python 2.7. I don't think this is a Python issue.

     
  • Peter A. Bigot

    Peter A. Bigot - 2013-02-10
    • status changed from reopened to closed
    • version changed from PyXB 1.2.1 to PyXB 1.1.3
    • resolution set to fixed
    • milestone changed from PyXB 1.2.2 to PyXB 1.1.4

    Please do not re-open closed tickets when reporting a new issue in new code. Especially do not change the metadata associated with the closed ticket.

    I've moved this to #190.

     
MongoDB Logo MongoDB