##// END OF EJS Templates
Use pathlib
Joyce Er -
Show More
@@ -14,6 +14,7 b' from unittest import TestCase'
14 14 from unittest import mock
15 15 from importlib import invalidate_caches
16 16 from io import StringIO
17 from pathlib import Path
17 18
18 19 import nose.tools as nt
19 20
@@ -831,8 +832,7 b' def test_file():'
831 832 'line1',
832 833 'line2',
833 834 ]))
834 with open(fname) as f:
835 s = f.read()
835 s = Path(fname).read_text()
836 836 nt.assert_in('line1\n', s)
837 837 nt.assert_in('line2', s)
838 838
@@ -846,8 +846,7 b' def test_file_single_quote():'
846 846 'line1',
847 847 'line2',
848 848 ]))
849 with open(fname) as f:
850 s = f.read()
849 s = Path(fname).read_text()
851 850 nt.assert_in('line1\n', s)
852 851 nt.assert_in('line2', s)
853 852
@@ -861,8 +860,7 b' def test_file_double_quote():'
861 860 'line1',
862 861 'line2',
863 862 ]))
864 with open(fname) as f:
865 s = f.read()
863 s = Path(fname).read_text()
866 864 nt.assert_in('line1\n', s)
867 865 nt.assert_in('line2', s)
868 866
@@ -876,8 +874,7 b' def test_file_var_expand():'
876 874 'line1',
877 875 'line2',
878 876 ]))
879 with open(fname) as f:
880 s = f.read()
877 s = Path(fname).read_text()
881 878 nt.assert_in('line1\n', s)
882 879 nt.assert_in('line2', s)
883 880
@@ -908,8 +905,7 b' def test_file_amend():'
908 905 'line3',
909 906 'line4',
910 907 ]))
911 with open(fname) as f:
912 s = f.read()
908 s = Path(fname).read_text()
913 909 nt.assert_in('line1\n', s)
914 910 nt.assert_in('line3\n', s)
915 911
@@ -922,8 +918,7 b' def test_file_spaces():'
922 918 'line1',
923 919 'line2',
924 920 ]))
925 with open(fname) as f:
926 s = f.read()
921 s = Path(fname).read_text()
927 922 nt.assert_in('line1\n', s)
928 923 nt.assert_in('line2', s)
929 924
@@ -1063,15 +1058,13 b' def test_save():'
1063 1058 with TemporaryDirectory() as tmpdir:
1064 1059 file = os.path.join(tmpdir, "testsave.py")
1065 1060 ip.run_line_magic("save", "%s 1-10" % file)
1066 with open(file) as f:
1067 content = f.read()
1068 nt.assert_equal(content.count(cmds[0]), 1)
1069 nt.assert_in('coding: utf-8', content)
1061 content = Path(file).read_text()
1062 nt.assert_equal(content.count(cmds[0]), 1)
1063 nt.assert_in('coding: utf-8', content)
1070 1064 ip.run_line_magic("save", "-a %s 1-10" % file)
1071 with open(file) as f:
1072 content = f.read()
1073 nt.assert_equal(content.count(cmds[0]), 2)
1074 nt.assert_in('coding: utf-8', content)
1065 content = Path(file).read_text()
1066 nt.assert_equal(content.count(cmds[0]), 2)
1067 nt.assert_in('coding: utf-8', content)
1075 1068
1076 1069
1077 1070 def test_store():
@@ -1231,8 +1224,7 b' def test_run_module_from_import_hook():'
1231 1224 "Test that a module can be loaded via an import hook"
1232 1225 with TemporaryDirectory() as tmpdir:
1233 1226 fullpath = os.path.join(tmpdir, 'my_tmp.py')
1234 with open(fullpath, 'w') as f:
1235 f.write(TEST_MODULE)
1227 Path(fullpath).write_text(TEST_MODULE)
1236 1228
1237 1229 class MyTempImporter(object):
1238 1230 def __init__(self):
@@ -1248,8 +1240,7 b' def test_run_module_from_import_hook():'
1248 1240 return imp.load_source('my_tmp', fullpath)
1249 1241
1250 1242 def get_code(self, fullname):
1251 with open(fullpath, 'r') as f:
1252 return compile(f.read(), 'foo', 'exec')
1243 return compile(Path(fullpath).read_text(), 'foo', 'exec')
1253 1244
1254 1245 def is_package(self, __):
1255 1246 return False
@@ -2,7 +2,7 b''
2 2
3 3 from os.path import join, dirname, abspath
4 4 import inspect
5
5 from pathlib import Path
6 6 from IPython.terminal.ipapp import TerminalIPythonApp
7 7 from ipykernel.kernelapp import IPKernelApp
8 8 from traitlets import Undefined
@@ -118,8 +118,7 b' def write_doc(name, title, app, preamble=None):'
118 118
119 119 if __name__ == '__main__':
120 120 # Touch this file for the make target
121 with open(generated, 'w'):
122 pass
121 Path(generated).write_text('')
123 122
124 123 write_doc('terminal', 'Terminal IPython options', TerminalIPythonApp())
125 124 write_doc('kernel', 'IPython kernel options', IPKernelApp(),
@@ -1,5 +1,5 b''
1 1 import os
2
2 from pathlib import Path
3 3 from IPython.core.alias import Alias
4 4 from IPython.core.interactiveshell import InteractiveShell
5 5 from IPython.core.magic import MagicAlias
@@ -63,6 +63,5 b" for name, func in sorted(magics['cell'].items(), key=sortkey):"
63 63 ""])
64 64
65 65 here = os.path.dirname(__file__)
66 dest = os.path.join(here, 'source', 'interactive', 'magics-generated.txt')
67 with open(dest, "w") as f:
68 f.write("\n".join(output))
66 dest = Path(os.path.join(here, 'source', 'interactive', 'magics-generated.txt'))
67 dest.write_text("\n".join(output))
General Comments 0
You need to be logged in to leave comments. Login now