diff --git a/notebook/files/handlers.py b/notebook/files/handlers.py index 7cf6fc64b..318d4887e 100644 --- a/notebook/files/handlers.py +++ b/notebook/files/handlers.py @@ -3,10 +3,14 @@ # Copyright (c) Jupyter Development Team. # Distributed under the terms of the Modified BSD License. -import os import mimetypes import json -import base64 + +try: #PY3 + from base64 import decodebytes +except ImportError: #PY2 + from base64 import decodestring as decodebytes + from tornado import web @@ -53,7 +57,7 @@ class FilesHandler(IPythonHandler): if include_body: if model['format'] == 'base64': b64_bytes = model['content'].encode('ascii') - self.write(base64.decodestring(b64_bytes)) + self.write(decodebytes(b64_bytes)) elif model['format'] == 'json': self.write(json.dumps(model['content'])) else: diff --git a/notebook/nbconvert/tests/test_nbconvert_handlers.py b/notebook/nbconvert/tests/test_nbconvert_handlers.py index b72400958..2428e61bc 100644 --- a/notebook/nbconvert/tests/test_nbconvert_handlers.py +++ b/notebook/nbconvert/tests/test_nbconvert_handlers.py @@ -1,5 +1,4 @@ # coding: utf-8 -import base64 import io import json import os @@ -17,6 +16,13 @@ from nbformat.v4 import ( from ipython_genutils.testing.decorators import onlyif_cmds_exist +try: #PY3 + from base64 import encodebytes +except ImportError: #PY2 + from base64 import encodestring as encodebytes + + + class NbconvertAPI(object): """Wrapper for nbconvert API calls.""" @@ -42,7 +48,7 @@ class NbconvertAPI(object): def list_formats(self): return self._req('GET', '') -png_green_pixel = base64.encodestring(b'\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00' +png_green_pixel = encodebytes(b'\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00' b'\x00\x00\x01\x00\x00x00\x01\x08\x02\x00\x00\x00\x90wS\xde\x00\x00\x00\x0cIDAT' b'\x08\xd7c\x90\xfb\xcf\x00\x00\x02\\\x01\x1e.~d\x87\x00\x00\x00\x00IEND\xaeB`\x82' ).decode('ascii') diff --git a/notebook/notebookapp.py b/notebook/notebookapp.py index ef5a3a683..2c33f3c12 100644 --- a/notebook/notebookapp.py +++ b/notebook/notebookapp.py @@ -6,7 +6,6 @@ from __future__ import absolute_import, print_function -import base64 import datetime import errno import importlib @@ -23,6 +22,12 @@ import sys import threading import webbrowser +try: #PY3 + from base64 import encodebytes +except ImportError: #PY2 + from base64 import encodestring as encodebytes + + from jinja2 import Environment, FileSystemLoader # Install the pyzmq ioloop. This has to be done before anything else from @@ -504,7 +509,7 @@ class NotebookApp(JupyterApp): with io.open(self.cookie_secret_file, 'rb') as f: return f.read() else: - secret = base64.encodestring(os.urandom(1024)) + secret = encodebytes(os.urandom(1024)) self._write_cookie_secret_file(secret) return secret diff --git a/notebook/services/contents/fileio.py b/notebook/services/contents/fileio.py index 9dd283572..4827b0725 100644 --- a/notebook/services/contents/fileio.py +++ b/notebook/services/contents/fileio.py @@ -5,7 +5,6 @@ Utilities for file-based Contents/Checkpoints managers. # Copyright (c) Jupyter Development Team. # Distributed under the terms of the Modified BSD License. -import base64 from contextlib import contextmanager import errno import io @@ -25,6 +24,12 @@ from ipython_genutils.py3compat import str_to_unicode from traitlets.config import Configurable from traitlets import Bool +try: #PY3 + from base64 import encodebytes, decodebytes +except ImportError: #PY2 + from base64 import encodestring as encodebytes, decodestring as decodebytes + + def copy2_safe(src, dst, log=None): """copy src to dst @@ -281,7 +286,7 @@ class FileManagerMixin(Configurable): "%s is not UTF-8 encoded" % os_path, reason='bad format', ) - return base64.encodestring(bcontent).decode('ascii'), 'base64' + return encodebytes(bcontent).decode('ascii'), 'base64' def _save_file(self, os_path, content, format): """Save content of a generic file.""" @@ -295,7 +300,7 @@ class FileManagerMixin(Configurable): bcontent = content.encode('utf8') else: b64_bytes = content.encode('ascii') - bcontent = base64.decodestring(b64_bytes) + bcontent = decodebytes(b64_bytes) except Exception as e: raise HTTPError( 400, u'Encoding error saving %s: %s' % (os_path, e) diff --git a/notebook/services/contents/tests/test_contents_api.py b/notebook/services/contents/tests/test_contents_api.py index 800a3a20f..e77ac89e1 100644 --- a/notebook/services/contents/tests/test_contents_api.py +++ b/notebook/services/contents/tests/test_contents_api.py @@ -1,7 +1,6 @@ # coding: utf-8 """Test the contents webservice API.""" -import base64 from contextlib import contextmanager import io import json @@ -26,6 +25,12 @@ from nbformat import v2 from ipython_genutils import py3compat from ipython_genutils.tempdir import TemporaryDirectory +try: #PY3 + from base64 import encodebytes, decodebytes +except ImportError: #PY2 + from base64 import encodestring as encodebytes, decodestring as decodebytes + + def uniq_stable(elems): """uniq_stable(elems) -> list @@ -320,7 +325,7 @@ class APITest(NotebookTestBase): self.assertEqual(model['format'], 'base64') self.assertEqual(model['type'], 'file') self.assertEqual( - base64.decodestring(model['content'].encode('ascii')), + decodebytes(model['content'].encode('ascii')), self._blob_for_name(name), ) @@ -415,7 +420,7 @@ class APITest(NotebookTestBase): def test_upload_b64(self): body = b'\xFFblob' - b64body = base64.encodestring(body).decode('ascii') + b64body = encodebytes(body).decode('ascii') model = { 'content' : b64body, 'format' : 'base64', @@ -430,7 +435,7 @@ class APITest(NotebookTestBase): self.assertEqual(model['type'], 'file') self.assertEqual(model['path'], path) self.assertEqual(model['format'], 'base64') - decoded = base64.decodestring(model['content'].encode('ascii')) + decoded = decodebytes(model['content'].encode('ascii')) self.assertEqual(decoded, body) def test_upload_v2(self):