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 for edit magic command #12544

Merged
merged 4 commits into from
Sep 7, 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
28 changes: 14 additions & 14 deletions IPython/core/magics/code.py
Expand Up @@ -22,6 +22,7 @@
from itertools import chain
from urllib.request import urlopen
from urllib.parse import urlencode
from pathlib import Path

# Our own packages
from IPython.core.error import TryNext, StdinNotImplementedError, UsageError
Expand Down Expand Up @@ -512,8 +513,7 @@ def _edit_macro(self,mname,macro):
self.shell.hooks.editor(filename)

# and make a new macro object, to replace the old one
with open(filename) as mfile:
mvalue = mfile.read()
mvalue = Path(filename).read_text()
self.shell.user_ns[mname] = Macro(mvalue)

@skip_doctest
Expand Down Expand Up @@ -688,20 +688,22 @@ def edit(self, parameter_s='',last_call=['','']):
# do actual editing here
print('Editing...', end=' ')
sys.stdout.flush()
filepath = Path(filename)
try:
# Quote filenames that may have spaces in them
if ' ' in filename:
filename = "'%s'" % filename
self.shell.hooks.editor(filename,lineno)
# Quote filenames that may have spaces in them when opening
# the editor
quoted = filename = str(filepath.absolute())
if " " in quoted:
quoted = "'%s'" % quoted
self.shell.hooks.editor(quoted, lineno)
except TryNext:
warn('Could not open editor')
return

# XXX TODO: should this be generalized for all string vars?
# For now, this is special-cased to blocks created by cpaste
if args.strip() == 'pasted_block':
with open(filename, 'r') as f:
self.shell.user_ns['pasted_block'] = f.read()
if args.strip() == "pasted_block":
self.shell.user_ns["pasted_block"] = filepath.read_text()

if 'x' in opts: # -x prevents actual execution
print()
Expand All @@ -711,19 +713,17 @@ def edit(self, parameter_s='',last_call=['','']):
if not is_temp:
self.shell.user_ns['__file__'] = filename
if 'r' in opts: # Untranslated IPython code
with open(filename, 'r') as f:
source = f.read()
source = filepath.read_text()
self.shell.run_cell(source, store_history=False)
else:
self.shell.safe_execfile(filename, self.shell.user_ns,
self.shell.user_ns)

if is_temp:
try:
with open(filename) as f:
return f.read()
return filepath.read_text()
except IOError as msg:
if msg.filename == filename:
if Path(msg.filename) == filepath:
warn('File not found. Did you forget to save?')
return
else:
Expand Down
5 changes: 5 additions & 0 deletions IPython/core/tests/test_magic.py
Expand Up @@ -1138,6 +1138,11 @@ def test_edit_cell():
# test
_run_edit_test("1", exp_contents=ip.user_ns['In'][1], exp_is_temp=True)

def test_edit_fname():
"""%edit file"""
# test
_run_edit_test("test file.py", exp_filename="test file.py")

def test_bookmark():
ip = get_ipython()
ip.run_line_magic('bookmark', 'bmname')
Expand Down