Repository: ipython/ipython

Issue:
mistyping `ed -p` breaks `ed -p`
``` python

In [2]: ed
IPython will make a temporary file named: /tmp/ipython_edit_ScaKLT.py
Editing...Waiting for Emacs...
 done. Executing edited code...
Out[2]: '#\n'

In [3]: ed -p
IPython will make a temporary file named: /tmp/ipython_edit_Spe8Xb.py
Editing...Waiting for Emacs...
 done. Executing edited code...
Out[3]: '##\n'

In [4]: ed - p
WARNING: Argument given (- p) can't be found as a variable or as a filename.---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)

TypeError: 'NoneType' object is not iterable

In [5]: ed -p
WARNING: Argument given (- p) can't be found as a variable or as a filename.---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)

TypeError: 'NoneType' object is not iterable
```

works correctly until line 4 where it breaks indefinitely, i then have to go back up copy it out and paste into a new edit window. Very annoying, especially when it happens almost every single day for months.

Full Traceback:

``` python
WARNING: Argument given (- p) can't be found as a variable or as a filename.---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-4-f5355427c5e8> in <module>()
----> 1 get_ipython().magic(u'ed - p')

/home/ddd/Dropbox/prog/oth/ipython/IPython/core/interactiveshell.pyc in magic(self, arg_s)
   2155         magic_name, _, magic_arg_s = arg_s.partition(' ')
   2156         magic_name = magic_name.lstrip(prefilter.ESC_MAGIC)
-> 2157         return self.run_line_magic(magic_name, magic_arg_s)
   2158 
   2159     #-------------------------------------------------------------------------

/home/ddd/Dropbox/prog/oth/ipython/IPython/core/interactiveshell.pyc in run_line_magic(self, magic_name, line)
   2081                 args.append(sys._getframe(stack_depth).f_locals)
   2082             with self.builtin_trap:
-> 2083                 result = fn(*args)
   2084             return result
   2085 

/home/ddd/Dropbox/prog/oth/ipython/IPython/core/magics/code.pyc in ed(self, parameter_s)

/home/ddd/Dropbox/prog/oth/ipython/IPython/core/magic.pyc in <lambda>(f, *a, **k)
    189     # but it's overkill for just that one bit of state.
    190     def magic_deco(arg):
--> 191         call = lambda f, *a, **k: f(*a, **k)
    192 
    193         if callable(arg):

/home/ddd/Dropbox/prog/oth/ipython/IPython/core/magics/code.pyc in ed(self, parameter_s)
    337     def ed(self, parameter_s=''):
    338         """Alias to %edit."""
--> 339         return self.edit(parameter_s)
    340 
    341     @skip_doctest

/home/ddd/Dropbox/prog/oth/ipython/IPython/core/magics/code.pyc in edit(self, parameter_s, last_call)

/home/ddd/Dropbox/prog/oth/ipython/IPython/core/magic.pyc in <lambda>(f, *a, **k)
    189     # but it's overkill for just that one bit of state.
    190     def magic_deco(arg):
--> 191         call = lambda f, *a, **k: f(*a, **k)
    192 
    193         if callable(arg):

/home/ddd/Dropbox/prog/oth/ipython/IPython/core/magics/code.pyc in edit(self, parameter_s, last_call)
    488         try:
    489             filename, lineno, is_temp = self._find_edit_target(self.shell, 
--> 490                                                        args, opts, last_call)
    491         except MacroToEdit as e:
    492             self._edit_macro(args, e.args[0])

TypeError: 'NoneType' object is not iterable
```



Discussion:

Resolving patch:
diff --git a/IPython/core/magics/code.py b/IPython/core/magics/code.py
--- a/IPython/core/magics/code.py
+++ b/IPython/core/magics/code.py
@@ -245,15 +245,6 @@ class DataIsObject(Exception): pass
             if args not in shell.user_ns:
                 args = last_call[1]
 
-        # use last_call to remember the state of the previous call, but don't
-        # let it be clobbered by successive '-p' calls.
-        try:
-            last_call[0] = shell.displayhook.prompt_count
-            if not opts_prev:
-                last_call[1] = args
-        except:
-            pass
-
         # by default this is done with temp files, except when the given
         # arg is a filename
         use_temp = True
@@ -340,6 +331,16 @@ class DataIsObject(Exception): pass
             filename = shell.mktempfile(data)
             print 'IPython will make a temporary file named:',filename
 
+        # use last_call to remember the state of the previous call, but don't
+        # let it be clobbered by successive '-p' calls.
+        try:
+            last_call[0] = shell.displayhook.prompt_count
+            if not opts_prev:
+                last_call[1] = args
+        except:
+            pass
+
+
         return filename, lineno, use_temp
 
     def _edit_macro(self,mname,macro):


Test patch: