##// END OF EJS Templates
Use pathlib in edit magic
rchiodo -
Show More
@@ -22,6 +22,7 b' import ast'
22 22 from itertools import chain
23 23 from urllib.request import urlopen
24 24 from urllib.parse import urlencode
25 from pathlib import Path
25 26
26 27 # Our own packages
27 28 from IPython.core.error import TryNext, StdinNotImplementedError, UsageError
@@ -688,11 +689,14 b' class CodeMagics(Magics):'
688 689 # do actual editing here
689 690 print('Editing...', end=' ')
690 691 sys.stdout.flush()
692 filepath = Path(filename)
691 693 try:
692 # Quote filenames that may have spaces in them
693 if ' ' in filename:
694 filename = "'%s'" % filename
695 self.shell.hooks.editor(filename,lineno)
694 # Quote filenames that may have spaces in them when opening
695 # the editor
696 quoted = filename = str(filepath.absolute())
697 if ' ' in quoted:
698 quoted = "'%s'" % quoted
699 self.shell.hooks.editor(quoted,lineno)
696 700 except TryNext:
697 701 warn('Could not open editor')
698 702 return
@@ -700,7 +704,7 b' class CodeMagics(Magics):'
700 704 # XXX TODO: should this be generalized for all string vars?
701 705 # For now, this is special-cased to blocks created by cpaste
702 706 if args.strip() == 'pasted_block':
703 with open(filename, 'r') as f:
707 with filepath.open('r') as f:
704 708 self.shell.user_ns['pasted_block'] = f.read()
705 709
706 710 if 'x' in opts: # -x prevents actual execution
@@ -711,7 +715,7 b' class CodeMagics(Magics):'
711 715 if not is_temp:
712 716 self.shell.user_ns['__file__'] = filename
713 717 if 'r' in opts: # Untranslated IPython code
714 with open(filename, 'r') as f:
718 with filepath.open('r') as f:
715 719 source = f.read()
716 720 self.shell.run_cell(source, store_history=False)
717 721 else:
@@ -720,10 +724,10 b' class CodeMagics(Magics):'
720 724
721 725 if is_temp:
722 726 try:
723 with open(filename) as f:
727 with filepath.open() as f:
724 728 return f.read()
725 729 except IOError as msg:
726 if msg.filename == filename:
730 if Path(msg.filename) == filepath:
727 731 warn('File not found. Did you forget to save?')
728 732 return
729 733 else:
@@ -1138,6 +1138,11 b' def test_edit_cell():'
1138 1138 # test
1139 1139 _run_edit_test("1", exp_contents=ip.user_ns['In'][1], exp_is_temp=True)
1140 1140
1141 def test_edit_fname():
1142 """%edit file"""
1143 # test
1144 _run_edit_test("test file.py", exp_filename="test file.py")
1145
1141 1146 def test_bookmark():
1142 1147 ip = get_ipython()
1143 1148 ip.run_line_magic('bookmark', 'bmname')
General Comments 0
You need to be logged in to leave comments. Login now