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 |
@@ -512,8 +513,7 b' class CodeMagics(Magics):' | |||
|
512 | 513 | self.shell.hooks.editor(filename) |
|
513 | 514 | |
|
514 | 515 | # and make a new macro object, to replace the old one |
|
515 | with open(filename) as mfile: | |
|
516 | mvalue = mfile.read() | |
|
516 | mvalue = Path(filename).read_text() | |
|
517 | 517 | self.shell.user_ns[mname] = Macro(mvalue) |
|
518 | 518 | |
|
519 | 519 | @skip_doctest |
@@ -688,20 +688,22 b' class CodeMagics(Magics):' | |||
|
688 | 688 | # do actual editing here |
|
689 | 689 | print('Editing...', end=' ') |
|
690 | 690 | sys.stdout.flush() |
|
691 | filepath = Path(filename) | |
|
691 | 692 | try: |
|
692 | # Quote filenames that may have spaces in them | |
|
693 | if ' ' in filename: | |
|
694 |
|
|
|
695 | self.shell.hooks.editor(filename,lineno) | |
|
693 | # Quote filenames that may have spaces in them when opening | |
|
694 | # the editor | |
|
695 | quoted = filename = str(filepath.absolute()) | |
|
696 | if " " in quoted: | |
|
697 | quoted = "'%s'" % quoted | |
|
698 | self.shell.hooks.editor(quoted, lineno) | |
|
696 | 699 | except TryNext: |
|
697 | 700 | warn('Could not open editor') |
|
698 | 701 | return |
|
699 | 702 | |
|
700 | 703 | # XXX TODO: should this be generalized for all string vars? |
|
701 | 704 | # For now, this is special-cased to blocks created by cpaste |
|
702 |
if args.strip() == |
|
|
703 | with open(filename, 'r') as f: | |
|
704 | self.shell.user_ns['pasted_block'] = f.read() | |
|
705 | if args.strip() == "pasted_block": | |
|
706 | self.shell.user_ns["pasted_block"] = filepath.read_text() | |
|
705 | 707 | |
|
706 | 708 | if 'x' in opts: # -x prevents actual execution |
|
707 | 709 | print() |
@@ -711,8 +713,7 b' class CodeMagics(Magics):' | |||
|
711 | 713 | if not is_temp: |
|
712 | 714 | self.shell.user_ns['__file__'] = filename |
|
713 | 715 | if 'r' in opts: # Untranslated IPython code |
|
714 | with open(filename, 'r') as f: | |
|
715 | source = f.read() | |
|
716 | source = filepath.read_text() | |
|
716 | 717 | self.shell.run_cell(source, store_history=False) |
|
717 | 718 | else: |
|
718 | 719 | self.shell.safe_execfile(filename, self.shell.user_ns, |
@@ -720,10 +721,9 b' class CodeMagics(Magics):' | |||
|
720 | 721 | |
|
721 | 722 | if is_temp: |
|
722 | 723 | try: |
|
723 | with open(filename) as f: | |
|
724 | return f.read() | |
|
724 | return filepath.read_text() | |
|
725 | 725 | except IOError as msg: |
|
726 |
if msg.filename == file |
|
|
726 | if Path(msg.filename) == filepath: | |
|
727 | 727 | warn('File not found. Did you forget to save?') |
|
728 | 728 | return |
|
729 | 729 | 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