Skip to content

move _encode_binary to jsonutil.encode_images #2048

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

Merged
merged 2 commits into from
Jun 28, 2012

Conversation

minrk
Copy link
Member

@minrk minrk commented Jun 28, 2012

And fix identification of PNG/JPEG data, which could be double-encoded when using a JSON library that decodes to str instead of unicode (simplejson 2.6, for instance).

The function is also moved to jsonutil.

Changes are tested.

This is critical for 0.13, because the parallel magics cannot republish images without it, depending on whether simplejson is present.

And fix identification of PNG/JPEG data, which could be double-encoded when using a JSON library that decodes to str instead of unicode (simplejson 2.6, for instance).

Changes are tested.
@minrk
Copy link
Member Author

minrk commented Jun 28, 2012

More specifics:

_encode_binary checked if the data was bytes (str on Python 2). However, this data could still be base64-encoded str if simplejson is in use, rather than unicode, in which case the republishing would double-encode the data. This, instead, actually checks the first few bytes to see if these are in fact jpeg/png data.

Some alternatives:

  1. handle this in json_clean, so that images anywhere in any message will be b64-encoded
  2. reverse the test, so that rather than checking the first few bytes of data against proper PNG/JPG, check them against b64-encoded PNG/JPEG. The main difference being how garbage data would be handled.

@fperez
Copy link
Member

fperez commented Jun 28, 2012

Test results for commit 0014248 merged into master
Platform: linux2

  • python2.7: OK
  • python3.2: OK (libraries not available: cython matplotlib oct2py pygments pymongo rpy2 wx wx.aui)

Not available for testing: python2.6, python3.1

JPEG = b'\xff\xd8'

def encode_images(format_dict):
"""b64-encodes images in a displaypub format dict
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this is now public, the docstring should explain what format_dict is meant to contain.

@fperez
Copy link
Member

fperez commented Jun 28, 2012

Tiny docstring fix, otherwise looks good. thanks!

@minrk
Copy link
Member Author

minrk commented Jun 28, 2012

Thanks - docstring updated, merging.

minrk added a commit that referenced this pull request Jun 28, 2012
move _encode_binary to jsonutil.encode_images

and avoid double-encoding image data
@minrk minrk merged commit 2e54c6f into ipython:master Jun 28, 2012
@minrk minrk deleted the encode_images branch March 31, 2014 23:36
mattvonrocketstein pushed a commit to mattvonrocketstein/ipython that referenced this pull request Nov 3, 2014
move _encode_binary to jsonutil.encode_images

and avoid double-encoding image data
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants