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

Partial #6153 #4463 : reduce ipython_genutils usage #6214

Open
wants to merge 3 commits into
base: 6.4.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 7 additions & 7 deletions docs/source/public_server.rst
Expand Up @@ -137,7 +137,7 @@ You can then add the hashed password to your
:file:`jupyter_notebook_config.py` is in your Jupyter folder in your home
directory, ``~/.jupyter``, e.g.::

c.NotebookApp.password = u'sha1:67c9e60bb8b6:9ffede0825894254b2e042ea597d771089e11aed'
c.NotebookApp.password = 'sha1:67c9e60bb8b6:9ffede0825894254b2e042ea597d771089e11aed'

Automatic password setup will store the hash in ``jupyter_notebook_config.json``
while this method stores the hash in ``jupyter_notebook_config.py``. The ``.json``
Expand Down Expand Up @@ -210,11 +210,11 @@ following::

# Set options for certfile, ip, password, and toggle off
# browser auto-opening
c.NotebookApp.certfile = u'/absolute/path/to/your/certificate/mycert.pem'
c.NotebookApp.keyfile = u'/absolute/path/to/your/certificate/mykey.key'
c.NotebookApp.certfile = '/absolute/path/to/your/certificate/mycert.pem'
c.NotebookApp.keyfile = '/absolute/path/to/your/certificate/mykey.key'
# Set ip to '*' to bind on all interfaces (ips) for the public server
c.NotebookApp.ip = '*'
c.NotebookApp.password = u'sha1:bcd259ccf...<your hashed password here>'
c.NotebookApp.password = 'sha1:bcd259ccf...<your hashed password here>'
c.NotebookApp.open_browser = False

# It is a good idea to set a known, fixed port for server access
Expand Down Expand Up @@ -249,11 +249,11 @@ following::

# Set options for certfile, ip, password, and toggle off
# browser auto-opening
c.NotebookApp.certfile = u'/absolute/path/to/your/certificate/fullchain.pem'
c.NotebookApp.keyfile = u'/absolute/path/to/your/certificate/privkey.pem'
c.NotebookApp.certfile = '/absolute/path/to/your/certificate/fullchain.pem'
c.NotebookApp.keyfile = '/absolute/path/to/your/certificate/privkey.pem'
# Set ip to '*' to bind on all interfaces (ips) for the public server
c.NotebookApp.ip = '*'
c.NotebookApp.password = u'sha1:bcd259ccf...<your hashed password here>'
c.NotebookApp.password = 'sha1:bcd259ccf...<your hashed password here>'
c.NotebookApp.open_browser = False

# It is a good idea to set a known, fixed port for server access
Expand Down
8 changes: 4 additions & 4 deletions notebook/_sysinfo.py
Expand Up @@ -11,7 +11,7 @@
import sys
import subprocess

from ipython_genutils import py3compat, encoding
from ipython_genutils import encoding

import notebook

Expand Down Expand Up @@ -55,10 +55,10 @@ def pkg_commit_hash(pkg_path):
if repo_commit:
return 'repository', repo_commit.strip().decode('ascii')
else:
return u'', u''
return '', ''
par_path = p.dirname(par_path)
return u'', u''

return '', ''


def pkg_info(pkg_path):
Expand Down
6 changes: 3 additions & 3 deletions notebook/auth/login.py
Expand Up @@ -75,8 +75,8 @@ def passwd_check(self, a, b):
return passwd_check(a, b)

def post(self):
typed_password = self.get_argument('password', default=u'')
new_password = self.get_argument('new_password', default=u'')
typed_password = self.get_argument('password', default='')
new_password = self.get_argument('new_password', default='')



Expand Down Expand Up @@ -245,7 +245,7 @@ def password_from_settings(cls, settings):

