##// END OF EJS Templates
Merge pull request #12544 from rchiodo/rchiodo/add_pathlib_to_edit
Matthias Bussonnier -
r26012:fedd6a8e merge
parent child Browse files
Show More
@@ -22,6 +22,7 b' import ast'
22 from itertools import chain
22 from itertools import chain
23 from urllib.request import urlopen
23 from urllib.request import urlopen
24 from urllib.parse import urlencode
24 from urllib.parse import urlencode
25 from pathlib import Path
25
26
26 # Our own packages
27 # Our own packages
27 from IPython.core.error import TryNext, StdinNotImplementedError, UsageError
28 from IPython.core.error import TryNext, StdinNotImplementedError, UsageError
@@ -512,8 +513,7 b' class CodeMagics(Magics):'
512 self.shell.hooks.editor(filename)
513 self.shell.hooks.editor(filename)
513
514
514 # and make a new macro object, to replace the old one
515 # and make a new macro object, to replace the old one
515 with open(filename) as mfile:
516 mvalue = Path(filename).read_text()
516 mvalue = mfile.read()
517 self.shell.user_ns[mname] = Macro(mvalue)
517 self.shell.user_ns[mname] = Macro(mvalue)
518
518
519 @skip_doctest
519 @skip_doctest
@@ -688,20 +688,22 b' class CodeMagics(Magics):'
688 # do actual editing here
688 # do actual editing here
689 print('Editing...', end=' ')
689 print('Editing...', end=' ')
690 sys.stdout.flush()
690 sys.stdout.flush()
691 filepath = Path(filename)
691 try:
692 try:
692 # Quote filenames that may have spaces in them
693 # Quote filenames that may have spaces in them when opening
693 if ' ' in filename:
694 # the editor
694 filename = "'%s'" % filename
695 quoted = filename = str(filepath.absolute())
695 self.shell.hooks.editor(filename,lineno)
696 if " " in quoted:
697 quoted = "'%s'" % quoted
698 self.shell.hooks.editor(quoted, lineno)
696 except TryNext:
699 except TryNext:
697 warn('Could not open editor')
700 warn('Could not open editor')
698 return
701 return
699
702
700 # XXX TODO: should this be generalized for all string vars?
703 # XXX TODO: should this be generalized for all string vars?
701 # For now, this is special-cased to blocks created by cpaste
704 # For now, this is special-cased to blocks created by cpaste
702 if args.strip() == 'pasted_block':
705 if args.strip() == "pasted_block":
703 with open(filename, 'r') as f:
706 self.shell.user_ns["pasted_block"] = filepath.read_text()
704 self.shell.user_ns['pasted_block'] = f.read()
705
707
706 if 'x' in opts: # -x prevents actual execution
708 if 'x' in opts: # -x prevents actual execution
707 print()
709 print()
@@ -711,8 +713,7 b' class CodeMagics(Magics):'
711 if not is_temp:
713 if not is_temp:
712 self.shell.user_ns['__file__'] = filename
714 self.shell.user_ns['__file__'] = filename
713 if 'r' in opts: # Untranslated IPython code
715 if 'r' in opts: # Untranslated IPython code
714 with open(filename, 'r') as f:
716 source = filepath.read_text()
715 source = f.read()
716 self.shell.run_cell(source, store_history=False)
717 self.shell.run_cell(source, store_history=False)
717 else:
718 else:
718 self.shell.safe_execfile(filename, self.shell.user_ns,
719 self.shell.safe_execfile(filename, self.shell.user_ns,
@@ -720,10 +721,9 b' class CodeMagics(Magics):'
720
721
721 if is_temp:
722 if is_temp:
722 try:
723 try:
723 with open(filename) as f:
724 return filepath.read_text()
724 return f.read()
725 except IOError as msg:
725 except IOError as msg:
726 if msg.filename == filename:
726 if Path(msg.filename) == filepath:
727 warn('File not found. Did you forget to save?')
727 warn('File not found. Did you forget to save?')
728 return
728 return
729 else:
729 else:
@@ -1138,6 +1138,11 b' def test_edit_cell():'
1138 # test
1138 # test
1139 _run_edit_test("1", exp_contents=ip.user_ns['In'][1], exp_is_temp=True)
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 def test_bookmark():
1146 def test_bookmark():
1142 ip = get_ipython()
1147 ip = get_ipython()
1143 ip.run_line_magic('bookmark', 'bmname')
1148 ip.run_line_magic('bookmark', 'bmname')
General Comments 0
You need to be logged in to leave comments. Login now