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

Serialize photo - AttributeError: 'bytes' object has no attribute 'encode' #59

Closed
leingang opened this issue Jan 20, 2017 · 7 comments
Closed

Comments

@leingang
Copy link

leingang commented Jan 20, 2017

Hi,

I have been using vobject 0.7.1 with python 2.7 since 2008. I am trying to update my script to python 3.5 with vobject 0.9.3.

I have a local file that I want to add to a vCard object. The enclosed code below worked in the 2.7 version but fails now.

#!/usr/bin/env python

import base64
import os
import vobject
import unittest
import urllib.request

class TestVcardPhotoSerialize(unittest.TestCase):

    def setUp(self):
        self.card=vobject.vCard()
        self.card.add('n')
        self.card.n.value=vobject.vcard.Name(family="Thecat",given="Felix")
        self.card.add('fn')
        self.card.fn.value="Felix Thecat"
        if not os.path.isfile("felix-229.png"):
            urllib.request.urlretrieve("https://upload.wikimedia.org/wikipedia/commons/thumb/0/0f/Felix_the_cat.svg/229px-Felix_the_cat.svg.png","felix-229.png")

    def test_add_photo_serialize_27(self):
        """add a photo and serialize (worked with the python 2.7 version)"""
        self.card.add('photo')
        photo_fh=open("felix-229.png",'rb')
        self.card.photo.value = photo_fh.read()
        self.card.photo.encoding_param = "b"
        self.card.photo.type_param = "PNG"
        photo_fh.close()
        print(self.card.serialize())
        # AttributeError: 'bytes' object has no attribute 'encode'

if __name__ == '__main__':
    unittest.main()

Here is the log:

(ps2vcard) 172-17-96-87:ps2vcard matthew$ ./test_vcard_photo.py
EE
======================================================================
ERROR: test_add_photo_serialize_27 (__main__.TestVcardPhotoSerialize)
add a photo and serialize (worked with the python 2.7 version)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "./test_vcard_photo.py", line 28, in test_add_photo_serialize_27
    print(self.card.serialize())
  File "/Users/matthew/Library/virtualenvs/ps2vcard/lib/python3.5/site-packages/vobject/base.py", line 255, in serialize
    return behavior.serialize(self, buf, lineLength, validate)
  File "/Users/matthew/Library/virtualenvs/ps2vcard/lib/python3.5/site-packages/vobject/behavior.py", line 162, in serialize
    out = base.defaultSerialize(transformed, buf, lineLength)
  File "/Users/matthew/Library/virtualenvs/ps2vcard/lib/python3.5/site-packages/vobject/base.py", line 993, in defaultSerialize
    child.serialize(outbuf, lineLength, validate=False)
  File "/Users/matthew/Library/virtualenvs/ps2vcard/lib/python3.5/site-packages/vobject/base.py", line 255, in serialize
    return behavior.serialize(self, buf, lineLength, validate)
  File "/Users/matthew/Library/virtualenvs/ps2vcard/lib/python3.5/site-packages/vobject/vcard.py", line 227, in serialize
    VCardTextBehavior.serialize(obj, buf, lineLength, validate)
  File "/Users/matthew/Library/virtualenvs/ps2vcard/lib/python3.5/site-packages/vobject/behavior.py", line 162, in serialize
    out = base.defaultSerialize(transformed, buf, lineLength)
  File "/Users/matthew/Library/virtualenvs/ps2vcard/lib/python3.5/site-packages/vobject/base.py", line 1001, in defaultSerialize
    obj.behavior.encode(obj)
  File "/Users/matthew/Library/virtualenvs/ps2vcard/lib/python3.5/site-packages/vobject/vcard.py", line 152, in encode
    line.value = codecs.encode(line.value.encode(coding), "base64").decode("utf-8")
AttributeError: 'bytes' object has no attribute 'encode'

What's the right way to do this now?

@wpercy
Copy link
Contributor

wpercy commented Jan 20, 2017

can you post the full traceback?

@leingang
Copy link
Author

@wpercy Sure thing. See edit.

@wpercy
Copy link
Contributor

wpercy commented Jan 20, 2017

Thanks! I'll be sure to look into it

@wpercy
Copy link
Contributor

wpercy commented Jan 20, 2017

@leingang this is fixed in the most recent release 0.9.4

@wpercy wpercy closed this as completed Jan 20, 2017
@leingang
Copy link
Author

Thanks so much. Works perfectly now.

@potuz
Copy link

potuz commented Jun 27, 2017

I still hit this error with version 0.9.4.1 and the following vcard

BEGIN:VCARD
VERSION:3.0
UID:87jdz2q60li3cjuyx7656aa2s23g6uhtyuzc
EMAIL;TYPE=home:cama@estoy.net
FN:Cama Estoy
N:Estoy;Cama;;;
PHOTO;TYPE=JPEG;ENCODING=b:/9j/4AAQSkZJRgABAQEASA......
.....
UqVKh+wnu//9k=
REV:20170626T224828Z
END:VCARD

Get the following Trace:

2017-06-26 22:50:14,037 - [747ff460] ERROR: An exception occurred during request:
Traceback (most recent call last):
  File "/usr/lib/python3.4/wsgiref/handlers.py", line 137, in run
    self.result = application(self.environ, self.start_response)
  File "/usr/lib/python3.4/site-packages/radicale/__init__.py", line 425, in __call__
    environ, base_prefix, path, user)
  File "/usr/lib/python3.4/site-packages/radicale/__init__.py", line 691, in do_PUT
    new_item = parent_item.upload(href, items[0])
  File "/usr/lib/python3.4/site-packages/radicale/storage.py", line 662, in upload
    fd.write(item.serialize())
  File "/usr/lib/python3.4/site-packages/vobject/base.py", line 255, in serialize
    else:
  File "/usr/lib/python3.4/site-packages/vobject/behavior.py", line 162, in serialize
    else:
  File "/usr/lib/python3.4/site-packages/vobject/base.py", line 993, in defaultSerialize
    if obj.group is None:
  File "/usr/lib/python3.4/site-packages/vobject/base.py", line 255, in serialize
    else:
  File "/usr/lib/python3.4/site-packages/vobject/vcard.py", line 227, in serialize
  File "/usr/lib/python3.4/site-packages/vobject/behavior.py", line 162, in serialize
    else:
  File "/usr/lib/python3.4/site-packages/vobject/base.py", line 1001, in defaultSerialize
    # validate is recursive, we only need to validate once
  File "/usr/lib/python3.4/site-packages/vobject/vcard.py", line 152, in encode
    """
AttributeError: 'bytes' object has no attribute 'encode'

@potuz
Copy link

potuz commented Jun 27, 2017

I'm sorry for the noise, It seems I hadn't restarted radicale after bumping vobject version. This issue is fine here.

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

No branches or pull requests

3 participants