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 2 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
22 changes: 13 additions & 9 deletions IPython/core/magics/code.py
Original file line number Diff line number Diff line change
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,7 +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:
with Path(filename).open() as mfile:
mvalue = mfile.read()
Carreau marked this conversation as resolved.
Show resolved Hide resolved
self.shell.user_ns[mname] = Macro(mvalue)

Expand Down Expand Up @@ -688,19 +689,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:
with filepath.open('r') as f:
self.shell.user_ns['pasted_block'] = f.read()
Carreau marked this conversation as resolved.
Show resolved Hide resolved

if 'x' in opts: # -x prevents actual execution
Expand All @@ -711,7 +715,7 @@ 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:
with filepath.open('r') as f:
source = f.read()
Carreau marked this conversation as resolved.
Show resolved Hide resolved
self.shell.run_cell(source, store_history=False)
else:
Expand All @@ -720,10 +724,10 @@ def edit(self, parameter_s='',last_call=['','']):

if is_temp:
try:
with open(filename) as f:
with filepath.open() as f:
return f.read()
Carreau marked this conversation as resolved.
Show resolved Hide resolved
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
Original file line number Diff line number Diff line change
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