Skip to content

Commit 4ba07e1

Browse files
authored
Fix #6256 : migrate to python 3.7+ (#6260)
1 parent 3c54a9a commit 4ba07e1

File tree

89 files changed

+972
-1042
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

89 files changed

+972
-1042
lines changed

docs/source/conf.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,7 @@
319319
# -- Options for link checks ----------------------------------------------
320320

321321
linkcheck_ignore = [
322-
'http://127\.0\.0\.1/*'
322+
r'http://127\.0\.0\.1/*'
323323
]
324324

325325

notebook/__main__.py

-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
if __name__ == '__main__':
32
from notebook import notebookapp as app
43
app.launch_new_instance()

notebook/_sysinfo.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,10 @@ def pkg_commit_hash(pkg_path):
5555
if repo_commit:
5656
return 'repository', repo_commit.strip().decode('ascii')
5757
else:
58-
return u'', u''
58+
return '', ''
5959
par_path = p.dirname(par_path)
60-
61-
return u'', u''
60+
61+
return '', ''
6262

6363

6464
def pkg_info(pkg_path):

notebook/auth/__main__.py

+6-3
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,17 @@ def set_password(args):
2525
}
2626
})
2727
if not args.quiet:
28-
print("password stored in config dir: %s" % jupyter_config_dir())
28+
print(f"password stored in config dir: {jupyter_config_dir()}")
2929

3030
def main(argv):
3131
parser = argparse.ArgumentParser(argv[0])
3232
subparsers = parser.add_subparsers()
3333
parser_password = subparsers.add_parser('password', help='sets a password for your notebook server')
34-
parser_password.add_argument("password", help="password to set, if not given, a password will be queried for (NOTE: this may not be safe)",
35-
nargs="?")
34+
parser_password.add_argument(
35+
"password",
36+
help="password to set, if not given, a password will be queried for (NOTE: this may not be safe)",
37+
nargs="?"
38+
)
3639
parser_password.add_argument("--quiet", help="suppress messages", action="store_true")
3740
parser_password.set_defaults(function=set_password)
3841
args = parser.parse_args(argv[1:])

notebook/auth/login.py

+9-9
Original file line numberDiff line numberDiff line change
@@ -48,15 +48,15 @@ def _redirect_safe(self, url, default=None):
4848
# OR pass our cross-origin check
4949
if parsed.netloc:
5050
# if full URL, run our cross-origin check:
51-
origin = '%s://%s' % (parsed.scheme, parsed.netloc)
51+
origin = f'{parsed.scheme}://{parsed.netloc}'
5252
origin = origin.lower()
5353
if self.allow_origin:
5454
allow = self.allow_origin == origin
5555
elif self.allow_origin_pat:
5656
allow = bool(self.allow_origin_pat.match(origin))
5757
if not allow:
5858
# not allowed, use default
59-
self.log.warning("Not allowing login redirect to %r" % url)
59+
self.log.warning(f"Not allowing login redirect to {url!r}")
6060
url = default
6161
self.redirect(url)
6262

@@ -73,13 +73,13 @@ def hashed_password(self):
7373

7474
def passwd_check(self, a, b):
7575
return passwd_check(a, b)
76-
76+
7777
def post(self):
78-
typed_password = self.get_argument('password', default=u'')
79-
new_password = self.get_argument('new_password', default=u'')
78+
typed_password = self.get_argument('password', default='')
79+
new_password = self.get_argument('new_password', default='')
80+
8081

8182

