#55 UnicodeDecodeError: 'utf8' codec can't decode

closed-fixed
Waseem Daher
None
5
2008-11-08
2008-08-01
No

With simple example we get "UnicodeDecodeError: 'utf8' codec can't decode" error when reading email content.

See message in Forum "UnicodeDecodeError: 'utf8' codec can't decode" for further information

Discussion

  • Waseem Daher
    Waseem Daher
    2008-08-02

    Logged In: YES
    user_id=338872
    Originator: NO

    Hi. I can't seem to find your forum message -- can you attach an example or something that might help me debug the problem here, on the bug? Thanks.

     
  • Trevis
    Trevis
    2008-09-01

    Logged In: YES
    user_id=1370113
    Originator: NO

    I get an error very similar to this when attempting to get an attachment's contents, probably related.

    CODE:
    #!/usr/bin/python
    import sys
    import libgmail

    def checkmail():
    sentfrom = 0
    ga = libgmail.GmailAccount('email@gmail.com', 'pass')
    ga.login()
    folder = ga.getUnreadMessages()
    for thread in folder:
    for msg in thread:
    for attachment in msg.attachments:
    f=open('/tmp/' + attachment.filename, "wb")
    print "attempting to write out attachment to /tmp/" + attachment.filename
    f.write(attachment.content)
    f.close()
    sys.exit()

    checkmail()

    OUTPUT:
    pbot@haxit ~/code $ ./test.py
    attempting to write out attachment to /tmp/photo.jpg
    Traceback (most recent call last):
    File "./test.py", line 19, in <module>
    checkmail()
    File "./test.py", line 15, in checkmail
    f.write(attachment.content)
    File "/usr/lib64/python2.5/site-packages/libgmail.py", line 1507, in _getContent
    attid=self.id, th=self._parent._parent.id))
    File "/usr/lib64/python2.5/site-packages/libgmail.py", line 358, in _retrievePage
    return pageData.decode('utf-8')
    File "/usr/lib64/python2.5/encodings/utf_8.py", line 16, in decode
    return codecs.utf_8_decode(input, errors, True)
    UnicodeDecodeError: 'utf8' codec can't decode byte 0xff in position 0: unexpected code byte

    I tried using the latest release and the latest cvs, both give the same error.

     
  • I had this same issue. I solved it by replacing "return pageData.decode("utf-8")" in libgmail.py with "return pageData". In other words, don't convert some binary file to a unicode string...

     
  • Waseem Daher
    Waseem Daher
    2008-11-08

    • assigned_to: nobody --> wdaher
    • status: open --> closed-fixed
     
  • Waseem Daher
    Waseem Daher
    2008-11-08

    Fixed in CVS. Please check out the latest version; if it still doesn't work for you there, let us know.