skarim / vobject

A full-featured Python package for parsing and creating iCalendar and vCard files
http://eventable.github.io/vobject/
254 stars 93 forks source link

TypeError: a bytes-like object is required, not 'str' in backslashEscape function #117

Open yannc2021 opened 6 years ago

yannc2021 commented 6 years ago

Traceback (most recent call last): object.serialize() File "C:\Users\Emma Zhang\AppData\Local\Programs\Python\Python36-32\lib\site-packages\vobject\base.py", line 254, in serialize return behavior.serialize(self, buf, lineLength, validate) File "C:\Users\Emma Zhang\AppData\Local\Programs\Python\Python36-32\lib\site-packages\vobject\behavior.py", line 166, in serialize out = base.defaultSerialize(transformed, buf, lineLength) File "C:\Users\Emma Zhang\AppData\Local\Programs\Python\Python36-32\lib\site-packages\vobject\base.py", line 1007, in defaultSerialize child.serialize(outbuf, lineLength, validate=False) File "C:\Users\Emma Zhang\AppData\Local\Programs\Python\Python36-32\lib\site-packages\vobject\base.py", line 254, in serialize return behavior.serialize(self, buf, lineLength, validate) File "C:\Users\Emma Zhang\AppData\Local\Programs\Python\Python36-32\lib\site-packages\vobject\vcard.py", line 237, in serialize VCardTextBehavior.serialize(obj, buf, lineLength, validate) File "C:\Users\Emma Zhang\AppData\Local\Programs\Python\Python36-32\lib\site-packages\vobject\behavior.py", line 166, in serialize out = base.defaultSerialize(transformed, buf, lineLength) File "C:\Users\Emma Zhang\AppData\Local\Programs\Python\Python36-32\lib\site-packages\vobject\base.py", line 1015, in defaultSerialize obj.behavior.encode(obj) File "C:\Users\Emma Zhang\AppData\Local\Programs\Python\Python36-32\lib\site-packages\vobject\vcard.py", line 161, in encode line.value = backslashEscape(line.value) File "C:\Users\Emma Zhang\AppData\Local\Programs\Python\Python36-32\lib\site-packages\vobject\base.py", line 1216, in backslashEscape s = s.replace("\", "\\").replace(";", "\;").replace(",", "\,") TypeError: a bytes-like object is required, not 'str'

wpercy commented 6 years ago

Can you please provide the object that caused this error?

philip-stuckey commented 1 year ago

I'm not the original poster but I was able to reproduce this error in vobject version 0.9.6.1 and python version 3.10.6 I believe this has something to do with encoding photos in base64. My minimal working example is

import vobject

card_text_ok = '''
BEGIN:VCARD
VERSION:2.1
FN:ok
PHOTO;type=JPEG:
END:VCARD
'''

card_text_bad = '''
BEGIN:VCARD
VERSION:2.1
FN:bad
PHOTO;ENCODING=BASE64;TYPE=JPEG:
END:VCARD
'''

if __name__ == '__main__':
    card = vobject.readOne(card_text_ok)
    print(card.serialize())
    card = vobject.readOne(card_text_bad)
    print(card.serialize())

Note that I specify the ENCODING parameter in the photo property. I'm pretty sure this is valid vCard2.1

The stack trace is essentially the same except for a few different line numbers

Traceback (most recent call last):
  File "/home/philip-stuckey/Projects/contact-fs/src/err.py", line 23, in <module>
    print(card.serialize())
  File "/home/philip-stuckey/Projects/contact-fs/venv/lib/python3.10/site-packages/vobject/base.py", line 254, in serialize
    return behavior.serialize(self, buf, lineLength, validate)
  File "/home/philip-stuckey/Projects/contact-fs/venv/lib/python3.10/site-packages/vobject/behavior.py", line 166, in serialize
    out = base.defaultSerialize(transformed, buf, lineLength)
  File "/home/philip-stuckey/Projects/contact-fs/venv/lib/python3.10/site-packages/vobject/base.py", line 1007, in defaultSerialize
    child.serialize(outbuf, lineLength, validate=False)
  File "/home/philip-stuckey/Projects/contact-fs/venv/lib/python3.10/site-packages/vobject/base.py", line 254, in serialize
    return behavior.serialize(self, buf, lineLength, validate)
  File "/home/philip-stuckey/Projects/contact-fs/venv/lib/python3.10/site-packages/vobject/vcard.py", line 237, in serialize
    VCardTextBehavior.serialize(obj, buf, lineLength, validate)
  File "/home/philip-stuckey/Projects/contact-fs/venv/lib/python3.10/site-packages/vobject/behavior.py", line 166, in serialize
    out = base.defaultSerialize(transformed, buf, lineLength)
  File "/home/philip-stuckey/Projects/contact-fs/venv/lib/python3.10/site-packages/vobject/base.py", line 1015, in defaultSerialize
    obj.behavior.encode(obj)
  File "/home/philip-stuckey/Projects/contact-fs/venv/lib/python3.10/site-packages/vobject/vcard.py", line 161, in encode
    line.value = backslashEscape(line.value)
  File "/home/philip-stuckey/Projects/contact-fs/venv/lib/python3.10/site-packages/vobject/base.py", line 1219, in backslashEscape
    s = s.replace("\\", "\\\\").replace(";", "\;").replace(",", "\,")
TypeError: a bytes-like object is required, not 'str'