diff --git a/IPython/core/tests/test_magic.py b/IPython/core/tests/test_magic.py index 5021fa1..9eab3a8 100644 --- a/IPython/core/tests/test_magic.py +++ b/IPython/core/tests/test_magic.py @@ -14,6 +14,7 @@ from unittest import TestCase from unittest import mock from importlib import invalidate_caches from io import StringIO +from pathlib import Path import nose.tools as nt @@ -831,8 +832,7 @@ def test_file(): 'line1', 'line2', ])) - with open(fname) as f: - s = f.read() + s = Path(fname).read_text() nt.assert_in('line1\n', s) nt.assert_in('line2', s) @@ -846,8 +846,7 @@ def test_file_single_quote(): 'line1', 'line2', ])) - with open(fname) as f: - s = f.read() + s = Path(fname).read_text() nt.assert_in('line1\n', s) nt.assert_in('line2', s) @@ -861,8 +860,7 @@ def test_file_double_quote(): 'line1', 'line2', ])) - with open(fname) as f: - s = f.read() + s = Path(fname).read_text() nt.assert_in('line1\n', s) nt.assert_in('line2', s) @@ -876,8 +874,7 @@ def test_file_var_expand(): 'line1', 'line2', ])) - with open(fname) as f: - s = f.read() + s = Path(fname).read_text() nt.assert_in('line1\n', s) nt.assert_in('line2', s) @@ -908,8 +905,7 @@ def test_file_amend(): 'line3', 'line4', ])) - with open(fname) as f: - s = f.read() + s = Path(fname).read_text() nt.assert_in('line1\n', s) nt.assert_in('line3\n', s) @@ -922,8 +918,7 @@ def test_file_spaces(): 'line1', 'line2', ])) - with open(fname) as f: - s = f.read() + s = Path(fname).read_text() nt.assert_in('line1\n', s) nt.assert_in('line2', s) @@ -1063,15 +1058,13 @@ def test_save(): with TemporaryDirectory() as tmpdir: file = os.path.join(tmpdir, "testsave.py") ip.run_line_magic("save", "%s 1-10" % file) - with open(file) as f: - content = f.read() - nt.assert_equal(content.count(cmds[0]), 1) - nt.assert_in('coding: utf-8', content) + content = Path(file).read_text() + nt.assert_equal(content.count(cmds[0]), 1) + nt.assert_in('coding: utf-8', content) ip.run_line_magic("save", "-a %s 1-10" % file) - with open(file) as f: - content = f.read() - nt.assert_equal(content.count(cmds[0]), 2) - nt.assert_in('coding: utf-8', content) + content = Path(file).read_text() + nt.assert_equal(content.count(cmds[0]), 2) + nt.assert_in('coding: utf-8', content) def test_store(): @@ -1231,8 +1224,7 @@ def test_run_module_from_import_hook(): "Test that a module can be loaded via an import hook" with TemporaryDirectory() as tmpdir: fullpath = os.path.join(tmpdir, 'my_tmp.py') - with open(fullpath, 'w') as f: - f.write(TEST_MODULE) + Path(fullpath).write_text(TEST_MODULE) class MyTempImporter(object): def __init__(self): @@ -1248,8 +1240,7 @@ def test_run_module_from_import_hook(): return imp.load_source('my_tmp', fullpath) def get_code(self, fullname): - with open(fullpath, 'r') as f: - return compile(f.read(), 'foo', 'exec') + return compile(Path(fullpath).read_text(), 'foo', 'exec') def is_package(self, __): return False diff --git a/docs/autogen_config.py b/docs/autogen_config.py index 515e092..0330b6d 100755 --- a/docs/autogen_config.py +++ b/docs/autogen_config.py @@ -2,7 +2,7 @@ from os.path import join, dirname, abspath import inspect - +from pathlib import Path from IPython.terminal.ipapp import TerminalIPythonApp from ipykernel.kernelapp import IPKernelApp from traitlets import Undefined @@ -118,8 +118,7 @@ def write_doc(name, title, app, preamble=None): if __name__ == '__main__': # Touch this file for the make target - with open(generated, 'w'): - pass + Path(generated).write_text('') write_doc('terminal', 'Terminal IPython options', TerminalIPythonApp()) write_doc('kernel', 'IPython kernel options', IPKernelApp(), diff --git a/docs/autogen_magics.py b/docs/autogen_magics.py index b1662c6..ca4a69f 100644 --- a/docs/autogen_magics.py +++ b/docs/autogen_magics.py @@ -1,5 +1,5 @@ import os - +from pathlib import Path from IPython.core.alias import Alias from IPython.core.interactiveshell import InteractiveShell from IPython.core.magic import MagicAlias @@ -63,6 +63,5 @@ for name, func in sorted(magics['cell'].items(), key=sortkey): ""]) here = os.path.dirname(__file__) -dest = os.path.join(here, 'source', 'interactive', 'magics-generated.txt') -with open(dest, "w") as f: - f.write("\n".join(output)) +dest = Path(os.path.join(here, 'source', 'interactive', 'magics-generated.txt')) +dest.write_text("\n".join(output))