##// END OF EJS Templates
- Fairly significant changes to include Vivian's patches for improved pdb...
fperez -
Show More

The requested changes are too big and content was truncated. Show full diff

@@ -0,0 +1,109 b''
1 # -*- coding: utf-8 -*-
2 """
3 Color schemes for exception handling code in IPython.
4
5 $Id: Prompts.py 638 2005-07-18 03:01:41Z fperez $"""
6
7 #*****************************************************************************
8 # Copyright (C) 2005 Fernando Perez <fperez@colorado.edu>
9 #
10 # Distributed under the terms of the BSD License. The full license is in
11 # the file COPYING, distributed as part of this software.
12 #*****************************************************************************
13
14 from IPython import Release
15 __author__ = '%s <%s>' % Release.authors['Fernando']
16 __license__ = Release.license
17 __version__ = Release.version
18
19 #****************************************************************************
20 # Required modules
21 from IPython.ColorANSI import ColorSchemeTable, TermColors, ColorScheme
22
23 ExceptionColors = ColorSchemeTable()
24
25 # Populate it with color schemes
26 C = TermColors # shorthand and local lookup
27 ExceptionColors.add_scheme(ColorScheme(
28 'NoColor',
29 # The color to be used for the top line
30 topline = C.NoColor,
31
32 # The colors to be used in the traceback
33 filename = C.NoColor,
34 lineno = C.NoColor,
35 name = C.NoColor,
36 vName = C.NoColor,
37 val = C.NoColor,
38 em = C.NoColor,
39
40 # Emphasized colors for the last frame of the traceback
41 normalEm = C.NoColor,
42 filenameEm = C.NoColor,
43 linenoEm = C.NoColor,
44 nameEm = C.NoColor,
45 valEm = C.NoColor,
46
47 # Colors for printing the exception
48 excName = C.NoColor,
49 line = C.NoColor,
50 caret = C.NoColor,
51 Normal = C.NoColor
52 ))
53
54 # make some schemes as instances so we can copy them for modification easily
55 ExceptionColors.add_scheme(ColorScheme(
56 'Linux',
57 # The color to be used for the top line
58 topline = C.LightRed,
59
60 # The colors to be used in the traceback
61 filename = C.Green,
62 lineno = C.Green,
63 name = C.Purple,
64 vName = C.Cyan,
65 val = C.Green,
66 em = C.LightCyan,
67
68 # Emphasized colors for the last frame of the traceback
69 normalEm = C.LightCyan,
70 filenameEm = C.LightGreen,
71 linenoEm = C.LightGreen,
72 nameEm = C.LightPurple,
73 valEm = C.LightBlue,
74
75 # Colors for printing the exception
76 excName = C.LightRed,
77 line = C.Yellow,
78 caret = C.White,
79 Normal = C.Normal
80 ))
81
82 # For light backgrounds, swap dark/light colors
83 ExceptionColors.add_scheme(ColorScheme(
84 'LightBG',
85 # The color to be used for the top line
86 topline = C.Red,
87
88 # The colors to be used in the traceback
89 filename = C.LightGreen,
90 lineno = C.LightGreen,
91 name = C.LightPurple,
92 vName = C.Cyan,
93 val = C.LightGreen,
94 em = C.Cyan,
95
96 # Emphasized colors for the last frame of the traceback
97 normalEm = C.Cyan,
98 filenameEm = C.Green,
99 linenoEm = C.Green,
100 nameEm = C.Purple,
101 valEm = C.Blue,
102
103 # Colors for printing the exception
104 excName = C.Red,
105 #line = C.Brown, # brown often is displayed as yellow
106 line = C.Red,
107 caret = C.Normal,
108 Normal = C.Normal
109 ))
@@ -1,155 +1,164 b''
1 # -*- coding: utf-8 -*-
1 # -*- coding: utf-8 -*-
2 """Tools for coloring text in ANSI terminals.
2 """Tools for coloring text in ANSI terminals.
3
3
4 $Id: ColorANSI.py 410 2004-11-04 07:58:17Z fperez $"""
4 $Id: ColorANSI.py 951 2005-12-25 00:57:24Z fperez $"""
5
5
6 #*****************************************************************************
6 #*****************************************************************************
7 # Copyright (C) 2002-2004 Fernando Perez. <fperez@colorado.edu>
7 # Copyright (C) 2002-2004 Fernando Perez. <fperez@colorado.edu>
8 #
8 #
9 # Distributed under the terms of the BSD License. The full license is in
9 # Distributed under the terms of the BSD License. The full license is in
10 # the file COPYING, distributed as part of this software.
10 # the file COPYING, distributed as part of this software.
11 #*****************************************************************************
11 #*****************************************************************************
12
12
13 from IPython import Release
13 from IPython import Release
14 __author__ = '%s <%s>' % Release.authors['Fernando']
14 __author__ = '%s <%s>' % Release.authors['Fernando']
15 __license__ = Release.license
15 __license__ = Release.license
16
16
17 __all__ = ['TermColors','InputTermColors','ColorScheme','ColorSchemeTable']
17 __all__ = ['TermColors','InputTermColors','ColorScheme','ColorSchemeTable']
18
18
19 import os
19 import os
20 from UserDict import UserDict
21
20
22 from IPython.Struct import Struct
21 from IPython.Struct import Struct
23
22
24 def make_color_table(in_class):
23 def make_color_table(in_class):
25 """Build a set of color attributes in a class.
24 """Build a set of color attributes in a class.
26
25
27 Helper function for building the *TermColors classes."""
26 Helper function for building the *TermColors classes."""
28
27
29 color_templates = (
28 color_templates = (
30 ("Black" , "0;30"),
29 ("Black" , "0;30"),
31 ("Red" , "0;31"),
30 ("Red" , "0;31"),
32 ("Green" , "0;32"),
31 ("Green" , "0;32"),
33 ("Brown" , "0;33"),
32 ("Brown" , "0;33"),
34 ("Blue" , "0;34"),
33 ("Blue" , "0;34"),
35 ("Purple" , "0;35"),
34 ("Purple" , "0;35"),
36 ("Cyan" , "0;36"),
35 ("Cyan" , "0;36"),
37 ("LightGray" , "0;37"),
36 ("LightGray" , "0;37"),
38 ("DarkGray" , "1;30"),
37 ("DarkGray" , "1;30"),
39 ("LightRed" , "1;31"),
38 ("LightRed" , "1;31"),
40 ("LightGreen" , "1;32"),
39 ("LightGreen" , "1;32"),
41 ("Yellow" , "1;33"),
40 ("Yellow" , "1;33"),
42 ("LightBlue" , "1;34"),
41 ("LightBlue" , "1;34"),
43 ("LightPurple" , "1;35"),
42 ("LightPurple" , "1;35"),
44 ("LightCyan" , "1;36"),
43 ("LightCyan" , "1;36"),
45 ("White" , "1;37"), )
44 ("White" , "1;37"), )
46
45
47 for name,value in color_templates:
46 for name,value in color_templates:
48 setattr(in_class,name,in_class._base % value)
47 setattr(in_class,name,in_class._base % value)
49
48
50 class TermColors:
49 class TermColors:
51 """Color escape sequences.
50 """Color escape sequences.
52
51
53 This class defines the escape sequences for all the standard (ANSI?)
52 This class defines the escape sequences for all the standard (ANSI?)
54 colors in terminals. Also defines a NoColor escape which is just the null
53 colors in terminals. Also defines a NoColor escape which is just the null
55 string, suitable for defining 'dummy' color schemes in terminals which get
54 string, suitable for defining 'dummy' color schemes in terminals which get
56 confused by color escapes.
55 confused by color escapes.
57
56
58 This class should be used as a mixin for building color schemes."""
57 This class should be used as a mixin for building color schemes."""
59
58
60 NoColor = '' # for color schemes in color-less terminals.
59 NoColor = '' # for color schemes in color-less terminals.
61 Normal = '\033[0m' # Reset normal coloring
60 Normal = '\033[0m' # Reset normal coloring
62 _base = '\033[%sm' # Template for all other colors
61 _base = '\033[%sm' # Template for all other colors
63
62
64 # Build the actual color table as a set of class attributes:
63 # Build the actual color table as a set of class attributes:
65 make_color_table(TermColors)
64 make_color_table(TermColors)
66
65
67 class InputTermColors:
66 class InputTermColors:
68 """Color escape sequences for input prompts.
67 """Color escape sequences for input prompts.
69
68
70 This class is similar to TermColors, but the escapes are wrapped in \001
69 This class is similar to TermColors, but the escapes are wrapped in \001
71 and \002 so that readline can properly know the length of each line and
70 and \002 so that readline can properly know the length of each line and
72 can wrap lines accordingly. Use this class for any colored text which
71 can wrap lines accordingly. Use this class for any colored text which
73 needs to be used in input prompts, such as in calls to raw_input().
72 needs to be used in input prompts, such as in calls to raw_input().
74
73
75 This class defines the escape sequences for all the standard (ANSI?)
74 This class defines the escape sequences for all the standard (ANSI?)
76 colors in terminals. Also defines a NoColor escape which is just the null
75 colors in terminals. Also defines a NoColor escape which is just the null
77 string, suitable for defining 'dummy' color schemes in terminals which get
76 string, suitable for defining 'dummy' color schemes in terminals which get
78 confused by color escapes.
77 confused by color escapes.
79
78
80 This class should be used as a mixin for building color schemes."""
79 This class should be used as a mixin for building color schemes."""
81
80
82 NoColor = '' # for color schemes in color-less terminals.
81 NoColor = '' # for color schemes in color-less terminals.
83 Normal = '\001\033[0m\002' # Reset normal coloring
82 Normal = '\001\033[0m\002' # Reset normal coloring
84 _base = '\001\033[%sm\002' # Template for all other colors
83 _base = '\001\033[%sm\002' # Template for all other colors
85
84
86 # Build the actual color table as a set of class attributes:
85 # Build the actual color table as a set of class attributes:
87 make_color_table(InputTermColors)
86 make_color_table(InputTermColors)
88
87
89 class ColorScheme:
88 class ColorScheme:
90 """Generic color scheme class. Just a name and a Struct."""
89 """Generic color scheme class. Just a name and a Struct."""
91 def __init__(self,__scheme_name_,colordict=None,**colormap):
90 def __init__(self,__scheme_name_,colordict=None,**colormap):
92 self.name = __scheme_name_
91 self.name = __scheme_name_
93 if colordict is None:
92 if colordict is None:
94 self.colors = Struct(**colormap)
93 self.colors = Struct(**colormap)
95 else:
94 else:
96 self.colors = Struct(colordict)
95 self.colors = Struct(colordict)
97
96
98 class ColorSchemeTable(UserDict):
97 def copy(self,name=None):
98 """Return a full copy of the object, optionally renaming it."""
99 if name is None:
100 name = self.name
101 return ColorScheme(name,self.colors.__dict__)
102
103 class ColorSchemeTable(dict):
99 """General class to handle tables of color schemes.
104 """General class to handle tables of color schemes.
100
105
101 It's basically a dict of color schemes with a couple of shorthand
106 It's basically a dict of color schemes with a couple of shorthand
102 attributes and some convenient methods.
107 attributes and some convenient methods.
103
108
104 active_scheme_name -> obvious
109 active_scheme_name -> obvious
105 active_colors -> actual color table of the active scheme"""
110 active_colors -> actual color table of the active scheme"""
106
111
107 def __init__(self,scheme_list=None,default_scheme=''):
112 def __init__(self,scheme_list=None,default_scheme=''):
108 """Create a table of color schemes.
113 """Create a table of color schemes.
109
114
110 The table can be created empty and manually filled or it can be
115 The table can be created empty and manually filled or it can be
111 created with a list of valid color schemes AND the specification for
116 created with a list of valid color schemes AND the specification for
112 the default active scheme.
117 the default active scheme.
113 """
118 """
114
119
115 UserDict.__init__(self)
120 # create object attributes to be set later
116 if scheme_list is None:
117 self.active_scheme_name = ''
121 self.active_scheme_name = ''
118 self.active_colors = None
122 self.active_colors = None
119 else:
123
124 if scheme_list:
120 if default_scheme == '':
125 if default_scheme == '':
121 raise ValueError,'you must specify the default color scheme'
126 raise ValueError,'you must specify the default color scheme'
122 for scheme in scheme_list:
127 for scheme in scheme_list:
123 self.add_scheme(scheme)
128 self.add_scheme(scheme)
124 self.set_active_scheme(default_scheme)
129 self.set_active_scheme(default_scheme)
125
130
131 def copy(self):
132 """Return full copy of object"""
133 return ColorSchemeTable(self.values(),self.active_scheme_name)
134
126 def add_scheme(self,new_scheme):
135 def add_scheme(self,new_scheme):
127 """Add a new color scheme to the table."""
136 """Add a new color scheme to the table."""
128 if not isinstance(new_scheme,ColorScheme):
137 if not isinstance(new_scheme,ColorScheme):
129 raise ValueError,'ColorSchemeTable only accepts ColorScheme instances'
138 raise ValueError,'ColorSchemeTable only accepts ColorScheme instances'
130 self[new_scheme.name] = new_scheme
139 self[new_scheme.name] = new_scheme
131
140
132 def set_active_scheme(self,scheme,case_sensitive=0):
141 def set_active_scheme(self,scheme,case_sensitive=0):
133 """Set the currently active scheme.
142 """Set the currently active scheme.
134
143
135 Names are by default compared in a case-insensitive way, but this can
144 Names are by default compared in a case-insensitive way, but this can
136 be changed by setting the parameter case_sensitive to true."""
145 be changed by setting the parameter case_sensitive to true."""
137
146
138 scheme_list = self.keys()
147 scheme_names = self.keys()
139 if case_sensitive:
148 if case_sensitive:
140 valid_schemes = scheme_list
149 valid_schemes = scheme_names
141 scheme_test = scheme
150 scheme_test = scheme
142 else:
151 else:
143 valid_schemes = [s.lower() for s in scheme_list]
152 valid_schemes = [s.lower() for s in scheme_names]
144 scheme_test = scheme.lower()
153 scheme_test = scheme.lower()
145 try:
154 try:
146 scheme_idx = valid_schemes.index(scheme_test)
155 scheme_idx = valid_schemes.index(scheme_test)
147 except ValueError:
156 except ValueError:
148 raise ValueError,'Unrecognized color scheme: ' + scheme + \
157 raise ValueError,'Unrecognized color scheme: ' + scheme + \
149 '\nValid schemes: '+str(scheme_list).replace("'', ",'')
158 '\nValid schemes: '+str(scheme_names).replace("'', ",'')
150 else:
159 else:
151 active = scheme_list[scheme_idx]
160 active = scheme_names[scheme_idx]
152 self.active_scheme_name = active
161 self.active_scheme_name = active
153 self.active_colors = self[active].colors
162 self.active_colors = self[active].colors
154 # Now allow using '' as an index for the current active scheme
163 # Now allow using '' as an index for the current active scheme
155 self[''] = self[active]
164 self[''] = self[active]
@@ -1,110 +1,110 b''
1 # -*- coding: utf-8 -*-
1 # -*- coding: utf-8 -*-
2 """sys.excepthook for IPython itself, leaves a detailed report on disk.
2 """sys.excepthook for IPython itself, leaves a detailed report on disk.
3
3
4 $Id: CrashHandler.py 775 2005-09-01 20:24:59Z fperez $"""
4 $Id: CrashHandler.py 951 2005-12-25 00:57:24Z fperez $"""
5
5
6 #*****************************************************************************
6 #*****************************************************************************
7 # Copyright (C) 2001-2004 Fernando Perez. <fperez@colorado.edu>
7 # Copyright (C) 2001-2004 Fernando Perez. <fperez@colorado.edu>
8 #
8 #
9 # Distributed under the terms of the BSD License. The full license is in
9 # Distributed under the terms of the BSD License. The full license is in
10 # the file COPYING, distributed as part of this software.
10 # the file COPYING, distributed as part of this software.
11 #*****************************************************************************
11 #*****************************************************************************
12
12
13 from IPython import Release
13 from IPython import Release
14 __author__ = '%s <%s>' % Release.authors['Fernando']
14 __author__ = '%s <%s>' % Release.authors['Fernando']
15 __license__ = Release.license
15 __license__ = Release.license
16 __version__ = Release.version
16 __version__ = Release.version
17
17
18 #****************************************************************************
18 #****************************************************************************
19 # Required modules
19 # Required modules
20
20
21 # From the standard library
21 # From the standard library
22 import os,sys
22 import os,sys
23 from pprint import pprint,pformat
23 from pprint import pprint,pformat
24
24
25 # Homebrewed
25 # Homebrewed
26 from IPython.genutils import *
27 from IPython.Itpl import Itpl,itpl,printpl
26 from IPython.Itpl import Itpl,itpl,printpl
27 from IPython.ColorANSI import ColorScheme,ColorSchemeTable # too long names
28 from IPython import ultraTB
28 from IPython import ultraTB
29 from IPython.ultraTB import ColorScheme,ColorSchemeTable # too long names
29 from IPython.genutils import *
30
30
31 #****************************************************************************
31 #****************************************************************************
32 class CrashHandler:
32 class CrashHandler:
33 """sys.excepthook for IPython itself, leaves a detailed report on disk."""
33 """sys.excepthook for IPython itself, leaves a detailed report on disk."""
34
34
35 def __init__(self,IP):
35 def __init__(self,IP):
36 self.IP = IP # IPython instance
36 self.IP = IP # IPython instance
37 self.bug_contact = Release.authors['Fernando'][0]
37 self.bug_contact = Release.authors['Fernando'][0]
38 self.mailto = Release.authors['Fernando'][1]
38 self.mailto = Release.authors['Fernando'][1]
39
39
40 def __call__(self,etype, evalue, etb):
40 def __call__(self,etype, evalue, etb):
41
41
42 # Report tracebacks shouldn't use color in general (safer for users)
42 # Report tracebacks shouldn't use color in general (safer for users)
43 color_scheme = 'NoColor'
43 color_scheme = 'NoColor'
44
44
45 # Use this ONLY for developer debugging (keep commented out for release)
45 # Use this ONLY for developer debugging (keep commented out for release)
46 #color_scheme = 'Linux' # dbg
46 #color_scheme = 'Linux' # dbg
47
47
48 try:
48 try:
49 rptdir = self.IP.rc.ipythondir
49 rptdir = self.IP.rc.ipythondir
50 except:
50 except:
51 rptdir = os.getcwd()
51 rptdir = os.getcwd()
52 if not os.path.isdir(rptdir):
52 if not os.path.isdir(rptdir):
53 rptdir = os.getcwd()
53 rptdir = os.getcwd()
54 self.report_name = os.path.join(rptdir,'IPython_crash_report.txt')
54 self.report_name = os.path.join(rptdir,'IPython_crash_report.txt')
55 self.TBhandler = ultraTB.VerboseTB(color_scheme=color_scheme,long_header=1)
55 self.TBhandler = ultraTB.VerboseTB(color_scheme=color_scheme,long_header=1)
56 traceback = self.TBhandler.text(etype,evalue,etb,context=31)
56 traceback = self.TBhandler.text(etype,evalue,etb,context=31)
57
57
58 # print traceback to screen
58 # print traceback to screen
59 print >> sys.stderr, traceback
59 print >> sys.stderr, traceback
60
60
61 # and generate a complete report on disk
61 # and generate a complete report on disk
62 try:
62 try:
63 report = open(self.report_name,'w')
63 report = open(self.report_name,'w')
64 except:
64 except:
65 print >> sys.stderr, 'Could not create crash report on disk.'
65 print >> sys.stderr, 'Could not create crash report on disk.'
66 return
66 return
67
67
68 msg = itpl('\n'+'*'*70+'\n'
68 msg = itpl('\n'+'*'*70+'\n'
69 """
69 """
70 Oops, IPython crashed. We do our best to make it stable, but...
70 Oops, IPython crashed. We do our best to make it stable, but...
71
71
72 A crash report was automatically generated with the following information:
72 A crash report was automatically generated with the following information:
73 - A verbatim copy of the traceback above this text.
73 - A verbatim copy of the traceback above this text.
74 - A copy of your input history during this session.
74 - A copy of your input history during this session.
75 - Data on your current IPython configuration.
75 - Data on your current IPython configuration.
76
76
77 It was left in the file named:
77 It was left in the file named:
78 \t'$self.report_name'
78 \t'$self.report_name'
79 If you can email this file to the developers, the information in it will help
79 If you can email this file to the developers, the information in it will help
80 them in understanding and correcting the problem.
80 them in understanding and correcting the problem.
81
81
82 You can mail it to $self.bug_contact at $self.mailto
82 You can mail it to $self.bug_contact at $self.mailto
83 with the subject 'IPython Crash Report'.
83 with the subject 'IPython Crash Report'.
84
84
85 If you want to do it now, the following command will work (under Unix):
85 If you want to do it now, the following command will work (under Unix):
86 mail -s 'IPython Crash Report' $self.mailto < $self.report_name
86 mail -s 'IPython Crash Report' $self.mailto < $self.report_name
87
87
88 To ensure accurate tracking of this issue, please file a report about it at:
88 To ensure accurate tracking of this issue, please file a report about it at:
89 http://www.scipy.net/roundup/ipython (IPython's online bug tracker).
89 http://www.scipy.net/roundup/ipython (IPython's online bug tracker).
90 """)
90 """)
91 print >> sys.stderr, msg
91 print >> sys.stderr, msg
92
92
93 sec_sep = '\n\n'+'*'*75+'\n\n'
93 sec_sep = '\n\n'+'*'*75+'\n\n'
94 report.write('*'*75+'\n\n'+'IPython post-mortem report\n\n')
94 report.write('*'*75+'\n\n'+'IPython post-mortem report\n\n')
95 report.write('IPython version: %s \n\n' % Release.version)
95 report.write('IPython version: %s \n\n' % Release.version)
96 report.write('SVN revision : %s \n\n' % Release.revision)
96 report.write('SVN revision : %s \n\n' % Release.revision)
97 report.write('Platform info : os.name -> %s, sys.platform -> %s' %
97 report.write('Platform info : os.name -> %s, sys.platform -> %s' %
98 (os.name,sys.platform) )
98 (os.name,sys.platform) )
99 report.write(sec_sep+'Current user configuration structure:\n\n')
99 report.write(sec_sep+'Current user configuration structure:\n\n')
100 report.write(pformat(self.IP.rc.dict()))
100 report.write(pformat(self.IP.rc.dict()))
101 report.write(sec_sep+'Crash traceback:\n\n' + traceback)
101 report.write(sec_sep+'Crash traceback:\n\n' + traceback)
102 try:
102 try:
103 report.write(sec_sep+"History of session input:")
103 report.write(sec_sep+"History of session input:")
104 for line in self.IP.user_ns['_ih']:
104 for line in self.IP.user_ns['_ih']:
105 report.write(line)
105 report.write(line)
106 report.write('\n*** Last line of input (may not be in above history):\n')
106 report.write('\n*** Last line of input (may not be in above history):\n')
107 report.write(self.IP._last_input_line+'\n')
107 report.write(self.IP._last_input_line+'\n')
108 except:
108 except:
109 pass
109 pass
110 report.close()
110 report.close()
@@ -1,53 +1,264 b''
1 # -*- coding: utf-8 -*-
1 # -*- coding: utf-8 -*-
2 """
2 """
3 Pdb debugger class.
3 Pdb debugger class.
4
4
5 Modified from the standard pdb.Pdb class to avoid including readline, so that
5 Modified from the standard pdb.Pdb class to avoid including readline, so that
6 the command line completion of other programs which include this isn't
6 the command line completion of other programs which include this isn't
7 damaged.
7 damaged.
8
8
9 In the future, this class will be expanded with improvements over the standard
9 In the future, this class will be expanded with improvements over the standard
10 pdb.
10 pdb.
11
11
12 The code in this file is mainly lifted out of cmd.py in Python 2.2, with minor
12 The code in this file is mainly lifted out of cmd.py in Python 2.2, with minor
13 changes. Licensing should therefore be under the standard Python terms. For
13 changes. Licensing should therefore be under the standard Python terms. For
14 details on the PSF (Python Software Foundation) standard license, see:
14 details on the PSF (Python Software Foundation) standard license, see:
15
15
16 http://www.python.org/2.2.3/license.html
16 http://www.python.org/2.2.3/license.html
17
17
18 $Id: Debugger.py 590 2005-05-30 06:26:51Z fperez $"""
18 $Id: Debugger.py 951 2005-12-25 00:57:24Z fperez $"""
19
19
20 from IPython import Release
20 from IPython import Release
21 __author__ = '%s <%s>' % Release.authors['Fernando']
21 __author__ = '%s <%s>' % Release.authors['Fernando']
22 __license__ = 'Python'
22 __license__ = 'Python'
23
23
24 import pdb,bdb,cmd,os,sys
24 import pdb,bdb,cmd,os,sys,linecache
25 from IPython import PyColorize, ColorANSI
26 from IPython.genutils import Term
27 from IPython.excolors import ExceptionColors
28
29 def _file_lines(fname):
30 """Return the contents of a named file as a list of lines.
31
32 This function never raises an IOError exception: if the file can't be
33 read, it simply returns an empty list."""
34
35 try:
36 outfile = open(fname)
37 except IOError:
38 return []
39 else:
40 out = outfile.readlines()
41 outfile.close()
42 return out
43
25
44
26 class Pdb(pdb.Pdb):
45 class Pdb(pdb.Pdb):
27 """Modified Pdb class, does not load readline."""
46 """Modified Pdb class, does not load readline."""
28 def __init__(self):
47 def __init__(self,color_scheme='NoColor'):
29 bdb.Bdb.__init__(self)
48 bdb.Bdb.__init__(self)
30 cmd.Cmd.__init__(self,completekey=None) # don't load readline
49 cmd.Cmd.__init__(self,completekey=None) # don't load readline
31 self.prompt = '(Pdb) '
50 self.prompt = 'ipdb> ' # The default prompt is '(Pdb)'
32 self.aliases = {}
51 self.aliases = {}
33
52
34 # Read $HOME/.pdbrc and ./.pdbrc
53 # Read $HOME/.pdbrc and ./.pdbrc
54 try:
55 self.rcLines = _file_lines(os.path.join(os.environ['HOME'],
56 ".pdbrc"))
57 except KeyError:
35 self.rcLines = []
58 self.rcLines = []
36 if os.environ.has_key('HOME'):
59 self.rcLines.extend(_file_lines(".pdbrc"))
37 envHome = os.environ['HOME']
60
61 # Create color table: we copy the default one from the traceback
62 # module and add a few attributes needed for debugging
63 self.color_scheme_table = ExceptionColors.copy()
64
65 # shorthands
66 C = ColorANSI.TermColors
67 cst = self.color_scheme_table
68
69 cst['NoColor'].colors.breakpoint_enabled = C.NoColor
70 cst['NoColor'].colors.breakpoint_disabled = C.NoColor
71
72 cst['Linux'].colors.breakpoint_enabled = C.LightRed
73 cst['Linux'].colors.breakpoint_disabled = C.Red
74
75 cst['LightBG'].colors.breakpoint_enabled = C.LightRed
76 cst['LightBG'].colors.breakpoint_disabled = C.Red
77
78 self.set_colors(color_scheme)
79
80 def set_colors(self, scheme):
81 """Shorthand access to the color table scheme selector method."""
82 self.color_scheme_table.set_active_scheme(scheme)
83
84
85 def interaction(self, frame, traceback):
86 __IPYTHON__.set_completer_frame(frame)
87 pdb.Pdb.interaction(self, frame, traceback)
88
89
90 def do_up(self, arg):
91 pdb.Pdb.do_up(self, arg)
92 __IPYTHON__.set_completer_frame(self.curframe)
93 do_u = do_up
94
95
96 def do_down(self, arg):
97 pdb.Pdb.do_down(self, arg)
98 __IPYTHON__.set_completer_frame(self.curframe)
99 do_d = do_down
100
101
102 def postloop(self):
103 __IPYTHON__.set_completer_frame(None)
104
105
106 def print_stack_trace(self):
38 try:
107 try:
39 rcFile = open(os.path.join(envHome, ".pdbrc"))
108 for frame_lineno in self.stack:
40 except IOError:
109 self.print_stack_entry(frame_lineno, context = 5)
110 except KeyboardInterrupt:
41 pass
111 pass
112
113
114 def print_stack_entry(self,frame_lineno,prompt_prefix='\n-> ',
115 context = 3):
116 frame, lineno = frame_lineno
117 print >>Term.cout, self.format_stack_entry(frame_lineno, '', context)
118
119
120 def format_stack_entry(self, frame_lineno, lprefix=': ', context = 3):
121 import linecache, repr
122
123 ret = ""
124
125 Colors = self.color_scheme_table.active_colors
126 ColorsNormal = Colors.Normal
127 tpl_link = '%s%%s%s' % (Colors.filenameEm, ColorsNormal)
128 tpl_call = 'in %s%%s%s%%s%s' % (Colors.vName, Colors.valEm, ColorsNormal)
129 tpl_line = '%%s%s%%s %s%%s' % (Colors.lineno, ColorsNormal)
130 tpl_line_em = '%%s%s%%s %s%%s%s' % (Colors.linenoEm, Colors.line,
131 ColorsNormal)
132
133 frame, lineno = frame_lineno
134
135 return_value = ''
136 if '__return__' in frame.f_locals:
137 rv = frame.f_locals['__return__']
138 #return_value += '->'
139 return_value += repr.repr(rv) + '\n'
140 ret += return_value
141
142 #s = filename + '(' + `lineno` + ')'
143 filename = self.canonic(frame.f_code.co_filename)
144 link = tpl_link % filename
145
146 if frame.f_code.co_name:
147 func = frame.f_code.co_name
148 else:
149 func = "<lambda>"
150
151 call = ''
152 if func != '?':
153 if '__args__' in frame.f_locals:
154 args = repr.repr(frame.f_locals['__args__'])
155 else:
156 args = '()'
157 call = tpl_call % (func, args)
158
159 level = '%s %s\n' % (link, call)
160 ret += level
161
162 start = lineno - 1 - context//2
163 lines = linecache.getlines(filename)
164 start = max(start, 0)
165 start = min(start, len(lines) - context)
166 lines = lines[start : start + context]
167
168 for i in range(len(lines)):
169 line = lines[i]
170 if start + 1 + i == lineno:
171 ret += self.__format_line(tpl_line_em, filename, start + 1 + i, line, arrow = True)
172 else:
173 ret += self.__format_line(tpl_line, filename, start + 1 + i, line, arrow = False)
174
175 return ret
176
177
178 def __format_line(self, tpl_line, filename, lineno, line, arrow = False):
179 bp_mark = ""
180 bp_mark_color = ""
181
182 bp = None
183 if lineno in self.get_file_breaks(filename):
184 bps = self.get_breaks(filename, lineno)
185 bp = bps[-1]
186
187 if bp:
188 Colors = self.color_scheme_table.active_colors
189 bp_mark = str(bp.number)
190 bp_mark_color = Colors.breakpoint_enabled
191 if not bp.enabled:
192 bp_mark_color = Colors.breakpoint_disabled
193
194 numbers_width = 7
195 if arrow:
196 # This is the line with the error
197 pad = numbers_width - len(str(lineno)) - len(bp_mark)
198 if pad >= 3:
199 marker = '-'*(pad-3) + '-> '
200 elif pad == 2:
201 marker = '> '
202 elif pad == 1:
203 marker = '>'
204 else:
205 marker = ''
206 num = '%s%s' % (marker, str(lineno))
207 line = tpl_line % (bp_mark_color + bp_mark, num, line)
42 else:
208 else:
43 for line in rcFile.readlines():
209 num = '%*s' % (numbers_width - len(bp_mark), str(lineno))
44 self.rcLines.append(line)
210 line = tpl_line % (bp_mark_color + bp_mark, num, line)
45 rcFile.close()
211
212 return line
213
214
215 def do_list(self, arg):
216 self.lastcmd = 'list'
217 last = None
218 if arg:
46 try:
219 try:
47 rcFile = open(".pdbrc")
220 x = eval(arg, {}, {})
48 except IOError:
221 if type(x) == type(()):
49 pass
222 first, last = x
223 first = int(first)
224 last = int(last)
225 if last < first:
226 # Assume it's a count
227 last = first + last
228 else:
229 first = max(1, int(x) - 5)
230 except:
231 print '*** Error in argument:', `arg`
232 return
233 elif self.lineno is None:
234 first = max(1, self.curframe.f_lineno - 5)
50 else:
235 else:
51 for line in rcFile.readlines():
236 first = self.lineno + 1
52 self.rcLines.append(line)
237 if last is None:
53 rcFile.close()
238 last = first + 10
239 filename = self.curframe.f_code.co_filename
240 try:
241 Colors = self.color_scheme_table.active_colors
242 ColorsNormal = Colors.Normal
243 tpl_line = '%%s%s%%s %s%%s' % (Colors.lineno, ColorsNormal)
244 tpl_line_em = '%%s%s%%s %s%%s%s' % (Colors.linenoEm, Colors.line, ColorsNormal)
245 src = []
246 for lineno in range(first, last+1):
247 line = linecache.getline(filename, lineno)
248 if not line:
249 break
250
251 if lineno == self.curframe.f_lineno:
252 line = self.__format_line(tpl_line_em, filename, lineno, line, arrow = True)
253 else:
254 line = self.__format_line(tpl_line, filename, lineno, line, arrow = False)
255
256 src.append(line)
257 self.lineno = lineno
258
259 print >>Term.cout, ''.join(src)
260
261 except KeyboardInterrupt:
262 pass
263
264 do_l = do_list
@@ -1,2563 +1,2573 b''
1 # -*- coding: utf-8 -*-
1 # -*- coding: utf-8 -*-
2 """Magic functions for InteractiveShell.
2 """Magic functions for InteractiveShell.
3
3
4 $Id: Magic.py 923 2005-11-15 08:51:15Z fperez $"""
4 $Id: Magic.py 951 2005-12-25 00:57:24Z fperez $"""
5
5
6 #*****************************************************************************
6 #*****************************************************************************
7 # Copyright (C) 2001 Janko Hauser <jhauser@zscout.de> and
7 # Copyright (C) 2001 Janko Hauser <jhauser@zscout.de> and
8 # Copyright (C) 2001-2004 Fernando Perez <fperez@colorado.edu>
8 # Copyright (C) 2001-2004 Fernando Perez <fperez@colorado.edu>
9 #
9 #
10 # Distributed under the terms of the BSD License. The full license is in
10 # Distributed under the terms of the BSD License. The full license is in
11 # the file COPYING, distributed as part of this software.
11 # the file COPYING, distributed as part of this software.
12 #*****************************************************************************
12 #*****************************************************************************
13
13
14 #****************************************************************************
14 #****************************************************************************
15 # Modules and globals
15 # Modules and globals
16
16
17 from IPython import Release
17 from IPython import Release
18 __author__ = '%s <%s>\n%s <%s>' % \
18 __author__ = '%s <%s>\n%s <%s>' % \
19 ( Release.authors['Janko'] + Release.authors['Fernando'] )
19 ( Release.authors['Janko'] + Release.authors['Fernando'] )
20 __license__ = Release.license
20 __license__ = Release.license
21
21
22 # Python standard modules
22 # Python standard modules
23 import __builtin__
23 import __builtin__
24 import os,sys,inspect,pydoc,re,tempfile,pdb,bdb,time
24 import os,sys,inspect,pydoc,re,tempfile,pdb,bdb,time
25 import Debugger
26 from getopt import getopt
27 from pprint import pprint, pformat
28 from cStringIO import StringIO
29
30 # profile isn't bundled by default in Debian for license reasons
25 try:
31 try:
26 import profile,pstats
32 import profile,pstats
27 except ImportError:
33 except ImportError:
28 profile = pstats = None
34 profile = pstats = None
29 from getopt import getopt
30 from pprint import pprint, pformat
31 from cStringIO import StringIO
32
35
33 # Homebrewed
36 # Homebrewed
34 from IPython.Struct import Struct
37 from IPython.Struct import Struct
35 from IPython.Itpl import Itpl, itpl, printpl,itplns
38 from IPython.Itpl import Itpl, itpl, printpl,itplns
36 from IPython.FakeModule import FakeModule
39 from IPython.FakeModule import FakeModule
37 from IPython.PyColorize import Parser
40 from IPython.PyColorize import Parser
38 from IPython import OInspect
41 from IPython import OInspect
39 from IPython import wildcard
42 from IPython import wildcard
40 from IPython.genutils import *
43 from IPython.genutils import *
41
44
42 # Globals to be set later by Magic constructor
45 # Globals to be set later by Magic constructor
43 MAGIC_PREFIX = ''
46 MAGIC_PREFIX = ''
44 MAGIC_ESCAPE = ''
47 MAGIC_ESCAPE = ''
45
48
46 #***************************************************************************
49 #***************************************************************************
47 # Utility functions
50 # Utility functions
48 def magic2python(cmd):
51 def magic2python(cmd):
49 """Convert a command string of magic syntax to valid Python code."""
52 """Convert a command string of magic syntax to valid Python code."""
50
53
51 if cmd.startswith('#'+MAGIC_ESCAPE) or \
54 if cmd.startswith('#'+MAGIC_ESCAPE) or \
52 cmd.startswith(MAGIC_ESCAPE):
55 cmd.startswith(MAGIC_ESCAPE):
53 if cmd[0]=='#':
56 if cmd[0]=='#':
54 cmd = cmd[1:]
57 cmd = cmd[1:]
55 # we need to return the proper line end later
58 # we need to return the proper line end later
56 if cmd[-1] == '\n':
59 if cmd[-1] == '\n':
57 endl = '\n'
60 endl = '\n'
58 else:
61 else:
59 endl = ''
62 endl = ''
60 try:
63 try:
61 func,args = cmd[1:].split(' ',1)
64 func,args = cmd[1:].split(' ',1)
62 except:
65 except:
63 func,args = cmd[1:].rstrip(),''
66 func,args = cmd[1:].rstrip(),''
64 args = args.replace('"','\\"').replace("'","\\'").rstrip()
67 args = args.replace('"','\\"').replace("'","\\'").rstrip()
65 return '%s%s ("%s")%s' % (MAGIC_PREFIX,func,args,endl)
68 return '%s%s ("%s")%s' % (MAGIC_PREFIX,func,args,endl)
66 else:
69 else:
67 return cmd
70 return cmd
68
71
69 def on_off(tag):
72 def on_off(tag):
70 """Return an ON/OFF string for a 1/0 input. Simple utility function."""
73 """Return an ON/OFF string for a 1/0 input. Simple utility function."""
71 return ['OFF','ON'][tag]
74 return ['OFF','ON'][tag]
72
75
73
76
74 #****************************************************************************
77 #****************************************************************************
75 # Utility classes
78 # Utility classes
76 class Macro:
79 class Macro:
77 """Simple class to store the value of macros as strings.
80 """Simple class to store the value of macros as strings.
78
81
79 This allows us to later exec them by checking when something is an
82 This allows us to later exec them by checking when something is an
80 instance of this class."""
83 instance of this class."""
81
84
82 def __init__(self,cmds):
85 def __init__(self,cmds):
83 """Build a macro from a list of commands."""
86 """Build a macro from a list of commands."""
84
87
85 # Since the list may include multi-line entries, first make sure that
88 # Since the list may include multi-line entries, first make sure that
86 # they've been all broken up before passing it to magic2python
89 # they've been all broken up before passing it to magic2python
87 cmdlist = map(magic2python,''.join(cmds).split('\n'))
90 cmdlist = map(magic2python,''.join(cmds).split('\n'))
88 self.value = '\n'.join(cmdlist)
91 self.value = '\n'.join(cmdlist)
89
92
90 def __str__(self):
93 def __str__(self):
91 return self.value
94 return self.value
92
95
93 #***************************************************************************
96 #***************************************************************************
94 # Main class implementing Magic functionality
97 # Main class implementing Magic functionality
95 class Magic:
98 class Magic:
96 """Magic functions for InteractiveShell.
99 """Magic functions for InteractiveShell.
97
100
98 Shell functions which can be reached as %function_name. All magic
101 Shell functions which can be reached as %function_name. All magic
99 functions should accept a string, which they can parse for their own
102 functions should accept a string, which they can parse for their own
100 needs. This can make some functions easier to type, eg `%cd ../`
103 needs. This can make some functions easier to type, eg `%cd ../`
101 vs. `%cd("../")`
104 vs. `%cd("../")`
102
105
103 ALL definitions MUST begin with the prefix magic_. The user won't need it
106 ALL definitions MUST begin with the prefix magic_. The user won't need it
104 at the command line, but it is is needed in the definition. """
107 at the command line, but it is is needed in the definition. """
105
108
106 # class globals
109 # class globals
107 auto_status = ['Automagic is OFF, % prefix IS needed for magic functions.',
110 auto_status = ['Automagic is OFF, % prefix IS needed for magic functions.',
108 'Automagic is ON, % prefix NOT needed for magic functions.']
111 'Automagic is ON, % prefix NOT needed for magic functions.']
109
112
110 #......................................................................
113 #......................................................................
111 # some utility functions
114 # some utility functions
112
115
113 def __init__(self,shell):
116 def __init__(self,shell):
114 # XXX This is hackish, clean up later to avoid these messy globals
117 # XXX This is hackish, clean up later to avoid these messy globals
115 global MAGIC_PREFIX, MAGIC_ESCAPE
118 global MAGIC_PREFIX, MAGIC_ESCAPE
116
119
117 self.options_table = {}
120 self.options_table = {}
118 MAGIC_PREFIX = shell.name+'.magic_'
121 MAGIC_PREFIX = shell.name+'.magic_'
119 MAGIC_ESCAPE = shell.ESC_MAGIC
122 MAGIC_ESCAPE = shell.ESC_MAGIC
120 if profile is None:
123 if profile is None:
121 self.magic_prun = self.profile_missing_notice
124 self.magic_prun = self.profile_missing_notice
122
125
123 def profile_missing_notice(self, *args, **kwargs):
126 def profile_missing_notice(self, *args, **kwargs):
124 error("""\
127 error("""\
125 The profile module could not be found. If you are a Debian user,
128 The profile module could not be found. If you are a Debian user,
126 it has been removed from the standard Debian package because of its non-free
129 it has been removed from the standard Debian package because of its non-free
127 license. To use profiling, please install"python2.3-profiler" from non-free.""")
130 license. To use profiling, please install"python2.3-profiler" from non-free.""")
128
131
129 def default_option(self,fn,optstr):
132 def default_option(self,fn,optstr):
130 """Make an entry in the options_table for fn, with value optstr"""
133 """Make an entry in the options_table for fn, with value optstr"""
131
134
132 if fn not in self.lsmagic():
135 if fn not in self.lsmagic():
133 error("%s is not a magic function" % fn)
136 error("%s is not a magic function" % fn)
134 self.options_table[fn] = optstr
137 self.options_table[fn] = optstr
135
138
136 def lsmagic(self):
139 def lsmagic(self):
137 """Return a list of currently available magic functions.
140 """Return a list of currently available magic functions.
138
141
139 Gives a list of the bare names after mangling (['ls','cd', ...], not
142 Gives a list of the bare names after mangling (['ls','cd', ...], not
140 ['magic_ls','magic_cd',...]"""
143 ['magic_ls','magic_cd',...]"""
141
144
142 # FIXME. This needs a cleanup, in the way the magics list is built.
145 # FIXME. This needs a cleanup, in the way the magics list is built.
143
146
144 # magics in class definition
147 # magics in class definition
145 class_magic = lambda fn: fn.startswith('magic_') and \
148 class_magic = lambda fn: fn.startswith('magic_') and \
146 callable(Magic.__dict__[fn])
149 callable(Magic.__dict__[fn])
147 # in instance namespace (run-time user additions)
150 # in instance namespace (run-time user additions)
148 inst_magic = lambda fn: fn.startswith('magic_') and \
151 inst_magic = lambda fn: fn.startswith('magic_') and \
149 callable(self.__dict__[fn])
152 callable(self.__dict__[fn])
150 # and bound magics by user (so they can access self):
153 # and bound magics by user (so they can access self):
151 inst_bound_magic = lambda fn: fn.startswith('magic_') and \
154 inst_bound_magic = lambda fn: fn.startswith('magic_') and \
152 callable(self.__class__.__dict__[fn])
155 callable(self.__class__.__dict__[fn])
153 magics = filter(class_magic,Magic.__dict__.keys()) + \
156 magics = filter(class_magic,Magic.__dict__.keys()) + \
154 filter(inst_magic,self.__dict__.keys()) + \
157 filter(inst_magic,self.__dict__.keys()) + \
155 filter(inst_bound_magic,self.__class__.__dict__.keys())
158 filter(inst_bound_magic,self.__class__.__dict__.keys())
156 out = []
159 out = []
157 for fn in magics:
160 for fn in magics:
158 out.append(fn.replace('magic_','',1))
161 out.append(fn.replace('magic_','',1))
159 out.sort()
162 out.sort()
160 return out
163 return out
161
164
162 def set_shell(self,shell):
165 def set_shell(self,shell):
163 self.shell = shell
166 self.shell = shell
164 self.alias_table = shell.alias_table
167 self.alias_table = shell.alias_table
165
168
166 def extract_input_slices(self,slices):
169 def extract_input_slices(self,slices):
167 """Return as a string a set of input history slices.
170 """Return as a string a set of input history slices.
168
171
169 The set of slices is given as a list of strings (like ['1','4:8','9'],
172 The set of slices is given as a list of strings (like ['1','4:8','9'],
170 since this function is for use by magic functions which get their
173 since this function is for use by magic functions which get their
171 arguments as strings."""
174 arguments as strings."""
172
175
173 cmds = []
176 cmds = []
174 for chunk in slices:
177 for chunk in slices:
175 if ':' in chunk:
178 if ':' in chunk:
176 ini,fin = map(int,chunk.split(':'))
179 ini,fin = map(int,chunk.split(':'))
177 else:
180 else:
178 ini = int(chunk)
181 ini = int(chunk)
179 fin = ini+1
182 fin = ini+1
180 cmds.append(self.shell.input_hist[ini:fin])
183 cmds.append(self.shell.input_hist[ini:fin])
181 return cmds
184 return cmds
182
185
183 def _ofind(self,oname):
186 def _ofind(self,oname):
184 """Find an object in the available namespaces.
187 """Find an object in the available namespaces.
185
188
186 self._ofind(oname) -> dict with keys: found,obj,ospace,ismagic
189 self._ofind(oname) -> dict with keys: found,obj,ospace,ismagic
187
190
188 Has special code to detect magic functions.
191 Has special code to detect magic functions.
189 """
192 """
190
193
191 oname = oname.strip()
194 oname = oname.strip()
192
195
193 # Namespaces to search in:
196 # Namespaces to search in:
194 user_ns = self.shell.user_ns
197 user_ns = self.shell.user_ns
195 internal_ns = self.shell.internal_ns
198 internal_ns = self.shell.internal_ns
196 builtin_ns = __builtin__.__dict__
199 builtin_ns = __builtin__.__dict__
197 alias_ns = self.shell.alias_table
200 alias_ns = self.shell.alias_table
198
201
199 # Put them in a list. The order is important so that we find things in
202 # Put them in a list. The order is important so that we find things in
200 # the same order that Python finds them.
203 # the same order that Python finds them.
201 namespaces = [ ('Interactive',user_ns),
204 namespaces = [ ('Interactive',user_ns),
202 ('IPython internal',internal_ns),
205 ('IPython internal',internal_ns),
203 ('Python builtin',builtin_ns),
206 ('Python builtin',builtin_ns),
204 ('Alias',alias_ns),
207 ('Alias',alias_ns),
205 ]
208 ]
206
209
207 # initialize results to 'null'
210 # initialize results to 'null'
208 found = 0; obj = None; ospace = None; ds = None;
211 found = 0; obj = None; ospace = None; ds = None;
209 ismagic = 0; isalias = 0
212 ismagic = 0; isalias = 0
210
213
211 # Look for the given name by splitting it in parts. If the head is
214 # Look for the given name by splitting it in parts. If the head is
212 # found, then we look for all the remaining parts as members, and only
215 # found, then we look for all the remaining parts as members, and only
213 # declare success if we can find them all.
216 # declare success if we can find them all.
214 oname_parts = oname.split('.')
217 oname_parts = oname.split('.')
215 oname_head, oname_rest = oname_parts[0],oname_parts[1:]
218 oname_head, oname_rest = oname_parts[0],oname_parts[1:]
216 for nsname,ns in namespaces:
219 for nsname,ns in namespaces:
217 try:
220 try:
218 obj = ns[oname_head]
221 obj = ns[oname_head]
219 except KeyError:
222 except KeyError:
220 continue
223 continue
221 else:
224 else:
222 for part in oname_rest:
225 for part in oname_rest:
223 try:
226 try:
224 obj = getattr(obj,part)
227 obj = getattr(obj,part)
225 except:
228 except:
226 # Blanket except b/c some badly implemented objects
229 # Blanket except b/c some badly implemented objects
227 # allow __getattr__ to raise exceptions other than
230 # allow __getattr__ to raise exceptions other than
228 # AttributeError, which then crashes IPython.
231 # AttributeError, which then crashes IPython.
229 break
232 break
230 else:
233 else:
231 # If we finish the for loop (no break), we got all members
234 # If we finish the for loop (no break), we got all members
232 found = 1
235 found = 1
233 ospace = nsname
236 ospace = nsname
234 if ns == alias_ns:
237 if ns == alias_ns:
235 isalias = 1
238 isalias = 1
236 break # namespace loop
239 break # namespace loop
237
240
238 # Try to see if it's magic
241 # Try to see if it's magic
239 if not found:
242 if not found:
240 if oname.startswith(self.shell.ESC_MAGIC):
243 if oname.startswith(self.shell.ESC_MAGIC):
241 oname = oname[1:]
244 oname = oname[1:]
242 obj = getattr(self,'magic_'+oname,None)
245 obj = getattr(self,'magic_'+oname,None)
243 if obj is not None:
246 if obj is not None:
244 found = 1
247 found = 1
245 ospace = 'IPython internal'
248 ospace = 'IPython internal'
246 ismagic = 1
249 ismagic = 1
247
250
248 # Last try: special-case some literals like '', [], {}, etc:
251 # Last try: special-case some literals like '', [], {}, etc:
249 if not found and oname_head in ["''",'""','[]','{}','()']:
252 if not found and oname_head in ["''",'""','[]','{}','()']:
250 obj = eval(oname_head)
253 obj = eval(oname_head)
251 found = 1
254 found = 1
252 ospace = 'Interactive'
255 ospace = 'Interactive'
253
256
254 return {'found':found, 'obj':obj, 'namespace':ospace,
257 return {'found':found, 'obj':obj, 'namespace':ospace,
255 'ismagic':ismagic, 'isalias':isalias}
258 'ismagic':ismagic, 'isalias':isalias}
256
259
257 def arg_err(self,func):
260 def arg_err(self,func):
258 """Print docstring if incorrect arguments were passed"""
261 """Print docstring if incorrect arguments were passed"""
259 print 'Error in arguments:'
262 print 'Error in arguments:'
260 print OInspect.getdoc(func)
263 print OInspect.getdoc(func)
261
264
262
265
263 def format_latex(self,str):
266 def format_latex(self,str):
264 """Format a string for latex inclusion."""
267 """Format a string for latex inclusion."""
265
268
266 # Characters that need to be escaped for latex:
269 # Characters that need to be escaped for latex:
267 escape_re = re.compile(r'(%|_|\$)',re.MULTILINE)
270 escape_re = re.compile(r'(%|_|\$)',re.MULTILINE)
268 # Magic command names as headers:
271 # Magic command names as headers:
269 cmd_name_re = re.compile(r'^(%s.*?):' % self.shell.ESC_MAGIC,
272 cmd_name_re = re.compile(r'^(%s.*?):' % self.shell.ESC_MAGIC,
270 re.MULTILINE)
273 re.MULTILINE)
271 # Magic commands
274 # Magic commands
272 cmd_re = re.compile(r'(?P<cmd>%s.+?\b)(?!\}\}:)' % self.shell.ESC_MAGIC,
275 cmd_re = re.compile(r'(?P<cmd>%s.+?\b)(?!\}\}:)' % self.shell.ESC_MAGIC,
273 re.MULTILINE)
276 re.MULTILINE)
274 # Paragraph continue
277 # Paragraph continue
275 par_re = re.compile(r'\\$',re.MULTILINE)
278 par_re = re.compile(r'\\$',re.MULTILINE)
276
279
277 str = cmd_name_re.sub(r'\n\\texttt{\\textsl{\\large \1}}:',str)
280 str = cmd_name_re.sub(r'\n\\texttt{\\textsl{\\large \1}}:',str)
278 str = cmd_re.sub(r'\\texttt{\g<cmd>}',str)
281 str = cmd_re.sub(r'\\texttt{\g<cmd>}',str)
279 str = par_re.sub(r'\\\\',str)
282 str = par_re.sub(r'\\\\',str)
280 str = escape_re.sub(r'\\\1',str)
283 str = escape_re.sub(r'\\\1',str)
281 return str
284 return str
282
285
283 def format_screen(self,str):
286 def format_screen(self,str):
284 """Format a string for screen printing.
287 """Format a string for screen printing.
285
288
286 This removes some latex-type format codes."""
289 This removes some latex-type format codes."""
287 # Paragraph continue
290 # Paragraph continue
288 par_re = re.compile(r'\\$',re.MULTILINE)
291 par_re = re.compile(r'\\$',re.MULTILINE)
289 str = par_re.sub('',str)
292 str = par_re.sub('',str)
290 return str
293 return str
291
294
292 def parse_options(self,arg_str,opt_str,*long_opts,**kw):
295 def parse_options(self,arg_str,opt_str,*long_opts,**kw):
293 """Parse options passed to an argument string.
296 """Parse options passed to an argument string.
294
297
295 The interface is similar to that of getopt(), but it returns back a
298 The interface is similar to that of getopt(), but it returns back a
296 Struct with the options as keys and the stripped argument string still
299 Struct with the options as keys and the stripped argument string still
297 as a string.
300 as a string.
298
301
299 arg_str is quoted as a true sys.argv vector by using shlex.split.
302 arg_str is quoted as a true sys.argv vector by using shlex.split.
300 This allows us to easily expand variables, glob files, quote
303 This allows us to easily expand variables, glob files, quote
301 arguments, etc.
304 arguments, etc.
302
305
303 Options:
306 Options:
304 -mode: default 'string'. If given as 'list', the argument string is
307 -mode: default 'string'. If given as 'list', the argument string is
305 returned as a list (split on whitespace) instead of a string.
308 returned as a list (split on whitespace) instead of a string.
306
309
307 -list_all: put all option values in lists. Normally only options
310 -list_all: put all option values in lists. Normally only options
308 appearing more than once are put in a list."""
311 appearing more than once are put in a list."""
309
312
310 # inject default options at the beginning of the input line
313 # inject default options at the beginning of the input line
311 caller = sys._getframe(1).f_code.co_name.replace('magic_','')
314 caller = sys._getframe(1).f_code.co_name.replace('magic_','')
312 arg_str = '%s %s' % (self.options_table.get(caller,''),arg_str)
315 arg_str = '%s %s' % (self.options_table.get(caller,''),arg_str)
313
316
314 mode = kw.get('mode','string')
317 mode = kw.get('mode','string')
315 if mode not in ['string','list']:
318 if mode not in ['string','list']:
316 raise ValueError,'incorrect mode given: %s' % mode
319 raise ValueError,'incorrect mode given: %s' % mode
317 # Get options
320 # Get options
318 list_all = kw.get('list_all',0)
321 list_all = kw.get('list_all',0)
319
322
320 # Check if we have more than one argument to warrant extra processing:
323 # Check if we have more than one argument to warrant extra processing:
321 odict = {} # Dictionary with options
324 odict = {} # Dictionary with options
322 args = arg_str.split()
325 args = arg_str.split()
323 if len(args) >= 1:
326 if len(args) >= 1:
324 # If the list of inputs only has 0 or 1 thing in it, there's no
327 # If the list of inputs only has 0 or 1 thing in it, there's no
325 # need to look for options
328 # need to look for options
326 argv = shlex_split(arg_str)
329 argv = shlex_split(arg_str)
327 # Do regular option processing
330 # Do regular option processing
328 opts,args = getopt(argv,opt_str,*long_opts)
331 opts,args = getopt(argv,opt_str,*long_opts)
329 for o,a in opts:
332 for o,a in opts:
330 if o.startswith('--'):
333 if o.startswith('--'):
331 o = o[2:]
334 o = o[2:]
332 else:
335 else:
333 o = o[1:]
336 o = o[1:]
334 try:
337 try:
335 odict[o].append(a)
338 odict[o].append(a)
336 except AttributeError:
339 except AttributeError:
337 odict[o] = [odict[o],a]
340 odict[o] = [odict[o],a]
338 except KeyError:
341 except KeyError:
339 if list_all:
342 if list_all:
340 odict[o] = [a]
343 odict[o] = [a]
341 else:
344 else:
342 odict[o] = a
345 odict[o] = a
343
346
344 # Prepare opts,args for return
347 # Prepare opts,args for return
345 opts = Struct(odict)
348 opts = Struct(odict)
346 if mode == 'string':
349 if mode == 'string':
347 args = ' '.join(args)
350 args = ' '.join(args)
348
351
349 return opts,args
352 return opts,args
350
353
351 #......................................................................
354 #......................................................................
352 # And now the actual magic functions
355 # And now the actual magic functions
353
356
354 # Functions for IPython shell work (vars,funcs, config, etc)
357 # Functions for IPython shell work (vars,funcs, config, etc)
355 def magic_lsmagic(self, parameter_s = ''):
358 def magic_lsmagic(self, parameter_s = ''):
356 """List currently available magic functions."""
359 """List currently available magic functions."""
357 mesc = self.shell.ESC_MAGIC
360 mesc = self.shell.ESC_MAGIC
358 print 'Available magic functions:\n'+mesc+\
361 print 'Available magic functions:\n'+mesc+\
359 (' '+mesc).join(self.lsmagic())
362 (' '+mesc).join(self.lsmagic())
360 print '\n' + Magic.auto_status[self.shell.rc.automagic]
363 print '\n' + Magic.auto_status[self.shell.rc.automagic]
361 return None
364 return None
362
365
363 def magic_magic(self, parameter_s = ''):
366 def magic_magic(self, parameter_s = ''):
364 """Print information about the magic function system."""
367 """Print information about the magic function system."""
365
368
366 mode = ''
369 mode = ''
367 try:
370 try:
368 if parameter_s.split()[0] == '-latex':
371 if parameter_s.split()[0] == '-latex':
369 mode = 'latex'
372 mode = 'latex'
370 except:
373 except:
371 pass
374 pass
372
375
373 magic_docs = []
376 magic_docs = []
374 for fname in self.lsmagic():
377 for fname in self.lsmagic():
375 mname = 'magic_' + fname
378 mname = 'magic_' + fname
376 for space in (Magic,self,self.__class__):
379 for space in (Magic,self,self.__class__):
377 try:
380 try:
378 fn = space.__dict__[mname]
381 fn = space.__dict__[mname]
379 except KeyError:
382 except KeyError:
380 pass
383 pass
381 else:
384 else:
382 break
385 break
383 magic_docs.append('%s%s:\n\t%s\n' %(self.shell.ESC_MAGIC,
386 magic_docs.append('%s%s:\n\t%s\n' %(self.shell.ESC_MAGIC,
384 fname,fn.__doc__))
387 fname,fn.__doc__))
385 magic_docs = ''.join(magic_docs)
388 magic_docs = ''.join(magic_docs)
386
389
387 if mode == 'latex':
390 if mode == 'latex':
388 print self.format_latex(magic_docs)
391 print self.format_latex(magic_docs)
389 return
392 return
390 else:
393 else:
391 magic_docs = self.format_screen(magic_docs)
394 magic_docs = self.format_screen(magic_docs)
392
395
393 outmsg = """
396 outmsg = """
394 IPython's 'magic' functions
397 IPython's 'magic' functions
395 ===========================
398 ===========================
396
399
397 The magic function system provides a series of functions which allow you to
400 The magic function system provides a series of functions which allow you to
398 control the behavior of IPython itself, plus a lot of system-type
401 control the behavior of IPython itself, plus a lot of system-type
399 features. All these functions are prefixed with a % character, but parameters
402 features. All these functions are prefixed with a % character, but parameters
400 are given without parentheses or quotes.
403 are given without parentheses or quotes.
401
404
402 NOTE: If you have 'automagic' enabled (via the command line option or with the
405 NOTE: If you have 'automagic' enabled (via the command line option or with the
403 %automagic function), you don't need to type in the % explicitly. By default,
406 %automagic function), you don't need to type in the % explicitly. By default,
404 IPython ships with automagic on, so you should only rarely need the % escape.
407 IPython ships with automagic on, so you should only rarely need the % escape.
405
408
406 Example: typing '%cd mydir' (without the quotes) changes you working directory
409 Example: typing '%cd mydir' (without the quotes) changes you working directory
407 to 'mydir', if it exists.
410 to 'mydir', if it exists.
408
411
409 You can define your own magic functions to extend the system. See the supplied
412 You can define your own magic functions to extend the system. See the supplied
410 ipythonrc and example-magic.py files for details (in your ipython
413 ipythonrc and example-magic.py files for details (in your ipython
411 configuration directory, typically $HOME/.ipython/).
414 configuration directory, typically $HOME/.ipython/).
412
415
413 You can also define your own aliased names for magic functions. In your
416 You can also define your own aliased names for magic functions. In your
414 ipythonrc file, placing a line like:
417 ipythonrc file, placing a line like:
415
418
416 execute __IPYTHON__.magic_pf = __IPYTHON__.magic_profile
419 execute __IPYTHON__.magic_pf = __IPYTHON__.magic_profile
417
420
418 will define %pf as a new name for %profile.
421 will define %pf as a new name for %profile.
419
422
420 You can also call magics in code using the ipmagic() function, which IPython
423 You can also call magics in code using the ipmagic() function, which IPython
421 automatically adds to the builtin namespace. Type 'ipmagic?' for details.
424 automatically adds to the builtin namespace. Type 'ipmagic?' for details.
422
425
423 For a list of the available magic functions, use %lsmagic. For a description
426 For a list of the available magic functions, use %lsmagic. For a description
424 of any of them, type %magic_name?, e.g. '%cd?'.
427 of any of them, type %magic_name?, e.g. '%cd?'.
425
428
426 Currently the magic system has the following functions:\n"""
429 Currently the magic system has the following functions:\n"""
427
430
428 mesc = self.shell.ESC_MAGIC
431 mesc = self.shell.ESC_MAGIC
429 outmsg = ("%s\n%s\n\nSummary of magic functions (from %slsmagic):"
432 outmsg = ("%s\n%s\n\nSummary of magic functions (from %slsmagic):"
430 "\n\n%s%s\n\n%s" % (outmsg,
433 "\n\n%s%s\n\n%s" % (outmsg,
431 magic_docs,mesc,mesc,
434 magic_docs,mesc,mesc,
432 (' '+mesc).join(self.lsmagic()),
435 (' '+mesc).join(self.lsmagic()),
433 Magic.auto_status[self.shell.rc.automagic] ) )
436 Magic.auto_status[self.shell.rc.automagic] ) )
434
437
435 page(outmsg,screen_lines=self.shell.rc.screen_length)
438 page(outmsg,screen_lines=self.shell.rc.screen_length)
436
439
437 def magic_automagic(self, parameter_s = ''):
440 def magic_automagic(self, parameter_s = ''):
438 """Make magic functions callable without having to type the initial %.
441 """Make magic functions callable without having to type the initial %.
439
442
440 Toggles on/off (when off, you must call it as %automagic, of
443 Toggles on/off (when off, you must call it as %automagic, of
441 course). Note that magic functions have lowest priority, so if there's
444 course). Note that magic functions have lowest priority, so if there's
442 a variable whose name collides with that of a magic fn, automagic
445 a variable whose name collides with that of a magic fn, automagic
443 won't work for that function (you get the variable instead). However,
446 won't work for that function (you get the variable instead). However,
444 if you delete the variable (del var), the previously shadowed magic
447 if you delete the variable (del var), the previously shadowed magic
445 function becomes visible to automagic again."""
448 function becomes visible to automagic again."""
446
449
447 rc = self.shell.rc
450 rc = self.shell.rc
448 rc.automagic = not rc.automagic
451 rc.automagic = not rc.automagic
449 print '\n' + Magic.auto_status[rc.automagic]
452 print '\n' + Magic.auto_status[rc.automagic]
450
453
451 def magic_autocall(self, parameter_s = ''):
454 def magic_autocall(self, parameter_s = ''):
452 """Make functions callable without having to type parentheses.
455 """Make functions callable without having to type parentheses.
453
456
454 This toggles the autocall command line option on and off."""
457 This toggles the autocall command line option on and off."""
455
458
456 rc = self.shell.rc
459 rc = self.shell.rc
457 rc.autocall = not rc.autocall
460 rc.autocall = not rc.autocall
458 print "Automatic calling is:",['OFF','ON'][rc.autocall]
461 print "Automatic calling is:",['OFF','ON'][rc.autocall]
459
462
460 def magic_autoindent(self, parameter_s = ''):
463 def magic_autoindent(self, parameter_s = ''):
461 """Toggle autoindent on/off (if available)."""
464 """Toggle autoindent on/off (if available)."""
462
465
463 self.shell.set_autoindent()
466 self.shell.set_autoindent()
464 print "Automatic indentation is:",['OFF','ON'][self.shell.autoindent]
467 print "Automatic indentation is:",['OFF','ON'][self.shell.autoindent]
465
468
466 def magic_system_verbose(self, parameter_s = ''):
469 def magic_system_verbose(self, parameter_s = ''):
467 """Toggle verbose printing of system calls on/off."""
470 """Toggle verbose printing of system calls on/off."""
468
471
469 self.shell.rc_set_toggle('system_verbose')
472 self.shell.rc_set_toggle('system_verbose')
470 print "System verbose printing is:",\
473 print "System verbose printing is:",\
471 ['OFF','ON'][self.shell.rc.system_verbose]
474 ['OFF','ON'][self.shell.rc.system_verbose]
472
475
473 def magic_history(self, parameter_s = ''):
476 def magic_history(self, parameter_s = ''):
474 """Print input history (_i<n> variables), with most recent last.
477 """Print input history (_i<n> variables), with most recent last.
475
478
476 %history [-n] -> print at most 40 inputs (some may be multi-line)\\
479 %history [-n] -> print at most 40 inputs (some may be multi-line)\\
477 %history [-n] n -> print at most n inputs\\
480 %history [-n] n -> print at most n inputs\\
478 %history [-n] n1 n2 -> print inputs between n1 and n2 (n2 not included)\\
481 %history [-n] n1 n2 -> print inputs between n1 and n2 (n2 not included)\\
479
482
480 Each input's number <n> is shown, and is accessible as the
483 Each input's number <n> is shown, and is accessible as the
481 automatically generated variable _i<n>. Multi-line statements are
484 automatically generated variable _i<n>. Multi-line statements are
482 printed starting at a new line for easy copy/paste.
485 printed starting at a new line for easy copy/paste.
483
486
484 If option -n is used, input numbers are not printed. This is useful if
487 If option -n is used, input numbers are not printed. This is useful if
485 you want to get a printout of many lines which can be directly pasted
488 you want to get a printout of many lines which can be directly pasted
486 into a text editor.
489 into a text editor.
487
490
488 This feature is only available if numbered prompts are in use."""
491 This feature is only available if numbered prompts are in use."""
489
492
490 if not self.do_full_cache:
493 if not self.do_full_cache:
491 print 'This feature is only available if numbered prompts are in use.'
494 print 'This feature is only available if numbered prompts are in use.'
492 return
495 return
493 opts,args = self.parse_options(parameter_s,'n',mode='list')
496 opts,args = self.parse_options(parameter_s,'n',mode='list')
494
497
495 default_length = 40
498 default_length = 40
496 if len(args) == 0:
499 if len(args) == 0:
497 final = self.outputcache.prompt_count
500 final = self.outputcache.prompt_count
498 init = max(1,final-default_length)
501 init = max(1,final-default_length)
499 elif len(args) == 1:
502 elif len(args) == 1:
500 final = self.outputcache.prompt_count
503 final = self.outputcache.prompt_count
501 init = max(1,final-int(args[0]))
504 init = max(1,final-int(args[0]))
502 elif len(args) == 2:
505 elif len(args) == 2:
503 init,final = map(int,args)
506 init,final = map(int,args)
504 else:
507 else:
505 warn('%hist takes 0, 1 or 2 arguments separated by spaces.')
508 warn('%hist takes 0, 1 or 2 arguments separated by spaces.')
506 print self.magic_hist.__doc__
509 print self.magic_hist.__doc__
507 return
510 return
508 width = len(str(final))
511 width = len(str(final))
509 line_sep = ['','\n']
512 line_sep = ['','\n']
510 input_hist = self.shell.input_hist
513 input_hist = self.shell.input_hist
511 print_nums = not opts.has_key('n')
514 print_nums = not opts.has_key('n')
512 for in_num in range(init,final):
515 for in_num in range(init,final):
513 inline = input_hist[in_num]
516 inline = input_hist[in_num]
514 multiline = inline.count('\n') > 1
517 multiline = inline.count('\n') > 1
515 if print_nums:
518 if print_nums:
516 print str(in_num).ljust(width)+':'+ line_sep[multiline],
519 print str(in_num).ljust(width)+':'+ line_sep[multiline],
517 if inline.startswith('#'+self.shell.ESC_MAGIC) or \
520 if inline.startswith('#'+self.shell.ESC_MAGIC) or \
518 inline.startswith('#!'):
521 inline.startswith('#!'):
519 print inline[1:],
522 print inline[1:],
520 else:
523 else:
521 print inline,
524 print inline,
522
525
523 def magic_hist(self, parameter_s=''):
526 def magic_hist(self, parameter_s=''):
524 """Alternate name for %history."""
527 """Alternate name for %history."""
525 return self.magic_history(parameter_s)
528 return self.magic_history(parameter_s)
526
529
527 def magic_p(self, parameter_s=''):
530 def magic_p(self, parameter_s=''):
528 """Just a short alias for Python's 'print'."""
531 """Just a short alias for Python's 'print'."""
529 exec 'print ' + parameter_s in self.shell.user_ns
532 exec 'print ' + parameter_s in self.shell.user_ns
530
533
531 def magic_r(self, parameter_s=''):
534 def magic_r(self, parameter_s=''):
532 """Repeat previous input.
535 """Repeat previous input.
533
536
534 If given an argument, repeats the previous command which starts with
537 If given an argument, repeats the previous command which starts with
535 the same string, otherwise it just repeats the previous input.
538 the same string, otherwise it just repeats the previous input.
536
539
537 Shell escaped commands (with ! as first character) are not recognized
540 Shell escaped commands (with ! as first character) are not recognized
538 by this system, only pure python code and magic commands.
541 by this system, only pure python code and magic commands.
539 """
542 """
540
543
541 start = parameter_s.strip()
544 start = parameter_s.strip()
542 esc_magic = self.shell.ESC_MAGIC
545 esc_magic = self.shell.ESC_MAGIC
543 # Identify magic commands even if automagic is on (which means
546 # Identify magic commands even if automagic is on (which means
544 # the in-memory version is different from that typed by the user).
547 # the in-memory version is different from that typed by the user).
545 if self.shell.rc.automagic:
548 if self.shell.rc.automagic:
546 start_magic = esc_magic+start
549 start_magic = esc_magic+start
547 else:
550 else:
548 start_magic = start
551 start_magic = start
549 # Look through the input history in reverse
552 # Look through the input history in reverse
550 for n in range(len(self.shell.input_hist)-2,0,-1):
553 for n in range(len(self.shell.input_hist)-2,0,-1):
551 input = self.shell.input_hist[n]
554 input = self.shell.input_hist[n]
552 # skip plain 'r' lines so we don't recurse to infinity
555 # skip plain 'r' lines so we don't recurse to infinity
553 if input != 'ipmagic("r")\n' and \
556 if input != 'ipmagic("r")\n' and \
554 (input.startswith(start) or input.startswith(start_magic)):
557 (input.startswith(start) or input.startswith(start_magic)):
555 #print 'match',`input` # dbg
558 #print 'match',`input` # dbg
556 if input.startswith(esc_magic):
559 if input.startswith(esc_magic):
557 input = magic2python(input)
560 input = magic2python(input)
558 #print 'modified',`input` # dbg
561 #print 'modified',`input` # dbg
559 print 'Executing:',input,
562 print 'Executing:',input,
560 exec input in self.shell.user_ns
563 exec input in self.shell.user_ns
561 return
564 return
562 print 'No previous input matching `%s` found.' % start
565 print 'No previous input matching `%s` found.' % start
563
566
564 def magic_page(self, parameter_s=''):
567 def magic_page(self, parameter_s=''):
565 """Pretty print the object and display it through a pager.
568 """Pretty print the object and display it through a pager.
566
569
567 If no parameter is given, use _ (last output)."""
570 If no parameter is given, use _ (last output)."""
568 # After a function contributed by Olivier Aubert, slightly modified.
571 # After a function contributed by Olivier Aubert, slightly modified.
569
572
570 oname = parameter_s and parameter_s or '_'
573 oname = parameter_s and parameter_s or '_'
571 info = self._ofind(oname)
574 info = self._ofind(oname)
572 if info['found']:
575 if info['found']:
573 page(pformat(info['obj']))
576 page(pformat(info['obj']))
574 else:
577 else:
575 print 'Object `%s` not found' % oname
578 print 'Object `%s` not found' % oname
576
579
577 def magic_profile(self, parameter_s=''):
580 def magic_profile(self, parameter_s=''):
578 """Print your currently active IPyhton profile."""
581 """Print your currently active IPyhton profile."""
579 if self.shell.rc.profile:
582 if self.shell.rc.profile:
580 printpl('Current IPython profile: $self.shell.rc.profile.')
583 printpl('Current IPython profile: $self.shell.rc.profile.')
581 else:
584 else:
582 print 'No profile active.'
585 print 'No profile active.'
583
586
584 def _inspect(self,meth,oname,**kw):
587 def _inspect(self,meth,oname,**kw):
585 """Generic interface to the inspector system.
588 """Generic interface to the inspector system.
586
589
587 This function is meant to be called by pdef, pdoc & friends."""
590 This function is meant to be called by pdef, pdoc & friends."""
588
591
589 oname = oname.strip()
592 oname = oname.strip()
590 info = Struct(self._ofind(oname))
593 info = Struct(self._ofind(oname))
591 if info.found:
594 if info.found:
592 pmethod = getattr(self.shell.inspector,meth)
595 pmethod = getattr(self.shell.inspector,meth)
593 formatter = info.ismagic and self.format_screen or None
596 formatter = info.ismagic and self.format_screen or None
594 if meth == 'pdoc':
597 if meth == 'pdoc':
595 pmethod(info.obj,oname,formatter)
598 pmethod(info.obj,oname,formatter)
596 elif meth == 'pinfo':
599 elif meth == 'pinfo':
597 pmethod(info.obj,oname,formatter,info,**kw)
600 pmethod(info.obj,oname,formatter,info,**kw)
598 else:
601 else:
599 pmethod(info.obj,oname)
602 pmethod(info.obj,oname)
600 else:
603 else:
601 print 'Object `%s` not found.' % oname
604 print 'Object `%s` not found.' % oname
602 return 'not found' # so callers can take other action
605 return 'not found' # so callers can take other action
603
606
604 def magic_pdef(self, parameter_s=''):
607 def magic_pdef(self, parameter_s=''):
605 """Print the definition header for any callable object.
608 """Print the definition header for any callable object.
606
609
607 If the object is a class, print the constructor information."""
610 If the object is a class, print the constructor information."""
608 self._inspect('pdef',parameter_s)
611 self._inspect('pdef',parameter_s)
609
612
610 def magic_pdoc(self, parameter_s=''):
613 def magic_pdoc(self, parameter_s=''):
611 """Print the docstring for an object.
614 """Print the docstring for an object.
612
615
613 If the given object is a class, it will print both the class and the
616 If the given object is a class, it will print both the class and the
614 constructor docstrings."""
617 constructor docstrings."""
615 self._inspect('pdoc',parameter_s)
618 self._inspect('pdoc',parameter_s)
616
619
617 def magic_psource(self, parameter_s=''):
620 def magic_psource(self, parameter_s=''):
618 """Print (or run through pager) the source code for an object."""
621 """Print (or run through pager) the source code for an object."""
619 self._inspect('psource',parameter_s)
622 self._inspect('psource',parameter_s)
620
623
621 def magic_pfile(self, parameter_s=''):
624 def magic_pfile(self, parameter_s=''):
622 """Print (or run through pager) the file where an object is defined.
625 """Print (or run through pager) the file where an object is defined.
623
626
624 The file opens at the line where the object definition begins. IPython
627 The file opens at the line where the object definition begins. IPython
625 will honor the environment variable PAGER if set, and otherwise will
628 will honor the environment variable PAGER if set, and otherwise will
626 do its best to print the file in a convenient form.
629 do its best to print the file in a convenient form.
627
630
628 If the given argument is not an object currently defined, IPython will
631 If the given argument is not an object currently defined, IPython will
629 try to interpret it as a filename (automatically adding a .py extension
632 try to interpret it as a filename (automatically adding a .py extension
630 if needed). You can thus use %pfile as a syntax highlighting code
633 if needed). You can thus use %pfile as a syntax highlighting code
631 viewer."""
634 viewer."""
632
635
633 # first interpret argument as an object name
636 # first interpret argument as an object name
634 out = self._inspect('pfile',parameter_s)
637 out = self._inspect('pfile',parameter_s)
635 # if not, try the input as a filename
638 # if not, try the input as a filename
636 if out == 'not found':
639 if out == 'not found':
637 try:
640 try:
638 filename = get_py_filename(parameter_s)
641 filename = get_py_filename(parameter_s)
639 except IOError,msg:
642 except IOError,msg:
640 print msg
643 print msg
641 return
644 return
642 page(self.shell.inspector.format(file(filename).read()))
645 page(self.shell.inspector.format(file(filename).read()))
643
646
644 def magic_pinfo(self, parameter_s=''):
647 def magic_pinfo(self, parameter_s=''):
645 """Provide detailed information about an object.
648 """Provide detailed information about an object.
646
649
647 '%pinfo object' is just a synonym for object? or ?object."""
650 '%pinfo object' is just a synonym for object? or ?object."""
648
651
649 #print 'pinfo par: <%s>' % parameter_s # dbg
652 #print 'pinfo par: <%s>' % parameter_s # dbg
650
653
651 # detail_level: 0 -> obj? , 1 -> obj??
654 # detail_level: 0 -> obj? , 1 -> obj??
652 detail_level = 0
655 detail_level = 0
653 # We need to detect if we got called as 'pinfo pinfo foo', which can
656 # We need to detect if we got called as 'pinfo pinfo foo', which can
654 # happen if the user types 'pinfo foo?' at the cmd line.
657 # happen if the user types 'pinfo foo?' at the cmd line.
655 pinfo,qmark1,oname,qmark2 = \
658 pinfo,qmark1,oname,qmark2 = \
656 re.match('(pinfo )?(\?*)(.*?)(\??$)',parameter_s).groups()
659 re.match('(pinfo )?(\?*)(.*?)(\??$)',parameter_s).groups()
657 if pinfo or qmark1 or qmark2:
660 if pinfo or qmark1 or qmark2:
658 detail_level = 1
661 detail_level = 1
659 if "*" in oname:
662 if "*" in oname:
660 self.magic_psearch(oname)
663 self.magic_psearch(oname)
661 else:
664 else:
662 self._inspect('pinfo',oname,detail_level=detail_level)
665 self._inspect('pinfo',oname,detail_level=detail_level)
663
666
664 def magic_psearch(self, parameter_s=''):
667 def magic_psearch(self, parameter_s=''):
665 """Search for object in namespaces by wildcard.
668 """Search for object in namespaces by wildcard.
666
669
667 %psearch [options] PATTERN [OBJECT TYPE]
670 %psearch [options] PATTERN [OBJECT TYPE]
668
671
669 Note: ? can be used as a synonym for %psearch, at the beginning or at
672 Note: ? can be used as a synonym for %psearch, at the beginning or at
670 the end: both a*? and ?a* are equivalent to '%psearch a*'. Still, the
673 the end: both a*? and ?a* are equivalent to '%psearch a*'. Still, the
671 rest of the command line must be unchanged (options come first), so
674 rest of the command line must be unchanged (options come first), so
672 for example the following forms are equivalent
675 for example the following forms are equivalent
673
676
674 %psearch -i a* function
677 %psearch -i a* function
675 -i a* function?
678 -i a* function?
676 ?-i a* function
679 ?-i a* function
677
680
678 Arguments:
681 Arguments:
679
682
680 PATTERN
683 PATTERN
681
684
682 where PATTERN is a string containing * as a wildcard similar to its
685 where PATTERN is a string containing * as a wildcard similar to its
683 use in a shell. The pattern is matched in all namespaces on the
686 use in a shell. The pattern is matched in all namespaces on the
684 search path. By default objects starting with a single _ are not
687 search path. By default objects starting with a single _ are not
685 matched, many IPython generated objects have a single
688 matched, many IPython generated objects have a single
686 underscore. The default is case insensitive matching. Matching is
689 underscore. The default is case insensitive matching. Matching is
687 also done on the attributes of objects and not only on the objects
690 also done on the attributes of objects and not only on the objects
688 in a module.
691 in a module.
689
692
690 [OBJECT TYPE]
693 [OBJECT TYPE]
691
694
692 Is the name of a python type from the types module. The name is
695 Is the name of a python type from the types module. The name is
693 given in lowercase without the ending type, ex. StringType is
696 given in lowercase without the ending type, ex. StringType is
694 written string. By adding a type here only objects matching the
697 written string. By adding a type here only objects matching the
695 given type are matched. Using all here makes the pattern match all
698 given type are matched. Using all here makes the pattern match all
696 types (this is the default).
699 types (this is the default).
697
700
698 Options:
701 Options:
699
702
700 -a: makes the pattern match even objects whose names start with a
703 -a: makes the pattern match even objects whose names start with a
701 single underscore. These names are normally ommitted from the
704 single underscore. These names are normally ommitted from the
702 search.
705 search.
703
706
704 -i/-c: make the pattern case insensitive/sensitive. If neither of
707 -i/-c: make the pattern case insensitive/sensitive. If neither of
705 these options is given, the default is read from your ipythonrc
708 these options is given, the default is read from your ipythonrc
706 file. The option name which sets this value is
709 file. The option name which sets this value is
707 'wildcards_case_sensitive'. If this option is not specified in your
710 'wildcards_case_sensitive'. If this option is not specified in your
708 ipythonrc file, IPython's internal default is to do a case sensitive
711 ipythonrc file, IPython's internal default is to do a case sensitive
709 search.
712 search.
710
713
711 -e/-s NAMESPACE: exclude/search a given namespace. The pattern you
714 -e/-s NAMESPACE: exclude/search a given namespace. The pattern you
712 specifiy can be searched in any of the following namespaces:
715 specifiy can be searched in any of the following namespaces:
713 'builtin', 'user', 'user_global','internal', 'alias', where
716 'builtin', 'user', 'user_global','internal', 'alias', where
714 'builtin' and 'user' are the search defaults. Note that you should
717 'builtin' and 'user' are the search defaults. Note that you should
715 not use quotes when specifying namespaces.
718 not use quotes when specifying namespaces.
716
719
717 'Builtin' contains the python module builtin, 'user' contains all
720 'Builtin' contains the python module builtin, 'user' contains all
718 user data, 'alias' only contain the shell aliases and no python
721 user data, 'alias' only contain the shell aliases and no python
719 objects, 'internal' contains objects used by IPython. The
722 objects, 'internal' contains objects used by IPython. The
720 'user_global' namespace is only used by embedded IPython instances,
723 'user_global' namespace is only used by embedded IPython instances,
721 and it contains module-level globals. You can add namespaces to the
724 and it contains module-level globals. You can add namespaces to the
722 search with -s or exclude them with -e (these options can be given
725 search with -s or exclude them with -e (these options can be given
723 more than once).
726 more than once).
724
727
725 Examples:
728 Examples:
726
729
727 %psearch a* -> objects beginning with an a
730 %psearch a* -> objects beginning with an a
728 %psearch -e builtin a* -> objects NOT in the builtin space starting in a
731 %psearch -e builtin a* -> objects NOT in the builtin space starting in a
729 %psearch a* function -> all functions beginning with an a
732 %psearch a* function -> all functions beginning with an a
730 %psearch re.e* -> objects beginning with an e in module re
733 %psearch re.e* -> objects beginning with an e in module re
731 %psearch r*.e* -> objects that start with e in modules starting in r
734 %psearch r*.e* -> objects that start with e in modules starting in r
732 %psearch r*.* string -> all strings in modules beginning with r
735 %psearch r*.* string -> all strings in modules beginning with r
733
736
734 Case sensitve search:
737 Case sensitve search:
735
738
736 %psearch -c a* list all object beginning with lower case a
739 %psearch -c a* list all object beginning with lower case a
737
740
738 Show objects beginning with a single _:
741 Show objects beginning with a single _:
739
742
740 %psearch -a _* list objects beginning with a single underscore"""
743 %psearch -a _* list objects beginning with a single underscore"""
741
744
742 # default namespaces to be searched
745 # default namespaces to be searched
743 def_search = ['user','builtin']
746 def_search = ['user','builtin']
744
747
745 # Process options/args
748 # Process options/args
746 opts,args = self.parse_options(parameter_s,'cias:e:',list_all=True)
749 opts,args = self.parse_options(parameter_s,'cias:e:',list_all=True)
747 opt = opts.get
750 opt = opts.get
748 shell = self.shell
751 shell = self.shell
749 psearch = shell.inspector.psearch
752 psearch = shell.inspector.psearch
750
753
751 # select case options
754 # select case options
752 if opts.has_key('i'):
755 if opts.has_key('i'):
753 ignore_case = True
756 ignore_case = True
754 elif opts.has_key('c'):
757 elif opts.has_key('c'):
755 ignore_case = False
758 ignore_case = False
756 else:
759 else:
757 ignore_case = not shell.rc.wildcards_case_sensitive
760 ignore_case = not shell.rc.wildcards_case_sensitive
758
761
759 # Build list of namespaces to search from user options
762 # Build list of namespaces to search from user options
760 def_search.extend(opt('s',[]))
763 def_search.extend(opt('s',[]))
761 ns_exclude = ns_exclude=opt('e',[])
764 ns_exclude = ns_exclude=opt('e',[])
762 ns_search = [nm for nm in def_search if nm not in ns_exclude]
765 ns_search = [nm for nm in def_search if nm not in ns_exclude]
763
766
764 # Call the actual search
767 # Call the actual search
765 try:
768 try:
766 psearch(args,shell.ns_table,ns_search,
769 psearch(args,shell.ns_table,ns_search,
767 show_all=opt('a'),ignore_case=ignore_case)
770 show_all=opt('a'),ignore_case=ignore_case)
768 except:
771 except:
769 shell.showtraceback()
772 shell.showtraceback()
770
773
771 def magic_who_ls(self, parameter_s=''):
774 def magic_who_ls(self, parameter_s=''):
772 """Return a sorted list of all interactive variables.
775 """Return a sorted list of all interactive variables.
773
776
774 If arguments are given, only variables of types matching these
777 If arguments are given, only variables of types matching these
775 arguments are returned."""
778 arguments are returned."""
776
779
777 user_ns = self.shell.user_ns
780 user_ns = self.shell.user_ns
778 out = []
781 out = []
779 typelist = parameter_s.split()
782 typelist = parameter_s.split()
780 for i in self.shell.user_ns.keys():
783 for i in self.shell.user_ns.keys():
781 if not (i.startswith('_') or i.startswith('_i')) \
784 if not (i.startswith('_') or i.startswith('_i')) \
782 and not (self.internal_ns.has_key(i) or
785 and not (self.internal_ns.has_key(i) or
783 self.user_config_ns.has_key(i)):
786 self.user_config_ns.has_key(i)):
784 if typelist:
787 if typelist:
785 if type(user_ns[i]).__name__ in typelist:
788 if type(user_ns[i]).__name__ in typelist:
786 out.append(i)
789 out.append(i)
787 else:
790 else:
788 out.append(i)
791 out.append(i)
789 out.sort()
792 out.sort()
790 return out
793 return out
791
794
792 def magic_who(self, parameter_s=''):
795 def magic_who(self, parameter_s=''):
793 """Print all interactive variables, with some minimal formatting.
796 """Print all interactive variables, with some minimal formatting.
794
797
795 If any arguments are given, only variables whose type matches one of
798 If any arguments are given, only variables whose type matches one of
796 these are printed. For example:
799 these are printed. For example:
797
800
798 %who function str
801 %who function str
799
802
800 will only list functions and strings, excluding all other types of
803 will only list functions and strings, excluding all other types of
801 variables. To find the proper type names, simply use type(var) at a
804 variables. To find the proper type names, simply use type(var) at a
802 command line to see how python prints type names. For example:
805 command line to see how python prints type names. For example:
803
806
804 In [1]: type('hello')\\
807 In [1]: type('hello')\\
805 Out[1]: <type 'str'>
808 Out[1]: <type 'str'>
806
809
807 indicates that the type name for strings is 'str'.
810 indicates that the type name for strings is 'str'.
808
811
809 %who always excludes executed names loaded through your configuration
812 %who always excludes executed names loaded through your configuration
810 file and things which are internal to IPython.
813 file and things which are internal to IPython.
811
814
812 This is deliberate, as typically you may load many modules and the
815 This is deliberate, as typically you may load many modules and the
813 purpose of %who is to show you only what you've manually defined."""
816 purpose of %who is to show you only what you've manually defined."""
814
817
815 varlist = self.magic_who_ls(parameter_s)
818 varlist = self.magic_who_ls(parameter_s)
816 if not varlist:
819 if not varlist:
817 print 'Interactive namespace is empty.'
820 print 'Interactive namespace is empty.'
818 return
821 return
819
822
820 # if we have variables, move on...
823 # if we have variables, move on...
821
824
822 # stupid flushing problem: when prompts have no separators, stdout is
825 # stupid flushing problem: when prompts have no separators, stdout is
823 # getting lost. I'm starting to think this is a python bug. I'm having
826 # getting lost. I'm starting to think this is a python bug. I'm having
824 # to force a flush with a print because even a sys.stdout.flush
827 # to force a flush with a print because even a sys.stdout.flush
825 # doesn't seem to do anything!
828 # doesn't seem to do anything!
826
829
827 count = 0
830 count = 0
828 for i in varlist:
831 for i in varlist:
829 print i+'\t',
832 print i+'\t',
830 count += 1
833 count += 1
831 if count > 8:
834 if count > 8:
832 count = 0
835 count = 0
833 print
836 print
834 sys.stdout.flush() # FIXME. Why the hell isn't this flushing???
837 sys.stdout.flush() # FIXME. Why the hell isn't this flushing???
835
838
836 print # well, this does force a flush at the expense of an extra \n
839 print # well, this does force a flush at the expense of an extra \n
837
840
838 def magic_whos(self, parameter_s=''):
841 def magic_whos(self, parameter_s=''):
839 """Like %who, but gives some extra information about each variable.
842 """Like %who, but gives some extra information about each variable.
840
843
841 The same type filtering of %who can be applied here.
844 The same type filtering of %who can be applied here.
842
845
843 For all variables, the type is printed. Additionally it prints:
846 For all variables, the type is printed. Additionally it prints:
844
847
845 - For {},[],(): their length.
848 - For {},[],(): their length.
846
849
847 - For Numeric arrays, a summary with shape, number of elements,
850 - For Numeric arrays, a summary with shape, number of elements,
848 typecode and size in memory.
851 typecode and size in memory.
849
852
850 - Everything else: a string representation, snipping their middle if
853 - Everything else: a string representation, snipping their middle if
851 too long."""
854 too long."""
852
855
853 varnames = self.magic_who_ls(parameter_s)
856 varnames = self.magic_who_ls(parameter_s)
854 if not varnames:
857 if not varnames:
855 print 'Interactive namespace is empty.'
858 print 'Interactive namespace is empty.'
856 return
859 return
857
860
858 # if we have variables, move on...
861 # if we have variables, move on...
859
862
860 # for these types, show len() instead of data:
863 # for these types, show len() instead of data:
861 seq_types = [types.DictType,types.ListType,types.TupleType]
864 seq_types = [types.DictType,types.ListType,types.TupleType]
862
865
863 # for Numeric arrays, display summary info
866 # for Numeric arrays, display summary info
864 try:
867 try:
865 import Numeric
868 import Numeric
866 except ImportError:
869 except ImportError:
867 array_type = None
870 array_type = None
868 else:
871 else:
869 array_type = Numeric.ArrayType.__name__
872 array_type = Numeric.ArrayType.__name__
870
873
871 # Find all variable names and types so we can figure out column sizes
874 # Find all variable names and types so we can figure out column sizes
872 get_vars = lambda i: self.shell.user_ns[i]
875 get_vars = lambda i: self.shell.user_ns[i]
873 type_name = lambda v: type(v).__name__
876 type_name = lambda v: type(v).__name__
874 varlist = map(get_vars,varnames)
877 varlist = map(get_vars,varnames)
875 typelist = map(type_name,varlist)
878 typelist = map(type_name,varlist)
876 # column labels and # of spaces as separator
879 # column labels and # of spaces as separator
877 varlabel = 'Variable'
880 varlabel = 'Variable'
878 typelabel = 'Type'
881 typelabel = 'Type'
879 datalabel = 'Data/Info'
882 datalabel = 'Data/Info'
880 colsep = 3
883 colsep = 3
881 # variable format strings
884 # variable format strings
882 vformat = "$vname.ljust(varwidth)$vtype.ljust(typewidth)"
885 vformat = "$vname.ljust(varwidth)$vtype.ljust(typewidth)"
883 vfmt_short = '$vstr[:25]<...>$vstr[-25:]'
886 vfmt_short = '$vstr[:25]<...>$vstr[-25:]'
884 aformat = "%s: %s elems, type `%s`, %s bytes"
887 aformat = "%s: %s elems, type `%s`, %s bytes"
885 # find the size of the columns to format the output nicely
888 # find the size of the columns to format the output nicely
886 varwidth = max(max(map(len,varnames)), len(varlabel)) + colsep
889 varwidth = max(max(map(len,varnames)), len(varlabel)) + colsep
887 typewidth = max(max(map(len,typelist)), len(typelabel)) + colsep
890 typewidth = max(max(map(len,typelist)), len(typelabel)) + colsep
888 # table header
891 # table header
889 print varlabel.ljust(varwidth) + typelabel.ljust(typewidth) + \
892 print varlabel.ljust(varwidth) + typelabel.ljust(typewidth) + \
890 ' '+datalabel+'\n' + '-'*(varwidth+typewidth+len(datalabel)+1)
893 ' '+datalabel+'\n' + '-'*(varwidth+typewidth+len(datalabel)+1)
891 # and the table itself
894 # and the table itself
892 kb = 1024
895 kb = 1024
893 Mb = 1048576 # kb**2
896 Mb = 1048576 # kb**2
894 for vname,var,vtype in zip(varnames,varlist,typelist):
897 for vname,var,vtype in zip(varnames,varlist,typelist):
895 print itpl(vformat),
898 print itpl(vformat),
896 if vtype in seq_types:
899 if vtype in seq_types:
897 print len(var)
900 print len(var)
898 elif vtype==array_type:
901 elif vtype==array_type:
899 vshape = str(var.shape).replace(',','').replace(' ','x')[1:-1]
902 vshape = str(var.shape).replace(',','').replace(' ','x')[1:-1]
900 vsize = Numeric.size(var)
903 vsize = Numeric.size(var)
901 vbytes = vsize*var.itemsize()
904 vbytes = vsize*var.itemsize()
902 if vbytes < 100000:
905 if vbytes < 100000:
903 print aformat % (vshape,vsize,var.typecode(),vbytes)
906 print aformat % (vshape,vsize,var.typecode(),vbytes)
904 else:
907 else:
905 print aformat % (vshape,vsize,var.typecode(),vbytes),
908 print aformat % (vshape,vsize,var.typecode(),vbytes),
906 if vbytes < Mb:
909 if vbytes < Mb:
907 print '(%s kb)' % (vbytes/kb,)
910 print '(%s kb)' % (vbytes/kb,)
908 else:
911 else:
909 print '(%s Mb)' % (vbytes/Mb,)
912 print '(%s Mb)' % (vbytes/Mb,)
910 else:
913 else:
911 vstr = str(var)
914 vstr = str(var)
912 if len(vstr) < 50:
915 if len(vstr) < 50:
913 print vstr
916 print vstr
914 else:
917 else:
915 printpl(vfmt_short)
918 printpl(vfmt_short)
916
919
917 def magic_reset(self, parameter_s=''):
920 def magic_reset(self, parameter_s=''):
918 """Resets the namespace by removing all names defined by the user.
921 """Resets the namespace by removing all names defined by the user.
919
922
920 Input/Output history are left around in case you need them."""
923 Input/Output history are left around in case you need them."""
921
924
922 ans = raw_input(
925 ans = raw_input(
923 "Once deleted, variables cannot be recovered. Proceed (y/n)? ")
926 "Once deleted, variables cannot be recovered. Proceed (y/n)? ")
924 if not ans.lower() == 'y':
927 if not ans.lower() == 'y':
925 print 'Nothing done.'
928 print 'Nothing done.'
926 return
929 return
927 user_ns = self.shell.user_ns
930 user_ns = self.shell.user_ns
928 for i in self.magic_who_ls():
931 for i in self.magic_who_ls():
929 del(user_ns[i])
932 del(user_ns[i])
930
933
931 def magic_config(self,parameter_s=''):
934 def magic_config(self,parameter_s=''):
932 """Show IPython's internal configuration."""
935 """Show IPython's internal configuration."""
933
936
934 page('Current configuration structure:\n'+
937 page('Current configuration structure:\n'+
935 pformat(self.shell.rc.dict()))
938 pformat(self.shell.rc.dict()))
936
939
937 def magic_logstart(self,parameter_s=''):
940 def magic_logstart(self,parameter_s=''):
938 """Start logging anywhere in a session.
941 """Start logging anywhere in a session.
939
942
940 %logstart [log_name [log_mode]]
943 %logstart [log_name [log_mode]]
941
944
942 If no name is given, it defaults to a file named 'ipython.log' in your
945 If no name is given, it defaults to a file named 'ipython.log' in your
943 current directory, in 'rotate' mode (see below).
946 current directory, in 'rotate' mode (see below).
944
947
945 '%logstart name' saves to file 'name' in 'backup' mode. It saves your
948 '%logstart name' saves to file 'name' in 'backup' mode. It saves your
946 history up to that point and then continues logging.
949 history up to that point and then continues logging.
947
950
948 %logstart takes a second optional parameter: logging mode. This can be one
951 %logstart takes a second optional parameter: logging mode. This can be one
949 of (note that the modes are given unquoted):\\
952 of (note that the modes are given unquoted):\\
950 over: overwrite existing log.\\
953 over: overwrite existing log.\\
951 backup: rename (if exists) to name~ and start name.\\
954 backup: rename (if exists) to name~ and start name.\\
952 append: well, that says it.\\
955 append: well, that says it.\\
953 rotate: create rotating logs name.1~, name.2~, etc.
956 rotate: create rotating logs name.1~, name.2~, etc.
954 """
957 """
955
958
956 #FIXME. This function should all be moved to the Logger class.
959 #FIXME. This function should all be moved to the Logger class.
957
960
958 valid_modes = qw('over backup append rotate')
961 valid_modes = qw('over backup append rotate')
959 if self.LOG:
962 if self.LOG:
960 print 'Logging is already in place. Logfile:',self.LOG
963 print 'Logging is already in place. Logfile:',self.LOG
961 return
964 return
962
965
963 par = parameter_s.strip()
966 par = parameter_s.strip()
964 if not par:
967 if not par:
965 logname = self.LOGDEF
968 logname = self.LOGDEF
966 logmode = 'rotate' # use rotate for the auto-generated logs
969 logmode = 'rotate' # use rotate for the auto-generated logs
967 else:
970 else:
968 try:
971 try:
969 logname,logmode = par.split()
972 logname,logmode = par.split()
970 except:
973 except:
971 try:
974 try:
972 logname = par
975 logname = par
973 logmode = 'backup'
976 logmode = 'backup'
974 except:
977 except:
975 warn('Usage: %log [log_name [log_mode]]')
978 warn('Usage: %log [log_name [log_mode]]')
976 return
979 return
977 if not logmode in valid_modes:
980 if not logmode in valid_modes:
978 warn('Logging NOT activated.\n'
981 warn('Logging NOT activated.\n'
979 'Usage: %log [log_name [log_mode]]\n'
982 'Usage: %log [log_name [log_mode]]\n'
980 'Valid modes: '+str(valid_modes))
983 'Valid modes: '+str(valid_modes))
981 return
984 return
982
985
983 # If we made it this far, I think we're ok:
986 # If we made it this far, I think we're ok:
984 print 'Activating auto-logging.'
987 print 'Activating auto-logging.'
985 print 'Current session state plus future input saved to:',logname
988 print 'Current session state plus future input saved to:',logname
986 print 'Logging mode: ',logmode
989 print 'Logging mode: ',logmode
987 # put logname into rc struct as if it had been called on the command line,
990 # put logname into rc struct as if it had been called on the command line,
988 # so it ends up saved in the log header
991 # so it ends up saved in the log header
989 # Save it in case we need to restore it...
992 # Save it in case we need to restore it...
990 old_logfile = self.shell.rc.opts.get('logfile','')
993 old_logfile = self.shell.rc.opts.get('logfile','')
991 logname = os.path.expanduser(logname)
994 logname = os.path.expanduser(logname)
992 self.shell.rc.opts.logfile = logname
995 self.shell.rc.opts.logfile = logname
993 self.LOGMODE = logmode # FIXME: this should be set through a function.
996 self.LOGMODE = logmode # FIXME: this should be set through a function.
994 try:
997 try:
995 header = str(self.LOGHEAD)
998 header = str(self.LOGHEAD)
996 self.create_log(header,logname)
999 self.create_log(header,logname)
997 self.logstart(header,logname)
1000 self.logstart(header,logname)
998 except:
1001 except:
999 self.LOG = '' # we are NOT logging, something went wrong
1002 self.LOG = '' # we are NOT logging, something went wrong
1000 self.shell.rc.opts.logfile = old_logfile
1003 self.shell.rc.opts.logfile = old_logfile
1001 warn("Couldn't start log: "+str(sys.exc_info()[1]))
1004 warn("Couldn't start log: "+str(sys.exc_info()[1]))
1002 else: # log input history up to this point
1005 else: # log input history up to this point
1003 self.logfile.write(self.shell.user_ns['_ih'][1:])
1006 self.logfile.write(self.shell.user_ns['_ih'][1:])
1004 self.logfile.flush()
1007 self.logfile.flush()
1005
1008
1006 def magic_logoff(self,parameter_s=''):
1009 def magic_logoff(self,parameter_s=''):
1007 """Temporarily stop logging.
1010 """Temporarily stop logging.
1008
1011
1009 You must have previously started logging."""
1012 You must have previously started logging."""
1010 self.switch_log(0)
1013 self.switch_log(0)
1011
1014
1012 def magic_logon(self,parameter_s=''):
1015 def magic_logon(self,parameter_s=''):
1013 """Restart logging.
1016 """Restart logging.
1014
1017
1015 This function is for restarting logging which you've temporarily
1018 This function is for restarting logging which you've temporarily
1016 stopped with %logoff. For starting logging for the first time, you
1019 stopped with %logoff. For starting logging for the first time, you
1017 must use the %logstart function, which allows you to specify an
1020 must use the %logstart function, which allows you to specify an
1018 optional log filename."""
1021 optional log filename."""
1019
1022
1020 self.switch_log(1)
1023 self.switch_log(1)
1021
1024
1022 def magic_logstate(self,parameter_s=''):
1025 def magic_logstate(self,parameter_s=''):
1023 """Print the status of the logging system."""
1026 """Print the status of the logging system."""
1024
1027
1025 self.logstate()
1028 self.logstate()
1026
1029
1027 def magic_pdb(self, parameter_s=''):
1030 def magic_pdb(self, parameter_s=''):
1028 """Control the calling of the pdb interactive debugger.
1031 """Control the calling of the pdb interactive debugger.
1029
1032
1030 Call as '%pdb on', '%pdb 1', '%pdb off' or '%pdb 0'. If called without
1033 Call as '%pdb on', '%pdb 1', '%pdb off' or '%pdb 0'. If called without
1031 argument it works as a toggle.
1034 argument it works as a toggle.
1032
1035
1033 When an exception is triggered, IPython can optionally call the
1036 When an exception is triggered, IPython can optionally call the
1034 interactive pdb debugger after the traceback printout. %pdb toggles
1037 interactive pdb debugger after the traceback printout. %pdb toggles
1035 this feature on and off."""
1038 this feature on and off."""
1036
1039
1037 par = parameter_s.strip().lower()
1040 par = parameter_s.strip().lower()
1038
1041
1039 if par:
1042 if par:
1040 try:
1043 try:
1041 pdb = {'off':0,'0':0,'on':1,'1':1}[par]
1044 pdb = {'off':0,'0':0,'on':1,'1':1}[par]
1042 except KeyError:
1045 except KeyError:
1043 print 'Incorrect argument. Use on/1, off/0 or nothing for a toggle.'
1046 print 'Incorrect argument. Use on/1, off/0 or nothing for a toggle.'
1044 return
1047 return
1045 else:
1048 else:
1046 self.shell.InteractiveTB.call_pdb = pdb
1049 self.shell.InteractiveTB.call_pdb = pdb
1047 else:
1050 else:
1048 self.shell.InteractiveTB.call_pdb = 1 - self.shell.InteractiveTB.call_pdb
1051 self.shell.InteractiveTB.call_pdb = 1 - self.shell.InteractiveTB.call_pdb
1049 print 'Automatic pdb calling has been turned',\
1052 print 'Automatic pdb calling has been turned',\
1050 on_off(self.shell.InteractiveTB.call_pdb)
1053 on_off(self.shell.InteractiveTB.call_pdb)
1051
1054
1052
1055
1053 def magic_prun(self, parameter_s ='',user_mode=1,
1056 def magic_prun(self, parameter_s ='',user_mode=1,
1054 opts=None,arg_lst=None,prog_ns=None):
1057 opts=None,arg_lst=None,prog_ns=None):
1055
1058
1056 """Run a statement through the python code profiler.
1059 """Run a statement through the python code profiler.
1057
1060
1058 Usage:\\
1061 Usage:\\
1059 %prun [options] statement
1062 %prun [options] statement
1060
1063
1061 The given statement (which doesn't require quote marks) is run via the
1064 The given statement (which doesn't require quote marks) is run via the
1062 python profiler in a manner similar to the profile.run() function.
1065 python profiler in a manner similar to the profile.run() function.
1063 Namespaces are internally managed to work correctly; profile.run
1066 Namespaces are internally managed to work correctly; profile.run
1064 cannot be used in IPython because it makes certain assumptions about
1067 cannot be used in IPython because it makes certain assumptions about
1065 namespaces which do not hold under IPython.
1068 namespaces which do not hold under IPython.
1066
1069
1067 Options:
1070 Options:
1068
1071
1069 -l <limit>: you can place restrictions on what or how much of the
1072 -l <limit>: you can place restrictions on what or how much of the
1070 profile gets printed. The limit value can be:
1073 profile gets printed. The limit value can be:
1071
1074
1072 * A string: only information for function names containing this string
1075 * A string: only information for function names containing this string
1073 is printed.
1076 is printed.
1074
1077
1075 * An integer: only these many lines are printed.
1078 * An integer: only these many lines are printed.
1076
1079
1077 * A float (between 0 and 1): this fraction of the report is printed
1080 * A float (between 0 and 1): this fraction of the report is printed
1078 (for example, use a limit of 0.4 to see the topmost 40% only).
1081 (for example, use a limit of 0.4 to see the topmost 40% only).
1079
1082
1080 You can combine several limits with repeated use of the option. For
1083 You can combine several limits with repeated use of the option. For
1081 example, '-l __init__ -l 5' will print only the topmost 5 lines of
1084 example, '-l __init__ -l 5' will print only the topmost 5 lines of
1082 information about class constructors.
1085 information about class constructors.
1083
1086
1084 -r: return the pstats.Stats object generated by the profiling. This
1087 -r: return the pstats.Stats object generated by the profiling. This
1085 object has all the information about the profile in it, and you can
1088 object has all the information about the profile in it, and you can
1086 later use it for further analysis or in other functions.
1089 later use it for further analysis or in other functions.
1087
1090
1088 Since magic functions have a particular form of calling which prevents
1091 Since magic functions have a particular form of calling which prevents
1089 you from writing something like:\\
1092 you from writing something like:\\
1090 In [1]: p = %prun -r print 4 # invalid!\\
1093 In [1]: p = %prun -r print 4 # invalid!\\
1091 you must instead use IPython's automatic variables to assign this:\\
1094 you must instead use IPython's automatic variables to assign this:\\
1092 In [1]: %prun -r print 4 \\
1095 In [1]: %prun -r print 4 \\
1093 Out[1]: <pstats.Stats instance at 0x8222cec>\\
1096 Out[1]: <pstats.Stats instance at 0x8222cec>\\
1094 In [2]: stats = _
1097 In [2]: stats = _
1095
1098
1096 If you really need to assign this value via an explicit function call,
1099 If you really need to assign this value via an explicit function call,
1097 you can always tap directly into the true name of the magic function
1100 you can always tap directly into the true name of the magic function
1098 by using the ipmagic function (which IPython automatically adds to the
1101 by using the ipmagic function (which IPython automatically adds to the
1099 builtins):\\
1102 builtins):\\
1100 In [3]: stats = ipmagic('prun','-r print 4')
1103 In [3]: stats = ipmagic('prun','-r print 4')
1101
1104
1102 You can type ipmagic? for more details on ipmagic.
1105 You can type ipmagic? for more details on ipmagic.
1103
1106
1104 -s <key>: sort profile by given key. You can provide more than one key
1107 -s <key>: sort profile by given key. You can provide more than one key
1105 by using the option several times: '-s key1 -s key2 -s key3...'. The
1108 by using the option several times: '-s key1 -s key2 -s key3...'. The
1106 default sorting key is 'time'.
1109 default sorting key is 'time'.
1107
1110
1108 The following is copied verbatim from the profile documentation
1111 The following is copied verbatim from the profile documentation
1109 referenced below:
1112 referenced below:
1110
1113
1111 When more than one key is provided, additional keys are used as
1114 When more than one key is provided, additional keys are used as
1112 secondary criteria when the there is equality in all keys selected
1115 secondary criteria when the there is equality in all keys selected
1113 before them.
1116 before them.
1114
1117
1115 Abbreviations can be used for any key names, as long as the
1118 Abbreviations can be used for any key names, as long as the
1116 abbreviation is unambiguous. The following are the keys currently
1119 abbreviation is unambiguous. The following are the keys currently
1117 defined:
1120 defined:
1118
1121
1119 Valid Arg Meaning\\
1122 Valid Arg Meaning\\
1120 "calls" call count\\
1123 "calls" call count\\
1121 "cumulative" cumulative time\\
1124 "cumulative" cumulative time\\
1122 "file" file name\\
1125 "file" file name\\
1123 "module" file name\\
1126 "module" file name\\
1124 "pcalls" primitive call count\\
1127 "pcalls" primitive call count\\
1125 "line" line number\\
1128 "line" line number\\
1126 "name" function name\\
1129 "name" function name\\
1127 "nfl" name/file/line\\
1130 "nfl" name/file/line\\
1128 "stdname" standard name\\
1131 "stdname" standard name\\
1129 "time" internal time
1132 "time" internal time
1130
1133
1131 Note that all sorts on statistics are in descending order (placing
1134 Note that all sorts on statistics are in descending order (placing
1132 most time consuming items first), where as name, file, and line number
1135 most time consuming items first), where as name, file, and line number
1133 searches are in ascending order (i.e., alphabetical). The subtle
1136 searches are in ascending order (i.e., alphabetical). The subtle
1134 distinction between "nfl" and "stdname" is that the standard name is a
1137 distinction between "nfl" and "stdname" is that the standard name is a
1135 sort of the name as printed, which means that the embedded line
1138 sort of the name as printed, which means that the embedded line
1136 numbers get compared in an odd way. For example, lines 3, 20, and 40
1139 numbers get compared in an odd way. For example, lines 3, 20, and 40
1137 would (if the file names were the same) appear in the string order
1140 would (if the file names were the same) appear in the string order
1138 "20" "3" and "40". In contrast, "nfl" does a numeric compare of the
1141 "20" "3" and "40". In contrast, "nfl" does a numeric compare of the
1139 line numbers. In fact, sort_stats("nfl") is the same as
1142 line numbers. In fact, sort_stats("nfl") is the same as
1140 sort_stats("name", "file", "line").
1143 sort_stats("name", "file", "line").
1141
1144
1142 -T <filename>: save profile results as shown on screen to a text
1145 -T <filename>: save profile results as shown on screen to a text
1143 file. The profile is still shown on screen.
1146 file. The profile is still shown on screen.
1144
1147
1145 -D <filename>: save (via dump_stats) profile statistics to given
1148 -D <filename>: save (via dump_stats) profile statistics to given
1146 filename. This data is in a format understod by the pstats module, and
1149 filename. This data is in a format understod by the pstats module, and
1147 is generated by a call to the dump_stats() method of profile
1150 is generated by a call to the dump_stats() method of profile
1148 objects. The profile is still shown on screen.
1151 objects. The profile is still shown on screen.
1149
1152
1150 If you want to run complete programs under the profiler's control, use
1153 If you want to run complete programs under the profiler's control, use
1151 '%run -p [prof_opts] filename.py [args to program]' where prof_opts
1154 '%run -p [prof_opts] filename.py [args to program]' where prof_opts
1152 contains profiler specific options as described here.
1155 contains profiler specific options as described here.
1153
1156
1154 You can read the complete documentation for the profile module with:\\
1157 You can read the complete documentation for the profile module with:\\
1155 In [1]: import profile; profile.help() """
1158 In [1]: import profile; profile.help() """
1156
1159
1157 opts_def = Struct(D=[''],l=[],s=['time'],T=[''])
1160 opts_def = Struct(D=[''],l=[],s=['time'],T=[''])
1158 # protect user quote marks
1161 # protect user quote marks
1159 parameter_s = parameter_s.replace('"',r'\"').replace("'",r"\'")
1162 parameter_s = parameter_s.replace('"',r'\"').replace("'",r"\'")
1160
1163
1161 if user_mode: # regular user call
1164 if user_mode: # regular user call
1162 opts,arg_str = self.parse_options(parameter_s,'D:l:rs:T:',
1165 opts,arg_str = self.parse_options(parameter_s,'D:l:rs:T:',
1163 list_all=1)
1166 list_all=1)
1164 namespace = self.shell.user_ns
1167 namespace = self.shell.user_ns
1165 else: # called to run a program by %run -p
1168 else: # called to run a program by %run -p
1166 try:
1169 try:
1167 filename = get_py_filename(arg_lst[0])
1170 filename = get_py_filename(arg_lst[0])
1168 except IOError,msg:
1171 except IOError,msg:
1169 error(msg)
1172 error(msg)
1170 return
1173 return
1171
1174
1172 arg_str = 'execfile(filename,prog_ns)'
1175 arg_str = 'execfile(filename,prog_ns)'
1173 namespace = locals()
1176 namespace = locals()
1174
1177
1175 opts.merge(opts_def)
1178 opts.merge(opts_def)
1176
1179
1177 prof = profile.Profile()
1180 prof = profile.Profile()
1178 try:
1181 try:
1179 prof = prof.runctx(arg_str,namespace,namespace)
1182 prof = prof.runctx(arg_str,namespace,namespace)
1180 sys_exit = ''
1183 sys_exit = ''
1181 except SystemExit:
1184 except SystemExit:
1182 sys_exit = """*** SystemExit exception caught in code being profiled."""
1185 sys_exit = """*** SystemExit exception caught in code being profiled."""
1183
1186
1184 stats = pstats.Stats(prof).strip_dirs().sort_stats(*opts.s)
1187 stats = pstats.Stats(prof).strip_dirs().sort_stats(*opts.s)
1185
1188
1186 lims = opts.l
1189 lims = opts.l
1187 if lims:
1190 if lims:
1188 lims = [] # rebuild lims with ints/floats/strings
1191 lims = [] # rebuild lims with ints/floats/strings
1189 for lim in opts.l:
1192 for lim in opts.l:
1190 try:
1193 try:
1191 lims.append(int(lim))
1194 lims.append(int(lim))
1192 except ValueError:
1195 except ValueError:
1193 try:
1196 try:
1194 lims.append(float(lim))
1197 lims.append(float(lim))
1195 except ValueError:
1198 except ValueError:
1196 lims.append(lim)
1199 lims.append(lim)
1197
1200
1198 # trap output
1201 # trap output
1199 sys_stdout = sys.stdout
1202 sys_stdout = sys.stdout
1200 stdout_trap = StringIO()
1203 stdout_trap = StringIO()
1201 try:
1204 try:
1202 sys.stdout = stdout_trap
1205 sys.stdout = stdout_trap
1203 stats.print_stats(*lims)
1206 stats.print_stats(*lims)
1204 finally:
1207 finally:
1205 sys.stdout = sys_stdout
1208 sys.stdout = sys_stdout
1206 output = stdout_trap.getvalue()
1209 output = stdout_trap.getvalue()
1207 output = output.rstrip()
1210 output = output.rstrip()
1208
1211
1209 page(output,screen_lines=self.shell.rc.screen_length)
1212 page(output,screen_lines=self.shell.rc.screen_length)
1210 print sys_exit,
1213 print sys_exit,
1211
1214
1212 dump_file = opts.D[0]
1215 dump_file = opts.D[0]
1213 text_file = opts.T[0]
1216 text_file = opts.T[0]
1214 if dump_file:
1217 if dump_file:
1215 prof.dump_stats(dump_file)
1218 prof.dump_stats(dump_file)
1216 print '\n*** Profile stats marshalled to file',\
1219 print '\n*** Profile stats marshalled to file',\
1217 `dump_file`+'.',sys_exit
1220 `dump_file`+'.',sys_exit
1218 if text_file:
1221 if text_file:
1219 file(text_file,'w').write(output)
1222 file(text_file,'w').write(output)
1220 print '\n*** Profile printout saved to text file',\
1223 print '\n*** Profile printout saved to text file',\
1221 `text_file`+'.',sys_exit
1224 `text_file`+'.',sys_exit
1222
1225
1223 if opts.has_key('r'):
1226 if opts.has_key('r'):
1224 return stats
1227 return stats
1225 else:
1228 else:
1226 return None
1229 return None
1227
1230
1228 def magic_run(self, parameter_s ='',runner=None):
1231 def magic_run(self, parameter_s ='',runner=None):
1229 """Run the named file inside IPython as a program.
1232 """Run the named file inside IPython as a program.
1230
1233
1231 Usage:\\
1234 Usage:\\
1232 %run [-n -i -t [-N<N>] -d [-b<N>] -p [profile options]] file [args]
1235 %run [-n -i -t [-N<N>] -d [-b<N>] -p [profile options]] file [args]
1233
1236
1234 Parameters after the filename are passed as command-line arguments to
1237 Parameters after the filename are passed as command-line arguments to
1235 the program (put in sys.argv). Then, control returns to IPython's
1238 the program (put in sys.argv). Then, control returns to IPython's
1236 prompt.
1239 prompt.
1237
1240
1238 This is similar to running at a system prompt:\\
1241 This is similar to running at a system prompt:\\
1239 $ python file args\\
1242 $ python file args\\
1240 but with the advantage of giving you IPython's tracebacks, and of
1243 but with the advantage of giving you IPython's tracebacks, and of
1241 loading all variables into your interactive namespace for further use
1244 loading all variables into your interactive namespace for further use
1242 (unless -p is used, see below).
1245 (unless -p is used, see below).
1243
1246
1244 The file is executed in a namespace initially consisting only of
1247 The file is executed in a namespace initially consisting only of
1245 __name__=='__main__' and sys.argv constructed as indicated. It thus
1248 __name__=='__main__' and sys.argv constructed as indicated. It thus
1246 sees its environment as if it were being run as a stand-alone
1249 sees its environment as if it were being run as a stand-alone
1247 program. But after execution, the IPython interactive namespace gets
1250 program. But after execution, the IPython interactive namespace gets
1248 updated with all variables defined in the program (except for __name__
1251 updated with all variables defined in the program (except for __name__
1249 and sys.argv). This allows for very convenient loading of code for
1252 and sys.argv). This allows for very convenient loading of code for
1250 interactive work, while giving each program a 'clean sheet' to run in.
1253 interactive work, while giving each program a 'clean sheet' to run in.
1251
1254
1252 Options:
1255 Options:
1253
1256
1254 -n: __name__ is NOT set to '__main__', but to the running file's name
1257 -n: __name__ is NOT set to '__main__', but to the running file's name
1255 without extension (as python does under import). This allows running
1258 without extension (as python does under import). This allows running
1256 scripts and reloading the definitions in them without calling code
1259 scripts and reloading the definitions in them without calling code
1257 protected by an ' if __name__ == "__main__" ' clause.
1260 protected by an ' if __name__ == "__main__" ' clause.
1258
1261
1259 -i: run the file in IPython's namespace instead of an empty one. This
1262 -i: run the file in IPython's namespace instead of an empty one. This
1260 is useful if you are experimenting with code written in a text editor
1263 is useful if you are experimenting with code written in a text editor
1261 which depends on variables defined interactively.
1264 which depends on variables defined interactively.
1262
1265
1263 -e: ignore sys.exit() calls or SystemExit exceptions in the script
1266 -e: ignore sys.exit() calls or SystemExit exceptions in the script
1264 being run. This is particularly useful if IPython is being used to
1267 being run. This is particularly useful if IPython is being used to
1265 run unittests, which always exit with a sys.exit() call. In such
1268 run unittests, which always exit with a sys.exit() call. In such
1266 cases you are interested in the output of the test results, not in
1269 cases you are interested in the output of the test results, not in
1267 seeing a traceback of the unittest module.
1270 seeing a traceback of the unittest module.
1268
1271
1269 -t: print timing information at the end of the run. IPython will give
1272 -t: print timing information at the end of the run. IPython will give
1270 you an estimated CPU time consumption for your script, which under
1273 you an estimated CPU time consumption for your script, which under
1271 Unix uses the resource module to avoid the wraparound problems of
1274 Unix uses the resource module to avoid the wraparound problems of
1272 time.clock(). Under Unix, an estimate of time spent on system tasks
1275 time.clock(). Under Unix, an estimate of time spent on system tasks
1273 is also given (for Windows platforms this is reported as 0.0).
1276 is also given (for Windows platforms this is reported as 0.0).
1274
1277
1275 If -t is given, an additional -N<N> option can be given, where <N>
1278 If -t is given, an additional -N<N> option can be given, where <N>
1276 must be an integer indicating how many times you want the script to
1279 must be an integer indicating how many times you want the script to
1277 run. The final timing report will include total and per run results.
1280 run. The final timing report will include total and per run results.
1278
1281
1279 For example (testing the script uniq_stable.py):
1282 For example (testing the script uniq_stable.py):
1280
1283
1281 In [1]: run -t uniq_stable
1284 In [1]: run -t uniq_stable
1282
1285
1283 IPython CPU timings (estimated):\\
1286 IPython CPU timings (estimated):\\
1284 User : 0.19597 s.\\
1287 User : 0.19597 s.\\
1285 System: 0.0 s.\\
1288 System: 0.0 s.\\
1286
1289
1287 In [2]: run -t -N5 uniq_stable
1290 In [2]: run -t -N5 uniq_stable
1288
1291
1289 IPython CPU timings (estimated):\\
1292 IPython CPU timings (estimated):\\
1290 Total runs performed: 5\\
1293 Total runs performed: 5\\
1291 Times : Total Per run\\
1294 Times : Total Per run\\
1292 User : 0.910862 s, 0.1821724 s.\\
1295 User : 0.910862 s, 0.1821724 s.\\
1293 System: 0.0 s, 0.0 s.
1296 System: 0.0 s, 0.0 s.
1294
1297
1295 -d: run your program under the control of pdb, the Python debugger.
1298 -d: run your program under the control of pdb, the Python debugger.
1296 This allows you to execute your program step by step, watch variables,
1299 This allows you to execute your program step by step, watch variables,
1297 etc. Internally, what IPython does is similar to calling:
1300 etc. Internally, what IPython does is similar to calling:
1298
1301
1299 pdb.run('execfile("YOURFILENAME")')
1302 pdb.run('execfile("YOURFILENAME")')
1300
1303
1301 with a breakpoint set on line 1 of your file. You can change the line
1304 with a breakpoint set on line 1 of your file. You can change the line
1302 number for this automatic breakpoint to be <N> by using the -bN option
1305 number for this automatic breakpoint to be <N> by using the -bN option
1303 (where N must be an integer). For example:
1306 (where N must be an integer). For example:
1304
1307
1305 %run -d -b40 myscript
1308 %run -d -b40 myscript
1306
1309
1307 will set the first breakpoint at line 40 in myscript.py. Note that
1310 will set the first breakpoint at line 40 in myscript.py. Note that
1308 the first breakpoint must be set on a line which actually does
1311 the first breakpoint must be set on a line which actually does
1309 something (not a comment or docstring) for it to stop execution.
1312 something (not a comment or docstring) for it to stop execution.
1310
1313
1311 When the pdb debugger starts, you will see a (Pdb) prompt. You must
1314 When the pdb debugger starts, you will see a (Pdb) prompt. You must
1312 first enter 'c' (without qoutes) to start execution up to the first
1315 first enter 'c' (without qoutes) to start execution up to the first
1313 breakpoint.
1316 breakpoint.
1314
1317
1315 Entering 'help' gives information about the use of the debugger. You
1318 Entering 'help' gives information about the use of the debugger. You
1316 can easily see pdb's full documentation with "import pdb;pdb.help()"
1319 can easily see pdb's full documentation with "import pdb;pdb.help()"
1317 at a prompt.
1320 at a prompt.
1318
1321
1319 -p: run program under the control of the Python profiler module (which
1322 -p: run program under the control of the Python profiler module (which
1320 prints a detailed report of execution times, function calls, etc).
1323 prints a detailed report of execution times, function calls, etc).
1321
1324
1322 You can pass other options after -p which affect the behavior of the
1325 You can pass other options after -p which affect the behavior of the
1323 profiler itself. See the docs for %prun for details.
1326 profiler itself. See the docs for %prun for details.
1324
1327
1325 In this mode, the program's variables do NOT propagate back to the
1328 In this mode, the program's variables do NOT propagate back to the
1326 IPython interactive namespace (because they remain in the namespace
1329 IPython interactive namespace (because they remain in the namespace
1327 where the profiler executes them).
1330 where the profiler executes them).
1328
1331
1329 Internally this triggers a call to %prun, see its documentation for
1332 Internally this triggers a call to %prun, see its documentation for
1330 details on the options available specifically for profiling."""
1333 details on the options available specifically for profiling."""
1331
1334
1332 # get arguments and set sys.argv for program to be run.
1335 # get arguments and set sys.argv for program to be run.
1333 opts,arg_lst = self.parse_options(parameter_s,'nidtN:b:pD:l:rs:T:e',
1336 opts,arg_lst = self.parse_options(parameter_s,'nidtN:b:pD:l:rs:T:e',
1334 mode='list',list_all=1)
1337 mode='list',list_all=1)
1335
1338
1336 try:
1339 try:
1337 filename = get_py_filename(arg_lst[0])
1340 filename = get_py_filename(arg_lst[0])
1338 except IndexError:
1341 except IndexError:
1339 warn('you must provide at least a filename.')
1342 warn('you must provide at least a filename.')
1340 print '\n%run:\n',OInspect.getdoc(self.magic_run)
1343 print '\n%run:\n',OInspect.getdoc(self.magic_run)
1341 return
1344 return
1342 except IOError,msg:
1345 except IOError,msg:
1343 error(msg)
1346 error(msg)
1344 return
1347 return
1345
1348
1346 # Control the response to exit() calls made by the script being run
1349 # Control the response to exit() calls made by the script being run
1347 exit_ignore = opts.has_key('e')
1350 exit_ignore = opts.has_key('e')
1348
1351
1349 # Make sure that the running script gets a proper sys.argv as if it
1352 # Make sure that the running script gets a proper sys.argv as if it
1350 # were run from a system shell.
1353 # were run from a system shell.
1351 save_argv = sys.argv # save it for later restoring
1354 save_argv = sys.argv # save it for later restoring
1352 sys.argv = [filename]+ arg_lst[1:] # put in the proper filename
1355 sys.argv = [filename]+ arg_lst[1:] # put in the proper filename
1353
1356
1354 if opts.has_key('i'):
1357 if opts.has_key('i'):
1355 prog_ns = self.shell.user_ns
1358 prog_ns = self.shell.user_ns
1356 __name__save = self.shell.user_ns['__name__']
1359 __name__save = self.shell.user_ns['__name__']
1357 prog_ns['__name__'] = '__main__'
1360 prog_ns['__name__'] = '__main__'
1358 else:
1361 else:
1359 if opts.has_key('n'):
1362 if opts.has_key('n'):
1360 name = os.path.splitext(os.path.basename(filename))[0]
1363 name = os.path.splitext(os.path.basename(filename))[0]
1361 else:
1364 else:
1362 name = '__main__'
1365 name = '__main__'
1363 prog_ns = {'__name__':name}
1366 prog_ns = {'__name__':name}
1364
1367
1365 # pickle fix. See iplib for an explanation
1368 # pickle fix. See iplib for an explanation
1366 sys.modules[prog_ns['__name__']] = FakeModule(prog_ns)
1369 sys.modules[prog_ns['__name__']] = FakeModule(prog_ns)
1367
1370
1368 stats = None
1371 stats = None
1369 try:
1372 try:
1370 if opts.has_key('p'):
1373 if opts.has_key('p'):
1371 stats = self.magic_prun('',0,opts,arg_lst,prog_ns)
1374 stats = self.magic_prun('',0,opts,arg_lst,prog_ns)
1372 else:
1375 else:
1373 if opts.has_key('d'):
1376 if opts.has_key('d'):
1374 deb = pdb.Pdb()
1377 deb = Debugger.Pdb(self.shell.rc.colors)
1375 # reset Breakpoint state, which is moronically kept
1378 # reset Breakpoint state, which is moronically kept
1376 # in a class
1379 # in a class
1377 bdb.Breakpoint.next = 1
1380 bdb.Breakpoint.next = 1
1378 bdb.Breakpoint.bplist = {}
1381 bdb.Breakpoint.bplist = {}
1379 bdb.Breakpoint.bpbynumber = [None]
1382 bdb.Breakpoint.bpbynumber = [None]
1380 # Set an initial breakpoint to stop execution
1383 # Set an initial breakpoint to stop execution
1381 maxtries = 10
1384 maxtries = 10
1382 bp = int(opts.get('b',[1])[0])
1385 bp = int(opts.get('b',[1])[0])
1383 checkline = deb.checkline(filename,bp)
1386 checkline = deb.checkline(filename,bp)
1384 if not checkline:
1387 if not checkline:
1385 for bp in range(bp+1,bp+maxtries+1):
1388 for bp in range(bp+1,bp+maxtries+1):
1386 if deb.checkline(filename,bp):
1389 if deb.checkline(filename,bp):
1387 break
1390 break
1388 else:
1391 else:
1389 msg = ("\nI failed to find a valid line to set "
1392 msg = ("\nI failed to find a valid line to set "
1390 "a breakpoint\n"
1393 "a breakpoint\n"
1391 "after trying up to line: %s.\n"
1394 "after trying up to line: %s.\n"
1392 "Please set a valid breakpoint manually "
1395 "Please set a valid breakpoint manually "
1393 "with the -b option." % bp)
1396 "with the -b option." % bp)
1394 error(msg)
1397 error(msg)
1395 return
1398 return
1396 # if we find a good linenumber, set the breakpoint
1399 # if we find a good linenumber, set the breakpoint
1397 deb.do_break('%s:%s' % (filename,bp))
1400 deb.do_break('%s:%s' % (filename,bp))
1398 # Start file run
1401 # Start file run
1399 print "NOTE: Enter 'c' at the",
1402 print "NOTE: Enter 'c' at the",
1400 print "(Pdb) prompt to start your script."
1403 print "ipdb> prompt to start your script."
1404 try:
1401 deb.run('execfile("%s")' % filename,prog_ns)
1405 deb.run('execfile("%s")' % filename,prog_ns)
1406 except:
1407 etype, value, tb = sys.exc_info()
1408 # Skip three frames in the traceback: the %run one,
1409 # one inside bdb.py, and the command-line typed by the
1410 # user (run by exec in pdb itself).
1411 self.shell.InteractiveTB(etype,value,tb,tb_offset=3)
1402 else:
1412 else:
1403 if runner is None:
1413 if runner is None:
1404 runner = self.shell.safe_execfile
1414 runner = self.shell.safe_execfile
1405 if opts.has_key('t'):
1415 if opts.has_key('t'):
1406 try:
1416 try:
1407 nruns = int(opts['N'][0])
1417 nruns = int(opts['N'][0])
1408 if nruns < 1:
1418 if nruns < 1:
1409 error('Number of runs must be >=1')
1419 error('Number of runs must be >=1')
1410 return
1420 return
1411 except (KeyError):
1421 except (KeyError):
1412 nruns = 1
1422 nruns = 1
1413 if nruns == 1:
1423 if nruns == 1:
1414 t0 = clock2()
1424 t0 = clock2()
1415 runner(filename,prog_ns,prog_ns,exit_ignore=exit_ignore)
1425 runner(filename,prog_ns,prog_ns,exit_ignore=exit_ignore)
1416 t1 = clock2()
1426 t1 = clock2()
1417 t_usr = t1[0]-t0[0]
1427 t_usr = t1[0]-t0[0]
1418 t_sys = t1[1]-t1[1]
1428 t_sys = t1[1]-t1[1]
1419 print "\nIPython CPU timings (estimated):"
1429 print "\nIPython CPU timings (estimated):"
1420 print " User : %10s s." % t_usr
1430 print " User : %10s s." % t_usr
1421 print " System: %10s s." % t_sys
1431 print " System: %10s s." % t_sys
1422 else:
1432 else:
1423 runs = range(nruns)
1433 runs = range(nruns)
1424 t0 = clock2()
1434 t0 = clock2()
1425 for nr in runs:
1435 for nr in runs:
1426 runner(filename,prog_ns,prog_ns,exit_ignore=exit_ignore)
1436 runner(filename,prog_ns,prog_ns,exit_ignore=exit_ignore)
1427 t1 = clock2()
1437 t1 = clock2()
1428 t_usr = t1[0]-t0[0]
1438 t_usr = t1[0]-t0[0]
1429 t_sys = t1[1]-t1[1]
1439 t_sys = t1[1]-t1[1]
1430 print "\nIPython CPU timings (estimated):"
1440 print "\nIPython CPU timings (estimated):"
1431 print "Total runs performed:",nruns
1441 print "Total runs performed:",nruns
1432 print " Times : %10s %10s" % ('Total','Per run')
1442 print " Times : %10s %10s" % ('Total','Per run')
1433 print " User : %10s s, %10s s." % (t_usr,t_usr/nruns)
1443 print " User : %10s s, %10s s." % (t_usr,t_usr/nruns)
1434 print " System: %10s s, %10s s." % (t_sys,t_sys/nruns)
1444 print " System: %10s s, %10s s." % (t_sys,t_sys/nruns)
1435
1445
1436 else:
1446 else:
1437 runner(filename,prog_ns,prog_ns,exit_ignore=exit_ignore)
1447 runner(filename,prog_ns,prog_ns,exit_ignore=exit_ignore)
1438 if opts.has_key('i'):
1448 if opts.has_key('i'):
1439 self.shell.user_ns['__name__'] = __name__save
1449 self.shell.user_ns['__name__'] = __name__save
1440 else:
1450 else:
1441 # update IPython interactive namespace
1451 # update IPython interactive namespace
1442 del prog_ns['__name__']
1452 del prog_ns['__name__']
1443 self.shell.user_ns.update(prog_ns)
1453 self.shell.user_ns.update(prog_ns)
1444 finally:
1454 finally:
1445 sys.argv = save_argv
1455 sys.argv = save_argv
1446 return stats
1456 return stats
1447
1457
1448 def magic_runlog(self, parameter_s =''):
1458 def magic_runlog(self, parameter_s =''):
1449 """Run files as logs.
1459 """Run files as logs.
1450
1460
1451 Usage:\\
1461 Usage:\\
1452 %runlog file1 file2 ...
1462 %runlog file1 file2 ...
1453
1463
1454 Run the named files (treating them as log files) in sequence inside
1464 Run the named files (treating them as log files) in sequence inside
1455 the interpreter, and return to the prompt. This is much slower than
1465 the interpreter, and return to the prompt. This is much slower than
1456 %run because each line is executed in a try/except block, but it
1466 %run because each line is executed in a try/except block, but it
1457 allows running files with syntax errors in them.
1467 allows running files with syntax errors in them.
1458
1468
1459 Normally IPython will guess when a file is one of its own logfiles, so
1469 Normally IPython will guess when a file is one of its own logfiles, so
1460 you can typically use %run even for logs. This shorthand allows you to
1470 you can typically use %run even for logs. This shorthand allows you to
1461 force any file to be treated as a log file."""
1471 force any file to be treated as a log file."""
1462
1472
1463 for f in parameter_s.split():
1473 for f in parameter_s.split():
1464 self.shell.safe_execfile(f,self.shell.user_ns,
1474 self.shell.safe_execfile(f,self.shell.user_ns,
1465 self.shell.user_ns,islog=1)
1475 self.shell.user_ns,islog=1)
1466
1476
1467 def magic_time(self,parameter_s = ''):
1477 def magic_time(self,parameter_s = ''):
1468 """Time execution of a Python statement or expression.
1478 """Time execution of a Python statement or expression.
1469
1479
1470 The CPU and wall clock times are printed, and the value of the
1480 The CPU and wall clock times are printed, and the value of the
1471 expression (if any) is returned. Note that under Win32, system time
1481 expression (if any) is returned. Note that under Win32, system time
1472 is always reported as 0, since it can not be measured.
1482 is always reported as 0, since it can not be measured.
1473
1483
1474 This function provides very basic timing functionality. In Python
1484 This function provides very basic timing functionality. In Python
1475 2.3, the timeit module offers more control and sophistication, but for
1485 2.3, the timeit module offers more control and sophistication, but for
1476 now IPython supports Python 2.2, so we can not rely on timeit being
1486 now IPython supports Python 2.2, so we can not rely on timeit being
1477 present.
1487 present.
1478
1488
1479 Some examples:
1489 Some examples:
1480
1490
1481 In [1]: time 2**128
1491 In [1]: time 2**128
1482 CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s
1492 CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s
1483 Wall time: 0.00
1493 Wall time: 0.00
1484 Out[1]: 340282366920938463463374607431768211456L
1494 Out[1]: 340282366920938463463374607431768211456L
1485
1495
1486 In [2]: n = 1000000
1496 In [2]: n = 1000000
1487
1497
1488 In [3]: time sum(range(n))
1498 In [3]: time sum(range(n))
1489 CPU times: user 1.20 s, sys: 0.05 s, total: 1.25 s
1499 CPU times: user 1.20 s, sys: 0.05 s, total: 1.25 s
1490 Wall time: 1.37
1500 Wall time: 1.37
1491 Out[3]: 499999500000L
1501 Out[3]: 499999500000L
1492
1502
1493 In [4]: time print 'hello world'
1503 In [4]: time print 'hello world'
1494 hello world
1504 hello world
1495 CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s
1505 CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s
1496 Wall time: 0.00
1506 Wall time: 0.00
1497 """
1507 """
1498
1508
1499 # fail immediately if the given expression can't be compiled
1509 # fail immediately if the given expression can't be compiled
1500 try:
1510 try:
1501 mode = 'eval'
1511 mode = 'eval'
1502 code = compile(parameter_s,'<timed eval>',mode)
1512 code = compile(parameter_s,'<timed eval>',mode)
1503 except SyntaxError:
1513 except SyntaxError:
1504 mode = 'exec'
1514 mode = 'exec'
1505 code = compile(parameter_s,'<timed exec>',mode)
1515 code = compile(parameter_s,'<timed exec>',mode)
1506 # skew measurement as little as possible
1516 # skew measurement as little as possible
1507 glob = self.shell.user_ns
1517 glob = self.shell.user_ns
1508 clk = clock2
1518 clk = clock2
1509 wtime = time.time
1519 wtime = time.time
1510 # time execution
1520 # time execution
1511 wall_st = wtime()
1521 wall_st = wtime()
1512 if mode=='eval':
1522 if mode=='eval':
1513 st = clk()
1523 st = clk()
1514 out = eval(code,glob)
1524 out = eval(code,glob)
1515 end = clk()
1525 end = clk()
1516 else:
1526 else:
1517 st = clk()
1527 st = clk()
1518 exec code in glob
1528 exec code in glob
1519 end = clk()
1529 end = clk()
1520 out = None
1530 out = None
1521 wall_end = wtime()
1531 wall_end = wtime()
1522 # Compute actual times and report
1532 # Compute actual times and report
1523 wall_time = wall_end-wall_st
1533 wall_time = wall_end-wall_st
1524 cpu_user = end[0]-st[0]
1534 cpu_user = end[0]-st[0]
1525 cpu_sys = end[1]-st[1]
1535 cpu_sys = end[1]-st[1]
1526 cpu_tot = cpu_user+cpu_sys
1536 cpu_tot = cpu_user+cpu_sys
1527 print "CPU times: user %.2f s, sys: %.2f s, total: %.2f s" % \
1537 print "CPU times: user %.2f s, sys: %.2f s, total: %.2f s" % \
1528 (cpu_user,cpu_sys,cpu_tot)
1538 (cpu_user,cpu_sys,cpu_tot)
1529 print "Wall time: %.2f" % wall_time
1539 print "Wall time: %.2f" % wall_time
1530 return out
1540 return out
1531
1541
1532 def magic_macro(self,parameter_s = ''):
1542 def magic_macro(self,parameter_s = ''):
1533 """Define a set of input lines as a macro for future re-execution.
1543 """Define a set of input lines as a macro for future re-execution.
1534
1544
1535 Usage:\\
1545 Usage:\\
1536 %macro name n1:n2 n3:n4 ... n5 .. n6 ...
1546 %macro name n1:n2 n3:n4 ... n5 .. n6 ...
1537
1547
1538 This will define a global variable called `name` which is a string
1548 This will define a global variable called `name` which is a string
1539 made of joining the slices and lines you specify (n1,n2,... numbers
1549 made of joining the slices and lines you specify (n1,n2,... numbers
1540 above) from your input history into a single string. This variable
1550 above) from your input history into a single string. This variable
1541 acts like an automatic function which re-executes those lines as if
1551 acts like an automatic function which re-executes those lines as if
1542 you had typed them. You just type 'name' at the prompt and the code
1552 you had typed them. You just type 'name' at the prompt and the code
1543 executes.
1553 executes.
1544
1554
1545 Note that the slices use the standard Python slicing notation (5:8
1555 Note that the slices use the standard Python slicing notation (5:8
1546 means include lines numbered 5,6,7).
1556 means include lines numbered 5,6,7).
1547
1557
1548 For example, if your history contains (%hist prints it):
1558 For example, if your history contains (%hist prints it):
1549
1559
1550 44: x=1\\
1560 44: x=1\\
1551 45: y=3\\
1561 45: y=3\\
1552 46: z=x+y\\
1562 46: z=x+y\\
1553 47: print x\\
1563 47: print x\\
1554 48: a=5\\
1564 48: a=5\\
1555 49: print 'x',x,'y',y\\
1565 49: print 'x',x,'y',y\\
1556
1566
1557 you can create a macro with lines 44 through 47 (included) and line 49
1567 you can create a macro with lines 44 through 47 (included) and line 49
1558 called my_macro with:
1568 called my_macro with:
1559
1569
1560 In [51]: %macro my_macro 44:48 49
1570 In [51]: %macro my_macro 44:48 49
1561
1571
1562 Now, typing `my_macro` (without quotes) will re-execute all this code
1572 Now, typing `my_macro` (without quotes) will re-execute all this code
1563 in one pass.
1573 in one pass.
1564
1574
1565 You don't need to give the line-numbers in order, and any given line
1575 You don't need to give the line-numbers in order, and any given line
1566 number can appear multiple times. You can assemble macros with any
1576 number can appear multiple times. You can assemble macros with any
1567 lines from your input history in any order.
1577 lines from your input history in any order.
1568
1578
1569 The macro is a simple object which holds its value in an attribute,
1579 The macro is a simple object which holds its value in an attribute,
1570 but IPython's display system checks for macros and executes them as
1580 but IPython's display system checks for macros and executes them as
1571 code instead of printing them when you type their name.
1581 code instead of printing them when you type their name.
1572
1582
1573 You can view a macro's contents by explicitly printing it with:
1583 You can view a macro's contents by explicitly printing it with:
1574
1584
1575 'print macro_name'.
1585 'print macro_name'.
1576
1586
1577 For one-off cases which DON'T contain magic function calls in them you
1587 For one-off cases which DON'T contain magic function calls in them you
1578 can obtain similar results by explicitly executing slices from your
1588 can obtain similar results by explicitly executing slices from your
1579 input history with:
1589 input history with:
1580
1590
1581 In [60]: exec In[44:48]+In[49]"""
1591 In [60]: exec In[44:48]+In[49]"""
1582
1592
1583 args = parameter_s.split()
1593 args = parameter_s.split()
1584 name,ranges = args[0], args[1:]
1594 name,ranges = args[0], args[1:]
1585 #print 'rng',ranges # dbg
1595 #print 'rng',ranges # dbg
1586 cmds = self.extract_input_slices(ranges)
1596 cmds = self.extract_input_slices(ranges)
1587 macro = Macro(cmds)
1597 macro = Macro(cmds)
1588 self.shell.user_ns.update({name:macro})
1598 self.shell.user_ns.update({name:macro})
1589 print 'Macro `%s` created. To execute, type its name (without quotes).' % name
1599 print 'Macro `%s` created. To execute, type its name (without quotes).' % name
1590 print 'Macro contents:'
1600 print 'Macro contents:'
1591 print str(macro).rstrip(),
1601 print str(macro).rstrip(),
1592
1602
1593 def magic_save(self,parameter_s = ''):
1603 def magic_save(self,parameter_s = ''):
1594 """Save a set of lines to a given filename.
1604 """Save a set of lines to a given filename.
1595
1605
1596 Usage:\\
1606 Usage:\\
1597 %save filename n1:n2 n3:n4 ... n5 .. n6 ...
1607 %save filename n1:n2 n3:n4 ... n5 .. n6 ...
1598
1608
1599 This function uses the same syntax as %macro for line extraction, but
1609 This function uses the same syntax as %macro for line extraction, but
1600 instead of creating a macro it saves the resulting string to the
1610 instead of creating a macro it saves the resulting string to the
1601 filename you specify.
1611 filename you specify.
1602
1612
1603 It adds a '.py' extension to the file if you don't do so yourself, and
1613 It adds a '.py' extension to the file if you don't do so yourself, and
1604 it asks for confirmation before overwriting existing files."""
1614 it asks for confirmation before overwriting existing files."""
1605
1615
1606 args = parameter_s.split()
1616 args = parameter_s.split()
1607 fname,ranges = args[0], args[1:]
1617 fname,ranges = args[0], args[1:]
1608 if not fname.endswith('.py'):
1618 if not fname.endswith('.py'):
1609 fname += '.py'
1619 fname += '.py'
1610 if os.path.isfile(fname):
1620 if os.path.isfile(fname):
1611 ans = raw_input('File `%s` exists. Overwrite (y/[N])? ' % fname)
1621 ans = raw_input('File `%s` exists. Overwrite (y/[N])? ' % fname)
1612 if ans.lower() not in ['y','yes']:
1622 if ans.lower() not in ['y','yes']:
1613 print 'Operation cancelled.'
1623 print 'Operation cancelled.'
1614 return
1624 return
1615 cmds = ''.join(self.extract_input_slices(ranges))
1625 cmds = ''.join(self.extract_input_slices(ranges))
1616 f = file(fname,'w')
1626 f = file(fname,'w')
1617 f.write(cmds)
1627 f.write(cmds)
1618 f.close()
1628 f.close()
1619 print 'The following commands were written to file `%s`:' % fname
1629 print 'The following commands were written to file `%s`:' % fname
1620 print cmds
1630 print cmds
1621
1631
1622 def magic_ed(self,parameter_s = ''):
1632 def magic_ed(self,parameter_s = ''):
1623 """Alias to %edit."""
1633 """Alias to %edit."""
1624 return self.magic_edit(parameter_s)
1634 return self.magic_edit(parameter_s)
1625
1635
1626 def magic_edit(self,parameter_s = '',last_call=['','']):
1636 def magic_edit(self,parameter_s = '',last_call=['','']):
1627 """Bring up an editor and execute the resulting code.
1637 """Bring up an editor and execute the resulting code.
1628
1638
1629 Usage:
1639 Usage:
1630 %edit [options] [args]
1640 %edit [options] [args]
1631
1641
1632 %edit runs IPython's editor hook. The default version of this hook is
1642 %edit runs IPython's editor hook. The default version of this hook is
1633 set to call the __IPYTHON__.rc.editor command. This is read from your
1643 set to call the __IPYTHON__.rc.editor command. This is read from your
1634 environment variable $EDITOR. If this isn't found, it will default to
1644 environment variable $EDITOR. If this isn't found, it will default to
1635 vi under Linux/Unix and to notepad under Windows. See the end of this
1645 vi under Linux/Unix and to notepad under Windows. See the end of this
1636 docstring for how to change the editor hook.
1646 docstring for how to change the editor hook.
1637
1647
1638 You can also set the value of this editor via the command line option
1648 You can also set the value of this editor via the command line option
1639 '-editor' or in your ipythonrc file. This is useful if you wish to use
1649 '-editor' or in your ipythonrc file. This is useful if you wish to use
1640 specifically for IPython an editor different from your typical default
1650 specifically for IPython an editor different from your typical default
1641 (and for Windows users who typically don't set environment variables).
1651 (and for Windows users who typically don't set environment variables).
1642
1652
1643 This command allows you to conveniently edit multi-line code right in
1653 This command allows you to conveniently edit multi-line code right in
1644 your IPython session.
1654 your IPython session.
1645
1655
1646 If called without arguments, %edit opens up an empty editor with a
1656 If called without arguments, %edit opens up an empty editor with a
1647 temporary file and will execute the contents of this file when you
1657 temporary file and will execute the contents of this file when you
1648 close it (don't forget to save it!).
1658 close it (don't forget to save it!).
1649
1659
1650 Options:
1660 Options:
1651
1661
1652 -p: this will call the editor with the same data as the previous time
1662 -p: this will call the editor with the same data as the previous time
1653 it was used, regardless of how long ago (in your current session) it
1663 it was used, regardless of how long ago (in your current session) it
1654 was.
1664 was.
1655
1665
1656 -x: do not execute the edited code immediately upon exit. This is
1666 -x: do not execute the edited code immediately upon exit. This is
1657 mainly useful if you are editing programs which need to be called with
1667 mainly useful if you are editing programs which need to be called with
1658 command line arguments, which you can then do using %run.
1668 command line arguments, which you can then do using %run.
1659
1669
1660 Arguments:
1670 Arguments:
1661
1671
1662 If arguments are given, the following possibilites exist:
1672 If arguments are given, the following possibilites exist:
1663
1673
1664 - The arguments are numbers or pairs of colon-separated numbers (like
1674 - The arguments are numbers or pairs of colon-separated numbers (like
1665 1 4:8 9). These are interpreted as lines of previous input to be
1675 1 4:8 9). These are interpreted as lines of previous input to be
1666 loaded into the editor. The syntax is the same of the %macro command.
1676 loaded into the editor. The syntax is the same of the %macro command.
1667
1677
1668 - If the argument doesn't start with a number, it is evaluated as a
1678 - If the argument doesn't start with a number, it is evaluated as a
1669 variable and its contents loaded into the editor. You can thus edit
1679 variable and its contents loaded into the editor. You can thus edit
1670 any string which contains python code (including the result of
1680 any string which contains python code (including the result of
1671 previous edits).
1681 previous edits).
1672
1682
1673 - If the argument is the name of an object (other than a string),
1683 - If the argument is the name of an object (other than a string),
1674 IPython will try to locate the file where it was defined and open the
1684 IPython will try to locate the file where it was defined and open the
1675 editor at the point where it is defined. You can use `%edit function`
1685 editor at the point where it is defined. You can use `%edit function`
1676 to load an editor exactly at the point where 'function' is defined,
1686 to load an editor exactly at the point where 'function' is defined,
1677 edit it and have the file be executed automatically.
1687 edit it and have the file be executed automatically.
1678
1688
1679 Note: opening at an exact line is only supported under Unix, and some
1689 Note: opening at an exact line is only supported under Unix, and some
1680 editors (like kedit and gedit up to Gnome 2.8) do not understand the
1690 editors (like kedit and gedit up to Gnome 2.8) do not understand the
1681 '+NUMBER' parameter necessary for this feature. Good editors like
1691 '+NUMBER' parameter necessary for this feature. Good editors like
1682 (X)Emacs, vi, jed, pico and joe all do.
1692 (X)Emacs, vi, jed, pico and joe all do.
1683
1693
1684 - If the argument is not found as a variable, IPython will look for a
1694 - If the argument is not found as a variable, IPython will look for a
1685 file with that name (adding .py if necessary) and load it into the
1695 file with that name (adding .py if necessary) and load it into the
1686 editor. It will execute its contents with execfile() when you exit,
1696 editor. It will execute its contents with execfile() when you exit,
1687 loading any code in the file into your interactive namespace.
1697 loading any code in the file into your interactive namespace.
1688
1698
1689 After executing your code, %edit will return as output the code you
1699 After executing your code, %edit will return as output the code you
1690 typed in the editor (except when it was an existing file). This way
1700 typed in the editor (except when it was an existing file). This way
1691 you can reload the code in further invocations of %edit as a variable,
1701 you can reload the code in further invocations of %edit as a variable,
1692 via _<NUMBER> or Out[<NUMBER>], where <NUMBER> is the prompt number of
1702 via _<NUMBER> or Out[<NUMBER>], where <NUMBER> is the prompt number of
1693 the output.
1703 the output.
1694
1704
1695 Note that %edit is also available through the alias %ed.
1705 Note that %edit is also available through the alias %ed.
1696
1706
1697 This is an example of creating a simple function inside the editor and
1707 This is an example of creating a simple function inside the editor and
1698 then modifying it. First, start up the editor:
1708 then modifying it. First, start up the editor:
1699
1709
1700 In [1]: ed\\
1710 In [1]: ed\\
1701 Editing... done. Executing edited code...\\
1711 Editing... done. Executing edited code...\\
1702 Out[1]: 'def foo():\\n print "foo() was defined in an editing session"\\n'
1712 Out[1]: 'def foo():\\n print "foo() was defined in an editing session"\\n'
1703
1713
1704 We can then call the function foo():
1714 We can then call the function foo():
1705
1715
1706 In [2]: foo()\\
1716 In [2]: foo()\\
1707 foo() was defined in an editing session
1717 foo() was defined in an editing session
1708
1718
1709 Now we edit foo. IPython automatically loads the editor with the
1719 Now we edit foo. IPython automatically loads the editor with the
1710 (temporary) file where foo() was previously defined:
1720 (temporary) file where foo() was previously defined:
1711
1721
1712 In [3]: ed foo\\
1722 In [3]: ed foo\\
1713 Editing... done. Executing edited code...
1723 Editing... done. Executing edited code...
1714
1724
1715 And if we call foo() again we get the modified version:
1725 And if we call foo() again we get the modified version:
1716
1726
1717 In [4]: foo()\\
1727 In [4]: foo()\\
1718 foo() has now been changed!
1728 foo() has now been changed!
1719
1729
1720 Here is an example of how to edit a code snippet successive
1730 Here is an example of how to edit a code snippet successive
1721 times. First we call the editor:
1731 times. First we call the editor:
1722
1732
1723 In [8]: ed\\
1733 In [8]: ed\\
1724 Editing... done. Executing edited code...\\
1734 Editing... done. Executing edited code...\\
1725 hello\\
1735 hello\\
1726 Out[8]: "print 'hello'\\n"
1736 Out[8]: "print 'hello'\\n"
1727
1737
1728 Now we call it again with the previous output (stored in _):
1738 Now we call it again with the previous output (stored in _):
1729
1739
1730 In [9]: ed _\\
1740 In [9]: ed _\\
1731 Editing... done. Executing edited code...\\
1741 Editing... done. Executing edited code...\\
1732 hello world\\
1742 hello world\\
1733 Out[9]: "print 'hello world'\\n"
1743 Out[9]: "print 'hello world'\\n"
1734
1744
1735 Now we call it with the output #8 (stored in _8, also as Out[8]):
1745 Now we call it with the output #8 (stored in _8, also as Out[8]):
1736
1746
1737 In [10]: ed _8\\
1747 In [10]: ed _8\\
1738 Editing... done. Executing edited code...\\
1748 Editing... done. Executing edited code...\\
1739 hello again\\
1749 hello again\\
1740 Out[10]: "print 'hello again'\\n"
1750 Out[10]: "print 'hello again'\\n"
1741
1751
1742
1752
1743 Changing the default editor hook:
1753 Changing the default editor hook:
1744
1754
1745 If you wish to write your own editor hook, you can put it in a
1755 If you wish to write your own editor hook, you can put it in a
1746 configuration file which you load at startup time. The default hook
1756 configuration file which you load at startup time. The default hook
1747 is defined in the IPython.hooks module, and you can use that as a
1757 is defined in the IPython.hooks module, and you can use that as a
1748 starting example for further modifications. That file also has
1758 starting example for further modifications. That file also has
1749 general instructions on how to set a new hook for use once you've
1759 general instructions on how to set a new hook for use once you've
1750 defined it."""
1760 defined it."""
1751
1761
1752 # FIXME: This function has become a convoluted mess. It needs a
1762 # FIXME: This function has become a convoluted mess. It needs a
1753 # ground-up rewrite with clean, simple logic.
1763 # ground-up rewrite with clean, simple logic.
1754
1764
1755 def make_filename(arg):
1765 def make_filename(arg):
1756 "Make a filename from the given args"
1766 "Make a filename from the given args"
1757 try:
1767 try:
1758 filename = get_py_filename(arg)
1768 filename = get_py_filename(arg)
1759 except IOError:
1769 except IOError:
1760 if args.endswith('.py'):
1770 if args.endswith('.py'):
1761 filename = arg
1771 filename = arg
1762 else:
1772 else:
1763 filename = None
1773 filename = None
1764 return filename
1774 return filename
1765
1775
1766 # custom exceptions
1776 # custom exceptions
1767 class DataIsObject(Exception): pass
1777 class DataIsObject(Exception): pass
1768
1778
1769 opts,args = self.parse_options(parameter_s,'px')
1779 opts,args = self.parse_options(parameter_s,'px')
1770
1780
1771 # Default line number value
1781 # Default line number value
1772 lineno = None
1782 lineno = None
1773 if opts.has_key('p'):
1783 if opts.has_key('p'):
1774 args = '_%s' % last_call[0]
1784 args = '_%s' % last_call[0]
1775 if not self.shell.user_ns.has_key(args):
1785 if not self.shell.user_ns.has_key(args):
1776 args = last_call[1]
1786 args = last_call[1]
1777
1787
1778 # use last_call to remember the state of the previous call, but don't
1788 # use last_call to remember the state of the previous call, but don't
1779 # let it be clobbered by successive '-p' calls.
1789 # let it be clobbered by successive '-p' calls.
1780 try:
1790 try:
1781 last_call[0] = self.shell.outputcache.prompt_count
1791 last_call[0] = self.shell.outputcache.prompt_count
1782 if not opts.has_key('p'):
1792 if not opts.has_key('p'):
1783 last_call[1] = parameter_s
1793 last_call[1] = parameter_s
1784 except:
1794 except:
1785 pass
1795 pass
1786
1796
1787 # by default this is done with temp files, except when the given
1797 # by default this is done with temp files, except when the given
1788 # arg is a filename
1798 # arg is a filename
1789 use_temp = 1
1799 use_temp = 1
1790
1800
1791 if re.match(r'\d',args):
1801 if re.match(r'\d',args):
1792 # Mode where user specifies ranges of lines, like in %macro.
1802 # Mode where user specifies ranges of lines, like in %macro.
1793 # This means that you can't edit files whose names begin with
1803 # This means that you can't edit files whose names begin with
1794 # numbers this way. Tough.
1804 # numbers this way. Tough.
1795 ranges = args.split()
1805 ranges = args.split()
1796 data = ''.join(self.extract_input_slices(ranges))
1806 data = ''.join(self.extract_input_slices(ranges))
1797 elif args.endswith('.py'):
1807 elif args.endswith('.py'):
1798 filename = make_filename(args)
1808 filename = make_filename(args)
1799 data = ''
1809 data = ''
1800 use_temp = 0
1810 use_temp = 0
1801 elif args:
1811 elif args:
1802 try:
1812 try:
1803 # Load the parameter given as a variable. If not a string,
1813 # Load the parameter given as a variable. If not a string,
1804 # process it as an object instead (below)
1814 # process it as an object instead (below)
1805
1815
1806 #print '*** args',args,'type',type(args) # dbg
1816 #print '*** args',args,'type',type(args) # dbg
1807 data = eval(args,self.shell.user_ns)
1817 data = eval(args,self.shell.user_ns)
1808 if not type(data) in StringTypes:
1818 if not type(data) in StringTypes:
1809 raise DataIsObject
1819 raise DataIsObject
1810 except (NameError,SyntaxError):
1820 except (NameError,SyntaxError):
1811 # given argument is not a variable, try as a filename
1821 # given argument is not a variable, try as a filename
1812 filename = make_filename(args)
1822 filename = make_filename(args)
1813 if filename is None:
1823 if filename is None:
1814 warn("Argument given (%s) can't be found as a variable "
1824 warn("Argument given (%s) can't be found as a variable "
1815 "or as a filename." % args)
1825 "or as a filename." % args)
1816 return
1826 return
1817 data = ''
1827 data = ''
1818 use_temp = 0
1828 use_temp = 0
1819 except DataIsObject:
1829 except DataIsObject:
1820 # For objects, try to edit the file where they are defined
1830 # For objects, try to edit the file where they are defined
1821 try:
1831 try:
1822 filename = inspect.getabsfile(data)
1832 filename = inspect.getabsfile(data)
1823 datafile = 1
1833 datafile = 1
1824 except TypeError:
1834 except TypeError:
1825 filename = make_filename(args)
1835 filename = make_filename(args)
1826 datafile = 1
1836 datafile = 1
1827 warn('Could not find file where `%s` is defined.\n'
1837 warn('Could not find file where `%s` is defined.\n'
1828 'Opening a file named `%s`' % (args,filename))
1838 'Opening a file named `%s`' % (args,filename))
1829 # Now, make sure we can actually read the source (if it was in
1839 # Now, make sure we can actually read the source (if it was in
1830 # a temp file it's gone by now).
1840 # a temp file it's gone by now).
1831 if datafile:
1841 if datafile:
1832 try:
1842 try:
1833 lineno = inspect.getsourcelines(data)[1]
1843 lineno = inspect.getsourcelines(data)[1]
1834 except IOError:
1844 except IOError:
1835 filename = make_filename(args)
1845 filename = make_filename(args)
1836 if filename is None:
1846 if filename is None:
1837 warn('The file `%s` where `%s` was defined cannot '
1847 warn('The file `%s` where `%s` was defined cannot '
1838 'be read.' % (filename,data))
1848 'be read.' % (filename,data))
1839 return
1849 return
1840 use_temp = 0
1850 use_temp = 0
1841 else:
1851 else:
1842 data = ''
1852 data = ''
1843
1853
1844 if use_temp:
1854 if use_temp:
1845 filename = tempfile.mktemp('.py')
1855 filename = tempfile.mktemp('.py')
1846 self.shell.tempfiles.append(filename)
1856 self.shell.tempfiles.append(filename)
1847
1857
1848 if data and use_temp:
1858 if data and use_temp:
1849 tmp_file = open(filename,'w')
1859 tmp_file = open(filename,'w')
1850 tmp_file.write(data)
1860 tmp_file.write(data)
1851 tmp_file.close()
1861 tmp_file.close()
1852
1862
1853 # do actual editing here
1863 # do actual editing here
1854 print 'Editing...',
1864 print 'Editing...',
1855 sys.stdout.flush()
1865 sys.stdout.flush()
1856 self.shell.hooks.editor(filename,lineno)
1866 self.shell.hooks.editor(filename,lineno)
1857 if opts.has_key('x'): # -x prevents actual execution
1867 if opts.has_key('x'): # -x prevents actual execution
1858 print
1868 print
1859 else:
1869 else:
1860 print 'done. Executing edited code...'
1870 print 'done. Executing edited code...'
1861 try:
1871 try:
1862 execfile(filename,self.shell.user_ns)
1872 execfile(filename,self.shell.user_ns)
1863 except IOError,msg:
1873 except IOError,msg:
1864 if msg.filename == filename:
1874 if msg.filename == filename:
1865 warn('File not found. Did you forget to save?')
1875 warn('File not found. Did you forget to save?')
1866 return
1876 return
1867 else:
1877 else:
1868 self.shell.showtraceback()
1878 self.shell.showtraceback()
1869 except:
1879 except:
1870 self.shell.showtraceback()
1880 self.shell.showtraceback()
1871 if use_temp:
1881 if use_temp:
1872 contents = open(filename).read()
1882 contents = open(filename).read()
1873 return contents
1883 return contents
1874
1884
1875 def magic_xmode(self,parameter_s = ''):
1885 def magic_xmode(self,parameter_s = ''):
1876 """Switch modes for the exception handlers.
1886 """Switch modes for the exception handlers.
1877
1887
1878 Valid modes: Plain, Context and Verbose.
1888 Valid modes: Plain, Context and Verbose.
1879
1889
1880 If called without arguments, acts as a toggle."""
1890 If called without arguments, acts as a toggle."""
1881
1891
1882 new_mode = parameter_s.strip().capitalize()
1892 new_mode = parameter_s.strip().capitalize()
1883 try:
1893 try:
1884 self.InteractiveTB.set_mode(mode = new_mode)
1894 self.InteractiveTB.set_mode(mode = new_mode)
1885 print 'Exception reporting mode:',self.InteractiveTB.mode
1895 print 'Exception reporting mode:',self.InteractiveTB.mode
1886 except:
1896 except:
1887 warn('Error changing exception modes.\n' + str(sys.exc_info()[1]))
1897 warn('Error changing exception modes.\n' + str(sys.exc_info()[1]))
1888
1898
1889 def magic_colors(self,parameter_s = ''):
1899 def magic_colors(self,parameter_s = ''):
1890 """Switch color scheme for prompts, info system and exception handlers.
1900 """Switch color scheme for prompts, info system and exception handlers.
1891
1901
1892 Currently implemented schemes: NoColor, Linux, LightBG.
1902 Currently implemented schemes: NoColor, Linux, LightBG.
1893
1903
1894 Color scheme names are not case-sensitive."""
1904 Color scheme names are not case-sensitive."""
1895
1905
1896 new_scheme = parameter_s.strip()
1906 new_scheme = parameter_s.strip()
1897 if not new_scheme:
1907 if not new_scheme:
1898 print 'You must specify a color scheme.'
1908 print 'You must specify a color scheme.'
1899 return
1909 return
1900 # Under Windows, check for Gary Bishop's readline, which is necessary
1910 # Under Windows, check for Gary Bishop's readline, which is necessary
1901 # for ANSI coloring
1911 # for ANSI coloring
1902 if os.name in ['nt','dos']:
1912 if os.name in ['nt','dos']:
1903 try:
1913 try:
1904 import readline
1914 import readline
1905 except ImportError:
1915 except ImportError:
1906 has_readline = 0
1916 has_readline = 0
1907 else:
1917 else:
1908 try:
1918 try:
1909 readline.GetOutputFile()
1919 readline.GetOutputFile()
1910 except AttributeError:
1920 except AttributeError:
1911 has_readline = 0
1921 has_readline = 0
1912 else:
1922 else:
1913 has_readline = 1
1923 has_readline = 1
1914 if not has_readline:
1924 if not has_readline:
1915 msg = """\
1925 msg = """\
1916 Proper color support under MS Windows requires Gary Bishop's readline library.
1926 Proper color support under MS Windows requires Gary Bishop's readline library.
1917 You can find it at:
1927 You can find it at:
1918 http://sourceforge.net/projects/uncpythontools
1928 http://sourceforge.net/projects/uncpythontools
1919 Gary's readline needs the ctypes module, from:
1929 Gary's readline needs the ctypes module, from:
1920 http://starship.python.net/crew/theller/ctypes
1930 http://starship.python.net/crew/theller/ctypes
1921
1931
1922 Defaulting color scheme to 'NoColor'"""
1932 Defaulting color scheme to 'NoColor'"""
1923 new_scheme = 'NoColor'
1933 new_scheme = 'NoColor'
1924 warn(msg)
1934 warn(msg)
1925
1935
1926 # Set prompt colors
1936 # Set prompt colors
1927 try:
1937 try:
1928 self.shell.outputcache.set_colors(new_scheme)
1938 self.shell.outputcache.set_colors(new_scheme)
1929 except:
1939 except:
1930 warn('Error changing prompt color schemes.\n'
1940 warn('Error changing prompt color schemes.\n'
1931 + str(sys.exc_info()[1]))
1941 + str(sys.exc_info()[1]))
1932 else:
1942 else:
1933 self.shell.rc.colors = \
1943 self.shell.rc.colors = \
1934 self.shell.outputcache.color_table.active_scheme_name
1944 self.shell.outputcache.color_table.active_scheme_name
1935 # Set exception colors
1945 # Set exception colors
1936 try:
1946 try:
1937 self.shell.InteractiveTB.set_colors(scheme = new_scheme)
1947 self.shell.InteractiveTB.set_colors(scheme = new_scheme)
1938 self.shell.SyntaxTB.set_colors(scheme = new_scheme)
1948 self.shell.SyntaxTB.set_colors(scheme = new_scheme)
1939 except:
1949 except:
1940 warn('Error changing exception color schemes.\n'
1950 warn('Error changing exception color schemes.\n'
1941 + str(sys.exc_info()[1]))
1951 + str(sys.exc_info()[1]))
1942 # Set info (for 'object?') colors
1952 # Set info (for 'object?') colors
1943 if self.shell.rc.color_info:
1953 if self.shell.rc.color_info:
1944 try:
1954 try:
1945 self.shell.inspector.set_active_scheme(new_scheme)
1955 self.shell.inspector.set_active_scheme(new_scheme)
1946 except:
1956 except:
1947 warn('Error changing object inspector color schemes.\n'
1957 warn('Error changing object inspector color schemes.\n'
1948 + str(sys.exc_info()[1]))
1958 + str(sys.exc_info()[1]))
1949 else:
1959 else:
1950 self.shell.inspector.set_active_scheme('NoColor')
1960 self.shell.inspector.set_active_scheme('NoColor')
1951
1961
1952 def magic_color_info(self,parameter_s = ''):
1962 def magic_color_info(self,parameter_s = ''):
1953 """Toggle color_info.
1963 """Toggle color_info.
1954
1964
1955 The color_info configuration parameter controls whether colors are
1965 The color_info configuration parameter controls whether colors are
1956 used for displaying object details (by things like %psource, %pfile or
1966 used for displaying object details (by things like %psource, %pfile or
1957 the '?' system). This function toggles this value with each call.
1967 the '?' system). This function toggles this value with each call.
1958
1968
1959 Note that unless you have a fairly recent pager (less works better
1969 Note that unless you have a fairly recent pager (less works better
1960 than more) in your system, using colored object information displays
1970 than more) in your system, using colored object information displays
1961 will not work properly. Test it and see."""
1971 will not work properly. Test it and see."""
1962
1972
1963 self.shell.rc.color_info = 1 - self.shell.rc.color_info
1973 self.shell.rc.color_info = 1 - self.shell.rc.color_info
1964 self.magic_colors(self.shell.rc.colors)
1974 self.magic_colors(self.shell.rc.colors)
1965 print 'Object introspection functions have now coloring:',
1975 print 'Object introspection functions have now coloring:',
1966 print ['OFF','ON'][self.shell.rc.color_info]
1976 print ['OFF','ON'][self.shell.rc.color_info]
1967
1977
1968 def magic_Pprint(self, parameter_s=''):
1978 def magic_Pprint(self, parameter_s=''):
1969 """Toggle pretty printing on/off."""
1979 """Toggle pretty printing on/off."""
1970
1980
1971 self.shell.outputcache.Pprint = 1 - self.shell.outputcache.Pprint
1981 self.shell.outputcache.Pprint = 1 - self.shell.outputcache.Pprint
1972 print 'Pretty printing has been turned', \
1982 print 'Pretty printing has been turned', \
1973 ['OFF','ON'][self.shell.outputcache.Pprint]
1983 ['OFF','ON'][self.shell.outputcache.Pprint]
1974
1984
1975 def magic_Exit(self, parameter_s=''):
1985 def magic_Exit(self, parameter_s=''):
1976 """Exit IPython without confirmation."""
1986 """Exit IPython without confirmation."""
1977
1987
1978 self.shell.exit_now = True
1988 self.shell.exit_now = True
1979
1989
1980 def magic_Quit(self, parameter_s=''):
1990 def magic_Quit(self, parameter_s=''):
1981 """Exit IPython without confirmation (like %Exit)."""
1991 """Exit IPython without confirmation (like %Exit)."""
1982
1992
1983 self.shell.exit_now = True
1993 self.shell.exit_now = True
1984
1994
1985 #......................................................................
1995 #......................................................................
1986 # Functions to implement unix shell-type things
1996 # Functions to implement unix shell-type things
1987
1997
1988 def magic_alias(self, parameter_s = ''):
1998 def magic_alias(self, parameter_s = ''):
1989 """Define an alias for a system command.
1999 """Define an alias for a system command.
1990
2000
1991 '%alias alias_name cmd' defines 'alias_name' as an alias for 'cmd'
2001 '%alias alias_name cmd' defines 'alias_name' as an alias for 'cmd'
1992
2002
1993 Then, typing 'alias_name params' will execute the system command 'cmd
2003 Then, typing 'alias_name params' will execute the system command 'cmd
1994 params' (from your underlying operating system).
2004 params' (from your underlying operating system).
1995
2005
1996 Aliases have lower precedence than magic functions and Python normal
2006 Aliases have lower precedence than magic functions and Python normal
1997 variables, so if 'foo' is both a Python variable and an alias, the
2007 variables, so if 'foo' is both a Python variable and an alias, the
1998 alias can not be executed until 'del foo' removes the Python variable.
2008 alias can not be executed until 'del foo' removes the Python variable.
1999
2009
2000 You can use the %l specifier in an alias definition to represent the
2010 You can use the %l specifier in an alias definition to represent the
2001 whole line when the alias is called. For example:
2011 whole line when the alias is called. For example:
2002
2012
2003 In [2]: alias all echo "Input in brackets: <%l>"\\
2013 In [2]: alias all echo "Input in brackets: <%l>"\\
2004 In [3]: all hello world\\
2014 In [3]: all hello world\\
2005 Input in brackets: <hello world>
2015 Input in brackets: <hello world>
2006
2016
2007 You can also define aliases with parameters using %s specifiers (one
2017 You can also define aliases with parameters using %s specifiers (one
2008 per parameter):
2018 per parameter):
2009
2019
2010 In [1]: alias parts echo first %s second %s\\
2020 In [1]: alias parts echo first %s second %s\\
2011 In [2]: %parts A B\\
2021 In [2]: %parts A B\\
2012 first A second B\\
2022 first A second B\\
2013 In [3]: %parts A\\
2023 In [3]: %parts A\\
2014 Incorrect number of arguments: 2 expected.\\
2024 Incorrect number of arguments: 2 expected.\\
2015 parts is an alias to: 'echo first %s second %s'
2025 parts is an alias to: 'echo first %s second %s'
2016
2026
2017 Note that %l and %s are mutually exclusive. You can only use one or
2027 Note that %l and %s are mutually exclusive. You can only use one or
2018 the other in your aliases.
2028 the other in your aliases.
2019
2029
2020 Aliases expand Python variables just like system calls using ! or !!
2030 Aliases expand Python variables just like system calls using ! or !!
2021 do: all expressions prefixed with '$' get expanded. For details of
2031 do: all expressions prefixed with '$' get expanded. For details of
2022 the semantic rules, see PEP-215:
2032 the semantic rules, see PEP-215:
2023 http://www.python.org/peps/pep-0215.html. This is the library used by
2033 http://www.python.org/peps/pep-0215.html. This is the library used by
2024 IPython for variable expansion. If you want to access a true shell
2034 IPython for variable expansion. If you want to access a true shell
2025 variable, an extra $ is necessary to prevent its expansion by IPython:
2035 variable, an extra $ is necessary to prevent its expansion by IPython:
2026
2036
2027 In [6]: alias show echo\\
2037 In [6]: alias show echo\\
2028 In [7]: PATH='A Python string'\\
2038 In [7]: PATH='A Python string'\\
2029 In [8]: show $PATH\\
2039 In [8]: show $PATH\\
2030 A Python string\\
2040 A Python string\\
2031 In [9]: show $$PATH\\
2041 In [9]: show $$PATH\\
2032 /usr/local/lf9560/bin:/usr/local/intel/compiler70/ia32/bin:...
2042 /usr/local/lf9560/bin:/usr/local/intel/compiler70/ia32/bin:...
2033
2043
2034 You can use the alias facility to acess all of $PATH. See the %rehash
2044 You can use the alias facility to acess all of $PATH. See the %rehash
2035 and %rehashx functions, which automatically create aliases for the
2045 and %rehashx functions, which automatically create aliases for the
2036 contents of your $PATH.
2046 contents of your $PATH.
2037
2047
2038 If called with no parameters, %alias prints the current alias table."""
2048 If called with no parameters, %alias prints the current alias table."""
2039
2049
2040 par = parameter_s.strip()
2050 par = parameter_s.strip()
2041 if not par:
2051 if not par:
2042 if self.shell.rc.automagic:
2052 if self.shell.rc.automagic:
2043 prechar = ''
2053 prechar = ''
2044 else:
2054 else:
2045 prechar = self.shell.ESC_MAGIC
2055 prechar = self.shell.ESC_MAGIC
2046 print 'Alias\t\tSystem Command\n'+'-'*30
2056 print 'Alias\t\tSystem Command\n'+'-'*30
2047 atab = self.shell.alias_table
2057 atab = self.shell.alias_table
2048 aliases = atab.keys()
2058 aliases = atab.keys()
2049 aliases.sort()
2059 aliases.sort()
2050 for alias in aliases:
2060 for alias in aliases:
2051 print prechar+alias+'\t\t'+atab[alias][1]
2061 print prechar+alias+'\t\t'+atab[alias][1]
2052 print '-'*30+'\nTotal number of aliases:',len(aliases)
2062 print '-'*30+'\nTotal number of aliases:',len(aliases)
2053 return
2063 return
2054 try:
2064 try:
2055 alias,cmd = par.split(None,1)
2065 alias,cmd = par.split(None,1)
2056 except:
2066 except:
2057 print OInspect.getdoc(self.magic_alias)
2067 print OInspect.getdoc(self.magic_alias)
2058 else:
2068 else:
2059 nargs = cmd.count('%s')
2069 nargs = cmd.count('%s')
2060 if nargs>0 and cmd.find('%l')>=0:
2070 if nargs>0 and cmd.find('%l')>=0:
2061 error('The %s and %l specifiers are mutually exclusive '
2071 error('The %s and %l specifiers are mutually exclusive '
2062 'in alias definitions.')
2072 'in alias definitions.')
2063 else: # all looks OK
2073 else: # all looks OK
2064 self.shell.alias_table[alias] = (nargs,cmd)
2074 self.shell.alias_table[alias] = (nargs,cmd)
2065 self.shell.alias_table_validate(verbose=1)
2075 self.shell.alias_table_validate(verbose=1)
2066 # end magic_alias
2076 # end magic_alias
2067
2077
2068 def magic_unalias(self, parameter_s = ''):
2078 def magic_unalias(self, parameter_s = ''):
2069 """Remove an alias"""
2079 """Remove an alias"""
2070
2080
2071 aname = parameter_s.strip()
2081 aname = parameter_s.strip()
2072 if aname in self.shell.alias_table:
2082 if aname in self.shell.alias_table:
2073 del self.shell.alias_table[aname]
2083 del self.shell.alias_table[aname]
2074
2084
2075 def magic_rehash(self, parameter_s = ''):
2085 def magic_rehash(self, parameter_s = ''):
2076 """Update the alias table with all entries in $PATH.
2086 """Update the alias table with all entries in $PATH.
2077
2087
2078 This version does no checks on execute permissions or whether the
2088 This version does no checks on execute permissions or whether the
2079 contents of $PATH are truly files (instead of directories or something
2089 contents of $PATH are truly files (instead of directories or something
2080 else). For such a safer (but slower) version, use %rehashx."""
2090 else). For such a safer (but slower) version, use %rehashx."""
2081
2091
2082 # This function (and rehashx) manipulate the alias_table directly
2092 # This function (and rehashx) manipulate the alias_table directly
2083 # rather than calling magic_alias, for speed reasons. A rehash on a
2093 # rather than calling magic_alias, for speed reasons. A rehash on a
2084 # typical Linux box involves several thousand entries, so efficiency
2094 # typical Linux box involves several thousand entries, so efficiency
2085 # here is a top concern.
2095 # here is a top concern.
2086
2096
2087 path = filter(os.path.isdir,os.environ['PATH'].split(os.pathsep))
2097 path = filter(os.path.isdir,os.environ['PATH'].split(os.pathsep))
2088 alias_table = self.shell.alias_table
2098 alias_table = self.shell.alias_table
2089 for pdir in path:
2099 for pdir in path:
2090 for ff in os.listdir(pdir):
2100 for ff in os.listdir(pdir):
2091 # each entry in the alias table must be (N,name), where
2101 # each entry in the alias table must be (N,name), where
2092 # N is the number of positional arguments of the alias.
2102 # N is the number of positional arguments of the alias.
2093 alias_table[ff] = (0,ff)
2103 alias_table[ff] = (0,ff)
2094 # Make sure the alias table doesn't contain keywords or builtins
2104 # Make sure the alias table doesn't contain keywords or builtins
2095 self.shell.alias_table_validate()
2105 self.shell.alias_table_validate()
2096 # Call again init_auto_alias() so we get 'rm -i' and other modified
2106 # Call again init_auto_alias() so we get 'rm -i' and other modified
2097 # aliases since %rehash will probably clobber them
2107 # aliases since %rehash will probably clobber them
2098 self.shell.init_auto_alias()
2108 self.shell.init_auto_alias()
2099
2109
2100 def magic_rehashx(self, parameter_s = ''):
2110 def magic_rehashx(self, parameter_s = ''):
2101 """Update the alias table with all executable files in $PATH.
2111 """Update the alias table with all executable files in $PATH.
2102
2112
2103 This version explicitly checks that every entry in $PATH is a file
2113 This version explicitly checks that every entry in $PATH is a file
2104 with execute access (os.X_OK), so it is much slower than %rehash.
2114 with execute access (os.X_OK), so it is much slower than %rehash.
2105
2115
2106 Under Windows, it checks executability as a match agains a
2116 Under Windows, it checks executability as a match agains a
2107 '|'-separated string of extensions, stored in the IPython config
2117 '|'-separated string of extensions, stored in the IPython config
2108 variable win_exec_ext. This defaults to 'exe|com|bat'. """
2118 variable win_exec_ext. This defaults to 'exe|com|bat'. """
2109
2119
2110 path = filter(os.path.isdir,os.environ['PATH'].split(os.pathsep))
2120 path = filter(os.path.isdir,os.environ['PATH'].split(os.pathsep))
2111 alias_table = self.shell.alias_table
2121 alias_table = self.shell.alias_table
2112
2122
2113 if os.name == 'posix':
2123 if os.name == 'posix':
2114 isexec = lambda fname:os.path.isfile(fname) and \
2124 isexec = lambda fname:os.path.isfile(fname) and \
2115 os.access(fname,os.X_OK)
2125 os.access(fname,os.X_OK)
2116 else:
2126 else:
2117
2127
2118 try:
2128 try:
2119 winext = os.environ['pathext'].replace(';','|').replace('.','')
2129 winext = os.environ['pathext'].replace(';','|').replace('.','')
2120 except KeyError:
2130 except KeyError:
2121 winext = 'exe|com|bat'
2131 winext = 'exe|com|bat'
2122
2132
2123 execre = re.compile(r'(.*)\.(%s)$' % winext,re.IGNORECASE)
2133 execre = re.compile(r'(.*)\.(%s)$' % winext,re.IGNORECASE)
2124 isexec = lambda fname:os.path.isfile(fname) and execre.match(fname)
2134 isexec = lambda fname:os.path.isfile(fname) and execre.match(fname)
2125 savedir = os.getcwd()
2135 savedir = os.getcwd()
2126 try:
2136 try:
2127 # write the whole loop for posix/Windows so we don't have an if in
2137 # write the whole loop for posix/Windows so we don't have an if in
2128 # the innermost part
2138 # the innermost part
2129 if os.name == 'posix':
2139 if os.name == 'posix':
2130 for pdir in path:
2140 for pdir in path:
2131 os.chdir(pdir)
2141 os.chdir(pdir)
2132 for ff in os.listdir(pdir):
2142 for ff in os.listdir(pdir):
2133 if isexec(ff):
2143 if isexec(ff):
2134 # each entry in the alias table must be (N,name),
2144 # each entry in the alias table must be (N,name),
2135 # where N is the number of positional arguments of the
2145 # where N is the number of positional arguments of the
2136 # alias.
2146 # alias.
2137 alias_table[ff] = (0,ff)
2147 alias_table[ff] = (0,ff)
2138 else:
2148 else:
2139 for pdir in path:
2149 for pdir in path:
2140 os.chdir(pdir)
2150 os.chdir(pdir)
2141 for ff in os.listdir(pdir):
2151 for ff in os.listdir(pdir):
2142 if isexec(ff):
2152 if isexec(ff):
2143 alias_table[execre.sub(r'\1',ff)] = (0,ff)
2153 alias_table[execre.sub(r'\1',ff)] = (0,ff)
2144 # Make sure the alias table doesn't contain keywords or builtins
2154 # Make sure the alias table doesn't contain keywords or builtins
2145 self.shell.alias_table_validate()
2155 self.shell.alias_table_validate()
2146 # Call again init_auto_alias() so we get 'rm -i' and other
2156 # Call again init_auto_alias() so we get 'rm -i' and other
2147 # modified aliases since %rehashx will probably clobber them
2157 # modified aliases since %rehashx will probably clobber them
2148 self.shell.init_auto_alias()
2158 self.shell.init_auto_alias()
2149 finally:
2159 finally:
2150 os.chdir(savedir)
2160 os.chdir(savedir)
2151
2161
2152 def magic_pwd(self, parameter_s = ''):
2162 def magic_pwd(self, parameter_s = ''):
2153 """Return the current working directory path."""
2163 """Return the current working directory path."""
2154 return os.getcwd()
2164 return os.getcwd()
2155
2165
2156 def magic_cd(self, parameter_s=''):
2166 def magic_cd(self, parameter_s=''):
2157 """Change the current working directory.
2167 """Change the current working directory.
2158
2168
2159 This command automatically maintains an internal list of directories
2169 This command automatically maintains an internal list of directories
2160 you visit during your IPython session, in the variable _dh. The
2170 you visit during your IPython session, in the variable _dh. The
2161 command %dhist shows this history nicely formatted.
2171 command %dhist shows this history nicely formatted.
2162
2172
2163 Usage:
2173 Usage:
2164
2174
2165 cd 'dir': changes to directory 'dir'.
2175 cd 'dir': changes to directory 'dir'.
2166
2176
2167 cd -: changes to the last visited directory.
2177 cd -: changes to the last visited directory.
2168
2178
2169 cd -<n>: changes to the n-th directory in the directory history.
2179 cd -<n>: changes to the n-th directory in the directory history.
2170
2180
2171 cd -b <bookmark_name>: jump to a bookmark set by %bookmark
2181 cd -b <bookmark_name>: jump to a bookmark set by %bookmark
2172 (note: cd <bookmark_name> is enough if there is no
2182 (note: cd <bookmark_name> is enough if there is no
2173 directory <bookmark_name>, but a bookmark with the name exists.)
2183 directory <bookmark_name>, but a bookmark with the name exists.)
2174
2184
2175 Options:
2185 Options:
2176
2186
2177 -q: quiet. Do not print the working directory after the cd command is
2187 -q: quiet. Do not print the working directory after the cd command is
2178 executed. By default IPython's cd command does print this directory,
2188 executed. By default IPython's cd command does print this directory,
2179 since the default prompts do not display path information.
2189 since the default prompts do not display path information.
2180
2190
2181 Note that !cd doesn't work for this purpose because the shell where
2191 Note that !cd doesn't work for this purpose because the shell where
2182 !command runs is immediately discarded after executing 'command'."""
2192 !command runs is immediately discarded after executing 'command'."""
2183
2193
2184 parameter_s = parameter_s.strip()
2194 parameter_s = parameter_s.strip()
2185 bkms = self.shell.persist.get("bookmarks",{})
2195 bkms = self.shell.persist.get("bookmarks",{})
2186
2196
2187 numcd = re.match(r'(-)(\d+)$',parameter_s)
2197 numcd = re.match(r'(-)(\d+)$',parameter_s)
2188 # jump in directory history by number
2198 # jump in directory history by number
2189 if numcd:
2199 if numcd:
2190 nn = int(numcd.group(2))
2200 nn = int(numcd.group(2))
2191 try:
2201 try:
2192 ps = self.shell.user_ns['_dh'][nn]
2202 ps = self.shell.user_ns['_dh'][nn]
2193 except IndexError:
2203 except IndexError:
2194 print 'The requested directory does not exist in history.'
2204 print 'The requested directory does not exist in history.'
2195 return
2205 return
2196 else:
2206 else:
2197 opts = {}
2207 opts = {}
2198 else:
2208 else:
2199 opts,ps = self.parse_options(parameter_s,'qb',mode='string')
2209 opts,ps = self.parse_options(parameter_s,'qb',mode='string')
2200 # jump to previous
2210 # jump to previous
2201 if ps == '-':
2211 if ps == '-':
2202 try:
2212 try:
2203 ps = self.shell.user_ns['_dh'][-2]
2213 ps = self.shell.user_ns['_dh'][-2]
2204 except IndexError:
2214 except IndexError:
2205 print 'No previous directory to change to.'
2215 print 'No previous directory to change to.'
2206 return
2216 return
2207 # jump to bookmark
2217 # jump to bookmark
2208 elif opts.has_key('b') or (bkms.has_key(ps) and not os.path.isdir(ps)):
2218 elif opts.has_key('b') or (bkms.has_key(ps) and not os.path.isdir(ps)):
2209 if bkms.has_key(ps):
2219 if bkms.has_key(ps):
2210 target = bkms[ps]
2220 target = bkms[ps]
2211 print '(bookmark:%s) -> %s' % (ps,target)
2221 print '(bookmark:%s) -> %s' % (ps,target)
2212 ps = target
2222 ps = target
2213 else:
2223 else:
2214 if bkms:
2224 if bkms:
2215 error("Bookmark '%s' not found. "
2225 error("Bookmark '%s' not found. "
2216 "Use '%bookmark -l' to see your bookmarks." % ps)
2226 "Use '%bookmark -l' to see your bookmarks." % ps)
2217 else:
2227 else:
2218 print "Bookmarks not set - use %bookmark <bookmarkname>"
2228 print "Bookmarks not set - use %bookmark <bookmarkname>"
2219 return
2229 return
2220
2230
2221 # at this point ps should point to the target dir
2231 # at this point ps should point to the target dir
2222 if ps:
2232 if ps:
2223 try:
2233 try:
2224 os.chdir(os.path.expanduser(ps))
2234 os.chdir(os.path.expanduser(ps))
2225 except OSError:
2235 except OSError:
2226 print sys.exc_info()[1]
2236 print sys.exc_info()[1]
2227 else:
2237 else:
2228 self.shell.user_ns['_dh'].append(os.getcwd())
2238 self.shell.user_ns['_dh'].append(os.getcwd())
2229 else:
2239 else:
2230 os.chdir(self.home_dir)
2240 os.chdir(self.home_dir)
2231 self.shell.user_ns['_dh'].append(os.getcwd())
2241 self.shell.user_ns['_dh'].append(os.getcwd())
2232 if not 'q' in opts:
2242 if not 'q' in opts:
2233 print self.shell.user_ns['_dh'][-1]
2243 print self.shell.user_ns['_dh'][-1]
2234
2244
2235 def magic_dhist(self, parameter_s=''):
2245 def magic_dhist(self, parameter_s=''):
2236 """Print your history of visited directories.
2246 """Print your history of visited directories.
2237
2247
2238 %dhist -> print full history\\
2248 %dhist -> print full history\\
2239 %dhist n -> print last n entries only\\
2249 %dhist n -> print last n entries only\\
2240 %dhist n1 n2 -> print entries between n1 and n2 (n1 not included)\\
2250 %dhist n1 n2 -> print entries between n1 and n2 (n1 not included)\\
2241
2251
2242 This history is automatically maintained by the %cd command, and
2252 This history is automatically maintained by the %cd command, and
2243 always available as the global list variable _dh. You can use %cd -<n>
2253 always available as the global list variable _dh. You can use %cd -<n>
2244 to go to directory number <n>."""
2254 to go to directory number <n>."""
2245
2255
2246 dh = self.shell.user_ns['_dh']
2256 dh = self.shell.user_ns['_dh']
2247 if parameter_s:
2257 if parameter_s:
2248 try:
2258 try:
2249 args = map(int,parameter_s.split())
2259 args = map(int,parameter_s.split())
2250 except:
2260 except:
2251 self.arg_err(Magic.magic_dhist)
2261 self.arg_err(Magic.magic_dhist)
2252 return
2262 return
2253 if len(args) == 1:
2263 if len(args) == 1:
2254 ini,fin = max(len(dh)-(args[0]),0),len(dh)
2264 ini,fin = max(len(dh)-(args[0]),0),len(dh)
2255 elif len(args) == 2:
2265 elif len(args) == 2:
2256 ini,fin = args
2266 ini,fin = args
2257 else:
2267 else:
2258 self.arg_err(Magic.magic_dhist)
2268 self.arg_err(Magic.magic_dhist)
2259 return
2269 return
2260 else:
2270 else:
2261 ini,fin = 0,len(dh)
2271 ini,fin = 0,len(dh)
2262 nlprint(dh,
2272 nlprint(dh,
2263 header = 'Directory history (kept in _dh)',
2273 header = 'Directory history (kept in _dh)',
2264 start=ini,stop=fin)
2274 start=ini,stop=fin)
2265
2275
2266 def magic_env(self, parameter_s=''):
2276 def magic_env(self, parameter_s=''):
2267 """List environment variables."""
2277 """List environment variables."""
2268
2278
2269 # environ is an instance of UserDict
2279 # environ is an instance of UserDict
2270 return os.environ.data
2280 return os.environ.data
2271
2281
2272 def magic_pushd(self, parameter_s=''):
2282 def magic_pushd(self, parameter_s=''):
2273 """Place the current dir on stack and change directory.
2283 """Place the current dir on stack and change directory.
2274
2284
2275 Usage:\\
2285 Usage:\\
2276 %pushd ['dirname']
2286 %pushd ['dirname']
2277
2287
2278 %pushd with no arguments does a %pushd to your home directory.
2288 %pushd with no arguments does a %pushd to your home directory.
2279 """
2289 """
2280 if parameter_s == '': parameter_s = '~'
2290 if parameter_s == '': parameter_s = '~'
2281 if len(self.dir_stack)>0 and os.path.expanduser(parameter_s) != \
2291 if len(self.dir_stack)>0 and os.path.expanduser(parameter_s) != \
2282 os.path.expanduser(self.dir_stack[0]):
2292 os.path.expanduser(self.dir_stack[0]):
2283 try:
2293 try:
2284 self.magic_cd(parameter_s)
2294 self.magic_cd(parameter_s)
2285 self.dir_stack.insert(0,os.getcwd().replace(self.home_dir,'~'))
2295 self.dir_stack.insert(0,os.getcwd().replace(self.home_dir,'~'))
2286 self.magic_dirs()
2296 self.magic_dirs()
2287 except:
2297 except:
2288 print 'Invalid directory'
2298 print 'Invalid directory'
2289 else:
2299 else:
2290 print 'You are already there!'
2300 print 'You are already there!'
2291
2301
2292 def magic_popd(self, parameter_s=''):
2302 def magic_popd(self, parameter_s=''):
2293 """Change to directory popped off the top of the stack.
2303 """Change to directory popped off the top of the stack.
2294 """
2304 """
2295 if len (self.dir_stack) > 1:
2305 if len (self.dir_stack) > 1:
2296 self.dir_stack.pop(0)
2306 self.dir_stack.pop(0)
2297 self.magic_cd(self.dir_stack[0])
2307 self.magic_cd(self.dir_stack[0])
2298 print self.dir_stack[0]
2308 print self.dir_stack[0]
2299 else:
2309 else:
2300 print "You can't remove the starting directory from the stack:",\
2310 print "You can't remove the starting directory from the stack:",\
2301 self.dir_stack
2311 self.dir_stack
2302
2312
2303 def magic_dirs(self, parameter_s=''):
2313 def magic_dirs(self, parameter_s=''):
2304 """Return the current directory stack."""
2314 """Return the current directory stack."""
2305
2315
2306 return self.dir_stack[:]
2316 return self.dir_stack[:]
2307
2317
2308 def magic_sc(self, parameter_s=''):
2318 def magic_sc(self, parameter_s=''):
2309 """Shell capture - execute a shell command and capture its output.
2319 """Shell capture - execute a shell command and capture its output.
2310
2320
2311 %sc [options] varname=command
2321 %sc [options] varname=command
2312
2322
2313 IPython will run the given command using commands.getoutput(), and
2323 IPython will run the given command using commands.getoutput(), and
2314 will then update the user's interactive namespace with a variable
2324 will then update the user's interactive namespace with a variable
2315 called varname, containing the value of the call. Your command can
2325 called varname, containing the value of the call. Your command can
2316 contain shell wildcards, pipes, etc.
2326 contain shell wildcards, pipes, etc.
2317
2327
2318 The '=' sign in the syntax is mandatory, and the variable name you
2328 The '=' sign in the syntax is mandatory, and the variable name you
2319 supply must follow Python's standard conventions for valid names.
2329 supply must follow Python's standard conventions for valid names.
2320
2330
2321 Options:
2331 Options:
2322
2332
2323 -l: list output. Split the output on newlines into a list before
2333 -l: list output. Split the output on newlines into a list before
2324 assigning it to the given variable. By default the output is stored
2334 assigning it to the given variable. By default the output is stored
2325 as a single string.
2335 as a single string.
2326
2336
2327 -v: verbose. Print the contents of the variable.
2337 -v: verbose. Print the contents of the variable.
2328
2338
2329 In most cases you should not need to split as a list, because the
2339 In most cases you should not need to split as a list, because the
2330 returned value is a special type of string which can automatically
2340 returned value is a special type of string which can automatically
2331 provide its contents either as a list (split on newlines) or as a
2341 provide its contents either as a list (split on newlines) or as a
2332 space-separated string. These are convenient, respectively, either
2342 space-separated string. These are convenient, respectively, either
2333 for sequential processing or to be passed to a shell command.
2343 for sequential processing or to be passed to a shell command.
2334
2344
2335 For example:
2345 For example:
2336
2346
2337 # Capture into variable a
2347 # Capture into variable a
2338 In [9]: sc a=ls *py
2348 In [9]: sc a=ls *py
2339
2349
2340 # a is a string with embedded newlines
2350 # a is a string with embedded newlines
2341 In [10]: a
2351 In [10]: a
2342 Out[10]: 'setup.py\nwin32_manual_post_install.py'
2352 Out[10]: 'setup.py\nwin32_manual_post_install.py'
2343
2353
2344 # which can be seen as a list:
2354 # which can be seen as a list:
2345 In [11]: a.l
2355 In [11]: a.l
2346 Out[11]: ['setup.py', 'win32_manual_post_install.py']
2356 Out[11]: ['setup.py', 'win32_manual_post_install.py']
2347
2357
2348 # or as a whitespace-separated string:
2358 # or as a whitespace-separated string:
2349 In [12]: a.s
2359 In [12]: a.s
2350 Out[12]: 'setup.py win32_manual_post_install.py'
2360 Out[12]: 'setup.py win32_manual_post_install.py'
2351
2361
2352 # a.s is useful to pass as a single command line:
2362 # a.s is useful to pass as a single command line:
2353 In [13]: !wc -l $a.s
2363 In [13]: !wc -l $a.s
2354 146 setup.py
2364 146 setup.py
2355 130 win32_manual_post_install.py
2365 130 win32_manual_post_install.py
2356 276 total
2366 276 total
2357
2367
2358 # while the list form is useful to loop over:
2368 # while the list form is useful to loop over:
2359 In [14]: for f in a.l:
2369 In [14]: for f in a.l:
2360 ....: !wc -l $f
2370 ....: !wc -l $f
2361 ....:
2371 ....:
2362 146 setup.py
2372 146 setup.py
2363 130 win32_manual_post_install.py
2373 130 win32_manual_post_install.py
2364
2374
2365 Similiarly, the lists returned by the -l option are also special, in
2375 Similiarly, the lists returned by the -l option are also special, in
2366 the sense that you can equally invoke the .s attribute on them to
2376 the sense that you can equally invoke the .s attribute on them to
2367 automatically get a whitespace-separated string from their contents:
2377 automatically get a whitespace-separated string from their contents:
2368
2378
2369 In [1]: sc -l b=ls *py
2379 In [1]: sc -l b=ls *py
2370
2380
2371 In [2]: b
2381 In [2]: b
2372 Out[2]: ['setup.py', 'win32_manual_post_install.py']
2382 Out[2]: ['setup.py', 'win32_manual_post_install.py']
2373
2383
2374 In [3]: b.s
2384 In [3]: b.s
2375 Out[3]: 'setup.py win32_manual_post_install.py'
2385 Out[3]: 'setup.py win32_manual_post_install.py'
2376
2386
2377 In summary, both the lists and strings used for ouptut capture have
2387 In summary, both the lists and strings used for ouptut capture have
2378 the following special attributes:
2388 the following special attributes:
2379
2389
2380 .l (or .list) : value as list.
2390 .l (or .list) : value as list.
2381 .n (or .nlstr): value as newline-separated string.
2391 .n (or .nlstr): value as newline-separated string.
2382 .s (or .spstr): value as space-separated string.
2392 .s (or .spstr): value as space-separated string.
2383 """
2393 """
2384
2394
2385 opts,args = self.parse_options(parameter_s,'lv')
2395 opts,args = self.parse_options(parameter_s,'lv')
2386 # Try to get a variable name and command to run
2396 # Try to get a variable name and command to run
2387 try:
2397 try:
2388 # the variable name must be obtained from the parse_options
2398 # the variable name must be obtained from the parse_options
2389 # output, which uses shlex.split to strip options out.
2399 # output, which uses shlex.split to strip options out.
2390 var,_ = args.split('=',1)
2400 var,_ = args.split('=',1)
2391 var = var.strip()
2401 var = var.strip()
2392 # But the the command has to be extracted from the original input
2402 # But the the command has to be extracted from the original input
2393 # parameter_s, not on what parse_options returns, to avoid the
2403 # parameter_s, not on what parse_options returns, to avoid the
2394 # quote stripping which shlex.split performs on it.
2404 # quote stripping which shlex.split performs on it.
2395 _,cmd = parameter_s.split('=',1)
2405 _,cmd = parameter_s.split('=',1)
2396 except ValueError:
2406 except ValueError:
2397 var,cmd = '',''
2407 var,cmd = '',''
2398 if not var:
2408 if not var:
2399 error('you must specify a variable to assign the command to.')
2409 error('you must specify a variable to assign the command to.')
2400 return
2410 return
2401 # If all looks ok, proceed
2411 # If all looks ok, proceed
2402 out,err = self.shell.getoutputerror(cmd)
2412 out,err = self.shell.getoutputerror(cmd)
2403 if err:
2413 if err:
2404 print >> Term.cerr,err
2414 print >> Term.cerr,err
2405 if opts.has_key('l'):
2415 if opts.has_key('l'):
2406 out = SList(out.split('\n'))
2416 out = SList(out.split('\n'))
2407 else:
2417 else:
2408 out = LSString(out)
2418 out = LSString(out)
2409 if opts.has_key('v'):
2419 if opts.has_key('v'):
2410 print '%s ==\n%s' % (var,pformat(out))
2420 print '%s ==\n%s' % (var,pformat(out))
2411 self.shell.user_ns.update({var:out})
2421 self.shell.user_ns.update({var:out})
2412
2422
2413 def magic_sx(self, parameter_s=''):
2423 def magic_sx(self, parameter_s=''):
2414 """Shell execute - run a shell command and capture its output.
2424 """Shell execute - run a shell command and capture its output.
2415
2425
2416 %sx command
2426 %sx command
2417
2427
2418 IPython will run the given command using commands.getoutput(), and
2428 IPython will run the given command using commands.getoutput(), and
2419 return the result formatted as a list (split on '\\n'). Since the
2429 return the result formatted as a list (split on '\\n'). Since the
2420 output is _returned_, it will be stored in ipython's regular output
2430 output is _returned_, it will be stored in ipython's regular output
2421 cache Out[N] and in the '_N' automatic variables.
2431 cache Out[N] and in the '_N' automatic variables.
2422
2432
2423 Notes:
2433 Notes:
2424
2434
2425 1) If an input line begins with '!!', then %sx is automatically
2435 1) If an input line begins with '!!', then %sx is automatically
2426 invoked. That is, while:
2436 invoked. That is, while:
2427 !ls
2437 !ls
2428 causes ipython to simply issue system('ls'), typing
2438 causes ipython to simply issue system('ls'), typing
2429 !!ls
2439 !!ls
2430 is a shorthand equivalent to:
2440 is a shorthand equivalent to:
2431 %sx ls
2441 %sx ls
2432
2442
2433 2) %sx differs from %sc in that %sx automatically splits into a list,
2443 2) %sx differs from %sc in that %sx automatically splits into a list,
2434 like '%sc -l'. The reason for this is to make it as easy as possible
2444 like '%sc -l'. The reason for this is to make it as easy as possible
2435 to process line-oriented shell output via further python commands.
2445 to process line-oriented shell output via further python commands.
2436 %sc is meant to provide much finer control, but requires more
2446 %sc is meant to provide much finer control, but requires more
2437 typing.
2447 typing.
2438
2448
2439 3) Just like %sc -l, this is a list with special attributes:
2449 3) Just like %sc -l, this is a list with special attributes:
2440
2450
2441 .l (or .list) : value as list.
2451 .l (or .list) : value as list.
2442 .n (or .nlstr): value as newline-separated string.
2452 .n (or .nlstr): value as newline-separated string.
2443 .s (or .spstr): value as whitespace-separated string.
2453 .s (or .spstr): value as whitespace-separated string.
2444
2454
2445 This is very useful when trying to use such lists as arguments to
2455 This is very useful when trying to use such lists as arguments to
2446 system commands."""
2456 system commands."""
2447
2457
2448 if parameter_s:
2458 if parameter_s:
2449 out,err = self.shell.getoutputerror(parameter_s)
2459 out,err = self.shell.getoutputerror(parameter_s)
2450 if err:
2460 if err:
2451 print >> Term.cerr,err
2461 print >> Term.cerr,err
2452 return SList(out.split('\n'))
2462 return SList(out.split('\n'))
2453
2463
2454 def magic_bg(self, parameter_s=''):
2464 def magic_bg(self, parameter_s=''):
2455 """Run a job in the background, in a separate thread.
2465 """Run a job in the background, in a separate thread.
2456
2466
2457 For example,
2467 For example,
2458
2468
2459 %bg myfunc(x,y,z=1)
2469 %bg myfunc(x,y,z=1)
2460
2470
2461 will execute 'myfunc(x,y,z=1)' in a background thread. As soon as the
2471 will execute 'myfunc(x,y,z=1)' in a background thread. As soon as the
2462 execution starts, a message will be printed indicating the job
2472 execution starts, a message will be printed indicating the job
2463 number. If your job number is 5, you can use
2473 number. If your job number is 5, you can use
2464
2474
2465 myvar = jobs.result(5) or myvar = jobs[5].result
2475 myvar = jobs.result(5) or myvar = jobs[5].result
2466
2476
2467 to assign this result to variable 'myvar'.
2477 to assign this result to variable 'myvar'.
2468
2478
2469 IPython has a job manager, accessible via the 'jobs' object. You can
2479 IPython has a job manager, accessible via the 'jobs' object. You can
2470 type jobs? to get more information about it, and use jobs.<TAB> to see
2480 type jobs? to get more information about it, and use jobs.<TAB> to see
2471 its attributes. All attributes not starting with an underscore are
2481 its attributes. All attributes not starting with an underscore are
2472 meant for public use.
2482 meant for public use.
2473
2483
2474 In particular, look at the jobs.new() method, which is used to create
2484 In particular, look at the jobs.new() method, which is used to create
2475 new jobs. This magic %bg function is just a convenience wrapper
2485 new jobs. This magic %bg function is just a convenience wrapper
2476 around jobs.new(), for expression-based jobs. If you want to create a
2486 around jobs.new(), for expression-based jobs. If you want to create a
2477 new job with an explicit function object and arguments, you must call
2487 new job with an explicit function object and arguments, you must call
2478 jobs.new() directly.
2488 jobs.new() directly.
2479
2489
2480 The jobs.new docstring also describes in detail several important
2490 The jobs.new docstring also describes in detail several important
2481 caveats associated with a thread-based model for background job
2491 caveats associated with a thread-based model for background job
2482 execution. Type jobs.new? for details.
2492 execution. Type jobs.new? for details.
2483
2493
2484 You can check the status of all jobs with jobs.status().
2494 You can check the status of all jobs with jobs.status().
2485
2495
2486 The jobs variable is set by IPython into the Python builtin namespace.
2496 The jobs variable is set by IPython into the Python builtin namespace.
2487 If you ever declare a variable named 'jobs', you will shadow this
2497 If you ever declare a variable named 'jobs', you will shadow this
2488 name. You can either delete your global jobs variable to regain
2498 name. You can either delete your global jobs variable to regain
2489 access to the job manager, or make a new name and assign it manually
2499 access to the job manager, or make a new name and assign it manually
2490 to the manager (stored in IPython's namespace). For example, to
2500 to the manager (stored in IPython's namespace). For example, to
2491 assign the job manager to the Jobs name, use:
2501 assign the job manager to the Jobs name, use:
2492
2502
2493 Jobs = __builtins__.jobs"""
2503 Jobs = __builtins__.jobs"""
2494
2504
2495 self.shell.jobs.new(parameter_s,self.shell.user_ns)
2505 self.shell.jobs.new(parameter_s,self.shell.user_ns)
2496
2506
2497 def magic_bookmark(self, parameter_s=''):
2507 def magic_bookmark(self, parameter_s=''):
2498 """Manage IPython's bookmark system.
2508 """Manage IPython's bookmark system.
2499
2509
2500 %bookmark <name> - set bookmark to current dir
2510 %bookmark <name> - set bookmark to current dir
2501 %bookmark <name> <dir> - set bookmark to <dir>
2511 %bookmark <name> <dir> - set bookmark to <dir>
2502 %bookmark -l - list all bookmarks
2512 %bookmark -l - list all bookmarks
2503 %bookmark -d <name> - remove bookmark
2513 %bookmark -d <name> - remove bookmark
2504 %bookmark -r - remove all bookmarks
2514 %bookmark -r - remove all bookmarks
2505
2515
2506 You can later on access a bookmarked folder with:
2516 You can later on access a bookmarked folder with:
2507 %cd -b <name>
2517 %cd -b <name>
2508 or simply '%cd <name>' if there is no directory called <name> AND
2518 or simply '%cd <name>' if there is no directory called <name> AND
2509 there is such a bookmark defined.
2519 there is such a bookmark defined.
2510
2520
2511 Your bookmarks persist through IPython sessions, but they are
2521 Your bookmarks persist through IPython sessions, but they are
2512 associated with each profile."""
2522 associated with each profile."""
2513
2523
2514 opts,args = self.parse_options(parameter_s,'drl',mode='list')
2524 opts,args = self.parse_options(parameter_s,'drl',mode='list')
2515 if len(args) > 2:
2525 if len(args) > 2:
2516 error('You can only give at most two arguments')
2526 error('You can only give at most two arguments')
2517 return
2527 return
2518
2528
2519 bkms = self.shell.persist.get('bookmarks',{})
2529 bkms = self.shell.persist.get('bookmarks',{})
2520
2530
2521 if opts.has_key('d'):
2531 if opts.has_key('d'):
2522 try:
2532 try:
2523 todel = args[0]
2533 todel = args[0]
2524 except IndexError:
2534 except IndexError:
2525 error('You must provide a bookmark to delete')
2535 error('You must provide a bookmark to delete')
2526 else:
2536 else:
2527 try:
2537 try:
2528 del bkms[todel]
2538 del bkms[todel]
2529 except:
2539 except:
2530 error("Can't delete bookmark '%s'" % todel)
2540 error("Can't delete bookmark '%s'" % todel)
2531 elif opts.has_key('r'):
2541 elif opts.has_key('r'):
2532 bkms = {}
2542 bkms = {}
2533 elif opts.has_key('l'):
2543 elif opts.has_key('l'):
2534 bks = bkms.keys()
2544 bks = bkms.keys()
2535 bks.sort()
2545 bks.sort()
2536 if bks:
2546 if bks:
2537 size = max(map(len,bks))
2547 size = max(map(len,bks))
2538 else:
2548 else:
2539 size = 0
2549 size = 0
2540 fmt = '%-'+str(size)+'s -> %s'
2550 fmt = '%-'+str(size)+'s -> %s'
2541 print 'Current bookmarks:'
2551 print 'Current bookmarks:'
2542 for bk in bks:
2552 for bk in bks:
2543 print fmt % (bk,bkms[bk])
2553 print fmt % (bk,bkms[bk])
2544 else:
2554 else:
2545 if not args:
2555 if not args:
2546 error("You must specify the bookmark name")
2556 error("You must specify the bookmark name")
2547 elif len(args)==1:
2557 elif len(args)==1:
2548 bkms[args[0]] = os.getcwd()
2558 bkms[args[0]] = os.getcwd()
2549 elif len(args)==2:
2559 elif len(args)==2:
2550 bkms[args[0]] = args[1]
2560 bkms[args[0]] = args[1]
2551 self.persist['bookmarks'] = bkms
2561 self.persist['bookmarks'] = bkms
2552
2562
2553 def magic_pycat(self, parameter_s=''):
2563 def magic_pycat(self, parameter_s=''):
2554 """Show a syntax-highlighted file through a pager.
2564 """Show a syntax-highlighted file through a pager.
2555
2565
2556 This magic is similar to the cat utility, but it will assume the file
2566 This magic is similar to the cat utility, but it will assume the file
2557 to be Python source and will show it with syntax highlighting. """
2567 to be Python source and will show it with syntax highlighting. """
2558
2568
2559 filename = get_py_filename(parameter_s)
2569 filename = get_py_filename(parameter_s)
2560 page(self.shell.colorize(file_read(filename)),
2570 page(self.shell.colorize(file_read(filename)),
2561 screen_lines=self.shell.rc.screen_length)
2571 screen_lines=self.shell.rc.screen_length)
2562
2572
2563 # end Magic
2573 # end Magic
@@ -1,572 +1,574 b''
1 # -*- coding: utf-8 -*-
1 # -*- coding: utf-8 -*-
2 """
2 """
3 Classes for handling input/output prompts.
3 Classes for handling input/output prompts.
4
4
5 $Id: Prompts.py 638 2005-07-18 03:01:41Z fperez $"""
5 $Id: Prompts.py 951 2005-12-25 00:57:24Z fperez $"""
6
6
7 #*****************************************************************************
7 #*****************************************************************************
8 # Copyright (C) 2001-2004 Fernando Perez <fperez@colorado.edu>
8 # Copyright (C) 2001-2004 Fernando Perez <fperez@colorado.edu>
9 #
9 #
10 # Distributed under the terms of the BSD License. The full license is in
10 # Distributed under the terms of the BSD License. The full license is in
11 # the file COPYING, distributed as part of this software.
11 # the file COPYING, distributed as part of this software.
12 #*****************************************************************************
12 #*****************************************************************************
13
13
14 from IPython import Release
14 from IPython import Release
15 __author__ = '%s <%s>' % Release.authors['Fernando']
15 __author__ = '%s <%s>' % Release.authors['Fernando']
16 __license__ = Release.license
16 __license__ = Release.license
17 __version__ = Release.version
17 __version__ = Release.version
18
18
19 #****************************************************************************
19 #****************************************************************************
20 # Required modules
20 # Required modules
21 import __builtin__
21 import __builtin__
22 import os,sys,socket
22 import os,sys,socket
23 import time
23 import time
24 from pprint import pprint,pformat
24 from pprint import pprint,pformat
25
25
26 # IPython's own
26 # IPython's own
27 from IPython.genutils import *
27 from IPython.genutils import *
28 from IPython.Struct import Struct
28 from IPython.Struct import Struct
29 from IPython.Magic import Macro
29 from IPython.Magic import Macro
30 from IPython.Itpl import ItplNS
30 from IPython.Itpl import ItplNS
31 from IPython import ColorANSI
31 from IPython import ColorANSI
32
32
33 #****************************************************************************
33 #****************************************************************************
34 #Color schemes for Prompts.
34 #Color schemes for Prompts.
35
35
36 PromptColors = ColorANSI.ColorSchemeTable()
36 PromptColors = ColorANSI.ColorSchemeTable()
37 InputColors = ColorANSI.InputTermColors # just a shorthand
37 InputColors = ColorANSI.InputTermColors # just a shorthand
38 Colors = ColorANSI.TermColors # just a shorthand
38 Colors = ColorANSI.TermColors # just a shorthand
39
39
40 PromptColors.add_scheme(ColorANSI.ColorScheme(
40 PromptColors.add_scheme(ColorANSI.ColorScheme(
41 'NoColor',
41 'NoColor',
42 in_prompt = InputColors.NoColor, # Input prompt
42 in_prompt = InputColors.NoColor, # Input prompt
43 in_number = InputColors.NoColor, # Input prompt number
43 in_number = InputColors.NoColor, # Input prompt number
44 in_prompt2 = InputColors.NoColor, # Continuation prompt
44 in_prompt2 = InputColors.NoColor, # Continuation prompt
45 in_normal = InputColors.NoColor, # color off (usu. Colors.Normal)
45 in_normal = InputColors.NoColor, # color off (usu. Colors.Normal)
46
46
47 out_prompt = Colors.NoColor, # Output prompt
47 out_prompt = Colors.NoColor, # Output prompt
48 out_number = Colors.NoColor, # Output prompt number
48 out_number = Colors.NoColor, # Output prompt number
49
49
50 normal = Colors.NoColor # color off (usu. Colors.Normal)
50 normal = Colors.NoColor # color off (usu. Colors.Normal)
51 ))
51 ))
52
52 # make some schemes as instances so we can copy them for modification easily:
53 # make some schemes as instances so we can copy them for modification easily:
53 __PColLinux = ColorANSI.ColorScheme(
54 __PColLinux = ColorANSI.ColorScheme(
54 'Linux',
55 'Linux',
55 in_prompt = InputColors.Green,
56 in_prompt = InputColors.Green,
56 in_number = InputColors.LightGreen,
57 in_number = InputColors.LightGreen,
57 in_prompt2 = InputColors.Green,
58 in_prompt2 = InputColors.Green,
58 in_normal = InputColors.Normal, # color off (usu. Colors.Normal)
59 in_normal = InputColors.Normal, # color off (usu. Colors.Normal)
59
60
60 out_prompt = Colors.Red,
61 out_prompt = Colors.Red,
61 out_number = Colors.LightRed,
62 out_number = Colors.LightRed,
62
63
63 normal = Colors.Normal
64 normal = Colors.Normal
64 )
65 )
65 # Don't forget to enter it into the table!
66 # Don't forget to enter it into the table!
66 PromptColors.add_scheme(__PColLinux)
67 PromptColors.add_scheme(__PColLinux)
68
67 # Slightly modified Linux for light backgrounds
69 # Slightly modified Linux for light backgrounds
68 __PColLightBG = ColorANSI.ColorScheme('LightBG',**__PColLinux.colors.dict().copy())
70 __PColLightBG = __PColLinux.copy('LightBG')
69
71
70 __PColLightBG.colors.update(
72 __PColLightBG.colors.update(
71 in_prompt = InputColors.Blue,
73 in_prompt = InputColors.Blue,
72 in_number = InputColors.LightBlue,
74 in_number = InputColors.LightBlue,
73 in_prompt2 = InputColors.Blue
75 in_prompt2 = InputColors.Blue
74 )
76 )
75 PromptColors.add_scheme(__PColLightBG)
77 PromptColors.add_scheme(__PColLightBG)
76
78
77 del Colors,InputColors
79 del Colors,InputColors
78
80
79 #-----------------------------------------------------------------------------
81 #-----------------------------------------------------------------------------
80 def multiple_replace(dict, text):
82 def multiple_replace(dict, text):
81 """ Replace in 'text' all occurences of any key in the given
83 """ Replace in 'text' all occurences of any key in the given
82 dictionary by its corresponding value. Returns the new string."""
84 dictionary by its corresponding value. Returns the new string."""
83
85
84 # Function by Xavier Defrang, originally found at:
86 # Function by Xavier Defrang, originally found at:
85 # http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/81330
87 # http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/81330
86
88
87 # Create a regular expression from the dictionary keys
89 # Create a regular expression from the dictionary keys
88 regex = re.compile("(%s)" % "|".join(map(re.escape, dict.keys())))
90 regex = re.compile("(%s)" % "|".join(map(re.escape, dict.keys())))
89 # For each match, look-up corresponding value in dictionary
91 # For each match, look-up corresponding value in dictionary
90 return regex.sub(lambda mo: dict[mo.string[mo.start():mo.end()]], text)
92 return regex.sub(lambda mo: dict[mo.string[mo.start():mo.end()]], text)
91
93
92 #-----------------------------------------------------------------------------
94 #-----------------------------------------------------------------------------
93 # Special characters that can be used in prompt templates, mainly bash-like
95 # Special characters that can be used in prompt templates, mainly bash-like
94
96
95 # If $HOME isn't defined (Windows), make it an absurd string so that it can
97 # If $HOME isn't defined (Windows), make it an absurd string so that it can
96 # never be expanded out into '~'. Basically anything which can never be a
98 # never be expanded out into '~'. Basically anything which can never be a
97 # reasonable directory name will do, we just want the $HOME -> '~' operation
99 # reasonable directory name will do, we just want the $HOME -> '~' operation
98 # to become a no-op. We pre-compute $HOME here so it's not done on every
100 # to become a no-op. We pre-compute $HOME here so it's not done on every
99 # prompt call.
101 # prompt call.
100
102
101 # FIXME:
103 # FIXME:
102
104
103 # - This should be turned into a class which does proper namespace management,
105 # - This should be turned into a class which does proper namespace management,
104 # since the prompt specials need to be evaluated in a certain namespace.
106 # since the prompt specials need to be evaluated in a certain namespace.
105 # Currently it's just globals, which need to be managed manually by code
107 # Currently it's just globals, which need to be managed manually by code
106 # below.
108 # below.
107
109
108 # - I also need to split up the color schemes from the prompt specials
110 # - I also need to split up the color schemes from the prompt specials
109 # somehow. I don't have a clean design for that quite yet.
111 # somehow. I don't have a clean design for that quite yet.
110
112
111 HOME = os.environ.get("HOME","//////:::::ZZZZZ,,,~~~")
113 HOME = os.environ.get("HOME","//////:::::ZZZZZ,,,~~~")
112
114
113 # We precompute a few more strings here for the prompt_specials, which are
115 # We precompute a few more strings here for the prompt_specials, which are
114 # fixed once ipython starts. This reduces the runtime overhead of computing
116 # fixed once ipython starts. This reduces the runtime overhead of computing
115 # prompt strings.
117 # prompt strings.
116 USER = os.environ.get("USER")
118 USER = os.environ.get("USER")
117 HOSTNAME = socket.gethostname()
119 HOSTNAME = socket.gethostname()
118 HOSTNAME_SHORT = HOSTNAME.split(".")[0]
120 HOSTNAME_SHORT = HOSTNAME.split(".")[0]
119 ROOT_SYMBOL = "$#"[os.name=='nt' or os.getuid()==0]
121 ROOT_SYMBOL = "$#"[os.name=='nt' or os.getuid()==0]
120
122
121 prompt_specials_color = {
123 prompt_specials_color = {
122 # Prompt/history count
124 # Prompt/history count
123 '%n' : '${self.col_num}' '${self.cache.prompt_count}' '${self.col_p}',
125 '%n' : '${self.col_num}' '${self.cache.prompt_count}' '${self.col_p}',
124 '\\#': '${self.col_num}' '${self.cache.prompt_count}' '${self.col_p}',
126 '\\#': '${self.col_num}' '${self.cache.prompt_count}' '${self.col_p}',
125 # Prompt/history count, with the actual digits replaced by dots. Used
127 # Prompt/history count, with the actual digits replaced by dots. Used
126 # mainly in continuation prompts (prompt_in2)
128 # mainly in continuation prompts (prompt_in2)
127 '\\D': '${"."*len(str(self.cache.prompt_count))}',
129 '\\D': '${"."*len(str(self.cache.prompt_count))}',
128 # Current working directory
130 # Current working directory
129 '\\w': '${os.getcwd()}',
131 '\\w': '${os.getcwd()}',
130 # Current time
132 # Current time
131 '\\t' : '${time.strftime("%H:%M:%S")}',
133 '\\t' : '${time.strftime("%H:%M:%S")}',
132 # Basename of current working directory.
134 # Basename of current working directory.
133 # (use os.sep to make this portable across OSes)
135 # (use os.sep to make this portable across OSes)
134 '\\W' : '${os.getcwd().split("%s")[-1]}' % os.sep,
136 '\\W' : '${os.getcwd().split("%s")[-1]}' % os.sep,
135 # These X<N> are an extension to the normal bash prompts. They return
137 # These X<N> are an extension to the normal bash prompts. They return
136 # N terms of the path, after replacing $HOME with '~'
138 # N terms of the path, after replacing $HOME with '~'
137 '\\X0': '${os.getcwd().replace("%s","~")}' % HOME,
139 '\\X0': '${os.getcwd().replace("%s","~")}' % HOME,
138 '\\X1': '${self.cwd_filt(1)}',
140 '\\X1': '${self.cwd_filt(1)}',
139 '\\X2': '${self.cwd_filt(2)}',
141 '\\X2': '${self.cwd_filt(2)}',
140 '\\X3': '${self.cwd_filt(3)}',
142 '\\X3': '${self.cwd_filt(3)}',
141 '\\X4': '${self.cwd_filt(4)}',
143 '\\X4': '${self.cwd_filt(4)}',
142 '\\X5': '${self.cwd_filt(5)}',
144 '\\X5': '${self.cwd_filt(5)}',
143 # Y<N> are similar to X<N>, but they show '~' if it's the directory
145 # Y<N> are similar to X<N>, but they show '~' if it's the directory
144 # N+1 in the list. Somewhat like %cN in tcsh.
146 # N+1 in the list. Somewhat like %cN in tcsh.
145 '\\Y0': '${self.cwd_filt2(0)}',
147 '\\Y0': '${self.cwd_filt2(0)}',
146 '\\Y1': '${self.cwd_filt2(1)}',
148 '\\Y1': '${self.cwd_filt2(1)}',
147 '\\Y2': '${self.cwd_filt2(2)}',
149 '\\Y2': '${self.cwd_filt2(2)}',
148 '\\Y3': '${self.cwd_filt2(3)}',
150 '\\Y3': '${self.cwd_filt2(3)}',
149 '\\Y4': '${self.cwd_filt2(4)}',
151 '\\Y4': '${self.cwd_filt2(4)}',
150 '\\Y5': '${self.cwd_filt2(5)}',
152 '\\Y5': '${self.cwd_filt2(5)}',
151 # Hostname up to first .
153 # Hostname up to first .
152 '\\h': HOSTNAME_SHORT,
154 '\\h': HOSTNAME_SHORT,
153 # Full hostname
155 # Full hostname
154 '\\H': HOSTNAME,
156 '\\H': HOSTNAME,
155 # Username of current user
157 # Username of current user
156 '\\u': USER,
158 '\\u': USER,
157 # Escaped '\'
159 # Escaped '\'
158 '\\\\': '\\',
160 '\\\\': '\\',
159 # Newline
161 # Newline
160 '\\n': '\n',
162 '\\n': '\n',
161 # Carriage return
163 # Carriage return
162 '\\r': '\r',
164 '\\r': '\r',
163 # Release version
165 # Release version
164 '\\v': __version__,
166 '\\v': __version__,
165 # Root symbol ($ or #)
167 # Root symbol ($ or #)
166 '\\$': ROOT_SYMBOL,
168 '\\$': ROOT_SYMBOL,
167 }
169 }
168
170
169 # A copy of the prompt_specials dictionary but with all color escapes removed,
171 # A copy of the prompt_specials dictionary but with all color escapes removed,
170 # so we can correctly compute the prompt length for the auto_rewrite method.
172 # so we can correctly compute the prompt length for the auto_rewrite method.
171 prompt_specials_nocolor = prompt_specials_color.copy()
173 prompt_specials_nocolor = prompt_specials_color.copy()
172 prompt_specials_nocolor['%n'] = '${self.cache.prompt_count}'
174 prompt_specials_nocolor['%n'] = '${self.cache.prompt_count}'
173 prompt_specials_nocolor['\\#'] = '${self.cache.prompt_count}'
175 prompt_specials_nocolor['\\#'] = '${self.cache.prompt_count}'
174
176
175 # Add in all the InputTermColors color escapes as valid prompt characters.
177 # Add in all the InputTermColors color escapes as valid prompt characters.
176 # They all get added as \\C_COLORNAME, so that we don't have any conflicts
178 # They all get added as \\C_COLORNAME, so that we don't have any conflicts
177 # with a color name which may begin with a letter used by any other of the
179 # with a color name which may begin with a letter used by any other of the
178 # allowed specials. This of course means that \\C will never be allowed for
180 # allowed specials. This of course means that \\C will never be allowed for
179 # anything else.
181 # anything else.
180 input_colors = ColorANSI.InputTermColors
182 input_colors = ColorANSI.InputTermColors
181 for _color in dir(input_colors):
183 for _color in dir(input_colors):
182 if _color[0] != '_':
184 if _color[0] != '_':
183 c_name = '\\C_'+_color
185 c_name = '\\C_'+_color
184 prompt_specials_color[c_name] = getattr(input_colors,_color)
186 prompt_specials_color[c_name] = getattr(input_colors,_color)
185 prompt_specials_nocolor[c_name] = ''
187 prompt_specials_nocolor[c_name] = ''
186
188
187 # we default to no color for safety. Note that prompt_specials is a global
189 # we default to no color for safety. Note that prompt_specials is a global
188 # variable used by all prompt objects.
190 # variable used by all prompt objects.
189 prompt_specials = prompt_specials_nocolor
191 prompt_specials = prompt_specials_nocolor
190
192
191 #-----------------------------------------------------------------------------
193 #-----------------------------------------------------------------------------
192 def str_safe(arg):
194 def str_safe(arg):
193 """Convert to a string, without ever raising an exception.
195 """Convert to a string, without ever raising an exception.
194
196
195 If str(arg) fails, <ERROR: ... > is returned, where ... is the exception
197 If str(arg) fails, <ERROR: ... > is returned, where ... is the exception
196 error message."""
198 error message."""
197
199
198 try:
200 try:
199 out = str(arg)
201 out = str(arg)
200 except UnicodeError:
202 except UnicodeError:
201 try:
203 try:
202 out = arg.encode('utf_8','replace')
204 out = arg.encode('utf_8','replace')
203 except Exception,msg:
205 except Exception,msg:
204 # let's keep this little duplication here, so that the most common
206 # let's keep this little duplication here, so that the most common
205 # case doesn't suffer from a double try wrapping.
207 # case doesn't suffer from a double try wrapping.
206 out = '<ERROR: %s>' % msg
208 out = '<ERROR: %s>' % msg
207 except Exception,msg:
209 except Exception,msg:
208 out = '<ERROR: %s>' % msg
210 out = '<ERROR: %s>' % msg
209 return out
211 return out
210
212
211 class BasePrompt:
213 class BasePrompt:
212 """Interactive prompt similar to Mathematica's."""
214 """Interactive prompt similar to Mathematica's."""
213 def __init__(self,cache,sep,prompt,pad_left=False):
215 def __init__(self,cache,sep,prompt,pad_left=False):
214
216
215 # Hack: we access information about the primary prompt through the
217 # Hack: we access information about the primary prompt through the
216 # cache argument. We need this, because we want the secondary prompt
218 # cache argument. We need this, because we want the secondary prompt
217 # to be aligned with the primary one. Color table info is also shared
219 # to be aligned with the primary one. Color table info is also shared
218 # by all prompt classes through the cache. Nice OO spaghetti code!
220 # by all prompt classes through the cache. Nice OO spaghetti code!
219 self.cache = cache
221 self.cache = cache
220 self.sep = sep
222 self.sep = sep
221
223
222 # regexp to count the number of spaces at the end of a prompt
224 # regexp to count the number of spaces at the end of a prompt
223 # expression, useful for prompt auto-rewriting
225 # expression, useful for prompt auto-rewriting
224 self.rspace = re.compile(r'(\s*)$')
226 self.rspace = re.compile(r'(\s*)$')
225 # Flag to left-pad prompt strings to match the length of the primary
227 # Flag to left-pad prompt strings to match the length of the primary
226 # prompt
228 # prompt
227 self.pad_left = pad_left
229 self.pad_left = pad_left
228 # Set template to create each actual prompt (where numbers change)
230 # Set template to create each actual prompt (where numbers change)
229 self.p_template = prompt
231 self.p_template = prompt
230 self.set_p_str()
232 self.set_p_str()
231
233
232 def set_p_str(self):
234 def set_p_str(self):
233 """ Set the interpolating prompt strings.
235 """ Set the interpolating prompt strings.
234
236
235 This must be called every time the color settings change, because the
237 This must be called every time the color settings change, because the
236 prompt_specials global may have changed."""
238 prompt_specials global may have changed."""
237
239
238 import os,time # needed in locals for prompt string handling
240 import os,time # needed in locals for prompt string handling
239 loc = locals()
241 loc = locals()
240 self.p_str = ItplNS('%s%s%s' %
242 self.p_str = ItplNS('%s%s%s' %
241 ('${self.sep}${self.col_p}',
243 ('${self.sep}${self.col_p}',
242 multiple_replace(prompt_specials, self.p_template),
244 multiple_replace(prompt_specials, self.p_template),
243 '${self.col_norm}'),self.cache.user_ns,loc)
245 '${self.col_norm}'),self.cache.user_ns,loc)
244
246
245 self.p_str_nocolor = ItplNS(multiple_replace(prompt_specials_nocolor,
247 self.p_str_nocolor = ItplNS(multiple_replace(prompt_specials_nocolor,
246 self.p_template),
248 self.p_template),
247 self.cache.user_ns,loc)
249 self.cache.user_ns,loc)
248
250
249 def write(self,msg): # dbg
251 def write(self,msg): # dbg
250 sys.stdout.write(msg)
252 sys.stdout.write(msg)
251 return ''
253 return ''
252
254
253 def __str__(self):
255 def __str__(self):
254 """Return a string form of the prompt.
256 """Return a string form of the prompt.
255
257
256 This for is useful for continuation and output prompts, since it is
258 This for is useful for continuation and output prompts, since it is
257 left-padded to match lengths with the primary one (if the
259 left-padded to match lengths with the primary one (if the
258 self.pad_left attribute is set)."""
260 self.pad_left attribute is set)."""
259
261
260 out_str = str_safe(self.p_str)
262 out_str = str_safe(self.p_str)
261 if self.pad_left:
263 if self.pad_left:
262 # We must find the amount of padding required to match lengths,
264 # We must find the amount of padding required to match lengths,
263 # taking the color escapes (which are invisible on-screen) into
265 # taking the color escapes (which are invisible on-screen) into
264 # account.
266 # account.
265 esc_pad = len(out_str) - len(str_safe(self.p_str_nocolor))
267 esc_pad = len(out_str) - len(str_safe(self.p_str_nocolor))
266 format = '%%%ss' % (len(str(self.cache.last_prompt))+esc_pad)
268 format = '%%%ss' % (len(str(self.cache.last_prompt))+esc_pad)
267 return format % out_str
269 return format % out_str
268 else:
270 else:
269 return out_str
271 return out_str
270
272
271 # these path filters are put in as methods so that we can control the
273 # these path filters are put in as methods so that we can control the
272 # namespace where the prompt strings get evaluated
274 # namespace where the prompt strings get evaluated
273 def cwd_filt(self,depth):
275 def cwd_filt(self,depth):
274 """Return the last depth elements of the current working directory.
276 """Return the last depth elements of the current working directory.
275
277
276 $HOME is always replaced with '~'.
278 $HOME is always replaced with '~'.
277 If depth==0, the full path is returned."""
279 If depth==0, the full path is returned."""
278
280
279 cwd = os.getcwd().replace(HOME,"~")
281 cwd = os.getcwd().replace(HOME,"~")
280 out = os.sep.join(cwd.split(os.sep)[-depth:])
282 out = os.sep.join(cwd.split(os.sep)[-depth:])
281 if out:
283 if out:
282 return out
284 return out
283 else:
285 else:
284 return os.sep
286 return os.sep
285
287
286 def cwd_filt2(self,depth):
288 def cwd_filt2(self,depth):
287 """Return the last depth elements of the current working directory.
289 """Return the last depth elements of the current working directory.
288
290
289 $HOME is always replaced with '~'.
291 $HOME is always replaced with '~'.
290 If depth==0, the full path is returned."""
292 If depth==0, the full path is returned."""
291
293
292 cwd = os.getcwd().replace(HOME,"~").split(os.sep)
294 cwd = os.getcwd().replace(HOME,"~").split(os.sep)
293 if '~' in cwd and len(cwd) == depth+1:
295 if '~' in cwd and len(cwd) == depth+1:
294 depth += 1
296 depth += 1
295 out = os.sep.join(cwd[-depth:])
297 out = os.sep.join(cwd[-depth:])
296 if out:
298 if out:
297 return out
299 return out
298 else:
300 else:
299 return os.sep
301 return os.sep
300
302
301 class Prompt1(BasePrompt):
303 class Prompt1(BasePrompt):
302 """Input interactive prompt similar to Mathematica's."""
304 """Input interactive prompt similar to Mathematica's."""
303
305
304 def __init__(self,cache,sep='\n',prompt='In [\\#]: ',pad_left=True):
306 def __init__(self,cache,sep='\n',prompt='In [\\#]: ',pad_left=True):
305 BasePrompt.__init__(self,cache,sep,prompt,pad_left)
307 BasePrompt.__init__(self,cache,sep,prompt,pad_left)
306
308
307 def set_colors(self):
309 def set_colors(self):
308 self.set_p_str()
310 self.set_p_str()
309 Colors = self.cache.color_table.active_colors # shorthand
311 Colors = self.cache.color_table.active_colors # shorthand
310 self.col_p = Colors.in_prompt
312 self.col_p = Colors.in_prompt
311 self.col_num = Colors.in_number
313 self.col_num = Colors.in_number
312 self.col_norm = Colors.in_normal
314 self.col_norm = Colors.in_normal
313 # We need a non-input version of these escapes for the '--->'
315 # We need a non-input version of these escapes for the '--->'
314 # auto-call prompts used in the auto_rewrite() method.
316 # auto-call prompts used in the auto_rewrite() method.
315 self.col_p_ni = self.col_p.replace('\001','').replace('\002','')
317 self.col_p_ni = self.col_p.replace('\001','').replace('\002','')
316 self.col_norm_ni = Colors.normal
318 self.col_norm_ni = Colors.normal
317
319
318 def __str__(self):
320 def __str__(self):
319 self.cache.prompt_count += 1
321 self.cache.prompt_count += 1
320 self.cache.last_prompt = str_safe(self.p_str_nocolor).split('\n')[-1]
322 self.cache.last_prompt = str_safe(self.p_str_nocolor).split('\n')[-1]
321 return str_safe(self.p_str)
323 return str_safe(self.p_str)
322
324
323 def auto_rewrite(self):
325 def auto_rewrite(self):
324 """Print a string of the form '--->' which lines up with the previous
326 """Print a string of the form '--->' which lines up with the previous
325 input string. Useful for systems which re-write the user input when
327 input string. Useful for systems which re-write the user input when
326 handling automatically special syntaxes."""
328 handling automatically special syntaxes."""
327
329
328 curr = str(self.cache.last_prompt)
330 curr = str(self.cache.last_prompt)
329 nrspaces = len(self.rspace.search(curr).group())
331 nrspaces = len(self.rspace.search(curr).group())
330 return '%s%s>%s%s' % (self.col_p_ni,'-'*(len(curr)-nrspaces-1),
332 return '%s%s>%s%s' % (self.col_p_ni,'-'*(len(curr)-nrspaces-1),
331 ' '*nrspaces,self.col_norm_ni)
333 ' '*nrspaces,self.col_norm_ni)
332
334
333 class PromptOut(BasePrompt):
335 class PromptOut(BasePrompt):
334 """Output interactive prompt similar to Mathematica's."""
336 """Output interactive prompt similar to Mathematica's."""
335
337
336 def __init__(self,cache,sep='',prompt='Out[\\#]: ',pad_left=True):
338 def __init__(self,cache,sep='',prompt='Out[\\#]: ',pad_left=True):
337 BasePrompt.__init__(self,cache,sep,prompt,pad_left)
339 BasePrompt.__init__(self,cache,sep,prompt,pad_left)
338 if not self.p_template:
340 if not self.p_template:
339 self.__str__ = lambda: ''
341 self.__str__ = lambda: ''
340
342
341 def set_colors(self):
343 def set_colors(self):
342 self.set_p_str()
344 self.set_p_str()
343 Colors = self.cache.color_table.active_colors # shorthand
345 Colors = self.cache.color_table.active_colors # shorthand
344 self.col_p = Colors.out_prompt
346 self.col_p = Colors.out_prompt
345 self.col_num = Colors.out_number
347 self.col_num = Colors.out_number
346 self.col_norm = Colors.normal
348 self.col_norm = Colors.normal
347
349
348 class Prompt2(BasePrompt):
350 class Prompt2(BasePrompt):
349 """Interactive continuation prompt."""
351 """Interactive continuation prompt."""
350
352
351 def __init__(self,cache,prompt=' .\\D.: ',pad_left=True):
353 def __init__(self,cache,prompt=' .\\D.: ',pad_left=True):
352 self.cache = cache
354 self.cache = cache
353 self.p_template = prompt
355 self.p_template = prompt
354 self.pad_left = pad_left
356 self.pad_left = pad_left
355 self.set_p_str()
357 self.set_p_str()
356
358
357 def set_p_str(self):
359 def set_p_str(self):
358 import os,time # needed in locals for prompt string handling
360 import os,time # needed in locals for prompt string handling
359 loc = locals()
361 loc = locals()
360 self.p_str = ItplNS('%s%s%s' %
362 self.p_str = ItplNS('%s%s%s' %
361 ('${self.col_p2}',
363 ('${self.col_p2}',
362 multiple_replace(prompt_specials, self.p_template),
364 multiple_replace(prompt_specials, self.p_template),
363 '$self.col_norm'),
365 '$self.col_norm'),
364 self.cache.user_ns,loc)
366 self.cache.user_ns,loc)
365 self.p_str_nocolor = ItplNS(multiple_replace(prompt_specials_nocolor,
367 self.p_str_nocolor = ItplNS(multiple_replace(prompt_specials_nocolor,
366 self.p_template),
368 self.p_template),
367 self.cache.user_ns,loc)
369 self.cache.user_ns,loc)
368
370
369 def set_colors(self):
371 def set_colors(self):
370 self.set_p_str()
372 self.set_p_str()
371 Colors = self.cache.color_table.active_colors
373 Colors = self.cache.color_table.active_colors
372 self.col_p2 = Colors.in_prompt2
374 self.col_p2 = Colors.in_prompt2
373 self.col_norm = Colors.in_normal
375 self.col_norm = Colors.in_normal
374 # FIXME (2004-06-16) HACK: prevent crashes for users who haven't
376 # FIXME (2004-06-16) HACK: prevent crashes for users who haven't
375 # updated their prompt_in2 definitions. Remove eventually.
377 # updated their prompt_in2 definitions. Remove eventually.
376 self.col_p = Colors.out_prompt
378 self.col_p = Colors.out_prompt
377 self.col_num = Colors.out_number
379 self.col_num = Colors.out_number
378
380
379 #-----------------------------------------------------------------------------
381 #-----------------------------------------------------------------------------
380 class CachedOutput:
382 class CachedOutput:
381 """Class for printing output from calculations while keeping a cache of
383 """Class for printing output from calculations while keeping a cache of
382 reults. It dynamically creates global variables prefixed with _ which
384 reults. It dynamically creates global variables prefixed with _ which
383 contain these results.
385 contain these results.
384
386
385 Meant to be used as a sys.displayhook replacement, providing numbered
387 Meant to be used as a sys.displayhook replacement, providing numbered
386 prompts and cache services.
388 prompts and cache services.
387
389
388 Initialize with initial and final values for cache counter (this defines
390 Initialize with initial and final values for cache counter (this defines
389 the maximum size of the cache."""
391 the maximum size of the cache."""
390
392
391 def __init__(self,cache_size,Pprint,colors='NoColor',input_sep='\n',
393 def __init__(self,cache_size,Pprint,colors='NoColor',input_sep='\n',
392 output_sep='\n',output_sep2='',user_ns={},
394 output_sep='\n',output_sep2='',user_ns={},
393 ps1 = None, ps2 = None,ps_out = None,
395 ps1 = None, ps2 = None,ps_out = None,
394 input_hist = None,pad_left=True):
396 input_hist = None,pad_left=True):
395
397
396 cache_size_min = 20
398 cache_size_min = 20
397 if cache_size <= 0:
399 if cache_size <= 0:
398 self.do_full_cache = 0
400 self.do_full_cache = 0
399 cache_size = 0
401 cache_size = 0
400 elif cache_size < cache_size_min:
402 elif cache_size < cache_size_min:
401 self.do_full_cache = 0
403 self.do_full_cache = 0
402 cache_size = 0
404 cache_size = 0
403 warn('caching was disabled (min value for cache size is %s).' %
405 warn('caching was disabled (min value for cache size is %s).' %
404 cache_size_min,level=3)
406 cache_size_min,level=3)
405 else:
407 else:
406 self.do_full_cache = 1
408 self.do_full_cache = 1
407
409
408 self.cache_size = cache_size
410 self.cache_size = cache_size
409 self.input_sep = input_sep
411 self.input_sep = input_sep
410
412
411 # we need a reference to the user-level namespace
413 # we need a reference to the user-level namespace
412 self.user_ns = user_ns
414 self.user_ns = user_ns
413 # and to the user's input
415 # and to the user's input
414 self.input_hist = input_hist
416 self.input_hist = input_hist
415
417
416 # Set input prompt strings and colors
418 # Set input prompt strings and colors
417 if cache_size == 0:
419 if cache_size == 0:
418 if ps1.find('%n') > -1 or ps1.find('\\#') > -1: ps1 = '>>> '
420 if ps1.find('%n') > -1 or ps1.find('\\#') > -1: ps1 = '>>> '
419 if ps2.find('%n') > -1 or ps2.find('\\#') > -1: ps2 = '... '
421 if ps2.find('%n') > -1 or ps2.find('\\#') > -1: ps2 = '... '
420 self.ps1_str = self._set_prompt_str(ps1,'In [\\#]: ','>>> ')
422 self.ps1_str = self._set_prompt_str(ps1,'In [\\#]: ','>>> ')
421 self.ps2_str = self._set_prompt_str(ps2,' .\\D.: ','... ')
423 self.ps2_str = self._set_prompt_str(ps2,' .\\D.: ','... ')
422 self.ps_out_str = self._set_prompt_str(ps_out,'Out[\\#]: ','')
424 self.ps_out_str = self._set_prompt_str(ps_out,'Out[\\#]: ','')
423
425
424 self.color_table = PromptColors
426 self.color_table = PromptColors
425 self.prompt1 = Prompt1(self,sep=input_sep,prompt=self.ps1_str,
427 self.prompt1 = Prompt1(self,sep=input_sep,prompt=self.ps1_str,
426 pad_left=pad_left)
428 pad_left=pad_left)
427 self.prompt2 = Prompt2(self,prompt=self.ps2_str,pad_left=pad_left)
429 self.prompt2 = Prompt2(self,prompt=self.ps2_str,pad_left=pad_left)
428 self.prompt_out = PromptOut(self,sep='',prompt=self.ps_out_str,
430 self.prompt_out = PromptOut(self,sep='',prompt=self.ps_out_str,
429 pad_left=pad_left)
431 pad_left=pad_left)
430 self.set_colors(colors)
432 self.set_colors(colors)
431
433
432 # other more normal stuff
434 # other more normal stuff
433 # b/c each call to the In[] prompt raises it by 1, even the first.
435 # b/c each call to the In[] prompt raises it by 1, even the first.
434 self.prompt_count = 0
436 self.prompt_count = 0
435 self.cache_count = 1
437 self.cache_count = 1
436 # Store the last prompt string each time, we need it for aligning
438 # Store the last prompt string each time, we need it for aligning
437 # continuation and auto-rewrite prompts
439 # continuation and auto-rewrite prompts
438 self.last_prompt = ''
440 self.last_prompt = ''
439 self.entries = [None] # output counter starts at 1 for the user
441 self.entries = [None] # output counter starts at 1 for the user
440 self.Pprint = Pprint
442 self.Pprint = Pprint
441 self.output_sep = output_sep
443 self.output_sep = output_sep
442 self.output_sep2 = output_sep2
444 self.output_sep2 = output_sep2
443 self._,self.__,self.___ = '','',''
445 self._,self.__,self.___ = '','',''
444 self.pprint_types = map(type,[(),[],{}])
446 self.pprint_types = map(type,[(),[],{}])
445
447
446 # these are deliberately global:
448 # these are deliberately global:
447 to_user_ns = {'_':self._,'__':self.__,'___':self.___}
449 to_user_ns = {'_':self._,'__':self.__,'___':self.___}
448 self.user_ns.update(to_user_ns)
450 self.user_ns.update(to_user_ns)
449
451
450 def _set_prompt_str(self,p_str,cache_def,no_cache_def):
452 def _set_prompt_str(self,p_str,cache_def,no_cache_def):
451 if p_str is None:
453 if p_str is None:
452 if self.do_full_cache:
454 if self.do_full_cache:
453 return cache_def
455 return cache_def
454 else:
456 else:
455 return no_cache_def
457 return no_cache_def
456 else:
458 else:
457 return p_str
459 return p_str
458
460
459 def set_colors(self,colors):
461 def set_colors(self,colors):
460 """Set the active color scheme and configure colors for the three
462 """Set the active color scheme and configure colors for the three
461 prompt subsystems."""
463 prompt subsystems."""
462
464
463 # FIXME: the prompt_specials global should be gobbled inside this
465 # FIXME: the prompt_specials global should be gobbled inside this
464 # class instead. Do it when cleaning up the whole 3-prompt system.
466 # class instead. Do it when cleaning up the whole 3-prompt system.
465 global prompt_specials
467 global prompt_specials
466 if colors.lower()=='nocolor':
468 if colors.lower()=='nocolor':
467 prompt_specials = prompt_specials_nocolor
469 prompt_specials = prompt_specials_nocolor
468 else:
470 else:
469 prompt_specials = prompt_specials_color
471 prompt_specials = prompt_specials_color
470
472
471 self.color_table.set_active_scheme(colors)
473 self.color_table.set_active_scheme(colors)
472 self.prompt1.set_colors()
474 self.prompt1.set_colors()
473 self.prompt2.set_colors()
475 self.prompt2.set_colors()
474 self.prompt_out.set_colors()
476 self.prompt_out.set_colors()
475
477
476 def __call__(self,arg=None):
478 def __call__(self,arg=None):
477 """Printing with history cache management.
479 """Printing with history cache management.
478
480
479 This is invoked everytime the interpreter needs to print, and is
481 This is invoked everytime the interpreter needs to print, and is
480 activated by setting the variable sys.displayhook to it."""
482 activated by setting the variable sys.displayhook to it."""
481
483
482 # If something injected a '_' variable in __builtin__, delete
484 # If something injected a '_' variable in __builtin__, delete
483 # ipython's automatic one so we don't clobber that. gettext() in
485 # ipython's automatic one so we don't clobber that. gettext() in
484 # particular uses _, so we need to stay away from it.
486 # particular uses _, so we need to stay away from it.
485 if '_' in __builtin__.__dict__:
487 if '_' in __builtin__.__dict__:
486 try:
488 try:
487 del self.user_ns['_']
489 del self.user_ns['_']
488 except KeyError:
490 except KeyError:
489 pass
491 pass
490 if arg is not None:
492 if arg is not None:
491 cout_write = Term.cout.write # fast lookup
493 cout_write = Term.cout.write # fast lookup
492 # first handle the cache and counters
494 # first handle the cache and counters
493 self.update(arg)
495 self.update(arg)
494 # do not print output if input ends in ';'
496 # do not print output if input ends in ';'
495 if self.input_hist[self.prompt_count].endswith(';\n'):
497 if self.input_hist[self.prompt_count].endswith(';\n'):
496 return
498 return
497 # don't use print, puts an extra space
499 # don't use print, puts an extra space
498 cout_write(self.output_sep)
500 cout_write(self.output_sep)
499 if self.do_full_cache:
501 if self.do_full_cache:
500 cout_write(str(self.prompt_out))
502 cout_write(str(self.prompt_out))
501
503
502 if isinstance(arg,Macro):
504 if isinstance(arg,Macro):
503 print 'Executing Macro...'
505 print 'Executing Macro...'
504 # in case the macro takes a long time to execute
506 # in case the macro takes a long time to execute
505 Term.cout.flush()
507 Term.cout.flush()
506 exec arg.value in self.user_ns
508 exec arg.value in self.user_ns
507 return None
509 return None
508
510
509 # and now call a possibly user-defined print mechanism
511 # and now call a possibly user-defined print mechanism
510 self.display(arg)
512 self.display(arg)
511 cout_write(self.output_sep2)
513 cout_write(self.output_sep2)
512 Term.cout.flush()
514 Term.cout.flush()
513
515
514 def _display(self,arg):
516 def _display(self,arg):
515 """Default printer method, uses pprint.
517 """Default printer method, uses pprint.
516
518
517 This can be over-ridden by the users to implement special formatting
519 This can be over-ridden by the users to implement special formatting
518 of certain types of output."""
520 of certain types of output."""
519
521
520 if self.Pprint:
522 if self.Pprint:
521 out = pformat(arg)
523 out = pformat(arg)
522 if '\n' in out:
524 if '\n' in out:
523 # So that multi-line strings line up with the left column of
525 # So that multi-line strings line up with the left column of
524 # the screen, instead of having the output prompt mess up
526 # the screen, instead of having the output prompt mess up
525 # their first line.
527 # their first line.
526 Term.cout.write('\n')
528 Term.cout.write('\n')
527 print >>Term.cout, out
529 print >>Term.cout, out
528 else:
530 else:
529 print >>Term.cout, arg
531 print >>Term.cout, arg
530
532
531 # Assign the default display method:
533 # Assign the default display method:
532 display = _display
534 display = _display
533
535
534 def update(self,arg):
536 def update(self,arg):
535 #print '***cache_count', self.cache_count # dbg
537 #print '***cache_count', self.cache_count # dbg
536 if self.cache_count >= self.cache_size and self.do_full_cache:
538 if self.cache_count >= self.cache_size and self.do_full_cache:
537 self.flush()
539 self.flush()
538 # Don't overwrite '_' and friends if '_' is in __builtin__ (otherwise
540 # Don't overwrite '_' and friends if '_' is in __builtin__ (otherwise
539 # we cause buggy behavior for things like gettext).
541 # we cause buggy behavior for things like gettext).
540 if '_' not in __builtin__.__dict__:
542 if '_' not in __builtin__.__dict__:
541 self.___ = self.__
543 self.___ = self.__
542 self.__ = self._
544 self.__ = self._
543 self._ = arg
545 self._ = arg
544 self.user_ns.update({'_':self._,'__':self.__,'___':self.___})
546 self.user_ns.update({'_':self._,'__':self.__,'___':self.___})
545
547
546 # hackish access to top-level namespace to create _1,_2... dynamically
548 # hackish access to top-level namespace to create _1,_2... dynamically
547 to_main = {}
549 to_main = {}
548 if self.do_full_cache:
550 if self.do_full_cache:
549 self.cache_count += 1
551 self.cache_count += 1
550 self.entries.append(arg)
552 self.entries.append(arg)
551 new_result = '_'+`self.prompt_count`
553 new_result = '_'+`self.prompt_count`
552 to_main[new_result] = self.entries[-1]
554 to_main[new_result] = self.entries[-1]
553 self.user_ns.update(to_main)
555 self.user_ns.update(to_main)
554 self.user_ns['_oh'][self.prompt_count] = arg
556 self.user_ns['_oh'][self.prompt_count] = arg
555
557
556 def flush(self):
558 def flush(self):
557 if not self.do_full_cache:
559 if not self.do_full_cache:
558 raise ValueError,"You shouldn't have reached the cache flush "\
560 raise ValueError,"You shouldn't have reached the cache flush "\
559 "if full caching is not enabled!"
561 "if full caching is not enabled!"
560 warn('Output cache limit (currently '+\
562 warn('Output cache limit (currently '+\
561 `self.cache_count`+' entries) hit.\n'
563 `self.cache_count`+' entries) hit.\n'
562 'Flushing cache and resetting history counter...\n'
564 'Flushing cache and resetting history counter...\n'
563 'The only history variables available will be _,__,___ and _1\n'
565 'The only history variables available will be _,__,___ and _1\n'
564 'with the current result.')
566 'with the current result.')
565 # delete auto-generated vars from global namespace
567 # delete auto-generated vars from global namespace
566 for n in range(1,self.prompt_count + 1):
568 for n in range(1,self.prompt_count + 1):
567 key = '_'+`n`
569 key = '_'+`n`
568 try:
570 try:
569 del self.user_ns[key]
571 del self.user_ns[key]
570 except: pass
572 except: pass
571 self.prompt_count = 1
573 self.prompt_count = 1
572 self.cache_count = 1
574 self.cache_count = 1
@@ -1,74 +1,76 b''
1 # -*- coding: utf-8 -*-
1 # -*- coding: utf-8 -*-
2 """Release data for the IPython project.
2 """Release data for the IPython project.
3
3
4 $Id: Release.py 775 2005-09-01 20:24:59Z fperez $"""
4 $Id: Release.py 951 2005-12-25 00:57:24Z fperez $"""
5
5
6 #*****************************************************************************
6 #*****************************************************************************
7 # Copyright (C) 2001-2005 Fernando Perez <fperez@colorado.edu>
7 # Copyright (C) 2001-2005 Fernando Perez <fperez@colorado.edu>
8 #
8 #
9 # Copyright (c) 2001 Janko Hauser <jhauser@zscout.de> and Nathaniel Gray
9 # Copyright (c) 2001 Janko Hauser <jhauser@zscout.de> and Nathaniel Gray
10 # <n8gray@caltech.edu>
10 # <n8gray@caltech.edu>
11 #
11 #
12 # Distributed under the terms of the BSD License. The full license is in
12 # Distributed under the terms of the BSD License. The full license is in
13 # the file COPYING, distributed as part of this software.
13 # the file COPYING, distributed as part of this software.
14 #*****************************************************************************
14 #*****************************************************************************
15
15
16 # Name of the package for release purposes. This is the name which labels
16 # Name of the package for release purposes. This is the name which labels
17 # the tarballs and RPMs made by distutils, so it's best to lowercase it.
17 # the tarballs and RPMs made by distutils, so it's best to lowercase it.
18 name = 'ipython'
18 name = 'ipython'
19
19
20 # For versions with substrings (like 0.6.16.svn), use an extra . to separate
20 # For versions with substrings (like 0.6.16.svn), use an extra . to separate
21 # the new substring. We have to avoid using either dashes or underscores,
21 # the new substring. We have to avoid using either dashes or underscores,
22 # because bdist_rpm does not accept dashes (an RPM) convention, and
22 # because bdist_rpm does not accept dashes (an RPM) convention, and
23 # bdist_deb does not accept underscores (a Debian convention).
23 # bdist_deb does not accept underscores (a Debian convention).
24
24
25 version = '0.6.16.svn'
25 version = '0.6.16.svn'
26
26
27 revision = '$Revision: 775 $'
27 revision = '$Revision: 951 $'
28
28
29 description = "An enhanced interactive Python shell."
29 description = "An enhanced interactive Python shell."
30
30
31 long_description = \
31 long_description = \
32 """
32 """
33 IPython provides a replacement for the interactive Python interpreter with
33 IPython provides a replacement for the interactive Python interpreter with
34 extra functionality.
34 extra functionality.
35
35
36 Main features:
36 Main features:
37
37
38 * Comprehensive object introspection.
38 * Comprehensive object introspection.
39
39
40 * Input history, persistent across sessions.
40 * Input history, persistent across sessions.
41
41
42 * Caching of output results during a session with automatically generated
42 * Caching of output results during a session with automatically generated
43 references.
43 references.
44
44
45 * Readline based name completion.
45 * Readline based name completion.
46
46
47 * Extensible system of 'magic' commands for controlling the environment and
47 * Extensible system of 'magic' commands for controlling the environment and
48 performing many tasks related either to IPython or the operating system.
48 performing many tasks related either to IPython or the operating system.
49
49
50 * Configuration system with easy switching between different setups (simpler
50 * Configuration system with easy switching between different setups (simpler
51 than changing $PYTHONSTARTUP environment variables every time).
51 than changing $PYTHONSTARTUP environment variables every time).
52
52
53 * Session logging and reloading.
53 * Session logging and reloading.
54
54
55 * Extensible syntax processing for special purpose situations.
55 * Extensible syntax processing for special purpose situations.
56
56
57 * Access to the system shell with user-extensible alias system.
57 * Access to the system shell with user-extensible alias system.
58
58
59 * Easily embeddable in other Python programs.
59 * Easily embeddable in other Python programs.
60
60
61 * Integrated access to the pdb debugger and the Python profiler. """
61 * Integrated access to the pdb debugger and the Python profiler. """
62
62
63 license = 'BSD'
63 license = 'BSD'
64
64
65 authors = {'Fernando' : ('Fernando Perez','fperez@colorado.edu'),
65 authors = {'Fernando' : ('Fernando Perez','fperez@colorado.edu'),
66 'Janko' : ('Janko Hauser','jhauser@zscout.de'),
66 'Janko' : ('Janko Hauser','jhauser@zscout.de'),
67 'Nathan' : ('Nathaniel Gray','n8gray@caltech.edu')
67 'Nathan' : ('Nathaniel Gray','n8gray@caltech.edu')
68 }
68 }
69
69
70 url = 'http://ipython.scipy.org'
70 url = 'http://ipython.scipy.org'
71
71
72 download_url = 'http://ipython.scipy.org/dist'
73
72 platforms = ['Linux','Mac OSX','Windows XP/2000/NT','Windows 95/98/ME']
74 platforms = ['Linux','Mac OSX','Windows XP/2000/NT','Windows 95/98/ME']
73
75
74 keywords = ['Interactive','Interpreter','Shell']
76 keywords = ['Interactive','Interpreter','Shell']
@@ -1,2104 +1,2112 b''
1 # -*- coding: utf-8 -*-
1 # -*- coding: utf-8 -*-
2 """
2 """
3 IPython -- An enhanced Interactive Python
3 IPython -- An enhanced Interactive Python
4
4
5 Requires Python 2.1 or newer.
5 Requires Python 2.1 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 924 2005-11-15 20:24:31Z fperez $
9 $Id: iplib.py 951 2005-12-25 00:57:24Z fperez $
10 """
10 """
11
11
12 #*****************************************************************************
12 #*****************************************************************************
13 # Copyright (C) 2001 Janko Hauser <jhauser@zscout.de> and
13 # Copyright (C) 2001 Janko Hauser <jhauser@zscout.de> and
14 # Copyright (C) 2001-2004 Fernando Perez. <fperez@colorado.edu>
14 # Copyright (C) 2001-2004 Fernando Perez. <fperez@colorado.edu>
15 #
15 #
16 # Distributed under the terms of the BSD License. The full license is in
16 # Distributed under the terms of the BSD License. The full license is in
17 # the file COPYING, distributed as part of this software.
17 # the file COPYING, distributed as part of this software.
18 #
18 #
19 # Note: this code originally subclassed code.InteractiveConsole from the
19 # Note: this code originally subclassed code.InteractiveConsole from the
20 # Python standard library. Over time, much of that class has been copied
20 # Python standard library. Over time, much of that class has been copied
21 # verbatim here for modifications which could not be accomplished by
21 # verbatim here for modifications which could not be accomplished by
22 # subclassing. The Python License (sec. 2) allows for this, but it's always
22 # subclassing. The Python License (sec. 2) allows for this, but it's always
23 # nice to acknowledge credit where credit is due.
23 # nice to acknowledge credit where credit is due.
24 #*****************************************************************************
24 #*****************************************************************************
25
25
26 #****************************************************************************
26 #****************************************************************************
27 # Modules and globals
27 # Modules and globals
28
28
29 from __future__ import generators # for 2.2 backwards-compatibility
29 from __future__ import generators # for 2.2 backwards-compatibility
30
30
31 from IPython import Release
31 from IPython import Release
32 __author__ = '%s <%s>\n%s <%s>' % \
32 __author__ = '%s <%s>\n%s <%s>' % \
33 ( Release.authors['Janko'] + Release.authors['Fernando'] )
33 ( Release.authors['Janko'] + Release.authors['Fernando'] )
34 __license__ = Release.license
34 __license__ = Release.license
35 __version__ = Release.version
35 __version__ = Release.version
36
36
37 # Python standard modules
37 # Python standard modules
38 import __main__
38 import __main__
39 import __builtin__
39 import __builtin__
40 import exceptions
40 import exceptions
41 import keyword
41 import keyword
42 import new
42 import new
43 import os, sys, shutil
43 import os, sys, shutil
44 import code, glob, types, re
44 import code, glob, types, re
45 import string, StringIO
45 import string, StringIO
46 import inspect, pydoc
46 import inspect, pydoc
47 import bdb, pdb
47 import bdb, pdb
48 import UserList # don't subclass list so this works with Python2.1
48 import UserList # don't subclass list so this works with Python2.1
49 from pprint import pprint, pformat
49 from pprint import pprint, pformat
50 import cPickle as pickle
50 import cPickle as pickle
51 import traceback
51 import traceback
52 from codeop import CommandCompiler
52 from codeop import CommandCompiler
53
53
54 # IPython's own modules
54 # IPython's own modules
55 import IPython
55 import IPython
56 from IPython import OInspect,PyColorize,ultraTB
56 from IPython import OInspect,PyColorize,ultraTB
57 from IPython.ultraTB import ColorScheme,ColorSchemeTable # too long names
57 from IPython.ColorANSI import ColorScheme,ColorSchemeTable # too long names
58 from IPython.Logger import Logger
58 from IPython.Logger import Logger
59 from IPython.Magic import Magic,magic2python,shlex_split
59 from IPython.Magic import Magic,magic2python,shlex_split
60 from IPython.usage import cmd_line_usage,interactive_usage
60 from IPython.usage import cmd_line_usage,interactive_usage
61 from IPython.Struct import Struct
61 from IPython.Struct import Struct
62 from IPython.Itpl import Itpl,itpl,printpl,ItplNS,itplns
62 from IPython.Itpl import Itpl,itpl,printpl,ItplNS,itplns
63 from IPython.FakeModule import FakeModule
63 from IPython.FakeModule import FakeModule
64 from IPython.background_jobs import BackgroundJobManager
64 from IPython.background_jobs import BackgroundJobManager
65 from IPython.PyColorize import Parser
65 from IPython.PyColorize import Parser
66 from IPython.genutils import *
66 from IPython.genutils import *
67
67
68 # Global pointer to the running
68 # Global pointer to the running
69
69
70 # store the builtin raw_input globally, and use this always, in case user code
70 # store the builtin raw_input globally, and use this always, in case user code
71 # overwrites it (like wx.py.PyShell does)
71 # overwrites it (like wx.py.PyShell does)
72 raw_input_original = raw_input
72 raw_input_original = raw_input
73
73
74 #****************************************************************************
74 #****************************************************************************
75 # Some utility function definitions
75 # Some utility function definitions
76
76
77 class Bunch: pass
77 class Bunch: pass
78
78
79 def esc_quotes(strng):
79 def esc_quotes(strng):
80 """Return the input string with single and double quotes escaped out"""
80 """Return the input string with single and double quotes escaped out"""
81
81
82 return strng.replace('"','\\"').replace("'","\\'")
82 return strng.replace('"','\\"').replace("'","\\'")
83
83
84 def import_fail_info(mod_name,fns=None):
84 def import_fail_info(mod_name,fns=None):
85 """Inform load failure for a module."""
85 """Inform load failure for a module."""
86
86
87 if fns == None:
87 if fns == None:
88 warn("Loading of %s failed.\n" % (mod_name,))
88 warn("Loading of %s failed.\n" % (mod_name,))
89 else:
89 else:
90 warn("Loading of %s from %s failed.\n" % (fns,mod_name))
90 warn("Loading of %s from %s failed.\n" % (fns,mod_name))
91
91
92 def qw_lol(indata):
92 def qw_lol(indata):
93 """qw_lol('a b') -> [['a','b']],
93 """qw_lol('a b') -> [['a','b']],
94 otherwise it's just a call to qw().
94 otherwise it's just a call to qw().
95
95
96 We need this to make sure the modules_some keys *always* end up as a
96 We need this to make sure the modules_some keys *always* end up as a
97 list of lists."""
97 list of lists."""
98
98
99 if type(indata) in StringTypes:
99 if type(indata) in StringTypes:
100 return [qw(indata)]
100 return [qw(indata)]
101 else:
101 else:
102 return qw(indata)
102 return qw(indata)
103
103
104 def ipmagic(arg_s):
104 def ipmagic(arg_s):
105 """Call a magic function by name.
105 """Call a magic function by name.
106
106
107 Input: a string containing the name of the magic function to call and any
107 Input: a string containing the name of the magic function to call and any
108 additional arguments to be passed to the magic.
108 additional arguments to be passed to the magic.
109
109
110 ipmagic('name -opt foo bar') is equivalent to typing at the ipython
110 ipmagic('name -opt foo bar') is equivalent to typing at the ipython
111 prompt:
111 prompt:
112
112
113 In[1]: %name -opt foo bar
113 In[1]: %name -opt foo bar
114
114
115 To call a magic without arguments, simply use ipmagic('name').
115 To call a magic without arguments, simply use ipmagic('name').
116
116
117 This provides a proper Python function to call IPython's magics in any
117 This provides a proper Python function to call IPython's magics in any
118 valid Python code you can type at the interpreter, including loops and
118 valid Python code you can type at the interpreter, including loops and
119 compound statements. It is added by IPython to the Python builtin
119 compound statements. It is added by IPython to the Python builtin
120 namespace upon initialization."""
120 namespace upon initialization."""
121
121
122 args = arg_s.split(' ',1)
122 args = arg_s.split(' ',1)
123 magic_name = args[0]
123 magic_name = args[0]
124 if magic_name.startswith(__IPYTHON__.ESC_MAGIC):
124 if magic_name.startswith(__IPYTHON__.ESC_MAGIC):
125 magic_name = magic_name[1:]
125 magic_name = magic_name[1:]
126 try:
126 try:
127 magic_args = args[1]
127 magic_args = args[1]
128 except IndexError:
128 except IndexError:
129 magic_args = ''
129 magic_args = ''
130 fn = getattr(__IPYTHON__,'magic_'+magic_name,None)
130 fn = getattr(__IPYTHON__,'magic_'+magic_name,None)
131 if fn is None:
131 if fn is None:
132 error("Magic function `%s` not found." % magic_name)
132 error("Magic function `%s` not found." % magic_name)
133 else:
133 else:
134 magic_args = __IPYTHON__.var_expand(magic_args)
134 magic_args = __IPYTHON__.var_expand(magic_args)
135 return fn(magic_args)
135 return fn(magic_args)
136
136
137 def ipalias(arg_s):
137 def ipalias(arg_s):
138 """Call an alias by name.
138 """Call an alias by name.
139
139
140 Input: a string containing the name of the alias to call and any
140 Input: a string containing the name of the alias to call and any
141 additional arguments to be passed to the magic.
141 additional arguments to be passed to the magic.
142
142
143 ipalias('name -opt foo bar') is equivalent to typing at the ipython
143 ipalias('name -opt foo bar') is equivalent to typing at the ipython
144 prompt:
144 prompt:
145
145
146 In[1]: name -opt foo bar
146 In[1]: name -opt foo bar
147
147
148 To call an alias without arguments, simply use ipalias('name').
148 To call an alias without arguments, simply use ipalias('name').
149
149
150 This provides a proper Python function to call IPython's aliases in any
150 This provides a proper Python function to call IPython's aliases in any
151 valid Python code you can type at the interpreter, including loops and
151 valid Python code you can type at the interpreter, including loops and
152 compound statements. It is added by IPython to the Python builtin
152 compound statements. It is added by IPython to the Python builtin
153 namespace upon initialization."""
153 namespace upon initialization."""
154
154
155 args = arg_s.split(' ',1)
155 args = arg_s.split(' ',1)
156 alias_name = args[0]
156 alias_name = args[0]
157 try:
157 try:
158 alias_args = args[1]
158 alias_args = args[1]
159 except IndexError:
159 except IndexError:
160 alias_args = ''
160 alias_args = ''
161 if alias_name in __IPYTHON__.alias_table:
161 if alias_name in __IPYTHON__.alias_table:
162 __IPYTHON__.call_alias(alias_name,alias_args)
162 __IPYTHON__.call_alias(alias_name,alias_args)
163 else:
163 else:
164 error("Alias `%s` not found." % alias_name)
164 error("Alias `%s` not found." % alias_name)
165
165
166 #-----------------------------------------------------------------------------
166 #-----------------------------------------------------------------------------
167 # Local use classes
167 # Local use classes
168 try:
168 try:
169 from IPython import FlexCompleter
169 from IPython import FlexCompleter
170
170
171 class MagicCompleter(FlexCompleter.Completer):
171 class MagicCompleter(FlexCompleter.Completer):
172 """Extension of the completer class to work on %-prefixed lines."""
172 """Extension of the completer class to work on %-prefixed lines."""
173
173
174 def __init__(self,shell,namespace=None,omit__names=0,alias_table=None):
174 def __init__(self,shell,namespace=None,omit__names=0,alias_table=None):
175 """MagicCompleter() -> completer
175 """MagicCompleter() -> completer
176
176
177 Return a completer object suitable for use by the readline library
177 Return a completer object suitable for use by the readline library
178 via readline.set_completer().
178 via readline.set_completer().
179
179
180 Inputs:
180 Inputs:
181
181
182 - shell: a pointer to the ipython shell itself. This is needed
182 - shell: a pointer to the ipython shell itself. This is needed
183 because this completer knows about magic functions, and those can
183 because this completer knows about magic functions, and those can
184 only be accessed via the ipython instance.
184 only be accessed via the ipython instance.
185
185
186 - namespace: an optional dict where completions are performed.
186 - namespace: an optional dict where completions are performed.
187
187
188 - The optional omit__names parameter sets the completer to omit the
188 - The optional omit__names parameter sets the completer to omit the
189 'magic' names (__magicname__) for python objects unless the text
189 'magic' names (__magicname__) for python objects unless the text
190 to be completed explicitly starts with one or more underscores.
190 to be completed explicitly starts with one or more underscores.
191
191
192 - If alias_table is supplied, it should be a dictionary of aliases
192 - If alias_table is supplied, it should be a dictionary of aliases
193 to complete. """
193 to complete. """
194
194
195 FlexCompleter.Completer.__init__(self,namespace)
195 FlexCompleter.Completer.__init__(self,namespace)
196 self.magic_prefix = shell.name+'.magic_'
196 self.magic_prefix = shell.name+'.magic_'
197 self.magic_escape = shell.ESC_MAGIC
197 self.magic_escape = shell.ESC_MAGIC
198 self.readline = FlexCompleter.readline
198 self.readline = FlexCompleter.readline
199 delims = self.readline.get_completer_delims()
199 delims = self.readline.get_completer_delims()
200 delims = delims.replace(self.magic_escape,'')
200 delims = delims.replace(self.magic_escape,'')
201 self.readline.set_completer_delims(delims)
201 self.readline.set_completer_delims(delims)
202 self.get_line_buffer = self.readline.get_line_buffer
202 self.get_line_buffer = self.readline.get_line_buffer
203 self.omit__names = omit__names
203 self.omit__names = omit__names
204 self.merge_completions = shell.rc.readline_merge_completions
204 self.merge_completions = shell.rc.readline_merge_completions
205
205
206 if alias_table is None:
206 if alias_table is None:
207 alias_table = {}
207 alias_table = {}
208 self.alias_table = alias_table
208 self.alias_table = alias_table
209 # Regexp to split filenames with spaces in them
209 # Regexp to split filenames with spaces in them
210 self.space_name_re = re.compile(r'([^\\] )')
210 self.space_name_re = re.compile(r'([^\\] )')
211 # Hold a local ref. to glob.glob for speed
211 # Hold a local ref. to glob.glob for speed
212 self.glob = glob.glob
212 self.glob = glob.glob
213 # Special handling of backslashes needed in win32 platforms
213 # Special handling of backslashes needed in win32 platforms
214 if sys.platform == "win32":
214 if sys.platform == "win32":
215 self.clean_glob = self._clean_glob_win32
215 self.clean_glob = self._clean_glob_win32
216 else:
216 else:
217 self.clean_glob = self._clean_glob
217 self.clean_glob = self._clean_glob
218 self.matchers = [self.python_matches,
218 self.matchers = [self.python_matches,
219 self.file_matches,
219 self.file_matches,
220 self.alias_matches,
220 self.alias_matches,
221 self.python_func_kw_matches]
221 self.python_func_kw_matches]
222
222
223 # Code contributed by Alex Schmolck, for ipython/emacs integration
223 # Code contributed by Alex Schmolck, for ipython/emacs integration
224 def all_completions(self, text):
224 def all_completions(self, text):
225 """Return all possible completions for the benefit of emacs."""
225 """Return all possible completions for the benefit of emacs."""
226
226
227 completions = []
227 completions = []
228 try:
228 try:
229 for i in xrange(sys.maxint):
229 for i in xrange(sys.maxint):
230 res = self.complete(text, i)
230 res = self.complete(text, i)
231
231
232 if not res: break
232 if not res: break
233
233
234 completions.append(res)
234 completions.append(res)
235 #XXX workaround for ``notDefined.<tab>``
235 #XXX workaround for ``notDefined.<tab>``
236 except NameError:
236 except NameError:
237 pass
237 pass
238 return completions
238 return completions
239 # /end Alex Schmolck code.
239 # /end Alex Schmolck code.
240
240
241 def _clean_glob(self,text):
241 def _clean_glob(self,text):
242 return self.glob("%s*" % text)
242 return self.glob("%s*" % text)
243
243
244 def _clean_glob_win32(self,text):
244 def _clean_glob_win32(self,text):
245 return [f.replace("\\","/")
245 return [f.replace("\\","/")
246 for f in self.glob("%s*" % text)]
246 for f in self.glob("%s*" % text)]
247
247
248 def file_matches(self, text):
248 def file_matches(self, text):
249 """Match filneames, expanding ~USER type strings.
249 """Match filneames, expanding ~USER type strings.
250
250
251 Most of the seemingly convoluted logic in this completer is an
251 Most of the seemingly convoluted logic in this completer is an
252 attempt to handle filenames with spaces in them. And yet it's not
252 attempt to handle filenames with spaces in them. And yet it's not
253 quite perfect, because Python's readline doesn't expose all of the
253 quite perfect, because Python's readline doesn't expose all of the
254 GNU readline details needed for this to be done correctly.
254 GNU readline details needed for this to be done correctly.
255
255
256 For a filename with a space in it, the printed completions will be
256 For a filename with a space in it, the printed completions will be
257 only the parts after what's already been typed (instead of the
257 only the parts after what's already been typed (instead of the
258 full completions, as is normally done). I don't think with the
258 full completions, as is normally done). I don't think with the
259 current (as of Python 2.3) Python readline it's possible to do
259 current (as of Python 2.3) Python readline it's possible to do
260 better."""
260 better."""
261
261
262 #print 'Completer->file_matches: <%s>' % text # dbg
262 #print 'Completer->file_matches: <%s>' % text # dbg
263
263
264 # chars that require escaping with backslash - i.e. chars
264 # chars that require escaping with backslash - i.e. chars
265 # that readline treats incorrectly as delimiters, but we
265 # that readline treats incorrectly as delimiters, but we
266 # don't want to treat as delimiters in filename matching
266 # don't want to treat as delimiters in filename matching
267 # when escaped with backslash
267 # when escaped with backslash
268
268
269 protectables = ' ()[]{}'
269 protectables = ' ()[]{}'
270
270
271 def protect_filename(s):
271 def protect_filename(s):
272 return "".join([(ch in protectables and '\\' + ch or ch)
272 return "".join([(ch in protectables and '\\' + ch or ch)
273 for ch in s])
273 for ch in s])
274
274
275 lbuf = self.get_line_buffer()[:self.readline.get_endidx()]
275 lbuf = self.get_line_buffer()[:self.readline.get_endidx()]
276 open_quotes = 0 # track strings with open quotes
276 open_quotes = 0 # track strings with open quotes
277 try:
277 try:
278 lsplit = shlex_split(lbuf)[-1]
278 lsplit = shlex_split(lbuf)[-1]
279 except ValueError:
279 except ValueError:
280 # typically an unmatched ", or backslash without escaped char.
280 # typically an unmatched ", or backslash without escaped char.
281 if lbuf.count('"')==1:
281 if lbuf.count('"')==1:
282 open_quotes = 1
282 open_quotes = 1
283 lsplit = lbuf.split('"')[-1]
283 lsplit = lbuf.split('"')[-1]
284 elif lbuf.count("'")==1:
284 elif lbuf.count("'")==1:
285 open_quotes = 1
285 open_quotes = 1
286 lsplit = lbuf.split("'")[-1]
286 lsplit = lbuf.split("'")[-1]
287 else:
287 else:
288 return None
288 return None
289 except IndexError:
289 except IndexError:
290 # tab pressed on empty line
290 # tab pressed on empty line
291 lsplit = ""
291 lsplit = ""
292
292
293 if lsplit != protect_filename(lsplit):
293 if lsplit != protect_filename(lsplit):
294 # if protectables are found, do matching on the whole escaped
294 # if protectables are found, do matching on the whole escaped
295 # name
295 # name
296 has_protectables = 1
296 has_protectables = 1
297 text0,text = text,lsplit
297 text0,text = text,lsplit
298 else:
298 else:
299 has_protectables = 0
299 has_protectables = 0
300 text = os.path.expanduser(text)
300 text = os.path.expanduser(text)
301
301
302 if text == "":
302 if text == "":
303 return [protect_filename(f) for f in self.glob("*")]
303 return [protect_filename(f) for f in self.glob("*")]
304
304
305 m0 = self.clean_glob(text.replace('\\',''))
305 m0 = self.clean_glob(text.replace('\\',''))
306 if has_protectables:
306 if has_protectables:
307 # If we had protectables, we need to revert our changes to the
307 # If we had protectables, we need to revert our changes to the
308 # beginning of filename so that we don't double-write the part
308 # beginning of filename so that we don't double-write the part
309 # of the filename we have so far
309 # of the filename we have so far
310 len_lsplit = len(lsplit)
310 len_lsplit = len(lsplit)
311 matches = [text0 + protect_filename(f[len_lsplit:]) for f in m0]
311 matches = [text0 + protect_filename(f[len_lsplit:]) for f in m0]
312 else:
312 else:
313 if open_quotes:
313 if open_quotes:
314 # if we have a string with an open quote, we don't need to
314 # if we have a string with an open quote, we don't need to
315 # protect the names at all (and we _shouldn't_, as it
315 # protect the names at all (and we _shouldn't_, as it
316 # would cause bugs when the filesystem call is made).
316 # would cause bugs when the filesystem call is made).
317 matches = m0
317 matches = m0
318 else:
318 else:
319 matches = [protect_filename(f) for f in m0]
319 matches = [protect_filename(f) for f in m0]
320 if len(matches) == 1 and os.path.isdir(matches[0]):
320 if len(matches) == 1 and os.path.isdir(matches[0]):
321 # Takes care of links to directories also. Use '/'
321 # Takes care of links to directories also. Use '/'
322 # explicitly, even under Windows, so that name completions
322 # explicitly, even under Windows, so that name completions
323 # don't end up escaped.
323 # don't end up escaped.
324 matches[0] += '/'
324 matches[0] += '/'
325 return matches
325 return matches
326
326
327 def alias_matches(self, text):
327 def alias_matches(self, text):
328 """Match internal system aliases"""
328 """Match internal system aliases"""
329 #print 'Completer->alias_matches:',text # dbg
329 #print 'Completer->alias_matches:',text # dbg
330 text = os.path.expanduser(text)
330 text = os.path.expanduser(text)
331 aliases = self.alias_table.keys()
331 aliases = self.alias_table.keys()
332 if text == "":
332 if text == "":
333 return aliases
333 return aliases
334 else:
334 else:
335 return [alias for alias in aliases if alias.startswith(text)]
335 return [alias for alias in aliases if alias.startswith(text)]
336
336
337 def python_matches(self,text):
337 def python_matches(self,text):
338 """Match attributes or global python names"""
338 """Match attributes or global python names"""
339 #print 'Completer->python_matches' # dbg
339 #print 'Completer->python_matches' # dbg
340 if "." in text:
340 if "." in text:
341 try:
341 try:
342 matches = self.attr_matches(text)
342 matches = self.attr_matches(text)
343 if text.endswith('.') and self.omit__names:
343 if text.endswith('.') and self.omit__names:
344 if self.omit__names == 1:
344 if self.omit__names == 1:
345 # true if txt is _not_ a __ name, false otherwise:
345 # true if txt is _not_ a __ name, false otherwise:
346 no__name = (lambda txt:
346 no__name = (lambda txt:
347 re.match(r'.*\.__.*?__',txt) is None)
347 re.match(r'.*\.__.*?__',txt) is None)
348 else:
348 else:
349 # true if txt is _not_ a _ name, false otherwise:
349 # true if txt is _not_ a _ name, false otherwise:
350 no__name = (lambda txt:
350 no__name = (lambda txt:
351 re.match(r'.*\._.*?',txt) is None)
351 re.match(r'.*\._.*?',txt) is None)
352 matches = filter(no__name, matches)
352 matches = filter(no__name, matches)
353 except NameError:
353 except NameError:
354 # catches <undefined attributes>.<tab>
354 # catches <undefined attributes>.<tab>
355 matches = []
355 matches = []
356 else:
356 else:
357 matches = self.global_matches(text)
357 matches = self.global_matches(text)
358 # this is so completion finds magics when automagic is on:
358 # this is so completion finds magics when automagic is on:
359 if matches == [] and not text.startswith(os.sep):
359 if matches == [] and not text.startswith(os.sep):
360 matches = self.attr_matches(self.magic_prefix+text)
360 matches = self.attr_matches(self.magic_prefix+text)
361 return matches
361 return matches
362
362
363 def _default_arguments(self, obj):
363 def _default_arguments(self, obj):
364 """Return the list of default arguments of obj if it is callable,
364 """Return the list of default arguments of obj if it is callable,
365 or empty list otherwise."""
365 or empty list otherwise."""
366
366
367 if not (inspect.isfunction(obj) or inspect.ismethod(obj)):
367 if not (inspect.isfunction(obj) or inspect.ismethod(obj)):
368 # for classes, check for __init__,__new__
368 # for classes, check for __init__,__new__
369 if inspect.isclass(obj):
369 if inspect.isclass(obj):
370 obj = (getattr(obj,'__init__',None) or
370 obj = (getattr(obj,'__init__',None) or
371 getattr(obj,'__new__',None))
371 getattr(obj,'__new__',None))
372 # for all others, check if they are __call__able
372 # for all others, check if they are __call__able
373 elif hasattr(obj, '__call__'):
373 elif hasattr(obj, '__call__'):
374 obj = obj.__call__
374 obj = obj.__call__
375 # XXX: is there a way to handle the builtins ?
375 # XXX: is there a way to handle the builtins ?
376 try:
376 try:
377 args,_,_1,defaults = inspect.getargspec(obj)
377 args,_,_1,defaults = inspect.getargspec(obj)
378 if defaults:
378 if defaults:
379 return args[-len(defaults):]
379 return args[-len(defaults):]
380 except TypeError: pass
380 except TypeError: pass
381 return []
381 return []
382
382
383 def python_func_kw_matches(self,text):
383 def python_func_kw_matches(self,text):
384 """Match named parameters (kwargs) of the last open function"""
384 """Match named parameters (kwargs) of the last open function"""
385
385
386 if "." in text: # a parameter cannot be dotted
386 if "." in text: # a parameter cannot be dotted
387 return []
387 return []
388 try: regexp = self.__funcParamsRegex
388 try: regexp = self.__funcParamsRegex
389 except AttributeError:
389 except AttributeError:
390 regexp = self.__funcParamsRegex = re.compile(r'''
390 regexp = self.__funcParamsRegex = re.compile(r'''
391 '.*?' | # single quoted strings or
391 '.*?' | # single quoted strings or
392 ".*?" | # double quoted strings or
392 ".*?" | # double quoted strings or
393 \w+ | # identifier
393 \w+ | # identifier
394 \S # other characters
394 \S # other characters
395 ''', re.VERBOSE | re.DOTALL)
395 ''', re.VERBOSE | re.DOTALL)
396 # 1. find the nearest identifier that comes before an unclosed
396 # 1. find the nearest identifier that comes before an unclosed
397 # parenthesis e.g. for "foo (1+bar(x), pa", the candidate is "foo"
397 # parenthesis e.g. for "foo (1+bar(x), pa", the candidate is "foo"
398 tokens = regexp.findall(self.get_line_buffer())
398 tokens = regexp.findall(self.get_line_buffer())
399 tokens.reverse()
399 tokens.reverse()
400 iterTokens = iter(tokens); openPar = 0
400 iterTokens = iter(tokens); openPar = 0
401 for token in iterTokens:
401 for token in iterTokens:
402 if token == ')':
402 if token == ')':
403 openPar -= 1
403 openPar -= 1
404 elif token == '(':
404 elif token == '(':
405 openPar += 1
405 openPar += 1
406 if openPar > 0:
406 if openPar > 0:
407 # found the last unclosed parenthesis
407 # found the last unclosed parenthesis
408 break
408 break
409 else:
409 else:
410 return []
410 return []
411 # 2. Concatenate any dotted names (e.g. "foo.bar" for "foo.bar(x, pa" )
411 # 2. Concatenate any dotted names (e.g. "foo.bar" for "foo.bar(x, pa" )
412 ids = []
412 ids = []
413 isId = re.compile(r'\w+$').match
413 isId = re.compile(r'\w+$').match
414 while True:
414 while True:
415 try:
415 try:
416 ids.append(iterTokens.next())
416 ids.append(iterTokens.next())
417 if not isId(ids[-1]):
417 if not isId(ids[-1]):
418 ids.pop(); break
418 ids.pop(); break
419 if not iterTokens.next() == '.':
419 if not iterTokens.next() == '.':
420 break
420 break
421 except StopIteration:
421 except StopIteration:
422 break
422 break
423 # lookup the candidate callable matches either using global_matches
423 # lookup the candidate callable matches either using global_matches
424 # or attr_matches for dotted names
424 # or attr_matches for dotted names
425 if len(ids) == 1:
425 if len(ids) == 1:
426 callableMatches = self.global_matches(ids[0])
426 callableMatches = self.global_matches(ids[0])
427 else:
427 else:
428 callableMatches = self.attr_matches('.'.join(ids[::-1]))
428 callableMatches = self.attr_matches('.'.join(ids[::-1]))
429 argMatches = []
429 argMatches = []
430 for callableMatch in callableMatches:
430 for callableMatch in callableMatches:
431 try: namedArgs = self._default_arguments(eval(callableMatch,
431 try: namedArgs = self._default_arguments(eval(callableMatch,
432 self.namespace))
432 self.namespace))
433 except: continue
433 except: continue
434 for namedArg in namedArgs:
434 for namedArg in namedArgs:
435 if namedArg.startswith(text):
435 if namedArg.startswith(text):
436 argMatches.append("%s=" %namedArg)
436 argMatches.append("%s=" %namedArg)
437 return argMatches
437 return argMatches
438
438
439 def complete(self, text, state):
439 def complete(self, text, state):
440 """Return the next possible completion for 'text'.
440 """Return the next possible completion for 'text'.
441
441
442 This is called successively with state == 0, 1, 2, ... until it
442 This is called successively with state == 0, 1, 2, ... until it
443 returns None. The completion should begin with 'text'. """
443 returns None. The completion should begin with 'text'. """
444
444
445 #print '\n*** COMPLETE: <%s> (%s)' % (text,state) # dbg
445 #print '\n*** COMPLETE: <%s> (%s)' % (text,state) # dbg
446 magic_escape = self.magic_escape
446 magic_escape = self.magic_escape
447 magic_prefix = self.magic_prefix
447 magic_prefix = self.magic_prefix
448
448
449 try:
449 try:
450 if text.startswith(magic_escape):
450 if text.startswith(magic_escape):
451 text = text.replace(magic_escape,magic_prefix)
451 text = text.replace(magic_escape,magic_prefix)
452 elif text.startswith('~'):
452 elif text.startswith('~'):
453 text = os.path.expanduser(text)
453 text = os.path.expanduser(text)
454 if state == 0:
454 if state == 0:
455 # Extend the list of completions with the results of each
455 # Extend the list of completions with the results of each
456 # matcher, so we return results to the user from all
456 # matcher, so we return results to the user from all
457 # namespaces.
457 # namespaces.
458 if self.merge_completions:
458 if self.merge_completions:
459 self.matches = []
459 self.matches = []
460 for matcher in self.matchers:
460 for matcher in self.matchers:
461 self.matches.extend(matcher(text))
461 self.matches.extend(matcher(text))
462 else:
462 else:
463 for matcher in self.matchers:
463 for matcher in self.matchers:
464 self.matches = matcher(text)
464 self.matches = matcher(text)
465 if self.matches:
465 if self.matches:
466 break
466 break
467
467
468 try:
468 try:
469 return self.matches[state].replace(magic_prefix,magic_escape)
469 return self.matches[state].replace(magic_prefix,magic_escape)
470 except IndexError:
470 except IndexError:
471 return None
471 return None
472 except:
472 except:
473 # If completion fails, don't annoy the user.
473 # If completion fails, don't annoy the user.
474 pass
474 pass
475
475
476 except ImportError:
476 except ImportError:
477 pass # no readline support
477 pass # no readline support
478
478
479 except KeyError:
479 except KeyError:
480 pass # Windows doesn't set TERM, it doesn't matter
480 pass # Windows doesn't set TERM, it doesn't matter
481
481
482
482
483 class InputList(UserList.UserList):
483 class InputList(UserList.UserList):
484 """Class to store user input.
484 """Class to store user input.
485
485
486 It's basically a list, but slices return a string instead of a list, thus
486 It's basically a list, but slices return a string instead of a list, thus
487 allowing things like (assuming 'In' is an instance):
487 allowing things like (assuming 'In' is an instance):
488
488
489 exec In[4:7]
489 exec In[4:7]
490
490
491 or
491 or
492
492
493 exec In[5:9] + In[14] + In[21:25]"""
493 exec In[5:9] + In[14] + In[21:25]"""
494
494
495 def __getslice__(self,i,j):
495 def __getslice__(self,i,j):
496 return ''.join(UserList.UserList.__getslice__(self,i,j))
496 return ''.join(UserList.UserList.__getslice__(self,i,j))
497
497
498 #****************************************************************************
498 #****************************************************************************
499 # Local use exceptions
499 # Local use exceptions
500 class SpaceInInput(exceptions.Exception):
500 class SpaceInInput(exceptions.Exception):
501 pass
501 pass
502
502
503 #****************************************************************************
503 #****************************************************************************
504 # Main IPython class
504 # Main IPython class
505
505
506 class InteractiveShell(code.InteractiveConsole, Logger, Magic):
506 class InteractiveShell(code.InteractiveConsole, Logger, Magic):
507 """An enhanced console for Python."""
507 """An enhanced console for Python."""
508
508
509 def __init__(self,name,usage=None,rc=Struct(opts=None,args=None),
509 def __init__(self,name,usage=None,rc=Struct(opts=None,args=None),
510 user_ns = None,user_global_ns=None,banner2='',
510 user_ns = None,user_global_ns=None,banner2='',
511 custom_exceptions=((),None),embedded=False):
511 custom_exceptions=((),None),embedded=False):
512
512
513 # Put a reference to self in builtins so that any form of embedded or
513 # Put a reference to self in builtins so that any form of embedded or
514 # imported code can test for being inside IPython.
514 # imported code can test for being inside IPython.
515 __builtin__.__IPYTHON__ = self
515 __builtin__.__IPYTHON__ = self
516
516
517 # And load into builtins ipmagic/ipalias as well
517 # And load into builtins ipmagic/ipalias as well
518 __builtin__.ipmagic = ipmagic
518 __builtin__.ipmagic = ipmagic
519 __builtin__.ipalias = ipalias
519 __builtin__.ipalias = ipalias
520
520
521 # Add to __builtin__ other parts of IPython's public API
521 # Add to __builtin__ other parts of IPython's public API
522 __builtin__.ip_set_hook = self.set_hook
522 __builtin__.ip_set_hook = self.set_hook
523
523
524 # Keep in the builtins a flag for when IPython is active. We set it
524 # Keep in the builtins a flag for when IPython is active. We set it
525 # with setdefault so that multiple nested IPythons don't clobber one
525 # with setdefault so that multiple nested IPythons don't clobber one
526 # another. Each will increase its value by one upon being activated,
526 # another. Each will increase its value by one upon being activated,
527 # which also gives us a way to determine the nesting level.
527 # which also gives us a way to determine the nesting level.
528 __builtin__.__dict__.setdefault('__IPYTHON__active',0)
528 __builtin__.__dict__.setdefault('__IPYTHON__active',0)
529
529
530 # Inform the user of ipython's fast exit magics.
530 # Inform the user of ipython's fast exit magics.
531 _exit = ' Use %Exit or %Quit to exit without confirmation.'
531 _exit = ' Use %Exit or %Quit to exit without confirmation.'
532 __builtin__.exit += _exit
532 __builtin__.exit += _exit
533 __builtin__.quit += _exit
533 __builtin__.quit += _exit
534
534
535 # We need to know whether the instance is meant for embedding, since
535 # We need to know whether the instance is meant for embedding, since
536 # global/local namespaces need to be handled differently in that case
536 # global/local namespaces need to be handled differently in that case
537 self.embedded = embedded
537 self.embedded = embedded
538
538
539 # compiler command
539 # compiler command
540 self.compile = CommandCompiler()
540 self.compile = CommandCompiler()
541
541
542 # User input buffer
542 # User input buffer
543 self.buffer = []
543 self.buffer = []
544
544
545 # Default name given in compilation of code
545 # Default name given in compilation of code
546 self.filename = '<ipython console>'
546 self.filename = '<ipython console>'
547
547
548 # Create the namespace where the user will operate. user_ns is
548 # Create the namespace where the user will operate. user_ns is
549 # normally the only one used, and it is passed to the exec calls as
549 # normally the only one used, and it is passed to the exec calls as
550 # the locals argument. But we do carry a user_global_ns namespace
550 # the locals argument. But we do carry a user_global_ns namespace
551 # given as the exec 'globals' argument, This is useful in embedding
551 # given as the exec 'globals' argument, This is useful in embedding
552 # situations where the ipython shell opens in a context where the
552 # situations where the ipython shell opens in a context where the
553 # distinction between locals and globals is meaningful.
553 # distinction between locals and globals is meaningful.
554
554
555 # FIXME. For some strange reason, __builtins__ is showing up at user
555 # FIXME. For some strange reason, __builtins__ is showing up at user
556 # level as a dict instead of a module. This is a manual fix, but I
556 # level as a dict instead of a module. This is a manual fix, but I
557 # should really track down where the problem is coming from. Alex
557 # should really track down where the problem is coming from. Alex
558 # Schmolck reported this problem first.
558 # Schmolck reported this problem first.
559
559
560 # A useful post by Alex Martelli on this topic:
560 # A useful post by Alex Martelli on this topic:
561 # Re: inconsistent value from __builtins__
561 # Re: inconsistent value from __builtins__
562 # Von: Alex Martelli <aleaxit@yahoo.com>
562 # Von: Alex Martelli <aleaxit@yahoo.com>
563 # Datum: Freitag 01 Oktober 2004 04:45:34 nachmittags/abends
563 # Datum: Freitag 01 Oktober 2004 04:45:34 nachmittags/abends
564 # Gruppen: comp.lang.python
564 # Gruppen: comp.lang.python
565 # Referenzen: 1
565 # Referenzen: 1
566
566
567 # Michael Hohn <hohn@hooknose.lbl.gov> wrote:
567 # Michael Hohn <hohn@hooknose.lbl.gov> wrote:
568 # > >>> print type(builtin_check.get_global_binding('__builtins__'))
568 # > >>> print type(builtin_check.get_global_binding('__builtins__'))
569 # > <type 'dict'>
569 # > <type 'dict'>
570 # > >>> print type(__builtins__)
570 # > >>> print type(__builtins__)
571 # > <type 'module'>
571 # > <type 'module'>
572 # > Is this difference in return value intentional?
572 # > Is this difference in return value intentional?
573
573
574 # Well, it's documented that '__builtins__' can be either a dictionary
574 # Well, it's documented that '__builtins__' can be either a dictionary
575 # or a module, and it's been that way for a long time. Whether it's
575 # or a module, and it's been that way for a long time. Whether it's
576 # intentional (or sensible), I don't know. In any case, the idea is that
576 # intentional (or sensible), I don't know. In any case, the idea is that
577 # if you need to access the built-in namespace directly, you should start
577 # if you need to access the built-in namespace directly, you should start
578 # with "import __builtin__" (note, no 's') which will definitely give you
578 # with "import __builtin__" (note, no 's') which will definitely give you
579 # a module. Yeah, it's somewhatΒ confusing:-(.
579 # a module. Yeah, it's somewhatΒ confusing:-(.
580
580
581 if user_ns is None:
581 if user_ns is None:
582 # Set __name__ to __main__ to better match the behavior of the
582 # Set __name__ to __main__ to better match the behavior of the
583 # normal interpreter.
583 # normal interpreter.
584 user_ns = {'__name__' :'__main__',
584 user_ns = {'__name__' :'__main__',
585 '__builtins__' : __builtin__,
585 '__builtins__' : __builtin__,
586 }
586 }
587
587
588 if user_global_ns is None:
588 if user_global_ns is None:
589 user_global_ns = {}
589 user_global_ns = {}
590
590
591 # Assign namespaces
591 # Assign namespaces
592 # This is the namespace where all normal user variables live
592 # This is the namespace where all normal user variables live
593 self.user_ns = user_ns
593 self.user_ns = user_ns
594 # Embedded instances require a separate namespace for globals.
594 # Embedded instances require a separate namespace for globals.
595 # Normally this one is unused by non-embedded instances.
595 # Normally this one is unused by non-embedded instances.
596 self.user_global_ns = user_global_ns
596 self.user_global_ns = user_global_ns
597 # A namespace to keep track of internal data structures to prevent
597 # A namespace to keep track of internal data structures to prevent
598 # them from cluttering user-visible stuff. Will be updated later
598 # them from cluttering user-visible stuff. Will be updated later
599 self.internal_ns = {}
599 self.internal_ns = {}
600
600
601 # Namespace of system aliases. Each entry in the alias
601 # Namespace of system aliases. Each entry in the alias
602 # table must be a 2-tuple of the form (N,name), where N is the number
602 # table must be a 2-tuple of the form (N,name), where N is the number
603 # of positional arguments of the alias.
603 # of positional arguments of the alias.
604 self.alias_table = {}
604 self.alias_table = {}
605
605
606 # A table holding all the namespaces IPython deals with, so that
606 # A table holding all the namespaces IPython deals with, so that
607 # introspection facilities can search easily.
607 # introspection facilities can search easily.
608 self.ns_table = {'user':user_ns,
608 self.ns_table = {'user':user_ns,
609 'user_global':user_global_ns,
609 'user_global':user_global_ns,
610 'alias':self.alias_table,
610 'alias':self.alias_table,
611 'internal':self.internal_ns,
611 'internal':self.internal_ns,
612 'builtin':__builtin__.__dict__
612 'builtin':__builtin__.__dict__
613 }
613 }
614
614
615 # The user namespace MUST have a pointer to the shell itself.
615 # The user namespace MUST have a pointer to the shell itself.
616 self.user_ns[name] = self
616 self.user_ns[name] = self
617
617
618 # We need to insert into sys.modules something that looks like a
618 # We need to insert into sys.modules something that looks like a
619 # module but which accesses the IPython namespace, for shelve and
619 # module but which accesses the IPython namespace, for shelve and
620 # pickle to work interactively. Normally they rely on getting
620 # pickle to work interactively. Normally they rely on getting
621 # everything out of __main__, but for embedding purposes each IPython
621 # everything out of __main__, but for embedding purposes each IPython
622 # instance has its own private namespace, so we can't go shoving
622 # instance has its own private namespace, so we can't go shoving
623 # everything into __main__.
623 # everything into __main__.
624
624
625 try:
625 try:
626 main_name = self.user_ns['__name__']
626 main_name = self.user_ns['__name__']
627 except KeyError:
627 except KeyError:
628 raise KeyError,'user_ns dictionary MUST have a "__name__" key'
628 raise KeyError,'user_ns dictionary MUST have a "__name__" key'
629 else:
629 else:
630 #print "pickle hack in place" # dbg
630 #print "pickle hack in place" # dbg
631 sys.modules[main_name] = FakeModule(self.user_ns)
631 sys.modules[main_name] = FakeModule(self.user_ns)
632
632
633 # List of input with multi-line handling.
633 # List of input with multi-line handling.
634 # Fill its zero entry, user counter starts at 1
634 # Fill its zero entry, user counter starts at 1
635 self.input_hist = InputList(['\n'])
635 self.input_hist = InputList(['\n'])
636
636
637 # list of visited directories
637 # list of visited directories
638 try:
638 try:
639 self.dir_hist = [os.getcwd()]
639 self.dir_hist = [os.getcwd()]
640 except IOError, e:
640 except IOError, e:
641 self.dir_hist = []
641 self.dir_hist = []
642
642
643 # dict of output history
643 # dict of output history
644 self.output_hist = {}
644 self.output_hist = {}
645
645
646 # dict of things NOT to alias (keywords, builtins and some special magics)
646 # dict of things NOT to alias (keywords, builtins and some special magics)
647 no_alias = {}
647 no_alias = {}
648 no_alias_magics = ['cd','popd','pushd','dhist','alias','unalias']
648 no_alias_magics = ['cd','popd','pushd','dhist','alias','unalias']
649 for key in keyword.kwlist + no_alias_magics:
649 for key in keyword.kwlist + no_alias_magics:
650 no_alias[key] = 1
650 no_alias[key] = 1
651 no_alias.update(__builtin__.__dict__)
651 no_alias.update(__builtin__.__dict__)
652 self.no_alias = no_alias
652 self.no_alias = no_alias
653
653
654 # make global variables for user access to these
654 # make global variables for user access to these
655 self.user_ns['_ih'] = self.input_hist
655 self.user_ns['_ih'] = self.input_hist
656 self.user_ns['_oh'] = self.output_hist
656 self.user_ns['_oh'] = self.output_hist
657 self.user_ns['_dh'] = self.dir_hist
657 self.user_ns['_dh'] = self.dir_hist
658
658
659 # user aliases to input and output histories
659 # user aliases to input and output histories
660 self.user_ns['In'] = self.input_hist
660 self.user_ns['In'] = self.input_hist
661 self.user_ns['Out'] = self.output_hist
661 self.user_ns['Out'] = self.output_hist
662
662
663 # Store the actual shell's name
663 # Store the actual shell's name
664 self.name = name
664 self.name = name
665
665
666 # Object variable to store code object waiting execution. This is
666 # Object variable to store code object waiting execution. This is
667 # used mainly by the multithreaded shells, but it can come in handy in
667 # used mainly by the multithreaded shells, but it can come in handy in
668 # other situations. No need to use a Queue here, since it's a single
668 # other situations. No need to use a Queue here, since it's a single
669 # item which gets cleared once run.
669 # item which gets cleared once run.
670 self.code_to_run = None
670 self.code_to_run = None
671
671
672 # Job manager (for jobs run as background threads)
672 # Job manager (for jobs run as background threads)
673 self.jobs = BackgroundJobManager()
673 self.jobs = BackgroundJobManager()
674 # Put the job manager into builtins so it's always there.
674 # Put the job manager into builtins so it's always there.
675 __builtin__.jobs = self.jobs
675 __builtin__.jobs = self.jobs
676
676
677 # escapes for automatic behavior on the command line
677 # escapes for automatic behavior on the command line
678 self.ESC_SHELL = '!'
678 self.ESC_SHELL = '!'
679 self.ESC_HELP = '?'
679 self.ESC_HELP = '?'
680 self.ESC_MAGIC = '%'
680 self.ESC_MAGIC = '%'
681 self.ESC_QUOTE = ','
681 self.ESC_QUOTE = ','
682 self.ESC_QUOTE2 = ';'
682 self.ESC_QUOTE2 = ';'
683 self.ESC_PAREN = '/'
683 self.ESC_PAREN = '/'
684
684
685 # And their associated handlers
685 # And their associated handlers
686 self.esc_handlers = {self.ESC_PAREN:self.handle_auto,
686 self.esc_handlers = {self.ESC_PAREN:self.handle_auto,
687 self.ESC_QUOTE:self.handle_auto,
687 self.ESC_QUOTE:self.handle_auto,
688 self.ESC_QUOTE2:self.handle_auto,
688 self.ESC_QUOTE2:self.handle_auto,
689 self.ESC_MAGIC:self.handle_magic,
689 self.ESC_MAGIC:self.handle_magic,
690 self.ESC_HELP:self.handle_help,
690 self.ESC_HELP:self.handle_help,
691 self.ESC_SHELL:self.handle_shell_escape,
691 self.ESC_SHELL:self.handle_shell_escape,
692 }
692 }
693
693
694 # class initializations
694 # class initializations
695 Logger.__init__(self,log_ns = self.user_ns)
695 Logger.__init__(self,log_ns = self.user_ns)
696 Magic.__init__(self,self)
696 Magic.__init__(self,self)
697
697
698 # an ugly hack to get a pointer to the shell, so I can start writing
698 # an ugly hack to get a pointer to the shell, so I can start writing
699 # magic code via this pointer instead of the current mixin salad.
699 # magic code via this pointer instead of the current mixin salad.
700 Magic.set_shell(self,self)
700 Magic.set_shell(self,self)
701
701
702 # Python source parser/formatter for syntax highlighting
702 # Python source parser/formatter for syntax highlighting
703 pyformat = Parser().format
703 pyformat = Parser().format
704 self.pycolorize = lambda src: pyformat(src,'str',self.rc['colors'])
704 self.pycolorize = lambda src: pyformat(src,'str',self.rc['colors'])
705
705
706 # hooks holds pointers used for user-side customizations
706 # hooks holds pointers used for user-side customizations
707 self.hooks = Struct()
707 self.hooks = Struct()
708
708
709 # Set all default hooks, defined in the IPython.hooks module.
709 # Set all default hooks, defined in the IPython.hooks module.
710 hooks = IPython.hooks
710 hooks = IPython.hooks
711 for hook_name in hooks.__all__:
711 for hook_name in hooks.__all__:
712 self.set_hook(hook_name,getattr(hooks,hook_name))
712 self.set_hook(hook_name,getattr(hooks,hook_name))
713
713
714 # Flag to mark unconditional exit
714 # Flag to mark unconditional exit
715 self.exit_now = False
715 self.exit_now = False
716
716
717 self.usage_min = """\
717 self.usage_min = """\
718 An enhanced console for Python.
718 An enhanced console for Python.
719 Some of its features are:
719 Some of its features are:
720 - Readline support if the readline library is present.
720 - Readline support if the readline library is present.
721 - Tab completion in the local namespace.
721 - Tab completion in the local namespace.
722 - Logging of input, see command-line options.
722 - Logging of input, see command-line options.
723 - System shell escape via ! , eg !ls.
723 - System shell escape via ! , eg !ls.
724 - Magic commands, starting with a % (like %ls, %pwd, %cd, etc.)
724 - Magic commands, starting with a % (like %ls, %pwd, %cd, etc.)
725 - Keeps track of locally defined variables via %who, %whos.
725 - Keeps track of locally defined variables via %who, %whos.
726 - Show object information with a ? eg ?x or x? (use ?? for more info).
726 - Show object information with a ? eg ?x or x? (use ?? for more info).
727 """
727 """
728 if usage: self.usage = usage
728 if usage: self.usage = usage
729 else: self.usage = self.usage_min
729 else: self.usage = self.usage_min
730
730
731 # Storage
731 # Storage
732 self.rc = rc # This will hold all configuration information
732 self.rc = rc # This will hold all configuration information
733 self.inputcache = []
733 self.inputcache = []
734 self._boundcache = []
734 self._boundcache = []
735 self.pager = 'less'
735 self.pager = 'less'
736 # temporary files used for various purposes. Deleted at exit.
736 # temporary files used for various purposes. Deleted at exit.
737 self.tempfiles = []
737 self.tempfiles = []
738
738
739 # Keep track of readline usage (later set by init_readline)
739 # Keep track of readline usage (later set by init_readline)
740 self.has_readline = 0
740 self.has_readline = 0
741
741
742 # for pushd/popd management
742 # for pushd/popd management
743 try:
743 try:
744 self.home_dir = get_home_dir()
744 self.home_dir = get_home_dir()
745 except HomeDirError,msg:
745 except HomeDirError,msg:
746 fatal(msg)
746 fatal(msg)
747
747
748 self.dir_stack = [os.getcwd().replace(self.home_dir,'~')]
748 self.dir_stack = [os.getcwd().replace(self.home_dir,'~')]
749
749
750 # Functions to call the underlying shell.
750 # Functions to call the underlying shell.
751
751
752 # utility to expand user variables via Itpl
752 # utility to expand user variables via Itpl
753 self.var_expand = lambda cmd: str(ItplNS(cmd.replace('#','\#'),
753 self.var_expand = lambda cmd: str(ItplNS(cmd.replace('#','\#'),
754 self.user_ns))
754 self.user_ns))
755 # The first is similar to os.system, but it doesn't return a value,
755 # The first is similar to os.system, but it doesn't return a value,
756 # and it allows interpolation of variables in the user's namespace.
756 # and it allows interpolation of variables in the user's namespace.
757 self.system = lambda cmd: shell(self.var_expand(cmd),
757 self.system = lambda cmd: shell(self.var_expand(cmd),
758 header='IPython system call: ',
758 header='IPython system call: ',
759 verbose=self.rc.system_verbose)
759 verbose=self.rc.system_verbose)
760 # These are for getoutput and getoutputerror:
760 # These are for getoutput and getoutputerror:
761 self.getoutput = lambda cmd: \
761 self.getoutput = lambda cmd: \
762 getoutput(self.var_expand(cmd),
762 getoutput(self.var_expand(cmd),
763 header='IPython system call: ',
763 header='IPython system call: ',
764 verbose=self.rc.system_verbose)
764 verbose=self.rc.system_verbose)
765 self.getoutputerror = lambda cmd: \
765 self.getoutputerror = lambda cmd: \
766 getoutputerror(str(ItplNS(cmd.replace('#','\#'),
766 getoutputerror(str(ItplNS(cmd.replace('#','\#'),
767 self.user_ns)),
767 self.user_ns)),
768 header='IPython system call: ',
768 header='IPython system call: ',
769 verbose=self.rc.system_verbose)
769 verbose=self.rc.system_verbose)
770
770
771 # RegExp for splitting line contents into pre-char//first
771 # RegExp for splitting line contents into pre-char//first
772 # word-method//rest. For clarity, each group in on one line.
772 # word-method//rest. For clarity, each group in on one line.
773
773
774 # WARNING: update the regexp if the above escapes are changed, as they
774 # WARNING: update the regexp if the above escapes are changed, as they
775 # are hardwired in.
775 # are hardwired in.
776
776
777 # Don't get carried away with trying to make the autocalling catch too
777 # Don't get carried away with trying to make the autocalling catch too
778 # much: it's better to be conservative rather than to trigger hidden
778 # much: it's better to be conservative rather than to trigger hidden
779 # evals() somewhere and end up causing side effects.
779 # evals() somewhere and end up causing side effects.
780
780
781 self.line_split = re.compile(r'^([\s*,;/])'
781 self.line_split = re.compile(r'^([\s*,;/])'
782 r'([\?\w\.]+\w*\s*)'
782 r'([\?\w\.]+\w*\s*)'
783 r'(\(?.*$)')
783 r'(\(?.*$)')
784
784
785 # Original re, keep around for a while in case changes break something
785 # Original re, keep around for a while in case changes break something
786 #self.line_split = re.compile(r'(^[\s*!\?%,/]?)'
786 #self.line_split = re.compile(r'(^[\s*!\?%,/]?)'
787 # r'(\s*[\?\w\.]+\w*\s*)'
787 # r'(\s*[\?\w\.]+\w*\s*)'
788 # r'(\(?.*$)')
788 # r'(\(?.*$)')
789
789
790 # RegExp to identify potential function names
790 # RegExp to identify potential function names
791 self.re_fun_name = re.compile(r'[a-zA-Z_]([a-zA-Z0-9_.]*) *$')
791 self.re_fun_name = re.compile(r'[a-zA-Z_]([a-zA-Z0-9_.]*) *$')
792 # RegExp to exclude strings with this start from autocalling
792 # RegExp to exclude strings with this start from autocalling
793 self.re_exclude_auto = re.compile('^[!=()<>,\*/\+-]|^is ')
793 self.re_exclude_auto = re.compile('^[!=()<>,\*/\+-]|^is ')
794 # try to catch also methods for stuff in lists/tuples/dicts: off
794 # try to catch also methods for stuff in lists/tuples/dicts: off
795 # (experimental). For this to work, the line_split regexp would need
795 # (experimental). For this to work, the line_split regexp would need
796 # to be modified so it wouldn't break things at '['. That line is
796 # to be modified so it wouldn't break things at '['. That line is
797 # nasty enough that I shouldn't change it until I can test it _well_.
797 # nasty enough that I shouldn't change it until I can test it _well_.
798 #self.re_fun_name = re.compile (r'[a-zA-Z_]([a-zA-Z0-9_.\[\]]*) ?$')
798 #self.re_fun_name = re.compile (r'[a-zA-Z_]([a-zA-Z0-9_.\[\]]*) ?$')
799
799
800 # keep track of where we started running (mainly for crash post-mortem)
800 # keep track of where we started running (mainly for crash post-mortem)
801 self.starting_dir = os.getcwd()
801 self.starting_dir = os.getcwd()
802
802
803 # Attributes for Logger mixin class, make defaults here
803 # Attributes for Logger mixin class, make defaults here
804 self._dolog = 0
804 self._dolog = 0
805 self.LOG = ''
805 self.LOG = ''
806 self.LOGDEF = '.InteractiveShell.log'
806 self.LOGDEF = '.InteractiveShell.log'
807 self.LOGMODE = 'over'
807 self.LOGMODE = 'over'
808 self.LOGHEAD = Itpl(
808 self.LOGHEAD = Itpl(
809 """#log# Automatic Logger file. *** THIS MUST BE THE FIRST LINE ***
809 """#log# Automatic Logger file. *** THIS MUST BE THE FIRST LINE ***
810 #log# DO NOT CHANGE THIS LINE OR THE TWO BELOW
810 #log# DO NOT CHANGE THIS LINE OR THE TWO BELOW
811 #log# opts = $self.rc.opts
811 #log# opts = $self.rc.opts
812 #log# args = $self.rc.args
812 #log# args = $self.rc.args
813 #log# It is safe to make manual edits below here.
813 #log# It is safe to make manual edits below here.
814 #log#-----------------------------------------------------------------------
814 #log#-----------------------------------------------------------------------
815 """)
815 """)
816 # Various switches which can be set
816 # Various switches which can be set
817 self.CACHELENGTH = 5000 # this is cheap, it's just text
817 self.CACHELENGTH = 5000 # this is cheap, it's just text
818 self.BANNER = "Python %(version)s on %(platform)s\n" % sys.__dict__
818 self.BANNER = "Python %(version)s on %(platform)s\n" % sys.__dict__
819 self.banner2 = banner2
819 self.banner2 = banner2
820
820
821 # TraceBack handlers:
821 # TraceBack handlers:
822 # Need two, one for syntax errors and one for other exceptions.
822 # Need two, one for syntax errors and one for other exceptions.
823 self.SyntaxTB = ultraTB.ListTB(color_scheme='NoColor')
823 self.SyntaxTB = ultraTB.ListTB(color_scheme='NoColor')
824 # This one is initialized with an offset, meaning we always want to
824 # This one is initialized with an offset, meaning we always want to
825 # remove the topmost item in the traceback, which is our own internal
825 # remove the topmost item in the traceback, which is our own internal
826 # code. Valid modes: ['Plain','Context','Verbose']
826 # code. Valid modes: ['Plain','Context','Verbose']
827 self.InteractiveTB = ultraTB.AutoFormattedTB(mode = 'Plain',
827 self.InteractiveTB = ultraTB.AutoFormattedTB(mode = 'Plain',
828 color_scheme='NoColor',
828 color_scheme='NoColor',
829 tb_offset = 1)
829 tb_offset = 1)
830 # and add any custom exception handlers the user may have specified
830 # and add any custom exception handlers the user may have specified
831 self.set_custom_exc(*custom_exceptions)
831 self.set_custom_exc(*custom_exceptions)
832
832
833 # Object inspector
833 # Object inspector
834 ins_colors = OInspect.InspectColors
834 ins_colors = OInspect.InspectColors
835 code_colors = PyColorize.ANSICodeColors
835 code_colors = PyColorize.ANSICodeColors
836 self.inspector = OInspect.Inspector(ins_colors,code_colors,'NoColor')
836 self.inspector = OInspect.Inspector(ins_colors,code_colors,'NoColor')
837 self.autoindent = 0
837 self.autoindent = 0
838
838
839 # Make some aliases automatically
839 # Make some aliases automatically
840 # Prepare list of shell aliases to auto-define
840 # Prepare list of shell aliases to auto-define
841 if os.name == 'posix':
841 if os.name == 'posix':
842 auto_alias = ('mkdir mkdir', 'rmdir rmdir',
842 auto_alias = ('mkdir mkdir', 'rmdir rmdir',
843 'mv mv -i','rm rm -i','cp cp -i',
843 'mv mv -i','rm rm -i','cp cp -i',
844 'cat cat','less less','clear clear',
844 'cat cat','less less','clear clear',
845 # a better ls
845 # a better ls
846 'ls ls -F',
846 'ls ls -F',
847 # long ls
847 # long ls
848 'll ls -lF',
848 'll ls -lF',
849 # color ls
849 # color ls
850 'lc ls -F -o --color',
850 'lc ls -F -o --color',
851 # ls normal files only
851 # ls normal files only
852 'lf ls -F -o --color %l | grep ^-',
852 'lf ls -F -o --color %l | grep ^-',
853 # ls symbolic links
853 # ls symbolic links
854 'lk ls -F -o --color %l | grep ^l',
854 'lk ls -F -o --color %l | grep ^l',
855 # directories or links to directories,
855 # directories or links to directories,
856 'ldir ls -F -o --color %l | grep /$',
856 'ldir ls -F -o --color %l | grep /$',
857 # things which are executable
857 # things which are executable
858 'lx ls -F -o --color %l | grep ^-..x',
858 'lx ls -F -o --color %l | grep ^-..x',
859 )
859 )
860 elif os.name in ['nt','dos']:
860 elif os.name in ['nt','dos']:
861 auto_alias = ('dir dir /on', 'ls dir /on',
861 auto_alias = ('dir dir /on', 'ls dir /on',
862 'ddir dir /ad /on', 'ldir dir /ad /on',
862 'ddir dir /ad /on', 'ldir dir /ad /on',
863 'mkdir mkdir','rmdir rmdir','echo echo',
863 'mkdir mkdir','rmdir rmdir','echo echo',
864 'ren ren','cls cls','copy copy')
864 'ren ren','cls cls','copy copy')
865 else:
865 else:
866 auto_alias = ()
866 auto_alias = ()
867 self.auto_alias = map(lambda s:s.split(None,1),auto_alias)
867 self.auto_alias = map(lambda s:s.split(None,1),auto_alias)
868 # Call the actual (public) initializer
868 # Call the actual (public) initializer
869 self.init_auto_alias()
869 self.init_auto_alias()
870 # end __init__
870 # end __init__
871
871
872 def set_hook(self,name,hook):
872 def set_hook(self,name,hook):
873 """set_hook(name,hook) -> sets an internal IPython hook.
873 """set_hook(name,hook) -> sets an internal IPython hook.
874
874
875 IPython exposes some of its internal API as user-modifiable hooks. By
875 IPython exposes some of its internal API as user-modifiable hooks. By
876 resetting one of these hooks, you can modify IPython's behavior to
876 resetting one of these hooks, you can modify IPython's behavior to
877 call at runtime your own routines."""
877 call at runtime your own routines."""
878
878
879 # At some point in the future, this should validate the hook before it
879 # At some point in the future, this should validate the hook before it
880 # accepts it. Probably at least check that the hook takes the number
880 # accepts it. Probably at least check that the hook takes the number
881 # of args it's supposed to.
881 # of args it's supposed to.
882 setattr(self.hooks,name,new.instancemethod(hook,self,self.__class__))
882 setattr(self.hooks,name,new.instancemethod(hook,self,self.__class__))
883
883
884 def set_custom_exc(self,exc_tuple,handler):
884 def set_custom_exc(self,exc_tuple,handler):
885 """set_custom_exc(exc_tuple,handler)
885 """set_custom_exc(exc_tuple,handler)
886
886
887 Set a custom exception handler, which will be called if any of the
887 Set a custom exception handler, which will be called if any of the
888 exceptions in exc_tuple occur in the mainloop (specifically, in the
888 exceptions in exc_tuple occur in the mainloop (specifically, in the
889 runcode() method.
889 runcode() method.
890
890
891 Inputs:
891 Inputs:
892
892
893 - exc_tuple: a *tuple* of valid exceptions to call the defined
893 - exc_tuple: a *tuple* of valid exceptions to call the defined
894 handler for. It is very important that you use a tuple, and NOT A
894 handler for. It is very important that you use a tuple, and NOT A
895 LIST here, because of the way Python's except statement works. If
895 LIST here, because of the way Python's except statement works. If
896 you only want to trap a single exception, use a singleton tuple:
896 you only want to trap a single exception, use a singleton tuple:
897
897
898 exc_tuple == (MyCustomException,)
898 exc_tuple == (MyCustomException,)
899
899
900 - handler: this must be defined as a function with the following
900 - handler: this must be defined as a function with the following
901 basic interface: def my_handler(self,etype,value,tb).
901 basic interface: def my_handler(self,etype,value,tb).
902
902
903 This will be made into an instance method (via new.instancemethod)
903 This will be made into an instance method (via new.instancemethod)
904 of IPython itself, and it will be called if any of the exceptions
904 of IPython itself, and it will be called if any of the exceptions
905 listed in the exc_tuple are caught. If the handler is None, an
905 listed in the exc_tuple are caught. If the handler is None, an
906 internal basic one is used, which just prints basic info.
906 internal basic one is used, which just prints basic info.
907
907
908 WARNING: by putting in your own exception handler into IPython's main
908 WARNING: by putting in your own exception handler into IPython's main
909 execution loop, you run a very good chance of nasty crashes. This
909 execution loop, you run a very good chance of nasty crashes. This
910 facility should only be used if you really know what you are doing."""
910 facility should only be used if you really know what you are doing."""
911
911
912 assert type(exc_tuple)==type(()) , \
912 assert type(exc_tuple)==type(()) , \
913 "The custom exceptions must be given AS A TUPLE."
913 "The custom exceptions must be given AS A TUPLE."
914
914
915 def dummy_handler(self,etype,value,tb):
915 def dummy_handler(self,etype,value,tb):
916 print '*** Simple custom exception handler ***'
916 print '*** Simple custom exception handler ***'
917 print 'Exception type :',etype
917 print 'Exception type :',etype
918 print 'Exception value:',value
918 print 'Exception value:',value
919 print 'Traceback :',tb
919 print 'Traceback :',tb
920 print 'Source code :','\n'.join(self.buffer)
920 print 'Source code :','\n'.join(self.buffer)
921
921
922 if handler is None: handler = dummy_handler
922 if handler is None: handler = dummy_handler
923
923
924 self.CustomTB = new.instancemethod(handler,self,self.__class__)
924 self.CustomTB = new.instancemethod(handler,self,self.__class__)
925 self.custom_exceptions = exc_tuple
925 self.custom_exceptions = exc_tuple
926
926
927 def set_custom_completer(self,completer,pos=0):
927 def set_custom_completer(self,completer,pos=0):
928 """set_custom_completer(completer,pos=0)
928 """set_custom_completer(completer,pos=0)
929
929
930 Adds a new custom completer function.
930 Adds a new custom completer function.
931
931
932 The position argument (defaults to 0) is the index in the completers
932 The position argument (defaults to 0) is the index in the completers
933 list where you want the completer to be inserted."""
933 list where you want the completer to be inserted."""
934
934
935 newcomp = new.instancemethod(completer,self.Completer,
935 newcomp = new.instancemethod(completer,self.Completer,
936 self.Completer.__class__)
936 self.Completer.__class__)
937 self.Completer.matchers.insert(pos,newcomp)
937 self.Completer.matchers.insert(pos,newcomp)
938
938
939 def complete(self,text):
939 def complete(self,text):
940 """Return a sorted list of all possible completions on text.
940 """Return a sorted list of all possible completions on text.
941
941
942 Inputs:
942 Inputs:
943
943
944 - text: a string of text to be completed on.
944 - text: a string of text to be completed on.
945
945
946 This is a wrapper around the completion mechanism, similar to what
946 This is a wrapper around the completion mechanism, similar to what
947 readline does at the command line when the TAB key is hit. By
947 readline does at the command line when the TAB key is hit. By
948 exposing it as a method, it can be used by other non-readline
948 exposing it as a method, it can be used by other non-readline
949 environments (such as GUIs) for text completion.
949 environments (such as GUIs) for text completion.
950
950
951 Simple usage example:
951 Simple usage example:
952
952
953 In [1]: x = 'hello'
953 In [1]: x = 'hello'
954
954
955 In [2]: __IP.complete('x.l')
955 In [2]: __IP.complete('x.l')
956 Out[2]: ['x.ljust', 'x.lower', 'x.lstrip']"""
956 Out[2]: ['x.ljust', 'x.lower', 'x.lstrip']"""
957
957
958 complete = self.Completer.complete
958 complete = self.Completer.complete
959 state = 0
959 state = 0
960 # use a dict so we get unique keys, since ipyhton's multiple
960 # use a dict so we get unique keys, since ipyhton's multiple
961 # completers can return duplicates.
961 # completers can return duplicates.
962 comps = {}
962 comps = {}
963 while True:
963 while True:
964 newcomp = complete(text,state)
964 newcomp = complete(text,state)
965 if newcomp is None:
965 if newcomp is None:
966 break
966 break
967 comps[newcomp] = 1
967 comps[newcomp] = 1
968 state += 1
968 state += 1
969 outcomps = comps.keys()
969 outcomps = comps.keys()
970 outcomps.sort()
970 outcomps.sort()
971 return outcomps
971 return outcomps
972
972
973 def set_completer_frame(self, frame):
974 if frame:
975 ns = frame.f_globals.copy()
976 ns.update(frame.f_locals)
977 self.Completer.namespace = ns
978 else:
979 self.Completer.namespace = self.user_ns
980
973 def post_config_initialization(self):
981 def post_config_initialization(self):
974 """Post configuration init method
982 """Post configuration init method
975
983
976 This is called after the configuration files have been processed to
984 This is called after the configuration files have been processed to
977 'finalize' the initialization."""
985 'finalize' the initialization."""
978
986
979 rc = self.rc
987 rc = self.rc
980
988
981 # Load readline proper
989 # Load readline proper
982 if rc.readline:
990 if rc.readline:
983 self.init_readline()
991 self.init_readline()
984
992
985 # Set user colors (don't do it in the constructor above so that it doesn't
993 # Set user colors (don't do it in the constructor above so that it doesn't
986 # crash if colors option is invalid)
994 # crash if colors option is invalid)
987 self.magic_colors(rc.colors)
995 self.magic_colors(rc.colors)
988
996
989 # Load user aliases
997 # Load user aliases
990 for alias in rc.alias:
998 for alias in rc.alias:
991 self.magic_alias(alias)
999 self.magic_alias(alias)
992
1000
993 # dynamic data that survives through sessions
1001 # dynamic data that survives through sessions
994 # XXX make the filename a config option?
1002 # XXX make the filename a config option?
995 persist_base = 'persist'
1003 persist_base = 'persist'
996 if rc.profile:
1004 if rc.profile:
997 persist_base += '_%s' % rc.profile
1005 persist_base += '_%s' % rc.profile
998 self.persist_fname = os.path.join(rc.ipythondir,persist_base)
1006 self.persist_fname = os.path.join(rc.ipythondir,persist_base)
999
1007
1000 try:
1008 try:
1001 self.persist = pickle.load(file(self.persist_fname))
1009 self.persist = pickle.load(file(self.persist_fname))
1002 except:
1010 except:
1003 self.persist = {}
1011 self.persist = {}
1004
1012
1005 def init_auto_alias(self):
1013 def init_auto_alias(self):
1006 """Define some aliases automatically.
1014 """Define some aliases automatically.
1007
1015
1008 These are ALL parameter-less aliases"""
1016 These are ALL parameter-less aliases"""
1009 for alias,cmd in self.auto_alias:
1017 for alias,cmd in self.auto_alias:
1010 self.alias_table[alias] = (0,cmd)
1018 self.alias_table[alias] = (0,cmd)
1011
1019
1012 def alias_table_validate(self,verbose=0):
1020 def alias_table_validate(self,verbose=0):
1013 """Update information about the alias table.
1021 """Update information about the alias table.
1014
1022
1015 In particular, make sure no Python keywords/builtins are in it."""
1023 In particular, make sure no Python keywords/builtins are in it."""
1016
1024
1017 no_alias = self.no_alias
1025 no_alias = self.no_alias
1018 for k in self.alias_table.keys():
1026 for k in self.alias_table.keys():
1019 if k in no_alias:
1027 if k in no_alias:
1020 del self.alias_table[k]
1028 del self.alias_table[k]
1021 if verbose:
1029 if verbose:
1022 print ("Deleting alias <%s>, it's a Python "
1030 print ("Deleting alias <%s>, it's a Python "
1023 "keyword or builtin." % k)
1031 "keyword or builtin." % k)
1024
1032
1025 def set_autoindent(self,value=None):
1033 def set_autoindent(self,value=None):
1026 """Set the autoindent flag, checking for readline support.
1034 """Set the autoindent flag, checking for readline support.
1027
1035
1028 If called with no arguments, it acts as a toggle."""
1036 If called with no arguments, it acts as a toggle."""
1029
1037
1030 if not self.has_readline:
1038 if not self.has_readline:
1031 if os.name == 'posix':
1039 if os.name == 'posix':
1032 warn("The auto-indent feature requires the readline library")
1040 warn("The auto-indent feature requires the readline library")
1033 self.autoindent = 0
1041 self.autoindent = 0
1034 return
1042 return
1035 if value is None:
1043 if value is None:
1036 self.autoindent = not self.autoindent
1044 self.autoindent = not self.autoindent
1037 else:
1045 else:
1038 self.autoindent = value
1046 self.autoindent = value
1039
1047
1040 def rc_set_toggle(self,rc_field,value=None):
1048 def rc_set_toggle(self,rc_field,value=None):
1041 """Set or toggle a field in IPython's rc config. structure.
1049 """Set or toggle a field in IPython's rc config. structure.
1042
1050
1043 If called with no arguments, it acts as a toggle.
1051 If called with no arguments, it acts as a toggle.
1044
1052
1045 If called with a non-existent field, the resulting AttributeError
1053 If called with a non-existent field, the resulting AttributeError
1046 exception will propagate out."""
1054 exception will propagate out."""
1047
1055
1048 rc_val = getattr(self.rc,rc_field)
1056 rc_val = getattr(self.rc,rc_field)
1049 if value is None:
1057 if value is None:
1050 value = not rc_val
1058 value = not rc_val
1051 setattr(self.rc,rc_field,value)
1059 setattr(self.rc,rc_field,value)
1052
1060
1053 def user_setup(self,ipythondir,rc_suffix,mode='install'):
1061 def user_setup(self,ipythondir,rc_suffix,mode='install'):
1054 """Install the user configuration directory.
1062 """Install the user configuration directory.
1055
1063
1056 Can be called when running for the first time or to upgrade the user's
1064 Can be called when running for the first time or to upgrade the user's
1057 .ipython/ directory with the mode parameter. Valid modes are 'install'
1065 .ipython/ directory with the mode parameter. Valid modes are 'install'
1058 and 'upgrade'."""
1066 and 'upgrade'."""
1059
1067
1060 def wait():
1068 def wait():
1061 try:
1069 try:
1062 raw_input("Please press <RETURN> to start IPython.")
1070 raw_input("Please press <RETURN> to start IPython.")
1063 except EOFError:
1071 except EOFError:
1064 print >> Term.cout
1072 print >> Term.cout
1065 print '*'*70
1073 print '*'*70
1066
1074
1067 cwd = os.getcwd() # remember where we started
1075 cwd = os.getcwd() # remember where we started
1068 glb = glob.glob
1076 glb = glob.glob
1069 print '*'*70
1077 print '*'*70
1070 if mode == 'install':
1078 if mode == 'install':
1071 print \
1079 print \
1072 """Welcome to IPython. I will try to create a personal configuration directory
1080 """Welcome to IPython. I will try to create a personal configuration directory
1073 where you can customize many aspects of IPython's functionality in:\n"""
1081 where you can customize many aspects of IPython's functionality in:\n"""
1074 else:
1082 else:
1075 print 'I am going to upgrade your configuration in:'
1083 print 'I am going to upgrade your configuration in:'
1076
1084
1077 print ipythondir
1085 print ipythondir
1078
1086
1079 rcdirend = os.path.join('IPython','UserConfig')
1087 rcdirend = os.path.join('IPython','UserConfig')
1080 cfg = lambda d: os.path.join(d,rcdirend)
1088 cfg = lambda d: os.path.join(d,rcdirend)
1081 try:
1089 try:
1082 rcdir = filter(os.path.isdir,map(cfg,sys.path))[0]
1090 rcdir = filter(os.path.isdir,map(cfg,sys.path))[0]
1083 except IOError:
1091 except IOError:
1084 warning = """
1092 warning = """
1085 Installation error. IPython's directory was not found.
1093 Installation error. IPython's directory was not found.
1086
1094
1087 Check the following:
1095 Check the following:
1088
1096
1089 The ipython/IPython directory should be in a directory belonging to your
1097 The ipython/IPython directory should be in a directory belonging to your
1090 PYTHONPATH environment variable (that is, it should be in a directory
1098 PYTHONPATH environment variable (that is, it should be in a directory
1091 belonging to sys.path). You can copy it explicitly there or just link to it.
1099 belonging to sys.path). You can copy it explicitly there or just link to it.
1092
1100
1093 IPython will proceed with builtin defaults.
1101 IPython will proceed with builtin defaults.
1094 """
1102 """
1095 warn(warning)
1103 warn(warning)
1096 wait()
1104 wait()
1097 return
1105 return
1098
1106
1099 if mode == 'install':
1107 if mode == 'install':
1100 try:
1108 try:
1101 shutil.copytree(rcdir,ipythondir)
1109 shutil.copytree(rcdir,ipythondir)
1102 os.chdir(ipythondir)
1110 os.chdir(ipythondir)
1103 rc_files = glb("ipythonrc*")
1111 rc_files = glb("ipythonrc*")
1104 for rc_file in rc_files:
1112 for rc_file in rc_files:
1105 os.rename(rc_file,rc_file+rc_suffix)
1113 os.rename(rc_file,rc_file+rc_suffix)
1106 except:
1114 except:
1107 warning = """
1115 warning = """
1108
1116
1109 There was a problem with the installation:
1117 There was a problem with the installation:
1110 %s
1118 %s
1111 Try to correct it or contact the developers if you think it's a bug.
1119 Try to correct it or contact the developers if you think it's a bug.
1112 IPython will proceed with builtin defaults.""" % sys.exc_info()[1]
1120 IPython will proceed with builtin defaults.""" % sys.exc_info()[1]
1113 warn(warning)
1121 warn(warning)
1114 wait()
1122 wait()
1115 return
1123 return
1116
1124
1117 elif mode == 'upgrade':
1125 elif mode == 'upgrade':
1118 try:
1126 try:
1119 os.chdir(ipythondir)
1127 os.chdir(ipythondir)
1120 except:
1128 except:
1121 print """
1129 print """
1122 Can not upgrade: changing to directory %s failed. Details:
1130 Can not upgrade: changing to directory %s failed. Details:
1123 %s
1131 %s
1124 """ % (ipythondir,sys.exc_info()[1])
1132 """ % (ipythondir,sys.exc_info()[1])
1125 wait()
1133 wait()
1126 return
1134 return
1127 else:
1135 else:
1128 sources = glb(os.path.join(rcdir,'[A-Za-z]*'))
1136 sources = glb(os.path.join(rcdir,'[A-Za-z]*'))
1129 for new_full_path in sources:
1137 for new_full_path in sources:
1130 new_filename = os.path.basename(new_full_path)
1138 new_filename = os.path.basename(new_full_path)
1131 if new_filename.startswith('ipythonrc'):
1139 if new_filename.startswith('ipythonrc'):
1132 new_filename = new_filename + rc_suffix
1140 new_filename = new_filename + rc_suffix
1133 # The config directory should only contain files, skip any
1141 # The config directory should only contain files, skip any
1134 # directories which may be there (like CVS)
1142 # directories which may be there (like CVS)
1135 if os.path.isdir(new_full_path):
1143 if os.path.isdir(new_full_path):
1136 continue
1144 continue
1137 if os.path.exists(new_filename):
1145 if os.path.exists(new_filename):
1138 old_file = new_filename+'.old'
1146 old_file = new_filename+'.old'
1139 if os.path.exists(old_file):
1147 if os.path.exists(old_file):
1140 os.remove(old_file)
1148 os.remove(old_file)
1141 os.rename(new_filename,old_file)
1149 os.rename(new_filename,old_file)
1142 shutil.copy(new_full_path,new_filename)
1150 shutil.copy(new_full_path,new_filename)
1143 else:
1151 else:
1144 raise ValueError,'unrecognized mode for install:',`mode`
1152 raise ValueError,'unrecognized mode for install:',`mode`
1145
1153
1146 # Fix line-endings to those native to each platform in the config
1154 # Fix line-endings to those native to each platform in the config
1147 # directory.
1155 # directory.
1148 try:
1156 try:
1149 os.chdir(ipythondir)
1157 os.chdir(ipythondir)
1150 except:
1158 except:
1151 print """
1159 print """
1152 Problem: changing to directory %s failed.
1160 Problem: changing to directory %s failed.
1153 Details:
1161 Details:
1154 %s
1162 %s
1155
1163
1156 Some configuration files may have incorrect line endings. This should not
1164 Some configuration files may have incorrect line endings. This should not
1157 cause any problems during execution. """ % (ipythondir,sys.exc_info()[1])
1165 cause any problems during execution. """ % (ipythondir,sys.exc_info()[1])
1158 wait()
1166 wait()
1159 else:
1167 else:
1160 for fname in glb('ipythonrc*'):
1168 for fname in glb('ipythonrc*'):
1161 try:
1169 try:
1162 native_line_ends(fname,backup=0)
1170 native_line_ends(fname,backup=0)
1163 except IOError:
1171 except IOError:
1164 pass
1172 pass
1165
1173
1166 if mode == 'install':
1174 if mode == 'install':
1167 print """
1175 print """
1168 Successful installation!
1176 Successful installation!
1169
1177
1170 Please read the sections 'Initial Configuration' and 'Quick Tips' in the
1178 Please read the sections 'Initial Configuration' and 'Quick Tips' in the
1171 IPython manual (there are both HTML and PDF versions supplied with the
1179 IPython manual (there are both HTML and PDF versions supplied with the
1172 distribution) to make sure that your system environment is properly configured
1180 distribution) to make sure that your system environment is properly configured
1173 to take advantage of IPython's features."""
1181 to take advantage of IPython's features."""
1174 else:
1182 else:
1175 print """
1183 print """
1176 Successful upgrade!
1184 Successful upgrade!
1177
1185
1178 All files in your directory:
1186 All files in your directory:
1179 %(ipythondir)s
1187 %(ipythondir)s
1180 which would have been overwritten by the upgrade were backed up with a .old
1188 which would have been overwritten by the upgrade were backed up with a .old
1181 extension. If you had made particular customizations in those files you may
1189 extension. If you had made particular customizations in those files you may
1182 want to merge them back into the new files.""" % locals()
1190 want to merge them back into the new files.""" % locals()
1183 wait()
1191 wait()
1184 os.chdir(cwd)
1192 os.chdir(cwd)
1185 # end user_setup()
1193 # end user_setup()
1186
1194
1187 def atexit_operations(self):
1195 def atexit_operations(self):
1188 """This will be executed at the time of exit.
1196 """This will be executed at the time of exit.
1189
1197
1190 Saving of persistent data should be performed here. """
1198 Saving of persistent data should be performed here. """
1191
1199
1192 # input history
1200 # input history
1193 self.savehist()
1201 self.savehist()
1194
1202
1195 # Cleanup all tempfiles left around
1203 # Cleanup all tempfiles left around
1196 for tfile in self.tempfiles:
1204 for tfile in self.tempfiles:
1197 try:
1205 try:
1198 os.unlink(tfile)
1206 os.unlink(tfile)
1199 except OSError:
1207 except OSError:
1200 pass
1208 pass
1201
1209
1202 # save the "persistent data" catch-all dictionary
1210 # save the "persistent data" catch-all dictionary
1203 try:
1211 try:
1204 pickle.dump(self.persist, open(self.persist_fname,"w"))
1212 pickle.dump(self.persist, open(self.persist_fname,"w"))
1205 except:
1213 except:
1206 print "*** ERROR *** persistent data saving failed."
1214 print "*** ERROR *** persistent data saving failed."
1207
1215
1208 def savehist(self):
1216 def savehist(self):
1209 """Save input history to a file (via readline library)."""
1217 """Save input history to a file (via readline library)."""
1210 try:
1218 try:
1211 self.readline.write_history_file(self.histfile)
1219 self.readline.write_history_file(self.histfile)
1212 except:
1220 except:
1213 print 'Unable to save IPython command history to file: ' + \
1221 print 'Unable to save IPython command history to file: ' + \
1214 `self.histfile`
1222 `self.histfile`
1215
1223
1216 def pre_readline(self):
1224 def pre_readline(self):
1217 """readline hook to be used at the start of each line.
1225 """readline hook to be used at the start of each line.
1218
1226
1219 Currently it handles auto-indent only."""
1227 Currently it handles auto-indent only."""
1220
1228
1221 self.readline.insert_text(' '* self.readline_indent)
1229 self.readline.insert_text(' '* self.readline_indent)
1222
1230
1223 def init_readline(self):
1231 def init_readline(self):
1224 """Command history completion/saving/reloading."""
1232 """Command history completion/saving/reloading."""
1225 try:
1233 try:
1226 import readline
1234 import readline
1227 self.Completer = MagicCompleter(self,
1235 self.Completer = MagicCompleter(self,
1228 self.user_ns,
1236 self.user_ns,
1229 self.rc.readline_omit__names,
1237 self.rc.readline_omit__names,
1230 self.alias_table)
1238 self.alias_table)
1231 except ImportError,NameError:
1239 except ImportError,NameError:
1232 # If FlexCompleter failed to import, MagicCompleter won't be
1240 # If FlexCompleter failed to import, MagicCompleter won't be
1233 # defined. This can happen because of a problem with readline
1241 # defined. This can happen because of a problem with readline
1234 self.has_readline = 0
1242 self.has_readline = 0
1235 # no point in bugging windows users with this every time:
1243 # no point in bugging windows users with this every time:
1236 if os.name == 'posix':
1244 if os.name == 'posix':
1237 warn('Readline services not available on this platform.')
1245 warn('Readline services not available on this platform.')
1238 else:
1246 else:
1239 import atexit
1247 import atexit
1240
1248
1241 # Platform-specific configuration
1249 # Platform-specific configuration
1242 if os.name == 'nt':
1250 if os.name == 'nt':
1243 # readline under Windows modifies the default exit behavior
1251 # readline under Windows modifies the default exit behavior
1244 # from being Ctrl-Z/Return to the Unix Ctrl-D one.
1252 # from being Ctrl-Z/Return to the Unix Ctrl-D one.
1245 __builtin__.exit = __builtin__.quit = \
1253 __builtin__.exit = __builtin__.quit = \
1246 ('Use Ctrl-D (i.e. EOF) to exit. '
1254 ('Use Ctrl-D (i.e. EOF) to exit. '
1247 'Use %Exit or %Quit to exit without confirmation.')
1255 'Use %Exit or %Quit to exit without confirmation.')
1248 self.readline_startup_hook = readline.set_pre_input_hook
1256 self.readline_startup_hook = readline.set_pre_input_hook
1249 else:
1257 else:
1250 self.readline_startup_hook = readline.set_startup_hook
1258 self.readline_startup_hook = readline.set_startup_hook
1251
1259
1252 # Load user's initrc file (readline config)
1260 # Load user's initrc file (readline config)
1253 inputrc_name = os.environ.get('INPUTRC')
1261 inputrc_name = os.environ.get('INPUTRC')
1254 if inputrc_name is None:
1262 if inputrc_name is None:
1255 home_dir = get_home_dir()
1263 home_dir = get_home_dir()
1256 if home_dir is not None:
1264 if home_dir is not None:
1257 inputrc_name = os.path.join(home_dir,'.inputrc')
1265 inputrc_name = os.path.join(home_dir,'.inputrc')
1258 if os.path.isfile(inputrc_name):
1266 if os.path.isfile(inputrc_name):
1259 try:
1267 try:
1260 readline.read_init_file(inputrc_name)
1268 readline.read_init_file(inputrc_name)
1261 except:
1269 except:
1262 warn('Problems reading readline initialization file <%s>'
1270 warn('Problems reading readline initialization file <%s>'
1263 % inputrc_name)
1271 % inputrc_name)
1264
1272
1265 self.has_readline = 1
1273 self.has_readline = 1
1266 self.readline = readline
1274 self.readline = readline
1267 self.readline_indent = 0 # for auto-indenting via readline
1275 self.readline_indent = 0 # for auto-indenting via readline
1268 # save this in sys so embedded copies can restore it properly
1276 # save this in sys so embedded copies can restore it properly
1269 sys.ipcompleter = self.Completer.complete
1277 sys.ipcompleter = self.Completer.complete
1270 readline.set_completer(self.Completer.complete)
1278 readline.set_completer(self.Completer.complete)
1271
1279
1272 # Configure readline according to user's prefs
1280 # Configure readline according to user's prefs
1273 for rlcommand in self.rc.readline_parse_and_bind:
1281 for rlcommand in self.rc.readline_parse_and_bind:
1274 readline.parse_and_bind(rlcommand)
1282 readline.parse_and_bind(rlcommand)
1275
1283
1276 # remove some chars from the delimiters list
1284 # remove some chars from the delimiters list
1277 delims = readline.get_completer_delims()
1285 delims = readline.get_completer_delims()
1278 delims = delims.translate(string._idmap,
1286 delims = delims.translate(string._idmap,
1279 self.rc.readline_remove_delims)
1287 self.rc.readline_remove_delims)
1280 readline.set_completer_delims(delims)
1288 readline.set_completer_delims(delims)
1281 # otherwise we end up with a monster history after a while:
1289 # otherwise we end up with a monster history after a while:
1282 readline.set_history_length(1000)
1290 readline.set_history_length(1000)
1283 try:
1291 try:
1284 #print '*** Reading readline history' # dbg
1292 #print '*** Reading readline history' # dbg
1285 readline.read_history_file(self.histfile)
1293 readline.read_history_file(self.histfile)
1286 except IOError:
1294 except IOError:
1287 pass # It doesn't exist yet.
1295 pass # It doesn't exist yet.
1288
1296
1289 atexit.register(self.atexit_operations)
1297 atexit.register(self.atexit_operations)
1290 del atexit
1298 del atexit
1291
1299
1292 # Configure auto-indent for all platforms
1300 # Configure auto-indent for all platforms
1293 self.set_autoindent(self.rc.autoindent)
1301 self.set_autoindent(self.rc.autoindent)
1294
1302
1295 def showsyntaxerror(self, filename=None):
1303 def showsyntaxerror(self, filename=None):
1296 """Display the syntax error that just occurred.
1304 """Display the syntax error that just occurred.
1297
1305
1298 This doesn't display a stack trace because there isn't one.
1306 This doesn't display a stack trace because there isn't one.
1299
1307
1300 If a filename is given, it is stuffed in the exception instead
1308 If a filename is given, it is stuffed in the exception instead
1301 of what was there before (because Python's parser always uses
1309 of what was there before (because Python's parser always uses
1302 "<string>" when reading from a string).
1310 "<string>" when reading from a string).
1303 """
1311 """
1304 type, value, sys.last_traceback = sys.exc_info()
1312 type, value, sys.last_traceback = sys.exc_info()
1305 sys.last_type = type
1313 sys.last_type = type
1306 sys.last_value = value
1314 sys.last_value = value
1307 if filename and type is SyntaxError:
1315 if filename and type is SyntaxError:
1308 # Work hard to stuff the correct filename in the exception
1316 # Work hard to stuff the correct filename in the exception
1309 try:
1317 try:
1310 msg, (dummy_filename, lineno, offset, line) = value
1318 msg, (dummy_filename, lineno, offset, line) = value
1311 except:
1319 except:
1312 # Not the format we expect; leave it alone
1320 # Not the format we expect; leave it alone
1313 pass
1321 pass
1314 else:
1322 else:
1315 # Stuff in the right filename
1323 # Stuff in the right filename
1316 try:
1324 try:
1317 # Assume SyntaxError is a class exception
1325 # Assume SyntaxError is a class exception
1318 value = SyntaxError(msg, (filename, lineno, offset, line))
1326 value = SyntaxError(msg, (filename, lineno, offset, line))
1319 except:
1327 except:
1320 # If that failed, assume SyntaxError is a string
1328 # If that failed, assume SyntaxError is a string
1321 value = msg, (filename, lineno, offset, line)
1329 value = msg, (filename, lineno, offset, line)
1322 self.SyntaxTB(type,value,[])
1330 self.SyntaxTB(type,value,[])
1323
1331
1324 def debugger(self):
1332 def debugger(self):
1325 """Call the pdb debugger."""
1333 """Call the pdb debugger."""
1326
1334
1327 if not self.rc.pdb:
1335 if not self.rc.pdb:
1328 return
1336 return
1329 pdb.pm()
1337 pdb.pm()
1330
1338
1331 def showtraceback(self,exc_tuple = None,filename=None):
1339 def showtraceback(self,exc_tuple = None,filename=None):
1332 """Display the exception that just occurred."""
1340 """Display the exception that just occurred."""
1333
1341
1334 # Though this won't be called by syntax errors in the input line,
1342 # Though this won't be called by syntax errors in the input line,
1335 # there may be SyntaxError cases whith imported code.
1343 # there may be SyntaxError cases whith imported code.
1336 if exc_tuple is None:
1344 if exc_tuple is None:
1337 type, value, tb = sys.exc_info()
1345 type, value, tb = sys.exc_info()
1338 else:
1346 else:
1339 type, value, tb = exc_tuple
1347 type, value, tb = exc_tuple
1340 if type is SyntaxError:
1348 if type is SyntaxError:
1341 self.showsyntaxerror(filename)
1349 self.showsyntaxerror(filename)
1342 else:
1350 else:
1343 sys.last_type = type
1351 sys.last_type = type
1344 sys.last_value = value
1352 sys.last_value = value
1345 sys.last_traceback = tb
1353 sys.last_traceback = tb
1346 self.InteractiveTB()
1354 self.InteractiveTB()
1347 if self.InteractiveTB.call_pdb and self.has_readline:
1355 if self.InteractiveTB.call_pdb and self.has_readline:
1348 # pdb mucks up readline, fix it back
1356 # pdb mucks up readline, fix it back
1349 self.readline.set_completer(self.Completer.complete)
1357 self.readline.set_completer(self.Completer.complete)
1350
1358
1351 def update_cache(self, line):
1359 def update_cache(self, line):
1352 """puts line into cache"""
1360 """puts line into cache"""
1353 self.inputcache.insert(0, line) # This copies the cache every time ... :-(
1361 self.inputcache.insert(0, line) # This copies the cache every time ... :-(
1354 if len(self.inputcache) >= self.CACHELENGTH:
1362 if len(self.inputcache) >= self.CACHELENGTH:
1355 self.inputcache.pop() # This not :-)
1363 self.inputcache.pop() # This not :-)
1356
1364
1357 def mainloop(self,banner=None):
1365 def mainloop(self,banner=None):
1358 """Creates the local namespace and starts the mainloop.
1366 """Creates the local namespace and starts the mainloop.
1359
1367
1360 If an optional banner argument is given, it will override the
1368 If an optional banner argument is given, it will override the
1361 internally created default banner."""
1369 internally created default banner."""
1362
1370
1363 if self.rc.c: # Emulate Python's -c option
1371 if self.rc.c: # Emulate Python's -c option
1364 self.exec_init_cmd()
1372 self.exec_init_cmd()
1365 if banner is None:
1373 if banner is None:
1366 if self.rc.banner:
1374 if self.rc.banner:
1367 banner = self.BANNER+self.banner2
1375 banner = self.BANNER+self.banner2
1368 else:
1376 else:
1369 banner = ''
1377 banner = ''
1370 self.interact(banner)
1378 self.interact(banner)
1371
1379
1372 def exec_init_cmd(self):
1380 def exec_init_cmd(self):
1373 """Execute a command given at the command line.
1381 """Execute a command given at the command line.
1374
1382
1375 This emulates Python's -c option."""
1383 This emulates Python's -c option."""
1376
1384
1377 sys.argv = ['-c']
1385 sys.argv = ['-c']
1378 self.push(self.rc.c)
1386 self.push(self.rc.c)
1379
1387
1380 def embed_mainloop(self,header='',local_ns=None,global_ns=None,stack_depth=0):
1388 def embed_mainloop(self,header='',local_ns=None,global_ns=None,stack_depth=0):
1381 """Embeds IPython into a running python program.
1389 """Embeds IPython into a running python program.
1382
1390
1383 Input:
1391 Input:
1384
1392
1385 - header: An optional header message can be specified.
1393 - header: An optional header message can be specified.
1386
1394
1387 - local_ns, global_ns: working namespaces. If given as None, the
1395 - local_ns, global_ns: working namespaces. If given as None, the
1388 IPython-initialized one is updated with __main__.__dict__, so that
1396 IPython-initialized one is updated with __main__.__dict__, so that
1389 program variables become visible but user-specific configuration
1397 program variables become visible but user-specific configuration
1390 remains possible.
1398 remains possible.
1391
1399
1392 - stack_depth: specifies how many levels in the stack to go to
1400 - stack_depth: specifies how many levels in the stack to go to
1393 looking for namespaces (when local_ns and global_ns are None). This
1401 looking for namespaces (when local_ns and global_ns are None). This
1394 allows an intermediate caller to make sure that this function gets
1402 allows an intermediate caller to make sure that this function gets
1395 the namespace from the intended level in the stack. By default (0)
1403 the namespace from the intended level in the stack. By default (0)
1396 it will get its locals and globals from the immediate caller.
1404 it will get its locals and globals from the immediate caller.
1397
1405
1398 Warning: it's possible to use this in a program which is being run by
1406 Warning: it's possible to use this in a program which is being run by
1399 IPython itself (via %run), but some funny things will happen (a few
1407 IPython itself (via %run), but some funny things will happen (a few
1400 globals get overwritten). In the future this will be cleaned up, as
1408 globals get overwritten). In the future this will be cleaned up, as
1401 there is no fundamental reason why it can't work perfectly."""
1409 there is no fundamental reason why it can't work perfectly."""
1402
1410
1403 # Get locals and globals from caller
1411 # Get locals and globals from caller
1404 if local_ns is None or global_ns is None:
1412 if local_ns is None or global_ns is None:
1405 call_frame = sys._getframe(stack_depth).f_back
1413 call_frame = sys._getframe(stack_depth).f_back
1406
1414
1407 if local_ns is None:
1415 if local_ns is None:
1408 local_ns = call_frame.f_locals
1416 local_ns = call_frame.f_locals
1409 if global_ns is None:
1417 if global_ns is None:
1410 global_ns = call_frame.f_globals
1418 global_ns = call_frame.f_globals
1411
1419
1412 # Update namespaces and fire up interpreter
1420 # Update namespaces and fire up interpreter
1413 self.user_ns = local_ns
1421 self.user_ns = local_ns
1414 self.user_global_ns = global_ns
1422 self.user_global_ns = global_ns
1415
1423
1416 # Patch for global embedding to make sure that things don't overwrite
1424 # Patch for global embedding to make sure that things don't overwrite
1417 # user globals accidentally. Thanks to Richard <rxe@renre-europe.com>
1425 # user globals accidentally. Thanks to Richard <rxe@renre-europe.com>
1418 # FIXME. Test this a bit more carefully (the if.. is new)
1426 # FIXME. Test this a bit more carefully (the if.. is new)
1419 if local_ns is None and global_ns is None:
1427 if local_ns is None and global_ns is None:
1420 self.user_global_ns.update(__main__.__dict__)
1428 self.user_global_ns.update(__main__.__dict__)
1421
1429
1422 self.interact(header)
1430 self.interact(header)
1423
1431
1424 def interact(self, banner=None):
1432 def interact(self, banner=None):
1425 """Closely emulate the interactive Python console.
1433 """Closely emulate the interactive Python console.
1426
1434
1427 The optional banner argument specify the banner to print
1435 The optional banner argument specify the banner to print
1428 before the first interaction; by default it prints a banner
1436 before the first interaction; by default it prints a banner
1429 similar to the one printed by the real Python interpreter,
1437 similar to the one printed by the real Python interpreter,
1430 followed by the current class name in parentheses (so as not
1438 followed by the current class name in parentheses (so as not
1431 to confuse this with the real interpreter -- since it's so
1439 to confuse this with the real interpreter -- since it's so
1432 close!).
1440 close!).
1433
1441
1434 """
1442 """
1435 cprt = 'Type "copyright", "credits" or "license" for more information.'
1443 cprt = 'Type "copyright", "credits" or "license" for more information.'
1436 if banner is None:
1444 if banner is None:
1437 self.write("Python %s on %s\n%s\n(%s)\n" %
1445 self.write("Python %s on %s\n%s\n(%s)\n" %
1438 (sys.version, sys.platform, cprt,
1446 (sys.version, sys.platform, cprt,
1439 self.__class__.__name__))
1447 self.__class__.__name__))
1440 else:
1448 else:
1441 self.write(banner)
1449 self.write(banner)
1442
1450
1443 more = 0
1451 more = 0
1444
1452
1445 # Mark activity in the builtins
1453 # Mark activity in the builtins
1446 __builtin__.__dict__['__IPYTHON__active'] += 1
1454 __builtin__.__dict__['__IPYTHON__active'] += 1
1447
1455
1448 # exit_now is set by a call to %Exit or %Quit
1456 # exit_now is set by a call to %Exit or %Quit
1449 while not self.exit_now:
1457 while not self.exit_now:
1450 try:
1458 try:
1451 if more:
1459 if more:
1452 prompt = self.outputcache.prompt2
1460 prompt = self.outputcache.prompt2
1453 if self.autoindent:
1461 if self.autoindent:
1454 self.readline_startup_hook(self.pre_readline)
1462 self.readline_startup_hook(self.pre_readline)
1455 else:
1463 else:
1456 prompt = self.outputcache.prompt1
1464 prompt = self.outputcache.prompt1
1457 try:
1465 try:
1458 line = self.raw_input(prompt)
1466 line = self.raw_input(prompt)
1459 if self.autoindent:
1467 if self.autoindent:
1460 self.readline_startup_hook(None)
1468 self.readline_startup_hook(None)
1461 except EOFError:
1469 except EOFError:
1462 if self.autoindent:
1470 if self.autoindent:
1463 self.readline_startup_hook(None)
1471 self.readline_startup_hook(None)
1464 self.write("\n")
1472 self.write("\n")
1465 if self.rc.confirm_exit:
1473 if self.rc.confirm_exit:
1466 if ask_yes_no('Do you really want to exit ([y]/n)?','y'):
1474 if ask_yes_no('Do you really want to exit ([y]/n)?','y'):
1467 break
1475 break
1468 else:
1476 else:
1469 break
1477 break
1470 else:
1478 else:
1471 more = self.push(line)
1479 more = self.push(line)
1472 # Auto-indent management
1480 # Auto-indent management
1473 if self.autoindent:
1481 if self.autoindent:
1474 if line:
1482 if line:
1475 ini_spaces = re.match('^(\s+)',line)
1483 ini_spaces = re.match('^(\s+)',line)
1476 if ini_spaces:
1484 if ini_spaces:
1477 nspaces = ini_spaces.end()
1485 nspaces = ini_spaces.end()
1478 else:
1486 else:
1479 nspaces = 0
1487 nspaces = 0
1480 self.readline_indent = nspaces
1488 self.readline_indent = nspaces
1481
1489
1482 if line[-1] == ':':
1490 if line[-1] == ':':
1483 self.readline_indent += 4
1491 self.readline_indent += 4
1484 elif re.match(r'^\s+raise|^\s+return',line):
1492 elif re.match(r'^\s+raise|^\s+return',line):
1485 self.readline_indent -= 4
1493 self.readline_indent -= 4
1486 else:
1494 else:
1487 self.readline_indent = 0
1495 self.readline_indent = 0
1488
1496
1489 except KeyboardInterrupt:
1497 except KeyboardInterrupt:
1490 self.write("\nKeyboardInterrupt\n")
1498 self.write("\nKeyboardInterrupt\n")
1491 self.resetbuffer()
1499 self.resetbuffer()
1492 more = 0
1500 more = 0
1493 # keep cache in sync with the prompt counter:
1501 # keep cache in sync with the prompt counter:
1494 self.outputcache.prompt_count -= 1
1502 self.outputcache.prompt_count -= 1
1495
1503
1496 if self.autoindent:
1504 if self.autoindent:
1497 self.readline_indent = 0
1505 self.readline_indent = 0
1498
1506
1499 except bdb.BdbQuit:
1507 except bdb.BdbQuit:
1500 warn("The Python debugger has exited with a BdbQuit exception.\n"
1508 warn("The Python debugger has exited with a BdbQuit exception.\n"
1501 "Because of how pdb handles the stack, it is impossible\n"
1509 "Because of how pdb handles the stack, it is impossible\n"
1502 "for IPython to properly format this particular exception.\n"
1510 "for IPython to properly format this particular exception.\n"
1503 "IPython will resume normal operation.")
1511 "IPython will resume normal operation.")
1504
1512
1505 # We are off again...
1513 # We are off again...
1506 __builtin__.__dict__['__IPYTHON__active'] -= 1
1514 __builtin__.__dict__['__IPYTHON__active'] -= 1
1507
1515
1508 def excepthook(self, type, value, tb):
1516 def excepthook(self, type, value, tb):
1509 """One more defense for GUI apps that call sys.excepthook.
1517 """One more defense for GUI apps that call sys.excepthook.
1510
1518
1511 GUI frameworks like wxPython trap exceptions and call
1519 GUI frameworks like wxPython trap exceptions and call
1512 sys.excepthook themselves. I guess this is a feature that
1520 sys.excepthook themselves. I guess this is a feature that
1513 enables them to keep running after exceptions that would
1521 enables them to keep running after exceptions that would
1514 otherwise kill their mainloop. This is a bother for IPython
1522 otherwise kill their mainloop. This is a bother for IPython
1515 which excepts to catch all of the program exceptions with a try:
1523 which excepts to catch all of the program exceptions with a try:
1516 except: statement.
1524 except: statement.
1517
1525
1518 Normally, IPython sets sys.excepthook to a CrashHandler instance, so if
1526 Normally, IPython sets sys.excepthook to a CrashHandler instance, so if
1519 any app directly invokes sys.excepthook, it will look to the user like
1527 any app directly invokes sys.excepthook, it will look to the user like
1520 IPython crashed. In order to work around this, we can disable the
1528 IPython crashed. In order to work around this, we can disable the
1521 CrashHandler and replace it with this excepthook instead, which prints a
1529 CrashHandler and replace it with this excepthook instead, which prints a
1522 regular traceback using our InteractiveTB. In this fashion, apps which
1530 regular traceback using our InteractiveTB. In this fashion, apps which
1523 call sys.excepthook will generate a regular-looking exception from
1531 call sys.excepthook will generate a regular-looking exception from
1524 IPython, and the CrashHandler will only be triggered by real IPython
1532 IPython, and the CrashHandler will only be triggered by real IPython
1525 crashes.
1533 crashes.
1526
1534
1527 This hook should be used sparingly, only in places which are not likely
1535 This hook should be used sparingly, only in places which are not likely
1528 to be true IPython errors.
1536 to be true IPython errors.
1529 """
1537 """
1530
1538
1531 self.InteractiveTB(type, value, tb, tb_offset=0)
1539 self.InteractiveTB(type, value, tb, tb_offset=0)
1532 if self.InteractiveTB.call_pdb and self.has_readline:
1540 if self.InteractiveTB.call_pdb and self.has_readline:
1533 self.readline.set_completer(self.Completer.complete)
1541 self.readline.set_completer(self.Completer.complete)
1534
1542
1535 def call_alias(self,alias,rest=''):
1543 def call_alias(self,alias,rest=''):
1536 """Call an alias given its name and the rest of the line.
1544 """Call an alias given its name and the rest of the line.
1537
1545
1538 This function MUST be given a proper alias, because it doesn't make
1546 This function MUST be given a proper alias, because it doesn't make
1539 any checks when looking up into the alias table. The caller is
1547 any checks when looking up into the alias table. The caller is
1540 responsible for invoking it only with a valid alias."""
1548 responsible for invoking it only with a valid alias."""
1541
1549
1542 #print 'ALIAS: <%s>+<%s>' % (alias,rest) # dbg
1550 #print 'ALIAS: <%s>+<%s>' % (alias,rest) # dbg
1543 nargs,cmd = self.alias_table[alias]
1551 nargs,cmd = self.alias_table[alias]
1544 # Expand the %l special to be the user's input line
1552 # Expand the %l special to be the user's input line
1545 if cmd.find('%l') >= 0:
1553 if cmd.find('%l') >= 0:
1546 cmd = cmd.replace('%l',rest)
1554 cmd = cmd.replace('%l',rest)
1547 rest = ''
1555 rest = ''
1548 if nargs==0:
1556 if nargs==0:
1549 # Simple, argument-less aliases
1557 # Simple, argument-less aliases
1550 cmd = '%s %s' % (cmd,rest)
1558 cmd = '%s %s' % (cmd,rest)
1551 else:
1559 else:
1552 # Handle aliases with positional arguments
1560 # Handle aliases with positional arguments
1553 args = rest.split(None,nargs)
1561 args = rest.split(None,nargs)
1554 if len(args)< nargs:
1562 if len(args)< nargs:
1555 error('Alias <%s> requires %s arguments, %s given.' %
1563 error('Alias <%s> requires %s arguments, %s given.' %
1556 (alias,nargs,len(args)))
1564 (alias,nargs,len(args)))
1557 return
1565 return
1558 cmd = '%s %s' % (cmd % tuple(args[:nargs]),' '.join(args[nargs:]))
1566 cmd = '%s %s' % (cmd % tuple(args[:nargs]),' '.join(args[nargs:]))
1559 # Now call the macro, evaluating in the user's namespace
1567 # Now call the macro, evaluating in the user's namespace
1560 try:
1568 try:
1561 self.system(cmd)
1569 self.system(cmd)
1562 except:
1570 except:
1563 self.showtraceback()
1571 self.showtraceback()
1564
1572
1565 def runlines(self,lines):
1573 def runlines(self,lines):
1566 """Run a string of one or more lines of source.
1574 """Run a string of one or more lines of source.
1567
1575
1568 This method is capable of running a string containing multiple source
1576 This method is capable of running a string containing multiple source
1569 lines, as if they had been entered at the IPython prompt. Since it
1577 lines, as if they had been entered at the IPython prompt. Since it
1570 exposes IPython's processing machinery, the given strings can contain
1578 exposes IPython's processing machinery, the given strings can contain
1571 magic calls (%magic), special shell access (!cmd), etc."""
1579 magic calls (%magic), special shell access (!cmd), etc."""
1572
1580
1573 # We must start with a clean buffer, in case this is run from an
1581 # We must start with a clean buffer, in case this is run from an
1574 # interactive IPython session (via a magic, for example).
1582 # interactive IPython session (via a magic, for example).
1575 self.resetbuffer()
1583 self.resetbuffer()
1576 lines = lines.split('\n')
1584 lines = lines.split('\n')
1577 more = 0
1585 more = 0
1578 for line in lines:
1586 for line in lines:
1579 # skip blank lines so we don't mess up the prompt counter, but do
1587 # skip blank lines so we don't mess up the prompt counter, but do
1580 # NOT skip even a blank line if we are in a code block (more is
1588 # NOT skip even a blank line if we are in a code block (more is
1581 # true)
1589 # true)
1582 if line or more:
1590 if line or more:
1583 more = self.push((self.prefilter(line,more)))
1591 more = self.push((self.prefilter(line,more)))
1584 # IPython's runsource returns None if there was an error
1592 # IPython's runsource returns None if there was an error
1585 # compiling the code. This allows us to stop processing right
1593 # compiling the code. This allows us to stop processing right
1586 # away, so the user gets the error message at the right place.
1594 # away, so the user gets the error message at the right place.
1587 if more is None:
1595 if more is None:
1588 break
1596 break
1589 # final newline in case the input didn't have it, so that the code
1597 # final newline in case the input didn't have it, so that the code
1590 # actually does get executed
1598 # actually does get executed
1591 if more:
1599 if more:
1592 self.push('\n')
1600 self.push('\n')
1593
1601
1594 def runsource(self, source, filename="<input>", symbol="single"):
1602 def runsource(self, source, filename="<input>", symbol="single"):
1595 """Compile and run some source in the interpreter.
1603 """Compile and run some source in the interpreter.
1596
1604
1597 Arguments are as for compile_command().
1605 Arguments are as for compile_command().
1598
1606
1599 One several things can happen:
1607 One several things can happen:
1600
1608
1601 1) The input is incorrect; compile_command() raised an
1609 1) The input is incorrect; compile_command() raised an
1602 exception (SyntaxError or OverflowError). A syntax traceback
1610 exception (SyntaxError or OverflowError). A syntax traceback
1603 will be printed by calling the showsyntaxerror() method.
1611 will be printed by calling the showsyntaxerror() method.
1604
1612
1605 2) The input is incomplete, and more input is required;
1613 2) The input is incomplete, and more input is required;
1606 compile_command() returned None. Nothing happens.
1614 compile_command() returned None. Nothing happens.
1607
1615
1608 3) The input is complete; compile_command() returned a code
1616 3) The input is complete; compile_command() returned a code
1609 object. The code is executed by calling self.runcode() (which
1617 object. The code is executed by calling self.runcode() (which
1610 also handles run-time exceptions, except for SystemExit).
1618 also handles run-time exceptions, except for SystemExit).
1611
1619
1612 The return value is:
1620 The return value is:
1613
1621
1614 - True in case 2
1622 - True in case 2
1615
1623
1616 - False in the other cases, unless an exception is raised, where
1624 - False in the other cases, unless an exception is raised, where
1617 None is returned instead. This can be used by external callers to
1625 None is returned instead. This can be used by external callers to
1618 know whether to continue feeding input or not.
1626 know whether to continue feeding input or not.
1619
1627
1620 The return value can be used to decide whether to use sys.ps1 or
1628 The return value can be used to decide whether to use sys.ps1 or
1621 sys.ps2 to prompt the next line."""
1629 sys.ps2 to prompt the next line."""
1622
1630
1623 try:
1631 try:
1624 code = self.compile(source, filename, symbol)
1632 code = self.compile(source, filename, symbol)
1625 except (OverflowError, SyntaxError, ValueError):
1633 except (OverflowError, SyntaxError, ValueError):
1626 # Case 1
1634 # Case 1
1627 self.showsyntaxerror(filename)
1635 self.showsyntaxerror(filename)
1628 return None
1636 return None
1629
1637
1630 if code is None:
1638 if code is None:
1631 # Case 2
1639 # Case 2
1632 return True
1640 return True
1633
1641
1634 # Case 3
1642 # Case 3
1635 # We store the code object so that threaded shells and
1643 # We store the code object so that threaded shells and
1636 # custom exception handlers can access all this info if needed.
1644 # custom exception handlers can access all this info if needed.
1637 # The source corresponding to this can be obtained from the
1645 # The source corresponding to this can be obtained from the
1638 # buffer attribute as '\n'.join(self.buffer).
1646 # buffer attribute as '\n'.join(self.buffer).
1639 self.code_to_run = code
1647 self.code_to_run = code
1640 # now actually execute the code object
1648 # now actually execute the code object
1641 if self.runcode(code) == 0:
1649 if self.runcode(code) == 0:
1642 return False
1650 return False
1643 else:
1651 else:
1644 return None
1652 return None
1645
1653
1646 def runcode(self,code_obj):
1654 def runcode(self,code_obj):
1647 """Execute a code object.
1655 """Execute a code object.
1648
1656
1649 When an exception occurs, self.showtraceback() is called to display a
1657 When an exception occurs, self.showtraceback() is called to display a
1650 traceback.
1658 traceback.
1651
1659
1652 Return value: a flag indicating whether the code to be run completed
1660 Return value: a flag indicating whether the code to be run completed
1653 successfully:
1661 successfully:
1654
1662
1655 - 0: successful execution.
1663 - 0: successful execution.
1656 - 1: an error occurred.
1664 - 1: an error occurred.
1657 """
1665 """
1658
1666
1659 # Set our own excepthook in case the user code tries to call it
1667 # Set our own excepthook in case the user code tries to call it
1660 # directly, so that the IPython crash handler doesn't get triggered
1668 # directly, so that the IPython crash handler doesn't get triggered
1661 old_excepthook,sys.excepthook = sys.excepthook, self.excepthook
1669 old_excepthook,sys.excepthook = sys.excepthook, self.excepthook
1662 outflag = 1 # happens in more places, so it's easier as default
1670 outflag = 1 # happens in more places, so it's easier as default
1663 try:
1671 try:
1664 try:
1672 try:
1665 # Embedded instances require separate global/local namespaces
1673 # Embedded instances require separate global/local namespaces
1666 # so they can see both the surrounding (local) namespace and
1674 # so they can see both the surrounding (local) namespace and
1667 # the module-level globals when called inside another function.
1675 # the module-level globals when called inside another function.
1668 if self.embedded:
1676 if self.embedded:
1669 exec code_obj in self.user_global_ns, self.user_ns
1677 exec code_obj in self.user_global_ns, self.user_ns
1670 # Normal (non-embedded) instances should only have a single
1678 # Normal (non-embedded) instances should only have a single
1671 # namespace for user code execution, otherwise functions won't
1679 # namespace for user code execution, otherwise functions won't
1672 # see interactive top-level globals.
1680 # see interactive top-level globals.
1673 else:
1681 else:
1674 exec code_obj in self.user_ns
1682 exec code_obj in self.user_ns
1675 finally:
1683 finally:
1676 # Reset our crash handler in place
1684 # Reset our crash handler in place
1677 sys.excepthook = old_excepthook
1685 sys.excepthook = old_excepthook
1678 except SystemExit:
1686 except SystemExit:
1679 self.resetbuffer()
1687 self.resetbuffer()
1680 self.showtraceback()
1688 self.showtraceback()
1681 warn( __builtin__.exit,level=1)
1689 warn( __builtin__.exit,level=1)
1682 except self.custom_exceptions:
1690 except self.custom_exceptions:
1683 etype,value,tb = sys.exc_info()
1691 etype,value,tb = sys.exc_info()
1684 self.CustomTB(etype,value,tb)
1692 self.CustomTB(etype,value,tb)
1685 except:
1693 except:
1686 self.showtraceback()
1694 self.showtraceback()
1687 else:
1695 else:
1688 outflag = 0
1696 outflag = 0
1689 if code.softspace(sys.stdout, 0):
1697 if code.softspace(sys.stdout, 0):
1690 print
1698 print
1691 # Flush out code object which has been run (and source)
1699 # Flush out code object which has been run (and source)
1692 self.code_to_run = None
1700 self.code_to_run = None
1693 return outflag
1701 return outflag
1694
1702
1695 def raw_input(self, prompt=""):
1703 def raw_input(self, prompt=""):
1696 """Write a prompt and read a line.
1704 """Write a prompt and read a line.
1697
1705
1698 The returned line does not include the trailing newline.
1706 The returned line does not include the trailing newline.
1699 When the user enters the EOF key sequence, EOFError is raised.
1707 When the user enters the EOF key sequence, EOFError is raised.
1700
1708
1701 The base implementation uses the built-in function
1709 The base implementation uses the built-in function
1702 raw_input(); a subclass may replace this with a different
1710 raw_input(); a subclass may replace this with a different
1703 implementation.
1711 implementation.
1704 """
1712 """
1705 return self.prefilter(raw_input_original(prompt),
1713 return self.prefilter(raw_input_original(prompt),
1706 prompt==self.outputcache.prompt2)
1714 prompt==self.outputcache.prompt2)
1707
1715
1708 def split_user_input(self,line):
1716 def split_user_input(self,line):
1709 """Split user input into pre-char, function part and rest."""
1717 """Split user input into pre-char, function part and rest."""
1710
1718
1711 lsplit = self.line_split.match(line)
1719 lsplit = self.line_split.match(line)
1712 if lsplit is None: # no regexp match returns None
1720 if lsplit is None: # no regexp match returns None
1713 try:
1721 try:
1714 iFun,theRest = line.split(None,1)
1722 iFun,theRest = line.split(None,1)
1715 except ValueError:
1723 except ValueError:
1716 iFun,theRest = line,''
1724 iFun,theRest = line,''
1717 pre = re.match('^(\s*)(.*)',line).groups()[0]
1725 pre = re.match('^(\s*)(.*)',line).groups()[0]
1718 else:
1726 else:
1719 pre,iFun,theRest = lsplit.groups()
1727 pre,iFun,theRest = lsplit.groups()
1720
1728
1721 #print 'line:<%s>' % line # dbg
1729 #print 'line:<%s>' % line # dbg
1722 #print 'pre <%s> iFun <%s> rest <%s>' % (pre,iFun.strip(),theRest) # dbg
1730 #print 'pre <%s> iFun <%s> rest <%s>' % (pre,iFun.strip(),theRest) # dbg
1723 return pre,iFun.strip(),theRest
1731 return pre,iFun.strip(),theRest
1724
1732
1725 def _prefilter(self, line, continue_prompt):
1733 def _prefilter(self, line, continue_prompt):
1726 """Calls different preprocessors, depending on the form of line."""
1734 """Calls different preprocessors, depending on the form of line."""
1727
1735
1728 # All handlers *must* return a value, even if it's blank ('').
1736 # All handlers *must* return a value, even if it's blank ('').
1729
1737
1730 # Lines are NOT logged here. Handlers should process the line as
1738 # Lines are NOT logged here. Handlers should process the line as
1731 # needed, update the cache AND log it (so that the input cache array
1739 # needed, update the cache AND log it (so that the input cache array
1732 # stays synced).
1740 # stays synced).
1733
1741
1734 # This function is _very_ delicate, and since it's also the one which
1742 # This function is _very_ delicate, and since it's also the one which
1735 # determines IPython's response to user input, it must be as efficient
1743 # determines IPython's response to user input, it must be as efficient
1736 # as possible. For this reason it has _many_ returns in it, trying
1744 # as possible. For this reason it has _many_ returns in it, trying
1737 # always to exit as quickly as it can figure out what it needs to do.
1745 # always to exit as quickly as it can figure out what it needs to do.
1738
1746
1739 # This function is the main responsible for maintaining IPython's
1747 # This function is the main responsible for maintaining IPython's
1740 # behavior respectful of Python's semantics. So be _very_ careful if
1748 # behavior respectful of Python's semantics. So be _very_ careful if
1741 # making changes to anything here.
1749 # making changes to anything here.
1742
1750
1743 #.....................................................................
1751 #.....................................................................
1744 # Code begins
1752 # Code begins
1745
1753
1746 #if line.startswith('%crash'): raise RuntimeError,'Crash now!' # dbg
1754 #if line.startswith('%crash'): raise RuntimeError,'Crash now!' # dbg
1747
1755
1748 # save the line away in case we crash, so the post-mortem handler can
1756 # save the line away in case we crash, so the post-mortem handler can
1749 # record it
1757 # record it
1750 self._last_input_line = line
1758 self._last_input_line = line
1751
1759
1752 #print '***line: <%s>' % line # dbg
1760 #print '***line: <%s>' % line # dbg
1753
1761
1754 # the input history needs to track even empty lines
1762 # the input history needs to track even empty lines
1755 if not line.strip():
1763 if not line.strip():
1756 if not continue_prompt:
1764 if not continue_prompt:
1757 self.outputcache.prompt_count -= 1
1765 self.outputcache.prompt_count -= 1
1758 return self.handle_normal('',continue_prompt)
1766 return self.handle_normal('',continue_prompt)
1759
1767
1760 # print '***cont',continue_prompt # dbg
1768 # print '***cont',continue_prompt # dbg
1761 # special handlers are only allowed for single line statements
1769 # special handlers are only allowed for single line statements
1762 if continue_prompt and not self.rc.multi_line_specials:
1770 if continue_prompt and not self.rc.multi_line_specials:
1763 return self.handle_normal(line,continue_prompt)
1771 return self.handle_normal(line,continue_prompt)
1764
1772
1765 # For the rest, we need the structure of the input
1773 # For the rest, we need the structure of the input
1766 pre,iFun,theRest = self.split_user_input(line)
1774 pre,iFun,theRest = self.split_user_input(line)
1767 #print 'pre <%s> iFun <%s> rest <%s>' % (pre,iFun,theRest) # dbg
1775 #print 'pre <%s> iFun <%s> rest <%s>' % (pre,iFun,theRest) # dbg
1768
1776
1769 # First check for explicit escapes in the last/first character
1777 # First check for explicit escapes in the last/first character
1770 handler = None
1778 handler = None
1771 if line[-1] == self.ESC_HELP:
1779 if line[-1] == self.ESC_HELP:
1772 handler = self.esc_handlers.get(line[-1]) # the ? can be at the end
1780 handler = self.esc_handlers.get(line[-1]) # the ? can be at the end
1773 if handler is None:
1781 if handler is None:
1774 # look at the first character of iFun, NOT of line, so we skip
1782 # look at the first character of iFun, NOT of line, so we skip
1775 # leading whitespace in multiline input
1783 # leading whitespace in multiline input
1776 handler = self.esc_handlers.get(iFun[0:1])
1784 handler = self.esc_handlers.get(iFun[0:1])
1777 if handler is not None:
1785 if handler is not None:
1778 return handler(line,continue_prompt,pre,iFun,theRest)
1786 return handler(line,continue_prompt,pre,iFun,theRest)
1779 # Emacs ipython-mode tags certain input lines
1787 # Emacs ipython-mode tags certain input lines
1780 if line.endswith('# PYTHON-MODE'):
1788 if line.endswith('# PYTHON-MODE'):
1781 return self.handle_emacs(line,continue_prompt)
1789 return self.handle_emacs(line,continue_prompt)
1782
1790
1783 # Next, check if we can automatically execute this thing
1791 # Next, check if we can automatically execute this thing
1784
1792
1785 # Allow ! in multi-line statements if multi_line_specials is on:
1793 # Allow ! in multi-line statements if multi_line_specials is on:
1786 if continue_prompt and self.rc.multi_line_specials and \
1794 if continue_prompt and self.rc.multi_line_specials and \
1787 iFun.startswith(self.ESC_SHELL):
1795 iFun.startswith(self.ESC_SHELL):
1788 return self.handle_shell_escape(line,continue_prompt,
1796 return self.handle_shell_escape(line,continue_prompt,
1789 pre=pre,iFun=iFun,
1797 pre=pre,iFun=iFun,
1790 theRest=theRest)
1798 theRest=theRest)
1791
1799
1792 # Let's try to find if the input line is a magic fn
1800 # Let's try to find if the input line is a magic fn
1793 oinfo = None
1801 oinfo = None
1794 if hasattr(self,'magic_'+iFun):
1802 if hasattr(self,'magic_'+iFun):
1795 oinfo = self._ofind(iFun) # FIXME - _ofind is part of Magic
1803 oinfo = self._ofind(iFun) # FIXME - _ofind is part of Magic
1796 if oinfo['ismagic']:
1804 if oinfo['ismagic']:
1797 # Be careful not to call magics when a variable assignment is
1805 # Be careful not to call magics when a variable assignment is
1798 # being made (ls='hi', for example)
1806 # being made (ls='hi', for example)
1799 if self.rc.automagic and \
1807 if self.rc.automagic and \
1800 (len(theRest)==0 or theRest[0] not in '!=()<>,') and \
1808 (len(theRest)==0 or theRest[0] not in '!=()<>,') and \
1801 (self.rc.multi_line_specials or not continue_prompt):
1809 (self.rc.multi_line_specials or not continue_prompt):
1802 return self.handle_magic(line,continue_prompt,
1810 return self.handle_magic(line,continue_prompt,
1803 pre,iFun,theRest)
1811 pre,iFun,theRest)
1804 else:
1812 else:
1805 return self.handle_normal(line,continue_prompt)
1813 return self.handle_normal(line,continue_prompt)
1806
1814
1807 # If the rest of the line begins with an (in)equality, assginment or
1815 # If the rest of the line begins with an (in)equality, assginment or
1808 # function call, we should not call _ofind but simply execute it.
1816 # function call, we should not call _ofind but simply execute it.
1809 # This avoids spurious geattr() accesses on objects upon assignment.
1817 # This avoids spurious geattr() accesses on objects upon assignment.
1810 #
1818 #
1811 # It also allows users to assign to either alias or magic names true
1819 # It also allows users to assign to either alias or magic names true
1812 # python variables (the magic/alias systems always take second seat to
1820 # python variables (the magic/alias systems always take second seat to
1813 # true python code).
1821 # true python code).
1814 if theRest and theRest[0] in '!=()':
1822 if theRest and theRest[0] in '!=()':
1815 return self.handle_normal(line,continue_prompt)
1823 return self.handle_normal(line,continue_prompt)
1816
1824
1817 if oinfo is None:
1825 if oinfo is None:
1818 oinfo = self._ofind(iFun) # FIXME - _ofind is part of Magic
1826 oinfo = self._ofind(iFun) # FIXME - _ofind is part of Magic
1819
1827
1820 if not oinfo['found']:
1828 if not oinfo['found']:
1821 return self.handle_normal(line,continue_prompt)
1829 return self.handle_normal(line,continue_prompt)
1822 else:
1830 else:
1823 #print 'iFun <%s> rest <%s>' % (iFun,theRest) # dbg
1831 #print 'iFun <%s> rest <%s>' % (iFun,theRest) # dbg
1824 if oinfo['isalias']:
1832 if oinfo['isalias']:
1825 return self.handle_alias(line,continue_prompt,
1833 return self.handle_alias(line,continue_prompt,
1826 pre,iFun,theRest)
1834 pre,iFun,theRest)
1827
1835
1828 if self.rc.autocall and \
1836 if self.rc.autocall and \
1829 not self.re_exclude_auto.match(theRest) and \
1837 not self.re_exclude_auto.match(theRest) and \
1830 self.re_fun_name.match(iFun) and \
1838 self.re_fun_name.match(iFun) and \
1831 callable(oinfo['obj']) :
1839 callable(oinfo['obj']) :
1832 #print 'going auto' # dbg
1840 #print 'going auto' # dbg
1833 return self.handle_auto(line,continue_prompt,pre,iFun,theRest)
1841 return self.handle_auto(line,continue_prompt,pre,iFun,theRest)
1834 else:
1842 else:
1835 #print 'was callable?', callable(oinfo['obj']) # dbg
1843 #print 'was callable?', callable(oinfo['obj']) # dbg
1836 return self.handle_normal(line,continue_prompt)
1844 return self.handle_normal(line,continue_prompt)
1837
1845
1838 # If we get here, we have a normal Python line. Log and return.
1846 # If we get here, we have a normal Python line. Log and return.
1839 return self.handle_normal(line,continue_prompt)
1847 return self.handle_normal(line,continue_prompt)
1840
1848
1841 def _prefilter_dumb(self, line, continue_prompt):
1849 def _prefilter_dumb(self, line, continue_prompt):
1842 """simple prefilter function, for debugging"""
1850 """simple prefilter function, for debugging"""
1843 return self.handle_normal(line,continue_prompt)
1851 return self.handle_normal(line,continue_prompt)
1844
1852
1845 # Set the default prefilter() function (this can be user-overridden)
1853 # Set the default prefilter() function (this can be user-overridden)
1846 prefilter = _prefilter
1854 prefilter = _prefilter
1847
1855
1848 def handle_normal(self,line,continue_prompt=None,
1856 def handle_normal(self,line,continue_prompt=None,
1849 pre=None,iFun=None,theRest=None):
1857 pre=None,iFun=None,theRest=None):
1850 """Handle normal input lines. Use as a template for handlers."""
1858 """Handle normal input lines. Use as a template for handlers."""
1851
1859
1852 self.log(line,continue_prompt)
1860 self.log(line,continue_prompt)
1853 self.update_cache(line)
1861 self.update_cache(line)
1854 return line
1862 return line
1855
1863
1856 def handle_alias(self,line,continue_prompt=None,
1864 def handle_alias(self,line,continue_prompt=None,
1857 pre=None,iFun=None,theRest=None):
1865 pre=None,iFun=None,theRest=None):
1858 """Handle alias input lines. """
1866 """Handle alias input lines. """
1859
1867
1860 theRest = esc_quotes(theRest)
1868 theRest = esc_quotes(theRest)
1861 line_out = "%s%s.call_alias('%s','%s')" % (pre,self.name,iFun,theRest)
1869 line_out = "%s%s.call_alias('%s','%s')" % (pre,self.name,iFun,theRest)
1862 self.log(line_out,continue_prompt)
1870 self.log(line_out,continue_prompt)
1863 self.update_cache(line_out)
1871 self.update_cache(line_out)
1864 return line_out
1872 return line_out
1865
1873
1866 def handle_shell_escape(self, line, continue_prompt=None,
1874 def handle_shell_escape(self, line, continue_prompt=None,
1867 pre=None,iFun=None,theRest=None):
1875 pre=None,iFun=None,theRest=None):
1868 """Execute the line in a shell, empty return value"""
1876 """Execute the line in a shell, empty return value"""
1869
1877
1870 #print 'line in :', `line` # dbg
1878 #print 'line in :', `line` # dbg
1871 # Example of a special handler. Others follow a similar pattern.
1879 # Example of a special handler. Others follow a similar pattern.
1872 if continue_prompt: # multi-line statements
1880 if continue_prompt: # multi-line statements
1873 if iFun.startswith('!!'):
1881 if iFun.startswith('!!'):
1874 print 'SyntaxError: !! is not allowed in multiline statements'
1882 print 'SyntaxError: !! is not allowed in multiline statements'
1875 return pre
1883 return pre
1876 else:
1884 else:
1877 cmd = ("%s %s" % (iFun[1:],theRest)).replace('"','\\"')
1885 cmd = ("%s %s" % (iFun[1:],theRest)).replace('"','\\"')
1878 line_out = '%s%s.system("%s")' % (pre,self.name,cmd)
1886 line_out = '%s%s.system("%s")' % (pre,self.name,cmd)
1879 #line_out = ('%s%s.system(' % (pre,self.name)) + repr(cmd) + ')'
1887 #line_out = ('%s%s.system(' % (pre,self.name)) + repr(cmd) + ')'
1880 else: # single-line input
1888 else: # single-line input
1881 if line.startswith('!!'):
1889 if line.startswith('!!'):
1882 # rewrite iFun/theRest to properly hold the call to %sx and
1890 # rewrite iFun/theRest to properly hold the call to %sx and
1883 # the actual command to be executed, so handle_magic can work
1891 # the actual command to be executed, so handle_magic can work
1884 # correctly
1892 # correctly
1885 theRest = '%s %s' % (iFun[2:],theRest)
1893 theRest = '%s %s' % (iFun[2:],theRest)
1886 iFun = 'sx'
1894 iFun = 'sx'
1887 return self.handle_magic('%ssx %s' % (self.ESC_MAGIC,line[2:]),
1895 return self.handle_magic('%ssx %s' % (self.ESC_MAGIC,line[2:]),
1888 continue_prompt,pre,iFun,theRest)
1896 continue_prompt,pre,iFun,theRest)
1889 else:
1897 else:
1890 cmd = esc_quotes(line[1:])
1898 cmd = esc_quotes(line[1:])
1891 line_out = '%s.system("%s")' % (self.name,cmd)
1899 line_out = '%s.system("%s")' % (self.name,cmd)
1892 #line_out = ('%s.system(' % self.name) + repr(cmd)+ ')'
1900 #line_out = ('%s.system(' % self.name) + repr(cmd)+ ')'
1893 # update cache/log and return
1901 # update cache/log and return
1894 self.log(line_out,continue_prompt)
1902 self.log(line_out,continue_prompt)
1895 self.update_cache(line_out) # readline cache gets normal line
1903 self.update_cache(line_out) # readline cache gets normal line
1896 #print 'line out r:', `line_out` # dbg
1904 #print 'line out r:', `line_out` # dbg
1897 #print 'line out s:', line_out # dbg
1905 #print 'line out s:', line_out # dbg
1898 return line_out
1906 return line_out
1899
1907
1900 def handle_magic(self, line, continue_prompt=None,
1908 def handle_magic(self, line, continue_prompt=None,
1901 pre=None,iFun=None,theRest=None):
1909 pre=None,iFun=None,theRest=None):
1902 """Execute magic functions.
1910 """Execute magic functions.
1903
1911
1904 Also log them with a prepended # so the log is clean Python."""
1912 Also log them with a prepended # so the log is clean Python."""
1905
1913
1906 cmd = '%sipmagic("%s")' % (pre,esc_quotes('%s %s' % (iFun,theRest)))
1914 cmd = '%sipmagic("%s")' % (pre,esc_quotes('%s %s' % (iFun,theRest)))
1907 self.log(cmd,continue_prompt)
1915 self.log(cmd,continue_prompt)
1908 self.update_cache(line)
1916 self.update_cache(line)
1909 #print 'in handle_magic, cmd=<%s>' % cmd # dbg
1917 #print 'in handle_magic, cmd=<%s>' % cmd # dbg
1910 return cmd
1918 return cmd
1911
1919
1912 def handle_auto(self, line, continue_prompt=None,
1920 def handle_auto(self, line, continue_prompt=None,
1913 pre=None,iFun=None,theRest=None):
1921 pre=None,iFun=None,theRest=None):
1914 """Hande lines which can be auto-executed, quoting if requested."""
1922 """Hande lines which can be auto-executed, quoting if requested."""
1915
1923
1916 #print 'pre <%s> iFun <%s> rest <%s>' % (pre,iFun,theRest) # dbg
1924 #print 'pre <%s> iFun <%s> rest <%s>' % (pre,iFun,theRest) # dbg
1917
1925
1918 # This should only be active for single-line input!
1926 # This should only be active for single-line input!
1919 if continue_prompt:
1927 if continue_prompt:
1920 return line
1928 return line
1921
1929
1922 if pre == self.ESC_QUOTE:
1930 if pre == self.ESC_QUOTE:
1923 # Auto-quote splitting on whitespace
1931 # Auto-quote splitting on whitespace
1924 newcmd = '%s("%s")' % (iFun,'", "'.join(theRest.split()) )
1932 newcmd = '%s("%s")' % (iFun,'", "'.join(theRest.split()) )
1925 elif pre == self.ESC_QUOTE2:
1933 elif pre == self.ESC_QUOTE2:
1926 # Auto-quote whole string
1934 # Auto-quote whole string
1927 newcmd = '%s("%s")' % (iFun,theRest)
1935 newcmd = '%s("%s")' % (iFun,theRest)
1928 else:
1936 else:
1929 # Auto-paren
1937 # Auto-paren
1930 if theRest[0:1] in ('=','['):
1938 if theRest[0:1] in ('=','['):
1931 # Don't autocall in these cases. They can be either
1939 # Don't autocall in these cases. They can be either
1932 # rebindings of an existing callable's name, or item access
1940 # rebindings of an existing callable's name, or item access
1933 # for an object which is BOTH callable and implements
1941 # for an object which is BOTH callable and implements
1934 # __getitem__.
1942 # __getitem__.
1935 return '%s %s' % (iFun,theRest)
1943 return '%s %s' % (iFun,theRest)
1936 if theRest.endswith(';'):
1944 if theRest.endswith(';'):
1937 newcmd = '%s(%s);' % (iFun.rstrip(),theRest[:-1])
1945 newcmd = '%s(%s);' % (iFun.rstrip(),theRest[:-1])
1938 else:
1946 else:
1939 newcmd = '%s(%s)' % (iFun.rstrip(),theRest)
1947 newcmd = '%s(%s)' % (iFun.rstrip(),theRest)
1940
1948
1941 print >>Term.cout, self.outputcache.prompt1.auto_rewrite() + newcmd
1949 print >>Term.cout, self.outputcache.prompt1.auto_rewrite() + newcmd
1942 # log what is now valid Python, not the actual user input (without the
1950 # log what is now valid Python, not the actual user input (without the
1943 # final newline)
1951 # final newline)
1944 self.log(newcmd,continue_prompt)
1952 self.log(newcmd,continue_prompt)
1945 return newcmd
1953 return newcmd
1946
1954
1947 def handle_help(self, line, continue_prompt=None,
1955 def handle_help(self, line, continue_prompt=None,
1948 pre=None,iFun=None,theRest=None):
1956 pre=None,iFun=None,theRest=None):
1949 """Try to get some help for the object.
1957 """Try to get some help for the object.
1950
1958
1951 obj? or ?obj -> basic information.
1959 obj? or ?obj -> basic information.
1952 obj?? or ??obj -> more details.
1960 obj?? or ??obj -> more details.
1953 """
1961 """
1954
1962
1955 # We need to make sure that we don't process lines which would be
1963 # We need to make sure that we don't process lines which would be
1956 # otherwise valid python, such as "x=1 # what?"
1964 # otherwise valid python, such as "x=1 # what?"
1957 try:
1965 try:
1958 code.compile_command(line)
1966 code.compile_command(line)
1959 except SyntaxError:
1967 except SyntaxError:
1960 # We should only handle as help stuff which is NOT valid syntax
1968 # We should only handle as help stuff which is NOT valid syntax
1961 if line[0]==self.ESC_HELP:
1969 if line[0]==self.ESC_HELP:
1962 line = line[1:]
1970 line = line[1:]
1963 elif line[-1]==self.ESC_HELP:
1971 elif line[-1]==self.ESC_HELP:
1964 line = line[:-1]
1972 line = line[:-1]
1965 self.log('#?'+line)
1973 self.log('#?'+line)
1966 self.update_cache(line)
1974 self.update_cache(line)
1967 if line:
1975 if line:
1968 self.magic_pinfo(line)
1976 self.magic_pinfo(line)
1969 else:
1977 else:
1970 page(self.usage,screen_lines=self.rc.screen_length)
1978 page(self.usage,screen_lines=self.rc.screen_length)
1971 return '' # Empty string is needed here!
1979 return '' # Empty string is needed here!
1972 except:
1980 except:
1973 # Pass any other exceptions through to the normal handler
1981 # Pass any other exceptions through to the normal handler
1974 return self.handle_normal(line,continue_prompt)
1982 return self.handle_normal(line,continue_prompt)
1975 else:
1983 else:
1976 # If the code compiles ok, we should handle it normally
1984 # If the code compiles ok, we should handle it normally
1977 return self.handle_normal(line,continue_prompt)
1985 return self.handle_normal(line,continue_prompt)
1978
1986
1979 def handle_emacs(self,line,continue_prompt=None,
1987 def handle_emacs(self,line,continue_prompt=None,
1980 pre=None,iFun=None,theRest=None):
1988 pre=None,iFun=None,theRest=None):
1981 """Handle input lines marked by python-mode."""
1989 """Handle input lines marked by python-mode."""
1982
1990
1983 # Currently, nothing is done. Later more functionality can be added
1991 # Currently, nothing is done. Later more functionality can be added
1984 # here if needed.
1992 # here if needed.
1985
1993
1986 # The input cache shouldn't be updated
1994 # The input cache shouldn't be updated
1987
1995
1988 return line
1996 return line
1989
1997
1990 def write(self,data):
1998 def write(self,data):
1991 """Write a string to the default output"""
1999 """Write a string to the default output"""
1992 Term.cout.write(data)
2000 Term.cout.write(data)
1993
2001
1994 def write_err(self,data):
2002 def write_err(self,data):
1995 """Write a string to the default error output"""
2003 """Write a string to the default error output"""
1996 Term.cerr.write(data)
2004 Term.cerr.write(data)
1997
2005
1998 def safe_execfile(self,fname,*where,**kw):
2006 def safe_execfile(self,fname,*where,**kw):
1999 fname = os.path.expanduser(fname)
2007 fname = os.path.expanduser(fname)
2000
2008
2001 # find things also in current directory
2009 # find things also in current directory
2002 dname = os.path.dirname(fname)
2010 dname = os.path.dirname(fname)
2003 if not sys.path.count(dname):
2011 if not sys.path.count(dname):
2004 sys.path.append(dname)
2012 sys.path.append(dname)
2005
2013
2006 try:
2014 try:
2007 xfile = open(fname)
2015 xfile = open(fname)
2008 except:
2016 except:
2009 print >> Term.cerr, \
2017 print >> Term.cerr, \
2010 'Could not open file <%s> for safe execution.' % fname
2018 'Could not open file <%s> for safe execution.' % fname
2011 return None
2019 return None
2012
2020
2013 kw.setdefault('islog',0)
2021 kw.setdefault('islog',0)
2014 kw.setdefault('quiet',1)
2022 kw.setdefault('quiet',1)
2015 kw.setdefault('exit_ignore',0)
2023 kw.setdefault('exit_ignore',0)
2016 first = xfile.readline()
2024 first = xfile.readline()
2017 _LOGHEAD = str(self.LOGHEAD).split('\n',1)[0].strip()
2025 _LOGHEAD = str(self.LOGHEAD).split('\n',1)[0].strip()
2018 xfile.close()
2026 xfile.close()
2019 # line by line execution
2027 # line by line execution
2020 if first.startswith(_LOGHEAD) or kw['islog']:
2028 if first.startswith(_LOGHEAD) or kw['islog']:
2021 print 'Loading log file <%s> one line at a time...' % fname
2029 print 'Loading log file <%s> one line at a time...' % fname
2022 if kw['quiet']:
2030 if kw['quiet']:
2023 stdout_save = sys.stdout
2031 stdout_save = sys.stdout
2024 sys.stdout = StringIO.StringIO()
2032 sys.stdout = StringIO.StringIO()
2025 try:
2033 try:
2026 globs,locs = where[0:2]
2034 globs,locs = where[0:2]
2027 except:
2035 except:
2028 try:
2036 try:
2029 globs = locs = where[0]
2037 globs = locs = where[0]
2030 except:
2038 except:
2031 globs = locs = globals()
2039 globs = locs = globals()
2032 badblocks = []
2040 badblocks = []
2033
2041
2034 # we also need to identify indented blocks of code when replaying
2042 # we also need to identify indented blocks of code when replaying
2035 # logs and put them together before passing them to an exec
2043 # logs and put them together before passing them to an exec
2036 # statement. This takes a bit of regexp and look-ahead work in the
2044 # statement. This takes a bit of regexp and look-ahead work in the
2037 # file. It's easiest if we swallow the whole thing in memory
2045 # file. It's easiest if we swallow the whole thing in memory
2038 # first, and manually walk through the lines list moving the
2046 # first, and manually walk through the lines list moving the
2039 # counter ourselves.
2047 # counter ourselves.
2040 indent_re = re.compile('\s+\S')
2048 indent_re = re.compile('\s+\S')
2041 xfile = open(fname)
2049 xfile = open(fname)
2042 filelines = xfile.readlines()
2050 filelines = xfile.readlines()
2043 xfile.close()
2051 xfile.close()
2044 nlines = len(filelines)
2052 nlines = len(filelines)
2045 lnum = 0
2053 lnum = 0
2046 while lnum < nlines:
2054 while lnum < nlines:
2047 line = filelines[lnum]
2055 line = filelines[lnum]
2048 lnum += 1
2056 lnum += 1
2049 # don't re-insert logger status info into cache
2057 # don't re-insert logger status info into cache
2050 if line.startswith('#log#'):
2058 if line.startswith('#log#'):
2051 continue
2059 continue
2052 elif line.startswith('#%s'% self.ESC_MAGIC):
2060 elif line.startswith('#%s'% self.ESC_MAGIC):
2053 self.update_cache(line[1:])
2061 self.update_cache(line[1:])
2054 line = magic2python(line)
2062 line = magic2python(line)
2055 elif line.startswith('#!'):
2063 elif line.startswith('#!'):
2056 self.update_cache(line[1:])
2064 self.update_cache(line[1:])
2057 else:
2065 else:
2058 # build a block of code (maybe a single line) for execution
2066 # build a block of code (maybe a single line) for execution
2059 block = line
2067 block = line
2060 try:
2068 try:
2061 next = filelines[lnum] # lnum has already incremented
2069 next = filelines[lnum] # lnum has already incremented
2062 except:
2070 except:
2063 next = None
2071 next = None
2064 while next and indent_re.match(next):
2072 while next and indent_re.match(next):
2065 block += next
2073 block += next
2066 lnum += 1
2074 lnum += 1
2067 try:
2075 try:
2068 next = filelines[lnum]
2076 next = filelines[lnum]
2069 except:
2077 except:
2070 next = None
2078 next = None
2071 # now execute the block of one or more lines
2079 # now execute the block of one or more lines
2072 try:
2080 try:
2073 exec block in globs,locs
2081 exec block in globs,locs
2074 self.update_cache(block.rstrip())
2082 self.update_cache(block.rstrip())
2075 except SystemExit:
2083 except SystemExit:
2076 pass
2084 pass
2077 except:
2085 except:
2078 badblocks.append(block.rstrip())
2086 badblocks.append(block.rstrip())
2079 if kw['quiet']: # restore stdout
2087 if kw['quiet']: # restore stdout
2080 sys.stdout.close()
2088 sys.stdout.close()
2081 sys.stdout = stdout_save
2089 sys.stdout = stdout_save
2082 print 'Finished replaying log file <%s>' % fname
2090 print 'Finished replaying log file <%s>' % fname
2083 if badblocks:
2091 if badblocks:
2084 print >> sys.stderr, \
2092 print >> sys.stderr, \
2085 '\nThe following lines/blocks in file <%s> reported errors:' \
2093 '\nThe following lines/blocks in file <%s> reported errors:' \
2086 % fname
2094 % fname
2087 for badline in badblocks:
2095 for badline in badblocks:
2088 print >> sys.stderr, badline
2096 print >> sys.stderr, badline
2089 else: # regular file execution
2097 else: # regular file execution
2090 try:
2098 try:
2091 execfile(fname,*where)
2099 execfile(fname,*where)
2092 except SyntaxError:
2100 except SyntaxError:
2093 etype, evalue = sys.exc_info()[0:2]
2101 etype, evalue = sys.exc_info()[0:2]
2094 self.SyntaxTB(etype,evalue,[])
2102 self.SyntaxTB(etype,evalue,[])
2095 warn('Failure executing file: <%s>' % fname)
2103 warn('Failure executing file: <%s>' % fname)
2096 except SystemExit,status:
2104 except SystemExit,status:
2097 if not kw['exit_ignore']:
2105 if not kw['exit_ignore']:
2098 self.InteractiveTB()
2106 self.InteractiveTB()
2099 warn('Failure executing file: <%s>' % fname)
2107 warn('Failure executing file: <%s>' % fname)
2100 except:
2108 except:
2101 self.InteractiveTB()
2109 self.InteractiveTB()
2102 warn('Failure executing file: <%s>' % fname)
2110 warn('Failure executing file: <%s>' % fname)
2103
2111
2104 #************************* end of file <iplib.py> *****************************
2112 #************************* end of file <iplib.py> *****************************
@@ -1,860 +1,778 b''
1 # -*- coding: utf-8 -*-
1 # -*- coding: utf-8 -*-
2 """
2 """
3 ultraTB.py -- Spice up your tracebacks!
3 ultraTB.py -- Spice up your tracebacks!
4
4
5 * ColorTB
5 * ColorTB
6 I've always found it a bit hard to visually parse tracebacks in Python. The
6 I've always found it a bit hard to visually parse tracebacks in Python. The
7 ColorTB class is a solution to that problem. It colors the different parts of a
7 ColorTB class is a solution to that problem. It colors the different parts of a
8 traceback in a manner similar to what you would expect from a syntax-highlighting
8 traceback in a manner similar to what you would expect from a syntax-highlighting
9 text editor.
9 text editor.
10
10
11 Installation instructions for ColorTB:
11 Installation instructions for ColorTB:
12 import sys,ultraTB
12 import sys,ultraTB
13 sys.excepthook = ultraTB.ColorTB()
13 sys.excepthook = ultraTB.ColorTB()
14
14
15 * VerboseTB
15 * VerboseTB
16 I've also included a port of Ka-Ping Yee's "cgitb.py" that produces all kinds
16 I've also included a port of Ka-Ping Yee's "cgitb.py" that produces all kinds
17 of useful info when a traceback occurs. Ping originally had it spit out HTML
17 of useful info when a traceback occurs. Ping originally had it spit out HTML
18 and intended it for CGI programmers, but why should they have all the fun? I
18 and intended it for CGI programmers, but why should they have all the fun? I
19 altered it to spit out colored text to the terminal. It's a bit overwhelming,
19 altered it to spit out colored text to the terminal. It's a bit overwhelming,
20 but kind of neat, and maybe useful for long-running programs that you believe
20 but kind of neat, and maybe useful for long-running programs that you believe
21 are bug-free. If a crash *does* occur in that type of program you want details.
21 are bug-free. If a crash *does* occur in that type of program you want details.
22 Give it a shot--you'll love it or you'll hate it.
22 Give it a shot--you'll love it or you'll hate it.
23
23
24 Note:
24 Note:
25
25
26 The Verbose mode prints the variables currently visible where the exception
26 The Verbose mode prints the variables currently visible where the exception
27 happened (shortening their strings if too long). This can potentially be
27 happened (shortening their strings if too long). This can potentially be
28 very slow, if you happen to have a huge data structure whose string
28 very slow, if you happen to have a huge data structure whose string
29 representation is complex to compute. Your computer may appear to freeze for
29 representation is complex to compute. Your computer may appear to freeze for
30 a while with cpu usage at 100%. If this occurs, you can cancel the traceback
30 a while with cpu usage at 100%. If this occurs, you can cancel the traceback
31 with Ctrl-C (maybe hitting it more than once).
31 with Ctrl-C (maybe hitting it more than once).
32
32
33 If you encounter this kind of situation often, you may want to use the
33 If you encounter this kind of situation often, you may want to use the
34 Verbose_novars mode instead of the regular Verbose, which avoids formatting
34 Verbose_novars mode instead of the regular Verbose, which avoids formatting
35 variables (but otherwise includes the information and context given by
35 variables (but otherwise includes the information and context given by
36 Verbose).
36 Verbose).
37
37
38
38
39 Installation instructions for ColorTB:
39 Installation instructions for ColorTB:
40 import sys,ultraTB
40 import sys,ultraTB
41 sys.excepthook = ultraTB.VerboseTB()
41 sys.excepthook = ultraTB.VerboseTB()
42
42
43 Note: Much of the code in this module was lifted verbatim from the standard
43 Note: Much of the code in this module was lifted verbatim from the standard
44 library module 'traceback.py' and Ka-Ping Yee's 'cgitb.py'.
44 library module 'traceback.py' and Ka-Ping Yee's 'cgitb.py'.
45
45
46 * Color schemes
46 * Color schemes
47 The colors are defined in the class TBTools through the use of the
47 The colors are defined in the class TBTools through the use of the
48 ColorSchemeTable class. Currently the following exist:
48 ColorSchemeTable class. Currently the following exist:
49
49
50 - NoColor: allows all of this module to be used in any terminal (the color
50 - NoColor: allows all of this module to be used in any terminal (the color
51 escapes are just dummy blank strings).
51 escapes are just dummy blank strings).
52
52
53 - Linux: is meant to look good in a terminal like the Linux console (black
53 - Linux: is meant to look good in a terminal like the Linux console (black
54 or very dark background).
54 or very dark background).
55
55
56 - LightBG: similar to Linux but swaps dark/light colors to be more readable
56 - LightBG: similar to Linux but swaps dark/light colors to be more readable
57 in light background terminals.
57 in light background terminals.
58
58
59 You can implement other color schemes easily, the syntax is fairly
59 You can implement other color schemes easily, the syntax is fairly
60 self-explanatory. Please send back new schemes you develop to the author for
60 self-explanatory. Please send back new schemes you develop to the author for
61 possible inclusion in future releases.
61 possible inclusion in future releases.
62
62
63 $Id: ultraTB.py 703 2005-08-16 17:34:44Z fperez $"""
63 $Id: ultraTB.py 951 2005-12-25 00:57:24Z fperez $"""
64
64
65 #*****************************************************************************
65 #*****************************************************************************
66 # Copyright (C) 2001 Nathaniel Gray <n8gray@caltech.edu>
66 # Copyright (C) 2001 Nathaniel Gray <n8gray@caltech.edu>
67 # Copyright (C) 2001-2004 Fernando Perez <fperez@colorado.edu>
67 # Copyright (C) 2001-2004 Fernando Perez <fperez@colorado.edu>
68 #
68 #
69 # Distributed under the terms of the BSD License. The full license is in
69 # Distributed under the terms of the BSD License. The full license is in
70 # the file COPYING, distributed as part of this software.
70 # the file COPYING, distributed as part of this software.
71 #*****************************************************************************
71 #*****************************************************************************
72
72
73 from IPython import Release
73 from IPython import Release
74 __author__ = '%s <%s>\n%s <%s>' % (Release.authors['Nathan']+
74 __author__ = '%s <%s>\n%s <%s>' % (Release.authors['Nathan']+
75 Release.authors['Fernando'])
75 Release.authors['Fernando'])
76 __license__ = Release.license
76 __license__ = Release.license
77
77
78 # Required modules
78 # Required modules
79 import sys, os, traceback, types, string, time
79 import sys, os, traceback, types, string, time
80 import keyword, tokenize, linecache, inspect, pydoc
80 import keyword, tokenize, linecache, inspect, pydoc
81 from UserDict import UserDict
81 from UserDict import UserDict
82
82
83 # IPython's own modules
83 # IPython's own modules
84 # Modified pdb which doesn't damage IPython's readline handling
84 # Modified pdb which doesn't damage IPython's readline handling
85 from IPython import Debugger
85 from IPython import Debugger
86
86
87 from IPython.Struct import Struct
87 from IPython.Struct import Struct
88 from IPython.ColorANSI import *
89 from IPython.genutils import Term,uniq_stable,error,info
88 from IPython.genutils import Term,uniq_stable,error,info
89 from IPython.excolors import ExceptionColors
90
90
91 #---------------------------------------------------------------------------
91 #---------------------------------------------------------------------------
92 # Code begins
92 # Code begins
93
93
94 def inspect_error():
94 def inspect_error():
95 """Print a message about internal inspect errors.
95 """Print a message about internal inspect errors.
96
96
97 These are unfortunately quite common."""
97 These are unfortunately quite common."""
98
98
99 error('Internal Python error in the inspect module.\n'
99 error('Internal Python error in the inspect module.\n'
100 'Below is the traceback from this internal error.\n')
100 'Below is the traceback from this internal error.\n')
101
101
102 # Make a global variable out of the color scheme table used for coloring
103 # exception tracebacks. This allows user code to add new schemes at runtime.
104 ExceptionColors = ColorSchemeTable()
105
106 # Populate it with color schemes
107 C = TermColors # shorthand and local lookup
108 ExceptionColors.add_scheme(ColorScheme(
109 'NoColor',
110 # The color to be used for the top line
111 topline = C.NoColor,
112
113 # The colors to be used in the traceback
114 filename = C.NoColor,
115 lineno = C.NoColor,
116 name = C.NoColor,
117 vName = C.NoColor,
118 val = C.NoColor,
119 em = C.NoColor,
120
121 # Emphasized colors for the last frame of the traceback
122 normalEm = C.NoColor,
123 filenameEm = C.NoColor,
124 linenoEm = C.NoColor,
125 nameEm = C.NoColor,
126 valEm = C.NoColor,
127
128 # Colors for printing the exception
129 excName = C.NoColor,
130 line = C.NoColor,
131 caret = C.NoColor,
132 Normal = C.NoColor
133 ))
134
135 # make some schemes as instances so we can copy them for modification easily
136 ExceptionColors.add_scheme(ColorScheme(
137 'Linux',
138 # The color to be used for the top line
139 topline = C.LightRed,
140
141 # The colors to be used in the traceback
142 filename = C.Green,
143 lineno = C.Green,
144 name = C.Purple,
145 vName = C.Cyan,
146 val = C.Green,
147 em = C.LightCyan,
148
149 # Emphasized colors for the last frame of the traceback
150 normalEm = C.LightCyan,
151 filenameEm = C.LightGreen,
152 linenoEm = C.LightGreen,
153 nameEm = C.LightPurple,
154 valEm = C.LightBlue,
155
156 # Colors for printing the exception
157 excName = C.LightRed,
158 line = C.Yellow,
159 caret = C.White,
160 Normal = C.Normal
161 ))
162
163 # For light backgrounds, swap dark/light colors
164 ExceptionColors.add_scheme(ColorScheme(
165 'LightBG',
166 # The color to be used for the top line
167 topline = C.Red,
168
169 # The colors to be used in the traceback
170 filename = C.LightGreen,
171 lineno = C.LightGreen,
172 name = C.LightPurple,
173 vName = C.Cyan,
174 val = C.LightGreen,
175 em = C.Cyan,
176
177 # Emphasized colors for the last frame of the traceback
178 normalEm = C.Cyan,
179 filenameEm = C.Green,
180 linenoEm = C.Green,
181 nameEm = C.Purple,
182 valEm = C.Blue,
183
184 # Colors for printing the exception
185 excName = C.Red,
186 #line = C.Brown, # brown often is displayed as yellow
187 line = C.Red,
188 caret = C.Normal,
189 Normal = C.Normal
190 ))
191
192 class TBTools:
102 class TBTools:
193 """Basic tools used by all traceback printer classes."""
103 """Basic tools used by all traceback printer classes."""
194
104
195 def __init__(self,color_scheme = 'NoColor',call_pdb=0):
105 def __init__(self,color_scheme = 'NoColor',call_pdb=False):
196 # Whether to call the interactive pdb debugger after printing
106 # Whether to call the interactive pdb debugger after printing
197 # tracebacks or not
107 # tracebacks or not
198 self.call_pdb = call_pdb
108 self.call_pdb = call_pdb
199 if call_pdb:
200 self.pdb = Debugger.Pdb()
201 else:
202 self.pdb = None
203
109
204 # Create color table
110 # Create color table
205 self.ColorSchemeTable = ExceptionColors
111 self.color_scheme_table = ExceptionColors
206
112
207 self.set_colors(color_scheme)
113 self.set_colors(color_scheme)
208 self.old_scheme = color_scheme # save initial value for toggles
114 self.old_scheme = color_scheme # save initial value for toggles
209
115
116 if call_pdb:
117 self.pdb = Debugger.Pdb(self.color_scheme_table.active_scheme_name)
118 else:
119 self.pdb = None
120
210 def set_colors(self,*args,**kw):
121 def set_colors(self,*args,**kw):
211 """Shorthand access to the color table scheme selector method."""
122 """Shorthand access to the color table scheme selector method."""
212
123
213 self.ColorSchemeTable.set_active_scheme(*args,**kw)
124 self.color_scheme_table.set_active_scheme(*args,**kw)
214 # for convenience, set Colors to the active scheme
125 # for convenience, set Colors to the active scheme
215 self.Colors = self.ColorSchemeTable.active_colors
126 self.Colors = self.color_scheme_table.active_colors
216
127
217 def color_toggle(self):
128 def color_toggle(self):
218 """Toggle between the currently active color scheme and NoColor."""
129 """Toggle between the currently active color scheme and NoColor."""
219
130
220 if self.ColorSchemeTable.active_scheme_name == 'NoColor':
131 if self.color_scheme_table.active_scheme_name == 'NoColor':
221 self.ColorSchemeTable.set_active_scheme(self.old_scheme)
132 self.color_scheme_table.set_active_scheme(self.old_scheme)
222 self.Colors = self.ColorSchemeTable.active_colors
133 self.Colors = self.color_scheme_table.active_colors
223 else:
134 else:
224 self.old_scheme = self.ColorSchemeTable.active_scheme_name
135 self.old_scheme = self.color_scheme_table.active_scheme_name
225 self.ColorSchemeTable.set_active_scheme('NoColor')
136 self.color_scheme_table.set_active_scheme('NoColor')
226 self.Colors = self.ColorSchemeTable.active_colors
137 self.Colors = self.color_scheme_table.active_colors
227
138
228 #---------------------------------------------------------------------------
139 #---------------------------------------------------------------------------
229 class ListTB(TBTools):
140 class ListTB(TBTools):
230 """Print traceback information from a traceback list, with optional color.
141 """Print traceback information from a traceback list, with optional color.
231
142
232 Calling: requires 3 arguments:
143 Calling: requires 3 arguments:
233 (etype, evalue, elist)
144 (etype, evalue, elist)
234 as would be obtained by:
145 as would be obtained by:
235 etype, evalue, tb = sys.exc_info()
146 etype, evalue, tb = sys.exc_info()
236 if tb:
147 if tb:
237 elist = traceback.extract_tb(tb)
148 elist = traceback.extract_tb(tb)
238 else:
149 else:
239 elist = None
150 elist = None
240
151
241 It can thus be used by programs which need to process the traceback before
152 It can thus be used by programs which need to process the traceback before
242 printing (such as console replacements based on the code module from the
153 printing (such as console replacements based on the code module from the
243 standard library).
154 standard library).
244
155
245 Because they are meant to be called without a full traceback (only a
156 Because they are meant to be called without a full traceback (only a
246 list), instances of this class can't call the interactive pdb debugger."""
157 list), instances of this class can't call the interactive pdb debugger."""
247
158
248 def __init__(self,color_scheme = 'NoColor'):
159 def __init__(self,color_scheme = 'NoColor'):
249 TBTools.__init__(self,color_scheme = color_scheme,call_pdb=0)
160 TBTools.__init__(self,color_scheme = color_scheme,call_pdb=0)
250
161
251 def __call__(self, etype, value, elist):
162 def __call__(self, etype, value, elist):
252 print >> Term.cerr, self.text(etype,value,elist)
163 print >> Term.cerr, self.text(etype,value,elist)
253
164
254 def text(self,etype, value, elist,context=5):
165 def text(self,etype, value, elist,context=5):
255 """Return a color formatted string with the traceback info."""
166 """Return a color formatted string with the traceback info."""
256
167
257 Colors = self.Colors
168 Colors = self.Colors
258 out_string = ['%s%s%s\n' % (Colors.topline,'-'*60,Colors.Normal)]
169 out_string = ['%s%s%s\n' % (Colors.topline,'-'*60,Colors.Normal)]
259 if elist:
170 if elist:
260 out_string.append('Traceback %s(most recent call last)%s:' % \
171 out_string.append('Traceback %s(most recent call last)%s:' % \
261 (Colors.normalEm, Colors.Normal) + '\n')
172 (Colors.normalEm, Colors.Normal) + '\n')
262 out_string.extend(self._format_list(elist))
173 out_string.extend(self._format_list(elist))
263 lines = self._format_exception_only(etype, value)
174 lines = self._format_exception_only(etype, value)
264 for line in lines[:-1]:
175 for line in lines[:-1]:
265 out_string.append(" "+line)
176 out_string.append(" "+line)
266 out_string.append(lines[-1])
177 out_string.append(lines[-1])
267 return ''.join(out_string)
178 return ''.join(out_string)
268
179
269 def _format_list(self, extracted_list):
180 def _format_list(self, extracted_list):
270 """Format a list of traceback entry tuples for printing.
181 """Format a list of traceback entry tuples for printing.
271
182
272 Given a list of tuples as returned by extract_tb() or
183 Given a list of tuples as returned by extract_tb() or
273 extract_stack(), return a list of strings ready for printing.
184 extract_stack(), return a list of strings ready for printing.
274 Each string in the resulting list corresponds to the item with the
185 Each string in the resulting list corresponds to the item with the
275 same index in the argument list. Each string ends in a newline;
186 same index in the argument list. Each string ends in a newline;
276 the strings may contain internal newlines as well, for those items
187 the strings may contain internal newlines as well, for those items
277 whose source text line is not None.
188 whose source text line is not None.
278
189
279 Lifted almost verbatim from traceback.py
190 Lifted almost verbatim from traceback.py
280 """
191 """
281
192
282 Colors = self.Colors
193 Colors = self.Colors
283 list = []
194 list = []
284 for filename, lineno, name, line in extracted_list[:-1]:
195 for filename, lineno, name, line in extracted_list[:-1]:
285 item = ' File %s"%s"%s, line %s%d%s, in %s%s%s\n' % \
196 item = ' File %s"%s"%s, line %s%d%s, in %s%s%s\n' % \
286 (Colors.filename, filename, Colors.Normal,
197 (Colors.filename, filename, Colors.Normal,
287 Colors.lineno, lineno, Colors.Normal,
198 Colors.lineno, lineno, Colors.Normal,
288 Colors.name, name, Colors.Normal)
199 Colors.name, name, Colors.Normal)
289 if line:
200 if line:
290 item = item + ' %s\n' % line.strip()
201 item = item + ' %s\n' % line.strip()
291 list.append(item)
202 list.append(item)
292 # Emphasize the last entry
203 # Emphasize the last entry
293 filename, lineno, name, line = extracted_list[-1]
204 filename, lineno, name, line = extracted_list[-1]
294 item = '%s File %s"%s"%s, line %s%d%s, in %s%s%s%s\n' % \
205 item = '%s File %s"%s"%s, line %s%d%s, in %s%s%s%s\n' % \
295 (Colors.normalEm,
206 (Colors.normalEm,
296 Colors.filenameEm, filename, Colors.normalEm,
207 Colors.filenameEm, filename, Colors.normalEm,
297 Colors.linenoEm, lineno, Colors.normalEm,
208 Colors.linenoEm, lineno, Colors.normalEm,
298 Colors.nameEm, name, Colors.normalEm,
209 Colors.nameEm, name, Colors.normalEm,
299 Colors.Normal)
210 Colors.Normal)
300 if line:
211 if line:
301 item = item + '%s %s%s\n' % (Colors.line, line.strip(),
212 item = item + '%s %s%s\n' % (Colors.line, line.strip(),
302 Colors.Normal)
213 Colors.Normal)
303 list.append(item)
214 list.append(item)
304 return list
215 return list
305
216
306 def _format_exception_only(self, etype, value):
217 def _format_exception_only(self, etype, value):
307 """Format the exception part of a traceback.
218 """Format the exception part of a traceback.
308
219
309 The arguments are the exception type and value such as given by
220 The arguments are the exception type and value such as given by
310 sys.last_type and sys.last_value. The return value is a list of
221 sys.last_type and sys.last_value. The return value is a list of
311 strings, each ending in a newline. Normally, the list contains a
222 strings, each ending in a newline. Normally, the list contains a
312 single string; however, for SyntaxError exceptions, it contains
223 single string; however, for SyntaxError exceptions, it contains
313 several lines that (when printed) display detailed information
224 several lines that (when printed) display detailed information
314 about where the syntax error occurred. The message indicating
225 about where the syntax error occurred. The message indicating
315 which exception occurred is the always last string in the list.
226 which exception occurred is the always last string in the list.
316
227
317 Also lifted nearly verbatim from traceback.py
228 Also lifted nearly verbatim from traceback.py
318 """
229 """
319
230
320 Colors = self.Colors
231 Colors = self.Colors
321 list = []
232 list = []
322 if type(etype) == types.ClassType:
233 if type(etype) == types.ClassType:
323 stype = Colors.excName + etype.__name__ + Colors.Normal
234 stype = Colors.excName + etype.__name__ + Colors.Normal
324 else:
235 else:
325 stype = etype # String exceptions don't get special coloring
236 stype = etype # String exceptions don't get special coloring
326 if value is None:
237 if value is None:
327 list.append( str(stype) + '\n')
238 list.append( str(stype) + '\n')
328 else:
239 else:
329 if etype is SyntaxError:
240 if etype is SyntaxError:
330 try:
241 try:
331 msg, (filename, lineno, offset, line) = value
242 msg, (filename, lineno, offset, line) = value
332 except:
243 except:
333 pass
244 pass
334 else:
245 else:
335 #print 'filename is',filename # dbg
246 #print 'filename is',filename # dbg
336 if not filename: filename = "<string>"
247 if not filename: filename = "<string>"
337 list.append('%s File %s"%s"%s, line %s%d%s\n' % \
248 list.append('%s File %s"%s"%s, line %s%d%s\n' % \
338 (Colors.normalEm,
249 (Colors.normalEm,
339 Colors.filenameEm, filename, Colors.normalEm,
250 Colors.filenameEm, filename, Colors.normalEm,
340 Colors.linenoEm, lineno, Colors.Normal ))
251 Colors.linenoEm, lineno, Colors.Normal ))
341 if line is not None:
252 if line is not None:
342 i = 0
253 i = 0
343 while i < len(line) and line[i].isspace():
254 while i < len(line) and line[i].isspace():
344 i = i+1
255 i = i+1
345 list.append('%s %s%s\n' % (Colors.line,
256 list.append('%s %s%s\n' % (Colors.line,
346 line.strip(),
257 line.strip(),
347 Colors.Normal))
258 Colors.Normal))
348 if offset is not None:
259 if offset is not None:
349 s = ' '
260 s = ' '
350 for c in line[i:offset-1]:
261 for c in line[i:offset-1]:
351 if c.isspace():
262 if c.isspace():
352 s = s + c
263 s = s + c
353 else:
264 else:
354 s = s + ' '
265 s = s + ' '
355 list.append('%s%s^%s\n' % (Colors.caret, s,
266 list.append('%s%s^%s\n' % (Colors.caret, s,
356 Colors.Normal) )
267 Colors.Normal) )
357 value = msg
268 value = msg
358 s = self._some_str(value)
269 s = self._some_str(value)
359 if s:
270 if s:
360 list.append('%s%s:%s %s\n' % (str(stype), Colors.excName,
271 list.append('%s%s:%s %s\n' % (str(stype), Colors.excName,
361 Colors.Normal, s))
272 Colors.Normal, s))
362 else:
273 else:
363 list.append('%s\n' % str(stype))
274 list.append('%s\n' % str(stype))
364 return list
275 return list
365
276
366 def _some_str(self, value):
277 def _some_str(self, value):
367 # Lifted from traceback.py
278 # Lifted from traceback.py
368 try:
279 try:
369 return str(value)
280 return str(value)
370 except:
281 except:
371 return '<unprintable %s object>' % type(value).__name__
282 return '<unprintable %s object>' % type(value).__name__
372
283
373 #----------------------------------------------------------------------------
284 #----------------------------------------------------------------------------
374 class VerboseTB(TBTools):
285 class VerboseTB(TBTools):
375 """A port of Ka-Ping Yee's cgitb.py module that outputs color text instead
286 """A port of Ka-Ping Yee's cgitb.py module that outputs color text instead
376 of HTML. Requires inspect and pydoc. Crazy, man.
287 of HTML. Requires inspect and pydoc. Crazy, man.
377
288
378 Modified version which optionally strips the topmost entries from the
289 Modified version which optionally strips the topmost entries from the
379 traceback, to be used with alternate interpreters (because their own code
290 traceback, to be used with alternate interpreters (because their own code
380 would appear in the traceback)."""
291 would appear in the traceback)."""
381
292
382 def __init__(self,color_scheme = 'Linux',tb_offset=0,long_header=0,
293 def __init__(self,color_scheme = 'Linux',tb_offset=0,long_header=0,
383 call_pdb = 0, include_vars=1):
294 call_pdb = 0, include_vars=1):
384 """Specify traceback offset, headers and color scheme.
295 """Specify traceback offset, headers and color scheme.
385
296
386 Define how many frames to drop from the tracebacks. Calling it with
297 Define how many frames to drop from the tracebacks. Calling it with
387 tb_offset=1 allows use of this handler in interpreters which will have
298 tb_offset=1 allows use of this handler in interpreters which will have
388 their own code at the top of the traceback (VerboseTB will first
299 their own code at the top of the traceback (VerboseTB will first
389 remove that frame before printing the traceback info)."""
300 remove that frame before printing the traceback info)."""
390 TBTools.__init__(self,color_scheme=color_scheme,call_pdb=call_pdb)
301 TBTools.__init__(self,color_scheme=color_scheme,call_pdb=call_pdb)
391 self.tb_offset = tb_offset
302 self.tb_offset = tb_offset
392 self.long_header = long_header
303 self.long_header = long_header
393 self.include_vars = include_vars
304 self.include_vars = include_vars
394
305
395 def text(self, etype, evalue, etb, context=5):
306 def text(self, etype, evalue, etb, context=5):
396 """Return a nice text document describing the traceback."""
307 """Return a nice text document describing the traceback."""
397
308
398 # some locals
309 # some locals
399 Colors = self.Colors # just a shorthand + quicker name lookup
310 Colors = self.Colors # just a shorthand + quicker name lookup
400 ColorsNormal = Colors.Normal # used a lot
311 ColorsNormal = Colors.Normal # used a lot
401 indent_size = 8 # we need some space to put line numbers before
312 indent_size = 8 # we need some space to put line numbers before
402 indent = ' '*indent_size
313 indent = ' '*indent_size
403 numbers_width = indent_size - 1 # leave space between numbers & code
314 numbers_width = indent_size - 1 # leave space between numbers & code
404 text_repr = pydoc.text.repr
315 text_repr = pydoc.text.repr
405 exc = '%s%s%s' % (Colors.excName, str(etype), ColorsNormal)
316 exc = '%s%s%s' % (Colors.excName, str(etype), ColorsNormal)
406 em_normal = '%s\n%s%s' % (Colors.valEm, indent,ColorsNormal)
317 em_normal = '%s\n%s%s' % (Colors.valEm, indent,ColorsNormal)
407 undefined = '%sundefined%s' % (Colors.em, ColorsNormal)
318 undefined = '%sundefined%s' % (Colors.em, ColorsNormal)
408
319
409 # some internal-use functions
320 # some internal-use functions
410 def eqrepr(value, repr=text_repr): return '=%s' % repr(value)
321 def eqrepr(value, repr=text_repr): return '=%s' % repr(value)
411 def nullrepr(value, repr=text_repr): return ''
322 def nullrepr(value, repr=text_repr): return ''
412
323
413 # meat of the code begins
324 # meat of the code begins
414 if type(etype) is types.ClassType:
325 if type(etype) is types.ClassType:
415 etype = etype.__name__
326 etype = etype.__name__
416
327
417 if self.long_header:
328 if self.long_header:
418 # Header with the exception type, python version, and date
329 # Header with the exception type, python version, and date
419 pyver = 'Python ' + string.split(sys.version)[0] + ': ' + sys.executable
330 pyver = 'Python ' + string.split(sys.version)[0] + ': ' + sys.executable
420 date = time.ctime(time.time())
331 date = time.ctime(time.time())
421
332
422 head = '%s%s%s\n%s%s%s\n%s' % (Colors.topline, '-'*75, ColorsNormal,
333 head = '%s%s%s\n%s%s%s\n%s' % (Colors.topline, '-'*75, ColorsNormal,
423 exc, ' '*(75-len(str(etype))-len(pyver)),
334 exc, ' '*(75-len(str(etype))-len(pyver)),
424 pyver, string.rjust(date, 75) )
335 pyver, string.rjust(date, 75) )
425 head += "\nA problem occured executing Python code. Here is the sequence of function"\
336 head += "\nA problem occured executing Python code. Here is the sequence of function"\
426 "\ncalls leading up to the error, with the most recent (innermost) call last."
337 "\ncalls leading up to the error, with the most recent (innermost) call last."
427 else:
338 else:
428 # Simplified header
339 # Simplified header
429 head = '%s%s%s\n%s%s' % (Colors.topline, '-'*75, ColorsNormal,exc,
340 head = '%s%s%s\n%s%s' % (Colors.topline, '-'*75, ColorsNormal,exc,
430 string.rjust('Traceback (most recent call last)',
341 string.rjust('Traceback (most recent call last)',
431 75 - len(str(etype)) ) )
342 75 - len(str(etype)) ) )
432 frames = []
343 frames = []
433 # Flush cache before calling inspect. This helps alleviate some of the
344 # Flush cache before calling inspect. This helps alleviate some of the
434 # problems with python 2.3's inspect.py.
345 # problems with python 2.3's inspect.py.
435 linecache.checkcache()
346 linecache.checkcache()
436 # Drop topmost frames if requested
347 # Drop topmost frames if requested
437 try:
348 try:
438 records = inspect.getinnerframes(etb, context)[self.tb_offset:]
349 records = inspect.getinnerframes(etb, context)[self.tb_offset:]
439 except:
350 except:
440
351
441 # FIXME: I've been getting many crash reports from python 2.3
352 # FIXME: I've been getting many crash reports from python 2.3
442 # users, traceable to inspect.py. If I can find a small test-case
353 # users, traceable to inspect.py. If I can find a small test-case
443 # to reproduce this, I should either write a better workaround or
354 # to reproduce this, I should either write a better workaround or
444 # file a bug report against inspect (if that's the real problem).
355 # file a bug report against inspect (if that's the real problem).
445 # So far, I haven't been able to find an isolated example to
356 # So far, I haven't been able to find an isolated example to
446 # reproduce the problem.
357 # reproduce the problem.
447 inspect_error()
358 inspect_error()
448 traceback.print_exc(file=Term.cerr)
359 traceback.print_exc(file=Term.cerr)
449 info('\nUnfortunately, your original traceback can not be constructed.\n')
360 info('\nUnfortunately, your original traceback can not be constructed.\n')
450 return ''
361 return ''
451
362
452 # build some color string templates outside these nested loops
363 # build some color string templates outside these nested loops
453 tpl_link = '%s%%s%s' % (Colors.filenameEm,ColorsNormal)
364 tpl_link = '%s%%s%s' % (Colors.filenameEm,ColorsNormal)
454 tpl_call = 'in %s%%s%s%%s%s' % (Colors.vName, Colors.valEm,
365 tpl_call = 'in %s%%s%s%%s%s' % (Colors.vName, Colors.valEm,
455 ColorsNormal)
366 ColorsNormal)
456 tpl_call_fail = 'in %s%%s%s(***failed resolving arguments***)%s' % \
367 tpl_call_fail = 'in %s%%s%s(***failed resolving arguments***)%s' % \
457 (Colors.vName, Colors.valEm, ColorsNormal)
368 (Colors.vName, Colors.valEm, ColorsNormal)
458 tpl_local_var = '%s%%s%s' % (Colors.vName, ColorsNormal)
369 tpl_local_var = '%s%%s%s' % (Colors.vName, ColorsNormal)
459 tpl_global_var = '%sglobal%s %s%%s%s' % (Colors.em, ColorsNormal,
370 tpl_global_var = '%sglobal%s %s%%s%s' % (Colors.em, ColorsNormal,
460 Colors.vName, ColorsNormal)
371 Colors.vName, ColorsNormal)
461 tpl_name_val = '%%s %s= %%s%s' % (Colors.valEm, ColorsNormal)
372 tpl_name_val = '%%s %s= %%s%s' % (Colors.valEm, ColorsNormal)
462 tpl_line = '%s%%s%s %%s' % (Colors.lineno, ColorsNormal)
373 tpl_line = '%s%%s%s %%s' % (Colors.lineno, ColorsNormal)
463 tpl_line_em = '%s%%s%s %%s%s' % (Colors.linenoEm,Colors.line,
374 tpl_line_em = '%s%%s%s %%s%s' % (Colors.linenoEm,Colors.line,
464 ColorsNormal)
375 ColorsNormal)
465
376
466 # now, loop over all records printing context and info
377 # now, loop over all records printing context and info
467 abspath = os.path.abspath
378 abspath = os.path.abspath
468 for frame, file, lnum, func, lines, index in records:
379 for frame, file, lnum, func, lines, index in records:
469 #print '*** record:',file,lnum,func,lines,index # dbg
380 #print '*** record:',file,lnum,func,lines,index # dbg
470 try:
381 try:
471 file = file and abspath(file) or '?'
382 file = file and abspath(file) or '?'
472 except OSError:
383 except OSError:
473 # if file is '<console>' or something not in the filesystem,
384 # if file is '<console>' or something not in the filesystem,
474 # the abspath call will throw an OSError. Just ignore it and
385 # the abspath call will throw an OSError. Just ignore it and
475 # keep the original file string.
386 # keep the original file string.
476 pass
387 pass
477 link = tpl_link % file
388 link = tpl_link % file
478 try:
389 try:
479 args, varargs, varkw, locals = inspect.getargvalues(frame)
390 args, varargs, varkw, locals = inspect.getargvalues(frame)
480 except:
391 except:
481 # This can happen due to a bug in python2.3. We should be
392 # This can happen due to a bug in python2.3. We should be
482 # able to remove this try/except when 2.4 becomes a
393 # able to remove this try/except when 2.4 becomes a
483 # requirement. Bug details at http://python.org/sf/1005466
394 # requirement. Bug details at http://python.org/sf/1005466
484 inspect_error()
395 inspect_error()
485 traceback.print_exc(file=Term.cerr)
396 traceback.print_exc(file=Term.cerr)
486 info("\nIPython's exception reporting continues...\n")
397 info("\nIPython's exception reporting continues...\n")
487
398
488 if func == '?':
399 if func == '?':
489 call = ''
400 call = ''
490 else:
401 else:
491 # Decide whether to include variable details or not
402 # Decide whether to include variable details or not
492 var_repr = self.include_vars and eqrepr or nullrepr
403 var_repr = self.include_vars and eqrepr or nullrepr
493 try:
404 try:
494 call = tpl_call % (func,inspect.formatargvalues(args,
405 call = tpl_call % (func,inspect.formatargvalues(args,
495 varargs, varkw,
406 varargs, varkw,
496 locals,formatvalue=var_repr))
407 locals,formatvalue=var_repr))
497 except KeyError:
408 except KeyError:
498 # Very odd crash from inspect.formatargvalues(). The
409 # Very odd crash from inspect.formatargvalues(). The
499 # scenario under which it appeared was a call to
410 # scenario under which it appeared was a call to
500 # view(array,scale) in NumTut.view.view(), where scale had
411 # view(array,scale) in NumTut.view.view(), where scale had
501 # been defined as a scalar (it should be a tuple). Somehow
412 # been defined as a scalar (it should be a tuple). Somehow
502 # inspect messes up resolving the argument list of view()
413 # inspect messes up resolving the argument list of view()
503 # and barfs out. At some point I should dig into this one
414 # and barfs out. At some point I should dig into this one
504 # and file a bug report about it.
415 # and file a bug report about it.
505 inspect_error()
416 inspect_error()
506 traceback.print_exc(file=Term.cerr)
417 traceback.print_exc(file=Term.cerr)
507 info("\nIPython's exception reporting continues...\n")
418 info("\nIPython's exception reporting continues...\n")
508 call = tpl_call_fail % func
419 call = tpl_call_fail % func
509
420
510 # Initialize a list of names on the current line, which the
421 # Initialize a list of names on the current line, which the
511 # tokenizer below will populate.
422 # tokenizer below will populate.
512 names = []
423 names = []
513
424
514 def tokeneater(token_type, token, start, end, line):
425 def tokeneater(token_type, token, start, end, line):
515 """Stateful tokeneater which builds dotted names.
426 """Stateful tokeneater which builds dotted names.
516
427
517 The list of names it appends to (from the enclosing scope) can
428 The list of names it appends to (from the enclosing scope) can
518 contain repeated composite names. This is unavoidable, since
429 contain repeated composite names. This is unavoidable, since
519 there is no way to disambguate partial dotted structures until
430 there is no way to disambguate partial dotted structures until
520 the full list is known. The caller is responsible for pruning
431 the full list is known. The caller is responsible for pruning
521 the final list of duplicates before using it."""
432 the final list of duplicates before using it."""
522
433
523 # build composite names
434 # build composite names
524 if token == '.':
435 if token == '.':
525 try:
436 try:
526 names[-1] += '.'
437 names[-1] += '.'
527 # store state so the next token is added for x.y.z names
438 # store state so the next token is added for x.y.z names
528 tokeneater.name_cont = True
439 tokeneater.name_cont = True
529 return
440 return
530 except IndexError:
441 except IndexError:
531 pass
442 pass
532 if token_type == tokenize.NAME and token not in keyword.kwlist:
443 if token_type == tokenize.NAME and token not in keyword.kwlist:
533 if tokeneater.name_cont:
444 if tokeneater.name_cont:
534 # Dotted names
445 # Dotted names
535 names[-1] += token
446 names[-1] += token
536 tokeneater.name_cont = False
447 tokeneater.name_cont = False
537 else:
448 else:
538 # Regular new names. We append everything, the caller
449 # Regular new names. We append everything, the caller
539 # will be responsible for pruning the list later. It's
450 # will be responsible for pruning the list later. It's
540 # very tricky to try to prune as we go, b/c composite
451 # very tricky to try to prune as we go, b/c composite
541 # names can fool us. The pruning at the end is easy
452 # names can fool us. The pruning at the end is easy
542 # to do (or the caller can print a list with repeated
453 # to do (or the caller can print a list with repeated
543 # names if so desired.
454 # names if so desired.
544 names.append(token)
455 names.append(token)
545 elif token_type == tokenize.NEWLINE:
456 elif token_type == tokenize.NEWLINE:
546 raise IndexError
457 raise IndexError
547 # we need to store a bit of state in the tokenizer to build
458 # we need to store a bit of state in the tokenizer to build
548 # dotted names
459 # dotted names
549 tokeneater.name_cont = False
460 tokeneater.name_cont = False
550
461
551 def linereader(file=file, lnum=[lnum], getline=linecache.getline):
462 def linereader(file=file, lnum=[lnum], getline=linecache.getline):
552 line = getline(file, lnum[0])
463 line = getline(file, lnum[0])
553 lnum[0] += 1
464 lnum[0] += 1
554 return line
465 return line
555
466
556 # Build the list of names on this line of code where the exception
467 # Build the list of names on this line of code where the exception
557 # occurred.
468 # occurred.
558 try:
469 try:
559 # This builds the names list in-place by capturing it from the
470 # This builds the names list in-place by capturing it from the
560 # enclosing scope.
471 # enclosing scope.
561 tokenize.tokenize(linereader, tokeneater)
472 tokenize.tokenize(linereader, tokeneater)
562 except IndexError:
473 except IndexError:
563 # signals exit of tokenizer
474 # signals exit of tokenizer
564 pass
475 pass
565 except tokenize.TokenError,msg:
476 except tokenize.TokenError,msg:
566 _m = ("An unexpected error occurred while tokenizing input\n"
477 _m = ("An unexpected error occurred while tokenizing input\n"
567 "The following traceback may be corrupted or invalid\n"
478 "The following traceback may be corrupted or invalid\n"
568 "The error message is: %s\n" % msg)
479 "The error message is: %s\n" % msg)
569 error(_m)
480 error(_m)
570
481
571 # prune names list of duplicates, but keep the right order
482 # prune names list of duplicates, but keep the right order
572 unique_names = uniq_stable(names)
483 unique_names = uniq_stable(names)
573
484
574 # Start loop over vars
485 # Start loop over vars
575 lvals = []
486 lvals = []
576 if self.include_vars:
487 if self.include_vars:
577 for name_full in unique_names:
488 for name_full in unique_names:
578 name_base = name_full.split('.',1)[0]
489 name_base = name_full.split('.',1)[0]
579 if name_base in frame.f_code.co_varnames:
490 if name_base in frame.f_code.co_varnames:
580 if locals.has_key(name_base):
491 if locals.has_key(name_base):
581 try:
492 try:
582 value = repr(eval(name_full,locals))
493 value = repr(eval(name_full,locals))
583 except:
494 except:
584 value = undefined
495 value = undefined
585 else:
496 else:
586 value = undefined
497 value = undefined
587 name = tpl_local_var % name_full
498 name = tpl_local_var % name_full
588 else:
499 else:
589 if frame.f_globals.has_key(name_base):
500 if frame.f_globals.has_key(name_base):
590 try:
501 try:
591 value = repr(eval(name_full,frame.f_globals))
502 value = repr(eval(name_full,frame.f_globals))
592 except:
503 except:
593 value = undefined
504 value = undefined
594 else:
505 else:
595 value = undefined
506 value = undefined
596 name = tpl_global_var % name_full
507 name = tpl_global_var % name_full
597 lvals.append(tpl_name_val % (name,value))
508 lvals.append(tpl_name_val % (name,value))
598 if lvals:
509 if lvals:
599 lvals = '%s%s' % (indent,em_normal.join(lvals))
510 lvals = '%s%s' % (indent,em_normal.join(lvals))
600 else:
511 else:
601 lvals = ''
512 lvals = ''
602
513
603 level = '%s %s\n' % (link,call)
514 level = '%s %s\n' % (link,call)
604 excerpt = []
515 excerpt = []
605 if index is not None:
516 if index is not None:
606 i = lnum - index
517 i = lnum - index
607 for line in lines:
518 for line in lines:
608 if i == lnum:
519 if i == lnum:
609 # This is the line with the error
520 # This is the line with the error
610 pad = numbers_width - len(str(i))
521 pad = numbers_width - len(str(i))
611 if pad >= 3:
522 if pad >= 3:
612 marker = '-'*(pad-3) + '-> '
523 marker = '-'*(pad-3) + '-> '
613 elif pad == 2:
524 elif pad == 2:
614 marker = '> '
525 marker = '> '
615 elif pad == 1:
526 elif pad == 1:
616 marker = '>'
527 marker = '>'
617 else:
528 else:
618 marker = ''
529 marker = ''
619 num = '%s%s' % (marker,i)
530 num = '%s%s' % (marker,i)
620 line = tpl_line_em % (num,line)
531 line = tpl_line_em % (num,line)
621 else:
532 else:
622 num = '%*s' % (numbers_width,i)
533 num = '%*s' % (numbers_width,i)
623 line = tpl_line % (num,line)
534 line = tpl_line % (num,line)
624
535
625 excerpt.append(line)
536 excerpt.append(line)
626 if self.include_vars and i == lnum:
537 if self.include_vars and i == lnum:
627 excerpt.append('%s\n' % lvals)
538 excerpt.append('%s\n' % lvals)
628 i += 1
539 i += 1
629 frames.append('%s%s' % (level,''.join(excerpt)) )
540 frames.append('%s%s' % (level,''.join(excerpt)) )
630
541
631 # Get (safely) a string form of the exception info
542 # Get (safely) a string form of the exception info
632 try:
543 try:
633 etype_str,evalue_str = map(str,(etype,evalue))
544 etype_str,evalue_str = map(str,(etype,evalue))
634 except:
545 except:
635 # User exception is improperly defined.
546 # User exception is improperly defined.
636 etype,evalue = str,sys.exc_info()[:2]
547 etype,evalue = str,sys.exc_info()[:2]
637 etype_str,evalue_str = map(str,(etype,evalue))
548 etype_str,evalue_str = map(str,(etype,evalue))
638 # ... and format it
549 # ... and format it
639 exception = ['%s%s%s: %s' % (Colors.excName, etype_str,
550 exception = ['%s%s%s: %s' % (Colors.excName, etype_str,
640 ColorsNormal, evalue_str)]
551 ColorsNormal, evalue_str)]
641 if type(evalue) is types.InstanceType:
552 if type(evalue) is types.InstanceType:
642 names = [w for w in dir(evalue) if isinstance(w, basestring)]
553 names = [w for w in dir(evalue) if isinstance(w, basestring)]
643 for name in names:
554 for name in names:
644 value = text_repr(getattr(evalue, name))
555 value = text_repr(getattr(evalue, name))
645 exception.append('\n%s%s = %s' % (indent, name, value))
556 exception.append('\n%s%s = %s' % (indent, name, value))
646 # return all our info assembled as a single string
557 # return all our info assembled as a single string
647 return '%s\n\n%s\n%s' % (head,'\n'.join(frames),''.join(exception[0]) )
558 return '%s\n\n%s\n%s' % (head,'\n'.join(frames),''.join(exception[0]) )
648
559
649 def debugger(self):
560 def debugger(self):
650 """Call up the pdb debugger if desired, always clean up the tb reference.
561 """Call up the pdb debugger if desired, always clean up the tb reference.
651
562
652 If the call_pdb flag is set, the pdb interactive debugger is
563 If the call_pdb flag is set, the pdb interactive debugger is
653 invoked. In all cases, the self.tb reference to the current traceback
564 invoked. In all cases, the self.tb reference to the current traceback
654 is deleted to prevent lingering references which hamper memory
565 is deleted to prevent lingering references which hamper memory
655 management.
566 management.
656
567
657 Note that each call to pdb() does an 'import readline', so if your app
568 Note that each call to pdb() does an 'import readline', so if your app
658 requires a special setup for the readline completers, you'll have to
569 requires a special setup for the readline completers, you'll have to
659 fix that by hand after invoking the exception handler."""
570 fix that by hand after invoking the exception handler."""
660
571
661 if self.call_pdb:
572 if self.call_pdb:
662 if self.pdb is None:
573 if self.pdb is None:
663 self.pdb = Debugger.Pdb()
574 self.pdb = Debugger.Pdb(
664 # the system displayhook may have changed, restore the original for pdb
575 self.color_scheme_table.active_scheme_name)
576 # the system displayhook may have changed, restore the original
577 # for pdb
665 dhook = sys.displayhook
578 dhook = sys.displayhook
666 sys.displayhook = sys.__displayhook__
579 sys.displayhook = sys.__displayhook__
667 self.pdb.reset()
580 self.pdb.reset()
581 # Find the right frame so we don't pop up inside ipython itself
582 etb = self.tb
668 while self.tb.tb_next is not None:
583 while self.tb.tb_next is not None:
669 self.tb = self.tb.tb_next
584 self.tb = self.tb.tb_next
670 try:
585 try:
586 if etb and etb.tb_next:
587 etb = etb.tb_next
588 self.pdb.botframe = etb.tb_frame
671 self.pdb.interaction(self.tb.tb_frame, self.tb)
589 self.pdb.interaction(self.tb.tb_frame, self.tb)
672 except:
590 except:
673 print '*** ERROR ***'
591 print '*** ERROR ***'
674 print 'This version of pdb has a bug and crashed.'
592 print 'This version of pdb has a bug and crashed.'
675 print 'Returning to IPython...'
593 print 'Returning to IPython...'
676 sys.displayhook = dhook
594 sys.displayhook = dhook
677 del self.tb
595 del self.tb
678
596
679 def handler(self, info=None):
597 def handler(self, info=None):
680 (etype, evalue, etb) = info or sys.exc_info()
598 (etype, evalue, etb) = info or sys.exc_info()
681 self.tb = etb
599 self.tb = etb
682 print >> Term.cerr, self.text(etype, evalue, etb)
600 print >> Term.cerr, self.text(etype, evalue, etb)
683
601
684 # Changed so an instance can just be called as VerboseTB_inst() and print
602 # Changed so an instance can just be called as VerboseTB_inst() and print
685 # out the right info on its own.
603 # out the right info on its own.
686 def __call__(self, etype=None, evalue=None, etb=None):
604 def __call__(self, etype=None, evalue=None, etb=None):
687 """This hook can replace sys.excepthook (for Python 2.1 or higher)."""
605 """This hook can replace sys.excepthook (for Python 2.1 or higher)."""
688 if etb is not None:
606 if etb is not None:
689 self.handler((etype, evalue, etb))
607 self.handler((etype, evalue, etb))
690 else:
608 else:
691 self.handler()
609 self.handler()
692 self.debugger()
610 self.debugger()
693
611
694 #----------------------------------------------------------------------------
612 #----------------------------------------------------------------------------
695 class FormattedTB(VerboseTB,ListTB):
613 class FormattedTB(VerboseTB,ListTB):
696 """Subclass ListTB but allow calling with a traceback.
614 """Subclass ListTB but allow calling with a traceback.
697
615
698 It can thus be used as a sys.excepthook for Python > 2.1.
616 It can thus be used as a sys.excepthook for Python > 2.1.
699
617
700 Also adds 'Context' and 'Verbose' modes, not available in ListTB.
618 Also adds 'Context' and 'Verbose' modes, not available in ListTB.
701
619
702 Allows a tb_offset to be specified. This is useful for situations where
620 Allows a tb_offset to be specified. This is useful for situations where
703 one needs to remove a number of topmost frames from the traceback (such as
621 one needs to remove a number of topmost frames from the traceback (such as
704 occurs with python programs that themselves execute other python code,
622 occurs with python programs that themselves execute other python code,
705 like Python shells). """
623 like Python shells). """
706
624
707 def __init__(self, mode = 'Plain', color_scheme='Linux',
625 def __init__(self, mode = 'Plain', color_scheme='Linux',
708 tb_offset = 0,long_header=0,call_pdb=0,include_vars=0):
626 tb_offset = 0,long_header=0,call_pdb=0,include_vars=0):
709
627
710 # NEVER change the order of this list. Put new modes at the end:
628 # NEVER change the order of this list. Put new modes at the end:
711 self.valid_modes = ['Plain','Context','Verbose']
629 self.valid_modes = ['Plain','Context','Verbose']
712 self.verbose_modes = self.valid_modes[1:3]
630 self.verbose_modes = self.valid_modes[1:3]
713
631
714 VerboseTB.__init__(self,color_scheme,tb_offset,long_header,
632 VerboseTB.__init__(self,color_scheme,tb_offset,long_header,
715 call_pdb=call_pdb,include_vars=include_vars)
633 call_pdb=call_pdb,include_vars=include_vars)
716 self.set_mode(mode)
634 self.set_mode(mode)
717
635
718 def _extract_tb(self,tb):
636 def _extract_tb(self,tb):
719 if tb:
637 if tb:
720 return traceback.extract_tb(tb)
638 return traceback.extract_tb(tb)
721 else:
639 else:
722 return None
640 return None
723
641
724 def text(self, etype, value, tb,context=5,mode=None):
642 def text(self, etype, value, tb,context=5,mode=None):
725 """Return formatted traceback.
643 """Return formatted traceback.
726
644
727 If the optional mode parameter is given, it overrides the current
645 If the optional mode parameter is given, it overrides the current
728 mode."""
646 mode."""
729
647
730 if mode is None:
648 if mode is None:
731 mode = self.mode
649 mode = self.mode
732 if mode in self.verbose_modes:
650 if mode in self.verbose_modes:
733 # verbose modes need a full traceback
651 # verbose modes need a full traceback
734 return VerboseTB.text(self,etype, value, tb,context=5)
652 return VerboseTB.text(self,etype, value, tb,context=5)
735 else:
653 else:
736 # We must check the source cache because otherwise we can print
654 # We must check the source cache because otherwise we can print
737 # out-of-date source code.
655 # out-of-date source code.
738 linecache.checkcache()
656 linecache.checkcache()
739 # Now we can extract and format the exception
657 # Now we can extract and format the exception
740 elist = self._extract_tb(tb)
658 elist = self._extract_tb(tb)
741 if len(elist) > self.tb_offset:
659 if len(elist) > self.tb_offset:
742 del elist[:self.tb_offset]
660 del elist[:self.tb_offset]
743 return ListTB.text(self,etype,value,elist)
661 return ListTB.text(self,etype,value,elist)
744
662
745 def set_mode(self,mode=None):
663 def set_mode(self,mode=None):
746 """Switch to the desired mode.
664 """Switch to the desired mode.
747
665
748 If mode is not specified, cycles through the available modes."""
666 If mode is not specified, cycles through the available modes."""
749
667
750 if not mode:
668 if not mode:
751 new_idx = ( self.valid_modes.index(self.mode) + 1 ) % \
669 new_idx = ( self.valid_modes.index(self.mode) + 1 ) % \
752 len(self.valid_modes)
670 len(self.valid_modes)
753 self.mode = self.valid_modes[new_idx]
671 self.mode = self.valid_modes[new_idx]
754 elif mode not in self.valid_modes:
672 elif mode not in self.valid_modes:
755 raise ValueError, 'Unrecognized mode in FormattedTB: <'+mode+'>\n'\
673 raise ValueError, 'Unrecognized mode in FormattedTB: <'+mode+'>\n'\
756 'Valid modes: '+str(self.valid_modes)
674 'Valid modes: '+str(self.valid_modes)
757 else:
675 else:
758 self.mode = mode
676 self.mode = mode
759 # include variable details only in 'Verbose' mode
677 # include variable details only in 'Verbose' mode
760 self.include_vars = (self.mode == self.valid_modes[2])
678 self.include_vars = (self.mode == self.valid_modes[2])
761
679
762 # some convenient shorcuts
680 # some convenient shorcuts
763 def plain(self):
681 def plain(self):
764 self.set_mode(self.valid_modes[0])
682 self.set_mode(self.valid_modes[0])
765
683
766 def context(self):
684 def context(self):
767 self.set_mode(self.valid_modes[1])
685 self.set_mode(self.valid_modes[1])
768
686
769 def verbose(self):
687 def verbose(self):
770 self.set_mode(self.valid_modes[2])
688 self.set_mode(self.valid_modes[2])
771
689
772 #----------------------------------------------------------------------------
690 #----------------------------------------------------------------------------
773 class AutoFormattedTB(FormattedTB):
691 class AutoFormattedTB(FormattedTB):
774 """A traceback printer which can be called on the fly.
692 """A traceback printer which can be called on the fly.
775
693
776 It will find out about exceptions by itself.
694 It will find out about exceptions by itself.
777
695
778 A brief example:
696 A brief example:
779
697
780 AutoTB = AutoFormattedTB(mode = 'Verbose',color_scheme='Linux')
698 AutoTB = AutoFormattedTB(mode = 'Verbose',color_scheme='Linux')
781 try:
699 try:
782 ...
700 ...
783 except:
701 except:
784 AutoTB() # or AutoTB(out=logfile) where logfile is an open file object
702 AutoTB() # or AutoTB(out=logfile) where logfile is an open file object
785 """
703 """
786 def __call__(self,etype=None,evalue=None,etb=None,
704 def __call__(self,etype=None,evalue=None,etb=None,
787 out=None,tb_offset=None):
705 out=None,tb_offset=None):
788 """Print out a formatted exception traceback.
706 """Print out a formatted exception traceback.
789
707
790 Optional arguments:
708 Optional arguments:
791 - out: an open file-like object to direct output to.
709 - out: an open file-like object to direct output to.
792
710
793 - tb_offset: the number of frames to skip over in the stack, on a
711 - tb_offset: the number of frames to skip over in the stack, on a
794 per-call basis (this overrides temporarily the instance's tb_offset
712 per-call basis (this overrides temporarily the instance's tb_offset
795 given at initialization time. """
713 given at initialization time. """
796
714
797 if out is None:
715 if out is None:
798 out = Term.cerr
716 out = Term.cerr
799 if tb_offset is not None:
717 if tb_offset is not None:
800 tb_offset, self.tb_offset = self.tb_offset, tb_offset
718 tb_offset, self.tb_offset = self.tb_offset, tb_offset
801 print >> out, self.text(etype, evalue, etb)
719 print >> out, self.text(etype, evalue, etb)
802 self.tb_offset = tb_offset
720 self.tb_offset = tb_offset
803 else:
721 else:
804 print >> out, self.text()
722 print >> out, self.text(etype, evalue, etb)
805 self.debugger()
723 self.debugger()
806
724
807 def text(self,etype=None,value=None,tb=None,context=5,mode=None):
725 def text(self,etype=None,value=None,tb=None,context=5,mode=None):
808 if etype is None:
726 if etype is None:
809 etype,value,tb = sys.exc_info()
727 etype,value,tb = sys.exc_info()
810 self.tb = tb
728 self.tb = tb
811 return FormattedTB.text(self,etype,value,tb,context=5,mode=mode)
729 return FormattedTB.text(self,etype,value,tb,context=5,mode=mode)
812
730
813 #---------------------------------------------------------------------------
731 #---------------------------------------------------------------------------
814 # A simple class to preserve Nathan's original functionality.
732 # A simple class to preserve Nathan's original functionality.
815 class ColorTB(FormattedTB):
733 class ColorTB(FormattedTB):
816 """Shorthand to initialize a FormattedTB in Linux colors mode."""
734 """Shorthand to initialize a FormattedTB in Linux colors mode."""
817 def __init__(self,color_scheme='Linux',call_pdb=0):
735 def __init__(self,color_scheme='Linux',call_pdb=0):
818 FormattedTB.__init__(self,color_scheme=color_scheme,
736 FormattedTB.__init__(self,color_scheme=color_scheme,
819 call_pdb=call_pdb)
737 call_pdb=call_pdb)
820
738
821 #----------------------------------------------------------------------------
739 #----------------------------------------------------------------------------
822 # module testing (minimal)
740 # module testing (minimal)
823 if __name__ == "__main__":
741 if __name__ == "__main__":
824 def spam(c, (d, e)):
742 def spam(c, (d, e)):
825 x = c + d
743 x = c + d
826 y = c * d
744 y = c * d
827 foo(x, y)
745 foo(x, y)
828
746
829 def foo(a, b, bar=1):
747 def foo(a, b, bar=1):
830 eggs(a, b + bar)
748 eggs(a, b + bar)
831
749
832 def eggs(f, g, z=globals()):
750 def eggs(f, g, z=globals()):
833 h = f + g
751 h = f + g
834 i = f - g
752 i = f - g
835 return h / i
753 return h / i
836
754
837 print ''
755 print ''
838 print '*** Before ***'
756 print '*** Before ***'
839 try:
757 try:
840 print spam(1, (2, 3))
758 print spam(1, (2, 3))
841 except:
759 except:
842 traceback.print_exc()
760 traceback.print_exc()
843 print ''
761 print ''
844
762
845 handler = ColorTB()
763 handler = ColorTB()
846 print '*** ColorTB ***'
764 print '*** ColorTB ***'
847 try:
765 try:
848 print spam(1, (2, 3))
766 print spam(1, (2, 3))
849 except:
767 except:
850 apply(handler, sys.exc_info() )
768 apply(handler, sys.exc_info() )
851 print ''
769 print ''
852
770
853 handler = VerboseTB()
771 handler = VerboseTB()
854 print '*** VerboseTB ***'
772 print '*** VerboseTB ***'
855 try:
773 try:
856 print spam(1, (2, 3))
774 print spam(1, (2, 3))
857 except:
775 except:
858 apply(handler, sys.exc_info() )
776 apply(handler, sys.exc_info() )
859 print ''
777 print ''
860
778
1 NO CONTENT: modified file
NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
@@ -1,149 +1,150 b''
1 #!/usr/bin/env python
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
2 # -*- coding: utf-8 -*-
3 """Setup script for IPython.
3 """Setup script for IPython.
4
4
5 Under Posix environments it works like a typical setup.py script.
5 Under Posix environments it works like a typical setup.py script.
6 Under Windows, the command sdist is not supported, since IPython
6 Under Windows, the command sdist is not supported, since IPython
7 requires utilities, which are not available under Windows."""
7 requires utilities, which are not available under Windows."""
8
8
9 #*****************************************************************************
9 #*****************************************************************************
10 # Copyright (C) 2001-2005 Fernando Perez <fperez@colorado.edu>
10 # Copyright (C) 2001-2005 Fernando Perez <fperez@colorado.edu>
11 #
11 #
12 # Distributed under the terms of the BSD License. The full license is in
12 # Distributed under the terms of the BSD License. The full license is in
13 # the file COPYING, distributed as part of this software.
13 # the file COPYING, distributed as part of this software.
14 #*****************************************************************************
14 #*****************************************************************************
15
15
16 import sys, os
16 import sys, os
17 from glob import glob
17 from glob import glob
18 from setupext import install_data_ext
18 from setupext import install_data_ext
19 isfile = os.path.isfile
19 isfile = os.path.isfile
20
20
21 # BEFORE importing distutils, remove MANIFEST. distutils doesn't properly
21 # BEFORE importing distutils, remove MANIFEST. distutils doesn't properly
22 # update it when the contents of directories change.
22 # update it when the contents of directories change.
23 if os.path.exists('MANIFEST'): os.remove('MANIFEST')
23 if os.path.exists('MANIFEST'): os.remove('MANIFEST')
24
24
25 if os.name == 'posix':
25 if os.name == 'posix':
26 os_name = 'posix'
26 os_name = 'posix'
27 elif os.name in ['nt','dos']:
27 elif os.name in ['nt','dos']:
28 os_name = 'windows'
28 os_name = 'windows'
29 else:
29 else:
30 print 'Unsupported operating system:',os.name
30 print 'Unsupported operating system:',os.name
31 sys.exit(1)
31 sys.exit(1)
32
32
33 # Under Windows, 'sdist' is not supported, since it requires lyxport (and
33 # Under Windows, 'sdist' is not supported, since it requires lyxport (and
34 # hence lyx,perl,latex,pdflatex,latex2html,sh,...)
34 # hence lyx,perl,latex,pdflatex,latex2html,sh,...)
35 if os_name == 'windows' and sys.argv[1] == 'sdist':
35 if os_name == 'windows' and sys.argv[1] == 'sdist':
36 print 'The sdist command is not available under Windows. Exiting.'
36 print 'The sdist command is not available under Windows. Exiting.'
37 sys.exit(1)
37 sys.exit(1)
38
38
39 from distutils.core import setup
39 from distutils.core import setup
40
40
41 # update the manuals when building a source dist
41 # update the manuals when building a source dist
42 if len(sys.argv) >= 2 and sys.argv[1] in ('sdist','bdist_rpm'):
42 if len(sys.argv) >= 2 and sys.argv[1] in ('sdist','bdist_rpm'):
43 from IPython.genutils import target_update
43 from IPython.genutils import target_update
44 # list of things to be updated. Each entry is a triplet of args for
44 # list of things to be updated. Each entry is a triplet of args for
45 # target_update()
45 # target_update()
46 to_update = [('doc/magic.tex',
46 to_update = [('doc/magic.tex',
47 ['IPython/Magic.py'],
47 ['IPython/Magic.py'],
48 "cd doc && ./update_magic.sh" ),
48 "cd doc && ./update_magic.sh" ),
49
49
50 ('doc/manual.lyx',
50 ('doc/manual.lyx',
51 ['IPython/Release.py','doc/manual_base.lyx'],
51 ['IPython/Release.py','doc/manual_base.lyx'],
52 "cd doc && ./update_version.sh" ),
52 "cd doc && ./update_version.sh" ),
53
53
54 ('doc/manual/manual.html',
54 ('doc/manual/manual.html',
55 ['doc/manual.lyx',
55 ['doc/manual.lyx',
56 'doc/magic.tex',
56 'doc/magic.tex',
57 'doc/examples/example-gnuplot.py',
57 'doc/examples/example-gnuplot.py',
58 'doc/examples/example-magic.py',
58 'doc/examples/example-magic.py',
59 'doc/examples/example-embed.py',
59 'doc/examples/example-embed.py',
60 'doc/examples/example-embed-short.py',
60 'doc/examples/example-embed-short.py',
61 'IPython/UserConfig/ipythonrc',
61 'IPython/UserConfig/ipythonrc',
62 ],
62 ],
63 "cd doc && "
63 "cd doc && "
64 "lyxport -tt --leave --pdf "
64 "lyxport -tt --leave --pdf "
65 "--html -o '-noinfo -split +1 -local_icons' manual.lyx"),
65 "--html -o '-noinfo -split +1 -local_icons' manual.lyx"),
66
66
67 ('doc/new_design.pdf',
67 ('doc/new_design.pdf',
68 ['doc/new_design.lyx'],
68 ['doc/new_design.lyx'],
69 "cd doc && lyxport -tt --pdf new_design.lyx"),
69 "cd doc && lyxport -tt --pdf new_design.lyx"),
70
70
71 ('doc/ipython.1.gz',
71 ('doc/ipython.1.gz',
72 ['doc/ipython.1'],
72 ['doc/ipython.1'],
73 "cd doc && gzip -9c ipython.1 > ipython.1.gz"),
73 "cd doc && gzip -9c ipython.1 > ipython.1.gz"),
74
74
75 ('doc/pycolor.1.gz',
75 ('doc/pycolor.1.gz',
76 ['doc/pycolor.1'],
76 ['doc/pycolor.1'],
77 "cd doc && gzip -9c pycolor.1 > pycolor.1.gz"),
77 "cd doc && gzip -9c pycolor.1 > pycolor.1.gz"),
78 ]
78 ]
79 for target in to_update:
79 for target in to_update:
80 target_update(*target)
80 target_update(*target)
81
81
82 # Release.py contains version, authors, license, url, keywords, etc.
82 # Release.py contains version, authors, license, url, keywords, etc.
83 execfile(os.path.join('IPython','Release.py'))
83 execfile(os.path.join('IPython','Release.py'))
84
84
85 # A little utility we'll need below, since glob() does NOT allow you to do
85 # A little utility we'll need below, since glob() does NOT allow you to do
86 # exclusion on multiple endings!
86 # exclusion on multiple endings!
87 def file_doesnt_endwith(test,endings):
87 def file_doesnt_endwith(test,endings):
88 """Return true if test is a file and its name does NOT end with any
88 """Return true if test is a file and its name does NOT end with any
89 of the strings listed in endings."""
89 of the strings listed in endings."""
90 if not isfile(test):
90 if not isfile(test):
91 return False
91 return False
92 for e in endings:
92 for e in endings:
93 if test.endswith(e):
93 if test.endswith(e):
94 return False
94 return False
95 return True
95 return True
96
96
97 # I can't find how to make distutils create a nested dir. structure, so
97 # I can't find how to make distutils create a nested dir. structure, so
98 # in the meantime do it manually. Butt ugly.
98 # in the meantime do it manually. Butt ugly.
99 # Note that http://www.redbrick.dcu.ie/~noel/distutils.html, ex. 2/3, contain
99 # Note that http://www.redbrick.dcu.ie/~noel/distutils.html, ex. 2/3, contain
100 # information on how to do this more cleanly once python 2.4 can be assumed.
100 # information on how to do this more cleanly once python 2.4 can be assumed.
101 # Thanks to Noel for the tip.
101 # Thanks to Noel for the tip.
102 docdirbase = 'share/doc/ipython-%s' % version
102 docdirbase = 'share/doc/ipython-%s' % version
103 manpagebase = 'share/man/man1'
103 manpagebase = 'share/man/man1'
104
104
105 # We only need to exclude from this things NOT already excluded in the
105 # We only need to exclude from this things NOT already excluded in the
106 # MANIFEST.in file.
106 # MANIFEST.in file.
107 exclude = ('.sh','.1.gz')
107 exclude = ('.sh','.1.gz')
108 docfiles = filter(lambda f:file_doesnt_endwith(f,exclude),glob('doc/*'))
108 docfiles = filter(lambda f:file_doesnt_endwith(f,exclude),glob('doc/*'))
109
109
110 examfiles = filter(isfile, glob('doc/examples/*.py'))
110 examfiles = filter(isfile, glob('doc/examples/*.py'))
111 manfiles = filter(isfile, glob('doc/manual/*.html')) + \
111 manfiles = filter(isfile, glob('doc/manual/*.html')) + \
112 filter(isfile, glob('doc/manual/*.css')) + \
112 filter(isfile, glob('doc/manual/*.css')) + \
113 filter(isfile, glob('doc/manual/*.png'))
113 filter(isfile, glob('doc/manual/*.png'))
114 manpages = filter(isfile, glob('doc/*.1.gz'))
114 manpages = filter(isfile, glob('doc/*.1.gz'))
115 cfgfiles = filter(isfile, glob('IPython/UserConfig/*'))
115 cfgfiles = filter(isfile, glob('IPython/UserConfig/*'))
116 scriptfiles = filter(isfile, ['scripts/ipython','scripts/pycolor'])
116 scriptfiles = filter(isfile, ['scripts/ipython','scripts/pycolor'])
117
117
118 # Script to be run by the windows binary installer after the default setup
118 # Script to be run by the windows binary installer after the default setup
119 # routine, to add shortcuts and similar windows-only things. Windows
119 # routine, to add shortcuts and similar windows-only things. Windows
120 # post-install scripts MUST reside in the scripts/ dir, otherwise distutils
120 # post-install scripts MUST reside in the scripts/ dir, otherwise distutils
121 # doesn't find them.
121 # doesn't find them.
122 if 'bdist_wininst' in sys.argv:
122 if 'bdist_wininst' in sys.argv:
123 if len(sys.argv) > 2 and ('sdist' in sys.argv or 'bdist_rpm' in sys.argv):
123 if len(sys.argv) > 2 and ('sdist' in sys.argv or 'bdist_rpm' in sys.argv):
124 print >> sys.stderr,"ERROR: bdist_wininst must be run alone. Exiting."
124 print >> sys.stderr,"ERROR: bdist_wininst must be run alone. Exiting."
125 sys.exit(1)
125 sys.exit(1)
126 scriptfiles.append('scripts/ipython_win_post_install.py')
126 scriptfiles.append('scripts/ipython_win_post_install.py')
127
127
128 # Call the setup() routine which does most of the work
128 # Call the setup() routine which does most of the work
129 setup(name = name,
129 setup(name = name,
130 version = version,
130 version = version,
131 description = description,
131 description = description,
132 long_description = long_description,
132 long_description = long_description,
133 author = authors['Fernando'][0],
133 author = authors['Fernando'][0],
134 author_email = authors['Fernando'][1],
134 author_email = authors['Fernando'][1],
135 url = url,
135 url = url,
136 download_url = download_url,
136 license = license,
137 license = license,
137 platforms = platforms,
138 platforms = platforms,
138 keywords = keywords,
139 keywords = keywords,
139 packages = ['IPython', 'IPython.Extensions'],
140 packages = ['IPython', 'IPython.Extensions'],
140 scripts = scriptfiles,
141 scripts = scriptfiles,
141 cmdclass = {'install_data': install_data_ext},
142 cmdclass = {'install_data': install_data_ext},
142 data_files = [('data', docdirbase, docfiles),
143 data_files = [('data', docdirbase, docfiles),
143 ('data', os.path.join(docdirbase, 'examples'),
144 ('data', os.path.join(docdirbase, 'examples'),
144 examfiles),
145 examfiles),
145 ('data', os.path.join(docdirbase, 'manual'),
146 ('data', os.path.join(docdirbase, 'manual'),
146 manfiles),
147 manfiles),
147 ('data', manpagebase, manpages),
148 ('data', manpagebase, manpages),
148 ('lib', 'IPython/UserConfig', cfgfiles)]
149 ('lib', 'IPython/UserConfig', cfgfiles)]
149 )
150 )
General Comments 0
You need to be logged in to leave comments. Login now