diff --git a/IPython/iplib.py b/IPython/iplib.py index ca749b0..59698f4 100644 --- a/IPython/iplib.py +++ b/IPython/iplib.py @@ -6,7 +6,7 @@ Requires Python 2.3 or newer. This file contains all the classes and helper functions specific to IPython. -$Id: iplib.py 1879 2006-11-04 00:34:34Z fptest $ +$Id: iplib.py 1885 2006-11-08 01:07:28Z fperez $ """ #***************************************************************************** @@ -1844,6 +1844,13 @@ want to merge them back into the new files.""" % locals() The return value can be used to decide whether to use sys.ps1 or sys.ps2 to prompt the next line.""" + # if the source code has leading blanks, add 'if 1:\n' to it + # this allows execution of indented pasted code. It is tempting + # to add '\n' at the end of source to run commands like ' a=1' + # directly, but this fails for more complicated scenarios + if source[:1] in [' ', '\t']: + source = 'if 1:\n%s' % source + try: code = self.compile(source,filename,symbol) except (OverflowError, SyntaxError, ValueError): @@ -2419,13 +2426,37 @@ want to merge them back into the new files.""" % locals() self.exit_now = True def safe_execfile(self,fname,*where,**kw): + """A safe version of the builtin execfile(). + + This version will never throw an exception, and knows how to handle + ipython logs as well.""" + + def syspath_cleanup(): + """Internal cleanup routine for sys.path.""" + if add_dname: + try: + sys.path.remove(dname) + except ValueError: + # For some reason the user has already removed it, ignore. + pass + fname = os.path.expanduser(fname) + # Find things also in current directory. This is needed to mimic the + # behavior of running a script from the system command line, where + # Python inserts the script's directory into sys.path + dname = os.path.dirname(os.path.abspath(fname)) + add_dname = False + if dname not in sys.path: + sys.path.insert(0,dname) + add_dname = True + try: xfile = open(fname) except: print >> Term.cerr, \ 'Could not open file <%s> for safe execution.' % fname + syspath_cleanup() return None kw.setdefault('islog',0) @@ -2512,4 +2543,6 @@ want to merge them back into the new files.""" % locals() self.showtraceback() warn('Failure executing file: <%s>' % fname) + syspath_cleanup() + #************************* end of file ***************************** diff --git a/doc/ChangeLog b/doc/ChangeLog index 35a9bb4..a77631a 100644 --- a/doc/ChangeLog +++ b/doc/ChangeLog @@ -1,3 +1,12 @@ +2006-11-07 Fernando Perez + + * IPython/iplib.py (runsource): Prepend an 'if 1:' to the user + input if it starts with whitespace. This allows you to paste + indented input from any editor without manually having to type in + the 'if 1:', which is conveninent when working interactively. + Slightly modifed version of a patch by Bo Peng + . + 2006-11-03 Fernando Perez * IPython/irunner.py (main): modified irunner so it automatically diff --git a/ipython.py b/ipython.py index a6ab6ea..a4933b6 100755 --- a/ipython.py +++ b/ipython.py @@ -7,12 +7,6 @@ in './scripts' directory. This file is here (ipython source root directory) to facilitate non-root 'zero-installation' (just copy the source tree somewhere and run ipython.py) and development. """ -# Start by cleaning up sys.path: Python automatically inserts the script's -# base directory into sys.path, at the front. This can lead to unpleasant -# surprises. -import sys -sys.path.pop(0) - import IPython IPython.Shell.start().mainloop() diff --git a/scripts/ipython b/scripts/ipython index 5ec4557..b365678 100755 --- a/scripts/ipython +++ b/scripts/ipython @@ -23,11 +23,5 @@ this mode, there is no way to pass IPython any command-line options, as those are trapped first by Python itself. """ -# Start by cleaning up sys.path: Python automatically inserts the script's -# base directory into sys.path, at the front. This can lead to unpleasant -# surprises. -import sys -sys.path.pop(0) - import IPython IPython.Shell.start().mainloop()