|
@@
-1,2463
+1,2489
|
|
1
|
# -*- coding: utf-8 -*-
|
|
1
|
# -*- coding: utf-8 -*-
|
|
2
|
"""Main IPython class."""
|
|
2
|
"""Main IPython class."""
|
|
3
|
|
|
3
|
|
|
4
|
#-----------------------------------------------------------------------------
|
|
4
|
#-----------------------------------------------------------------------------
|
|
5
|
# Copyright (C) 2001 Janko Hauser <jhauser@zscout.de>
|
|
5
|
# Copyright (C) 2001 Janko Hauser <jhauser@zscout.de>
|
|
6
|
# Copyright (C) 2001-2007 Fernando Perez. <fperez@colorado.edu>
|
|
6
|
# Copyright (C) 2001-2007 Fernando Perez. <fperez@colorado.edu>
|
|
7
|
# Copyright (C) 2008-2010 The IPython Development Team
|
|
7
|
# Copyright (C) 2008-2010 The IPython Development Team
|
|
8
|
#
|
|
8
|
#
|
|
9
|
# Distributed under the terms of the BSD License. The full license is in
|
|
9
|
# Distributed under the terms of the BSD License. The full license is in
|
|
10
|
# the file COPYING, distributed as part of this software.
|
|
10
|
# the file COPYING, distributed as part of this software.
|
|
11
|
#-----------------------------------------------------------------------------
|
|
11
|
#-----------------------------------------------------------------------------
|
|
12
|
|
|
12
|
|
|
13
|
#-----------------------------------------------------------------------------
|
|
13
|
#-----------------------------------------------------------------------------
|
|
14
|
# Imports
|
|
14
|
# Imports
|
|
15
|
#-----------------------------------------------------------------------------
|
|
15
|
#-----------------------------------------------------------------------------
|
|
16
|
|
|
16
|
|
|
17
|
from __future__ import with_statement
|
|
17
|
from __future__ import with_statement
|
|
18
|
from __future__ import absolute_import
|
|
18
|
from __future__ import absolute_import
|
|
19
|
|
|
19
|
|
|
20
|
import __builtin__
|
|
20
|
import __builtin__
|
|
21
|
import __future__
|
|
21
|
import __future__
|
|
22
|
import abc
|
|
22
|
import abc
|
|
23
|
import atexit
|
|
23
|
import atexit
|
|
24
|
import codeop
|
|
24
|
import codeop
|
|
25
|
import exceptions
|
|
25
|
import exceptions
|
|
26
|
import new
|
|
26
|
import new
|
|
27
|
import os
|
|
27
|
import os
|
|
28
|
import re
|
|
28
|
import re
|
|
29
|
import string
|
|
29
|
import string
|
|
30
|
import sys
|
|
30
|
import sys
|
|
31
|
import tempfile
|
|
31
|
import tempfile
|
|
32
|
from contextlib import nested
|
|
32
|
from contextlib import nested
|
|
33
|
|
|
33
|
|
|
34
|
from IPython.config.configurable import Configurable
|
|
34
|
from IPython.config.configurable import Configurable
|
|
35
|
from IPython.core import debugger, oinspect
|
|
35
|
from IPython.core import debugger, oinspect
|
|
36
|
from IPython.core import history as ipcorehist
|
|
36
|
from IPython.core import history as ipcorehist
|
|
37
|
from IPython.core import page
|
|
37
|
from IPython.core import page
|
|
38
|
from IPython.core import prefilter
|
|
38
|
from IPython.core import prefilter
|
|
39
|
from IPython.core import shadowns
|
|
39
|
from IPython.core import shadowns
|
|
40
|
from IPython.core import ultratb
|
|
40
|
from IPython.core import ultratb
|
|
41
|
from IPython.core.alias import AliasManager
|
|
41
|
from IPython.core.alias import AliasManager
|
|
42
|
from IPython.core.builtin_trap import BuiltinTrap
|
|
42
|
from IPython.core.builtin_trap import BuiltinTrap
|
|
43
|
from IPython.core.display_trap import DisplayTrap
|
|
43
|
from IPython.core.display_trap import DisplayTrap
|
|
44
|
from IPython.core.displayhook import DisplayHook
|
|
44
|
from IPython.core.displayhook import DisplayHook
|
|
45
|
from IPython.core.error import TryNext, UsageError
|
|
45
|
from IPython.core.error import TryNext, UsageError
|
|
46
|
from IPython.core.extensions import ExtensionManager
|
|
46
|
from IPython.core.extensions import ExtensionManager
|
|
47
|
from IPython.core.fakemodule import FakeModule, init_fakemod_dict
|
|
47
|
from IPython.core.fakemodule import FakeModule, init_fakemod_dict
|
|
48
|
from IPython.core.inputlist import InputList
|
|
48
|
from IPython.core.inputlist import InputList
|
|
49
|
from IPython.core.inputsplitter import IPythonInputSplitter
|
|
49
|
from IPython.core.inputsplitter import IPythonInputSplitter
|
|
50
|
from IPython.core.logger import Logger
|
|
50
|
from IPython.core.logger import Logger
|
|
51
|
from IPython.core.magic import Magic
|
|
51
|
from IPython.core.magic import Magic
|
|
52
|
from IPython.core.payload import PayloadManager
|
|
52
|
from IPython.core.payload import PayloadManager
|
|
53
|
from IPython.core.plugin import PluginManager
|
|
53
|
from IPython.core.plugin import PluginManager
|
|
54
|
from IPython.core.prefilter import PrefilterManager, ESC_MAGIC
|
|
54
|
from IPython.core.prefilter import PrefilterManager, ESC_MAGIC
|
|
55
|
from IPython.external.Itpl import ItplNS
|
|
55
|
from IPython.external.Itpl import ItplNS
|
|
56
|
from IPython.utils import PyColorize
|
|
56
|
from IPython.utils import PyColorize
|
|
57
|
from IPython.utils import io
|
|
57
|
from IPython.utils import io
|
|
58
|
from IPython.utils import pickleshare
|
|
58
|
from IPython.utils import pickleshare
|
|
59
|
from IPython.utils.doctestreload import doctest_reload
|
|
59
|
from IPython.utils.doctestreload import doctest_reload
|
|
60
|
from IPython.utils.io import ask_yes_no, rprint
|
|
60
|
from IPython.utils.io import ask_yes_no, rprint
|
|
61
|
from IPython.utils.ipstruct import Struct
|
|
61
|
from IPython.utils.ipstruct import Struct
|
|
62
|
from IPython.utils.path import get_home_dir, get_ipython_dir, HomeDirError
|
|
62
|
from IPython.utils.path import get_home_dir, get_ipython_dir, HomeDirError
|
|
63
|
from IPython.utils.process import system, getoutput
|
|
63
|
from IPython.utils.process import system, getoutput
|
|
64
|
from IPython.utils.strdispatch import StrDispatch
|
|
64
|
from IPython.utils.strdispatch import StrDispatch
|
|
65
|
from IPython.utils.syspathcontext import prepended_to_syspath
|
|
65
|
from IPython.utils.syspathcontext import prepended_to_syspath
|
|
66
|
from IPython.utils.text import num_ini_spaces, format_screen
|
|
66
|
from IPython.utils.text import num_ini_spaces, format_screen
|
|
67
|
from IPython.utils.traitlets import (Int, Str, CBool, CaselessStrEnum, Enum,
|
|
67
|
from IPython.utils.traitlets import (Int, Str, CBool, CaselessStrEnum, Enum,
|
|
68
|
List, Unicode, Instance, Type)
|
|
68
|
List, Unicode, Instance, Type)
|
|
69
|
from IPython.utils.warn import warn, error, fatal
|
|
69
|
from IPython.utils.warn import warn, error, fatal
|
|
70
|
import IPython.core.hooks
|
|
70
|
import IPython.core.hooks
|
|
71
|
|
|
71
|
|
|
72
|
#-----------------------------------------------------------------------------
|
|
72
|
#-----------------------------------------------------------------------------
|
|
73
|
# Globals
|
|
73
|
# Globals
|
|
74
|
#-----------------------------------------------------------------------------
|
|
74
|
#-----------------------------------------------------------------------------
|
|
75
|
|
|
75
|
|
|
76
|
# compiled regexps for autoindent management
|
|
76
|
# compiled regexps for autoindent management
|
|
77
|
dedent_re = re.compile(r'^\s+raise|^\s+return|^\s+pass')
|
|
77
|
dedent_re = re.compile(r'^\s+raise|^\s+return|^\s+pass')
|
|
78
|
|
|
78
|
|
|
79
|
#-----------------------------------------------------------------------------
|
|
79
|
#-----------------------------------------------------------------------------
|
|
80
|
# Utilities
|
|
80
|
# Utilities
|
|
81
|
#-----------------------------------------------------------------------------
|
|
81
|
#-----------------------------------------------------------------------------
|
|
82
|
|
|
82
|
|
|
83
|
# store the builtin raw_input globally, and use this always, in case user code
|
|
83
|
# store the builtin raw_input globally, and use this always, in case user code
|
|
84
|
# overwrites it (like wx.py.PyShell does)
|
|
84
|
# overwrites it (like wx.py.PyShell does)
|
|
85
|
raw_input_original = raw_input
|
|
85
|
raw_input_original = raw_input
|
|
86
|
|
|
86
|
|
|
87
|
def softspace(file, newvalue):
|
|
87
|
def softspace(file, newvalue):
|
|
88
|
"""Copied from code.py, to remove the dependency"""
|
|
88
|
"""Copied from code.py, to remove the dependency"""
|
|
89
|
|
|
89
|
|
|
90
|
oldvalue = 0
|
|
90
|
oldvalue = 0
|
|
91
|
try:
|
|
91
|
try:
|
|
92
|
oldvalue = file.softspace
|
|
92
|
oldvalue = file.softspace
|
|
93
|
except AttributeError:
|
|
93
|
except AttributeError:
|
|
94
|
pass
|
|
94
|
pass
|
|
95
|
try:
|
|
95
|
try:
|
|
96
|
file.softspace = newvalue
|
|
96
|
file.softspace = newvalue
|
|
97
|
except (AttributeError, TypeError):
|
|
97
|
except (AttributeError, TypeError):
|
|
98
|
# "attribute-less object" or "read-only attributes"
|
|
98
|
# "attribute-less object" or "read-only attributes"
|
|
99
|
pass
|
|
99
|
pass
|
|
100
|
return oldvalue
|
|
100
|
return oldvalue
|
|
101
|
|
|
101
|
|
|
102
|
|
|
102
|
|
|
103
|
def no_op(*a, **kw): pass
|
|
103
|
def no_op(*a, **kw): pass
|
|
104
|
|
|
104
|
|
|
105
|
class SpaceInInput(exceptions.Exception): pass
|
|
105
|
class SpaceInInput(exceptions.Exception): pass
|
|
106
|
|
|
106
|
|
|
107
|
class Bunch: pass
|
|
107
|
class Bunch: pass
|
|
108
|
|
|
108
|
|
|
109
|
|
|
109
|
|
|
110
|
def get_default_colors():
|
|
110
|
def get_default_colors():
|
|
111
|
if sys.platform=='darwin':
|
|
111
|
if sys.platform=='darwin':
|
|
112
|
return "LightBG"
|
|
112
|
return "LightBG"
|
|
113
|
elif os.name=='nt':
|
|
113
|
elif os.name=='nt':
|
|
114
|
return 'Linux'
|
|
114
|
return 'Linux'
|
|
115
|
else:
|
|
115
|
else:
|
|
116
|
return 'Linux'
|
|
116
|
return 'Linux'
|
|
117
|
|
|
117
|
|
|
118
|
|
|
118
|
|
|
119
|
class SeparateStr(Str):
|
|
119
|
class SeparateStr(Str):
|
|
120
|
"""A Str subclass to validate separate_in, separate_out, etc.
|
|
120
|
"""A Str subclass to validate separate_in, separate_out, etc.
|
|
121
|
|
|
121
|
|
|
122
|
This is a Str based trait that converts '0'->'' and '\\n'->'\n'.
|
|
122
|
This is a Str based trait that converts '0'->'' and '\\n'->'\n'.
|
|
123
|
"""
|
|
123
|
"""
|
|
124
|
|
|
124
|
|
|
125
|
def validate(self, obj, value):
|
|
125
|
def validate(self, obj, value):
|
|
126
|
if value == '0': value = ''
|
|
126
|
if value == '0': value = ''
|
|
127
|
value = value.replace('\\n','\n')
|
|
127
|
value = value.replace('\\n','\n')
|
|
128
|
return super(SeparateStr, self).validate(obj, value)
|
|
128
|
return super(SeparateStr, self).validate(obj, value)
|
|
129
|
|
|
129
|
|
|
130
|
class MultipleInstanceError(Exception):
|
|
130
|
class MultipleInstanceError(Exception):
|
|
131
|
pass
|
|
131
|
pass
|
|
132
|
|
|
132
|
|
|
133
|
|
|
133
|
|
|
134
|
#-----------------------------------------------------------------------------
|
|
134
|
#-----------------------------------------------------------------------------
|
|
135
|
# Main IPython class
|
|
135
|
# Main IPython class
|
|
136
|
#-----------------------------------------------------------------------------
|
|
136
|
#-----------------------------------------------------------------------------
|
|
137
|
|
|
137
|
|
|
138
|
|
|
138
|
|
|
139
|
class InteractiveShell(Configurable, Magic):
|
|
139
|
class InteractiveShell(Configurable, Magic):
|
|
140
|
"""An enhanced, interactive shell for Python."""
|
|
140
|
"""An enhanced, interactive shell for Python."""
|
|
141
|
|
|
141
|
|
|
142
|
_instance = None
|
|
142
|
_instance = None
|
|
143
|
autocall = Enum((0,1,2), default_value=1, config=True)
|
|
143
|
autocall = Enum((0,1,2), default_value=1, config=True)
|
|
144
|
# TODO: remove all autoindent logic and put into frontends.
|
|
144
|
# TODO: remove all autoindent logic and put into frontends.
|
|
145
|
# We can't do this yet because even runlines uses the autoindent.
|
|
145
|
# We can't do this yet because even runlines uses the autoindent.
|
|
146
|
autoindent = CBool(True, config=True)
|
|
146
|
autoindent = CBool(True, config=True)
|
|
147
|
automagic = CBool(True, config=True)
|
|
147
|
automagic = CBool(True, config=True)
|
|
148
|
cache_size = Int(1000, config=True)
|
|
148
|
cache_size = Int(1000, config=True)
|
|
149
|
color_info = CBool(True, config=True)
|
|
149
|
color_info = CBool(True, config=True)
|
|
150
|
colors = CaselessStrEnum(('NoColor','LightBG','Linux'),
|
|
150
|
colors = CaselessStrEnum(('NoColor','LightBG','Linux'),
|
|
151
|
default_value=get_default_colors(), config=True)
|
|
151
|
default_value=get_default_colors(), config=True)
|
|
152
|
debug = CBool(False, config=True)
|
|
152
|
debug = CBool(False, config=True)
|
|
153
|
deep_reload = CBool(False, config=True)
|
|
153
|
deep_reload = CBool(False, config=True)
|
|
154
|
displayhook_class = Type(DisplayHook)
|
|
154
|
displayhook_class = Type(DisplayHook)
|
|
155
|
exit_now = CBool(False)
|
|
155
|
exit_now = CBool(False)
|
|
156
|
filename = Str("<ipython console>")
|
|
156
|
filename = Str("<ipython console>")
|
|
157
|
ipython_dir= Unicode('', config=True) # Set to get_ipython_dir() in __init__
|
|
157
|
ipython_dir= Unicode('', config=True) # Set to get_ipython_dir() in __init__
|
|
158
|
input_splitter = Instance('IPython.core.inputsplitter.IPythonInputSplitter')
|
|
158
|
input_splitter = Instance('IPython.core.inputsplitter.IPythonInputSplitter')
|
|
159
|
logstart = CBool(False, config=True)
|
|
159
|
logstart = CBool(False, config=True)
|
|
160
|
logfile = Str('', config=True)
|
|
160
|
logfile = Str('', config=True)
|
|
161
|
logappend = Str('', config=True)
|
|
161
|
logappend = Str('', config=True)
|
|
162
|
object_info_string_level = Enum((0,1,2), default_value=0,
|
|
162
|
object_info_string_level = Enum((0,1,2), default_value=0,
|
|
163
|
config=True)
|
|
163
|
config=True)
|
|
164
|
pdb = CBool(False, config=True)
|
|
164
|
pdb = CBool(False, config=True)
|
|
|
|
|
165
|
|
|
165
|
pprint = CBool(True, config=True)
|
|
166
|
pprint = CBool(True, config=True)
|
|
166
|
profile = Str('', config=True)
|
|
167
|
profile = Str('', config=True)
|
|
167
|
prompt_in1 = Str('In [\\#]: ', config=True)
|
|
168
|
prompt_in1 = Str('In [\\#]: ', config=True)
|
|
168
|
prompt_in2 = Str(' .\\D.: ', config=True)
|
|
169
|
prompt_in2 = Str(' .\\D.: ', config=True)
|
|
169
|
prompt_out = Str('Out[\\#]: ', config=True)
|
|
170
|
prompt_out = Str('Out[\\#]: ', config=True)
|
|
170
|
prompts_pad_left = CBool(True, config=True)
|
|
171
|
prompts_pad_left = CBool(True, config=True)
|
|
171
|
quiet = CBool(False, config=True)
|
|
172
|
quiet = CBool(False, config=True)
|
|
172
|
|
|
173
|
|
|
173
|
# The readline stuff will eventually be moved to the terminal subclass
|
|
174
|
# The readline stuff will eventually be moved to the terminal subclass
|
|
174
|
# but for now, we can't do that as readline is welded in everywhere.
|
|
175
|
# but for now, we can't do that as readline is welded in everywhere.
|
|
175
|
readline_use = CBool(True, config=True)
|
|
176
|
readline_use = CBool(True, config=True)
|
|
176
|
readline_merge_completions = CBool(True, config=True)
|
|
177
|
readline_merge_completions = CBool(True, config=True)
|
|
177
|
readline_omit__names = Enum((0,1,2), default_value=0, config=True)
|
|
178
|
readline_omit__names = Enum((0,1,2), default_value=0, config=True)
|
|
178
|
readline_remove_delims = Str('-/~', config=True)
|
|
179
|
readline_remove_delims = Str('-/~', config=True)
|
|
179
|
readline_parse_and_bind = List([
|
|
180
|
readline_parse_and_bind = List([
|
|
180
|
'tab: complete',
|
|
181
|
'tab: complete',
|
|
181
|
'"\C-l": clear-screen',
|
|
182
|
'"\C-l": clear-screen',
|
|
182
|
'set show-all-if-ambiguous on',
|
|
183
|
'set show-all-if-ambiguous on',
|
|
183
|
'"\C-o": tab-insert',
|
|
184
|
'"\C-o": tab-insert',
|
|
184
|
'"\M-i": " "',
|
|
185
|
'"\M-i": " "',
|
|
185
|
'"\M-o": "\d\d\d\d"',
|
|
186
|
'"\M-o": "\d\d\d\d"',
|
|
186
|
'"\M-I": "\d\d\d\d"',
|
|
187
|
'"\M-I": "\d\d\d\d"',
|
|
187
|
'"\C-r": reverse-search-history',
|
|
188
|
'"\C-r": reverse-search-history',
|
|
188
|
'"\C-s": forward-search-history',
|
|
189
|
'"\C-s": forward-search-history',
|
|
189
|
'"\C-p": history-search-backward',
|
|
190
|
'"\C-p": history-search-backward',
|
|
190
|
'"\C-n": history-search-forward',
|
|
191
|
'"\C-n": history-search-forward',
|
|
191
|
'"\e[A": history-search-backward',
|
|
192
|
'"\e[A": history-search-backward',
|
|
192
|
'"\e[B": history-search-forward',
|
|
193
|
'"\e[B": history-search-forward',
|
|
193
|
'"\C-k": kill-line',
|
|
194
|
'"\C-k": kill-line',
|
|
194
|
'"\C-u": unix-line-discard',
|
|
195
|
'"\C-u": unix-line-discard',
|
|
195
|
], allow_none=False, config=True)
|
|
196
|
], allow_none=False, config=True)
|
|
196
|
|
|
197
|
|
|
197
|
# TODO: this part of prompt management should be moved to the frontends.
|
|
198
|
# TODO: this part of prompt management should be moved to the frontends.
|
|
198
|
# Use custom TraitTypes that convert '0'->'' and '\\n'->'\n'
|
|
199
|
# Use custom TraitTypes that convert '0'->'' and '\\n'->'\n'
|
|
199
|
separate_in = SeparateStr('\n', config=True)
|
|
200
|
separate_in = SeparateStr('\n', config=True)
|
|
200
|
separate_out = SeparateStr('', config=True)
|
|
201
|
separate_out = SeparateStr('', config=True)
|
|
201
|
separate_out2 = SeparateStr('', config=True)
|
|
202
|
separate_out2 = SeparateStr('', config=True)
|
|
202
|
wildcards_case_sensitive = CBool(True, config=True)
|
|
203
|
wildcards_case_sensitive = CBool(True, config=True)
|
|
203
|
xmode = CaselessStrEnum(('Context','Plain', 'Verbose'),
|
|
204
|
xmode = CaselessStrEnum(('Context','Plain', 'Verbose'),
|
|
204
|
default_value='Context', config=True)
|
|
205
|
default_value='Context', config=True)
|
|
205
|
|
|
206
|
|
|
206
|
# Subcomponents of InteractiveShell
|
|
207
|
# Subcomponents of InteractiveShell
|
|
207
|
alias_manager = Instance('IPython.core.alias.AliasManager')
|
|
208
|
alias_manager = Instance('IPython.core.alias.AliasManager')
|
|
208
|
prefilter_manager = Instance('IPython.core.prefilter.PrefilterManager')
|
|
209
|
prefilter_manager = Instance('IPython.core.prefilter.PrefilterManager')
|
|
209
|
builtin_trap = Instance('IPython.core.builtin_trap.BuiltinTrap')
|
|
210
|
builtin_trap = Instance('IPython.core.builtin_trap.BuiltinTrap')
|
|
210
|
display_trap = Instance('IPython.core.display_trap.DisplayTrap')
|
|
211
|
display_trap = Instance('IPython.core.display_trap.DisplayTrap')
|
|
211
|
extension_manager = Instance('IPython.core.extensions.ExtensionManager')
|
|
212
|
extension_manager = Instance('IPython.core.extensions.ExtensionManager')
|
|
212
|
plugin_manager = Instance('IPython.core.plugin.PluginManager')
|
|
213
|
plugin_manager = Instance('IPython.core.plugin.PluginManager')
|
|
213
|
payload_manager = Instance('IPython.core.payload.PayloadManager')
|
|
214
|
payload_manager = Instance('IPython.core.payload.PayloadManager')
|
|
214
|
|
|
215
|
|
|
|
|
|
216
|
# Private interface
|
|
|
|
|
217
|
_post_execute = set()
|
|
|
|
|
218
|
|
|
215
|
def __init__(self, config=None, ipython_dir=None,
|
|
219
|
def __init__(self, config=None, ipython_dir=None,
|
|
216
|
user_ns=None, user_global_ns=None,
|
|
220
|
user_ns=None, user_global_ns=None,
|
|
217
|
custom_exceptions=((), None)):
|
|
221
|
custom_exceptions=((), None)):
|
|
218
|
|
|
222
|
|
|
219
|
# This is where traits with a config_key argument are updated
|
|
223
|
# This is where traits with a config_key argument are updated
|
|
220
|
# from the values on config.
|
|
224
|
# from the values on config.
|
|
221
|
super(InteractiveShell, self).__init__(config=config)
|
|
225
|
super(InteractiveShell, self).__init__(config=config)
|
|
222
|
|
|
226
|
|
|
223
|
# These are relatively independent and stateless
|
|
227
|
# These are relatively independent and stateless
|
|
224
|
self.init_ipython_dir(ipython_dir)
|
|
228
|
self.init_ipython_dir(ipython_dir)
|
|
225
|
self.init_instance_attrs()
|
|
229
|
self.init_instance_attrs()
|
|
226
|
|
|
230
|
|
|
227
|
# Create namespaces (user_ns, user_global_ns, etc.)
|
|
231
|
# Create namespaces (user_ns, user_global_ns, etc.)
|
|
228
|
self.init_create_namespaces(user_ns, user_global_ns)
|
|
232
|
self.init_create_namespaces(user_ns, user_global_ns)
|
|
229
|
# This has to be done after init_create_namespaces because it uses
|
|
233
|
# This has to be done after init_create_namespaces because it uses
|
|
230
|
# something in self.user_ns, but before init_sys_modules, which
|
|
234
|
# something in self.user_ns, but before init_sys_modules, which
|
|
231
|
# is the first thing to modify sys.
|
|
235
|
# is the first thing to modify sys.
|
|
232
|
# TODO: When we override sys.stdout and sys.stderr before this class
|
|
236
|
# TODO: When we override sys.stdout and sys.stderr before this class
|
|
233
|
# is created, we are saving the overridden ones here. Not sure if this
|
|
237
|
# is created, we are saving the overridden ones here. Not sure if this
|
|
234
|
# is what we want to do.
|
|
238
|
# is what we want to do.
|
|
235
|
self.save_sys_module_state()
|
|
239
|
self.save_sys_module_state()
|
|
236
|
self.init_sys_modules()
|
|
240
|
self.init_sys_modules()
|
|
237
|
|
|
241
|
|
|
238
|
self.init_history()
|
|
242
|
self.init_history()
|
|
239
|
self.init_encoding()
|
|
243
|
self.init_encoding()
|
|
240
|
self.init_prefilter()
|
|
244
|
self.init_prefilter()
|
|
241
|
|
|
245
|
|
|
242
|
Magic.__init__(self, self)
|
|
246
|
Magic.__init__(self, self)
|
|
243
|
|
|
247
|
|
|
244
|
self.init_syntax_highlighting()
|
|
248
|
self.init_syntax_highlighting()
|
|
245
|
self.init_hooks()
|
|
249
|
self.init_hooks()
|
|
246
|
self.init_pushd_popd_magic()
|
|
250
|
self.init_pushd_popd_magic()
|
|
247
|
# self.init_traceback_handlers use to be here, but we moved it below
|
|
251
|
# self.init_traceback_handlers use to be here, but we moved it below
|
|
248
|
# because it and init_io have to come after init_readline.
|
|
252
|
# because it and init_io have to come after init_readline.
|
|
249
|
self.init_user_ns()
|
|
253
|
self.init_user_ns()
|
|
250
|
self.init_logger()
|
|
254
|
self.init_logger()
|
|
251
|
self.init_alias()
|
|
255
|
self.init_alias()
|
|
252
|
self.init_builtins()
|
|
256
|
self.init_builtins()
|
|
253
|
|
|
257
|
|
|
254
|
# pre_config_initialization
|
|
258
|
# pre_config_initialization
|
|
255
|
self.init_shadow_hist()
|
|
259
|
self.init_shadow_hist()
|
|
256
|
|
|
260
|
|
|
257
|
# The next section should contain everything that was in ipmaker.
|
|
261
|
# The next section should contain everything that was in ipmaker.
|
|
258
|
self.init_logstart()
|
|
262
|
self.init_logstart()
|
|
259
|
|
|
263
|
|
|
260
|
# The following was in post_config_initialization
|
|
264
|
# The following was in post_config_initialization
|
|
261
|
self.init_inspector()
|
|
265
|
self.init_inspector()
|
|
262
|
# init_readline() must come before init_io(), because init_io uses
|
|
266
|
# init_readline() must come before init_io(), because init_io uses
|
|
263
|
# readline related things.
|
|
267
|
# readline related things.
|
|
264
|
self.init_readline()
|
|
268
|
self.init_readline()
|
|
265
|
# init_completer must come after init_readline, because it needs to
|
|
269
|
# init_completer must come after init_readline, because it needs to
|
|
266
|
# know whether readline is present or not system-wide to configure the
|
|
270
|
# know whether readline is present or not system-wide to configure the
|
|
267
|
# completers, since the completion machinery can now operate
|
|
271
|
# completers, since the completion machinery can now operate
|
|
268
|
# independently of readline (e.g. over the network)
|
|
272
|
# independently of readline (e.g. over the network)
|
|
269
|
self.init_completer()
|
|
273
|
self.init_completer()
|
|
270
|
# TODO: init_io() needs to happen before init_traceback handlers
|
|
274
|
# TODO: init_io() needs to happen before init_traceback handlers
|
|
271
|
# because the traceback handlers hardcode the stdout/stderr streams.
|
|
275
|
# because the traceback handlers hardcode the stdout/stderr streams.
|
|
272
|
# This logic in in debugger.Pdb and should eventually be changed.
|
|
276
|
# This logic in in debugger.Pdb and should eventually be changed.
|
|
273
|
self.init_io()
|
|
277
|
self.init_io()
|
|
274
|
self.init_traceback_handlers(custom_exceptions)
|
|
278
|
self.init_traceback_handlers(custom_exceptions)
|
|
275
|
self.init_prompts()
|
|
279
|
self.init_prompts()
|
|
276
|
self.init_displayhook()
|
|
280
|
self.init_displayhook()
|
|
277
|
self.init_reload_doctest()
|
|
281
|
self.init_reload_doctest()
|
|
278
|
self.init_magics()
|
|
282
|
self.init_magics()
|
|
279
|
self.init_pdb()
|
|
283
|
self.init_pdb()
|
|
280
|
self.init_extension_manager()
|
|
284
|
self.init_extension_manager()
|
|
281
|
self.init_plugin_manager()
|
|
285
|
self.init_plugin_manager()
|
|
282
|
self.init_payload()
|
|
286
|
self.init_payload()
|
|
283
|
self.hooks.late_startup_hook()
|
|
287
|
self.hooks.late_startup_hook()
|
|
284
|
atexit.register(self.atexit_operations)
|
|
288
|
atexit.register(self.atexit_operations)
|
|
285
|
|
|
289
|
|
|
286
|
@classmethod
|
|
290
|
@classmethod
|
|
287
|
def instance(cls, *args, **kwargs):
|
|
291
|
def instance(cls, *args, **kwargs):
|
|
288
|
"""Returns a global InteractiveShell instance."""
|
|
292
|
"""Returns a global InteractiveShell instance."""
|
|
289
|
if cls._instance is None:
|
|
293
|
if cls._instance is None:
|
|
290
|
inst = cls(*args, **kwargs)
|
|
294
|
inst = cls(*args, **kwargs)
|
|
291
|
# Now make sure that the instance will also be returned by
|
|
295
|
# Now make sure that the instance will also be returned by
|
|
292
|
# the subclasses instance attribute.
|
|
296
|
# the subclasses instance attribute.
|
|
293
|
for subclass in cls.mro():
|
|
297
|
for subclass in cls.mro():
|
|
294
|
if issubclass(cls, subclass) and \
|
|
298
|
if issubclass(cls, subclass) and \
|
|
295
|
issubclass(subclass, InteractiveShell):
|
|
299
|
issubclass(subclass, InteractiveShell):
|
|
296
|
subclass._instance = inst
|
|
300
|
subclass._instance = inst
|
|
297
|
else:
|
|
301
|
else:
|
|
298
|
break
|
|
302
|
break
|
|
299
|
if isinstance(cls._instance, cls):
|
|
303
|
if isinstance(cls._instance, cls):
|
|
300
|
return cls._instance
|
|
304
|
return cls._instance
|
|
301
|
else:
|
|
305
|
else:
|
|
302
|
raise MultipleInstanceError(
|
|
306
|
raise MultipleInstanceError(
|
|
303
|
'Multiple incompatible subclass instances of '
|
|
307
|
'Multiple incompatible subclass instances of '
|
|
304
|
'InteractiveShell are being created.'
|
|
308
|
'InteractiveShell are being created.'
|
|
305
|
)
|
|
309
|
)
|
|
306
|
|
|
310
|
|
|
307
|
@classmethod
|
|
311
|
@classmethod
|
|
308
|
def initialized(cls):
|
|
312
|
def initialized(cls):
|
|
309
|
return hasattr(cls, "_instance")
|
|
313
|
return hasattr(cls, "_instance")
|
|
310
|
|
|
314
|
|
|
311
|
def get_ipython(self):
|
|
315
|
def get_ipython(self):
|
|
312
|
"""Return the currently running IPython instance."""
|
|
316
|
"""Return the currently running IPython instance."""
|
|
313
|
return self
|
|
317
|
return self
|
|
314
|
|
|
318
|
|
|
315
|
#-------------------------------------------------------------------------
|
|
319
|
#-------------------------------------------------------------------------
|
|
316
|
# Trait changed handlers
|
|
320
|
# Trait changed handlers
|
|
317
|
#-------------------------------------------------------------------------
|
|
321
|
#-------------------------------------------------------------------------
|
|
318
|
|
|
322
|
|
|
319
|
def _ipython_dir_changed(self, name, new):
|
|
323
|
def _ipython_dir_changed(self, name, new):
|
|
320
|
if not os.path.isdir(new):
|
|
324
|
if not os.path.isdir(new):
|
|
321
|
os.makedirs(new, mode = 0777)
|
|
325
|
os.makedirs(new, mode = 0777)
|
|
322
|
|
|
326
|
|
|
323
|
def set_autoindent(self,value=None):
|
|
327
|
def set_autoindent(self,value=None):
|
|
324
|
"""Set the autoindent flag, checking for readline support.
|
|
328
|
"""Set the autoindent flag, checking for readline support.
|
|
325
|
|
|
329
|
|
|
326
|
If called with no arguments, it acts as a toggle."""
|
|
330
|
If called with no arguments, it acts as a toggle."""
|
|
327
|
|
|
331
|
|
|
328
|
if not self.has_readline:
|
|
332
|
if not self.has_readline:
|
|
329
|
if os.name == 'posix':
|
|
333
|
if os.name == 'posix':
|
|
330
|
warn("The auto-indent feature requires the readline library")
|
|
334
|
warn("The auto-indent feature requires the readline library")
|
|
331
|
self.autoindent = 0
|
|
335
|
self.autoindent = 0
|
|
332
|
return
|
|
336
|
return
|
|
333
|
if value is None:
|
|
337
|
if value is None:
|
|
334
|
self.autoindent = not self.autoindent
|
|
338
|
self.autoindent = not self.autoindent
|
|
335
|
else:
|
|
339
|
else:
|
|
336
|
self.autoindent = value
|
|
340
|
self.autoindent = value
|
|
337
|
|
|
341
|
|
|
338
|
#-------------------------------------------------------------------------
|
|
342
|
#-------------------------------------------------------------------------
|
|
339
|
# init_* methods called by __init__
|
|
343
|
# init_* methods called by __init__
|
|
340
|
#-------------------------------------------------------------------------
|
|
344
|
#-------------------------------------------------------------------------
|
|
341
|
|
|
345
|
|
|
342
|
def init_ipython_dir(self, ipython_dir):
|
|
346
|
def init_ipython_dir(self, ipython_dir):
|
|
343
|
if ipython_dir is not None:
|
|
347
|
if ipython_dir is not None:
|
|
344
|
self.ipython_dir = ipython_dir
|
|
348
|
self.ipython_dir = ipython_dir
|
|
345
|
self.config.Global.ipython_dir = self.ipython_dir
|
|
349
|
self.config.Global.ipython_dir = self.ipython_dir
|
|
346
|
return
|
|
350
|
return
|
|
347
|
|
|
351
|
|
|
348
|
if hasattr(self.config.Global, 'ipython_dir'):
|
|
352
|
if hasattr(self.config.Global, 'ipython_dir'):
|
|
349
|
self.ipython_dir = self.config.Global.ipython_dir
|
|
353
|
self.ipython_dir = self.config.Global.ipython_dir
|
|
350
|
else:
|
|
354
|
else:
|
|
351
|
self.ipython_dir = get_ipython_dir()
|
|
355
|
self.ipython_dir = get_ipython_dir()
|
|
352
|
|
|
356
|
|
|
353
|
# All children can just read this
|
|
357
|
# All children can just read this
|
|
354
|
self.config.Global.ipython_dir = self.ipython_dir
|
|
358
|
self.config.Global.ipython_dir = self.ipython_dir
|
|
355
|
|
|
359
|
|
|
356
|
def init_instance_attrs(self):
|
|
360
|
def init_instance_attrs(self):
|
|
357
|
self.more = False
|
|
361
|
self.more = False
|
|
358
|
|
|
362
|
|
|
359
|
# command compiler
|
|
363
|
# command compiler
|
|
360
|
self.compile = codeop.CommandCompiler()
|
|
364
|
self.compile = codeop.CommandCompiler()
|
|
361
|
|
|
365
|
|
|
362
|
# User input buffer
|
|
366
|
# User input buffer
|
|
363
|
self.buffer = []
|
|
367
|
self.buffer = []
|
|
364
|
|
|
368
|
|
|
365
|
# Make an empty namespace, which extension writers can rely on both
|
|
369
|
# Make an empty namespace, which extension writers can rely on both
|
|
366
|
# existing and NEVER being used by ipython itself. This gives them a
|
|
370
|
# existing and NEVER being used by ipython itself. This gives them a
|
|
367
|
# convenient location for storing additional information and state
|
|
371
|
# convenient location for storing additional information and state
|
|
368
|
# their extensions may require, without fear of collisions with other
|
|
372
|
# their extensions may require, without fear of collisions with other
|
|
369
|
# ipython names that may develop later.
|
|
373
|
# ipython names that may develop later.
|
|
370
|
self.meta = Struct()
|
|
374
|
self.meta = Struct()
|
|
371
|
|
|
375
|
|
|
372
|
# Object variable to store code object waiting execution. This is
|
|
376
|
# Object variable to store code object waiting execution. This is
|
|
373
|
# used mainly by the multithreaded shells, but it can come in handy in
|
|
377
|
# used mainly by the multithreaded shells, but it can come in handy in
|
|
374
|
# other situations. No need to use a Queue here, since it's a single
|
|
378
|
# other situations. No need to use a Queue here, since it's a single
|
|
375
|
# item which gets cleared once run.
|
|
379
|
# item which gets cleared once run.
|
|
376
|
self.code_to_run = None
|
|
380
|
self.code_to_run = None
|
|
377
|
|
|
381
|
|
|
378
|
# Temporary files used for various purposes. Deleted at exit.
|
|
382
|
# Temporary files used for various purposes. Deleted at exit.
|
|
379
|
self.tempfiles = []
|
|
383
|
self.tempfiles = []
|
|
380
|
|
|
384
|
|
|
381
|
# Keep track of readline usage (later set by init_readline)
|
|
385
|
# Keep track of readline usage (later set by init_readline)
|
|
382
|
self.has_readline = False
|
|
386
|
self.has_readline = False
|
|
383
|
|
|
387
|
|
|
384
|
# keep track of where we started running (mainly for crash post-mortem)
|
|
388
|
# keep track of where we started running (mainly for crash post-mortem)
|
|
385
|
# This is not being used anywhere currently.
|
|
389
|
# This is not being used anywhere currently.
|
|
386
|
self.starting_dir = os.getcwd()
|
|
390
|
self.starting_dir = os.getcwd()
|
|
387
|
|
|
391
|
|
|
388
|
# Indentation management
|
|
392
|
# Indentation management
|
|
389
|
self.indent_current_nsp = 0
|
|
393
|
self.indent_current_nsp = 0
|
|
390
|
|
|
394
|
|
|
391
|
# Input splitter, to split entire cells of input into either individual
|
|
395
|
# Input splitter, to split entire cells of input into either individual
|
|
392
|
# interactive statements or whole blocks.
|
|
396
|
# interactive statements or whole blocks.
|
|
393
|
self.input_splitter = IPythonInputSplitter()
|
|
397
|
self.input_splitter = IPythonInputSplitter()
|
|
394
|
|
|
398
|
|
|
395
|
def init_encoding(self):
|
|
399
|
def init_encoding(self):
|
|
396
|
# Get system encoding at startup time. Certain terminals (like Emacs
|
|
400
|
# Get system encoding at startup time. Certain terminals (like Emacs
|
|
397
|
# under Win32 have it set to None, and we need to have a known valid
|
|
401
|
# under Win32 have it set to None, and we need to have a known valid
|
|
398
|
# encoding to use in the raw_input() method
|
|
402
|
# encoding to use in the raw_input() method
|
|
399
|
try:
|
|
403
|
try:
|
|
400
|
self.stdin_encoding = sys.stdin.encoding or 'ascii'
|
|
404
|
self.stdin_encoding = sys.stdin.encoding or 'ascii'
|
|
401
|
except AttributeError:
|
|
405
|
except AttributeError:
|
|
402
|
self.stdin_encoding = 'ascii'
|
|
406
|
self.stdin_encoding = 'ascii'
|
|
403
|
|
|
407
|
|
|
404
|
def init_syntax_highlighting(self):
|
|
408
|
def init_syntax_highlighting(self):
|
|
405
|
# Python source parser/formatter for syntax highlighting
|
|
409
|
# Python source parser/formatter for syntax highlighting
|
|
406
|
pyformat = PyColorize.Parser().format
|
|
410
|
pyformat = PyColorize.Parser().format
|
|
407
|
self.pycolorize = lambda src: pyformat(src,'str',self.colors)
|
|
411
|
self.pycolorize = lambda src: pyformat(src,'str',self.colors)
|
|
408
|
|
|
412
|
|
|
409
|
def init_pushd_popd_magic(self):
|
|
413
|
def init_pushd_popd_magic(self):
|
|
410
|
# for pushd/popd management
|
|
414
|
# for pushd/popd management
|
|
411
|
try:
|
|
415
|
try:
|
|
412
|
self.home_dir = get_home_dir()
|
|
416
|
self.home_dir = get_home_dir()
|
|
413
|
except HomeDirError, msg:
|
|
417
|
except HomeDirError, msg:
|
|
414
|
fatal(msg)
|
|
418
|
fatal(msg)
|
|
415
|
|
|
419
|
|
|
416
|
self.dir_stack = []
|
|
420
|
self.dir_stack = []
|
|
417
|
|
|
421
|
|
|
418
|
def init_logger(self):
|
|
422
|
def init_logger(self):
|
|
419
|
self.logger = Logger(self, logfname='ipython_log.py', logmode='rotate')
|
|
423
|
self.logger = Logger(self, logfname='ipython_log.py', logmode='rotate')
|
|
420
|
# local shortcut, this is used a LOT
|
|
424
|
# local shortcut, this is used a LOT
|
|
421
|
self.log = self.logger.log
|
|
425
|
self.log = self.logger.log
|
|
422
|
|
|
426
|
|
|
423
|
def init_logstart(self):
|
|
427
|
def init_logstart(self):
|
|
424
|
if self.logappend:
|
|
428
|
if self.logappend:
|
|
425
|
self.magic_logstart(self.logappend + ' append')
|
|
429
|
self.magic_logstart(self.logappend + ' append')
|
|
426
|
elif self.logfile:
|
|
430
|
elif self.logfile:
|
|
427
|
self.magic_logstart(self.logfile)
|
|
431
|
self.magic_logstart(self.logfile)
|
|
428
|
elif self.logstart:
|
|
432
|
elif self.logstart:
|
|
429
|
self.magic_logstart()
|
|
433
|
self.magic_logstart()
|
|
430
|
|
|
434
|
|
|
431
|
def init_builtins(self):
|
|
435
|
def init_builtins(self):
|
|
432
|
self.builtin_trap = BuiltinTrap(shell=self)
|
|
436
|
self.builtin_trap = BuiltinTrap(shell=self)
|
|
433
|
|
|
437
|
|
|
434
|
def init_inspector(self):
|
|
438
|
def init_inspector(self):
|
|
435
|
# Object inspector
|
|
439
|
# Object inspector
|
|
436
|
self.inspector = oinspect.Inspector(oinspect.InspectColors,
|
|
440
|
self.inspector = oinspect.Inspector(oinspect.InspectColors,
|
|
437
|
PyColorize.ANSICodeColors,
|
|
441
|
PyColorize.ANSICodeColors,
|
|
438
|
'NoColor',
|
|
442
|
'NoColor',
|
|
439
|
self.object_info_string_level)
|
|
443
|
self.object_info_string_level)
|
|
440
|
|
|
444
|
|
|
441
|
def init_io(self):
|
|
445
|
def init_io(self):
|
|
442
|
# This will just use sys.stdout and sys.stderr. If you want to
|
|
446
|
# This will just use sys.stdout and sys.stderr. If you want to
|
|
443
|
# override sys.stdout and sys.stderr themselves, you need to do that
|
|
447
|
# override sys.stdout and sys.stderr themselves, you need to do that
|
|
444
|
# *before* instantiating this class, because Term holds onto
|
|
448
|
# *before* instantiating this class, because Term holds onto
|
|
445
|
# references to the underlying streams.
|
|
449
|
# references to the underlying streams.
|
|
446
|
if sys.platform == 'win32' and self.has_readline:
|
|
450
|
if sys.platform == 'win32' and self.has_readline:
|
|
447
|
Term = io.IOTerm(cout=self.readline._outputfile,
|
|
451
|
Term = io.IOTerm(cout=self.readline._outputfile,
|
|
448
|
cerr=self.readline._outputfile)
|
|
452
|
cerr=self.readline._outputfile)
|
|
449
|
else:
|
|
453
|
else:
|
|
450
|
Term = io.IOTerm()
|
|
454
|
Term = io.IOTerm()
|
|
451
|
io.Term = Term
|
|
455
|
io.Term = Term
|
|
452
|
|
|
456
|
|
|
453
|
def init_prompts(self):
|
|
457
|
def init_prompts(self):
|
|
454
|
# TODO: This is a pass for now because the prompts are managed inside
|
|
458
|
# TODO: This is a pass for now because the prompts are managed inside
|
|
455
|
# the DisplayHook. Once there is a separate prompt manager, this
|
|
459
|
# the DisplayHook. Once there is a separate prompt manager, this
|
|
456
|
# will initialize that object and all prompt related information.
|
|
460
|
# will initialize that object and all prompt related information.
|
|
457
|
pass
|
|
461
|
pass
|
|
458
|
|
|
462
|
|
|
459
|
def init_displayhook(self):
|
|
463
|
def init_displayhook(self):
|
|
460
|
# Initialize displayhook, set in/out prompts and printing system
|
|
464
|
# Initialize displayhook, set in/out prompts and printing system
|
|
461
|
self.displayhook = self.displayhook_class(
|
|
465
|
self.displayhook = self.displayhook_class(
|
|
462
|
shell=self,
|
|
466
|
shell=self,
|
|
463
|
cache_size=self.cache_size,
|
|
467
|
cache_size=self.cache_size,
|
|
464
|
input_sep = self.separate_in,
|
|
468
|
input_sep = self.separate_in,
|
|
465
|
output_sep = self.separate_out,
|
|
469
|
output_sep = self.separate_out,
|
|
466
|
output_sep2 = self.separate_out2,
|
|
470
|
output_sep2 = self.separate_out2,
|
|
467
|
ps1 = self.prompt_in1,
|
|
471
|
ps1 = self.prompt_in1,
|
|
468
|
ps2 = self.prompt_in2,
|
|
472
|
ps2 = self.prompt_in2,
|
|
469
|
ps_out = self.prompt_out,
|
|
473
|
ps_out = self.prompt_out,
|
|
470
|
pad_left = self.prompts_pad_left
|
|
474
|
pad_left = self.prompts_pad_left
|
|
471
|
)
|
|
475
|
)
|
|
472
|
# This is a context manager that installs/revmoes the displayhook at
|
|
476
|
# This is a context manager that installs/revmoes the displayhook at
|
|
473
|
# the appropriate time.
|
|
477
|
# the appropriate time.
|
|
474
|
self.display_trap = DisplayTrap(hook=self.displayhook)
|
|
478
|
self.display_trap = DisplayTrap(hook=self.displayhook)
|
|
475
|
|
|
479
|
|
|
476
|
def init_reload_doctest(self):
|
|
480
|
def init_reload_doctest(self):
|
|
477
|
# Do a proper resetting of doctest, including the necessary displayhook
|
|
481
|
# Do a proper resetting of doctest, including the necessary displayhook
|
|
478
|
# monkeypatching
|
|
482
|
# monkeypatching
|
|
479
|
try:
|
|
483
|
try:
|
|
480
|
doctest_reload()
|
|
484
|
doctest_reload()
|
|
481
|
except ImportError:
|
|
485
|
except ImportError:
|
|
482
|
warn("doctest module does not exist.")
|
|
486
|
warn("doctest module does not exist.")
|
|
483
|
|
|
487
|
|
|
484
|
#-------------------------------------------------------------------------
|
|
488
|
#-------------------------------------------------------------------------
|
|
485
|
# Things related to injections into the sys module
|
|
489
|
# Things related to injections into the sys module
|
|
486
|
#-------------------------------------------------------------------------
|
|
490
|
#-------------------------------------------------------------------------
|
|
487
|
|
|
491
|
|
|
488
|
def save_sys_module_state(self):
|
|
492
|
def save_sys_module_state(self):
|
|
489
|
"""Save the state of hooks in the sys module.
|
|
493
|
"""Save the state of hooks in the sys module.
|
|
490
|
|
|
494
|
|
|
491
|
This has to be called after self.user_ns is created.
|
|
495
|
This has to be called after self.user_ns is created.
|
|
492
|
"""
|
|
496
|
"""
|
|
493
|
self._orig_sys_module_state = {}
|
|
497
|
self._orig_sys_module_state = {}
|
|
494
|
self._orig_sys_module_state['stdin'] = sys.stdin
|
|
498
|
self._orig_sys_module_state['stdin'] = sys.stdin
|
|
495
|
self._orig_sys_module_state['stdout'] = sys.stdout
|
|
499
|
self._orig_sys_module_state['stdout'] = sys.stdout
|
|
496
|
self._orig_sys_module_state['stderr'] = sys.stderr
|
|
500
|
self._orig_sys_module_state['stderr'] = sys.stderr
|
|
497
|
self._orig_sys_module_state['excepthook'] = sys.excepthook
|
|
501
|
self._orig_sys_module_state['excepthook'] = sys.excepthook
|
|
498
|
try:
|
|
502
|
try:
|
|
499
|
self._orig_sys_modules_main_name = self.user_ns['__name__']
|
|
503
|
self._orig_sys_modules_main_name = self.user_ns['__name__']
|
|
500
|
except KeyError:
|
|
504
|
except KeyError:
|
|
501
|
pass
|
|
505
|
pass
|
|
502
|
|
|
506
|
|
|
503
|
def restore_sys_module_state(self):
|
|
507
|
def restore_sys_module_state(self):
|
|
504
|
"""Restore the state of the sys module."""
|
|
508
|
"""Restore the state of the sys module."""
|
|
505
|
try:
|
|
509
|
try:
|
|
506
|
for k, v in self._orig_sys_module_state.items():
|
|
510
|
for k, v in self._orig_sys_module_state.items():
|
|
507
|
setattr(sys, k, v)
|
|
511
|
setattr(sys, k, v)
|
|
508
|
except AttributeError:
|
|
512
|
except AttributeError:
|
|
509
|
pass
|
|
513
|
pass
|
|
510
|
# Reset what what done in self.init_sys_modules
|
|
514
|
# Reset what what done in self.init_sys_modules
|
|
511
|
try:
|
|
515
|
try:
|
|
512
|
sys.modules[self.user_ns['__name__']] = self._orig_sys_modules_main_name
|
|
516
|
sys.modules[self.user_ns['__name__']] = self._orig_sys_modules_main_name
|
|
513
|
except (AttributeError, KeyError):
|
|
517
|
except (AttributeError, KeyError):
|
|
514
|
pass
|
|
518
|
pass
|
|
515
|
|
|
519
|
|
|
516
|
#-------------------------------------------------------------------------
|
|
520
|
#-------------------------------------------------------------------------
|
|
517
|
# Things related to hooks
|
|
521
|
# Things related to hooks
|
|
518
|
#-------------------------------------------------------------------------
|
|
522
|
#-------------------------------------------------------------------------
|
|
519
|
|
|
523
|
|
|
520
|
def init_hooks(self):
|
|
524
|
def init_hooks(self):
|
|
521
|
# hooks holds pointers used for user-side customizations
|
|
525
|
# hooks holds pointers used for user-side customizations
|
|
522
|
self.hooks = Struct()
|
|
526
|
self.hooks = Struct()
|
|
523
|
|
|
527
|
|
|
524
|
self.strdispatchers = {}
|
|
528
|
self.strdispatchers = {}
|
|
525
|
|
|
529
|
|
|
526
|
# Set all default hooks, defined in the IPython.hooks module.
|
|
530
|
# Set all default hooks, defined in the IPython.hooks module.
|
|
527
|
hooks = IPython.core.hooks
|
|
531
|
hooks = IPython.core.hooks
|
|
528
|
for hook_name in hooks.__all__:
|
|
532
|
for hook_name in hooks.__all__:
|
|
529
|
# default hooks have priority 100, i.e. low; user hooks should have
|
|
533
|
# default hooks have priority 100, i.e. low; user hooks should have
|
|
530
|
# 0-100 priority
|
|
534
|
# 0-100 priority
|
|
531
|
self.set_hook(hook_name,getattr(hooks,hook_name), 100)
|
|
535
|
self.set_hook(hook_name,getattr(hooks,hook_name), 100)
|
|
532
|
|
|
536
|
|
|
533
|
def set_hook(self,name,hook, priority = 50, str_key = None, re_key = None):
|
|
537
|
def set_hook(self,name,hook, priority = 50, str_key = None, re_key = None):
|
|
534
|
"""set_hook(name,hook) -> sets an internal IPython hook.
|
|
538
|
"""set_hook(name,hook) -> sets an internal IPython hook.
|
|
535
|
|
|
539
|
|
|
536
|
IPython exposes some of its internal API as user-modifiable hooks. By
|
|
540
|
IPython exposes some of its internal API as user-modifiable hooks. By
|
|
537
|
adding your function to one of these hooks, you can modify IPython's
|
|
541
|
adding your function to one of these hooks, you can modify IPython's
|
|
538
|
behavior to call at runtime your own routines."""
|
|
542
|
behavior to call at runtime your own routines."""
|
|
539
|
|
|
543
|
|
|
540
|
# At some point in the future, this should validate the hook before it
|
|
544
|
# At some point in the future, this should validate the hook before it
|
|
541
|
# accepts it. Probably at least check that the hook takes the number
|
|
545
|
# accepts it. Probably at least check that the hook takes the number
|
|
542
|
# of args it's supposed to.
|
|
546
|
# of args it's supposed to.
|
|
543
|
|
|
547
|
|
|
544
|
f = new.instancemethod(hook,self,self.__class__)
|
|
548
|
f = new.instancemethod(hook,self,self.__class__)
|
|
545
|
|
|
549
|
|
|
546
|
# check if the hook is for strdispatcher first
|
|
550
|
# check if the hook is for strdispatcher first
|
|
547
|
if str_key is not None:
|
|
551
|
if str_key is not None:
|
|
548
|
sdp = self.strdispatchers.get(name, StrDispatch())
|
|
552
|
sdp = self.strdispatchers.get(name, StrDispatch())
|
|
549
|
sdp.add_s(str_key, f, priority )
|
|
553
|
sdp.add_s(str_key, f, priority )
|
|
550
|
self.strdispatchers[name] = sdp
|
|
554
|
self.strdispatchers[name] = sdp
|
|
551
|
return
|
|
555
|
return
|
|
552
|
if re_key is not None:
|
|
556
|
if re_key is not None:
|
|
553
|
sdp = self.strdispatchers.get(name, StrDispatch())
|
|
557
|
sdp = self.strdispatchers.get(name, StrDispatch())
|
|
554
|
sdp.add_re(re.compile(re_key), f, priority )
|
|
558
|
sdp.add_re(re.compile(re_key), f, priority )
|
|
555
|
self.strdispatchers[name] = sdp
|
|
559
|
self.strdispatchers[name] = sdp
|
|
556
|
return
|
|
560
|
return
|
|
557
|
|
|
561
|
|
|
558
|
dp = getattr(self.hooks, name, None)
|
|
562
|
dp = getattr(self.hooks, name, None)
|
|
559
|
if name not in IPython.core.hooks.__all__:
|
|
563
|
if name not in IPython.core.hooks.__all__:
|
|
560
|
print "Warning! Hook '%s' is not one of %s" % \
|
|
564
|
print "Warning! Hook '%s' is not one of %s" % \
|
|
561
|
(name, IPython.core.hooks.__all__ )
|
|
565
|
(name, IPython.core.hooks.__all__ )
|
|
562
|
if not dp:
|
|
566
|
if not dp:
|
|
563
|
dp = IPython.core.hooks.CommandChainDispatcher()
|
|
567
|
dp = IPython.core.hooks.CommandChainDispatcher()
|
|
564
|
|
|
568
|
|
|
565
|
try:
|
|
569
|
try:
|
|
566
|
dp.add(f,priority)
|
|
570
|
dp.add(f,priority)
|
|
567
|
except AttributeError:
|
|
571
|
except AttributeError:
|
|
568
|
# it was not commandchain, plain old func - replace
|
|
572
|
# it was not commandchain, plain old func - replace
|
|
569
|
dp = f
|
|
573
|
dp = f
|
|
570
|
|
|
574
|
|
|
571
|
setattr(self.hooks,name, dp)
|
|
575
|
setattr(self.hooks,name, dp)
|
|
572
|
|
|
576
|
|
|
|
|
|
577
|
def register_post_execute(self, func):
|
|
|
|
|
578
|
"""Register a function for calling after code execution.
|
|
|
|
|
579
|
"""
|
|
|
|
|
580
|
if not callable(func):
|
|
|
|
|
581
|
raise ValueError('argument %s must be callable' % func)
|
|
|
|
|
582
|
self._post_execute.add(func)
|
|
|
|
|
583
|
|
|
573
|
#-------------------------------------------------------------------------
|
|
584
|
#-------------------------------------------------------------------------
|
|
574
|
# Things related to the "main" module
|
|
585
|
# Things related to the "main" module
|
|
575
|
#-------------------------------------------------------------------------
|
|
586
|
#-------------------------------------------------------------------------
|
|
576
|
|
|
587
|
|
|
577
|
def new_main_mod(self,ns=None):
|
|
588
|
def new_main_mod(self,ns=None):
|
|
578
|
"""Return a new 'main' module object for user code execution.
|
|
589
|
"""Return a new 'main' module object for user code execution.
|
|
579
|
"""
|
|
590
|
"""
|
|
580
|
main_mod = self._user_main_module
|
|
591
|
main_mod = self._user_main_module
|
|
581
|
init_fakemod_dict(main_mod,ns)
|
|
592
|
init_fakemod_dict(main_mod,ns)
|
|
582
|
return main_mod
|
|
593
|
return main_mod
|
|
583
|
|
|
594
|
|
|
584
|
def cache_main_mod(self,ns,fname):
|
|
595
|
def cache_main_mod(self,ns,fname):
|
|
585
|
"""Cache a main module's namespace.
|
|
596
|
"""Cache a main module's namespace.
|
|
586
|
|
|
597
|
|
|
587
|
When scripts are executed via %run, we must keep a reference to the
|
|
598
|
When scripts are executed via %run, we must keep a reference to the
|
|
588
|
namespace of their __main__ module (a FakeModule instance) around so
|
|
599
|
namespace of their __main__ module (a FakeModule instance) around so
|
|
589
|
that Python doesn't clear it, rendering objects defined therein
|
|
600
|
that Python doesn't clear it, rendering objects defined therein
|
|
590
|
useless.
|
|
601
|
useless.
|
|
591
|
|
|
602
|
|
|
592
|
This method keeps said reference in a private dict, keyed by the
|
|
603
|
This method keeps said reference in a private dict, keyed by the
|
|
593
|
absolute path of the module object (which corresponds to the script
|
|
604
|
absolute path of the module object (which corresponds to the script
|
|
594
|
path). This way, for multiple executions of the same script we only
|
|
605
|
path). This way, for multiple executions of the same script we only
|
|
595
|
keep one copy of the namespace (the last one), thus preventing memory
|
|
606
|
keep one copy of the namespace (the last one), thus preventing memory
|
|
596
|
leaks from old references while allowing the objects from the last
|
|
607
|
leaks from old references while allowing the objects from the last
|
|
597
|
execution to be accessible.
|
|
608
|
execution to be accessible.
|
|
598
|
|
|
609
|
|
|
599
|
Note: we can not allow the actual FakeModule instances to be deleted,
|
|
610
|
Note: we can not allow the actual FakeModule instances to be deleted,
|
|
600
|
because of how Python tears down modules (it hard-sets all their
|
|
611
|
because of how Python tears down modules (it hard-sets all their
|
|
601
|
references to None without regard for reference counts). This method
|
|
612
|
references to None without regard for reference counts). This method
|
|
602
|
must therefore make a *copy* of the given namespace, to allow the
|
|
613
|
must therefore make a *copy* of the given namespace, to allow the
|
|
603
|
original module's __dict__ to be cleared and reused.
|
|
614
|
original module's __dict__ to be cleared and reused.
|
|
604
|
|
|
615
|
|
|
605
|
|
|
616
|
|
|
606
|
Parameters
|
|
617
|
Parameters
|
|
607
|
----------
|
|
618
|
----------
|
|
608
|
ns : a namespace (a dict, typically)
|
|
619
|
ns : a namespace (a dict, typically)
|
|
609
|
|
|
620
|
|
|
610
|
fname : str
|
|
621
|
fname : str
|
|
611
|
Filename associated with the namespace.
|
|
622
|
Filename associated with the namespace.
|
|
612
|
|
|
623
|
|
|
613
|
Examples
|
|
624
|
Examples
|
|
614
|
--------
|
|
625
|
--------
|
|
615
|
|
|
626
|
|
|
616
|
In [10]: import IPython
|
|
627
|
In [10]: import IPython
|
|
617
|
|
|
628
|
|
|
618
|
In [11]: _ip.cache_main_mod(IPython.__dict__,IPython.__file__)
|
|
629
|
In [11]: _ip.cache_main_mod(IPython.__dict__,IPython.__file__)
|
|
619
|
|
|
630
|
|
|
620
|
In [12]: IPython.__file__ in _ip._main_ns_cache
|
|
631
|
In [12]: IPython.__file__ in _ip._main_ns_cache
|
|
621
|
Out[12]: True
|
|
632
|
Out[12]: True
|
|
622
|
"""
|
|
633
|
"""
|
|
623
|
self._main_ns_cache[os.path.abspath(fname)] = ns.copy()
|
|
634
|
self._main_ns_cache[os.path.abspath(fname)] = ns.copy()
|
|
624
|
|
|
635
|
|
|
625
|
def clear_main_mod_cache(self):
|
|
636
|
def clear_main_mod_cache(self):
|
|
626
|
"""Clear the cache of main modules.
|
|
637
|
"""Clear the cache of main modules.
|
|
627
|
|
|
638
|
|
|
628
|
Mainly for use by utilities like %reset.
|
|
639
|
Mainly for use by utilities like %reset.
|
|
629
|
|
|
640
|
|
|
630
|
Examples
|
|
641
|
Examples
|
|
631
|
--------
|
|
642
|
--------
|
|
632
|
|
|
643
|
|
|
633
|
In [15]: import IPython
|
|
644
|
In [15]: import IPython
|
|
634
|
|
|
645
|
|
|
635
|
In [16]: _ip.cache_main_mod(IPython.__dict__,IPython.__file__)
|
|
646
|
In [16]: _ip.cache_main_mod(IPython.__dict__,IPython.__file__)
|
|
636
|
|
|
647
|
|
|
637
|
In [17]: len(_ip._main_ns_cache) > 0
|
|
648
|
In [17]: len(_ip._main_ns_cache) > 0
|
|
638
|
Out[17]: True
|
|
649
|
Out[17]: True
|
|
639
|
|
|
650
|
|
|
640
|
In [18]: _ip.clear_main_mod_cache()
|
|
651
|
In [18]: _ip.clear_main_mod_cache()
|
|
641
|
|
|
652
|
|
|
642
|
In [19]: len(_ip._main_ns_cache) == 0
|
|
653
|
In [19]: len(_ip._main_ns_cache) == 0
|
|
643
|
Out[19]: True
|
|
654
|
Out[19]: True
|
|
644
|
"""
|
|
655
|
"""
|
|
645
|
self._main_ns_cache.clear()
|
|
656
|
self._main_ns_cache.clear()
|
|
646
|
|
|
657
|
|
|
647
|
#-------------------------------------------------------------------------
|
|
658
|
#-------------------------------------------------------------------------
|
|
648
|
# Things related to debugging
|
|
659
|
# Things related to debugging
|
|
649
|
#-------------------------------------------------------------------------
|
|
660
|
#-------------------------------------------------------------------------
|
|
650
|
|
|
661
|
|
|
651
|
def init_pdb(self):
|
|
662
|
def init_pdb(self):
|
|
652
|
# Set calling of pdb on exceptions
|
|
663
|
# Set calling of pdb on exceptions
|
|
653
|
# self.call_pdb is a property
|
|
664
|
# self.call_pdb is a property
|
|
654
|
self.call_pdb = self.pdb
|
|
665
|
self.call_pdb = self.pdb
|
|
655
|
|
|
666
|
|
|
656
|
def _get_call_pdb(self):
|
|
667
|
def _get_call_pdb(self):
|
|
657
|
return self._call_pdb
|
|
668
|
return self._call_pdb
|
|
658
|
|
|
669
|
|
|
659
|
def _set_call_pdb(self,val):
|
|
670
|
def _set_call_pdb(self,val):
|
|
660
|
|
|
671
|
|
|
661
|
if val not in (0,1,False,True):
|
|
672
|
if val not in (0,1,False,True):
|
|
662
|
raise ValueError,'new call_pdb value must be boolean'
|
|
673
|
raise ValueError,'new call_pdb value must be boolean'
|
|
663
|
|
|
674
|
|
|
664
|
# store value in instance
|
|
675
|
# store value in instance
|
|
665
|
self._call_pdb = val
|
|
676
|
self._call_pdb = val
|
|
666
|
|
|
677
|
|
|
667
|
# notify the actual exception handlers
|
|
678
|
# notify the actual exception handlers
|
|
668
|
self.InteractiveTB.call_pdb = val
|
|
679
|
self.InteractiveTB.call_pdb = val
|
|
669
|
|
|
680
|
|
|
670
|
call_pdb = property(_get_call_pdb,_set_call_pdb,None,
|
|
681
|
call_pdb = property(_get_call_pdb,_set_call_pdb,None,
|
|
671
|
'Control auto-activation of pdb at exceptions')
|
|
682
|
'Control auto-activation of pdb at exceptions')
|
|
672
|
|
|
683
|
|
|
673
|
def debugger(self,force=False):
|
|
684
|
def debugger(self,force=False):
|
|
674
|
"""Call the pydb/pdb debugger.
|
|
685
|
"""Call the pydb/pdb debugger.
|
|
675
|
|
|
686
|
|
|
676
|
Keywords:
|
|
687
|
Keywords:
|
|
677
|
|
|
688
|
|
|
678
|
- force(False): by default, this routine checks the instance call_pdb
|
|
689
|
- force(False): by default, this routine checks the instance call_pdb
|
|
679
|
flag and does not actually invoke the debugger if the flag is false.
|
|
690
|
flag and does not actually invoke the debugger if the flag is false.
|
|
680
|
The 'force' option forces the debugger to activate even if the flag
|
|
691
|
The 'force' option forces the debugger to activate even if the flag
|
|
681
|
is false.
|
|
692
|
is false.
|
|
682
|
"""
|
|
693
|
"""
|
|
683
|
|
|
694
|
|
|
684
|
if not (force or self.call_pdb):
|
|
695
|
if not (force or self.call_pdb):
|
|
685
|
return
|
|
696
|
return
|
|
686
|
|
|
697
|
|
|
687
|
if not hasattr(sys,'last_traceback'):
|
|
698
|
if not hasattr(sys,'last_traceback'):
|
|
688
|
error('No traceback has been produced, nothing to debug.')
|
|
699
|
error('No traceback has been produced, nothing to debug.')
|
|
689
|
return
|
|
700
|
return
|
|
690
|
|
|
701
|
|
|
691
|
# use pydb if available
|
|
702
|
# use pydb if available
|
|
692
|
if debugger.has_pydb:
|
|
703
|
if debugger.has_pydb:
|
|
693
|
from pydb import pm
|
|
704
|
from pydb import pm
|
|
694
|
else:
|
|
705
|
else:
|
|
695
|
# fallback to our internal debugger
|
|
706
|
# fallback to our internal debugger
|
|
696
|
pm = lambda : self.InteractiveTB.debugger(force=True)
|
|
707
|
pm = lambda : self.InteractiveTB.debugger(force=True)
|
|
697
|
self.history_saving_wrapper(pm)()
|
|
708
|
self.history_saving_wrapper(pm)()
|
|
698
|
|
|
709
|
|
|
699
|
#-------------------------------------------------------------------------
|
|
710
|
#-------------------------------------------------------------------------
|
|
700
|
# Things related to IPython's various namespaces
|
|
711
|
# Things related to IPython's various namespaces
|
|
701
|
#-------------------------------------------------------------------------
|
|
712
|
#-------------------------------------------------------------------------
|
|
702
|
|
|
713
|
|
|
703
|
def init_create_namespaces(self, user_ns=None, user_global_ns=None):
|
|
714
|
def init_create_namespaces(self, user_ns=None, user_global_ns=None):
|
|
704
|
# Create the namespace where the user will operate. user_ns is
|
|
715
|
# Create the namespace where the user will operate. user_ns is
|
|
705
|
# normally the only one used, and it is passed to the exec calls as
|
|
716
|
# normally the only one used, and it is passed to the exec calls as
|
|
706
|
# the locals argument. But we do carry a user_global_ns namespace
|
|
717
|
# the locals argument. But we do carry a user_global_ns namespace
|
|
707
|
# given as the exec 'globals' argument, This is useful in embedding
|
|
718
|
# given as the exec 'globals' argument, This is useful in embedding
|
|
708
|
# situations where the ipython shell opens in a context where the
|
|
719
|
# situations where the ipython shell opens in a context where the
|
|
709
|
# distinction between locals and globals is meaningful. For
|
|
720
|
# distinction between locals and globals is meaningful. For
|
|
710
|
# non-embedded contexts, it is just the same object as the user_ns dict.
|
|
721
|
# non-embedded contexts, it is just the same object as the user_ns dict.
|
|
711
|
|
|
722
|
|
|
712
|
# FIXME. For some strange reason, __builtins__ is showing up at user
|
|
723
|
# FIXME. For some strange reason, __builtins__ is showing up at user
|
|
713
|
# level as a dict instead of a module. This is a manual fix, but I
|
|
724
|
# level as a dict instead of a module. This is a manual fix, but I
|
|
714
|
# should really track down where the problem is coming from. Alex
|
|
725
|
# should really track down where the problem is coming from. Alex
|
|
715
|
# Schmolck reported this problem first.
|
|
726
|
# Schmolck reported this problem first.
|
|
716
|
|
|
727
|
|
|
717
|
# A useful post by Alex Martelli on this topic:
|
|
728
|
# A useful post by Alex Martelli on this topic:
|
|
718
|
# Re: inconsistent value from __builtins__
|
|
729
|
# Re: inconsistent value from __builtins__
|
|
719
|
# Von: Alex Martelli <aleaxit@yahoo.com>
|
|
730
|
# Von: Alex Martelli <aleaxit@yahoo.com>
|
|
720
|
# Datum: Freitag 01 Oktober 2004 04:45:34 nachmittags/abends
|
|
731
|
# Datum: Freitag 01 Oktober 2004 04:45:34 nachmittags/abends
|
|
721
|
# Gruppen: comp.lang.python
|
|
732
|
# Gruppen: comp.lang.python
|
|
722
|
|
|
733
|
|
|
723
|
# Michael Hohn <hohn@hooknose.lbl.gov> wrote:
|
|
734
|
# Michael Hohn <hohn@hooknose.lbl.gov> wrote:
|
|
724
|
# > >>> print type(builtin_check.get_global_binding('__builtins__'))
|
|
735
|
# > >>> print type(builtin_check.get_global_binding('__builtins__'))
|
|
725
|
# > <type 'dict'>
|
|
736
|
# > <type 'dict'>
|
|
726
|
# > >>> print type(__builtins__)
|
|
737
|
# > >>> print type(__builtins__)
|
|
727
|
# > <type 'module'>
|
|
738
|
# > <type 'module'>
|
|
728
|
# > Is this difference in return value intentional?
|
|
739
|
# > Is this difference in return value intentional?
|
|
729
|
|
|
740
|
|
|
730
|
# Well, it's documented that '__builtins__' can be either a dictionary
|
|
741
|
# Well, it's documented that '__builtins__' can be either a dictionary
|
|
731
|
# or a module, and it's been that way for a long time. Whether it's
|
|
742
|
# or a module, and it's been that way for a long time. Whether it's
|
|
732
|
# intentional (or sensible), I don't know. In any case, the idea is
|
|
743
|
# intentional (or sensible), I don't know. In any case, the idea is
|
|
733
|
# that if you need to access the built-in namespace directly, you
|
|
744
|
# that if you need to access the built-in namespace directly, you
|
|
734
|
# should start with "import __builtin__" (note, no 's') which will
|
|
745
|
# should start with "import __builtin__" (note, no 's') which will
|
|
735
|
# definitely give you a module. Yeah, it's somewhat confusing:-(.
|
|
746
|
# definitely give you a module. Yeah, it's somewhat confusing:-(.
|
|
736
|
|
|
747
|
|
|
737
|
# These routines return properly built dicts as needed by the rest of
|
|
748
|
# These routines return properly built dicts as needed by the rest of
|
|
738
|
# the code, and can also be used by extension writers to generate
|
|
749
|
# the code, and can also be used by extension writers to generate
|
|
739
|
# properly initialized namespaces.
|
|
750
|
# properly initialized namespaces.
|
|
740
|
user_ns, user_global_ns = self.make_user_namespaces(user_ns,
|
|
751
|
user_ns, user_global_ns = self.make_user_namespaces(user_ns,
|
|
741
|
user_global_ns)
|
|
752
|
user_global_ns)
|
|
742
|
|
|
753
|
|
|
743
|
# Assign namespaces
|
|
754
|
# Assign namespaces
|
|
744
|
# This is the namespace where all normal user variables live
|
|
755
|
# This is the namespace where all normal user variables live
|
|
745
|
self.user_ns = user_ns
|
|
756
|
self.user_ns = user_ns
|
|
746
|
self.user_global_ns = user_global_ns
|
|
757
|
self.user_global_ns = user_global_ns
|
|
747
|
|
|
758
|
|
|
748
|
# An auxiliary namespace that checks what parts of the user_ns were
|
|
759
|
# An auxiliary namespace that checks what parts of the user_ns were
|
|
749
|
# loaded at startup, so we can list later only variables defined in
|
|
760
|
# loaded at startup, so we can list later only variables defined in
|
|
750
|
# actual interactive use. Since it is always a subset of user_ns, it
|
|
761
|
# actual interactive use. Since it is always a subset of user_ns, it
|
|
751
|
# doesn't need to be separately tracked in the ns_table.
|
|
762
|
# doesn't need to be separately tracked in the ns_table.
|
|
752
|
self.user_ns_hidden = {}
|
|
763
|
self.user_ns_hidden = {}
|
|
753
|
|
|
764
|
|
|
754
|
# A namespace to keep track of internal data structures to prevent
|
|
765
|
# A namespace to keep track of internal data structures to prevent
|
|
755
|
# them from cluttering user-visible stuff. Will be updated later
|
|
766
|
# them from cluttering user-visible stuff. Will be updated later
|
|
756
|
self.internal_ns = {}
|
|
767
|
self.internal_ns = {}
|
|
757
|
|
|
768
|
|
|
758
|
# Now that FakeModule produces a real module, we've run into a nasty
|
|
769
|
# Now that FakeModule produces a real module, we've run into a nasty
|
|
759
|
# problem: after script execution (via %run), the module where the user
|
|
770
|
# problem: after script execution (via %run), the module where the user
|
|
760
|
# code ran is deleted. Now that this object is a true module (needed
|
|
771
|
# code ran is deleted. Now that this object is a true module (needed
|
|
761
|
# so docetst and other tools work correctly), the Python module
|
|
772
|
# so docetst and other tools work correctly), the Python module
|
|
762
|
# teardown mechanism runs over it, and sets to None every variable
|
|
773
|
# teardown mechanism runs over it, and sets to None every variable
|
|
763
|
# present in that module. Top-level references to objects from the
|
|
774
|
# present in that module. Top-level references to objects from the
|
|
764
|
# script survive, because the user_ns is updated with them. However,
|
|
775
|
# script survive, because the user_ns is updated with them. However,
|
|
765
|
# calling functions defined in the script that use other things from
|
|
776
|
# calling functions defined in the script that use other things from
|
|
766
|
# the script will fail, because the function's closure had references
|
|
777
|
# the script will fail, because the function's closure had references
|
|
767
|
# to the original objects, which are now all None. So we must protect
|
|
778
|
# to the original objects, which are now all None. So we must protect
|
|
768
|
# these modules from deletion by keeping a cache.
|
|
779
|
# these modules from deletion by keeping a cache.
|
|
769
|
#
|
|
780
|
#
|
|
770
|
# To avoid keeping stale modules around (we only need the one from the
|
|
781
|
# To avoid keeping stale modules around (we only need the one from the
|
|
771
|
# last run), we use a dict keyed with the full path to the script, so
|
|
782
|
# last run), we use a dict keyed with the full path to the script, so
|
|
772
|
# only the last version of the module is held in the cache. Note,
|
|
783
|
# only the last version of the module is held in the cache. Note,
|
|
773
|
# however, that we must cache the module *namespace contents* (their
|
|
784
|
# however, that we must cache the module *namespace contents* (their
|
|
774
|
# __dict__). Because if we try to cache the actual modules, old ones
|
|
785
|
# __dict__). Because if we try to cache the actual modules, old ones
|
|
775
|
# (uncached) could be destroyed while still holding references (such as
|
|
786
|
# (uncached) could be destroyed while still holding references (such as
|
|
776
|
# those held by GUI objects that tend to be long-lived)>
|
|
787
|
# those held by GUI objects that tend to be long-lived)>
|
|
777
|
#
|
|
788
|
#
|
|
778
|
# The %reset command will flush this cache. See the cache_main_mod()
|
|
789
|
# The %reset command will flush this cache. See the cache_main_mod()
|
|
779
|
# and clear_main_mod_cache() methods for details on use.
|
|
790
|
# and clear_main_mod_cache() methods for details on use.
|
|
780
|
|
|
791
|
|
|
781
|
# This is the cache used for 'main' namespaces
|
|
792
|
# This is the cache used for 'main' namespaces
|
|
782
|
self._main_ns_cache = {}
|
|
793
|
self._main_ns_cache = {}
|
|
783
|
# And this is the single instance of FakeModule whose __dict__ we keep
|
|
794
|
# And this is the single instance of FakeModule whose __dict__ we keep
|
|
784
|
# copying and clearing for reuse on each %run
|
|
795
|
# copying and clearing for reuse on each %run
|
|
785
|
self._user_main_module = FakeModule()
|
|
796
|
self._user_main_module = FakeModule()
|
|
786
|
|
|
797
|
|
|
787
|
# A table holding all the namespaces IPython deals with, so that
|
|
798
|
# A table holding all the namespaces IPython deals with, so that
|
|
788
|
# introspection facilities can search easily.
|
|
799
|
# introspection facilities can search easily.
|
|
789
|
self.ns_table = {'user':user_ns,
|
|
800
|
self.ns_table = {'user':user_ns,
|
|
790
|
'user_global':user_global_ns,
|
|
801
|
'user_global':user_global_ns,
|
|
791
|
'internal':self.internal_ns,
|
|
802
|
'internal':self.internal_ns,
|
|
792
|
'builtin':__builtin__.__dict__
|
|
803
|
'builtin':__builtin__.__dict__
|
|
793
|
}
|
|
804
|
}
|
|
794
|
|
|
805
|
|
|
795
|
# Similarly, track all namespaces where references can be held and that
|
|
806
|
# Similarly, track all namespaces where references can be held and that
|
|
796
|
# we can safely clear (so it can NOT include builtin). This one can be
|
|
807
|
# we can safely clear (so it can NOT include builtin). This one can be
|
|
797
|
# a simple list.
|
|
808
|
# a simple list.
|
|
798
|
self.ns_refs_table = [ user_ns, user_global_ns, self.user_ns_hidden,
|
|
809
|
self.ns_refs_table = [ user_ns, user_global_ns, self.user_ns_hidden,
|
|
799
|
self.internal_ns, self._main_ns_cache ]
|
|
810
|
self.internal_ns, self._main_ns_cache ]
|
|
800
|
|
|
811
|
|
|
801
|
def make_user_namespaces(self, user_ns=None, user_global_ns=None):
|
|
812
|
def make_user_namespaces(self, user_ns=None, user_global_ns=None):
|
|
802
|
"""Return a valid local and global user interactive namespaces.
|
|
813
|
"""Return a valid local and global user interactive namespaces.
|
|
803
|
|
|
814
|
|
|
804
|
This builds a dict with the minimal information needed to operate as a
|
|
815
|
This builds a dict with the minimal information needed to operate as a
|
|
805
|
valid IPython user namespace, which you can pass to the various
|
|
816
|
valid IPython user namespace, which you can pass to the various
|
|
806
|
embedding classes in ipython. The default implementation returns the
|
|
817
|
embedding classes in ipython. The default implementation returns the
|
|
807
|
same dict for both the locals and the globals to allow functions to
|
|
818
|
same dict for both the locals and the globals to allow functions to
|
|
808
|
refer to variables in the namespace. Customized implementations can
|
|
819
|
refer to variables in the namespace. Customized implementations can
|
|
809
|
return different dicts. The locals dictionary can actually be anything
|
|
820
|
return different dicts. The locals dictionary can actually be anything
|
|
810
|
following the basic mapping protocol of a dict, but the globals dict
|
|
821
|
following the basic mapping protocol of a dict, but the globals dict
|
|
811
|
must be a true dict, not even a subclass. It is recommended that any
|
|
822
|
must be a true dict, not even a subclass. It is recommended that any
|
|
812
|
custom object for the locals namespace synchronize with the globals
|
|
823
|
custom object for the locals namespace synchronize with the globals
|
|
813
|
dict somehow.
|
|
824
|
dict somehow.
|
|
814
|
|
|
825
|
|
|
815
|
Raises TypeError if the provided globals namespace is not a true dict.
|
|
826
|
Raises TypeError if the provided globals namespace is not a true dict.
|
|
816
|
|
|
827
|
|
|
817
|
Parameters
|
|
828
|
Parameters
|
|
818
|
----------
|
|
829
|
----------
|
|
819
|
user_ns : dict-like, optional
|
|
830
|
user_ns : dict-like, optional
|
|
820
|
The current user namespace. The items in this namespace should
|
|
831
|
The current user namespace. The items in this namespace should
|
|
821
|
be included in the output. If None, an appropriate blank
|
|
832
|
be included in the output. If None, an appropriate blank
|
|
822
|
namespace should be created.
|
|
833
|
namespace should be created.
|
|
823
|
user_global_ns : dict, optional
|
|
834
|
user_global_ns : dict, optional
|
|
824
|
The current user global namespace. The items in this namespace
|
|
835
|
The current user global namespace. The items in this namespace
|
|
825
|
should be included in the output. If None, an appropriate
|
|
836
|
should be included in the output. If None, an appropriate
|
|
826
|
blank namespace should be created.
|
|
837
|
blank namespace should be created.
|
|
827
|
|
|
838
|
|
|
828
|
Returns
|
|
839
|
Returns
|
|
829
|
-------
|
|
840
|
-------
|
|
830
|
A pair of dictionary-like object to be used as the local namespace
|
|
841
|
A pair of dictionary-like object to be used as the local namespace
|
|
831
|
of the interpreter and a dict to be used as the global namespace.
|
|
842
|
of the interpreter and a dict to be used as the global namespace.
|
|
832
|
"""
|
|
843
|
"""
|
|
833
|
|
|
844
|
|
|
834
|
|
|
845
|
|
|
835
|
# We must ensure that __builtin__ (without the final 's') is always
|
|
846
|
# We must ensure that __builtin__ (without the final 's') is always
|
|
836
|
# available and pointing to the __builtin__ *module*. For more details:
|
|
847
|
# available and pointing to the __builtin__ *module*. For more details:
|
|
837
|
# http://mail.python.org/pipermail/python-dev/2001-April/014068.html
|
|
848
|
# http://mail.python.org/pipermail/python-dev/2001-April/014068.html
|
|
838
|
|
|
849
|
|
|
839
|
if user_ns is None:
|
|
850
|
if user_ns is None:
|
|
840
|
# Set __name__ to __main__ to better match the behavior of the
|
|
851
|
# Set __name__ to __main__ to better match the behavior of the
|
|
841
|
# normal interpreter.
|
|
852
|
# normal interpreter.
|
|
842
|
user_ns = {'__name__' :'__main__',
|
|
853
|
user_ns = {'__name__' :'__main__',
|
|
843
|
'__builtin__' : __builtin__,
|
|
854
|
'__builtin__' : __builtin__,
|
|
844
|
'__builtins__' : __builtin__,
|
|
855
|
'__builtins__' : __builtin__,
|
|
845
|
}
|
|
856
|
}
|
|
846
|
else:
|
|
857
|
else:
|
|
847
|
user_ns.setdefault('__name__','__main__')
|
|
858
|
user_ns.setdefault('__name__','__main__')
|
|
848
|
user_ns.setdefault('__builtin__',__builtin__)
|
|
859
|
user_ns.setdefault('__builtin__',__builtin__)
|
|
849
|
user_ns.setdefault('__builtins__',__builtin__)
|
|
860
|
user_ns.setdefault('__builtins__',__builtin__)
|
|
850
|
|
|
861
|
|
|
851
|
if user_global_ns is None:
|
|
862
|
if user_global_ns is None:
|
|
852
|
user_global_ns = user_ns
|
|
863
|
user_global_ns = user_ns
|
|
853
|
if type(user_global_ns) is not dict:
|
|
864
|
if type(user_global_ns) is not dict:
|
|
854
|
raise TypeError("user_global_ns must be a true dict; got %r"
|
|
865
|
raise TypeError("user_global_ns must be a true dict; got %r"
|
|
855
|
% type(user_global_ns))
|
|
866
|
% type(user_global_ns))
|
|
856
|
|
|
867
|
|
|
857
|
return user_ns, user_global_ns
|
|
868
|
return user_ns, user_global_ns
|
|
858
|
|
|
869
|
|
|
859
|
def init_sys_modules(self):
|
|
870
|
def init_sys_modules(self):
|
|
860
|
# We need to insert into sys.modules something that looks like a
|
|
871
|
# We need to insert into sys.modules something that looks like a
|
|
861
|
# module but which accesses the IPython namespace, for shelve and
|
|
872
|
# module but which accesses the IPython namespace, for shelve and
|
|
862
|
# pickle to work interactively. Normally they rely on getting
|
|
873
|
# pickle to work interactively. Normally they rely on getting
|
|
863
|
# everything out of __main__, but for embedding purposes each IPython
|
|
874
|
# everything out of __main__, but for embedding purposes each IPython
|
|
864
|
# instance has its own private namespace, so we can't go shoving
|
|
875
|
# instance has its own private namespace, so we can't go shoving
|
|
865
|
# everything into __main__.
|
|
876
|
# everything into __main__.
|
|
866
|
|
|
877
|
|
|
867
|
# note, however, that we should only do this for non-embedded
|
|
878
|
# note, however, that we should only do this for non-embedded
|
|
868
|
# ipythons, which really mimic the __main__.__dict__ with their own
|
|
879
|
# ipythons, which really mimic the __main__.__dict__ with their own
|
|
869
|
# namespace. Embedded instances, on the other hand, should not do
|
|
880
|
# namespace. Embedded instances, on the other hand, should not do
|
|
870
|
# this because they need to manage the user local/global namespaces
|
|
881
|
# this because they need to manage the user local/global namespaces
|
|
871
|
# only, but they live within a 'normal' __main__ (meaning, they
|
|
882
|
# only, but they live within a 'normal' __main__ (meaning, they
|
|
872
|
# shouldn't overtake the execution environment of the script they're
|
|
883
|
# shouldn't overtake the execution environment of the script they're
|
|
873
|
# embedded in).
|
|
884
|
# embedded in).
|
|
874
|
|
|
885
|
|
|
875
|
# This is overridden in the InteractiveShellEmbed subclass to a no-op.
|
|
886
|
# This is overridden in the InteractiveShellEmbed subclass to a no-op.
|
|
876
|
|
|
887
|
|
|
877
|
try:
|
|
888
|
try:
|
|
878
|
main_name = self.user_ns['__name__']
|
|
889
|
main_name = self.user_ns['__name__']
|
|
879
|
except KeyError:
|
|
890
|
except KeyError:
|
|
880
|
raise KeyError('user_ns dictionary MUST have a "__name__" key')
|
|
891
|
raise KeyError('user_ns dictionary MUST have a "__name__" key')
|
|
881
|
else:
|
|
892
|
else:
|
|
882
|
sys.modules[main_name] = FakeModule(self.user_ns)
|
|
893
|
sys.modules[main_name] = FakeModule(self.user_ns)
|
|
883
|
|
|
894
|
|
|
884
|
def init_user_ns(self):
|
|
895
|
def init_user_ns(self):
|
|
885
|
"""Initialize all user-visible namespaces to their minimum defaults.
|
|
896
|
"""Initialize all user-visible namespaces to their minimum defaults.
|
|
886
|
|
|
897
|
|
|
887
|
Certain history lists are also initialized here, as they effectively
|
|
898
|
Certain history lists are also initialized here, as they effectively
|
|
888
|
act as user namespaces.
|
|
899
|
act as user namespaces.
|
|
889
|
|
|
900
|
|
|
890
|
Notes
|
|
901
|
Notes
|
|
891
|
-----
|
|
902
|
-----
|
|
892
|
All data structures here are only filled in, they are NOT reset by this
|
|
903
|
All data structures here are only filled in, they are NOT reset by this
|
|
893
|
method. If they were not empty before, data will simply be added to
|
|
904
|
method. If they were not empty before, data will simply be added to
|
|
894
|
therm.
|
|
905
|
therm.
|
|
895
|
"""
|
|
906
|
"""
|
|
896
|
# This function works in two parts: first we put a few things in
|
|
907
|
# This function works in two parts: first we put a few things in
|
|
897
|
# user_ns, and we sync that contents into user_ns_hidden so that these
|
|
908
|
# user_ns, and we sync that contents into user_ns_hidden so that these
|
|
898
|
# initial variables aren't shown by %who. After the sync, we add the
|
|
909
|
# initial variables aren't shown by %who. After the sync, we add the
|
|
899
|
# rest of what we *do* want the user to see with %who even on a new
|
|
910
|
# rest of what we *do* want the user to see with %who even on a new
|
|
900
|
# session (probably nothing, so theye really only see their own stuff)
|
|
911
|
# session (probably nothing, so theye really only see their own stuff)
|
|
901
|
|
|
912
|
|
|
902
|
# The user dict must *always* have a __builtin__ reference to the
|
|
913
|
# The user dict must *always* have a __builtin__ reference to the
|
|
903
|
# Python standard __builtin__ namespace, which must be imported.
|
|
914
|
# Python standard __builtin__ namespace, which must be imported.
|
|
904
|
# This is so that certain operations in prompt evaluation can be
|
|
915
|
# This is so that certain operations in prompt evaluation can be
|
|
905
|
# reliably executed with builtins. Note that we can NOT use
|
|
916
|
# reliably executed with builtins. Note that we can NOT use
|
|
906
|
# __builtins__ (note the 's'), because that can either be a dict or a
|
|
917
|
# __builtins__ (note the 's'), because that can either be a dict or a
|
|
907
|
# module, and can even mutate at runtime, depending on the context
|
|
918
|
# module, and can even mutate at runtime, depending on the context
|
|
908
|
# (Python makes no guarantees on it). In contrast, __builtin__ is
|
|
919
|
# (Python makes no guarantees on it). In contrast, __builtin__ is
|
|
909
|
# always a module object, though it must be explicitly imported.
|
|
920
|
# always a module object, though it must be explicitly imported.
|
|
910
|
|
|
921
|
|
|
911
|
# For more details:
|
|
922
|
# For more details:
|
|
912
|
# http://mail.python.org/pipermail/python-dev/2001-April/014068.html
|
|
923
|
# http://mail.python.org/pipermail/python-dev/2001-April/014068.html
|
|
913
|
ns = dict(__builtin__ = __builtin__)
|
|
924
|
ns = dict(__builtin__ = __builtin__)
|
|
914
|
|
|
925
|
|
|
915
|
# Put 'help' in the user namespace
|
|
926
|
# Put 'help' in the user namespace
|
|
916
|
try:
|
|
927
|
try:
|
|
917
|
from site import _Helper
|
|
928
|
from site import _Helper
|
|
918
|
ns['help'] = _Helper()
|
|
929
|
ns['help'] = _Helper()
|
|
919
|
except ImportError:
|
|
930
|
except ImportError:
|
|
920
|
warn('help() not available - check site.py')
|
|
931
|
warn('help() not available - check site.py')
|
|
921
|
|
|
932
|
|
|
922
|
# make global variables for user access to the histories
|
|
933
|
# make global variables for user access to the histories
|
|
923
|
ns['_ih'] = self.input_hist
|
|
934
|
ns['_ih'] = self.input_hist
|
|
924
|
ns['_oh'] = self.output_hist
|
|
935
|
ns['_oh'] = self.output_hist
|
|
925
|
ns['_dh'] = self.dir_hist
|
|
936
|
ns['_dh'] = self.dir_hist
|
|
926
|
|
|
937
|
|
|
927
|
ns['_sh'] = shadowns
|
|
938
|
ns['_sh'] = shadowns
|
|
928
|
|
|
939
|
|
|
929
|
# user aliases to input and output histories. These shouldn't show up
|
|
940
|
# user aliases to input and output histories. These shouldn't show up
|
|
930
|
# in %who, as they can have very large reprs.
|
|
941
|
# in %who, as they can have very large reprs.
|
|
931
|
ns['In'] = self.input_hist
|
|
942
|
ns['In'] = self.input_hist
|
|
932
|
ns['Out'] = self.output_hist
|
|
943
|
ns['Out'] = self.output_hist
|
|
933
|
|
|
944
|
|
|
934
|
# Store myself as the public api!!!
|
|
945
|
# Store myself as the public api!!!
|
|
935
|
ns['get_ipython'] = self.get_ipython
|
|
946
|
ns['get_ipython'] = self.get_ipython
|
|
936
|
|
|
947
|
|
|
937
|
# Sync what we've added so far to user_ns_hidden so these aren't seen
|
|
948
|
# Sync what we've added so far to user_ns_hidden so these aren't seen
|
|
938
|
# by %who
|
|
949
|
# by %who
|
|
939
|
self.user_ns_hidden.update(ns)
|
|
950
|
self.user_ns_hidden.update(ns)
|
|
940
|
|
|
951
|
|
|
941
|
# Anything put into ns now would show up in %who. Think twice before
|
|
952
|
# Anything put into ns now would show up in %who. Think twice before
|
|
942
|
# putting anything here, as we really want %who to show the user their
|
|
953
|
# putting anything here, as we really want %who to show the user their
|
|
943
|
# stuff, not our variables.
|
|
954
|
# stuff, not our variables.
|
|
944
|
|
|
955
|
|
|
945
|
# Finally, update the real user's namespace
|
|
956
|
# Finally, update the real user's namespace
|
|
946
|
self.user_ns.update(ns)
|
|
957
|
self.user_ns.update(ns)
|
|
947
|
|
|
958
|
|
|
948
|
|
|
959
|
|
|
949
|
def reset(self):
|
|
960
|
def reset(self):
|
|
950
|
"""Clear all internal namespaces.
|
|
961
|
"""Clear all internal namespaces.
|
|
951
|
|
|
962
|
|
|
952
|
Note that this is much more aggressive than %reset, since it clears
|
|
963
|
Note that this is much more aggressive than %reset, since it clears
|
|
953
|
fully all namespaces, as well as all input/output lists.
|
|
964
|
fully all namespaces, as well as all input/output lists.
|
|
954
|
"""
|
|
965
|
"""
|
|
955
|
for ns in self.ns_refs_table:
|
|
966
|
for ns in self.ns_refs_table:
|
|
956
|
ns.clear()
|
|
967
|
ns.clear()
|
|
957
|
|
|
968
|
|
|
958
|
self.alias_manager.clear_aliases()
|
|
969
|
self.alias_manager.clear_aliases()
|
|
959
|
|
|
970
|
|
|
960
|
# Clear input and output histories
|
|
971
|
# Clear input and output histories
|
|
961
|
self.input_hist[:] = []
|
|
972
|
self.input_hist[:] = []
|
|
962
|
self.input_hist_raw[:] = []
|
|
973
|
self.input_hist_raw[:] = []
|
|
963
|
self.output_hist.clear()
|
|
974
|
self.output_hist.clear()
|
|
964
|
|
|
975
|
|
|
965
|
# Restore the user namespaces to minimal usability
|
|
976
|
# Restore the user namespaces to minimal usability
|
|
966
|
self.init_user_ns()
|
|
977
|
self.init_user_ns()
|
|
967
|
|
|
978
|
|
|
968
|
# Restore the default and user aliases
|
|
979
|
# Restore the default and user aliases
|
|
969
|
self.alias_manager.init_aliases()
|
|
980
|
self.alias_manager.init_aliases()
|
|
970
|
|
|
981
|
|
|
971
|
def reset_selective(self, regex=None):
|
|
982
|
def reset_selective(self, regex=None):
|
|
972
|
"""Clear selective variables from internal namespaces based on a
|
|
983
|
"""Clear selective variables from internal namespaces based on a
|
|
973
|
specified regular expression.
|
|
984
|
specified regular expression.
|
|
974
|
|
|
985
|
|
|
975
|
Parameters
|
|
986
|
Parameters
|
|
976
|
----------
|
|
987
|
----------
|
|
977
|
regex : string or compiled pattern, optional
|
|
988
|
regex : string or compiled pattern, optional
|
|
978
|
A regular expression pattern that will be used in searching
|
|
989
|
A regular expression pattern that will be used in searching
|
|
979
|
variable names in the users namespaces.
|
|
990
|
variable names in the users namespaces.
|
|
980
|
"""
|
|
991
|
"""
|
|
981
|
if regex is not None:
|
|
992
|
if regex is not None:
|
|
982
|
try:
|
|
993
|
try:
|
|
983
|
m = re.compile(regex)
|
|
994
|
m = re.compile(regex)
|
|
984
|
except TypeError:
|
|
995
|
except TypeError:
|
|
985
|
raise TypeError('regex must be a string or compiled pattern')
|
|
996
|
raise TypeError('regex must be a string or compiled pattern')
|
|
986
|
# Search for keys in each namespace that match the given regex
|
|
997
|
# Search for keys in each namespace that match the given regex
|
|
987
|
# If a match is found, delete the key/value pair.
|
|
998
|
# If a match is found, delete the key/value pair.
|
|
988
|
for ns in self.ns_refs_table:
|
|
999
|
for ns in self.ns_refs_table:
|
|
989
|
for var in ns:
|
|
1000
|
for var in ns:
|
|
990
|
if m.search(var):
|
|
1001
|
if m.search(var):
|
|
991
|
del ns[var]
|
|
1002
|
del ns[var]
|
|
992
|
|
|
1003
|
|
|
993
|
def push(self, variables, interactive=True):
|
|
1004
|
def push(self, variables, interactive=True):
|
|
994
|
"""Inject a group of variables into the IPython user namespace.
|
|
1005
|
"""Inject a group of variables into the IPython user namespace.
|
|
995
|
|
|
1006
|
|
|
996
|
Parameters
|
|
1007
|
Parameters
|
|
997
|
----------
|
|
1008
|
----------
|
|
998
|
variables : dict, str or list/tuple of str
|
|
1009
|
variables : dict, str or list/tuple of str
|
|
999
|
The variables to inject into the user's namespace. If a dict, a
|
|
1010
|
The variables to inject into the user's namespace. If a dict, a
|
|
1000
|
simple update is done. If a str, the string is assumed to have
|
|
1011
|
simple update is done. If a str, the string is assumed to have
|
|
1001
|
variable names separated by spaces. A list/tuple of str can also
|
|
1012
|
variable names separated by spaces. A list/tuple of str can also
|
|
1002
|
be used to give the variable names. If just the variable names are
|
|
1013
|
be used to give the variable names. If just the variable names are
|
|
1003
|
give (list/tuple/str) then the variable values looked up in the
|
|
1014
|
give (list/tuple/str) then the variable values looked up in the
|
|
1004
|
callers frame.
|
|
1015
|
callers frame.
|
|
1005
|
interactive : bool
|
|
1016
|
interactive : bool
|
|
1006
|
If True (default), the variables will be listed with the ``who``
|
|
1017
|
If True (default), the variables will be listed with the ``who``
|
|
1007
|
magic.
|
|
1018
|
magic.
|
|
1008
|
"""
|
|
1019
|
"""
|
|
1009
|
vdict = None
|
|
1020
|
vdict = None
|
|
1010
|
|
|
1021
|
|
|
1011
|
# We need a dict of name/value pairs to do namespace updates.
|
|
1022
|
# We need a dict of name/value pairs to do namespace updates.
|
|
1012
|
if isinstance(variables, dict):
|
|
1023
|
if isinstance(variables, dict):
|
|
1013
|
vdict = variables
|
|
1024
|
vdict = variables
|
|
1014
|
elif isinstance(variables, (basestring, list, tuple)):
|
|
1025
|
elif isinstance(variables, (basestring, list, tuple)):
|
|
1015
|
if isinstance(variables, basestring):
|
|
1026
|
if isinstance(variables, basestring):
|
|
1016
|
vlist = variables.split()
|
|
1027
|
vlist = variables.split()
|
|
1017
|
else:
|
|
1028
|
else:
|
|
1018
|
vlist = variables
|
|
1029
|
vlist = variables
|
|
1019
|
vdict = {}
|
|
1030
|
vdict = {}
|
|
1020
|
cf = sys._getframe(1)
|
|
1031
|
cf = sys._getframe(1)
|
|
1021
|
for name in vlist:
|
|
1032
|
for name in vlist:
|
|
1022
|
try:
|
|
1033
|
try:
|
|
1023
|
vdict[name] = eval(name, cf.f_globals, cf.f_locals)
|
|
1034
|
vdict[name] = eval(name, cf.f_globals, cf.f_locals)
|
|
1024
|
except:
|
|
1035
|
except:
|
|
1025
|
print ('Could not get variable %s from %s' %
|
|
1036
|
print ('Could not get variable %s from %s' %
|
|
1026
|
(name,cf.f_code.co_name))
|
|
1037
|
(name,cf.f_code.co_name))
|
|
1027
|
else:
|
|
1038
|
else:
|
|
1028
|
raise ValueError('variables must be a dict/str/list/tuple')
|
|
1039
|
raise ValueError('variables must be a dict/str/list/tuple')
|
|
1029
|
|
|
1040
|
|
|
1030
|
# Propagate variables to user namespace
|
|
1041
|
# Propagate variables to user namespace
|
|
1031
|
self.user_ns.update(vdict)
|
|
1042
|
self.user_ns.update(vdict)
|
|
1032
|
|
|
1043
|
|
|
1033
|
# And configure interactive visibility
|
|
1044
|
# And configure interactive visibility
|
|
1034
|
config_ns = self.user_ns_hidden
|
|
1045
|
config_ns = self.user_ns_hidden
|
|
1035
|
if interactive:
|
|
1046
|
if interactive:
|
|
1036
|
for name, val in vdict.iteritems():
|
|
1047
|
for name, val in vdict.iteritems():
|
|
1037
|
config_ns.pop(name, None)
|
|
1048
|
config_ns.pop(name, None)
|
|
1038
|
else:
|
|
1049
|
else:
|
|
1039
|
for name,val in vdict.iteritems():
|
|
1050
|
for name,val in vdict.iteritems():
|
|
1040
|
config_ns[name] = val
|
|
1051
|
config_ns[name] = val
|
|
1041
|
|
|
1052
|
|
|
1042
|
#-------------------------------------------------------------------------
|
|
1053
|
#-------------------------------------------------------------------------
|
|
1043
|
# Things related to object introspection
|
|
1054
|
# Things related to object introspection
|
|
1044
|
#-------------------------------------------------------------------------
|
|
1055
|
#-------------------------------------------------------------------------
|
|
1045
|
def _ofind(self, oname, namespaces=None):
|
|
1056
|
def _ofind(self, oname, namespaces=None):
|
|
1046
|
"""Find an object in the available namespaces.
|
|
1057
|
"""Find an object in the available namespaces.
|
|
1047
|
|
|
1058
|
|
|
1048
|
self._ofind(oname) -> dict with keys: found,obj,ospace,ismagic
|
|
1059
|
self._ofind(oname) -> dict with keys: found,obj,ospace,ismagic
|
|
1049
|
|
|
1060
|
|
|
1050
|
Has special code to detect magic functions.
|
|
1061
|
Has special code to detect magic functions.
|
|
1051
|
"""
|
|
1062
|
"""
|
|
1052
|
#oname = oname.strip()
|
|
1063
|
#oname = oname.strip()
|
|
1053
|
#print '1- oname: <%r>' % oname # dbg
|
|
1064
|
#print '1- oname: <%r>' % oname # dbg
|
|
1054
|
try:
|
|
1065
|
try:
|
|
1055
|
oname = oname.strip().encode('ascii')
|
|
1066
|
oname = oname.strip().encode('ascii')
|
|
1056
|
#print '2- oname: <%r>' % oname # dbg
|
|
1067
|
#print '2- oname: <%r>' % oname # dbg
|
|
1057
|
except UnicodeEncodeError:
|
|
1068
|
except UnicodeEncodeError:
|
|
1058
|
print 'Python identifiers can only contain ascii characters.'
|
|
1069
|
print 'Python identifiers can only contain ascii characters.'
|
|
1059
|
return dict(found=False)
|
|
1070
|
return dict(found=False)
|
|
1060
|
|
|
1071
|
|
|
1061
|
alias_ns = None
|
|
1072
|
alias_ns = None
|
|
1062
|
if namespaces is None:
|
|
1073
|
if namespaces is None:
|
|
1063
|
# Namespaces to search in:
|
|
1074
|
# Namespaces to search in:
|
|
1064
|
# Put them in a list. The order is important so that we
|
|
1075
|
# Put them in a list. The order is important so that we
|
|
1065
|
# find things in the same order that Python finds them.
|
|
1076
|
# find things in the same order that Python finds them.
|
|
1066
|
namespaces = [ ('Interactive', self.user_ns),
|
|
1077
|
namespaces = [ ('Interactive', self.user_ns),
|
|
1067
|
('IPython internal', self.internal_ns),
|
|
1078
|
('IPython internal', self.internal_ns),
|
|
1068
|
('Python builtin', __builtin__.__dict__),
|
|
1079
|
('Python builtin', __builtin__.__dict__),
|
|
1069
|
('Alias', self.alias_manager.alias_table),
|
|
1080
|
('Alias', self.alias_manager.alias_table),
|
|
1070
|
]
|
|
1081
|
]
|
|
1071
|
alias_ns = self.alias_manager.alias_table
|
|
1082
|
alias_ns = self.alias_manager.alias_table
|
|
1072
|
|
|
1083
|
|
|
1073
|
# initialize results to 'null'
|
|
1084
|
# initialize results to 'null'
|
|
1074
|
found = False; obj = None; ospace = None; ds = None;
|
|
1085
|
found = False; obj = None; ospace = None; ds = None;
|
|
1075
|
ismagic = False; isalias = False; parent = None
|
|
1086
|
ismagic = False; isalias = False; parent = None
|
|
1076
|
|
|
1087
|
|
|
1077
|
# We need to special-case 'print', which as of python2.6 registers as a
|
|
1088
|
# We need to special-case 'print', which as of python2.6 registers as a
|
|
1078
|
# function but should only be treated as one if print_function was
|
|
1089
|
# function but should only be treated as one if print_function was
|
|
1079
|
# loaded with a future import. In this case, just bail.
|
|
1090
|
# loaded with a future import. In this case, just bail.
|
|
1080
|
if (oname == 'print' and not (self.compile.compiler.flags &
|
|
1091
|
if (oname == 'print' and not (self.compile.compiler.flags &
|
|
1081
|
__future__.CO_FUTURE_PRINT_FUNCTION)):
|
|
1092
|
__future__.CO_FUTURE_PRINT_FUNCTION)):
|
|
1082
|
return {'found':found, 'obj':obj, 'namespace':ospace,
|
|
1093
|
return {'found':found, 'obj':obj, 'namespace':ospace,
|
|
1083
|
'ismagic':ismagic, 'isalias':isalias, 'parent':parent}
|
|
1094
|
'ismagic':ismagic, 'isalias':isalias, 'parent':parent}
|
|
1084
|
|
|
1095
|
|
|
1085
|
# Look for the given name by splitting it in parts. If the head is
|
|
1096
|
# Look for the given name by splitting it in parts. If the head is
|
|
1086
|
# found, then we look for all the remaining parts as members, and only
|
|
1097
|
# found, then we look for all the remaining parts as members, and only
|
|
1087
|
# declare success if we can find them all.
|
|
1098
|
# declare success if we can find them all.
|
|
1088
|
oname_parts = oname.split('.')
|
|
1099
|
oname_parts = oname.split('.')
|
|
1089
|
oname_head, oname_rest = oname_parts[0],oname_parts[1:]
|
|
1100
|
oname_head, oname_rest = oname_parts[0],oname_parts[1:]
|
|
1090
|
for nsname,ns in namespaces:
|
|
1101
|
for nsname,ns in namespaces:
|
|
1091
|
try:
|
|
1102
|
try:
|
|
1092
|
obj = ns[oname_head]
|
|
1103
|
obj = ns[oname_head]
|
|
1093
|
except KeyError:
|
|
1104
|
except KeyError:
|
|
1094
|
continue
|
|
1105
|
continue
|
|
1095
|
else:
|
|
1106
|
else:
|
|
1096
|
#print 'oname_rest:', oname_rest # dbg
|
|
1107
|
#print 'oname_rest:', oname_rest # dbg
|
|
1097
|
for part in oname_rest:
|
|
1108
|
for part in oname_rest:
|
|
1098
|
try:
|
|
1109
|
try:
|
|
1099
|
parent = obj
|
|
1110
|
parent = obj
|
|
1100
|
obj = getattr(obj,part)
|
|
1111
|
obj = getattr(obj,part)
|
|
1101
|
except:
|
|
1112
|
except:
|
|
1102
|
# Blanket except b/c some badly implemented objects
|
|
1113
|
# Blanket except b/c some badly implemented objects
|
|
1103
|
# allow __getattr__ to raise exceptions other than
|
|
1114
|
# allow __getattr__ to raise exceptions other than
|
|
1104
|
# AttributeError, which then crashes IPython.
|
|
1115
|
# AttributeError, which then crashes IPython.
|
|
1105
|
break
|
|
1116
|
break
|
|
1106
|
else:
|
|
1117
|
else:
|
|
1107
|
# If we finish the for loop (no break), we got all members
|
|
1118
|
# If we finish the for loop (no break), we got all members
|
|
1108
|
found = True
|
|
1119
|
found = True
|
|
1109
|
ospace = nsname
|
|
1120
|
ospace = nsname
|
|
1110
|
if ns == alias_ns:
|
|
1121
|
if ns == alias_ns:
|
|
1111
|
isalias = True
|
|
1122
|
isalias = True
|
|
1112
|
break # namespace loop
|
|
1123
|
break # namespace loop
|
|
1113
|
|
|
1124
|
|
|
1114
|
# Try to see if it's magic
|
|
1125
|
# Try to see if it's magic
|
|
1115
|
if not found:
|
|
1126
|
if not found:
|
|
1116
|
if oname.startswith(ESC_MAGIC):
|
|
1127
|
if oname.startswith(ESC_MAGIC):
|
|
1117
|
oname = oname[1:]
|
|
1128
|
oname = oname[1:]
|
|
1118
|
obj = getattr(self,'magic_'+oname,None)
|
|
1129
|
obj = getattr(self,'magic_'+oname,None)
|
|
1119
|
if obj is not None:
|
|
1130
|
if obj is not None:
|
|
1120
|
found = True
|
|
1131
|
found = True
|
|
1121
|
ospace = 'IPython internal'
|
|
1132
|
ospace = 'IPython internal'
|
|
1122
|
ismagic = True
|
|
1133
|
ismagic = True
|
|
1123
|
|
|
1134
|
|
|
1124
|
# Last try: special-case some literals like '', [], {}, etc:
|
|
1135
|
# Last try: special-case some literals like '', [], {}, etc:
|
|
1125
|
if not found and oname_head in ["''",'""','[]','{}','()']:
|
|
1136
|
if not found and oname_head in ["''",'""','[]','{}','()']:
|
|
1126
|
obj = eval(oname_head)
|
|
1137
|
obj = eval(oname_head)
|
|
1127
|
found = True
|
|
1138
|
found = True
|
|
1128
|
ospace = 'Interactive'
|
|
1139
|
ospace = 'Interactive'
|
|
1129
|
|
|
1140
|
|
|
1130
|
return {'found':found, 'obj':obj, 'namespace':ospace,
|
|
1141
|
return {'found':found, 'obj':obj, 'namespace':ospace,
|
|
1131
|
'ismagic':ismagic, 'isalias':isalias, 'parent':parent}
|
|
1142
|
'ismagic':ismagic, 'isalias':isalias, 'parent':parent}
|
|
1132
|
|
|
1143
|
|
|
1133
|
def _ofind_property(self, oname, info):
|
|
1144
|
def _ofind_property(self, oname, info):
|
|
1134
|
"""Second part of object finding, to look for property details."""
|
|
1145
|
"""Second part of object finding, to look for property details."""
|
|
1135
|
if info.found:
|
|
1146
|
if info.found:
|
|
1136
|
# Get the docstring of the class property if it exists.
|
|
1147
|
# Get the docstring of the class property if it exists.
|
|
1137
|
path = oname.split('.')
|
|
1148
|
path = oname.split('.')
|
|
1138
|
root = '.'.join(path[:-1])
|
|
1149
|
root = '.'.join(path[:-1])
|
|
1139
|
if info.parent is not None:
|
|
1150
|
if info.parent is not None:
|
|
1140
|
try:
|
|
1151
|
try:
|
|
1141
|
target = getattr(info.parent, '__class__')
|
|
1152
|
target = getattr(info.parent, '__class__')
|
|
1142
|
# The object belongs to a class instance.
|
|
1153
|
# The object belongs to a class instance.
|
|
1143
|
try:
|
|
1154
|
try:
|
|
1144
|
target = getattr(target, path[-1])
|
|
1155
|
target = getattr(target, path[-1])
|
|
1145
|
# The class defines the object.
|
|
1156
|
# The class defines the object.
|
|
1146
|
if isinstance(target, property):
|
|
1157
|
if isinstance(target, property):
|
|
1147
|
oname = root + '.__class__.' + path[-1]
|
|
1158
|
oname = root + '.__class__.' + path[-1]
|
|
1148
|
info = Struct(self._ofind(oname))
|
|
1159
|
info = Struct(self._ofind(oname))
|
|
1149
|
except AttributeError: pass
|
|
1160
|
except AttributeError: pass
|
|
1150
|
except AttributeError: pass
|
|
1161
|
except AttributeError: pass
|
|
1151
|
|
|
1162
|
|
|
1152
|
# We return either the new info or the unmodified input if the object
|
|
1163
|
# We return either the new info or the unmodified input if the object
|
|
1153
|
# hadn't been found
|
|
1164
|
# hadn't been found
|
|
1154
|
return info
|
|
1165
|
return info
|
|
1155
|
|
|
1166
|
|
|
1156
|
def _object_find(self, oname, namespaces=None):
|
|
1167
|
def _object_find(self, oname, namespaces=None):
|
|
1157
|
"""Find an object and return a struct with info about it."""
|
|
1168
|
"""Find an object and return a struct with info about it."""
|
|
1158
|
inf = Struct(self._ofind(oname, namespaces))
|
|
1169
|
inf = Struct(self._ofind(oname, namespaces))
|
|
1159
|
return Struct(self._ofind_property(oname, inf))
|
|
1170
|
return Struct(self._ofind_property(oname, inf))
|
|
1160
|
|
|
1171
|
|
|
1161
|
def _inspect(self, meth, oname, namespaces=None, **kw):
|
|
1172
|
def _inspect(self, meth, oname, namespaces=None, **kw):
|
|
1162
|
"""Generic interface to the inspector system.
|
|
1173
|
"""Generic interface to the inspector system.
|
|
1163
|
|
|
1174
|
|
|
1164
|
This function is meant to be called by pdef, pdoc & friends."""
|
|
1175
|
This function is meant to be called by pdef, pdoc & friends."""
|
|
1165
|
info = self._object_find(oname)
|
|
1176
|
info = self._object_find(oname)
|
|
1166
|
if info.found:
|
|
1177
|
if info.found:
|
|
1167
|
pmethod = getattr(self.inspector, meth)
|
|
1178
|
pmethod = getattr(self.inspector, meth)
|
|
1168
|
formatter = format_screen if info.ismagic else None
|
|
1179
|
formatter = format_screen if info.ismagic else None
|
|
1169
|
if meth == 'pdoc':
|
|
1180
|
if meth == 'pdoc':
|
|
1170
|
pmethod(info.obj, oname, formatter)
|
|
1181
|
pmethod(info.obj, oname, formatter)
|
|
1171
|
elif meth == 'pinfo':
|
|
1182
|
elif meth == 'pinfo':
|
|
1172
|
pmethod(info.obj, oname, formatter, info, **kw)
|
|
1183
|
pmethod(info.obj, oname, formatter, info, **kw)
|
|
1173
|
else:
|
|
1184
|
else:
|
|
1174
|
pmethod(info.obj, oname)
|
|
1185
|
pmethod(info.obj, oname)
|
|
1175
|
else:
|
|
1186
|
else:
|
|
1176
|
print 'Object `%s` not found.' % oname
|
|
1187
|
print 'Object `%s` not found.' % oname
|
|
1177
|
return 'not found' # so callers can take other action
|
|
1188
|
return 'not found' # so callers can take other action
|
|
1178
|
|
|
1189
|
|
|
1179
|
def object_inspect(self, oname):
|
|
1190
|
def object_inspect(self, oname):
|
|
1180
|
info = self._object_find(oname)
|
|
1191
|
info = self._object_find(oname)
|
|
1181
|
if info.found:
|
|
1192
|
if info.found:
|
|
1182
|
return self.inspector.info(info.obj, info=info)
|
|
1193
|
return self.inspector.info(info.obj, info=info)
|
|
1183
|
else:
|
|
1194
|
else:
|
|
1184
|
return oinspect.mk_object_info({'found' : False})
|
|
1195
|
return oinspect.mk_object_info({'found' : False})
|
|
1185
|
|
|
1196
|
|
|
1186
|
#-------------------------------------------------------------------------
|
|
1197
|
#-------------------------------------------------------------------------
|
|
1187
|
# Things related to history management
|
|
1198
|
# Things related to history management
|
|
1188
|
#-------------------------------------------------------------------------
|
|
1199
|
#-------------------------------------------------------------------------
|
|
1189
|
|
|
1200
|
|
|
1190
|
def init_history(self):
|
|
1201
|
def init_history(self):
|
|
1191
|
# List of input with multi-line handling.
|
|
1202
|
# List of input with multi-line handling.
|
|
1192
|
self.input_hist = InputList()
|
|
1203
|
self.input_hist = InputList()
|
|
1193
|
# This one will hold the 'raw' input history, without any
|
|
1204
|
# This one will hold the 'raw' input history, without any
|
|
1194
|
# pre-processing. This will allow users to retrieve the input just as
|
|
1205
|
# pre-processing. This will allow users to retrieve the input just as
|
|
1195
|
# it was exactly typed in by the user, with %hist -r.
|
|
1206
|
# it was exactly typed in by the user, with %hist -r.
|
|
1196
|
self.input_hist_raw = InputList()
|
|
1207
|
self.input_hist_raw = InputList()
|
|
1197
|
|
|
1208
|
|
|
1198
|
# list of visited directories
|
|
1209
|
# list of visited directories
|
|
1199
|
try:
|
|
1210
|
try:
|
|
1200
|
self.dir_hist = [os.getcwd()]
|
|
1211
|
self.dir_hist = [os.getcwd()]
|
|
1201
|
except OSError:
|
|
1212
|
except OSError:
|
|
1202
|
self.dir_hist = []
|
|
1213
|
self.dir_hist = []
|
|
1203
|
|
|
1214
|
|
|
1204
|
# dict of output history
|
|
1215
|
# dict of output history
|
|
1205
|
self.output_hist = {}
|
|
1216
|
self.output_hist = {}
|
|
1206
|
|
|
1217
|
|
|
1207
|
# Now the history file
|
|
1218
|
# Now the history file
|
|
1208
|
if self.profile:
|
|
1219
|
if self.profile:
|
|
1209
|
histfname = 'history-%s' % self.profile
|
|
1220
|
histfname = 'history-%s' % self.profile
|
|
1210
|
else:
|
|
1221
|
else:
|
|
1211
|
histfname = 'history'
|
|
1222
|
histfname = 'history'
|
|
1212
|
self.histfile = os.path.join(self.ipython_dir, histfname)
|
|
1223
|
self.histfile = os.path.join(self.ipython_dir, histfname)
|
|
1213
|
|
|
1224
|
|
|
1214
|
# Fill the history zero entry, user counter starts at 1
|
|
1225
|
# Fill the history zero entry, user counter starts at 1
|
|
1215
|
self.input_hist.append('\n')
|
|
1226
|
self.input_hist.append('\n')
|
|
1216
|
self.input_hist_raw.append('\n')
|
|
1227
|
self.input_hist_raw.append('\n')
|
|
1217
|
|
|
1228
|
|
|
1218
|
def init_shadow_hist(self):
|
|
1229
|
def init_shadow_hist(self):
|
|
1219
|
try:
|
|
1230
|
try:
|
|
1220
|
self.db = pickleshare.PickleShareDB(self.ipython_dir + "/db")
|
|
1231
|
self.db = pickleshare.PickleShareDB(self.ipython_dir + "/db")
|
|
1221
|
except exceptions.UnicodeDecodeError:
|
|
1232
|
except exceptions.UnicodeDecodeError:
|
|
1222
|
print "Your ipython_dir can't be decoded to unicode!"
|
|
1233
|
print "Your ipython_dir can't be decoded to unicode!"
|
|
1223
|
print "Please set HOME environment variable to something that"
|
|
1234
|
print "Please set HOME environment variable to something that"
|
|
1224
|
print r"only has ASCII characters, e.g. c:\home"
|
|
1235
|
print r"only has ASCII characters, e.g. c:\home"
|
|
1225
|
print "Now it is", self.ipython_dir
|
|
1236
|
print "Now it is", self.ipython_dir
|
|
1226
|
sys.exit()
|
|
1237
|
sys.exit()
|
|
1227
|
self.shadowhist = ipcorehist.ShadowHist(self.db)
|
|
1238
|
self.shadowhist = ipcorehist.ShadowHist(self.db)
|
|
1228
|
|
|
1239
|
|
|
1229
|
def savehist(self):
|
|
1240
|
def savehist(self):
|
|
1230
|
"""Save input history to a file (via readline library)."""
|
|
1241
|
"""Save input history to a file (via readline library)."""
|
|
1231
|
|
|
1242
|
|
|
1232
|
try:
|
|
1243
|
try:
|
|
1233
|
self.readline.write_history_file(self.histfile)
|
|
1244
|
self.readline.write_history_file(self.histfile)
|
|
1234
|
except:
|
|
1245
|
except:
|
|
1235
|
print 'Unable to save IPython command history to file: ' + \
|
|
1246
|
print 'Unable to save IPython command history to file: ' + \
|
|
1236
|
`self.histfile`
|
|
1247
|
`self.histfile`
|
|
1237
|
|
|
1248
|
|
|
1238
|
def reloadhist(self):
|
|
1249
|
def reloadhist(self):
|
|
1239
|
"""Reload the input history from disk file."""
|
|
1250
|
"""Reload the input history from disk file."""
|
|
1240
|
|
|
1251
|
|
|
1241
|
try:
|
|
1252
|
try:
|
|
1242
|
self.readline.clear_history()
|
|
1253
|
self.readline.clear_history()
|
|
1243
|
self.readline.read_history_file(self.shell.histfile)
|
|
1254
|
self.readline.read_history_file(self.shell.histfile)
|
|
1244
|
except AttributeError:
|
|
1255
|
except AttributeError:
|
|
1245
|
pass
|
|
1256
|
pass
|
|
1246
|
|
|
1257
|
|
|
1247
|
def history_saving_wrapper(self, func):
|
|
1258
|
def history_saving_wrapper(self, func):
|
|
1248
|
""" Wrap func for readline history saving
|
|
1259
|
""" Wrap func for readline history saving
|
|
1249
|
|
|
1260
|
|
|
1250
|
Convert func into callable that saves & restores
|
|
1261
|
Convert func into callable that saves & restores
|
|
1251
|
history around the call """
|
|
1262
|
history around the call """
|
|
1252
|
|
|
1263
|
|
|
1253
|
if self.has_readline:
|
|
1264
|
if self.has_readline:
|
|
1254
|
from IPython.utils import rlineimpl as readline
|
|
1265
|
from IPython.utils import rlineimpl as readline
|
|
1255
|
else:
|
|
1266
|
else:
|
|
1256
|
return func
|
|
1267
|
return func
|
|
1257
|
|
|
1268
|
|
|
1258
|
def wrapper():
|
|
1269
|
def wrapper():
|
|
1259
|
self.savehist()
|
|
1270
|
self.savehist()
|
|
1260
|
try:
|
|
1271
|
try:
|
|
1261
|
func()
|
|
1272
|
func()
|
|
1262
|
finally:
|
|
1273
|
finally:
|
|
1263
|
readline.read_history_file(self.histfile)
|
|
1274
|
readline.read_history_file(self.histfile)
|
|
1264
|
return wrapper
|
|
1275
|
return wrapper
|
|
1265
|
|
|
1276
|
|
|
1266
|
def get_history(self, index=None, raw=False, output=True):
|
|
1277
|
def get_history(self, index=None, raw=False, output=True):
|
|
1267
|
"""Get the history list.
|
|
1278
|
"""Get the history list.
|
|
1268
|
|
|
1279
|
|
|
1269
|
Get the input and output history.
|
|
1280
|
Get the input and output history.
|
|
1270
|
|
|
1281
|
|
|
1271
|
Parameters
|
|
1282
|
Parameters
|
|
1272
|
----------
|
|
1283
|
----------
|
|
1273
|
index : n or (n1, n2) or None
|
|
1284
|
index : n or (n1, n2) or None
|
|
1274
|
If n, then the last entries. If a tuple, then all in
|
|
1285
|
If n, then the last entries. If a tuple, then all in
|
|
1275
|
range(n1, n2). If None, then all entries. Raises IndexError if
|
|
1286
|
range(n1, n2). If None, then all entries. Raises IndexError if
|
|
1276
|
the format of index is incorrect.
|
|
1287
|
the format of index is incorrect.
|
|
1277
|
raw : bool
|
|
1288
|
raw : bool
|
|
1278
|
If True, return the raw input.
|
|
1289
|
If True, return the raw input.
|
|
1279
|
output : bool
|
|
1290
|
output : bool
|
|
1280
|
If True, then return the output as well.
|
|
1291
|
If True, then return the output as well.
|
|
1281
|
|
|
1292
|
|
|
1282
|
Returns
|
|
1293
|
Returns
|
|
1283
|
-------
|
|
1294
|
-------
|
|
1284
|
If output is True, then return a dict of tuples, keyed by the prompt
|
|
1295
|
If output is True, then return a dict of tuples, keyed by the prompt
|
|
1285
|
numbers and with values of (input, output). If output is False, then
|
|
1296
|
numbers and with values of (input, output). If output is False, then
|
|
1286
|
a dict, keyed by the prompt number with the values of input. Raises
|
|
1297
|
a dict, keyed by the prompt number with the values of input. Raises
|
|
1287
|
IndexError if no history is found.
|
|
1298
|
IndexError if no history is found.
|
|
1288
|
"""
|
|
1299
|
"""
|
|
1289
|
if raw:
|
|
1300
|
if raw:
|
|
1290
|
input_hist = self.input_hist_raw
|
|
1301
|
input_hist = self.input_hist_raw
|
|
1291
|
else:
|
|
1302
|
else:
|
|
1292
|
input_hist = self.input_hist
|
|
1303
|
input_hist = self.input_hist
|
|
1293
|
if output:
|
|
1304
|
if output:
|
|
1294
|
output_hist = self.user_ns['Out']
|
|
1305
|
output_hist = self.user_ns['Out']
|
|
1295
|
n = len(input_hist)
|
|
1306
|
n = len(input_hist)
|
|
1296
|
if index is None:
|
|
1307
|
if index is None:
|
|
1297
|
start=0; stop=n
|
|
1308
|
start=0; stop=n
|
|
1298
|
elif isinstance(index, int):
|
|
1309
|
elif isinstance(index, int):
|
|
1299
|
start=n-index; stop=n
|
|
1310
|
start=n-index; stop=n
|
|
1300
|
elif isinstance(index, tuple) and len(index) == 2:
|
|
1311
|
elif isinstance(index, tuple) and len(index) == 2:
|
|
1301
|
start=index[0]; stop=index[1]
|
|
1312
|
start=index[0]; stop=index[1]
|
|
1302
|
else:
|
|
1313
|
else:
|
|
1303
|
raise IndexError('Not a valid index for the input history: %r'
|
|
1314
|
raise IndexError('Not a valid index for the input history: %r'
|
|
1304
|
% index)
|
|
1315
|
% index)
|
|
1305
|
hist = {}
|
|
1316
|
hist = {}
|
|
1306
|
for i in range(start, stop):
|
|
1317
|
for i in range(start, stop):
|
|
1307
|
if output:
|
|
1318
|
if output:
|
|
1308
|
hist[i] = (input_hist[i], output_hist.get(i))
|
|
1319
|
hist[i] = (input_hist[i], output_hist.get(i))
|
|
1309
|
else:
|
|
1320
|
else:
|
|
1310
|
hist[i] = input_hist[i]
|
|
1321
|
hist[i] = input_hist[i]
|
|
1311
|
if len(hist)==0:
|
|
1322
|
if len(hist)==0:
|
|
1312
|
raise IndexError('No history for range of indices: %r' % index)
|
|
1323
|
raise IndexError('No history for range of indices: %r' % index)
|
|
1313
|
return hist
|
|
1324
|
return hist
|
|
1314
|
|
|
1325
|
|
|
1315
|
#-------------------------------------------------------------------------
|
|
1326
|
#-------------------------------------------------------------------------
|
|
1316
|
# Things related to exception handling and tracebacks (not debugging)
|
|
1327
|
# Things related to exception handling and tracebacks (not debugging)
|
|
1317
|
#-------------------------------------------------------------------------
|
|
1328
|
#-------------------------------------------------------------------------
|
|
1318
|
|
|
1329
|
|
|
1319
|
def init_traceback_handlers(self, custom_exceptions):
|
|
1330
|
def init_traceback_handlers(self, custom_exceptions):
|
|
1320
|
# Syntax error handler.
|
|
1331
|
# Syntax error handler.
|
|
1321
|
self.SyntaxTB = ultratb.SyntaxTB(color_scheme='NoColor')
|
|
1332
|
self.SyntaxTB = ultratb.SyntaxTB(color_scheme='NoColor')
|
|
1322
|
|
|
1333
|
|
|
1323
|
# The interactive one is initialized with an offset, meaning we always
|
|
1334
|
# The interactive one is initialized with an offset, meaning we always
|
|
1324
|
# want to remove the topmost item in the traceback, which is our own
|
|
1335
|
# want to remove the topmost item in the traceback, which is our own
|
|
1325
|
# internal code. Valid modes: ['Plain','Context','Verbose']
|
|
1336
|
# internal code. Valid modes: ['Plain','Context','Verbose']
|
|
1326
|
self.InteractiveTB = ultratb.AutoFormattedTB(mode = 'Plain',
|
|
1337
|
self.InteractiveTB = ultratb.AutoFormattedTB(mode = 'Plain',
|
|
1327
|
color_scheme='NoColor',
|
|
1338
|
color_scheme='NoColor',
|
|
1328
|
tb_offset = 1)
|
|
1339
|
tb_offset = 1)
|
|
1329
|
|
|
1340
|
|
|
1330
|
# The instance will store a pointer to the system-wide exception hook,
|
|
1341
|
# The instance will store a pointer to the system-wide exception hook,
|
|
1331
|
# so that runtime code (such as magics) can access it. This is because
|
|
1342
|
# so that runtime code (such as magics) can access it. This is because
|
|
1332
|
# during the read-eval loop, it may get temporarily overwritten.
|
|
1343
|
# during the read-eval loop, it may get temporarily overwritten.
|
|
1333
|
self.sys_excepthook = sys.excepthook
|
|
1344
|
self.sys_excepthook = sys.excepthook
|
|
1334
|
|
|
1345
|
|
|
1335
|
# and add any custom exception handlers the user may have specified
|
|
1346
|
# and add any custom exception handlers the user may have specified
|
|
1336
|
self.set_custom_exc(*custom_exceptions)
|
|
1347
|
self.set_custom_exc(*custom_exceptions)
|
|
1337
|
|
|
1348
|
|
|
1338
|
# Set the exception mode
|
|
1349
|
# Set the exception mode
|
|
1339
|
self.InteractiveTB.set_mode(mode=self.xmode)
|
|
1350
|
self.InteractiveTB.set_mode(mode=self.xmode)
|
|
1340
|
|
|
1351
|
|
|
1341
|
def set_custom_exc(self, exc_tuple, handler):
|
|
1352
|
def set_custom_exc(self, exc_tuple, handler):
|
|
1342
|
"""set_custom_exc(exc_tuple,handler)
|
|
1353
|
"""set_custom_exc(exc_tuple,handler)
|
|
1343
|
|
|
1354
|
|
|
1344
|
Set a custom exception handler, which will be called if any of the
|
|
1355
|
Set a custom exception handler, which will be called if any of the
|
|
1345
|
exceptions in exc_tuple occur in the mainloop (specifically, in the
|
|
1356
|
exceptions in exc_tuple occur in the mainloop (specifically, in the
|
|
1346
|
runcode() method.
|
|
1357
|
runcode() method.
|
|
1347
|
|
|
1358
|
|
|
1348
|
Inputs:
|
|
1359
|
Inputs:
|
|
1349
|
|
|
1360
|
|
|
1350
|
- exc_tuple: a *tuple* of valid exceptions to call the defined
|
|
1361
|
- exc_tuple: a *tuple* of valid exceptions to call the defined
|
|
1351
|
handler for. It is very important that you use a tuple, and NOT A
|
|
1362
|
handler for. It is very important that you use a tuple, and NOT A
|
|
1352
|
LIST here, because of the way Python's except statement works. If
|
|
1363
|
LIST here, because of the way Python's except statement works. If
|
|
1353
|
you only want to trap a single exception, use a singleton tuple:
|
|
1364
|
you only want to trap a single exception, use a singleton tuple:
|
|
1354
|
|
|
1365
|
|
|
1355
|
exc_tuple == (MyCustomException,)
|
|
1366
|
exc_tuple == (MyCustomException,)
|
|
1356
|
|
|
1367
|
|
|
1357
|
- handler: this must be defined as a function with the following
|
|
1368
|
- handler: this must be defined as a function with the following
|
|
1358
|
basic interface::
|
|
1369
|
basic interface::
|
|
1359
|
|
|
1370
|
|
|
1360
|
def my_handler(self, etype, value, tb, tb_offset=None)
|
|
1371
|
def my_handler(self, etype, value, tb, tb_offset=None)
|
|
1361
|
...
|
|
1372
|
...
|
|
1362
|
# The return value must be
|
|
1373
|
# The return value must be
|
|
1363
|
return structured_traceback
|
|
1374
|
return structured_traceback
|
|
1364
|
|
|
1375
|
|
|
1365
|
This will be made into an instance method (via new.instancemethod)
|
|
1376
|
This will be made into an instance method (via new.instancemethod)
|
|
1366
|
of IPython itself, and it will be called if any of the exceptions
|
|
1377
|
of IPython itself, and it will be called if any of the exceptions
|
|
1367
|
listed in the exc_tuple are caught. If the handler is None, an
|
|
1378
|
listed in the exc_tuple are caught. If the handler is None, an
|
|
1368
|
internal basic one is used, which just prints basic info.
|
|
1379
|
internal basic one is used, which just prints basic info.
|
|
1369
|
|
|
1380
|
|
|
1370
|
WARNING: by putting in your own exception handler into IPython's main
|
|
1381
|
WARNING: by putting in your own exception handler into IPython's main
|
|
1371
|
execution loop, you run a very good chance of nasty crashes. This
|
|
1382
|
execution loop, you run a very good chance of nasty crashes. This
|
|
1372
|
facility should only be used if you really know what you are doing."""
|
|
1383
|
facility should only be used if you really know what you are doing."""
|
|
1373
|
|
|
1384
|
|
|
1374
|
assert type(exc_tuple)==type(()) , \
|
|
1385
|
assert type(exc_tuple)==type(()) , \
|
|
1375
|
"The custom exceptions must be given AS A TUPLE."
|
|
1386
|
"The custom exceptions must be given AS A TUPLE."
|
|
1376
|
|
|
1387
|
|
|
1377
|
def dummy_handler(self,etype,value,tb):
|
|
1388
|
def dummy_handler(self,etype,value,tb):
|
|
1378
|
print '*** Simple custom exception handler ***'
|
|
1389
|
print '*** Simple custom exception handler ***'
|
|
1379
|
print 'Exception type :',etype
|
|
1390
|
print 'Exception type :',etype
|
|
1380
|
print 'Exception value:',value
|
|
1391
|
print 'Exception value:',value
|
|
1381
|
print 'Traceback :',tb
|
|
1392
|
print 'Traceback :',tb
|
|
1382
|
print 'Source code :','\n'.join(self.buffer)
|
|
1393
|
print 'Source code :','\n'.join(self.buffer)
|
|
1383
|
|
|
1394
|
|
|
1384
|
if handler is None: handler = dummy_handler
|
|
1395
|
if handler is None: handler = dummy_handler
|
|
1385
|
|
|
1396
|
|
|
1386
|
self.CustomTB = new.instancemethod(handler,self,self.__class__)
|
|
1397
|
self.CustomTB = new.instancemethod(handler,self,self.__class__)
|
|
1387
|
self.custom_exceptions = exc_tuple
|
|
1398
|
self.custom_exceptions = exc_tuple
|
|
1388
|
|
|
1399
|
|
|
1389
|
def excepthook(self, etype, value, tb):
|
|
1400
|
def excepthook(self, etype, value, tb):
|
|
1390
|
"""One more defense for GUI apps that call sys.excepthook.
|
|
1401
|
"""One more defense for GUI apps that call sys.excepthook.
|
|
1391
|
|
|
1402
|
|
|
1392
|
GUI frameworks like wxPython trap exceptions and call
|
|
1403
|
GUI frameworks like wxPython trap exceptions and call
|
|
1393
|
sys.excepthook themselves. I guess this is a feature that
|
|
1404
|
sys.excepthook themselves. I guess this is a feature that
|
|
1394
|
enables them to keep running after exceptions that would
|
|
1405
|
enables them to keep running after exceptions that would
|
|
1395
|
otherwise kill their mainloop. This is a bother for IPython
|
|
1406
|
otherwise kill their mainloop. This is a bother for IPython
|
|
1396
|
which excepts to catch all of the program exceptions with a try:
|
|
1407
|
which excepts to catch all of the program exceptions with a try:
|
|
1397
|
except: statement.
|
|
1408
|
except: statement.
|
|
1398
|
|
|
1409
|
|
|
1399
|
Normally, IPython sets sys.excepthook to a CrashHandler instance, so if
|
|
1410
|
Normally, IPython sets sys.excepthook to a CrashHandler instance, so if
|
|
1400
|
any app directly invokes sys.excepthook, it will look to the user like
|
|
1411
|
any app directly invokes sys.excepthook, it will look to the user like
|
|
1401
|
IPython crashed. In order to work around this, we can disable the
|
|
1412
|
IPython crashed. In order to work around this, we can disable the
|
|
1402
|
CrashHandler and replace it with this excepthook instead, which prints a
|
|
1413
|
CrashHandler and replace it with this excepthook instead, which prints a
|
|
1403
|
regular traceback using our InteractiveTB. In this fashion, apps which
|
|
1414
|
regular traceback using our InteractiveTB. In this fashion, apps which
|
|
1404
|
call sys.excepthook will generate a regular-looking exception from
|
|
1415
|
call sys.excepthook will generate a regular-looking exception from
|
|
1405
|
IPython, and the CrashHandler will only be triggered by real IPython
|
|
1416
|
IPython, and the CrashHandler will only be triggered by real IPython
|
|
1406
|
crashes.
|
|
1417
|
crashes.
|
|
1407
|
|
|
1418
|
|
|
1408
|
This hook should be used sparingly, only in places which are not likely
|
|
1419
|
This hook should be used sparingly, only in places which are not likely
|
|
1409
|
to be true IPython errors.
|
|
1420
|
to be true IPython errors.
|
|
1410
|
"""
|
|
1421
|
"""
|
|
1411
|
self.showtraceback((etype,value,tb),tb_offset=0)
|
|
1422
|
self.showtraceback((etype,value,tb),tb_offset=0)
|
|
1412
|
|
|
1423
|
|
|
1413
|
def showtraceback(self,exc_tuple = None,filename=None,tb_offset=None,
|
|
1424
|
def showtraceback(self,exc_tuple = None,filename=None,tb_offset=None,
|
|
1414
|
exception_only=False):
|
|
1425
|
exception_only=False):
|
|
1415
|
"""Display the exception that just occurred.
|
|
1426
|
"""Display the exception that just occurred.
|
|
1416
|
|
|
1427
|
|
|
1417
|
If nothing is known about the exception, this is the method which
|
|
1428
|
If nothing is known about the exception, this is the method which
|
|
1418
|
should be used throughout the code for presenting user tracebacks,
|
|
1429
|
should be used throughout the code for presenting user tracebacks,
|
|
1419
|
rather than directly invoking the InteractiveTB object.
|
|
1430
|
rather than directly invoking the InteractiveTB object.
|
|
1420
|
|
|
1431
|
|
|
1421
|
A specific showsyntaxerror() also exists, but this method can take
|
|
1432
|
A specific showsyntaxerror() also exists, but this method can take
|
|
1422
|
care of calling it if needed, so unless you are explicitly catching a
|
|
1433
|
care of calling it if needed, so unless you are explicitly catching a
|
|
1423
|
SyntaxError exception, don't try to analyze the stack manually and
|
|
1434
|
SyntaxError exception, don't try to analyze the stack manually and
|
|
1424
|
simply call this method."""
|
|
1435
|
simply call this method."""
|
|
1425
|
|
|
1436
|
|
|
1426
|
try:
|
|
1437
|
try:
|
|
1427
|
if exc_tuple is None:
|
|
1438
|
if exc_tuple is None:
|
|
1428
|
etype, value, tb = sys.exc_info()
|
|
1439
|
etype, value, tb = sys.exc_info()
|
|
1429
|
else:
|
|
1440
|
else:
|
|
1430
|
etype, value, tb = exc_tuple
|
|
1441
|
etype, value, tb = exc_tuple
|
|
1431
|
|
|
1442
|
|
|
1432
|
if etype is None:
|
|
1443
|
if etype is None:
|
|
1433
|
if hasattr(sys, 'last_type'):
|
|
1444
|
if hasattr(sys, 'last_type'):
|
|
1434
|
etype, value, tb = sys.last_type, sys.last_value, \
|
|
1445
|
etype, value, tb = sys.last_type, sys.last_value, \
|
|
1435
|
sys.last_traceback
|
|
1446
|
sys.last_traceback
|
|
1436
|
else:
|
|
1447
|
else:
|
|
1437
|
self.write_err('No traceback available to show.\n')
|
|
1448
|
self.write_err('No traceback available to show.\n')
|
|
1438
|
return
|
|
1449
|
return
|
|
1439
|
|
|
1450
|
|
|
1440
|
if etype is SyntaxError:
|
|
1451
|
if etype is SyntaxError:
|
|
1441
|
# Though this won't be called by syntax errors in the input
|
|
1452
|
# Though this won't be called by syntax errors in the input
|
|
1442
|
# line, there may be SyntaxError cases whith imported code.
|
|
1453
|
# line, there may be SyntaxError cases whith imported code.
|
|
1443
|
self.showsyntaxerror(filename)
|
|
1454
|
self.showsyntaxerror(filename)
|
|
1444
|
elif etype is UsageError:
|
|
1455
|
elif etype is UsageError:
|
|
1445
|
print "UsageError:", value
|
|
1456
|
print "UsageError:", value
|
|
1446
|
else:
|
|
1457
|
else:
|
|
1447
|
# WARNING: these variables are somewhat deprecated and not
|
|
1458
|
# WARNING: these variables are somewhat deprecated and not
|
|
1448
|
# necessarily safe to use in a threaded environment, but tools
|
|
1459
|
# necessarily safe to use in a threaded environment, but tools
|
|
1449
|
# like pdb depend on their existence, so let's set them. If we
|
|
1460
|
# like pdb depend on their existence, so let's set them. If we
|
|
1450
|
# find problems in the field, we'll need to revisit their use.
|
|
1461
|
# find problems in the field, we'll need to revisit their use.
|
|
1451
|
sys.last_type = etype
|
|
1462
|
sys.last_type = etype
|
|
1452
|
sys.last_value = value
|
|
1463
|
sys.last_value = value
|
|
1453
|
sys.last_traceback = tb
|
|
1464
|
sys.last_traceback = tb
|
|
1454
|
|
|
1465
|
|
|
1455
|
if etype in self.custom_exceptions:
|
|
1466
|
if etype in self.custom_exceptions:
|
|
1456
|
# FIXME: Old custom traceback objects may just return a
|
|
1467
|
# FIXME: Old custom traceback objects may just return a
|
|
1457
|
# string, in that case we just put it into a list
|
|
1468
|
# string, in that case we just put it into a list
|
|
1458
|
stb = self.CustomTB(etype, value, tb, tb_offset)
|
|
1469
|
stb = self.CustomTB(etype, value, tb, tb_offset)
|
|
1459
|
if isinstance(ctb, basestring):
|
|
1470
|
if isinstance(ctb, basestring):
|
|
1460
|
stb = [stb]
|
|
1471
|
stb = [stb]
|
|
1461
|
else:
|
|
1472
|
else:
|
|
1462
|
if exception_only:
|
|
1473
|
if exception_only:
|
|
1463
|
stb = ['An exception has occurred, use %tb to see '
|
|
1474
|
stb = ['An exception has occurred, use %tb to see '
|
|
1464
|
'the full traceback.\n']
|
|
1475
|
'the full traceback.\n']
|
|
1465
|
stb.extend(self.InteractiveTB.get_exception_only(etype,
|
|
1476
|
stb.extend(self.InteractiveTB.get_exception_only(etype,
|
|
1466
|
value))
|
|
1477
|
value))
|
|
1467
|
else:
|
|
1478
|
else:
|
|
1468
|
stb = self.InteractiveTB.structured_traceback(etype,
|
|
1479
|
stb = self.InteractiveTB.structured_traceback(etype,
|
|
1469
|
value, tb, tb_offset=tb_offset)
|
|
1480
|
value, tb, tb_offset=tb_offset)
|
|
1470
|
# FIXME: the pdb calling should be done by us, not by
|
|
1481
|
# FIXME: the pdb calling should be done by us, not by
|
|
1471
|
# the code computing the traceback.
|
|
1482
|
# the code computing the traceback.
|
|
1472
|
if self.InteractiveTB.call_pdb:
|
|
1483
|
if self.InteractiveTB.call_pdb:
|
|
1473
|
# pdb mucks up readline, fix it back
|
|
1484
|
# pdb mucks up readline, fix it back
|
|
1474
|
self.set_readline_completer()
|
|
1485
|
self.set_readline_completer()
|
|
1475
|
|
|
1486
|
|
|
1476
|
# Actually show the traceback
|
|
1487
|
# Actually show the traceback
|
|
1477
|
self._showtraceback(etype, value, stb)
|
|
1488
|
self._showtraceback(etype, value, stb)
|
|
1478
|
|
|
1489
|
|
|
1479
|
except KeyboardInterrupt:
|
|
1490
|
except KeyboardInterrupt:
|
|
1480
|
self.write_err("\nKeyboardInterrupt\n")
|
|
1491
|
self.write_err("\nKeyboardInterrupt\n")
|
|
1481
|
|
|
1492
|
|
|
1482
|
def _showtraceback(self, etype, evalue, stb):
|
|
1493
|
def _showtraceback(self, etype, evalue, stb):
|
|
1483
|
"""Actually show a traceback.
|
|
1494
|
"""Actually show a traceback.
|
|
1484
|
|
|
1495
|
|
|
1485
|
Subclasses may override this method to put the traceback on a different
|
|
1496
|
Subclasses may override this method to put the traceback on a different
|
|
1486
|
place, like a side channel.
|
|
1497
|
place, like a side channel.
|
|
1487
|
"""
|
|
1498
|
"""
|
|
1488
|
print >> io.Term.cout, self.InteractiveTB.stb2text(stb)
|
|
1499
|
print >> io.Term.cout, self.InteractiveTB.stb2text(stb)
|
|
1489
|
|
|
1500
|
|
|
1490
|
def showsyntaxerror(self, filename=None):
|
|
1501
|
def showsyntaxerror(self, filename=None):
|
|
1491
|
"""Display the syntax error that just occurred.
|
|
1502
|
"""Display the syntax error that just occurred.
|
|
1492
|
|
|
1503
|
|
|
1493
|
This doesn't display a stack trace because there isn't one.
|
|
1504
|
This doesn't display a stack trace because there isn't one.
|
|
1494
|
|
|
1505
|
|
|
1495
|
If a filename is given, it is stuffed in the exception instead
|
|
1506
|
If a filename is given, it is stuffed in the exception instead
|
|
1496
|
of what was there before (because Python's parser always uses
|
|
1507
|
of what was there before (because Python's parser always uses
|
|
1497
|
"<string>" when reading from a string).
|
|
1508
|
"<string>" when reading from a string).
|
|
1498
|
"""
|
|
1509
|
"""
|
|
1499
|
etype, value, last_traceback = sys.exc_info()
|
|
1510
|
etype, value, last_traceback = sys.exc_info()
|
|
1500
|
|
|
1511
|
|
|
1501
|
# See note about these variables in showtraceback() above
|
|
1512
|
# See note about these variables in showtraceback() above
|
|
1502
|
sys.last_type = etype
|
|
1513
|
sys.last_type = etype
|
|
1503
|
sys.last_value = value
|
|
1514
|
sys.last_value = value
|
|
1504
|
sys.last_traceback = last_traceback
|
|
1515
|
sys.last_traceback = last_traceback
|
|
1505
|
|
|
1516
|
|
|
1506
|
if filename and etype is SyntaxError:
|
|
1517
|
if filename and etype is SyntaxError:
|
|
1507
|
# Work hard to stuff the correct filename in the exception
|
|
1518
|
# Work hard to stuff the correct filename in the exception
|
|
1508
|
try:
|
|
1519
|
try:
|
|
1509
|
msg, (dummy_filename, lineno, offset, line) = value
|
|
1520
|
msg, (dummy_filename, lineno, offset, line) = value
|
|
1510
|
except:
|
|
1521
|
except:
|
|
1511
|
# Not the format we expect; leave it alone
|
|
1522
|
# Not the format we expect; leave it alone
|
|
1512
|
pass
|
|
1523
|
pass
|
|
1513
|
else:
|
|
1524
|
else:
|
|
1514
|
# Stuff in the right filename
|
|
1525
|
# Stuff in the right filename
|
|
1515
|
try:
|
|
1526
|
try:
|
|
1516
|
# Assume SyntaxError is a class exception
|
|
1527
|
# Assume SyntaxError is a class exception
|
|
1517
|
value = SyntaxError(msg, (filename, lineno, offset, line))
|
|
1528
|
value = SyntaxError(msg, (filename, lineno, offset, line))
|
|
1518
|
except:
|
|
1529
|
except:
|
|
1519
|
# If that failed, assume SyntaxError is a string
|
|
1530
|
# If that failed, assume SyntaxError is a string
|
|
1520
|
value = msg, (filename, lineno, offset, line)
|
|
1531
|
value = msg, (filename, lineno, offset, line)
|
|
1521
|
stb = self.SyntaxTB.structured_traceback(etype, value, [])
|
|
1532
|
stb = self.SyntaxTB.structured_traceback(etype, value, [])
|
|
1522
|
self._showtraceback(etype, value, stb)
|
|
1533
|
self._showtraceback(etype, value, stb)
|
|
1523
|
|
|
1534
|
|
|
1524
|
#-------------------------------------------------------------------------
|
|
1535
|
#-------------------------------------------------------------------------
|
|
1525
|
# Things related to readline
|
|
1536
|
# Things related to readline
|
|
1526
|
#-------------------------------------------------------------------------
|
|
1537
|
#-------------------------------------------------------------------------
|
|
1527
|
|
|
1538
|
|
|
1528
|
def init_readline(self):
|
|
1539
|
def init_readline(self):
|
|
1529
|
"""Command history completion/saving/reloading."""
|
|
1540
|
"""Command history completion/saving/reloading."""
|
|
1530
|
|
|
1541
|
|
|
1531
|
if self.readline_use:
|
|
1542
|
if self.readline_use:
|
|
1532
|
import IPython.utils.rlineimpl as readline
|
|
1543
|
import IPython.utils.rlineimpl as readline
|
|
1533
|
|
|
1544
|
|
|
1534
|
self.rl_next_input = None
|
|
1545
|
self.rl_next_input = None
|
|
1535
|
self.rl_do_indent = False
|
|
1546
|
self.rl_do_indent = False
|
|
1536
|
|
|
1547
|
|
|
1537
|
if not self.readline_use or not readline.have_readline:
|
|
1548
|
if not self.readline_use or not readline.have_readline:
|
|
1538
|
self.has_readline = False
|
|
1549
|
self.has_readline = False
|
|
1539
|
self.readline = None
|
|
1550
|
self.readline = None
|
|
1540
|
# Set a number of methods that depend on readline to be no-op
|
|
1551
|
# Set a number of methods that depend on readline to be no-op
|
|
1541
|
self.savehist = no_op
|
|
1552
|
self.savehist = no_op
|
|
1542
|
self.reloadhist = no_op
|
|
1553
|
self.reloadhist = no_op
|
|
1543
|
self.set_readline_completer = no_op
|
|
1554
|
self.set_readline_completer = no_op
|
|
1544
|
self.set_custom_completer = no_op
|
|
1555
|
self.set_custom_completer = no_op
|
|
1545
|
self.set_completer_frame = no_op
|
|
1556
|
self.set_completer_frame = no_op
|
|
1546
|
warn('Readline services not available or not loaded.')
|
|
1557
|
warn('Readline services not available or not loaded.')
|
|
1547
|
else:
|
|
1558
|
else:
|
|
1548
|
self.has_readline = True
|
|
1559
|
self.has_readline = True
|
|
1549
|
self.readline = readline
|
|
1560
|
self.readline = readline
|
|
1550
|
sys.modules['readline'] = readline
|
|
1561
|
sys.modules['readline'] = readline
|
|
1551
|
|
|
1562
|
|
|
1552
|
# Platform-specific configuration
|
|
1563
|
# Platform-specific configuration
|
|
1553
|
if os.name == 'nt':
|
|
1564
|
if os.name == 'nt':
|
|
1554
|
# FIXME - check with Frederick to see if we can harmonize
|
|
1565
|
# FIXME - check with Frederick to see if we can harmonize
|
|
1555
|
# naming conventions with pyreadline to avoid this
|
|
1566
|
# naming conventions with pyreadline to avoid this
|
|
1556
|
# platform-dependent check
|
|
1567
|
# platform-dependent check
|
|
1557
|
self.readline_startup_hook = readline.set_pre_input_hook
|
|
1568
|
self.readline_startup_hook = readline.set_pre_input_hook
|
|
1558
|
else:
|
|
1569
|
else:
|
|
1559
|
self.readline_startup_hook = readline.set_startup_hook
|
|
1570
|
self.readline_startup_hook = readline.set_startup_hook
|
|
1560
|
|
|
1571
|
|
|
1561
|
# Load user's initrc file (readline config)
|
|
1572
|
# Load user's initrc file (readline config)
|
|
1562
|
# Or if libedit is used, load editrc.
|
|
1573
|
# Or if libedit is used, load editrc.
|
|
1563
|
inputrc_name = os.environ.get('INPUTRC')
|
|
1574
|
inputrc_name = os.environ.get('INPUTRC')
|
|
1564
|
if inputrc_name is None:
|
|
1575
|
if inputrc_name is None:
|
|
1565
|
home_dir = get_home_dir()
|
|
1576
|
home_dir = get_home_dir()
|
|
1566
|
if home_dir is not None:
|
|
1577
|
if home_dir is not None:
|
|
1567
|
inputrc_name = '.inputrc'
|
|
1578
|
inputrc_name = '.inputrc'
|
|
1568
|
if readline.uses_libedit:
|
|
1579
|
if readline.uses_libedit:
|
|
1569
|
inputrc_name = '.editrc'
|
|
1580
|
inputrc_name = '.editrc'
|
|
1570
|
inputrc_name = os.path.join(home_dir, inputrc_name)
|
|
1581
|
inputrc_name = os.path.join(home_dir, inputrc_name)
|
|
1571
|
if os.path.isfile(inputrc_name):
|
|
1582
|
if os.path.isfile(inputrc_name):
|
|
1572
|
try:
|
|
1583
|
try:
|
|
1573
|
readline.read_init_file(inputrc_name)
|
|
1584
|
readline.read_init_file(inputrc_name)
|
|
1574
|
except:
|
|
1585
|
except:
|
|
1575
|
warn('Problems reading readline initialization file <%s>'
|
|
1586
|
warn('Problems reading readline initialization file <%s>'
|
|
1576
|
% inputrc_name)
|
|
1587
|
% inputrc_name)
|
|
1577
|
|
|
1588
|
|
|
1578
|
# Configure readline according to user's prefs
|
|
1589
|
# Configure readline according to user's prefs
|
|
1579
|
# This is only done if GNU readline is being used. If libedit
|
|
1590
|
# This is only done if GNU readline is being used. If libedit
|
|
1580
|
# is being used (as on Leopard) the readline config is
|
|
1591
|
# is being used (as on Leopard) the readline config is
|
|
1581
|
# not run as the syntax for libedit is different.
|
|
1592
|
# not run as the syntax for libedit is different.
|
|
1582
|
if not readline.uses_libedit:
|
|
1593
|
if not readline.uses_libedit:
|
|
1583
|
for rlcommand in self.readline_parse_and_bind:
|
|
1594
|
for rlcommand in self.readline_parse_and_bind:
|
|
1584
|
#print "loading rl:",rlcommand # dbg
|
|
1595
|
#print "loading rl:",rlcommand # dbg
|
|
1585
|
readline.parse_and_bind(rlcommand)
|
|
1596
|
readline.parse_and_bind(rlcommand)
|
|
1586
|
|
|
1597
|
|
|
1587
|
# Remove some chars from the delimiters list. If we encounter
|
|
1598
|
# Remove some chars from the delimiters list. If we encounter
|
|
1588
|
# unicode chars, discard them.
|
|
1599
|
# unicode chars, discard them.
|
|
1589
|
delims = readline.get_completer_delims().encode("ascii", "ignore")
|
|
1600
|
delims = readline.get_completer_delims().encode("ascii", "ignore")
|
|
1590
|
delims = delims.translate(string._idmap,
|
|
1601
|
delims = delims.translate(string._idmap,
|
|
1591
|
self.readline_remove_delims)
|
|
1602
|
self.readline_remove_delims)
|
|
1592
|
delims = delims.replace(ESC_MAGIC, '')
|
|
1603
|
delims = delims.replace(ESC_MAGIC, '')
|
|
1593
|
readline.set_completer_delims(delims)
|
|
1604
|
readline.set_completer_delims(delims)
|
|
1594
|
# otherwise we end up with a monster history after a while:
|
|
1605
|
# otherwise we end up with a monster history after a while:
|
|
1595
|
readline.set_history_length(1000)
|
|
1606
|
readline.set_history_length(1000)
|
|
1596
|
try:
|
|
1607
|
try:
|
|
1597
|
#print '*** Reading readline history' # dbg
|
|
1608
|
#print '*** Reading readline history' # dbg
|
|
1598
|
readline.read_history_file(self.histfile)
|
|
1609
|
readline.read_history_file(self.histfile)
|
|
1599
|
except IOError:
|
|
1610
|
except IOError:
|
|
1600
|
pass # It doesn't exist yet.
|
|
1611
|
pass # It doesn't exist yet.
|
|
1601
|
|
|
1612
|
|
|
1602
|
# If we have readline, we want our history saved upon ipython
|
|
1613
|
# If we have readline, we want our history saved upon ipython
|
|
1603
|
# exiting.
|
|
1614
|
# exiting.
|
|
1604
|
atexit.register(self.savehist)
|
|
1615
|
atexit.register(self.savehist)
|
|
1605
|
|
|
1616
|
|
|
1606
|
# Configure auto-indent for all platforms
|
|
1617
|
# Configure auto-indent for all platforms
|
|
1607
|
self.set_autoindent(self.autoindent)
|
|
1618
|
self.set_autoindent(self.autoindent)
|
|
1608
|
|
|
1619
|
|
|
1609
|
def set_next_input(self, s):
|
|
1620
|
def set_next_input(self, s):
|
|
1610
|
""" Sets the 'default' input string for the next command line.
|
|
1621
|
""" Sets the 'default' input string for the next command line.
|
|
1611
|
|
|
1622
|
|
|
1612
|
Requires readline.
|
|
1623
|
Requires readline.
|
|
1613
|
|
|
1624
|
|
|
1614
|
Example:
|
|
1625
|
Example:
|
|
1615
|
|
|
1626
|
|
|
1616
|
[D:\ipython]|1> _ip.set_next_input("Hello Word")
|
|
1627
|
[D:\ipython]|1> _ip.set_next_input("Hello Word")
|
|
1617
|
[D:\ipython]|2> Hello Word_ # cursor is here
|
|
1628
|
[D:\ipython]|2> Hello Word_ # cursor is here
|
|
1618
|
"""
|
|
1629
|
"""
|
|
1619
|
|
|
1630
|
|
|
1620
|
self.rl_next_input = s
|
|
1631
|
self.rl_next_input = s
|
|
1621
|
|
|
1632
|
|
|
1622
|
# Maybe move this to the terminal subclass?
|
|
1633
|
# Maybe move this to the terminal subclass?
|
|
1623
|
def pre_readline(self):
|
|
1634
|
def pre_readline(self):
|
|
1624
|
"""readline hook to be used at the start of each line.
|
|
1635
|
"""readline hook to be used at the start of each line.
|
|
1625
|
|
|
1636
|
|
|
1626
|
Currently it handles auto-indent only."""
|
|
1637
|
Currently it handles auto-indent only."""
|
|
1627
|
|
|
1638
|
|
|
1628
|
if self.rl_do_indent:
|
|
1639
|
if self.rl_do_indent:
|
|
1629
|
self.readline.insert_text(self._indent_current_str())
|
|
1640
|
self.readline.insert_text(self._indent_current_str())
|
|
1630
|
if self.rl_next_input is not None:
|
|
1641
|
if self.rl_next_input is not None:
|
|
1631
|
self.readline.insert_text(self.rl_next_input)
|
|
1642
|
self.readline.insert_text(self.rl_next_input)
|
|
1632
|
self.rl_next_input = None
|
|
1643
|
self.rl_next_input = None
|
|
1633
|
|
|
1644
|
|
|
1634
|
def _indent_current_str(self):
|
|
1645
|
def _indent_current_str(self):
|
|
1635
|
"""return the current level of indentation as a string"""
|
|
1646
|
"""return the current level of indentation as a string"""
|
|
1636
|
return self.indent_current_nsp * ' '
|
|
1647
|
return self.indent_current_nsp * ' '
|
|
1637
|
|
|
1648
|
|
|
1638
|
#-------------------------------------------------------------------------
|
|
1649
|
#-------------------------------------------------------------------------
|
|
1639
|
# Things related to text completion
|
|
1650
|
# Things related to text completion
|
|
1640
|
#-------------------------------------------------------------------------
|
|
1651
|
#-------------------------------------------------------------------------
|
|
1641
|
|
|
1652
|
|
|
1642
|
def init_completer(self):
|
|
1653
|
def init_completer(self):
|
|
1643
|
"""Initialize the completion machinery.
|
|
1654
|
"""Initialize the completion machinery.
|
|
1644
|
|
|
1655
|
|
|
1645
|
This creates completion machinery that can be used by client code,
|
|
1656
|
This creates completion machinery that can be used by client code,
|
|
1646
|
either interactively in-process (typically triggered by the readline
|
|
1657
|
either interactively in-process (typically triggered by the readline
|
|
1647
|
library), programatically (such as in test suites) or out-of-prcess
|
|
1658
|
library), programatically (such as in test suites) or out-of-prcess
|
|
1648
|
(typically over the network by remote frontends).
|
|
1659
|
(typically over the network by remote frontends).
|
|
1649
|
"""
|
|
1660
|
"""
|
|
1650
|
from IPython.core.completer import IPCompleter
|
|
1661
|
from IPython.core.completer import IPCompleter
|
|
1651
|
from IPython.core.completerlib import (module_completer,
|
|
1662
|
from IPython.core.completerlib import (module_completer,
|
|
1652
|
magic_run_completer, cd_completer)
|
|
1663
|
magic_run_completer, cd_completer)
|
|
1653
|
|
|
1664
|
|
|
1654
|
self.Completer = IPCompleter(self,
|
|
1665
|
self.Completer = IPCompleter(self,
|
|
1655
|
self.user_ns,
|
|
1666
|
self.user_ns,
|
|
1656
|
self.user_global_ns,
|
|
1667
|
self.user_global_ns,
|
|
1657
|
self.readline_omit__names,
|
|
1668
|
self.readline_omit__names,
|
|
1658
|
self.alias_manager.alias_table,
|
|
1669
|
self.alias_manager.alias_table,
|
|
1659
|
self.has_readline)
|
|
1670
|
self.has_readline)
|
|
1660
|
|
|
1671
|
|
|
1661
|
# Add custom completers to the basic ones built into IPCompleter
|
|
1672
|
# Add custom completers to the basic ones built into IPCompleter
|
|
1662
|
sdisp = self.strdispatchers.get('complete_command', StrDispatch())
|
|
1673
|
sdisp = self.strdispatchers.get('complete_command', StrDispatch())
|
|
1663
|
self.strdispatchers['complete_command'] = sdisp
|
|
1674
|
self.strdispatchers['complete_command'] = sdisp
|
|
1664
|
self.Completer.custom_completers = sdisp
|
|
1675
|
self.Completer.custom_completers = sdisp
|
|
1665
|
|
|
1676
|
|
|
1666
|
self.set_hook('complete_command', module_completer, str_key = 'import')
|
|
1677
|
self.set_hook('complete_command', module_completer, str_key = 'import')
|
|
1667
|
self.set_hook('complete_command', module_completer, str_key = 'from')
|
|
1678
|
self.set_hook('complete_command', module_completer, str_key = 'from')
|
|
1668
|
self.set_hook('complete_command', magic_run_completer, str_key = '%run')
|
|
1679
|
self.set_hook('complete_command', magic_run_completer, str_key = '%run')
|
|
1669
|
self.set_hook('complete_command', cd_completer, str_key = '%cd')
|
|
1680
|
self.set_hook('complete_command', cd_completer, str_key = '%cd')
|
|
1670
|
|
|
1681
|
|
|
1671
|
# Only configure readline if we truly are using readline. IPython can
|
|
1682
|
# Only configure readline if we truly are using readline. IPython can
|
|
1672
|
# do tab-completion over the network, in GUIs, etc, where readline
|
|
1683
|
# do tab-completion over the network, in GUIs, etc, where readline
|
|
1673
|
# itself may be absent
|
|
1684
|
# itself may be absent
|
|
1674
|
if self.has_readline:
|
|
1685
|
if self.has_readline:
|
|
1675
|
self.set_readline_completer()
|
|
1686
|
self.set_readline_completer()
|
|
1676
|
|
|
1687
|
|
|
1677
|
def complete(self, text, line=None, cursor_pos=None):
|
|
1688
|
def complete(self, text, line=None, cursor_pos=None):
|
|
1678
|
"""Return the completed text and a list of completions.
|
|
1689
|
"""Return the completed text and a list of completions.
|
|
1679
|
|
|
1690
|
|
|
1680
|
Parameters
|
|
1691
|
Parameters
|
|
1681
|
----------
|
|
1692
|
----------
|
|
1682
|
|
|
1693
|
|
|
1683
|
text : string
|
|
1694
|
text : string
|
|
1684
|
A string of text to be completed on. It can be given as empty and
|
|
1695
|
A string of text to be completed on. It can be given as empty and
|
|
1685
|
instead a line/position pair are given. In this case, the
|
|
1696
|
instead a line/position pair are given. In this case, the
|
|
1686
|
completer itself will split the line like readline does.
|
|
1697
|
completer itself will split the line like readline does.
|
|
1687
|
|
|
1698
|
|
|
1688
|
line : string, optional
|
|
1699
|
line : string, optional
|
|
1689
|
The complete line that text is part of.
|
|
1700
|
The complete line that text is part of.
|
|
1690
|
|
|
1701
|
|
|
1691
|
cursor_pos : int, optional
|
|
1702
|
cursor_pos : int, optional
|
|
1692
|
The position of the cursor on the input line.
|
|
1703
|
The position of the cursor on the input line.
|
|
1693
|
|
|
1704
|
|
|
1694
|
Returns
|
|
1705
|
Returns
|
|
1695
|
-------
|
|
1706
|
-------
|
|
1696
|
text : string
|
|
1707
|
text : string
|
|
1697
|
The actual text that was completed.
|
|
1708
|
The actual text that was completed.
|
|
1698
|
|
|
1709
|
|
|
1699
|
matches : list
|
|
1710
|
matches : list
|
|
1700
|
A sorted list with all possible completions.
|
|
1711
|
A sorted list with all possible completions.
|
|
1701
|
|
|
1712
|
|
|
1702
|
The optional arguments allow the completion to take more context into
|
|
1713
|
The optional arguments allow the completion to take more context into
|
|
1703
|
account, and are part of the low-level completion API.
|
|
1714
|
account, and are part of the low-level completion API.
|
|
1704
|
|
|
1715
|
|
|
1705
|
This is a wrapper around the completion mechanism, similar to what
|
|
1716
|
This is a wrapper around the completion mechanism, similar to what
|
|
1706
|
readline does at the command line when the TAB key is hit. By
|
|
1717
|
readline does at the command line when the TAB key is hit. By
|
|
1707
|
exposing it as a method, it can be used by other non-readline
|
|
1718
|
exposing it as a method, it can be used by other non-readline
|
|
1708
|
environments (such as GUIs) for text completion.
|
|
1719
|
environments (such as GUIs) for text completion.
|
|
1709
|
|
|
1720
|
|
|
1710
|
Simple usage example:
|
|
1721
|
Simple usage example:
|
|
1711
|
|
|
1722
|
|
|
1712
|
In [1]: x = 'hello'
|
|
1723
|
In [1]: x = 'hello'
|
|
1713
|
|
|
1724
|
|
|
1714
|
In [2]: _ip.complete('x.l')
|
|
1725
|
In [2]: _ip.complete('x.l')
|
|
1715
|
Out[2]: ('x.l', ['x.ljust', 'x.lower', 'x.lstrip'])
|
|
1726
|
Out[2]: ('x.l', ['x.ljust', 'x.lower', 'x.lstrip'])
|
|
1716
|
"""
|
|
1727
|
"""
|
|
1717
|
|
|
1728
|
|
|
1718
|
# Inject names into __builtin__ so we can complete on the added names.
|
|
1729
|
# Inject names into __builtin__ so we can complete on the added names.
|
|
1719
|
with self.builtin_trap:
|
|
1730
|
with self.builtin_trap:
|
|
1720
|
return self.Completer.complete(text, line, cursor_pos)
|
|
1731
|
return self.Completer.complete(text, line, cursor_pos)
|
|
1721
|
|
|
1732
|
|
|
1722
|
def set_custom_completer(self, completer, pos=0):
|
|
1733
|
def set_custom_completer(self, completer, pos=0):
|
|
1723
|
"""Adds a new custom completer function.
|
|
1734
|
"""Adds a new custom completer function.
|
|
1724
|
|
|
1735
|
|
|
1725
|
The position argument (defaults to 0) is the index in the completers
|
|
1736
|
The position argument (defaults to 0) is the index in the completers
|
|
1726
|
list where you want the completer to be inserted."""
|
|
1737
|
list where you want the completer to be inserted."""
|
|
1727
|
|
|
1738
|
|
|
1728
|
newcomp = new.instancemethod(completer,self.Completer,
|
|
1739
|
newcomp = new.instancemethod(completer,self.Completer,
|
|
1729
|
self.Completer.__class__)
|
|
1740
|
self.Completer.__class__)
|
|
1730
|
self.Completer.matchers.insert(pos,newcomp)
|
|
1741
|
self.Completer.matchers.insert(pos,newcomp)
|
|
1731
|
|
|
1742
|
|
|
1732
|
def set_readline_completer(self):
|
|
1743
|
def set_readline_completer(self):
|
|
1733
|
"""Reset readline's completer to be our own."""
|
|
1744
|
"""Reset readline's completer to be our own."""
|
|
1734
|
self.readline.set_completer(self.Completer.rlcomplete)
|
|
1745
|
self.readline.set_completer(self.Completer.rlcomplete)
|
|
1735
|
|
|
1746
|
|
|
1736
|
def set_completer_frame(self, frame=None):
|
|
1747
|
def set_completer_frame(self, frame=None):
|
|
1737
|
"""Set the frame of the completer."""
|
|
1748
|
"""Set the frame of the completer."""
|
|
1738
|
if frame:
|
|
1749
|
if frame:
|
|
1739
|
self.Completer.namespace = frame.f_locals
|
|
1750
|
self.Completer.namespace = frame.f_locals
|
|
1740
|
self.Completer.global_namespace = frame.f_globals
|
|
1751
|
self.Completer.global_namespace = frame.f_globals
|
|
1741
|
else:
|
|
1752
|
else:
|
|
1742
|
self.Completer.namespace = self.user_ns
|
|
1753
|
self.Completer.namespace = self.user_ns
|
|
1743
|
self.Completer.global_namespace = self.user_global_ns
|
|
1754
|
self.Completer.global_namespace = self.user_global_ns
|
|
1744
|
|
|
1755
|
|
|
1745
|
#-------------------------------------------------------------------------
|
|
1756
|
#-------------------------------------------------------------------------
|
|
1746
|
# Things related to magics
|
|
1757
|
# Things related to magics
|
|
1747
|
#-------------------------------------------------------------------------
|
|
1758
|
#-------------------------------------------------------------------------
|
|
1748
|
|
|
1759
|
|
|
1749
|
def init_magics(self):
|
|
1760
|
def init_magics(self):
|
|
1750
|
# FIXME: Move the color initialization to the DisplayHook, which
|
|
1761
|
# FIXME: Move the color initialization to the DisplayHook, which
|
|
1751
|
# should be split into a prompt manager and displayhook. We probably
|
|
1762
|
# should be split into a prompt manager and displayhook. We probably
|
|
1752
|
# even need a centralize colors management object.
|
|
1763
|
# even need a centralize colors management object.
|
|
1753
|
self.magic_colors(self.colors)
|
|
1764
|
self.magic_colors(self.colors)
|
|
1754
|
# History was moved to a separate module
|
|
1765
|
# History was moved to a separate module
|
|
1755
|
from . import history
|
|
1766
|
from . import history
|
|
1756
|
history.init_ipython(self)
|
|
1767
|
history.init_ipython(self)
|
|
1757
|
|
|
1768
|
|
|
1758
|
def magic(self,arg_s):
|
|
1769
|
def magic(self,arg_s):
|
|
1759
|
"""Call a magic function by name.
|
|
1770
|
"""Call a magic function by name.
|
|
1760
|
|
|
1771
|
|
|
1761
|
Input: a string containing the name of the magic function to call and
|
|
1772
|
Input: a string containing the name of the magic function to call and
|
|
1762
|
any additional arguments to be passed to the magic.
|
|
1773
|
any additional arguments to be passed to the magic.
|
|
1763
|
|
|
1774
|
|
|
1764
|
magic('name -opt foo bar') is equivalent to typing at the ipython
|
|
1775
|
magic('name -opt foo bar') is equivalent to typing at the ipython
|
|
1765
|
prompt:
|
|
1776
|
prompt:
|
|
1766
|
|
|
1777
|
|
|
1767
|
In[1]: %name -opt foo bar
|
|
1778
|
In[1]: %name -opt foo bar
|
|
1768
|
|
|
1779
|
|
|
1769
|
To call a magic without arguments, simply use magic('name').
|
|
1780
|
To call a magic without arguments, simply use magic('name').
|
|
1770
|
|
|
1781
|
|
|
1771
|
This provides a proper Python function to call IPython's magics in any
|
|
1782
|
This provides a proper Python function to call IPython's magics in any
|
|
1772
|
valid Python code you can type at the interpreter, including loops and
|
|
1783
|
valid Python code you can type at the interpreter, including loops and
|
|
1773
|
compound statements.
|
|
1784
|
compound statements.
|
|
1774
|
"""
|
|
1785
|
"""
|
|
1775
|
args = arg_s.split(' ',1)
|
|
1786
|
args = arg_s.split(' ',1)
|
|
1776
|
magic_name = args[0]
|
|
1787
|
magic_name = args[0]
|
|
1777
|
magic_name = magic_name.lstrip(prefilter.ESC_MAGIC)
|
|
1788
|
magic_name = magic_name.lstrip(prefilter.ESC_MAGIC)
|
|
1778
|
|
|
1789
|
|
|
1779
|
try:
|
|
1790
|
try:
|
|
1780
|
magic_args = args[1]
|
|
1791
|
magic_args = args[1]
|
|
1781
|
except IndexError:
|
|
1792
|
except IndexError:
|
|
1782
|
magic_args = ''
|
|
1793
|
magic_args = ''
|
|
1783
|
fn = getattr(self,'magic_'+magic_name,None)
|
|
1794
|
fn = getattr(self,'magic_'+magic_name,None)
|
|
1784
|
if fn is None:
|
|
1795
|
if fn is None:
|
|
1785
|
error("Magic function `%s` not found." % magic_name)
|
|
1796
|
error("Magic function `%s` not found." % magic_name)
|
|
1786
|
else:
|
|
1797
|
else:
|
|
1787
|
magic_args = self.var_expand(magic_args,1)
|
|
1798
|
magic_args = self.var_expand(magic_args,1)
|
|
1788
|
with nested(self.builtin_trap,):
|
|
1799
|
with nested(self.builtin_trap,):
|
|
1789
|
result = fn(magic_args)
|
|
1800
|
result = fn(magic_args)
|
|
1790
|
return result
|
|
1801
|
return result
|
|
1791
|
|
|
1802
|
|
|
1792
|
def define_magic(self, magicname, func):
|
|
1803
|
def define_magic(self, magicname, func):
|
|
1793
|
"""Expose own function as magic function for ipython
|
|
1804
|
"""Expose own function as magic function for ipython
|
|
1794
|
|
|
1805
|
|
|
1795
|
def foo_impl(self,parameter_s=''):
|
|
1806
|
def foo_impl(self,parameter_s=''):
|
|
1796
|
'My very own magic!. (Use docstrings, IPython reads them).'
|
|
1807
|
'My very own magic!. (Use docstrings, IPython reads them).'
|
|
1797
|
print 'Magic function. Passed parameter is between < >:'
|
|
1808
|
print 'Magic function. Passed parameter is between < >:'
|
|
1798
|
print '<%s>' % parameter_s
|
|
1809
|
print '<%s>' % parameter_s
|
|
1799
|
print 'The self object is:',self
|
|
1810
|
print 'The self object is:',self
|
|
1800
|
|
|
1811
|
|
|
1801
|
self.define_magic('foo',foo_impl)
|
|
1812
|
self.define_magic('foo',foo_impl)
|
|
1802
|
"""
|
|
1813
|
"""
|
|
1803
|
|
|
1814
|
|
|
1804
|
import new
|
|
1815
|
import new
|
|
1805
|
im = new.instancemethod(func,self, self.__class__)
|
|
1816
|
im = new.instancemethod(func,self, self.__class__)
|
|
1806
|
old = getattr(self, "magic_" + magicname, None)
|
|
1817
|
old = getattr(self, "magic_" + magicname, None)
|
|
1807
|
setattr(self, "magic_" + magicname, im)
|
|
1818
|
setattr(self, "magic_" + magicname, im)
|
|
1808
|
return old
|
|
1819
|
return old
|
|
1809
|
|
|
1820
|
|
|
1810
|
#-------------------------------------------------------------------------
|
|
1821
|
#-------------------------------------------------------------------------
|
|
1811
|
# Things related to macros
|
|
1822
|
# Things related to macros
|
|
1812
|
#-------------------------------------------------------------------------
|
|
1823
|
#-------------------------------------------------------------------------
|
|
1813
|
|
|
1824
|
|
|
1814
|
def define_macro(self, name, themacro):
|
|
1825
|
def define_macro(self, name, themacro):
|
|
1815
|
"""Define a new macro
|
|
1826
|
"""Define a new macro
|
|
1816
|
|
|
1827
|
|
|
1817
|
Parameters
|
|
1828
|
Parameters
|
|
1818
|
----------
|
|
1829
|
----------
|
|
1819
|
name : str
|
|
1830
|
name : str
|
|
1820
|
The name of the macro.
|
|
1831
|
The name of the macro.
|
|
1821
|
themacro : str or Macro
|
|
1832
|
themacro : str or Macro
|
|
1822
|
The action to do upon invoking the macro. If a string, a new
|
|
1833
|
The action to do upon invoking the macro. If a string, a new
|
|
1823
|
Macro object is created by passing the string to it.
|
|
1834
|
Macro object is created by passing the string to it.
|
|
1824
|
"""
|
|
1835
|
"""
|
|
1825
|
|
|
1836
|
|
|
1826
|
from IPython.core import macro
|
|
1837
|
from IPython.core import macro
|
|
1827
|
|
|
1838
|
|
|
1828
|
if isinstance(themacro, basestring):
|
|
1839
|
if isinstance(themacro, basestring):
|
|
1829
|
themacro = macro.Macro(themacro)
|
|
1840
|
themacro = macro.Macro(themacro)
|
|
1830
|
if not isinstance(themacro, macro.Macro):
|
|
1841
|
if not isinstance(themacro, macro.Macro):
|
|
1831
|
raise ValueError('A macro must be a string or a Macro instance.')
|
|
1842
|
raise ValueError('A macro must be a string or a Macro instance.')
|
|
1832
|
self.user_ns[name] = themacro
|
|
1843
|
self.user_ns[name] = themacro
|
|
1833
|
|
|
1844
|
|
|
1834
|
#-------------------------------------------------------------------------
|
|
1845
|
#-------------------------------------------------------------------------
|
|
1835
|
# Things related to the running of system commands
|
|
1846
|
# Things related to the running of system commands
|
|
1836
|
#-------------------------------------------------------------------------
|
|
1847
|
#-------------------------------------------------------------------------
|
|
1837
|
|
|
1848
|
|
|
1838
|
def system(self, cmd):
|
|
1849
|
def system(self, cmd):
|
|
1839
|
"""Call the given cmd in a subprocess."""
|
|
1850
|
"""Call the given cmd in a subprocess."""
|
|
1840
|
# We do not support backgrounding processes because we either use
|
|
1851
|
# We do not support backgrounding processes because we either use
|
|
1841
|
# pexpect or pipes to read from. Users can always just call
|
|
1852
|
# pexpect or pipes to read from. Users can always just call
|
|
1842
|
# os.system() if they really want a background process.
|
|
1853
|
# os.system() if they really want a background process.
|
|
1843
|
if cmd.endswith('&'):
|
|
1854
|
if cmd.endswith('&'):
|
|
1844
|
raise OSError("Background processes not supported.")
|
|
1855
|
raise OSError("Background processes not supported.")
|
|
1845
|
|
|
1856
|
|
|
1846
|
return system(self.var_expand(cmd, depth=2))
|
|
1857
|
return system(self.var_expand(cmd, depth=2))
|
|
1847
|
|
|
1858
|
|
|
1848
|
def getoutput(self, cmd):
|
|
1859
|
def getoutput(self, cmd):
|
|
1849
|
"""Get output (possibly including stderr) from a subprocess."""
|
|
1860
|
"""Get output (possibly including stderr) from a subprocess."""
|
|
1850
|
if cmd.endswith('&'):
|
|
1861
|
if cmd.endswith('&'):
|
|
1851
|
raise OSError("Background processes not supported.")
|
|
1862
|
raise OSError("Background processes not supported.")
|
|
1852
|
return getoutput(self.var_expand(cmd, depth=2))
|
|
1863
|
return getoutput(self.var_expand(cmd, depth=2))
|
|
1853
|
|
|
1864
|
|
|
1854
|
#-------------------------------------------------------------------------
|
|
1865
|
#-------------------------------------------------------------------------
|
|
1855
|
# Things related to aliases
|
|
1866
|
# Things related to aliases
|
|
1856
|
#-------------------------------------------------------------------------
|
|
1867
|
#-------------------------------------------------------------------------
|
|
1857
|
|
|
1868
|
|
|
1858
|
def init_alias(self):
|
|
1869
|
def init_alias(self):
|
|
1859
|
self.alias_manager = AliasManager(shell=self, config=self.config)
|
|
1870
|
self.alias_manager = AliasManager(shell=self, config=self.config)
|
|
1860
|
self.ns_table['alias'] = self.alias_manager.alias_table,
|
|
1871
|
self.ns_table['alias'] = self.alias_manager.alias_table,
|
|
1861
|
|
|
1872
|
|
|
1862
|
#-------------------------------------------------------------------------
|
|
1873
|
#-------------------------------------------------------------------------
|
|
1863
|
# Things related to extensions and plugins
|
|
1874
|
# Things related to extensions and plugins
|
|
1864
|
#-------------------------------------------------------------------------
|
|
1875
|
#-------------------------------------------------------------------------
|
|
1865
|
|
|
1876
|
|
|
1866
|
def init_extension_manager(self):
|
|
1877
|
def init_extension_manager(self):
|
|
1867
|
self.extension_manager = ExtensionManager(shell=self, config=self.config)
|
|
1878
|
self.extension_manager = ExtensionManager(shell=self, config=self.config)
|
|
1868
|
|
|
1879
|
|
|
1869
|
def init_plugin_manager(self):
|
|
1880
|
def init_plugin_manager(self):
|
|
1870
|
self.plugin_manager = PluginManager(config=self.config)
|
|
1881
|
self.plugin_manager = PluginManager(config=self.config)
|
|
1871
|
|
|
1882
|
|
|
1872
|
#-------------------------------------------------------------------------
|
|
1883
|
#-------------------------------------------------------------------------
|
|
1873
|
# Things related to payloads
|
|
1884
|
# Things related to payloads
|
|
1874
|
#-------------------------------------------------------------------------
|
|
1885
|
#-------------------------------------------------------------------------
|
|
1875
|
|
|
1886
|
|
|
1876
|
def init_payload(self):
|
|
1887
|
def init_payload(self):
|
|
1877
|
self.payload_manager = PayloadManager(config=self.config)
|
|
1888
|
self.payload_manager = PayloadManager(config=self.config)
|
|
1878
|
|
|
1889
|
|
|
1879
|
#-------------------------------------------------------------------------
|
|
1890
|
#-------------------------------------------------------------------------
|
|
1880
|
# Things related to the prefilter
|
|
1891
|
# Things related to the prefilter
|
|
1881
|
#-------------------------------------------------------------------------
|
|
1892
|
#-------------------------------------------------------------------------
|
|
1882
|
|
|
1893
|
|
|
1883
|
def init_prefilter(self):
|
|
1894
|
def init_prefilter(self):
|
|
1884
|
self.prefilter_manager = PrefilterManager(shell=self, config=self.config)
|
|
1895
|
self.prefilter_manager = PrefilterManager(shell=self, config=self.config)
|
|
1885
|
# Ultimately this will be refactored in the new interpreter code, but
|
|
1896
|
# Ultimately this will be refactored in the new interpreter code, but
|
|
1886
|
# for now, we should expose the main prefilter method (there's legacy
|
|
1897
|
# for now, we should expose the main prefilter method (there's legacy
|
|
1887
|
# code out there that may rely on this).
|
|
1898
|
# code out there that may rely on this).
|
|
1888
|
self.prefilter = self.prefilter_manager.prefilter_lines
|
|
1899
|
self.prefilter = self.prefilter_manager.prefilter_lines
|
|
1889
|
|
|
1900
|
|
|
1890
|
|
|
1901
|
|
|
1891
|
def auto_rewrite_input(self, cmd):
|
|
1902
|
def auto_rewrite_input(self, cmd):
|
|
1892
|
"""Print to the screen the rewritten form of the user's command.
|
|
1903
|
"""Print to the screen the rewritten form of the user's command.
|
|
1893
|
|
|
1904
|
|
|
1894
|
This shows visual feedback by rewriting input lines that cause
|
|
1905
|
This shows visual feedback by rewriting input lines that cause
|
|
1895
|
automatic calling to kick in, like::
|
|
1906
|
automatic calling to kick in, like::
|
|
1896
|
|
|
1907
|
|
|
1897
|
/f x
|
|
1908
|
/f x
|
|
1898
|
|
|
1909
|
|
|
1899
|
into::
|
|
1910
|
into::
|
|
1900
|
|
|
1911
|
|
|
1901
|
------> f(x)
|
|
1912
|
------> f(x)
|
|
1902
|
|
|
1913
|
|
|
1903
|
after the user's input prompt. This helps the user understand that the
|
|
1914
|
after the user's input prompt. This helps the user understand that the
|
|
1904
|
input line was transformed automatically by IPython.
|
|
1915
|
input line was transformed automatically by IPython.
|
|
1905
|
"""
|
|
1916
|
"""
|
|
1906
|
rw = self.displayhook.prompt1.auto_rewrite() + cmd
|
|
1917
|
rw = self.displayhook.prompt1.auto_rewrite() + cmd
|
|
1907
|
|
|
1918
|
|
|
1908
|
try:
|
|
1919
|
try:
|
|
1909
|
# plain ascii works better w/ pyreadline, on some machines, so
|
|
1920
|
# plain ascii works better w/ pyreadline, on some machines, so
|
|
1910
|
# we use it and only print uncolored rewrite if we have unicode
|
|
1921
|
# we use it and only print uncolored rewrite if we have unicode
|
|
1911
|
rw = str(rw)
|
|
1922
|
rw = str(rw)
|
|
1912
|
print >> IPython.utils.io.Term.cout, rw
|
|
1923
|
print >> IPython.utils.io.Term.cout, rw
|
|
1913
|
except UnicodeEncodeError:
|
|
1924
|
except UnicodeEncodeError:
|
|
1914
|
print "------> " + cmd
|
|
1925
|
print "------> " + cmd
|
|
1915
|
|
|
1926
|
|
|
1916
|
#-------------------------------------------------------------------------
|
|
1927
|
#-------------------------------------------------------------------------
|
|
1917
|
# Things related to extracting values/expressions from kernel and user_ns
|
|
1928
|
# Things related to extracting values/expressions from kernel and user_ns
|
|
1918
|
#-------------------------------------------------------------------------
|
|
1929
|
#-------------------------------------------------------------------------
|
|
1919
|
|
|
1930
|
|
|
1920
|
def _simple_error(self):
|
|
1931
|
def _simple_error(self):
|
|
1921
|
etype, value = sys.exc_info()[:2]
|
|
1932
|
etype, value = sys.exc_info()[:2]
|
|
1922
|
return u'[ERROR] {e.__name__}: {v}'.format(e=etype, v=value)
|
|
1933
|
return u'[ERROR] {e.__name__}: {v}'.format(e=etype, v=value)
|
|
1923
|
|
|
1934
|
|
|
1924
|
def get_user_variables(self, names):
|
|
1935
|
def get_user_variables(self, names):
|
|
1925
|
"""Get a list of variable names from the user's namespace.
|
|
1936
|
"""Get a list of variable names from the user's namespace.
|
|
1926
|
|
|
1937
|
|
|
1927
|
The return value is a dict with the repr() of each value.
|
|
1938
|
The return value is a dict with the repr() of each value.
|
|
1928
|
"""
|
|
1939
|
"""
|
|
1929
|
out = {}
|
|
1940
|
out = {}
|
|
1930
|
user_ns = self.user_ns
|
|
1941
|
user_ns = self.user_ns
|
|
1931
|
for varname in names:
|
|
1942
|
for varname in names:
|
|
1932
|
try:
|
|
1943
|
try:
|
|
1933
|
value = repr(user_ns[varname])
|
|
1944
|
value = repr(user_ns[varname])
|
|
1934
|
except:
|
|
1945
|
except:
|
|
1935
|
value = self._simple_error()
|
|
1946
|
value = self._simple_error()
|
|
1936
|
out[varname] = value
|
|
1947
|
out[varname] = value
|
|
1937
|
return out
|
|
1948
|
return out
|
|
1938
|
|
|
1949
|
|
|
1939
|
def eval_expressions(self, expressions):
|
|
1950
|
def eval_expressions(self, expressions):
|
|
1940
|
"""Evaluate a dict of expressions in the user's namespace.
|
|
1951
|
"""Evaluate a dict of expressions in the user's namespace.
|
|
1941
|
|
|
1952
|
|
|
1942
|
The return value is a dict with the repr() of each value.
|
|
1953
|
The return value is a dict with the repr() of each value.
|
|
1943
|
"""
|
|
1954
|
"""
|
|
1944
|
out = {}
|
|
1955
|
out = {}
|
|
1945
|
user_ns = self.user_ns
|
|
1956
|
user_ns = self.user_ns
|
|
1946
|
global_ns = self.user_global_ns
|
|
1957
|
global_ns = self.user_global_ns
|
|
1947
|
for key, expr in expressions.iteritems():
|
|
1958
|
for key, expr in expressions.iteritems():
|
|
1948
|
try:
|
|
1959
|
try:
|
|
1949
|
value = repr(eval(expr, global_ns, user_ns))
|
|
1960
|
value = repr(eval(expr, global_ns, user_ns))
|
|
1950
|
except:
|
|
1961
|
except:
|
|
1951
|
value = self._simple_error()
|
|
1962
|
value = self._simple_error()
|
|
1952
|
out[key] = value
|
|
1963
|
out[key] = value
|
|
1953
|
return out
|
|
1964
|
return out
|
|
1954
|
|
|
1965
|
|
|
1955
|
#-------------------------------------------------------------------------
|
|
1966
|
#-------------------------------------------------------------------------
|
|
1956
|
# Things related to the running of code
|
|
1967
|
# Things related to the running of code
|
|
1957
|
#-------------------------------------------------------------------------
|
|
1968
|
#-------------------------------------------------------------------------
|
|
1958
|
|
|
1969
|
|
|
1959
|
def ex(self, cmd):
|
|
1970
|
def ex(self, cmd):
|
|
1960
|
"""Execute a normal python statement in user namespace."""
|
|
1971
|
"""Execute a normal python statement in user namespace."""
|
|
1961
|
with nested(self.builtin_trap,):
|
|
1972
|
with nested(self.builtin_trap,):
|
|
1962
|
exec cmd in self.user_global_ns, self.user_ns
|
|
1973
|
exec cmd in self.user_global_ns, self.user_ns
|
|
1963
|
|
|
1974
|
|
|
1964
|
def ev(self, expr):
|
|
1975
|
def ev(self, expr):
|
|
1965
|
"""Evaluate python expression expr in user namespace.
|
|
1976
|
"""Evaluate python expression expr in user namespace.
|
|
1966
|
|
|
1977
|
|
|
1967
|
Returns the result of evaluation
|
|
1978
|
Returns the result of evaluation
|
|
1968
|
"""
|
|
1979
|
"""
|
|
1969
|
with nested(self.builtin_trap,):
|
|
1980
|
with nested(self.builtin_trap,):
|
|
1970
|
return eval(expr, self.user_global_ns, self.user_ns)
|
|
1981
|
return eval(expr, self.user_global_ns, self.user_ns)
|
|
1971
|
|
|
1982
|
|
|
1972
|
def safe_execfile(self, fname, *where, **kw):
|
|
1983
|
def safe_execfile(self, fname, *where, **kw):
|
|
1973
|
"""A safe version of the builtin execfile().
|
|
1984
|
"""A safe version of the builtin execfile().
|
|
1974
|
|
|
1985
|
|
|
1975
|
This version will never throw an exception, but instead print
|
|
1986
|
This version will never throw an exception, but instead print
|
|
1976
|
helpful error messages to the screen. This only works on pure
|
|
1987
|
helpful error messages to the screen. This only works on pure
|
|
1977
|
Python files with the .py extension.
|
|
1988
|
Python files with the .py extension.
|
|
1978
|
|
|
1989
|
|
|
1979
|
Parameters
|
|
1990
|
Parameters
|
|
1980
|
----------
|
|
1991
|
----------
|
|
1981
|
fname : string
|
|
1992
|
fname : string
|
|
1982
|
The name of the file to be executed.
|
|
1993
|
The name of the file to be executed.
|
|
1983
|
where : tuple
|
|
1994
|
where : tuple
|
|
1984
|
One or two namespaces, passed to execfile() as (globals,locals).
|
|
1995
|
One or two namespaces, passed to execfile() as (globals,locals).
|
|
1985
|
If only one is given, it is passed as both.
|
|
1996
|
If only one is given, it is passed as both.
|
|
1986
|
exit_ignore : bool (False)
|
|
1997
|
exit_ignore : bool (False)
|
|
1987
|
If True, then silence SystemExit for non-zero status (it is always
|
|
1998
|
If True, then silence SystemExit for non-zero status (it is always
|
|
1988
|
silenced for zero status, as it is so common).
|
|
1999
|
silenced for zero status, as it is so common).
|
|
1989
|
"""
|
|
2000
|
"""
|
|
1990
|
kw.setdefault('exit_ignore', False)
|
|
2001
|
kw.setdefault('exit_ignore', False)
|
|
1991
|
|
|
2002
|
|
|
1992
|
fname = os.path.abspath(os.path.expanduser(fname))
|
|
2003
|
fname = os.path.abspath(os.path.expanduser(fname))
|
|
1993
|
|
|
2004
|
|
|
1994
|
# Make sure we have a .py file
|
|
2005
|
# Make sure we have a .py file
|
|
1995
|
if not fname.endswith('.py'):
|
|
2006
|
if not fname.endswith('.py'):
|
|
1996
|
warn('File must end with .py to be run using execfile: <%s>' % fname)
|
|
2007
|
warn('File must end with .py to be run using execfile: <%s>' % fname)
|
|
1997
|
|
|
2008
|
|
|
1998
|
# Make sure we can open the file
|
|
2009
|
# Make sure we can open the file
|
|
1999
|
try:
|
|
2010
|
try:
|
|
2000
|
with open(fname) as thefile:
|
|
2011
|
with open(fname) as thefile:
|
|
2001
|
pass
|
|
2012
|
pass
|
|
2002
|
except:
|
|
2013
|
except:
|
|
2003
|
warn('Could not open file <%s> for safe execution.' % fname)
|
|
2014
|
warn('Could not open file <%s> for safe execution.' % fname)
|
|
2004
|
return
|
|
2015
|
return
|
|
2005
|
|
|
2016
|
|
|
2006
|
# Find things also in current directory. This is needed to mimic the
|
|
2017
|
# Find things also in current directory. This is needed to mimic the
|
|
2007
|
# behavior of running a script from the system command line, where
|
|
2018
|
# behavior of running a script from the system command line, where
|
|
2008
|
# Python inserts the script's directory into sys.path
|
|
2019
|
# Python inserts the script's directory into sys.path
|
|
2009
|
dname = os.path.dirname(fname)
|
|
2020
|
dname = os.path.dirname(fname)
|
|
2010
|
|
|
2021
|
|
|
2011
|
with prepended_to_syspath(dname):
|
|
2022
|
with prepended_to_syspath(dname):
|
|
2012
|
try:
|
|
2023
|
try:
|
|
2013
|
execfile(fname,*where)
|
|
2024
|
execfile(fname,*where)
|
|
2014
|
except SystemExit, status:
|
|
2025
|
except SystemExit, status:
|
|
2015
|
# If the call was made with 0 or None exit status (sys.exit(0)
|
|
2026
|
# If the call was made with 0 or None exit status (sys.exit(0)
|
|
2016
|
# or sys.exit() ), don't bother showing a traceback, as both of
|
|
2027
|
# or sys.exit() ), don't bother showing a traceback, as both of
|
|
2017
|
# these are considered normal by the OS:
|
|
2028
|
# these are considered normal by the OS:
|
|
2018
|
# > python -c'import sys;sys.exit(0)'; echo $?
|
|
2029
|
# > python -c'import sys;sys.exit(0)'; echo $?
|
|
2019
|
# 0
|
|
2030
|
# 0
|
|
2020
|
# > python -c'import sys;sys.exit()'; echo $?
|
|
2031
|
# > python -c'import sys;sys.exit()'; echo $?
|
|
2021
|
# 0
|
|
2032
|
# 0
|
|
2022
|
# For other exit status, we show the exception unless
|
|
2033
|
# For other exit status, we show the exception unless
|
|
2023
|
# explicitly silenced, but only in short form.
|
|
2034
|
# explicitly silenced, but only in short form.
|
|
2024
|
if status.code not in (0, None) and not kw['exit_ignore']:
|
|
2035
|
if status.code not in (0, None) and not kw['exit_ignore']:
|
|
2025
|
self.showtraceback(exception_only=True)
|
|
2036
|
self.showtraceback(exception_only=True)
|
|
2026
|
except:
|
|
2037
|
except:
|
|
2027
|
self.showtraceback()
|
|
2038
|
self.showtraceback()
|
|
2028
|
|
|
2039
|
|
|
2029
|
def safe_execfile_ipy(self, fname):
|
|
2040
|
def safe_execfile_ipy(self, fname):
|
|
2030
|
"""Like safe_execfile, but for .ipy files with IPython syntax.
|
|
2041
|
"""Like safe_execfile, but for .ipy files with IPython syntax.
|
|
2031
|
|
|
2042
|
|
|
2032
|
Parameters
|
|
2043
|
Parameters
|
|
2033
|
----------
|
|
2044
|
----------
|
|
2034
|
fname : str
|
|
2045
|
fname : str
|
|
2035
|
The name of the file to execute. The filename must have a
|
|
2046
|
The name of the file to execute. The filename must have a
|
|
2036
|
.ipy extension.
|
|
2047
|
.ipy extension.
|
|
2037
|
"""
|
|
2048
|
"""
|
|
2038
|
fname = os.path.abspath(os.path.expanduser(fname))
|
|
2049
|
fname = os.path.abspath(os.path.expanduser(fname))
|
|
2039
|
|
|
2050
|
|
|
2040
|
# Make sure we have a .py file
|
|
2051
|
# Make sure we have a .py file
|
|
2041
|
if not fname.endswith('.ipy'):
|
|
2052
|
if not fname.endswith('.ipy'):
|
|
2042
|
warn('File must end with .py to be run using execfile: <%s>' % fname)
|
|
2053
|
warn('File must end with .py to be run using execfile: <%s>' % fname)
|
|
2043
|
|
|
2054
|
|
|
2044
|
# Make sure we can open the file
|
|
2055
|
# Make sure we can open the file
|
|
2045
|
try:
|
|
2056
|
try:
|
|
2046
|
with open(fname) as thefile:
|
|
2057
|
with open(fname) as thefile:
|
|
2047
|
pass
|
|
2058
|
pass
|
|
2048
|
except:
|
|
2059
|
except:
|
|
2049
|
warn('Could not open file <%s> for safe execution.' % fname)
|
|
2060
|
warn('Could not open file <%s> for safe execution.' % fname)
|
|
2050
|
return
|
|
2061
|
return
|
|
2051
|
|
|
2062
|
|
|
2052
|
# Find things also in current directory. This is needed to mimic the
|
|
2063
|
# Find things also in current directory. This is needed to mimic the
|
|
2053
|
# behavior of running a script from the system command line, where
|
|
2064
|
# behavior of running a script from the system command line, where
|
|
2054
|
# Python inserts the script's directory into sys.path
|
|
2065
|
# Python inserts the script's directory into sys.path
|
|
2055
|
dname = os.path.dirname(fname)
|
|
2066
|
dname = os.path.dirname(fname)
|
|
2056
|
|
|
2067
|
|
|
2057
|
with prepended_to_syspath(dname):
|
|
2068
|
with prepended_to_syspath(dname):
|
|
2058
|
try:
|
|
2069
|
try:
|
|
2059
|
with open(fname) as thefile:
|
|
2070
|
with open(fname) as thefile:
|
|
2060
|
script = thefile.read()
|
|
2071
|
script = thefile.read()
|
|
2061
|
# self.runlines currently captures all exceptions
|
|
2072
|
# self.runlines currently captures all exceptions
|
|
2062
|
# raise in user code. It would be nice if there were
|
|
2073
|
# raise in user code. It would be nice if there were
|
|
2063
|
# versions of runlines, execfile that did raise, so
|
|
2074
|
# versions of runlines, execfile that did raise, so
|
|
2064
|
# we could catch the errors.
|
|
2075
|
# we could catch the errors.
|
|
2065
|
self.runlines(script, clean=True)
|
|
2076
|
self.runlines(script, clean=True)
|
|
2066
|
except:
|
|
2077
|
except:
|
|
2067
|
self.showtraceback()
|
|
2078
|
self.showtraceback()
|
|
2068
|
warn('Unknown failure executing file: <%s>' % fname)
|
|
2079
|
warn('Unknown failure executing file: <%s>' % fname)
|
|
2069
|
|
|
2080
|
|
|
2070
|
def run_cell(self, cell):
|
|
2081
|
def run_cell(self, cell):
|
|
2071
|
"""Run the contents of an entire multiline 'cell' of code.
|
|
2082
|
"""Run the contents of an entire multiline 'cell' of code.
|
|
2072
|
|
|
2083
|
|
|
2073
|
The cell is split into separate blocks which can be executed
|
|
2084
|
The cell is split into separate blocks which can be executed
|
|
2074
|
individually. Then, based on how many blocks there are, they are
|
|
2085
|
individually. Then, based on how many blocks there are, they are
|
|
2075
|
executed as follows:
|
|
2086
|
executed as follows:
|
|
2076
|
|
|
2087
|
|
|
2077
|
- A single block: 'single' mode.
|
|
2088
|
- A single block: 'single' mode.
|
|
2078
|
|
|
2089
|
|
|
2079
|
If there's more than one block, it depends:
|
|
2090
|
If there's more than one block, it depends:
|
|
2080
|
|
|
2091
|
|
|
2081
|
- if the last one is a single line long, run all but the last in
|
|
2092
|
- if the last one is a single line long, run all but the last in
|
|
2082
|
'exec' mode and the very last one in 'single' mode. This makes it
|
|
2093
|
'exec' mode and the very last one in 'single' mode. This makes it
|
|
2083
|
easy to type simple expressions at the end to see computed values.
|
|
2094
|
easy to type simple expressions at the end to see computed values.
|
|
2084
|
- otherwise (last one is also multiline), run all in 'exec' mode
|
|
2095
|
- otherwise (last one is also multiline), run all in 'exec' mode
|
|
2085
|
|
|
2096
|
|
|
2086
|
When code is executed in 'single' mode, :func:`sys.displayhook` fires,
|
|
2097
|
When code is executed in 'single' mode, :func:`sys.displayhook` fires,
|
|
2087
|
results are displayed and output prompts are computed. In 'exec' mode,
|
|
2098
|
results are displayed and output prompts are computed. In 'exec' mode,
|
|
2088
|
no results are displayed unless :func:`print` is called explicitly;
|
|
2099
|
no results are displayed unless :func:`print` is called explicitly;
|
|
2089
|
this mode is more akin to running a script.
|
|
2100
|
this mode is more akin to running a script.
|
|
2090
|
|
|
2101
|
|
|
2091
|
Parameters
|
|
2102
|
Parameters
|
|
2092
|
----------
|
|
2103
|
----------
|
|
2093
|
cell : str
|
|
2104
|
cell : str
|
|
2094
|
A single or multiline string.
|
|
2105
|
A single or multiline string.
|
|
2095
|
"""
|
|
2106
|
"""
|
|
2096
|
# We need to break up the input into executable blocks that can be run
|
|
2107
|
# We need to break up the input into executable blocks that can be run
|
|
2097
|
# in 'single' mode, to provide comfortable user behavior.
|
|
2108
|
# in 'single' mode, to provide comfortable user behavior.
|
|
2098
|
blocks = self.input_splitter.split_blocks(cell)
|
|
2109
|
blocks = self.input_splitter.split_blocks(cell)
|
|
2099
|
|
|
2110
|
|
|
2100
|
if not blocks:
|
|
2111
|
if not blocks:
|
|
2101
|
return
|
|
2112
|
return
|
|
2102
|
|
|
2113
|
|
|
2103
|
# Single-block input should behave like an interactive prompt
|
|
2114
|
# Single-block input should behave like an interactive prompt
|
|
2104
|
if len(blocks) == 1:
|
|
2115
|
if len(blocks) == 1:
|
|
2105
|
self.runlines(blocks[0])
|
|
2116
|
self.runlines(blocks[0])
|
|
2106
|
return
|
|
2117
|
return
|
|
2107
|
|
|
2118
|
|
|
2108
|
# In multi-block input, if the last block is a simple (one-two lines)
|
|
2119
|
# In multi-block input, if the last block is a simple (one-two lines)
|
|
2109
|
# expression, run it in single mode so it produces output. Otherwise
|
|
2120
|
# expression, run it in single mode so it produces output. Otherwise
|
|
2110
|
# just feed the whole thing to runcode.
|
|
2121
|
# just feed the whole thing to runcode.
|
|
2111
|
# This seems like a reasonable usability design.
|
|
2122
|
# This seems like a reasonable usability design.
|
|
2112
|
last = blocks[-1]
|
|
2123
|
last = blocks[-1]
|
|
2113
|
if len(last.splitlines()) < 2:
|
|
2124
|
if len(last.splitlines()) < 2:
|
|
2114
|
self.runcode('\n'.join(blocks[:-1]))
|
|
2125
|
self.runcode('\n'.join(blocks[:-1]))
|
|
2115
|
self.runlines(last)
|
|
2126
|
self.runlines(last)
|
|
2116
|
else:
|
|
2127
|
else:
|
|
2117
|
self.runcode(cell)
|
|
2128
|
self.runcode(cell)
|
|
2118
|
|
|
2129
|
|
|
2119
|
def runlines(self, lines, clean=False):
|
|
2130
|
def runlines(self, lines, clean=False):
|
|
2120
|
"""Run a string of one or more lines of source.
|
|
2131
|
"""Run a string of one or more lines of source.
|
|
2121
|
|
|
2132
|
|
|
2122
|
This method is capable of running a string containing multiple source
|
|
2133
|
This method is capable of running a string containing multiple source
|
|
2123
|
lines, as if they had been entered at the IPython prompt. Since it
|
|
2134
|
lines, as if they had been entered at the IPython prompt. Since it
|
|
2124
|
exposes IPython's processing machinery, the given strings can contain
|
|
2135
|
exposes IPython's processing machinery, the given strings can contain
|
|
2125
|
magic calls (%magic), special shell access (!cmd), etc.
|
|
2136
|
magic calls (%magic), special shell access (!cmd), etc.
|
|
2126
|
"""
|
|
2137
|
"""
|
|
2127
|
|
|
2138
|
|
|
2128
|
if isinstance(lines, (list, tuple)):
|
|
2139
|
if isinstance(lines, (list, tuple)):
|
|
2129
|
lines = '\n'.join(lines)
|
|
2140
|
lines = '\n'.join(lines)
|
|
2130
|
|
|
2141
|
|
|
2131
|
if clean:
|
|
2142
|
if clean:
|
|
2132
|
lines = self._cleanup_ipy_script(lines)
|
|
2143
|
lines = self._cleanup_ipy_script(lines)
|
|
2133
|
|
|
2144
|
|
|
2134
|
# We must start with a clean buffer, in case this is run from an
|
|
2145
|
# We must start with a clean buffer, in case this is run from an
|
|
2135
|
# interactive IPython session (via a magic, for example).
|
|
2146
|
# interactive IPython session (via a magic, for example).
|
|
2136
|
self.resetbuffer()
|
|
2147
|
self.resetbuffer()
|
|
2137
|
lines = lines.splitlines()
|
|
2148
|
lines = lines.splitlines()
|
|
2138
|
more = 0
|
|
2149
|
more = 0
|
|
2139
|
with nested(self.builtin_trap, self.display_trap):
|
|
2150
|
with nested(self.builtin_trap, self.display_trap):
|
|
2140
|
for line in lines:
|
|
2151
|
for line in lines:
|
|
2141
|
# skip blank lines so we don't mess up the prompt counter, but
|
|
2152
|
# skip blank lines so we don't mess up the prompt counter, but
|
|
2142
|
# do NOT skip even a blank line if we are in a code block (more
|
|
2153
|
# do NOT skip even a blank line if we are in a code block (more
|
|
2143
|
# is true)
|
|
2154
|
# is true)
|
|
2144
|
|
|
2155
|
|
|
2145
|
if line or more:
|
|
2156
|
if line or more:
|
|
2146
|
# push to raw history, so hist line numbers stay in sync
|
|
2157
|
# push to raw history, so hist line numbers stay in sync
|
|
2147
|
self.input_hist_raw.append(line + '\n')
|
|
2158
|
self.input_hist_raw.append(line + '\n')
|
|
2148
|
prefiltered = self.prefilter_manager.prefilter_lines(line,
|
|
2159
|
prefiltered = self.prefilter_manager.prefilter_lines(line,
|
|
2149
|
more)
|
|
2160
|
more)
|
|
2150
|
more = self.push_line(prefiltered)
|
|
2161
|
more = self.push_line(prefiltered)
|
|
2151
|
# IPython's runsource returns None if there was an error
|
|
2162
|
# IPython's runsource returns None if there was an error
|
|
2152
|
# compiling the code. This allows us to stop processing
|
|
2163
|
# compiling the code. This allows us to stop processing
|
|
2153
|
# right away, so the user gets the error message at the
|
|
2164
|
# right away, so the user gets the error message at the
|
|
2154
|
# right place.
|
|
2165
|
# right place.
|
|
2155
|
if more is None:
|
|
2166
|
if more is None:
|
|
2156
|
break
|
|
2167
|
break
|
|
2157
|
else:
|
|
2168
|
else:
|
|
2158
|
self.input_hist_raw.append("\n")
|
|
2169
|
self.input_hist_raw.append("\n")
|
|
2159
|
# final newline in case the input didn't have it, so that the code
|
|
2170
|
# final newline in case the input didn't have it, so that the code
|
|
2160
|
# actually does get executed
|
|
2171
|
# actually does get executed
|
|
2161
|
if more:
|
|
2172
|
if more:
|
|
2162
|
self.push_line('\n')
|
|
2173
|
self.push_line('\n')
|
|
2163
|
|
|
2174
|
|
|
2164
|
def runsource(self, source, filename='<input>', symbol='single'):
|
|
2175
|
def runsource(self, source, filename='<input>', symbol='single'):
|
|
2165
|
"""Compile and run some source in the interpreter.
|
|
2176
|
"""Compile and run some source in the interpreter.
|
|
2166
|
|
|
2177
|
|
|
2167
|
Arguments are as for compile_command().
|
|
2178
|
Arguments are as for compile_command().
|
|
2168
|
|
|
2179
|
|
|
2169
|
One several things can happen:
|
|
2180
|
One several things can happen:
|
|
2170
|
|
|
2181
|
|
|
2171
|
1) The input is incorrect; compile_command() raised an
|
|
2182
|
1) The input is incorrect; compile_command() raised an
|
|
2172
|
exception (SyntaxError or OverflowError). A syntax traceback
|
|
2183
|
exception (SyntaxError or OverflowError). A syntax traceback
|
|
2173
|
will be printed by calling the showsyntaxerror() method.
|
|
2184
|
will be printed by calling the showsyntaxerror() method.
|
|
2174
|
|
|
2185
|
|
|
2175
|
2) The input is incomplete, and more input is required;
|
|
2186
|
2) The input is incomplete, and more input is required;
|
|
2176
|
compile_command() returned None. Nothing happens.
|
|
2187
|
compile_command() returned None. Nothing happens.
|
|
2177
|
|
|
2188
|
|
|
2178
|
3) The input is complete; compile_command() returned a code
|
|
2189
|
3) The input is complete; compile_command() returned a code
|
|
2179
|
object. The code is executed by calling self.runcode() (which
|
|
2190
|
object. The code is executed by calling self.runcode() (which
|
|
2180
|
also handles run-time exceptions, except for SystemExit).
|
|
2191
|
also handles run-time exceptions, except for SystemExit).
|
|
2181
|
|
|
2192
|
|
|
2182
|
The return value is:
|
|
2193
|
The return value is:
|
|
2183
|
|
|
2194
|
|
|
2184
|
- True in case 2
|
|
2195
|
- True in case 2
|
|
2185
|
|
|
2196
|
|
|
2186
|
- False in the other cases, unless an exception is raised, where
|
|
2197
|
- False in the other cases, unless an exception is raised, where
|
|
2187
|
None is returned instead. This can be used by external callers to
|
|
2198
|
None is returned instead. This can be used by external callers to
|
|
2188
|
know whether to continue feeding input or not.
|
|
2199
|
know whether to continue feeding input or not.
|
|
2189
|
|
|
2200
|
|
|
2190
|
The return value can be used to decide whether to use sys.ps1 or
|
|
2201
|
The return value can be used to decide whether to use sys.ps1 or
|
|
2191
|
sys.ps2 to prompt the next line."""
|
|
2202
|
sys.ps2 to prompt the next line."""
|
|
2192
|
|
|
2203
|
|
|
2193
|
# if the source code has leading blanks, add 'if 1:\n' to it
|
|
2204
|
# if the source code has leading blanks, add 'if 1:\n' to it
|
|
2194
|
# this allows execution of indented pasted code. It is tempting
|
|
2205
|
# this allows execution of indented pasted code. It is tempting
|
|
2195
|
# to add '\n' at the end of source to run commands like ' a=1'
|
|
2206
|
# to add '\n' at the end of source to run commands like ' a=1'
|
|
2196
|
# directly, but this fails for more complicated scenarios
|
|
2207
|
# directly, but this fails for more complicated scenarios
|
|
2197
|
source=source.encode(self.stdin_encoding)
|
|
2208
|
source=source.encode(self.stdin_encoding)
|
|
2198
|
if source[:1] in [' ', '\t']:
|
|
2209
|
if source[:1] in [' ', '\t']:
|
|
2199
|
source = 'if 1:\n%s' % source
|
|
2210
|
source = 'if 1:\n%s' % source
|
|
2200
|
|
|
2211
|
|
|
2201
|
try:
|
|
2212
|
try:
|
|
2202
|
code = self.compile(source,filename,symbol)
|
|
2213
|
code = self.compile(source,filename,symbol)
|
|
2203
|
except (OverflowError, SyntaxError, ValueError, TypeError, MemoryError):
|
|
2214
|
except (OverflowError, SyntaxError, ValueError, TypeError, MemoryError):
|
|
2204
|
# Case 1
|
|
2215
|
# Case 1
|
|
2205
|
self.showsyntaxerror(filename)
|
|
2216
|
self.showsyntaxerror(filename)
|
|
2206
|
return None
|
|
2217
|
return None
|
|
2207
|
|
|
2218
|
|
|
2208
|
if code is None:
|
|
2219
|
if code is None:
|
|
2209
|
# Case 2
|
|
2220
|
# Case 2
|
|
2210
|
return True
|
|
2221
|
return True
|
|
2211
|
|
|
2222
|
|
|
2212
|
# Case 3
|
|
2223
|
# Case 3
|
|
2213
|
# We store the code object so that threaded shells and
|
|
2224
|
# We store the code object so that threaded shells and
|
|
2214
|
# custom exception handlers can access all this info if needed.
|
|
2225
|
# custom exception handlers can access all this info if needed.
|
|
2215
|
# The source corresponding to this can be obtained from the
|
|
2226
|
# The source corresponding to this can be obtained from the
|
|
2216
|
# buffer attribute as '\n'.join(self.buffer).
|
|
2227
|
# buffer attribute as '\n'.join(self.buffer).
|
|
2217
|
self.code_to_run = code
|
|
2228
|
self.code_to_run = code
|
|
2218
|
# now actually execute the code object
|
|
2229
|
# now actually execute the code object
|
|
2219
|
if self.runcode(code) == 0:
|
|
2230
|
if self.runcode(code) == 0:
|
|
2220
|
return False
|
|
2231
|
return False
|
|
2221
|
else:
|
|
2232
|
else:
|
|
2222
|
return None
|
|
2233
|
return None
|
|
2223
|
|
|
2234
|
|
|
2224
|
def runcode(self, code_obj):
|
|
2235
|
def runcode(self, code_obj):
|
|
2225
|
"""Execute a code object.
|
|
2236
|
"""Execute a code object.
|
|
2226
|
|
|
2237
|
|
|
2227
|
When an exception occurs, self.showtraceback() is called to display a
|
|
2238
|
When an exception occurs, self.showtraceback() is called to display a
|
|
2228
|
traceback.
|
|
2239
|
traceback.
|
|
2229
|
|
|
2240
|
|
|
2230
|
Return value: a flag indicating whether the code to be run completed
|
|
2241
|
Return value: a flag indicating whether the code to be run completed
|
|
2231
|
successfully:
|
|
2242
|
successfully:
|
|
2232
|
|
|
2243
|
|
|
2233
|
- 0: successful execution.
|
|
2244
|
- 0: successful execution.
|
|
2234
|
- 1: an error occurred.
|
|
2245
|
- 1: an error occurred.
|
|
2235
|
"""
|
|
2246
|
"""
|
|
2236
|
|
|
2247
|
|
|
2237
|
# It's also possible that we've been fed a plain string. In that case,
|
|
2248
|
# It's also possible that we've been fed a plain string. In that case,
|
|
2238
|
# we must store it in the input history.
|
|
2249
|
# we must store it in the input history.
|
|
2239
|
if isinstance(code_obj, basestring):
|
|
2250
|
if isinstance(code_obj, basestring):
|
|
2240
|
self.input_hist_raw.append(code_obj)
|
|
2251
|
self.input_hist_raw.append(code_obj)
|
|
2241
|
|
|
2252
|
|
|
2242
|
# Set our own excepthook in case the user code tries to call it
|
|
2253
|
# Set our own excepthook in case the user code tries to call it
|
|
2243
|
# directly, so that the IPython crash handler doesn't get triggered
|
|
2254
|
# directly, so that the IPython crash handler doesn't get triggered
|
|
2244
|
old_excepthook,sys.excepthook = sys.excepthook, self.excepthook
|
|
2255
|
old_excepthook,sys.excepthook = sys.excepthook, self.excepthook
|
|
2245
|
|
|
2256
|
|
|
2246
|
# we save the original sys.excepthook in the instance, in case config
|
|
2257
|
# we save the original sys.excepthook in the instance, in case config
|
|
2247
|
# code (such as magics) needs access to it.
|
|
2258
|
# code (such as magics) needs access to it.
|
|
2248
|
self.sys_excepthook = old_excepthook
|
|
2259
|
self.sys_excepthook = old_excepthook
|
|
2249
|
outflag = 1 # happens in more places, so it's easier as default
|
|
2260
|
outflag = 1 # happens in more places, so it's easier as default
|
|
2250
|
try:
|
|
2261
|
try:
|
|
2251
|
try:
|
|
2262
|
try:
|
|
2252
|
self.hooks.pre_runcode_hook()
|
|
2263
|
self.hooks.pre_runcode_hook()
|
|
2253
|
#rprint('Running code') # dbg
|
|
2264
|
#rprint('Running code') # dbg
|
|
2254
|
exec code_obj in self.user_global_ns, self.user_ns
|
|
2265
|
exec code_obj in self.user_global_ns, self.user_ns
|
|
2255
|
finally:
|
|
2266
|
finally:
|
|
2256
|
# Reset our crash handler in place
|
|
2267
|
# Reset our crash handler in place
|
|
2257
|
sys.excepthook = old_excepthook
|
|
2268
|
sys.excepthook = old_excepthook
|
|
2258
|
except SystemExit:
|
|
2269
|
except SystemExit:
|
|
2259
|
self.resetbuffer()
|
|
2270
|
self.resetbuffer()
|
|
2260
|
self.showtraceback(exception_only=True)
|
|
2271
|
self.showtraceback(exception_only=True)
|
|
2261
|
warn("To exit: use any of 'exit', 'quit', %Exit or Ctrl-D.", level=1)
|
|
2272
|
warn("To exit: use any of 'exit', 'quit', %Exit or Ctrl-D.", level=1)
|
|
2262
|
except self.custom_exceptions:
|
|
2273
|
except self.custom_exceptions:
|
|
2263
|
etype,value,tb = sys.exc_info()
|
|
2274
|
etype,value,tb = sys.exc_info()
|
|
2264
|
self.CustomTB(etype,value,tb)
|
|
2275
|
self.CustomTB(etype,value,tb)
|
|
2265
|
except:
|
|
2276
|
except:
|
|
2266
|
self.showtraceback()
|
|
2277
|
self.showtraceback()
|
|
2267
|
else:
|
|
2278
|
else:
|
|
2268
|
outflag = 0
|
|
2279
|
outflag = 0
|
|
2269
|
if softspace(sys.stdout, 0):
|
|
2280
|
if softspace(sys.stdout, 0):
|
|
2270
|
print
|
|
2281
|
print
|
|
|
|
|
2282
|
|
|
|
|
|
2283
|
# Execute any registered post-execution functions. Here, any errors
|
|
|
|
|
2284
|
# are reported only minimally and just on the terminal, because the
|
|
|
|
|
2285
|
# main exception channel may be occupied with a user traceback.
|
|
|
|
|
2286
|
# FIXME: we need to think this mechanism a little more carefully.
|
|
|
|
|
2287
|
for func in self._post_execute:
|
|
|
|
|
2288
|
try:
|
|
|
|
|
2289
|
func()
|
|
|
|
|
2290
|
except:
|
|
|
|
|
2291
|
head = '[ ERROR ] Evaluating post_execute function: %s' % func
|
|
|
|
|
2292
|
print >> io.Term.cout, head
|
|
|
|
|
2293
|
print >> io.Term.cout, self._simple_error()
|
|
|
|
|
2294
|
print >> io.Term.cout, 'Removing from post_execute'
|
|
|
|
|
2295
|
self._post_execute.remove(func)
|
|
|
|
|
2296
|
|
|
2271
|
# Flush out code object which has been run (and source)
|
|
2297
|
# Flush out code object which has been run (and source)
|
|
2272
|
self.code_to_run = None
|
|
2298
|
self.code_to_run = None
|
|
2273
|
return outflag
|
|
2299
|
return outflag
|
|
2274
|
|
|
2300
|
|
|
2275
|
def push_line(self, line):
|
|
2301
|
def push_line(self, line):
|
|
2276
|
"""Push a line to the interpreter.
|
|
2302
|
"""Push a line to the interpreter.
|
|
2277
|
|
|
2303
|
|
|
2278
|
The line should not have a trailing newline; it may have
|
|
2304
|
The line should not have a trailing newline; it may have
|
|
2279
|
internal newlines. The line is appended to a buffer and the
|
|
2305
|
internal newlines. The line is appended to a buffer and the
|
|
2280
|
interpreter's runsource() method is called with the
|
|
2306
|
interpreter's runsource() method is called with the
|
|
2281
|
concatenated contents of the buffer as source. If this
|
|
2307
|
concatenated contents of the buffer as source. If this
|
|
2282
|
indicates that the command was executed or invalid, the buffer
|
|
2308
|
indicates that the command was executed or invalid, the buffer
|
|
2283
|
is reset; otherwise, the command is incomplete, and the buffer
|
|
2309
|
is reset; otherwise, the command is incomplete, and the buffer
|
|
2284
|
is left as it was after the line was appended. The return
|
|
2310
|
is left as it was after the line was appended. The return
|
|
2285
|
value is 1 if more input is required, 0 if the line was dealt
|
|
2311
|
value is 1 if more input is required, 0 if the line was dealt
|
|
2286
|
with in some way (this is the same as runsource()).
|
|
2312
|
with in some way (this is the same as runsource()).
|
|
2287
|
"""
|
|
2313
|
"""
|
|
2288
|
|
|
2314
|
|
|
2289
|
# autoindent management should be done here, and not in the
|
|
2315
|
# autoindent management should be done here, and not in the
|
|
2290
|
# interactive loop, since that one is only seen by keyboard input. We
|
|
2316
|
# interactive loop, since that one is only seen by keyboard input. We
|
|
2291
|
# need this done correctly even for code run via runlines (which uses
|
|
2317
|
# need this done correctly even for code run via runlines (which uses
|
|
2292
|
# push).
|
|
2318
|
# push).
|
|
2293
|
|
|
2319
|
|
|
2294
|
#print 'push line: <%s>' % line # dbg
|
|
2320
|
#print 'push line: <%s>' % line # dbg
|
|
2295
|
for subline in line.splitlines():
|
|
2321
|
for subline in line.splitlines():
|
|
2296
|
self._autoindent_update(subline)
|
|
2322
|
self._autoindent_update(subline)
|
|
2297
|
self.buffer.append(line)
|
|
2323
|
self.buffer.append(line)
|
|
2298
|
more = self.runsource('\n'.join(self.buffer), self.filename)
|
|
2324
|
more = self.runsource('\n'.join(self.buffer), self.filename)
|
|
2299
|
if not more:
|
|
2325
|
if not more:
|
|
2300
|
self.resetbuffer()
|
|
2326
|
self.resetbuffer()
|
|
2301
|
return more
|
|
2327
|
return more
|
|
2302
|
|
|
2328
|
|
|
2303
|
def resetbuffer(self):
|
|
2329
|
def resetbuffer(self):
|
|
2304
|
"""Reset the input buffer."""
|
|
2330
|
"""Reset the input buffer."""
|
|
2305
|
self.buffer[:] = []
|
|
2331
|
self.buffer[:] = []
|
|
2306
|
|
|
2332
|
|
|
2307
|
def _is_secondary_block_start(self, s):
|
|
2333
|
def _is_secondary_block_start(self, s):
|
|
2308
|
if not s.endswith(':'):
|
|
2334
|
if not s.endswith(':'):
|
|
2309
|
return False
|
|
2335
|
return False
|
|
2310
|
if (s.startswith('elif') or
|
|
2336
|
if (s.startswith('elif') or
|
|
2311
|
s.startswith('else') or
|
|
2337
|
s.startswith('else') or
|
|
2312
|
s.startswith('except') or
|
|
2338
|
s.startswith('except') or
|
|
2313
|
s.startswith('finally')):
|
|
2339
|
s.startswith('finally')):
|
|
2314
|
return True
|
|
2340
|
return True
|
|
2315
|
|
|
2341
|
|
|
2316
|
def _cleanup_ipy_script(self, script):
|
|
2342
|
def _cleanup_ipy_script(self, script):
|
|
2317
|
"""Make a script safe for self.runlines()
|
|
2343
|
"""Make a script safe for self.runlines()
|
|
2318
|
|
|
2344
|
|
|
2319
|
Currently, IPython is lines based, with blocks being detected by
|
|
2345
|
Currently, IPython is lines based, with blocks being detected by
|
|
2320
|
empty lines. This is a problem for block based scripts that may
|
|
2346
|
empty lines. This is a problem for block based scripts that may
|
|
2321
|
not have empty lines after blocks. This script adds those empty
|
|
2347
|
not have empty lines after blocks. This script adds those empty
|
|
2322
|
lines to make scripts safe for running in the current line based
|
|
2348
|
lines to make scripts safe for running in the current line based
|
|
2323
|
IPython.
|
|
2349
|
IPython.
|
|
2324
|
"""
|
|
2350
|
"""
|
|
2325
|
res = []
|
|
2351
|
res = []
|
|
2326
|
lines = script.splitlines()
|
|
2352
|
lines = script.splitlines()
|
|
2327
|
level = 0
|
|
2353
|
level = 0
|
|
2328
|
|
|
2354
|
|
|
2329
|
for l in lines:
|
|
2355
|
for l in lines:
|
|
2330
|
lstripped = l.lstrip()
|
|
2356
|
lstripped = l.lstrip()
|
|
2331
|
stripped = l.strip()
|
|
2357
|
stripped = l.strip()
|
|
2332
|
if not stripped:
|
|
2358
|
if not stripped:
|
|
2333
|
continue
|
|
2359
|
continue
|
|
2334
|
newlevel = len(l) - len(lstripped)
|
|
2360
|
newlevel = len(l) - len(lstripped)
|
|
2335
|
if level > 0 and newlevel == 0 and \
|
|
2361
|
if level > 0 and newlevel == 0 and \
|
|
2336
|
not self._is_secondary_block_start(stripped):
|
|
2362
|
not self._is_secondary_block_start(stripped):
|
|
2337
|
# add empty line
|
|
2363
|
# add empty line
|
|
2338
|
res.append('')
|
|
2364
|
res.append('')
|
|
2339
|
res.append(l)
|
|
2365
|
res.append(l)
|
|
2340
|
level = newlevel
|
|
2366
|
level = newlevel
|
|
2341
|
|
|
2367
|
|
|
2342
|
return '\n'.join(res) + '\n'
|
|
2368
|
return '\n'.join(res) + '\n'
|
|
2343
|
|
|
2369
|
|
|
2344
|
def _autoindent_update(self,line):
|
|
2370
|
def _autoindent_update(self,line):
|
|
2345
|
"""Keep track of the indent level."""
|
|
2371
|
"""Keep track of the indent level."""
|
|
2346
|
|
|
2372
|
|
|
2347
|
#debugx('line')
|
|
2373
|
#debugx('line')
|
|
2348
|
#debugx('self.indent_current_nsp')
|
|
2374
|
#debugx('self.indent_current_nsp')
|
|
2349
|
if self.autoindent:
|
|
2375
|
if self.autoindent:
|
|
2350
|
if line:
|
|
2376
|
if line:
|
|
2351
|
inisp = num_ini_spaces(line)
|
|
2377
|
inisp = num_ini_spaces(line)
|
|
2352
|
if inisp < self.indent_current_nsp:
|
|
2378
|
if inisp < self.indent_current_nsp:
|
|
2353
|
self.indent_current_nsp = inisp
|
|
2379
|
self.indent_current_nsp = inisp
|
|
2354
|
|
|
2380
|
|
|
2355
|
if line[-1] == ':':
|
|
2381
|
if line[-1] == ':':
|
|
2356
|
self.indent_current_nsp += 4
|
|
2382
|
self.indent_current_nsp += 4
|
|
2357
|
elif dedent_re.match(line):
|
|
2383
|
elif dedent_re.match(line):
|
|
2358
|
self.indent_current_nsp -= 4
|
|
2384
|
self.indent_current_nsp -= 4
|
|
2359
|
else:
|
|
2385
|
else:
|
|
2360
|
self.indent_current_nsp = 0
|
|
2386
|
self.indent_current_nsp = 0
|
|
2361
|
|
|
2387
|
|
|
2362
|
#-------------------------------------------------------------------------
|
|
2388
|
#-------------------------------------------------------------------------
|
|
2363
|
# Things related to GUI support and pylab
|
|
2389
|
# Things related to GUI support and pylab
|
|
2364
|
#-------------------------------------------------------------------------
|
|
2390
|
#-------------------------------------------------------------------------
|
|
2365
|
|
|
2391
|
|
|
2366
|
def enable_pylab(self, gui=None):
|
|
2392
|
def enable_pylab(self, gui=None):
|
|
2367
|
raise NotImplementedError('Implement enable_pylab in a subclass')
|
|
2393
|
raise NotImplementedError('Implement enable_pylab in a subclass')
|
|
2368
|
|
|
2394
|
|
|
2369
|
#-------------------------------------------------------------------------
|
|
2395
|
#-------------------------------------------------------------------------
|
|
2370
|
# Utilities
|
|
2396
|
# Utilities
|
|
2371
|
#-------------------------------------------------------------------------
|
|
2397
|
#-------------------------------------------------------------------------
|
|
2372
|
|
|
2398
|
|
|
2373
|
def var_expand(self,cmd,depth=0):
|
|
2399
|
def var_expand(self,cmd,depth=0):
|
|
2374
|
"""Expand python variables in a string.
|
|
2400
|
"""Expand python variables in a string.
|
|
2375
|
|
|
2401
|
|
|
2376
|
The depth argument indicates how many frames above the caller should
|
|
2402
|
The depth argument indicates how many frames above the caller should
|
|
2377
|
be walked to look for the local namespace where to expand variables.
|
|
2403
|
be walked to look for the local namespace where to expand variables.
|
|
2378
|
|
|
2404
|
|
|
2379
|
The global namespace for expansion is always the user's interactive
|
|
2405
|
The global namespace for expansion is always the user's interactive
|
|
2380
|
namespace.
|
|
2406
|
namespace.
|
|
2381
|
"""
|
|
2407
|
"""
|
|
2382
|
|
|
2408
|
|
|
2383
|
return str(ItplNS(cmd,
|
|
2409
|
return str(ItplNS(cmd,
|
|
2384
|
self.user_ns, # globals
|
|
2410
|
self.user_ns, # globals
|
|
2385
|
# Skip our own frame in searching for locals:
|
|
2411
|
# Skip our own frame in searching for locals:
|
|
2386
|
sys._getframe(depth+1).f_locals # locals
|
|
2412
|
sys._getframe(depth+1).f_locals # locals
|
|
2387
|
))
|
|
2413
|
))
|
|
2388
|
|
|
2414
|
|
|
2389
|
def mktempfile(self,data=None):
|
|
2415
|
def mktempfile(self,data=None):
|
|
2390
|
"""Make a new tempfile and return its filename.
|
|
2416
|
"""Make a new tempfile and return its filename.
|
|
2391
|
|
|
2417
|
|
|
2392
|
This makes a call to tempfile.mktemp, but it registers the created
|
|
2418
|
This makes a call to tempfile.mktemp, but it registers the created
|
|
2393
|
filename internally so ipython cleans it up at exit time.
|
|
2419
|
filename internally so ipython cleans it up at exit time.
|
|
2394
|
|
|
2420
|
|
|
2395
|
Optional inputs:
|
|
2421
|
Optional inputs:
|
|
2396
|
|
|
2422
|
|
|
2397
|
- data(None): if data is given, it gets written out to the temp file
|
|
2423
|
- data(None): if data is given, it gets written out to the temp file
|
|
2398
|
immediately, and the file is closed again."""
|
|
2424
|
immediately, and the file is closed again."""
|
|
2399
|
|
|
2425
|
|
|
2400
|
filename = tempfile.mktemp('.py','ipython_edit_')
|
|
2426
|
filename = tempfile.mktemp('.py','ipython_edit_')
|
|
2401
|
self.tempfiles.append(filename)
|
|
2427
|
self.tempfiles.append(filename)
|
|
2402
|
|
|
2428
|
|
|
2403
|
if data:
|
|
2429
|
if data:
|
|
2404
|
tmp_file = open(filename,'w')
|
|
2430
|
tmp_file = open(filename,'w')
|
|
2405
|
tmp_file.write(data)
|
|
2431
|
tmp_file.write(data)
|
|
2406
|
tmp_file.close()
|
|
2432
|
tmp_file.close()
|
|
2407
|
return filename
|
|
2433
|
return filename
|
|
2408
|
|
|
2434
|
|
|
2409
|
# TODO: This should be removed when Term is refactored.
|
|
2435
|
# TODO: This should be removed when Term is refactored.
|
|
2410
|
def write(self,data):
|
|
2436
|
def write(self,data):
|
|
2411
|
"""Write a string to the default output"""
|
|
2437
|
"""Write a string to the default output"""
|
|
2412
|
io.Term.cout.write(data)
|
|
2438
|
io.Term.cout.write(data)
|
|
2413
|
|
|
2439
|
|
|
2414
|
# TODO: This should be removed when Term is refactored.
|
|
2440
|
# TODO: This should be removed when Term is refactored.
|
|
2415
|
def write_err(self,data):
|
|
2441
|
def write_err(self,data):
|
|
2416
|
"""Write a string to the default error output"""
|
|
2442
|
"""Write a string to the default error output"""
|
|
2417
|
io.Term.cerr.write(data)
|
|
2443
|
io.Term.cerr.write(data)
|
|
2418
|
|
|
2444
|
|
|
2419
|
def ask_yes_no(self,prompt,default=True):
|
|
2445
|
def ask_yes_no(self,prompt,default=True):
|
|
2420
|
if self.quiet:
|
|
2446
|
if self.quiet:
|
|
2421
|
return True
|
|
2447
|
return True
|
|
2422
|
return ask_yes_no(prompt,default)
|
|
2448
|
return ask_yes_no(prompt,default)
|
|
2423
|
|
|
2449
|
|
|
2424
|
def show_usage(self):
|
|
2450
|
def show_usage(self):
|
|
2425
|
"""Show a usage message"""
|
|
2451
|
"""Show a usage message"""
|
|
2426
|
page.page(IPython.core.usage.interactive_usage)
|
|
2452
|
page.page(IPython.core.usage.interactive_usage)
|
|
2427
|
|
|
2453
|
|
|
2428
|
#-------------------------------------------------------------------------
|
|
2454
|
#-------------------------------------------------------------------------
|
|
2429
|
# Things related to IPython exiting
|
|
2455
|
# Things related to IPython exiting
|
|
2430
|
#-------------------------------------------------------------------------
|
|
2456
|
#-------------------------------------------------------------------------
|
|
2431
|
def atexit_operations(self):
|
|
2457
|
def atexit_operations(self):
|
|
2432
|
"""This will be executed at the time of exit.
|
|
2458
|
"""This will be executed at the time of exit.
|
|
2433
|
|
|
2459
|
|
|
2434
|
Cleanup operations and saving of persistent data that is done
|
|
2460
|
Cleanup operations and saving of persistent data that is done
|
|
2435
|
unconditionally by IPython should be performed here.
|
|
2461
|
unconditionally by IPython should be performed here.
|
|
2436
|
|
|
2462
|
|
|
2437
|
For things that may depend on startup flags or platform specifics (such
|
|
2463
|
For things that may depend on startup flags or platform specifics (such
|
|
2438
|
as having readline or not), register a separate atexit function in the
|
|
2464
|
as having readline or not), register a separate atexit function in the
|
|
2439
|
code that has the appropriate information, rather than trying to
|
|
2465
|
code that has the appropriate information, rather than trying to
|
|
2440
|
clutter
|
|
2466
|
clutter
|
|
2441
|
"""
|
|
2467
|
"""
|
|
2442
|
# Cleanup all tempfiles left around
|
|
2468
|
# Cleanup all tempfiles left around
|
|
2443
|
for tfile in self.tempfiles:
|
|
2469
|
for tfile in self.tempfiles:
|
|
2444
|
try:
|
|
2470
|
try:
|
|
2445
|
os.unlink(tfile)
|
|
2471
|
os.unlink(tfile)
|
|
2446
|
except OSError:
|
|
2472
|
except OSError:
|
|
2447
|
pass
|
|
2473
|
pass
|
|
2448
|
|
|
2474
|
|
|
2449
|
# Clear all user namespaces to release all references cleanly.
|
|
2475
|
# Clear all user namespaces to release all references cleanly.
|
|
2450
|
self.reset()
|
|
2476
|
self.reset()
|
|
2451
|
|
|
2477
|
|
|
2452
|
# Run user hooks
|
|
2478
|
# Run user hooks
|
|
2453
|
self.hooks.shutdown_hook()
|
|
2479
|
self.hooks.shutdown_hook()
|
|
2454
|
|
|
2480
|
|
|
2455
|
def cleanup(self):
|
|
2481
|
def cleanup(self):
|
|
2456
|
self.restore_sys_module_state()
|
|
2482
|
self.restore_sys_module_state()
|
|
2457
|
|
|
2483
|
|
|
2458
|
|
|
2484
|
|
|
2459
|
class InteractiveShellABC(object):
|
|
2485
|
class InteractiveShellABC(object):
|
|
2460
|
"""An abstract base class for InteractiveShell."""
|
|
2486
|
"""An abstract base class for InteractiveShell."""
|
|
2461
|
__metaclass__ = abc.ABCMeta
|
|
2487
|
__metaclass__ = abc.ABCMeta
|
|
2462
|
|
|
2488
|
|
|
2463
|
InteractiveShellABC.register(InteractiveShell)
|
|
2489
|
InteractiveShellABC.register(InteractiveShell)
|