Skip to content

Commit 66e16ca

Browse files
authored
Merge pull request #249 from dlukes/master
Add explicit encoding in open calls
2 parents 2ccb576 + fc2edff commit 66e16ca

File tree

6 files changed

+20
-19
lines changed

6 files changed

+20
-19
lines changed

jupyter_core/application.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ def ask():
147147
config_text = config_text.decode('utf8')
148148
print("Writing default config to: %s" % config_file)
149149
ensure_dir_exists(os.path.abspath(os.path.dirname(config_file)), 0o700)
150-
with open(config_file, mode='w') as f:
150+
with open(config_file, mode='w', encoding='utf-8') as f:
151151
f.write(config_text)
152152

153153
def migrate_config(self):

jupyter_core/migrate.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -109,11 +109,11 @@ def migrate_file(src, dst, substitutions=None):
109109
ensure_dir_exists(os.path.dirname(dst))
110110
shutil.copy(src, dst)
111111
if substitutions:
112-
with open(dst) as f:
112+
with open(dst, encoding='utf-8') as f:
113113
text = f.read()
114114
for pat, replacement in substitutions.items():
115115
text = pat.sub(replacement, text)
116-
with open(dst, 'w') as f:
116+
with open(dst, 'w', encoding='utf-8') as f:
117117
f.write(text)
118118
return True
119119