If there is no configured password, an empty string will be returned.
"""
return settings.get('password', u'')
return settings.get('password', '')

@classmethod
def get_login_available(cls, settings):
Expand Down
9 changes: 4 additions & 5 deletions notebook/auth/security.py
Expand Up @@ -12,7 +12,6 @@
import traceback
import warnings

from ipython_genutils.py3compat import cast_bytes, str_to_bytes, cast_unicode
from traitlets.config import Config, ConfigFileNotFound, JSONFileConfigLoader
from jupyter_core.paths import jupyter_config_dir

Expand Down Expand Up @@ -68,11 +67,11 @@ def passwd(passphrase=None, algorithm='argon2'):
)
h = ph.hash(passphrase)

return ':'.join((algorithm, cast_unicode(h, 'ascii')))
return ':'.join((algorithm, h))
else:
h = hashlib.new(algorithm)
salt = ('%0' + str(salt_len) + 'x') % random.getrandbits(4 * salt_len)
h.update(cast_bytes(passphrase, 'utf-8') + str_to_bytes(salt, 'ascii'))
h.update(passphrase.encode('utf-8') + salt.encode('ascii'))

return ':'.join((algorithm, salt, h.hexdigest()))

Expand Down Expand Up @@ -127,7 +126,7 @@ def passwd_check(hashed_passphrase, passphrase):
if len(pw_digest) == 0:
return False

h.update(cast_bytes(passphrase, 'utf-8') + cast_bytes(salt, 'ascii'))
h.update(passphrase.encode('utf-8') + salt.encode('ascii'))

return h.hexdigest() == pw_digest

Expand All @@ -153,7 +152,7 @@ def persist_config(config_file=None, mode=0o600):
yield config

with io.open(config_file, 'w', encoding='utf8') as f:
f.write(cast_unicode(json.dumps(config, indent=2)))
f.write(json.dumps(config, indent=2))

try:
os.chmod(config_file, mode)
Expand Down
10 changes: 5 additions & 5 deletions notebook/auth/tests/test_security.py
Expand Up @@ -18,8 +18,8 @@ def test_bad():

def test_passwd_check_unicode():
# GH issue #4524
phash = u'sha1:23862bc21dd3:7a415a95ae4580582e314072143d9c382c491e4f'
assert passwd_check(phash, u"łe¶ŧ←↓→")
phash = (u'argon2:$argon2id$v=19$m=10240,t=10,p=8$'
u'qjjDiZUofUVVnrVYxacnbA$l5pQq1bJ8zglGT2uXP6iOg')
assert passwd_check(phash, u"łe¶ŧ←↓→")
phash = 'sha1:23862bc21dd3:7a415a95ae4580582e314072143d9c382c491e4f'
assert passwd_check(phash, "łe¶ŧ←↓→")
phash = ('argon2:$argon2id$v=19$m=10240,t=10,p=8$'
'qjjDiZUofUVVnrVYxacnbA$l5pQq1bJ8zglGT2uXP6iOg')
assert passwd_check(phash, "łe¶ŧ←↓→")
7 changes: 3 additions & 4 deletions notebook/base/handlers.py
Expand Up @@ -27,7 +27,6 @@

from traitlets.config import Application
from ipython_genutils.path import filefind
from ipython_genutils.py3compat import string_types

import notebook
from notebook._tz import utcnow
Expand Down Expand Up @@ -543,13 +542,13 @@ def get_json_body(self):
if not self.request.body:
return None
# Do we need to call body.decode('utf-8') here?
body = self.request.body.strip().decode(u'utf-8')
body = self.request.body.strip().decode('utf-8')
try:
model = json.loads(body)
except Exception as e:
self.log.debug("Bad JSON: %r", body)
self.log.error("Couldn't parse JSON", exc_info=True)
raise web.HTTPError(400, u'Invalid JSON in body of request') from e
raise web.HTTPError(400, 'Invalid JSON in body of request') from e
return model

def write_error(self, status_code, **kwargs):
Expand Down Expand Up @@ -807,7 +806,7 @@ def set_headers(self):
def initialize(self, path, default_filename=None, no_cache_paths=None):
self.no_cache_paths = no_cache_paths or []

if isinstance(path, string_types):
if isinstance(path, str):
path = [path]

self.root = tuple(
Expand Down
5 changes: 2 additions & 3 deletions notebook/base/zmqhandlers.py
Expand Up @@ -20,7 +20,6 @@
from jupyter_client.jsonutil import (
date_default as json_default, extract_dates
)
from ipython_genutils.py3compat import cast_unicode

from notebook.utils import maybe_future
from .handlers import IPythonHandler
Expand Down Expand Up @@ -236,7 +235,7 @@ def _reserialize_reply(self, msg_or_list, channel=None):
return buf
else:
smsg = json.dumps(msg, default=json_default)
return cast_unicode(smsg)
return smsg

def _on_zmq_reply(self, stream, msg_list):
# Sometimes this gets triggered when the on_close method is scheduled in the
Expand Down Expand Up @@ -281,7 +280,7 @@ def pre_get(self):
raise web.HTTPError(403)

if self.get_argument('session_id', False):
self.session.session = cast_unicode(self.get_argument('session_id'))
self.session.session = self.get_argument('session_id')
else:
self.log.warning("No session ID specified")

Expand Down
6 changes: 3 additions & 3 deletions notebook/bundler/bundlerextensions.py
Expand Up @@ -248,7 +248,7 @@ def list_nbextensions(self):
print("Known bundlerextensions:")

for config_dir in config_dirs:
head = u' config dir: {}'.format(config_dir)
head = ' config dir: {}'.format(config_dir)
head_shown = False

cm = BaseJSONConfigManager(parent=self, config_dir=config_dir)
Expand All @@ -263,9 +263,9 @@ def list_nbextensions(self):
label = info.get('label')
module = info.get('module_name')
if label is None or module is None:
msg = u' {} {}'.format(bundler_id, RED_DISABLED)
msg = ' {} {}'.format(bundler_id, RED_DISABLED)
else:
msg = u' "{}" from {} {}'.format(
msg = ' "{}" from {} {}'.format(
label, module, GREEN_ENABLED
)
print(msg)
Expand Down
6 changes: 3 additions & 3 deletions notebook/bundler/tests/test_bundler_api.py
Expand Up @@ -30,9 +30,9 @@ def setup_class(cls):

nb = new_notebook()

nb.cells.append(new_markdown_cell(u'Created by test'))
cc1 = new_code_cell(source=u'print(2*6)')
cc1.outputs.append(new_output(output_type="stream", text=u'12'))
nb.cells.append(new_markdown_cell('Created by test'))
cc1 = new_code_cell(source='print(2*6)')
cc1.outputs.append(new_output(output_type="stream", text='12'))
nb.cells.append(cc1)

with io.open(pjoin(nbdir, 'testnb.ipynb'), 'w',
Expand Down
5 changes: 2 additions & 3 deletions notebook/bundler/tests/test_bundlerextension.py
Expand Up @@ -6,10 +6,9 @@
import os
import shutil
import unittest
from tempfile import TemporaryDirectory

from unittest.mock import patch
from ipython_genutils.tempdir import TemporaryDirectory
from ipython_genutils import py3compat

from traitlets.tests.utils import check_help_all_output

Expand All @@ -29,7 +28,7 @@ def setUp(self):
"""Build an isolated config environment."""
td = TemporaryDirectory()

self.test_dir = py3compat.cast_unicode(td.name)
self.test_dir = td.name
self.data_dir = os.path.join(self.test_dir, 'data')
self.config_dir = os.path.join(self.test_dir, 'config')
self.system_data_dir = os.path.join(self.test_dir, 'system_data')
Expand Down
2 changes: 1 addition & 1 deletion notebook/edit/handlers.py
Expand Up @@ -13,7 +13,7 @@ class EditorHandler(IPythonHandler):
def get(self, path):
path = path.strip('/')
if not self.contents_manager.file_exists(path):
raise web.HTTPError(404, u'File does not exist: %s' % path)
raise web.HTTPError(404, 'File does not exist: %s' % path)

basename = path.rsplit('/', 1)[-1]
self.write(self.render_template('edit.html',
Expand Down
5 changes: 2 additions & 3 deletions notebook/gateway/handlers.py
Expand Up @@ -16,7 +16,6 @@
from tornado.httpclient import HTTPRequest
from tornado.escape import url_escape, json_decode, utf8

from ipython_genutils.py3compat import cast_unicode
from jupyter_client.session import Session
from traitlets.config.configurable import LoggingConfigurable

Expand Down Expand Up @@ -56,7 +55,7 @@ def authenticate(self):
raise web.HTTPError(403)

if self.get_argument('session_id', False):
self.session.session = cast_unicode(self.get_argument('session_id'))
self.session.session = self.get_argument('session_id')
else:
self.log.warning("No session ID specified")

Expand All @@ -68,7 +67,7 @@ def initialize(self):
@gen.coroutine
def get(self, kernel_id, *args, **kwargs):
self.authenticate()
self.kernel_id = cast_unicode(kernel_id, 'ascii')
self.kernel_id = kernel_id
yield super().get(kernel_id=kernel_id, *args, **kwargs)

def send_ping(self):
Expand Down
11 changes: 6 additions & 5 deletions notebook/jstest.py
Expand Up @@ -18,14 +18,15 @@
import subprocess
import time
from io import BytesIO
from shutil import which
from tempfile import TemporaryDirectory
from threading import Thread, Lock, Event

from unittest.mock import patch

from jupyter_core.paths import jupyter_runtime_dir
from ipython_genutils.py3compat import bytes_to_str, which
from ipython_genutils.py3compat import bytes_to_str
from notebook._sysinfo import get_sys_info
from ipython_genutils.tempdir import TemporaryDirectory

from subprocess import TimeoutExpired
def popen_wait(p, timeout):
Expand Down Expand Up @@ -59,7 +60,7 @@ def run(self):
with self.buffer_lock:
self.buffer.write(chunk)
if self.echo:
sys.stdout.write(bytes_to_str(chunk))
sys.stdout.write(chunk)

os.close(self.readfd)
os.close(self.writefd)
Expand Down Expand Up @@ -226,8 +227,8 @@ def setup(self):
self.dirs.append(self.home)
self.dirs.append(self.config_dir)
self.dirs.append(self.nbdir)
os.makedirs(os.path.join(self.nbdir.name, os.path.join(u'sub ∂ir1', u'sub ∂ir 1a')))
os.makedirs(os.path.join(self.nbdir.name, os.path.join(u'sub ∂ir2', u'sub ∂ir 1b')))
os.makedirs(os.path.join(self.nbdir.name, os.path.join('sub ∂ir1', 'sub ∂ir 1a')))
os.makedirs(os.path.join(self.nbdir.name, os.path.join('sub ∂ir2', 'sub ∂ir 1b')))

if self.xunit:
self.add_xunit()
Expand Down
2 changes: 1 addition & 1 deletion notebook/kernelspecs/handlers.py
Expand Up @@ -15,7 +15,7 @@ def get(self, kernel_name, path, include_body=True):
self.root = ksm.get_kernel_spec(kernel_name).resource_dir
except KeyError as e:
raise web.HTTPError(404,
u'Kernel spec %s not found' % kernel_name) from e
'Kernel spec %s not found' % kernel_name) from e
self.log.debug("Serving kernel resource from: %s", self.root)
return web.StaticFileHandler.get(self, path, include_body=include_body)

Expand Down
5 changes: 2 additions & 3 deletions notebook/nbconvert/handlers.py
Expand Up @@ -17,7 +17,6 @@
from ..utils import maybe_future
from nbformat import from_dict

from ipython_genutils.py3compat import cast_bytes
from ipython_genutils import text

def find_resource_files(output_files_dir):
Expand Down Expand Up @@ -47,7 +46,7 @@ def respond_zip(handler, name, output, resources):
buffer = io.BytesIO()
zipf = zipfile.ZipFile(buffer, mode='w', compression=zipfile.ZIP_DEFLATED)
output_filename = os.path.splitext(name)[0] + resources['output_extension']
zipf.writestr(output_filename, cast_bytes(output, 'utf-8'))
zipf.writestr(output_filename, output)
for filename, data in output_files.items():
zipf.writestr(os.path.basename(filename), data)
zipf.close()
Expand All @@ -67,7 +66,7 @@ def get_exporter(format, **kwargs):
Exporter = get_exporter(format)
except KeyError as e:
# should this be 400?
raise web.HTTPError(404, u"No exporter for format: %s" % format) from e
raise web.HTTPError(404, "No exporter for format: %s" % format) from e

try:
return Exporter(**kwargs)
Expand Down