##// END OF EJS Templates
- Automatically prepend 'if 1:' to user input that starts with whitespace,...
fperez -
r420:a9e64d26
parent child
Show More
@@ -6,7 +6,7 Requires Python 2.3 or newer.
6
6
7 This file contains all the classes and helper functions specific to IPython.
7 This file contains all the classes and helper functions specific to IPython.
8
8
9 $Id: iplib.py 1879 2006-11-04 00:34:34Z fptest $
9 $Id: iplib.py 1885 2006-11-08 01:07:28Z fperez $
10 """
10 """
11
11
12 #*****************************************************************************
12 #*****************************************************************************
@@ -1844,6 +1844,13 want to merge them back into the new files.""" % locals()
1844 The return value can be used to decide whether to use sys.ps1 or
1844 The return value can be used to decide whether to use sys.ps1 or
1845 sys.ps2 to prompt the next line."""
1845 sys.ps2 to prompt the next line."""
1846
1846
1847 # if the source code has leading blanks, add 'if 1:\n' to it
1848 # this allows execution of indented pasted code. It is tempting
1849 # to add '\n' at the end of source to run commands like ' a=1'
1850 # directly, but this fails for more complicated scenarios
1851 if source[:1] in [' ', '\t']:
1852 source = 'if 1:\n%s' % source
1853
1847 try:
1854 try:
1848 code = self.compile(source,filename,symbol)
1855 code = self.compile(source,filename,symbol)
1849 except (OverflowError, SyntaxError, ValueError):
1856 except (OverflowError, SyntaxError, ValueError):
@@ -2419,13 +2426,37 want to merge them back into the new files.""" % locals()
2419 self.exit_now = True
2426 self.exit_now = True
2420
2427
2421 def safe_execfile(self,fname,*where,**kw):
2428 def safe_execfile(self,fname,*where,**kw):
2429 """A safe version of the builtin execfile().
2430
2431 This version will never throw an exception, and knows how to handle
2432 ipython logs as well."""
2433
2434 def syspath_cleanup():
2435 """Internal cleanup routine for sys.path."""
2436 if add_dname:
2437 try:
2438 sys.path.remove(dname)
2439 except ValueError:
2440 # For some reason the user has already removed it, ignore.
2441 pass
2442
2422 fname = os.path.expanduser(fname)
2443 fname = os.path.expanduser(fname)
2423
2444
2445 # Find things also in current directory. This is needed to mimic the
2446 # behavior of running a script from the system command line, where
2447 # Python inserts the script's directory into sys.path
2448 dname = os.path.dirname(os.path.abspath(fname))
2449 add_dname = False
2450 if dname not in sys.path:
2451 sys.path.insert(0,dname)
2452 add_dname = True
2453
2424 try:
2454 try:
2425 xfile = open(fname)
2455 xfile = open(fname)
2426 except:
2456 except:
2427 print >> Term.cerr, \
2457 print >> Term.cerr, \
2428 'Could not open file <%s> for safe execution.' % fname
2458 'Could not open file <%s> for safe execution.' % fname
2459 syspath_cleanup()
2429 return None
2460 return None
2430
2461
2431 kw.setdefault('islog',0)
2462 kw.setdefault('islog',0)
@@ -2512,4 +2543,6 want to merge them back into the new files.""" % locals()
2512 self.showtraceback()
2543 self.showtraceback()
2513 warn('Failure executing file: <%s>' % fname)
2544 warn('Failure executing file: <%s>' % fname)
2514
2545
2546 syspath_cleanup()
2547
2515 #************************* end of file <iplib.py> *****************************
2548 #************************* end of file <iplib.py> *****************************
@@ -1,3 +1,12
1 2006-11-07 Fernando Perez <Fernando.Perez@colorado.edu>
2
3 * IPython/iplib.py (runsource): Prepend an 'if 1:' to the user
4 input if it starts with whitespace. This allows you to paste
5 indented input from any editor without manually having to type in
6 the 'if 1:', which is conveninent when working interactively.
7 Slightly modifed version of a patch by Bo Peng
8 <bpeng-AT-rice.edu>.
9
1 2006-11-03 Fernando Perez <Fernando.Perez@colorado.edu>
10 2006-11-03 Fernando Perez <Fernando.Perez@colorado.edu>
2
11
3 * IPython/irunner.py (main): modified irunner so it automatically
12 * IPython/irunner.py (main): modified irunner so it automatically
@@ -7,12 +7,6 in './scripts' directory. This file is here (ipython source root directory)
7 to facilitate non-root 'zero-installation' (just copy the source tree
7 to facilitate non-root 'zero-installation' (just copy the source tree
8 somewhere and run ipython.py) and development. """
8 somewhere and run ipython.py) and development. """
9
9
10 # Start by cleaning up sys.path: Python automatically inserts the script's
11 # base directory into sys.path, at the front. This can lead to unpleasant
12 # surprises.
13 import sys
14 sys.path.pop(0)
15
16 import IPython
10 import IPython
17
11
18 IPython.Shell.start().mainloop()
12 IPython.Shell.start().mainloop()
@@ -23,11 +23,5 this mode, there is no way to pass IPython any command-line options, as those
23 are trapped first by Python itself.
23 are trapped first by Python itself.
24 """
24 """
25
25
26 # Start by cleaning up sys.path: Python automatically inserts the script's
27 # base directory into sys.path, at the front. This can lead to unpleasant
28 # surprises.
29 import sys
30 sys.path.pop(0)
31
32 import IPython
26 import IPython
33 IPython.Shell.start().mainloop()
27 IPython.Shell.start().mainloop()
General Comments 0
You need to be logged in to leave comments. Login now