Show More
@@ -0,0 +1,62 b'' | |||||
|
1 | #!/usr/bin/env python | |||
|
2 | # encoding: utf-8 | |||
|
3 | """ | |||
|
4 | A context manager for handling sys.displayhook. | |||
|
5 | ||||
|
6 | Authors: | |||
|
7 | ||||
|
8 | * Robert Kern | |||
|
9 | * Brian Granger | |||
|
10 | """ | |||
|
11 | ||||
|
12 | #----------------------------------------------------------------------------- | |||
|
13 | # Copyright (C) 2008-2009 The IPython Development Team | |||
|
14 | # | |||
|
15 | # Distributed under the terms of the BSD License. The full license is in | |||
|
16 | # the file COPYING, distributed as part of this software. | |||
|
17 | #----------------------------------------------------------------------------- | |||
|
18 | ||||
|
19 | #----------------------------------------------------------------------------- | |||
|
20 | # Imports | |||
|
21 | #----------------------------------------------------------------------------- | |||
|
22 | ||||
|
23 | import sys | |||
|
24 | ||||
|
25 | from IPython.core.component import Component | |||
|
26 | ||||
|
27 | #----------------------------------------------------------------------------- | |||
|
28 | # Classes and functions | |||
|
29 | #----------------------------------------------------------------------------- | |||
|
30 | ||||
|
31 | ||||
|
32 | class DisplayTrap(Component): | |||
|
33 | """Object to manage sys.displayhook. | |||
|
34 | ||||
|
35 | This came from IPython.core.kernel.display_hook, but is simplified | |||
|
36 | (no callbacks or formatters) until more of the core is refactored. | |||
|
37 | """ | |||
|
38 | ||||
|
39 | def __init__(self, parent, hook): | |||
|
40 | super(DisplayTrap, self).__init__(parent, None, None) | |||
|
41 | ||||
|
42 | self.hook = hook | |||
|
43 | self.old_hook = None | |||
|
44 | ||||
|
45 | def __enter__(self): | |||
|
46 | self.set() | |||
|
47 | return self | |||
|
48 | ||||
|
49 | def __exit__(self, type, value, traceback): | |||
|
50 | self.unset() | |||
|
51 | return True | |||
|
52 | ||||
|
53 | def set(self): | |||
|
54 | """Set the hook.""" | |||
|
55 | if sys.displayhook is not self.hook: | |||
|
56 | self.old_hook = sys.displayhook | |||
|
57 | sys.displayhook = self.hook | |||
|
58 | ||||
|
59 | def unset(self): | |||
|
60 | """Unset the hook.""" | |||
|
61 | sys.displayhook = self.old_hook | |||
|
62 |
@@ -1,107 +1,107 b'' | |||||
1 | #!/usr/bin/env python |
|
1 | #!/usr/bin/env python | |
2 | # encoding: utf-8 |
|
2 | # encoding: utf-8 | |
3 | """ |
|
3 | """ | |
4 | A context manager for managing things injected into :mod:`__builtin__`. |
|
4 | A context manager for managing things injected into :mod:`__builtin__`. | |
5 |
|
5 | |||
6 | Authors: |
|
6 | Authors: | |
7 |
|
7 | |||
8 | * Brian Granger |
|
8 | * Brian Granger | |
9 | """ |
|
9 | """ | |
10 |
|
10 | |||
11 | #----------------------------------------------------------------------------- |
|
11 | #----------------------------------------------------------------------------- | |
12 | # Copyright (C) 2008-2009 The IPython Development Team |
|
12 | # Copyright (C) 2008-2009 The IPython Development Team | |
13 | # |
|
13 | # | |
14 | # Distributed under the terms of the BSD License. The full license is in |
|
14 | # Distributed under the terms of the BSD License. The full license is in | |
15 | # the file COPYING, distributed as part of this software. |
|
15 | # the file COPYING, distributed as part of this software. | |
16 | #----------------------------------------------------------------------------- |
|
16 | #----------------------------------------------------------------------------- | |
17 |
|
17 | |||
18 | #----------------------------------------------------------------------------- |
|
18 | #----------------------------------------------------------------------------- | |
19 | # Imports |
|
19 | # Imports | |
20 | #----------------------------------------------------------------------------- |
|
20 | #----------------------------------------------------------------------------- | |
21 |
|
21 | |||
22 | import __builtin__ |
|
22 | import __builtin__ | |
23 |
|
23 | |||
24 | from IPython.core.component import Component |
|
24 | from IPython.core.component import Component | |
25 | from IPython.core.quitter import Quitter |
|
25 | from IPython.core.quitter import Quitter | |
26 |
|
26 | |||
27 | from IPython.utils.traitlets import Instance |
|
27 | from IPython.utils.traitlets import Instance | |
28 |
|
28 | |||
29 | #----------------------------------------------------------------------------- |
|
29 | #----------------------------------------------------------------------------- | |
30 | # Classes and functions |
|
30 | # Classes and functions | |
31 | #----------------------------------------------------------------------------- |
|
31 | #----------------------------------------------------------------------------- | |
32 |
|
32 | |||
33 |
|
33 | |||
34 | class BuiltinUndefined(object): pass |
|
34 | class BuiltinUndefined(object): pass | |
35 | BuiltinUndefined = BuiltinUndefined() |
|
35 | BuiltinUndefined = BuiltinUndefined() | |
36 |
|
36 | |||
37 |
|
37 | |||
38 | class BuiltinTrap(Component): |
|
38 | class BuiltinTrap(Component): | |
39 | shell = Instance('IPython.core.iplib.InteractiveShell') |
|
39 | shell = Instance('IPython.core.iplib.InteractiveShell') | |
40 |
|
40 | |||
41 |
def __init__(self, parent |
|
41 | def __init__(self, parent): | |
42 |
super(BuiltinTrap, self).__init__(parent, n |
|
42 | super(BuiltinTrap, self).__init__(parent, None, None) | |
43 | # Don't just grab parent!!! |
|
43 | # Don't just grab parent!!! | |
44 | self.shell = Component.get_instances(root=self.root, |
|
44 | self.shell = Component.get_instances(root=self.root, | |
45 | klass='IPython.core.iplib.InteractiveShell')[0] |
|
45 | klass='IPython.core.iplib.InteractiveShell')[0] | |
46 | self._orig_builtins = {} |
|
46 | self._orig_builtins = {} | |
47 |
|
47 | |||
48 | def __enter__(self): |
|
48 | def __enter__(self): | |
49 | self.set() |
|
49 | self.set() | |
50 | # I return self, so callers can use add_builtin in a with clause. |
|
50 | # I return self, so callers can use add_builtin in a with clause. | |
51 | return self |
|
51 | return self | |
52 |
|
52 | |||
53 | def __exit__(self, type, value, traceback): |
|
53 | def __exit__(self, type, value, traceback): | |
54 | self.unset() |
|
54 | self.unset() | |
55 | return True |
|
55 | return True | |
56 |
|
56 | |||
57 | def add_builtin(self, key, value): |
|
57 | def add_builtin(self, key, value): | |
58 | """Add a builtin and save the original.""" |
|
58 | """Add a builtin and save the original.""" | |
59 | orig = __builtin__.__dict__.get(key, BuiltinUndefined) |
|
59 | orig = __builtin__.__dict__.get(key, BuiltinUndefined) | |
60 | self._orig_builtins[key] = orig |
|
60 | self._orig_builtins[key] = orig | |
61 | __builtin__.__dict__[key] = value |
|
61 | __builtin__.__dict__[key] = value | |
62 |
|
62 | |||
63 | def remove_builtin(self, key): |
|
63 | def remove_builtin(self, key): | |
64 | """Remove an added builtin and re-set the original.""" |
|
64 | """Remove an added builtin and re-set the original.""" | |
65 | try: |
|
65 | try: | |
66 | orig = self._orig_builtins.pop(key) |
|
66 | orig = self._orig_builtins.pop(key) | |
67 | except KeyError: |
|
67 | except KeyError: | |
68 | pass |
|
68 | pass | |
69 | else: |
|
69 | else: | |
70 | if orig is BuiltinUndefined: |
|
70 | if orig is BuiltinUndefined: | |
71 | del __builtin__.__dict__[key] |
|
71 | del __builtin__.__dict__[key] | |
72 | else: |
|
72 | else: | |
73 | __builtin__.__dict__[key] = orig |
|
73 | __builtin__.__dict__[key] = orig | |
74 |
|
74 | |||
75 | def set(self): |
|
75 | def set(self): | |
76 | """Store ipython references in the __builtin__ namespace.""" |
|
76 | """Store ipython references in the __builtin__ namespace.""" | |
77 | self.add_builtin('exit', Quitter(self.shell, 'exit')) |
|
77 | self.add_builtin('exit', Quitter(self.shell, 'exit')) | |
78 | self.add_builtin('quit', Quitter(self.shell, 'quit')) |
|
78 | self.add_builtin('quit', Quitter(self.shell, 'quit')) | |
79 |
|
79 | |||
80 | # Recursive reload function |
|
80 | # Recursive reload function | |
81 | try: |
|
81 | try: | |
82 | from IPython.lib import deepreload |
|
82 | from IPython.lib import deepreload | |
83 | if self.shell.deep_reload: |
|
83 | if self.shell.deep_reload: | |
84 | self.add_builtin('reload', deepreload.reload) |
|
84 | self.add_builtin('reload', deepreload.reload) | |
85 | else: |
|
85 | else: | |
86 | self.add_builtin('dreload', deepreload.reload) |
|
86 | self.add_builtin('dreload', deepreload.reload) | |
87 | del deepreload |
|
87 | del deepreload | |
88 | except ImportError: |
|
88 | except ImportError: | |
89 | pass |
|
89 | pass | |
90 |
|
90 | |||
91 | # Keep in the builtins a flag for when IPython is active. We set it |
|
91 | # Keep in the builtins a flag for when IPython is active. We set it | |
92 | # with setdefault so that multiple nested IPythons don't clobber one |
|
92 | # with setdefault so that multiple nested IPythons don't clobber one | |
93 | # another. Each will increase its value by one upon being activated, |
|
93 | # another. Each will increase its value by one upon being activated, | |
94 | # which also gives us a way to determine the nesting level. |
|
94 | # which also gives us a way to determine the nesting level. | |
95 | __builtin__.__dict__.setdefault('__IPYTHON__active',0) |
|
95 | __builtin__.__dict__.setdefault('__IPYTHON__active',0) | |
96 |
|
96 | |||
97 | def unset(self): |
|
97 | def unset(self): | |
98 | """Remove any builtins which might have been added by add_builtins, or |
|
98 | """Remove any builtins which might have been added by add_builtins, or | |
99 | restore overwritten ones to their previous values.""" |
|
99 | restore overwritten ones to their previous values.""" | |
100 | for key in self._orig_builtins.keys(): |
|
100 | for key in self._orig_builtins.keys(): | |
101 | self.remove_builtin(key) |
|
101 | self.remove_builtin(key) | |
102 | self._orig_builtins.clear() |
|
102 | self._orig_builtins.clear() | |
103 | self._builtins_added = False |
|
103 | self._builtins_added = False | |
104 | try: |
|
104 | try: | |
105 | del __builtin__.__dict__['__IPYTHON__active'] |
|
105 | del __builtin__.__dict__['__IPYTHON__active'] | |
106 | except KeyError: |
|
106 | except KeyError: | |
107 | pass No newline at end of file |
|
107 | pass |
@@ -1,282 +1,260 b'' | |||||
1 | #!/usr/bin/env python |
|
1 | #!/usr/bin/env python | |
2 | # encoding: utf-8 |
|
2 | # encoding: utf-8 | |
3 | """ |
|
3 | """ | |
4 | An embedded IPython shell. |
|
4 | An embedded IPython shell. | |
5 |
|
5 | |||
6 | Authors: |
|
6 | Authors: | |
7 |
|
7 | |||
8 | * Brian Granger |
|
8 | * Brian Granger | |
9 | * Fernando Perez |
|
9 | * Fernando Perez | |
10 |
|
10 | |||
11 | Notes |
|
11 | Notes | |
12 | ----- |
|
12 | ----- | |
13 | """ |
|
13 | """ | |
14 |
|
14 | |||
15 | #----------------------------------------------------------------------------- |
|
15 | #----------------------------------------------------------------------------- | |
16 | # Copyright (C) 2008-2009 The IPython Development Team |
|
16 | # Copyright (C) 2008-2009 The IPython Development Team | |
17 | # |
|
17 | # | |
18 | # Distributed under the terms of the BSD License. The full license is in |
|
18 | # Distributed under the terms of the BSD License. The full license is in | |
19 | # the file COPYING, distributed as part of this software. |
|
19 | # the file COPYING, distributed as part of this software. | |
20 | #----------------------------------------------------------------------------- |
|
20 | #----------------------------------------------------------------------------- | |
21 |
|
21 | |||
22 | #----------------------------------------------------------------------------- |
|
22 | #----------------------------------------------------------------------------- | |
23 | # Imports |
|
23 | # Imports | |
24 | #----------------------------------------------------------------------------- |
|
24 | #----------------------------------------------------------------------------- | |
25 |
|
25 | |||
26 | from __future__ import with_statement |
|
26 | from __future__ import with_statement | |
27 |
|
27 | |||
28 | import sys |
|
28 | import sys | |
|
29 | from contextlib import nested | |||
29 |
|
30 | |||
30 | from IPython.core import ultratb |
|
31 | from IPython.core import ultratb | |
31 | from IPython.core.iplib import InteractiveShell |
|
32 | from IPython.core.iplib import InteractiveShell | |
32 |
|
33 | |||
33 | from IPython.utils.traitlets import Bool, Str, CBool |
|
34 | from IPython.utils.traitlets import Bool, Str, CBool | |
34 | from IPython.utils.genutils import ask_yes_no |
|
35 | from IPython.utils.genutils import ask_yes_no | |
35 |
|
36 | |||
36 | #----------------------------------------------------------------------------- |
|
37 | #----------------------------------------------------------------------------- | |
37 | # Classes and functions |
|
38 | # Classes and functions | |
38 | #----------------------------------------------------------------------------- |
|
39 | #----------------------------------------------------------------------------- | |
39 |
|
40 | |||
40 | # This is an additional magic that is exposed in embedded shells. |
|
41 | # This is an additional magic that is exposed in embedded shells. | |
41 | def kill_embedded(self,parameter_s=''): |
|
42 | def kill_embedded(self,parameter_s=''): | |
42 | """%kill_embedded : deactivate for good the current embedded IPython. |
|
43 | """%kill_embedded : deactivate for good the current embedded IPython. | |
43 |
|
44 | |||
44 | This function (after asking for confirmation) sets an internal flag so that |
|
45 | This function (after asking for confirmation) sets an internal flag so that | |
45 | an embedded IPython will never activate again. This is useful to |
|
46 | an embedded IPython will never activate again. This is useful to | |
46 | permanently disable a shell that is being called inside a loop: once you've |
|
47 | permanently disable a shell that is being called inside a loop: once you've | |
47 | figured out what you needed from it, you may then kill it and the program |
|
48 | figured out what you needed from it, you may then kill it and the program | |
48 | will then continue to run without the interactive shell interfering again. |
|
49 | will then continue to run without the interactive shell interfering again. | |
49 | """ |
|
50 | """ | |
50 |
|
51 | |||
51 | kill = ask_yes_no("Are you sure you want to kill this embedded instance " |
|
52 | kill = ask_yes_no("Are you sure you want to kill this embedded instance " | |
52 | "(y/n)? [y/N] ",'n') |
|
53 | "(y/n)? [y/N] ",'n') | |
53 | if kill: |
|
54 | if kill: | |
54 | self.embedded_active = False |
|
55 | self.embedded_active = False | |
55 | print "This embedded IPython will not reactivate anymore once you exit." |
|
56 | print "This embedded IPython will not reactivate anymore once you exit." | |
56 |
|
57 | |||
57 |
|
58 | |||
58 | class InteractiveShellEmbed(InteractiveShell): |
|
59 | class InteractiveShellEmbed(InteractiveShell): | |
59 |
|
60 | |||
60 | dummy_mode = Bool(False) |
|
61 | dummy_mode = Bool(False) | |
61 | exit_msg = Str('') |
|
62 | exit_msg = Str('') | |
62 | embedded = CBool(True) |
|
63 | embedded = CBool(True) | |
63 | embedded_active = CBool(True) |
|
64 | embedded_active = CBool(True) | |
64 |
|
65 | |||
65 | def __init__(self, parent=None, config=None, ipythondir=None, usage=None, |
|
66 | def __init__(self, parent=None, config=None, ipythondir=None, usage=None, | |
66 | user_ns=None, user_global_ns=None, |
|
67 | user_ns=None, user_global_ns=None, | |
67 | banner1=None, banner2=None, |
|
68 | banner1=None, banner2=None, | |
68 | custom_exceptions=((),None), exit_msg=''): |
|
69 | custom_exceptions=((),None), exit_msg=''): | |
69 |
|
70 | |||
70 | # First we need to save the state of sys.displayhook and |
|
|||
71 | # sys.ipcompleter so we can restore it when we are done. |
|
|||
72 | self.save_sys_displayhook() |
|
|||
73 | self.save_sys_ipcompleter() |
|
71 | self.save_sys_ipcompleter() | |
74 |
|
72 | |||
75 | super(InteractiveShellEmbed,self).__init__( |
|
73 | super(InteractiveShellEmbed,self).__init__( | |
76 | parent=parent, config=config, ipythondir=ipythondir, usage=usage, |
|
74 | parent=parent, config=config, ipythondir=ipythondir, usage=usage, | |
77 | user_ns=user_ns, user_global_ns=user_global_ns, |
|
75 | user_ns=user_ns, user_global_ns=user_global_ns, | |
78 |
banner1=banner1, banner2=banner2, |
|
76 | banner1=banner1, banner2=banner2, | |
79 | custom_exceptions=custom_exceptions) |
|
77 | custom_exceptions=custom_exceptions) | |
80 |
|
78 | |||
81 | self.save_sys_displayhook_embed() |
|
|||
82 | self.exit_msg = exit_msg |
|
79 | self.exit_msg = exit_msg | |
83 | self.define_magic("kill_embedded", kill_embedded) |
|
80 | self.define_magic("kill_embedded", kill_embedded) | |
84 |
|
81 | |||
85 | # don't use the ipython crash handler so that user exceptions aren't |
|
82 | # don't use the ipython crash handler so that user exceptions aren't | |
86 | # trapped |
|
83 | # trapped | |
87 | sys.excepthook = ultratb.FormattedTB(color_scheme=self.colors, |
|
84 | sys.excepthook = ultratb.FormattedTB(color_scheme=self.colors, | |
88 | mode=self.xmode, |
|
85 | mode=self.xmode, | |
89 | call_pdb=self.pdb) |
|
86 | call_pdb=self.pdb) | |
90 |
|
87 | |||
91 | self.restore_sys_displayhook() |
|
|||
92 | self.restore_sys_ipcompleter() |
|
88 | self.restore_sys_ipcompleter() | |
93 |
|
89 | |||
94 | def init_sys_modules(self): |
|
90 | def init_sys_modules(self): | |
95 | pass |
|
91 | pass | |
96 |
|
92 | |||
97 | def save_sys_displayhook(self): |
|
|||
98 | # sys.displayhook is a global, we need to save the user's original |
|
|||
99 | # Don't rely on __displayhook__, as the user may have changed that. |
|
|||
100 | self.sys_displayhook_orig = sys.displayhook |
|
|||
101 |
|
||||
102 | def save_sys_ipcompleter(self): |
|
93 | def save_sys_ipcompleter(self): | |
103 | """Save readline completer status.""" |
|
94 | """Save readline completer status.""" | |
104 | try: |
|
95 | try: | |
105 | #print 'Save completer',sys.ipcompleter # dbg |
|
96 | #print 'Save completer',sys.ipcompleter # dbg | |
106 | self.sys_ipcompleter_orig = sys.ipcompleter |
|
97 | self.sys_ipcompleter_orig = sys.ipcompleter | |
107 | except: |
|
98 | except: | |
108 | pass # not nested with IPython |
|
99 | pass # not nested with IPython | |
109 |
|
100 | |||
110 | def restore_sys_displayhook(self): |
|
|||
111 | sys.displayhook = self.sys_displayhook_orig |
|
|||
112 |
|
||||
113 | def restore_sys_ipcompleter(self): |
|
101 | def restore_sys_ipcompleter(self): | |
114 | """Restores the readline completer which was in place. |
|
102 | """Restores the readline completer which was in place. | |
115 |
|
103 | |||
116 | This allows embedded IPython within IPython not to disrupt the |
|
104 | This allows embedded IPython within IPython not to disrupt the | |
117 | parent's completion. |
|
105 | parent's completion. | |
118 | """ |
|
106 | """ | |
119 | try: |
|
107 | try: | |
120 | self.readline.set_completer(self.sys_ipcompleter_orig) |
|
108 | self.readline.set_completer(self.sys_ipcompleter_orig) | |
121 | sys.ipcompleter = self.sys_ipcompleter_orig |
|
109 | sys.ipcompleter = self.sys_ipcompleter_orig | |
122 | except: |
|
110 | except: | |
123 | pass |
|
111 | pass | |
124 |
|
112 | |||
125 | def save_sys_displayhook_embed(self): |
|
|||
126 | self.sys_displayhook_embed = sys.displayhook |
|
|||
127 |
|
||||
128 | def restore_sys_displayhook_embed(self): |
|
|||
129 | sys.displayhook = self.sys_displayhook_embed |
|
|||
130 |
|
||||
131 | def __call__(self, header='', local_ns=None, global_ns=None, dummy=None, |
|
113 | def __call__(self, header='', local_ns=None, global_ns=None, dummy=None, | |
132 | stack_depth=1): |
|
114 | stack_depth=1): | |
133 | """Activate the interactive interpreter. |
|
115 | """Activate the interactive interpreter. | |
134 |
|
116 | |||
135 | __call__(self,header='',local_ns=None,global_ns,dummy=None) -> Start |
|
117 | __call__(self,header='',local_ns=None,global_ns,dummy=None) -> Start | |
136 | the interpreter shell with the given local and global namespaces, and |
|
118 | the interpreter shell with the given local and global namespaces, and | |
137 | optionally print a header string at startup. |
|
119 | optionally print a header string at startup. | |
138 |
|
120 | |||
139 | The shell can be globally activated/deactivated using the |
|
121 | The shell can be globally activated/deactivated using the | |
140 | set/get_dummy_mode methods. This allows you to turn off a shell used |
|
122 | set/get_dummy_mode methods. This allows you to turn off a shell used | |
141 | for debugging globally. |
|
123 | for debugging globally. | |
142 |
|
124 | |||
143 | However, *each* time you call the shell you can override the current |
|
125 | However, *each* time you call the shell you can override the current | |
144 | state of dummy_mode with the optional keyword parameter 'dummy'. For |
|
126 | state of dummy_mode with the optional keyword parameter 'dummy'. For | |
145 | example, if you set dummy mode on with IPShell.set_dummy_mode(1), you |
|
127 | example, if you set dummy mode on with IPShell.set_dummy_mode(1), you | |
146 | can still have a specific call work by making it as IPShell(dummy=0). |
|
128 | can still have a specific call work by making it as IPShell(dummy=0). | |
147 |
|
129 | |||
148 | The optional keyword parameter dummy controls whether the call |
|
130 | The optional keyword parameter dummy controls whether the call | |
149 | actually does anything. |
|
131 | actually does anything. | |
150 | """ |
|
132 | """ | |
151 |
|
133 | |||
152 | # If the user has turned it off, go away |
|
134 | # If the user has turned it off, go away | |
153 | if not self.embedded_active: |
|
135 | if not self.embedded_active: | |
154 | return |
|
136 | return | |
155 |
|
137 | |||
156 | # Normal exits from interactive mode set this flag, so the shell can't |
|
138 | # Normal exits from interactive mode set this flag, so the shell can't | |
157 | # re-enter (it checks this variable at the start of interactive mode). |
|
139 | # re-enter (it checks this variable at the start of interactive mode). | |
158 | self.exit_now = False |
|
140 | self.exit_now = False | |
159 |
|
141 | |||
160 | # Allow the dummy parameter to override the global __dummy_mode |
|
142 | # Allow the dummy parameter to override the global __dummy_mode | |
161 | if dummy or (dummy != 0 and self.dummy_mode): |
|
143 | if dummy or (dummy != 0 and self.dummy_mode): | |
162 | return |
|
144 | return | |
163 |
|
145 | |||
164 | self.restore_sys_displayhook_embed() |
|
|||
165 |
|
||||
166 | if self.has_readline: |
|
146 | if self.has_readline: | |
167 | self.set_completer() |
|
147 | self.set_completer() | |
168 |
|
148 | |||
169 | if self.banner and header: |
|
149 | if self.banner and header: | |
170 | format = '%s\n%s\n' |
|
150 | format = '%s\n%s\n' | |
171 | else: |
|
151 | else: | |
172 | format = '%s%s\n' |
|
152 | format = '%s%s\n' | |
173 | banner = format % (self.banner,header) |
|
153 | banner = format % (self.banner,header) | |
174 |
|
154 | |||
175 | # Call the embedding code with a stack depth of 1 so it can skip over |
|
155 | # Call the embedding code with a stack depth of 1 so it can skip over | |
176 | # our call and get the original caller's namespaces. |
|
156 | # our call and get the original caller's namespaces. | |
177 |
self.mainloop(banner, local_ns, global_ns, |
|
157 | self.mainloop(banner, local_ns, global_ns, | |
178 | stack_depth=stack_depth) |
|
158 | stack_depth=stack_depth) | |
179 |
|
159 | |||
180 | if self.exit_msg is not None: |
|
160 | if self.exit_msg is not None: | |
181 | print self.exit_msg |
|
161 | print self.exit_msg | |
182 |
|
162 | |||
183 | # Restore global systems (display, completion) |
|
|||
184 | self.restore_sys_displayhook() |
|
|||
185 | self.restore_sys_ipcompleter() |
|
163 | self.restore_sys_ipcompleter() | |
186 |
|
164 | |||
187 | def mainloop(self,header='',local_ns=None,global_ns=None,stack_depth=0): |
|
165 | def mainloop(self,header='',local_ns=None,global_ns=None,stack_depth=0): | |
188 | """Embeds IPython into a running python program. |
|
166 | """Embeds IPython into a running python program. | |
189 |
|
167 | |||
190 | Input: |
|
168 | Input: | |
191 |
|
169 | |||
192 | - header: An optional header message can be specified. |
|
170 | - header: An optional header message can be specified. | |
193 |
|
171 | |||
194 | - local_ns, global_ns: working namespaces. If given as None, the |
|
172 | - local_ns, global_ns: working namespaces. If given as None, the | |
195 | IPython-initialized one is updated with __main__.__dict__, so that |
|
173 | IPython-initialized one is updated with __main__.__dict__, so that | |
196 | program variables become visible but user-specific configuration |
|
174 | program variables become visible but user-specific configuration | |
197 | remains possible. |
|
175 | remains possible. | |
198 |
|
176 | |||
199 | - stack_depth: specifies how many levels in the stack to go to |
|
177 | - stack_depth: specifies how many levels in the stack to go to | |
200 | looking for namespaces (when local_ns and global_ns are None). This |
|
178 | looking for namespaces (when local_ns and global_ns are None). This | |
201 | allows an intermediate caller to make sure that this function gets |
|
179 | allows an intermediate caller to make sure that this function gets | |
202 | the namespace from the intended level in the stack. By default (0) |
|
180 | the namespace from the intended level in the stack. By default (0) | |
203 | it will get its locals and globals from the immediate caller. |
|
181 | it will get its locals and globals from the immediate caller. | |
204 |
|
182 | |||
205 | Warning: it's possible to use this in a program which is being run by |
|
183 | Warning: it's possible to use this in a program which is being run by | |
206 | IPython itself (via %run), but some funny things will happen (a few |
|
184 | IPython itself (via %run), but some funny things will happen (a few | |
207 | globals get overwritten). In the future this will be cleaned up, as |
|
185 | globals get overwritten). In the future this will be cleaned up, as | |
208 | there is no fundamental reason why it can't work perfectly.""" |
|
186 | there is no fundamental reason why it can't work perfectly.""" | |
209 |
|
187 | |||
210 | # Get locals and globals from caller |
|
188 | # Get locals and globals from caller | |
211 | if local_ns is None or global_ns is None: |
|
189 | if local_ns is None or global_ns is None: | |
212 | call_frame = sys._getframe(stack_depth).f_back |
|
190 | call_frame = sys._getframe(stack_depth).f_back | |
213 |
|
191 | |||
214 | if local_ns is None: |
|
192 | if local_ns is None: | |
215 | local_ns = call_frame.f_locals |
|
193 | local_ns = call_frame.f_locals | |
216 | if global_ns is None: |
|
194 | if global_ns is None: | |
217 | global_ns = call_frame.f_globals |
|
195 | global_ns = call_frame.f_globals | |
218 |
|
196 | |||
219 | # Update namespaces and fire up interpreter |
|
197 | # Update namespaces and fire up interpreter | |
220 |
|
198 | |||
221 | # The global one is easy, we can just throw it in |
|
199 | # The global one is easy, we can just throw it in | |
222 | self.user_global_ns = global_ns |
|
200 | self.user_global_ns = global_ns | |
223 |
|
201 | |||
224 | # but the user/local one is tricky: ipython needs it to store internal |
|
202 | # but the user/local one is tricky: ipython needs it to store internal | |
225 | # data, but we also need the locals. We'll copy locals in the user |
|
203 | # data, but we also need the locals. We'll copy locals in the user | |
226 | # one, but will track what got copied so we can delete them at exit. |
|
204 | # one, but will track what got copied so we can delete them at exit. | |
227 | # This is so that a later embedded call doesn't see locals from a |
|
205 | # This is so that a later embedded call doesn't see locals from a | |
228 | # previous call (which most likely existed in a separate scope). |
|
206 | # previous call (which most likely existed in a separate scope). | |
229 | local_varnames = local_ns.keys() |
|
207 | local_varnames = local_ns.keys() | |
230 | self.user_ns.update(local_ns) |
|
208 | self.user_ns.update(local_ns) | |
231 | #self.user_ns['local_ns'] = local_ns # dbg |
|
209 | #self.user_ns['local_ns'] = local_ns # dbg | |
232 |
|
210 | |||
233 | # Patch for global embedding to make sure that things don't overwrite |
|
211 | # Patch for global embedding to make sure that things don't overwrite | |
234 | # user globals accidentally. Thanks to Richard <rxe@renre-europe.com> |
|
212 | # user globals accidentally. Thanks to Richard <rxe@renre-europe.com> | |
235 | # FIXME. Test this a bit more carefully (the if.. is new) |
|
213 | # FIXME. Test this a bit more carefully (the if.. is new) | |
236 | if local_ns is None and global_ns is None: |
|
214 | if local_ns is None and global_ns is None: | |
237 | self.user_global_ns.update(__main__.__dict__) |
|
215 | self.user_global_ns.update(__main__.__dict__) | |
238 |
|
216 | |||
239 | # make sure the tab-completer has the correct frame information, so it |
|
217 | # make sure the tab-completer has the correct frame information, so it | |
240 | # actually completes using the frame's locals/globals |
|
218 | # actually completes using the frame's locals/globals | |
241 | self.set_completer_frame() |
|
219 | self.set_completer_frame() | |
242 |
|
220 | |||
243 | with self.builtin_trap: |
|
221 | with nested(self.builtin_trap, self.display_trap): | |
244 | self.interact(header) |
|
222 | self.interact(header) | |
245 |
|
223 | |||
246 | # now, purge out the user namespace from anything we might have added |
|
224 | # now, purge out the user namespace from anything we might have added | |
247 | # from the caller's local namespace |
|
225 | # from the caller's local namespace | |
248 | delvar = self.user_ns.pop |
|
226 | delvar = self.user_ns.pop | |
249 | for var in local_varnames: |
|
227 | for var in local_varnames: | |
250 | delvar(var,None) |
|
228 | delvar(var,None) | |
251 |
|
229 | |||
252 |
|
230 | |||
253 | _embedded_shell = None |
|
231 | _embedded_shell = None | |
254 |
|
232 | |||
255 |
|
233 | |||
256 | def embed(header='', config=None, usage=None, banner1=None, banner2=None, |
|
234 | def embed(header='', config=None, usage=None, banner1=None, banner2=None, | |
257 | exit_msg=''): |
|
235 | exit_msg=''): | |
258 | """Call this to embed IPython at the current point in your program. |
|
236 | """Call this to embed IPython at the current point in your program. | |
259 |
|
237 | |||
260 | The first invocation of this will create an :class:`InteractiveShellEmbed` |
|
238 | The first invocation of this will create an :class:`InteractiveShellEmbed` | |
261 | instance and then call it. Consecutive calls just call the already |
|
239 | instance and then call it. Consecutive calls just call the already | |
262 | created instance. |
|
240 | created instance. | |
263 |
|
241 | |||
264 | Here is a simple example:: |
|
242 | Here is a simple example:: | |
265 |
|
243 | |||
266 | from IPython import embed |
|
244 | from IPython import embed | |
267 | a = 10 |
|
245 | a = 10 | |
268 | b = 20 |
|
246 | b = 20 | |
269 | embed('First time') |
|
247 | embed('First time') | |
270 | c = 30 |
|
248 | c = 30 | |
271 | d = 40 |
|
249 | d = 40 | |
272 | embed |
|
250 | embed | |
273 |
|
251 | |||
274 | Full customization can be done by passing a :class:`Struct` in as the |
|
252 | Full customization can be done by passing a :class:`Struct` in as the | |
275 | config argument. |
|
253 | config argument. | |
276 | """ |
|
254 | """ | |
277 | global _embedded_shell |
|
255 | global _embedded_shell | |
278 | if _embedded_shell is None: |
|
256 | if _embedded_shell is None: | |
279 | _embedded_shell = InteractiveShellEmbed(config=config, |
|
257 | _embedded_shell = InteractiveShellEmbed(config=config, | |
280 | usage=usage, banner1=banner1, banner2=banner2, exit_msg=exit_msg) |
|
258 | usage=usage, banner1=banner1, banner2=banner2, exit_msg=exit_msg) | |
281 | _embedded_shell(header=header, stack_depth=2) |
|
259 | _embedded_shell(header=header, stack_depth=2) | |
282 |
|
260 |
@@ -1,2961 +1,2958 b'' | |||||
1 | # -*- coding: utf-8 -*- |
|
1 | # -*- coding: utf-8 -*- | |
2 | """ |
|
2 | """ | |
3 | Main IPython Component |
|
3 | Main IPython Component | |
4 | """ |
|
4 | """ | |
5 |
|
5 | |||
6 | #----------------------------------------------------------------------------- |
|
6 | #----------------------------------------------------------------------------- | |
7 | # Copyright (C) 2001 Janko Hauser <jhauser@zscout.de> |
|
7 | # Copyright (C) 2001 Janko Hauser <jhauser@zscout.de> | |
8 | # Copyright (C) 2001-2007 Fernando Perez. <fperez@colorado.edu> |
|
8 | # Copyright (C) 2001-2007 Fernando Perez. <fperez@colorado.edu> | |
9 | # Copyright (C) 2008-2009 The IPython Development Team |
|
9 | # Copyright (C) 2008-2009 The IPython Development Team | |
10 | # |
|
10 | # | |
11 | # Distributed under the terms of the BSD License. The full license is in |
|
11 | # Distributed under the terms of the BSD License. The full license is in | |
12 | # the file COPYING, distributed as part of this software. |
|
12 | # the file COPYING, distributed as part of this software. | |
13 | #----------------------------------------------------------------------------- |
|
13 | #----------------------------------------------------------------------------- | |
14 |
|
14 | |||
15 | #----------------------------------------------------------------------------- |
|
15 | #----------------------------------------------------------------------------- | |
16 | # Imports |
|
16 | # Imports | |
17 | #----------------------------------------------------------------------------- |
|
17 | #----------------------------------------------------------------------------- | |
18 |
|
18 | |||
19 | from __future__ import with_statement |
|
19 | from __future__ import with_statement | |
20 |
|
20 | |||
21 | import __main__ |
|
21 | import __main__ | |
22 | import __builtin__ |
|
22 | import __builtin__ | |
23 | import StringIO |
|
23 | import StringIO | |
24 | import bdb |
|
24 | import bdb | |
25 | import codeop |
|
25 | import codeop | |
26 | import exceptions |
|
26 | import exceptions | |
27 | import glob |
|
27 | import glob | |
28 | import keyword |
|
28 | import keyword | |
29 | import new |
|
29 | import new | |
30 | import os |
|
30 | import os | |
31 | import re |
|
31 | import re | |
32 | import shutil |
|
32 | import shutil | |
33 | import string |
|
33 | import string | |
34 | import sys |
|
34 | import sys | |
35 | import tempfile |
|
35 | import tempfile | |
|
36 | from contextlib import nested | |||
36 |
|
37 | |||
37 | from IPython.core import ultratb |
|
38 | from IPython.core import ultratb | |
38 | from IPython.core import debugger, oinspect |
|
39 | from IPython.core import debugger, oinspect | |
39 | from IPython.core import shadowns |
|
40 | from IPython.core import shadowns | |
40 | from IPython.core import history as ipcorehist |
|
41 | from IPython.core import history as ipcorehist | |
41 | from IPython.core import prefilter |
|
42 | from IPython.core import prefilter | |
42 | from IPython.core.autocall import IPyAutocall |
|
43 | from IPython.core.autocall import IPyAutocall | |
43 | from IPython.core.builtin_trap import BuiltinTrap |
|
44 | from IPython.core.builtin_trap import BuiltinTrap | |
|
45 | from IPython.core.display_trap import DisplayTrap | |||
44 | from IPython.core.fakemodule import FakeModule, init_fakemod_dict |
|
46 | from IPython.core.fakemodule import FakeModule, init_fakemod_dict | |
45 | from IPython.core.logger import Logger |
|
47 | from IPython.core.logger import Logger | |
46 | from IPython.core.magic import Magic |
|
48 | from IPython.core.magic import Magic | |
47 | from IPython.core.prompts import CachedOutput |
|
49 | from IPython.core.prompts import CachedOutput | |
48 | from IPython.core.page import page |
|
50 | from IPython.core.page import page | |
49 | from IPython.core.component import Component |
|
51 | from IPython.core.component import Component | |
50 | from IPython.core.oldusersetup import user_setup |
|
52 | from IPython.core.oldusersetup import user_setup | |
51 | from IPython.core.usage import interactive_usage, default_banner |
|
53 | from IPython.core.usage import interactive_usage, default_banner | |
52 | from IPython.core.error import TryNext, UsageError |
|
54 | from IPython.core.error import TryNext, UsageError | |
53 |
|
55 | |||
54 | from IPython.extensions import pickleshare |
|
56 | from IPython.extensions import pickleshare | |
55 | from IPython.external.Itpl import ItplNS |
|
57 | from IPython.external.Itpl import ItplNS | |
56 | from IPython.lib.backgroundjobs import BackgroundJobManager |
|
58 | from IPython.lib.backgroundjobs import BackgroundJobManager | |
57 | from IPython.utils.ipstruct import Struct |
|
59 | from IPython.utils.ipstruct import Struct | |
58 | from IPython.utils import PyColorize |
|
60 | from IPython.utils import PyColorize | |
59 | from IPython.utils.genutils import * |
|
61 | from IPython.utils.genutils import * | |
60 | from IPython.utils.strdispatch import StrDispatch |
|
62 | from IPython.utils.strdispatch import StrDispatch | |
61 | from IPython.utils.platutils import toggle_set_term_title, set_term_title |
|
63 | from IPython.utils.platutils import toggle_set_term_title, set_term_title | |
62 |
|
64 | |||
63 | from IPython.utils.traitlets import ( |
|
65 | from IPython.utils.traitlets import ( | |
64 | Int, Float, Str, CBool, CaselessStrEnum, Enum, List, Unicode |
|
66 | Int, Float, Str, CBool, CaselessStrEnum, Enum, List, Unicode | |
65 | ) |
|
67 | ) | |
66 |
|
68 | |||
67 | #----------------------------------------------------------------------------- |
|
69 | #----------------------------------------------------------------------------- | |
68 | # Globals |
|
70 | # Globals | |
69 | #----------------------------------------------------------------------------- |
|
71 | #----------------------------------------------------------------------------- | |
70 |
|
72 | |||
71 |
|
73 | |||
72 | # store the builtin raw_input globally, and use this always, in case user code |
|
74 | # store the builtin raw_input globally, and use this always, in case user code | |
73 | # overwrites it (like wx.py.PyShell does) |
|
75 | # overwrites it (like wx.py.PyShell does) | |
74 | raw_input_original = raw_input |
|
76 | raw_input_original = raw_input | |
75 |
|
77 | |||
76 | # compiled regexps for autoindent management |
|
78 | # compiled regexps for autoindent management | |
77 | dedent_re = re.compile(r'^\s+raise|^\s+return|^\s+pass') |
|
79 | dedent_re = re.compile(r'^\s+raise|^\s+return|^\s+pass') | |
78 |
|
80 | |||
79 |
|
81 | |||
80 | #----------------------------------------------------------------------------- |
|
82 | #----------------------------------------------------------------------------- | |
81 | # Utilities |
|
83 | # Utilities | |
82 | #----------------------------------------------------------------------------- |
|
84 | #----------------------------------------------------------------------------- | |
83 |
|
85 | |||
84 |
|
86 | |||
85 | ini_spaces_re = re.compile(r'^(\s+)') |
|
87 | ini_spaces_re = re.compile(r'^(\s+)') | |
86 |
|
88 | |||
87 |
|
89 | |||
88 | def num_ini_spaces(strng): |
|
90 | def num_ini_spaces(strng): | |
89 | """Return the number of initial spaces in a string""" |
|
91 | """Return the number of initial spaces in a string""" | |
90 |
|
92 | |||
91 | ini_spaces = ini_spaces_re.match(strng) |
|
93 | ini_spaces = ini_spaces_re.match(strng) | |
92 | if ini_spaces: |
|
94 | if ini_spaces: | |
93 | return ini_spaces.end() |
|
95 | return ini_spaces.end() | |
94 | else: |
|
96 | else: | |
95 | return 0 |
|
97 | return 0 | |
96 |
|
98 | |||
97 |
|
99 | |||
98 | def softspace(file, newvalue): |
|
100 | def softspace(file, newvalue): | |
99 | """Copied from code.py, to remove the dependency""" |
|
101 | """Copied from code.py, to remove the dependency""" | |
100 |
|
102 | |||
101 | oldvalue = 0 |
|
103 | oldvalue = 0 | |
102 | try: |
|
104 | try: | |
103 | oldvalue = file.softspace |
|
105 | oldvalue = file.softspace | |
104 | except AttributeError: |
|
106 | except AttributeError: | |
105 | pass |
|
107 | pass | |
106 | try: |
|
108 | try: | |
107 | file.softspace = newvalue |
|
109 | file.softspace = newvalue | |
108 | except (AttributeError, TypeError): |
|
110 | except (AttributeError, TypeError): | |
109 | # "attribute-less object" or "read-only attributes" |
|
111 | # "attribute-less object" or "read-only attributes" | |
110 | pass |
|
112 | pass | |
111 | return oldvalue |
|
113 | return oldvalue | |
112 |
|
114 | |||
113 |
|
115 | |||
114 | class SpaceInInput(exceptions.Exception): pass |
|
116 | class SpaceInInput(exceptions.Exception): pass | |
115 |
|
117 | |||
116 | class Bunch: pass |
|
118 | class Bunch: pass | |
117 |
|
119 | |||
118 | class InputList(list): |
|
120 | class InputList(list): | |
119 | """Class to store user input. |
|
121 | """Class to store user input. | |
120 |
|
122 | |||
121 | It's basically a list, but slices return a string instead of a list, thus |
|
123 | It's basically a list, but slices return a string instead of a list, thus | |
122 | allowing things like (assuming 'In' is an instance): |
|
124 | allowing things like (assuming 'In' is an instance): | |
123 |
|
125 | |||
124 | exec In[4:7] |
|
126 | exec In[4:7] | |
125 |
|
127 | |||
126 | or |
|
128 | or | |
127 |
|
129 | |||
128 | exec In[5:9] + In[14] + In[21:25]""" |
|
130 | exec In[5:9] + In[14] + In[21:25]""" | |
129 |
|
131 | |||
130 | def __getslice__(self,i,j): |
|
132 | def __getslice__(self,i,j): | |
131 | return ''.join(list.__getslice__(self,i,j)) |
|
133 | return ''.join(list.__getslice__(self,i,j)) | |
132 |
|
134 | |||
133 |
|
135 | |||
134 | class SyntaxTB(ultratb.ListTB): |
|
136 | class SyntaxTB(ultratb.ListTB): | |
135 | """Extension which holds some state: the last exception value""" |
|
137 | """Extension which holds some state: the last exception value""" | |
136 |
|
138 | |||
137 | def __init__(self,color_scheme = 'NoColor'): |
|
139 | def __init__(self,color_scheme = 'NoColor'): | |
138 | ultratb.ListTB.__init__(self,color_scheme) |
|
140 | ultratb.ListTB.__init__(self,color_scheme) | |
139 | self.last_syntax_error = None |
|
141 | self.last_syntax_error = None | |
140 |
|
142 | |||
141 | def __call__(self, etype, value, elist): |
|
143 | def __call__(self, etype, value, elist): | |
142 | self.last_syntax_error = value |
|
144 | self.last_syntax_error = value | |
143 | ultratb.ListTB.__call__(self,etype,value,elist) |
|
145 | ultratb.ListTB.__call__(self,etype,value,elist) | |
144 |
|
146 | |||
145 | def clear_err_state(self): |
|
147 | def clear_err_state(self): | |
146 | """Return the current error state and clear it""" |
|
148 | """Return the current error state and clear it""" | |
147 | e = self.last_syntax_error |
|
149 | e = self.last_syntax_error | |
148 | self.last_syntax_error = None |
|
150 | self.last_syntax_error = None | |
149 | return e |
|
151 | return e | |
150 |
|
152 | |||
151 |
|
153 | |||
152 | def get_default_editor(): |
|
154 | def get_default_editor(): | |
153 | try: |
|
155 | try: | |
154 | ed = os.environ['EDITOR'] |
|
156 | ed = os.environ['EDITOR'] | |
155 | except KeyError: |
|
157 | except KeyError: | |
156 | if os.name == 'posix': |
|
158 | if os.name == 'posix': | |
157 | ed = 'vi' # the only one guaranteed to be there! |
|
159 | ed = 'vi' # the only one guaranteed to be there! | |
158 | else: |
|
160 | else: | |
159 | ed = 'notepad' # same in Windows! |
|
161 | ed = 'notepad' # same in Windows! | |
160 | return ed |
|
162 | return ed | |
161 |
|
163 | |||
162 |
|
164 | |||
163 | class SeparateStr(Str): |
|
165 | class SeparateStr(Str): | |
164 | """A Str subclass to validate separate_in, separate_out, etc. |
|
166 | """A Str subclass to validate separate_in, separate_out, etc. | |
165 |
|
167 | |||
166 | This is a Str based traitlet that converts '0'->'' and '\\n'->'\n'. |
|
168 | This is a Str based traitlet that converts '0'->'' and '\\n'->'\n'. | |
167 | """ |
|
169 | """ | |
168 |
|
170 | |||
169 | def validate(self, obj, value): |
|
171 | def validate(self, obj, value): | |
170 | if value == '0': value = '' |
|
172 | if value == '0': value = '' | |
171 | value = value.replace('\\n','\n') |
|
173 | value = value.replace('\\n','\n') | |
172 | return super(SeparateStr, self).validate(obj, value) |
|
174 | return super(SeparateStr, self).validate(obj, value) | |
173 |
|
175 | |||
174 |
|
176 | |||
175 | #----------------------------------------------------------------------------- |
|
177 | #----------------------------------------------------------------------------- | |
176 | # Main IPython class |
|
178 | # Main IPython class | |
177 | #----------------------------------------------------------------------------- |
|
179 | #----------------------------------------------------------------------------- | |
178 |
|
180 | |||
179 |
|
181 | |||
180 | class InteractiveShell(Component, Magic): |
|
182 | class InteractiveShell(Component, Magic): | |
181 | """An enhanced, interactive shell for Python.""" |
|
183 | """An enhanced, interactive shell for Python.""" | |
182 |
|
184 | |||
183 | autocall = Enum((0,1,2), config_key='AUTOCALL') |
|
185 | autocall = Enum((0,1,2), config_key='AUTOCALL') | |
184 | autoedit_syntax = CBool(False, config_key='AUTOEDIT_SYNTAX') |
|
186 | autoedit_syntax = CBool(False, config_key='AUTOEDIT_SYNTAX') | |
185 | autoindent = CBool(True, config_key='AUTOINDENT') |
|
187 | autoindent = CBool(True, config_key='AUTOINDENT') | |
186 | automagic = CBool(True, config_key='AUTOMAGIC') |
|
188 | automagic = CBool(True, config_key='AUTOMAGIC') | |
187 | display_banner = CBool(True, config_key='DISPLAY_BANNER') |
|
189 | display_banner = CBool(True, config_key='DISPLAY_BANNER') | |
188 | banner = Str('') |
|
190 | banner = Str('') | |
189 | banner1 = Str(default_banner, config_key='BANNER1') |
|
191 | banner1 = Str(default_banner, config_key='BANNER1') | |
190 | banner2 = Str('', config_key='BANNER2') |
|
192 | banner2 = Str('', config_key='BANNER2') | |
191 | c = Str('', config_key='C') |
|
193 | c = Str('', config_key='C') | |
192 | cache_size = Int(1000, config_key='CACHE_SIZE') |
|
194 | cache_size = Int(1000, config_key='CACHE_SIZE') | |
193 | classic = CBool(False, config_key='CLASSIC') |
|
195 | classic = CBool(False, config_key='CLASSIC') | |
194 | color_info = CBool(True, config_key='COLOR_INFO') |
|
196 | color_info = CBool(True, config_key='COLOR_INFO') | |
195 | colors = CaselessStrEnum(('NoColor','LightBG','Linux'), |
|
197 | colors = CaselessStrEnum(('NoColor','LightBG','Linux'), | |
196 | default_value='LightBG', config_key='COLORS') |
|
198 | default_value='LightBG', config_key='COLORS') | |
197 | confirm_exit = CBool(True, config_key='CONFIRM_EXIT') |
|
199 | confirm_exit = CBool(True, config_key='CONFIRM_EXIT') | |
198 | debug = CBool(False, config_key='DEBUG') |
|
200 | debug = CBool(False, config_key='DEBUG') | |
199 | deep_reload = CBool(False, config_key='DEEP_RELOAD') |
|
201 | deep_reload = CBool(False, config_key='DEEP_RELOAD') | |
200 | embedded = CBool(False) |
|
202 | embedded = CBool(False) | |
201 | embedded_active = CBool(False) |
|
203 | embedded_active = CBool(False) | |
202 | editor = Str(get_default_editor(), config_key='EDITOR') |
|
204 | editor = Str(get_default_editor(), config_key='EDITOR') | |
203 | filename = Str("<ipython console>") |
|
205 | filename = Str("<ipython console>") | |
204 | interactive = CBool(False, config_key='INTERACTIVE') |
|
206 | interactive = CBool(False, config_key='INTERACTIVE') | |
205 | ipythondir= Unicode('', config_key='IPYTHONDIR') # Set to os.getcwd() in __init__ |
|
207 | ipythondir= Unicode('', config_key='IPYTHONDIR') # Set to os.getcwd() in __init__ | |
206 | logstart = CBool(False, config_key='LOGSTART') |
|
208 | logstart = CBool(False, config_key='LOGSTART') | |
207 | logfile = Str('', config_key='LOGFILE') |
|
209 | logfile = Str('', config_key='LOGFILE') | |
208 | logplay = Str('', config_key='LOGPLAY') |
|
210 | logplay = Str('', config_key='LOGPLAY') | |
209 | multi_line_specials = CBool(True, config_key='MULTI_LINE_SPECIALS') |
|
211 | multi_line_specials = CBool(True, config_key='MULTI_LINE_SPECIALS') | |
210 | object_info_string_level = Enum((0,1,2), default_value=0, |
|
212 | object_info_string_level = Enum((0,1,2), default_value=0, | |
211 | config_keys='OBJECT_INFO_STRING_LEVEL') |
|
213 | config_keys='OBJECT_INFO_STRING_LEVEL') | |
212 | pager = Str('less', config_key='PAGER') |
|
214 | pager = Str('less', config_key='PAGER') | |
213 | pdb = CBool(False, config_key='PDB') |
|
215 | pdb = CBool(False, config_key='PDB') | |
214 | pprint = CBool(True, config_key='PPRINT') |
|
216 | pprint = CBool(True, config_key='PPRINT') | |
215 | profile = Str('', config_key='PROFILE') |
|
217 | profile = Str('', config_key='PROFILE') | |
216 | prompt_in1 = Str('In [\\#]: ', config_key='PROMPT_IN1') |
|
218 | prompt_in1 = Str('In [\\#]: ', config_key='PROMPT_IN1') | |
217 | prompt_in2 = Str(' .\\D.: ', config_key='PROMPT_IN2') |
|
219 | prompt_in2 = Str(' .\\D.: ', config_key='PROMPT_IN2') | |
218 | prompt_out = Str('Out[\\#]: ', config_key='PROMPT_OUT1') |
|
220 | prompt_out = Str('Out[\\#]: ', config_key='PROMPT_OUT1') | |
219 | prompts_pad_left = CBool(True, config_key='PROMPTS_PAD_LEFT') |
|
221 | prompts_pad_left = CBool(True, config_key='PROMPTS_PAD_LEFT') | |
220 | quiet = CBool(False, config_key='QUIET') |
|
222 | quiet = CBool(False, config_key='QUIET') | |
221 |
|
223 | |||
222 | readline_use = CBool(True, config_key='READLINE_USE') |
|
224 | readline_use = CBool(True, config_key='READLINE_USE') | |
223 | readline_merge_completions = CBool(True, |
|
225 | readline_merge_completions = CBool(True, | |
224 | config_key='READLINE_MERGE_COMPLETIONS') |
|
226 | config_key='READLINE_MERGE_COMPLETIONS') | |
225 | readline_omit__names = Enum((0,1,2), default_value=0, |
|
227 | readline_omit__names = Enum((0,1,2), default_value=0, | |
226 | config_key='READLINE_OMIT_NAMES') |
|
228 | config_key='READLINE_OMIT_NAMES') | |
227 | readline_remove_delims = Str('-/~', config_key='READLINE_REMOVE_DELIMS') |
|
229 | readline_remove_delims = Str('-/~', config_key='READLINE_REMOVE_DELIMS') | |
228 | readline_parse_and_bind = List([ |
|
230 | readline_parse_and_bind = List([ | |
229 | 'tab: complete', |
|
231 | 'tab: complete', | |
230 | '"\C-l": possible-completions', |
|
232 | '"\C-l": possible-completions', | |
231 | 'set show-all-if-ambiguous on', |
|
233 | 'set show-all-if-ambiguous on', | |
232 | '"\C-o": tab-insert', |
|
234 | '"\C-o": tab-insert', | |
233 | '"\M-i": " "', |
|
235 | '"\M-i": " "', | |
234 | '"\M-o": "\d\d\d\d"', |
|
236 | '"\M-o": "\d\d\d\d"', | |
235 | '"\M-I": "\d\d\d\d"', |
|
237 | '"\M-I": "\d\d\d\d"', | |
236 | '"\C-r": reverse-search-history', |
|
238 | '"\C-r": reverse-search-history', | |
237 | '"\C-s": forward-search-history', |
|
239 | '"\C-s": forward-search-history', | |
238 | '"\C-p": history-search-backward', |
|
240 | '"\C-p": history-search-backward', | |
239 | '"\C-n": history-search-forward', |
|
241 | '"\C-n": history-search-forward', | |
240 | '"\e[A": history-search-backward', |
|
242 | '"\e[A": history-search-backward', | |
241 | '"\e[B": history-search-forward', |
|
243 | '"\e[B": history-search-forward', | |
242 | '"\C-k": kill-line', |
|
244 | '"\C-k": kill-line', | |
243 | '"\C-u": unix-line-discard', |
|
245 | '"\C-u": unix-line-discard', | |
244 | ], allow_none=False, config_key='READLINE_PARSE_AND_BIND' |
|
246 | ], allow_none=False, config_key='READLINE_PARSE_AND_BIND' | |
245 | ) |
|
247 | ) | |
246 |
|
248 | |||
247 | screen_length = Int(0, config_key='SCREEN_LENGTH') |
|
249 | screen_length = Int(0, config_key='SCREEN_LENGTH') | |
248 |
|
250 | |||
249 | # Use custom TraitletTypes that convert '0'->'' and '\\n'->'\n' |
|
251 | # Use custom TraitletTypes that convert '0'->'' and '\\n'->'\n' | |
250 | separate_in = SeparateStr('\n', config_key='SEPARATE_IN') |
|
252 | separate_in = SeparateStr('\n', config_key='SEPARATE_IN') | |
251 | separate_out = SeparateStr('', config_key='SEPARATE_OUT') |
|
253 | separate_out = SeparateStr('', config_key='SEPARATE_OUT') | |
252 | separate_out2 = SeparateStr('', config_key='SEPARATE_OUT2') |
|
254 | separate_out2 = SeparateStr('', config_key='SEPARATE_OUT2') | |
253 |
|
255 | |||
254 | system_header = Str('IPython system call: ', config_key='SYSTEM_HEADER') |
|
256 | system_header = Str('IPython system call: ', config_key='SYSTEM_HEADER') | |
255 | system_verbose = CBool(False, config_key='SYSTEM_VERBOSE') |
|
257 | system_verbose = CBool(False, config_key='SYSTEM_VERBOSE') | |
256 | term_title = CBool(False, config_key='TERM_TITLE') |
|
258 | term_title = CBool(False, config_key='TERM_TITLE') | |
257 | wildcards_case_sensitive = CBool(True, config_key='WILDCARDS_CASE_SENSITIVE') |
|
259 | wildcards_case_sensitive = CBool(True, config_key='WILDCARDS_CASE_SENSITIVE') | |
258 | xmode = CaselessStrEnum(('Context','Plain', 'Verbose'), |
|
260 | xmode = CaselessStrEnum(('Context','Plain', 'Verbose'), | |
259 | default_value='Context', config_key='XMODE') |
|
261 | default_value='Context', config_key='XMODE') | |
260 |
|
262 | |||
261 | alias = List(allow_none=False, config_key='ALIAS') |
|
263 | alias = List(allow_none=False, config_key='ALIAS') | |
262 | autoexec = List(allow_none=False) |
|
264 | autoexec = List(allow_none=False) | |
263 |
|
265 | |||
264 | # class attribute to indicate whether the class supports threads or not. |
|
266 | # class attribute to indicate whether the class supports threads or not. | |
265 | # Subclasses with thread support should override this as needed. |
|
267 | # Subclasses with thread support should override this as needed. | |
266 | isthreaded = False |
|
268 | isthreaded = False | |
267 |
|
269 | |||
268 | def __init__(self, parent=None, config=None, ipythondir=None, usage=None, |
|
270 | def __init__(self, parent=None, config=None, ipythondir=None, usage=None, | |
269 | user_ns=None, user_global_ns=None, |
|
271 | user_ns=None, user_global_ns=None, | |
270 | banner1=None, banner2=None, |
|
272 | banner1=None, banner2=None, | |
271 | custom_exceptions=((),None)): |
|
273 | custom_exceptions=((),None)): | |
272 |
|
274 | |||
273 | # This is where traitlets with a config_key argument are updated |
|
275 | # This is where traitlets with a config_key argument are updated | |
274 | # from the values on config. |
|
276 | # from the values on config. | |
275 | super(InteractiveShell, self).__init__(parent, config=config, name='__IP') |
|
277 | super(InteractiveShell, self).__init__(parent, config=config, name='__IP') | |
276 |
|
278 | |||
277 | # These are relatively independent and stateless |
|
279 | # These are relatively independent and stateless | |
278 | self.init_ipythondir(ipythondir) |
|
280 | self.init_ipythondir(ipythondir) | |
279 | self.init_instance_attrs() |
|
281 | self.init_instance_attrs() | |
280 | self.init_term_title() |
|
282 | self.init_term_title() | |
281 | self.init_usage(usage) |
|
283 | self.init_usage(usage) | |
282 | self.init_banner(banner1, banner2) |
|
284 | self.init_banner(banner1, banner2) | |
283 |
|
285 | |||
284 | # Create namespaces (user_ns, user_global_ns, alias_table, etc.) |
|
286 | # Create namespaces (user_ns, user_global_ns, alias_table, etc.) | |
285 | self.init_create_namespaces(user_ns, user_global_ns) |
|
287 | self.init_create_namespaces(user_ns, user_global_ns) | |
286 | # This has to be done after init_create_namespaces because it uses |
|
288 | # This has to be done after init_create_namespaces because it uses | |
287 | # something in self.user_ns, but before init_sys_modules, which |
|
289 | # something in self.user_ns, but before init_sys_modules, which | |
288 | # is the first thing to modify sys. |
|
290 | # is the first thing to modify sys. | |
289 | self.save_sys_module_state() |
|
291 | self.save_sys_module_state() | |
290 | self.init_sys_modules() |
|
292 | self.init_sys_modules() | |
291 |
|
293 | |||
292 | self.init_history() |
|
294 | self.init_history() | |
293 | self.init_encoding() |
|
295 | self.init_encoding() | |
294 | self.init_handlers() |
|
296 | self.init_handlers() | |
295 |
|
297 | |||
296 | Magic.__init__(self, self) |
|
298 | Magic.__init__(self, self) | |
297 |
|
299 | |||
298 | self.init_syntax_highlighting() |
|
300 | self.init_syntax_highlighting() | |
299 | self.init_hooks() |
|
301 | self.init_hooks() | |
300 | self.init_pushd_popd_magic() |
|
302 | self.init_pushd_popd_magic() | |
301 | self.init_traceback_handlers(custom_exceptions) |
|
303 | self.init_traceback_handlers(custom_exceptions) | |
302 | self.init_user_ns() |
|
304 | self.init_user_ns() | |
303 | self.init_logger() |
|
305 | self.init_logger() | |
304 | self.init_aliases() |
|
306 | self.init_aliases() | |
305 | self.init_builtins() |
|
307 | self.init_builtins() | |
306 |
|
308 | |||
307 | # pre_config_initialization |
|
309 | # pre_config_initialization | |
308 | self.init_shadow_hist() |
|
310 | self.init_shadow_hist() | |
309 |
|
311 | |||
310 | # The next section should contain averything that was in ipmaker. |
|
312 | # The next section should contain averything that was in ipmaker. | |
311 | self.init_logstart() |
|
313 | self.init_logstart() | |
312 |
|
314 | |||
313 | # The following was in post_config_initialization |
|
315 | # The following was in post_config_initialization | |
314 | self.init_inspector() |
|
316 | self.init_inspector() | |
315 | self.init_readline() |
|
317 | self.init_readline() | |
316 | self.init_prompts() |
|
318 | self.init_prompts() | |
317 | self.init_displayhook() |
|
319 | self.init_displayhook() | |
318 | self.init_reload_doctest() |
|
320 | self.init_reload_doctest() | |
319 | self.init_magics() |
|
321 | self.init_magics() | |
320 | self.init_pdb() |
|
322 | self.init_pdb() | |
321 | self.hooks.late_startup_hook() |
|
323 | self.hooks.late_startup_hook() | |
322 |
|
324 | |||
323 | def cleanup(self): |
|
325 | def cleanup(self): | |
324 | self.restore_sys_module_state() |
|
326 | self.restore_sys_module_state() | |
325 |
|
327 | |||
326 | #------------------------------------------------------------------------- |
|
328 | #------------------------------------------------------------------------- | |
327 | # Traitlet changed handlers |
|
329 | # Traitlet changed handlers | |
328 | #------------------------------------------------------------------------- |
|
330 | #------------------------------------------------------------------------- | |
329 |
|
331 | |||
330 | def _banner1_changed(self): |
|
332 | def _banner1_changed(self): | |
331 | self.compute_banner() |
|
333 | self.compute_banner() | |
332 |
|
334 | |||
333 | def _banner2_changed(self): |
|
335 | def _banner2_changed(self): | |
334 | self.compute_banner() |
|
336 | self.compute_banner() | |
335 |
|
337 | |||
336 | @property |
|
338 | @property | |
337 | def usable_screen_length(self): |
|
339 | def usable_screen_length(self): | |
338 | if self.screen_length == 0: |
|
340 | if self.screen_length == 0: | |
339 | return 0 |
|
341 | return 0 | |
340 | else: |
|
342 | else: | |
341 | num_lines_bot = self.separate_in.count('\n')+1 |
|
343 | num_lines_bot = self.separate_in.count('\n')+1 | |
342 | return self.screen_length - num_lines_bot |
|
344 | return self.screen_length - num_lines_bot | |
343 |
|
345 | |||
344 | def _term_title_changed(self, name, new_value): |
|
346 | def _term_title_changed(self, name, new_value): | |
345 | self.init_term_title() |
|
347 | self.init_term_title() | |
346 |
|
348 | |||
347 | #------------------------------------------------------------------------- |
|
349 | #------------------------------------------------------------------------- | |
348 | # init_* methods called by __init__ |
|
350 | # init_* methods called by __init__ | |
349 | #------------------------------------------------------------------------- |
|
351 | #------------------------------------------------------------------------- | |
350 |
|
352 | |||
351 | def init_ipythondir(self, ipythondir): |
|
353 | def init_ipythondir(self, ipythondir): | |
352 | if ipythondir is not None: |
|
354 | if ipythondir is not None: | |
353 | self.ipythondir = ipythondir |
|
355 | self.ipythondir = ipythondir | |
354 | self.config.IPYTHONDIR = self.ipythondir |
|
356 | self.config.IPYTHONDIR = self.ipythondir | |
355 | return |
|
357 | return | |
356 |
|
358 | |||
357 | if hasattr(self.config, 'IPYTHONDIR'): |
|
359 | if hasattr(self.config, 'IPYTHONDIR'): | |
358 | self.ipythondir = self.config.IPYTHONDIR |
|
360 | self.ipythondir = self.config.IPYTHONDIR | |
359 | if not hasattr(self.config, 'IPYTHONDIR'): |
|
361 | if not hasattr(self.config, 'IPYTHONDIR'): | |
360 | # cdw is always defined |
|
362 | # cdw is always defined | |
361 | self.ipythondir = os.getcwd() |
|
363 | self.ipythondir = os.getcwd() | |
362 |
|
364 | |||
363 | # The caller must make sure that ipythondir exists. We should |
|
365 | # The caller must make sure that ipythondir exists. We should | |
364 | # probably handle this using a Dir traitlet. |
|
366 | # probably handle this using a Dir traitlet. | |
365 | if not os.path.isdir(self.ipythondir): |
|
367 | if not os.path.isdir(self.ipythondir): | |
366 | raise IOError('IPython dir does not exist: %s' % self.ipythondir) |
|
368 | raise IOError('IPython dir does not exist: %s' % self.ipythondir) | |
367 |
|
369 | |||
368 | # All children can just read this |
|
370 | # All children can just read this | |
369 | self.config.IPYTHONDIR = self.ipythondir |
|
371 | self.config.IPYTHONDIR = self.ipythondir | |
370 |
|
372 | |||
371 | def init_instance_attrs(self): |
|
373 | def init_instance_attrs(self): | |
372 | self.jobs = BackgroundJobManager() |
|
374 | self.jobs = BackgroundJobManager() | |
373 | self.more = False |
|
375 | self.more = False | |
374 |
|
376 | |||
375 | # command compiler |
|
377 | # command compiler | |
376 | self.compile = codeop.CommandCompiler() |
|
378 | self.compile = codeop.CommandCompiler() | |
377 |
|
379 | |||
378 | # User input buffer |
|
380 | # User input buffer | |
379 | self.buffer = [] |
|
381 | self.buffer = [] | |
380 |
|
382 | |||
381 | # Make an empty namespace, which extension writers can rely on both |
|
383 | # Make an empty namespace, which extension writers can rely on both | |
382 | # existing and NEVER being used by ipython itself. This gives them a |
|
384 | # existing and NEVER being used by ipython itself. This gives them a | |
383 | # convenient location for storing additional information and state |
|
385 | # convenient location for storing additional information and state | |
384 | # their extensions may require, without fear of collisions with other |
|
386 | # their extensions may require, without fear of collisions with other | |
385 | # ipython names that may develop later. |
|
387 | # ipython names that may develop later. | |
386 | self.meta = Struct() |
|
388 | self.meta = Struct() | |
387 |
|
389 | |||
388 | # Object variable to store code object waiting execution. This is |
|
390 | # Object variable to store code object waiting execution. This is | |
389 | # used mainly by the multithreaded shells, but it can come in handy in |
|
391 | # used mainly by the multithreaded shells, but it can come in handy in | |
390 | # other situations. No need to use a Queue here, since it's a single |
|
392 | # other situations. No need to use a Queue here, since it's a single | |
391 | # item which gets cleared once run. |
|
393 | # item which gets cleared once run. | |
392 | self.code_to_run = None |
|
394 | self.code_to_run = None | |
393 |
|
395 | |||
394 | # Flag to mark unconditional exit |
|
396 | # Flag to mark unconditional exit | |
395 | self.exit_now = False |
|
397 | self.exit_now = False | |
396 |
|
398 | |||
397 | # Temporary files used for various purposes. Deleted at exit. |
|
399 | # Temporary files used for various purposes. Deleted at exit. | |
398 | self.tempfiles = [] |
|
400 | self.tempfiles = [] | |
399 |
|
401 | |||
400 | # Keep track of readline usage (later set by init_readline) |
|
402 | # Keep track of readline usage (later set by init_readline) | |
401 | self.has_readline = False |
|
403 | self.has_readline = False | |
402 |
|
404 | |||
403 | # keep track of where we started running (mainly for crash post-mortem) |
|
405 | # keep track of where we started running (mainly for crash post-mortem) | |
404 | # This is not being used anywhere currently. |
|
406 | # This is not being used anywhere currently. | |
405 | self.starting_dir = os.getcwd() |
|
407 | self.starting_dir = os.getcwd() | |
406 |
|
408 | |||
407 | # Indentation management |
|
409 | # Indentation management | |
408 | self.indent_current_nsp = 0 |
|
410 | self.indent_current_nsp = 0 | |
409 |
|
411 | |||
410 | def init_term_title(self): |
|
412 | def init_term_title(self): | |
411 | # Enable or disable the terminal title. |
|
413 | # Enable or disable the terminal title. | |
412 | if self.term_title: |
|
414 | if self.term_title: | |
413 | toggle_set_term_title(True) |
|
415 | toggle_set_term_title(True) | |
414 | set_term_title('IPython: ' + abbrev_cwd()) |
|
416 | set_term_title('IPython: ' + abbrev_cwd()) | |
415 | else: |
|
417 | else: | |
416 | toggle_set_term_title(False) |
|
418 | toggle_set_term_title(False) | |
417 |
|
419 | |||
418 | def init_usage(self, usage=None): |
|
420 | def init_usage(self, usage=None): | |
419 | if usage is None: |
|
421 | if usage is None: | |
420 | self.usage = interactive_usage |
|
422 | self.usage = interactive_usage | |
421 | else: |
|
423 | else: | |
422 | self.usage = usage |
|
424 | self.usage = usage | |
423 |
|
425 | |||
424 | def init_banner(self, banner1, banner2): |
|
426 | def init_banner(self, banner1, banner2): | |
425 | if self.c: # regular python doesn't print the banner with -c |
|
427 | if self.c: # regular python doesn't print the banner with -c | |
426 | self.display_banner = False |
|
428 | self.display_banner = False | |
427 | if banner1 is not None: |
|
429 | if banner1 is not None: | |
428 | self.banner1 = banner1 |
|
430 | self.banner1 = banner1 | |
429 | if banner2 is not None: |
|
431 | if banner2 is not None: | |
430 | self.banner2 = banner2 |
|
432 | self.banner2 = banner2 | |
431 | self.compute_banner() |
|
433 | self.compute_banner() | |
432 |
|
434 | |||
433 | def compute_banner(self): |
|
435 | def compute_banner(self): | |
434 | self.banner = self.banner1 + '\n' |
|
436 | self.banner = self.banner1 + '\n' | |
435 | if self.profile: |
|
437 | if self.profile: | |
436 | self.banner += '\nIPython profile: %s\n' % self.profile |
|
438 | self.banner += '\nIPython profile: %s\n' % self.profile | |
437 | if self.banner2: |
|
439 | if self.banner2: | |
438 | self.banner += '\n' + self.banner2 + '\n' |
|
440 | self.banner += '\n' + self.banner2 + '\n' | |
439 |
|
441 | |||
440 | def init_create_namespaces(self, user_ns=None, user_global_ns=None): |
|
442 | def init_create_namespaces(self, user_ns=None, user_global_ns=None): | |
441 | # Create the namespace where the user will operate. user_ns is |
|
443 | # Create the namespace where the user will operate. user_ns is | |
442 | # normally the only one used, and it is passed to the exec calls as |
|
444 | # normally the only one used, and it is passed to the exec calls as | |
443 | # the locals argument. But we do carry a user_global_ns namespace |
|
445 | # the locals argument. But we do carry a user_global_ns namespace | |
444 | # given as the exec 'globals' argument, This is useful in embedding |
|
446 | # given as the exec 'globals' argument, This is useful in embedding | |
445 | # situations where the ipython shell opens in a context where the |
|
447 | # situations where the ipython shell opens in a context where the | |
446 | # distinction between locals and globals is meaningful. For |
|
448 | # distinction between locals and globals is meaningful. For | |
447 | # non-embedded contexts, it is just the same object as the user_ns dict. |
|
449 | # non-embedded contexts, it is just the same object as the user_ns dict. | |
448 |
|
450 | |||
449 | # FIXME. For some strange reason, __builtins__ is showing up at user |
|
451 | # FIXME. For some strange reason, __builtins__ is showing up at user | |
450 | # level as a dict instead of a module. This is a manual fix, but I |
|
452 | # level as a dict instead of a module. This is a manual fix, but I | |
451 | # should really track down where the problem is coming from. Alex |
|
453 | # should really track down where the problem is coming from. Alex | |
452 | # Schmolck reported this problem first. |
|
454 | # Schmolck reported this problem first. | |
453 |
|
455 | |||
454 | # A useful post by Alex Martelli on this topic: |
|
456 | # A useful post by Alex Martelli on this topic: | |
455 | # Re: inconsistent value from __builtins__ |
|
457 | # Re: inconsistent value from __builtins__ | |
456 | # Von: Alex Martelli <aleaxit@yahoo.com> |
|
458 | # Von: Alex Martelli <aleaxit@yahoo.com> | |
457 | # Datum: Freitag 01 Oktober 2004 04:45:34 nachmittags/abends |
|
459 | # Datum: Freitag 01 Oktober 2004 04:45:34 nachmittags/abends | |
458 | # Gruppen: comp.lang.python |
|
460 | # Gruppen: comp.lang.python | |
459 |
|
461 | |||
460 | # Michael Hohn <hohn@hooknose.lbl.gov> wrote: |
|
462 | # Michael Hohn <hohn@hooknose.lbl.gov> wrote: | |
461 | # > >>> print type(builtin_check.get_global_binding('__builtins__')) |
|
463 | # > >>> print type(builtin_check.get_global_binding('__builtins__')) | |
462 | # > <type 'dict'> |
|
464 | # > <type 'dict'> | |
463 | # > >>> print type(__builtins__) |
|
465 | # > >>> print type(__builtins__) | |
464 | # > <type 'module'> |
|
466 | # > <type 'module'> | |
465 | # > Is this difference in return value intentional? |
|
467 | # > Is this difference in return value intentional? | |
466 |
|
468 | |||
467 | # Well, it's documented that '__builtins__' can be either a dictionary |
|
469 | # Well, it's documented that '__builtins__' can be either a dictionary | |
468 | # or a module, and it's been that way for a long time. Whether it's |
|
470 | # or a module, and it's been that way for a long time. Whether it's | |
469 | # intentional (or sensible), I don't know. In any case, the idea is |
|
471 | # intentional (or sensible), I don't know. In any case, the idea is | |
470 | # that if you need to access the built-in namespace directly, you |
|
472 | # that if you need to access the built-in namespace directly, you | |
471 | # should start with "import __builtin__" (note, no 's') which will |
|
473 | # should start with "import __builtin__" (note, no 's') which will | |
472 | # definitely give you a module. Yeah, it's somewhat confusing:-(. |
|
474 | # definitely give you a module. Yeah, it's somewhat confusing:-(. | |
473 |
|
475 | |||
474 | # These routines return properly built dicts as needed by the rest of |
|
476 | # These routines return properly built dicts as needed by the rest of | |
475 | # the code, and can also be used by extension writers to generate |
|
477 | # the code, and can also be used by extension writers to generate | |
476 | # properly initialized namespaces. |
|
478 | # properly initialized namespaces. | |
477 | user_ns, user_global_ns = self.make_user_namespaces(user_ns, |
|
479 | user_ns, user_global_ns = self.make_user_namespaces(user_ns, | |
478 | user_global_ns) |
|
480 | user_global_ns) | |
479 |
|
481 | |||
480 | # Assign namespaces |
|
482 | # Assign namespaces | |
481 | # This is the namespace where all normal user variables live |
|
483 | # This is the namespace where all normal user variables live | |
482 | self.user_ns = user_ns |
|
484 | self.user_ns = user_ns | |
483 | self.user_global_ns = user_global_ns |
|
485 | self.user_global_ns = user_global_ns | |
484 |
|
486 | |||
485 | # An auxiliary namespace that checks what parts of the user_ns were |
|
487 | # An auxiliary namespace that checks what parts of the user_ns were | |
486 | # loaded at startup, so we can list later only variables defined in |
|
488 | # loaded at startup, so we can list later only variables defined in | |
487 | # actual interactive use. Since it is always a subset of user_ns, it |
|
489 | # actual interactive use. Since it is always a subset of user_ns, it | |
488 | # doesn't need to be seaparately tracked in the ns_table |
|
490 | # doesn't need to be seaparately tracked in the ns_table | |
489 | self.user_config_ns = {} |
|
491 | self.user_config_ns = {} | |
490 |
|
492 | |||
491 | # A namespace to keep track of internal data structures to prevent |
|
493 | # A namespace to keep track of internal data structures to prevent | |
492 | # them from cluttering user-visible stuff. Will be updated later |
|
494 | # them from cluttering user-visible stuff. Will be updated later | |
493 | self.internal_ns = {} |
|
495 | self.internal_ns = {} | |
494 |
|
496 | |||
495 | # Namespace of system aliases. Each entry in the alias |
|
497 | # Namespace of system aliases. Each entry in the alias | |
496 | # table must be a 2-tuple of the form (N,name), where N is the number |
|
498 | # table must be a 2-tuple of the form (N,name), where N is the number | |
497 | # of positional arguments of the alias. |
|
499 | # of positional arguments of the alias. | |
498 | self.alias_table = {} |
|
500 | self.alias_table = {} | |
499 |
|
501 | |||
500 | # Now that FakeModule produces a real module, we've run into a nasty |
|
502 | # Now that FakeModule produces a real module, we've run into a nasty | |
501 | # problem: after script execution (via %run), the module where the user |
|
503 | # problem: after script execution (via %run), the module where the user | |
502 | # code ran is deleted. Now that this object is a true module (needed |
|
504 | # code ran is deleted. Now that this object is a true module (needed | |
503 | # so docetst and other tools work correctly), the Python module |
|
505 | # so docetst and other tools work correctly), the Python module | |
504 | # teardown mechanism runs over it, and sets to None every variable |
|
506 | # teardown mechanism runs over it, and sets to None every variable | |
505 | # present in that module. Top-level references to objects from the |
|
507 | # present in that module. Top-level references to objects from the | |
506 | # script survive, because the user_ns is updated with them. However, |
|
508 | # script survive, because the user_ns is updated with them. However, | |
507 | # calling functions defined in the script that use other things from |
|
509 | # calling functions defined in the script that use other things from | |
508 | # the script will fail, because the function's closure had references |
|
510 | # the script will fail, because the function's closure had references | |
509 | # to the original objects, which are now all None. So we must protect |
|
511 | # to the original objects, which are now all None. So we must protect | |
510 | # these modules from deletion by keeping a cache. |
|
512 | # these modules from deletion by keeping a cache. | |
511 | # |
|
513 | # | |
512 | # To avoid keeping stale modules around (we only need the one from the |
|
514 | # To avoid keeping stale modules around (we only need the one from the | |
513 | # last run), we use a dict keyed with the full path to the script, so |
|
515 | # last run), we use a dict keyed with the full path to the script, so | |
514 | # only the last version of the module is held in the cache. Note, |
|
516 | # only the last version of the module is held in the cache. Note, | |
515 | # however, that we must cache the module *namespace contents* (their |
|
517 | # however, that we must cache the module *namespace contents* (their | |
516 | # __dict__). Because if we try to cache the actual modules, old ones |
|
518 | # __dict__). Because if we try to cache the actual modules, old ones | |
517 | # (uncached) could be destroyed while still holding references (such as |
|
519 | # (uncached) could be destroyed while still holding references (such as | |
518 | # those held by GUI objects that tend to be long-lived)> |
|
520 | # those held by GUI objects that tend to be long-lived)> | |
519 | # |
|
521 | # | |
520 | # The %reset command will flush this cache. See the cache_main_mod() |
|
522 | # The %reset command will flush this cache. See the cache_main_mod() | |
521 | # and clear_main_mod_cache() methods for details on use. |
|
523 | # and clear_main_mod_cache() methods for details on use. | |
522 |
|
524 | |||
523 | # This is the cache used for 'main' namespaces |
|
525 | # This is the cache used for 'main' namespaces | |
524 | self._main_ns_cache = {} |
|
526 | self._main_ns_cache = {} | |
525 | # And this is the single instance of FakeModule whose __dict__ we keep |
|
527 | # And this is the single instance of FakeModule whose __dict__ we keep | |
526 | # copying and clearing for reuse on each %run |
|
528 | # copying and clearing for reuse on each %run | |
527 | self._user_main_module = FakeModule() |
|
529 | self._user_main_module = FakeModule() | |
528 |
|
530 | |||
529 | # A table holding all the namespaces IPython deals with, so that |
|
531 | # A table holding all the namespaces IPython deals with, so that | |
530 | # introspection facilities can search easily. |
|
532 | # introspection facilities can search easily. | |
531 | self.ns_table = {'user':user_ns, |
|
533 | self.ns_table = {'user':user_ns, | |
532 | 'user_global':user_global_ns, |
|
534 | 'user_global':user_global_ns, | |
533 | 'alias':self.alias_table, |
|
535 | 'alias':self.alias_table, | |
534 | 'internal':self.internal_ns, |
|
536 | 'internal':self.internal_ns, | |
535 | 'builtin':__builtin__.__dict__ |
|
537 | 'builtin':__builtin__.__dict__ | |
536 | } |
|
538 | } | |
537 |
|
539 | |||
538 | # Similarly, track all namespaces where references can be held and that |
|
540 | # Similarly, track all namespaces where references can be held and that | |
539 | # we can safely clear (so it can NOT include builtin). This one can be |
|
541 | # we can safely clear (so it can NOT include builtin). This one can be | |
540 | # a simple list. |
|
542 | # a simple list. | |
541 | self.ns_refs_table = [ user_ns, user_global_ns, self.user_config_ns, |
|
543 | self.ns_refs_table = [ user_ns, user_global_ns, self.user_config_ns, | |
542 | self.alias_table, self.internal_ns, |
|
544 | self.alias_table, self.internal_ns, | |
543 | self._main_ns_cache ] |
|
545 | self._main_ns_cache ] | |
544 |
|
546 | |||
545 | def init_sys_modules(self): |
|
547 | def init_sys_modules(self): | |
546 | # We need to insert into sys.modules something that looks like a |
|
548 | # We need to insert into sys.modules something that looks like a | |
547 | # module but which accesses the IPython namespace, for shelve and |
|
549 | # module but which accesses the IPython namespace, for shelve and | |
548 | # pickle to work interactively. Normally they rely on getting |
|
550 | # pickle to work interactively. Normally they rely on getting | |
549 | # everything out of __main__, but for embedding purposes each IPython |
|
551 | # everything out of __main__, but for embedding purposes each IPython | |
550 | # instance has its own private namespace, so we can't go shoving |
|
552 | # instance has its own private namespace, so we can't go shoving | |
551 | # everything into __main__. |
|
553 | # everything into __main__. | |
552 |
|
554 | |||
553 | # note, however, that we should only do this for non-embedded |
|
555 | # note, however, that we should only do this for non-embedded | |
554 | # ipythons, which really mimic the __main__.__dict__ with their own |
|
556 | # ipythons, which really mimic the __main__.__dict__ with their own | |
555 | # namespace. Embedded instances, on the other hand, should not do |
|
557 | # namespace. Embedded instances, on the other hand, should not do | |
556 | # this because they need to manage the user local/global namespaces |
|
558 | # this because they need to manage the user local/global namespaces | |
557 | # only, but they live within a 'normal' __main__ (meaning, they |
|
559 | # only, but they live within a 'normal' __main__ (meaning, they | |
558 | # shouldn't overtake the execution environment of the script they're |
|
560 | # shouldn't overtake the execution environment of the script they're | |
559 | # embedded in). |
|
561 | # embedded in). | |
560 |
|
562 | |||
561 | # This is overridden in the InteractiveShellEmbed subclass to a no-op. |
|
563 | # This is overridden in the InteractiveShellEmbed subclass to a no-op. | |
562 |
|
564 | |||
563 | try: |
|
565 | try: | |
564 | main_name = self.user_ns['__name__'] |
|
566 | main_name = self.user_ns['__name__'] | |
565 | except KeyError: |
|
567 | except KeyError: | |
566 | raise KeyError('user_ns dictionary MUST have a "__name__" key') |
|
568 | raise KeyError('user_ns dictionary MUST have a "__name__" key') | |
567 | else: |
|
569 | else: | |
568 | sys.modules[main_name] = FakeModule(self.user_ns) |
|
570 | sys.modules[main_name] = FakeModule(self.user_ns) | |
569 |
|
571 | |||
570 | def make_user_namespaces(self, user_ns=None, user_global_ns=None): |
|
572 | def make_user_namespaces(self, user_ns=None, user_global_ns=None): | |
571 | """Return a valid local and global user interactive namespaces. |
|
573 | """Return a valid local and global user interactive namespaces. | |
572 |
|
574 | |||
573 | This builds a dict with the minimal information needed to operate as a |
|
575 | This builds a dict with the minimal information needed to operate as a | |
574 | valid IPython user namespace, which you can pass to the various |
|
576 | valid IPython user namespace, which you can pass to the various | |
575 | embedding classes in ipython. The default implementation returns the |
|
577 | embedding classes in ipython. The default implementation returns the | |
576 | same dict for both the locals and the globals to allow functions to |
|
578 | same dict for both the locals and the globals to allow functions to | |
577 | refer to variables in the namespace. Customized implementations can |
|
579 | refer to variables in the namespace. Customized implementations can | |
578 | return different dicts. The locals dictionary can actually be anything |
|
580 | return different dicts. The locals dictionary can actually be anything | |
579 | following the basic mapping protocol of a dict, but the globals dict |
|
581 | following the basic mapping protocol of a dict, but the globals dict | |
580 | must be a true dict, not even a subclass. It is recommended that any |
|
582 | must be a true dict, not even a subclass. It is recommended that any | |
581 | custom object for the locals namespace synchronize with the globals |
|
583 | custom object for the locals namespace synchronize with the globals | |
582 | dict somehow. |
|
584 | dict somehow. | |
583 |
|
585 | |||
584 | Raises TypeError if the provided globals namespace is not a true dict. |
|
586 | Raises TypeError if the provided globals namespace is not a true dict. | |
585 |
|
587 | |||
586 | :Parameters: |
|
588 | :Parameters: | |
587 | user_ns : dict-like, optional |
|
589 | user_ns : dict-like, optional | |
588 | The current user namespace. The items in this namespace should |
|
590 | The current user namespace. The items in this namespace should | |
589 | be included in the output. If None, an appropriate blank |
|
591 | be included in the output. If None, an appropriate blank | |
590 | namespace should be created. |
|
592 | namespace should be created. | |
591 | user_global_ns : dict, optional |
|
593 | user_global_ns : dict, optional | |
592 | The current user global namespace. The items in this namespace |
|
594 | The current user global namespace. The items in this namespace | |
593 | should be included in the output. If None, an appropriate |
|
595 | should be included in the output. If None, an appropriate | |
594 | blank namespace should be created. |
|
596 | blank namespace should be created. | |
595 |
|
597 | |||
596 | :Returns: |
|
598 | :Returns: | |
597 | A tuple pair of dictionary-like object to be used as the local namespace |
|
599 | A tuple pair of dictionary-like object to be used as the local namespace | |
598 | of the interpreter and a dict to be used as the global namespace. |
|
600 | of the interpreter and a dict to be used as the global namespace. | |
599 | """ |
|
601 | """ | |
600 |
|
602 | |||
601 | if user_ns is None: |
|
603 | if user_ns is None: | |
602 | # Set __name__ to __main__ to better match the behavior of the |
|
604 | # Set __name__ to __main__ to better match the behavior of the | |
603 | # normal interpreter. |
|
605 | # normal interpreter. | |
604 | user_ns = {'__name__' :'__main__', |
|
606 | user_ns = {'__name__' :'__main__', | |
605 | '__builtins__' : __builtin__, |
|
607 | '__builtins__' : __builtin__, | |
606 | } |
|
608 | } | |
607 | else: |
|
609 | else: | |
608 | user_ns.setdefault('__name__','__main__') |
|
610 | user_ns.setdefault('__name__','__main__') | |
609 | user_ns.setdefault('__builtins__',__builtin__) |
|
611 | user_ns.setdefault('__builtins__',__builtin__) | |
610 |
|
612 | |||
611 | if user_global_ns is None: |
|
613 | if user_global_ns is None: | |
612 | user_global_ns = user_ns |
|
614 | user_global_ns = user_ns | |
613 | if type(user_global_ns) is not dict: |
|
615 | if type(user_global_ns) is not dict: | |
614 | raise TypeError("user_global_ns must be a true dict; got %r" |
|
616 | raise TypeError("user_global_ns must be a true dict; got %r" | |
615 | % type(user_global_ns)) |
|
617 | % type(user_global_ns)) | |
616 |
|
618 | |||
617 | return user_ns, user_global_ns |
|
619 | return user_ns, user_global_ns | |
618 |
|
620 | |||
619 | def init_history(self): |
|
621 | def init_history(self): | |
620 | # List of input with multi-line handling. |
|
622 | # List of input with multi-line handling. | |
621 | self.input_hist = InputList() |
|
623 | self.input_hist = InputList() | |
622 | # This one will hold the 'raw' input history, without any |
|
624 | # This one will hold the 'raw' input history, without any | |
623 | # pre-processing. This will allow users to retrieve the input just as |
|
625 | # pre-processing. This will allow users to retrieve the input just as | |
624 | # it was exactly typed in by the user, with %hist -r. |
|
626 | # it was exactly typed in by the user, with %hist -r. | |
625 | self.input_hist_raw = InputList() |
|
627 | self.input_hist_raw = InputList() | |
626 |
|
628 | |||
627 | # list of visited directories |
|
629 | # list of visited directories | |
628 | try: |
|
630 | try: | |
629 | self.dir_hist = [os.getcwd()] |
|
631 | self.dir_hist = [os.getcwd()] | |
630 | except OSError: |
|
632 | except OSError: | |
631 | self.dir_hist = [] |
|
633 | self.dir_hist = [] | |
632 |
|
634 | |||
633 | # dict of output history |
|
635 | # dict of output history | |
634 | self.output_hist = {} |
|
636 | self.output_hist = {} | |
635 |
|
637 | |||
636 | # Now the history file |
|
638 | # Now the history file | |
637 | try: |
|
639 | try: | |
638 | histfname = 'history-%s' % self.profile |
|
640 | histfname = 'history-%s' % self.profile | |
639 | except AttributeError: |
|
641 | except AttributeError: | |
640 | histfname = 'history' |
|
642 | histfname = 'history' | |
641 | self.histfile = os.path.join(self.config.IPYTHONDIR, histfname) |
|
643 | self.histfile = os.path.join(self.config.IPYTHONDIR, histfname) | |
642 |
|
644 | |||
643 | # Fill the history zero entry, user counter starts at 1 |
|
645 | # Fill the history zero entry, user counter starts at 1 | |
644 | self.input_hist.append('\n') |
|
646 | self.input_hist.append('\n') | |
645 | self.input_hist_raw.append('\n') |
|
647 | self.input_hist_raw.append('\n') | |
646 |
|
648 | |||
647 | def init_encoding(self): |
|
649 | def init_encoding(self): | |
648 | # Get system encoding at startup time. Certain terminals (like Emacs |
|
650 | # Get system encoding at startup time. Certain terminals (like Emacs | |
649 | # under Win32 have it set to None, and we need to have a known valid |
|
651 | # under Win32 have it set to None, and we need to have a known valid | |
650 | # encoding to use in the raw_input() method |
|
652 | # encoding to use in the raw_input() method | |
651 | try: |
|
653 | try: | |
652 | self.stdin_encoding = sys.stdin.encoding or 'ascii' |
|
654 | self.stdin_encoding = sys.stdin.encoding or 'ascii' | |
653 | except AttributeError: |
|
655 | except AttributeError: | |
654 | self.stdin_encoding = 'ascii' |
|
656 | self.stdin_encoding = 'ascii' | |
655 |
|
657 | |||
656 | def init_handlers(self): |
|
658 | def init_handlers(self): | |
657 | # escapes for automatic behavior on the command line |
|
659 | # escapes for automatic behavior on the command line | |
658 | self.ESC_SHELL = '!' |
|
660 | self.ESC_SHELL = '!' | |
659 | self.ESC_SH_CAP = '!!' |
|
661 | self.ESC_SH_CAP = '!!' | |
660 | self.ESC_HELP = '?' |
|
662 | self.ESC_HELP = '?' | |
661 | self.ESC_MAGIC = '%' |
|
663 | self.ESC_MAGIC = '%' | |
662 | self.ESC_QUOTE = ',' |
|
664 | self.ESC_QUOTE = ',' | |
663 | self.ESC_QUOTE2 = ';' |
|
665 | self.ESC_QUOTE2 = ';' | |
664 | self.ESC_PAREN = '/' |
|
666 | self.ESC_PAREN = '/' | |
665 |
|
667 | |||
666 | # And their associated handlers |
|
668 | # And their associated handlers | |
667 | self.esc_handlers = {self.ESC_PAREN : self.handle_auto, |
|
669 | self.esc_handlers = {self.ESC_PAREN : self.handle_auto, | |
668 | self.ESC_QUOTE : self.handle_auto, |
|
670 | self.ESC_QUOTE : self.handle_auto, | |
669 | self.ESC_QUOTE2 : self.handle_auto, |
|
671 | self.ESC_QUOTE2 : self.handle_auto, | |
670 | self.ESC_MAGIC : self.handle_magic, |
|
672 | self.ESC_MAGIC : self.handle_magic, | |
671 | self.ESC_HELP : self.handle_help, |
|
673 | self.ESC_HELP : self.handle_help, | |
672 | self.ESC_SHELL : self.handle_shell_escape, |
|
674 | self.ESC_SHELL : self.handle_shell_escape, | |
673 | self.ESC_SH_CAP : self.handle_shell_escape, |
|
675 | self.ESC_SH_CAP : self.handle_shell_escape, | |
674 | } |
|
676 | } | |
675 |
|
677 | |||
676 | def init_syntax_highlighting(self): |
|
678 | def init_syntax_highlighting(self): | |
677 | # Python source parser/formatter for syntax highlighting |
|
679 | # Python source parser/formatter for syntax highlighting | |
678 | pyformat = PyColorize.Parser().format |
|
680 | pyformat = PyColorize.Parser().format | |
679 | self.pycolorize = lambda src: pyformat(src,'str',self.colors) |
|
681 | self.pycolorize = lambda src: pyformat(src,'str',self.colors) | |
680 |
|
682 | |||
681 | def init_hooks(self): |
|
683 | def init_hooks(self): | |
682 | # hooks holds pointers used for user-side customizations |
|
684 | # hooks holds pointers used for user-side customizations | |
683 | self.hooks = Struct() |
|
685 | self.hooks = Struct() | |
684 |
|
686 | |||
685 | self.strdispatchers = {} |
|
687 | self.strdispatchers = {} | |
686 |
|
688 | |||
687 | # Set all default hooks, defined in the IPython.hooks module. |
|
689 | # Set all default hooks, defined in the IPython.hooks module. | |
688 | import IPython.core.hooks |
|
690 | import IPython.core.hooks | |
689 | hooks = IPython.core.hooks |
|
691 | hooks = IPython.core.hooks | |
690 | for hook_name in hooks.__all__: |
|
692 | for hook_name in hooks.__all__: | |
691 | # default hooks have priority 100, i.e. low; user hooks should have |
|
693 | # default hooks have priority 100, i.e. low; user hooks should have | |
692 | # 0-100 priority |
|
694 | # 0-100 priority | |
693 | self.set_hook(hook_name,getattr(hooks,hook_name), 100) |
|
695 | self.set_hook(hook_name,getattr(hooks,hook_name), 100) | |
694 |
|
696 | |||
695 | def init_pushd_popd_magic(self): |
|
697 | def init_pushd_popd_magic(self): | |
696 | # for pushd/popd management |
|
698 | # for pushd/popd management | |
697 | try: |
|
699 | try: | |
698 | self.home_dir = get_home_dir() |
|
700 | self.home_dir = get_home_dir() | |
699 | except HomeDirError, msg: |
|
701 | except HomeDirError, msg: | |
700 | fatal(msg) |
|
702 | fatal(msg) | |
701 |
|
703 | |||
702 | self.dir_stack = [] |
|
704 | self.dir_stack = [] | |
703 |
|
705 | |||
704 | def init_traceback_handlers(self, custom_exceptions): |
|
706 | def init_traceback_handlers(self, custom_exceptions): | |
705 | # Syntax error handler. |
|
707 | # Syntax error handler. | |
706 | self.SyntaxTB = SyntaxTB(color_scheme='NoColor') |
|
708 | self.SyntaxTB = SyntaxTB(color_scheme='NoColor') | |
707 |
|
709 | |||
708 | # The interactive one is initialized with an offset, meaning we always |
|
710 | # The interactive one is initialized with an offset, meaning we always | |
709 | # want to remove the topmost item in the traceback, which is our own |
|
711 | # want to remove the topmost item in the traceback, which is our own | |
710 | # internal code. Valid modes: ['Plain','Context','Verbose'] |
|
712 | # internal code. Valid modes: ['Plain','Context','Verbose'] | |
711 | self.InteractiveTB = ultratb.AutoFormattedTB(mode = 'Plain', |
|
713 | self.InteractiveTB = ultratb.AutoFormattedTB(mode = 'Plain', | |
712 | color_scheme='NoColor', |
|
714 | color_scheme='NoColor', | |
713 | tb_offset = 1) |
|
715 | tb_offset = 1) | |
714 |
|
716 | |||
715 | # IPython itself shouldn't crash. This will produce a detailed |
|
717 | # IPython itself shouldn't crash. This will produce a detailed | |
716 | # post-mortem if it does. But we only install the crash handler for |
|
718 | # post-mortem if it does. But we only install the crash handler for | |
717 | # non-threaded shells, the threaded ones use a normal verbose reporter |
|
719 | # non-threaded shells, the threaded ones use a normal verbose reporter | |
718 | # and lose the crash handler. This is because exceptions in the main |
|
720 | # and lose the crash handler. This is because exceptions in the main | |
719 | # thread (such as in GUI code) propagate directly to sys.excepthook, |
|
721 | # thread (such as in GUI code) propagate directly to sys.excepthook, | |
720 | # and there's no point in printing crash dumps for every user exception. |
|
722 | # and there's no point in printing crash dumps for every user exception. | |
721 | if self.isthreaded: |
|
723 | if self.isthreaded: | |
722 | ipCrashHandler = ultratb.FormattedTB() |
|
724 | ipCrashHandler = ultratb.FormattedTB() | |
723 | else: |
|
725 | else: | |
724 | from IPython.core import crashhandler |
|
726 | from IPython.core import crashhandler | |
725 | ipCrashHandler = crashhandler.IPythonCrashHandler(self) |
|
727 | ipCrashHandler = crashhandler.IPythonCrashHandler(self) | |
726 | self.set_crash_handler(ipCrashHandler) |
|
728 | self.set_crash_handler(ipCrashHandler) | |
727 |
|
729 | |||
728 | # and add any custom exception handlers the user may have specified |
|
730 | # and add any custom exception handlers the user may have specified | |
729 | self.set_custom_exc(*custom_exceptions) |
|
731 | self.set_custom_exc(*custom_exceptions) | |
730 |
|
732 | |||
731 | def init_logger(self): |
|
733 | def init_logger(self): | |
732 | self.logger = Logger(self, logfname='ipython_log.py', logmode='rotate') |
|
734 | self.logger = Logger(self, logfname='ipython_log.py', logmode='rotate') | |
733 | # local shortcut, this is used a LOT |
|
735 | # local shortcut, this is used a LOT | |
734 | self.log = self.logger.log |
|
736 | self.log = self.logger.log | |
735 | # template for logfile headers. It gets resolved at runtime by the |
|
737 | # template for logfile headers. It gets resolved at runtime by the | |
736 | # logstart method. |
|
738 | # logstart method. | |
737 | self.loghead_tpl = \ |
|
739 | self.loghead_tpl = \ | |
738 | """#log# Automatic Logger file. *** THIS MUST BE THE FIRST LINE *** |
|
740 | """#log# Automatic Logger file. *** THIS MUST BE THE FIRST LINE *** | |
739 | #log# DO NOT CHANGE THIS LINE OR THE TWO BELOW |
|
741 | #log# DO NOT CHANGE THIS LINE OR THE TWO BELOW | |
740 | #log# opts = %s |
|
742 | #log# opts = %s | |
741 | #log# args = %s |
|
743 | #log# args = %s | |
742 | #log# It is safe to make manual edits below here. |
|
744 | #log# It is safe to make manual edits below here. | |
743 | #log#----------------------------------------------------------------------- |
|
745 | #log#----------------------------------------------------------------------- | |
744 | """ |
|
746 | """ | |
745 |
|
747 | |||
746 | def init_logstart(self): |
|
748 | def init_logstart(self): | |
747 | if self.logplay: |
|
749 | if self.logplay: | |
748 | self.magic_logstart(self.logplay + ' append') |
|
750 | self.magic_logstart(self.logplay + ' append') | |
749 | elif self.logfile: |
|
751 | elif self.logfile: | |
750 | self.magic_logstart(self.logfile) |
|
752 | self.magic_logstart(self.logfile) | |
751 | elif self.logstart: |
|
753 | elif self.logstart: | |
752 | self.magic_logstart() |
|
754 | self.magic_logstart() | |
753 |
|
755 | |||
754 | def init_aliases(self): |
|
756 | def init_aliases(self): | |
755 | # dict of things NOT to alias (keywords, builtins and some magics) |
|
757 | # dict of things NOT to alias (keywords, builtins and some magics) | |
756 | no_alias = {} |
|
758 | no_alias = {} | |
757 | no_alias_magics = ['cd','popd','pushd','dhist','alias','unalias'] |
|
759 | no_alias_magics = ['cd','popd','pushd','dhist','alias','unalias'] | |
758 | for key in keyword.kwlist + no_alias_magics: |
|
760 | for key in keyword.kwlist + no_alias_magics: | |
759 | no_alias[key] = 1 |
|
761 | no_alias[key] = 1 | |
760 | no_alias.update(__builtin__.__dict__) |
|
762 | no_alias.update(__builtin__.__dict__) | |
761 | self.no_alias = no_alias |
|
763 | self.no_alias = no_alias | |
762 |
|
764 | |||
763 | # Make some aliases automatically |
|
765 | # Make some aliases automatically | |
764 | # Prepare list of shell aliases to auto-define |
|
766 | # Prepare list of shell aliases to auto-define | |
765 | if os.name == 'posix': |
|
767 | if os.name == 'posix': | |
766 | auto_alias = ('mkdir mkdir', 'rmdir rmdir', |
|
768 | auto_alias = ('mkdir mkdir', 'rmdir rmdir', | |
767 | 'mv mv -i','rm rm -i','cp cp -i', |
|
769 | 'mv mv -i','rm rm -i','cp cp -i', | |
768 | 'cat cat','less less','clear clear', |
|
770 | 'cat cat','less less','clear clear', | |
769 | # a better ls |
|
771 | # a better ls | |
770 | 'ls ls -F', |
|
772 | 'ls ls -F', | |
771 | # long ls |
|
773 | # long ls | |
772 | 'll ls -lF') |
|
774 | 'll ls -lF') | |
773 | # Extra ls aliases with color, which need special treatment on BSD |
|
775 | # Extra ls aliases with color, which need special treatment on BSD | |
774 | # variants |
|
776 | # variants | |
775 | ls_extra = ( # color ls |
|
777 | ls_extra = ( # color ls | |
776 | 'lc ls -F -o --color', |
|
778 | 'lc ls -F -o --color', | |
777 | # ls normal files only |
|
779 | # ls normal files only | |
778 | 'lf ls -F -o --color %l | grep ^-', |
|
780 | 'lf ls -F -o --color %l | grep ^-', | |
779 | # ls symbolic links |
|
781 | # ls symbolic links | |
780 | 'lk ls -F -o --color %l | grep ^l', |
|
782 | 'lk ls -F -o --color %l | grep ^l', | |
781 | # directories or links to directories, |
|
783 | # directories or links to directories, | |
782 | 'ldir ls -F -o --color %l | grep /$', |
|
784 | 'ldir ls -F -o --color %l | grep /$', | |
783 | # things which are executable |
|
785 | # things which are executable | |
784 | 'lx ls -F -o --color %l | grep ^-..x', |
|
786 | 'lx ls -F -o --color %l | grep ^-..x', | |
785 | ) |
|
787 | ) | |
786 | # The BSDs don't ship GNU ls, so they don't understand the |
|
788 | # The BSDs don't ship GNU ls, so they don't understand the | |
787 | # --color switch out of the box |
|
789 | # --color switch out of the box | |
788 | if 'bsd' in sys.platform: |
|
790 | if 'bsd' in sys.platform: | |
789 | ls_extra = ( # ls normal files only |
|
791 | ls_extra = ( # ls normal files only | |
790 | 'lf ls -lF | grep ^-', |
|
792 | 'lf ls -lF | grep ^-', | |
791 | # ls symbolic links |
|
793 | # ls symbolic links | |
792 | 'lk ls -lF | grep ^l', |
|
794 | 'lk ls -lF | grep ^l', | |
793 | # directories or links to directories, |
|
795 | # directories or links to directories, | |
794 | 'ldir ls -lF | grep /$', |
|
796 | 'ldir ls -lF | grep /$', | |
795 | # things which are executable |
|
797 | # things which are executable | |
796 | 'lx ls -lF | grep ^-..x', |
|
798 | 'lx ls -lF | grep ^-..x', | |
797 | ) |
|
799 | ) | |
798 | auto_alias = auto_alias + ls_extra |
|
800 | auto_alias = auto_alias + ls_extra | |
799 | elif os.name in ['nt','dos']: |
|
801 | elif os.name in ['nt','dos']: | |
800 | auto_alias = ('ls dir /on', |
|
802 | auto_alias = ('ls dir /on', | |
801 | 'ddir dir /ad /on', 'ldir dir /ad /on', |
|
803 | 'ddir dir /ad /on', 'ldir dir /ad /on', | |
802 | 'mkdir mkdir','rmdir rmdir','echo echo', |
|
804 | 'mkdir mkdir','rmdir rmdir','echo echo', | |
803 | 'ren ren','cls cls','copy copy') |
|
805 | 'ren ren','cls cls','copy copy') | |
804 | else: |
|
806 | else: | |
805 | auto_alias = () |
|
807 | auto_alias = () | |
806 | self.auto_alias = [s.split(None,1) for s in auto_alias] |
|
808 | self.auto_alias = [s.split(None,1) for s in auto_alias] | |
807 |
|
809 | |||
808 | # Load default aliases |
|
810 | # Load default aliases | |
809 | for alias, cmd in self.auto_alias: |
|
811 | for alias, cmd in self.auto_alias: | |
810 | self.define_alias(alias,cmd) |
|
812 | self.define_alias(alias,cmd) | |
811 |
|
813 | |||
812 | # Load user aliases |
|
814 | # Load user aliases | |
813 | for alias in self.alias: |
|
815 | for alias in self.alias: | |
814 | self.magic_alias(alias) |
|
816 | self.magic_alias(alias) | |
815 |
|
817 | |||
816 | def init_builtins(self): |
|
818 | def init_builtins(self): | |
817 | self.builtin_trap = BuiltinTrap(self) |
|
819 | self.builtin_trap = BuiltinTrap(self) | |
818 |
|
820 | |||
819 | def init_shadow_hist(self): |
|
821 | def init_shadow_hist(self): | |
820 | try: |
|
822 | try: | |
821 | self.db = pickleshare.PickleShareDB(self.config.IPYTHONDIR + "/db") |
|
823 | self.db = pickleshare.PickleShareDB(self.config.IPYTHONDIR + "/db") | |
822 | except exceptions.UnicodeDecodeError: |
|
824 | except exceptions.UnicodeDecodeError: | |
823 | print "Your ipythondir can't be decoded to unicode!" |
|
825 | print "Your ipythondir can't be decoded to unicode!" | |
824 | print "Please set HOME environment variable to something that" |
|
826 | print "Please set HOME environment variable to something that" | |
825 | print r"only has ASCII characters, e.g. c:\home" |
|
827 | print r"only has ASCII characters, e.g. c:\home" | |
826 | print "Now it is", self.config.IPYTHONDIR |
|
828 | print "Now it is", self.config.IPYTHONDIR | |
827 | sys.exit() |
|
829 | sys.exit() | |
828 | self.shadowhist = ipcorehist.ShadowHist(self.db) |
|
830 | self.shadowhist = ipcorehist.ShadowHist(self.db) | |
829 |
|
831 | |||
830 | def init_inspector(self): |
|
832 | def init_inspector(self): | |
831 | # Object inspector |
|
833 | # Object inspector | |
832 | self.inspector = oinspect.Inspector(oinspect.InspectColors, |
|
834 | self.inspector = oinspect.Inspector(oinspect.InspectColors, | |
833 | PyColorize.ANSICodeColors, |
|
835 | PyColorize.ANSICodeColors, | |
834 | 'NoColor', |
|
836 | 'NoColor', | |
835 | self.object_info_string_level) |
|
837 | self.object_info_string_level) | |
836 |
|
838 | |||
837 | def init_readline(self): |
|
839 | def init_readline(self): | |
838 | """Command history completion/saving/reloading.""" |
|
840 | """Command history completion/saving/reloading.""" | |
839 |
|
841 | |||
840 | self.rl_next_input = None |
|
842 | self.rl_next_input = None | |
841 | self.rl_do_indent = False |
|
843 | self.rl_do_indent = False | |
842 |
|
844 | |||
843 | if not self.readline_use: |
|
845 | if not self.readline_use: | |
844 | return |
|
846 | return | |
845 |
|
847 | |||
846 | import IPython.utils.rlineimpl as readline |
|
848 | import IPython.utils.rlineimpl as readline | |
847 |
|
849 | |||
848 | if not readline.have_readline: |
|
850 | if not readline.have_readline: | |
849 | self.has_readline = 0 |
|
851 | self.has_readline = 0 | |
850 | self.readline = None |
|
852 | self.readline = None | |
851 | # no point in bugging windows users with this every time: |
|
853 | # no point in bugging windows users with this every time: | |
852 | warn('Readline services not available on this platform.') |
|
854 | warn('Readline services not available on this platform.') | |
853 | else: |
|
855 | else: | |
854 | sys.modules['readline'] = readline |
|
856 | sys.modules['readline'] = readline | |
855 | import atexit |
|
857 | import atexit | |
856 | from IPython.core.completer import IPCompleter |
|
858 | from IPython.core.completer import IPCompleter | |
857 | self.Completer = IPCompleter(self, |
|
859 | self.Completer = IPCompleter(self, | |
858 | self.user_ns, |
|
860 | self.user_ns, | |
859 | self.user_global_ns, |
|
861 | self.user_global_ns, | |
860 | self.readline_omit__names, |
|
862 | self.readline_omit__names, | |
861 | self.alias_table) |
|
863 | self.alias_table) | |
862 | sdisp = self.strdispatchers.get('complete_command', StrDispatch()) |
|
864 | sdisp = self.strdispatchers.get('complete_command', StrDispatch()) | |
863 | self.strdispatchers['complete_command'] = sdisp |
|
865 | self.strdispatchers['complete_command'] = sdisp | |
864 | self.Completer.custom_completers = sdisp |
|
866 | self.Completer.custom_completers = sdisp | |
865 | # Platform-specific configuration |
|
867 | # Platform-specific configuration | |
866 | if os.name == 'nt': |
|
868 | if os.name == 'nt': | |
867 | self.readline_startup_hook = readline.set_pre_input_hook |
|
869 | self.readline_startup_hook = readline.set_pre_input_hook | |
868 | else: |
|
870 | else: | |
869 | self.readline_startup_hook = readline.set_startup_hook |
|
871 | self.readline_startup_hook = readline.set_startup_hook | |
870 |
|
872 | |||
871 | # Load user's initrc file (readline config) |
|
873 | # Load user's initrc file (readline config) | |
872 | # Or if libedit is used, load editrc. |
|
874 | # Or if libedit is used, load editrc. | |
873 | inputrc_name = os.environ.get('INPUTRC') |
|
875 | inputrc_name = os.environ.get('INPUTRC') | |
874 | if inputrc_name is None: |
|
876 | if inputrc_name is None: | |
875 | home_dir = get_home_dir() |
|
877 | home_dir = get_home_dir() | |
876 | if home_dir is not None: |
|
878 | if home_dir is not None: | |
877 | inputrc_name = '.inputrc' |
|
879 | inputrc_name = '.inputrc' | |
878 | if readline.uses_libedit: |
|
880 | if readline.uses_libedit: | |
879 | inputrc_name = '.editrc' |
|
881 | inputrc_name = '.editrc' | |
880 | inputrc_name = os.path.join(home_dir, inputrc_name) |
|
882 | inputrc_name = os.path.join(home_dir, inputrc_name) | |
881 | if os.path.isfile(inputrc_name): |
|
883 | if os.path.isfile(inputrc_name): | |
882 | try: |
|
884 | try: | |
883 | readline.read_init_file(inputrc_name) |
|
885 | readline.read_init_file(inputrc_name) | |
884 | except: |
|
886 | except: | |
885 | warn('Problems reading readline initialization file <%s>' |
|
887 | warn('Problems reading readline initialization file <%s>' | |
886 | % inputrc_name) |
|
888 | % inputrc_name) | |
887 |
|
889 | |||
888 | self.has_readline = 1 |
|
890 | self.has_readline = 1 | |
889 | self.readline = readline |
|
891 | self.readline = readline | |
890 | # save this in sys so embedded copies can restore it properly |
|
892 | # save this in sys so embedded copies can restore it properly | |
891 | sys.ipcompleter = self.Completer.complete |
|
893 | sys.ipcompleter = self.Completer.complete | |
892 | self.set_completer() |
|
894 | self.set_completer() | |
893 |
|
895 | |||
894 | # Configure readline according to user's prefs |
|
896 | # Configure readline according to user's prefs | |
895 | # This is only done if GNU readline is being used. If libedit |
|
897 | # This is only done if GNU readline is being used. If libedit | |
896 | # is being used (as on Leopard) the readline config is |
|
898 | # is being used (as on Leopard) the readline config is | |
897 | # not run as the syntax for libedit is different. |
|
899 | # not run as the syntax for libedit is different. | |
898 | if not readline.uses_libedit: |
|
900 | if not readline.uses_libedit: | |
899 | for rlcommand in self.readline_parse_and_bind: |
|
901 | for rlcommand in self.readline_parse_and_bind: | |
900 | #print "loading rl:",rlcommand # dbg |
|
902 | #print "loading rl:",rlcommand # dbg | |
901 | readline.parse_and_bind(rlcommand) |
|
903 | readline.parse_and_bind(rlcommand) | |
902 |
|
904 | |||
903 | # Remove some chars from the delimiters list. If we encounter |
|
905 | # Remove some chars from the delimiters list. If we encounter | |
904 | # unicode chars, discard them. |
|
906 | # unicode chars, discard them. | |
905 | delims = readline.get_completer_delims().encode("ascii", "ignore") |
|
907 | delims = readline.get_completer_delims().encode("ascii", "ignore") | |
906 | delims = delims.translate(string._idmap, |
|
908 | delims = delims.translate(string._idmap, | |
907 | self.readline_remove_delims) |
|
909 | self.readline_remove_delims) | |
908 | readline.set_completer_delims(delims) |
|
910 | readline.set_completer_delims(delims) | |
909 | # otherwise we end up with a monster history after a while: |
|
911 | # otherwise we end up with a monster history after a while: | |
910 | readline.set_history_length(1000) |
|
912 | readline.set_history_length(1000) | |
911 | try: |
|
913 | try: | |
912 | #print '*** Reading readline history' # dbg |
|
914 | #print '*** Reading readline history' # dbg | |
913 | readline.read_history_file(self.histfile) |
|
915 | readline.read_history_file(self.histfile) | |
914 | except IOError: |
|
916 | except IOError: | |
915 | pass # It doesn't exist yet. |
|
917 | pass # It doesn't exist yet. | |
916 |
|
918 | |||
917 | atexit.register(self.atexit_operations) |
|
919 | atexit.register(self.atexit_operations) | |
918 | del atexit |
|
920 | del atexit | |
919 |
|
921 | |||
920 | # Configure auto-indent for all platforms |
|
922 | # Configure auto-indent for all platforms | |
921 | self.set_autoindent(self.autoindent) |
|
923 | self.set_autoindent(self.autoindent) | |
922 |
|
924 | |||
923 | def init_prompts(self): |
|
925 | def init_prompts(self): | |
924 | # Initialize cache, set in/out prompts and printing system |
|
926 | # Initialize cache, set in/out prompts and printing system | |
925 | self.outputcache = CachedOutput(self, |
|
927 | self.outputcache = CachedOutput(self, | |
926 | self.cache_size, |
|
928 | self.cache_size, | |
927 | self.pprint, |
|
929 | self.pprint, | |
928 | input_sep = self.separate_in, |
|
930 | input_sep = self.separate_in, | |
929 | output_sep = self.separate_out, |
|
931 | output_sep = self.separate_out, | |
930 | output_sep2 = self.separate_out2, |
|
932 | output_sep2 = self.separate_out2, | |
931 | ps1 = self.prompt_in1, |
|
933 | ps1 = self.prompt_in1, | |
932 | ps2 = self.prompt_in2, |
|
934 | ps2 = self.prompt_in2, | |
933 | ps_out = self.prompt_out, |
|
935 | ps_out = self.prompt_out, | |
934 | pad_left = self.prompts_pad_left) |
|
936 | pad_left = self.prompts_pad_left) | |
935 |
|
937 | |||
936 | # user may have over-ridden the default print hook: |
|
938 | # user may have over-ridden the default print hook: | |
937 | try: |
|
939 | try: | |
938 | self.outputcache.__class__.display = self.hooks.display |
|
940 | self.outputcache.__class__.display = self.hooks.display | |
939 | except AttributeError: |
|
941 | except AttributeError: | |
940 | pass |
|
942 | pass | |
941 |
|
943 | |||
942 | def init_displayhook(self): |
|
944 | def init_displayhook(self): | |
943 | # I don't like assigning globally to sys, because it means when |
|
945 | self.display_trap = DisplayTrap(self, self.outputcache) | |
944 | # embedding instances, each embedded instance overrides the previous |
|
|||
945 | # choice. But sys.displayhook seems to be called internally by exec, |
|
|||
946 | # so I don't see a way around it. We first save the original and then |
|
|||
947 | # overwrite it. |
|
|||
948 | self.sys_displayhook = sys.displayhook |
|
|||
949 | sys.displayhook = self.outputcache |
|
|||
950 |
|
946 | |||
951 | def init_reload_doctest(self): |
|
947 | def init_reload_doctest(self): | |
952 | # Do a proper resetting of doctest, including the necessary displayhook |
|
948 | # Do a proper resetting of doctest, including the necessary displayhook | |
953 | # monkeypatching |
|
949 | # monkeypatching | |
954 | try: |
|
950 | try: | |
955 | doctest_reload() |
|
951 | doctest_reload() | |
956 | except ImportError: |
|
952 | except ImportError: | |
957 | warn("doctest module does not exist.") |
|
953 | warn("doctest module does not exist.") | |
958 |
|
954 | |||
959 | def init_magics(self): |
|
955 | def init_magics(self): | |
960 | # Set user colors (don't do it in the constructor above so that it |
|
956 | # Set user colors (don't do it in the constructor above so that it | |
961 | # doesn't crash if colors option is invalid) |
|
957 | # doesn't crash if colors option is invalid) | |
962 | self.magic_colors(self.colors) |
|
958 | self.magic_colors(self.colors) | |
963 |
|
959 | |||
964 | def init_pdb(self): |
|
960 | def init_pdb(self): | |
965 | # Set calling of pdb on exceptions |
|
961 | # Set calling of pdb on exceptions | |
966 | # self.call_pdb is a property |
|
962 | # self.call_pdb is a property | |
967 | self.call_pdb = self.pdb |
|
963 | self.call_pdb = self.pdb | |
968 |
|
964 | |||
969 | # def init_exec_commands(self): |
|
965 | # def init_exec_commands(self): | |
970 | # for cmd in self.config.EXECUTE: |
|
966 | # for cmd in self.config.EXECUTE: | |
971 | # print "execute:", cmd |
|
967 | # print "execute:", cmd | |
972 | # self.api.runlines(cmd) |
|
968 | # self.api.runlines(cmd) | |
973 | # |
|
969 | # | |
974 | # batchrun = False |
|
970 | # batchrun = False | |
975 | # if self.config.has_key('EXECFILE'): |
|
971 | # if self.config.has_key('EXECFILE'): | |
976 | # for batchfile in [path(arg) for arg in self.config.EXECFILE |
|
972 | # for batchfile in [path(arg) for arg in self.config.EXECFILE | |
977 | # if arg.lower().endswith('.ipy')]: |
|
973 | # if arg.lower().endswith('.ipy')]: | |
978 | # if not batchfile.isfile(): |
|
974 | # if not batchfile.isfile(): | |
979 | # print "No such batch file:", batchfile |
|
975 | # print "No such batch file:", batchfile | |
980 | # continue |
|
976 | # continue | |
981 | # self.api.runlines(batchfile.text()) |
|
977 | # self.api.runlines(batchfile.text()) | |
982 | # batchrun = True |
|
978 | # batchrun = True | |
983 | # # without -i option, exit after running the batch file |
|
979 | # # without -i option, exit after running the batch file | |
984 | # if batchrun and not self.interactive: |
|
980 | # if batchrun and not self.interactive: | |
985 | # self.ask_exit() |
|
981 | # self.ask_exit() | |
986 |
|
982 | |||
987 | # def load(self, mod): |
|
983 | # def load(self, mod): | |
988 | # """ Load an extension. |
|
984 | # """ Load an extension. | |
989 | # |
|
985 | # | |
990 | # Some modules should (or must) be 'load()':ed, rather than just imported. |
|
986 | # Some modules should (or must) be 'load()':ed, rather than just imported. | |
991 | # |
|
987 | # | |
992 | # Loading will do: |
|
988 | # Loading will do: | |
993 | # |
|
989 | # | |
994 | # - run init_ipython(ip) |
|
990 | # - run init_ipython(ip) | |
995 | # - run ipython_firstrun(ip) |
|
991 | # - run ipython_firstrun(ip) | |
996 | # """ |
|
992 | # """ | |
997 | # |
|
993 | # | |
998 | # if mod in self.extensions: |
|
994 | # if mod in self.extensions: | |
999 | # # just to make sure we don't init it twice |
|
995 | # # just to make sure we don't init it twice | |
1000 | # # note that if you 'load' a module that has already been |
|
996 | # # note that if you 'load' a module that has already been | |
1001 | # # imported, init_ipython gets run anyway |
|
997 | # # imported, init_ipython gets run anyway | |
1002 | # |
|
998 | # | |
1003 | # return self.extensions[mod] |
|
999 | # return self.extensions[mod] | |
1004 | # __import__(mod) |
|
1000 | # __import__(mod) | |
1005 | # m = sys.modules[mod] |
|
1001 | # m = sys.modules[mod] | |
1006 | # if hasattr(m,'init_ipython'): |
|
1002 | # if hasattr(m,'init_ipython'): | |
1007 | # m.init_ipython(self) |
|
1003 | # m.init_ipython(self) | |
1008 | # |
|
1004 | # | |
1009 | # if hasattr(m,'ipython_firstrun'): |
|
1005 | # if hasattr(m,'ipython_firstrun'): | |
1010 | # already_loaded = self.db.get('firstrun_done', set()) |
|
1006 | # already_loaded = self.db.get('firstrun_done', set()) | |
1011 | # if mod not in already_loaded: |
|
1007 | # if mod not in already_loaded: | |
1012 | # m.ipython_firstrun(self) |
|
1008 | # m.ipython_firstrun(self) | |
1013 | # already_loaded.add(mod) |
|
1009 | # already_loaded.add(mod) | |
1014 | # self.db['firstrun_done'] = already_loaded |
|
1010 | # self.db['firstrun_done'] = already_loaded | |
1015 | # |
|
1011 | # | |
1016 | # self.extensions[mod] = m |
|
1012 | # self.extensions[mod] = m | |
1017 | # return m |
|
1013 | # return m | |
1018 |
|
1014 | |||
1019 | def init_user_ns(self): |
|
1015 | def init_user_ns(self): | |
1020 | """Initialize all user-visible namespaces to their minimum defaults. |
|
1016 | """Initialize all user-visible namespaces to their minimum defaults. | |
1021 |
|
1017 | |||
1022 | Certain history lists are also initialized here, as they effectively |
|
1018 | Certain history lists are also initialized here, as they effectively | |
1023 | act as user namespaces. |
|
1019 | act as user namespaces. | |
1024 |
|
1020 | |||
1025 | Notes |
|
1021 | Notes | |
1026 | ----- |
|
1022 | ----- | |
1027 | All data structures here are only filled in, they are NOT reset by this |
|
1023 | All data structures here are only filled in, they are NOT reset by this | |
1028 | method. If they were not empty before, data will simply be added to |
|
1024 | method. If they were not empty before, data will simply be added to | |
1029 | therm. |
|
1025 | therm. | |
1030 | """ |
|
1026 | """ | |
1031 | # The user namespace MUST have a pointer to the shell itself. |
|
1027 | # The user namespace MUST have a pointer to the shell itself. | |
1032 | self.user_ns[self.name] = self |
|
1028 | self.user_ns[self.name] = self | |
1033 |
|
1029 | |||
1034 | # Store myself as the public api!!! |
|
1030 | # Store myself as the public api!!! | |
1035 | self.user_ns['_ip'] = self |
|
1031 | self.user_ns['_ip'] = self | |
1036 |
|
1032 | |||
1037 | # make global variables for user access to the histories |
|
1033 | # make global variables for user access to the histories | |
1038 | self.user_ns['_ih'] = self.input_hist |
|
1034 | self.user_ns['_ih'] = self.input_hist | |
1039 | self.user_ns['_oh'] = self.output_hist |
|
1035 | self.user_ns['_oh'] = self.output_hist | |
1040 | self.user_ns['_dh'] = self.dir_hist |
|
1036 | self.user_ns['_dh'] = self.dir_hist | |
1041 |
|
1037 | |||
1042 | # user aliases to input and output histories |
|
1038 | # user aliases to input and output histories | |
1043 | self.user_ns['In'] = self.input_hist |
|
1039 | self.user_ns['In'] = self.input_hist | |
1044 | self.user_ns['Out'] = self.output_hist |
|
1040 | self.user_ns['Out'] = self.output_hist | |
1045 |
|
1041 | |||
1046 | self.user_ns['_sh'] = shadowns |
|
1042 | self.user_ns['_sh'] = shadowns | |
1047 |
|
1043 | |||
1048 | # Put 'help' in the user namespace |
|
1044 | # Put 'help' in the user namespace | |
1049 | try: |
|
1045 | try: | |
1050 | from site import _Helper |
|
1046 | from site import _Helper | |
1051 | self.user_ns['help'] = _Helper() |
|
1047 | self.user_ns['help'] = _Helper() | |
1052 | except ImportError: |
|
1048 | except ImportError: | |
1053 | warn('help() not available - check site.py') |
|
1049 | warn('help() not available - check site.py') | |
1054 |
|
1050 | |||
1055 | def save_sys_module_state(self): |
|
1051 | def save_sys_module_state(self): | |
1056 | """Save the state of hooks in the sys module. |
|
1052 | """Save the state of hooks in the sys module. | |
1057 |
|
1053 | |||
1058 | This has to be called after self.user_ns is created. |
|
1054 | This has to be called after self.user_ns is created. | |
1059 | """ |
|
1055 | """ | |
1060 | self._orig_sys_module_state = {} |
|
1056 | self._orig_sys_module_state = {} | |
1061 | self._orig_sys_module_state['stdin'] = sys.stdin |
|
1057 | self._orig_sys_module_state['stdin'] = sys.stdin | |
1062 | self._orig_sys_module_state['stdout'] = sys.stdout |
|
1058 | self._orig_sys_module_state['stdout'] = sys.stdout | |
1063 | self._orig_sys_module_state['stderr'] = sys.stderr |
|
1059 | self._orig_sys_module_state['stderr'] = sys.stderr | |
1064 | self._orig_sys_module_state['displayhook'] = sys.displayhook |
|
|||
1065 | self._orig_sys_module_state['excepthook'] = sys.excepthook |
|
1060 | self._orig_sys_module_state['excepthook'] = sys.excepthook | |
1066 | try: |
|
1061 | try: | |
1067 | self._orig_sys_modules_main_name = self.user_ns['__name__'] |
|
1062 | self._orig_sys_modules_main_name = self.user_ns['__name__'] | |
1068 | except KeyError: |
|
1063 | except KeyError: | |
1069 | pass |
|
1064 | pass | |
1070 |
|
1065 | |||
1071 | def restore_sys_module_state(self): |
|
1066 | def restore_sys_module_state(self): | |
1072 | """Restore the state of the sys module.""" |
|
1067 | """Restore the state of the sys module.""" | |
1073 | try: |
|
1068 | try: | |
1074 | for k, v in self._orig_sys_module_state.items(): |
|
1069 | for k, v in self._orig_sys_module_state.items(): | |
1075 | setattr(sys, k, v) |
|
1070 | setattr(sys, k, v) | |
1076 | except AttributeError: |
|
1071 | except AttributeError: | |
1077 | pass |
|
1072 | pass | |
1078 | try: |
|
1073 | try: | |
1079 | delattr(sys, 'ipcompleter') |
|
1074 | delattr(sys, 'ipcompleter') | |
1080 | except AttributeError: |
|
1075 | except AttributeError: | |
1081 | pass |
|
1076 | pass | |
1082 | # Reset what what done in self.init_sys_modules |
|
1077 | # Reset what what done in self.init_sys_modules | |
1083 | try: |
|
1078 | try: | |
1084 | sys.modules[self.user_ns['__name__']] = self._orig_sys_modules_main_name |
|
1079 | sys.modules[self.user_ns['__name__']] = self._orig_sys_modules_main_name | |
1085 | except (AttributeError, KeyError): |
|
1080 | except (AttributeError, KeyError): | |
1086 | pass |
|
1081 | pass | |
1087 |
|
1082 | |||
1088 | def set_hook(self,name,hook, priority = 50, str_key = None, re_key = None): |
|
1083 | def set_hook(self,name,hook, priority = 50, str_key = None, re_key = None): | |
1089 | """set_hook(name,hook) -> sets an internal IPython hook. |
|
1084 | """set_hook(name,hook) -> sets an internal IPython hook. | |
1090 |
|
1085 | |||
1091 | IPython exposes some of its internal API as user-modifiable hooks. By |
|
1086 | IPython exposes some of its internal API as user-modifiable hooks. By | |
1092 | adding your function to one of these hooks, you can modify IPython's |
|
1087 | adding your function to one of these hooks, you can modify IPython's | |
1093 | behavior to call at runtime your own routines.""" |
|
1088 | behavior to call at runtime your own routines.""" | |
1094 |
|
1089 | |||
1095 | # At some point in the future, this should validate the hook before it |
|
1090 | # At some point in the future, this should validate the hook before it | |
1096 | # accepts it. Probably at least check that the hook takes the number |
|
1091 | # accepts it. Probably at least check that the hook takes the number | |
1097 | # of args it's supposed to. |
|
1092 | # of args it's supposed to. | |
1098 |
|
1093 | |||
1099 | f = new.instancemethod(hook,self,self.__class__) |
|
1094 | f = new.instancemethod(hook,self,self.__class__) | |
1100 |
|
1095 | |||
1101 | # check if the hook is for strdispatcher first |
|
1096 | # check if the hook is for strdispatcher first | |
1102 | if str_key is not None: |
|
1097 | if str_key is not None: | |
1103 | sdp = self.strdispatchers.get(name, StrDispatch()) |
|
1098 | sdp = self.strdispatchers.get(name, StrDispatch()) | |
1104 | sdp.add_s(str_key, f, priority ) |
|
1099 | sdp.add_s(str_key, f, priority ) | |
1105 | self.strdispatchers[name] = sdp |
|
1100 | self.strdispatchers[name] = sdp | |
1106 | return |
|
1101 | return | |
1107 | if re_key is not None: |
|
1102 | if re_key is not None: | |
1108 | sdp = self.strdispatchers.get(name, StrDispatch()) |
|
1103 | sdp = self.strdispatchers.get(name, StrDispatch()) | |
1109 | sdp.add_re(re.compile(re_key), f, priority ) |
|
1104 | sdp.add_re(re.compile(re_key), f, priority ) | |
1110 | self.strdispatchers[name] = sdp |
|
1105 | self.strdispatchers[name] = sdp | |
1111 | return |
|
1106 | return | |
1112 |
|
1107 | |||
1113 | dp = getattr(self.hooks, name, None) |
|
1108 | dp = getattr(self.hooks, name, None) | |
1114 | if name not in IPython.core.hooks.__all__: |
|
1109 | if name not in IPython.core.hooks.__all__: | |
1115 | print "Warning! Hook '%s' is not one of %s" % (name, IPython.core.hooks.__all__ ) |
|
1110 | print "Warning! Hook '%s' is not one of %s" % (name, IPython.core.hooks.__all__ ) | |
1116 | if not dp: |
|
1111 | if not dp: | |
1117 | dp = IPython.core.hooks.CommandChainDispatcher() |
|
1112 | dp = IPython.core.hooks.CommandChainDispatcher() | |
1118 |
|
1113 | |||
1119 | try: |
|
1114 | try: | |
1120 | dp.add(f,priority) |
|
1115 | dp.add(f,priority) | |
1121 | except AttributeError: |
|
1116 | except AttributeError: | |
1122 | # it was not commandchain, plain old func - replace |
|
1117 | # it was not commandchain, plain old func - replace | |
1123 | dp = f |
|
1118 | dp = f | |
1124 |
|
1119 | |||
1125 | setattr(self.hooks,name, dp) |
|
1120 | setattr(self.hooks,name, dp) | |
1126 |
|
1121 | |||
1127 | def set_crash_handler(self, crashHandler): |
|
1122 | def set_crash_handler(self, crashHandler): | |
1128 | """Set the IPython crash handler. |
|
1123 | """Set the IPython crash handler. | |
1129 |
|
1124 | |||
1130 | This must be a callable with a signature suitable for use as |
|
1125 | This must be a callable with a signature suitable for use as | |
1131 | sys.excepthook.""" |
|
1126 | sys.excepthook.""" | |
1132 |
|
1127 | |||
1133 | # Install the given crash handler as the Python exception hook |
|
1128 | # Install the given crash handler as the Python exception hook | |
1134 | sys.excepthook = crashHandler |
|
1129 | sys.excepthook = crashHandler | |
1135 |
|
1130 | |||
1136 | # The instance will store a pointer to this, so that runtime code |
|
1131 | # The instance will store a pointer to this, so that runtime code | |
1137 | # (such as magics) can access it. This is because during the |
|
1132 | # (such as magics) can access it. This is because during the | |
1138 | # read-eval loop, it gets temporarily overwritten (to deal with GUI |
|
1133 | # read-eval loop, it gets temporarily overwritten (to deal with GUI | |
1139 | # frameworks). |
|
1134 | # frameworks). | |
1140 | self.sys_excepthook = sys.excepthook |
|
1135 | self.sys_excepthook = sys.excepthook | |
1141 |
|
1136 | |||
1142 | def set_custom_exc(self,exc_tuple,handler): |
|
1137 | def set_custom_exc(self,exc_tuple,handler): | |
1143 | """set_custom_exc(exc_tuple,handler) |
|
1138 | """set_custom_exc(exc_tuple,handler) | |
1144 |
|
1139 | |||
1145 | Set a custom exception handler, which will be called if any of the |
|
1140 | Set a custom exception handler, which will be called if any of the | |
1146 | exceptions in exc_tuple occur in the mainloop (specifically, in the |
|
1141 | exceptions in exc_tuple occur in the mainloop (specifically, in the | |
1147 | runcode() method. |
|
1142 | runcode() method. | |
1148 |
|
1143 | |||
1149 | Inputs: |
|
1144 | Inputs: | |
1150 |
|
1145 | |||
1151 | - exc_tuple: a *tuple* of valid exceptions to call the defined |
|
1146 | - exc_tuple: a *tuple* of valid exceptions to call the defined | |
1152 | handler for. It is very important that you use a tuple, and NOT A |
|
1147 | handler for. It is very important that you use a tuple, and NOT A | |
1153 | LIST here, because of the way Python's except statement works. If |
|
1148 | LIST here, because of the way Python's except statement works. If | |
1154 | you only want to trap a single exception, use a singleton tuple: |
|
1149 | you only want to trap a single exception, use a singleton tuple: | |
1155 |
|
1150 | |||
1156 | exc_tuple == (MyCustomException,) |
|
1151 | exc_tuple == (MyCustomException,) | |
1157 |
|
1152 | |||
1158 | - handler: this must be defined as a function with the following |
|
1153 | - handler: this must be defined as a function with the following | |
1159 | basic interface: def my_handler(self,etype,value,tb). |
|
1154 | basic interface: def my_handler(self,etype,value,tb). | |
1160 |
|
1155 | |||
1161 | This will be made into an instance method (via new.instancemethod) |
|
1156 | This will be made into an instance method (via new.instancemethod) | |
1162 | of IPython itself, and it will be called if any of the exceptions |
|
1157 | of IPython itself, and it will be called if any of the exceptions | |
1163 | listed in the exc_tuple are caught. If the handler is None, an |
|
1158 | listed in the exc_tuple are caught. If the handler is None, an | |
1164 | internal basic one is used, which just prints basic info. |
|
1159 | internal basic one is used, which just prints basic info. | |
1165 |
|
1160 | |||
1166 | WARNING: by putting in your own exception handler into IPython's main |
|
1161 | WARNING: by putting in your own exception handler into IPython's main | |
1167 | execution loop, you run a very good chance of nasty crashes. This |
|
1162 | execution loop, you run a very good chance of nasty crashes. This | |
1168 | facility should only be used if you really know what you are doing.""" |
|
1163 | facility should only be used if you really know what you are doing.""" | |
1169 |
|
1164 | |||
1170 | assert type(exc_tuple)==type(()) , \ |
|
1165 | assert type(exc_tuple)==type(()) , \ | |
1171 | "The custom exceptions must be given AS A TUPLE." |
|
1166 | "The custom exceptions must be given AS A TUPLE." | |
1172 |
|
1167 | |||
1173 | def dummy_handler(self,etype,value,tb): |
|
1168 | def dummy_handler(self,etype,value,tb): | |
1174 | print '*** Simple custom exception handler ***' |
|
1169 | print '*** Simple custom exception handler ***' | |
1175 | print 'Exception type :',etype |
|
1170 | print 'Exception type :',etype | |
1176 | print 'Exception value:',value |
|
1171 | print 'Exception value:',value | |
1177 | print 'Traceback :',tb |
|
1172 | print 'Traceback :',tb | |
1178 | print 'Source code :','\n'.join(self.buffer) |
|
1173 | print 'Source code :','\n'.join(self.buffer) | |
1179 |
|
1174 | |||
1180 | if handler is None: handler = dummy_handler |
|
1175 | if handler is None: handler = dummy_handler | |
1181 |
|
1176 | |||
1182 | self.CustomTB = new.instancemethod(handler,self,self.__class__) |
|
1177 | self.CustomTB = new.instancemethod(handler,self,self.__class__) | |
1183 | self.custom_exceptions = exc_tuple |
|
1178 | self.custom_exceptions = exc_tuple | |
1184 |
|
1179 | |||
1185 | def set_custom_completer(self,completer,pos=0): |
|
1180 | def set_custom_completer(self,completer,pos=0): | |
1186 | """set_custom_completer(completer,pos=0) |
|
1181 | """set_custom_completer(completer,pos=0) | |
1187 |
|
1182 | |||
1188 | Adds a new custom completer function. |
|
1183 | Adds a new custom completer function. | |
1189 |
|
1184 | |||
1190 | The position argument (defaults to 0) is the index in the completers |
|
1185 | The position argument (defaults to 0) is the index in the completers | |
1191 | list where you want the completer to be inserted.""" |
|
1186 | list where you want the completer to be inserted.""" | |
1192 |
|
1187 | |||
1193 | newcomp = new.instancemethod(completer,self.Completer, |
|
1188 | newcomp = new.instancemethod(completer,self.Completer, | |
1194 | self.Completer.__class__) |
|
1189 | self.Completer.__class__) | |
1195 | self.Completer.matchers.insert(pos,newcomp) |
|
1190 | self.Completer.matchers.insert(pos,newcomp) | |
1196 |
|
1191 | |||
1197 | def set_completer(self): |
|
1192 | def set_completer(self): | |
1198 | """reset readline's completer to be our own.""" |
|
1193 | """reset readline's completer to be our own.""" | |
1199 | self.readline.set_completer(self.Completer.complete) |
|
1194 | self.readline.set_completer(self.Completer.complete) | |
1200 |
|
1195 | |||
1201 | def _get_call_pdb(self): |
|
1196 | def _get_call_pdb(self): | |
1202 | return self._call_pdb |
|
1197 | return self._call_pdb | |
1203 |
|
1198 | |||
1204 | def _set_call_pdb(self,val): |
|
1199 | def _set_call_pdb(self,val): | |
1205 |
|
1200 | |||
1206 | if val not in (0,1,False,True): |
|
1201 | if val not in (0,1,False,True): | |
1207 | raise ValueError,'new call_pdb value must be boolean' |
|
1202 | raise ValueError,'new call_pdb value must be boolean' | |
1208 |
|
1203 | |||
1209 | # store value in instance |
|
1204 | # store value in instance | |
1210 | self._call_pdb = val |
|
1205 | self._call_pdb = val | |
1211 |
|
1206 | |||
1212 | # notify the actual exception handlers |
|
1207 | # notify the actual exception handlers | |
1213 | self.InteractiveTB.call_pdb = val |
|
1208 | self.InteractiveTB.call_pdb = val | |
1214 | if self.isthreaded: |
|
1209 | if self.isthreaded: | |
1215 | try: |
|
1210 | try: | |
1216 | self.sys_excepthook.call_pdb = val |
|
1211 | self.sys_excepthook.call_pdb = val | |
1217 | except: |
|
1212 | except: | |
1218 | warn('Failed to activate pdb for threaded exception handler') |
|
1213 | warn('Failed to activate pdb for threaded exception handler') | |
1219 |
|
1214 | |||
1220 | call_pdb = property(_get_call_pdb,_set_call_pdb,None, |
|
1215 | call_pdb = property(_get_call_pdb,_set_call_pdb,None, | |
1221 | 'Control auto-activation of pdb at exceptions') |
|
1216 | 'Control auto-activation of pdb at exceptions') | |
1222 |
|
1217 | |||
1223 | def magic(self,arg_s): |
|
1218 | def magic(self,arg_s): | |
1224 | """Call a magic function by name. |
|
1219 | """Call a magic function by name. | |
1225 |
|
1220 | |||
1226 | Input: a string containing the name of the magic function to call and any |
|
1221 | Input: a string containing the name of the magic function to call and any | |
1227 | additional arguments to be passed to the magic. |
|
1222 | additional arguments to be passed to the magic. | |
1228 |
|
1223 | |||
1229 | magic('name -opt foo bar') is equivalent to typing at the ipython |
|
1224 | magic('name -opt foo bar') is equivalent to typing at the ipython | |
1230 | prompt: |
|
1225 | prompt: | |
1231 |
|
1226 | |||
1232 | In[1]: %name -opt foo bar |
|
1227 | In[1]: %name -opt foo bar | |
1233 |
|
1228 | |||
1234 | To call a magic without arguments, simply use magic('name'). |
|
1229 | To call a magic without arguments, simply use magic('name'). | |
1235 |
|
1230 | |||
1236 | This provides a proper Python function to call IPython's magics in any |
|
1231 | This provides a proper Python function to call IPython's magics in any | |
1237 | valid Python code you can type at the interpreter, including loops and |
|
1232 | valid Python code you can type at the interpreter, including loops and | |
1238 | compound statements. |
|
1233 | compound statements. | |
1239 | """ |
|
1234 | """ | |
1240 |
|
1235 | |||
1241 | args = arg_s.split(' ',1) |
|
1236 | args = arg_s.split(' ',1) | |
1242 | magic_name = args[0] |
|
1237 | magic_name = args[0] | |
1243 | magic_name = magic_name.lstrip(self.ESC_MAGIC) |
|
1238 | magic_name = magic_name.lstrip(self.ESC_MAGIC) | |
1244 |
|
1239 | |||
1245 | try: |
|
1240 | try: | |
1246 | magic_args = args[1] |
|
1241 | magic_args = args[1] | |
1247 | except IndexError: |
|
1242 | except IndexError: | |
1248 | magic_args = '' |
|
1243 | magic_args = '' | |
1249 | fn = getattr(self,'magic_'+magic_name,None) |
|
1244 | fn = getattr(self,'magic_'+magic_name,None) | |
1250 | if fn is None: |
|
1245 | if fn is None: | |
1251 | error("Magic function `%s` not found." % magic_name) |
|
1246 | error("Magic function `%s` not found." % magic_name) | |
1252 | else: |
|
1247 | else: | |
1253 | magic_args = self.var_expand(magic_args,1) |
|
1248 | magic_args = self.var_expand(magic_args,1) | |
1254 | with self.builtin_trap: |
|
1249 | with nested(self.builtin_trap, self.display_trap): | |
1255 | return fn(magic_args) |
|
1250 | return fn(magic_args) | |
1256 | # return result |
|
1251 | # return result | |
1257 |
|
1252 | |||
1258 | def define_magic(self, magicname, func): |
|
1253 | def define_magic(self, magicname, func): | |
1259 | """Expose own function as magic function for ipython |
|
1254 | """Expose own function as magic function for ipython | |
1260 |
|
1255 | |||
1261 | def foo_impl(self,parameter_s=''): |
|
1256 | def foo_impl(self,parameter_s=''): | |
1262 | 'My very own magic!. (Use docstrings, IPython reads them).' |
|
1257 | 'My very own magic!. (Use docstrings, IPython reads them).' | |
1263 | print 'Magic function. Passed parameter is between < >:' |
|
1258 | print 'Magic function. Passed parameter is between < >:' | |
1264 | print '<%s>' % parameter_s |
|
1259 | print '<%s>' % parameter_s | |
1265 | print 'The self object is:',self |
|
1260 | print 'The self object is:',self | |
1266 |
|
1261 | |||
1267 | self.define_magic('foo',foo_impl) |
|
1262 | self.define_magic('foo',foo_impl) | |
1268 | """ |
|
1263 | """ | |
1269 |
|
1264 | |||
1270 | import new |
|
1265 | import new | |
1271 | im = new.instancemethod(func,self, self.__class__) |
|
1266 | im = new.instancemethod(func,self, self.__class__) | |
1272 | old = getattr(self, "magic_" + magicname, None) |
|
1267 | old = getattr(self, "magic_" + magicname, None) | |
1273 | setattr(self, "magic_" + magicname, im) |
|
1268 | setattr(self, "magic_" + magicname, im) | |
1274 | return old |
|
1269 | return old | |
1275 |
|
1270 | |||
1276 | def define_macro(self, name, themacro): |
|
1271 | def define_macro(self, name, themacro): | |
1277 | """Define a new macro |
|
1272 | """Define a new macro | |
1278 |
|
1273 | |||
1279 | Parameters |
|
1274 | Parameters | |
1280 | ---------- |
|
1275 | ---------- | |
1281 | name : str |
|
1276 | name : str | |
1282 | The name of the macro. |
|
1277 | The name of the macro. | |
1283 | themacro : str or Macro |
|
1278 | themacro : str or Macro | |
1284 | The action to do upon invoking the macro. If a string, a new |
|
1279 | The action to do upon invoking the macro. If a string, a new | |
1285 | Macro object is created by passing the string to it. |
|
1280 | Macro object is created by passing the string to it. | |
1286 | """ |
|
1281 | """ | |
1287 |
|
1282 | |||
1288 | from IPython.core import macro |
|
1283 | from IPython.core import macro | |
1289 |
|
1284 | |||
1290 | if isinstance(themacro, basestring): |
|
1285 | if isinstance(themacro, basestring): | |
1291 | themacro = macro.Macro(themacro) |
|
1286 | themacro = macro.Macro(themacro) | |
1292 | if not isinstance(themacro, macro.Macro): |
|
1287 | if not isinstance(themacro, macro.Macro): | |
1293 | raise ValueError('A macro must be a string or a Macro instance.') |
|
1288 | raise ValueError('A macro must be a string or a Macro instance.') | |
1294 | self.user_ns[name] = themacro |
|
1289 | self.user_ns[name] = themacro | |
1295 |
|
1290 | |||
1296 | def define_alias(self, name, cmd): |
|
1291 | def define_alias(self, name, cmd): | |
1297 | """ Define a new alias.""" |
|
1292 | """ Define a new alias.""" | |
1298 |
|
1293 | |||
1299 | if callable(cmd): |
|
1294 | if callable(cmd): | |
1300 | self.alias_table[name] = cmd |
|
1295 | self.alias_table[name] = cmd | |
1301 | from IPython.core import shadowns |
|
1296 | from IPython.core import shadowns | |
1302 | setattr(shadowns, name, cmd) |
|
1297 | setattr(shadowns, name, cmd) | |
1303 | return |
|
1298 | return | |
1304 |
|
1299 | |||
1305 | if isinstance(cmd, basestring): |
|
1300 | if isinstance(cmd, basestring): | |
1306 | nargs = cmd.count('%s') |
|
1301 | nargs = cmd.count('%s') | |
1307 | if nargs>0 and cmd.find('%l')>=0: |
|
1302 | if nargs>0 and cmd.find('%l')>=0: | |
1308 | raise Exception('The %s and %l specifiers are mutually ' |
|
1303 | raise Exception('The %s and %l specifiers are mutually ' | |
1309 | 'exclusive in alias definitions.') |
|
1304 | 'exclusive in alias definitions.') | |
1310 |
|
1305 | |||
1311 | self.alias_table[name] = (nargs,cmd) |
|
1306 | self.alias_table[name] = (nargs,cmd) | |
1312 | return |
|
1307 | return | |
1313 |
|
1308 | |||
1314 | self.alias_table[name] = cmd |
|
1309 | self.alias_table[name] = cmd | |
1315 |
|
1310 | |||
1316 | def ipalias(self,arg_s): |
|
1311 | def ipalias(self,arg_s): | |
1317 | """Call an alias by name. |
|
1312 | """Call an alias by name. | |
1318 |
|
1313 | |||
1319 | Input: a string containing the name of the alias to call and any |
|
1314 | Input: a string containing the name of the alias to call and any | |
1320 | additional arguments to be passed to the magic. |
|
1315 | additional arguments to be passed to the magic. | |
1321 |
|
1316 | |||
1322 | ipalias('name -opt foo bar') is equivalent to typing at the ipython |
|
1317 | ipalias('name -opt foo bar') is equivalent to typing at the ipython | |
1323 | prompt: |
|
1318 | prompt: | |
1324 |
|
1319 | |||
1325 | In[1]: name -opt foo bar |
|
1320 | In[1]: name -opt foo bar | |
1326 |
|
1321 | |||
1327 | To call an alias without arguments, simply use ipalias('name'). |
|
1322 | To call an alias without arguments, simply use ipalias('name'). | |
1328 |
|
1323 | |||
1329 | This provides a proper Python function to call IPython's aliases in any |
|
1324 | This provides a proper Python function to call IPython's aliases in any | |
1330 | valid Python code you can type at the interpreter, including loops and |
|
1325 | valid Python code you can type at the interpreter, including loops and | |
1331 | compound statements. It is added by IPython to the Python builtin |
|
1326 | compound statements. It is added by IPython to the Python builtin | |
1332 | namespace upon initialization.""" |
|
1327 | namespace upon initialization.""" | |
1333 |
|
1328 | |||
1334 | args = arg_s.split(' ',1) |
|
1329 | args = arg_s.split(' ',1) | |
1335 | alias_name = args[0] |
|
1330 | alias_name = args[0] | |
1336 | try: |
|
1331 | try: | |
1337 | alias_args = args[1] |
|
1332 | alias_args = args[1] | |
1338 | except IndexError: |
|
1333 | except IndexError: | |
1339 | alias_args = '' |
|
1334 | alias_args = '' | |
1340 | if alias_name in self.alias_table: |
|
1335 | if alias_name in self.alias_table: | |
1341 | self.call_alias(alias_name,alias_args) |
|
1336 | self.call_alias(alias_name,alias_args) | |
1342 | else: |
|
1337 | else: | |
1343 | error("Alias `%s` not found." % alias_name) |
|
1338 | error("Alias `%s` not found." % alias_name) | |
1344 |
|
1339 | |||
1345 | def system(self, cmd): |
|
1340 | def system(self, cmd): | |
1346 | """Make a system call, using IPython.""" |
|
1341 | """Make a system call, using IPython.""" | |
1347 | return self.hooks.shell_hook(self.var_expand(cmd, depth=2)) |
|
1342 | return self.hooks.shell_hook(self.var_expand(cmd, depth=2)) | |
1348 |
|
1343 | |||
1349 | def ex(self, cmd): |
|
1344 | def ex(self, cmd): | |
1350 | """Execute a normal python statement in user namespace.""" |
|
1345 | """Execute a normal python statement in user namespace.""" | |
1351 | with self.builtin_trap: |
|
1346 | with nested(self.builtin_trap, self.display_trap): | |
1352 | exec cmd in self.user_global_ns, self.user_ns |
|
1347 | exec cmd in self.user_global_ns, self.user_ns | |
1353 |
|
1348 | |||
1354 | def ev(self, expr): |
|
1349 | def ev(self, expr): | |
1355 | """Evaluate python expression expr in user namespace. |
|
1350 | """Evaluate python expression expr in user namespace. | |
1356 |
|
1351 | |||
1357 | Returns the result of evaluation |
|
1352 | Returns the result of evaluation | |
1358 | """ |
|
1353 | """ | |
1359 | with self.builtin_trap: |
|
1354 | with nested(self.builtin_trap, self.display_trap): | |
1360 | return eval(expr, self.user_global_ns, self.user_ns) |
|
1355 | return eval(expr, self.user_global_ns, self.user_ns) | |
1361 |
|
1356 | |||
1362 | def getoutput(self, cmd): |
|
1357 | def getoutput(self, cmd): | |
1363 | return getoutput(self.var_expand(cmd,depth=2), |
|
1358 | return getoutput(self.var_expand(cmd,depth=2), | |
1364 | header=self.system_header, |
|
1359 | header=self.system_header, | |
1365 | verbose=self.system_verbose) |
|
1360 | verbose=self.system_verbose) | |
1366 |
|
1361 | |||
1367 | def getoutputerror(self, cmd): |
|
1362 | def getoutputerror(self, cmd): | |
1368 | return getoutputerror(self.var_expand(cmd,depth=2), |
|
1363 | return getoutputerror(self.var_expand(cmd,depth=2), | |
1369 | header=self.system_header, |
|
1364 | header=self.system_header, | |
1370 | verbose=self.system_verbose) |
|
1365 | verbose=self.system_verbose) | |
1371 |
|
1366 | |||
1372 | def complete(self, text): |
|
1367 | def complete(self, text): | |
1373 | """Return a sorted list of all possible completions on text. |
|
1368 | """Return a sorted list of all possible completions on text. | |
1374 |
|
1369 | |||
1375 | Inputs: |
|
1370 | Inputs: | |
1376 |
|
1371 | |||
1377 | - text: a string of text to be completed on. |
|
1372 | - text: a string of text to be completed on. | |
1378 |
|
1373 | |||
1379 | This is a wrapper around the completion mechanism, similar to what |
|
1374 | This is a wrapper around the completion mechanism, similar to what | |
1380 | readline does at the command line when the TAB key is hit. By |
|
1375 | readline does at the command line when the TAB key is hit. By | |
1381 | exposing it as a method, it can be used by other non-readline |
|
1376 | exposing it as a method, it can be used by other non-readline | |
1382 | environments (such as GUIs) for text completion. |
|
1377 | environments (such as GUIs) for text completion. | |
1383 |
|
1378 | |||
1384 | Simple usage example: |
|
1379 | Simple usage example: | |
1385 |
|
1380 | |||
1386 | In [7]: x = 'hello' |
|
1381 | In [7]: x = 'hello' | |
1387 |
|
1382 | |||
1388 | In [8]: x |
|
1383 | In [8]: x | |
1389 | Out[8]: 'hello' |
|
1384 | Out[8]: 'hello' | |
1390 |
|
1385 | |||
1391 | In [9]: print x |
|
1386 | In [9]: print x | |
1392 | hello |
|
1387 | hello | |
1393 |
|
1388 | |||
1394 | In [10]: _ip.complete('x.l') |
|
1389 | In [10]: _ip.complete('x.l') | |
1395 | Out[10]: ['x.ljust', 'x.lower', 'x.lstrip'] |
|
1390 | Out[10]: ['x.ljust', 'x.lower', 'x.lstrip'] | |
1396 | """ |
|
1391 | """ | |
1397 |
|
1392 | |||
1398 | # Inject names into __builtin__ so we can complete on the added names. |
|
1393 | # Inject names into __builtin__ so we can complete on the added names. | |
1399 | with self.builtin_trap: |
|
1394 | with self.builtin_trap: | |
1400 | complete = self.Completer.complete |
|
1395 | complete = self.Completer.complete | |
1401 | state = 0 |
|
1396 | state = 0 | |
1402 | # use a dict so we get unique keys, since ipyhton's multiple |
|
1397 | # use a dict so we get unique keys, since ipyhton's multiple | |
1403 | # completers can return duplicates. When we make 2.4 a requirement, |
|
1398 | # completers can return duplicates. When we make 2.4 a requirement, | |
1404 | # start using sets instead, which are faster. |
|
1399 | # start using sets instead, which are faster. | |
1405 | comps = {} |
|
1400 | comps = {} | |
1406 | while True: |
|
1401 | while True: | |
1407 | newcomp = complete(text,state,line_buffer=text) |
|
1402 | newcomp = complete(text,state,line_buffer=text) | |
1408 | if newcomp is None: |
|
1403 | if newcomp is None: | |
1409 | break |
|
1404 | break | |
1410 | comps[newcomp] = 1 |
|
1405 | comps[newcomp] = 1 | |
1411 | state += 1 |
|
1406 | state += 1 | |
1412 | outcomps = comps.keys() |
|
1407 | outcomps = comps.keys() | |
1413 | outcomps.sort() |
|
1408 | outcomps.sort() | |
1414 | #print "T:",text,"OC:",outcomps # dbg |
|
1409 | #print "T:",text,"OC:",outcomps # dbg | |
1415 | #print "vars:",self.user_ns.keys() |
|
1410 | #print "vars:",self.user_ns.keys() | |
1416 | return outcomps |
|
1411 | return outcomps | |
1417 |
|
1412 | |||
1418 | def set_completer_frame(self, frame=None): |
|
1413 | def set_completer_frame(self, frame=None): | |
1419 | if frame: |
|
1414 | if frame: | |
1420 | self.Completer.namespace = frame.f_locals |
|
1415 | self.Completer.namespace = frame.f_locals | |
1421 | self.Completer.global_namespace = frame.f_globals |
|
1416 | self.Completer.global_namespace = frame.f_globals | |
1422 | else: |
|
1417 | else: | |
1423 | self.Completer.namespace = self.user_ns |
|
1418 | self.Completer.namespace = self.user_ns | |
1424 | self.Completer.global_namespace = self.user_global_ns |
|
1419 | self.Completer.global_namespace = self.user_global_ns | |
1425 |
|
1420 | |||
1426 | def init_auto_alias(self): |
|
1421 | def init_auto_alias(self): | |
1427 | """Define some aliases automatically. |
|
1422 | """Define some aliases automatically. | |
1428 |
|
1423 | |||
1429 | These are ALL parameter-less aliases""" |
|
1424 | These are ALL parameter-less aliases""" | |
1430 |
|
1425 | |||
1431 | for alias,cmd in self.auto_alias: |
|
1426 | for alias,cmd in self.auto_alias: | |
1432 | self.define_alias(alias,cmd) |
|
1427 | self.define_alias(alias,cmd) | |
1433 |
|
1428 | |||
1434 | def alias_table_validate(self,verbose=0): |
|
1429 | def alias_table_validate(self,verbose=0): | |
1435 | """Update information about the alias table. |
|
1430 | """Update information about the alias table. | |
1436 |
|
1431 | |||
1437 | In particular, make sure no Python keywords/builtins are in it.""" |
|
1432 | In particular, make sure no Python keywords/builtins are in it.""" | |
1438 |
|
1433 | |||
1439 | no_alias = self.no_alias |
|
1434 | no_alias = self.no_alias | |
1440 | for k in self.alias_table.keys(): |
|
1435 | for k in self.alias_table.keys(): | |
1441 | if k in no_alias: |
|
1436 | if k in no_alias: | |
1442 | del self.alias_table[k] |
|
1437 | del self.alias_table[k] | |
1443 | if verbose: |
|
1438 | if verbose: | |
1444 | print ("Deleting alias <%s>, it's a Python " |
|
1439 | print ("Deleting alias <%s>, it's a Python " | |
1445 | "keyword or builtin." % k) |
|
1440 | "keyword or builtin." % k) | |
1446 |
|
1441 | |||
1447 | def set_next_input(self, s): |
|
1442 | def set_next_input(self, s): | |
1448 | """ Sets the 'default' input string for the next command line. |
|
1443 | """ Sets the 'default' input string for the next command line. | |
1449 |
|
1444 | |||
1450 | Requires readline. |
|
1445 | Requires readline. | |
1451 |
|
1446 | |||
1452 | Example: |
|
1447 | Example: | |
1453 |
|
1448 | |||
1454 | [D:\ipython]|1> _ip.set_next_input("Hello Word") |
|
1449 | [D:\ipython]|1> _ip.set_next_input("Hello Word") | |
1455 | [D:\ipython]|2> Hello Word_ # cursor is here |
|
1450 | [D:\ipython]|2> Hello Word_ # cursor is here | |
1456 | """ |
|
1451 | """ | |
1457 |
|
1452 | |||
1458 | self.rl_next_input = s |
|
1453 | self.rl_next_input = s | |
1459 |
|
1454 | |||
1460 | def set_autoindent(self,value=None): |
|
1455 | def set_autoindent(self,value=None): | |
1461 | """Set the autoindent flag, checking for readline support. |
|
1456 | """Set the autoindent flag, checking for readline support. | |
1462 |
|
1457 | |||
1463 | If called with no arguments, it acts as a toggle.""" |
|
1458 | If called with no arguments, it acts as a toggle.""" | |
1464 |
|
1459 | |||
1465 | if not self.has_readline: |
|
1460 | if not self.has_readline: | |
1466 | if os.name == 'posix': |
|
1461 | if os.name == 'posix': | |
1467 | warn("The auto-indent feature requires the readline library") |
|
1462 | warn("The auto-indent feature requires the readline library") | |
1468 | self.autoindent = 0 |
|
1463 | self.autoindent = 0 | |
1469 | return |
|
1464 | return | |
1470 | if value is None: |
|
1465 | if value is None: | |
1471 | self.autoindent = not self.autoindent |
|
1466 | self.autoindent = not self.autoindent | |
1472 | else: |
|
1467 | else: | |
1473 | self.autoindent = value |
|
1468 | self.autoindent = value | |
1474 |
|
1469 | |||
1475 | def atexit_operations(self): |
|
1470 | def atexit_operations(self): | |
1476 | """This will be executed at the time of exit. |
|
1471 | """This will be executed at the time of exit. | |
1477 |
|
1472 | |||
1478 | Saving of persistent data should be performed here. """ |
|
1473 | Saving of persistent data should be performed here. """ | |
1479 |
|
1474 | |||
1480 | #print '*** IPython exit cleanup ***' # dbg |
|
1475 | #print '*** IPython exit cleanup ***' # dbg | |
1481 | # input history |
|
1476 | # input history | |
1482 | self.savehist() |
|
1477 | self.savehist() | |
1483 |
|
1478 | |||
1484 | # Cleanup all tempfiles left around |
|
1479 | # Cleanup all tempfiles left around | |
1485 | for tfile in self.tempfiles: |
|
1480 | for tfile in self.tempfiles: | |
1486 | try: |
|
1481 | try: | |
1487 | os.unlink(tfile) |
|
1482 | os.unlink(tfile) | |
1488 | except OSError: |
|
1483 | except OSError: | |
1489 | pass |
|
1484 | pass | |
1490 |
|
1485 | |||
1491 | # Clear all user namespaces to release all references cleanly. |
|
1486 | # Clear all user namespaces to release all references cleanly. | |
1492 | self.reset() |
|
1487 | self.reset() | |
1493 |
|
1488 | |||
1494 | # Run user hooks |
|
1489 | # Run user hooks | |
1495 | self.hooks.shutdown_hook() |
|
1490 | self.hooks.shutdown_hook() | |
1496 |
|
1491 | |||
1497 | def reset(self): |
|
1492 | def reset(self): | |
1498 | """Clear all internal namespaces. |
|
1493 | """Clear all internal namespaces. | |
1499 |
|
1494 | |||
1500 | Note that this is much more aggressive than %reset, since it clears |
|
1495 | Note that this is much more aggressive than %reset, since it clears | |
1501 | fully all namespaces, as well as all input/output lists. |
|
1496 | fully all namespaces, as well as all input/output lists. | |
1502 | """ |
|
1497 | """ | |
1503 | for ns in self.ns_refs_table: |
|
1498 | for ns in self.ns_refs_table: | |
1504 | ns.clear() |
|
1499 | ns.clear() | |
1505 |
|
1500 | |||
1506 | # Clear input and output histories |
|
1501 | # Clear input and output histories | |
1507 | self.input_hist[:] = [] |
|
1502 | self.input_hist[:] = [] | |
1508 | self.input_hist_raw[:] = [] |
|
1503 | self.input_hist_raw[:] = [] | |
1509 | self.output_hist.clear() |
|
1504 | self.output_hist.clear() | |
1510 | # Restore the user namespaces to minimal usability |
|
1505 | # Restore the user namespaces to minimal usability | |
1511 | self.init_user_ns() |
|
1506 | self.init_user_ns() | |
1512 |
|
1507 | |||
1513 | def savehist(self): |
|
1508 | def savehist(self): | |
1514 | """Save input history to a file (via readline library).""" |
|
1509 | """Save input history to a file (via readline library).""" | |
1515 |
|
1510 | |||
1516 | if not self.has_readline: |
|
1511 | if not self.has_readline: | |
1517 | return |
|
1512 | return | |
1518 |
|
1513 | |||
1519 | try: |
|
1514 | try: | |
1520 | self.readline.write_history_file(self.histfile) |
|
1515 | self.readline.write_history_file(self.histfile) | |
1521 | except: |
|
1516 | except: | |
1522 | print 'Unable to save IPython command history to file: ' + \ |
|
1517 | print 'Unable to save IPython command history to file: ' + \ | |
1523 | `self.histfile` |
|
1518 | `self.histfile` | |
1524 |
|
1519 | |||
1525 | def reloadhist(self): |
|
1520 | def reloadhist(self): | |
1526 | """Reload the input history from disk file.""" |
|
1521 | """Reload the input history from disk file.""" | |
1527 |
|
1522 | |||
1528 | if self.has_readline: |
|
1523 | if self.has_readline: | |
1529 | try: |
|
1524 | try: | |
1530 | self.readline.clear_history() |
|
1525 | self.readline.clear_history() | |
1531 | self.readline.read_history_file(self.shell.histfile) |
|
1526 | self.readline.read_history_file(self.shell.histfile) | |
1532 | except AttributeError: |
|
1527 | except AttributeError: | |
1533 | pass |
|
1528 | pass | |
1534 |
|
1529 | |||
1535 |
|
1530 | |||
1536 | def history_saving_wrapper(self, func): |
|
1531 | def history_saving_wrapper(self, func): | |
1537 | """ Wrap func for readline history saving |
|
1532 | """ Wrap func for readline history saving | |
1538 |
|
1533 | |||
1539 | Convert func into callable that saves & restores |
|
1534 | Convert func into callable that saves & restores | |
1540 | history around the call """ |
|
1535 | history around the call """ | |
1541 |
|
1536 | |||
1542 | if not self.has_readline: |
|
1537 | if not self.has_readline: | |
1543 | return func |
|
1538 | return func | |
1544 |
|
1539 | |||
1545 | def wrapper(): |
|
1540 | def wrapper(): | |
1546 | self.savehist() |
|
1541 | self.savehist() | |
1547 | try: |
|
1542 | try: | |
1548 | func() |
|
1543 | func() | |
1549 | finally: |
|
1544 | finally: | |
1550 | readline.read_history_file(self.histfile) |
|
1545 | readline.read_history_file(self.histfile) | |
1551 | return wrapper |
|
1546 | return wrapper | |
1552 |
|
1547 | |||
1553 | def pre_readline(self): |
|
1548 | def pre_readline(self): | |
1554 | """readline hook to be used at the start of each line. |
|
1549 | """readline hook to be used at the start of each line. | |
1555 |
|
1550 | |||
1556 | Currently it handles auto-indent only.""" |
|
1551 | Currently it handles auto-indent only.""" | |
1557 |
|
1552 | |||
1558 | #debugx('self.indent_current_nsp','pre_readline:') |
|
1553 | #debugx('self.indent_current_nsp','pre_readline:') | |
1559 |
|
1554 | |||
1560 | if self.rl_do_indent: |
|
1555 | if self.rl_do_indent: | |
1561 | self.readline.insert_text(self.indent_current_str()) |
|
1556 | self.readline.insert_text(self.indent_current_str()) | |
1562 | if self.rl_next_input is not None: |
|
1557 | if self.rl_next_input is not None: | |
1563 | self.readline.insert_text(self.rl_next_input) |
|
1558 | self.readline.insert_text(self.rl_next_input) | |
1564 | self.rl_next_input = None |
|
1559 | self.rl_next_input = None | |
1565 |
|
1560 | |||
1566 | def ask_yes_no(self,prompt,default=True): |
|
1561 | def ask_yes_no(self,prompt,default=True): | |
1567 | if self.quiet: |
|
1562 | if self.quiet: | |
1568 | return True |
|
1563 | return True | |
1569 | return ask_yes_no(prompt,default) |
|
1564 | return ask_yes_no(prompt,default) | |
1570 |
|
1565 | |||
1571 | def new_main_mod(self,ns=None): |
|
1566 | def new_main_mod(self,ns=None): | |
1572 | """Return a new 'main' module object for user code execution. |
|
1567 | """Return a new 'main' module object for user code execution. | |
1573 | """ |
|
1568 | """ | |
1574 | main_mod = self._user_main_module |
|
1569 | main_mod = self._user_main_module | |
1575 | init_fakemod_dict(main_mod,ns) |
|
1570 | init_fakemod_dict(main_mod,ns) | |
1576 | return main_mod |
|
1571 | return main_mod | |
1577 |
|
1572 | |||
1578 | def cache_main_mod(self,ns,fname): |
|
1573 | def cache_main_mod(self,ns,fname): | |
1579 | """Cache a main module's namespace. |
|
1574 | """Cache a main module's namespace. | |
1580 |
|
1575 | |||
1581 | When scripts are executed via %run, we must keep a reference to the |
|
1576 | When scripts are executed via %run, we must keep a reference to the | |
1582 | namespace of their __main__ module (a FakeModule instance) around so |
|
1577 | namespace of their __main__ module (a FakeModule instance) around so | |
1583 | that Python doesn't clear it, rendering objects defined therein |
|
1578 | that Python doesn't clear it, rendering objects defined therein | |
1584 | useless. |
|
1579 | useless. | |
1585 |
|
1580 | |||
1586 | This method keeps said reference in a private dict, keyed by the |
|
1581 | This method keeps said reference in a private dict, keyed by the | |
1587 | absolute path of the module object (which corresponds to the script |
|
1582 | absolute path of the module object (which corresponds to the script | |
1588 | path). This way, for multiple executions of the same script we only |
|
1583 | path). This way, for multiple executions of the same script we only | |
1589 | keep one copy of the namespace (the last one), thus preventing memory |
|
1584 | keep one copy of the namespace (the last one), thus preventing memory | |
1590 | leaks from old references while allowing the objects from the last |
|
1585 | leaks from old references while allowing the objects from the last | |
1591 | execution to be accessible. |
|
1586 | execution to be accessible. | |
1592 |
|
1587 | |||
1593 | Note: we can not allow the actual FakeModule instances to be deleted, |
|
1588 | Note: we can not allow the actual FakeModule instances to be deleted, | |
1594 | because of how Python tears down modules (it hard-sets all their |
|
1589 | because of how Python tears down modules (it hard-sets all their | |
1595 | references to None without regard for reference counts). This method |
|
1590 | references to None without regard for reference counts). This method | |
1596 | must therefore make a *copy* of the given namespace, to allow the |
|
1591 | must therefore make a *copy* of the given namespace, to allow the | |
1597 | original module's __dict__ to be cleared and reused. |
|
1592 | original module's __dict__ to be cleared and reused. | |
1598 |
|
1593 | |||
1599 |
|
1594 | |||
1600 | Parameters |
|
1595 | Parameters | |
1601 | ---------- |
|
1596 | ---------- | |
1602 | ns : a namespace (a dict, typically) |
|
1597 | ns : a namespace (a dict, typically) | |
1603 |
|
1598 | |||
1604 | fname : str |
|
1599 | fname : str | |
1605 | Filename associated with the namespace. |
|
1600 | Filename associated with the namespace. | |
1606 |
|
1601 | |||
1607 | Examples |
|
1602 | Examples | |
1608 | -------- |
|
1603 | -------- | |
1609 |
|
1604 | |||
1610 | In [10]: import IPython |
|
1605 | In [10]: import IPython | |
1611 |
|
1606 | |||
1612 | In [11]: _ip.cache_main_mod(IPython.__dict__,IPython.__file__) |
|
1607 | In [11]: _ip.cache_main_mod(IPython.__dict__,IPython.__file__) | |
1613 |
|
1608 | |||
1614 | In [12]: IPython.__file__ in _ip._main_ns_cache |
|
1609 | In [12]: IPython.__file__ in _ip._main_ns_cache | |
1615 | Out[12]: True |
|
1610 | Out[12]: True | |
1616 | """ |
|
1611 | """ | |
1617 | self._main_ns_cache[os.path.abspath(fname)] = ns.copy() |
|
1612 | self._main_ns_cache[os.path.abspath(fname)] = ns.copy() | |
1618 |
|
1613 | |||
1619 | def clear_main_mod_cache(self): |
|
1614 | def clear_main_mod_cache(self): | |
1620 | """Clear the cache of main modules. |
|
1615 | """Clear the cache of main modules. | |
1621 |
|
1616 | |||
1622 | Mainly for use by utilities like %reset. |
|
1617 | Mainly for use by utilities like %reset. | |
1623 |
|
1618 | |||
1624 | Examples |
|
1619 | Examples | |
1625 | -------- |
|
1620 | -------- | |
1626 |
|
1621 | |||
1627 | In [15]: import IPython |
|
1622 | In [15]: import IPython | |
1628 |
|
1623 | |||
1629 | In [16]: _ip.cache_main_mod(IPython.__dict__,IPython.__file__) |
|
1624 | In [16]: _ip.cache_main_mod(IPython.__dict__,IPython.__file__) | |
1630 |
|
1625 | |||
1631 | In [17]: len(_ip._main_ns_cache) > 0 |
|
1626 | In [17]: len(_ip._main_ns_cache) > 0 | |
1632 | Out[17]: True |
|
1627 | Out[17]: True | |
1633 |
|
1628 | |||
1634 | In [18]: _ip.clear_main_mod_cache() |
|
1629 | In [18]: _ip.clear_main_mod_cache() | |
1635 |
|
1630 | |||
1636 | In [19]: len(_ip._main_ns_cache) == 0 |
|
1631 | In [19]: len(_ip._main_ns_cache) == 0 | |
1637 | Out[19]: True |
|
1632 | Out[19]: True | |
1638 | """ |
|
1633 | """ | |
1639 | self._main_ns_cache.clear() |
|
1634 | self._main_ns_cache.clear() | |
1640 |
|
1635 | |||
1641 | def _should_recompile(self,e): |
|
1636 | def _should_recompile(self,e): | |
1642 | """Utility routine for edit_syntax_error""" |
|
1637 | """Utility routine for edit_syntax_error""" | |
1643 |
|
1638 | |||
1644 | if e.filename in ('<ipython console>','<input>','<string>', |
|
1639 | if e.filename in ('<ipython console>','<input>','<string>', | |
1645 | '<console>','<BackgroundJob compilation>', |
|
1640 | '<console>','<BackgroundJob compilation>', | |
1646 | None): |
|
1641 | None): | |
1647 |
|
1642 | |||
1648 | return False |
|
1643 | return False | |
1649 | try: |
|
1644 | try: | |
1650 | if (self.autoedit_syntax and |
|
1645 | if (self.autoedit_syntax and | |
1651 | not self.ask_yes_no('Return to editor to correct syntax error? ' |
|
1646 | not self.ask_yes_no('Return to editor to correct syntax error? ' | |
1652 | '[Y/n] ','y')): |
|
1647 | '[Y/n] ','y')): | |
1653 | return False |
|
1648 | return False | |
1654 | except EOFError: |
|
1649 | except EOFError: | |
1655 | return False |
|
1650 | return False | |
1656 |
|
1651 | |||
1657 | def int0(x): |
|
1652 | def int0(x): | |
1658 | try: |
|
1653 | try: | |
1659 | return int(x) |
|
1654 | return int(x) | |
1660 | except TypeError: |
|
1655 | except TypeError: | |
1661 | return 0 |
|
1656 | return 0 | |
1662 | # always pass integer line and offset values to editor hook |
|
1657 | # always pass integer line and offset values to editor hook | |
1663 | try: |
|
1658 | try: | |
1664 | self.hooks.fix_error_editor(e.filename, |
|
1659 | self.hooks.fix_error_editor(e.filename, | |
1665 | int0(e.lineno),int0(e.offset),e.msg) |
|
1660 | int0(e.lineno),int0(e.offset),e.msg) | |
1666 | except TryNext: |
|
1661 | except TryNext: | |
1667 | warn('Could not open editor') |
|
1662 | warn('Could not open editor') | |
1668 | return False |
|
1663 | return False | |
1669 | return True |
|
1664 | return True | |
1670 |
|
1665 | |||
1671 | def edit_syntax_error(self): |
|
1666 | def edit_syntax_error(self): | |
1672 | """The bottom half of the syntax error handler called in the main loop. |
|
1667 | """The bottom half of the syntax error handler called in the main loop. | |
1673 |
|
1668 | |||
1674 | Loop until syntax error is fixed or user cancels. |
|
1669 | Loop until syntax error is fixed or user cancels. | |
1675 | """ |
|
1670 | """ | |
1676 |
|
1671 | |||
1677 | while self.SyntaxTB.last_syntax_error: |
|
1672 | while self.SyntaxTB.last_syntax_error: | |
1678 | # copy and clear last_syntax_error |
|
1673 | # copy and clear last_syntax_error | |
1679 | err = self.SyntaxTB.clear_err_state() |
|
1674 | err = self.SyntaxTB.clear_err_state() | |
1680 | if not self._should_recompile(err): |
|
1675 | if not self._should_recompile(err): | |
1681 | return |
|
1676 | return | |
1682 | try: |
|
1677 | try: | |
1683 | # may set last_syntax_error again if a SyntaxError is raised |
|
1678 | # may set last_syntax_error again if a SyntaxError is raised | |
1684 | self.safe_execfile(err.filename,self.user_ns) |
|
1679 | self.safe_execfile(err.filename,self.user_ns) | |
1685 | except: |
|
1680 | except: | |
1686 | self.showtraceback() |
|
1681 | self.showtraceback() | |
1687 | else: |
|
1682 | else: | |
1688 | try: |
|
1683 | try: | |
1689 | f = file(err.filename) |
|
1684 | f = file(err.filename) | |
1690 | try: |
|
1685 | try: | |
|
1686 | # This should be inside a display_trap block and I | |||
|
1687 | # think it is. | |||
1691 | sys.displayhook(f.read()) |
|
1688 | sys.displayhook(f.read()) | |
1692 | finally: |
|
1689 | finally: | |
1693 | f.close() |
|
1690 | f.close() | |
1694 | except: |
|
1691 | except: | |
1695 | self.showtraceback() |
|
1692 | self.showtraceback() | |
1696 |
|
1693 | |||
1697 | def showsyntaxerror(self, filename=None): |
|
1694 | def showsyntaxerror(self, filename=None): | |
1698 | """Display the syntax error that just occurred. |
|
1695 | """Display the syntax error that just occurred. | |
1699 |
|
1696 | |||
1700 | This doesn't display a stack trace because there isn't one. |
|
1697 | This doesn't display a stack trace because there isn't one. | |
1701 |
|
1698 | |||
1702 | If a filename is given, it is stuffed in the exception instead |
|
1699 | If a filename is given, it is stuffed in the exception instead | |
1703 | of what was there before (because Python's parser always uses |
|
1700 | of what was there before (because Python's parser always uses | |
1704 | "<string>" when reading from a string). |
|
1701 | "<string>" when reading from a string). | |
1705 | """ |
|
1702 | """ | |
1706 | etype, value, last_traceback = sys.exc_info() |
|
1703 | etype, value, last_traceback = sys.exc_info() | |
1707 |
|
1704 | |||
1708 | # See note about these variables in showtraceback() below |
|
1705 | # See note about these variables in showtraceback() below | |
1709 | sys.last_type = etype |
|
1706 | sys.last_type = etype | |
1710 | sys.last_value = value |
|
1707 | sys.last_value = value | |
1711 | sys.last_traceback = last_traceback |
|
1708 | sys.last_traceback = last_traceback | |
1712 |
|
1709 | |||
1713 | if filename and etype is SyntaxError: |
|
1710 | if filename and etype is SyntaxError: | |
1714 | # Work hard to stuff the correct filename in the exception |
|
1711 | # Work hard to stuff the correct filename in the exception | |
1715 | try: |
|
1712 | try: | |
1716 | msg, (dummy_filename, lineno, offset, line) = value |
|
1713 | msg, (dummy_filename, lineno, offset, line) = value | |
1717 | except: |
|
1714 | except: | |
1718 | # Not the format we expect; leave it alone |
|
1715 | # Not the format we expect; leave it alone | |
1719 | pass |
|
1716 | pass | |
1720 | else: |
|
1717 | else: | |
1721 | # Stuff in the right filename |
|
1718 | # Stuff in the right filename | |
1722 | try: |
|
1719 | try: | |
1723 | # Assume SyntaxError is a class exception |
|
1720 | # Assume SyntaxError is a class exception | |
1724 | value = SyntaxError(msg, (filename, lineno, offset, line)) |
|
1721 | value = SyntaxError(msg, (filename, lineno, offset, line)) | |
1725 | except: |
|
1722 | except: | |
1726 | # If that failed, assume SyntaxError is a string |
|
1723 | # If that failed, assume SyntaxError is a string | |
1727 | value = msg, (filename, lineno, offset, line) |
|
1724 | value = msg, (filename, lineno, offset, line) | |
1728 | self.SyntaxTB(etype,value,[]) |
|
1725 | self.SyntaxTB(etype,value,[]) | |
1729 |
|
1726 | |||
1730 | def debugger(self,force=False): |
|
1727 | def debugger(self,force=False): | |
1731 | """Call the pydb/pdb debugger. |
|
1728 | """Call the pydb/pdb debugger. | |
1732 |
|
1729 | |||
1733 | Keywords: |
|
1730 | Keywords: | |
1734 |
|
1731 | |||
1735 | - force(False): by default, this routine checks the instance call_pdb |
|
1732 | - force(False): by default, this routine checks the instance call_pdb | |
1736 | flag and does not actually invoke the debugger if the flag is false. |
|
1733 | flag and does not actually invoke the debugger if the flag is false. | |
1737 | The 'force' option forces the debugger to activate even if the flag |
|
1734 | The 'force' option forces the debugger to activate even if the flag | |
1738 | is false. |
|
1735 | is false. | |
1739 | """ |
|
1736 | """ | |
1740 |
|
1737 | |||
1741 | if not (force or self.call_pdb): |
|
1738 | if not (force or self.call_pdb): | |
1742 | return |
|
1739 | return | |
1743 |
|
1740 | |||
1744 | if not hasattr(sys,'last_traceback'): |
|
1741 | if not hasattr(sys,'last_traceback'): | |
1745 | error('No traceback has been produced, nothing to debug.') |
|
1742 | error('No traceback has been produced, nothing to debug.') | |
1746 | return |
|
1743 | return | |
1747 |
|
1744 | |||
1748 | # use pydb if available |
|
1745 | # use pydb if available | |
1749 | if debugger.has_pydb: |
|
1746 | if debugger.has_pydb: | |
1750 | from pydb import pm |
|
1747 | from pydb import pm | |
1751 | else: |
|
1748 | else: | |
1752 | # fallback to our internal debugger |
|
1749 | # fallback to our internal debugger | |
1753 | pm = lambda : self.InteractiveTB.debugger(force=True) |
|
1750 | pm = lambda : self.InteractiveTB.debugger(force=True) | |
1754 | self.history_saving_wrapper(pm)() |
|
1751 | self.history_saving_wrapper(pm)() | |
1755 |
|
1752 | |||
1756 | def showtraceback(self,exc_tuple = None,filename=None,tb_offset=None): |
|
1753 | def showtraceback(self,exc_tuple = None,filename=None,tb_offset=None): | |
1757 | """Display the exception that just occurred. |
|
1754 | """Display the exception that just occurred. | |
1758 |
|
1755 | |||
1759 | If nothing is known about the exception, this is the method which |
|
1756 | If nothing is known about the exception, this is the method which | |
1760 | should be used throughout the code for presenting user tracebacks, |
|
1757 | should be used throughout the code for presenting user tracebacks, | |
1761 | rather than directly invoking the InteractiveTB object. |
|
1758 | rather than directly invoking the InteractiveTB object. | |
1762 |
|
1759 | |||
1763 | A specific showsyntaxerror() also exists, but this method can take |
|
1760 | A specific showsyntaxerror() also exists, but this method can take | |
1764 | care of calling it if needed, so unless you are explicitly catching a |
|
1761 | care of calling it if needed, so unless you are explicitly catching a | |
1765 | SyntaxError exception, don't try to analyze the stack manually and |
|
1762 | SyntaxError exception, don't try to analyze the stack manually and | |
1766 | simply call this method.""" |
|
1763 | simply call this method.""" | |
1767 |
|
1764 | |||
1768 |
|
1765 | |||
1769 | # Though this won't be called by syntax errors in the input line, |
|
1766 | # Though this won't be called by syntax errors in the input line, | |
1770 | # there may be SyntaxError cases whith imported code. |
|
1767 | # there may be SyntaxError cases whith imported code. | |
1771 |
|
1768 | |||
1772 | try: |
|
1769 | try: | |
1773 | if exc_tuple is None: |
|
1770 | if exc_tuple is None: | |
1774 | etype, value, tb = sys.exc_info() |
|
1771 | etype, value, tb = sys.exc_info() | |
1775 | else: |
|
1772 | else: | |
1776 | etype, value, tb = exc_tuple |
|
1773 | etype, value, tb = exc_tuple | |
1777 |
|
1774 | |||
1778 | if etype is SyntaxError: |
|
1775 | if etype is SyntaxError: | |
1779 | self.showsyntaxerror(filename) |
|
1776 | self.showsyntaxerror(filename) | |
1780 | elif etype is UsageError: |
|
1777 | elif etype is UsageError: | |
1781 | print "UsageError:", value |
|
1778 | print "UsageError:", value | |
1782 | else: |
|
1779 | else: | |
1783 | # WARNING: these variables are somewhat deprecated and not |
|
1780 | # WARNING: these variables are somewhat deprecated and not | |
1784 | # necessarily safe to use in a threaded environment, but tools |
|
1781 | # necessarily safe to use in a threaded environment, but tools | |
1785 | # like pdb depend on their existence, so let's set them. If we |
|
1782 | # like pdb depend on their existence, so let's set them. If we | |
1786 | # find problems in the field, we'll need to revisit their use. |
|
1783 | # find problems in the field, we'll need to revisit their use. | |
1787 | sys.last_type = etype |
|
1784 | sys.last_type = etype | |
1788 | sys.last_value = value |
|
1785 | sys.last_value = value | |
1789 | sys.last_traceback = tb |
|
1786 | sys.last_traceback = tb | |
1790 |
|
1787 | |||
1791 | if etype in self.custom_exceptions: |
|
1788 | if etype in self.custom_exceptions: | |
1792 | self.CustomTB(etype,value,tb) |
|
1789 | self.CustomTB(etype,value,tb) | |
1793 | else: |
|
1790 | else: | |
1794 | self.InteractiveTB(etype,value,tb,tb_offset=tb_offset) |
|
1791 | self.InteractiveTB(etype,value,tb,tb_offset=tb_offset) | |
1795 | if self.InteractiveTB.call_pdb and self.has_readline: |
|
1792 | if self.InteractiveTB.call_pdb and self.has_readline: | |
1796 | # pdb mucks up readline, fix it back |
|
1793 | # pdb mucks up readline, fix it back | |
1797 | self.set_completer() |
|
1794 | self.set_completer() | |
1798 | except KeyboardInterrupt: |
|
1795 | except KeyboardInterrupt: | |
1799 | self.write("\nKeyboardInterrupt\n") |
|
1796 | self.write("\nKeyboardInterrupt\n") | |
1800 |
|
1797 | |||
1801 | def mainloop(self, banner=None): |
|
1798 | def mainloop(self, banner=None): | |
1802 | """Start the mainloop. |
|
1799 | """Start the mainloop. | |
1803 |
|
1800 | |||
1804 | If an optional banner argument is given, it will override the |
|
1801 | If an optional banner argument is given, it will override the | |
1805 | internally created default banner. |
|
1802 | internally created default banner. | |
1806 | """ |
|
1803 | """ | |
1807 |
|
1804 | |||
1808 | with self.builtin_trap: |
|
1805 | with nested(self.builtin_trap, self.display_trap): | |
1809 | if self.c: # Emulate Python's -c option |
|
1806 | if self.c: # Emulate Python's -c option | |
1810 | self.exec_init_cmd() |
|
1807 | self.exec_init_cmd() | |
1811 |
|
1808 | |||
1812 | if self.display_banner: |
|
1809 | if self.display_banner: | |
1813 | if banner is None: |
|
1810 | if banner is None: | |
1814 | banner = self.banner |
|
1811 | banner = self.banner | |
1815 |
|
1812 | |||
1816 | # if you run stuff with -c <cmd>, raw hist is not updated |
|
1813 | # if you run stuff with -c <cmd>, raw hist is not updated | |
1817 | # ensure that it's in sync |
|
1814 | # ensure that it's in sync | |
1818 | if len(self.input_hist) != len (self.input_hist_raw): |
|
1815 | if len(self.input_hist) != len (self.input_hist_raw): | |
1819 | self.input_hist_raw = InputList(self.input_hist) |
|
1816 | self.input_hist_raw = InputList(self.input_hist) | |
1820 |
|
1817 | |||
1821 | while 1: |
|
1818 | while 1: | |
1822 | try: |
|
1819 | try: | |
1823 | self.interact() |
|
1820 | self.interact() | |
1824 | #self.interact_with_readline() |
|
1821 | #self.interact_with_readline() | |
1825 | # XXX for testing of a readline-decoupled repl loop, call |
|
1822 | # XXX for testing of a readline-decoupled repl loop, call | |
1826 | # interact_with_readline above |
|
1823 | # interact_with_readline above | |
1827 | break |
|
1824 | break | |
1828 | except KeyboardInterrupt: |
|
1825 | except KeyboardInterrupt: | |
1829 | # this should not be necessary, but KeyboardInterrupt |
|
1826 | # this should not be necessary, but KeyboardInterrupt | |
1830 | # handling seems rather unpredictable... |
|
1827 | # handling seems rather unpredictable... | |
1831 | self.write("\nKeyboardInterrupt in interact()\n") |
|
1828 | self.write("\nKeyboardInterrupt in interact()\n") | |
1832 |
|
1829 | |||
1833 | def exec_init_cmd(self): |
|
1830 | def exec_init_cmd(self): | |
1834 | """Execute a command given at the command line. |
|
1831 | """Execute a command given at the command line. | |
1835 |
|
1832 | |||
1836 | This emulates Python's -c option.""" |
|
1833 | This emulates Python's -c option.""" | |
1837 |
|
1834 | |||
1838 | #sys.argv = ['-c'] |
|
1835 | #sys.argv = ['-c'] | |
1839 | self.push_line(self.prefilter(self.c, False)) |
|
1836 | self.push_line(self.prefilter(self.c, False)) | |
1840 | if not self.interactive: |
|
1837 | if not self.interactive: | |
1841 | self.ask_exit() |
|
1838 | self.ask_exit() | |
1842 |
|
1839 | |||
1843 | def interact_prompt(self): |
|
1840 | def interact_prompt(self): | |
1844 | """ Print the prompt (in read-eval-print loop) |
|
1841 | """ Print the prompt (in read-eval-print loop) | |
1845 |
|
1842 | |||
1846 | Provided for those who want to implement their own read-eval-print loop (e.g. GUIs), not |
|
1843 | Provided for those who want to implement their own read-eval-print loop (e.g. GUIs), not | |
1847 | used in standard IPython flow. |
|
1844 | used in standard IPython flow. | |
1848 | """ |
|
1845 | """ | |
1849 | if self.more: |
|
1846 | if self.more: | |
1850 | try: |
|
1847 | try: | |
1851 | prompt = self.hooks.generate_prompt(True) |
|
1848 | prompt = self.hooks.generate_prompt(True) | |
1852 | except: |
|
1849 | except: | |
1853 | self.showtraceback() |
|
1850 | self.showtraceback() | |
1854 | if self.autoindent: |
|
1851 | if self.autoindent: | |
1855 | self.rl_do_indent = True |
|
1852 | self.rl_do_indent = True | |
1856 |
|
1853 | |||
1857 | else: |
|
1854 | else: | |
1858 | try: |
|
1855 | try: | |
1859 | prompt = self.hooks.generate_prompt(False) |
|
1856 | prompt = self.hooks.generate_prompt(False) | |
1860 | except: |
|
1857 | except: | |
1861 | self.showtraceback() |
|
1858 | self.showtraceback() | |
1862 | self.write(prompt) |
|
1859 | self.write(prompt) | |
1863 |
|
1860 | |||
1864 | def interact_handle_input(self,line): |
|
1861 | def interact_handle_input(self,line): | |
1865 | """ Handle the input line (in read-eval-print loop) |
|
1862 | """ Handle the input line (in read-eval-print loop) | |
1866 |
|
1863 | |||
1867 | Provided for those who want to implement their own read-eval-print loop (e.g. GUIs), not |
|
1864 | Provided for those who want to implement their own read-eval-print loop (e.g. GUIs), not | |
1868 | used in standard IPython flow. |
|
1865 | used in standard IPython flow. | |
1869 | """ |
|
1866 | """ | |
1870 | if line.lstrip() == line: |
|
1867 | if line.lstrip() == line: | |
1871 | self.shadowhist.add(line.strip()) |
|
1868 | self.shadowhist.add(line.strip()) | |
1872 | lineout = self.prefilter(line,self.more) |
|
1869 | lineout = self.prefilter(line,self.more) | |
1873 |
|
1870 | |||
1874 | if line.strip(): |
|
1871 | if line.strip(): | |
1875 | if self.more: |
|
1872 | if self.more: | |
1876 | self.input_hist_raw[-1] += '%s\n' % line |
|
1873 | self.input_hist_raw[-1] += '%s\n' % line | |
1877 | else: |
|
1874 | else: | |
1878 | self.input_hist_raw.append('%s\n' % line) |
|
1875 | self.input_hist_raw.append('%s\n' % line) | |
1879 |
|
1876 | |||
1880 |
|
1877 | |||
1881 | self.more = self.push_line(lineout) |
|
1878 | self.more = self.push_line(lineout) | |
1882 | if (self.SyntaxTB.last_syntax_error and |
|
1879 | if (self.SyntaxTB.last_syntax_error and | |
1883 | self.autoedit_syntax): |
|
1880 | self.autoedit_syntax): | |
1884 | self.edit_syntax_error() |
|
1881 | self.edit_syntax_error() | |
1885 |
|
1882 | |||
1886 | def interact_with_readline(self): |
|
1883 | def interact_with_readline(self): | |
1887 | """ Demo of using interact_handle_input, interact_prompt |
|
1884 | """ Demo of using interact_handle_input, interact_prompt | |
1888 |
|
1885 | |||
1889 | This is the main read-eval-print loop. If you need to implement your own (e.g. for GUI), |
|
1886 | This is the main read-eval-print loop. If you need to implement your own (e.g. for GUI), | |
1890 | it should work like this. |
|
1887 | it should work like this. | |
1891 | """ |
|
1888 | """ | |
1892 | self.readline_startup_hook(self.pre_readline) |
|
1889 | self.readline_startup_hook(self.pre_readline) | |
1893 | while not self.exit_now: |
|
1890 | while not self.exit_now: | |
1894 | self.interact_prompt() |
|
1891 | self.interact_prompt() | |
1895 | if self.more: |
|
1892 | if self.more: | |
1896 | self.rl_do_indent = True |
|
1893 | self.rl_do_indent = True | |
1897 | else: |
|
1894 | else: | |
1898 | self.rl_do_indent = False |
|
1895 | self.rl_do_indent = False | |
1899 | line = raw_input_original().decode(self.stdin_encoding) |
|
1896 | line = raw_input_original().decode(self.stdin_encoding) | |
1900 | self.interact_handle_input(line) |
|
1897 | self.interact_handle_input(line) | |
1901 |
|
1898 | |||
1902 | def interact(self, banner=None): |
|
1899 | def interact(self, banner=None): | |
1903 | """Closely emulate the interactive Python console.""" |
|
1900 | """Closely emulate the interactive Python console.""" | |
1904 |
|
1901 | |||
1905 | # batch run -> do not interact |
|
1902 | # batch run -> do not interact | |
1906 | if self.exit_now: |
|
1903 | if self.exit_now: | |
1907 | return |
|
1904 | return | |
1908 |
|
1905 | |||
1909 | if self.display_banner: |
|
1906 | if self.display_banner: | |
1910 | if banner is None: |
|
1907 | if banner is None: | |
1911 | banner = self.banner |
|
1908 | banner = self.banner | |
1912 | self.write(banner) |
|
1909 | self.write(banner) | |
1913 |
|
1910 | |||
1914 | more = 0 |
|
1911 | more = 0 | |
1915 |
|
1912 | |||
1916 | # Mark activity in the builtins |
|
1913 | # Mark activity in the builtins | |
1917 | __builtin__.__dict__['__IPYTHON__active'] += 1 |
|
1914 | __builtin__.__dict__['__IPYTHON__active'] += 1 | |
1918 |
|
1915 | |||
1919 | if self.has_readline: |
|
1916 | if self.has_readline: | |
1920 | self.readline_startup_hook(self.pre_readline) |
|
1917 | self.readline_startup_hook(self.pre_readline) | |
1921 | # exit_now is set by a call to %Exit or %Quit, through the |
|
1918 | # exit_now is set by a call to %Exit or %Quit, through the | |
1922 | # ask_exit callback. |
|
1919 | # ask_exit callback. | |
1923 |
|
1920 | |||
1924 | while not self.exit_now: |
|
1921 | while not self.exit_now: | |
1925 | self.hooks.pre_prompt_hook() |
|
1922 | self.hooks.pre_prompt_hook() | |
1926 | if more: |
|
1923 | if more: | |
1927 | try: |
|
1924 | try: | |
1928 | prompt = self.hooks.generate_prompt(True) |
|
1925 | prompt = self.hooks.generate_prompt(True) | |
1929 | except: |
|
1926 | except: | |
1930 | self.showtraceback() |
|
1927 | self.showtraceback() | |
1931 | if self.autoindent: |
|
1928 | if self.autoindent: | |
1932 | self.rl_do_indent = True |
|
1929 | self.rl_do_indent = True | |
1933 |
|
1930 | |||
1934 | else: |
|
1931 | else: | |
1935 | try: |
|
1932 | try: | |
1936 | prompt = self.hooks.generate_prompt(False) |
|
1933 | prompt = self.hooks.generate_prompt(False) | |
1937 | except: |
|
1934 | except: | |
1938 | self.showtraceback() |
|
1935 | self.showtraceback() | |
1939 | try: |
|
1936 | try: | |
1940 | line = self.raw_input(prompt, more) |
|
1937 | line = self.raw_input(prompt, more) | |
1941 | if self.exit_now: |
|
1938 | if self.exit_now: | |
1942 | # quick exit on sys.std[in|out] close |
|
1939 | # quick exit on sys.std[in|out] close | |
1943 | break |
|
1940 | break | |
1944 | if self.autoindent: |
|
1941 | if self.autoindent: | |
1945 | self.rl_do_indent = False |
|
1942 | self.rl_do_indent = False | |
1946 |
|
1943 | |||
1947 | except KeyboardInterrupt: |
|
1944 | except KeyboardInterrupt: | |
1948 | #double-guard against keyboardinterrupts during kbdint handling |
|
1945 | #double-guard against keyboardinterrupts during kbdint handling | |
1949 | try: |
|
1946 | try: | |
1950 | self.write('\nKeyboardInterrupt\n') |
|
1947 | self.write('\nKeyboardInterrupt\n') | |
1951 | self.resetbuffer() |
|
1948 | self.resetbuffer() | |
1952 | # keep cache in sync with the prompt counter: |
|
1949 | # keep cache in sync with the prompt counter: | |
1953 | self.outputcache.prompt_count -= 1 |
|
1950 | self.outputcache.prompt_count -= 1 | |
1954 |
|
1951 | |||
1955 | if self.autoindent: |
|
1952 | if self.autoindent: | |
1956 | self.indent_current_nsp = 0 |
|
1953 | self.indent_current_nsp = 0 | |
1957 | more = 0 |
|
1954 | more = 0 | |
1958 | except KeyboardInterrupt: |
|
1955 | except KeyboardInterrupt: | |
1959 | pass |
|
1956 | pass | |
1960 | except EOFError: |
|
1957 | except EOFError: | |
1961 | if self.autoindent: |
|
1958 | if self.autoindent: | |
1962 | self.rl_do_indent = False |
|
1959 | self.rl_do_indent = False | |
1963 | self.readline_startup_hook(None) |
|
1960 | self.readline_startup_hook(None) | |
1964 | self.write('\n') |
|
1961 | self.write('\n') | |
1965 | self.exit() |
|
1962 | self.exit() | |
1966 | except bdb.BdbQuit: |
|
1963 | except bdb.BdbQuit: | |
1967 | warn('The Python debugger has exited with a BdbQuit exception.\n' |
|
1964 | warn('The Python debugger has exited with a BdbQuit exception.\n' | |
1968 | 'Because of how pdb handles the stack, it is impossible\n' |
|
1965 | 'Because of how pdb handles the stack, it is impossible\n' | |
1969 | 'for IPython to properly format this particular exception.\n' |
|
1966 | 'for IPython to properly format this particular exception.\n' | |
1970 | 'IPython will resume normal operation.') |
|
1967 | 'IPython will resume normal operation.') | |
1971 | except: |
|
1968 | except: | |
1972 | # exceptions here are VERY RARE, but they can be triggered |
|
1969 | # exceptions here are VERY RARE, but they can be triggered | |
1973 | # asynchronously by signal handlers, for example. |
|
1970 | # asynchronously by signal handlers, for example. | |
1974 | self.showtraceback() |
|
1971 | self.showtraceback() | |
1975 | else: |
|
1972 | else: | |
1976 | more = self.push_line(line) |
|
1973 | more = self.push_line(line) | |
1977 | if (self.SyntaxTB.last_syntax_error and |
|
1974 | if (self.SyntaxTB.last_syntax_error and | |
1978 | self.autoedit_syntax): |
|
1975 | self.autoedit_syntax): | |
1979 | self.edit_syntax_error() |
|
1976 | self.edit_syntax_error() | |
1980 |
|
1977 | |||
1981 | # We are off again... |
|
1978 | # We are off again... | |
1982 | __builtin__.__dict__['__IPYTHON__active'] -= 1 |
|
1979 | __builtin__.__dict__['__IPYTHON__active'] -= 1 | |
1983 |
|
1980 | |||
1984 | def excepthook(self, etype, value, tb): |
|
1981 | def excepthook(self, etype, value, tb): | |
1985 | """One more defense for GUI apps that call sys.excepthook. |
|
1982 | """One more defense for GUI apps that call sys.excepthook. | |
1986 |
|
1983 | |||
1987 | GUI frameworks like wxPython trap exceptions and call |
|
1984 | GUI frameworks like wxPython trap exceptions and call | |
1988 | sys.excepthook themselves. I guess this is a feature that |
|
1985 | sys.excepthook themselves. I guess this is a feature that | |
1989 | enables them to keep running after exceptions that would |
|
1986 | enables them to keep running after exceptions that would | |
1990 | otherwise kill their mainloop. This is a bother for IPython |
|
1987 | otherwise kill their mainloop. This is a bother for IPython | |
1991 | which excepts to catch all of the program exceptions with a try: |
|
1988 | which excepts to catch all of the program exceptions with a try: | |
1992 | except: statement. |
|
1989 | except: statement. | |
1993 |
|
1990 | |||
1994 | Normally, IPython sets sys.excepthook to a CrashHandler instance, so if |
|
1991 | Normally, IPython sets sys.excepthook to a CrashHandler instance, so if | |
1995 | any app directly invokes sys.excepthook, it will look to the user like |
|
1992 | any app directly invokes sys.excepthook, it will look to the user like | |
1996 | IPython crashed. In order to work around this, we can disable the |
|
1993 | IPython crashed. In order to work around this, we can disable the | |
1997 | CrashHandler and replace it with this excepthook instead, which prints a |
|
1994 | CrashHandler and replace it with this excepthook instead, which prints a | |
1998 | regular traceback using our InteractiveTB. In this fashion, apps which |
|
1995 | regular traceback using our InteractiveTB. In this fashion, apps which | |
1999 | call sys.excepthook will generate a regular-looking exception from |
|
1996 | call sys.excepthook will generate a regular-looking exception from | |
2000 | IPython, and the CrashHandler will only be triggered by real IPython |
|
1997 | IPython, and the CrashHandler will only be triggered by real IPython | |
2001 | crashes. |
|
1998 | crashes. | |
2002 |
|
1999 | |||
2003 | This hook should be used sparingly, only in places which are not likely |
|
2000 | This hook should be used sparingly, only in places which are not likely | |
2004 | to be true IPython errors. |
|
2001 | to be true IPython errors. | |
2005 | """ |
|
2002 | """ | |
2006 | self.showtraceback((etype,value,tb),tb_offset=0) |
|
2003 | self.showtraceback((etype,value,tb),tb_offset=0) | |
2007 |
|
2004 | |||
2008 | def expand_alias(self, line): |
|
2005 | def expand_alias(self, line): | |
2009 | """ Expand an alias in the command line |
|
2006 | """ Expand an alias in the command line | |
2010 |
|
2007 | |||
2011 | Returns the provided command line, possibly with the first word |
|
2008 | Returns the provided command line, possibly with the first word | |
2012 | (command) translated according to alias expansion rules. |
|
2009 | (command) translated according to alias expansion rules. | |
2013 |
|
2010 | |||
2014 | [ipython]|16> _ip.expand_aliases("np myfile.txt") |
|
2011 | [ipython]|16> _ip.expand_aliases("np myfile.txt") | |
2015 | <16> 'q:/opt/np/notepad++.exe myfile.txt' |
|
2012 | <16> 'q:/opt/np/notepad++.exe myfile.txt' | |
2016 | """ |
|
2013 | """ | |
2017 |
|
2014 | |||
2018 | pre,fn,rest = self.split_user_input(line) |
|
2015 | pre,fn,rest = self.split_user_input(line) | |
2019 | res = pre + self.expand_aliases(fn, rest) |
|
2016 | res = pre + self.expand_aliases(fn, rest) | |
2020 | return res |
|
2017 | return res | |
2021 |
|
2018 | |||
2022 | def expand_aliases(self, fn, rest): |
|
2019 | def expand_aliases(self, fn, rest): | |
2023 | """Expand multiple levels of aliases: |
|
2020 | """Expand multiple levels of aliases: | |
2024 |
|
2021 | |||
2025 | if: |
|
2022 | if: | |
2026 |
|
2023 | |||
2027 | alias foo bar /tmp |
|
2024 | alias foo bar /tmp | |
2028 | alias baz foo |
|
2025 | alias baz foo | |
2029 |
|
2026 | |||
2030 | then: |
|
2027 | then: | |
2031 |
|
2028 | |||
2032 | baz huhhahhei -> bar /tmp huhhahhei |
|
2029 | baz huhhahhei -> bar /tmp huhhahhei | |
2033 |
|
2030 | |||
2034 | """ |
|
2031 | """ | |
2035 | line = fn + " " + rest |
|
2032 | line = fn + " " + rest | |
2036 |
|
2033 | |||
2037 | done = set() |
|
2034 | done = set() | |
2038 | while 1: |
|
2035 | while 1: | |
2039 | pre,fn,rest = prefilter.splitUserInput(line, |
|
2036 | pre,fn,rest = prefilter.splitUserInput(line, | |
2040 | prefilter.shell_line_split) |
|
2037 | prefilter.shell_line_split) | |
2041 | if fn in self.alias_table: |
|
2038 | if fn in self.alias_table: | |
2042 | if fn in done: |
|
2039 | if fn in done: | |
2043 | warn("Cyclic alias definition, repeated '%s'" % fn) |
|
2040 | warn("Cyclic alias definition, repeated '%s'" % fn) | |
2044 | return "" |
|
2041 | return "" | |
2045 | done.add(fn) |
|
2042 | done.add(fn) | |
2046 |
|
2043 | |||
2047 | l2 = self.transform_alias(fn,rest) |
|
2044 | l2 = self.transform_alias(fn,rest) | |
2048 | # dir -> dir |
|
2045 | # dir -> dir | |
2049 | # print "alias",line, "->",l2 #dbg |
|
2046 | # print "alias",line, "->",l2 #dbg | |
2050 | if l2 == line: |
|
2047 | if l2 == line: | |
2051 | break |
|
2048 | break | |
2052 | # ls -> ls -F should not recurse forever |
|
2049 | # ls -> ls -F should not recurse forever | |
2053 | if l2.split(None,1)[0] == line.split(None,1)[0]: |
|
2050 | if l2.split(None,1)[0] == line.split(None,1)[0]: | |
2054 | line = l2 |
|
2051 | line = l2 | |
2055 | break |
|
2052 | break | |
2056 |
|
2053 | |||
2057 | line=l2 |
|
2054 | line=l2 | |
2058 |
|
2055 | |||
2059 |
|
2056 | |||
2060 | # print "al expand to",line #dbg |
|
2057 | # print "al expand to",line #dbg | |
2061 | else: |
|
2058 | else: | |
2062 | break |
|
2059 | break | |
2063 |
|
2060 | |||
2064 | return line |
|
2061 | return line | |
2065 |
|
2062 | |||
2066 | def transform_alias(self, alias,rest=''): |
|
2063 | def transform_alias(self, alias,rest=''): | |
2067 | """ Transform alias to system command string. |
|
2064 | """ Transform alias to system command string. | |
2068 | """ |
|
2065 | """ | |
2069 | trg = self.alias_table[alias] |
|
2066 | trg = self.alias_table[alias] | |
2070 |
|
2067 | |||
2071 | nargs,cmd = trg |
|
2068 | nargs,cmd = trg | |
2072 | # print trg #dbg |
|
2069 | # print trg #dbg | |
2073 | if ' ' in cmd and os.path.isfile(cmd): |
|
2070 | if ' ' in cmd and os.path.isfile(cmd): | |
2074 | cmd = '"%s"' % cmd |
|
2071 | cmd = '"%s"' % cmd | |
2075 |
|
2072 | |||
2076 | # Expand the %l special to be the user's input line |
|
2073 | # Expand the %l special to be the user's input line | |
2077 | if cmd.find('%l') >= 0: |
|
2074 | if cmd.find('%l') >= 0: | |
2078 | cmd = cmd.replace('%l',rest) |
|
2075 | cmd = cmd.replace('%l',rest) | |
2079 | rest = '' |
|
2076 | rest = '' | |
2080 | if nargs==0: |
|
2077 | if nargs==0: | |
2081 | # Simple, argument-less aliases |
|
2078 | # Simple, argument-less aliases | |
2082 | cmd = '%s %s' % (cmd,rest) |
|
2079 | cmd = '%s %s' % (cmd,rest) | |
2083 | else: |
|
2080 | else: | |
2084 | # Handle aliases with positional arguments |
|
2081 | # Handle aliases with positional arguments | |
2085 | args = rest.split(None,nargs) |
|
2082 | args = rest.split(None,nargs) | |
2086 | if len(args)< nargs: |
|
2083 | if len(args)< nargs: | |
2087 | error('Alias <%s> requires %s arguments, %s given.' % |
|
2084 | error('Alias <%s> requires %s arguments, %s given.' % | |
2088 | (alias,nargs,len(args))) |
|
2085 | (alias,nargs,len(args))) | |
2089 | return None |
|
2086 | return None | |
2090 | cmd = '%s %s' % (cmd % tuple(args[:nargs]),' '.join(args[nargs:])) |
|
2087 | cmd = '%s %s' % (cmd % tuple(args[:nargs]),' '.join(args[nargs:])) | |
2091 | # Now call the macro, evaluating in the user's namespace |
|
2088 | # Now call the macro, evaluating in the user's namespace | |
2092 | #print 'new command: <%r>' % cmd # dbg |
|
2089 | #print 'new command: <%r>' % cmd # dbg | |
2093 | return cmd |
|
2090 | return cmd | |
2094 |
|
2091 | |||
2095 | def call_alias(self,alias,rest=''): |
|
2092 | def call_alias(self,alias,rest=''): | |
2096 | """Call an alias given its name and the rest of the line. |
|
2093 | """Call an alias given its name and the rest of the line. | |
2097 |
|
2094 | |||
2098 | This is only used to provide backwards compatibility for users of |
|
2095 | This is only used to provide backwards compatibility for users of | |
2099 | ipalias(), use of which is not recommended for anymore.""" |
|
2096 | ipalias(), use of which is not recommended for anymore.""" | |
2100 |
|
2097 | |||
2101 | # Now call the macro, evaluating in the user's namespace |
|
2098 | # Now call the macro, evaluating in the user's namespace | |
2102 | cmd = self.transform_alias(alias, rest) |
|
2099 | cmd = self.transform_alias(alias, rest) | |
2103 | try: |
|
2100 | try: | |
2104 | self.system(cmd) |
|
2101 | self.system(cmd) | |
2105 | except: |
|
2102 | except: | |
2106 | self.showtraceback() |
|
2103 | self.showtraceback() | |
2107 |
|
2104 | |||
2108 | def indent_current_str(self): |
|
2105 | def indent_current_str(self): | |
2109 | """return the current level of indentation as a string""" |
|
2106 | """return the current level of indentation as a string""" | |
2110 | return self.indent_current_nsp * ' ' |
|
2107 | return self.indent_current_nsp * ' ' | |
2111 |
|
2108 | |||
2112 | def autoindent_update(self,line): |
|
2109 | def autoindent_update(self,line): | |
2113 | """Keep track of the indent level.""" |
|
2110 | """Keep track of the indent level.""" | |
2114 |
|
2111 | |||
2115 | #debugx('line') |
|
2112 | #debugx('line') | |
2116 | #debugx('self.indent_current_nsp') |
|
2113 | #debugx('self.indent_current_nsp') | |
2117 | if self.autoindent: |
|
2114 | if self.autoindent: | |
2118 | if line: |
|
2115 | if line: | |
2119 | inisp = num_ini_spaces(line) |
|
2116 | inisp = num_ini_spaces(line) | |
2120 | if inisp < self.indent_current_nsp: |
|
2117 | if inisp < self.indent_current_nsp: | |
2121 | self.indent_current_nsp = inisp |
|
2118 | self.indent_current_nsp = inisp | |
2122 |
|
2119 | |||
2123 | if line[-1] == ':': |
|
2120 | if line[-1] == ':': | |
2124 | self.indent_current_nsp += 4 |
|
2121 | self.indent_current_nsp += 4 | |
2125 | elif dedent_re.match(line): |
|
2122 | elif dedent_re.match(line): | |
2126 | self.indent_current_nsp -= 4 |
|
2123 | self.indent_current_nsp -= 4 | |
2127 | else: |
|
2124 | else: | |
2128 | self.indent_current_nsp = 0 |
|
2125 | self.indent_current_nsp = 0 | |
2129 |
|
2126 | |||
2130 | def push(self, variables, interactive=True): |
|
2127 | def push(self, variables, interactive=True): | |
2131 | """Inject a group of variables into the IPython user namespace. |
|
2128 | """Inject a group of variables into the IPython user namespace. | |
2132 |
|
2129 | |||
2133 | Parameters |
|
2130 | Parameters | |
2134 | ---------- |
|
2131 | ---------- | |
2135 | variables : dict, str or list/tuple of str |
|
2132 | variables : dict, str or list/tuple of str | |
2136 | The variables to inject into the user's namespace. If a dict, |
|
2133 | The variables to inject into the user's namespace. If a dict, | |
2137 | a simple update is done. If a str, the string is assumed to |
|
2134 | a simple update is done. If a str, the string is assumed to | |
2138 | have variable names separated by spaces. A list/tuple of str |
|
2135 | have variable names separated by spaces. A list/tuple of str | |
2139 | can also be used to give the variable names. If just the variable |
|
2136 | can also be used to give the variable names. If just the variable | |
2140 | names are give (list/tuple/str) then the variable values looked |
|
2137 | names are give (list/tuple/str) then the variable values looked | |
2141 | up in the callers frame. |
|
2138 | up in the callers frame. | |
2142 | interactive : bool |
|
2139 | interactive : bool | |
2143 | If True (default), the variables will be listed with the ``who`` |
|
2140 | If True (default), the variables will be listed with the ``who`` | |
2144 | magic. |
|
2141 | magic. | |
2145 | """ |
|
2142 | """ | |
2146 | vdict = None |
|
2143 | vdict = None | |
2147 |
|
2144 | |||
2148 | # We need a dict of name/value pairs to do namespace updates. |
|
2145 | # We need a dict of name/value pairs to do namespace updates. | |
2149 | if isinstance(variables, dict): |
|
2146 | if isinstance(variables, dict): | |
2150 | vdict = variables |
|
2147 | vdict = variables | |
2151 | elif isinstance(variables, (basestring, list, tuple)): |
|
2148 | elif isinstance(variables, (basestring, list, tuple)): | |
2152 | if isinstance(variables, basestring): |
|
2149 | if isinstance(variables, basestring): | |
2153 | vlist = variables.split() |
|
2150 | vlist = variables.split() | |
2154 | else: |
|
2151 | else: | |
2155 | vlist = variables |
|
2152 | vlist = variables | |
2156 | vdict = {} |
|
2153 | vdict = {} | |
2157 | cf = sys._getframe(1) |
|
2154 | cf = sys._getframe(1) | |
2158 | for name in vlist: |
|
2155 | for name in vlist: | |
2159 | try: |
|
2156 | try: | |
2160 | vdict[name] = eval(name, cf.f_globals, cf.f_locals) |
|
2157 | vdict[name] = eval(name, cf.f_globals, cf.f_locals) | |
2161 | except: |
|
2158 | except: | |
2162 | print ('Could not get variable %s from %s' % |
|
2159 | print ('Could not get variable %s from %s' % | |
2163 | (name,cf.f_code.co_name)) |
|
2160 | (name,cf.f_code.co_name)) | |
2164 | else: |
|
2161 | else: | |
2165 | raise ValueError('variables must be a dict/str/list/tuple') |
|
2162 | raise ValueError('variables must be a dict/str/list/tuple') | |
2166 |
|
2163 | |||
2167 | # Propagate variables to user namespace |
|
2164 | # Propagate variables to user namespace | |
2168 | self.user_ns.update(vdict) |
|
2165 | self.user_ns.update(vdict) | |
2169 |
|
2166 | |||
2170 | # And configure interactive visibility |
|
2167 | # And configure interactive visibility | |
2171 | config_ns = self.user_config_ns |
|
2168 | config_ns = self.user_config_ns | |
2172 | if interactive: |
|
2169 | if interactive: | |
2173 | for name, val in vdict.iteritems(): |
|
2170 | for name, val in vdict.iteritems(): | |
2174 | config_ns.pop(name, None) |
|
2171 | config_ns.pop(name, None) | |
2175 | else: |
|
2172 | else: | |
2176 | for name,val in vdict.iteritems(): |
|
2173 | for name,val in vdict.iteritems(): | |
2177 | config_ns[name] = val |
|
2174 | config_ns[name] = val | |
2178 |
|
2175 | |||
2179 | def cleanup_ipy_script(self, script): |
|
2176 | def cleanup_ipy_script(self, script): | |
2180 | """Make a script safe for self.runlines() |
|
2177 | """Make a script safe for self.runlines() | |
2181 |
|
2178 | |||
2182 | Notes |
|
2179 | Notes | |
2183 | ----- |
|
2180 | ----- | |
2184 | This was copied over from the old ipapi and probably can be done |
|
2181 | This was copied over from the old ipapi and probably can be done | |
2185 | away with once we move to block based interpreter. |
|
2182 | away with once we move to block based interpreter. | |
2186 |
|
2183 | |||
2187 | - Removes empty lines Suffixes all indented blocks that end with |
|
2184 | - Removes empty lines Suffixes all indented blocks that end with | |
2188 | - unindented lines with empty lines |
|
2185 | - unindented lines with empty lines | |
2189 | """ |
|
2186 | """ | |
2190 |
|
2187 | |||
2191 | res = [] |
|
2188 | res = [] | |
2192 | lines = script.splitlines() |
|
2189 | lines = script.splitlines() | |
2193 |
|
2190 | |||
2194 | level = 0 |
|
2191 | level = 0 | |
2195 | for l in lines: |
|
2192 | for l in lines: | |
2196 | lstripped = l.lstrip() |
|
2193 | lstripped = l.lstrip() | |
2197 | stripped = l.strip() |
|
2194 | stripped = l.strip() | |
2198 | if not stripped: |
|
2195 | if not stripped: | |
2199 | continue |
|
2196 | continue | |
2200 | newlevel = len(l) - len(lstripped) |
|
2197 | newlevel = len(l) - len(lstripped) | |
2201 | def is_secondary_block_start(s): |
|
2198 | def is_secondary_block_start(s): | |
2202 | if not s.endswith(':'): |
|
2199 | if not s.endswith(':'): | |
2203 | return False |
|
2200 | return False | |
2204 | if (s.startswith('elif') or |
|
2201 | if (s.startswith('elif') or | |
2205 | s.startswith('else') or |
|
2202 | s.startswith('else') or | |
2206 | s.startswith('except') or |
|
2203 | s.startswith('except') or | |
2207 | s.startswith('finally')): |
|
2204 | s.startswith('finally')): | |
2208 | return True |
|
2205 | return True | |
2209 |
|
2206 | |||
2210 | if level > 0 and newlevel == 0 and \ |
|
2207 | if level > 0 and newlevel == 0 and \ | |
2211 | not is_secondary_block_start(stripped): |
|
2208 | not is_secondary_block_start(stripped): | |
2212 | # add empty line |
|
2209 | # add empty line | |
2213 | res.append('') |
|
2210 | res.append('') | |
2214 |
|
2211 | |||
2215 | res.append(l) |
|
2212 | res.append(l) | |
2216 | level = newlevel |
|
2213 | level = newlevel | |
2217 | return '\n'.join(res) + '\n' |
|
2214 | return '\n'.join(res) + '\n' | |
2218 |
|
2215 | |||
2219 | def runlines(self, lines, clean=False): |
|
2216 | def runlines(self, lines, clean=False): | |
2220 | """Run a string of one or more lines of source. |
|
2217 | """Run a string of one or more lines of source. | |
2221 |
|
2218 | |||
2222 | This method is capable of running a string containing multiple source |
|
2219 | This method is capable of running a string containing multiple source | |
2223 | lines, as if they had been entered at the IPython prompt. Since it |
|
2220 | lines, as if they had been entered at the IPython prompt. Since it | |
2224 | exposes IPython's processing machinery, the given strings can contain |
|
2221 | exposes IPython's processing machinery, the given strings can contain | |
2225 | magic calls (%magic), special shell access (!cmd), etc. |
|
2222 | magic calls (%magic), special shell access (!cmd), etc. | |
2226 | """ |
|
2223 | """ | |
2227 |
|
2224 | |||
2228 | if isinstance(lines, (list, tuple)): |
|
2225 | if isinstance(lines, (list, tuple)): | |
2229 | lines = '\n'.join(lines) |
|
2226 | lines = '\n'.join(lines) | |
2230 |
|
2227 | |||
2231 | if clean: |
|
2228 | if clean: | |
2232 | lines = self.cleanup_ipy_script(lines) |
|
2229 | lines = self.cleanup_ipy_script(lines) | |
2233 |
|
2230 | |||
2234 | # We must start with a clean buffer, in case this is run from an |
|
2231 | # We must start with a clean buffer, in case this is run from an | |
2235 | # interactive IPython session (via a magic, for example). |
|
2232 | # interactive IPython session (via a magic, for example). | |
2236 | self.resetbuffer() |
|
2233 | self.resetbuffer() | |
2237 | lines = lines.splitlines() |
|
2234 | lines = lines.splitlines() | |
2238 | more = 0 |
|
2235 | more = 0 | |
2239 |
|
2236 | |||
2240 | with self.builtin_trap: |
|
2237 | with nested(self.builtin_trap, self.display_trap): | |
2241 | for line in lines: |
|
2238 | for line in lines: | |
2242 | # skip blank lines so we don't mess up the prompt counter, but do |
|
2239 | # skip blank lines so we don't mess up the prompt counter, but do | |
2243 | # NOT skip even a blank line if we are in a code block (more is |
|
2240 | # NOT skip even a blank line if we are in a code block (more is | |
2244 | # true) |
|
2241 | # true) | |
2245 |
|
2242 | |||
2246 | if line or more: |
|
2243 | if line or more: | |
2247 | # push to raw history, so hist line numbers stay in sync |
|
2244 | # push to raw history, so hist line numbers stay in sync | |
2248 | self.input_hist_raw.append("# " + line + "\n") |
|
2245 | self.input_hist_raw.append("# " + line + "\n") | |
2249 | more = self.push_line(self.prefilter(line,more)) |
|
2246 | more = self.push_line(self.prefilter(line,more)) | |
2250 | # IPython's runsource returns None if there was an error |
|
2247 | # IPython's runsource returns None if there was an error | |
2251 | # compiling the code. This allows us to stop processing right |
|
2248 | # compiling the code. This allows us to stop processing right | |
2252 | # away, so the user gets the error message at the right place. |
|
2249 | # away, so the user gets the error message at the right place. | |
2253 | if more is None: |
|
2250 | if more is None: | |
2254 | break |
|
2251 | break | |
2255 | else: |
|
2252 | else: | |
2256 | self.input_hist_raw.append("\n") |
|
2253 | self.input_hist_raw.append("\n") | |
2257 | # final newline in case the input didn't have it, so that the code |
|
2254 | # final newline in case the input didn't have it, so that the code | |
2258 | # actually does get executed |
|
2255 | # actually does get executed | |
2259 | if more: |
|
2256 | if more: | |
2260 | self.push_line('\n') |
|
2257 | self.push_line('\n') | |
2261 |
|
2258 | |||
2262 | def runsource(self, source, filename='<input>', symbol='single'): |
|
2259 | def runsource(self, source, filename='<input>', symbol='single'): | |
2263 | """Compile and run some source in the interpreter. |
|
2260 | """Compile and run some source in the interpreter. | |
2264 |
|
2261 | |||
2265 | Arguments are as for compile_command(). |
|
2262 | Arguments are as for compile_command(). | |
2266 |
|
2263 | |||
2267 | One several things can happen: |
|
2264 | One several things can happen: | |
2268 |
|
2265 | |||
2269 | 1) The input is incorrect; compile_command() raised an |
|
2266 | 1) The input is incorrect; compile_command() raised an | |
2270 | exception (SyntaxError or OverflowError). A syntax traceback |
|
2267 | exception (SyntaxError or OverflowError). A syntax traceback | |
2271 | will be printed by calling the showsyntaxerror() method. |
|
2268 | will be printed by calling the showsyntaxerror() method. | |
2272 |
|
2269 | |||
2273 | 2) The input is incomplete, and more input is required; |
|
2270 | 2) The input is incomplete, and more input is required; | |
2274 | compile_command() returned None. Nothing happens. |
|
2271 | compile_command() returned None. Nothing happens. | |
2275 |
|
2272 | |||
2276 | 3) The input is complete; compile_command() returned a code |
|
2273 | 3) The input is complete; compile_command() returned a code | |
2277 | object. The code is executed by calling self.runcode() (which |
|
2274 | object. The code is executed by calling self.runcode() (which | |
2278 | also handles run-time exceptions, except for SystemExit). |
|
2275 | also handles run-time exceptions, except for SystemExit). | |
2279 |
|
2276 | |||
2280 | The return value is: |
|
2277 | The return value is: | |
2281 |
|
2278 | |||
2282 | - True in case 2 |
|
2279 | - True in case 2 | |
2283 |
|
2280 | |||
2284 | - False in the other cases, unless an exception is raised, where |
|
2281 | - False in the other cases, unless an exception is raised, where | |
2285 | None is returned instead. This can be used by external callers to |
|
2282 | None is returned instead. This can be used by external callers to | |
2286 | know whether to continue feeding input or not. |
|
2283 | know whether to continue feeding input or not. | |
2287 |
|
2284 | |||
2288 | The return value can be used to decide whether to use sys.ps1 or |
|
2285 | The return value can be used to decide whether to use sys.ps1 or | |
2289 | sys.ps2 to prompt the next line.""" |
|
2286 | sys.ps2 to prompt the next line.""" | |
2290 |
|
2287 | |||
2291 | # if the source code has leading blanks, add 'if 1:\n' to it |
|
2288 | # if the source code has leading blanks, add 'if 1:\n' to it | |
2292 | # this allows execution of indented pasted code. It is tempting |
|
2289 | # this allows execution of indented pasted code. It is tempting | |
2293 | # to add '\n' at the end of source to run commands like ' a=1' |
|
2290 | # to add '\n' at the end of source to run commands like ' a=1' | |
2294 | # directly, but this fails for more complicated scenarios |
|
2291 | # directly, but this fails for more complicated scenarios | |
2295 | source=source.encode(self.stdin_encoding) |
|
2292 | source=source.encode(self.stdin_encoding) | |
2296 | if source[:1] in [' ', '\t']: |
|
2293 | if source[:1] in [' ', '\t']: | |
2297 | source = 'if 1:\n%s' % source |
|
2294 | source = 'if 1:\n%s' % source | |
2298 |
|
2295 | |||
2299 | try: |
|
2296 | try: | |
2300 | code = self.compile(source,filename,symbol) |
|
2297 | code = self.compile(source,filename,symbol) | |
2301 | except (OverflowError, SyntaxError, ValueError, TypeError, MemoryError): |
|
2298 | except (OverflowError, SyntaxError, ValueError, TypeError, MemoryError): | |
2302 | # Case 1 |
|
2299 | # Case 1 | |
2303 | self.showsyntaxerror(filename) |
|
2300 | self.showsyntaxerror(filename) | |
2304 | return None |
|
2301 | return None | |
2305 |
|
2302 | |||
2306 | if code is None: |
|
2303 | if code is None: | |
2307 | # Case 2 |
|
2304 | # Case 2 | |
2308 | return True |
|
2305 | return True | |
2309 |
|
2306 | |||
2310 | # Case 3 |
|
2307 | # Case 3 | |
2311 | # We store the code object so that threaded shells and |
|
2308 | # We store the code object so that threaded shells and | |
2312 | # custom exception handlers can access all this info if needed. |
|
2309 | # custom exception handlers can access all this info if needed. | |
2313 | # The source corresponding to this can be obtained from the |
|
2310 | # The source corresponding to this can be obtained from the | |
2314 | # buffer attribute as '\n'.join(self.buffer). |
|
2311 | # buffer attribute as '\n'.join(self.buffer). | |
2315 | self.code_to_run = code |
|
2312 | self.code_to_run = code | |
2316 | # now actually execute the code object |
|
2313 | # now actually execute the code object | |
2317 | if self.runcode(code) == 0: |
|
2314 | if self.runcode(code) == 0: | |
2318 | return False |
|
2315 | return False | |
2319 | else: |
|
2316 | else: | |
2320 | return None |
|
2317 | return None | |
2321 |
|
2318 | |||
2322 | def runcode(self,code_obj): |
|
2319 | def runcode(self,code_obj): | |
2323 | """Execute a code object. |
|
2320 | """Execute a code object. | |
2324 |
|
2321 | |||
2325 | When an exception occurs, self.showtraceback() is called to display a |
|
2322 | When an exception occurs, self.showtraceback() is called to display a | |
2326 | traceback. |
|
2323 | traceback. | |
2327 |
|
2324 | |||
2328 | Return value: a flag indicating whether the code to be run completed |
|
2325 | Return value: a flag indicating whether the code to be run completed | |
2329 | successfully: |
|
2326 | successfully: | |
2330 |
|
2327 | |||
2331 | - 0: successful execution. |
|
2328 | - 0: successful execution. | |
2332 | - 1: an error occurred. |
|
2329 | - 1: an error occurred. | |
2333 | """ |
|
2330 | """ | |
2334 |
|
2331 | |||
2335 | # Set our own excepthook in case the user code tries to call it |
|
2332 | # Set our own excepthook in case the user code tries to call it | |
2336 | # directly, so that the IPython crash handler doesn't get triggered |
|
2333 | # directly, so that the IPython crash handler doesn't get triggered | |
2337 | old_excepthook,sys.excepthook = sys.excepthook, self.excepthook |
|
2334 | old_excepthook,sys.excepthook = sys.excepthook, self.excepthook | |
2338 |
|
2335 | |||
2339 | # we save the original sys.excepthook in the instance, in case config |
|
2336 | # we save the original sys.excepthook in the instance, in case config | |
2340 | # code (such as magics) needs access to it. |
|
2337 | # code (such as magics) needs access to it. | |
2341 | self.sys_excepthook = old_excepthook |
|
2338 | self.sys_excepthook = old_excepthook | |
2342 | outflag = 1 # happens in more places, so it's easier as default |
|
2339 | outflag = 1 # happens in more places, so it's easier as default | |
2343 | try: |
|
2340 | try: | |
2344 | try: |
|
2341 | try: | |
2345 | self.hooks.pre_runcode_hook() |
|
2342 | self.hooks.pre_runcode_hook() | |
2346 | exec code_obj in self.user_global_ns, self.user_ns |
|
2343 | exec code_obj in self.user_global_ns, self.user_ns | |
2347 | finally: |
|
2344 | finally: | |
2348 | # Reset our crash handler in place |
|
2345 | # Reset our crash handler in place | |
2349 | sys.excepthook = old_excepthook |
|
2346 | sys.excepthook = old_excepthook | |
2350 | except SystemExit: |
|
2347 | except SystemExit: | |
2351 | self.resetbuffer() |
|
2348 | self.resetbuffer() | |
2352 | self.showtraceback() |
|
2349 | self.showtraceback() | |
2353 | warn("Type %exit or %quit to exit IPython " |
|
2350 | warn("Type %exit or %quit to exit IPython " | |
2354 | "(%Exit or %Quit do so unconditionally).",level=1) |
|
2351 | "(%Exit or %Quit do so unconditionally).",level=1) | |
2355 | except self.custom_exceptions: |
|
2352 | except self.custom_exceptions: | |
2356 | etype,value,tb = sys.exc_info() |
|
2353 | etype,value,tb = sys.exc_info() | |
2357 | self.CustomTB(etype,value,tb) |
|
2354 | self.CustomTB(etype,value,tb) | |
2358 | except: |
|
2355 | except: | |
2359 | self.showtraceback() |
|
2356 | self.showtraceback() | |
2360 | else: |
|
2357 | else: | |
2361 | outflag = 0 |
|
2358 | outflag = 0 | |
2362 | if softspace(sys.stdout, 0): |
|
2359 | if softspace(sys.stdout, 0): | |
2363 |
|
2360 | |||
2364 | # Flush out code object which has been run (and source) |
|
2361 | # Flush out code object which has been run (and source) | |
2365 | self.code_to_run = None |
|
2362 | self.code_to_run = None | |
2366 | return outflag |
|
2363 | return outflag | |
2367 |
|
2364 | |||
2368 | def push_line(self, line): |
|
2365 | def push_line(self, line): | |
2369 | """Push a line to the interpreter. |
|
2366 | """Push a line to the interpreter. | |
2370 |
|
2367 | |||
2371 | The line should not have a trailing newline; it may have |
|
2368 | The line should not have a trailing newline; it may have | |
2372 | internal newlines. The line is appended to a buffer and the |
|
2369 | internal newlines. The line is appended to a buffer and the | |
2373 | interpreter's runsource() method is called with the |
|
2370 | interpreter's runsource() method is called with the | |
2374 | concatenated contents of the buffer as source. If this |
|
2371 | concatenated contents of the buffer as source. If this | |
2375 | indicates that the command was executed or invalid, the buffer |
|
2372 | indicates that the command was executed or invalid, the buffer | |
2376 | is reset; otherwise, the command is incomplete, and the buffer |
|
2373 | is reset; otherwise, the command is incomplete, and the buffer | |
2377 | is left as it was after the line was appended. The return |
|
2374 | is left as it was after the line was appended. The return | |
2378 | value is 1 if more input is required, 0 if the line was dealt |
|
2375 | value is 1 if more input is required, 0 if the line was dealt | |
2379 | with in some way (this is the same as runsource()). |
|
2376 | with in some way (this is the same as runsource()). | |
2380 | """ |
|
2377 | """ | |
2381 |
|
2378 | |||
2382 | # autoindent management should be done here, and not in the |
|
2379 | # autoindent management should be done here, and not in the | |
2383 | # interactive loop, since that one is only seen by keyboard input. We |
|
2380 | # interactive loop, since that one is only seen by keyboard input. We | |
2384 | # need this done correctly even for code run via runlines (which uses |
|
2381 | # need this done correctly even for code run via runlines (which uses | |
2385 | # push). |
|
2382 | # push). | |
2386 |
|
2383 | |||
2387 | #print 'push line: <%s>' % line # dbg |
|
2384 | #print 'push line: <%s>' % line # dbg | |
2388 | for subline in line.splitlines(): |
|
2385 | for subline in line.splitlines(): | |
2389 | self.autoindent_update(subline) |
|
2386 | self.autoindent_update(subline) | |
2390 | self.buffer.append(line) |
|
2387 | self.buffer.append(line) | |
2391 | more = self.runsource('\n'.join(self.buffer), self.filename) |
|
2388 | more = self.runsource('\n'.join(self.buffer), self.filename) | |
2392 | if not more: |
|
2389 | if not more: | |
2393 | self.resetbuffer() |
|
2390 | self.resetbuffer() | |
2394 | return more |
|
2391 | return more | |
2395 |
|
2392 | |||
2396 | def split_user_input(self, line): |
|
2393 | def split_user_input(self, line): | |
2397 | # This is really a hold-over to support ipapi and some extensions |
|
2394 | # This is really a hold-over to support ipapi and some extensions | |
2398 | return prefilter.splitUserInput(line) |
|
2395 | return prefilter.splitUserInput(line) | |
2399 |
|
2396 | |||
2400 | def resetbuffer(self): |
|
2397 | def resetbuffer(self): | |
2401 | """Reset the input buffer.""" |
|
2398 | """Reset the input buffer.""" | |
2402 | self.buffer[:] = [] |
|
2399 | self.buffer[:] = [] | |
2403 |
|
2400 | |||
2404 | def raw_input(self,prompt='',continue_prompt=False): |
|
2401 | def raw_input(self,prompt='',continue_prompt=False): | |
2405 | """Write a prompt and read a line. |
|
2402 | """Write a prompt and read a line. | |
2406 |
|
2403 | |||
2407 | The returned line does not include the trailing newline. |
|
2404 | The returned line does not include the trailing newline. | |
2408 | When the user enters the EOF key sequence, EOFError is raised. |
|
2405 | When the user enters the EOF key sequence, EOFError is raised. | |
2409 |
|
2406 | |||
2410 | Optional inputs: |
|
2407 | Optional inputs: | |
2411 |
|
2408 | |||
2412 | - prompt(''): a string to be printed to prompt the user. |
|
2409 | - prompt(''): a string to be printed to prompt the user. | |
2413 |
|
2410 | |||
2414 | - continue_prompt(False): whether this line is the first one or a |
|
2411 | - continue_prompt(False): whether this line is the first one or a | |
2415 | continuation in a sequence of inputs. |
|
2412 | continuation in a sequence of inputs. | |
2416 | """ |
|
2413 | """ | |
2417 |
|
2414 | |||
2418 | # Code run by the user may have modified the readline completer state. |
|
2415 | # Code run by the user may have modified the readline completer state. | |
2419 | # We must ensure that our completer is back in place. |
|
2416 | # We must ensure that our completer is back in place. | |
2420 | if self.has_readline: |
|
2417 | if self.has_readline: | |
2421 | self.set_completer() |
|
2418 | self.set_completer() | |
2422 |
|
2419 | |||
2423 | try: |
|
2420 | try: | |
2424 | line = raw_input_original(prompt).decode(self.stdin_encoding) |
|
2421 | line = raw_input_original(prompt).decode(self.stdin_encoding) | |
2425 | except ValueError: |
|
2422 | except ValueError: | |
2426 | warn("\n********\nYou or a %run:ed script called sys.stdin.close()" |
|
2423 | warn("\n********\nYou or a %run:ed script called sys.stdin.close()" | |
2427 | " or sys.stdout.close()!\nExiting IPython!") |
|
2424 | " or sys.stdout.close()!\nExiting IPython!") | |
2428 | self.ask_exit() |
|
2425 | self.ask_exit() | |
2429 | return "" |
|
2426 | return "" | |
2430 |
|
2427 | |||
2431 | # Try to be reasonably smart about not re-indenting pasted input more |
|
2428 | # Try to be reasonably smart about not re-indenting pasted input more | |
2432 | # than necessary. We do this by trimming out the auto-indent initial |
|
2429 | # than necessary. We do this by trimming out the auto-indent initial | |
2433 | # spaces, if the user's actual input started itself with whitespace. |
|
2430 | # spaces, if the user's actual input started itself with whitespace. | |
2434 | #debugx('self.buffer[-1]') |
|
2431 | #debugx('self.buffer[-1]') | |
2435 |
|
2432 | |||
2436 | if self.autoindent: |
|
2433 | if self.autoindent: | |
2437 | if num_ini_spaces(line) > self.indent_current_nsp: |
|
2434 | if num_ini_spaces(line) > self.indent_current_nsp: | |
2438 | line = line[self.indent_current_nsp:] |
|
2435 | line = line[self.indent_current_nsp:] | |
2439 | self.indent_current_nsp = 0 |
|
2436 | self.indent_current_nsp = 0 | |
2440 |
|
2437 | |||
2441 | # store the unfiltered input before the user has any chance to modify |
|
2438 | # store the unfiltered input before the user has any chance to modify | |
2442 | # it. |
|
2439 | # it. | |
2443 | if line.strip(): |
|
2440 | if line.strip(): | |
2444 | if continue_prompt: |
|
2441 | if continue_prompt: | |
2445 | self.input_hist_raw[-1] += '%s\n' % line |
|
2442 | self.input_hist_raw[-1] += '%s\n' % line | |
2446 | if self.has_readline: # and some config option is set? |
|
2443 | if self.has_readline: # and some config option is set? | |
2447 | try: |
|
2444 | try: | |
2448 | histlen = self.readline.get_current_history_length() |
|
2445 | histlen = self.readline.get_current_history_length() | |
2449 | if histlen > 1: |
|
2446 | if histlen > 1: | |
2450 | newhist = self.input_hist_raw[-1].rstrip() |
|
2447 | newhist = self.input_hist_raw[-1].rstrip() | |
2451 | self.readline.remove_history_item(histlen-1) |
|
2448 | self.readline.remove_history_item(histlen-1) | |
2452 | self.readline.replace_history_item(histlen-2, |
|
2449 | self.readline.replace_history_item(histlen-2, | |
2453 | newhist.encode(self.stdin_encoding)) |
|
2450 | newhist.encode(self.stdin_encoding)) | |
2454 | except AttributeError: |
|
2451 | except AttributeError: | |
2455 | pass # re{move,place}_history_item are new in 2.4. |
|
2452 | pass # re{move,place}_history_item are new in 2.4. | |
2456 | else: |
|
2453 | else: | |
2457 | self.input_hist_raw.append('%s\n' % line) |
|
2454 | self.input_hist_raw.append('%s\n' % line) | |
2458 | # only entries starting at first column go to shadow history |
|
2455 | # only entries starting at first column go to shadow history | |
2459 | if line.lstrip() == line: |
|
2456 | if line.lstrip() == line: | |
2460 | self.shadowhist.add(line.strip()) |
|
2457 | self.shadowhist.add(line.strip()) | |
2461 | elif not continue_prompt: |
|
2458 | elif not continue_prompt: | |
2462 | self.input_hist_raw.append('\n') |
|
2459 | self.input_hist_raw.append('\n') | |
2463 | try: |
|
2460 | try: | |
2464 | lineout = self.prefilter(line,continue_prompt) |
|
2461 | lineout = self.prefilter(line,continue_prompt) | |
2465 | except: |
|
2462 | except: | |
2466 | # blanket except, in case a user-defined prefilter crashes, so it |
|
2463 | # blanket except, in case a user-defined prefilter crashes, so it | |
2467 | # can't take all of ipython with it. |
|
2464 | # can't take all of ipython with it. | |
2468 | self.showtraceback() |
|
2465 | self.showtraceback() | |
2469 | return '' |
|
2466 | return '' | |
2470 | else: |
|
2467 | else: | |
2471 | return lineout |
|
2468 | return lineout | |
2472 |
|
2469 | |||
2473 | def _prefilter(self, line, continue_prompt): |
|
2470 | def _prefilter(self, line, continue_prompt): | |
2474 | """Calls different preprocessors, depending on the form of line.""" |
|
2471 | """Calls different preprocessors, depending on the form of line.""" | |
2475 |
|
2472 | |||
2476 | # All handlers *must* return a value, even if it's blank (''). |
|
2473 | # All handlers *must* return a value, even if it's blank (''). | |
2477 |
|
2474 | |||
2478 | # Lines are NOT logged here. Handlers should process the line as |
|
2475 | # Lines are NOT logged here. Handlers should process the line as | |
2479 | # needed, update the cache AND log it (so that the input cache array |
|
2476 | # needed, update the cache AND log it (so that the input cache array | |
2480 | # stays synced). |
|
2477 | # stays synced). | |
2481 |
|
2478 | |||
2482 | #..................................................................... |
|
2479 | #..................................................................... | |
2483 | # Code begins |
|
2480 | # Code begins | |
2484 |
|
2481 | |||
2485 | #if line.startswith('%crash'): raise RuntimeError,'Crash now!' # dbg |
|
2482 | #if line.startswith('%crash'): raise RuntimeError,'Crash now!' # dbg | |
2486 |
|
2483 | |||
2487 | # save the line away in case we crash, so the post-mortem handler can |
|
2484 | # save the line away in case we crash, so the post-mortem handler can | |
2488 | # record it |
|
2485 | # record it | |
2489 | self._last_input_line = line |
|
2486 | self._last_input_line = line | |
2490 |
|
2487 | |||
2491 | #print '***line: <%s>' % line # dbg |
|
2488 | #print '***line: <%s>' % line # dbg | |
2492 |
|
2489 | |||
2493 | if not line: |
|
2490 | if not line: | |
2494 | # Return immediately on purely empty lines, so that if the user |
|
2491 | # Return immediately on purely empty lines, so that if the user | |
2495 | # previously typed some whitespace that started a continuation |
|
2492 | # previously typed some whitespace that started a continuation | |
2496 | # prompt, he can break out of that loop with just an empty line. |
|
2493 | # prompt, he can break out of that loop with just an empty line. | |
2497 | # This is how the default python prompt works. |
|
2494 | # This is how the default python prompt works. | |
2498 |
|
2495 | |||
2499 | # Only return if the accumulated input buffer was just whitespace! |
|
2496 | # Only return if the accumulated input buffer was just whitespace! | |
2500 | if ''.join(self.buffer).isspace(): |
|
2497 | if ''.join(self.buffer).isspace(): | |
2501 | self.buffer[:] = [] |
|
2498 | self.buffer[:] = [] | |
2502 | return '' |
|
2499 | return '' | |
2503 |
|
2500 | |||
2504 | line_info = prefilter.LineInfo(line, continue_prompt) |
|
2501 | line_info = prefilter.LineInfo(line, continue_prompt) | |
2505 |
|
2502 | |||
2506 | # the input history needs to track even empty lines |
|
2503 | # the input history needs to track even empty lines | |
2507 | stripped = line.strip() |
|
2504 | stripped = line.strip() | |
2508 |
|
2505 | |||
2509 | if not stripped: |
|
2506 | if not stripped: | |
2510 | if not continue_prompt: |
|
2507 | if not continue_prompt: | |
2511 | self.outputcache.prompt_count -= 1 |
|
2508 | self.outputcache.prompt_count -= 1 | |
2512 | return self.handle_normal(line_info) |
|
2509 | return self.handle_normal(line_info) | |
2513 |
|
2510 | |||
2514 | # print '***cont',continue_prompt # dbg |
|
2511 | # print '***cont',continue_prompt # dbg | |
2515 | # special handlers are only allowed for single line statements |
|
2512 | # special handlers are only allowed for single line statements | |
2516 | if continue_prompt and not self.multi_line_specials: |
|
2513 | if continue_prompt and not self.multi_line_specials: | |
2517 | return self.handle_normal(line_info) |
|
2514 | return self.handle_normal(line_info) | |
2518 |
|
2515 | |||
2519 |
|
2516 | |||
2520 | # See whether any pre-existing handler can take care of it |
|
2517 | # See whether any pre-existing handler can take care of it | |
2521 | rewritten = self.hooks.input_prefilter(stripped) |
|
2518 | rewritten = self.hooks.input_prefilter(stripped) | |
2522 | if rewritten != stripped: # ok, some prefilter did something |
|
2519 | if rewritten != stripped: # ok, some prefilter did something | |
2523 | rewritten = line_info.pre + rewritten # add indentation |
|
2520 | rewritten = line_info.pre + rewritten # add indentation | |
2524 | return self.handle_normal(prefilter.LineInfo(rewritten, |
|
2521 | return self.handle_normal(prefilter.LineInfo(rewritten, | |
2525 | continue_prompt)) |
|
2522 | continue_prompt)) | |
2526 |
|
2523 | |||
2527 | #print 'pre <%s> iFun <%s> rest <%s>' % (pre,iFun,theRest) # dbg |
|
2524 | #print 'pre <%s> iFun <%s> rest <%s>' % (pre,iFun,theRest) # dbg | |
2528 |
|
2525 | |||
2529 | return prefilter.prefilter(line_info, self) |
|
2526 | return prefilter.prefilter(line_info, self) | |
2530 |
|
2527 | |||
2531 |
|
2528 | |||
2532 | def _prefilter_dumb(self, line, continue_prompt): |
|
2529 | def _prefilter_dumb(self, line, continue_prompt): | |
2533 | """simple prefilter function, for debugging""" |
|
2530 | """simple prefilter function, for debugging""" | |
2534 | return self.handle_normal(line,continue_prompt) |
|
2531 | return self.handle_normal(line,continue_prompt) | |
2535 |
|
2532 | |||
2536 |
|
2533 | |||
2537 | def multiline_prefilter(self, line, continue_prompt): |
|
2534 | def multiline_prefilter(self, line, continue_prompt): | |
2538 | """ Run _prefilter for each line of input |
|
2535 | """ Run _prefilter for each line of input | |
2539 |
|
2536 | |||
2540 | Covers cases where there are multiple lines in the user entry, |
|
2537 | Covers cases where there are multiple lines in the user entry, | |
2541 | which is the case when the user goes back to a multiline history |
|
2538 | which is the case when the user goes back to a multiline history | |
2542 | entry and presses enter. |
|
2539 | entry and presses enter. | |
2543 |
|
2540 | |||
2544 | """ |
|
2541 | """ | |
2545 | out = [] |
|
2542 | out = [] | |
2546 | for l in line.rstrip('\n').split('\n'): |
|
2543 | for l in line.rstrip('\n').split('\n'): | |
2547 | out.append(self._prefilter(l, continue_prompt)) |
|
2544 | out.append(self._prefilter(l, continue_prompt)) | |
2548 | return '\n'.join(out) |
|
2545 | return '\n'.join(out) | |
2549 |
|
2546 | |||
2550 | # Set the default prefilter() function (this can be user-overridden) |
|
2547 | # Set the default prefilter() function (this can be user-overridden) | |
2551 | prefilter = multiline_prefilter |
|
2548 | prefilter = multiline_prefilter | |
2552 |
|
2549 | |||
2553 | def handle_normal(self,line_info): |
|
2550 | def handle_normal(self,line_info): | |
2554 | """Handle normal input lines. Use as a template for handlers.""" |
|
2551 | """Handle normal input lines. Use as a template for handlers.""" | |
2555 |
|
2552 | |||
2556 | # With autoindent on, we need some way to exit the input loop, and I |
|
2553 | # With autoindent on, we need some way to exit the input loop, and I | |
2557 | # don't want to force the user to have to backspace all the way to |
|
2554 | # don't want to force the user to have to backspace all the way to | |
2558 | # clear the line. The rule will be in this case, that either two |
|
2555 | # clear the line. The rule will be in this case, that either two | |
2559 | # lines of pure whitespace in a row, or a line of pure whitespace but |
|
2556 | # lines of pure whitespace in a row, or a line of pure whitespace but | |
2560 | # of a size different to the indent level, will exit the input loop. |
|
2557 | # of a size different to the indent level, will exit the input loop. | |
2561 | line = line_info.line |
|
2558 | line = line_info.line | |
2562 | continue_prompt = line_info.continue_prompt |
|
2559 | continue_prompt = line_info.continue_prompt | |
2563 |
|
2560 | |||
2564 | if (continue_prompt and self.autoindent and line.isspace() and |
|
2561 | if (continue_prompt and self.autoindent and line.isspace() and | |
2565 | (0 < abs(len(line) - self.indent_current_nsp) <= 2 or |
|
2562 | (0 < abs(len(line) - self.indent_current_nsp) <= 2 or | |
2566 | (self.buffer[-1]).isspace() )): |
|
2563 | (self.buffer[-1]).isspace() )): | |
2567 | line = '' |
|
2564 | line = '' | |
2568 |
|
2565 | |||
2569 | self.log(line,line,continue_prompt) |
|
2566 | self.log(line,line,continue_prompt) | |
2570 | return line |
|
2567 | return line | |
2571 |
|
2568 | |||
2572 | def handle_alias(self,line_info): |
|
2569 | def handle_alias(self,line_info): | |
2573 | """Handle alias input lines. """ |
|
2570 | """Handle alias input lines. """ | |
2574 | tgt = self.alias_table[line_info.iFun] |
|
2571 | tgt = self.alias_table[line_info.iFun] | |
2575 | # print "=>",tgt #dbg |
|
2572 | # print "=>",tgt #dbg | |
2576 | if callable(tgt): |
|
2573 | if callable(tgt): | |
2577 | if '$' in line_info.line: |
|
2574 | if '$' in line_info.line: | |
2578 | call_meth = '(_ip, _ip.var_expand(%s))' |
|
2575 | call_meth = '(_ip, _ip.var_expand(%s))' | |
2579 | else: |
|
2576 | else: | |
2580 | call_meth = '(_ip,%s)' |
|
2577 | call_meth = '(_ip,%s)' | |
2581 | line_out = ("%s_sh.%s" + call_meth) % (line_info.preWhitespace, |
|
2578 | line_out = ("%s_sh.%s" + call_meth) % (line_info.preWhitespace, | |
2582 | line_info.iFun, |
|
2579 | line_info.iFun, | |
2583 | make_quoted_expr(line_info.line)) |
|
2580 | make_quoted_expr(line_info.line)) | |
2584 | else: |
|
2581 | else: | |
2585 | transformed = self.expand_aliases(line_info.iFun,line_info.theRest) |
|
2582 | transformed = self.expand_aliases(line_info.iFun,line_info.theRest) | |
2586 |
|
2583 | |||
2587 | # pre is needed, because it carries the leading whitespace. Otherwise |
|
2584 | # pre is needed, because it carries the leading whitespace. Otherwise | |
2588 | # aliases won't work in indented sections. |
|
2585 | # aliases won't work in indented sections. | |
2589 | line_out = '%s_ip.system(%s)' % (line_info.preWhitespace, |
|
2586 | line_out = '%s_ip.system(%s)' % (line_info.preWhitespace, | |
2590 | make_quoted_expr( transformed )) |
|
2587 | make_quoted_expr( transformed )) | |
2591 |
|
2588 | |||
2592 | self.log(line_info.line,line_out,line_info.continue_prompt) |
|
2589 | self.log(line_info.line,line_out,line_info.continue_prompt) | |
2593 | #print 'line out:',line_out # dbg |
|
2590 | #print 'line out:',line_out # dbg | |
2594 | return line_out |
|
2591 | return line_out | |
2595 |
|
2592 | |||
2596 | def handle_shell_escape(self, line_info): |
|
2593 | def handle_shell_escape(self, line_info): | |
2597 | """Execute the line in a shell, empty return value""" |
|
2594 | """Execute the line in a shell, empty return value""" | |
2598 | #print 'line in :', `line` # dbg |
|
2595 | #print 'line in :', `line` # dbg | |
2599 | line = line_info.line |
|
2596 | line = line_info.line | |
2600 | if line.lstrip().startswith('!!'): |
|
2597 | if line.lstrip().startswith('!!'): | |
2601 | # rewrite LineInfo's line, iFun and theRest to properly hold the |
|
2598 | # rewrite LineInfo's line, iFun and theRest to properly hold the | |
2602 | # call to %sx and the actual command to be executed, so |
|
2599 | # call to %sx and the actual command to be executed, so | |
2603 | # handle_magic can work correctly. Note that this works even if |
|
2600 | # handle_magic can work correctly. Note that this works even if | |
2604 | # the line is indented, so it handles multi_line_specials |
|
2601 | # the line is indented, so it handles multi_line_specials | |
2605 | # properly. |
|
2602 | # properly. | |
2606 | new_rest = line.lstrip()[2:] |
|
2603 | new_rest = line.lstrip()[2:] | |
2607 | line_info.line = '%ssx %s' % (self.ESC_MAGIC,new_rest) |
|
2604 | line_info.line = '%ssx %s' % (self.ESC_MAGIC,new_rest) | |
2608 | line_info.iFun = 'sx' |
|
2605 | line_info.iFun = 'sx' | |
2609 | line_info.theRest = new_rest |
|
2606 | line_info.theRest = new_rest | |
2610 | return self.handle_magic(line_info) |
|
2607 | return self.handle_magic(line_info) | |
2611 | else: |
|
2608 | else: | |
2612 | cmd = line.lstrip().lstrip('!') |
|
2609 | cmd = line.lstrip().lstrip('!') | |
2613 | line_out = '%s_ip.system(%s)' % (line_info.preWhitespace, |
|
2610 | line_out = '%s_ip.system(%s)' % (line_info.preWhitespace, | |
2614 | make_quoted_expr(cmd)) |
|
2611 | make_quoted_expr(cmd)) | |
2615 | # update cache/log and return |
|
2612 | # update cache/log and return | |
2616 | self.log(line,line_out,line_info.continue_prompt) |
|
2613 | self.log(line,line_out,line_info.continue_prompt) | |
2617 | return line_out |
|
2614 | return line_out | |
2618 |
|
2615 | |||
2619 | def handle_magic(self, line_info): |
|
2616 | def handle_magic(self, line_info): | |
2620 | """Execute magic functions.""" |
|
2617 | """Execute magic functions.""" | |
2621 | iFun = line_info.iFun |
|
2618 | iFun = line_info.iFun | |
2622 | theRest = line_info.theRest |
|
2619 | theRest = line_info.theRest | |
2623 | cmd = '%s_ip.magic(%s)' % (line_info.preWhitespace, |
|
2620 | cmd = '%s_ip.magic(%s)' % (line_info.preWhitespace, | |
2624 | make_quoted_expr(iFun + " " + theRest)) |
|
2621 | make_quoted_expr(iFun + " " + theRest)) | |
2625 | self.log(line_info.line,cmd,line_info.continue_prompt) |
|
2622 | self.log(line_info.line,cmd,line_info.continue_prompt) | |
2626 | #print 'in handle_magic, cmd=<%s>' % cmd # dbg |
|
2623 | #print 'in handle_magic, cmd=<%s>' % cmd # dbg | |
2627 | return cmd |
|
2624 | return cmd | |
2628 |
|
2625 | |||
2629 | def handle_auto(self, line_info): |
|
2626 | def handle_auto(self, line_info): | |
2630 | """Hande lines which can be auto-executed, quoting if requested.""" |
|
2627 | """Hande lines which can be auto-executed, quoting if requested.""" | |
2631 |
|
2628 | |||
2632 | line = line_info.line |
|
2629 | line = line_info.line | |
2633 | iFun = line_info.iFun |
|
2630 | iFun = line_info.iFun | |
2634 | theRest = line_info.theRest |
|
2631 | theRest = line_info.theRest | |
2635 | pre = line_info.pre |
|
2632 | pre = line_info.pre | |
2636 | continue_prompt = line_info.continue_prompt |
|
2633 | continue_prompt = line_info.continue_prompt | |
2637 | obj = line_info.ofind(self)['obj'] |
|
2634 | obj = line_info.ofind(self)['obj'] | |
2638 |
|
2635 | |||
2639 | #print 'pre <%s> iFun <%s> rest <%s>' % (pre,iFun,theRest) # dbg |
|
2636 | #print 'pre <%s> iFun <%s> rest <%s>' % (pre,iFun,theRest) # dbg | |
2640 |
|
2637 | |||
2641 | # This should only be active for single-line input! |
|
2638 | # This should only be active for single-line input! | |
2642 | if continue_prompt: |
|
2639 | if continue_prompt: | |
2643 | self.log(line,line,continue_prompt) |
|
2640 | self.log(line,line,continue_prompt) | |
2644 | return line |
|
2641 | return line | |
2645 |
|
2642 | |||
2646 | force_auto = isinstance(obj, IPyAutocall) |
|
2643 | force_auto = isinstance(obj, IPyAutocall) | |
2647 | auto_rewrite = True |
|
2644 | auto_rewrite = True | |
2648 |
|
2645 | |||
2649 | if pre == self.ESC_QUOTE: |
|
2646 | if pre == self.ESC_QUOTE: | |
2650 | # Auto-quote splitting on whitespace |
|
2647 | # Auto-quote splitting on whitespace | |
2651 | newcmd = '%s("%s")' % (iFun,'", "'.join(theRest.split()) ) |
|
2648 | newcmd = '%s("%s")' % (iFun,'", "'.join(theRest.split()) ) | |
2652 | elif pre == self.ESC_QUOTE2: |
|
2649 | elif pre == self.ESC_QUOTE2: | |
2653 | # Auto-quote whole string |
|
2650 | # Auto-quote whole string | |
2654 | newcmd = '%s("%s")' % (iFun,theRest) |
|
2651 | newcmd = '%s("%s")' % (iFun,theRest) | |
2655 | elif pre == self.ESC_PAREN: |
|
2652 | elif pre == self.ESC_PAREN: | |
2656 | newcmd = '%s(%s)' % (iFun,",".join(theRest.split())) |
|
2653 | newcmd = '%s(%s)' % (iFun,",".join(theRest.split())) | |
2657 | else: |
|
2654 | else: | |
2658 | # Auto-paren. |
|
2655 | # Auto-paren. | |
2659 | # We only apply it to argument-less calls if the autocall |
|
2656 | # We only apply it to argument-less calls if the autocall | |
2660 | # parameter is set to 2. We only need to check that autocall is < |
|
2657 | # parameter is set to 2. We only need to check that autocall is < | |
2661 | # 2, since this function isn't called unless it's at least 1. |
|
2658 | # 2, since this function isn't called unless it's at least 1. | |
2662 | if not theRest and (self.autocall < 2) and not force_auto: |
|
2659 | if not theRest and (self.autocall < 2) and not force_auto: | |
2663 | newcmd = '%s %s' % (iFun,theRest) |
|
2660 | newcmd = '%s %s' % (iFun,theRest) | |
2664 | auto_rewrite = False |
|
2661 | auto_rewrite = False | |
2665 | else: |
|
2662 | else: | |
2666 | if not force_auto and theRest.startswith('['): |
|
2663 | if not force_auto and theRest.startswith('['): | |
2667 | if hasattr(obj,'__getitem__'): |
|
2664 | if hasattr(obj,'__getitem__'): | |
2668 | # Don't autocall in this case: item access for an object |
|
2665 | # Don't autocall in this case: item access for an object | |
2669 | # which is BOTH callable and implements __getitem__. |
|
2666 | # which is BOTH callable and implements __getitem__. | |
2670 | newcmd = '%s %s' % (iFun,theRest) |
|
2667 | newcmd = '%s %s' % (iFun,theRest) | |
2671 | auto_rewrite = False |
|
2668 | auto_rewrite = False | |
2672 | else: |
|
2669 | else: | |
2673 | # if the object doesn't support [] access, go ahead and |
|
2670 | # if the object doesn't support [] access, go ahead and | |
2674 | # autocall |
|
2671 | # autocall | |
2675 | newcmd = '%s(%s)' % (iFun.rstrip(),theRest) |
|
2672 | newcmd = '%s(%s)' % (iFun.rstrip(),theRest) | |
2676 | elif theRest.endswith(';'): |
|
2673 | elif theRest.endswith(';'): | |
2677 | newcmd = '%s(%s);' % (iFun.rstrip(),theRest[:-1]) |
|
2674 | newcmd = '%s(%s);' % (iFun.rstrip(),theRest[:-1]) | |
2678 | else: |
|
2675 | else: | |
2679 | newcmd = '%s(%s)' % (iFun.rstrip(), theRest) |
|
2676 | newcmd = '%s(%s)' % (iFun.rstrip(), theRest) | |
2680 |
|
2677 | |||
2681 | if auto_rewrite: |
|
2678 | if auto_rewrite: | |
2682 | rw = self.outputcache.prompt1.auto_rewrite() + newcmd |
|
2679 | rw = self.outputcache.prompt1.auto_rewrite() + newcmd | |
2683 |
|
2680 | |||
2684 | try: |
|
2681 | try: | |
2685 | # plain ascii works better w/ pyreadline, on some machines, so |
|
2682 | # plain ascii works better w/ pyreadline, on some machines, so | |
2686 | # we use it and only print uncolored rewrite if we have unicode |
|
2683 | # we use it and only print uncolored rewrite if we have unicode | |
2687 | rw = str(rw) |
|
2684 | rw = str(rw) | |
2688 | print >>Term.cout, rw |
|
2685 | print >>Term.cout, rw | |
2689 | except UnicodeEncodeError: |
|
2686 | except UnicodeEncodeError: | |
2690 | print "-------------->" + newcmd |
|
2687 | print "-------------->" + newcmd | |
2691 |
|
2688 | |||
2692 | # log what is now valid Python, not the actual user input (without the |
|
2689 | # log what is now valid Python, not the actual user input (without the | |
2693 | # final newline) |
|
2690 | # final newline) | |
2694 | self.log(line,newcmd,continue_prompt) |
|
2691 | self.log(line,newcmd,continue_prompt) | |
2695 | return newcmd |
|
2692 | return newcmd | |
2696 |
|
2693 | |||
2697 | def handle_help(self, line_info): |
|
2694 | def handle_help(self, line_info): | |
2698 | """Try to get some help for the object. |
|
2695 | """Try to get some help for the object. | |
2699 |
|
2696 | |||
2700 | obj? or ?obj -> basic information. |
|
2697 | obj? or ?obj -> basic information. | |
2701 | obj?? or ??obj -> more details. |
|
2698 | obj?? or ??obj -> more details. | |
2702 | """ |
|
2699 | """ | |
2703 |
|
2700 | |||
2704 | line = line_info.line |
|
2701 | line = line_info.line | |
2705 | # We need to make sure that we don't process lines which would be |
|
2702 | # We need to make sure that we don't process lines which would be | |
2706 | # otherwise valid python, such as "x=1 # what?" |
|
2703 | # otherwise valid python, such as "x=1 # what?" | |
2707 | try: |
|
2704 | try: | |
2708 | codeop.compile_command(line) |
|
2705 | codeop.compile_command(line) | |
2709 | except SyntaxError: |
|
2706 | except SyntaxError: | |
2710 | # We should only handle as help stuff which is NOT valid syntax |
|
2707 | # We should only handle as help stuff which is NOT valid syntax | |
2711 | if line[0]==self.ESC_HELP: |
|
2708 | if line[0]==self.ESC_HELP: | |
2712 | line = line[1:] |
|
2709 | line = line[1:] | |
2713 | elif line[-1]==self.ESC_HELP: |
|
2710 | elif line[-1]==self.ESC_HELP: | |
2714 | line = line[:-1] |
|
2711 | line = line[:-1] | |
2715 | self.log(line,'#?'+line,line_info.continue_prompt) |
|
2712 | self.log(line,'#?'+line,line_info.continue_prompt) | |
2716 | if line: |
|
2713 | if line: | |
2717 | #print 'line:<%r>' % line # dbg |
|
2714 | #print 'line:<%r>' % line # dbg | |
2718 | self.magic_pinfo(line) |
|
2715 | self.magic_pinfo(line) | |
2719 | else: |
|
2716 | else: | |
2720 | page(self.usage,screen_lines=self.usable_screen_length) |
|
2717 | page(self.usage,screen_lines=self.usable_screen_length) | |
2721 | return '' # Empty string is needed here! |
|
2718 | return '' # Empty string is needed here! | |
2722 | except: |
|
2719 | except: | |
2723 | # Pass any other exceptions through to the normal handler |
|
2720 | # Pass any other exceptions through to the normal handler | |
2724 | return self.handle_normal(line_info) |
|
2721 | return self.handle_normal(line_info) | |
2725 | else: |
|
2722 | else: | |
2726 | # If the code compiles ok, we should handle it normally |
|
2723 | # If the code compiles ok, we should handle it normally | |
2727 | return self.handle_normal(line_info) |
|
2724 | return self.handle_normal(line_info) | |
2728 |
|
2725 | |||
2729 | def handle_emacs(self, line_info): |
|
2726 | def handle_emacs(self, line_info): | |
2730 | """Handle input lines marked by python-mode.""" |
|
2727 | """Handle input lines marked by python-mode.""" | |
2731 |
|
2728 | |||
2732 | # Currently, nothing is done. Later more functionality can be added |
|
2729 | # Currently, nothing is done. Later more functionality can be added | |
2733 | # here if needed. |
|
2730 | # here if needed. | |
2734 |
|
2731 | |||
2735 | # The input cache shouldn't be updated |
|
2732 | # The input cache shouldn't be updated | |
2736 | return line_info.line |
|
2733 | return line_info.line | |
2737 |
|
2734 | |||
2738 | def var_expand(self,cmd,depth=0): |
|
2735 | def var_expand(self,cmd,depth=0): | |
2739 | """Expand python variables in a string. |
|
2736 | """Expand python variables in a string. | |
2740 |
|
2737 | |||
2741 | The depth argument indicates how many frames above the caller should |
|
2738 | The depth argument indicates how many frames above the caller should | |
2742 | be walked to look for the local namespace where to expand variables. |
|
2739 | be walked to look for the local namespace where to expand variables. | |
2743 |
|
2740 | |||
2744 | The global namespace for expansion is always the user's interactive |
|
2741 | The global namespace for expansion is always the user's interactive | |
2745 | namespace. |
|
2742 | namespace. | |
2746 | """ |
|
2743 | """ | |
2747 |
|
2744 | |||
2748 | return str(ItplNS(cmd, |
|
2745 | return str(ItplNS(cmd, | |
2749 | self.user_ns, # globals |
|
2746 | self.user_ns, # globals | |
2750 | # Skip our own frame in searching for locals: |
|
2747 | # Skip our own frame in searching for locals: | |
2751 | sys._getframe(depth+1).f_locals # locals |
|
2748 | sys._getframe(depth+1).f_locals # locals | |
2752 | )) |
|
2749 | )) | |
2753 |
|
2750 | |||
2754 | def mktempfile(self,data=None): |
|
2751 | def mktempfile(self,data=None): | |
2755 | """Make a new tempfile and return its filename. |
|
2752 | """Make a new tempfile and return its filename. | |
2756 |
|
2753 | |||
2757 | This makes a call to tempfile.mktemp, but it registers the created |
|
2754 | This makes a call to tempfile.mktemp, but it registers the created | |
2758 | filename internally so ipython cleans it up at exit time. |
|
2755 | filename internally so ipython cleans it up at exit time. | |
2759 |
|
2756 | |||
2760 | Optional inputs: |
|
2757 | Optional inputs: | |
2761 |
|
2758 | |||
2762 | - data(None): if data is given, it gets written out to the temp file |
|
2759 | - data(None): if data is given, it gets written out to the temp file | |
2763 | immediately, and the file is closed again.""" |
|
2760 | immediately, and the file is closed again.""" | |
2764 |
|
2761 | |||
2765 | filename = tempfile.mktemp('.py','ipython_edit_') |
|
2762 | filename = tempfile.mktemp('.py','ipython_edit_') | |
2766 | self.tempfiles.append(filename) |
|
2763 | self.tempfiles.append(filename) | |
2767 |
|
2764 | |||
2768 | if data: |
|
2765 | if data: | |
2769 | tmp_file = open(filename,'w') |
|
2766 | tmp_file = open(filename,'w') | |
2770 | tmp_file.write(data) |
|
2767 | tmp_file.write(data) | |
2771 | tmp_file.close() |
|
2768 | tmp_file.close() | |
2772 | return filename |
|
2769 | return filename | |
2773 |
|
2770 | |||
2774 | def write(self,data): |
|
2771 | def write(self,data): | |
2775 | """Write a string to the default output""" |
|
2772 | """Write a string to the default output""" | |
2776 | Term.cout.write(data) |
|
2773 | Term.cout.write(data) | |
2777 |
|
2774 | |||
2778 | def write_err(self,data): |
|
2775 | def write_err(self,data): | |
2779 | """Write a string to the default error output""" |
|
2776 | """Write a string to the default error output""" | |
2780 | Term.cerr.write(data) |
|
2777 | Term.cerr.write(data) | |
2781 |
|
2778 | |||
2782 | def ask_exit(self): |
|
2779 | def ask_exit(self): | |
2783 | """ Call for exiting. Can be overiden and used as a callback. """ |
|
2780 | """ Call for exiting. Can be overiden and used as a callback. """ | |
2784 | self.exit_now = True |
|
2781 | self.exit_now = True | |
2785 |
|
2782 | |||
2786 | def exit(self): |
|
2783 | def exit(self): | |
2787 | """Handle interactive exit. |
|
2784 | """Handle interactive exit. | |
2788 |
|
2785 | |||
2789 | This method calls the ask_exit callback.""" |
|
2786 | This method calls the ask_exit callback.""" | |
2790 | if self.confirm_exit: |
|
2787 | if self.confirm_exit: | |
2791 | if self.ask_yes_no('Do you really want to exit ([y]/n)?','y'): |
|
2788 | if self.ask_yes_no('Do you really want to exit ([y]/n)?','y'): | |
2792 | self.ask_exit() |
|
2789 | self.ask_exit() | |
2793 | else: |
|
2790 | else: | |
2794 | self.ask_exit() |
|
2791 | self.ask_exit() | |
2795 |
|
2792 | |||
2796 | def safe_execfile(self,fname,*where,**kw): |
|
2793 | def safe_execfile(self,fname,*where,**kw): | |
2797 | """A safe version of the builtin execfile(). |
|
2794 | """A safe version of the builtin execfile(). | |
2798 |
|
2795 | |||
2799 | This version will never throw an exception, and knows how to handle |
|
2796 | This version will never throw an exception, and knows how to handle | |
2800 | ipython logs as well. |
|
2797 | ipython logs as well. | |
2801 |
|
2798 | |||
2802 | :Parameters: |
|
2799 | :Parameters: | |
2803 | fname : string |
|
2800 | fname : string | |
2804 | Name of the file to be executed. |
|
2801 | Name of the file to be executed. | |
2805 |
|
2802 | |||
2806 | where : tuple |
|
2803 | where : tuple | |
2807 | One or two namespaces, passed to execfile() as (globals,locals). |
|
2804 | One or two namespaces, passed to execfile() as (globals,locals). | |
2808 | If only one is given, it is passed as both. |
|
2805 | If only one is given, it is passed as both. | |
2809 |
|
2806 | |||
2810 | :Keywords: |
|
2807 | :Keywords: | |
2811 | islog : boolean (False) |
|
2808 | islog : boolean (False) | |
2812 |
|
2809 | |||
2813 | quiet : boolean (True) |
|
2810 | quiet : boolean (True) | |
2814 |
|
2811 | |||
2815 | exit_ignore : boolean (False) |
|
2812 | exit_ignore : boolean (False) | |
2816 | """ |
|
2813 | """ | |
2817 |
|
2814 | |||
2818 | def syspath_cleanup(): |
|
2815 | def syspath_cleanup(): | |
2819 | """Internal cleanup routine for sys.path.""" |
|
2816 | """Internal cleanup routine for sys.path.""" | |
2820 | if add_dname: |
|
2817 | if add_dname: | |
2821 | try: |
|
2818 | try: | |
2822 | sys.path.remove(dname) |
|
2819 | sys.path.remove(dname) | |
2823 | except ValueError: |
|
2820 | except ValueError: | |
2824 | # For some reason the user has already removed it, ignore. |
|
2821 | # For some reason the user has already removed it, ignore. | |
2825 | pass |
|
2822 | pass | |
2826 |
|
2823 | |||
2827 | fname = os.path.expanduser(fname) |
|
2824 | fname = os.path.expanduser(fname) | |
2828 |
|
2825 | |||
2829 | # Find things also in current directory. This is needed to mimic the |
|
2826 | # Find things also in current directory. This is needed to mimic the | |
2830 | # behavior of running a script from the system command line, where |
|
2827 | # behavior of running a script from the system command line, where | |
2831 | # Python inserts the script's directory into sys.path |
|
2828 | # Python inserts the script's directory into sys.path | |
2832 | dname = os.path.dirname(os.path.abspath(fname)) |
|
2829 | dname = os.path.dirname(os.path.abspath(fname)) | |
2833 | add_dname = False |
|
2830 | add_dname = False | |
2834 | if dname not in sys.path: |
|
2831 | if dname not in sys.path: | |
2835 | sys.path.insert(0,dname) |
|
2832 | sys.path.insert(0,dname) | |
2836 | add_dname = True |
|
2833 | add_dname = True | |
2837 |
|
2834 | |||
2838 | try: |
|
2835 | try: | |
2839 | xfile = open(fname) |
|
2836 | xfile = open(fname) | |
2840 | except: |
|
2837 | except: | |
2841 | print >> Term.cerr, \ |
|
2838 | print >> Term.cerr, \ | |
2842 | 'Could not open file <%s> for safe execution.' % fname |
|
2839 | 'Could not open file <%s> for safe execution.' % fname | |
2843 | syspath_cleanup() |
|
2840 | syspath_cleanup() | |
2844 | return None |
|
2841 | return None | |
2845 |
|
2842 | |||
2846 | kw.setdefault('islog',0) |
|
2843 | kw.setdefault('islog',0) | |
2847 | kw.setdefault('quiet',1) |
|
2844 | kw.setdefault('quiet',1) | |
2848 | kw.setdefault('exit_ignore',0) |
|
2845 | kw.setdefault('exit_ignore',0) | |
2849 |
|
2846 | |||
2850 | first = xfile.readline() |
|
2847 | first = xfile.readline() | |
2851 | loghead = str(self.loghead_tpl).split('\n',1)[0].strip() |
|
2848 | loghead = str(self.loghead_tpl).split('\n',1)[0].strip() | |
2852 | xfile.close() |
|
2849 | xfile.close() | |
2853 | # line by line execution |
|
2850 | # line by line execution | |
2854 | if first.startswith(loghead) or kw['islog']: |
|
2851 | if first.startswith(loghead) or kw['islog']: | |
2855 | print 'Loading log file <%s> one line at a time...' % fname |
|
2852 | print 'Loading log file <%s> one line at a time...' % fname | |
2856 | if kw['quiet']: |
|
2853 | if kw['quiet']: | |
2857 | stdout_save = sys.stdout |
|
2854 | stdout_save = sys.stdout | |
2858 | sys.stdout = StringIO.StringIO() |
|
2855 | sys.stdout = StringIO.StringIO() | |
2859 | try: |
|
2856 | try: | |
2860 | globs,locs = where[0:2] |
|
2857 | globs,locs = where[0:2] | |
2861 | except: |
|
2858 | except: | |
2862 | try: |
|
2859 | try: | |
2863 | globs = locs = where[0] |
|
2860 | globs = locs = where[0] | |
2864 | except: |
|
2861 | except: | |
2865 | globs = locs = globals() |
|
2862 | globs = locs = globals() | |
2866 | badblocks = [] |
|
2863 | badblocks = [] | |
2867 |
|
2864 | |||
2868 | # we also need to identify indented blocks of code when replaying |
|
2865 | # we also need to identify indented blocks of code when replaying | |
2869 | # logs and put them together before passing them to an exec |
|
2866 | # logs and put them together before passing them to an exec | |
2870 | # statement. This takes a bit of regexp and look-ahead work in the |
|
2867 | # statement. This takes a bit of regexp and look-ahead work in the | |
2871 | # file. It's easiest if we swallow the whole thing in memory |
|
2868 | # file. It's easiest if we swallow the whole thing in memory | |
2872 | # first, and manually walk through the lines list moving the |
|
2869 | # first, and manually walk through the lines list moving the | |
2873 | # counter ourselves. |
|
2870 | # counter ourselves. | |
2874 | indent_re = re.compile('\s+\S') |
|
2871 | indent_re = re.compile('\s+\S') | |
2875 | xfile = open(fname) |
|
2872 | xfile = open(fname) | |
2876 | filelines = xfile.readlines() |
|
2873 | filelines = xfile.readlines() | |
2877 | xfile.close() |
|
2874 | xfile.close() | |
2878 | nlines = len(filelines) |
|
2875 | nlines = len(filelines) | |
2879 | lnum = 0 |
|
2876 | lnum = 0 | |
2880 | while lnum < nlines: |
|
2877 | while lnum < nlines: | |
2881 | line = filelines[lnum] |
|
2878 | line = filelines[lnum] | |
2882 | lnum += 1 |
|
2879 | lnum += 1 | |
2883 | # don't re-insert logger status info into cache |
|
2880 | # don't re-insert logger status info into cache | |
2884 | if line.startswith('#log#'): |
|
2881 | if line.startswith('#log#'): | |
2885 | continue |
|
2882 | continue | |
2886 | else: |
|
2883 | else: | |
2887 | # build a block of code (maybe a single line) for execution |
|
2884 | # build a block of code (maybe a single line) for execution | |
2888 | block = line |
|
2885 | block = line | |
2889 | try: |
|
2886 | try: | |
2890 | next = filelines[lnum] # lnum has already incremented |
|
2887 | next = filelines[lnum] # lnum has already incremented | |
2891 | except: |
|
2888 | except: | |
2892 | next = None |
|
2889 | next = None | |
2893 | while next and indent_re.match(next): |
|
2890 | while next and indent_re.match(next): | |
2894 | block += next |
|
2891 | block += next | |
2895 | lnum += 1 |
|
2892 | lnum += 1 | |
2896 | try: |
|
2893 | try: | |
2897 | next = filelines[lnum] |
|
2894 | next = filelines[lnum] | |
2898 | except: |
|
2895 | except: | |
2899 | next = None |
|
2896 | next = None | |
2900 | # now execute the block of one or more lines |
|
2897 | # now execute the block of one or more lines | |
2901 | try: |
|
2898 | try: | |
2902 | exec block in globs,locs |
|
2899 | exec block in globs,locs | |
2903 | except SystemExit: |
|
2900 | except SystemExit: | |
2904 | pass |
|
2901 | pass | |
2905 | except: |
|
2902 | except: | |
2906 | badblocks.append(block.rstrip()) |
|
2903 | badblocks.append(block.rstrip()) | |
2907 | if kw['quiet']: # restore stdout |
|
2904 | if kw['quiet']: # restore stdout | |
2908 | sys.stdout.close() |
|
2905 | sys.stdout.close() | |
2909 | sys.stdout = stdout_save |
|
2906 | sys.stdout = stdout_save | |
2910 | print 'Finished replaying log file <%s>' % fname |
|
2907 | print 'Finished replaying log file <%s>' % fname | |
2911 | if badblocks: |
|
2908 | if badblocks: | |
2912 | print >> sys.stderr, ('\nThe following lines/blocks in file ' |
|
2909 | print >> sys.stderr, ('\nThe following lines/blocks in file ' | |
2913 | '<%s> reported errors:' % fname) |
|
2910 | '<%s> reported errors:' % fname) | |
2914 |
|
2911 | |||
2915 | for badline in badblocks: |
|
2912 | for badline in badblocks: | |
2916 | print >> sys.stderr, badline |
|
2913 | print >> sys.stderr, badline | |
2917 | else: # regular file execution |
|
2914 | else: # regular file execution | |
2918 | try: |
|
2915 | try: | |
2919 | if sys.platform == 'win32' and sys.version_info < (2,5,1): |
|
2916 | if sys.platform == 'win32' and sys.version_info < (2,5,1): | |
2920 | # Work around a bug in Python for Windows. The bug was |
|
2917 | # Work around a bug in Python for Windows. The bug was | |
2921 | # fixed in in Python 2.5 r54159 and 54158, but that's still |
|
2918 | # fixed in in Python 2.5 r54159 and 54158, but that's still | |
2922 | # SVN Python as of March/07. For details, see: |
|
2919 | # SVN Python as of March/07. For details, see: | |
2923 | # http://projects.scipy.org/ipython/ipython/ticket/123 |
|
2920 | # http://projects.scipy.org/ipython/ipython/ticket/123 | |
2924 | try: |
|
2921 | try: | |
2925 | globs,locs = where[0:2] |
|
2922 | globs,locs = where[0:2] | |
2926 | except: |
|
2923 | except: | |
2927 | try: |
|
2924 | try: | |
2928 | globs = locs = where[0] |
|
2925 | globs = locs = where[0] | |
2929 | except: |
|
2926 | except: | |
2930 | globs = locs = globals() |
|
2927 | globs = locs = globals() | |
2931 | exec file(fname) in globs,locs |
|
2928 | exec file(fname) in globs,locs | |
2932 | else: |
|
2929 | else: | |
2933 | execfile(fname,*where) |
|
2930 | execfile(fname,*where) | |
2934 | except SyntaxError: |
|
2931 | except SyntaxError: | |
2935 | self.showsyntaxerror() |
|
2932 | self.showsyntaxerror() | |
2936 | warn('Failure executing file: <%s>' % fname) |
|
2933 | warn('Failure executing file: <%s>' % fname) | |
2937 | except SystemExit,status: |
|
2934 | except SystemExit,status: | |
2938 | # Code that correctly sets the exit status flag to success (0) |
|
2935 | # Code that correctly sets the exit status flag to success (0) | |
2939 | # shouldn't be bothered with a traceback. Note that a plain |
|
2936 | # shouldn't be bothered with a traceback. Note that a plain | |
2940 | # sys.exit() does NOT set the message to 0 (it's empty) so that |
|
2937 | # sys.exit() does NOT set the message to 0 (it's empty) so that | |
2941 | # will still get a traceback. Note that the structure of the |
|
2938 | # will still get a traceback. Note that the structure of the | |
2942 | # SystemExit exception changed between Python 2.4 and 2.5, so |
|
2939 | # SystemExit exception changed between Python 2.4 and 2.5, so | |
2943 | # the checks must be done in a version-dependent way. |
|
2940 | # the checks must be done in a version-dependent way. | |
2944 | show = False |
|
2941 | show = False | |
2945 |
|
2942 | |||
2946 | if sys.version_info[:2] > (2,5): |
|
2943 | if sys.version_info[:2] > (2,5): | |
2947 | if status.message!=0 and not kw['exit_ignore']: |
|
2944 | if status.message!=0 and not kw['exit_ignore']: | |
2948 | show = True |
|
2945 | show = True | |
2949 | else: |
|
2946 | else: | |
2950 | if status.code and not kw['exit_ignore']: |
|
2947 | if status.code and not kw['exit_ignore']: | |
2951 | show = True |
|
2948 | show = True | |
2952 | if show: |
|
2949 | if show: | |
2953 | self.showtraceback() |
|
2950 | self.showtraceback() | |
2954 | warn('Failure executing file: <%s>' % fname) |
|
2951 | warn('Failure executing file: <%s>' % fname) | |
2955 | except: |
|
2952 | except: | |
2956 | self.showtraceback() |
|
2953 | self.showtraceback() | |
2957 | warn('Failure executing file: <%s>' % fname) |
|
2954 | warn('Failure executing file: <%s>' % fname) | |
2958 |
|
2955 | |||
2959 | syspath_cleanup() |
|
2956 | syspath_cleanup() | |
2960 |
|
2957 | |||
2961 | #************************* end of file <iplib.py> ***************************** |
|
2958 | #************************* end of file <iplib.py> ***************************** |
@@ -1,1063 +1,1062 b'' | |||||
1 | # -*- coding: utf-8 -*- |
|
1 | # -*- coding: utf-8 -*- | |
2 | """ |
|
2 | """ | |
3 | ultratb.py -- Spice up your tracebacks! |
|
3 | ultratb.py -- Spice up your tracebacks! | |
4 |
|
4 | |||
5 | * ColorTB |
|
5 | * ColorTB | |
6 | I've always found it a bit hard to visually parse tracebacks in Python. The |
|
6 | I've always found it a bit hard to visually parse tracebacks in Python. The | |
7 | ColorTB class is a solution to that problem. It colors the different parts of a |
|
7 | ColorTB class is a solution to that problem. It colors the different parts of a | |
8 | traceback in a manner similar to what you would expect from a syntax-highlighting |
|
8 | traceback in a manner similar to what you would expect from a syntax-highlighting | |
9 | text editor. |
|
9 | text editor. | |
10 |
|
10 | |||
11 | Installation instructions for ColorTB: |
|
11 | Installation instructions for ColorTB: | |
12 | import sys,ultratb |
|
12 | import sys,ultratb | |
13 | sys.excepthook = ultratb.ColorTB() |
|
13 | sys.excepthook = ultratb.ColorTB() | |
14 |
|
14 | |||
15 | * VerboseTB |
|
15 | * VerboseTB | |
16 | I've also included a port of Ka-Ping Yee's "cgitb.py" that produces all kinds |
|
16 | I've also included a port of Ka-Ping Yee's "cgitb.py" that produces all kinds | |
17 | of useful info when a traceback occurs. Ping originally had it spit out HTML |
|
17 | of useful info when a traceback occurs. Ping originally had it spit out HTML | |
18 | and intended it for CGI programmers, but why should they have all the fun? I |
|
18 | and intended it for CGI programmers, but why should they have all the fun? I | |
19 | altered it to spit out colored text to the terminal. It's a bit overwhelming, |
|
19 | altered it to spit out colored text to the terminal. It's a bit overwhelming, | |
20 | but kind of neat, and maybe useful for long-running programs that you believe |
|
20 | but kind of neat, and maybe useful for long-running programs that you believe | |
21 | are bug-free. If a crash *does* occur in that type of program you want details. |
|
21 | are bug-free. If a crash *does* occur in that type of program you want details. | |
22 | Give it a shot--you'll love it or you'll hate it. |
|
22 | Give it a shot--you'll love it or you'll hate it. | |
23 |
|
23 | |||
24 | Note: |
|
24 | Note: | |
25 |
|
25 | |||
26 | The Verbose mode prints the variables currently visible where the exception |
|
26 | The Verbose mode prints the variables currently visible where the exception | |
27 | happened (shortening their strings if too long). This can potentially be |
|
27 | happened (shortening their strings if too long). This can potentially be | |
28 | very slow, if you happen to have a huge data structure whose string |
|
28 | very slow, if you happen to have a huge data structure whose string | |
29 | representation is complex to compute. Your computer may appear to freeze for |
|
29 | representation is complex to compute. Your computer may appear to freeze for | |
30 | a while with cpu usage at 100%. If this occurs, you can cancel the traceback |
|
30 | a while with cpu usage at 100%. If this occurs, you can cancel the traceback | |
31 | with Ctrl-C (maybe hitting it more than once). |
|
31 | with Ctrl-C (maybe hitting it more than once). | |
32 |
|
32 | |||
33 | If you encounter this kind of situation often, you may want to use the |
|
33 | If you encounter this kind of situation often, you may want to use the | |
34 | Verbose_novars mode instead of the regular Verbose, which avoids formatting |
|
34 | Verbose_novars mode instead of the regular Verbose, which avoids formatting | |
35 | variables (but otherwise includes the information and context given by |
|
35 | variables (but otherwise includes the information and context given by | |
36 | Verbose). |
|
36 | Verbose). | |
37 |
|
37 | |||
38 |
|
38 | |||
39 | Installation instructions for ColorTB: |
|
39 | Installation instructions for ColorTB: | |
40 | import sys,ultratb |
|
40 | import sys,ultratb | |
41 | sys.excepthook = ultratb.VerboseTB() |
|
41 | sys.excepthook = ultratb.VerboseTB() | |
42 |
|
42 | |||
43 | Note: Much of the code in this module was lifted verbatim from the standard |
|
43 | Note: Much of the code in this module was lifted verbatim from the standard | |
44 | library module 'traceback.py' and Ka-Ping Yee's 'cgitb.py'. |
|
44 | library module 'traceback.py' and Ka-Ping Yee's 'cgitb.py'. | |
45 |
|
45 | |||
46 | * Color schemes |
|
46 | * Color schemes | |
47 | The colors are defined in the class TBTools through the use of the |
|
47 | The colors are defined in the class TBTools through the use of the | |
48 | ColorSchemeTable class. Currently the following exist: |
|
48 | ColorSchemeTable class. Currently the following exist: | |
49 |
|
49 | |||
50 | - NoColor: allows all of this module to be used in any terminal (the color |
|
50 | - NoColor: allows all of this module to be used in any terminal (the color | |
51 | escapes are just dummy blank strings). |
|
51 | escapes are just dummy blank strings). | |
52 |
|
52 | |||
53 | - Linux: is meant to look good in a terminal like the Linux console (black |
|
53 | - Linux: is meant to look good in a terminal like the Linux console (black | |
54 | or very dark background). |
|
54 | or very dark background). | |
55 |
|
55 | |||
56 | - LightBG: similar to Linux but swaps dark/light colors to be more readable |
|
56 | - LightBG: similar to Linux but swaps dark/light colors to be more readable | |
57 | in light background terminals. |
|
57 | in light background terminals. | |
58 |
|
58 | |||
59 | You can implement other color schemes easily, the syntax is fairly |
|
59 | You can implement other color schemes easily, the syntax is fairly | |
60 | self-explanatory. Please send back new schemes you develop to the author for |
|
60 | self-explanatory. Please send back new schemes you develop to the author for | |
61 | possible inclusion in future releases. |
|
61 | possible inclusion in future releases. | |
62 | """ |
|
62 | """ | |
63 |
|
63 | |||
64 | #***************************************************************************** |
|
64 | #***************************************************************************** | |
65 | # Copyright (C) 2001 Nathaniel Gray <n8gray@caltech.edu> |
|
65 | # Copyright (C) 2001 Nathaniel Gray <n8gray@caltech.edu> | |
66 | # Copyright (C) 2001-2004 Fernando Perez <fperez@colorado.edu> |
|
66 | # Copyright (C) 2001-2004 Fernando Perez <fperez@colorado.edu> | |
67 | # |
|
67 | # | |
68 | # Distributed under the terms of the BSD License. The full license is in |
|
68 | # Distributed under the terms of the BSD License. The full license is in | |
69 | # the file COPYING, distributed as part of this software. |
|
69 | # the file COPYING, distributed as part of this software. | |
70 | #***************************************************************************** |
|
70 | #***************************************************************************** | |
71 |
|
71 | |||
72 | # Required modules |
|
72 | from __future__ import with_statement | |
|
73 | ||||
73 | import inspect |
|
74 | import inspect | |
74 | import keyword |
|
75 | import keyword | |
75 | import linecache |
|
76 | import linecache | |
76 | import os |
|
77 | import os | |
77 | import pydoc |
|
78 | import pydoc | |
78 | import re |
|
79 | import re | |
79 | import string |
|
80 | import string | |
80 | import sys |
|
81 | import sys | |
81 | import time |
|
82 | import time | |
82 | import tokenize |
|
83 | import tokenize | |
83 | import traceback |
|
84 | import traceback | |
84 | import types |
|
85 | import types | |
85 |
|
86 | |||
86 | # For purposes of monkeypatching inspect to fix a bug in it. |
|
87 | # For purposes of monkeypatching inspect to fix a bug in it. | |
87 | from inspect import getsourcefile, getfile, getmodule,\ |
|
88 | from inspect import getsourcefile, getfile, getmodule,\ | |
88 |
ismodule, |
|
89 | ismodule, isclass, ismethod, isfunction, istraceback, isframe, iscode | |
89 |
|
90 | |||
90 |
|
91 | |||
91 | # IPython's own modules |
|
92 | # IPython's own modules | |
92 | # Modified pdb which doesn't damage IPython's readline handling |
|
93 | # Modified pdb which doesn't damage IPython's readline handling | |
93 | from IPython.utils import PyColorize |
|
94 | from IPython.utils import PyColorize | |
94 | from IPython.core import debugger, ipapi |
|
95 | from IPython.core import debugger, ipapi | |
|
96 | from IPython.core.display_trap import DisplayTrap | |||
95 | from IPython.utils.ipstruct import Struct |
|
97 | from IPython.utils.ipstruct import Struct | |
96 | from IPython.core.excolors import exception_colors |
|
98 | from IPython.core.excolors import exception_colors | |
97 | from IPython.utils.genutils import Term,uniq_stable,error,info |
|
99 | from IPython.utils.genutils import Term, uniq_stable, error, info | |
98 |
|
100 | |||
99 | # Globals |
|
101 | # Globals | |
100 | # amount of space to put line numbers before verbose tracebacks |
|
102 | # amount of space to put line numbers before verbose tracebacks | |
101 | INDENT_SIZE = 8 |
|
103 | INDENT_SIZE = 8 | |
102 |
|
104 | |||
103 | # Default color scheme. This is used, for example, by the traceback |
|
105 | # Default color scheme. This is used, for example, by the traceback | |
104 | # formatter. When running in an actual IPython instance, the user's rc.colors |
|
106 | # formatter. When running in an actual IPython instance, the user's rc.colors | |
105 | # value is used, but havinga module global makes this functionality available |
|
107 | # value is used, but havinga module global makes this functionality available | |
106 | # to users of ultratb who are NOT running inside ipython. |
|
108 | # to users of ultratb who are NOT running inside ipython. | |
107 | DEFAULT_SCHEME = 'NoColor' |
|
109 | DEFAULT_SCHEME = 'NoColor' | |
108 |
|
110 | |||
109 | #--------------------------------------------------------------------------- |
|
111 | #--------------------------------------------------------------------------- | |
110 | # Code begins |
|
112 | # Code begins | |
111 |
|
113 | |||
112 | # Utility functions |
|
114 | # Utility functions | |
113 | def inspect_error(): |
|
115 | def inspect_error(): | |
114 | """Print a message about internal inspect errors. |
|
116 | """Print a message about internal inspect errors. | |
115 |
|
117 | |||
116 | These are unfortunately quite common.""" |
|
118 | These are unfortunately quite common.""" | |
117 |
|
119 | |||
118 | error('Internal Python error in the inspect module.\n' |
|
120 | error('Internal Python error in the inspect module.\n' | |
119 | 'Below is the traceback from this internal error.\n') |
|
121 | 'Below is the traceback from this internal error.\n') | |
120 |
|
122 | |||
121 |
|
123 | |||
122 | def findsource(object): |
|
124 | def findsource(object): | |
123 | """Return the entire source file and starting line number for an object. |
|
125 | """Return the entire source file and starting line number for an object. | |
124 |
|
126 | |||
125 | The argument may be a module, class, method, function, traceback, frame, |
|
127 | The argument may be a module, class, method, function, traceback, frame, | |
126 | or code object. The source code is returned as a list of all the lines |
|
128 | or code object. The source code is returned as a list of all the lines | |
127 | in the file and the line number indexes a line in that list. An IOError |
|
129 | in the file and the line number indexes a line in that list. An IOError | |
128 | is raised if the source code cannot be retrieved. |
|
130 | is raised if the source code cannot be retrieved. | |
129 |
|
131 | |||
130 | FIXED version with which we monkeypatch the stdlib to work around a bug.""" |
|
132 | FIXED version with which we monkeypatch the stdlib to work around a bug.""" | |
131 |
|
133 | |||
132 | file = getsourcefile(object) or getfile(object) |
|
134 | file = getsourcefile(object) or getfile(object) | |
133 | # If the object is a frame, then trying to get the globals dict from its |
|
135 | # If the object is a frame, then trying to get the globals dict from its | |
134 | # module won't work. Instead, the frame object itself has the globals |
|
136 | # module won't work. Instead, the frame object itself has the globals | |
135 | # dictionary. |
|
137 | # dictionary. | |
136 | globals_dict = None |
|
138 | globals_dict = None | |
137 | if inspect.isframe(object): |
|
139 | if inspect.isframe(object): | |
138 | # XXX: can this ever be false? |
|
140 | # XXX: can this ever be false? | |
139 | globals_dict = object.f_globals |
|
141 | globals_dict = object.f_globals | |
140 | else: |
|
142 | else: | |
141 | module = getmodule(object, file) |
|
143 | module = getmodule(object, file) | |
142 | if module: |
|
144 | if module: | |
143 | globals_dict = module.__dict__ |
|
145 | globals_dict = module.__dict__ | |
144 | lines = linecache.getlines(file, globals_dict) |
|
146 | lines = linecache.getlines(file, globals_dict) | |
145 | if not lines: |
|
147 | if not lines: | |
146 | raise IOError('could not get source code') |
|
148 | raise IOError('could not get source code') | |
147 |
|
149 | |||
148 | if ismodule(object): |
|
150 | if ismodule(object): | |
149 | return lines, 0 |
|
151 | return lines, 0 | |
150 |
|
152 | |||
151 | if isclass(object): |
|
153 | if isclass(object): | |
152 | name = object.__name__ |
|
154 | name = object.__name__ | |
153 | pat = re.compile(r'^(\s*)class\s*' + name + r'\b') |
|
155 | pat = re.compile(r'^(\s*)class\s*' + name + r'\b') | |
154 | # make some effort to find the best matching class definition: |
|
156 | # make some effort to find the best matching class definition: | |
155 | # use the one with the least indentation, which is the one |
|
157 | # use the one with the least indentation, which is the one | |
156 | # that's most probably not inside a function definition. |
|
158 | # that's most probably not inside a function definition. | |
157 | candidates = [] |
|
159 | candidates = [] | |
158 | for i in range(len(lines)): |
|
160 | for i in range(len(lines)): | |
159 | match = pat.match(lines[i]) |
|
161 | match = pat.match(lines[i]) | |
160 | if match: |
|
162 | if match: | |
161 | # if it's at toplevel, it's already the best one |
|
163 | # if it's at toplevel, it's already the best one | |
162 | if lines[i][0] == 'c': |
|
164 | if lines[i][0] == 'c': | |
163 | return lines, i |
|
165 | return lines, i | |
164 | # else add whitespace to candidate list |
|
166 | # else add whitespace to candidate list | |
165 | candidates.append((match.group(1), i)) |
|
167 | candidates.append((match.group(1), i)) | |
166 | if candidates: |
|
168 | if candidates: | |
167 | # this will sort by whitespace, and by line number, |
|
169 | # this will sort by whitespace, and by line number, | |
168 | # less whitespace first |
|
170 | # less whitespace first | |
169 | candidates.sort() |
|
171 | candidates.sort() | |
170 | return lines, candidates[0][1] |
|
172 | return lines, candidates[0][1] | |
171 | else: |
|
173 | else: | |
172 | raise IOError('could not find class definition') |
|
174 | raise IOError('could not find class definition') | |
173 |
|
175 | |||
174 | if ismethod(object): |
|
176 | if ismethod(object): | |
175 | object = object.im_func |
|
177 | object = object.im_func | |
176 | if isfunction(object): |
|
178 | if isfunction(object): | |
177 | object = object.func_code |
|
179 | object = object.func_code | |
178 | if istraceback(object): |
|
180 | if istraceback(object): | |
179 | object = object.tb_frame |
|
181 | object = object.tb_frame | |
180 | if isframe(object): |
|
182 | if isframe(object): | |
181 | object = object.f_code |
|
183 | object = object.f_code | |
182 | if iscode(object): |
|
184 | if iscode(object): | |
183 | if not hasattr(object, 'co_firstlineno'): |
|
185 | if not hasattr(object, 'co_firstlineno'): | |
184 | raise IOError('could not find function definition') |
|
186 | raise IOError('could not find function definition') | |
185 | pat = re.compile(r'^(\s*def\s)|(.*(?<!\w)lambda(:|\s))|^(\s*@)') |
|
187 | pat = re.compile(r'^(\s*def\s)|(.*(?<!\w)lambda(:|\s))|^(\s*@)') | |
186 | pmatch = pat.match |
|
188 | pmatch = pat.match | |
187 | # fperez - fix: sometimes, co_firstlineno can give a number larger than |
|
189 | # fperez - fix: sometimes, co_firstlineno can give a number larger than | |
188 | # the length of lines, which causes an error. Safeguard against that. |
|
190 | # the length of lines, which causes an error. Safeguard against that. | |
189 | lnum = min(object.co_firstlineno,len(lines))-1 |
|
191 | lnum = min(object.co_firstlineno,len(lines))-1 | |
190 | while lnum > 0: |
|
192 | while lnum > 0: | |
191 | if pmatch(lines[lnum]): break |
|
193 | if pmatch(lines[lnum]): break | |
192 | lnum -= 1 |
|
194 | lnum -= 1 | |
193 |
|
195 | |||
194 | return lines, lnum |
|
196 | return lines, lnum | |
195 | raise IOError('could not find code object') |
|
197 | raise IOError('could not find code object') | |
196 |
|
198 | |||
197 | # Monkeypatch inspect to apply our bugfix. This code only works with py25 |
|
199 | # Monkeypatch inspect to apply our bugfix. This code only works with py25 | |
198 | if sys.version_info[:2] >= (2,5): |
|
200 | if sys.version_info[:2] >= (2,5): | |
199 | inspect.findsource = findsource |
|
201 | inspect.findsource = findsource | |
200 |
|
202 | |||
201 | def fix_frame_records_filenames(records): |
|
203 | def fix_frame_records_filenames(records): | |
202 | """Try to fix the filenames in each record from inspect.getinnerframes(). |
|
204 | """Try to fix the filenames in each record from inspect.getinnerframes(). | |
203 |
|
205 | |||
204 | Particularly, modules loaded from within zip files have useless filenames |
|
206 | Particularly, modules loaded from within zip files have useless filenames | |
205 | attached to their code object, and inspect.getinnerframes() just uses it. |
|
207 | attached to their code object, and inspect.getinnerframes() just uses it. | |
206 | """ |
|
208 | """ | |
207 | fixed_records = [] |
|
209 | fixed_records = [] | |
208 | for frame, filename, line_no, func_name, lines, index in records: |
|
210 | for frame, filename, line_no, func_name, lines, index in records: | |
209 | # Look inside the frame's globals dictionary for __file__, which should |
|
211 | # Look inside the frame's globals dictionary for __file__, which should | |
210 | # be better. |
|
212 | # be better. | |
211 | better_fn = frame.f_globals.get('__file__', None) |
|
213 | better_fn = frame.f_globals.get('__file__', None) | |
212 | if isinstance(better_fn, str): |
|
214 | if isinstance(better_fn, str): | |
213 | # Check the type just in case someone did something weird with |
|
215 | # Check the type just in case someone did something weird with | |
214 | # __file__. It might also be None if the error occurred during |
|
216 | # __file__. It might also be None if the error occurred during | |
215 | # import. |
|
217 | # import. | |
216 | filename = better_fn |
|
218 | filename = better_fn | |
217 | fixed_records.append((frame, filename, line_no, func_name, lines, index)) |
|
219 | fixed_records.append((frame, filename, line_no, func_name, lines, index)) | |
218 | return fixed_records |
|
220 | return fixed_records | |
219 |
|
221 | |||
220 |
|
222 | |||
221 | def _fixed_getinnerframes(etb, context=1,tb_offset=0): |
|
223 | def _fixed_getinnerframes(etb, context=1,tb_offset=0): | |
222 | import linecache |
|
224 | import linecache | |
223 | LNUM_POS, LINES_POS, INDEX_POS = 2, 4, 5 |
|
225 | LNUM_POS, LINES_POS, INDEX_POS = 2, 4, 5 | |
224 |
|
226 | |||
225 | records = fix_frame_records_filenames(inspect.getinnerframes(etb, context)) |
|
227 | records = fix_frame_records_filenames(inspect.getinnerframes(etb, context)) | |
226 |
|
228 | |||
227 | # If the error is at the console, don't build any context, since it would |
|
229 | # If the error is at the console, don't build any context, since it would | |
228 | # otherwise produce 5 blank lines printed out (there is no file at the |
|
230 | # otherwise produce 5 blank lines printed out (there is no file at the | |
229 | # console) |
|
231 | # console) | |
230 | rec_check = records[tb_offset:] |
|
232 | rec_check = records[tb_offset:] | |
231 | try: |
|
233 | try: | |
232 | rname = rec_check[0][1] |
|
234 | rname = rec_check[0][1] | |
233 | if rname == '<ipython console>' or rname.endswith('<string>'): |
|
235 | if rname == '<ipython console>' or rname.endswith('<string>'): | |
234 | return rec_check |
|
236 | return rec_check | |
235 | except IndexError: |
|
237 | except IndexError: | |
236 | pass |
|
238 | pass | |
237 |
|
239 | |||
238 | aux = traceback.extract_tb(etb) |
|
240 | aux = traceback.extract_tb(etb) | |
239 | assert len(records) == len(aux) |
|
241 | assert len(records) == len(aux) | |
240 | for i, (file, lnum, _, _) in zip(range(len(records)), aux): |
|
242 | for i, (file, lnum, _, _) in zip(range(len(records)), aux): | |
241 | maybeStart = lnum-1 - context//2 |
|
243 | maybeStart = lnum-1 - context//2 | |
242 | start = max(maybeStart, 0) |
|
244 | start = max(maybeStart, 0) | |
243 | end = start + context |
|
245 | end = start + context | |
244 | lines = linecache.getlines(file)[start:end] |
|
246 | lines = linecache.getlines(file)[start:end] | |
245 | # pad with empty lines if necessary |
|
247 | # pad with empty lines if necessary | |
246 | if maybeStart < 0: |
|
248 | if maybeStart < 0: | |
247 | lines = (['\n'] * -maybeStart) + lines |
|
249 | lines = (['\n'] * -maybeStart) + lines | |
248 | if len(lines) < context: |
|
250 | if len(lines) < context: | |
249 | lines += ['\n'] * (context - len(lines)) |
|
251 | lines += ['\n'] * (context - len(lines)) | |
250 | buf = list(records[i]) |
|
252 | buf = list(records[i]) | |
251 | buf[LNUM_POS] = lnum |
|
253 | buf[LNUM_POS] = lnum | |
252 | buf[INDEX_POS] = lnum - 1 - start |
|
254 | buf[INDEX_POS] = lnum - 1 - start | |
253 | buf[LINES_POS] = lines |
|
255 | buf[LINES_POS] = lines | |
254 | records[i] = tuple(buf) |
|
256 | records[i] = tuple(buf) | |
255 | return records[tb_offset:] |
|
257 | return records[tb_offset:] | |
256 |
|
258 | |||
257 | # Helper function -- largely belongs to VerboseTB, but we need the same |
|
259 | # Helper function -- largely belongs to VerboseTB, but we need the same | |
258 | # functionality to produce a pseudo verbose TB for SyntaxErrors, so that they |
|
260 | # functionality to produce a pseudo verbose TB for SyntaxErrors, so that they | |
259 | # can be recognized properly by ipython.el's py-traceback-line-re |
|
261 | # can be recognized properly by ipython.el's py-traceback-line-re | |
260 | # (SyntaxErrors have to be treated specially because they have no traceback) |
|
262 | # (SyntaxErrors have to be treated specially because they have no traceback) | |
261 |
|
263 | |||
262 | _parser = PyColorize.Parser() |
|
264 | _parser = PyColorize.Parser() | |
263 |
|
265 | |||
264 | def _formatTracebackLines(lnum, index, lines, Colors, lvals=None,scheme=None): |
|
266 | def _formatTracebackLines(lnum, index, lines, Colors, lvals=None,scheme=None): | |
265 | numbers_width = INDENT_SIZE - 1 |
|
267 | numbers_width = INDENT_SIZE - 1 | |
266 | res = [] |
|
268 | res = [] | |
267 | i = lnum - index |
|
269 | i = lnum - index | |
268 |
|
270 | |||
269 | # This lets us get fully syntax-highlighted tracebacks. |
|
271 | # This lets us get fully syntax-highlighted tracebacks. | |
270 | if scheme is None: |
|
272 | if scheme is None: | |
271 | ipinst = ipapi.get() |
|
273 | ipinst = ipapi.get() | |
272 | if ipinst is not None: |
|
274 | if ipinst is not None: | |
273 | scheme = ipinst.colors |
|
275 | scheme = ipinst.colors | |
274 | else: |
|
276 | else: | |
275 | scheme = DEFAULT_SCHEME |
|
277 | scheme = DEFAULT_SCHEME | |
276 |
|
278 | |||
277 | _line_format = _parser.format2 |
|
279 | _line_format = _parser.format2 | |
278 |
|
280 | |||
279 | for line in lines: |
|
281 | for line in lines: | |
280 | new_line, err = _line_format(line,'str',scheme) |
|
282 | new_line, err = _line_format(line,'str',scheme) | |
281 | if not err: line = new_line |
|
283 | if not err: line = new_line | |
282 |
|
284 | |||
283 | if i == lnum: |
|
285 | if i == lnum: | |
284 | # This is the line with the error |
|
286 | # This is the line with the error | |
285 | pad = numbers_width - len(str(i)) |
|
287 | pad = numbers_width - len(str(i)) | |
286 | if pad >= 3: |
|
288 | if pad >= 3: | |
287 | marker = '-'*(pad-3) + '-> ' |
|
289 | marker = '-'*(pad-3) + '-> ' | |
288 | elif pad == 2: |
|
290 | elif pad == 2: | |
289 | marker = '> ' |
|
291 | marker = '> ' | |
290 | elif pad == 1: |
|
292 | elif pad == 1: | |
291 | marker = '>' |
|
293 | marker = '>' | |
292 | else: |
|
294 | else: | |
293 | marker = '' |
|
295 | marker = '' | |
294 | num = marker + str(i) |
|
296 | num = marker + str(i) | |
295 | line = '%s%s%s %s%s' %(Colors.linenoEm, num, |
|
297 | line = '%s%s%s %s%s' %(Colors.linenoEm, num, | |
296 | Colors.line, line, Colors.Normal) |
|
298 | Colors.line, line, Colors.Normal) | |
297 | else: |
|
299 | else: | |
298 | num = '%*s' % (numbers_width,i) |
|
300 | num = '%*s' % (numbers_width,i) | |
299 | line = '%s%s%s %s' %(Colors.lineno, num, |
|
301 | line = '%s%s%s %s' %(Colors.lineno, num, | |
300 | Colors.Normal, line) |
|
302 | Colors.Normal, line) | |
301 |
|
303 | |||
302 | res.append(line) |
|
304 | res.append(line) | |
303 | if lvals and i == lnum: |
|
305 | if lvals and i == lnum: | |
304 | res.append(lvals + '\n') |
|
306 | res.append(lvals + '\n') | |
305 | i = i + 1 |
|
307 | i = i + 1 | |
306 | return res |
|
308 | return res | |
307 |
|
309 | |||
308 |
|
310 | |||
309 | #--------------------------------------------------------------------------- |
|
311 | #--------------------------------------------------------------------------- | |
310 | # Module classes |
|
312 | # Module classes | |
311 | class TBTools: |
|
313 | class TBTools: | |
312 | """Basic tools used by all traceback printer classes.""" |
|
314 | """Basic tools used by all traceback printer classes.""" | |
313 |
|
315 | |||
314 | def __init__(self,color_scheme = 'NoColor',call_pdb=False): |
|
316 | def __init__(self,color_scheme = 'NoColor',call_pdb=False): | |
315 | # Whether to call the interactive pdb debugger after printing |
|
317 | # Whether to call the interactive pdb debugger after printing | |
316 | # tracebacks or not |
|
318 | # tracebacks or not | |
317 | self.call_pdb = call_pdb |
|
319 | self.call_pdb = call_pdb | |
318 |
|
320 | |||
319 | # Create color table |
|
321 | # Create color table | |
320 | self.color_scheme_table = exception_colors() |
|
322 | self.color_scheme_table = exception_colors() | |
321 |
|
323 | |||
322 | self.set_colors(color_scheme) |
|
324 | self.set_colors(color_scheme) | |
323 | self.old_scheme = color_scheme # save initial value for toggles |
|
325 | self.old_scheme = color_scheme # save initial value for toggles | |
324 |
|
326 | |||
325 | if call_pdb: |
|
327 | if call_pdb: | |
326 | self.pdb = debugger.Pdb(self.color_scheme_table.active_scheme_name) |
|
328 | self.pdb = debugger.Pdb(self.color_scheme_table.active_scheme_name) | |
327 | else: |
|
329 | else: | |
328 | self.pdb = None |
|
330 | self.pdb = None | |
329 |
|
331 | |||
330 | def set_colors(self,*args,**kw): |
|
332 | def set_colors(self,*args,**kw): | |
331 | """Shorthand access to the color table scheme selector method.""" |
|
333 | """Shorthand access to the color table scheme selector method.""" | |
332 |
|
334 | |||
333 | # Set own color table |
|
335 | # Set own color table | |
334 | self.color_scheme_table.set_active_scheme(*args,**kw) |
|
336 | self.color_scheme_table.set_active_scheme(*args,**kw) | |
335 | # for convenience, set Colors to the active scheme |
|
337 | # for convenience, set Colors to the active scheme | |
336 | self.Colors = self.color_scheme_table.active_colors |
|
338 | self.Colors = self.color_scheme_table.active_colors | |
337 | # Also set colors of debugger |
|
339 | # Also set colors of debugger | |
338 | if hasattr(self,'pdb') and self.pdb is not None: |
|
340 | if hasattr(self,'pdb') and self.pdb is not None: | |
339 | self.pdb.set_colors(*args,**kw) |
|
341 | self.pdb.set_colors(*args,**kw) | |
340 |
|
342 | |||
341 | def color_toggle(self): |
|
343 | def color_toggle(self): | |
342 | """Toggle between the currently active color scheme and NoColor.""" |
|
344 | """Toggle between the currently active color scheme and NoColor.""" | |
343 |
|
345 | |||
344 | if self.color_scheme_table.active_scheme_name == 'NoColor': |
|
346 | if self.color_scheme_table.active_scheme_name == 'NoColor': | |
345 | self.color_scheme_table.set_active_scheme(self.old_scheme) |
|
347 | self.color_scheme_table.set_active_scheme(self.old_scheme) | |
346 | self.Colors = self.color_scheme_table.active_colors |
|
348 | self.Colors = self.color_scheme_table.active_colors | |
347 | else: |
|
349 | else: | |
348 | self.old_scheme = self.color_scheme_table.active_scheme_name |
|
350 | self.old_scheme = self.color_scheme_table.active_scheme_name | |
349 | self.color_scheme_table.set_active_scheme('NoColor') |
|
351 | self.color_scheme_table.set_active_scheme('NoColor') | |
350 | self.Colors = self.color_scheme_table.active_colors |
|
352 | self.Colors = self.color_scheme_table.active_colors | |
351 |
|
353 | |||
352 | #--------------------------------------------------------------------------- |
|
354 | #--------------------------------------------------------------------------- | |
353 | class ListTB(TBTools): |
|
355 | class ListTB(TBTools): | |
354 | """Print traceback information from a traceback list, with optional color. |
|
356 | """Print traceback information from a traceback list, with optional color. | |
355 |
|
357 | |||
356 | Calling: requires 3 arguments: |
|
358 | Calling: requires 3 arguments: | |
357 | (etype, evalue, elist) |
|
359 | (etype, evalue, elist) | |
358 | as would be obtained by: |
|
360 | as would be obtained by: | |
359 | etype, evalue, tb = sys.exc_info() |
|
361 | etype, evalue, tb = sys.exc_info() | |
360 | if tb: |
|
362 | if tb: | |
361 | elist = traceback.extract_tb(tb) |
|
363 | elist = traceback.extract_tb(tb) | |
362 | else: |
|
364 | else: | |
363 | elist = None |
|
365 | elist = None | |
364 |
|
366 | |||
365 | It can thus be used by programs which need to process the traceback before |
|
367 | It can thus be used by programs which need to process the traceback before | |
366 | printing (such as console replacements based on the code module from the |
|
368 | printing (such as console replacements based on the code module from the | |
367 | standard library). |
|
369 | standard library). | |
368 |
|
370 | |||
369 | Because they are meant to be called without a full traceback (only a |
|
371 | Because they are meant to be called without a full traceback (only a | |
370 | list), instances of this class can't call the interactive pdb debugger.""" |
|
372 | list), instances of this class can't call the interactive pdb debugger.""" | |
371 |
|
373 | |||
372 | def __init__(self,color_scheme = 'NoColor'): |
|
374 | def __init__(self,color_scheme = 'NoColor'): | |
373 | TBTools.__init__(self,color_scheme = color_scheme,call_pdb=0) |
|
375 | TBTools.__init__(self,color_scheme = color_scheme,call_pdb=0) | |
374 |
|
376 | |||
375 | def __call__(self, etype, value, elist): |
|
377 | def __call__(self, etype, value, elist): | |
376 | Term.cout.flush() |
|
378 | Term.cout.flush() | |
377 | print >> Term.cerr, self.text(etype,value,elist) |
|
379 | print >> Term.cerr, self.text(etype,value,elist) | |
378 | Term.cerr.flush() |
|
380 | Term.cerr.flush() | |
379 |
|
381 | |||
380 | def text(self,etype, value, elist,context=5): |
|
382 | def text(self,etype, value, elist,context=5): | |
381 | """Return a color formatted string with the traceback info.""" |
|
383 | """Return a color formatted string with the traceback info.""" | |
382 |
|
384 | |||
383 | Colors = self.Colors |
|
385 | Colors = self.Colors | |
384 | out_string = ['%s%s%s\n' % (Colors.topline,'-'*60,Colors.Normal)] |
|
386 | out_string = ['%s%s%s\n' % (Colors.topline,'-'*60,Colors.Normal)] | |
385 | if elist: |
|
387 | if elist: | |
386 | out_string.append('Traceback %s(most recent call last)%s:' % \ |
|
388 | out_string.append('Traceback %s(most recent call last)%s:' % \ | |
387 | (Colors.normalEm, Colors.Normal) + '\n') |
|
389 | (Colors.normalEm, Colors.Normal) + '\n') | |
388 | out_string.extend(self._format_list(elist)) |
|
390 | out_string.extend(self._format_list(elist)) | |
389 | lines = self._format_exception_only(etype, value) |
|
391 | lines = self._format_exception_only(etype, value) | |
390 | for line in lines[:-1]: |
|
392 | for line in lines[:-1]: | |
391 | out_string.append(" "+line) |
|
393 | out_string.append(" "+line) | |
392 | out_string.append(lines[-1]) |
|
394 | out_string.append(lines[-1]) | |
393 | return ''.join(out_string) |
|
395 | return ''.join(out_string) | |
394 |
|
396 | |||
395 | def _format_list(self, extracted_list): |
|
397 | def _format_list(self, extracted_list): | |
396 | """Format a list of traceback entry tuples for printing. |
|
398 | """Format a list of traceback entry tuples for printing. | |
397 |
|
399 | |||
398 | Given a list of tuples as returned by extract_tb() or |
|
400 | Given a list of tuples as returned by extract_tb() or | |
399 | extract_stack(), return a list of strings ready for printing. |
|
401 | extract_stack(), return a list of strings ready for printing. | |
400 | Each string in the resulting list corresponds to the item with the |
|
402 | Each string in the resulting list corresponds to the item with the | |
401 | same index in the argument list. Each string ends in a newline; |
|
403 | same index in the argument list. Each string ends in a newline; | |
402 | the strings may contain internal newlines as well, for those items |
|
404 | the strings may contain internal newlines as well, for those items | |
403 | whose source text line is not None. |
|
405 | whose source text line is not None. | |
404 |
|
406 | |||
405 | Lifted almost verbatim from traceback.py |
|
407 | Lifted almost verbatim from traceback.py | |
406 | """ |
|
408 | """ | |
407 |
|
409 | |||
408 | Colors = self.Colors |
|
410 | Colors = self.Colors | |
409 | list = [] |
|
411 | list = [] | |
410 | for filename, lineno, name, line in extracted_list[:-1]: |
|
412 | for filename, lineno, name, line in extracted_list[:-1]: | |
411 | item = ' File %s"%s"%s, line %s%d%s, in %s%s%s\n' % \ |
|
413 | item = ' File %s"%s"%s, line %s%d%s, in %s%s%s\n' % \ | |
412 | (Colors.filename, filename, Colors.Normal, |
|
414 | (Colors.filename, filename, Colors.Normal, | |
413 | Colors.lineno, lineno, Colors.Normal, |
|
415 | Colors.lineno, lineno, Colors.Normal, | |
414 | Colors.name, name, Colors.Normal) |
|
416 | Colors.name, name, Colors.Normal) | |
415 | if line: |
|
417 | if line: | |
416 | item = item + ' %s\n' % line.strip() |
|
418 | item = item + ' %s\n' % line.strip() | |
417 | list.append(item) |
|
419 | list.append(item) | |
418 | # Emphasize the last entry |
|
420 | # Emphasize the last entry | |
419 | filename, lineno, name, line = extracted_list[-1] |
|
421 | filename, lineno, name, line = extracted_list[-1] | |
420 | item = '%s File %s"%s"%s, line %s%d%s, in %s%s%s%s\n' % \ |
|
422 | item = '%s File %s"%s"%s, line %s%d%s, in %s%s%s%s\n' % \ | |
421 | (Colors.normalEm, |
|
423 | (Colors.normalEm, | |
422 | Colors.filenameEm, filename, Colors.normalEm, |
|
424 | Colors.filenameEm, filename, Colors.normalEm, | |
423 | Colors.linenoEm, lineno, Colors.normalEm, |
|
425 | Colors.linenoEm, lineno, Colors.normalEm, | |
424 | Colors.nameEm, name, Colors.normalEm, |
|
426 | Colors.nameEm, name, Colors.normalEm, | |
425 | Colors.Normal) |
|
427 | Colors.Normal) | |
426 | if line: |
|
428 | if line: | |
427 | item = item + '%s %s%s\n' % (Colors.line, line.strip(), |
|
429 | item = item + '%s %s%s\n' % (Colors.line, line.strip(), | |
428 | Colors.Normal) |
|
430 | Colors.Normal) | |
429 | list.append(item) |
|
431 | list.append(item) | |
430 | return list |
|
432 | return list | |
431 |
|
433 | |||
432 | def _format_exception_only(self, etype, value): |
|
434 | def _format_exception_only(self, etype, value): | |
433 | """Format the exception part of a traceback. |
|
435 | """Format the exception part of a traceback. | |
434 |
|
436 | |||
435 | The arguments are the exception type and value such as given by |
|
437 | The arguments are the exception type and value such as given by | |
436 | sys.exc_info()[:2]. The return value is a list of strings, each ending |
|
438 | sys.exc_info()[:2]. The return value is a list of strings, each ending | |
437 | in a newline. Normally, the list contains a single string; however, |
|
439 | in a newline. Normally, the list contains a single string; however, | |
438 | for SyntaxError exceptions, it contains several lines that (when |
|
440 | for SyntaxError exceptions, it contains several lines that (when | |
439 | printed) display detailed information about where the syntax error |
|
441 | printed) display detailed information about where the syntax error | |
440 | occurred. The message indicating which exception occurred is the |
|
442 | occurred. The message indicating which exception occurred is the | |
441 | always last string in the list. |
|
443 | always last string in the list. | |
442 |
|
444 | |||
443 | Also lifted nearly verbatim from traceback.py |
|
445 | Also lifted nearly verbatim from traceback.py | |
444 | """ |
|
446 | """ | |
445 |
|
447 | |||
446 | have_filedata = False |
|
448 | have_filedata = False | |
447 | Colors = self.Colors |
|
449 | Colors = self.Colors | |
448 | list = [] |
|
450 | list = [] | |
449 | try: |
|
451 | try: | |
450 | stype = Colors.excName + etype.__name__ + Colors.Normal |
|
452 | stype = Colors.excName + etype.__name__ + Colors.Normal | |
451 | except AttributeError: |
|
453 | except AttributeError: | |
452 | stype = etype # String exceptions don't get special coloring |
|
454 | stype = etype # String exceptions don't get special coloring | |
453 | if value is None: |
|
455 | if value is None: | |
454 | list.append( str(stype) + '\n') |
|
456 | list.append( str(stype) + '\n') | |
455 | else: |
|
457 | else: | |
456 | if etype is SyntaxError: |
|
458 | if etype is SyntaxError: | |
457 | try: |
|
459 | try: | |
458 | msg, (filename, lineno, offset, line) = value |
|
460 | msg, (filename, lineno, offset, line) = value | |
459 | except: |
|
461 | except: | |
460 | have_filedata = False |
|
462 | have_filedata = False | |
461 | else: |
|
463 | else: | |
462 | have_filedata = True |
|
464 | have_filedata = True | |
463 | #print 'filename is',filename # dbg |
|
465 | #print 'filename is',filename # dbg | |
464 | if not filename: filename = "<string>" |
|
466 | if not filename: filename = "<string>" | |
465 | list.append('%s File %s"%s"%s, line %s%d%s\n' % \ |
|
467 | list.append('%s File %s"%s"%s, line %s%d%s\n' % \ | |
466 | (Colors.normalEm, |
|
468 | (Colors.normalEm, | |
467 | Colors.filenameEm, filename, Colors.normalEm, |
|
469 | Colors.filenameEm, filename, Colors.normalEm, | |
468 | Colors.linenoEm, lineno, Colors.Normal )) |
|
470 | Colors.linenoEm, lineno, Colors.Normal )) | |
469 | if line is not None: |
|
471 | if line is not None: | |
470 | i = 0 |
|
472 | i = 0 | |
471 | while i < len(line) and line[i].isspace(): |
|
473 | while i < len(line) and line[i].isspace(): | |
472 | i = i+1 |
|
474 | i = i+1 | |
473 | list.append('%s %s%s\n' % (Colors.line, |
|
475 | list.append('%s %s%s\n' % (Colors.line, | |
474 | line.strip(), |
|
476 | line.strip(), | |
475 | Colors.Normal)) |
|
477 | Colors.Normal)) | |
476 | if offset is not None: |
|
478 | if offset is not None: | |
477 | s = ' ' |
|
479 | s = ' ' | |
478 | for c in line[i:offset-1]: |
|
480 | for c in line[i:offset-1]: | |
479 | if c.isspace(): |
|
481 | if c.isspace(): | |
480 | s = s + c |
|
482 | s = s + c | |
481 | else: |
|
483 | else: | |
482 | s = s + ' ' |
|
484 | s = s + ' ' | |
483 | list.append('%s%s^%s\n' % (Colors.caret, s, |
|
485 | list.append('%s%s^%s\n' % (Colors.caret, s, | |
484 | Colors.Normal) ) |
|
486 | Colors.Normal) ) | |
485 | value = msg |
|
487 | value = msg | |
486 | s = self._some_str(value) |
|
488 | s = self._some_str(value) | |
487 | if s: |
|
489 | if s: | |
488 | list.append('%s%s:%s %s\n' % (str(stype), Colors.excName, |
|
490 | list.append('%s%s:%s %s\n' % (str(stype), Colors.excName, | |
489 | Colors.Normal, s)) |
|
491 | Colors.Normal, s)) | |
490 | else: |
|
492 | else: | |
491 | list.append('%s\n' % str(stype)) |
|
493 | list.append('%s\n' % str(stype)) | |
492 |
|
494 | |||
493 | # vds:>> |
|
495 | # vds:>> | |
494 | if have_filedata: |
|
496 | if have_filedata: | |
495 | ipinst = ipapi.get() |
|
497 | ipinst = ipapi.get() | |
496 | if ipinst is not None: |
|
498 | if ipinst is not None: | |
497 | ipinst.hooks.synchronize_with_editor(filename, lineno, 0) |
|
499 | ipinst.hooks.synchronize_with_editor(filename, lineno, 0) | |
498 | # vds:<< |
|
500 | # vds:<< | |
499 |
|
501 | |||
500 | return list |
|
502 | return list | |
501 |
|
503 | |||
502 | def _some_str(self, value): |
|
504 | def _some_str(self, value): | |
503 | # Lifted from traceback.py |
|
505 | # Lifted from traceback.py | |
504 | try: |
|
506 | try: | |
505 | return str(value) |
|
507 | return str(value) | |
506 | except: |
|
508 | except: | |
507 | return '<unprintable %s object>' % type(value).__name__ |
|
509 | return '<unprintable %s object>' % type(value).__name__ | |
508 |
|
510 | |||
509 | #---------------------------------------------------------------------------- |
|
511 | #---------------------------------------------------------------------------- | |
510 | class VerboseTB(TBTools): |
|
512 | class VerboseTB(TBTools): | |
511 | """A port of Ka-Ping Yee's cgitb.py module that outputs color text instead |
|
513 | """A port of Ka-Ping Yee's cgitb.py module that outputs color text instead | |
512 | of HTML. Requires inspect and pydoc. Crazy, man. |
|
514 | of HTML. Requires inspect and pydoc. Crazy, man. | |
513 |
|
515 | |||
514 | Modified version which optionally strips the topmost entries from the |
|
516 | Modified version which optionally strips the topmost entries from the | |
515 | traceback, to be used with alternate interpreters (because their own code |
|
517 | traceback, to be used with alternate interpreters (because their own code | |
516 | would appear in the traceback).""" |
|
518 | would appear in the traceback).""" | |
517 |
|
519 | |||
518 | def __init__(self,color_scheme = 'Linux',tb_offset=0,long_header=0, |
|
520 | def __init__(self,color_scheme = 'Linux',tb_offset=0,long_header=0, | |
519 | call_pdb = 0, include_vars=1): |
|
521 | call_pdb = 0, include_vars=1): | |
520 | """Specify traceback offset, headers and color scheme. |
|
522 | """Specify traceback offset, headers and color scheme. | |
521 |
|
523 | |||
522 | Define how many frames to drop from the tracebacks. Calling it with |
|
524 | Define how many frames to drop from the tracebacks. Calling it with | |
523 | tb_offset=1 allows use of this handler in interpreters which will have |
|
525 | tb_offset=1 allows use of this handler in interpreters which will have | |
524 | their own code at the top of the traceback (VerboseTB will first |
|
526 | their own code at the top of the traceback (VerboseTB will first | |
525 | remove that frame before printing the traceback info).""" |
|
527 | remove that frame before printing the traceback info).""" | |
526 | TBTools.__init__(self,color_scheme=color_scheme,call_pdb=call_pdb) |
|
528 | TBTools.__init__(self,color_scheme=color_scheme,call_pdb=call_pdb) | |
527 | self.tb_offset = tb_offset |
|
529 | self.tb_offset = tb_offset | |
528 | self.long_header = long_header |
|
530 | self.long_header = long_header | |
529 | self.include_vars = include_vars |
|
531 | self.include_vars = include_vars | |
530 |
|
532 | |||
531 | def text(self, etype, evalue, etb, context=5): |
|
533 | def text(self, etype, evalue, etb, context=5): | |
532 | """Return a nice text document describing the traceback.""" |
|
534 | """Return a nice text document describing the traceback.""" | |
533 |
|
535 | |||
534 | # some locals |
|
536 | # some locals | |
535 | try: |
|
537 | try: | |
536 | etype = etype.__name__ |
|
538 | etype = etype.__name__ | |
537 | except AttributeError: |
|
539 | except AttributeError: | |
538 | pass |
|
540 | pass | |
539 | Colors = self.Colors # just a shorthand + quicker name lookup |
|
541 | Colors = self.Colors # just a shorthand + quicker name lookup | |
540 | ColorsNormal = Colors.Normal # used a lot |
|
542 | ColorsNormal = Colors.Normal # used a lot | |
541 | col_scheme = self.color_scheme_table.active_scheme_name |
|
543 | col_scheme = self.color_scheme_table.active_scheme_name | |
542 | indent = ' '*INDENT_SIZE |
|
544 | indent = ' '*INDENT_SIZE | |
543 | em_normal = '%s\n%s%s' % (Colors.valEm, indent,ColorsNormal) |
|
545 | em_normal = '%s\n%s%s' % (Colors.valEm, indent,ColorsNormal) | |
544 | undefined = '%sundefined%s' % (Colors.em, ColorsNormal) |
|
546 | undefined = '%sundefined%s' % (Colors.em, ColorsNormal) | |
545 | exc = '%s%s%s' % (Colors.excName,etype,ColorsNormal) |
|
547 | exc = '%s%s%s' % (Colors.excName,etype,ColorsNormal) | |
546 |
|
548 | |||
547 | # some internal-use functions |
|
549 | # some internal-use functions | |
548 | def text_repr(value): |
|
550 | def text_repr(value): | |
549 | """Hopefully pretty robust repr equivalent.""" |
|
551 | """Hopefully pretty robust repr equivalent.""" | |
550 | # this is pretty horrible but should always return *something* |
|
552 | # this is pretty horrible but should always return *something* | |
551 | try: |
|
553 | try: | |
552 | return pydoc.text.repr(value) |
|
554 | return pydoc.text.repr(value) | |
553 | except KeyboardInterrupt: |
|
555 | except KeyboardInterrupt: | |
554 | raise |
|
556 | raise | |
555 | except: |
|
557 | except: | |
556 | try: |
|
558 | try: | |
557 | return repr(value) |
|
559 | return repr(value) | |
558 | except KeyboardInterrupt: |
|
560 | except KeyboardInterrupt: | |
559 | raise |
|
561 | raise | |
560 | except: |
|
562 | except: | |
561 | try: |
|
563 | try: | |
562 | # all still in an except block so we catch |
|
564 | # all still in an except block so we catch | |
563 | # getattr raising |
|
565 | # getattr raising | |
564 | name = getattr(value, '__name__', None) |
|
566 | name = getattr(value, '__name__', None) | |
565 | if name: |
|
567 | if name: | |
566 | # ick, recursion |
|
568 | # ick, recursion | |
567 | return text_repr(name) |
|
569 | return text_repr(name) | |
568 | klass = getattr(value, '__class__', None) |
|
570 | klass = getattr(value, '__class__', None) | |
569 | if klass: |
|
571 | if klass: | |
570 | return '%s instance' % text_repr(klass) |
|
572 | return '%s instance' % text_repr(klass) | |
571 | except KeyboardInterrupt: |
|
573 | except KeyboardInterrupt: | |
572 | raise |
|
574 | raise | |
573 | except: |
|
575 | except: | |
574 | return 'UNRECOVERABLE REPR FAILURE' |
|
576 | return 'UNRECOVERABLE REPR FAILURE' | |
575 | def eqrepr(value, repr=text_repr): return '=%s' % repr(value) |
|
577 | def eqrepr(value, repr=text_repr): return '=%s' % repr(value) | |
576 | def nullrepr(value, repr=text_repr): return '' |
|
578 | def nullrepr(value, repr=text_repr): return '' | |
577 |
|
579 | |||
578 | # meat of the code begins |
|
580 | # meat of the code begins | |
579 | try: |
|
581 | try: | |
580 | etype = etype.__name__ |
|
582 | etype = etype.__name__ | |
581 | except AttributeError: |
|
583 | except AttributeError: | |
582 | pass |
|
584 | pass | |
583 |
|
585 | |||
584 | if self.long_header: |
|
586 | if self.long_header: | |
585 | # Header with the exception type, python version, and date |
|
587 | # Header with the exception type, python version, and date | |
586 | pyver = 'Python ' + string.split(sys.version)[0] + ': ' + sys.executable |
|
588 | pyver = 'Python ' + string.split(sys.version)[0] + ': ' + sys.executable | |
587 | date = time.ctime(time.time()) |
|
589 | date = time.ctime(time.time()) | |
588 |
|
590 | |||
589 | head = '%s%s%s\n%s%s%s\n%s' % (Colors.topline, '-'*75, ColorsNormal, |
|
591 | head = '%s%s%s\n%s%s%s\n%s' % (Colors.topline, '-'*75, ColorsNormal, | |
590 | exc, ' '*(75-len(str(etype))-len(pyver)), |
|
592 | exc, ' '*(75-len(str(etype))-len(pyver)), | |
591 | pyver, string.rjust(date, 75) ) |
|
593 | pyver, string.rjust(date, 75) ) | |
592 | head += "\nA problem occured executing Python code. Here is the sequence of function"\ |
|
594 | head += "\nA problem occured executing Python code. Here is the sequence of function"\ | |
593 | "\ncalls leading up to the error, with the most recent (innermost) call last." |
|
595 | "\ncalls leading up to the error, with the most recent (innermost) call last." | |
594 | else: |
|
596 | else: | |
595 | # Simplified header |
|
597 | # Simplified header | |
596 | head = '%s%s%s\n%s%s' % (Colors.topline, '-'*75, ColorsNormal,exc, |
|
598 | head = '%s%s%s\n%s%s' % (Colors.topline, '-'*75, ColorsNormal,exc, | |
597 | string.rjust('Traceback (most recent call last)', |
|
599 | string.rjust('Traceback (most recent call last)', | |
598 | 75 - len(str(etype)) ) ) |
|
600 | 75 - len(str(etype)) ) ) | |
599 | frames = [] |
|
601 | frames = [] | |
600 | # Flush cache before calling inspect. This helps alleviate some of the |
|
602 | # Flush cache before calling inspect. This helps alleviate some of the | |
601 | # problems with python 2.3's inspect.py. |
|
603 | # problems with python 2.3's inspect.py. | |
602 | linecache.checkcache() |
|
604 | linecache.checkcache() | |
603 | # Drop topmost frames if requested |
|
605 | # Drop topmost frames if requested | |
604 | try: |
|
606 | try: | |
605 | # Try the default getinnerframes and Alex's: Alex's fixes some |
|
607 | # Try the default getinnerframes and Alex's: Alex's fixes some | |
606 | # problems, but it generates empty tracebacks for console errors |
|
608 | # problems, but it generates empty tracebacks for console errors | |
607 | # (5 blanks lines) where none should be returned. |
|
609 | # (5 blanks lines) where none should be returned. | |
608 | #records = inspect.getinnerframes(etb, context)[self.tb_offset:] |
|
610 | #records = inspect.getinnerframes(etb, context)[self.tb_offset:] | |
609 | #print 'python records:', records # dbg |
|
611 | #print 'python records:', records # dbg | |
610 | records = _fixed_getinnerframes(etb, context,self.tb_offset) |
|
612 | records = _fixed_getinnerframes(etb, context,self.tb_offset) | |
611 | #print 'alex records:', records # dbg |
|
613 | #print 'alex records:', records # dbg | |
612 | except: |
|
614 | except: | |
613 |
|
615 | |||
614 | # FIXME: I've been getting many crash reports from python 2.3 |
|
616 | # FIXME: I've been getting many crash reports from python 2.3 | |
615 | # users, traceable to inspect.py. If I can find a small test-case |
|
617 | # users, traceable to inspect.py. If I can find a small test-case | |
616 | # to reproduce this, I should either write a better workaround or |
|
618 | # to reproduce this, I should either write a better workaround or | |
617 | # file a bug report against inspect (if that's the real problem). |
|
619 | # file a bug report against inspect (if that's the real problem). | |
618 | # So far, I haven't been able to find an isolated example to |
|
620 | # So far, I haven't been able to find an isolated example to | |
619 | # reproduce the problem. |
|
621 | # reproduce the problem. | |
620 | inspect_error() |
|
622 | inspect_error() | |
621 | traceback.print_exc(file=Term.cerr) |
|
623 | traceback.print_exc(file=Term.cerr) | |
622 | info('\nUnfortunately, your original traceback can not be constructed.\n') |
|
624 | info('\nUnfortunately, your original traceback can not be constructed.\n') | |
623 | return '' |
|
625 | return '' | |
624 |
|
626 | |||
625 | # build some color string templates outside these nested loops |
|
627 | # build some color string templates outside these nested loops | |
626 | tpl_link = '%s%%s%s' % (Colors.filenameEm,ColorsNormal) |
|
628 | tpl_link = '%s%%s%s' % (Colors.filenameEm,ColorsNormal) | |
627 | tpl_call = 'in %s%%s%s%%s%s' % (Colors.vName, Colors.valEm, |
|
629 | tpl_call = 'in %s%%s%s%%s%s' % (Colors.vName, Colors.valEm, | |
628 | ColorsNormal) |
|
630 | ColorsNormal) | |
629 | tpl_call_fail = 'in %s%%s%s(***failed resolving arguments***)%s' % \ |
|
631 | tpl_call_fail = 'in %s%%s%s(***failed resolving arguments***)%s' % \ | |
630 | (Colors.vName, Colors.valEm, ColorsNormal) |
|
632 | (Colors.vName, Colors.valEm, ColorsNormal) | |
631 | tpl_local_var = '%s%%s%s' % (Colors.vName, ColorsNormal) |
|
633 | tpl_local_var = '%s%%s%s' % (Colors.vName, ColorsNormal) | |
632 | tpl_global_var = '%sglobal%s %s%%s%s' % (Colors.em, ColorsNormal, |
|
634 | tpl_global_var = '%sglobal%s %s%%s%s' % (Colors.em, ColorsNormal, | |
633 | Colors.vName, ColorsNormal) |
|
635 | Colors.vName, ColorsNormal) | |
634 | tpl_name_val = '%%s %s= %%s%s' % (Colors.valEm, ColorsNormal) |
|
636 | tpl_name_val = '%%s %s= %%s%s' % (Colors.valEm, ColorsNormal) | |
635 | tpl_line = '%s%%s%s %%s' % (Colors.lineno, ColorsNormal) |
|
637 | tpl_line = '%s%%s%s %%s' % (Colors.lineno, ColorsNormal) | |
636 | tpl_line_em = '%s%%s%s %%s%s' % (Colors.linenoEm,Colors.line, |
|
638 | tpl_line_em = '%s%%s%s %%s%s' % (Colors.linenoEm,Colors.line, | |
637 | ColorsNormal) |
|
639 | ColorsNormal) | |
638 |
|
640 | |||
639 | # now, loop over all records printing context and info |
|
641 | # now, loop over all records printing context and info | |
640 | abspath = os.path.abspath |
|
642 | abspath = os.path.abspath | |
641 | for frame, file, lnum, func, lines, index in records: |
|
643 | for frame, file, lnum, func, lines, index in records: | |
642 | #print '*** record:',file,lnum,func,lines,index # dbg |
|
644 | #print '*** record:',file,lnum,func,lines,index # dbg | |
643 | try: |
|
645 | try: | |
644 | file = file and abspath(file) or '?' |
|
646 | file = file and abspath(file) or '?' | |
645 | except OSError: |
|
647 | except OSError: | |
646 | # if file is '<console>' or something not in the filesystem, |
|
648 | # if file is '<console>' or something not in the filesystem, | |
647 | # the abspath call will throw an OSError. Just ignore it and |
|
649 | # the abspath call will throw an OSError. Just ignore it and | |
648 | # keep the original file string. |
|
650 | # keep the original file string. | |
649 | pass |
|
651 | pass | |
650 | link = tpl_link % file |
|
652 | link = tpl_link % file | |
651 | try: |
|
653 | try: | |
652 | args, varargs, varkw, locals = inspect.getargvalues(frame) |
|
654 | args, varargs, varkw, locals = inspect.getargvalues(frame) | |
653 | except: |
|
655 | except: | |
654 | # This can happen due to a bug in python2.3. We should be |
|
656 | # This can happen due to a bug in python2.3. We should be | |
655 | # able to remove this try/except when 2.4 becomes a |
|
657 | # able to remove this try/except when 2.4 becomes a | |
656 | # requirement. Bug details at http://python.org/sf/1005466 |
|
658 | # requirement. Bug details at http://python.org/sf/1005466 | |
657 | inspect_error() |
|
659 | inspect_error() | |
658 | traceback.print_exc(file=Term.cerr) |
|
660 | traceback.print_exc(file=Term.cerr) | |
659 | info("\nIPython's exception reporting continues...\n") |
|
661 | info("\nIPython's exception reporting continues...\n") | |
660 |
|
662 | |||
661 | if func == '?': |
|
663 | if func == '?': | |
662 | call = '' |
|
664 | call = '' | |
663 | else: |
|
665 | else: | |
664 | # Decide whether to include variable details or not |
|
666 | # Decide whether to include variable details or not | |
665 | var_repr = self.include_vars and eqrepr or nullrepr |
|
667 | var_repr = self.include_vars and eqrepr or nullrepr | |
666 | try: |
|
668 | try: | |
667 | call = tpl_call % (func,inspect.formatargvalues(args, |
|
669 | call = tpl_call % (func,inspect.formatargvalues(args, | |
668 | varargs, varkw, |
|
670 | varargs, varkw, | |
669 | locals,formatvalue=var_repr)) |
|
671 | locals,formatvalue=var_repr)) | |
670 | except KeyError: |
|
672 | except KeyError: | |
671 | # Very odd crash from inspect.formatargvalues(). The |
|
673 | # Very odd crash from inspect.formatargvalues(). The | |
672 | # scenario under which it appeared was a call to |
|
674 | # scenario under which it appeared was a call to | |
673 | # view(array,scale) in NumTut.view.view(), where scale had |
|
675 | # view(array,scale) in NumTut.view.view(), where scale had | |
674 | # been defined as a scalar (it should be a tuple). Somehow |
|
676 | # been defined as a scalar (it should be a tuple). Somehow | |
675 | # inspect messes up resolving the argument list of view() |
|
677 | # inspect messes up resolving the argument list of view() | |
676 | # and barfs out. At some point I should dig into this one |
|
678 | # and barfs out. At some point I should dig into this one | |
677 | # and file a bug report about it. |
|
679 | # and file a bug report about it. | |
678 | inspect_error() |
|
680 | inspect_error() | |
679 | traceback.print_exc(file=Term.cerr) |
|
681 | traceback.print_exc(file=Term.cerr) | |
680 | info("\nIPython's exception reporting continues...\n") |
|
682 | info("\nIPython's exception reporting continues...\n") | |
681 | call = tpl_call_fail % func |
|
683 | call = tpl_call_fail % func | |
682 |
|
684 | |||
683 | # Initialize a list of names on the current line, which the |
|
685 | # Initialize a list of names on the current line, which the | |
684 | # tokenizer below will populate. |
|
686 | # tokenizer below will populate. | |
685 | names = [] |
|
687 | names = [] | |
686 |
|
688 | |||
687 | def tokeneater(token_type, token, start, end, line): |
|
689 | def tokeneater(token_type, token, start, end, line): | |
688 | """Stateful tokeneater which builds dotted names. |
|
690 | """Stateful tokeneater which builds dotted names. | |
689 |
|
691 | |||
690 | The list of names it appends to (from the enclosing scope) can |
|
692 | The list of names it appends to (from the enclosing scope) can | |
691 | contain repeated composite names. This is unavoidable, since |
|
693 | contain repeated composite names. This is unavoidable, since | |
692 | there is no way to disambguate partial dotted structures until |
|
694 | there is no way to disambguate partial dotted structures until | |
693 | the full list is known. The caller is responsible for pruning |
|
695 | the full list is known. The caller is responsible for pruning | |
694 | the final list of duplicates before using it.""" |
|
696 | the final list of duplicates before using it.""" | |
695 |
|
697 | |||
696 | # build composite names |
|
698 | # build composite names | |
697 | if token == '.': |
|
699 | if token == '.': | |
698 | try: |
|
700 | try: | |
699 | names[-1] += '.' |
|
701 | names[-1] += '.' | |
700 | # store state so the next token is added for x.y.z names |
|
702 | # store state so the next token is added for x.y.z names | |
701 | tokeneater.name_cont = True |
|
703 | tokeneater.name_cont = True | |
702 | return |
|
704 | return | |
703 | except IndexError: |
|
705 | except IndexError: | |
704 | pass |
|
706 | pass | |
705 | if token_type == tokenize.NAME and token not in keyword.kwlist: |
|
707 | if token_type == tokenize.NAME and token not in keyword.kwlist: | |
706 | if tokeneater.name_cont: |
|
708 | if tokeneater.name_cont: | |
707 | # Dotted names |
|
709 | # Dotted names | |
708 | names[-1] += token |
|
710 | names[-1] += token | |
709 | tokeneater.name_cont = False |
|
711 | tokeneater.name_cont = False | |
710 | else: |
|
712 | else: | |
711 | # Regular new names. We append everything, the caller |
|
713 | # Regular new names. We append everything, the caller | |
712 | # will be responsible for pruning the list later. It's |
|
714 | # will be responsible for pruning the list later. It's | |
713 | # very tricky to try to prune as we go, b/c composite |
|
715 | # very tricky to try to prune as we go, b/c composite | |
714 | # names can fool us. The pruning at the end is easy |
|
716 | # names can fool us. The pruning at the end is easy | |
715 | # to do (or the caller can print a list with repeated |
|
717 | # to do (or the caller can print a list with repeated | |
716 | # names if so desired. |
|
718 | # names if so desired. | |
717 | names.append(token) |
|
719 | names.append(token) | |
718 | elif token_type == tokenize.NEWLINE: |
|
720 | elif token_type == tokenize.NEWLINE: | |
719 | raise IndexError |
|
721 | raise IndexError | |
720 | # we need to store a bit of state in the tokenizer to build |
|
722 | # we need to store a bit of state in the tokenizer to build | |
721 | # dotted names |
|
723 | # dotted names | |
722 | tokeneater.name_cont = False |
|
724 | tokeneater.name_cont = False | |
723 |
|
725 | |||
724 | def linereader(file=file, lnum=[lnum], getline=linecache.getline): |
|
726 | def linereader(file=file, lnum=[lnum], getline=linecache.getline): | |
725 | line = getline(file, lnum[0]) |
|
727 | line = getline(file, lnum[0]) | |
726 | lnum[0] += 1 |
|
728 | lnum[0] += 1 | |
727 | return line |
|
729 | return line | |
728 |
|
730 | |||
729 | # Build the list of names on this line of code where the exception |
|
731 | # Build the list of names on this line of code where the exception | |
730 | # occurred. |
|
732 | # occurred. | |
731 | try: |
|
733 | try: | |
732 | # This builds the names list in-place by capturing it from the |
|
734 | # This builds the names list in-place by capturing it from the | |
733 | # enclosing scope. |
|
735 | # enclosing scope. | |
734 | tokenize.tokenize(linereader, tokeneater) |
|
736 | tokenize.tokenize(linereader, tokeneater) | |
735 | except IndexError: |
|
737 | except IndexError: | |
736 | # signals exit of tokenizer |
|
738 | # signals exit of tokenizer | |
737 | pass |
|
739 | pass | |
738 | except tokenize.TokenError,msg: |
|
740 | except tokenize.TokenError,msg: | |
739 | _m = ("An unexpected error occurred while tokenizing input\n" |
|
741 | _m = ("An unexpected error occurred while tokenizing input\n" | |
740 | "The following traceback may be corrupted or invalid\n" |
|
742 | "The following traceback may be corrupted or invalid\n" | |
741 | "The error message is: %s\n" % msg) |
|
743 | "The error message is: %s\n" % msg) | |
742 | error(_m) |
|
744 | error(_m) | |
743 |
|
745 | |||
744 | # prune names list of duplicates, but keep the right order |
|
746 | # prune names list of duplicates, but keep the right order | |
745 | unique_names = uniq_stable(names) |
|
747 | unique_names = uniq_stable(names) | |
746 |
|
748 | |||
747 | # Start loop over vars |
|
749 | # Start loop over vars | |
748 | lvals = [] |
|
750 | lvals = [] | |
749 | if self.include_vars: |
|
751 | if self.include_vars: | |
750 | for name_full in unique_names: |
|
752 | for name_full in unique_names: | |
751 | name_base = name_full.split('.',1)[0] |
|
753 | name_base = name_full.split('.',1)[0] | |
752 | if name_base in frame.f_code.co_varnames: |
|
754 | if name_base in frame.f_code.co_varnames: | |
753 | if locals.has_key(name_base): |
|
755 | if locals.has_key(name_base): | |
754 | try: |
|
756 | try: | |
755 | value = repr(eval(name_full,locals)) |
|
757 | value = repr(eval(name_full,locals)) | |
756 | except: |
|
758 | except: | |
757 | value = undefined |
|
759 | value = undefined | |
758 | else: |
|
760 | else: | |
759 | value = undefined |
|
761 | value = undefined | |
760 | name = tpl_local_var % name_full |
|
762 | name = tpl_local_var % name_full | |
761 | else: |
|
763 | else: | |
762 | if frame.f_globals.has_key(name_base): |
|
764 | if frame.f_globals.has_key(name_base): | |
763 | try: |
|
765 | try: | |
764 | value = repr(eval(name_full,frame.f_globals)) |
|
766 | value = repr(eval(name_full,frame.f_globals)) | |
765 | except: |
|
767 | except: | |
766 | value = undefined |
|
768 | value = undefined | |
767 | else: |
|
769 | else: | |
768 | value = undefined |
|
770 | value = undefined | |
769 | name = tpl_global_var % name_full |
|
771 | name = tpl_global_var % name_full | |
770 | lvals.append(tpl_name_val % (name,value)) |
|
772 | lvals.append(tpl_name_val % (name,value)) | |
771 | if lvals: |
|
773 | if lvals: | |
772 | lvals = '%s%s' % (indent,em_normal.join(lvals)) |
|
774 | lvals = '%s%s' % (indent,em_normal.join(lvals)) | |
773 | else: |
|
775 | else: | |
774 | lvals = '' |
|
776 | lvals = '' | |
775 |
|
777 | |||
776 | level = '%s %s\n' % (link,call) |
|
778 | level = '%s %s\n' % (link,call) | |
777 |
|
779 | |||
778 | if index is None: |
|
780 | if index is None: | |
779 | frames.append(level) |
|
781 | frames.append(level) | |
780 | else: |
|
782 | else: | |
781 | frames.append('%s%s' % (level,''.join( |
|
783 | frames.append('%s%s' % (level,''.join( | |
782 | _formatTracebackLines(lnum,index,lines,Colors,lvals, |
|
784 | _formatTracebackLines(lnum,index,lines,Colors,lvals, | |
783 | col_scheme)))) |
|
785 | col_scheme)))) | |
784 |
|
786 | |||
785 | # Get (safely) a string form of the exception info |
|
787 | # Get (safely) a string form of the exception info | |
786 | try: |
|
788 | try: | |
787 | etype_str,evalue_str = map(str,(etype,evalue)) |
|
789 | etype_str,evalue_str = map(str,(etype,evalue)) | |
788 | except: |
|
790 | except: | |
789 | # User exception is improperly defined. |
|
791 | # User exception is improperly defined. | |
790 | etype,evalue = str,sys.exc_info()[:2] |
|
792 | etype,evalue = str,sys.exc_info()[:2] | |
791 | etype_str,evalue_str = map(str,(etype,evalue)) |
|
793 | etype_str,evalue_str = map(str,(etype,evalue)) | |
792 | # ... and format it |
|
794 | # ... and format it | |
793 | exception = ['%s%s%s: %s' % (Colors.excName, etype_str, |
|
795 | exception = ['%s%s%s: %s' % (Colors.excName, etype_str, | |
794 | ColorsNormal, evalue_str)] |
|
796 | ColorsNormal, evalue_str)] | |
795 | if type(evalue) is types.InstanceType: |
|
797 | if type(evalue) is types.InstanceType: | |
796 | try: |
|
798 | try: | |
797 | names = [w for w in dir(evalue) if isinstance(w, basestring)] |
|
799 | names = [w for w in dir(evalue) if isinstance(w, basestring)] | |
798 | except: |
|
800 | except: | |
799 | # Every now and then, an object with funny inernals blows up |
|
801 | # Every now and then, an object with funny inernals blows up | |
800 | # when dir() is called on it. We do the best we can to report |
|
802 | # when dir() is called on it. We do the best we can to report | |
801 | # the problem and continue |
|
803 | # the problem and continue | |
802 | _m = '%sException reporting error (object with broken dir())%s:' |
|
804 | _m = '%sException reporting error (object with broken dir())%s:' | |
803 | exception.append(_m % (Colors.excName,ColorsNormal)) |
|
805 | exception.append(_m % (Colors.excName,ColorsNormal)) | |
804 | etype_str,evalue_str = map(str,sys.exc_info()[:2]) |
|
806 | etype_str,evalue_str = map(str,sys.exc_info()[:2]) | |
805 | exception.append('%s%s%s: %s' % (Colors.excName,etype_str, |
|
807 | exception.append('%s%s%s: %s' % (Colors.excName,etype_str, | |
806 | ColorsNormal, evalue_str)) |
|
808 | ColorsNormal, evalue_str)) | |
807 | names = [] |
|
809 | names = [] | |
808 | for name in names: |
|
810 | for name in names: | |
809 | value = text_repr(getattr(evalue, name)) |
|
811 | value = text_repr(getattr(evalue, name)) | |
810 | exception.append('\n%s%s = %s' % (indent, name, value)) |
|
812 | exception.append('\n%s%s = %s' % (indent, name, value)) | |
811 |
|
813 | |||
812 | # vds: >> |
|
814 | # vds: >> | |
813 | if records: |
|
815 | if records: | |
814 | filepath, lnum = records[-1][1:3] |
|
816 | filepath, lnum = records[-1][1:3] | |
815 | #print "file:", str(file), "linenb", str(lnum) # dbg |
|
817 | #print "file:", str(file), "linenb", str(lnum) # dbg | |
816 | filepath = os.path.abspath(filepath) |
|
818 | filepath = os.path.abspath(filepath) | |
817 | ipinst = ipapi.get() |
|
819 | ipinst = ipapi.get() | |
818 | if ipinst is not None: |
|
820 | if ipinst is not None: | |
819 | ipinst.hooks.synchronize_with_editor(filepath, lnum, 0) |
|
821 | ipinst.hooks.synchronize_with_editor(filepath, lnum, 0) | |
820 | # vds: << |
|
822 | # vds: << | |
821 |
|
823 | |||
822 | # return all our info assembled as a single string |
|
824 | # return all our info assembled as a single string | |
823 | return '%s\n\n%s\n%s' % (head,'\n'.join(frames),''.join(exception[0]) ) |
|
825 | return '%s\n\n%s\n%s' % (head,'\n'.join(frames),''.join(exception[0]) ) | |
824 |
|
826 | |||
825 | def debugger(self,force=False): |
|
827 | def debugger(self,force=False): | |
826 | """Call up the pdb debugger if desired, always clean up the tb |
|
828 | """Call up the pdb debugger if desired, always clean up the tb | |
827 | reference. |
|
829 | reference. | |
828 |
|
830 | |||
829 | Keywords: |
|
831 | Keywords: | |
830 |
|
832 | |||
831 | - force(False): by default, this routine checks the instance call_pdb |
|
833 | - force(False): by default, this routine checks the instance call_pdb | |
832 | flag and does not actually invoke the debugger if the flag is false. |
|
834 | flag and does not actually invoke the debugger if the flag is false. | |
833 | The 'force' option forces the debugger to activate even if the flag |
|
835 | The 'force' option forces the debugger to activate even if the flag | |
834 | is false. |
|
836 | is false. | |
835 |
|
837 | |||
836 | If the call_pdb flag is set, the pdb interactive debugger is |
|
838 | If the call_pdb flag is set, the pdb interactive debugger is | |
837 | invoked. In all cases, the self.tb reference to the current traceback |
|
839 | invoked. In all cases, the self.tb reference to the current traceback | |
838 | is deleted to prevent lingering references which hamper memory |
|
840 | is deleted to prevent lingering references which hamper memory | |
839 | management. |
|
841 | management. | |
840 |
|
842 | |||
841 | Note that each call to pdb() does an 'import readline', so if your app |
|
843 | Note that each call to pdb() does an 'import readline', so if your app | |
842 | requires a special setup for the readline completers, you'll have to |
|
844 | requires a special setup for the readline completers, you'll have to | |
843 | fix that by hand after invoking the exception handler.""" |
|
845 | fix that by hand after invoking the exception handler.""" | |
844 |
|
846 | |||
845 | if force or self.call_pdb: |
|
847 | if force or self.call_pdb: | |
846 | if self.pdb is None: |
|
848 | if self.pdb is None: | |
847 | self.pdb = debugger.Pdb( |
|
849 | self.pdb = debugger.Pdb( | |
848 | self.color_scheme_table.active_scheme_name) |
|
850 | self.color_scheme_table.active_scheme_name) | |
849 | # the system displayhook may have changed, restore the original |
|
851 | # the system displayhook may have changed, restore the original | |
850 | # for pdb |
|
852 | # for pdb | |
851 |
d |
|
853 | display_trap = DisplayTrap(None, sys.__displayhook__) | |
852 | sys.displayhook = sys.__displayhook__ |
|
854 | with display_trap: | |
853 | self.pdb.reset() |
|
855 | self.pdb.reset() | |
854 | # Find the right frame so we don't pop up inside ipython itself |
|
856 | # Find the right frame so we don't pop up inside ipython itself | |
855 | if hasattr(self,'tb'): |
|
857 | if hasattr(self,'tb'): | |
856 | etb = self.tb |
|
858 | etb = self.tb | |
857 | else: |
|
859 | else: | |
858 | etb = self.tb = sys.last_traceback |
|
860 | etb = self.tb = sys.last_traceback | |
859 | while self.tb.tb_next is not None: |
|
861 | while self.tb.tb_next is not None: | |
860 | self.tb = self.tb.tb_next |
|
862 | self.tb = self.tb.tb_next | |
861 | try: |
|
|||
862 | if etb and etb.tb_next: |
|
863 | if etb and etb.tb_next: | |
863 | etb = etb.tb_next |
|
864 | etb = etb.tb_next | |
864 | self.pdb.botframe = etb.tb_frame |
|
865 | self.pdb.botframe = etb.tb_frame | |
865 | self.pdb.interaction(self.tb.tb_frame, self.tb) |
|
866 | self.pdb.interaction(self.tb.tb_frame, self.tb) | |
866 | finally: |
|
|||
867 | sys.displayhook = dhook |
|
|||
868 |
|
867 | |||
869 | if hasattr(self,'tb'): |
|
868 | if hasattr(self,'tb'): | |
870 | del self.tb |
|
869 | del self.tb | |
871 |
|
870 | |||
872 | def handler(self, info=None): |
|
871 | def handler(self, info=None): | |
873 | (etype, evalue, etb) = info or sys.exc_info() |
|
872 | (etype, evalue, etb) = info or sys.exc_info() | |
874 | self.tb = etb |
|
873 | self.tb = etb | |
875 | Term.cout.flush() |
|
874 | Term.cout.flush() | |
876 | print >> Term.cerr, self.text(etype, evalue, etb) |
|
875 | print >> Term.cerr, self.text(etype, evalue, etb) | |
877 | Term.cerr.flush() |
|
876 | Term.cerr.flush() | |
878 |
|
877 | |||
879 | # Changed so an instance can just be called as VerboseTB_inst() and print |
|
878 | # Changed so an instance can just be called as VerboseTB_inst() and print | |
880 | # out the right info on its own. |
|
879 | # out the right info on its own. | |
881 | def __call__(self, etype=None, evalue=None, etb=None): |
|
880 | def __call__(self, etype=None, evalue=None, etb=None): | |
882 | """This hook can replace sys.excepthook (for Python 2.1 or higher).""" |
|
881 | """This hook can replace sys.excepthook (for Python 2.1 or higher).""" | |
883 | if etb is None: |
|
882 | if etb is None: | |
884 | self.handler() |
|
883 | self.handler() | |
885 | else: |
|
884 | else: | |
886 | self.handler((etype, evalue, etb)) |
|
885 | self.handler((etype, evalue, etb)) | |
887 | try: |
|
886 | try: | |
888 | self.debugger() |
|
887 | self.debugger() | |
889 | except KeyboardInterrupt: |
|
888 | except KeyboardInterrupt: | |
890 | print "\nKeyboardInterrupt" |
|
889 | print "\nKeyboardInterrupt" | |
891 |
|
890 | |||
892 | #---------------------------------------------------------------------------- |
|
891 | #---------------------------------------------------------------------------- | |
893 | class FormattedTB(VerboseTB,ListTB): |
|
892 | class FormattedTB(VerboseTB,ListTB): | |
894 | """Subclass ListTB but allow calling with a traceback. |
|
893 | """Subclass ListTB but allow calling with a traceback. | |
895 |
|
894 | |||
896 | It can thus be used as a sys.excepthook for Python > 2.1. |
|
895 | It can thus be used as a sys.excepthook for Python > 2.1. | |
897 |
|
896 | |||
898 | Also adds 'Context' and 'Verbose' modes, not available in ListTB. |
|
897 | Also adds 'Context' and 'Verbose' modes, not available in ListTB. | |
899 |
|
898 | |||
900 | Allows a tb_offset to be specified. This is useful for situations where |
|
899 | Allows a tb_offset to be specified. This is useful for situations where | |
901 | one needs to remove a number of topmost frames from the traceback (such as |
|
900 | one needs to remove a number of topmost frames from the traceback (such as | |
902 | occurs with python programs that themselves execute other python code, |
|
901 | occurs with python programs that themselves execute other python code, | |
903 | like Python shells). """ |
|
902 | like Python shells). """ | |
904 |
|
903 | |||
905 | def __init__(self, mode = 'Plain', color_scheme='Linux', |
|
904 | def __init__(self, mode = 'Plain', color_scheme='Linux', | |
906 | tb_offset = 0,long_header=0,call_pdb=0,include_vars=0): |
|
905 | tb_offset = 0,long_header=0,call_pdb=0,include_vars=0): | |
907 |
|
906 | |||
908 | # NEVER change the order of this list. Put new modes at the end: |
|
907 | # NEVER change the order of this list. Put new modes at the end: | |
909 | self.valid_modes = ['Plain','Context','Verbose'] |
|
908 | self.valid_modes = ['Plain','Context','Verbose'] | |
910 | self.verbose_modes = self.valid_modes[1:3] |
|
909 | self.verbose_modes = self.valid_modes[1:3] | |
911 |
|
910 | |||
912 | VerboseTB.__init__(self,color_scheme,tb_offset,long_header, |
|
911 | VerboseTB.__init__(self,color_scheme,tb_offset,long_header, | |
913 | call_pdb=call_pdb,include_vars=include_vars) |
|
912 | call_pdb=call_pdb,include_vars=include_vars) | |
914 | self.set_mode(mode) |
|
913 | self.set_mode(mode) | |
915 |
|
914 | |||
916 | def _extract_tb(self,tb): |
|
915 | def _extract_tb(self,tb): | |
917 | if tb: |
|
916 | if tb: | |
918 | return traceback.extract_tb(tb) |
|
917 | return traceback.extract_tb(tb) | |
919 | else: |
|
918 | else: | |
920 | return None |
|
919 | return None | |
921 |
|
920 | |||
922 | def text(self, etype, value, tb,context=5,mode=None): |
|
921 | def text(self, etype, value, tb,context=5,mode=None): | |
923 | """Return formatted traceback. |
|
922 | """Return formatted traceback. | |
924 |
|
923 | |||
925 | If the optional mode parameter is given, it overrides the current |
|
924 | If the optional mode parameter is given, it overrides the current | |
926 | mode.""" |
|
925 | mode.""" | |
927 |
|
926 | |||
928 | if mode is None: |
|
927 | if mode is None: | |
929 | mode = self.mode |
|
928 | mode = self.mode | |
930 | if mode in self.verbose_modes: |
|
929 | if mode in self.verbose_modes: | |
931 | # verbose modes need a full traceback |
|
930 | # verbose modes need a full traceback | |
932 | return VerboseTB.text(self,etype, value, tb,context=5) |
|
931 | return VerboseTB.text(self,etype, value, tb,context=5) | |
933 | else: |
|
932 | else: | |
934 | # We must check the source cache because otherwise we can print |
|
933 | # We must check the source cache because otherwise we can print | |
935 | # out-of-date source code. |
|
934 | # out-of-date source code. | |
936 | linecache.checkcache() |
|
935 | linecache.checkcache() | |
937 | # Now we can extract and format the exception |
|
936 | # Now we can extract and format the exception | |
938 | elist = self._extract_tb(tb) |
|
937 | elist = self._extract_tb(tb) | |
939 | if len(elist) > self.tb_offset: |
|
938 | if len(elist) > self.tb_offset: | |
940 | del elist[:self.tb_offset] |
|
939 | del elist[:self.tb_offset] | |
941 | return ListTB.text(self,etype,value,elist) |
|
940 | return ListTB.text(self,etype,value,elist) | |
942 |
|
941 | |||
943 | def set_mode(self,mode=None): |
|
942 | def set_mode(self,mode=None): | |
944 | """Switch to the desired mode. |
|
943 | """Switch to the desired mode. | |
945 |
|
944 | |||
946 | If mode is not specified, cycles through the available modes.""" |
|
945 | If mode is not specified, cycles through the available modes.""" | |
947 |
|
946 | |||
948 | if not mode: |
|
947 | if not mode: | |
949 | new_idx = ( self.valid_modes.index(self.mode) + 1 ) % \ |
|
948 | new_idx = ( self.valid_modes.index(self.mode) + 1 ) % \ | |
950 | len(self.valid_modes) |
|
949 | len(self.valid_modes) | |
951 | self.mode = self.valid_modes[new_idx] |
|
950 | self.mode = self.valid_modes[new_idx] | |
952 | elif mode not in self.valid_modes: |
|
951 | elif mode not in self.valid_modes: | |
953 | raise ValueError, 'Unrecognized mode in FormattedTB: <'+mode+'>\n'\ |
|
952 | raise ValueError, 'Unrecognized mode in FormattedTB: <'+mode+'>\n'\ | |
954 | 'Valid modes: '+str(self.valid_modes) |
|
953 | 'Valid modes: '+str(self.valid_modes) | |
955 | else: |
|
954 | else: | |
956 | self.mode = mode |
|
955 | self.mode = mode | |
957 | # include variable details only in 'Verbose' mode |
|
956 | # include variable details only in 'Verbose' mode | |
958 | self.include_vars = (self.mode == self.valid_modes[2]) |
|
957 | self.include_vars = (self.mode == self.valid_modes[2]) | |
959 |
|
958 | |||
960 | # some convenient shorcuts |
|
959 | # some convenient shorcuts | |
961 | def plain(self): |
|
960 | def plain(self): | |
962 | self.set_mode(self.valid_modes[0]) |
|
961 | self.set_mode(self.valid_modes[0]) | |
963 |
|
962 | |||
964 | def context(self): |
|
963 | def context(self): | |
965 | self.set_mode(self.valid_modes[1]) |
|
964 | self.set_mode(self.valid_modes[1]) | |
966 |
|
965 | |||
967 | def verbose(self): |
|
966 | def verbose(self): | |
968 | self.set_mode(self.valid_modes[2]) |
|
967 | self.set_mode(self.valid_modes[2]) | |
969 |
|
968 | |||
970 | #---------------------------------------------------------------------------- |
|
969 | #---------------------------------------------------------------------------- | |
971 | class AutoFormattedTB(FormattedTB): |
|
970 | class AutoFormattedTB(FormattedTB): | |
972 | """A traceback printer which can be called on the fly. |
|
971 | """A traceback printer which can be called on the fly. | |
973 |
|
972 | |||
974 | It will find out about exceptions by itself. |
|
973 | It will find out about exceptions by itself. | |
975 |
|
974 | |||
976 | A brief example: |
|
975 | A brief example: | |
977 |
|
976 | |||
978 | AutoTB = AutoFormattedTB(mode = 'Verbose',color_scheme='Linux') |
|
977 | AutoTB = AutoFormattedTB(mode = 'Verbose',color_scheme='Linux') | |
979 | try: |
|
978 | try: | |
980 | ... |
|
979 | ... | |
981 | except: |
|
980 | except: | |
982 | AutoTB() # or AutoTB(out=logfile) where logfile is an open file object |
|
981 | AutoTB() # or AutoTB(out=logfile) where logfile is an open file object | |
983 | """ |
|
982 | """ | |
984 | def __call__(self,etype=None,evalue=None,etb=None, |
|
983 | def __call__(self,etype=None,evalue=None,etb=None, | |
985 | out=None,tb_offset=None): |
|
984 | out=None,tb_offset=None): | |
986 | """Print out a formatted exception traceback. |
|
985 | """Print out a formatted exception traceback. | |
987 |
|
986 | |||
988 | Optional arguments: |
|
987 | Optional arguments: | |
989 | - out: an open file-like object to direct output to. |
|
988 | - out: an open file-like object to direct output to. | |
990 |
|
989 | |||
991 | - tb_offset: the number of frames to skip over in the stack, on a |
|
990 | - tb_offset: the number of frames to skip over in the stack, on a | |
992 | per-call basis (this overrides temporarily the instance's tb_offset |
|
991 | per-call basis (this overrides temporarily the instance's tb_offset | |
993 | given at initialization time. """ |
|
992 | given at initialization time. """ | |
994 |
|
993 | |||
995 | if out is None: |
|
994 | if out is None: | |
996 | out = Term.cerr |
|
995 | out = Term.cerr | |
997 | Term.cout.flush() |
|
996 | Term.cout.flush() | |
998 | if tb_offset is not None: |
|
997 | if tb_offset is not None: | |
999 | tb_offset, self.tb_offset = self.tb_offset, tb_offset |
|
998 | tb_offset, self.tb_offset = self.tb_offset, tb_offset | |
1000 | print >> out, self.text(etype, evalue, etb) |
|
999 | print >> out, self.text(etype, evalue, etb) | |
1001 | self.tb_offset = tb_offset |
|
1000 | self.tb_offset = tb_offset | |
1002 | else: |
|
1001 | else: | |
1003 | print >> out, self.text(etype, evalue, etb) |
|
1002 | print >> out, self.text(etype, evalue, etb) | |
1004 | out.flush() |
|
1003 | out.flush() | |
1005 | try: |
|
1004 | try: | |
1006 | self.debugger() |
|
1005 | self.debugger() | |
1007 | except KeyboardInterrupt: |
|
1006 | except KeyboardInterrupt: | |
1008 | print "\nKeyboardInterrupt" |
|
1007 | print "\nKeyboardInterrupt" | |
1009 |
|
1008 | |||
1010 | def text(self,etype=None,value=None,tb=None,context=5,mode=None): |
|
1009 | def text(self,etype=None,value=None,tb=None,context=5,mode=None): | |
1011 | if etype is None: |
|
1010 | if etype is None: | |
1012 | etype,value,tb = sys.exc_info() |
|
1011 | etype,value,tb = sys.exc_info() | |
1013 | self.tb = tb |
|
1012 | self.tb = tb | |
1014 | return FormattedTB.text(self,etype,value,tb,context=5,mode=mode) |
|
1013 | return FormattedTB.text(self,etype,value,tb,context=5,mode=mode) | |
1015 |
|
1014 | |||
1016 | #--------------------------------------------------------------------------- |
|
1015 | #--------------------------------------------------------------------------- | |
1017 | # A simple class to preserve Nathan's original functionality. |
|
1016 | # A simple class to preserve Nathan's original functionality. | |
1018 | class ColorTB(FormattedTB): |
|
1017 | class ColorTB(FormattedTB): | |
1019 | """Shorthand to initialize a FormattedTB in Linux colors mode.""" |
|
1018 | """Shorthand to initialize a FormattedTB in Linux colors mode.""" | |
1020 | def __init__(self,color_scheme='Linux',call_pdb=0): |
|
1019 | def __init__(self,color_scheme='Linux',call_pdb=0): | |
1021 | FormattedTB.__init__(self,color_scheme=color_scheme, |
|
1020 | FormattedTB.__init__(self,color_scheme=color_scheme, | |
1022 | call_pdb=call_pdb) |
|
1021 | call_pdb=call_pdb) | |
1023 |
|
1022 | |||
1024 | #---------------------------------------------------------------------------- |
|
1023 | #---------------------------------------------------------------------------- | |
1025 | # module testing (minimal) |
|
1024 | # module testing (minimal) | |
1026 | if __name__ == "__main__": |
|
1025 | if __name__ == "__main__": | |
1027 | def spam(c, (d, e)): |
|
1026 | def spam(c, (d, e)): | |
1028 | x = c + d |
|
1027 | x = c + d | |
1029 | y = c * d |
|
1028 | y = c * d | |
1030 | foo(x, y) |
|
1029 | foo(x, y) | |
1031 |
|
1030 | |||
1032 | def foo(a, b, bar=1): |
|
1031 | def foo(a, b, bar=1): | |
1033 | eggs(a, b + bar) |
|
1032 | eggs(a, b + bar) | |
1034 |
|
1033 | |||
1035 | def eggs(f, g, z=globals()): |
|
1034 | def eggs(f, g, z=globals()): | |
1036 | h = f + g |
|
1035 | h = f + g | |
1037 | i = f - g |
|
1036 | i = f - g | |
1038 | return h / i |
|
1037 | return h / i | |
1039 |
|
1038 | |||
1040 | print '' |
|
1039 | print '' | |
1041 | print '*** Before ***' |
|
1040 | print '*** Before ***' | |
1042 | try: |
|
1041 | try: | |
1043 | print spam(1, (2, 3)) |
|
1042 | print spam(1, (2, 3)) | |
1044 | except: |
|
1043 | except: | |
1045 | traceback.print_exc() |
|
1044 | traceback.print_exc() | |
1046 | print '' |
|
1045 | print '' | |
1047 |
|
1046 | |||
1048 | handler = ColorTB() |
|
1047 | handler = ColorTB() | |
1049 | print '*** ColorTB ***' |
|
1048 | print '*** ColorTB ***' | |
1050 | try: |
|
1049 | try: | |
1051 | print spam(1, (2, 3)) |
|
1050 | print spam(1, (2, 3)) | |
1052 | except: |
|
1051 | except: | |
1053 | apply(handler, sys.exc_info() ) |
|
1052 | apply(handler, sys.exc_info() ) | |
1054 | print '' |
|
1053 | print '' | |
1055 |
|
1054 | |||
1056 | handler = VerboseTB() |
|
1055 | handler = VerboseTB() | |
1057 | print '*** VerboseTB ***' |
|
1056 | print '*** VerboseTB ***' | |
1058 | try: |
|
1057 | try: | |
1059 | print spam(1, (2, 3)) |
|
1058 | print spam(1, (2, 3)) | |
1060 | except: |
|
1059 | except: | |
1061 | apply(handler, sys.exc_info() ) |
|
1060 | apply(handler, sys.exc_info() ) | |
1062 | print '' |
|
1061 | print '' | |
1063 |
|
1062 |
General Comments 0
You need to be logged in to leave comments.
Login now