Show More
The requested changes are too big and content was truncated. Show full diff
@@ -0,0 +1,109 b'' | |||
|
1 | # -*- coding: utf-8 -*- | |
|
2 | """ | |
|
3 | Color schemes for exception handling code in IPython. | |
|
4 | ||
|
5 | $Id: Prompts.py 638 2005-07-18 03:01:41Z fperez $""" | |
|
6 | ||
|
7 | #***************************************************************************** | |
|
8 | # Copyright (C) 2005 Fernando Perez <fperez@colorado.edu> | |
|
9 | # | |
|
10 | # Distributed under the terms of the BSD License. The full license is in | |
|
11 | # the file COPYING, distributed as part of this software. | |
|
12 | #***************************************************************************** | |
|
13 | ||
|
14 | from IPython import Release | |
|
15 | __author__ = '%s <%s>' % Release.authors['Fernando'] | |
|
16 | __license__ = Release.license | |
|
17 | __version__ = Release.version | |
|
18 | ||
|
19 | #**************************************************************************** | |
|
20 | # Required modules | |
|
21 | from IPython.ColorANSI import ColorSchemeTable, TermColors, ColorScheme | |
|
22 | ||
|
23 | ExceptionColors = ColorSchemeTable() | |
|
24 | ||
|
25 | # Populate it with color schemes | |
|
26 | C = TermColors # shorthand and local lookup | |
|
27 | ExceptionColors.add_scheme(ColorScheme( | |
|
28 | 'NoColor', | |
|
29 | # The color to be used for the top line | |
|
30 | topline = C.NoColor, | |
|
31 | ||
|
32 | # The colors to be used in the traceback | |
|
33 | filename = C.NoColor, | |
|
34 | lineno = C.NoColor, | |
|
35 | name = C.NoColor, | |
|
36 | vName = C.NoColor, | |
|
37 | val = C.NoColor, | |
|
38 | em = C.NoColor, | |
|
39 | ||
|
40 | # Emphasized colors for the last frame of the traceback | |
|
41 | normalEm = C.NoColor, | |
|
42 | filenameEm = C.NoColor, | |
|
43 | linenoEm = C.NoColor, | |
|
44 | nameEm = C.NoColor, | |
|
45 | valEm = C.NoColor, | |
|
46 | ||
|
47 | # Colors for printing the exception | |
|
48 | excName = C.NoColor, | |
|
49 | line = C.NoColor, | |
|
50 | caret = C.NoColor, | |
|
51 | Normal = C.NoColor | |
|
52 | )) | |
|
53 | ||
|
54 | # make some schemes as instances so we can copy them for modification easily | |
|
55 | ExceptionColors.add_scheme(ColorScheme( | |
|
56 | 'Linux', | |
|
57 | # The color to be used for the top line | |
|
58 | topline = C.LightRed, | |
|
59 | ||
|
60 | # The colors to be used in the traceback | |
|
61 | filename = C.Green, | |
|
62 | lineno = C.Green, | |
|
63 | name = C.Purple, | |
|
64 | vName = C.Cyan, | |
|
65 | val = C.Green, | |
|
66 | em = C.LightCyan, | |
|
67 | ||
|
68 | # Emphasized colors for the last frame of the traceback | |
|
69 | normalEm = C.LightCyan, | |
|
70 | filenameEm = C.LightGreen, | |
|
71 | linenoEm = C.LightGreen, | |
|
72 | nameEm = C.LightPurple, | |
|
73 | valEm = C.LightBlue, | |
|
74 | ||
|
75 | # Colors for printing the exception | |
|
76 | excName = C.LightRed, | |
|
77 | line = C.Yellow, | |
|
78 | caret = C.White, | |
|
79 | Normal = C.Normal | |
|
80 | )) | |
|
81 | ||
|
82 | # For light backgrounds, swap dark/light colors | |
|
83 | ExceptionColors.add_scheme(ColorScheme( | |
|
84 | 'LightBG', | |
|
85 | # The color to be used for the top line | |
|
86 | topline = C.Red, | |
|
87 | ||
|
88 | # The colors to be used in the traceback | |
|
89 | filename = C.LightGreen, | |
|
90 | lineno = C.LightGreen, | |
|
91 | name = C.LightPurple, | |
|
92 | vName = C.Cyan, | |
|
93 | val = C.LightGreen, | |
|
94 | em = C.Cyan, | |
|
95 | ||
|
96 | # Emphasized colors for the last frame of the traceback | |
|
97 | normalEm = C.Cyan, | |
|
98 | filenameEm = C.Green, | |
|
99 | linenoEm = C.Green, | |
|
100 | nameEm = C.Purple, | |
|
101 | valEm = C.Blue, | |
|
102 | ||
|
103 | # Colors for printing the exception | |
|
104 | excName = C.Red, | |
|
105 | #line = C.Brown, # brown often is displayed as yellow | |
|
106 | line = C.Red, | |
|
107 | caret = C.Normal, | |
|
108 | Normal = C.Normal | |
|
109 | )) |
@@ -1,155 +1,164 b'' | |||
|
1 | 1 | # -*- coding: utf-8 -*- |
|
2 | 2 | """Tools for coloring text in ANSI terminals. |
|
3 | 3 | |
|
4 |
$Id: ColorANSI.py |
|
|
4 | $Id: ColorANSI.py 951 2005-12-25 00:57:24Z fperez $""" | |
|
5 | 5 | |
|
6 | 6 | #***************************************************************************** |
|
7 | 7 | # Copyright (C) 2002-2004 Fernando Perez. <fperez@colorado.edu> |
|
8 | 8 | # |
|
9 | 9 | # Distributed under the terms of the BSD License. The full license is in |
|
10 | 10 | # the file COPYING, distributed as part of this software. |
|
11 | 11 | #***************************************************************************** |
|
12 | 12 | |
|
13 | 13 | from IPython import Release |
|
14 | 14 | __author__ = '%s <%s>' % Release.authors['Fernando'] |
|
15 | 15 | __license__ = Release.license |
|
16 | 16 | |
|
17 | 17 | __all__ = ['TermColors','InputTermColors','ColorScheme','ColorSchemeTable'] |
|
18 | 18 | |
|
19 | 19 | import os |
|
20 | from UserDict import UserDict | |
|
21 | 20 | |
|
22 | 21 | from IPython.Struct import Struct |
|
23 | 22 | |
|
24 | 23 | def make_color_table(in_class): |
|
25 | 24 | """Build a set of color attributes in a class. |
|
26 | 25 | |
|
27 | 26 | Helper function for building the *TermColors classes.""" |
|
28 | 27 | |
|
29 | 28 | color_templates = ( |
|
30 | 29 | ("Black" , "0;30"), |
|
31 | 30 | ("Red" , "0;31"), |
|
32 | 31 | ("Green" , "0;32"), |
|
33 | 32 | ("Brown" , "0;33"), |
|
34 | 33 | ("Blue" , "0;34"), |
|
35 | 34 | ("Purple" , "0;35"), |
|
36 | 35 | ("Cyan" , "0;36"), |
|
37 | 36 | ("LightGray" , "0;37"), |
|
38 | 37 | ("DarkGray" , "1;30"), |
|
39 | 38 | ("LightRed" , "1;31"), |
|
40 | 39 | ("LightGreen" , "1;32"), |
|
41 | 40 | ("Yellow" , "1;33"), |
|
42 | 41 | ("LightBlue" , "1;34"), |
|
43 | 42 | ("LightPurple" , "1;35"), |
|
44 | 43 | ("LightCyan" , "1;36"), |
|
45 | 44 | ("White" , "1;37"), ) |
|
46 | 45 | |
|
47 | 46 | for name,value in color_templates: |
|
48 | 47 | setattr(in_class,name,in_class._base % value) |
|
49 | 48 | |
|
50 | 49 | class TermColors: |
|
51 | 50 | """Color escape sequences. |
|
52 | 51 | |
|
53 | 52 | This class defines the escape sequences for all the standard (ANSI?) |
|
54 | 53 | colors in terminals. Also defines a NoColor escape which is just the null |
|
55 | 54 | string, suitable for defining 'dummy' color schemes in terminals which get |
|
56 | 55 | confused by color escapes. |
|
57 | 56 | |
|
58 | 57 | This class should be used as a mixin for building color schemes.""" |
|
59 | 58 | |
|
60 | 59 | NoColor = '' # for color schemes in color-less terminals. |
|
61 | 60 | Normal = '\033[0m' # Reset normal coloring |
|
62 | 61 | _base = '\033[%sm' # Template for all other colors |
|
63 | 62 | |
|
64 | 63 | # Build the actual color table as a set of class attributes: |
|
65 | 64 | make_color_table(TermColors) |
|
66 | 65 | |
|
67 | 66 | class InputTermColors: |
|
68 | 67 | """Color escape sequences for input prompts. |
|
69 | 68 | |
|
70 | 69 | This class is similar to TermColors, but the escapes are wrapped in \001 |
|
71 | 70 | and \002 so that readline can properly know the length of each line and |
|
72 | 71 | can wrap lines accordingly. Use this class for any colored text which |
|
73 | 72 | needs to be used in input prompts, such as in calls to raw_input(). |
|
74 | 73 | |
|
75 | 74 | This class defines the escape sequences for all the standard (ANSI?) |
|
76 | 75 | colors in terminals. Also defines a NoColor escape which is just the null |
|
77 | 76 | string, suitable for defining 'dummy' color schemes in terminals which get |
|
78 | 77 | confused by color escapes. |
|
79 | 78 | |
|
80 | 79 | This class should be used as a mixin for building color schemes.""" |
|
81 | 80 | |
|
82 | 81 | NoColor = '' # for color schemes in color-less terminals. |
|
83 | 82 | Normal = '\001\033[0m\002' # Reset normal coloring |
|
84 | 83 | _base = '\001\033[%sm\002' # Template for all other colors |
|
85 | 84 | |
|
86 | 85 | # Build the actual color table as a set of class attributes: |
|
87 | 86 | make_color_table(InputTermColors) |
|
88 | 87 | |
|
89 | 88 | class ColorScheme: |
|
90 | 89 | """Generic color scheme class. Just a name and a Struct.""" |
|
91 | 90 | def __init__(self,__scheme_name_,colordict=None,**colormap): |
|
92 | 91 | self.name = __scheme_name_ |
|
93 | 92 | if colordict is None: |
|
94 | 93 | self.colors = Struct(**colormap) |
|
95 | 94 | else: |
|
96 | 95 | self.colors = Struct(colordict) |
|
96 | ||
|
97 | def copy(self,name=None): | |
|
98 | """Return a full copy of the object, optionally renaming it.""" | |
|
99 | if name is None: | |
|
100 | name = self.name | |
|
101 | return ColorScheme(name,self.colors.__dict__) | |
|
97 | 102 | |
|
98 |
class ColorSchemeTable( |
|
|
103 | class ColorSchemeTable(dict): | |
|
99 | 104 | """General class to handle tables of color schemes. |
|
100 | 105 | |
|
101 | 106 | It's basically a dict of color schemes with a couple of shorthand |
|
102 | 107 | attributes and some convenient methods. |
|
103 | 108 | |
|
104 | 109 | active_scheme_name -> obvious |
|
105 | 110 | active_colors -> actual color table of the active scheme""" |
|
106 | 111 | |
|
107 | 112 | def __init__(self,scheme_list=None,default_scheme=''): |
|
108 | 113 | """Create a table of color schemes. |
|
109 | 114 | |
|
110 | 115 | The table can be created empty and manually filled or it can be |
|
111 | 116 | created with a list of valid color schemes AND the specification for |
|
112 | 117 | the default active scheme. |
|
113 | 118 | """ |
|
114 | 119 | |
|
115 | UserDict.__init__(self) | |
|
116 | if scheme_list is None: | |
|
117 |
|
|
|
118 | self.active_colors = None | |
|
119 |
|
|
|
120 | # create object attributes to be set later | |
|
121 | self.active_scheme_name = '' | |
|
122 | self.active_colors = None | |
|
123 | ||
|
124 | if scheme_list: | |
|
120 | 125 | if default_scheme == '': |
|
121 | 126 | raise ValueError,'you must specify the default color scheme' |
|
122 | 127 | for scheme in scheme_list: |
|
123 | 128 | self.add_scheme(scheme) |
|
124 | 129 | self.set_active_scheme(default_scheme) |
|
125 | 130 | |
|
131 | def copy(self): | |
|
132 | """Return full copy of object""" | |
|
133 | return ColorSchemeTable(self.values(),self.active_scheme_name) | |
|
134 | ||
|
126 | 135 | def add_scheme(self,new_scheme): |
|
127 | 136 | """Add a new color scheme to the table.""" |
|
128 | 137 | if not isinstance(new_scheme,ColorScheme): |
|
129 | 138 | raise ValueError,'ColorSchemeTable only accepts ColorScheme instances' |
|
130 | 139 | self[new_scheme.name] = new_scheme |
|
131 | 140 | |
|
132 | 141 | def set_active_scheme(self,scheme,case_sensitive=0): |
|
133 | 142 | """Set the currently active scheme. |
|
134 | 143 | |
|
135 | 144 | Names are by default compared in a case-insensitive way, but this can |
|
136 | 145 | be changed by setting the parameter case_sensitive to true.""" |
|
137 | 146 | |
|
138 |
scheme_ |
|
|
147 | scheme_names = self.keys() | |
|
139 | 148 | if case_sensitive: |
|
140 |
valid_schemes = scheme_ |
|
|
149 | valid_schemes = scheme_names | |
|
141 | 150 | scheme_test = scheme |
|
142 | 151 | else: |
|
143 |
valid_schemes = [s.lower() for s in scheme_ |
|
|
152 | valid_schemes = [s.lower() for s in scheme_names] | |
|
144 | 153 | scheme_test = scheme.lower() |
|
145 | 154 | try: |
|
146 | 155 | scheme_idx = valid_schemes.index(scheme_test) |
|
147 | 156 | except ValueError: |
|
148 | 157 | raise ValueError,'Unrecognized color scheme: ' + scheme + \ |
|
149 |
'\nValid schemes: '+str(scheme_ |
|
|
158 | '\nValid schemes: '+str(scheme_names).replace("'', ",'') | |
|
150 | 159 | else: |
|
151 |
active = scheme_ |
|
|
160 | active = scheme_names[scheme_idx] | |
|
152 | 161 | self.active_scheme_name = active |
|
153 | 162 | self.active_colors = self[active].colors |
|
154 | 163 | # Now allow using '' as an index for the current active scheme |
|
155 | 164 | self[''] = self[active] |
@@ -1,110 +1,110 b'' | |||
|
1 | 1 | # -*- coding: utf-8 -*- |
|
2 | 2 | """sys.excepthook for IPython itself, leaves a detailed report on disk. |
|
3 | 3 | |
|
4 |
$Id: CrashHandler.py |
|
|
4 | $Id: CrashHandler.py 951 2005-12-25 00:57:24Z fperez $""" | |
|
5 | 5 | |
|
6 | 6 | #***************************************************************************** |
|
7 | 7 | # Copyright (C) 2001-2004 Fernando Perez. <fperez@colorado.edu> |
|
8 | 8 | # |
|
9 | 9 | # Distributed under the terms of the BSD License. The full license is in |
|
10 | 10 | # the file COPYING, distributed as part of this software. |
|
11 | 11 | #***************************************************************************** |
|
12 | 12 | |
|
13 | 13 | from IPython import Release |
|
14 | 14 | __author__ = '%s <%s>' % Release.authors['Fernando'] |
|
15 | 15 | __license__ = Release.license |
|
16 | 16 | __version__ = Release.version |
|
17 | 17 | |
|
18 | 18 | #**************************************************************************** |
|
19 | 19 | # Required modules |
|
20 | 20 | |
|
21 | 21 | # From the standard library |
|
22 | 22 | import os,sys |
|
23 | 23 | from pprint import pprint,pformat |
|
24 | 24 | |
|
25 | 25 | # Homebrewed |
|
26 | from IPython.genutils import * | |
|
27 | 26 | from IPython.Itpl import Itpl,itpl,printpl |
|
27 | from IPython.ColorANSI import ColorScheme,ColorSchemeTable # too long names | |
|
28 | 28 | from IPython import ultraTB |
|
29 | from IPython.ultraTB import ColorScheme,ColorSchemeTable # too long names | |
|
29 | from IPython.genutils import * | |
|
30 | 30 | |
|
31 | 31 | #**************************************************************************** |
|
32 | 32 | class CrashHandler: |
|
33 | 33 | """sys.excepthook for IPython itself, leaves a detailed report on disk.""" |
|
34 | 34 | |
|
35 | 35 | def __init__(self,IP): |
|
36 | 36 | self.IP = IP # IPython instance |
|
37 | 37 | self.bug_contact = Release.authors['Fernando'][0] |
|
38 | 38 | self.mailto = Release.authors['Fernando'][1] |
|
39 | 39 | |
|
40 | 40 | def __call__(self,etype, evalue, etb): |
|
41 | 41 | |
|
42 | 42 | # Report tracebacks shouldn't use color in general (safer for users) |
|
43 | 43 | color_scheme = 'NoColor' |
|
44 | 44 | |
|
45 | 45 | # Use this ONLY for developer debugging (keep commented out for release) |
|
46 | 46 | #color_scheme = 'Linux' # dbg |
|
47 | 47 | |
|
48 | 48 | try: |
|
49 | 49 | rptdir = self.IP.rc.ipythondir |
|
50 | 50 | except: |
|
51 | 51 | rptdir = os.getcwd() |
|
52 | 52 | if not os.path.isdir(rptdir): |
|
53 | 53 | rptdir = os.getcwd() |
|
54 | 54 | self.report_name = os.path.join(rptdir,'IPython_crash_report.txt') |
|
55 | 55 | self.TBhandler = ultraTB.VerboseTB(color_scheme=color_scheme,long_header=1) |
|
56 | 56 | traceback = self.TBhandler.text(etype,evalue,etb,context=31) |
|
57 | 57 | |
|
58 | 58 | # print traceback to screen |
|
59 | 59 | print >> sys.stderr, traceback |
|
60 | 60 | |
|
61 | 61 | # and generate a complete report on disk |
|
62 | 62 | try: |
|
63 | 63 | report = open(self.report_name,'w') |
|
64 | 64 | except: |
|
65 | 65 | print >> sys.stderr, 'Could not create crash report on disk.' |
|
66 | 66 | return |
|
67 | 67 | |
|
68 | 68 | msg = itpl('\n'+'*'*70+'\n' |
|
69 | 69 | """ |
|
70 | 70 | Oops, IPython crashed. We do our best to make it stable, but... |
|
71 | 71 | |
|
72 | 72 | A crash report was automatically generated with the following information: |
|
73 | 73 | - A verbatim copy of the traceback above this text. |
|
74 | 74 | - A copy of your input history during this session. |
|
75 | 75 | - Data on your current IPython configuration. |
|
76 | 76 | |
|
77 | 77 | It was left in the file named: |
|
78 | 78 | \t'$self.report_name' |
|
79 | 79 | If you can email this file to the developers, the information in it will help |
|
80 | 80 | them in understanding and correcting the problem. |
|
81 | 81 | |
|
82 | 82 | You can mail it to $self.bug_contact at $self.mailto |
|
83 | 83 | with the subject 'IPython Crash Report'. |
|
84 | 84 | |
|
85 | 85 | If you want to do it now, the following command will work (under Unix): |
|
86 | 86 | mail -s 'IPython Crash Report' $self.mailto < $self.report_name |
|
87 | 87 | |
|
88 | 88 | To ensure accurate tracking of this issue, please file a report about it at: |
|
89 | 89 | http://www.scipy.net/roundup/ipython (IPython's online bug tracker). |
|
90 | 90 | """) |
|
91 | 91 | print >> sys.stderr, msg |
|
92 | 92 | |
|
93 | 93 | sec_sep = '\n\n'+'*'*75+'\n\n' |
|
94 | 94 | report.write('*'*75+'\n\n'+'IPython post-mortem report\n\n') |
|
95 | 95 | report.write('IPython version: %s \n\n' % Release.version) |
|
96 | 96 | report.write('SVN revision : %s \n\n' % Release.revision) |
|
97 | 97 | report.write('Platform info : os.name -> %s, sys.platform -> %s' % |
|
98 | 98 | (os.name,sys.platform) ) |
|
99 | 99 | report.write(sec_sep+'Current user configuration structure:\n\n') |
|
100 | 100 | report.write(pformat(self.IP.rc.dict())) |
|
101 | 101 | report.write(sec_sep+'Crash traceback:\n\n' + traceback) |
|
102 | 102 | try: |
|
103 | 103 | report.write(sec_sep+"History of session input:") |
|
104 | 104 | for line in self.IP.user_ns['_ih']: |
|
105 | 105 | report.write(line) |
|
106 | 106 | report.write('\n*** Last line of input (may not be in above history):\n') |
|
107 | 107 | report.write(self.IP._last_input_line+'\n') |
|
108 | 108 | except: |
|
109 | 109 | pass |
|
110 | 110 | report.close() |
@@ -1,53 +1,264 b'' | |||
|
1 | 1 | # -*- coding: utf-8 -*- |
|
2 | 2 | """ |
|
3 | 3 | Pdb debugger class. |
|
4 | 4 | |
|
5 | 5 | Modified from the standard pdb.Pdb class to avoid including readline, so that |
|
6 | 6 | the command line completion of other programs which include this isn't |
|
7 | 7 | damaged. |
|
8 | 8 | |
|
9 | 9 | In the future, this class will be expanded with improvements over the standard |
|
10 | 10 | pdb. |
|
11 | 11 | |
|
12 | 12 | The code in this file is mainly lifted out of cmd.py in Python 2.2, with minor |
|
13 | 13 | changes. Licensing should therefore be under the standard Python terms. For |
|
14 | 14 | details on the PSF (Python Software Foundation) standard license, see: |
|
15 | 15 | |
|
16 | 16 | http://www.python.org/2.2.3/license.html |
|
17 | 17 | |
|
18 |
$Id: Debugger.py |
|
|
18 | $Id: Debugger.py 951 2005-12-25 00:57:24Z fperez $""" | |
|
19 | 19 | |
|
20 | 20 | from IPython import Release |
|
21 | 21 | __author__ = '%s <%s>' % Release.authors['Fernando'] |
|
22 | 22 | __license__ = 'Python' |
|
23 | 23 | |
|
24 | import pdb,bdb,cmd,os,sys | |
|
24 | import pdb,bdb,cmd,os,sys,linecache | |
|
25 | from IPython import PyColorize, ColorANSI | |
|
26 | from IPython.genutils import Term | |
|
27 | from IPython.excolors import ExceptionColors | |
|
28 | ||
|
29 | def _file_lines(fname): | |
|
30 | """Return the contents of a named file as a list of lines. | |
|
31 | ||
|
32 | This function never raises an IOError exception: if the file can't be | |
|
33 | read, it simply returns an empty list.""" | |
|
34 | ||
|
35 | try: | |
|
36 | outfile = open(fname) | |
|
37 | except IOError: | |
|
38 | return [] | |
|
39 | else: | |
|
40 | out = outfile.readlines() | |
|
41 | outfile.close() | |
|
42 | return out | |
|
43 | ||
|
25 | 44 | |
|
26 | 45 | class Pdb(pdb.Pdb): |
|
27 | 46 | """Modified Pdb class, does not load readline.""" |
|
28 | def __init__(self): | |
|
47 | def __init__(self,color_scheme='NoColor'): | |
|
29 | 48 | bdb.Bdb.__init__(self) |
|
30 | 49 | cmd.Cmd.__init__(self,completekey=None) # don't load readline |
|
31 |
self.prompt = ' |
|
|
50 | self.prompt = 'ipdb> ' # The default prompt is '(Pdb)' | |
|
32 | 51 | self.aliases = {} |
|
33 | 52 | |
|
34 | 53 | # Read $HOME/.pdbrc and ./.pdbrc |
|
35 | self.rcLines = [] | |
|
36 | if os.environ.has_key('HOME'): | |
|
37 | envHome = os.environ['HOME'] | |
|
38 | try: | |
|
39 | rcFile = open(os.path.join(envHome, ".pdbrc")) | |
|
40 | except IOError: | |
|
41 | pass | |
|
42 | else: | |
|
43 | for line in rcFile.readlines(): | |
|
44 | self.rcLines.append(line) | |
|
45 | rcFile.close() | |
|
46 | 54 | try: |
|
47 | rcFile = open(".pdbrc") | |
|
48 | except IOError: | |
|
55 | self.rcLines = _file_lines(os.path.join(os.environ['HOME'], | |
|
56 | ".pdbrc")) | |
|
57 | except KeyError: | |
|
58 | self.rcLines = [] | |
|
59 | self.rcLines.extend(_file_lines(".pdbrc")) | |
|
60 | ||
|
61 | # Create color table: we copy the default one from the traceback | |
|
62 | # module and add a few attributes needed for debugging | |
|
63 | self.color_scheme_table = ExceptionColors.copy() | |
|
64 | ||
|
65 | # shorthands | |
|
66 | C = ColorANSI.TermColors | |
|
67 | cst = self.color_scheme_table | |
|
68 | ||
|
69 | cst['NoColor'].colors.breakpoint_enabled = C.NoColor | |
|
70 | cst['NoColor'].colors.breakpoint_disabled = C.NoColor | |
|
71 | ||
|
72 | cst['Linux'].colors.breakpoint_enabled = C.LightRed | |
|
73 | cst['Linux'].colors.breakpoint_disabled = C.Red | |
|
74 | ||
|
75 | cst['LightBG'].colors.breakpoint_enabled = C.LightRed | |
|
76 | cst['LightBG'].colors.breakpoint_disabled = C.Red | |
|
77 | ||
|
78 | self.set_colors(color_scheme) | |
|
79 | ||
|
80 | def set_colors(self, scheme): | |
|
81 | """Shorthand access to the color table scheme selector method.""" | |
|
82 | self.color_scheme_table.set_active_scheme(scheme) | |
|
83 | ||
|
84 | ||
|
85 | def interaction(self, frame, traceback): | |
|
86 | __IPYTHON__.set_completer_frame(frame) | |
|
87 | pdb.Pdb.interaction(self, frame, traceback) | |
|
88 | ||
|
89 | ||
|
90 | def do_up(self, arg): | |
|
91 | pdb.Pdb.do_up(self, arg) | |
|
92 | __IPYTHON__.set_completer_frame(self.curframe) | |
|
93 | do_u = do_up | |
|
94 | ||
|
95 | ||
|
96 | def do_down(self, arg): | |
|
97 | pdb.Pdb.do_down(self, arg) | |
|
98 | __IPYTHON__.set_completer_frame(self.curframe) | |
|
99 | do_d = do_down | |
|
100 | ||
|
101 | ||
|
102 | def postloop(self): | |
|
103 | __IPYTHON__.set_completer_frame(None) | |
|
104 | ||
|
105 | ||
|
106 | def print_stack_trace(self): | |
|
107 | try: | |
|
108 | for frame_lineno in self.stack: | |
|
109 | self.print_stack_entry(frame_lineno, context = 5) | |
|
110 | except KeyboardInterrupt: | |
|
49 | 111 | pass |
|
112 | ||
|
113 | ||
|
114 | def print_stack_entry(self,frame_lineno,prompt_prefix='\n-> ', | |
|
115 | context = 3): | |
|
116 | frame, lineno = frame_lineno | |
|
117 | print >>Term.cout, self.format_stack_entry(frame_lineno, '', context) | |
|
118 | ||
|
119 | ||
|
120 | def format_stack_entry(self, frame_lineno, lprefix=': ', context = 3): | |
|
121 | import linecache, repr | |
|
122 | ||
|
123 | ret = "" | |
|
124 | ||
|
125 | Colors = self.color_scheme_table.active_colors | |
|
126 | ColorsNormal = Colors.Normal | |
|
127 | tpl_link = '%s%%s%s' % (Colors.filenameEm, ColorsNormal) | |
|
128 | tpl_call = 'in %s%%s%s%%s%s' % (Colors.vName, Colors.valEm, ColorsNormal) | |
|
129 | tpl_line = '%%s%s%%s %s%%s' % (Colors.lineno, ColorsNormal) | |
|
130 | tpl_line_em = '%%s%s%%s %s%%s%s' % (Colors.linenoEm, Colors.line, | |
|
131 | ColorsNormal) | |
|
132 | ||
|
133 | frame, lineno = frame_lineno | |
|
134 | ||
|
135 | return_value = '' | |
|
136 | if '__return__' in frame.f_locals: | |
|
137 | rv = frame.f_locals['__return__'] | |
|
138 | #return_value += '->' | |
|
139 | return_value += repr.repr(rv) + '\n' | |
|
140 | ret += return_value | |
|
141 | ||
|
142 | #s = filename + '(' + `lineno` + ')' | |
|
143 | filename = self.canonic(frame.f_code.co_filename) | |
|
144 | link = tpl_link % filename | |
|
145 | ||
|
146 | if frame.f_code.co_name: | |
|
147 | func = frame.f_code.co_name | |
|
148 | else: | |
|
149 | func = "<lambda>" | |
|
150 | ||
|
151 | call = '' | |
|
152 | if func != '?': | |
|
153 | if '__args__' in frame.f_locals: | |
|
154 | args = repr.repr(frame.f_locals['__args__']) | |
|
155 | else: | |
|
156 | args = '()' | |
|
157 | call = tpl_call % (func, args) | |
|
158 | ||
|
159 | level = '%s %s\n' % (link, call) | |
|
160 | ret += level | |
|
161 | ||
|
162 | start = lineno - 1 - context//2 | |
|
163 | lines = linecache.getlines(filename) | |
|
164 | start = max(start, 0) | |
|
165 | start = min(start, len(lines) - context) | |
|
166 | lines = lines[start : start + context] | |
|
167 | ||
|
168 | for i in range(len(lines)): | |
|
169 | line = lines[i] | |
|
170 | if start + 1 + i == lineno: | |
|
171 | ret += self.__format_line(tpl_line_em, filename, start + 1 + i, line, arrow = True) | |
|
172 | else: | |
|
173 | ret += self.__format_line(tpl_line, filename, start + 1 + i, line, arrow = False) | |
|
174 | ||
|
175 | return ret | |
|
176 | ||
|
177 | ||
|
178 | def __format_line(self, tpl_line, filename, lineno, line, arrow = False): | |
|
179 | bp_mark = "" | |
|
180 | bp_mark_color = "" | |
|
181 | ||
|
182 | bp = None | |
|
183 | if lineno in self.get_file_breaks(filename): | |
|
184 | bps = self.get_breaks(filename, lineno) | |
|
185 | bp = bps[-1] | |
|
186 | ||
|
187 | if bp: | |
|
188 | Colors = self.color_scheme_table.active_colors | |
|
189 | bp_mark = str(bp.number) | |
|
190 | bp_mark_color = Colors.breakpoint_enabled | |
|
191 | if not bp.enabled: | |
|
192 | bp_mark_color = Colors.breakpoint_disabled | |
|
193 | ||
|
194 | numbers_width = 7 | |
|
195 | if arrow: | |
|
196 | # This is the line with the error | |
|
197 | pad = numbers_width - len(str(lineno)) - len(bp_mark) | |
|
198 | if pad >= 3: | |
|
199 | marker = '-'*(pad-3) + '-> ' | |
|
200 | elif pad == 2: | |
|
201 | marker = '> ' | |
|
202 | elif pad == 1: | |
|
203 | marker = '>' | |
|
204 | else: | |
|
205 | marker = '' | |
|
206 | num = '%s%s' % (marker, str(lineno)) | |
|
207 | line = tpl_line % (bp_mark_color + bp_mark, num, line) | |
|
208 | else: | |
|
209 | num = '%*s' % (numbers_width - len(bp_mark), str(lineno)) | |
|
210 | line = tpl_line % (bp_mark_color + bp_mark, num, line) | |
|
211 | ||
|
212 | return line | |
|
213 | ||
|
214 | ||
|
215 | def do_list(self, arg): | |
|
216 | self.lastcmd = 'list' | |
|
217 | last = None | |
|
218 | if arg: | |
|
219 | try: | |
|
220 | x = eval(arg, {}, {}) | |
|
221 | if type(x) == type(()): | |
|
222 | first, last = x | |
|
223 | first = int(first) | |
|
224 | last = int(last) | |
|
225 | if last < first: | |
|
226 | # Assume it's a count | |
|
227 | last = first + last | |
|
228 | else: | |
|
229 | first = max(1, int(x) - 5) | |
|
230 | except: | |
|
231 | print '*** Error in argument:', `arg` | |
|
232 | return | |
|
233 | elif self.lineno is None: | |
|
234 | first = max(1, self.curframe.f_lineno - 5) | |
|
50 | 235 | else: |
|
51 | for line in rcFile.readlines(): | |
|
52 | self.rcLines.append(line) | |
|
53 | rcFile.close() | |
|
236 | first = self.lineno + 1 | |
|
237 | if last is None: | |
|
238 | last = first + 10 | |
|
239 | filename = self.curframe.f_code.co_filename | |
|
240 | try: | |
|
241 | Colors = self.color_scheme_table.active_colors | |
|
242 | ColorsNormal = Colors.Normal | |
|
243 | tpl_line = '%%s%s%%s %s%%s' % (Colors.lineno, ColorsNormal) | |
|
244 | tpl_line_em = '%%s%s%%s %s%%s%s' % (Colors.linenoEm, Colors.line, ColorsNormal) | |
|
245 | src = [] | |
|
246 | for lineno in range(first, last+1): | |
|
247 | line = linecache.getline(filename, lineno) | |
|
248 | if not line: | |
|
249 | break | |
|
250 | ||
|
251 | if lineno == self.curframe.f_lineno: | |
|
252 | line = self.__format_line(tpl_line_em, filename, lineno, line, arrow = True) | |
|
253 | else: | |
|
254 | line = self.__format_line(tpl_line, filename, lineno, line, arrow = False) | |
|
255 | ||
|
256 | src.append(line) | |
|
257 | self.lineno = lineno | |
|
258 | ||
|
259 | print >>Term.cout, ''.join(src) | |
|
260 | ||
|
261 | except KeyboardInterrupt: | |
|
262 | pass | |
|
263 | ||
|
264 | do_l = do_list |
@@ -1,2563 +1,2573 b'' | |||
|
1 | 1 | # -*- coding: utf-8 -*- |
|
2 | 2 | """Magic functions for InteractiveShell. |
|
3 | 3 | |
|
4 |
$Id: Magic.py 9 |
|
|
4 | $Id: Magic.py 951 2005-12-25 00:57:24Z fperez $""" | |
|
5 | 5 | |
|
6 | 6 | #***************************************************************************** |
|
7 | 7 | # Copyright (C) 2001 Janko Hauser <jhauser@zscout.de> and |
|
8 | 8 | # Copyright (C) 2001-2004 Fernando Perez <fperez@colorado.edu> |
|
9 | 9 | # |
|
10 | 10 | # Distributed under the terms of the BSD License. The full license is in |
|
11 | 11 | # the file COPYING, distributed as part of this software. |
|
12 | 12 | #***************************************************************************** |
|
13 | 13 | |
|
14 | 14 | #**************************************************************************** |
|
15 | 15 | # Modules and globals |
|
16 | 16 | |
|
17 | 17 | from IPython import Release |
|
18 | 18 | __author__ = '%s <%s>\n%s <%s>' % \ |
|
19 | 19 | ( Release.authors['Janko'] + Release.authors['Fernando'] ) |
|
20 | 20 | __license__ = Release.license |
|
21 | 21 | |
|
22 | 22 | # Python standard modules |
|
23 | 23 | import __builtin__ |
|
24 | 24 | import os,sys,inspect,pydoc,re,tempfile,pdb,bdb,time |
|
25 | import Debugger | |
|
26 | from getopt import getopt | |
|
27 | from pprint import pprint, pformat | |
|
28 | from cStringIO import StringIO | |
|
29 | ||
|
30 | # profile isn't bundled by default in Debian for license reasons | |
|
25 | 31 | try: |
|
26 | 32 | import profile,pstats |
|
27 | 33 | except ImportError: |
|
28 | 34 | profile = pstats = None |
|
29 | from getopt import getopt | |
|
30 | from pprint import pprint, pformat | |
|
31 | from cStringIO import StringIO | |
|
32 | 35 | |
|
33 | 36 | # Homebrewed |
|
34 | 37 | from IPython.Struct import Struct |
|
35 | 38 | from IPython.Itpl import Itpl, itpl, printpl,itplns |
|
36 | 39 | from IPython.FakeModule import FakeModule |
|
37 | 40 | from IPython.PyColorize import Parser |
|
38 | 41 | from IPython import OInspect |
|
39 | 42 | from IPython import wildcard |
|
40 | 43 | from IPython.genutils import * |
|
41 | 44 | |
|
42 | 45 | # Globals to be set later by Magic constructor |
|
43 | 46 | MAGIC_PREFIX = '' |
|
44 | 47 | MAGIC_ESCAPE = '' |
|
45 | 48 | |
|
46 | 49 | #*************************************************************************** |
|
47 | 50 | # Utility functions |
|
48 | 51 | def magic2python(cmd): |
|
49 | 52 | """Convert a command string of magic syntax to valid Python code.""" |
|
50 | 53 | |
|
51 | 54 | if cmd.startswith('#'+MAGIC_ESCAPE) or \ |
|
52 | 55 | cmd.startswith(MAGIC_ESCAPE): |
|
53 | 56 | if cmd[0]=='#': |
|
54 | 57 | cmd = cmd[1:] |
|
55 | 58 | # we need to return the proper line end later |
|
56 | 59 | if cmd[-1] == '\n': |
|
57 | 60 | endl = '\n' |
|
58 | 61 | else: |
|
59 | 62 | endl = '' |
|
60 | 63 | try: |
|
61 | 64 | func,args = cmd[1:].split(' ',1) |
|
62 | 65 | except: |
|
63 | 66 | func,args = cmd[1:].rstrip(),'' |
|
64 | 67 | args = args.replace('"','\\"').replace("'","\\'").rstrip() |
|
65 | 68 | return '%s%s ("%s")%s' % (MAGIC_PREFIX,func,args,endl) |
|
66 | 69 | else: |
|
67 | 70 | return cmd |
|
68 | 71 | |
|
69 | 72 | def on_off(tag): |
|
70 | 73 | """Return an ON/OFF string for a 1/0 input. Simple utility function.""" |
|
71 | 74 | return ['OFF','ON'][tag] |
|
72 | 75 | |
|
73 | 76 | |
|
74 | 77 | #**************************************************************************** |
|
75 | 78 | # Utility classes |
|
76 | 79 | class Macro: |
|
77 | 80 | """Simple class to store the value of macros as strings. |
|
78 | 81 | |
|
79 | 82 | This allows us to later exec them by checking when something is an |
|
80 | 83 | instance of this class.""" |
|
81 | 84 | |
|
82 | 85 | def __init__(self,cmds): |
|
83 | 86 | """Build a macro from a list of commands.""" |
|
84 | 87 | |
|
85 | 88 | # Since the list may include multi-line entries, first make sure that |
|
86 | 89 | # they've been all broken up before passing it to magic2python |
|
87 | 90 | cmdlist = map(magic2python,''.join(cmds).split('\n')) |
|
88 | 91 | self.value = '\n'.join(cmdlist) |
|
89 | 92 | |
|
90 | 93 | def __str__(self): |
|
91 | 94 | return self.value |
|
92 | 95 | |
|
93 | 96 | #*************************************************************************** |
|
94 | 97 | # Main class implementing Magic functionality |
|
95 | 98 | class Magic: |
|
96 | 99 | """Magic functions for InteractiveShell. |
|
97 | 100 | |
|
98 | 101 | Shell functions which can be reached as %function_name. All magic |
|
99 | 102 | functions should accept a string, which they can parse for their own |
|
100 | 103 | needs. This can make some functions easier to type, eg `%cd ../` |
|
101 | 104 | vs. `%cd("../")` |
|
102 | 105 | |
|
103 | 106 | ALL definitions MUST begin with the prefix magic_. The user won't need it |
|
104 | 107 | at the command line, but it is is needed in the definition. """ |
|
105 | 108 | |
|
106 | 109 | # class globals |
|
107 | 110 | auto_status = ['Automagic is OFF, % prefix IS needed for magic functions.', |
|
108 | 111 | 'Automagic is ON, % prefix NOT needed for magic functions.'] |
|
109 | 112 | |
|
110 | 113 | #...................................................................... |
|
111 | 114 | # some utility functions |
|
112 | 115 | |
|
113 | 116 | def __init__(self,shell): |
|
114 | 117 | # XXX This is hackish, clean up later to avoid these messy globals |
|
115 | 118 | global MAGIC_PREFIX, MAGIC_ESCAPE |
|
116 | 119 | |
|
117 | 120 | self.options_table = {} |
|
118 | 121 | MAGIC_PREFIX = shell.name+'.magic_' |
|
119 | 122 | MAGIC_ESCAPE = shell.ESC_MAGIC |
|
120 | 123 | if profile is None: |
|
121 | 124 | self.magic_prun = self.profile_missing_notice |
|
122 | 125 | |
|
123 | 126 | def profile_missing_notice(self, *args, **kwargs): |
|
124 | 127 | error("""\ |
|
125 | 128 | The profile module could not be found. If you are a Debian user, |
|
126 | 129 | it has been removed from the standard Debian package because of its non-free |
|
127 | 130 | license. To use profiling, please install"python2.3-profiler" from non-free.""") |
|
128 | 131 | |
|
129 | 132 | def default_option(self,fn,optstr): |
|
130 | 133 | """Make an entry in the options_table for fn, with value optstr""" |
|
131 | 134 | |
|
132 | 135 | if fn not in self.lsmagic(): |
|
133 | 136 | error("%s is not a magic function" % fn) |
|
134 | 137 | self.options_table[fn] = optstr |
|
135 | 138 | |
|
136 | 139 | def lsmagic(self): |
|
137 | 140 | """Return a list of currently available magic functions. |
|
138 | 141 | |
|
139 | 142 | Gives a list of the bare names after mangling (['ls','cd', ...], not |
|
140 | 143 | ['magic_ls','magic_cd',...]""" |
|
141 | 144 | |
|
142 | 145 | # FIXME. This needs a cleanup, in the way the magics list is built. |
|
143 | 146 | |
|
144 | 147 | # magics in class definition |
|
145 | 148 | class_magic = lambda fn: fn.startswith('magic_') and \ |
|
146 | 149 | callable(Magic.__dict__[fn]) |
|
147 | 150 | # in instance namespace (run-time user additions) |
|
148 | 151 | inst_magic = lambda fn: fn.startswith('magic_') and \ |
|
149 | 152 | callable(self.__dict__[fn]) |
|
150 | 153 | # and bound magics by user (so they can access self): |
|
151 | 154 | inst_bound_magic = lambda fn: fn.startswith('magic_') and \ |
|
152 | 155 | callable(self.__class__.__dict__[fn]) |
|
153 | 156 | magics = filter(class_magic,Magic.__dict__.keys()) + \ |
|
154 | 157 | filter(inst_magic,self.__dict__.keys()) + \ |
|
155 | 158 | filter(inst_bound_magic,self.__class__.__dict__.keys()) |
|
156 | 159 | out = [] |
|
157 | 160 | for fn in magics: |
|
158 | 161 | out.append(fn.replace('magic_','',1)) |
|
159 | 162 | out.sort() |
|
160 | 163 | return out |
|
161 | 164 | |
|
162 | 165 | def set_shell(self,shell): |
|
163 | 166 | self.shell = shell |
|
164 | 167 | self.alias_table = shell.alias_table |
|
165 | 168 | |
|
166 | 169 | def extract_input_slices(self,slices): |
|
167 | 170 | """Return as a string a set of input history slices. |
|
168 | 171 | |
|
169 | 172 | The set of slices is given as a list of strings (like ['1','4:8','9'], |
|
170 | 173 | since this function is for use by magic functions which get their |
|
171 | 174 | arguments as strings.""" |
|
172 | 175 | |
|
173 | 176 | cmds = [] |
|
174 | 177 | for chunk in slices: |
|
175 | 178 | if ':' in chunk: |
|
176 | 179 | ini,fin = map(int,chunk.split(':')) |
|
177 | 180 | else: |
|
178 | 181 | ini = int(chunk) |
|
179 | 182 | fin = ini+1 |
|
180 | 183 | cmds.append(self.shell.input_hist[ini:fin]) |
|
181 | 184 | return cmds |
|
182 | 185 | |
|
183 | 186 | def _ofind(self,oname): |
|
184 | 187 | """Find an object in the available namespaces. |
|
185 | 188 | |
|
186 | 189 | self._ofind(oname) -> dict with keys: found,obj,ospace,ismagic |
|
187 | 190 | |
|
188 | 191 | Has special code to detect magic functions. |
|
189 | 192 | """ |
|
190 | 193 | |
|
191 | 194 | oname = oname.strip() |
|
192 | 195 | |
|
193 | 196 | # Namespaces to search in: |
|
194 | 197 | user_ns = self.shell.user_ns |
|
195 | 198 | internal_ns = self.shell.internal_ns |
|
196 | 199 | builtin_ns = __builtin__.__dict__ |
|
197 | 200 | alias_ns = self.shell.alias_table |
|
198 | 201 | |
|
199 | 202 | # Put them in a list. The order is important so that we find things in |
|
200 | 203 | # the same order that Python finds them. |
|
201 | 204 | namespaces = [ ('Interactive',user_ns), |
|
202 | 205 | ('IPython internal',internal_ns), |
|
203 | 206 | ('Python builtin',builtin_ns), |
|
204 | 207 | ('Alias',alias_ns), |
|
205 | 208 | ] |
|
206 | 209 | |
|
207 | 210 | # initialize results to 'null' |
|
208 | 211 | found = 0; obj = None; ospace = None; ds = None; |
|
209 | 212 | ismagic = 0; isalias = 0 |
|
210 | 213 | |
|
211 | 214 | # Look for the given name by splitting it in parts. If the head is |
|
212 | 215 | # found, then we look for all the remaining parts as members, and only |
|
213 | 216 | # declare success if we can find them all. |
|
214 | 217 | oname_parts = oname.split('.') |
|
215 | 218 | oname_head, oname_rest = oname_parts[0],oname_parts[1:] |
|
216 | 219 | for nsname,ns in namespaces: |
|
217 | 220 | try: |
|
218 | 221 | obj = ns[oname_head] |
|
219 | 222 | except KeyError: |
|
220 | 223 | continue |
|
221 | 224 | else: |
|
222 | 225 | for part in oname_rest: |
|
223 | 226 | try: |
|
224 | 227 | obj = getattr(obj,part) |
|
225 | 228 | except: |
|
226 | 229 | # Blanket except b/c some badly implemented objects |
|
227 | 230 | # allow __getattr__ to raise exceptions other than |
|
228 | 231 | # AttributeError, which then crashes IPython. |
|
229 | 232 | break |
|
230 | 233 | else: |
|
231 | 234 | # If we finish the for loop (no break), we got all members |
|
232 | 235 | found = 1 |
|
233 | 236 | ospace = nsname |
|
234 | 237 | if ns == alias_ns: |
|
235 | 238 | isalias = 1 |
|
236 | 239 | break # namespace loop |
|
237 | 240 | |
|
238 | 241 | # Try to see if it's magic |
|
239 | 242 | if not found: |
|
240 | 243 | if oname.startswith(self.shell.ESC_MAGIC): |
|
241 | 244 | oname = oname[1:] |
|
242 | 245 | obj = getattr(self,'magic_'+oname,None) |
|
243 | 246 | if obj is not None: |
|
244 | 247 | found = 1 |
|
245 | 248 | ospace = 'IPython internal' |
|
246 | 249 | ismagic = 1 |
|
247 | 250 | |
|
248 | 251 | # Last try: special-case some literals like '', [], {}, etc: |
|
249 | 252 | if not found and oname_head in ["''",'""','[]','{}','()']: |
|
250 | 253 | obj = eval(oname_head) |
|
251 | 254 | found = 1 |
|
252 | 255 | ospace = 'Interactive' |
|
253 | 256 | |
|
254 | 257 | return {'found':found, 'obj':obj, 'namespace':ospace, |
|
255 | 258 | 'ismagic':ismagic, 'isalias':isalias} |
|
256 | 259 | |
|
257 | 260 | def arg_err(self,func): |
|
258 | 261 | """Print docstring if incorrect arguments were passed""" |
|
259 | 262 | print 'Error in arguments:' |
|
260 | 263 | print OInspect.getdoc(func) |
|
261 | 264 | |
|
262 | 265 | |
|
263 | 266 | def format_latex(self,str): |
|
264 | 267 | """Format a string for latex inclusion.""" |
|
265 | 268 | |
|
266 | 269 | # Characters that need to be escaped for latex: |
|
267 | 270 | escape_re = re.compile(r'(%|_|\$)',re.MULTILINE) |
|
268 | 271 | # Magic command names as headers: |
|
269 | 272 | cmd_name_re = re.compile(r'^(%s.*?):' % self.shell.ESC_MAGIC, |
|
270 | 273 | re.MULTILINE) |
|
271 | 274 | # Magic commands |
|
272 | 275 | cmd_re = re.compile(r'(?P<cmd>%s.+?\b)(?!\}\}:)' % self.shell.ESC_MAGIC, |
|
273 | 276 | re.MULTILINE) |
|
274 | 277 | # Paragraph continue |
|
275 | 278 | par_re = re.compile(r'\\$',re.MULTILINE) |
|
276 | 279 | |
|
277 | 280 | str = cmd_name_re.sub(r'\n\\texttt{\\textsl{\\large \1}}:',str) |
|
278 | 281 | str = cmd_re.sub(r'\\texttt{\g<cmd>}',str) |
|
279 | 282 | str = par_re.sub(r'\\\\',str) |
|
280 | 283 | str = escape_re.sub(r'\\\1',str) |
|
281 | 284 | return str |
|
282 | 285 | |
|
283 | 286 | def format_screen(self,str): |
|
284 | 287 | """Format a string for screen printing. |
|
285 | 288 | |
|
286 | 289 | This removes some latex-type format codes.""" |
|
287 | 290 | # Paragraph continue |
|
288 | 291 | par_re = re.compile(r'\\$',re.MULTILINE) |
|
289 | 292 | str = par_re.sub('',str) |
|
290 | 293 | return str |
|
291 | 294 | |
|
292 | 295 | def parse_options(self,arg_str,opt_str,*long_opts,**kw): |
|
293 | 296 | """Parse options passed to an argument string. |
|
294 | 297 | |
|
295 | 298 | The interface is similar to that of getopt(), but it returns back a |
|
296 | 299 | Struct with the options as keys and the stripped argument string still |
|
297 | 300 | as a string. |
|
298 | 301 | |
|
299 | 302 | arg_str is quoted as a true sys.argv vector by using shlex.split. |
|
300 | 303 | This allows us to easily expand variables, glob files, quote |
|
301 | 304 | arguments, etc. |
|
302 | 305 | |
|
303 | 306 | Options: |
|
304 | 307 | -mode: default 'string'. If given as 'list', the argument string is |
|
305 | 308 | returned as a list (split on whitespace) instead of a string. |
|
306 | 309 | |
|
307 | 310 | -list_all: put all option values in lists. Normally only options |
|
308 | 311 | appearing more than once are put in a list.""" |
|
309 | 312 | |
|
310 | 313 | # inject default options at the beginning of the input line |
|
311 | 314 | caller = sys._getframe(1).f_code.co_name.replace('magic_','') |
|
312 | 315 | arg_str = '%s %s' % (self.options_table.get(caller,''),arg_str) |
|
313 | 316 | |
|
314 | 317 | mode = kw.get('mode','string') |
|
315 | 318 | if mode not in ['string','list']: |
|
316 | 319 | raise ValueError,'incorrect mode given: %s' % mode |
|
317 | 320 | # Get options |
|
318 | 321 | list_all = kw.get('list_all',0) |
|
319 | 322 | |
|
320 | 323 | # Check if we have more than one argument to warrant extra processing: |
|
321 | 324 | odict = {} # Dictionary with options |
|
322 | 325 | args = arg_str.split() |
|
323 | 326 | if len(args) >= 1: |
|
324 | 327 | # If the list of inputs only has 0 or 1 thing in it, there's no |
|
325 | 328 | # need to look for options |
|
326 | 329 | argv = shlex_split(arg_str) |
|
327 | 330 | # Do regular option processing |
|
328 | 331 | opts,args = getopt(argv,opt_str,*long_opts) |
|
329 | 332 | for o,a in opts: |
|
330 | 333 | if o.startswith('--'): |
|
331 | 334 | o = o[2:] |
|
332 | 335 | else: |
|
333 | 336 | o = o[1:] |
|
334 | 337 | try: |
|
335 | 338 | odict[o].append(a) |
|
336 | 339 | except AttributeError: |
|
337 | 340 | odict[o] = [odict[o],a] |
|
338 | 341 | except KeyError: |
|
339 | 342 | if list_all: |
|
340 | 343 | odict[o] = [a] |
|
341 | 344 | else: |
|
342 | 345 | odict[o] = a |
|
343 | 346 | |
|
344 | 347 | # Prepare opts,args for return |
|
345 | 348 | opts = Struct(odict) |
|
346 | 349 | if mode == 'string': |
|
347 | 350 | args = ' '.join(args) |
|
348 | 351 | |
|
349 | 352 | return opts,args |
|
350 | 353 | |
|
351 | 354 | #...................................................................... |
|
352 | 355 | # And now the actual magic functions |
|
353 | 356 | |
|
354 | 357 | # Functions for IPython shell work (vars,funcs, config, etc) |
|
355 | 358 | def magic_lsmagic(self, parameter_s = ''): |
|
356 | 359 | """List currently available magic functions.""" |
|
357 | 360 | mesc = self.shell.ESC_MAGIC |
|
358 | 361 | print 'Available magic functions:\n'+mesc+\ |
|
359 | 362 | (' '+mesc).join(self.lsmagic()) |
|
360 | 363 | print '\n' + Magic.auto_status[self.shell.rc.automagic] |
|
361 | 364 | return None |
|
362 | 365 | |
|
363 | 366 | def magic_magic(self, parameter_s = ''): |
|
364 | 367 | """Print information about the magic function system.""" |
|
365 | 368 | |
|
366 | 369 | mode = '' |
|
367 | 370 | try: |
|
368 | 371 | if parameter_s.split()[0] == '-latex': |
|
369 | 372 | mode = 'latex' |
|
370 | 373 | except: |
|
371 | 374 | pass |
|
372 | 375 | |
|
373 | 376 | magic_docs = [] |
|
374 | 377 | for fname in self.lsmagic(): |
|
375 | 378 | mname = 'magic_' + fname |
|
376 | 379 | for space in (Magic,self,self.__class__): |
|
377 | 380 | try: |
|
378 | 381 | fn = space.__dict__[mname] |
|
379 | 382 | except KeyError: |
|
380 | 383 | pass |
|
381 | 384 | else: |
|
382 | 385 | break |
|
383 | 386 | magic_docs.append('%s%s:\n\t%s\n' %(self.shell.ESC_MAGIC, |
|
384 | 387 | fname,fn.__doc__)) |
|
385 | 388 | magic_docs = ''.join(magic_docs) |
|
386 | 389 | |
|
387 | 390 | if mode == 'latex': |
|
388 | 391 | print self.format_latex(magic_docs) |
|
389 | 392 | return |
|
390 | 393 | else: |
|
391 | 394 | magic_docs = self.format_screen(magic_docs) |
|
392 | 395 | |
|
393 | 396 | outmsg = """ |
|
394 | 397 | IPython's 'magic' functions |
|
395 | 398 | =========================== |
|
396 | 399 | |
|
397 | 400 | The magic function system provides a series of functions which allow you to |
|
398 | 401 | control the behavior of IPython itself, plus a lot of system-type |
|
399 | 402 | features. All these functions are prefixed with a % character, but parameters |
|
400 | 403 | are given without parentheses or quotes. |
|
401 | 404 | |
|
402 | 405 | NOTE: If you have 'automagic' enabled (via the command line option or with the |
|
403 | 406 | %automagic function), you don't need to type in the % explicitly. By default, |
|
404 | 407 | IPython ships with automagic on, so you should only rarely need the % escape. |
|
405 | 408 | |
|
406 | 409 | Example: typing '%cd mydir' (without the quotes) changes you working directory |
|
407 | 410 | to 'mydir', if it exists. |
|
408 | 411 | |
|
409 | 412 | You can define your own magic functions to extend the system. See the supplied |
|
410 | 413 | ipythonrc and example-magic.py files for details (in your ipython |
|
411 | 414 | configuration directory, typically $HOME/.ipython/). |
|
412 | 415 | |
|
413 | 416 | You can also define your own aliased names for magic functions. In your |
|
414 | 417 | ipythonrc file, placing a line like: |
|
415 | 418 | |
|
416 | 419 | execute __IPYTHON__.magic_pf = __IPYTHON__.magic_profile |
|
417 | 420 | |
|
418 | 421 | will define %pf as a new name for %profile. |
|
419 | 422 | |
|
420 | 423 | You can also call magics in code using the ipmagic() function, which IPython |
|
421 | 424 | automatically adds to the builtin namespace. Type 'ipmagic?' for details. |
|
422 | 425 | |
|
423 | 426 | For a list of the available magic functions, use %lsmagic. For a description |
|
424 | 427 | of any of them, type %magic_name?, e.g. '%cd?'. |
|
425 | 428 | |
|
426 | 429 | Currently the magic system has the following functions:\n""" |
|
427 | 430 | |
|
428 | 431 | mesc = self.shell.ESC_MAGIC |
|
429 | 432 | outmsg = ("%s\n%s\n\nSummary of magic functions (from %slsmagic):" |
|
430 | 433 | "\n\n%s%s\n\n%s" % (outmsg, |
|
431 | 434 | magic_docs,mesc,mesc, |
|
432 | 435 | (' '+mesc).join(self.lsmagic()), |
|
433 | 436 | Magic.auto_status[self.shell.rc.automagic] ) ) |
|
434 | 437 | |
|
435 | 438 | page(outmsg,screen_lines=self.shell.rc.screen_length) |
|
436 | 439 | |
|
437 | 440 | def magic_automagic(self, parameter_s = ''): |
|
438 | 441 | """Make magic functions callable without having to type the initial %. |
|
439 | 442 | |
|
440 | 443 | Toggles on/off (when off, you must call it as %automagic, of |
|
441 | 444 | course). Note that magic functions have lowest priority, so if there's |
|
442 | 445 | a variable whose name collides with that of a magic fn, automagic |
|
443 | 446 | won't work for that function (you get the variable instead). However, |
|
444 | 447 | if you delete the variable (del var), the previously shadowed magic |
|
445 | 448 | function becomes visible to automagic again.""" |
|
446 | 449 | |
|
447 | 450 | rc = self.shell.rc |
|
448 | 451 | rc.automagic = not rc.automagic |
|
449 | 452 | print '\n' + Magic.auto_status[rc.automagic] |
|
450 | 453 | |
|
451 | 454 | def magic_autocall(self, parameter_s = ''): |
|
452 | 455 | """Make functions callable without having to type parentheses. |
|
453 | 456 | |
|
454 | 457 | This toggles the autocall command line option on and off.""" |
|
455 | 458 | |
|
456 | 459 | rc = self.shell.rc |
|
457 | 460 | rc.autocall = not rc.autocall |
|
458 | 461 | print "Automatic calling is:",['OFF','ON'][rc.autocall] |
|
459 | 462 | |
|
460 | 463 | def magic_autoindent(self, parameter_s = ''): |
|
461 | 464 | """Toggle autoindent on/off (if available).""" |
|
462 | 465 | |
|
463 | 466 | self.shell.set_autoindent() |
|
464 | 467 | print "Automatic indentation is:",['OFF','ON'][self.shell.autoindent] |
|
465 | 468 | |
|
466 | 469 | def magic_system_verbose(self, parameter_s = ''): |
|
467 | 470 | """Toggle verbose printing of system calls on/off.""" |
|
468 | 471 | |
|
469 | 472 | self.shell.rc_set_toggle('system_verbose') |
|
470 | 473 | print "System verbose printing is:",\ |
|
471 | 474 | ['OFF','ON'][self.shell.rc.system_verbose] |
|
472 | 475 | |
|
473 | 476 | def magic_history(self, parameter_s = ''): |
|
474 | 477 | """Print input history (_i<n> variables), with most recent last. |
|
475 | 478 | |
|
476 | 479 | %history [-n] -> print at most 40 inputs (some may be multi-line)\\ |
|
477 | 480 | %history [-n] n -> print at most n inputs\\ |
|
478 | 481 | %history [-n] n1 n2 -> print inputs between n1 and n2 (n2 not included)\\ |
|
479 | 482 | |
|
480 | 483 | Each input's number <n> is shown, and is accessible as the |
|
481 | 484 | automatically generated variable _i<n>. Multi-line statements are |
|
482 | 485 | printed starting at a new line for easy copy/paste. |
|
483 | 486 | |
|
484 | 487 | If option -n is used, input numbers are not printed. This is useful if |
|
485 | 488 | you want to get a printout of many lines which can be directly pasted |
|
486 | 489 | into a text editor. |
|
487 | 490 | |
|
488 | 491 | This feature is only available if numbered prompts are in use.""" |
|
489 | 492 | |
|
490 | 493 | if not self.do_full_cache: |
|
491 | 494 | print 'This feature is only available if numbered prompts are in use.' |
|
492 | 495 | return |
|
493 | 496 | opts,args = self.parse_options(parameter_s,'n',mode='list') |
|
494 | 497 | |
|
495 | 498 | default_length = 40 |
|
496 | 499 | if len(args) == 0: |
|
497 | 500 | final = self.outputcache.prompt_count |
|
498 | 501 | init = max(1,final-default_length) |
|
499 | 502 | elif len(args) == 1: |
|
500 | 503 | final = self.outputcache.prompt_count |
|
501 | 504 | init = max(1,final-int(args[0])) |
|
502 | 505 | elif len(args) == 2: |
|
503 | 506 | init,final = map(int,args) |
|
504 | 507 | else: |
|
505 | 508 | warn('%hist takes 0, 1 or 2 arguments separated by spaces.') |
|
506 | 509 | print self.magic_hist.__doc__ |
|
507 | 510 | return |
|
508 | 511 | width = len(str(final)) |
|
509 | 512 | line_sep = ['','\n'] |
|
510 | 513 | input_hist = self.shell.input_hist |
|
511 | 514 | print_nums = not opts.has_key('n') |
|
512 | 515 | for in_num in range(init,final): |
|
513 | 516 | inline = input_hist[in_num] |
|
514 | 517 | multiline = inline.count('\n') > 1 |
|
515 | 518 | if print_nums: |
|
516 | 519 | print str(in_num).ljust(width)+':'+ line_sep[multiline], |
|
517 | 520 | if inline.startswith('#'+self.shell.ESC_MAGIC) or \ |
|
518 | 521 | inline.startswith('#!'): |
|
519 | 522 | print inline[1:], |
|
520 | 523 | else: |
|
521 | 524 | print inline, |
|
522 | 525 | |
|
523 | 526 | def magic_hist(self, parameter_s=''): |
|
524 | 527 | """Alternate name for %history.""" |
|
525 | 528 | return self.magic_history(parameter_s) |
|
526 | 529 | |
|
527 | 530 | def magic_p(self, parameter_s=''): |
|
528 | 531 | """Just a short alias for Python's 'print'.""" |
|
529 | 532 | exec 'print ' + parameter_s in self.shell.user_ns |
|
530 | 533 | |
|
531 | 534 | def magic_r(self, parameter_s=''): |
|
532 | 535 | """Repeat previous input. |
|
533 | 536 | |
|
534 | 537 | If given an argument, repeats the previous command which starts with |
|
535 | 538 | the same string, otherwise it just repeats the previous input. |
|
536 | 539 | |
|
537 | 540 | Shell escaped commands (with ! as first character) are not recognized |
|
538 | 541 | by this system, only pure python code and magic commands. |
|
539 | 542 | """ |
|
540 | 543 | |
|
541 | 544 | start = parameter_s.strip() |
|
542 | 545 | esc_magic = self.shell.ESC_MAGIC |
|
543 | 546 | # Identify magic commands even if automagic is on (which means |
|
544 | 547 | # the in-memory version is different from that typed by the user). |
|
545 | 548 | if self.shell.rc.automagic: |
|
546 | 549 | start_magic = esc_magic+start |
|
547 | 550 | else: |
|
548 | 551 | start_magic = start |
|
549 | 552 | # Look through the input history in reverse |
|
550 | 553 | for n in range(len(self.shell.input_hist)-2,0,-1): |
|
551 | 554 | input = self.shell.input_hist[n] |
|
552 | 555 | # skip plain 'r' lines so we don't recurse to infinity |
|
553 | 556 | if input != 'ipmagic("r")\n' and \ |
|
554 | 557 | (input.startswith(start) or input.startswith(start_magic)): |
|
555 | 558 | #print 'match',`input` # dbg |
|
556 | 559 | if input.startswith(esc_magic): |
|
557 | 560 | input = magic2python(input) |
|
558 | 561 | #print 'modified',`input` # dbg |
|
559 | 562 | print 'Executing:',input, |
|
560 | 563 | exec input in self.shell.user_ns |
|
561 | 564 | return |
|
562 | 565 | print 'No previous input matching `%s` found.' % start |
|
563 | 566 | |
|
564 | 567 | def magic_page(self, parameter_s=''): |
|
565 | 568 | """Pretty print the object and display it through a pager. |
|
566 | 569 | |
|
567 | 570 | If no parameter is given, use _ (last output).""" |
|
568 | 571 | # After a function contributed by Olivier Aubert, slightly modified. |
|
569 | 572 | |
|
570 | 573 | oname = parameter_s and parameter_s or '_' |
|
571 | 574 | info = self._ofind(oname) |
|
572 | 575 | if info['found']: |
|
573 | 576 | page(pformat(info['obj'])) |
|
574 | 577 | else: |
|
575 | 578 | print 'Object `%s` not found' % oname |
|
576 | 579 | |
|
577 | 580 | def magic_profile(self, parameter_s=''): |
|
578 | 581 | """Print your currently active IPyhton profile.""" |
|
579 | 582 | if self.shell.rc.profile: |
|
580 | 583 | printpl('Current IPython profile: $self.shell.rc.profile.') |
|
581 | 584 | else: |
|
582 | 585 | print 'No profile active.' |
|
583 | 586 | |
|
584 | 587 | def _inspect(self,meth,oname,**kw): |
|
585 | 588 | """Generic interface to the inspector system. |
|
586 | 589 | |
|
587 | 590 | This function is meant to be called by pdef, pdoc & friends.""" |
|
588 | 591 | |
|
589 | 592 | oname = oname.strip() |
|
590 | 593 | info = Struct(self._ofind(oname)) |
|
591 | 594 | if info.found: |
|
592 | 595 | pmethod = getattr(self.shell.inspector,meth) |
|
593 | 596 | formatter = info.ismagic and self.format_screen or None |
|
594 | 597 | if meth == 'pdoc': |
|
595 | 598 | pmethod(info.obj,oname,formatter) |
|
596 | 599 | elif meth == 'pinfo': |
|
597 | 600 | pmethod(info.obj,oname,formatter,info,**kw) |
|
598 | 601 | else: |
|
599 | 602 | pmethod(info.obj,oname) |
|
600 | 603 | else: |
|
601 | 604 | print 'Object `%s` not found.' % oname |
|
602 | 605 | return 'not found' # so callers can take other action |
|
603 | 606 | |
|
604 | 607 | def magic_pdef(self, parameter_s=''): |
|
605 | 608 | """Print the definition header for any callable object. |
|
606 | 609 | |
|
607 | 610 | If the object is a class, print the constructor information.""" |
|
608 | 611 | self._inspect('pdef',parameter_s) |
|
609 | 612 | |
|
610 | 613 | def magic_pdoc(self, parameter_s=''): |
|
611 | 614 | """Print the docstring for an object. |
|
612 | 615 | |
|
613 | 616 | If the given object is a class, it will print both the class and the |
|
614 | 617 | constructor docstrings.""" |
|
615 | 618 | self._inspect('pdoc',parameter_s) |
|
616 | 619 | |
|
617 | 620 | def magic_psource(self, parameter_s=''): |
|
618 | 621 | """Print (or run through pager) the source code for an object.""" |
|
619 | 622 | self._inspect('psource',parameter_s) |
|
620 | 623 | |
|
621 | 624 | def magic_pfile(self, parameter_s=''): |
|
622 | 625 | """Print (or run through pager) the file where an object is defined. |
|
623 | 626 | |
|
624 | 627 | The file opens at the line where the object definition begins. IPython |
|
625 | 628 | will honor the environment variable PAGER if set, and otherwise will |
|
626 | 629 | do its best to print the file in a convenient form. |
|
627 | 630 | |
|
628 | 631 | If the given argument is not an object currently defined, IPython will |
|
629 | 632 | try to interpret it as a filename (automatically adding a .py extension |
|
630 | 633 | if needed). You can thus use %pfile as a syntax highlighting code |
|
631 | 634 | viewer.""" |
|
632 | 635 | |
|
633 | 636 | # first interpret argument as an object name |
|
634 | 637 | out = self._inspect('pfile',parameter_s) |
|
635 | 638 | # if not, try the input as a filename |
|
636 | 639 | if out == 'not found': |
|
637 | 640 | try: |
|
638 | 641 | filename = get_py_filename(parameter_s) |
|
639 | 642 | except IOError,msg: |
|
640 | 643 | print msg |
|
641 | 644 | return |
|
642 | 645 | page(self.shell.inspector.format(file(filename).read())) |
|
643 | 646 | |
|
644 | 647 | def magic_pinfo(self, parameter_s=''): |
|
645 | 648 | """Provide detailed information about an object. |
|
646 | 649 | |
|
647 | 650 | '%pinfo object' is just a synonym for object? or ?object.""" |
|
648 | 651 | |
|
649 | 652 | #print 'pinfo par: <%s>' % parameter_s # dbg |
|
650 | 653 | |
|
651 | 654 | # detail_level: 0 -> obj? , 1 -> obj?? |
|
652 | 655 | detail_level = 0 |
|
653 | 656 | # We need to detect if we got called as 'pinfo pinfo foo', which can |
|
654 | 657 | # happen if the user types 'pinfo foo?' at the cmd line. |
|
655 | 658 | pinfo,qmark1,oname,qmark2 = \ |
|
656 | 659 | re.match('(pinfo )?(\?*)(.*?)(\??$)',parameter_s).groups() |
|
657 | 660 | if pinfo or qmark1 or qmark2: |
|
658 | 661 | detail_level = 1 |
|
659 | 662 | if "*" in oname: |
|
660 | 663 | self.magic_psearch(oname) |
|
661 | 664 | else: |
|
662 | 665 | self._inspect('pinfo',oname,detail_level=detail_level) |
|
663 | 666 | |
|
664 | 667 | def magic_psearch(self, parameter_s=''): |
|
665 | 668 | """Search for object in namespaces by wildcard. |
|
666 | 669 | |
|
667 | 670 | %psearch [options] PATTERN [OBJECT TYPE] |
|
668 | 671 | |
|
669 | 672 | Note: ? can be used as a synonym for %psearch, at the beginning or at |
|
670 | 673 | the end: both a*? and ?a* are equivalent to '%psearch a*'. Still, the |
|
671 | 674 | rest of the command line must be unchanged (options come first), so |
|
672 | 675 | for example the following forms are equivalent |
|
673 | 676 | |
|
674 | 677 | %psearch -i a* function |
|
675 | 678 | -i a* function? |
|
676 | 679 | ?-i a* function |
|
677 | 680 | |
|
678 | 681 | Arguments: |
|
679 | 682 | |
|
680 | 683 | PATTERN |
|
681 | 684 | |
|
682 | 685 | where PATTERN is a string containing * as a wildcard similar to its |
|
683 | 686 | use in a shell. The pattern is matched in all namespaces on the |
|
684 | 687 | search path. By default objects starting with a single _ are not |
|
685 | 688 | matched, many IPython generated objects have a single |
|
686 | 689 | underscore. The default is case insensitive matching. Matching is |
|
687 | 690 | also done on the attributes of objects and not only on the objects |
|
688 | 691 | in a module. |
|
689 | 692 | |
|
690 | 693 | [OBJECT TYPE] |
|
691 | 694 | |
|
692 | 695 | Is the name of a python type from the types module. The name is |
|
693 | 696 | given in lowercase without the ending type, ex. StringType is |
|
694 | 697 | written string. By adding a type here only objects matching the |
|
695 | 698 | given type are matched. Using all here makes the pattern match all |
|
696 | 699 | types (this is the default). |
|
697 | 700 | |
|
698 | 701 | Options: |
|
699 | 702 | |
|
700 | 703 | -a: makes the pattern match even objects whose names start with a |
|
701 | 704 | single underscore. These names are normally ommitted from the |
|
702 | 705 | search. |
|
703 | 706 | |
|
704 | 707 | -i/-c: make the pattern case insensitive/sensitive. If neither of |
|
705 | 708 | these options is given, the default is read from your ipythonrc |
|
706 | 709 | file. The option name which sets this value is |
|
707 | 710 | 'wildcards_case_sensitive'. If this option is not specified in your |
|
708 | 711 | ipythonrc file, IPython's internal default is to do a case sensitive |
|
709 | 712 | search. |
|
710 | 713 | |
|
711 | 714 | -e/-s NAMESPACE: exclude/search a given namespace. The pattern you |
|
712 | 715 | specifiy can be searched in any of the following namespaces: |
|
713 | 716 | 'builtin', 'user', 'user_global','internal', 'alias', where |
|
714 | 717 | 'builtin' and 'user' are the search defaults. Note that you should |
|
715 | 718 | not use quotes when specifying namespaces. |
|
716 | 719 | |
|
717 | 720 | 'Builtin' contains the python module builtin, 'user' contains all |
|
718 | 721 | user data, 'alias' only contain the shell aliases and no python |
|
719 | 722 | objects, 'internal' contains objects used by IPython. The |
|
720 | 723 | 'user_global' namespace is only used by embedded IPython instances, |
|
721 | 724 | and it contains module-level globals. You can add namespaces to the |
|
722 | 725 | search with -s or exclude them with -e (these options can be given |
|
723 | 726 | more than once). |
|
724 | 727 | |
|
725 | 728 | Examples: |
|
726 | 729 | |
|
727 | 730 | %psearch a* -> objects beginning with an a |
|
728 | 731 | %psearch -e builtin a* -> objects NOT in the builtin space starting in a |
|
729 | 732 | %psearch a* function -> all functions beginning with an a |
|
730 | 733 | %psearch re.e* -> objects beginning with an e in module re |
|
731 | 734 | %psearch r*.e* -> objects that start with e in modules starting in r |
|
732 | 735 | %psearch r*.* string -> all strings in modules beginning with r |
|
733 | 736 | |
|
734 | 737 | Case sensitve search: |
|
735 | 738 | |
|
736 | 739 | %psearch -c a* list all object beginning with lower case a |
|
737 | 740 | |
|
738 | 741 | Show objects beginning with a single _: |
|
739 | 742 | |
|
740 | 743 | %psearch -a _* list objects beginning with a single underscore""" |
|
741 | 744 | |
|
742 | 745 | # default namespaces to be searched |
|
743 | 746 | def_search = ['user','builtin'] |
|
744 | 747 | |
|
745 | 748 | # Process options/args |
|
746 | 749 | opts,args = self.parse_options(parameter_s,'cias:e:',list_all=True) |
|
747 | 750 | opt = opts.get |
|
748 | 751 | shell = self.shell |
|
749 | 752 | psearch = shell.inspector.psearch |
|
750 | 753 | |
|
751 | 754 | # select case options |
|
752 | 755 | if opts.has_key('i'): |
|
753 | 756 | ignore_case = True |
|
754 | 757 | elif opts.has_key('c'): |
|
755 | 758 | ignore_case = False |
|
756 | 759 | else: |
|
757 | 760 | ignore_case = not shell.rc.wildcards_case_sensitive |
|
758 | 761 | |
|
759 | 762 | # Build list of namespaces to search from user options |
|
760 | 763 | def_search.extend(opt('s',[])) |
|
761 | 764 | ns_exclude = ns_exclude=opt('e',[]) |
|
762 | 765 | ns_search = [nm for nm in def_search if nm not in ns_exclude] |
|
763 | 766 | |
|
764 | 767 | # Call the actual search |
|
765 | 768 | try: |
|
766 | 769 | psearch(args,shell.ns_table,ns_search, |
|
767 | 770 | show_all=opt('a'),ignore_case=ignore_case) |
|
768 | 771 | except: |
|
769 | 772 | shell.showtraceback() |
|
770 | 773 | |
|
771 | 774 | def magic_who_ls(self, parameter_s=''): |
|
772 | 775 | """Return a sorted list of all interactive variables. |
|
773 | 776 | |
|
774 | 777 | If arguments are given, only variables of types matching these |
|
775 | 778 | arguments are returned.""" |
|
776 | 779 | |
|
777 | 780 | user_ns = self.shell.user_ns |
|
778 | 781 | out = [] |
|
779 | 782 | typelist = parameter_s.split() |
|
780 | 783 | for i in self.shell.user_ns.keys(): |
|
781 | 784 | if not (i.startswith('_') or i.startswith('_i')) \ |
|
782 | 785 | and not (self.internal_ns.has_key(i) or |
|
783 | 786 | self.user_config_ns.has_key(i)): |
|
784 | 787 | if typelist: |
|
785 | 788 | if type(user_ns[i]).__name__ in typelist: |
|
786 | 789 | out.append(i) |
|
787 | 790 | else: |
|
788 | 791 | out.append(i) |
|
789 | 792 | out.sort() |
|
790 | 793 | return out |
|
791 | 794 | |
|
792 | 795 | def magic_who(self, parameter_s=''): |
|
793 | 796 | """Print all interactive variables, with some minimal formatting. |
|
794 | 797 | |
|
795 | 798 | If any arguments are given, only variables whose type matches one of |
|
796 | 799 | these are printed. For example: |
|
797 | 800 | |
|
798 | 801 | %who function str |
|
799 | 802 | |
|
800 | 803 | will only list functions and strings, excluding all other types of |
|
801 | 804 | variables. To find the proper type names, simply use type(var) at a |
|
802 | 805 | command line to see how python prints type names. For example: |
|
803 | 806 | |
|
804 | 807 | In [1]: type('hello')\\ |
|
805 | 808 | Out[1]: <type 'str'> |
|
806 | 809 | |
|
807 | 810 | indicates that the type name for strings is 'str'. |
|
808 | 811 | |
|
809 | 812 | %who always excludes executed names loaded through your configuration |
|
810 | 813 | file and things which are internal to IPython. |
|
811 | 814 | |
|
812 | 815 | This is deliberate, as typically you may load many modules and the |
|
813 | 816 | purpose of %who is to show you only what you've manually defined.""" |
|
814 | 817 | |
|
815 | 818 | varlist = self.magic_who_ls(parameter_s) |
|
816 | 819 | if not varlist: |
|
817 | 820 | print 'Interactive namespace is empty.' |
|
818 | 821 | return |
|
819 | 822 | |
|
820 | 823 | # if we have variables, move on... |
|
821 | 824 | |
|
822 | 825 | # stupid flushing problem: when prompts have no separators, stdout is |
|
823 | 826 | # getting lost. I'm starting to think this is a python bug. I'm having |
|
824 | 827 | # to force a flush with a print because even a sys.stdout.flush |
|
825 | 828 | # doesn't seem to do anything! |
|
826 | 829 | |
|
827 | 830 | count = 0 |
|
828 | 831 | for i in varlist: |
|
829 | 832 | print i+'\t', |
|
830 | 833 | count += 1 |
|
831 | 834 | if count > 8: |
|
832 | 835 | count = 0 |
|
833 | 836 | |
|
834 | 837 | sys.stdout.flush() # FIXME. Why the hell isn't this flushing??? |
|
835 | 838 | |
|
836 | 839 | print # well, this does force a flush at the expense of an extra \n |
|
837 | 840 | |
|
838 | 841 | def magic_whos(self, parameter_s=''): |
|
839 | 842 | """Like %who, but gives some extra information about each variable. |
|
840 | 843 | |
|
841 | 844 | The same type filtering of %who can be applied here. |
|
842 | 845 | |
|
843 | 846 | For all variables, the type is printed. Additionally it prints: |
|
844 | 847 | |
|
845 | 848 | - For {},[],(): their length. |
|
846 | 849 | |
|
847 | 850 | - For Numeric arrays, a summary with shape, number of elements, |
|
848 | 851 | typecode and size in memory. |
|
849 | 852 | |
|
850 | 853 | - Everything else: a string representation, snipping their middle if |
|
851 | 854 | too long.""" |
|
852 | 855 | |
|
853 | 856 | varnames = self.magic_who_ls(parameter_s) |
|
854 | 857 | if not varnames: |
|
855 | 858 | print 'Interactive namespace is empty.' |
|
856 | 859 | return |
|
857 | 860 | |
|
858 | 861 | # if we have variables, move on... |
|
859 | 862 | |
|
860 | 863 | # for these types, show len() instead of data: |
|
861 | 864 | seq_types = [types.DictType,types.ListType,types.TupleType] |
|
862 | 865 | |
|
863 | 866 | # for Numeric arrays, display summary info |
|
864 | 867 | try: |
|
865 | 868 | import Numeric |
|
866 | 869 | except ImportError: |
|
867 | 870 | array_type = None |
|
868 | 871 | else: |
|
869 | 872 | array_type = Numeric.ArrayType.__name__ |
|
870 | 873 | |
|
871 | 874 | # Find all variable names and types so we can figure out column sizes |
|
872 | 875 | get_vars = lambda i: self.shell.user_ns[i] |
|
873 | 876 | type_name = lambda v: type(v).__name__ |
|
874 | 877 | varlist = map(get_vars,varnames) |
|
875 | 878 | typelist = map(type_name,varlist) |
|
876 | 879 | # column labels and # of spaces as separator |
|
877 | 880 | varlabel = 'Variable' |
|
878 | 881 | typelabel = 'Type' |
|
879 | 882 | datalabel = 'Data/Info' |
|
880 | 883 | colsep = 3 |
|
881 | 884 | # variable format strings |
|
882 | 885 | vformat = "$vname.ljust(varwidth)$vtype.ljust(typewidth)" |
|
883 | 886 | vfmt_short = '$vstr[:25]<...>$vstr[-25:]' |
|
884 | 887 | aformat = "%s: %s elems, type `%s`, %s bytes" |
|
885 | 888 | # find the size of the columns to format the output nicely |
|
886 | 889 | varwidth = max(max(map(len,varnames)), len(varlabel)) + colsep |
|
887 | 890 | typewidth = max(max(map(len,typelist)), len(typelabel)) + colsep |
|
888 | 891 | # table header |
|
889 | 892 | print varlabel.ljust(varwidth) + typelabel.ljust(typewidth) + \ |
|
890 | 893 | ' '+datalabel+'\n' + '-'*(varwidth+typewidth+len(datalabel)+1) |
|
891 | 894 | # and the table itself |
|
892 | 895 | kb = 1024 |
|
893 | 896 | Mb = 1048576 # kb**2 |
|
894 | 897 | for vname,var,vtype in zip(varnames,varlist,typelist): |
|
895 | 898 | print itpl(vformat), |
|
896 | 899 | if vtype in seq_types: |
|
897 | 900 | print len(var) |
|
898 | 901 | elif vtype==array_type: |
|
899 | 902 | vshape = str(var.shape).replace(',','').replace(' ','x')[1:-1] |
|
900 | 903 | vsize = Numeric.size(var) |
|
901 | 904 | vbytes = vsize*var.itemsize() |
|
902 | 905 | if vbytes < 100000: |
|
903 | 906 | print aformat % (vshape,vsize,var.typecode(),vbytes) |
|
904 | 907 | else: |
|
905 | 908 | print aformat % (vshape,vsize,var.typecode(),vbytes), |
|
906 | 909 | if vbytes < Mb: |
|
907 | 910 | print '(%s kb)' % (vbytes/kb,) |
|
908 | 911 | else: |
|
909 | 912 | print '(%s Mb)' % (vbytes/Mb,) |
|
910 | 913 | else: |
|
911 | 914 | vstr = str(var) |
|
912 | 915 | if len(vstr) < 50: |
|
913 | 916 | print vstr |
|
914 | 917 | else: |
|
915 | 918 | printpl(vfmt_short) |
|
916 | 919 | |
|
917 | 920 | def magic_reset(self, parameter_s=''): |
|
918 | 921 | """Resets the namespace by removing all names defined by the user. |
|
919 | 922 | |
|
920 | 923 | Input/Output history are left around in case you need them.""" |
|
921 | 924 | |
|
922 | 925 | ans = raw_input( |
|
923 | 926 | "Once deleted, variables cannot be recovered. Proceed (y/n)? ") |
|
924 | 927 | if not ans.lower() == 'y': |
|
925 | 928 | print 'Nothing done.' |
|
926 | 929 | return |
|
927 | 930 | user_ns = self.shell.user_ns |
|
928 | 931 | for i in self.magic_who_ls(): |
|
929 | 932 | del(user_ns[i]) |
|
930 | 933 | |
|
931 | 934 | def magic_config(self,parameter_s=''): |
|
932 | 935 | """Show IPython's internal configuration.""" |
|
933 | 936 | |
|
934 | 937 | page('Current configuration structure:\n'+ |
|
935 | 938 | pformat(self.shell.rc.dict())) |
|
936 | 939 | |
|
937 | 940 | def magic_logstart(self,parameter_s=''): |
|
938 | 941 | """Start logging anywhere in a session. |
|
939 | 942 | |
|
940 | 943 | %logstart [log_name [log_mode]] |
|
941 | 944 | |
|
942 | 945 | If no name is given, it defaults to a file named 'ipython.log' in your |
|
943 | 946 | current directory, in 'rotate' mode (see below). |
|
944 | 947 | |
|
945 | 948 | '%logstart name' saves to file 'name' in 'backup' mode. It saves your |
|
946 | 949 | history up to that point and then continues logging. |
|
947 | 950 | |
|
948 | 951 | %logstart takes a second optional parameter: logging mode. This can be one |
|
949 | 952 | of (note that the modes are given unquoted):\\ |
|
950 | 953 | over: overwrite existing log.\\ |
|
951 | 954 | backup: rename (if exists) to name~ and start name.\\ |
|
952 | 955 | append: well, that says it.\\ |
|
953 | 956 | rotate: create rotating logs name.1~, name.2~, etc. |
|
954 | 957 | """ |
|
955 | 958 | |
|
956 | 959 | #FIXME. This function should all be moved to the Logger class. |
|
957 | 960 | |
|
958 | 961 | valid_modes = qw('over backup append rotate') |
|
959 | 962 | if self.LOG: |
|
960 | 963 | print 'Logging is already in place. Logfile:',self.LOG |
|
961 | 964 | return |
|
962 | 965 | |
|
963 | 966 | par = parameter_s.strip() |
|
964 | 967 | if not par: |
|
965 | 968 | logname = self.LOGDEF |
|
966 | 969 | logmode = 'rotate' # use rotate for the auto-generated logs |
|
967 | 970 | else: |
|
968 | 971 | try: |
|
969 | 972 | logname,logmode = par.split() |
|
970 | 973 | except: |
|
971 | 974 | try: |
|
972 | 975 | logname = par |
|
973 | 976 | logmode = 'backup' |
|
974 | 977 | except: |
|
975 | 978 | warn('Usage: %log [log_name [log_mode]]') |
|
976 | 979 | return |
|
977 | 980 | if not logmode in valid_modes: |
|
978 | 981 | warn('Logging NOT activated.\n' |
|
979 | 982 | 'Usage: %log [log_name [log_mode]]\n' |
|
980 | 983 | 'Valid modes: '+str(valid_modes)) |
|
981 | 984 | return |
|
982 | 985 | |
|
983 | 986 | # If we made it this far, I think we're ok: |
|
984 | 987 | print 'Activating auto-logging.' |
|
985 | 988 | print 'Current session state plus future input saved to:',logname |
|
986 | 989 | print 'Logging mode: ',logmode |
|
987 | 990 | # put logname into rc struct as if it had been called on the command line, |
|
988 | 991 | # so it ends up saved in the log header |
|
989 | 992 | # Save it in case we need to restore it... |
|
990 | 993 | old_logfile = self.shell.rc.opts.get('logfile','') |
|
991 | 994 | logname = os.path.expanduser(logname) |
|
992 | 995 | self.shell.rc.opts.logfile = logname |
|
993 | 996 | self.LOGMODE = logmode # FIXME: this should be set through a function. |
|
994 | 997 | try: |
|
995 | 998 | header = str(self.LOGHEAD) |
|
996 | 999 | self.create_log(header,logname) |
|
997 | 1000 | self.logstart(header,logname) |
|
998 | 1001 | except: |
|
999 | 1002 | self.LOG = '' # we are NOT logging, something went wrong |
|
1000 | 1003 | self.shell.rc.opts.logfile = old_logfile |
|
1001 | 1004 | warn("Couldn't start log: "+str(sys.exc_info()[1])) |
|
1002 | 1005 | else: # log input history up to this point |
|
1003 | 1006 | self.logfile.write(self.shell.user_ns['_ih'][1:]) |
|
1004 | 1007 | self.logfile.flush() |
|
1005 | 1008 | |
|
1006 | 1009 | def magic_logoff(self,parameter_s=''): |
|
1007 | 1010 | """Temporarily stop logging. |
|
1008 | 1011 | |
|
1009 | 1012 | You must have previously started logging.""" |
|
1010 | 1013 | self.switch_log(0) |
|
1011 | 1014 | |
|
1012 | 1015 | def magic_logon(self,parameter_s=''): |
|
1013 | 1016 | """Restart logging. |
|
1014 | 1017 | |
|
1015 | 1018 | This function is for restarting logging which you've temporarily |
|
1016 | 1019 | stopped with %logoff. For starting logging for the first time, you |
|
1017 | 1020 | must use the %logstart function, which allows you to specify an |
|
1018 | 1021 | optional log filename.""" |
|
1019 | 1022 | |
|
1020 | 1023 | self.switch_log(1) |
|
1021 | 1024 | |
|
1022 | 1025 | def magic_logstate(self,parameter_s=''): |
|
1023 | 1026 | """Print the status of the logging system.""" |
|
1024 | 1027 | |
|
1025 | 1028 | self.logstate() |
|
1026 | 1029 | |
|
1027 | 1030 | def magic_pdb(self, parameter_s=''): |
|
1028 | 1031 | """Control the calling of the pdb interactive debugger. |
|
1029 | 1032 | |
|
1030 | 1033 | Call as '%pdb on', '%pdb 1', '%pdb off' or '%pdb 0'. If called without |
|
1031 | 1034 | argument it works as a toggle. |
|
1032 | 1035 | |
|
1033 | 1036 | When an exception is triggered, IPython can optionally call the |
|
1034 | 1037 | interactive pdb debugger after the traceback printout. %pdb toggles |
|
1035 | 1038 | this feature on and off.""" |
|
1036 | 1039 | |
|
1037 | 1040 | par = parameter_s.strip().lower() |
|
1038 | 1041 | |
|
1039 | 1042 | if par: |
|
1040 | 1043 | try: |
|
1041 | 1044 | pdb = {'off':0,'0':0,'on':1,'1':1}[par] |
|
1042 | 1045 | except KeyError: |
|
1043 | 1046 | print 'Incorrect argument. Use on/1, off/0 or nothing for a toggle.' |
|
1044 | 1047 | return |
|
1045 | 1048 | else: |
|
1046 | 1049 | self.shell.InteractiveTB.call_pdb = pdb |
|
1047 | 1050 | else: |
|
1048 | 1051 | self.shell.InteractiveTB.call_pdb = 1 - self.shell.InteractiveTB.call_pdb |
|
1049 | 1052 | print 'Automatic pdb calling has been turned',\ |
|
1050 | 1053 | on_off(self.shell.InteractiveTB.call_pdb) |
|
1051 | 1054 | |
|
1052 | 1055 | |
|
1053 | 1056 | def magic_prun(self, parameter_s ='',user_mode=1, |
|
1054 | 1057 | opts=None,arg_lst=None,prog_ns=None): |
|
1055 | 1058 | |
|
1056 | 1059 | """Run a statement through the python code profiler. |
|
1057 | 1060 | |
|
1058 | 1061 | Usage:\\ |
|
1059 | 1062 | %prun [options] statement |
|
1060 | 1063 | |
|
1061 | 1064 | The given statement (which doesn't require quote marks) is run via the |
|
1062 | 1065 | python profiler in a manner similar to the profile.run() function. |
|
1063 | 1066 | Namespaces are internally managed to work correctly; profile.run |
|
1064 | 1067 | cannot be used in IPython because it makes certain assumptions about |
|
1065 | 1068 | namespaces which do not hold under IPython. |
|
1066 | 1069 | |
|
1067 | 1070 | Options: |
|
1068 | 1071 | |
|
1069 | 1072 | -l <limit>: you can place restrictions on what or how much of the |
|
1070 | 1073 | profile gets printed. The limit value can be: |
|
1071 | 1074 | |
|
1072 | 1075 | * A string: only information for function names containing this string |
|
1073 | 1076 | is printed. |
|
1074 | 1077 | |
|
1075 | 1078 | * An integer: only these many lines are printed. |
|
1076 | 1079 | |
|
1077 | 1080 | * A float (between 0 and 1): this fraction of the report is printed |
|
1078 | 1081 | (for example, use a limit of 0.4 to see the topmost 40% only). |
|
1079 | 1082 | |
|
1080 | 1083 | You can combine several limits with repeated use of the option. For |
|
1081 | 1084 | example, '-l __init__ -l 5' will print only the topmost 5 lines of |
|
1082 | 1085 | information about class constructors. |
|
1083 | 1086 | |
|
1084 | 1087 | -r: return the pstats.Stats object generated by the profiling. This |
|
1085 | 1088 | object has all the information about the profile in it, and you can |
|
1086 | 1089 | later use it for further analysis or in other functions. |
|
1087 | 1090 | |
|
1088 | 1091 | Since magic functions have a particular form of calling which prevents |
|
1089 | 1092 | you from writing something like:\\ |
|
1090 | 1093 | In [1]: p = %prun -r print 4 # invalid!\\ |
|
1091 | 1094 | you must instead use IPython's automatic variables to assign this:\\ |
|
1092 | 1095 | In [1]: %prun -r print 4 \\ |
|
1093 | 1096 | Out[1]: <pstats.Stats instance at 0x8222cec>\\ |
|
1094 | 1097 | In [2]: stats = _ |
|
1095 | 1098 | |
|
1096 | 1099 | If you really need to assign this value via an explicit function call, |
|
1097 | 1100 | you can always tap directly into the true name of the magic function |
|
1098 | 1101 | by using the ipmagic function (which IPython automatically adds to the |
|
1099 | 1102 | builtins):\\ |
|
1100 | 1103 | In [3]: stats = ipmagic('prun','-r print 4') |
|
1101 | 1104 | |
|
1102 | 1105 | You can type ipmagic? for more details on ipmagic. |
|
1103 | 1106 | |
|
1104 | 1107 | -s <key>: sort profile by given key. You can provide more than one key |
|
1105 | 1108 | by using the option several times: '-s key1 -s key2 -s key3...'. The |
|
1106 | 1109 | default sorting key is 'time'. |
|
1107 | 1110 | |
|
1108 | 1111 | The following is copied verbatim from the profile documentation |
|
1109 | 1112 | referenced below: |
|
1110 | 1113 | |
|
1111 | 1114 | When more than one key is provided, additional keys are used as |
|
1112 | 1115 | secondary criteria when the there is equality in all keys selected |
|
1113 | 1116 | before them. |
|
1114 | 1117 | |
|
1115 | 1118 | Abbreviations can be used for any key names, as long as the |
|
1116 | 1119 | abbreviation is unambiguous. The following are the keys currently |
|
1117 | 1120 | defined: |
|
1118 | 1121 | |
|
1119 | 1122 | Valid Arg Meaning\\ |
|
1120 | 1123 | "calls" call count\\ |
|
1121 | 1124 | "cumulative" cumulative time\\ |
|
1122 | 1125 | "file" file name\\ |
|
1123 | 1126 | "module" file name\\ |
|
1124 | 1127 | "pcalls" primitive call count\\ |
|
1125 | 1128 | "line" line number\\ |
|
1126 | 1129 | "name" function name\\ |
|
1127 | 1130 | "nfl" name/file/line\\ |
|
1128 | 1131 | "stdname" standard name\\ |
|
1129 | 1132 | "time" internal time |
|
1130 | 1133 | |
|
1131 | 1134 | Note that all sorts on statistics are in descending order (placing |
|
1132 | 1135 | most time consuming items first), where as name, file, and line number |
|
1133 | 1136 | searches are in ascending order (i.e., alphabetical). The subtle |
|
1134 | 1137 | distinction between "nfl" and "stdname" is that the standard name is a |
|
1135 | 1138 | sort of the name as printed, which means that the embedded line |
|
1136 | 1139 | numbers get compared in an odd way. For example, lines 3, 20, and 40 |
|
1137 | 1140 | would (if the file names were the same) appear in the string order |
|
1138 | 1141 | "20" "3" and "40". In contrast, "nfl" does a numeric compare of the |
|
1139 | 1142 | line numbers. In fact, sort_stats("nfl") is the same as |
|
1140 | 1143 | sort_stats("name", "file", "line"). |
|
1141 | 1144 | |
|
1142 | 1145 | -T <filename>: save profile results as shown on screen to a text |
|
1143 | 1146 | file. The profile is still shown on screen. |
|
1144 | 1147 | |
|
1145 | 1148 | -D <filename>: save (via dump_stats) profile statistics to given |
|
1146 | 1149 | filename. This data is in a format understod by the pstats module, and |
|
1147 | 1150 | is generated by a call to the dump_stats() method of profile |
|
1148 | 1151 | objects. The profile is still shown on screen. |
|
1149 | 1152 | |
|
1150 | 1153 | If you want to run complete programs under the profiler's control, use |
|
1151 | 1154 | '%run -p [prof_opts] filename.py [args to program]' where prof_opts |
|
1152 | 1155 | contains profiler specific options as described here. |
|
1153 | 1156 | |
|
1154 | 1157 | You can read the complete documentation for the profile module with:\\ |
|
1155 | 1158 | In [1]: import profile; profile.help() """ |
|
1156 | 1159 | |
|
1157 | 1160 | opts_def = Struct(D=[''],l=[],s=['time'],T=['']) |
|
1158 | 1161 | # protect user quote marks |
|
1159 | 1162 | parameter_s = parameter_s.replace('"',r'\"').replace("'",r"\'") |
|
1160 | 1163 | |
|
1161 | 1164 | if user_mode: # regular user call |
|
1162 | 1165 | opts,arg_str = self.parse_options(parameter_s,'D:l:rs:T:', |
|
1163 | 1166 | list_all=1) |
|
1164 | 1167 | namespace = self.shell.user_ns |
|
1165 | 1168 | else: # called to run a program by %run -p |
|
1166 | 1169 | try: |
|
1167 | 1170 | filename = get_py_filename(arg_lst[0]) |
|
1168 | 1171 | except IOError,msg: |
|
1169 | 1172 | error(msg) |
|
1170 | 1173 | return |
|
1171 | 1174 | |
|
1172 | 1175 | arg_str = 'execfile(filename,prog_ns)' |
|
1173 | 1176 | namespace = locals() |
|
1174 | 1177 | |
|
1175 | 1178 | opts.merge(opts_def) |
|
1176 | 1179 | |
|
1177 | 1180 | prof = profile.Profile() |
|
1178 | 1181 | try: |
|
1179 | 1182 | prof = prof.runctx(arg_str,namespace,namespace) |
|
1180 | 1183 | sys_exit = '' |
|
1181 | 1184 | except SystemExit: |
|
1182 | 1185 | sys_exit = """*** SystemExit exception caught in code being profiled.""" |
|
1183 | 1186 | |
|
1184 | 1187 | stats = pstats.Stats(prof).strip_dirs().sort_stats(*opts.s) |
|
1185 | 1188 | |
|
1186 | 1189 | lims = opts.l |
|
1187 | 1190 | if lims: |
|
1188 | 1191 | lims = [] # rebuild lims with ints/floats/strings |
|
1189 | 1192 | for lim in opts.l: |
|
1190 | 1193 | try: |
|
1191 | 1194 | lims.append(int(lim)) |
|
1192 | 1195 | except ValueError: |
|
1193 | 1196 | try: |
|
1194 | 1197 | lims.append(float(lim)) |
|
1195 | 1198 | except ValueError: |
|
1196 | 1199 | lims.append(lim) |
|
1197 | 1200 | |
|
1198 | 1201 | # trap output |
|
1199 | 1202 | sys_stdout = sys.stdout |
|
1200 | 1203 | stdout_trap = StringIO() |
|
1201 | 1204 | try: |
|
1202 | 1205 | sys.stdout = stdout_trap |
|
1203 | 1206 | stats.print_stats(*lims) |
|
1204 | 1207 | finally: |
|
1205 | 1208 | sys.stdout = sys_stdout |
|
1206 | 1209 | output = stdout_trap.getvalue() |
|
1207 | 1210 | output = output.rstrip() |
|
1208 | 1211 | |
|
1209 | 1212 | page(output,screen_lines=self.shell.rc.screen_length) |
|
1210 | 1213 | print sys_exit, |
|
1211 | 1214 | |
|
1212 | 1215 | dump_file = opts.D[0] |
|
1213 | 1216 | text_file = opts.T[0] |
|
1214 | 1217 | if dump_file: |
|
1215 | 1218 | prof.dump_stats(dump_file) |
|
1216 | 1219 | print '\n*** Profile stats marshalled to file',\ |
|
1217 | 1220 | `dump_file`+'.',sys_exit |
|
1218 | 1221 | if text_file: |
|
1219 | 1222 | file(text_file,'w').write(output) |
|
1220 | 1223 | print '\n*** Profile printout saved to text file',\ |
|
1221 | 1224 | `text_file`+'.',sys_exit |
|
1222 | 1225 | |
|
1223 | 1226 | if opts.has_key('r'): |
|
1224 | 1227 | return stats |
|
1225 | 1228 | else: |
|
1226 | 1229 | return None |
|
1227 | 1230 | |
|
1228 | 1231 | def magic_run(self, parameter_s ='',runner=None): |
|
1229 | 1232 | """Run the named file inside IPython as a program. |
|
1230 | 1233 | |
|
1231 | 1234 | Usage:\\ |
|
1232 | 1235 | %run [-n -i -t [-N<N>] -d [-b<N>] -p [profile options]] file [args] |
|
1233 | 1236 | |
|
1234 | 1237 | Parameters after the filename are passed as command-line arguments to |
|
1235 | 1238 | the program (put in sys.argv). Then, control returns to IPython's |
|
1236 | 1239 | prompt. |
|
1237 | 1240 | |
|
1238 | 1241 | This is similar to running at a system prompt:\\ |
|
1239 | 1242 | $ python file args\\ |
|
1240 | 1243 | but with the advantage of giving you IPython's tracebacks, and of |
|
1241 | 1244 | loading all variables into your interactive namespace for further use |
|
1242 | 1245 | (unless -p is used, see below). |
|
1243 | 1246 | |
|
1244 | 1247 | The file is executed in a namespace initially consisting only of |
|
1245 | 1248 | __name__=='__main__' and sys.argv constructed as indicated. It thus |
|
1246 | 1249 | sees its environment as if it were being run as a stand-alone |
|
1247 | 1250 | program. But after execution, the IPython interactive namespace gets |
|
1248 | 1251 | updated with all variables defined in the program (except for __name__ |
|
1249 | 1252 | and sys.argv). This allows for very convenient loading of code for |
|
1250 | 1253 | interactive work, while giving each program a 'clean sheet' to run in. |
|
1251 | 1254 | |
|
1252 | 1255 | Options: |
|
1253 | 1256 | |
|
1254 | 1257 | -n: __name__ is NOT set to '__main__', but to the running file's name |
|
1255 | 1258 | without extension (as python does under import). This allows running |
|
1256 | 1259 | scripts and reloading the definitions in them without calling code |
|
1257 | 1260 | protected by an ' if __name__ == "__main__" ' clause. |
|
1258 | 1261 | |
|
1259 | 1262 | -i: run the file in IPython's namespace instead of an empty one. This |
|
1260 | 1263 | is useful if you are experimenting with code written in a text editor |
|
1261 | 1264 | which depends on variables defined interactively. |
|
1262 | 1265 | |
|
1263 | 1266 | -e: ignore sys.exit() calls or SystemExit exceptions in the script |
|
1264 | 1267 | being run. This is particularly useful if IPython is being used to |
|
1265 | 1268 | run unittests, which always exit with a sys.exit() call. In such |
|
1266 | 1269 | cases you are interested in the output of the test results, not in |
|
1267 | 1270 | seeing a traceback of the unittest module. |
|
1268 | 1271 | |
|
1269 | 1272 | -t: print timing information at the end of the run. IPython will give |
|
1270 | 1273 | you an estimated CPU time consumption for your script, which under |
|
1271 | 1274 | Unix uses the resource module to avoid the wraparound problems of |
|
1272 | 1275 | time.clock(). Under Unix, an estimate of time spent on system tasks |
|
1273 | 1276 | is also given (for Windows platforms this is reported as 0.0). |
|
1274 | 1277 | |
|
1275 | 1278 | If -t is given, an additional -N<N> option can be given, where <N> |
|
1276 | 1279 | must be an integer indicating how many times you want the script to |
|
1277 | 1280 | run. The final timing report will include total and per run results. |
|
1278 | 1281 | |
|
1279 | 1282 | For example (testing the script uniq_stable.py): |
|
1280 | 1283 | |
|
1281 | 1284 | In [1]: run -t uniq_stable |
|
1282 | 1285 | |
|
1283 | 1286 | IPython CPU timings (estimated):\\ |
|
1284 | 1287 | User : 0.19597 s.\\ |
|
1285 | 1288 | System: 0.0 s.\\ |
|
1286 | 1289 | |
|
1287 | 1290 | In [2]: run -t -N5 uniq_stable |
|
1288 | 1291 | |
|
1289 | 1292 | IPython CPU timings (estimated):\\ |
|
1290 | 1293 | Total runs performed: 5\\ |
|
1291 | 1294 | Times : Total Per run\\ |
|
1292 | 1295 | User : 0.910862 s, 0.1821724 s.\\ |
|
1293 | 1296 | System: 0.0 s, 0.0 s. |
|
1294 | 1297 | |
|
1295 | 1298 | -d: run your program under the control of pdb, the Python debugger. |
|
1296 | 1299 | This allows you to execute your program step by step, watch variables, |
|
1297 | 1300 | etc. Internally, what IPython does is similar to calling: |
|
1298 | 1301 | |
|
1299 | 1302 | pdb.run('execfile("YOURFILENAME")') |
|
1300 | 1303 | |
|
1301 | 1304 | with a breakpoint set on line 1 of your file. You can change the line |
|
1302 | 1305 | number for this automatic breakpoint to be <N> by using the -bN option |
|
1303 | 1306 | (where N must be an integer). For example: |
|
1304 | 1307 | |
|
1305 | 1308 | %run -d -b40 myscript |
|
1306 | 1309 | |
|
1307 | 1310 | will set the first breakpoint at line 40 in myscript.py. Note that |
|
1308 | 1311 | the first breakpoint must be set on a line which actually does |
|
1309 | 1312 | something (not a comment or docstring) for it to stop execution. |
|
1310 | 1313 | |
|
1311 | 1314 | When the pdb debugger starts, you will see a (Pdb) prompt. You must |
|
1312 | 1315 | first enter 'c' (without qoutes) to start execution up to the first |
|
1313 | 1316 | breakpoint. |
|
1314 | 1317 | |
|
1315 | 1318 | Entering 'help' gives information about the use of the debugger. You |
|
1316 | 1319 | can easily see pdb's full documentation with "import pdb;pdb.help()" |
|
1317 | 1320 | at a prompt. |
|
1318 | 1321 | |
|
1319 | 1322 | -p: run program under the control of the Python profiler module (which |
|
1320 | 1323 | prints a detailed report of execution times, function calls, etc). |
|
1321 | 1324 | |
|
1322 | 1325 | You can pass other options after -p which affect the behavior of the |
|
1323 | 1326 | profiler itself. See the docs for %prun for details. |
|
1324 | 1327 | |
|
1325 | 1328 | In this mode, the program's variables do NOT propagate back to the |
|
1326 | 1329 | IPython interactive namespace (because they remain in the namespace |
|
1327 | 1330 | where the profiler executes them). |
|
1328 | 1331 | |
|
1329 | 1332 | Internally this triggers a call to %prun, see its documentation for |
|
1330 | 1333 | details on the options available specifically for profiling.""" |
|
1331 | 1334 | |
|
1332 | 1335 | # get arguments and set sys.argv for program to be run. |
|
1333 | 1336 | opts,arg_lst = self.parse_options(parameter_s,'nidtN:b:pD:l:rs:T:e', |
|
1334 | 1337 | mode='list',list_all=1) |
|
1335 | 1338 | |
|
1336 | 1339 | try: |
|
1337 | 1340 | filename = get_py_filename(arg_lst[0]) |
|
1338 | 1341 | except IndexError: |
|
1339 | 1342 | warn('you must provide at least a filename.') |
|
1340 | 1343 | print '\n%run:\n',OInspect.getdoc(self.magic_run) |
|
1341 | 1344 | return |
|
1342 | 1345 | except IOError,msg: |
|
1343 | 1346 | error(msg) |
|
1344 | 1347 | return |
|
1345 | 1348 | |
|
1346 | 1349 | # Control the response to exit() calls made by the script being run |
|
1347 | 1350 | exit_ignore = opts.has_key('e') |
|
1348 | 1351 | |
|
1349 | 1352 | # Make sure that the running script gets a proper sys.argv as if it |
|
1350 | 1353 | # were run from a system shell. |
|
1351 | 1354 | save_argv = sys.argv # save it for later restoring |
|
1352 | 1355 | sys.argv = [filename]+ arg_lst[1:] # put in the proper filename |
|
1353 | 1356 | |
|
1354 | 1357 | if opts.has_key('i'): |
|
1355 | 1358 | prog_ns = self.shell.user_ns |
|
1356 | 1359 | __name__save = self.shell.user_ns['__name__'] |
|
1357 | 1360 | prog_ns['__name__'] = '__main__' |
|
1358 | 1361 | else: |
|
1359 | 1362 | if opts.has_key('n'): |
|
1360 | 1363 | name = os.path.splitext(os.path.basename(filename))[0] |
|
1361 | 1364 | else: |
|
1362 | 1365 | name = '__main__' |
|
1363 | 1366 | prog_ns = {'__name__':name} |
|
1364 | 1367 | |
|
1365 | 1368 | # pickle fix. See iplib for an explanation |
|
1366 | 1369 | sys.modules[prog_ns['__name__']] = FakeModule(prog_ns) |
|
1367 | 1370 | |
|
1368 | 1371 | stats = None |
|
1369 | 1372 | try: |
|
1370 | 1373 | if opts.has_key('p'): |
|
1371 | 1374 | stats = self.magic_prun('',0,opts,arg_lst,prog_ns) |
|
1372 | 1375 | else: |
|
1373 | 1376 | if opts.has_key('d'): |
|
1374 |
deb = |
|
|
1377 | deb = Debugger.Pdb(self.shell.rc.colors) | |
|
1375 | 1378 | # reset Breakpoint state, which is moronically kept |
|
1376 | 1379 | # in a class |
|
1377 | 1380 | bdb.Breakpoint.next = 1 |
|
1378 | 1381 | bdb.Breakpoint.bplist = {} |
|
1379 | 1382 | bdb.Breakpoint.bpbynumber = [None] |
|
1380 | 1383 | # Set an initial breakpoint to stop execution |
|
1381 | 1384 | maxtries = 10 |
|
1382 | 1385 | bp = int(opts.get('b',[1])[0]) |
|
1383 | 1386 | checkline = deb.checkline(filename,bp) |
|
1384 | 1387 | if not checkline: |
|
1385 | 1388 | for bp in range(bp+1,bp+maxtries+1): |
|
1386 | 1389 | if deb.checkline(filename,bp): |
|
1387 | 1390 | break |
|
1388 | 1391 | else: |
|
1389 | 1392 | msg = ("\nI failed to find a valid line to set " |
|
1390 | 1393 | "a breakpoint\n" |
|
1391 | 1394 | "after trying up to line: %s.\n" |
|
1392 | 1395 | "Please set a valid breakpoint manually " |
|
1393 | 1396 | "with the -b option." % bp) |
|
1394 | 1397 | error(msg) |
|
1395 | 1398 | return |
|
1396 | 1399 | # if we find a good linenumber, set the breakpoint |
|
1397 | 1400 | deb.do_break('%s:%s' % (filename,bp)) |
|
1398 | 1401 | # Start file run |
|
1399 | 1402 | print "NOTE: Enter 'c' at the", |
|
1400 |
print " |
|
|
1401 | deb.run('execfile("%s")' % filename,prog_ns) | |
|
1403 | print "ipdb> prompt to start your script." | |
|
1404 | try: | |
|
1405 | deb.run('execfile("%s")' % filename,prog_ns) | |
|
1406 | except: | |
|
1407 | etype, value, tb = sys.exc_info() | |
|
1408 | # Skip three frames in the traceback: the %run one, | |
|
1409 | # one inside bdb.py, and the command-line typed by the | |
|
1410 | # user (run by exec in pdb itself). | |
|
1411 | self.shell.InteractiveTB(etype,value,tb,tb_offset=3) | |
|
1402 | 1412 | else: |
|
1403 | 1413 | if runner is None: |
|
1404 | 1414 | runner = self.shell.safe_execfile |
|
1405 | 1415 | if opts.has_key('t'): |
|
1406 | 1416 | try: |
|
1407 | 1417 | nruns = int(opts['N'][0]) |
|
1408 | 1418 | if nruns < 1: |
|
1409 | 1419 | error('Number of runs must be >=1') |
|
1410 | 1420 | return |
|
1411 | 1421 | except (KeyError): |
|
1412 | 1422 | nruns = 1 |
|
1413 | 1423 | if nruns == 1: |
|
1414 | 1424 | t0 = clock2() |
|
1415 | 1425 | runner(filename,prog_ns,prog_ns,exit_ignore=exit_ignore) |
|
1416 | 1426 | t1 = clock2() |
|
1417 | 1427 | t_usr = t1[0]-t0[0] |
|
1418 | 1428 | t_sys = t1[1]-t1[1] |
|
1419 | 1429 | print "\nIPython CPU timings (estimated):" |
|
1420 | 1430 | print " User : %10s s." % t_usr |
|
1421 | 1431 | print " System: %10s s." % t_sys |
|
1422 | 1432 | else: |
|
1423 | 1433 | runs = range(nruns) |
|
1424 | 1434 | t0 = clock2() |
|
1425 | 1435 | for nr in runs: |
|
1426 | 1436 | runner(filename,prog_ns,prog_ns,exit_ignore=exit_ignore) |
|
1427 | 1437 | t1 = clock2() |
|
1428 | 1438 | t_usr = t1[0]-t0[0] |
|
1429 | 1439 | t_sys = t1[1]-t1[1] |
|
1430 | 1440 | print "\nIPython CPU timings (estimated):" |
|
1431 | 1441 | print "Total runs performed:",nruns |
|
1432 | 1442 | print " Times : %10s %10s" % ('Total','Per run') |
|
1433 | 1443 | print " User : %10s s, %10s s." % (t_usr,t_usr/nruns) |
|
1434 | 1444 | print " System: %10s s, %10s s." % (t_sys,t_sys/nruns) |
|
1435 | 1445 | |
|
1436 | 1446 | else: |
|
1437 | 1447 | runner(filename,prog_ns,prog_ns,exit_ignore=exit_ignore) |
|
1438 | 1448 | if opts.has_key('i'): |
|
1439 | 1449 | self.shell.user_ns['__name__'] = __name__save |
|
1440 | 1450 | else: |
|
1441 | 1451 | # update IPython interactive namespace |
|
1442 | 1452 | del prog_ns['__name__'] |
|
1443 | 1453 | self.shell.user_ns.update(prog_ns) |
|
1444 | 1454 | finally: |
|
1445 | 1455 | sys.argv = save_argv |
|
1446 | 1456 | return stats |
|
1447 | 1457 | |
|
1448 | 1458 | def magic_runlog(self, parameter_s =''): |
|
1449 | 1459 | """Run files as logs. |
|
1450 | 1460 | |
|
1451 | 1461 | Usage:\\ |
|
1452 | 1462 | %runlog file1 file2 ... |
|
1453 | 1463 | |
|
1454 | 1464 | Run the named files (treating them as log files) in sequence inside |
|
1455 | 1465 | the interpreter, and return to the prompt. This is much slower than |
|
1456 | 1466 | %run because each line is executed in a try/except block, but it |
|
1457 | 1467 | allows running files with syntax errors in them. |
|
1458 | 1468 | |
|
1459 | 1469 | Normally IPython will guess when a file is one of its own logfiles, so |
|
1460 | 1470 | you can typically use %run even for logs. This shorthand allows you to |
|
1461 | 1471 | force any file to be treated as a log file.""" |
|
1462 | 1472 | |
|
1463 | 1473 | for f in parameter_s.split(): |
|
1464 | 1474 | self.shell.safe_execfile(f,self.shell.user_ns, |
|
1465 | 1475 | self.shell.user_ns,islog=1) |
|
1466 | 1476 | |
|
1467 | 1477 | def magic_time(self,parameter_s = ''): |
|
1468 | 1478 | """Time execution of a Python statement or expression. |
|
1469 | 1479 | |
|
1470 | 1480 | The CPU and wall clock times are printed, and the value of the |
|
1471 | 1481 | expression (if any) is returned. Note that under Win32, system time |
|
1472 | 1482 | is always reported as 0, since it can not be measured. |
|
1473 | 1483 | |
|
1474 | 1484 | This function provides very basic timing functionality. In Python |
|
1475 | 1485 | 2.3, the timeit module offers more control and sophistication, but for |
|
1476 | 1486 | now IPython supports Python 2.2, so we can not rely on timeit being |
|
1477 | 1487 | present. |
|
1478 | 1488 | |
|
1479 | 1489 | Some examples: |
|
1480 | 1490 | |
|
1481 | 1491 | In [1]: time 2**128 |
|
1482 | 1492 | CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s |
|
1483 | 1493 | Wall time: 0.00 |
|
1484 | 1494 | Out[1]: 340282366920938463463374607431768211456L |
|
1485 | 1495 | |
|
1486 | 1496 | In [2]: n = 1000000 |
|
1487 | 1497 | |
|
1488 | 1498 | In [3]: time sum(range(n)) |
|
1489 | 1499 | CPU times: user 1.20 s, sys: 0.05 s, total: 1.25 s |
|
1490 | 1500 | Wall time: 1.37 |
|
1491 | 1501 | Out[3]: 499999500000L |
|
1492 | 1502 | |
|
1493 | 1503 | In [4]: time print 'hello world' |
|
1494 | 1504 | hello world |
|
1495 | 1505 | CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s |
|
1496 | 1506 | Wall time: 0.00 |
|
1497 | 1507 | """ |
|
1498 | 1508 | |
|
1499 | 1509 | # fail immediately if the given expression can't be compiled |
|
1500 | 1510 | try: |
|
1501 | 1511 | mode = 'eval' |
|
1502 | 1512 | code = compile(parameter_s,'<timed eval>',mode) |
|
1503 | 1513 | except SyntaxError: |
|
1504 | 1514 | mode = 'exec' |
|
1505 | 1515 | code = compile(parameter_s,'<timed exec>',mode) |
|
1506 | 1516 | # skew measurement as little as possible |
|
1507 | 1517 | glob = self.shell.user_ns |
|
1508 | 1518 | clk = clock2 |
|
1509 | 1519 | wtime = time.time |
|
1510 | 1520 | # time execution |
|
1511 | 1521 | wall_st = wtime() |
|
1512 | 1522 | if mode=='eval': |
|
1513 | 1523 | st = clk() |
|
1514 | 1524 | out = eval(code,glob) |
|
1515 | 1525 | end = clk() |
|
1516 | 1526 | else: |
|
1517 | 1527 | st = clk() |
|
1518 | 1528 | exec code in glob |
|
1519 | 1529 | end = clk() |
|
1520 | 1530 | out = None |
|
1521 | 1531 | wall_end = wtime() |
|
1522 | 1532 | # Compute actual times and report |
|
1523 | 1533 | wall_time = wall_end-wall_st |
|
1524 | 1534 | cpu_user = end[0]-st[0] |
|
1525 | 1535 | cpu_sys = end[1]-st[1] |
|
1526 | 1536 | cpu_tot = cpu_user+cpu_sys |
|
1527 | 1537 | print "CPU times: user %.2f s, sys: %.2f s, total: %.2f s" % \ |
|
1528 | 1538 | (cpu_user,cpu_sys,cpu_tot) |
|
1529 | 1539 | print "Wall time: %.2f" % wall_time |
|
1530 | 1540 | return out |
|
1531 | 1541 | |
|
1532 | 1542 | def magic_macro(self,parameter_s = ''): |
|
1533 | 1543 | """Define a set of input lines as a macro for future re-execution. |
|
1534 | 1544 | |
|
1535 | 1545 | Usage:\\ |
|
1536 | 1546 | %macro name n1:n2 n3:n4 ... n5 .. n6 ... |
|
1537 | 1547 | |
|
1538 | 1548 | This will define a global variable called `name` which is a string |
|
1539 | 1549 | made of joining the slices and lines you specify (n1,n2,... numbers |
|
1540 | 1550 | above) from your input history into a single string. This variable |
|
1541 | 1551 | acts like an automatic function which re-executes those lines as if |
|
1542 | 1552 | you had typed them. You just type 'name' at the prompt and the code |
|
1543 | 1553 | executes. |
|
1544 | 1554 | |
|
1545 | 1555 | Note that the slices use the standard Python slicing notation (5:8 |
|
1546 | 1556 | means include lines numbered 5,6,7). |
|
1547 | 1557 | |
|
1548 | 1558 | For example, if your history contains (%hist prints it): |
|
1549 | 1559 | |
|
1550 | 1560 | 44: x=1\\ |
|
1551 | 1561 | 45: y=3\\ |
|
1552 | 1562 | 46: z=x+y\\ |
|
1553 | 1563 | 47: print x\\ |
|
1554 | 1564 | 48: a=5\\ |
|
1555 | 1565 | 49: print 'x',x,'y',y\\ |
|
1556 | 1566 | |
|
1557 | 1567 | you can create a macro with lines 44 through 47 (included) and line 49 |
|
1558 | 1568 | called my_macro with: |
|
1559 | 1569 | |
|
1560 | 1570 | In [51]: %macro my_macro 44:48 49 |
|
1561 | 1571 | |
|
1562 | 1572 | Now, typing `my_macro` (without quotes) will re-execute all this code |
|
1563 | 1573 | in one pass. |
|
1564 | 1574 | |
|
1565 | 1575 | You don't need to give the line-numbers in order, and any given line |
|
1566 | 1576 | number can appear multiple times. You can assemble macros with any |
|
1567 | 1577 | lines from your input history in any order. |
|
1568 | 1578 | |
|
1569 | 1579 | The macro is a simple object which holds its value in an attribute, |
|
1570 | 1580 | but IPython's display system checks for macros and executes them as |
|
1571 | 1581 | code instead of printing them when you type their name. |
|
1572 | 1582 | |
|
1573 | 1583 | You can view a macro's contents by explicitly printing it with: |
|
1574 | 1584 | |
|
1575 | 1585 | 'print macro_name'. |
|
1576 | 1586 | |
|
1577 | 1587 | For one-off cases which DON'T contain magic function calls in them you |
|
1578 | 1588 | can obtain similar results by explicitly executing slices from your |
|
1579 | 1589 | input history with: |
|
1580 | 1590 | |
|
1581 | 1591 | In [60]: exec In[44:48]+In[49]""" |
|
1582 | 1592 | |
|
1583 | 1593 | args = parameter_s.split() |
|
1584 | 1594 | name,ranges = args[0], args[1:] |
|
1585 | 1595 | #print 'rng',ranges # dbg |
|
1586 | 1596 | cmds = self.extract_input_slices(ranges) |
|
1587 | 1597 | macro = Macro(cmds) |
|
1588 | 1598 | self.shell.user_ns.update({name:macro}) |
|
1589 | 1599 | print 'Macro `%s` created. To execute, type its name (without quotes).' % name |
|
1590 | 1600 | print 'Macro contents:' |
|
1591 | 1601 | print str(macro).rstrip(), |
|
1592 | 1602 | |
|
1593 | 1603 | def magic_save(self,parameter_s = ''): |
|
1594 | 1604 | """Save a set of lines to a given filename. |
|
1595 | 1605 | |
|
1596 | 1606 | Usage:\\ |
|
1597 | 1607 | %save filename n1:n2 n3:n4 ... n5 .. n6 ... |
|
1598 | 1608 | |
|
1599 | 1609 | This function uses the same syntax as %macro for line extraction, but |
|
1600 | 1610 | instead of creating a macro it saves the resulting string to the |
|
1601 | 1611 | filename you specify. |
|
1602 | 1612 | |
|
1603 | 1613 | It adds a '.py' extension to the file if you don't do so yourself, and |
|
1604 | 1614 | it asks for confirmation before overwriting existing files.""" |
|
1605 | 1615 | |
|
1606 | 1616 | args = parameter_s.split() |
|
1607 | 1617 | fname,ranges = args[0], args[1:] |
|
1608 | 1618 | if not fname.endswith('.py'): |
|
1609 | 1619 | fname += '.py' |
|
1610 | 1620 | if os.path.isfile(fname): |
|
1611 | 1621 | ans = raw_input('File `%s` exists. Overwrite (y/[N])? ' % fname) |
|
1612 | 1622 | if ans.lower() not in ['y','yes']: |
|
1613 | 1623 | print 'Operation cancelled.' |
|
1614 | 1624 | return |
|
1615 | 1625 | cmds = ''.join(self.extract_input_slices(ranges)) |
|
1616 | 1626 | f = file(fname,'w') |
|
1617 | 1627 | f.write(cmds) |
|
1618 | 1628 | f.close() |
|
1619 | 1629 | print 'The following commands were written to file `%s`:' % fname |
|
1620 | 1630 | print cmds |
|
1621 | 1631 | |
|
1622 | 1632 | def magic_ed(self,parameter_s = ''): |
|
1623 | 1633 | """Alias to %edit.""" |
|
1624 | 1634 | return self.magic_edit(parameter_s) |
|
1625 | 1635 | |
|
1626 | 1636 | def magic_edit(self,parameter_s = '',last_call=['','']): |
|
1627 | 1637 | """Bring up an editor and execute the resulting code. |
|
1628 | 1638 | |
|
1629 | 1639 | Usage: |
|
1630 | 1640 | %edit [options] [args] |
|
1631 | 1641 | |
|
1632 | 1642 | %edit runs IPython's editor hook. The default version of this hook is |
|
1633 | 1643 | set to call the __IPYTHON__.rc.editor command. This is read from your |
|
1634 | 1644 | environment variable $EDITOR. If this isn't found, it will default to |
|
1635 | 1645 | vi under Linux/Unix and to notepad under Windows. See the end of this |
|
1636 | 1646 | docstring for how to change the editor hook. |
|
1637 | 1647 | |
|
1638 | 1648 | You can also set the value of this editor via the command line option |
|
1639 | 1649 | '-editor' or in your ipythonrc file. This is useful if you wish to use |
|
1640 | 1650 | specifically for IPython an editor different from your typical default |
|
1641 | 1651 | (and for Windows users who typically don't set environment variables). |
|
1642 | 1652 | |
|
1643 | 1653 | This command allows you to conveniently edit multi-line code right in |
|
1644 | 1654 | your IPython session. |
|
1645 | 1655 | |
|
1646 | 1656 | If called without arguments, %edit opens up an empty editor with a |
|
1647 | 1657 | temporary file and will execute the contents of this file when you |
|
1648 | 1658 | close it (don't forget to save it!). |
|
1649 | 1659 | |
|
1650 | 1660 | Options: |
|
1651 | 1661 | |
|
1652 | 1662 | -p: this will call the editor with the same data as the previous time |
|
1653 | 1663 | it was used, regardless of how long ago (in your current session) it |
|
1654 | 1664 | was. |
|
1655 | 1665 | |
|
1656 | 1666 | -x: do not execute the edited code immediately upon exit. This is |
|
1657 | 1667 | mainly useful if you are editing programs which need to be called with |
|
1658 | 1668 | command line arguments, which you can then do using %run. |
|
1659 | 1669 | |
|
1660 | 1670 | Arguments: |
|
1661 | 1671 | |
|
1662 | 1672 | If arguments are given, the following possibilites exist: |
|
1663 | 1673 | |
|
1664 | 1674 | - The arguments are numbers or pairs of colon-separated numbers (like |
|
1665 | 1675 | 1 4:8 9). These are interpreted as lines of previous input to be |
|
1666 | 1676 | loaded into the editor. The syntax is the same of the %macro command. |
|
1667 | 1677 | |
|
1668 | 1678 | - If the argument doesn't start with a number, it is evaluated as a |
|
1669 | 1679 | variable and its contents loaded into the editor. You can thus edit |
|
1670 | 1680 | any string which contains python code (including the result of |
|
1671 | 1681 | previous edits). |
|
1672 | 1682 | |
|
1673 | 1683 | - If the argument is the name of an object (other than a string), |
|
1674 | 1684 | IPython will try to locate the file where it was defined and open the |
|
1675 | 1685 | editor at the point where it is defined. You can use `%edit function` |
|
1676 | 1686 | to load an editor exactly at the point where 'function' is defined, |
|
1677 | 1687 | edit it and have the file be executed automatically. |
|
1678 | 1688 | |
|
1679 | 1689 | Note: opening at an exact line is only supported under Unix, and some |
|
1680 | 1690 | editors (like kedit and gedit up to Gnome 2.8) do not understand the |
|
1681 | 1691 | '+NUMBER' parameter necessary for this feature. Good editors like |
|
1682 | 1692 | (X)Emacs, vi, jed, pico and joe all do. |
|
1683 | 1693 | |
|
1684 | 1694 | - If the argument is not found as a variable, IPython will look for a |
|
1685 | 1695 | file with that name (adding .py if necessary) and load it into the |
|
1686 | 1696 | editor. It will execute its contents with execfile() when you exit, |
|
1687 | 1697 | loading any code in the file into your interactive namespace. |
|
1688 | 1698 | |
|
1689 | 1699 | After executing your code, %edit will return as output the code you |
|
1690 | 1700 | typed in the editor (except when it was an existing file). This way |
|
1691 | 1701 | you can reload the code in further invocations of %edit as a variable, |
|
1692 | 1702 | via _<NUMBER> or Out[<NUMBER>], where <NUMBER> is the prompt number of |
|
1693 | 1703 | the output. |
|
1694 | 1704 | |
|
1695 | 1705 | Note that %edit is also available through the alias %ed. |
|
1696 | 1706 | |
|
1697 | 1707 | This is an example of creating a simple function inside the editor and |
|
1698 | 1708 | then modifying it. First, start up the editor: |
|
1699 | 1709 | |
|
1700 | 1710 | In [1]: ed\\ |
|
1701 | 1711 | Editing... done. Executing edited code...\\ |
|
1702 | 1712 | Out[1]: 'def foo():\\n print "foo() was defined in an editing session"\\n' |
|
1703 | 1713 | |
|
1704 | 1714 | We can then call the function foo(): |
|
1705 | 1715 | |
|
1706 | 1716 | In [2]: foo()\\ |
|
1707 | 1717 | foo() was defined in an editing session |
|
1708 | 1718 | |
|
1709 | 1719 | Now we edit foo. IPython automatically loads the editor with the |
|
1710 | 1720 | (temporary) file where foo() was previously defined: |
|
1711 | 1721 | |
|
1712 | 1722 | In [3]: ed foo\\ |
|
1713 | 1723 | Editing... done. Executing edited code... |
|
1714 | 1724 | |
|
1715 | 1725 | And if we call foo() again we get the modified version: |
|
1716 | 1726 | |
|
1717 | 1727 | In [4]: foo()\\ |
|
1718 | 1728 | foo() has now been changed! |
|
1719 | 1729 | |
|
1720 | 1730 | Here is an example of how to edit a code snippet successive |
|
1721 | 1731 | times. First we call the editor: |
|
1722 | 1732 | |
|
1723 | 1733 | In [8]: ed\\ |
|
1724 | 1734 | Editing... done. Executing edited code...\\ |
|
1725 | 1735 | hello\\ |
|
1726 | 1736 | Out[8]: "print 'hello'\\n" |
|
1727 | 1737 | |
|
1728 | 1738 | Now we call it again with the previous output (stored in _): |
|
1729 | 1739 | |
|
1730 | 1740 | In [9]: ed _\\ |
|
1731 | 1741 | Editing... done. Executing edited code...\\ |
|
1732 | 1742 | hello world\\ |
|
1733 | 1743 | Out[9]: "print 'hello world'\\n" |
|
1734 | 1744 | |
|
1735 | 1745 | Now we call it with the output #8 (stored in _8, also as Out[8]): |
|
1736 | 1746 | |
|
1737 | 1747 | In [10]: ed _8\\ |
|
1738 | 1748 | Editing... done. Executing edited code...\\ |
|
1739 | 1749 | hello again\\ |
|
1740 | 1750 | Out[10]: "print 'hello again'\\n" |
|
1741 | 1751 | |
|
1742 | 1752 | |
|
1743 | 1753 | Changing the default editor hook: |
|
1744 | 1754 | |
|
1745 | 1755 | If you wish to write your own editor hook, you can put it in a |
|
1746 | 1756 | configuration file which you load at startup time. The default hook |
|
1747 | 1757 | is defined in the IPython.hooks module, and you can use that as a |
|
1748 | 1758 | starting example for further modifications. That file also has |
|
1749 | 1759 | general instructions on how to set a new hook for use once you've |
|
1750 | 1760 | defined it.""" |
|
1751 | 1761 | |
|
1752 | 1762 | # FIXME: This function has become a convoluted mess. It needs a |
|
1753 | 1763 | # ground-up rewrite with clean, simple logic. |
|
1754 | 1764 | |
|
1755 | 1765 | def make_filename(arg): |
|
1756 | 1766 | "Make a filename from the given args" |
|
1757 | 1767 | try: |
|
1758 | 1768 | filename = get_py_filename(arg) |
|
1759 | 1769 | except IOError: |
|
1760 | 1770 | if args.endswith('.py'): |
|
1761 | 1771 | filename = arg |
|
1762 | 1772 | else: |
|
1763 | 1773 | filename = None |
|
1764 | 1774 | return filename |
|
1765 | 1775 | |
|
1766 | 1776 | # custom exceptions |
|
1767 | 1777 | class DataIsObject(Exception): pass |
|
1768 | 1778 | |
|
1769 | 1779 | opts,args = self.parse_options(parameter_s,'px') |
|
1770 | 1780 | |
|
1771 | 1781 | # Default line number value |
|
1772 | 1782 | lineno = None |
|
1773 | 1783 | if opts.has_key('p'): |
|
1774 | 1784 | args = '_%s' % last_call[0] |
|
1775 | 1785 | if not self.shell.user_ns.has_key(args): |
|
1776 | 1786 | args = last_call[1] |
|
1777 | 1787 | |
|
1778 | 1788 | # use last_call to remember the state of the previous call, but don't |
|
1779 | 1789 | # let it be clobbered by successive '-p' calls. |
|
1780 | 1790 | try: |
|
1781 | 1791 | last_call[0] = self.shell.outputcache.prompt_count |
|
1782 | 1792 | if not opts.has_key('p'): |
|
1783 | 1793 | last_call[1] = parameter_s |
|
1784 | 1794 | except: |
|
1785 | 1795 | pass |
|
1786 | 1796 | |
|
1787 | 1797 | # by default this is done with temp files, except when the given |
|
1788 | 1798 | # arg is a filename |
|
1789 | 1799 | use_temp = 1 |
|
1790 | 1800 | |
|
1791 | 1801 | if re.match(r'\d',args): |
|
1792 | 1802 | # Mode where user specifies ranges of lines, like in %macro. |
|
1793 | 1803 | # This means that you can't edit files whose names begin with |
|
1794 | 1804 | # numbers this way. Tough. |
|
1795 | 1805 | ranges = args.split() |
|
1796 | 1806 | data = ''.join(self.extract_input_slices(ranges)) |
|
1797 | 1807 | elif args.endswith('.py'): |
|
1798 | 1808 | filename = make_filename(args) |
|
1799 | 1809 | data = '' |
|
1800 | 1810 | use_temp = 0 |
|
1801 | 1811 | elif args: |
|
1802 | 1812 | try: |
|
1803 | 1813 | # Load the parameter given as a variable. If not a string, |
|
1804 | 1814 | # process it as an object instead (below) |
|
1805 | 1815 | |
|
1806 | 1816 | #print '*** args',args,'type',type(args) # dbg |
|
1807 | 1817 | data = eval(args,self.shell.user_ns) |
|
1808 | 1818 | if not type(data) in StringTypes: |
|
1809 | 1819 | raise DataIsObject |
|
1810 | 1820 | except (NameError,SyntaxError): |
|
1811 | 1821 | # given argument is not a variable, try as a filename |
|
1812 | 1822 | filename = make_filename(args) |
|
1813 | 1823 | if filename is None: |
|
1814 | 1824 | warn("Argument given (%s) can't be found as a variable " |
|
1815 | 1825 | "or as a filename." % args) |
|
1816 | 1826 | return |
|
1817 | 1827 | data = '' |
|
1818 | 1828 | use_temp = 0 |
|
1819 | 1829 | except DataIsObject: |
|
1820 | 1830 | # For objects, try to edit the file where they are defined |
|
1821 | 1831 | try: |
|
1822 | 1832 | filename = inspect.getabsfile(data) |
|
1823 | 1833 | datafile = 1 |
|
1824 | 1834 | except TypeError: |
|
1825 | 1835 | filename = make_filename(args) |
|
1826 | 1836 | datafile = 1 |
|
1827 | 1837 | warn('Could not find file where `%s` is defined.\n' |
|
1828 | 1838 | 'Opening a file named `%s`' % (args,filename)) |
|
1829 | 1839 | # Now, make sure we can actually read the source (if it was in |
|
1830 | 1840 | # a temp file it's gone by now). |
|
1831 | 1841 | if datafile: |
|
1832 | 1842 | try: |
|
1833 | 1843 | lineno = inspect.getsourcelines(data)[1] |
|
1834 | 1844 | except IOError: |
|
1835 | 1845 | filename = make_filename(args) |
|
1836 | 1846 | if filename is None: |
|
1837 | 1847 | warn('The file `%s` where `%s` was defined cannot ' |
|
1838 | 1848 | 'be read.' % (filename,data)) |
|
1839 | 1849 | return |
|
1840 | 1850 | use_temp = 0 |
|
1841 | 1851 | else: |
|
1842 | 1852 | data = '' |
|
1843 | 1853 | |
|
1844 | 1854 | if use_temp: |
|
1845 | 1855 | filename = tempfile.mktemp('.py') |
|
1846 | 1856 | self.shell.tempfiles.append(filename) |
|
1847 | 1857 | |
|
1848 | 1858 | if data and use_temp: |
|
1849 | 1859 | tmp_file = open(filename,'w') |
|
1850 | 1860 | tmp_file.write(data) |
|
1851 | 1861 | tmp_file.close() |
|
1852 | 1862 | |
|
1853 | 1863 | # do actual editing here |
|
1854 | 1864 | print 'Editing...', |
|
1855 | 1865 | sys.stdout.flush() |
|
1856 | 1866 | self.shell.hooks.editor(filename,lineno) |
|
1857 | 1867 | if opts.has_key('x'): # -x prevents actual execution |
|
1858 | 1868 | |
|
1859 | 1869 | else: |
|
1860 | 1870 | print 'done. Executing edited code...' |
|
1861 | 1871 | try: |
|
1862 | 1872 | execfile(filename,self.shell.user_ns) |
|
1863 | 1873 | except IOError,msg: |
|
1864 | 1874 | if msg.filename == filename: |
|
1865 | 1875 | warn('File not found. Did you forget to save?') |
|
1866 | 1876 | return |
|
1867 | 1877 | else: |
|
1868 | 1878 | self.shell.showtraceback() |
|
1869 | 1879 | except: |
|
1870 | 1880 | self.shell.showtraceback() |
|
1871 | 1881 | if use_temp: |
|
1872 | 1882 | contents = open(filename).read() |
|
1873 | 1883 | return contents |
|
1874 | 1884 | |
|
1875 | 1885 | def magic_xmode(self,parameter_s = ''): |
|
1876 | 1886 | """Switch modes for the exception handlers. |
|
1877 | 1887 | |
|
1878 | 1888 | Valid modes: Plain, Context and Verbose. |
|
1879 | 1889 | |
|
1880 | 1890 | If called without arguments, acts as a toggle.""" |
|
1881 | 1891 | |
|
1882 | 1892 | new_mode = parameter_s.strip().capitalize() |
|
1883 | 1893 | try: |
|
1884 | 1894 | self.InteractiveTB.set_mode(mode = new_mode) |
|
1885 | 1895 | print 'Exception reporting mode:',self.InteractiveTB.mode |
|
1886 | 1896 | except: |
|
1887 | 1897 | warn('Error changing exception modes.\n' + str(sys.exc_info()[1])) |
|
1888 | 1898 | |
|
1889 | 1899 | def magic_colors(self,parameter_s = ''): |
|
1890 | 1900 | """Switch color scheme for prompts, info system and exception handlers. |
|
1891 | 1901 | |
|
1892 | 1902 | Currently implemented schemes: NoColor, Linux, LightBG. |
|
1893 | 1903 | |
|
1894 | 1904 | Color scheme names are not case-sensitive.""" |
|
1895 | 1905 | |
|
1896 | 1906 | new_scheme = parameter_s.strip() |
|
1897 | 1907 | if not new_scheme: |
|
1898 | 1908 | print 'You must specify a color scheme.' |
|
1899 | 1909 | return |
|
1900 | 1910 | # Under Windows, check for Gary Bishop's readline, which is necessary |
|
1901 | 1911 | # for ANSI coloring |
|
1902 | 1912 | if os.name in ['nt','dos']: |
|
1903 | 1913 | try: |
|
1904 | 1914 | import readline |
|
1905 | 1915 | except ImportError: |
|
1906 | 1916 | has_readline = 0 |
|
1907 | 1917 | else: |
|
1908 | 1918 | try: |
|
1909 | 1919 | readline.GetOutputFile() |
|
1910 | 1920 | except AttributeError: |
|
1911 | 1921 | has_readline = 0 |
|
1912 | 1922 | else: |
|
1913 | 1923 | has_readline = 1 |
|
1914 | 1924 | if not has_readline: |
|
1915 | 1925 | msg = """\ |
|
1916 | 1926 | Proper color support under MS Windows requires Gary Bishop's readline library. |
|
1917 | 1927 | You can find it at: |
|
1918 | 1928 | http://sourceforge.net/projects/uncpythontools |
|
1919 | 1929 | Gary's readline needs the ctypes module, from: |
|
1920 | 1930 | http://starship.python.net/crew/theller/ctypes |
|
1921 | 1931 | |
|
1922 | 1932 | Defaulting color scheme to 'NoColor'""" |
|
1923 | 1933 | new_scheme = 'NoColor' |
|
1924 | 1934 | warn(msg) |
|
1925 | 1935 | |
|
1926 | 1936 | # Set prompt colors |
|
1927 | 1937 | try: |
|
1928 | 1938 | self.shell.outputcache.set_colors(new_scheme) |
|
1929 | 1939 | except: |
|
1930 | 1940 | warn('Error changing prompt color schemes.\n' |
|
1931 | 1941 | + str(sys.exc_info()[1])) |
|
1932 | 1942 | else: |
|
1933 | 1943 | self.shell.rc.colors = \ |
|
1934 | 1944 | self.shell.outputcache.color_table.active_scheme_name |
|
1935 | 1945 | # Set exception colors |
|
1936 | 1946 | try: |
|
1937 | 1947 | self.shell.InteractiveTB.set_colors(scheme = new_scheme) |
|
1938 | 1948 | self.shell.SyntaxTB.set_colors(scheme = new_scheme) |
|
1939 | 1949 | except: |
|
1940 | 1950 | warn('Error changing exception color schemes.\n' |
|
1941 | 1951 | + str(sys.exc_info()[1])) |
|
1942 | 1952 | # Set info (for 'object?') colors |
|
1943 | 1953 | if self.shell.rc.color_info: |
|
1944 | 1954 | try: |
|
1945 | 1955 | self.shell.inspector.set_active_scheme(new_scheme) |
|
1946 | 1956 | except: |
|
1947 | 1957 | warn('Error changing object inspector color schemes.\n' |
|
1948 | 1958 | + str(sys.exc_info()[1])) |
|
1949 | 1959 | else: |
|
1950 | 1960 | self.shell.inspector.set_active_scheme('NoColor') |
|
1951 | 1961 | |
|
1952 | 1962 | def magic_color_info(self,parameter_s = ''): |
|
1953 | 1963 | """Toggle color_info. |
|
1954 | 1964 | |
|
1955 | 1965 | The color_info configuration parameter controls whether colors are |
|
1956 | 1966 | used for displaying object details (by things like %psource, %pfile or |
|
1957 | 1967 | the '?' system). This function toggles this value with each call. |
|
1958 | 1968 | |
|
1959 | 1969 | Note that unless you have a fairly recent pager (less works better |
|
1960 | 1970 | than more) in your system, using colored object information displays |
|
1961 | 1971 | will not work properly. Test it and see.""" |
|
1962 | 1972 | |
|
1963 | 1973 | self.shell.rc.color_info = 1 - self.shell.rc.color_info |
|
1964 | 1974 | self.magic_colors(self.shell.rc.colors) |
|
1965 | 1975 | print 'Object introspection functions have now coloring:', |
|
1966 | 1976 | print ['OFF','ON'][self.shell.rc.color_info] |
|
1967 | 1977 | |
|
1968 | 1978 | def magic_Pprint(self, parameter_s=''): |
|
1969 | 1979 | """Toggle pretty printing on/off.""" |
|
1970 | 1980 | |
|
1971 | 1981 | self.shell.outputcache.Pprint = 1 - self.shell.outputcache.Pprint |
|
1972 | 1982 | print 'Pretty printing has been turned', \ |
|
1973 | 1983 | ['OFF','ON'][self.shell.outputcache.Pprint] |
|
1974 | 1984 | |
|
1975 | 1985 | def magic_Exit(self, parameter_s=''): |
|
1976 | 1986 | """Exit IPython without confirmation.""" |
|
1977 | 1987 | |
|
1978 | 1988 | self.shell.exit_now = True |
|
1979 | 1989 | |
|
1980 | 1990 | def magic_Quit(self, parameter_s=''): |
|
1981 | 1991 | """Exit IPython without confirmation (like %Exit).""" |
|
1982 | 1992 | |
|
1983 | 1993 | self.shell.exit_now = True |
|
1984 | 1994 | |
|
1985 | 1995 | #...................................................................... |
|
1986 | 1996 | # Functions to implement unix shell-type things |
|
1987 | 1997 | |
|
1988 | 1998 | def magic_alias(self, parameter_s = ''): |
|
1989 | 1999 | """Define an alias for a system command. |
|
1990 | 2000 | |
|
1991 | 2001 | '%alias alias_name cmd' defines 'alias_name' as an alias for 'cmd' |
|
1992 | 2002 | |
|
1993 | 2003 | Then, typing 'alias_name params' will execute the system command 'cmd |
|
1994 | 2004 | params' (from your underlying operating system). |
|
1995 | 2005 | |
|
1996 | 2006 | Aliases have lower precedence than magic functions and Python normal |
|
1997 | 2007 | variables, so if 'foo' is both a Python variable and an alias, the |
|
1998 | 2008 | alias can not be executed until 'del foo' removes the Python variable. |
|
1999 | 2009 | |
|
2000 | 2010 | You can use the %l specifier in an alias definition to represent the |
|
2001 | 2011 | whole line when the alias is called. For example: |
|
2002 | 2012 | |
|
2003 | 2013 | In [2]: alias all echo "Input in brackets: <%l>"\\ |
|
2004 | 2014 | In [3]: all hello world\\ |
|
2005 | 2015 | Input in brackets: <hello world> |
|
2006 | 2016 | |
|
2007 | 2017 | You can also define aliases with parameters using %s specifiers (one |
|
2008 | 2018 | per parameter): |
|
2009 | 2019 | |
|
2010 | 2020 | In [1]: alias parts echo first %s second %s\\ |
|
2011 | 2021 | In [2]: %parts A B\\ |
|
2012 | 2022 | first A second B\\ |
|
2013 | 2023 | In [3]: %parts A\\ |
|
2014 | 2024 | Incorrect number of arguments: 2 expected.\\ |
|
2015 | 2025 | parts is an alias to: 'echo first %s second %s' |
|
2016 | 2026 | |
|
2017 | 2027 | Note that %l and %s are mutually exclusive. You can only use one or |
|
2018 | 2028 | the other in your aliases. |
|
2019 | 2029 | |
|
2020 | 2030 | Aliases expand Python variables just like system calls using ! or !! |
|
2021 | 2031 | do: all expressions prefixed with '$' get expanded. For details of |
|
2022 | 2032 | the semantic rules, see PEP-215: |
|
2023 | 2033 | http://www.python.org/peps/pep-0215.html. This is the library used by |
|
2024 | 2034 | IPython for variable expansion. If you want to access a true shell |
|
2025 | 2035 | variable, an extra $ is necessary to prevent its expansion by IPython: |
|
2026 | 2036 | |
|
2027 | 2037 | In [6]: alias show echo\\ |
|
2028 | 2038 | In [7]: PATH='A Python string'\\ |
|
2029 | 2039 | In [8]: show $PATH\\ |
|
2030 | 2040 | A Python string\\ |
|
2031 | 2041 | In [9]: show $$PATH\\ |
|
2032 | 2042 | /usr/local/lf9560/bin:/usr/local/intel/compiler70/ia32/bin:... |
|
2033 | 2043 | |
|
2034 | 2044 | You can use the alias facility to acess all of $PATH. See the %rehash |
|
2035 | 2045 | and %rehashx functions, which automatically create aliases for the |
|
2036 | 2046 | contents of your $PATH. |
|
2037 | 2047 | |
|
2038 | 2048 | If called with no parameters, %alias prints the current alias table.""" |
|
2039 | 2049 | |
|
2040 | 2050 | par = parameter_s.strip() |
|
2041 | 2051 | if not par: |
|
2042 | 2052 | if self.shell.rc.automagic: |
|
2043 | 2053 | prechar = '' |
|
2044 | 2054 | else: |
|
2045 | 2055 | prechar = self.shell.ESC_MAGIC |
|
2046 | 2056 | print 'Alias\t\tSystem Command\n'+'-'*30 |
|
2047 | 2057 | atab = self.shell.alias_table |
|
2048 | 2058 | aliases = atab.keys() |
|
2049 | 2059 | aliases.sort() |
|
2050 | 2060 | for alias in aliases: |
|
2051 | 2061 | print prechar+alias+'\t\t'+atab[alias][1] |
|
2052 | 2062 | print '-'*30+'\nTotal number of aliases:',len(aliases) |
|
2053 | 2063 | return |
|
2054 | 2064 | try: |
|
2055 | 2065 | alias,cmd = par.split(None,1) |
|
2056 | 2066 | except: |
|
2057 | 2067 | print OInspect.getdoc(self.magic_alias) |
|
2058 | 2068 | else: |
|
2059 | 2069 | nargs = cmd.count('%s') |
|
2060 | 2070 | if nargs>0 and cmd.find('%l')>=0: |
|
2061 | 2071 | error('The %s and %l specifiers are mutually exclusive ' |
|
2062 | 2072 | 'in alias definitions.') |
|
2063 | 2073 | else: # all looks OK |
|
2064 | 2074 | self.shell.alias_table[alias] = (nargs,cmd) |
|
2065 | 2075 | self.shell.alias_table_validate(verbose=1) |
|
2066 | 2076 | # end magic_alias |
|
2067 | 2077 | |
|
2068 | 2078 | def magic_unalias(self, parameter_s = ''): |
|
2069 | 2079 | """Remove an alias""" |
|
2070 | 2080 | |
|
2071 | 2081 | aname = parameter_s.strip() |
|
2072 | 2082 | if aname in self.shell.alias_table: |
|
2073 | 2083 | del self.shell.alias_table[aname] |
|
2074 | 2084 | |
|
2075 | 2085 | def magic_rehash(self, parameter_s = ''): |
|
2076 | 2086 | """Update the alias table with all entries in $PATH. |
|
2077 | 2087 | |
|
2078 | 2088 | This version does no checks on execute permissions or whether the |
|
2079 | 2089 | contents of $PATH are truly files (instead of directories or something |
|
2080 | 2090 | else). For such a safer (but slower) version, use %rehashx.""" |
|
2081 | 2091 | |
|
2082 | 2092 | # This function (and rehashx) manipulate the alias_table directly |
|
2083 | 2093 | # rather than calling magic_alias, for speed reasons. A rehash on a |
|
2084 | 2094 | # typical Linux box involves several thousand entries, so efficiency |
|
2085 | 2095 | # here is a top concern. |
|
2086 | 2096 | |
|
2087 | 2097 | path = filter(os.path.isdir,os.environ['PATH'].split(os.pathsep)) |
|
2088 | 2098 | alias_table = self.shell.alias_table |
|
2089 | 2099 | for pdir in path: |
|
2090 | 2100 | for ff in os.listdir(pdir): |
|
2091 | 2101 | # each entry in the alias table must be (N,name), where |
|
2092 | 2102 | # N is the number of positional arguments of the alias. |
|
2093 | 2103 | alias_table[ff] = (0,ff) |
|
2094 | 2104 | # Make sure the alias table doesn't contain keywords or builtins |
|
2095 | 2105 | self.shell.alias_table_validate() |
|
2096 | 2106 | # Call again init_auto_alias() so we get 'rm -i' and other modified |
|
2097 | 2107 | # aliases since %rehash will probably clobber them |
|
2098 | 2108 | self.shell.init_auto_alias() |
|
2099 | 2109 | |
|
2100 | 2110 | def magic_rehashx(self, parameter_s = ''): |
|
2101 | 2111 | """Update the alias table with all executable files in $PATH. |
|
2102 | 2112 | |
|
2103 | 2113 | This version explicitly checks that every entry in $PATH is a file |
|
2104 | 2114 | with execute access (os.X_OK), so it is much slower than %rehash. |
|
2105 | 2115 | |
|
2106 | 2116 | Under Windows, it checks executability as a match agains a |
|
2107 | 2117 | '|'-separated string of extensions, stored in the IPython config |
|
2108 | 2118 | variable win_exec_ext. This defaults to 'exe|com|bat'. """ |
|
2109 | 2119 | |
|
2110 | 2120 | path = filter(os.path.isdir,os.environ['PATH'].split(os.pathsep)) |
|
2111 | 2121 | alias_table = self.shell.alias_table |
|
2112 | 2122 | |
|
2113 | 2123 | if os.name == 'posix': |
|
2114 | 2124 | isexec = lambda fname:os.path.isfile(fname) and \ |
|
2115 | 2125 | os.access(fname,os.X_OK) |
|
2116 | 2126 | else: |
|
2117 | 2127 | |
|
2118 | 2128 | try: |
|
2119 | 2129 | winext = os.environ['pathext'].replace(';','|').replace('.','') |
|
2120 | 2130 | except KeyError: |
|
2121 | 2131 | winext = 'exe|com|bat' |
|
2122 | 2132 | |
|
2123 | 2133 | execre = re.compile(r'(.*)\.(%s)$' % winext,re.IGNORECASE) |
|
2124 | 2134 | isexec = lambda fname:os.path.isfile(fname) and execre.match(fname) |
|
2125 | 2135 | savedir = os.getcwd() |
|
2126 | 2136 | try: |
|
2127 | 2137 | # write the whole loop for posix/Windows so we don't have an if in |
|
2128 | 2138 | # the innermost part |
|
2129 | 2139 | if os.name == 'posix': |
|
2130 | 2140 | for pdir in path: |
|
2131 | 2141 | os.chdir(pdir) |
|
2132 | 2142 | for ff in os.listdir(pdir): |
|
2133 | 2143 | if isexec(ff): |
|
2134 | 2144 | # each entry in the alias table must be (N,name), |
|
2135 | 2145 | # where N is the number of positional arguments of the |
|
2136 | 2146 | # alias. |
|
2137 | 2147 | alias_table[ff] = (0,ff) |
|
2138 | 2148 | else: |
|
2139 | 2149 | for pdir in path: |
|
2140 | 2150 | os.chdir(pdir) |
|
2141 | 2151 | for ff in os.listdir(pdir): |
|
2142 | 2152 | if isexec(ff): |
|
2143 | 2153 | alias_table[execre.sub(r'\1',ff)] = (0,ff) |
|
2144 | 2154 | # Make sure the alias table doesn't contain keywords or builtins |
|
2145 | 2155 | self.shell.alias_table_validate() |
|
2146 | 2156 | # Call again init_auto_alias() so we get 'rm -i' and other |
|
2147 | 2157 | # modified aliases since %rehashx will probably clobber them |
|
2148 | 2158 | self.shell.init_auto_alias() |
|
2149 | 2159 | finally: |
|
2150 | 2160 | os.chdir(savedir) |
|
2151 | 2161 | |
|
2152 | 2162 | def magic_pwd(self, parameter_s = ''): |
|
2153 | 2163 | """Return the current working directory path.""" |
|
2154 | 2164 | return os.getcwd() |
|
2155 | 2165 | |
|
2156 | 2166 | def magic_cd(self, parameter_s=''): |
|
2157 | 2167 | """Change the current working directory. |
|
2158 | 2168 | |
|
2159 | 2169 | This command automatically maintains an internal list of directories |
|
2160 | 2170 | you visit during your IPython session, in the variable _dh. The |
|
2161 | 2171 | command %dhist shows this history nicely formatted. |
|
2162 | 2172 | |
|
2163 | 2173 | Usage: |
|
2164 | 2174 | |
|
2165 | 2175 | cd 'dir': changes to directory 'dir'. |
|
2166 | 2176 | |
|
2167 | 2177 | cd -: changes to the last visited directory. |
|
2168 | 2178 | |
|
2169 | 2179 | cd -<n>: changes to the n-th directory in the directory history. |
|
2170 | 2180 | |
|
2171 | 2181 | cd -b <bookmark_name>: jump to a bookmark set by %bookmark |
|
2172 | 2182 | (note: cd <bookmark_name> is enough if there is no |
|
2173 | 2183 | directory <bookmark_name>, but a bookmark with the name exists.) |
|
2174 | 2184 | |
|
2175 | 2185 | Options: |
|
2176 | 2186 | |
|
2177 | 2187 | -q: quiet. Do not print the working directory after the cd command is |
|
2178 | 2188 | executed. By default IPython's cd command does print this directory, |
|
2179 | 2189 | since the default prompts do not display path information. |
|
2180 | 2190 | |
|
2181 | 2191 | Note that !cd doesn't work for this purpose because the shell where |
|
2182 | 2192 | !command runs is immediately discarded after executing 'command'.""" |
|
2183 | 2193 | |
|
2184 | 2194 | parameter_s = parameter_s.strip() |
|
2185 | 2195 | bkms = self.shell.persist.get("bookmarks",{}) |
|
2186 | 2196 | |
|
2187 | 2197 | numcd = re.match(r'(-)(\d+)$',parameter_s) |
|
2188 | 2198 | # jump in directory history by number |
|
2189 | 2199 | if numcd: |
|
2190 | 2200 | nn = int(numcd.group(2)) |
|
2191 | 2201 | try: |
|
2192 | 2202 | ps = self.shell.user_ns['_dh'][nn] |
|
2193 | 2203 | except IndexError: |
|
2194 | 2204 | print 'The requested directory does not exist in history.' |
|
2195 | 2205 | return |
|
2196 | 2206 | else: |
|
2197 | 2207 | opts = {} |
|
2198 | 2208 | else: |
|
2199 | 2209 | opts,ps = self.parse_options(parameter_s,'qb',mode='string') |
|
2200 | 2210 | # jump to previous |
|
2201 | 2211 | if ps == '-': |
|
2202 | 2212 | try: |
|
2203 | 2213 | ps = self.shell.user_ns['_dh'][-2] |
|
2204 | 2214 | except IndexError: |
|
2205 | 2215 | print 'No previous directory to change to.' |
|
2206 | 2216 | return |
|
2207 | 2217 | # jump to bookmark |
|
2208 | 2218 | elif opts.has_key('b') or (bkms.has_key(ps) and not os.path.isdir(ps)): |
|
2209 | 2219 | if bkms.has_key(ps): |
|
2210 | 2220 | target = bkms[ps] |
|
2211 | 2221 | print '(bookmark:%s) -> %s' % (ps,target) |
|
2212 | 2222 | ps = target |
|
2213 | 2223 | else: |
|
2214 | 2224 | if bkms: |
|
2215 | 2225 | error("Bookmark '%s' not found. " |
|
2216 | 2226 | "Use '%bookmark -l' to see your bookmarks." % ps) |
|
2217 | 2227 | else: |
|
2218 | 2228 | print "Bookmarks not set - use %bookmark <bookmarkname>" |
|
2219 | 2229 | return |
|
2220 | 2230 | |
|
2221 | 2231 | # at this point ps should point to the target dir |
|
2222 | 2232 | if ps: |
|
2223 | 2233 | try: |
|
2224 | 2234 | os.chdir(os.path.expanduser(ps)) |
|
2225 | 2235 | except OSError: |
|
2226 | 2236 | print sys.exc_info()[1] |
|
2227 | 2237 | else: |
|
2228 | 2238 | self.shell.user_ns['_dh'].append(os.getcwd()) |
|
2229 | 2239 | else: |
|
2230 | 2240 | os.chdir(self.home_dir) |
|
2231 | 2241 | self.shell.user_ns['_dh'].append(os.getcwd()) |
|
2232 | 2242 | if not 'q' in opts: |
|
2233 | 2243 | print self.shell.user_ns['_dh'][-1] |
|
2234 | 2244 | |
|
2235 | 2245 | def magic_dhist(self, parameter_s=''): |
|
2236 | 2246 | """Print your history of visited directories. |
|
2237 | 2247 | |
|
2238 | 2248 | %dhist -> print full history\\ |
|
2239 | 2249 | %dhist n -> print last n entries only\\ |
|
2240 | 2250 | %dhist n1 n2 -> print entries between n1 and n2 (n1 not included)\\ |
|
2241 | 2251 | |
|
2242 | 2252 | This history is automatically maintained by the %cd command, and |
|
2243 | 2253 | always available as the global list variable _dh. You can use %cd -<n> |
|
2244 | 2254 | to go to directory number <n>.""" |
|
2245 | 2255 | |
|
2246 | 2256 | dh = self.shell.user_ns['_dh'] |
|
2247 | 2257 | if parameter_s: |
|
2248 | 2258 | try: |
|
2249 | 2259 | args = map(int,parameter_s.split()) |
|
2250 | 2260 | except: |
|
2251 | 2261 | self.arg_err(Magic.magic_dhist) |
|
2252 | 2262 | return |
|
2253 | 2263 | if len(args) == 1: |
|
2254 | 2264 | ini,fin = max(len(dh)-(args[0]),0),len(dh) |
|
2255 | 2265 | elif len(args) == 2: |
|
2256 | 2266 | ini,fin = args |
|
2257 | 2267 | else: |
|
2258 | 2268 | self.arg_err(Magic.magic_dhist) |
|
2259 | 2269 | return |
|
2260 | 2270 | else: |
|
2261 | 2271 | ini,fin = 0,len(dh) |
|
2262 | 2272 | nlprint(dh, |
|
2263 | 2273 | header = 'Directory history (kept in _dh)', |
|
2264 | 2274 | start=ini,stop=fin) |
|
2265 | 2275 | |
|
2266 | 2276 | def magic_env(self, parameter_s=''): |
|
2267 | 2277 | """List environment variables.""" |
|
2268 | 2278 | |
|
2269 | 2279 | # environ is an instance of UserDict |
|
2270 | 2280 | return os.environ.data |
|
2271 | 2281 | |
|
2272 | 2282 | def magic_pushd(self, parameter_s=''): |
|
2273 | 2283 | """Place the current dir on stack and change directory. |
|
2274 | 2284 | |
|
2275 | 2285 | Usage:\\ |
|
2276 | 2286 | %pushd ['dirname'] |
|
2277 | 2287 | |
|
2278 | 2288 | %pushd with no arguments does a %pushd to your home directory. |
|
2279 | 2289 | """ |
|
2280 | 2290 | if parameter_s == '': parameter_s = '~' |
|
2281 | 2291 | if len(self.dir_stack)>0 and os.path.expanduser(parameter_s) != \ |
|
2282 | 2292 | os.path.expanduser(self.dir_stack[0]): |
|
2283 | 2293 | try: |
|
2284 | 2294 | self.magic_cd(parameter_s) |
|
2285 | 2295 | self.dir_stack.insert(0,os.getcwd().replace(self.home_dir,'~')) |
|
2286 | 2296 | self.magic_dirs() |
|
2287 | 2297 | except: |
|
2288 | 2298 | print 'Invalid directory' |
|
2289 | 2299 | else: |
|
2290 | 2300 | print 'You are already there!' |
|
2291 | 2301 | |
|
2292 | 2302 | def magic_popd(self, parameter_s=''): |
|
2293 | 2303 | """Change to directory popped off the top of the stack. |
|
2294 | 2304 | """ |
|
2295 | 2305 | if len (self.dir_stack) > 1: |
|
2296 | 2306 | self.dir_stack.pop(0) |
|
2297 | 2307 | self.magic_cd(self.dir_stack[0]) |
|
2298 | 2308 | print self.dir_stack[0] |
|
2299 | 2309 | else: |
|
2300 | 2310 | print "You can't remove the starting directory from the stack:",\ |
|
2301 | 2311 | self.dir_stack |
|
2302 | 2312 | |
|
2303 | 2313 | def magic_dirs(self, parameter_s=''): |
|
2304 | 2314 | """Return the current directory stack.""" |
|
2305 | 2315 | |
|
2306 | 2316 | return self.dir_stack[:] |
|
2307 | 2317 | |
|
2308 | 2318 | def magic_sc(self, parameter_s=''): |
|
2309 | 2319 | """Shell capture - execute a shell command and capture its output. |
|
2310 | 2320 | |
|
2311 | 2321 | %sc [options] varname=command |
|
2312 | 2322 | |
|
2313 | 2323 | IPython will run the given command using commands.getoutput(), and |
|
2314 | 2324 | will then update the user's interactive namespace with a variable |
|
2315 | 2325 | called varname, containing the value of the call. Your command can |
|
2316 | 2326 | contain shell wildcards, pipes, etc. |
|
2317 | 2327 | |
|
2318 | 2328 | The '=' sign in the syntax is mandatory, and the variable name you |
|
2319 | 2329 | supply must follow Python's standard conventions for valid names. |
|
2320 | 2330 | |
|
2321 | 2331 | Options: |
|
2322 | 2332 | |
|
2323 | 2333 | -l: list output. Split the output on newlines into a list before |
|
2324 | 2334 | assigning it to the given variable. By default the output is stored |
|
2325 | 2335 | as a single string. |
|
2326 | 2336 | |
|
2327 | 2337 | -v: verbose. Print the contents of the variable. |
|
2328 | 2338 | |
|
2329 | 2339 | In most cases you should not need to split as a list, because the |
|
2330 | 2340 | returned value is a special type of string which can automatically |
|
2331 | 2341 | provide its contents either as a list (split on newlines) or as a |
|
2332 | 2342 | space-separated string. These are convenient, respectively, either |
|
2333 | 2343 | for sequential processing or to be passed to a shell command. |
|
2334 | 2344 | |
|
2335 | 2345 | For example: |
|
2336 | 2346 | |
|
2337 | 2347 | # Capture into variable a |
|
2338 | 2348 | In [9]: sc a=ls *py |
|
2339 | 2349 | |
|
2340 | 2350 | # a is a string with embedded newlines |
|
2341 | 2351 | In [10]: a |
|
2342 | 2352 | Out[10]: 'setup.py\nwin32_manual_post_install.py' |
|
2343 | 2353 | |
|
2344 | 2354 | # which can be seen as a list: |
|
2345 | 2355 | In [11]: a.l |
|
2346 | 2356 | Out[11]: ['setup.py', 'win32_manual_post_install.py'] |
|
2347 | 2357 | |
|
2348 | 2358 | # or as a whitespace-separated string: |
|
2349 | 2359 | In [12]: a.s |
|
2350 | 2360 | Out[12]: 'setup.py win32_manual_post_install.py' |
|
2351 | 2361 | |
|
2352 | 2362 | # a.s is useful to pass as a single command line: |
|
2353 | 2363 | In [13]: !wc -l $a.s |
|
2354 | 2364 | 146 setup.py |
|
2355 | 2365 | 130 win32_manual_post_install.py |
|
2356 | 2366 | 276 total |
|
2357 | 2367 | |
|
2358 | 2368 | # while the list form is useful to loop over: |
|
2359 | 2369 | In [14]: for f in a.l: |
|
2360 | 2370 | ....: !wc -l $f |
|
2361 | 2371 | ....: |
|
2362 | 2372 | 146 setup.py |
|
2363 | 2373 | 130 win32_manual_post_install.py |
|
2364 | 2374 | |
|
2365 | 2375 | Similiarly, the lists returned by the -l option are also special, in |
|
2366 | 2376 | the sense that you can equally invoke the .s attribute on them to |
|
2367 | 2377 | automatically get a whitespace-separated string from their contents: |
|
2368 | 2378 | |
|
2369 | 2379 | In [1]: sc -l b=ls *py |
|
2370 | 2380 | |
|
2371 | 2381 | In [2]: b |
|
2372 | 2382 | Out[2]: ['setup.py', 'win32_manual_post_install.py'] |
|
2373 | 2383 | |
|
2374 | 2384 | In [3]: b.s |
|
2375 | 2385 | Out[3]: 'setup.py win32_manual_post_install.py' |
|
2376 | 2386 | |
|
2377 | 2387 | In summary, both the lists and strings used for ouptut capture have |
|
2378 | 2388 | the following special attributes: |
|
2379 | 2389 | |
|
2380 | 2390 | .l (or .list) : value as list. |
|
2381 | 2391 | .n (or .nlstr): value as newline-separated string. |
|
2382 | 2392 | .s (or .spstr): value as space-separated string. |
|
2383 | 2393 | """ |
|
2384 | 2394 | |
|
2385 | 2395 | opts,args = self.parse_options(parameter_s,'lv') |
|
2386 | 2396 | # Try to get a variable name and command to run |
|
2387 | 2397 | try: |
|
2388 | 2398 | # the variable name must be obtained from the parse_options |
|
2389 | 2399 | # output, which uses shlex.split to strip options out. |
|
2390 | 2400 | var,_ = args.split('=',1) |
|
2391 | 2401 | var = var.strip() |
|
2392 | 2402 | # But the the command has to be extracted from the original input |
|
2393 | 2403 | # parameter_s, not on what parse_options returns, to avoid the |
|
2394 | 2404 | # quote stripping which shlex.split performs on it. |
|
2395 | 2405 | _,cmd = parameter_s.split('=',1) |
|
2396 | 2406 | except ValueError: |
|
2397 | 2407 | var,cmd = '','' |
|
2398 | 2408 | if not var: |
|
2399 | 2409 | error('you must specify a variable to assign the command to.') |
|
2400 | 2410 | return |
|
2401 | 2411 | # If all looks ok, proceed |
|
2402 | 2412 | out,err = self.shell.getoutputerror(cmd) |
|
2403 | 2413 | if err: |
|
2404 | 2414 | print >> Term.cerr,err |
|
2405 | 2415 | if opts.has_key('l'): |
|
2406 | 2416 | out = SList(out.split('\n')) |
|
2407 | 2417 | else: |
|
2408 | 2418 | out = LSString(out) |
|
2409 | 2419 | if opts.has_key('v'): |
|
2410 | 2420 | print '%s ==\n%s' % (var,pformat(out)) |
|
2411 | 2421 | self.shell.user_ns.update({var:out}) |
|
2412 | 2422 | |
|
2413 | 2423 | def magic_sx(self, parameter_s=''): |
|
2414 | 2424 | """Shell execute - run a shell command and capture its output. |
|
2415 | 2425 | |
|
2416 | 2426 | %sx command |
|
2417 | 2427 | |
|
2418 | 2428 | IPython will run the given command using commands.getoutput(), and |
|
2419 | 2429 | return the result formatted as a list (split on '\\n'). Since the |
|
2420 | 2430 | output is _returned_, it will be stored in ipython's regular output |
|
2421 | 2431 | cache Out[N] and in the '_N' automatic variables. |
|
2422 | 2432 | |
|
2423 | 2433 | Notes: |
|
2424 | 2434 | |
|
2425 | 2435 | 1) If an input line begins with '!!', then %sx is automatically |
|
2426 | 2436 | invoked. That is, while: |
|
2427 | 2437 | !ls |
|
2428 | 2438 | causes ipython to simply issue system('ls'), typing |
|
2429 | 2439 | !!ls |
|
2430 | 2440 | is a shorthand equivalent to: |
|
2431 | 2441 | %sx ls |
|
2432 | 2442 | |
|
2433 | 2443 | 2) %sx differs from %sc in that %sx automatically splits into a list, |
|
2434 | 2444 | like '%sc -l'. The reason for this is to make it as easy as possible |
|
2435 | 2445 | to process line-oriented shell output via further python commands. |
|
2436 | 2446 | %sc is meant to provide much finer control, but requires more |
|
2437 | 2447 | typing. |
|
2438 | 2448 | |
|
2439 | 2449 | 3) Just like %sc -l, this is a list with special attributes: |
|
2440 | 2450 | |
|
2441 | 2451 | .l (or .list) : value as list. |
|
2442 | 2452 | .n (or .nlstr): value as newline-separated string. |
|
2443 | 2453 | .s (or .spstr): value as whitespace-separated string. |
|
2444 | 2454 | |
|
2445 | 2455 | This is very useful when trying to use such lists as arguments to |
|
2446 | 2456 | system commands.""" |
|
2447 | 2457 | |
|
2448 | 2458 | if parameter_s: |
|
2449 | 2459 | out,err = self.shell.getoutputerror(parameter_s) |
|
2450 | 2460 | if err: |
|
2451 | 2461 | print >> Term.cerr,err |
|
2452 | 2462 | return SList(out.split('\n')) |
|
2453 | 2463 | |
|
2454 | 2464 | def magic_bg(self, parameter_s=''): |
|
2455 | 2465 | """Run a job in the background, in a separate thread. |
|
2456 | 2466 | |
|
2457 | 2467 | For example, |
|
2458 | 2468 | |
|
2459 | 2469 | %bg myfunc(x,y,z=1) |
|
2460 | 2470 | |
|
2461 | 2471 | will execute 'myfunc(x,y,z=1)' in a background thread. As soon as the |
|
2462 | 2472 | execution starts, a message will be printed indicating the job |
|
2463 | 2473 | number. If your job number is 5, you can use |
|
2464 | 2474 | |
|
2465 | 2475 | myvar = jobs.result(5) or myvar = jobs[5].result |
|
2466 | 2476 | |
|
2467 | 2477 | to assign this result to variable 'myvar'. |
|
2468 | 2478 | |
|
2469 | 2479 | IPython has a job manager, accessible via the 'jobs' object. You can |
|
2470 | 2480 | type jobs? to get more information about it, and use jobs.<TAB> to see |
|
2471 | 2481 | its attributes. All attributes not starting with an underscore are |
|
2472 | 2482 | meant for public use. |
|
2473 | 2483 | |
|
2474 | 2484 | In particular, look at the jobs.new() method, which is used to create |
|
2475 | 2485 | new jobs. This magic %bg function is just a convenience wrapper |
|
2476 | 2486 | around jobs.new(), for expression-based jobs. If you want to create a |
|
2477 | 2487 | new job with an explicit function object and arguments, you must call |
|
2478 | 2488 | jobs.new() directly. |
|
2479 | 2489 | |
|
2480 | 2490 | The jobs.new docstring also describes in detail several important |
|
2481 | 2491 | caveats associated with a thread-based model for background job |
|
2482 | 2492 | execution. Type jobs.new? for details. |
|
2483 | 2493 | |
|
2484 | 2494 | You can check the status of all jobs with jobs.status(). |
|
2485 | 2495 | |
|
2486 | 2496 | The jobs variable is set by IPython into the Python builtin namespace. |
|
2487 | 2497 | If you ever declare a variable named 'jobs', you will shadow this |
|
2488 | 2498 | name. You can either delete your global jobs variable to regain |
|
2489 | 2499 | access to the job manager, or make a new name and assign it manually |
|
2490 | 2500 | to the manager (stored in IPython's namespace). For example, to |
|
2491 | 2501 | assign the job manager to the Jobs name, use: |
|
2492 | 2502 | |
|
2493 | 2503 | Jobs = __builtins__.jobs""" |
|
2494 | 2504 | |
|
2495 | 2505 | self.shell.jobs.new(parameter_s,self.shell.user_ns) |
|
2496 | 2506 | |
|
2497 | 2507 | def magic_bookmark(self, parameter_s=''): |
|
2498 | 2508 | """Manage IPython's bookmark system. |
|
2499 | 2509 | |
|
2500 | 2510 | %bookmark <name> - set bookmark to current dir |
|
2501 | 2511 | %bookmark <name> <dir> - set bookmark to <dir> |
|
2502 | 2512 | %bookmark -l - list all bookmarks |
|
2503 | 2513 | %bookmark -d <name> - remove bookmark |
|
2504 | 2514 | %bookmark -r - remove all bookmarks |
|
2505 | 2515 | |
|
2506 | 2516 | You can later on access a bookmarked folder with: |
|
2507 | 2517 | %cd -b <name> |
|
2508 | 2518 | or simply '%cd <name>' if there is no directory called <name> AND |
|
2509 | 2519 | there is such a bookmark defined. |
|
2510 | 2520 | |
|
2511 | 2521 | Your bookmarks persist through IPython sessions, but they are |
|
2512 | 2522 | associated with each profile.""" |
|
2513 | 2523 | |
|
2514 | 2524 | opts,args = self.parse_options(parameter_s,'drl',mode='list') |
|
2515 | 2525 | if len(args) > 2: |
|
2516 | 2526 | error('You can only give at most two arguments') |
|
2517 | 2527 | return |
|
2518 | 2528 | |
|
2519 | 2529 | bkms = self.shell.persist.get('bookmarks',{}) |
|
2520 | 2530 | |
|
2521 | 2531 | if opts.has_key('d'): |
|
2522 | 2532 | try: |
|
2523 | 2533 | todel = args[0] |
|
2524 | 2534 | except IndexError: |
|
2525 | 2535 | error('You must provide a bookmark to delete') |
|
2526 | 2536 | else: |
|
2527 | 2537 | try: |
|
2528 | 2538 | del bkms[todel] |
|
2529 | 2539 | except: |
|
2530 | 2540 | error("Can't delete bookmark '%s'" % todel) |
|
2531 | 2541 | elif opts.has_key('r'): |
|
2532 | 2542 | bkms = {} |
|
2533 | 2543 | elif opts.has_key('l'): |
|
2534 | 2544 | bks = bkms.keys() |
|
2535 | 2545 | bks.sort() |
|
2536 | 2546 | if bks: |
|
2537 | 2547 | size = max(map(len,bks)) |
|
2538 | 2548 | else: |
|
2539 | 2549 | size = 0 |
|
2540 | 2550 | fmt = '%-'+str(size)+'s -> %s' |
|
2541 | 2551 | print 'Current bookmarks:' |
|
2542 | 2552 | for bk in bks: |
|
2543 | 2553 | print fmt % (bk,bkms[bk]) |
|
2544 | 2554 | else: |
|
2545 | 2555 | if not args: |
|
2546 | 2556 | error("You must specify the bookmark name") |
|
2547 | 2557 | elif len(args)==1: |
|
2548 | 2558 | bkms[args[0]] = os.getcwd() |
|
2549 | 2559 | elif len(args)==2: |
|
2550 | 2560 | bkms[args[0]] = args[1] |
|
2551 | 2561 | self.persist['bookmarks'] = bkms |
|
2552 | 2562 | |
|
2553 | 2563 | def magic_pycat(self, parameter_s=''): |
|
2554 | 2564 | """Show a syntax-highlighted file through a pager. |
|
2555 | 2565 | |
|
2556 | 2566 | This magic is similar to the cat utility, but it will assume the file |
|
2557 | 2567 | to be Python source and will show it with syntax highlighting. """ |
|
2558 | 2568 | |
|
2559 | 2569 | filename = get_py_filename(parameter_s) |
|
2560 | 2570 | page(self.shell.colorize(file_read(filename)), |
|
2561 | 2571 | screen_lines=self.shell.rc.screen_length) |
|
2562 | 2572 | |
|
2563 | 2573 | # end Magic |
@@ -1,572 +1,574 b'' | |||
|
1 | 1 | # -*- coding: utf-8 -*- |
|
2 | 2 | """ |
|
3 | 3 | Classes for handling input/output prompts. |
|
4 | 4 | |
|
5 |
$Id: Prompts.py |
|
|
5 | $Id: Prompts.py 951 2005-12-25 00:57:24Z fperez $""" | |
|
6 | 6 | |
|
7 | 7 | #***************************************************************************** |
|
8 | 8 | # Copyright (C) 2001-2004 Fernando Perez <fperez@colorado.edu> |
|
9 | 9 | # |
|
10 | 10 | # Distributed under the terms of the BSD License. The full license is in |
|
11 | 11 | # the file COPYING, distributed as part of this software. |
|
12 | 12 | #***************************************************************************** |
|
13 | 13 | |
|
14 | 14 | from IPython import Release |
|
15 | 15 | __author__ = '%s <%s>' % Release.authors['Fernando'] |
|
16 | 16 | __license__ = Release.license |
|
17 | 17 | __version__ = Release.version |
|
18 | 18 | |
|
19 | 19 | #**************************************************************************** |
|
20 | 20 | # Required modules |
|
21 | 21 | import __builtin__ |
|
22 | 22 | import os,sys,socket |
|
23 | 23 | import time |
|
24 | 24 | from pprint import pprint,pformat |
|
25 | 25 | |
|
26 | 26 | # IPython's own |
|
27 | 27 | from IPython.genutils import * |
|
28 | 28 | from IPython.Struct import Struct |
|
29 | 29 | from IPython.Magic import Macro |
|
30 | 30 | from IPython.Itpl import ItplNS |
|
31 | 31 | from IPython import ColorANSI |
|
32 | 32 | |
|
33 | 33 | #**************************************************************************** |
|
34 | 34 | #Color schemes for Prompts. |
|
35 | 35 | |
|
36 | 36 | PromptColors = ColorANSI.ColorSchemeTable() |
|
37 | 37 | InputColors = ColorANSI.InputTermColors # just a shorthand |
|
38 | 38 | Colors = ColorANSI.TermColors # just a shorthand |
|
39 | 39 | |
|
40 | 40 | PromptColors.add_scheme(ColorANSI.ColorScheme( |
|
41 | 41 | 'NoColor', |
|
42 | 42 | in_prompt = InputColors.NoColor, # Input prompt |
|
43 | 43 | in_number = InputColors.NoColor, # Input prompt number |
|
44 | 44 | in_prompt2 = InputColors.NoColor, # Continuation prompt |
|
45 | 45 | in_normal = InputColors.NoColor, # color off (usu. Colors.Normal) |
|
46 | 46 | |
|
47 | 47 | out_prompt = Colors.NoColor, # Output prompt |
|
48 | 48 | out_number = Colors.NoColor, # Output prompt number |
|
49 | 49 | |
|
50 | 50 | normal = Colors.NoColor # color off (usu. Colors.Normal) |
|
51 | 51 | )) |
|
52 | ||
|
52 | 53 | # make some schemes as instances so we can copy them for modification easily: |
|
53 | 54 | __PColLinux = ColorANSI.ColorScheme( |
|
54 | 55 | 'Linux', |
|
55 | 56 | in_prompt = InputColors.Green, |
|
56 | 57 | in_number = InputColors.LightGreen, |
|
57 | 58 | in_prompt2 = InputColors.Green, |
|
58 | 59 | in_normal = InputColors.Normal, # color off (usu. Colors.Normal) |
|
59 | 60 | |
|
60 | 61 | out_prompt = Colors.Red, |
|
61 | 62 | out_number = Colors.LightRed, |
|
62 | 63 | |
|
63 | 64 | normal = Colors.Normal |
|
64 | 65 | ) |
|
65 | 66 | # Don't forget to enter it into the table! |
|
66 | 67 | PromptColors.add_scheme(__PColLinux) |
|
68 | ||
|
67 | 69 | # Slightly modified Linux for light backgrounds |
|
68 | __PColLightBG = ColorANSI.ColorScheme('LightBG',**__PColLinux.colors.dict().copy()) | |
|
70 | __PColLightBG = __PColLinux.copy('LightBG') | |
|
69 | 71 | |
|
70 | 72 | __PColLightBG.colors.update( |
|
71 | 73 | in_prompt = InputColors.Blue, |
|
72 | 74 | in_number = InputColors.LightBlue, |
|
73 | 75 | in_prompt2 = InputColors.Blue |
|
74 | 76 | ) |
|
75 | 77 | PromptColors.add_scheme(__PColLightBG) |
|
76 | 78 | |
|
77 | 79 | del Colors,InputColors |
|
78 | 80 | |
|
79 | 81 | #----------------------------------------------------------------------------- |
|
80 | 82 | def multiple_replace(dict, text): |
|
81 | 83 | """ Replace in 'text' all occurences of any key in the given |
|
82 | 84 | dictionary by its corresponding value. Returns the new string.""" |
|
83 | 85 | |
|
84 | 86 | # Function by Xavier Defrang, originally found at: |
|
85 | 87 | # http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/81330 |
|
86 | 88 | |
|
87 | 89 | # Create a regular expression from the dictionary keys |
|
88 | 90 | regex = re.compile("(%s)" % "|".join(map(re.escape, dict.keys()))) |
|
89 | 91 | # For each match, look-up corresponding value in dictionary |
|
90 | 92 | return regex.sub(lambda mo: dict[mo.string[mo.start():mo.end()]], text) |
|
91 | 93 | |
|
92 | 94 | #----------------------------------------------------------------------------- |
|
93 | 95 | # Special characters that can be used in prompt templates, mainly bash-like |
|
94 | 96 | |
|
95 | 97 | # If $HOME isn't defined (Windows), make it an absurd string so that it can |
|
96 | 98 | # never be expanded out into '~'. Basically anything which can never be a |
|
97 | 99 | # reasonable directory name will do, we just want the $HOME -> '~' operation |
|
98 | 100 | # to become a no-op. We pre-compute $HOME here so it's not done on every |
|
99 | 101 | # prompt call. |
|
100 | 102 | |
|
101 | 103 | # FIXME: |
|
102 | 104 | |
|
103 | 105 | # - This should be turned into a class which does proper namespace management, |
|
104 | 106 | # since the prompt specials need to be evaluated in a certain namespace. |
|
105 | 107 | # Currently it's just globals, which need to be managed manually by code |
|
106 | 108 | # below. |
|
107 | 109 | |
|
108 | 110 | # - I also need to split up the color schemes from the prompt specials |
|
109 | 111 | # somehow. I don't have a clean design for that quite yet. |
|
110 | 112 | |
|
111 | 113 | HOME = os.environ.get("HOME","//////:::::ZZZZZ,,,~~~") |
|
112 | 114 | |
|
113 | 115 | # We precompute a few more strings here for the prompt_specials, which are |
|
114 | 116 | # fixed once ipython starts. This reduces the runtime overhead of computing |
|
115 | 117 | # prompt strings. |
|
116 | 118 | USER = os.environ.get("USER") |
|
117 | 119 | HOSTNAME = socket.gethostname() |
|
118 | 120 | HOSTNAME_SHORT = HOSTNAME.split(".")[0] |
|
119 | 121 | ROOT_SYMBOL = "$#"[os.name=='nt' or os.getuid()==0] |
|
120 | 122 | |
|
121 | 123 | prompt_specials_color = { |
|
122 | 124 | # Prompt/history count |
|
123 | 125 | '%n' : '${self.col_num}' '${self.cache.prompt_count}' '${self.col_p}', |
|
124 | 126 | '\\#': '${self.col_num}' '${self.cache.prompt_count}' '${self.col_p}', |
|
125 | 127 | # Prompt/history count, with the actual digits replaced by dots. Used |
|
126 | 128 | # mainly in continuation prompts (prompt_in2) |
|
127 | 129 | '\\D': '${"."*len(str(self.cache.prompt_count))}', |
|
128 | 130 | # Current working directory |
|
129 | 131 | '\\w': '${os.getcwd()}', |
|
130 | 132 | # Current time |
|
131 | 133 | '\\t' : '${time.strftime("%H:%M:%S")}', |
|
132 | 134 | # Basename of current working directory. |
|
133 | 135 | # (use os.sep to make this portable across OSes) |
|
134 | 136 | '\\W' : '${os.getcwd().split("%s")[-1]}' % os.sep, |
|
135 | 137 | # These X<N> are an extension to the normal bash prompts. They return |
|
136 | 138 | # N terms of the path, after replacing $HOME with '~' |
|
137 | 139 | '\\X0': '${os.getcwd().replace("%s","~")}' % HOME, |
|
138 | 140 | '\\X1': '${self.cwd_filt(1)}', |
|
139 | 141 | '\\X2': '${self.cwd_filt(2)}', |
|
140 | 142 | '\\X3': '${self.cwd_filt(3)}', |
|
141 | 143 | '\\X4': '${self.cwd_filt(4)}', |
|
142 | 144 | '\\X5': '${self.cwd_filt(5)}', |
|
143 | 145 | # Y<N> are similar to X<N>, but they show '~' if it's the directory |
|
144 | 146 | # N+1 in the list. Somewhat like %cN in tcsh. |
|
145 | 147 | '\\Y0': '${self.cwd_filt2(0)}', |
|
146 | 148 | '\\Y1': '${self.cwd_filt2(1)}', |
|
147 | 149 | '\\Y2': '${self.cwd_filt2(2)}', |
|
148 | 150 | '\\Y3': '${self.cwd_filt2(3)}', |
|
149 | 151 | '\\Y4': '${self.cwd_filt2(4)}', |
|
150 | 152 | '\\Y5': '${self.cwd_filt2(5)}', |
|
151 | 153 | # Hostname up to first . |
|
152 | 154 | '\\h': HOSTNAME_SHORT, |
|
153 | 155 | # Full hostname |
|
154 | 156 | '\\H': HOSTNAME, |
|
155 | 157 | # Username of current user |
|
156 | 158 | '\\u': USER, |
|
157 | 159 | # Escaped '\' |
|
158 | 160 | '\\\\': '\\', |
|
159 | 161 | # Newline |
|
160 | 162 | '\\n': '\n', |
|
161 | 163 | # Carriage return |
|
162 | 164 | '\\r': '\r', |
|
163 | 165 | # Release version |
|
164 | 166 | '\\v': __version__, |
|
165 | 167 | # Root symbol ($ or #) |
|
166 | 168 | '\\$': ROOT_SYMBOL, |
|
167 | 169 | } |
|
168 | 170 | |
|
169 | 171 | # A copy of the prompt_specials dictionary but with all color escapes removed, |
|
170 | 172 | # so we can correctly compute the prompt length for the auto_rewrite method. |
|
171 | 173 | prompt_specials_nocolor = prompt_specials_color.copy() |
|
172 | 174 | prompt_specials_nocolor['%n'] = '${self.cache.prompt_count}' |
|
173 | 175 | prompt_specials_nocolor['\\#'] = '${self.cache.prompt_count}' |
|
174 | 176 | |
|
175 | 177 | # Add in all the InputTermColors color escapes as valid prompt characters. |
|
176 | 178 | # They all get added as \\C_COLORNAME, so that we don't have any conflicts |
|
177 | 179 | # with a color name which may begin with a letter used by any other of the |
|
178 | 180 | # allowed specials. This of course means that \\C will never be allowed for |
|
179 | 181 | # anything else. |
|
180 | 182 | input_colors = ColorANSI.InputTermColors |
|
181 | 183 | for _color in dir(input_colors): |
|
182 | 184 | if _color[0] != '_': |
|
183 | 185 | c_name = '\\C_'+_color |
|
184 | 186 | prompt_specials_color[c_name] = getattr(input_colors,_color) |
|
185 | 187 | prompt_specials_nocolor[c_name] = '' |
|
186 | 188 | |
|
187 | 189 | # we default to no color for safety. Note that prompt_specials is a global |
|
188 | 190 | # variable used by all prompt objects. |
|
189 | 191 | prompt_specials = prompt_specials_nocolor |
|
190 | 192 | |
|
191 | 193 | #----------------------------------------------------------------------------- |
|
192 | 194 | def str_safe(arg): |
|
193 | 195 | """Convert to a string, without ever raising an exception. |
|
194 | 196 | |
|
195 | 197 | If str(arg) fails, <ERROR: ... > is returned, where ... is the exception |
|
196 | 198 | error message.""" |
|
197 | 199 | |
|
198 | 200 | try: |
|
199 | 201 | out = str(arg) |
|
200 | 202 | except UnicodeError: |
|
201 | 203 | try: |
|
202 | 204 | out = arg.encode('utf_8','replace') |
|
203 | 205 | except Exception,msg: |
|
204 | 206 | # let's keep this little duplication here, so that the most common |
|
205 | 207 | # case doesn't suffer from a double try wrapping. |
|
206 | 208 | out = '<ERROR: %s>' % msg |
|
207 | 209 | except Exception,msg: |
|
208 | 210 | out = '<ERROR: %s>' % msg |
|
209 | 211 | return out |
|
210 | 212 | |
|
211 | 213 | class BasePrompt: |
|
212 | 214 | """Interactive prompt similar to Mathematica's.""" |
|
213 | 215 | def __init__(self,cache,sep,prompt,pad_left=False): |
|
214 | 216 | |
|
215 | 217 | # Hack: we access information about the primary prompt through the |
|
216 | 218 | # cache argument. We need this, because we want the secondary prompt |
|
217 | 219 | # to be aligned with the primary one. Color table info is also shared |
|
218 | 220 | # by all prompt classes through the cache. Nice OO spaghetti code! |
|
219 | 221 | self.cache = cache |
|
220 | 222 | self.sep = sep |
|
221 | 223 | |
|
222 | 224 | # regexp to count the number of spaces at the end of a prompt |
|
223 | 225 | # expression, useful for prompt auto-rewriting |
|
224 | 226 | self.rspace = re.compile(r'(\s*)$') |
|
225 | 227 | # Flag to left-pad prompt strings to match the length of the primary |
|
226 | 228 | # prompt |
|
227 | 229 | self.pad_left = pad_left |
|
228 | 230 | # Set template to create each actual prompt (where numbers change) |
|
229 | 231 | self.p_template = prompt |
|
230 | 232 | self.set_p_str() |
|
231 | 233 | |
|
232 | 234 | def set_p_str(self): |
|
233 | 235 | """ Set the interpolating prompt strings. |
|
234 | 236 | |
|
235 | 237 | This must be called every time the color settings change, because the |
|
236 | 238 | prompt_specials global may have changed.""" |
|
237 | 239 | |
|
238 | 240 | import os,time # needed in locals for prompt string handling |
|
239 | 241 | loc = locals() |
|
240 | 242 | self.p_str = ItplNS('%s%s%s' % |
|
241 | 243 | ('${self.sep}${self.col_p}', |
|
242 | 244 | multiple_replace(prompt_specials, self.p_template), |
|
243 | 245 | '${self.col_norm}'),self.cache.user_ns,loc) |
|
244 | 246 | |
|
245 | 247 | self.p_str_nocolor = ItplNS(multiple_replace(prompt_specials_nocolor, |
|
246 | 248 | self.p_template), |
|
247 | 249 | self.cache.user_ns,loc) |
|
248 | 250 | |
|
249 | 251 | def write(self,msg): # dbg |
|
250 | 252 | sys.stdout.write(msg) |
|
251 | 253 | return '' |
|
252 | 254 | |
|
253 | 255 | def __str__(self): |
|
254 | 256 | """Return a string form of the prompt. |
|
255 | 257 | |
|
256 | 258 | This for is useful for continuation and output prompts, since it is |
|
257 | 259 | left-padded to match lengths with the primary one (if the |
|
258 | 260 | self.pad_left attribute is set).""" |
|
259 | 261 | |
|
260 | 262 | out_str = str_safe(self.p_str) |
|
261 | 263 | if self.pad_left: |
|
262 | 264 | # We must find the amount of padding required to match lengths, |
|
263 | 265 | # taking the color escapes (which are invisible on-screen) into |
|
264 | 266 | # account. |
|
265 | 267 | esc_pad = len(out_str) - len(str_safe(self.p_str_nocolor)) |
|
266 | 268 | format = '%%%ss' % (len(str(self.cache.last_prompt))+esc_pad) |
|
267 | 269 | return format % out_str |
|
268 | 270 | else: |
|
269 | 271 | return out_str |
|
270 | 272 | |
|
271 | 273 | # these path filters are put in as methods so that we can control the |
|
272 | 274 | # namespace where the prompt strings get evaluated |
|
273 | 275 | def cwd_filt(self,depth): |
|
274 | 276 | """Return the last depth elements of the current working directory. |
|
275 | 277 | |
|
276 | 278 | $HOME is always replaced with '~'. |
|
277 | 279 | If depth==0, the full path is returned.""" |
|
278 | 280 | |
|
279 | 281 | cwd = os.getcwd().replace(HOME,"~") |
|
280 | 282 | out = os.sep.join(cwd.split(os.sep)[-depth:]) |
|
281 | 283 | if out: |
|
282 | 284 | return out |
|
283 | 285 | else: |
|
284 | 286 | return os.sep |
|
285 | 287 | |
|
286 | 288 | def cwd_filt2(self,depth): |
|
287 | 289 | """Return the last depth elements of the current working directory. |
|
288 | 290 | |
|
289 | 291 | $HOME is always replaced with '~'. |
|
290 | 292 | If depth==0, the full path is returned.""" |
|
291 | 293 | |
|
292 | 294 | cwd = os.getcwd().replace(HOME,"~").split(os.sep) |
|
293 | 295 | if '~' in cwd and len(cwd) == depth+1: |
|
294 | 296 | depth += 1 |
|
295 | 297 | out = os.sep.join(cwd[-depth:]) |
|
296 | 298 | if out: |
|
297 | 299 | return out |
|
298 | 300 | else: |
|
299 | 301 | return os.sep |
|
300 | 302 | |
|
301 | 303 | class Prompt1(BasePrompt): |
|
302 | 304 | """Input interactive prompt similar to Mathematica's.""" |
|
303 | 305 | |
|
304 | 306 | def __init__(self,cache,sep='\n',prompt='In [\\#]: ',pad_left=True): |
|
305 | 307 | BasePrompt.__init__(self,cache,sep,prompt,pad_left) |
|
306 | 308 | |
|
307 | 309 | def set_colors(self): |
|
308 | 310 | self.set_p_str() |
|
309 | 311 | Colors = self.cache.color_table.active_colors # shorthand |
|
310 | 312 | self.col_p = Colors.in_prompt |
|
311 | 313 | self.col_num = Colors.in_number |
|
312 | 314 | self.col_norm = Colors.in_normal |
|
313 | 315 | # We need a non-input version of these escapes for the '--->' |
|
314 | 316 | # auto-call prompts used in the auto_rewrite() method. |
|
315 | 317 | self.col_p_ni = self.col_p.replace('\001','').replace('\002','') |
|
316 | 318 | self.col_norm_ni = Colors.normal |
|
317 | 319 | |
|
318 | 320 | def __str__(self): |
|
319 | 321 | self.cache.prompt_count += 1 |
|
320 | 322 | self.cache.last_prompt = str_safe(self.p_str_nocolor).split('\n')[-1] |
|
321 | 323 | return str_safe(self.p_str) |
|
322 | 324 | |
|
323 | 325 | def auto_rewrite(self): |
|
324 | 326 | """Print a string of the form '--->' which lines up with the previous |
|
325 | 327 | input string. Useful for systems which re-write the user input when |
|
326 | 328 | handling automatically special syntaxes.""" |
|
327 | 329 | |
|
328 | 330 | curr = str(self.cache.last_prompt) |
|
329 | 331 | nrspaces = len(self.rspace.search(curr).group()) |
|
330 | 332 | return '%s%s>%s%s' % (self.col_p_ni,'-'*(len(curr)-nrspaces-1), |
|
331 | 333 | ' '*nrspaces,self.col_norm_ni) |
|
332 | 334 | |
|
333 | 335 | class PromptOut(BasePrompt): |
|
334 | 336 | """Output interactive prompt similar to Mathematica's.""" |
|
335 | 337 | |
|
336 | 338 | def __init__(self,cache,sep='',prompt='Out[\\#]: ',pad_left=True): |
|
337 | 339 | BasePrompt.__init__(self,cache,sep,prompt,pad_left) |
|
338 | 340 | if not self.p_template: |
|
339 | 341 | self.__str__ = lambda: '' |
|
340 | 342 | |
|
341 | 343 | def set_colors(self): |
|
342 | 344 | self.set_p_str() |
|
343 | 345 | Colors = self.cache.color_table.active_colors # shorthand |
|
344 | 346 | self.col_p = Colors.out_prompt |
|
345 | 347 | self.col_num = Colors.out_number |
|
346 | 348 | self.col_norm = Colors.normal |
|
347 | 349 | |
|
348 | 350 | class Prompt2(BasePrompt): |
|
349 | 351 | """Interactive continuation prompt.""" |
|
350 | 352 | |
|
351 | 353 | def __init__(self,cache,prompt=' .\\D.: ',pad_left=True): |
|
352 | 354 | self.cache = cache |
|
353 | 355 | self.p_template = prompt |
|
354 | 356 | self.pad_left = pad_left |
|
355 | 357 | self.set_p_str() |
|
356 | 358 | |
|
357 | 359 | def set_p_str(self): |
|
358 | 360 | import os,time # needed in locals for prompt string handling |
|
359 | 361 | loc = locals() |
|
360 | 362 | self.p_str = ItplNS('%s%s%s' % |
|
361 | 363 | ('${self.col_p2}', |
|
362 | 364 | multiple_replace(prompt_specials, self.p_template), |
|
363 | 365 | '$self.col_norm'), |
|
364 | 366 | self.cache.user_ns,loc) |
|
365 | 367 | self.p_str_nocolor = ItplNS(multiple_replace(prompt_specials_nocolor, |
|
366 | 368 | self.p_template), |
|
367 | 369 | self.cache.user_ns,loc) |
|
368 | 370 | |
|
369 | 371 | def set_colors(self): |
|
370 | 372 | self.set_p_str() |
|
371 | 373 | Colors = self.cache.color_table.active_colors |
|
372 | 374 | self.col_p2 = Colors.in_prompt2 |
|
373 | 375 | self.col_norm = Colors.in_normal |
|
374 | 376 | # FIXME (2004-06-16) HACK: prevent crashes for users who haven't |
|
375 | 377 | # updated their prompt_in2 definitions. Remove eventually. |
|
376 | 378 | self.col_p = Colors.out_prompt |
|
377 | 379 | self.col_num = Colors.out_number |
|
378 | 380 | |
|
379 | 381 | #----------------------------------------------------------------------------- |
|
380 | 382 | class CachedOutput: |
|
381 | 383 | """Class for printing output from calculations while keeping a cache of |
|
382 | 384 | reults. It dynamically creates global variables prefixed with _ which |
|
383 | 385 | contain these results. |
|
384 | 386 | |
|
385 | 387 | Meant to be used as a sys.displayhook replacement, providing numbered |
|
386 | 388 | prompts and cache services. |
|
387 | 389 | |
|
388 | 390 | Initialize with initial and final values for cache counter (this defines |
|
389 | 391 | the maximum size of the cache.""" |
|
390 | 392 | |
|
391 | 393 | def __init__(self,cache_size,Pprint,colors='NoColor',input_sep='\n', |
|
392 | 394 | output_sep='\n',output_sep2='',user_ns={}, |
|
393 | 395 | ps1 = None, ps2 = None,ps_out = None, |
|
394 | 396 | input_hist = None,pad_left=True): |
|
395 | 397 | |
|
396 | 398 | cache_size_min = 20 |
|
397 | 399 | if cache_size <= 0: |
|
398 | 400 | self.do_full_cache = 0 |
|
399 | 401 | cache_size = 0 |
|
400 | 402 | elif cache_size < cache_size_min: |
|
401 | 403 | self.do_full_cache = 0 |
|
402 | 404 | cache_size = 0 |
|
403 | 405 | warn('caching was disabled (min value for cache size is %s).' % |
|
404 | 406 | cache_size_min,level=3) |
|
405 | 407 | else: |
|
406 | 408 | self.do_full_cache = 1 |
|
407 | 409 | |
|
408 | 410 | self.cache_size = cache_size |
|
409 | 411 | self.input_sep = input_sep |
|
410 | 412 | |
|
411 | 413 | # we need a reference to the user-level namespace |
|
412 | 414 | self.user_ns = user_ns |
|
413 | 415 | # and to the user's input |
|
414 | 416 | self.input_hist = input_hist |
|
415 | 417 | |
|
416 | 418 | # Set input prompt strings and colors |
|
417 | 419 | if cache_size == 0: |
|
418 | 420 | if ps1.find('%n') > -1 or ps1.find('\\#') > -1: ps1 = '>>> ' |
|
419 | 421 | if ps2.find('%n') > -1 or ps2.find('\\#') > -1: ps2 = '... ' |
|
420 | 422 | self.ps1_str = self._set_prompt_str(ps1,'In [\\#]: ','>>> ') |
|
421 | 423 | self.ps2_str = self._set_prompt_str(ps2,' .\\D.: ','... ') |
|
422 | 424 | self.ps_out_str = self._set_prompt_str(ps_out,'Out[\\#]: ','') |
|
423 | 425 | |
|
424 | 426 | self.color_table = PromptColors |
|
425 | 427 | self.prompt1 = Prompt1(self,sep=input_sep,prompt=self.ps1_str, |
|
426 | 428 | pad_left=pad_left) |
|
427 | 429 | self.prompt2 = Prompt2(self,prompt=self.ps2_str,pad_left=pad_left) |
|
428 | 430 | self.prompt_out = PromptOut(self,sep='',prompt=self.ps_out_str, |
|
429 | 431 | pad_left=pad_left) |
|
430 | 432 | self.set_colors(colors) |
|
431 | 433 | |
|
432 | 434 | # other more normal stuff |
|
433 | 435 | # b/c each call to the In[] prompt raises it by 1, even the first. |
|
434 | 436 | self.prompt_count = 0 |
|
435 | 437 | self.cache_count = 1 |
|
436 | 438 | # Store the last prompt string each time, we need it for aligning |
|
437 | 439 | # continuation and auto-rewrite prompts |
|
438 | 440 | self.last_prompt = '' |
|
439 | 441 | self.entries = [None] # output counter starts at 1 for the user |
|
440 | 442 | self.Pprint = Pprint |
|
441 | 443 | self.output_sep = output_sep |
|
442 | 444 | self.output_sep2 = output_sep2 |
|
443 | 445 | self._,self.__,self.___ = '','','' |
|
444 | 446 | self.pprint_types = map(type,[(),[],{}]) |
|
445 | 447 | |
|
446 | 448 | # these are deliberately global: |
|
447 | 449 | to_user_ns = {'_':self._,'__':self.__,'___':self.___} |
|
448 | 450 | self.user_ns.update(to_user_ns) |
|
449 | 451 | |
|
450 | 452 | def _set_prompt_str(self,p_str,cache_def,no_cache_def): |
|
451 | 453 | if p_str is None: |
|
452 | 454 | if self.do_full_cache: |
|
453 | 455 | return cache_def |
|
454 | 456 | else: |
|
455 | 457 | return no_cache_def |
|
456 | 458 | else: |
|
457 | 459 | return p_str |
|
458 | 460 | |
|
459 | 461 | def set_colors(self,colors): |
|
460 | 462 | """Set the active color scheme and configure colors for the three |
|
461 | 463 | prompt subsystems.""" |
|
462 | 464 | |
|
463 | 465 | # FIXME: the prompt_specials global should be gobbled inside this |
|
464 | 466 | # class instead. Do it when cleaning up the whole 3-prompt system. |
|
465 | 467 | global prompt_specials |
|
466 | 468 | if colors.lower()=='nocolor': |
|
467 | 469 | prompt_specials = prompt_specials_nocolor |
|
468 | 470 | else: |
|
469 | 471 | prompt_specials = prompt_specials_color |
|
470 | 472 | |
|
471 | 473 | self.color_table.set_active_scheme(colors) |
|
472 | 474 | self.prompt1.set_colors() |
|
473 | 475 | self.prompt2.set_colors() |
|
474 | 476 | self.prompt_out.set_colors() |
|
475 | 477 | |
|
476 | 478 | def __call__(self,arg=None): |
|
477 | 479 | """Printing with history cache management. |
|
478 | 480 | |
|
479 | 481 | This is invoked everytime the interpreter needs to print, and is |
|
480 | 482 | activated by setting the variable sys.displayhook to it.""" |
|
481 | 483 | |
|
482 | 484 | # If something injected a '_' variable in __builtin__, delete |
|
483 | 485 | # ipython's automatic one so we don't clobber that. gettext() in |
|
484 | 486 | # particular uses _, so we need to stay away from it. |
|
485 | 487 | if '_' in __builtin__.__dict__: |
|
486 | 488 | try: |
|
487 | 489 | del self.user_ns['_'] |
|
488 | 490 | except KeyError: |
|
489 | 491 | pass |
|
490 | 492 | if arg is not None: |
|
491 | 493 | cout_write = Term.cout.write # fast lookup |
|
492 | 494 | # first handle the cache and counters |
|
493 | 495 | self.update(arg) |
|
494 | 496 | # do not print output if input ends in ';' |
|
495 | 497 | if self.input_hist[self.prompt_count].endswith(';\n'): |
|
496 | 498 | return |
|
497 | 499 | # don't use print, puts an extra space |
|
498 | 500 | cout_write(self.output_sep) |
|
499 | 501 | if self.do_full_cache: |
|
500 | 502 | cout_write(str(self.prompt_out)) |
|
501 | 503 | |
|
502 | 504 | if isinstance(arg,Macro): |
|
503 | 505 | print 'Executing Macro...' |
|
504 | 506 | # in case the macro takes a long time to execute |
|
505 | 507 | Term.cout.flush() |
|
506 | 508 | exec arg.value in self.user_ns |
|
507 | 509 | return None |
|
508 | 510 | |
|
509 | 511 | # and now call a possibly user-defined print mechanism |
|
510 | 512 | self.display(arg) |
|
511 | 513 | cout_write(self.output_sep2) |
|
512 | 514 | Term.cout.flush() |
|
513 | 515 | |
|
514 | 516 | def _display(self,arg): |
|
515 | 517 | """Default printer method, uses pprint. |
|
516 | 518 | |
|
517 | 519 | This can be over-ridden by the users to implement special formatting |
|
518 | 520 | of certain types of output.""" |
|
519 | 521 | |
|
520 | 522 | if self.Pprint: |
|
521 | 523 | out = pformat(arg) |
|
522 | 524 | if '\n' in out: |
|
523 | 525 | # So that multi-line strings line up with the left column of |
|
524 | 526 | # the screen, instead of having the output prompt mess up |
|
525 | 527 | # their first line. |
|
526 | 528 | Term.cout.write('\n') |
|
527 | 529 | print >>Term.cout, out |
|
528 | 530 | else: |
|
529 | 531 | print >>Term.cout, arg |
|
530 | 532 | |
|
531 | 533 | # Assign the default display method: |
|
532 | 534 | display = _display |
|
533 | 535 | |
|
534 | 536 | def update(self,arg): |
|
535 | 537 | #print '***cache_count', self.cache_count # dbg |
|
536 | 538 | if self.cache_count >= self.cache_size and self.do_full_cache: |
|
537 | 539 | self.flush() |
|
538 | 540 | # Don't overwrite '_' and friends if '_' is in __builtin__ (otherwise |
|
539 | 541 | # we cause buggy behavior for things like gettext). |
|
540 | 542 | if '_' not in __builtin__.__dict__: |
|
541 | 543 | self.___ = self.__ |
|
542 | 544 | self.__ = self._ |
|
543 | 545 | self._ = arg |
|
544 | 546 | self.user_ns.update({'_':self._,'__':self.__,'___':self.___}) |
|
545 | 547 | |
|
546 | 548 | # hackish access to top-level namespace to create _1,_2... dynamically |
|
547 | 549 | to_main = {} |
|
548 | 550 | if self.do_full_cache: |
|
549 | 551 | self.cache_count += 1 |
|
550 | 552 | self.entries.append(arg) |
|
551 | 553 | new_result = '_'+`self.prompt_count` |
|
552 | 554 | to_main[new_result] = self.entries[-1] |
|
553 | 555 | self.user_ns.update(to_main) |
|
554 | 556 | self.user_ns['_oh'][self.prompt_count] = arg |
|
555 | 557 | |
|
556 | 558 | def flush(self): |
|
557 | 559 | if not self.do_full_cache: |
|
558 | 560 | raise ValueError,"You shouldn't have reached the cache flush "\ |
|
559 | 561 | "if full caching is not enabled!" |
|
560 | 562 | warn('Output cache limit (currently '+\ |
|
561 | 563 | `self.cache_count`+' entries) hit.\n' |
|
562 | 564 | 'Flushing cache and resetting history counter...\n' |
|
563 | 565 | 'The only history variables available will be _,__,___ and _1\n' |
|
564 | 566 | 'with the current result.') |
|
565 | 567 | # delete auto-generated vars from global namespace |
|
566 | 568 | for n in range(1,self.prompt_count + 1): |
|
567 | 569 | key = '_'+`n` |
|
568 | 570 | try: |
|
569 | 571 | del self.user_ns[key] |
|
570 | 572 | except: pass |
|
571 | 573 | self.prompt_count = 1 |
|
572 | 574 | self.cache_count = 1 |
@@ -1,74 +1,76 b'' | |||
|
1 | 1 | # -*- coding: utf-8 -*- |
|
2 | 2 | """Release data for the IPython project. |
|
3 | 3 | |
|
4 |
$Id: Release.py |
|
|
4 | $Id: Release.py 951 2005-12-25 00:57:24Z fperez $""" | |
|
5 | 5 | |
|
6 | 6 | #***************************************************************************** |
|
7 | 7 | # Copyright (C) 2001-2005 Fernando Perez <fperez@colorado.edu> |
|
8 | 8 | # |
|
9 | 9 | # Copyright (c) 2001 Janko Hauser <jhauser@zscout.de> and Nathaniel Gray |
|
10 | 10 | # <n8gray@caltech.edu> |
|
11 | 11 | # |
|
12 | 12 | # Distributed under the terms of the BSD License. The full license is in |
|
13 | 13 | # the file COPYING, distributed as part of this software. |
|
14 | 14 | #***************************************************************************** |
|
15 | 15 | |
|
16 | 16 | # Name of the package for release purposes. This is the name which labels |
|
17 | 17 | # the tarballs and RPMs made by distutils, so it's best to lowercase it. |
|
18 | 18 | name = 'ipython' |
|
19 | 19 | |
|
20 | 20 | # For versions with substrings (like 0.6.16.svn), use an extra . to separate |
|
21 | 21 | # the new substring. We have to avoid using either dashes or underscores, |
|
22 | 22 | # because bdist_rpm does not accept dashes (an RPM) convention, and |
|
23 | 23 | # bdist_deb does not accept underscores (a Debian convention). |
|
24 | 24 | |
|
25 | 25 | version = '0.6.16.svn' |
|
26 | 26 | |
|
27 |
revision = '$Revision: |
|
|
27 | revision = '$Revision: 951 $' | |
|
28 | 28 | |
|
29 | 29 | description = "An enhanced interactive Python shell." |
|
30 | 30 | |
|
31 | 31 | long_description = \ |
|
32 | 32 | """ |
|
33 | 33 | IPython provides a replacement for the interactive Python interpreter with |
|
34 | 34 | extra functionality. |
|
35 | 35 | |
|
36 | 36 | Main features: |
|
37 | 37 | |
|
38 | 38 | * Comprehensive object introspection. |
|
39 | 39 | |
|
40 | 40 | * Input history, persistent across sessions. |
|
41 | 41 | |
|
42 | 42 | * Caching of output results during a session with automatically generated |
|
43 | 43 | references. |
|
44 | 44 | |
|
45 | 45 | * Readline based name completion. |
|
46 | 46 | |
|
47 | 47 | * Extensible system of 'magic' commands for controlling the environment and |
|
48 | 48 | performing many tasks related either to IPython or the operating system. |
|
49 | 49 | |
|
50 | 50 | * Configuration system with easy switching between different setups (simpler |
|
51 | 51 | than changing $PYTHONSTARTUP environment variables every time). |
|
52 | 52 | |
|
53 | 53 | * Session logging and reloading. |
|
54 | 54 | |
|
55 | 55 | * Extensible syntax processing for special purpose situations. |
|
56 | 56 | |
|
57 | 57 | * Access to the system shell with user-extensible alias system. |
|
58 | 58 | |
|
59 | 59 | * Easily embeddable in other Python programs. |
|
60 | 60 | |
|
61 | 61 | * Integrated access to the pdb debugger and the Python profiler. """ |
|
62 | 62 | |
|
63 | 63 | license = 'BSD' |
|
64 | 64 | |
|
65 | 65 | authors = {'Fernando' : ('Fernando Perez','fperez@colorado.edu'), |
|
66 | 66 | 'Janko' : ('Janko Hauser','jhauser@zscout.de'), |
|
67 | 67 | 'Nathan' : ('Nathaniel Gray','n8gray@caltech.edu') |
|
68 | 68 | } |
|
69 | 69 | |
|
70 | 70 | url = 'http://ipython.scipy.org' |
|
71 | 71 | |
|
72 | download_url = 'http://ipython.scipy.org/dist' | |
|
73 | ||
|
72 | 74 | platforms = ['Linux','Mac OSX','Windows XP/2000/NT','Windows 95/98/ME'] |
|
73 | 75 | |
|
74 | 76 | keywords = ['Interactive','Interpreter','Shell'] |
@@ -1,2104 +1,2112 b'' | |||
|
1 | 1 | # -*- coding: utf-8 -*- |
|
2 | 2 | """ |
|
3 | 3 | IPython -- An enhanced Interactive Python |
|
4 | 4 | |
|
5 | 5 | Requires Python 2.1 or newer. |
|
6 | 6 | |
|
7 | 7 | This file contains all the classes and helper functions specific to IPython. |
|
8 | 8 | |
|
9 |
$Id: iplib.py 9 |
|
|
9 | $Id: iplib.py 951 2005-12-25 00:57:24Z fperez $ | |
|
10 | 10 | """ |
|
11 | 11 | |
|
12 | 12 | #***************************************************************************** |
|
13 | 13 | # Copyright (C) 2001 Janko Hauser <jhauser@zscout.de> and |
|
14 | 14 | # Copyright (C) 2001-2004 Fernando Perez. <fperez@colorado.edu> |
|
15 | 15 | # |
|
16 | 16 | # Distributed under the terms of the BSD License. The full license is in |
|
17 | 17 | # the file COPYING, distributed as part of this software. |
|
18 | 18 | # |
|
19 | 19 | # Note: this code originally subclassed code.InteractiveConsole from the |
|
20 | 20 | # Python standard library. Over time, much of that class has been copied |
|
21 | 21 | # verbatim here for modifications which could not be accomplished by |
|
22 | 22 | # subclassing. The Python License (sec. 2) allows for this, but it's always |
|
23 | 23 | # nice to acknowledge credit where credit is due. |
|
24 | 24 | #***************************************************************************** |
|
25 | 25 | |
|
26 | 26 | #**************************************************************************** |
|
27 | 27 | # Modules and globals |
|
28 | 28 | |
|
29 | 29 | from __future__ import generators # for 2.2 backwards-compatibility |
|
30 | 30 | |
|
31 | 31 | from IPython import Release |
|
32 | 32 | __author__ = '%s <%s>\n%s <%s>' % \ |
|
33 | 33 | ( Release.authors['Janko'] + Release.authors['Fernando'] ) |
|
34 | 34 | __license__ = Release.license |
|
35 | 35 | __version__ = Release.version |
|
36 | 36 | |
|
37 | 37 | # Python standard modules |
|
38 | 38 | import __main__ |
|
39 | 39 | import __builtin__ |
|
40 | 40 | import exceptions |
|
41 | 41 | import keyword |
|
42 | 42 | import new |
|
43 | 43 | import os, sys, shutil |
|
44 | 44 | import code, glob, types, re |
|
45 | 45 | import string, StringIO |
|
46 | 46 | import inspect, pydoc |
|
47 | 47 | import bdb, pdb |
|
48 | 48 | import UserList # don't subclass list so this works with Python2.1 |
|
49 | 49 | from pprint import pprint, pformat |
|
50 | 50 | import cPickle as pickle |
|
51 | 51 | import traceback |
|
52 | 52 | from codeop import CommandCompiler |
|
53 | 53 | |
|
54 | 54 | # IPython's own modules |
|
55 | 55 | import IPython |
|
56 | 56 | from IPython import OInspect,PyColorize,ultraTB |
|
57 |
from IPython. |
|
|
57 | from IPython.ColorANSI import ColorScheme,ColorSchemeTable # too long names | |
|
58 | 58 | from IPython.Logger import Logger |
|
59 | 59 | from IPython.Magic import Magic,magic2python,shlex_split |
|
60 | 60 | from IPython.usage import cmd_line_usage,interactive_usage |
|
61 | 61 | from IPython.Struct import Struct |
|
62 | 62 | from IPython.Itpl import Itpl,itpl,printpl,ItplNS,itplns |
|
63 | 63 | from IPython.FakeModule import FakeModule |
|
64 | 64 | from IPython.background_jobs import BackgroundJobManager |
|
65 | 65 | from IPython.PyColorize import Parser |
|
66 | 66 | from IPython.genutils import * |
|
67 | 67 | |
|
68 | 68 | # Global pointer to the running |
|
69 | 69 | |
|
70 | 70 | # store the builtin raw_input globally, and use this always, in case user code |
|
71 | 71 | # overwrites it (like wx.py.PyShell does) |
|
72 | 72 | raw_input_original = raw_input |
|
73 | 73 | |
|
74 | 74 | #**************************************************************************** |
|
75 | 75 | # Some utility function definitions |
|
76 | 76 | |
|
77 | 77 | class Bunch: pass |
|
78 | 78 | |
|
79 | 79 | def esc_quotes(strng): |
|
80 | 80 | """Return the input string with single and double quotes escaped out""" |
|
81 | 81 | |
|
82 | 82 | return strng.replace('"','\\"').replace("'","\\'") |
|
83 | 83 | |
|
84 | 84 | def import_fail_info(mod_name,fns=None): |
|
85 | 85 | """Inform load failure for a module.""" |
|
86 | 86 | |
|
87 | 87 | if fns == None: |
|
88 | 88 | warn("Loading of %s failed.\n" % (mod_name,)) |
|
89 | 89 | else: |
|
90 | 90 | warn("Loading of %s from %s failed.\n" % (fns,mod_name)) |
|
91 | 91 | |
|
92 | 92 | def qw_lol(indata): |
|
93 | 93 | """qw_lol('a b') -> [['a','b']], |
|
94 | 94 | otherwise it's just a call to qw(). |
|
95 | 95 | |
|
96 | 96 | We need this to make sure the modules_some keys *always* end up as a |
|
97 | 97 | list of lists.""" |
|
98 | 98 | |
|
99 | 99 | if type(indata) in StringTypes: |
|
100 | 100 | return [qw(indata)] |
|
101 | 101 | else: |
|
102 | 102 | return qw(indata) |
|
103 | 103 | |
|
104 | 104 | def ipmagic(arg_s): |
|
105 | 105 | """Call a magic function by name. |
|
106 | 106 | |
|
107 | 107 | Input: a string containing the name of the magic function to call and any |
|
108 | 108 | additional arguments to be passed to the magic. |
|
109 | 109 | |
|
110 | 110 | ipmagic('name -opt foo bar') is equivalent to typing at the ipython |
|
111 | 111 | prompt: |
|
112 | 112 | |
|
113 | 113 | In[1]: %name -opt foo bar |
|
114 | 114 | |
|
115 | 115 | To call a magic without arguments, simply use ipmagic('name'). |
|
116 | 116 | |
|
117 | 117 | This provides a proper Python function to call IPython's magics in any |
|
118 | 118 | valid Python code you can type at the interpreter, including loops and |
|
119 | 119 | compound statements. It is added by IPython to the Python builtin |
|
120 | 120 | namespace upon initialization.""" |
|
121 | 121 | |
|
122 | 122 | args = arg_s.split(' ',1) |
|
123 | 123 | magic_name = args[0] |
|
124 | 124 | if magic_name.startswith(__IPYTHON__.ESC_MAGIC): |
|
125 | 125 | magic_name = magic_name[1:] |
|
126 | 126 | try: |
|
127 | 127 | magic_args = args[1] |
|
128 | 128 | except IndexError: |
|
129 | 129 | magic_args = '' |
|
130 | 130 | fn = getattr(__IPYTHON__,'magic_'+magic_name,None) |
|
131 | 131 | if fn is None: |
|
132 | 132 | error("Magic function `%s` not found." % magic_name) |
|
133 | 133 | else: |
|
134 | 134 | magic_args = __IPYTHON__.var_expand(magic_args) |
|
135 | 135 | return fn(magic_args) |
|
136 | 136 | |
|
137 | 137 | def ipalias(arg_s): |
|
138 | 138 | """Call an alias by name. |
|
139 | 139 | |
|
140 | 140 | Input: a string containing the name of the alias to call and any |
|
141 | 141 | additional arguments to be passed to the magic. |
|
142 | 142 | |
|
143 | 143 | ipalias('name -opt foo bar') is equivalent to typing at the ipython |
|
144 | 144 | prompt: |
|
145 | 145 | |
|
146 | 146 | In[1]: name -opt foo bar |
|
147 | 147 | |
|
148 | 148 | To call an alias without arguments, simply use ipalias('name'). |
|
149 | 149 | |
|
150 | 150 | This provides a proper Python function to call IPython's aliases in any |
|
151 | 151 | valid Python code you can type at the interpreter, including loops and |
|
152 | 152 | compound statements. It is added by IPython to the Python builtin |
|
153 | 153 | namespace upon initialization.""" |
|
154 | 154 | |
|
155 | 155 | args = arg_s.split(' ',1) |
|
156 | 156 | alias_name = args[0] |
|
157 | 157 | try: |
|
158 | 158 | alias_args = args[1] |
|
159 | 159 | except IndexError: |
|
160 | 160 | alias_args = '' |
|
161 | 161 | if alias_name in __IPYTHON__.alias_table: |
|
162 | 162 | __IPYTHON__.call_alias(alias_name,alias_args) |
|
163 | 163 | else: |
|
164 | 164 | error("Alias `%s` not found." % alias_name) |
|
165 | 165 | |
|
166 | 166 | #----------------------------------------------------------------------------- |
|
167 | 167 | # Local use classes |
|
168 | 168 | try: |
|
169 | 169 | from IPython import FlexCompleter |
|
170 | 170 | |
|
171 | 171 | class MagicCompleter(FlexCompleter.Completer): |
|
172 | 172 | """Extension of the completer class to work on %-prefixed lines.""" |
|
173 | 173 | |
|
174 | 174 | def __init__(self,shell,namespace=None,omit__names=0,alias_table=None): |
|
175 | 175 | """MagicCompleter() -> completer |
|
176 | 176 | |
|
177 | 177 | Return a completer object suitable for use by the readline library |
|
178 | 178 | via readline.set_completer(). |
|
179 | 179 | |
|
180 | 180 | Inputs: |
|
181 | 181 | |
|
182 | 182 | - shell: a pointer to the ipython shell itself. This is needed |
|
183 | 183 | because this completer knows about magic functions, and those can |
|
184 | 184 | only be accessed via the ipython instance. |
|
185 | 185 | |
|
186 | 186 | - namespace: an optional dict where completions are performed. |
|
187 | 187 | |
|
188 | 188 | - The optional omit__names parameter sets the completer to omit the |
|
189 | 189 | 'magic' names (__magicname__) for python objects unless the text |
|
190 | 190 | to be completed explicitly starts with one or more underscores. |
|
191 | 191 | |
|
192 | 192 | - If alias_table is supplied, it should be a dictionary of aliases |
|
193 | 193 | to complete. """ |
|
194 | 194 | |
|
195 | 195 | FlexCompleter.Completer.__init__(self,namespace) |
|
196 | 196 | self.magic_prefix = shell.name+'.magic_' |
|
197 | 197 | self.magic_escape = shell.ESC_MAGIC |
|
198 | 198 | self.readline = FlexCompleter.readline |
|
199 | 199 | delims = self.readline.get_completer_delims() |
|
200 | 200 | delims = delims.replace(self.magic_escape,'') |
|
201 | 201 | self.readline.set_completer_delims(delims) |
|
202 | 202 | self.get_line_buffer = self.readline.get_line_buffer |
|
203 | 203 | self.omit__names = omit__names |
|
204 | 204 | self.merge_completions = shell.rc.readline_merge_completions |
|
205 | 205 | |
|
206 | 206 | if alias_table is None: |
|
207 | 207 | alias_table = {} |
|
208 | 208 | self.alias_table = alias_table |
|
209 | 209 | # Regexp to split filenames with spaces in them |
|
210 | 210 | self.space_name_re = re.compile(r'([^\\] )') |
|
211 | 211 | # Hold a local ref. to glob.glob for speed |
|
212 | 212 | self.glob = glob.glob |
|
213 | 213 | # Special handling of backslashes needed in win32 platforms |
|
214 | 214 | if sys.platform == "win32": |
|
215 | 215 | self.clean_glob = self._clean_glob_win32 |
|
216 | 216 | else: |
|
217 | 217 | self.clean_glob = self._clean_glob |
|
218 | 218 | self.matchers = [self.python_matches, |
|
219 | 219 | self.file_matches, |
|
220 | 220 | self.alias_matches, |
|
221 | 221 | self.python_func_kw_matches] |
|
222 | 222 | |
|
223 | 223 | # Code contributed by Alex Schmolck, for ipython/emacs integration |
|
224 | 224 | def all_completions(self, text): |
|
225 | 225 | """Return all possible completions for the benefit of emacs.""" |
|
226 | 226 | |
|
227 | 227 | completions = [] |
|
228 | 228 | try: |
|
229 | 229 | for i in xrange(sys.maxint): |
|
230 | 230 | res = self.complete(text, i) |
|
231 | 231 | |
|
232 | 232 | if not res: break |
|
233 | 233 | |
|
234 | 234 | completions.append(res) |
|
235 | 235 | #XXX workaround for ``notDefined.<tab>`` |
|
236 | 236 | except NameError: |
|
237 | 237 | pass |
|
238 | 238 | return completions |
|
239 | 239 | # /end Alex Schmolck code. |
|
240 | 240 | |
|
241 | 241 | def _clean_glob(self,text): |
|
242 | 242 | return self.glob("%s*" % text) |
|
243 | 243 | |
|
244 | 244 | def _clean_glob_win32(self,text): |
|
245 | 245 | return [f.replace("\\","/") |
|
246 | 246 | for f in self.glob("%s*" % text)] |
|
247 | 247 | |
|
248 | 248 | def file_matches(self, text): |
|
249 | 249 | """Match filneames, expanding ~USER type strings. |
|
250 | 250 | |
|
251 | 251 | Most of the seemingly convoluted logic in this completer is an |
|
252 | 252 | attempt to handle filenames with spaces in them. And yet it's not |
|
253 | 253 | quite perfect, because Python's readline doesn't expose all of the |
|
254 | 254 | GNU readline details needed for this to be done correctly. |
|
255 | 255 | |
|
256 | 256 | For a filename with a space in it, the printed completions will be |
|
257 | 257 | only the parts after what's already been typed (instead of the |
|
258 | 258 | full completions, as is normally done). I don't think with the |
|
259 | 259 | current (as of Python 2.3) Python readline it's possible to do |
|
260 | 260 | better.""" |
|
261 | 261 | |
|
262 | 262 | #print 'Completer->file_matches: <%s>' % text # dbg |
|
263 | 263 | |
|
264 | 264 | # chars that require escaping with backslash - i.e. chars |
|
265 | 265 | # that readline treats incorrectly as delimiters, but we |
|
266 | 266 | # don't want to treat as delimiters in filename matching |
|
267 | 267 | # when escaped with backslash |
|
268 | 268 | |
|
269 | 269 | protectables = ' ()[]{}' |
|
270 | 270 | |
|
271 | 271 | def protect_filename(s): |
|
272 | 272 | return "".join([(ch in protectables and '\\' + ch or ch) |
|
273 | 273 | for ch in s]) |
|
274 | 274 | |
|
275 | 275 | lbuf = self.get_line_buffer()[:self.readline.get_endidx()] |
|
276 | 276 | open_quotes = 0 # track strings with open quotes |
|
277 | 277 | try: |
|
278 | 278 | lsplit = shlex_split(lbuf)[-1] |
|
279 | 279 | except ValueError: |
|
280 | 280 | # typically an unmatched ", or backslash without escaped char. |
|
281 | 281 | if lbuf.count('"')==1: |
|
282 | 282 | open_quotes = 1 |
|
283 | 283 | lsplit = lbuf.split('"')[-1] |
|
284 | 284 | elif lbuf.count("'")==1: |
|
285 | 285 | open_quotes = 1 |
|
286 | 286 | lsplit = lbuf.split("'")[-1] |
|
287 | 287 | else: |
|
288 | 288 | return None |
|
289 | 289 | except IndexError: |
|
290 | 290 | # tab pressed on empty line |
|
291 | 291 | lsplit = "" |
|
292 | 292 | |
|
293 | 293 | if lsplit != protect_filename(lsplit): |
|
294 | 294 | # if protectables are found, do matching on the whole escaped |
|
295 | 295 | # name |
|
296 | 296 | has_protectables = 1 |
|
297 | 297 | text0,text = text,lsplit |
|
298 | 298 | else: |
|
299 | 299 | has_protectables = 0 |
|
300 | 300 | text = os.path.expanduser(text) |
|
301 | 301 | |
|
302 | 302 | if text == "": |
|
303 | 303 | return [protect_filename(f) for f in self.glob("*")] |
|
304 | 304 | |
|
305 | 305 | m0 = self.clean_glob(text.replace('\\','')) |
|
306 | 306 | if has_protectables: |
|
307 | 307 | # If we had protectables, we need to revert our changes to the |
|
308 | 308 | # beginning of filename so that we don't double-write the part |
|
309 | 309 | # of the filename we have so far |
|
310 | 310 | len_lsplit = len(lsplit) |
|
311 | 311 | matches = [text0 + protect_filename(f[len_lsplit:]) for f in m0] |
|
312 | 312 | else: |
|
313 | 313 | if open_quotes: |
|
314 | 314 | # if we have a string with an open quote, we don't need to |
|
315 | 315 | # protect the names at all (and we _shouldn't_, as it |
|
316 | 316 | # would cause bugs when the filesystem call is made). |
|
317 | 317 | matches = m0 |
|
318 | 318 | else: |
|
319 | 319 | matches = [protect_filename(f) for f in m0] |
|
320 | 320 | if len(matches) == 1 and os.path.isdir(matches[0]): |
|
321 | 321 | # Takes care of links to directories also. Use '/' |
|
322 | 322 | # explicitly, even under Windows, so that name completions |
|
323 | 323 | # don't end up escaped. |
|
324 | 324 | matches[0] += '/' |
|
325 | 325 | return matches |
|
326 | 326 | |
|
327 | 327 | def alias_matches(self, text): |
|
328 | 328 | """Match internal system aliases""" |
|
329 | 329 | #print 'Completer->alias_matches:',text # dbg |
|
330 | 330 | text = os.path.expanduser(text) |
|
331 | 331 | aliases = self.alias_table.keys() |
|
332 | 332 | if text == "": |
|
333 | 333 | return aliases |
|
334 | 334 | else: |
|
335 | 335 | return [alias for alias in aliases if alias.startswith(text)] |
|
336 | 336 | |
|
337 | 337 | def python_matches(self,text): |
|
338 | 338 | """Match attributes or global python names""" |
|
339 | 339 | #print 'Completer->python_matches' # dbg |
|
340 | 340 | if "." in text: |
|
341 | 341 | try: |
|
342 | 342 | matches = self.attr_matches(text) |
|
343 | 343 | if text.endswith('.') and self.omit__names: |
|
344 | 344 | if self.omit__names == 1: |
|
345 | 345 | # true if txt is _not_ a __ name, false otherwise: |
|
346 | 346 | no__name = (lambda txt: |
|
347 | 347 | re.match(r'.*\.__.*?__',txt) is None) |
|
348 | 348 | else: |
|
349 | 349 | # true if txt is _not_ a _ name, false otherwise: |
|
350 | 350 | no__name = (lambda txt: |
|
351 | 351 | re.match(r'.*\._.*?',txt) is None) |
|
352 | 352 | matches = filter(no__name, matches) |
|
353 | 353 | except NameError: |
|
354 | 354 | # catches <undefined attributes>.<tab> |
|
355 | 355 | matches = [] |
|
356 | 356 | else: |
|
357 | 357 | matches = self.global_matches(text) |
|
358 | 358 | # this is so completion finds magics when automagic is on: |
|
359 | 359 | if matches == [] and not text.startswith(os.sep): |
|
360 | 360 | matches = self.attr_matches(self.magic_prefix+text) |
|
361 | 361 | return matches |
|
362 | 362 | |
|
363 | 363 | def _default_arguments(self, obj): |
|
364 | 364 | """Return the list of default arguments of obj if it is callable, |
|
365 | 365 | or empty list otherwise.""" |
|
366 | 366 | |
|
367 | 367 | if not (inspect.isfunction(obj) or inspect.ismethod(obj)): |
|
368 | 368 | # for classes, check for __init__,__new__ |
|
369 | 369 | if inspect.isclass(obj): |
|
370 | 370 | obj = (getattr(obj,'__init__',None) or |
|
371 | 371 | getattr(obj,'__new__',None)) |
|
372 | 372 | # for all others, check if they are __call__able |
|
373 | 373 | elif hasattr(obj, '__call__'): |
|
374 | 374 | obj = obj.__call__ |
|
375 | 375 | # XXX: is there a way to handle the builtins ? |
|
376 | 376 | try: |
|
377 | 377 | args,_,_1,defaults = inspect.getargspec(obj) |
|
378 | 378 | if defaults: |
|
379 | 379 | return args[-len(defaults):] |
|
380 | 380 | except TypeError: pass |
|
381 | 381 | return [] |
|
382 | 382 | |
|
383 | 383 | def python_func_kw_matches(self,text): |
|
384 | 384 | """Match named parameters (kwargs) of the last open function""" |
|
385 | 385 | |
|
386 | 386 | if "." in text: # a parameter cannot be dotted |
|
387 | 387 | return [] |
|
388 | 388 | try: regexp = self.__funcParamsRegex |
|
389 | 389 | except AttributeError: |
|
390 | 390 | regexp = self.__funcParamsRegex = re.compile(r''' |
|
391 | 391 | '.*?' | # single quoted strings or |
|
392 | 392 | ".*?" | # double quoted strings or |
|
393 | 393 | \w+ | # identifier |
|
394 | 394 | \S # other characters |
|
395 | 395 | ''', re.VERBOSE | re.DOTALL) |
|
396 | 396 | # 1. find the nearest identifier that comes before an unclosed |
|
397 | 397 | # parenthesis e.g. for "foo (1+bar(x), pa", the candidate is "foo" |
|
398 | 398 | tokens = regexp.findall(self.get_line_buffer()) |
|
399 | 399 | tokens.reverse() |
|
400 | 400 | iterTokens = iter(tokens); openPar = 0 |
|
401 | 401 | for token in iterTokens: |
|
402 | 402 | if token == ')': |
|
403 | 403 | openPar -= 1 |
|
404 | 404 | elif token == '(': |
|
405 | 405 | openPar += 1 |
|
406 | 406 | if openPar > 0: |
|
407 | 407 | # found the last unclosed parenthesis |
|
408 | 408 | break |
|
409 | 409 | else: |
|
410 | 410 | return [] |
|
411 | 411 | # 2. Concatenate any dotted names (e.g. "foo.bar" for "foo.bar(x, pa" ) |
|
412 | 412 | ids = [] |
|
413 | 413 | isId = re.compile(r'\w+$').match |
|
414 | 414 | while True: |
|
415 | 415 | try: |
|
416 | 416 | ids.append(iterTokens.next()) |
|
417 | 417 | if not isId(ids[-1]): |
|
418 | 418 | ids.pop(); break |
|
419 | 419 | if not iterTokens.next() == '.': |
|
420 | 420 | break |
|
421 | 421 | except StopIteration: |
|
422 | 422 | break |
|
423 | 423 | # lookup the candidate callable matches either using global_matches |
|
424 | 424 | # or attr_matches for dotted names |
|
425 | 425 | if len(ids) == 1: |
|
426 | 426 | callableMatches = self.global_matches(ids[0]) |
|
427 | 427 | else: |
|
428 | 428 | callableMatches = self.attr_matches('.'.join(ids[::-1])) |
|
429 | 429 | argMatches = [] |
|
430 | 430 | for callableMatch in callableMatches: |
|
431 | 431 | try: namedArgs = self._default_arguments(eval(callableMatch, |
|
432 | 432 | self.namespace)) |
|
433 | 433 | except: continue |
|
434 | 434 | for namedArg in namedArgs: |
|
435 | 435 | if namedArg.startswith(text): |
|
436 | 436 | argMatches.append("%s=" %namedArg) |
|
437 | 437 | return argMatches |
|
438 | 438 | |
|
439 | 439 | def complete(self, text, state): |
|
440 | 440 | """Return the next possible completion for 'text'. |
|
441 | 441 | |
|
442 | 442 | This is called successively with state == 0, 1, 2, ... until it |
|
443 | 443 | returns None. The completion should begin with 'text'. """ |
|
444 | 444 | |
|
445 | 445 | #print '\n*** COMPLETE: <%s> (%s)' % (text,state) # dbg |
|
446 | 446 | magic_escape = self.magic_escape |
|
447 | 447 | magic_prefix = self.magic_prefix |
|
448 | 448 | |
|
449 | 449 | try: |
|
450 | 450 | if text.startswith(magic_escape): |
|
451 | 451 | text = text.replace(magic_escape,magic_prefix) |
|
452 | 452 | elif text.startswith('~'): |
|
453 | 453 | text = os.path.expanduser(text) |
|
454 | 454 | if state == 0: |
|
455 | 455 | # Extend the list of completions with the results of each |
|
456 | 456 | # matcher, so we return results to the user from all |
|
457 | 457 | # namespaces. |
|
458 | 458 | if self.merge_completions: |
|
459 | 459 | self.matches = [] |
|
460 | 460 | for matcher in self.matchers: |
|
461 | 461 | self.matches.extend(matcher(text)) |
|
462 | 462 | else: |
|
463 | 463 | for matcher in self.matchers: |
|
464 | 464 | self.matches = matcher(text) |
|
465 | 465 | if self.matches: |
|
466 | 466 | break |
|
467 | 467 | |
|
468 | 468 | try: |
|
469 | 469 | return self.matches[state].replace(magic_prefix,magic_escape) |
|
470 | 470 | except IndexError: |
|
471 | 471 | return None |
|
472 | 472 | except: |
|
473 | 473 | # If completion fails, don't annoy the user. |
|
474 | 474 | pass |
|
475 | 475 | |
|
476 | 476 | except ImportError: |
|
477 | 477 | pass # no readline support |
|
478 | 478 | |
|
479 | 479 | except KeyError: |
|
480 | 480 | pass # Windows doesn't set TERM, it doesn't matter |
|
481 | 481 | |
|
482 | 482 | |
|
483 | 483 | class InputList(UserList.UserList): |
|
484 | 484 | """Class to store user input. |
|
485 | 485 | |
|
486 | 486 | It's basically a list, but slices return a string instead of a list, thus |
|
487 | 487 | allowing things like (assuming 'In' is an instance): |
|
488 | 488 | |
|
489 | 489 | exec In[4:7] |
|
490 | 490 | |
|
491 | 491 | or |
|
492 | 492 | |
|
493 | 493 | exec In[5:9] + In[14] + In[21:25]""" |
|
494 | 494 | |
|
495 | 495 | def __getslice__(self,i,j): |
|
496 | 496 | return ''.join(UserList.UserList.__getslice__(self,i,j)) |
|
497 | 497 | |
|
498 | 498 | #**************************************************************************** |
|
499 | 499 | # Local use exceptions |
|
500 | 500 | class SpaceInInput(exceptions.Exception): |
|
501 | 501 | pass |
|
502 | 502 | |
|
503 | 503 | #**************************************************************************** |
|
504 | 504 | # Main IPython class |
|
505 | 505 | |
|
506 | 506 | class InteractiveShell(code.InteractiveConsole, Logger, Magic): |
|
507 | 507 | """An enhanced console for Python.""" |
|
508 | 508 | |
|
509 | 509 | def __init__(self,name,usage=None,rc=Struct(opts=None,args=None), |
|
510 | 510 | user_ns = None,user_global_ns=None,banner2='', |
|
511 | 511 | custom_exceptions=((),None),embedded=False): |
|
512 | 512 | |
|
513 | 513 | # Put a reference to self in builtins so that any form of embedded or |
|
514 | 514 | # imported code can test for being inside IPython. |
|
515 | 515 | __builtin__.__IPYTHON__ = self |
|
516 | 516 | |
|
517 | 517 | # And load into builtins ipmagic/ipalias as well |
|
518 | 518 | __builtin__.ipmagic = ipmagic |
|
519 | 519 | __builtin__.ipalias = ipalias |
|
520 | 520 | |
|
521 | 521 | # Add to __builtin__ other parts of IPython's public API |
|
522 | 522 | __builtin__.ip_set_hook = self.set_hook |
|
523 | 523 | |
|
524 | 524 | # Keep in the builtins a flag for when IPython is active. We set it |
|
525 | 525 | # with setdefault so that multiple nested IPythons don't clobber one |
|
526 | 526 | # another. Each will increase its value by one upon being activated, |
|
527 | 527 | # which also gives us a way to determine the nesting level. |
|
528 | 528 | __builtin__.__dict__.setdefault('__IPYTHON__active',0) |
|
529 | 529 | |
|
530 | 530 | # Inform the user of ipython's fast exit magics. |
|
531 | 531 | _exit = ' Use %Exit or %Quit to exit without confirmation.' |
|
532 | 532 | __builtin__.exit += _exit |
|
533 | 533 | __builtin__.quit += _exit |
|
534 | 534 | |
|
535 | 535 | # We need to know whether the instance is meant for embedding, since |
|
536 | 536 | # global/local namespaces need to be handled differently in that case |
|
537 | 537 | self.embedded = embedded |
|
538 | 538 | |
|
539 | 539 | # compiler command |
|
540 | 540 | self.compile = CommandCompiler() |
|
541 | 541 | |
|
542 | 542 | # User input buffer |
|
543 | 543 | self.buffer = [] |
|
544 | 544 | |
|
545 | 545 | # Default name given in compilation of code |
|
546 | 546 | self.filename = '<ipython console>' |
|
547 | 547 | |
|
548 | 548 | # Create the namespace where the user will operate. user_ns is |
|
549 | 549 | # normally the only one used, and it is passed to the exec calls as |
|
550 | 550 | # the locals argument. But we do carry a user_global_ns namespace |
|
551 | 551 | # given as the exec 'globals' argument, This is useful in embedding |
|
552 | 552 | # situations where the ipython shell opens in a context where the |
|
553 | 553 | # distinction between locals and globals is meaningful. |
|
554 | 554 | |
|
555 | 555 | # FIXME. For some strange reason, __builtins__ is showing up at user |
|
556 | 556 | # level as a dict instead of a module. This is a manual fix, but I |
|
557 | 557 | # should really track down where the problem is coming from. Alex |
|
558 | 558 | # Schmolck reported this problem first. |
|
559 | 559 | |
|
560 | 560 | # A useful post by Alex Martelli on this topic: |
|
561 | 561 | # Re: inconsistent value from __builtins__ |
|
562 | 562 | # Von: Alex Martelli <aleaxit@yahoo.com> |
|
563 | 563 | # Datum: Freitag 01 Oktober 2004 04:45:34 nachmittags/abends |
|
564 | 564 | # Gruppen: comp.lang.python |
|
565 | 565 | # Referenzen: 1 |
|
566 | 566 | |
|
567 | 567 | # Michael Hohn <hohn@hooknose.lbl.gov> wrote: |
|
568 | 568 | # > >>> print type(builtin_check.get_global_binding('__builtins__')) |
|
569 | 569 | # > <type 'dict'> |
|
570 | 570 | # > >>> print type(__builtins__) |
|
571 | 571 | # > <type 'module'> |
|
572 | 572 | # > Is this difference in return value intentional? |
|
573 | 573 | |
|
574 | 574 | # Well, it's documented that '__builtins__' can be either a dictionary |
|
575 | 575 | # or a module, and it's been that way for a long time. Whether it's |
|
576 | 576 | # intentional (or sensible), I don't know. In any case, the idea is that |
|
577 | 577 | # if you need to access the built-in namespace directly, you should start |
|
578 | 578 | # with "import __builtin__" (note, no 's') which will definitely give you |
|
579 | 579 | # a module. Yeah, it's somewhatΒ confusing:-(. |
|
580 | 580 | |
|
581 | 581 | if user_ns is None: |
|
582 | 582 | # Set __name__ to __main__ to better match the behavior of the |
|
583 | 583 | # normal interpreter. |
|
584 | 584 | user_ns = {'__name__' :'__main__', |
|
585 | 585 | '__builtins__' : __builtin__, |
|
586 | 586 | } |
|
587 | 587 | |
|
588 | 588 | if user_global_ns is None: |
|
589 | 589 | user_global_ns = {} |
|
590 | 590 | |
|
591 | 591 | # Assign namespaces |
|
592 | 592 | # This is the namespace where all normal user variables live |
|
593 | 593 | self.user_ns = user_ns |
|
594 | 594 | # Embedded instances require a separate namespace for globals. |
|
595 | 595 | # Normally this one is unused by non-embedded instances. |
|
596 | 596 | self.user_global_ns = user_global_ns |
|
597 | 597 | # A namespace to keep track of internal data structures to prevent |
|
598 | 598 | # them from cluttering user-visible stuff. Will be updated later |
|
599 | 599 | self.internal_ns = {} |
|
600 | 600 | |
|
601 | 601 | # Namespace of system aliases. Each entry in the alias |
|
602 | 602 | # table must be a 2-tuple of the form (N,name), where N is the number |
|
603 | 603 | # of positional arguments of the alias. |
|
604 | 604 | self.alias_table = {} |
|
605 | 605 | |
|
606 | 606 | # A table holding all the namespaces IPython deals with, so that |
|
607 | 607 | # introspection facilities can search easily. |
|
608 | 608 | self.ns_table = {'user':user_ns, |
|
609 | 609 | 'user_global':user_global_ns, |
|
610 | 610 | 'alias':self.alias_table, |
|
611 | 611 | 'internal':self.internal_ns, |
|
612 | 612 | 'builtin':__builtin__.__dict__ |
|
613 | 613 | } |
|
614 | 614 | |
|
615 | 615 | # The user namespace MUST have a pointer to the shell itself. |
|
616 | 616 | self.user_ns[name] = self |
|
617 | 617 | |
|
618 | 618 | # We need to insert into sys.modules something that looks like a |
|
619 | 619 | # module but which accesses the IPython namespace, for shelve and |
|
620 | 620 | # pickle to work interactively. Normally they rely on getting |
|
621 | 621 | # everything out of __main__, but for embedding purposes each IPython |
|
622 | 622 | # instance has its own private namespace, so we can't go shoving |
|
623 | 623 | # everything into __main__. |
|
624 | 624 | |
|
625 | 625 | try: |
|
626 | 626 | main_name = self.user_ns['__name__'] |
|
627 | 627 | except KeyError: |
|
628 | 628 | raise KeyError,'user_ns dictionary MUST have a "__name__" key' |
|
629 | 629 | else: |
|
630 | 630 | #print "pickle hack in place" # dbg |
|
631 | 631 | sys.modules[main_name] = FakeModule(self.user_ns) |
|
632 | 632 | |
|
633 | 633 | # List of input with multi-line handling. |
|
634 | 634 | # Fill its zero entry, user counter starts at 1 |
|
635 | 635 | self.input_hist = InputList(['\n']) |
|
636 | 636 | |
|
637 | 637 | # list of visited directories |
|
638 | 638 | try: |
|
639 | 639 | self.dir_hist = [os.getcwd()] |
|
640 | 640 | except IOError, e: |
|
641 | 641 | self.dir_hist = [] |
|
642 | 642 | |
|
643 | 643 | # dict of output history |
|
644 | 644 | self.output_hist = {} |
|
645 | 645 | |
|
646 | 646 | # dict of things NOT to alias (keywords, builtins and some special magics) |
|
647 | 647 | no_alias = {} |
|
648 | 648 | no_alias_magics = ['cd','popd','pushd','dhist','alias','unalias'] |
|
649 | 649 | for key in keyword.kwlist + no_alias_magics: |
|
650 | 650 | no_alias[key] = 1 |
|
651 | 651 | no_alias.update(__builtin__.__dict__) |
|
652 | 652 | self.no_alias = no_alias |
|
653 | 653 | |
|
654 | 654 | # make global variables for user access to these |
|
655 | 655 | self.user_ns['_ih'] = self.input_hist |
|
656 | 656 | self.user_ns['_oh'] = self.output_hist |
|
657 | 657 | self.user_ns['_dh'] = self.dir_hist |
|
658 | 658 | |
|
659 | 659 | # user aliases to input and output histories |
|
660 | 660 | self.user_ns['In'] = self.input_hist |
|
661 | 661 | self.user_ns['Out'] = self.output_hist |
|
662 | 662 | |
|
663 | 663 | # Store the actual shell's name |
|
664 | 664 | self.name = name |
|
665 | 665 | |
|
666 | 666 | # Object variable to store code object waiting execution. This is |
|
667 | 667 | # used mainly by the multithreaded shells, but it can come in handy in |
|
668 | 668 | # other situations. No need to use a Queue here, since it's a single |
|
669 | 669 | # item which gets cleared once run. |
|
670 | 670 | self.code_to_run = None |
|
671 | 671 | |
|
672 | 672 | # Job manager (for jobs run as background threads) |
|
673 | 673 | self.jobs = BackgroundJobManager() |
|
674 | 674 | # Put the job manager into builtins so it's always there. |
|
675 | 675 | __builtin__.jobs = self.jobs |
|
676 | 676 | |
|
677 | 677 | # escapes for automatic behavior on the command line |
|
678 | 678 | self.ESC_SHELL = '!' |
|
679 | 679 | self.ESC_HELP = '?' |
|
680 | 680 | self.ESC_MAGIC = '%' |
|
681 | 681 | self.ESC_QUOTE = ',' |
|
682 | 682 | self.ESC_QUOTE2 = ';' |
|
683 | 683 | self.ESC_PAREN = '/' |
|
684 | 684 | |
|
685 | 685 | # And their associated handlers |
|
686 | 686 | self.esc_handlers = {self.ESC_PAREN:self.handle_auto, |
|
687 | 687 | self.ESC_QUOTE:self.handle_auto, |
|
688 | 688 | self.ESC_QUOTE2:self.handle_auto, |
|
689 | 689 | self.ESC_MAGIC:self.handle_magic, |
|
690 | 690 | self.ESC_HELP:self.handle_help, |
|
691 | 691 | self.ESC_SHELL:self.handle_shell_escape, |
|
692 | 692 | } |
|
693 | 693 | |
|
694 | 694 | # class initializations |
|
695 | 695 | Logger.__init__(self,log_ns = self.user_ns) |
|
696 | 696 | Magic.__init__(self,self) |
|
697 | 697 | |
|
698 | 698 | # an ugly hack to get a pointer to the shell, so I can start writing |
|
699 | 699 | # magic code via this pointer instead of the current mixin salad. |
|
700 | 700 | Magic.set_shell(self,self) |
|
701 | 701 | |
|
702 | 702 | # Python source parser/formatter for syntax highlighting |
|
703 | 703 | pyformat = Parser().format |
|
704 | 704 | self.pycolorize = lambda src: pyformat(src,'str',self.rc['colors']) |
|
705 | 705 | |
|
706 | 706 | # hooks holds pointers used for user-side customizations |
|
707 | 707 | self.hooks = Struct() |
|
708 | 708 | |
|
709 | 709 | # Set all default hooks, defined in the IPython.hooks module. |
|
710 | 710 | hooks = IPython.hooks |
|
711 | 711 | for hook_name in hooks.__all__: |
|
712 | 712 | self.set_hook(hook_name,getattr(hooks,hook_name)) |
|
713 | 713 | |
|
714 | 714 | # Flag to mark unconditional exit |
|
715 | 715 | self.exit_now = False |
|
716 | 716 | |
|
717 | 717 | self.usage_min = """\ |
|
718 | 718 | An enhanced console for Python. |
|
719 | 719 | Some of its features are: |
|
720 | 720 | - Readline support if the readline library is present. |
|
721 | 721 | - Tab completion in the local namespace. |
|
722 | 722 | - Logging of input, see command-line options. |
|
723 | 723 | - System shell escape via ! , eg !ls. |
|
724 | 724 | - Magic commands, starting with a % (like %ls, %pwd, %cd, etc.) |
|
725 | 725 | - Keeps track of locally defined variables via %who, %whos. |
|
726 | 726 | - Show object information with a ? eg ?x or x? (use ?? for more info). |
|
727 | 727 | """ |
|
728 | 728 | if usage: self.usage = usage |
|
729 | 729 | else: self.usage = self.usage_min |
|
730 | 730 | |
|
731 | 731 | # Storage |
|
732 | 732 | self.rc = rc # This will hold all configuration information |
|
733 | 733 | self.inputcache = [] |
|
734 | 734 | self._boundcache = [] |
|
735 | 735 | self.pager = 'less' |
|
736 | 736 | # temporary files used for various purposes. Deleted at exit. |
|
737 | 737 | self.tempfiles = [] |
|
738 | 738 | |
|
739 | 739 | # Keep track of readline usage (later set by init_readline) |
|
740 | 740 | self.has_readline = 0 |
|
741 | 741 | |
|
742 | 742 | # for pushd/popd management |
|
743 | 743 | try: |
|
744 | 744 | self.home_dir = get_home_dir() |
|
745 | 745 | except HomeDirError,msg: |
|
746 | 746 | fatal(msg) |
|
747 | 747 | |
|
748 | 748 | self.dir_stack = [os.getcwd().replace(self.home_dir,'~')] |
|
749 | 749 | |
|
750 | 750 | # Functions to call the underlying shell. |
|
751 | 751 | |
|
752 | 752 | # utility to expand user variables via Itpl |
|
753 | 753 | self.var_expand = lambda cmd: str(ItplNS(cmd.replace('#','\#'), |
|
754 | 754 | self.user_ns)) |
|
755 | 755 | # The first is similar to os.system, but it doesn't return a value, |
|
756 | 756 | # and it allows interpolation of variables in the user's namespace. |
|
757 | 757 | self.system = lambda cmd: shell(self.var_expand(cmd), |
|
758 | 758 | header='IPython system call: ', |
|
759 | 759 | verbose=self.rc.system_verbose) |
|
760 | 760 | # These are for getoutput and getoutputerror: |
|
761 | 761 | self.getoutput = lambda cmd: \ |
|
762 | 762 | getoutput(self.var_expand(cmd), |
|
763 | 763 | header='IPython system call: ', |
|
764 | 764 | verbose=self.rc.system_verbose) |
|
765 | 765 | self.getoutputerror = lambda cmd: \ |
|
766 | 766 | getoutputerror(str(ItplNS(cmd.replace('#','\#'), |
|
767 | 767 | self.user_ns)), |
|
768 | 768 | header='IPython system call: ', |
|
769 | 769 | verbose=self.rc.system_verbose) |
|
770 | 770 | |
|
771 | 771 | # RegExp for splitting line contents into pre-char//first |
|
772 | 772 | # word-method//rest. For clarity, each group in on one line. |
|
773 | 773 | |
|
774 | 774 | # WARNING: update the regexp if the above escapes are changed, as they |
|
775 | 775 | # are hardwired in. |
|
776 | 776 | |
|
777 | 777 | # Don't get carried away with trying to make the autocalling catch too |
|
778 | 778 | # much: it's better to be conservative rather than to trigger hidden |
|
779 | 779 | # evals() somewhere and end up causing side effects. |
|
780 | 780 | |
|
781 | 781 | self.line_split = re.compile(r'^([\s*,;/])' |
|
782 | 782 | r'([\?\w\.]+\w*\s*)' |
|
783 | 783 | r'(\(?.*$)') |
|
784 | 784 | |
|
785 | 785 | # Original re, keep around for a while in case changes break something |
|
786 | 786 | #self.line_split = re.compile(r'(^[\s*!\?%,/]?)' |
|
787 | 787 | # r'(\s*[\?\w\.]+\w*\s*)' |
|
788 | 788 | # r'(\(?.*$)') |
|
789 | 789 | |
|
790 | 790 | # RegExp to identify potential function names |
|
791 | 791 | self.re_fun_name = re.compile(r'[a-zA-Z_]([a-zA-Z0-9_.]*) *$') |
|
792 | 792 | # RegExp to exclude strings with this start from autocalling |
|
793 | 793 | self.re_exclude_auto = re.compile('^[!=()<>,\*/\+-]|^is ') |
|
794 | 794 | # try to catch also methods for stuff in lists/tuples/dicts: off |
|
795 | 795 | # (experimental). For this to work, the line_split regexp would need |
|
796 | 796 | # to be modified so it wouldn't break things at '['. That line is |
|
797 | 797 | # nasty enough that I shouldn't change it until I can test it _well_. |
|
798 | 798 | #self.re_fun_name = re.compile (r'[a-zA-Z_]([a-zA-Z0-9_.\[\]]*) ?$') |
|
799 | 799 | |
|
800 | 800 | # keep track of where we started running (mainly for crash post-mortem) |
|
801 | 801 | self.starting_dir = os.getcwd() |
|
802 | 802 | |
|
803 | 803 | # Attributes for Logger mixin class, make defaults here |
|
804 | 804 | self._dolog = 0 |
|
805 | 805 | self.LOG = '' |
|
806 | 806 | self.LOGDEF = '.InteractiveShell.log' |
|
807 | 807 | self.LOGMODE = 'over' |
|
808 | 808 | self.LOGHEAD = Itpl( |
|
809 | 809 | """#log# Automatic Logger file. *** THIS MUST BE THE FIRST LINE *** |
|
810 | 810 | #log# DO NOT CHANGE THIS LINE OR THE TWO BELOW |
|
811 | 811 | #log# opts = $self.rc.opts |
|
812 | 812 | #log# args = $self.rc.args |
|
813 | 813 | #log# It is safe to make manual edits below here. |
|
814 | 814 | #log#----------------------------------------------------------------------- |
|
815 | 815 | """) |
|
816 | 816 | # Various switches which can be set |
|
817 | 817 | self.CACHELENGTH = 5000 # this is cheap, it's just text |
|
818 | 818 | self.BANNER = "Python %(version)s on %(platform)s\n" % sys.__dict__ |
|
819 | 819 | self.banner2 = banner2 |
|
820 | 820 | |
|
821 | 821 | # TraceBack handlers: |
|
822 | 822 | # Need two, one for syntax errors and one for other exceptions. |
|
823 | 823 | self.SyntaxTB = ultraTB.ListTB(color_scheme='NoColor') |
|
824 | 824 | # This one is initialized with an offset, meaning we always want to |
|
825 | 825 | # remove the topmost item in the traceback, which is our own internal |
|
826 | 826 | # code. Valid modes: ['Plain','Context','Verbose'] |
|
827 | 827 | self.InteractiveTB = ultraTB.AutoFormattedTB(mode = 'Plain', |
|
828 | 828 | color_scheme='NoColor', |
|
829 | 829 | tb_offset = 1) |
|
830 | 830 | # and add any custom exception handlers the user may have specified |
|
831 | 831 | self.set_custom_exc(*custom_exceptions) |
|
832 | 832 | |
|
833 | 833 | # Object inspector |
|
834 | 834 | ins_colors = OInspect.InspectColors |
|
835 | 835 | code_colors = PyColorize.ANSICodeColors |
|
836 | 836 | self.inspector = OInspect.Inspector(ins_colors,code_colors,'NoColor') |
|
837 | 837 | self.autoindent = 0 |
|
838 | 838 | |
|
839 | 839 | # Make some aliases automatically |
|
840 | 840 | # Prepare list of shell aliases to auto-define |
|
841 | 841 | if os.name == 'posix': |
|
842 | 842 | auto_alias = ('mkdir mkdir', 'rmdir rmdir', |
|
843 | 843 | 'mv mv -i','rm rm -i','cp cp -i', |
|
844 | 844 | 'cat cat','less less','clear clear', |
|
845 | 845 | # a better ls |
|
846 | 846 | 'ls ls -F', |
|
847 | 847 | # long ls |
|
848 | 848 | 'll ls -lF', |
|
849 | 849 | # color ls |
|
850 | 850 | 'lc ls -F -o --color', |
|
851 | 851 | # ls normal files only |
|
852 | 852 | 'lf ls -F -o --color %l | grep ^-', |
|
853 | 853 | # ls symbolic links |
|
854 | 854 | 'lk ls -F -o --color %l | grep ^l', |
|
855 | 855 | # directories or links to directories, |
|
856 | 856 | 'ldir ls -F -o --color %l | grep /$', |
|
857 | 857 | # things which are executable |
|
858 | 858 | 'lx ls -F -o --color %l | grep ^-..x', |
|
859 | 859 | ) |
|
860 | 860 | elif os.name in ['nt','dos']: |
|
861 | 861 | auto_alias = ('dir dir /on', 'ls dir /on', |
|
862 | 862 | 'ddir dir /ad /on', 'ldir dir /ad /on', |
|
863 | 863 | 'mkdir mkdir','rmdir rmdir','echo echo', |
|
864 | 864 | 'ren ren','cls cls','copy copy') |
|
865 | 865 | else: |
|
866 | 866 | auto_alias = () |
|
867 | 867 | self.auto_alias = map(lambda s:s.split(None,1),auto_alias) |
|
868 | 868 | # Call the actual (public) initializer |
|
869 | 869 | self.init_auto_alias() |
|
870 | 870 | # end __init__ |
|
871 | 871 | |
|
872 | 872 | def set_hook(self,name,hook): |
|
873 | 873 | """set_hook(name,hook) -> sets an internal IPython hook. |
|
874 | 874 | |
|
875 | 875 | IPython exposes some of its internal API as user-modifiable hooks. By |
|
876 | 876 | resetting one of these hooks, you can modify IPython's behavior to |
|
877 | 877 | call at runtime your own routines.""" |
|
878 | 878 | |
|
879 | 879 | # At some point in the future, this should validate the hook before it |
|
880 | 880 | # accepts it. Probably at least check that the hook takes the number |
|
881 | 881 | # of args it's supposed to. |
|
882 | 882 | setattr(self.hooks,name,new.instancemethod(hook,self,self.__class__)) |
|
883 | 883 | |
|
884 | 884 | def set_custom_exc(self,exc_tuple,handler): |
|
885 | 885 | """set_custom_exc(exc_tuple,handler) |
|
886 | 886 | |
|
887 | 887 | Set a custom exception handler, which will be called if any of the |
|
888 | 888 | exceptions in exc_tuple occur in the mainloop (specifically, in the |
|
889 | 889 | runcode() method. |
|
890 | 890 | |
|
891 | 891 | Inputs: |
|
892 | 892 | |
|
893 | 893 | - exc_tuple: a *tuple* of valid exceptions to call the defined |
|
894 | 894 | handler for. It is very important that you use a tuple, and NOT A |
|
895 | 895 | LIST here, because of the way Python's except statement works. If |
|
896 | 896 | you only want to trap a single exception, use a singleton tuple: |
|
897 | 897 | |
|
898 | 898 | exc_tuple == (MyCustomException,) |
|
899 | 899 | |
|
900 | 900 | - handler: this must be defined as a function with the following |
|
901 | 901 | basic interface: def my_handler(self,etype,value,tb). |
|
902 | 902 | |
|
903 | 903 | This will be made into an instance method (via new.instancemethod) |
|
904 | 904 | of IPython itself, and it will be called if any of the exceptions |
|
905 | 905 | listed in the exc_tuple are caught. If the handler is None, an |
|
906 | 906 | internal basic one is used, which just prints basic info. |
|
907 | 907 | |
|
908 | 908 | WARNING: by putting in your own exception handler into IPython's main |
|
909 | 909 | execution loop, you run a very good chance of nasty crashes. This |
|
910 | 910 | facility should only be used if you really know what you are doing.""" |
|
911 | 911 | |
|
912 | 912 | assert type(exc_tuple)==type(()) , \ |
|
913 | 913 | "The custom exceptions must be given AS A TUPLE." |
|
914 | 914 | |
|
915 | 915 | def dummy_handler(self,etype,value,tb): |
|
916 | 916 | print '*** Simple custom exception handler ***' |
|
917 | 917 | print 'Exception type :',etype |
|
918 | 918 | print 'Exception value:',value |
|
919 | 919 | print 'Traceback :',tb |
|
920 | 920 | print 'Source code :','\n'.join(self.buffer) |
|
921 | 921 | |
|
922 | 922 | if handler is None: handler = dummy_handler |
|
923 | 923 | |
|
924 | 924 | self.CustomTB = new.instancemethod(handler,self,self.__class__) |
|
925 | 925 | self.custom_exceptions = exc_tuple |
|
926 | 926 | |
|
927 | 927 | def set_custom_completer(self,completer,pos=0): |
|
928 | 928 | """set_custom_completer(completer,pos=0) |
|
929 | 929 | |
|
930 | 930 | Adds a new custom completer function. |
|
931 | 931 | |
|
932 | 932 | The position argument (defaults to 0) is the index in the completers |
|
933 | 933 | list where you want the completer to be inserted.""" |
|
934 | 934 | |
|
935 | 935 | newcomp = new.instancemethod(completer,self.Completer, |
|
936 | 936 | self.Completer.__class__) |
|
937 | 937 | self.Completer.matchers.insert(pos,newcomp) |
|
938 | 938 | |
|
939 | 939 | def complete(self,text): |
|
940 | 940 | """Return a sorted list of all possible completions on text. |
|
941 | 941 | |
|
942 | 942 | Inputs: |
|
943 | 943 | |
|
944 | 944 | - text: a string of text to be completed on. |
|
945 | 945 | |
|
946 | 946 | This is a wrapper around the completion mechanism, similar to what |
|
947 | 947 | readline does at the command line when the TAB key is hit. By |
|
948 | 948 | exposing it as a method, it can be used by other non-readline |
|
949 | 949 | environments (such as GUIs) for text completion. |
|
950 | 950 | |
|
951 | 951 | Simple usage example: |
|
952 | 952 | |
|
953 | 953 | In [1]: x = 'hello' |
|
954 | 954 | |
|
955 | 955 | In [2]: __IP.complete('x.l') |
|
956 | 956 | Out[2]: ['x.ljust', 'x.lower', 'x.lstrip']""" |
|
957 | 957 | |
|
958 | 958 | complete = self.Completer.complete |
|
959 | 959 | state = 0 |
|
960 | 960 | # use a dict so we get unique keys, since ipyhton's multiple |
|
961 | 961 | # completers can return duplicates. |
|
962 | 962 | comps = {} |
|
963 | 963 | while True: |
|
964 | 964 | newcomp = complete(text,state) |
|
965 | 965 | if newcomp is None: |
|
966 | 966 | break |
|
967 | 967 | comps[newcomp] = 1 |
|
968 | 968 | state += 1 |
|
969 | 969 | outcomps = comps.keys() |
|
970 | 970 | outcomps.sort() |
|
971 | 971 | return outcomps |
|
972 | ||
|
973 | def set_completer_frame(self, frame): | |
|
974 | if frame: | |
|
975 | ns = frame.f_globals.copy() | |
|
976 | ns.update(frame.f_locals) | |
|
977 | self.Completer.namespace = ns | |
|
978 | else: | |
|
979 | self.Completer.namespace = self.user_ns | |
|
972 | 980 | |
|
973 | 981 | def post_config_initialization(self): |
|
974 | 982 | """Post configuration init method |
|
975 | 983 | |
|
976 | 984 | This is called after the configuration files have been processed to |
|
977 | 985 | 'finalize' the initialization.""" |
|
978 | 986 | |
|
979 | 987 | rc = self.rc |
|
980 | 988 | |
|
981 | 989 | # Load readline proper |
|
982 | 990 | if rc.readline: |
|
983 | 991 | self.init_readline() |
|
984 | 992 | |
|
985 | 993 | # Set user colors (don't do it in the constructor above so that it doesn't |
|
986 | 994 | # crash if colors option is invalid) |
|
987 | 995 | self.magic_colors(rc.colors) |
|
988 | 996 | |
|
989 | 997 | # Load user aliases |
|
990 | 998 | for alias in rc.alias: |
|
991 | 999 | self.magic_alias(alias) |
|
992 | 1000 | |
|
993 | 1001 | # dynamic data that survives through sessions |
|
994 | 1002 | # XXX make the filename a config option? |
|
995 | 1003 | persist_base = 'persist' |
|
996 | 1004 | if rc.profile: |
|
997 | 1005 | persist_base += '_%s' % rc.profile |
|
998 | 1006 | self.persist_fname = os.path.join(rc.ipythondir,persist_base) |
|
999 | 1007 | |
|
1000 | 1008 | try: |
|
1001 | 1009 | self.persist = pickle.load(file(self.persist_fname)) |
|
1002 | 1010 | except: |
|
1003 | 1011 | self.persist = {} |
|
1004 | 1012 | |
|
1005 | 1013 | def init_auto_alias(self): |
|
1006 | 1014 | """Define some aliases automatically. |
|
1007 | 1015 | |
|
1008 | 1016 | These are ALL parameter-less aliases""" |
|
1009 | 1017 | for alias,cmd in self.auto_alias: |
|
1010 | 1018 | self.alias_table[alias] = (0,cmd) |
|
1011 | 1019 | |
|
1012 | 1020 | def alias_table_validate(self,verbose=0): |
|
1013 | 1021 | """Update information about the alias table. |
|
1014 | 1022 | |
|
1015 | 1023 | In particular, make sure no Python keywords/builtins are in it.""" |
|
1016 | 1024 | |
|
1017 | 1025 | no_alias = self.no_alias |
|
1018 | 1026 | for k in self.alias_table.keys(): |
|
1019 | 1027 | if k in no_alias: |
|
1020 | 1028 | del self.alias_table[k] |
|
1021 | 1029 | if verbose: |
|
1022 | 1030 | print ("Deleting alias <%s>, it's a Python " |
|
1023 | 1031 | "keyword or builtin." % k) |
|
1024 | 1032 | |
|
1025 | 1033 | def set_autoindent(self,value=None): |
|
1026 | 1034 | """Set the autoindent flag, checking for readline support. |
|
1027 | 1035 | |
|
1028 | 1036 | If called with no arguments, it acts as a toggle.""" |
|
1029 | 1037 | |
|
1030 | 1038 | if not self.has_readline: |
|
1031 | 1039 | if os.name == 'posix': |
|
1032 | 1040 | warn("The auto-indent feature requires the readline library") |
|
1033 | 1041 | self.autoindent = 0 |
|
1034 | 1042 | return |
|
1035 | 1043 | if value is None: |
|
1036 | 1044 | self.autoindent = not self.autoindent |
|
1037 | 1045 | else: |
|
1038 | 1046 | self.autoindent = value |
|
1039 | 1047 | |
|
1040 | 1048 | def rc_set_toggle(self,rc_field,value=None): |
|
1041 | 1049 | """Set or toggle a field in IPython's rc config. structure. |
|
1042 | 1050 | |
|
1043 | 1051 | If called with no arguments, it acts as a toggle. |
|
1044 | 1052 | |
|
1045 | 1053 | If called with a non-existent field, the resulting AttributeError |
|
1046 | 1054 | exception will propagate out.""" |
|
1047 | 1055 | |
|
1048 | 1056 | rc_val = getattr(self.rc,rc_field) |
|
1049 | 1057 | if value is None: |
|
1050 | 1058 | value = not rc_val |
|
1051 | 1059 | setattr(self.rc,rc_field,value) |
|
1052 | 1060 | |
|
1053 | 1061 | def user_setup(self,ipythondir,rc_suffix,mode='install'): |
|
1054 | 1062 | """Install the user configuration directory. |
|
1055 | 1063 | |
|
1056 | 1064 | Can be called when running for the first time or to upgrade the user's |
|
1057 | 1065 | .ipython/ directory with the mode parameter. Valid modes are 'install' |
|
1058 | 1066 | and 'upgrade'.""" |
|
1059 | 1067 | |
|
1060 | 1068 | def wait(): |
|
1061 | 1069 | try: |
|
1062 | 1070 | raw_input("Please press <RETURN> to start IPython.") |
|
1063 | 1071 | except EOFError: |
|
1064 | 1072 | print >> Term.cout |
|
1065 | 1073 | print '*'*70 |
|
1066 | 1074 | |
|
1067 | 1075 | cwd = os.getcwd() # remember where we started |
|
1068 | 1076 | glb = glob.glob |
|
1069 | 1077 | print '*'*70 |
|
1070 | 1078 | if mode == 'install': |
|
1071 | 1079 | print \ |
|
1072 | 1080 | """Welcome to IPython. I will try to create a personal configuration directory |
|
1073 | 1081 | where you can customize many aspects of IPython's functionality in:\n""" |
|
1074 | 1082 | else: |
|
1075 | 1083 | print 'I am going to upgrade your configuration in:' |
|
1076 | 1084 | |
|
1077 | 1085 | print ipythondir |
|
1078 | 1086 | |
|
1079 | 1087 | rcdirend = os.path.join('IPython','UserConfig') |
|
1080 | 1088 | cfg = lambda d: os.path.join(d,rcdirend) |
|
1081 | 1089 | try: |
|
1082 | 1090 | rcdir = filter(os.path.isdir,map(cfg,sys.path))[0] |
|
1083 | 1091 | except IOError: |
|
1084 | 1092 | warning = """ |
|
1085 | 1093 | Installation error. IPython's directory was not found. |
|
1086 | 1094 | |
|
1087 | 1095 | Check the following: |
|
1088 | 1096 | |
|
1089 | 1097 | The ipython/IPython directory should be in a directory belonging to your |
|
1090 | 1098 | PYTHONPATH environment variable (that is, it should be in a directory |
|
1091 | 1099 | belonging to sys.path). You can copy it explicitly there or just link to it. |
|
1092 | 1100 | |
|
1093 | 1101 | IPython will proceed with builtin defaults. |
|
1094 | 1102 | """ |
|
1095 | 1103 | warn(warning) |
|
1096 | 1104 | wait() |
|
1097 | 1105 | return |
|
1098 | 1106 | |
|
1099 | 1107 | if mode == 'install': |
|
1100 | 1108 | try: |
|
1101 | 1109 | shutil.copytree(rcdir,ipythondir) |
|
1102 | 1110 | os.chdir(ipythondir) |
|
1103 | 1111 | rc_files = glb("ipythonrc*") |
|
1104 | 1112 | for rc_file in rc_files: |
|
1105 | 1113 | os.rename(rc_file,rc_file+rc_suffix) |
|
1106 | 1114 | except: |
|
1107 | 1115 | warning = """ |
|
1108 | 1116 | |
|
1109 | 1117 | There was a problem with the installation: |
|
1110 | 1118 | %s |
|
1111 | 1119 | Try to correct it or contact the developers if you think it's a bug. |
|
1112 | 1120 | IPython will proceed with builtin defaults.""" % sys.exc_info()[1] |
|
1113 | 1121 | warn(warning) |
|
1114 | 1122 | wait() |
|
1115 | 1123 | return |
|
1116 | 1124 | |
|
1117 | 1125 | elif mode == 'upgrade': |
|
1118 | 1126 | try: |
|
1119 | 1127 | os.chdir(ipythondir) |
|
1120 | 1128 | except: |
|
1121 | 1129 | print """ |
|
1122 | 1130 | Can not upgrade: changing to directory %s failed. Details: |
|
1123 | 1131 | %s |
|
1124 | 1132 | """ % (ipythondir,sys.exc_info()[1]) |
|
1125 | 1133 | wait() |
|
1126 | 1134 | return |
|
1127 | 1135 | else: |
|
1128 | 1136 | sources = glb(os.path.join(rcdir,'[A-Za-z]*')) |
|
1129 | 1137 | for new_full_path in sources: |
|
1130 | 1138 | new_filename = os.path.basename(new_full_path) |
|
1131 | 1139 | if new_filename.startswith('ipythonrc'): |
|
1132 | 1140 | new_filename = new_filename + rc_suffix |
|
1133 | 1141 | # The config directory should only contain files, skip any |
|
1134 | 1142 | # directories which may be there (like CVS) |
|
1135 | 1143 | if os.path.isdir(new_full_path): |
|
1136 | 1144 | continue |
|
1137 | 1145 | if os.path.exists(new_filename): |
|
1138 | 1146 | old_file = new_filename+'.old' |
|
1139 | 1147 | if os.path.exists(old_file): |
|
1140 | 1148 | os.remove(old_file) |
|
1141 | 1149 | os.rename(new_filename,old_file) |
|
1142 | 1150 | shutil.copy(new_full_path,new_filename) |
|
1143 | 1151 | else: |
|
1144 | 1152 | raise ValueError,'unrecognized mode for install:',`mode` |
|
1145 | 1153 | |
|
1146 | 1154 | # Fix line-endings to those native to each platform in the config |
|
1147 | 1155 | # directory. |
|
1148 | 1156 | try: |
|
1149 | 1157 | os.chdir(ipythondir) |
|
1150 | 1158 | except: |
|
1151 | 1159 | print """ |
|
1152 | 1160 | Problem: changing to directory %s failed. |
|
1153 | 1161 | Details: |
|
1154 | 1162 | %s |
|
1155 | 1163 | |
|
1156 | 1164 | Some configuration files may have incorrect line endings. This should not |
|
1157 | 1165 | cause any problems during execution. """ % (ipythondir,sys.exc_info()[1]) |
|
1158 | 1166 | wait() |
|
1159 | 1167 | else: |
|
1160 | 1168 | for fname in glb('ipythonrc*'): |
|
1161 | 1169 | try: |
|
1162 | 1170 | native_line_ends(fname,backup=0) |
|
1163 | 1171 | except IOError: |
|
1164 | 1172 | pass |
|
1165 | 1173 | |
|
1166 | 1174 | if mode == 'install': |
|
1167 | 1175 | print """ |
|
1168 | 1176 | Successful installation! |
|
1169 | 1177 | |
|
1170 | 1178 | Please read the sections 'Initial Configuration' and 'Quick Tips' in the |
|
1171 | 1179 | IPython manual (there are both HTML and PDF versions supplied with the |
|
1172 | 1180 | distribution) to make sure that your system environment is properly configured |
|
1173 | 1181 | to take advantage of IPython's features.""" |
|
1174 | 1182 | else: |
|
1175 | 1183 | print """ |
|
1176 | 1184 | Successful upgrade! |
|
1177 | 1185 | |
|
1178 | 1186 | All files in your directory: |
|
1179 | 1187 | %(ipythondir)s |
|
1180 | 1188 | which would have been overwritten by the upgrade were backed up with a .old |
|
1181 | 1189 | extension. If you had made particular customizations in those files you may |
|
1182 | 1190 | want to merge them back into the new files.""" % locals() |
|
1183 | 1191 | wait() |
|
1184 | 1192 | os.chdir(cwd) |
|
1185 | 1193 | # end user_setup() |
|
1186 | 1194 | |
|
1187 | 1195 | def atexit_operations(self): |
|
1188 | 1196 | """This will be executed at the time of exit. |
|
1189 | 1197 | |
|
1190 | 1198 | Saving of persistent data should be performed here. """ |
|
1191 | 1199 | |
|
1192 | 1200 | # input history |
|
1193 | 1201 | self.savehist() |
|
1194 | 1202 | |
|
1195 | 1203 | # Cleanup all tempfiles left around |
|
1196 | 1204 | for tfile in self.tempfiles: |
|
1197 | 1205 | try: |
|
1198 | 1206 | os.unlink(tfile) |
|
1199 | 1207 | except OSError: |
|
1200 | 1208 | pass |
|
1201 | 1209 | |
|
1202 | 1210 | # save the "persistent data" catch-all dictionary |
|
1203 | 1211 | try: |
|
1204 | 1212 | pickle.dump(self.persist, open(self.persist_fname,"w")) |
|
1205 | 1213 | except: |
|
1206 | 1214 | print "*** ERROR *** persistent data saving failed." |
|
1207 | 1215 | |
|
1208 | 1216 | def savehist(self): |
|
1209 | 1217 | """Save input history to a file (via readline library).""" |
|
1210 | 1218 | try: |
|
1211 | 1219 | self.readline.write_history_file(self.histfile) |
|
1212 | 1220 | except: |
|
1213 | 1221 | print 'Unable to save IPython command history to file: ' + \ |
|
1214 | 1222 | `self.histfile` |
|
1215 | 1223 | |
|
1216 | 1224 | def pre_readline(self): |
|
1217 | 1225 | """readline hook to be used at the start of each line. |
|
1218 | 1226 | |
|
1219 | 1227 | Currently it handles auto-indent only.""" |
|
1220 | 1228 | |
|
1221 | 1229 | self.readline.insert_text(' '* self.readline_indent) |
|
1222 | 1230 | |
|
1223 | 1231 | def init_readline(self): |
|
1224 | 1232 | """Command history completion/saving/reloading.""" |
|
1225 | 1233 | try: |
|
1226 | 1234 | import readline |
|
1227 | 1235 | self.Completer = MagicCompleter(self, |
|
1228 | 1236 | self.user_ns, |
|
1229 | 1237 | self.rc.readline_omit__names, |
|
1230 | 1238 | self.alias_table) |
|
1231 | 1239 | except ImportError,NameError: |
|
1232 | 1240 | # If FlexCompleter failed to import, MagicCompleter won't be |
|
1233 | 1241 | # defined. This can happen because of a problem with readline |
|
1234 | 1242 | self.has_readline = 0 |
|
1235 | 1243 | # no point in bugging windows users with this every time: |
|
1236 | 1244 | if os.name == 'posix': |
|
1237 | 1245 | warn('Readline services not available on this platform.') |
|
1238 | 1246 | else: |
|
1239 | 1247 | import atexit |
|
1240 | 1248 | |
|
1241 | 1249 | # Platform-specific configuration |
|
1242 | 1250 | if os.name == 'nt': |
|
1243 | 1251 | # readline under Windows modifies the default exit behavior |
|
1244 | 1252 | # from being Ctrl-Z/Return to the Unix Ctrl-D one. |
|
1245 | 1253 | __builtin__.exit = __builtin__.quit = \ |
|
1246 | 1254 | ('Use Ctrl-D (i.e. EOF) to exit. ' |
|
1247 | 1255 | 'Use %Exit or %Quit to exit without confirmation.') |
|
1248 | 1256 | self.readline_startup_hook = readline.set_pre_input_hook |
|
1249 | 1257 | else: |
|
1250 | 1258 | self.readline_startup_hook = readline.set_startup_hook |
|
1251 | 1259 | |
|
1252 | 1260 | # Load user's initrc file (readline config) |
|
1253 | 1261 | inputrc_name = os.environ.get('INPUTRC') |
|
1254 | 1262 | if inputrc_name is None: |
|
1255 | 1263 | home_dir = get_home_dir() |
|
1256 | 1264 | if home_dir is not None: |
|
1257 | 1265 | inputrc_name = os.path.join(home_dir,'.inputrc') |
|
1258 | 1266 | if os.path.isfile(inputrc_name): |
|
1259 | 1267 | try: |
|
1260 | 1268 | readline.read_init_file(inputrc_name) |
|
1261 | 1269 | except: |
|
1262 | 1270 | warn('Problems reading readline initialization file <%s>' |
|
1263 | 1271 | % inputrc_name) |
|
1264 | 1272 | |
|
1265 | 1273 | self.has_readline = 1 |
|
1266 | 1274 | self.readline = readline |
|
1267 | 1275 | self.readline_indent = 0 # for auto-indenting via readline |
|
1268 | 1276 | # save this in sys so embedded copies can restore it properly |
|
1269 | 1277 | sys.ipcompleter = self.Completer.complete |
|
1270 | 1278 | readline.set_completer(self.Completer.complete) |
|
1271 | 1279 | |
|
1272 | 1280 | # Configure readline according to user's prefs |
|
1273 | 1281 | for rlcommand in self.rc.readline_parse_and_bind: |
|
1274 | 1282 | readline.parse_and_bind(rlcommand) |
|
1275 | 1283 | |
|
1276 | 1284 | # remove some chars from the delimiters list |
|
1277 | 1285 | delims = readline.get_completer_delims() |
|
1278 | 1286 | delims = delims.translate(string._idmap, |
|
1279 | 1287 | self.rc.readline_remove_delims) |
|
1280 | 1288 | readline.set_completer_delims(delims) |
|
1281 | 1289 | # otherwise we end up with a monster history after a while: |
|
1282 | 1290 | readline.set_history_length(1000) |
|
1283 | 1291 | try: |
|
1284 | 1292 | #print '*** Reading readline history' # dbg |
|
1285 | 1293 | readline.read_history_file(self.histfile) |
|
1286 | 1294 | except IOError: |
|
1287 | 1295 | pass # It doesn't exist yet. |
|
1288 | 1296 | |
|
1289 | 1297 | atexit.register(self.atexit_operations) |
|
1290 | 1298 | del atexit |
|
1291 | 1299 | |
|
1292 | 1300 | # Configure auto-indent for all platforms |
|
1293 | 1301 | self.set_autoindent(self.rc.autoindent) |
|
1294 | 1302 | |
|
1295 | 1303 | def showsyntaxerror(self, filename=None): |
|
1296 | 1304 | """Display the syntax error that just occurred. |
|
1297 | 1305 | |
|
1298 | 1306 | This doesn't display a stack trace because there isn't one. |
|
1299 | 1307 | |
|
1300 | 1308 | If a filename is given, it is stuffed in the exception instead |
|
1301 | 1309 | of what was there before (because Python's parser always uses |
|
1302 | 1310 | "<string>" when reading from a string). |
|
1303 | 1311 | """ |
|
1304 | 1312 | type, value, sys.last_traceback = sys.exc_info() |
|
1305 | 1313 | sys.last_type = type |
|
1306 | 1314 | sys.last_value = value |
|
1307 | 1315 | if filename and type is SyntaxError: |
|
1308 | 1316 | # Work hard to stuff the correct filename in the exception |
|
1309 | 1317 | try: |
|
1310 | 1318 | msg, (dummy_filename, lineno, offset, line) = value |
|
1311 | 1319 | except: |
|
1312 | 1320 | # Not the format we expect; leave it alone |
|
1313 | 1321 | pass |
|
1314 | 1322 | else: |
|
1315 | 1323 | # Stuff in the right filename |
|
1316 | 1324 | try: |
|
1317 | 1325 | # Assume SyntaxError is a class exception |
|
1318 | 1326 | value = SyntaxError(msg, (filename, lineno, offset, line)) |
|
1319 | 1327 | except: |
|
1320 | 1328 | # If that failed, assume SyntaxError is a string |
|
1321 | 1329 | value = msg, (filename, lineno, offset, line) |
|
1322 | 1330 | self.SyntaxTB(type,value,[]) |
|
1323 | 1331 | |
|
1324 | 1332 | def debugger(self): |
|
1325 | 1333 | """Call the pdb debugger.""" |
|
1326 | 1334 | |
|
1327 | 1335 | if not self.rc.pdb: |
|
1328 | 1336 | return |
|
1329 | 1337 | pdb.pm() |
|
1330 | 1338 | |
|
1331 | 1339 | def showtraceback(self,exc_tuple = None,filename=None): |
|
1332 | 1340 | """Display the exception that just occurred.""" |
|
1333 | 1341 | |
|
1334 | 1342 | # Though this won't be called by syntax errors in the input line, |
|
1335 | 1343 | # there may be SyntaxError cases whith imported code. |
|
1336 | 1344 | if exc_tuple is None: |
|
1337 | 1345 | type, value, tb = sys.exc_info() |
|
1338 | 1346 | else: |
|
1339 | 1347 | type, value, tb = exc_tuple |
|
1340 | 1348 | if type is SyntaxError: |
|
1341 | 1349 | self.showsyntaxerror(filename) |
|
1342 | 1350 | else: |
|
1343 | 1351 | sys.last_type = type |
|
1344 | 1352 | sys.last_value = value |
|
1345 | 1353 | sys.last_traceback = tb |
|
1346 | 1354 | self.InteractiveTB() |
|
1347 | 1355 | if self.InteractiveTB.call_pdb and self.has_readline: |
|
1348 | 1356 | # pdb mucks up readline, fix it back |
|
1349 | 1357 | self.readline.set_completer(self.Completer.complete) |
|
1350 | 1358 | |
|
1351 | 1359 | def update_cache(self, line): |
|
1352 | 1360 | """puts line into cache""" |
|
1353 | 1361 | self.inputcache.insert(0, line) # This copies the cache every time ... :-( |
|
1354 | 1362 | if len(self.inputcache) >= self.CACHELENGTH: |
|
1355 | 1363 | self.inputcache.pop() # This not :-) |
|
1356 | 1364 | |
|
1357 | 1365 | def mainloop(self,banner=None): |
|
1358 | 1366 | """Creates the local namespace and starts the mainloop. |
|
1359 | 1367 | |
|
1360 | 1368 | If an optional banner argument is given, it will override the |
|
1361 | 1369 | internally created default banner.""" |
|
1362 | 1370 | |
|
1363 | 1371 | if self.rc.c: # Emulate Python's -c option |
|
1364 | 1372 | self.exec_init_cmd() |
|
1365 | 1373 | if banner is None: |
|
1366 | 1374 | if self.rc.banner: |
|
1367 | 1375 | banner = self.BANNER+self.banner2 |
|
1368 | 1376 | else: |
|
1369 | 1377 | banner = '' |
|
1370 | 1378 | self.interact(banner) |
|
1371 | 1379 | |
|
1372 | 1380 | def exec_init_cmd(self): |
|
1373 | 1381 | """Execute a command given at the command line. |
|
1374 | 1382 | |
|
1375 | 1383 | This emulates Python's -c option.""" |
|
1376 | 1384 | |
|
1377 | 1385 | sys.argv = ['-c'] |
|
1378 | 1386 | self.push(self.rc.c) |
|
1379 | 1387 | |
|
1380 | 1388 | def embed_mainloop(self,header='',local_ns=None,global_ns=None,stack_depth=0): |
|
1381 | 1389 | """Embeds IPython into a running python program. |
|
1382 | 1390 | |
|
1383 | 1391 | Input: |
|
1384 | 1392 | |
|
1385 | 1393 | - header: An optional header message can be specified. |
|
1386 | 1394 | |
|
1387 | 1395 | - local_ns, global_ns: working namespaces. If given as None, the |
|
1388 | 1396 | IPython-initialized one is updated with __main__.__dict__, so that |
|
1389 | 1397 | program variables become visible but user-specific configuration |
|
1390 | 1398 | remains possible. |
|
1391 | 1399 | |
|
1392 | 1400 | - stack_depth: specifies how many levels in the stack to go to |
|
1393 | 1401 | looking for namespaces (when local_ns and global_ns are None). This |
|
1394 | 1402 | allows an intermediate caller to make sure that this function gets |
|
1395 | 1403 | the namespace from the intended level in the stack. By default (0) |
|
1396 | 1404 | it will get its locals and globals from the immediate caller. |
|
1397 | 1405 | |
|
1398 | 1406 | Warning: it's possible to use this in a program which is being run by |
|
1399 | 1407 | IPython itself (via %run), but some funny things will happen (a few |
|
1400 | 1408 | globals get overwritten). In the future this will be cleaned up, as |
|
1401 | 1409 | there is no fundamental reason why it can't work perfectly.""" |
|
1402 | 1410 | |
|
1403 | 1411 | # Get locals and globals from caller |
|
1404 | 1412 | if local_ns is None or global_ns is None: |
|
1405 | 1413 | call_frame = sys._getframe(stack_depth).f_back |
|
1406 | 1414 | |
|
1407 | 1415 | if local_ns is None: |
|
1408 | 1416 | local_ns = call_frame.f_locals |
|
1409 | 1417 | if global_ns is None: |
|
1410 | 1418 | global_ns = call_frame.f_globals |
|
1411 | 1419 | |
|
1412 | 1420 | # Update namespaces and fire up interpreter |
|
1413 | 1421 | self.user_ns = local_ns |
|
1414 | 1422 | self.user_global_ns = global_ns |
|
1415 | 1423 | |
|
1416 | 1424 | # Patch for global embedding to make sure that things don't overwrite |
|
1417 | 1425 | # user globals accidentally. Thanks to Richard <rxe@renre-europe.com> |
|
1418 | 1426 | # FIXME. Test this a bit more carefully (the if.. is new) |
|
1419 | 1427 | if local_ns is None and global_ns is None: |
|
1420 | 1428 | self.user_global_ns.update(__main__.__dict__) |
|
1421 | 1429 | |
|
1422 | 1430 | self.interact(header) |
|
1423 | 1431 | |
|
1424 | 1432 | def interact(self, banner=None): |
|
1425 | 1433 | """Closely emulate the interactive Python console. |
|
1426 | 1434 | |
|
1427 | 1435 | The optional banner argument specify the banner to print |
|
1428 | 1436 | before the first interaction; by default it prints a banner |
|
1429 | 1437 | similar to the one printed by the real Python interpreter, |
|
1430 | 1438 | followed by the current class name in parentheses (so as not |
|
1431 | 1439 | to confuse this with the real interpreter -- since it's so |
|
1432 | 1440 | close!). |
|
1433 | 1441 | |
|
1434 | 1442 | """ |
|
1435 | 1443 | cprt = 'Type "copyright", "credits" or "license" for more information.' |
|
1436 | 1444 | if banner is None: |
|
1437 | 1445 | self.write("Python %s on %s\n%s\n(%s)\n" % |
|
1438 | 1446 | (sys.version, sys.platform, cprt, |
|
1439 | 1447 | self.__class__.__name__)) |
|
1440 | 1448 | else: |
|
1441 | 1449 | self.write(banner) |
|
1442 | 1450 | |
|
1443 | 1451 | more = 0 |
|
1444 | ||
|
1452 | ||
|
1445 | 1453 | # Mark activity in the builtins |
|
1446 | 1454 | __builtin__.__dict__['__IPYTHON__active'] += 1 |
|
1447 | 1455 | |
|
1448 | 1456 | # exit_now is set by a call to %Exit or %Quit |
|
1449 | 1457 | while not self.exit_now: |
|
1450 | 1458 | try: |
|
1451 | 1459 | if more: |
|
1452 | 1460 | prompt = self.outputcache.prompt2 |
|
1453 | 1461 | if self.autoindent: |
|
1454 | 1462 | self.readline_startup_hook(self.pre_readline) |
|
1455 | 1463 | else: |
|
1456 | 1464 | prompt = self.outputcache.prompt1 |
|
1457 | 1465 | try: |
|
1458 | 1466 | line = self.raw_input(prompt) |
|
1459 | 1467 | if self.autoindent: |
|
1460 | 1468 | self.readline_startup_hook(None) |
|
1461 | 1469 | except EOFError: |
|
1462 | 1470 | if self.autoindent: |
|
1463 | 1471 | self.readline_startup_hook(None) |
|
1464 | 1472 | self.write("\n") |
|
1465 | 1473 | if self.rc.confirm_exit: |
|
1466 | 1474 | if ask_yes_no('Do you really want to exit ([y]/n)?','y'): |
|
1467 | 1475 | break |
|
1468 | 1476 | else: |
|
1469 | 1477 | break |
|
1470 | 1478 | else: |
|
1471 | 1479 | more = self.push(line) |
|
1472 | 1480 | # Auto-indent management |
|
1473 | 1481 | if self.autoindent: |
|
1474 | 1482 | if line: |
|
1475 | 1483 | ini_spaces = re.match('^(\s+)',line) |
|
1476 | 1484 | if ini_spaces: |
|
1477 | 1485 | nspaces = ini_spaces.end() |
|
1478 | 1486 | else: |
|
1479 | 1487 | nspaces = 0 |
|
1480 | 1488 | self.readline_indent = nspaces |
|
1481 | 1489 | |
|
1482 | 1490 | if line[-1] == ':': |
|
1483 | 1491 | self.readline_indent += 4 |
|
1484 | 1492 | elif re.match(r'^\s+raise|^\s+return',line): |
|
1485 | 1493 | self.readline_indent -= 4 |
|
1486 | 1494 | else: |
|
1487 | 1495 | self.readline_indent = 0 |
|
1488 | 1496 | |
|
1489 | 1497 | except KeyboardInterrupt: |
|
1490 | 1498 | self.write("\nKeyboardInterrupt\n") |
|
1491 | 1499 | self.resetbuffer() |
|
1492 | 1500 | more = 0 |
|
1493 | 1501 | # keep cache in sync with the prompt counter: |
|
1494 | 1502 | self.outputcache.prompt_count -= 1 |
|
1495 | 1503 | |
|
1496 | 1504 | if self.autoindent: |
|
1497 | 1505 | self.readline_indent = 0 |
|
1498 | 1506 | |
|
1499 | 1507 | except bdb.BdbQuit: |
|
1500 | 1508 | warn("The Python debugger has exited with a BdbQuit exception.\n" |
|
1501 | 1509 | "Because of how pdb handles the stack, it is impossible\n" |
|
1502 | 1510 | "for IPython to properly format this particular exception.\n" |
|
1503 | 1511 | "IPython will resume normal operation.") |
|
1504 | 1512 | |
|
1505 | 1513 | # We are off again... |
|
1506 | 1514 | __builtin__.__dict__['__IPYTHON__active'] -= 1 |
|
1507 | 1515 | |
|
1508 | 1516 | def excepthook(self, type, value, tb): |
|
1509 | 1517 | """One more defense for GUI apps that call sys.excepthook. |
|
1510 | 1518 | |
|
1511 | 1519 | GUI frameworks like wxPython trap exceptions and call |
|
1512 | 1520 | sys.excepthook themselves. I guess this is a feature that |
|
1513 | 1521 | enables them to keep running after exceptions that would |
|
1514 | 1522 | otherwise kill their mainloop. This is a bother for IPython |
|
1515 | 1523 | which excepts to catch all of the program exceptions with a try: |
|
1516 | 1524 | except: statement. |
|
1517 | 1525 | |
|
1518 | 1526 | Normally, IPython sets sys.excepthook to a CrashHandler instance, so if |
|
1519 | 1527 | any app directly invokes sys.excepthook, it will look to the user like |
|
1520 | 1528 | IPython crashed. In order to work around this, we can disable the |
|
1521 | 1529 | CrashHandler and replace it with this excepthook instead, which prints a |
|
1522 | 1530 | regular traceback using our InteractiveTB. In this fashion, apps which |
|
1523 | 1531 | call sys.excepthook will generate a regular-looking exception from |
|
1524 | 1532 | IPython, and the CrashHandler will only be triggered by real IPython |
|
1525 | 1533 | crashes. |
|
1526 | 1534 | |
|
1527 | 1535 | This hook should be used sparingly, only in places which are not likely |
|
1528 | 1536 | to be true IPython errors. |
|
1529 | 1537 | """ |
|
1530 | 1538 | |
|
1531 | 1539 | self.InteractiveTB(type, value, tb, tb_offset=0) |
|
1532 | 1540 | if self.InteractiveTB.call_pdb and self.has_readline: |
|
1533 | 1541 | self.readline.set_completer(self.Completer.complete) |
|
1534 | 1542 | |
|
1535 | 1543 | def call_alias(self,alias,rest=''): |
|
1536 | 1544 | """Call an alias given its name and the rest of the line. |
|
1537 | 1545 | |
|
1538 | 1546 | This function MUST be given a proper alias, because it doesn't make |
|
1539 | 1547 | any checks when looking up into the alias table. The caller is |
|
1540 | 1548 | responsible for invoking it only with a valid alias.""" |
|
1541 | 1549 | |
|
1542 | 1550 | #print 'ALIAS: <%s>+<%s>' % (alias,rest) # dbg |
|
1543 | 1551 | nargs,cmd = self.alias_table[alias] |
|
1544 | 1552 | # Expand the %l special to be the user's input line |
|
1545 | 1553 | if cmd.find('%l') >= 0: |
|
1546 | 1554 | cmd = cmd.replace('%l',rest) |
|
1547 | 1555 | rest = '' |
|
1548 | 1556 | if nargs==0: |
|
1549 | 1557 | # Simple, argument-less aliases |
|
1550 | 1558 | cmd = '%s %s' % (cmd,rest) |
|
1551 | 1559 | else: |
|
1552 | 1560 | # Handle aliases with positional arguments |
|
1553 | 1561 | args = rest.split(None,nargs) |
|
1554 | 1562 | if len(args)< nargs: |
|
1555 | 1563 | error('Alias <%s> requires %s arguments, %s given.' % |
|
1556 | 1564 | (alias,nargs,len(args))) |
|
1557 | 1565 | return |
|
1558 | 1566 | cmd = '%s %s' % (cmd % tuple(args[:nargs]),' '.join(args[nargs:])) |
|
1559 | 1567 | # Now call the macro, evaluating in the user's namespace |
|
1560 | 1568 | try: |
|
1561 | 1569 | self.system(cmd) |
|
1562 | 1570 | except: |
|
1563 | 1571 | self.showtraceback() |
|
1564 | 1572 | |
|
1565 | 1573 | def runlines(self,lines): |
|
1566 | 1574 | """Run a string of one or more lines of source. |
|
1567 | 1575 | |
|
1568 | 1576 | This method is capable of running a string containing multiple source |
|
1569 | 1577 | lines, as if they had been entered at the IPython prompt. Since it |
|
1570 | 1578 | exposes IPython's processing machinery, the given strings can contain |
|
1571 | 1579 | magic calls (%magic), special shell access (!cmd), etc.""" |
|
1572 | 1580 | |
|
1573 | 1581 | # We must start with a clean buffer, in case this is run from an |
|
1574 | 1582 | # interactive IPython session (via a magic, for example). |
|
1575 | 1583 | self.resetbuffer() |
|
1576 | 1584 | lines = lines.split('\n') |
|
1577 | 1585 | more = 0 |
|
1578 | 1586 | for line in lines: |
|
1579 | 1587 | # skip blank lines so we don't mess up the prompt counter, but do |
|
1580 | 1588 | # NOT skip even a blank line if we are in a code block (more is |
|
1581 | 1589 | # true) |
|
1582 | 1590 | if line or more: |
|
1583 | 1591 | more = self.push((self.prefilter(line,more))) |
|
1584 | 1592 | # IPython's runsource returns None if there was an error |
|
1585 | 1593 | # compiling the code. This allows us to stop processing right |
|
1586 | 1594 | # away, so the user gets the error message at the right place. |
|
1587 | 1595 | if more is None: |
|
1588 | 1596 | break |
|
1589 | 1597 | # final newline in case the input didn't have it, so that the code |
|
1590 | 1598 | # actually does get executed |
|
1591 | 1599 | if more: |
|
1592 | 1600 | self.push('\n') |
|
1593 | 1601 | |
|
1594 | 1602 | def runsource(self, source, filename="<input>", symbol="single"): |
|
1595 | 1603 | """Compile and run some source in the interpreter. |
|
1596 | 1604 | |
|
1597 | 1605 | Arguments are as for compile_command(). |
|
1598 | 1606 | |
|
1599 | 1607 | One several things can happen: |
|
1600 | 1608 | |
|
1601 | 1609 | 1) The input is incorrect; compile_command() raised an |
|
1602 | 1610 | exception (SyntaxError or OverflowError). A syntax traceback |
|
1603 | 1611 | will be printed by calling the showsyntaxerror() method. |
|
1604 | 1612 | |
|
1605 | 1613 | 2) The input is incomplete, and more input is required; |
|
1606 | 1614 | compile_command() returned None. Nothing happens. |
|
1607 | 1615 | |
|
1608 | 1616 | 3) The input is complete; compile_command() returned a code |
|
1609 | 1617 | object. The code is executed by calling self.runcode() (which |
|
1610 | 1618 | also handles run-time exceptions, except for SystemExit). |
|
1611 | 1619 | |
|
1612 | 1620 | The return value is: |
|
1613 | 1621 | |
|
1614 | 1622 | - True in case 2 |
|
1615 | 1623 | |
|
1616 | 1624 | - False in the other cases, unless an exception is raised, where |
|
1617 | 1625 | None is returned instead. This can be used by external callers to |
|
1618 | 1626 | know whether to continue feeding input or not. |
|
1619 | 1627 | |
|
1620 | 1628 | The return value can be used to decide whether to use sys.ps1 or |
|
1621 | 1629 | sys.ps2 to prompt the next line.""" |
|
1622 | 1630 | |
|
1623 | 1631 | try: |
|
1624 | 1632 | code = self.compile(source, filename, symbol) |
|
1625 | 1633 | except (OverflowError, SyntaxError, ValueError): |
|
1626 | 1634 | # Case 1 |
|
1627 | 1635 | self.showsyntaxerror(filename) |
|
1628 | 1636 | return None |
|
1629 | 1637 | |
|
1630 | 1638 | if code is None: |
|
1631 | 1639 | # Case 2 |
|
1632 | 1640 | return True |
|
1633 | 1641 | |
|
1634 | 1642 | # Case 3 |
|
1635 | 1643 | # We store the code object so that threaded shells and |
|
1636 | 1644 | # custom exception handlers can access all this info if needed. |
|
1637 | 1645 | # The source corresponding to this can be obtained from the |
|
1638 | 1646 | # buffer attribute as '\n'.join(self.buffer). |
|
1639 | 1647 | self.code_to_run = code |
|
1640 | 1648 | # now actually execute the code object |
|
1641 | 1649 | if self.runcode(code) == 0: |
|
1642 | 1650 | return False |
|
1643 | 1651 | else: |
|
1644 | 1652 | return None |
|
1645 | 1653 | |
|
1646 | 1654 | def runcode(self,code_obj): |
|
1647 | 1655 | """Execute a code object. |
|
1648 | 1656 | |
|
1649 | 1657 | When an exception occurs, self.showtraceback() is called to display a |
|
1650 | 1658 | traceback. |
|
1651 | 1659 | |
|
1652 | 1660 | Return value: a flag indicating whether the code to be run completed |
|
1653 | 1661 | successfully: |
|
1654 | 1662 | |
|
1655 | 1663 | - 0: successful execution. |
|
1656 | 1664 | - 1: an error occurred. |
|
1657 | 1665 | """ |
|
1658 | 1666 | |
|
1659 | 1667 | # Set our own excepthook in case the user code tries to call it |
|
1660 | 1668 | # directly, so that the IPython crash handler doesn't get triggered |
|
1661 | 1669 | old_excepthook,sys.excepthook = sys.excepthook, self.excepthook |
|
1662 | 1670 | outflag = 1 # happens in more places, so it's easier as default |
|
1663 | 1671 | try: |
|
1664 | 1672 | try: |
|
1665 | 1673 | # Embedded instances require separate global/local namespaces |
|
1666 | 1674 | # so they can see both the surrounding (local) namespace and |
|
1667 | 1675 | # the module-level globals when called inside another function. |
|
1668 | 1676 | if self.embedded: |
|
1669 | 1677 | exec code_obj in self.user_global_ns, self.user_ns |
|
1670 | 1678 | # Normal (non-embedded) instances should only have a single |
|
1671 | 1679 | # namespace for user code execution, otherwise functions won't |
|
1672 | 1680 | # see interactive top-level globals. |
|
1673 | 1681 | else: |
|
1674 | 1682 | exec code_obj in self.user_ns |
|
1675 | 1683 | finally: |
|
1676 | 1684 | # Reset our crash handler in place |
|
1677 | 1685 | sys.excepthook = old_excepthook |
|
1678 | 1686 | except SystemExit: |
|
1679 | 1687 | self.resetbuffer() |
|
1680 | 1688 | self.showtraceback() |
|
1681 | 1689 | warn( __builtin__.exit,level=1) |
|
1682 | 1690 | except self.custom_exceptions: |
|
1683 | 1691 | etype,value,tb = sys.exc_info() |
|
1684 | 1692 | self.CustomTB(etype,value,tb) |
|
1685 | 1693 | except: |
|
1686 | 1694 | self.showtraceback() |
|
1687 | 1695 | else: |
|
1688 | 1696 | outflag = 0 |
|
1689 | 1697 | if code.softspace(sys.stdout, 0): |
|
1690 | 1698 | |
|
1691 | 1699 | # Flush out code object which has been run (and source) |
|
1692 | 1700 | self.code_to_run = None |
|
1693 | 1701 | return outflag |
|
1694 | 1702 | |
|
1695 | 1703 | def raw_input(self, prompt=""): |
|
1696 | 1704 | """Write a prompt and read a line. |
|
1697 | 1705 | |
|
1698 | 1706 | The returned line does not include the trailing newline. |
|
1699 | 1707 | When the user enters the EOF key sequence, EOFError is raised. |
|
1700 | 1708 | |
|
1701 | 1709 | The base implementation uses the built-in function |
|
1702 | 1710 | raw_input(); a subclass may replace this with a different |
|
1703 | 1711 | implementation. |
|
1704 | 1712 | """ |
|
1705 | 1713 | return self.prefilter(raw_input_original(prompt), |
|
1706 | 1714 | prompt==self.outputcache.prompt2) |
|
1707 | 1715 | |
|
1708 | 1716 | def split_user_input(self,line): |
|
1709 | 1717 | """Split user input into pre-char, function part and rest.""" |
|
1710 | 1718 | |
|
1711 | 1719 | lsplit = self.line_split.match(line) |
|
1712 | 1720 | if lsplit is None: # no regexp match returns None |
|
1713 | 1721 | try: |
|
1714 | 1722 | iFun,theRest = line.split(None,1) |
|
1715 | 1723 | except ValueError: |
|
1716 | 1724 | iFun,theRest = line,'' |
|
1717 | 1725 | pre = re.match('^(\s*)(.*)',line).groups()[0] |
|
1718 | 1726 | else: |
|
1719 | 1727 | pre,iFun,theRest = lsplit.groups() |
|
1720 | 1728 | |
|
1721 | 1729 | #print 'line:<%s>' % line # dbg |
|
1722 | 1730 | #print 'pre <%s> iFun <%s> rest <%s>' % (pre,iFun.strip(),theRest) # dbg |
|
1723 | 1731 | return pre,iFun.strip(),theRest |
|
1724 | 1732 | |
|
1725 | 1733 | def _prefilter(self, line, continue_prompt): |
|
1726 | 1734 | """Calls different preprocessors, depending on the form of line.""" |
|
1727 | 1735 | |
|
1728 | 1736 | # All handlers *must* return a value, even if it's blank (''). |
|
1729 | 1737 | |
|
1730 | 1738 | # Lines are NOT logged here. Handlers should process the line as |
|
1731 | 1739 | # needed, update the cache AND log it (so that the input cache array |
|
1732 | 1740 | # stays synced). |
|
1733 | 1741 | |
|
1734 | 1742 | # This function is _very_ delicate, and since it's also the one which |
|
1735 | 1743 | # determines IPython's response to user input, it must be as efficient |
|
1736 | 1744 | # as possible. For this reason it has _many_ returns in it, trying |
|
1737 | 1745 | # always to exit as quickly as it can figure out what it needs to do. |
|
1738 | 1746 | |
|
1739 | 1747 | # This function is the main responsible for maintaining IPython's |
|
1740 | 1748 | # behavior respectful of Python's semantics. So be _very_ careful if |
|
1741 | 1749 | # making changes to anything here. |
|
1742 | 1750 | |
|
1743 | 1751 | #..................................................................... |
|
1744 | 1752 | # Code begins |
|
1745 | 1753 | |
|
1746 | 1754 | #if line.startswith('%crash'): raise RuntimeError,'Crash now!' # dbg |
|
1747 | 1755 | |
|
1748 | 1756 | # save the line away in case we crash, so the post-mortem handler can |
|
1749 | 1757 | # record it |
|
1750 | 1758 | self._last_input_line = line |
|
1751 | 1759 | |
|
1752 | 1760 | #print '***line: <%s>' % line # dbg |
|
1753 | 1761 | |
|
1754 | 1762 | # the input history needs to track even empty lines |
|
1755 | 1763 | if not line.strip(): |
|
1756 | 1764 | if not continue_prompt: |
|
1757 | 1765 | self.outputcache.prompt_count -= 1 |
|
1758 | 1766 | return self.handle_normal('',continue_prompt) |
|
1759 | 1767 | |
|
1760 | 1768 | # print '***cont',continue_prompt # dbg |
|
1761 | 1769 | # special handlers are only allowed for single line statements |
|
1762 | 1770 | if continue_prompt and not self.rc.multi_line_specials: |
|
1763 | 1771 | return self.handle_normal(line,continue_prompt) |
|
1764 | 1772 | |
|
1765 | 1773 | # For the rest, we need the structure of the input |
|
1766 | 1774 | pre,iFun,theRest = self.split_user_input(line) |
|
1767 | 1775 | #print 'pre <%s> iFun <%s> rest <%s>' % (pre,iFun,theRest) # dbg |
|
1768 | 1776 | |
|
1769 | 1777 | # First check for explicit escapes in the last/first character |
|
1770 | 1778 | handler = None |
|
1771 | 1779 | if line[-1] == self.ESC_HELP: |
|
1772 | 1780 | handler = self.esc_handlers.get(line[-1]) # the ? can be at the end |
|
1773 | 1781 | if handler is None: |
|
1774 | 1782 | # look at the first character of iFun, NOT of line, so we skip |
|
1775 | 1783 | # leading whitespace in multiline input |
|
1776 | 1784 | handler = self.esc_handlers.get(iFun[0:1]) |
|
1777 | 1785 | if handler is not None: |
|
1778 | 1786 | return handler(line,continue_prompt,pre,iFun,theRest) |
|
1779 | 1787 | # Emacs ipython-mode tags certain input lines |
|
1780 | 1788 | if line.endswith('# PYTHON-MODE'): |
|
1781 | 1789 | return self.handle_emacs(line,continue_prompt) |
|
1782 | 1790 | |
|
1783 | 1791 | # Next, check if we can automatically execute this thing |
|
1784 | 1792 | |
|
1785 | 1793 | # Allow ! in multi-line statements if multi_line_specials is on: |
|
1786 | 1794 | if continue_prompt and self.rc.multi_line_specials and \ |
|
1787 | 1795 | iFun.startswith(self.ESC_SHELL): |
|
1788 | 1796 | return self.handle_shell_escape(line,continue_prompt, |
|
1789 | 1797 | pre=pre,iFun=iFun, |
|
1790 | 1798 | theRest=theRest) |
|
1791 | 1799 | |
|
1792 | 1800 | # Let's try to find if the input line is a magic fn |
|
1793 | 1801 | oinfo = None |
|
1794 | 1802 | if hasattr(self,'magic_'+iFun): |
|
1795 | 1803 | oinfo = self._ofind(iFun) # FIXME - _ofind is part of Magic |
|
1796 | 1804 | if oinfo['ismagic']: |
|
1797 | 1805 | # Be careful not to call magics when a variable assignment is |
|
1798 | 1806 | # being made (ls='hi', for example) |
|
1799 | 1807 | if self.rc.automagic and \ |
|
1800 | 1808 | (len(theRest)==0 or theRest[0] not in '!=()<>,') and \ |
|
1801 | 1809 | (self.rc.multi_line_specials or not continue_prompt): |
|
1802 | 1810 | return self.handle_magic(line,continue_prompt, |
|
1803 | 1811 | pre,iFun,theRest) |
|
1804 | 1812 | else: |
|
1805 | 1813 | return self.handle_normal(line,continue_prompt) |
|
1806 | 1814 | |
|
1807 | 1815 | # If the rest of the line begins with an (in)equality, assginment or |
|
1808 | 1816 | # function call, we should not call _ofind but simply execute it. |
|
1809 | 1817 | # This avoids spurious geattr() accesses on objects upon assignment. |
|
1810 | 1818 | # |
|
1811 | 1819 | # It also allows users to assign to either alias or magic names true |
|
1812 | 1820 | # python variables (the magic/alias systems always take second seat to |
|
1813 | 1821 | # true python code). |
|
1814 | 1822 | if theRest and theRest[0] in '!=()': |
|
1815 | 1823 | return self.handle_normal(line,continue_prompt) |
|
1816 | 1824 | |
|
1817 | 1825 | if oinfo is None: |
|
1818 | 1826 | oinfo = self._ofind(iFun) # FIXME - _ofind is part of Magic |
|
1819 | 1827 | |
|
1820 | 1828 | if not oinfo['found']: |
|
1821 | 1829 | return self.handle_normal(line,continue_prompt) |
|
1822 | 1830 | else: |
|
1823 | 1831 | #print 'iFun <%s> rest <%s>' % (iFun,theRest) # dbg |
|
1824 | 1832 | if oinfo['isalias']: |
|
1825 | 1833 | return self.handle_alias(line,continue_prompt, |
|
1826 | 1834 | pre,iFun,theRest) |
|
1827 | 1835 | |
|
1828 | 1836 | if self.rc.autocall and \ |
|
1829 | 1837 | not self.re_exclude_auto.match(theRest) and \ |
|
1830 | 1838 | self.re_fun_name.match(iFun) and \ |
|
1831 | 1839 | callable(oinfo['obj']) : |
|
1832 | 1840 | #print 'going auto' # dbg |
|
1833 | 1841 | return self.handle_auto(line,continue_prompt,pre,iFun,theRest) |
|
1834 | 1842 | else: |
|
1835 | 1843 | #print 'was callable?', callable(oinfo['obj']) # dbg |
|
1836 | 1844 | return self.handle_normal(line,continue_prompt) |
|
1837 | 1845 | |
|
1838 | 1846 | # If we get here, we have a normal Python line. Log and return. |
|
1839 | 1847 | return self.handle_normal(line,continue_prompt) |
|
1840 | 1848 | |
|
1841 | 1849 | def _prefilter_dumb(self, line, continue_prompt): |
|
1842 | 1850 | """simple prefilter function, for debugging""" |
|
1843 | 1851 | return self.handle_normal(line,continue_prompt) |
|
1844 | 1852 | |
|
1845 | 1853 | # Set the default prefilter() function (this can be user-overridden) |
|
1846 | 1854 | prefilter = _prefilter |
|
1847 | 1855 | |
|
1848 | 1856 | def handle_normal(self,line,continue_prompt=None, |
|
1849 | 1857 | pre=None,iFun=None,theRest=None): |
|
1850 | 1858 | """Handle normal input lines. Use as a template for handlers.""" |
|
1851 | 1859 | |
|
1852 | 1860 | self.log(line,continue_prompt) |
|
1853 | 1861 | self.update_cache(line) |
|
1854 | 1862 | return line |
|
1855 | 1863 | |
|
1856 | 1864 | def handle_alias(self,line,continue_prompt=None, |
|
1857 | 1865 | pre=None,iFun=None,theRest=None): |
|
1858 | 1866 | """Handle alias input lines. """ |
|
1859 | 1867 | |
|
1860 | 1868 | theRest = esc_quotes(theRest) |
|
1861 | 1869 | line_out = "%s%s.call_alias('%s','%s')" % (pre,self.name,iFun,theRest) |
|
1862 | 1870 | self.log(line_out,continue_prompt) |
|
1863 | 1871 | self.update_cache(line_out) |
|
1864 | 1872 | return line_out |
|
1865 | 1873 | |
|
1866 | 1874 | def handle_shell_escape(self, line, continue_prompt=None, |
|
1867 | 1875 | pre=None,iFun=None,theRest=None): |
|
1868 | 1876 | """Execute the line in a shell, empty return value""" |
|
1869 | 1877 | |
|
1870 | 1878 | #print 'line in :', `line` # dbg |
|
1871 | 1879 | # Example of a special handler. Others follow a similar pattern. |
|
1872 | 1880 | if continue_prompt: # multi-line statements |
|
1873 | 1881 | if iFun.startswith('!!'): |
|
1874 | 1882 | print 'SyntaxError: !! is not allowed in multiline statements' |
|
1875 | 1883 | return pre |
|
1876 | 1884 | else: |
|
1877 | 1885 | cmd = ("%s %s" % (iFun[1:],theRest)).replace('"','\\"') |
|
1878 | 1886 | line_out = '%s%s.system("%s")' % (pre,self.name,cmd) |
|
1879 | 1887 | #line_out = ('%s%s.system(' % (pre,self.name)) + repr(cmd) + ')' |
|
1880 | 1888 | else: # single-line input |
|
1881 | 1889 | if line.startswith('!!'): |
|
1882 | 1890 | # rewrite iFun/theRest to properly hold the call to %sx and |
|
1883 | 1891 | # the actual command to be executed, so handle_magic can work |
|
1884 | 1892 | # correctly |
|
1885 | 1893 | theRest = '%s %s' % (iFun[2:],theRest) |
|
1886 | 1894 | iFun = 'sx' |
|
1887 | 1895 | return self.handle_magic('%ssx %s' % (self.ESC_MAGIC,line[2:]), |
|
1888 | 1896 | continue_prompt,pre,iFun,theRest) |
|
1889 | 1897 | else: |
|
1890 | 1898 | cmd = esc_quotes(line[1:]) |
|
1891 | 1899 | line_out = '%s.system("%s")' % (self.name,cmd) |
|
1892 | 1900 | #line_out = ('%s.system(' % self.name) + repr(cmd)+ ')' |
|
1893 | 1901 | # update cache/log and return |
|
1894 | 1902 | self.log(line_out,continue_prompt) |
|
1895 | 1903 | self.update_cache(line_out) # readline cache gets normal line |
|
1896 | 1904 | #print 'line out r:', `line_out` # dbg |
|
1897 | 1905 | #print 'line out s:', line_out # dbg |
|
1898 | 1906 | return line_out |
|
1899 | 1907 | |
|
1900 | 1908 | def handle_magic(self, line, continue_prompt=None, |
|
1901 | 1909 | pre=None,iFun=None,theRest=None): |
|
1902 | 1910 | """Execute magic functions. |
|
1903 | 1911 | |
|
1904 | 1912 | Also log them with a prepended # so the log is clean Python.""" |
|
1905 | 1913 | |
|
1906 | 1914 | cmd = '%sipmagic("%s")' % (pre,esc_quotes('%s %s' % (iFun,theRest))) |
|
1907 | 1915 | self.log(cmd,continue_prompt) |
|
1908 | 1916 | self.update_cache(line) |
|
1909 | 1917 | #print 'in handle_magic, cmd=<%s>' % cmd # dbg |
|
1910 | 1918 | return cmd |
|
1911 | 1919 | |
|
1912 | 1920 | def handle_auto(self, line, continue_prompt=None, |
|
1913 | 1921 | pre=None,iFun=None,theRest=None): |
|
1914 | 1922 | """Hande lines which can be auto-executed, quoting if requested.""" |
|
1915 | 1923 | |
|
1916 | 1924 | #print 'pre <%s> iFun <%s> rest <%s>' % (pre,iFun,theRest) # dbg |
|
1917 | 1925 | |
|
1918 | 1926 | # This should only be active for single-line input! |
|
1919 | 1927 | if continue_prompt: |
|
1920 | 1928 | return line |
|
1921 | 1929 | |
|
1922 | 1930 | if pre == self.ESC_QUOTE: |
|
1923 | 1931 | # Auto-quote splitting on whitespace |
|
1924 | 1932 | newcmd = '%s("%s")' % (iFun,'", "'.join(theRest.split()) ) |
|
1925 | 1933 | elif pre == self.ESC_QUOTE2: |
|
1926 | 1934 | # Auto-quote whole string |
|
1927 | 1935 | newcmd = '%s("%s")' % (iFun,theRest) |
|
1928 | 1936 | else: |
|
1929 | 1937 | # Auto-paren |
|
1930 | 1938 | if theRest[0:1] in ('=','['): |
|
1931 | 1939 | # Don't autocall in these cases. They can be either |
|
1932 | 1940 | # rebindings of an existing callable's name, or item access |
|
1933 | 1941 | # for an object which is BOTH callable and implements |
|
1934 | 1942 | # __getitem__. |
|
1935 | 1943 | return '%s %s' % (iFun,theRest) |
|
1936 | 1944 | if theRest.endswith(';'): |
|
1937 | 1945 | newcmd = '%s(%s);' % (iFun.rstrip(),theRest[:-1]) |
|
1938 | 1946 | else: |
|
1939 | 1947 | newcmd = '%s(%s)' % (iFun.rstrip(),theRest) |
|
1940 | 1948 | |
|
1941 | 1949 | print >>Term.cout, self.outputcache.prompt1.auto_rewrite() + newcmd |
|
1942 | 1950 | # log what is now valid Python, not the actual user input (without the |
|
1943 | 1951 | # final newline) |
|
1944 | 1952 | self.log(newcmd,continue_prompt) |
|
1945 | 1953 | return newcmd |
|
1946 | 1954 | |
|
1947 | 1955 | def handle_help(self, line, continue_prompt=None, |
|
1948 | 1956 | pre=None,iFun=None,theRest=None): |
|
1949 | 1957 | """Try to get some help for the object. |
|
1950 | 1958 | |
|
1951 | 1959 | obj? or ?obj -> basic information. |
|
1952 | 1960 | obj?? or ??obj -> more details. |
|
1953 | 1961 | """ |
|
1954 | 1962 | |
|
1955 | 1963 | # We need to make sure that we don't process lines which would be |
|
1956 | 1964 | # otherwise valid python, such as "x=1 # what?" |
|
1957 | 1965 | try: |
|
1958 | 1966 | code.compile_command(line) |
|
1959 | 1967 | except SyntaxError: |
|
1960 | 1968 | # We should only handle as help stuff which is NOT valid syntax |
|
1961 | 1969 | if line[0]==self.ESC_HELP: |
|
1962 | 1970 | line = line[1:] |
|
1963 | 1971 | elif line[-1]==self.ESC_HELP: |
|
1964 | 1972 | line = line[:-1] |
|
1965 | 1973 | self.log('#?'+line) |
|
1966 | 1974 | self.update_cache(line) |
|
1967 | 1975 | if line: |
|
1968 | 1976 | self.magic_pinfo(line) |
|
1969 | 1977 | else: |
|
1970 | 1978 | page(self.usage,screen_lines=self.rc.screen_length) |
|
1971 | 1979 | return '' # Empty string is needed here! |
|
1972 | 1980 | except: |
|
1973 | 1981 | # Pass any other exceptions through to the normal handler |
|
1974 | 1982 | return self.handle_normal(line,continue_prompt) |
|
1975 | 1983 | else: |
|
1976 | 1984 | # If the code compiles ok, we should handle it normally |
|
1977 | 1985 | return self.handle_normal(line,continue_prompt) |
|
1978 | 1986 | |
|
1979 | 1987 | def handle_emacs(self,line,continue_prompt=None, |
|
1980 | 1988 | pre=None,iFun=None,theRest=None): |
|
1981 | 1989 | """Handle input lines marked by python-mode.""" |
|
1982 | 1990 | |
|
1983 | 1991 | # Currently, nothing is done. Later more functionality can be added |
|
1984 | 1992 | # here if needed. |
|
1985 | 1993 | |
|
1986 | 1994 | # The input cache shouldn't be updated |
|
1987 | 1995 | |
|
1988 | 1996 | return line |
|
1989 | 1997 | |
|
1990 | 1998 | def write(self,data): |
|
1991 | 1999 | """Write a string to the default output""" |
|
1992 | 2000 | Term.cout.write(data) |
|
1993 | 2001 | |
|
1994 | 2002 | def write_err(self,data): |
|
1995 | 2003 | """Write a string to the default error output""" |
|
1996 | 2004 | Term.cerr.write(data) |
|
1997 | 2005 | |
|
1998 | 2006 | def safe_execfile(self,fname,*where,**kw): |
|
1999 | 2007 | fname = os.path.expanduser(fname) |
|
2000 | 2008 | |
|
2001 | 2009 | # find things also in current directory |
|
2002 | 2010 | dname = os.path.dirname(fname) |
|
2003 | 2011 | if not sys.path.count(dname): |
|
2004 | 2012 | sys.path.append(dname) |
|
2005 | 2013 | |
|
2006 | 2014 | try: |
|
2007 | 2015 | xfile = open(fname) |
|
2008 | 2016 | except: |
|
2009 | 2017 | print >> Term.cerr, \ |
|
2010 | 2018 | 'Could not open file <%s> for safe execution.' % fname |
|
2011 | 2019 | return None |
|
2012 | 2020 | |
|
2013 | 2021 | kw.setdefault('islog',0) |
|
2014 | 2022 | kw.setdefault('quiet',1) |
|
2015 | 2023 | kw.setdefault('exit_ignore',0) |
|
2016 | 2024 | first = xfile.readline() |
|
2017 | 2025 | _LOGHEAD = str(self.LOGHEAD).split('\n',1)[0].strip() |
|
2018 | 2026 | xfile.close() |
|
2019 | 2027 | # line by line execution |
|
2020 | 2028 | if first.startswith(_LOGHEAD) or kw['islog']: |
|
2021 | 2029 | print 'Loading log file <%s> one line at a time...' % fname |
|
2022 | 2030 | if kw['quiet']: |
|
2023 | 2031 | stdout_save = sys.stdout |
|
2024 | 2032 | sys.stdout = StringIO.StringIO() |
|
2025 | 2033 | try: |
|
2026 | 2034 | globs,locs = where[0:2] |
|
2027 | 2035 | except: |
|
2028 | 2036 | try: |
|
2029 | 2037 | globs = locs = where[0] |
|
2030 | 2038 | except: |
|
2031 | 2039 | globs = locs = globals() |
|
2032 | 2040 | badblocks = [] |
|
2033 | 2041 | |
|
2034 | 2042 | # we also need to identify indented blocks of code when replaying |
|
2035 | 2043 | # logs and put them together before passing them to an exec |
|
2036 | 2044 | # statement. This takes a bit of regexp and look-ahead work in the |
|
2037 | 2045 | # file. It's easiest if we swallow the whole thing in memory |
|
2038 | 2046 | # first, and manually walk through the lines list moving the |
|
2039 | 2047 | # counter ourselves. |
|
2040 | 2048 | indent_re = re.compile('\s+\S') |
|
2041 | 2049 | xfile = open(fname) |
|
2042 | 2050 | filelines = xfile.readlines() |
|
2043 | 2051 | xfile.close() |
|
2044 | 2052 | nlines = len(filelines) |
|
2045 | 2053 | lnum = 0 |
|
2046 | 2054 | while lnum < nlines: |
|
2047 | 2055 | line = filelines[lnum] |
|
2048 | 2056 | lnum += 1 |
|
2049 | 2057 | # don't re-insert logger status info into cache |
|
2050 | 2058 | if line.startswith('#log#'): |
|
2051 | 2059 | continue |
|
2052 | 2060 | elif line.startswith('#%s'% self.ESC_MAGIC): |
|
2053 | 2061 | self.update_cache(line[1:]) |
|
2054 | 2062 | line = magic2python(line) |
|
2055 | 2063 | elif line.startswith('#!'): |
|
2056 | 2064 | self.update_cache(line[1:]) |
|
2057 | 2065 | else: |
|
2058 | 2066 | # build a block of code (maybe a single line) for execution |
|
2059 | 2067 | block = line |
|
2060 | 2068 | try: |
|
2061 | 2069 | next = filelines[lnum] # lnum has already incremented |
|
2062 | 2070 | except: |
|
2063 | 2071 | next = None |
|
2064 | 2072 | while next and indent_re.match(next): |
|
2065 | 2073 | block += next |
|
2066 | 2074 | lnum += 1 |
|
2067 | 2075 | try: |
|
2068 | 2076 | next = filelines[lnum] |
|
2069 | 2077 | except: |
|
2070 | 2078 | next = None |
|
2071 | 2079 | # now execute the block of one or more lines |
|
2072 | 2080 | try: |
|
2073 | 2081 | exec block in globs,locs |
|
2074 | 2082 | self.update_cache(block.rstrip()) |
|
2075 | 2083 | except SystemExit: |
|
2076 | 2084 | pass |
|
2077 | 2085 | except: |
|
2078 | 2086 | badblocks.append(block.rstrip()) |
|
2079 | 2087 | if kw['quiet']: # restore stdout |
|
2080 | 2088 | sys.stdout.close() |
|
2081 | 2089 | sys.stdout = stdout_save |
|
2082 | 2090 | print 'Finished replaying log file <%s>' % fname |
|
2083 | 2091 | if badblocks: |
|
2084 | 2092 | print >> sys.stderr, \ |
|
2085 | 2093 | '\nThe following lines/blocks in file <%s> reported errors:' \ |
|
2086 | 2094 | % fname |
|
2087 | 2095 | for badline in badblocks: |
|
2088 | 2096 | print >> sys.stderr, badline |
|
2089 | 2097 | else: # regular file execution |
|
2090 | 2098 | try: |
|
2091 | 2099 | execfile(fname,*where) |
|
2092 | 2100 | except SyntaxError: |
|
2093 | 2101 | etype, evalue = sys.exc_info()[0:2] |
|
2094 | 2102 | self.SyntaxTB(etype,evalue,[]) |
|
2095 | 2103 | warn('Failure executing file: <%s>' % fname) |
|
2096 | 2104 | except SystemExit,status: |
|
2097 | 2105 | if not kw['exit_ignore']: |
|
2098 | 2106 | self.InteractiveTB() |
|
2099 | 2107 | warn('Failure executing file: <%s>' % fname) |
|
2100 | 2108 | except: |
|
2101 | 2109 | self.InteractiveTB() |
|
2102 | 2110 | warn('Failure executing file: <%s>' % fname) |
|
2103 | 2111 | |
|
2104 | 2112 | #************************* end of file <iplib.py> ***************************** |
@@ -1,860 +1,778 b'' | |||
|
1 | 1 | # -*- coding: utf-8 -*- |
|
2 | 2 | """ |
|
3 | 3 | ultraTB.py -- Spice up your tracebacks! |
|
4 | 4 | |
|
5 | 5 | * ColorTB |
|
6 | 6 | I've always found it a bit hard to visually parse tracebacks in Python. The |
|
7 | 7 | ColorTB class is a solution to that problem. It colors the different parts of a |
|
8 | 8 | traceback in a manner similar to what you would expect from a syntax-highlighting |
|
9 | 9 | text editor. |
|
10 | 10 | |
|
11 | 11 | Installation instructions for ColorTB: |
|
12 | 12 | import sys,ultraTB |
|
13 | 13 | sys.excepthook = ultraTB.ColorTB() |
|
14 | 14 | |
|
15 | 15 | * VerboseTB |
|
16 | 16 | I've also included a port of Ka-Ping Yee's "cgitb.py" that produces all kinds |
|
17 | 17 | of useful info when a traceback occurs. Ping originally had it spit out HTML |
|
18 | 18 | and intended it for CGI programmers, but why should they have all the fun? I |
|
19 | 19 | altered it to spit out colored text to the terminal. It's a bit overwhelming, |
|
20 | 20 | but kind of neat, and maybe useful for long-running programs that you believe |
|
21 | 21 | are bug-free. If a crash *does* occur in that type of program you want details. |
|
22 | 22 | Give it a shot--you'll love it or you'll hate it. |
|
23 | 23 | |
|
24 | 24 | Note: |
|
25 | 25 | |
|
26 | 26 | The Verbose mode prints the variables currently visible where the exception |
|
27 | 27 | happened (shortening their strings if too long). This can potentially be |
|
28 | 28 | very slow, if you happen to have a huge data structure whose string |
|
29 | 29 | representation is complex to compute. Your computer may appear to freeze for |
|
30 | 30 | a while with cpu usage at 100%. If this occurs, you can cancel the traceback |
|
31 | 31 | with Ctrl-C (maybe hitting it more than once). |
|
32 | 32 | |
|
33 | 33 | If you encounter this kind of situation often, you may want to use the |
|
34 | 34 | Verbose_novars mode instead of the regular Verbose, which avoids formatting |
|
35 | 35 | variables (but otherwise includes the information and context given by |
|
36 | 36 | Verbose). |
|
37 | 37 | |
|
38 | 38 | |
|
39 | 39 | Installation instructions for ColorTB: |
|
40 | 40 | import sys,ultraTB |
|
41 | 41 | sys.excepthook = ultraTB.VerboseTB() |
|
42 | 42 | |
|
43 | 43 | Note: Much of the code in this module was lifted verbatim from the standard |
|
44 | 44 | library module 'traceback.py' and Ka-Ping Yee's 'cgitb.py'. |
|
45 | 45 | |
|
46 | 46 | * Color schemes |
|
47 | 47 | The colors are defined in the class TBTools through the use of the |
|
48 | 48 | ColorSchemeTable class. Currently the following exist: |
|
49 | 49 | |
|
50 | 50 | - NoColor: allows all of this module to be used in any terminal (the color |
|
51 | 51 | escapes are just dummy blank strings). |
|
52 | 52 | |
|
53 | 53 | - Linux: is meant to look good in a terminal like the Linux console (black |
|
54 | 54 | or very dark background). |
|
55 | 55 | |
|
56 | 56 | - LightBG: similar to Linux but swaps dark/light colors to be more readable |
|
57 | 57 | in light background terminals. |
|
58 | 58 | |
|
59 | 59 | You can implement other color schemes easily, the syntax is fairly |
|
60 | 60 | self-explanatory. Please send back new schemes you develop to the author for |
|
61 | 61 | possible inclusion in future releases. |
|
62 | 62 | |
|
63 |
$Id: ultraTB.py |
|
|
63 | $Id: ultraTB.py 951 2005-12-25 00:57:24Z fperez $""" | |
|
64 | 64 | |
|
65 | 65 | #***************************************************************************** |
|
66 | 66 | # Copyright (C) 2001 Nathaniel Gray <n8gray@caltech.edu> |
|
67 | 67 | # Copyright (C) 2001-2004 Fernando Perez <fperez@colorado.edu> |
|
68 | 68 | # |
|
69 | 69 | # Distributed under the terms of the BSD License. The full license is in |
|
70 | 70 | # the file COPYING, distributed as part of this software. |
|
71 | 71 | #***************************************************************************** |
|
72 | 72 | |
|
73 | 73 | from IPython import Release |
|
74 | 74 | __author__ = '%s <%s>\n%s <%s>' % (Release.authors['Nathan']+ |
|
75 | 75 | Release.authors['Fernando']) |
|
76 | 76 | __license__ = Release.license |
|
77 | 77 | |
|
78 | 78 | # Required modules |
|
79 | 79 | import sys, os, traceback, types, string, time |
|
80 | 80 | import keyword, tokenize, linecache, inspect, pydoc |
|
81 | 81 | from UserDict import UserDict |
|
82 | 82 | |
|
83 | 83 | # IPython's own modules |
|
84 | 84 | # Modified pdb which doesn't damage IPython's readline handling |
|
85 | 85 | from IPython import Debugger |
|
86 | 86 | |
|
87 | 87 | from IPython.Struct import Struct |
|
88 | from IPython.ColorANSI import * | |
|
89 | 88 | from IPython.genutils import Term,uniq_stable,error,info |
|
89 | from IPython.excolors import ExceptionColors | |
|
90 | 90 | |
|
91 | 91 | #--------------------------------------------------------------------------- |
|
92 | 92 | # Code begins |
|
93 | 93 | |
|
94 | 94 | def inspect_error(): |
|
95 | 95 | """Print a message about internal inspect errors. |
|
96 | 96 | |
|
97 | 97 | These are unfortunately quite common.""" |
|
98 | 98 | |
|
99 | 99 | error('Internal Python error in the inspect module.\n' |
|
100 | 100 | 'Below is the traceback from this internal error.\n') |
|
101 | 101 | |
|
102 | # Make a global variable out of the color scheme table used for coloring | |
|
103 | # exception tracebacks. This allows user code to add new schemes at runtime. | |
|
104 | ExceptionColors = ColorSchemeTable() | |
|
105 | ||
|
106 | # Populate it with color schemes | |
|
107 | C = TermColors # shorthand and local lookup | |
|
108 | ExceptionColors.add_scheme(ColorScheme( | |
|
109 | 'NoColor', | |
|
110 | # The color to be used for the top line | |
|
111 | topline = C.NoColor, | |
|
112 | ||
|
113 | # The colors to be used in the traceback | |
|
114 | filename = C.NoColor, | |
|
115 | lineno = C.NoColor, | |
|
116 | name = C.NoColor, | |
|
117 | vName = C.NoColor, | |
|
118 | val = C.NoColor, | |
|
119 | em = C.NoColor, | |
|
120 | ||
|
121 | # Emphasized colors for the last frame of the traceback | |
|
122 | normalEm = C.NoColor, | |
|
123 | filenameEm = C.NoColor, | |
|
124 | linenoEm = C.NoColor, | |
|
125 | nameEm = C.NoColor, | |
|
126 | valEm = C.NoColor, | |
|
127 | ||
|
128 | # Colors for printing the exception | |
|
129 | excName = C.NoColor, | |
|
130 | line = C.NoColor, | |
|
131 | caret = C.NoColor, | |
|
132 | Normal = C.NoColor | |
|
133 | )) | |
|
134 | ||
|
135 | # make some schemes as instances so we can copy them for modification easily | |
|
136 | ExceptionColors.add_scheme(ColorScheme( | |
|
137 | 'Linux', | |
|
138 | # The color to be used for the top line | |
|
139 | topline = C.LightRed, | |
|
140 | ||
|
141 | # The colors to be used in the traceback | |
|
142 | filename = C.Green, | |
|
143 | lineno = C.Green, | |
|
144 | name = C.Purple, | |
|
145 | vName = C.Cyan, | |
|
146 | val = C.Green, | |
|
147 | em = C.LightCyan, | |
|
148 | ||
|
149 | # Emphasized colors for the last frame of the traceback | |
|
150 | normalEm = C.LightCyan, | |
|
151 | filenameEm = C.LightGreen, | |
|
152 | linenoEm = C.LightGreen, | |
|
153 | nameEm = C.LightPurple, | |
|
154 | valEm = C.LightBlue, | |
|
155 | ||
|
156 | # Colors for printing the exception | |
|
157 | excName = C.LightRed, | |
|
158 | line = C.Yellow, | |
|
159 | caret = C.White, | |
|
160 | Normal = C.Normal | |
|
161 | )) | |
|
162 | ||
|
163 | # For light backgrounds, swap dark/light colors | |
|
164 | ExceptionColors.add_scheme(ColorScheme( | |
|
165 | 'LightBG', | |
|
166 | # The color to be used for the top line | |
|
167 | topline = C.Red, | |
|
168 | ||
|
169 | # The colors to be used in the traceback | |
|
170 | filename = C.LightGreen, | |
|
171 | lineno = C.LightGreen, | |
|
172 | name = C.LightPurple, | |
|
173 | vName = C.Cyan, | |
|
174 | val = C.LightGreen, | |
|
175 | em = C.Cyan, | |
|
176 | ||
|
177 | # Emphasized colors for the last frame of the traceback | |
|
178 | normalEm = C.Cyan, | |
|
179 | filenameEm = C.Green, | |
|
180 | linenoEm = C.Green, | |
|
181 | nameEm = C.Purple, | |
|
182 | valEm = C.Blue, | |
|
183 | ||
|
184 | # Colors for printing the exception | |
|
185 | excName = C.Red, | |
|
186 | #line = C.Brown, # brown often is displayed as yellow | |
|
187 | line = C.Red, | |
|
188 | caret = C.Normal, | |
|
189 | Normal = C.Normal | |
|
190 | )) | |
|
191 | ||
|
192 | 102 | class TBTools: |
|
193 | 103 | """Basic tools used by all traceback printer classes.""" |
|
194 | 104 | |
|
195 |
def __init__(self,color_scheme = 'NoColor',call_pdb= |
|
|
105 | def __init__(self,color_scheme = 'NoColor',call_pdb=False): | |
|
196 | 106 | # Whether to call the interactive pdb debugger after printing |
|
197 | 107 | # tracebacks or not |
|
198 | 108 | self.call_pdb = call_pdb |
|
199 | if call_pdb: | |
|
200 | self.pdb = Debugger.Pdb() | |
|
201 | else: | |
|
202 | self.pdb = None | |
|
203 | 109 | |
|
204 | 110 | # Create color table |
|
205 |
self. |
|
|
111 | self.color_scheme_table = ExceptionColors | |
|
206 | 112 | |
|
207 | 113 | self.set_colors(color_scheme) |
|
208 | 114 | self.old_scheme = color_scheme # save initial value for toggles |
|
209 | 115 | |
|
116 | if call_pdb: | |
|
117 | self.pdb = Debugger.Pdb(self.color_scheme_table.active_scheme_name) | |
|
118 | else: | |
|
119 | self.pdb = None | |
|
120 | ||
|
210 | 121 | def set_colors(self,*args,**kw): |
|
211 | 122 | """Shorthand access to the color table scheme selector method.""" |
|
212 | 123 | |
|
213 |
self. |
|
|
124 | self.color_scheme_table.set_active_scheme(*args,**kw) | |
|
214 | 125 | # for convenience, set Colors to the active scheme |
|
215 |
self.Colors = self. |
|
|
126 | self.Colors = self.color_scheme_table.active_colors | |
|
216 | 127 | |
|
217 | 128 | def color_toggle(self): |
|
218 | 129 | """Toggle between the currently active color scheme and NoColor.""" |
|
219 | 130 | |
|
220 |
if self. |
|
|
221 |
self. |
|
|
222 |
self.Colors = self. |
|
|
131 | if self.color_scheme_table.active_scheme_name == 'NoColor': | |
|
132 | self.color_scheme_table.set_active_scheme(self.old_scheme) | |
|
133 | self.Colors = self.color_scheme_table.active_colors | |
|
223 | 134 | else: |
|
224 |
self.old_scheme = self. |
|
|
225 |
self. |
|
|
226 |
self.Colors = self. |
|
|
135 | self.old_scheme = self.color_scheme_table.active_scheme_name | |
|
136 | self.color_scheme_table.set_active_scheme('NoColor') | |
|
137 | self.Colors = self.color_scheme_table.active_colors | |
|
227 | 138 | |
|
228 | 139 | #--------------------------------------------------------------------------- |
|
229 | 140 | class ListTB(TBTools): |
|
230 | 141 | """Print traceback information from a traceback list, with optional color. |
|
231 | 142 | |
|
232 | 143 | Calling: requires 3 arguments: |
|
233 | 144 | (etype, evalue, elist) |
|
234 | 145 | as would be obtained by: |
|
235 | 146 | etype, evalue, tb = sys.exc_info() |
|
236 | 147 | if tb: |
|
237 | 148 | elist = traceback.extract_tb(tb) |
|
238 | 149 | else: |
|
239 | 150 | elist = None |
|
240 | 151 | |
|
241 | 152 | It can thus be used by programs which need to process the traceback before |
|
242 | 153 | printing (such as console replacements based on the code module from the |
|
243 | 154 | standard library). |
|
244 | 155 | |
|
245 | 156 | Because they are meant to be called without a full traceback (only a |
|
246 | 157 | list), instances of this class can't call the interactive pdb debugger.""" |
|
247 | 158 | |
|
248 | 159 | def __init__(self,color_scheme = 'NoColor'): |
|
249 | 160 | TBTools.__init__(self,color_scheme = color_scheme,call_pdb=0) |
|
250 | 161 | |
|
251 | 162 | def __call__(self, etype, value, elist): |
|
252 | 163 | print >> Term.cerr, self.text(etype,value,elist) |
|
253 | 164 | |
|
254 | 165 | def text(self,etype, value, elist,context=5): |
|
255 | 166 | """Return a color formatted string with the traceback info.""" |
|
256 | 167 | |
|
257 | 168 | Colors = self.Colors |
|
258 | 169 | out_string = ['%s%s%s\n' % (Colors.topline,'-'*60,Colors.Normal)] |
|
259 | 170 | if elist: |
|
260 | 171 | out_string.append('Traceback %s(most recent call last)%s:' % \ |
|
261 | 172 | (Colors.normalEm, Colors.Normal) + '\n') |
|
262 | 173 | out_string.extend(self._format_list(elist)) |
|
263 | 174 | lines = self._format_exception_only(etype, value) |
|
264 | 175 | for line in lines[:-1]: |
|
265 | 176 | out_string.append(" "+line) |
|
266 | 177 | out_string.append(lines[-1]) |
|
267 | 178 | return ''.join(out_string) |
|
268 | 179 | |
|
269 | 180 | def _format_list(self, extracted_list): |
|
270 | 181 | """Format a list of traceback entry tuples for printing. |
|
271 | 182 | |
|
272 | 183 | Given a list of tuples as returned by extract_tb() or |
|
273 | 184 | extract_stack(), return a list of strings ready for printing. |
|
274 | 185 | Each string in the resulting list corresponds to the item with the |
|
275 | 186 | same index in the argument list. Each string ends in a newline; |
|
276 | 187 | the strings may contain internal newlines as well, for those items |
|
277 | 188 | whose source text line is not None. |
|
278 | 189 | |
|
279 | 190 | Lifted almost verbatim from traceback.py |
|
280 | 191 | """ |
|
281 | 192 | |
|
282 | 193 | Colors = self.Colors |
|
283 | 194 | list = [] |
|
284 | 195 | for filename, lineno, name, line in extracted_list[:-1]: |
|
285 | 196 | item = ' File %s"%s"%s, line %s%d%s, in %s%s%s\n' % \ |
|
286 | 197 | (Colors.filename, filename, Colors.Normal, |
|
287 | 198 | Colors.lineno, lineno, Colors.Normal, |
|
288 | 199 | Colors.name, name, Colors.Normal) |
|
289 | 200 | if line: |
|
290 | 201 | item = item + ' %s\n' % line.strip() |
|
291 | 202 | list.append(item) |
|
292 | 203 | # Emphasize the last entry |
|
293 | 204 | filename, lineno, name, line = extracted_list[-1] |
|
294 | 205 | item = '%s File %s"%s"%s, line %s%d%s, in %s%s%s%s\n' % \ |
|
295 | 206 | (Colors.normalEm, |
|
296 | 207 | Colors.filenameEm, filename, Colors.normalEm, |
|
297 | 208 | Colors.linenoEm, lineno, Colors.normalEm, |
|
298 | 209 | Colors.nameEm, name, Colors.normalEm, |
|
299 | 210 | Colors.Normal) |
|
300 | 211 | if line: |
|
301 | 212 | item = item + '%s %s%s\n' % (Colors.line, line.strip(), |
|
302 | 213 | Colors.Normal) |
|
303 | 214 | list.append(item) |
|
304 | 215 | return list |
|
305 | 216 | |
|
306 | 217 | def _format_exception_only(self, etype, value): |
|
307 | 218 | """Format the exception part of a traceback. |
|
308 | 219 | |
|
309 | 220 | The arguments are the exception type and value such as given by |
|
310 | 221 | sys.last_type and sys.last_value. The return value is a list of |
|
311 | 222 | strings, each ending in a newline. Normally, the list contains a |
|
312 | 223 | single string; however, for SyntaxError exceptions, it contains |
|
313 | 224 | several lines that (when printed) display detailed information |
|
314 | 225 | about where the syntax error occurred. The message indicating |
|
315 | 226 | which exception occurred is the always last string in the list. |
|
316 | 227 | |
|
317 | 228 | Also lifted nearly verbatim from traceback.py |
|
318 | 229 | """ |
|
319 | 230 | |
|
320 | 231 | Colors = self.Colors |
|
321 | 232 | list = [] |
|
322 | 233 | if type(etype) == types.ClassType: |
|
323 | 234 | stype = Colors.excName + etype.__name__ + Colors.Normal |
|
324 | 235 | else: |
|
325 | 236 | stype = etype # String exceptions don't get special coloring |
|
326 | 237 | if value is None: |
|
327 | 238 | list.append( str(stype) + '\n') |
|
328 | 239 | else: |
|
329 | 240 | if etype is SyntaxError: |
|
330 | 241 | try: |
|
331 | 242 | msg, (filename, lineno, offset, line) = value |
|
332 | 243 | except: |
|
333 | 244 | pass |
|
334 | 245 | else: |
|
335 | 246 | #print 'filename is',filename # dbg |
|
336 | 247 | if not filename: filename = "<string>" |
|
337 | 248 | list.append('%s File %s"%s"%s, line %s%d%s\n' % \ |
|
338 | 249 | (Colors.normalEm, |
|
339 | 250 | Colors.filenameEm, filename, Colors.normalEm, |
|
340 | 251 | Colors.linenoEm, lineno, Colors.Normal )) |
|
341 | 252 | if line is not None: |
|
342 | 253 | i = 0 |
|
343 | 254 | while i < len(line) and line[i].isspace(): |
|
344 | 255 | i = i+1 |
|
345 | 256 | list.append('%s %s%s\n' % (Colors.line, |
|
346 | 257 | line.strip(), |
|
347 | 258 | Colors.Normal)) |
|
348 | 259 | if offset is not None: |
|
349 | 260 | s = ' ' |
|
350 | 261 | for c in line[i:offset-1]: |
|
351 | 262 | if c.isspace(): |
|
352 | 263 | s = s + c |
|
353 | 264 | else: |
|
354 | 265 | s = s + ' ' |
|
355 | 266 | list.append('%s%s^%s\n' % (Colors.caret, s, |
|
356 | 267 | Colors.Normal) ) |
|
357 | 268 | value = msg |
|
358 | 269 | s = self._some_str(value) |
|
359 | 270 | if s: |
|
360 | 271 | list.append('%s%s:%s %s\n' % (str(stype), Colors.excName, |
|
361 | 272 | Colors.Normal, s)) |
|
362 | 273 | else: |
|
363 | 274 | list.append('%s\n' % str(stype)) |
|
364 | 275 | return list |
|
365 | 276 | |
|
366 | 277 | def _some_str(self, value): |
|
367 | 278 | # Lifted from traceback.py |
|
368 | 279 | try: |
|
369 | 280 | return str(value) |
|
370 | 281 | except: |
|
371 | 282 | return '<unprintable %s object>' % type(value).__name__ |
|
372 | 283 | |
|
373 | 284 | #---------------------------------------------------------------------------- |
|
374 | 285 | class VerboseTB(TBTools): |
|
375 | 286 | """A port of Ka-Ping Yee's cgitb.py module that outputs color text instead |
|
376 | 287 | of HTML. Requires inspect and pydoc. Crazy, man. |
|
377 | 288 | |
|
378 | 289 | Modified version which optionally strips the topmost entries from the |
|
379 | 290 | traceback, to be used with alternate interpreters (because their own code |
|
380 | 291 | would appear in the traceback).""" |
|
381 | 292 | |
|
382 | 293 | def __init__(self,color_scheme = 'Linux',tb_offset=0,long_header=0, |
|
383 | 294 | call_pdb = 0, include_vars=1): |
|
384 | 295 | """Specify traceback offset, headers and color scheme. |
|
385 | 296 | |
|
386 | 297 | Define how many frames to drop from the tracebacks. Calling it with |
|
387 | 298 | tb_offset=1 allows use of this handler in interpreters which will have |
|
388 | 299 | their own code at the top of the traceback (VerboseTB will first |
|
389 | 300 | remove that frame before printing the traceback info).""" |
|
390 | 301 | TBTools.__init__(self,color_scheme=color_scheme,call_pdb=call_pdb) |
|
391 | 302 | self.tb_offset = tb_offset |
|
392 | 303 | self.long_header = long_header |
|
393 | 304 | self.include_vars = include_vars |
|
394 | 305 | |
|
395 | 306 | def text(self, etype, evalue, etb, context=5): |
|
396 | 307 | """Return a nice text document describing the traceback.""" |
|
397 | 308 | |
|
398 | 309 | # some locals |
|
399 | 310 | Colors = self.Colors # just a shorthand + quicker name lookup |
|
400 | 311 | ColorsNormal = Colors.Normal # used a lot |
|
401 | 312 | indent_size = 8 # we need some space to put line numbers before |
|
402 | 313 | indent = ' '*indent_size |
|
403 | 314 | numbers_width = indent_size - 1 # leave space between numbers & code |
|
404 | 315 | text_repr = pydoc.text.repr |
|
405 | 316 | exc = '%s%s%s' % (Colors.excName, str(etype), ColorsNormal) |
|
406 | 317 | em_normal = '%s\n%s%s' % (Colors.valEm, indent,ColorsNormal) |
|
407 | 318 | undefined = '%sundefined%s' % (Colors.em, ColorsNormal) |
|
408 | 319 | |
|
409 | 320 | # some internal-use functions |
|
410 | 321 | def eqrepr(value, repr=text_repr): return '=%s' % repr(value) |
|
411 | 322 | def nullrepr(value, repr=text_repr): return '' |
|
412 | 323 | |
|
413 | 324 | # meat of the code begins |
|
414 | 325 | if type(etype) is types.ClassType: |
|
415 | 326 | etype = etype.__name__ |
|
416 | 327 | |
|
417 | 328 | if self.long_header: |
|
418 | 329 | # Header with the exception type, python version, and date |
|
419 | 330 | pyver = 'Python ' + string.split(sys.version)[0] + ': ' + sys.executable |
|
420 | 331 | date = time.ctime(time.time()) |
|
421 | 332 | |
|
422 | 333 | head = '%s%s%s\n%s%s%s\n%s' % (Colors.topline, '-'*75, ColorsNormal, |
|
423 | 334 | exc, ' '*(75-len(str(etype))-len(pyver)), |
|
424 | 335 | pyver, string.rjust(date, 75) ) |
|
425 | 336 | head += "\nA problem occured executing Python code. Here is the sequence of function"\ |
|
426 | 337 | "\ncalls leading up to the error, with the most recent (innermost) call last." |
|
427 | 338 | else: |
|
428 | 339 | # Simplified header |
|
429 | 340 | head = '%s%s%s\n%s%s' % (Colors.topline, '-'*75, ColorsNormal,exc, |
|
430 | 341 | string.rjust('Traceback (most recent call last)', |
|
431 | 342 | 75 - len(str(etype)) ) ) |
|
432 | 343 | frames = [] |
|
433 | 344 | # Flush cache before calling inspect. This helps alleviate some of the |
|
434 | 345 | # problems with python 2.3's inspect.py. |
|
435 | 346 | linecache.checkcache() |
|
436 | 347 | # Drop topmost frames if requested |
|
437 | 348 | try: |
|
438 | 349 | records = inspect.getinnerframes(etb, context)[self.tb_offset:] |
|
439 | 350 | except: |
|
440 | 351 | |
|
441 | 352 | # FIXME: I've been getting many crash reports from python 2.3 |
|
442 | 353 | # users, traceable to inspect.py. If I can find a small test-case |
|
443 | 354 | # to reproduce this, I should either write a better workaround or |
|
444 | 355 | # file a bug report against inspect (if that's the real problem). |
|
445 | 356 | # So far, I haven't been able to find an isolated example to |
|
446 | 357 | # reproduce the problem. |
|
447 | 358 | inspect_error() |
|
448 | 359 | traceback.print_exc(file=Term.cerr) |
|
449 | 360 | info('\nUnfortunately, your original traceback can not be constructed.\n') |
|
450 | 361 | return '' |
|
451 | 362 | |
|
452 | 363 | # build some color string templates outside these nested loops |
|
453 | 364 | tpl_link = '%s%%s%s' % (Colors.filenameEm,ColorsNormal) |
|
454 | 365 | tpl_call = 'in %s%%s%s%%s%s' % (Colors.vName, Colors.valEm, |
|
455 | 366 | ColorsNormal) |
|
456 | 367 | tpl_call_fail = 'in %s%%s%s(***failed resolving arguments***)%s' % \ |
|
457 | 368 | (Colors.vName, Colors.valEm, ColorsNormal) |
|
458 | 369 | tpl_local_var = '%s%%s%s' % (Colors.vName, ColorsNormal) |
|
459 | 370 | tpl_global_var = '%sglobal%s %s%%s%s' % (Colors.em, ColorsNormal, |
|
460 | 371 | Colors.vName, ColorsNormal) |
|
461 | 372 | tpl_name_val = '%%s %s= %%s%s' % (Colors.valEm, ColorsNormal) |
|
462 | 373 | tpl_line = '%s%%s%s %%s' % (Colors.lineno, ColorsNormal) |
|
463 | 374 | tpl_line_em = '%s%%s%s %%s%s' % (Colors.linenoEm,Colors.line, |
|
464 | 375 | ColorsNormal) |
|
465 | 376 | |
|
466 | 377 | # now, loop over all records printing context and info |
|
467 | 378 | abspath = os.path.abspath |
|
468 | 379 | for frame, file, lnum, func, lines, index in records: |
|
469 | 380 | #print '*** record:',file,lnum,func,lines,index # dbg |
|
470 | 381 | try: |
|
471 | 382 | file = file and abspath(file) or '?' |
|
472 | 383 | except OSError: |
|
473 | 384 | # if file is '<console>' or something not in the filesystem, |
|
474 | 385 | # the abspath call will throw an OSError. Just ignore it and |
|
475 | 386 | # keep the original file string. |
|
476 | 387 | pass |
|
477 | 388 | link = tpl_link % file |
|
478 | 389 | try: |
|
479 | 390 | args, varargs, varkw, locals = inspect.getargvalues(frame) |
|
480 | 391 | except: |
|
481 | 392 | # This can happen due to a bug in python2.3. We should be |
|
482 | 393 | # able to remove this try/except when 2.4 becomes a |
|
483 | 394 | # requirement. Bug details at http://python.org/sf/1005466 |
|
484 | 395 | inspect_error() |
|
485 | 396 | traceback.print_exc(file=Term.cerr) |
|
486 | 397 | info("\nIPython's exception reporting continues...\n") |
|
487 | 398 | |
|
488 | 399 | if func == '?': |
|
489 | 400 | call = '' |
|
490 | 401 | else: |
|
491 | 402 | # Decide whether to include variable details or not |
|
492 | 403 | var_repr = self.include_vars and eqrepr or nullrepr |
|
493 | 404 | try: |
|
494 | 405 | call = tpl_call % (func,inspect.formatargvalues(args, |
|
495 | 406 | varargs, varkw, |
|
496 | 407 | locals,formatvalue=var_repr)) |
|
497 | 408 | except KeyError: |
|
498 | 409 | # Very odd crash from inspect.formatargvalues(). The |
|
499 | 410 | # scenario under which it appeared was a call to |
|
500 | 411 | # view(array,scale) in NumTut.view.view(), where scale had |
|
501 | 412 | # been defined as a scalar (it should be a tuple). Somehow |
|
502 | 413 | # inspect messes up resolving the argument list of view() |
|
503 | 414 | # and barfs out. At some point I should dig into this one |
|
504 | 415 | # and file a bug report about it. |
|
505 | 416 | inspect_error() |
|
506 | 417 | traceback.print_exc(file=Term.cerr) |
|
507 | 418 | info("\nIPython's exception reporting continues...\n") |
|
508 | 419 | call = tpl_call_fail % func |
|
509 | 420 | |
|
510 | 421 | # Initialize a list of names on the current line, which the |
|
511 | 422 | # tokenizer below will populate. |
|
512 | 423 | names = [] |
|
513 | 424 | |
|
514 | 425 | def tokeneater(token_type, token, start, end, line): |
|
515 | 426 | """Stateful tokeneater which builds dotted names. |
|
516 | 427 | |
|
517 | 428 | The list of names it appends to (from the enclosing scope) can |
|
518 | 429 | contain repeated composite names. This is unavoidable, since |
|
519 | 430 | there is no way to disambguate partial dotted structures until |
|
520 | 431 | the full list is known. The caller is responsible for pruning |
|
521 | 432 | the final list of duplicates before using it.""" |
|
522 | 433 | |
|
523 | 434 | # build composite names |
|
524 | 435 | if token == '.': |
|
525 | 436 | try: |
|
526 | 437 | names[-1] += '.' |
|
527 | 438 | # store state so the next token is added for x.y.z names |
|
528 | 439 | tokeneater.name_cont = True |
|
529 | 440 | return |
|
530 | 441 | except IndexError: |
|
531 | 442 | pass |
|
532 | 443 | if token_type == tokenize.NAME and token not in keyword.kwlist: |
|
533 | 444 | if tokeneater.name_cont: |
|
534 | 445 | # Dotted names |
|
535 | 446 | names[-1] += token |
|
536 | 447 | tokeneater.name_cont = False |
|
537 | 448 | else: |
|
538 | 449 | # Regular new names. We append everything, the caller |
|
539 | 450 | # will be responsible for pruning the list later. It's |
|
540 | 451 | # very tricky to try to prune as we go, b/c composite |
|
541 | 452 | # names can fool us. The pruning at the end is easy |
|
542 | 453 | # to do (or the caller can print a list with repeated |
|
543 | 454 | # names if so desired. |
|
544 | 455 | names.append(token) |
|
545 | 456 | elif token_type == tokenize.NEWLINE: |
|
546 | 457 | raise IndexError |
|
547 | 458 | # we need to store a bit of state in the tokenizer to build |
|
548 | 459 | # dotted names |
|
549 | 460 | tokeneater.name_cont = False |
|
550 | 461 | |
|
551 | 462 | def linereader(file=file, lnum=[lnum], getline=linecache.getline): |
|
552 | 463 | line = getline(file, lnum[0]) |
|
553 | 464 | lnum[0] += 1 |
|
554 | 465 | return line |
|
555 | 466 | |
|
556 | 467 | # Build the list of names on this line of code where the exception |
|
557 | 468 | # occurred. |
|
558 | 469 | try: |
|
559 | 470 | # This builds the names list in-place by capturing it from the |
|
560 | 471 | # enclosing scope. |
|
561 | 472 | tokenize.tokenize(linereader, tokeneater) |
|
562 | 473 | except IndexError: |
|
563 | 474 | # signals exit of tokenizer |
|
564 | 475 | pass |
|
565 | 476 | except tokenize.TokenError,msg: |
|
566 | 477 | _m = ("An unexpected error occurred while tokenizing input\n" |
|
567 | 478 | "The following traceback may be corrupted or invalid\n" |
|
568 | 479 | "The error message is: %s\n" % msg) |
|
569 | 480 | error(_m) |
|
570 | 481 | |
|
571 | 482 | # prune names list of duplicates, but keep the right order |
|
572 | 483 | unique_names = uniq_stable(names) |
|
573 | 484 | |
|
574 | 485 | # Start loop over vars |
|
575 | 486 | lvals = [] |
|
576 | 487 | if self.include_vars: |
|
577 | 488 | for name_full in unique_names: |
|
578 | 489 | name_base = name_full.split('.',1)[0] |
|
579 | 490 | if name_base in frame.f_code.co_varnames: |
|
580 | 491 | if locals.has_key(name_base): |
|
581 | 492 | try: |
|
582 | 493 | value = repr(eval(name_full,locals)) |
|
583 | 494 | except: |
|
584 | 495 | value = undefined |
|
585 | 496 | else: |
|
586 | 497 | value = undefined |
|
587 | 498 | name = tpl_local_var % name_full |
|
588 | 499 | else: |
|
589 | 500 | if frame.f_globals.has_key(name_base): |
|
590 | 501 | try: |
|
591 | 502 | value = repr(eval(name_full,frame.f_globals)) |
|
592 | 503 | except: |
|
593 | 504 | value = undefined |
|
594 | 505 | else: |
|
595 | 506 | value = undefined |
|
596 | 507 | name = tpl_global_var % name_full |
|
597 | 508 | lvals.append(tpl_name_val % (name,value)) |
|
598 | 509 | if lvals: |
|
599 | 510 | lvals = '%s%s' % (indent,em_normal.join(lvals)) |
|
600 | 511 | else: |
|
601 | 512 | lvals = '' |
|
602 | 513 | |
|
603 | 514 | level = '%s %s\n' % (link,call) |
|
604 | 515 | excerpt = [] |
|
605 | 516 | if index is not None: |
|
606 | 517 | i = lnum - index |
|
607 | 518 | for line in lines: |
|
608 | 519 | if i == lnum: |
|
609 | 520 | # This is the line with the error |
|
610 | 521 | pad = numbers_width - len(str(i)) |
|
611 | 522 | if pad >= 3: |
|
612 | 523 | marker = '-'*(pad-3) + '-> ' |
|
613 | 524 | elif pad == 2: |
|
614 | 525 | marker = '> ' |
|
615 | 526 | elif pad == 1: |
|
616 | 527 | marker = '>' |
|
617 | 528 | else: |
|
618 | 529 | marker = '' |
|
619 | 530 | num = '%s%s' % (marker,i) |
|
620 | 531 | line = tpl_line_em % (num,line) |
|
621 | 532 | else: |
|
622 | 533 | num = '%*s' % (numbers_width,i) |
|
623 | 534 | line = tpl_line % (num,line) |
|
624 | 535 | |
|
625 | 536 | excerpt.append(line) |
|
626 | 537 | if self.include_vars and i == lnum: |
|
627 | 538 | excerpt.append('%s\n' % lvals) |
|
628 | 539 | i += 1 |
|
629 | 540 | frames.append('%s%s' % (level,''.join(excerpt)) ) |
|
630 | 541 | |
|
631 | 542 | # Get (safely) a string form of the exception info |
|
632 | 543 | try: |
|
633 | 544 | etype_str,evalue_str = map(str,(etype,evalue)) |
|
634 | 545 | except: |
|
635 | 546 | # User exception is improperly defined. |
|
636 | 547 | etype,evalue = str,sys.exc_info()[:2] |
|
637 | 548 | etype_str,evalue_str = map(str,(etype,evalue)) |
|
638 | 549 | # ... and format it |
|
639 | 550 | exception = ['%s%s%s: %s' % (Colors.excName, etype_str, |
|
640 | 551 | ColorsNormal, evalue_str)] |
|
641 | 552 | if type(evalue) is types.InstanceType: |
|
642 | 553 | names = [w for w in dir(evalue) if isinstance(w, basestring)] |
|
643 | 554 | for name in names: |
|
644 | 555 | value = text_repr(getattr(evalue, name)) |
|
645 | 556 | exception.append('\n%s%s = %s' % (indent, name, value)) |
|
646 | 557 | # return all our info assembled as a single string |
|
647 | 558 | return '%s\n\n%s\n%s' % (head,'\n'.join(frames),''.join(exception[0]) ) |
|
648 | 559 | |
|
649 | 560 | def debugger(self): |
|
650 | 561 | """Call up the pdb debugger if desired, always clean up the tb reference. |
|
651 | 562 | |
|
652 | 563 | If the call_pdb flag is set, the pdb interactive debugger is |
|
653 | 564 | invoked. In all cases, the self.tb reference to the current traceback |
|
654 | 565 | is deleted to prevent lingering references which hamper memory |
|
655 | 566 | management. |
|
656 | 567 | |
|
657 | 568 | Note that each call to pdb() does an 'import readline', so if your app |
|
658 | 569 | requires a special setup for the readline completers, you'll have to |
|
659 | 570 | fix that by hand after invoking the exception handler.""" |
|
660 | 571 | |
|
661 | 572 | if self.call_pdb: |
|
662 | 573 | if self.pdb is None: |
|
663 |
self.pdb = Debugger.Pdb( |
|
|
664 | # the system displayhook may have changed, restore the original for pdb | |
|
574 | self.pdb = Debugger.Pdb( | |
|
575 | self.color_scheme_table.active_scheme_name) | |
|
576 | # the system displayhook may have changed, restore the original | |
|
577 | # for pdb | |
|
665 | 578 | dhook = sys.displayhook |
|
666 | 579 | sys.displayhook = sys.__displayhook__ |
|
667 | 580 | self.pdb.reset() |
|
581 | # Find the right frame so we don't pop up inside ipython itself | |
|
582 | etb = self.tb | |
|
668 | 583 | while self.tb.tb_next is not None: |
|
669 | 584 | self.tb = self.tb.tb_next |
|
670 | 585 | try: |
|
586 | if etb and etb.tb_next: | |
|
587 | etb = etb.tb_next | |
|
588 | self.pdb.botframe = etb.tb_frame | |
|
671 | 589 | self.pdb.interaction(self.tb.tb_frame, self.tb) |
|
672 | 590 | except: |
|
673 | 591 | print '*** ERROR ***' |
|
674 | 592 | print 'This version of pdb has a bug and crashed.' |
|
675 | 593 | print 'Returning to IPython...' |
|
676 | 594 | sys.displayhook = dhook |
|
677 | 595 | del self.tb |
|
678 | 596 | |
|
679 | 597 | def handler(self, info=None): |
|
680 | 598 | (etype, evalue, etb) = info or sys.exc_info() |
|
681 | 599 | self.tb = etb |
|
682 | 600 | print >> Term.cerr, self.text(etype, evalue, etb) |
|
683 | 601 | |
|
684 | 602 | # Changed so an instance can just be called as VerboseTB_inst() and print |
|
685 | 603 | # out the right info on its own. |
|
686 | 604 | def __call__(self, etype=None, evalue=None, etb=None): |
|
687 | 605 | """This hook can replace sys.excepthook (for Python 2.1 or higher).""" |
|
688 | 606 | if etb is not None: |
|
689 | 607 | self.handler((etype, evalue, etb)) |
|
690 | 608 | else: |
|
691 | 609 | self.handler() |
|
692 | 610 | self.debugger() |
|
693 | 611 | |
|
694 | 612 | #---------------------------------------------------------------------------- |
|
695 | 613 | class FormattedTB(VerboseTB,ListTB): |
|
696 | 614 | """Subclass ListTB but allow calling with a traceback. |
|
697 | 615 | |
|
698 | 616 | It can thus be used as a sys.excepthook for Python > 2.1. |
|
699 | 617 | |
|
700 | 618 | Also adds 'Context' and 'Verbose' modes, not available in ListTB. |
|
701 | 619 | |
|
702 | 620 | Allows a tb_offset to be specified. This is useful for situations where |
|
703 | 621 | one needs to remove a number of topmost frames from the traceback (such as |
|
704 | 622 | occurs with python programs that themselves execute other python code, |
|
705 | 623 | like Python shells). """ |
|
706 | 624 | |
|
707 | 625 | def __init__(self, mode = 'Plain', color_scheme='Linux', |
|
708 | 626 | tb_offset = 0,long_header=0,call_pdb=0,include_vars=0): |
|
709 | 627 | |
|
710 | 628 | # NEVER change the order of this list. Put new modes at the end: |
|
711 | 629 | self.valid_modes = ['Plain','Context','Verbose'] |
|
712 | 630 | self.verbose_modes = self.valid_modes[1:3] |
|
713 | 631 | |
|
714 | 632 | VerboseTB.__init__(self,color_scheme,tb_offset,long_header, |
|
715 | 633 | call_pdb=call_pdb,include_vars=include_vars) |
|
716 | 634 | self.set_mode(mode) |
|
717 | 635 | |
|
718 | 636 | def _extract_tb(self,tb): |
|
719 | 637 | if tb: |
|
720 | 638 | return traceback.extract_tb(tb) |
|
721 | 639 | else: |
|
722 | 640 | return None |
|
723 | 641 | |
|
724 | 642 | def text(self, etype, value, tb,context=5,mode=None): |
|
725 | 643 | """Return formatted traceback. |
|
726 | 644 | |
|
727 | 645 | If the optional mode parameter is given, it overrides the current |
|
728 | 646 | mode.""" |
|
729 | 647 | |
|
730 | 648 | if mode is None: |
|
731 | 649 | mode = self.mode |
|
732 | 650 | if mode in self.verbose_modes: |
|
733 | 651 | # verbose modes need a full traceback |
|
734 | 652 | return VerboseTB.text(self,etype, value, tb,context=5) |
|
735 | 653 | else: |
|
736 | 654 | # We must check the source cache because otherwise we can print |
|
737 | 655 | # out-of-date source code. |
|
738 | 656 | linecache.checkcache() |
|
739 | 657 | # Now we can extract and format the exception |
|
740 | 658 | elist = self._extract_tb(tb) |
|
741 | 659 | if len(elist) > self.tb_offset: |
|
742 | 660 | del elist[:self.tb_offset] |
|
743 | 661 | return ListTB.text(self,etype,value,elist) |
|
744 | 662 | |
|
745 | 663 | def set_mode(self,mode=None): |
|
746 | 664 | """Switch to the desired mode. |
|
747 | 665 | |
|
748 | 666 | If mode is not specified, cycles through the available modes.""" |
|
749 | 667 | |
|
750 | 668 | if not mode: |
|
751 | 669 | new_idx = ( self.valid_modes.index(self.mode) + 1 ) % \ |
|
752 | 670 | len(self.valid_modes) |
|
753 | 671 | self.mode = self.valid_modes[new_idx] |
|
754 | 672 | elif mode not in self.valid_modes: |
|
755 | 673 | raise ValueError, 'Unrecognized mode in FormattedTB: <'+mode+'>\n'\ |
|
756 | 674 | 'Valid modes: '+str(self.valid_modes) |
|
757 | 675 | else: |
|
758 | 676 | self.mode = mode |
|
759 | 677 | # include variable details only in 'Verbose' mode |
|
760 | 678 | self.include_vars = (self.mode == self.valid_modes[2]) |
|
761 | 679 | |
|
762 | 680 | # some convenient shorcuts |
|
763 | 681 | def plain(self): |
|
764 | 682 | self.set_mode(self.valid_modes[0]) |
|
765 | 683 | |
|
766 | 684 | def context(self): |
|
767 | 685 | self.set_mode(self.valid_modes[1]) |
|
768 | 686 | |
|
769 | 687 | def verbose(self): |
|
770 | 688 | self.set_mode(self.valid_modes[2]) |
|
771 | 689 | |
|
772 | 690 | #---------------------------------------------------------------------------- |
|
773 | 691 | class AutoFormattedTB(FormattedTB): |
|
774 | 692 | """A traceback printer which can be called on the fly. |
|
775 | 693 | |
|
776 | 694 | It will find out about exceptions by itself. |
|
777 | 695 | |
|
778 | 696 | A brief example: |
|
779 | 697 | |
|
780 | 698 | AutoTB = AutoFormattedTB(mode = 'Verbose',color_scheme='Linux') |
|
781 | 699 | try: |
|
782 | 700 | ... |
|
783 | 701 | except: |
|
784 | 702 | AutoTB() # or AutoTB(out=logfile) where logfile is an open file object |
|
785 | 703 | """ |
|
786 | 704 | def __call__(self,etype=None,evalue=None,etb=None, |
|
787 | 705 | out=None,tb_offset=None): |
|
788 | 706 | """Print out a formatted exception traceback. |
|
789 | 707 | |
|
790 | 708 | Optional arguments: |
|
791 | 709 | - out: an open file-like object to direct output to. |
|
792 | 710 | |
|
793 | 711 | - tb_offset: the number of frames to skip over in the stack, on a |
|
794 | 712 | per-call basis (this overrides temporarily the instance's tb_offset |
|
795 | 713 | given at initialization time. """ |
|
796 | 714 | |
|
797 | 715 | if out is None: |
|
798 | 716 | out = Term.cerr |
|
799 | 717 | if tb_offset is not None: |
|
800 | 718 | tb_offset, self.tb_offset = self.tb_offset, tb_offset |
|
801 | 719 | print >> out, self.text(etype, evalue, etb) |
|
802 | 720 | self.tb_offset = tb_offset |
|
803 | 721 | else: |
|
804 | print >> out, self.text() | |
|
722 | print >> out, self.text(etype, evalue, etb) | |
|
805 | 723 | self.debugger() |
|
806 | 724 | |
|
807 | 725 | def text(self,etype=None,value=None,tb=None,context=5,mode=None): |
|
808 | 726 | if etype is None: |
|
809 | 727 | etype,value,tb = sys.exc_info() |
|
810 | 728 | self.tb = tb |
|
811 | 729 | return FormattedTB.text(self,etype,value,tb,context=5,mode=mode) |
|
812 | 730 | |
|
813 | 731 | #--------------------------------------------------------------------------- |
|
814 | 732 | # A simple class to preserve Nathan's original functionality. |
|
815 | 733 | class ColorTB(FormattedTB): |
|
816 | 734 | """Shorthand to initialize a FormattedTB in Linux colors mode.""" |
|
817 | 735 | def __init__(self,color_scheme='Linux',call_pdb=0): |
|
818 | 736 | FormattedTB.__init__(self,color_scheme=color_scheme, |
|
819 | 737 | call_pdb=call_pdb) |
|
820 | 738 | |
|
821 | 739 | #---------------------------------------------------------------------------- |
|
822 | 740 | # module testing (minimal) |
|
823 | 741 | if __name__ == "__main__": |
|
824 | 742 | def spam(c, (d, e)): |
|
825 | 743 | x = c + d |
|
826 | 744 | y = c * d |
|
827 | 745 | foo(x, y) |
|
828 | 746 | |
|
829 | 747 | def foo(a, b, bar=1): |
|
830 | 748 | eggs(a, b + bar) |
|
831 | 749 | |
|
832 | 750 | def eggs(f, g, z=globals()): |
|
833 | 751 | h = f + g |
|
834 | 752 | i = f - g |
|
835 | 753 | return h / i |
|
836 | 754 | |
|
837 | 755 | print '' |
|
838 | 756 | print '*** Before ***' |
|
839 | 757 | try: |
|
840 | 758 | print spam(1, (2, 3)) |
|
841 | 759 | except: |
|
842 | 760 | traceback.print_exc() |
|
843 | 761 | print '' |
|
844 | 762 | |
|
845 | 763 | handler = ColorTB() |
|
846 | 764 | print '*** ColorTB ***' |
|
847 | 765 | try: |
|
848 | 766 | print spam(1, (2, 3)) |
|
849 | 767 | except: |
|
850 | 768 | apply(handler, sys.exc_info() ) |
|
851 | 769 | print '' |
|
852 | 770 | |
|
853 | 771 | handler = VerboseTB() |
|
854 | 772 | print '*** VerboseTB ***' |
|
855 | 773 | try: |
|
856 | 774 | print spam(1, (2, 3)) |
|
857 | 775 | except: |
|
858 | 776 | apply(handler, sys.exc_info() ) |
|
859 | 777 | print '' |
|
860 | 778 |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
@@ -1,149 +1,150 b'' | |||
|
1 | 1 | #!/usr/bin/env python |
|
2 | 2 | # -*- coding: utf-8 -*- |
|
3 | 3 | """Setup script for IPython. |
|
4 | 4 | |
|
5 | 5 | Under Posix environments it works like a typical setup.py script. |
|
6 | 6 | Under Windows, the command sdist is not supported, since IPython |
|
7 | 7 | requires utilities, which are not available under Windows.""" |
|
8 | 8 | |
|
9 | 9 | #***************************************************************************** |
|
10 | 10 | # Copyright (C) 2001-2005 Fernando Perez <fperez@colorado.edu> |
|
11 | 11 | # |
|
12 | 12 | # Distributed under the terms of the BSD License. The full license is in |
|
13 | 13 | # the file COPYING, distributed as part of this software. |
|
14 | 14 | #***************************************************************************** |
|
15 | 15 | |
|
16 | 16 | import sys, os |
|
17 | 17 | from glob import glob |
|
18 | 18 | from setupext import install_data_ext |
|
19 | 19 | isfile = os.path.isfile |
|
20 | 20 | |
|
21 | 21 | # BEFORE importing distutils, remove MANIFEST. distutils doesn't properly |
|
22 | 22 | # update it when the contents of directories change. |
|
23 | 23 | if os.path.exists('MANIFEST'): os.remove('MANIFEST') |
|
24 | 24 | |
|
25 | 25 | if os.name == 'posix': |
|
26 | 26 | os_name = 'posix' |
|
27 | 27 | elif os.name in ['nt','dos']: |
|
28 | 28 | os_name = 'windows' |
|
29 | 29 | else: |
|
30 | 30 | print 'Unsupported operating system:',os.name |
|
31 | 31 | sys.exit(1) |
|
32 | 32 | |
|
33 | 33 | # Under Windows, 'sdist' is not supported, since it requires lyxport (and |
|
34 | 34 | # hence lyx,perl,latex,pdflatex,latex2html,sh,...) |
|
35 | 35 | if os_name == 'windows' and sys.argv[1] == 'sdist': |
|
36 | 36 | print 'The sdist command is not available under Windows. Exiting.' |
|
37 | 37 | sys.exit(1) |
|
38 | 38 | |
|
39 | 39 | from distutils.core import setup |
|
40 | 40 | |
|
41 | 41 | # update the manuals when building a source dist |
|
42 | 42 | if len(sys.argv) >= 2 and sys.argv[1] in ('sdist','bdist_rpm'): |
|
43 | 43 | from IPython.genutils import target_update |
|
44 | 44 | # list of things to be updated. Each entry is a triplet of args for |
|
45 | 45 | # target_update() |
|
46 | 46 | to_update = [('doc/magic.tex', |
|
47 | 47 | ['IPython/Magic.py'], |
|
48 | 48 | "cd doc && ./update_magic.sh" ), |
|
49 | 49 | |
|
50 | 50 | ('doc/manual.lyx', |
|
51 | 51 | ['IPython/Release.py','doc/manual_base.lyx'], |
|
52 | 52 | "cd doc && ./update_version.sh" ), |
|
53 | 53 | |
|
54 | 54 | ('doc/manual/manual.html', |
|
55 | 55 | ['doc/manual.lyx', |
|
56 | 56 | 'doc/magic.tex', |
|
57 | 57 | 'doc/examples/example-gnuplot.py', |
|
58 | 58 | 'doc/examples/example-magic.py', |
|
59 | 59 | 'doc/examples/example-embed.py', |
|
60 | 60 | 'doc/examples/example-embed-short.py', |
|
61 | 61 | 'IPython/UserConfig/ipythonrc', |
|
62 | 62 | ], |
|
63 | 63 | "cd doc && " |
|
64 | 64 | "lyxport -tt --leave --pdf " |
|
65 | 65 | "--html -o '-noinfo -split +1 -local_icons' manual.lyx"), |
|
66 | 66 | |
|
67 | 67 | ('doc/new_design.pdf', |
|
68 | 68 | ['doc/new_design.lyx'], |
|
69 | 69 | "cd doc && lyxport -tt --pdf new_design.lyx"), |
|
70 | 70 | |
|
71 | 71 | ('doc/ipython.1.gz', |
|
72 | 72 | ['doc/ipython.1'], |
|
73 | 73 | "cd doc && gzip -9c ipython.1 > ipython.1.gz"), |
|
74 | 74 | |
|
75 | 75 | ('doc/pycolor.1.gz', |
|
76 | 76 | ['doc/pycolor.1'], |
|
77 | 77 | "cd doc && gzip -9c pycolor.1 > pycolor.1.gz"), |
|
78 | 78 | ] |
|
79 | 79 | for target in to_update: |
|
80 | 80 | target_update(*target) |
|
81 | 81 | |
|
82 | 82 | # Release.py contains version, authors, license, url, keywords, etc. |
|
83 | 83 | execfile(os.path.join('IPython','Release.py')) |
|
84 | 84 | |
|
85 | 85 | # A little utility we'll need below, since glob() does NOT allow you to do |
|
86 | 86 | # exclusion on multiple endings! |
|
87 | 87 | def file_doesnt_endwith(test,endings): |
|
88 | 88 | """Return true if test is a file and its name does NOT end with any |
|
89 | 89 | of the strings listed in endings.""" |
|
90 | 90 | if not isfile(test): |
|
91 | 91 | return False |
|
92 | 92 | for e in endings: |
|
93 | 93 | if test.endswith(e): |
|
94 | 94 | return False |
|
95 | 95 | return True |
|
96 | 96 | |
|
97 | 97 | # I can't find how to make distutils create a nested dir. structure, so |
|
98 | 98 | # in the meantime do it manually. Butt ugly. |
|
99 | 99 | # Note that http://www.redbrick.dcu.ie/~noel/distutils.html, ex. 2/3, contain |
|
100 | 100 | # information on how to do this more cleanly once python 2.4 can be assumed. |
|
101 | 101 | # Thanks to Noel for the tip. |
|
102 | 102 | docdirbase = 'share/doc/ipython-%s' % version |
|
103 | 103 | manpagebase = 'share/man/man1' |
|
104 | 104 | |
|
105 | 105 | # We only need to exclude from this things NOT already excluded in the |
|
106 | 106 | # MANIFEST.in file. |
|
107 | 107 | exclude = ('.sh','.1.gz') |
|
108 | 108 | docfiles = filter(lambda f:file_doesnt_endwith(f,exclude),glob('doc/*')) |
|
109 | 109 | |
|
110 | 110 | examfiles = filter(isfile, glob('doc/examples/*.py')) |
|
111 | 111 | manfiles = filter(isfile, glob('doc/manual/*.html')) + \ |
|
112 | 112 | filter(isfile, glob('doc/manual/*.css')) + \ |
|
113 | 113 | filter(isfile, glob('doc/manual/*.png')) |
|
114 | 114 | manpages = filter(isfile, glob('doc/*.1.gz')) |
|
115 | 115 | cfgfiles = filter(isfile, glob('IPython/UserConfig/*')) |
|
116 | 116 | scriptfiles = filter(isfile, ['scripts/ipython','scripts/pycolor']) |
|
117 | 117 | |
|
118 | 118 | # Script to be run by the windows binary installer after the default setup |
|
119 | 119 | # routine, to add shortcuts and similar windows-only things. Windows |
|
120 | 120 | # post-install scripts MUST reside in the scripts/ dir, otherwise distutils |
|
121 | 121 | # doesn't find them. |
|
122 | 122 | if 'bdist_wininst' in sys.argv: |
|
123 | 123 | if len(sys.argv) > 2 and ('sdist' in sys.argv or 'bdist_rpm' in sys.argv): |
|
124 | 124 | print >> sys.stderr,"ERROR: bdist_wininst must be run alone. Exiting." |
|
125 | 125 | sys.exit(1) |
|
126 | 126 | scriptfiles.append('scripts/ipython_win_post_install.py') |
|
127 | 127 | |
|
128 | 128 | # Call the setup() routine which does most of the work |
|
129 | 129 | setup(name = name, |
|
130 | 130 | version = version, |
|
131 | 131 | description = description, |
|
132 | 132 | long_description = long_description, |
|
133 | 133 | author = authors['Fernando'][0], |
|
134 | 134 | author_email = authors['Fernando'][1], |
|
135 | 135 | url = url, |
|
136 | download_url = download_url, | |
|
136 | 137 | license = license, |
|
137 | 138 | platforms = platforms, |
|
138 | 139 | keywords = keywords, |
|
139 | 140 | packages = ['IPython', 'IPython.Extensions'], |
|
140 | 141 | scripts = scriptfiles, |
|
141 | 142 | cmdclass = {'install_data': install_data_ext}, |
|
142 | 143 | data_files = [('data', docdirbase, docfiles), |
|
143 | 144 | ('data', os.path.join(docdirbase, 'examples'), |
|
144 | 145 | examfiles), |
|
145 | 146 | ('data', os.path.join(docdirbase, 'manual'), |
|
146 | 147 | manfiles), |
|
147 | 148 | ('data', manpagebase, manpages), |
|
148 | 149 | ('lib', 'IPython/UserConfig', cfgfiles)] |
|
149 | 150 | ) |
General Comments 0
You need to be logged in to leave comments.
Login now