From 182b8f994074233b33b51d8cef3973226c33276a 2020-09-04 22:36:13 From: rchiodo Date: 2020-09-04 22:36:13 Subject: [PATCH] Use pathlib in edit magic --- diff --git a/IPython/core/magics/code.py b/IPython/core/magics/code.py index f1289b5..5713406 100644 --- a/IPython/core/magics/code.py +++ b/IPython/core/magics/code.py @@ -22,6 +22,7 @@ import ast 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 @@ -688,11 +689,14 @@ class CodeMagics(Magics): # 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 @@ -700,7 +704,7 @@ class CodeMagics(Magics): # 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() if 'x' in opts: # -x prevents actual execution @@ -711,7 +715,7 @@ class CodeMagics(Magics): 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() self.shell.run_cell(source, store_history=False) else: @@ -720,10 +724,10 @@ class CodeMagics(Magics): if is_temp: try: - with open(filename) as f: + with filepath.open() as f: return f.read() except IOError as msg: - if msg.filename == filename: + if Path(msg.filename) == filepath: warn('File not found. Did you forget to save?') return else: diff --git a/IPython/core/tests/test_magic.py b/IPython/core/tests/test_magic.py index a52ab02..5021fa1 100644 --- a/IPython/core/tests/test_magic.py +++ b/IPython/core/tests/test_magic.py @@ -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')