Skip to content

Conversation

@dreua
Copy link

@dreua dreua commented May 27, 2024

Not sure if I understood everything correctly here but this works for me. Might update this PR after some more testing.

@dreua
Copy link
Author

dreua commented May 27, 2024

I don't think there is any test file with a "PHOTO" in it, is it?

@dreua
Copy link
Author

dreua commented May 27, 2024

Here is the stacktrace without the patch:

Traceback (most recent call last):
  File "/usr/lib64/python3.12/runpy.py", line 198, in _run_module_as_main
    return _run_code(code, main_globals, None,
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib64/python3.12/runpy.py", line 88, in _run_code
    exec(code, run_globals)
  File "/home/da/.vscode/extensions/ms-python.debugpy-2024.6.0-linux-x64/bundled/libs/debugpy/adapter/../../debugpy/launcher/../../debugpy/__main__.py", line 39, in <module>
    cli.main()
  File "/home/da/.vscode/extensions/ms-python.debugpy-2024.6.0-linux-x64/bundled/libs/debugpy/adapter/../../debugpy/launcher/../../debugpy/../debugpy/server/cli.py", line 430, in main
    run()
  File "/home/da/.vscode/extensions/ms-python.debugpy-2024.6.0-linux-x64/bundled/libs/debugpy/adapter/../../debugpy/launcher/../../debugpy/../debugpy/server/cli.py", line 284, in run_file
    runpy.run_path(target, run_name="__main__")
  File "/home/da/.vscode/extensions/ms-python.debugpy-2024.6.0-linux-x64/bundled/libs/debugpy/_vendored/pydevd/_pydevd_bundle/pydevd_runpy.py", line 321, in run_path
    return _run_module_code(code, init_globals, run_name,
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/da/.vscode/extensions/ms-python.debugpy-2024.6.0-linux-x64/bundled/libs/debugpy/_vendored/pydevd/_pydevd_bundle/pydevd_runpy.py", line 135, in _run_module_code
    _run_code(code, mod_globals, init_globals,
  File "/home/da/.vscode/extensions/ms-python.debugpy-2024.6.0-linux-x64/bundled/libs/debugpy/_vendored/pydevd/_pydevd_bundle/pydevd_runpy.py", line 124, in _run_code
    exec(code, run_globals)

[My code just loading a contact from file with an attached photo and then calling serialize on it.]

  File "/home/da/.local/lib/python3.12/site-packages/vobject/base.py", line 254, in serialize
    return behavior.serialize(self, buf, lineLength, validate, *args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/da/.local/lib/python3.12/site-packages/vobject/behavior.py", line 166, in serialize
    out = base.defaultSerialize(transformed, buf, lineLength)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/da/.local/lib/python3.12/site-packages/vobject/base.py", line 1007, in defaultSerialize
    child.serialize(outbuf, lineLength, validate=False)
  File "/home/da/.local/lib/python3.12/site-packages/vobject/base.py", line 254, in serialize
    return behavior.serialize(self, buf, lineLength, validate, *args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/da/.local/lib/python3.12/site-packages/vobject/vcard.py", line 231, in serialize
    VCardTextBehavior.serialize(obj, buf, lineLength, validate, *args, **kwargs)
  File "/home/da/.local/lib/python3.12/site-packages/vobject/behavior.py", line 166, in serialize
    out = base.defaultSerialize(transformed, buf, lineLength)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/da/.local/lib/python3.12/site-packages/vobject/base.py", line 1015, in defaultSerialize
    obj.behavior.encode(obj)
  File "/home/da/.local/lib/python3.12/site-packages/vobject/vcard.py", line 155, in encode
    line.value = backslashEscape(line.value)
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/da/.local/lib/python3.12/site-packages/vobject/base.py", line 1219, in backslashEscape
    s = s.replace("\\", "\\\\").replace(";", "\\;").replace(",", "\\,")
        ^^^^^^^^^^^^^^^^^^^^^^^
TypeError: a bytes-like object is required, not 'str'

@dreua
Copy link
Author

dreua commented May 27, 2024

In a first step I tried to just ignore byte objects in "backslashEscape" but it still failed when serializing:


Traceback (most recent call last):
[...]
    pprint(vo.serialize())
           ^^^^^^^^^^^^^^
  File "/home/da/.local/lib/python3.12/site-packages/vobject/base.py", line 254, in serialize
    return behavior.serialize(self, buf, lineLength, validate, *args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/da/.local/lib/python3.12/site-packages/vobject/behavior.py", line 166, in serialize
    out = base.defaultSerialize(transformed, buf, lineLength)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/da/.local/lib/python3.12/site-packages/vobject/base.py", line 1007, in defaultSerialize
    child.serialize(outbuf, lineLength, validate=False)
  File "/home/da/.local/lib/python3.12/site-packages/vobject/base.py", line 254, in serialize
    return behavior.serialize(self, buf, lineLength, validate, *args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/da/.local/lib/python3.12/site-packages/vobject/vcard.py", line 231, in serialize
    VCardTextBehavior.serialize(obj, buf, lineLength, validate, *args, **kwargs)
  File "/home/da/.local/lib/python3.12/site-packages/vobject/behavior.py", line 166, in serialize
    out = base.defaultSerialize(transformed, buf, lineLength)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/da/.local/lib/python3.12/site-packages/vobject/base.py", line 1034, in defaultSerialize
    obj.behavior.decode(obj)
  File "/home/da/.local/lib/python3.12/site-packages/vobject/vcard.py", line 135, in decode
    line.value = codecs.decode(line.value, "base64")
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib64/python3.12/encodings/base64_codec.py", line 19, in base64_decode
    return (base64.decodebytes(input), len(input))
            ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib64/python3.12/base64.py", line 554, in decodebytes
    return binascii.a2b_base64(s)
           ^^^^^^^^^^^^^^^^^^^^^^
binascii.Error: Invalid base64-encoded string: number of data characters (445) cannot be 1 more than a multiple of 4
decoding with 'base64' codec failed

@dreua dreua force-pushed the fix-base64-serialization branch from 863e5ac to 617ac78 Compare May 27, 2024 17:19
@dreua
Copy link
Author

dreua commented May 27, 2024

Rebased on top of testfile and test

@da4089
Copy link

da4089 commented May 27, 2024

Thanks for the PR!

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

Successfully merging this pull request may close these issues.

2 participants