Skip to content

Commit

Permalink
Merge pull request #12614 from joyceerhl/pathlib
Browse files Browse the repository at this point in the history
  • Loading branch information
Carreau committed Oct 13, 2020
2 parents 7fe090c + 967118d commit cc69379
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 31 deletions.
39 changes: 15 additions & 24 deletions IPython/core/tests/test_magic.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from unittest import mock
from importlib import invalidate_caches
from io import StringIO
from pathlib import Path

import nose.tools as nt

Expand Down Expand Up @@ -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)

Expand All @@ -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)

Expand All @@ -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)

Expand All @@ -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)

Expand Down Expand Up @@ -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)

Expand All @@ -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)

Expand Down Expand Up @@ -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():
Expand Down Expand Up @@ -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):
Expand All @@ -1248,8 +1240,7 @@ def load_module(self, name):
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
Expand Down
5 changes: 2 additions & 3 deletions docs/autogen_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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(),
Expand Down
7 changes: 3 additions & 4 deletions docs/autogen_magics.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -63,6 +63,5 @@ def sortkey(s): return s[0].lower()
""])

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))

0 comments on commit cc69379

Please sign in to comment.