Skip to content

Encountered an interesting issue with vobject. Calling 'v.serialize()' multiple times results in unexpected errors. #84

@Gerald-Meyers

Description

@Gerald-Meyers

So I was modifying a vcf file I made on my phone and just trying out the vobject module to do so. I was trying to save a file but kept getting type errors pointing back to vobject's base.py.

'Gerald Meyers'
TypeError: a bytes-like object is required, not 'str'
v.serialize() output:

TypeError Traceback (most recent call last)
Cell In[14], line 4
2 with open(my_card, 'wb') as card:
3 # card.write(serialized.encode('utf-8'))
----> 4 card.write(v.serialize().encode('utf-8'))
5 except TypeError as e:

File c:\ProgramData\miniconda3\lib\site-packages\vobject\base.py:254, in VBase.serialize(self, buf, lineLength, validate, behavior)
253 logger.debug("serializing {0!s} with behavior {1!s}".format(self.name, behavior))
--> 254 return behavior.serialize(self, buf, lineLength, validate)
255 else:

File c:\ProgramData\miniconda3\lib\site-packages\vobject\behavior.py:166, in Behavior.serialize(cls, obj, buf, lineLength, validate)
164 undoTransform = False
--> 166 out = base.defaultSerialize(transformed, buf, lineLength)
167 if undoTransform:

File c:\ProgramData\miniconda3\lib\site-packages\vobject\base.py:1007, in defaultSerialize(obj, buf, lineLength)
1005 for child in obj.getSortedChildren():
1006 # validate is recursive, we only need to validate once
-> 1007 child.serialize(outbuf, lineLength, validate=False)
1008 if obj.useBegin:

File c:\ProgramData\miniconda3\lib\site-packages\vobject\base.py:254, in VBase.serialize(self, buf, lineLength, validate, behavior)
253 logger.debug("serializing {0!s} with behavior {1!s}".format(self.name, behavior))
--> 254 return behavior.serialize(self, buf, lineLength, validate)
255 else:

File c:\ProgramData\miniconda3\lib\site-packages\vobject\vcard.py:237, in Photo.serialize(cls, obj, buf, lineLength, validate)
236 lineLength = REALLY_LARGE
--> 237 VCardTextBehavior.serialize(obj, buf, lineLength, validate)

File c:\ProgramData\miniconda3\lib\site-packages\vobject\behavior.py:166, in Behavior.serialize(cls, obj, buf, lineLength, validate)
164 undoTransform = False
--> 166 out = base.defaultSerialize(transformed, buf, lineLength)
167 if undoTransform:

File c:\ProgramData\miniconda3\lib\site-packages\vobject\base.py:1015, in defaultSerialize(obj, buf, lineLength)
1014 if obj.behavior and not startedEncoded:
-> 1015 obj.behavior.encode(obj)
1017 s = six.StringIO()

File c:\ProgramData\miniconda3\lib\site-packages\vobject\vcard.py:161, in VCardTextBehavior.encode(cls, line)
160 else:
--> 161 line.value = backslashEscape(line.value)
162 line.encoded = True

File c:\ProgramData\miniconda3\lib\site-packages\vobject\base.py:1219, in backslashEscape(s)
1218 def backslashEscape(s):
-> 1219 s = s.replace("\", "\\").replace(";", ";").replace(",", ",")
1220 return s.replace("\r\n", "\n").replace("\n", "\n").replace("\r", "\n")

TypeError: a bytes-like object is required, not 'str'

During handling of the above exception, another exception occurred:

TypeError Traceback (most recent call last)
Cell In[14], line 8
6 print(f"TypeError: {e}")
7 print("v.serialize() output:")
----> 8 print(v.serialize())

File c:\ProgramData\miniconda3\lib\site-packages\vobject\base.py:254, in VBase.serialize(self, buf, lineLength, validate, behavior)
252 if DEBUG:
253 logger.debug("serializing {0!s} with behavior {1!s}".format(self.name, behavior))
--> 254 return behavior.serialize(self, buf, lineLength, validate)
255 else:
256 if DEBUG:

File c:\ProgramData\miniconda3\lib\site-packages\vobject\behavior.py:166, in Behavior.serialize(cls, obj, buf, lineLength, validate)
163 transformed = obj
164 undoTransform = False
--> 166 out = base.defaultSerialize(transformed, buf, lineLength)
167 if undoTransform:
168 obj.transformToNative()

File c:\ProgramData\miniconda3\lib\site-packages\vobject\base.py:1007, in defaultSerialize(obj, buf, lineLength)
1003 foldOneLine(outbuf, "{0}BEGIN:{1}".format(groupString, obj.name),
1004 lineLength)
1005 for child in obj.getSortedChildren():
1006 # validate is recursive, we only need to validate once
-> 1007 child.serialize(outbuf, lineLength, validate=False)
1008 if obj.useBegin:
1009 foldOneLine(outbuf, "{0}END:{1}".format(groupString, obj.name),
1010 lineLength)

File c:\ProgramData\miniconda3\lib\site-packages\vobject\base.py:254, in VBase.serialize(self, buf, lineLength, validate, behavior)
252 if DEBUG:
253 logger.debug("serializing {0!s} with behavior {1!s}".format(self.name, behavior))
--> 254 return behavior.serialize(self, buf, lineLength, validate)
255 else:
256 if DEBUG:

File c:\ProgramData\miniconda3\lib\site-packages\vobject\vcard.py:237, in Photo.serialize(cls, obj, buf, lineLength, validate)
235 if wacky_apple_photo_serialize:
236 lineLength = REALLY_LARGE
--> 237 VCardTextBehavior.serialize(obj, buf, lineLength, validate)

File c:\ProgramData\miniconda3\lib\site-packages\vobject\behavior.py:166, in Behavior.serialize(cls, obj, buf, lineLength, validate)
163 transformed = obj
164 undoTransform = False
--> 166 out = base.defaultSerialize(transformed, buf, lineLength)
167 if undoTransform:
168 obj.transformToNative()

File c:\ProgramData\miniconda3\lib\site-packages\vobject\base.py:1015, in defaultSerialize(obj, buf, lineLength)
1013 startedEncoded = obj.encoded
1014 if obj.behavior and not startedEncoded:
-> 1015 obj.behavior.encode(obj)
1017 s = six.StringIO()
1019 if obj.group is not None:

File c:\ProgramData\miniconda3\lib\site-packages\vobject\vcard.py:161, in VCardTextBehavior.encode(cls, line)
159 line.value = codecs.encode(line.value.encode(encoding), "base64").decode("utf-8")
160 else:
--> 161 line.value = backslashEscape(line.value)
162 line.encoded = True

File c:\ProgramData\miniconda3\lib\site-packages\vobject\base.py:1219, in backslashEscape(s)
1218 def backslashEscape(s):
-> 1219 s = s.replace("\", "\\").replace(";", ";").replace(",", ",")
1220 return s.replace("\r\n", "\n").replace("\n", "\n").replace("\r", "\n")

TypeError: a bytes-like object is required, not 'str'
vobject.base.ContentLine

Metadata

Metadata

Assignees

Labels

bugSomething isn't workingneeds testNeeds unit tests before mergingtech debtNeeds cleanup or modernisation

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions