##// END OF EJS Templates
Small fix in ultraTB, and fix autocall....
fperez -
Show More

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

@@ -1,164 +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 951 2005-12-25 00:57:24Z fperez $"""
4 $Id: ColorANSI.py 994 2006-01-08 08:29:44Z fperez $"""
5
5
6 #*****************************************************************************
6 #*****************************************************************************
7 # Copyright (C) 2002-2004 Fernando Perez. <fperez@colorado.edu>
7 # Copyright (C) 2002-2006 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
20
21 from IPython.Struct import Struct
21 from IPython.Struct import Struct
22
22
23 def make_color_table(in_class):
23 def make_color_table(in_class):
24 """Build a set of color attributes in a class.
24 """Build a set of color attributes in a class.
25
25
26 Helper function for building the *TermColors classes."""
26 Helper function for building the *TermColors classes."""
27
27
28 color_templates = (
28 color_templates = (
29 ("Black" , "0;30"),
29 ("Black" , "0;30"),
30 ("Red" , "0;31"),
30 ("Red" , "0;31"),
31 ("Green" , "0;32"),
31 ("Green" , "0;32"),
32 ("Brown" , "0;33"),
32 ("Brown" , "0;33"),
33 ("Blue" , "0;34"),
33 ("Blue" , "0;34"),
34 ("Purple" , "0;35"),
34 ("Purple" , "0;35"),
35 ("Cyan" , "0;36"),
35 ("Cyan" , "0;36"),
36 ("LightGray" , "0;37"),
36 ("LightGray" , "0;37"),
37 ("DarkGray" , "1;30"),
37 ("DarkGray" , "1;30"),
38 ("LightRed" , "1;31"),
38 ("LightRed" , "1;31"),
39 ("LightGreen" , "1;32"),
39 ("LightGreen" , "1;32"),
40 ("Yellow" , "1;33"),
40 ("Yellow" , "1;33"),
41 ("LightBlue" , "1;34"),
41 ("LightBlue" , "1;34"),
42 ("LightPurple" , "1;35"),
42 ("LightPurple" , "1;35"),
43 ("LightCyan" , "1;36"),
43 ("LightCyan" , "1;36"),
44 ("White" , "1;37"), )
44 ("White" , "1;37"), )
45
45
46 for name,value in color_templates:
46 for name,value in color_templates:
47 setattr(in_class,name,in_class._base % value)
47 setattr(in_class,name,in_class._base % value)
48
48
49 class TermColors:
49 class TermColors:
50 """Color escape sequences.
50 """Color escape sequences.
51
51
52 This class defines the escape sequences for all the standard (ANSI?)
52 This class defines the escape sequences for all the standard (ANSI?)
53 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
54 string, suitable for defining 'dummy' color schemes in terminals which get
54 string, suitable for defining 'dummy' color schemes in terminals which get
55 confused by color escapes.
55 confused by color escapes.
56
56
57 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."""
58
58
59 NoColor = '' # for color schemes in color-less terminals.
59 NoColor = '' # for color schemes in color-less terminals.
60 Normal = '\033[0m' # Reset normal coloring
60 Normal = '\033[0m' # Reset normal coloring
61 _base = '\033[%sm' # Template for all other colors
61 _base = '\033[%sm' # Template for all other colors
62
62
63 # Build the actual color table as a set of class attributes:
63 # Build the actual color table as a set of class attributes:
64 make_color_table(TermColors)
64 make_color_table(TermColors)
65
65
66 class InputTermColors:
66 class InputTermColors:
67 """Color escape sequences for input prompts.
67 """Color escape sequences for input prompts.
68
68
69 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
70 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
71 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
72 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().
73
73
74 This class defines the escape sequences for all the standard (ANSI?)
74 This class defines the escape sequences for all the standard (ANSI?)
75 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
76 string, suitable for defining 'dummy' color schemes in terminals which get
76 string, suitable for defining 'dummy' color schemes in terminals which get
77 confused by color escapes.
77 confused by color escapes.
78
78
79 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."""
80
80
81 NoColor = '' # for color schemes in color-less terminals.
81 NoColor = '' # for color schemes in color-less terminals.
82 Normal = '\001\033[0m\002' # Reset normal coloring
82 Normal = '\001\033[0m\002' # Reset normal coloring
83 _base = '\001\033[%sm\002' # Template for all other colors
83 _base = '\001\033[%sm\002' # Template for all other colors
84
84
85 # Build the actual color table as a set of class attributes:
85 # Build the actual color table as a set of class attributes:
86 make_color_table(InputTermColors)
86 make_color_table(InputTermColors)
87
87
88 class ColorScheme:
88 class ColorScheme:
89 """Generic color scheme class. Just a name and a Struct."""
89 """Generic color scheme class. Just a name and a Struct."""
90 def __init__(self,__scheme_name_,colordict=None,**colormap):
90 def __init__(self,__scheme_name_,colordict=None,**colormap):
91 self.name = __scheme_name_
91 self.name = __scheme_name_
92 if colordict is None:
92 if colordict is None:
93 self.colors = Struct(**colormap)
93 self.colors = Struct(**colormap)
94 else:
94 else:
95 self.colors = Struct(colordict)
95 self.colors = Struct(colordict)
96
96
97 def copy(self,name=None):
97 def copy(self,name=None):
98 """Return a full copy of the object, optionally renaming it."""
98 """Return a full copy of the object, optionally renaming it."""
99 if name is None:
99 if name is None:
100 name = self.name
100 name = self.name
101 return ColorScheme(name,self.colors.__dict__)
101 return ColorScheme(name,self.colors.__dict__)
102
102
103 class ColorSchemeTable(dict):
103 class ColorSchemeTable(dict):
104 """General class to handle tables of color schemes.
104 """General class to handle tables of color schemes.
105
105
106 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
107 attributes and some convenient methods.
107 attributes and some convenient methods.
108
108
109 active_scheme_name -> obvious
109 active_scheme_name -> obvious
110 active_colors -> actual color table of the active scheme"""
110 active_colors -> actual color table of the active scheme"""
111
111
112 def __init__(self,scheme_list=None,default_scheme=''):
112 def __init__(self,scheme_list=None,default_scheme=''):
113 """Create a table of color schemes.
113 """Create a table of color schemes.
114
114
115 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
116 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
117 the default active scheme.
117 the default active scheme.
118 """
118 """
119
119
120 # create object attributes to be set later
120 # create object attributes to be set later
121 self.active_scheme_name = ''
121 self.active_scheme_name = ''
122 self.active_colors = None
122 self.active_colors = None
123
123
124 if scheme_list:
124 if scheme_list:
125 if default_scheme == '':
125 if default_scheme == '':
126 raise ValueError,'you must specify the default color scheme'
126 raise ValueError,'you must specify the default color scheme'
127 for scheme in scheme_list:
127 for scheme in scheme_list:
128 self.add_scheme(scheme)
128 self.add_scheme(scheme)
129 self.set_active_scheme(default_scheme)
129 self.set_active_scheme(default_scheme)
130
130
131 def copy(self):
131 def copy(self):
132 """Return full copy of object"""
132 """Return full copy of object"""
133 return ColorSchemeTable(self.values(),self.active_scheme_name)
133 return ColorSchemeTable(self.values(),self.active_scheme_name)
134
134
135 def add_scheme(self,new_scheme):
135 def add_scheme(self,new_scheme):
136 """Add a new color scheme to the table."""
136 """Add a new color scheme to the table."""
137 if not isinstance(new_scheme,ColorScheme):
137 if not isinstance(new_scheme,ColorScheme):
138 raise ValueError,'ColorSchemeTable only accepts ColorScheme instances'
138 raise ValueError,'ColorSchemeTable only accepts ColorScheme instances'
139 self[new_scheme.name] = new_scheme
139 self[new_scheme.name] = new_scheme
140
140
141 def set_active_scheme(self,scheme,case_sensitive=0):
141 def set_active_scheme(self,scheme,case_sensitive=0):
142 """Set the currently active scheme.
142 """Set the currently active scheme.
143
143
144 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
145 be changed by setting the parameter case_sensitive to true."""
145 be changed by setting the parameter case_sensitive to true."""
146
146
147 scheme_names = self.keys()
147 scheme_names = self.keys()
148 if case_sensitive:
148 if case_sensitive:
149 valid_schemes = scheme_names
149 valid_schemes = scheme_names
150 scheme_test = scheme
150 scheme_test = scheme
151 else:
151 else:
152 valid_schemes = [s.lower() for s in scheme_names]
152 valid_schemes = [s.lower() for s in scheme_names]
153 scheme_test = scheme.lower()
153 scheme_test = scheme.lower()
154 try:
154 try:
155 scheme_idx = valid_schemes.index(scheme_test)
155 scheme_idx = valid_schemes.index(scheme_test)
156 except ValueError:
156 except ValueError:
157 raise ValueError,'Unrecognized color scheme: ' + scheme + \
157 raise ValueError,'Unrecognized color scheme: ' + scheme + \
158 '\nValid schemes: '+str(scheme_names).replace("'', ",'')
158 '\nValid schemes: '+str(scheme_names).replace("'', ",'')
159 else:
159 else:
160 active = scheme_names[scheme_idx]
160 active = scheme_names[scheme_idx]
161 self.active_scheme_name = active
161 self.active_scheme_name = active
162 self.active_colors = self[active].colors
162 self.active_colors = self[active].colors
163 # Now allow using '' as an index for the current active scheme
163 # Now allow using '' as an index for the current active scheme
164 self[''] = self[active]
164 self[''] = self[active]
@@ -1,116 +1,116 b''
1 # -*- coding: utf-8 -*-
1 # -*- coding: utf-8 -*-
2 """Configuration loader
2 """Configuration loader
3
3
4 $Id: ConfigLoader.py 958 2005-12-27 23:17:51Z fperez $"""
4 $Id: ConfigLoader.py 994 2006-01-08 08:29:44Z fperez $"""
5
5
6 #*****************************************************************************
6 #*****************************************************************************
7 # Copyright (C) 2001-2004 Fernando Perez. <fperez@colorado.edu>
7 # Copyright (C) 2001-2006 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 import exceptions
17 import exceptions
18 import os
18 import os
19 from pprint import pprint
19 from pprint import pprint
20
20
21 from IPython import ultraTB
21 from IPython import ultraTB
22 from IPython.Struct import Struct
22 from IPython.Struct import Struct
23 from IPython.genutils import *
23 from IPython.genutils import *
24
24
25 class ConfigLoaderError(exceptions.Exception):
25 class ConfigLoaderError(exceptions.Exception):
26 """Exception for ConfigLoader class."""
26 """Exception for ConfigLoader class."""
27
27
28 def __init__(self,args=None):
28 def __init__(self,args=None):
29 self.args = args
29 self.args = args
30
30
31 class ConfigLoader:
31 class ConfigLoader:
32
32
33 """Configuration file loader capable of handling recursive inclusions and
33 """Configuration file loader capable of handling recursive inclusions and
34 with parametrized conflict resolution for multiply found keys."""
34 with parametrized conflict resolution for multiply found keys."""
35
35
36 def __init__(self,conflict=None,field_sep=None,reclimit=15):
36 def __init__(self,conflict=None,field_sep=None,reclimit=15):
37
37
38 """The reclimit parameter controls the number of recursive
38 """The reclimit parameter controls the number of recursive
39 configuration file inclusions. This way we can stop early on (before
39 configuration file inclusions. This way we can stop early on (before
40 python's own recursion limit is hit) if there is a circular
40 python's own recursion limit is hit) if there is a circular
41 inclusion.
41 inclusion.
42
42
43 - conflict: dictionary for conflict resolutions (see Struct.merge())
43 - conflict: dictionary for conflict resolutions (see Struct.merge())
44
44
45 """
45 """
46 self.conflict = conflict
46 self.conflict = conflict
47 self.field_sep = field_sep
47 self.field_sep = field_sep
48 self.reset(reclimit)
48 self.reset(reclimit)
49
49
50 def reset(self,reclimit=15):
50 def reset(self,reclimit=15):
51 self.reclimit = reclimit
51 self.reclimit = reclimit
52 self.recdepth = 0
52 self.recdepth = 0
53 self.included = []
53 self.included = []
54
54
55 def load(self,fname,convert=None,recurse_key='',incpath = '.',**kw):
55 def load(self,fname,convert=None,recurse_key='',incpath = '.',**kw):
56 """Load a configuration file, return the resulting Struct.
56 """Load a configuration file, return the resulting Struct.
57
57
58 Call: load_config(fname,convert=None,conflict=None,recurse_key='')
58 Call: load_config(fname,convert=None,conflict=None,recurse_key='')
59
59
60 - fname: file to load from.
60 - fname: file to load from.
61 - convert: dictionary of type conversions (see read_dict())
61 - convert: dictionary of type conversions (see read_dict())
62 - recurse_key: keyword in dictionary to trigger recursive file
62 - recurse_key: keyword in dictionary to trigger recursive file
63 inclusions.
63 inclusions.
64 """
64 """
65
65
66 if self.recdepth > self.reclimit:
66 if self.recdepth > self.reclimit:
67 raise ConfigLoaderError, 'maximum recursive inclusion of rcfiles '+\
67 raise ConfigLoaderError, 'maximum recursive inclusion of rcfiles '+\
68 'exceeded: ' + `self.recdepth` + \
68 'exceeded: ' + `self.recdepth` + \
69 '.\nMaybe you have a circular chain of inclusions?'
69 '.\nMaybe you have a circular chain of inclusions?'
70 self.recdepth += 1
70 self.recdepth += 1
71 fname = filefind(fname,incpath)
71 fname = filefind(fname,incpath)
72 data = Struct()
72 data = Struct()
73 # avoid including the same file more than once
73 # avoid including the same file more than once
74 if fname in self.included:
74 if fname in self.included:
75 return data
75 return data
76 Xinfo = ultraTB.AutoFormattedTB()
76 Xinfo = ultraTB.AutoFormattedTB()
77 if convert==None and recurse_key : convert = {qwflat:recurse_key}
77 if convert==None and recurse_key : convert = {qwflat:recurse_key}
78 # for production, change warn to 0:
78 # for production, change warn to 0:
79 data.merge(read_dict(fname,convert,fs=self.field_sep,strip=1,
79 data.merge(read_dict(fname,convert,fs=self.field_sep,strip=1,
80 warn=0,no_empty=0,**kw))
80 warn=0,no_empty=0,**kw))
81 # keep track of successfully loaded files
81 # keep track of successfully loaded files
82 self.included.append(fname)
82 self.included.append(fname)
83 if recurse_key in data.keys():
83 if recurse_key in data.keys():
84 for incfilename in data[recurse_key]:
84 for incfilename in data[recurse_key]:
85 found=0
85 found=0
86 try:
86 try:
87 incfile = filefind(incfilename,incpath)
87 incfile = filefind(incfilename,incpath)
88 except IOError:
88 except IOError:
89 if os.name in ['nt','dos']:
89 if os.name in ['nt','dos']:
90 try:
90 try:
91 # Try again with '.ini' extension
91 # Try again with '.ini' extension
92 incfilename += '.ini'
92 incfilename += '.ini'
93 incfile = filefind(incfilename,incpath)
93 incfile = filefind(incfilename,incpath)
94 except IOError:
94 except IOError:
95 found = 0
95 found = 0
96 else:
96 else:
97 found = 1
97 found = 1
98 else:
98 else:
99 found = 0
99 found = 0
100 else:
100 else:
101 found = 1
101 found = 1
102 if found:
102 if found:
103 try:
103 try:
104 data.merge(self.load(incfile,convert,recurse_key,
104 data.merge(self.load(incfile,convert,recurse_key,
105 incpath,**kw),
105 incpath,**kw),
106 self.conflict)
106 self.conflict)
107 except:
107 except:
108 Xinfo()
108 Xinfo()
109 warn('Problem loading included file: '+
109 warn('Problem loading included file: '+
110 `incfilename` + '. Ignoring it...')
110 `incfilename` + '. Ignoring it...')
111 else:
111 else:
112 warn('File `%s` not found. Included by %s' % (incfilename,fname))
112 warn('File `%s` not found. Included by %s' % (incfilename,fname))
113
113
114 return data
114 return data
115
115
116 # end ConfigLoader
116 # end ConfigLoader
@@ -1,111 +1,111 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 958 2005-12-27 23:17:51Z fperez $"""
4 $Id: CrashHandler.py 994 2006-01-08 08:29:44Z fperez $"""
5
5
6 #*****************************************************************************
6 #*****************************************************************************
7 # Copyright (C) 2001-2004 Fernando Perez. <fperez@colorado.edu>
7 # Copyright (C) 2001-2006 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
22 import os
23 import sys
23 import sys
24 from pprint import pprint,pformat
24 from pprint import pprint,pformat
25
25
26 # Homebrewed
26 # Homebrewed
27 from IPython.Itpl import Itpl,itpl,printpl
27 from IPython.Itpl import Itpl,itpl,printpl
28 from IPython.ColorANSI import ColorScheme,ColorSchemeTable # too long names
28 from IPython.ColorANSI import ColorScheme,ColorSchemeTable # too long names
29 from IPython import ultraTB
29 from IPython import ultraTB
30 from IPython.genutils import *
30 from IPython.genutils import *
31
31
32 #****************************************************************************
32 #****************************************************************************
33 class CrashHandler:
33 class CrashHandler:
34 """sys.excepthook for IPython itself, leaves a detailed report on disk."""
34 """sys.excepthook for IPython itself, leaves a detailed report on disk."""
35
35
36 def __init__(self,IP):
36 def __init__(self,IP):
37 self.IP = IP # IPython instance
37 self.IP = IP # IPython instance
38 self.bug_contact = Release.authors['Fernando'][0]
38 self.bug_contact = Release.authors['Fernando'][0]
39 self.mailto = Release.authors['Fernando'][1]
39 self.mailto = Release.authors['Fernando'][1]
40
40
41 def __call__(self,etype, evalue, etb):
41 def __call__(self,etype, evalue, etb):
42
42
43 # Report tracebacks shouldn't use color in general (safer for users)
43 # Report tracebacks shouldn't use color in general (safer for users)
44 color_scheme = 'NoColor'
44 color_scheme = 'NoColor'
45
45
46 # Use this ONLY for developer debugging (keep commented out for release)
46 # Use this ONLY for developer debugging (keep commented out for release)
47 #color_scheme = 'Linux' # dbg
47 #color_scheme = 'Linux' # dbg
48
48
49 try:
49 try:
50 rptdir = self.IP.rc.ipythondir
50 rptdir = self.IP.rc.ipythondir
51 except:
51 except:
52 rptdir = os.getcwd()
52 rptdir = os.getcwd()
53 if not os.path.isdir(rptdir):
53 if not os.path.isdir(rptdir):
54 rptdir = os.getcwd()
54 rptdir = os.getcwd()
55 self.report_name = os.path.join(rptdir,'IPython_crash_report.txt')
55 self.report_name = os.path.join(rptdir,'IPython_crash_report.txt')
56 self.TBhandler = ultraTB.VerboseTB(color_scheme=color_scheme,long_header=1)
56 self.TBhandler = ultraTB.VerboseTB(color_scheme=color_scheme,long_header=1)
57 traceback = self.TBhandler.text(etype,evalue,etb,context=31)
57 traceback = self.TBhandler.text(etype,evalue,etb,context=31)
58
58
59 # print traceback to screen
59 # print traceback to screen
60 print >> sys.stderr, traceback
60 print >> sys.stderr, traceback
61
61
62 # and generate a complete report on disk
62 # and generate a complete report on disk
63 try:
63 try:
64 report = open(self.report_name,'w')
64 report = open(self.report_name,'w')
65 except:
65 except:
66 print >> sys.stderr, 'Could not create crash report on disk.'
66 print >> sys.stderr, 'Could not create crash report on disk.'
67 return
67 return
68
68
69 msg = itpl('\n'+'*'*70+'\n'
69 msg = itpl('\n'+'*'*70+'\n'
70 """
70 """
71 Oops, IPython crashed. We do our best to make it stable, but...
71 Oops, IPython crashed. We do our best to make it stable, but...
72
72
73 A crash report was automatically generated with the following information:
73 A crash report was automatically generated with the following information:
74 - A verbatim copy of the traceback above this text.
74 - A verbatim copy of the traceback above this text.
75 - A copy of your input history during this session.
75 - A copy of your input history during this session.
76 - Data on your current IPython configuration.
76 - Data on your current IPython configuration.
77
77
78 It was left in the file named:
78 It was left in the file named:
79 \t'$self.report_name'
79 \t'$self.report_name'
80 If you can email this file to the developers, the information in it will help
80 If you can email this file to the developers, the information in it will help
81 them in understanding and correcting the problem.
81 them in understanding and correcting the problem.
82
82
83 You can mail it to $self.bug_contact at $self.mailto
83 You can mail it to $self.bug_contact at $self.mailto
84 with the subject 'IPython Crash Report'.
84 with the subject 'IPython Crash Report'.
85
85
86 If you want to do it now, the following command will work (under Unix):
86 If you want to do it now, the following command will work (under Unix):
87 mail -s 'IPython Crash Report' $self.mailto < $self.report_name
87 mail -s 'IPython Crash Report' $self.mailto < $self.report_name
88
88
89 To ensure accurate tracking of this issue, please file a report about it at:
89 To ensure accurate tracking of this issue, please file a report about it at:
90 http://www.scipy.net/roundup/ipython (IPython's online bug tracker).
90 http://www.scipy.net/roundup/ipython (IPython's online bug tracker).
91 """)
91 """)
92 print >> sys.stderr, msg
92 print >> sys.stderr, msg
93
93
94 sec_sep = '\n\n'+'*'*75+'\n\n'
94 sec_sep = '\n\n'+'*'*75+'\n\n'
95 report.write('*'*75+'\n\n'+'IPython post-mortem report\n\n')
95 report.write('*'*75+'\n\n'+'IPython post-mortem report\n\n')
96 report.write('IPython version: %s \n\n' % Release.version)
96 report.write('IPython version: %s \n\n' % Release.version)
97 report.write('SVN revision : %s \n\n' % Release.revision)
97 report.write('SVN revision : %s \n\n' % Release.revision)
98 report.write('Platform info : os.name -> %s, sys.platform -> %s' %
98 report.write('Platform info : os.name -> %s, sys.platform -> %s' %
99 (os.name,sys.platform) )
99 (os.name,sys.platform) )
100 report.write(sec_sep+'Current user configuration structure:\n\n')
100 report.write(sec_sep+'Current user configuration structure:\n\n')
101 report.write(pformat(self.IP.rc.dict()))
101 report.write(pformat(self.IP.rc.dict()))
102 report.write(sec_sep+'Crash traceback:\n\n' + traceback)
102 report.write(sec_sep+'Crash traceback:\n\n' + traceback)
103 try:
103 try:
104 report.write(sec_sep+"History of session input:")
104 report.write(sec_sep+"History of session input:")
105 for line in self.IP.user_ns['_ih']:
105 for line in self.IP.user_ns['_ih']:
106 report.write(line)
106 report.write(line)
107 report.write('\n*** Last line of input (may not be in above history):\n')
107 report.write('\n*** Last line of input (may not be in above history):\n')
108 report.write(self.IP._last_input_line+'\n')
108 report.write(self.IP._last_input_line+'\n')
109 except:
109 except:
110 pass
110 pass
111 report.close()
111 report.close()
@@ -1,270 +1,286 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 958 2005-12-27 23:17:51Z fperez $"""
18 $Id: Debugger.py 994 2006-01-08 08:29:44Z fperez $"""
19
20 #*****************************************************************************
21 #
22 # Since this file is essentially a modified copy of the pdb module which is
23 # part of the standard Python distribution, I assume that the proper procedure
24 # is to maintain its copyright as belonging to the Python Software Foundation
25 # (in addition to my own, for all new code).
26 #
27 # Copyright (C) 2001 Python Software Foundation, www.python.org
28 # Copyright (C) 2005-2006 Fernando Perez. <fperez@colorado.edu>
29 #
30 # Distributed under the terms of the BSD License. The full license is in
31 # the file COPYING, distributed as part of this software.
32 #
33 #*****************************************************************************
34
19
35
20 from IPython import Release
36 from IPython import Release
21 __author__ = '%s <%s>' % Release.authors['Fernando']
37 __author__ = '%s <%s>' % Release.authors['Fernando']
22 __license__ = 'Python'
38 __license__ = 'Python'
23
39
24 import bdb
40 import bdb
25 import cmd
41 import cmd
26 import linecache
42 import linecache
27 import os
43 import os
28 import pdb
44 import pdb
29 import sys
45 import sys
30
46
31 from IPython import PyColorize, ColorANSI
47 from IPython import PyColorize, ColorANSI
32 from IPython.genutils import Term
48 from IPython.genutils import Term
33 from IPython.excolors import ExceptionColors
49 from IPython.excolors import ExceptionColors
34
50
35 def _file_lines(fname):
51 def _file_lines(fname):
36 """Return the contents of a named file as a list of lines.
52 """Return the contents of a named file as a list of lines.
37
53
38 This function never raises an IOError exception: if the file can't be
54 This function never raises an IOError exception: if the file can't be
39 read, it simply returns an empty list."""
55 read, it simply returns an empty list."""
40
56
41 try:
57 try:
42 outfile = open(fname)
58 outfile = open(fname)
43 except IOError:
59 except IOError:
44 return []
60 return []
45 else:
61 else:
46 out = outfile.readlines()
62 out = outfile.readlines()
47 outfile.close()
63 outfile.close()
48 return out
64 return out
49
65
50
66
51 class Pdb(pdb.Pdb):
67 class Pdb(pdb.Pdb):
52 """Modified Pdb class, does not load readline."""
68 """Modified Pdb class, does not load readline."""
53 def __init__(self,color_scheme='NoColor'):
69 def __init__(self,color_scheme='NoColor'):
54 bdb.Bdb.__init__(self)
70 bdb.Bdb.__init__(self)
55 cmd.Cmd.__init__(self,completekey=None) # don't load readline
71 cmd.Cmd.__init__(self,completekey=None) # don't load readline
56 self.prompt = 'ipdb> ' # The default prompt is '(Pdb)'
72 self.prompt = 'ipdb> ' # The default prompt is '(Pdb)'
57 self.aliases = {}
73 self.aliases = {}
58
74
59 # Read $HOME/.pdbrc and ./.pdbrc
75 # Read $HOME/.pdbrc and ./.pdbrc
60 try:
76 try:
61 self.rcLines = _file_lines(os.path.join(os.environ['HOME'],
77 self.rcLines = _file_lines(os.path.join(os.environ['HOME'],
62 ".pdbrc"))
78 ".pdbrc"))
63 except KeyError:
79 except KeyError:
64 self.rcLines = []
80 self.rcLines = []
65 self.rcLines.extend(_file_lines(".pdbrc"))
81 self.rcLines.extend(_file_lines(".pdbrc"))
66
82
67 # Create color table: we copy the default one from the traceback
83 # Create color table: we copy the default one from the traceback
68 # module and add a few attributes needed for debugging
84 # module and add a few attributes needed for debugging
69 self.color_scheme_table = ExceptionColors.copy()
85 self.color_scheme_table = ExceptionColors.copy()
70
86
71 # shorthands
87 # shorthands
72 C = ColorANSI.TermColors
88 C = ColorANSI.TermColors
73 cst = self.color_scheme_table
89 cst = self.color_scheme_table
74
90
75 cst['NoColor'].colors.breakpoint_enabled = C.NoColor
91 cst['NoColor'].colors.breakpoint_enabled = C.NoColor
76 cst['NoColor'].colors.breakpoint_disabled = C.NoColor
92 cst['NoColor'].colors.breakpoint_disabled = C.NoColor
77
93
78 cst['Linux'].colors.breakpoint_enabled = C.LightRed
94 cst['Linux'].colors.breakpoint_enabled = C.LightRed
79 cst['Linux'].colors.breakpoint_disabled = C.Red
95 cst['Linux'].colors.breakpoint_disabled = C.Red
80
96
81 cst['LightBG'].colors.breakpoint_enabled = C.LightRed
97 cst['LightBG'].colors.breakpoint_enabled = C.LightRed
82 cst['LightBG'].colors.breakpoint_disabled = C.Red
98 cst['LightBG'].colors.breakpoint_disabled = C.Red
83
99
84 self.set_colors(color_scheme)
100 self.set_colors(color_scheme)
85
101
86 def set_colors(self, scheme):
102 def set_colors(self, scheme):
87 """Shorthand access to the color table scheme selector method."""
103 """Shorthand access to the color table scheme selector method."""
88 self.color_scheme_table.set_active_scheme(scheme)
104 self.color_scheme_table.set_active_scheme(scheme)
89
105
90
106
91 def interaction(self, frame, traceback):
107 def interaction(self, frame, traceback):
92 __IPYTHON__.set_completer_frame(frame)
108 __IPYTHON__.set_completer_frame(frame)
93 pdb.Pdb.interaction(self, frame, traceback)
109 pdb.Pdb.interaction(self, frame, traceback)
94
110
95
111
96 def do_up(self, arg):
112 def do_up(self, arg):
97 pdb.Pdb.do_up(self, arg)
113 pdb.Pdb.do_up(self, arg)
98 __IPYTHON__.set_completer_frame(self.curframe)
114 __IPYTHON__.set_completer_frame(self.curframe)
99 do_u = do_up
115 do_u = do_up
100
116
101
117
102 def do_down(self, arg):
118 def do_down(self, arg):
103 pdb.Pdb.do_down(self, arg)
119 pdb.Pdb.do_down(self, arg)
104 __IPYTHON__.set_completer_frame(self.curframe)
120 __IPYTHON__.set_completer_frame(self.curframe)
105 do_d = do_down
121 do_d = do_down
106
122
107
123
108 def postloop(self):
124 def postloop(self):
109 __IPYTHON__.set_completer_frame(None)
125 __IPYTHON__.set_completer_frame(None)
110
126
111
127
112 def print_stack_trace(self):
128 def print_stack_trace(self):
113 try:
129 try:
114 for frame_lineno in self.stack:
130 for frame_lineno in self.stack:
115 self.print_stack_entry(frame_lineno, context = 5)
131 self.print_stack_entry(frame_lineno, context = 5)
116 except KeyboardInterrupt:
132 except KeyboardInterrupt:
117 pass
133 pass
118
134
119
135
120 def print_stack_entry(self,frame_lineno,prompt_prefix='\n-> ',
136 def print_stack_entry(self,frame_lineno,prompt_prefix='\n-> ',
121 context = 3):
137 context = 3):
122 frame, lineno = frame_lineno
138 frame, lineno = frame_lineno
123 print >>Term.cout, self.format_stack_entry(frame_lineno, '', context)
139 print >>Term.cout, self.format_stack_entry(frame_lineno, '', context)
124
140
125
141
126 def format_stack_entry(self, frame_lineno, lprefix=': ', context = 3):
142 def format_stack_entry(self, frame_lineno, lprefix=': ', context = 3):
127 import linecache, repr
143 import linecache, repr
128
144
129 ret = ""
145 ret = ""
130
146
131 Colors = self.color_scheme_table.active_colors
147 Colors = self.color_scheme_table.active_colors
132 ColorsNormal = Colors.Normal
148 ColorsNormal = Colors.Normal
133 tpl_link = '%s%%s%s' % (Colors.filenameEm, ColorsNormal)
149 tpl_link = '%s%%s%s' % (Colors.filenameEm, ColorsNormal)
134 tpl_call = 'in %s%%s%s%%s%s' % (Colors.vName, Colors.valEm, ColorsNormal)
150 tpl_call = 'in %s%%s%s%%s%s' % (Colors.vName, Colors.valEm, ColorsNormal)
135 tpl_line = '%%s%s%%s %s%%s' % (Colors.lineno, ColorsNormal)
151 tpl_line = '%%s%s%%s %s%%s' % (Colors.lineno, ColorsNormal)
136 tpl_line_em = '%%s%s%%s %s%%s%s' % (Colors.linenoEm, Colors.line,
152 tpl_line_em = '%%s%s%%s %s%%s%s' % (Colors.linenoEm, Colors.line,
137 ColorsNormal)
153 ColorsNormal)
138
154
139 frame, lineno = frame_lineno
155 frame, lineno = frame_lineno
140
156
141 return_value = ''
157 return_value = ''
142 if '__return__' in frame.f_locals:
158 if '__return__' in frame.f_locals:
143 rv = frame.f_locals['__return__']
159 rv = frame.f_locals['__return__']
144 #return_value += '->'
160 #return_value += '->'
145 return_value += repr.repr(rv) + '\n'
161 return_value += repr.repr(rv) + '\n'
146 ret += return_value
162 ret += return_value
147
163
148 #s = filename + '(' + `lineno` + ')'
164 #s = filename + '(' + `lineno` + ')'
149 filename = self.canonic(frame.f_code.co_filename)
165 filename = self.canonic(frame.f_code.co_filename)
150 link = tpl_link % filename
166 link = tpl_link % filename
151
167
152 if frame.f_code.co_name:
168 if frame.f_code.co_name:
153 func = frame.f_code.co_name
169 func = frame.f_code.co_name
154 else:
170 else:
155 func = "<lambda>"
171 func = "<lambda>"
156
172
157 call = ''
173 call = ''
158 if func != '?':
174 if func != '?':
159 if '__args__' in frame.f_locals:
175 if '__args__' in frame.f_locals:
160 args = repr.repr(frame.f_locals['__args__'])
176 args = repr.repr(frame.f_locals['__args__'])
161 else:
177 else:
162 args = '()'
178 args = '()'
163 call = tpl_call % (func, args)
179 call = tpl_call % (func, args)
164
180
165 level = '%s %s\n' % (link, call)
181 level = '%s %s\n' % (link, call)
166 ret += level
182 ret += level
167
183
168 start = lineno - 1 - context//2
184 start = lineno - 1 - context//2
169 lines = linecache.getlines(filename)
185 lines = linecache.getlines(filename)
170 start = max(start, 0)
186 start = max(start, 0)
171 start = min(start, len(lines) - context)
187 start = min(start, len(lines) - context)
172 lines = lines[start : start + context]
188 lines = lines[start : start + context]
173
189
174 for i in range(len(lines)):
190 for i in range(len(lines)):
175 line = lines[i]
191 line = lines[i]
176 if start + 1 + i == lineno:
192 if start + 1 + i == lineno:
177 ret += self.__format_line(tpl_line_em, filename, start + 1 + i, line, arrow = True)
193 ret += self.__format_line(tpl_line_em, filename, start + 1 + i, line, arrow = True)
178 else:
194 else:
179 ret += self.__format_line(tpl_line, filename, start + 1 + i, line, arrow = False)
195 ret += self.__format_line(tpl_line, filename, start + 1 + i, line, arrow = False)
180
196
181 return ret
197 return ret
182
198
183
199
184 def __format_line(self, tpl_line, filename, lineno, line, arrow = False):
200 def __format_line(self, tpl_line, filename, lineno, line, arrow = False):
185 bp_mark = ""
201 bp_mark = ""
186 bp_mark_color = ""
202 bp_mark_color = ""
187
203
188 bp = None
204 bp = None
189 if lineno in self.get_file_breaks(filename):
205 if lineno in self.get_file_breaks(filename):
190 bps = self.get_breaks(filename, lineno)
206 bps = self.get_breaks(filename, lineno)
191 bp = bps[-1]
207 bp = bps[-1]
192
208
193 if bp:
209 if bp:
194 Colors = self.color_scheme_table.active_colors
210 Colors = self.color_scheme_table.active_colors
195 bp_mark = str(bp.number)
211 bp_mark = str(bp.number)
196 bp_mark_color = Colors.breakpoint_enabled
212 bp_mark_color = Colors.breakpoint_enabled
197 if not bp.enabled:
213 if not bp.enabled:
198 bp_mark_color = Colors.breakpoint_disabled
214 bp_mark_color = Colors.breakpoint_disabled
199
215
200 numbers_width = 7
216 numbers_width = 7
201 if arrow:
217 if arrow:
202 # This is the line with the error
218 # This is the line with the error
203 pad = numbers_width - len(str(lineno)) - len(bp_mark)
219 pad = numbers_width - len(str(lineno)) - len(bp_mark)
204 if pad >= 3:
220 if pad >= 3:
205 marker = '-'*(pad-3) + '-> '
221 marker = '-'*(pad-3) + '-> '
206 elif pad == 2:
222 elif pad == 2:
207 marker = '> '
223 marker = '> '
208 elif pad == 1:
224 elif pad == 1:
209 marker = '>'
225 marker = '>'
210 else:
226 else:
211 marker = ''
227 marker = ''
212 num = '%s%s' % (marker, str(lineno))
228 num = '%s%s' % (marker, str(lineno))
213 line = tpl_line % (bp_mark_color + bp_mark, num, line)
229 line = tpl_line % (bp_mark_color + bp_mark, num, line)
214 else:
230 else:
215 num = '%*s' % (numbers_width - len(bp_mark), str(lineno))
231 num = '%*s' % (numbers_width - len(bp_mark), str(lineno))
216 line = tpl_line % (bp_mark_color + bp_mark, num, line)
232 line = tpl_line % (bp_mark_color + bp_mark, num, line)
217
233
218 return line
234 return line
219
235
220
236
221 def do_list(self, arg):
237 def do_list(self, arg):
222 self.lastcmd = 'list'
238 self.lastcmd = 'list'
223 last = None
239 last = None
224 if arg:
240 if arg:
225 try:
241 try:
226 x = eval(arg, {}, {})
242 x = eval(arg, {}, {})
227 if type(x) == type(()):
243 if type(x) == type(()):
228 first, last = x
244 first, last = x
229 first = int(first)
245 first = int(first)
230 last = int(last)
246 last = int(last)
231 if last < first:
247 if last < first:
232 # Assume it's a count
248 # Assume it's a count
233 last = first + last
249 last = first + last
234 else:
250 else:
235 first = max(1, int(x) - 5)
251 first = max(1, int(x) - 5)
236 except:
252 except:
237 print '*** Error in argument:', `arg`
253 print '*** Error in argument:', `arg`
238 return
254 return
239 elif self.lineno is None:
255 elif self.lineno is None:
240 first = max(1, self.curframe.f_lineno - 5)
256 first = max(1, self.curframe.f_lineno - 5)
241 else:
257 else:
242 first = self.lineno + 1
258 first = self.lineno + 1
243 if last is None:
259 if last is None:
244 last = first + 10
260 last = first + 10
245 filename = self.curframe.f_code.co_filename
261 filename = self.curframe.f_code.co_filename
246 try:
262 try:
247 Colors = self.color_scheme_table.active_colors
263 Colors = self.color_scheme_table.active_colors
248 ColorsNormal = Colors.Normal
264 ColorsNormal = Colors.Normal
249 tpl_line = '%%s%s%%s %s%%s' % (Colors.lineno, ColorsNormal)
265 tpl_line = '%%s%s%%s %s%%s' % (Colors.lineno, ColorsNormal)
250 tpl_line_em = '%%s%s%%s %s%%s%s' % (Colors.linenoEm, Colors.line, ColorsNormal)
266 tpl_line_em = '%%s%s%%s %s%%s%s' % (Colors.linenoEm, Colors.line, ColorsNormal)
251 src = []
267 src = []
252 for lineno in range(first, last+1):
268 for lineno in range(first, last+1):
253 line = linecache.getline(filename, lineno)
269 line = linecache.getline(filename, lineno)
254 if not line:
270 if not line:
255 break
271 break
256
272
257 if lineno == self.curframe.f_lineno:
273 if lineno == self.curframe.f_lineno:
258 line = self.__format_line(tpl_line_em, filename, lineno, line, arrow = True)
274 line = self.__format_line(tpl_line_em, filename, lineno, line, arrow = True)
259 else:
275 else:
260 line = self.__format_line(tpl_line, filename, lineno, line, arrow = False)
276 line = self.__format_line(tpl_line, filename, lineno, line, arrow = False)
261
277
262 src.append(line)
278 src.append(line)
263 self.lineno = lineno
279 self.lineno = lineno
264
280
265 print >>Term.cout, ''.join(src)
281 print >>Term.cout, ''.join(src)
266
282
267 except KeyboardInterrupt:
283 except KeyboardInterrupt:
268 pass
284 pass
269
285
270 do_l = do_list
286 do_l = do_list
@@ -1,272 +1,272 b''
1 # -*- coding: utf-8 -*-
1 # -*- coding: utf-8 -*-
2 """Modified input prompt for executing files.
2 """Modified input prompt for executing files.
3
3
4 We define a special input line filter to allow typing lines which begin with
4 We define a special input line filter to allow typing lines which begin with
5 '~', '/' or '.'. If one of those strings is encountered, it is automatically
5 '~', '/' or '.'. If one of those strings is encountered, it is automatically
6 executed.
6 executed.
7
7
8 $Id: InterpreterExec.py 638 2005-07-18 03:01:41Z fperez $"""
8 $Id: InterpreterExec.py 994 2006-01-08 08:29:44Z fperez $"""
9
9
10 #*****************************************************************************
10 #*****************************************************************************
11 # Copyright (C) 2004 W.J. van der Laan <gnufnork@hetdigitalegat.nl>
11 # Copyright (C) 2004 W.J. van der Laan <gnufnork@hetdigitalegat.nl>
12 # Copyright (C) 2004 Fernando Perez <fperez@colorado.edu>
12 # Copyright (C) 2004-2006 Fernando Perez <fperez@colorado.edu>
13 #
13 #
14 # Distributed under the terms of the BSD License. The full license is in
14 # Distributed under the terms of the BSD License. The full license is in
15 # the file COPYING, distributed as part of this software.
15 # the file COPYING, distributed as part of this software.
16 #*****************************************************************************
16 #*****************************************************************************
17
17
18 from IPython import Release
18 from IPython import Release
19 __author__ = 'W.J. van der Laan <gnufnork@hetdigitalegat.nl>, '\
19 __author__ = 'W.J. van der Laan <gnufnork@hetdigitalegat.nl>, '\
20 '%s <%s>' % Release.authors['Fernando']
20 '%s <%s>' % Release.authors['Fernando']
21 __license__ = Release.license
21 __license__ = Release.license
22
22
23 def prefilter_shell(self,line,continuation):
23 def prefilter_shell(self,line,continuation):
24 """Alternate prefilter, modified for shell-like functionality.
24 """Alternate prefilter, modified for shell-like functionality.
25
25
26 - Execute all lines beginning with '~', '/' or '.'
26 - Execute all lines beginning with '~', '/' or '.'
27 - $var=cmd <=> %sc var=cmd
27 - $var=cmd <=> %sc var=cmd
28 - $$var=cmd <=> %sc -l var=cmd
28 - $$var=cmd <=> %sc -l var=cmd
29 """
29 """
30
30
31 if line:
31 if line:
32 l0 = line[0]
32 l0 = line[0]
33 if l0 in '~/.':
33 if l0 in '~/.':
34 return self._prefilter("!%s"%line,continuation)
34 return self._prefilter("!%s"%line,continuation)
35 elif l0=='$':
35 elif l0=='$':
36 lrest = line[1:]
36 lrest = line[1:]
37 if lrest.startswith('$'):
37 if lrest.startswith('$'):
38 # $$var=cmd <=> %sc -l var=cmd
38 # $$var=cmd <=> %sc -l var=cmd
39 return self._prefilter("%ssc -l %s" % (self.ESC_MAGIC,lrest[1:]),
39 return self._prefilter("%ssc -l %s" % (self.ESC_MAGIC,lrest[1:]),
40 continuation)
40 continuation)
41 else:
41 else:
42 # $var=cmd <=> %sc var=cmd
42 # $var=cmd <=> %sc var=cmd
43 return self._prefilter("%ssc %s" % (self.ESC_MAGIC,lrest),
43 return self._prefilter("%ssc %s" % (self.ESC_MAGIC,lrest),
44 continuation)
44 continuation)
45 else:
45 else:
46 return self._prefilter(line,continuation)
46 return self._prefilter(line,continuation)
47 else:
47 else:
48 return self._prefilter(line,continuation)
48 return self._prefilter(line,continuation)
49
49
50 # Rebind this to be the new IPython prefilter:
50 # Rebind this to be the new IPython prefilter:
51 from IPython.iplib import InteractiveShell
51 from IPython.iplib import InteractiveShell
52 InteractiveShell.prefilter = prefilter_shell
52 InteractiveShell.prefilter = prefilter_shell
53 # Clean up the namespace.
53 # Clean up the namespace.
54 del InteractiveShell,prefilter_shell
54 del InteractiveShell,prefilter_shell
55
55
56 # Provide pysh and further shell-oriented services
56 # Provide pysh and further shell-oriented services
57 import os,sys,shutil
57 import os,sys,shutil
58 from IPython.genutils import system,shell,getoutput,getoutputerror
58 from IPython.genutils import system,shell,getoutput,getoutputerror
59
59
60 # Short aliases for getting shell output as a string and a list
60 # Short aliases for getting shell output as a string and a list
61 sout = getoutput
61 sout = getoutput
62 lout = lambda cmd: getoutput(cmd,split=1)
62 lout = lambda cmd: getoutput(cmd,split=1)
63
63
64 # Empty function, meant as a docstring holder so help(pysh) works.
64 # Empty function, meant as a docstring holder so help(pysh) works.
65 def pysh():
65 def pysh():
66 """Pysh is a set of modules and extensions to IPython which make shell-like
66 """Pysh is a set of modules and extensions to IPython which make shell-like
67 usage with Python syntax more convenient. Keep in mind that pysh is NOT a
67 usage with Python syntax more convenient. Keep in mind that pysh is NOT a
68 full-blown shell, so don't try to make it your /etc/passwd entry!
68 full-blown shell, so don't try to make it your /etc/passwd entry!
69
69
70 In particular, it has no job control, so if you type Ctrl-Z (under Unix),
70 In particular, it has no job control, so if you type Ctrl-Z (under Unix),
71 you'll suspend pysh itself, not the process you just started.
71 you'll suspend pysh itself, not the process you just started.
72
72
73 Since pysh is really nothing but a customized IPython, you should
73 Since pysh is really nothing but a customized IPython, you should
74 familiarize yourself with IPython's features. This brief help mainly
74 familiarize yourself with IPython's features. This brief help mainly
75 documents areas in which pysh differs from the normal IPython.
75 documents areas in which pysh differs from the normal IPython.
76
76
77 ALIASES
77 ALIASES
78 -------
78 -------
79 All of your $PATH has been loaded as IPython aliases, so you should be
79 All of your $PATH has been loaded as IPython aliases, so you should be
80 able to type any normal system command and have it executed. See %alias?
80 able to type any normal system command and have it executed. See %alias?
81 and %unalias? for details on the alias facilities.
81 and %unalias? for details on the alias facilities.
82
82
83 SPECIAL SYNTAX
83 SPECIAL SYNTAX
84 --------------
84 --------------
85 Any lines which begin with '~', '/' and '.' will be executed as shell
85 Any lines which begin with '~', '/' and '.' will be executed as shell
86 commands instead of as Python code. The special escapes below are also
86 commands instead of as Python code. The special escapes below are also
87 recognized. !cmd is valid in single or multi-line input, all others are
87 recognized. !cmd is valid in single or multi-line input, all others are
88 only valid in single-line input:
88 only valid in single-line input:
89
89
90 !cmd - pass 'cmd' directly to the shell
90 !cmd - pass 'cmd' directly to the shell
91 !!cmd - execute 'cmd' and return output as a list (split on '\\n')
91 !!cmd - execute 'cmd' and return output as a list (split on '\\n')
92 $var=cmd - capture output of cmd into var, as a string
92 $var=cmd - capture output of cmd into var, as a string
93 $$var=cmd - capture output of cmd into var, as a list (split on '\\n')
93 $$var=cmd - capture output of cmd into var, as a list (split on '\\n')
94
94
95 The $/$$ syntaxes make Python variables from system output, which you can
95 The $/$$ syntaxes make Python variables from system output, which you can
96 later use for further scripting. The converse is also possible: when
96 later use for further scripting. The converse is also possible: when
97 executing an alias or calling to the system via !/!!, you can expand any
97 executing an alias or calling to the system via !/!!, you can expand any
98 python variable or expression by prepending it with $. Full details of
98 python variable or expression by prepending it with $. Full details of
99 the allowed syntax can be found in Python's PEP 215.
99 the allowed syntax can be found in Python's PEP 215.
100
100
101 A few brief examples will illustrate these:
101 A few brief examples will illustrate these:
102
102
103 fperez[~/test]|3> !ls *s.py
103 fperez[~/test]|3> !ls *s.py
104 scopes.py strings.py
104 scopes.py strings.py
105
105
106 ls is an internal alias, so there's no need to use !:
106 ls is an internal alias, so there's no need to use !:
107 fperez[~/test]|4> ls *s.py
107 fperez[~/test]|4> ls *s.py
108 scopes.py* strings.py
108 scopes.py* strings.py
109
109
110 !!ls will return the output into a Python variable:
110 !!ls will return the output into a Python variable:
111 fperez[~/test]|5> !!ls *s.py
111 fperez[~/test]|5> !!ls *s.py
112 <5> ['scopes.py', 'strings.py']
112 <5> ['scopes.py', 'strings.py']
113 fperez[~/test]|6> print _5
113 fperez[~/test]|6> print _5
114 ['scopes.py', 'strings.py']
114 ['scopes.py', 'strings.py']
115
115
116 $ and $$ allow direct capture to named variables:
116 $ and $$ allow direct capture to named variables:
117 fperez[~/test]|7> $astr = ls *s.py
117 fperez[~/test]|7> $astr = ls *s.py
118 fperez[~/test]|8> astr
118 fperez[~/test]|8> astr
119 <8> 'scopes.py\\nstrings.py'
119 <8> 'scopes.py\\nstrings.py'
120
120
121 fperez[~/test]|9> $$alist = ls *s.py
121 fperez[~/test]|9> $$alist = ls *s.py
122 fperez[~/test]|10> alist
122 fperez[~/test]|10> alist
123 <10> ['scopes.py', 'strings.py']
123 <10> ['scopes.py', 'strings.py']
124
124
125 alist is now a normal python list you can loop over. Using $ will expand
125 alist is now a normal python list you can loop over. Using $ will expand
126 back the python values when alias calls are made:
126 back the python values when alias calls are made:
127 fperez[~/test]|11> for f in alist:
127 fperez[~/test]|11> for f in alist:
128 |..> print 'file',f,
128 |..> print 'file',f,
129 |..> wc -l $f
129 |..> wc -l $f
130 |..>
130 |..>
131 file scopes.py 13 scopes.py
131 file scopes.py 13 scopes.py
132 file strings.py 4 strings.py
132 file strings.py 4 strings.py
133
133
134 Note that you may need to protect your variables with braces if you want
134 Note that you may need to protect your variables with braces if you want
135 to append strings to their names. To copy all files in alist to .bak
135 to append strings to their names. To copy all files in alist to .bak
136 extensions, you must use:
136 extensions, you must use:
137 fperez[~/test]|12> for f in alist:
137 fperez[~/test]|12> for f in alist:
138 |..> cp $f ${f}.bak
138 |..> cp $f ${f}.bak
139
139
140 If you try using $f.bak, you'll get an AttributeError exception saying
140 If you try using $f.bak, you'll get an AttributeError exception saying
141 that your string object doesn't have a .bak attribute. This is because
141 that your string object doesn't have a .bak attribute. This is because
142 the $ expansion mechanism allows you to expand full Python expressions:
142 the $ expansion mechanism allows you to expand full Python expressions:
143 fperez[~/test]|13> echo "sys.platform is: $sys.platform"
143 fperez[~/test]|13> echo "sys.platform is: $sys.platform"
144 sys.platform is: linux2
144 sys.platform is: linux2
145
145
146 IPython's input history handling is still active, which allows you to
146 IPython's input history handling is still active, which allows you to
147 rerun a single block of multi-line input by simply using exec:
147 rerun a single block of multi-line input by simply using exec:
148 fperez[~/test]|14> $$alist = ls *.eps
148 fperez[~/test]|14> $$alist = ls *.eps
149 fperez[~/test]|15> exec _i11
149 fperez[~/test]|15> exec _i11
150 file image2.eps 921 image2.eps
150 file image2.eps 921 image2.eps
151 file image.eps 921 image.eps
151 file image.eps 921 image.eps
152
152
153 While these are new special-case syntaxes, they are designed to allow very
153 While these are new special-case syntaxes, they are designed to allow very
154 efficient use of the shell with minimal typing. At an interactive shell
154 efficient use of the shell with minimal typing. At an interactive shell
155 prompt, conciseness of expression wins over readability.
155 prompt, conciseness of expression wins over readability.
156
156
157 USEFUL FUNCTIONS AND MODULES
157 USEFUL FUNCTIONS AND MODULES
158 ----------------------------
158 ----------------------------
159 The os, sys and shutil modules from the Python standard library are
159 The os, sys and shutil modules from the Python standard library are
160 automatically loaded. Some additional functions, useful for shell usage,
160 automatically loaded. Some additional functions, useful for shell usage,
161 are listed below. You can request more help about them with '?'.
161 are listed below. You can request more help about them with '?'.
162
162
163 shell - execute a command in the underlying system shell
163 shell - execute a command in the underlying system shell
164 system - like shell(), but return the exit status of the command
164 system - like shell(), but return the exit status of the command
165 sout - capture the output of a command as a string
165 sout - capture the output of a command as a string
166 lout - capture the output of a command as a list (split on '\\n')
166 lout - capture the output of a command as a list (split on '\\n')
167 getoutputerror - capture (output,error) of a shell command
167 getoutputerror - capture (output,error) of a shell command
168
168
169 sout/lout are the functional equivalents of $/$$. They are provided to
169 sout/lout are the functional equivalents of $/$$. They are provided to
170 allow you to capture system output in the middle of true python code,
170 allow you to capture system output in the middle of true python code,
171 function definitions, etc (where $ and $$ are invalid).
171 function definitions, etc (where $ and $$ are invalid).
172
172
173 DIRECTORY MANAGEMENT
173 DIRECTORY MANAGEMENT
174 --------------------
174 --------------------
175 Since each command passed by pysh to the underlying system is executed in
175 Since each command passed by pysh to the underlying system is executed in
176 a subshell which exits immediately, you can NOT use !cd to navigate the
176 a subshell which exits immediately, you can NOT use !cd to navigate the
177 filesystem.
177 filesystem.
178
178
179 Pysh provides its own builtin '%cd' magic command to move in the
179 Pysh provides its own builtin '%cd' magic command to move in the
180 filesystem (the % is not required with automagic on). It also maintains a
180 filesystem (the % is not required with automagic on). It also maintains a
181 list of visited directories (use %dhist to see it) and allows direct
181 list of visited directories (use %dhist to see it) and allows direct
182 switching to any of them. Type 'cd?' for more details.
182 switching to any of them. Type 'cd?' for more details.
183
183
184 %pushd, %popd and %dirs are provided for directory stack handling.
184 %pushd, %popd and %dirs are provided for directory stack handling.
185
185
186 PROMPT CUSTOMIZATION
186 PROMPT CUSTOMIZATION
187 --------------------
187 --------------------
188
188
189 The supplied ipythonrc-pysh profile comes with an example of a very
189 The supplied ipythonrc-pysh profile comes with an example of a very
190 colored and detailed prompt, mainly to serve as an illustration. The
190 colored and detailed prompt, mainly to serve as an illustration. The
191 valid escape sequences, besides color names, are:
191 valid escape sequences, besides color names, are:
192
192
193 \\# - Prompt number.
193 \\# - Prompt number.
194 \\D - Dots, as many as there are digits in \\# (so they align).
194 \\D - Dots, as many as there are digits in \\# (so they align).
195 \\w - Current working directory (cwd).
195 \\w - Current working directory (cwd).
196 \\W - Basename of current working directory.
196 \\W - Basename of current working directory.
197 \\XN - Where N=0..5. N terms of the cwd, with $HOME written as ~.
197 \\XN - Where N=0..5. N terms of the cwd, with $HOME written as ~.
198 \\YN - Where N=0..5. Like XN, but if ~ is term N+1 it's also shown.
198 \\YN - Where N=0..5. Like XN, but if ~ is term N+1 it's also shown.
199 \\u - Username.
199 \\u - Username.
200 \\H - Full hostname.
200 \\H - Full hostname.
201 \\h - Hostname up to first '.'
201 \\h - Hostname up to first '.'
202 \\$ - Root symbol ($ or #).
202 \\$ - Root symbol ($ or #).
203 \\t - Current time, in H:M:S format.
203 \\t - Current time, in H:M:S format.
204 \\v - IPython release version.
204 \\v - IPython release version.
205 \\n - Newline.
205 \\n - Newline.
206 \\r - Carriage return.
206 \\r - Carriage return.
207 \\\\ - An explicitly escaped '\\'.
207 \\\\ - An explicitly escaped '\\'.
208
208
209 You can configure your prompt colors using any ANSI color escape. Each
209 You can configure your prompt colors using any ANSI color escape. Each
210 color escape sets the color for any subsequent text, until another escape
210 color escape sets the color for any subsequent text, until another escape
211 comes in and changes things. The valid color escapes are:
211 comes in and changes things. The valid color escapes are:
212
212
213 \\C_Black
213 \\C_Black
214 \\C_Blue
214 \\C_Blue
215 \\C_Brown
215 \\C_Brown
216 \\C_Cyan
216 \\C_Cyan
217 \\C_DarkGray
217 \\C_DarkGray
218 \\C_Green
218 \\C_Green
219 \\C_LightBlue
219 \\C_LightBlue
220 \\C_LightCyan
220 \\C_LightCyan
221 \\C_LightGray
221 \\C_LightGray
222 \\C_LightGreen
222 \\C_LightGreen
223 \\C_LightPurple
223 \\C_LightPurple
224 \\C_LightRed
224 \\C_LightRed
225 \\C_Purple
225 \\C_Purple
226 \\C_Red
226 \\C_Red
227 \\C_White
227 \\C_White
228 \\C_Yellow
228 \\C_Yellow
229 \\C_Normal - Stop coloring, defaults to your terminal settings.
229 \\C_Normal - Stop coloring, defaults to your terminal settings.
230 """
230 """
231 pass
231 pass
232
232
233 # Configure a few things. Much of this is fairly hackish, since IPython
233 # Configure a few things. Much of this is fairly hackish, since IPython
234 # doesn't really expose a clean API for it. Be careful if you start making
234 # doesn't really expose a clean API for it. Be careful if you start making
235 # many modifications here.
235 # many modifications here.
236
236
237 print """\
237 print """\
238 Welcome to pysh, a set of extensions to IPython for shell usage.
238 Welcome to pysh, a set of extensions to IPython for shell usage.
239 help(pysh) -> help on the installed shell extensions and syntax.
239 help(pysh) -> help on the installed shell extensions and syntax.
240 """
240 """
241
241
242 # Set the 'cd' command to quiet mode, a more shell-like behavior
242 # Set the 'cd' command to quiet mode, a more shell-like behavior
243 __IPYTHON__.default_option('cd','-q')
243 __IPYTHON__.default_option('cd','-q')
244
244
245 # Load all of $PATH as aliases
245 # Load all of $PATH as aliases
246 if os.name == 'posix':
246 if os.name == 'posix':
247 # %rehash is very fast, but it doesn't check for executability, it simply
247 # %rehash is very fast, but it doesn't check for executability, it simply
248 # dumps everything in $PATH as an alias. Use rehashx if you want more
248 # dumps everything in $PATH as an alias. Use rehashx if you want more
249 # checks.
249 # checks.
250 __IPYTHON__.magic_rehash()
250 __IPYTHON__.magic_rehash()
251 else:
251 else:
252 # Windows users: the list of extensions considered executable is read from
252 # Windows users: the list of extensions considered executable is read from
253 # the environment variable 'pathext'. If this is undefined, IPython
253 # the environment variable 'pathext'. If this is undefined, IPython
254 # defaults to EXE, COM and BAT.
254 # defaults to EXE, COM and BAT.
255 # %rehashx is the one which does extension analysis, at the cost of
255 # %rehashx is the one which does extension analysis, at the cost of
256 # being much slower than %rehash.
256 # being much slower than %rehash.
257 __IPYTHON__.magic_rehashx()
257 __IPYTHON__.magic_rehashx()
258
258
259 # Remove %sc,%sx if present as aliases
259 # Remove %sc,%sx if present as aliases
260 __IPYTHON__.magic_unalias('sc')
260 __IPYTHON__.magic_unalias('sc')
261 __IPYTHON__.magic_unalias('sx')
261 __IPYTHON__.magic_unalias('sx')
262
262
263 # We need different criteria for line-splitting, so that aliases such as
263 # We need different criteria for line-splitting, so that aliases such as
264 # 'gnome-terminal' are interpreted as a single alias instead of variable
264 # 'gnome-terminal' are interpreted as a single alias instead of variable
265 # 'gnome' minus variable 'terminal'.
265 # 'gnome' minus variable 'terminal'.
266 import re
266 import re
267 __IPYTHON__.line_split = re.compile(r'^([\s*,;/])'
267 __IPYTHON__.line_split = re.compile(r'^([\s*,;/])'
268 r'([\?\w\.\-\+]+\w*\s*)'
268 r'([\?\w\.\-\+]+\w*\s*)'
269 r'(\(?.*$)')
269 r'(\(?.*$)')
270
270
271 # Namespace cleanup
271 # Namespace cleanup
272 del re
272 del re
@@ -1,91 +1,91 b''
1 # -*- coding: utf-8 -*-
1 # -*- coding: utf-8 -*-
2 """Modified input prompt for entering text with >>> or ... at the start.
2 """Modified input prompt for entering text with >>> or ... at the start.
3
3
4 We define a special input line filter to allow typing lines which begin with
4 We define a special input line filter to allow typing lines which begin with
5 '>>> ' or '... '. These two strings, if present at the start of the input
5 '>>> ' or '... '. These two strings, if present at the start of the input
6 line, are stripped. This allows for direct pasting of code from examples such
6 line, are stripped. This allows for direct pasting of code from examples such
7 as those available in the standard Python tutorial.
7 as those available in the standard Python tutorial.
8
8
9 Normally pasting such code is one chunk is impossible because of the
9 Normally pasting such code is one chunk is impossible because of the
10 extraneous >>> and ..., requiring one to do a line by line paste with careful
10 extraneous >>> and ..., requiring one to do a line by line paste with careful
11 removal of those characters. This module allows pasting that kind of
11 removal of those characters. This module allows pasting that kind of
12 multi-line examples in one pass.
12 multi-line examples in one pass.
13
13
14 Here is an 'screenshot' of a section of the tutorial pasted into IPython with
14 Here is an 'screenshot' of a section of the tutorial pasted into IPython with
15 this feature enabled:
15 this feature enabled:
16
16
17 In [1]: >>> def fib2(n): # return Fibonacci series up to n
17 In [1]: >>> def fib2(n): # return Fibonacci series up to n
18 ...: ... '''Return a list containing the Fibonacci series up to n.'''
18 ...: ... '''Return a list containing the Fibonacci series up to n.'''
19 ...: ... result = []
19 ...: ... result = []
20 ...: ... a, b = 0, 1
20 ...: ... a, b = 0, 1
21 ...: ... while b < n:
21 ...: ... while b < n:
22 ...: ... result.append(b) # see below
22 ...: ... result.append(b) # see below
23 ...: ... a, b = b, a+b
23 ...: ... a, b = b, a+b
24 ...: ... return result
24 ...: ... return result
25 ...:
25 ...:
26
26
27 In [2]: fib2(10)
27 In [2]: fib2(10)
28 Out[2]: [1, 1, 2, 3, 5, 8]
28 Out[2]: [1, 1, 2, 3, 5, 8]
29
29
30 The >>> and ... are stripped from the input so that the python interpreter
30 The >>> and ... are stripped from the input so that the python interpreter
31 only sees the real part of the code.
31 only sees the real part of the code.
32
32
33 All other input is processed normally.
33 All other input is processed normally.
34 """
34 """
35 #*****************************************************************************
35 #*****************************************************************************
36 # Copyright (C) 2001-2004 Fernando Perez <fperez@colorado.edu>
36 # Copyright (C) 2001-2006 Fernando Perez <fperez@colorado.edu>
37 #
37 #
38 # Distributed under the terms of the BSD License. The full license is in
38 # Distributed under the terms of the BSD License. The full license is in
39 # the file COPYING, distributed as part of this software.
39 # the file COPYING, distributed as part of this software.
40 #*****************************************************************************
40 #*****************************************************************************
41
41
42 from IPython import Release
42 from IPython import Release
43 __author__ = '%s <%s>' % Release.authors['Fernando']
43 __author__ = '%s <%s>' % Release.authors['Fernando']
44 __license__ = Release.license
44 __license__ = Release.license
45
45
46 # This file is an example of how to modify IPython's line-processing behavior
46 # This file is an example of how to modify IPython's line-processing behavior
47 # without touching the internal code. We'll define an alternate pre-processing
47 # without touching the internal code. We'll define an alternate pre-processing
48 # stage which allows a special form of input (which is invalid Python syntax)
48 # stage which allows a special form of input (which is invalid Python syntax)
49 # for certain quantities, rewrites a line of proper Python in those cases, and
49 # for certain quantities, rewrites a line of proper Python in those cases, and
50 # then passes it off to IPython's normal processor for further work.
50 # then passes it off to IPython's normal processor for further work.
51
51
52 # With this kind of customization, IPython can be adapted for many
52 # With this kind of customization, IPython can be adapted for many
53 # special-purpose scenarios providing alternate input syntaxes.
53 # special-purpose scenarios providing alternate input syntaxes.
54
54
55 # This file can be imported like a regular module.
55 # This file can be imported like a regular module.
56
56
57 # IPython has a prefilter() function that analyzes each input line. We redefine
57 # IPython has a prefilter() function that analyzes each input line. We redefine
58 # it here to first pre-process certain forms of input
58 # it here to first pre-process certain forms of input
59
59
60 # The prototype of any alternate prefilter must be like this one (the name
60 # The prototype of any alternate prefilter must be like this one (the name
61 # doesn't matter):
61 # doesn't matter):
62 # - line is a string containing the user input line.
62 # - line is a string containing the user input line.
63 # - continuation is a parameter which tells us if we are processing a first line of
63 # - continuation is a parameter which tells us if we are processing a first line of
64 # user input or the second or higher of a multi-line statement.
64 # user input or the second or higher of a multi-line statement.
65
65
66 def prefilter_paste(self,line,continuation):
66 def prefilter_paste(self,line,continuation):
67 """Alternate prefilter for input of pasted code from an interpreter.
67 """Alternate prefilter for input of pasted code from an interpreter.
68 """
68 """
69
69
70 from re import match
70 from re import match
71
71
72 if match(r'^>>> |^\.\.\. ',line):
72 if match(r'^>>> |^\.\.\. ',line):
73 # In the end, always call the default IPython _prefilter() function.
73 # In the end, always call the default IPython _prefilter() function.
74 # Note that self must be passed explicitly, b/c we're calling the
74 # Note that self must be passed explicitly, b/c we're calling the
75 # unbound class method (since this method will overwrite the instance
75 # unbound class method (since this method will overwrite the instance
76 # prefilter())
76 # prefilter())
77 return self._prefilter(line[4:],continuation)
77 return self._prefilter(line[4:],continuation)
78 elif line.strip() == '...':
78 elif line.strip() == '...':
79 return self._prefilter('',continuation)
79 return self._prefilter('',continuation)
80 else:
80 else:
81 return self._prefilter(line,continuation)
81 return self._prefilter(line,continuation)
82
82
83 # Rebind this to be the new IPython prefilter:
83 # Rebind this to be the new IPython prefilter:
84 from IPython.iplib import InteractiveShell
84 from IPython.iplib import InteractiveShell
85 InteractiveShell.prefilter = prefilter_paste
85 InteractiveShell.prefilter = prefilter_paste
86
86
87 # Clean up the namespace.
87 # Clean up the namespace.
88 del InteractiveShell,prefilter_paste
88 del InteractiveShell,prefilter_paste
89
89
90 # Just a heads up at the console
90 # Just a heads up at the console
91 print '*** Pasting of code with ">>>" or "..." has been enabled.'
91 print '*** Pasting of code with ">>>" or "..." has been enabled.'
@@ -1,233 +1,233 b''
1 # -*- coding: utf-8 -*-
1 # -*- coding: utf-8 -*-
2 """
2 """
3 Logger class for IPython's logging facilities.
3 Logger class for IPython's logging facilities.
4
4
5 $Id: Logger.py 988 2006-01-02 21:21:47Z fperez $
5 $Id: Logger.py 994 2006-01-08 08:29:44Z fperez $
6 """
6 """
7
7
8 #*****************************************************************************
8 #*****************************************************************************
9 # Copyright (C) 2001 Janko Hauser <jhauser@zscout.de> and
9 # Copyright (C) 2001 Janko Hauser <jhauser@zscout.de> and
10 # Copyright (C) 2001-2005 Fernando Perez <fperez@colorado.edu>
10 # Copyright (C) 2001-2006 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 #****************************************************************************
16 #****************************************************************************
17 # Modules and globals
17 # Modules and globals
18
18
19 from IPython import Release
19 from IPython import Release
20 __author__ = '%s <%s>\n%s <%s>' % \
20 __author__ = '%s <%s>\n%s <%s>' % \
21 ( Release.authors['Janko'] + Release.authors['Fernando'] )
21 ( Release.authors['Janko'] + Release.authors['Fernando'] )
22 __license__ = Release.license
22 __license__ = Release.license
23
23
24 # Python standard modules
24 # Python standard modules
25 import glob
25 import glob
26 import os
26 import os
27 import time
27 import time
28
28
29 #****************************************************************************
29 #****************************************************************************
30 # FIXME: This class isn't a mixin anymore, but it still needs attributes from
30 # FIXME: This class isn't a mixin anymore, but it still needs attributes from
31 # ipython and does input cache management. Finish cleanup later...
31 # ipython and does input cache management. Finish cleanup later...
32
32
33 class Logger(object):
33 class Logger(object):
34 """A Logfile class with different policies for file creation"""
34 """A Logfile class with different policies for file creation"""
35
35
36 def __init__(self,shell,logfname='Logger.log',loghead='',logmode='over'):
36 def __init__(self,shell,logfname='Logger.log',loghead='',logmode='over'):
37
37
38 self._i00,self._i,self._ii,self._iii = '','','',''
38 self._i00,self._i,self._ii,self._iii = '','','',''
39
39
40 # this is the full ipython instance, we need some attributes from it
40 # this is the full ipython instance, we need some attributes from it
41 # which won't exist until later. What a mess, clean up later...
41 # which won't exist until later. What a mess, clean up later...
42 self.shell = shell
42 self.shell = shell
43
43
44 self.logfname = logfname
44 self.logfname = logfname
45 self.loghead = loghead
45 self.loghead = loghead
46 self.logmode = logmode
46 self.logmode = logmode
47 self.logfile = None
47 self.logfile = None
48
48
49 # whether to also log output
49 # whether to also log output
50 self.log_output = False
50 self.log_output = False
51
51
52 # whether to put timestamps before each log entry
52 # whether to put timestamps before each log entry
53 self.timestamp = False
53 self.timestamp = False
54
54
55 # activity control flags
55 # activity control flags
56 self.log_active = False
56 self.log_active = False
57
57
58 # logmode is a validated property
58 # logmode is a validated property
59 def _set_mode(self,mode):
59 def _set_mode(self,mode):
60 if mode not in ['append','backup','global','over','rotate']:
60 if mode not in ['append','backup','global','over','rotate']:
61 raise ValueError,'invalid log mode %s given' % mode
61 raise ValueError,'invalid log mode %s given' % mode
62 self._logmode = mode
62 self._logmode = mode
63
63
64 def _get_mode(self):
64 def _get_mode(self):
65 return self._logmode
65 return self._logmode
66
66
67 logmode = property(_get_mode,_set_mode)
67 logmode = property(_get_mode,_set_mode)
68
68
69 def logstart(self,logfname=None,loghead=None,logmode=None,
69 def logstart(self,logfname=None,loghead=None,logmode=None,
70 log_output=False,timestamp=False):
70 log_output=False,timestamp=False):
71 """Generate a new log-file with a default header.
71 """Generate a new log-file with a default header.
72
72
73 Raises RuntimeError if the log has already been started"""
73 Raises RuntimeError if the log has already been started"""
74
74
75 if self.logfile is not None:
75 if self.logfile is not None:
76 raise RuntimeError('Log file is already active: %s' %
76 raise RuntimeError('Log file is already active: %s' %
77 self.logfname)
77 self.logfname)
78
78
79 self.log_active = True
79 self.log_active = True
80
80
81 # The three parameters can override constructor defaults
81 # The three parameters can override constructor defaults
82 if logfname: self.logfname = logfname
82 if logfname: self.logfname = logfname
83 if loghead: self.loghead = loghead
83 if loghead: self.loghead = loghead
84 if logmode: self.logmode = logmode
84 if logmode: self.logmode = logmode
85 self.timestamp = timestamp
85 self.timestamp = timestamp
86 self.log_output = log_output
86 self.log_output = log_output
87
87
88 # init depending on the log mode requested
88 # init depending on the log mode requested
89 isfile = os.path.isfile
89 isfile = os.path.isfile
90 logmode = self.logmode
90 logmode = self.logmode
91
91
92 if logmode == 'append':
92 if logmode == 'append':
93 self.logfile = open(self.logfname,'a')
93 self.logfile = open(self.logfname,'a')
94
94
95 elif logmode == 'backup':
95 elif logmode == 'backup':
96 if isfile(self.logfname):
96 if isfile(self.logfname):
97 backup_logname = self.logfname+'~'
97 backup_logname = self.logfname+'~'
98 # Manually remove any old backup, since os.rename may fail
98 # Manually remove any old backup, since os.rename may fail
99 # under Windows.
99 # under Windows.
100 if isfile(backup_logname):
100 if isfile(backup_logname):
101 os.remove(backup_logname)
101 os.remove(backup_logname)
102 os.rename(self.logfname,backup_logname)
102 os.rename(self.logfname,backup_logname)
103 self.logfile = open(self.logfname,'w')
103 self.logfile = open(self.logfname,'w')
104
104
105 elif logmode == 'global':
105 elif logmode == 'global':
106 self.logfname = os.path.join(self.shell.home_dir,self.logfname)
106 self.logfname = os.path.join(self.shell.home_dir,self.logfname)
107 self.logfile = open(self.logfname, 'a')
107 self.logfile = open(self.logfname, 'a')
108
108
109 elif logmode == 'over':
109 elif logmode == 'over':
110 if isfile(self.logfname):
110 if isfile(self.logfname):
111 os.remove(self.logfname)
111 os.remove(self.logfname)
112 self.logfile = open(self.logfname,'w')
112 self.logfile = open(self.logfname,'w')
113
113
114 elif logmode == 'rotate':
114 elif logmode == 'rotate':
115 if isfile(self.logfname):
115 if isfile(self.logfname):
116 if isfile(self.logfname+'.001~'):
116 if isfile(self.logfname+'.001~'):
117 old = glob.glob(self.logfname+'.*~')
117 old = glob.glob(self.logfname+'.*~')
118 old.sort()
118 old.sort()
119 old.reverse()
119 old.reverse()
120 for f in old:
120 for f in old:
121 root, ext = os.path.splitext(f)
121 root, ext = os.path.splitext(f)
122 num = int(ext[1:-1])+1
122 num = int(ext[1:-1])+1
123 os.rename(f, root+'.'+`num`.zfill(3)+'~')
123 os.rename(f, root+'.'+`num`.zfill(3)+'~')
124 os.rename(self.logfname, self.logfname+'.001~')
124 os.rename(self.logfname, self.logfname+'.001~')
125 self.logfile = open(self.logfname,'w')
125 self.logfile = open(self.logfname,'w')
126
126
127 if logmode != 'append':
127 if logmode != 'append':
128 self.logfile.write(self.loghead)
128 self.logfile.write(self.loghead)
129
129
130 self.logfile.flush()
130 self.logfile.flush()
131
131
132 def switch_log(self,val):
132 def switch_log(self,val):
133 """Switch logging on/off. val should be ONLY a boolean."""
133 """Switch logging on/off. val should be ONLY a boolean."""
134
134
135 if val not in [False,True,0,1]:
135 if val not in [False,True,0,1]:
136 raise ValueError, \
136 raise ValueError, \
137 'Call switch_log ONLY with a boolean argument, not with:',val
137 'Call switch_log ONLY with a boolean argument, not with:',val
138
138
139 label = {0:'OFF',1:'ON',False:'OFF',True:'ON'}
139 label = {0:'OFF',1:'ON',False:'OFF',True:'ON'}
140
140
141 if self.logfile is None:
141 if self.logfile is None:
142 print """
142 print """
143 Logging hasn't been started yet (use logstart for that).
143 Logging hasn't been started yet (use logstart for that).
144
144
145 %logon/%logoff are for temporarily starting and stopping logging for a logfile
145 %logon/%logoff are for temporarily starting and stopping logging for a logfile
146 which already exists. But you must first start the logging process with
146 which already exists. But you must first start the logging process with
147 %logstart (optionally giving a logfile name)."""
147 %logstart (optionally giving a logfile name)."""
148
148
149 else:
149 else:
150 if self.log_active == val:
150 if self.log_active == val:
151 print 'Logging is already',label[val]
151 print 'Logging is already',label[val]
152 else:
152 else:
153 print 'Switching logging',label[val]
153 print 'Switching logging',label[val]
154 self.log_active = not self.log_active
154 self.log_active = not self.log_active
155 self.log_active_out = self.log_active
155 self.log_active_out = self.log_active
156
156
157 def logstate(self):
157 def logstate(self):
158 """Print a status message about the logger."""
158 """Print a status message about the logger."""
159 if self.logfile is None:
159 if self.logfile is None:
160 print 'Logging has not been activated.'
160 print 'Logging has not been activated.'
161 else:
161 else:
162 state = self.log_active and 'active' or 'temporarily suspended'
162 state = self.log_active and 'active' or 'temporarily suspended'
163 print 'Filename :',self.logfname
163 print 'Filename :',self.logfname
164 print 'Mode :',self.logmode
164 print 'Mode :',self.logmode
165 print 'Output logging :',self.log_output
165 print 'Output logging :',self.log_output
166 print 'Timestamping :',self.timestamp
166 print 'Timestamping :',self.timestamp
167 print 'State :',state
167 print 'State :',state
168
168
169 def log(self, line,continuation=None):
169 def log(self, line,continuation=None):
170 """Write the line to a log and create input cache variables _i*."""
170 """Write the line to a log and create input cache variables _i*."""
171
171
172 # update the auto _i tables
172 # update the auto _i tables
173 #print '***logging line',line # dbg
173 #print '***logging line',line # dbg
174 #print '***cache_count', self.shell.outputcache.prompt_count # dbg
174 #print '***cache_count', self.shell.outputcache.prompt_count # dbg
175 try:
175 try:
176 input_hist = self.shell.user_ns['_ih']
176 input_hist = self.shell.user_ns['_ih']
177 except:
177 except:
178 print 'userns:',self.shell.user_ns.keys()
178 print 'userns:',self.shell.user_ns.keys()
179 return
179 return
180
180
181 if not continuation and line:
181 if not continuation and line:
182 self._iii = self._ii
182 self._iii = self._ii
183 self._ii = self._i
183 self._ii = self._i
184 self._i = self._i00
184 self._i = self._i00
185 # put back the final \n of every input line
185 # put back the final \n of every input line
186 self._i00 = line+'\n'
186 self._i00 = line+'\n'
187 #print 'Logging input:<%s>' % line # dbg
187 #print 'Logging input:<%s>' % line # dbg
188 input_hist.append(self._i00)
188 input_hist.append(self._i00)
189 #print '---[%s]' % (len(input_hist)-1,) # dbg
189 #print '---[%s]' % (len(input_hist)-1,) # dbg
190
190
191 # hackish access to top-level namespace to create _i1,_i2... dynamically
191 # hackish access to top-level namespace to create _i1,_i2... dynamically
192 to_main = {'_i':self._i,'_ii':self._ii,'_iii':self._iii}
192 to_main = {'_i':self._i,'_ii':self._ii,'_iii':self._iii}
193 if self.shell.outputcache.do_full_cache:
193 if self.shell.outputcache.do_full_cache:
194 in_num = self.shell.outputcache.prompt_count
194 in_num = self.shell.outputcache.prompt_count
195 # add blank lines if the input cache fell out of sync. This can
195 # add blank lines if the input cache fell out of sync. This can
196 # happen for embedded instances which get killed via C-D and then
196 # happen for embedded instances which get killed via C-D and then
197 # get resumed.
197 # get resumed.
198 while in_num >= len(input_hist):
198 while in_num >= len(input_hist):
199 input_hist.append('\n')
199 input_hist.append('\n')
200 # but if the opposite is true (a macro can produce multiple inputs
200 # but if the opposite is true (a macro can produce multiple inputs
201 # with no output display called), then bring the output counter in
201 # with no output display called), then bring the output counter in
202 # sync:
202 # sync:
203 last_num = len(input_hist)-1
203 last_num = len(input_hist)-1
204 if in_num != last_num:
204 if in_num != last_num:
205 in_num = self.shell.outputcache.prompt_count = last_num
205 in_num = self.shell.outputcache.prompt_count = last_num
206 new_i = '_i%s' % in_num
206 new_i = '_i%s' % in_num
207 if continuation:
207 if continuation:
208 self._i00 = '%s%s\n' % (self.shell.user_ns[new_i],line)
208 self._i00 = '%s%s\n' % (self.shell.user_ns[new_i],line)
209 input_hist[in_num] = self._i00
209 input_hist[in_num] = self._i00
210 to_main[new_i] = self._i00
210 to_main[new_i] = self._i00
211 self.shell.user_ns.update(to_main)
211 self.shell.user_ns.update(to_main)
212 self.log_write(line)
212 self.log_write(line)
213
213
214 def log_write(self,data,kind='input'):
214 def log_write(self,data,kind='input'):
215 """Write data to the log file, if active"""
215 """Write data to the log file, if active"""
216
216
217 if self.log_active and data:
217 if self.log_active and data:
218 write = self.logfile.write
218 write = self.logfile.write
219 if kind=='input':
219 if kind=='input':
220 if self.timestamp:
220 if self.timestamp:
221 write(time.strftime('# %a, %d %b %Y %H:%M:%S\n',
221 write(time.strftime('# %a, %d %b %Y %H:%M:%S\n',
222 time.localtime()))
222 time.localtime()))
223 write('%s\n' % data)
223 write('%s\n' % data)
224 elif kind=='output' and self.log_output:
224 elif kind=='output' and self.log_output:
225 odata = '\n'.join(['#[Out]# %s' % s
225 odata = '\n'.join(['#[Out]# %s' % s
226 for s in data.split('\n')])
226 for s in data.split('\n')])
227 write('%s\n' % odata)
227 write('%s\n' % odata)
228 self.logfile.flush()
228 self.logfile.flush()
229
229
230 def close_log(self):
230 def close_log(self):
231 self.logfile.close()
231 self.logfile.close()
232 self.logfile = None
232 self.logfile = None
233 self.logfname = ''
233 self.logfname = ''
@@ -1,2707 +1,2736 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 991 2006-01-04 18:15:34Z fperez $"""
4 $Id: Magic.py 994 2006-01-08 08:29:44Z 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-2006 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 bdb
24 import bdb
25 import inspect
25 import inspect
26 import os
26 import os
27 import pdb
27 import pdb
28 import pydoc
28 import pydoc
29 import sys
29 import sys
30 import re
30 import re
31 import tempfile
31 import tempfile
32 import time
32 import time
33 import cPickle as pickle
33 import cPickle as pickle
34 from cStringIO import StringIO
34 from cStringIO import StringIO
35 from getopt import getopt
35 from getopt import getopt
36 from pprint import pprint, pformat
36 from pprint import pprint, pformat
37
37
38 # profile isn't bundled by default in Debian for license reasons
38 # profile isn't bundled by default in Debian for license reasons
39 try:
39 try:
40 import profile,pstats
40 import profile,pstats
41 except ImportError:
41 except ImportError:
42 profile = pstats = None
42 profile = pstats = None
43
43
44 # Homebrewed
44 # Homebrewed
45 from IPython import Debugger, OInspect, wildcard
45 from IPython import Debugger, OInspect, wildcard
46 from IPython.FakeModule import FakeModule
46 from IPython.FakeModule import FakeModule
47 from IPython.Itpl import Itpl, itpl, printpl,itplns
47 from IPython.Itpl import Itpl, itpl, printpl,itplns
48 from IPython.PyColorize import Parser
48 from IPython.PyColorize import Parser
49 from IPython.Struct import Struct
49 from IPython.Struct import Struct
50 from IPython.macro import Macro
50 from IPython.macro import Macro
51 from IPython.genutils import *
51 from IPython.genutils import *
52
52
53 #***************************************************************************
53 #***************************************************************************
54 # Utility functions
54 # Utility functions
55 def on_off(tag):
55 def on_off(tag):
56 """Return an ON/OFF string for a 1/0 input. Simple utility function."""
56 """Return an ON/OFF string for a 1/0 input. Simple utility function."""
57 return ['OFF','ON'][tag]
57 return ['OFF','ON'][tag]
58
58
59 class Bunch: pass
59
60
60 #***************************************************************************
61 #***************************************************************************
61 # Main class implementing Magic functionality
62 # Main class implementing Magic functionality
62 class Magic:
63 class Magic:
63 """Magic functions for InteractiveShell.
64 """Magic functions for InteractiveShell.
64
65
65 Shell functions which can be reached as %function_name. All magic
66 Shell functions which can be reached as %function_name. All magic
66 functions should accept a string, which they can parse for their own
67 functions should accept a string, which they can parse for their own
67 needs. This can make some functions easier to type, eg `%cd ../`
68 needs. This can make some functions easier to type, eg `%cd ../`
68 vs. `%cd("../")`
69 vs. `%cd("../")`
69
70
70 ALL definitions MUST begin with the prefix magic_. The user won't need it
71 ALL definitions MUST begin with the prefix magic_. The user won't need it
71 at the command line, but it is is needed in the definition. """
72 at the command line, but it is is needed in the definition. """
72
73
73 # class globals
74 # class globals
74 auto_status = ['Automagic is OFF, % prefix IS needed for magic functions.',
75 auto_status = ['Automagic is OFF, % prefix IS needed for magic functions.',
75 'Automagic is ON, % prefix NOT needed for magic functions.']
76 'Automagic is ON, % prefix NOT needed for magic functions.']
76
77
77 #......................................................................
78 #......................................................................
78 # some utility functions
79 # some utility functions
79
80
80 def __init__(self,shell):
81 def __init__(self,shell):
81
82
82 self.options_table = {}
83 self.options_table = {}
83 if profile is None:
84 if profile is None:
84 self.magic_prun = self.profile_missing_notice
85 self.magic_prun = self.profile_missing_notice
85 self.shell = shell
86 self.shell = shell
86
87
88 # namespace for holding state we may need
89 self._magic_state = Bunch()
90
87 def profile_missing_notice(self, *args, **kwargs):
91 def profile_missing_notice(self, *args, **kwargs):
88 error("""\
92 error("""\
89 The profile module could not be found. If you are a Debian user,
93 The profile module could not be found. If you are a Debian user,
90 it has been removed from the standard Debian package because of its non-free
94 it has been removed from the standard Debian package because of its non-free
91 license. To use profiling, please install"python2.3-profiler" from non-free.""")
95 license. To use profiling, please install"python2.3-profiler" from non-free.""")
92
96
93 def default_option(self,fn,optstr):
97 def default_option(self,fn,optstr):
94 """Make an entry in the options_table for fn, with value optstr"""
98 """Make an entry in the options_table for fn, with value optstr"""
95
99
96 if fn not in self.lsmagic():
100 if fn not in self.lsmagic():
97 error("%s is not a magic function" % fn)
101 error("%s is not a magic function" % fn)
98 self.options_table[fn] = optstr
102 self.options_table[fn] = optstr
99
103
100 def lsmagic(self):
104 def lsmagic(self):
101 """Return a list of currently available magic functions.
105 """Return a list of currently available magic functions.
102
106
103 Gives a list of the bare names after mangling (['ls','cd', ...], not
107 Gives a list of the bare names after mangling (['ls','cd', ...], not
104 ['magic_ls','magic_cd',...]"""
108 ['magic_ls','magic_cd',...]"""
105
109
106 # FIXME. This needs a cleanup, in the way the magics list is built.
110 # FIXME. This needs a cleanup, in the way the magics list is built.
107
111
108 # magics in class definition
112 # magics in class definition
109 class_magic = lambda fn: fn.startswith('magic_') and \
113 class_magic = lambda fn: fn.startswith('magic_') and \
110 callable(Magic.__dict__[fn])
114 callable(Magic.__dict__[fn])
111 # in instance namespace (run-time user additions)
115 # in instance namespace (run-time user additions)
112 inst_magic = lambda fn: fn.startswith('magic_') and \
116 inst_magic = lambda fn: fn.startswith('magic_') and \
113 callable(self.__dict__[fn])
117 callable(self.__dict__[fn])
114 # and bound magics by user (so they can access self):
118 # and bound magics by user (so they can access self):
115 inst_bound_magic = lambda fn: fn.startswith('magic_') and \
119 inst_bound_magic = lambda fn: fn.startswith('magic_') and \
116 callable(self.__class__.__dict__[fn])
120 callable(self.__class__.__dict__[fn])
117 magics = filter(class_magic,Magic.__dict__.keys()) + \
121 magics = filter(class_magic,Magic.__dict__.keys()) + \
118 filter(inst_magic,self.__dict__.keys()) + \
122 filter(inst_magic,self.__dict__.keys()) + \
119 filter(inst_bound_magic,self.__class__.__dict__.keys())
123 filter(inst_bound_magic,self.__class__.__dict__.keys())
120 out = []
124 out = []
121 for fn in magics:
125 for fn in magics:
122 out.append(fn.replace('magic_','',1))
126 out.append(fn.replace('magic_','',1))
123 out.sort()
127 out.sort()
124 return out
128 return out
125
129
126 def extract_input_slices(self,slices):
130 def extract_input_slices(self,slices):
127 """Return as a string a set of input history slices.
131 """Return as a string a set of input history slices.
128
132
129 The set of slices is given as a list of strings (like ['1','4:8','9'],
133 The set of slices is given as a list of strings (like ['1','4:8','9'],
130 since this function is for use by magic functions which get their
134 since this function is for use by magic functions which get their
131 arguments as strings.
135 arguments as strings.
132
136
133 Note that slices can be called with two notations:
137 Note that slices can be called with two notations:
134
138
135 N:M -> standard python form, means including items N...(M-1).
139 N:M -> standard python form, means including items N...(M-1).
136
140
137 N-M -> include items N..M (closed endpoint)."""
141 N-M -> include items N..M (closed endpoint)."""
138
142
139 cmds = []
143 cmds = []
140 for chunk in slices:
144 for chunk in slices:
141 if ':' in chunk:
145 if ':' in chunk:
142 ini,fin = map(int,chunk.split(':'))
146 ini,fin = map(int,chunk.split(':'))
143 elif '-' in chunk:
147 elif '-' in chunk:
144 ini,fin = map(int,chunk.split('-'))
148 ini,fin = map(int,chunk.split('-'))
145 fin += 1
149 fin += 1
146 else:
150 else:
147 ini = int(chunk)
151 ini = int(chunk)
148 fin = ini+1
152 fin = ini+1
149 cmds.append(self.shell.input_hist[ini:fin])
153 cmds.append(self.shell.input_hist[ini:fin])
150 return cmds
154 return cmds
151
155
152 def _ofind(self,oname):
156 def _ofind(self,oname):
153 """Find an object in the available namespaces.
157 """Find an object in the available namespaces.
154
158
155 self._ofind(oname) -> dict with keys: found,obj,ospace,ismagic
159 self._ofind(oname) -> dict with keys: found,obj,ospace,ismagic
156
160
157 Has special code to detect magic functions.
161 Has special code to detect magic functions.
158 """
162 """
159
163
160 oname = oname.strip()
164 oname = oname.strip()
161
165
162 # Namespaces to search in:
166 # Namespaces to search in:
163 user_ns = self.shell.user_ns
167 user_ns = self.shell.user_ns
164 internal_ns = self.shell.internal_ns
168 internal_ns = self.shell.internal_ns
165 builtin_ns = __builtin__.__dict__
169 builtin_ns = __builtin__.__dict__
166 alias_ns = self.shell.alias_table
170 alias_ns = self.shell.alias_table
167
171
168 # Put them in a list. The order is important so that we find things in
172 # Put them in a list. The order is important so that we find things in
169 # the same order that Python finds them.
173 # the same order that Python finds them.
170 namespaces = [ ('Interactive',user_ns),
174 namespaces = [ ('Interactive',user_ns),
171 ('IPython internal',internal_ns),
175 ('IPython internal',internal_ns),
172 ('Python builtin',builtin_ns),
176 ('Python builtin',builtin_ns),
173 ('Alias',alias_ns),
177 ('Alias',alias_ns),
174 ]
178 ]
175
179
176 # initialize results to 'null'
180 # initialize results to 'null'
177 found = 0; obj = None; ospace = None; ds = None;
181 found = 0; obj = None; ospace = None; ds = None;
178 ismagic = 0; isalias = 0
182 ismagic = 0; isalias = 0
179
183
180 # Look for the given name by splitting it in parts. If the head is
184 # Look for the given name by splitting it in parts. If the head is
181 # found, then we look for all the remaining parts as members, and only
185 # found, then we look for all the remaining parts as members, and only
182 # declare success if we can find them all.
186 # declare success if we can find them all.
183 oname_parts = oname.split('.')
187 oname_parts = oname.split('.')
184 oname_head, oname_rest = oname_parts[0],oname_parts[1:]
188 oname_head, oname_rest = oname_parts[0],oname_parts[1:]
185 for nsname,ns in namespaces:
189 for nsname,ns in namespaces:
186 try:
190 try:
187 obj = ns[oname_head]
191 obj = ns[oname_head]
188 except KeyError:
192 except KeyError:
189 continue
193 continue
190 else:
194 else:
191 for part in oname_rest:
195 for part in oname_rest:
192 try:
196 try:
193 obj = getattr(obj,part)
197 obj = getattr(obj,part)
194 except:
198 except:
195 # Blanket except b/c some badly implemented objects
199 # Blanket except b/c some badly implemented objects
196 # allow __getattr__ to raise exceptions other than
200 # allow __getattr__ to raise exceptions other than
197 # AttributeError, which then crashes IPython.
201 # AttributeError, which then crashes IPython.
198 break
202 break
199 else:
203 else:
200 # If we finish the for loop (no break), we got all members
204 # If we finish the for loop (no break), we got all members
201 found = 1
205 found = 1
202 ospace = nsname
206 ospace = nsname
203 if ns == alias_ns:
207 if ns == alias_ns:
204 isalias = 1
208 isalias = 1
205 break # namespace loop
209 break # namespace loop
206
210
207 # Try to see if it's magic
211 # Try to see if it's magic
208 if not found:
212 if not found:
209 if oname.startswith(self.shell.ESC_MAGIC):
213 if oname.startswith(self.shell.ESC_MAGIC):
210 oname = oname[1:]
214 oname = oname[1:]
211 obj = getattr(self,'magic_'+oname,None)
215 obj = getattr(self,'magic_'+oname,None)
212 if obj is not None:
216 if obj is not None:
213 found = 1
217 found = 1
214 ospace = 'IPython internal'
218 ospace = 'IPython internal'
215 ismagic = 1
219 ismagic = 1
216
220
217 # Last try: special-case some literals like '', [], {}, etc:
221 # Last try: special-case some literals like '', [], {}, etc:
218 if not found and oname_head in ["''",'""','[]','{}','()']:
222 if not found and oname_head in ["''",'""','[]','{}','()']:
219 obj = eval(oname_head)
223 obj = eval(oname_head)
220 found = 1
224 found = 1
221 ospace = 'Interactive'
225 ospace = 'Interactive'
222
226
223 return {'found':found, 'obj':obj, 'namespace':ospace,
227 return {'found':found, 'obj':obj, 'namespace':ospace,
224 'ismagic':ismagic, 'isalias':isalias}
228 'ismagic':ismagic, 'isalias':isalias}
225
229
226 def arg_err(self,func):
230 def arg_err(self,func):
227 """Print docstring if incorrect arguments were passed"""
231 """Print docstring if incorrect arguments were passed"""
228 print 'Error in arguments:'
232 print 'Error in arguments:'
229 print OInspect.getdoc(func)
233 print OInspect.getdoc(func)
230
234
231 def format_latex(self,strng):
235 def format_latex(self,strng):
232 """Format a string for latex inclusion."""
236 """Format a string for latex inclusion."""
233
237
234 # Characters that need to be escaped for latex:
238 # Characters that need to be escaped for latex:
235 escape_re = re.compile(r'(%|_|\$|#)',re.MULTILINE)
239 escape_re = re.compile(r'(%|_|\$|#)',re.MULTILINE)
236 # Magic command names as headers:
240 # Magic command names as headers:
237 cmd_name_re = re.compile(r'^(%s.*?):' % self.shell.ESC_MAGIC,
241 cmd_name_re = re.compile(r'^(%s.*?):' % self.shell.ESC_MAGIC,
238 re.MULTILINE)
242 re.MULTILINE)
239 # Magic commands
243 # Magic commands
240 cmd_re = re.compile(r'(?P<cmd>%s.+?\b)(?!\}\}:)' % self.shell.ESC_MAGIC,
244 cmd_re = re.compile(r'(?P<cmd>%s.+?\b)(?!\}\}:)' % self.shell.ESC_MAGIC,
241 re.MULTILINE)
245 re.MULTILINE)
242 # Paragraph continue
246 # Paragraph continue
243 par_re = re.compile(r'\\$',re.MULTILINE)
247 par_re = re.compile(r'\\$',re.MULTILINE)
244
248
245 # The "\n" symbol
249 # The "\n" symbol
246 newline_re = re.compile(r'\\n')
250 newline_re = re.compile(r'\\n')
247
251
248 # Now build the string for output:
252 # Now build the string for output:
249 strng = cmd_name_re.sub(r'\n\\texttt{\\textsl{\\large \1}}:',strng)
253 strng = cmd_name_re.sub(r'\n\\texttt{\\textsl{\\large \1}}:',strng)
250 strng = cmd_re.sub(r'\\texttt{\g<cmd>}',strng)
254 strng = cmd_re.sub(r'\\texttt{\g<cmd>}',strng)
251 strng = par_re.sub(r'\\\\',strng)
255 strng = par_re.sub(r'\\\\',strng)
252 strng = escape_re.sub(r'\\\1',strng)
256 strng = escape_re.sub(r'\\\1',strng)
253 strng = newline_re.sub(r'\\textbackslash{}n',strng)
257 strng = newline_re.sub(r'\\textbackslash{}n',strng)
254 return strng
258 return strng
255
259
256 def format_screen(self,strng):
260 def format_screen(self,strng):
257 """Format a string for screen printing.
261 """Format a string for screen printing.
258
262
259 This removes some latex-type format codes."""
263 This removes some latex-type format codes."""
260 # Paragraph continue
264 # Paragraph continue
261 par_re = re.compile(r'\\$',re.MULTILINE)
265 par_re = re.compile(r'\\$',re.MULTILINE)
262 strng = par_re.sub('',strng)
266 strng = par_re.sub('',strng)
263 return strng
267 return strng
264
268
265 def parse_options(self,arg_str,opt_str,*long_opts,**kw):
269 def parse_options(self,arg_str,opt_str,*long_opts,**kw):
266 """Parse options passed to an argument string.
270 """Parse options passed to an argument string.
267
271
268 The interface is similar to that of getopt(), but it returns back a
272 The interface is similar to that of getopt(), but it returns back a
269 Struct with the options as keys and the stripped argument string still
273 Struct with the options as keys and the stripped argument string still
270 as a string.
274 as a string.
271
275
272 arg_str is quoted as a true sys.argv vector by using shlex.split.
276 arg_str is quoted as a true sys.argv vector by using shlex.split.
273 This allows us to easily expand variables, glob files, quote
277 This allows us to easily expand variables, glob files, quote
274 arguments, etc.
278 arguments, etc.
275
279
276 Options:
280 Options:
277 -mode: default 'string'. If given as 'list', the argument string is
281 -mode: default 'string'. If given as 'list', the argument string is
278 returned as a list (split on whitespace) instead of a string.
282 returned as a list (split on whitespace) instead of a string.
279
283
280 -list_all: put all option values in lists. Normally only options
284 -list_all: put all option values in lists. Normally only options
281 appearing more than once are put in a list."""
285 appearing more than once are put in a list."""
282
286
283 # inject default options at the beginning of the input line
287 # inject default options at the beginning of the input line
284 caller = sys._getframe(1).f_code.co_name.replace('magic_','')
288 caller = sys._getframe(1).f_code.co_name.replace('magic_','')
285 arg_str = '%s %s' % (self.options_table.get(caller,''),arg_str)
289 arg_str = '%s %s' % (self.options_table.get(caller,''),arg_str)
286
290
287 mode = kw.get('mode','string')
291 mode = kw.get('mode','string')
288 if mode not in ['string','list']:
292 if mode not in ['string','list']:
289 raise ValueError,'incorrect mode given: %s' % mode
293 raise ValueError,'incorrect mode given: %s' % mode
290 # Get options
294 # Get options
291 list_all = kw.get('list_all',0)
295 list_all = kw.get('list_all',0)
292
296
293 # Check if we have more than one argument to warrant extra processing:
297 # Check if we have more than one argument to warrant extra processing:
294 odict = {} # Dictionary with options
298 odict = {} # Dictionary with options
295 args = arg_str.split()
299 args = arg_str.split()
296 if len(args) >= 1:
300 if len(args) >= 1:
297 # If the list of inputs only has 0 or 1 thing in it, there's no
301 # If the list of inputs only has 0 or 1 thing in it, there's no
298 # need to look for options
302 # need to look for options
299 argv = shlex_split(arg_str)
303 argv = shlex_split(arg_str)
300 # Do regular option processing
304 # Do regular option processing
301 opts,args = getopt(argv,opt_str,*long_opts)
305 opts,args = getopt(argv,opt_str,*long_opts)
302 for o,a in opts:
306 for o,a in opts:
303 if o.startswith('--'):
307 if o.startswith('--'):
304 o = o[2:]
308 o = o[2:]
305 else:
309 else:
306 o = o[1:]
310 o = o[1:]
307 try:
311 try:
308 odict[o].append(a)
312 odict[o].append(a)
309 except AttributeError:
313 except AttributeError:
310 odict[o] = [odict[o],a]
314 odict[o] = [odict[o],a]
311 except KeyError:
315 except KeyError:
312 if list_all:
316 if list_all:
313 odict[o] = [a]
317 odict[o] = [a]
314 else:
318 else:
315 odict[o] = a
319 odict[o] = a
316
320
317 # Prepare opts,args for return
321 # Prepare opts,args for return
318 opts = Struct(odict)
322 opts = Struct(odict)
319 if mode == 'string':
323 if mode == 'string':
320 args = ' '.join(args)
324 args = ' '.join(args)
321
325
322 return opts,args
326 return opts,args
323
327
324 #......................................................................
328 #......................................................................
325 # And now the actual magic functions
329 # And now the actual magic functions
326
330
327 # Functions for IPython shell work (vars,funcs, config, etc)
331 # Functions for IPython shell work (vars,funcs, config, etc)
328 def magic_lsmagic(self, parameter_s = ''):
332 def magic_lsmagic(self, parameter_s = ''):
329 """List currently available magic functions."""
333 """List currently available magic functions."""
330 mesc = self.shell.ESC_MAGIC
334 mesc = self.shell.ESC_MAGIC
331 print 'Available magic functions:\n'+mesc+\
335 print 'Available magic functions:\n'+mesc+\
332 (' '+mesc).join(self.lsmagic())
336 (' '+mesc).join(self.lsmagic())
333 print '\n' + Magic.auto_status[self.shell.rc.automagic]
337 print '\n' + Magic.auto_status[self.shell.rc.automagic]
334 return None
338 return None
335
339
336 def magic_magic(self, parameter_s = ''):
340 def magic_magic(self, parameter_s = ''):
337 """Print information about the magic function system."""
341 """Print information about the magic function system."""
338
342
339 mode = ''
343 mode = ''
340 try:
344 try:
341 if parameter_s.split()[0] == '-latex':
345 if parameter_s.split()[0] == '-latex':
342 mode = 'latex'
346 mode = 'latex'
343 except:
347 except:
344 pass
348 pass
345
349
346 magic_docs = []
350 magic_docs = []
347 for fname in self.lsmagic():
351 for fname in self.lsmagic():
348 mname = 'magic_' + fname
352 mname = 'magic_' + fname
349 for space in (Magic,self,self.__class__):
353 for space in (Magic,self,self.__class__):
350 try:
354 try:
351 fn = space.__dict__[mname]
355 fn = space.__dict__[mname]
352 except KeyError:
356 except KeyError:
353 pass
357 pass
354 else:
358 else:
355 break
359 break
356 magic_docs.append('%s%s:\n\t%s\n' %(self.shell.ESC_MAGIC,
360 magic_docs.append('%s%s:\n\t%s\n' %(self.shell.ESC_MAGIC,
357 fname,fn.__doc__))
361 fname,fn.__doc__))
358 magic_docs = ''.join(magic_docs)
362 magic_docs = ''.join(magic_docs)
359
363
360 if mode == 'latex':
364 if mode == 'latex':
361 print self.format_latex(magic_docs)
365 print self.format_latex(magic_docs)
362 return
366 return
363 else:
367 else:
364 magic_docs = self.format_screen(magic_docs)
368 magic_docs = self.format_screen(magic_docs)
365
369
366 outmsg = """
370 outmsg = """
367 IPython's 'magic' functions
371 IPython's 'magic' functions
368 ===========================
372 ===========================
369
373
370 The magic function system provides a series of functions which allow you to
374 The magic function system provides a series of functions which allow you to
371 control the behavior of IPython itself, plus a lot of system-type
375 control the behavior of IPython itself, plus a lot of system-type
372 features. All these functions are prefixed with a % character, but parameters
376 features. All these functions are prefixed with a % character, but parameters
373 are given without parentheses or quotes.
377 are given without parentheses or quotes.
374
378
375 NOTE: If you have 'automagic' enabled (via the command line option or with the
379 NOTE: If you have 'automagic' enabled (via the command line option or with the
376 %automagic function), you don't need to type in the % explicitly. By default,
380 %automagic function), you don't need to type in the % explicitly. By default,
377 IPython ships with automagic on, so you should only rarely need the % escape.
381 IPython ships with automagic on, so you should only rarely need the % escape.
378
382
379 Example: typing '%cd mydir' (without the quotes) changes you working directory
383 Example: typing '%cd mydir' (without the quotes) changes you working directory
380 to 'mydir', if it exists.
384 to 'mydir', if it exists.
381
385
382 You can define your own magic functions to extend the system. See the supplied
386 You can define your own magic functions to extend the system. See the supplied
383 ipythonrc and example-magic.py files for details (in your ipython
387 ipythonrc and example-magic.py files for details (in your ipython
384 configuration directory, typically $HOME/.ipython/).
388 configuration directory, typically $HOME/.ipython/).
385
389
386 You can also define your own aliased names for magic functions. In your
390 You can also define your own aliased names for magic functions. In your
387 ipythonrc file, placing a line like:
391 ipythonrc file, placing a line like:
388
392
389 execute __IPYTHON__.magic_pf = __IPYTHON__.magic_profile
393 execute __IPYTHON__.magic_pf = __IPYTHON__.magic_profile
390
394
391 will define %pf as a new name for %profile.
395 will define %pf as a new name for %profile.
392
396
393 You can also call magics in code using the ipmagic() function, which IPython
397 You can also call magics in code using the ipmagic() function, which IPython
394 automatically adds to the builtin namespace. Type 'ipmagic?' for details.
398 automatically adds to the builtin namespace. Type 'ipmagic?' for details.
395
399
396 For a list of the available magic functions, use %lsmagic. For a description
400 For a list of the available magic functions, use %lsmagic. For a description
397 of any of them, type %magic_name?, e.g. '%cd?'.
401 of any of them, type %magic_name?, e.g. '%cd?'.
398
402
399 Currently the magic system has the following functions:\n"""
403 Currently the magic system has the following functions:\n"""
400
404
401 mesc = self.shell.ESC_MAGIC
405 mesc = self.shell.ESC_MAGIC
402 outmsg = ("%s\n%s\n\nSummary of magic functions (from %slsmagic):"
406 outmsg = ("%s\n%s\n\nSummary of magic functions (from %slsmagic):"
403 "\n\n%s%s\n\n%s" % (outmsg,
407 "\n\n%s%s\n\n%s" % (outmsg,
404 magic_docs,mesc,mesc,
408 magic_docs,mesc,mesc,
405 (' '+mesc).join(self.lsmagic()),
409 (' '+mesc).join(self.lsmagic()),
406 Magic.auto_status[self.shell.rc.automagic] ) )
410 Magic.auto_status[self.shell.rc.automagic] ) )
407
411
408 page(outmsg,screen_lines=self.shell.rc.screen_length)
412 page(outmsg,screen_lines=self.shell.rc.screen_length)
409
413
410 def magic_automagic(self, parameter_s = ''):
414 def magic_automagic(self, parameter_s = ''):
411 """Make magic functions callable without having to type the initial %.
415 """Make magic functions callable without having to type the initial %.
412
416
413 Toggles on/off (when off, you must call it as %automagic, of
417 Toggles on/off (when off, you must call it as %automagic, of
414 course). Note that magic functions have lowest priority, so if there's
418 course). Note that magic functions have lowest priority, so if there's
415 a variable whose name collides with that of a magic fn, automagic
419 a variable whose name collides with that of a magic fn, automagic
416 won't work for that function (you get the variable instead). However,
420 won't work for that function (you get the variable instead). However,
417 if you delete the variable (del var), the previously shadowed magic
421 if you delete the variable (del var), the previously shadowed magic
418 function becomes visible to automagic again."""
422 function becomes visible to automagic again."""
419
423
420 rc = self.shell.rc
424 rc = self.shell.rc
421 rc.automagic = not rc.automagic
425 rc.automagic = not rc.automagic
422 print '\n' + Magic.auto_status[rc.automagic]
426 print '\n' + Magic.auto_status[rc.automagic]
423
427
424 def magic_autocall(self, parameter_s = ''):
428 def magic_autocall(self, parameter_s = ''):
425 """Make functions callable without having to type parentheses.
429 """Make functions callable without having to type parentheses.
426
430
427 This cycles the autocall command line through its three valid values
431 Usage:
428 (0->Off, 1->Smart, 2->Full)"""
432
433 %autocall [mode]
434
435 The mode can be one of: 0->Off, 1->Smart, 2->Full. If not given, the
436 value is toggled on and off (remembering the previous state)."""
429
437
430 rc = self.shell.rc
438 rc = self.shell.rc
431 rc.autocall = not rc.autocall
439
440 if parameter_s:
441 arg = int(parameter_s)
442 else:
443 arg = 'toggle'
444
445 if not arg in (0,1,2,'toggle'):
446 error('Valid modes: (0->Off, 1->Smart, 2->Full')
447 return
448
449 if arg in (0,1,2):
450 rc.autocall = arg
451 else: # toggle
452 if rc.autocall:
453 self._magic_state.autocall_save = rc.autocall
454 rc.autocall = 0
455 else:
456 try:
457 rc.autocall = self._magic_state.autocall_save
458 except AttributeError:
459 rc.autocall = self._magic_state.autocall_save = 1
460
432 print "Automatic calling is:",['OFF','Smart','Full'][rc.autocall]
461 print "Automatic calling is:",['OFF','Smart','Full'][rc.autocall]
433
462
434 def magic_autoindent(self, parameter_s = ''):
463 def magic_autoindent(self, parameter_s = ''):
435 """Toggle autoindent on/off (if available)."""
464 """Toggle autoindent on/off (if available)."""
436
465
437 self.shell.set_autoindent()
466 self.shell.set_autoindent()
438 print "Automatic indentation is:",['OFF','ON'][self.shell.autoindent]
467 print "Automatic indentation is:",['OFF','ON'][self.shell.autoindent]
439
468
440 def magic_system_verbose(self, parameter_s = ''):
469 def magic_system_verbose(self, parameter_s = ''):
441 """Toggle verbose printing of system calls on/off."""
470 """Toggle verbose printing of system calls on/off."""
442
471
443 self.shell.rc_set_toggle('system_verbose')
472 self.shell.rc_set_toggle('system_verbose')
444 print "System verbose printing is:",\
473 print "System verbose printing is:",\
445 ['OFF','ON'][self.shell.rc.system_verbose]
474 ['OFF','ON'][self.shell.rc.system_verbose]
446
475
447 def magic_history(self, parameter_s = ''):
476 def magic_history(self, parameter_s = ''):
448 """Print input history (_i<n> variables), with most recent last.
477 """Print input history (_i<n> variables), with most recent last.
449
478
450 %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)\\
451 %history [-n] n -> print at most n inputs\\
480 %history [-n] n -> print at most n inputs\\
452 %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)\\
453
482
454 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
455 automatically generated variable _i<n>. Multi-line statements are
484 automatically generated variable _i<n>. Multi-line statements are
456 printed starting at a new line for easy copy/paste.
485 printed starting at a new line for easy copy/paste.
457
486
458 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
459 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
460 into a text editor.
489 into a text editor.
461
490
462 This feature is only available if numbered prompts are in use."""
491 This feature is only available if numbered prompts are in use."""
463
492
464 shell = self.shell
493 shell = self.shell
465 if not shell.outputcache.do_full_cache:
494 if not shell.outputcache.do_full_cache:
466 print 'This feature is only available if numbered prompts are in use.'
495 print 'This feature is only available if numbered prompts are in use.'
467 return
496 return
468 opts,args = self.parse_options(parameter_s,'n',mode='list')
497 opts,args = self.parse_options(parameter_s,'n',mode='list')
469
498
470 input_hist = shell.input_hist
499 input_hist = shell.input_hist
471 default_length = 40
500 default_length = 40
472 if len(args) == 0:
501 if len(args) == 0:
473 final = len(input_hist)
502 final = len(input_hist)
474 init = max(1,final-default_length)
503 init = max(1,final-default_length)
475 elif len(args) == 1:
504 elif len(args) == 1:
476 final = len(input_hist)
505 final = len(input_hist)
477 init = max(1,final-int(args[0]))
506 init = max(1,final-int(args[0]))
478 elif len(args) == 2:
507 elif len(args) == 2:
479 init,final = map(int,args)
508 init,final = map(int,args)
480 else:
509 else:
481 warn('%hist takes 0, 1 or 2 arguments separated by spaces.')
510 warn('%hist takes 0, 1 or 2 arguments separated by spaces.')
482 print self.magic_hist.__doc__
511 print self.magic_hist.__doc__
483 return
512 return
484 width = len(str(final))
513 width = len(str(final))
485 line_sep = ['','\n']
514 line_sep = ['','\n']
486 print_nums = not opts.has_key('n')
515 print_nums = not opts.has_key('n')
487 for in_num in range(init,final):
516 for in_num in range(init,final):
488 inline = input_hist[in_num]
517 inline = input_hist[in_num]
489 multiline = int(inline.count('\n') > 1)
518 multiline = int(inline.count('\n') > 1)
490 if print_nums:
519 if print_nums:
491 print '%s:%s' % (str(in_num).ljust(width),line_sep[multiline]),
520 print '%s:%s' % (str(in_num).ljust(width),line_sep[multiline]),
492 print inline,
521 print inline,
493
522
494 def magic_hist(self, parameter_s=''):
523 def magic_hist(self, parameter_s=''):
495 """Alternate name for %history."""
524 """Alternate name for %history."""
496 return self.magic_history(parameter_s)
525 return self.magic_history(parameter_s)
497
526
498 def magic_p(self, parameter_s=''):
527 def magic_p(self, parameter_s=''):
499 """Just a short alias for Python's 'print'."""
528 """Just a short alias for Python's 'print'."""
500 exec 'print ' + parameter_s in self.shell.user_ns
529 exec 'print ' + parameter_s in self.shell.user_ns
501
530
502 def magic_r(self, parameter_s=''):
531 def magic_r(self, parameter_s=''):
503 """Repeat previous input.
532 """Repeat previous input.
504
533
505 If given an argument, repeats the previous command which starts with
534 If given an argument, repeats the previous command which starts with
506 the same string, otherwise it just repeats the previous input.
535 the same string, otherwise it just repeats the previous input.
507
536
508 Shell escaped commands (with ! as first character) are not recognized
537 Shell escaped commands (with ! as first character) are not recognized
509 by this system, only pure python code and magic commands.
538 by this system, only pure python code and magic commands.
510 """
539 """
511
540
512 start = parameter_s.strip()
541 start = parameter_s.strip()
513 esc_magic = self.shell.ESC_MAGIC
542 esc_magic = self.shell.ESC_MAGIC
514 # Identify magic commands even if automagic is on (which means
543 # Identify magic commands even if automagic is on (which means
515 # the in-memory version is different from that typed by the user).
544 # the in-memory version is different from that typed by the user).
516 if self.shell.rc.automagic:
545 if self.shell.rc.automagic:
517 start_magic = esc_magic+start
546 start_magic = esc_magic+start
518 else:
547 else:
519 start_magic = start
548 start_magic = start
520 # Look through the input history in reverse
549 # Look through the input history in reverse
521 for n in range(len(self.shell.input_hist)-2,0,-1):
550 for n in range(len(self.shell.input_hist)-2,0,-1):
522 input = self.shell.input_hist[n]
551 input = self.shell.input_hist[n]
523 # skip plain 'r' lines so we don't recurse to infinity
552 # skip plain 'r' lines so we don't recurse to infinity
524 if input != 'ipmagic("r")\n' and \
553 if input != 'ipmagic("r")\n' and \
525 (input.startswith(start) or input.startswith(start_magic)):
554 (input.startswith(start) or input.startswith(start_magic)):
526 #print 'match',`input` # dbg
555 #print 'match',`input` # dbg
527 print 'Executing:',input,
556 print 'Executing:',input,
528 self.shell.runlines(input)
557 self.shell.runlines(input)
529 return
558 return
530 print 'No previous input matching `%s` found.' % start
559 print 'No previous input matching `%s` found.' % start
531
560
532 def magic_page(self, parameter_s=''):
561 def magic_page(self, parameter_s=''):
533 """Pretty print the object and display it through a pager.
562 """Pretty print the object and display it through a pager.
534
563
535 If no parameter is given, use _ (last output)."""
564 If no parameter is given, use _ (last output)."""
536 # After a function contributed by Olivier Aubert, slightly modified.
565 # After a function contributed by Olivier Aubert, slightly modified.
537
566
538 oname = parameter_s and parameter_s or '_'
567 oname = parameter_s and parameter_s or '_'
539 info = self._ofind(oname)
568 info = self._ofind(oname)
540 if info['found']:
569 if info['found']:
541 page(pformat(info['obj']))
570 page(pformat(info['obj']))
542 else:
571 else:
543 print 'Object `%s` not found' % oname
572 print 'Object `%s` not found' % oname
544
573
545 def magic_profile(self, parameter_s=''):
574 def magic_profile(self, parameter_s=''):
546 """Print your currently active IPyhton profile."""
575 """Print your currently active IPyhton profile."""
547 if self.shell.rc.profile:
576 if self.shell.rc.profile:
548 printpl('Current IPython profile: $self.shell.rc.profile.')
577 printpl('Current IPython profile: $self.shell.rc.profile.')
549 else:
578 else:
550 print 'No profile active.'
579 print 'No profile active.'
551
580
552 def _inspect(self,meth,oname,**kw):
581 def _inspect(self,meth,oname,**kw):
553 """Generic interface to the inspector system.
582 """Generic interface to the inspector system.
554
583
555 This function is meant to be called by pdef, pdoc & friends."""
584 This function is meant to be called by pdef, pdoc & friends."""
556
585
557 oname = oname.strip()
586 oname = oname.strip()
558 info = Struct(self._ofind(oname))
587 info = Struct(self._ofind(oname))
559 if info.found:
588 if info.found:
560 pmethod = getattr(self.shell.inspector,meth)
589 pmethod = getattr(self.shell.inspector,meth)
561 formatter = info.ismagic and self.format_screen or None
590 formatter = info.ismagic and self.format_screen or None
562 if meth == 'pdoc':
591 if meth == 'pdoc':
563 pmethod(info.obj,oname,formatter)
592 pmethod(info.obj,oname,formatter)
564 elif meth == 'pinfo':
593 elif meth == 'pinfo':
565 pmethod(info.obj,oname,formatter,info,**kw)
594 pmethod(info.obj,oname,formatter,info,**kw)
566 else:
595 else:
567 pmethod(info.obj,oname)
596 pmethod(info.obj,oname)
568 else:
597 else:
569 print 'Object `%s` not found.' % oname
598 print 'Object `%s` not found.' % oname
570 return 'not found' # so callers can take other action
599 return 'not found' # so callers can take other action
571
600
572 def magic_pdef(self, parameter_s=''):
601 def magic_pdef(self, parameter_s=''):
573 """Print the definition header for any callable object.
602 """Print the definition header for any callable object.
574
603
575 If the object is a class, print the constructor information."""
604 If the object is a class, print the constructor information."""
576 self._inspect('pdef',parameter_s)
605 self._inspect('pdef',parameter_s)
577
606
578 def magic_pdoc(self, parameter_s=''):
607 def magic_pdoc(self, parameter_s=''):
579 """Print the docstring for an object.
608 """Print the docstring for an object.
580
609
581 If the given object is a class, it will print both the class and the
610 If the given object is a class, it will print both the class and the
582 constructor docstrings."""
611 constructor docstrings."""
583 self._inspect('pdoc',parameter_s)
612 self._inspect('pdoc',parameter_s)
584
613
585 def magic_psource(self, parameter_s=''):
614 def magic_psource(self, parameter_s=''):
586 """Print (or run through pager) the source code for an object."""
615 """Print (or run through pager) the source code for an object."""
587 self._inspect('psource',parameter_s)
616 self._inspect('psource',parameter_s)
588
617
589 def magic_pfile(self, parameter_s=''):
618 def magic_pfile(self, parameter_s=''):
590 """Print (or run through pager) the file where an object is defined.
619 """Print (or run through pager) the file where an object is defined.
591
620
592 The file opens at the line where the object definition begins. IPython
621 The file opens at the line where the object definition begins. IPython
593 will honor the environment variable PAGER if set, and otherwise will
622 will honor the environment variable PAGER if set, and otherwise will
594 do its best to print the file in a convenient form.
623 do its best to print the file in a convenient form.
595
624
596 If the given argument is not an object currently defined, IPython will
625 If the given argument is not an object currently defined, IPython will
597 try to interpret it as a filename (automatically adding a .py extension
626 try to interpret it as a filename (automatically adding a .py extension
598 if needed). You can thus use %pfile as a syntax highlighting code
627 if needed). You can thus use %pfile as a syntax highlighting code
599 viewer."""
628 viewer."""
600
629
601 # first interpret argument as an object name
630 # first interpret argument as an object name
602 out = self._inspect('pfile',parameter_s)
631 out = self._inspect('pfile',parameter_s)
603 # if not, try the input as a filename
632 # if not, try the input as a filename
604 if out == 'not found':
633 if out == 'not found':
605 try:
634 try:
606 filename = get_py_filename(parameter_s)
635 filename = get_py_filename(parameter_s)
607 except IOError,msg:
636 except IOError,msg:
608 print msg
637 print msg
609 return
638 return
610 page(self.shell.inspector.format(file(filename).read()))
639 page(self.shell.inspector.format(file(filename).read()))
611
640
612 def magic_pinfo(self, parameter_s=''):
641 def magic_pinfo(self, parameter_s=''):
613 """Provide detailed information about an object.
642 """Provide detailed information about an object.
614
643
615 '%pinfo object' is just a synonym for object? or ?object."""
644 '%pinfo object' is just a synonym for object? or ?object."""
616
645
617 #print 'pinfo par: <%s>' % parameter_s # dbg
646 #print 'pinfo par: <%s>' % parameter_s # dbg
618
647
619 # detail_level: 0 -> obj? , 1 -> obj??
648 # detail_level: 0 -> obj? , 1 -> obj??
620 detail_level = 0
649 detail_level = 0
621 # We need to detect if we got called as 'pinfo pinfo foo', which can
650 # We need to detect if we got called as 'pinfo pinfo foo', which can
622 # happen if the user types 'pinfo foo?' at the cmd line.
651 # happen if the user types 'pinfo foo?' at the cmd line.
623 pinfo,qmark1,oname,qmark2 = \
652 pinfo,qmark1,oname,qmark2 = \
624 re.match('(pinfo )?(\?*)(.*?)(\??$)',parameter_s).groups()
653 re.match('(pinfo )?(\?*)(.*?)(\??$)',parameter_s).groups()
625 if pinfo or qmark1 or qmark2:
654 if pinfo or qmark1 or qmark2:
626 detail_level = 1
655 detail_level = 1
627 if "*" in oname:
656 if "*" in oname:
628 self.magic_psearch(oname)
657 self.magic_psearch(oname)
629 else:
658 else:
630 self._inspect('pinfo',oname,detail_level=detail_level)
659 self._inspect('pinfo',oname,detail_level=detail_level)
631
660
632 def magic_psearch(self, parameter_s=''):
661 def magic_psearch(self, parameter_s=''):
633 """Search for object in namespaces by wildcard.
662 """Search for object in namespaces by wildcard.
634
663
635 %psearch [options] PATTERN [OBJECT TYPE]
664 %psearch [options] PATTERN [OBJECT TYPE]
636
665
637 Note: ? can be used as a synonym for %psearch, at the beginning or at
666 Note: ? can be used as a synonym for %psearch, at the beginning or at
638 the end: both a*? and ?a* are equivalent to '%psearch a*'. Still, the
667 the end: both a*? and ?a* are equivalent to '%psearch a*'. Still, the
639 rest of the command line must be unchanged (options come first), so
668 rest of the command line must be unchanged (options come first), so
640 for example the following forms are equivalent
669 for example the following forms are equivalent
641
670
642 %psearch -i a* function
671 %psearch -i a* function
643 -i a* function?
672 -i a* function?
644 ?-i a* function
673 ?-i a* function
645
674
646 Arguments:
675 Arguments:
647
676
648 PATTERN
677 PATTERN
649
678
650 where PATTERN is a string containing * as a wildcard similar to its
679 where PATTERN is a string containing * as a wildcard similar to its
651 use in a shell. The pattern is matched in all namespaces on the
680 use in a shell. The pattern is matched in all namespaces on the
652 search path. By default objects starting with a single _ are not
681 search path. By default objects starting with a single _ are not
653 matched, many IPython generated objects have a single
682 matched, many IPython generated objects have a single
654 underscore. The default is case insensitive matching. Matching is
683 underscore. The default is case insensitive matching. Matching is
655 also done on the attributes of objects and not only on the objects
684 also done on the attributes of objects and not only on the objects
656 in a module.
685 in a module.
657
686
658 [OBJECT TYPE]
687 [OBJECT TYPE]
659
688
660 Is the name of a python type from the types module. The name is
689 Is the name of a python type from the types module. The name is
661 given in lowercase without the ending type, ex. StringType is
690 given in lowercase without the ending type, ex. StringType is
662 written string. By adding a type here only objects matching the
691 written string. By adding a type here only objects matching the
663 given type are matched. Using all here makes the pattern match all
692 given type are matched. Using all here makes the pattern match all
664 types (this is the default).
693 types (this is the default).
665
694
666 Options:
695 Options:
667
696
668 -a: makes the pattern match even objects whose names start with a
697 -a: makes the pattern match even objects whose names start with a
669 single underscore. These names are normally ommitted from the
698 single underscore. These names are normally ommitted from the
670 search.
699 search.
671
700
672 -i/-c: make the pattern case insensitive/sensitive. If neither of
701 -i/-c: make the pattern case insensitive/sensitive. If neither of
673 these options is given, the default is read from your ipythonrc
702 these options is given, the default is read from your ipythonrc
674 file. The option name which sets this value is
703 file. The option name which sets this value is
675 'wildcards_case_sensitive'. If this option is not specified in your
704 'wildcards_case_sensitive'. If this option is not specified in your
676 ipythonrc file, IPython's internal default is to do a case sensitive
705 ipythonrc file, IPython's internal default is to do a case sensitive
677 search.
706 search.
678
707
679 -e/-s NAMESPACE: exclude/search a given namespace. The pattern you
708 -e/-s NAMESPACE: exclude/search a given namespace. The pattern you
680 specifiy can be searched in any of the following namespaces:
709 specifiy can be searched in any of the following namespaces:
681 'builtin', 'user', 'user_global','internal', 'alias', where
710 'builtin', 'user', 'user_global','internal', 'alias', where
682 'builtin' and 'user' are the search defaults. Note that you should
711 'builtin' and 'user' are the search defaults. Note that you should
683 not use quotes when specifying namespaces.
712 not use quotes when specifying namespaces.
684
713
685 'Builtin' contains the python module builtin, 'user' contains all
714 'Builtin' contains the python module builtin, 'user' contains all
686 user data, 'alias' only contain the shell aliases and no python
715 user data, 'alias' only contain the shell aliases and no python
687 objects, 'internal' contains objects used by IPython. The
716 objects, 'internal' contains objects used by IPython. The
688 'user_global' namespace is only used by embedded IPython instances,
717 'user_global' namespace is only used by embedded IPython instances,
689 and it contains module-level globals. You can add namespaces to the
718 and it contains module-level globals. You can add namespaces to the
690 search with -s or exclude them with -e (these options can be given
719 search with -s or exclude them with -e (these options can be given
691 more than once).
720 more than once).
692
721
693 Examples:
722 Examples:
694
723
695 %psearch a* -> objects beginning with an a
724 %psearch a* -> objects beginning with an a
696 %psearch -e builtin a* -> objects NOT in the builtin space starting in a
725 %psearch -e builtin a* -> objects NOT in the builtin space starting in a
697 %psearch a* function -> all functions beginning with an a
726 %psearch a* function -> all functions beginning with an a
698 %psearch re.e* -> objects beginning with an e in module re
727 %psearch re.e* -> objects beginning with an e in module re
699 %psearch r*.e* -> objects that start with e in modules starting in r
728 %psearch r*.e* -> objects that start with e in modules starting in r
700 %psearch r*.* string -> all strings in modules beginning with r
729 %psearch r*.* string -> all strings in modules beginning with r
701
730
702 Case sensitve search:
731 Case sensitve search:
703
732
704 %psearch -c a* list all object beginning with lower case a
733 %psearch -c a* list all object beginning with lower case a
705
734
706 Show objects beginning with a single _:
735 Show objects beginning with a single _:
707
736
708 %psearch -a _* list objects beginning with a single underscore"""
737 %psearch -a _* list objects beginning with a single underscore"""
709
738
710 # default namespaces to be searched
739 # default namespaces to be searched
711 def_search = ['user','builtin']
740 def_search = ['user','builtin']
712
741
713 # Process options/args
742 # Process options/args
714 opts,args = self.parse_options(parameter_s,'cias:e:',list_all=True)
743 opts,args = self.parse_options(parameter_s,'cias:e:',list_all=True)
715 opt = opts.get
744 opt = opts.get
716 shell = self.shell
745 shell = self.shell
717 psearch = shell.inspector.psearch
746 psearch = shell.inspector.psearch
718
747
719 # select case options
748 # select case options
720 if opts.has_key('i'):
749 if opts.has_key('i'):
721 ignore_case = True
750 ignore_case = True
722 elif opts.has_key('c'):
751 elif opts.has_key('c'):
723 ignore_case = False
752 ignore_case = False
724 else:
753 else:
725 ignore_case = not shell.rc.wildcards_case_sensitive
754 ignore_case = not shell.rc.wildcards_case_sensitive
726
755
727 # Build list of namespaces to search from user options
756 # Build list of namespaces to search from user options
728 def_search.extend(opt('s',[]))
757 def_search.extend(opt('s',[]))
729 ns_exclude = ns_exclude=opt('e',[])
758 ns_exclude = ns_exclude=opt('e',[])
730 ns_search = [nm for nm in def_search if nm not in ns_exclude]
759 ns_search = [nm for nm in def_search if nm not in ns_exclude]
731
760
732 # Call the actual search
761 # Call the actual search
733 try:
762 try:
734 psearch(args,shell.ns_table,ns_search,
763 psearch(args,shell.ns_table,ns_search,
735 show_all=opt('a'),ignore_case=ignore_case)
764 show_all=opt('a'),ignore_case=ignore_case)
736 except:
765 except:
737 shell.showtraceback()
766 shell.showtraceback()
738
767
739 def magic_who_ls(self, parameter_s=''):
768 def magic_who_ls(self, parameter_s=''):
740 """Return a sorted list of all interactive variables.
769 """Return a sorted list of all interactive variables.
741
770
742 If arguments are given, only variables of types matching these
771 If arguments are given, only variables of types matching these
743 arguments are returned."""
772 arguments are returned."""
744
773
745 user_ns = self.shell.user_ns
774 user_ns = self.shell.user_ns
746 internal_ns = self.shell.internal_ns
775 internal_ns = self.shell.internal_ns
747 user_config_ns = self.shell.user_config_ns
776 user_config_ns = self.shell.user_config_ns
748 out = []
777 out = []
749 typelist = parameter_s.split()
778 typelist = parameter_s.split()
750
779
751 for i in user_ns:
780 for i in user_ns:
752 if not (i.startswith('_') or i.startswith('_i')) \
781 if not (i.startswith('_') or i.startswith('_i')) \
753 and not (i in internal_ns or i in user_config_ns):
782 and not (i in internal_ns or i in user_config_ns):
754 if typelist:
783 if typelist:
755 if type(user_ns[i]).__name__ in typelist:
784 if type(user_ns[i]).__name__ in typelist:
756 out.append(i)
785 out.append(i)
757 else:
786 else:
758 out.append(i)
787 out.append(i)
759 out.sort()
788 out.sort()
760 return out
789 return out
761
790
762 def magic_who(self, parameter_s=''):
791 def magic_who(self, parameter_s=''):
763 """Print all interactive variables, with some minimal formatting.
792 """Print all interactive variables, with some minimal formatting.
764
793
765 If any arguments are given, only variables whose type matches one of
794 If any arguments are given, only variables whose type matches one of
766 these are printed. For example:
795 these are printed. For example:
767
796
768 %who function str
797 %who function str
769
798
770 will only list functions and strings, excluding all other types of
799 will only list functions and strings, excluding all other types of
771 variables. To find the proper type names, simply use type(var) at a
800 variables. To find the proper type names, simply use type(var) at a
772 command line to see how python prints type names. For example:
801 command line to see how python prints type names. For example:
773
802
774 In [1]: type('hello')\\
803 In [1]: type('hello')\\
775 Out[1]: <type 'str'>
804 Out[1]: <type 'str'>
776
805
777 indicates that the type name for strings is 'str'.
806 indicates that the type name for strings is 'str'.
778
807
779 %who always excludes executed names loaded through your configuration
808 %who always excludes executed names loaded through your configuration
780 file and things which are internal to IPython.
809 file and things which are internal to IPython.
781
810
782 This is deliberate, as typically you may load many modules and the
811 This is deliberate, as typically you may load many modules and the
783 purpose of %who is to show you only what you've manually defined."""
812 purpose of %who is to show you only what you've manually defined."""
784
813
785 varlist = self.magic_who_ls(parameter_s)
814 varlist = self.magic_who_ls(parameter_s)
786 if not varlist:
815 if not varlist:
787 print 'Interactive namespace is empty.'
816 print 'Interactive namespace is empty.'
788 return
817 return
789
818
790 # if we have variables, move on...
819 # if we have variables, move on...
791
820
792 # stupid flushing problem: when prompts have no separators, stdout is
821 # stupid flushing problem: when prompts have no separators, stdout is
793 # getting lost. I'm starting to think this is a python bug. I'm having
822 # getting lost. I'm starting to think this is a python bug. I'm having
794 # to force a flush with a print because even a sys.stdout.flush
823 # to force a flush with a print because even a sys.stdout.flush
795 # doesn't seem to do anything!
824 # doesn't seem to do anything!
796
825
797 count = 0
826 count = 0
798 for i in varlist:
827 for i in varlist:
799 print i+'\t',
828 print i+'\t',
800 count += 1
829 count += 1
801 if count > 8:
830 if count > 8:
802 count = 0
831 count = 0
803 print
832 print
804 sys.stdout.flush() # FIXME. Why the hell isn't this flushing???
833 sys.stdout.flush() # FIXME. Why the hell isn't this flushing???
805
834
806 print # well, this does force a flush at the expense of an extra \n
835 print # well, this does force a flush at the expense of an extra \n
807
836
808 def magic_whos(self, parameter_s=''):
837 def magic_whos(self, parameter_s=''):
809 """Like %who, but gives some extra information about each variable.
838 """Like %who, but gives some extra information about each variable.
810
839
811 The same type filtering of %who can be applied here.
840 The same type filtering of %who can be applied here.
812
841
813 For all variables, the type is printed. Additionally it prints:
842 For all variables, the type is printed. Additionally it prints:
814
843
815 - For {},[],(): their length.
844 - For {},[],(): their length.
816
845
817 - For Numeric arrays, a summary with shape, number of elements,
846 - For Numeric arrays, a summary with shape, number of elements,
818 typecode and size in memory.
847 typecode and size in memory.
819
848
820 - Everything else: a string representation, snipping their middle if
849 - Everything else: a string representation, snipping their middle if
821 too long."""
850 too long."""
822
851
823 varnames = self.magic_who_ls(parameter_s)
852 varnames = self.magic_who_ls(parameter_s)
824 if not varnames:
853 if not varnames:
825 print 'Interactive namespace is empty.'
854 print 'Interactive namespace is empty.'
826 return
855 return
827
856
828 # if we have variables, move on...
857 # if we have variables, move on...
829
858
830 # for these types, show len() instead of data:
859 # for these types, show len() instead of data:
831 seq_types = [types.DictType,types.ListType,types.TupleType]
860 seq_types = [types.DictType,types.ListType,types.TupleType]
832
861
833 # for Numeric arrays, display summary info
862 # for Numeric arrays, display summary info
834 try:
863 try:
835 import Numeric
864 import Numeric
836 except ImportError:
865 except ImportError:
837 array_type = None
866 array_type = None
838 else:
867 else:
839 array_type = Numeric.ArrayType.__name__
868 array_type = Numeric.ArrayType.__name__
840
869
841 # Find all variable names and types so we can figure out column sizes
870 # Find all variable names and types so we can figure out column sizes
842 get_vars = lambda i: self.shell.user_ns[i]
871 get_vars = lambda i: self.shell.user_ns[i]
843 type_name = lambda v: type(v).__name__
872 type_name = lambda v: type(v).__name__
844 varlist = map(get_vars,varnames)
873 varlist = map(get_vars,varnames)
845
874
846 typelist = []
875 typelist = []
847 for vv in varlist:
876 for vv in varlist:
848 tt = type_name(vv)
877 tt = type_name(vv)
849 if tt=='instance':
878 if tt=='instance':
850 typelist.append(str(vv.__class__))
879 typelist.append(str(vv.__class__))
851 else:
880 else:
852 typelist.append(tt)
881 typelist.append(tt)
853
882
854 # column labels and # of spaces as separator
883 # column labels and # of spaces as separator
855 varlabel = 'Variable'
884 varlabel = 'Variable'
856 typelabel = 'Type'
885 typelabel = 'Type'
857 datalabel = 'Data/Info'
886 datalabel = 'Data/Info'
858 colsep = 3
887 colsep = 3
859 # variable format strings
888 # variable format strings
860 vformat = "$vname.ljust(varwidth)$vtype.ljust(typewidth)"
889 vformat = "$vname.ljust(varwidth)$vtype.ljust(typewidth)"
861 vfmt_short = '$vstr[:25]<...>$vstr[-25:]'
890 vfmt_short = '$vstr[:25]<...>$vstr[-25:]'
862 aformat = "%s: %s elems, type `%s`, %s bytes"
891 aformat = "%s: %s elems, type `%s`, %s bytes"
863 # find the size of the columns to format the output nicely
892 # find the size of the columns to format the output nicely
864 varwidth = max(max(map(len,varnames)), len(varlabel)) + colsep
893 varwidth = max(max(map(len,varnames)), len(varlabel)) + colsep
865 typewidth = max(max(map(len,typelist)), len(typelabel)) + colsep
894 typewidth = max(max(map(len,typelist)), len(typelabel)) + colsep
866 # table header
895 # table header
867 print varlabel.ljust(varwidth) + typelabel.ljust(typewidth) + \
896 print varlabel.ljust(varwidth) + typelabel.ljust(typewidth) + \
868 ' '+datalabel+'\n' + '-'*(varwidth+typewidth+len(datalabel)+1)
897 ' '+datalabel+'\n' + '-'*(varwidth+typewidth+len(datalabel)+1)
869 # and the table itself
898 # and the table itself
870 kb = 1024
899 kb = 1024
871 Mb = 1048576 # kb**2
900 Mb = 1048576 # kb**2
872 for vname,var,vtype in zip(varnames,varlist,typelist):
901 for vname,var,vtype in zip(varnames,varlist,typelist):
873 print itpl(vformat),
902 print itpl(vformat),
874 if vtype in seq_types:
903 if vtype in seq_types:
875 print len(var)
904 print len(var)
876 elif vtype==array_type:
905 elif vtype==array_type:
877 vshape = str(var.shape).replace(',','').replace(' ','x')[1:-1]
906 vshape = str(var.shape).replace(',','').replace(' ','x')[1:-1]
878 vsize = Numeric.size(var)
907 vsize = Numeric.size(var)
879 vbytes = vsize*var.itemsize()
908 vbytes = vsize*var.itemsize()
880 if vbytes < 100000:
909 if vbytes < 100000:
881 print aformat % (vshape,vsize,var.typecode(),vbytes)
910 print aformat % (vshape,vsize,var.typecode(),vbytes)
882 else:
911 else:
883 print aformat % (vshape,vsize,var.typecode(),vbytes),
912 print aformat % (vshape,vsize,var.typecode(),vbytes),
884 if vbytes < Mb:
913 if vbytes < Mb:
885 print '(%s kb)' % (vbytes/kb,)
914 print '(%s kb)' % (vbytes/kb,)
886 else:
915 else:
887 print '(%s Mb)' % (vbytes/Mb,)
916 print '(%s Mb)' % (vbytes/Mb,)
888 else:
917 else:
889 vstr = str(var).replace('\n','\\n')
918 vstr = str(var).replace('\n','\\n')
890 if len(vstr) < 50:
919 if len(vstr) < 50:
891 print vstr
920 print vstr
892 else:
921 else:
893 printpl(vfmt_short)
922 printpl(vfmt_short)
894
923
895 def magic_reset(self, parameter_s=''):
924 def magic_reset(self, parameter_s=''):
896 """Resets the namespace by removing all names defined by the user.
925 """Resets the namespace by removing all names defined by the user.
897
926
898 Input/Output history are left around in case you need them."""
927 Input/Output history are left around in case you need them."""
899
928
900 ans = raw_input(
929 ans = raw_input(
901 "Once deleted, variables cannot be recovered. Proceed (y/n)? ")
930 "Once deleted, variables cannot be recovered. Proceed (y/n)? ")
902 if not ans.lower() == 'y':
931 if not ans.lower() == 'y':
903 print 'Nothing done.'
932 print 'Nothing done.'
904 return
933 return
905 user_ns = self.shell.user_ns
934 user_ns = self.shell.user_ns
906 for i in self.magic_who_ls():
935 for i in self.magic_who_ls():
907 del(user_ns[i])
936 del(user_ns[i])
908
937
909 def magic_config(self,parameter_s=''):
938 def magic_config(self,parameter_s=''):
910 """Show IPython's internal configuration."""
939 """Show IPython's internal configuration."""
911
940
912 page('Current configuration structure:\n'+
941 page('Current configuration structure:\n'+
913 pformat(self.shell.rc.dict()))
942 pformat(self.shell.rc.dict()))
914
943
915 def magic_logstart(self,parameter_s=''):
944 def magic_logstart(self,parameter_s=''):
916 """Start logging anywhere in a session.
945 """Start logging anywhere in a session.
917
946
918 %logstart [-o|-t] [log_name [log_mode]]
947 %logstart [-o|-t] [log_name [log_mode]]
919
948
920 If no name is given, it defaults to a file named 'ipython_log.py' in your
949 If no name is given, it defaults to a file named 'ipython_log.py' in your
921 current directory, in 'rotate' mode (see below).
950 current directory, in 'rotate' mode (see below).
922
951
923 '%logstart name' saves to file 'name' in 'backup' mode. It saves your
952 '%logstart name' saves to file 'name' in 'backup' mode. It saves your
924 history up to that point and then continues logging.
953 history up to that point and then continues logging.
925
954
926 %logstart takes a second optional parameter: logging mode. This can be one
955 %logstart takes a second optional parameter: logging mode. This can be one
927 of (note that the modes are given unquoted):\\
956 of (note that the modes are given unquoted):\\
928 append: well, that says it.\\
957 append: well, that says it.\\
929 backup: rename (if exists) to name~ and start name.\\
958 backup: rename (if exists) to name~ and start name.\\
930 global: single logfile in your home dir, appended to.\\
959 global: single logfile in your home dir, appended to.\\
931 over : overwrite existing log.\\
960 over : overwrite existing log.\\
932 rotate: create rotating logs name.1~, name.2~, etc.
961 rotate: create rotating logs name.1~, name.2~, etc.
933
962
934 Options:
963 Options:
935
964
936 -o: log also IPython's output. In this mode, all commands which
965 -o: log also IPython's output. In this mode, all commands which
937 generate an Out[NN] prompt are recorded to the logfile, right after
966 generate an Out[NN] prompt are recorded to the logfile, right after
938 their corresponding input line. The output lines are always
967 their corresponding input line. The output lines are always
939 prepended with a '#[Out]# ' marker, so that the log remains valid
968 prepended with a '#[Out]# ' marker, so that the log remains valid
940 Python code.
969 Python code.
941
970
942 Since this marker is always the same, filtering only the output from
971 Since this marker is always the same, filtering only the output from
943 a log is very easy, using for example a simple awk call:
972 a log is very easy, using for example a simple awk call:
944
973
945 awk -F'#\\[Out\\]# ' '{if($2) {print $2}}' ipython_log.py
974 awk -F'#\\[Out\\]# ' '{if($2) {print $2}}' ipython_log.py
946
975
947 -t: put timestamps before each input line logged (these are put in
976 -t: put timestamps before each input line logged (these are put in
948 comments)."""
977 comments)."""
949
978
950 opts,par = self.parse_options(parameter_s,'ot')
979 opts,par = self.parse_options(parameter_s,'ot')
951 log_output = 'o' in opts
980 log_output = 'o' in opts
952 timestamp = 't' in opts
981 timestamp = 't' in opts
953
982
954 rc = self.shell.rc
983 rc = self.shell.rc
955 logger = self.shell.logger
984 logger = self.shell.logger
956
985
957 # if no args are given, the defaults set in the logger constructor by
986 # if no args are given, the defaults set in the logger constructor by
958 # ipytohn remain valid
987 # ipytohn remain valid
959 if par:
988 if par:
960 try:
989 try:
961 logfname,logmode = par.split()
990 logfname,logmode = par.split()
962 except:
991 except:
963 logfname = par
992 logfname = par
964 logmode = 'backup'
993 logmode = 'backup'
965 else:
994 else:
966 logfname = logger.logfname
995 logfname = logger.logfname
967 logmode = logger.logmode
996 logmode = logger.logmode
968 # put logfname into rc struct as if it had been called on the command
997 # put logfname into rc struct as if it had been called on the command
969 # line, so it ends up saved in the log header Save it in case we need
998 # line, so it ends up saved in the log header Save it in case we need
970 # to restore it...
999 # to restore it...
971 old_logfile = rc.opts.get('logfile','')
1000 old_logfile = rc.opts.get('logfile','')
972 if logfname:
1001 if logfname:
973 logfname = os.path.expanduser(logfname)
1002 logfname = os.path.expanduser(logfname)
974 rc.opts.logfile = logfname
1003 rc.opts.logfile = logfname
975 loghead = self.shell.loghead_tpl % (rc.opts,rc.args)
1004 loghead = self.shell.loghead_tpl % (rc.opts,rc.args)
976 try:
1005 try:
977 started = logger.logstart(logfname,loghead,logmode,
1006 started = logger.logstart(logfname,loghead,logmode,
978 log_output,timestamp)
1007 log_output,timestamp)
979 except:
1008 except:
980 rc.opts.logfile = old_logfile
1009 rc.opts.logfile = old_logfile
981 warn("Couldn't start log: %s" % sys.exc_info()[1])
1010 warn("Couldn't start log: %s" % sys.exc_info()[1])
982 else:
1011 else:
983 # log input history up to this point, optionally interleaving
1012 # log input history up to this point, optionally interleaving
984 # output if requested
1013 # output if requested
985
1014
986 if timestamp:
1015 if timestamp:
987 # disable timestamping for the previous history, since we've
1016 # disable timestamping for the previous history, since we've
988 # lost those already (no time machine here).
1017 # lost those already (no time machine here).
989 logger.timestamp = False
1018 logger.timestamp = False
990 if log_output:
1019 if log_output:
991 log_write = logger.log_write
1020 log_write = logger.log_write
992 input_hist = self.shell.input_hist
1021 input_hist = self.shell.input_hist
993 output_hist = self.shell.output_hist
1022 output_hist = self.shell.output_hist
994 for n in range(1,len(input_hist)-1):
1023 for n in range(1,len(input_hist)-1):
995 log_write(input_hist[n].rstrip())
1024 log_write(input_hist[n].rstrip())
996 if n in output_hist:
1025 if n in output_hist:
997 log_write(repr(output_hist[n]),'output')
1026 log_write(repr(output_hist[n]),'output')
998 else:
1027 else:
999 logger.log_write(self.shell.input_hist[1:])
1028 logger.log_write(self.shell.input_hist[1:])
1000 if timestamp:
1029 if timestamp:
1001 # re-enable timestamping
1030 # re-enable timestamping
1002 logger.timestamp = True
1031 logger.timestamp = True
1003
1032
1004 print ('Activating auto-logging. '
1033 print ('Activating auto-logging. '
1005 'Current session state plus future input saved.')
1034 'Current session state plus future input saved.')
1006 logger.logstate()
1035 logger.logstate()
1007
1036
1008 def magic_logoff(self,parameter_s=''):
1037 def magic_logoff(self,parameter_s=''):
1009 """Temporarily stop logging.
1038 """Temporarily stop logging.
1010
1039
1011 You must have previously started logging."""
1040 You must have previously started logging."""
1012 self.shell.logger.switch_log(0)
1041 self.shell.logger.switch_log(0)
1013
1042
1014 def magic_logon(self,parameter_s=''):
1043 def magic_logon(self,parameter_s=''):
1015 """Restart logging.
1044 """Restart logging.
1016
1045
1017 This function is for restarting logging which you've temporarily
1046 This function is for restarting logging which you've temporarily
1018 stopped with %logoff. For starting logging for the first time, you
1047 stopped with %logoff. For starting logging for the first time, you
1019 must use the %logstart function, which allows you to specify an
1048 must use the %logstart function, which allows you to specify an
1020 optional log filename."""
1049 optional log filename."""
1021
1050
1022 self.shell.logger.switch_log(1)
1051 self.shell.logger.switch_log(1)
1023
1052
1024 def magic_logstate(self,parameter_s=''):
1053 def magic_logstate(self,parameter_s=''):
1025 """Print the status of the logging system."""
1054 """Print the status of the logging system."""
1026
1055
1027 self.shell.logger.logstate()
1056 self.shell.logger.logstate()
1028
1057
1029 def magic_pdb(self, parameter_s=''):
1058 def magic_pdb(self, parameter_s=''):
1030 """Control the calling of the pdb interactive debugger.
1059 """Control the calling of the pdb interactive debugger.
1031
1060
1032 Call as '%pdb on', '%pdb 1', '%pdb off' or '%pdb 0'. If called without
1061 Call as '%pdb on', '%pdb 1', '%pdb off' or '%pdb 0'. If called without
1033 argument it works as a toggle.
1062 argument it works as a toggle.
1034
1063
1035 When an exception is triggered, IPython can optionally call the
1064 When an exception is triggered, IPython can optionally call the
1036 interactive pdb debugger after the traceback printout. %pdb toggles
1065 interactive pdb debugger after the traceback printout. %pdb toggles
1037 this feature on and off."""
1066 this feature on and off."""
1038
1067
1039 par = parameter_s.strip().lower()
1068 par = parameter_s.strip().lower()
1040
1069
1041 if par:
1070 if par:
1042 try:
1071 try:
1043 new_pdb = {'off':0,'0':0,'on':1,'1':1}[par]
1072 new_pdb = {'off':0,'0':0,'on':1,'1':1}[par]
1044 except KeyError:
1073 except KeyError:
1045 print ('Incorrect argument. Use on/1, off/0, '
1074 print ('Incorrect argument. Use on/1, off/0, '
1046 'or nothing for a toggle.')
1075 'or nothing for a toggle.')
1047 return
1076 return
1048 else:
1077 else:
1049 # toggle
1078 # toggle
1050 new_pdb = not self.shell.InteractiveTB.call_pdb
1079 new_pdb = not self.shell.InteractiveTB.call_pdb
1051
1080
1052 # set on the shell
1081 # set on the shell
1053 self.shell.call_pdb = new_pdb
1082 self.shell.call_pdb = new_pdb
1054 print 'Automatic pdb calling has been turned',on_off(new_pdb)
1083 print 'Automatic pdb calling has been turned',on_off(new_pdb)
1055
1084
1056 def magic_prun(self, parameter_s ='',user_mode=1,
1085 def magic_prun(self, parameter_s ='',user_mode=1,
1057 opts=None,arg_lst=None,prog_ns=None):
1086 opts=None,arg_lst=None,prog_ns=None):
1058
1087
1059 """Run a statement through the python code profiler.
1088 """Run a statement through the python code profiler.
1060
1089
1061 Usage:\\
1090 Usage:\\
1062 %prun [options] statement
1091 %prun [options] statement
1063
1092
1064 The given statement (which doesn't require quote marks) is run via the
1093 The given statement (which doesn't require quote marks) is run via the
1065 python profiler in a manner similar to the profile.run() function.
1094 python profiler in a manner similar to the profile.run() function.
1066 Namespaces are internally managed to work correctly; profile.run
1095 Namespaces are internally managed to work correctly; profile.run
1067 cannot be used in IPython because it makes certain assumptions about
1096 cannot be used in IPython because it makes certain assumptions about
1068 namespaces which do not hold under IPython.
1097 namespaces which do not hold under IPython.
1069
1098
1070 Options:
1099 Options:
1071
1100
1072 -l <limit>: you can place restrictions on what or how much of the
1101 -l <limit>: you can place restrictions on what or how much of the
1073 profile gets printed. The limit value can be:
1102 profile gets printed. The limit value can be:
1074
1103
1075 * A string: only information for function names containing this string
1104 * A string: only information for function names containing this string
1076 is printed.
1105 is printed.
1077
1106
1078 * An integer: only these many lines are printed.
1107 * An integer: only these many lines are printed.
1079
1108
1080 * A float (between 0 and 1): this fraction of the report is printed
1109 * A float (between 0 and 1): this fraction of the report is printed
1081 (for example, use a limit of 0.4 to see the topmost 40% only).
1110 (for example, use a limit of 0.4 to see the topmost 40% only).
1082
1111
1083 You can combine several limits with repeated use of the option. For
1112 You can combine several limits with repeated use of the option. For
1084 example, '-l __init__ -l 5' will print only the topmost 5 lines of
1113 example, '-l __init__ -l 5' will print only the topmost 5 lines of
1085 information about class constructors.
1114 information about class constructors.
1086
1115
1087 -r: return the pstats.Stats object generated by the profiling. This
1116 -r: return the pstats.Stats object generated by the profiling. This
1088 object has all the information about the profile in it, and you can
1117 object has all the information about the profile in it, and you can
1089 later use it for further analysis or in other functions.
1118 later use it for further analysis or in other functions.
1090
1119
1091 Since magic functions have a particular form of calling which prevents
1120 Since magic functions have a particular form of calling which prevents
1092 you from writing something like:\\
1121 you from writing something like:\\
1093 In [1]: p = %prun -r print 4 # invalid!\\
1122 In [1]: p = %prun -r print 4 # invalid!\\
1094 you must instead use IPython's automatic variables to assign this:\\
1123 you must instead use IPython's automatic variables to assign this:\\
1095 In [1]: %prun -r print 4 \\
1124 In [1]: %prun -r print 4 \\
1096 Out[1]: <pstats.Stats instance at 0x8222cec>\\
1125 Out[1]: <pstats.Stats instance at 0x8222cec>\\
1097 In [2]: stats = _
1126 In [2]: stats = _
1098
1127
1099 If you really need to assign this value via an explicit function call,
1128 If you really need to assign this value via an explicit function call,
1100 you can always tap directly into the true name of the magic function
1129 you can always tap directly into the true name of the magic function
1101 by using the ipmagic function (which IPython automatically adds to the
1130 by using the ipmagic function (which IPython automatically adds to the
1102 builtins):\\
1131 builtins):\\
1103 In [3]: stats = ipmagic('prun','-r print 4')
1132 In [3]: stats = ipmagic('prun','-r print 4')
1104
1133
1105 You can type ipmagic? for more details on ipmagic.
1134 You can type ipmagic? for more details on ipmagic.
1106
1135
1107 -s <key>: sort profile by given key. You can provide more than one key
1136 -s <key>: sort profile by given key. You can provide more than one key
1108 by using the option several times: '-s key1 -s key2 -s key3...'. The
1137 by using the option several times: '-s key1 -s key2 -s key3...'. The
1109 default sorting key is 'time'.
1138 default sorting key is 'time'.
1110
1139
1111 The following is copied verbatim from the profile documentation
1140 The following is copied verbatim from the profile documentation
1112 referenced below:
1141 referenced below:
1113
1142
1114 When more than one key is provided, additional keys are used as
1143 When more than one key is provided, additional keys are used as
1115 secondary criteria when the there is equality in all keys selected
1144 secondary criteria when the there is equality in all keys selected
1116 before them.
1145 before them.
1117
1146
1118 Abbreviations can be used for any key names, as long as the
1147 Abbreviations can be used for any key names, as long as the
1119 abbreviation is unambiguous. The following are the keys currently
1148 abbreviation is unambiguous. The following are the keys currently
1120 defined:
1149 defined:
1121
1150
1122 Valid Arg Meaning\\
1151 Valid Arg Meaning\\
1123 "calls" call count\\
1152 "calls" call count\\
1124 "cumulative" cumulative time\\
1153 "cumulative" cumulative time\\
1125 "file" file name\\
1154 "file" file name\\
1126 "module" file name\\
1155 "module" file name\\
1127 "pcalls" primitive call count\\
1156 "pcalls" primitive call count\\
1128 "line" line number\\
1157 "line" line number\\
1129 "name" function name\\
1158 "name" function name\\
1130 "nfl" name/file/line\\
1159 "nfl" name/file/line\\
1131 "stdname" standard name\\
1160 "stdname" standard name\\
1132 "time" internal time
1161 "time" internal time
1133
1162
1134 Note that all sorts on statistics are in descending order (placing
1163 Note that all sorts on statistics are in descending order (placing
1135 most time consuming items first), where as name, file, and line number
1164 most time consuming items first), where as name, file, and line number
1136 searches are in ascending order (i.e., alphabetical). The subtle
1165 searches are in ascending order (i.e., alphabetical). The subtle
1137 distinction between "nfl" and "stdname" is that the standard name is a
1166 distinction between "nfl" and "stdname" is that the standard name is a
1138 sort of the name as printed, which means that the embedded line
1167 sort of the name as printed, which means that the embedded line
1139 numbers get compared in an odd way. For example, lines 3, 20, and 40
1168 numbers get compared in an odd way. For example, lines 3, 20, and 40
1140 would (if the file names were the same) appear in the string order
1169 would (if the file names were the same) appear in the string order
1141 "20" "3" and "40". In contrast, "nfl" does a numeric compare of the
1170 "20" "3" and "40". In contrast, "nfl" does a numeric compare of the
1142 line numbers. In fact, sort_stats("nfl") is the same as
1171 line numbers. In fact, sort_stats("nfl") is the same as
1143 sort_stats("name", "file", "line").
1172 sort_stats("name", "file", "line").
1144
1173
1145 -T <filename>: save profile results as shown on screen to a text
1174 -T <filename>: save profile results as shown on screen to a text
1146 file. The profile is still shown on screen.
1175 file. The profile is still shown on screen.
1147
1176
1148 -D <filename>: save (via dump_stats) profile statistics to given
1177 -D <filename>: save (via dump_stats) profile statistics to given
1149 filename. This data is in a format understod by the pstats module, and
1178 filename. This data is in a format understod by the pstats module, and
1150 is generated by a call to the dump_stats() method of profile
1179 is generated by a call to the dump_stats() method of profile
1151 objects. The profile is still shown on screen.
1180 objects. The profile is still shown on screen.
1152
1181
1153 If you want to run complete programs under the profiler's control, use
1182 If you want to run complete programs under the profiler's control, use
1154 '%run -p [prof_opts] filename.py [args to program]' where prof_opts
1183 '%run -p [prof_opts] filename.py [args to program]' where prof_opts
1155 contains profiler specific options as described here.
1184 contains profiler specific options as described here.
1156
1185
1157 You can read the complete documentation for the profile module with:\\
1186 You can read the complete documentation for the profile module with:\\
1158 In [1]: import profile; profile.help() """
1187 In [1]: import profile; profile.help() """
1159
1188
1160 opts_def = Struct(D=[''],l=[],s=['time'],T=[''])
1189 opts_def = Struct(D=[''],l=[],s=['time'],T=[''])
1161 # protect user quote marks
1190 # protect user quote marks
1162 parameter_s = parameter_s.replace('"',r'\"').replace("'",r"\'")
1191 parameter_s = parameter_s.replace('"',r'\"').replace("'",r"\'")
1163
1192
1164 if user_mode: # regular user call
1193 if user_mode: # regular user call
1165 opts,arg_str = self.parse_options(parameter_s,'D:l:rs:T:',
1194 opts,arg_str = self.parse_options(parameter_s,'D:l:rs:T:',
1166 list_all=1)
1195 list_all=1)
1167 namespace = self.shell.user_ns
1196 namespace = self.shell.user_ns
1168 else: # called to run a program by %run -p
1197 else: # called to run a program by %run -p
1169 try:
1198 try:
1170 filename = get_py_filename(arg_lst[0])
1199 filename = get_py_filename(arg_lst[0])
1171 except IOError,msg:
1200 except IOError,msg:
1172 error(msg)
1201 error(msg)
1173 return
1202 return
1174
1203
1175 arg_str = 'execfile(filename,prog_ns)'
1204 arg_str = 'execfile(filename,prog_ns)'
1176 namespace = locals()
1205 namespace = locals()
1177
1206
1178 opts.merge(opts_def)
1207 opts.merge(opts_def)
1179
1208
1180 prof = profile.Profile()
1209 prof = profile.Profile()
1181 try:
1210 try:
1182 prof = prof.runctx(arg_str,namespace,namespace)
1211 prof = prof.runctx(arg_str,namespace,namespace)
1183 sys_exit = ''
1212 sys_exit = ''
1184 except SystemExit:
1213 except SystemExit:
1185 sys_exit = """*** SystemExit exception caught in code being profiled."""
1214 sys_exit = """*** SystemExit exception caught in code being profiled."""
1186
1215
1187 stats = pstats.Stats(prof).strip_dirs().sort_stats(*opts.s)
1216 stats = pstats.Stats(prof).strip_dirs().sort_stats(*opts.s)
1188
1217
1189 lims = opts.l
1218 lims = opts.l
1190 if lims:
1219 if lims:
1191 lims = [] # rebuild lims with ints/floats/strings
1220 lims = [] # rebuild lims with ints/floats/strings
1192 for lim in opts.l:
1221 for lim in opts.l:
1193 try:
1222 try:
1194 lims.append(int(lim))
1223 lims.append(int(lim))
1195 except ValueError:
1224 except ValueError:
1196 try:
1225 try:
1197 lims.append(float(lim))
1226 lims.append(float(lim))
1198 except ValueError:
1227 except ValueError:
1199 lims.append(lim)
1228 lims.append(lim)
1200
1229
1201 # trap output
1230 # trap output
1202 sys_stdout = sys.stdout
1231 sys_stdout = sys.stdout
1203 stdout_trap = StringIO()
1232 stdout_trap = StringIO()
1204 try:
1233 try:
1205 sys.stdout = stdout_trap
1234 sys.stdout = stdout_trap
1206 stats.print_stats(*lims)
1235 stats.print_stats(*lims)
1207 finally:
1236 finally:
1208 sys.stdout = sys_stdout
1237 sys.stdout = sys_stdout
1209 output = stdout_trap.getvalue()
1238 output = stdout_trap.getvalue()
1210 output = output.rstrip()
1239 output = output.rstrip()
1211
1240
1212 page(output,screen_lines=self.shell.rc.screen_length)
1241 page(output,screen_lines=self.shell.rc.screen_length)
1213 print sys_exit,
1242 print sys_exit,
1214
1243
1215 dump_file = opts.D[0]
1244 dump_file = opts.D[0]
1216 text_file = opts.T[0]
1245 text_file = opts.T[0]
1217 if dump_file:
1246 if dump_file:
1218 prof.dump_stats(dump_file)
1247 prof.dump_stats(dump_file)
1219 print '\n*** Profile stats marshalled to file',\
1248 print '\n*** Profile stats marshalled to file',\
1220 `dump_file`+'.',sys_exit
1249 `dump_file`+'.',sys_exit
1221 if text_file:
1250 if text_file:
1222 file(text_file,'w').write(output)
1251 file(text_file,'w').write(output)
1223 print '\n*** Profile printout saved to text file',\
1252 print '\n*** Profile printout saved to text file',\
1224 `text_file`+'.',sys_exit
1253 `text_file`+'.',sys_exit
1225
1254
1226 if opts.has_key('r'):
1255 if opts.has_key('r'):
1227 return stats
1256 return stats
1228 else:
1257 else:
1229 return None
1258 return None
1230
1259
1231 def magic_run(self, parameter_s ='',runner=None):
1260 def magic_run(self, parameter_s ='',runner=None):
1232 """Run the named file inside IPython as a program.
1261 """Run the named file inside IPython as a program.
1233
1262
1234 Usage:\\
1263 Usage:\\
1235 %run [-n -i -t [-N<N>] -d [-b<N>] -p [profile options]] file [args]
1264 %run [-n -i -t [-N<N>] -d [-b<N>] -p [profile options]] file [args]
1236
1265
1237 Parameters after the filename are passed as command-line arguments to
1266 Parameters after the filename are passed as command-line arguments to
1238 the program (put in sys.argv). Then, control returns to IPython's
1267 the program (put in sys.argv). Then, control returns to IPython's
1239 prompt.
1268 prompt.
1240
1269
1241 This is similar to running at a system prompt:\\
1270 This is similar to running at a system prompt:\\
1242 $ python file args\\
1271 $ python file args\\
1243 but with the advantage of giving you IPython's tracebacks, and of
1272 but with the advantage of giving you IPython's tracebacks, and of
1244 loading all variables into your interactive namespace for further use
1273 loading all variables into your interactive namespace for further use
1245 (unless -p is used, see below).
1274 (unless -p is used, see below).
1246
1275
1247 The file is executed in a namespace initially consisting only of
1276 The file is executed in a namespace initially consisting only of
1248 __name__=='__main__' and sys.argv constructed as indicated. It thus
1277 __name__=='__main__' and sys.argv constructed as indicated. It thus
1249 sees its environment as if it were being run as a stand-alone
1278 sees its environment as if it were being run as a stand-alone
1250 program. But after execution, the IPython interactive namespace gets
1279 program. But after execution, the IPython interactive namespace gets
1251 updated with all variables defined in the program (except for __name__
1280 updated with all variables defined in the program (except for __name__
1252 and sys.argv). This allows for very convenient loading of code for
1281 and sys.argv). This allows for very convenient loading of code for
1253 interactive work, while giving each program a 'clean sheet' to run in.
1282 interactive work, while giving each program a 'clean sheet' to run in.
1254
1283
1255 Options:
1284 Options:
1256
1285
1257 -n: __name__ is NOT set to '__main__', but to the running file's name
1286 -n: __name__ is NOT set to '__main__', but to the running file's name
1258 without extension (as python does under import). This allows running
1287 without extension (as python does under import). This allows running
1259 scripts and reloading the definitions in them without calling code
1288 scripts and reloading the definitions in them without calling code
1260 protected by an ' if __name__ == "__main__" ' clause.
1289 protected by an ' if __name__ == "__main__" ' clause.
1261
1290
1262 -i: run the file in IPython's namespace instead of an empty one. This
1291 -i: run the file in IPython's namespace instead of an empty one. This
1263 is useful if you are experimenting with code written in a text editor
1292 is useful if you are experimenting with code written in a text editor
1264 which depends on variables defined interactively.
1293 which depends on variables defined interactively.
1265
1294
1266 -e: ignore sys.exit() calls or SystemExit exceptions in the script
1295 -e: ignore sys.exit() calls or SystemExit exceptions in the script
1267 being run. This is particularly useful if IPython is being used to
1296 being run. This is particularly useful if IPython is being used to
1268 run unittests, which always exit with a sys.exit() call. In such
1297 run unittests, which always exit with a sys.exit() call. In such
1269 cases you are interested in the output of the test results, not in
1298 cases you are interested in the output of the test results, not in
1270 seeing a traceback of the unittest module.
1299 seeing a traceback of the unittest module.
1271
1300
1272 -t: print timing information at the end of the run. IPython will give
1301 -t: print timing information at the end of the run. IPython will give
1273 you an estimated CPU time consumption for your script, which under
1302 you an estimated CPU time consumption for your script, which under
1274 Unix uses the resource module to avoid the wraparound problems of
1303 Unix uses the resource module to avoid the wraparound problems of
1275 time.clock(). Under Unix, an estimate of time spent on system tasks
1304 time.clock(). Under Unix, an estimate of time spent on system tasks
1276 is also given (for Windows platforms this is reported as 0.0).
1305 is also given (for Windows platforms this is reported as 0.0).
1277
1306
1278 If -t is given, an additional -N<N> option can be given, where <N>
1307 If -t is given, an additional -N<N> option can be given, where <N>
1279 must be an integer indicating how many times you want the script to
1308 must be an integer indicating how many times you want the script to
1280 run. The final timing report will include total and per run results.
1309 run. The final timing report will include total and per run results.
1281
1310
1282 For example (testing the script uniq_stable.py):
1311 For example (testing the script uniq_stable.py):
1283
1312
1284 In [1]: run -t uniq_stable
1313 In [1]: run -t uniq_stable
1285
1314
1286 IPython CPU timings (estimated):\\
1315 IPython CPU timings (estimated):\\
1287 User : 0.19597 s.\\
1316 User : 0.19597 s.\\
1288 System: 0.0 s.\\
1317 System: 0.0 s.\\
1289
1318
1290 In [2]: run -t -N5 uniq_stable
1319 In [2]: run -t -N5 uniq_stable
1291
1320
1292 IPython CPU timings (estimated):\\
1321 IPython CPU timings (estimated):\\
1293 Total runs performed: 5\\
1322 Total runs performed: 5\\
1294 Times : Total Per run\\
1323 Times : Total Per run\\
1295 User : 0.910862 s, 0.1821724 s.\\
1324 User : 0.910862 s, 0.1821724 s.\\
1296 System: 0.0 s, 0.0 s.
1325 System: 0.0 s, 0.0 s.
1297
1326
1298 -d: run your program under the control of pdb, the Python debugger.
1327 -d: run your program under the control of pdb, the Python debugger.
1299 This allows you to execute your program step by step, watch variables,
1328 This allows you to execute your program step by step, watch variables,
1300 etc. Internally, what IPython does is similar to calling:
1329 etc. Internally, what IPython does is similar to calling:
1301
1330
1302 pdb.run('execfile("YOURFILENAME")')
1331 pdb.run('execfile("YOURFILENAME")')
1303
1332
1304 with a breakpoint set on line 1 of your file. You can change the line
1333 with a breakpoint set on line 1 of your file. You can change the line
1305 number for this automatic breakpoint to be <N> by using the -bN option
1334 number for this automatic breakpoint to be <N> by using the -bN option
1306 (where N must be an integer). For example:
1335 (where N must be an integer). For example:
1307
1336
1308 %run -d -b40 myscript
1337 %run -d -b40 myscript
1309
1338
1310 will set the first breakpoint at line 40 in myscript.py. Note that
1339 will set the first breakpoint at line 40 in myscript.py. Note that
1311 the first breakpoint must be set on a line which actually does
1340 the first breakpoint must be set on a line which actually does
1312 something (not a comment or docstring) for it to stop execution.
1341 something (not a comment or docstring) for it to stop execution.
1313
1342
1314 When the pdb debugger starts, you will see a (Pdb) prompt. You must
1343 When the pdb debugger starts, you will see a (Pdb) prompt. You must
1315 first enter 'c' (without qoutes) to start execution up to the first
1344 first enter 'c' (without qoutes) to start execution up to the first
1316 breakpoint.
1345 breakpoint.
1317
1346
1318 Entering 'help' gives information about the use of the debugger. You
1347 Entering 'help' gives information about the use of the debugger. You
1319 can easily see pdb's full documentation with "import pdb;pdb.help()"
1348 can easily see pdb's full documentation with "import pdb;pdb.help()"
1320 at a prompt.
1349 at a prompt.
1321
1350
1322 -p: run program under the control of the Python profiler module (which
1351 -p: run program under the control of the Python profiler module (which
1323 prints a detailed report of execution times, function calls, etc).
1352 prints a detailed report of execution times, function calls, etc).
1324
1353
1325 You can pass other options after -p which affect the behavior of the
1354 You can pass other options after -p which affect the behavior of the
1326 profiler itself. See the docs for %prun for details.
1355 profiler itself. See the docs for %prun for details.
1327
1356
1328 In this mode, the program's variables do NOT propagate back to the
1357 In this mode, the program's variables do NOT propagate back to the
1329 IPython interactive namespace (because they remain in the namespace
1358 IPython interactive namespace (because they remain in the namespace
1330 where the profiler executes them).
1359 where the profiler executes them).
1331
1360
1332 Internally this triggers a call to %prun, see its documentation for
1361 Internally this triggers a call to %prun, see its documentation for
1333 details on the options available specifically for profiling."""
1362 details on the options available specifically for profiling."""
1334
1363
1335 # get arguments and set sys.argv for program to be run.
1364 # get arguments and set sys.argv for program to be run.
1336 opts,arg_lst = self.parse_options(parameter_s,'nidtN:b:pD:l:rs:T:e',
1365 opts,arg_lst = self.parse_options(parameter_s,'nidtN:b:pD:l:rs:T:e',
1337 mode='list',list_all=1)
1366 mode='list',list_all=1)
1338
1367
1339 try:
1368 try:
1340 filename = get_py_filename(arg_lst[0])
1369 filename = get_py_filename(arg_lst[0])
1341 except IndexError:
1370 except IndexError:
1342 warn('you must provide at least a filename.')
1371 warn('you must provide at least a filename.')
1343 print '\n%run:\n',OInspect.getdoc(self.magic_run)
1372 print '\n%run:\n',OInspect.getdoc(self.magic_run)
1344 return
1373 return
1345 except IOError,msg:
1374 except IOError,msg:
1346 error(msg)
1375 error(msg)
1347 return
1376 return
1348
1377
1349 # Control the response to exit() calls made by the script being run
1378 # Control the response to exit() calls made by the script being run
1350 exit_ignore = opts.has_key('e')
1379 exit_ignore = opts.has_key('e')
1351
1380
1352 # Make sure that the running script gets a proper sys.argv as if it
1381 # Make sure that the running script gets a proper sys.argv as if it
1353 # were run from a system shell.
1382 # were run from a system shell.
1354 save_argv = sys.argv # save it for later restoring
1383 save_argv = sys.argv # save it for later restoring
1355 sys.argv = [filename]+ arg_lst[1:] # put in the proper filename
1384 sys.argv = [filename]+ arg_lst[1:] # put in the proper filename
1356
1385
1357 if opts.has_key('i'):
1386 if opts.has_key('i'):
1358 prog_ns = self.shell.user_ns
1387 prog_ns = self.shell.user_ns
1359 __name__save = self.shell.user_ns['__name__']
1388 __name__save = self.shell.user_ns['__name__']
1360 prog_ns['__name__'] = '__main__'
1389 prog_ns['__name__'] = '__main__'
1361 else:
1390 else:
1362 if opts.has_key('n'):
1391 if opts.has_key('n'):
1363 name = os.path.splitext(os.path.basename(filename))[0]
1392 name = os.path.splitext(os.path.basename(filename))[0]
1364 else:
1393 else:
1365 name = '__main__'
1394 name = '__main__'
1366 prog_ns = {'__name__':name}
1395 prog_ns = {'__name__':name}
1367
1396
1368 # pickle fix. See iplib for an explanation. But we need to make sure
1397 # pickle fix. See iplib for an explanation. But we need to make sure
1369 # that, if we overwrite __main__, we replace it at the end
1398 # that, if we overwrite __main__, we replace it at the end
1370 if prog_ns['__name__'] == '__main__':
1399 if prog_ns['__name__'] == '__main__':
1371 restore_main = sys.modules['__main__']
1400 restore_main = sys.modules['__main__']
1372 else:
1401 else:
1373 restore_main = False
1402 restore_main = False
1374
1403
1375 sys.modules[prog_ns['__name__']] = FakeModule(prog_ns)
1404 sys.modules[prog_ns['__name__']] = FakeModule(prog_ns)
1376
1405
1377 stats = None
1406 stats = None
1378 try:
1407 try:
1379 if opts.has_key('p'):
1408 if opts.has_key('p'):
1380 stats = self.magic_prun('',0,opts,arg_lst,prog_ns)
1409 stats = self.magic_prun('',0,opts,arg_lst,prog_ns)
1381 else:
1410 else:
1382 if opts.has_key('d'):
1411 if opts.has_key('d'):
1383 deb = Debugger.Pdb(self.shell.rc.colors)
1412 deb = Debugger.Pdb(self.shell.rc.colors)
1384 # reset Breakpoint state, which is moronically kept
1413 # reset Breakpoint state, which is moronically kept
1385 # in a class
1414 # in a class
1386 bdb.Breakpoint.next = 1
1415 bdb.Breakpoint.next = 1
1387 bdb.Breakpoint.bplist = {}
1416 bdb.Breakpoint.bplist = {}
1388 bdb.Breakpoint.bpbynumber = [None]
1417 bdb.Breakpoint.bpbynumber = [None]
1389 # Set an initial breakpoint to stop execution
1418 # Set an initial breakpoint to stop execution
1390 maxtries = 10
1419 maxtries = 10
1391 bp = int(opts.get('b',[1])[0])
1420 bp = int(opts.get('b',[1])[0])
1392 checkline = deb.checkline(filename,bp)
1421 checkline = deb.checkline(filename,bp)
1393 if not checkline:
1422 if not checkline:
1394 for bp in range(bp+1,bp+maxtries+1):
1423 for bp in range(bp+1,bp+maxtries+1):
1395 if deb.checkline(filename,bp):
1424 if deb.checkline(filename,bp):
1396 break
1425 break
1397 else:
1426 else:
1398 msg = ("\nI failed to find a valid line to set "
1427 msg = ("\nI failed to find a valid line to set "
1399 "a breakpoint\n"
1428 "a breakpoint\n"
1400 "after trying up to line: %s.\n"
1429 "after trying up to line: %s.\n"
1401 "Please set a valid breakpoint manually "
1430 "Please set a valid breakpoint manually "
1402 "with the -b option." % bp)
1431 "with the -b option." % bp)
1403 error(msg)
1432 error(msg)
1404 return
1433 return
1405 # if we find a good linenumber, set the breakpoint
1434 # if we find a good linenumber, set the breakpoint
1406 deb.do_break('%s:%s' % (filename,bp))
1435 deb.do_break('%s:%s' % (filename,bp))
1407 # Start file run
1436 # Start file run
1408 print "NOTE: Enter 'c' at the",
1437 print "NOTE: Enter 'c' at the",
1409 print "ipdb> prompt to start your script."
1438 print "ipdb> prompt to start your script."
1410 try:
1439 try:
1411 deb.run('execfile("%s")' % filename,prog_ns)
1440 deb.run('execfile("%s")' % filename,prog_ns)
1412 except:
1441 except:
1413 etype, value, tb = sys.exc_info()
1442 etype, value, tb = sys.exc_info()
1414 # Skip three frames in the traceback: the %run one,
1443 # Skip three frames in the traceback: the %run one,
1415 # one inside bdb.py, and the command-line typed by the
1444 # one inside bdb.py, and the command-line typed by the
1416 # user (run by exec in pdb itself).
1445 # user (run by exec in pdb itself).
1417 self.shell.InteractiveTB(etype,value,tb,tb_offset=3)
1446 self.shell.InteractiveTB(etype,value,tb,tb_offset=3)
1418 else:
1447 else:
1419 if runner is None:
1448 if runner is None:
1420 runner = self.shell.safe_execfile
1449 runner = self.shell.safe_execfile
1421 if opts.has_key('t'):
1450 if opts.has_key('t'):
1422 try:
1451 try:
1423 nruns = int(opts['N'][0])
1452 nruns = int(opts['N'][0])
1424 if nruns < 1:
1453 if nruns < 1:
1425 error('Number of runs must be >=1')
1454 error('Number of runs must be >=1')
1426 return
1455 return
1427 except (KeyError):
1456 except (KeyError):
1428 nruns = 1
1457 nruns = 1
1429 if nruns == 1:
1458 if nruns == 1:
1430 t0 = clock2()
1459 t0 = clock2()
1431 runner(filename,prog_ns,prog_ns,exit_ignore=exit_ignore)
1460 runner(filename,prog_ns,prog_ns,exit_ignore=exit_ignore)
1432 t1 = clock2()
1461 t1 = clock2()
1433 t_usr = t1[0]-t0[0]
1462 t_usr = t1[0]-t0[0]
1434 t_sys = t1[1]-t1[1]
1463 t_sys = t1[1]-t1[1]
1435 print "\nIPython CPU timings (estimated):"
1464 print "\nIPython CPU timings (estimated):"
1436 print " User : %10s s." % t_usr
1465 print " User : %10s s." % t_usr
1437 print " System: %10s s." % t_sys
1466 print " System: %10s s." % t_sys
1438 else:
1467 else:
1439 runs = range(nruns)
1468 runs = range(nruns)
1440 t0 = clock2()
1469 t0 = clock2()
1441 for nr in runs:
1470 for nr in runs:
1442 runner(filename,prog_ns,prog_ns,exit_ignore=exit_ignore)
1471 runner(filename,prog_ns,prog_ns,exit_ignore=exit_ignore)
1443 t1 = clock2()
1472 t1 = clock2()
1444 t_usr = t1[0]-t0[0]
1473 t_usr = t1[0]-t0[0]
1445 t_sys = t1[1]-t1[1]
1474 t_sys = t1[1]-t1[1]
1446 print "\nIPython CPU timings (estimated):"
1475 print "\nIPython CPU timings (estimated):"
1447 print "Total runs performed:",nruns
1476 print "Total runs performed:",nruns
1448 print " Times : %10s %10s" % ('Total','Per run')
1477 print " Times : %10s %10s" % ('Total','Per run')
1449 print " User : %10s s, %10s s." % (t_usr,t_usr/nruns)
1478 print " User : %10s s, %10s s." % (t_usr,t_usr/nruns)
1450 print " System: %10s s, %10s s." % (t_sys,t_sys/nruns)
1479 print " System: %10s s, %10s s." % (t_sys,t_sys/nruns)
1451
1480
1452 else:
1481 else:
1453 runner(filename,prog_ns,prog_ns,exit_ignore=exit_ignore)
1482 runner(filename,prog_ns,prog_ns,exit_ignore=exit_ignore)
1454 if opts.has_key('i'):
1483 if opts.has_key('i'):
1455 self.shell.user_ns['__name__'] = __name__save
1484 self.shell.user_ns['__name__'] = __name__save
1456 else:
1485 else:
1457 # update IPython interactive namespace
1486 # update IPython interactive namespace
1458 del prog_ns['__name__']
1487 del prog_ns['__name__']
1459 self.shell.user_ns.update(prog_ns)
1488 self.shell.user_ns.update(prog_ns)
1460 finally:
1489 finally:
1461 sys.argv = save_argv
1490 sys.argv = save_argv
1462 if restore_main:
1491 if restore_main:
1463 sys.modules['__main__'] = restore_main
1492 sys.modules['__main__'] = restore_main
1464 return stats
1493 return stats
1465
1494
1466 def magic_runlog(self, parameter_s =''):
1495 def magic_runlog(self, parameter_s =''):
1467 """Run files as logs.
1496 """Run files as logs.
1468
1497
1469 Usage:\\
1498 Usage:\\
1470 %runlog file1 file2 ...
1499 %runlog file1 file2 ...
1471
1500
1472 Run the named files (treating them as log files) in sequence inside
1501 Run the named files (treating them as log files) in sequence inside
1473 the interpreter, and return to the prompt. This is much slower than
1502 the interpreter, and return to the prompt. This is much slower than
1474 %run because each line is executed in a try/except block, but it
1503 %run because each line is executed in a try/except block, but it
1475 allows running files with syntax errors in them.
1504 allows running files with syntax errors in them.
1476
1505
1477 Normally IPython will guess when a file is one of its own logfiles, so
1506 Normally IPython will guess when a file is one of its own logfiles, so
1478 you can typically use %run even for logs. This shorthand allows you to
1507 you can typically use %run even for logs. This shorthand allows you to
1479 force any file to be treated as a log file."""
1508 force any file to be treated as a log file."""
1480
1509
1481 for f in parameter_s.split():
1510 for f in parameter_s.split():
1482 self.shell.safe_execfile(f,self.shell.user_ns,
1511 self.shell.safe_execfile(f,self.shell.user_ns,
1483 self.shell.user_ns,islog=1)
1512 self.shell.user_ns,islog=1)
1484
1513
1485 def magic_time(self,parameter_s = ''):
1514 def magic_time(self,parameter_s = ''):
1486 """Time execution of a Python statement or expression.
1515 """Time execution of a Python statement or expression.
1487
1516
1488 The CPU and wall clock times are printed, and the value of the
1517 The CPU and wall clock times are printed, and the value of the
1489 expression (if any) is returned. Note that under Win32, system time
1518 expression (if any) is returned. Note that under Win32, system time
1490 is always reported as 0, since it can not be measured.
1519 is always reported as 0, since it can not be measured.
1491
1520
1492 This function provides very basic timing functionality. In Python
1521 This function provides very basic timing functionality. In Python
1493 2.3, the timeit module offers more control and sophistication, but for
1522 2.3, the timeit module offers more control and sophistication, but for
1494 now IPython supports Python 2.2, so we can not rely on timeit being
1523 now IPython supports Python 2.2, so we can not rely on timeit being
1495 present.
1524 present.
1496
1525
1497 Some examples:
1526 Some examples:
1498
1527
1499 In [1]: time 2**128
1528 In [1]: time 2**128
1500 CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s
1529 CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s
1501 Wall time: 0.00
1530 Wall time: 0.00
1502 Out[1]: 340282366920938463463374607431768211456L
1531 Out[1]: 340282366920938463463374607431768211456L
1503
1532
1504 In [2]: n = 1000000
1533 In [2]: n = 1000000
1505
1534
1506 In [3]: time sum(range(n))
1535 In [3]: time sum(range(n))
1507 CPU times: user 1.20 s, sys: 0.05 s, total: 1.25 s
1536 CPU times: user 1.20 s, sys: 0.05 s, total: 1.25 s
1508 Wall time: 1.37
1537 Wall time: 1.37
1509 Out[3]: 499999500000L
1538 Out[3]: 499999500000L
1510
1539
1511 In [4]: time print 'hello world'
1540 In [4]: time print 'hello world'
1512 hello world
1541 hello world
1513 CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s
1542 CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s
1514 Wall time: 0.00
1543 Wall time: 0.00
1515 """
1544 """
1516
1545
1517 # fail immediately if the given expression can't be compiled
1546 # fail immediately if the given expression can't be compiled
1518 try:
1547 try:
1519 mode = 'eval'
1548 mode = 'eval'
1520 code = compile(parameter_s,'<timed eval>',mode)
1549 code = compile(parameter_s,'<timed eval>',mode)
1521 except SyntaxError:
1550 except SyntaxError:
1522 mode = 'exec'
1551 mode = 'exec'
1523 code = compile(parameter_s,'<timed exec>',mode)
1552 code = compile(parameter_s,'<timed exec>',mode)
1524 # skew measurement as little as possible
1553 # skew measurement as little as possible
1525 glob = self.shell.user_ns
1554 glob = self.shell.user_ns
1526 clk = clock2
1555 clk = clock2
1527 wtime = time.time
1556 wtime = time.time
1528 # time execution
1557 # time execution
1529 wall_st = wtime()
1558 wall_st = wtime()
1530 if mode=='eval':
1559 if mode=='eval':
1531 st = clk()
1560 st = clk()
1532 out = eval(code,glob)
1561 out = eval(code,glob)
1533 end = clk()
1562 end = clk()
1534 else:
1563 else:
1535 st = clk()
1564 st = clk()
1536 exec code in glob
1565 exec code in glob
1537 end = clk()
1566 end = clk()
1538 out = None
1567 out = None
1539 wall_end = wtime()
1568 wall_end = wtime()
1540 # Compute actual times and report
1569 # Compute actual times and report
1541 wall_time = wall_end-wall_st
1570 wall_time = wall_end-wall_st
1542 cpu_user = end[0]-st[0]
1571 cpu_user = end[0]-st[0]
1543 cpu_sys = end[1]-st[1]
1572 cpu_sys = end[1]-st[1]
1544 cpu_tot = cpu_user+cpu_sys
1573 cpu_tot = cpu_user+cpu_sys
1545 print "CPU times: user %.2f s, sys: %.2f s, total: %.2f s" % \
1574 print "CPU times: user %.2f s, sys: %.2f s, total: %.2f s" % \
1546 (cpu_user,cpu_sys,cpu_tot)
1575 (cpu_user,cpu_sys,cpu_tot)
1547 print "Wall time: %.2f" % wall_time
1576 print "Wall time: %.2f" % wall_time
1548 return out
1577 return out
1549
1578
1550 def magic_macro(self,parameter_s = ''):
1579 def magic_macro(self,parameter_s = ''):
1551 """Define a set of input lines as a macro for future re-execution.
1580 """Define a set of input lines as a macro for future re-execution.
1552
1581
1553 Usage:\\
1582 Usage:\\
1554 %macro name n1-n2 n3-n4 ... n5 .. n6 ...
1583 %macro name n1-n2 n3-n4 ... n5 .. n6 ...
1555
1584
1556 This will define a global variable called `name` which is a string
1585 This will define a global variable called `name` which is a string
1557 made of joining the slices and lines you specify (n1,n2,... numbers
1586 made of joining the slices and lines you specify (n1,n2,... numbers
1558 above) from your input history into a single string. This variable
1587 above) from your input history into a single string. This variable
1559 acts like an automatic function which re-executes those lines as if
1588 acts like an automatic function which re-executes those lines as if
1560 you had typed them. You just type 'name' at the prompt and the code
1589 you had typed them. You just type 'name' at the prompt and the code
1561 executes.
1590 executes.
1562
1591
1563 The notation for indicating number ranges is: n1-n2 means 'use line
1592 The notation for indicating number ranges is: n1-n2 means 'use line
1564 numbers n1,...n2' (the endpoint is included). That is, '5-7' means
1593 numbers n1,...n2' (the endpoint is included). That is, '5-7' means
1565 using the lines numbered 5,6 and 7.
1594 using the lines numbered 5,6 and 7.
1566
1595
1567 Note: as a 'hidden' feature, you can also use traditional python slice
1596 Note: as a 'hidden' feature, you can also use traditional python slice
1568 notation, where N:M means numbers N through M-1.
1597 notation, where N:M means numbers N through M-1.
1569
1598
1570 For example, if your history contains (%hist prints it):
1599 For example, if your history contains (%hist prints it):
1571
1600
1572 44: x=1\\
1601 44: x=1\\
1573 45: y=3\\
1602 45: y=3\\
1574 46: z=x+y\\
1603 46: z=x+y\\
1575 47: print x\\
1604 47: print x\\
1576 48: a=5\\
1605 48: a=5\\
1577 49: print 'x',x,'y',y\\
1606 49: print 'x',x,'y',y\\
1578
1607
1579 you can create a macro with lines 44 through 47 (included) and line 49
1608 you can create a macro with lines 44 through 47 (included) and line 49
1580 called my_macro with:
1609 called my_macro with:
1581
1610
1582 In [51]: %macro my_macro 44-47 49
1611 In [51]: %macro my_macro 44-47 49
1583
1612
1584 Now, typing `my_macro` (without quotes) will re-execute all this code
1613 Now, typing `my_macro` (without quotes) will re-execute all this code
1585 in one pass.
1614 in one pass.
1586
1615
1587 You don't need to give the line-numbers in order, and any given line
1616 You don't need to give the line-numbers in order, and any given line
1588 number can appear multiple times. You can assemble macros with any
1617 number can appear multiple times. You can assemble macros with any
1589 lines from your input history in any order.
1618 lines from your input history in any order.
1590
1619
1591 The macro is a simple object which holds its value in an attribute,
1620 The macro is a simple object which holds its value in an attribute,
1592 but IPython's display system checks for macros and executes them as
1621 but IPython's display system checks for macros and executes them as
1593 code instead of printing them when you type their name.
1622 code instead of printing them when you type their name.
1594
1623
1595 You can view a macro's contents by explicitly printing it with:
1624 You can view a macro's contents by explicitly printing it with:
1596
1625
1597 'print macro_name'.
1626 'print macro_name'.
1598
1627
1599 For one-off cases which DON'T contain magic function calls in them you
1628 For one-off cases which DON'T contain magic function calls in them you
1600 can obtain similar results by explicitly executing slices from your
1629 can obtain similar results by explicitly executing slices from your
1601 input history with:
1630 input history with:
1602
1631
1603 In [60]: exec In[44:48]+In[49]"""
1632 In [60]: exec In[44:48]+In[49]"""
1604
1633
1605 args = parameter_s.split()
1634 args = parameter_s.split()
1606 name,ranges = args[0], args[1:]
1635 name,ranges = args[0], args[1:]
1607 #print 'rng',ranges # dbg
1636 #print 'rng',ranges # dbg
1608 lines = self.extract_input_slices(ranges)
1637 lines = self.extract_input_slices(ranges)
1609 macro = Macro(lines)
1638 macro = Macro(lines)
1610 self.shell.user_ns.update({name:macro})
1639 self.shell.user_ns.update({name:macro})
1611 print 'Macro `%s` created. To execute, type its name (without quotes).' % name
1640 print 'Macro `%s` created. To execute, type its name (without quotes).' % name
1612 print 'Macro contents:'
1641 print 'Macro contents:'
1613 print macro,
1642 print macro,
1614
1643
1615 def magic_save(self,parameter_s = ''):
1644 def magic_save(self,parameter_s = ''):
1616 """Save a set of lines to a given filename.
1645 """Save a set of lines to a given filename.
1617
1646
1618 Usage:\\
1647 Usage:\\
1619 %save filename n1-n2 n3-n4 ... n5 .. n6 ...
1648 %save filename n1-n2 n3-n4 ... n5 .. n6 ...
1620
1649
1621 This function uses the same syntax as %macro for line extraction, but
1650 This function uses the same syntax as %macro for line extraction, but
1622 instead of creating a macro it saves the resulting string to the
1651 instead of creating a macro it saves the resulting string to the
1623 filename you specify.
1652 filename you specify.
1624
1653
1625 It adds a '.py' extension to the file if you don't do so yourself, and
1654 It adds a '.py' extension to the file if you don't do so yourself, and
1626 it asks for confirmation before overwriting existing files."""
1655 it asks for confirmation before overwriting existing files."""
1627
1656
1628 args = parameter_s.split()
1657 args = parameter_s.split()
1629 fname,ranges = args[0], args[1:]
1658 fname,ranges = args[0], args[1:]
1630 if not fname.endswith('.py'):
1659 if not fname.endswith('.py'):
1631 fname += '.py'
1660 fname += '.py'
1632 if os.path.isfile(fname):
1661 if os.path.isfile(fname):
1633 ans = raw_input('File `%s` exists. Overwrite (y/[N])? ' % fname)
1662 ans = raw_input('File `%s` exists. Overwrite (y/[N])? ' % fname)
1634 if ans.lower() not in ['y','yes']:
1663 if ans.lower() not in ['y','yes']:
1635 print 'Operation cancelled.'
1664 print 'Operation cancelled.'
1636 return
1665 return
1637 cmds = ''.join(self.extract_input_slices(ranges))
1666 cmds = ''.join(self.extract_input_slices(ranges))
1638 f = file(fname,'w')
1667 f = file(fname,'w')
1639 f.write(cmds)
1668 f.write(cmds)
1640 f.close()
1669 f.close()
1641 print 'The following commands were written to file `%s`:' % fname
1670 print 'The following commands were written to file `%s`:' % fname
1642 print cmds
1671 print cmds
1643
1672
1644 def _edit_macro(self,mname,macro):
1673 def _edit_macro(self,mname,macro):
1645 """open an editor with the macro data in a file"""
1674 """open an editor with the macro data in a file"""
1646 filename = self.shell.mktempfile(macro.value)
1675 filename = self.shell.mktempfile(macro.value)
1647 self.shell.hooks.editor(filename)
1676 self.shell.hooks.editor(filename)
1648
1677
1649 # and make a new macro object, to replace the old one
1678 # and make a new macro object, to replace the old one
1650 mfile = open(filename)
1679 mfile = open(filename)
1651 mvalue = mfile.read()
1680 mvalue = mfile.read()
1652 mfile.close()
1681 mfile.close()
1653 self.shell.user_ns[mname] = Macro(mvalue)
1682 self.shell.user_ns[mname] = Macro(mvalue)
1654
1683
1655 def magic_ed(self,parameter_s=''):
1684 def magic_ed(self,parameter_s=''):
1656 """Alias to %edit."""
1685 """Alias to %edit."""
1657 return self.magic_edit(parameter_s)
1686 return self.magic_edit(parameter_s)
1658
1687
1659 def magic_edit(self,parameter_s='',last_call=['','']):
1688 def magic_edit(self,parameter_s='',last_call=['','']):
1660 """Bring up an editor and execute the resulting code.
1689 """Bring up an editor and execute the resulting code.
1661
1690
1662 Usage:
1691 Usage:
1663 %edit [options] [args]
1692 %edit [options] [args]
1664
1693
1665 %edit runs IPython's editor hook. The default version of this hook is
1694 %edit runs IPython's editor hook. The default version of this hook is
1666 set to call the __IPYTHON__.rc.editor command. This is read from your
1695 set to call the __IPYTHON__.rc.editor command. This is read from your
1667 environment variable $EDITOR. If this isn't found, it will default to
1696 environment variable $EDITOR. If this isn't found, it will default to
1668 vi under Linux/Unix and to notepad under Windows. See the end of this
1697 vi under Linux/Unix and to notepad under Windows. See the end of this
1669 docstring for how to change the editor hook.
1698 docstring for how to change the editor hook.
1670
1699
1671 You can also set the value of this editor via the command line option
1700 You can also set the value of this editor via the command line option
1672 '-editor' or in your ipythonrc file. This is useful if you wish to use
1701 '-editor' or in your ipythonrc file. This is useful if you wish to use
1673 specifically for IPython an editor different from your typical default
1702 specifically for IPython an editor different from your typical default
1674 (and for Windows users who typically don't set environment variables).
1703 (and for Windows users who typically don't set environment variables).
1675
1704
1676 This command allows you to conveniently edit multi-line code right in
1705 This command allows you to conveniently edit multi-line code right in
1677 your IPython session.
1706 your IPython session.
1678
1707
1679 If called without arguments, %edit opens up an empty editor with a
1708 If called without arguments, %edit opens up an empty editor with a
1680 temporary file and will execute the contents of this file when you
1709 temporary file and will execute the contents of this file when you
1681 close it (don't forget to save it!).
1710 close it (don't forget to save it!).
1682
1711
1683 Options:
1712 Options:
1684
1713
1685 -p: this will call the editor with the same data as the previous time
1714 -p: this will call the editor with the same data as the previous time
1686 it was used, regardless of how long ago (in your current session) it
1715 it was used, regardless of how long ago (in your current session) it
1687 was.
1716 was.
1688
1717
1689 -x: do not execute the edited code immediately upon exit. This is
1718 -x: do not execute the edited code immediately upon exit. This is
1690 mainly useful if you are editing programs which need to be called with
1719 mainly useful if you are editing programs which need to be called with
1691 command line arguments, which you can then do using %run.
1720 command line arguments, which you can then do using %run.
1692
1721
1693 Arguments:
1722 Arguments:
1694
1723
1695 If arguments are given, the following possibilites exist:
1724 If arguments are given, the following possibilites exist:
1696
1725
1697 - The arguments are numbers or pairs of colon-separated numbers (like
1726 - The arguments are numbers or pairs of colon-separated numbers (like
1698 1 4:8 9). These are interpreted as lines of previous input to be
1727 1 4:8 9). These are interpreted as lines of previous input to be
1699 loaded into the editor. The syntax is the same of the %macro command.
1728 loaded into the editor. The syntax is the same of the %macro command.
1700
1729
1701 - If the argument doesn't start with a number, it is evaluated as a
1730 - If the argument doesn't start with a number, it is evaluated as a
1702 variable and its contents loaded into the editor. You can thus edit
1731 variable and its contents loaded into the editor. You can thus edit
1703 any string which contains python code (including the result of
1732 any string which contains python code (including the result of
1704 previous edits).
1733 previous edits).
1705
1734
1706 - If the argument is the name of an object (other than a string),
1735 - If the argument is the name of an object (other than a string),
1707 IPython will try to locate the file where it was defined and open the
1736 IPython will try to locate the file where it was defined and open the
1708 editor at the point where it is defined. You can use `%edit function`
1737 editor at the point where it is defined. You can use `%edit function`
1709 to load an editor exactly at the point where 'function' is defined,
1738 to load an editor exactly at the point where 'function' is defined,
1710 edit it and have the file be executed automatically.
1739 edit it and have the file be executed automatically.
1711
1740
1712 If the object is a macro (see %macro for details), this opens up your
1741 If the object is a macro (see %macro for details), this opens up your
1713 specified editor with a temporary file containing the macro's data.
1742 specified editor with a temporary file containing the macro's data.
1714 Upon exit, the macro is reloaded with the contents of the file.
1743 Upon exit, the macro is reloaded with the contents of the file.
1715
1744
1716 Note: opening at an exact line is only supported under Unix, and some
1745 Note: opening at an exact line is only supported under Unix, and some
1717 editors (like kedit and gedit up to Gnome 2.8) do not understand the
1746 editors (like kedit and gedit up to Gnome 2.8) do not understand the
1718 '+NUMBER' parameter necessary for this feature. Good editors like
1747 '+NUMBER' parameter necessary for this feature. Good editors like
1719 (X)Emacs, vi, jed, pico and joe all do.
1748 (X)Emacs, vi, jed, pico and joe all do.
1720
1749
1721 - If the argument is not found as a variable, IPython will look for a
1750 - If the argument is not found as a variable, IPython will look for a
1722 file with that name (adding .py if necessary) and load it into the
1751 file with that name (adding .py if necessary) and load it into the
1723 editor. It will execute its contents with execfile() when you exit,
1752 editor. It will execute its contents with execfile() when you exit,
1724 loading any code in the file into your interactive namespace.
1753 loading any code in the file into your interactive namespace.
1725
1754
1726 After executing your code, %edit will return as output the code you
1755 After executing your code, %edit will return as output the code you
1727 typed in the editor (except when it was an existing file). This way
1756 typed in the editor (except when it was an existing file). This way
1728 you can reload the code in further invocations of %edit as a variable,
1757 you can reload the code in further invocations of %edit as a variable,
1729 via _<NUMBER> or Out[<NUMBER>], where <NUMBER> is the prompt number of
1758 via _<NUMBER> or Out[<NUMBER>], where <NUMBER> is the prompt number of
1730 the output.
1759 the output.
1731
1760
1732 Note that %edit is also available through the alias %ed.
1761 Note that %edit is also available through the alias %ed.
1733
1762
1734 This is an example of creating a simple function inside the editor and
1763 This is an example of creating a simple function inside the editor and
1735 then modifying it. First, start up the editor:
1764 then modifying it. First, start up the editor:
1736
1765
1737 In [1]: ed\\
1766 In [1]: ed\\
1738 Editing... done. Executing edited code...\\
1767 Editing... done. Executing edited code...\\
1739 Out[1]: 'def foo():\\n print "foo() was defined in an editing session"\\n'
1768 Out[1]: 'def foo():\\n print "foo() was defined in an editing session"\\n'
1740
1769
1741 We can then call the function foo():
1770 We can then call the function foo():
1742
1771
1743 In [2]: foo()\\
1772 In [2]: foo()\\
1744 foo() was defined in an editing session
1773 foo() was defined in an editing session
1745
1774
1746 Now we edit foo. IPython automatically loads the editor with the
1775 Now we edit foo. IPython automatically loads the editor with the
1747 (temporary) file where foo() was previously defined:
1776 (temporary) file where foo() was previously defined:
1748
1777
1749 In [3]: ed foo\\
1778 In [3]: ed foo\\
1750 Editing... done. Executing edited code...
1779 Editing... done. Executing edited code...
1751
1780
1752 And if we call foo() again we get the modified version:
1781 And if we call foo() again we get the modified version:
1753
1782
1754 In [4]: foo()\\
1783 In [4]: foo()\\
1755 foo() has now been changed!
1784 foo() has now been changed!
1756
1785
1757 Here is an example of how to edit a code snippet successive
1786 Here is an example of how to edit a code snippet successive
1758 times. First we call the editor:
1787 times. First we call the editor:
1759
1788
1760 In [8]: ed\\
1789 In [8]: ed\\
1761 Editing... done. Executing edited code...\\
1790 Editing... done. Executing edited code...\\
1762 hello\\
1791 hello\\
1763 Out[8]: "print 'hello'\\n"
1792 Out[8]: "print 'hello'\\n"
1764
1793
1765 Now we call it again with the previous output (stored in _):
1794 Now we call it again with the previous output (stored in _):
1766
1795
1767 In [9]: ed _\\
1796 In [9]: ed _\\
1768 Editing... done. Executing edited code...\\
1797 Editing... done. Executing edited code...\\
1769 hello world\\
1798 hello world\\
1770 Out[9]: "print 'hello world'\\n"
1799 Out[9]: "print 'hello world'\\n"
1771
1800
1772 Now we call it with the output #8 (stored in _8, also as Out[8]):
1801 Now we call it with the output #8 (stored in _8, also as Out[8]):
1773
1802
1774 In [10]: ed _8\\
1803 In [10]: ed _8\\
1775 Editing... done. Executing edited code...\\
1804 Editing... done. Executing edited code...\\
1776 hello again\\
1805 hello again\\
1777 Out[10]: "print 'hello again'\\n"
1806 Out[10]: "print 'hello again'\\n"
1778
1807
1779
1808
1780 Changing the default editor hook:
1809 Changing the default editor hook:
1781
1810
1782 If you wish to write your own editor hook, you can put it in a
1811 If you wish to write your own editor hook, you can put it in a
1783 configuration file which you load at startup time. The default hook
1812 configuration file which you load at startup time. The default hook
1784 is defined in the IPython.hooks module, and you can use that as a
1813 is defined in the IPython.hooks module, and you can use that as a
1785 starting example for further modifications. That file also has
1814 starting example for further modifications. That file also has
1786 general instructions on how to set a new hook for use once you've
1815 general instructions on how to set a new hook for use once you've
1787 defined it."""
1816 defined it."""
1788
1817
1789 # FIXME: This function has become a convoluted mess. It needs a
1818 # FIXME: This function has become a convoluted mess. It needs a
1790 # ground-up rewrite with clean, simple logic.
1819 # ground-up rewrite with clean, simple logic.
1791
1820
1792 def make_filename(arg):
1821 def make_filename(arg):
1793 "Make a filename from the given args"
1822 "Make a filename from the given args"
1794 try:
1823 try:
1795 filename = get_py_filename(arg)
1824 filename = get_py_filename(arg)
1796 except IOError:
1825 except IOError:
1797 if args.endswith('.py'):
1826 if args.endswith('.py'):
1798 filename = arg
1827 filename = arg
1799 else:
1828 else:
1800 filename = None
1829 filename = None
1801 return filename
1830 return filename
1802
1831
1803 # custom exceptions
1832 # custom exceptions
1804 class DataIsObject(Exception): pass
1833 class DataIsObject(Exception): pass
1805
1834
1806 opts,args = self.parse_options(parameter_s,'px')
1835 opts,args = self.parse_options(parameter_s,'px')
1807
1836
1808 # Default line number value
1837 # Default line number value
1809 lineno = None
1838 lineno = None
1810 if opts.has_key('p'):
1839 if opts.has_key('p'):
1811 args = '_%s' % last_call[0]
1840 args = '_%s' % last_call[0]
1812 if not self.shell.user_ns.has_key(args):
1841 if not self.shell.user_ns.has_key(args):
1813 args = last_call[1]
1842 args = last_call[1]
1814
1843
1815 # use last_call to remember the state of the previous call, but don't
1844 # use last_call to remember the state of the previous call, but don't
1816 # let it be clobbered by successive '-p' calls.
1845 # let it be clobbered by successive '-p' calls.
1817 try:
1846 try:
1818 last_call[0] = self.shell.outputcache.prompt_count
1847 last_call[0] = self.shell.outputcache.prompt_count
1819 if not opts.has_key('p'):
1848 if not opts.has_key('p'):
1820 last_call[1] = parameter_s
1849 last_call[1] = parameter_s
1821 except:
1850 except:
1822 pass
1851 pass
1823
1852
1824 # by default this is done with temp files, except when the given
1853 # by default this is done with temp files, except when the given
1825 # arg is a filename
1854 # arg is a filename
1826 use_temp = 1
1855 use_temp = 1
1827
1856
1828 if re.match(r'\d',args):
1857 if re.match(r'\d',args):
1829 # Mode where user specifies ranges of lines, like in %macro.
1858 # Mode where user specifies ranges of lines, like in %macro.
1830 # This means that you can't edit files whose names begin with
1859 # This means that you can't edit files whose names begin with
1831 # numbers this way. Tough.
1860 # numbers this way. Tough.
1832 ranges = args.split()
1861 ranges = args.split()
1833 data = ''.join(self.extract_input_slices(ranges))
1862 data = ''.join(self.extract_input_slices(ranges))
1834 elif args.endswith('.py'):
1863 elif args.endswith('.py'):
1835 filename = make_filename(args)
1864 filename = make_filename(args)
1836 data = ''
1865 data = ''
1837 use_temp = 0
1866 use_temp = 0
1838 elif args:
1867 elif args:
1839 try:
1868 try:
1840 # Load the parameter given as a variable. If not a string,
1869 # Load the parameter given as a variable. If not a string,
1841 # process it as an object instead (below)
1870 # process it as an object instead (below)
1842
1871
1843 #print '*** args',args,'type',type(args) # dbg
1872 #print '*** args',args,'type',type(args) # dbg
1844 data = eval(args,self.shell.user_ns)
1873 data = eval(args,self.shell.user_ns)
1845 if not type(data) in StringTypes:
1874 if not type(data) in StringTypes:
1846 raise DataIsObject
1875 raise DataIsObject
1847
1876
1848 except (NameError,SyntaxError):
1877 except (NameError,SyntaxError):
1849 # given argument is not a variable, try as a filename
1878 # given argument is not a variable, try as a filename
1850 filename = make_filename(args)
1879 filename = make_filename(args)
1851 if filename is None:
1880 if filename is None:
1852 warn("Argument given (%s) can't be found as a variable "
1881 warn("Argument given (%s) can't be found as a variable "
1853 "or as a filename." % args)
1882 "or as a filename." % args)
1854 return
1883 return
1855
1884
1856 data = ''
1885 data = ''
1857 use_temp = 0
1886 use_temp = 0
1858 except DataIsObject:
1887 except DataIsObject:
1859
1888
1860 # macros have a special edit function
1889 # macros have a special edit function
1861 if isinstance(data,Macro):
1890 if isinstance(data,Macro):
1862 self._edit_macro(args,data)
1891 self._edit_macro(args,data)
1863 return
1892 return
1864
1893
1865 # For objects, try to edit the file where they are defined
1894 # For objects, try to edit the file where they are defined
1866 try:
1895 try:
1867 filename = inspect.getabsfile(data)
1896 filename = inspect.getabsfile(data)
1868 datafile = 1
1897 datafile = 1
1869 except TypeError:
1898 except TypeError:
1870 filename = make_filename(args)
1899 filename = make_filename(args)
1871 datafile = 1
1900 datafile = 1
1872 warn('Could not find file where `%s` is defined.\n'
1901 warn('Could not find file where `%s` is defined.\n'
1873 'Opening a file named `%s`' % (args,filename))
1902 'Opening a file named `%s`' % (args,filename))
1874 # Now, make sure we can actually read the source (if it was in
1903 # Now, make sure we can actually read the source (if it was in
1875 # a temp file it's gone by now).
1904 # a temp file it's gone by now).
1876 if datafile:
1905 if datafile:
1877 try:
1906 try:
1878 lineno = inspect.getsourcelines(data)[1]
1907 lineno = inspect.getsourcelines(data)[1]
1879 except IOError:
1908 except IOError:
1880 filename = make_filename(args)
1909 filename = make_filename(args)
1881 if filename is None:
1910 if filename is None:
1882 warn('The file `%s` where `%s` was defined cannot '
1911 warn('The file `%s` where `%s` was defined cannot '
1883 'be read.' % (filename,data))
1912 'be read.' % (filename,data))
1884 return
1913 return
1885 use_temp = 0
1914 use_temp = 0
1886 else:
1915 else:
1887 data = ''
1916 data = ''
1888
1917
1889 if use_temp:
1918 if use_temp:
1890 filename = self.shell.mktempfile(data)
1919 filename = self.shell.mktempfile(data)
1891
1920
1892 # do actual editing here
1921 # do actual editing here
1893 print 'Editing...',
1922 print 'Editing...',
1894 sys.stdout.flush()
1923 sys.stdout.flush()
1895 self.shell.hooks.editor(filename,lineno)
1924 self.shell.hooks.editor(filename,lineno)
1896 if opts.has_key('x'): # -x prevents actual execution
1925 if opts.has_key('x'): # -x prevents actual execution
1897 print
1926 print
1898 else:
1927 else:
1899 print 'done. Executing edited code...'
1928 print 'done. Executing edited code...'
1900 try:
1929 try:
1901 self.shell.safe_execfile(filename,self.shell.user_ns)
1930 self.shell.safe_execfile(filename,self.shell.user_ns)
1902 except IOError,msg:
1931 except IOError,msg:
1903 if msg.filename == filename:
1932 if msg.filename == filename:
1904 warn('File not found. Did you forget to save?')
1933 warn('File not found. Did you forget to save?')
1905 return
1934 return
1906 else:
1935 else:
1907 self.shell.showtraceback()
1936 self.shell.showtraceback()
1908 except:
1937 except:
1909 self.shell.showtraceback()
1938 self.shell.showtraceback()
1910
1939
1911 def magic_xmode(self,parameter_s = ''):
1940 def magic_xmode(self,parameter_s = ''):
1912 """Switch modes for the exception handlers.
1941 """Switch modes for the exception handlers.
1913
1942
1914 Valid modes: Plain, Context and Verbose.
1943 Valid modes: Plain, Context and Verbose.
1915
1944
1916 If called without arguments, acts as a toggle."""
1945 If called without arguments, acts as a toggle."""
1917
1946
1918 def xmode_switch_err(name):
1947 def xmode_switch_err(name):
1919 warn('Error changing %s exception modes.\n%s' %
1948 warn('Error changing %s exception modes.\n%s' %
1920 (name,sys.exc_info()[1]))
1949 (name,sys.exc_info()[1]))
1921
1950
1922 shell = self.shell
1951 shell = self.shell
1923 new_mode = parameter_s.strip().capitalize()
1952 new_mode = parameter_s.strip().capitalize()
1924 try:
1953 try:
1925 shell.InteractiveTB.set_mode(mode=new_mode)
1954 shell.InteractiveTB.set_mode(mode=new_mode)
1926 print 'Exception reporting mode:',shell.InteractiveTB.mode
1955 print 'Exception reporting mode:',shell.InteractiveTB.mode
1927 except:
1956 except:
1928 xmode_switch_err('user')
1957 xmode_switch_err('user')
1929
1958
1930 # threaded shells use a special handler in sys.excepthook
1959 # threaded shells use a special handler in sys.excepthook
1931 if shell.isthreaded:
1960 if shell.isthreaded:
1932 try:
1961 try:
1933 shell.sys_excepthook.set_mode(mode=new_mode)
1962 shell.sys_excepthook.set_mode(mode=new_mode)
1934 except:
1963 except:
1935 xmode_switch_err('threaded')
1964 xmode_switch_err('threaded')
1936
1965
1937 def magic_colors(self,parameter_s = ''):
1966 def magic_colors(self,parameter_s = ''):
1938 """Switch color scheme for prompts, info system and exception handlers.
1967 """Switch color scheme for prompts, info system and exception handlers.
1939
1968
1940 Currently implemented schemes: NoColor, Linux, LightBG.
1969 Currently implemented schemes: NoColor, Linux, LightBG.
1941
1970
1942 Color scheme names are not case-sensitive."""
1971 Color scheme names are not case-sensitive."""
1943
1972
1944 def color_switch_err(name):
1973 def color_switch_err(name):
1945 warn('Error changing %s color schemes.\n%s' %
1974 warn('Error changing %s color schemes.\n%s' %
1946 (name,sys.exc_info()[1]))
1975 (name,sys.exc_info()[1]))
1947
1976
1948
1977
1949 new_scheme = parameter_s.strip()
1978 new_scheme = parameter_s.strip()
1950 if not new_scheme:
1979 if not new_scheme:
1951 print 'You must specify a color scheme.'
1980 print 'You must specify a color scheme.'
1952 return
1981 return
1953 # Under Windows, check for Gary Bishop's readline, which is necessary
1982 # Under Windows, check for Gary Bishop's readline, which is necessary
1954 # for ANSI coloring
1983 # for ANSI coloring
1955 if os.name in ['nt','dos']:
1984 if os.name in ['nt','dos']:
1956 try:
1985 try:
1957 import readline
1986 import readline
1958 except ImportError:
1987 except ImportError:
1959 has_readline = 0
1988 has_readline = 0
1960 else:
1989 else:
1961 try:
1990 try:
1962 readline.GetOutputFile()
1991 readline.GetOutputFile()
1963 except AttributeError:
1992 except AttributeError:
1964 has_readline = 0
1993 has_readline = 0
1965 else:
1994 else:
1966 has_readline = 1
1995 has_readline = 1
1967 if not has_readline:
1996 if not has_readline:
1968 msg = """\
1997 msg = """\
1969 Proper color support under MS Windows requires Gary Bishop's readline library.
1998 Proper color support under MS Windows requires Gary Bishop's readline library.
1970 You can find it at:
1999 You can find it at:
1971 http://sourceforge.net/projects/uncpythontools
2000 http://sourceforge.net/projects/uncpythontools
1972 Gary's readline needs the ctypes module, from:
2001 Gary's readline needs the ctypes module, from:
1973 http://starship.python.net/crew/theller/ctypes
2002 http://starship.python.net/crew/theller/ctypes
1974
2003
1975 Defaulting color scheme to 'NoColor'"""
2004 Defaulting color scheme to 'NoColor'"""
1976 new_scheme = 'NoColor'
2005 new_scheme = 'NoColor'
1977 warn(msg)
2006 warn(msg)
1978 # local shortcut
2007 # local shortcut
1979 shell = self.shell
2008 shell = self.shell
1980
2009
1981 # Set prompt colors
2010 # Set prompt colors
1982 try:
2011 try:
1983 shell.outputcache.set_colors(new_scheme)
2012 shell.outputcache.set_colors(new_scheme)
1984 except:
2013 except:
1985 color_switch_err('prompt')
2014 color_switch_err('prompt')
1986 else:
2015 else:
1987 shell.rc.colors = \
2016 shell.rc.colors = \
1988 shell.outputcache.color_table.active_scheme_name
2017 shell.outputcache.color_table.active_scheme_name
1989 # Set exception colors
2018 # Set exception colors
1990 try:
2019 try:
1991 shell.InteractiveTB.set_colors(scheme = new_scheme)
2020 shell.InteractiveTB.set_colors(scheme = new_scheme)
1992 shell.SyntaxTB.set_colors(scheme = new_scheme)
2021 shell.SyntaxTB.set_colors(scheme = new_scheme)
1993 except:
2022 except:
1994 color_switch_err('exception')
2023 color_switch_err('exception')
1995
2024
1996 # threaded shells use a verbose traceback in sys.excepthook
2025 # threaded shells use a verbose traceback in sys.excepthook
1997 if shell.isthreaded:
2026 if shell.isthreaded:
1998 try:
2027 try:
1999 shell.sys_excepthook.set_colors(scheme=new_scheme)
2028 shell.sys_excepthook.set_colors(scheme=new_scheme)
2000 except:
2029 except:
2001 color_switch_err('system exception handler')
2030 color_switch_err('system exception handler')
2002
2031
2003 # Set info (for 'object?') colors
2032 # Set info (for 'object?') colors
2004 if shell.rc.color_info:
2033 if shell.rc.color_info:
2005 try:
2034 try:
2006 shell.inspector.set_active_scheme(new_scheme)
2035 shell.inspector.set_active_scheme(new_scheme)
2007 except:
2036 except:
2008 color_switch_err('object inspector')
2037 color_switch_err('object inspector')
2009 else:
2038 else:
2010 shell.inspector.set_active_scheme('NoColor')
2039 shell.inspector.set_active_scheme('NoColor')
2011
2040
2012 def magic_color_info(self,parameter_s = ''):
2041 def magic_color_info(self,parameter_s = ''):
2013 """Toggle color_info.
2042 """Toggle color_info.
2014
2043
2015 The color_info configuration parameter controls whether colors are
2044 The color_info configuration parameter controls whether colors are
2016 used for displaying object details (by things like %psource, %pfile or
2045 used for displaying object details (by things like %psource, %pfile or
2017 the '?' system). This function toggles this value with each call.
2046 the '?' system). This function toggles this value with each call.
2018
2047
2019 Note that unless you have a fairly recent pager (less works better
2048 Note that unless you have a fairly recent pager (less works better
2020 than more) in your system, using colored object information displays
2049 than more) in your system, using colored object information displays
2021 will not work properly. Test it and see."""
2050 will not work properly. Test it and see."""
2022
2051
2023 self.shell.rc.color_info = 1 - self.shell.rc.color_info
2052 self.shell.rc.color_info = 1 - self.shell.rc.color_info
2024 self.magic_colors(self.shell.rc.colors)
2053 self.magic_colors(self.shell.rc.colors)
2025 print 'Object introspection functions have now coloring:',
2054 print 'Object introspection functions have now coloring:',
2026 print ['OFF','ON'][self.shell.rc.color_info]
2055 print ['OFF','ON'][self.shell.rc.color_info]
2027
2056
2028 def magic_Pprint(self, parameter_s=''):
2057 def magic_Pprint(self, parameter_s=''):
2029 """Toggle pretty printing on/off."""
2058 """Toggle pretty printing on/off."""
2030
2059
2031 self.shell.outputcache.Pprint = 1 - self.shell.outputcache.Pprint
2060 self.shell.outputcache.Pprint = 1 - self.shell.outputcache.Pprint
2032 print 'Pretty printing has been turned', \
2061 print 'Pretty printing has been turned', \
2033 ['OFF','ON'][self.shell.outputcache.Pprint]
2062 ['OFF','ON'][self.shell.outputcache.Pprint]
2034
2063
2035 def magic_exit(self, parameter_s=''):
2064 def magic_exit(self, parameter_s=''):
2036 """Exit IPython, confirming if configured to do so.
2065 """Exit IPython, confirming if configured to do so.
2037
2066
2038 You can configure whether IPython asks for confirmation upon exit by
2067 You can configure whether IPython asks for confirmation upon exit by
2039 setting the confirm_exit flag in the ipythonrc file."""
2068 setting the confirm_exit flag in the ipythonrc file."""
2040
2069
2041 self.shell.exit()
2070 self.shell.exit()
2042
2071
2043 def magic_quit(self, parameter_s=''):
2072 def magic_quit(self, parameter_s=''):
2044 """Exit IPython, confirming if configured to do so (like %exit)"""
2073 """Exit IPython, confirming if configured to do so (like %exit)"""
2045
2074
2046 self.shell.exit()
2075 self.shell.exit()
2047
2076
2048 def magic_Exit(self, parameter_s=''):
2077 def magic_Exit(self, parameter_s=''):
2049 """Exit IPython without confirmation."""
2078 """Exit IPython without confirmation."""
2050
2079
2051 self.shell.exit_now = True
2080 self.shell.exit_now = True
2052
2081
2053 def magic_Quit(self, parameter_s=''):
2082 def magic_Quit(self, parameter_s=''):
2054 """Exit IPython without confirmation (like %Exit)."""
2083 """Exit IPython without confirmation (like %Exit)."""
2055
2084
2056 self.shell.exit_now = True
2085 self.shell.exit_now = True
2057
2086
2058 #......................................................................
2087 #......................................................................
2059 # Functions to implement unix shell-type things
2088 # Functions to implement unix shell-type things
2060
2089
2061 def magic_alias(self, parameter_s = ''):
2090 def magic_alias(self, parameter_s = ''):
2062 """Define an alias for a system command.
2091 """Define an alias for a system command.
2063
2092
2064 '%alias alias_name cmd' defines 'alias_name' as an alias for 'cmd'
2093 '%alias alias_name cmd' defines 'alias_name' as an alias for 'cmd'
2065
2094
2066 Then, typing 'alias_name params' will execute the system command 'cmd
2095 Then, typing 'alias_name params' will execute the system command 'cmd
2067 params' (from your underlying operating system).
2096 params' (from your underlying operating system).
2068
2097
2069 Aliases have lower precedence than magic functions and Python normal
2098 Aliases have lower precedence than magic functions and Python normal
2070 variables, so if 'foo' is both a Python variable and an alias, the
2099 variables, so if 'foo' is both a Python variable and an alias, the
2071 alias can not be executed until 'del foo' removes the Python variable.
2100 alias can not be executed until 'del foo' removes the Python variable.
2072
2101
2073 You can use the %l specifier in an alias definition to represent the
2102 You can use the %l specifier in an alias definition to represent the
2074 whole line when the alias is called. For example:
2103 whole line when the alias is called. For example:
2075
2104
2076 In [2]: alias all echo "Input in brackets: <%l>"\\
2105 In [2]: alias all echo "Input in brackets: <%l>"\\
2077 In [3]: all hello world\\
2106 In [3]: all hello world\\
2078 Input in brackets: <hello world>
2107 Input in brackets: <hello world>
2079
2108
2080 You can also define aliases with parameters using %s specifiers (one
2109 You can also define aliases with parameters using %s specifiers (one
2081 per parameter):
2110 per parameter):
2082
2111
2083 In [1]: alias parts echo first %s second %s\\
2112 In [1]: alias parts echo first %s second %s\\
2084 In [2]: %parts A B\\
2113 In [2]: %parts A B\\
2085 first A second B\\
2114 first A second B\\
2086 In [3]: %parts A\\
2115 In [3]: %parts A\\
2087 Incorrect number of arguments: 2 expected.\\
2116 Incorrect number of arguments: 2 expected.\\
2088 parts is an alias to: 'echo first %s second %s'
2117 parts is an alias to: 'echo first %s second %s'
2089
2118
2090 Note that %l and %s are mutually exclusive. You can only use one or
2119 Note that %l and %s are mutually exclusive. You can only use one or
2091 the other in your aliases.
2120 the other in your aliases.
2092
2121
2093 Aliases expand Python variables just like system calls using ! or !!
2122 Aliases expand Python variables just like system calls using ! or !!
2094 do: all expressions prefixed with '$' get expanded. For details of
2123 do: all expressions prefixed with '$' get expanded. For details of
2095 the semantic rules, see PEP-215:
2124 the semantic rules, see PEP-215:
2096 http://www.python.org/peps/pep-0215.html. This is the library used by
2125 http://www.python.org/peps/pep-0215.html. This is the library used by
2097 IPython for variable expansion. If you want to access a true shell
2126 IPython for variable expansion. If you want to access a true shell
2098 variable, an extra $ is necessary to prevent its expansion by IPython:
2127 variable, an extra $ is necessary to prevent its expansion by IPython:
2099
2128
2100 In [6]: alias show echo\\
2129 In [6]: alias show echo\\
2101 In [7]: PATH='A Python string'\\
2130 In [7]: PATH='A Python string'\\
2102 In [8]: show $PATH\\
2131 In [8]: show $PATH\\
2103 A Python string\\
2132 A Python string\\
2104 In [9]: show $$PATH\\
2133 In [9]: show $$PATH\\
2105 /usr/local/lf9560/bin:/usr/local/intel/compiler70/ia32/bin:...
2134 /usr/local/lf9560/bin:/usr/local/intel/compiler70/ia32/bin:...
2106
2135
2107 You can use the alias facility to acess all of $PATH. See the %rehash
2136 You can use the alias facility to acess all of $PATH. See the %rehash
2108 and %rehashx functions, which automatically create aliases for the
2137 and %rehashx functions, which automatically create aliases for the
2109 contents of your $PATH.
2138 contents of your $PATH.
2110
2139
2111 If called with no parameters, %alias prints the current alias table."""
2140 If called with no parameters, %alias prints the current alias table."""
2112
2141
2113 par = parameter_s.strip()
2142 par = parameter_s.strip()
2114 if not par:
2143 if not par:
2115 if self.shell.rc.automagic:
2144 if self.shell.rc.automagic:
2116 prechar = ''
2145 prechar = ''
2117 else:
2146 else:
2118 prechar = self.shell.ESC_MAGIC
2147 prechar = self.shell.ESC_MAGIC
2119 print 'Alias\t\tSystem Command\n'+'-'*30
2148 print 'Alias\t\tSystem Command\n'+'-'*30
2120 atab = self.shell.alias_table
2149 atab = self.shell.alias_table
2121 aliases = atab.keys()
2150 aliases = atab.keys()
2122 aliases.sort()
2151 aliases.sort()
2123 for alias in aliases:
2152 for alias in aliases:
2124 print prechar+alias+'\t\t'+atab[alias][1]
2153 print prechar+alias+'\t\t'+atab[alias][1]
2125 print '-'*30+'\nTotal number of aliases:',len(aliases)
2154 print '-'*30+'\nTotal number of aliases:',len(aliases)
2126 return
2155 return
2127 try:
2156 try:
2128 alias,cmd = par.split(None,1)
2157 alias,cmd = par.split(None,1)
2129 except:
2158 except:
2130 print OInspect.getdoc(self.magic_alias)
2159 print OInspect.getdoc(self.magic_alias)
2131 else:
2160 else:
2132 nargs = cmd.count('%s')
2161 nargs = cmd.count('%s')
2133 if nargs>0 and cmd.find('%l')>=0:
2162 if nargs>0 and cmd.find('%l')>=0:
2134 error('The %s and %l specifiers are mutually exclusive '
2163 error('The %s and %l specifiers are mutually exclusive '
2135 'in alias definitions.')
2164 'in alias definitions.')
2136 else: # all looks OK
2165 else: # all looks OK
2137 self.shell.alias_table[alias] = (nargs,cmd)
2166 self.shell.alias_table[alias] = (nargs,cmd)
2138 self.shell.alias_table_validate(verbose=1)
2167 self.shell.alias_table_validate(verbose=1)
2139 # end magic_alias
2168 # end magic_alias
2140
2169
2141 def magic_unalias(self, parameter_s = ''):
2170 def magic_unalias(self, parameter_s = ''):
2142 """Remove an alias"""
2171 """Remove an alias"""
2143
2172
2144 aname = parameter_s.strip()
2173 aname = parameter_s.strip()
2145 if aname in self.shell.alias_table:
2174 if aname in self.shell.alias_table:
2146 del self.shell.alias_table[aname]
2175 del self.shell.alias_table[aname]
2147
2176
2148 def magic_rehash(self, parameter_s = ''):
2177 def magic_rehash(self, parameter_s = ''):
2149 """Update the alias table with all entries in $PATH.
2178 """Update the alias table with all entries in $PATH.
2150
2179
2151 This version does no checks on execute permissions or whether the
2180 This version does no checks on execute permissions or whether the
2152 contents of $PATH are truly files (instead of directories or something
2181 contents of $PATH are truly files (instead of directories or something
2153 else). For such a safer (but slower) version, use %rehashx."""
2182 else). For such a safer (but slower) version, use %rehashx."""
2154
2183
2155 # This function (and rehashx) manipulate the alias_table directly
2184 # This function (and rehashx) manipulate the alias_table directly
2156 # rather than calling magic_alias, for speed reasons. A rehash on a
2185 # rather than calling magic_alias, for speed reasons. A rehash on a
2157 # typical Linux box involves several thousand entries, so efficiency
2186 # typical Linux box involves several thousand entries, so efficiency
2158 # here is a top concern.
2187 # here is a top concern.
2159
2188
2160 path = filter(os.path.isdir,os.environ['PATH'].split(os.pathsep))
2189 path = filter(os.path.isdir,os.environ['PATH'].split(os.pathsep))
2161 alias_table = self.shell.alias_table
2190 alias_table = self.shell.alias_table
2162 for pdir in path:
2191 for pdir in path:
2163 for ff in os.listdir(pdir):
2192 for ff in os.listdir(pdir):
2164 # each entry in the alias table must be (N,name), where
2193 # each entry in the alias table must be (N,name), where
2165 # N is the number of positional arguments of the alias.
2194 # N is the number of positional arguments of the alias.
2166 alias_table[ff] = (0,ff)
2195 alias_table[ff] = (0,ff)
2167 # Make sure the alias table doesn't contain keywords or builtins
2196 # Make sure the alias table doesn't contain keywords or builtins
2168 self.shell.alias_table_validate()
2197 self.shell.alias_table_validate()
2169 # Call again init_auto_alias() so we get 'rm -i' and other modified
2198 # Call again init_auto_alias() so we get 'rm -i' and other modified
2170 # aliases since %rehash will probably clobber them
2199 # aliases since %rehash will probably clobber them
2171 self.shell.init_auto_alias()
2200 self.shell.init_auto_alias()
2172
2201
2173 def magic_rehashx(self, parameter_s = ''):
2202 def magic_rehashx(self, parameter_s = ''):
2174 """Update the alias table with all executable files in $PATH.
2203 """Update the alias table with all executable files in $PATH.
2175
2204
2176 This version explicitly checks that every entry in $PATH is a file
2205 This version explicitly checks that every entry in $PATH is a file
2177 with execute access (os.X_OK), so it is much slower than %rehash.
2206 with execute access (os.X_OK), so it is much slower than %rehash.
2178
2207
2179 Under Windows, it checks executability as a match agains a
2208 Under Windows, it checks executability as a match agains a
2180 '|'-separated string of extensions, stored in the IPython config
2209 '|'-separated string of extensions, stored in the IPython config
2181 variable win_exec_ext. This defaults to 'exe|com|bat'. """
2210 variable win_exec_ext. This defaults to 'exe|com|bat'. """
2182
2211
2183 path = filter(os.path.isdir,os.environ['PATH'].split(os.pathsep))
2212 path = filter(os.path.isdir,os.environ['PATH'].split(os.pathsep))
2184 alias_table = self.shell.alias_table
2213 alias_table = self.shell.alias_table
2185
2214
2186 if os.name == 'posix':
2215 if os.name == 'posix':
2187 isexec = lambda fname:os.path.isfile(fname) and \
2216 isexec = lambda fname:os.path.isfile(fname) and \
2188 os.access(fname,os.X_OK)
2217 os.access(fname,os.X_OK)
2189 else:
2218 else:
2190
2219
2191 try:
2220 try:
2192 winext = os.environ['pathext'].replace(';','|').replace('.','')
2221 winext = os.environ['pathext'].replace(';','|').replace('.','')
2193 except KeyError:
2222 except KeyError:
2194 winext = 'exe|com|bat'
2223 winext = 'exe|com|bat'
2195
2224
2196 execre = re.compile(r'(.*)\.(%s)$' % winext,re.IGNORECASE)
2225 execre = re.compile(r'(.*)\.(%s)$' % winext,re.IGNORECASE)
2197 isexec = lambda fname:os.path.isfile(fname) and execre.match(fname)
2226 isexec = lambda fname:os.path.isfile(fname) and execre.match(fname)
2198 savedir = os.getcwd()
2227 savedir = os.getcwd()
2199 try:
2228 try:
2200 # write the whole loop for posix/Windows so we don't have an if in
2229 # write the whole loop for posix/Windows so we don't have an if in
2201 # the innermost part
2230 # the innermost part
2202 if os.name == 'posix':
2231 if os.name == 'posix':
2203 for pdir in path:
2232 for pdir in path:
2204 os.chdir(pdir)
2233 os.chdir(pdir)
2205 for ff in os.listdir(pdir):
2234 for ff in os.listdir(pdir):
2206 if isexec(ff):
2235 if isexec(ff):
2207 # each entry in the alias table must be (N,name),
2236 # each entry in the alias table must be (N,name),
2208 # where N is the number of positional arguments of the
2237 # where N is the number of positional arguments of the
2209 # alias.
2238 # alias.
2210 alias_table[ff] = (0,ff)
2239 alias_table[ff] = (0,ff)
2211 else:
2240 else:
2212 for pdir in path:
2241 for pdir in path:
2213 os.chdir(pdir)
2242 os.chdir(pdir)
2214 for ff in os.listdir(pdir):
2243 for ff in os.listdir(pdir):
2215 if isexec(ff):
2244 if isexec(ff):
2216 alias_table[execre.sub(r'\1',ff)] = (0,ff)
2245 alias_table[execre.sub(r'\1',ff)] = (0,ff)
2217 # Make sure the alias table doesn't contain keywords or builtins
2246 # Make sure the alias table doesn't contain keywords or builtins
2218 self.shell.alias_table_validate()
2247 self.shell.alias_table_validate()
2219 # Call again init_auto_alias() so we get 'rm -i' and other
2248 # Call again init_auto_alias() so we get 'rm -i' and other
2220 # modified aliases since %rehashx will probably clobber them
2249 # modified aliases since %rehashx will probably clobber them
2221 self.shell.init_auto_alias()
2250 self.shell.init_auto_alias()
2222 finally:
2251 finally:
2223 os.chdir(savedir)
2252 os.chdir(savedir)
2224
2253
2225 def magic_pwd(self, parameter_s = ''):
2254 def magic_pwd(self, parameter_s = ''):
2226 """Return the current working directory path."""
2255 """Return the current working directory path."""
2227 return os.getcwd()
2256 return os.getcwd()
2228
2257
2229 def magic_cd(self, parameter_s=''):
2258 def magic_cd(self, parameter_s=''):
2230 """Change the current working directory.
2259 """Change the current working directory.
2231
2260
2232 This command automatically maintains an internal list of directories
2261 This command automatically maintains an internal list of directories
2233 you visit during your IPython session, in the variable _dh. The
2262 you visit during your IPython session, in the variable _dh. The
2234 command %dhist shows this history nicely formatted.
2263 command %dhist shows this history nicely formatted.
2235
2264
2236 Usage:
2265 Usage:
2237
2266
2238 cd 'dir': changes to directory 'dir'.
2267 cd 'dir': changes to directory 'dir'.
2239
2268
2240 cd -: changes to the last visited directory.
2269 cd -: changes to the last visited directory.
2241
2270
2242 cd -<n>: changes to the n-th directory in the directory history.
2271 cd -<n>: changes to the n-th directory in the directory history.
2243
2272
2244 cd -b <bookmark_name>: jump to a bookmark set by %bookmark
2273 cd -b <bookmark_name>: jump to a bookmark set by %bookmark
2245 (note: cd <bookmark_name> is enough if there is no
2274 (note: cd <bookmark_name> is enough if there is no
2246 directory <bookmark_name>, but a bookmark with the name exists.)
2275 directory <bookmark_name>, but a bookmark with the name exists.)
2247
2276
2248 Options:
2277 Options:
2249
2278
2250 -q: quiet. Do not print the working directory after the cd command is
2279 -q: quiet. Do not print the working directory after the cd command is
2251 executed. By default IPython's cd command does print this directory,
2280 executed. By default IPython's cd command does print this directory,
2252 since the default prompts do not display path information.
2281 since the default prompts do not display path information.
2253
2282
2254 Note that !cd doesn't work for this purpose because the shell where
2283 Note that !cd doesn't work for this purpose because the shell where
2255 !command runs is immediately discarded after executing 'command'."""
2284 !command runs is immediately discarded after executing 'command'."""
2256
2285
2257 parameter_s = parameter_s.strip()
2286 parameter_s = parameter_s.strip()
2258 bkms = self.shell.persist.get("bookmarks",{})
2287 bkms = self.shell.persist.get("bookmarks",{})
2259
2288
2260 numcd = re.match(r'(-)(\d+)$',parameter_s)
2289 numcd = re.match(r'(-)(\d+)$',parameter_s)
2261 # jump in directory history by number
2290 # jump in directory history by number
2262 if numcd:
2291 if numcd:
2263 nn = int(numcd.group(2))
2292 nn = int(numcd.group(2))
2264 try:
2293 try:
2265 ps = self.shell.user_ns['_dh'][nn]
2294 ps = self.shell.user_ns['_dh'][nn]
2266 except IndexError:
2295 except IndexError:
2267 print 'The requested directory does not exist in history.'
2296 print 'The requested directory does not exist in history.'
2268 return
2297 return
2269 else:
2298 else:
2270 opts = {}
2299 opts = {}
2271 else:
2300 else:
2272 opts,ps = self.parse_options(parameter_s,'qb',mode='string')
2301 opts,ps = self.parse_options(parameter_s,'qb',mode='string')
2273 # jump to previous
2302 # jump to previous
2274 if ps == '-':
2303 if ps == '-':
2275 try:
2304 try:
2276 ps = self.shell.user_ns['_dh'][-2]
2305 ps = self.shell.user_ns['_dh'][-2]
2277 except IndexError:
2306 except IndexError:
2278 print 'No previous directory to change to.'
2307 print 'No previous directory to change to.'
2279 return
2308 return
2280 # jump to bookmark
2309 # jump to bookmark
2281 elif opts.has_key('b') or (bkms.has_key(ps) and not os.path.isdir(ps)):
2310 elif opts.has_key('b') or (bkms.has_key(ps) and not os.path.isdir(ps)):
2282 if bkms.has_key(ps):
2311 if bkms.has_key(ps):
2283 target = bkms[ps]
2312 target = bkms[ps]
2284 print '(bookmark:%s) -> %s' % (ps,target)
2313 print '(bookmark:%s) -> %s' % (ps,target)
2285 ps = target
2314 ps = target
2286 else:
2315 else:
2287 if bkms:
2316 if bkms:
2288 error("Bookmark '%s' not found. "
2317 error("Bookmark '%s' not found. "
2289 "Use '%%bookmark -l' to see your bookmarks." % ps)
2318 "Use '%%bookmark -l' to see your bookmarks." % ps)
2290 else:
2319 else:
2291 print "Bookmarks not set - use %bookmark <bookmarkname>"
2320 print "Bookmarks not set - use %bookmark <bookmarkname>"
2292 return
2321 return
2293
2322
2294 # at this point ps should point to the target dir
2323 # at this point ps should point to the target dir
2295 if ps:
2324 if ps:
2296 try:
2325 try:
2297 os.chdir(os.path.expanduser(ps))
2326 os.chdir(os.path.expanduser(ps))
2298 except OSError:
2327 except OSError:
2299 print sys.exc_info()[1]
2328 print sys.exc_info()[1]
2300 else:
2329 else:
2301 self.shell.user_ns['_dh'].append(os.getcwd())
2330 self.shell.user_ns['_dh'].append(os.getcwd())
2302 else:
2331 else:
2303 os.chdir(self.shell.home_dir)
2332 os.chdir(self.shell.home_dir)
2304 self.shell.user_ns['_dh'].append(os.getcwd())
2333 self.shell.user_ns['_dh'].append(os.getcwd())
2305 if not 'q' in opts:
2334 if not 'q' in opts:
2306 print self.shell.user_ns['_dh'][-1]
2335 print self.shell.user_ns['_dh'][-1]
2307
2336
2308 def magic_dhist(self, parameter_s=''):
2337 def magic_dhist(self, parameter_s=''):
2309 """Print your history of visited directories.
2338 """Print your history of visited directories.
2310
2339
2311 %dhist -> print full history\\
2340 %dhist -> print full history\\
2312 %dhist n -> print last n entries only\\
2341 %dhist n -> print last n entries only\\
2313 %dhist n1 n2 -> print entries between n1 and n2 (n1 not included)\\
2342 %dhist n1 n2 -> print entries between n1 and n2 (n1 not included)\\
2314
2343
2315 This history is automatically maintained by the %cd command, and
2344 This history is automatically maintained by the %cd command, and
2316 always available as the global list variable _dh. You can use %cd -<n>
2345 always available as the global list variable _dh. You can use %cd -<n>
2317 to go to directory number <n>."""
2346 to go to directory number <n>."""
2318
2347
2319 dh = self.shell.user_ns['_dh']
2348 dh = self.shell.user_ns['_dh']
2320 if parameter_s:
2349 if parameter_s:
2321 try:
2350 try:
2322 args = map(int,parameter_s.split())
2351 args = map(int,parameter_s.split())
2323 except:
2352 except:
2324 self.arg_err(Magic.magic_dhist)
2353 self.arg_err(Magic.magic_dhist)
2325 return
2354 return
2326 if len(args) == 1:
2355 if len(args) == 1:
2327 ini,fin = max(len(dh)-(args[0]),0),len(dh)
2356 ini,fin = max(len(dh)-(args[0]),0),len(dh)
2328 elif len(args) == 2:
2357 elif len(args) == 2:
2329 ini,fin = args
2358 ini,fin = args
2330 else:
2359 else:
2331 self.arg_err(Magic.magic_dhist)
2360 self.arg_err(Magic.magic_dhist)
2332 return
2361 return
2333 else:
2362 else:
2334 ini,fin = 0,len(dh)
2363 ini,fin = 0,len(dh)
2335 nlprint(dh,
2364 nlprint(dh,
2336 header = 'Directory history (kept in _dh)',
2365 header = 'Directory history (kept in _dh)',
2337 start=ini,stop=fin)
2366 start=ini,stop=fin)
2338
2367
2339 def magic_env(self, parameter_s=''):
2368 def magic_env(self, parameter_s=''):
2340 """List environment variables."""
2369 """List environment variables."""
2341
2370
2342 return os.environ.data
2371 return os.environ.data
2343
2372
2344 def magic_pushd(self, parameter_s=''):
2373 def magic_pushd(self, parameter_s=''):
2345 """Place the current dir on stack and change directory.
2374 """Place the current dir on stack and change directory.
2346
2375
2347 Usage:\\
2376 Usage:\\
2348 %pushd ['dirname']
2377 %pushd ['dirname']
2349
2378
2350 %pushd with no arguments does a %pushd to your home directory.
2379 %pushd with no arguments does a %pushd to your home directory.
2351 """
2380 """
2352 if parameter_s == '': parameter_s = '~'
2381 if parameter_s == '': parameter_s = '~'
2353 dir_s = self.shell.dir_stack
2382 dir_s = self.shell.dir_stack
2354 if len(dir_s)>0 and os.path.expanduser(parameter_s) != \
2383 if len(dir_s)>0 and os.path.expanduser(parameter_s) != \
2355 os.path.expanduser(self.shell.dir_stack[0]):
2384 os.path.expanduser(self.shell.dir_stack[0]):
2356 try:
2385 try:
2357 self.magic_cd(parameter_s)
2386 self.magic_cd(parameter_s)
2358 dir_s.insert(0,os.getcwd().replace(self.home_dir,'~'))
2387 dir_s.insert(0,os.getcwd().replace(self.home_dir,'~'))
2359 self.magic_dirs()
2388 self.magic_dirs()
2360 except:
2389 except:
2361 print 'Invalid directory'
2390 print 'Invalid directory'
2362 else:
2391 else:
2363 print 'You are already there!'
2392 print 'You are already there!'
2364
2393
2365 def magic_popd(self, parameter_s=''):
2394 def magic_popd(self, parameter_s=''):
2366 """Change to directory popped off the top of the stack.
2395 """Change to directory popped off the top of the stack.
2367 """
2396 """
2368 if len (self.shell.dir_stack) > 1:
2397 if len (self.shell.dir_stack) > 1:
2369 self.shell.dir_stack.pop(0)
2398 self.shell.dir_stack.pop(0)
2370 self.magic_cd(self.shell.dir_stack[0])
2399 self.magic_cd(self.shell.dir_stack[0])
2371 print self.shell.dir_stack[0]
2400 print self.shell.dir_stack[0]
2372 else:
2401 else:
2373 print "You can't remove the starting directory from the stack:",\
2402 print "You can't remove the starting directory from the stack:",\
2374 self.shell.dir_stack
2403 self.shell.dir_stack
2375
2404
2376 def magic_dirs(self, parameter_s=''):
2405 def magic_dirs(self, parameter_s=''):
2377 """Return the current directory stack."""
2406 """Return the current directory stack."""
2378
2407
2379 return self.shell.dir_stack[:]
2408 return self.shell.dir_stack[:]
2380
2409
2381 def magic_sc(self, parameter_s=''):
2410 def magic_sc(self, parameter_s=''):
2382 """Shell capture - execute a shell command and capture its output.
2411 """Shell capture - execute a shell command and capture its output.
2383
2412
2384 %sc [options] varname=command
2413 %sc [options] varname=command
2385
2414
2386 IPython will run the given command using commands.getoutput(), and
2415 IPython will run the given command using commands.getoutput(), and
2387 will then update the user's interactive namespace with a variable
2416 will then update the user's interactive namespace with a variable
2388 called varname, containing the value of the call. Your command can
2417 called varname, containing the value of the call. Your command can
2389 contain shell wildcards, pipes, etc.
2418 contain shell wildcards, pipes, etc.
2390
2419
2391 The '=' sign in the syntax is mandatory, and the variable name you
2420 The '=' sign in the syntax is mandatory, and the variable name you
2392 supply must follow Python's standard conventions for valid names.
2421 supply must follow Python's standard conventions for valid names.
2393
2422
2394 Options:
2423 Options:
2395
2424
2396 -l: list output. Split the output on newlines into a list before
2425 -l: list output. Split the output on newlines into a list before
2397 assigning it to the given variable. By default the output is stored
2426 assigning it to the given variable. By default the output is stored
2398 as a single string.
2427 as a single string.
2399
2428
2400 -v: verbose. Print the contents of the variable.
2429 -v: verbose. Print the contents of the variable.
2401
2430
2402 In most cases you should not need to split as a list, because the
2431 In most cases you should not need to split as a list, because the
2403 returned value is a special type of string which can automatically
2432 returned value is a special type of string which can automatically
2404 provide its contents either as a list (split on newlines) or as a
2433 provide its contents either as a list (split on newlines) or as a
2405 space-separated string. These are convenient, respectively, either
2434 space-separated string. These are convenient, respectively, either
2406 for sequential processing or to be passed to a shell command.
2435 for sequential processing or to be passed to a shell command.
2407
2436
2408 For example:
2437 For example:
2409
2438
2410 # Capture into variable a
2439 # Capture into variable a
2411 In [9]: sc a=ls *py
2440 In [9]: sc a=ls *py
2412
2441
2413 # a is a string with embedded newlines
2442 # a is a string with embedded newlines
2414 In [10]: a
2443 In [10]: a
2415 Out[10]: 'setup.py\nwin32_manual_post_install.py'
2444 Out[10]: 'setup.py\nwin32_manual_post_install.py'
2416
2445
2417 # which can be seen as a list:
2446 # which can be seen as a list:
2418 In [11]: a.l
2447 In [11]: a.l
2419 Out[11]: ['setup.py', 'win32_manual_post_install.py']
2448 Out[11]: ['setup.py', 'win32_manual_post_install.py']
2420
2449
2421 # or as a whitespace-separated string:
2450 # or as a whitespace-separated string:
2422 In [12]: a.s
2451 In [12]: a.s
2423 Out[12]: 'setup.py win32_manual_post_install.py'
2452 Out[12]: 'setup.py win32_manual_post_install.py'
2424
2453
2425 # a.s is useful to pass as a single command line:
2454 # a.s is useful to pass as a single command line:
2426 In [13]: !wc -l $a.s
2455 In [13]: !wc -l $a.s
2427 146 setup.py
2456 146 setup.py
2428 130 win32_manual_post_install.py
2457 130 win32_manual_post_install.py
2429 276 total
2458 276 total
2430
2459
2431 # while the list form is useful to loop over:
2460 # while the list form is useful to loop over:
2432 In [14]: for f in a.l:
2461 In [14]: for f in a.l:
2433 ....: !wc -l $f
2462 ....: !wc -l $f
2434 ....:
2463 ....:
2435 146 setup.py
2464 146 setup.py
2436 130 win32_manual_post_install.py
2465 130 win32_manual_post_install.py
2437
2466
2438 Similiarly, the lists returned by the -l option are also special, in
2467 Similiarly, the lists returned by the -l option are also special, in
2439 the sense that you can equally invoke the .s attribute on them to
2468 the sense that you can equally invoke the .s attribute on them to
2440 automatically get a whitespace-separated string from their contents:
2469 automatically get a whitespace-separated string from their contents:
2441
2470
2442 In [1]: sc -l b=ls *py
2471 In [1]: sc -l b=ls *py
2443
2472
2444 In [2]: b
2473 In [2]: b
2445 Out[2]: ['setup.py', 'win32_manual_post_install.py']
2474 Out[2]: ['setup.py', 'win32_manual_post_install.py']
2446
2475
2447 In [3]: b.s
2476 In [3]: b.s
2448 Out[3]: 'setup.py win32_manual_post_install.py'
2477 Out[3]: 'setup.py win32_manual_post_install.py'
2449
2478
2450 In summary, both the lists and strings used for ouptut capture have
2479 In summary, both the lists and strings used for ouptut capture have
2451 the following special attributes:
2480 the following special attributes:
2452
2481
2453 .l (or .list) : value as list.
2482 .l (or .list) : value as list.
2454 .n (or .nlstr): value as newline-separated string.
2483 .n (or .nlstr): value as newline-separated string.
2455 .s (or .spstr): value as space-separated string.
2484 .s (or .spstr): value as space-separated string.
2456 """
2485 """
2457
2486
2458 opts,args = self.parse_options(parameter_s,'lv')
2487 opts,args = self.parse_options(parameter_s,'lv')
2459 # Try to get a variable name and command to run
2488 # Try to get a variable name and command to run
2460 try:
2489 try:
2461 # the variable name must be obtained from the parse_options
2490 # the variable name must be obtained from the parse_options
2462 # output, which uses shlex.split to strip options out.
2491 # output, which uses shlex.split to strip options out.
2463 var,_ = args.split('=',1)
2492 var,_ = args.split('=',1)
2464 var = var.strip()
2493 var = var.strip()
2465 # But the the command has to be extracted from the original input
2494 # But the the command has to be extracted from the original input
2466 # parameter_s, not on what parse_options returns, to avoid the
2495 # parameter_s, not on what parse_options returns, to avoid the
2467 # quote stripping which shlex.split performs on it.
2496 # quote stripping which shlex.split performs on it.
2468 _,cmd = parameter_s.split('=',1)
2497 _,cmd = parameter_s.split('=',1)
2469 except ValueError:
2498 except ValueError:
2470 var,cmd = '',''
2499 var,cmd = '',''
2471 if not var:
2500 if not var:
2472 error('you must specify a variable to assign the command to.')
2501 error('you must specify a variable to assign the command to.')
2473 return
2502 return
2474 # If all looks ok, proceed
2503 # If all looks ok, proceed
2475 out,err = self.shell.getoutputerror(cmd)
2504 out,err = self.shell.getoutputerror(cmd)
2476 if err:
2505 if err:
2477 print >> Term.cerr,err
2506 print >> Term.cerr,err
2478 if opts.has_key('l'):
2507 if opts.has_key('l'):
2479 out = SList(out.split('\n'))
2508 out = SList(out.split('\n'))
2480 else:
2509 else:
2481 out = LSString(out)
2510 out = LSString(out)
2482 if opts.has_key('v'):
2511 if opts.has_key('v'):
2483 print '%s ==\n%s' % (var,pformat(out))
2512 print '%s ==\n%s' % (var,pformat(out))
2484 self.shell.user_ns.update({var:out})
2513 self.shell.user_ns.update({var:out})
2485
2514
2486 def magic_sx(self, parameter_s=''):
2515 def magic_sx(self, parameter_s=''):
2487 """Shell execute - run a shell command and capture its output.
2516 """Shell execute - run a shell command and capture its output.
2488
2517
2489 %sx command
2518 %sx command
2490
2519
2491 IPython will run the given command using commands.getoutput(), and
2520 IPython will run the given command using commands.getoutput(), and
2492 return the result formatted as a list (split on '\\n'). Since the
2521 return the result formatted as a list (split on '\\n'). Since the
2493 output is _returned_, it will be stored in ipython's regular output
2522 output is _returned_, it will be stored in ipython's regular output
2494 cache Out[N] and in the '_N' automatic variables.
2523 cache Out[N] and in the '_N' automatic variables.
2495
2524
2496 Notes:
2525 Notes:
2497
2526
2498 1) If an input line begins with '!!', then %sx is automatically
2527 1) If an input line begins with '!!', then %sx is automatically
2499 invoked. That is, while:
2528 invoked. That is, while:
2500 !ls
2529 !ls
2501 causes ipython to simply issue system('ls'), typing
2530 causes ipython to simply issue system('ls'), typing
2502 !!ls
2531 !!ls
2503 is a shorthand equivalent to:
2532 is a shorthand equivalent to:
2504 %sx ls
2533 %sx ls
2505
2534
2506 2) %sx differs from %sc in that %sx automatically splits into a list,
2535 2) %sx differs from %sc in that %sx automatically splits into a list,
2507 like '%sc -l'. The reason for this is to make it as easy as possible
2536 like '%sc -l'. The reason for this is to make it as easy as possible
2508 to process line-oriented shell output via further python commands.
2537 to process line-oriented shell output via further python commands.
2509 %sc is meant to provide much finer control, but requires more
2538 %sc is meant to provide much finer control, but requires more
2510 typing.
2539 typing.
2511
2540
2512 3) Just like %sc -l, this is a list with special attributes:
2541 3) Just like %sc -l, this is a list with special attributes:
2513
2542
2514 .l (or .list) : value as list.
2543 .l (or .list) : value as list.
2515 .n (or .nlstr): value as newline-separated string.
2544 .n (or .nlstr): value as newline-separated string.
2516 .s (or .spstr): value as whitespace-separated string.
2545 .s (or .spstr): value as whitespace-separated string.
2517
2546
2518 This is very useful when trying to use such lists as arguments to
2547 This is very useful when trying to use such lists as arguments to
2519 system commands."""
2548 system commands."""
2520
2549
2521 if parameter_s:
2550 if parameter_s:
2522 out,err = self.shell.getoutputerror(parameter_s)
2551 out,err = self.shell.getoutputerror(parameter_s)
2523 if err:
2552 if err:
2524 print >> Term.cerr,err
2553 print >> Term.cerr,err
2525 return SList(out.split('\n'))
2554 return SList(out.split('\n'))
2526
2555
2527 def magic_bg(self, parameter_s=''):
2556 def magic_bg(self, parameter_s=''):
2528 """Run a job in the background, in a separate thread.
2557 """Run a job in the background, in a separate thread.
2529
2558
2530 For example,
2559 For example,
2531
2560
2532 %bg myfunc(x,y,z=1)
2561 %bg myfunc(x,y,z=1)
2533
2562
2534 will execute 'myfunc(x,y,z=1)' in a background thread. As soon as the
2563 will execute 'myfunc(x,y,z=1)' in a background thread. As soon as the
2535 execution starts, a message will be printed indicating the job
2564 execution starts, a message will be printed indicating the job
2536 number. If your job number is 5, you can use
2565 number. If your job number is 5, you can use
2537
2566
2538 myvar = jobs.result(5) or myvar = jobs[5].result
2567 myvar = jobs.result(5) or myvar = jobs[5].result
2539
2568
2540 to assign this result to variable 'myvar'.
2569 to assign this result to variable 'myvar'.
2541
2570
2542 IPython has a job manager, accessible via the 'jobs' object. You can
2571 IPython has a job manager, accessible via the 'jobs' object. You can
2543 type jobs? to get more information about it, and use jobs.<TAB> to see
2572 type jobs? to get more information about it, and use jobs.<TAB> to see
2544 its attributes. All attributes not starting with an underscore are
2573 its attributes. All attributes not starting with an underscore are
2545 meant for public use.
2574 meant for public use.
2546
2575
2547 In particular, look at the jobs.new() method, which is used to create
2576 In particular, look at the jobs.new() method, which is used to create
2548 new jobs. This magic %bg function is just a convenience wrapper
2577 new jobs. This magic %bg function is just a convenience wrapper
2549 around jobs.new(), for expression-based jobs. If you want to create a
2578 around jobs.new(), for expression-based jobs. If you want to create a
2550 new job with an explicit function object and arguments, you must call
2579 new job with an explicit function object and arguments, you must call
2551 jobs.new() directly.
2580 jobs.new() directly.
2552
2581
2553 The jobs.new docstring also describes in detail several important
2582 The jobs.new docstring also describes in detail several important
2554 caveats associated with a thread-based model for background job
2583 caveats associated with a thread-based model for background job
2555 execution. Type jobs.new? for details.
2584 execution. Type jobs.new? for details.
2556
2585
2557 You can check the status of all jobs with jobs.status().
2586 You can check the status of all jobs with jobs.status().
2558
2587
2559 The jobs variable is set by IPython into the Python builtin namespace.
2588 The jobs variable is set by IPython into the Python builtin namespace.
2560 If you ever declare a variable named 'jobs', you will shadow this
2589 If you ever declare a variable named 'jobs', you will shadow this
2561 name. You can either delete your global jobs variable to regain
2590 name. You can either delete your global jobs variable to regain
2562 access to the job manager, or make a new name and assign it manually
2591 access to the job manager, or make a new name and assign it manually
2563 to the manager (stored in IPython's namespace). For example, to
2592 to the manager (stored in IPython's namespace). For example, to
2564 assign the job manager to the Jobs name, use:
2593 assign the job manager to the Jobs name, use:
2565
2594
2566 Jobs = __builtins__.jobs"""
2595 Jobs = __builtins__.jobs"""
2567
2596
2568 self.shell.jobs.new(parameter_s,self.shell.user_ns)
2597 self.shell.jobs.new(parameter_s,self.shell.user_ns)
2569
2598
2570 def magic_store(self, parameter_s=''):
2599 def magic_store(self, parameter_s=''):
2571 """Lightweight persistence for python variables.
2600 """Lightweight persistence for python variables.
2572
2601
2573 Example:
2602 Example:
2574
2603
2575 ville@badger[~]|1> A = ['hello',10,'world']\\
2604 ville@badger[~]|1> A = ['hello',10,'world']\\
2576 ville@badger[~]|2> %store A\\
2605 ville@badger[~]|2> %store A\\
2577 ville@badger[~]|3> Exit
2606 ville@badger[~]|3> Exit
2578
2607
2579 (IPython session is closed and started again...)
2608 (IPython session is closed and started again...)
2580
2609
2581 ville@badger:~$ ipython -p pysh\\
2610 ville@badger:~$ ipython -p pysh\\
2582 ville@badger[~]|1> print A
2611 ville@badger[~]|1> print A
2583
2612
2584 ['hello', 10, 'world']
2613 ['hello', 10, 'world']
2585
2614
2586 Usage:
2615 Usage:
2587
2616
2588 %store - Show list of all variables and their current values\\
2617 %store - Show list of all variables and their current values\\
2589 %store <var> - Store the *current* value of the variable to disk\\
2618 %store <var> - Store the *current* value of the variable to disk\\
2590 %store -d <var> - Remove the variable and its value from storage\\
2619 %store -d <var> - Remove the variable and its value from storage\\
2591 %store -r - Remove all variables from storage
2620 %store -r - Remove all variables from storage
2592
2621
2593 It should be noted that if you change the value of a variable, you
2622 It should be noted that if you change the value of a variable, you
2594 need to %store it again if you want to persist the new value.
2623 need to %store it again if you want to persist the new value.
2595
2624
2596 Note also that the variables will need to be pickleable; most basic
2625 Note also that the variables will need to be pickleable; most basic
2597 python types can be safely %stored.
2626 python types can be safely %stored.
2598 """
2627 """
2599
2628
2600 opts,args = self.parse_options(parameter_s,'dr',mode='list')
2629 opts,args = self.parse_options(parameter_s,'dr',mode='list')
2601 # delete
2630 # delete
2602 if opts.has_key('d'):
2631 if opts.has_key('d'):
2603 try:
2632 try:
2604 todel = args[0]
2633 todel = args[0]
2605 except IndexError:
2634 except IndexError:
2606 error('You must provide the variable to forget')
2635 error('You must provide the variable to forget')
2607 else:
2636 else:
2608 try:
2637 try:
2609 del self.shell.persist['S:' + todel]
2638 del self.shell.persist['S:' + todel]
2610 except:
2639 except:
2611 error("Can't delete variable '%s'" % todel)
2640 error("Can't delete variable '%s'" % todel)
2612 # reset
2641 # reset
2613 elif opts.has_key('r'):
2642 elif opts.has_key('r'):
2614 for k in self.shell.persist.keys():
2643 for k in self.shell.persist.keys():
2615 if k.startswith('S:'):
2644 if k.startswith('S:'):
2616 del self.shell.persist[k]
2645 del self.shell.persist[k]
2617
2646
2618 # run without arguments -> list variables & values
2647 # run without arguments -> list variables & values
2619 elif not args:
2648 elif not args:
2620 vars = [v[2:] for v in self.shell.persist.keys()
2649 vars = [v[2:] for v in self.shell.persist.keys()
2621 if v.startswith('S:')]
2650 if v.startswith('S:')]
2622 vars.sort()
2651 vars.sort()
2623 if vars:
2652 if vars:
2624 size = max(map(len,vars))
2653 size = max(map(len,vars))
2625 else:
2654 else:
2626 size = 0
2655 size = 0
2627
2656
2628 print 'Stored variables and their in-memory values:'
2657 print 'Stored variables and their in-memory values:'
2629 fmt = '%-'+str(size)+'s -> %s'
2658 fmt = '%-'+str(size)+'s -> %s'
2630 get = self.shell.user_ns.get
2659 get = self.shell.user_ns.get
2631 for var in vars:
2660 for var in vars:
2632 # print 30 first characters from every var
2661 # print 30 first characters from every var
2633 print fmt % (var,repr(get(var,'<unavailable>'))[:50])
2662 print fmt % (var,repr(get(var,'<unavailable>'))[:50])
2634
2663
2635 # default action - store the variable
2664 # default action - store the variable
2636 else:
2665 else:
2637 pickled = pickle.dumps(self.shell.user_ns[args[0] ])
2666 pickled = pickle.dumps(self.shell.user_ns[args[0] ])
2638 self.shell.persist[ 'S:' + args[0] ] = pickled
2667 self.shell.persist[ 'S:' + args[0] ] = pickled
2639 print "Stored '%s' (%d bytes)" % (args[0], len(pickled))
2668 print "Stored '%s' (%d bytes)" % (args[0], len(pickled))
2640
2669
2641 def magic_bookmark(self, parameter_s=''):
2670 def magic_bookmark(self, parameter_s=''):
2642 """Manage IPython's bookmark system.
2671 """Manage IPython's bookmark system.
2643
2672
2644 %bookmark <name> - set bookmark to current dir
2673 %bookmark <name> - set bookmark to current dir
2645 %bookmark <name> <dir> - set bookmark to <dir>
2674 %bookmark <name> <dir> - set bookmark to <dir>
2646 %bookmark -l - list all bookmarks
2675 %bookmark -l - list all bookmarks
2647 %bookmark -d <name> - remove bookmark
2676 %bookmark -d <name> - remove bookmark
2648 %bookmark -r - remove all bookmarks
2677 %bookmark -r - remove all bookmarks
2649
2678
2650 You can later on access a bookmarked folder with:
2679 You can later on access a bookmarked folder with:
2651 %cd -b <name>
2680 %cd -b <name>
2652 or simply '%cd <name>' if there is no directory called <name> AND
2681 or simply '%cd <name>' if there is no directory called <name> AND
2653 there is such a bookmark defined.
2682 there is such a bookmark defined.
2654
2683
2655 Your bookmarks persist through IPython sessions, but they are
2684 Your bookmarks persist through IPython sessions, but they are
2656 associated with each profile."""
2685 associated with each profile."""
2657
2686
2658 opts,args = self.parse_options(parameter_s,'drl',mode='list')
2687 opts,args = self.parse_options(parameter_s,'drl',mode='list')
2659 if len(args) > 2:
2688 if len(args) > 2:
2660 error('You can only give at most two arguments')
2689 error('You can only give at most two arguments')
2661 return
2690 return
2662
2691
2663 bkms = self.shell.persist.get('bookmarks',{})
2692 bkms = self.shell.persist.get('bookmarks',{})
2664
2693
2665 if opts.has_key('d'):
2694 if opts.has_key('d'):
2666 try:
2695 try:
2667 todel = args[0]
2696 todel = args[0]
2668 except IndexError:
2697 except IndexError:
2669 error('You must provide a bookmark to delete')
2698 error('You must provide a bookmark to delete')
2670 else:
2699 else:
2671 try:
2700 try:
2672 del bkms[todel]
2701 del bkms[todel]
2673 except:
2702 except:
2674 error("Can't delete bookmark '%s'" % todel)
2703 error("Can't delete bookmark '%s'" % todel)
2675 elif opts.has_key('r'):
2704 elif opts.has_key('r'):
2676 bkms = {}
2705 bkms = {}
2677 elif opts.has_key('l'):
2706 elif opts.has_key('l'):
2678 bks = bkms.keys()
2707 bks = bkms.keys()
2679 bks.sort()
2708 bks.sort()
2680 if bks:
2709 if bks:
2681 size = max(map(len,bks))
2710 size = max(map(len,bks))
2682 else:
2711 else:
2683 size = 0
2712 size = 0
2684 fmt = '%-'+str(size)+'s -> %s'
2713 fmt = '%-'+str(size)+'s -> %s'
2685 print 'Current bookmarks:'
2714 print 'Current bookmarks:'
2686 for bk in bks:
2715 for bk in bks:
2687 print fmt % (bk,bkms[bk])
2716 print fmt % (bk,bkms[bk])
2688 else:
2717 else:
2689 if not args:
2718 if not args:
2690 error("You must specify the bookmark name")
2719 error("You must specify the bookmark name")
2691 elif len(args)==1:
2720 elif len(args)==1:
2692 bkms[args[0]] = os.getcwd()
2721 bkms[args[0]] = os.getcwd()
2693 elif len(args)==2:
2722 elif len(args)==2:
2694 bkms[args[0]] = args[1]
2723 bkms[args[0]] = args[1]
2695 self.shell.persist['bookmarks'] = bkms
2724 self.shell.persist['bookmarks'] = bkms
2696
2725
2697 def magic_pycat(self, parameter_s=''):
2726 def magic_pycat(self, parameter_s=''):
2698 """Show a syntax-highlighted file through a pager.
2727 """Show a syntax-highlighted file through a pager.
2699
2728
2700 This magic is similar to the cat utility, but it will assume the file
2729 This magic is similar to the cat utility, but it will assume the file
2701 to be Python source and will show it with syntax highlighting. """
2730 to be Python source and will show it with syntax highlighting. """
2702
2731
2703 filename = get_py_filename(parameter_s)
2732 filename = get_py_filename(parameter_s)
2704 page(self.shell.colorize(file_read(filename)),
2733 page(self.shell.colorize(file_read(filename)),
2705 screen_lines=self.shell.rc.screen_length)
2734 screen_lines=self.shell.rc.screen_length)
2706
2735
2707 # end Magic
2736 # end Magic
@@ -1,583 +1,583 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 975 2005-12-29 23:50:22Z fperez $"""
5 $Id: Prompts.py 994 2006-01-08 08:29:44Z fperez $"""
6
6
7 #*****************************************************************************
7 #*****************************************************************************
8 # Copyright (C) 2001-2004 Fernando Perez <fperez@colorado.edu>
8 # Copyright (C) 2001-2006 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
22 import os
23 import socket
23 import socket
24 import sys
24 import sys
25 import time
25 import time
26 from pprint import pprint,pformat
26 from pprint import pprint,pformat
27
27
28 # IPython's own
28 # IPython's own
29 from IPython import ColorANSI
29 from IPython import ColorANSI
30 from IPython.Itpl import ItplNS
30 from IPython.Itpl import ItplNS
31 from IPython.Struct import Struct
31 from IPython.Struct import Struct
32 from IPython.macro import Macro
32 from IPython.macro import Macro
33 from IPython.genutils import *
33 from IPython.genutils import *
34
34
35 #****************************************************************************
35 #****************************************************************************
36 #Color schemes for Prompts.
36 #Color schemes for Prompts.
37
37
38 PromptColors = ColorANSI.ColorSchemeTable()
38 PromptColors = ColorANSI.ColorSchemeTable()
39 InputColors = ColorANSI.InputTermColors # just a shorthand
39 InputColors = ColorANSI.InputTermColors # just a shorthand
40 Colors = ColorANSI.TermColors # just a shorthand
40 Colors = ColorANSI.TermColors # just a shorthand
41
41
42 PromptColors.add_scheme(ColorANSI.ColorScheme(
42 PromptColors.add_scheme(ColorANSI.ColorScheme(
43 'NoColor',
43 'NoColor',
44 in_prompt = InputColors.NoColor, # Input prompt
44 in_prompt = InputColors.NoColor, # Input prompt
45 in_number = InputColors.NoColor, # Input prompt number
45 in_number = InputColors.NoColor, # Input prompt number
46 in_prompt2 = InputColors.NoColor, # Continuation prompt
46 in_prompt2 = InputColors.NoColor, # Continuation prompt
47 in_normal = InputColors.NoColor, # color off (usu. Colors.Normal)
47 in_normal = InputColors.NoColor, # color off (usu. Colors.Normal)
48
48
49 out_prompt = Colors.NoColor, # Output prompt
49 out_prompt = Colors.NoColor, # Output prompt
50 out_number = Colors.NoColor, # Output prompt number
50 out_number = Colors.NoColor, # Output prompt number
51
51
52 normal = Colors.NoColor # color off (usu. Colors.Normal)
52 normal = Colors.NoColor # color off (usu. Colors.Normal)
53 ))
53 ))
54
54
55 # make some schemes as instances so we can copy them for modification easily:
55 # make some schemes as instances so we can copy them for modification easily:
56 __PColLinux = ColorANSI.ColorScheme(
56 __PColLinux = ColorANSI.ColorScheme(
57 'Linux',
57 'Linux',
58 in_prompt = InputColors.Green,
58 in_prompt = InputColors.Green,
59 in_number = InputColors.LightGreen,
59 in_number = InputColors.LightGreen,
60 in_prompt2 = InputColors.Green,
60 in_prompt2 = InputColors.Green,
61 in_normal = InputColors.Normal, # color off (usu. Colors.Normal)
61 in_normal = InputColors.Normal, # color off (usu. Colors.Normal)
62
62
63 out_prompt = Colors.Red,
63 out_prompt = Colors.Red,
64 out_number = Colors.LightRed,
64 out_number = Colors.LightRed,
65
65
66 normal = Colors.Normal
66 normal = Colors.Normal
67 )
67 )
68 # Don't forget to enter it into the table!
68 # Don't forget to enter it into the table!
69 PromptColors.add_scheme(__PColLinux)
69 PromptColors.add_scheme(__PColLinux)
70
70
71 # Slightly modified Linux for light backgrounds
71 # Slightly modified Linux for light backgrounds
72 __PColLightBG = __PColLinux.copy('LightBG')
72 __PColLightBG = __PColLinux.copy('LightBG')
73
73
74 __PColLightBG.colors.update(
74 __PColLightBG.colors.update(
75 in_prompt = InputColors.Blue,
75 in_prompt = InputColors.Blue,
76 in_number = InputColors.LightBlue,
76 in_number = InputColors.LightBlue,
77 in_prompt2 = InputColors.Blue
77 in_prompt2 = InputColors.Blue
78 )
78 )
79 PromptColors.add_scheme(__PColLightBG)
79 PromptColors.add_scheme(__PColLightBG)
80
80
81 del Colors,InputColors
81 del Colors,InputColors
82
82
83 #-----------------------------------------------------------------------------
83 #-----------------------------------------------------------------------------
84 def multiple_replace(dict, text):
84 def multiple_replace(dict, text):
85 """ Replace in 'text' all occurences of any key in the given
85 """ Replace in 'text' all occurences of any key in the given
86 dictionary by its corresponding value. Returns the new string."""
86 dictionary by its corresponding value. Returns the new string."""
87
87
88 # Function by Xavier Defrang, originally found at:
88 # Function by Xavier Defrang, originally found at:
89 # http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/81330
89 # http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/81330
90
90
91 # Create a regular expression from the dictionary keys
91 # Create a regular expression from the dictionary keys
92 regex = re.compile("(%s)" % "|".join(map(re.escape, dict.keys())))
92 regex = re.compile("(%s)" % "|".join(map(re.escape, dict.keys())))
93 # For each match, look-up corresponding value in dictionary
93 # For each match, look-up corresponding value in dictionary
94 return regex.sub(lambda mo: dict[mo.string[mo.start():mo.end()]], text)
94 return regex.sub(lambda mo: dict[mo.string[mo.start():mo.end()]], text)
95
95
96 #-----------------------------------------------------------------------------
96 #-----------------------------------------------------------------------------
97 # Special characters that can be used in prompt templates, mainly bash-like
97 # Special characters that can be used in prompt templates, mainly bash-like
98
98
99 # If $HOME isn't defined (Windows), make it an absurd string so that it can
99 # If $HOME isn't defined (Windows), make it an absurd string so that it can
100 # never be expanded out into '~'. Basically anything which can never be a
100 # never be expanded out into '~'. Basically anything which can never be a
101 # reasonable directory name will do, we just want the $HOME -> '~' operation
101 # reasonable directory name will do, we just want the $HOME -> '~' operation
102 # to become a no-op. We pre-compute $HOME here so it's not done on every
102 # to become a no-op. We pre-compute $HOME here so it's not done on every
103 # prompt call.
103 # prompt call.
104
104
105 # FIXME:
105 # FIXME:
106
106
107 # - This should be turned into a class which does proper namespace management,
107 # - This should be turned into a class which does proper namespace management,
108 # since the prompt specials need to be evaluated in a certain namespace.
108 # since the prompt specials need to be evaluated in a certain namespace.
109 # Currently it's just globals, which need to be managed manually by code
109 # Currently it's just globals, which need to be managed manually by code
110 # below.
110 # below.
111
111
112 # - I also need to split up the color schemes from the prompt specials
112 # - I also need to split up the color schemes from the prompt specials
113 # somehow. I don't have a clean design for that quite yet.
113 # somehow. I don't have a clean design for that quite yet.
114
114
115 HOME = os.environ.get("HOME","//////:::::ZZZZZ,,,~~~")
115 HOME = os.environ.get("HOME","//////:::::ZZZZZ,,,~~~")
116
116
117 # We precompute a few more strings here for the prompt_specials, which are
117 # We precompute a few more strings here for the prompt_specials, which are
118 # fixed once ipython starts. This reduces the runtime overhead of computing
118 # fixed once ipython starts. This reduces the runtime overhead of computing
119 # prompt strings.
119 # prompt strings.
120 USER = os.environ.get("USER")
120 USER = os.environ.get("USER")
121 HOSTNAME = socket.gethostname()
121 HOSTNAME = socket.gethostname()
122 HOSTNAME_SHORT = HOSTNAME.split(".")[0]
122 HOSTNAME_SHORT = HOSTNAME.split(".")[0]
123 ROOT_SYMBOL = "$#"[os.name=='nt' or os.getuid()==0]
123 ROOT_SYMBOL = "$#"[os.name=='nt' or os.getuid()==0]
124
124
125 prompt_specials_color = {
125 prompt_specials_color = {
126 # Prompt/history count
126 # Prompt/history count
127 '%n' : '${self.col_num}' '${self.cache.prompt_count}' '${self.col_p}',
127 '%n' : '${self.col_num}' '${self.cache.prompt_count}' '${self.col_p}',
128 '\\#': '${self.col_num}' '${self.cache.prompt_count}' '${self.col_p}',
128 '\\#': '${self.col_num}' '${self.cache.prompt_count}' '${self.col_p}',
129 # Prompt/history count, with the actual digits replaced by dots. Used
129 # Prompt/history count, with the actual digits replaced by dots. Used
130 # mainly in continuation prompts (prompt_in2)
130 # mainly in continuation prompts (prompt_in2)
131 '\\D': '${"."*len(str(self.cache.prompt_count))}',
131 '\\D': '${"."*len(str(self.cache.prompt_count))}',
132 # Current working directory
132 # Current working directory
133 '\\w': '${os.getcwd()}',
133 '\\w': '${os.getcwd()}',
134 # Current time
134 # Current time
135 '\\t' : '${time.strftime("%H:%M:%S")}',
135 '\\t' : '${time.strftime("%H:%M:%S")}',
136 # Basename of current working directory.
136 # Basename of current working directory.
137 # (use os.sep to make this portable across OSes)
137 # (use os.sep to make this portable across OSes)
138 '\\W' : '${os.getcwd().split("%s")[-1]}' % os.sep,
138 '\\W' : '${os.getcwd().split("%s")[-1]}' % os.sep,
139 # These X<N> are an extension to the normal bash prompts. They return
139 # These X<N> are an extension to the normal bash prompts. They return
140 # N terms of the path, after replacing $HOME with '~'
140 # N terms of the path, after replacing $HOME with '~'
141 '\\X0': '${os.getcwd().replace("%s","~")}' % HOME,
141 '\\X0': '${os.getcwd().replace("%s","~")}' % HOME,
142 '\\X1': '${self.cwd_filt(1)}',
142 '\\X1': '${self.cwd_filt(1)}',
143 '\\X2': '${self.cwd_filt(2)}',
143 '\\X2': '${self.cwd_filt(2)}',
144 '\\X3': '${self.cwd_filt(3)}',
144 '\\X3': '${self.cwd_filt(3)}',
145 '\\X4': '${self.cwd_filt(4)}',
145 '\\X4': '${self.cwd_filt(4)}',
146 '\\X5': '${self.cwd_filt(5)}',
146 '\\X5': '${self.cwd_filt(5)}',
147 # Y<N> are similar to X<N>, but they show '~' if it's the directory
147 # Y<N> are similar to X<N>, but they show '~' if it's the directory
148 # N+1 in the list. Somewhat like %cN in tcsh.
148 # N+1 in the list. Somewhat like %cN in tcsh.
149 '\\Y0': '${self.cwd_filt2(0)}',
149 '\\Y0': '${self.cwd_filt2(0)}',
150 '\\Y1': '${self.cwd_filt2(1)}',
150 '\\Y1': '${self.cwd_filt2(1)}',
151 '\\Y2': '${self.cwd_filt2(2)}',
151 '\\Y2': '${self.cwd_filt2(2)}',
152 '\\Y3': '${self.cwd_filt2(3)}',
152 '\\Y3': '${self.cwd_filt2(3)}',
153 '\\Y4': '${self.cwd_filt2(4)}',
153 '\\Y4': '${self.cwd_filt2(4)}',
154 '\\Y5': '${self.cwd_filt2(5)}',
154 '\\Y5': '${self.cwd_filt2(5)}',
155 # Hostname up to first .
155 # Hostname up to first .
156 '\\h': HOSTNAME_SHORT,
156 '\\h': HOSTNAME_SHORT,
157 # Full hostname
157 # Full hostname
158 '\\H': HOSTNAME,
158 '\\H': HOSTNAME,
159 # Username of current user
159 # Username of current user
160 '\\u': USER,
160 '\\u': USER,
161 # Escaped '\'
161 # Escaped '\'
162 '\\\\': '\\',
162 '\\\\': '\\',
163 # Newline
163 # Newline
164 '\\n': '\n',
164 '\\n': '\n',
165 # Carriage return
165 # Carriage return
166 '\\r': '\r',
166 '\\r': '\r',
167 # Release version
167 # Release version
168 '\\v': __version__,
168 '\\v': __version__,
169 # Root symbol ($ or #)
169 # Root symbol ($ or #)
170 '\\$': ROOT_SYMBOL,
170 '\\$': ROOT_SYMBOL,
171 }
171 }
172
172
173 # A copy of the prompt_specials dictionary but with all color escapes removed,
173 # A copy of the prompt_specials dictionary but with all color escapes removed,
174 # so we can correctly compute the prompt length for the auto_rewrite method.
174 # so we can correctly compute the prompt length for the auto_rewrite method.
175 prompt_specials_nocolor = prompt_specials_color.copy()
175 prompt_specials_nocolor = prompt_specials_color.copy()
176 prompt_specials_nocolor['%n'] = '${self.cache.prompt_count}'
176 prompt_specials_nocolor['%n'] = '${self.cache.prompt_count}'
177 prompt_specials_nocolor['\\#'] = '${self.cache.prompt_count}'
177 prompt_specials_nocolor['\\#'] = '${self.cache.prompt_count}'
178
178
179 # Add in all the InputTermColors color escapes as valid prompt characters.
179 # Add in all the InputTermColors color escapes as valid prompt characters.
180 # They all get added as \\C_COLORNAME, so that we don't have any conflicts
180 # They all get added as \\C_COLORNAME, so that we don't have any conflicts
181 # with a color name which may begin with a letter used by any other of the
181 # with a color name which may begin with a letter used by any other of the
182 # allowed specials. This of course means that \\C will never be allowed for
182 # allowed specials. This of course means that \\C will never be allowed for
183 # anything else.
183 # anything else.
184 input_colors = ColorANSI.InputTermColors
184 input_colors = ColorANSI.InputTermColors
185 for _color in dir(input_colors):
185 for _color in dir(input_colors):
186 if _color[0] != '_':
186 if _color[0] != '_':
187 c_name = '\\C_'+_color
187 c_name = '\\C_'+_color
188 prompt_specials_color[c_name] = getattr(input_colors,_color)
188 prompt_specials_color[c_name] = getattr(input_colors,_color)
189 prompt_specials_nocolor[c_name] = ''
189 prompt_specials_nocolor[c_name] = ''
190
190
191 # we default to no color for safety. Note that prompt_specials is a global
191 # we default to no color for safety. Note that prompt_specials is a global
192 # variable used by all prompt objects.
192 # variable used by all prompt objects.
193 prompt_specials = prompt_specials_nocolor
193 prompt_specials = prompt_specials_nocolor
194
194
195 #-----------------------------------------------------------------------------
195 #-----------------------------------------------------------------------------
196 def str_safe(arg):
196 def str_safe(arg):
197 """Convert to a string, without ever raising an exception.
197 """Convert to a string, without ever raising an exception.
198
198
199 If str(arg) fails, <ERROR: ... > is returned, where ... is the exception
199 If str(arg) fails, <ERROR: ... > is returned, where ... is the exception
200 error message."""
200 error message."""
201
201
202 try:
202 try:
203 out = str(arg)
203 out = str(arg)
204 except UnicodeError:
204 except UnicodeError:
205 try:
205 try:
206 out = arg.encode('utf_8','replace')
206 out = arg.encode('utf_8','replace')
207 except Exception,msg:
207 except Exception,msg:
208 # let's keep this little duplication here, so that the most common
208 # let's keep this little duplication here, so that the most common
209 # case doesn't suffer from a double try wrapping.
209 # case doesn't suffer from a double try wrapping.
210 out = '<ERROR: %s>' % msg
210 out = '<ERROR: %s>' % msg
211 except Exception,msg:
211 except Exception,msg:
212 out = '<ERROR: %s>' % msg
212 out = '<ERROR: %s>' % msg
213 return out
213 return out
214
214
215 class BasePrompt:
215 class BasePrompt:
216 """Interactive prompt similar to Mathematica's."""
216 """Interactive prompt similar to Mathematica's."""
217 def __init__(self,cache,sep,prompt,pad_left=False):
217 def __init__(self,cache,sep,prompt,pad_left=False):
218
218
219 # Hack: we access information about the primary prompt through the
219 # Hack: we access information about the primary prompt through the
220 # cache argument. We need this, because we want the secondary prompt
220 # cache argument. We need this, because we want the secondary prompt
221 # to be aligned with the primary one. Color table info is also shared
221 # to be aligned with the primary one. Color table info is also shared
222 # by all prompt classes through the cache. Nice OO spaghetti code!
222 # by all prompt classes through the cache. Nice OO spaghetti code!
223 self.cache = cache
223 self.cache = cache
224 self.sep = sep
224 self.sep = sep
225
225
226 # regexp to count the number of spaces at the end of a prompt
226 # regexp to count the number of spaces at the end of a prompt
227 # expression, useful for prompt auto-rewriting
227 # expression, useful for prompt auto-rewriting
228 self.rspace = re.compile(r'(\s*)$')
228 self.rspace = re.compile(r'(\s*)$')
229 # Flag to left-pad prompt strings to match the length of the primary
229 # Flag to left-pad prompt strings to match the length of the primary
230 # prompt
230 # prompt
231 self.pad_left = pad_left
231 self.pad_left = pad_left
232 # Set template to create each actual prompt (where numbers change)
232 # Set template to create each actual prompt (where numbers change)
233 self.p_template = prompt
233 self.p_template = prompt
234 self.set_p_str()
234 self.set_p_str()
235
235
236 def set_p_str(self):
236 def set_p_str(self):
237 """ Set the interpolating prompt strings.
237 """ Set the interpolating prompt strings.
238
238
239 This must be called every time the color settings change, because the
239 This must be called every time the color settings change, because the
240 prompt_specials global may have changed."""
240 prompt_specials global may have changed."""
241
241
242 import os,time # needed in locals for prompt string handling
242 import os,time # needed in locals for prompt string handling
243 loc = locals()
243 loc = locals()
244 self.p_str = ItplNS('%s%s%s' %
244 self.p_str = ItplNS('%s%s%s' %
245 ('${self.sep}${self.col_p}',
245 ('${self.sep}${self.col_p}',
246 multiple_replace(prompt_specials, self.p_template),
246 multiple_replace(prompt_specials, self.p_template),
247 '${self.col_norm}'),self.cache.user_ns,loc)
247 '${self.col_norm}'),self.cache.user_ns,loc)
248
248
249 self.p_str_nocolor = ItplNS(multiple_replace(prompt_specials_nocolor,
249 self.p_str_nocolor = ItplNS(multiple_replace(prompt_specials_nocolor,
250 self.p_template),
250 self.p_template),
251 self.cache.user_ns,loc)
251 self.cache.user_ns,loc)
252
252
253 def write(self,msg): # dbg
253 def write(self,msg): # dbg
254 sys.stdout.write(msg)
254 sys.stdout.write(msg)
255 return ''
255 return ''
256
256
257 def __str__(self):
257 def __str__(self):
258 """Return a string form of the prompt.
258 """Return a string form of the prompt.
259
259
260 This for is useful for continuation and output prompts, since it is
260 This for is useful for continuation and output prompts, since it is
261 left-padded to match lengths with the primary one (if the
261 left-padded to match lengths with the primary one (if the
262 self.pad_left attribute is set)."""
262 self.pad_left attribute is set)."""
263
263
264 out_str = str_safe(self.p_str)
264 out_str = str_safe(self.p_str)
265 if self.pad_left:
265 if self.pad_left:
266 # We must find the amount of padding required to match lengths,
266 # We must find the amount of padding required to match lengths,
267 # taking the color escapes (which are invisible on-screen) into
267 # taking the color escapes (which are invisible on-screen) into
268 # account.
268 # account.
269 esc_pad = len(out_str) - len(str_safe(self.p_str_nocolor))
269 esc_pad = len(out_str) - len(str_safe(self.p_str_nocolor))
270 format = '%%%ss' % (len(str(self.cache.last_prompt))+esc_pad)
270 format = '%%%ss' % (len(str(self.cache.last_prompt))+esc_pad)
271 return format % out_str
271 return format % out_str
272 else:
272 else:
273 return out_str
273 return out_str
274
274
275 # these path filters are put in as methods so that we can control the
275 # these path filters are put in as methods so that we can control the
276 # namespace where the prompt strings get evaluated
276 # namespace where the prompt strings get evaluated
277 def cwd_filt(self,depth):
277 def cwd_filt(self,depth):
278 """Return the last depth elements of the current working directory.
278 """Return the last depth elements of the current working directory.
279
279
280 $HOME is always replaced with '~'.
280 $HOME is always replaced with '~'.
281 If depth==0, the full path is returned."""
281 If depth==0, the full path is returned."""
282
282
283 cwd = os.getcwd().replace(HOME,"~")
283 cwd = os.getcwd().replace(HOME,"~")
284 out = os.sep.join(cwd.split(os.sep)[-depth:])
284 out = os.sep.join(cwd.split(os.sep)[-depth:])
285 if out:
285 if out:
286 return out
286 return out
287 else:
287 else:
288 return os.sep
288 return os.sep
289
289
290 def cwd_filt2(self,depth):
290 def cwd_filt2(self,depth):
291 """Return the last depth elements of the current working directory.
291 """Return the last depth elements of the current working directory.
292
292
293 $HOME is always replaced with '~'.
293 $HOME is always replaced with '~'.
294 If depth==0, the full path is returned."""
294 If depth==0, the full path is returned."""
295
295
296 cwd = os.getcwd().replace(HOME,"~").split(os.sep)
296 cwd = os.getcwd().replace(HOME,"~").split(os.sep)
297 if '~' in cwd and len(cwd) == depth+1:
297 if '~' in cwd and len(cwd) == depth+1:
298 depth += 1
298 depth += 1
299 out = os.sep.join(cwd[-depth:])
299 out = os.sep.join(cwd[-depth:])
300 if out:
300 if out:
301 return out
301 return out
302 else:
302 else:
303 return os.sep
303 return os.sep
304
304
305 class Prompt1(BasePrompt):
305 class Prompt1(BasePrompt):
306 """Input interactive prompt similar to Mathematica's."""
306 """Input interactive prompt similar to Mathematica's."""
307
307
308 def __init__(self,cache,sep='\n',prompt='In [\\#]: ',pad_left=True):
308 def __init__(self,cache,sep='\n',prompt='In [\\#]: ',pad_left=True):
309 BasePrompt.__init__(self,cache,sep,prompt,pad_left)
309 BasePrompt.__init__(self,cache,sep,prompt,pad_left)
310
310
311 def set_colors(self):
311 def set_colors(self):
312 self.set_p_str()
312 self.set_p_str()
313 Colors = self.cache.color_table.active_colors # shorthand
313 Colors = self.cache.color_table.active_colors # shorthand
314 self.col_p = Colors.in_prompt
314 self.col_p = Colors.in_prompt
315 self.col_num = Colors.in_number
315 self.col_num = Colors.in_number
316 self.col_norm = Colors.in_normal
316 self.col_norm = Colors.in_normal
317 # We need a non-input version of these escapes for the '--->'
317 # We need a non-input version of these escapes for the '--->'
318 # auto-call prompts used in the auto_rewrite() method.
318 # auto-call prompts used in the auto_rewrite() method.
319 self.col_p_ni = self.col_p.replace('\001','').replace('\002','')
319 self.col_p_ni = self.col_p.replace('\001','').replace('\002','')
320 self.col_norm_ni = Colors.normal
320 self.col_norm_ni = Colors.normal
321
321
322 def __str__(self):
322 def __str__(self):
323 self.cache.prompt_count += 1
323 self.cache.prompt_count += 1
324 self.cache.last_prompt = str_safe(self.p_str_nocolor).split('\n')[-1]
324 self.cache.last_prompt = str_safe(self.p_str_nocolor).split('\n')[-1]
325 return str_safe(self.p_str)
325 return str_safe(self.p_str)
326
326
327 def auto_rewrite(self):
327 def auto_rewrite(self):
328 """Print a string of the form '--->' which lines up with the previous
328 """Print a string of the form '--->' which lines up with the previous
329 input string. Useful for systems which re-write the user input when
329 input string. Useful for systems which re-write the user input when
330 handling automatically special syntaxes."""
330 handling automatically special syntaxes."""
331
331
332 curr = str(self.cache.last_prompt)
332 curr = str(self.cache.last_prompt)
333 nrspaces = len(self.rspace.search(curr).group())
333 nrspaces = len(self.rspace.search(curr).group())
334 return '%s%s>%s%s' % (self.col_p_ni,'-'*(len(curr)-nrspaces-1),
334 return '%s%s>%s%s' % (self.col_p_ni,'-'*(len(curr)-nrspaces-1),
335 ' '*nrspaces,self.col_norm_ni)
335 ' '*nrspaces,self.col_norm_ni)
336
336
337 class PromptOut(BasePrompt):
337 class PromptOut(BasePrompt):
338 """Output interactive prompt similar to Mathematica's."""
338 """Output interactive prompt similar to Mathematica's."""
339
339
340 def __init__(self,cache,sep='',prompt='Out[\\#]: ',pad_left=True):
340 def __init__(self,cache,sep='',prompt='Out[\\#]: ',pad_left=True):
341 BasePrompt.__init__(self,cache,sep,prompt,pad_left)
341 BasePrompt.__init__(self,cache,sep,prompt,pad_left)
342 if not self.p_template:
342 if not self.p_template:
343 self.__str__ = lambda: ''
343 self.__str__ = lambda: ''
344
344
345 def set_colors(self):
345 def set_colors(self):
346 self.set_p_str()
346 self.set_p_str()
347 Colors = self.cache.color_table.active_colors # shorthand
347 Colors = self.cache.color_table.active_colors # shorthand
348 self.col_p = Colors.out_prompt
348 self.col_p = Colors.out_prompt
349 self.col_num = Colors.out_number
349 self.col_num = Colors.out_number
350 self.col_norm = Colors.normal
350 self.col_norm = Colors.normal
351
351
352 class Prompt2(BasePrompt):
352 class Prompt2(BasePrompt):
353 """Interactive continuation prompt."""
353 """Interactive continuation prompt."""
354
354
355 def __init__(self,cache,prompt=' .\\D.: ',pad_left=True):
355 def __init__(self,cache,prompt=' .\\D.: ',pad_left=True):
356 self.cache = cache
356 self.cache = cache
357 self.p_template = prompt
357 self.p_template = prompt
358 self.pad_left = pad_left
358 self.pad_left = pad_left
359 self.set_p_str()
359 self.set_p_str()
360
360
361 def set_p_str(self):
361 def set_p_str(self):
362 import os,time # needed in locals for prompt string handling
362 import os,time # needed in locals for prompt string handling
363 loc = locals()
363 loc = locals()
364 self.p_str = ItplNS('%s%s%s' %
364 self.p_str = ItplNS('%s%s%s' %
365 ('${self.col_p2}',
365 ('${self.col_p2}',
366 multiple_replace(prompt_specials, self.p_template),
366 multiple_replace(prompt_specials, self.p_template),
367 '$self.col_norm'),
367 '$self.col_norm'),
368 self.cache.user_ns,loc)
368 self.cache.user_ns,loc)
369 self.p_str_nocolor = ItplNS(multiple_replace(prompt_specials_nocolor,
369 self.p_str_nocolor = ItplNS(multiple_replace(prompt_specials_nocolor,
370 self.p_template),
370 self.p_template),
371 self.cache.user_ns,loc)
371 self.cache.user_ns,loc)
372
372
373 def set_colors(self):
373 def set_colors(self):
374 self.set_p_str()
374 self.set_p_str()
375 Colors = self.cache.color_table.active_colors
375 Colors = self.cache.color_table.active_colors
376 self.col_p2 = Colors.in_prompt2
376 self.col_p2 = Colors.in_prompt2
377 self.col_norm = Colors.in_normal
377 self.col_norm = Colors.in_normal
378 # FIXME (2004-06-16) HACK: prevent crashes for users who haven't
378 # FIXME (2004-06-16) HACK: prevent crashes for users who haven't
379 # updated their prompt_in2 definitions. Remove eventually.
379 # updated their prompt_in2 definitions. Remove eventually.
380 self.col_p = Colors.out_prompt
380 self.col_p = Colors.out_prompt
381 self.col_num = Colors.out_number
381 self.col_num = Colors.out_number
382
382
383 #-----------------------------------------------------------------------------
383 #-----------------------------------------------------------------------------
384 class CachedOutput:
384 class CachedOutput:
385 """Class for printing output from calculations while keeping a cache of
385 """Class for printing output from calculations while keeping a cache of
386 reults. It dynamically creates global variables prefixed with _ which
386 reults. It dynamically creates global variables prefixed with _ which
387 contain these results.
387 contain these results.
388
388
389 Meant to be used as a sys.displayhook replacement, providing numbered
389 Meant to be used as a sys.displayhook replacement, providing numbered
390 prompts and cache services.
390 prompts and cache services.
391
391
392 Initialize with initial and final values for cache counter (this defines
392 Initialize with initial and final values for cache counter (this defines
393 the maximum size of the cache."""
393 the maximum size of the cache."""
394
394
395 def __init__(self,shell,cache_size,Pprint,
395 def __init__(self,shell,cache_size,Pprint,
396 colors='NoColor',input_sep='\n',
396 colors='NoColor',input_sep='\n',
397 output_sep='\n',output_sep2='',
397 output_sep='\n',output_sep2='',
398 ps1 = None, ps2 = None,ps_out = None,pad_left=True):
398 ps1 = None, ps2 = None,ps_out = None,pad_left=True):
399
399
400 cache_size_min = 20
400 cache_size_min = 20
401 if cache_size <= 0:
401 if cache_size <= 0:
402 self.do_full_cache = 0
402 self.do_full_cache = 0
403 cache_size = 0
403 cache_size = 0
404 elif cache_size < cache_size_min:
404 elif cache_size < cache_size_min:
405 self.do_full_cache = 0
405 self.do_full_cache = 0
406 cache_size = 0
406 cache_size = 0
407 warn('caching was disabled (min value for cache size is %s).' %
407 warn('caching was disabled (min value for cache size is %s).' %
408 cache_size_min,level=3)
408 cache_size_min,level=3)
409 else:
409 else:
410 self.do_full_cache = 1
410 self.do_full_cache = 1
411
411
412 self.cache_size = cache_size
412 self.cache_size = cache_size
413 self.input_sep = input_sep
413 self.input_sep = input_sep
414
414
415 # we need a reference to the user-level namespace
415 # we need a reference to the user-level namespace
416 self.shell = shell
416 self.shell = shell
417 self.user_ns = shell.user_ns
417 self.user_ns = shell.user_ns
418 # and to the user's input
418 # and to the user's input
419 self.input_hist = shell.input_hist
419 self.input_hist = shell.input_hist
420 # and to the user's logger, for logging output
420 # and to the user's logger, for logging output
421 self.logger = shell.logger
421 self.logger = shell.logger
422
422
423 # Set input prompt strings and colors
423 # Set input prompt strings and colors
424 if cache_size == 0:
424 if cache_size == 0:
425 if ps1.find('%n') > -1 or ps1.find('\\#') > -1: ps1 = '>>> '
425 if ps1.find('%n') > -1 or ps1.find('\\#') > -1: ps1 = '>>> '
426 if ps2.find('%n') > -1 or ps2.find('\\#') > -1: ps2 = '... '
426 if ps2.find('%n') > -1 or ps2.find('\\#') > -1: ps2 = '... '
427 self.ps1_str = self._set_prompt_str(ps1,'In [\\#]: ','>>> ')
427 self.ps1_str = self._set_prompt_str(ps1,'In [\\#]: ','>>> ')
428 self.ps2_str = self._set_prompt_str(ps2,' .\\D.: ','... ')
428 self.ps2_str = self._set_prompt_str(ps2,' .\\D.: ','... ')
429 self.ps_out_str = self._set_prompt_str(ps_out,'Out[\\#]: ','')
429 self.ps_out_str = self._set_prompt_str(ps_out,'Out[\\#]: ','')
430
430
431 self.color_table = PromptColors
431 self.color_table = PromptColors
432 self.prompt1 = Prompt1(self,sep=input_sep,prompt=self.ps1_str,
432 self.prompt1 = Prompt1(self,sep=input_sep,prompt=self.ps1_str,
433 pad_left=pad_left)
433 pad_left=pad_left)
434 self.prompt2 = Prompt2(self,prompt=self.ps2_str,pad_left=pad_left)
434 self.prompt2 = Prompt2(self,prompt=self.ps2_str,pad_left=pad_left)
435 self.prompt_out = PromptOut(self,sep='',prompt=self.ps_out_str,
435 self.prompt_out = PromptOut(self,sep='',prompt=self.ps_out_str,
436 pad_left=pad_left)
436 pad_left=pad_left)
437 self.set_colors(colors)
437 self.set_colors(colors)
438
438
439 # other more normal stuff
439 # other more normal stuff
440 # b/c each call to the In[] prompt raises it by 1, even the first.
440 # b/c each call to the In[] prompt raises it by 1, even the first.
441 self.prompt_count = 0
441 self.prompt_count = 0
442 self.cache_count = 1
442 self.cache_count = 1
443 # Store the last prompt string each time, we need it for aligning
443 # Store the last prompt string each time, we need it for aligning
444 # continuation and auto-rewrite prompts
444 # continuation and auto-rewrite prompts
445 self.last_prompt = ''
445 self.last_prompt = ''
446 self.entries = [None] # output counter starts at 1 for the user
446 self.entries = [None] # output counter starts at 1 for the user
447 self.Pprint = Pprint
447 self.Pprint = Pprint
448 self.output_sep = output_sep
448 self.output_sep = output_sep
449 self.output_sep2 = output_sep2
449 self.output_sep2 = output_sep2
450 self._,self.__,self.___ = '','',''
450 self._,self.__,self.___ = '','',''
451 self.pprint_types = map(type,[(),[],{}])
451 self.pprint_types = map(type,[(),[],{}])
452
452
453 # these are deliberately global:
453 # these are deliberately global:
454 to_user_ns = {'_':self._,'__':self.__,'___':self.___}
454 to_user_ns = {'_':self._,'__':self.__,'___':self.___}
455 self.user_ns.update(to_user_ns)
455 self.user_ns.update(to_user_ns)
456
456
457 def _set_prompt_str(self,p_str,cache_def,no_cache_def):
457 def _set_prompt_str(self,p_str,cache_def,no_cache_def):
458 if p_str is None:
458 if p_str is None:
459 if self.do_full_cache:
459 if self.do_full_cache:
460 return cache_def
460 return cache_def
461 else:
461 else:
462 return no_cache_def
462 return no_cache_def
463 else:
463 else:
464 return p_str
464 return p_str
465
465
466 def set_colors(self,colors):
466 def set_colors(self,colors):
467 """Set the active color scheme and configure colors for the three
467 """Set the active color scheme and configure colors for the three
468 prompt subsystems."""
468 prompt subsystems."""
469
469
470 # FIXME: the prompt_specials global should be gobbled inside this
470 # FIXME: the prompt_specials global should be gobbled inside this
471 # class instead. Do it when cleaning up the whole 3-prompt system.
471 # class instead. Do it when cleaning up the whole 3-prompt system.
472 global prompt_specials
472 global prompt_specials
473 if colors.lower()=='nocolor':
473 if colors.lower()=='nocolor':
474 prompt_specials = prompt_specials_nocolor
474 prompt_specials = prompt_specials_nocolor
475 else:
475 else:
476 prompt_specials = prompt_specials_color
476 prompt_specials = prompt_specials_color
477
477
478 self.color_table.set_active_scheme(colors)
478 self.color_table.set_active_scheme(colors)
479 self.prompt1.set_colors()
479 self.prompt1.set_colors()
480 self.prompt2.set_colors()
480 self.prompt2.set_colors()
481 self.prompt_out.set_colors()
481 self.prompt_out.set_colors()
482
482
483 def __call__(self,arg=None):
483 def __call__(self,arg=None):
484 """Printing with history cache management.
484 """Printing with history cache management.
485
485
486 This is invoked everytime the interpreter needs to print, and is
486 This is invoked everytime the interpreter needs to print, and is
487 activated by setting the variable sys.displayhook to it."""
487 activated by setting the variable sys.displayhook to it."""
488
488
489 # If something injected a '_' variable in __builtin__, delete
489 # If something injected a '_' variable in __builtin__, delete
490 # ipython's automatic one so we don't clobber that. gettext() in
490 # ipython's automatic one so we don't clobber that. gettext() in
491 # particular uses _, so we need to stay away from it.
491 # particular uses _, so we need to stay away from it.
492 if '_' in __builtin__.__dict__:
492 if '_' in __builtin__.__dict__:
493 try:
493 try:
494 del self.user_ns['_']
494 del self.user_ns['_']
495 except KeyError:
495 except KeyError:
496 pass
496 pass
497 if arg is not None:
497 if arg is not None:
498 cout_write = Term.cout.write # fast lookup
498 cout_write = Term.cout.write # fast lookup
499 # first handle the cache and counters
499 # first handle the cache and counters
500 # but avoid recursive reference when displaying _oh/Out
500 # but avoid recursive reference when displaying _oh/Out
501 if arg is not self.user_ns['_oh']:
501 if arg is not self.user_ns['_oh']:
502 self.update(arg)
502 self.update(arg)
503 # do not print output if input ends in ';'
503 # do not print output if input ends in ';'
504 if self.input_hist[self.prompt_count].endswith(';\n'):
504 if self.input_hist[self.prompt_count].endswith(';\n'):
505 return
505 return
506 # don't use print, puts an extra space
506 # don't use print, puts an extra space
507 cout_write(self.output_sep)
507 cout_write(self.output_sep)
508 if self.do_full_cache:
508 if self.do_full_cache:
509 cout_write(str(self.prompt_out))
509 cout_write(str(self.prompt_out))
510
510
511 if isinstance(arg,Macro):
511 if isinstance(arg,Macro):
512 print 'Executing Macro...'
512 print 'Executing Macro...'
513 # in case the macro takes a long time to execute
513 # in case the macro takes a long time to execute
514 Term.cout.flush()
514 Term.cout.flush()
515 self.shell.runlines(arg.value)
515 self.shell.runlines(arg.value)
516 return None
516 return None
517
517
518 # and now call a possibly user-defined print mechanism
518 # and now call a possibly user-defined print mechanism
519 self.display(arg)
519 self.display(arg)
520 if self.logger.log_output:
520 if self.logger.log_output:
521 self.logger.log_write(repr(arg),'output')
521 self.logger.log_write(repr(arg),'output')
522 cout_write(self.output_sep2)
522 cout_write(self.output_sep2)
523 Term.cout.flush()
523 Term.cout.flush()
524
524
525 def _display(self,arg):
525 def _display(self,arg):
526 """Default printer method, uses pprint.
526 """Default printer method, uses pprint.
527
527
528 This can be over-ridden by the users to implement special formatting
528 This can be over-ridden by the users to implement special formatting
529 of certain types of output."""
529 of certain types of output."""
530
530
531 if self.Pprint:
531 if self.Pprint:
532 out = pformat(arg)
532 out = pformat(arg)
533 if '\n' in out:
533 if '\n' in out:
534 # So that multi-line strings line up with the left column of
534 # So that multi-line strings line up with the left column of
535 # the screen, instead of having the output prompt mess up
535 # the screen, instead of having the output prompt mess up
536 # their first line.
536 # their first line.
537 Term.cout.write('\n')
537 Term.cout.write('\n')
538 print >>Term.cout, out
538 print >>Term.cout, out
539 else:
539 else:
540 print >>Term.cout, arg
540 print >>Term.cout, arg
541
541
542 # Assign the default display method:
542 # Assign the default display method:
543 display = _display
543 display = _display
544
544
545 def update(self,arg):
545 def update(self,arg):
546 #print '***cache_count', self.cache_count # dbg
546 #print '***cache_count', self.cache_count # dbg
547 if self.cache_count >= self.cache_size and self.do_full_cache:
547 if self.cache_count >= self.cache_size and self.do_full_cache:
548 self.flush()
548 self.flush()
549 # Don't overwrite '_' and friends if '_' is in __builtin__ (otherwise
549 # Don't overwrite '_' and friends if '_' is in __builtin__ (otherwise
550 # we cause buggy behavior for things like gettext).
550 # we cause buggy behavior for things like gettext).
551 if '_' not in __builtin__.__dict__:
551 if '_' not in __builtin__.__dict__:
552 self.___ = self.__
552 self.___ = self.__
553 self.__ = self._
553 self.__ = self._
554 self._ = arg
554 self._ = arg
555 self.user_ns.update({'_':self._,'__':self.__,'___':self.___})
555 self.user_ns.update({'_':self._,'__':self.__,'___':self.___})
556
556
557 # hackish access to top-level namespace to create _1,_2... dynamically
557 # hackish access to top-level namespace to create _1,_2... dynamically
558 to_main = {}
558 to_main = {}
559 if self.do_full_cache:
559 if self.do_full_cache:
560 self.cache_count += 1
560 self.cache_count += 1
561 self.entries.append(arg)
561 self.entries.append(arg)
562 new_result = '_'+`self.prompt_count`
562 new_result = '_'+`self.prompt_count`
563 to_main[new_result] = self.entries[-1]
563 to_main[new_result] = self.entries[-1]
564 self.user_ns.update(to_main)
564 self.user_ns.update(to_main)
565 self.user_ns['_oh'][self.prompt_count] = arg
565 self.user_ns['_oh'][self.prompt_count] = arg
566
566
567 def flush(self):
567 def flush(self):
568 if not self.do_full_cache:
568 if not self.do_full_cache:
569 raise ValueError,"You shouldn't have reached the cache flush "\
569 raise ValueError,"You shouldn't have reached the cache flush "\
570 "if full caching is not enabled!"
570 "if full caching is not enabled!"
571 warn('Output cache limit (currently '+\
571 warn('Output cache limit (currently '+\
572 `self.cache_count`+' entries) hit.\n'
572 `self.cache_count`+' entries) hit.\n'
573 'Flushing cache and resetting history counter...\n'
573 'Flushing cache and resetting history counter...\n'
574 'The only history variables available will be _,__,___ and _1\n'
574 'The only history variables available will be _,__,___ and _1\n'
575 'with the current result.')
575 'with the current result.')
576 # delete auto-generated vars from global namespace
576 # delete auto-generated vars from global namespace
577 for n in range(1,self.prompt_count + 1):
577 for n in range(1,self.prompt_count + 1):
578 key = '_'+`n`
578 key = '_'+`n`
579 try:
579 try:
580 del self.user_ns[key]
580 del self.user_ns[key]
581 except: pass
581 except: pass
582 self.prompt_count = 1
582 self.prompt_count = 1
583 self.cache_count = 1
583 self.cache_count = 1
@@ -1,76 +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 992 2006-01-04 18:35:40Z fperez $"""
4 $Id: Release.py 994 2006-01-08 08:29:44Z fperez $"""
5
5
6 #*****************************************************************************
6 #*****************************************************************************
7 # Copyright (C) 2001-2005 Fernando Perez <fperez@colorado.edu>
7 # Copyright (C) 2001-2006 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.7.0.rc8'
25 version = '0.7.0.rc8'
26
26
27 revision = '$Revision: 992 $'
27 revision = '$Revision: 994 $'
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'
72 download_url = 'http://ipython.scipy.org/dist'
73
73
74 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']
75
75
76 keywords = ['Interactive','Interpreter','Shell']
76 keywords = ['Interactive','Interpreter','Shell']
@@ -1,917 +1,917 b''
1 # -*- coding: utf-8 -*-
1 # -*- coding: utf-8 -*-
2 """IPython Shell classes.
2 """IPython Shell classes.
3
3
4 All the matplotlib support code was co-developed with John Hunter,
4 All the matplotlib support code was co-developed with John Hunter,
5 matplotlib's author.
5 matplotlib's author.
6
6
7 $Id: Shell.py 993 2006-01-04 19:51:01Z fperez $"""
7 $Id: Shell.py 994 2006-01-08 08:29:44Z fperez $"""
8
8
9 #*****************************************************************************
9 #*****************************************************************************
10 # Copyright (C) 2001-2004 Fernando Perez <fperez@colorado.edu>
10 # Copyright (C) 2001-2006 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 from IPython import Release
16 from IPython import Release
17 __author__ = '%s <%s>' % Release.authors['Fernando']
17 __author__ = '%s <%s>' % Release.authors['Fernando']
18 __license__ = Release.license
18 __license__ = Release.license
19
19
20 # Code begins
20 # Code begins
21 import __main__
21 import __main__
22 import __builtin__
22 import __builtin__
23 import os
23 import os
24 import sys
24 import sys
25 import signal
25 import signal
26 import threading
26 import threading
27
27
28 import IPython
28 import IPython
29 from IPython import ultraTB
29 from IPython import ultraTB
30 from IPython.genutils import Term,warn,error,flag_calls
30 from IPython.genutils import Term,warn,error,flag_calls
31 from IPython.iplib import InteractiveShell
31 from IPython.iplib import InteractiveShell
32 from IPython.ipmaker import make_IPython
32 from IPython.ipmaker import make_IPython
33 from IPython.Magic import Magic
33 from IPython.Magic import Magic
34 from IPython.Struct import Struct
34 from IPython.Struct import Struct
35
35
36 # global flag to pass around information about Ctrl-C without exceptions
36 # global flag to pass around information about Ctrl-C without exceptions
37 KBINT = False
37 KBINT = False
38
38
39 # global flag to turn on/off Tk support.
39 # global flag to turn on/off Tk support.
40 USE_TK = False
40 USE_TK = False
41
41
42 #-----------------------------------------------------------------------------
42 #-----------------------------------------------------------------------------
43 # This class is trivial now, but I want to have it in to publish a clean
43 # This class is trivial now, but I want to have it in to publish a clean
44 # interface. Later when the internals are reorganized, code that uses this
44 # interface. Later when the internals are reorganized, code that uses this
45 # shouldn't have to change.
45 # shouldn't have to change.
46
46
47 class IPShell:
47 class IPShell:
48 """Create an IPython instance."""
48 """Create an IPython instance."""
49
49
50 def __init__(self,argv=None,user_ns=None,user_global_ns=None,
50 def __init__(self,argv=None,user_ns=None,user_global_ns=None,
51 debug=1,shell_class=InteractiveShell):
51 debug=1,shell_class=InteractiveShell):
52 self.IP = make_IPython(argv,user_ns=user_ns,user_global_ns=user_global_ns,
52 self.IP = make_IPython(argv,user_ns=user_ns,user_global_ns=user_global_ns,
53 debug=debug,shell_class=shell_class)
53 debug=debug,shell_class=shell_class)
54
54
55 def mainloop(self,sys_exit=0,banner=None):
55 def mainloop(self,sys_exit=0,banner=None):
56 self.IP.mainloop(banner)
56 self.IP.mainloop(banner)
57 if sys_exit:
57 if sys_exit:
58 sys.exit()
58 sys.exit()
59
59
60 #-----------------------------------------------------------------------------
60 #-----------------------------------------------------------------------------
61 class IPShellEmbed:
61 class IPShellEmbed:
62 """Allow embedding an IPython shell into a running program.
62 """Allow embedding an IPython shell into a running program.
63
63
64 Instances of this class are callable, with the __call__ method being an
64 Instances of this class are callable, with the __call__ method being an
65 alias to the embed() method of an InteractiveShell instance.
65 alias to the embed() method of an InteractiveShell instance.
66
66
67 Usage (see also the example-embed.py file for a running example):
67 Usage (see also the example-embed.py file for a running example):
68
68
69 ipshell = IPShellEmbed([argv,banner,exit_msg,rc_override])
69 ipshell = IPShellEmbed([argv,banner,exit_msg,rc_override])
70
70
71 - argv: list containing valid command-line options for IPython, as they
71 - argv: list containing valid command-line options for IPython, as they
72 would appear in sys.argv[1:].
72 would appear in sys.argv[1:].
73
73
74 For example, the following command-line options:
74 For example, the following command-line options:
75
75
76 $ ipython -prompt_in1 'Input <\\#>' -colors LightBG
76 $ ipython -prompt_in1 'Input <\\#>' -colors LightBG
77
77
78 would be passed in the argv list as:
78 would be passed in the argv list as:
79
79
80 ['-prompt_in1','Input <\\#>','-colors','LightBG']
80 ['-prompt_in1','Input <\\#>','-colors','LightBG']
81
81
82 - banner: string which gets printed every time the interpreter starts.
82 - banner: string which gets printed every time the interpreter starts.
83
83
84 - exit_msg: string which gets printed every time the interpreter exits.
84 - exit_msg: string which gets printed every time the interpreter exits.
85
85
86 - rc_override: a dict or Struct of configuration options such as those
86 - rc_override: a dict or Struct of configuration options such as those
87 used by IPython. These options are read from your ~/.ipython/ipythonrc
87 used by IPython. These options are read from your ~/.ipython/ipythonrc
88 file when the Shell object is created. Passing an explicit rc_override
88 file when the Shell object is created. Passing an explicit rc_override
89 dict with any options you want allows you to override those values at
89 dict with any options you want allows you to override those values at
90 creation time without having to modify the file. This way you can create
90 creation time without having to modify the file. This way you can create
91 embeddable instances configured in any way you want without editing any
91 embeddable instances configured in any way you want without editing any
92 global files (thus keeping your interactive IPython configuration
92 global files (thus keeping your interactive IPython configuration
93 unchanged).
93 unchanged).
94
94
95 Then the ipshell instance can be called anywhere inside your code:
95 Then the ipshell instance can be called anywhere inside your code:
96
96
97 ipshell(header='') -> Opens up an IPython shell.
97 ipshell(header='') -> Opens up an IPython shell.
98
98
99 - header: string printed by the IPython shell upon startup. This can let
99 - header: string printed by the IPython shell upon startup. This can let
100 you know where in your code you are when dropping into the shell. Note
100 you know where in your code you are when dropping into the shell. Note
101 that 'banner' gets prepended to all calls, so header is used for
101 that 'banner' gets prepended to all calls, so header is used for
102 location-specific information.
102 location-specific information.
103
103
104 For more details, see the __call__ method below.
104 For more details, see the __call__ method below.
105
105
106 When the IPython shell is exited with Ctrl-D, normal program execution
106 When the IPython shell is exited with Ctrl-D, normal program execution
107 resumes.
107 resumes.
108
108
109 This functionality was inspired by a posting on comp.lang.python by cmkl
109 This functionality was inspired by a posting on comp.lang.python by cmkl
110 <cmkleffner@gmx.de> on Dec. 06/01 concerning similar uses of pyrepl, and
110 <cmkleffner@gmx.de> on Dec. 06/01 concerning similar uses of pyrepl, and
111 by the IDL stop/continue commands."""
111 by the IDL stop/continue commands."""
112
112
113 def __init__(self,argv=None,banner='',exit_msg=None,rc_override=None):
113 def __init__(self,argv=None,banner='',exit_msg=None,rc_override=None):
114 """Note that argv here is a string, NOT a list."""
114 """Note that argv here is a string, NOT a list."""
115 self.set_banner(banner)
115 self.set_banner(banner)
116 self.set_exit_msg(exit_msg)
116 self.set_exit_msg(exit_msg)
117 self.set_dummy_mode(0)
117 self.set_dummy_mode(0)
118
118
119 # sys.displayhook is a global, we need to save the user's original
119 # sys.displayhook is a global, we need to save the user's original
120 # Don't rely on __displayhook__, as the user may have changed that.
120 # Don't rely on __displayhook__, as the user may have changed that.
121 self.sys_displayhook_ori = sys.displayhook
121 self.sys_displayhook_ori = sys.displayhook
122
122
123 # save readline completer status
123 # save readline completer status
124 try:
124 try:
125 #print 'Save completer',sys.ipcompleter # dbg
125 #print 'Save completer',sys.ipcompleter # dbg
126 self.sys_ipcompleter_ori = sys.ipcompleter
126 self.sys_ipcompleter_ori = sys.ipcompleter
127 except:
127 except:
128 pass # not nested with IPython
128 pass # not nested with IPython
129
129
130 # FIXME. Passing user_ns breaks namespace handling.
130 # FIXME. Passing user_ns breaks namespace handling.
131 #self.IP = make_IPython(argv,user_ns=__main__.__dict__)
131 #self.IP = make_IPython(argv,user_ns=__main__.__dict__)
132 self.IP = make_IPython(argv,rc_override=rc_override,embedded=True)
132 self.IP = make_IPython(argv,rc_override=rc_override,embedded=True)
133
133
134 # copy our own displayhook also
134 # copy our own displayhook also
135 self.sys_displayhook_embed = sys.displayhook
135 self.sys_displayhook_embed = sys.displayhook
136 # and leave the system's display hook clean
136 # and leave the system's display hook clean
137 sys.displayhook = self.sys_displayhook_ori
137 sys.displayhook = self.sys_displayhook_ori
138 # don't use the ipython crash handler so that user exceptions aren't
138 # don't use the ipython crash handler so that user exceptions aren't
139 # trapped
139 # trapped
140 sys.excepthook = ultraTB.FormattedTB(color_scheme = self.IP.rc.colors,
140 sys.excepthook = ultraTB.FormattedTB(color_scheme = self.IP.rc.colors,
141 mode = self.IP.rc.xmode,
141 mode = self.IP.rc.xmode,
142 call_pdb = self.IP.rc.pdb)
142 call_pdb = self.IP.rc.pdb)
143 self.restore_system_completer()
143 self.restore_system_completer()
144
144
145 def restore_system_completer(self):
145 def restore_system_completer(self):
146 """Restores the readline completer which was in place.
146 """Restores the readline completer which was in place.
147
147
148 This allows embedded IPython within IPython not to disrupt the
148 This allows embedded IPython within IPython not to disrupt the
149 parent's completion.
149 parent's completion.
150 """
150 """
151
151
152 try:
152 try:
153 self.IP.readline.set_completer(self.sys_ipcompleter_ori)
153 self.IP.readline.set_completer(self.sys_ipcompleter_ori)
154 sys.ipcompleter = self.sys_ipcompleter_ori
154 sys.ipcompleter = self.sys_ipcompleter_ori
155 except:
155 except:
156 pass
156 pass
157
157
158 def __call__(self,header='',local_ns=None,global_ns=None,dummy=None):
158 def __call__(self,header='',local_ns=None,global_ns=None,dummy=None):
159 """Activate the interactive interpreter.
159 """Activate the interactive interpreter.
160
160
161 __call__(self,header='',local_ns=None,global_ns,dummy=None) -> Start
161 __call__(self,header='',local_ns=None,global_ns,dummy=None) -> Start
162 the interpreter shell with the given local and global namespaces, and
162 the interpreter shell with the given local and global namespaces, and
163 optionally print a header string at startup.
163 optionally print a header string at startup.
164
164
165 The shell can be globally activated/deactivated using the
165 The shell can be globally activated/deactivated using the
166 set/get_dummy_mode methods. This allows you to turn off a shell used
166 set/get_dummy_mode methods. This allows you to turn off a shell used
167 for debugging globally.
167 for debugging globally.
168
168
169 However, *each* time you call the shell you can override the current
169 However, *each* time you call the shell you can override the current
170 state of dummy_mode with the optional keyword parameter 'dummy'. For
170 state of dummy_mode with the optional keyword parameter 'dummy'. For
171 example, if you set dummy mode on with IPShell.set_dummy_mode(1), you
171 example, if you set dummy mode on with IPShell.set_dummy_mode(1), you
172 can still have a specific call work by making it as IPShell(dummy=0).
172 can still have a specific call work by making it as IPShell(dummy=0).
173
173
174 The optional keyword parameter dummy controls whether the call
174 The optional keyword parameter dummy controls whether the call
175 actually does anything. """
175 actually does anything. """
176
176
177 # Allow the dummy parameter to override the global __dummy_mode
177 # Allow the dummy parameter to override the global __dummy_mode
178 if dummy or (dummy != 0 and self.__dummy_mode):
178 if dummy or (dummy != 0 and self.__dummy_mode):
179 return
179 return
180
180
181 # Set global subsystems (display,completions) to our values
181 # Set global subsystems (display,completions) to our values
182 sys.displayhook = self.sys_displayhook_embed
182 sys.displayhook = self.sys_displayhook_embed
183 if self.IP.has_readline:
183 if self.IP.has_readline:
184 self.IP.readline.set_completer(self.IP.Completer.complete)
184 self.IP.readline.set_completer(self.IP.Completer.complete)
185
185
186 if self.banner and header:
186 if self.banner and header:
187 format = '%s\n%s\n'
187 format = '%s\n%s\n'
188 else:
188 else:
189 format = '%s%s\n'
189 format = '%s%s\n'
190 banner = format % (self.banner,header)
190 banner = format % (self.banner,header)
191
191
192 # Call the embedding code with a stack depth of 1 so it can skip over
192 # Call the embedding code with a stack depth of 1 so it can skip over
193 # our call and get the original caller's namespaces.
193 # our call and get the original caller's namespaces.
194 self.IP.embed_mainloop(banner,local_ns,global_ns,stack_depth=1)
194 self.IP.embed_mainloop(banner,local_ns,global_ns,stack_depth=1)
195
195
196 if self.exit_msg:
196 if self.exit_msg:
197 print self.exit_msg
197 print self.exit_msg
198
198
199 # Restore global systems (display, completion)
199 # Restore global systems (display, completion)
200 sys.displayhook = self.sys_displayhook_ori
200 sys.displayhook = self.sys_displayhook_ori
201 self.restore_system_completer()
201 self.restore_system_completer()
202
202
203 def set_dummy_mode(self,dummy):
203 def set_dummy_mode(self,dummy):
204 """Sets the embeddable shell's dummy mode parameter.
204 """Sets the embeddable shell's dummy mode parameter.
205
205
206 set_dummy_mode(dummy): dummy = 0 or 1.
206 set_dummy_mode(dummy): dummy = 0 or 1.
207
207
208 This parameter is persistent and makes calls to the embeddable shell
208 This parameter is persistent and makes calls to the embeddable shell
209 silently return without performing any action. This allows you to
209 silently return without performing any action. This allows you to
210 globally activate or deactivate a shell you're using with a single call.
210 globally activate or deactivate a shell you're using with a single call.
211
211
212 If you need to manually"""
212 If you need to manually"""
213
213
214 if dummy not in [0,1,False,True]:
214 if dummy not in [0,1,False,True]:
215 raise ValueError,'dummy parameter must be boolean'
215 raise ValueError,'dummy parameter must be boolean'
216 self.__dummy_mode = dummy
216 self.__dummy_mode = dummy
217
217
218 def get_dummy_mode(self):
218 def get_dummy_mode(self):
219 """Return the current value of the dummy mode parameter.
219 """Return the current value of the dummy mode parameter.
220 """
220 """
221 return self.__dummy_mode
221 return self.__dummy_mode
222
222
223 def set_banner(self,banner):
223 def set_banner(self,banner):
224 """Sets the global banner.
224 """Sets the global banner.
225
225
226 This banner gets prepended to every header printed when the shell
226 This banner gets prepended to every header printed when the shell
227 instance is called."""
227 instance is called."""
228
228
229 self.banner = banner
229 self.banner = banner
230
230
231 def set_exit_msg(self,exit_msg):
231 def set_exit_msg(self,exit_msg):
232 """Sets the global exit_msg.
232 """Sets the global exit_msg.
233
233
234 This exit message gets printed upon exiting every time the embedded
234 This exit message gets printed upon exiting every time the embedded
235 shell is called. It is None by default. """
235 shell is called. It is None by default. """
236
236
237 self.exit_msg = exit_msg
237 self.exit_msg = exit_msg
238
238
239 #-----------------------------------------------------------------------------
239 #-----------------------------------------------------------------------------
240 def sigint_handler (signum,stack_frame):
240 def sigint_handler (signum,stack_frame):
241 """Sigint handler for threaded apps.
241 """Sigint handler for threaded apps.
242
242
243 This is a horrible hack to pass information about SIGINT _without_ using
243 This is a horrible hack to pass information about SIGINT _without_ using
244 exceptions, since I haven't been able to properly manage cross-thread
244 exceptions, since I haven't been able to properly manage cross-thread
245 exceptions in GTK/WX. In fact, I don't think it can be done (or at least
245 exceptions in GTK/WX. In fact, I don't think it can be done (or at least
246 that's my understanding from a c.l.py thread where this was discussed)."""
246 that's my understanding from a c.l.py thread where this was discussed)."""
247
247
248 global KBINT
248 global KBINT
249
249
250 print '\nKeyboardInterrupt - Press <Enter> to continue.',
250 print '\nKeyboardInterrupt - Press <Enter> to continue.',
251 Term.cout.flush()
251 Term.cout.flush()
252 # Set global flag so that runsource can know that Ctrl-C was hit
252 # Set global flag so that runsource can know that Ctrl-C was hit
253 KBINT = True
253 KBINT = True
254
254
255 class MTInteractiveShell(InteractiveShell):
255 class MTInteractiveShell(InteractiveShell):
256 """Simple multi-threaded shell."""
256 """Simple multi-threaded shell."""
257
257
258 # Threading strategy taken from:
258 # Threading strategy taken from:
259 # http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/65109, by Brian
259 # http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/65109, by Brian
260 # McErlean and John Finlay. Modified with corrections by Antoon Pardon,
260 # McErlean and John Finlay. Modified with corrections by Antoon Pardon,
261 # from the pygtk mailing list, to avoid lockups with system calls.
261 # from the pygtk mailing list, to avoid lockups with system calls.
262
262
263 # class attribute to indicate whether the class supports threads or not.
263 # class attribute to indicate whether the class supports threads or not.
264 # Subclasses with thread support should override this as needed.
264 # Subclasses with thread support should override this as needed.
265 isthreaded = True
265 isthreaded = True
266
266
267 def __init__(self,name,usage=None,rc=Struct(opts=None,args=None),
267 def __init__(self,name,usage=None,rc=Struct(opts=None,args=None),
268 user_ns=None,user_global_ns=None,banner2='',**kw):
268 user_ns=None,user_global_ns=None,banner2='',**kw):
269 """Similar to the normal InteractiveShell, but with threading control"""
269 """Similar to the normal InteractiveShell, but with threading control"""
270
270
271 InteractiveShell.__init__(self,name,usage,rc,user_ns,
271 InteractiveShell.__init__(self,name,usage,rc,user_ns,
272 user_global_ns,banner2)
272 user_global_ns,banner2)
273
273
274 # Locking control variable
274 # Locking control variable
275 self.thread_ready = threading.Condition()
275 self.thread_ready = threading.Condition()
276
276
277 # Stuff to do at closing time
277 # Stuff to do at closing time
278 self._kill = False
278 self._kill = False
279 on_kill = kw.get('on_kill')
279 on_kill = kw.get('on_kill')
280 if on_kill is None:
280 if on_kill is None:
281 on_kill = []
281 on_kill = []
282 # Check that all things to kill are callable:
282 # Check that all things to kill are callable:
283 for t in on_kill:
283 for t in on_kill:
284 if not callable(t):
284 if not callable(t):
285 raise TypeError,'on_kill must be a list of callables'
285 raise TypeError,'on_kill must be a list of callables'
286 self.on_kill = on_kill
286 self.on_kill = on_kill
287
287
288 def runsource(self, source, filename="<input>", symbol="single"):
288 def runsource(self, source, filename="<input>", symbol="single"):
289 """Compile and run some source in the interpreter.
289 """Compile and run some source in the interpreter.
290
290
291 Modified version of code.py's runsource(), to handle threading issues.
291 Modified version of code.py's runsource(), to handle threading issues.
292 See the original for full docstring details."""
292 See the original for full docstring details."""
293
293
294 global KBINT
294 global KBINT
295
295
296 # If Ctrl-C was typed, we reset the flag and return right away
296 # If Ctrl-C was typed, we reset the flag and return right away
297 if KBINT:
297 if KBINT:
298 KBINT = False
298 KBINT = False
299 return False
299 return False
300
300
301 try:
301 try:
302 code = self.compile(source, filename, symbol)
302 code = self.compile(source, filename, symbol)
303 except (OverflowError, SyntaxError, ValueError):
303 except (OverflowError, SyntaxError, ValueError):
304 # Case 1
304 # Case 1
305 self.showsyntaxerror(filename)
305 self.showsyntaxerror(filename)
306 return False
306 return False
307
307
308 if code is None:
308 if code is None:
309 # Case 2
309 # Case 2
310 return True
310 return True
311
311
312 # Case 3
312 # Case 3
313 # Store code in self, so the execution thread can handle it
313 # Store code in self, so the execution thread can handle it
314 self.thread_ready.acquire()
314 self.thread_ready.acquire()
315 self.code_to_run = code
315 self.code_to_run = code
316 self.thread_ready.wait() # Wait until processed in timeout interval
316 self.thread_ready.wait() # Wait until processed in timeout interval
317 self.thread_ready.release()
317 self.thread_ready.release()
318
318
319 return False
319 return False
320
320
321 def runcode(self):
321 def runcode(self):
322 """Execute a code object.
322 """Execute a code object.
323
323
324 Multithreaded wrapper around IPython's runcode()."""
324 Multithreaded wrapper around IPython's runcode()."""
325
325
326 # lock thread-protected stuff
326 # lock thread-protected stuff
327 self.thread_ready.acquire()
327 self.thread_ready.acquire()
328
328
329 # Install sigint handler
329 # Install sigint handler
330 try:
330 try:
331 signal.signal(signal.SIGINT, sigint_handler)
331 signal.signal(signal.SIGINT, sigint_handler)
332 except SystemError:
332 except SystemError:
333 # This happens under Windows, which seems to have all sorts
333 # This happens under Windows, which seems to have all sorts
334 # of problems with signal handling. Oh well...
334 # of problems with signal handling. Oh well...
335 pass
335 pass
336
336
337 if self._kill:
337 if self._kill:
338 print >>Term.cout, 'Closing threads...',
338 print >>Term.cout, 'Closing threads...',
339 Term.cout.flush()
339 Term.cout.flush()
340 for tokill in self.on_kill:
340 for tokill in self.on_kill:
341 tokill()
341 tokill()
342 print >>Term.cout, 'Done.'
342 print >>Term.cout, 'Done.'
343
343
344 # Run pending code by calling parent class
344 # Run pending code by calling parent class
345 if self.code_to_run is not None:
345 if self.code_to_run is not None:
346 self.thread_ready.notify()
346 self.thread_ready.notify()
347 InteractiveShell.runcode(self,self.code_to_run)
347 InteractiveShell.runcode(self,self.code_to_run)
348
348
349 # We're done with thread-protected variables
349 # We're done with thread-protected variables
350 self.thread_ready.release()
350 self.thread_ready.release()
351 # This MUST return true for gtk threading to work
351 # This MUST return true for gtk threading to work
352 return True
352 return True
353
353
354 def kill (self):
354 def kill (self):
355 """Kill the thread, returning when it has been shut down."""
355 """Kill the thread, returning when it has been shut down."""
356 self.thread_ready.acquire()
356 self.thread_ready.acquire()
357 self._kill = True
357 self._kill = True
358 self.thread_ready.release()
358 self.thread_ready.release()
359
359
360 class MatplotlibShellBase:
360 class MatplotlibShellBase:
361 """Mixin class to provide the necessary modifications to regular IPython
361 """Mixin class to provide the necessary modifications to regular IPython
362 shell classes for matplotlib support.
362 shell classes for matplotlib support.
363
363
364 Given Python's MRO, this should be used as the FIRST class in the
364 Given Python's MRO, this should be used as the FIRST class in the
365 inheritance hierarchy, so that it overrides the relevant methods."""
365 inheritance hierarchy, so that it overrides the relevant methods."""
366
366
367 def _matplotlib_config(self,name):
367 def _matplotlib_config(self,name):
368 """Return various items needed to setup the user's shell with matplotlib"""
368 """Return various items needed to setup the user's shell with matplotlib"""
369
369
370 # Initialize matplotlib to interactive mode always
370 # Initialize matplotlib to interactive mode always
371 import matplotlib
371 import matplotlib
372 from matplotlib import backends
372 from matplotlib import backends
373 matplotlib.interactive(True)
373 matplotlib.interactive(True)
374
374
375 def use(arg):
375 def use(arg):
376 """IPython wrapper for matplotlib's backend switcher.
376 """IPython wrapper for matplotlib's backend switcher.
377
377
378 In interactive use, we can not allow switching to a different
378 In interactive use, we can not allow switching to a different
379 interactive backend, since thread conflicts will most likely crash
379 interactive backend, since thread conflicts will most likely crash
380 the python interpreter. This routine does a safety check first,
380 the python interpreter. This routine does a safety check first,
381 and refuses to perform a dangerous switch. It still allows
381 and refuses to perform a dangerous switch. It still allows
382 switching to non-interactive backends."""
382 switching to non-interactive backends."""
383
383
384 if arg in backends.interactive_bk and arg != self.mpl_backend:
384 if arg in backends.interactive_bk and arg != self.mpl_backend:
385 m=('invalid matplotlib backend switch.\n'
385 m=('invalid matplotlib backend switch.\n'
386 'This script attempted to switch to the interactive '
386 'This script attempted to switch to the interactive '
387 'backend: `%s`\n'
387 'backend: `%s`\n'
388 'Your current choice of interactive backend is: `%s`\n\n'
388 'Your current choice of interactive backend is: `%s`\n\n'
389 'Switching interactive matplotlib backends at runtime\n'
389 'Switching interactive matplotlib backends at runtime\n'
390 'would crash the python interpreter, '
390 'would crash the python interpreter, '
391 'and IPython has blocked it.\n\n'
391 'and IPython has blocked it.\n\n'
392 'You need to either change your choice of matplotlib backend\n'
392 'You need to either change your choice of matplotlib backend\n'
393 'by editing your .matplotlibrc file, or run this script as a \n'
393 'by editing your .matplotlibrc file, or run this script as a \n'
394 'standalone file from the command line, not using IPython.\n' %
394 'standalone file from the command line, not using IPython.\n' %
395 (arg,self.mpl_backend) )
395 (arg,self.mpl_backend) )
396 raise RuntimeError, m
396 raise RuntimeError, m
397 else:
397 else:
398 self.mpl_use(arg)
398 self.mpl_use(arg)
399 self.mpl_use._called = True
399 self.mpl_use._called = True
400
400
401 self.matplotlib = matplotlib
401 self.matplotlib = matplotlib
402 self.mpl_backend = matplotlib.rcParams['backend']
402 self.mpl_backend = matplotlib.rcParams['backend']
403
403
404 # we also need to block switching of interactive backends by use()
404 # we also need to block switching of interactive backends by use()
405 self.mpl_use = matplotlib.use
405 self.mpl_use = matplotlib.use
406 self.mpl_use._called = False
406 self.mpl_use._called = False
407 # overwrite the original matplotlib.use with our wrapper
407 # overwrite the original matplotlib.use with our wrapper
408 matplotlib.use = use
408 matplotlib.use = use
409
409
410
410
411 # This must be imported last in the matplotlib series, after
411 # This must be imported last in the matplotlib series, after
412 # backend/interactivity choices have been made
412 # backend/interactivity choices have been made
413 try:
413 try:
414 import matplotlib.pylab as pylab
414 import matplotlib.pylab as pylab
415 self.pylab = pylab
415 self.pylab = pylab
416 self.pylab_name = 'pylab'
416 self.pylab_name = 'pylab'
417 except ImportError:
417 except ImportError:
418 import matplotlib.matlab as matlab
418 import matplotlib.matlab as matlab
419 self.pylab = matlab
419 self.pylab = matlab
420 self.pylab_name = 'matlab'
420 self.pylab_name = 'matlab'
421
421
422 self.pylab.show._needmain = False
422 self.pylab.show._needmain = False
423 # We need to detect at runtime whether show() is called by the user.
423 # We need to detect at runtime whether show() is called by the user.
424 # For this, we wrap it into a decorator which adds a 'called' flag.
424 # For this, we wrap it into a decorator which adds a 'called' flag.
425 self.pylab.draw_if_interactive = flag_calls(self.pylab.draw_if_interactive)
425 self.pylab.draw_if_interactive = flag_calls(self.pylab.draw_if_interactive)
426
426
427 # Build a user namespace initialized with matplotlib/matlab features.
427 # Build a user namespace initialized with matplotlib/matlab features.
428 user_ns = {'__name__':'__main__',
428 user_ns = {'__name__':'__main__',
429 '__builtins__' : __builtin__ }
429 '__builtins__' : __builtin__ }
430
430
431 # Be careful not to remove the final \n in the code string below, or
431 # Be careful not to remove the final \n in the code string below, or
432 # things will break badly with py22 (I think it's a python bug, 2.3 is
432 # things will break badly with py22 (I think it's a python bug, 2.3 is
433 # OK).
433 # OK).
434 pname = self.pylab_name # Python can't interpolate dotted var names
434 pname = self.pylab_name # Python can't interpolate dotted var names
435 exec ("import matplotlib\n"
435 exec ("import matplotlib\n"
436 "import matplotlib.%(pname)s as %(pname)s\n"
436 "import matplotlib.%(pname)s as %(pname)s\n"
437 "from matplotlib.%(pname)s import *\n" % locals()) in user_ns
437 "from matplotlib.%(pname)s import *\n" % locals()) in user_ns
438
438
439 # Build matplotlib info banner
439 # Build matplotlib info banner
440 b="""
440 b="""
441 Welcome to pylab, a matplotlib-based Python environment.
441 Welcome to pylab, a matplotlib-based Python environment.
442 For more information, type 'help(pylab)'.
442 For more information, type 'help(pylab)'.
443 """
443 """
444 return user_ns,b
444 return user_ns,b
445
445
446 def mplot_exec(self,fname,*where,**kw):
446 def mplot_exec(self,fname,*where,**kw):
447 """Execute a matplotlib script.
447 """Execute a matplotlib script.
448
448
449 This is a call to execfile(), but wrapped in safeties to properly
449 This is a call to execfile(), but wrapped in safeties to properly
450 handle interactive rendering and backend switching."""
450 handle interactive rendering and backend switching."""
451
451
452 #print '*** Matplotlib runner ***' # dbg
452 #print '*** Matplotlib runner ***' # dbg
453 # turn off rendering until end of script
453 # turn off rendering until end of script
454 isInteractive = self.matplotlib.rcParams['interactive']
454 isInteractive = self.matplotlib.rcParams['interactive']
455 self.matplotlib.interactive(False)
455 self.matplotlib.interactive(False)
456 self.safe_execfile(fname,*where,**kw)
456 self.safe_execfile(fname,*where,**kw)
457 self.matplotlib.interactive(isInteractive)
457 self.matplotlib.interactive(isInteractive)
458 # make rendering call now, if the user tried to do it
458 # make rendering call now, if the user tried to do it
459 if self.pylab.draw_if_interactive.called:
459 if self.pylab.draw_if_interactive.called:
460 self.pylab.draw()
460 self.pylab.draw()
461 self.pylab.draw_if_interactive.called = False
461 self.pylab.draw_if_interactive.called = False
462
462
463 # if a backend switch was performed, reverse it now
463 # if a backend switch was performed, reverse it now
464 if self.mpl_use._called:
464 if self.mpl_use._called:
465 self.matplotlib.rcParams['backend'] = self.mpl_backend
465 self.matplotlib.rcParams['backend'] = self.mpl_backend
466
466
467 def magic_run(self,parameter_s=''):
467 def magic_run(self,parameter_s=''):
468 Magic.magic_run(self,parameter_s,runner=self.mplot_exec)
468 Magic.magic_run(self,parameter_s,runner=self.mplot_exec)
469
469
470 # Fix the docstring so users see the original as well
470 # Fix the docstring so users see the original as well
471 magic_run.__doc__ = "%s\n%s" % (Magic.magic_run.__doc__,
471 magic_run.__doc__ = "%s\n%s" % (Magic.magic_run.__doc__,
472 "\n *** Modified %run for Matplotlib,"
472 "\n *** Modified %run for Matplotlib,"
473 " with proper interactive handling ***")
473 " with proper interactive handling ***")
474
474
475 # Now we provide 2 versions of a matplotlib-aware IPython base shells, single
475 # Now we provide 2 versions of a matplotlib-aware IPython base shells, single
476 # and multithreaded. Note that these are meant for internal use, the IPShell*
476 # and multithreaded. Note that these are meant for internal use, the IPShell*
477 # classes below are the ones meant for public consumption.
477 # classes below are the ones meant for public consumption.
478
478
479 class MatplotlibShell(MatplotlibShellBase,InteractiveShell):
479 class MatplotlibShell(MatplotlibShellBase,InteractiveShell):
480 """Single-threaded shell with matplotlib support."""
480 """Single-threaded shell with matplotlib support."""
481
481
482 def __init__(self,name,usage=None,rc=Struct(opts=None,args=None),
482 def __init__(self,name,usage=None,rc=Struct(opts=None,args=None),
483 user_ns=None,user_global_ns=None,**kw):
483 user_ns=None,user_global_ns=None,**kw):
484 user_ns,b2 = self._matplotlib_config(name)
484 user_ns,b2 = self._matplotlib_config(name)
485 InteractiveShell.__init__(self,name,usage,rc,user_ns,user_global_ns,
485 InteractiveShell.__init__(self,name,usage,rc,user_ns,user_global_ns,
486 banner2=b2,**kw)
486 banner2=b2,**kw)
487
487
488 class MatplotlibMTShell(MatplotlibShellBase,MTInteractiveShell):
488 class MatplotlibMTShell(MatplotlibShellBase,MTInteractiveShell):
489 """Multi-threaded shell with matplotlib support."""
489 """Multi-threaded shell with matplotlib support."""
490
490
491 def __init__(self,name,usage=None,rc=Struct(opts=None,args=None),
491 def __init__(self,name,usage=None,rc=Struct(opts=None,args=None),
492 user_ns=None,user_global_ns=None, **kw):
492 user_ns=None,user_global_ns=None, **kw):
493 user_ns,b2 = self._matplotlib_config(name)
493 user_ns,b2 = self._matplotlib_config(name)
494 MTInteractiveShell.__init__(self,name,usage,rc,user_ns,user_global_ns,
494 MTInteractiveShell.__init__(self,name,usage,rc,user_ns,user_global_ns,
495 banner2=b2,**kw)
495 banner2=b2,**kw)
496
496
497 #-----------------------------------------------------------------------------
497 #-----------------------------------------------------------------------------
498 # Utility functions for the different GUI enabled IPShell* classes.
498 # Utility functions for the different GUI enabled IPShell* classes.
499
499
500 def get_tk():
500 def get_tk():
501 """Tries to import Tkinter and returns a withdrawn Tkinter root
501 """Tries to import Tkinter and returns a withdrawn Tkinter root
502 window. If Tkinter is already imported or not available, this
502 window. If Tkinter is already imported or not available, this
503 returns None. This function calls `hijack_tk` underneath.
503 returns None. This function calls `hijack_tk` underneath.
504 """
504 """
505 if not USE_TK or sys.modules.has_key('Tkinter'):
505 if not USE_TK or sys.modules.has_key('Tkinter'):
506 return None
506 return None
507 else:
507 else:
508 try:
508 try:
509 import Tkinter
509 import Tkinter
510 except ImportError:
510 except ImportError:
511 return None
511 return None
512 else:
512 else:
513 hijack_tk()
513 hijack_tk()
514 r = Tkinter.Tk()
514 r = Tkinter.Tk()
515 r.withdraw()
515 r.withdraw()
516 return r
516 return r
517
517
518 def hijack_tk():
518 def hijack_tk():
519 """Modifies Tkinter's mainloop with a dummy so when a module calls
519 """Modifies Tkinter's mainloop with a dummy so when a module calls
520 mainloop, it does not block.
520 mainloop, it does not block.
521
521
522 """
522 """
523 def misc_mainloop(self, n=0):
523 def misc_mainloop(self, n=0):
524 pass
524 pass
525 def tkinter_mainloop(n=0):
525 def tkinter_mainloop(n=0):
526 pass
526 pass
527
527
528 import Tkinter
528 import Tkinter
529 Tkinter.Misc.mainloop = misc_mainloop
529 Tkinter.Misc.mainloop = misc_mainloop
530 Tkinter.mainloop = tkinter_mainloop
530 Tkinter.mainloop = tkinter_mainloop
531
531
532 def update_tk(tk):
532 def update_tk(tk):
533 """Updates the Tkinter event loop. This is typically called from
533 """Updates the Tkinter event loop. This is typically called from
534 the respective WX or GTK mainloops.
534 the respective WX or GTK mainloops.
535 """
535 """
536 if tk:
536 if tk:
537 tk.update()
537 tk.update()
538
538
539 def hijack_wx():
539 def hijack_wx():
540 """Modifies wxPython's MainLoop with a dummy so user code does not
540 """Modifies wxPython's MainLoop with a dummy so user code does not
541 block IPython. The hijacked mainloop function is returned.
541 block IPython. The hijacked mainloop function is returned.
542 """
542 """
543 def dummy_mainloop(*args, **kw):
543 def dummy_mainloop(*args, **kw):
544 pass
544 pass
545 import wxPython
545 import wxPython
546 ver = wxPython.__version__
546 ver = wxPython.__version__
547 orig_mainloop = None
547 orig_mainloop = None
548 if ver[:3] >= '2.5':
548 if ver[:3] >= '2.5':
549 import wx
549 import wx
550 if hasattr(wx, '_core_'): core = getattr(wx, '_core_')
550 if hasattr(wx, '_core_'): core = getattr(wx, '_core_')
551 elif hasattr(wx, '_core'): core = getattr(wx, '_core')
551 elif hasattr(wx, '_core'): core = getattr(wx, '_core')
552 else: raise AttributeError('Could not find wx core module')
552 else: raise AttributeError('Could not find wx core module')
553 orig_mainloop = core.PyApp_MainLoop
553 orig_mainloop = core.PyApp_MainLoop
554 core.PyApp_MainLoop = dummy_mainloop
554 core.PyApp_MainLoop = dummy_mainloop
555 elif ver[:3] == '2.4':
555 elif ver[:3] == '2.4':
556 orig_mainloop = wxPython.wxc.wxPyApp_MainLoop
556 orig_mainloop = wxPython.wxc.wxPyApp_MainLoop
557 wxPython.wxc.wxPyApp_MainLoop = dummy_mainloop
557 wxPython.wxc.wxPyApp_MainLoop = dummy_mainloop
558 else:
558 else:
559 warn("Unable to find either wxPython version 2.4 or >= 2.5.")
559 warn("Unable to find either wxPython version 2.4 or >= 2.5.")
560 return orig_mainloop
560 return orig_mainloop
561
561
562 def hijack_gtk():
562 def hijack_gtk():
563 """Modifies pyGTK's mainloop with a dummy so user code does not
563 """Modifies pyGTK's mainloop with a dummy so user code does not
564 block IPython. This function returns the original `gtk.mainloop`
564 block IPython. This function returns the original `gtk.mainloop`
565 function that has been hijacked.
565 function that has been hijacked.
566 """
566 """
567 def dummy_mainloop(*args, **kw):
567 def dummy_mainloop(*args, **kw):
568 pass
568 pass
569 import gtk
569 import gtk
570 if gtk.pygtk_version >= (2,4,0): orig_mainloop = gtk.main
570 if gtk.pygtk_version >= (2,4,0): orig_mainloop = gtk.main
571 else: orig_mainloop = gtk.mainloop
571 else: orig_mainloop = gtk.mainloop
572 gtk.mainloop = dummy_mainloop
572 gtk.mainloop = dummy_mainloop
573 gtk.main = dummy_mainloop
573 gtk.main = dummy_mainloop
574 return orig_mainloop
574 return orig_mainloop
575
575
576 #-----------------------------------------------------------------------------
576 #-----------------------------------------------------------------------------
577 # The IPShell* classes below are the ones meant to be run by external code as
577 # The IPShell* classes below are the ones meant to be run by external code as
578 # IPython instances. Note that unless a specific threading strategy is
578 # IPython instances. Note that unless a specific threading strategy is
579 # desired, the factory function start() below should be used instead (it
579 # desired, the factory function start() below should be used instead (it
580 # selects the proper threaded class).
580 # selects the proper threaded class).
581
581
582 class IPShellGTK(threading.Thread):
582 class IPShellGTK(threading.Thread):
583 """Run a gtk mainloop() in a separate thread.
583 """Run a gtk mainloop() in a separate thread.
584
584
585 Python commands can be passed to the thread where they will be executed.
585 Python commands can be passed to the thread where they will be executed.
586 This is implemented by periodically checking for passed code using a
586 This is implemented by periodically checking for passed code using a
587 GTK timeout callback."""
587 GTK timeout callback."""
588
588
589 TIMEOUT = 100 # Millisecond interval between timeouts.
589 TIMEOUT = 100 # Millisecond interval between timeouts.
590
590
591 def __init__(self,argv=None,user_ns=None,user_global_ns=None,
591 def __init__(self,argv=None,user_ns=None,user_global_ns=None,
592 debug=1,shell_class=MTInteractiveShell):
592 debug=1,shell_class=MTInteractiveShell):
593
593
594 import gtk
594 import gtk
595
595
596 self.gtk = gtk
596 self.gtk = gtk
597 self.gtk_mainloop = hijack_gtk()
597 self.gtk_mainloop = hijack_gtk()
598
598
599 # Allows us to use both Tk and GTK.
599 # Allows us to use both Tk and GTK.
600 self.tk = get_tk()
600 self.tk = get_tk()
601
601
602 if gtk.pygtk_version >= (2,4,0): mainquit = self.gtk.main_quit
602 if gtk.pygtk_version >= (2,4,0): mainquit = self.gtk.main_quit
603 else: mainquit = self.gtk.mainquit
603 else: mainquit = self.gtk.mainquit
604
604
605 self.IP = make_IPython(argv,user_ns=user_ns,
605 self.IP = make_IPython(argv,user_ns=user_ns,
606 user_global_ns=user_global_ns,
606 user_global_ns=user_global_ns,
607 debug=debug,
607 debug=debug,
608 shell_class=shell_class,
608 shell_class=shell_class,
609 on_kill=[mainquit])
609 on_kill=[mainquit])
610
610
611 # HACK: slot for banner in self; it will be passed to the mainloop
611 # HACK: slot for banner in self; it will be passed to the mainloop
612 # method only and .run() needs it. The actual value will be set by
612 # method only and .run() needs it. The actual value will be set by
613 # .mainloop().
613 # .mainloop().
614 self._banner = None
614 self._banner = None
615
615
616 threading.Thread.__init__(self)
616 threading.Thread.__init__(self)
617
617
618 def run(self):
618 def run(self):
619 self.IP.mainloop(self._banner)
619 self.IP.mainloop(self._banner)
620 self.IP.kill()
620 self.IP.kill()
621
621
622 def mainloop(self,sys_exit=0,banner=None):
622 def mainloop(self,sys_exit=0,banner=None):
623
623
624 self._banner = banner
624 self._banner = banner
625
625
626 if self.gtk.pygtk_version >= (2,4,0):
626 if self.gtk.pygtk_version >= (2,4,0):
627 import gobject
627 import gobject
628 gobject.timeout_add(self.TIMEOUT, self.on_timer)
628 gobject.timeout_add(self.TIMEOUT, self.on_timer)
629 else:
629 else:
630 self.gtk.timeout_add(self.TIMEOUT, self.on_timer)
630 self.gtk.timeout_add(self.TIMEOUT, self.on_timer)
631
631
632 if sys.platform != 'win32':
632 if sys.platform != 'win32':
633 try:
633 try:
634 if self.gtk.gtk_version[0] >= 2:
634 if self.gtk.gtk_version[0] >= 2:
635 self.gtk.threads_init()
635 self.gtk.threads_init()
636 except AttributeError:
636 except AttributeError:
637 pass
637 pass
638 except RuntimeError:
638 except RuntimeError:
639 error('Your pyGTK likely has not been compiled with '
639 error('Your pyGTK likely has not been compiled with '
640 'threading support.\n'
640 'threading support.\n'
641 'The exception printout is below.\n'
641 'The exception printout is below.\n'
642 'You can either rebuild pyGTK with threads, or '
642 'You can either rebuild pyGTK with threads, or '
643 'try using \n'
643 'try using \n'
644 'matplotlib with a different backend (like Tk or WX).\n'
644 'matplotlib with a different backend (like Tk or WX).\n'
645 'Note that matplotlib will most likely not work in its '
645 'Note that matplotlib will most likely not work in its '
646 'current state!')
646 'current state!')
647 self.IP.InteractiveTB()
647 self.IP.InteractiveTB()
648 self.start()
648 self.start()
649 self.gtk.threads_enter()
649 self.gtk.threads_enter()
650 self.gtk_mainloop()
650 self.gtk_mainloop()
651 self.gtk.threads_leave()
651 self.gtk.threads_leave()
652 self.join()
652 self.join()
653
653
654 def on_timer(self):
654 def on_timer(self):
655 update_tk(self.tk)
655 update_tk(self.tk)
656 return self.IP.runcode()
656 return self.IP.runcode()
657
657
658
658
659 class IPShellWX(threading.Thread):
659 class IPShellWX(threading.Thread):
660 """Run a wx mainloop() in a separate thread.
660 """Run a wx mainloop() in a separate thread.
661
661
662 Python commands can be passed to the thread where they will be executed.
662 Python commands can be passed to the thread where they will be executed.
663 This is implemented by periodically checking for passed code using a
663 This is implemented by periodically checking for passed code using a
664 GTK timeout callback."""
664 GTK timeout callback."""
665
665
666 TIMEOUT = 100 # Millisecond interval between timeouts.
666 TIMEOUT = 100 # Millisecond interval between timeouts.
667
667
668 def __init__(self,argv=None,user_ns=None,user_global_ns=None,
668 def __init__(self,argv=None,user_ns=None,user_global_ns=None,
669 debug=1,shell_class=MTInteractiveShell):
669 debug=1,shell_class=MTInteractiveShell):
670
670
671 import wxPython.wx as wx
671 import wxPython.wx as wx
672
672
673 threading.Thread.__init__(self)
673 threading.Thread.__init__(self)
674 self.wx = wx
674 self.wx = wx
675 self.wx_mainloop = hijack_wx()
675 self.wx_mainloop = hijack_wx()
676
676
677 # Allows us to use both Tk and GTK.
677 # Allows us to use both Tk and GTK.
678 self.tk = get_tk()
678 self.tk = get_tk()
679
679
680 self.IP = make_IPython(argv,user_ns=user_ns,
680 self.IP = make_IPython(argv,user_ns=user_ns,
681 user_global_ns=user_global_ns,
681 user_global_ns=user_global_ns,
682 debug=debug,
682 debug=debug,
683 shell_class=shell_class,
683 shell_class=shell_class,
684 on_kill=[self.wxexit])
684 on_kill=[self.wxexit])
685 # HACK: slot for banner in self; it will be passed to the mainloop
685 # HACK: slot for banner in self; it will be passed to the mainloop
686 # method only and .run() needs it. The actual value will be set by
686 # method only and .run() needs it. The actual value will be set by
687 # .mainloop().
687 # .mainloop().
688 self._banner = None
688 self._banner = None
689
689
690 self.app = None
690 self.app = None
691
691
692 def wxexit(self, *args):
692 def wxexit(self, *args):
693 if self.app is not None:
693 if self.app is not None:
694 self.app.agent.timer.Stop()
694 self.app.agent.timer.Stop()
695 self.app.ExitMainLoop()
695 self.app.ExitMainLoop()
696
696
697 def run(self):
697 def run(self):
698 self.IP.mainloop(self._banner)
698 self.IP.mainloop(self._banner)
699 self.IP.kill()
699 self.IP.kill()
700
700
701 def mainloop(self,sys_exit=0,banner=None):
701 def mainloop(self,sys_exit=0,banner=None):
702
702
703 self._banner = banner
703 self._banner = banner
704
704
705 self.start()
705 self.start()
706
706
707 class TimerAgent(self.wx.wxMiniFrame):
707 class TimerAgent(self.wx.wxMiniFrame):
708 wx = self.wx
708 wx = self.wx
709 IP = self.IP
709 IP = self.IP
710 tk = self.tk
710 tk = self.tk
711 def __init__(self, parent, interval):
711 def __init__(self, parent, interval):
712 style = self.wx.wxDEFAULT_FRAME_STYLE | self.wx.wxTINY_CAPTION_HORIZ
712 style = self.wx.wxDEFAULT_FRAME_STYLE | self.wx.wxTINY_CAPTION_HORIZ
713 self.wx.wxMiniFrame.__init__(self, parent, -1, ' ', pos=(200, 200),
713 self.wx.wxMiniFrame.__init__(self, parent, -1, ' ', pos=(200, 200),
714 size=(100, 100),style=style)
714 size=(100, 100),style=style)
715 self.Show(False)
715 self.Show(False)
716 self.interval = interval
716 self.interval = interval
717 self.timerId = self.wx.wxNewId()
717 self.timerId = self.wx.wxNewId()
718
718
719 def StartWork(self):
719 def StartWork(self):
720 self.timer = self.wx.wxTimer(self, self.timerId)
720 self.timer = self.wx.wxTimer(self, self.timerId)
721 self.wx.EVT_TIMER(self, self.timerId, self.OnTimer)
721 self.wx.EVT_TIMER(self, self.timerId, self.OnTimer)
722 self.timer.Start(self.interval)
722 self.timer.Start(self.interval)
723
723
724 def OnTimer(self, event):
724 def OnTimer(self, event):
725 update_tk(self.tk)
725 update_tk(self.tk)
726 self.IP.runcode()
726 self.IP.runcode()
727
727
728 class App(self.wx.wxApp):
728 class App(self.wx.wxApp):
729 wx = self.wx
729 wx = self.wx
730 TIMEOUT = self.TIMEOUT
730 TIMEOUT = self.TIMEOUT
731 def OnInit(self):
731 def OnInit(self):
732 'Create the main window and insert the custom frame'
732 'Create the main window and insert the custom frame'
733 self.agent = TimerAgent(None, self.TIMEOUT)
733 self.agent = TimerAgent(None, self.TIMEOUT)
734 self.agent.Show(self.wx.false)
734 self.agent.Show(self.wx.false)
735 self.agent.StartWork()
735 self.agent.StartWork()
736 return self.wx.true
736 return self.wx.true
737
737
738 self.app = App(redirect=False)
738 self.app = App(redirect=False)
739 self.wx_mainloop(self.app)
739 self.wx_mainloop(self.app)
740 self.join()
740 self.join()
741
741
742
742
743 class IPShellQt(threading.Thread):
743 class IPShellQt(threading.Thread):
744 """Run a Qt event loop in a separate thread.
744 """Run a Qt event loop in a separate thread.
745
745
746 Python commands can be passed to the thread where they will be executed.
746 Python commands can be passed to the thread where they will be executed.
747 This is implemented by periodically checking for passed code using a
747 This is implemented by periodically checking for passed code using a
748 Qt timer / slot."""
748 Qt timer / slot."""
749
749
750 TIMEOUT = 100 # Millisecond interval between timeouts.
750 TIMEOUT = 100 # Millisecond interval between timeouts.
751
751
752 def __init__(self,argv=None,user_ns=None,user_global_ns=None,
752 def __init__(self,argv=None,user_ns=None,user_global_ns=None,
753 debug=0,shell_class=MTInteractiveShell):
753 debug=0,shell_class=MTInteractiveShell):
754
754
755 import qt
755 import qt
756
756
757 class newQApplication:
757 class newQApplication:
758 def __init__( self ):
758 def __init__( self ):
759 self.QApplication = qt.QApplication
759 self.QApplication = qt.QApplication
760
760
761 def __call__( *args, **kwargs ):
761 def __call__( *args, **kwargs ):
762 return qt.qApp
762 return qt.qApp
763
763
764 def exec_loop( *args, **kwargs ):
764 def exec_loop( *args, **kwargs ):
765 pass
765 pass
766
766
767 def __getattr__( self, name ):
767 def __getattr__( self, name ):
768 return getattr( self.QApplication, name )
768 return getattr( self.QApplication, name )
769
769
770 qt.QApplication = newQApplication()
770 qt.QApplication = newQApplication()
771
771
772 # Allows us to use both Tk and QT.
772 # Allows us to use both Tk and QT.
773 self.tk = get_tk()
773 self.tk = get_tk()
774
774
775 self.IP = make_IPython(argv,user_ns=user_ns,
775 self.IP = make_IPython(argv,user_ns=user_ns,
776 user_global_ns=user_global_ns,
776 user_global_ns=user_global_ns,
777 debug=debug,
777 debug=debug,
778 shell_class=shell_class,
778 shell_class=shell_class,
779 on_kill=[qt.qApp.exit])
779 on_kill=[qt.qApp.exit])
780
780
781 # HACK: slot for banner in self; it will be passed to the mainloop
781 # HACK: slot for banner in self; it will be passed to the mainloop
782 # method only and .run() needs it. The actual value will be set by
782 # method only and .run() needs it. The actual value will be set by
783 # .mainloop().
783 # .mainloop().
784 self._banner = None
784 self._banner = None
785
785
786 threading.Thread.__init__(self)
786 threading.Thread.__init__(self)
787
787
788 def run(self):
788 def run(self):
789 self.IP.mainloop(self._banner)
789 self.IP.mainloop(self._banner)
790 self.IP.kill()
790 self.IP.kill()
791
791
792 def mainloop(self,sys_exit=0,banner=None):
792 def mainloop(self,sys_exit=0,banner=None):
793
793
794 import qt
794 import qt
795
795
796 self._banner = banner
796 self._banner = banner
797
797
798 if qt.QApplication.startingUp():
798 if qt.QApplication.startingUp():
799 a = qt.QApplication.QApplication(sys.argv)
799 a = qt.QApplication.QApplication(sys.argv)
800 self.timer = qt.QTimer()
800 self.timer = qt.QTimer()
801 qt.QObject.connect( self.timer, qt.SIGNAL( 'timeout()' ), self.on_timer )
801 qt.QObject.connect( self.timer, qt.SIGNAL( 'timeout()' ), self.on_timer )
802
802
803 self.start()
803 self.start()
804 self.timer.start( self.TIMEOUT, True )
804 self.timer.start( self.TIMEOUT, True )
805 while True:
805 while True:
806 if self.IP._kill: break
806 if self.IP._kill: break
807 qt.qApp.exec_loop()
807 qt.qApp.exec_loop()
808 self.join()
808 self.join()
809
809
810 def on_timer(self):
810 def on_timer(self):
811 update_tk(self.tk)
811 update_tk(self.tk)
812 result = self.IP.runcode()
812 result = self.IP.runcode()
813 self.timer.start( self.TIMEOUT, True )
813 self.timer.start( self.TIMEOUT, True )
814 return result
814 return result
815
815
816 # A set of matplotlib public IPython shell classes, for single-threaded
816 # A set of matplotlib public IPython shell classes, for single-threaded
817 # (Tk* and FLTK* backends) and multithreaded (GTK* and WX* backends) use.
817 # (Tk* and FLTK* backends) and multithreaded (GTK* and WX* backends) use.
818 class IPShellMatplotlib(IPShell):
818 class IPShellMatplotlib(IPShell):
819 """Subclass IPShell with MatplotlibShell as the internal shell.
819 """Subclass IPShell with MatplotlibShell as the internal shell.
820
820
821 Single-threaded class, meant for the Tk* and FLTK* backends.
821 Single-threaded class, meant for the Tk* and FLTK* backends.
822
822
823 Having this on a separate class simplifies the external driver code."""
823 Having this on a separate class simplifies the external driver code."""
824
824
825 def __init__(self,argv=None,user_ns=None,user_global_ns=None,debug=1):
825 def __init__(self,argv=None,user_ns=None,user_global_ns=None,debug=1):
826 IPShell.__init__(self,argv,user_ns,user_global_ns,debug,
826 IPShell.__init__(self,argv,user_ns,user_global_ns,debug,
827 shell_class=MatplotlibShell)
827 shell_class=MatplotlibShell)
828
828
829 class IPShellMatplotlibGTK(IPShellGTK):
829 class IPShellMatplotlibGTK(IPShellGTK):
830 """Subclass IPShellGTK with MatplotlibMTShell as the internal shell.
830 """Subclass IPShellGTK with MatplotlibMTShell as the internal shell.
831
831
832 Multi-threaded class, meant for the GTK* backends."""
832 Multi-threaded class, meant for the GTK* backends."""
833
833
834 def __init__(self,argv=None,user_ns=None,user_global_ns=None,debug=1):
834 def __init__(self,argv=None,user_ns=None,user_global_ns=None,debug=1):
835 IPShellGTK.__init__(self,argv,user_ns,user_global_ns,debug,
835 IPShellGTK.__init__(self,argv,user_ns,user_global_ns,debug,
836 shell_class=MatplotlibMTShell)
836 shell_class=MatplotlibMTShell)
837
837
838 class IPShellMatplotlibWX(IPShellWX):
838 class IPShellMatplotlibWX(IPShellWX):
839 """Subclass IPShellWX with MatplotlibMTShell as the internal shell.
839 """Subclass IPShellWX with MatplotlibMTShell as the internal shell.
840
840
841 Multi-threaded class, meant for the WX* backends."""
841 Multi-threaded class, meant for the WX* backends."""
842
842
843 def __init__(self,argv=None,user_ns=None,user_global_ns=None,debug=1):
843 def __init__(self,argv=None,user_ns=None,user_global_ns=None,debug=1):
844 IPShellWX.__init__(self,argv,user_ns,user_global_ns,debug,
844 IPShellWX.__init__(self,argv,user_ns,user_global_ns,debug,
845 shell_class=MatplotlibMTShell)
845 shell_class=MatplotlibMTShell)
846
846
847 class IPShellMatplotlibQt(IPShellQt):
847 class IPShellMatplotlibQt(IPShellQt):
848 """Subclass IPShellQt with MatplotlibMTShell as the internal shell.
848 """Subclass IPShellQt with MatplotlibMTShell as the internal shell.
849
849
850 Multi-threaded class, meant for the Qt* backends."""
850 Multi-threaded class, meant for the Qt* backends."""
851
851
852 def __init__(self,argv=None,user_ns=None,user_global_ns=None,debug=1):
852 def __init__(self,argv=None,user_ns=None,user_global_ns=None,debug=1):
853 IPShellQt.__init__(self,argv,user_ns,user_global_ns,debug,
853 IPShellQt.__init__(self,argv,user_ns,user_global_ns,debug,
854 shell_class=MatplotlibMTShell)
854 shell_class=MatplotlibMTShell)
855
855
856 #-----------------------------------------------------------------------------
856 #-----------------------------------------------------------------------------
857 # Factory functions to actually start the proper thread-aware shell
857 # Factory functions to actually start the proper thread-aware shell
858
858
859 def _matplotlib_shell_class():
859 def _matplotlib_shell_class():
860 """Factory function to handle shell class selection for matplotlib.
860 """Factory function to handle shell class selection for matplotlib.
861
861
862 The proper shell class to use depends on the matplotlib backend, since
862 The proper shell class to use depends on the matplotlib backend, since
863 each backend requires a different threading strategy."""
863 each backend requires a different threading strategy."""
864
864
865 try:
865 try:
866 import matplotlib
866 import matplotlib
867 except ImportError:
867 except ImportError:
868 error('matplotlib could NOT be imported! Starting normal IPython.')
868 error('matplotlib could NOT be imported! Starting normal IPython.')
869 sh_class = IPShell
869 sh_class = IPShell
870 else:
870 else:
871 backend = matplotlib.rcParams['backend']
871 backend = matplotlib.rcParams['backend']
872 if backend.startswith('GTK'):
872 if backend.startswith('GTK'):
873 sh_class = IPShellMatplotlibGTK
873 sh_class = IPShellMatplotlibGTK
874 elif backend.startswith('WX'):
874 elif backend.startswith('WX'):
875 sh_class = IPShellMatplotlibWX
875 sh_class = IPShellMatplotlibWX
876 elif backend.startswith('Qt'):
876 elif backend.startswith('Qt'):
877 sh_class = IPShellMatplotlibQt
877 sh_class = IPShellMatplotlibQt
878 else:
878 else:
879 sh_class = IPShellMatplotlib
879 sh_class = IPShellMatplotlib
880 #print 'Using %s with the %s backend.' % (sh_class,backend) # dbg
880 #print 'Using %s with the %s backend.' % (sh_class,backend) # dbg
881 return sh_class
881 return sh_class
882
882
883 # This is the one which should be called by external code.
883 # This is the one which should be called by external code.
884 def start():
884 def start():
885 """Return a running shell instance, dealing with threading options.
885 """Return a running shell instance, dealing with threading options.
886
886
887 This is a factory function which will instantiate the proper IPython shell
887 This is a factory function which will instantiate the proper IPython shell
888 based on the user's threading choice. Such a selector is needed because
888 based on the user's threading choice. Such a selector is needed because
889 different GUI toolkits require different thread handling details."""
889 different GUI toolkits require different thread handling details."""
890
890
891 global USE_TK
891 global USE_TK
892 # Crude sys.argv hack to extract the threading options.
892 # Crude sys.argv hack to extract the threading options.
893 argv = sys.argv
893 argv = sys.argv
894 if len(argv) > 1:
894 if len(argv) > 1:
895 if len(argv) > 2:
895 if len(argv) > 2:
896 arg2 = argv[2]
896 arg2 = argv[2]
897 if arg2.endswith('-tk'):
897 if arg2.endswith('-tk'):
898 USE_TK = True
898 USE_TK = True
899 arg1 = argv[1]
899 arg1 = argv[1]
900 if arg1.endswith('-gthread'):
900 if arg1.endswith('-gthread'):
901 shell = IPShellGTK
901 shell = IPShellGTK
902 elif arg1.endswith( '-qthread' ):
902 elif arg1.endswith( '-qthread' ):
903 shell = IPShellQt
903 shell = IPShellQt
904 elif arg1.endswith('-wthread'):
904 elif arg1.endswith('-wthread'):
905 shell = IPShellWX
905 shell = IPShellWX
906 elif arg1.endswith('-pylab'):
906 elif arg1.endswith('-pylab'):
907 shell = _matplotlib_shell_class()
907 shell = _matplotlib_shell_class()
908 else:
908 else:
909 shell = IPShell
909 shell = IPShell
910 else:
910 else:
911 shell = IPShell
911 shell = IPShell
912 return shell()
912 return shell()
913
913
914 # Some aliases for backwards compatibility
914 # Some aliases for backwards compatibility
915 IPythonShell = IPShell
915 IPythonShell = IPShell
916 IPythonShellEmbed = IPShellEmbed
916 IPythonShellEmbed = IPShellEmbed
917 #************************ End of file <Shell.py> ***************************
917 #************************ End of file <Shell.py> ***************************
@@ -1,496 +1,496 b''
1 # -*- coding: utf-8 -*-
1 # -*- coding: utf-8 -*-
2 """Manage background (threaded) jobs conveniently from an interactive shell.
2 """Manage background (threaded) jobs conveniently from an interactive shell.
3
3
4 This module provides a BackgroundJobManager class. This is the main class
4 This module provides a BackgroundJobManager class. This is the main class
5 meant for public usage, it implements an object which can create and manage
5 meant for public usage, it implements an object which can create and manage
6 new background jobs.
6 new background jobs.
7
7
8 It also provides the actual job classes managed by these BackgroundJobManager
8 It also provides the actual job classes managed by these BackgroundJobManager
9 objects, see their docstrings below.
9 objects, see their docstrings below.
10
10
11
11
12 This system was inspired by discussions with B. Granger and the
12 This system was inspired by discussions with B. Granger and the
13 BackgroundCommand class described in the book Python Scripting for
13 BackgroundCommand class described in the book Python Scripting for
14 Computational Science, by H. P. Langtangen:
14 Computational Science, by H. P. Langtangen:
15
15
16 http://folk.uio.no/hpl/scripting
16 http://folk.uio.no/hpl/scripting
17
17
18 (although ultimately no code from this text was used, as IPython's system is a
18 (although ultimately no code from this text was used, as IPython's system is a
19 separate implementation).
19 separate implementation).
20
20
21 $Id: background_jobs.py 958 2005-12-27 23:17:51Z fperez $
21 $Id: background_jobs.py 994 2006-01-08 08:29:44Z fperez $
22 """
22 """
23
23
24 #*****************************************************************************
24 #*****************************************************************************
25 # Copyright (C) 2005 Fernando Perez <fperez@colorado.edu>
25 # Copyright (C) 2005-2006 Fernando Perez <fperez@colorado.edu>
26 #
26 #
27 # Distributed under the terms of the BSD License. The full license is in
27 # Distributed under the terms of the BSD License. The full license is in
28 # the file COPYING, distributed as part of this software.
28 # the file COPYING, distributed as part of this software.
29 #*****************************************************************************
29 #*****************************************************************************
30
30
31 from IPython import Release
31 from IPython import Release
32 __author__ = '%s <%s>' % Release.authors['Fernando']
32 __author__ = '%s <%s>' % Release.authors['Fernando']
33 __license__ = Release.license
33 __license__ = Release.license
34
34
35 # Code begins
35 # Code begins
36 import sys
36 import sys
37 import threading
37 import threading
38
38
39 from IPython.ultraTB import AutoFormattedTB
39 from IPython.ultraTB import AutoFormattedTB
40 from IPython.genutils import warn,error
40 from IPython.genutils import warn,error
41
41
42 class BackgroundJobManager:
42 class BackgroundJobManager:
43 """Class to manage a pool of backgrounded threaded jobs.
43 """Class to manage a pool of backgrounded threaded jobs.
44
44
45 Below, we assume that 'jobs' is a BackgroundJobManager instance.
45 Below, we assume that 'jobs' is a BackgroundJobManager instance.
46
46
47 Usage summary (see the method docstrings for details):
47 Usage summary (see the method docstrings for details):
48
48
49 jobs.new(...) -> start a new job
49 jobs.new(...) -> start a new job
50
50
51 jobs() or jobs.status() -> print status summary of all jobs
51 jobs() or jobs.status() -> print status summary of all jobs
52
52
53 jobs[N] -> returns job number N.
53 jobs[N] -> returns job number N.
54
54
55 foo = jobs[N].result -> assign to variable foo the result of job N
55 foo = jobs[N].result -> assign to variable foo the result of job N
56
56
57 jobs[N].traceback() -> print the traceback of dead job N
57 jobs[N].traceback() -> print the traceback of dead job N
58
58
59 jobs.remove(N) -> remove (finished) job N
59 jobs.remove(N) -> remove (finished) job N
60
60
61 jobs.flush_finished() -> remove all finished jobs
61 jobs.flush_finished() -> remove all finished jobs
62
62
63 As a convenience feature, BackgroundJobManager instances provide the
63 As a convenience feature, BackgroundJobManager instances provide the
64 utility result and traceback methods which retrieve the corresponding
64 utility result and traceback methods which retrieve the corresponding
65 information from the jobs list:
65 information from the jobs list:
66
66
67 jobs.result(N) <--> jobs[N].result
67 jobs.result(N) <--> jobs[N].result
68 jobs.traceback(N) <--> jobs[N].traceback()
68 jobs.traceback(N) <--> jobs[N].traceback()
69
69
70 While this appears minor, it allows you to use tab completion
70 While this appears minor, it allows you to use tab completion
71 interactively on the job manager instance.
71 interactively on the job manager instance.
72
72
73 In interactive mode, IPython provides the magic fuction %bg for quick
73 In interactive mode, IPython provides the magic fuction %bg for quick
74 creation of backgrounded expression-based jobs. Type bg? for details."""
74 creation of backgrounded expression-based jobs. Type bg? for details."""
75
75
76 def __init__(self):
76 def __init__(self):
77 # Lists for job management
77 # Lists for job management
78 self.jobs_run = []
78 self.jobs_run = []
79 self.jobs_comp = []
79 self.jobs_comp = []
80 self.jobs_dead = []
80 self.jobs_dead = []
81 # A dict of all jobs, so users can easily access any of them
81 # A dict of all jobs, so users can easily access any of them
82 self.jobs_all = {}
82 self.jobs_all = {}
83 # For reporting
83 # For reporting
84 self._comp_report = []
84 self._comp_report = []
85 self._dead_report = []
85 self._dead_report = []
86 # Store status codes locally for fast lookups
86 # Store status codes locally for fast lookups
87 self._s_created = BackgroundJobBase.stat_created_c
87 self._s_created = BackgroundJobBase.stat_created_c
88 self._s_running = BackgroundJobBase.stat_running_c
88 self._s_running = BackgroundJobBase.stat_running_c
89 self._s_completed = BackgroundJobBase.stat_completed_c
89 self._s_completed = BackgroundJobBase.stat_completed_c
90 self._s_dead = BackgroundJobBase.stat_dead_c
90 self._s_dead = BackgroundJobBase.stat_dead_c
91
91
92 def new(self,func_or_exp,*args,**kwargs):
92 def new(self,func_or_exp,*args,**kwargs):
93 """Add a new background job and start it in a separate thread.
93 """Add a new background job and start it in a separate thread.
94
94
95 There are two types of jobs which can be created:
95 There are two types of jobs which can be created:
96
96
97 1. Jobs based on expressions which can be passed to an eval() call.
97 1. Jobs based on expressions which can be passed to an eval() call.
98 The expression must be given as a string. For example:
98 The expression must be given as a string. For example:
99
99
100 job_manager.new('myfunc(x,y,z=1)'[,glob[,loc]])
100 job_manager.new('myfunc(x,y,z=1)'[,glob[,loc]])
101
101
102 The given expression is passed to eval(), along with the optional
102 The given expression is passed to eval(), along with the optional
103 global/local dicts provided. If no dicts are given, they are
103 global/local dicts provided. If no dicts are given, they are
104 extracted automatically from the caller's frame.
104 extracted automatically from the caller's frame.
105
105
106 A Python statement is NOT a valid eval() expression. Basically, you
106 A Python statement is NOT a valid eval() expression. Basically, you
107 can only use as an eval() argument something which can go on the right
107 can only use as an eval() argument something which can go on the right
108 of an '=' sign and be assigned to a variable.
108 of an '=' sign and be assigned to a variable.
109
109
110 For example,"print 'hello'" is not valid, but '2+3' is.
110 For example,"print 'hello'" is not valid, but '2+3' is.
111
111
112 2. Jobs given a function object, optionally passing additional
112 2. Jobs given a function object, optionally passing additional
113 positional arguments:
113 positional arguments:
114
114
115 job_manager.new(myfunc,x,y)
115 job_manager.new(myfunc,x,y)
116
116
117 The function is called with the given arguments.
117 The function is called with the given arguments.
118
118
119 If you need to pass keyword arguments to your function, you must
119 If you need to pass keyword arguments to your function, you must
120 supply them as a dict named kw:
120 supply them as a dict named kw:
121
121
122 job_manager.new(myfunc,x,y,kw=dict(z=1))
122 job_manager.new(myfunc,x,y,kw=dict(z=1))
123
123
124 The reason for this assymmetry is that the new() method needs to
124 The reason for this assymmetry is that the new() method needs to
125 maintain access to its own keywords, and this prevents name collisions
125 maintain access to its own keywords, and this prevents name collisions
126 between arguments to new() and arguments to your own functions.
126 between arguments to new() and arguments to your own functions.
127
127
128 In both cases, the result is stored in the job.result field of the
128 In both cases, the result is stored in the job.result field of the
129 background job object.
129 background job object.
130
130
131
131
132 Notes and caveats:
132 Notes and caveats:
133
133
134 1. All threads running share the same standard output. Thus, if your
134 1. All threads running share the same standard output. Thus, if your
135 background jobs generate output, it will come out on top of whatever
135 background jobs generate output, it will come out on top of whatever
136 you are currently writing. For this reason, background jobs are best
136 you are currently writing. For this reason, background jobs are best
137 used with silent functions which simply return their output.
137 used with silent functions which simply return their output.
138
138
139 2. Threads also all work within the same global namespace, and this
139 2. Threads also all work within the same global namespace, and this
140 system does not lock interactive variables. So if you send job to the
140 system does not lock interactive variables. So if you send job to the
141 background which operates on a mutable object for a long time, and
141 background which operates on a mutable object for a long time, and
142 start modifying that same mutable object interactively (or in another
142 start modifying that same mutable object interactively (or in another
143 backgrounded job), all sorts of bizarre behaviour will occur.
143 backgrounded job), all sorts of bizarre behaviour will occur.
144
144
145 3. If a background job is spending a lot of time inside a C extension
145 3. If a background job is spending a lot of time inside a C extension
146 module which does not release the Python Global Interpreter Lock
146 module which does not release the Python Global Interpreter Lock
147 (GIL), this will block the IPython prompt. This is simply because the
147 (GIL), this will block the IPython prompt. This is simply because the
148 Python interpreter can only switch between threads at Python
148 Python interpreter can only switch between threads at Python
149 bytecodes. While the execution is inside C code, the interpreter must
149 bytecodes. While the execution is inside C code, the interpreter must
150 simply wait unless the extension module releases the GIL.
150 simply wait unless the extension module releases the GIL.
151
151
152 4. There is no way, due to limitations in the Python threads library,
152 4. There is no way, due to limitations in the Python threads library,
153 to kill a thread once it has started."""
153 to kill a thread once it has started."""
154
154
155 if callable(func_or_exp):
155 if callable(func_or_exp):
156 kw = kwargs.get('kw',{})
156 kw = kwargs.get('kw',{})
157 job = BackgroundJobFunc(func_or_exp,*args,**kw)
157 job = BackgroundJobFunc(func_or_exp,*args,**kw)
158 elif isinstance(func_or_exp,basestring):
158 elif isinstance(func_or_exp,basestring):
159 if not args:
159 if not args:
160 frame = sys._getframe(1)
160 frame = sys._getframe(1)
161 glob, loc = frame.f_globals, frame.f_locals
161 glob, loc = frame.f_globals, frame.f_locals
162 elif len(args)==1:
162 elif len(args)==1:
163 glob = loc = args[0]
163 glob = loc = args[0]
164 elif len(args)==2:
164 elif len(args)==2:
165 glob,loc = args
165 glob,loc = args
166 else:
166 else:
167 raise ValueError,\
167 raise ValueError,\
168 'Expression jobs take at most 2 args (globals,locals)'
168 'Expression jobs take at most 2 args (globals,locals)'
169 job = BackgroundJobExpr(func_or_exp,glob,loc)
169 job = BackgroundJobExpr(func_or_exp,glob,loc)
170 else:
170 else:
171 raise
171 raise
172 jkeys = self.jobs_all.keys()
172 jkeys = self.jobs_all.keys()
173 if jkeys:
173 if jkeys:
174 job.num = max(jkeys)+1
174 job.num = max(jkeys)+1
175 else:
175 else:
176 job.num = 0
176 job.num = 0
177 self.jobs_run.append(job)
177 self.jobs_run.append(job)
178 self.jobs_all[job.num] = job
178 self.jobs_all[job.num] = job
179 print 'Starting job # %s in a separate thread.' % job.num
179 print 'Starting job # %s in a separate thread.' % job.num
180 job.start()
180 job.start()
181 return job
181 return job
182
182
183 def __getitem__(self,key):
183 def __getitem__(self,key):
184 return self.jobs_all[key]
184 return self.jobs_all[key]
185
185
186 def __call__(self):
186 def __call__(self):
187 """An alias to self.status(),
187 """An alias to self.status(),
188
188
189 This allows you to simply call a job manager instance much like the
189 This allows you to simply call a job manager instance much like the
190 Unix jobs shell command."""
190 Unix jobs shell command."""
191
191
192 return self.status()
192 return self.status()
193
193
194 def _update_status(self):
194 def _update_status(self):
195 """Update the status of the job lists.
195 """Update the status of the job lists.
196
196
197 This method moves finished jobs to one of two lists:
197 This method moves finished jobs to one of two lists:
198 - self.jobs_comp: jobs which completed successfully
198 - self.jobs_comp: jobs which completed successfully
199 - self.jobs_dead: jobs which finished but died.
199 - self.jobs_dead: jobs which finished but died.
200
200
201 It also copies those jobs to corresponding _report lists. These lists
201 It also copies those jobs to corresponding _report lists. These lists
202 are used to report jobs completed/dead since the last update, and are
202 are used to report jobs completed/dead since the last update, and are
203 then cleared by the reporting function after each call."""
203 then cleared by the reporting function after each call."""
204
204
205 run,comp,dead = self._s_running,self._s_completed,self._s_dead
205 run,comp,dead = self._s_running,self._s_completed,self._s_dead
206 jobs_run = self.jobs_run
206 jobs_run = self.jobs_run
207 for num in range(len(jobs_run)):
207 for num in range(len(jobs_run)):
208 job = jobs_run[num]
208 job = jobs_run[num]
209 stat = job.stat_code
209 stat = job.stat_code
210 if stat == run:
210 if stat == run:
211 continue
211 continue
212 elif stat == comp:
212 elif stat == comp:
213 self.jobs_comp.append(job)
213 self.jobs_comp.append(job)
214 self._comp_report.append(job)
214 self._comp_report.append(job)
215 jobs_run[num] = False
215 jobs_run[num] = False
216 elif stat == dead:
216 elif stat == dead:
217 self.jobs_dead.append(job)
217 self.jobs_dead.append(job)
218 self._dead_report.append(job)
218 self._dead_report.append(job)
219 jobs_run[num] = False
219 jobs_run[num] = False
220 self.jobs_run = filter(None,self.jobs_run)
220 self.jobs_run = filter(None,self.jobs_run)
221
221
222 def _group_report(self,group,name):
222 def _group_report(self,group,name):
223 """Report summary for a given job group.
223 """Report summary for a given job group.
224
224
225 Return True if the group had any elements."""
225 Return True if the group had any elements."""
226
226
227 if group:
227 if group:
228 print '%s jobs:' % name
228 print '%s jobs:' % name
229 for job in group:
229 for job in group:
230 print '%s : %s' % (job.num,job)
230 print '%s : %s' % (job.num,job)
231 print
231 print
232 return True
232 return True
233
233
234 def _group_flush(self,group,name):
234 def _group_flush(self,group,name):
235 """Flush a given job group
235 """Flush a given job group
236
236
237 Return True if the group had any elements."""
237 Return True if the group had any elements."""
238
238
239 njobs = len(group)
239 njobs = len(group)
240 if njobs:
240 if njobs:
241 plural = {1:''}.setdefault(njobs,'s')
241 plural = {1:''}.setdefault(njobs,'s')
242 print 'Flushing %s %s job%s.' % (njobs,name,plural)
242 print 'Flushing %s %s job%s.' % (njobs,name,plural)
243 group[:] = []
243 group[:] = []
244 return True
244 return True
245
245
246 def _status_new(self):
246 def _status_new(self):
247 """Print the status of newly finished jobs.
247 """Print the status of newly finished jobs.
248
248
249 Return True if any new jobs are reported.
249 Return True if any new jobs are reported.
250
250
251 This call resets its own state every time, so it only reports jobs
251 This call resets its own state every time, so it only reports jobs
252 which have finished since the last time it was called."""
252 which have finished since the last time it was called."""
253
253
254 self._update_status()
254 self._update_status()
255 new_comp = self._group_report(self._comp_report,'Completed')
255 new_comp = self._group_report(self._comp_report,'Completed')
256 new_dead = self._group_report(self._dead_report,
256 new_dead = self._group_report(self._dead_report,
257 'Dead, call job.traceback() for details')
257 'Dead, call job.traceback() for details')
258 self._comp_report[:] = []
258 self._comp_report[:] = []
259 self._dead_report[:] = []
259 self._dead_report[:] = []
260 return new_comp or new_dead
260 return new_comp or new_dead
261
261
262 def status(self,verbose=0):
262 def status(self,verbose=0):
263 """Print a status of all jobs currently being managed."""
263 """Print a status of all jobs currently being managed."""
264
264
265 self._update_status()
265 self._update_status()
266 self._group_report(self.jobs_run,'Running')
266 self._group_report(self.jobs_run,'Running')
267 self._group_report(self.jobs_comp,'Completed')
267 self._group_report(self.jobs_comp,'Completed')
268 self._group_report(self.jobs_dead,'Dead')
268 self._group_report(self.jobs_dead,'Dead')
269 # Also flush the report queues
269 # Also flush the report queues
270 self._comp_report[:] = []
270 self._comp_report[:] = []
271 self._dead_report[:] = []
271 self._dead_report[:] = []
272
272
273 def remove(self,num):
273 def remove(self,num):
274 """Remove a finished (completed or dead) job."""
274 """Remove a finished (completed or dead) job."""
275
275
276 try:
276 try:
277 job = self.jobs_all[num]
277 job = self.jobs_all[num]
278 except KeyError:
278 except KeyError:
279 error('Job #%s not found' % num)
279 error('Job #%s not found' % num)
280 else:
280 else:
281 stat_code = job.stat_code
281 stat_code = job.stat_code
282 if stat_code == self._s_running:
282 if stat_code == self._s_running:
283 error('Job #%s is still running, it can not be removed.' % num)
283 error('Job #%s is still running, it can not be removed.' % num)
284 return
284 return
285 elif stat_code == self._s_completed:
285 elif stat_code == self._s_completed:
286 self.jobs_comp.remove(job)
286 self.jobs_comp.remove(job)
287 elif stat_code == self._s_dead:
287 elif stat_code == self._s_dead:
288 self.jobs_dead.remove(job)
288 self.jobs_dead.remove(job)
289
289
290 def flush_finished(self):
290 def flush_finished(self):
291 """Flush all jobs finished (completed and dead) from lists.
291 """Flush all jobs finished (completed and dead) from lists.
292
292
293 Running jobs are never flushed.
293 Running jobs are never flushed.
294
294
295 It first calls _status_new(), to update info. If any jobs have
295 It first calls _status_new(), to update info. If any jobs have
296 completed since the last _status_new() call, the flush operation
296 completed since the last _status_new() call, the flush operation
297 aborts."""
297 aborts."""
298
298
299 if self._status_new():
299 if self._status_new():
300 error('New jobs completed since last '\
300 error('New jobs completed since last '\
301 '_status_new(), aborting flush.')
301 '_status_new(), aborting flush.')
302 return
302 return
303
303
304 # Remove the finished jobs from the master dict
304 # Remove the finished jobs from the master dict
305 jobs_all = self.jobs_all
305 jobs_all = self.jobs_all
306 for job in self.jobs_comp+self.jobs_dead:
306 for job in self.jobs_comp+self.jobs_dead:
307 del(jobs_all[job.num])
307 del(jobs_all[job.num])
308
308
309 # Now flush these lists completely
309 # Now flush these lists completely
310 fl_comp = self._group_flush(self.jobs_comp,'Completed')
310 fl_comp = self._group_flush(self.jobs_comp,'Completed')
311 fl_dead = self._group_flush(self.jobs_dead,'Dead')
311 fl_dead = self._group_flush(self.jobs_dead,'Dead')
312 if not (fl_comp or fl_dead):
312 if not (fl_comp or fl_dead):
313 print 'No jobs to flush.'
313 print 'No jobs to flush.'
314
314
315 def result(self,num):
315 def result(self,num):
316 """result(N) -> return the result of job N."""
316 """result(N) -> return the result of job N."""
317 try:
317 try:
318 return self.jobs_all[num].result
318 return self.jobs_all[num].result
319 except KeyError:
319 except KeyError:
320 error('Job #%s not found' % num)
320 error('Job #%s not found' % num)
321
321
322 def traceback(self,num):
322 def traceback(self,num):
323 try:
323 try:
324 self.jobs_all[num].traceback()
324 self.jobs_all[num].traceback()
325 except KeyError:
325 except KeyError:
326 error('Job #%s not found' % num)
326 error('Job #%s not found' % num)
327
327
328
328
329 class BackgroundJobBase(threading.Thread):
329 class BackgroundJobBase(threading.Thread):
330 """Base class to build BackgroundJob classes.
330 """Base class to build BackgroundJob classes.
331
331
332 The derived classes must implement:
332 The derived classes must implement:
333
333
334 - Their own __init__, since the one here raises NotImplementedError. The
334 - Their own __init__, since the one here raises NotImplementedError. The
335 derived constructor must call self._init() at the end, to provide common
335 derived constructor must call self._init() at the end, to provide common
336 initialization.
336 initialization.
337
337
338 - A strform attribute used in calls to __str__.
338 - A strform attribute used in calls to __str__.
339
339
340 - A call() method, which will make the actual execution call and must
340 - A call() method, which will make the actual execution call and must
341 return a value to be held in the 'result' field of the job object."""
341 return a value to be held in the 'result' field of the job object."""
342
342
343 # Class constants for status, in string and as numerical codes (when
343 # Class constants for status, in string and as numerical codes (when
344 # updating jobs lists, we don't want to do string comparisons). This will
344 # updating jobs lists, we don't want to do string comparisons). This will
345 # be done at every user prompt, so it has to be as fast as possible
345 # be done at every user prompt, so it has to be as fast as possible
346 stat_created = 'Created'; stat_created_c = 0
346 stat_created = 'Created'; stat_created_c = 0
347 stat_running = 'Running'; stat_running_c = 1
347 stat_running = 'Running'; stat_running_c = 1
348 stat_completed = 'Completed'; stat_completed_c = 2
348 stat_completed = 'Completed'; stat_completed_c = 2
349 stat_dead = 'Dead (Exception), call job.traceback() for details'
349 stat_dead = 'Dead (Exception), call job.traceback() for details'
350 stat_dead_c = -1
350 stat_dead_c = -1
351
351
352 def __init__(self):
352 def __init__(self):
353 raise NotImplementedError, \
353 raise NotImplementedError, \
354 "This class can not be instantiated directly."
354 "This class can not be instantiated directly."
355
355
356 def _init(self):
356 def _init(self):
357 """Common initialization for all BackgroundJob objects"""
357 """Common initialization for all BackgroundJob objects"""
358
358
359 for attr in ['call','strform']:
359 for attr in ['call','strform']:
360 assert hasattr(self,attr), "Missing attribute <%s>" % attr
360 assert hasattr(self,attr), "Missing attribute <%s>" % attr
361
361
362 # The num tag can be set by an external job manager
362 # The num tag can be set by an external job manager
363 self.num = None
363 self.num = None
364
364
365 self.status = BackgroundJobBase.stat_created
365 self.status = BackgroundJobBase.stat_created
366 self.stat_code = BackgroundJobBase.stat_created_c
366 self.stat_code = BackgroundJobBase.stat_created_c
367 self.finished = False
367 self.finished = False
368 self.result = '<BackgroundJob has not completed>'
368 self.result = '<BackgroundJob has not completed>'
369 # reuse the ipython traceback handler if we can get to it, otherwise
369 # reuse the ipython traceback handler if we can get to it, otherwise
370 # make a new one
370 # make a new one
371 try:
371 try:
372 self._make_tb = __IPYTHON__.InteractiveTB.text
372 self._make_tb = __IPYTHON__.InteractiveTB.text
373 except:
373 except:
374 self._make_tb = AutoFormattedTB(mode = 'Context',
374 self._make_tb = AutoFormattedTB(mode = 'Context',
375 color_scheme='NoColor',
375 color_scheme='NoColor',
376 tb_offset = 1).text
376 tb_offset = 1).text
377 # Hold a formatted traceback if one is generated.
377 # Hold a formatted traceback if one is generated.
378 self._tb = None
378 self._tb = None
379
379
380 threading.Thread.__init__(self)
380 threading.Thread.__init__(self)
381
381
382 def __str__(self):
382 def __str__(self):
383 return self.strform
383 return self.strform
384
384
385 def __repr__(self):
385 def __repr__(self):
386 return '<BackgroundJob: %s>' % self.strform
386 return '<BackgroundJob: %s>' % self.strform
387
387
388 def traceback(self):
388 def traceback(self):
389 print self._tb
389 print self._tb
390
390
391 def run(self):
391 def run(self):
392 try:
392 try:
393 self.status = BackgroundJobBase.stat_running
393 self.status = BackgroundJobBase.stat_running
394 self.stat_code = BackgroundJobBase.stat_running_c
394 self.stat_code = BackgroundJobBase.stat_running_c
395 self.result = self.call()
395 self.result = self.call()
396 except:
396 except:
397 self.status = BackgroundJobBase.stat_dead
397 self.status = BackgroundJobBase.stat_dead
398 self.stat_code = BackgroundJobBase.stat_dead_c
398 self.stat_code = BackgroundJobBase.stat_dead_c
399 self.finished = None
399 self.finished = None
400 self.result = ('<BackgroundJob died, call job.traceback() for details>')
400 self.result = ('<BackgroundJob died, call job.traceback() for details>')
401 self._tb = self._make_tb()
401 self._tb = self._make_tb()
402 else:
402 else:
403 self.status = BackgroundJobBase.stat_completed
403 self.status = BackgroundJobBase.stat_completed
404 self.stat_code = BackgroundJobBase.stat_completed_c
404 self.stat_code = BackgroundJobBase.stat_completed_c
405 self.finished = True
405 self.finished = True
406
406
407 class BackgroundJobExpr(BackgroundJobBase):
407 class BackgroundJobExpr(BackgroundJobBase):
408 """Evaluate an expression as a background job (uses a separate thread)."""
408 """Evaluate an expression as a background job (uses a separate thread)."""
409
409
410 def __init__(self,expression,glob=None,loc=None):
410 def __init__(self,expression,glob=None,loc=None):
411 """Create a new job from a string which can be fed to eval().
411 """Create a new job from a string which can be fed to eval().
412
412
413 global/locals dicts can be provided, which will be passed to the eval
413 global/locals dicts can be provided, which will be passed to the eval
414 call."""
414 call."""
415
415
416 # fail immediately if the given expression can't be compiled
416 # fail immediately if the given expression can't be compiled
417 self.code = compile(expression,'<BackgroundJob compilation>','eval')
417 self.code = compile(expression,'<BackgroundJob compilation>','eval')
418
418
419 if glob is None:
419 if glob is None:
420 glob = {}
420 glob = {}
421 if loc is None:
421 if loc is None:
422 loc = {}
422 loc = {}
423
423
424 self.expression = self.strform = expression
424 self.expression = self.strform = expression
425 self.glob = glob
425 self.glob = glob
426 self.loc = loc
426 self.loc = loc
427 self._init()
427 self._init()
428
428
429 def call(self):
429 def call(self):
430 return eval(self.code,self.glob,self.loc)
430 return eval(self.code,self.glob,self.loc)
431
431
432 class BackgroundJobFunc(BackgroundJobBase):
432 class BackgroundJobFunc(BackgroundJobBase):
433 """Run a function call as a background job (uses a separate thread)."""
433 """Run a function call as a background job (uses a separate thread)."""
434
434
435 def __init__(self,func,*args,**kwargs):
435 def __init__(self,func,*args,**kwargs):
436 """Create a new job from a callable object.
436 """Create a new job from a callable object.
437
437
438 Any positional arguments and keyword args given to this constructor
438 Any positional arguments and keyword args given to this constructor
439 after the initial callable are passed directly to it."""
439 after the initial callable are passed directly to it."""
440
440
441 assert callable(func),'first argument must be callable'
441 assert callable(func),'first argument must be callable'
442
442
443 if args is None:
443 if args is None:
444 args = []
444 args = []
445 if kwargs is None:
445 if kwargs is None:
446 kwargs = {}
446 kwargs = {}
447
447
448 self.func = func
448 self.func = func
449 self.args = args
449 self.args = args
450 self.kwargs = kwargs
450 self.kwargs = kwargs
451 # The string form will only include the function passed, because
451 # The string form will only include the function passed, because
452 # generating string representations of the arguments is a potentially
452 # generating string representations of the arguments is a potentially
453 # _very_ expensive operation (e.g. with large arrays).
453 # _very_ expensive operation (e.g. with large arrays).
454 self.strform = str(func)
454 self.strform = str(func)
455 self._init()
455 self._init()
456
456
457 def call(self):
457 def call(self):
458 return self.func(*self.args,**self.kwargs)
458 return self.func(*self.args,**self.kwargs)
459
459
460
460
461 if __name__=='__main__':
461 if __name__=='__main__':
462
462
463 import time
463 import time
464
464
465 def sleepfunc(interval=2,*a,**kw):
465 def sleepfunc(interval=2,*a,**kw):
466 args = dict(interval=interval,
466 args = dict(interval=interval,
467 args=a,
467 args=a,
468 kwargs=kw)
468 kwargs=kw)
469 time.sleep(interval)
469 time.sleep(interval)
470 return args
470 return args
471
471
472 def diefunc(interval=2,*a,**kw):
472 def diefunc(interval=2,*a,**kw):
473 time.sleep(interval)
473 time.sleep(interval)
474 die
474 die
475
475
476 def printfunc(interval=1,reps=5):
476 def printfunc(interval=1,reps=5):
477 for n in range(reps):
477 for n in range(reps):
478 time.sleep(interval)
478 time.sleep(interval)
479 print 'In the background...'
479 print 'In the background...'
480
480
481 jobs = BackgroundJobManager()
481 jobs = BackgroundJobManager()
482 # first job will have # 0
482 # first job will have # 0
483 jobs.new(sleepfunc,4)
483 jobs.new(sleepfunc,4)
484 jobs.new(sleepfunc,kw={'reps':2})
484 jobs.new(sleepfunc,kw={'reps':2})
485 # This makes a job which will die
485 # This makes a job which will die
486 jobs.new(diefunc,1)
486 jobs.new(diefunc,1)
487 jobs.new('printfunc(1,3)')
487 jobs.new('printfunc(1,3)')
488
488
489 # after a while, you can get the traceback of a dead job. Run the line
489 # after a while, you can get the traceback of a dead job. Run the line
490 # below again interactively until it prints a traceback (check the status
490 # below again interactively until it prints a traceback (check the status
491 # of the job):
491 # of the job):
492 print jobs[1].status
492 print jobs[1].status
493 jobs[1].traceback()
493 jobs[1].traceback()
494
494
495 # Run this line again until the printed result changes
495 # Run this line again until the printed result changes
496 print "The result of job #0 is:",jobs[0].result
496 print "The result of job #0 is:",jobs[0].result
@@ -1,556 +1,556 b''
1 """Word completion for IPython.
1 """Word completion for IPython.
2
2
3 This module is a fork of the rlcompleter module in the Python standard
3 This module is a fork of the rlcompleter module in the Python standard
4 library. The original enhancements made to rlcompleter have been sent
4 library. The original enhancements made to rlcompleter have been sent
5 upstream and were accepted as of Python 2.3, but we need a lot more
5 upstream and were accepted as of Python 2.3, but we need a lot more
6 functionality specific to IPython, so this module will continue to live as an
6 functionality specific to IPython, so this module will continue to live as an
7 IPython-specific utility.
7 IPython-specific utility.
8
8
9 ---------------------------------------------------------------------------
9 ---------------------------------------------------------------------------
10 Original rlcompleter documentation:
10 Original rlcompleter documentation:
11
11
12 This requires the latest extension to the readline module (the
12 This requires the latest extension to the readline module (the
13 completes keywords, built-ins and globals in __main__; when completing
13 completes keywords, built-ins and globals in __main__; when completing
14 NAME.NAME..., it evaluates (!) the expression up to the last dot and
14 NAME.NAME..., it evaluates (!) the expression up to the last dot and
15 completes its attributes.
15 completes its attributes.
16
16
17 It's very cool to do "import string" type "string.", hit the
17 It's very cool to do "import string" type "string.", hit the
18 completion key (twice), and see the list of names defined by the
18 completion key (twice), and see the list of names defined by the
19 string module!
19 string module!
20
20
21 Tip: to use the tab key as the completion key, call
21 Tip: to use the tab key as the completion key, call
22
22
23 readline.parse_and_bind("tab: complete")
23 readline.parse_and_bind("tab: complete")
24
24
25 Notes:
25 Notes:
26
26
27 - Exceptions raised by the completer function are *ignored* (and
27 - Exceptions raised by the completer function are *ignored* (and
28 generally cause the completion to fail). This is a feature -- since
28 generally cause the completion to fail). This is a feature -- since
29 readline sets the tty device in raw (or cbreak) mode, printing a
29 readline sets the tty device in raw (or cbreak) mode, printing a
30 traceback wouldn't work well without some complicated hoopla to save,
30 traceback wouldn't work well without some complicated hoopla to save,
31 reset and restore the tty state.
31 reset and restore the tty state.
32
32
33 - The evaluation of the NAME.NAME... form may cause arbitrary
33 - The evaluation of the NAME.NAME... form may cause arbitrary
34 application defined code to be executed if an object with a
34 application defined code to be executed if an object with a
35 __getattr__ hook is found. Since it is the responsibility of the
35 __getattr__ hook is found. Since it is the responsibility of the
36 application (or the user) to enable this feature, I consider this an
36 application (or the user) to enable this feature, I consider this an
37 acceptable risk. More complicated expressions (e.g. function calls or
37 acceptable risk. More complicated expressions (e.g. function calls or
38 indexing operations) are *not* evaluated.
38 indexing operations) are *not* evaluated.
39
39
40 - GNU readline is also used by the built-in functions input() and
40 - GNU readline is also used by the built-in functions input() and
41 raw_input(), and thus these also benefit/suffer from the completer
41 raw_input(), and thus these also benefit/suffer from the completer
42 features. Clearly an interactive application can benefit by
42 features. Clearly an interactive application can benefit by
43 specifying its own completer function and using raw_input() for all
43 specifying its own completer function and using raw_input() for all
44 its input.
44 its input.
45
45
46 - When the original stdin is not a tty device, GNU readline is never
46 - When the original stdin is not a tty device, GNU readline is never
47 used, and this module (and the readline module) are silently inactive.
47 used, and this module (and the readline module) are silently inactive.
48
48
49 """
49 """
50
50
51 #*****************************************************************************
51 #*****************************************************************************
52 #
52 #
53 # Since this file is essentially a minimally modified copy of the rlcompleter
53 # Since this file is essentially a minimally modified copy of the rlcompleter
54 # module which is part of the standard Python distribution, I assume that the
54 # module which is part of the standard Python distribution, I assume that the
55 # proper procedure is to maintain its copyright as belonging to the Python
55 # proper procedure is to maintain its copyright as belonging to the Python
56 # Software Foundation (in addition to my own, for all new code).
56 # Software Foundation (in addition to my own, for all new code).
57 #
57 #
58 # Copyright (C) 2001 Python Software Foundation, www.python.org
58 # Copyright (C) 2001 Python Software Foundation, www.python.org
59 # Copyright (C) 2001-2005 Fernando Perez. <fperez@colorado.edu>
59 # Copyright (C) 2001-2006 Fernando Perez. <fperez@colorado.edu>
60 #
60 #
61 # Distributed under the terms of the BSD License. The full license is in
61 # Distributed under the terms of the BSD License. The full license is in
62 # the file COPYING, distributed as part of this software.
62 # the file COPYING, distributed as part of this software.
63 #
63 #
64 #*****************************************************************************
64 #*****************************************************************************
65
65
66 import __builtin__
66 import __builtin__
67 import __main__
67 import __main__
68 import glob
68 import glob
69 import keyword
69 import keyword
70 import os
70 import os
71 import re
71 import re
72 import readline
72 import readline
73 import sys
73 import sys
74 import types
74 import types
75
75
76 # Python 2.4 offers sets as a builtin
76 # Python 2.4 offers sets as a builtin
77 try:
77 try:
78 set([1,2])
78 set([1,2])
79 except NameError:
79 except NameError:
80 from sets import Set as set
80 from sets import Set as set
81
81
82
82
83 from IPython.genutils import shlex_split
83 from IPython.genutils import shlex_split
84
84
85 __all__ = ['Completer','IPCompleter']
85 __all__ = ['Completer','IPCompleter']
86
86
87 def get_class_members(cls):
87 def get_class_members(cls):
88 ret = dir(cls)
88 ret = dir(cls)
89 if hasattr(cls,'__bases__'):
89 if hasattr(cls,'__bases__'):
90 for base in cls.__bases__:
90 for base in cls.__bases__:
91 ret.extend(get_class_members(base))
91 ret.extend(get_class_members(base))
92 return ret
92 return ret
93
93
94 class Completer:
94 class Completer:
95 def __init__(self,namespace=None,global_namespace=None):
95 def __init__(self,namespace=None,global_namespace=None):
96 """Create a new completer for the command line.
96 """Create a new completer for the command line.
97
97
98 Completer([namespace,global_namespace]) -> completer instance.
98 Completer([namespace,global_namespace]) -> completer instance.
99
99
100 If unspecified, the default namespace where completions are performed
100 If unspecified, the default namespace where completions are performed
101 is __main__ (technically, __main__.__dict__). Namespaces should be
101 is __main__ (technically, __main__.__dict__). Namespaces should be
102 given as dictionaries.
102 given as dictionaries.
103
103
104 An optional second namespace can be given. This allows the completer
104 An optional second namespace can be given. This allows the completer
105 to handle cases where both the local and global scopes need to be
105 to handle cases where both the local and global scopes need to be
106 distinguished.
106 distinguished.
107
107
108 Completer instances should be used as the completion mechanism of
108 Completer instances should be used as the completion mechanism of
109 readline via the set_completer() call:
109 readline via the set_completer() call:
110
110
111 readline.set_completer(Completer(my_namespace).complete)
111 readline.set_completer(Completer(my_namespace).complete)
112 """
112 """
113
113
114 # some minimal strict typechecks. For some core data structures, I
114 # some minimal strict typechecks. For some core data structures, I
115 # want actual basic python types, not just anything that looks like
115 # want actual basic python types, not just anything that looks like
116 # one. This is especially true for namespaces.
116 # one. This is especially true for namespaces.
117 for ns in (namespace,global_namespace):
117 for ns in (namespace,global_namespace):
118 if ns is not None and type(ns) != types.DictType:
118 if ns is not None and type(ns) != types.DictType:
119 raise TypeError,'namespace must be a dictionary'
119 raise TypeError,'namespace must be a dictionary'
120
120
121 # Don't bind to namespace quite yet, but flag whether the user wants a
121 # Don't bind to namespace quite yet, but flag whether the user wants a
122 # specific namespace or to use __main__.__dict__. This will allow us
122 # specific namespace or to use __main__.__dict__. This will allow us
123 # to bind to __main__.__dict__ at completion time, not now.
123 # to bind to __main__.__dict__ at completion time, not now.
124 if namespace is None:
124 if namespace is None:
125 self.use_main_ns = 1
125 self.use_main_ns = 1
126 else:
126 else:
127 self.use_main_ns = 0
127 self.use_main_ns = 0
128 self.namespace = namespace
128 self.namespace = namespace
129
129
130 # The global namespace, if given, can be bound directly
130 # The global namespace, if given, can be bound directly
131 if global_namespace is None:
131 if global_namespace is None:
132 self.global_namespace = {}
132 self.global_namespace = {}
133 else:
133 else:
134 self.global_namespace = global_namespace
134 self.global_namespace = global_namespace
135
135
136 def complete(self, text, state):
136 def complete(self, text, state):
137 """Return the next possible completion for 'text'.
137 """Return the next possible completion for 'text'.
138
138
139 This is called successively with state == 0, 1, 2, ... until it
139 This is called successively with state == 0, 1, 2, ... until it
140 returns None. The completion should begin with 'text'.
140 returns None. The completion should begin with 'text'.
141
141
142 """
142 """
143 if self.use_main_ns:
143 if self.use_main_ns:
144 self.namespace = __main__.__dict__
144 self.namespace = __main__.__dict__
145
145
146 if state == 0:
146 if state == 0:
147 if "." in text:
147 if "." in text:
148 self.matches = self.attr_matches(text)
148 self.matches = self.attr_matches(text)
149 else:
149 else:
150 self.matches = self.global_matches(text)
150 self.matches = self.global_matches(text)
151 try:
151 try:
152 return self.matches[state]
152 return self.matches[state]
153 except IndexError:
153 except IndexError:
154 return None
154 return None
155
155
156 def global_matches(self, text):
156 def global_matches(self, text):
157 """Compute matches when text is a simple name.
157 """Compute matches when text is a simple name.
158
158
159 Return a list of all keywords, built-in functions and names currently
159 Return a list of all keywords, built-in functions and names currently
160 defined in self.namespace or self.global_namespace that match.
160 defined in self.namespace or self.global_namespace that match.
161
161
162 """
162 """
163 matches = []
163 matches = []
164 match_append = matches.append
164 match_append = matches.append
165 n = len(text)
165 n = len(text)
166 for lst in [keyword.kwlist,
166 for lst in [keyword.kwlist,
167 __builtin__.__dict__.keys(),
167 __builtin__.__dict__.keys(),
168 self.namespace.keys(),
168 self.namespace.keys(),
169 self.global_namespace.keys()]:
169 self.global_namespace.keys()]:
170 for word in lst:
170 for word in lst:
171 if word[:n] == text and word != "__builtins__":
171 if word[:n] == text and word != "__builtins__":
172 match_append(word)
172 match_append(word)
173 return matches
173 return matches
174
174
175 def attr_matches(self, text):
175 def attr_matches(self, text):
176 """Compute matches when text contains a dot.
176 """Compute matches when text contains a dot.
177
177
178 Assuming the text is of the form NAME.NAME....[NAME], and is
178 Assuming the text is of the form NAME.NAME....[NAME], and is
179 evaluatable in self.namespace or self.global_namespace, it will be
179 evaluatable in self.namespace or self.global_namespace, it will be
180 evaluated and its attributes (as revealed by dir()) are used as
180 evaluated and its attributes (as revealed by dir()) are used as
181 possible completions. (For class instances, class members are are
181 possible completions. (For class instances, class members are are
182 also considered.)
182 also considered.)
183
183
184 WARNING: this can still invoke arbitrary C code, if an object
184 WARNING: this can still invoke arbitrary C code, if an object
185 with a __getattr__ hook is evaluated.
185 with a __getattr__ hook is evaluated.
186
186
187 """
187 """
188 import re
188 import re
189
189
190 # Another option, seems to work great. Catches things like ''.<tab>
190 # Another option, seems to work great. Catches things like ''.<tab>
191 m = re.match(r"(\S+(\.\w+)*)\.(\w*)$", text)
191 m = re.match(r"(\S+(\.\w+)*)\.(\w*)$", text)
192
192
193 if not m:
193 if not m:
194 return []
194 return []
195
195
196 expr, attr = m.group(1, 3)
196 expr, attr = m.group(1, 3)
197 try:
197 try:
198 object = eval(expr, self.namespace)
198 object = eval(expr, self.namespace)
199 except:
199 except:
200 object = eval(expr, self.global_namespace)
200 object = eval(expr, self.global_namespace)
201
201
202 # Start building the attribute list via dir(), and then complete it
202 # Start building the attribute list via dir(), and then complete it
203 # with a few extra special-purpose calls.
203 # with a few extra special-purpose calls.
204 words = dir(object)
204 words = dir(object)
205
205
206 if hasattr(object,'__class__'):
206 if hasattr(object,'__class__'):
207 words.append('__class__')
207 words.append('__class__')
208 words.extend(get_class_members(object.__class__))
208 words.extend(get_class_members(object.__class__))
209
209
210 # this is the 'dir' function for objects with Enthought's traits
210 # this is the 'dir' function for objects with Enthought's traits
211 if hasattr(object, 'trait_names'):
211 if hasattr(object, 'trait_names'):
212 words.extend(object.trait_names())
212 words.extend(object.trait_names())
213 # eliminate possible duplicates, as some traits may also appear as
213 # eliminate possible duplicates, as some traits may also appear as
214 # normal attributes in the dir() call.
214 # normal attributes in the dir() call.
215 words = set(words)
215 words = set(words)
216
216
217 # filter out non-string attributes which may be stuffed by dir() calls
217 # filter out non-string attributes which may be stuffed by dir() calls
218 # and poor coding in third-party modules
218 # and poor coding in third-party modules
219 words = [w for w in words
219 words = [w for w in words
220 if isinstance(w, basestring) and w != "__builtins__"]
220 if isinstance(w, basestring) and w != "__builtins__"]
221 # Build match list to return
221 # Build match list to return
222 n = len(attr)
222 n = len(attr)
223 return ["%s.%s" % (expr, w) for w in words if w[:n] == attr ]
223 return ["%s.%s" % (expr, w) for w in words if w[:n] == attr ]
224
224
225 class IPCompleter(Completer):
225 class IPCompleter(Completer):
226 """Extension of the completer class with IPython-specific features"""
226 """Extension of the completer class with IPython-specific features"""
227
227
228 def __init__(self,shell,namespace=None,global_namespace=None,
228 def __init__(self,shell,namespace=None,global_namespace=None,
229 omit__names=0,alias_table=None):
229 omit__names=0,alias_table=None):
230 """IPCompleter() -> completer
230 """IPCompleter() -> completer
231
231
232 Return a completer object suitable for use by the readline library
232 Return a completer object suitable for use by the readline library
233 via readline.set_completer().
233 via readline.set_completer().
234
234
235 Inputs:
235 Inputs:
236
236
237 - shell: a pointer to the ipython shell itself. This is needed
237 - shell: a pointer to the ipython shell itself. This is needed
238 because this completer knows about magic functions, and those can
238 because this completer knows about magic functions, and those can
239 only be accessed via the ipython instance.
239 only be accessed via the ipython instance.
240
240
241 - namespace: an optional dict where completions are performed.
241 - namespace: an optional dict where completions are performed.
242
242
243 - global_namespace: secondary optional dict for completions, to
243 - global_namespace: secondary optional dict for completions, to
244 handle cases (such as IPython embedded inside functions) where
244 handle cases (such as IPython embedded inside functions) where
245 both Python scopes are visible.
245 both Python scopes are visible.
246
246
247 - The optional omit__names parameter sets the completer to omit the
247 - The optional omit__names parameter sets the completer to omit the
248 'magic' names (__magicname__) for python objects unless the text
248 'magic' names (__magicname__) for python objects unless the text
249 to be completed explicitly starts with one or more underscores.
249 to be completed explicitly starts with one or more underscores.
250
250
251 - If alias_table is supplied, it should be a dictionary of aliases
251 - If alias_table is supplied, it should be a dictionary of aliases
252 to complete. """
252 to complete. """
253
253
254 Completer.__init__(self,namespace,global_namespace)
254 Completer.__init__(self,namespace,global_namespace)
255 self.magic_prefix = shell.name+'.magic_'
255 self.magic_prefix = shell.name+'.magic_'
256 self.magic_escape = shell.ESC_MAGIC
256 self.magic_escape = shell.ESC_MAGIC
257 self.readline = readline
257 self.readline = readline
258 delims = self.readline.get_completer_delims()
258 delims = self.readline.get_completer_delims()
259 delims = delims.replace(self.magic_escape,'')
259 delims = delims.replace(self.magic_escape,'')
260 self.readline.set_completer_delims(delims)
260 self.readline.set_completer_delims(delims)
261 self.get_line_buffer = self.readline.get_line_buffer
261 self.get_line_buffer = self.readline.get_line_buffer
262 self.omit__names = omit__names
262 self.omit__names = omit__names
263 self.merge_completions = shell.rc.readline_merge_completions
263 self.merge_completions = shell.rc.readline_merge_completions
264
264
265 if alias_table is None:
265 if alias_table is None:
266 alias_table = {}
266 alias_table = {}
267 self.alias_table = alias_table
267 self.alias_table = alias_table
268 # Regexp to split filenames with spaces in them
268 # Regexp to split filenames with spaces in them
269 self.space_name_re = re.compile(r'([^\\] )')
269 self.space_name_re = re.compile(r'([^\\] )')
270 # Hold a local ref. to glob.glob for speed
270 # Hold a local ref. to glob.glob for speed
271 self.glob = glob.glob
271 self.glob = glob.glob
272
272
273 # Determine if we are running on 'dumb' terminals, like (X)Emacs
273 # Determine if we are running on 'dumb' terminals, like (X)Emacs
274 # buffers, to avoid completion problems.
274 # buffers, to avoid completion problems.
275 term = os.environ.get('TERM','xterm')
275 term = os.environ.get('TERM','xterm')
276 self.dumb_terminal = term in ['dumb','emacs']
276 self.dumb_terminal = term in ['dumb','emacs']
277
277
278 # Special handling of backslashes needed in win32 platforms
278 # Special handling of backslashes needed in win32 platforms
279 if sys.platform == "win32":
279 if sys.platform == "win32":
280 self.clean_glob = self._clean_glob_win32
280 self.clean_glob = self._clean_glob_win32
281 else:
281 else:
282 self.clean_glob = self._clean_glob
282 self.clean_glob = self._clean_glob
283 self.matchers = [self.python_matches,
283 self.matchers = [self.python_matches,
284 self.file_matches,
284 self.file_matches,
285 self.alias_matches,
285 self.alias_matches,
286 self.python_func_kw_matches]
286 self.python_func_kw_matches]
287
287
288 # Code contributed by Alex Schmolck, for ipython/emacs integration
288 # Code contributed by Alex Schmolck, for ipython/emacs integration
289 def all_completions(self, text):
289 def all_completions(self, text):
290 """Return all possible completions for the benefit of emacs."""
290 """Return all possible completions for the benefit of emacs."""
291
291
292 completions = []
292 completions = []
293 comp_append = completions.append
293 comp_append = completions.append
294 try:
294 try:
295 for i in xrange(sys.maxint):
295 for i in xrange(sys.maxint):
296 res = self.complete(text, i)
296 res = self.complete(text, i)
297
297
298 if not res: break
298 if not res: break
299
299
300 comp_append(res)
300 comp_append(res)
301 #XXX workaround for ``notDefined.<tab>``
301 #XXX workaround for ``notDefined.<tab>``
302 except NameError:
302 except NameError:
303 pass
303 pass
304 return completions
304 return completions
305 # /end Alex Schmolck code.
305 # /end Alex Schmolck code.
306
306
307 def _clean_glob(self,text):
307 def _clean_glob(self,text):
308 return self.glob("%s*" % text)
308 return self.glob("%s*" % text)
309
309
310 def _clean_glob_win32(self,text):
310 def _clean_glob_win32(self,text):
311 return [f.replace("\\","/")
311 return [f.replace("\\","/")
312 for f in self.glob("%s*" % text)]
312 for f in self.glob("%s*" % text)]
313
313
314 def file_matches(self, text):
314 def file_matches(self, text):
315 """Match filneames, expanding ~USER type strings.
315 """Match filneames, expanding ~USER type strings.
316
316
317 Most of the seemingly convoluted logic in this completer is an
317 Most of the seemingly convoluted logic in this completer is an
318 attempt to handle filenames with spaces in them. And yet it's not
318 attempt to handle filenames with spaces in them. And yet it's not
319 quite perfect, because Python's readline doesn't expose all of the
319 quite perfect, because Python's readline doesn't expose all of the
320 GNU readline details needed for this to be done correctly.
320 GNU readline details needed for this to be done correctly.
321
321
322 For a filename with a space in it, the printed completions will be
322 For a filename with a space in it, the printed completions will be
323 only the parts after what's already been typed (instead of the
323 only the parts after what's already been typed (instead of the
324 full completions, as is normally done). I don't think with the
324 full completions, as is normally done). I don't think with the
325 current (as of Python 2.3) Python readline it's possible to do
325 current (as of Python 2.3) Python readline it's possible to do
326 better."""
326 better."""
327
327
328 #print 'Completer->file_matches: <%s>' % text # dbg
328 #print 'Completer->file_matches: <%s>' % text # dbg
329
329
330 # chars that require escaping with backslash - i.e. chars
330 # chars that require escaping with backslash - i.e. chars
331 # that readline treats incorrectly as delimiters, but we
331 # that readline treats incorrectly as delimiters, but we
332 # don't want to treat as delimiters in filename matching
332 # don't want to treat as delimiters in filename matching
333 # when escaped with backslash
333 # when escaped with backslash
334
334
335 protectables = ' ()[]{}'
335 protectables = ' ()[]{}'
336
336
337 def protect_filename(s):
337 def protect_filename(s):
338 return "".join([(ch in protectables and '\\' + ch or ch)
338 return "".join([(ch in protectables and '\\' + ch or ch)
339 for ch in s])
339 for ch in s])
340
340
341 lbuf = self.get_line_buffer()[:self.readline.get_endidx()]
341 lbuf = self.get_line_buffer()[:self.readline.get_endidx()]
342 open_quotes = 0 # track strings with open quotes
342 open_quotes = 0 # track strings with open quotes
343 try:
343 try:
344 lsplit = shlex_split(lbuf)[-1]
344 lsplit = shlex_split(lbuf)[-1]
345 except ValueError:
345 except ValueError:
346 # typically an unmatched ", or backslash without escaped char.
346 # typically an unmatched ", or backslash without escaped char.
347 if lbuf.count('"')==1:
347 if lbuf.count('"')==1:
348 open_quotes = 1
348 open_quotes = 1
349 lsplit = lbuf.split('"')[-1]
349 lsplit = lbuf.split('"')[-1]
350 elif lbuf.count("'")==1:
350 elif lbuf.count("'")==1:
351 open_quotes = 1
351 open_quotes = 1
352 lsplit = lbuf.split("'")[-1]
352 lsplit = lbuf.split("'")[-1]
353 else:
353 else:
354 return None
354 return None
355 except IndexError:
355 except IndexError:
356 # tab pressed on empty line
356 # tab pressed on empty line
357 lsplit = ""
357 lsplit = ""
358
358
359 if lsplit != protect_filename(lsplit):
359 if lsplit != protect_filename(lsplit):
360 # if protectables are found, do matching on the whole escaped
360 # if protectables are found, do matching on the whole escaped
361 # name
361 # name
362 has_protectables = 1
362 has_protectables = 1
363 text0,text = text,lsplit
363 text0,text = text,lsplit
364 else:
364 else:
365 has_protectables = 0
365 has_protectables = 0
366 text = os.path.expanduser(text)
366 text = os.path.expanduser(text)
367
367
368 if text == "":
368 if text == "":
369 return [protect_filename(f) for f in self.glob("*")]
369 return [protect_filename(f) for f in self.glob("*")]
370
370
371 m0 = self.clean_glob(text.replace('\\',''))
371 m0 = self.clean_glob(text.replace('\\',''))
372 if has_protectables:
372 if has_protectables:
373 # If we had protectables, we need to revert our changes to the
373 # If we had protectables, we need to revert our changes to the
374 # beginning of filename so that we don't double-write the part
374 # beginning of filename so that we don't double-write the part
375 # of the filename we have so far
375 # of the filename we have so far
376 len_lsplit = len(lsplit)
376 len_lsplit = len(lsplit)
377 matches = [text0 + protect_filename(f[len_lsplit:]) for f in m0]
377 matches = [text0 + protect_filename(f[len_lsplit:]) for f in m0]
378 else:
378 else:
379 if open_quotes:
379 if open_quotes:
380 # if we have a string with an open quote, we don't need to
380 # if we have a string with an open quote, we don't need to
381 # protect the names at all (and we _shouldn't_, as it
381 # protect the names at all (and we _shouldn't_, as it
382 # would cause bugs when the filesystem call is made).
382 # would cause bugs when the filesystem call is made).
383 matches = m0
383 matches = m0
384 else:
384 else:
385 matches = [protect_filename(f) for f in m0]
385 matches = [protect_filename(f) for f in m0]
386 if len(matches) == 1 and os.path.isdir(matches[0]):
386 if len(matches) == 1 and os.path.isdir(matches[0]):
387 # Takes care of links to directories also. Use '/'
387 # Takes care of links to directories also. Use '/'
388 # explicitly, even under Windows, so that name completions
388 # explicitly, even under Windows, so that name completions
389 # don't end up escaped.
389 # don't end up escaped.
390 matches[0] += '/'
390 matches[0] += '/'
391 return matches
391 return matches
392
392
393 def alias_matches(self, text):
393 def alias_matches(self, text):
394 """Match internal system aliases"""
394 """Match internal system aliases"""
395 #print 'Completer->alias_matches:',text # dbg
395 #print 'Completer->alias_matches:',text # dbg
396 text = os.path.expanduser(text)
396 text = os.path.expanduser(text)
397 aliases = self.alias_table.keys()
397 aliases = self.alias_table.keys()
398 if text == "":
398 if text == "":
399 return aliases
399 return aliases
400 else:
400 else:
401 return [alias for alias in aliases if alias.startswith(text)]
401 return [alias for alias in aliases if alias.startswith(text)]
402
402
403 def python_matches(self,text):
403 def python_matches(self,text):
404 """Match attributes or global python names"""
404 """Match attributes or global python names"""
405 #print 'Completer->python_matches' # dbg
405 #print 'Completer->python_matches' # dbg
406 if "." in text:
406 if "." in text:
407 try:
407 try:
408 matches = self.attr_matches(text)
408 matches = self.attr_matches(text)
409 if text.endswith('.') and self.omit__names:
409 if text.endswith('.') and self.omit__names:
410 if self.omit__names == 1:
410 if self.omit__names == 1:
411 # true if txt is _not_ a __ name, false otherwise:
411 # true if txt is _not_ a __ name, false otherwise:
412 no__name = (lambda txt:
412 no__name = (lambda txt:
413 re.match(r'.*\.__.*?__',txt) is None)
413 re.match(r'.*\.__.*?__',txt) is None)
414 else:
414 else:
415 # true if txt is _not_ a _ name, false otherwise:
415 # true if txt is _not_ a _ name, false otherwise:
416 no__name = (lambda txt:
416 no__name = (lambda txt:
417 re.match(r'.*\._.*?',txt) is None)
417 re.match(r'.*\._.*?',txt) is None)
418 matches = filter(no__name, matches)
418 matches = filter(no__name, matches)
419 except NameError:
419 except NameError:
420 # catches <undefined attributes>.<tab>
420 # catches <undefined attributes>.<tab>
421 matches = []
421 matches = []
422 else:
422 else:
423 matches = self.global_matches(text)
423 matches = self.global_matches(text)
424 # this is so completion finds magics when automagic is on:
424 # this is so completion finds magics when automagic is on:
425 if matches == [] and not text.startswith(os.sep):
425 if matches == [] and not text.startswith(os.sep):
426 matches = self.attr_matches(self.magic_prefix+text)
426 matches = self.attr_matches(self.magic_prefix+text)
427 return matches
427 return matches
428
428
429 def _default_arguments(self, obj):
429 def _default_arguments(self, obj):
430 """Return the list of default arguments of obj if it is callable,
430 """Return the list of default arguments of obj if it is callable,
431 or empty list otherwise."""
431 or empty list otherwise."""
432
432
433 if not (inspect.isfunction(obj) or inspect.ismethod(obj)):
433 if not (inspect.isfunction(obj) or inspect.ismethod(obj)):
434 # for classes, check for __init__,__new__
434 # for classes, check for __init__,__new__
435 if inspect.isclass(obj):
435 if inspect.isclass(obj):
436 obj = (getattr(obj,'__init__',None) or
436 obj = (getattr(obj,'__init__',None) or
437 getattr(obj,'__new__',None))
437 getattr(obj,'__new__',None))
438 # for all others, check if they are __call__able
438 # for all others, check if they are __call__able
439 elif hasattr(obj, '__call__'):
439 elif hasattr(obj, '__call__'):
440 obj = obj.__call__
440 obj = obj.__call__
441 # XXX: is there a way to handle the builtins ?
441 # XXX: is there a way to handle the builtins ?
442 try:
442 try:
443 args,_,_1,defaults = inspect.getargspec(obj)
443 args,_,_1,defaults = inspect.getargspec(obj)
444 if defaults:
444 if defaults:
445 return args[-len(defaults):]
445 return args[-len(defaults):]
446 except TypeError: pass
446 except TypeError: pass
447 return []
447 return []
448
448
449 def python_func_kw_matches(self,text):
449 def python_func_kw_matches(self,text):
450 """Match named parameters (kwargs) of the last open function"""
450 """Match named parameters (kwargs) of the last open function"""
451
451
452 if "." in text: # a parameter cannot be dotted
452 if "." in text: # a parameter cannot be dotted
453 return []
453 return []
454 try: regexp = self.__funcParamsRegex
454 try: regexp = self.__funcParamsRegex
455 except AttributeError:
455 except AttributeError:
456 regexp = self.__funcParamsRegex = re.compile(r'''
456 regexp = self.__funcParamsRegex = re.compile(r'''
457 '.*?' | # single quoted strings or
457 '.*?' | # single quoted strings or
458 ".*?" | # double quoted strings or
458 ".*?" | # double quoted strings or
459 \w+ | # identifier
459 \w+ | # identifier
460 \S # other characters
460 \S # other characters
461 ''', re.VERBOSE | re.DOTALL)
461 ''', re.VERBOSE | re.DOTALL)
462 # 1. find the nearest identifier that comes before an unclosed
462 # 1. find the nearest identifier that comes before an unclosed
463 # parenthesis e.g. for "foo (1+bar(x), pa", the candidate is "foo"
463 # parenthesis e.g. for "foo (1+bar(x), pa", the candidate is "foo"
464 tokens = regexp.findall(self.get_line_buffer())
464 tokens = regexp.findall(self.get_line_buffer())
465 tokens.reverse()
465 tokens.reverse()
466 iterTokens = iter(tokens); openPar = 0
466 iterTokens = iter(tokens); openPar = 0
467 for token in iterTokens:
467 for token in iterTokens:
468 if token == ')':
468 if token == ')':
469 openPar -= 1
469 openPar -= 1
470 elif token == '(':
470 elif token == '(':
471 openPar += 1
471 openPar += 1
472 if openPar > 0:
472 if openPar > 0:
473 # found the last unclosed parenthesis
473 # found the last unclosed parenthesis
474 break
474 break
475 else:
475 else:
476 return []
476 return []
477 # 2. Concatenate dotted names ("foo.bar" for "foo.bar(x, pa" )
477 # 2. Concatenate dotted names ("foo.bar" for "foo.bar(x, pa" )
478 ids = []
478 ids = []
479 isId = re.compile(r'\w+$').match
479 isId = re.compile(r'\w+$').match
480 while True:
480 while True:
481 try:
481 try:
482 ids.append(iterTokens.next())
482 ids.append(iterTokens.next())
483 if not isId(ids[-1]):
483 if not isId(ids[-1]):
484 ids.pop(); break
484 ids.pop(); break
485 if not iterTokens.next() == '.':
485 if not iterTokens.next() == '.':
486 break
486 break
487 except StopIteration:
487 except StopIteration:
488 break
488 break
489 # lookup the candidate callable matches either using global_matches
489 # lookup the candidate callable matches either using global_matches
490 # or attr_matches for dotted names
490 # or attr_matches for dotted names
491 if len(ids) == 1:
491 if len(ids) == 1:
492 callableMatches = self.global_matches(ids[0])
492 callableMatches = self.global_matches(ids[0])
493 else:
493 else:
494 callableMatches = self.attr_matches('.'.join(ids[::-1]))
494 callableMatches = self.attr_matches('.'.join(ids[::-1]))
495 argMatches = []
495 argMatches = []
496 for callableMatch in callableMatches:
496 for callableMatch in callableMatches:
497 try: namedArgs = self._default_arguments(eval(callableMatch,
497 try: namedArgs = self._default_arguments(eval(callableMatch,
498 self.namespace))
498 self.namespace))
499 except: continue
499 except: continue
500 for namedArg in namedArgs:
500 for namedArg in namedArgs:
501 if namedArg.startswith(text):
501 if namedArg.startswith(text):
502 argMatches.append("%s=" %namedArg)
502 argMatches.append("%s=" %namedArg)
503 return argMatches
503 return argMatches
504
504
505 def complete(self, text, state):
505 def complete(self, text, state):
506 """Return the next possible completion for 'text'.
506 """Return the next possible completion for 'text'.
507
507
508 This is called successively with state == 0, 1, 2, ... until it
508 This is called successively with state == 0, 1, 2, ... until it
509 returns None. The completion should begin with 'text'. """
509 returns None. The completion should begin with 'text'. """
510
510
511 #print '\n*** COMPLETE: <%s> (%s)' % (text,state) # dbg
511 #print '\n*** COMPLETE: <%s> (%s)' % (text,state) # dbg
512
512
513 # if there is only a tab on a line with only whitespace, instead
513 # if there is only a tab on a line with only whitespace, instead
514 # of the mostly useless 'do you want to see all million
514 # of the mostly useless 'do you want to see all million
515 # completions' message, just do the right thing and give the user
515 # completions' message, just do the right thing and give the user
516 # his tab! Incidentally, this enables pasting of tabbed text from
516 # his tab! Incidentally, this enables pasting of tabbed text from
517 # an editor (as long as autoindent is off).
517 # an editor (as long as autoindent is off).
518
518
519 # don't apply this on 'dumb' terminals, such as emacs buffers, so we
519 # don't apply this on 'dumb' terminals, such as emacs buffers, so we
520 # don't interfere with their own tab-completion mechanism.
520 # don't interfere with their own tab-completion mechanism.
521 if not (self.dumb_terminal or self.get_line_buffer().strip()):
521 if not (self.dumb_terminal or self.get_line_buffer().strip()):
522 self.readline.insert_text('\t')
522 self.readline.insert_text('\t')
523 return None
523 return None
524
524
525 magic_escape = self.magic_escape
525 magic_escape = self.magic_escape
526 magic_prefix = self.magic_prefix
526 magic_prefix = self.magic_prefix
527
527
528 try:
528 try:
529 if text.startswith(magic_escape):
529 if text.startswith(magic_escape):
530 text = text.replace(magic_escape,magic_prefix)
530 text = text.replace(magic_escape,magic_prefix)
531 elif text.startswith('~'):
531 elif text.startswith('~'):
532 text = os.path.expanduser(text)
532 text = os.path.expanduser(text)
533 if state == 0:
533 if state == 0:
534 # Extend the list of completions with the results of each
534 # Extend the list of completions with the results of each
535 # matcher, so we return results to the user from all
535 # matcher, so we return results to the user from all
536 # namespaces.
536 # namespaces.
537 if self.merge_completions:
537 if self.merge_completions:
538 self.matches = []
538 self.matches = []
539 for matcher in self.matchers:
539 for matcher in self.matchers:
540 self.matches.extend(matcher(text))
540 self.matches.extend(matcher(text))
541 else:
541 else:
542 for matcher in self.matchers:
542 for matcher in self.matchers:
543 self.matches = matcher(text)
543 self.matches = matcher(text)
544 if self.matches:
544 if self.matches:
545 break
545 break
546
546
547 try:
547 try:
548 return self.matches[state].replace(magic_prefix,magic_escape)
548 return self.matches[state].replace(magic_prefix,magic_escape)
549 except IndexError:
549 except IndexError:
550 return None
550 return None
551 except:
551 except:
552 #from IPython.ultraTB import AutoFormattedTB; # dbg
552 #from IPython.ultraTB import AutoFormattedTB; # dbg
553 #tb=AutoFormattedTB('Verbose');tb() #dbg
553 #tb=AutoFormattedTB('Verbose');tb() #dbg
554
554
555 # If completion fails, don't annoy the user.
555 # If completion fails, don't annoy the user.
556 return None
556 return None
@@ -1,309 +1,309 b''
1 """Module for interactive demos using IPython.
1 """Module for interactive demos using IPython.
2
2
3 This module implements a single class, Demo, for running Python scripts
3 This module implements a single class, Demo, for running Python scripts
4 interactively in IPython for demonstrations. With very simple markup (a few
4 interactively in IPython for demonstrations. With very simple markup (a few
5 tags in comments), you can control points where the script stops executing and
5 tags in comments), you can control points where the script stops executing and
6 returns control to IPython.
6 returns control to IPython.
7
7
8 The file is run in its own empty namespace (though you can pass it a string of
8 The file is run in its own empty namespace (though you can pass it a string of
9 arguments as if in a command line environment, and it will see those as
9 arguments as if in a command line environment, and it will see those as
10 sys.argv). But at each stop, the global IPython namespace is updated with the
10 sys.argv). But at each stop, the global IPython namespace is updated with the
11 current internal demo namespace, so you can work interactively with the data
11 current internal demo namespace, so you can work interactively with the data
12 accumulated so far.
12 accumulated so far.
13
13
14 By default, each block of code is printed (with syntax highlighting) before
14 By default, each block of code is printed (with syntax highlighting) before
15 executing it and you have to confirm execution. This is intended to show the
15 executing it and you have to confirm execution. This is intended to show the
16 code to an audience first so you can discuss it, and only proceed with
16 code to an audience first so you can discuss it, and only proceed with
17 execution once you agree. There are a few tags which allow you to modify this
17 execution once you agree. There are a few tags which allow you to modify this
18 behavior.
18 behavior.
19
19
20 The supported tags are:
20 The supported tags are:
21
21
22 # <demo> --- stop ---
22 # <demo> --- stop ---
23
23
24 Defines block boundaries, the points where IPython stops execution of the
24 Defines block boundaries, the points where IPython stops execution of the
25 file and returns to the interactive prompt.
25 file and returns to the interactive prompt.
26
26
27 # <demo> silent
27 # <demo> silent
28
28
29 Make a block execute silently (and hence automatically). Typically used in
29 Make a block execute silently (and hence automatically). Typically used in
30 cases where you have some boilerplate or initialization code which you need
30 cases where you have some boilerplate or initialization code which you need
31 executed but do not want to be seen in the demo.
31 executed but do not want to be seen in the demo.
32
32
33 # <demo> auto
33 # <demo> auto
34
34
35 Make a block execute automatically, but still being printed. Useful for
35 Make a block execute automatically, but still being printed. Useful for
36 simple code which does not warrant discussion, since it avoids the extra
36 simple code which does not warrant discussion, since it avoids the extra
37 manual confirmation.
37 manual confirmation.
38
38
39 # <demo> auto_all
39 # <demo> auto_all
40
40
41 This tag can _only_ be in the first block, and if given it overrides the
41 This tag can _only_ be in the first block, and if given it overrides the
42 individual auto tags to make the whole demo fully automatic (no block asks
42 individual auto tags to make the whole demo fully automatic (no block asks
43 for confirmation). It can also be given at creation time (or the attribute
43 for confirmation). It can also be given at creation time (or the attribute
44 set later) to override what's in the file.
44 set later) to override what's in the file.
45
45
46 While _any_ python file can be run as a Demo instance, if there are no stop
46 While _any_ python file can be run as a Demo instance, if there are no stop
47 tags the whole file will run in a single block (no different that calling
47 tags the whole file will run in a single block (no different that calling
48 first %pycat and then %run). The minimal markup to make this useful is to
48 first %pycat and then %run). The minimal markup to make this useful is to
49 place a set of stop tags; the other tags are only there to let you fine-tune
49 place a set of stop tags; the other tags are only there to let you fine-tune
50 the execution.
50 the execution.
51
51
52 This is probably best explained with the simple example file below. You can
52 This is probably best explained with the simple example file below. You can
53 copy this into a file named ex_demo.py, and try running it via:
53 copy this into a file named ex_demo.py, and try running it via:
54
54
55 from IPython.demo import Demo
55 from IPython.demo import Demo
56 d = Demo('ex_demo.py')
56 d = Demo('ex_demo.py')
57 d() <--- Call the d object (omit the parens if you have autocall on).
57 d() <--- Call the d object (omit the parens if you have autocall on).
58
58
59 Each time you call the demo object, it runs the next block. The demo object
59 Each time you call the demo object, it runs the next block. The demo object
60 has a few useful methods for navigation, like again(), jump(), seek() and
60 has a few useful methods for navigation, like again(), jump(), seek() and
61 back(). It can be reset for a new run via reset() or reloaded from disk (in
61 back(). It can be reset for a new run via reset() or reloaded from disk (in
62 case you've edited the source) via reload(). See their docstrings below.
62 case you've edited the source) via reload(). See their docstrings below.
63
63
64 #################### EXAMPLE DEMO <ex_demo.py> ###############################
64 #################### EXAMPLE DEMO <ex_demo.py> ###############################
65 '''A simple interactive demo to illustrate the use of IPython's Demo class.'''
65 '''A simple interactive demo to illustrate the use of IPython's Demo class.'''
66
66
67 print 'Hello, welcome to an interactive IPython demo.'
67 print 'Hello, welcome to an interactive IPython demo.'
68
68
69 # The mark below defines a block boundary, which is a point where IPython will
69 # The mark below defines a block boundary, which is a point where IPython will
70 # stop execution and return to the interactive prompt.
70 # stop execution and return to the interactive prompt.
71 # Note that in actual interactive execution,
71 # Note that in actual interactive execution,
72 # <demo> --- stop ---
72 # <demo> --- stop ---
73
73
74 x = 1
74 x = 1
75 y = 2
75 y = 2
76
76
77 # <demo> --- stop ---
77 # <demo> --- stop ---
78
78
79 # the mark below makes this block as silent
79 # the mark below makes this block as silent
80 # <demo> silent
80 # <demo> silent
81
81
82 print 'This is a silent block, which gets executed but not printed.'
82 print 'This is a silent block, which gets executed but not printed.'
83
83
84 # <demo> --- stop ---
84 # <demo> --- stop ---
85 # <demo> auto
85 # <demo> auto
86 print 'This is an automatic block.'
86 print 'This is an automatic block.'
87 print 'It is executed without asking for confirmation, but printed.'
87 print 'It is executed without asking for confirmation, but printed.'
88 z = x+y
88 z = x+y
89
89
90 print 'z=',x
90 print 'z=',x
91
91
92 # <demo> --- stop ---
92 # <demo> --- stop ---
93 # This is just another normal block.
93 # This is just another normal block.
94 print 'z is now:', z
94 print 'z is now:', z
95
95
96 print 'bye!'
96 print 'bye!'
97 ################### END EXAMPLE DEMO <ex_demo.py> ############################
97 ################### END EXAMPLE DEMO <ex_demo.py> ############################
98
98
99 WARNING: this module uses Python 2.3 features, so it won't work in 2.2
99 WARNING: this module uses Python 2.3 features, so it won't work in 2.2
100 environments.
100 environments.
101 """
101 """
102 #*****************************************************************************
102 #*****************************************************************************
103 # Copyright (C) 2005 Fernando Perez. <Fernando.Perez@colorado.edu>
103 # Copyright (C) 2005-2006 Fernando Perez. <Fernando.Perez@colorado.edu>
104 #
104 #
105 # Distributed under the terms of the BSD License. The full license is in
105 # Distributed under the terms of the BSD License. The full license is in
106 # the file COPYING, distributed as part of this software.
106 # the file COPYING, distributed as part of this software.
107 #
107 #
108 #*****************************************************************************
108 #*****************************************************************************
109
109
110 import exceptions
110 import exceptions
111 import re
111 import re
112 import sys
112 import sys
113
113
114 from IPython.PyColorize import Parser
114 from IPython.PyColorize import Parser
115 from IPython.genutils import marquee, shlex_split, file_read
115 from IPython.genutils import marquee, shlex_split, file_read
116
116
117 __all__ = ['Demo','DemoError']
117 __all__ = ['Demo','DemoError']
118
118
119 class DemoError(exceptions.Exception): pass
119 class DemoError(exceptions.Exception): pass
120
120
121 def re_mark(mark):
121 def re_mark(mark):
122 return re.compile(r'^\s*#\s+<demo>\s+%s\s*$' % mark,re.MULTILINE)
122 return re.compile(r'^\s*#\s+<demo>\s+%s\s*$' % mark,re.MULTILINE)
123
123
124 class Demo:
124 class Demo:
125
125
126 re_stop = re_mark('---\s?stop\s?---')
126 re_stop = re_mark('---\s?stop\s?---')
127 re_silent = re_mark('silent')
127 re_silent = re_mark('silent')
128 re_auto = re_mark('auto')
128 re_auto = re_mark('auto')
129 re_auto_all = re_mark('auto_all')
129 re_auto_all = re_mark('auto_all')
130
130
131 def __init__(self,fname,arg_str='',auto_all=None):
131 def __init__(self,fname,arg_str='',auto_all=None):
132 """Make a new demo object. To run the demo, simply call the object.
132 """Make a new demo object. To run the demo, simply call the object.
133
133
134 See the module docstring for full details and an example (you can use
134 See the module docstring for full details and an example (you can use
135 IPython.Demo? in IPython to see it).
135 IPython.Demo? in IPython to see it).
136
136
137 Inputs:
137 Inputs:
138
138
139 - fname = filename.
139 - fname = filename.
140
140
141 Optional inputs:
141 Optional inputs:
142
142
143 - arg_str(''): a string of arguments, internally converted to a list
143 - arg_str(''): a string of arguments, internally converted to a list
144 just like sys.argv, so the demo script can see a similar
144 just like sys.argv, so the demo script can see a similar
145 environment.
145 environment.
146
146
147 - auto_all(None): global flag to run all blocks automatically without
147 - auto_all(None): global flag to run all blocks automatically without
148 confirmation. This attribute overrides the block-level tags and
148 confirmation. This attribute overrides the block-level tags and
149 applies to the whole demo. It is an attribute of the object, and
149 applies to the whole demo. It is an attribute of the object, and
150 can be changed at runtime simply by reassigning it to a boolean
150 can be changed at runtime simply by reassigning it to a boolean
151 value.
151 value.
152 """
152 """
153
153
154 self.fname = fname
154 self.fname = fname
155 self.sys_argv = [fname] + shlex_split(arg_str)
155 self.sys_argv = [fname] + shlex_split(arg_str)
156 self.auto_all = auto_all
156 self.auto_all = auto_all
157
157
158 # get a few things from ipython. While it's a bit ugly design-wise,
158 # get a few things from ipython. While it's a bit ugly design-wise,
159 # it ensures that things like color scheme and the like are always in
159 # it ensures that things like color scheme and the like are always in
160 # sync with the ipython mode being used. This class is only meant to
160 # sync with the ipython mode being used. This class is only meant to
161 # be used inside ipython anyways, so it's OK.
161 # be used inside ipython anyways, so it's OK.
162 self.ip_showtb = __IPYTHON__.showtraceback
162 self.ip_showtb = __IPYTHON__.showtraceback
163 self.ip_ns = __IPYTHON__.user_ns
163 self.ip_ns = __IPYTHON__.user_ns
164 self.ip_colorize = __IPYTHON__.pycolorize
164 self.ip_colorize = __IPYTHON__.pycolorize
165
165
166 # load user data and initialize data structures
166 # load user data and initialize data structures
167 self.reload()
167 self.reload()
168
168
169 def reload(self):
169 def reload(self):
170 """Reload source from disk and initialize state."""
170 """Reload source from disk and initialize state."""
171 # read data and parse into blocks
171 # read data and parse into blocks
172 self.src = file_read(self.fname)
172 self.src = file_read(self.fname)
173 src_b = [b.strip() for b in self.re_stop.split(self.src) if b]
173 src_b = [b.strip() for b in self.re_stop.split(self.src) if b]
174 self._silent = [bool(self.re_silent.findall(b)) for b in src_b]
174 self._silent = [bool(self.re_silent.findall(b)) for b in src_b]
175 self._auto = [bool(self.re_auto.findall(b)) for b in src_b]
175 self._auto = [bool(self.re_auto.findall(b)) for b in src_b]
176
176
177 # if auto_all is not given (def. None), we read it from the file
177 # if auto_all is not given (def. None), we read it from the file
178 if self.auto_all is None:
178 if self.auto_all is None:
179 self.auto_all = bool(self.re_auto_all.findall(src_b[0]))
179 self.auto_all = bool(self.re_auto_all.findall(src_b[0]))
180 else:
180 else:
181 self.auto_all = bool(self.auto_all)
181 self.auto_all = bool(self.auto_all)
182
182
183 # Clean the sources from all markup so it doesn't get displayed when
183 # Clean the sources from all markup so it doesn't get displayed when
184 # running the demo
184 # running the demo
185 src_blocks = []
185 src_blocks = []
186 auto_strip = lambda s: self.re_auto.sub('',s)
186 auto_strip = lambda s: self.re_auto.sub('',s)
187 for i,b in enumerate(src_b):
187 for i,b in enumerate(src_b):
188 if self._auto[i]:
188 if self._auto[i]:
189 src_blocks.append(auto_strip(b))
189 src_blocks.append(auto_strip(b))
190 else:
190 else:
191 src_blocks.append(b)
191 src_blocks.append(b)
192 # remove the auto_all marker
192 # remove the auto_all marker
193 src_blocks[0] = self.re_auto_all.sub('',src_blocks[0])
193 src_blocks[0] = self.re_auto_all.sub('',src_blocks[0])
194
194
195 self.nblocks = len(src_blocks)
195 self.nblocks = len(src_blocks)
196 self.src_blocks = src_blocks
196 self.src_blocks = src_blocks
197
197
198 # also build syntax-highlighted source
198 # also build syntax-highlighted source
199 self.src_blocks_colored = map(self.ip_colorize,self.src_blocks)
199 self.src_blocks_colored = map(self.ip_colorize,self.src_blocks)
200
200
201 # ensure clean namespace and seek offset
201 # ensure clean namespace and seek offset
202 self.reset()
202 self.reset()
203
203
204 def reset(self):
204 def reset(self):
205 """Reset the namespace and seek pointer to restart the demo"""
205 """Reset the namespace and seek pointer to restart the demo"""
206 self.user_ns = {}
206 self.user_ns = {}
207 self.finished = False
207 self.finished = False
208 self.block_index = 0
208 self.block_index = 0
209
209
210 def _validate_index(self,index):
210 def _validate_index(self,index):
211 if index<0 or index>=self.nblocks:
211 if index<0 or index>=self.nblocks:
212 raise ValueError('invalid block index %s' % index)
212 raise ValueError('invalid block index %s' % index)
213
213
214 def seek(self,index):
214 def seek(self,index):
215 """Move the current seek pointer to the given block"""
215 """Move the current seek pointer to the given block"""
216 self._validate_index(index)
216 self._validate_index(index)
217 self.block_index = index
217 self.block_index = index
218 self.finished = False
218 self.finished = False
219
219
220 def back(self,num=1):
220 def back(self,num=1):
221 """Move the seek pointer back num blocks (default is 1)."""
221 """Move the seek pointer back num blocks (default is 1)."""
222 self.seek(self.block_index-num)
222 self.seek(self.block_index-num)
223
223
224 def jump(self,num):
224 def jump(self,num):
225 """Jump a given number of blocks relative to the current one."""
225 """Jump a given number of blocks relative to the current one."""
226 self.seek(self.block_index+num)
226 self.seek(self.block_index+num)
227
227
228 def again(self):
228 def again(self):
229 """Move the seek pointer back one block and re-execute."""
229 """Move the seek pointer back one block and re-execute."""
230 self.back(1)
230 self.back(1)
231 self()
231 self()
232
232
233 def show(self,index=None):
233 def show(self,index=None):
234 """Show a single block on screen"""
234 """Show a single block on screen"""
235 if index is None:
235 if index is None:
236 if self.finished:
236 if self.finished:
237 print 'Demo finished. Use reset() if you want to rerun it.'
237 print 'Demo finished. Use reset() if you want to rerun it.'
238 return
238 return
239 index = self.block_index
239 index = self.block_index
240 else:
240 else:
241 self._validate_index(index)
241 self._validate_index(index)
242 print marquee('<%s> block # %s (%s remaining)' %
242 print marquee('<%s> block # %s (%s remaining)' %
243 (self.fname,index,self.nblocks-index-1))
243 (self.fname,index,self.nblocks-index-1))
244 print self.src_blocks_colored[index],
244 print self.src_blocks_colored[index],
245
245
246 def show_all(self):
246 def show_all(self):
247 """Show entire demo on screen, block by block"""
247 """Show entire demo on screen, block by block"""
248
248
249 fname = self.fname
249 fname = self.fname
250 nblocks = self.nblocks
250 nblocks = self.nblocks
251 silent = self._silent
251 silent = self._silent
252 for index,block in enumerate(self.src_blocks_colored):
252 for index,block in enumerate(self.src_blocks_colored):
253 if silent[index]:
253 if silent[index]:
254 print marquee('<%s> SILENT block # %s (%s remaining)' %
254 print marquee('<%s> SILENT block # %s (%s remaining)' %
255 (fname,index,nblocks-index-1))
255 (fname,index,nblocks-index-1))
256 else:
256 else:
257 print marquee('<%s> block # %s (%s remaining)' %
257 print marquee('<%s> block # %s (%s remaining)' %
258 (fname,index,nblocks-index-1))
258 (fname,index,nblocks-index-1))
259 print block,
259 print block,
260
260
261 def __call__(self,index=None):
261 def __call__(self,index=None):
262 """run a block of the demo.
262 """run a block of the demo.
263
263
264 If index is given, it should be an integer >=1 and <= nblocks. This
264 If index is given, it should be an integer >=1 and <= nblocks. This
265 means that the calling convention is one off from typical Python
265 means that the calling convention is one off from typical Python
266 lists. The reason for the inconsistency is that the demo always
266 lists. The reason for the inconsistency is that the demo always
267 prints 'Block n/N, and N is the total, so it would be very odd to use
267 prints 'Block n/N, and N is the total, so it would be very odd to use
268 zero-indexing here."""
268 zero-indexing here."""
269
269
270 if index is None and self.finished:
270 if index is None and self.finished:
271 print 'Demo finished. Use reset() if you want to rerun it.'
271 print 'Demo finished. Use reset() if you want to rerun it.'
272 return
272 return
273 if index is None:
273 if index is None:
274 index = self.block_index
274 index = self.block_index
275 self._validate_index(index)
275 self._validate_index(index)
276 try:
276 try:
277 next_block = self.src_blocks[index]
277 next_block = self.src_blocks[index]
278 self.block_index += 1
278 self.block_index += 1
279 if self._silent[index]:
279 if self._silent[index]:
280 print marquee('Executing silent block # %s (%s remaining)' %
280 print marquee('Executing silent block # %s (%s remaining)' %
281 (index,self.nblocks-index-1))
281 (index,self.nblocks-index-1))
282 else:
282 else:
283 self.show(index)
283 self.show(index)
284 if self.auto_all or self._auto[index]:
284 if self.auto_all or self._auto[index]:
285 print marquee('output')
285 print marquee('output')
286 else:
286 else:
287 print marquee('Press <q> to quit, <Enter> to execute...'),
287 print marquee('Press <q> to quit, <Enter> to execute...'),
288 ans = raw_input().strip()
288 ans = raw_input().strip()
289 if ans:
289 if ans:
290 print marquee('Block NOT executed')
290 print marquee('Block NOT executed')
291 return
291 return
292 try:
292 try:
293 save_argv = sys.argv
293 save_argv = sys.argv
294 sys.argv = self.sys_argv
294 sys.argv = self.sys_argv
295 exec next_block in self.user_ns
295 exec next_block in self.user_ns
296 finally:
296 finally:
297 sys.argv = save_argv
297 sys.argv = save_argv
298
298
299 except:
299 except:
300 self.ip_showtb(filename=self.fname)
300 self.ip_showtb(filename=self.fname)
301 else:
301 else:
302 self.ip_ns.update(self.user_ns)
302 self.ip_ns.update(self.user_ns)
303
303
304 if self.block_index == self.nblocks:
304 if self.block_index == self.nblocks:
305 print
305 print
306 print marquee(' END OF DEMO ')
306 print marquee(' END OF DEMO ')
307 print marquee('Use reset() if you want to rerun it.')
307 print marquee('Use reset() if you want to rerun it.')
308 self.finished = True
308 self.finished = True
309
309
@@ -1,109 +1,109 b''
1 # -*- coding: utf-8 -*-
1 # -*- coding: utf-8 -*-
2 """
2 """
3 Color schemes for exception handling code in IPython.
3 Color schemes for exception handling code in IPython.
4
4
5 $Id: Prompts.py 638 2005-07-18 03:01:41Z fperez $"""
5 $Id: Prompts.py 638 2005-07-18 03:01:41Z fperez $"""
6
6
7 #*****************************************************************************
7 #*****************************************************************************
8 # Copyright (C) 2005 Fernando Perez <fperez@colorado.edu>
8 # Copyright (C) 2005-2006 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 from IPython.ColorANSI import ColorSchemeTable, TermColors, ColorScheme
21 from IPython.ColorANSI import ColorSchemeTable, TermColors, ColorScheme
22
22
23 ExceptionColors = ColorSchemeTable()
23 ExceptionColors = ColorSchemeTable()
24
24
25 # Populate it with color schemes
25 # Populate it with color schemes
26 C = TermColors # shorthand and local lookup
26 C = TermColors # shorthand and local lookup
27 ExceptionColors.add_scheme(ColorScheme(
27 ExceptionColors.add_scheme(ColorScheme(
28 'NoColor',
28 'NoColor',
29 # The color to be used for the top line
29 # The color to be used for the top line
30 topline = C.NoColor,
30 topline = C.NoColor,
31
31
32 # The colors to be used in the traceback
32 # The colors to be used in the traceback
33 filename = C.NoColor,
33 filename = C.NoColor,
34 lineno = C.NoColor,
34 lineno = C.NoColor,
35 name = C.NoColor,
35 name = C.NoColor,
36 vName = C.NoColor,
36 vName = C.NoColor,
37 val = C.NoColor,
37 val = C.NoColor,
38 em = C.NoColor,
38 em = C.NoColor,
39
39
40 # Emphasized colors for the last frame of the traceback
40 # Emphasized colors for the last frame of the traceback
41 normalEm = C.NoColor,
41 normalEm = C.NoColor,
42 filenameEm = C.NoColor,
42 filenameEm = C.NoColor,
43 linenoEm = C.NoColor,
43 linenoEm = C.NoColor,
44 nameEm = C.NoColor,
44 nameEm = C.NoColor,
45 valEm = C.NoColor,
45 valEm = C.NoColor,
46
46
47 # Colors for printing the exception
47 # Colors for printing the exception
48 excName = C.NoColor,
48 excName = C.NoColor,
49 line = C.NoColor,
49 line = C.NoColor,
50 caret = C.NoColor,
50 caret = C.NoColor,
51 Normal = C.NoColor
51 Normal = C.NoColor
52 ))
52 ))
53
53
54 # make some schemes as instances so we can copy them for modification easily
54 # make some schemes as instances so we can copy them for modification easily
55 ExceptionColors.add_scheme(ColorScheme(
55 ExceptionColors.add_scheme(ColorScheme(
56 'Linux',
56 'Linux',
57 # The color to be used for the top line
57 # The color to be used for the top line
58 topline = C.LightRed,
58 topline = C.LightRed,
59
59
60 # The colors to be used in the traceback
60 # The colors to be used in the traceback
61 filename = C.Green,
61 filename = C.Green,
62 lineno = C.Green,
62 lineno = C.Green,
63 name = C.Purple,
63 name = C.Purple,
64 vName = C.Cyan,
64 vName = C.Cyan,
65 val = C.Green,
65 val = C.Green,
66 em = C.LightCyan,
66 em = C.LightCyan,
67
67
68 # Emphasized colors for the last frame of the traceback
68 # Emphasized colors for the last frame of the traceback
69 normalEm = C.LightCyan,
69 normalEm = C.LightCyan,
70 filenameEm = C.LightGreen,
70 filenameEm = C.LightGreen,
71 linenoEm = C.LightGreen,
71 linenoEm = C.LightGreen,
72 nameEm = C.LightPurple,
72 nameEm = C.LightPurple,
73 valEm = C.LightBlue,
73 valEm = C.LightBlue,
74
74
75 # Colors for printing the exception
75 # Colors for printing the exception
76 excName = C.LightRed,
76 excName = C.LightRed,
77 line = C.Yellow,
77 line = C.Yellow,
78 caret = C.White,
78 caret = C.White,
79 Normal = C.Normal
79 Normal = C.Normal
80 ))
80 ))
81
81
82 # For light backgrounds, swap dark/light colors
82 # For light backgrounds, swap dark/light colors
83 ExceptionColors.add_scheme(ColorScheme(
83 ExceptionColors.add_scheme(ColorScheme(
84 'LightBG',
84 'LightBG',
85 # The color to be used for the top line
85 # The color to be used for the top line
86 topline = C.Red,
86 topline = C.Red,
87
87
88 # The colors to be used in the traceback
88 # The colors to be used in the traceback
89 filename = C.LightGreen,
89 filename = C.LightGreen,
90 lineno = C.LightGreen,
90 lineno = C.LightGreen,
91 name = C.LightPurple,
91 name = C.LightPurple,
92 vName = C.Cyan,
92 vName = C.Cyan,
93 val = C.LightGreen,
93 val = C.LightGreen,
94 em = C.Cyan,
94 em = C.Cyan,
95
95
96 # Emphasized colors for the last frame of the traceback
96 # Emphasized colors for the last frame of the traceback
97 normalEm = C.Cyan,
97 normalEm = C.Cyan,
98 filenameEm = C.Green,
98 filenameEm = C.Green,
99 linenoEm = C.Green,
99 linenoEm = C.Green,
100 nameEm = C.Purple,
100 nameEm = C.Purple,
101 valEm = C.Blue,
101 valEm = C.Blue,
102
102
103 # Colors for printing the exception
103 # Colors for printing the exception
104 excName = C.Red,
104 excName = C.Red,
105 #line = C.Brown, # brown often is displayed as yellow
105 #line = C.Brown, # brown often is displayed as yellow
106 line = C.Red,
106 line = C.Red,
107 caret = C.Normal,
107 caret = C.Normal,
108 Normal = C.Normal
108 Normal = C.Normal
109 ))
109 ))
@@ -1,1717 +1,1717 b''
1 # -*- coding: utf-8 -*-
1 # -*- coding: utf-8 -*-
2 """
2 """
3 General purpose utilities.
3 General purpose utilities.
4
4
5 This is a grab-bag of stuff I find useful in most programs I write. Some of
5 This is a grab-bag of stuff I find useful in most programs I write. Some of
6 these things are also convenient when working at the command line.
6 these things are also convenient when working at the command line.
7
7
8 $Id: genutils.py 990 2006-01-04 06:59:02Z fperez $"""
8 $Id: genutils.py 994 2006-01-08 08:29:44Z fperez $"""
9
9
10 #*****************************************************************************
10 #*****************************************************************************
11 # Copyright (C) 2001-2004 Fernando Perez. <fperez@colorado.edu>
11 # Copyright (C) 2001-2006 Fernando Perez. <fperez@colorado.edu>
12 #
12 #
13 # Distributed under the terms of the BSD License. The full license is in
13 # Distributed under the terms of the BSD License. The full license is in
14 # the file COPYING, distributed as part of this software.
14 # the file COPYING, distributed as part of this software.
15 #*****************************************************************************
15 #*****************************************************************************
16
16
17 from __future__ import generators # 2.2 compatibility
17 from __future__ import generators # 2.2 compatibility
18
18
19 from IPython import Release
19 from IPython import Release
20 __author__ = '%s <%s>' % Release.authors['Fernando']
20 __author__ = '%s <%s>' % Release.authors['Fernando']
21 __license__ = Release.license
21 __license__ = Release.license
22
22
23 #****************************************************************************
23 #****************************************************************************
24 # required modules from the Python standard library
24 # required modules from the Python standard library
25 import __main__
25 import __main__
26 import commands
26 import commands
27 import os
27 import os
28 import re
28 import re
29 import shlex
29 import shlex
30 import shutil
30 import shutil
31 import sys
31 import sys
32 import tempfile
32 import tempfile
33 import time
33 import time
34 import types
34 import types
35
35
36 # Other IPython utilities
36 # Other IPython utilities
37 from IPython.Itpl import Itpl,itpl,printpl
37 from IPython.Itpl import Itpl,itpl,printpl
38 from IPython import DPyGetOpt
38 from IPython import DPyGetOpt
39
39
40 if os.name == "nt":
40 if os.name == "nt":
41 from IPython.winconsole import get_console_size
41 from IPython.winconsole import get_console_size
42
42
43 # Build objects which appeared in Python 2.3 for 2.2, to make ipython
43 # Build objects which appeared in Python 2.3 for 2.2, to make ipython
44 # 2.2-friendly
44 # 2.2-friendly
45 try:
45 try:
46 basestring
46 basestring
47 except NameError:
47 except NameError:
48 import types
48 import types
49 basestring = (types.StringType, types.UnicodeType)
49 basestring = (types.StringType, types.UnicodeType)
50 True = 1==1
50 True = 1==1
51 False = 1==0
51 False = 1==0
52
52
53 def enumerate(obj):
53 def enumerate(obj):
54 i = -1
54 i = -1
55 for item in obj:
55 for item in obj:
56 i += 1
56 i += 1
57 yield i, item
57 yield i, item
58
58
59 # add these to the builtin namespace, so that all modules find them
59 # add these to the builtin namespace, so that all modules find them
60 import __builtin__
60 import __builtin__
61 __builtin__.basestring = basestring
61 __builtin__.basestring = basestring
62 __builtin__.True = True
62 __builtin__.True = True
63 __builtin__.False = False
63 __builtin__.False = False
64 __builtin__.enumerate = enumerate
64 __builtin__.enumerate = enumerate
65
65
66 # Try to use shlex.split for converting an input string into a sys.argv-type
66 # Try to use shlex.split for converting an input string into a sys.argv-type
67 # list. This appeared in Python 2.3, so here's a quick backport for 2.2.
67 # list. This appeared in Python 2.3, so here's a quick backport for 2.2.
68 try:
68 try:
69 shlex_split = shlex.split
69 shlex_split = shlex.split
70 except AttributeError:
70 except AttributeError:
71 _quotesre = re.compile(r'[\'"](.*)[\'"]')
71 _quotesre = re.compile(r'[\'"](.*)[\'"]')
72 _wordchars = ('abcdfeghijklmnopqrstuvwxyz'
72 _wordchars = ('abcdfeghijklmnopqrstuvwxyz'
73 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_-.~*?'
73 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_-.~*?'
74 'ßàáâãäåæçèéêëìíîïðñòóôõöøùúûüýþÿ'
74 'ßàáâãäåæçèéêëìíîïðñòóôõöøùúûüýþÿ'
75 'ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÜÝÞ%s'
75 'ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÜÝÞ%s'
76 % os.sep)
76 % os.sep)
77
77
78 def shlex_split(s):
78 def shlex_split(s):
79 """Simplified backport to Python 2.2 of shlex.split().
79 """Simplified backport to Python 2.2 of shlex.split().
80
80
81 This is a quick and dirty hack, since the shlex module under 2.2 lacks
81 This is a quick and dirty hack, since the shlex module under 2.2 lacks
82 several of the features needed to really match the functionality of
82 several of the features needed to really match the functionality of
83 shlex.split() in 2.3."""
83 shlex.split() in 2.3."""
84
84
85 lex = shlex.shlex(StringIO(s))
85 lex = shlex.shlex(StringIO(s))
86 # Try to get options, extensions and path separators as characters
86 # Try to get options, extensions and path separators as characters
87 lex.wordchars = _wordchars
87 lex.wordchars = _wordchars
88 lex.commenters = ''
88 lex.commenters = ''
89 # Make a list out of the lexer by hand, since in 2.2 it's not an
89 # Make a list out of the lexer by hand, since in 2.2 it's not an
90 # iterator.
90 # iterator.
91 lout = []
91 lout = []
92 while 1:
92 while 1:
93 token = lex.get_token()
93 token = lex.get_token()
94 if token == '':
94 if token == '':
95 break
95 break
96 # Try to handle quoted tokens correctly
96 # Try to handle quoted tokens correctly
97 quotes = _quotesre.match(token)
97 quotes = _quotesre.match(token)
98 if quotes:
98 if quotes:
99 token = quotes.group(1)
99 token = quotes.group(1)
100 lout.append(token)
100 lout.append(token)
101 return lout
101 return lout
102
102
103 #****************************************************************************
103 #****************************************************************************
104 # Exceptions
104 # Exceptions
105 class Error(Exception):
105 class Error(Exception):
106 """Base class for exceptions in this module."""
106 """Base class for exceptions in this module."""
107 pass
107 pass
108
108
109 #----------------------------------------------------------------------------
109 #----------------------------------------------------------------------------
110 class IOStream:
110 class IOStream:
111 def __init__(self,stream,fallback):
111 def __init__(self,stream,fallback):
112 if not hasattr(stream,'write') or not hasattr(stream,'flush'):
112 if not hasattr(stream,'write') or not hasattr(stream,'flush'):
113 stream = fallback
113 stream = fallback
114 self.stream = stream
114 self.stream = stream
115 self._swrite = stream.write
115 self._swrite = stream.write
116 self.flush = stream.flush
116 self.flush = stream.flush
117
117
118 def write(self,data):
118 def write(self,data):
119 try:
119 try:
120 self._swrite(data)
120 self._swrite(data)
121 except:
121 except:
122 try:
122 try:
123 # print handles some unicode issues which may trip a plain
123 # print handles some unicode issues which may trip a plain
124 # write() call. Attempt to emulate write() by using a
124 # write() call. Attempt to emulate write() by using a
125 # trailing comma
125 # trailing comma
126 print >> self.stream, data,
126 print >> self.stream, data,
127 except:
127 except:
128 # if we get here, something is seriously broken.
128 # if we get here, something is seriously broken.
129 print >> sys.stderr, \
129 print >> sys.stderr, \
130 'ERROR - failed to write data to stream:', stream
130 'ERROR - failed to write data to stream:', stream
131
131
132 class IOTerm:
132 class IOTerm:
133 """ Term holds the file or file-like objects for handling I/O operations.
133 """ Term holds the file or file-like objects for handling I/O operations.
134
134
135 These are normally just sys.stdin, sys.stdout and sys.stderr but for
135 These are normally just sys.stdin, sys.stdout and sys.stderr but for
136 Windows they can can replaced to allow editing the strings before they are
136 Windows they can can replaced to allow editing the strings before they are
137 displayed."""
137 displayed."""
138
138
139 # In the future, having IPython channel all its I/O operations through
139 # In the future, having IPython channel all its I/O operations through
140 # this class will make it easier to embed it into other environments which
140 # this class will make it easier to embed it into other environments which
141 # are not a normal terminal (such as a GUI-based shell)
141 # are not a normal terminal (such as a GUI-based shell)
142 def __init__(self,cin=None,cout=None,cerr=None):
142 def __init__(self,cin=None,cout=None,cerr=None):
143 self.cin = IOStream(cin,sys.stdin)
143 self.cin = IOStream(cin,sys.stdin)
144 self.cout = IOStream(cout,sys.stdout)
144 self.cout = IOStream(cout,sys.stdout)
145 self.cerr = IOStream(cerr,sys.stderr)
145 self.cerr = IOStream(cerr,sys.stderr)
146
146
147 # Global variable to be used for all I/O
147 # Global variable to be used for all I/O
148 Term = IOTerm()
148 Term = IOTerm()
149
149
150 # Windows-specific code to load Gary Bishop's readline and configure it
150 # Windows-specific code to load Gary Bishop's readline and configure it
151 # automatically for the users
151 # automatically for the users
152 # Note: os.name on cygwin returns posix, so this should only pick up 'native'
152 # Note: os.name on cygwin returns posix, so this should only pick up 'native'
153 # windows. Cygwin returns 'cygwin' for sys.platform.
153 # windows. Cygwin returns 'cygwin' for sys.platform.
154 if os.name == 'nt':
154 if os.name == 'nt':
155 try:
155 try:
156 import readline
156 import readline
157 except ImportError:
157 except ImportError:
158 pass
158 pass
159 else:
159 else:
160 try:
160 try:
161 _out = readline.GetOutputFile()
161 _out = readline.GetOutputFile()
162 except AttributeError:
162 except AttributeError:
163 pass
163 pass
164 else:
164 else:
165 # Remake Term to use the readline i/o facilities
165 # Remake Term to use the readline i/o facilities
166 Term = IOTerm(cout=_out,cerr=_out)
166 Term = IOTerm(cout=_out,cerr=_out)
167 del _out
167 del _out
168
168
169 #****************************************************************************
169 #****************************************************************************
170 # Generic warning/error printer, used by everything else
170 # Generic warning/error printer, used by everything else
171 def warn(msg,level=2,exit_val=1):
171 def warn(msg,level=2,exit_val=1):
172 """Standard warning printer. Gives formatting consistency.
172 """Standard warning printer. Gives formatting consistency.
173
173
174 Output is sent to Term.cerr (sys.stderr by default).
174 Output is sent to Term.cerr (sys.stderr by default).
175
175
176 Options:
176 Options:
177
177
178 -level(2): allows finer control:
178 -level(2): allows finer control:
179 0 -> Do nothing, dummy function.
179 0 -> Do nothing, dummy function.
180 1 -> Print message.
180 1 -> Print message.
181 2 -> Print 'WARNING:' + message. (Default level).
181 2 -> Print 'WARNING:' + message. (Default level).
182 3 -> Print 'ERROR:' + message.
182 3 -> Print 'ERROR:' + message.
183 4 -> Print 'FATAL ERROR:' + message and trigger a sys.exit(exit_val).
183 4 -> Print 'FATAL ERROR:' + message and trigger a sys.exit(exit_val).
184
184
185 -exit_val (1): exit value returned by sys.exit() for a level 4
185 -exit_val (1): exit value returned by sys.exit() for a level 4
186 warning. Ignored for all other levels."""
186 warning. Ignored for all other levels."""
187
187
188 if level>0:
188 if level>0:
189 header = ['','','WARNING: ','ERROR: ','FATAL ERROR: ']
189 header = ['','','WARNING: ','ERROR: ','FATAL ERROR: ']
190 print >> Term.cerr, '%s%s' % (header[level],msg)
190 print >> Term.cerr, '%s%s' % (header[level],msg)
191 if level == 4:
191 if level == 4:
192 print >> Term.cerr,'Exiting.\n'
192 print >> Term.cerr,'Exiting.\n'
193 sys.exit(exit_val)
193 sys.exit(exit_val)
194
194
195 def info(msg):
195 def info(msg):
196 """Equivalent to warn(msg,level=1)."""
196 """Equivalent to warn(msg,level=1)."""
197
197
198 warn(msg,level=1)
198 warn(msg,level=1)
199
199
200 def error(msg):
200 def error(msg):
201 """Equivalent to warn(msg,level=3)."""
201 """Equivalent to warn(msg,level=3)."""
202
202
203 warn(msg,level=3)
203 warn(msg,level=3)
204
204
205 def fatal(msg,exit_val=1):
205 def fatal(msg,exit_val=1):
206 """Equivalent to warn(msg,exit_val=exit_val,level=4)."""
206 """Equivalent to warn(msg,exit_val=exit_val,level=4)."""
207
207
208 warn(msg,exit_val=exit_val,level=4)
208 warn(msg,exit_val=exit_val,level=4)
209
209
210
210
211 # useful for debugging
211 # useful for debugging
212 def debugp(expr):
212 def debugp(expr):
213 """Print the value of an expression from the caller's frame.
213 """Print the value of an expression from the caller's frame.
214
214
215 Takes an expression, evaluates it in the caller's frame and prints both
215 Takes an expression, evaluates it in the caller's frame and prints both
216 the given expression and the resulting value. The input must be of a form
216 the given expression and the resulting value. The input must be of a form
217 suitable for eval()."""
217 suitable for eval()."""
218
218
219 cf = sys._getframe(1)
219 cf = sys._getframe(1)
220 print '[DBG] %s -> %r' % (expr,eval(expr,cf.f_globals,cf.f_locals))
220 print '[DBG] %s -> %r' % (expr,eval(expr,cf.f_globals,cf.f_locals))
221
221
222 #----------------------------------------------------------------------------
222 #----------------------------------------------------------------------------
223 StringTypes = types.StringTypes
223 StringTypes = types.StringTypes
224
224
225 # Basic timing functionality
225 # Basic timing functionality
226
226
227 # If possible (Unix), use the resource module instead of time.clock()
227 # If possible (Unix), use the resource module instead of time.clock()
228 try:
228 try:
229 import resource
229 import resource
230 def clock():
230 def clock():
231 """clock() -> floating point number
231 """clock() -> floating point number
232
232
233 Return the CPU time in seconds (user time only, system time is
233 Return the CPU time in seconds (user time only, system time is
234 ignored) since the start of the process. This is done via a call to
234 ignored) since the start of the process. This is done via a call to
235 resource.getrusage, so it avoids the wraparound problems in
235 resource.getrusage, so it avoids the wraparound problems in
236 time.clock()."""
236 time.clock()."""
237
237
238 return resource.getrusage(resource.RUSAGE_SELF)[0]
238 return resource.getrusage(resource.RUSAGE_SELF)[0]
239
239
240 def clock2():
240 def clock2():
241 """clock2() -> (t_user,t_system)
241 """clock2() -> (t_user,t_system)
242
242
243 Similar to clock(), but return a tuple of user/system times."""
243 Similar to clock(), but return a tuple of user/system times."""
244 return resource.getrusage(resource.RUSAGE_SELF)[:2]
244 return resource.getrusage(resource.RUSAGE_SELF)[:2]
245
245
246 except ImportError:
246 except ImportError:
247 clock = time.clock
247 clock = time.clock
248 def clock2():
248 def clock2():
249 """Under windows, system CPU time can't be measured.
249 """Under windows, system CPU time can't be measured.
250
250
251 This just returns clock() and zero."""
251 This just returns clock() and zero."""
252 return time.clock(),0.0
252 return time.clock(),0.0
253
253
254 def timings_out(reps,func,*args,**kw):
254 def timings_out(reps,func,*args,**kw):
255 """timings_out(reps,func,*args,**kw) -> (t_total,t_per_call,output)
255 """timings_out(reps,func,*args,**kw) -> (t_total,t_per_call,output)
256
256
257 Execute a function reps times, return a tuple with the elapsed total
257 Execute a function reps times, return a tuple with the elapsed total
258 CPU time in seconds, the time per call and the function's output.
258 CPU time in seconds, the time per call and the function's output.
259
259
260 Under Unix, the return value is the sum of user+system time consumed by
260 Under Unix, the return value is the sum of user+system time consumed by
261 the process, computed via the resource module. This prevents problems
261 the process, computed via the resource module. This prevents problems
262 related to the wraparound effect which the time.clock() function has.
262 related to the wraparound effect which the time.clock() function has.
263
263
264 Under Windows the return value is in wall clock seconds. See the
264 Under Windows the return value is in wall clock seconds. See the
265 documentation for the time module for more details."""
265 documentation for the time module for more details."""
266
266
267 reps = int(reps)
267 reps = int(reps)
268 assert reps >=1, 'reps must be >= 1'
268 assert reps >=1, 'reps must be >= 1'
269 if reps==1:
269 if reps==1:
270 start = clock()
270 start = clock()
271 out = func(*args,**kw)
271 out = func(*args,**kw)
272 tot_time = clock()-start
272 tot_time = clock()-start
273 else:
273 else:
274 rng = xrange(reps-1) # the last time is executed separately to store output
274 rng = xrange(reps-1) # the last time is executed separately to store output
275 start = clock()
275 start = clock()
276 for dummy in rng: func(*args,**kw)
276 for dummy in rng: func(*args,**kw)
277 out = func(*args,**kw) # one last time
277 out = func(*args,**kw) # one last time
278 tot_time = clock()-start
278 tot_time = clock()-start
279 av_time = tot_time / reps
279 av_time = tot_time / reps
280 return tot_time,av_time,out
280 return tot_time,av_time,out
281
281
282 def timings(reps,func,*args,**kw):
282 def timings(reps,func,*args,**kw):
283 """timings(reps,func,*args,**kw) -> (t_total,t_per_call)
283 """timings(reps,func,*args,**kw) -> (t_total,t_per_call)
284
284
285 Execute a function reps times, return a tuple with the elapsed total CPU
285 Execute a function reps times, return a tuple with the elapsed total CPU
286 time in seconds and the time per call. These are just the first two values
286 time in seconds and the time per call. These are just the first two values
287 in timings_out()."""
287 in timings_out()."""
288
288
289 return timings_out(reps,func,*args,**kw)[0:2]
289 return timings_out(reps,func,*args,**kw)[0:2]
290
290
291 def timing(func,*args,**kw):
291 def timing(func,*args,**kw):
292 """timing(func,*args,**kw) -> t_total
292 """timing(func,*args,**kw) -> t_total
293
293
294 Execute a function once, return the elapsed total CPU time in
294 Execute a function once, return the elapsed total CPU time in
295 seconds. This is just the first value in timings_out()."""
295 seconds. This is just the first value in timings_out()."""
296
296
297 return timings_out(1,func,*args,**kw)[0]
297 return timings_out(1,func,*args,**kw)[0]
298
298
299 #****************************************************************************
299 #****************************************************************************
300 # file and system
300 # file and system
301
301
302 def system(cmd,verbose=0,debug=0,header=''):
302 def system(cmd,verbose=0,debug=0,header=''):
303 """Execute a system command, return its exit status.
303 """Execute a system command, return its exit status.
304
304
305 Options:
305 Options:
306
306
307 - verbose (0): print the command to be executed.
307 - verbose (0): print the command to be executed.
308
308
309 - debug (0): only print, do not actually execute.
309 - debug (0): only print, do not actually execute.
310
310
311 - header (''): Header to print on screen prior to the executed command (it
311 - header (''): Header to print on screen prior to the executed command (it
312 is only prepended to the command, no newlines are added).
312 is only prepended to the command, no newlines are added).
313
313
314 Note: a stateful version of this function is available through the
314 Note: a stateful version of this function is available through the
315 SystemExec class."""
315 SystemExec class."""
316
316
317 stat = 0
317 stat = 0
318 if verbose or debug: print header+cmd
318 if verbose or debug: print header+cmd
319 sys.stdout.flush()
319 sys.stdout.flush()
320 if not debug: stat = os.system(cmd)
320 if not debug: stat = os.system(cmd)
321 return stat
321 return stat
322
322
323 # This function is used by ipython in a lot of places to make system calls.
323 # This function is used by ipython in a lot of places to make system calls.
324 # We need it to be slightly different under win32, due to the vagaries of
324 # We need it to be slightly different under win32, due to the vagaries of
325 # 'network shares'. A win32 override is below.
325 # 'network shares'. A win32 override is below.
326
326
327 def shell(cmd,verbose=0,debug=0,header=''):
327 def shell(cmd,verbose=0,debug=0,header=''):
328 """Execute a command in the system shell, always return None.
328 """Execute a command in the system shell, always return None.
329
329
330 Options:
330 Options:
331
331
332 - verbose (0): print the command to be executed.
332 - verbose (0): print the command to be executed.
333
333
334 - debug (0): only print, do not actually execute.
334 - debug (0): only print, do not actually execute.
335
335
336 - header (''): Header to print on screen prior to the executed command (it
336 - header (''): Header to print on screen prior to the executed command (it
337 is only prepended to the command, no newlines are added).
337 is only prepended to the command, no newlines are added).
338
338
339 Note: this is similar to genutils.system(), but it returns None so it can
339 Note: this is similar to genutils.system(), but it returns None so it can
340 be conveniently used in interactive loops without getting the return value
340 be conveniently used in interactive loops without getting the return value
341 (typically 0) printed many times."""
341 (typically 0) printed many times."""
342
342
343 stat = 0
343 stat = 0
344 if verbose or debug: print header+cmd
344 if verbose or debug: print header+cmd
345 # flush stdout so we don't mangle python's buffering
345 # flush stdout so we don't mangle python's buffering
346 sys.stdout.flush()
346 sys.stdout.flush()
347 if not debug:
347 if not debug:
348 os.system(cmd)
348 os.system(cmd)
349
349
350 # override shell() for win32 to deal with network shares
350 # override shell() for win32 to deal with network shares
351 if os.name in ('nt','dos'):
351 if os.name in ('nt','dos'):
352
352
353 shell_ori = shell
353 shell_ori = shell
354
354
355 def shell(cmd,verbose=0,debug=0,header=''):
355 def shell(cmd,verbose=0,debug=0,header=''):
356 if os.getcwd().startswith(r"\\"):
356 if os.getcwd().startswith(r"\\"):
357 path = os.getcwd()
357 path = os.getcwd()
358 # change to c drive (cannot be on UNC-share when issuing os.system,
358 # change to c drive (cannot be on UNC-share when issuing os.system,
359 # as cmd.exe cannot handle UNC addresses)
359 # as cmd.exe cannot handle UNC addresses)
360 os.chdir("c:")
360 os.chdir("c:")
361 # issue pushd to the UNC-share and then run the command
361 # issue pushd to the UNC-share and then run the command
362 try:
362 try:
363 shell_ori('"pushd %s&&"'%path+cmd,verbose,debug,header)
363 shell_ori('"pushd %s&&"'%path+cmd,verbose,debug,header)
364 finally:
364 finally:
365 os.chdir(path)
365 os.chdir(path)
366 else:
366 else:
367 shell_ori(cmd,verbose,debug,header)
367 shell_ori(cmd,verbose,debug,header)
368
368
369 shell.__doc__ = shell_ori.__doc__
369 shell.__doc__ = shell_ori.__doc__
370
370
371 def getoutput(cmd,verbose=0,debug=0,header='',split=0):
371 def getoutput(cmd,verbose=0,debug=0,header='',split=0):
372 """Dummy substitute for perl's backquotes.
372 """Dummy substitute for perl's backquotes.
373
373
374 Executes a command and returns the output.
374 Executes a command and returns the output.
375
375
376 Accepts the same arguments as system(), plus:
376 Accepts the same arguments as system(), plus:
377
377
378 - split(0): if true, the output is returned as a list split on newlines.
378 - split(0): if true, the output is returned as a list split on newlines.
379
379
380 Note: a stateful version of this function is available through the
380 Note: a stateful version of this function is available through the
381 SystemExec class."""
381 SystemExec class."""
382
382
383 if verbose or debug: print header+cmd
383 if verbose or debug: print header+cmd
384 if not debug:
384 if not debug:
385 output = commands.getoutput(cmd)
385 output = commands.getoutput(cmd)
386 if split:
386 if split:
387 return output.split('\n')
387 return output.split('\n')
388 else:
388 else:
389 return output
389 return output
390
390
391 def getoutputerror(cmd,verbose=0,debug=0,header='',split=0):
391 def getoutputerror(cmd,verbose=0,debug=0,header='',split=0):
392 """Return (standard output,standard error) of executing cmd in a shell.
392 """Return (standard output,standard error) of executing cmd in a shell.
393
393
394 Accepts the same arguments as system(), plus:
394 Accepts the same arguments as system(), plus:
395
395
396 - split(0): if true, each of stdout/err is returned as a list split on
396 - split(0): if true, each of stdout/err is returned as a list split on
397 newlines.
397 newlines.
398
398
399 Note: a stateful version of this function is available through the
399 Note: a stateful version of this function is available through the
400 SystemExec class."""
400 SystemExec class."""
401
401
402 if verbose or debug: print header+cmd
402 if verbose or debug: print header+cmd
403 if not cmd:
403 if not cmd:
404 if split:
404 if split:
405 return [],[]
405 return [],[]
406 else:
406 else:
407 return '',''
407 return '',''
408 if not debug:
408 if not debug:
409 pin,pout,perr = os.popen3(cmd)
409 pin,pout,perr = os.popen3(cmd)
410 tout = pout.read().rstrip()
410 tout = pout.read().rstrip()
411 terr = perr.read().rstrip()
411 terr = perr.read().rstrip()
412 pin.close()
412 pin.close()
413 pout.close()
413 pout.close()
414 perr.close()
414 perr.close()
415 if split:
415 if split:
416 return tout.split('\n'),terr.split('\n')
416 return tout.split('\n'),terr.split('\n')
417 else:
417 else:
418 return tout,terr
418 return tout,terr
419
419
420 # for compatibility with older naming conventions
420 # for compatibility with older naming conventions
421 xsys = system
421 xsys = system
422 bq = getoutput
422 bq = getoutput
423
423
424 class SystemExec:
424 class SystemExec:
425 """Access the system and getoutput functions through a stateful interface.
425 """Access the system and getoutput functions through a stateful interface.
426
426
427 Note: here we refer to the system and getoutput functions from this
427 Note: here we refer to the system and getoutput functions from this
428 library, not the ones from the standard python library.
428 library, not the ones from the standard python library.
429
429
430 This class offers the system and getoutput functions as methods, but the
430 This class offers the system and getoutput functions as methods, but the
431 verbose, debug and header parameters can be set for the instance (at
431 verbose, debug and header parameters can be set for the instance (at
432 creation time or later) so that they don't need to be specified on each
432 creation time or later) so that they don't need to be specified on each
433 call.
433 call.
434
434
435 For efficiency reasons, there's no way to override the parameters on a
435 For efficiency reasons, there's no way to override the parameters on a
436 per-call basis other than by setting instance attributes. If you need
436 per-call basis other than by setting instance attributes. If you need
437 local overrides, it's best to directly call system() or getoutput().
437 local overrides, it's best to directly call system() or getoutput().
438
438
439 The following names are provided as alternate options:
439 The following names are provided as alternate options:
440 - xsys: alias to system
440 - xsys: alias to system
441 - bq: alias to getoutput
441 - bq: alias to getoutput
442
442
443 An instance can then be created as:
443 An instance can then be created as:
444 >>> sysexec = SystemExec(verbose=1,debug=0,header='Calling: ')
444 >>> sysexec = SystemExec(verbose=1,debug=0,header='Calling: ')
445
445
446 And used as:
446 And used as:
447 >>> sysexec.xsys('pwd')
447 >>> sysexec.xsys('pwd')
448 >>> dirlist = sysexec.bq('ls -l')
448 >>> dirlist = sysexec.bq('ls -l')
449 """
449 """
450
450
451 def __init__(self,verbose=0,debug=0,header='',split=0):
451 def __init__(self,verbose=0,debug=0,header='',split=0):
452 """Specify the instance's values for verbose, debug and header."""
452 """Specify the instance's values for verbose, debug and header."""
453 setattr_list(self,'verbose debug header split')
453 setattr_list(self,'verbose debug header split')
454
454
455 def system(self,cmd):
455 def system(self,cmd):
456 """Stateful interface to system(), with the same keyword parameters."""
456 """Stateful interface to system(), with the same keyword parameters."""
457
457
458 system(cmd,self.verbose,self.debug,self.header)
458 system(cmd,self.verbose,self.debug,self.header)
459
459
460 def shell(self,cmd):
460 def shell(self,cmd):
461 """Stateful interface to shell(), with the same keyword parameters."""
461 """Stateful interface to shell(), with the same keyword parameters."""
462
462
463 shell(cmd,self.verbose,self.debug,self.header)
463 shell(cmd,self.verbose,self.debug,self.header)
464
464
465 xsys = system # alias
465 xsys = system # alias
466
466
467 def getoutput(self,cmd):
467 def getoutput(self,cmd):
468 """Stateful interface to getoutput()."""
468 """Stateful interface to getoutput()."""
469
469
470 return getoutput(cmd,self.verbose,self.debug,self.header,self.split)
470 return getoutput(cmd,self.verbose,self.debug,self.header,self.split)
471
471
472 def getoutputerror(self,cmd):
472 def getoutputerror(self,cmd):
473 """Stateful interface to getoutputerror()."""
473 """Stateful interface to getoutputerror()."""
474
474
475 return getoutputerror(cmd,self.verbose,self.debug,self.header,self.split)
475 return getoutputerror(cmd,self.verbose,self.debug,self.header,self.split)
476
476
477 bq = getoutput # alias
477 bq = getoutput # alias
478
478
479 #-----------------------------------------------------------------------------
479 #-----------------------------------------------------------------------------
480 def mutex_opts(dict,ex_op):
480 def mutex_opts(dict,ex_op):
481 """Check for presence of mutually exclusive keys in a dict.
481 """Check for presence of mutually exclusive keys in a dict.
482
482
483 Call: mutex_opts(dict,[[op1a,op1b],[op2a,op2b]...]"""
483 Call: mutex_opts(dict,[[op1a,op1b],[op2a,op2b]...]"""
484 for op1,op2 in ex_op:
484 for op1,op2 in ex_op:
485 if op1 in dict and op2 in dict:
485 if op1 in dict and op2 in dict:
486 raise ValueError,'\n*** ERROR in Arguments *** '\
486 raise ValueError,'\n*** ERROR in Arguments *** '\
487 'Options '+op1+' and '+op2+' are mutually exclusive.'
487 'Options '+op1+' and '+op2+' are mutually exclusive.'
488
488
489 #-----------------------------------------------------------------------------
489 #-----------------------------------------------------------------------------
490 def get_py_filename(name):
490 def get_py_filename(name):
491 """Return a valid python filename in the current directory.
491 """Return a valid python filename in the current directory.
492
492
493 If the given name is not a file, it adds '.py' and searches again.
493 If the given name is not a file, it adds '.py' and searches again.
494 Raises IOError with an informative message if the file isn't found."""
494 Raises IOError with an informative message if the file isn't found."""
495
495
496 name = os.path.expanduser(name)
496 name = os.path.expanduser(name)
497 if not os.path.isfile(name) and not name.endswith('.py'):
497 if not os.path.isfile(name) and not name.endswith('.py'):
498 name += '.py'
498 name += '.py'
499 if os.path.isfile(name):
499 if os.path.isfile(name):
500 return name
500 return name
501 else:
501 else:
502 raise IOError,'File `%s` not found.' % name
502 raise IOError,'File `%s` not found.' % name
503
503
504 #-----------------------------------------------------------------------------
504 #-----------------------------------------------------------------------------
505 def filefind(fname,alt_dirs = None):
505 def filefind(fname,alt_dirs = None):
506 """Return the given filename either in the current directory, if it
506 """Return the given filename either in the current directory, if it
507 exists, or in a specified list of directories.
507 exists, or in a specified list of directories.
508
508
509 ~ expansion is done on all file and directory names.
509 ~ expansion is done on all file and directory names.
510
510
511 Upon an unsuccessful search, raise an IOError exception."""
511 Upon an unsuccessful search, raise an IOError exception."""
512
512
513 if alt_dirs is None:
513 if alt_dirs is None:
514 try:
514 try:
515 alt_dirs = get_home_dir()
515 alt_dirs = get_home_dir()
516 except HomeDirError:
516 except HomeDirError:
517 alt_dirs = os.getcwd()
517 alt_dirs = os.getcwd()
518 search = [fname] + list_strings(alt_dirs)
518 search = [fname] + list_strings(alt_dirs)
519 search = map(os.path.expanduser,search)
519 search = map(os.path.expanduser,search)
520 #print 'search list for',fname,'list:',search # dbg
520 #print 'search list for',fname,'list:',search # dbg
521 fname = search[0]
521 fname = search[0]
522 if os.path.isfile(fname):
522 if os.path.isfile(fname):
523 return fname
523 return fname
524 for direc in search[1:]:
524 for direc in search[1:]:
525 testname = os.path.join(direc,fname)
525 testname = os.path.join(direc,fname)
526 #print 'testname',testname # dbg
526 #print 'testname',testname # dbg
527 if os.path.isfile(testname):
527 if os.path.isfile(testname):
528 return testname
528 return testname
529 raise IOError,'File' + `fname` + \
529 raise IOError,'File' + `fname` + \
530 ' not found in current or supplied directories:' + `alt_dirs`
530 ' not found in current or supplied directories:' + `alt_dirs`
531
531
532 #----------------------------------------------------------------------------
532 #----------------------------------------------------------------------------
533 def file_read(filename):
533 def file_read(filename):
534 """Read a file and close it. Returns the file source."""
534 """Read a file and close it. Returns the file source."""
535 fobj=open(filename,'r');
535 fobj=open(filename,'r');
536 source = fobj.read();
536 source = fobj.read();
537 fobj.close()
537 fobj.close()
538 return source
538 return source
539
539
540 #----------------------------------------------------------------------------
540 #----------------------------------------------------------------------------
541 def target_outdated(target,deps):
541 def target_outdated(target,deps):
542 """Determine whether a target is out of date.
542 """Determine whether a target is out of date.
543
543
544 target_outdated(target,deps) -> 1/0
544 target_outdated(target,deps) -> 1/0
545
545
546 deps: list of filenames which MUST exist.
546 deps: list of filenames which MUST exist.
547 target: single filename which may or may not exist.
547 target: single filename which may or may not exist.
548
548
549 If target doesn't exist or is older than any file listed in deps, return
549 If target doesn't exist or is older than any file listed in deps, return
550 true, otherwise return false.
550 true, otherwise return false.
551 """
551 """
552 try:
552 try:
553 target_time = os.path.getmtime(target)
553 target_time = os.path.getmtime(target)
554 except os.error:
554 except os.error:
555 return 1
555 return 1
556 for dep in deps:
556 for dep in deps:
557 dep_time = os.path.getmtime(dep)
557 dep_time = os.path.getmtime(dep)
558 if dep_time > target_time:
558 if dep_time > target_time:
559 #print "For target",target,"Dep failed:",dep # dbg
559 #print "For target",target,"Dep failed:",dep # dbg
560 #print "times (dep,tar):",dep_time,target_time # dbg
560 #print "times (dep,tar):",dep_time,target_time # dbg
561 return 1
561 return 1
562 return 0
562 return 0
563
563
564 #-----------------------------------------------------------------------------
564 #-----------------------------------------------------------------------------
565 def target_update(target,deps,cmd):
565 def target_update(target,deps,cmd):
566 """Update a target with a given command given a list of dependencies.
566 """Update a target with a given command given a list of dependencies.
567
567
568 target_update(target,deps,cmd) -> runs cmd if target is outdated.
568 target_update(target,deps,cmd) -> runs cmd if target is outdated.
569
569
570 This is just a wrapper around target_outdated() which calls the given
570 This is just a wrapper around target_outdated() which calls the given
571 command if target is outdated."""
571 command if target is outdated."""
572
572
573 if target_outdated(target,deps):
573 if target_outdated(target,deps):
574 xsys(cmd)
574 xsys(cmd)
575
575
576 #----------------------------------------------------------------------------
576 #----------------------------------------------------------------------------
577 def unquote_ends(istr):
577 def unquote_ends(istr):
578 """Remove a single pair of quotes from the endpoints of a string."""
578 """Remove a single pair of quotes from the endpoints of a string."""
579
579
580 if not istr:
580 if not istr:
581 return istr
581 return istr
582 if (istr[0]=="'" and istr[-1]=="'") or \
582 if (istr[0]=="'" and istr[-1]=="'") or \
583 (istr[0]=='"' and istr[-1]=='"'):
583 (istr[0]=='"' and istr[-1]=='"'):
584 return istr[1:-1]
584 return istr[1:-1]
585 else:
585 else:
586 return istr
586 return istr
587
587
588 #----------------------------------------------------------------------------
588 #----------------------------------------------------------------------------
589 def process_cmdline(argv,names=[],defaults={},usage=''):
589 def process_cmdline(argv,names=[],defaults={},usage=''):
590 """ Process command-line options and arguments.
590 """ Process command-line options and arguments.
591
591
592 Arguments:
592 Arguments:
593
593
594 - argv: list of arguments, typically sys.argv.
594 - argv: list of arguments, typically sys.argv.
595
595
596 - names: list of option names. See DPyGetOpt docs for details on options
596 - names: list of option names. See DPyGetOpt docs for details on options
597 syntax.
597 syntax.
598
598
599 - defaults: dict of default values.
599 - defaults: dict of default values.
600
600
601 - usage: optional usage notice to print if a wrong argument is passed.
601 - usage: optional usage notice to print if a wrong argument is passed.
602
602
603 Return a dict of options and a list of free arguments."""
603 Return a dict of options and a list of free arguments."""
604
604
605 getopt = DPyGetOpt.DPyGetOpt()
605 getopt = DPyGetOpt.DPyGetOpt()
606 getopt.setIgnoreCase(0)
606 getopt.setIgnoreCase(0)
607 getopt.parseConfiguration(names)
607 getopt.parseConfiguration(names)
608
608
609 try:
609 try:
610 getopt.processArguments(argv)
610 getopt.processArguments(argv)
611 except:
611 except:
612 print usage
612 print usage
613 warn(`sys.exc_value`,level=4)
613 warn(`sys.exc_value`,level=4)
614
614
615 defaults.update(getopt.optionValues)
615 defaults.update(getopt.optionValues)
616 args = getopt.freeValues
616 args = getopt.freeValues
617
617
618 return defaults,args
618 return defaults,args
619
619
620 #----------------------------------------------------------------------------
620 #----------------------------------------------------------------------------
621 def optstr2types(ostr):
621 def optstr2types(ostr):
622 """Convert a string of option names to a dict of type mappings.
622 """Convert a string of option names to a dict of type mappings.
623
623
624 optstr2types(str) -> {None:'string_opts',int:'int_opts',float:'float_opts'}
624 optstr2types(str) -> {None:'string_opts',int:'int_opts',float:'float_opts'}
625
625
626 This is used to get the types of all the options in a string formatted
626 This is used to get the types of all the options in a string formatted
627 with the conventions of DPyGetOpt. The 'type' None is used for options
627 with the conventions of DPyGetOpt. The 'type' None is used for options
628 which are strings (they need no further conversion). This function's main
628 which are strings (they need no further conversion). This function's main
629 use is to get a typemap for use with read_dict().
629 use is to get a typemap for use with read_dict().
630 """
630 """
631
631
632 typeconv = {None:'',int:'',float:''}
632 typeconv = {None:'',int:'',float:''}
633 typemap = {'s':None,'i':int,'f':float}
633 typemap = {'s':None,'i':int,'f':float}
634 opt_re = re.compile(r'([\w]*)([^:=]*:?=?)([sif]?)')
634 opt_re = re.compile(r'([\w]*)([^:=]*:?=?)([sif]?)')
635
635
636 for w in ostr.split():
636 for w in ostr.split():
637 oname,alias,otype = opt_re.match(w).groups()
637 oname,alias,otype = opt_re.match(w).groups()
638 if otype == '' or alias == '!': # simple switches are integers too
638 if otype == '' or alias == '!': # simple switches are integers too
639 otype = 'i'
639 otype = 'i'
640 typeconv[typemap[otype]] += oname + ' '
640 typeconv[typemap[otype]] += oname + ' '
641 return typeconv
641 return typeconv
642
642
643 #----------------------------------------------------------------------------
643 #----------------------------------------------------------------------------
644 def read_dict(filename,type_conv=None,**opt):
644 def read_dict(filename,type_conv=None,**opt):
645
645
646 """Read a dictionary of key=value pairs from an input file, optionally
646 """Read a dictionary of key=value pairs from an input file, optionally
647 performing conversions on the resulting values.
647 performing conversions on the resulting values.
648
648
649 read_dict(filename,type_conv,**opt) -> dict
649 read_dict(filename,type_conv,**opt) -> dict
650
650
651 Only one value per line is accepted, the format should be
651 Only one value per line is accepted, the format should be
652 # optional comments are ignored
652 # optional comments are ignored
653 key value\n
653 key value\n
654
654
655 Args:
655 Args:
656
656
657 - type_conv: A dictionary specifying which keys need to be converted to
657 - type_conv: A dictionary specifying which keys need to be converted to
658 which types. By default all keys are read as strings. This dictionary
658 which types. By default all keys are read as strings. This dictionary
659 should have as its keys valid conversion functions for strings
659 should have as its keys valid conversion functions for strings
660 (int,long,float,complex, or your own). The value for each key
660 (int,long,float,complex, or your own). The value for each key
661 (converter) should be a whitespace separated string containing the names
661 (converter) should be a whitespace separated string containing the names
662 of all the entries in the file to be converted using that function. For
662 of all the entries in the file to be converted using that function. For
663 keys to be left alone, use None as the conversion function (only needed
663 keys to be left alone, use None as the conversion function (only needed
664 with purge=1, see below).
664 with purge=1, see below).
665
665
666 - opt: dictionary with extra options as below (default in parens)
666 - opt: dictionary with extra options as below (default in parens)
667
667
668 purge(0): if set to 1, all keys *not* listed in type_conv are purged out
668 purge(0): if set to 1, all keys *not* listed in type_conv are purged out
669 of the dictionary to be returned. If purge is going to be used, the
669 of the dictionary to be returned. If purge is going to be used, the
670 set of keys to be left as strings also has to be explicitly specified
670 set of keys to be left as strings also has to be explicitly specified
671 using the (non-existent) conversion function None.
671 using the (non-existent) conversion function None.
672
672
673 fs(None): field separator. This is the key/value separator to be used
673 fs(None): field separator. This is the key/value separator to be used
674 when parsing the file. The None default means any whitespace [behavior
674 when parsing the file. The None default means any whitespace [behavior
675 of string.split()].
675 of string.split()].
676
676
677 strip(0): if 1, strip string values of leading/trailinig whitespace.
677 strip(0): if 1, strip string values of leading/trailinig whitespace.
678
678
679 warn(1): warning level if requested keys are not found in file.
679 warn(1): warning level if requested keys are not found in file.
680 - 0: silently ignore.
680 - 0: silently ignore.
681 - 1: inform but proceed.
681 - 1: inform but proceed.
682 - 2: raise KeyError exception.
682 - 2: raise KeyError exception.
683
683
684 no_empty(0): if 1, remove keys with whitespace strings as a value.
684 no_empty(0): if 1, remove keys with whitespace strings as a value.
685
685
686 unique([]): list of keys (or space separated string) which can't be
686 unique([]): list of keys (or space separated string) which can't be
687 repeated. If one such key is found in the file, each new instance
687 repeated. If one such key is found in the file, each new instance
688 overwrites the previous one. For keys not listed here, the behavior is
688 overwrites the previous one. For keys not listed here, the behavior is
689 to make a list of all appearances.
689 to make a list of all appearances.
690
690
691 Example:
691 Example:
692 If the input file test.ini has:
692 If the input file test.ini has:
693 i 3
693 i 3
694 x 4.5
694 x 4.5
695 y 5.5
695 y 5.5
696 s hi ho
696 s hi ho
697 Then:
697 Then:
698
698
699 >>> type_conv={int:'i',float:'x',None:'s'}
699 >>> type_conv={int:'i',float:'x',None:'s'}
700 >>> read_dict('test.ini')
700 >>> read_dict('test.ini')
701 {'i': '3', 's': 'hi ho', 'x': '4.5', 'y': '5.5'}
701 {'i': '3', 's': 'hi ho', 'x': '4.5', 'y': '5.5'}
702 >>> read_dict('test.ini',type_conv)
702 >>> read_dict('test.ini',type_conv)
703 {'i': 3, 's': 'hi ho', 'x': 4.5, 'y': '5.5'}
703 {'i': 3, 's': 'hi ho', 'x': 4.5, 'y': '5.5'}
704 >>> read_dict('test.ini',type_conv,purge=1)
704 >>> read_dict('test.ini',type_conv,purge=1)
705 {'i': 3, 's': 'hi ho', 'x': 4.5}
705 {'i': 3, 's': 'hi ho', 'x': 4.5}
706 """
706 """
707
707
708 # starting config
708 # starting config
709 opt.setdefault('purge',0)
709 opt.setdefault('purge',0)
710 opt.setdefault('fs',None) # field sep defaults to any whitespace
710 opt.setdefault('fs',None) # field sep defaults to any whitespace
711 opt.setdefault('strip',0)
711 opt.setdefault('strip',0)
712 opt.setdefault('warn',1)
712 opt.setdefault('warn',1)
713 opt.setdefault('no_empty',0)
713 opt.setdefault('no_empty',0)
714 opt.setdefault('unique','')
714 opt.setdefault('unique','')
715 if type(opt['unique']) in StringTypes:
715 if type(opt['unique']) in StringTypes:
716 unique_keys = qw(opt['unique'])
716 unique_keys = qw(opt['unique'])
717 elif type(opt['unique']) in (types.TupleType,types.ListType):
717 elif type(opt['unique']) in (types.TupleType,types.ListType):
718 unique_keys = opt['unique']
718 unique_keys = opt['unique']
719 else:
719 else:
720 raise ValueError, 'Unique keys must be given as a string, List or Tuple'
720 raise ValueError, 'Unique keys must be given as a string, List or Tuple'
721
721
722 dict = {}
722 dict = {}
723 # first read in table of values as strings
723 # first read in table of values as strings
724 file = open(filename,'r')
724 file = open(filename,'r')
725 for line in file.readlines():
725 for line in file.readlines():
726 line = line.strip()
726 line = line.strip()
727 if len(line) and line[0]=='#': continue
727 if len(line) and line[0]=='#': continue
728 if len(line)>0:
728 if len(line)>0:
729 lsplit = line.split(opt['fs'],1)
729 lsplit = line.split(opt['fs'],1)
730 try:
730 try:
731 key,val = lsplit
731 key,val = lsplit
732 except ValueError:
732 except ValueError:
733 key,val = lsplit[0],''
733 key,val = lsplit[0],''
734 key = key.strip()
734 key = key.strip()
735 if opt['strip']: val = val.strip()
735 if opt['strip']: val = val.strip()
736 if val == "''" or val == '""': val = ''
736 if val == "''" or val == '""': val = ''
737 if opt['no_empty'] and (val=='' or val.isspace()):
737 if opt['no_empty'] and (val=='' or val.isspace()):
738 continue
738 continue
739 # if a key is found more than once in the file, build a list
739 # if a key is found more than once in the file, build a list
740 # unless it's in the 'unique' list. In that case, last found in file
740 # unless it's in the 'unique' list. In that case, last found in file
741 # takes precedence. User beware.
741 # takes precedence. User beware.
742 try:
742 try:
743 if dict[key] and key in unique_keys:
743 if dict[key] and key in unique_keys:
744 dict[key] = val
744 dict[key] = val
745 elif type(dict[key]) is types.ListType:
745 elif type(dict[key]) is types.ListType:
746 dict[key].append(val)
746 dict[key].append(val)
747 else:
747 else:
748 dict[key] = [dict[key],val]
748 dict[key] = [dict[key],val]
749 except KeyError:
749 except KeyError:
750 dict[key] = val
750 dict[key] = val
751 # purge if requested
751 # purge if requested
752 if opt['purge']:
752 if opt['purge']:
753 accepted_keys = qwflat(type_conv.values())
753 accepted_keys = qwflat(type_conv.values())
754 for key in dict.keys():
754 for key in dict.keys():
755 if key in accepted_keys: continue
755 if key in accepted_keys: continue
756 del(dict[key])
756 del(dict[key])
757 # now convert if requested
757 # now convert if requested
758 if type_conv==None: return dict
758 if type_conv==None: return dict
759 conversions = type_conv.keys()
759 conversions = type_conv.keys()
760 try: conversions.remove(None)
760 try: conversions.remove(None)
761 except: pass
761 except: pass
762 for convert in conversions:
762 for convert in conversions:
763 for val in qw(type_conv[convert]):
763 for val in qw(type_conv[convert]):
764 try:
764 try:
765 dict[val] = convert(dict[val])
765 dict[val] = convert(dict[val])
766 except KeyError,e:
766 except KeyError,e:
767 if opt['warn'] == 0:
767 if opt['warn'] == 0:
768 pass
768 pass
769 elif opt['warn'] == 1:
769 elif opt['warn'] == 1:
770 print >>sys.stderr, 'Warning: key',val,\
770 print >>sys.stderr, 'Warning: key',val,\
771 'not found in file',filename
771 'not found in file',filename
772 elif opt['warn'] == 2:
772 elif opt['warn'] == 2:
773 raise KeyError,e
773 raise KeyError,e
774 else:
774 else:
775 raise ValueError,'Warning level must be 0,1 or 2'
775 raise ValueError,'Warning level must be 0,1 or 2'
776
776
777 return dict
777 return dict
778
778
779 #----------------------------------------------------------------------------
779 #----------------------------------------------------------------------------
780 def flag_calls(func):
780 def flag_calls(func):
781 """Wrap a function to detect and flag when it gets called.
781 """Wrap a function to detect and flag when it gets called.
782
782
783 This is a decorator which takes a function and wraps it in a function with
783 This is a decorator which takes a function and wraps it in a function with
784 a 'called' attribute. wrapper.called is initialized to False.
784 a 'called' attribute. wrapper.called is initialized to False.
785
785
786 The wrapper.called attribute is set to False right before each call to the
786 The wrapper.called attribute is set to False right before each call to the
787 wrapped function, so if the call fails it remains False. After the call
787 wrapped function, so if the call fails it remains False. After the call
788 completes, wrapper.called is set to True and the output is returned.
788 completes, wrapper.called is set to True and the output is returned.
789
789
790 Testing for truth in wrapper.called allows you to determine if a call to
790 Testing for truth in wrapper.called allows you to determine if a call to
791 func() was attempted and succeeded."""
791 func() was attempted and succeeded."""
792
792
793 def wrapper(*args,**kw):
793 def wrapper(*args,**kw):
794 wrapper.called = False
794 wrapper.called = False
795 out = func(*args,**kw)
795 out = func(*args,**kw)
796 wrapper.called = True
796 wrapper.called = True
797 return out
797 return out
798
798
799 wrapper.called = False
799 wrapper.called = False
800 wrapper.__doc__ = func.__doc__
800 wrapper.__doc__ = func.__doc__
801 return wrapper
801 return wrapper
802
802
803 #----------------------------------------------------------------------------
803 #----------------------------------------------------------------------------
804 class HomeDirError(Error):
804 class HomeDirError(Error):
805 pass
805 pass
806
806
807 def get_home_dir():
807 def get_home_dir():
808 """Return the closest possible equivalent to a 'home' directory.
808 """Return the closest possible equivalent to a 'home' directory.
809
809
810 We first try $HOME. Absent that, on NT it's $HOMEDRIVE\$HOMEPATH.
810 We first try $HOME. Absent that, on NT it's $HOMEDRIVE\$HOMEPATH.
811
811
812 Currently only Posix and NT are implemented, a HomeDirError exception is
812 Currently only Posix and NT are implemented, a HomeDirError exception is
813 raised for all other OSes. """
813 raised for all other OSes. """
814
814
815 isdir = os.path.isdir
815 isdir = os.path.isdir
816 env = os.environ
816 env = os.environ
817 try:
817 try:
818 homedir = env['HOME']
818 homedir = env['HOME']
819 if not isdir(homedir):
819 if not isdir(homedir):
820 # in case a user stuck some string which does NOT resolve to a
820 # in case a user stuck some string which does NOT resolve to a
821 # valid path, it's as good as if we hadn't foud it
821 # valid path, it's as good as if we hadn't foud it
822 raise KeyError
822 raise KeyError
823 return homedir
823 return homedir
824 except KeyError:
824 except KeyError:
825 if os.name == 'posix':
825 if os.name == 'posix':
826 raise HomeDirError,'undefined $HOME, IPython can not proceed.'
826 raise HomeDirError,'undefined $HOME, IPython can not proceed.'
827 elif os.name == 'nt':
827 elif os.name == 'nt':
828 # For some strange reason, win9x returns 'nt' for os.name.
828 # For some strange reason, win9x returns 'nt' for os.name.
829 try:
829 try:
830 homedir = os.path.join(env['HOMEDRIVE'],env['HOMEPATH'])
830 homedir = os.path.join(env['HOMEDRIVE'],env['HOMEPATH'])
831 if not isdir(homedir):
831 if not isdir(homedir):
832 homedir = os.path.join(env['USERPROFILE'])
832 homedir = os.path.join(env['USERPROFILE'])
833 if not isdir(homedir):
833 if not isdir(homedir):
834 raise HomeDirError
834 raise HomeDirError
835 return homedir
835 return homedir
836 except:
836 except:
837 try:
837 try:
838 # Use the registry to get the 'My Documents' folder.
838 # Use the registry to get the 'My Documents' folder.
839 import _winreg as wreg
839 import _winreg as wreg
840 key = wreg.OpenKey(wreg.HKEY_CURRENT_USER,
840 key = wreg.OpenKey(wreg.HKEY_CURRENT_USER,
841 "Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders")
841 "Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders")
842 homedir = wreg.QueryValueEx(key,'Personal')[0]
842 homedir = wreg.QueryValueEx(key,'Personal')[0]
843 key.Close()
843 key.Close()
844 if not isdir(homedir):
844 if not isdir(homedir):
845 e = ('Invalid "Personal" folder registry key '
845 e = ('Invalid "Personal" folder registry key '
846 'typically "My Documents".\n'
846 'typically "My Documents".\n'
847 'Value: %s\n'
847 'Value: %s\n'
848 'This is not a valid directory on your system.' %
848 'This is not a valid directory on your system.' %
849 homedir)
849 homedir)
850 raise HomeDirError(e)
850 raise HomeDirError(e)
851 return homedir
851 return homedir
852 except HomeDirError:
852 except HomeDirError:
853 raise
853 raise
854 except:
854 except:
855 return 'C:\\'
855 return 'C:\\'
856 elif os.name == 'dos':
856 elif os.name == 'dos':
857 # Desperate, may do absurd things in classic MacOS. May work under DOS.
857 # Desperate, may do absurd things in classic MacOS. May work under DOS.
858 return 'C:\\'
858 return 'C:\\'
859 else:
859 else:
860 raise HomeDirError,'support for your operating system not implemented.'
860 raise HomeDirError,'support for your operating system not implemented.'
861
861
862 #****************************************************************************
862 #****************************************************************************
863 # strings and text
863 # strings and text
864
864
865 class LSString(str):
865 class LSString(str):
866 """String derivative with a special access attributes.
866 """String derivative with a special access attributes.
867
867
868 These are normal strings, but with the special attributes:
868 These are normal strings, but with the special attributes:
869
869
870 .l (or .list) : value as list (split on newlines).
870 .l (or .list) : value as list (split on newlines).
871 .n (or .nlstr): original value (the string itself).
871 .n (or .nlstr): original value (the string itself).
872 .s (or .spstr): value as whitespace-separated string.
872 .s (or .spstr): value as whitespace-separated string.
873
873
874 Any values which require transformations are computed only once and
874 Any values which require transformations are computed only once and
875 cached.
875 cached.
876
876
877 Such strings are very useful to efficiently interact with the shell, which
877 Such strings are very useful to efficiently interact with the shell, which
878 typically only understands whitespace-separated options for commands."""
878 typically only understands whitespace-separated options for commands."""
879
879
880 def get_list(self):
880 def get_list(self):
881 try:
881 try:
882 return self.__list
882 return self.__list
883 except AttributeError:
883 except AttributeError:
884 self.__list = self.split('\n')
884 self.__list = self.split('\n')
885 return self.__list
885 return self.__list
886
886
887 l = list = property(get_list)
887 l = list = property(get_list)
888
888
889 def get_spstr(self):
889 def get_spstr(self):
890 try:
890 try:
891 return self.__spstr
891 return self.__spstr
892 except AttributeError:
892 except AttributeError:
893 self.__spstr = self.replace('\n',' ')
893 self.__spstr = self.replace('\n',' ')
894 return self.__spstr
894 return self.__spstr
895
895
896 s = spstr = property(get_spstr)
896 s = spstr = property(get_spstr)
897
897
898 def get_nlstr(self):
898 def get_nlstr(self):
899 return self
899 return self
900
900
901 n = nlstr = property(get_nlstr)
901 n = nlstr = property(get_nlstr)
902
902
903 #----------------------------------------------------------------------------
903 #----------------------------------------------------------------------------
904 class SList(list):
904 class SList(list):
905 """List derivative with a special access attributes.
905 """List derivative with a special access attributes.
906
906
907 These are normal lists, but with the special attributes:
907 These are normal lists, but with the special attributes:
908
908
909 .l (or .list) : value as list (the list itself).
909 .l (or .list) : value as list (the list itself).
910 .n (or .nlstr): value as a string, joined on newlines.
910 .n (or .nlstr): value as a string, joined on newlines.
911 .s (or .spstr): value as a string, joined on spaces.
911 .s (or .spstr): value as a string, joined on spaces.
912
912
913 Any values which require transformations are computed only once and
913 Any values which require transformations are computed only once and
914 cached."""
914 cached."""
915
915
916 def get_list(self):
916 def get_list(self):
917 return self
917 return self
918
918
919 l = list = property(get_list)
919 l = list = property(get_list)
920
920
921 def get_spstr(self):
921 def get_spstr(self):
922 try:
922 try:
923 return self.__spstr
923 return self.__spstr
924 except AttributeError:
924 except AttributeError:
925 self.__spstr = ' '.join(self)
925 self.__spstr = ' '.join(self)
926 return self.__spstr
926 return self.__spstr
927
927
928 s = spstr = property(get_spstr)
928 s = spstr = property(get_spstr)
929
929
930 def get_nlstr(self):
930 def get_nlstr(self):
931 try:
931 try:
932 return self.__nlstr
932 return self.__nlstr
933 except AttributeError:
933 except AttributeError:
934 self.__nlstr = '\n'.join(self)
934 self.__nlstr = '\n'.join(self)
935 return self.__nlstr
935 return self.__nlstr
936
936
937 n = nlstr = property(get_nlstr)
937 n = nlstr = property(get_nlstr)
938
938
939 #----------------------------------------------------------------------------
939 #----------------------------------------------------------------------------
940 # This can be replaced with an isspace() call once we drop 2.2 compatibility
940 # This can be replaced with an isspace() call once we drop 2.2 compatibility
941 _isspace_match = re.compile(r'^\s+$').match
941 _isspace_match = re.compile(r'^\s+$').match
942 def isspace(s):
942 def isspace(s):
943 return bool(_isspace_match(s))
943 return bool(_isspace_match(s))
944
944
945 #----------------------------------------------------------------------------
945 #----------------------------------------------------------------------------
946 def esc_quotes(strng):
946 def esc_quotes(strng):
947 """Return the input string with single and double quotes escaped out"""
947 """Return the input string with single and double quotes escaped out"""
948
948
949 return strng.replace('"','\\"').replace("'","\\'")
949 return strng.replace('"','\\"').replace("'","\\'")
950
950
951 #----------------------------------------------------------------------------
951 #----------------------------------------------------------------------------
952 def raw_input_multi(header='', ps1='==> ', ps2='..> ',terminate_str = '.'):
952 def raw_input_multi(header='', ps1='==> ', ps2='..> ',terminate_str = '.'):
953 """Take multiple lines of input.
953 """Take multiple lines of input.
954
954
955 A list with each line of input as a separate element is returned when a
955 A list with each line of input as a separate element is returned when a
956 termination string is entered (defaults to a single '.'). Input can also
956 termination string is entered (defaults to a single '.'). Input can also
957 terminate via EOF (^D in Unix, ^Z-RET in Windows).
957 terminate via EOF (^D in Unix, ^Z-RET in Windows).
958
958
959 Lines of input which end in \\ are joined into single entries (and a
959 Lines of input which end in \\ are joined into single entries (and a
960 secondary continuation prompt is issued as long as the user terminates
960 secondary continuation prompt is issued as long as the user terminates
961 lines with \\). This allows entering very long strings which are still
961 lines with \\). This allows entering very long strings which are still
962 meant to be treated as single entities.
962 meant to be treated as single entities.
963 """
963 """
964
964
965 try:
965 try:
966 if header:
966 if header:
967 header += '\n'
967 header += '\n'
968 lines = [raw_input(header + ps1)]
968 lines = [raw_input(header + ps1)]
969 except EOFError:
969 except EOFError:
970 return []
970 return []
971 terminate = [terminate_str]
971 terminate = [terminate_str]
972 try:
972 try:
973 while lines[-1:] != terminate:
973 while lines[-1:] != terminate:
974 new_line = raw_input(ps1)
974 new_line = raw_input(ps1)
975 while new_line.endswith('\\'):
975 while new_line.endswith('\\'):
976 new_line = new_line[:-1] + raw_input(ps2)
976 new_line = new_line[:-1] + raw_input(ps2)
977 lines.append(new_line)
977 lines.append(new_line)
978
978
979 return lines[:-1] # don't return the termination command
979 return lines[:-1] # don't return the termination command
980 except EOFError:
980 except EOFError:
981 print
981 print
982 return lines
982 return lines
983
983
984 #----------------------------------------------------------------------------
984 #----------------------------------------------------------------------------
985 def raw_input_ext(prompt='', ps2='... '):
985 def raw_input_ext(prompt='', ps2='... '):
986 """Similar to raw_input(), but accepts extended lines if input ends with \\."""
986 """Similar to raw_input(), but accepts extended lines if input ends with \\."""
987
987
988 line = raw_input(prompt)
988 line = raw_input(prompt)
989 while line.endswith('\\'):
989 while line.endswith('\\'):
990 line = line[:-1] + raw_input(ps2)
990 line = line[:-1] + raw_input(ps2)
991 return line
991 return line
992
992
993 #----------------------------------------------------------------------------
993 #----------------------------------------------------------------------------
994 def ask_yes_no(prompt,default=None):
994 def ask_yes_no(prompt,default=None):
995 """Asks a question and returns an integer 1/0 (y/n) answer.
995 """Asks a question and returns an integer 1/0 (y/n) answer.
996
996
997 If default is given (one of 'y','n'), it is used if the user input is
997 If default is given (one of 'y','n'), it is used if the user input is
998 empty. Otherwise the question is repeated until an answer is given.
998 empty. Otherwise the question is repeated until an answer is given.
999 If EOF occurs 20 times consecutively, the default answer is assumed,
999 If EOF occurs 20 times consecutively, the default answer is assumed,
1000 or if there is no default, an exception is raised to prevent infinite
1000 or if there is no default, an exception is raised to prevent infinite
1001 loops.
1001 loops.
1002
1002
1003 Valid answers are: y/yes/n/no (match is not case sensitive)."""
1003 Valid answers are: y/yes/n/no (match is not case sensitive)."""
1004
1004
1005 answers = {'y':True,'n':False,'yes':True,'no':False}
1005 answers = {'y':True,'n':False,'yes':True,'no':False}
1006 ans = None
1006 ans = None
1007 eofs, max_eofs = 0, 20
1007 eofs, max_eofs = 0, 20
1008 while ans not in answers.keys():
1008 while ans not in answers.keys():
1009 try:
1009 try:
1010 ans = raw_input(prompt+' ').lower()
1010 ans = raw_input(prompt+' ').lower()
1011 if not ans: # response was an empty string
1011 if not ans: # response was an empty string
1012 ans = default
1012 ans = default
1013 eofs = 0
1013 eofs = 0
1014 except (EOFError,KeyboardInterrupt):
1014 except (EOFError,KeyboardInterrupt):
1015 eofs = eofs + 1
1015 eofs = eofs + 1
1016 if eofs >= max_eofs:
1016 if eofs >= max_eofs:
1017 if default in answers.keys():
1017 if default in answers.keys():
1018 ans = default
1018 ans = default
1019 else:
1019 else:
1020 raise
1020 raise
1021
1021
1022 return answers[ans]
1022 return answers[ans]
1023
1023
1024 #----------------------------------------------------------------------------
1024 #----------------------------------------------------------------------------
1025 def marquee(txt='',width=78,mark='*'):
1025 def marquee(txt='',width=78,mark='*'):
1026 """Return the input string centered in a 'marquee'."""
1026 """Return the input string centered in a 'marquee'."""
1027 if not txt:
1027 if not txt:
1028 return (mark*width)[:width]
1028 return (mark*width)[:width]
1029 nmark = (width-len(txt)-2)/len(mark)/2
1029 nmark = (width-len(txt)-2)/len(mark)/2
1030 if nmark < 0: nmark =0
1030 if nmark < 0: nmark =0
1031 marks = mark*nmark
1031 marks = mark*nmark
1032 return '%s %s %s' % (marks,txt,marks)
1032 return '%s %s %s' % (marks,txt,marks)
1033
1033
1034 #----------------------------------------------------------------------------
1034 #----------------------------------------------------------------------------
1035 class EvalDict:
1035 class EvalDict:
1036 """
1036 """
1037 Emulate a dict which evaluates its contents in the caller's frame.
1037 Emulate a dict which evaluates its contents in the caller's frame.
1038
1038
1039 Usage:
1039 Usage:
1040 >>>number = 19
1040 >>>number = 19
1041 >>>text = "python"
1041 >>>text = "python"
1042 >>>print "%(text.capitalize())s %(number/9.0).1f rules!" % EvalDict()
1042 >>>print "%(text.capitalize())s %(number/9.0).1f rules!" % EvalDict()
1043 """
1043 """
1044
1044
1045 # This version is due to sismex01@hebmex.com on c.l.py, and is basically a
1045 # This version is due to sismex01@hebmex.com on c.l.py, and is basically a
1046 # modified (shorter) version of:
1046 # modified (shorter) version of:
1047 # http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/66018 by
1047 # http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/66018 by
1048 # Skip Montanaro (skip@pobox.com).
1048 # Skip Montanaro (skip@pobox.com).
1049
1049
1050 def __getitem__(self, name):
1050 def __getitem__(self, name):
1051 frame = sys._getframe(1)
1051 frame = sys._getframe(1)
1052 return eval(name, frame.f_globals, frame.f_locals)
1052 return eval(name, frame.f_globals, frame.f_locals)
1053
1053
1054 EvalString = EvalDict # for backwards compatibility
1054 EvalString = EvalDict # for backwards compatibility
1055 #----------------------------------------------------------------------------
1055 #----------------------------------------------------------------------------
1056 def qw(words,flat=0,sep=None,maxsplit=-1):
1056 def qw(words,flat=0,sep=None,maxsplit=-1):
1057 """Similar to Perl's qw() operator, but with some more options.
1057 """Similar to Perl's qw() operator, but with some more options.
1058
1058
1059 qw(words,flat=0,sep=' ',maxsplit=-1) -> words.split(sep,maxsplit)
1059 qw(words,flat=0,sep=' ',maxsplit=-1) -> words.split(sep,maxsplit)
1060
1060
1061 words can also be a list itself, and with flat=1, the output will be
1061 words can also be a list itself, and with flat=1, the output will be
1062 recursively flattened. Examples:
1062 recursively flattened. Examples:
1063
1063
1064 >>> qw('1 2')
1064 >>> qw('1 2')
1065 ['1', '2']
1065 ['1', '2']
1066 >>> qw(['a b','1 2',['m n','p q']])
1066 >>> qw(['a b','1 2',['m n','p q']])
1067 [['a', 'b'], ['1', '2'], [['m', 'n'], ['p', 'q']]]
1067 [['a', 'b'], ['1', '2'], [['m', 'n'], ['p', 'q']]]
1068 >>> qw(['a b','1 2',['m n','p q']],flat=1)
1068 >>> qw(['a b','1 2',['m n','p q']],flat=1)
1069 ['a', 'b', '1', '2', 'm', 'n', 'p', 'q'] """
1069 ['a', 'b', '1', '2', 'm', 'n', 'p', 'q'] """
1070
1070
1071 if type(words) in StringTypes:
1071 if type(words) in StringTypes:
1072 return [word.strip() for word in words.split(sep,maxsplit)
1072 return [word.strip() for word in words.split(sep,maxsplit)
1073 if word and not word.isspace() ]
1073 if word and not word.isspace() ]
1074 if flat:
1074 if flat:
1075 return flatten(map(qw,words,[1]*len(words)))
1075 return flatten(map(qw,words,[1]*len(words)))
1076 return map(qw,words)
1076 return map(qw,words)
1077
1077
1078 #----------------------------------------------------------------------------
1078 #----------------------------------------------------------------------------
1079 def qwflat(words,sep=None,maxsplit=-1):
1079 def qwflat(words,sep=None,maxsplit=-1):
1080 """Calls qw(words) in flat mode. It's just a convenient shorthand."""
1080 """Calls qw(words) in flat mode. It's just a convenient shorthand."""
1081 return qw(words,1,sep,maxsplit)
1081 return qw(words,1,sep,maxsplit)
1082
1082
1083 #----------------------------------------------------------------------------
1083 #----------------------------------------------------------------------------
1084 def qw_lol(indata):
1084 def qw_lol(indata):
1085 """qw_lol('a b') -> [['a','b']],
1085 """qw_lol('a b') -> [['a','b']],
1086 otherwise it's just a call to qw().
1086 otherwise it's just a call to qw().
1087
1087
1088 We need this to make sure the modules_some keys *always* end up as a
1088 We need this to make sure the modules_some keys *always* end up as a
1089 list of lists."""
1089 list of lists."""
1090
1090
1091 if type(indata) in StringTypes:
1091 if type(indata) in StringTypes:
1092 return [qw(indata)]
1092 return [qw(indata)]
1093 else:
1093 else:
1094 return qw(indata)
1094 return qw(indata)
1095
1095
1096 #-----------------------------------------------------------------------------
1096 #-----------------------------------------------------------------------------
1097 def list_strings(arg):
1097 def list_strings(arg):
1098 """Always return a list of strings, given a string or list of strings
1098 """Always return a list of strings, given a string or list of strings
1099 as input."""
1099 as input."""
1100
1100
1101 if type(arg) in StringTypes: return [arg]
1101 if type(arg) in StringTypes: return [arg]
1102 else: return arg
1102 else: return arg
1103
1103
1104 #----------------------------------------------------------------------------
1104 #----------------------------------------------------------------------------
1105 def grep(pat,list,case=1):
1105 def grep(pat,list,case=1):
1106 """Simple minded grep-like function.
1106 """Simple minded grep-like function.
1107 grep(pat,list) returns occurrences of pat in list, None on failure.
1107 grep(pat,list) returns occurrences of pat in list, None on failure.
1108
1108
1109 It only does simple string matching, with no support for regexps. Use the
1109 It only does simple string matching, with no support for regexps. Use the
1110 option case=0 for case-insensitive matching."""
1110 option case=0 for case-insensitive matching."""
1111
1111
1112 # This is pretty crude. At least it should implement copying only references
1112 # This is pretty crude. At least it should implement copying only references
1113 # to the original data in case it's big. Now it copies the data for output.
1113 # to the original data in case it's big. Now it copies the data for output.
1114 out=[]
1114 out=[]
1115 if case:
1115 if case:
1116 for term in list:
1116 for term in list:
1117 if term.find(pat)>-1: out.append(term)
1117 if term.find(pat)>-1: out.append(term)
1118 else:
1118 else:
1119 lpat=pat.lower()
1119 lpat=pat.lower()
1120 for term in list:
1120 for term in list:
1121 if term.lower().find(lpat)>-1: out.append(term)
1121 if term.lower().find(lpat)>-1: out.append(term)
1122
1122
1123 if len(out): return out
1123 if len(out): return out
1124 else: return None
1124 else: return None
1125
1125
1126 #----------------------------------------------------------------------------
1126 #----------------------------------------------------------------------------
1127 def dgrep(pat,*opts):
1127 def dgrep(pat,*opts):
1128 """Return grep() on dir()+dir(__builtins__).
1128 """Return grep() on dir()+dir(__builtins__).
1129
1129
1130 A very common use of grep() when working interactively."""
1130 A very common use of grep() when working interactively."""
1131
1131
1132 return grep(pat,dir(__main__)+dir(__main__.__builtins__),*opts)
1132 return grep(pat,dir(__main__)+dir(__main__.__builtins__),*opts)
1133
1133
1134 #----------------------------------------------------------------------------
1134 #----------------------------------------------------------------------------
1135 def idgrep(pat):
1135 def idgrep(pat):
1136 """Case-insensitive dgrep()"""
1136 """Case-insensitive dgrep()"""
1137
1137
1138 return dgrep(pat,0)
1138 return dgrep(pat,0)
1139
1139
1140 #----------------------------------------------------------------------------
1140 #----------------------------------------------------------------------------
1141 def igrep(pat,list):
1141 def igrep(pat,list):
1142 """Synonym for case-insensitive grep."""
1142 """Synonym for case-insensitive grep."""
1143
1143
1144 return grep(pat,list,case=0)
1144 return grep(pat,list,case=0)
1145
1145
1146 #----------------------------------------------------------------------------
1146 #----------------------------------------------------------------------------
1147 def indent(str,nspaces=4,ntabs=0):
1147 def indent(str,nspaces=4,ntabs=0):
1148 """Indent a string a given number of spaces or tabstops.
1148 """Indent a string a given number of spaces or tabstops.
1149
1149
1150 indent(str,nspaces=4,ntabs=0) -> indent str by ntabs+nspaces.
1150 indent(str,nspaces=4,ntabs=0) -> indent str by ntabs+nspaces.
1151 """
1151 """
1152 if str is None:
1152 if str is None:
1153 return
1153 return
1154 ind = '\t'*ntabs+' '*nspaces
1154 ind = '\t'*ntabs+' '*nspaces
1155 outstr = '%s%s' % (ind,str.replace(os.linesep,os.linesep+ind))
1155 outstr = '%s%s' % (ind,str.replace(os.linesep,os.linesep+ind))
1156 if outstr.endswith(os.linesep+ind):
1156 if outstr.endswith(os.linesep+ind):
1157 return outstr[:-len(ind)]
1157 return outstr[:-len(ind)]
1158 else:
1158 else:
1159 return outstr
1159 return outstr
1160
1160
1161 #-----------------------------------------------------------------------------
1161 #-----------------------------------------------------------------------------
1162 def native_line_ends(filename,backup=1):
1162 def native_line_ends(filename,backup=1):
1163 """Convert (in-place) a file to line-ends native to the current OS.
1163 """Convert (in-place) a file to line-ends native to the current OS.
1164
1164
1165 If the optional backup argument is given as false, no backup of the
1165 If the optional backup argument is given as false, no backup of the
1166 original file is left. """
1166 original file is left. """
1167
1167
1168 backup_suffixes = {'posix':'~','dos':'.bak','nt':'.bak','mac':'.bak'}
1168 backup_suffixes = {'posix':'~','dos':'.bak','nt':'.bak','mac':'.bak'}
1169
1169
1170 bak_filename = filename + backup_suffixes[os.name]
1170 bak_filename = filename + backup_suffixes[os.name]
1171
1171
1172 original = open(filename).read()
1172 original = open(filename).read()
1173 shutil.copy2(filename,bak_filename)
1173 shutil.copy2(filename,bak_filename)
1174 try:
1174 try:
1175 new = open(filename,'wb')
1175 new = open(filename,'wb')
1176 new.write(os.linesep.join(original.splitlines()))
1176 new.write(os.linesep.join(original.splitlines()))
1177 new.write(os.linesep) # ALWAYS put an eol at the end of the file
1177 new.write(os.linesep) # ALWAYS put an eol at the end of the file
1178 new.close()
1178 new.close()
1179 except:
1179 except:
1180 os.rename(bak_filename,filename)
1180 os.rename(bak_filename,filename)
1181 if not backup:
1181 if not backup:
1182 try:
1182 try:
1183 os.remove(bak_filename)
1183 os.remove(bak_filename)
1184 except:
1184 except:
1185 pass
1185 pass
1186
1186
1187 #----------------------------------------------------------------------------
1187 #----------------------------------------------------------------------------
1188 def get_pager_cmd(pager_cmd = None):
1188 def get_pager_cmd(pager_cmd = None):
1189 """Return a pager command.
1189 """Return a pager command.
1190
1190
1191 Makes some attempts at finding an OS-correct one."""
1191 Makes some attempts at finding an OS-correct one."""
1192
1192
1193 if os.name == 'posix':
1193 if os.name == 'posix':
1194 default_pager_cmd = 'less -r' # -r for color control sequences
1194 default_pager_cmd = 'less -r' # -r for color control sequences
1195 elif os.name in ['nt','dos']:
1195 elif os.name in ['nt','dos']:
1196 default_pager_cmd = 'type'
1196 default_pager_cmd = 'type'
1197
1197
1198 if pager_cmd is None:
1198 if pager_cmd is None:
1199 try:
1199 try:
1200 pager_cmd = os.environ['PAGER']
1200 pager_cmd = os.environ['PAGER']
1201 except:
1201 except:
1202 pager_cmd = default_pager_cmd
1202 pager_cmd = default_pager_cmd
1203 return pager_cmd
1203 return pager_cmd
1204
1204
1205 #-----------------------------------------------------------------------------
1205 #-----------------------------------------------------------------------------
1206 def get_pager_start(pager,start):
1206 def get_pager_start(pager,start):
1207 """Return the string for paging files with an offset.
1207 """Return the string for paging files with an offset.
1208
1208
1209 This is the '+N' argument which less and more (under Unix) accept.
1209 This is the '+N' argument which less and more (under Unix) accept.
1210 """
1210 """
1211
1211
1212 if pager in ['less','more']:
1212 if pager in ['less','more']:
1213 if start:
1213 if start:
1214 start_string = '+' + str(start)
1214 start_string = '+' + str(start)
1215 else:
1215 else:
1216 start_string = ''
1216 start_string = ''
1217 else:
1217 else:
1218 start_string = ''
1218 start_string = ''
1219 return start_string
1219 return start_string
1220
1220
1221 #----------------------------------------------------------------------------
1221 #----------------------------------------------------------------------------
1222 if os.name == "nt":
1222 if os.name == "nt":
1223 import msvcrt
1223 import msvcrt
1224 def page_more():
1224 def page_more():
1225 """ Smart pausing between pages
1225 """ Smart pausing between pages
1226
1226
1227 @return: True if need print more lines, False if quit
1227 @return: True if need print more lines, False if quit
1228 """
1228 """
1229 Term.cout.write('---Return to continue, q to quit--- ')
1229 Term.cout.write('---Return to continue, q to quit--- ')
1230 ans = msvcrt.getch()
1230 ans = msvcrt.getch()
1231 if ans in ("q", "Q"):
1231 if ans in ("q", "Q"):
1232 result = False
1232 result = False
1233 else:
1233 else:
1234 result = True
1234 result = True
1235 Term.cout.write("\b"*37 + " "*37 + "\b"*37)
1235 Term.cout.write("\b"*37 + " "*37 + "\b"*37)
1236 return result
1236 return result
1237 else:
1237 else:
1238 def page_more():
1238 def page_more():
1239 ans = raw_input('---Return to continue, q to quit--- ')
1239 ans = raw_input('---Return to continue, q to quit--- ')
1240 if ans.lower().startswith('q'):
1240 if ans.lower().startswith('q'):
1241 return False
1241 return False
1242 else:
1242 else:
1243 return True
1243 return True
1244
1244
1245 esc_re = re.compile(r"(\x1b[^m]+m)")
1245 esc_re = re.compile(r"(\x1b[^m]+m)")
1246
1246
1247 def page_dumb(strng,start=0,screen_lines=25):
1247 def page_dumb(strng,start=0,screen_lines=25):
1248 """Very dumb 'pager' in Python, for when nothing else works.
1248 """Very dumb 'pager' in Python, for when nothing else works.
1249
1249
1250 Only moves forward, same interface as page(), except for pager_cmd and
1250 Only moves forward, same interface as page(), except for pager_cmd and
1251 mode."""
1251 mode."""
1252
1252
1253 out_ln = strng.splitlines()[start:]
1253 out_ln = strng.splitlines()[start:]
1254 screens = chop(out_ln,screen_lines-1)
1254 screens = chop(out_ln,screen_lines-1)
1255 if len(screens) == 1:
1255 if len(screens) == 1:
1256 print >>Term.cout, os.linesep.join(screens[0])
1256 print >>Term.cout, os.linesep.join(screens[0])
1257 else:
1257 else:
1258 last_escape = ""
1258 last_escape = ""
1259 for scr in screens[0:-1]:
1259 for scr in screens[0:-1]:
1260 hunk = os.linesep.join(scr)
1260 hunk = os.linesep.join(scr)
1261 print >>Term.cout, last_escape + hunk
1261 print >>Term.cout, last_escape + hunk
1262 if not page_more():
1262 if not page_more():
1263 return
1263 return
1264 esc_list = esc_re.findall(hunk)
1264 esc_list = esc_re.findall(hunk)
1265 if len(esc_list) > 0:
1265 if len(esc_list) > 0:
1266 last_escape = esc_list[-1]
1266 last_escape = esc_list[-1]
1267 print >>Term.cout, last_escape + os.linesep.join(screens[-1])
1267 print >>Term.cout, last_escape + os.linesep.join(screens[-1])
1268
1268
1269 #----------------------------------------------------------------------------
1269 #----------------------------------------------------------------------------
1270 def page(strng,start=0,screen_lines=0,pager_cmd = None):
1270 def page(strng,start=0,screen_lines=0,pager_cmd = None):
1271 """Print a string, piping through a pager after a certain length.
1271 """Print a string, piping through a pager after a certain length.
1272
1272
1273 The screen_lines parameter specifies the number of *usable* lines of your
1273 The screen_lines parameter specifies the number of *usable* lines of your
1274 terminal screen (total lines minus lines you need to reserve to show other
1274 terminal screen (total lines minus lines you need to reserve to show other
1275 information).
1275 information).
1276
1276
1277 If you set screen_lines to a number <=0, page() will try to auto-determine
1277 If you set screen_lines to a number <=0, page() will try to auto-determine
1278 your screen size and will only use up to (screen_size+screen_lines) for
1278 your screen size and will only use up to (screen_size+screen_lines) for
1279 printing, paging after that. That is, if you want auto-detection but need
1279 printing, paging after that. That is, if you want auto-detection but need
1280 to reserve the bottom 3 lines of the screen, use screen_lines = -3, and for
1280 to reserve the bottom 3 lines of the screen, use screen_lines = -3, and for
1281 auto-detection without any lines reserved simply use screen_lines = 0.
1281 auto-detection without any lines reserved simply use screen_lines = 0.
1282
1282
1283 If a string won't fit in the allowed lines, it is sent through the
1283 If a string won't fit in the allowed lines, it is sent through the
1284 specified pager command. If none given, look for PAGER in the environment,
1284 specified pager command. If none given, look for PAGER in the environment,
1285 and ultimately default to less.
1285 and ultimately default to less.
1286
1286
1287 If no system pager works, the string is sent through a 'dumb pager'
1287 If no system pager works, the string is sent through a 'dumb pager'
1288 written in python, very simplistic.
1288 written in python, very simplistic.
1289 """
1289 """
1290
1290
1291 # Ugly kludge, but calling curses.initscr() flat out crashes in emacs
1291 # Ugly kludge, but calling curses.initscr() flat out crashes in emacs
1292 TERM = os.environ.get('TERM','dumb')
1292 TERM = os.environ.get('TERM','dumb')
1293 if TERM in ['dumb','emacs'] and os.name != 'nt':
1293 if TERM in ['dumb','emacs'] and os.name != 'nt':
1294 print strng
1294 print strng
1295 return
1295 return
1296 # chop off the topmost part of the string we don't want to see
1296 # chop off the topmost part of the string we don't want to see
1297 str_lines = strng.split(os.linesep)[start:]
1297 str_lines = strng.split(os.linesep)[start:]
1298 str_toprint = os.linesep.join(str_lines)
1298 str_toprint = os.linesep.join(str_lines)
1299 num_newlines = len(str_lines)
1299 num_newlines = len(str_lines)
1300 len_str = len(str_toprint)
1300 len_str = len(str_toprint)
1301
1301
1302 # Dumb heuristics to guesstimate number of on-screen lines the string
1302 # Dumb heuristics to guesstimate number of on-screen lines the string
1303 # takes. Very basic, but good enough for docstrings in reasonable
1303 # takes. Very basic, but good enough for docstrings in reasonable
1304 # terminals. If someone later feels like refining it, it's not hard.
1304 # terminals. If someone later feels like refining it, it's not hard.
1305 numlines = max(num_newlines,int(len_str/80)+1)
1305 numlines = max(num_newlines,int(len_str/80)+1)
1306
1306
1307 if os.name == "nt":
1307 if os.name == "nt":
1308 screen_lines_def = get_console_size(defaulty=25)[1]
1308 screen_lines_def = get_console_size(defaulty=25)[1]
1309 else:
1309 else:
1310 screen_lines_def = 25 # default value if we can't auto-determine
1310 screen_lines_def = 25 # default value if we can't auto-determine
1311
1311
1312 # auto-determine screen size
1312 # auto-determine screen size
1313 if screen_lines <= 0:
1313 if screen_lines <= 0:
1314 if TERM=='xterm':
1314 if TERM=='xterm':
1315 try:
1315 try:
1316 import curses
1316 import curses
1317 if hasattr(curses,'initscr'):
1317 if hasattr(curses,'initscr'):
1318 use_curses = 1
1318 use_curses = 1
1319 else:
1319 else:
1320 use_curses = 0
1320 use_curses = 0
1321 except ImportError:
1321 except ImportError:
1322 use_curses = 0
1322 use_curses = 0
1323 else:
1323 else:
1324 # curses causes problems on many terminals other than xterm.
1324 # curses causes problems on many terminals other than xterm.
1325 use_curses = 0
1325 use_curses = 0
1326 if use_curses:
1326 if use_curses:
1327 scr = curses.initscr()
1327 scr = curses.initscr()
1328 screen_lines_real,screen_cols = scr.getmaxyx()
1328 screen_lines_real,screen_cols = scr.getmaxyx()
1329 curses.endwin()
1329 curses.endwin()
1330 screen_lines += screen_lines_real
1330 screen_lines += screen_lines_real
1331 #print '***Screen size:',screen_lines_real,'lines x',\
1331 #print '***Screen size:',screen_lines_real,'lines x',\
1332 #screen_cols,'columns.' # dbg
1332 #screen_cols,'columns.' # dbg
1333 else:
1333 else:
1334 screen_lines += screen_lines_def
1334 screen_lines += screen_lines_def
1335
1335
1336 #print 'numlines',numlines,'screenlines',screen_lines # dbg
1336 #print 'numlines',numlines,'screenlines',screen_lines # dbg
1337 if numlines <= screen_lines :
1337 if numlines <= screen_lines :
1338 #print '*** normal print' # dbg
1338 #print '*** normal print' # dbg
1339 print >>Term.cout, str_toprint
1339 print >>Term.cout, str_toprint
1340 else:
1340 else:
1341 # Try to open pager and default to internal one if that fails.
1341 # Try to open pager and default to internal one if that fails.
1342 # All failure modes are tagged as 'retval=1', to match the return
1342 # All failure modes are tagged as 'retval=1', to match the return
1343 # value of a failed system command. If any intermediate attempt
1343 # value of a failed system command. If any intermediate attempt
1344 # sets retval to 1, at the end we resort to our own page_dumb() pager.
1344 # sets retval to 1, at the end we resort to our own page_dumb() pager.
1345 pager_cmd = get_pager_cmd(pager_cmd)
1345 pager_cmd = get_pager_cmd(pager_cmd)
1346 pager_cmd += ' ' + get_pager_start(pager_cmd,start)
1346 pager_cmd += ' ' + get_pager_start(pager_cmd,start)
1347 if os.name == 'nt':
1347 if os.name == 'nt':
1348 if pager_cmd.startswith('type'):
1348 if pager_cmd.startswith('type'):
1349 # The default WinXP 'type' command is failing on complex strings.
1349 # The default WinXP 'type' command is failing on complex strings.
1350 retval = 1
1350 retval = 1
1351 else:
1351 else:
1352 tmpname = tempfile.mktemp('.txt')
1352 tmpname = tempfile.mktemp('.txt')
1353 tmpfile = file(tmpname,'wt')
1353 tmpfile = file(tmpname,'wt')
1354 tmpfile.write(strng)
1354 tmpfile.write(strng)
1355 tmpfile.close()
1355 tmpfile.close()
1356 cmd = "%s < %s" % (pager_cmd,tmpname)
1356 cmd = "%s < %s" % (pager_cmd,tmpname)
1357 if os.system(cmd):
1357 if os.system(cmd):
1358 retval = 1
1358 retval = 1
1359 else:
1359 else:
1360 retval = None
1360 retval = None
1361 os.remove(tmpname)
1361 os.remove(tmpname)
1362 else:
1362 else:
1363 try:
1363 try:
1364 retval = None
1364 retval = None
1365 # if I use popen4, things hang. No idea why.
1365 # if I use popen4, things hang. No idea why.
1366 #pager,shell_out = os.popen4(pager_cmd)
1366 #pager,shell_out = os.popen4(pager_cmd)
1367 pager = os.popen(pager_cmd,'w')
1367 pager = os.popen(pager_cmd,'w')
1368 pager.write(strng)
1368 pager.write(strng)
1369 pager.close()
1369 pager.close()
1370 retval = pager.close() # success returns None
1370 retval = pager.close() # success returns None
1371 except IOError,msg: # broken pipe when user quits
1371 except IOError,msg: # broken pipe when user quits
1372 if msg.args == (32,'Broken pipe'):
1372 if msg.args == (32,'Broken pipe'):
1373 retval = None
1373 retval = None
1374 else:
1374 else:
1375 retval = 1
1375 retval = 1
1376 except OSError:
1376 except OSError:
1377 # Other strange problems, sometimes seen in Win2k/cygwin
1377 # Other strange problems, sometimes seen in Win2k/cygwin
1378 retval = 1
1378 retval = 1
1379 if retval is not None:
1379 if retval is not None:
1380 page_dumb(strng,screen_lines=screen_lines)
1380 page_dumb(strng,screen_lines=screen_lines)
1381
1381
1382 #----------------------------------------------------------------------------
1382 #----------------------------------------------------------------------------
1383 def page_file(fname,start = 0, pager_cmd = None):
1383 def page_file(fname,start = 0, pager_cmd = None):
1384 """Page a file, using an optional pager command and starting line.
1384 """Page a file, using an optional pager command and starting line.
1385 """
1385 """
1386
1386
1387 pager_cmd = get_pager_cmd(pager_cmd)
1387 pager_cmd = get_pager_cmd(pager_cmd)
1388 pager_cmd += ' ' + get_pager_start(pager_cmd,start)
1388 pager_cmd += ' ' + get_pager_start(pager_cmd,start)
1389
1389
1390 try:
1390 try:
1391 if os.environ['TERM'] in ['emacs','dumb']:
1391 if os.environ['TERM'] in ['emacs','dumb']:
1392 raise EnvironmentError
1392 raise EnvironmentError
1393 xsys(pager_cmd + ' ' + fname)
1393 xsys(pager_cmd + ' ' + fname)
1394 except:
1394 except:
1395 try:
1395 try:
1396 if start > 0:
1396 if start > 0:
1397 start -= 1
1397 start -= 1
1398 page(open(fname).read(),start)
1398 page(open(fname).read(),start)
1399 except:
1399 except:
1400 print 'Unable to show file',`fname`
1400 print 'Unable to show file',`fname`
1401
1401
1402 #----------------------------------------------------------------------------
1402 #----------------------------------------------------------------------------
1403 def snip_print(str,width = 75,print_full = 0,header = ''):
1403 def snip_print(str,width = 75,print_full = 0,header = ''):
1404 """Print a string snipping the midsection to fit in width.
1404 """Print a string snipping the midsection to fit in width.
1405
1405
1406 print_full: mode control:
1406 print_full: mode control:
1407 - 0: only snip long strings
1407 - 0: only snip long strings
1408 - 1: send to page() directly.
1408 - 1: send to page() directly.
1409 - 2: snip long strings and ask for full length viewing with page()
1409 - 2: snip long strings and ask for full length viewing with page()
1410 Return 1 if snipping was necessary, 0 otherwise."""
1410 Return 1 if snipping was necessary, 0 otherwise."""
1411
1411
1412 if print_full == 1:
1412 if print_full == 1:
1413 page(header+str)
1413 page(header+str)
1414 return 0
1414 return 0
1415
1415
1416 print header,
1416 print header,
1417 if len(str) < width:
1417 if len(str) < width:
1418 print str
1418 print str
1419 snip = 0
1419 snip = 0
1420 else:
1420 else:
1421 whalf = int((width -5)/2)
1421 whalf = int((width -5)/2)
1422 print str[:whalf] + ' <...> ' + str[-whalf:]
1422 print str[:whalf] + ' <...> ' + str[-whalf:]
1423 snip = 1
1423 snip = 1
1424 if snip and print_full == 2:
1424 if snip and print_full == 2:
1425 if raw_input(header+' Snipped. View (y/n)? [N]').lower() == 'y':
1425 if raw_input(header+' Snipped. View (y/n)? [N]').lower() == 'y':
1426 page(str)
1426 page(str)
1427 return snip
1427 return snip
1428
1428
1429 #****************************************************************************
1429 #****************************************************************************
1430 # lists, dicts and structures
1430 # lists, dicts and structures
1431
1431
1432 def belong(candidates,checklist):
1432 def belong(candidates,checklist):
1433 """Check whether a list of items appear in a given list of options.
1433 """Check whether a list of items appear in a given list of options.
1434
1434
1435 Returns a list of 1 and 0, one for each candidate given."""
1435 Returns a list of 1 and 0, one for each candidate given."""
1436
1436
1437 return [x in checklist for x in candidates]
1437 return [x in checklist for x in candidates]
1438
1438
1439 #----------------------------------------------------------------------------
1439 #----------------------------------------------------------------------------
1440 def uniq_stable(elems):
1440 def uniq_stable(elems):
1441 """uniq_stable(elems) -> list
1441 """uniq_stable(elems) -> list
1442
1442
1443 Return from an iterable, a list of all the unique elements in the input,
1443 Return from an iterable, a list of all the unique elements in the input,
1444 but maintaining the order in which they first appear.
1444 but maintaining the order in which they first appear.
1445
1445
1446 A naive solution to this problem which just makes a dictionary with the
1446 A naive solution to this problem which just makes a dictionary with the
1447 elements as keys fails to respect the stability condition, since
1447 elements as keys fails to respect the stability condition, since
1448 dictionaries are unsorted by nature.
1448 dictionaries are unsorted by nature.
1449
1449
1450 Note: All elements in the input must be valid dictionary keys for this
1450 Note: All elements in the input must be valid dictionary keys for this
1451 routine to work, as it internally uses a dictionary for efficiency
1451 routine to work, as it internally uses a dictionary for efficiency
1452 reasons."""
1452 reasons."""
1453
1453
1454 unique = []
1454 unique = []
1455 unique_dict = {}
1455 unique_dict = {}
1456 for nn in elems:
1456 for nn in elems:
1457 if nn not in unique_dict:
1457 if nn not in unique_dict:
1458 unique.append(nn)
1458 unique.append(nn)
1459 unique_dict[nn] = None
1459 unique_dict[nn] = None
1460 return unique
1460 return unique
1461
1461
1462 #----------------------------------------------------------------------------
1462 #----------------------------------------------------------------------------
1463 class NLprinter:
1463 class NLprinter:
1464 """Print an arbitrarily nested list, indicating index numbers.
1464 """Print an arbitrarily nested list, indicating index numbers.
1465
1465
1466 An instance of this class called nlprint is available and callable as a
1466 An instance of this class called nlprint is available and callable as a
1467 function.
1467 function.
1468
1468
1469 nlprint(list,indent=' ',sep=': ') -> prints indenting each level by 'indent'
1469 nlprint(list,indent=' ',sep=': ') -> prints indenting each level by 'indent'
1470 and using 'sep' to separate the index from the value. """
1470 and using 'sep' to separate the index from the value. """
1471
1471
1472 def __init__(self):
1472 def __init__(self):
1473 self.depth = 0
1473 self.depth = 0
1474
1474
1475 def __call__(self,lst,pos='',**kw):
1475 def __call__(self,lst,pos='',**kw):
1476 """Prints the nested list numbering levels."""
1476 """Prints the nested list numbering levels."""
1477 kw.setdefault('indent',' ')
1477 kw.setdefault('indent',' ')
1478 kw.setdefault('sep',': ')
1478 kw.setdefault('sep',': ')
1479 kw.setdefault('start',0)
1479 kw.setdefault('start',0)
1480 kw.setdefault('stop',len(lst))
1480 kw.setdefault('stop',len(lst))
1481 # we need to remove start and stop from kw so they don't propagate
1481 # we need to remove start and stop from kw so they don't propagate
1482 # into a recursive call for a nested list.
1482 # into a recursive call for a nested list.
1483 start = kw['start']; del kw['start']
1483 start = kw['start']; del kw['start']
1484 stop = kw['stop']; del kw['stop']
1484 stop = kw['stop']; del kw['stop']
1485 if self.depth == 0 and 'header' in kw.keys():
1485 if self.depth == 0 and 'header' in kw.keys():
1486 print kw['header']
1486 print kw['header']
1487
1487
1488 for idx in range(start,stop):
1488 for idx in range(start,stop):
1489 elem = lst[idx]
1489 elem = lst[idx]
1490 if type(elem)==type([]):
1490 if type(elem)==type([]):
1491 self.depth += 1
1491 self.depth += 1
1492 self.__call__(elem,itpl('$pos$idx,'),**kw)
1492 self.__call__(elem,itpl('$pos$idx,'),**kw)
1493 self.depth -= 1
1493 self.depth -= 1
1494 else:
1494 else:
1495 printpl(kw['indent']*self.depth+'$pos$idx$kw["sep"]$elem')
1495 printpl(kw['indent']*self.depth+'$pos$idx$kw["sep"]$elem')
1496
1496
1497 nlprint = NLprinter()
1497 nlprint = NLprinter()
1498 #----------------------------------------------------------------------------
1498 #----------------------------------------------------------------------------
1499 def all_belong(candidates,checklist):
1499 def all_belong(candidates,checklist):
1500 """Check whether a list of items ALL appear in a given list of options.
1500 """Check whether a list of items ALL appear in a given list of options.
1501
1501
1502 Returns a single 1 or 0 value."""
1502 Returns a single 1 or 0 value."""
1503
1503
1504 return 1-(0 in [x in checklist for x in candidates])
1504 return 1-(0 in [x in checklist for x in candidates])
1505
1505
1506 #----------------------------------------------------------------------------
1506 #----------------------------------------------------------------------------
1507 def sort_compare(lst1,lst2,inplace = 1):
1507 def sort_compare(lst1,lst2,inplace = 1):
1508 """Sort and compare two lists.
1508 """Sort and compare two lists.
1509
1509
1510 By default it does it in place, thus modifying the lists. Use inplace = 0
1510 By default it does it in place, thus modifying the lists. Use inplace = 0
1511 to avoid that (at the cost of temporary copy creation)."""
1511 to avoid that (at the cost of temporary copy creation)."""
1512 if not inplace:
1512 if not inplace:
1513 lst1 = lst1[:]
1513 lst1 = lst1[:]
1514 lst2 = lst2[:]
1514 lst2 = lst2[:]
1515 lst1.sort(); lst2.sort()
1515 lst1.sort(); lst2.sort()
1516 return lst1 == lst2
1516 return lst1 == lst2
1517
1517
1518 #----------------------------------------------------------------------------
1518 #----------------------------------------------------------------------------
1519 def mkdict(**kwargs):
1519 def mkdict(**kwargs):
1520 """Return a dict from a keyword list.
1520 """Return a dict from a keyword list.
1521
1521
1522 It's just syntactic sugar for making ditcionary creation more convenient:
1522 It's just syntactic sugar for making ditcionary creation more convenient:
1523 # the standard way
1523 # the standard way
1524 >>>data = { 'red' : 1, 'green' : 2, 'blue' : 3 }
1524 >>>data = { 'red' : 1, 'green' : 2, 'blue' : 3 }
1525 # a cleaner way
1525 # a cleaner way
1526 >>>data = dict(red=1, green=2, blue=3)
1526 >>>data = dict(red=1, green=2, blue=3)
1527
1527
1528 If you need more than this, look at the Struct() class."""
1528 If you need more than this, look at the Struct() class."""
1529
1529
1530 return kwargs
1530 return kwargs
1531
1531
1532 #----------------------------------------------------------------------------
1532 #----------------------------------------------------------------------------
1533 def list2dict(lst):
1533 def list2dict(lst):
1534 """Takes a list of (key,value) pairs and turns it into a dict."""
1534 """Takes a list of (key,value) pairs and turns it into a dict."""
1535
1535
1536 dic = {}
1536 dic = {}
1537 for k,v in lst: dic[k] = v
1537 for k,v in lst: dic[k] = v
1538 return dic
1538 return dic
1539
1539
1540 #----------------------------------------------------------------------------
1540 #----------------------------------------------------------------------------
1541 def list2dict2(lst,default=''):
1541 def list2dict2(lst,default=''):
1542 """Takes a list and turns it into a dict.
1542 """Takes a list and turns it into a dict.
1543 Much slower than list2dict, but more versatile. This version can take
1543 Much slower than list2dict, but more versatile. This version can take
1544 lists with sublists of arbitrary length (including sclars)."""
1544 lists with sublists of arbitrary length (including sclars)."""
1545
1545
1546 dic = {}
1546 dic = {}
1547 for elem in lst:
1547 for elem in lst:
1548 if type(elem) in (types.ListType,types.TupleType):
1548 if type(elem) in (types.ListType,types.TupleType):
1549 size = len(elem)
1549 size = len(elem)
1550 if size == 0:
1550 if size == 0:
1551 pass
1551 pass
1552 elif size == 1:
1552 elif size == 1:
1553 dic[elem] = default
1553 dic[elem] = default
1554 else:
1554 else:
1555 k,v = elem[0], elem[1:]
1555 k,v = elem[0], elem[1:]
1556 if len(v) == 1: v = v[0]
1556 if len(v) == 1: v = v[0]
1557 dic[k] = v
1557 dic[k] = v
1558 else:
1558 else:
1559 dic[elem] = default
1559 dic[elem] = default
1560 return dic
1560 return dic
1561
1561
1562 #----------------------------------------------------------------------------
1562 #----------------------------------------------------------------------------
1563 def flatten(seq):
1563 def flatten(seq):
1564 """Flatten a list of lists (NOT recursive, only works for 2d lists)."""
1564 """Flatten a list of lists (NOT recursive, only works for 2d lists)."""
1565
1565
1566 # bug in python??? (YES. Fixed in 2.2, let's leave the kludgy fix in).
1566 # bug in python??? (YES. Fixed in 2.2, let's leave the kludgy fix in).
1567
1567
1568 # if the x=0 isn't made, a *global* variable x is left over after calling
1568 # if the x=0 isn't made, a *global* variable x is left over after calling
1569 # this function, with the value of the last element in the return
1569 # this function, with the value of the last element in the return
1570 # list. This does seem like a bug big time to me.
1570 # list. This does seem like a bug big time to me.
1571
1571
1572 # the problem is fixed with the x=0, which seems to force the creation of
1572 # the problem is fixed with the x=0, which seems to force the creation of
1573 # a local name
1573 # a local name
1574
1574
1575 x = 0
1575 x = 0
1576 return [x for subseq in seq for x in subseq]
1576 return [x for subseq in seq for x in subseq]
1577
1577
1578 #----------------------------------------------------------------------------
1578 #----------------------------------------------------------------------------
1579 def get_slice(seq,start=0,stop=None,step=1):
1579 def get_slice(seq,start=0,stop=None,step=1):
1580 """Get a slice of a sequence with variable step. Specify start,stop,step."""
1580 """Get a slice of a sequence with variable step. Specify start,stop,step."""
1581 if stop == None:
1581 if stop == None:
1582 stop = len(seq)
1582 stop = len(seq)
1583 item = lambda i: seq[i]
1583 item = lambda i: seq[i]
1584 return map(item,xrange(start,stop,step))
1584 return map(item,xrange(start,stop,step))
1585
1585
1586 #----------------------------------------------------------------------------
1586 #----------------------------------------------------------------------------
1587 def chop(seq,size):
1587 def chop(seq,size):
1588 """Chop a sequence into chunks of the given size."""
1588 """Chop a sequence into chunks of the given size."""
1589 chunk = lambda i: seq[i:i+size]
1589 chunk = lambda i: seq[i:i+size]
1590 return map(chunk,xrange(0,len(seq),size))
1590 return map(chunk,xrange(0,len(seq),size))
1591
1591
1592 #----------------------------------------------------------------------------
1592 #----------------------------------------------------------------------------
1593 def with(object, **args):
1593 def with(object, **args):
1594 """Set multiple attributes for an object, similar to Pascal's with.
1594 """Set multiple attributes for an object, similar to Pascal's with.
1595
1595
1596 Example:
1596 Example:
1597 with(jim,
1597 with(jim,
1598 born = 1960,
1598 born = 1960,
1599 haircolour = 'Brown',
1599 haircolour = 'Brown',
1600 eyecolour = 'Green')
1600 eyecolour = 'Green')
1601
1601
1602 Credit: Greg Ewing, in
1602 Credit: Greg Ewing, in
1603 http://mail.python.org/pipermail/python-list/2001-May/040703.html"""
1603 http://mail.python.org/pipermail/python-list/2001-May/040703.html"""
1604
1604
1605 object.__dict__.update(args)
1605 object.__dict__.update(args)
1606
1606
1607 #----------------------------------------------------------------------------
1607 #----------------------------------------------------------------------------
1608 def setattr_list(obj,alist,nspace = None):
1608 def setattr_list(obj,alist,nspace = None):
1609 """Set a list of attributes for an object taken from a namespace.
1609 """Set a list of attributes for an object taken from a namespace.
1610
1610
1611 setattr_list(obj,alist,nspace) -> sets in obj all the attributes listed in
1611 setattr_list(obj,alist,nspace) -> sets in obj all the attributes listed in
1612 alist with their values taken from nspace, which must be a dict (something
1612 alist with their values taken from nspace, which must be a dict (something
1613 like locals() will often do) If nspace isn't given, locals() of the
1613 like locals() will often do) If nspace isn't given, locals() of the
1614 *caller* is used, so in most cases you can omit it.
1614 *caller* is used, so in most cases you can omit it.
1615
1615
1616 Note that alist can be given as a string, which will be automatically
1616 Note that alist can be given as a string, which will be automatically
1617 split into a list on whitespace. If given as a list, it must be a list of
1617 split into a list on whitespace. If given as a list, it must be a list of
1618 *strings* (the variable names themselves), not of variables."""
1618 *strings* (the variable names themselves), not of variables."""
1619
1619
1620 # this grabs the local variables from the *previous* call frame -- that is
1620 # this grabs the local variables from the *previous* call frame -- that is
1621 # the locals from the function that called setattr_list().
1621 # the locals from the function that called setattr_list().
1622 # - snipped from weave.inline()
1622 # - snipped from weave.inline()
1623 if nspace is None:
1623 if nspace is None:
1624 call_frame = sys._getframe().f_back
1624 call_frame = sys._getframe().f_back
1625 nspace = call_frame.f_locals
1625 nspace = call_frame.f_locals
1626
1626
1627 if type(alist) in StringTypes:
1627 if type(alist) in StringTypes:
1628 alist = alist.split()
1628 alist = alist.split()
1629 for attr in alist:
1629 for attr in alist:
1630 val = eval(attr,nspace)
1630 val = eval(attr,nspace)
1631 setattr(obj,attr,val)
1631 setattr(obj,attr,val)
1632
1632
1633 #----------------------------------------------------------------------------
1633 #----------------------------------------------------------------------------
1634 def getattr_list(obj,alist,*args):
1634 def getattr_list(obj,alist,*args):
1635 """getattr_list(obj,alist[, default]) -> attribute list.
1635 """getattr_list(obj,alist[, default]) -> attribute list.
1636
1636
1637 Get a list of named attributes for an object. When a default argument is
1637 Get a list of named attributes for an object. When a default argument is
1638 given, it is returned when the attribute doesn't exist; without it, an
1638 given, it is returned when the attribute doesn't exist; without it, an
1639 exception is raised in that case.
1639 exception is raised in that case.
1640
1640
1641 Note that alist can be given as a string, which will be automatically
1641 Note that alist can be given as a string, which will be automatically
1642 split into a list on whitespace. If given as a list, it must be a list of
1642 split into a list on whitespace. If given as a list, it must be a list of
1643 *strings* (the variable names themselves), not of variables."""
1643 *strings* (the variable names themselves), not of variables."""
1644
1644
1645 if type(alist) in StringTypes:
1645 if type(alist) in StringTypes:
1646 alist = alist.split()
1646 alist = alist.split()
1647 if args:
1647 if args:
1648 if len(args)==1:
1648 if len(args)==1:
1649 default = args[0]
1649 default = args[0]
1650 return map(lambda attr: getattr(obj,attr,default),alist)
1650 return map(lambda attr: getattr(obj,attr,default),alist)
1651 else:
1651 else:
1652 raise ValueError,'getattr_list() takes only one optional argument'
1652 raise ValueError,'getattr_list() takes only one optional argument'
1653 else:
1653 else:
1654 return map(lambda attr: getattr(obj,attr),alist)
1654 return map(lambda attr: getattr(obj,attr),alist)
1655
1655
1656 #----------------------------------------------------------------------------
1656 #----------------------------------------------------------------------------
1657 def map_method(method,object_list,*argseq,**kw):
1657 def map_method(method,object_list,*argseq,**kw):
1658 """map_method(method,object_list,*args,**kw) -> list
1658 """map_method(method,object_list,*args,**kw) -> list
1659
1659
1660 Return a list of the results of applying the methods to the items of the
1660 Return a list of the results of applying the methods to the items of the
1661 argument sequence(s). If more than one sequence is given, the method is
1661 argument sequence(s). If more than one sequence is given, the method is
1662 called with an argument list consisting of the corresponding item of each
1662 called with an argument list consisting of the corresponding item of each
1663 sequence. All sequences must be of the same length.
1663 sequence. All sequences must be of the same length.
1664
1664
1665 Keyword arguments are passed verbatim to all objects called.
1665 Keyword arguments are passed verbatim to all objects called.
1666
1666
1667 This is Python code, so it's not nearly as fast as the builtin map()."""
1667 This is Python code, so it's not nearly as fast as the builtin map()."""
1668
1668
1669 out_list = []
1669 out_list = []
1670 idx = 0
1670 idx = 0
1671 for object in object_list:
1671 for object in object_list:
1672 try:
1672 try:
1673 handler = getattr(object, method)
1673 handler = getattr(object, method)
1674 except AttributeError:
1674 except AttributeError:
1675 out_list.append(None)
1675 out_list.append(None)
1676 else:
1676 else:
1677 if argseq:
1677 if argseq:
1678 args = map(lambda lst:lst[idx],argseq)
1678 args = map(lambda lst:lst[idx],argseq)
1679 #print 'ob',object,'hand',handler,'ar',args # dbg
1679 #print 'ob',object,'hand',handler,'ar',args # dbg
1680 out_list.append(handler(args,**kw))
1680 out_list.append(handler(args,**kw))
1681 else:
1681 else:
1682 out_list.append(handler(**kw))
1682 out_list.append(handler(**kw))
1683 idx += 1
1683 idx += 1
1684 return out_list
1684 return out_list
1685
1685
1686 #----------------------------------------------------------------------------
1686 #----------------------------------------------------------------------------
1687 def import_fail_info(mod_name,fns=None):
1687 def import_fail_info(mod_name,fns=None):
1688 """Inform load failure for a module."""
1688 """Inform load failure for a module."""
1689
1689
1690 if fns == None:
1690 if fns == None:
1691 warn("Loading of %s failed.\n" % (mod_name,))
1691 warn("Loading of %s failed.\n" % (mod_name,))
1692 else:
1692 else:
1693 warn("Loading of %s from %s failed.\n" % (fns,mod_name))
1693 warn("Loading of %s from %s failed.\n" % (fns,mod_name))
1694
1694
1695 #----------------------------------------------------------------------------
1695 #----------------------------------------------------------------------------
1696 # Proposed popitem() extension, written as a method
1696 # Proposed popitem() extension, written as a method
1697
1697
1698 class NotGiven: pass
1698 class NotGiven: pass
1699
1699
1700 def popkey(dct,key,default=NotGiven):
1700 def popkey(dct,key,default=NotGiven):
1701 """Return dct[key] and delete dct[key].
1701 """Return dct[key] and delete dct[key].
1702
1702
1703 If default is given, return it if dct[key] doesn't exist, otherwise raise
1703 If default is given, return it if dct[key] doesn't exist, otherwise raise
1704 KeyError. """
1704 KeyError. """
1705
1705
1706 try:
1706 try:
1707 val = dct[key]
1707 val = dct[key]
1708 except KeyError:
1708 except KeyError:
1709 if default is NotGiven:
1709 if default is NotGiven:
1710 raise
1710 raise
1711 else:
1711 else:
1712 return default
1712 return default
1713 else:
1713 else:
1714 del dct[key]
1714 del dct[key]
1715 return val
1715 return val
1716 #*************************** end of file <genutils.py> **********************
1716 #*************************** end of file <genutils.py> **********************
1717
1717
@@ -1,2153 +1,2163 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 993 2006-01-04 19:51:01Z fperez $
9 $Id: iplib.py 994 2006-01-08 08:29:44Z 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-2005 Fernando Perez. <fperez@colorado.edu>
14 # Copyright (C) 2001-2006 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, all of that class has been copied
20 # Python standard library. Over time, all 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. At this point, there are no dependencies at all on the code
22 # subclassing. At this point, there are no dependencies at all on the code
23 # module anymore (it is not even imported). The Python License (sec. 2)
23 # module anymore (it is not even imported). The Python License (sec. 2)
24 # allows for this, but it's always nice to acknowledge credit where credit is
24 # allows for this, but it's always nice to acknowledge credit where credit is
25 # due.
25 # due.
26 #*****************************************************************************
26 #*****************************************************************************
27
27
28 #****************************************************************************
28 #****************************************************************************
29 # Modules and globals
29 # Modules and globals
30
30
31 from __future__ import generators # for 2.2 backwards-compatibility
31 from __future__ import generators # for 2.2 backwards-compatibility
32
32
33 from IPython import Release
33 from IPython import Release
34 __author__ = '%s <%s>\n%s <%s>' % \
34 __author__ = '%s <%s>\n%s <%s>' % \
35 ( Release.authors['Janko'] + Release.authors['Fernando'] )
35 ( Release.authors['Janko'] + Release.authors['Fernando'] )
36 __license__ = Release.license
36 __license__ = Release.license
37 __version__ = Release.version
37 __version__ = Release.version
38
38
39 # Python standard modules
39 # Python standard modules
40 import __main__
40 import __main__
41 import __builtin__
41 import __builtin__
42 import StringIO
42 import StringIO
43 import bdb
43 import bdb
44 import cPickle as pickle
44 import cPickle as pickle
45 import codeop
45 import codeop
46 import exceptions
46 import exceptions
47 import glob
47 import glob
48 import inspect
48 import inspect
49 import keyword
49 import keyword
50 import new
50 import new
51 import os
51 import os
52 import pdb
52 import pdb
53 import pydoc
53 import pydoc
54 import re
54 import re
55 import shutil
55 import shutil
56 import string
56 import string
57 import sys
57 import sys
58 import tempfile
58 import tempfile
59 import traceback
59 import traceback
60 import types
60 import types
61
61
62 from pprint import pprint, pformat
62 from pprint import pprint, pformat
63
63
64 # IPython's own modules
64 # IPython's own modules
65 import IPython
65 import IPython
66 from IPython import OInspect,PyColorize,ultraTB
66 from IPython import OInspect,PyColorize,ultraTB
67 from IPython.ColorANSI import ColorScheme,ColorSchemeTable # too long names
67 from IPython.ColorANSI import ColorScheme,ColorSchemeTable # too long names
68 from IPython.FakeModule import FakeModule
68 from IPython.FakeModule import FakeModule
69 from IPython.Itpl import Itpl,itpl,printpl,ItplNS,itplns
69 from IPython.Itpl import Itpl,itpl,printpl,ItplNS,itplns
70 from IPython.Logger import Logger
70 from IPython.Logger import Logger
71 from IPython.Magic import Magic
71 from IPython.Magic import Magic
72 from IPython.Prompts import CachedOutput
72 from IPython.Prompts import CachedOutput
73 from IPython.Struct import Struct
73 from IPython.Struct import Struct
74 from IPython.background_jobs import BackgroundJobManager
74 from IPython.background_jobs import BackgroundJobManager
75 from IPython.usage import cmd_line_usage,interactive_usage
75 from IPython.usage import cmd_line_usage,interactive_usage
76 from IPython.genutils import *
76 from IPython.genutils import *
77
77
78 # Globals
79
78 # store the builtin raw_input globally, and use this always, in case user code
80 # store the builtin raw_input globally, and use this always, in case user code
79 # overwrites it (like wx.py.PyShell does)
81 # overwrites it (like wx.py.PyShell does)
80 raw_input_original = raw_input
82 raw_input_original = raw_input
81
83
82 # compiled regexps for autoindent management
84 # compiled regexps for autoindent management
83 ini_spaces_re = re.compile(r'^(\s+)')
85 ini_spaces_re = re.compile(r'^(\s+)')
84 dedent_re = re.compile(r'^\s+raise|^\s+return|^\s+pass')
86 dedent_re = re.compile(r'^\s+raise|^\s+return|^\s+pass')
85
87
88
86 #****************************************************************************
89 #****************************************************************************
87 # Some utility function definitions
90 # Some utility function definitions
88
91
89 def softspace(file, newvalue):
92 def softspace(file, newvalue):
90 """Copied from code.py, to remove the dependency"""
93 """Copied from code.py, to remove the dependency"""
91 oldvalue = 0
94 oldvalue = 0
92 try:
95 try:
93 oldvalue = file.softspace
96 oldvalue = file.softspace
94 except AttributeError:
97 except AttributeError:
95 pass
98 pass
96 try:
99 try:
97 file.softspace = newvalue
100 file.softspace = newvalue
98 except (AttributeError, TypeError):
101 except (AttributeError, TypeError):
99 # "attribute-less object" or "read-only attributes"
102 # "attribute-less object" or "read-only attributes"
100 pass
103 pass
101 return oldvalue
104 return oldvalue
102
105
103 #****************************************************************************
104
105
106
106 #****************************************************************************
107 #****************************************************************************
107 # Local use exceptions
108 # Local use exceptions
108 class SpaceInInput(exceptions.Exception): pass
109 class SpaceInInput(exceptions.Exception): pass
109
110
111
110 #****************************************************************************
112 #****************************************************************************
111 # Local use classes
113 # Local use classes
112 class Bunch: pass
114 class Bunch: pass
113
115
114 class Undefined: pass
116 class Undefined: pass
115
117
116 class InputList(list):
118 class InputList(list):
117 """Class to store user input.
119 """Class to store user input.
118
120
119 It's basically a list, but slices return a string instead of a list, thus
121 It's basically a list, but slices return a string instead of a list, thus
120 allowing things like (assuming 'In' is an instance):
122 allowing things like (assuming 'In' is an instance):
121
123
122 exec In[4:7]
124 exec In[4:7]
123
125
124 or
126 or
125
127
126 exec In[5:9] + In[14] + In[21:25]"""
128 exec In[5:9] + In[14] + In[21:25]"""
127
129
128 def __getslice__(self,i,j):
130 def __getslice__(self,i,j):
129 return ''.join(list.__getslice__(self,i,j))
131 return ''.join(list.__getslice__(self,i,j))
130
132
131 class SyntaxTB(ultraTB.ListTB):
133 class SyntaxTB(ultraTB.ListTB):
132 """Extension which holds some state: the last exception value"""
134 """Extension which holds some state: the last exception value"""
133
135
134 def __init__(self,color_scheme = 'NoColor'):
136 def __init__(self,color_scheme = 'NoColor'):
135 ultraTB.ListTB.__init__(self,color_scheme)
137 ultraTB.ListTB.__init__(self,color_scheme)
136 self.last_syntax_error = None
138 self.last_syntax_error = None
137
139
138 def __call__(self, etype, value, elist):
140 def __call__(self, etype, value, elist):
139 self.last_syntax_error = value
141 self.last_syntax_error = value
140 ultraTB.ListTB.__call__(self,etype,value,elist)
142 ultraTB.ListTB.__call__(self,etype,value,elist)
141
143
142 def clear_err_state(self):
144 def clear_err_state(self):
143 """Return the current error state and clear it"""
145 """Return the current error state and clear it"""
144 e = self.last_syntax_error
146 e = self.last_syntax_error
145 self.last_syntax_error = None
147 self.last_syntax_error = None
146 return e
148 return e
147
149
148 #****************************************************************************
150 #****************************************************************************
149 # Main IPython class
151 # Main IPython class
150
152
151 # FIXME: the Magic class is a mixin for now, and will unfortunately remain so
153 # FIXME: the Magic class is a mixin for now, and will unfortunately remain so
152 # until a full rewrite is made. I've cleaned all cross-class uses of
154 # until a full rewrite is made. I've cleaned all cross-class uses of
153 # attributes and methods, but too much user code out there relies on the
155 # attributes and methods, but too much user code out there relies on the
154 # equlity %foo == __IP.magic_foo, so I can't actually remove the mixin usage.
156 # equlity %foo == __IP.magic_foo, so I can't actually remove the mixin usage.
155 #
157 #
156 # But at least now, all the pieces have been separated and we could, in
158 # But at least now, all the pieces have been separated and we could, in
157 # principle, stop using the mixin. This will ease the transition to the
159 # principle, stop using the mixin. This will ease the transition to the
158 # chainsaw branch.
160 # chainsaw branch.
159
161
160 # For reference, the following is the list of 'self.foo' uses in the Magic
162 # For reference, the following is the list of 'self.foo' uses in the Magic
161 # class as of 2005-12-28. These are names we CAN'T use in the main ipython
163 # class as of 2005-12-28. These are names we CAN'T use in the main ipython
162 # class, to prevent clashes.
164 # class, to prevent clashes.
163
165
164 # ['self.__class__', 'self.__dict__', 'self._inspect', 'self._ofind',
166 # ['self.__class__', 'self.__dict__', 'self._inspect', 'self._ofind',
165 # 'self.arg_err', 'self.extract_input', 'self.format_', 'self.lsmagic',
167 # 'self.arg_err', 'self.extract_input', 'self.format_', 'self.lsmagic',
166 # 'self.magic_', 'self.options_table', 'self.parse', 'self.shell',
168 # 'self.magic_', 'self.options_table', 'self.parse', 'self.shell',
167 # 'self.value']
169 # 'self.value']
168
170
169 class InteractiveShell(object,Magic):
171 class InteractiveShell(object,Magic):
170 """An enhanced console for Python."""
172 """An enhanced console for Python."""
171
173
172 # class attribute to indicate whether the class supports threads or not.
174 # class attribute to indicate whether the class supports threads or not.
173 # Subclasses with thread support should override this as needed.
175 # Subclasses with thread support should override this as needed.
174 isthreaded = False
176 isthreaded = False
175
177
176 def __init__(self,name,usage=None,rc=Struct(opts=None,args=None),
178 def __init__(self,name,usage=None,rc=Struct(opts=None,args=None),
177 user_ns = None,user_global_ns=None,banner2='',
179 user_ns = None,user_global_ns=None,banner2='',
178 custom_exceptions=((),None),embedded=False):
180 custom_exceptions=((),None),embedded=False):
179
181
180 # some minimal strict typechecks. For some core data structures, I
182 # some minimal strict typechecks. For some core data structures, I
181 # want actual basic python types, not just anything that looks like
183 # want actual basic python types, not just anything that looks like
182 # one. This is especially true for namespaces.
184 # one. This is especially true for namespaces.
183 for ns in (user_ns,user_global_ns):
185 for ns in (user_ns,user_global_ns):
184 if ns is not None and type(ns) != types.DictType:
186 if ns is not None and type(ns) != types.DictType:
185 raise TypeError,'namespace must be a dictionary'
187 raise TypeError,'namespace must be a dictionary'
186
188
187 # Job manager (for jobs run as background threads)
189 # Job manager (for jobs run as background threads)
188 self.jobs = BackgroundJobManager()
190 self.jobs = BackgroundJobManager()
189
191
190 # track which builtins we add, so we can clean up later
192 # track which builtins we add, so we can clean up later
191 self.builtins_added = {}
193 self.builtins_added = {}
192 # This method will add the necessary builtins for operation, but
194 # This method will add the necessary builtins for operation, but
193 # tracking what it did via the builtins_added dict.
195 # tracking what it did via the builtins_added dict.
194 self.add_builtins()
196 self.add_builtins()
195
197
196 # Do the intuitively correct thing for quit/exit: we remove the
198 # Do the intuitively correct thing for quit/exit: we remove the
197 # builtins if they exist, and our own magics will deal with this
199 # builtins if they exist, and our own magics will deal with this
198 try:
200 try:
199 del __builtin__.exit, __builtin__.quit
201 del __builtin__.exit, __builtin__.quit
200 except AttributeError:
202 except AttributeError:
201 pass
203 pass
202
204
203 # Store the actual shell's name
205 # Store the actual shell's name
204 self.name = name
206 self.name = name
205
207
206 # We need to know whether the instance is meant for embedding, since
208 # We need to know whether the instance is meant for embedding, since
207 # global/local namespaces need to be handled differently in that case
209 # global/local namespaces need to be handled differently in that case
208 self.embedded = embedded
210 self.embedded = embedded
209
211
210 # command compiler
212 # command compiler
211 self.compile = codeop.CommandCompiler()
213 self.compile = codeop.CommandCompiler()
212
214
213 # User input buffer
215 # User input buffer
214 self.buffer = []
216 self.buffer = []
215
217
216 # Default name given in compilation of code
218 # Default name given in compilation of code
217 self.filename = '<ipython console>'
219 self.filename = '<ipython console>'
218
220
219 # Make an empty namespace, which extension writers can rely on both
221 # Make an empty namespace, which extension writers can rely on both
220 # existing and NEVER being used by ipython itself. This gives them a
222 # existing and NEVER being used by ipython itself. This gives them a
221 # convenient location for storing additional information and state
223 # convenient location for storing additional information and state
222 # their extensions may require, without fear of collisions with other
224 # their extensions may require, without fear of collisions with other
223 # ipython names that may develop later.
225 # ipython names that may develop later.
224 self.meta = Bunch()
226 self.meta = Bunch()
225
227
226 # Create the namespace where the user will operate. user_ns is
228 # Create the namespace where the user will operate. user_ns is
227 # normally the only one used, and it is passed to the exec calls as
229 # normally the only one used, and it is passed to the exec calls as
228 # the locals argument. But we do carry a user_global_ns namespace
230 # the locals argument. But we do carry a user_global_ns namespace
229 # given as the exec 'globals' argument, This is useful in embedding
231 # given as the exec 'globals' argument, This is useful in embedding
230 # situations where the ipython shell opens in a context where the
232 # situations where the ipython shell opens in a context where the
231 # distinction between locals and globals is meaningful.
233 # distinction between locals and globals is meaningful.
232
234
233 # FIXME. For some strange reason, __builtins__ is showing up at user
235 # FIXME. For some strange reason, __builtins__ is showing up at user
234 # level as a dict instead of a module. This is a manual fix, but I
236 # level as a dict instead of a module. This is a manual fix, but I
235 # should really track down where the problem is coming from. Alex
237 # should really track down where the problem is coming from. Alex
236 # Schmolck reported this problem first.
238 # Schmolck reported this problem first.
237
239
238 # A useful post by Alex Martelli on this topic:
240 # A useful post by Alex Martelli on this topic:
239 # Re: inconsistent value from __builtins__
241 # Re: inconsistent value from __builtins__
240 # Von: Alex Martelli <aleaxit@yahoo.com>
242 # Von: Alex Martelli <aleaxit@yahoo.com>
241 # Datum: Freitag 01 Oktober 2004 04:45:34 nachmittags/abends
243 # Datum: Freitag 01 Oktober 2004 04:45:34 nachmittags/abends
242 # Gruppen: comp.lang.python
244 # Gruppen: comp.lang.python
243
245
244 # Michael Hohn <hohn@hooknose.lbl.gov> wrote:
246 # Michael Hohn <hohn@hooknose.lbl.gov> wrote:
245 # > >>> print type(builtin_check.get_global_binding('__builtins__'))
247 # > >>> print type(builtin_check.get_global_binding('__builtins__'))
246 # > <type 'dict'>
248 # > <type 'dict'>
247 # > >>> print type(__builtins__)
249 # > >>> print type(__builtins__)
248 # > <type 'module'>
250 # > <type 'module'>
249 # > Is this difference in return value intentional?
251 # > Is this difference in return value intentional?
250
252
251 # Well, it's documented that '__builtins__' can be either a dictionary
253 # Well, it's documented that '__builtins__' can be either a dictionary
252 # or a module, and it's been that way for a long time. Whether it's
254 # or a module, and it's been that way for a long time. Whether it's
253 # intentional (or sensible), I don't know. In any case, the idea is
255 # intentional (or sensible), I don't know. In any case, the idea is
254 # that if you need to access the built-in namespace directly, you
256 # that if you need to access the built-in namespace directly, you
255 # should start with "import __builtin__" (note, no 's') which will
257 # should start with "import __builtin__" (note, no 's') which will
256 # definitely give you a module. Yeah, it's somewhat confusing:-(.
258 # definitely give you a module. Yeah, it's somewhat confusing:-(.
257
259
258 if user_ns is None:
260 if user_ns is None:
259 # Set __name__ to __main__ to better match the behavior of the
261 # Set __name__ to __main__ to better match the behavior of the
260 # normal interpreter.
262 # normal interpreter.
261 user_ns = {'__name__' :'__main__',
263 user_ns = {'__name__' :'__main__',
262 '__builtins__' : __builtin__,
264 '__builtins__' : __builtin__,
263 }
265 }
264
266
265 if user_global_ns is None:
267 if user_global_ns is None:
266 user_global_ns = {}
268 user_global_ns = {}
267
269
268 # Assign namespaces
270 # Assign namespaces
269 # This is the namespace where all normal user variables live
271 # This is the namespace where all normal user variables live
270 self.user_ns = user_ns
272 self.user_ns = user_ns
271 # Embedded instances require a separate namespace for globals.
273 # Embedded instances require a separate namespace for globals.
272 # Normally this one is unused by non-embedded instances.
274 # Normally this one is unused by non-embedded instances.
273 self.user_global_ns = user_global_ns
275 self.user_global_ns = user_global_ns
274 # A namespace to keep track of internal data structures to prevent
276 # A namespace to keep track of internal data structures to prevent
275 # them from cluttering user-visible stuff. Will be updated later
277 # them from cluttering user-visible stuff. Will be updated later
276 self.internal_ns = {}
278 self.internal_ns = {}
277
279
278 # Namespace of system aliases. Each entry in the alias
280 # Namespace of system aliases. Each entry in the alias
279 # table must be a 2-tuple of the form (N,name), where N is the number
281 # table must be a 2-tuple of the form (N,name), where N is the number
280 # of positional arguments of the alias.
282 # of positional arguments of the alias.
281 self.alias_table = {}
283 self.alias_table = {}
282
284
283 # A table holding all the namespaces IPython deals with, so that
285 # A table holding all the namespaces IPython deals with, so that
284 # introspection facilities can search easily.
286 # introspection facilities can search easily.
285 self.ns_table = {'user':user_ns,
287 self.ns_table = {'user':user_ns,
286 'user_global':user_global_ns,
288 'user_global':user_global_ns,
287 'alias':self.alias_table,
289 'alias':self.alias_table,
288 'internal':self.internal_ns,
290 'internal':self.internal_ns,
289 'builtin':__builtin__.__dict__
291 'builtin':__builtin__.__dict__
290 }
292 }
291
293
292 # The user namespace MUST have a pointer to the shell itself.
294 # The user namespace MUST have a pointer to the shell itself.
293 self.user_ns[name] = self
295 self.user_ns[name] = self
294
296
295 # We need to insert into sys.modules something that looks like a
297 # We need to insert into sys.modules something that looks like a
296 # module but which accesses the IPython namespace, for shelve and
298 # module but which accesses the IPython namespace, for shelve and
297 # pickle to work interactively. Normally they rely on getting
299 # pickle to work interactively. Normally they rely on getting
298 # everything out of __main__, but for embedding purposes each IPython
300 # everything out of __main__, but for embedding purposes each IPython
299 # instance has its own private namespace, so we can't go shoving
301 # instance has its own private namespace, so we can't go shoving
300 # everything into __main__.
302 # everything into __main__.
301
303
302 # note, however, that we should only do this for non-embedded
304 # note, however, that we should only do this for non-embedded
303 # ipythons, which really mimic the __main__.__dict__ with their own
305 # ipythons, which really mimic the __main__.__dict__ with their own
304 # namespace. Embedded instances, on the other hand, should not do
306 # namespace. Embedded instances, on the other hand, should not do
305 # this because they need to manage the user local/global namespaces
307 # this because they need to manage the user local/global namespaces
306 # only, but they live within a 'normal' __main__ (meaning, they
308 # only, but they live within a 'normal' __main__ (meaning, they
307 # shouldn't overtake the execution environment of the script they're
309 # shouldn't overtake the execution environment of the script they're
308 # embedded in).
310 # embedded in).
309
311
310 if not embedded:
312 if not embedded:
311 try:
313 try:
312 main_name = self.user_ns['__name__']
314 main_name = self.user_ns['__name__']
313 except KeyError:
315 except KeyError:
314 raise KeyError,'user_ns dictionary MUST have a "__name__" key'
316 raise KeyError,'user_ns dictionary MUST have a "__name__" key'
315 else:
317 else:
316 #print "pickle hack in place" # dbg
318 #print "pickle hack in place" # dbg
317 #print 'main_name:',main_name # dbg
319 #print 'main_name:',main_name # dbg
318 sys.modules[main_name] = FakeModule(self.user_ns)
320 sys.modules[main_name] = FakeModule(self.user_ns)
319
321
320 # List of input with multi-line handling.
322 # List of input with multi-line handling.
321 # Fill its zero entry, user counter starts at 1
323 # Fill its zero entry, user counter starts at 1
322 self.input_hist = InputList(['\n'])
324 self.input_hist = InputList(['\n'])
323
325
324 # list of visited directories
326 # list of visited directories
325 try:
327 try:
326 self.dir_hist = [os.getcwd()]
328 self.dir_hist = [os.getcwd()]
327 except IOError, e:
329 except IOError, e:
328 self.dir_hist = []
330 self.dir_hist = []
329
331
330 # dict of output history
332 # dict of output history
331 self.output_hist = {}
333 self.output_hist = {}
332
334
333 # dict of things NOT to alias (keywords, builtins and some magics)
335 # dict of things NOT to alias (keywords, builtins and some magics)
334 no_alias = {}
336 no_alias = {}
335 no_alias_magics = ['cd','popd','pushd','dhist','alias','unalias']
337 no_alias_magics = ['cd','popd','pushd','dhist','alias','unalias']
336 for key in keyword.kwlist + no_alias_magics:
338 for key in keyword.kwlist + no_alias_magics:
337 no_alias[key] = 1
339 no_alias[key] = 1
338 no_alias.update(__builtin__.__dict__)
340 no_alias.update(__builtin__.__dict__)
339 self.no_alias = no_alias
341 self.no_alias = no_alias
340
342
341 # make global variables for user access to these
343 # make global variables for user access to these
342 self.user_ns['_ih'] = self.input_hist
344 self.user_ns['_ih'] = self.input_hist
343 self.user_ns['_oh'] = self.output_hist
345 self.user_ns['_oh'] = self.output_hist
344 self.user_ns['_dh'] = self.dir_hist
346 self.user_ns['_dh'] = self.dir_hist
345
347
346 # user aliases to input and output histories
348 # user aliases to input and output histories
347 self.user_ns['In'] = self.input_hist
349 self.user_ns['In'] = self.input_hist
348 self.user_ns['Out'] = self.output_hist
350 self.user_ns['Out'] = self.output_hist
349
351
350 # Object variable to store code object waiting execution. This is
352 # Object variable to store code object waiting execution. This is
351 # used mainly by the multithreaded shells, but it can come in handy in
353 # used mainly by the multithreaded shells, but it can come in handy in
352 # other situations. No need to use a Queue here, since it's a single
354 # other situations. No need to use a Queue here, since it's a single
353 # item which gets cleared once run.
355 # item which gets cleared once run.
354 self.code_to_run = None
356 self.code_to_run = None
355
357
356 # escapes for automatic behavior on the command line
358 # escapes for automatic behavior on the command line
357 self.ESC_SHELL = '!'
359 self.ESC_SHELL = '!'
358 self.ESC_HELP = '?'
360 self.ESC_HELP = '?'
359 self.ESC_MAGIC = '%'
361 self.ESC_MAGIC = '%'
360 self.ESC_QUOTE = ','
362 self.ESC_QUOTE = ','
361 self.ESC_QUOTE2 = ';'
363 self.ESC_QUOTE2 = ';'
362 self.ESC_PAREN = '/'
364 self.ESC_PAREN = '/'
363
365
364 # And their associated handlers
366 # And their associated handlers
365 self.esc_handlers = {self.ESC_PAREN : self.handle_auto,
367 self.esc_handlers = {self.ESC_PAREN : self.handle_auto,
366 self.ESC_QUOTE : self.handle_auto,
368 self.ESC_QUOTE : self.handle_auto,
367 self.ESC_QUOTE2 : self.handle_auto,
369 self.ESC_QUOTE2 : self.handle_auto,
368 self.ESC_MAGIC : self.handle_magic,
370 self.ESC_MAGIC : self.handle_magic,
369 self.ESC_HELP : self.handle_help,
371 self.ESC_HELP : self.handle_help,
370 self.ESC_SHELL : self.handle_shell_escape,
372 self.ESC_SHELL : self.handle_shell_escape,
371 }
373 }
372
374
373 # class initializations
375 # class initializations
374 Magic.__init__(self,self)
376 Magic.__init__(self,self)
375
377
376 # Python source parser/formatter for syntax highlighting
378 # Python source parser/formatter for syntax highlighting
377 pyformat = PyColorize.Parser().format
379 pyformat = PyColorize.Parser().format
378 self.pycolorize = lambda src: pyformat(src,'str',self.rc['colors'])
380 self.pycolorize = lambda src: pyformat(src,'str',self.rc['colors'])
379
381
380 # hooks holds pointers used for user-side customizations
382 # hooks holds pointers used for user-side customizations
381 self.hooks = Struct()
383 self.hooks = Struct()
382
384
383 # Set all default hooks, defined in the IPython.hooks module.
385 # Set all default hooks, defined in the IPython.hooks module.
384 hooks = IPython.hooks
386 hooks = IPython.hooks
385 for hook_name in hooks.__all__:
387 for hook_name in hooks.__all__:
386 self.set_hook(hook_name,getattr(hooks,hook_name))
388 self.set_hook(hook_name,getattr(hooks,hook_name))
387
389
388 # Flag to mark unconditional exit
390 # Flag to mark unconditional exit
389 self.exit_now = False
391 self.exit_now = False
390
392
391 self.usage_min = """\
393 self.usage_min = """\
392 An enhanced console for Python.
394 An enhanced console for Python.
393 Some of its features are:
395 Some of its features are:
394 - Readline support if the readline library is present.
396 - Readline support if the readline library is present.
395 - Tab completion in the local namespace.
397 - Tab completion in the local namespace.
396 - Logging of input, see command-line options.
398 - Logging of input, see command-line options.
397 - System shell escape via ! , eg !ls.
399 - System shell escape via ! , eg !ls.
398 - Magic commands, starting with a % (like %ls, %pwd, %cd, etc.)
400 - Magic commands, starting with a % (like %ls, %pwd, %cd, etc.)
399 - Keeps track of locally defined variables via %who, %whos.
401 - Keeps track of locally defined variables via %who, %whos.
400 - Show object information with a ? eg ?x or x? (use ?? for more info).
402 - Show object information with a ? eg ?x or x? (use ?? for more info).
401 """
403 """
402 if usage: self.usage = usage
404 if usage: self.usage = usage
403 else: self.usage = self.usage_min
405 else: self.usage = self.usage_min
404
406
405 # Storage
407 # Storage
406 self.rc = rc # This will hold all configuration information
408 self.rc = rc # This will hold all configuration information
407 self.pager = 'less'
409 self.pager = 'less'
408 # temporary files used for various purposes. Deleted at exit.
410 # temporary files used for various purposes. Deleted at exit.
409 self.tempfiles = []
411 self.tempfiles = []
410
412
411 # Keep track of readline usage (later set by init_readline)
413 # Keep track of readline usage (later set by init_readline)
412 self.has_readline = False
414 self.has_readline = False
413
415
414 # template for logfile headers. It gets resolved at runtime by the
416 # template for logfile headers. It gets resolved at runtime by the
415 # logstart method.
417 # logstart method.
416 self.loghead_tpl = \
418 self.loghead_tpl = \
417 """#log# Automatic Logger file. *** THIS MUST BE THE FIRST LINE ***
419 """#log# Automatic Logger file. *** THIS MUST BE THE FIRST LINE ***
418 #log# DO NOT CHANGE THIS LINE OR THE TWO BELOW
420 #log# DO NOT CHANGE THIS LINE OR THE TWO BELOW
419 #log# opts = %s
421 #log# opts = %s
420 #log# args = %s
422 #log# args = %s
421 #log# It is safe to make manual edits below here.
423 #log# It is safe to make manual edits below here.
422 #log#-----------------------------------------------------------------------
424 #log#-----------------------------------------------------------------------
423 """
425 """
424 # for pushd/popd management
426 # for pushd/popd management
425 try:
427 try:
426 self.home_dir = get_home_dir()
428 self.home_dir = get_home_dir()
427 except HomeDirError,msg:
429 except HomeDirError,msg:
428 fatal(msg)
430 fatal(msg)
429
431
430 self.dir_stack = [os.getcwd().replace(self.home_dir,'~')]
432 self.dir_stack = [os.getcwd().replace(self.home_dir,'~')]
431
433
432 # Functions to call the underlying shell.
434 # Functions to call the underlying shell.
433
435
434 # utility to expand user variables via Itpl
436 # utility to expand user variables via Itpl
435 self.var_expand = lambda cmd: str(ItplNS(cmd.replace('#','\#'),
437 self.var_expand = lambda cmd: str(ItplNS(cmd.replace('#','\#'),
436 self.user_ns))
438 self.user_ns))
437 # The first is similar to os.system, but it doesn't return a value,
439 # The first is similar to os.system, but it doesn't return a value,
438 # and it allows interpolation of variables in the user's namespace.
440 # and it allows interpolation of variables in the user's namespace.
439 self.system = lambda cmd: shell(self.var_expand(cmd),
441 self.system = lambda cmd: shell(self.var_expand(cmd),
440 header='IPython system call: ',
442 header='IPython system call: ',
441 verbose=self.rc.system_verbose)
443 verbose=self.rc.system_verbose)
442 # These are for getoutput and getoutputerror:
444 # These are for getoutput and getoutputerror:
443 self.getoutput = lambda cmd: \
445 self.getoutput = lambda cmd: \
444 getoutput(self.var_expand(cmd),
446 getoutput(self.var_expand(cmd),
445 header='IPython system call: ',
447 header='IPython system call: ',
446 verbose=self.rc.system_verbose)
448 verbose=self.rc.system_verbose)
447 self.getoutputerror = lambda cmd: \
449 self.getoutputerror = lambda cmd: \
448 getoutputerror(str(ItplNS(cmd.replace('#','\#'),
450 getoutputerror(str(ItplNS(cmd.replace('#','\#'),
449 self.user_ns)),
451 self.user_ns)),
450 header='IPython system call: ',
452 header='IPython system call: ',
451 verbose=self.rc.system_verbose)
453 verbose=self.rc.system_verbose)
452
454
453 # RegExp for splitting line contents into pre-char//first
455 # RegExp for splitting line contents into pre-char//first
454 # word-method//rest. For clarity, each group in on one line.
456 # word-method//rest. For clarity, each group in on one line.
455
457
456 # WARNING: update the regexp if the above escapes are changed, as they
458 # WARNING: update the regexp if the above escapes are changed, as they
457 # are hardwired in.
459 # are hardwired in.
458
460
459 # Don't get carried away with trying to make the autocalling catch too
461 # Don't get carried away with trying to make the autocalling catch too
460 # much: it's better to be conservative rather than to trigger hidden
462 # much: it's better to be conservative rather than to trigger hidden
461 # evals() somewhere and end up causing side effects.
463 # evals() somewhere and end up causing side effects.
462
464
463 self.line_split = re.compile(r'^([\s*,;/])'
465 self.line_split = re.compile(r'^([\s*,;/])'
464 r'([\?\w\.]+\w*\s*)'
466 r'([\?\w\.]+\w*\s*)'
465 r'(\(?.*$)')
467 r'(\(?.*$)')
466
468
467 # Original re, keep around for a while in case changes break something
469 # Original re, keep around for a while in case changes break something
468 #self.line_split = re.compile(r'(^[\s*!\?%,/]?)'
470 #self.line_split = re.compile(r'(^[\s*!\?%,/]?)'
469 # r'(\s*[\?\w\.]+\w*\s*)'
471 # r'(\s*[\?\w\.]+\w*\s*)'
470 # r'(\(?.*$)')
472 # r'(\(?.*$)')
471
473
472 # RegExp to identify potential function names
474 # RegExp to identify potential function names
473 self.re_fun_name = re.compile(r'[a-zA-Z_]([a-zA-Z0-9_.]*) *$')
475 self.re_fun_name = re.compile(r'[a-zA-Z_]([a-zA-Z0-9_.]*) *$')
474 # RegExp to exclude strings with this start from autocalling
476 # RegExp to exclude strings with this start from autocalling
475 self.re_exclude_auto = re.compile('^[!=()<>,\*/\+-]|^is ')
477 self.re_exclude_auto = re.compile('^[!=()<>,\*/\+-]|^is ')
476
478
477 # try to catch also methods for stuff in lists/tuples/dicts: off
479 # try to catch also methods for stuff in lists/tuples/dicts: off
478 # (experimental). For this to work, the line_split regexp would need
480 # (experimental). For this to work, the line_split regexp would need
479 # to be modified so it wouldn't break things at '['. That line is
481 # to be modified so it wouldn't break things at '['. That line is
480 # nasty enough that I shouldn't change it until I can test it _well_.
482 # nasty enough that I shouldn't change it until I can test it _well_.
481 #self.re_fun_name = re.compile (r'[a-zA-Z_]([a-zA-Z0-9_.\[\]]*) ?$')
483 #self.re_fun_name = re.compile (r'[a-zA-Z_]([a-zA-Z0-9_.\[\]]*) ?$')
482
484
483 # keep track of where we started running (mainly for crash post-mortem)
485 # keep track of where we started running (mainly for crash post-mortem)
484 self.starting_dir = os.getcwd()
486 self.starting_dir = os.getcwd()
485
487
486 # Various switches which can be set
488 # Various switches which can be set
487 self.CACHELENGTH = 5000 # this is cheap, it's just text
489 self.CACHELENGTH = 5000 # this is cheap, it's just text
488 self.BANNER = "Python %(version)s on %(platform)s\n" % sys.__dict__
490 self.BANNER = "Python %(version)s on %(platform)s\n" % sys.__dict__
489 self.banner2 = banner2
491 self.banner2 = banner2
490
492
491 # TraceBack handlers:
493 # TraceBack handlers:
492
494
493 # Syntax error handler.
495 # Syntax error handler.
494 self.SyntaxTB = SyntaxTB(color_scheme='NoColor')
496 self.SyntaxTB = SyntaxTB(color_scheme='NoColor')
495
497
496 # The interactive one is initialized with an offset, meaning we always
498 # The interactive one is initialized with an offset, meaning we always
497 # want to remove the topmost item in the traceback, which is our own
499 # want to remove the topmost item in the traceback, which is our own
498 # internal code. Valid modes: ['Plain','Context','Verbose']
500 # internal code. Valid modes: ['Plain','Context','Verbose']
499 self.InteractiveTB = ultraTB.AutoFormattedTB(mode = 'Plain',
501 self.InteractiveTB = ultraTB.AutoFormattedTB(mode = 'Plain',
500 color_scheme='NoColor',
502 color_scheme='NoColor',
501 tb_offset = 1)
503 tb_offset = 1)
502
504
503 # IPython itself shouldn't crash. This will produce a detailed
505 # IPython itself shouldn't crash. This will produce a detailed
504 # post-mortem if it does. But we only install the crash handler for
506 # post-mortem if it does. But we only install the crash handler for
505 # non-threaded shells, the threaded ones use a normal verbose reporter
507 # non-threaded shells, the threaded ones use a normal verbose reporter
506 # and lose the crash handler. This is because exceptions in the main
508 # and lose the crash handler. This is because exceptions in the main
507 # thread (such as in GUI code) propagate directly to sys.excepthook,
509 # thread (such as in GUI code) propagate directly to sys.excepthook,
508 # and there's no point in printing crash dumps for every user exception.
510 # and there's no point in printing crash dumps for every user exception.
509 if self.isthreaded:
511 if self.isthreaded:
510 sys.excepthook = ultraTB.FormattedTB()
512 sys.excepthook = ultraTB.FormattedTB()
511 else:
513 else:
512 from IPython import CrashHandler
514 from IPython import CrashHandler
513 sys.excepthook = CrashHandler.CrashHandler(self)
515 sys.excepthook = CrashHandler.CrashHandler(self)
514
516
515 # The instance will store a pointer to this, so that runtime code
517 # The instance will store a pointer to this, so that runtime code
516 # (such as magics) can access it. This is because during the
518 # (such as magics) can access it. This is because during the
517 # read-eval loop, it gets temporarily overwritten (to deal with GUI
519 # read-eval loop, it gets temporarily overwritten (to deal with GUI
518 # frameworks).
520 # frameworks).
519 self.sys_excepthook = sys.excepthook
521 self.sys_excepthook = sys.excepthook
520
522
521 # and add any custom exception handlers the user may have specified
523 # and add any custom exception handlers the user may have specified
522 self.set_custom_exc(*custom_exceptions)
524 self.set_custom_exc(*custom_exceptions)
523
525
524 # Object inspector
526 # Object inspector
525 self.inspector = OInspect.Inspector(OInspect.InspectColors,
527 self.inspector = OInspect.Inspector(OInspect.InspectColors,
526 PyColorize.ANSICodeColors,
528 PyColorize.ANSICodeColors,
527 'NoColor')
529 'NoColor')
528 # indentation management
530 # indentation management
529 self.autoindent = False
531 self.autoindent = False
530 self.indent_current_nsp = 0
532 self.indent_current_nsp = 0
531 self.indent_current = '' # actual indent string
533 self.indent_current = '' # actual indent string
532
534
533 # Make some aliases automatically
535 # Make some aliases automatically
534 # Prepare list of shell aliases to auto-define
536 # Prepare list of shell aliases to auto-define
535 if os.name == 'posix':
537 if os.name == 'posix':
536 auto_alias = ('mkdir mkdir', 'rmdir rmdir',
538 auto_alias = ('mkdir mkdir', 'rmdir rmdir',
537 'mv mv -i','rm rm -i','cp cp -i',
539 'mv mv -i','rm rm -i','cp cp -i',
538 'cat cat','less less','clear clear',
540 'cat cat','less less','clear clear',
539 # a better ls
541 # a better ls
540 'ls ls -F',
542 'ls ls -F',
541 # long ls
543 # long ls
542 'll ls -lF',
544 'll ls -lF',
543 # color ls
545 # color ls
544 'lc ls -F -o --color',
546 'lc ls -F -o --color',
545 # ls normal files only
547 # ls normal files only
546 'lf ls -F -o --color %l | grep ^-',
548 'lf ls -F -o --color %l | grep ^-',
547 # ls symbolic links
549 # ls symbolic links
548 'lk ls -F -o --color %l | grep ^l',
550 'lk ls -F -o --color %l | grep ^l',
549 # directories or links to directories,
551 # directories or links to directories,
550 'ldir ls -F -o --color %l | grep /$',
552 'ldir ls -F -o --color %l | grep /$',
551 # things which are executable
553 # things which are executable
552 'lx ls -F -o --color %l | grep ^-..x',
554 'lx ls -F -o --color %l | grep ^-..x',
553 )
555 )
554 elif os.name in ['nt','dos']:
556 elif os.name in ['nt','dos']:
555 auto_alias = ('dir dir /on', 'ls dir /on',
557 auto_alias = ('dir dir /on', 'ls dir /on',
556 'ddir dir /ad /on', 'ldir dir /ad /on',
558 'ddir dir /ad /on', 'ldir dir /ad /on',
557 'mkdir mkdir','rmdir rmdir','echo echo',
559 'mkdir mkdir','rmdir rmdir','echo echo',
558 'ren ren','cls cls','copy copy')
560 'ren ren','cls cls','copy copy')
559 else:
561 else:
560 auto_alias = ()
562 auto_alias = ()
561 self.auto_alias = map(lambda s:s.split(None,1),auto_alias)
563 self.auto_alias = map(lambda s:s.split(None,1),auto_alias)
562 # Call the actual (public) initializer
564 # Call the actual (public) initializer
563 self.init_auto_alias()
565 self.init_auto_alias()
564 # end __init__
566 # end __init__
565
567
566 def post_config_initialization(self):
568 def post_config_initialization(self):
567 """Post configuration init method
569 """Post configuration init method
568
570
569 This is called after the configuration files have been processed to
571 This is called after the configuration files have been processed to
570 'finalize' the initialization."""
572 'finalize' the initialization."""
571
573
572 rc = self.rc
574 rc = self.rc
573
575
574 # Load readline proper
576 # Load readline proper
575 if rc.readline:
577 if rc.readline:
576 self.init_readline()
578 self.init_readline()
577
579
578 # log system
580 # log system
579 self.logger = Logger(self,logfname='ipython_log.py',logmode='rotate')
581 self.logger = Logger(self,logfname='ipython_log.py',logmode='rotate')
580 # local shortcut, this is used a LOT
582 # local shortcut, this is used a LOT
581 self.log = self.logger.log
583 self.log = self.logger.log
582
584
583 # Initialize cache, set in/out prompts and printing system
585 # Initialize cache, set in/out prompts and printing system
584 self.outputcache = CachedOutput(self,
586 self.outputcache = CachedOutput(self,
585 rc.cache_size,
587 rc.cache_size,
586 rc.pprint,
588 rc.pprint,
587 input_sep = rc.separate_in,
589 input_sep = rc.separate_in,
588 output_sep = rc.separate_out,
590 output_sep = rc.separate_out,
589 output_sep2 = rc.separate_out2,
591 output_sep2 = rc.separate_out2,
590 ps1 = rc.prompt_in1,
592 ps1 = rc.prompt_in1,
591 ps2 = rc.prompt_in2,
593 ps2 = rc.prompt_in2,
592 ps_out = rc.prompt_out,
594 ps_out = rc.prompt_out,
593 pad_left = rc.prompts_pad_left)
595 pad_left = rc.prompts_pad_left)
594
596
595 # user may have over-ridden the default print hook:
597 # user may have over-ridden the default print hook:
596 try:
598 try:
597 self.outputcache.__class__.display = self.hooks.display
599 self.outputcache.__class__.display = self.hooks.display
598 except AttributeError:
600 except AttributeError:
599 pass
601 pass
600
602
601 # I don't like assigning globally to sys, because it means when embedding
603 # I don't like assigning globally to sys, because it means when embedding
602 # instances, each embedded instance overrides the previous choice. But
604 # instances, each embedded instance overrides the previous choice. But
603 # sys.displayhook seems to be called internally by exec, so I don't see a
605 # sys.displayhook seems to be called internally by exec, so I don't see a
604 # way around it.
606 # way around it.
605 sys.displayhook = self.outputcache
607 sys.displayhook = self.outputcache
606
608
607 # Set user colors (don't do it in the constructor above so that it
609 # Set user colors (don't do it in the constructor above so that it
608 # doesn't crash if colors option is invalid)
610 # doesn't crash if colors option is invalid)
609 self.magic_colors(rc.colors)
611 self.magic_colors(rc.colors)
610
612
611 # Set calling of pdb on exceptions
613 # Set calling of pdb on exceptions
612 self.call_pdb = rc.pdb
614 self.call_pdb = rc.pdb
613
615
614 # Load user aliases
616 # Load user aliases
615 for alias in rc.alias:
617 for alias in rc.alias:
616 self.magic_alias(alias)
618 self.magic_alias(alias)
617
619
618 # dynamic data that survives through sessions
620 # dynamic data that survives through sessions
619 # XXX make the filename a config option?
621 # XXX make the filename a config option?
620 persist_base = 'persist'
622 persist_base = 'persist'
621 if rc.profile:
623 if rc.profile:
622 persist_base += '_%s' % rc.profile
624 persist_base += '_%s' % rc.profile
623 self.persist_fname = os.path.join(rc.ipythondir,persist_base)
625 self.persist_fname = os.path.join(rc.ipythondir,persist_base)
624
626
625 try:
627 try:
626 self.persist = pickle.load(file(self.persist_fname))
628 self.persist = pickle.load(file(self.persist_fname))
627 except:
629 except:
628 self.persist = {}
630 self.persist = {}
629
631
630
632
631 for (key, value) in [(k[2:],v) for (k,v) in self.persist.items() if k.startswith('S:')]:
633 for (key, value) in [(k[2:],v) for (k,v) in self.persist.items() if k.startswith('S:')]:
632 try:
634 try:
633 obj = pickle.loads(value)
635 obj = pickle.loads(value)
634 except:
636 except:
635
637
636 print "Unable to restore variable '%s', ignoring (use %%store -d to forget!)" % key
638 print "Unable to restore variable '%s', ignoring (use %%store -d to forget!)" % key
637 print "The error was:",sys.exc_info()[0]
639 print "The error was:",sys.exc_info()[0]
638 continue
640 continue
639
641
640
642
641 self.user_ns[key] = obj
643 self.user_ns[key] = obj
642
644
643 def add_builtins(self):
645 def add_builtins(self):
644 """Store ipython references into the builtin namespace.
646 """Store ipython references into the builtin namespace.
645
647
646 Some parts of ipython operate via builtins injected here, which hold a
648 Some parts of ipython operate via builtins injected here, which hold a
647 reference to IPython itself."""
649 reference to IPython itself."""
648
650
649 builtins_new = dict(__IPYTHON__ = self,
651 builtins_new = dict(__IPYTHON__ = self,
650 ip_set_hook = self.set_hook,
652 ip_set_hook = self.set_hook,
651 jobs = self.jobs,
653 jobs = self.jobs,
652 ipmagic = self.ipmagic,
654 ipmagic = self.ipmagic,
653 ipalias = self.ipalias,
655 ipalias = self.ipalias,
654 ipsystem = self.ipsystem,
656 ipsystem = self.ipsystem,
655 )
657 )
656 for biname,bival in builtins_new.items():
658 for biname,bival in builtins_new.items():
657 try:
659 try:
658 # store the orignal value so we can restore it
660 # store the orignal value so we can restore it
659 self.builtins_added[biname] = __builtin__.__dict__[biname]
661 self.builtins_added[biname] = __builtin__.__dict__[biname]
660 except KeyError:
662 except KeyError:
661 # or mark that it wasn't defined, and we'll just delete it at
663 # or mark that it wasn't defined, and we'll just delete it at
662 # cleanup
664 # cleanup
663 self.builtins_added[biname] = Undefined
665 self.builtins_added[biname] = Undefined
664 __builtin__.__dict__[biname] = bival
666 __builtin__.__dict__[biname] = bival
665
667
666 # Keep in the builtins a flag for when IPython is active. We set it
668 # Keep in the builtins a flag for when IPython is active. We set it
667 # with setdefault so that multiple nested IPythons don't clobber one
669 # with setdefault so that multiple nested IPythons don't clobber one
668 # another. Each will increase its value by one upon being activated,
670 # another. Each will increase its value by one upon being activated,
669 # which also gives us a way to determine the nesting level.
671 # which also gives us a way to determine the nesting level.
670 __builtin__.__dict__.setdefault('__IPYTHON__active',0)
672 __builtin__.__dict__.setdefault('__IPYTHON__active',0)
671
673
672 def clean_builtins(self):
674 def clean_builtins(self):
673 """Remove any builtins which might have been added by add_builtins, or
675 """Remove any builtins which might have been added by add_builtins, or
674 restore overwritten ones to their previous values."""
676 restore overwritten ones to their previous values."""
675 for biname,bival in self.builtins_added.items():
677 for biname,bival in self.builtins_added.items():
676 if bival is Undefined:
678 if bival is Undefined:
677 del __builtin__.__dict__[biname]
679 del __builtin__.__dict__[biname]
678 else:
680 else:
679 __builtin__.__dict__[biname] = bival
681 __builtin__.__dict__[biname] = bival
680 self.builtins_added.clear()
682 self.builtins_added.clear()
681
683
682 def set_hook(self,name,hook):
684 def set_hook(self,name,hook):
683 """set_hook(name,hook) -> sets an internal IPython hook.
685 """set_hook(name,hook) -> sets an internal IPython hook.
684
686
685 IPython exposes some of its internal API as user-modifiable hooks. By
687 IPython exposes some of its internal API as user-modifiable hooks. By
686 resetting one of these hooks, you can modify IPython's behavior to
688 resetting one of these hooks, you can modify IPython's behavior to
687 call at runtime your own routines."""
689 call at runtime your own routines."""
688
690
689 # At some point in the future, this should validate the hook before it
691 # At some point in the future, this should validate the hook before it
690 # accepts it. Probably at least check that the hook takes the number
692 # accepts it. Probably at least check that the hook takes the number
691 # of args it's supposed to.
693 # of args it's supposed to.
692 setattr(self.hooks,name,new.instancemethod(hook,self,self.__class__))
694 setattr(self.hooks,name,new.instancemethod(hook,self,self.__class__))
693
695
694 def set_custom_exc(self,exc_tuple,handler):
696 def set_custom_exc(self,exc_tuple,handler):
695 """set_custom_exc(exc_tuple,handler)
697 """set_custom_exc(exc_tuple,handler)
696
698
697 Set a custom exception handler, which will be called if any of the
699 Set a custom exception handler, which will be called if any of the
698 exceptions in exc_tuple occur in the mainloop (specifically, in the
700 exceptions in exc_tuple occur in the mainloop (specifically, in the
699 runcode() method.
701 runcode() method.
700
702
701 Inputs:
703 Inputs:
702
704
703 - exc_tuple: a *tuple* of valid exceptions to call the defined
705 - exc_tuple: a *tuple* of valid exceptions to call the defined
704 handler for. It is very important that you use a tuple, and NOT A
706 handler for. It is very important that you use a tuple, and NOT A
705 LIST here, because of the way Python's except statement works. If
707 LIST here, because of the way Python's except statement works. If
706 you only want to trap a single exception, use a singleton tuple:
708 you only want to trap a single exception, use a singleton tuple:
707
709
708 exc_tuple == (MyCustomException,)
710 exc_tuple == (MyCustomException,)
709
711
710 - handler: this must be defined as a function with the following
712 - handler: this must be defined as a function with the following
711 basic interface: def my_handler(self,etype,value,tb).
713 basic interface: def my_handler(self,etype,value,tb).
712
714
713 This will be made into an instance method (via new.instancemethod)
715 This will be made into an instance method (via new.instancemethod)
714 of IPython itself, and it will be called if any of the exceptions
716 of IPython itself, and it will be called if any of the exceptions
715 listed in the exc_tuple are caught. If the handler is None, an
717 listed in the exc_tuple are caught. If the handler is None, an
716 internal basic one is used, which just prints basic info.
718 internal basic one is used, which just prints basic info.
717
719
718 WARNING: by putting in your own exception handler into IPython's main
720 WARNING: by putting in your own exception handler into IPython's main
719 execution loop, you run a very good chance of nasty crashes. This
721 execution loop, you run a very good chance of nasty crashes. This
720 facility should only be used if you really know what you are doing."""
722 facility should only be used if you really know what you are doing."""
721
723
722 assert type(exc_tuple)==type(()) , \
724 assert type(exc_tuple)==type(()) , \
723 "The custom exceptions must be given AS A TUPLE."
725 "The custom exceptions must be given AS A TUPLE."
724
726
725 def dummy_handler(self,etype,value,tb):
727 def dummy_handler(self,etype,value,tb):
726 print '*** Simple custom exception handler ***'
728 print '*** Simple custom exception handler ***'
727 print 'Exception type :',etype
729 print 'Exception type :',etype
728 print 'Exception value:',value
730 print 'Exception value:',value
729 print 'Traceback :',tb
731 print 'Traceback :',tb
730 print 'Source code :','\n'.join(self.buffer)
732 print 'Source code :','\n'.join(self.buffer)
731
733
732 if handler is None: handler = dummy_handler
734 if handler is None: handler = dummy_handler
733
735
734 self.CustomTB = new.instancemethod(handler,self,self.__class__)
736 self.CustomTB = new.instancemethod(handler,self,self.__class__)
735 self.custom_exceptions = exc_tuple
737 self.custom_exceptions = exc_tuple
736
738
737 def set_custom_completer(self,completer,pos=0):
739 def set_custom_completer(self,completer,pos=0):
738 """set_custom_completer(completer,pos=0)
740 """set_custom_completer(completer,pos=0)
739
741
740 Adds a new custom completer function.
742 Adds a new custom completer function.
741
743
742 The position argument (defaults to 0) is the index in the completers
744 The position argument (defaults to 0) is the index in the completers
743 list where you want the completer to be inserted."""
745 list where you want the completer to be inserted."""
744
746
745 newcomp = new.instancemethod(completer,self.Completer,
747 newcomp = new.instancemethod(completer,self.Completer,
746 self.Completer.__class__)
748 self.Completer.__class__)
747 self.Completer.matchers.insert(pos,newcomp)
749 self.Completer.matchers.insert(pos,newcomp)
748
750
749 def _get_call_pdb(self):
751 def _get_call_pdb(self):
750 return self._call_pdb
752 return self._call_pdb
751
753
752 def _set_call_pdb(self,val):
754 def _set_call_pdb(self,val):
753
755
754 if val not in (0,1,False,True):
756 if val not in (0,1,False,True):
755 raise ValueError,'new call_pdb value must be boolean'
757 raise ValueError,'new call_pdb value must be boolean'
756
758
757 # store value in instance
759 # store value in instance
758 self._call_pdb = val
760 self._call_pdb = val
759
761
760 # notify the actual exception handlers
762 # notify the actual exception handlers
761 self.InteractiveTB.call_pdb = val
763 self.InteractiveTB.call_pdb = val
762 if self.isthreaded:
764 if self.isthreaded:
763 try:
765 try:
764 self.sys_excepthook.call_pdb = val
766 self.sys_excepthook.call_pdb = val
765 except:
767 except:
766 warn('Failed to activate pdb for threaded exception handler')
768 warn('Failed to activate pdb for threaded exception handler')
767
769
768 call_pdb = property(_get_call_pdb,_set_call_pdb,None,
770 call_pdb = property(_get_call_pdb,_set_call_pdb,None,
769 'Control auto-activation of pdb at exceptions')
771 'Control auto-activation of pdb at exceptions')
770
772
771
773
772 # These special functions get installed in the builtin namespace, to
774 # These special functions get installed in the builtin namespace, to
773 # provide programmatic (pure python) access to magics, aliases and system
775 # provide programmatic (pure python) access to magics, aliases and system
774 # calls. This is important for logging, user scripting, and more.
776 # calls. This is important for logging, user scripting, and more.
775
777
776 # We are basically exposing, via normal python functions, the three
778 # We are basically exposing, via normal python functions, the three
777 # mechanisms in which ipython offers special call modes (magics for
779 # mechanisms in which ipython offers special call modes (magics for
778 # internal control, aliases for direct system access via pre-selected
780 # internal control, aliases for direct system access via pre-selected
779 # names, and !cmd for calling arbitrary system commands).
781 # names, and !cmd for calling arbitrary system commands).
780
782
781 def ipmagic(self,arg_s):
783 def ipmagic(self,arg_s):
782 """Call a magic function by name.
784 """Call a magic function by name.
783
785
784 Input: a string containing the name of the magic function to call and any
786 Input: a string containing the name of the magic function to call and any
785 additional arguments to be passed to the magic.
787 additional arguments to be passed to the magic.
786
788
787 ipmagic('name -opt foo bar') is equivalent to typing at the ipython
789 ipmagic('name -opt foo bar') is equivalent to typing at the ipython
788 prompt:
790 prompt:
789
791
790 In[1]: %name -opt foo bar
792 In[1]: %name -opt foo bar
791
793
792 To call a magic without arguments, simply use ipmagic('name').
794 To call a magic without arguments, simply use ipmagic('name').
793
795
794 This provides a proper Python function to call IPython's magics in any
796 This provides a proper Python function to call IPython's magics in any
795 valid Python code you can type at the interpreter, including loops and
797 valid Python code you can type at the interpreter, including loops and
796 compound statements. It is added by IPython to the Python builtin
798 compound statements. It is added by IPython to the Python builtin
797 namespace upon initialization."""
799 namespace upon initialization."""
798
800
799 args = arg_s.split(' ',1)
801 args = arg_s.split(' ',1)
800 magic_name = args[0]
802 magic_name = args[0]
801 if magic_name.startswith(self.ESC_MAGIC):
803 if magic_name.startswith(self.ESC_MAGIC):
802 magic_name = magic_name[1:]
804 magic_name = magic_name[1:]
803 try:
805 try:
804 magic_args = args[1]
806 magic_args = args[1]
805 except IndexError:
807 except IndexError:
806 magic_args = ''
808 magic_args = ''
807 fn = getattr(self,'magic_'+magic_name,None)
809 fn = getattr(self,'magic_'+magic_name,None)
808 if fn is None:
810 if fn is None:
809 error("Magic function `%s` not found." % magic_name)
811 error("Magic function `%s` not found." % magic_name)
810 else:
812 else:
811 magic_args = self.var_expand(magic_args)
813 magic_args = self.var_expand(magic_args)
812 return fn(magic_args)
814 return fn(magic_args)
813
815
814 def ipalias(self,arg_s):
816 def ipalias(self,arg_s):
815 """Call an alias by name.
817 """Call an alias by name.
816
818
817 Input: a string containing the name of the alias to call and any
819 Input: a string containing the name of the alias to call and any
818 additional arguments to be passed to the magic.
820 additional arguments to be passed to the magic.
819
821
820 ipalias('name -opt foo bar') is equivalent to typing at the ipython
822 ipalias('name -opt foo bar') is equivalent to typing at the ipython
821 prompt:
823 prompt:
822
824
823 In[1]: name -opt foo bar
825 In[1]: name -opt foo bar
824
826
825 To call an alias without arguments, simply use ipalias('name').
827 To call an alias without arguments, simply use ipalias('name').
826
828
827 This provides a proper Python function to call IPython's aliases in any
829 This provides a proper Python function to call IPython's aliases in any
828 valid Python code you can type at the interpreter, including loops and
830 valid Python code you can type at the interpreter, including loops and
829 compound statements. It is added by IPython to the Python builtin
831 compound statements. It is added by IPython to the Python builtin
830 namespace upon initialization."""
832 namespace upon initialization."""
831
833
832 args = arg_s.split(' ',1)
834 args = arg_s.split(' ',1)
833 alias_name = args[0]
835 alias_name = args[0]
834 try:
836 try:
835 alias_args = args[1]
837 alias_args = args[1]
836 except IndexError:
838 except IndexError:
837 alias_args = ''
839 alias_args = ''
838 if alias_name in self.alias_table:
840 if alias_name in self.alias_table:
839 self.call_alias(alias_name,alias_args)
841 self.call_alias(alias_name,alias_args)
840 else:
842 else:
841 error("Alias `%s` not found." % alias_name)
843 error("Alias `%s` not found." % alias_name)
842
844
843 def ipsystem(self,arg_s):
845 def ipsystem(self,arg_s):
844 """Make a system call, using IPython."""
846 """Make a system call, using IPython."""
845 self.system(arg_s)
847 self.system(arg_s)
846
848
847 def complete(self,text):
849 def complete(self,text):
848 """Return a sorted list of all possible completions on text.
850 """Return a sorted list of all possible completions on text.
849
851
850 Inputs:
852 Inputs:
851
853
852 - text: a string of text to be completed on.
854 - text: a string of text to be completed on.
853
855
854 This is a wrapper around the completion mechanism, similar to what
856 This is a wrapper around the completion mechanism, similar to what
855 readline does at the command line when the TAB key is hit. By
857 readline does at the command line when the TAB key is hit. By
856 exposing it as a method, it can be used by other non-readline
858 exposing it as a method, it can be used by other non-readline
857 environments (such as GUIs) for text completion.
859 environments (such as GUIs) for text completion.
858
860
859 Simple usage example:
861 Simple usage example:
860
862
861 In [1]: x = 'hello'
863 In [1]: x = 'hello'
862
864
863 In [2]: __IP.complete('x.l')
865 In [2]: __IP.complete('x.l')
864 Out[2]: ['x.ljust', 'x.lower', 'x.lstrip']"""
866 Out[2]: ['x.ljust', 'x.lower', 'x.lstrip']"""
865
867
866 complete = self.Completer.complete
868 complete = self.Completer.complete
867 state = 0
869 state = 0
868 # use a dict so we get unique keys, since ipyhton's multiple
870 # use a dict so we get unique keys, since ipyhton's multiple
869 # completers can return duplicates.
871 # completers can return duplicates.
870 comps = {}
872 comps = {}
871 while True:
873 while True:
872 newcomp = complete(text,state)
874 newcomp = complete(text,state)
873 if newcomp is None:
875 if newcomp is None:
874 break
876 break
875 comps[newcomp] = 1
877 comps[newcomp] = 1
876 state += 1
878 state += 1
877 outcomps = comps.keys()
879 outcomps = comps.keys()
878 outcomps.sort()
880 outcomps.sort()
879 return outcomps
881 return outcomps
880
882
881 def set_completer_frame(self, frame):
883 def set_completer_frame(self, frame):
882 if frame:
884 if frame:
883 self.Completer.namespace = frame.f_locals
885 self.Completer.namespace = frame.f_locals
884 self.Completer.global_namespace = frame.f_globals
886 self.Completer.global_namespace = frame.f_globals
885 else:
887 else:
886 self.Completer.namespace = self.user_ns
888 self.Completer.namespace = self.user_ns
887 self.Completer.global_namespace = self.user_global_ns
889 self.Completer.global_namespace = self.user_global_ns
888
890
889 def init_auto_alias(self):
891 def init_auto_alias(self):
890 """Define some aliases automatically.
892 """Define some aliases automatically.
891
893
892 These are ALL parameter-less aliases"""
894 These are ALL parameter-less aliases"""
893 for alias,cmd in self.auto_alias:
895 for alias,cmd in self.auto_alias:
894 self.alias_table[alias] = (0,cmd)
896 self.alias_table[alias] = (0,cmd)
895
897
896 def alias_table_validate(self,verbose=0):
898 def alias_table_validate(self,verbose=0):
897 """Update information about the alias table.
899 """Update information about the alias table.
898
900
899 In particular, make sure no Python keywords/builtins are in it."""
901 In particular, make sure no Python keywords/builtins are in it."""
900
902
901 no_alias = self.no_alias
903 no_alias = self.no_alias
902 for k in self.alias_table.keys():
904 for k in self.alias_table.keys():
903 if k in no_alias:
905 if k in no_alias:
904 del self.alias_table[k]
906 del self.alias_table[k]
905 if verbose:
907 if verbose:
906 print ("Deleting alias <%s>, it's a Python "
908 print ("Deleting alias <%s>, it's a Python "
907 "keyword or builtin." % k)
909 "keyword or builtin." % k)
908
910
909 def set_autoindent(self,value=None):
911 def set_autoindent(self,value=None):
910 """Set the autoindent flag, checking for readline support.
912 """Set the autoindent flag, checking for readline support.
911
913
912 If called with no arguments, it acts as a toggle."""
914 If called with no arguments, it acts as a toggle."""
913
915
914 if not self.has_readline:
916 if not self.has_readline:
915 if os.name == 'posix':
917 if os.name == 'posix':
916 warn("The auto-indent feature requires the readline library")
918 warn("The auto-indent feature requires the readline library")
917 self.autoindent = 0
919 self.autoindent = 0
918 return
920 return
919 if value is None:
921 if value is None:
920 self.autoindent = not self.autoindent
922 self.autoindent = not self.autoindent
921 else:
923 else:
922 self.autoindent = value
924 self.autoindent = value
923
925
924 def rc_set_toggle(self,rc_field,value=None):
926 def rc_set_toggle(self,rc_field,value=None):
925 """Set or toggle a field in IPython's rc config. structure.
927 """Set or toggle a field in IPython's rc config. structure.
926
928
927 If called with no arguments, it acts as a toggle.
929 If called with no arguments, it acts as a toggle.
928
930
929 If called with a non-existent field, the resulting AttributeError
931 If called with a non-existent field, the resulting AttributeError
930 exception will propagate out."""
932 exception will propagate out."""
931
933
932 rc_val = getattr(self.rc,rc_field)
934 rc_val = getattr(self.rc,rc_field)
933 if value is None:
935 if value is None:
934 value = not rc_val
936 value = not rc_val
935 setattr(self.rc,rc_field,value)
937 setattr(self.rc,rc_field,value)
936
938
937 def user_setup(self,ipythondir,rc_suffix,mode='install'):
939 def user_setup(self,ipythondir,rc_suffix,mode='install'):
938 """Install the user configuration directory.
940 """Install the user configuration directory.
939
941
940 Can be called when running for the first time or to upgrade the user's
942 Can be called when running for the first time or to upgrade the user's
941 .ipython/ directory with the mode parameter. Valid modes are 'install'
943 .ipython/ directory with the mode parameter. Valid modes are 'install'
942 and 'upgrade'."""
944 and 'upgrade'."""
943
945
944 def wait():
946 def wait():
945 try:
947 try:
946 raw_input("Please press <RETURN> to start IPython.")
948 raw_input("Please press <RETURN> to start IPython.")
947 except EOFError:
949 except EOFError:
948 print >> Term.cout
950 print >> Term.cout
949 print '*'*70
951 print '*'*70
950
952
951 cwd = os.getcwd() # remember where we started
953 cwd = os.getcwd() # remember where we started
952 glb = glob.glob
954 glb = glob.glob
953 print '*'*70
955 print '*'*70
954 if mode == 'install':
956 if mode == 'install':
955 print \
957 print \
956 """Welcome to IPython. I will try to create a personal configuration directory
958 """Welcome to IPython. I will try to create a personal configuration directory
957 where you can customize many aspects of IPython's functionality in:\n"""
959 where you can customize many aspects of IPython's functionality in:\n"""
958 else:
960 else:
959 print 'I am going to upgrade your configuration in:'
961 print 'I am going to upgrade your configuration in:'
960
962
961 print ipythondir
963 print ipythondir
962
964
963 rcdirend = os.path.join('IPython','UserConfig')
965 rcdirend = os.path.join('IPython','UserConfig')
964 cfg = lambda d: os.path.join(d,rcdirend)
966 cfg = lambda d: os.path.join(d,rcdirend)
965 try:
967 try:
966 rcdir = filter(os.path.isdir,map(cfg,sys.path))[0]
968 rcdir = filter(os.path.isdir,map(cfg,sys.path))[0]
967 except IOError:
969 except IOError:
968 warning = """
970 warning = """
969 Installation error. IPython's directory was not found.
971 Installation error. IPython's directory was not found.
970
972
971 Check the following:
973 Check the following:
972
974
973 The ipython/IPython directory should be in a directory belonging to your
975 The ipython/IPython directory should be in a directory belonging to your
974 PYTHONPATH environment variable (that is, it should be in a directory
976 PYTHONPATH environment variable (that is, it should be in a directory
975 belonging to sys.path). You can copy it explicitly there or just link to it.
977 belonging to sys.path). You can copy it explicitly there or just link to it.
976
978
977 IPython will proceed with builtin defaults.
979 IPython will proceed with builtin defaults.
978 """
980 """
979 warn(warning)
981 warn(warning)
980 wait()
982 wait()
981 return
983 return
982
984
983 if mode == 'install':
985 if mode == 'install':
984 try:
986 try:
985 shutil.copytree(rcdir,ipythondir)
987 shutil.copytree(rcdir,ipythondir)
986 os.chdir(ipythondir)
988 os.chdir(ipythondir)
987 rc_files = glb("ipythonrc*")
989 rc_files = glb("ipythonrc*")
988 for rc_file in rc_files:
990 for rc_file in rc_files:
989 os.rename(rc_file,rc_file+rc_suffix)
991 os.rename(rc_file,rc_file+rc_suffix)
990 except:
992 except:
991 warning = """
993 warning = """
992
994
993 There was a problem with the installation:
995 There was a problem with the installation:
994 %s
996 %s
995 Try to correct it or contact the developers if you think it's a bug.
997 Try to correct it or contact the developers if you think it's a bug.
996 IPython will proceed with builtin defaults.""" % sys.exc_info()[1]
998 IPython will proceed with builtin defaults.""" % sys.exc_info()[1]
997 warn(warning)
999 warn(warning)
998 wait()
1000 wait()
999 return
1001 return
1000
1002
1001 elif mode == 'upgrade':
1003 elif mode == 'upgrade':
1002 try:
1004 try:
1003 os.chdir(ipythondir)
1005 os.chdir(ipythondir)
1004 except:
1006 except:
1005 print """
1007 print """
1006 Can not upgrade: changing to directory %s failed. Details:
1008 Can not upgrade: changing to directory %s failed. Details:
1007 %s
1009 %s
1008 """ % (ipythondir,sys.exc_info()[1])
1010 """ % (ipythondir,sys.exc_info()[1])
1009 wait()
1011 wait()
1010 return
1012 return
1011 else:
1013 else:
1012 sources = glb(os.path.join(rcdir,'[A-Za-z]*'))
1014 sources = glb(os.path.join(rcdir,'[A-Za-z]*'))
1013 for new_full_path in sources:
1015 for new_full_path in sources:
1014 new_filename = os.path.basename(new_full_path)
1016 new_filename = os.path.basename(new_full_path)
1015 if new_filename.startswith('ipythonrc'):
1017 if new_filename.startswith('ipythonrc'):
1016 new_filename = new_filename + rc_suffix
1018 new_filename = new_filename + rc_suffix
1017 # The config directory should only contain files, skip any
1019 # The config directory should only contain files, skip any
1018 # directories which may be there (like CVS)
1020 # directories which may be there (like CVS)
1019 if os.path.isdir(new_full_path):
1021 if os.path.isdir(new_full_path):
1020 continue
1022 continue
1021 if os.path.exists(new_filename):
1023 if os.path.exists(new_filename):
1022 old_file = new_filename+'.old'
1024 old_file = new_filename+'.old'
1023 if os.path.exists(old_file):
1025 if os.path.exists(old_file):
1024 os.remove(old_file)
1026 os.remove(old_file)
1025 os.rename(new_filename,old_file)
1027 os.rename(new_filename,old_file)
1026 shutil.copy(new_full_path,new_filename)
1028 shutil.copy(new_full_path,new_filename)
1027 else:
1029 else:
1028 raise ValueError,'unrecognized mode for install:',`mode`
1030 raise ValueError,'unrecognized mode for install:',`mode`
1029
1031
1030 # Fix line-endings to those native to each platform in the config
1032 # Fix line-endings to those native to each platform in the config
1031 # directory.
1033 # directory.
1032 try:
1034 try:
1033 os.chdir(ipythondir)
1035 os.chdir(ipythondir)
1034 except:
1036 except:
1035 print """
1037 print """
1036 Problem: changing to directory %s failed.
1038 Problem: changing to directory %s failed.
1037 Details:
1039 Details:
1038 %s
1040 %s
1039
1041
1040 Some configuration files may have incorrect line endings. This should not
1042 Some configuration files may have incorrect line endings. This should not
1041 cause any problems during execution. """ % (ipythondir,sys.exc_info()[1])
1043 cause any problems during execution. """ % (ipythondir,sys.exc_info()[1])
1042 wait()
1044 wait()
1043 else:
1045 else:
1044 for fname in glb('ipythonrc*'):
1046 for fname in glb('ipythonrc*'):
1045 try:
1047 try:
1046 native_line_ends(fname,backup=0)
1048 native_line_ends(fname,backup=0)
1047 except IOError:
1049 except IOError:
1048 pass
1050 pass
1049
1051
1050 if mode == 'install':
1052 if mode == 'install':
1051 print """
1053 print """
1052 Successful installation!
1054 Successful installation!
1053
1055
1054 Please read the sections 'Initial Configuration' and 'Quick Tips' in the
1056 Please read the sections 'Initial Configuration' and 'Quick Tips' in the
1055 IPython manual (there are both HTML and PDF versions supplied with the
1057 IPython manual (there are both HTML and PDF versions supplied with the
1056 distribution) to make sure that your system environment is properly configured
1058 distribution) to make sure that your system environment is properly configured
1057 to take advantage of IPython's features."""
1059 to take advantage of IPython's features."""
1058 else:
1060 else:
1059 print """
1061 print """
1060 Successful upgrade!
1062 Successful upgrade!
1061
1063
1062 All files in your directory:
1064 All files in your directory:
1063 %(ipythondir)s
1065 %(ipythondir)s
1064 which would have been overwritten by the upgrade were backed up with a .old
1066 which would have been overwritten by the upgrade were backed up with a .old
1065 extension. If you had made particular customizations in those files you may
1067 extension. If you had made particular customizations in those files you may
1066 want to merge them back into the new files.""" % locals()
1068 want to merge them back into the new files.""" % locals()
1067 wait()
1069 wait()
1068 os.chdir(cwd)
1070 os.chdir(cwd)
1069 # end user_setup()
1071 # end user_setup()
1070
1072
1071 def atexit_operations(self):
1073 def atexit_operations(self):
1072 """This will be executed at the time of exit.
1074 """This will be executed at the time of exit.
1073
1075
1074 Saving of persistent data should be performed here. """
1076 Saving of persistent data should be performed here. """
1075
1077
1076 # input history
1078 # input history
1077 self.savehist()
1079 self.savehist()
1078
1080
1079 # Cleanup all tempfiles left around
1081 # Cleanup all tempfiles left around
1080 for tfile in self.tempfiles:
1082 for tfile in self.tempfiles:
1081 try:
1083 try:
1082 os.unlink(tfile)
1084 os.unlink(tfile)
1083 except OSError:
1085 except OSError:
1084 pass
1086 pass
1085
1087
1086 # save the "persistent data" catch-all dictionary
1088 # save the "persistent data" catch-all dictionary
1087 try:
1089 try:
1088 pickle.dump(self.persist, open(self.persist_fname,"w"))
1090 pickle.dump(self.persist, open(self.persist_fname,"w"))
1089 except:
1091 except:
1090 print "*** ERROR *** persistent data saving failed."
1092 print "*** ERROR *** persistent data saving failed."
1091
1093
1092 def savehist(self):
1094 def savehist(self):
1093 """Save input history to a file (via readline library)."""
1095 """Save input history to a file (via readline library)."""
1094 try:
1096 try:
1095 self.readline.write_history_file(self.histfile)
1097 self.readline.write_history_file(self.histfile)
1096 except:
1098 except:
1097 print 'Unable to save IPython command history to file: ' + \
1099 print 'Unable to save IPython command history to file: ' + \
1098 `self.histfile`
1100 `self.histfile`
1099
1101
1100 def pre_readline(self):
1102 def pre_readline(self):
1101 """readline hook to be used at the start of each line.
1103 """readline hook to be used at the start of each line.
1102
1104
1103 Currently it handles auto-indent only."""
1105 Currently it handles auto-indent only."""
1104
1106
1105 self.readline.insert_text(self.indent_current)
1107 self.readline.insert_text(self.indent_current)
1106
1108
1107 def init_readline(self):
1109 def init_readline(self):
1108 """Command history completion/saving/reloading."""
1110 """Command history completion/saving/reloading."""
1109 try:
1111 try:
1110 import readline
1112 import readline
1111 except ImportError:
1113 except ImportError:
1112 self.has_readline = 0
1114 self.has_readline = 0
1113 self.readline = None
1115 self.readline = None
1114 # no point in bugging windows users with this every time:
1116 # no point in bugging windows users with this every time:
1115 if os.name == 'posix':
1117 if os.name == 'posix':
1116 warn('Readline services not available on this platform.')
1118 warn('Readline services not available on this platform.')
1117 else:
1119 else:
1118 import atexit
1120 import atexit
1119 from IPython.completer import IPCompleter
1121 from IPython.completer import IPCompleter
1120 self.Completer = IPCompleter(self,
1122 self.Completer = IPCompleter(self,
1121 self.user_ns,
1123 self.user_ns,
1122 self.user_global_ns,
1124 self.user_global_ns,
1123 self.rc.readline_omit__names,
1125 self.rc.readline_omit__names,
1124 self.alias_table)
1126 self.alias_table)
1125
1127
1126 # Platform-specific configuration
1128 # Platform-specific configuration
1127 if os.name == 'nt':
1129 if os.name == 'nt':
1128 self.readline_startup_hook = readline.set_pre_input_hook
1130 self.readline_startup_hook = readline.set_pre_input_hook
1129 else:
1131 else:
1130 self.readline_startup_hook = readline.set_startup_hook
1132 self.readline_startup_hook = readline.set_startup_hook
1131
1133
1132 # Load user's initrc file (readline config)
1134 # Load user's initrc file (readline config)
1133 inputrc_name = os.environ.get('INPUTRC')
1135 inputrc_name = os.environ.get('INPUTRC')
1134 if inputrc_name is None:
1136 if inputrc_name is None:
1135 home_dir = get_home_dir()
1137 home_dir = get_home_dir()
1136 if home_dir is not None:
1138 if home_dir is not None:
1137 inputrc_name = os.path.join(home_dir,'.inputrc')
1139 inputrc_name = os.path.join(home_dir,'.inputrc')
1138 if os.path.isfile(inputrc_name):
1140 if os.path.isfile(inputrc_name):
1139 try:
1141 try:
1140 readline.read_init_file(inputrc_name)
1142 readline.read_init_file(inputrc_name)
1141 except:
1143 except:
1142 warn('Problems reading readline initialization file <%s>'
1144 warn('Problems reading readline initialization file <%s>'
1143 % inputrc_name)
1145 % inputrc_name)
1144
1146
1145 self.has_readline = 1
1147 self.has_readline = 1
1146 self.readline = readline
1148 self.readline = readline
1147 # save this in sys so embedded copies can restore it properly
1149 # save this in sys so embedded copies can restore it properly
1148 sys.ipcompleter = self.Completer.complete
1150 sys.ipcompleter = self.Completer.complete
1149 readline.set_completer(self.Completer.complete)
1151 readline.set_completer(self.Completer.complete)
1150
1152
1151 # Configure readline according to user's prefs
1153 # Configure readline according to user's prefs
1152 for rlcommand in self.rc.readline_parse_and_bind:
1154 for rlcommand in self.rc.readline_parse_and_bind:
1153 readline.parse_and_bind(rlcommand)
1155 readline.parse_and_bind(rlcommand)
1154
1156
1155 # remove some chars from the delimiters list
1157 # remove some chars from the delimiters list
1156 delims = readline.get_completer_delims()
1158 delims = readline.get_completer_delims()
1157 delims = delims.translate(string._idmap,
1159 delims = delims.translate(string._idmap,
1158 self.rc.readline_remove_delims)
1160 self.rc.readline_remove_delims)
1159 readline.set_completer_delims(delims)
1161 readline.set_completer_delims(delims)
1160 # otherwise we end up with a monster history after a while:
1162 # otherwise we end up with a monster history after a while:
1161 readline.set_history_length(1000)
1163 readline.set_history_length(1000)
1162 try:
1164 try:
1163 #print '*** Reading readline history' # dbg
1165 #print '*** Reading readline history' # dbg
1164 readline.read_history_file(self.histfile)
1166 readline.read_history_file(self.histfile)
1165 except IOError:
1167 except IOError:
1166 pass # It doesn't exist yet.
1168 pass # It doesn't exist yet.
1167
1169
1168 atexit.register(self.atexit_operations)
1170 atexit.register(self.atexit_operations)
1169 del atexit
1171 del atexit
1170
1172
1171 # Configure auto-indent for all platforms
1173 # Configure auto-indent for all platforms
1172 self.set_autoindent(self.rc.autoindent)
1174 self.set_autoindent(self.rc.autoindent)
1173
1175
1174 def _should_recompile(self,e):
1176 def _should_recompile(self,e):
1175 """Utility routine for edit_syntax_error"""
1177 """Utility routine for edit_syntax_error"""
1176
1178
1177 if e.filename in ('<ipython console>','<input>','<string>',
1179 if e.filename in ('<ipython console>','<input>','<string>',
1178 '<console>'):
1180 '<console>'):
1179 return False
1181 return False
1180 try:
1182 try:
1181 if not ask_yes_no('Return to editor to correct syntax error? '
1183 if not ask_yes_no('Return to editor to correct syntax error? '
1182 '[Y/n] ','y'):
1184 '[Y/n] ','y'):
1183 return False
1185 return False
1184 except EOFError:
1186 except EOFError:
1185 return False
1187 return False
1186 self.hooks.fix_error_editor(e.filename,e.lineno,e.offset,e.msg)
1188
1189 def int0(x):
1190 try:
1191 return int(x)
1192 except TypeError:
1193 return 0
1194 # always pass integer line and offset values to editor hook
1195 self.hooks.fix_error_editor(e.filename,
1196 int0(e.lineno),int0(e.offset),e.msg)
1187 return True
1197 return True
1188
1198
1189 def edit_syntax_error(self):
1199 def edit_syntax_error(self):
1190 """The bottom half of the syntax error handler called in the main loop.
1200 """The bottom half of the syntax error handler called in the main loop.
1191
1201
1192 Loop until syntax error is fixed or user cancels.
1202 Loop until syntax error is fixed or user cancels.
1193 """
1203 """
1194
1204
1195 while self.SyntaxTB.last_syntax_error:
1205 while self.SyntaxTB.last_syntax_error:
1196 # copy and clear last_syntax_error
1206 # copy and clear last_syntax_error
1197 err = self.SyntaxTB.clear_err_state()
1207 err = self.SyntaxTB.clear_err_state()
1198 if not self._should_recompile(err):
1208 if not self._should_recompile(err):
1199 return
1209 return
1200 try:
1210 try:
1201 # may set last_syntax_error again if a SyntaxError is raised
1211 # may set last_syntax_error again if a SyntaxError is raised
1202 self.safe_execfile(err.filename,self.shell.user_ns)
1212 self.safe_execfile(err.filename,self.shell.user_ns)
1203 except:
1213 except:
1204 self.showtraceback()
1214 self.showtraceback()
1205 else:
1215 else:
1206 f = file(err.filename)
1216 f = file(err.filename)
1207 try:
1217 try:
1208 sys.displayhook(f.read())
1218 sys.displayhook(f.read())
1209 finally:
1219 finally:
1210 f.close()
1220 f.close()
1211
1221
1212 def showsyntaxerror(self, filename=None):
1222 def showsyntaxerror(self, filename=None):
1213 """Display the syntax error that just occurred.
1223 """Display the syntax error that just occurred.
1214
1224
1215 This doesn't display a stack trace because there isn't one.
1225 This doesn't display a stack trace because there isn't one.
1216
1226
1217 If a filename is given, it is stuffed in the exception instead
1227 If a filename is given, it is stuffed in the exception instead
1218 of what was there before (because Python's parser always uses
1228 of what was there before (because Python's parser always uses
1219 "<string>" when reading from a string).
1229 "<string>" when reading from a string).
1220 """
1230 """
1221 etype, value, last_traceback = sys.exc_info()
1231 etype, value, last_traceback = sys.exc_info()
1222 if filename and etype is SyntaxError:
1232 if filename and etype is SyntaxError:
1223 # Work hard to stuff the correct filename in the exception
1233 # Work hard to stuff the correct filename in the exception
1224 try:
1234 try:
1225 msg, (dummy_filename, lineno, offset, line) = value
1235 msg, (dummy_filename, lineno, offset, line) = value
1226 except:
1236 except:
1227 # Not the format we expect; leave it alone
1237 # Not the format we expect; leave it alone
1228 pass
1238 pass
1229 else:
1239 else:
1230 # Stuff in the right filename
1240 # Stuff in the right filename
1231 try:
1241 try:
1232 # Assume SyntaxError is a class exception
1242 # Assume SyntaxError is a class exception
1233 value = SyntaxError(msg, (filename, lineno, offset, line))
1243 value = SyntaxError(msg, (filename, lineno, offset, line))
1234 except:
1244 except:
1235 # If that failed, assume SyntaxError is a string
1245 # If that failed, assume SyntaxError is a string
1236 value = msg, (filename, lineno, offset, line)
1246 value = msg, (filename, lineno, offset, line)
1237 self.SyntaxTB(etype,value,[])
1247 self.SyntaxTB(etype,value,[])
1238
1248
1239 def debugger(self):
1249 def debugger(self):
1240 """Call the pdb debugger."""
1250 """Call the pdb debugger."""
1241
1251
1242 if not self.rc.pdb:
1252 if not self.rc.pdb:
1243 return
1253 return
1244 pdb.pm()
1254 pdb.pm()
1245
1255
1246 def showtraceback(self,exc_tuple = None,filename=None):
1256 def showtraceback(self,exc_tuple = None,filename=None):
1247 """Display the exception that just occurred."""
1257 """Display the exception that just occurred."""
1248
1258
1249 # Though this won't be called by syntax errors in the input line,
1259 # Though this won't be called by syntax errors in the input line,
1250 # there may be SyntaxError cases whith imported code.
1260 # there may be SyntaxError cases whith imported code.
1251 if exc_tuple is None:
1261 if exc_tuple is None:
1252 type, value, tb = sys.exc_info()
1262 type, value, tb = sys.exc_info()
1253 else:
1263 else:
1254 type, value, tb = exc_tuple
1264 type, value, tb = exc_tuple
1255 if type is SyntaxError:
1265 if type is SyntaxError:
1256 self.showsyntaxerror(filename)
1266 self.showsyntaxerror(filename)
1257 else:
1267 else:
1258 self.InteractiveTB()
1268 self.InteractiveTB()
1259 if self.InteractiveTB.call_pdb and self.has_readline:
1269 if self.InteractiveTB.call_pdb and self.has_readline:
1260 # pdb mucks up readline, fix it back
1270 # pdb mucks up readline, fix it back
1261 self.readline.set_completer(self.Completer.complete)
1271 self.readline.set_completer(self.Completer.complete)
1262
1272
1263 def mainloop(self,banner=None):
1273 def mainloop(self,banner=None):
1264 """Creates the local namespace and starts the mainloop.
1274 """Creates the local namespace and starts the mainloop.
1265
1275
1266 If an optional banner argument is given, it will override the
1276 If an optional banner argument is given, it will override the
1267 internally created default banner."""
1277 internally created default banner."""
1268
1278
1269 if self.rc.c: # Emulate Python's -c option
1279 if self.rc.c: # Emulate Python's -c option
1270 self.exec_init_cmd()
1280 self.exec_init_cmd()
1271 if banner is None:
1281 if banner is None:
1272 if self.rc.banner:
1282 if self.rc.banner:
1273 banner = self.BANNER+self.banner2
1283 banner = self.BANNER+self.banner2
1274 else:
1284 else:
1275 banner = ''
1285 banner = ''
1276 self.interact(banner)
1286 self.interact(banner)
1277
1287
1278 def exec_init_cmd(self):
1288 def exec_init_cmd(self):
1279 """Execute a command given at the command line.
1289 """Execute a command given at the command line.
1280
1290
1281 This emulates Python's -c option."""
1291 This emulates Python's -c option."""
1282
1292
1283 sys.argv = ['-c']
1293 sys.argv = ['-c']
1284 self.push(self.rc.c)
1294 self.push(self.rc.c)
1285
1295
1286 def embed_mainloop(self,header='',local_ns=None,global_ns=None,stack_depth=0):
1296 def embed_mainloop(self,header='',local_ns=None,global_ns=None,stack_depth=0):
1287 """Embeds IPython into a running python program.
1297 """Embeds IPython into a running python program.
1288
1298
1289 Input:
1299 Input:
1290
1300
1291 - header: An optional header message can be specified.
1301 - header: An optional header message can be specified.
1292
1302
1293 - local_ns, global_ns: working namespaces. If given as None, the
1303 - local_ns, global_ns: working namespaces. If given as None, the
1294 IPython-initialized one is updated with __main__.__dict__, so that
1304 IPython-initialized one is updated with __main__.__dict__, so that
1295 program variables become visible but user-specific configuration
1305 program variables become visible but user-specific configuration
1296 remains possible.
1306 remains possible.
1297
1307
1298 - stack_depth: specifies how many levels in the stack to go to
1308 - stack_depth: specifies how many levels in the stack to go to
1299 looking for namespaces (when local_ns and global_ns are None). This
1309 looking for namespaces (when local_ns and global_ns are None). This
1300 allows an intermediate caller to make sure that this function gets
1310 allows an intermediate caller to make sure that this function gets
1301 the namespace from the intended level in the stack. By default (0)
1311 the namespace from the intended level in the stack. By default (0)
1302 it will get its locals and globals from the immediate caller.
1312 it will get its locals and globals from the immediate caller.
1303
1313
1304 Warning: it's possible to use this in a program which is being run by
1314 Warning: it's possible to use this in a program which is being run by
1305 IPython itself (via %run), but some funny things will happen (a few
1315 IPython itself (via %run), but some funny things will happen (a few
1306 globals get overwritten). In the future this will be cleaned up, as
1316 globals get overwritten). In the future this will be cleaned up, as
1307 there is no fundamental reason why it can't work perfectly."""
1317 there is no fundamental reason why it can't work perfectly."""
1308
1318
1309 # Get locals and globals from caller
1319 # Get locals and globals from caller
1310 if local_ns is None or global_ns is None:
1320 if local_ns is None or global_ns is None:
1311 call_frame = sys._getframe(stack_depth).f_back
1321 call_frame = sys._getframe(stack_depth).f_back
1312
1322
1313 if local_ns is None:
1323 if local_ns is None:
1314 local_ns = call_frame.f_locals
1324 local_ns = call_frame.f_locals
1315 if global_ns is None:
1325 if global_ns is None:
1316 global_ns = call_frame.f_globals
1326 global_ns = call_frame.f_globals
1317
1327
1318 # Update namespaces and fire up interpreter
1328 # Update namespaces and fire up interpreter
1319
1329
1320 # The global one is easy, we can just throw it in
1330 # The global one is easy, we can just throw it in
1321 self.user_global_ns = global_ns
1331 self.user_global_ns = global_ns
1322
1332
1323 # but the user/local one is tricky: ipython needs it to store internal
1333 # but the user/local one is tricky: ipython needs it to store internal
1324 # data, but we also need the locals. We'll copy locals in the user
1334 # data, but we also need the locals. We'll copy locals in the user
1325 # one, but will track what got copied so we can delete them at exit.
1335 # one, but will track what got copied so we can delete them at exit.
1326 # This is so that a later embedded call doesn't see locals from a
1336 # This is so that a later embedded call doesn't see locals from a
1327 # previous call (which most likely existed in a separate scope).
1337 # previous call (which most likely existed in a separate scope).
1328 local_varnames = local_ns.keys()
1338 local_varnames = local_ns.keys()
1329 self.user_ns.update(local_ns)
1339 self.user_ns.update(local_ns)
1330
1340
1331 # Patch for global embedding to make sure that things don't overwrite
1341 # Patch for global embedding to make sure that things don't overwrite
1332 # user globals accidentally. Thanks to Richard <rxe@renre-europe.com>
1342 # user globals accidentally. Thanks to Richard <rxe@renre-europe.com>
1333 # FIXME. Test this a bit more carefully (the if.. is new)
1343 # FIXME. Test this a bit more carefully (the if.. is new)
1334 if local_ns is None and global_ns is None:
1344 if local_ns is None and global_ns is None:
1335 self.user_global_ns.update(__main__.__dict__)
1345 self.user_global_ns.update(__main__.__dict__)
1336
1346
1337 # make sure the tab-completer has the correct frame information, so it
1347 # make sure the tab-completer has the correct frame information, so it
1338 # actually completes using the frame's locals/globals
1348 # actually completes using the frame's locals/globals
1339 self.set_completer_frame(call_frame)
1349 self.set_completer_frame(call_frame)
1340
1350
1341 # before activating the interactive mode, we need to make sure that
1351 # before activating the interactive mode, we need to make sure that
1342 # all names in the builtin namespace needed by ipython point to
1352 # all names in the builtin namespace needed by ipython point to
1343 # ourselves, and not to other instances.
1353 # ourselves, and not to other instances.
1344 self.add_builtins()
1354 self.add_builtins()
1345
1355
1346 self.interact(header)
1356 self.interact(header)
1347
1357
1348 # now, purge out the user namespace from anything we might have added
1358 # now, purge out the user namespace from anything we might have added
1349 # from the caller's local namespace
1359 # from the caller's local namespace
1350 delvar = self.user_ns.pop
1360 delvar = self.user_ns.pop
1351 for var in local_varnames:
1361 for var in local_varnames:
1352 delvar(var,None)
1362 delvar(var,None)
1353 # and clean builtins we may have overridden
1363 # and clean builtins we may have overridden
1354 self.clean_builtins()
1364 self.clean_builtins()
1355
1365
1356 def interact(self, banner=None):
1366 def interact(self, banner=None):
1357 """Closely emulate the interactive Python console.
1367 """Closely emulate the interactive Python console.
1358
1368
1359 The optional banner argument specify the banner to print
1369 The optional banner argument specify the banner to print
1360 before the first interaction; by default it prints a banner
1370 before the first interaction; by default it prints a banner
1361 similar to the one printed by the real Python interpreter,
1371 similar to the one printed by the real Python interpreter,
1362 followed by the current class name in parentheses (so as not
1372 followed by the current class name in parentheses (so as not
1363 to confuse this with the real interpreter -- since it's so
1373 to confuse this with the real interpreter -- since it's so
1364 close!).
1374 close!).
1365
1375
1366 """
1376 """
1367 cprt = 'Type "copyright", "credits" or "license" for more information.'
1377 cprt = 'Type "copyright", "credits" or "license" for more information.'
1368 if banner is None:
1378 if banner is None:
1369 self.write("Python %s on %s\n%s\n(%s)\n" %
1379 self.write("Python %s on %s\n%s\n(%s)\n" %
1370 (sys.version, sys.platform, cprt,
1380 (sys.version, sys.platform, cprt,
1371 self.__class__.__name__))
1381 self.__class__.__name__))
1372 else:
1382 else:
1373 self.write(banner)
1383 self.write(banner)
1374
1384
1375 more = 0
1385 more = 0
1376
1386
1377 # Mark activity in the builtins
1387 # Mark activity in the builtins
1378 __builtin__.__dict__['__IPYTHON__active'] += 1
1388 __builtin__.__dict__['__IPYTHON__active'] += 1
1379
1389
1380 # exit_now is set by a call to %Exit or %Quit
1390 # exit_now is set by a call to %Exit or %Quit
1381 self.exit_now = False
1391 self.exit_now = False
1382 while not self.exit_now:
1392 while not self.exit_now:
1383
1393
1384 try:
1394 try:
1385 if more:
1395 if more:
1386 prompt = self.outputcache.prompt2
1396 prompt = self.outputcache.prompt2
1387 if self.autoindent:
1397 if self.autoindent:
1388 self.readline_startup_hook(self.pre_readline)
1398 self.readline_startup_hook(self.pre_readline)
1389 else:
1399 else:
1390 prompt = self.outputcache.prompt1
1400 prompt = self.outputcache.prompt1
1391 try:
1401 try:
1392 line = self.raw_input(prompt,more)
1402 line = self.raw_input(prompt,more)
1393 if self.autoindent:
1403 if self.autoindent:
1394 self.readline_startup_hook(None)
1404 self.readline_startup_hook(None)
1395 except EOFError:
1405 except EOFError:
1396 if self.autoindent:
1406 if self.autoindent:
1397 self.readline_startup_hook(None)
1407 self.readline_startup_hook(None)
1398 self.write("\n")
1408 self.write("\n")
1399 self.exit()
1409 self.exit()
1400 else:
1410 else:
1401 more = self.push(line)
1411 more = self.push(line)
1402
1412
1403 if (self.SyntaxTB.last_syntax_error and
1413 if (self.SyntaxTB.last_syntax_error and
1404 self.rc.autoedit_syntax):
1414 self.rc.autoedit_syntax):
1405 self.edit_syntax_error()
1415 self.edit_syntax_error()
1406
1416
1407 except KeyboardInterrupt:
1417 except KeyboardInterrupt:
1408 self.write("\nKeyboardInterrupt\n")
1418 self.write("\nKeyboardInterrupt\n")
1409 self.resetbuffer()
1419 self.resetbuffer()
1410 more = 0
1420 more = 0
1411 # keep cache in sync with the prompt counter:
1421 # keep cache in sync with the prompt counter:
1412 self.outputcache.prompt_count -= 1
1422 self.outputcache.prompt_count -= 1
1413
1423
1414 if self.autoindent:
1424 if self.autoindent:
1415 self.indent_current_nsp = 0
1425 self.indent_current_nsp = 0
1416 self.indent_current = ' '* self.indent_current_nsp
1426 self.indent_current = ' '* self.indent_current_nsp
1417
1427
1418 except bdb.BdbQuit:
1428 except bdb.BdbQuit:
1419 warn("The Python debugger has exited with a BdbQuit exception.\n"
1429 warn("The Python debugger has exited with a BdbQuit exception.\n"
1420 "Because of how pdb handles the stack, it is impossible\n"
1430 "Because of how pdb handles the stack, it is impossible\n"
1421 "for IPython to properly format this particular exception.\n"
1431 "for IPython to properly format this particular exception.\n"
1422 "IPython will resume normal operation.")
1432 "IPython will resume normal operation.")
1423
1433
1424 # We are off again...
1434 # We are off again...
1425 __builtin__.__dict__['__IPYTHON__active'] -= 1
1435 __builtin__.__dict__['__IPYTHON__active'] -= 1
1426
1436
1427 def excepthook(self, type, value, tb):
1437 def excepthook(self, type, value, tb):
1428 """One more defense for GUI apps that call sys.excepthook.
1438 """One more defense for GUI apps that call sys.excepthook.
1429
1439
1430 GUI frameworks like wxPython trap exceptions and call
1440 GUI frameworks like wxPython trap exceptions and call
1431 sys.excepthook themselves. I guess this is a feature that
1441 sys.excepthook themselves. I guess this is a feature that
1432 enables them to keep running after exceptions that would
1442 enables them to keep running after exceptions that would
1433 otherwise kill their mainloop. This is a bother for IPython
1443 otherwise kill their mainloop. This is a bother for IPython
1434 which excepts to catch all of the program exceptions with a try:
1444 which excepts to catch all of the program exceptions with a try:
1435 except: statement.
1445 except: statement.
1436
1446
1437 Normally, IPython sets sys.excepthook to a CrashHandler instance, so if
1447 Normally, IPython sets sys.excepthook to a CrashHandler instance, so if
1438 any app directly invokes sys.excepthook, it will look to the user like
1448 any app directly invokes sys.excepthook, it will look to the user like
1439 IPython crashed. In order to work around this, we can disable the
1449 IPython crashed. In order to work around this, we can disable the
1440 CrashHandler and replace it with this excepthook instead, which prints a
1450 CrashHandler and replace it with this excepthook instead, which prints a
1441 regular traceback using our InteractiveTB. In this fashion, apps which
1451 regular traceback using our InteractiveTB. In this fashion, apps which
1442 call sys.excepthook will generate a regular-looking exception from
1452 call sys.excepthook will generate a regular-looking exception from
1443 IPython, and the CrashHandler will only be triggered by real IPython
1453 IPython, and the CrashHandler will only be triggered by real IPython
1444 crashes.
1454 crashes.
1445
1455
1446 This hook should be used sparingly, only in places which are not likely
1456 This hook should be used sparingly, only in places which are not likely
1447 to be true IPython errors.
1457 to be true IPython errors.
1448 """
1458 """
1449
1459
1450 self.InteractiveTB(type, value, tb, tb_offset=0)
1460 self.InteractiveTB(type, value, tb, tb_offset=0)
1451 if self.InteractiveTB.call_pdb and self.has_readline:
1461 if self.InteractiveTB.call_pdb and self.has_readline:
1452 self.readline.set_completer(self.Completer.complete)
1462 self.readline.set_completer(self.Completer.complete)
1453
1463
1454 def call_alias(self,alias,rest=''):
1464 def call_alias(self,alias,rest=''):
1455 """Call an alias given its name and the rest of the line.
1465 """Call an alias given its name and the rest of the line.
1456
1466
1457 This function MUST be given a proper alias, because it doesn't make
1467 This function MUST be given a proper alias, because it doesn't make
1458 any checks when looking up into the alias table. The caller is
1468 any checks when looking up into the alias table. The caller is
1459 responsible for invoking it only with a valid alias."""
1469 responsible for invoking it only with a valid alias."""
1460
1470
1461 #print 'ALIAS: <%s>+<%s>' % (alias,rest) # dbg
1471 #print 'ALIAS: <%s>+<%s>' % (alias,rest) # dbg
1462 nargs,cmd = self.alias_table[alias]
1472 nargs,cmd = self.alias_table[alias]
1463 # Expand the %l special to be the user's input line
1473 # Expand the %l special to be the user's input line
1464 if cmd.find('%l') >= 0:
1474 if cmd.find('%l') >= 0:
1465 cmd = cmd.replace('%l',rest)
1475 cmd = cmd.replace('%l',rest)
1466 rest = ''
1476 rest = ''
1467 if nargs==0:
1477 if nargs==0:
1468 # Simple, argument-less aliases
1478 # Simple, argument-less aliases
1469 cmd = '%s %s' % (cmd,rest)
1479 cmd = '%s %s' % (cmd,rest)
1470 else:
1480 else:
1471 # Handle aliases with positional arguments
1481 # Handle aliases with positional arguments
1472 args = rest.split(None,nargs)
1482 args = rest.split(None,nargs)
1473 if len(args)< nargs:
1483 if len(args)< nargs:
1474 error('Alias <%s> requires %s arguments, %s given.' %
1484 error('Alias <%s> requires %s arguments, %s given.' %
1475 (alias,nargs,len(args)))
1485 (alias,nargs,len(args)))
1476 return
1486 return
1477 cmd = '%s %s' % (cmd % tuple(args[:nargs]),' '.join(args[nargs:]))
1487 cmd = '%s %s' % (cmd % tuple(args[:nargs]),' '.join(args[nargs:]))
1478 # Now call the macro, evaluating in the user's namespace
1488 # Now call the macro, evaluating in the user's namespace
1479 try:
1489 try:
1480 self.system(cmd)
1490 self.system(cmd)
1481 except:
1491 except:
1482 self.showtraceback()
1492 self.showtraceback()
1483
1493
1484 def autoindent_update(self,line):
1494 def autoindent_update(self,line):
1485 """Keep track of the indent level."""
1495 """Keep track of the indent level."""
1486 if self.autoindent:
1496 if self.autoindent:
1487 if line:
1497 if line:
1488 ini_spaces = ini_spaces_re.match(line)
1498 ini_spaces = ini_spaces_re.match(line)
1489 if ini_spaces:
1499 if ini_spaces:
1490 nspaces = ini_spaces.end()
1500 nspaces = ini_spaces.end()
1491 else:
1501 else:
1492 nspaces = 0
1502 nspaces = 0
1493 self.indent_current_nsp = nspaces
1503 self.indent_current_nsp = nspaces
1494
1504
1495 if line[-1] == ':':
1505 if line[-1] == ':':
1496 self.indent_current_nsp += 4
1506 self.indent_current_nsp += 4
1497 elif dedent_re.match(line):
1507 elif dedent_re.match(line):
1498 self.indent_current_nsp -= 4
1508 self.indent_current_nsp -= 4
1499 else:
1509 else:
1500 self.indent_current_nsp = 0
1510 self.indent_current_nsp = 0
1501
1511
1502 # indent_current is the actual string to be inserted
1512 # indent_current is the actual string to be inserted
1503 # by the readline hooks for indentation
1513 # by the readline hooks for indentation
1504 self.indent_current = ' '* self.indent_current_nsp
1514 self.indent_current = ' '* self.indent_current_nsp
1505
1515
1506 def runlines(self,lines):
1516 def runlines(self,lines):
1507 """Run a string of one or more lines of source.
1517 """Run a string of one or more lines of source.
1508
1518
1509 This method is capable of running a string containing multiple source
1519 This method is capable of running a string containing multiple source
1510 lines, as if they had been entered at the IPython prompt. Since it
1520 lines, as if they had been entered at the IPython prompt. Since it
1511 exposes IPython's processing machinery, the given strings can contain
1521 exposes IPython's processing machinery, the given strings can contain
1512 magic calls (%magic), special shell access (!cmd), etc."""
1522 magic calls (%magic), special shell access (!cmd), etc."""
1513
1523
1514 # We must start with a clean buffer, in case this is run from an
1524 # We must start with a clean buffer, in case this is run from an
1515 # interactive IPython session (via a magic, for example).
1525 # interactive IPython session (via a magic, for example).
1516 self.resetbuffer()
1526 self.resetbuffer()
1517 lines = lines.split('\n')
1527 lines = lines.split('\n')
1518 more = 0
1528 more = 0
1519 for line in lines:
1529 for line in lines:
1520 # skip blank lines so we don't mess up the prompt counter, but do
1530 # skip blank lines so we don't mess up the prompt counter, but do
1521 # NOT skip even a blank line if we are in a code block (more is
1531 # NOT skip even a blank line if we are in a code block (more is
1522 # true)
1532 # true)
1523 if line or more:
1533 if line or more:
1524 more = self.push(self.prefilter(line,more))
1534 more = self.push(self.prefilter(line,more))
1525 # IPython's runsource returns None if there was an error
1535 # IPython's runsource returns None if there was an error
1526 # compiling the code. This allows us to stop processing right
1536 # compiling the code. This allows us to stop processing right
1527 # away, so the user gets the error message at the right place.
1537 # away, so the user gets the error message at the right place.
1528 if more is None:
1538 if more is None:
1529 break
1539 break
1530 # final newline in case the input didn't have it, so that the code
1540 # final newline in case the input didn't have it, so that the code
1531 # actually does get executed
1541 # actually does get executed
1532 if more:
1542 if more:
1533 self.push('\n')
1543 self.push('\n')
1534
1544
1535 def runsource(self, source, filename='<input>', symbol='single'):
1545 def runsource(self, source, filename='<input>', symbol='single'):
1536 """Compile and run some source in the interpreter.
1546 """Compile and run some source in the interpreter.
1537
1547
1538 Arguments are as for compile_command().
1548 Arguments are as for compile_command().
1539
1549
1540 One several things can happen:
1550 One several things can happen:
1541
1551
1542 1) The input is incorrect; compile_command() raised an
1552 1) The input is incorrect; compile_command() raised an
1543 exception (SyntaxError or OverflowError). A syntax traceback
1553 exception (SyntaxError or OverflowError). A syntax traceback
1544 will be printed by calling the showsyntaxerror() method.
1554 will be printed by calling the showsyntaxerror() method.
1545
1555
1546 2) The input is incomplete, and more input is required;
1556 2) The input is incomplete, and more input is required;
1547 compile_command() returned None. Nothing happens.
1557 compile_command() returned None. Nothing happens.
1548
1558
1549 3) The input is complete; compile_command() returned a code
1559 3) The input is complete; compile_command() returned a code
1550 object. The code is executed by calling self.runcode() (which
1560 object. The code is executed by calling self.runcode() (which
1551 also handles run-time exceptions, except for SystemExit).
1561 also handles run-time exceptions, except for SystemExit).
1552
1562
1553 The return value is:
1563 The return value is:
1554
1564
1555 - True in case 2
1565 - True in case 2
1556
1566
1557 - False in the other cases, unless an exception is raised, where
1567 - False in the other cases, unless an exception is raised, where
1558 None is returned instead. This can be used by external callers to
1568 None is returned instead. This can be used by external callers to
1559 know whether to continue feeding input or not.
1569 know whether to continue feeding input or not.
1560
1570
1561 The return value can be used to decide whether to use sys.ps1 or
1571 The return value can be used to decide whether to use sys.ps1 or
1562 sys.ps2 to prompt the next line."""
1572 sys.ps2 to prompt the next line."""
1563
1573
1564 try:
1574 try:
1565 code = self.compile(source,filename,symbol)
1575 code = self.compile(source,filename,symbol)
1566 except (OverflowError, SyntaxError, ValueError):
1576 except (OverflowError, SyntaxError, ValueError):
1567 # Case 1
1577 # Case 1
1568 self.showsyntaxerror(filename)
1578 self.showsyntaxerror(filename)
1569 return None
1579 return None
1570
1580
1571 if code is None:
1581 if code is None:
1572 # Case 2
1582 # Case 2
1573 return True
1583 return True
1574
1584
1575 # Case 3
1585 # Case 3
1576 # We store the code object so that threaded shells and
1586 # We store the code object so that threaded shells and
1577 # custom exception handlers can access all this info if needed.
1587 # custom exception handlers can access all this info if needed.
1578 # The source corresponding to this can be obtained from the
1588 # The source corresponding to this can be obtained from the
1579 # buffer attribute as '\n'.join(self.buffer).
1589 # buffer attribute as '\n'.join(self.buffer).
1580 self.code_to_run = code
1590 self.code_to_run = code
1581 # now actually execute the code object
1591 # now actually execute the code object
1582 if self.runcode(code) == 0:
1592 if self.runcode(code) == 0:
1583 return False
1593 return False
1584 else:
1594 else:
1585 return None
1595 return None
1586
1596
1587 def runcode(self,code_obj):
1597 def runcode(self,code_obj):
1588 """Execute a code object.
1598 """Execute a code object.
1589
1599
1590 When an exception occurs, self.showtraceback() is called to display a
1600 When an exception occurs, self.showtraceback() is called to display a
1591 traceback.
1601 traceback.
1592
1602
1593 Return value: a flag indicating whether the code to be run completed
1603 Return value: a flag indicating whether the code to be run completed
1594 successfully:
1604 successfully:
1595
1605
1596 - 0: successful execution.
1606 - 0: successful execution.
1597 - 1: an error occurred.
1607 - 1: an error occurred.
1598 """
1608 """
1599
1609
1600 # Set our own excepthook in case the user code tries to call it
1610 # Set our own excepthook in case the user code tries to call it
1601 # directly, so that the IPython crash handler doesn't get triggered
1611 # directly, so that the IPython crash handler doesn't get triggered
1602 old_excepthook,sys.excepthook = sys.excepthook, self.excepthook
1612 old_excepthook,sys.excepthook = sys.excepthook, self.excepthook
1603
1613
1604 # we save the original sys.excepthook in the instance, in case config
1614 # we save the original sys.excepthook in the instance, in case config
1605 # code (such as magics) needs access to it.
1615 # code (such as magics) needs access to it.
1606 self.sys_excepthook = old_excepthook
1616 self.sys_excepthook = old_excepthook
1607 outflag = 1 # happens in more places, so it's easier as default
1617 outflag = 1 # happens in more places, so it's easier as default
1608 try:
1618 try:
1609 try:
1619 try:
1610 # Embedded instances require separate global/local namespaces
1620 # Embedded instances require separate global/local namespaces
1611 # so they can see both the surrounding (local) namespace and
1621 # so they can see both the surrounding (local) namespace and
1612 # the module-level globals when called inside another function.
1622 # the module-level globals when called inside another function.
1613 if self.embedded:
1623 if self.embedded:
1614 exec code_obj in self.user_global_ns, self.user_ns
1624 exec code_obj in self.user_global_ns, self.user_ns
1615 # Normal (non-embedded) instances should only have a single
1625 # Normal (non-embedded) instances should only have a single
1616 # namespace for user code execution, otherwise functions won't
1626 # namespace for user code execution, otherwise functions won't
1617 # see interactive top-level globals.
1627 # see interactive top-level globals.
1618 else:
1628 else:
1619 exec code_obj in self.user_ns
1629 exec code_obj in self.user_ns
1620 finally:
1630 finally:
1621 # Reset our crash handler in place
1631 # Reset our crash handler in place
1622 sys.excepthook = old_excepthook
1632 sys.excepthook = old_excepthook
1623 except SystemExit:
1633 except SystemExit:
1624 self.resetbuffer()
1634 self.resetbuffer()
1625 self.showtraceback()
1635 self.showtraceback()
1626 warn("Type exit or quit to exit IPython "
1636 warn("Type exit or quit to exit IPython "
1627 "(%Exit or %Quit do so unconditionally).",level=1)
1637 "(%Exit or %Quit do so unconditionally).",level=1)
1628 except self.custom_exceptions:
1638 except self.custom_exceptions:
1629 etype,value,tb = sys.exc_info()
1639 etype,value,tb = sys.exc_info()
1630 self.CustomTB(etype,value,tb)
1640 self.CustomTB(etype,value,tb)
1631 except:
1641 except:
1632 self.showtraceback()
1642 self.showtraceback()
1633 else:
1643 else:
1634 outflag = 0
1644 outflag = 0
1635 if softspace(sys.stdout, 0):
1645 if softspace(sys.stdout, 0):
1636 print
1646 print
1637 # Flush out code object which has been run (and source)
1647 # Flush out code object which has been run (and source)
1638 self.code_to_run = None
1648 self.code_to_run = None
1639 return outflag
1649 return outflag
1640
1650
1641 def push(self, line):
1651 def push(self, line):
1642 """Push a line to the interpreter.
1652 """Push a line to the interpreter.
1643
1653
1644 The line should not have a trailing newline; it may have
1654 The line should not have a trailing newline; it may have
1645 internal newlines. The line is appended to a buffer and the
1655 internal newlines. The line is appended to a buffer and the
1646 interpreter's runsource() method is called with the
1656 interpreter's runsource() method is called with the
1647 concatenated contents of the buffer as source. If this
1657 concatenated contents of the buffer as source. If this
1648 indicates that the command was executed or invalid, the buffer
1658 indicates that the command was executed or invalid, the buffer
1649 is reset; otherwise, the command is incomplete, and the buffer
1659 is reset; otherwise, the command is incomplete, and the buffer
1650 is left as it was after the line was appended. The return
1660 is left as it was after the line was appended. The return
1651 value is 1 if more input is required, 0 if the line was dealt
1661 value is 1 if more input is required, 0 if the line was dealt
1652 with in some way (this is the same as runsource()).
1662 with in some way (this is the same as runsource()).
1653 """
1663 """
1654
1664
1655 # autoindent management should be done here, and not in the
1665 # autoindent management should be done here, and not in the
1656 # interactive loop, since that one is only seen by keyboard input. We
1666 # interactive loop, since that one is only seen by keyboard input. We
1657 # need this done correctly even for code run via runlines (which uses
1667 # need this done correctly even for code run via runlines (which uses
1658 # push).
1668 # push).
1659
1669
1660 #print 'push line: <%s>' % line # dbg
1670 #print 'push line: <%s>' % line # dbg
1661 self.autoindent_update(line)
1671 self.autoindent_update(line)
1662
1672
1663 self.buffer.append(line)
1673 self.buffer.append(line)
1664 more = self.runsource('\n'.join(self.buffer), self.filename)
1674 more = self.runsource('\n'.join(self.buffer), self.filename)
1665 if not more:
1675 if not more:
1666 self.resetbuffer()
1676 self.resetbuffer()
1667 return more
1677 return more
1668
1678
1669 def resetbuffer(self):
1679 def resetbuffer(self):
1670 """Reset the input buffer."""
1680 """Reset the input buffer."""
1671 self.buffer[:] = []
1681 self.buffer[:] = []
1672
1682
1673 def raw_input(self,prompt='',continue_prompt=False):
1683 def raw_input(self,prompt='',continue_prompt=False):
1674 """Write a prompt and read a line.
1684 """Write a prompt and read a line.
1675
1685
1676 The returned line does not include the trailing newline.
1686 The returned line does not include the trailing newline.
1677 When the user enters the EOF key sequence, EOFError is raised.
1687 When the user enters the EOF key sequence, EOFError is raised.
1678
1688
1679 Optional inputs:
1689 Optional inputs:
1680
1690
1681 - prompt(''): a string to be printed to prompt the user.
1691 - prompt(''): a string to be printed to prompt the user.
1682
1692
1683 - continue_prompt(False): whether this line is the first one or a
1693 - continue_prompt(False): whether this line is the first one or a
1684 continuation in a sequence of inputs.
1694 continuation in a sequence of inputs.
1685 """
1695 """
1686
1696
1687 line = raw_input_original(prompt)
1697 line = raw_input_original(prompt)
1688 # Try to be reasonably smart about not re-indenting pasted input more
1698 # Try to be reasonably smart about not re-indenting pasted input more
1689 # than necessary. We do this by trimming out the auto-indent initial
1699 # than necessary. We do this by trimming out the auto-indent initial
1690 # spaces, if the user's actual input started itself with whitespace.
1700 # spaces, if the user's actual input started itself with whitespace.
1691 if self.autoindent:
1701 if self.autoindent:
1692 line2 = line[self.indent_current_nsp:]
1702 line2 = line[self.indent_current_nsp:]
1693 if line2[0:1] in (' ','\t'):
1703 if line2[0:1] in (' ','\t'):
1694 line = line2
1704 line = line2
1695 return self.prefilter(line,continue_prompt)
1705 return self.prefilter(line,continue_prompt)
1696
1706
1697 def split_user_input(self,line):
1707 def split_user_input(self,line):
1698 """Split user input into pre-char, function part and rest."""
1708 """Split user input into pre-char, function part and rest."""
1699
1709
1700 lsplit = self.line_split.match(line)
1710 lsplit = self.line_split.match(line)
1701 if lsplit is None: # no regexp match returns None
1711 if lsplit is None: # no regexp match returns None
1702 try:
1712 try:
1703 iFun,theRest = line.split(None,1)
1713 iFun,theRest = line.split(None,1)
1704 except ValueError:
1714 except ValueError:
1705 iFun,theRest = line,''
1715 iFun,theRest = line,''
1706 pre = re.match('^(\s*)(.*)',line).groups()[0]
1716 pre = re.match('^(\s*)(.*)',line).groups()[0]
1707 else:
1717 else:
1708 pre,iFun,theRest = lsplit.groups()
1718 pre,iFun,theRest = lsplit.groups()
1709
1719
1710 #print 'line:<%s>' % line # dbg
1720 #print 'line:<%s>' % line # dbg
1711 #print 'pre <%s> iFun <%s> rest <%s>' % (pre,iFun.strip(),theRest) # dbg
1721 #print 'pre <%s> iFun <%s> rest <%s>' % (pre,iFun.strip(),theRest) # dbg
1712 return pre,iFun.strip(),theRest
1722 return pre,iFun.strip(),theRest
1713
1723
1714 def _prefilter(self, line, continue_prompt):
1724 def _prefilter(self, line, continue_prompt):
1715 """Calls different preprocessors, depending on the form of line."""
1725 """Calls different preprocessors, depending on the form of line."""
1716
1726
1717 # All handlers *must* return a value, even if it's blank ('').
1727 # All handlers *must* return a value, even if it's blank ('').
1718
1728
1719 # Lines are NOT logged here. Handlers should process the line as
1729 # Lines are NOT logged here. Handlers should process the line as
1720 # needed, update the cache AND log it (so that the input cache array
1730 # needed, update the cache AND log it (so that the input cache array
1721 # stays synced).
1731 # stays synced).
1722
1732
1723 # This function is _very_ delicate, and since it's also the one which
1733 # This function is _very_ delicate, and since it's also the one which
1724 # determines IPython's response to user input, it must be as efficient
1734 # determines IPython's response to user input, it must be as efficient
1725 # as possible. For this reason it has _many_ returns in it, trying
1735 # as possible. For this reason it has _many_ returns in it, trying
1726 # always to exit as quickly as it can figure out what it needs to do.
1736 # always to exit as quickly as it can figure out what it needs to do.
1727
1737
1728 # This function is the main responsible for maintaining IPython's
1738 # This function is the main responsible for maintaining IPython's
1729 # behavior respectful of Python's semantics. So be _very_ careful if
1739 # behavior respectful of Python's semantics. So be _very_ careful if
1730 # making changes to anything here.
1740 # making changes to anything here.
1731
1741
1732 #.....................................................................
1742 #.....................................................................
1733 # Code begins
1743 # Code begins
1734
1744
1735 #if line.startswith('%crash'): raise RuntimeError,'Crash now!' # dbg
1745 #if line.startswith('%crash'): raise RuntimeError,'Crash now!' # dbg
1736
1746
1737 # save the line away in case we crash, so the post-mortem handler can
1747 # save the line away in case we crash, so the post-mortem handler can
1738 # record it
1748 # record it
1739 self._last_input_line = line
1749 self._last_input_line = line
1740
1750
1741 #print '***line: <%s>' % line # dbg
1751 #print '***line: <%s>' % line # dbg
1742
1752
1743 # the input history needs to track even empty lines
1753 # the input history needs to track even empty lines
1744 if not line.strip():
1754 if not line.strip():
1745 if not continue_prompt:
1755 if not continue_prompt:
1746 self.outputcache.prompt_count -= 1
1756 self.outputcache.prompt_count -= 1
1747 return self.handle_normal(line,continue_prompt)
1757 return self.handle_normal(line,continue_prompt)
1748 #return self.handle_normal('',continue_prompt)
1758 #return self.handle_normal('',continue_prompt)
1749
1759
1750 # print '***cont',continue_prompt # dbg
1760 # print '***cont',continue_prompt # dbg
1751 # special handlers are only allowed for single line statements
1761 # special handlers are only allowed for single line statements
1752 if continue_prompt and not self.rc.multi_line_specials:
1762 if continue_prompt and not self.rc.multi_line_specials:
1753 return self.handle_normal(line,continue_prompt)
1763 return self.handle_normal(line,continue_prompt)
1754
1764
1755 # For the rest, we need the structure of the input
1765 # For the rest, we need the structure of the input
1756 pre,iFun,theRest = self.split_user_input(line)
1766 pre,iFun,theRest = self.split_user_input(line)
1757 #print 'pre <%s> iFun <%s> rest <%s>' % (pre,iFun,theRest) # dbg
1767 #print 'pre <%s> iFun <%s> rest <%s>' % (pre,iFun,theRest) # dbg
1758
1768
1759 # First check for explicit escapes in the last/first character
1769 # First check for explicit escapes in the last/first character
1760 handler = None
1770 handler = None
1761 if line[-1] == self.ESC_HELP:
1771 if line[-1] == self.ESC_HELP:
1762 handler = self.esc_handlers.get(line[-1]) # the ? can be at the end
1772 handler = self.esc_handlers.get(line[-1]) # the ? can be at the end
1763 if handler is None:
1773 if handler is None:
1764 # look at the first character of iFun, NOT of line, so we skip
1774 # look at the first character of iFun, NOT of line, so we skip
1765 # leading whitespace in multiline input
1775 # leading whitespace in multiline input
1766 handler = self.esc_handlers.get(iFun[0:1])
1776 handler = self.esc_handlers.get(iFun[0:1])
1767 if handler is not None:
1777 if handler is not None:
1768 return handler(line,continue_prompt,pre,iFun,theRest)
1778 return handler(line,continue_prompt,pre,iFun,theRest)
1769 # Emacs ipython-mode tags certain input lines
1779 # Emacs ipython-mode tags certain input lines
1770 if line.endswith('# PYTHON-MODE'):
1780 if line.endswith('# PYTHON-MODE'):
1771 return self.handle_emacs(line,continue_prompt)
1781 return self.handle_emacs(line,continue_prompt)
1772
1782
1773 # Next, check if we can automatically execute this thing
1783 # Next, check if we can automatically execute this thing
1774
1784
1775 # Allow ! in multi-line statements if multi_line_specials is on:
1785 # Allow ! in multi-line statements if multi_line_specials is on:
1776 if continue_prompt and self.rc.multi_line_specials and \
1786 if continue_prompt and self.rc.multi_line_specials and \
1777 iFun.startswith(self.ESC_SHELL):
1787 iFun.startswith(self.ESC_SHELL):
1778 return self.handle_shell_escape(line,continue_prompt,
1788 return self.handle_shell_escape(line,continue_prompt,
1779 pre=pre,iFun=iFun,
1789 pre=pre,iFun=iFun,
1780 theRest=theRest)
1790 theRest=theRest)
1781
1791
1782 # Let's try to find if the input line is a magic fn
1792 # Let's try to find if the input line is a magic fn
1783 oinfo = None
1793 oinfo = None
1784 if hasattr(self,'magic_'+iFun):
1794 if hasattr(self,'magic_'+iFun):
1785 # WARNING: _ofind uses getattr(), so it can consume generators and
1795 # WARNING: _ofind uses getattr(), so it can consume generators and
1786 # cause other side effects.
1796 # cause other side effects.
1787 oinfo = self._ofind(iFun) # FIXME - _ofind is part of Magic
1797 oinfo = self._ofind(iFun) # FIXME - _ofind is part of Magic
1788 if oinfo['ismagic']:
1798 if oinfo['ismagic']:
1789 # Be careful not to call magics when a variable assignment is
1799 # Be careful not to call magics when a variable assignment is
1790 # being made (ls='hi', for example)
1800 # being made (ls='hi', for example)
1791 if self.rc.automagic and \
1801 if self.rc.automagic and \
1792 (len(theRest)==0 or theRest[0] not in '!=()<>,') and \
1802 (len(theRest)==0 or theRest[0] not in '!=()<>,') and \
1793 (self.rc.multi_line_specials or not continue_prompt):
1803 (self.rc.multi_line_specials or not continue_prompt):
1794 return self.handle_magic(line,continue_prompt,
1804 return self.handle_magic(line,continue_prompt,
1795 pre,iFun,theRest)
1805 pre,iFun,theRest)
1796 else:
1806 else:
1797 return self.handle_normal(line,continue_prompt)
1807 return self.handle_normal(line,continue_prompt)
1798
1808
1799 # If the rest of the line begins with an (in)equality, assginment or
1809 # If the rest of the line begins with an (in)equality, assginment or
1800 # function call, we should not call _ofind but simply execute it.
1810 # function call, we should not call _ofind but simply execute it.
1801 # This avoids spurious geattr() accesses on objects upon assignment.
1811 # This avoids spurious geattr() accesses on objects upon assignment.
1802 #
1812 #
1803 # It also allows users to assign to either alias or magic names true
1813 # It also allows users to assign to either alias or magic names true
1804 # python variables (the magic/alias systems always take second seat to
1814 # python variables (the magic/alias systems always take second seat to
1805 # true python code).
1815 # true python code).
1806 if theRest and theRest[0] in '!=()':
1816 if theRest and theRest[0] in '!=()':
1807 return self.handle_normal(line,continue_prompt)
1817 return self.handle_normal(line,continue_prompt)
1808
1818
1809 if oinfo is None:
1819 if oinfo is None:
1810 # let's try to ensure that _oinfo is ONLY called when autocall is
1820 # let's try to ensure that _oinfo is ONLY called when autocall is
1811 # on. Since it has inevitable potential side effects, at least
1821 # on. Since it has inevitable potential side effects, at least
1812 # having autocall off should be a guarantee to the user that no
1822 # having autocall off should be a guarantee to the user that no
1813 # weird things will happen.
1823 # weird things will happen.
1814
1824
1815 if self.rc.autocall:
1825 if self.rc.autocall:
1816 oinfo = self._ofind(iFun) # FIXME - _ofind is part of Magic
1826 oinfo = self._ofind(iFun) # FIXME - _ofind is part of Magic
1817 else:
1827 else:
1818 # in this case, all that's left is either an alias or
1828 # in this case, all that's left is either an alias or
1819 # processing the line normally.
1829 # processing the line normally.
1820 if iFun in self.alias_table:
1830 if iFun in self.alias_table:
1821 return self.handle_alias(line,continue_prompt,
1831 return self.handle_alias(line,continue_prompt,
1822 pre,iFun,theRest)
1832 pre,iFun,theRest)
1823 else:
1833 else:
1824 return self.handle_normal(line,continue_prompt)
1834 return self.handle_normal(line,continue_prompt)
1825
1835
1826 if not oinfo['found']:
1836 if not oinfo['found']:
1827 return self.handle_normal(line,continue_prompt)
1837 return self.handle_normal(line,continue_prompt)
1828 else:
1838 else:
1829 #print 'iFun <%s> rest <%s>' % (iFun,theRest) # dbg
1839 #print 'iFun <%s> rest <%s>' % (iFun,theRest) # dbg
1830 if oinfo['isalias']:
1840 if oinfo['isalias']:
1831 return self.handle_alias(line,continue_prompt,
1841 return self.handle_alias(line,continue_prompt,
1832 pre,iFun,theRest)
1842 pre,iFun,theRest)
1833
1843
1834 if self.rc.autocall and \
1844 if self.rc.autocall and \
1835 not self.re_exclude_auto.match(theRest) and \
1845 not self.re_exclude_auto.match(theRest) and \
1836 self.re_fun_name.match(iFun) and \
1846 self.re_fun_name.match(iFun) and \
1837 callable(oinfo['obj']) :
1847 callable(oinfo['obj']) :
1838 #print 'going auto' # dbg
1848 #print 'going auto' # dbg
1839 return self.handle_auto(line,continue_prompt,
1849 return self.handle_auto(line,continue_prompt,
1840 pre,iFun,theRest,oinfo['obj'])
1850 pre,iFun,theRest,oinfo['obj'])
1841 else:
1851 else:
1842 #print 'was callable?', callable(oinfo['obj']) # dbg
1852 #print 'was callable?', callable(oinfo['obj']) # dbg
1843 return self.handle_normal(line,continue_prompt)
1853 return self.handle_normal(line,continue_prompt)
1844
1854
1845 # If we get here, we have a normal Python line. Log and return.
1855 # If we get here, we have a normal Python line. Log and return.
1846 return self.handle_normal(line,continue_prompt)
1856 return self.handle_normal(line,continue_prompt)
1847
1857
1848 def _prefilter_dumb(self, line, continue_prompt):
1858 def _prefilter_dumb(self, line, continue_prompt):
1849 """simple prefilter function, for debugging"""
1859 """simple prefilter function, for debugging"""
1850 return self.handle_normal(line,continue_prompt)
1860 return self.handle_normal(line,continue_prompt)
1851
1861
1852 # Set the default prefilter() function (this can be user-overridden)
1862 # Set the default prefilter() function (this can be user-overridden)
1853 prefilter = _prefilter
1863 prefilter = _prefilter
1854
1864
1855 def handle_normal(self,line,continue_prompt=None,
1865 def handle_normal(self,line,continue_prompt=None,
1856 pre=None,iFun=None,theRest=None):
1866 pre=None,iFun=None,theRest=None):
1857 """Handle normal input lines. Use as a template for handlers."""
1867 """Handle normal input lines. Use as a template for handlers."""
1858
1868
1859 # With autoindent on, we need some way to exit the input loop, and I
1869 # With autoindent on, we need some way to exit the input loop, and I
1860 # don't want to force the user to have to backspace all the way to
1870 # don't want to force the user to have to backspace all the way to
1861 # clear the line. The rule will be in this case, that either two
1871 # clear the line. The rule will be in this case, that either two
1862 # lines of pure whitespace in a row, or a line of pure whitespace but
1872 # lines of pure whitespace in a row, or a line of pure whitespace but
1863 # of a size different to the indent level, will exit the input loop.
1873 # of a size different to the indent level, will exit the input loop.
1864
1874
1865 if (continue_prompt and self.autoindent and isspace(line) and
1875 if (continue_prompt and self.autoindent and isspace(line) and
1866 (line != self.indent_current or isspace(self.buffer[-1]))):
1876 (line != self.indent_current or isspace(self.buffer[-1]))):
1867 line = ''
1877 line = ''
1868
1878
1869 self.log(line,continue_prompt)
1879 self.log(line,continue_prompt)
1870 return line
1880 return line
1871
1881
1872 def handle_alias(self,line,continue_prompt=None,
1882 def handle_alias(self,line,continue_prompt=None,
1873 pre=None,iFun=None,theRest=None):
1883 pre=None,iFun=None,theRest=None):
1874 """Handle alias input lines. """
1884 """Handle alias input lines. """
1875
1885
1876 # pre is needed, because it carries the leading whitespace. Otherwise
1886 # pre is needed, because it carries the leading whitespace. Otherwise
1877 # aliases won't work in indented sections.
1887 # aliases won't work in indented sections.
1878 line_out = '%sipalias("%s %s")' % (pre,iFun,esc_quotes(theRest))
1888 line_out = '%sipalias("%s %s")' % (pre,iFun,esc_quotes(theRest))
1879 self.log(line_out,continue_prompt)
1889 self.log(line_out,continue_prompt)
1880 return line_out
1890 return line_out
1881
1891
1882 def handle_shell_escape(self, line, continue_prompt=None,
1892 def handle_shell_escape(self, line, continue_prompt=None,
1883 pre=None,iFun=None,theRest=None):
1893 pre=None,iFun=None,theRest=None):
1884 """Execute the line in a shell, empty return value"""
1894 """Execute the line in a shell, empty return value"""
1885
1895
1886 #print 'line in :', `line` # dbg
1896 #print 'line in :', `line` # dbg
1887 # Example of a special handler. Others follow a similar pattern.
1897 # Example of a special handler. Others follow a similar pattern.
1888 if continue_prompt: # multi-line statements
1898 if continue_prompt: # multi-line statements
1889 if iFun.startswith('!!'):
1899 if iFun.startswith('!!'):
1890 print 'SyntaxError: !! is not allowed in multiline statements'
1900 print 'SyntaxError: !! is not allowed in multiline statements'
1891 return pre
1901 return pre
1892 else:
1902 else:
1893 cmd = ("%s %s" % (iFun[1:],theRest))
1903 cmd = ("%s %s" % (iFun[1:],theRest))
1894 line_out = '%sipsystem(r"""%s"""[:-1])' % (pre,cmd + "_")
1904 line_out = '%sipsystem(r"""%s"""[:-1])' % (pre,cmd + "_")
1895 else: # single-line input
1905 else: # single-line input
1896 if line.startswith('!!'):
1906 if line.startswith('!!'):
1897 # rewrite iFun/theRest to properly hold the call to %sx and
1907 # rewrite iFun/theRest to properly hold the call to %sx and
1898 # the actual command to be executed, so handle_magic can work
1908 # the actual command to be executed, so handle_magic can work
1899 # correctly
1909 # correctly
1900 theRest = '%s %s' % (iFun[2:],theRest)
1910 theRest = '%s %s' % (iFun[2:],theRest)
1901 iFun = 'sx'
1911 iFun = 'sx'
1902 return self.handle_magic('%ssx %s' % (self.ESC_MAGIC,line[2:]),
1912 return self.handle_magic('%ssx %s' % (self.ESC_MAGIC,line[2:]),
1903 continue_prompt,pre,iFun,theRest)
1913 continue_prompt,pre,iFun,theRest)
1904 else:
1914 else:
1905 cmd=line[1:]
1915 cmd=line[1:]
1906 line_out = '%sipsystem(r"""%s"""[:-1])' % (pre,cmd +"_")
1916 line_out = '%sipsystem(r"""%s"""[:-1])' % (pre,cmd +"_")
1907 # update cache/log and return
1917 # update cache/log and return
1908 self.log(line_out,continue_prompt)
1918 self.log(line_out,continue_prompt)
1909 return line_out
1919 return line_out
1910
1920
1911 def handle_magic(self, line, continue_prompt=None,
1921 def handle_magic(self, line, continue_prompt=None,
1912 pre=None,iFun=None,theRest=None):
1922 pre=None,iFun=None,theRest=None):
1913 """Execute magic functions.
1923 """Execute magic functions.
1914
1924
1915 Also log them with a prepended # so the log is clean Python."""
1925 Also log them with a prepended # so the log is clean Python."""
1916
1926
1917 cmd = '%sipmagic("%s")' % (pre,esc_quotes('%s %s' % (iFun,theRest)))
1927 cmd = '%sipmagic("%s")' % (pre,esc_quotes('%s %s' % (iFun,theRest)))
1918 self.log(cmd,continue_prompt)
1928 self.log(cmd,continue_prompt)
1919 #print 'in handle_magic, cmd=<%s>' % cmd # dbg
1929 #print 'in handle_magic, cmd=<%s>' % cmd # dbg
1920 return cmd
1930 return cmd
1921
1931
1922 def handle_auto(self, line, continue_prompt=None,
1932 def handle_auto(self, line, continue_prompt=None,
1923 pre=None,iFun=None,theRest=None,obj=None):
1933 pre=None,iFun=None,theRest=None,obj=None):
1924 """Hande lines which can be auto-executed, quoting if requested."""
1934 """Hande lines which can be auto-executed, quoting if requested."""
1925
1935
1926 #print 'pre <%s> iFun <%s> rest <%s>' % (pre,iFun,theRest) # dbg
1936 #print 'pre <%s> iFun <%s> rest <%s>' % (pre,iFun,theRest) # dbg
1927
1937
1928 # This should only be active for single-line input!
1938 # This should only be active for single-line input!
1929 if continue_prompt:
1939 if continue_prompt:
1930 self.log(line,continue_prompt)
1940 self.log(line,continue_prompt)
1931 return line
1941 return line
1932
1942
1933 auto_rewrite = True
1943 auto_rewrite = True
1934 if pre == self.ESC_QUOTE:
1944 if pre == self.ESC_QUOTE:
1935 # Auto-quote splitting on whitespace
1945 # Auto-quote splitting on whitespace
1936 newcmd = '%s("%s")' % (iFun,'", "'.join(theRest.split()) )
1946 newcmd = '%s("%s")' % (iFun,'", "'.join(theRest.split()) )
1937 elif pre == self.ESC_QUOTE2:
1947 elif pre == self.ESC_QUOTE2:
1938 # Auto-quote whole string
1948 # Auto-quote whole string
1939 newcmd = '%s("%s")' % (iFun,theRest)
1949 newcmd = '%s("%s")' % (iFun,theRest)
1940 else:
1950 else:
1941 # Auto-paren.
1951 # Auto-paren.
1942 # We only apply it to argument-less calls if the autocall
1952 # We only apply it to argument-less calls if the autocall
1943 # parameter is set to 2. We only need to check that autocall is <
1953 # parameter is set to 2. We only need to check that autocall is <
1944 # 2, since this function isn't called unless it's at least 1.
1954 # 2, since this function isn't called unless it's at least 1.
1945 if not theRest and (self.rc.autocall < 2):
1955 if not theRest and (self.rc.autocall < 2):
1946 newcmd = '%s %s' % (iFun,theRest)
1956 newcmd = '%s %s' % (iFun,theRest)
1947 auto_rewrite = False
1957 auto_rewrite = False
1948 else:
1958 else:
1949 if theRest.startswith('['):
1959 if theRest.startswith('['):
1950 if hasattr(obj,'__getitem__'):
1960 if hasattr(obj,'__getitem__'):
1951 # Don't autocall in this case: item access for an object
1961 # Don't autocall in this case: item access for an object
1952 # which is BOTH callable and implements __getitem__.
1962 # which is BOTH callable and implements __getitem__.
1953 newcmd = '%s %s' % (iFun,theRest)
1963 newcmd = '%s %s' % (iFun,theRest)
1954 auto_rewrite = False
1964 auto_rewrite = False
1955 else:
1965 else:
1956 # if the object doesn't support [] access, go ahead and
1966 # if the object doesn't support [] access, go ahead and
1957 # autocall
1967 # autocall
1958 newcmd = '%s(%s)' % (iFun.rstrip(),theRest)
1968 newcmd = '%s(%s)' % (iFun.rstrip(),theRest)
1959 elif theRest.endswith(';'):
1969 elif theRest.endswith(';'):
1960 newcmd = '%s(%s);' % (iFun.rstrip(),theRest[:-1])
1970 newcmd = '%s(%s);' % (iFun.rstrip(),theRest[:-1])
1961 else:
1971 else:
1962 newcmd = '%s(%s)' % (iFun.rstrip(),theRest)
1972 newcmd = '%s(%s)' % (iFun.rstrip(),theRest)
1963
1973
1964 if auto_rewrite:
1974 if auto_rewrite:
1965 print >>Term.cout, self.outputcache.prompt1.auto_rewrite() + newcmd
1975 print >>Term.cout, self.outputcache.prompt1.auto_rewrite() + newcmd
1966 # log what is now valid Python, not the actual user input (without the
1976 # log what is now valid Python, not the actual user input (without the
1967 # final newline)
1977 # final newline)
1968 self.log(newcmd,continue_prompt)
1978 self.log(newcmd,continue_prompt)
1969 return newcmd
1979 return newcmd
1970
1980
1971 def handle_help(self, line, continue_prompt=None,
1981 def handle_help(self, line, continue_prompt=None,
1972 pre=None,iFun=None,theRest=None):
1982 pre=None,iFun=None,theRest=None):
1973 """Try to get some help for the object.
1983 """Try to get some help for the object.
1974
1984
1975 obj? or ?obj -> basic information.
1985 obj? or ?obj -> basic information.
1976 obj?? or ??obj -> more details.
1986 obj?? or ??obj -> more details.
1977 """
1987 """
1978
1988
1979 # We need to make sure that we don't process lines which would be
1989 # We need to make sure that we don't process lines which would be
1980 # otherwise valid python, such as "x=1 # what?"
1990 # otherwise valid python, such as "x=1 # what?"
1981 try:
1991 try:
1982 codeop.compile_command(line)
1992 codeop.compile_command(line)
1983 except SyntaxError:
1993 except SyntaxError:
1984 # We should only handle as help stuff which is NOT valid syntax
1994 # We should only handle as help stuff which is NOT valid syntax
1985 if line[0]==self.ESC_HELP:
1995 if line[0]==self.ESC_HELP:
1986 line = line[1:]
1996 line = line[1:]
1987 elif line[-1]==self.ESC_HELP:
1997 elif line[-1]==self.ESC_HELP:
1988 line = line[:-1]
1998 line = line[:-1]
1989 self.log('#?'+line)
1999 self.log('#?'+line)
1990 if line:
2000 if line:
1991 self.magic_pinfo(line)
2001 self.magic_pinfo(line)
1992 else:
2002 else:
1993 page(self.usage,screen_lines=self.rc.screen_length)
2003 page(self.usage,screen_lines=self.rc.screen_length)
1994 return '' # Empty string is needed here!
2004 return '' # Empty string is needed here!
1995 except:
2005 except:
1996 # Pass any other exceptions through to the normal handler
2006 # Pass any other exceptions through to the normal handler
1997 return self.handle_normal(line,continue_prompt)
2007 return self.handle_normal(line,continue_prompt)
1998 else:
2008 else:
1999 # If the code compiles ok, we should handle it normally
2009 # If the code compiles ok, we should handle it normally
2000 return self.handle_normal(line,continue_prompt)
2010 return self.handle_normal(line,continue_prompt)
2001
2011
2002 def handle_emacs(self,line,continue_prompt=None,
2012 def handle_emacs(self,line,continue_prompt=None,
2003 pre=None,iFun=None,theRest=None):
2013 pre=None,iFun=None,theRest=None):
2004 """Handle input lines marked by python-mode."""
2014 """Handle input lines marked by python-mode."""
2005
2015
2006 # Currently, nothing is done. Later more functionality can be added
2016 # Currently, nothing is done. Later more functionality can be added
2007 # here if needed.
2017 # here if needed.
2008
2018
2009 # The input cache shouldn't be updated
2019 # The input cache shouldn't be updated
2010
2020
2011 return line
2021 return line
2012
2022
2013 def mktempfile(self,data=None):
2023 def mktempfile(self,data=None):
2014 """Make a new tempfile and return its filename.
2024 """Make a new tempfile and return its filename.
2015
2025
2016 This makes a call to tempfile.mktemp, but it registers the created
2026 This makes a call to tempfile.mktemp, but it registers the created
2017 filename internally so ipython cleans it up at exit time.
2027 filename internally so ipython cleans it up at exit time.
2018
2028
2019 Optional inputs:
2029 Optional inputs:
2020
2030
2021 - data(None): if data is given, it gets written out to the temp file
2031 - data(None): if data is given, it gets written out to the temp file
2022 immediately, and the file is closed again."""
2032 immediately, and the file is closed again."""
2023
2033
2024 filename = tempfile.mktemp('.py')
2034 filename = tempfile.mktemp('.py')
2025 self.tempfiles.append(filename)
2035 self.tempfiles.append(filename)
2026
2036
2027 if data:
2037 if data:
2028 tmp_file = open(filename,'w')
2038 tmp_file = open(filename,'w')
2029 tmp_file.write(data)
2039 tmp_file.write(data)
2030 tmp_file.close()
2040 tmp_file.close()
2031 return filename
2041 return filename
2032
2042
2033 def write(self,data):
2043 def write(self,data):
2034 """Write a string to the default output"""
2044 """Write a string to the default output"""
2035 Term.cout.write(data)
2045 Term.cout.write(data)
2036
2046
2037 def write_err(self,data):
2047 def write_err(self,data):
2038 """Write a string to the default error output"""
2048 """Write a string to the default error output"""
2039 Term.cerr.write(data)
2049 Term.cerr.write(data)
2040
2050
2041 def exit(self):
2051 def exit(self):
2042 """Handle interactive exit.
2052 """Handle interactive exit.
2043
2053
2044 This method sets the exit_now attribute."""
2054 This method sets the exit_now attribute."""
2045
2055
2046 if self.rc.confirm_exit:
2056 if self.rc.confirm_exit:
2047 if ask_yes_no('Do you really want to exit ([y]/n)?','y'):
2057 if ask_yes_no('Do you really want to exit ([y]/n)?','y'):
2048 self.exit_now = True
2058 self.exit_now = True
2049 else:
2059 else:
2050 self.exit_now = True
2060 self.exit_now = True
2051 return self.exit_now
2061 return self.exit_now
2052
2062
2053 def safe_execfile(self,fname,*where,**kw):
2063 def safe_execfile(self,fname,*where,**kw):
2054 fname = os.path.expanduser(fname)
2064 fname = os.path.expanduser(fname)
2055
2065
2056 # find things also in current directory
2066 # find things also in current directory
2057 dname = os.path.dirname(fname)
2067 dname = os.path.dirname(fname)
2058 if not sys.path.count(dname):
2068 if not sys.path.count(dname):
2059 sys.path.append(dname)
2069 sys.path.append(dname)
2060
2070
2061 try:
2071 try:
2062 xfile = open(fname)
2072 xfile = open(fname)
2063 except:
2073 except:
2064 print >> Term.cerr, \
2074 print >> Term.cerr, \
2065 'Could not open file <%s> for safe execution.' % fname
2075 'Could not open file <%s> for safe execution.' % fname
2066 return None
2076 return None
2067
2077
2068 kw.setdefault('islog',0)
2078 kw.setdefault('islog',0)
2069 kw.setdefault('quiet',1)
2079 kw.setdefault('quiet',1)
2070 kw.setdefault('exit_ignore',0)
2080 kw.setdefault('exit_ignore',0)
2071 first = xfile.readline()
2081 first = xfile.readline()
2072 loghead = str(self.loghead_tpl).split('\n',1)[0].strip()
2082 loghead = str(self.loghead_tpl).split('\n',1)[0].strip()
2073 xfile.close()
2083 xfile.close()
2074 # line by line execution
2084 # line by line execution
2075 if first.startswith(loghead) or kw['islog']:
2085 if first.startswith(loghead) or kw['islog']:
2076 print 'Loading log file <%s> one line at a time...' % fname
2086 print 'Loading log file <%s> one line at a time...' % fname
2077 if kw['quiet']:
2087 if kw['quiet']:
2078 stdout_save = sys.stdout
2088 stdout_save = sys.stdout
2079 sys.stdout = StringIO.StringIO()
2089 sys.stdout = StringIO.StringIO()
2080 try:
2090 try:
2081 globs,locs = where[0:2]
2091 globs,locs = where[0:2]
2082 except:
2092 except:
2083 try:
2093 try:
2084 globs = locs = where[0]
2094 globs = locs = where[0]
2085 except:
2095 except:
2086 globs = locs = globals()
2096 globs = locs = globals()
2087 badblocks = []
2097 badblocks = []
2088
2098
2089 # we also need to identify indented blocks of code when replaying
2099 # we also need to identify indented blocks of code when replaying
2090 # logs and put them together before passing them to an exec
2100 # logs and put them together before passing them to an exec
2091 # statement. This takes a bit of regexp and look-ahead work in the
2101 # statement. This takes a bit of regexp and look-ahead work in the
2092 # file. It's easiest if we swallow the whole thing in memory
2102 # file. It's easiest if we swallow the whole thing in memory
2093 # first, and manually walk through the lines list moving the
2103 # first, and manually walk through the lines list moving the
2094 # counter ourselves.
2104 # counter ourselves.
2095 indent_re = re.compile('\s+\S')
2105 indent_re = re.compile('\s+\S')
2096 xfile = open(fname)
2106 xfile = open(fname)
2097 filelines = xfile.readlines()
2107 filelines = xfile.readlines()
2098 xfile.close()
2108 xfile.close()
2099 nlines = len(filelines)
2109 nlines = len(filelines)
2100 lnum = 0
2110 lnum = 0
2101 while lnum < nlines:
2111 while lnum < nlines:
2102 line = filelines[lnum]
2112 line = filelines[lnum]
2103 lnum += 1
2113 lnum += 1
2104 # don't re-insert logger status info into cache
2114 # don't re-insert logger status info into cache
2105 if line.startswith('#log#'):
2115 if line.startswith('#log#'):
2106 continue
2116 continue
2107 else:
2117 else:
2108 # build a block of code (maybe a single line) for execution
2118 # build a block of code (maybe a single line) for execution
2109 block = line
2119 block = line
2110 try:
2120 try:
2111 next = filelines[lnum] # lnum has already incremented
2121 next = filelines[lnum] # lnum has already incremented
2112 except:
2122 except:
2113 next = None
2123 next = None
2114 while next and indent_re.match(next):
2124 while next and indent_re.match(next):
2115 block += next
2125 block += next
2116 lnum += 1
2126 lnum += 1
2117 try:
2127 try:
2118 next = filelines[lnum]
2128 next = filelines[lnum]
2119 except:
2129 except:
2120 next = None
2130 next = None
2121 # now execute the block of one or more lines
2131 # now execute the block of one or more lines
2122 try:
2132 try:
2123 exec block in globs,locs
2133 exec block in globs,locs
2124 except SystemExit:
2134 except SystemExit:
2125 pass
2135 pass
2126 except:
2136 except:
2127 badblocks.append(block.rstrip())
2137 badblocks.append(block.rstrip())
2128 if kw['quiet']: # restore stdout
2138 if kw['quiet']: # restore stdout
2129 sys.stdout.close()
2139 sys.stdout.close()
2130 sys.stdout = stdout_save
2140 sys.stdout = stdout_save
2131 print 'Finished replaying log file <%s>' % fname
2141 print 'Finished replaying log file <%s>' % fname
2132 if badblocks:
2142 if badblocks:
2133 print >> sys.stderr, ('\nThe following lines/blocks in file '
2143 print >> sys.stderr, ('\nThe following lines/blocks in file '
2134 '<%s> reported errors:' % fname)
2144 '<%s> reported errors:' % fname)
2135
2145
2136 for badline in badblocks:
2146 for badline in badblocks:
2137 print >> sys.stderr, badline
2147 print >> sys.stderr, badline
2138 else: # regular file execution
2148 else: # regular file execution
2139 try:
2149 try:
2140 execfile(fname,*where)
2150 execfile(fname,*where)
2141 except SyntaxError:
2151 except SyntaxError:
2142 etype,evalue = sys.exc_info()[:2]
2152 etype,evalue = sys.exc_info()[:2]
2143 self.SyntaxTB(etype,evalue,[])
2153 self.SyntaxTB(etype,evalue,[])
2144 warn('Failure executing file: <%s>' % fname)
2154 warn('Failure executing file: <%s>' % fname)
2145 except SystemExit,status:
2155 except SystemExit,status:
2146 if not kw['exit_ignore']:
2156 if not kw['exit_ignore']:
2147 self.InteractiveTB()
2157 self.InteractiveTB()
2148 warn('Failure executing file: <%s>' % fname)
2158 warn('Failure executing file: <%s>' % fname)
2149 except:
2159 except:
2150 self.InteractiveTB()
2160 self.InteractiveTB()
2151 warn('Failure executing file: <%s>' % fname)
2161 warn('Failure executing file: <%s>' % fname)
2152
2162
2153 #************************* end of file <iplib.py> *****************************
2163 #************************* end of file <iplib.py> *****************************
@@ -1,701 +1,701 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 better.
5 Requires Python 2.1 or better.
6
6
7 This file contains the main make_IPython() starter function.
7 This file contains the main make_IPython() starter function.
8
8
9 $Id: ipmaker.py 990 2006-01-04 06:59:02Z fperez $"""
9 $Id: ipmaker.py 994 2006-01-08 08:29:44Z fperez $"""
10
10
11 #*****************************************************************************
11 #*****************************************************************************
12 # Copyright (C) 2001-2004 Fernando Perez. <fperez@colorado.edu>
12 # Copyright (C) 2001-2006 Fernando Perez. <fperez@colorado.edu>
13 #
13 #
14 # Distributed under the terms of the BSD License. The full license is in
14 # Distributed under the terms of the BSD License. The full license is in
15 # the file COPYING, distributed as part of this software.
15 # the file COPYING, distributed as part of this software.
16 #*****************************************************************************
16 #*****************************************************************************
17
17
18 from IPython import Release
18 from IPython import Release
19 __author__ = '%s <%s>' % Release.authors['Fernando']
19 __author__ = '%s <%s>' % Release.authors['Fernando']
20 __license__ = Release.license
20 __license__ = Release.license
21 __version__ = Release.version
21 __version__ = Release.version
22
22
23 credits._Printer__data = """
23 credits._Printer__data = """
24 Python: %s
24 Python: %s
25
25
26 IPython: Fernando Perez, Janko Hauser, Nathan Gray, and many users.
26 IPython: Fernando Perez, Janko Hauser, Nathan Gray, and many users.
27 See http://ipython.scipy.org for more information.""" \
27 See http://ipython.scipy.org for more information.""" \
28 % credits._Printer__data
28 % credits._Printer__data
29
29
30 copyright._Printer__data += """
30 copyright._Printer__data += """
31
31
32 Copyright (c) 2001-2004 Fernando Perez, Janko Hauser, Nathan Gray.
32 Copyright (c) 2001-2004 Fernando Perez, Janko Hauser, Nathan Gray.
33 All Rights Reserved."""
33 All Rights Reserved."""
34
34
35 #****************************************************************************
35 #****************************************************************************
36 # Required modules
36 # Required modules
37
37
38 # From the standard library
38 # From the standard library
39 import __main__
39 import __main__
40 import __builtin__
40 import __builtin__
41 import os
41 import os
42 import re
42 import re
43 import sys
43 import sys
44 import types
44 import types
45 from pprint import pprint,pformat
45 from pprint import pprint,pformat
46
46
47 # Our own
47 # Our own
48 from IPython import DPyGetOpt
48 from IPython import DPyGetOpt
49 from IPython.Struct import Struct
49 from IPython.Struct import Struct
50 from IPython.OutputTrap import OutputTrap
50 from IPython.OutputTrap import OutputTrap
51 from IPython.ConfigLoader import ConfigLoader
51 from IPython.ConfigLoader import ConfigLoader
52 from IPython.iplib import InteractiveShell
52 from IPython.iplib import InteractiveShell
53 from IPython.usage import cmd_line_usage,interactive_usage
53 from IPython.usage import cmd_line_usage,interactive_usage
54 from IPython.genutils import *
54 from IPython.genutils import *
55
55
56 #-----------------------------------------------------------------------------
56 #-----------------------------------------------------------------------------
57 def make_IPython(argv=None,user_ns=None,user_global_ns=None,debug=1,
57 def make_IPython(argv=None,user_ns=None,user_global_ns=None,debug=1,
58 rc_override=None,shell_class=InteractiveShell,
58 rc_override=None,shell_class=InteractiveShell,
59 embedded=False,**kw):
59 embedded=False,**kw):
60 """This is a dump of IPython into a single function.
60 """This is a dump of IPython into a single function.
61
61
62 Later it will have to be broken up in a sensible manner.
62 Later it will have to be broken up in a sensible manner.
63
63
64 Arguments:
64 Arguments:
65
65
66 - argv: a list similar to sys.argv[1:]. It should NOT contain the desired
66 - argv: a list similar to sys.argv[1:]. It should NOT contain the desired
67 script name, b/c DPyGetOpt strips the first argument only for the real
67 script name, b/c DPyGetOpt strips the first argument only for the real
68 sys.argv.
68 sys.argv.
69
69
70 - user_ns: a dict to be used as the user's namespace."""
70 - user_ns: a dict to be used as the user's namespace."""
71
71
72 #----------------------------------------------------------------------
72 #----------------------------------------------------------------------
73 # Defaults and initialization
73 # Defaults and initialization
74
74
75 # For developer debugging, deactivates crash handler and uses pdb.
75 # For developer debugging, deactivates crash handler and uses pdb.
76 DEVDEBUG = False
76 DEVDEBUG = False
77
77
78 if argv is None:
78 if argv is None:
79 argv = sys.argv
79 argv = sys.argv
80
80
81 # __IP is the main global that lives throughout and represents the whole
81 # __IP is the main global that lives throughout and represents the whole
82 # application. If the user redefines it, all bets are off as to what
82 # application. If the user redefines it, all bets are off as to what
83 # happens.
83 # happens.
84
84
85 # __IP is the name of he global which the caller will have accessible as
85 # __IP is the name of he global which the caller will have accessible as
86 # __IP.name. We set its name via the first parameter passed to
86 # __IP.name. We set its name via the first parameter passed to
87 # InteractiveShell:
87 # InteractiveShell:
88
88
89 IP = shell_class('__IP',user_ns=user_ns,user_global_ns=user_global_ns,
89 IP = shell_class('__IP',user_ns=user_ns,user_global_ns=user_global_ns,
90 embedded=embedded,**kw)
90 embedded=embedded,**kw)
91
91
92 # Put 'help' in the user namespace
92 # Put 'help' in the user namespace
93 from site import _Helper
93 from site import _Helper
94 IP.user_ns['help'] = _Helper()
94 IP.user_ns['help'] = _Helper()
95
95
96
96
97 if DEVDEBUG:
97 if DEVDEBUG:
98 # For developer debugging only (global flag)
98 # For developer debugging only (global flag)
99 from IPython import ultraTB
99 from IPython import ultraTB
100 sys.excepthook = ultraTB.VerboseTB(call_pdb=1)
100 sys.excepthook = ultraTB.VerboseTB(call_pdb=1)
101
101
102 IP.BANNER_PARTS = ['Python %s\n'
102 IP.BANNER_PARTS = ['Python %s\n'
103 'Type "copyright", "credits" or "license" '
103 'Type "copyright", "credits" or "license" '
104 'for more information.\n'
104 'for more information.\n'
105 % (sys.version.split('\n')[0],),
105 % (sys.version.split('\n')[0],),
106 "IPython %s -- An enhanced Interactive Python."
106 "IPython %s -- An enhanced Interactive Python."
107 % (__version__,),
107 % (__version__,),
108 """? -> Introduction to IPython's features.
108 """? -> Introduction to IPython's features.
109 %magic -> Information about IPython's 'magic' % functions.
109 %magic -> Information about IPython's 'magic' % functions.
110 help -> Python's own help system.
110 help -> Python's own help system.
111 object? -> Details about 'object'. ?object also works, ?? prints more.
111 object? -> Details about 'object'. ?object also works, ?? prints more.
112 """ ]
112 """ ]
113
113
114 IP.usage = interactive_usage
114 IP.usage = interactive_usage
115
115
116 # Platform-dependent suffix and directory names. We use _ipython instead
116 # Platform-dependent suffix and directory names. We use _ipython instead
117 # of .ipython under win32 b/c there's software that breaks with .named
117 # of .ipython under win32 b/c there's software that breaks with .named
118 # directories on that platform.
118 # directories on that platform.
119 if os.name == 'posix':
119 if os.name == 'posix':
120 rc_suffix = ''
120 rc_suffix = ''
121 ipdir_def = '.ipython'
121 ipdir_def = '.ipython'
122 else:
122 else:
123 rc_suffix = '.ini'
123 rc_suffix = '.ini'
124 ipdir_def = '_ipython'
124 ipdir_def = '_ipython'
125
125
126 # default directory for configuration
126 # default directory for configuration
127 ipythondir = os.path.abspath(os.environ.get('IPYTHONDIR',
127 ipythondir = os.path.abspath(os.environ.get('IPYTHONDIR',
128 os.path.join(IP.home_dir,ipdir_def)))
128 os.path.join(IP.home_dir,ipdir_def)))
129
129
130 # we need the directory where IPython itself is installed
130 # we need the directory where IPython itself is installed
131 import IPython
131 import IPython
132 IPython_dir = os.path.dirname(IPython.__file__)
132 IPython_dir = os.path.dirname(IPython.__file__)
133 del IPython
133 del IPython
134
134
135 #-------------------------------------------------------------------------
135 #-------------------------------------------------------------------------
136 # Command line handling
136 # Command line handling
137
137
138 # Valid command line options (uses DPyGetOpt syntax, like Perl's
138 # Valid command line options (uses DPyGetOpt syntax, like Perl's
139 # GetOpt::Long)
139 # GetOpt::Long)
140
140
141 # Any key not listed here gets deleted even if in the file (like session
141 # Any key not listed here gets deleted even if in the file (like session
142 # or profile). That's deliberate, to maintain the rc namespace clean.
142 # or profile). That's deliberate, to maintain the rc namespace clean.
143
143
144 # Each set of options appears twice: under _conv only the names are
144 # Each set of options appears twice: under _conv only the names are
145 # listed, indicating which type they must be converted to when reading the
145 # listed, indicating which type they must be converted to when reading the
146 # ipythonrc file. And under DPyGetOpt they are listed with the regular
146 # ipythonrc file. And under DPyGetOpt they are listed with the regular
147 # DPyGetOpt syntax (=s,=i,:f,etc).
147 # DPyGetOpt syntax (=s,=i,:f,etc).
148
148
149 # Make sure there's a space before each end of line (they get auto-joined!)
149 # Make sure there's a space before each end of line (they get auto-joined!)
150 cmdline_opts = ('autocall=i autoindent! automagic! banner! cache_size|cs=i '
150 cmdline_opts = ('autocall=i autoindent! automagic! banner! cache_size|cs=i '
151 'c=s classic|cl color_info! colors=s confirm_exit! '
151 'c=s classic|cl color_info! colors=s confirm_exit! '
152 'debug! deep_reload! editor=s log|l messages! nosep pdb! '
152 'debug! deep_reload! editor=s log|l messages! nosep pdb! '
153 'pprint! prompt_in1|pi1=s prompt_in2|pi2=s prompt_out|po=s '
153 'pprint! prompt_in1|pi1=s prompt_in2|pi2=s prompt_out|po=s '
154 'quick screen_length|sl=i prompts_pad_left=i '
154 'quick screen_length|sl=i prompts_pad_left=i '
155 'logfile|lf=s logplay|lp=s profile|p=s '
155 'logfile|lf=s logplay|lp=s profile|p=s '
156 'readline! readline_merge_completions! '
156 'readline! readline_merge_completions! '
157 'readline_omit__names! '
157 'readline_omit__names! '
158 'rcfile=s separate_in|si=s separate_out|so=s '
158 'rcfile=s separate_in|si=s separate_out|so=s '
159 'separate_out2|so2=s xmode=s wildcards_case_sensitive! '
159 'separate_out2|so2=s xmode=s wildcards_case_sensitive! '
160 'magic_docstrings system_verbose! '
160 'magic_docstrings system_verbose! '
161 'multi_line_specials! '
161 'multi_line_specials! '
162 'autoedit_syntax!')
162 'autoedit_syntax!')
163
163
164 # Options that can *only* appear at the cmd line (not in rcfiles).
164 # Options that can *only* appear at the cmd line (not in rcfiles).
165
165
166 # The "ignore" option is a kludge so that Emacs buffers don't crash, since
166 # The "ignore" option is a kludge so that Emacs buffers don't crash, since
167 # the 'C-c !' command in emacs automatically appends a -i option at the end.
167 # the 'C-c !' command in emacs automatically appends a -i option at the end.
168 cmdline_only = ('help ignore|i ipythondir=s Version upgrade '
168 cmdline_only = ('help ignore|i ipythondir=s Version upgrade '
169 'gthread! qthread! wthread! pylab! tk!')
169 'gthread! qthread! wthread! pylab! tk!')
170
170
171 # Build the actual name list to be used by DPyGetOpt
171 # Build the actual name list to be used by DPyGetOpt
172 opts_names = qw(cmdline_opts) + qw(cmdline_only)
172 opts_names = qw(cmdline_opts) + qw(cmdline_only)
173
173
174 # Set sensible command line defaults.
174 # Set sensible command line defaults.
175 # This should have everything from cmdline_opts and cmdline_only
175 # This should have everything from cmdline_opts and cmdline_only
176 opts_def = Struct(autocall = 1,
176 opts_def = Struct(autocall = 1,
177 autoedit_syntax = 1,
177 autoedit_syntax = 1,
178 autoindent=0,
178 autoindent=0,
179 automagic = 1,
179 automagic = 1,
180 banner = 1,
180 banner = 1,
181 cache_size = 1000,
181 cache_size = 1000,
182 c = '',
182 c = '',
183 classic = 0,
183 classic = 0,
184 colors = 'NoColor',
184 colors = 'NoColor',
185 color_info = 0,
185 color_info = 0,
186 confirm_exit = 1,
186 confirm_exit = 1,
187 debug = 0,
187 debug = 0,
188 deep_reload = 0,
188 deep_reload = 0,
189 editor = '0',
189 editor = '0',
190 help = 0,
190 help = 0,
191 ignore = 0,
191 ignore = 0,
192 ipythondir = ipythondir,
192 ipythondir = ipythondir,
193 log = 0,
193 log = 0,
194 logfile = '',
194 logfile = '',
195 logplay = '',
195 logplay = '',
196 multi_line_specials = 1,
196 multi_line_specials = 1,
197 messages = 1,
197 messages = 1,
198 nosep = 0,
198 nosep = 0,
199 pdb = 0,
199 pdb = 0,
200 pprint = 0,
200 pprint = 0,
201 profile = '',
201 profile = '',
202 prompt_in1 = 'In [\\#]: ',
202 prompt_in1 = 'In [\\#]: ',
203 prompt_in2 = ' .\\D.: ',
203 prompt_in2 = ' .\\D.: ',
204 prompt_out = 'Out[\\#]: ',
204 prompt_out = 'Out[\\#]: ',
205 prompts_pad_left = 1,
205 prompts_pad_left = 1,
206 quick = 0,
206 quick = 0,
207 readline = 1,
207 readline = 1,
208 readline_merge_completions = 1,
208 readline_merge_completions = 1,
209 readline_omit__names = 0,
209 readline_omit__names = 0,
210 rcfile = 'ipythonrc' + rc_suffix,
210 rcfile = 'ipythonrc' + rc_suffix,
211 screen_length = 0,
211 screen_length = 0,
212 separate_in = '\n',
212 separate_in = '\n',
213 separate_out = '\n',
213 separate_out = '\n',
214 separate_out2 = '',
214 separate_out2 = '',
215 system_verbose = 0,
215 system_verbose = 0,
216 gthread = 0,
216 gthread = 0,
217 qthread = 0,
217 qthread = 0,
218 wthread = 0,
218 wthread = 0,
219 pylab = 0,
219 pylab = 0,
220 tk = 0,
220 tk = 0,
221 upgrade = 0,
221 upgrade = 0,
222 Version = 0,
222 Version = 0,
223 xmode = 'Verbose',
223 xmode = 'Verbose',
224 wildcards_case_sensitive = 1,
224 wildcards_case_sensitive = 1,
225 magic_docstrings = 0, # undocumented, for doc generation
225 magic_docstrings = 0, # undocumented, for doc generation
226 )
226 )
227
227
228 # Things that will *only* appear in rcfiles (not at the command line).
228 # Things that will *only* appear in rcfiles (not at the command line).
229 # Make sure there's a space before each end of line (they get auto-joined!)
229 # Make sure there's a space before each end of line (they get auto-joined!)
230 rcfile_opts = { qwflat: 'include import_mod import_all execfile ',
230 rcfile_opts = { qwflat: 'include import_mod import_all execfile ',
231 qw_lol: 'import_some ',
231 qw_lol: 'import_some ',
232 # for things with embedded whitespace:
232 # for things with embedded whitespace:
233 list_strings:'execute alias readline_parse_and_bind ',
233 list_strings:'execute alias readline_parse_and_bind ',
234 # Regular strings need no conversion:
234 # Regular strings need no conversion:
235 None:'readline_remove_delims ',
235 None:'readline_remove_delims ',
236 }
236 }
237 # Default values for these
237 # Default values for these
238 rc_def = Struct(include = [],
238 rc_def = Struct(include = [],
239 import_mod = [],
239 import_mod = [],
240 import_all = [],
240 import_all = [],
241 import_some = [[]],
241 import_some = [[]],
242 execute = [],
242 execute = [],
243 execfile = [],
243 execfile = [],
244 alias = [],
244 alias = [],
245 readline_parse_and_bind = [],
245 readline_parse_and_bind = [],
246 readline_remove_delims = '',
246 readline_remove_delims = '',
247 )
247 )
248
248
249 # Build the type conversion dictionary from the above tables:
249 # Build the type conversion dictionary from the above tables:
250 typeconv = rcfile_opts.copy()
250 typeconv = rcfile_opts.copy()
251 typeconv.update(optstr2types(cmdline_opts))
251 typeconv.update(optstr2types(cmdline_opts))
252
252
253 # FIXME: the None key appears in both, put that back together by hand. Ugly!
253 # FIXME: the None key appears in both, put that back together by hand. Ugly!
254 typeconv[None] += ' ' + rcfile_opts[None]
254 typeconv[None] += ' ' + rcfile_opts[None]
255
255
256 # Remove quotes at ends of all strings (used to protect spaces)
256 # Remove quotes at ends of all strings (used to protect spaces)
257 typeconv[unquote_ends] = typeconv[None]
257 typeconv[unquote_ends] = typeconv[None]
258 del typeconv[None]
258 del typeconv[None]
259
259
260 # Build the list we'll use to make all config decisions with defaults:
260 # Build the list we'll use to make all config decisions with defaults:
261 opts_all = opts_def.copy()
261 opts_all = opts_def.copy()
262 opts_all.update(rc_def)
262 opts_all.update(rc_def)
263
263
264 # Build conflict resolver for recursive loading of config files:
264 # Build conflict resolver for recursive loading of config files:
265 # - preserve means the outermost file maintains the value, it is not
265 # - preserve means the outermost file maintains the value, it is not
266 # overwritten if an included file has the same key.
266 # overwritten if an included file has the same key.
267 # - add_flip applies + to the two values, so it better make sense to add
267 # - add_flip applies + to the two values, so it better make sense to add
268 # those types of keys. But it flips them first so that things loaded
268 # those types of keys. But it flips them first so that things loaded
269 # deeper in the inclusion chain have lower precedence.
269 # deeper in the inclusion chain have lower precedence.
270 conflict = {'preserve': ' '.join([ typeconv[int],
270 conflict = {'preserve': ' '.join([ typeconv[int],
271 typeconv[unquote_ends] ]),
271 typeconv[unquote_ends] ]),
272 'add_flip': ' '.join([ typeconv[qwflat],
272 'add_flip': ' '.join([ typeconv[qwflat],
273 typeconv[qw_lol],
273 typeconv[qw_lol],
274 typeconv[list_strings] ])
274 typeconv[list_strings] ])
275 }
275 }
276
276
277 # Now actually process the command line
277 # Now actually process the command line
278 getopt = DPyGetOpt.DPyGetOpt()
278 getopt = DPyGetOpt.DPyGetOpt()
279 getopt.setIgnoreCase(0)
279 getopt.setIgnoreCase(0)
280
280
281 getopt.parseConfiguration(opts_names)
281 getopt.parseConfiguration(opts_names)
282
282
283 try:
283 try:
284 getopt.processArguments(argv)
284 getopt.processArguments(argv)
285 except:
285 except:
286 print cmd_line_usage
286 print cmd_line_usage
287 warn('\nError in Arguments: ' + `sys.exc_value`)
287 warn('\nError in Arguments: ' + `sys.exc_value`)
288 sys.exit(1)
288 sys.exit(1)
289
289
290 # convert the options dict to a struct for much lighter syntax later
290 # convert the options dict to a struct for much lighter syntax later
291 opts = Struct(getopt.optionValues)
291 opts = Struct(getopt.optionValues)
292 args = getopt.freeValues
292 args = getopt.freeValues
293
293
294 # this is the struct (which has default values at this point) with which
294 # this is the struct (which has default values at this point) with which
295 # we make all decisions:
295 # we make all decisions:
296 opts_all.update(opts)
296 opts_all.update(opts)
297
297
298 # Options that force an immediate exit
298 # Options that force an immediate exit
299 if opts_all.help:
299 if opts_all.help:
300 page(cmd_line_usage)
300 page(cmd_line_usage)
301 sys.exit()
301 sys.exit()
302
302
303 if opts_all.Version:
303 if opts_all.Version:
304 print __version__
304 print __version__
305 sys.exit()
305 sys.exit()
306
306
307 if opts_all.magic_docstrings:
307 if opts_all.magic_docstrings:
308 IP.magic_magic('-latex')
308 IP.magic_magic('-latex')
309 sys.exit()
309 sys.exit()
310
310
311 # Create user config directory if it doesn't exist. This must be done
311 # Create user config directory if it doesn't exist. This must be done
312 # *after* getting the cmd line options.
312 # *after* getting the cmd line options.
313 if not os.path.isdir(opts_all.ipythondir):
313 if not os.path.isdir(opts_all.ipythondir):
314 IP.user_setup(opts_all.ipythondir,rc_suffix,'install')
314 IP.user_setup(opts_all.ipythondir,rc_suffix,'install')
315
315
316 # upgrade user config files while preserving a copy of the originals
316 # upgrade user config files while preserving a copy of the originals
317 if opts_all.upgrade:
317 if opts_all.upgrade:
318 IP.user_setup(opts_all.ipythondir,rc_suffix,'upgrade')
318 IP.user_setup(opts_all.ipythondir,rc_suffix,'upgrade')
319
319
320 # check mutually exclusive options in the *original* command line
320 # check mutually exclusive options in the *original* command line
321 mutex_opts(opts,[qw('log logfile'),qw('rcfile profile'),
321 mutex_opts(opts,[qw('log logfile'),qw('rcfile profile'),
322 qw('classic profile'),qw('classic rcfile')])
322 qw('classic profile'),qw('classic rcfile')])
323
323
324 #---------------------------------------------------------------------------
324 #---------------------------------------------------------------------------
325 # Log replay
325 # Log replay
326
326
327 # if -logplay, we need to 'become' the other session. That basically means
327 # if -logplay, we need to 'become' the other session. That basically means
328 # replacing the current command line environment with that of the old
328 # replacing the current command line environment with that of the old
329 # session and moving on.
329 # session and moving on.
330
330
331 # this is needed so that later we know we're in session reload mode, as
331 # this is needed so that later we know we're in session reload mode, as
332 # opts_all will get overwritten:
332 # opts_all will get overwritten:
333 load_logplay = 0
333 load_logplay = 0
334
334
335 if opts_all.logplay:
335 if opts_all.logplay:
336 load_logplay = opts_all.logplay
336 load_logplay = opts_all.logplay
337 opts_debug_save = opts_all.debug
337 opts_debug_save = opts_all.debug
338 try:
338 try:
339 logplay = open(opts_all.logplay)
339 logplay = open(opts_all.logplay)
340 except IOError:
340 except IOError:
341 if opts_all.debug: IP.InteractiveTB()
341 if opts_all.debug: IP.InteractiveTB()
342 warn('Could not open logplay file '+`opts_all.logplay`)
342 warn('Could not open logplay file '+`opts_all.logplay`)
343 # restore state as if nothing had happened and move on, but make
343 # restore state as if nothing had happened and move on, but make
344 # sure that later we don't try to actually load the session file
344 # sure that later we don't try to actually load the session file
345 logplay = None
345 logplay = None
346 load_logplay = 0
346 load_logplay = 0
347 del opts_all.logplay
347 del opts_all.logplay
348 else:
348 else:
349 try:
349 try:
350 logplay.readline()
350 logplay.readline()
351 logplay.readline();
351 logplay.readline();
352 # this reloads that session's command line
352 # this reloads that session's command line
353 cmd = logplay.readline()[6:]
353 cmd = logplay.readline()[6:]
354 exec cmd
354 exec cmd
355 # restore the true debug flag given so that the process of
355 # restore the true debug flag given so that the process of
356 # session loading itself can be monitored.
356 # session loading itself can be monitored.
357 opts.debug = opts_debug_save
357 opts.debug = opts_debug_save
358 # save the logplay flag so later we don't overwrite the log
358 # save the logplay flag so later we don't overwrite the log
359 opts.logplay = load_logplay
359 opts.logplay = load_logplay
360 # now we must update our own structure with defaults
360 # now we must update our own structure with defaults
361 opts_all.update(opts)
361 opts_all.update(opts)
362 # now load args
362 # now load args
363 cmd = logplay.readline()[6:]
363 cmd = logplay.readline()[6:]
364 exec cmd
364 exec cmd
365 logplay.close()
365 logplay.close()
366 except:
366 except:
367 logplay.close()
367 logplay.close()
368 if opts_all.debug: IP.InteractiveTB()
368 if opts_all.debug: IP.InteractiveTB()
369 warn("Logplay file lacking full configuration information.\n"
369 warn("Logplay file lacking full configuration information.\n"
370 "I'll try to read it, but some things may not work.")
370 "I'll try to read it, but some things may not work.")
371
371
372 #-------------------------------------------------------------------------
372 #-------------------------------------------------------------------------
373 # set up output traps: catch all output from files, being run, modules
373 # set up output traps: catch all output from files, being run, modules
374 # loaded, etc. Then give it to the user in a clean form at the end.
374 # loaded, etc. Then give it to the user in a clean form at the end.
375
375
376 msg_out = 'Output messages. '
376 msg_out = 'Output messages. '
377 msg_err = 'Error messages. '
377 msg_err = 'Error messages. '
378 msg_sep = '\n'
378 msg_sep = '\n'
379 msg = Struct(config = OutputTrap('Configuration Loader',msg_out,
379 msg = Struct(config = OutputTrap('Configuration Loader',msg_out,
380 msg_err,msg_sep,debug,
380 msg_err,msg_sep,debug,
381 quiet_out=1),
381 quiet_out=1),
382 user_exec = OutputTrap('User File Execution',msg_out,
382 user_exec = OutputTrap('User File Execution',msg_out,
383 msg_err,msg_sep,debug),
383 msg_err,msg_sep,debug),
384 logplay = OutputTrap('Log Loader',msg_out,
384 logplay = OutputTrap('Log Loader',msg_out,
385 msg_err,msg_sep,debug),
385 msg_err,msg_sep,debug),
386 summary = ''
386 summary = ''
387 )
387 )
388
388
389 #-------------------------------------------------------------------------
389 #-------------------------------------------------------------------------
390 # Process user ipythonrc-type configuration files
390 # Process user ipythonrc-type configuration files
391
391
392 # turn on output trapping and log to msg.config
392 # turn on output trapping and log to msg.config
393 # remember that with debug on, trapping is actually disabled
393 # remember that with debug on, trapping is actually disabled
394 msg.config.trap_all()
394 msg.config.trap_all()
395
395
396 # look for rcfile in current or default directory
396 # look for rcfile in current or default directory
397 try:
397 try:
398 opts_all.rcfile = filefind(opts_all.rcfile,opts_all.ipythondir)
398 opts_all.rcfile = filefind(opts_all.rcfile,opts_all.ipythondir)
399 except IOError:
399 except IOError:
400 if opts_all.debug: IP.InteractiveTB()
400 if opts_all.debug: IP.InteractiveTB()
401 warn('Configuration file %s not found. Ignoring request.'
401 warn('Configuration file %s not found. Ignoring request.'
402 % (opts_all.rcfile) )
402 % (opts_all.rcfile) )
403
403
404 # 'profiles' are a shorthand notation for config filenames
404 # 'profiles' are a shorthand notation for config filenames
405 if opts_all.profile:
405 if opts_all.profile:
406 try:
406 try:
407 opts_all.rcfile = filefind('ipythonrc-' + opts_all.profile
407 opts_all.rcfile = filefind('ipythonrc-' + opts_all.profile
408 + rc_suffix,
408 + rc_suffix,
409 opts_all.ipythondir)
409 opts_all.ipythondir)
410 except IOError:
410 except IOError:
411 if opts_all.debug: IP.InteractiveTB()
411 if opts_all.debug: IP.InteractiveTB()
412 opts.profile = '' # remove profile from options if invalid
412 opts.profile = '' # remove profile from options if invalid
413 warn('Profile configuration file %s not found. Ignoring request.'
413 warn('Profile configuration file %s not found. Ignoring request.'
414 % (opts_all.profile) )
414 % (opts_all.profile) )
415
415
416 # load the config file
416 # load the config file
417 rcfiledata = None
417 rcfiledata = None
418 if opts_all.quick:
418 if opts_all.quick:
419 print 'Launching IPython in quick mode. No config file read.'
419 print 'Launching IPython in quick mode. No config file read.'
420 elif opts_all.classic:
420 elif opts_all.classic:
421 print 'Launching IPython in classic mode. No config file read.'
421 print 'Launching IPython in classic mode. No config file read.'
422 elif opts_all.rcfile:
422 elif opts_all.rcfile:
423 try:
423 try:
424 cfg_loader = ConfigLoader(conflict)
424 cfg_loader = ConfigLoader(conflict)
425 rcfiledata = cfg_loader.load(opts_all.rcfile,typeconv,
425 rcfiledata = cfg_loader.load(opts_all.rcfile,typeconv,
426 'include',opts_all.ipythondir,
426 'include',opts_all.ipythondir,
427 purge = 1,
427 purge = 1,
428 unique = conflict['preserve'])
428 unique = conflict['preserve'])
429 except:
429 except:
430 IP.InteractiveTB()
430 IP.InteractiveTB()
431 warn('Problems loading configuration file '+
431 warn('Problems loading configuration file '+
432 `opts_all.rcfile`+
432 `opts_all.rcfile`+
433 '\nStarting with default -bare bones- configuration.')
433 '\nStarting with default -bare bones- configuration.')
434 else:
434 else:
435 warn('No valid configuration file found in either currrent directory\n'+
435 warn('No valid configuration file found in either currrent directory\n'+
436 'or in the IPython config. directory: '+`opts_all.ipythondir`+
436 'or in the IPython config. directory: '+`opts_all.ipythondir`+
437 '\nProceeding with internal defaults.')
437 '\nProceeding with internal defaults.')
438
438
439 #------------------------------------------------------------------------
439 #------------------------------------------------------------------------
440 # Set exception handlers in mode requested by user.
440 # Set exception handlers in mode requested by user.
441 otrap = OutputTrap(trap_out=1) # trap messages from magic_xmode
441 otrap = OutputTrap(trap_out=1) # trap messages from magic_xmode
442 IP.magic_xmode(opts_all.xmode)
442 IP.magic_xmode(opts_all.xmode)
443 otrap.release_out()
443 otrap.release_out()
444
444
445 #------------------------------------------------------------------------
445 #------------------------------------------------------------------------
446 # Execute user config
446 # Execute user config
447
447
448 # Create a valid config structure with the right precedence order:
448 # Create a valid config structure with the right precedence order:
449 # defaults < rcfile < command line. This needs to be in the instance, so
449 # defaults < rcfile < command line. This needs to be in the instance, so
450 # that method calls below that rely on it find it.
450 # that method calls below that rely on it find it.
451 IP.rc = rc_def.copy()
451 IP.rc = rc_def.copy()
452
452
453 # Work with a local alias inside this routine to avoid unnecessary
453 # Work with a local alias inside this routine to avoid unnecessary
454 # attribute lookups.
454 # attribute lookups.
455 IP_rc = IP.rc
455 IP_rc = IP.rc
456
456
457 IP_rc.update(opts_def)
457 IP_rc.update(opts_def)
458 if rcfiledata:
458 if rcfiledata:
459 # now we can update
459 # now we can update
460 IP_rc.update(rcfiledata)
460 IP_rc.update(rcfiledata)
461 IP_rc.update(opts)
461 IP_rc.update(opts)
462 IP_rc.update(rc_override)
462 IP_rc.update(rc_override)
463
463
464 # Store the original cmd line for reference:
464 # Store the original cmd line for reference:
465 IP_rc.opts = opts
465 IP_rc.opts = opts
466 IP_rc.args = args
466 IP_rc.args = args
467
467
468 # create a *runtime* Struct like rc for holding parameters which may be
468 # create a *runtime* Struct like rc for holding parameters which may be
469 # created and/or modified by runtime user extensions.
469 # created and/or modified by runtime user extensions.
470 IP.runtime_rc = Struct()
470 IP.runtime_rc = Struct()
471
471
472 # from this point on, all config should be handled through IP_rc,
472 # from this point on, all config should be handled through IP_rc,
473 # opts* shouldn't be used anymore.
473 # opts* shouldn't be used anymore.
474
474
475 # add personal .ipython dir to sys.path so that users can put things in
475 # add personal .ipython dir to sys.path so that users can put things in
476 # there for customization
476 # there for customization
477 sys.path.append(IP_rc.ipythondir)
477 sys.path.append(IP_rc.ipythondir)
478 sys.path.insert(0, '') # add . to sys.path. Fix from Prabhu Ramachandran
478 sys.path.insert(0, '') # add . to sys.path. Fix from Prabhu Ramachandran
479
479
480 # update IP_rc with some special things that need manual
480 # update IP_rc with some special things that need manual
481 # tweaks. Basically options which affect other options. I guess this
481 # tweaks. Basically options which affect other options. I guess this
482 # should just be written so that options are fully orthogonal and we
482 # should just be written so that options are fully orthogonal and we
483 # wouldn't worry about this stuff!
483 # wouldn't worry about this stuff!
484
484
485 if IP_rc.classic:
485 if IP_rc.classic:
486 IP_rc.quick = 1
486 IP_rc.quick = 1
487 IP_rc.cache_size = 0
487 IP_rc.cache_size = 0
488 IP_rc.pprint = 0
488 IP_rc.pprint = 0
489 IP_rc.prompt_in1 = '>>> '
489 IP_rc.prompt_in1 = '>>> '
490 IP_rc.prompt_in2 = '... '
490 IP_rc.prompt_in2 = '... '
491 IP_rc.prompt_out = ''
491 IP_rc.prompt_out = ''
492 IP_rc.separate_in = IP_rc.separate_out = IP_rc.separate_out2 = '0'
492 IP_rc.separate_in = IP_rc.separate_out = IP_rc.separate_out2 = '0'
493 IP_rc.colors = 'NoColor'
493 IP_rc.colors = 'NoColor'
494 IP_rc.xmode = 'Plain'
494 IP_rc.xmode = 'Plain'
495
495
496 # configure readline
496 # configure readline
497 # Define the history file for saving commands in between sessions
497 # Define the history file for saving commands in between sessions
498 if IP_rc.profile:
498 if IP_rc.profile:
499 histfname = 'history-%s' % IP_rc.profile
499 histfname = 'history-%s' % IP_rc.profile
500 else:
500 else:
501 histfname = 'history'
501 histfname = 'history'
502 IP.histfile = os.path.join(opts_all.ipythondir,histfname)
502 IP.histfile = os.path.join(opts_all.ipythondir,histfname)
503
503
504 # update exception handlers with rc file status
504 # update exception handlers with rc file status
505 otrap.trap_out() # I don't want these messages ever.
505 otrap.trap_out() # I don't want these messages ever.
506 IP.magic_xmode(IP_rc.xmode)
506 IP.magic_xmode(IP_rc.xmode)
507 otrap.release_out()
507 otrap.release_out()
508
508
509 # activate logging if requested and not reloading a log
509 # activate logging if requested and not reloading a log
510 if IP_rc.logplay:
510 if IP_rc.logplay:
511 IP.magic_logstart(IP_rc.logplay + ' append')
511 IP.magic_logstart(IP_rc.logplay + ' append')
512 elif IP_rc.logfile:
512 elif IP_rc.logfile:
513 IP.magic_logstart(IP_rc.logfile)
513 IP.magic_logstart(IP_rc.logfile)
514 elif IP_rc.log:
514 elif IP_rc.log:
515 IP.magic_logstart()
515 IP.magic_logstart()
516
516
517 # find user editor so that it we don't have to look it up constantly
517 # find user editor so that it we don't have to look it up constantly
518 if IP_rc.editor.strip()=='0':
518 if IP_rc.editor.strip()=='0':
519 try:
519 try:
520 ed = os.environ['EDITOR']
520 ed = os.environ['EDITOR']
521 except KeyError:
521 except KeyError:
522 if os.name == 'posix':
522 if os.name == 'posix':
523 ed = 'vi' # the only one guaranteed to be there!
523 ed = 'vi' # the only one guaranteed to be there!
524 else:
524 else:
525 ed = 'notepad' # same in Windows!
525 ed = 'notepad' # same in Windows!
526 IP_rc.editor = ed
526 IP_rc.editor = ed
527
527
528 # Keep track of whether this is an embedded instance or not (useful for
528 # Keep track of whether this is an embedded instance or not (useful for
529 # post-mortems).
529 # post-mortems).
530 IP_rc.embedded = IP.embedded
530 IP_rc.embedded = IP.embedded
531
531
532 # Recursive reload
532 # Recursive reload
533 try:
533 try:
534 from IPython import deep_reload
534 from IPython import deep_reload
535 if IP_rc.deep_reload:
535 if IP_rc.deep_reload:
536 __builtin__.reload = deep_reload.reload
536 __builtin__.reload = deep_reload.reload
537 else:
537 else:
538 __builtin__.dreload = deep_reload.reload
538 __builtin__.dreload = deep_reload.reload
539 del deep_reload
539 del deep_reload
540 except ImportError:
540 except ImportError:
541 pass
541 pass
542
542
543 # Save the current state of our namespace so that the interactive shell
543 # Save the current state of our namespace so that the interactive shell
544 # can later know which variables have been created by us from config files
544 # can later know which variables have been created by us from config files
545 # and loading. This way, loading a file (in any way) is treated just like
545 # and loading. This way, loading a file (in any way) is treated just like
546 # defining things on the command line, and %who works as expected.
546 # defining things on the command line, and %who works as expected.
547
547
548 # DON'T do anything that affects the namespace beyond this point!
548 # DON'T do anything that affects the namespace beyond this point!
549 IP.internal_ns.update(__main__.__dict__)
549 IP.internal_ns.update(__main__.__dict__)
550
550
551 #IP.internal_ns.update(locals()) # so our stuff doesn't show up in %who
551 #IP.internal_ns.update(locals()) # so our stuff doesn't show up in %who
552
552
553 # Now run through the different sections of the users's config
553 # Now run through the different sections of the users's config
554 if IP_rc.debug:
554 if IP_rc.debug:
555 print 'Trying to execute the following configuration structure:'
555 print 'Trying to execute the following configuration structure:'
556 print '(Things listed first are deeper in the inclusion tree and get'
556 print '(Things listed first are deeper in the inclusion tree and get'
557 print 'loaded first).\n'
557 print 'loaded first).\n'
558 pprint(IP_rc.__dict__)
558 pprint(IP_rc.__dict__)
559
559
560 for mod in IP_rc.import_mod:
560 for mod in IP_rc.import_mod:
561 try:
561 try:
562 exec 'import '+mod in IP.user_ns
562 exec 'import '+mod in IP.user_ns
563 except :
563 except :
564 IP.InteractiveTB()
564 IP.InteractiveTB()
565 import_fail_info(mod)
565 import_fail_info(mod)
566
566
567 for mod_fn in IP_rc.import_some:
567 for mod_fn in IP_rc.import_some:
568 if mod_fn == []: break
568 if mod_fn == []: break
569 mod,fn = mod_fn[0],','.join(mod_fn[1:])
569 mod,fn = mod_fn[0],','.join(mod_fn[1:])
570 try:
570 try:
571 exec 'from '+mod+' import '+fn in IP.user_ns
571 exec 'from '+mod+' import '+fn in IP.user_ns
572 except :
572 except :
573 IP.InteractiveTB()
573 IP.InteractiveTB()
574 import_fail_info(mod,fn)
574 import_fail_info(mod,fn)
575
575
576 for mod in IP_rc.import_all:
576 for mod in IP_rc.import_all:
577 try:
577 try:
578 exec 'from '+mod+' import *' in IP.user_ns
578 exec 'from '+mod+' import *' in IP.user_ns
579 except :
579 except :
580 IP.InteractiveTB()
580 IP.InteractiveTB()
581 import_fail_info(mod)
581 import_fail_info(mod)
582
582
583 for code in IP_rc.execute:
583 for code in IP_rc.execute:
584 try:
584 try:
585 exec code in IP.user_ns
585 exec code in IP.user_ns
586 except:
586 except:
587 IP.InteractiveTB()
587 IP.InteractiveTB()
588 warn('Failure executing code: ' + `code`)
588 warn('Failure executing code: ' + `code`)
589
589
590 # Execute the files the user wants in ipythonrc
590 # Execute the files the user wants in ipythonrc
591 for file in IP_rc.execfile:
591 for file in IP_rc.execfile:
592 try:
592 try:
593 file = filefind(file,sys.path+[IPython_dir])
593 file = filefind(file,sys.path+[IPython_dir])
594 except IOError:
594 except IOError:
595 warn(itpl('File $file not found. Skipping it.'))
595 warn(itpl('File $file not found. Skipping it.'))
596 else:
596 else:
597 IP.safe_execfile(os.path.expanduser(file),IP.user_ns)
597 IP.safe_execfile(os.path.expanduser(file),IP.user_ns)
598
598
599 # release stdout and stderr and save config log into a global summary
599 # release stdout and stderr and save config log into a global summary
600 msg.config.release_all()
600 msg.config.release_all()
601 if IP_rc.messages:
601 if IP_rc.messages:
602 msg.summary += msg.config.summary_all()
602 msg.summary += msg.config.summary_all()
603
603
604 #------------------------------------------------------------------------
604 #------------------------------------------------------------------------
605 # Setup interactive session
605 # Setup interactive session
606
606
607 # Now we should be fully configured. We can then execute files or load
607 # Now we should be fully configured. We can then execute files or load
608 # things only needed for interactive use. Then we'll open the shell.
608 # things only needed for interactive use. Then we'll open the shell.
609
609
610 # Take a snapshot of the user namespace before opening the shell. That way
610 # Take a snapshot of the user namespace before opening the shell. That way
611 # we'll be able to identify which things were interactively defined and
611 # we'll be able to identify which things were interactively defined and
612 # which were defined through config files.
612 # which were defined through config files.
613 IP.user_config_ns = IP.user_ns.copy()
613 IP.user_config_ns = IP.user_ns.copy()
614
614
615 # Force reading a file as if it were a session log. Slower but safer.
615 # Force reading a file as if it were a session log. Slower but safer.
616 if load_logplay:
616 if load_logplay:
617 print 'Replaying log...'
617 print 'Replaying log...'
618 try:
618 try:
619 if IP_rc.debug:
619 if IP_rc.debug:
620 logplay_quiet = 0
620 logplay_quiet = 0
621 else:
621 else:
622 logplay_quiet = 1
622 logplay_quiet = 1
623
623
624 msg.logplay.trap_all()
624 msg.logplay.trap_all()
625 IP.safe_execfile(load_logplay,IP.user_ns,
625 IP.safe_execfile(load_logplay,IP.user_ns,
626 islog = 1, quiet = logplay_quiet)
626 islog = 1, quiet = logplay_quiet)
627 msg.logplay.release_all()
627 msg.logplay.release_all()
628 if IP_rc.messages:
628 if IP_rc.messages:
629 msg.summary += msg.logplay.summary_all()
629 msg.summary += msg.logplay.summary_all()
630 except:
630 except:
631 warn('Problems replaying logfile %s.' % load_logplay)
631 warn('Problems replaying logfile %s.' % load_logplay)
632 IP.InteractiveTB()
632 IP.InteractiveTB()
633
633
634 # Load remaining files in command line
634 # Load remaining files in command line
635 msg.user_exec.trap_all()
635 msg.user_exec.trap_all()
636
636
637 # Do NOT execute files named in the command line as scripts to be loaded
637 # Do NOT execute files named in the command line as scripts to be loaded
638 # by embedded instances. Doing so has the potential for an infinite
638 # by embedded instances. Doing so has the potential for an infinite
639 # recursion if there are exceptions thrown in the process.
639 # recursion if there are exceptions thrown in the process.
640
640
641 # XXX FIXME: the execution of user files should be moved out to after
641 # XXX FIXME: the execution of user files should be moved out to after
642 # ipython is fully initialized, just as if they were run via %run at the
642 # ipython is fully initialized, just as if they were run via %run at the
643 # ipython prompt. This would also give them the benefit of ipython's
643 # ipython prompt. This would also give them the benefit of ipython's
644 # nice tracebacks.
644 # nice tracebacks.
645
645
646 if not embedded and IP_rc.args:
646 if not embedded and IP_rc.args:
647 name_save = IP.user_ns['__name__']
647 name_save = IP.user_ns['__name__']
648 IP.user_ns['__name__'] = '__main__'
648 IP.user_ns['__name__'] = '__main__'
649 try:
649 try:
650 # Set our own excepthook in case the user code tries to call it
650 # Set our own excepthook in case the user code tries to call it
651 # directly. This prevents triggering the IPython crash handler.
651 # directly. This prevents triggering the IPython crash handler.
652 old_excepthook,sys.excepthook = sys.excepthook, IP.excepthook
652 old_excepthook,sys.excepthook = sys.excepthook, IP.excepthook
653 for run in args:
653 for run in args:
654 IP.safe_execfile(run,IP.user_ns)
654 IP.safe_execfile(run,IP.user_ns)
655 finally:
655 finally:
656 # Reset our crash handler in place
656 # Reset our crash handler in place
657 sys.excepthook = old_excepthook
657 sys.excepthook = old_excepthook
658
658
659 IP.user_ns['__name__'] = name_save
659 IP.user_ns['__name__'] = name_save
660
660
661 msg.user_exec.release_all()
661 msg.user_exec.release_all()
662 if IP_rc.messages:
662 if IP_rc.messages:
663 msg.summary += msg.user_exec.summary_all()
663 msg.summary += msg.user_exec.summary_all()
664
664
665 # since we can't specify a null string on the cmd line, 0 is the equivalent:
665 # since we can't specify a null string on the cmd line, 0 is the equivalent:
666 if IP_rc.nosep:
666 if IP_rc.nosep:
667 IP_rc.separate_in = IP_rc.separate_out = IP_rc.separate_out2 = '0'
667 IP_rc.separate_in = IP_rc.separate_out = IP_rc.separate_out2 = '0'
668 if IP_rc.separate_in == '0': IP_rc.separate_in = ''
668 if IP_rc.separate_in == '0': IP_rc.separate_in = ''
669 if IP_rc.separate_out == '0': IP_rc.separate_out = ''
669 if IP_rc.separate_out == '0': IP_rc.separate_out = ''
670 if IP_rc.separate_out2 == '0': IP_rc.separate_out2 = ''
670 if IP_rc.separate_out2 == '0': IP_rc.separate_out2 = ''
671 IP_rc.separate_in = IP_rc.separate_in.replace('\\n','\n')
671 IP_rc.separate_in = IP_rc.separate_in.replace('\\n','\n')
672 IP_rc.separate_out = IP_rc.separate_out.replace('\\n','\n')
672 IP_rc.separate_out = IP_rc.separate_out.replace('\\n','\n')
673 IP_rc.separate_out2 = IP_rc.separate_out2.replace('\\n','\n')
673 IP_rc.separate_out2 = IP_rc.separate_out2.replace('\\n','\n')
674
674
675 # Determine how many lines at the bottom of the screen are needed for
675 # Determine how many lines at the bottom of the screen are needed for
676 # showing prompts, so we can know wheter long strings are to be printed or
676 # showing prompts, so we can know wheter long strings are to be printed or
677 # paged:
677 # paged:
678 num_lines_bot = IP_rc.separate_in.count('\n')+1
678 num_lines_bot = IP_rc.separate_in.count('\n')+1
679 IP_rc.screen_length = IP_rc.screen_length - num_lines_bot
679 IP_rc.screen_length = IP_rc.screen_length - num_lines_bot
680
680
681 # configure startup banner
681 # configure startup banner
682 if IP_rc.c: # regular python doesn't print the banner with -c
682 if IP_rc.c: # regular python doesn't print the banner with -c
683 IP_rc.banner = 0
683 IP_rc.banner = 0
684 if IP_rc.banner:
684 if IP_rc.banner:
685 BANN_P = IP.BANNER_PARTS
685 BANN_P = IP.BANNER_PARTS
686 else:
686 else:
687 BANN_P = []
687 BANN_P = []
688
688
689 if IP_rc.profile: BANN_P.append('IPython profile: %s\n' % IP_rc.profile)
689 if IP_rc.profile: BANN_P.append('IPython profile: %s\n' % IP_rc.profile)
690
690
691 # add message log (possibly empty)
691 # add message log (possibly empty)
692 if msg.summary: BANN_P.append(msg.summary)
692 if msg.summary: BANN_P.append(msg.summary)
693 # Final banner is a string
693 # Final banner is a string
694 IP.BANNER = '\n'.join(BANN_P)
694 IP.BANNER = '\n'.join(BANN_P)
695
695
696 # Finalize the IPython instance. This assumes the rc structure is fully
696 # Finalize the IPython instance. This assumes the rc structure is fully
697 # in place.
697 # in place.
698 IP.post_config_initialization()
698 IP.post_config_initialization()
699
699
700 return IP
700 return IP
701 #************************ end of file <ipmaker.py> **************************
701 #************************ end of file <ipmaker.py> **************************
@@ -1,858 +1,857 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 992 2006-01-04 18:35:40Z fperez $"""
63 $Id: ultraTB.py 994 2006-01-08 08:29:44Z 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 inspect
79 import inspect
80 import keyword
80 import keyword
81 import linecache
81 import linecache
82 import os
82 import os
83 import pydoc
83 import pydoc
84 import string
84 import string
85 import sys
85 import sys
86 import time
86 import time
87 import tokenize
87 import tokenize
88 import traceback
88 import traceback
89 import types
89 import types
90
90
91 # IPython's own modules
91 # IPython's own modules
92 # Modified pdb which doesn't damage IPython's readline handling
92 # Modified pdb which doesn't damage IPython's readline handling
93 from IPython import Debugger
93 from IPython import Debugger
94 from IPython.Struct import Struct
94 from IPython.Struct import Struct
95 from IPython.excolors import ExceptionColors
95 from IPython.excolors import ExceptionColors
96 from IPython.genutils import Term,uniq_stable,error,info
96 from IPython.genutils import Term,uniq_stable,error,info
97
97
98 # Globals
98 # Globals
99 # amount of space to put line numbers before verbose tracebacks
99 # amount of space to put line numbers before verbose tracebacks
100 INDENT_SIZE = 8
100 INDENT_SIZE = 8
101
101
102 #---------------------------------------------------------------------------
102 #---------------------------------------------------------------------------
103 # Code begins
103 # Code begins
104
104
105 # Utility functions
105 # Utility functions
106 def inspect_error():
106 def inspect_error():
107 """Print a message about internal inspect errors.
107 """Print a message about internal inspect errors.
108
108
109 These are unfortunately quite common."""
109 These are unfortunately quite common."""
110
110
111 error('Internal Python error in the inspect module.\n'
111 error('Internal Python error in the inspect module.\n'
112 'Below is the traceback from this internal error.\n')
112 'Below is the traceback from this internal error.\n')
113
113
114 def _fixed_getinnerframes(etb, context=1,tb_offset=0):
114 def _fixed_getinnerframes(etb, context=1,tb_offset=0):
115 import linecache
115 import linecache
116 LNUM_POS, LINES_POS, INDEX_POS = 2, 4, 5
116 LNUM_POS, LINES_POS, INDEX_POS = 2, 4, 5
117
117
118 records = inspect.getinnerframes(etb, context)
118 records = inspect.getinnerframes(etb, context)
119
119
120 # If the error is at the console, don't build any context, since it would
120 # If the error is at the console, don't build any context, since it would
121 # otherwise produce 5 blank lines printed out (there is no file at the
121 # otherwise produce 5 blank lines printed out (there is no file at the
122 # console)
122 # console)
123 rec_check = records[tb_offset:]
123 rec_check = records[tb_offset:]
124 try:
124 try:
125 rname = rec_check[0][1]
125 rname = rec_check[0][1]
126 if rname == '<ipython console>' or rname.endswith('<string>'):
126 if rname == '<ipython console>' or rname.endswith('<string>'):
127 return rec_check
127 return rec_check
128 except IndexError:
128 except IndexError:
129 pass
129 pass
130
130
131 aux = traceback.extract_tb(etb)
131 aux = traceback.extract_tb(etb)
132 assert len(records) == len(aux)
132 assert len(records) == len(aux)
133 for i, (file, lnum, _, _) in zip(range(len(records)), aux):
133 for i, (file, lnum, _, _) in zip(range(len(records)), aux):
134 maybeStart = lnum-1 - context//2
134 maybeStart = lnum-1 - context//2
135 start = max(maybeStart, 0)
135 start = max(maybeStart, 0)
136 end = start + context
136 end = start + context
137 lines = linecache.getlines(file)[start:end]
137 lines = linecache.getlines(file)[start:end]
138 # pad with empty lines if necessary
138 # pad with empty lines if necessary
139 if maybeStart < 0:
139 if maybeStart < 0:
140 lines = (['\n'] * -maybeStart) + lines
140 lines = (['\n'] * -maybeStart) + lines
141 if len(lines) < context:
141 if len(lines) < context:
142 lines += ['\n'] * (context - len(lines))
142 lines += ['\n'] * (context - len(lines))
143 assert len(lines) == context
144 buf = list(records[i])
143 buf = list(records[i])
145 buf[LNUM_POS] = lnum
144 buf[LNUM_POS] = lnum
146 buf[INDEX_POS] = lnum - 1 - start
145 buf[INDEX_POS] = lnum - 1 - start
147 buf[LINES_POS] = lines
146 buf[LINES_POS] = lines
148 records[i] = tuple(buf)
147 records[i] = tuple(buf)
149 return records[tb_offset:]
148 return records[tb_offset:]
150
149
151 # Helper function -- largely belongs to VerboseTB, but we need the same
150 # Helper function -- largely belongs to VerboseTB, but we need the same
152 # functionality to produce a pseudo verbose TB for SyntaxErrors, so that they
151 # functionality to produce a pseudo verbose TB for SyntaxErrors, so that they
153 # can be recognized properly by ipython.el's py-traceback-line-re
152 # can be recognized properly by ipython.el's py-traceback-line-re
154 # (SyntaxErrors have to be treated specially because they have no traceback)
153 # (SyntaxErrors have to be treated specially because they have no traceback)
155 def _formatTracebackLines(lnum, index, lines, Colors, lvals=None):
154 def _formatTracebackLines(lnum, index, lines, Colors, lvals=None):
156 numbers_width = INDENT_SIZE - 1
155 numbers_width = INDENT_SIZE - 1
157 res = []
156 res = []
158 i = lnum - index
157 i = lnum - index
159 for line in lines:
158 for line in lines:
160 if i == lnum:
159 if i == lnum:
161 # This is the line with the error
160 # This is the line with the error
162 pad = numbers_width - len(str(i))
161 pad = numbers_width - len(str(i))
163 if pad >= 3:
162 if pad >= 3:
164 marker = '-'*(pad-3) + '-> '
163 marker = '-'*(pad-3) + '-> '
165 elif pad == 2:
164 elif pad == 2:
166 marker = '> '
165 marker = '> '
167 elif pad == 1:
166 elif pad == 1:
168 marker = '>'
167 marker = '>'
169 else:
168 else:
170 marker = ''
169 marker = ''
171 num = marker + str(i)
170 num = marker + str(i)
172 line = '%s%s%s %s%s' %(Colors.linenoEm, num,
171 line = '%s%s%s %s%s' %(Colors.linenoEm, num,
173 Colors.line, line, Colors.Normal)
172 Colors.line, line, Colors.Normal)
174 else:
173 else:
175 num = '%*s' % (numbers_width,i)
174 num = '%*s' % (numbers_width,i)
176 line = '%s%s%s %s' %(Colors.lineno, num,
175 line = '%s%s%s %s' %(Colors.lineno, num,
177 Colors.Normal, line)
176 Colors.Normal, line)
178
177
179 res.append(line)
178 res.append(line)
180 if lvals and i == lnum:
179 if lvals and i == lnum:
181 res.append(lvals + '\n')
180 res.append(lvals + '\n')
182 i = i + 1
181 i = i + 1
183 return res
182 return res
184
183
185 #---------------------------------------------------------------------------
184 #---------------------------------------------------------------------------
186 # Module classes
185 # Module classes
187 class TBTools:
186 class TBTools:
188 """Basic tools used by all traceback printer classes."""
187 """Basic tools used by all traceback printer classes."""
189
188
190 def __init__(self,color_scheme = 'NoColor',call_pdb=False):
189 def __init__(self,color_scheme = 'NoColor',call_pdb=False):
191 # Whether to call the interactive pdb debugger after printing
190 # Whether to call the interactive pdb debugger after printing
192 # tracebacks or not
191 # tracebacks or not
193 self.call_pdb = call_pdb
192 self.call_pdb = call_pdb
194
193
195 # Create color table
194 # Create color table
196 self.color_scheme_table = ExceptionColors
195 self.color_scheme_table = ExceptionColors
197
196
198 self.set_colors(color_scheme)
197 self.set_colors(color_scheme)
199 self.old_scheme = color_scheme # save initial value for toggles
198 self.old_scheme = color_scheme # save initial value for toggles
200
199
201 if call_pdb:
200 if call_pdb:
202 self.pdb = Debugger.Pdb(self.color_scheme_table.active_scheme_name)
201 self.pdb = Debugger.Pdb(self.color_scheme_table.active_scheme_name)
203 else:
202 else:
204 self.pdb = None
203 self.pdb = None
205
204
206 def set_colors(self,*args,**kw):
205 def set_colors(self,*args,**kw):
207 """Shorthand access to the color table scheme selector method."""
206 """Shorthand access to the color table scheme selector method."""
208
207
209 self.color_scheme_table.set_active_scheme(*args,**kw)
208 self.color_scheme_table.set_active_scheme(*args,**kw)
210 # for convenience, set Colors to the active scheme
209 # for convenience, set Colors to the active scheme
211 self.Colors = self.color_scheme_table.active_colors
210 self.Colors = self.color_scheme_table.active_colors
212
211
213 def color_toggle(self):
212 def color_toggle(self):
214 """Toggle between the currently active color scheme and NoColor."""
213 """Toggle between the currently active color scheme and NoColor."""
215
214
216 if self.color_scheme_table.active_scheme_name == 'NoColor':
215 if self.color_scheme_table.active_scheme_name == 'NoColor':
217 self.color_scheme_table.set_active_scheme(self.old_scheme)
216 self.color_scheme_table.set_active_scheme(self.old_scheme)
218 self.Colors = self.color_scheme_table.active_colors
217 self.Colors = self.color_scheme_table.active_colors
219 else:
218 else:
220 self.old_scheme = self.color_scheme_table.active_scheme_name
219 self.old_scheme = self.color_scheme_table.active_scheme_name
221 self.color_scheme_table.set_active_scheme('NoColor')
220 self.color_scheme_table.set_active_scheme('NoColor')
222 self.Colors = self.color_scheme_table.active_colors
221 self.Colors = self.color_scheme_table.active_colors
223
222
224 #---------------------------------------------------------------------------
223 #---------------------------------------------------------------------------
225 class ListTB(TBTools):
224 class ListTB(TBTools):
226 """Print traceback information from a traceback list, with optional color.
225 """Print traceback information from a traceback list, with optional color.
227
226
228 Calling: requires 3 arguments:
227 Calling: requires 3 arguments:
229 (etype, evalue, elist)
228 (etype, evalue, elist)
230 as would be obtained by:
229 as would be obtained by:
231 etype, evalue, tb = sys.exc_info()
230 etype, evalue, tb = sys.exc_info()
232 if tb:
231 if tb:
233 elist = traceback.extract_tb(tb)
232 elist = traceback.extract_tb(tb)
234 else:
233 else:
235 elist = None
234 elist = None
236
235
237 It can thus be used by programs which need to process the traceback before
236 It can thus be used by programs which need to process the traceback before
238 printing (such as console replacements based on the code module from the
237 printing (such as console replacements based on the code module from the
239 standard library).
238 standard library).
240
239
241 Because they are meant to be called without a full traceback (only a
240 Because they are meant to be called without a full traceback (only a
242 list), instances of this class can't call the interactive pdb debugger."""
241 list), instances of this class can't call the interactive pdb debugger."""
243
242
244 def __init__(self,color_scheme = 'NoColor'):
243 def __init__(self,color_scheme = 'NoColor'):
245 TBTools.__init__(self,color_scheme = color_scheme,call_pdb=0)
244 TBTools.__init__(self,color_scheme = color_scheme,call_pdb=0)
246
245
247 def __call__(self, etype, value, elist):
246 def __call__(self, etype, value, elist):
248 print >> Term.cerr, self.text(etype,value,elist)
247 print >> Term.cerr, self.text(etype,value,elist)
249
248
250 def text(self,etype, value, elist,context=5):
249 def text(self,etype, value, elist,context=5):
251 """Return a color formatted string with the traceback info."""
250 """Return a color formatted string with the traceback info."""
252
251
253 Colors = self.Colors
252 Colors = self.Colors
254 out_string = ['%s%s%s\n' % (Colors.topline,'-'*60,Colors.Normal)]
253 out_string = ['%s%s%s\n' % (Colors.topline,'-'*60,Colors.Normal)]
255 if elist:
254 if elist:
256 out_string.append('Traceback %s(most recent call last)%s:' % \
255 out_string.append('Traceback %s(most recent call last)%s:' % \
257 (Colors.normalEm, Colors.Normal) + '\n')
256 (Colors.normalEm, Colors.Normal) + '\n')
258 out_string.extend(self._format_list(elist))
257 out_string.extend(self._format_list(elist))
259 lines = self._format_exception_only(etype, value)
258 lines = self._format_exception_only(etype, value)
260 for line in lines[:-1]:
259 for line in lines[:-1]:
261 out_string.append(" "+line)
260 out_string.append(" "+line)
262 out_string.append(lines[-1])
261 out_string.append(lines[-1])
263 return ''.join(out_string)
262 return ''.join(out_string)
264
263
265 def _format_list(self, extracted_list):
264 def _format_list(self, extracted_list):
266 """Format a list of traceback entry tuples for printing.
265 """Format a list of traceback entry tuples for printing.
267
266
268 Given a list of tuples as returned by extract_tb() or
267 Given a list of tuples as returned by extract_tb() or
269 extract_stack(), return a list of strings ready for printing.
268 extract_stack(), return a list of strings ready for printing.
270 Each string in the resulting list corresponds to the item with the
269 Each string in the resulting list corresponds to the item with the
271 same index in the argument list. Each string ends in a newline;
270 same index in the argument list. Each string ends in a newline;
272 the strings may contain internal newlines as well, for those items
271 the strings may contain internal newlines as well, for those items
273 whose source text line is not None.
272 whose source text line is not None.
274
273
275 Lifted almost verbatim from traceback.py
274 Lifted almost verbatim from traceback.py
276 """
275 """
277
276
278 Colors = self.Colors
277 Colors = self.Colors
279 list = []
278 list = []
280 for filename, lineno, name, line in extracted_list[:-1]:
279 for filename, lineno, name, line in extracted_list[:-1]:
281 item = ' File %s"%s"%s, line %s%d%s, in %s%s%s\n' % \
280 item = ' File %s"%s"%s, line %s%d%s, in %s%s%s\n' % \
282 (Colors.filename, filename, Colors.Normal,
281 (Colors.filename, filename, Colors.Normal,
283 Colors.lineno, lineno, Colors.Normal,
282 Colors.lineno, lineno, Colors.Normal,
284 Colors.name, name, Colors.Normal)
283 Colors.name, name, Colors.Normal)
285 if line:
284 if line:
286 item = item + ' %s\n' % line.strip()
285 item = item + ' %s\n' % line.strip()
287 list.append(item)
286 list.append(item)
288 # Emphasize the last entry
287 # Emphasize the last entry
289 filename, lineno, name, line = extracted_list[-1]
288 filename, lineno, name, line = extracted_list[-1]
290 item = '%s File %s"%s"%s, line %s%d%s, in %s%s%s%s\n' % \
289 item = '%s File %s"%s"%s, line %s%d%s, in %s%s%s%s\n' % \
291 (Colors.normalEm,
290 (Colors.normalEm,
292 Colors.filenameEm, filename, Colors.normalEm,
291 Colors.filenameEm, filename, Colors.normalEm,
293 Colors.linenoEm, lineno, Colors.normalEm,
292 Colors.linenoEm, lineno, Colors.normalEm,
294 Colors.nameEm, name, Colors.normalEm,
293 Colors.nameEm, name, Colors.normalEm,
295 Colors.Normal)
294 Colors.Normal)
296 if line:
295 if line:
297 item = item + '%s %s%s\n' % (Colors.line, line.strip(),
296 item = item + '%s %s%s\n' % (Colors.line, line.strip(),
298 Colors.Normal)
297 Colors.Normal)
299 list.append(item)
298 list.append(item)
300 return list
299 return list
301
300
302 def _format_exception_only(self, etype, value):
301 def _format_exception_only(self, etype, value):
303 """Format the exception part of a traceback.
302 """Format the exception part of a traceback.
304
303
305 The arguments are the exception type and value such as given by
304 The arguments are the exception type and value such as given by
306 sys.exc_info()[:2]. The return value is a list of strings, each ending
305 sys.exc_info()[:2]. The return value is a list of strings, each ending
307 in a newline. Normally, the list contains a single string; however,
306 in a newline. Normally, the list contains a single string; however,
308 for SyntaxError exceptions, it contains several lines that (when
307 for SyntaxError exceptions, it contains several lines that (when
309 printed) display detailed information about where the syntax error
308 printed) display detailed information about where the syntax error
310 occurred. The message indicating which exception occurred is the
309 occurred. The message indicating which exception occurred is the
311 always last string in the list.
310 always last string in the list.
312
311
313 Also lifted nearly verbatim from traceback.py
312 Also lifted nearly verbatim from traceback.py
314 """
313 """
315
314
316 Colors = self.Colors
315 Colors = self.Colors
317 list = []
316 list = []
318 if type(etype) == types.ClassType:
317 if type(etype) == types.ClassType:
319 stype = Colors.excName + etype.__name__ + Colors.Normal
318 stype = Colors.excName + etype.__name__ + Colors.Normal
320 else:
319 else:
321 stype = etype # String exceptions don't get special coloring
320 stype = etype # String exceptions don't get special coloring
322 if value is None:
321 if value is None:
323 list.append( str(stype) + '\n')
322 list.append( str(stype) + '\n')
324 else:
323 else:
325 if etype is SyntaxError:
324 if etype is SyntaxError:
326 try:
325 try:
327 msg, (filename, lineno, offset, line) = value
326 msg, (filename, lineno, offset, line) = value
328 except:
327 except:
329 pass
328 pass
330 else:
329 else:
331 #print 'filename is',filename # dbg
330 #print 'filename is',filename # dbg
332 if not filename: filename = "<string>"
331 if not filename: filename = "<string>"
333 list.append('%s File %s"%s"%s, line %s%d%s\n' % \
332 list.append('%s File %s"%s"%s, line %s%d%s\n' % \
334 (Colors.normalEm,
333 (Colors.normalEm,
335 Colors.filenameEm, filename, Colors.normalEm,
334 Colors.filenameEm, filename, Colors.normalEm,
336 Colors.linenoEm, lineno, Colors.Normal ))
335 Colors.linenoEm, lineno, Colors.Normal ))
337 if line is not None:
336 if line is not None:
338 i = 0
337 i = 0
339 while i < len(line) and line[i].isspace():
338 while i < len(line) and line[i].isspace():
340 i = i+1
339 i = i+1
341 list.append('%s %s%s\n' % (Colors.line,
340 list.append('%s %s%s\n' % (Colors.line,
342 line.strip(),
341 line.strip(),
343 Colors.Normal))
342 Colors.Normal))
344 if offset is not None:
343 if offset is not None:
345 s = ' '
344 s = ' '
346 for c in line[i:offset-1]:
345 for c in line[i:offset-1]:
347 if c.isspace():
346 if c.isspace():
348 s = s + c
347 s = s + c
349 else:
348 else:
350 s = s + ' '
349 s = s + ' '
351 list.append('%s%s^%s\n' % (Colors.caret, s,
350 list.append('%s%s^%s\n' % (Colors.caret, s,
352 Colors.Normal) )
351 Colors.Normal) )
353 value = msg
352 value = msg
354 s = self._some_str(value)
353 s = self._some_str(value)
355 if s:
354 if s:
356 list.append('%s%s:%s %s\n' % (str(stype), Colors.excName,
355 list.append('%s%s:%s %s\n' % (str(stype), Colors.excName,
357 Colors.Normal, s))
356 Colors.Normal, s))
358 else:
357 else:
359 list.append('%s\n' % str(stype))
358 list.append('%s\n' % str(stype))
360 return list
359 return list
361
360
362 def _some_str(self, value):
361 def _some_str(self, value):
363 # Lifted from traceback.py
362 # Lifted from traceback.py
364 try:
363 try:
365 return str(value)
364 return str(value)
366 except:
365 except:
367 return '<unprintable %s object>' % type(value).__name__
366 return '<unprintable %s object>' % type(value).__name__
368
367
369 #----------------------------------------------------------------------------
368 #----------------------------------------------------------------------------
370 class VerboseTB(TBTools):
369 class VerboseTB(TBTools):
371 """A port of Ka-Ping Yee's cgitb.py module that outputs color text instead
370 """A port of Ka-Ping Yee's cgitb.py module that outputs color text instead
372 of HTML. Requires inspect and pydoc. Crazy, man.
371 of HTML. Requires inspect and pydoc. Crazy, man.
373
372
374 Modified version which optionally strips the topmost entries from the
373 Modified version which optionally strips the topmost entries from the
375 traceback, to be used with alternate interpreters (because their own code
374 traceback, to be used with alternate interpreters (because their own code
376 would appear in the traceback)."""
375 would appear in the traceback)."""
377
376
378 def __init__(self,color_scheme = 'Linux',tb_offset=0,long_header=0,
377 def __init__(self,color_scheme = 'Linux',tb_offset=0,long_header=0,
379 call_pdb = 0, include_vars=1):
378 call_pdb = 0, include_vars=1):
380 """Specify traceback offset, headers and color scheme.
379 """Specify traceback offset, headers and color scheme.
381
380
382 Define how many frames to drop from the tracebacks. Calling it with
381 Define how many frames to drop from the tracebacks. Calling it with
383 tb_offset=1 allows use of this handler in interpreters which will have
382 tb_offset=1 allows use of this handler in interpreters which will have
384 their own code at the top of the traceback (VerboseTB will first
383 their own code at the top of the traceback (VerboseTB will first
385 remove that frame before printing the traceback info)."""
384 remove that frame before printing the traceback info)."""
386 TBTools.__init__(self,color_scheme=color_scheme,call_pdb=call_pdb)
385 TBTools.__init__(self,color_scheme=color_scheme,call_pdb=call_pdb)
387 self.tb_offset = tb_offset
386 self.tb_offset = tb_offset
388 self.long_header = long_header
387 self.long_header = long_header
389 self.include_vars = include_vars
388 self.include_vars = include_vars
390
389
391 def text(self, etype, evalue, etb, context=5):
390 def text(self, etype, evalue, etb, context=5):
392 """Return a nice text document describing the traceback."""
391 """Return a nice text document describing the traceback."""
393
392
394 # some locals
393 # some locals
395 Colors = self.Colors # just a shorthand + quicker name lookup
394 Colors = self.Colors # just a shorthand + quicker name lookup
396 ColorsNormal = Colors.Normal # used a lot
395 ColorsNormal = Colors.Normal # used a lot
397 indent = ' '*INDENT_SIZE
396 indent = ' '*INDENT_SIZE
398 text_repr = pydoc.text.repr
397 text_repr = pydoc.text.repr
399 exc = '%s%s%s' % (Colors.excName, str(etype), ColorsNormal)
398 exc = '%s%s%s' % (Colors.excName, str(etype), ColorsNormal)
400 em_normal = '%s\n%s%s' % (Colors.valEm, indent,ColorsNormal)
399 em_normal = '%s\n%s%s' % (Colors.valEm, indent,ColorsNormal)
401 undefined = '%sundefined%s' % (Colors.em, ColorsNormal)
400 undefined = '%sundefined%s' % (Colors.em, ColorsNormal)
402
401
403 # some internal-use functions
402 # some internal-use functions
404 def eqrepr(value, repr=text_repr): return '=%s' % repr(value)
403 def eqrepr(value, repr=text_repr): return '=%s' % repr(value)
405 def nullrepr(value, repr=text_repr): return ''
404 def nullrepr(value, repr=text_repr): return ''
406
405
407 # meat of the code begins
406 # meat of the code begins
408 if type(etype) is types.ClassType:
407 if type(etype) is types.ClassType:
409 etype = etype.__name__
408 etype = etype.__name__
410
409
411 if self.long_header:
410 if self.long_header:
412 # Header with the exception type, python version, and date
411 # Header with the exception type, python version, and date
413 pyver = 'Python ' + string.split(sys.version)[0] + ': ' + sys.executable
412 pyver = 'Python ' + string.split(sys.version)[0] + ': ' + sys.executable
414 date = time.ctime(time.time())
413 date = time.ctime(time.time())
415
414
416 head = '%s%s%s\n%s%s%s\n%s' % (Colors.topline, '-'*75, ColorsNormal,
415 head = '%s%s%s\n%s%s%s\n%s' % (Colors.topline, '-'*75, ColorsNormal,
417 exc, ' '*(75-len(str(etype))-len(pyver)),
416 exc, ' '*(75-len(str(etype))-len(pyver)),
418 pyver, string.rjust(date, 75) )
417 pyver, string.rjust(date, 75) )
419 head += "\nA problem occured executing Python code. Here is the sequence of function"\
418 head += "\nA problem occured executing Python code. Here is the sequence of function"\
420 "\ncalls leading up to the error, with the most recent (innermost) call last."
419 "\ncalls leading up to the error, with the most recent (innermost) call last."
421 else:
420 else:
422 # Simplified header
421 # Simplified header
423 head = '%s%s%s\n%s%s' % (Colors.topline, '-'*75, ColorsNormal,exc,
422 head = '%s%s%s\n%s%s' % (Colors.topline, '-'*75, ColorsNormal,exc,
424 string.rjust('Traceback (most recent call last)',
423 string.rjust('Traceback (most recent call last)',
425 75 - len(str(etype)) ) )
424 75 - len(str(etype)) ) )
426 frames = []
425 frames = []
427 # Flush cache before calling inspect. This helps alleviate some of the
426 # Flush cache before calling inspect. This helps alleviate some of the
428 # problems with python 2.3's inspect.py.
427 # problems with python 2.3's inspect.py.
429 linecache.checkcache()
428 linecache.checkcache()
430 # Drop topmost frames if requested
429 # Drop topmost frames if requested
431 try:
430 try:
432 # Try the default getinnerframes and Alex's: Alex's fixes some
431 # Try the default getinnerframes and Alex's: Alex's fixes some
433 # problems, but it generates empty tracebacks for console errors
432 # problems, but it generates empty tracebacks for console errors
434 # (5 blanks lines) where none should be returned.
433 # (5 blanks lines) where none should be returned.
435 #records = inspect.getinnerframes(etb, context)[self.tb_offset:]
434 #records = inspect.getinnerframes(etb, context)[self.tb_offset:]
436 #print 'python records:', records # dbg
435 #print 'python records:', records # dbg
437 records = _fixed_getinnerframes(etb, context,self.tb_offset)
436 records = _fixed_getinnerframes(etb, context,self.tb_offset)
438 #print 'alex records:', records # dbg
437 #print 'alex records:', records # dbg
439 except:
438 except:
440
439
441 # FIXME: I've been getting many crash reports from python 2.3
440 # 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
441 # 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
442 # to reproduce this, I should either write a better workaround or
444 # file a bug report against inspect (if that's the real problem).
443 # 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
444 # So far, I haven't been able to find an isolated example to
446 # reproduce the problem.
445 # reproduce the problem.
447 inspect_error()
446 inspect_error()
448 traceback.print_exc(file=Term.cerr)
447 traceback.print_exc(file=Term.cerr)
449 info('\nUnfortunately, your original traceback can not be constructed.\n')
448 info('\nUnfortunately, your original traceback can not be constructed.\n')
450 return ''
449 return ''
451
450
452 # build some color string templates outside these nested loops
451 # build some color string templates outside these nested loops
453 tpl_link = '%s%%s%s' % (Colors.filenameEm,ColorsNormal)
452 tpl_link = '%s%%s%s' % (Colors.filenameEm,ColorsNormal)
454 tpl_call = 'in %s%%s%s%%s%s' % (Colors.vName, Colors.valEm,
453 tpl_call = 'in %s%%s%s%%s%s' % (Colors.vName, Colors.valEm,
455 ColorsNormal)
454 ColorsNormal)
456 tpl_call_fail = 'in %s%%s%s(***failed resolving arguments***)%s' % \
455 tpl_call_fail = 'in %s%%s%s(***failed resolving arguments***)%s' % \
457 (Colors.vName, Colors.valEm, ColorsNormal)
456 (Colors.vName, Colors.valEm, ColorsNormal)
458 tpl_local_var = '%s%%s%s' % (Colors.vName, ColorsNormal)
457 tpl_local_var = '%s%%s%s' % (Colors.vName, ColorsNormal)
459 tpl_global_var = '%sglobal%s %s%%s%s' % (Colors.em, ColorsNormal,
458 tpl_global_var = '%sglobal%s %s%%s%s' % (Colors.em, ColorsNormal,
460 Colors.vName, ColorsNormal)
459 Colors.vName, ColorsNormal)
461 tpl_name_val = '%%s %s= %%s%s' % (Colors.valEm, ColorsNormal)
460 tpl_name_val = '%%s %s= %%s%s' % (Colors.valEm, ColorsNormal)
462 tpl_line = '%s%%s%s %%s' % (Colors.lineno, ColorsNormal)
461 tpl_line = '%s%%s%s %%s' % (Colors.lineno, ColorsNormal)
463 tpl_line_em = '%s%%s%s %%s%s' % (Colors.linenoEm,Colors.line,
462 tpl_line_em = '%s%%s%s %%s%s' % (Colors.linenoEm,Colors.line,
464 ColorsNormal)
463 ColorsNormal)
465
464
466 # now, loop over all records printing context and info
465 # now, loop over all records printing context and info
467 abspath = os.path.abspath
466 abspath = os.path.abspath
468 for frame, file, lnum, func, lines, index in records:
467 for frame, file, lnum, func, lines, index in records:
469 #print '*** record:',file,lnum,func,lines,index # dbg
468 #print '*** record:',file,lnum,func,lines,index # dbg
470 try:
469 try:
471 file = file and abspath(file) or '?'
470 file = file and abspath(file) or '?'
472 except OSError:
471 except OSError:
473 # if file is '<console>' or something not in the filesystem,
472 # if file is '<console>' or something not in the filesystem,
474 # the abspath call will throw an OSError. Just ignore it and
473 # the abspath call will throw an OSError. Just ignore it and
475 # keep the original file string.
474 # keep the original file string.
476 pass
475 pass
477 link = tpl_link % file
476 link = tpl_link % file
478 try:
477 try:
479 args, varargs, varkw, locals = inspect.getargvalues(frame)
478 args, varargs, varkw, locals = inspect.getargvalues(frame)
480 except:
479 except:
481 # This can happen due to a bug in python2.3. We should be
480 # 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
481 # able to remove this try/except when 2.4 becomes a
483 # requirement. Bug details at http://python.org/sf/1005466
482 # requirement. Bug details at http://python.org/sf/1005466
484 inspect_error()
483 inspect_error()
485 traceback.print_exc(file=Term.cerr)
484 traceback.print_exc(file=Term.cerr)
486 info("\nIPython's exception reporting continues...\n")
485 info("\nIPython's exception reporting continues...\n")
487
486
488 if func == '?':
487 if func == '?':
489 call = ''
488 call = ''
490 else:
489 else:
491 # Decide whether to include variable details or not
490 # Decide whether to include variable details or not
492 var_repr = self.include_vars and eqrepr or nullrepr
491 var_repr = self.include_vars and eqrepr or nullrepr
493 try:
492 try:
494 call = tpl_call % (func,inspect.formatargvalues(args,
493 call = tpl_call % (func,inspect.formatargvalues(args,
495 varargs, varkw,
494 varargs, varkw,
496 locals,formatvalue=var_repr))
495 locals,formatvalue=var_repr))
497 except KeyError:
496 except KeyError:
498 # Very odd crash from inspect.formatargvalues(). The
497 # Very odd crash from inspect.formatargvalues(). The
499 # scenario under which it appeared was a call to
498 # scenario under which it appeared was a call to
500 # view(array,scale) in NumTut.view.view(), where scale had
499 # view(array,scale) in NumTut.view.view(), where scale had
501 # been defined as a scalar (it should be a tuple). Somehow
500 # been defined as a scalar (it should be a tuple). Somehow
502 # inspect messes up resolving the argument list of view()
501 # inspect messes up resolving the argument list of view()
503 # and barfs out. At some point I should dig into this one
502 # and barfs out. At some point I should dig into this one
504 # and file a bug report about it.
503 # and file a bug report about it.
505 inspect_error()
504 inspect_error()
506 traceback.print_exc(file=Term.cerr)
505 traceback.print_exc(file=Term.cerr)
507 info("\nIPython's exception reporting continues...\n")
506 info("\nIPython's exception reporting continues...\n")
508 call = tpl_call_fail % func
507 call = tpl_call_fail % func
509
508
510 # Initialize a list of names on the current line, which the
509 # Initialize a list of names on the current line, which the
511 # tokenizer below will populate.
510 # tokenizer below will populate.
512 names = []
511 names = []
513
512
514 def tokeneater(token_type, token, start, end, line):
513 def tokeneater(token_type, token, start, end, line):
515 """Stateful tokeneater which builds dotted names.
514 """Stateful tokeneater which builds dotted names.
516
515
517 The list of names it appends to (from the enclosing scope) can
516 The list of names it appends to (from the enclosing scope) can
518 contain repeated composite names. This is unavoidable, since
517 contain repeated composite names. This is unavoidable, since
519 there is no way to disambguate partial dotted structures until
518 there is no way to disambguate partial dotted structures until
520 the full list is known. The caller is responsible for pruning
519 the full list is known. The caller is responsible for pruning
521 the final list of duplicates before using it."""
520 the final list of duplicates before using it."""
522
521
523 # build composite names
522 # build composite names
524 if token == '.':
523 if token == '.':
525 try:
524 try:
526 names[-1] += '.'
525 names[-1] += '.'
527 # store state so the next token is added for x.y.z names
526 # store state so the next token is added for x.y.z names
528 tokeneater.name_cont = True
527 tokeneater.name_cont = True
529 return
528 return
530 except IndexError:
529 except IndexError:
531 pass
530 pass
532 if token_type == tokenize.NAME and token not in keyword.kwlist:
531 if token_type == tokenize.NAME and token not in keyword.kwlist:
533 if tokeneater.name_cont:
532 if tokeneater.name_cont:
534 # Dotted names
533 # Dotted names
535 names[-1] += token
534 names[-1] += token
536 tokeneater.name_cont = False
535 tokeneater.name_cont = False
537 else:
536 else:
538 # Regular new names. We append everything, the caller
537 # Regular new names. We append everything, the caller
539 # will be responsible for pruning the list later. It's
538 # will be responsible for pruning the list later. It's
540 # very tricky to try to prune as we go, b/c composite
539 # very tricky to try to prune as we go, b/c composite
541 # names can fool us. The pruning at the end is easy
540 # names can fool us. The pruning at the end is easy
542 # to do (or the caller can print a list with repeated
541 # to do (or the caller can print a list with repeated
543 # names if so desired.
542 # names if so desired.
544 names.append(token)
543 names.append(token)
545 elif token_type == tokenize.NEWLINE:
544 elif token_type == tokenize.NEWLINE:
546 raise IndexError
545 raise IndexError
547 # we need to store a bit of state in the tokenizer to build
546 # we need to store a bit of state in the tokenizer to build
548 # dotted names
547 # dotted names
549 tokeneater.name_cont = False
548 tokeneater.name_cont = False
550
549
551 def linereader(file=file, lnum=[lnum], getline=linecache.getline):
550 def linereader(file=file, lnum=[lnum], getline=linecache.getline):
552 line = getline(file, lnum[0])
551 line = getline(file, lnum[0])
553 lnum[0] += 1
552 lnum[0] += 1
554 return line
553 return line
555
554
556 # Build the list of names on this line of code where the exception
555 # Build the list of names on this line of code where the exception
557 # occurred.
556 # occurred.
558 try:
557 try:
559 # This builds the names list in-place by capturing it from the
558 # This builds the names list in-place by capturing it from the
560 # enclosing scope.
559 # enclosing scope.
561 tokenize.tokenize(linereader, tokeneater)
560 tokenize.tokenize(linereader, tokeneater)
562 except IndexError:
561 except IndexError:
563 # signals exit of tokenizer
562 # signals exit of tokenizer
564 pass
563 pass
565 except tokenize.TokenError,msg:
564 except tokenize.TokenError,msg:
566 _m = ("An unexpected error occurred while tokenizing input\n"
565 _m = ("An unexpected error occurred while tokenizing input\n"
567 "The following traceback may be corrupted or invalid\n"
566 "The following traceback may be corrupted or invalid\n"
568 "The error message is: %s\n" % msg)
567 "The error message is: %s\n" % msg)
569 error(_m)
568 error(_m)
570
569
571 # prune names list of duplicates, but keep the right order
570 # prune names list of duplicates, but keep the right order
572 unique_names = uniq_stable(names)
571 unique_names = uniq_stable(names)
573
572
574 # Start loop over vars
573 # Start loop over vars
575 lvals = []
574 lvals = []
576 if self.include_vars:
575 if self.include_vars:
577 for name_full in unique_names:
576 for name_full in unique_names:
578 name_base = name_full.split('.',1)[0]
577 name_base = name_full.split('.',1)[0]
579 if name_base in frame.f_code.co_varnames:
578 if name_base in frame.f_code.co_varnames:
580 if locals.has_key(name_base):
579 if locals.has_key(name_base):
581 try:
580 try:
582 value = repr(eval(name_full,locals))
581 value = repr(eval(name_full,locals))
583 except:
582 except:
584 value = undefined
583 value = undefined
585 else:
584 else:
586 value = undefined
585 value = undefined
587 name = tpl_local_var % name_full
586 name = tpl_local_var % name_full
588 else:
587 else:
589 if frame.f_globals.has_key(name_base):
588 if frame.f_globals.has_key(name_base):
590 try:
589 try:
591 value = repr(eval(name_full,frame.f_globals))
590 value = repr(eval(name_full,frame.f_globals))
592 except:
591 except:
593 value = undefined
592 value = undefined
594 else:
593 else:
595 value = undefined
594 value = undefined
596 name = tpl_global_var % name_full
595 name = tpl_global_var % name_full
597 lvals.append(tpl_name_val % (name,value))
596 lvals.append(tpl_name_val % (name,value))
598 if lvals:
597 if lvals:
599 lvals = '%s%s' % (indent,em_normal.join(lvals))
598 lvals = '%s%s' % (indent,em_normal.join(lvals))
600 else:
599 else:
601 lvals = ''
600 lvals = ''
602
601
603 level = '%s %s\n' % (link,call)
602 level = '%s %s\n' % (link,call)
604
603
605 if index is None:
604 if index is None:
606 frames.append(level)
605 frames.append(level)
607 else:
606 else:
608 frames.append('%s%s' % (level,''.join(
607 frames.append('%s%s' % (level,''.join(
609 _formatTracebackLines(lnum,index,lines,self.Colors,lvals))))
608 _formatTracebackLines(lnum,index,lines,self.Colors,lvals))))
610
609
611 # Get (safely) a string form of the exception info
610 # Get (safely) a string form of the exception info
612 try:
611 try:
613 etype_str,evalue_str = map(str,(etype,evalue))
612 etype_str,evalue_str = map(str,(etype,evalue))
614 except:
613 except:
615 # User exception is improperly defined.
614 # User exception is improperly defined.
616 etype,evalue = str,sys.exc_info()[:2]
615 etype,evalue = str,sys.exc_info()[:2]
617 etype_str,evalue_str = map(str,(etype,evalue))
616 etype_str,evalue_str = map(str,(etype,evalue))
618 # ... and format it
617 # ... and format it
619 exception = ['%s%s%s: %s' % (Colors.excName, etype_str,
618 exception = ['%s%s%s: %s' % (Colors.excName, etype_str,
620 ColorsNormal, evalue_str)]
619 ColorsNormal, evalue_str)]
621 if type(evalue) is types.InstanceType:
620 if type(evalue) is types.InstanceType:
622 try:
621 try:
623 names = [w for w in dir(evalue) if isinstance(w, basestring)]
622 names = [w for w in dir(evalue) if isinstance(w, basestring)]
624 except:
623 except:
625 # Every now and then, an object with funny inernals blows up
624 # Every now and then, an object with funny inernals blows up
626 # when dir() is called on it. We do the best we can to report
625 # when dir() is called on it. We do the best we can to report
627 # the problem and continue
626 # the problem and continue
628 _m = '%sException reporting error (object with broken dir())%s:'
627 _m = '%sException reporting error (object with broken dir())%s:'
629 exception.append(_m % (Colors.excName,ColorsNormal))
628 exception.append(_m % (Colors.excName,ColorsNormal))
630 etype_str,evalue_str = map(str,sys.exc_info()[:2])
629 etype_str,evalue_str = map(str,sys.exc_info()[:2])
631 exception.append('%s%s%s: %s' % (Colors.excName,etype_str,
630 exception.append('%s%s%s: %s' % (Colors.excName,etype_str,
632 ColorsNormal, evalue_str))
631 ColorsNormal, evalue_str))
633 names = []
632 names = []
634 for name in names:
633 for name in names:
635 value = text_repr(getattr(evalue, name))
634 value = text_repr(getattr(evalue, name))
636 exception.append('\n%s%s = %s' % (indent, name, value))
635 exception.append('\n%s%s = %s' % (indent, name, value))
637 # return all our info assembled as a single string
636 # return all our info assembled as a single string
638 return '%s\n\n%s\n%s' % (head,'\n'.join(frames),''.join(exception[0]) )
637 return '%s\n\n%s\n%s' % (head,'\n'.join(frames),''.join(exception[0]) )
639
638
640 def debugger(self):
639 def debugger(self):
641 """Call up the pdb debugger if desired, always clean up the tb reference.
640 """Call up the pdb debugger if desired, always clean up the tb reference.
642
641
643 If the call_pdb flag is set, the pdb interactive debugger is
642 If the call_pdb flag is set, the pdb interactive debugger is
644 invoked. In all cases, the self.tb reference to the current traceback
643 invoked. In all cases, the self.tb reference to the current traceback
645 is deleted to prevent lingering references which hamper memory
644 is deleted to prevent lingering references which hamper memory
646 management.
645 management.
647
646
648 Note that each call to pdb() does an 'import readline', so if your app
647 Note that each call to pdb() does an 'import readline', so if your app
649 requires a special setup for the readline completers, you'll have to
648 requires a special setup for the readline completers, you'll have to
650 fix that by hand after invoking the exception handler."""
649 fix that by hand after invoking the exception handler."""
651
650
652 if self.call_pdb:
651 if self.call_pdb:
653 if self.pdb is None:
652 if self.pdb is None:
654 self.pdb = Debugger.Pdb(
653 self.pdb = Debugger.Pdb(
655 self.color_scheme_table.active_scheme_name)
654 self.color_scheme_table.active_scheme_name)
656 # the system displayhook may have changed, restore the original
655 # the system displayhook may have changed, restore the original
657 # for pdb
656 # for pdb
658 dhook = sys.displayhook
657 dhook = sys.displayhook
659 sys.displayhook = sys.__displayhook__
658 sys.displayhook = sys.__displayhook__
660 self.pdb.reset()
659 self.pdb.reset()
661 # Find the right frame so we don't pop up inside ipython itself
660 # Find the right frame so we don't pop up inside ipython itself
662 etb = self.tb
661 etb = self.tb
663 while self.tb.tb_next is not None:
662 while self.tb.tb_next is not None:
664 self.tb = self.tb.tb_next
663 self.tb = self.tb.tb_next
665 try:
664 try:
666 if etb and etb.tb_next:
665 if etb and etb.tb_next:
667 etb = etb.tb_next
666 etb = etb.tb_next
668 self.pdb.botframe = etb.tb_frame
667 self.pdb.botframe = etb.tb_frame
669 self.pdb.interaction(self.tb.tb_frame, self.tb)
668 self.pdb.interaction(self.tb.tb_frame, self.tb)
670 except:
669 except:
671 print '*** ERROR ***'
670 print '*** ERROR ***'
672 print 'This version of pdb has a bug and crashed.'
671 print 'This version of pdb has a bug and crashed.'
673 print 'Returning to IPython...'
672 print 'Returning to IPython...'
674 sys.displayhook = dhook
673 sys.displayhook = dhook
675 del self.tb
674 del self.tb
676
675
677 def handler(self, info=None):
676 def handler(self, info=None):
678 (etype, evalue, etb) = info or sys.exc_info()
677 (etype, evalue, etb) = info or sys.exc_info()
679 self.tb = etb
678 self.tb = etb
680 print >> Term.cerr, self.text(etype, evalue, etb)
679 print >> Term.cerr, self.text(etype, evalue, etb)
681
680
682 # Changed so an instance can just be called as VerboseTB_inst() and print
681 # Changed so an instance can just be called as VerboseTB_inst() and print
683 # out the right info on its own.
682 # out the right info on its own.
684 def __call__(self, etype=None, evalue=None, etb=None):
683 def __call__(self, etype=None, evalue=None, etb=None):
685 """This hook can replace sys.excepthook (for Python 2.1 or higher)."""
684 """This hook can replace sys.excepthook (for Python 2.1 or higher)."""
686 if etb is None:
685 if etb is None:
687 self.handler()
686 self.handler()
688 else:
687 else:
689 self.handler((etype, evalue, etb))
688 self.handler((etype, evalue, etb))
690 self.debugger()
689 self.debugger()
691
690
692 #----------------------------------------------------------------------------
691 #----------------------------------------------------------------------------
693 class FormattedTB(VerboseTB,ListTB):
692 class FormattedTB(VerboseTB,ListTB):
694 """Subclass ListTB but allow calling with a traceback.
693 """Subclass ListTB but allow calling with a traceback.
695
694
696 It can thus be used as a sys.excepthook for Python > 2.1.
695 It can thus be used as a sys.excepthook for Python > 2.1.
697
696
698 Also adds 'Context' and 'Verbose' modes, not available in ListTB.
697 Also adds 'Context' and 'Verbose' modes, not available in ListTB.
699
698
700 Allows a tb_offset to be specified. This is useful for situations where
699 Allows a tb_offset to be specified. This is useful for situations where
701 one needs to remove a number of topmost frames from the traceback (such as
700 one needs to remove a number of topmost frames from the traceback (such as
702 occurs with python programs that themselves execute other python code,
701 occurs with python programs that themselves execute other python code,
703 like Python shells). """
702 like Python shells). """
704
703
705 def __init__(self, mode = 'Plain', color_scheme='Linux',
704 def __init__(self, mode = 'Plain', color_scheme='Linux',
706 tb_offset = 0,long_header=0,call_pdb=0,include_vars=0):
705 tb_offset = 0,long_header=0,call_pdb=0,include_vars=0):
707
706
708 # NEVER change the order of this list. Put new modes at the end:
707 # NEVER change the order of this list. Put new modes at the end:
709 self.valid_modes = ['Plain','Context','Verbose']
708 self.valid_modes = ['Plain','Context','Verbose']
710 self.verbose_modes = self.valid_modes[1:3]
709 self.verbose_modes = self.valid_modes[1:3]
711
710
712 VerboseTB.__init__(self,color_scheme,tb_offset,long_header,
711 VerboseTB.__init__(self,color_scheme,tb_offset,long_header,
713 call_pdb=call_pdb,include_vars=include_vars)
712 call_pdb=call_pdb,include_vars=include_vars)
714 self.set_mode(mode)
713 self.set_mode(mode)
715
714
716 def _extract_tb(self,tb):
715 def _extract_tb(self,tb):
717 if tb:
716 if tb:
718 return traceback.extract_tb(tb)
717 return traceback.extract_tb(tb)
719 else:
718 else:
720 return None
719 return None
721
720
722 def text(self, etype, value, tb,context=5,mode=None):
721 def text(self, etype, value, tb,context=5,mode=None):
723 """Return formatted traceback.
722 """Return formatted traceback.
724
723
725 If the optional mode parameter is given, it overrides the current
724 If the optional mode parameter is given, it overrides the current
726 mode."""
725 mode."""
727
726
728 if mode is None:
727 if mode is None:
729 mode = self.mode
728 mode = self.mode
730 if mode in self.verbose_modes:
729 if mode in self.verbose_modes:
731 # verbose modes need a full traceback
730 # verbose modes need a full traceback
732 return VerboseTB.text(self,etype, value, tb,context=5)
731 return VerboseTB.text(self,etype, value, tb,context=5)
733 else:
732 else:
734 # We must check the source cache because otherwise we can print
733 # We must check the source cache because otherwise we can print
735 # out-of-date source code.
734 # out-of-date source code.
736 linecache.checkcache()
735 linecache.checkcache()
737 # Now we can extract and format the exception
736 # Now we can extract and format the exception
738 elist = self._extract_tb(tb)
737 elist = self._extract_tb(tb)
739 if len(elist) > self.tb_offset:
738 if len(elist) > self.tb_offset:
740 del elist[:self.tb_offset]
739 del elist[:self.tb_offset]
741 return ListTB.text(self,etype,value,elist)
740 return ListTB.text(self,etype,value,elist)
742
741
743 def set_mode(self,mode=None):
742 def set_mode(self,mode=None):
744 """Switch to the desired mode.
743 """Switch to the desired mode.
745
744
746 If mode is not specified, cycles through the available modes."""
745 If mode is not specified, cycles through the available modes."""
747
746
748 if not mode:
747 if not mode:
749 new_idx = ( self.valid_modes.index(self.mode) + 1 ) % \
748 new_idx = ( self.valid_modes.index(self.mode) + 1 ) % \
750 len(self.valid_modes)
749 len(self.valid_modes)
751 self.mode = self.valid_modes[new_idx]
750 self.mode = self.valid_modes[new_idx]
752 elif mode not in self.valid_modes:
751 elif mode not in self.valid_modes:
753 raise ValueError, 'Unrecognized mode in FormattedTB: <'+mode+'>\n'\
752 raise ValueError, 'Unrecognized mode in FormattedTB: <'+mode+'>\n'\
754 'Valid modes: '+str(self.valid_modes)
753 'Valid modes: '+str(self.valid_modes)
755 else:
754 else:
756 self.mode = mode
755 self.mode = mode
757 # include variable details only in 'Verbose' mode
756 # include variable details only in 'Verbose' mode
758 self.include_vars = (self.mode == self.valid_modes[2])
757 self.include_vars = (self.mode == self.valid_modes[2])
759
758
760 # some convenient shorcuts
759 # some convenient shorcuts
761 def plain(self):
760 def plain(self):
762 self.set_mode(self.valid_modes[0])
761 self.set_mode(self.valid_modes[0])
763
762
764 def context(self):
763 def context(self):
765 self.set_mode(self.valid_modes[1])
764 self.set_mode(self.valid_modes[1])
766
765
767 def verbose(self):
766 def verbose(self):
768 self.set_mode(self.valid_modes[2])
767 self.set_mode(self.valid_modes[2])
769
768
770 #----------------------------------------------------------------------------
769 #----------------------------------------------------------------------------
771 class AutoFormattedTB(FormattedTB):
770 class AutoFormattedTB(FormattedTB):
772 """A traceback printer which can be called on the fly.
771 """A traceback printer which can be called on the fly.
773
772
774 It will find out about exceptions by itself.
773 It will find out about exceptions by itself.
775
774
776 A brief example:
775 A brief example:
777
776
778 AutoTB = AutoFormattedTB(mode = 'Verbose',color_scheme='Linux')
777 AutoTB = AutoFormattedTB(mode = 'Verbose',color_scheme='Linux')
779 try:
778 try:
780 ...
779 ...
781 except:
780 except:
782 AutoTB() # or AutoTB(out=logfile) where logfile is an open file object
781 AutoTB() # or AutoTB(out=logfile) where logfile is an open file object
783 """
782 """
784 def __call__(self,etype=None,evalue=None,etb=None,
783 def __call__(self,etype=None,evalue=None,etb=None,
785 out=None,tb_offset=None):
784 out=None,tb_offset=None):
786 """Print out a formatted exception traceback.
785 """Print out a formatted exception traceback.
787
786
788 Optional arguments:
787 Optional arguments:
789 - out: an open file-like object to direct output to.
788 - out: an open file-like object to direct output to.
790
789
791 - tb_offset: the number of frames to skip over in the stack, on a
790 - tb_offset: the number of frames to skip over in the stack, on a
792 per-call basis (this overrides temporarily the instance's tb_offset
791 per-call basis (this overrides temporarily the instance's tb_offset
793 given at initialization time. """
792 given at initialization time. """
794
793
795 if out is None:
794 if out is None:
796 out = Term.cerr
795 out = Term.cerr
797 if tb_offset is not None:
796 if tb_offset is not None:
798 tb_offset, self.tb_offset = self.tb_offset, tb_offset
797 tb_offset, self.tb_offset = self.tb_offset, tb_offset
799 print >> out, self.text(etype, evalue, etb)
798 print >> out, self.text(etype, evalue, etb)
800 self.tb_offset = tb_offset
799 self.tb_offset = tb_offset
801 else:
800 else:
802 print >> out, self.text(etype, evalue, etb)
801 print >> out, self.text(etype, evalue, etb)
803 self.debugger()
802 self.debugger()
804
803
805 def text(self,etype=None,value=None,tb=None,context=5,mode=None):
804 def text(self,etype=None,value=None,tb=None,context=5,mode=None):
806 if etype is None:
805 if etype is None:
807 etype,value,tb = sys.exc_info()
806 etype,value,tb = sys.exc_info()
808 self.tb = tb
807 self.tb = tb
809 return FormattedTB.text(self,etype,value,tb,context=5,mode=mode)
808 return FormattedTB.text(self,etype,value,tb,context=5,mode=mode)
810
809
811 #---------------------------------------------------------------------------
810 #---------------------------------------------------------------------------
812 # A simple class to preserve Nathan's original functionality.
811 # A simple class to preserve Nathan's original functionality.
813 class ColorTB(FormattedTB):
812 class ColorTB(FormattedTB):
814 """Shorthand to initialize a FormattedTB in Linux colors mode."""
813 """Shorthand to initialize a FormattedTB in Linux colors mode."""
815 def __init__(self,color_scheme='Linux',call_pdb=0):
814 def __init__(self,color_scheme='Linux',call_pdb=0):
816 FormattedTB.__init__(self,color_scheme=color_scheme,
815 FormattedTB.__init__(self,color_scheme=color_scheme,
817 call_pdb=call_pdb)
816 call_pdb=call_pdb)
818
817
819 #----------------------------------------------------------------------------
818 #----------------------------------------------------------------------------
820 # module testing (minimal)
819 # module testing (minimal)
821 if __name__ == "__main__":
820 if __name__ == "__main__":
822 def spam(c, (d, e)):
821 def spam(c, (d, e)):
823 x = c + d
822 x = c + d
824 y = c * d
823 y = c * d
825 foo(x, y)
824 foo(x, y)
826
825
827 def foo(a, b, bar=1):
826 def foo(a, b, bar=1):
828 eggs(a, b + bar)
827 eggs(a, b + bar)
829
828
830 def eggs(f, g, z=globals()):
829 def eggs(f, g, z=globals()):
831 h = f + g
830 h = f + g
832 i = f - g
831 i = f - g
833 return h / i
832 return h / i
834
833
835 print ''
834 print ''
836 print '*** Before ***'
835 print '*** Before ***'
837 try:
836 try:
838 print spam(1, (2, 3))
837 print spam(1, (2, 3))
839 except:
838 except:
840 traceback.print_exc()
839 traceback.print_exc()
841 print ''
840 print ''
842
841
843 handler = ColorTB()
842 handler = ColorTB()
844 print '*** ColorTB ***'
843 print '*** ColorTB ***'
845 try:
844 try:
846 print spam(1, (2, 3))
845 print spam(1, (2, 3))
847 except:
846 except:
848 apply(handler, sys.exc_info() )
847 apply(handler, sys.exc_info() )
849 print ''
848 print ''
850
849
851 handler = VerboseTB()
850 handler = VerboseTB()
852 print '*** VerboseTB ***'
851 print '*** VerboseTB ***'
853 try:
852 try:
854 print spam(1, (2, 3))
853 print spam(1, (2, 3))
855 except:
854 except:
856 apply(handler, sys.exc_info() )
855 apply(handler, sys.exc_info() )
857 print ''
856 print ''
858
857
@@ -1,43 +1,46 b''
1 """Set of functions to work with console on Windows.
1 """
2 """
2 Set of functions to work with console on Windows.
3
3
4 Author: Alexander Belchenko (e-mail: bialix AT ukr.net)
4 #*****************************************************************************
5 License: Public domain
5 # Copyright (C) 2005 Alexander Belchenko <bialix@ukr.net>
6 """
6 #
7 # This file is placed in the public domain.
8 #
9 #*****************************************************************************
7
10
8 __author__ = 'Alexander Belchenko (e-mail: bialix AT ukr.net)'
11 __author__ = 'Alexander Belchenko (e-mail: bialix AT ukr.net)'
9 __license__ = 'Public domain'
12 __license__ = 'Public domain'
10
13
11 import struct
14 import struct
12
15
13 try:
16 try:
14 import ctypes
17 import ctypes
15 except ImportError:
18 except ImportError:
16 ctypes = None
19 ctypes = None
17
20
18 def get_console_size(defaultx=80, defaulty=25):
21 def get_console_size(defaultx=80, defaulty=25):
19 """ Return size of current console.
22 """ Return size of current console.
20
23
21 This function try to determine actual size of current working
24 This function try to determine actual size of current working
22 console window and return tuple (sizex, sizey) if success,
25 console window and return tuple (sizex, sizey) if success,
23 or default size (defaultx, defaulty) otherwise.
26 or default size (defaultx, defaulty) otherwise.
24
27
25 Dependencies: ctypes should be installed.
28 Dependencies: ctypes should be installed.
26 """
29 """
27 if ctypes is None:
30 if ctypes is None:
28 # no ctypes is found
31 # no ctypes is found
29 return (defaultx, defaulty)
32 return (defaultx, defaulty)
30
33
31 h = ctypes.windll.kernel32.GetStdHandle(-11)
34 h = ctypes.windll.kernel32.GetStdHandle(-11)
32 csbi = ctypes.create_string_buffer(22)
35 csbi = ctypes.create_string_buffer(22)
33 res = ctypes.windll.kernel32.GetConsoleScreenBufferInfo(h, csbi)
36 res = ctypes.windll.kernel32.GetConsoleScreenBufferInfo(h, csbi)
34
37
35 if res:
38 if res:
36 (bufx, bufy, curx, cury, wattr,
39 (bufx, bufy, curx, cury, wattr,
37 left, top, right, bottom, maxx, maxy) = struct.unpack("hhhhHhhhhhh",
40 left, top, right, bottom, maxx, maxy) = struct.unpack("hhhhHhhhhhh",
38 csbi.raw)
41 csbi.raw)
39 sizex = right - left + 1
42 sizex = right - left + 1
40 sizey = bottom - top + 1
43 sizey = bottom - top + 1
41 return (sizex, sizey)
44 return (sizex, sizey)
42 else:
45 else:
43 return (defaultx, defaulty)
46 return (defaultx, defaulty)
1 NO CONTENT: modified file
NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
General Comments 0
You need to be logged in to leave comments. Login now