ewsterrenburg / python-otrs

Pythonic interface to OTRS SOAP API
GNU General Public License v3.0
47 stars 27 forks source link

Unicode codec error #16

Closed dtrudg closed 8 years ago

dtrudg commented 8 years ago

When using python-otrs to submit an article where the text contains unicode (\u210c in this case), the following error occurs:

python-otrs

Apologies for screenshot instead of text

Looking in objects.py around line 103, I don't really understand what's going on here.

 def to_xml(self):
        """
        @returns am etree.Element
        """
        root = etree.Element(self.XML_NAME)
        for k, v in self.attrs.items():
            e = etree.Element(k)
            if isinstance(e, str):  #  True
                v = v.encode('utf-8')
            e.text = str(v)
            root.append(e)
        return root

The # True comment here suggests that v is always being encoded as utf-8, but then str(v) is applied where an encoding error is being raised.

Not sure whether this is a bug, or there is a certain way in which python-otrs is should be passed unicode content?

Thanks,

ewsterrenburg commented 8 years ago

Thanks for reporting. This is most likely a bug.... Could you please check for me if it works correctly if you use the following: e.text = unicode(v)

Erwin

dtrudg commented 8 years ago

Confirm that change fixes the issue - it accepts unicode strings now, and they go into OTRS as expected.

diff --git a/otrs/objects.py b/otrs/objects.py
index b75bef4..fd7fb8f 100644
--- a/otrs/objects.py
+++ b/otrs/objects.py
@@ -100,7 +100,7 @@ class OTRSObject(object):
             e = etree.Element(k)
             if isinstance(e, str):  #  True
                 v = v.encode('utf-8')
-            e.text = str(v)
+            e.text = unicode(v)
             root.append(e)
         return root