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

Trailing newline in base64 encoded data #930

Closed
xl0 opened this issue Feb 12, 2023 · 0 comments
Closed

Trailing newline in base64 encoded data #930

xl0 opened this issue Feb 12, 2023 · 0 comments

Comments

@xl0
Copy link
Contributor

xl0 commented Feb 12, 2023

I've noticed that Jupyter notebook will append a "\n" to any base64 encoded data in the output of the cell. This does not cause problems, except that VScode-jupyter strips the "\n".

This results in excessively large diffs when Jupyter notebook and vsccde-jupyter are used on the same codebase. I understand that Jupyter was there first, but to me not having the extra "\n" feels like a better solution than having it. Unless I'm missing a good reason for having it, would it be possible to not add this trailing "\n"?

In my understanding the "\n" is introduced by calling b2a_base64 from session.py:json_default:

def json_default(obj):
    [...]
    if isinstance(obj, bytes):
        return b2a_base64(obj).decode('ascii')
    [...]

Since python 3.6 b2a_base64() accepts a newline=True argument.
The description of the function in 3.5 documentation is:

binascii.b2a_base64(data)
Convert binary data to a line of ASCII characters in base64 coding. The return value is the converted line, including a newline char.
The newline is added because the original use case for this function was to feed it a series of 57 byte input lines to get output lines
that conform to the MIME-base64 standard. Otherwise the output conforms to RFC 3548.

Would it be possible to not include that extra newline by passing newline=False to b2a_base64()?

To reptoduce:

from PIL import Image
Image.new("RGB", (10, 10), "green")

results in

   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAAoAAAAKCAIAAAACUFjqAAAAE0lEQVR4nGNkaGDAA5jwSY5caQCnUgCUBZU3vQAAAABJRU5ErkJggg==\n",
      "text/plain": [       "<PIL.Image.Image image mode=RGB size=10x10>"      ]     },
    ...
Carreau added a commit to ipython/ipython that referenced this issue Feb 13, 2023
…13941)

This addresses the issue highlighted in

jupyter/jupyter_client#930

Since, unlike the latest Jupyter, IPython aims to support python<3.6,
I'm using an approach that won't break this compatibility.
@xl0 xl0 closed this as completed Feb 17, 2023
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

1 participant