@@ -146,7 +146,7 @@ def migrate_static_custom(src, dst):
146146
# check if custom_js is empty:
147147
custom_js_empty = True
148148
if os.path.isfile(custom_js):
149-
with open(custom_js) as f:
149+
with open(custom_js, encoding='utf-8') as f:
150150
js = f.read().strip()
151151
for line in js.splitlines():
152152
if not (
@@ -159,7 +159,7 @@ def migrate_static_custom(src, dst):
159159
# check if custom_css is empty:
160160
custom_css_empty = True
161161
if os.path.isfile(custom_css):
162-
with open(custom_css) as f:
162+
with open(custom_css, encoding='utf-8') as f:
163163
css = f.read().strip()
164164
custom_css_empty = css.startswith('/*') and css.endswith('*/')
165165

@@ -242,7 +242,7 @@ def migrate():
242242

243243
# write a marker to avoid re-running migration checks
244244
ensure_dir_exists(env['jupyter_config'])
245-
with open(os.path.join(env['jupyter_config'], 'migrated'), 'w') as f:
245+
with open(os.path.join(env['jupyter_config'], 'migrated'), 'w', encoding='utf-8') as f:
246246
f.write(datetime.utcnow().isoformat())
247247

248248
return migrated

jupyter_core/paths.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -877,6 +877,7 @@ def secure_write(fname, binary=False):
877877
Indicates that the file is binary
878878
"""
879879
mode = 'wb' if binary else 'w'
880+
encoding = None if binary else 'utf-8'
880881
open_flag = os.O_CREAT | os.O_WRONLY | os.O_TRUNC
881882
try:
882883
os.remove(fname)
@@ -898,7 +899,7 @@ def secure_write(fname, binary=False):
898899
open_flag = os.O_WRONLY | os.O_TRUNC
899900
win32_restrict_file_to_user(fname)
900901

901-
with os.fdopen(os.open(fname, open_flag, 0o0600), mode) as f:
902+
with os.fdopen(os.open(fname, open_flag, 0o0600), mode, encoding=encoding) as f:
902903
if os.name != 'nt':
903904
# Enforce that the file got the requested permissions before writing
904905
file_mode = get_file_mode(fname)

jupyter_core/tests/test_application.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ def test_custom_config():
3333
app = DummyApp()
3434
td = mkdtemp()
3535
fname = pjoin(td, 'config.py')
36-
with open(fname, 'w') as f:
36+
with open(fname, 'w', encoding='utf-8') as f:
3737
f.write(_dummy_config)
3838
app.initialize(['--config', fname])
3939
shutil.rmtree(td)
@@ -44,7 +44,7 @@ def test_cli_override():
4444
app = DummyApp()
4545
td = mkdtemp()
4646
fname = pjoin(td, 'config.py')
47-
with open(fname, 'w') as f:
47+
with open(fname, 'w', encoding='utf-8') as f:
4848
f.write(_dummy_config)
4949
app.initialize(['--config', fname, '--DummyApp.n=20'])
5050
shutil.rmtree(td)
@@ -66,7 +66,7 @@ def test_generate_config():
6666
def test_load_config():
6767
config_dir = mkdtemp()
6868
wd = mkdtemp()
69-
with open(pjoin(config_dir, 'dummy_app_config.py'), 'w') as f:
69+
with open(pjoin(config_dir, 'dummy_app_config.py'), 'w', encoding='utf-8') as f:
7070
f.write('c.DummyApp.m = 1\n')
7171
f.write('c.DummyApp.n = 1')
7272
with patch.object(os, 'getcwd', lambda : wd):
@@ -75,7 +75,7 @@ def test_load_config():
7575

7676
assert app.n == 1, "Loaded config from config dir"
7777

78-
with open(pjoin(wd, 'dummy_app_config.py'), 'w') as f:
78+
with open(pjoin(wd, 'dummy_app_config.py'), 'w', encoding='utf-8') as f:
7979
f.write('c.DummyApp.n = 2')
8080

8181
with patch.object(os, 'getcwd', lambda : wd):
@@ -92,7 +92,7 @@ def test_load_config():
9292
def test_load_bad_config():
9393
config_dir = mkdtemp()
9494
wd = mkdtemp()
95-
with open(pjoin(config_dir, 'dummy_app_config.py'), 'w') as f:
95+
with open(pjoin(config_dir, 'dummy_app_config.py'), 'w', encoding='utf-8') as f:
9696
f.write('c.DummyApp.m = "a\n') # Syntax error
9797
with patch.object(os, 'getcwd', lambda : wd):
9898
with pytest.raises(SyntaxError):

jupyter_core/tests/test_migrate.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,18 +57,18 @@ def fin():
5757

5858
def touch(path, content=''):
5959
ensure_dir_exists(os.path.dirname(path))
60-
with open(path, 'w') as f:
60+
with open(path, 'w', encoding='utf-8') as f:
6161
f.write(content)
6262

6363

6464
def assert_files_equal(a, b):
6565
"""Verify that two files match"""
6666

6767
assert os.path.exists(b)
68-
with open(a) as f:
68+
with open(a, encoding='utf-8') as f:
6969
a_txt = f.read()
7070

71-
with open(b) as f:
71+
with open(b, encoding='utf-8') as f:
7272
b_txt = f.read()
7373

7474
assert a_txt == b_txt
@@ -166,7 +166,7 @@ def test_migrate_config(td):
166166
'jupyter_test_config.py',
167167
]
168168

169-
with open(pjoin(jpy, 'jupyter_test_config.py')) as f:
169+
with open(pjoin(jpy, 'jupyter_test_config.py'), encoding='utf-8') as f:
170170
text = f.read()
171171
assert text == 'c.Replaced.trait = 5\n'
172172

jupyter_core/tests/test_paths.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -396,7 +396,7 @@ def check_user_only_permissions(fname):
396396
with secure_write(fname) as f:
397397
f.write('test 1')
398398
check_user_only_permissions(fname)
399-
with open(fname, 'r') as f:
399+
with open(fname, 'r', encoding='utf-8') as f:
400400
assert f.read() == 'test 1'
401401
finally:
402402
shutil.rmtree(directory)
@@ -411,7 +411,7 @@ def test_secure_write_unix():
411411
f.write('test 1')
412412
mode = os.stat(fname).st_mode
413413
assert 0o0600 == (stat.S_IMODE(mode) & 0o7677) # tolerate owner-execute bit
414-
with open(fname, 'r') as f:
414+
with open(fname, 'r', encoding='utf-8') as f:
415415
assert f.read() == 'test 1'
416416

417417
# Try changing file permissions ahead of time
@@ -420,7 +420,7 @@ def test_secure_write_unix():
420420
f.write('test 2')
421421
mode = os.stat(fname).st_mode
422422
assert 0o0600 == (stat.S_IMODE(mode) & 0o7677) # tolerate owner-execute bit
423-
with open(fname, 'r') as f:
423+
with open(fname, 'r', encoding='utf-8') as f:
424424
assert f.read() == 'test 2'
425425
finally:
426426
shutil.rmtree(directory)

0 commit comments

Comments
 (0)