Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use pathlib in autogen_magics.py, autogen_config.py, and test_magic.py #12614

Merged
merged 2 commits into from
Oct 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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))