82-
8383
if self.get_login_available(self.settings):
8484
if self.passwd_check(self.hashed_password, typed_password) and not new_password:
8585
self.set_login_cookie(self, uuid.uuid4().hex)
@@ -89,7 +89,7 @@ def post(self):
8989
config_dir = self.settings.get('config_dir')
9090
config_file = os.path.join(config_dir, 'jupyter_notebook_config.json')
9191
set_password(new_password, config_file=config_file)
92-
self.log.info("Wrote hashed password to %s" % config_file)
92+
self.log.info(f"Wrote hashed password to {config_file}")
9393
else:
9494
self.set_status(401)
9595
self._render(message={'error': 'Invalid credentials'})
@@ -197,7 +197,7 @@ def get_user(cls, handler):
197197
@classmethod
198198
def get_user_token(cls, handler):
199199
"""Identify the user based on a token in the URL or Authorization header
200-
200+
201201
Returns:
202202
- uuid if authenticated
203203
- None if not
@@ -245,7 +245,7 @@ def password_from_settings(cls, settings):
245245
246246
If there is no configured password, an empty string will be returned.
247247
"""
248-
return settings.get('password', u'')
248+
return settings.get('password', '')
249249

250250
@classmethod
251251
def get_login_available(cls, settings):

notebook/auth/security.py

+5-6
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
from contextlib import contextmanager
66
import getpass
77
import hashlib
8-
import io
98
import json
109
import os
1110
import random
@@ -71,7 +70,7 @@ def passwd(passphrase=None, algorithm='argon2'):
7170
return ':'.join((algorithm, cast_unicode(h, 'ascii')))
7271
else:
7372
h = hashlib.new(algorithm)
74-
salt = ('%0' + str(salt_len) + 'x') % random.getrandbits(4 * salt_len)
73+
salt = f"{random.getrandbits(4 * salt_len):0{salt_len}x}"
7574
h.update(cast_bytes(passphrase, 'utf-8') + str_to_bytes(salt, 'ascii'))
7675

7776
return ':'.join((algorithm, salt, h.hexdigest()))
@@ -135,7 +134,7 @@ def passwd_check(hashed_passphrase, passphrase):
135134
def persist_config(config_file=None, mode=0o600):
136135
"""Context manager that can be used to modify a config object
137136
138-
On exit of the context manager, the config will be written back to disk,
137+
On exit of the context manager, the config will be written back to disk,
139138
by default with user-only (600) permissions.
140139
"""
141140

@@ -152,20 +151,20 @@ def persist_config(config_file=None, mode=0o600):
152151

153152
yield config
154153

155-
with io.open(config_file, 'w', encoding='utf8') as f:
154+
with open(config_file, 'w', encoding='utf8') as f:
156155
f.write(cast_unicode(json.dumps(config, indent=2)))
157156

158157
try:
159158
os.chmod(config_file, mode)
160159
except Exception as e:
161160
tb = traceback.format_exc()
162-
warnings.warn("Failed to set permissions on %s:\n%s" % (config_file, tb),
161+
warnings.warn(f"Failed to set permissions on {config_file}:\n{tb}",
163162
RuntimeWarning)
164163

165164

166165
def set_password(password=None, config_file=None):
167166
"""Ask user for password, store it in notebook json configuration file"""
168-
167+
169168
hashed_password = passwd(password)
170169

171170
with persist_config(config_file) as config:

notebook/auth/tests/test_security.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ def test_bad():
1818

1919
def test_passwd_check_unicode():
2020
# GH issue #4524
21-
phash = u'sha1:23862bc21dd3:7a415a95ae4580582e314072143d9c382c491e4f'
22-
assert passwd_check(phash, u"łe¶ŧ←↓→")
23-
phash = (u'argon2:$argon2id$v=19$m=10240,t=10,p=8$'
24-
u'qjjDiZUofUVVnrVYxacnbA$l5pQq1bJ8zglGT2uXP6iOg')
25-
assert passwd_check(phash, u"łe¶ŧ←↓→")
21+
phash = 'sha1:23862bc21dd3:7a415a95ae4580582e314072143d9c382c491e4f'
22+
assert passwd_check(phash, "łe¶ŧ←↓→")
23+
phash = ('argon2:$argon2id$v=19$m=10240,t=10,p=8$'
24+
'qjjDiZUofUVVnrVYxacnbA$l5pQq1bJ8zglGT2uXP6iOg')
25+
assert passwd_check(phash, "łe¶ŧ←↓→")

0 commit comments

Comments
 (0)